From 353f1954a5f327094879c1632e12aab1b7d53391 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 18 Aug 2020 13:49:08 +0200 Subject: [PATCH] for loop codegen --- .../target/c64/codegen/ForLoopsAsmGen.kt | 50 +++++++++++-------- examples/test.p8 | 39 ++++++++------- 2 files changed, 50 insertions(+), 39 deletions(-) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/ForLoopsAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/ForLoopsAsmGen.kt index 4447edc3f..c7ff34ae2 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/ForLoopsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/ForLoopsAsmGen.kt @@ -37,7 +37,6 @@ internal class ForLoopsAsmGen(private val program: Program, private val asmgen: } private fun translateForOverNonconstRange(stmt: ForLoop, iterableDt: DataType, range: RangeExpr) { - // TODO get rid of all cmp stack,x by using modifying code val loopLabel = asmgen.makeLabel("for_loop") val endLabel = asmgen.makeLabel("for_end") val modifiedLabel = asmgen.makeLabel("for_modified") @@ -137,25 +136,31 @@ $modifiedLabel2 cmp #0 ; modified if(stepsize==1) { asmgen.out(""" + inc $varname - bne + - inc $varname+1 + bne $loopLabel + inc $varname+1 + jmp $loopLabel """) } else { asmgen.out(""" + lda $varname bne + dec $varname+1 -+ dec $varname""") ++ dec $varname + jmp $loopLabel""") } - asmgen.out(""" -+ jmp $loopLabel -$endLabel inx""") + asmgen.out(endLabel) + asmgen.out(" inx") } stepsize > 0 -> { // (u)words, step >= 2 - asmgen.translateExpression(range.to) + asmgen.out(""" + lda $ESTACK_HI_PLUS1_HEX,x + sta $modifiedLabel+1 + lda $ESTACK_LO_PLUS1_HEX,x + sta $modifiedLabel2+1 + """) val varname = asmgen.asmIdentifierName(stmt.loopVar) val assignLoopvar = Assignment(AssignTarget(stmt.loopVar, null, null, stmt.loopVar.position), range.from, range.position) assignLoopvar.linkParents(stmt) @@ -172,12 +177,11 @@ $endLabel inx""") lda $varname+1 adc #>$stepsize sta $varname+1 - lda $ESTACK_HI_PLUS1_HEX,x TODO modifying code1 - cmp $varname+1 - bcc $endLabel - bne $loopLabel - lda $varname - cmp $ESTACK_LO_PLUS1_HEX,x TODO modifying code1 +$modifiedLabel cmp #0 ; modified + bcc $loopLabel + bne $endLabel +$modifiedLabel2 lda #0 ; modified + cmp $varname bcc $endLabel bcs $loopLabel $endLabel inx""") @@ -190,9 +194,9 @@ $endLabel inx""") lda $varname+1 adc #>$stepsize sta $varname+1 - lda $ESTACK_LO_PLUS1_HEX,x TODO modifying code2 +$modifiedLabel2 lda #0 ; modified cmp $varname - lda $ESTACK_HI_PLUS1_HEX,x TODO modifying code2 +$modifiedLabel lda #0 ; modified sbc $varname+1 bvc + eor #$80 @@ -204,6 +208,12 @@ $endLabel inx""") // (u)words, step <= -2 asmgen.translateExpression(range.to) + asmgen.out(""" + lda $ESTACK_HI_PLUS1_HEX,x + sta $modifiedLabel+1 + lda $ESTACK_LO_PLUS1_HEX,x + sta $modifiedLabel2+1 + """) val varname = asmgen.asmIdentifierName(stmt.loopVar) val assignLoopvar = Assignment(AssignTarget(stmt.loopVar, null, null, stmt.loopVar.position), range.from, range.position) assignLoopvar.linkParents(stmt) @@ -220,11 +230,11 @@ $endLabel inx""") lda $varname+1 sbc #>${stepsize.absoluteValue} sta $varname+1 - cmp $ESTACK_HI_PLUS1_HEX,x TODO modifying code3 +$modifiedLabel cmp #0 ; modified bcc $endLabel bne $loopLabel lda $varname - cmp $ESTACK_LO_PLUS1_HEX,x TODO modifying code3 +$modifiedLabel2 cmp #0 ; modified bcs $loopLabel $endLabel inx""") } else { @@ -238,9 +248,9 @@ $endLabel inx""") sbc #>${stepsize.absoluteValue} sta $varname+1 pla - cmp $ESTACK_LO_PLUS1_HEX,x TODO modifying code4 +$modifiedLabel2 cmp #0 ; modified lda $varname+1 - sbc $ESTACK_HI_PLUS1_HEX,x TODO modifying code4 +$modifiedLabel sbc #0 ; modified bvc + eor #$80 + bpl $loopLabel diff --git a/examples/test.p8 b/examples/test.p8 index 398a3a973..f8fb190a8 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -24,32 +24,33 @@ main { ; c64.CHROUT('\n') - ubyte bb - ubyte from = 10 - ubyte end = 20 +; ubyte bb +; ubyte from = 10 +; ubyte end = 20 +; +; for bb in from to end step 3 { +; c64scr.print_ub(bb) +; c64.CHROUT(',') +; } +; c64.CHROUT('\n') +; for bb in end to from step -3 { +; c64scr.print_ub(bb) +; c64.CHROUT(',') +; } +; c64.CHROUT('\n') - for bb in from to end step 3 { - c64scr.print_ub(bb) - c64.CHROUT(',') - } - c64.CHROUT('\n') - for bb in end to from step -3 { - c64scr.print_ub(bb) - c64.CHROUT(',') - } - c64.CHROUT('\n') - - uword ww - uword fromw = 10 - uword endw = 20 + word ww + word fromw = -10 + word endw = 21 for ww in fromw to endw step 3 { - c64scr.print_uw(ww) + c64scr.print_w(ww) c64.CHROUT(',') } + c64.CHROUT('\n') for ww in endw to fromw step -3 { - c64scr.print_uw(ww) + c64scr.print_w(ww) c64.CHROUT(',') } c64.CHROUT('\n')