Skip to content

Commit

Permalink
Merge pull request #424 from ethereum/cleanups
Browse files Browse the repository at this point in the history
Refactoring and clean-ups
  • Loading branch information
chfast authored Jan 26, 2022
2 parents 5aa8c7e + 43fb25e commit 6b02dd0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
17 changes: 9 additions & 8 deletions lib/evmone/baseline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,6 @@ inline evmc_status_code check_requirements(
}


/// Implementation of a generic instruction "case".
#define DISPATCH_CASE(OPCODE) \
case OPCODE: \
ASM_COMMENT(OPCODE); \
if (code_it = invoke<OPCODE>(cost_table, state, code_it); !code_it) \
goto exit; \
break

/// Helpers for invoking instruction implementations of different signatures.
/// @{
inline code_iterator invoke(
Expand Down Expand Up @@ -178,6 +170,15 @@ template <evmc_opcode Op>
return new_pos;
}


/// Implementation of a generic instruction "case".
#define DISPATCH_CASE(OPCODE) \
case OPCODE: \
ASM_COMMENT(OPCODE); \
if (code_it = invoke<OPCODE>(cost_table, state, code_it); !code_it) \
goto exit; \
break

template <bool TracingEnabled>
evmc_result execute(const VM& vm, ExecutionState& state, const CodeAnalysis& analysis) noexcept
{
Expand Down
31 changes: 17 additions & 14 deletions lib/evmone/execution_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,46 @@ struct Stack
/// The maximum number of stack items.
static constexpr auto limit = 1024;

/// The pointer to the top item, or below the stack bottom if stack is empty.
intx::uint256* top_item = nullptr;
/// The pointer to the top item.
/// This is never nullptr and when stack is empty it points to bottom(),
/// i.e. one item below the stack space.
uint256* top_item;

/// The storage allocated for maximum possible number of items.
/// This is also the pointer to the bottom item.
/// Items are aligned to 256 bits for better packing in cache lines.
alignas(sizeof(intx::uint256)) intx::uint256 storage[limit];
alignas(sizeof(uint256)) uint256 storage[limit];

/// Default constructor. Sets the top_item pointer to below the stack bottom.
Stack() noexcept { clear(); }
/// Returns the pointer to below the stack storage.
[[nodiscard, clang::no_sanitize("bounds")]] uint256* bottom() noexcept { return storage - 1; }

/// Default constructor. Stack is empty.
Stack() noexcept : top_item{bottom()} {}

/// The current number of items on the stack.
[[nodiscard]] int size() const noexcept { return static_cast<int>(top_item + 1 - storage); }

/// Returns the reference to the top item.
// NOLINTNEXTLINE(readability-make-member-function-const)
[[nodiscard]] intx::uint256& top() noexcept { return *top_item; }
[[nodiscard]] uint256& top() noexcept { return *top_item; }

/// Returns the reference to the stack item on given position from the stack top.
// NOLINTNEXTLINE(readability-make-member-function-const)
[[nodiscard]] intx::uint256& operator[](int index) noexcept { return *(top_item - index); }
[[nodiscard]] uint256& operator[](int index) noexcept { return *(top_item - index); }

/// Returns the const reference to the stack item on given position from the stack top.
[[nodiscard]] const intx::uint256& operator[](int index) const noexcept
[[nodiscard]] const uint256& operator[](int index) const noexcept
{
return *(top_item - index);
}

/// Pushes an item on the stack. The stack limit is not checked.
void push(const intx::uint256& item) noexcept { *++top_item = item; }
void push(const uint256& item) noexcept { *++top_item = item; }

/// Returns an item popped from the top of the stack.
intx::uint256 pop() noexcept { return *top_item--; }
uint256 pop() noexcept { return *top_item--; }

/// Clears the stack by resetting its size to 0 (sets the top_item pointer to below the stack
/// bottom).
[[clang::no_sanitize("bounds")]] void clear() noexcept { top_item = storage - 1; }
/// Empties the stack by resetting the top item pointer.
void clear() noexcept { top_item = bottom(); }
};

/// The EVM memory.
Expand Down

0 comments on commit 6b02dd0

Please sign in to comment.