Skip to content

Commit 549c598

Browse files
committed
variables sorted in asm
1 parent 7b59bc8 commit 549c598

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ internal class ProgramAndVarsGen(
465465

466466
private fun zeropagevars2asm(varNames: Set<String>) {
467467
val namesLists = varNames.map { it.split('.') }.toSet()
468-
val zpVariables = allocator.zeropageVars.filter { it.key in namesLists }
468+
val zpVariables = allocator.zeropageVars.filter { it.key in namesLists }.toList().sortedBy { it.second.address }
469469
for ((scopedName, zpvar) in zpVariables) {
470470
if (scopedName.size == 2 && scopedName[0] == "cx16" && scopedName[1][0] == 'r' && scopedName[1][1].isDigit())
471471
continue // The 16 virtual registers of the cx16 are not actual variables in zp, they're memory mapped
@@ -476,7 +476,7 @@ internal class ProgramAndVarsGen(
476476
private fun nonZpVariables2asm(variables: List<StStaticVariable>) {
477477
asmgen.out("")
478478
asmgen.out("; non-zeropage variables")
479-
val (stringvars, othervars) = variables.partition { it.dt==DataType.STR }
479+
val (stringvars, othervars) = variables.sortedBy { it.name }.partition { it.dt==DataType.STR }
480480
stringvars.forEach {
481481
outputStringvar(it.name, it.onetimeInitializationStringValue!!.second, it.onetimeInitializationStringValue!!.first)
482482
}
@@ -582,10 +582,10 @@ internal class ProgramAndVarsGen(
582582
}
583583

584584
private fun memdefsAndConsts2asm(memvars: Collection<StMemVar>, consts: Collection<StConstant>) {
585-
memvars.forEach {
585+
memvars.sortedBy { it.address }.forEach {
586586
asmgen.out(" ${it.name} = ${it.address.toHex()}")
587587
}
588-
consts.forEach {
588+
consts.sortedBy { it.name }.forEach {
589589
if(it.dt==DataType.FLOAT)
590590
asmgen.out(" ${it.name} = ${it.value}")
591591
else

codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ internal class VariableAllocator(private val symboltable: SymbolTable,
8888
// try to allocate any other interger variables into the zeropage until it is full.
8989
// TODO some form of intelligent priorization? most often used variables first? loopcounter vars first? ...?
9090
if(errors.noErrors()) {
91-
for (variable in varsDontCare.sortedBy { it.scopedName.count { chr -> chr=='.'}}) {
91+
val sortedList = varsDontCare.sortedWith(compareBy<StStaticVariable> { it.scopedName.count { chr -> chr=='.'}}.thenBy { it.scopedName })
92+
for (variable in sortedList) {
9293
if(variable.dt in IntegerDatatypes) {
9394
if(zeropage.free.isEmpty()) {
9495
break
@@ -124,6 +125,6 @@ internal class VariableAllocator(private val symboltable: SymbolTable,
124125
}
125126
}
126127
collect(st)
127-
return vars
128+
return vars.sortedWith(compareBy<StStaticVariable> { it.scopedName }.thenBy { it.dt })
128129
}
129130
}

0 commit comments

Comments
 (0)