don't optimize empty where choice away! It would call the else clause incorrectly.

This commit is contained in:
Irmen de Jong 2023-07-29 18:25:52 +02:00
parent c112b327ab
commit b89ad4b328
4 changed files with 21 additions and 33 deletions

View File

@ -413,10 +413,17 @@ class IRCodeGen(
result += translateNode(choice.statements)
addInstr(result, IRInstruction(Opcode.JUMP, labelSymbol = endLabel), null)
} else {
val choiceLabel = createLabelName()
choices.add(choiceLabel to choice)
choice.values.children.map { it as PtNumber }.sortedBy { it.number }.forEach { value ->
addInstr(result, IRInstruction(Opcode.BEQ, valueDt, reg1=valueTr.resultReg, immediate = value.number.toInt(), labelSymbol = choiceLabel), null)
if(choice.statements.children.isEmpty()) {
// no statements for this choice value, jump to the end immediately
choice.values.children.map { it as PtNumber }.sortedBy { it.number }.forEach { value ->
addInstr(result, IRInstruction(Opcode.BEQ, valueDt, reg1=valueTr.resultReg, immediate = value.number.toInt(), labelSymbol = endLabel), null)
}
} else {
val choiceLabel = createLabelName()
choices.add(choiceLabel to choice)
choice.values.children.map { it as PtNumber }.sortedBy { it.number }.forEach { value ->
addInstr(result, IRInstruction(Opcode.BEQ, valueDt, reg1=valueTr.resultReg, immediate = value.number.toInt(), labelSymbol = choiceLabel), null)
}
}
}
}

View File

@ -262,15 +262,5 @@ internal class VariousCleanups(val program: Program, val errors: IErrorReporter,
}
return noModifications
}
override fun after(whenStmt: When, parent: Node): Iterable<IAstModification> {
val removals = mutableListOf<Int>()
whenStmt.choices.withIndex().forEach { (index, choice) ->
if(choice.statements.isEmpty())
removals.add(index)
}
removals.reversed().forEach { whenStmt.choices.removeAt(it) }
return noModifications
}
}

View File

@ -1,6 +1,8 @@
TODO
====
- compiling Rockrunner with -noopt crashes the program soon after startup
- IR: reduce the number of branch instructions such as BEQ, BEQR, etc (gradually), replace with CMP(I) + status branch instruction
- IR: reduce amount of CMP/CMPI after instructions that set the status bits correctly (LOADs? INC? etc), but only after setting the status bits is verified!

View File

@ -1,27 +1,16 @@
%import gfx2
%import textio
%zeropage basicsafe
main {
sub start() {
gfx2.screen_mode(6) ; 1 and 5 are lo-res and hi-res monochrome
ubyte var = 0
uword xx
gfx2.rect(10, 10, 180, 140, 3)
gfx2.rect(12, 12, 180, 140, 3)
cbm.SETTIM(0,0,0)
for xx in 5 to 100 {
gfx2.text(xx, xx, 1, sc:"hello world! should be pixel-aligned.")
gfx2.text(xx, xx, 2, sc:"hello world! should be pixel-aligned.")
gfx2.text(xx, xx, 3, sc:"hello world! should be pixel-aligned.")
gfx2.text(xx, xx, 0, sc:"hello world! should be pixel-aligned.")
when var {
1 -> txt.print("one")
2 -> txt.print("two")
0 -> {
}
else -> txt.print("other")
}
gfx2.screen_mode(0)
txt.print_uw(cbm.RDTIM16())
txt.print(" jiffies")
repeat { }
}
}