diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt index f3c262571..02e7e4939 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt @@ -582,14 +582,7 @@ class AsmGen(internal val program: Program, loopEndLabels.push(endLabel) when (stmt.iterations) { - null -> { - // endless loop - val repeatLabel = makeLabel("repeat") - out(repeatLabel) - translate(stmt.body) - jmp(repeatLabel) - out(endLabel) - } + null -> throw AssemblyError("repeat-forever loop should have been replaced by label+jump") is NumericLiteral -> { val iterations = (stmt.iterations as NumericLiteral).number.toInt() when { diff --git a/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt b/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt index 778fb7ea2..8e36b2863 100644 --- a/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt +++ b/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt @@ -15,6 +15,7 @@ import prog8.ast.walk.AstWalker import prog8.ast.walk.IAstModification import prog8.compilerinterface.IErrorReporter +private var generatedLabelSequenceNumber: Int = 0 internal class CodeDesugarer(val program: Program, private val errors: IErrorReporter) : AstWalker() { @@ -27,9 +28,9 @@ internal class CodeDesugarer(val program: Program, private val errors: IErrorRep // - replace 'break' statements by a goto + generated after label. // - replace while and do-until loops by just jumps. // - replace peek() and poke() by direct memory accesses. + // - repeat-forever loops replaced by label+jump. - private var generatedLabelSequenceNumber: Int = 0 private val generatedLabelPrefix = "prog8_label_" private fun makeLabel(postfix: String, position: Position): Label { @@ -137,4 +138,17 @@ _after: } return noModifications } + + override fun after(repeatLoop: RepeatLoop, parent: Node): Iterable { + if(repeatLoop.iterations==null) { + val label = makeLabel("repeat", repeatLoop.position) + val jump = jumpLabel(label) + return listOf( + IAstModification.InsertFirst(label, repeatLoop.body), + IAstModification.InsertLast(jump, repeatLoop.body), + IAstModification.ReplaceNode(repeatLoop, repeatLoop.body, parent) + ) + } + return noModifications + } } diff --git a/compiler/test/TestScoping.kt b/compiler/test/TestScoping.kt index c2b2f1142..6e43d78ab 100644 --- a/compiler/test/TestScoping.kt +++ b/compiler/test/TestScoping.kt @@ -37,7 +37,7 @@ class TestScoping: FunSpec({ val src = """ main { sub start() { - repeat { + repeat 10 { ubyte xx = 99 xx++ } @@ -91,7 +91,7 @@ class TestScoping: FunSpec({ iflabel: } - repeat { + repeat 10 { addr = &iflabel addr = &labelinside addr = &labeloutside