Skip to content

Commit

Permalink
chore(avm-simulator): cleanup, tags as first instruction constructor …
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanks12 authored Jan 29, 2024
1 parent cb00630 commit 458f650
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 78 deletions.
5 changes: 3 additions & 2 deletions yarn-project/acir-simulator/src/avm/avm_memory_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ export class TaggedMemory {

public getAs<T>(offset: number): T {
assert(offset < TaggedMemory.MAX_MEMORY_SIZE);
const e = this._mem[offset];
return <T>e;
const word = this._mem[offset];
return word as T;
}

public getSlice(offset: number, size: number): MemoryValue[] {
Expand Down Expand Up @@ -282,6 +282,7 @@ export class TaggedMemory {

// Truncates the value to fit the type.
public static integralFromTag(v: bigint, tag: TypeTag): IntegralValue {
v = BigInt(v); // FIXME: not sure why this cast is needed, but this errors otherwise
switch (tag) {
case TypeTag.UINT8:
return new Uint8(v & ((1n << 8n) - 1n));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ describe('interpreter', () => {
const calldata: Fr[] = [new Fr(1), new Fr(2)];

const instructions: Instruction[] = [
new CalldataCopy(/*cdOffset=*/ 0, /*copySize=*/ 2, /*destOffset=*/ 0),
new Add(/*aOffset=*/ 0, /*bOffset=*/ 1, /*destOffset=*/ 2),
new CalldataCopy(/*cdOffset=*/ 0, /*copySize=*/ 2, /*dstOffset=*/ 0),
new Add(/*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 2),
new Return(/*returnOffset=*/ 2, /*copySize=*/ 1),
];

Expand Down
16 changes: 8 additions & 8 deletions yarn-project/acir-simulator/src/avm/opcodes/arithmetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class Add extends Instruction {
static type: string = 'ADD';
static numberOfOperands = 3;

constructor(private aOffset: number, private bOffset: number, private destOffset: number) {
constructor(private aOffset: number, private bOffset: number, private dstOffset: number) {
super();
}

Expand All @@ -15,7 +15,7 @@ export class Add extends Instruction {
const b = machineState.memory.get(this.bOffset);

const dest = a.add(b);
machineState.memory.set(this.destOffset, dest);
machineState.memory.set(this.dstOffset, dest);

this.incrementPc(machineState);
}
Expand All @@ -25,7 +25,7 @@ export class Sub extends Instruction {
static type: string = 'SUB';
static numberOfOperands = 3;

constructor(private aOffset: number, private bOffset: number, private destOffset: number) {
constructor(private aOffset: number, private bOffset: number, private dstOffset: number) {
super();
}

Expand All @@ -34,7 +34,7 @@ export class Sub extends Instruction {
const b = machineState.memory.get(this.bOffset);

const dest = a.sub(b);
machineState.memory.set(this.destOffset, dest);
machineState.memory.set(this.dstOffset, dest);

this.incrementPc(machineState);
}
Expand All @@ -44,7 +44,7 @@ export class Mul extends Instruction {
static type: string = 'MUL';
static numberOfOperands = 3;

constructor(private aOffset: number, private bOffset: number, private destOffset: number) {
constructor(private aOffset: number, private bOffset: number, private dstOffset: number) {
super();
}

Expand All @@ -53,7 +53,7 @@ export class Mul extends Instruction {
const b = machineState.memory.get(this.bOffset);

const dest = a.mul(b);
machineState.memory.set(this.destOffset, dest);
machineState.memory.set(this.dstOffset, dest);

this.incrementPc(machineState);
}
Expand All @@ -64,7 +64,7 @@ export class Div extends Instruction {
static type: string = 'DIV';
static numberOfOperands = 3;

constructor(private aOffset: number, private bOffset: number, private destOffset: number) {
constructor(private aOffset: number, private bOffset: number, private dstOffset: number) {
super();
}

Expand All @@ -73,7 +73,7 @@ export class Div extends Instruction {
const b = machineState.memory.get(this.bOffset);

const dest = a.div(b);
machineState.memory.set(this.destOffset, dest);
machineState.memory.set(this.dstOffset, dest);

this.incrementPc(machineState);
}
Expand Down
22 changes: 11 additions & 11 deletions yarn-project/acir-simulator/src/avm/opcodes/bitwise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('Bitwise instructions', () => {
machineState.memory.set(0, new Uint32(0b11111110010011100100n));
machineState.memory.set(1, new Uint32(0b11100100111001001111n));

await new And(0, 1, 2, TypeTag.UINT32).execute(machineState, journal);
await new And(TypeTag.UINT32, 0, 1, 2).execute(machineState, journal);

const actual = machineState.memory.get(2);
expect(actual).toEqual(new Uint32(0b11100100010001000100n));
Expand All @@ -32,7 +32,7 @@ describe('Bitwise instructions', () => {
machineState.memory.set(0, a);
machineState.memory.set(1, b);

await new Or(0, 1, 2, TypeTag.UINT32).execute(machineState, journal);
await new Or(TypeTag.UINT32, 0, 1, 2).execute(machineState, journal);

const expected = new Uint32(0b11111110111011101111n);
const actual = machineState.memory.get(2);
Expand All @@ -46,7 +46,7 @@ describe('Bitwise instructions', () => {
machineState.memory.set(0, a);
machineState.memory.set(1, b);

await new Xor(0, 1, 2, TypeTag.UINT32).execute(machineState, journal);
await new Xor(TypeTag.UINT32, 0, 1, 2).execute(machineState, journal);

const expected = new Uint32(0b00011010101010101011n);
const actual = machineState.memory.get(2);
Expand All @@ -61,7 +61,7 @@ describe('Bitwise instructions', () => {
machineState.memory.set(0, a);
machineState.memory.set(1, b);

await new Shr(0, 1, 2, TypeTag.UINT32).execute(machineState, journal);
await new Shr(TypeTag.UINT32, 0, 1, 2).execute(machineState, journal);

const expected = a;
const actual = machineState.memory.get(2);
Expand All @@ -75,7 +75,7 @@ describe('Bitwise instructions', () => {
machineState.memory.set(0, a);
machineState.memory.set(1, b);

await new Shr(0, 1, 2, TypeTag.UINT32).execute(machineState, journal);
await new Shr(TypeTag.UINT32, 0, 1, 2).execute(machineState, journal);

const expected = new Uint32(0b00111111100100111001n);
const actual = machineState.memory.get(2);
Expand All @@ -89,7 +89,7 @@ describe('Bitwise instructions', () => {
machineState.memory.set(0, a);
machineState.memory.set(1, b);

await new Shr(0, 1, 2, TypeTag.UINT32).execute(machineState, journal);
await new Shr(TypeTag.UINT32, 0, 1, 2).execute(machineState, journal);

const expected = new Uint32(0b01n);
const actual = machineState.memory.get(2);
Expand All @@ -105,7 +105,7 @@ describe('Bitwise instructions', () => {
machineState.memory.set(0, a);
machineState.memory.set(1, b);

await new Shl(0, 1, 2, TypeTag.UINT32).execute(machineState, journal);
await new Shl(TypeTag.UINT32, 0, 1, 2).execute(machineState, journal);

const expected = a;
const actual = machineState.memory.get(2);
Expand All @@ -119,7 +119,7 @@ describe('Bitwise instructions', () => {
machineState.memory.set(0, a);
machineState.memory.set(1, b);

await new Shl(0, 1, 2, TypeTag.UINT32).execute(machineState, journal);
await new Shl(TypeTag.UINT32, 0, 1, 2).execute(machineState, journal);

const expected = new Uint32(0b1111111001001110010000n);
const actual = machineState.memory.get(2);
Expand All @@ -133,7 +133,7 @@ describe('Bitwise instructions', () => {
machineState.memory.set(0, a);
machineState.memory.set(1, b);

await new Shl(0, 1, 2, TypeTag.UINT16).execute(machineState, journal);
await new Shl(TypeTag.UINT16, 0, 1, 2).execute(machineState, journal);

const expected = new Uint16(0n);
const actual = machineState.memory.get(2);
Expand All @@ -147,7 +147,7 @@ describe('Bitwise instructions', () => {
machineState.memory.set(0, a);
machineState.memory.set(1, b);

await new Shl(0, 1, 2, TypeTag.UINT16).execute(machineState, journal);
await new Shl(TypeTag.UINT16, 0, 1, 2).execute(machineState, journal);

const expected = new Uint16(0b1001001110011100n);
const actual = machineState.memory.get(2);
Expand All @@ -160,7 +160,7 @@ describe('Bitwise instructions', () => {

machineState.memory.set(0, a);

await new Not(0, 1, TypeTag.UINT16).execute(machineState, journal);
await new Not(TypeTag.UINT16, 0, 1).execute(machineState, journal);

const expected = new Uint16(0b1001101100011011n); // high bits!
const actual = machineState.memory.get(1);
Expand Down
24 changes: 12 additions & 12 deletions yarn-project/acir-simulator/src/avm/opcodes/bitwise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class And extends Instruction {
static type: string = 'AND';
static numberOfOperands = 3;

constructor(private aOffset: number, private bOffset: number, private destOffset: number, private inTag: TypeTag) {
constructor(private inTag: TypeTag, private aOffset: number, private bOffset: number, private dstOffset: number) {
super();
}

Expand All @@ -18,7 +18,7 @@ export class And extends Instruction {
const b = machineState.memory.getAs<IntegralValue>(this.bOffset);

const res = a.and(b);
machineState.memory.set(this.destOffset, res);
machineState.memory.set(this.dstOffset, res);

this.incrementPc(machineState);
}
Expand All @@ -28,7 +28,7 @@ export class Or extends Instruction {
static type: string = 'OR';
static numberOfOperands = 3;

constructor(private aOffset: number, private bOffset: number, private destOffset: number, private inTag: TypeTag) {
constructor(private inTag: TypeTag, private aOffset: number, private bOffset: number, private dstOffset: number) {
super();
}

Expand All @@ -39,7 +39,7 @@ export class Or extends Instruction {
const b = machineState.memory.getAs<IntegralValue>(this.bOffset);

const res = a.or(b);
machineState.memory.set(this.destOffset, res);
machineState.memory.set(this.dstOffset, res);

this.incrementPc(machineState);
}
Expand All @@ -49,7 +49,7 @@ export class Xor extends Instruction {
static type: string = 'XOR';
static numberOfOperands = 3;

constructor(private aOffset: number, private bOffset: number, private destOffset: number, private inTag: TypeTag) {
constructor(private inTag: TypeTag, private aOffset: number, private bOffset: number, private dstOffset: number) {
super();
}

Expand All @@ -60,7 +60,7 @@ export class Xor extends Instruction {
const b = machineState.memory.getAs<IntegralValue>(this.bOffset);

const res = a.xor(b);
machineState.memory.set(this.destOffset, res);
machineState.memory.set(this.dstOffset, res);

this.incrementPc(machineState);
}
Expand All @@ -70,7 +70,7 @@ export class Not extends Instruction {
static type: string = 'NOT';
static numberOfOperands = 2;

constructor(private aOffset: number, private destOffset: number, private inTag: TypeTag) {
constructor(private inTag: TypeTag, private aOffset: number, private dstOffset: number) {
super();
}

Expand All @@ -80,7 +80,7 @@ export class Not extends Instruction {
const a = machineState.memory.getAs<IntegralValue>(this.aOffset);

const res = a.not();
machineState.memory.set(this.destOffset, res);
machineState.memory.set(this.dstOffset, res);

this.incrementPc(machineState);
}
Expand All @@ -90,7 +90,7 @@ export class Shl extends Instruction {
static type: string = 'SHL';
static numberOfOperands = 3;

constructor(private aOffset: number, private bOffset: number, private destOffset: number, private inTag: TypeTag) {
constructor(private inTag: TypeTag, private aOffset: number, private bOffset: number, private dstOffset: number) {
super();
}

Expand All @@ -101,7 +101,7 @@ export class Shl extends Instruction {
const b = machineState.memory.getAs<IntegralValue>(this.bOffset);

const res = a.shl(b);
machineState.memory.set(this.destOffset, res);
machineState.memory.set(this.dstOffset, res);

this.incrementPc(machineState);
}
Expand All @@ -111,7 +111,7 @@ export class Shr extends Instruction {
static type: string = 'SHR';
static numberOfOperands = 3;

constructor(private aOffset: number, private bOffset: number, private destOffset: number, private inTag: TypeTag) {
constructor(private inTag: TypeTag, private aOffset: number, private bOffset: number, private dstOffset: number) {
super();
}

Expand All @@ -122,7 +122,7 @@ export class Shr extends Instruction {
const b = machineState.memory.getAs<IntegralValue>(this.bOffset);

const res = a.shr(b);
machineState.memory.set(this.destOffset, res);
machineState.memory.set(this.dstOffset, res);

this.incrementPc(machineState);
}
Expand Down
16 changes: 8 additions & 8 deletions yarn-project/acir-simulator/src/avm/opcodes/control_flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,17 @@ describe('Control Flow Opcodes', () => {
new Lt(TypeTag.UINT16, 0, 1, 2),
new Lte(TypeTag.UINT16, 0, 1, 2),
new Eq(TypeTag.UINT16, 0, 1, 2),
new Xor(0, 1, 2, TypeTag.UINT16),
new And(0, 1, 2, TypeTag.UINT16),
new Or(0, 1, 2, TypeTag.UINT16),
new Shl(0, 1, 2, TypeTag.UINT16),
new Shr(0, 1, 2, TypeTag.UINT16),
new Not(0, 2, TypeTag.UINT16),
new Xor(TypeTag.UINT16, 0, 1, 2),
new And(TypeTag.UINT16, 0, 1, 2),
new Or(TypeTag.UINT16, 0, 1, 2),
new Shl(TypeTag.UINT16, 0, 1, 2),
new Shr(TypeTag.UINT16, 0, 1, 2),
new Not(TypeTag.UINT16, 0, 2),
new CalldataCopy(0, 1, 2),
new Set(0n, 1, TypeTag.UINT16),
new Set(TypeTag.UINT16, 0n, 1),
new Mov(0, 1),
new CMov(0, 1, 2, 3),
new Cast(0, 1, TypeTag.UINT16),
new Cast(TypeTag.UINT16, 0, 1),
];

for (const instruction of instructions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Return extends Instruction {
async execute(machineState: AvmMachineState, _journal: AvmJournal): Promise<void> {
const returnData = machineState.memory
.getSlice(this.returnOffset, this.copySize)
.map(fvt => new Fr(fvt.toBigInt()));
.map(word => new Fr(word.toBigInt()));

machineState.setReturnData(returnData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ export function decodeBytecode(bytecode: Buffer): Instruction[] {
const opcodeByte = bytecode[bytePtr];
bytePtr += AVM_OPCODE_BYTE_LENGTH;
if (!(opcodeByte in Opcode)) {
throw new Error(`Opcode ${opcodeByte} not implemented`);
throw new Error(`Opcode 0x${opcodeByte.toString(16)} not implemented`);
}
const opcode = opcodeByte as Opcode;

const instructionType = INSTRUCTION_SET.get(opcode);
if (instructionType === undefined) {
throw new Error(`Opcode ${opcode} not implemented`);
throw new Error(`Opcode 0x${opcode.toString(16)} not implemented`);
}
const numberOfOperands = instructionType.numberOfOperands;
const operands: number[] = [];
for (let i = 0; i < numberOfOperands; i++) {
// TODO: support constants which might not be u32s
const operand = bytecode.readUInt32BE(bytePtr);
bytePtr += AVM_OPERAND_BYTE_LENGTH;
operands.push(operand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import { Opcode } from './opcodes.js';
export function encodeToBytecode(opcode: Opcode, args: number[]): Buffer {
const instructionType = INSTRUCTION_SET.get(opcode);
if (instructionType === undefined) {
throw new Error(`Opcode ${opcode} not implemented`);
throw new Error(`Opcode 0x${opcode.toString(16)} not implemented`);
}

const numberOfOperands = instructionType.numberOfOperands;
if (args.length !== numberOfOperands) {
throw new Error(`Opcode ${opcode} expects ${numberOfOperands} arguments, but ${args.length} were provided`);
throw new Error(
`Opcode 0x${opcode.toString(16)} expects ${numberOfOperands} arguments, but ${args.length} were provided`,
);
}

const bytecode = Buffer.alloc(AVM_OPCODE_BYTE_LENGTH + numberOfOperands * AVM_OPERAND_BYTE_LENGTH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('External Calls', () => {

const otherContextInstructions: [Opcode, any[]][] = [
// Place [1,2,3] into memory
[Opcode.CALLDATACOPY, [/*value=*/ 0, /*copySize=*/ argsSize, /*destOffset=*/ 0]],
[Opcode.CALLDATACOPY, [/*value=*/ 0, /*copySize=*/ argsSize, /*dstOffset=*/ 0]],
// Store 1 into slot 1
[Opcode.SSTORE, [/*slotOffset=*/ 0, /*dataOffset=*/ 0]],
// Return [1,2] from memory
Expand Down Expand Up @@ -110,7 +110,7 @@ describe('External Calls', () => {

const otherContextInstructions: [Opcode, any[]][] = [
// Place [1,2,3] into memory
[Opcode.CALLDATACOPY, [/*value=*/ 0, /*copySize=*/ argsSize, /*destOffset=*/ 0]],
[Opcode.CALLDATACOPY, [/*value=*/ 0, /*copySize=*/ argsSize, /*dstOffset=*/ 0]],
[Opcode.SSTORE, [/*slotOffset*/ 1, /*dataOffset=*/ 0]],
];

Expand Down
Loading

0 comments on commit 458f650

Please sign in to comment.