From d2154f5f2e7ceddc4596664de6feb01c44e110e0 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 7 Jul 2023 20:34:24 +0200 Subject: [PATCH] remove empty when choices, fixes ir compilation error on those --- .../prog8/codegen/intermediate/IRCodeGen.kt | 18 +++---- .../compiler/astprocessing/VariousCleanups.kt | 10 ++++ compiler/test/vm/TestCompilerVirtual.kt | 22 +++++++++ docs/source/todo.rst | 2 - examples/test.p8 | 47 +++++-------------- 5 files changed, 54 insertions(+), 45 deletions(-) diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt index 0d4a282ab..298262bdb 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt @@ -99,14 +99,16 @@ 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) - block.children.removeAt(0) - block.children.add(0, replacement) - } else if(first.label != block.label) { - throw AssemblyError("first chunk in block has label that differs from block name") + 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(firstAsm.label != block.label) { + throw AssemblyError("first chunk in block has label that differs from block name") + } } } diff --git a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt index 7b3658af1..0b377748b 100644 --- a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt +++ b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt @@ -262,5 +262,15 @@ 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/compiler/test/vm/TestCompilerVirtual.kt b/compiler/test/vm/TestCompilerVirtual.kt index 7e8c58412..b958b0c13 100644 --- a/compiler/test/vm/TestCompilerVirtual.kt +++ b/compiler/test/vm/TestCompilerVirtual.kt @@ -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 + } + }) \ No newline at end of file diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 190b9102e..d28f97d31 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,8 +1,6 @@ TODO ==== -- Fix expericodegen errors (assem, rockrunners) - ... diff --git a/examples/test.p8 b/examples/test.p8 index 7dfc8f478..14a13360e 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -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) - } - - sub command() { - cx16.r0++ + cx16.r0 = 2 + when cx16.r0 { + 1-> { + ;nothing + } + 2 -> { + txt.print("two") + } + 0-> { + ;nothing + } + } } }