Skip to content

Commit

Permalink
Add functionality to add new segments to memory
Browse files Browse the repository at this point in the history
  • Loading branch information
entropidelic committed Jul 11, 2023
1 parent fceb0b0 commit afd4a91
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 20 deletions.
40 changes: 34 additions & 6 deletions pkg/vm/memory/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,27 @@ func TestMemoryInsertWithHoles(t *testing.T) {
}

// Get the value from the address back
res_val, err := mem.Get(key)
resVal, err := mem.Get(key)
if err != nil {
t.Errorf("Get error in test: %s", err)
}

// Check that the original and the retrieved values are the same
if !reflect.DeepEqual(res_val, val) {
if !reflect.DeepEqual(resVal, val) {
t.Errorf("Inserted value and original value are not the same")
}

// Since we inserted in segment 1, offset 2 in an empty memory, now
// the values in segment 1, offset 0 and 1 should be `nil` (memory holes)
hole1_addr := memory.NewRelocatable(1, 0)
hole2_addr := memory.NewRelocatable(1, 1)
hole1Addr := memory.NewRelocatable(1, 0)
hole2Addr := memory.NewRelocatable(1, 1)

hole1, err := mem.Get(hole1_addr)
hole1, err := mem.Get(hole1Addr)
if err != nil {
t.Errorf("Get error in test: %s", err)
}

hole2, err := mem.Get(hole2_addr)
hole2, err := mem.Get(hole2Addr)
if err != nil {
t.Errorf("Get error in test: %s", err)
}
Expand All @@ -83,3 +83,31 @@ func TestMemoryInsertWithHoles(t *testing.T) {
t.Errorf("Expected nil value but got another")
}
}

func TestAddSegment(t *testing.T) {
// Instantiate memory with 3 empty segments
data := make([][]memory.MaybeRelocatable, 3)
mem := memory.NewMemory(data)

// Instantiate MemorySegmentManager
segmentManager := memory.MemorySegmentManager{}
segmentManager.Memory = *mem

// Check that number of segments is 3 before adding another one
if segmentManager.NumSegments() != 3 {
t.Errorf("The number of memory segments should be 3")
}

// Add a new segment to the memory, now we should have 4 segments
newSegmentAddr := segmentManager.Add()

// The address of the base of new segment should be (3, 0)
expectedAddr := memory.NewRelocatable(3, 0)
if !reflect.DeepEqual(newSegmentAddr, expectedAddr) {
t.Errorf("The new segment address is not the expected")
}

if segmentManager.NumSegments() != 4 {
t.Errorf("The number of memory segments should be 4")
}
}
18 changes: 17 additions & 1 deletion pkg/vm/memory/segments.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,21 @@ package memory
// the memory at the end of the VM run.
type MemorySegmentManager struct {
segmentSizes map[uint]uint
memory Memory
Memory Memory
}

// Returns the number of segments present in memory
func (ms *MemorySegmentManager) NumSegments() int {
return len(ms.Memory.data)
}

// Adds a new segment to the memory and returns the base address
// of that segment
func (ms *MemorySegmentManager) Add() *Relocatable {
extendedData := append((*ms).Memory.data, []MaybeRelocatable{})
ms.Memory.data = extendedData

addedSegmentIdx := len(extendedData) - 1

return NewRelocatable(addedSegmentIdx, 0)
}
31 changes: 18 additions & 13 deletions pkg/vm/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
)

type CairoRunner struct {
program Program
program_base memory.Relocatable
execution_base memory.Relocatable
initial_ap uint
initial_fp uint
initial_pc memory.Relocatable
final_pc memory.Relocatable
entrypoint uint
program Program
programBase memory.Relocatable
executionBase memory.Relocatable
initialAp uint
initialFp uint
initialPc memory.Relocatable
finalPc memory.Relocatable
entrypoint uint
}

// Creates a new instance of a CairoRunner.
Expand All @@ -22,11 +22,16 @@ func NewCairoRunner(program Program) *CairoRunner {
return &runner
}

func (cr *CairoRunner) InitializeMainEntrypoint(vm *VirtualMachine) (*memory.Relocatable, error) {
// func (cr *CairoRunner) InitializeFunctionEntrypoint(vm *VirtualMachine, entrypoint uint, stack []memory.MaybeRelocatable, return_fp memory.MaybeRelocatable) (memory.Relocatable, error) {
// finalPc :=

// }

func (cr *CairoRunner) InitializeMainEntrypoint(vm *VirtualMachine) (*memory.Relocatable, error) {
if &cr.entrypoint == nil {
return nil, errors.New("Missing main() entrypoint")
}
// stack := []memory.MaybeRelocatable{}

return nil, nil
}
Expand All @@ -36,12 +41,12 @@ func (cr *CairoRunner) InitializeVM(vm *VirtualMachine) error {
// it like this for now
var relocatableNil memory.Relocatable
var uintNil uint
if cr.initial_pc == relocatableNil || cr.initial_ap == uintNil || cr.initial_fp == uintNil {
if cr.initialPc == relocatableNil || cr.initialAp == uintNil || cr.initialFp == uintNil {
return errors.New("VM initialization error - Could not set initial state")
}
vm.run_context.pc = cr.initial_pc
vm.run_context.ap = cr.initial_ap
vm.run_context.fp = cr.initial_fp
vm.runContext.pc = cr.initialPc
vm.runContext.ap = cr.initialAp
vm.runContext.fp = cr.initialFp

// TODO: Add builtins validation rules and validate memory
// See `initialize_vm` method in cairo-vm.
Expand Down

0 comments on commit afd4a91

Please sign in to comment.