package vm import "fmt" import "github.com/pkg/errors" const globalMemoryEntries = 16 type State struct { running bool err error functions [][]*Instruction functionIndex int64 instructionIndex int64 comparisonResult bool globalMemory [globalMemoryEntries]uint64 stack []*StackFrame } func NewState(byteCodes [][]byte) (*State, error) { state := &State{} for i, byteCode := range byteCodes { instrs := []*Instruction{} for start := 0; start < len(byteCode); start += InstructionBytes { chunk := byteCode[start : start+InstructionBytes] instr, err := NewInstruction(chunk) if err != nil { return nil, errors.Wrapf(err, "At function index %d, byte offset %d", i, start) } instrs = append(instrs, instr) } instrs = append(instrs, &Instruction{ OpCode: OpReturn, }) state.functions = append(state.functions, instrs) } return state, nil } func (state *State) StackFrame() *StackFrame { return state.stack[len(state.stack)-1] } func (state *State) Function() []*Instruction { return state.functions[state.functionIndex] } func (state *State) Execute() { state.setHandlers() state.call(0) state.running = true for state.running { state.ProcessInstruction() } } func (state *State) setError(err error) { state.err = err state.running = false } func (state *State) setHandlers() { for _, fnc := range state.functions { for _, instr := range fnc { handler, found := OpHandlers[instr.OpCode] if !found { state.setError(fmt.Errorf("Invalid OpCode: 0x%08x", instr.OpCode)) return } instr.opHandler = handler } } } func (state *State) ProcessInstruction() { fnc := state.Function() instr := fnc[state.instructionIndex] state.instructionIndex += 1 instr.opHandler(state, instr) } func (state *State) ReadUnsigned(op *Operand) uint64 { switch op.Type { case Literal: return op.Value case FunctionMemoryIndex: if op.Value >= FunctionMemoryEntries { state.setError(fmt.Errorf("Invalid function memory index: %016x", op.Value)) return 0 } return state.StackFrame().FunctionMemory[op.Value] case GlobalMemoryIndex: if op.Value >= globalMemoryEntries { state.setError(fmt.Errorf("Invalid global memory index: %016x", op.Value)) return 0 } return state.globalMemory[op.Value] default: state.setError(fmt.Errorf("Unknown operand type: 0x%02x", op.Type)) return 0 } } func (state *State) ReadSigned(op *Operand) int64 { return int64(state.ReadUnsigned(op)) } func (state *State) WriteUnsigned(op *Operand, value uint64) { switch op.Type { case Literal: state.setError(fmt.Errorf("Write to literal operand")) case FunctionMemoryIndex: if op.Value >= FunctionMemoryEntries { state.setError(fmt.Errorf("Invalid function memory index: %016x", op.Value)) return } state.StackFrame().FunctionMemory[op.Value] = value case GlobalMemoryIndex: if op.Value >= globalMemoryEntries { state.setError(fmt.Errorf("Invalid global memory index: %016x", op.Value)) return } state.globalMemory[op.Value] = value default: state.setError(fmt.Errorf("Unknown operand type: 0x%02x", op.Type)) } } func (state *State) WriteSigned(op *Operand, value int64) { state.WriteUnsigned(op, uint64(value)) } func (state *State) call(functionOffset int64) { if state.functionIndex+functionOffset >= int64(len(state.functions)) { state.setError(fmt.Errorf("Invalid function call index: %d + %d = %d", state.functionIndex, functionOffset, state.functionIndex+functionOffset)) return } stackFrame := &StackFrame{ PreviousFunctionIndex: state.functionIndex, PreviousInstructionIndex: state.instructionIndex, } state.stack = append(state.stack, stackFrame) state.functionIndex += functionOffset state.instructionIndex = 0 } func (state *State) ret() { state.functionIndex = state.StackFrame().PreviousFunctionIndex state.instructionIndex = state.StackFrame().PreviousInstructionIndex state.stack = state.stack[:len(state.stack)-1] if len(state.stack) == 0 { state.running = false } }