diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt index 6cf701e7c..923109edc 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt @@ -231,11 +231,11 @@ class AsmGen(internal val program: Program, } CpuRegister.X -> { out(" stx prog8_regsaveX") - allocator.subroutineExtra(scope).usedRegsaveX = true + subroutineExtra(scope).usedRegsaveX = true } CpuRegister.Y -> { out(" sty prog8_regsaveY") - allocator.subroutineExtra(scope).usedRegsaveY = true + subroutineExtra(scope).usedRegsaveY = true } } } @@ -727,7 +727,7 @@ $repeatLabel lda $counterVar private fun createRepeatCounterVar(dt: DataType, preferZeropage: Boolean, stmt: RepeatLoop): String { val scope = stmt.definingSubroutine!! - val asmInfo = allocator.subroutineExtra(scope) + val asmInfo = subroutineExtra(scope) var parent = stmt.parent while(parent !is ParentSentinel) { if(parent is RepeatLoop) @@ -1448,7 +1448,7 @@ $repeatLabel lda $counterVar beq $jumpIfFalseLabel""") } else { val subroutine = left.definingSubroutine!! - allocator.subroutineExtra(subroutine).usedFloatEvalResultVar1 = true + subroutineExtra(subroutine).usedFloatEvalResultVar1 = true assignExpressionToVariable(right, subroutineFloatEvalResultVar1, DataType.FLOAT, subroutine) assignExpressionToRegister(left, RegisterOrPair.FAC1) out(""" @@ -1493,7 +1493,7 @@ $repeatLabel lda $counterVar beq $jumpIfFalseLabel""") } else { val subroutine = left.definingSubroutine!! - allocator.subroutineExtra(subroutine).usedFloatEvalResultVar1 = true + subroutineExtra(subroutine).usedFloatEvalResultVar1 = true assignExpressionToVariable(right, subroutineFloatEvalResultVar1, DataType.FLOAT, subroutine) assignExpressionToRegister(left, RegisterOrPair.FAC1) out(""" @@ -1538,7 +1538,7 @@ $repeatLabel lda $counterVar beq $jumpIfFalseLabel""") } else { val subroutine = left.definingSubroutine!! - allocator.subroutineExtra(subroutine).usedFloatEvalResultVar1 = true + subroutineExtra(subroutine).usedFloatEvalResultVar1 = true assignExpressionToVariable(right, subroutineFloatEvalResultVar1, DataType.FLOAT, subroutine) assignExpressionToRegister(left, RegisterOrPair.FAC1) out(""" @@ -1583,7 +1583,7 @@ $repeatLabel lda $counterVar beq $jumpIfFalseLabel""") } else { val subroutine = left.definingSubroutine!! - allocator.subroutineExtra(subroutine).usedFloatEvalResultVar1 = true + subroutineExtra(subroutine).usedFloatEvalResultVar1 = true assignExpressionToVariable(right, subroutineFloatEvalResultVar1, DataType.FLOAT, subroutine) assignExpressionToRegister(left, RegisterOrPair.FAC1) out(""" @@ -2523,7 +2523,7 @@ $repeatLabel lda $counterVar beq $jumpIfFalseLabel""") } else { val subroutine = left.definingSubroutine!! - allocator.subroutineExtra(subroutine).usedFloatEvalResultVar1 = true + subroutineExtra(subroutine).usedFloatEvalResultVar1 = true assignExpressionToVariable(right, subroutineFloatEvalResultVar1, DataType.FLOAT, subroutine) assignExpressionToRegister(left, RegisterOrPair.FAC1) out(""" @@ -2608,7 +2608,7 @@ $repeatLabel lda $counterVar bne $jumpIfFalseLabel""") } else { val subroutine = left.definingSubroutine!! - allocator.subroutineExtra(subroutine).usedFloatEvalResultVar1 = true + subroutineExtra(subroutine).usedFloatEvalResultVar1 = true assignExpressionToVariable(right, subroutineFloatEvalResultVar1, DataType.FLOAT, subroutine) assignExpressionToRegister(left, RegisterOrPair.FAC1) out(""" @@ -3039,4 +3039,34 @@ $repeatLabel lda $counterVar out(" pha | tya | pha") } } + + private val subroutineExtrasCache = mutableMapOf() + + internal fun subroutineExtra(sub: Subroutine): SubroutineExtraAsmInfo { + var extra = subroutineExtrasCache[sub] + return if(extra==null) { + extra = SubroutineExtraAsmInfo() + subroutineExtrasCache[sub] = extra + extra + } + else + extra + } } + + +/** + * 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>() +} \ No newline at end of file diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt index 36f71ebc3..853e47b1f 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt @@ -1622,7 +1622,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, if(scope==null) throw AssemblyError("cannot use float arguments outside of a subroutine scope") - allocations.subroutineExtra(scope).usedFloatEvalResultVar2 = true + asmgen.subroutineExtra(scope).usedFloatEvalResultVar2 = true val variable = IdentifierReference(listOf(subroutineFloatEvalResultVar2), value.position) val addr = AddressOf(variable, value.position) addr.linkParents(value) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt index 6bd4e3002..88b6eec76 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt @@ -331,7 +331,7 @@ internal class ProgramAndVarsGen( } asmgen.out("; variables") - val asmGenInfo = allocator.subroutineExtra(sub) + val asmGenInfo = asmgen.subroutineExtra(sub) for((dt, name, addr) in asmGenInfo.extraVars) { if(addr!=null) asmgen.out("$name = $addr") diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt index 89a9630ee..c1798a6d0 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt @@ -2,7 +2,6 @@ package prog8.codegen.cpu6502 import com.github.michaelbull.result.fold import com.github.michaelbull.result.onSuccess -import prog8.ast.statements.Subroutine import prog8.compilerinterface.* @@ -11,7 +10,6 @@ internal class VariableAllocator(private val symboltable: SymbolTable, private val errors: IErrorReporter) { private val zeropage = options.compTarget.machine.zeropage - private val subroutineExtras = mutableMapOf() private val memorySlabsInternal = mutableMapOf>() internal val memorySlabs: Map> = memorySlabsInternal internal val globalFloatConsts = mutableMapOf() // all float values in the entire program (value -> varname) @@ -29,17 +27,6 @@ internal class VariableAllocator(private val symboltable: SymbolTable, 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) @@ -152,21 +139,4 @@ internal class VariableAllocator(private val symboltable: SymbolTable, in ArrayDatatypes -> variable.arraysize!! else -> null } - - - /** - * 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 extraVars = mutableListOf>() - } } diff --git a/compiler/src/prog8/compiler/astprocessing/SymbolTableMaker.kt b/compiler/src/prog8/compiler/astprocessing/SymbolTableMaker.kt index c1e22b9e8..f1c7bcb9d 100644 --- a/compiler/src/prog8/compiler/astprocessing/SymbolTableMaker.kt +++ b/compiler/src/prog8/compiler/astprocessing/SymbolTableMaker.kt @@ -30,7 +30,7 @@ internal class SymbolTableMaker: IAstVisitor { super.visit(block) scopestack.pop() st.add(node) - st.origAstLinks[block] = node + // st.origAstLinks[block] = node } override fun visit(subroutine: Subroutine) { @@ -39,7 +39,7 @@ internal class SymbolTableMaker: IAstVisitor { super.visit(subroutine) scopestack.pop() scopestack.peek().add(node) - st.origAstLinks[subroutine] = node + // st.origAstLinks[subroutine] = node } override fun visit(decl: VarDecl) { @@ -57,7 +57,7 @@ internal class SymbolTableMaker: IAstVisitor { VarDeclType.MEMORY -> StMemVar(decl.name, decl.datatype, (decl.value as NumericLiteral).number.toUInt(), decl.position) } scopestack.peek().add(node) - st.origAstLinks[decl] = node + // st.origAstLinks[decl] = node } private fun map(zpw: ZeropageWish): StZeropageWish = when(zpw) { @@ -83,6 +83,6 @@ internal class SymbolTableMaker: IAstVisitor { override fun visit(label: Label) { val node = StNode(label.name, StNodeType.LABEL, label.position) scopestack.peek().add(node) - st.origAstLinks[label] = node + // st.origAstLinks[label] = node } } diff --git a/compilerInterfaces/src/prog8/compilerinterface/SymbolTable.kt b/compilerInterfaces/src/prog8/compilerinterface/SymbolTable.kt index 38a4a39dd..1649f18ce 100644 --- a/compilerInterfaces/src/prog8/compilerinterface/SymbolTable.kt +++ b/compilerInterfaces/src/prog8/compilerinterface/SymbolTable.kt @@ -1,7 +1,5 @@ package prog8.compilerinterface -import prog8.ast.Node - /** * Tree structure containing all symbol definitions in the program @@ -11,8 +9,6 @@ class SymbolTable : StNode("", StNodeType.GLOBAL, Position.DUMMY) { fun print() = printIndented(0) override fun printProperties() { } - - val origAstLinks = mutableMapOf() // links of the original Ast nodes to the symbol table node. }