Bit shift operators

This commit is contained in:
Ian Gulliver
2021-11-19 17:04:11 -10:00
parent e2dafcf21a
commit c536961df3
4 changed files with 66 additions and 18 deletions

View File

@@ -24,6 +24,11 @@ var opCodeByName = map[string]vm.OpCodeType{
"or": vm.OpOr, "or": vm.OpOr,
"xor": vm.OpXor, "xor": vm.OpXor,
"shiftright": vm.OpShiftRight,
"shr": vm.OpShR,
"shiftleft": vm.OpShiftLeft,
"shl": vm.OpShL,
"isequal": vm.OpIsEqual, "isequal": vm.OpIsEqual,
"eq": vm.OpEq, "eq": vm.OpEq,
"islessthanunsigned": vm.OpIsLessThanUnsigned, "islessthanunsigned": vm.OpIsLessThanUnsigned,
@@ -89,6 +94,9 @@ var operandsByOpCode = map[vm.OpCodeType][]operandType{
vm.OpOr: []operandType{r, u}, vm.OpOr: []operandType{r, u},
vm.OpXor: []operandType{r, u}, vm.OpXor: []operandType{r, u},
vm.OpShR: []operandType{r, u},
vm.OpShL: []operandType{r, u},
vm.OpEq: []operandType{us, us}, vm.OpEq: []operandType{us, us},
vm.OpLTU: []operandType{u, u}, vm.OpLTU: []operandType{u, u},
vm.OpLTS: []operandType{s, s}, vm.OpLTS: []operandType{s, s},

View File

@@ -135,6 +135,26 @@ functions:
expectGlobalMemory(t, state, 0, 21) expectGlobalMemory(t, state, 0, 21)
} }
func TestShR(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [mov, g0, 53]
- [shr, g0, 2]
`)
expectGlobalMemory(t, state, 0, 13)
}
func TestShL(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [mov, g0, 53]
- [shl, g0, 2]
`)
expectGlobalMemory(t, state, 0, 212)
}
func TestEq(t *testing.T) { func TestEq(t *testing.T) {
state := assembleAndExecute(t, ` state := assembleAndExecute(t, `
functions: functions:

View File

@@ -24,43 +24,48 @@ const (
OpOr = 0x00000302 OpOr = 0x00000302
OpXor = 0x00000303 OpXor = 0x00000303
OpIsEqual = 0x00000400 OpShiftRight = 0x00000400
OpShR = OpShiftRight
OpShiftLeft = 0x00000401
OpShL = OpShiftLeft
OpIsEqual = 0x00000500
OpEq = OpIsEqual OpEq = OpIsEqual
OpIsLessThanUnsigned = 0x00000401 OpIsLessThanUnsigned = 0x00000501
OpLTU = OpIsLessThanUnsigned OpLTU = OpIsLessThanUnsigned
OpIsLessThanSigned = 0x00000402 OpIsLessThanSigned = 0x00000502
OpLTS = OpIsLessThanSigned OpLTS = OpIsLessThanSigned
OpIsGreaterThanUnsigned = 0x00000403 OpIsGreaterThanUnsigned = 0x00000503
OpGTU = OpIsGreaterThanUnsigned OpGTU = OpIsGreaterThanUnsigned
OpIsGreaterThanSigned = 0x00000404 OpIsGreaterThanSigned = 0x00000504
OpGTS = OpIsGreaterThanSigned OpGTS = OpIsGreaterThanSigned
OpIsLessThanOrEqualUnsigned = 0x00000405 OpIsLessThanOrEqualUnsigned = 0x00000505
OpLTEU = OpIsLessThanOrEqualUnsigned OpLTEU = OpIsLessThanOrEqualUnsigned
OpIsLessThanOrEqualSigned = 0x00000406 OpIsLessThanOrEqualSigned = 0x00000506
OpLTES = OpIsLessThanOrEqualSigned OpLTES = OpIsLessThanOrEqualSigned
OpIsGreaterThanOrEqualUnsigned = 0x00000407 OpIsGreaterThanOrEqualUnsigned = 0x00000507
OpGTEU = OpIsGreaterThanOrEqualUnsigned OpGTEU = OpIsGreaterThanOrEqualUnsigned
OpIsGreaterThanOrEqualSigned = 0x00000408 OpIsGreaterThanOrEqualSigned = 0x00000508
OpGTES = OpIsGreaterThanOrEqualSigned OpGTES = OpIsGreaterThanOrEqualSigned
OpJump = 0x00000500 OpJump = 0x00000600
OpJmp = OpJump OpJmp = OpJump
OpJumpIfTrue = 0x00000501 OpJumpIfTrue = 0x00000601
OpJmpT = OpJumpIfTrue OpJmpT = OpJumpIfTrue
OpJumpIfFalse = 0x00000502 OpJumpIfFalse = 0x00000602
OpJmpF = OpJumpIfFalse OpJmpF = OpJumpIfFalse
OpCall = 0x00000600 OpCall = 0x00000700
OpCal = OpCall OpCal = OpCall
OpCallIfTrue = 0x00000601 OpCallIfTrue = 0x00000701
OpCalT = OpCallIfTrue OpCalT = OpCallIfTrue
OpCallIfFalse = 0x00000602 OpCallIfFalse = 0x00000702
OpCalF = OpCallIfFalse OpCalF = OpCallIfFalse
OpReturn = 0x00000700 OpReturn = 0x00000800
OpRet = OpReturn OpRet = OpReturn
OpReturnIfTrue = 0x00000701 OpReturnIfTrue = 0x00000801
OpRetT = OpReturnIfTrue OpRetT = OpReturnIfTrue
OpReturnIfFalse = 0x00000702 OpReturnIfFalse = 0x00000802
OpRetF = OpReturnIfFalse OpRetF = OpReturnIfFalse
) )

View File

@@ -18,6 +18,9 @@ var opHandlers = map[OpCodeType]opHandler{
OpOr: (*State).handleOr, OpOr: (*State).handleOr,
OpXor: (*State).handleXor, OpXor: (*State).handleXor,
OpShiftRight: (*State).handleShiftRight,
OpShiftLeft: (*State).handleShiftLeft,
OpIsEqual: (*State).handleIsEqual, OpIsEqual: (*State).handleIsEqual,
OpIsLessThanUnsigned: (*State).handleIsLessThanUnsigned, OpIsLessThanUnsigned: (*State).handleIsLessThanUnsigned,
OpIsLessThanSigned: (*State).handleIsLessThanSigned, OpIsLessThanSigned: (*State).handleIsLessThanSigned,
@@ -102,6 +105,18 @@ func (state *State) handleXor(instr *Instruction) {
state.writeUnsigned(&instr.Operand1, in1^in2) state.writeUnsigned(&instr.Operand1, in1^in2)
} }
func (state *State) handleShiftRight(instr *Instruction) {
in1 := state.readUnsigned(&instr.Operand1)
in2 := state.readUnsigned(&instr.Operand2)
state.writeUnsigned(&instr.Operand1, in1>>in2)
}
func (state *State) handleShiftLeft(instr *Instruction) {
in1 := state.readUnsigned(&instr.Operand1)
in2 := state.readUnsigned(&instr.Operand2)
state.writeUnsigned(&instr.Operand1, in1<<in2)
}
func (state *State) handleIsEqual(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)