Skip to content

Commit

Permalink
fix(avm-transpiler): RETURN is direct (#5277)
Browse files Browse the repository at this point in the history
* Brillig's `STOP` uses direct memory access, so it should be transpiled with `ALL_DIRECT`.
* Implement indirect support for `RETURN`, `REVERT`, `CALLDATACOPY` for the future.

cc @sirasistant
  • Loading branch information
fcarreiro authored Mar 18, 2024
1 parent ec21ccb commit f90b2cf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion avm-transpiler/src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub fn brillig_to_avm(brillig: &Brillig) -> Vec<u8> {
BrilligOpcode::Stop { return_data_offset, return_data_size } => {
avm_instrs.push(AvmInstruction {
opcode: AvmOpcode::RETURN,
indirect: Some(ZEROTH_OPERAND_INDIRECT),
indirect: Some(ALL_DIRECT),
operands: vec![
AvmOperand::U32 { value: *return_data_offset as u32 },
AvmOperand::U32 { value: *return_data_size as u32 },
Expand Down
12 changes: 7 additions & 5 deletions yarn-project/simulator/src/avm/opcodes/external_calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,16 @@ export class Return extends Instruction {
}

async execute(context: AvmContext): Promise<void> {
const output = context.machineState.memory.getSlice(this.returnOffset, this.copySize).map(word => word.toFr());
const [returnOffset] = Addressing.fromWire(this.indirect).resolve([this.returnOffset], context.machineState.memory);

const output = context.machineState.memory.getSlice(returnOffset, this.copySize).map(word => word.toFr());

context.machineState.return(output);
}
}

export class Revert extends Instruction {
static type: string = 'RETURN';
static type: string = 'REVERT';
static readonly opcode: Opcode = Opcode.REVERT;
// Informs (de)serialization. See Instruction.deserialize.
static readonly wireFormat: OperandType[] = [
Expand All @@ -187,9 +189,9 @@ export class Revert extends Instruction {
}

async execute(context: AvmContext): Promise<void> {
const output = context.machineState.memory
.getSlice(this.returnOffset, this.returnOffset + this.retSize)
.map(word => word.toFr());
const [returnOffset] = Addressing.fromWire(this.indirect).resolve([this.returnOffset], context.machineState.memory);

const output = context.machineState.memory.getSlice(returnOffset, this.retSize).map(word => word.toFr());

context.machineState.revert(output);
}
Expand Down
4 changes: 3 additions & 1 deletion yarn-project/simulator/src/avm/opcodes/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,13 @@ export class CalldataCopy extends Instruction {
}

async execute(context: AvmContext): Promise<void> {
const [dstOffset] = Addressing.fromWire(this.indirect).resolve([this.dstOffset], context.machineState.memory);

const transformedData = context.environment.calldata
.slice(this.cdOffset, this.cdOffset + this.copySize)
.map(f => new Field(f));

context.machineState.memory.setSlice(this.dstOffset, transformedData);
context.machineState.memory.setSlice(dstOffset, transformedData);

context.machineState.incrementPc();
}
Expand Down

0 comments on commit f90b2cf

Please sign in to comment.