Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(avm-simulator): cleanup, tags as first instruction constructor args #4244

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -207,8 +207,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;
fcarreiro marked this conversation as resolved.
Show resolved Hide resolved
}

public getSlice(offset: number, size: number): MemoryValue[] {
Expand Down Expand Up @@ -261,6 +261,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
fcarreiro marked this conversation as resolved.
Show resolved Hide resolved
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
12 changes: 6 additions & 6 deletions yarn-project/acir-simulator/src/avm/opcodes/comparators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class Eq extends Instruction {
static type: string = 'EQ';
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 @@ -16,7 +16,7 @@ export class Eq extends Instruction {
const b = machineState.memory.get(this.bOffset);

const dest = new Field(a.toBigInt() == b.toBigInt() ? 1 : 0);
machineState.memory.set(this.destOffset, dest);
machineState.memory.set(this.dstOffset, dest);

this.incrementPc(machineState);
}
Expand All @@ -26,7 +26,7 @@ export class Lt extends Instruction {
static type: string = 'Lt';
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 @@ -35,7 +35,7 @@ export class Lt extends Instruction {
const b = machineState.memory.get(this.bOffset);

const dest = new Field(a.toBigInt() < b.toBigInt() ? 1 : 0);
machineState.memory.set(this.destOffset, dest);
machineState.memory.set(this.dstOffset, dest);

this.incrementPc(machineState);
}
Expand All @@ -45,7 +45,7 @@ export class Lte extends Instruction {
static type: string = 'LTE';
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 @@ -54,7 +54,7 @@ export class Lte extends Instruction {
const b = machineState.memory.get(this.bOffset);

const dest = new Field(a.toBigInt() < b.toBigInt() ? 1 : 0);
machineState.memory.set(this.destOffset, dest);
machineState.memory.set(this.dstOffset, dest);

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(0, 1, 2),
new Lte(0, 1, 2),
new Eq(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()));
fcarreiro marked this conversation as resolved.
Show resolved Hide resolved

machineState.setReturnData(returnData);

Expand Down
Loading
Loading