reducing dependencies

This commit is contained in:
Irmen de Jong 2022-03-10 20:57:36 +01:00
parent 179a7a2792
commit 546a416f7e
2 changed files with 42 additions and 38 deletions

View File

@ -37,8 +37,6 @@ internal class ProgramAndVarsGen(
val allInitializers = blockVariableInitializers.asSequence().flatMap { it.value } val allInitializers = blockVariableInitializers.asSequence().flatMap { it.value }
require(allInitializers.all { it.origin==AssignmentOrigin.VARINIT }) {"all block-level assignments must be a variable initializer"} require(allInitializers.all { it.origin==AssignmentOrigin.VARINIT }) {"all block-level assignments must be a variable initializer"}
allocator.allocateZeropageVariables()
header() header()
val allBlocks = program.allBlocks val allBlocks = program.allBlocks
if(allBlocks.first().name != "main") if(allBlocks.first().name != "main")

View File

@ -17,16 +17,44 @@ internal class VariableAllocator(private val symboltable: SymbolTable,
internal val globalFloatConsts = mutableMapOf<Double, String>() // all float values in the entire program (value -> varname) internal val globalFloatConsts = mutableMapOf<Double, String>() // all float values in the entire program (value -> varname)
internal val zeropageVars: Map<List<String>, Zeropage.ZpAllocation> = zeropage.allocatedVariables internal val zeropageVars: Map<List<String>, Zeropage.ZpAllocation> = zeropage.allocatedVariables
init {
allocateZeropageVariables()
}
internal fun getMemorySlab(name: String) = memorySlabsInternal[name] internal fun getMemorySlab(name: String) = memorySlabsInternal[name]
internal fun allocateMemorySlab(name: String, size: UInt, align: UInt) { internal fun allocateMemorySlab(name: String, size: UInt, align: UInt) {
memorySlabsInternal[name] = Pair(size, align) memorySlabsInternal[name] = Pair(size, align)
} }
internal fun isZpVar(scopedName: List<String>) = 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. * Allocate variables into the Zeropage.
* The result should be retrieved from the current machine's zeropage object! * The result should be retrieved from the current machine's zeropage object!
*/ */
internal fun allocateZeropageVariables() { private fun allocateZeropageVariables() {
if(options.zeropage== ZeropageType.DONTUSE) if(options.zeropage== ZeropageType.DONTUSE)
return return
@ -118,8 +146,6 @@ internal class VariableAllocator(private val symboltable: SymbolTable,
return vars return vars
} }
internal fun isZpVar(scopedName: List<String>) = scopedName in zeropage.allocatedVariables
private fun numArrayElements(variable: StStaticVariable) = private fun numArrayElements(variable: StStaticVariable) =
when(variable.dt) { when(variable.dt) {
DataType.STR -> variable.initialStringValue!!.first.length+1 // 1 extra because of 0 termination char DataType.STR -> variable.initialStringValue!!.first.length+1 // 1 extra because of 0 termination char
@ -127,30 +153,9 @@ internal class VariableAllocator(private val symboltable: SymbolTable,
else -> null 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
val newName = "prog8_float_const_${globalFloatConsts.size}"
globalFloatConsts[number] = newName
return newName
}
}
/** /**
* This class contains various attributes that influence the assembly code generator. * Cntains various attributes that influence the assembly code generator.
* Conceptually it should be part of any INameScope. * Conceptually it should be part of any INameScope.
* But because the resulting code only creates "real" scopes on a subroutine level, * 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. * it's more consistent to only define these attributes on a Subroutine node.
@ -164,3 +169,4 @@ internal class SubroutineExtraAsmInfo {
val extraVars = mutableListOf<Triple<DataType, String, UInt?>>() val extraVars = mutableListOf<Triple<DataType, String, UInt?>>()
} }
}