Make private
This commit is contained in:
74
ophandler.go
74
ophandler.go
@@ -34,7 +34,7 @@ func (state *State) NoOp(instr *Instruction) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) Call(instr *Instruction) {
|
func (state *State) Call(instr *Instruction) {
|
||||||
in := state.ReadSigned(&instr.Operand1)
|
in := state.readSigned(&instr.Operand1)
|
||||||
state.call(in)
|
state.call(in)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,96 +43,96 @@ func (state *State) Return(instr *Instruction) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) Move(instr *Instruction) {
|
func (state *State) Move(instr *Instruction) {
|
||||||
in := state.ReadUnsigned(&instr.Operand2)
|
in := state.readUnsigned(&instr.Operand2)
|
||||||
state.WriteUnsigned(&instr.Operand1, in)
|
state.writeUnsigned(&instr.Operand1, in)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) Add(instr *Instruction) {
|
func (state *State) Add(instr *Instruction) {
|
||||||
in1 := state.ReadUnsigned(&instr.Operand1)
|
in1 := state.readUnsigned(&instr.Operand1)
|
||||||
in2 := state.ReadUnsigned(&instr.Operand2)
|
in2 := state.readUnsigned(&instr.Operand2)
|
||||||
state.WriteUnsigned(&instr.Operand1, in1+in2)
|
state.writeUnsigned(&instr.Operand1, in1+in2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) Subtract(instr *Instruction) {
|
func (state *State) Subtract(instr *Instruction) {
|
||||||
in1 := state.ReadUnsigned(&instr.Operand1)
|
in1 := state.readUnsigned(&instr.Operand1)
|
||||||
in2 := state.ReadUnsigned(&instr.Operand2)
|
in2 := state.readUnsigned(&instr.Operand2)
|
||||||
state.WriteUnsigned(&instr.Operand1, in1-in2)
|
state.writeUnsigned(&instr.Operand1, in1-in2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) Multiply(instr *Instruction) {
|
func (state *State) Multiply(instr *Instruction) {
|
||||||
in1 := state.ReadUnsigned(&instr.Operand1)
|
in1 := state.readUnsigned(&instr.Operand1)
|
||||||
in2 := state.ReadUnsigned(&instr.Operand2)
|
in2 := state.readUnsigned(&instr.Operand2)
|
||||||
state.WriteUnsigned(&instr.Operand1, in1*in2)
|
state.writeUnsigned(&instr.Operand1, in1*in2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) DivideUnsigned(instr *Instruction) {
|
func (state *State) DivideUnsigned(instr *Instruction) {
|
||||||
in1 := state.ReadUnsigned(&instr.Operand1)
|
in1 := state.readUnsigned(&instr.Operand1)
|
||||||
in2 := state.ReadUnsigned(&instr.Operand2)
|
in2 := state.readUnsigned(&instr.Operand2)
|
||||||
state.WriteUnsigned(&instr.Operand1, in1/in2)
|
state.writeUnsigned(&instr.Operand1, in1/in2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) DivideSigned(instr *Instruction) {
|
func (state *State) DivideSigned(instr *Instruction) {
|
||||||
in1 := state.ReadSigned(&instr.Operand1)
|
in1 := state.readSigned(&instr.Operand1)
|
||||||
in2 := state.ReadSigned(&instr.Operand2)
|
in2 := state.readSigned(&instr.Operand2)
|
||||||
state.WriteSigned(&instr.Operand1, in1/in2)
|
state.writeSigned(&instr.Operand1, in1/in2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) IsEqual(instr *Instruction) {
|
func (state *State) IsEqual(instr *Instruction) {
|
||||||
in1 := state.ReadUnsigned(&instr.Operand1)
|
in1 := state.readUnsigned(&instr.Operand1)
|
||||||
in2 := state.ReadUnsigned(&instr.Operand2)
|
in2 := state.readUnsigned(&instr.Operand2)
|
||||||
state.comparisonResult = (in1 == in2)
|
state.comparisonResult = (in1 == in2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) IsLessThanUnsigned(instr *Instruction) {
|
func (state *State) IsLessThanUnsigned(instr *Instruction) {
|
||||||
in1 := state.ReadUnsigned(&instr.Operand1)
|
in1 := state.readUnsigned(&instr.Operand1)
|
||||||
in2 := state.ReadUnsigned(&instr.Operand2)
|
in2 := state.readUnsigned(&instr.Operand2)
|
||||||
state.comparisonResult = (in1 < in2)
|
state.comparisonResult = (in1 < in2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) IsLessThanSigned(instr *Instruction) {
|
func (state *State) IsLessThanSigned(instr *Instruction) {
|
||||||
in1 := state.ReadSigned(&instr.Operand1)
|
in1 := state.readSigned(&instr.Operand1)
|
||||||
in2 := state.ReadSigned(&instr.Operand2)
|
in2 := state.readSigned(&instr.Operand2)
|
||||||
state.comparisonResult = (in1 < in2)
|
state.comparisonResult = (in1 < in2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) IsGreaterThanUnsigned(instr *Instruction) {
|
func (state *State) IsGreaterThanUnsigned(instr *Instruction) {
|
||||||
in1 := state.ReadUnsigned(&instr.Operand1)
|
in1 := state.readUnsigned(&instr.Operand1)
|
||||||
in2 := state.ReadUnsigned(&instr.Operand2)
|
in2 := state.readUnsigned(&instr.Operand2)
|
||||||
state.comparisonResult = (in1 > in2)
|
state.comparisonResult = (in1 > in2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) IsGreaterThanSigned(instr *Instruction) {
|
func (state *State) IsGreaterThanSigned(instr *Instruction) {
|
||||||
in1 := state.ReadSigned(&instr.Operand1)
|
in1 := state.readSigned(&instr.Operand1)
|
||||||
in2 := state.ReadSigned(&instr.Operand2)
|
in2 := state.readSigned(&instr.Operand2)
|
||||||
state.comparisonResult = (in1 > in2)
|
state.comparisonResult = (in1 > in2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) IsLessThanOrEqualUnsigned(instr *Instruction) {
|
func (state *State) IsLessThanOrEqualUnsigned(instr *Instruction) {
|
||||||
in1 := state.ReadUnsigned(&instr.Operand1)
|
in1 := state.readUnsigned(&instr.Operand1)
|
||||||
in2 := state.ReadUnsigned(&instr.Operand2)
|
in2 := state.readUnsigned(&instr.Operand2)
|
||||||
state.comparisonResult = (in1 <= in2)
|
state.comparisonResult = (in1 <= in2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) IsLessThanOrEqualSigned(instr *Instruction) {
|
func (state *State) IsLessThanOrEqualSigned(instr *Instruction) {
|
||||||
in1 := state.ReadSigned(&instr.Operand1)
|
in1 := state.readSigned(&instr.Operand1)
|
||||||
in2 := state.ReadSigned(&instr.Operand2)
|
in2 := state.readSigned(&instr.Operand2)
|
||||||
state.comparisonResult = (in1 <= in2)
|
state.comparisonResult = (in1 <= in2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) IsGreaterThanOrEqualUnsigned(instr *Instruction) {
|
func (state *State) IsGreaterThanOrEqualUnsigned(instr *Instruction) {
|
||||||
in1 := state.ReadUnsigned(&instr.Operand1)
|
in1 := state.readUnsigned(&instr.Operand1)
|
||||||
in2 := state.ReadUnsigned(&instr.Operand2)
|
in2 := state.readUnsigned(&instr.Operand2)
|
||||||
state.comparisonResult = (in1 >= in2)
|
state.comparisonResult = (in1 >= in2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) IsGreaterThanOrEqualSigned(instr *Instruction) {
|
func (state *State) IsGreaterThanOrEqualSigned(instr *Instruction) {
|
||||||
in1 := state.ReadSigned(&instr.Operand1)
|
in1 := state.readSigned(&instr.Operand1)
|
||||||
in2 := state.ReadSigned(&instr.Operand2)
|
in2 := state.readSigned(&instr.Operand2)
|
||||||
state.comparisonResult = (in1 >= in2)
|
state.comparisonResult = (in1 >= in2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) Jump(instr *Instruction) {
|
func (state *State) Jump(instr *Instruction) {
|
||||||
in := state.ReadSigned(&instr.Operand1)
|
in := state.readSigned(&instr.Operand1)
|
||||||
state.instructionIndex += in - 1
|
state.instructionIndex += in - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
42
state.go
42
state.go
@@ -44,24 +44,24 @@ func NewState(byteCodes [][]byte) (*State, error) {
|
|||||||
return state, nil
|
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() {
|
func (state *State) Execute() {
|
||||||
state.setHandlers()
|
state.setHandlers()
|
||||||
state.call(0)
|
state.call(0)
|
||||||
state.running = true
|
state.running = true
|
||||||
|
|
||||||
for state.running {
|
for state.running {
|
||||||
state.ProcessInstruction()
|
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) {
|
func (state *State) setError(err error) {
|
||||||
state.err = err
|
state.err = err
|
||||||
state.running = false
|
state.running = false
|
||||||
@@ -81,14 +81,14 @@ func (state *State) setHandlers() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) ProcessInstruction() {
|
func (state *State) processInstruction() {
|
||||||
fnc := state.Function()
|
fnc := state.function()
|
||||||
instr := fnc[state.instructionIndex]
|
instr := fnc[state.instructionIndex]
|
||||||
state.instructionIndex += 1
|
state.instructionIndex += 1
|
||||||
instr.opHandler(state, instr)
|
instr.opHandler(state, instr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) ReadUnsigned(op *Operand) uint64 {
|
func (state *State) readUnsigned(op *Operand) uint64 {
|
||||||
switch op.Type {
|
switch op.Type {
|
||||||
case Literal:
|
case Literal:
|
||||||
return op.Value
|
return op.Value
|
||||||
@@ -98,7 +98,7 @@ func (state *State) ReadUnsigned(op *Operand) uint64 {
|
|||||||
state.setError(fmt.Errorf("Invalid function memory index: %016x", op.Value))
|
state.setError(fmt.Errorf("Invalid function memory index: %016x", op.Value))
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return state.StackFrame().FunctionMemory[op.Value]
|
return state.stackFrame().FunctionMemory[op.Value]
|
||||||
|
|
||||||
case GlobalMemoryIndex:
|
case GlobalMemoryIndex:
|
||||||
if op.Value >= globalMemoryEntries {
|
if op.Value >= globalMemoryEntries {
|
||||||
@@ -113,11 +113,11 @@ func (state *State) ReadUnsigned(op *Operand) uint64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) ReadSigned(op *Operand) int64 {
|
func (state *State) readSigned(op *Operand) int64 {
|
||||||
return int64(state.ReadUnsigned(op))
|
return int64(state.readUnsigned(op))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) WriteUnsigned(op *Operand, value uint64) {
|
func (state *State) writeUnsigned(op *Operand, value uint64) {
|
||||||
switch op.Type {
|
switch op.Type {
|
||||||
case Literal:
|
case Literal:
|
||||||
state.setError(fmt.Errorf("Write to literal operand"))
|
state.setError(fmt.Errorf("Write to literal operand"))
|
||||||
@@ -127,7 +127,7 @@ func (state *State) WriteUnsigned(op *Operand, value uint64) {
|
|||||||
state.setError(fmt.Errorf("Invalid function memory index: %016x", op.Value))
|
state.setError(fmt.Errorf("Invalid function memory index: %016x", op.Value))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
state.StackFrame().FunctionMemory[op.Value] = value
|
state.stackFrame().FunctionMemory[op.Value] = value
|
||||||
|
|
||||||
case GlobalMemoryIndex:
|
case GlobalMemoryIndex:
|
||||||
if op.Value >= globalMemoryEntries {
|
if op.Value >= globalMemoryEntries {
|
||||||
@@ -141,8 +141,8 @@ func (state *State) WriteUnsigned(op *Operand, value uint64) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) WriteSigned(op *Operand, value int64) {
|
func (state *State) writeSigned(op *Operand, value int64) {
|
||||||
state.WriteUnsigned(op, uint64(value))
|
state.writeUnsigned(op, uint64(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) call(functionOffset int64) {
|
func (state *State) call(functionOffset int64) {
|
||||||
@@ -161,8 +161,8 @@ func (state *State) call(functionOffset int64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) ret() {
|
func (state *State) ret() {
|
||||||
state.functionIndex = state.StackFrame().PreviousFunctionIndex
|
state.functionIndex = state.stackFrame().PreviousFunctionIndex
|
||||||
state.instructionIndex = state.StackFrame().PreviousInstructionIndex
|
state.instructionIndex = state.stackFrame().PreviousInstructionIndex
|
||||||
state.stack = state.stack[:len(state.stack)-1]
|
state.stack = state.stack[:len(state.stack)-1]
|
||||||
if len(state.stack) == 0 {
|
if len(state.stack) == 0 {
|
||||||
state.running = false
|
state.running = false
|
||||||
|
|||||||
Reference in New Issue
Block a user