diff --git a/compiler/src/prog8/compiler/target/c64/codegen2/AsmGen2.kt b/compiler/src/prog8/compiler/target/c64/codegen2/AsmGen2.kt index ffd8cbf7f..ac5b66999 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen2/AsmGen2.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen2/AsmGen2.kt @@ -836,14 +836,44 @@ internal class AsmGen2(val program: Program, } private fun translate(stmt: WhileLoop) { - TODO("while $stmt") + val whileLabel = makeLabel("while") + val endLabel = makeLabel("whileend") + out(whileLabel) + // TODO optimize for the simple cases, can we avoid stack use? + translateExpression(stmt.condition) + if(stmt.condition.inferType(program) in ByteDatatypes) { + out(" inx | lda $ESTACK_LO_HEX,x | beq $endLabel") + } else { + out(""" + inx + lda $ESTACK_LO_HEX,x + bne + + lda $ESTACK_HI_HEX,x + beq $endLabel ++ """) + } + translate(stmt.body) + out(" jmp $whileLabel") + out(endLabel) } private fun translate(stmt: RepeatLoop) { - // TODO("repeat $stmt") - out(";------ TODO REPEAT") + val repeatLabel = makeLabel("repeat") + out(repeatLabel) + // TODO optimize this for the simple cases, can we avoid stack use? translate(stmt.body) - out(";------ TODO REPEAT END") + translateExpression(stmt.untilCondition) + if(stmt.untilCondition.inferType(program) in ByteDatatypes) { + out(" inx | lda $ESTACK_LO_HEX,x | beq $repeatLabel") + } else { + out(""" + inx + lda $ESTACK_LO_HEX,x + bne + + lda $ESTACK_HI_HEX,x + beq $repeatLabel ++ """) + } } private fun translate(stmt: WhenStatement) { diff --git a/examples/test.p8 b/examples/test.p8 index 976cb9241..01af605b9 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -4,32 +4,22 @@ main { sub start() { - byte b1 - byte b2 - word w1 - word w2 - ubyte ub1 - ubyte ub2 - uword uw1 - uword uw2 + byte bb=10 - b1 = 5 - b2= 122 - b1 = b2+b1 - c64scr.print_b(b1) - c64.CHROUT('\n') + while bb<15 { + bb++ + c64scr.print_b(bb) + c64.CHROUT('\n') + } + + word ww=5 + + while ww > -5 { + ww-- + c64scr.print_w(ww) + c64.CHROUT('\n') + } - w1 = -1111 - w2 = 11231 - w1 = w2+w1 - c64scr.print_w(w1) - c64.CHROUT('\n') - uw1 = 55555 - uw2 = 1123 - uw1 = uw2+uw1 - c64scr.print_uw(uw1) - c64.CHROUT('\n') - c64.CHROUT('\n') } }