diff --git a/compiler/src/prog8/compiler/intermediate/IntermediateProgram.kt b/compiler/src/prog8/compiler/intermediate/IntermediateProgram.kt index 715395e2e..f12037cf6 100644 --- a/compiler/src/prog8/compiler/intermediate/IntermediateProgram.kt +++ b/compiler/src/prog8/compiler/intermediate/IntermediateProgram.kt @@ -28,29 +28,17 @@ class IntermediateProgram(val name: String, var loadAddress: Int, val heap: Heap val memoryPointers: MutableMap> = mutableMapOf(), val labels: MutableMap = mutableMapOf(), // names are fully scoped val force_output: Boolean) - { - val numVariables: Int - get() { return variables.size } - val numInstructions: Int - get() { return instructions.filter { it.opcode!= Opcode.LINE }.size } - val variablesMarkedForZeropage: MutableSet = mutableSetOf() // TODO maybe this can be removed now we have ValueParameters - } val allocatedZeropageVariables = mutableMapOf>() val blocks = mutableListOf() val memory = mutableMapOf>() private lateinit var currentBlock: ProgramBlock - val numVariables: Int - get() = blocks.sumBy { it.numVariables } - val numInstructions: Int - get() = blocks.sumBy { it.numInstructions } - fun allocateZeropage(zeropage: Zeropage) { // allocates all @zp marked variables on the zeropage (for all blocks, as long as there is space in the ZP) var notAllocated = 0 for(block in blocks) { - val zpVariables = block.variables.filter { it.scopedname in block.variablesMarkedForZeropage } + val zpVariables = block.variables.filter { it.params.zp==ZeropageWish.REQUIRE_ZEROPAGE || it.params.zp==ZeropageWish.PREFER_ZEROPAGE } if (zpVariables.isNotEmpty()) { for (variable in zpVariables) { if(variable.params.zp==ZeropageWish.NOT_IN_ZEROPAGE || variable.params.memberOfStruct!=null) @@ -433,10 +421,6 @@ class IntermediateProgram(val name: String, var loadAddress: Int, val heap: Heap else -> throw CompilerException("weird datatype") } currentBlock.variables.add(Variable(scopedname, value, valueparams)) - if(decl.zeropage==ZeropageWish.PREFER_ZEROPAGE) - currentBlock.variablesMarkedForZeropage.add(scopedname) - else if(decl.zeropage==ZeropageWish.REQUIRE_ZEROPAGE) - TODO("REQUIRE_ZEROPAGE not yet implemented") } VarDeclType.MEMORY -> { // note that constants are all folded away, but assembly code may still refer to them diff --git a/compiler/src/prog8/compiler/target/c64/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/AsmGen.kt index 4dd1403dc..4b55e82eb 100644 --- a/compiler/src/prog8/compiler/target/c64/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/AsmGen.kt @@ -49,7 +49,6 @@ class AsmGen(private val options: CompilationOptions, private val program: Inter val newblocks = mutableListOf() for(block in program.blocks) { val newvars = block.variables.map { IntermediateProgram.Variable(symname(it.scopedname, block), it.value, it.params) }.toMutableList() - val newvarsZeropaged = block.variablesMarkedForZeropage.map{symname(it, block)}.toMutableSet() val newlabels = block.labels.map { symname(it.key, block) to it.value}.toMap().toMutableMap() val newinstructions = block.instructions.asSequence().map { when { @@ -70,8 +69,6 @@ class AsmGen(private val options: CompilationOptions, private val program: Inter newMempointers, newlabels, force_output = block.force_output) - newblock.variablesMarkedForZeropage.clear() - newblock.variablesMarkedForZeropage.addAll(newvarsZeropaged) newblocks.add(newblock) } program.blocks.clear() @@ -253,7 +250,6 @@ class AsmGen(private val options: CompilationOptions, private val program: Inter val address = zeropage.allocate(sym, variable.value.type, null) out("${variable.scopedname} = $address\t; auto zp ${variable.value.type}") // make sure we add the var to the set of zpvars for this block - blk.variablesMarkedForZeropage.add(variable.scopedname) program.allocatedZeropageVariables[sym] = Pair(address, variable.value.type) } catch (x: ZeropageDepletedError) { // leave it as it is. @@ -310,8 +306,9 @@ class AsmGen(private val options: CompilationOptions, private val program: Inter out("; other variables sorted by type") val sortedVars = normalVars.sortedBy { it.value.type } for (variable in sortedVars) { - if(variable.scopedname in block.variablesMarkedForZeropage) - continue // skip the ones that belong in the zero page + val sym = symname(block.name + "." + variable.scopedname, null) + if(sym in program.allocatedZeropageVariables) + continue // skip the ones that already belong in the zero page vardecl2asm(variable.scopedname, variable.value, variable.params) } } diff --git a/examples/test.p8 b/examples/test.p8 index 0e6eb8078..0e6df1a1b 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -7,11 +7,69 @@ sub start() { + word w1 + word w2 + word w3 + word w4 + word w5 + word w6 + word w7 + word w8 + word w9 + word w10 + word w11 + word w12 + word w13 + word w14 + word w15 + word w16 + word w17 + word w18 + word w19 + word @zp w20 + word @zp w21 + word w22 + word w23 + word w24 + word w25 + word w26 + word w27 + word w28 + word w29 + word w30 + ubyte[] blaat = 10 to 20 for ubyte c in 'a' to 'z' { c64.CHROUT(blaat[c]) } + + word q + q=w26 + q+=w27 + q+=w28 + q+=w29 + q+=w30 + q+=w10 + q+=w11 + q+=w12 + q+=w13 + q+=w14 + q+=w15 + q+=w16 + q+=w17 + q+=w18 + q+=w19 + q+=w20 + q+=w21 + q+=w22 + q+=w23 + q+=w24 + q+=w25 + q+=w26 + q+=w27 + + } }