161 lines
3.7 KiB
Go
161 lines
3.7 KiB
Go
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, err := NewInstructionsFromByteCode(byteCode)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "At function index %d", i)
|
|
}
|
|
|
|
state.functions = append(state.functions, instrs)
|
|
}
|
|
|
|
return state, nil
|
|
}
|
|
|
|
func (state *State) Execute() {
|
|
state.setHandlers()
|
|
state.call(0)
|
|
state.running = true
|
|
|
|
for state.running {
|
|
state.processInstruction()
|
|
}
|
|
}
|
|
|
|
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) 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
|
|
}
|
|
}
|