Use memory type for function memory

This commit is contained in:
Ian Gulliver
2021-11-16 16:21:32 -10:00
parent 012d5faecf
commit cf33a52392
2 changed files with 18 additions and 18 deletions

View File

@@ -1,9 +1,15 @@
package vm package vm
const functionMemoryEntries = 16
type stackFrame struct { type stackFrame struct {
previousFunctionIndex int64 previousFunctionIndex int64
previousInstructionIndex int64 previousInstructionIndex int64
functionMemory [functionMemoryEntries]uint64 functionMemory *memory
}
func newStackFrame(state *State) *stackFrame {
return &stackFrame{
previousFunctionIndex: state.functionIndex,
previousInstructionIndex: state.instructionIndex,
functionMemory: newMemory(16),
}
} }

View File

@@ -4,8 +4,6 @@ import "fmt"
import "github.com/pkg/errors" import "github.com/pkg/errors"
const globalMemoryEntries = 16
type State struct { type State struct {
running bool running bool
err error err error
@@ -22,7 +20,7 @@ type State struct {
func NewState(functions [][]*Instruction) (*State, error) { func NewState(functions [][]*Instruction) (*State, error) {
return &State{ return &State{
functions: functions, functions: functions,
globalMemory: newMemory(globalMemoryEntries), globalMemory: newMemory(16),
}, nil }, nil
} }
@@ -91,11 +89,11 @@ func (state *State) readUnsigned(op *Operand) uint64 {
return op.Value return op.Value
case FunctionMemoryIndex: case FunctionMemoryIndex:
if op.Value >= functionMemoryEntries { value, err := state.stackFrame().functionMemory.readUnsigned(op.Value)
state.setError(fmt.Errorf("Invalid function memory index: %016x", op.Value)) if err != nil {
return 0 state.setError(err)
} }
return state.stackFrame().functionMemory[op.Value] return value
case GlobalMemoryIndex: case GlobalMemoryIndex:
value, err := state.globalMemory.readUnsigned(op.Value) value, err := state.globalMemory.readUnsigned(op.Value)
@@ -120,11 +118,10 @@ func (state *State) writeUnsigned(op *Operand, value uint64) {
state.setError(fmt.Errorf("Write to literal operand")) state.setError(fmt.Errorf("Write to literal operand"))
case FunctionMemoryIndex: case FunctionMemoryIndex:
if op.Value >= functionMemoryEntries { err := state.stackFrame().functionMemory.writeUnsigned(op.Value, value)
state.setError(fmt.Errorf("Invalid function memory index: %016x", op.Value)) if err != nil {
return state.setError(err)
} }
state.stackFrame().functionMemory[op.Value] = value
case GlobalMemoryIndex: case GlobalMemoryIndex:
err := state.globalMemory.writeUnsigned(op.Value, value) err := state.globalMemory.writeUnsigned(op.Value, value)
@@ -147,10 +144,7 @@ func (state *State) call(functionOffset int64) {
return return
} }
stackFrame := &stackFrame{ stackFrame := newStackFrame(state)
previousFunctionIndex: state.functionIndex,
previousInstructionIndex: state.instructionIndex,
}
state.stack = append(state.stack, stackFrame) state.stack = append(state.stack, stackFrame)
state.functionIndex += functionOffset state.functionIndex += functionOffset
state.instructionIndex = 0 state.instructionIndex = 0