From b89ad4b3285d98f54396446d523c7aefb59b9a47 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 29 Jul 2023 18:25:52 +0200 Subject: [PATCH] don't optimize empty where choice away! It would call the else clause incorrectly. --- .../prog8/codegen/intermediate/IRCodeGen.kt | 15 ++++++++--- .../compiler/astprocessing/VariousCleanups.kt | 10 ------- docs/source/todo.rst | 2 ++ examples/test.p8 | 27 ++++++------------- 4 files changed, 21 insertions(+), 33 deletions(-) diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt index 49ab1b0f9..bad29de77 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt @@ -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) + } } } } diff --git a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt index 0b377748b..7b3658af1 100644 --- a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt +++ b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt @@ -262,15 +262,5 @@ internal class VariousCleanups(val program: Program, val errors: IErrorReporter, } return noModifications } - - override fun after(whenStmt: When, parent: Node): Iterable { - val removals = mutableListOf() - whenStmt.choices.withIndex().forEach { (index, choice) -> - if(choice.statements.isEmpty()) - removals.add(index) - } - removals.reversed().forEach { whenStmt.choices.removeAt(it) } - return noModifications - } } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index e38dc43c6..fff5915f8 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -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! diff --git a/examples/test.p8 b/examples/test.p8 index 2e97cdea7..681d459c2 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -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 { } } }