Skip to content

Commit 1d3b548

Browse files
committed
Always keep current instruction pointer in a register
This changes the signature of functions implementing instructions and the main loop. The pointer to current instructions has been removed from the state object, is now passed as an argument (rdi register) and next instruction pointer is returned from the instruction function (rax register).
1 parent f2344cc commit 1d3b548

File tree

3 files changed

+233
-126
lines changed

3 files changed

+233
-126
lines changed

lib/evmone/analysis.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ struct instr_info;
8686

8787
struct execution_state
8888
{
89-
const instr_info* next_instr{nullptr};
9089
evmc_status_code status = EVMC_SUCCESS;
9190
int64_t gas_left = 0;
9291

@@ -113,10 +112,10 @@ struct execution_state
113112
evmc_revision rev = {};
114113

115114
/// Terminates the execution with the given status code.
116-
void exit(evmc_status_code status_code) noexcept
115+
const instr_info* exit(evmc_status_code status_code) noexcept
117116
{
118117
status = status_code;
119-
next_instr = nullptr;
118+
return nullptr;
120119
}
121120
};
122121

@@ -134,7 +133,7 @@ union instr_argument
134133

135134
static_assert(sizeof(instr_argument) == sizeof(void*), "Incorrect size of instr_argument");
136135

137-
using exec_fn = void (*)(execution_state&, instr_argument arg);
136+
using exec_fn = const instr_info* (*)(const instr_info*, execution_state&, instr_argument arg);
138137

139138
/// The evmone intrinsic opcodes.
140139
///

lib/evmone/execution.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,16 @@ evmc_result execute(evmc_instance*, evmc_context* ctx, evmc_revision rev, const
2222

2323
auto state = std::make_unique<execution_state>();
2424
state->analysis = &analysis;
25-
state->next_instr = &state->analysis->instrs[0];
2625
state->msg = msg;
2726
state->code = code;
2827
state->code_size = code_size;
2928
state->host = evmc::HostContext{ctx};
3029
state->gas_left = msg->gas;
3130
state->rev = rev;
32-
while (state->next_instr)
33-
{
34-
const auto& instr = *state->next_instr;
35-
36-
// Advance next_instr to allow jump opcodes to overwrite it.
37-
++state->next_instr;
3831

39-
instr.fn(*state, instr.arg);
40-
}
32+
const instr_info* instr = &state->analysis->instrs[0];
33+
while (instr)
34+
instr = instr->fn(instr, *state, instr->arg);
4135

4236
evmc_result result{};
4337

0 commit comments

Comments
 (0)