Skip to content

Commit

Permalink
Simulator is enforcing integral tag type for DIV and field tag type for
Browse files Browse the repository at this point in the history
FDIV
  • Loading branch information
jeanmon committed Nov 13, 2024
1 parent a1ba20f commit c0a332a
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions yarn-project/simulator/src/avm/opcodes/arithmetic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { AvmContext } from '../avm_context.js';
import { type Field, type MemoryValue } from '../avm_memory_types.js';
import {
type Field,
type MemoryValue,
TaggedMemory,
type TaggedMemoryInterface,
TypeTag,
} from '../avm_memory_types.js';
import { ArithmeticError } from '../errors.js';
import { Opcode } from '../serialization/instruction_serialization.js';
import { Addressing } from './addressing_mode.js';
Expand All @@ -13,7 +19,7 @@ export abstract class ThreeOperandArithmeticInstruction extends ThreeOperandInst
const operands = [this.aOffset, this.bOffset, this.dstOffset];
const addressing = Addressing.fromWire(this.indirect, operands.length);
const [aOffset, bOffset, dstOffset] = addressing.resolve(operands, memory);
memory.checkTagsAreSame(aOffset, bOffset);
this.checkTags(memory, aOffset, bOffset);

const a = memory.get(aOffset);
const b = memory.get(bOffset);
Expand All @@ -25,6 +31,9 @@ export abstract class ThreeOperandArithmeticInstruction extends ThreeOperandInst
}

protected abstract compute(a: MemoryValue, b: MemoryValue): MemoryValue;
protected checkTags(memory: TaggedMemoryInterface, aOffset: number, bOffset: number) {
memory.checkTagsAreSame(aOffset, bOffset);
}
}

export class Add extends ThreeOperandArithmeticInstruction {
Expand Down Expand Up @@ -65,6 +74,11 @@ export class Div extends ThreeOperandArithmeticInstruction {

return a.div(b);
}

protected override checkTags(memory: TaggedMemoryInterface, aOffset: number, bOffset: number) {
memory.checkTagsAreSame(aOffset, bOffset);
TaggedMemory.checkIsIntegralTag(memory.getTag(aOffset)); // Follows that bOffset tag is also of integral type
}
}

export class FieldDiv extends ThreeOperandArithmeticInstruction {
Expand All @@ -75,4 +89,9 @@ export class FieldDiv extends ThreeOperandArithmeticInstruction {
// return (a as Field).fdiv(b as Field);
return a.fdiv(b);
}

protected override checkTags(memory: TaggedMemoryInterface, aOffset: number, bOffset: number) {
memory.checkTagsAreSame(aOffset, bOffset);
memory.checkTag(TypeTag.FIELD, aOffset); // Follows that bOffset has also tag of type Field
}
}

0 comments on commit c0a332a

Please sign in to comment.