|
| 1 | +#include "barretenberg/vm2/simulation/addressing.hpp" |
| 2 | + |
| 3 | +#include <gmock/gmock.h> |
| 4 | +#include <gtest/gtest.h> |
| 5 | + |
| 6 | +#include "barretenberg/vm2/common/memory_types.hpp" |
| 7 | +#include "barretenberg/vm2/common/opcodes.hpp" |
| 8 | +#include "barretenberg/vm2/simulation/events/event_emitter.hpp" |
| 9 | +#include "barretenberg/vm2/simulation/lib/instruction_info.hpp" |
| 10 | +#include "barretenberg/vm2/simulation/lib/serialization.hpp" |
| 11 | +#include "barretenberg/vm2/simulation/memory.hpp" |
| 12 | +#include "barretenberg/vm2/simulation/testing/mock_memory.hpp" |
| 13 | +#include "barretenberg/vm2/testing/instruction_builder.hpp" |
| 14 | + |
| 15 | +namespace bb::avm2::simulation { |
| 16 | +namespace { |
| 17 | + |
| 18 | +using ::bb::avm2::testing::InstructionBuilder; |
| 19 | +using ::bb::avm2::testing::OperandBuilder; |
| 20 | +using enum ::bb::avm2::WireOpCode; |
| 21 | +using ::testing::ElementsAre; |
| 22 | +using ::testing::ReturnRef; |
| 23 | +using ::testing::StrictMock; |
| 24 | + |
| 25 | +template <typename T> auto from = ::bb::avm2::simulation::Operand::from<T>; |
| 26 | + |
| 27 | +TEST(AvmSimulationAddressingTest, AllDirectAndNonRelative) |
| 28 | +{ |
| 29 | + InstructionInfoDB instruction_info_db; |
| 30 | + NoopEventEmitter<AddressingEvent> event_emitter; |
| 31 | + Addressing addressing(instruction_info_db, event_emitter); |
| 32 | + MemoryValue zero_u32 = MemoryValue::from<uint32_t>(0); |
| 33 | + |
| 34 | + { |
| 35 | + const auto instr = InstructionBuilder(SET_8) |
| 36 | + // dstOffset |
| 37 | + .operand<uint8_t>(1) |
| 38 | + // tag |
| 39 | + .operand(MemoryTag::FF) |
| 40 | + // value |
| 41 | + .operand<uint8_t>(1) |
| 42 | + .build(); |
| 43 | + |
| 44 | + StrictMock<MockMemory> memory; |
| 45 | + EXPECT_CALL(memory, get(0)).WillOnce(ReturnRef(zero_u32)); // call to get the base address. |
| 46 | + |
| 47 | + const auto operands = addressing.resolve(instr, memory); |
| 48 | + EXPECT_THAT(operands, |
| 49 | + ElementsAre( |
| 50 | + // dstOffset has been resolved and is now a MemoryAddress. |
| 51 | + from<MemoryAddress>(1), |
| 52 | + // tag is unchanged. |
| 53 | + instr.operands.at(1), |
| 54 | + // value is unchanged. |
| 55 | + instr.operands.at(2))); |
| 56 | + } |
| 57 | + { |
| 58 | + const auto instr = InstructionBuilder(ADD_16) |
| 59 | + // aOffset |
| 60 | + .operand<uint16_t>(1) |
| 61 | + // bOffset |
| 62 | + .operand<uint16_t>(2) |
| 63 | + // dstOffset |
| 64 | + .operand<uint16_t>(3) |
| 65 | + .build(); |
| 66 | + |
| 67 | + StrictMock<MockMemory> memory; |
| 68 | + EXPECT_CALL(memory, get(0)).WillOnce(ReturnRef(zero_u32)); // call to get the base address. |
| 69 | + |
| 70 | + const auto operands = addressing.resolve(instr, memory); |
| 71 | + EXPECT_THAT(operands, ElementsAre(from<MemoryAddress>(1), from<MemoryAddress>(2), from<MemoryAddress>(3))); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +TEST(AvmSimulationAddressingTest, RelativeAddressing) |
| 76 | +{ |
| 77 | + InstructionInfoDB instruction_info_db; |
| 78 | + NoopEventEmitter<AddressingEvent> event_emitter; |
| 79 | + Addressing addressing(instruction_info_db, event_emitter); |
| 80 | + |
| 81 | + // For relative addressing, we need a base address other than 0 |
| 82 | + // Base pointer at address 100 |
| 83 | + MemoryValue base_addr = MemoryValue::from<uint32_t>(100); |
| 84 | + |
| 85 | + // Set up the ADD_8 instruction with relative addressing |
| 86 | + const auto instr = InstructionBuilder(ADD_8) |
| 87 | + // aOffset |
| 88 | + .operand<uint8_t>(10) |
| 89 | + .relative() |
| 90 | + // bOffset |
| 91 | + .operand<uint8_t>(20) |
| 92 | + // dstOffset |
| 93 | + .operand<uint8_t>(30) |
| 94 | + .relative() |
| 95 | + .build(); |
| 96 | + |
| 97 | + StrictMock<MockMemory> memory; |
| 98 | + EXPECT_CALL(memory, get(0)).WillOnce(ReturnRef(base_addr)); |
| 99 | + |
| 100 | + const auto operands = addressing.resolve(instr, memory); |
| 101 | + |
| 102 | + EXPECT_THAT(operands, |
| 103 | + ElementsAre( |
| 104 | + // aOffset resolved as base + 10 = 110 |
| 105 | + from<MemoryAddress>(110), |
| 106 | + // bOffset stays the same |
| 107 | + from<MemoryAddress>(20), |
| 108 | + // dstOffset resolved as base + 30 = 130 |
| 109 | + from<MemoryAddress>(130))); |
| 110 | +} |
| 111 | + |
| 112 | +TEST(AvmSimulationAddressingTest, IndirectAddressing) |
| 113 | +{ |
| 114 | + InstructionInfoDB instruction_info_db; |
| 115 | + NoopEventEmitter<AddressingEvent> event_emitter; |
| 116 | + Addressing addressing(instruction_info_db, event_emitter); |
| 117 | + |
| 118 | + // For indirect addressing, memory locations contain the actual addresses |
| 119 | + MemoryValue zero_u32 = MemoryValue::from<uint32_t>(0); |
| 120 | + |
| 121 | + // Set up the ADD_8 instruction with indirect addressing |
| 122 | + const auto instr = InstructionBuilder(ADD_8) |
| 123 | + // aOffset - address 5 contains the actual address |
| 124 | + .operand<uint8_t>(5) |
| 125 | + .indirect() |
| 126 | + // bOffset |
| 127 | + .operand<uint8_t>(10) |
| 128 | + // dstOffset - address 15 contains the actual address |
| 129 | + .operand<uint8_t>(15) |
| 130 | + .indirect() |
| 131 | + .build(); |
| 132 | + |
| 133 | + StrictMock<MockMemory> memory; |
| 134 | + EXPECT_CALL(memory, get(0)).WillOnce(ReturnRef(zero_u32)); // Base address |
| 135 | + MemoryValue addr_5_value = MemoryValue::from<uint32_t>(50); |
| 136 | + EXPECT_CALL(memory, get(5)).WillOnce(ReturnRef(addr_5_value)); |
| 137 | + MemoryValue addr_15_value = MemoryValue::from<uint32_t>(60); |
| 138 | + EXPECT_CALL(memory, get(15)).WillOnce(ReturnRef(addr_15_value)); |
| 139 | + |
| 140 | + const auto operands = addressing.resolve(instr, memory); |
| 141 | + |
| 142 | + // Expect indirect offsets to be resolved by looking up their values in memory |
| 143 | + EXPECT_THAT(operands, |
| 144 | + ElementsAre( |
| 145 | + // aOffset resolved indirectly: memory[5] = 50 |
| 146 | + from<MemoryAddress>(50), |
| 147 | + // bOffset stays the same |
| 148 | + from<MemoryAddress>(10), |
| 149 | + // dstOffset resolved indirectly: memory[15] = 60 |
| 150 | + from<MemoryAddress>(60))); |
| 151 | +} |
| 152 | + |
| 153 | +TEST(AvmSimulationAddressingTest, IndirectAndRelativeAddressing) |
| 154 | +{ |
| 155 | + InstructionInfoDB instruction_info_db; |
| 156 | + NoopEventEmitter<AddressingEvent> event_emitter; |
| 157 | + Addressing addressing(instruction_info_db, event_emitter); |
| 158 | + |
| 159 | + // Base address is 100 |
| 160 | + MemoryValue base_addr = MemoryValue::from<uint32_t>(100); |
| 161 | + |
| 162 | + // Set up the ADD_8 instruction with both indirect and relative addressing |
| 163 | + const auto instr = InstructionBuilder(ADD_8) |
| 164 | + // aOffset (indirect and relative): address base+5 contains value |
| 165 | + .operand<uint8_t>(5) |
| 166 | + .indirect() |
| 167 | + .relative() |
| 168 | + // bOffset (indirect only): address 10 contains value |
| 169 | + .operand<uint8_t>(10) |
| 170 | + .indirect() |
| 171 | + // dstOffset (relative only) |
| 172 | + .operand<uint8_t>(15) |
| 173 | + .relative() |
| 174 | + .build(); |
| 175 | + |
| 176 | + StrictMock<MockMemory> memory; |
| 177 | + EXPECT_CALL(memory, get(0)).WillOnce(ReturnRef(base_addr)); // Base address (100) |
| 178 | + // Address 105 (base+5) contains value 200 |
| 179 | + MemoryValue addr_105_value = MemoryValue::from<uint32_t>(200); |
| 180 | + EXPECT_CALL(memory, get(105)).WillOnce(ReturnRef(addr_105_value)); |
| 181 | + // Address 10 contains value 60 |
| 182 | + MemoryValue addr_10_value = MemoryValue::from<uint32_t>(60); |
| 183 | + EXPECT_CALL(memory, get(10)).WillOnce(ReturnRef(addr_10_value)); |
| 184 | + |
| 185 | + const auto operands = addressing.resolve(instr, memory); |
| 186 | + |
| 187 | + // Expect combined indirect and relative addressing |
| 188 | + EXPECT_THAT(operands, |
| 189 | + ElementsAre( |
| 190 | + // aOffset: relative (base+5=105) and indirect (memory[105]=200) |
| 191 | + from<MemoryAddress>(200), |
| 192 | + // bOffset: indirect only (memory[10]=60) |
| 193 | + from<MemoryAddress>(60), |
| 194 | + // dstOffset: relative only (base+15=115) |
| 195 | + from<MemoryAddress>(115))); |
| 196 | +} |
| 197 | + |
| 198 | +} // namespace |
| 199 | +} // namespace bb::avm2::simulation |
0 commit comments