remove empty when choices, fixes ir compilation error on those

This commit is contained in:
Irmen de Jong 2023-07-07 20:34:24 +02:00
parent 334d382bfa
commit d2154f5f2e
5 changed files with 54 additions and 45 deletions

View File

@ -99,16 +99,18 @@ class IRCodeGen(
// make sure that first chunks in Blocks and Subroutines share the name of the block/sub as label.
irProg.blocks.forEach { block ->
block.children.firstOrNull { it is IRInlineAsmChunk }?.let { first->
first as IRInlineAsmChunk
if(first.label==null) {
val replacement = IRInlineAsmChunk(block.label, first.assembly, first.isIR, first.next)
if(block.isNotEmpty()) {
val firstAsm = block.children[0] as? IRInlineAsmChunk
if(firstAsm!=null) {
if(firstAsm.label==null) {
val replacement = IRInlineAsmChunk(block.label, firstAsm.assembly, firstAsm.isIR, firstAsm.next)
block.children.removeAt(0)
block.children.add(0, replacement)
} else if(first.label != block.label) {
} else if(firstAsm.label != block.label) {
throw AssemblyError("first chunk in block has label that differs from block name")
}
}
}
block.children.filterIsInstance<IRSubroutine>().forEach { sub ->
if(sub.chunks.isNotEmpty()) {

View File

@ -262,5 +262,15 @@ 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

@ -451,4 +451,26 @@ main {
}
}
test("asm chunk labels in IR code") {
val src="""
main {
sub start() {
instructions.match()
}
}
instructions {
asmsub match() {
%asm {{
rts
}}
}
%asm {{
nop
}}
}"""
compileText(VMTarget(), false, src, writeAssembly = true) shouldNotBe null
}
})

View File

@ -1,8 +1,6 @@
TODO
====
- Fix expericodegen errors (assem, rockrunners)
...

View File

@ -1,41 +1,18 @@
%import textio
main {
sub start() {
; uword[] routines = [ 0, &command, $4444 ]
; cx16.r0 = routines[1]
&ubyte[5] cells = $4000
cells = [1,2,3,4,5]
str name = "irmen"
ubyte[5] othercells = [11,22,33,44,55]
txt.print_ub(1 in cells)
txt.print_ub(44 in othercells)
txt.print_ub('a' in name)
txt.print_ub('e' in name)
txt.nl()
txt.print_ub(all(othercells))
txt.print_ub(any(othercells))
othercells[3] = 0
txt.print_ub(all(othercells))
txt.print_ub(any(othercells))
reverse(othercells)
sort(othercells)
txt.nl()
txt.print_ub(all(cells))
txt.print_ub(any(cells))
cells[3] = 0
txt.print_ub(all(cells))
txt.print_ub(any(cells))
reverse(cells)
sort(cells)
cx16.r0 = 2
when cx16.r0 {
1-> {
;nothing
}
2 -> {
txt.print("two")
}
0-> {
;nothing
}
}
sub command() {
cx16.r0++
}
}