diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt index 4e69dc90b..716c846f0 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt @@ -465,7 +465,7 @@ internal class ProgramAndVarsGen( private fun zeropagevars2asm(varNames: Set) { val namesLists = varNames.map { it.split('.') }.toSet() - val zpVariables = allocator.zeropageVars.filter { it.key in namesLists } + val zpVariables = allocator.zeropageVars.filter { it.key in namesLists }.toList().sortedBy { it.second.address } for ((scopedName, zpvar) in zpVariables) { if (scopedName.size == 2 && scopedName[0] == "cx16" && scopedName[1][0] == 'r' && scopedName[1][1].isDigit()) 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( private fun nonZpVariables2asm(variables: List) { asmgen.out("") asmgen.out("; non-zeropage variables") - val (stringvars, othervars) = variables.partition { it.dt==DataType.STR } + val (stringvars, othervars) = variables.sortedBy { it.name }.partition { it.dt==DataType.STR } stringvars.forEach { outputStringvar(it.name, it.onetimeInitializationStringValue!!.second, it.onetimeInitializationStringValue!!.first) } @@ -582,10 +582,10 @@ internal class ProgramAndVarsGen( } private fun memdefsAndConsts2asm(memvars: Collection, consts: Collection) { - memvars.forEach { + memvars.sortedBy { it.address }.forEach { asmgen.out(" ${it.name} = ${it.address.toHex()}") } - consts.forEach { + consts.sortedBy { it.name }.forEach { if(it.dt==DataType.FLOAT) asmgen.out(" ${it.name} = ${it.value}") else diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt index ae1e60702..9d67a2d3b 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt @@ -88,7 +88,8 @@ internal class VariableAllocator(private val symboltable: SymbolTable, // try to allocate any other interger variables into the zeropage until it is full. // TODO some form of intelligent priorization? most often used variables first? loopcounter vars first? ...? if(errors.noErrors()) { - for (variable in varsDontCare.sortedBy { it.scopedName.count { chr -> chr=='.'}}) { + val sortedList = varsDontCare.sortedWith(compareBy { it.scopedName.count { chr -> chr=='.'}}.thenBy { it.scopedName }) + for (variable in sortedList) { if(variable.dt in IntegerDatatypes) { if(zeropage.free.isEmpty()) { break @@ -124,6 +125,6 @@ internal class VariableAllocator(private val symboltable: SymbolTable, } } collect(st) - return vars + return vars.sortedWith(compareBy { it.scopedName }.thenBy { it.dt }) } }