fix auto var naming collisions

This commit is contained in:
Irmen de Jong 2019-08-07 22:25:57 +02:00
parent 2f0c0f6fcd
commit 2136db0e61
5 changed files with 9 additions and 14 deletions

View File

@ -113,7 +113,6 @@ private fun compileMain(args: Array<String>) {
exitProcess(1)
}
println("LAUNCH VM!@")
if (launchAstVm) {
println("\nLaunching AST-based vm...")
val vm = AstVm(compilationResult.programAst)

View File

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

View File

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

View File

@ -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<Statement>,
override lateinit var parent: Node
override val expensiveToInline
get() = statements.any { it.expensiveToInline }
private var sequenceNumber = 1
init {
name = "<anon-$sequenceNumber>" // 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) }

View File

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