more forloop codegen

This commit is contained in:
Irmen de Jong 2020-08-17 23:40:29 +02:00
parent 3976cc26a2
commit 4487499663
2 changed files with 65 additions and 82 deletions

View File

@ -37,8 +37,11 @@ internal class ForLoopsAsmGen(private val program: Program, private val asmgen:
} }
private fun translateForOverNonconstRange(stmt: ForLoop, iterableDt: DataType, range: RangeExpr) { 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 loopLabel = asmgen.makeLabel("for_loop")
val endLabel = asmgen.makeLabel("for_end") val endLabel = asmgen.makeLabel("for_end")
val modifiedLabel = asmgen.makeLabel("for_modified")
val modifiedLabel2 = asmgen.makeLabel("for_modifiedb")
asmgen.loopEndLabels.push(endLabel) asmgen.loopEndLabels.push(endLabel)
val stepsize=range.step.constValue(program)!!.number.toInt() val stepsize=range.step.constValue(program)!!.number.toInt()
when(iterableDt) { when(iterableDt) {
@ -54,13 +57,15 @@ internal class ForLoopsAsmGen(private val program: Program, private val asmgen:
asmgen.translateExpression(range.from) asmgen.translateExpression(range.from)
asmgen.out(""" asmgen.out("""
inx inx
lda ${ESTACK_LO_HEX},x lda $ESTACK_LO_HEX,x
sta $varname sta $varname
lda $ESTACK_LO_PLUS1_HEX,x
sta $modifiedLabel+1
$loopLabel""") $loopLabel""")
asmgen.translate(stmt.body) asmgen.translate(stmt.body)
asmgen.out(""" asmgen.out("""
lda $varname lda $varname
cmp $ESTACK_LO_PLUS1_HEX,x TODO modifying code $modifiedLabel cmp #0 ; modified
beq $endLabel beq $endLabel
$incdec $varname $incdec $varname
jmp $loopLabel jmp $loopLabel
@ -76,8 +81,10 @@ $endLabel inx""")
asmgen.translateExpression(range.from) asmgen.translateExpression(range.from)
asmgen.out(""" asmgen.out("""
inx inx
lda ${ESTACK_LO_HEX},x lda $ESTACK_LO_HEX,x
sta $varname sta $varname
lda $ESTACK_LO_PLUS1_HEX,x
sta $modifiedLabel+1
$loopLabel""") $loopLabel""")
asmgen.translate(stmt.body) asmgen.translate(stmt.body)
asmgen.out(""" asmgen.out("""
@ -87,7 +94,7 @@ $loopLabel""")
clc clc
adc #$stepsize adc #$stepsize
sta $varname sta $varname
cmp $ESTACK_LO_PLUS1_HEX,x TODO modifying code $modifiedLabel cmp #0 ; modified
bcc $loopLabel bcc $loopLabel
beq $loopLabel""") beq $loopLabel""")
} else { } else {
@ -95,7 +102,7 @@ $loopLabel""")
sec sec
sbc #${stepsize.absoluteValue} sbc #${stepsize.absoluteValue}
sta $varname sta $varname
cmp $ESTACK_LO_PLUS1_HEX,x TODO modifying code $modifiedLabel cmp #0 ; modified
bcs $loopLabel""") bcs $loopLabel""")
} }
asmgen.out(""" asmgen.out("""
@ -113,14 +120,19 @@ $endLabel inx""")
val assignLoopvar = Assignment(AssignTarget(stmt.loopVar, null, null, stmt.loopVar.position), range.from, range.position) val assignLoopvar = Assignment(AssignTarget(stmt.loopVar, null, null, stmt.loopVar.position), range.from, range.position)
assignLoopvar.linkParents(stmt) assignLoopvar.linkParents(stmt)
asmgen.translate(assignLoopvar) asmgen.translate(assignLoopvar)
asmgen.out(loopLabel) asmgen.out("""
lda $ESTACK_HI_PLUS1_HEX,x
sta $modifiedLabel+1
lda $ESTACK_LO_PLUS1_HEX,x
sta $modifiedLabel2+1
$loopLabel""")
asmgen.translate(stmt.body) asmgen.translate(stmt.body)
asmgen.out(""" asmgen.out("""
lda $varname+1 lda $varname+1
cmp $ESTACK_HI_PLUS1_HEX,x TODO modifying code $modifiedLabel cmp #0 ; modified
bne + bne +
lda $varname lda $varname
cmp $ESTACK_LO_PLUS1_HEX,x TODO modifying code $modifiedLabel2 cmp #0 ; modified
beq $endLabel""") beq $endLabel""")
if(stepsize==1) { if(stepsize==1) {
asmgen.out(""" asmgen.out("""
@ -160,12 +172,12 @@ $endLabel inx""")
lda $varname+1 lda $varname+1
adc #>$stepsize adc #>$stepsize
sta $varname+1 sta $varname+1
lda $ESTACK_HI_PLUS1_HEX,x TODO modifying code lda $ESTACK_HI_PLUS1_HEX,x TODO modifying code1
cmp $varname+1 cmp $varname+1
bcc $endLabel bcc $endLabel
bne $loopLabel bne $loopLabel
lda $varname lda $varname
cmp $ESTACK_LO_PLUS1_HEX,x TODO modifying code cmp $ESTACK_LO_PLUS1_HEX,x TODO modifying code1
bcc $endLabel bcc $endLabel
bcs $loopLabel bcs $loopLabel
$endLabel inx""") $endLabel inx""")
@ -178,9 +190,9 @@ $endLabel inx""")
lda $varname+1 lda $varname+1
adc #>$stepsize adc #>$stepsize
sta $varname+1 sta $varname+1
lda $ESTACK_LO_PLUS1_HEX,x TODO modifying code lda $ESTACK_LO_PLUS1_HEX,x TODO modifying code2
cmp $varname cmp $varname
lda $ESTACK_HI_PLUS1_HEX,x TODO modifying code lda $ESTACK_HI_PLUS1_HEX,x TODO modifying code2
sbc $varname+1 sbc $varname+1
bvc + bvc +
eor #$80 eor #$80
@ -208,11 +220,11 @@ $endLabel inx""")
lda $varname+1 lda $varname+1
sbc #>${stepsize.absoluteValue} sbc #>${stepsize.absoluteValue}
sta $varname+1 sta $varname+1
cmp $ESTACK_HI_PLUS1_HEX,x TODO modifying code cmp $ESTACK_HI_PLUS1_HEX,x TODO modifying code3
bcc $endLabel bcc $endLabel
bne $loopLabel bne $loopLabel
lda $varname lda $varname
cmp $ESTACK_LO_PLUS1_HEX,x TODO modifying code cmp $ESTACK_LO_PLUS1_HEX,x TODO modifying code3
bcs $loopLabel bcs $loopLabel
$endLabel inx""") $endLabel inx""")
} else { } else {
@ -226,9 +238,9 @@ $endLabel inx""")
sbc #>${stepsize.absoluteValue} sbc #>${stepsize.absoluteValue}
sta $varname+1 sta $varname+1
pla pla
cmp $ESTACK_LO_PLUS1_HEX,x TODO modifying code cmp $ESTACK_LO_PLUS1_HEX,x TODO modifying code4
lda $varname+1 lda $varname+1
sbc $ESTACK_HI_PLUS1_HEX,x TODO modifying code sbc $ESTACK_HI_PLUS1_HEX,x TODO modifying code4
bvc + bvc +
eor #$80 eor #$80
+ bpl $loopLabel + bpl $loopLabel
@ -244,6 +256,7 @@ $endLabel inx""")
} }
private fun translateForOverIterableVar(stmt: ForLoop, iterableDt: DataType, ident: IdentifierReference) { private fun translateForOverIterableVar(stmt: ForLoop, iterableDt: DataType, ident: IdentifierReference) {
// TODO optimize this more
val loopLabel = asmgen.makeLabel("for_loop") val loopLabel = asmgen.makeLabel("for_loop")
val endLabel = asmgen.makeLabel("for_end") val endLabel = asmgen.makeLabel("for_end")
asmgen.loopEndLabels.push(endLabel) asmgen.loopEndLabels.push(endLabel)

View File

@ -6,82 +6,52 @@
main { main {
sub start() { sub start() {
byte counterb ; byte[] data = [11,22,33,44,55,66]
word counterw ; word[] dataw = [1111,2222,3333,4444,5555,6666]
;
; byte d
; word w
;
; for d in data {
; c64scr.print_b(d)
; c64.CHROUT(',')
; }
; c64.CHROUT('\n')
; for w in dataw {
; c64scr.print_w(w)
; c64.CHROUT(',')
; }
; c64.CHROUT('\n')
for counterb in -10 to 11 {
c64scr.print_b(counterb) 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(',')
} }
c64.CHROUT('\n') c64.CHROUT('\n')
for counterb in 11 to -10 step -1 { uword ww
c64scr.print_b(counterb) uword fromw = 10
uword endw = 20
for ww in fromw to endw step 3 {
c64scr.print_uw(ww)
c64.CHROUT(',') c64.CHROUT(',')
} }
c64.CHROUT('\n') c64.CHROUT('\n')
for ww in endw to fromw step -3 {
for counterb in -10 to 11 step 2 { c64scr.print_uw(ww)
c64scr.print_b(counterb)
c64.CHROUT(',') c64.CHROUT(',')
} }
c64.CHROUT('\n') c64.CHROUT('\n')
for counterb in 11 to -10 step -2 {
c64scr.print_b(counterb)
c64.CHROUT(',')
}
c64.CHROUT('\n')
for counterb in -10 to 11 step 3 {
c64scr.print_b(counterb)
c64.CHROUT(',')
}
c64.CHROUT('\n')
for counterb in 11 to -10 step -3 {
c64scr.print_b(counterb)
c64.CHROUT(',')
}
c64.CHROUT('\n')
c64.CHROUT('\n')
c64.CHROUT('\n')
for counterw in -10 to 11 {
c64scr.print_w(counterw)
c64.CHROUT(',')
}
c64.CHROUT('\n')
for counterw in 11 to -10 step -1 {
c64scr.print_w(counterw)
c64.CHROUT(',')
}
c64.CHROUT('\n')
for counterw in -10 to 11 step 2 {
c64scr.print_w(counterw)
c64.CHROUT(',')
}
c64.CHROUT('\n')
for counterw in 11 to -10 step -2 {
c64scr.print_w(counterw)
c64.CHROUT(',')
}
c64.CHROUT('\n')
for counterw in -10 to 11 step 3 {
c64scr.print_w(counterw)
c64.CHROUT(',')
}
c64.CHROUT('\n')
for counterw in 11 to -10 step -3 {
c64scr.print_w(counterw)
c64.CHROUT(',')
}
c64.CHROUT('\n')
} }
} }