From c4615591c905a01699465676a39d12d24907663f Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 30 Mar 2019 00:31:40 +0100 Subject: [PATCH] fixing label names, fixes #11 --- .../src/prog8/compiler/target/c64/AsmGen.kt | 26 ++++---- examples/looplabelproblem.p8 | 20 ------ examples/test.p8 | 64 ++----------------- 3 files changed, 16 insertions(+), 94 deletions(-) delete mode 100644 examples/looplabelproblem.p8 diff --git a/compiler/src/prog8/compiler/target/c64/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/AsmGen.kt index af5416e31..83dfbedf7 100644 --- a/compiler/src/prog8/compiler/target/c64/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/AsmGen.kt @@ -23,10 +23,7 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram, private var breakpointCounter = 0 init { - // Because 64tass understands scoped names via .proc / .block, - // we'll strip the block prefix from all scoped names in the program. - // Also, convert invalid label names (such as "") to something that's allowed. - // Also have to do that for the variablesMarkedForZeropage! TODO is this true? + // Convert invalid label names (such as "") to something that's allowed. val newblocks = mutableListOf() for(block in program.blocks) { val newvars = block.variables.map { symname(it.key, block) to it.value }.toMap().toMutableMap() @@ -42,13 +39,13 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram, callLabel2 = if (it.callLabel2 != null) symname(it.callLabel2, block) else null) } }.toMutableList() - val newConstants = block.memoryPointers.map { symname(it.key, block) to it.value }.toMap().toMutableMap() + val newMempointers = block.memoryPointers.map { symname(it.key, block) to it.value }.toMap().toMutableMap() val newblock = IntermediateProgram.ProgramBlock( block.name, block.address, newinstructions, newvars, - newConstants, + newMempointers, newlabels, force_output = block.force_output) newblock.variablesMarkedForZeropage.clear() @@ -58,10 +55,9 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram, program.blocks.clear() program.blocks.addAll(newblocks) - // TODO is this still needed????? -// val newAllocatedZp = program.allocatedZeropageVariables.map { symname(it.key, null) to it.value} -// program.allocatedZeropageVariables.clear() -// program.allocatedZeropageVariables.putAll(newAllocatedZp) + val newAllocatedZp = program.allocatedZeropageVariables.map { symname(it.key, null) to it.value} + program.allocatedZeropageVariables.clear() + program.allocatedZeropageVariables.putAll(newAllocatedZp) // make a list of all const floats that are used for(block in program.blocks) { @@ -107,11 +103,11 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram, // convert a fully scoped name (defined in the given block) to a valid assembly symbol name - private fun symname(scoped: String, block: IntermediateProgram.ProgramBlock): String { + private fun symname(scoped: String, block: IntermediateProgram.ProgramBlock?): String { if(' ' in scoped) return scoped val blockLocal: Boolean - var name = if (scoped.startsWith("${block.name}.")) { + var name = if (block!=null && scoped.startsWith("${block.name}.")) { blockLocal = true scoped.substring(block.name.length+1) } @@ -221,16 +217,16 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram, out("* = ${block.address?.toHex()}") } - // deal with zeropage variables TODO does this still work correctly (the symname was changed a little) + // deal with zeropage variables for(variable in blk.variables) { - val sym = symname(blk.name+"."+variable.key, blk) + val sym = symname(blk.name+"."+variable.key, null) val zpVar = program.allocatedZeropageVariables[sym] if(zpVar==null) { // This var is not on the ZP yet. Attempt to move it there (if it's not a float, those take up too much space) if(variable.value.type in zeropage.allowedDatatypes && variable.value.type != DataType.FLOAT) { try { val address = zeropage.allocate(sym, variable.value.type, null) - out("${variable.key} = $address\t; zp ${variable.value.type}") + out("${variable.key} = $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.key) program.allocatedZeropageVariables[sym] = Pair(address, variable.value.type) diff --git a/examples/looplabelproblem.p8 b/examples/looplabelproblem.p8 deleted file mode 100644 index fc2c5798d..000000000 --- a/examples/looplabelproblem.p8 +++ /dev/null @@ -1,20 +0,0 @@ - -; @todo fix this (issue #11 on github): it generates invalid asm due to improper label names - -~ main { - - sub start() { - - if A>10 { - A=44 - while true { - ;derp - } - } else { - - gameover: - goto gameover - } - } - -} diff --git a/examples/test.p8 b/examples/test.p8 index 3f0b3facb..8440a3618 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,69 +1,15 @@ %zeropage basicsafe -; @todo fix this loop labeling problem (issue #11 on github): it generates invalid asm due to improper label names - ~ main { -label2: + ubyte @zp var1 sub start() { - -label3: - byte var1 - -label4: - - sub subsub() { - -label3: -label4: - byte var1 - } - - sub subsub2() { -label3: -label4: - byte var1 - byte var2 - byte var3 - - label5ss2: - - if A>10 { - - label6ss2: - A=44 - while true { - label7ss2: - ;derp - } - } else { - - gameoverss2: - goto gameoverss2 - } - label8ss2: - - } - -label5: - - if A>10 { - -label6: - A=44 - while true { -label7: - ;derp - } - } else { - - gameover: - goto gameover - } -label8: - + ubyte @zp var1 + A=20 + A=var1 + Y=main.var1 } }