-
Notifications
You must be signed in to change notification settings - Fork 294
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
Add classification of EVM instructions (op_kind) #173
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,19 +77,12 @@ code_analysis analyze(evmc_revision rev, const uint8_t* code, size_t code_size) | |
|
||
auto& instr = analysis.instrs.back(); | ||
|
||
bool is_terminator = false; // A flag whenever this is a block terminating instruction. | ||
switch (opcode) | ||
switch (opcode_info.kind) | ||
{ | ||
case OP_JUMP: | ||
case OP_JUMPI: | ||
case OP_STOP: | ||
case OP_RETURN: | ||
case OP_REVERT: | ||
case OP_SELFDESTRUCT: | ||
is_terminator = true; | ||
default: | ||
break; | ||
|
||
case ANY_SMALL_PUSH: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can |
||
case op_kind::small_push: | ||
{ | ||
const auto push_size = size_t(opcode - OP_PUSH1 + 1); | ||
const auto push_end = code_pos + push_size; | ||
|
@@ -104,7 +97,7 @@ code_analysis analyze(evmc_revision rev, const uint8_t* code, size_t code_size) | |
break; | ||
} | ||
|
||
case ANY_LARGE_PUSH: | ||
case op_kind::large_push: | ||
{ | ||
const auto push_size = size_t(opcode - OP_PUSH1 + 1); | ||
const auto push_end = code_pos + push_size; | ||
|
@@ -130,23 +123,18 @@ code_analysis analyze(evmc_revision rev, const uint8_t* code, size_t code_size) | |
break; | ||
} | ||
|
||
case OP_GAS: | ||
case OP_CALL: | ||
case OP_CALLCODE: | ||
case OP_DELEGATECALL: | ||
case OP_STATICCALL: | ||
case OP_CREATE: | ||
case OP_CREATE2: | ||
case op_kind::gas_counter_access: | ||
instr.arg.number = block.gas_cost; | ||
break; | ||
|
||
case OP_PC: | ||
case op_kind::pc: | ||
instr.arg.number = static_cast<int>(code_pos - code - 1); | ||
break; | ||
} | ||
|
||
// If this is a terminating instruction or the next instruction is a JUMPDEST. | ||
if (is_terminator || (code_pos != code_end && *code_pos == OP_JUMPDEST)) | ||
if (opcode_info.kind == op_kind::terminator || | ||
(code_pos != code_end && *code_pos == OP_JUMPDEST)) | ||
{ | ||
// Save current block. | ||
const auto stack_req = block.stack_req <= stack_req_max ? | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,13 +170,26 @@ enum intrinsic_opcodes | |
OPX_BEGINBLOCK = OP_JUMPDEST | ||
}; | ||
|
||
/// Defines classification of EVM instructions. | ||
enum class op_kind | ||
{ | ||
regular, ///< Totally uninteresting instruction. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😆 |
||
terminator, ///< Terminator instruction. | ||
small_push, ///< Push instruction up to 8 bytes of value. | ||
large_push, ///< Push instruction with more than 8 bytes of value. | ||
gas_counter_access, ///< Instruction that requires access to the gas counter. | ||
pc ///< The PC instruction. | ||
}; | ||
|
||
struct op_table_entry | ||
{ | ||
exec_fn fn; | ||
int16_t gas_cost; | ||
int8_t stack_req; | ||
int8_t stack_change; | ||
op_kind kind; | ||
}; | ||
static_assert(sizeof(op_table_entry) <= 2 * sizeof(void*)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if this is important, maybe |
||
|
||
using op_table = std::array<op_table_entry, 256>; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say explicit cases here would more clearly show the intention