error when 'else' choice in when statemetn isn't the last one, also generate slightly better code for when statements

This commit is contained in:
Irmen de Jong 2021-12-05 21:54:46 +01:00
parent edffe92a24
commit ff69da3fa2
4 changed files with 24 additions and 14 deletions

View File

@ -1293,7 +1293,6 @@ $repeatLabel lda $counterVar
if(choice.values==null) {
// the else choice
translate(choice.statements)
jmp(endLabel)
} else {
choiceBlocks.add(choiceLabel to choice.statements)
for (cv in choice.values!!) {
@ -1313,10 +1312,11 @@ $repeatLabel lda $counterVar
}
}
jmp(endLabel)
for(choiceBlock in choiceBlocks) {
out(choiceBlock.first)
translate(choiceBlock.second)
jmp(endLabel)
for(choiceBlock in choiceBlocks.withIndex()) {
out(choiceBlock.value.first)
translate(choiceBlock.value.second)
if(choiceBlock.index<choiceBlocks.size-1)
jmp(endLabel)
}
out(endLabel)
}

View File

@ -253,6 +253,13 @@ internal class StatementReorderer(val program: Program,
}
override fun after(whenStatement: WhenStatement, parent: Node): Iterable<IAstModification> {
val lastChoiceValues = whenStatement.choices.lastOrNull()?.values
if(lastChoiceValues?.isNotEmpty()==true) {
val elseChoice = whenStatement.choices.indexOfFirst { it.values==null || it.values?.isEmpty()==true }
if(elseChoice>=0)
errors.err("else choice must be the last one", whenStatement.choices[elseChoice].position)
}
val choices = whenStatement.choiceValues(program).sortedBy {
it.first?.first() ?: Int.MAX_VALUE
}

View File

@ -3,8 +3,7 @@ TODO
For next compiler release (7.5)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- fix: when statements generate unneeded branches to choice_end?
...
Blocked by an official Commander-x16 v39 release

View File

@ -4,14 +4,18 @@
main {
sub start() {
ubyte @shared bb = @(cx16.r0)
}
ubyte @shared bb
sub count() -> ubyte {
repeat {
%asm {{
rts
}}
when bb {
0,1,2 -> {
bb ++
}
3 -> {
bb--
}
else -> {
bb=0
}
}
}
}