diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt index a1e43cf2d..6bd4e3002 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt @@ -37,8 +37,6 @@ internal class ProgramAndVarsGen( val allInitializers = blockVariableInitializers.asSequence().flatMap { it.value } require(allInitializers.all { it.origin==AssignmentOrigin.VARINIT }) {"all block-level assignments must be a variable initializer"} - allocator.allocateZeropageVariables() - header() val allBlocks = program.allBlocks if(allBlocks.first().name != "main") diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt index 23c0f2243..89a9630ee 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt @@ -17,16 +17,44 @@ internal class VariableAllocator(private val symboltable: SymbolTable, internal val globalFloatConsts = mutableMapOf() // all float values in the entire program (value -> varname) internal val zeropageVars: Map, Zeropage.ZpAllocation> = zeropage.allocatedVariables + init { + allocateZeropageVariables() + } + internal fun getMemorySlab(name: String) = memorySlabsInternal[name] + internal fun allocateMemorySlab(name: String, size: UInt, align: UInt) { memorySlabsInternal[name] = Pair(size, align) } + internal fun isZpVar(scopedName: List) = scopedName in zeropage.allocatedVariables + + internal fun subroutineExtra(sub: Subroutine): SubroutineExtraAsmInfo { + var extra = subroutineExtras[sub] + return if(extra==null) { + extra = SubroutineExtraAsmInfo() + subroutineExtras[sub] = extra + extra + } + else + extra + } + + internal fun getFloatAsmConst(number: Double): String { + val asmName = globalFloatConsts[number] + if(asmName!=null) + return asmName + + val newName = "prog8_float_const_${globalFloatConsts.size}" + globalFloatConsts[number] = newName + return newName + } + /** * Allocate variables into the Zeropage. * The result should be retrieved from the current machine's zeropage object! */ - internal fun allocateZeropageVariables() { + private fun allocateZeropageVariables() { if(options.zeropage== ZeropageType.DONTUSE) return @@ -118,8 +146,6 @@ internal class VariableAllocator(private val symboltable: SymbolTable, return vars } - internal fun isZpVar(scopedName: List) = scopedName in zeropage.allocatedVariables - private fun numArrayElements(variable: StStaticVariable) = when(variable.dt) { DataType.STR -> variable.initialStringValue!!.first.length+1 // 1 extra because of 0 termination char @@ -127,40 +153,20 @@ internal class VariableAllocator(private val symboltable: SymbolTable, else -> null } - internal fun subroutineExtra(sub: Subroutine): SubroutineExtraAsmInfo { - var extra = subroutineExtras[sub] - return if(extra==null) { - extra = SubroutineExtraAsmInfo() - subroutineExtras[sub] = extra - extra - } - else - extra - } - internal fun getFloatAsmConst(number: Double): String { - val asmName = globalFloatConsts[number] - if(asmName!=null) - return asmName + /** + * Cntains various attributes that influence the assembly code generator. + * Conceptually it should be part of any INameScope. + * But because the resulting code only creates "real" scopes on a subroutine level, + * it's more consistent to only define these attributes on a Subroutine node. + */ + internal class SubroutineExtraAsmInfo { + var usedRegsaveA = false + var usedRegsaveX = false + var usedRegsaveY = false + var usedFloatEvalResultVar1 = false + var usedFloatEvalResultVar2 = false - val newName = "prog8_float_const_${globalFloatConsts.size}" - globalFloatConsts[number] = newName - return newName + val extraVars = mutableListOf>() } } - -/** - * This class contains various attributes that influence the assembly code generator. - * Conceptually it should be part of any INameScope. - * But because the resulting code only creates "real" scopes on a subroutine level, - * it's more consistent to only define these attributes on a Subroutine node. - */ -internal class SubroutineExtraAsmInfo { - var usedRegsaveA = false - var usedRegsaveX = false - var usedRegsaveY = false - var usedFloatEvalResultVar1 = false - var usedFloatEvalResultVar2 = false - - val extraVars = mutableListOf>() -}