Skip to content

Commit e722b33

Browse files
committed
Implement floating point memory instructions
1 parent c4a67e8 commit e722b33

1 file changed

Lines changed: 60 additions & 7 deletions

File tree

lib/fizzy/execute.cpp

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,23 @@ inline DstT extend(SrcT in) noexcept
324324
return DstT{in};
325325
}
326326

327+
template<>
328+
inline float extend(float in) noexcept
329+
{
330+
return in;
331+
}
332+
333+
template<>
334+
inline double extend(double in) noexcept
335+
{
336+
return in;
337+
}
338+
327339
template <typename DstT, typename SrcT = DstT>
328340
inline bool load_from_memory(
329341
bytes_view memory, OperandStack& stack, const uint8_t*& immediates) noexcept
330342
{
331-
const auto address = static_cast<uint32_t>(stack.pop());
343+
const auto address = stack.pop().as<uint32_t>();
332344
// NOTE: alignment is dropped by the parser
333345
const auto offset = read<uint32_t>(immediates);
334346
// Addressing is 32-bit, but we keep the value as 64-bit to detect overflows.
@@ -340,12 +352,21 @@ inline bool load_from_memory(
340352
return true;
341353
}
342354

355+
template <typename T>
356+
inline T pop_store_memory_arg(OperandStack& stack) noexcept
357+
{
358+
if constexpr (std::is_floating_point_v<T>)
359+
return stack.pop().as<T>();
360+
else
361+
return static_cast<T>(stack.pop().i64);
362+
}
363+
343364
template <typename DstT>
344365
inline bool store_into_memory(
345366
bytes& memory, OperandStack& stack, const uint8_t*& immediates) noexcept
346367
{
347-
const auto value = static_cast<DstT>(stack.pop());
348-
const auto address = static_cast<uint32_t>(stack.pop());
368+
const auto value = pop_store_memory_arg<DstT>(stack);
369+
const auto address = stack.pop().as<uint32_t>();
349370
// NOTE: alignment is dropped by the parser
350371
const auto offset = read<uint32_t>(immediates);
351372
// Addressing is 32-bit, but we keep the value as 64-bit to detect overflows.
@@ -835,6 +856,24 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, span<const Value>
835856
}
836857
break;
837858
}
859+
case Instr::f32_load:
860+
{
861+
if (!load_from_memory<float>(*memory, stack, immediates))
862+
{
863+
trap = true;
864+
goto end;
865+
}
866+
break;
867+
}
868+
case Instr::f64_load:
869+
{
870+
if (!load_from_memory<double>(*memory, stack, immediates))
871+
{
872+
trap = true;
873+
goto end;
874+
}
875+
break;
876+
}
838877
case Instr::i32_load8_s:
839878
{
840879
if (!load_from_memory<uint32_t, int8_t>(*memory, stack, immediates))
@@ -943,6 +982,24 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, span<const Value>
943982
}
944983
break;
945984
}
985+
case Instr::f32_store:
986+
{
987+
if (!store_into_memory<float>(*memory, stack, immediates))
988+
{
989+
trap = true;
990+
goto end;
991+
}
992+
break;
993+
}
994+
case Instr::f64_store:
995+
{
996+
if (!store_into_memory<double>(*memory, stack, immediates))
997+
{
998+
trap = true;
999+
goto end;
1000+
}
1001+
break;
1002+
}
9461003
case Instr::i32_store8:
9471004
case Instr::i64_store8:
9481005
{
@@ -1397,10 +1454,6 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, span<const Value>
13971454
break;
13981455
}
13991456

1400-
case Instr::f32_load:
1401-
case Instr::f64_load:
1402-
case Instr::f32_store:
1403-
case Instr::f64_store:
14041457
case Instr::f32_eq:
14051458
case Instr::f32_ne:
14061459
case Instr::f32_lt:

0 commit comments

Comments
 (0)