Skip to content

Commit

Permalink
don't optimize empty where choice away! It would call the else clause…
Browse files Browse the repository at this point in the history
… incorrectly.
  • Loading branch information
irmen committed Jul 29, 2023
1 parent c112b32 commit b89ad4b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 33 deletions.
15 changes: 11 additions & 4 deletions codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Expand Down
10 changes: 0 additions & 10 deletions compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

2 changes: 2 additions & 0 deletions docs/source/todo.rst
Original file line number Diff line number Diff line change
@@ -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!

Expand Down
27 changes: 8 additions & 19 deletions examples/test.p8
Original file line number Diff line number Diff line change
@@ -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 { }
}
}

0 comments on commit b89ad4b

Please sign in to comment.