More private

This commit is contained in:
Ian Gulliver
2021-11-16 15:33:03 -10:00
parent 7ab0aba088
commit 69e69acc6f
2 changed files with 50 additions and 45 deletions

View File

@@ -3,147 +3,147 @@ package vm
type opHandler func(*State, *Instruction) type opHandler func(*State, *Instruction)
var opHandlers = map[OpCodeType]opHandler{ var opHandlers = map[OpCodeType]opHandler{
OpNoOp: (*State).NoOp, OpNoOp: (*State).handleNoOp,
OpCall: (*State).Call, OpCall: (*State).handleCall,
OpReturn: (*State).Return, OpReturn: (*State).handleReturn,
OpMove: (*State).Move, OpMove: (*State).handleMove,
OpAdd: (*State).Add, OpAdd: (*State).handleAdd,
OpSubtract: (*State).Subtract, OpSubtract: (*State).handleSubtract,
OpMultiply: (*State).Multiply, OpMultiply: (*State).handleMultiply,
OpDivideUnsigned: (*State).DivideUnsigned, OpDivideUnsigned: (*State).handleDivideUnsigned,
OpDivideSigned: (*State).DivideSigned, OpDivideSigned: (*State).handleDivideSigned,
OpIsEqual: (*State).IsEqual, OpIsEqual: (*State).handleIsEqual,
OpIsLessThanUnsigned: (*State).IsLessThanUnsigned, OpIsLessThanUnsigned: (*State).handleIsLessThanUnsigned,
OpIsLessThanSigned: (*State).IsLessThanSigned, OpIsLessThanSigned: (*State).handleIsLessThanSigned,
OpIsGreaterThanUnsigned: (*State).IsGreaterThanUnsigned, OpIsGreaterThanUnsigned: (*State).handleIsGreaterThanUnsigned,
OpIsGreaterThanSigned: (*State).IsGreaterThanSigned, OpIsGreaterThanSigned: (*State).handleIsGreaterThanSigned,
OpIsLessThanOrEqualUnsigned: (*State).IsLessThanOrEqualUnsigned, OpIsLessThanOrEqualUnsigned: (*State).handleIsLessThanOrEqualUnsigned,
OpIsLessThanOrEqualSigned: (*State).IsLessThanOrEqualSigned, OpIsLessThanOrEqualSigned: (*State).handleIsLessThanOrEqualSigned,
OpIsGreaterThanOrEqualUnsigned: (*State).IsGreaterThanOrEqualUnsigned, OpIsGreaterThanOrEqualUnsigned: (*State).handleIsGreaterThanOrEqualUnsigned,
OpIsGreaterThanOrEqualSigned: (*State).IsGreaterThanOrEqualSigned, OpIsGreaterThanOrEqualSigned: (*State).handleIsGreaterThanOrEqualSigned,
OpJump: (*State).Jump, OpJump: (*State).handleJump,
OpJumpIfTrue: (*State).JumpIfTrue, OpJumpIfTrue: (*State).handleJumpIfTrue,
OpJumpIfFalse: (*State).JumpIfFalse, OpJumpIfFalse: (*State).handleJumpIfFalse,
} }
func (state *State) NoOp(instr *Instruction) { func (state *State) handleNoOp(instr *Instruction) {
} }
func (state *State) Call(instr *Instruction) { func (state *State) handleCall(instr *Instruction) {
in := state.readSigned(&instr.Operand1) in := state.readSigned(&instr.Operand1)
state.call(in) state.call(in)
} }
func (state *State) Return(instr *Instruction) { func (state *State) handleReturn(instr *Instruction) {
state.ret() state.ret()
} }
func (state *State) Move(instr *Instruction) { func (state *State) handleMove(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) handleAdd(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) handleSubtract(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) handleMultiply(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) handleDivideUnsigned(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) handleDivideSigned(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) handleIsEqual(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) handleIsLessThanUnsigned(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) handleIsLessThanSigned(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) handleIsGreaterThanUnsigned(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) handleIsGreaterThanSigned(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) handleIsLessThanOrEqualUnsigned(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) handleIsLessThanOrEqualSigned(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) handleIsGreaterThanOrEqualUnsigned(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) handleIsGreaterThanOrEqualSigned(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) handleJump(instr *Instruction) {
in := state.readSigned(&instr.Operand1) in := state.readSigned(&instr.Operand1)
state.instructionIndex += in - 1 state.jump(in)
} }
func (state *State) JumpIfTrue(instr *Instruction) { func (state *State) handleJumpIfTrue(instr *Instruction) {
if state.comparisonResult == true { if state.comparisonResult == true {
state.Jump(instr) state.handleJump(instr)
} }
} }
func (state *State) JumpIfFalse(instr *Instruction) { func (state *State) handleJumpIfFalse(instr *Instruction) {
if state.comparisonResult == false { if state.comparisonResult == false {
state.Jump(instr) state.handleJump(instr)
} }
} }

View File

@@ -158,3 +158,8 @@ func (state *State) ret() {
state.running = false state.running = false
} }
} }
func (state *State) jump(instructionOffset int64) {
// -1 accounts for the +1 processInstruction()
state.instructionIndex += instructionOffset - 1
}