Skip to content

Commit

Permalink
Use instruction pointer instead of index
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Jul 24, 2019
1 parent 39893c7 commit 8983e69
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
8 changes: 4 additions & 4 deletions lib/evmone/analysis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ struct evm_stack
uint256 pop() noexcept { return *top_item--; }
};

struct instr_info;

struct execution_state
{
static constexpr auto stop_sentinel = std::numeric_limits<size_t>::max();

size_t pc = 0;
const instr_info* next_instr{nullptr};
evmc_status_code status = EVMC_SUCCESS;
int64_t gas_left = 0;

Expand Down Expand Up @@ -91,7 +91,7 @@ struct execution_state
void exit(evmc_status_code status_code) noexcept
{
status = status_code;
pc = stop_sentinel;
next_instr = nullptr;
}
};

Expand Down
9 changes: 5 additions & 4 deletions lib/evmone/execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ evmc_result execute(evmc_instance*, evmc_context* ctx, evmc_revision rev, const

auto state = std::make_unique<execution_state>();
state->analysis = &analysis;
state->next_instr = &state->analysis->instrs[0];
state->msg = msg;
state->code = code;
state->code_size = code_size;
state->host = evmc::HostContext{ctx};
state->gas_left = msg->gas;
state->rev = rev;
while (state->pc != execution_state::stop_sentinel)
while (state->next_instr)
{
auto& instr = analysis.instrs[state->pc];
const auto& instr = *state->next_instr;

// Advance the PC to allow jump opcodes to overwrite it.
++state->pc;
// Advance next_instr to allow jump opcodes to overwrite it.
++state->next_instr;

instr.fn(*state, instr.arg);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/evmone/instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ void op_jump(execution_state& state, instr_argument) noexcept
(pc = state.analysis->find_jumpdest(static_cast<int>(dst))) < 0)
return state.exit(EVMC_BAD_JUMP_DESTINATION);

state.pc = static_cast<size_t>(pc);
state.next_instr = &state.analysis->instrs[static_cast<size_t>(pc)];
}

void op_jumpi(execution_state& state, instr_argument arg) noexcept
Expand Down

0 comments on commit 8983e69

Please sign in to comment.