variables extraction moved to the very end, so no need anymore to change the table after the fact

This commit is contained in:
Irmen de Jong 2022-03-04 23:08:05 +01:00
parent 1d740c7c36
commit 859ab36347
8 changed files with 8 additions and 50 deletions

View File

@ -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.

View File

@ -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)

View File

@ -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<IAstModification>()
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))

View File

@ -104,7 +104,7 @@ internal class VariablesAndConsts (
override val subroutineConsts: Map<Subroutine, Set<IVariablesAndConsts.ConstantNumberSymbol>>
override val subroutineMemvars: Map<Subroutine, Set<IVariablesAndConsts.MemoryMappedVariable>>
private val bv = astBlockVars.keys.associateWith { mutableSetOf<IVariablesAndConsts.StaticVariable>() }.toMutableMap()
private val bv = astBlockVars.keys.associateWith { mutableSetOf<IVariablesAndConsts.StaticVariable>() }
private val bc = astBlockConsts.keys.associateWith { mutableSetOf<IVariablesAndConsts.ConstantNumberSymbol>() }
private val bmv = astBlockMemvars.keys.associateWith { mutableSetOf<IVariablesAndConsts.MemoryMappedVariable>() }
private val sv = astSubroutineVars.keys.associateWith { mutableSetOf<IVariablesAndConsts.StaticVariable>() }
@ -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))
}
}

View File

@ -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

View File

@ -77,10 +77,6 @@ class TestAsmGenSymbols: StringSpec({
override val subroutineConsts: Map<Subroutine, Set<IVariablesAndConsts.ConstantNumberSymbol>>
override val subroutineMemvars: Map<Subroutine, Set<IVariablesAndConsts.MemoryMappedVariable>>
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))

View File

@ -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<Block, Set<IVariablesAndConsts.StaticVariable>>
get() = throw NotImplementedError("dummy")
override val blockConsts: Map<Block, Set<IVariablesAndConsts.ConstantNumberSymbol>>
get() = throw NotImplementedError("dummy")
override val blockMemvars: Map<Block, Set<IVariablesAndConsts.MemoryMappedVariable>>
get() = throw NotImplementedError("dummy")
override val subroutineVars: Map<Subroutine, Set<IVariablesAndConsts.StaticVariable>>
get() = throw NotImplementedError("dummy")
override val subroutineConsts: Map<Subroutine, Set<IVariablesAndConsts.ConstantNumberSymbol>>
get() = throw NotImplementedError("dummy")
override val subroutineMemvars: Map<Subroutine, Set<IVariablesAndConsts.MemoryMappedVariable>>
get() = throw NotImplementedError("dummy")
override fun addIfUnknown(definingBlock: Block, variable: VarDecl) {
throw NotImplementedError("dummy")
}
}

View File

@ -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<Subroutine, Set<StaticVariable>>
val subroutineConsts: Map<Subroutine, Set<ConstantNumberSymbol>>
val subroutineMemvars: Map<Subroutine, Set<MemoryMappedVariable>>
/**
* ability to add another new variable after the tables have already been created.
*/
fun addIfUnknown(definingBlock: Block, variable: VarDecl)
}