From 859ab36347e1f34a405ff4d4151c1fdb9ab25db2 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 4 Mar 2022 23:08:05 +0100 Subject: [PATCH] variables extraction moved to the very end, so no need anymore to change the table after the fact --- compiler/src/prog8/compiler/Compiler.kt | 4 ++-- .../compiler/astprocessing/AstExtensions.kt | 4 ++-- .../astprocessing/BeforeAsmAstChanger.kt | 7 ++----- .../astprocessing/VariablesAndConsts.kt | 10 +-------- compiler/test/TestOptimization.kt | 2 +- .../test/codegeneration/TestAsmGenSymbols.kt | 4 ---- compiler/test/helpers/Dummies.kt | 21 ------------------- .../compilerinterface/IVariablesAndConsts.kt | 6 ------ 8 files changed, 8 insertions(+), 50 deletions(-) diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index c53f22d2d..e212c6243 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -361,9 +361,9 @@ private fun writeAssembly(program: Program, compilerOptions: CompilationOptions ): WriteAssemblyResult { compilerOptions.compTarget.machine.initializeZeropage(compilerOptions) - val variables = VariableExtractor().extractVars(program) - program.processAstBeforeAsmGeneration(compilerOptions, variables, errors) + program.processAstBeforeAsmGeneration(compilerOptions, errors) errors.report() + val variables = VariableExtractor().extractVars(program) // TODO make removing all VarDecls work, but this needs inferType to be able to get its information from somewhere else as the VarDecl nodes in the Ast, // or don't use inferType at all anymore and "bake the type information" into the Ast somehow. diff --git a/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt b/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt index 4784923a1..b129130ed 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt @@ -21,8 +21,8 @@ internal fun Program.checkValid(errors: IErrorReporter, compilerOptions: Compila checker.visit(this) } -internal fun Program.processAstBeforeAsmGeneration(compilerOptions: CompilationOptions, variables: IVariablesAndConsts, errors: IErrorReporter) { - val fixer = BeforeAsmAstChanger(this, compilerOptions, variables, errors) +internal fun Program.processAstBeforeAsmGeneration(compilerOptions: CompilationOptions, errors: IErrorReporter) { + val fixer = BeforeAsmAstChanger(this, compilerOptions, errors) fixer.visit(this) while(errors.noErrors() && fixer.applyModifications()>0) { fixer.visit(this) diff --git a/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt b/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt index 267e73897..41babc278 100644 --- a/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt +++ b/compiler/src/prog8/compiler/astprocessing/BeforeAsmAstChanger.kt @@ -11,7 +11,6 @@ import prog8.compilerinterface.* internal class BeforeAsmAstChanger(val program: Program, private val options: CompilationOptions, - private val variables: IVariablesAndConsts, private val errors: IErrorReporter ) : AstWalker() { @@ -250,8 +249,7 @@ internal class BeforeAsmAstChanger(val program: Program, ) } if(separateRightExpr) { - val (tempVarName, tempVarDecl) = program.getTempVar(rightDt.getOrElse { throw FatalAstException("invalid dt") }, true) - variables.addIfUnknown(tempVarDecl.definingBlock, tempVarDecl) + val (tempVarName, _) = program.getTempVar(rightDt.getOrElse { throw FatalAstException("invalid dt") }, true) rightOperandReplacement = IdentifierReference(tempVarName, expr.position) rightAssignment = Assignment( AssignTarget(IdentifierReference(tempVarName, expr.position), null, null, expr.position), @@ -323,8 +321,7 @@ internal class BeforeAsmAstChanger(val program: Program, val modifications = mutableListOf() val statement = expr.containingStatement val dt = expr.indexer.indexExpr.inferType(program) - val (tempVarName, tempVarDecl) = program.getTempVar(dt.getOrElse { throw FatalAstException("invalid dt") }) - variables.addIfUnknown(tempVarDecl.definingBlock, tempVarDecl) + val (tempVarName, _) = program.getTempVar(dt.getOrElse { throw FatalAstException("invalid dt") }) val target = AssignTarget(IdentifierReference(tempVarName, expr.indexer.position), null, null, expr.indexer.position) val assign = Assignment(target, expr.indexer.indexExpr, AssignmentOrigin.BEFOREASMGEN, expr.indexer.position) modifications.add(IAstModification.InsertBefore(statement, assign, statement.parent as IStatementContainer)) diff --git a/compiler/src/prog8/compiler/astprocessing/VariablesAndConsts.kt b/compiler/src/prog8/compiler/astprocessing/VariablesAndConsts.kt index c29db036f..773bd4da5 100644 --- a/compiler/src/prog8/compiler/astprocessing/VariablesAndConsts.kt +++ b/compiler/src/prog8/compiler/astprocessing/VariablesAndConsts.kt @@ -104,7 +104,7 @@ internal class VariablesAndConsts ( override val subroutineConsts: Map> override val subroutineMemvars: Map> - private val bv = astBlockVars.keys.associateWith { mutableSetOf() }.toMutableMap() + private val bv = astBlockVars.keys.associateWith { mutableSetOf() } private val bc = astBlockConsts.keys.associateWith { mutableSetOf() } private val bmv = astBlockMemvars.keys.associateWith { mutableSetOf() } private val sv = astSubroutineVars.keys.associateWith { mutableSetOf() } @@ -180,12 +180,4 @@ internal class VariablesAndConsts ( private fun toStatic(decl: VarDecl) = IVariablesAndConsts.StaticVariable(decl.datatype, decl.scopedName, decl.value, decl.arraysize?.constIndex(), decl.zeropage, decl.position) - override fun addIfUnknown(definingBlock: Block, variable: VarDecl) { - var blockvars = bv[definingBlock] - if(blockvars==null) { - blockvars = mutableSetOf() - bv[definingBlock] = blockvars - } - blockvars.add(toStatic(variable)) - } } diff --git a/compiler/test/TestOptimization.kt b/compiler/test/TestOptimization.kt index a8b199cb8..bf6b8a166 100644 --- a/compiler/test/TestOptimization.kt +++ b/compiler/test/TestOptimization.kt @@ -303,7 +303,7 @@ class TestOptimization: FunSpec({ expr.inferType(result.program).getOrElse { fail("dt") } shouldBe DataType.UBYTE val options = CompilationOptions(OutputType.PRG, CbmPrgLauncherType.BASIC, ZeropageType.DONTUSE, emptyList(), false, true, C64Target(), outputDir= outputDir) - result.program.processAstBeforeAsmGeneration(options, DummyVarsAndConsts, ErrorReporterForTests()) + result.program.processAstBeforeAsmGeneration(options, ErrorReporterForTests()) // assignment is now split into: // bb = not bb diff --git a/compiler/test/codegeneration/TestAsmGenSymbols.kt b/compiler/test/codegeneration/TestAsmGenSymbols.kt index 6a1c8d9dd..1e2c0e736 100644 --- a/compiler/test/codegeneration/TestAsmGenSymbols.kt +++ b/compiler/test/codegeneration/TestAsmGenSymbols.kt @@ -77,10 +77,6 @@ class TestAsmGenSymbols: StringSpec({ override val subroutineConsts: Map> override val subroutineMemvars: Map> - override fun addIfUnknown(definingBlock: Block, variable: VarDecl) { - throw NotImplementedError("dummy") - } - init { blockVars = mutableMapOf() blockVars[block] = mutableSetOf(IVariablesAndConsts.StaticVariable(varInBlock.datatype, varInBlock.scopedName, varInBlock.value, varInBlock.arraysize?.constIndex(), varInBlock.zeropage, varInBlock.position)) diff --git a/compiler/test/helpers/Dummies.kt b/compiler/test/helpers/Dummies.kt index 40a84997c..676ff9c4e 100644 --- a/compiler/test/helpers/Dummies.kt +++ b/compiler/test/helpers/Dummies.kt @@ -6,7 +6,6 @@ import prog8.ast.base.Position import prog8.ast.expressions.Expression import prog8.ast.expressions.InferredTypes import prog8.ast.expressions.NumericLiteral -import prog8.ast.statements.Block import prog8.ast.statements.RegisterOrStatusflag import prog8.ast.statements.Subroutine import prog8.ast.statements.VarDecl @@ -79,23 +78,3 @@ internal object DummyCompilationTarget : ICompilationTarget { throw NotImplementedError("dummy") } } - -internal object DummyVarsAndConsts : IVariablesAndConsts { - override val blockVars: Map> - get() = throw NotImplementedError("dummy") - override val blockConsts: Map> - get() = throw NotImplementedError("dummy") - override val blockMemvars: Map> - get() = throw NotImplementedError("dummy") - override val subroutineVars: Map> - get() = throw NotImplementedError("dummy") - override val subroutineConsts: Map> - get() = throw NotImplementedError("dummy") - override val subroutineMemvars: Map> - get() = throw NotImplementedError("dummy") - - override fun addIfUnknown(definingBlock: Block, variable: VarDecl) { - throw NotImplementedError("dummy") - } - -} \ No newline at end of file diff --git a/compilerInterfaces/src/prog8/compilerinterface/IVariablesAndConsts.kt b/compilerInterfaces/src/prog8/compilerinterface/IVariablesAndConsts.kt index c4b5b64ee..34bd4fb54 100644 --- a/compilerInterfaces/src/prog8/compilerinterface/IVariablesAndConsts.kt +++ b/compilerInterfaces/src/prog8/compilerinterface/IVariablesAndConsts.kt @@ -5,7 +5,6 @@ import prog8.ast.base.Position import prog8.ast.expressions.Expression import prog8.ast.statements.Block import prog8.ast.statements.Subroutine -import prog8.ast.statements.VarDecl import prog8.ast.statements.ZeropageWish /** @@ -30,9 +29,4 @@ interface IVariablesAndConsts { val subroutineVars: Map> val subroutineConsts: Map> val subroutineMemvars: Map> - - /** - * ability to add another new variable after the tables have already been created. - */ - fun addIfUnknown(definingBlock: Block, variable: VarDecl) }