diff --git a/codeGeneration/src/prog8/codegen/target/cpu6502/codegen/AsmGen.kt b/codeGeneration/src/prog8/codegen/target/cpu6502/codegen/AsmGen.kt index a1deb9d9d..d82e3a42a 100644 --- a/codeGeneration/src/prog8/codegen/target/cpu6502/codegen/AsmGen.kt +++ b/codeGeneration/src/prog8/codegen/target/cpu6502/codegen/AsmGen.kt @@ -341,8 +341,9 @@ class AsmGen(private val program: Program, } } DataType.STR -> { - val str = decl.value as StringLiteralValue - outputStringvar(decl, compTarget.encodeString(str.value, str.altEncoding).plus(0.toUByte())) + throw AssemblyError("all string vars should have been interned") +// val str = decl.value as StringLiteralValue +// outputStringvar(decl, compTarget.encodeString(str.value, str.altEncoding).plus(0.toUByte())) } DataType.ARRAY_UB -> { val data = makeArrayFillDataUnsigned(decl) @@ -435,7 +436,7 @@ class AsmGen(private val program: Program, val vars = statements.filterIsInstance().filter { it.type==VarDeclType.VAR } val encodedstringVars = vars - .filter {it.datatype == DataType.STR } + .filter { it.datatype == DataType.STR && shouldActuallyOutputStringVar(it) } .map { val str = it.value as StringLiteralValue it to compTarget.encodeString(str.value, str.altEncoding).plus(0.toUByte()) @@ -456,6 +457,17 @@ class AsmGen(private val program: Program, } } + private fun shouldActuallyOutputStringVar(strvar: VarDecl): Boolean { + if(strvar.sharedWithAsm) + return true + val uses = callGraph.usages(strvar) + val onlyInMemoryFuncs = uses.all { + val builtinfunc = (it.parent as? IFunctionCall)?.target?.targetStatement(program) as? BuiltinFunctionPlaceholder + builtinfunc?.name=="memory" + } + return !onlyInMemoryFuncs + } + private fun outputStringvar(strdecl: VarDecl, bytes: List) { val sv = strdecl.value as StringLiteralValue val altEncoding = if(sv.altEncoding) "@" else "" diff --git a/compiler/test/codegeneration/TestVariables.kt b/compiler/test/codegeneration/TestVariables.kt index 953600c54..0ca0c5767 100644 --- a/compiler/test/codegeneration/TestVariables.kt +++ b/compiler/test/codegeneration/TestVariables.kt @@ -15,13 +15,13 @@ class TestVariables: FunSpec({ val text = """ main { sub start() { - ubyte[] @shared array = [1,2,3,4] - str @shared name = "test" + ubyte[] @shared arrayvar = [1,2,3,4] + str @shared stringvar = "test" ubyte @shared bytevar = 0 %asm {{ - lda array - lda name + lda arrayvar + lda stringvar lda bytevar }} } diff --git a/compilerInterfaces/src/prog8/compilerinterface/CallGraph.kt b/compilerInterfaces/src/prog8/compilerinterface/CallGraph.kt index 0659a7201..05adfe6b4 100644 --- a/compilerInterfaces/src/prog8/compilerinterface/CallGraph.kt +++ b/compilerInterfaces/src/prog8/compilerinterface/CallGraph.kt @@ -189,7 +189,7 @@ class CallGraph(private val program: Program) : IAstVisitor { } fun usages(decl: VarDecl): List { - if(decl.type!=VarDeclType.VAR || decl.autogeneratedDontRemove || decl.sharedWithAsm) + if(decl.type!=VarDeclType.VAR) return emptyList() if(decl.definingBlock !in usedBlocks) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index a0033a6b0..9a57c17c7 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,6 @@ TODO For next compiler release (7.6) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- fix that memory("name", ...) allocates a STR variable with contents "name" in the assembly source - make it possible to inline non-asmsub routines that just contain a single statement (return, functioncall, assignment) ... diff --git a/examples/test.p8 b/examples/test.p8 index 6b31e6c1b..9d9290b2c 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -8,6 +8,10 @@ main { sub start() { txt.print_uwhex(buffer, true) txt.print_uwhex(buffer2, true) + txt.print_uwhex("zzzz", true) + str xyz = "irmen" + xyz="2342344234" + txt.print_uwhex(xyz, true) } }