Skip to content

Commit

Permalink
Do not export and allow inlining of find_jumpdest
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Aug 3, 2019
1 parent e2f0e7c commit fdc1573
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
11 changes: 0 additions & 11 deletions lib/evmone/analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@ bool is_terminator(uint8_t c) noexcept
}
} // namespace

int code_analysis::find_jumpdest(int offset) const noexcept
{
// TODO: Replace with lower_bound().
for (const auto& d : jumpdest_map)
{
if (d.first == offset)
return d.second;
}
return -1;
}

evmc_call_kind op2call_kind(uint8_t opcode) noexcept
{
switch (opcode)
Expand Down
14 changes: 11 additions & 3 deletions lib/evmone/analysis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,19 @@ struct code_analysis
std::deque<bytes32> args_storage;

std::vector<std::pair<int, int>> jumpdest_map;

// TODO: Exported for unit tests. Rework unit tests?
EVMC_EXPORT int find_jumpdest(int offset) const noexcept;
};

inline int find_jumpdest(const code_analysis& analysis, int offset) noexcept
{
// TODO: Replace with lower_bound().
for (const auto& d : analysis.jumpdest_map)
{
if (d.first == offset)
return d.second;
}
return -1;
}

EVMC_EXPORT code_analysis analyze(
const exec_fn_table& fns, evmc_revision rev, const uint8_t* code, size_t code_size) noexcept;

Expand Down
2 changes: 1 addition & 1 deletion lib/evmone/instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ void op_jump(execution_state& state, instr_argument) noexcept
const auto dst = state.stack.pop();
auto pc = -1;
if (std::numeric_limits<int>::max() < dst ||
(pc = state.analysis->find_jumpdest(static_cast<int>(dst))) < 0)
(pc = find_jumpdest(*state.analysis, static_cast<int>(dst))) < 0)
return state.exit(EVMC_BAD_JUMP_DESTINATION);

state.next_instr = &state.analysis->instrs[static_cast<size_t>(pc)];
Expand Down
4 changes: 2 additions & 2 deletions test/unittests/analysis_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ TEST(analysis, jump1)
ASSERT_EQ(analysis.blocks.size(), 3);
ASSERT_EQ(analysis.jumpdest_map.size(), 1);
EXPECT_EQ(analysis.jumpdest_map[0], std::pair(6, 5));
EXPECT_EQ(analysis.find_jumpdest(6), 5);
EXPECT_EQ(analysis.find_jumpdest(0), -1);
EXPECT_EQ(find_jumpdest(analysis, 6), 5);
EXPECT_EQ(find_jumpdest(analysis, 0), -1);
}

TEST(analysis, empty)
Expand Down

0 comments on commit fdc1573

Please sign in to comment.