IR/VM: SGN sets status bits

This commit is contained in:
Irmen de Jong committed 2025-11-19 21:37:04 +01:00
1 parent 590feda903
commit 9504711fc7
4 files changed
+30 -10

No files matched your search

+2 -1
View File
@@ -59,7 +59,8 @@ Future Things and Ideas
IR/VM
-----
- make SGN set the N,Z flags and then optimize float<0 float==0 float>0 to use SGN instruction. Check what code is generated for other data types.
- optimize bool b = sgn(value)<0: still does a compare with 0 even though SGN sets all status bits. What is the code when a BIT instruction is used?
- optimize float<0 float==0 float>0 to use SGN instruction? Check what code is generated for other data types.
- getting it in shape for code generation: the IR file should be able to encode every detail about a prog8 program (the VM doesn't have to actually be able to run all of it though!)
- fix call() return value handling (... what's wrong with it again?)
- proper code gen for the CALLI instruction and that it (optionally) returns a word value that needs to be assigned to a reg
+8 -4
View File
@@ -31,10 +31,14 @@ main {
txt.print_bool(b4)
txt.nl()
b1 = sgn(bv)>0
b2 = sgn(wv)>0
b3 = sgn(lv)>0
b4 = sgn(fv)>0
bv = 1
wv = 1
lv = 1
fv = 1.1
b1 = sgn(bv)<0
b2 = sgn(wv)<0
b3 = sgn(lv)<0
b4 = sgn(fv)<0
txt.print_bool(b1)
txt.print_bool(b2)
txt.print_bool(b3)
@@ -498,7 +498,8 @@ val OpcodesThatEndSSAblock = OpcodesThatBranchUnconditionally + arrayOf(
val OpcodesThatSetStatusbitsIncludingCarry = arrayOf(
Opcode.BIT,
Opcode.CMP,
Opcode.CMPI
Opcode.CMPI,
Opcode.SGN
)
val OpcodesThatSetStatusbitsButNotCarry = arrayOf(
Opcode.LOAD,
+18 -4
View File
@@ -1433,12 +1433,26 @@ class VirtualMachine(irProgram: IRProgram) {
}
private fun InsSGN(i: IRInstruction) {
var sign: Int
when(i.type!!) {
IRDataType.BYTE -> registers.setSB(i.reg1!!, registers.getSB(i.reg2!!).toInt().sign.toByte())
IRDataType.WORD -> registers.setSB(i.reg1!!, registers.getSW(i.reg2!!).toInt().sign.toByte())
IRDataType.LONG -> registers.setSB(i.reg1!!, registers.getSL(i.reg2!!).sign.toByte())
IRDataType.FLOAT -> registers.setSB(i.reg1!!, registers.getFloat(i.fpReg1!!).sign.toInt().toByte())
IRDataType.BYTE -> {
sign = registers.getSB(i.reg2!!).toInt().sign
registers.setSB(i.reg1!!, sign.toByte())
}
IRDataType.WORD -> {
sign = registers.getSW(i.reg2!!).toInt().sign
registers.setSB(i.reg1!!, sign.toByte())
}
IRDataType.LONG -> {
sign = registers.getSL(i.reg2!!).sign
registers.setSB(i.reg1!!, sign.toByte())
}
IRDataType.FLOAT -> {
sign = registers.getFloat(i.fpReg1!!).sign.toInt()
registers.setSB(i.reg1!!, sign.toByte())
}
}
statusbitsComparison(sign, i.type!!)
nextPc()
}