mirror of
https://github.com/irmen/prog8.git
synced 2025-02-16 22:30:46 +00:00
fix auto var naming collisions
This commit is contained in:
parent
2f0c0f6fcd
commit
2136db0e61
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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) }
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user