Skip to content

Commit

Permalink
fixed rename bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mmk-1 committed Oct 9, 2023
1 parent 82aa05b commit 8192a63
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 58 deletions.
40 changes: 19 additions & 21 deletions pkg/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func NewVirtualMachine(initialContext Context, memory *mem.Memory, config Virtua
Memory: memory,
Trace: trace,
config: config,
instructions: make(map[uint64]*Instruction),
instructions: make(map[uint64]*assembler.Instruction),
}, nil
}

Expand Down Expand Up @@ -138,8 +138,7 @@ func (vm *VirtualMachine) RunStep(hintRunner HintRunner) error {
return nil
}


func (vm *VirtualMachine) RunInstruction(instruction *Instruction) error {
func (vm *VirtualMachine) RunInstruction(instruction *assembler.Instruction) error {
dstAddr, err := vm.getDstAddr(instruction)
if err != nil {
return fmt.Errorf("dst cell: %w", err)
Expand Down Expand Up @@ -202,7 +201,7 @@ func (vm *VirtualMachine) ExecutionTrace() ([]Trace, error) {
return vm.relocateTrace(), nil
}

func (vm *VirtualMachine) getDstAddr(instruction *Instruction) (mem.MemoryAddress, error) {
func (vm *VirtualMachine) getDstAddr(instruction *assembler.Instruction) (mem.MemoryAddress, error) {
var dstRegister uint64
if instruction.DstRegister == assembler.Ap {
dstRegister = vm.Context.Ap
Expand All @@ -217,7 +216,7 @@ func (vm *VirtualMachine) getDstAddr(instruction *Instruction) (mem.MemoryAddres
return mem.MemoryAddress{SegmentIndex: ExecutionSegment, Offset: addr}, nil
}

func (vm *VirtualMachine) getOp0Addr(instruction *Instruction) (mem.MemoryAddress, error) {
func (vm *VirtualMachine) getOp0Addr(instruction *assembler.Instruction) (mem.MemoryAddress, error) {
var op0Register uint64
if instruction.Op0Register == assembler.Ap {
op0Register = vm.Context.Ap
Expand All @@ -232,8 +231,7 @@ func (vm *VirtualMachine) getOp0Addr(instruction *Instruction) (mem.MemoryAddres
return mem.MemoryAddress{SegmentIndex: ExecutionSegment, Offset: addr}, nil
}


func (vm *VirtualMachine) getOp1Addr(instruction *Instruction, op0Addr *mem.MemoryAddress) (mem.MemoryAddress, error) {
func (vm *VirtualMachine) getOp1Addr(instruction *assembler.Instruction, op0Addr *mem.MemoryAddress) (mem.MemoryAddress, error) {
var op1Address mem.MemoryAddress
switch instruction.Op1Source {
case assembler.Op0:
Expand Down Expand Up @@ -269,10 +267,10 @@ func (vm *VirtualMachine) getOp1Addr(instruction *Instruction, op0Addr *mem.Memo
// dstCell value and either op0Cell xor op1Cell. This function infers the
// unknow operand as well as the `res` auxiliar value
func (vm *VirtualMachine) inferOperand(
instruction *Instruction, dstAddr *mem.MemoryAddress, op0Addr *mem.MemoryAddress, op1Addr *mem.MemoryAddress,
instruction *assembler.Instruction, dstAddr *mem.MemoryAddress, op0Addr *mem.MemoryAddress, op1Addr *mem.MemoryAddress,
) (mem.MemoryValue, error) {
if instruction.Opcode != AssertEq ||
(instruction.Res == Unconstrained) {
if instruction.Opcode != assembler.OpCodeAssertEq ||
(instruction.Res == assembler.Unconstrained) {
return mem.MemoryValue{}, nil
}

Expand All @@ -298,7 +296,7 @@ func (vm *VirtualMachine) inferOperand(
return mem.MemoryValue{}, fmt.Errorf("value at dst is unknown")
}

if instruction.Res == Op1 && !op1Value.Known() {
if instruction.Res == assembler.Op1 && !op1Value.Known() {
if err = vm.Memory.WriteToAddress(op1Addr, &dstValue); err != nil {
return mem.MemoryValue{}, err
}
Expand All @@ -315,8 +313,8 @@ func (vm *VirtualMachine) inferOperand(
unknownOpAddr = op0Addr
}

var missingVal mem.MemoryValue
if instruction.Res == assembler.AddOperands {
var missingVal mem.MemoryValue
if instruction.Res == assembler.AddOperands {
missingVal = mem.EmptyMemoryValueAs(dstValue.IsAddress())
err = missingVal.Sub(&dstValue, &knownOpValue)
} else {
Expand All @@ -334,12 +332,12 @@ func (vm *VirtualMachine) inferOperand(
}

func (vm *VirtualMachine) computeRes(
instruction *Instruction, op0Addr *mem.MemoryAddress, op1Addr *mem.MemoryAddress,
instruction *assembler.Instruction, op0Addr *mem.MemoryAddress, op1Addr *mem.MemoryAddress,
) (mem.MemoryValue, error) {
switch instruction.Res {
case Unconstrained:
case assembler.Unconstrained:
return mem.MemoryValue{}, nil
case Op1:
case assembler.Op1:
return vm.Memory.ReadFromAddress(op1Addr)
default:
op0, err := vm.Memory.ReadFromAddress(op0Addr)
Expand All @@ -353,9 +351,9 @@ func (vm *VirtualMachine) computeRes(
}

res := mem.EmptyMemoryValueAs(op0.IsAddress() || op1.IsAddress())
if instruction.Res == AddOperands {
if instruction.Res == assembler.AddOperands {
err = res.Add(&op0, &op1)
} else if instruction.Res == MulOperands {
} else if instruction.Res == assembler.MulOperands {
err = res.Mul(&op0, &op1)
} else {
return mem.MemoryValue{}, fmt.Errorf("invalid res flag value: %d", instruction.Res)
Expand All @@ -365,13 +363,13 @@ func (vm *VirtualMachine) computeRes(
}

func (vm *VirtualMachine) opcodeAssertions(
instruction *Instruction,
instruction *assembler.Instruction,
dstAddr *mem.MemoryAddress,
op0Addr *mem.MemoryAddress,
res *mem.MemoryValue,
) error {
switch instruction.Opcode {
case Call:
case assembler.OpCodeCall:
fpAddr := vm.Context.AddressFp()
fpMv := mem.MemoryValueFromMemoryAddress(&fpAddr)
// Store at [ap] the current fp
Expand Down Expand Up @@ -482,7 +480,7 @@ func (vm *VirtualMachine) updateAp(instruction *assembler.Instruction, res *mem.
return 0, fmt.Errorf("cannot update ap, unknown ApUpdate flag: %d", instruction.ApUpdate)
}

func (vm *VirtualMachine) updateFp(instruction *Instruction, dstAddr *mem.MemoryAddress) (uint64, error) {
func (vm *VirtualMachine) updateFp(instruction *assembler.Instruction, dstAddr *mem.MemoryAddress) (uint64, error) {
switch instruction.Opcode {
case assembler.OpCodeCall:
// [ap] and [ap + 1] are written to memory
Expand Down
74 changes: 37 additions & 37 deletions pkg/vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func TestGetCellApDstWithDifferentOffsets(t *testing.T) {

writeToDataSegment(vm, uint64(ap+offset), mem.MemoryValueFromInt(200))

instruction := Instruction{
instruction := assembler.Instruction{
OffDest: int16(offset),
DstRegister: Ap,
DstRegister: assembler.Ap,
}

addr, err := vm.getDstAddr(&instruction)
Expand Down Expand Up @@ -173,9 +173,9 @@ func TestGetApCellOp0NegOff(t *testing.T) {
vm.Context.Ap = ap
writeToDataSegment(vm, ap+offOp0, mem.MemoryValueFromInt(155))

instruction := Instruction{
instruction := assembler.Instruction{
OffOp0: offOp0,
Op0Register: Ap,
Op0Register: assembler.Ap,
}

addr, err := vm.getOp0Addr(&instruction)
Expand All @@ -199,9 +199,9 @@ func TestGetFpCellOp0(t *testing.T) {
vm.Context.Fp = fp
writeToDataSegment(vm, fp+offOp0, mem.MemoryValueFromInt(365))

instruction := Instruction{
instruction := assembler.Instruction{
OffOp0: offOp0,
Op0Register: Fp,
Op0Register: assembler.Fp,
}

addr, err := vm.getOp0Addr(&instruction)
Expand All @@ -225,9 +225,9 @@ func TestGetFpCellOp0NegOff(t *testing.T) {
vm.Context.Fp = fp
writeToDataSegment(vm, fp+offOp0, mem.MemoryValueFromInt(286))

instruction := Instruction{
instruction := assembler.Instruction{
OffOp0: offOp0,
Op0Register: Fp,
Op0Register: assembler.Fp,
}

addr, err := vm.getOp0Addr(&instruction)
Expand Down Expand Up @@ -411,7 +411,7 @@ func TestGetApNegCellOp1(t *testing.T) {
func TestInferOperandSub(t *testing.T) {
vm, _ := defaultVirtualMachine()
instruction := assembler.Instruction{
Opcode: assembler.AssertEq,
Opcode: assembler.OpCodeAssertEq,
Res: assembler.AddOperands,
}
writeToDataSegment(vm, 0, mem.MemoryValueFromSegmentAndOffset(3, 15)) //destCell
Expand All @@ -432,9 +432,9 @@ func TestInferOperandSub(t *testing.T) {

func TestInferResOp1(t *testing.T) {
vm, _ := defaultVirtualMachine()
instruction := Instruction{
Opcode: AssertEq,
Res: Op1,
instruction := assembler.Instruction{
Opcode: assembler.OpCodeAssertEq,
Res: assembler.Op1,
}
writeToDataSegment(vm, 0, mem.MemoryValueFromInt(1337)) //destCell
dstAddr := mem.MemoryAddress{SegmentIndex: ExecutionSegment, Offset: 0}
Expand All @@ -453,15 +453,15 @@ func TestInferResOp1(t *testing.T) {

func TestComputeResUnconstrained(t *testing.T) {
vm, _ := defaultVirtualMachine()
instruction := assembler.Instruction{Res: Unconstrained}
instruction := assembler.Instruction{Res: assembler.Unconstrained}
res, err := vm.computeRes(&instruction, nil, nil)
require.NoError(t, err)
require.False(t, res.Known())
}

func TestComputeResOp1(t *testing.T) {
vm, _ := defaultVirtualMachine()
instruction := Instruction{Res: Op1}
instruction := assembler.Instruction{Res: assembler.Op1}

writeToDataSegment(vm, 3, mem.MemoryValueFromInt(15))
op1Addr := mem.MemoryAddress{SegmentIndex: ExecutionSegment, Offset: 3}
Expand All @@ -475,7 +475,7 @@ func TestComputeResOp1(t *testing.T) {

func TestComputeAddResAddrToFelt(t *testing.T) {
vm, _ := defaultVirtualMachine()
instruction := Instruction{Res: AddOperands}
instruction := assembler.Instruction{Res: assembler.AddOperands}

op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromSegmentAndOffset(2, 10))
op1Addr := writeToDataSegment(vm, 8, mem.MemoryValueFromInt(15))
Expand All @@ -489,7 +489,7 @@ func TestComputeAddResAddrToFelt(t *testing.T) {

func TestComputeAddResFeltToAddr(t *testing.T) {
vm, _ := defaultVirtualMachine()
instruction := Instruction{Res: AddOperands}
instruction := assembler.Instruction{Res: assembler.AddOperands}

op0Addr := writeToDataSegment(vm, 2, mem.MemoryValueFromInt(8))
op1Addr := writeToDataSegment(vm, 5, mem.MemoryValueFromSegmentAndOffset(2, 7))
Expand All @@ -502,7 +502,7 @@ func TestComputeAddResFeltToAddr(t *testing.T) {

func TestComputeAddResBothAddrs(t *testing.T) {
vm, _ := defaultVirtualMachine()
instruction := Instruction{Res: AddOperands}
instruction := assembler.Instruction{Res: assembler.AddOperands}

op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromSegmentAndOffset(2, 10))
op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromSegmentAndOffset(2, 15))
Expand All @@ -513,7 +513,7 @@ func TestComputeAddResBothAddrs(t *testing.T) {

func TestComputeAddResBothFelts(t *testing.T) {
vm, _ := defaultVirtualMachine()
instruction := Instruction{Res: AddOperands}
instruction := assembler.Instruction{Res: assembler.AddOperands}

op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromInt(10))
op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromInt(15))
Expand All @@ -528,7 +528,7 @@ func TestComputeAddResBothFelts(t *testing.T) {
func TestComputeMulResPosToPosFelt(t *testing.T) {
//Positive Felt to Positive Felt compute
vm, _ := defaultVirtualMachine()
instruction := Instruction{Res: MulOperands}
instruction := assembler.Instruction{Res: assembler.MulOperands}

op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromInt(10))
op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromInt(15))
Expand All @@ -541,7 +541,7 @@ func TestComputeMulResPosToPosFelt(t *testing.T) {

func TestComputeMulResNegToPosFelts(t *testing.T) {
vm, _ := defaultVirtualMachine()
instruction := Instruction{Res: MulOperands}
instruction := assembler.Instruction{Res: assembler.MulOperands}
//Negative to Positive
op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromInt(-10))
op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromInt(15))
Expand All @@ -554,7 +554,7 @@ func TestComputeMulResNegToPosFelts(t *testing.T) {

func TestComputeMulResPosToNegFelt(t *testing.T) {
vm, _ := defaultVirtualMachine()
instruction := Instruction{Res: MulOperands}
instruction := assembler.Instruction{Res: assembler.MulOperands}
//Positive to Negative
op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromInt(10))
op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromInt(-15))
Expand All @@ -567,7 +567,7 @@ func TestComputeMulResPosToNegFelt(t *testing.T) {

func TestComputeMulResNegToNegFelt(t *testing.T) {
vm, _ := defaultVirtualMachine()
instruction := Instruction{Res: MulOperands}
instruction := assembler.Instruction{Res: assembler.MulOperands}
//Netagive to Negative
op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromInt(-10))
op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromInt(-15))
Expand All @@ -582,7 +582,7 @@ func TestComputeMulResNegToNegFelt(t *testing.T) {
// three failing cases
func TestComputeMulResAddrToFelt(t *testing.T) {
vm, _ := defaultVirtualMachine()
instruction := Instruction{Res: MulOperands}
instruction := assembler.Instruction{Res: assembler.MulOperands}

op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromSegmentAndOffset(2, 10))
op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromInt(15))
Expand All @@ -593,7 +593,7 @@ func TestComputeMulResAddrToFelt(t *testing.T) {

func TestComputeMulResFeltToAddr(t *testing.T) {
vm, _ := defaultVirtualMachine()
instruction := Instruction{Res: MulOperands}
instruction := assembler.Instruction{Res: assembler.MulOperands}

op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromInt(10))
op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromSegmentAndOffset(2, 15))
Expand All @@ -604,7 +604,7 @@ func TestComputeMulResFeltToAddr(t *testing.T) {

func TestComputeMulResBothAddrs(t *testing.T) {
vm, _ := defaultVirtualMachine()
instruction := Instruction{Res: MulOperands}
instruction := assembler.Instruction{Res: assembler.MulOperands}

op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromSegmentAndOffset(2, 10))
op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromSegmentAndOffset(2, 15))
Expand Down Expand Up @@ -748,9 +748,9 @@ func TestUpdateApSameAp(t *testing.T) {
vm, _ := defaultVirtualMachine()

vm.Context.Ap = 5
instruction := Instruction{
Opcode: Nop,
ApUpdate: SameAp,
instruction := assembler.Instruction{
Opcode: assembler.OpCodeNop,
ApUpdate: assembler.SameAp,
}

nextAp, err := vm.updateAp(&instruction, nil)
Expand All @@ -762,9 +762,9 @@ func TestUpdateApAddImmPos(t *testing.T) {
vm, _ := defaultVirtualMachine()

vm.Context.Ap = 5
instruction := Instruction{
Opcode: Nop,
ApUpdate: AddImm,
instruction := assembler.Instruction{
Opcode: assembler.OpCodeNop,
ApUpdate: assembler.AddRes,
}

res := mem.MemoryValueFromInt(7)
Expand All @@ -778,9 +778,9 @@ func TestUpdateApAddImmNeg(t *testing.T) {
vm, _ := defaultVirtualMachine()

vm.Context.Ap = 10
instruction := Instruction{
Opcode: Nop,
ApUpdate: AddImm,
instruction := assembler.Instruction{
Opcode: assembler.OpCodeNop,
ApUpdate: assembler.AddRes,
}

res := mem.MemoryValueFromInt(-3)
Expand Down Expand Up @@ -808,9 +808,9 @@ func TestUpdateApAddTwo(t *testing.T) {
vm, _ := defaultVirtualMachine()

vm.Context.Ap = 5
instruction := Instruction{
Opcode: Nop,
ApUpdate: Add2,
instruction := assembler.Instruction{
Opcode: assembler.OpCodeNop,
ApUpdate: assembler.Add2,
}

nextAp, err := vm.updateAp(&instruction, nil)
Expand Down

0 comments on commit 8192a63

Please sign in to comment.