Skip to content

Commit c461559

Browse files
committed
fixing label names, fixes #11
1 parent 25e3b59 commit c461559

File tree

3 files changed

+16
-94
lines changed

3 files changed

+16
-94
lines changed

compiler/src/prog8/compiler/target/c64/AsmGen.kt

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram,
2323
private var breakpointCounter = 0
2424

2525
init {
26-
// Because 64tass understands scoped names via .proc / .block,
27-
// we'll strip the block prefix from all scoped names in the program.
28-
// Also, convert invalid label names (such as "<anon-1>") to something that's allowed.
29-
// Also have to do that for the variablesMarkedForZeropage! TODO is this true?
26+
// Convert invalid label names (such as "<anon-1>") to something that's allowed.
3027
val newblocks = mutableListOf<IntermediateProgram.ProgramBlock>()
3128
for(block in program.blocks) {
3229
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,
4239
callLabel2 = if (it.callLabel2 != null) symname(it.callLabel2, block) else null)
4340
}
4441
}.toMutableList()
45-
val newConstants = block.memoryPointers.map { symname(it.key, block) to it.value }.toMap().toMutableMap()
42+
val newMempointers = block.memoryPointers.map { symname(it.key, block) to it.value }.toMap().toMutableMap()
4643
val newblock = IntermediateProgram.ProgramBlock(
4744
block.name,
4845
block.address,
4946
newinstructions,
5047
newvars,
51-
newConstants,
48+
newMempointers,
5249
newlabels,
5350
force_output = block.force_output)
5451
newblock.variablesMarkedForZeropage.clear()
@@ -58,10 +55,9 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram,
5855
program.blocks.clear()
5956
program.blocks.addAll(newblocks)
6057

61-
// TODO is this still needed?????
62-
// val newAllocatedZp = program.allocatedZeropageVariables.map { symname(it.key, null) to it.value}
63-
// program.allocatedZeropageVariables.clear()
64-
// program.allocatedZeropageVariables.putAll(newAllocatedZp)
58+
val newAllocatedZp = program.allocatedZeropageVariables.map { symname(it.key, null) to it.value}
59+
program.allocatedZeropageVariables.clear()
60+
program.allocatedZeropageVariables.putAll(newAllocatedZp)
6561

6662
// make a list of all const floats that are used
6763
for(block in program.blocks) {
@@ -107,11 +103,11 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram,
107103

108104

109105
// convert a fully scoped name (defined in the given block) to a valid assembly symbol name
110-
private fun symname(scoped: String, block: IntermediateProgram.ProgramBlock): String {
106+
private fun symname(scoped: String, block: IntermediateProgram.ProgramBlock?): String {
111107
if(' ' in scoped)
112108
return scoped
113109
val blockLocal: Boolean
114-
var name = if (scoped.startsWith("${block.name}.")) {
110+
var name = if (block!=null && scoped.startsWith("${block.name}.")) {
115111
blockLocal = true
116112
scoped.substring(block.name.length+1)
117113
}
@@ -221,16 +217,16 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram,
221217
out("* = ${block.address?.toHex()}")
222218
}
223219

224-
// deal with zeropage variables TODO does this still work correctly (the symname was changed a little)
220+
// deal with zeropage variables
225221
for(variable in blk.variables) {
226-
val sym = symname(blk.name+"."+variable.key, blk)
222+
val sym = symname(blk.name+"."+variable.key, null)
227223
val zpVar = program.allocatedZeropageVariables[sym]
228224
if(zpVar==null) {
229225
// 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)
230226
if(variable.value.type in zeropage.allowedDatatypes && variable.value.type != DataType.FLOAT) {
231227
try {
232228
val address = zeropage.allocate(sym, variable.value.type, null)
233-
out("${variable.key} = $address\t; zp ${variable.value.type}")
229+
out("${variable.key} = $address\t; auto zp ${variable.value.type}")
234230
// make sure we add the var to the set of zpvars for this block
235231
blk.variablesMarkedForZeropage.add(variable.key)
236232
program.allocatedZeropageVariables[sym] = Pair(address, variable.value.type)

examples/looplabelproblem.p8

Lines changed: 0 additions & 20 deletions
This file was deleted.

examples/test.p8

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,15 @@
11
%zeropage basicsafe
22

3-
; @todo fix this loop labeling problem (issue #11 on github): it generates invalid asm due to improper label names
4-
53

64
~ main {
75

8-
label2:
6+
ubyte @zp var1
97

108
sub start() {
11-
12-
label3:
13-
byte var1
14-
15-
label4:
16-
17-
sub subsub() {
18-
19-
label3:
20-
label4:
21-
byte var1
22-
}
23-
24-
sub subsub2() {
25-
label3:
26-
label4:
27-
byte var1
28-
byte var2
29-
byte var3
30-
31-
label5ss2:
32-
33-
if A>10 {
34-
35-
label6ss2:
36-
A=44
37-
while true {
38-
label7ss2:
39-
;derp
40-
}
41-
} else {
42-
43-
gameoverss2:
44-
goto gameoverss2
45-
}
46-
label8ss2:
47-
48-
}
49-
50-
label5:
51-
52-
if A>10 {
53-
54-
label6:
55-
A=44
56-
while true {
57-
label7:
58-
;derp
59-
}
60-
} else {
61-
62-
gameover:
63-
goto gameover
64-
}
65-
label8:
66-
9+
ubyte @zp var1
10+
A=20
11+
A=var1
12+
Y=main.var1
6713
}
6814

6915
}

0 commit comments

Comments
 (0)