diff --git a/compiler/src/prog8/ast/expressions/AstExpressions.kt b/compiler/src/prog8/ast/expressions/AstExpressions.kt index 89d4d7dc2..9f2200adf 100644 --- a/compiler/src/prog8/ast/expressions/AstExpressions.kt +++ b/compiler/src/prog8/ast/expressions/AstExpressions.kt @@ -407,10 +407,14 @@ class StructLiteralValue(var values: List, } } +private var heapIdSequence = 0 // unique ids for strings and arrays "on the heap" + class StringLiteralValue(val value: String, override val position: Position) : Expression() { override lateinit var parent: Node + val heapId = ++heapIdSequence + override fun linkParents(parent: Node) { this.parent = parent } @@ -427,19 +431,8 @@ class StringLiteralValue(val value: String, return false return value==other.value } - - var heapId: Int? = null - private set - - fun addToHeap() { - if(heapId==null) - heapId = ++heapIdSequence - } } -private var heapIdSequence = 0 - - class ArrayLiteralValue(val type: DataType, // only array types val value: Array, initHeapId: Int? =null, @@ -648,7 +641,7 @@ data class IdentifierReference(val nameInSource: List, override val posi val value = (node as? VarDecl)?.value ?: throw FatalAstException("requires a reference value") return when (value) { is IdentifierReference -> value.heapId(namespace) - is StringLiteralValue -> value.heapId ?: throw FatalAstException("string is not on the heap: $value") + is StringLiteralValue -> value.heapId is ArrayLiteralValue -> value.heapId ?: throw FatalAstException("array is not on the heap: $value") else -> throw FatalAstException("requires a reference value") } diff --git a/compiler/src/prog8/ast/processing/AstChecker.kt b/compiler/src/prog8/ast/processing/AstChecker.kt index ee1f334a5..a8d546086 100644 --- a/compiler/src/prog8/ast/processing/AstChecker.kt +++ b/compiler/src/prog8/ast/processing/AstChecker.kt @@ -702,8 +702,6 @@ internal class AstChecker(private val program: Program, override fun visit(string: StringLiteralValue) { checkValueTypeAndRangeString(DataType.STR, string) super.visit(string) - if(string.heapId==null) - throw FatalAstException("string should have been moved to heap at ${string.position}") } override fun visit(expr: PrefixExpression) { diff --git a/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt b/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt index 5e0876d24..f7567eddf 100644 --- a/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt +++ b/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt @@ -253,9 +253,6 @@ internal class AstIdentifiersChecker(private val program: Program) : IAstModifyi // intern the string; move it into the heap if (string.value.length !in 1..255) checkResult.add(ExpressionError("string literal length must be between 1 and 255", string.position)) - else { - string.addToHeap() - } return if (vardecl != null) string else @@ -292,7 +289,6 @@ internal class AstIdentifiersChecker(private val program: Program) : IAstModifyi // 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. - string.addToHeap() val scope = string.definingScope() val variable = VarDecl.createAuto(string) return replaceWithIdentifier(variable, scope, string.parent) diff --git a/compiler/src/prog8/ast/statements/AstStatements.kt b/compiler/src/prog8/ast/statements/AstStatements.kt index 49c0ed953..48963d4d0 100644 --- a/compiler/src/prog8/ast/statements/AstStatements.kt +++ b/compiler/src/prog8/ast/statements/AstStatements.kt @@ -192,8 +192,6 @@ class VarDecl(val type: VarDeclType, private var autoHeapValueSequenceNumber = 0 fun createAuto(string: StringLiteralValue): VarDecl { - if(string.heapId==null) - throw FatalAstException("can only create autovar for a string that has a heapid $string") val autoVarName = "auto_heap_value_${++autoHeapValueSequenceNumber}" return VarDecl(VarDeclType.VAR, DataType.STR, ZeropageWish.NOT_IN_ZEROPAGE, null, autoVarName, null, string, isArray = false, autogeneratedDontRemove = true, position = string.position) diff --git a/compiler/src/prog8/optimizer/ConstantFolding.kt b/compiler/src/prog8/optimizer/ConstantFolding.kt index c8e07fab2..e991bee42 100644 --- a/compiler/src/prog8/optimizer/ConstantFolding.kt +++ b/compiler/src/prog8/optimizer/ConstantFolding.kt @@ -146,19 +146,6 @@ class ConstantFolding(private val program: Program) : IAstModifyingVisitor { } } } - DataType.STR -> { - TODO("$decl") -// if(functionCall.target.nameInSource == listOf("c64scr")) { -// val parent = functionCall.parent -// if(parent is VarDecl && parent.datatype==DataType.STR && functionCall.args.size == 1) { -// val str = functionCall.args[0] as IdentifierReference -// if(str!=null) { -// val decl = str.targetVarDecl(functionCall.definingScope()) -// TODO("$decl") -// } -// } -// } - } else -> { // nothing to do for this type // this includes strings and structs diff --git a/compiler/src/prog8/vm/RuntimeValue.kt b/compiler/src/prog8/vm/RuntimeValue.kt index bdbce0912..b7098af23 100644 --- a/compiler/src/prog8/vm/RuntimeValue.kt +++ b/compiler/src/prog8/vm/RuntimeValue.kt @@ -573,7 +573,7 @@ class RuntimeValueNumeric(type: DataType, num: Number): RuntimeValueBase(type) { class RuntimeValueString(val str: String, val heapId: Int?): RuntimeValueBase(DataType.STR) { companion object { fun fromLv(string: StringLiteralValue): RuntimeValueString { - return RuntimeValueString(string.value, string.heapId!!) + return RuntimeValueString(string.value, string.heapId) } }