Skip to content

Commit 77ec2ea

Browse files
committed
Create more generic baseline::CodeAnalysis type
1 parent cd69225 commit 77ec2ea

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

lib/evmone/baseline.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
namespace evmone::baseline
1212
{
13-
JumpdestMap build_jumpdest_map(const uint8_t* code, size_t code_size)
13+
CodeAnalysis analyze(const uint8_t* code, size_t code_size)
1414
{
1515
// To find if op is any PUSH opcode (OP_PUSH1 <= op <= OP_PUSH32)
1616
// it can be noticed that OP_PUSH32 is INT8_MAX (0x7f) therefore
1717
// static_cast<int8_t>(op) <= OP_PUSH32 is always true and can be skipped.
1818
static_assert(OP_PUSH32 == std::numeric_limits<int8_t>::max());
1919

20-
JumpdestMap map(code_size); // Allocate and init bitmap with zeros.
20+
CodeAnalysis::JumpdestMap map(code_size); // Allocate and init bitmap with zeros.
2121
for (size_t i = 0; i < code_size; ++i)
2222
{
2323
const auto op = code[i];
@@ -26,12 +26,13 @@ JumpdestMap build_jumpdest_map(const uint8_t* code, size_t code_size)
2626
else if (INTX_UNLIKELY(op == OP_JUMPDEST))
2727
map[i] = true;
2828
}
29-
return map;
29+
return CodeAnalysis{std::move(map)};
3030
}
3131

3232
namespace
3333
{
34-
const uint8_t* op_jump(ExecutionState& state, const JumpdestMap& jumpdest_map) noexcept
34+
const uint8_t* op_jump(
35+
ExecutionState& state, const CodeAnalysis::JumpdestMap& jumpdest_map) noexcept
3536
{
3637
const auto dst = state.stack.pop();
3738
if (dst >= jumpdest_map.size() || !jumpdest_map[static_cast<size_t>(dst)])
@@ -98,12 +99,12 @@ inline evmc_status_code check_requirements(const char* const* instruction_names,
9899
evmc_result execute(evmc_vm* /*vm*/, const evmc_host_interface* host, evmc_host_context* ctx,
99100
evmc_revision rev, const evmc_message* msg, const uint8_t* code, size_t code_size) noexcept
100101
{
101-
const auto jumpdest_map = build_jumpdest_map(code, code_size);
102+
const auto jumpdest_map = analyze(code, code_size);
102103
auto state = std::make_unique<ExecutionState>(*msg, rev, *host, ctx, code, code_size);
103104
return execute(*state, jumpdest_map);
104105
}
105106

106-
evmc_result execute(ExecutionState& state, const JumpdestMap& jumpdest_map) noexcept
107+
evmc_result execute(ExecutionState& state, const CodeAnalysis& analysis) noexcept
107108
{
108109
const auto rev = state.rev;
109110
const auto code = state.code.data();
@@ -381,12 +382,12 @@ evmc_result execute(ExecutionState& state, const JumpdestMap& jumpdest_map) noex
381382
}
382383

383384
case OP_JUMP:
384-
pc = op_jump(state, jumpdest_map);
385+
pc = op_jump(state, analysis.jumpdest_map);
385386
continue;
386387
case OP_JUMPI:
387388
if (state.stack[1] != 0)
388389
{
389-
pc = op_jump(state, jumpdest_map);
390+
pc = op_jump(state, analysis.jumpdest_map);
390391
}
391392
else
392393
{

lib/evmone/baseline.hpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@
1010

1111
namespace evmone::baseline
1212
{
13-
using JumpdestMap = std::vector<bool>;
13+
struct CodeAnalysis
14+
{
15+
using JumpdestMap = std::vector<bool>;
16+
17+
JumpdestMap jumpdest_map;
18+
};
1419

15-
/// Builds the bitmap of valid JUMPDEST locations in the code.
16-
EVMC_EXPORT JumpdestMap build_jumpdest_map(const uint8_t* code, size_t code_size);
20+
/// Analyze the code to build the bitmap of valid JUMPDEST locations.
21+
EVMC_EXPORT CodeAnalysis analyze(const uint8_t* code, size_t code_size);
1722

1823
/// Executes in Baseline interpreter using EVMC-compatible parameters.
1924
evmc_result execute(evmc_vm* vm, const evmc_host_interface* host, evmc_host_context* ctx,
2025
evmc_revision rev, const evmc_message* msg, const uint8_t* code, size_t code_size) noexcept;
2126

2227
/// Executes in Baseline interpreter on the given external and initialized state.
23-
evmc_result execute(ExecutionState& state, const JumpdestMap& jumpdest_map) noexcept;
28+
evmc_result execute(ExecutionState& state, const CodeAnalysis& analysis) noexcept;
2429
} // namespace evmone::baseline

test/bench/bench.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ void register_benchmarks(const std::vector<BenchmarkCase>& benchmark_cases)
171171
if (registered_vms.count("baseline"))
172172
{
173173
RegisterBenchmark(("baseline/analyse/" + b.name).c_str(), [&b](State& state) {
174-
build_jumpdest_map(state, b.code);
174+
baseline_analyze(state, b.code);
175175
})->Unit(kMicrosecond);
176176
}
177177

test/bench/helpers.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ inline void analyse(benchmark::State& state, evmc_revision rev, bytes_view code)
3131
state.counters["rate"] = Counter(static_cast<double>(bytes_analysed), Counter::kIsRate);
3232
}
3333

34-
inline void build_jumpdest_map(benchmark::State& state, bytes_view code) noexcept
34+
inline void baseline_analyze(benchmark::State& state, bytes_view code) noexcept
3535
{
3636
auto bytes_analysed = uint64_t{0};
3737
for (auto _ : state)
3838
{
39-
auto r = evmone::build_jumpdest_map(code.data(), code.size());
39+
auto r = evmone::baseline::analyze(code.data(), code.size());
4040
benchmark::DoNotOptimize(r);
4141
bytes_analysed += code.size();
4242
}

0 commit comments

Comments
 (0)