Skip to content

Commit d4b69ac

Browse files
committed
improved repeat counter vars allocation (re-use var if possible)
1 parent e61a2d7 commit d4b69ac

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -880,11 +880,10 @@ internal class AsmGen(private val program: Program,
880880
}
881881

882882
out("; variables")
883-
for((name, addr) in sub.asmGenInfo.extraVarsZP) {
884-
out("$name = $addr")
885-
}
886-
for((dt, name) in sub.asmGenInfo.extraVars) {
887-
when(dt) {
883+
for((dt, name, addr) in sub.asmGenInfo.extraVars) {
884+
if(addr!=null)
885+
out("$name = $addr")
886+
else when(dt) {
888887
DataType.UBYTE -> out("$name .byte 0")
889888
DataType.UWORD -> out("$name .word 0")
890889
else -> throw AssemblyError("weird dt")
@@ -1069,33 +1068,40 @@ $repeatLabel lda $counterVar
10691068
}
10701069

10711070
private fun createRepeatCounterVar(dt: DataType, constIterations: Int?, stmt: RepeatLoop): String {
1072-
// TODO share counter variables between subroutines or even between repeat loops as long as they're not nested
1073-
// var parent = stmt.parent
1074-
// while(parent !is ParentSentinel) {
1075-
// if(parent is RepeatLoop)
1076-
// break
1077-
// parent = parent.parent
1078-
// }
1079-
// val isNested = parent is RepeatLoop
1080-
val counterVar = makeLabel("repeatcounter")
10811071
val asmInfo = stmt.definingSubroutine()!!.asmGenInfo
1072+
var parent = stmt.parent
1073+
while(parent !is ParentSentinel) {
1074+
if(parent is RepeatLoop)
1075+
break
1076+
parent = parent.parent
1077+
}
1078+
val isNested = parent is RepeatLoop
1079+
1080+
if(!isNested) {
1081+
// we can re-use a counter var from the subroutine if it already has one for that datatype
1082+
val existingVar = asmInfo.extraVars.firstOrNull { it.first==dt }
1083+
if(existingVar!=null)
1084+
return existingVar.second
1085+
}
1086+
1087+
val counterVar = makeLabel("repeatcounter")
10821088
when(dt) {
10831089
DataType.UBYTE -> {
10841090
if(constIterations!=null && constIterations>=16 && zeropage.hasByteAvailable()) {
10851091
// allocate count var on ZP
10861092
val zpAddr = zeropage.allocate(counterVar, DataType.UBYTE, stmt.position, errors)
1087-
asmInfo.extraVarsZP.add(Pair(counterVar, zpAddr))
1093+
asmInfo.extraVars.add(Triple(DataType.UBYTE, counterVar, zpAddr))
10881094
} else {
1089-
asmInfo.extraVars.add(Pair(DataType.UBYTE, counterVar))
1095+
asmInfo.extraVars.add(Triple(DataType.UBYTE, counterVar, null))
10901096
}
10911097
}
10921098
DataType.UWORD -> {
10931099
if(constIterations!=null && constIterations>=16 && zeropage.hasWordAvailable()) {
10941100
// allocate count var on ZP
10951101
val zpAddr = zeropage.allocate(counterVar, DataType.UWORD, stmt.position, errors)
1096-
asmInfo.extraVarsZP.add(Pair(counterVar, zpAddr))
1102+
asmInfo.extraVars.add(Triple(DataType.UWORD, counterVar, zpAddr))
10971103
} else {
1098-
asmInfo.extraVars.add(Pair(DataType.UWORD, counterVar))
1104+
asmInfo.extraVars.add(Triple(DataType.UWORD, counterVar, null))
10991105
}
11001106
}
11011107
else -> throw AssemblyError("invalidt dt")

compilerAst/src/prog8/ast/statements/AstStatements.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,7 @@ class AsmGenInfo {
589589
var usedFloatEvalResultVar1 = false
590590
var usedFloatEvalResultVar2 = false
591591

592-
val extraVars = mutableListOf<Pair<DataType, String>>()
593-
val extraVarsZP = mutableListOf<Pair<String, Int>>()
592+
val extraVars = mutableListOf<Triple<DataType, String, Int?>>()
594593
}
595594

596595
// the subroutine class covers both the normal user-defined subroutines,

docs/source/todo.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ TODO
55
- test all examples (including imgviewer, assembler and petaxian) before release of the new version
66

77
- simplify cx16.joystick_get2() once this cx16 rom issue is resolved: https://github.com/commanderx16/x16-rom/issues/203
8-
- improve repeat counter vars allocation in asmgen.createRepeatCounterVar()
98
- c64: make the graphics.BITMAP_ADDRESS configurable (VIC banking)
109
- get rid of all other TODO's in the code ;-)
1110

examples/test.p8

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
%import textio ; txt.*
1+
%import textio
22
%zeropage basicsafe
33

44
main {
55

66
; test program for the optimization of repeat var allocation (asmgen.createRepeatCounterVar)
77
; output must be: 60 6164 6224 12328
8-
; original program size: $046b
98

109
uword xx
1110

0 commit comments

Comments
 (0)