From 2136db0e612336f5d94cfc9fc5faa07846e2fc6b Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 7 Aug 2019 22:25:57 +0200 Subject: [PATCH] fix auto var naming collisions --- compiler/src/prog8/CompilerMain.kt | 1 - compiler/src/prog8/ast/base/Extensions.kt | 4 ---- .../src/prog8/ast/processing/AstIdentifiersChecker.kt | 2 ++ compiler/src/prog8/ast/statements/AstStatements.kt | 11 ++++++----- compiler/src/prog8/optimizer/StatementOptimizer.kt | 5 +---- 5 files changed, 9 insertions(+), 14 deletions(-) diff --git a/compiler/src/prog8/CompilerMain.kt b/compiler/src/prog8/CompilerMain.kt index 0dec273a1..9851d8ffa 100644 --- a/compiler/src/prog8/CompilerMain.kt +++ b/compiler/src/prog8/CompilerMain.kt @@ -113,7 +113,6 @@ private fun compileMain(args: Array) { exitProcess(1) } - println("LAUNCH VM!@") if (launchAstVm) { println("\nLaunching AST-based vm...") val vm = AstVm(compilationResult.programAst) diff --git a/compiler/src/prog8/ast/base/Extensions.kt b/compiler/src/prog8/ast/base/Extensions.kt index bb186c8fe..eb758a62b 100644 --- a/compiler/src/prog8/ast/base/Extensions.kt +++ b/compiler/src/prog8/ast/base/Extensions.kt @@ -12,10 +12,6 @@ import prog8.optimizer.FlattenAnonymousScopesAndRemoveNops internal const val initvarsSubName="prog8_init_vars" -// prefix for literal values that are turned into a variable on the heap -internal const val autoHeapValuePrefix = "auto_heap_value_" - - internal fun Program.removeNopsFlattenAnonScopes() { val flattener = FlattenAnonymousScopesAndRemoveNops() flattener.visit(this) diff --git a/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt b/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt index eeaced4a5..c20ab9d9d 100644 --- a/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt +++ b/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt @@ -301,9 +301,11 @@ internal class AstIdentifiersChecker(private val program: Program) : IAstModifyi private fun makeIdentifierFromRefLv(refLiteral: ReferenceLiteralValue): IdentifierReference { // a referencetype literal value that's not declared as a variable // we need to introduce an auto-generated variable for this to be able to refer to the value + // note: if the var references the same literal value, it is not yet de-duplicated here. refLiteral.addToHeap(program.heap) val scope = refLiteral.definingScope() var variable = VarDecl.createAuto(refLiteral, program.heap) + val existing = scope.lookup(listOf(variable.name), refLiteral) variable = addVarDecl(scope, variable) // replace the reference literal by a identifier reference val identifier = IdentifierReference(listOf(variable.name), variable.position) diff --git a/compiler/src/prog8/ast/statements/AstStatements.kt b/compiler/src/prog8/ast/statements/AstStatements.kt index f6149f384..c4d0cc1ca 100644 --- a/compiler/src/prog8/ast/statements/AstStatements.kt +++ b/compiler/src/prog8/ast/statements/AstStatements.kt @@ -192,12 +192,16 @@ class VarDecl(val type: VarDeclType, override val expensiveToInline get() = value!=null && value !is NumericLiteralValue + // prefix for literal values that are turned into a variable on the heap + companion object { + private var autoHeapValueSequenceNumber = 0 + fun createAuto(refLv: ReferenceLiteralValue, heap: HeapValues): VarDecl { if(refLv.heapId==null) throw FatalAstException("can only create autovar for a ref lv that has a heapid $refLv") - val autoVarName = "$autoHeapValuePrefix${refLv.heapId}" + val autoVarName = "auto_heap_value_${++autoHeapValueSequenceNumber}" return if(refLv.isArray) { val declaredType = ArrayElementTypes.getValue(refLv.type) val arraysize = ArrayIndex.forArray(refLv, heap) @@ -523,16 +527,13 @@ class AnonymousScope(override var statements: MutableList, override lateinit var parent: Node override val expensiveToInline get() = statements.any { it.expensiveToInline } + private var sequenceNumber = 1 init { name = "" // make sure it's an invalid soruce code identifier so user source code can never produce it sequenceNumber++ } - companion object { - private var sequenceNumber = 1 - } - override fun linkParents(parent: Node) { this.parent = parent statements.forEach { it.linkParents(this) } diff --git a/compiler/src/prog8/optimizer/StatementOptimizer.kt b/compiler/src/prog8/optimizer/StatementOptimizer.kt index 0a5b0f9ab..61f11a229 100644 --- a/compiler/src/prog8/optimizer/StatementOptimizer.kt +++ b/compiler/src/prog8/optimizer/StatementOptimizer.kt @@ -23,10 +23,7 @@ internal class StatementOptimizer(private val program: Program, private val opti private val pureBuiltinFunctions = BuiltinFunctions.filter { it.value.pure } private val callgraph = CallGraph(program) - - companion object { - private var generatedLabelSequenceNumber = 0 - } + private var generatedLabelSequenceNumber = 0 override fun visit(program: Program) { removeUnusedCode(callgraph)