Skip to content

Commit c0a332a

Browse files
author
jeanmon
committed
Simulator is enforcing integral tag type for DIV and field tag type for
FDIV
1 parent a1ba20f commit c0a332a

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

yarn-project/simulator/src/avm/opcodes/arithmetic.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import type { AvmContext } from '../avm_context.js';
2-
import { type Field, type MemoryValue } from '../avm_memory_types.js';
2+
import {
3+
type Field,
4+
type MemoryValue,
5+
TaggedMemory,
6+
type TaggedMemoryInterface,
7+
TypeTag,
8+
} from '../avm_memory_types.js';
39
import { ArithmeticError } from '../errors.js';
410
import { Opcode } from '../serialization/instruction_serialization.js';
511
import { Addressing } from './addressing_mode.js';
@@ -13,7 +19,7 @@ export abstract class ThreeOperandArithmeticInstruction extends ThreeOperandInst
1319
const operands = [this.aOffset, this.bOffset, this.dstOffset];
1420
const addressing = Addressing.fromWire(this.indirect, operands.length);
1521
const [aOffset, bOffset, dstOffset] = addressing.resolve(operands, memory);
16-
memory.checkTagsAreSame(aOffset, bOffset);
22+
this.checkTags(memory, aOffset, bOffset);
1723

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

2733
protected abstract compute(a: MemoryValue, b: MemoryValue): MemoryValue;
34+
protected checkTags(memory: TaggedMemoryInterface, aOffset: number, bOffset: number) {
35+
memory.checkTagsAreSame(aOffset, bOffset);
36+
}
2837
}
2938

3039
export class Add extends ThreeOperandArithmeticInstruction {
@@ -65,6 +74,11 @@ export class Div extends ThreeOperandArithmeticInstruction {
6574

6675
return a.div(b);
6776
}
77+
78+
protected override checkTags(memory: TaggedMemoryInterface, aOffset: number, bOffset: number) {
79+
memory.checkTagsAreSame(aOffset, bOffset);
80+
TaggedMemory.checkIsIntegralTag(memory.getTag(aOffset)); // Follows that bOffset tag is also of integral type
81+
}
6882
}
6983

7084
export class FieldDiv extends ThreeOperandArithmeticInstruction {
@@ -75,4 +89,9 @@ export class FieldDiv extends ThreeOperandArithmeticInstruction {
7589
// return (a as Field).fdiv(b as Field);
7690
return a.fdiv(b);
7791
}
92+
93+
protected override checkTags(memory: TaggedMemoryInterface, aOffset: number, bOffset: number) {
94+
memory.checkTagsAreSame(aOffset, bOffset);
95+
memory.checkTag(TypeTag.FIELD, aOffset); // Follows that bOffset has also tag of type Field
96+
}
7897
}

0 commit comments

Comments
 (0)