Move vm into subdir
This commit is contained in:
149
vm/ophandler.go
Normal file
149
vm/ophandler.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package vm
|
||||
|
||||
type opHandler func(*State, *Instruction)
|
||||
|
||||
var opHandlers = map[OpCodeType]opHandler{
|
||||
OpNoOp: (*State).handleNoOp,
|
||||
OpCall: (*State).handleCall,
|
||||
OpReturn: (*State).handleReturn,
|
||||
|
||||
OpMove: (*State).handleMove,
|
||||
|
||||
OpAdd: (*State).handleAdd,
|
||||
OpSubtract: (*State).handleSubtract,
|
||||
OpMultiply: (*State).handleMultiply,
|
||||
OpDivideUnsigned: (*State).handleDivideUnsigned,
|
||||
OpDivideSigned: (*State).handleDivideSigned,
|
||||
|
||||
OpIsEqual: (*State).handleIsEqual,
|
||||
OpIsLessThanUnsigned: (*State).handleIsLessThanUnsigned,
|
||||
OpIsLessThanSigned: (*State).handleIsLessThanSigned,
|
||||
OpIsGreaterThanUnsigned: (*State).handleIsGreaterThanUnsigned,
|
||||
OpIsGreaterThanSigned: (*State).handleIsGreaterThanSigned,
|
||||
OpIsLessThanOrEqualUnsigned: (*State).handleIsLessThanOrEqualUnsigned,
|
||||
OpIsLessThanOrEqualSigned: (*State).handleIsLessThanOrEqualSigned,
|
||||
OpIsGreaterThanOrEqualUnsigned: (*State).handleIsGreaterThanOrEqualUnsigned,
|
||||
OpIsGreaterThanOrEqualSigned: (*State).handleIsGreaterThanOrEqualSigned,
|
||||
|
||||
OpJump: (*State).handleJump,
|
||||
OpJumpIfTrue: (*State).handleJumpIfTrue,
|
||||
OpJumpIfFalse: (*State).handleJumpIfFalse,
|
||||
}
|
||||
|
||||
func (state *State) handleNoOp(instr *Instruction) {
|
||||
}
|
||||
|
||||
func (state *State) handleCall(instr *Instruction) {
|
||||
in := state.readSigned(&instr.Operand1)
|
||||
state.call(in)
|
||||
}
|
||||
|
||||
func (state *State) handleReturn(instr *Instruction) {
|
||||
state.ret()
|
||||
}
|
||||
|
||||
func (state *State) handleMove(instr *Instruction) {
|
||||
in := state.readUnsigned(&instr.Operand2)
|
||||
state.writeUnsigned(&instr.Operand1, in)
|
||||
}
|
||||
|
||||
func (state *State) handleAdd(instr *Instruction) {
|
||||
in1 := state.readUnsigned(&instr.Operand1)
|
||||
in2 := state.readUnsigned(&instr.Operand2)
|
||||
state.writeUnsigned(&instr.Operand1, in1+in2)
|
||||
}
|
||||
|
||||
func (state *State) handleSubtract(instr *Instruction) {
|
||||
in1 := state.readUnsigned(&instr.Operand1)
|
||||
in2 := state.readUnsigned(&instr.Operand2)
|
||||
state.writeUnsigned(&instr.Operand1, in1-in2)
|
||||
}
|
||||
|
||||
func (state *State) handleMultiply(instr *Instruction) {
|
||||
in1 := state.readUnsigned(&instr.Operand1)
|
||||
in2 := state.readUnsigned(&instr.Operand2)
|
||||
state.writeUnsigned(&instr.Operand1, in1*in2)
|
||||
}
|
||||
|
||||
func (state *State) handleDivideUnsigned(instr *Instruction) {
|
||||
in1 := state.readUnsigned(&instr.Operand1)
|
||||
in2 := state.readUnsigned(&instr.Operand2)
|
||||
state.writeUnsigned(&instr.Operand1, in1/in2)
|
||||
}
|
||||
|
||||
func (state *State) handleDivideSigned(instr *Instruction) {
|
||||
in1 := state.readSigned(&instr.Operand1)
|
||||
in2 := state.readSigned(&instr.Operand2)
|
||||
state.writeSigned(&instr.Operand1, in1/in2)
|
||||
}
|
||||
|
||||
func (state *State) handleIsEqual(instr *Instruction) {
|
||||
in1 := state.readUnsigned(&instr.Operand1)
|
||||
in2 := state.readUnsigned(&instr.Operand2)
|
||||
state.comparisonResult = (in1 == in2)
|
||||
}
|
||||
|
||||
func (state *State) handleIsLessThanUnsigned(instr *Instruction) {
|
||||
in1 := state.readUnsigned(&instr.Operand1)
|
||||
in2 := state.readUnsigned(&instr.Operand2)
|
||||
state.comparisonResult = (in1 < in2)
|
||||
}
|
||||
|
||||
func (state *State) handleIsLessThanSigned(instr *Instruction) {
|
||||
in1 := state.readSigned(&instr.Operand1)
|
||||
in2 := state.readSigned(&instr.Operand2)
|
||||
state.comparisonResult = (in1 < in2)
|
||||
}
|
||||
|
||||
func (state *State) handleIsGreaterThanUnsigned(instr *Instruction) {
|
||||
in1 := state.readUnsigned(&instr.Operand1)
|
||||
in2 := state.readUnsigned(&instr.Operand2)
|
||||
state.comparisonResult = (in1 > in2)
|
||||
}
|
||||
|
||||
func (state *State) handleIsGreaterThanSigned(instr *Instruction) {
|
||||
in1 := state.readSigned(&instr.Operand1)
|
||||
in2 := state.readSigned(&instr.Operand2)
|
||||
state.comparisonResult = (in1 > in2)
|
||||
}
|
||||
|
||||
func (state *State) handleIsLessThanOrEqualUnsigned(instr *Instruction) {
|
||||
in1 := state.readUnsigned(&instr.Operand1)
|
||||
in2 := state.readUnsigned(&instr.Operand2)
|
||||
state.comparisonResult = (in1 <= in2)
|
||||
}
|
||||
|
||||
func (state *State) handleIsLessThanOrEqualSigned(instr *Instruction) {
|
||||
in1 := state.readSigned(&instr.Operand1)
|
||||
in2 := state.readSigned(&instr.Operand2)
|
||||
state.comparisonResult = (in1 <= in2)
|
||||
}
|
||||
|
||||
func (state *State) handleIsGreaterThanOrEqualUnsigned(instr *Instruction) {
|
||||
in1 := state.readUnsigned(&instr.Operand1)
|
||||
in2 := state.readUnsigned(&instr.Operand2)
|
||||
state.comparisonResult = (in1 >= in2)
|
||||
}
|
||||
|
||||
func (state *State) handleIsGreaterThanOrEqualSigned(instr *Instruction) {
|
||||
in1 := state.readSigned(&instr.Operand1)
|
||||
in2 := state.readSigned(&instr.Operand2)
|
||||
state.comparisonResult = (in1 >= in2)
|
||||
}
|
||||
|
||||
func (state *State) handleJump(instr *Instruction) {
|
||||
in := state.readSigned(&instr.Operand1)
|
||||
state.jump(in)
|
||||
}
|
||||
|
||||
func (state *State) handleJumpIfTrue(instr *Instruction) {
|
||||
if state.comparisonResult == true {
|
||||
state.handleJump(instr)
|
||||
}
|
||||
}
|
||||
|
||||
func (state *State) handleJumpIfFalse(instr *Instruction) {
|
||||
if state.comparisonResult == false {
|
||||
state.handleJump(instr)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user