repeat-forever loop is now replaced by label+jump

This commit is contained in:
Irmen de Jong 2022-03-08 03:25:34 +01:00
parent 1078cc4642
commit 9872f43cbf
3 changed files with 18 additions and 11 deletions

View File

@ -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 {

View File

@ -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<IAstModification> {
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
}
}

View File

@ -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