Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip some instructions and PC updates #622

Draft
wants to merge 6 commits into
base: skip_nop_instructions
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions lib/fizzy/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,28 +540,27 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,

while (true)
{
const auto instruction = *pc++;
const auto instruction = *pc;
switch (instruction)
{
case Instr::unreachable:
goto trap;
case Instr::nop:
case Instr::block:
case Instr::loop:
break;
case Instr::if_:
{
if (stack.pop().as<uint32_t>() != 0)
{
immediates += 2 * sizeof(uint32_t); // Skip the immediates for else instruction.
break;
}
else
{
const auto target_pc = read<uint32_t>(immediates);
const auto target_imm = read<uint32_t>(immediates);

pc = code.instructions.data() + target_pc;
immediates = code.immediates.data() + target_imm;
continue;
}
break;
}
case Instr::else_:
{
Expand All @@ -573,18 +572,16 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
pc = code.instructions.data() + target_pc;
immediates = code.immediates.data() + target_imm;

break;
continue;
}
case Instr::end:
{
// End execution if it's a final end instruction.
if (pc == &code.instructions[code.instructions.size()])
goto end;
break;
assert(pc == &code.instructions[code.instructions.size() - 1]);
goto end;
}
case Instr::br:
case Instr::br_if:
case Instr::return_:
case Instr::return_: // TODO: Replace return with br
{
const auto arity = read<uint32_t>(immediates);

Expand All @@ -596,7 +593,7 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
}

branch(code, stack, pc, immediates, arity);
break;
continue;
}
case Instr::br_table:
{
Expand All @@ -611,7 +608,7 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
immediates += label_idx_offset;

branch(code, stack, pc, immediates, arity);
break;
continue;
}
case Instr::call:
{
Expand Down Expand Up @@ -1557,10 +1554,12 @@ ExecutionResult execute(Instance& instance, FuncIdx func_idx, const Value* args,
default:
FIZZY_UNREACHABLE();
}

++pc;
}

end:
assert(pc == &code.instructions[code.instructions.size()]); // End of code must be reached.
assert(pc == &code.instructions[code.instructions.size() - 1]); // End of code must be reached.
assert(stack.size() == instance.module->get_function_type(func_idx).outputs.size());

return stack.size() != 0 ? ExecutionResult{stack.top()} : Void;
Expand Down
18 changes: 11 additions & 7 deletions lib/fizzy/parser_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ parser_result<Code> parse_expr(const uint8_t* pos, const uint8_t* end, FuncIdx f
}

case Instr::nop:
continue;

case Instr::i32_eqz:
case Instr::i32_eq:
case Instr::i32_ne:
Expand Down Expand Up @@ -466,7 +468,7 @@ parser_result<Code> parse_expr(const uint8_t* pos, const uint8_t* end, FuncIdx f
// Push label with immediates offset after arity.
control_stack.emplace(Instr::block, block_type, static_cast<int>(operand_stack.size()),
code.instructions.size(), code.immediates.size());
break;
continue;
}

case Instr::loop:
Expand All @@ -476,7 +478,7 @@ parser_result<Code> parse_expr(const uint8_t* pos, const uint8_t* end, FuncIdx f

control_stack.emplace(Instr::loop, loop_type, static_cast<int>(operand_stack.size()),
code.instructions.size(), code.immediates.size());
break;
continue;
}

case Instr::if_:
Expand Down Expand Up @@ -538,9 +540,7 @@ parser_result<Code> parse_expr(const uint8_t* pos, const uint8_t* end, FuncIdx f
// In case it's an outermost implicit function block,
// we want br to jump to the final end of the function.
// Otherwise jump to the next instruction after block's end.
const auto target_pc = control_stack.size() == 1 ?
static_cast<uint32_t>(code.instructions.size()) :
static_cast<uint32_t>(code.instructions.size() + 1);
const auto target_pc = static_cast<uint32_t>(code.instructions.size());
const auto target_imm = static_cast<uint32_t>(code.immediates.size());

if (frame.instruction == Instr::if_ || frame.instruction == Instr::else_)
Expand Down Expand Up @@ -569,10 +569,14 @@ parser_result<Code> parse_expr(const uint8_t* pos, const uint8_t* end, FuncIdx f
control_stack.pop(); // Pop the current frame.

if (control_stack.empty())
{
continue_parsing = false;
else if (frame_type.has_value())
break;
}

if (frame_type.has_value())
push_operand(operand_stack, *frame_type);
break;
continue;
}

case Instr::br:
Expand Down