Skip to content

Commit

Permalink
Add CairoRunner functionality - WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
entropidelic committed Jul 11, 2023
1 parent afd4a91 commit d306b6a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pkg/vm/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (m *Memory) Insert(addr *Relocatable, val *MaybeRelocatable) error {
// FIXME: There should be a special handling if the key
// segment index is negative. This is an edge
// case, so for now let's raise an error.
if addr.segmentIndex < 0 {
if addr.SegmentIndex < 0 {
return errors.New("Segment index of key is negative - unimplemented")
}

Expand Down Expand Up @@ -61,7 +61,7 @@ func (m *Memory) Get(addr *Relocatable) (*MaybeRelocatable, error) {
// FIXME: There should be a special handling if the key
// segment index is negative. This is an edge
// case, so for now let's raise an error.
if addr.segmentIndex < 0 {
if addr.SegmentIndex < 0 {
return nil, errors.New("Segment index of key is negative - unimplemented")
}

Expand Down
16 changes: 10 additions & 6 deletions pkg/vm/memory/relocatable.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package memory
// these values are replaced by real memory addresses,
// represented by a field element.
type Relocatable struct {
segmentIndex int
offset uint
SegmentIndex int
Offset uint
}

// Creates a new Relocatable struct with the specified segment index
Expand All @@ -18,12 +18,12 @@ func NewRelocatable(segment_idx int, offset uint) *Relocatable {
// Get the the indexes of the Relocatable struct.
// Returns a tuple with both values (segment_index, offset)
func (r *Relocatable) into_indexes() (uint, uint) {
if r.segmentIndex < 0 {
corrected_segment_idx := uint(-(r.segmentIndex + 1))
return corrected_segment_idx, r.offset
if r.SegmentIndex < 0 {
corrected_segment_idx := uint(-(r.SegmentIndex + 1))
return corrected_segment_idx, r.Offset
}

return uint(r.segmentIndex), r.offset
return uint(r.SegmentIndex), r.Offset
}

// Int in the Cairo VM represents a value in memory that
Expand All @@ -47,6 +47,10 @@ func NewMaybeRelocatableInt(felt uint) *MaybeRelocatable {
return &MaybeRelocatable{inner: Int{felt}}
}

func NewMaybeRelocatableAddr(addr Relocatable) *MaybeRelocatable {
return &MaybeRelocatable{inner: addr}
}

// Creates a new MaybeRelocatable with a `nil` inner value
func NewMaybeRelocatableNil() *MaybeRelocatable {
return &MaybeRelocatable{inner: nil}
Expand Down
23 changes: 20 additions & 3 deletions pkg/vm/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,27 @@ func NewCairoRunner(program Program) *CairoRunner {
return &runner
}

// func (cr *CairoRunner) InitializeFunctionEntrypoint(vm *VirtualMachine, entrypoint uint, stack []memory.MaybeRelocatable, return_fp memory.MaybeRelocatable) (memory.Relocatable, error) {
// finalPc :=
func (cr *CairoRunner) InitializeFunctionEntrypoint(vm *VirtualMachine, entrypoint uint, stack []memory.MaybeRelocatable, returnFp memory.MaybeRelocatable) (*memory.Relocatable, error) {
finalPc := *vm.segments.Add()
stackExtension := []memory.MaybeRelocatable{returnFp, *memory.NewMaybeRelocatableAddr(finalPc)}

// }
stack = append(stack, stackExtension...)

var relocatableNil memory.Relocatable
if cr.executionBase == relocatableNil {
return nil, errors.New("No execution base")
}

cr.initialFp = cr.executionBase.Offset + uint(len(stack))
cr.finalPc = finalPc

// err := cr.InitializeState(vm, entrypoint, stack)
// if err != nil {
// return nil, errors.New("Error initializing VM state: %s", err)
// }

return &finalPc, nil
}

func (cr *CairoRunner) InitializeMainEntrypoint(vm *VirtualMachine) (*memory.Relocatable, error) {
if &cr.entrypoint == nil {
Expand Down

0 comments on commit d306b6a

Please sign in to comment.