mirror of
https://github.com/irmen/prog8.git
synced 2024-08-11 05:29:18 +00:00
vm: remove BNE opcode -> CMPI + BSTNE
This commit is contained in:
parent
eb64d92333
commit
cdf5a8f20f
@ -565,12 +565,11 @@ internal class ExpressionGen(private val codeGen: IRCodeGen) {
|
|||||||
result += IRCodeChunk(null, null).also {
|
result += IRCodeChunk(null, null).also {
|
||||||
it += IRInstruction(Opcode.LOAD, IRDataType.BYTE, reg1=resultRegister, immediate = 1)
|
it += IRInstruction(Opcode.LOAD, IRDataType.BYTE, reg1=resultRegister, immediate = 1)
|
||||||
it += IRInstruction(Opcode.FCOMP, IRDataType.FLOAT, reg1=valueReg, fpReg1 = leftTr.resultFpReg, fpReg2 = rightTr.resultFpReg)
|
it += IRInstruction(Opcode.FCOMP, IRDataType.FLOAT, reg1=valueReg, fpReg1 = leftTr.resultFpReg, fpReg2 = rightTr.resultFpReg)
|
||||||
if (notEquals) {
|
it += IRInstruction(Opcode.CMPI, IRDataType.BYTE, reg1=valueReg, immediate = 0)
|
||||||
it += IRInstruction(Opcode.BNE, IRDataType.BYTE, reg1=valueReg, immediate = 0, labelSymbol = label)
|
it += if (notEquals)
|
||||||
} else {
|
IRInstruction(Opcode.BSTNE, labelSymbol = label)
|
||||||
it += IRInstruction(Opcode.CMPI, IRDataType.BYTE, reg1=valueReg, immediate = 0)
|
else
|
||||||
it += IRInstruction(Opcode.BSTEQ, labelSymbol = label)
|
IRInstruction(Opcode.BSTEQ, labelSymbol = label)
|
||||||
}
|
|
||||||
it += IRInstruction(Opcode.LOAD, IRDataType.BYTE, reg1=resultRegister, immediate = 0)
|
it += IRInstruction(Opcode.LOAD, IRDataType.BYTE, reg1=resultRegister, immediate = 0)
|
||||||
}
|
}
|
||||||
result += IRCodeChunk(label, null)
|
result += IRCodeChunk(label, null)
|
||||||
|
@ -499,7 +499,8 @@ class IRCodeGen(
|
|||||||
result += translateNode(forLoop.statements)
|
result += translateNode(forLoop.statements)
|
||||||
result += IRCodeChunk(null, null).also {
|
result += IRCodeChunk(null, null).also {
|
||||||
it += IRInstruction(Opcode.INC, IRDataType.BYTE, reg1=indexReg)
|
it += IRInstruction(Opcode.INC, IRDataType.BYTE, reg1=indexReg)
|
||||||
it += IRInstruction(Opcode.BNE, IRDataType.BYTE, reg1=indexReg, immediate = if(iterableLength==256) 0 else iterableLength, labelSymbol = loopLabel)
|
it += IRInstruction(Opcode.CMPI, IRDataType.BYTE, reg1=indexReg, immediate = if(iterableLength==256) 0 else iterableLength)
|
||||||
|
it += IRInstruction(Opcode.BSTNE, labelSymbol = loopLabel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
@ -514,7 +515,10 @@ class IRCodeGen(
|
|||||||
}
|
}
|
||||||
result += translateNode(forLoop.statements)
|
result += translateNode(forLoop.statements)
|
||||||
result += addConstReg(IRDataType.BYTE, indexReg, elementSize)
|
result += addConstReg(IRDataType.BYTE, indexReg, elementSize)
|
||||||
addInstr(result, IRInstruction(Opcode.BNE, IRDataType.BYTE, reg1=indexReg, immediate = if(lengthBytes==256) 0 else lengthBytes, labelSymbol = loopLabel), null)
|
result += IRCodeChunk(null, null).also {
|
||||||
|
it += IRInstruction(Opcode.CMPI, IRDataType.BYTE, reg1=indexReg, immediate = if(lengthBytes==256) 0 else lengthBytes)
|
||||||
|
it += IRInstruction(Opcode.BSTNE, labelSymbol = loopLabel)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -608,7 +612,8 @@ class IRCodeGen(
|
|||||||
result += labelFirstChunk(translateNode(forLoop.statements), loopLabel)
|
result += labelFirstChunk(translateNode(forLoop.statements), loopLabel)
|
||||||
val chunk2 = addConstMem(loopvarDtIr, null, loopvarSymbol, iterable.step)
|
val chunk2 = addConstMem(loopvarDtIr, null, loopvarSymbol, iterable.step)
|
||||||
chunk2 += IRInstruction(Opcode.LOADM, loopvarDtIr, reg1 = indexReg, labelSymbol = loopvarSymbol)
|
chunk2 += IRInstruction(Opcode.LOADM, loopvarDtIr, reg1 = indexReg, labelSymbol = loopvarSymbol)
|
||||||
chunk2 += IRInstruction(Opcode.BNE, loopvarDtIr, reg1 = indexReg, immediate = rangeEndExclusiveWrapped, labelSymbol = loopLabel)
|
chunk2 += IRInstruction(Opcode.CMPI, loopvarDtIr, reg1 = indexReg, immediate = rangeEndExclusiveWrapped)
|
||||||
|
chunk2 += IRInstruction(Opcode.BSTNE, labelSymbol = loopLabel)
|
||||||
result += chunk2
|
result += chunk2
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
@ -973,10 +978,13 @@ class IRCodeGen(
|
|||||||
it += IRInstruction(Opcode.CMPI, IRDataType.BYTE, reg1 = compResultReg, immediate = 0)
|
it += IRInstruction(Opcode.CMPI, IRDataType.BYTE, reg1 = compResultReg, immediate = 0)
|
||||||
it += branchInstr(goto, Opcode.BSTEQ)
|
it += branchInstr(goto, Opcode.BSTEQ)
|
||||||
}
|
}
|
||||||
|
"!=" -> {
|
||||||
|
it += IRInstruction(Opcode.CMPI, IRDataType.BYTE, reg1 = compResultReg, immediate = 0)
|
||||||
|
it += branchInstr(goto, Opcode.BSTNE)
|
||||||
|
}
|
||||||
else -> {
|
else -> {
|
||||||
// TODO: the old list of operators, still to be converted
|
// TODO: the old list of operators, still to be converted
|
||||||
val gotoOpcode = when (condition.operator) {
|
val gotoOpcode = when (condition.operator) {
|
||||||
"!=" -> Opcode.BNE
|
|
||||||
"<" -> Opcode.BLTS
|
"<" -> Opcode.BLTS
|
||||||
">" -> Opcode.BGTS
|
">" -> Opcode.BGTS
|
||||||
"<=" -> Opcode.BLES
|
"<=" -> Opcode.BLES
|
||||||
@ -1029,10 +1037,13 @@ class IRCodeGen(
|
|||||||
addInstr(result, IRInstruction(Opcode.CMPI, irDtLeft, reg1 = leftTr.resultReg, immediate = 0), null)
|
addInstr(result, IRInstruction(Opcode.CMPI, irDtLeft, reg1 = leftTr.resultReg, immediate = 0), null)
|
||||||
addInstr(result, branchInstr(goto, Opcode.BSTEQ), null)
|
addInstr(result, branchInstr(goto, Opcode.BSTEQ), null)
|
||||||
}
|
}
|
||||||
|
"!=" -> {
|
||||||
|
addInstr(result, IRInstruction(Opcode.CMPI, irDtLeft, reg1 = leftTr.resultReg, immediate = 0), null)
|
||||||
|
addInstr(result, branchInstr(goto, Opcode.BSTNE), null)
|
||||||
|
}
|
||||||
else -> {
|
else -> {
|
||||||
// TODO: to-be converted operators
|
// TODO: to-be converted operators
|
||||||
val opcode = when (condition.operator) {
|
val opcode = when (condition.operator) {
|
||||||
"!=" -> Opcode.BNE
|
|
||||||
"<" -> if (signed) Opcode.BLTS else Opcode.BLT
|
"<" -> if (signed) Opcode.BLTS else Opcode.BLT
|
||||||
">" -> if (signed) Opcode.BGTS else Opcode.BGT
|
">" -> if (signed) Opcode.BGTS else Opcode.BGT
|
||||||
"<=" -> if (signed) Opcode.BLES else Opcode.BLE
|
"<=" -> if (signed) Opcode.BLES else Opcode.BLE
|
||||||
@ -1060,12 +1071,10 @@ class IRCodeGen(
|
|||||||
if(condition==null) {
|
if(condition==null) {
|
||||||
val tr = expressionEval.translateExpression(ifElse.condition)
|
val tr = expressionEval.translateExpression(ifElse.condition)
|
||||||
result += tr.chunks
|
result += tr.chunks
|
||||||
if (goto.address != null)
|
result += IRCodeChunk(null, null).also {
|
||||||
addInstr(result, IRInstruction(Opcode.BNE, irDtLeft, reg1 = tr.resultReg, immediate = 0, address = goto.address?.toInt()), null)
|
it += IRInstruction(Opcode.CMPI, irDtLeft, reg1 = tr.resultReg, immediate = 0)
|
||||||
else if (goto.generatedLabel != null)
|
it += branchInstr(goto, Opcode.BSTNE)
|
||||||
addInstr(result, IRInstruction(Opcode.BNE, irDtLeft, reg1 = tr.resultReg, immediate = 0, labelSymbol = goto.generatedLabel), null)
|
}
|
||||||
else
|
|
||||||
addInstr(result, IRInstruction(Opcode.BNE, irDtLeft, reg1 = tr.resultReg, immediate = 0, labelSymbol = goto.identifier!!.name), null)
|
|
||||||
} else {
|
} else {
|
||||||
val leftTr = expressionEval.translateExpression(condition.left)
|
val leftTr = expressionEval.translateExpression(condition.left)
|
||||||
addToResult(result, leftTr, leftTr.resultReg, -1)
|
addToResult(result, leftTr, leftTr.resultReg, -1)
|
||||||
@ -1078,10 +1087,13 @@ class IRCodeGen(
|
|||||||
addInstr(result, IRInstruction(Opcode.CMPI, irDtLeft, reg1 = firstReg, immediate = number), null)
|
addInstr(result, IRInstruction(Opcode.CMPI, irDtLeft, reg1 = firstReg, immediate = number), null)
|
||||||
addInstr(result, branchInstr(goto, Opcode.BSTEQ), null)
|
addInstr(result, branchInstr(goto, Opcode.BSTEQ), null)
|
||||||
}
|
}
|
||||||
|
"!=" -> {
|
||||||
|
addInstr(result, IRInstruction(Opcode.CMPI, irDtLeft, reg1 = firstReg, immediate = number), null)
|
||||||
|
addInstr(result, branchInstr(goto, Opcode.BSTNE), null)
|
||||||
|
}
|
||||||
else -> {
|
else -> {
|
||||||
// TODO: to-be converted operators
|
// TODO: to-be converted operators
|
||||||
val opcode = when (condition.operator) {
|
val opcode = when (condition.operator) {
|
||||||
"!=" -> Opcode.BNE
|
|
||||||
"<" -> if(signed) Opcode.BLTS else Opcode.BLT
|
"<" -> if(signed) Opcode.BLTS else Opcode.BLT
|
||||||
">" -> if(signed) Opcode.BGTS else Opcode.BGT
|
">" -> if(signed) Opcode.BGTS else Opcode.BGT
|
||||||
"<=" -> if(signed) Opcode.BLES else Opcode.BLE
|
"<=" -> if(signed) Opcode.BLES else Opcode.BLE
|
||||||
@ -1165,7 +1177,10 @@ class IRCodeGen(
|
|||||||
it += IRInstruction(Opcode.FCOMP, IRDataType.FLOAT, reg1=compResultReg, fpReg1 = leftTr.resultFpReg, fpReg2 = rightFpReg)
|
it += IRInstruction(Opcode.FCOMP, IRDataType.FLOAT, reg1=compResultReg, fpReg1 = leftTr.resultFpReg, fpReg2 = rightFpReg)
|
||||||
}
|
}
|
||||||
when (condition.operator) {
|
when (condition.operator) {
|
||||||
"==" -> elseBranch = Opcode.BNE
|
"==" -> {
|
||||||
|
elseBranch = Opcode.BSTNE
|
||||||
|
useCmpi = true
|
||||||
|
}
|
||||||
"!=" -> {
|
"!=" -> {
|
||||||
elseBranch = Opcode.BSTEQ
|
elseBranch = Opcode.BSTEQ
|
||||||
useCmpi = true
|
useCmpi = true
|
||||||
@ -1183,7 +1198,10 @@ class IRCodeGen(
|
|||||||
compResultReg = tr.resultReg
|
compResultReg = tr.resultReg
|
||||||
addToResult(result, tr, tr.resultReg, -1)
|
addToResult(result, tr, tr.resultReg, -1)
|
||||||
when (condition.operator) {
|
when (condition.operator) {
|
||||||
"==" -> elseBranch = Opcode.BNE
|
"==" -> {
|
||||||
|
elseBranch = Opcode.BSTNE
|
||||||
|
useCmpi = true
|
||||||
|
}
|
||||||
"!=" -> {
|
"!=" -> {
|
||||||
elseBranch = Opcode.BSTEQ
|
elseBranch = Opcode.BSTEQ
|
||||||
useCmpi = true
|
useCmpi = true
|
||||||
@ -1273,7 +1291,10 @@ class IRCodeGen(
|
|||||||
val elseBranch: Opcode
|
val elseBranch: Opcode
|
||||||
var useCmpi = false // for the branch opcodes that have been converted to CMPI + BSTxx form already
|
var useCmpi = false // for the branch opcodes that have been converted to CMPI + BSTxx form already
|
||||||
when (condition.operator) {
|
when (condition.operator) {
|
||||||
"==" -> elseBranch = Opcode.BNE
|
"==" -> {
|
||||||
|
elseBranch = Opcode.BSTNE
|
||||||
|
useCmpi = true
|
||||||
|
}
|
||||||
"!=" -> {
|
"!=" -> {
|
||||||
elseBranch = Opcode.BSTEQ
|
elseBranch = Opcode.BSTEQ
|
||||||
useCmpi = true
|
useCmpi = true
|
||||||
@ -1322,7 +1343,10 @@ class IRCodeGen(
|
|||||||
val elseBranch: Opcode
|
val elseBranch: Opcode
|
||||||
var useCmpi = false // for the branch opcodes that have been converted to CMPI + BSTxx form already
|
var useCmpi = false // for the branch opcodes that have been converted to CMPI + BSTxx form already
|
||||||
when (condition.operator) {
|
when (condition.operator) {
|
||||||
"==" -> elseBranch = Opcode.BNE
|
"==" -> {
|
||||||
|
elseBranch = Opcode.BSTNE
|
||||||
|
useCmpi = true
|
||||||
|
}
|
||||||
"!=" -> {
|
"!=" -> {
|
||||||
elseBranch = Opcode.BSTEQ
|
elseBranch = Opcode.BSTEQ
|
||||||
useCmpi = true
|
useCmpi = true
|
||||||
@ -1593,8 +1617,8 @@ class IRCodeGen(
|
|||||||
}
|
}
|
||||||
result += labelFirstChunk(translateNode(repeat.statements), repeatLabel)
|
result += labelFirstChunk(translateNode(repeat.statements), repeatLabel)
|
||||||
result += IRCodeChunk(null, null).also {
|
result += IRCodeChunk(null, null).also {
|
||||||
it += IRInstruction(Opcode.DEC, irDt, reg1 = countTr.resultReg)
|
it += IRInstruction(Opcode.DEC, irDt, reg1 = countTr.resultReg) // sets status bits
|
||||||
it += IRInstruction(Opcode.BNE, irDt, reg1 = countTr.resultReg, immediate = 0, labelSymbol = repeatLabel)
|
it += IRInstruction(Opcode.BSTNE, labelSymbol = repeatLabel)
|
||||||
}
|
}
|
||||||
result += IRCodeChunk(skipRepeatLabel, null)
|
result += IRCodeChunk(skipRepeatLabel, null)
|
||||||
return result
|
return result
|
||||||
|
@ -3,10 +3,12 @@
|
|||||||
|
|
||||||
main {
|
main {
|
||||||
sub start() {
|
sub start() {
|
||||||
float xx = 10.1
|
float x=10
|
||||||
ubyte yy= xx==10.1
|
float y=20
|
||||||
txt.print_ub(yy)
|
bool r = x!=y
|
||||||
if xx==10.1
|
txt.print_ub(r)
|
||||||
txt.print("equal")
|
repeat 4 {
|
||||||
|
txt.print(".")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,6 @@ bstvc address - branch to location if Status bit Overf
|
|||||||
bstvs address - branch to location if Status bit Overflow is set
|
bstvs address - branch to location if Status bit Overflow is set
|
||||||
beqr reg1, reg2, address - jump to location in program given by location, if reg1 == reg2
|
beqr reg1, reg2, address - jump to location in program given by location, if reg1 == reg2
|
||||||
bner reg1, reg2, address - jump to location in program given by location, if reg1 != reg2
|
bner reg1, reg2, address - jump to location in program given by location, if reg1 != reg2
|
||||||
bne reg1, value, address - jump to location in program given by location, if reg1 != immediate value
|
|
||||||
bgt reg1, value, address - jump to location in program given by location, if reg1 > immediate value (unsigned)
|
bgt reg1, value, address - jump to location in program given by location, if reg1 > immediate value (unsigned)
|
||||||
bgts reg1, value, address - jump to location in program given by location, if reg1 > immediate value (signed)
|
bgts reg1, value, address - jump to location in program given by location, if reg1 > immediate value (signed)
|
||||||
bgtr reg1, reg2, address - jump to location in program given by location, if reg1 > reg2 (unsigned)
|
bgtr reg1, reg2, address - jump to location in program given by location, if reg1 > reg2 (unsigned)
|
||||||
@ -262,7 +261,6 @@ enum class Opcode {
|
|||||||
BSTVS,
|
BSTVS,
|
||||||
BEQR,
|
BEQR,
|
||||||
BNER,
|
BNER,
|
||||||
BNE,
|
|
||||||
BGTR,
|
BGTR,
|
||||||
BGT,
|
BGT,
|
||||||
BLT,
|
BLT,
|
||||||
@ -407,7 +405,6 @@ val OpcodesThatBranch = setOf(
|
|||||||
Opcode.BSTVS,
|
Opcode.BSTVS,
|
||||||
Opcode.BEQR,
|
Opcode.BEQR,
|
||||||
Opcode.BNER,
|
Opcode.BNER,
|
||||||
Opcode.BNE,
|
|
||||||
Opcode.BGTR,
|
Opcode.BGTR,
|
||||||
Opcode.BGT,
|
Opcode.BGT,
|
||||||
Opcode.BLT,
|
Opcode.BLT,
|
||||||
@ -557,7 +554,6 @@ val instructionFormats = mutableMapOf(
|
|||||||
Opcode.BSTVS to InstructionFormat.from("N,<a"),
|
Opcode.BSTVS to InstructionFormat.from("N,<a"),
|
||||||
Opcode.BEQR to InstructionFormat.from("BW,<r1,<r2,<a"),
|
Opcode.BEQR to InstructionFormat.from("BW,<r1,<r2,<a"),
|
||||||
Opcode.BNER to InstructionFormat.from("BW,<r1,<r2,<a"),
|
Opcode.BNER to InstructionFormat.from("BW,<r1,<r2,<a"),
|
||||||
Opcode.BNE to InstructionFormat.from("BW,<r1,<i,<a"),
|
|
||||||
Opcode.BGTR to InstructionFormat.from("BW,<r1,<r2,<a"),
|
Opcode.BGTR to InstructionFormat.from("BW,<r1,<r2,<a"),
|
||||||
Opcode.BGT to InstructionFormat.from("BW,<r1,<i,<a"),
|
Opcode.BGT to InstructionFormat.from("BW,<r1,<i,<a"),
|
||||||
Opcode.BLT to InstructionFormat.from("BW,<r1,<i,<a"),
|
Opcode.BLT to InstructionFormat.from("BW,<r1,<i,<a"),
|
||||||
|
@ -197,7 +197,6 @@ class VirtualMachine(irProgram: IRProgram) {
|
|||||||
Opcode.BGTSR -> InsBGTSR(ins)
|
Opcode.BGTSR -> InsBGTSR(ins)
|
||||||
Opcode.BGER -> InsBGER(ins)
|
Opcode.BGER -> InsBGER(ins)
|
||||||
Opcode.BGESR -> InsBGESR(ins)
|
Opcode.BGESR -> InsBGESR(ins)
|
||||||
Opcode.BNE -> InsBNE(ins)
|
|
||||||
Opcode.BGT -> InsBGT(ins)
|
Opcode.BGT -> InsBGT(ins)
|
||||||
Opcode.BLT -> InsBLT(ins)
|
Opcode.BLT -> InsBLT(ins)
|
||||||
Opcode.BGTS -> InsBGTS(ins)
|
Opcode.BGTS -> InsBGTS(ins)
|
||||||
@ -686,14 +685,6 @@ class VirtualMachine(irProgram: IRProgram) {
|
|||||||
nextPc()
|
nextPc()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun InsBNE(i: IRInstruction) {
|
|
||||||
val (left: UInt, right: UInt) = getBranchOperandsImmU(i)
|
|
||||||
if(left!=right)
|
|
||||||
branchTo(i)
|
|
||||||
else
|
|
||||||
nextPc()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun InsBGTR(i: IRInstruction) {
|
private fun InsBGTR(i: IRInstruction) {
|
||||||
val (left: UInt, right: UInt) = getBranchOperandsU(i)
|
val (left: UInt, right: UInt) = getBranchOperandsU(i)
|
||||||
if(left>right)
|
if(left>right)
|
||||||
|
Loading…
Reference in New Issue
Block a user