diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt index 710f3cd9b..671064e5b 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/VariableAllocator.kt @@ -6,7 +6,6 @@ import prog8.ast.base.ArrayDatatypes import prog8.ast.base.DataType import prog8.ast.base.IntegerDatatypes import prog8.ast.statements.Subroutine -import prog8.ast.statements.ZeropageWish import prog8.compilerinterface.* @@ -37,10 +36,10 @@ internal class VariableAllocator(private val symboltable: SymbolTable, val allVariables = collectAllVariables(symboltable) val numberOfAllocatableVariables = allVariables.size - val varsRequiringZp = allVariables.filter { it.zpw == ZeropageWish.REQUIRE_ZEROPAGE } - val varsPreferringZp = allVariables.filter { it.zpw == ZeropageWish.PREFER_ZEROPAGE } - val varsDontCare = allVariables.filter { it.zpw == ZeropageWish.DONTCARE } - val numberOfExplicitNonZpVariables = allVariables.count { it.zpw == ZeropageWish.NOT_IN_ZEROPAGE } + val varsRequiringZp = allVariables.filter { it.zpw == StZeropageWish.REQUIRE_ZEROPAGE } + val varsPreferringZp = allVariables.filter { it.zpw == StZeropageWish.PREFER_ZEROPAGE } + val varsDontCare = allVariables.filter { it.zpw == StZeropageWish.DONTCARE } + val numberOfExplicitNonZpVariables = allVariables.count { it.zpw == StZeropageWish.NOT_IN_ZEROPAGE } require(varsDontCare.size + varsRequiringZp.size + varsPreferringZp.size + numberOfExplicitNonZpVariables == numberOfAllocatableVariables) var numVariablesAllocatedInZP = 0 diff --git a/codeGenTargets/src/prog8/codegen/target/AtariTarget.kt b/codeGenTargets/src/prog8/codegen/target/AtariTarget.kt index 8c3027be7..4ac153723 100644 --- a/codeGenTargets/src/prog8/codegen/target/AtariTarget.kt +++ b/codeGenTargets/src/prog8/codegen/target/AtariTarget.kt @@ -1,11 +1,12 @@ package prog8.codegen.target -import prog8.ast.base.* +import prog8.ast.base.ByteDatatypes +import prog8.ast.base.DataType +import prog8.ast.base.PassByReferenceDatatypes +import prog8.ast.base.WordDatatypes import prog8.ast.expressions.Expression -import prog8.ast.expressions.StringLiteral import prog8.ast.statements.RegisterOrStatusflag import prog8.ast.statements.Subroutine -import prog8.ast.statements.VarDecl import prog8.codegen.target.atari.AtariMachineDefinition import prog8.codegen.target.cbm.asmsub6502ArgsEvalOrder import prog8.codegen.target.cbm.asmsub6502ArgsHaveRegisterClobberRisk @@ -38,18 +39,4 @@ class AtariTarget: ICompilationTarget, IStringEncoding by Encoder, IMemSizer { else -> Int.MIN_VALUE } } - - override fun memorySize(decl: VarDecl): Int { - return when(decl.type) { - VarDeclType.CONST -> 0 - VarDeclType.VAR, VarDeclType.MEMORY -> { - when(val dt = decl.datatype) { - in NumericDatatypes -> return memorySize(dt) - in ArrayDatatypes -> decl.arraysize!!.constIndex()!! * memorySize(ArrayToElementTypes.getValue(dt)) - DataType.STR -> (decl.value as StringLiteral).value.length + 1 - else -> 0 - } - } - } - } } diff --git a/codeGenTargets/src/prog8/codegen/target/cbm/CbmMemorySizer.kt b/codeGenTargets/src/prog8/codegen/target/cbm/CbmMemorySizer.kt index 538685cf4..34b958333 100644 --- a/codeGenTargets/src/prog8/codegen/target/cbm/CbmMemorySizer.kt +++ b/codeGenTargets/src/prog8/codegen/target/cbm/CbmMemorySizer.kt @@ -1,8 +1,9 @@ package prog8.codegen.target.cbm -import prog8.ast.base.* -import prog8.ast.expressions.StringLiteral -import prog8.ast.statements.VarDecl +import prog8.ast.base.ByteDatatypes +import prog8.ast.base.DataType +import prog8.ast.base.PassByReferenceDatatypes +import prog8.ast.base.WordDatatypes import prog8.compilerinterface.IMemSizer internal object CbmMemorySizer: IMemSizer { @@ -14,18 +15,4 @@ internal object CbmMemorySizer: IMemSizer { else -> Int.MIN_VALUE } } - - override fun memorySize(decl: VarDecl): Int { - return when(decl.type) { - VarDeclType.CONST -> 0 - VarDeclType.VAR, VarDeclType.MEMORY -> { - when(val dt = decl.datatype) { - in NumericDatatypes -> return memorySize(dt) - in ArrayDatatypes -> decl.arraysize!!.constIndex()!! * memorySize(ArrayToElementTypes.getValue(dt)) - DataType.STR -> (decl.value as StringLiteral).value.length + 1 - else -> 0 - } - } - } - } } \ No newline at end of file diff --git a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt index 461b02789..726368479 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt @@ -644,7 +644,7 @@ internal class AstChecker(private val program: Program, } } - if(compilerOptions.zeropage==ZeropageType.DONTUSE && decl.zeropage==ZeropageWish.REQUIRE_ZEROPAGE) + if(compilerOptions.zeropage==ZeropageType.DONTUSE && decl.zeropage == ZeropageWish.REQUIRE_ZEROPAGE) err("zeropage usage has been disabled by options") super.visit(decl) diff --git a/compiler/src/prog8/compiler/astprocessing/SymbolTableMaker.kt b/compiler/src/prog8/compiler/astprocessing/SymbolTableMaker.kt index b286bc32e..ceec4e1f2 100644 --- a/compiler/src/prog8/compiler/astprocessing/SymbolTableMaker.kt +++ b/compiler/src/prog8/compiler/astprocessing/SymbolTableMaker.kt @@ -5,10 +5,7 @@ import prog8.ast.base.FatalAstException import prog8.ast.base.Position import prog8.ast.base.VarDeclType import prog8.ast.expressions.* -import prog8.ast.statements.Block -import prog8.ast.statements.Label -import prog8.ast.statements.Subroutine -import prog8.ast.statements.VarDecl +import prog8.ast.statements.* import prog8.ast.walk.IAstVisitor import prog8.compilerinterface.* import java.util.* @@ -56,7 +53,7 @@ internal class SymbolTableMaker: IAstVisitor { val initialString = if(initialStringLit==null) null else Pair(initialStringLit.value, initialStringLit.encoding) val initialArrayLit = decl.value as? ArrayLiteral val initialArray = makeInitialArray(initialArrayLit) - StStaticVariable(decl.name, decl.datatype, initialNumeric, initialString, initialArray, decl.arraysize?.constIndex(), decl.zeropage, decl.position) + StStaticVariable(decl.name, decl.datatype, initialNumeric, initialString, initialArray, decl.arraysize?.constIndex(), map(decl.zeropage), decl.position) } VarDeclType.CONST -> StConstant(decl.name, decl.datatype, (decl.value as NumericLiteral).number, decl.position) VarDeclType.MEMORY -> StMemVar(decl.name, decl.datatype, (decl.value as NumericLiteral).number.toUInt(), decl.position) @@ -65,6 +62,13 @@ internal class SymbolTableMaker: IAstVisitor { st.origAstLinks[decl] = node } + private fun map(zpw: ZeropageWish): StZeropageWish = when(zpw) { + ZeropageWish.REQUIRE_ZEROPAGE -> StZeropageWish.REQUIRE_ZEROPAGE + ZeropageWish.PREFER_ZEROPAGE -> StZeropageWish.PREFER_ZEROPAGE + ZeropageWish.DONTCARE -> StZeropageWish.DONTCARE + ZeropageWish.NOT_IN_ZEROPAGE -> StZeropageWish.NOT_IN_ZEROPAGE + } + private fun makeInitialArray(arrayLit: ArrayLiteral?): StArray? { if(arrayLit==null) return null diff --git a/compiler/test/TestSymbolTable.kt b/compiler/test/TestSymbolTable.kt index fdafc1ffd..03dfde751 100644 --- a/compiler/test/TestSymbolTable.kt +++ b/compiler/test/TestSymbolTable.kt @@ -6,7 +6,6 @@ import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldNotBe import prog8.ast.base.DataType import prog8.ast.base.Position -import prog8.ast.statements.ZeropageWish import prog8.compilerinterface.* class TestSymbolTable: FunSpec({ @@ -71,10 +70,10 @@ private fun makeSt(): SymbolTable { block1.add(sub12) block1.add(StConstant("c1", DataType.UWORD, 12345.0, Position.DUMMY)) block1.add(StConstant("blockc", DataType.UWORD, 999.0, Position.DUMMY)) - sub11.add(StStaticVariable("v1", DataType.BYTE, null, null, null, null, ZeropageWish.DONTCARE, Position.DUMMY)) - sub11.add(StStaticVariable("v2", DataType.BYTE, null, null, null, null, ZeropageWish.DONTCARE, Position.DUMMY)) - sub12.add(StStaticVariable("v1", DataType.BYTE, null, null, null, null, ZeropageWish.DONTCARE, Position.DUMMY)) - sub12.add(StStaticVariable("v2", DataType.BYTE, null, null, null, null, ZeropageWish.DONTCARE, Position.DUMMY)) + sub11.add(StStaticVariable("v1", DataType.BYTE, null, null, null, null, StZeropageWish.DONTCARE, Position.DUMMY)) + sub11.add(StStaticVariable("v2", DataType.BYTE, null, null, null, null, StZeropageWish.DONTCARE, Position.DUMMY)) + sub12.add(StStaticVariable("v1", DataType.BYTE, null, null, null, null, StZeropageWish.DONTCARE, Position.DUMMY)) + sub12.add(StStaticVariable("v2", DataType.BYTE, null, null, null, null, StZeropageWish.DONTCARE, Position.DUMMY)) val block2 = StNode("block2", StNodeType.BLOCK, Position.DUMMY) val sub21 = StNode("sub1", StNodeType.SUBROUTINE, Position.DUMMY) diff --git a/compiler/test/helpers/Dummies.kt b/compiler/test/helpers/Dummies.kt index 676ff9c4e..c7472e3dd 100644 --- a/compiler/test/helpers/Dummies.kt +++ b/compiler/test/helpers/Dummies.kt @@ -8,7 +8,6 @@ import prog8.ast.expressions.InferredTypes import prog8.ast.expressions.NumericLiteral import prog8.ast.statements.RegisterOrStatusflag import prog8.ast.statements.Subroutine -import prog8.ast.statements.VarDecl import prog8.compilerinterface.* internal object DummyFunctions : IBuiltinFunctions { @@ -25,7 +24,6 @@ internal object DummyFunctions : IBuiltinFunctions { internal object DummyMemsizer : IMemSizer { override fun memorySize(dt: DataType) = 0 - override fun memorySize(decl: VarDecl) = 0 } internal object DummyStringEncoder : IStringEncoding { @@ -73,8 +71,4 @@ internal object DummyCompilationTarget : ICompilationTarget { override fun memorySize(dt: DataType): Int { throw NotImplementedError("dummy") } - - override fun memorySize(decl: VarDecl): Int { - throw NotImplementedError("dummy") - } } diff --git a/compilerAst/src/prog8/compilerinterface/IMemSizer.kt b/compilerAst/src/prog8/compilerinterface/IMemSizer.kt index 710bf4e08..7a385d1f2 100644 --- a/compilerAst/src/prog8/compilerinterface/IMemSizer.kt +++ b/compilerAst/src/prog8/compilerinterface/IMemSizer.kt @@ -1,7 +1,6 @@ package prog8.compilerinterface import prog8.ast.base.DataType -import prog8.ast.statements.VarDecl // note: this is a separate interface in the compilerAst module because @@ -9,5 +8,4 @@ import prog8.ast.statements.VarDecl interface IMemSizer { fun memorySize(dt: DataType): Int - fun memorySize(decl: VarDecl): Int } diff --git a/compilerInterfaces/src/prog8/compilerinterface/SymbolTable.kt b/compilerInterfaces/src/prog8/compilerinterface/SymbolTable.kt index 5f389e0cd..efd1e17cf 100644 --- a/compilerInterfaces/src/prog8/compilerinterface/SymbolTable.kt +++ b/compilerInterfaces/src/prog8/compilerinterface/SymbolTable.kt @@ -3,7 +3,6 @@ package prog8.compilerinterface import prog8.ast.Node import prog8.ast.base.DataType import prog8.ast.base.Position -import prog8.ast.statements.ZeropageWish /** @@ -31,6 +30,16 @@ enum class StNodeType { BUILTINFUNC } +// TODO assumption: replicating this here (from the Ast namespace) allows us later to have 0 dependencies on the original Ast nodes/namespace +// same for DataType? or should those things be moved into a separate module 'elementary' that contains all shared enums and classes such as Position? +enum class StZeropageWish { + REQUIRE_ZEROPAGE, + PREFER_ZEROPAGE, + DONTCARE, + NOT_IN_ZEROPAGE +} + + open class StNode(val name: String, val type: StNodeType, val position: Position, @@ -125,7 +134,7 @@ class StStaticVariable(name: String, val initialStringValue: StString?, val initialArrayValue: StArray?, val arraysize: Int?, - val zpw: ZeropageWish, + val zpw: StZeropageWish, position: Position) : StNode(name, StNodeType.STATICVAR, position) { init {