Skip to content

Commit fcf0287

Browse files
authored
Merge pull request #578 from wasmx/exec_cleanup
Instructions implementation cleanup
2 parents 8294da8 + 20c761c commit fcf0287

1 file changed

Lines changed: 31 additions & 35 deletions

File tree

lib/fizzy/execute.cpp

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ template <typename DstT, typename SrcT = DstT>
6060
inline bool load_from_memory(
6161
bytes_view memory, OperandStack& stack, const uint8_t*& immediates) noexcept
6262
{
63-
const auto address = stack.pop().as<uint32_t>();
63+
const auto address = stack.top().as<uint32_t>();
6464
// NOTE: alignment is dropped by the parser
6565
const auto offset = read<uint32_t>(immediates);
6666
// Addressing is 32-bit, but we keep the value as 64-bit to detect overflows.
6767
if ((uint64_t{address} + offset + sizeof(SrcT)) > memory.size())
6868
return false;
6969

7070
const auto ret = load<SrcT>(memory, address + offset);
71-
stack.push(extend<DstT>(ret));
71+
stack.top() = extend<DstT>(ret);
7272
return true;
7373
}
7474

@@ -830,8 +830,7 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
830830
}
831831
case Instr::i32_eqz:
832832
{
833-
const auto value = stack.pop().as<uint32_t>();
834-
stack.push(uint32_t{value == 0});
833+
stack.top() = uint32_t{stack.top().as<uint32_t>() == 0};
835834
break;
836835
}
837836
case Instr::i32_eq:
@@ -886,7 +885,7 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
886885
}
887886
case Instr::i64_eqz:
888887
{
889-
stack.push(uint32_t{stack.pop().i64 == 0});
888+
stack.top() = uint32_t{stack.top().i64 == 0};
890889
break;
891890
}
892891
case Instr::i64_eq:
@@ -1034,42 +1033,41 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
10341033
}
10351034
case Instr::i32_div_s:
10361035
{
1037-
auto const rhs = stack[0].as<int32_t>();
1038-
auto const lhs = stack[1].as<int32_t>();
1036+
const auto rhs = stack.pop().as<int32_t>();
1037+
const auto lhs = stack.top().as<int32_t>();
10391038
if (rhs == 0 || (lhs == std::numeric_limits<int32_t>::min() && rhs == -1))
10401039
goto trap;
1041-
binary_op(stack, div<int32_t>);
1040+
stack.top() = div(lhs, rhs);
10421041
break;
10431042
}
10441043
case Instr::i32_div_u:
10451044
{
1046-
auto const rhs = stack.top().as<uint32_t>();
1045+
const auto rhs = stack.pop().as<uint32_t>();
10471046
if (rhs == 0)
10481047
goto trap;
1049-
binary_op(stack, div<uint32_t>);
1048+
const auto lhs = stack.top().as<uint32_t>();
1049+
stack.top() = div(lhs, rhs);
10501050
break;
10511051
}
10521052
case Instr::i32_rem_s:
10531053
{
1054-
auto const rhs = stack.top().as<int32_t>();
1054+
const auto rhs = stack.pop().as<int32_t>();
10551055
if (rhs == 0)
10561056
goto trap;
1057-
auto const lhs = stack[1].as<int32_t>();
1057+
const auto lhs = stack.top().as<int32_t>();
10581058
if (lhs == std::numeric_limits<int32_t>::min() && rhs == -1)
1059-
{
1060-
stack.pop();
10611059
stack.top() = 0;
1062-
}
10631060
else
1064-
binary_op(stack, rem<int32_t>);
1061+
stack.top() = rem(lhs, rhs);
10651062
break;
10661063
}
10671064
case Instr::i32_rem_u:
10681065
{
1069-
auto const rhs = stack.top().as<uint32_t>();
1066+
const auto rhs = stack.pop().as<uint32_t>();
10701067
if (rhs == 0)
10711068
goto trap;
1072-
binary_op(stack, rem<uint32_t>);
1069+
const auto lhs = stack.top().as<uint32_t>();
1070+
stack.top() = rem(lhs, rhs);
10731071
break;
10741072
}
10751073
case Instr::i32_and:
@@ -1145,42 +1143,41 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
11451143
}
11461144
case Instr::i64_div_s:
11471145
{
1148-
auto const rhs = stack[0].as<int64_t>();
1149-
auto const lhs = stack[1].as<int64_t>();
1146+
const auto rhs = stack.pop().as<int64_t>();
1147+
const auto lhs = stack.top().as<int64_t>();
11501148
if (rhs == 0 || (lhs == std::numeric_limits<int64_t>::min() && rhs == -1))
11511149
goto trap;
1152-
binary_op(stack, div<int64_t>);
1150+
stack.top() = div(lhs, rhs);
11531151
break;
11541152
}
11551153
case Instr::i64_div_u:
11561154
{
1157-
auto const rhs = stack.top().i64;
1155+
const auto rhs = stack.pop().i64;
11581156
if (rhs == 0)
11591157
goto trap;
1160-
binary_op(stack, div<uint64_t>);
1158+
const auto lhs = stack.top().i64;
1159+
stack.top() = div(lhs, rhs);
11611160
break;
11621161
}
11631162
case Instr::i64_rem_s:
11641163
{
1165-
auto const rhs = stack.top().as<int64_t>();
1164+
const auto rhs = stack.pop().as<int64_t>();
11661165
if (rhs == 0)
11671166
goto trap;
1168-
auto const lhs = stack[1].as<int64_t>();
1167+
const auto lhs = stack.top().as<int64_t>();
11691168
if (lhs == std::numeric_limits<int64_t>::min() && rhs == -1)
1170-
{
1171-
stack.pop();
11721169
stack.top() = 0;
1173-
}
11741170
else
1175-
binary_op(stack, rem<int64_t>);
1171+
stack.top() = rem(lhs, rhs);
11761172
break;
11771173
}
11781174
case Instr::i64_rem_u:
11791175
{
1180-
auto const rhs = stack.top().i64;
1176+
const auto rhs = stack.pop().i64;
11811177
if (rhs == 0)
11821178
goto trap;
1183-
binary_op(stack, rem<uint64_t>);
1179+
const auto lhs = stack.top().i64;
1180+
stack.top() = rem(lhs, rhs);
11841181
break;
11851182
}
11861183
case Instr::i64_and:
@@ -1377,7 +1374,7 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
13771374

13781375
case Instr::i32_wrap_i64:
13791376
{
1380-
stack.push(stack.pop().as<uint32_t>());
1377+
stack.top() = stack.top().as<uint32_t>();
13811378
break;
13821379
}
13831380
case Instr::i32_trunc_f32_s:
@@ -1406,8 +1403,7 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
14061403
}
14071404
case Instr::i64_extend_i32_s:
14081405
{
1409-
const auto value = stack.pop().as<int32_t>();
1410-
stack.push(int64_t{value});
1406+
stack.top() = int64_t{stack.top().as<int32_t>()};
14111407
break;
14121408
}
14131409
case Instr::i64_extend_i32_u:
@@ -1520,7 +1516,7 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
15201516
assert(pc == &code.instructions[code.instructions.size()]); // End of code must be reached.
15211517
assert(stack.size() == instance.module.get_function_type(func_idx).outputs.size());
15221518

1523-
return stack.size() != 0 ? ExecutionResult{stack.pop()} : Void;
1519+
return stack.size() != 0 ? ExecutionResult{stack.top()} : Void;
15241520

15251521
trap:
15261522
return Trap;

0 commit comments

Comments
 (0)