Skip to content

Commit

Permalink
feat(avm): handle debuglog (#6630)
Browse files Browse the repository at this point in the history
Handle it by not doing anything*. Also add deserialization for a bunch
of other opcodes.

*actually we insert a JUMP instead.
  • Loading branch information
fcarreiro authored May 28, 2024
1 parent a790d24 commit eba345b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const std::vector<OperandType> three_operand_format = {
OperandType::INDIRECT, OperandType::TAG, OperandType::UINT32, OperandType::UINT32, OperandType::UINT32,
};

const std::vector<OperandType> getter_format = {
OperandType::INDIRECT,
OperandType::UINT32,
};

// Contrary to TS, the format does not contain the opcode byte which prefixes any instruction.
// The format for OpCode::SET has to be handled separately as it is variable based on the tag.
const std::unordered_map<OpCode, std::vector<OperandType>> OPCODE_WIRE_FORMAT = {
Expand All @@ -33,27 +38,72 @@ const std::unordered_map<OpCode, std::vector<OperandType>> OPCODE_WIRE_FORMAT =
{ OpCode::LT, three_operand_format },
{ OpCode::LTE, three_operand_format },
// Compute - Bitwise
{ OpCode::NOT, { OperandType::INDIRECT, OperandType::TAG, OperandType::UINT32, OperandType::UINT32 } },
{ OpCode::AND, three_operand_format },
{ OpCode::OR, three_operand_format },
{ OpCode::XOR, three_operand_format },
{ OpCode::SHR, three_operand_format },
{ OpCode::NOT, { OperandType::INDIRECT, OperandType::TAG, OperandType::UINT32, OperandType::UINT32 } },
{ OpCode::SHL, three_operand_format },
{ OpCode::SHR, three_operand_format },
// Compute - Type Conversions
{ OpCode::CAST, { OperandType::INDIRECT, OperandType::TAG, OperandType::UINT32, OperandType::UINT32 } },
// Execution Environment - Globals
{ OpCode::ADDRESS, getter_format },
{ OpCode::STORAGEADDRESS, getter_format },
{ OpCode::SENDER, getter_format },
{ OpCode::FEEPERL2GAS, getter_format },
{ OpCode::FEEPERDAGAS, getter_format },
{ OpCode::TRANSACTIONFEE, getter_format },
// CONTRACTCALLDEPTH, -- not in simulator
// Execution Environment - Globals
{ OpCode::CHAINID, getter_format },
{ OpCode::VERSION, getter_format },
{ OpCode::BLOCKNUMBER, getter_format },
{ OpCode::TIMESTAMP, getter_format },
// COINBASE, -- not in simulator
// BLOCKL2GASLIMIT, -- not in simulator
// BLOCKDAGASLIMIT, -- not in simulator
// Execution Environment - Calldata
{ OpCode::CALLDATACOPY, { OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32, OperandType::UINT32 } },
// Machine State - Gas
{ OpCode::L2GASLEFT, getter_format },
{ OpCode::DAGASLEFT, getter_format },
// Machine State - Internal Control Flow
{ OpCode::JUMP, { OperandType::UINT32 } },
// JUMPI
{ OpCode::INTERNALCALL, { OperandType::UINT32 } },
{ OpCode::INTERNALRETURN, {} },
// Machine State - Memory
// OpCode::SET is handled differently
{ OpCode::MOV, { OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32 } },
{ OpCode::CMOV,
{ OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32, OperandType::UINT32, OperandType::UINT32 } },
// World State
// SLOAD,
// SSTORE,
// NOTEHASHEXISTS,
// EMITNOTEHASH,
// NULLIFIEREXISTS,
// EMITNULLIFIER,
// L1TOL2MSGEXISTS,
// HEADERMEMBER,
// GETCONTRACTINSTANCE,
// Accrued Substate
// EMITUNENCRYPTEDLOG,
// SENDL2TOL1MSG,
// Control Flow - Contract Calls
// CALL,
// STATICCALL,
// DELEGATECALL, -- not in simulator
{ OpCode::RETURN, { OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32 } },
// REVERT,
// Misc
{ OpCode::DEBUGLOG,
{ OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32, OperandType::UINT32, OperandType::UINT32 } },
// Gadgets
// KECCAK,
// POSEIDON2,
// SHA256,
// PEDERSEN,
// Gadget - Conversion
{ OpCode::TORADIXLE,
{ OperandType::INDIRECT, OperandType::UINT32, OperandType::UINT32, OperandType::UINT32, OperandType::UINT32 } },
Expand Down Expand Up @@ -200,4 +250,5 @@ std::vector<Instruction> Deserialization::parse(std::vector<uint8_t> const& byte
}
return instructions;
};

} // namespace bb::avm_trace
11 changes: 8 additions & 3 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ std::vector<Row> Execution::gen_trace(std::vector<Instruction> const& instructio
// on opcode logic and therefore is not maintained here. However, the next opcode in the execution
// is determined by this value which require read access to the code below.
uint32_t pc = 0;
auto const inst_size = instructions.size();

while ((pc = trace_builder.getPc()) < inst_size) {
while ((pc = trace_builder.getPc()) < instructions.size()) {
auto inst = instructions.at(pc);

// TODO: We do not yet support the indirect flag. Therefore we do not extract
Expand Down Expand Up @@ -275,6 +273,11 @@ std::vector<Row> Execution::gen_trace(std::vector<Instruction> const& instructio
returndata.insert(returndata.end(), ret.begin(), ret.end());
break;
}
case OpCode::DEBUGLOG:
// We want a noop, but we need to execute something that both advances the PC,
// and adds a valid row to the trace.
trace_builder.jump(pc + 1);
break;
case OpCode::TORADIXLE:
trace_builder.op_to_radix_le(std::get<uint8_t>(inst.operands.at(0)),
std::get<uint32_t>(inst.operands.at(1)),
Expand All @@ -283,6 +286,8 @@ std::vector<Row> Execution::gen_trace(std::vector<Instruction> const& instructio
std::get<uint32_t>(inst.operands.at(4)));
break;
default:
throw_or_abort("Don't know how to execute opcode " + to_hex(inst.op_code) + " at pc " + std::to_string(pc) +
".");
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const INSTRUCTION_SET = () =>
// Misc
[DebugLog.opcode, DebugLog],

// //// Gadgets
// Gadgets
[Keccak.opcode, Keccak],
[Poseidon2.opcode, Poseidon2],
[Sha256.opcode, Sha256],
Expand Down

0 comments on commit eba345b

Please sign in to comment.