404 lines
9.9 KiB
Go
404 lines
9.9 KiB
Go
package main
|
|
|
|
import "bytes"
|
|
import "encoding/hex"
|
|
import "fmt"
|
|
import "strings"
|
|
|
|
import "github.com/lunixbochs/struc"
|
|
import "github.com/pkg/errors"
|
|
|
|
type OperandType uint8
|
|
|
|
const (
|
|
Literal OperandType = 0
|
|
FunctionMemoryIndex = 1
|
|
GlobalMemoryIndex = 2
|
|
)
|
|
|
|
const InstructionBytes = 32
|
|
const GlobalMemoryEntries = 16
|
|
const FunctionMemoryEntries = 16
|
|
|
|
type Operand struct {
|
|
Type OperandType
|
|
Reserved [3]byte
|
|
Value uint64
|
|
}
|
|
|
|
type Instruction struct {
|
|
OpCode uint32
|
|
Reserved [4]byte
|
|
Operand1 Operand
|
|
Operand2 Operand
|
|
}
|
|
|
|
type State struct {
|
|
Error error
|
|
|
|
Functions [][]*Instruction
|
|
FunctionIndex int64
|
|
InstructionIndex int64
|
|
|
|
ComparisonResult bool
|
|
GlobalMemory [GlobalMemoryEntries]uint64
|
|
Stack []*StackFrame
|
|
}
|
|
|
|
type StackFrame struct {
|
|
PreviousFunctionIndex int64
|
|
PreviousInstructionIndex int64
|
|
FunctionMemory [FunctionMemoryEntries]uint64
|
|
}
|
|
|
|
type OpHandler func(*State, *Instruction)
|
|
|
|
var OpHandlers = map[uint32]OpHandler{
|
|
0x00000000: (*State).NoOp,
|
|
0x00000001: (*State).Call,
|
|
0x00000002: (*State).Return,
|
|
|
|
0x00000100: (*State).Move,
|
|
|
|
0x00000200: (*State).Add,
|
|
0x00000201: (*State).Subtract,
|
|
0x00000202: (*State).Multiply,
|
|
0x00000203: (*State).DivideUnsigned,
|
|
0x00000204: (*State).DivideSigned,
|
|
|
|
0x00000300: (*State).IsEqual,
|
|
0x00000301: (*State).IsLessThanUnsigned,
|
|
0x00000302: (*State).IsLessThanSigned,
|
|
0x00000303: (*State).IsGreaterThanUnsigned,
|
|
0x00000304: (*State).IsGreaterThanSigned,
|
|
0x00000305: (*State).IsLessThanOrEqualUnsigned,
|
|
0x00000306: (*State).IsLessThanOrEqualSigned,
|
|
0x00000307: (*State).IsGreaterThanOrEqualUnsigned,
|
|
0x00000308: (*State).IsGreaterThanOrEqualSigned,
|
|
|
|
0x00000400: (*State).Jump,
|
|
0x00000401: (*State).JumpIfTrue,
|
|
0x00000402: (*State).JumpIfFalse,
|
|
}
|
|
|
|
func NewInstruction(byteCode []byte) (*Instruction, error) {
|
|
instr := &Instruction{}
|
|
|
|
reader := bytes.NewReader(byteCode)
|
|
|
|
err := struc.Unpack(reader, instr)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "Error decoding instruction")
|
|
}
|
|
|
|
return instr, nil
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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.call(0)
|
|
|
|
for len(state.Stack) > 0 {
|
|
state.ProcessInstruction()
|
|
|
|
if state.Error != nil {
|
|
break
|
|
}
|
|
|
|
if state.InstructionIndex >= int64(len(state.Function())) {
|
|
state.ret()
|
|
}
|
|
}
|
|
}
|
|
|
|
func (state *State) ProcessInstruction() {
|
|
fnc := state.Function()
|
|
|
|
if state.InstructionIndex >= int64(len(fnc)) {
|
|
state.ret()
|
|
return
|
|
}
|
|
|
|
instr := fnc[state.InstructionIndex]
|
|
|
|
handler, found := OpHandlers[instr.OpCode]
|
|
if !found {
|
|
state.Error = fmt.Errorf("Invalid OpCode: 0x%08x", instr.OpCode)
|
|
return
|
|
}
|
|
|
|
state.InstructionIndex += 1
|
|
|
|
handler(state, instr)
|
|
}
|
|
|
|
func (state *State) ReadUnsigned(op *Operand) uint64 {
|
|
switch op.Type {
|
|
case Literal:
|
|
return op.Value
|
|
|
|
case FunctionMemoryIndex:
|
|
if op.Value >= FunctionMemoryEntries {
|
|
state.Error = fmt.Errorf("Invalid function memory index: %016x", op.Value)
|
|
return 0
|
|
}
|
|
return state.StackFrame().FunctionMemory[op.Value]
|
|
|
|
case GlobalMemoryIndex:
|
|
if op.Value >= GlobalMemoryEntries {
|
|
state.Error = fmt.Errorf("Invalid global memory index: %016x", op.Value)
|
|
return 0
|
|
}
|
|
return state.GlobalMemory[op.Value]
|
|
|
|
default:
|
|
state.Error = 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.Error = fmt.Errorf("Write to literal operand")
|
|
|
|
case FunctionMemoryIndex:
|
|
if op.Value >= FunctionMemoryEntries {
|
|
state.Error = fmt.Errorf("Invalid function memory index: %016x", op.Value)
|
|
return
|
|
}
|
|
state.StackFrame().FunctionMemory[op.Value] = value
|
|
|
|
case GlobalMemoryIndex:
|
|
if op.Value >= GlobalMemoryEntries {
|
|
state.Error = fmt.Errorf("Invalid global memory index: %016x", op.Value)
|
|
return
|
|
}
|
|
state.GlobalMemory[op.Value] = value
|
|
|
|
default:
|
|
state.Error = 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) NoOp(instr *Instruction) {
|
|
}
|
|
|
|
func (state *State) Call(instr *Instruction) {
|
|
in := state.ReadSigned(&instr.Operand1)
|
|
state.call(in)
|
|
}
|
|
|
|
func (state *State) call(functionOffset int64) {
|
|
if state.FunctionIndex+functionOffset >= int64(len(state.Functions)) {
|
|
state.Error = 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) Return(instr *Instruction) {
|
|
state.ret()
|
|
}
|
|
|
|
func (state *State) ret() {
|
|
state.FunctionIndex = state.StackFrame().PreviousFunctionIndex
|
|
state.InstructionIndex = state.StackFrame().PreviousInstructionIndex
|
|
state.Stack = state.Stack[:len(state.Stack)-1]
|
|
}
|
|
|
|
func (state *State) Move(instr *Instruction) {
|
|
in := state.ReadUnsigned(&instr.Operand2)
|
|
state.WriteUnsigned(&instr.Operand1, in)
|
|
}
|
|
|
|
func (state *State) Add(instr *Instruction) {
|
|
in1 := state.ReadUnsigned(&instr.Operand1)
|
|
in2 := state.ReadUnsigned(&instr.Operand2)
|
|
state.WriteUnsigned(&instr.Operand1, in1+in2)
|
|
}
|
|
|
|
func (state *State) Subtract(instr *Instruction) {
|
|
in1 := state.ReadUnsigned(&instr.Operand1)
|
|
in2 := state.ReadUnsigned(&instr.Operand2)
|
|
state.WriteUnsigned(&instr.Operand1, in1-in2)
|
|
}
|
|
|
|
func (state *State) Multiply(instr *Instruction) {
|
|
in1 := state.ReadUnsigned(&instr.Operand1)
|
|
in2 := state.ReadUnsigned(&instr.Operand2)
|
|
state.WriteUnsigned(&instr.Operand1, in1*in2)
|
|
}
|
|
|
|
func (state *State) DivideUnsigned(instr *Instruction) {
|
|
in1 := state.ReadUnsigned(&instr.Operand1)
|
|
in2 := state.ReadUnsigned(&instr.Operand2)
|
|
state.WriteUnsigned(&instr.Operand1, in1/in2)
|
|
}
|
|
|
|
func (state *State) DivideSigned(instr *Instruction) {
|
|
in1 := state.ReadSigned(&instr.Operand1)
|
|
in2 := state.ReadSigned(&instr.Operand2)
|
|
state.WriteSigned(&instr.Operand1, in1/in2)
|
|
}
|
|
|
|
func (state *State) IsEqual(instr *Instruction) {
|
|
in1 := state.ReadUnsigned(&instr.Operand1)
|
|
in2 := state.ReadUnsigned(&instr.Operand2)
|
|
state.ComparisonResult = (in1 == in2)
|
|
}
|
|
|
|
func (state *State) IsLessThanUnsigned(instr *Instruction) {
|
|
in1 := state.ReadUnsigned(&instr.Operand1)
|
|
in2 := state.ReadUnsigned(&instr.Operand2)
|
|
state.ComparisonResult = (in1 < in2)
|
|
}
|
|
|
|
func (state *State) IsLessThanSigned(instr *Instruction) {
|
|
in1 := state.ReadSigned(&instr.Operand1)
|
|
in2 := state.ReadSigned(&instr.Operand2)
|
|
state.ComparisonResult = (in1 < in2)
|
|
}
|
|
|
|
func (state *State) IsGreaterThanUnsigned(instr *Instruction) {
|
|
in1 := state.ReadUnsigned(&instr.Operand1)
|
|
in2 := state.ReadUnsigned(&instr.Operand2)
|
|
state.ComparisonResult = (in1 > in2)
|
|
}
|
|
|
|
func (state *State) IsGreaterThanSigned(instr *Instruction) {
|
|
in1 := state.ReadSigned(&instr.Operand1)
|
|
in2 := state.ReadSigned(&instr.Operand2)
|
|
state.ComparisonResult = (in1 > in2)
|
|
}
|
|
|
|
func (state *State) IsLessThanOrEqualUnsigned(instr *Instruction) {
|
|
in1 := state.ReadUnsigned(&instr.Operand1)
|
|
in2 := state.ReadUnsigned(&instr.Operand2)
|
|
state.ComparisonResult = (in1 <= in2)
|
|
}
|
|
|
|
func (state *State) IsLessThanOrEqualSigned(instr *Instruction) {
|
|
in1 := state.ReadSigned(&instr.Operand1)
|
|
in2 := state.ReadSigned(&instr.Operand2)
|
|
state.ComparisonResult = (in1 <= in2)
|
|
}
|
|
|
|
func (state *State) IsGreaterThanOrEqualUnsigned(instr *Instruction) {
|
|
in1 := state.ReadUnsigned(&instr.Operand1)
|
|
in2 := state.ReadUnsigned(&instr.Operand2)
|
|
state.ComparisonResult = (in1 >= in2)
|
|
}
|
|
|
|
func (state *State) IsGreaterThanOrEqualSigned(instr *Instruction) {
|
|
in1 := state.ReadSigned(&instr.Operand1)
|
|
in2 := state.ReadSigned(&instr.Operand2)
|
|
state.ComparisonResult = (in1 >= in2)
|
|
}
|
|
|
|
func (state *State) Jump(instr *Instruction) {
|
|
in := state.ReadSigned(&instr.Operand1)
|
|
state.InstructionIndex += in - 1
|
|
}
|
|
|
|
func (state *State) JumpIfTrue(instr *Instruction) {
|
|
if state.ComparisonResult == true {
|
|
state.Jump(instr)
|
|
}
|
|
}
|
|
|
|
func (state *State) JumpIfFalse(instr *Instruction) {
|
|
if state.ComparisonResult == false {
|
|
state.Jump(instr)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
asm := [][]string{
|
|
[]string{
|
|
"0000020000000000010000000000000000000000000000000000000000000001",
|
|
"0000000100000000000000000000000000000001000000000000000000000000",
|
|
"0000030100000000010000000000000000000000000000000000000000000003",
|
|
"000004010000000000000000fffffffffffffffd000000000000000000000000",
|
|
},
|
|
[]string{
|
|
"0000020000000000020000000000000000000000000000000000000000000001",
|
|
},
|
|
}
|
|
|
|
functionByteCode := [][]byte{}
|
|
|
|
for _, fnc := range asm {
|
|
fncString := strings.Join(fnc, "")
|
|
|
|
byteCode, err := hex.DecodeString(fncString)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
functionByteCode = append(functionByteCode, byteCode)
|
|
}
|
|
|
|
state, err := NewState(functionByteCode)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
state.Execute()
|
|
|
|
if state.Error != nil {
|
|
fmt.Printf("ERROR: %s\n", state.Error)
|
|
fmt.Printf("\tNext function index: 0x%016x\n", state.FunctionIndex)
|
|
fmt.Printf("\tNext instruction index: 0x%016x\n", state.InstructionIndex)
|
|
fmt.Printf("\n")
|
|
}
|
|
|
|
fmt.Printf("Global memory:\n")
|
|
for i, v := range state.GlobalMemory {
|
|
fmt.Printf("\t0x%08x: 0x%016x %d %d\n", i, v, v, int64(v))
|
|
}
|
|
}
|