mirror of
https://github.com/irmen/prog8.git
synced 2024-09-17 07:55:28 +00:00
repeat-forever loop is now replaced by label+jump
This commit is contained in:
parent
1078cc4642
commit
9872f43cbf
@ -582,14 +582,7 @@ class AsmGen(internal val program: Program,
|
|||||||
loopEndLabels.push(endLabel)
|
loopEndLabels.push(endLabel)
|
||||||
|
|
||||||
when (stmt.iterations) {
|
when (stmt.iterations) {
|
||||||
null -> {
|
null -> throw AssemblyError("repeat-forever loop should have been replaced by label+jump")
|
||||||
// endless loop
|
|
||||||
val repeatLabel = makeLabel("repeat")
|
|
||||||
out(repeatLabel)
|
|
||||||
translate(stmt.body)
|
|
||||||
jmp(repeatLabel)
|
|
||||||
out(endLabel)
|
|
||||||
}
|
|
||||||
is NumericLiteral -> {
|
is NumericLiteral -> {
|
||||||
val iterations = (stmt.iterations as NumericLiteral).number.toInt()
|
val iterations = (stmt.iterations as NumericLiteral).number.toInt()
|
||||||
when {
|
when {
|
||||||
|
@ -15,6 +15,7 @@ import prog8.ast.walk.AstWalker
|
|||||||
import prog8.ast.walk.IAstModification
|
import prog8.ast.walk.IAstModification
|
||||||
import prog8.compilerinterface.IErrorReporter
|
import prog8.compilerinterface.IErrorReporter
|
||||||
|
|
||||||
|
private var generatedLabelSequenceNumber: Int = 0
|
||||||
|
|
||||||
internal class CodeDesugarer(val program: Program, private val errors: IErrorReporter) : AstWalker() {
|
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 'break' statements by a goto + generated after label.
|
||||||
// - replace while and do-until loops by just jumps.
|
// - replace while and do-until loops by just jumps.
|
||||||
// - replace peek() and poke() by direct memory accesses.
|
// - 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 val generatedLabelPrefix = "prog8_label_"
|
||||||
|
|
||||||
private fun makeLabel(postfix: String, position: Position): Label {
|
private fun makeLabel(postfix: String, position: Position): Label {
|
||||||
@ -137,4 +138,17 @@ _after:
|
|||||||
}
|
}
|
||||||
return noModifications
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ class TestScoping: FunSpec({
|
|||||||
val src = """
|
val src = """
|
||||||
main {
|
main {
|
||||||
sub start() {
|
sub start() {
|
||||||
repeat {
|
repeat 10 {
|
||||||
ubyte xx = 99
|
ubyte xx = 99
|
||||||
xx++
|
xx++
|
||||||
}
|
}
|
||||||
@ -91,7 +91,7 @@ class TestScoping: FunSpec({
|
|||||||
iflabel:
|
iflabel:
|
||||||
}
|
}
|
||||||
|
|
||||||
repeat {
|
repeat 10 {
|
||||||
addr = &iflabel
|
addr = &iflabel
|
||||||
addr = &labelinside
|
addr = &labelinside
|
||||||
addr = &labeloutside
|
addr = &labeloutside
|
||||||
|
Loading…
Reference in New Issue
Block a user