Skip to content

Commit

Permalink
Decode operands tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsc2 committed Aug 3, 2023
1 parent 83a6a53 commit b6e4d05
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
29 changes: 19 additions & 10 deletions pkg/vm/vm_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (

// VirtualMachine represents the Cairo VM.
// Runs Cairo assembly and produces an execution trace.
// TODO: write proper methods to obtain the fields instead of making them public
type VirtualMachine struct {
runContext RunContext
currentStep uint
segments memory.MemorySegmentManager
RunContext RunContext
CurrentStep uint
Segments memory.MemorySegmentManager
}

type Operands struct {
Expand Down Expand Up @@ -53,6 +54,14 @@ func (deduced *DeducedOperands) set_op1(value uint8) {
// virtual machines funcs
// ------------------------

func VmNew(run_context RunContext, current_step uint, segments_manager memory.MemorySegmentManager) VirtualMachine {
return VirtualMachine{
RunContext: run_context,
CurrentStep: current_step,
Segments: segments_manager,
}
}

func (vm *VirtualMachine) ComputeRes(instruction Instruction, op0 memory.MaybeRelocatable, op1 memory.MaybeRelocatable) (memory.MaybeRelocatable, error) {
switch instruction.ResLogic {
case ResOp1:
Expand Down Expand Up @@ -81,25 +90,25 @@ func (vm *VirtualMachine) ComputeRes(instruction Instruction, op0 memory.MaybeRe
return memory.MaybeRelocatable{}, nil
}

func (vm *VirtualMachine) compute_operands(instruction Instruction) (Operands, OperandsAddresses, DeducedOperands, error) {
func (vm *VirtualMachine) ComputeOperands(instruction Instruction) (Operands, OperandsAddresses, DeducedOperands, error) {

dst_addr, err := vm.runContext.ComputeDstAddr(instruction)
dst_addr, err := vm.RunContext.ComputeDstAddr(instruction)
if err != nil {
return Operands{}, OperandsAddresses{}, DeducedOperands{}, errors.New("FailtToComputeDstAddr")
}
dst_op, _ := vm.segments.Memory.Get(dst_addr)
dst_op, _ := vm.Segments.Memory.Get(dst_addr)

op0_addr, err := vm.runContext.ComputeOp0Addr(instruction)
op0_addr, err := vm.RunContext.ComputeOp0Addr(instruction)
if err != nil {
return Operands{}, OperandsAddresses{}, DeducedOperands{}, errors.New("FailtToComputeOp0Addr")
}
op0_op, _ := vm.segments.Memory.Get(op0_addr)
op0_op, _ := vm.Segments.Memory.Get(op0_addr)

op1_addr, err := vm.runContext.ComputeOp1Addr(instruction, *op0_op)
op1_addr, err := vm.RunContext.ComputeOp1Addr(instruction, *op0_op)
if err != nil {
return Operands{}, OperandsAddresses{}, DeducedOperands{}, errors.New("FailtToComputeOp1Addr")
}
op1_op, _ := vm.segments.Memory.Get(op1_addr)
op1_op, _ := vm.Segments.Memory.Get(op1_addr)

deduced_operands := DeducedOperands{operands: 0}
res, err := vm.ComputeRes(instruction, *op0_op, *op1_op)
Expand Down
61 changes: 61 additions & 0 deletions pkg/vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package vm_test

import (
"testing"

"github.com/lambdaclass/cairo-vm.go/pkg/vm"

"github.com/lambdaclass/cairo-vm.go/pkg/vm/memory"

"github.com/lambdaclass/cairo-vm.go/pkg/lambdaworks"
)

// Things we are skipping for now:
Expand All @@ -18,3 +24,58 @@ func TestFibonacci(t *testing.T) {
// t.Errorf("Program execution failed with error: %s", err)
// }
}

func test_clambdaworksompute_operands_add_ap(t *testing.T) {
instruction := vm.Instruction{
OffDst: 0,
OffOp0: 1,
OffOp1: 2,
DstReg: vm.AP,
Op0Reg: vm.FP,
Op1Addr: vm.Op1SrcAP,achine

Check failure on line 35 in pkg/vm/vm_test.go

View workflow job for this annotation

GitHub Actions / test_and_format

missing ',' before newline in composite literal
ResLogic: vm.ResAdd,
PcUpdate: vm.PcUpdateRegular,
ApUpdate: vm.ApUpdateRegular,
FpUpdate: vm.FpUpdateRegular,
Opcode: achinevm.NOp,
}

memory_manager := memory.NewMemorySegmentManager()
run_context := vm.RunContext{
Ap: memory.NewRelocatable(1, 0),
Fp: memory.NewRelocatable(1, 0),
Pc: memory.NewRelocatable(1, 0),
}
vmachine := vmachine.VmNew(run_context, 0, *memory_manager)

for i := 0; i < 2; i++ {
vm.Segments.AddSegment()
}
vmachine.Segments.Memory = *memory.NewMemory()
dst_addr := memory.NewRelocatable(1, 0)
dst_addr_value := memory.NewMaybeRelocatableInt(lambdaworks.From(5))
op0_addr := memory.NewRelocatable(1, 1)
op0_addr_value := memory.NewMaybeRelocatableInt(lambdaworks.From(2))
op1_addr := memory.NewRelocatable(1, 2)
op1_addr_value := memory.NewMaybeRelocatableInt(lambdaworks.From(3))

vmachine.Segments.Memory.Insert(dst_addr, dst_addr_value)
vmachine.Segments.Memory.Insert(op0_addr, op0_addr_value)
vmachine.Segments.Memory.Insert(op1_addr, op1_addr_value)

expected_operands := vm.Operands{
dst: dst_addr_value,
res: dst_addr_value,
op0: op0_addr_value,
op1: op1_addr_value,
}

expected_addresses := vm.OperandsAddresses {
dst_addr: dst_addr,
op0_addr: op0_addr,
op1_addr: op1_addr,
}

operands, addresses, _, _:= vm.ComputeOperands(instruction)

}

0 comments on commit b6e4d05

Please sign in to comment.