Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
pure_addressing.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cstdint>
5#include <vector>
6
15
16namespace bb::avm2::simulation {
17
19{
20 BB_BENCH_NAME("PureAddressing::resolve");
21
22 ExecutionOpCode exec_opcode = instruction_info_db.get(instruction.opcode).exec_opcode;
23 const ExecInstructionSpec& spec = instruction_info_db.get(exec_opcode);
24
25 assert(spec.num_addresses <= instruction.operands.size());
26
28 std::vector<Operand> resolved_operands = instruction.operands;
29
30 for (size_t i = 0; i < spec.num_addresses; ++i) {
31 Operand& operand = resolved_operands[i];
32 const ValueTag tag = operand.get_tag();
33
34 // We assume from serialization that the operand is <= the bits of a memory address.
35 // We assert this here as it is a precondition.
37 // Normalize possibly smaller sizes to MemoryAddress.
38 if (tag != MemoryAddressTag) {
39 operand = Operand::from(static_cast<MemoryAddress>(operand.to<MemoryAddress>()));
40 }
41
42 // Handle relative addressing
43 if (is_operand_relative(instruction.indirect, i)) {
44 if (!base_address) {
45 MemoryValue maybe_base_address = memory.get(0);
46 if (!memory.is_valid_address(maybe_base_address)) {
47 throw AddressingException();
48 }
49 base_address = maybe_base_address.as<MemoryAddress>();
50 }
51 uint64_t offset = operand.as<MemoryAddress>();
52 offset += *base_address;
54 throw AddressingException();
55 }
56 operand = Operand::from(static_cast<MemoryAddress>(offset));
57 }
58
59 // Handle indirection
60 if (is_operand_indirect(instruction.indirect, i)) {
61 const MemoryValue& indirect_value = memory.get(operand.as<MemoryAddress>());
62 if (!memory.is_valid_address(indirect_value)) {
63 throw AddressingException();
64 }
65 operand = indirect_value;
66 }
67 }
68
69 return resolved_operands;
70}
71
72} // namespace bb::avm2::simulation
#define AVM_HIGHEST_MEM_ADDRESS
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:218
static TaggedValue from(T value)
ValueTag get_tag() const
virtual const ExecInstructionSpec & get(ExecutionOpCode opcode) const =0
const InstructionInfoDBInterface & instruction_info_db
std::vector< Operand > resolve(const Instruction &instruction, MemoryInterface &memory) override
ssize_t offset
Definition engine.cpp:36
Instruction instruction
bool is_operand_relative(uint16_t indirect_flag, size_t operand_index)
Definition addressing.hpp:8
bool is_operand_indirect(uint16_t indirect_flag, size_t operand_index)
uint8_t get_tag_bits(ValueTag tag)
uint32_t MemoryAddress
constexpr auto MemoryAddressTag
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13