Files
subcoding/state.go

161 lines
3.7 KiB
Go
Raw Normal View History

2021-11-15 20:36:02 -10:00
package vm
2021-11-14 16:27:03 -08:00
import "fmt"
import "github.com/pkg/errors"
2021-11-15 20:49:54 -10:00
const globalMemoryEntries = 16
2021-11-14 16:27:03 -08:00
type State struct {
2021-11-15 20:49:54 -10:00
running bool
err error
2021-11-14 16:27:03 -08:00
2021-11-15 20:49:54 -10:00
functions [][]*Instruction
functionIndex int64
instructionIndex int64
2021-11-14 16:27:03 -08:00
2021-11-15 20:49:54 -10:00
comparisonResult bool
globalMemory [globalMemoryEntries]uint64
stack []*StackFrame
2021-11-14 16:27:03 -08:00
}
2021-11-15 16:39:30 -10:00
func NewState(byteCodes [][]byte) (*State, error) {
state := &State{}
for i, byteCode := range byteCodes {
2021-11-16 15:27:30 -10:00
instrs, err := NewInstructionsFromByteCode(byteCode)
if err != nil {
return nil, errors.Wrapf(err, "At function index %d", i)
2021-11-15 16:39:30 -10:00
}
2021-11-15 20:49:54 -10:00
state.functions = append(state.functions, instrs)
2021-11-15 16:39:30 -10:00
}
return state, nil
2021-11-14 16:27:03 -08:00
}
func (state *State) Execute() {
2021-11-15 20:24:20 -10:00
state.setHandlers()
2021-11-14 20:37:33 -10:00
state.call(0)
2021-11-15 20:49:54 -10:00
state.running = true
2021-11-14 20:37:33 -10:00
2021-11-15 20:49:54 -10:00
for state.running {
2021-11-16 15:13:30 -10:00
state.processInstruction()
2021-11-15 16:46:37 -10:00
}
}
2021-11-16 15:13:30 -10:00
func (state *State) stackFrame() *StackFrame {
return state.stack[len(state.stack)-1]
}
func (state *State) function() []*Instruction {
return state.functions[state.functionIndex]
}
2021-11-15 20:24:20 -10:00
func (state *State) setError(err error) {
2021-11-15 20:49:54 -10:00
state.err = err
state.running = false
2021-11-15 20:24:20 -10:00
}
func (state *State) setHandlers() {
2021-11-15 20:49:54 -10:00
for _, fnc := range state.functions {
2021-11-15 16:46:37 -10:00
for _, instr := range fnc {
2021-11-16 15:27:30 -10:00
handler, found := opHandlers[instr.OpCode]
2021-11-15 16:46:37 -10:00
if !found {
2021-11-15 20:24:20 -10:00
state.setError(fmt.Errorf("Invalid OpCode: 0x%08x", instr.OpCode))
2021-11-15 16:46:37 -10:00
return
}
2021-11-14 16:27:03 -08:00
2021-11-15 16:46:37 -10:00
instr.opHandler = handler
2021-11-15 16:39:30 -10:00
}
2021-11-14 16:27:03 -08:00
}
}
2021-11-16 15:13:30 -10:00
func (state *State) processInstruction() {
fnc := state.function()
2021-11-15 20:49:54 -10:00
instr := fnc[state.instructionIndex]
state.instructionIndex += 1
2021-11-15 16:46:37 -10:00
instr.opHandler(state, instr)
2021-11-14 16:27:03 -08:00
}
2021-11-16 15:13:30 -10:00
func (state *State) readUnsigned(op *Operand) uint64 {
2021-11-14 16:27:03 -08:00
switch op.Type {
case Literal:
return op.Value
2021-11-14 16:27:03 -08:00
case FunctionMemoryIndex:
if op.Value >= FunctionMemoryEntries {
2021-11-15 20:24:20 -10:00
state.setError(fmt.Errorf("Invalid function memory index: %016x", op.Value))
return 0
}
2021-11-16 15:13:30 -10:00
return state.stackFrame().FunctionMemory[op.Value]
2021-11-14 16:27:03 -08:00
case GlobalMemoryIndex:
2021-11-15 20:49:54 -10:00
if op.Value >= globalMemoryEntries {
2021-11-15 20:24:20 -10:00
state.setError(fmt.Errorf("Invalid global memory index: %016x", op.Value))
return 0
}
2021-11-15 20:49:54 -10:00
return state.globalMemory[op.Value]
2021-11-14 16:27:03 -08:00
default:
2021-11-15 20:24:20 -10:00
state.setError(fmt.Errorf("Unknown operand type: 0x%02x", op.Type))
2021-11-14 16:27:03 -08:00
return 0
}
}
2021-11-16 15:13:30 -10:00
func (state *State) readSigned(op *Operand) int64 {
return int64(state.readUnsigned(op))
2021-11-14 16:27:03 -08:00
}
2021-11-16 15:13:30 -10:00
func (state *State) writeUnsigned(op *Operand, value uint64) {
2021-11-14 16:27:03 -08:00
switch op.Type {
case Literal:
2021-11-15 20:24:20 -10:00
state.setError(fmt.Errorf("Write to literal operand"))
2021-11-14 16:27:03 -08:00
case FunctionMemoryIndex:
if op.Value >= FunctionMemoryEntries {
2021-11-15 20:24:20 -10:00
state.setError(fmt.Errorf("Invalid function memory index: %016x", op.Value))
return
}
2021-11-16 15:13:30 -10:00
state.stackFrame().FunctionMemory[op.Value] = value
2021-11-14 16:27:03 -08:00
case GlobalMemoryIndex:
2021-11-15 20:49:54 -10:00
if op.Value >= globalMemoryEntries {
2021-11-15 20:24:20 -10:00
state.setError(fmt.Errorf("Invalid global memory index: %016x", op.Value))
return
}
2021-11-15 20:49:54 -10:00
state.globalMemory[op.Value] = value
2021-11-14 16:27:03 -08:00
default:
2021-11-15 20:24:20 -10:00
state.setError(fmt.Errorf("Unknown operand type: 0x%02x", op.Type))
2021-11-14 16:27:03 -08:00
}
}
2021-11-16 15:13:30 -10:00
func (state *State) writeSigned(op *Operand, value int64) {
state.writeUnsigned(op, uint64(value))
2021-11-14 16:27:03 -08:00
}
2021-11-14 20:37:33 -10:00
func (state *State) call(functionOffset int64) {
2021-11-15 20:49:54 -10:00
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
}
2021-11-14 16:27:03 -08:00
stackFrame := &StackFrame{
2021-11-15 20:49:54 -10:00
PreviousFunctionIndex: state.functionIndex,
PreviousInstructionIndex: state.instructionIndex,
2021-11-14 16:27:03 -08:00
}
2021-11-15 20:49:54 -10:00
state.stack = append(state.stack, stackFrame)
state.functionIndex += functionOffset
state.instructionIndex = 0
2021-11-14 16:27:03 -08:00
}
func (state *State) ret() {
2021-11-16 15:13:30 -10:00
state.functionIndex = state.stackFrame().PreviousFunctionIndex
state.instructionIndex = state.stackFrame().PreviousInstructionIndex
2021-11-15 20:49:54 -10:00
state.stack = state.stack[:len(state.stack)-1]
if len(state.stack) == 0 {
state.running = false
2021-11-15 20:24:20 -10:00
}
2021-11-14 16:27:03 -08:00
}