From cf6ea63fa66aea05411da84f802883770487a520 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 18 Aug 2020 15:29:39 +0200 Subject: [PATCH] forloop asm done --- compiler/src/prog8/compiler/Zeropage.kt | 2 +- .../compiler/target/c64/codegen/AsmGen.kt | 6 ++-- .../target/c64/codegen/ForLoopsAsmGen.kt | 28 +++++++++++++------ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/compiler/src/prog8/compiler/Zeropage.kt b/compiler/src/prog8/compiler/Zeropage.kt index f0a53bece..4eaa0ed9c 100644 --- a/compiler/src/prog8/compiler/Zeropage.kt +++ b/compiler/src/prog8/compiler/Zeropage.kt @@ -16,7 +16,7 @@ abstract class Zeropage(protected val options: CompilationOptions) { fun available() = if(options.zeropage==ZeropageType.DONTUSE) 0 else free.size fun allocate(scopedname: String, datatype: DataType, position: Position?, errors: ErrorReporter): Int { - assert(scopedname.isEmpty() || !allocations.values.any { it.first==scopedname } ) {"isSameAs scopedname can't be allocated twice"} + assert(scopedname.isEmpty() || !allocations.values.any { it.first==scopedname } ) {"scopedname can't be allocated twice"} if(options.zeropage==ZeropageType.DONTUSE) throw CompilerException("zero page usage has been disabled") diff --git a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt index d117e9801..76104e9fb 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/AsmGen.kt @@ -27,9 +27,9 @@ import kotlin.math.absoluteValue internal class AsmGen(private val program: Program, - private val errors: ErrorReporter, - private val zeropage: Zeropage, - private val options: CompilationOptions, + val errors: ErrorReporter, + val zeropage: Zeropage, + val options: CompilationOptions, private val outputDir: Path): IAssemblyGenerator { private val assemblyLines = mutableListOf() diff --git a/compiler/src/prog8/compiler/target/c64/codegen/ForLoopsAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/ForLoopsAsmGen.kt index 31f7dbd2c..eb15dc09c 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/ForLoopsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/ForLoopsAsmGen.kt @@ -291,7 +291,7 @@ $endLabel""") } DataType.ARRAY_UB, DataType.ARRAY_B -> { val length = decl.arraysize!!.size()!! - val indexVar = asmgen.makeLabel("for_index") // TODO allocate dynamically, zero page preferred if length >= 16 + val indexVar = asmgen.makeLabel("for_index") asmgen.out(""" ldy #0 $loopLabel sty $indexVar @@ -313,13 +313,19 @@ $loopLabel sty $indexVar bne $loopLabel beq $endLabel""") } - asmgen.out(""" -$indexVar .byte 0 -$endLabel""") + if(length>=16 && asmgen.zeropage.available() > 0) { + // allocate index var on ZP if possible + val zpAddr = asmgen.zeropage.allocate(indexVar, DataType.UBYTE, stmt.position, asmgen.errors) + asmgen.out("""$indexVar = $zpAddr ; auto zp UBYTE""") + } else { + asmgen.out(""" +$indexVar .byte 0""") + } + asmgen.out(endLabel) } DataType.ARRAY_W, DataType.ARRAY_UW -> { val length = decl.arraysize!!.size()!! * 2 - val indexVar = asmgen.makeLabel("for_index") // todo allocate dynamically, zero page preferred if length >= 16 + val indexVar = asmgen.makeLabel("for_index") val loopvarName = asmgen.asmIdentifierName(stmt.loopVar) asmgen.out(""" ldy #0 @@ -346,9 +352,15 @@ $loopLabel sty $indexVar bne $loopLabel beq $endLabel""") } - asmgen.out(""" -$indexVar .byte 0 -$endLabel""") + if(length>=16 && asmgen.zeropage.available() > 0) { + // allocate index var on ZP if possible + val zpAddr = asmgen.zeropage.allocate(indexVar, DataType.UBYTE, stmt.position, asmgen.errors) + asmgen.out("""$indexVar = $zpAddr ; auto zp UBYTE""") + } else { + asmgen.out(""" +$indexVar .byte 0""") + } + asmgen.out(endLabel) } DataType.ARRAY_F -> { throw AssemblyError("for loop with floating point variables is not supported")