diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt index 277209f82..c17510705 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/BuiltinFunctionsAsmGen.kt @@ -451,8 +451,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private fun funcMemory(fcall: IFunctionCall, discardResult: Boolean, resultToStack: Boolean, resultRegister: RegisterOrPair?) { if(discardResult || fcall !is FunctionCallExpression) throw AssemblyError("should not discard result of memory allocation at $fcall") - val nameRef = fcall.args[0] as IdentifierReference - val name = (nameRef.targetVarDecl(program)!!.value as StringLiteralValue).value + val name = (fcall.args[0] as StringLiteralValue).value require(name.all { it.isLetterOrDigit() || it=='_' }) {"memory name should be a valid symbol name"} val size = (fcall.args[1] as NumericLiteralValue).number.toUInt() val align = (fcall.args[2] as NumericLiteralValue).number.toUInt() diff --git a/compiler/src/prog8/compiler/astprocessing/LiteralsToAutoVars.kt b/compiler/src/prog8/compiler/astprocessing/LiteralsToAutoVars.kt index 3ac1e05a3..6d5bfb18e 100644 --- a/compiler/src/prog8/compiler/astprocessing/LiteralsToAutoVars.kt +++ b/compiler/src/prog8/compiler/astprocessing/LiteralsToAutoVars.kt @@ -1,5 +1,6 @@ package prog8.compiler.astprocessing +import prog8.ast.IFunctionCall import prog8.ast.Node import prog8.ast.Program import prog8.ast.base.DataType @@ -20,6 +21,14 @@ internal class LiteralsToAutoVars(private val program: Program) : AstWalker() { && string.parent !is WhenChoice && (string.parent !is ContainmentCheck || string.value.length>ContainmentCheck.max_inlined_string_length)) { // replace the literal string by an identifier reference to the interned string + + val parentFunc = (string.parent as? IFunctionCall)?.target + if(parentFunc!=null) { + if(parentFunc.nameInSource.size==1 && parentFunc.nameInSource[0]=="memory") { + // memory() builtin function just uses the string as a label name + return noModifications + } + } val scopedName = program.internString(string) val identifier = IdentifierReference(scopedName, string.position) return listOf(IAstModification.ReplaceNode(string, identifier, parent)) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index ca7acf261..1d2457989 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,6 @@ TODO For next release ^^^^^^^^^^^^^^^^ -- get rid of the interned string literals from memory() calls. - (newvaralloc) UnusedCodeRemover after(decl: VarDecl): fix that vars defined in a library can also safely be removed if unused. Currently this breaks programs such as textelite (due to diskio.save().end_address ?) - check that retval_interm_* are not in the varallocation if they're not used - make it so that subroutine parameters as variables can again be allocated in ZP, if there's still space diff --git a/examples/test.p8 b/examples/test.p8 index 8e2e80176..f92a820bd 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -6,7 +6,7 @@ main { ubyte @zp mainglobal1=10 float @shared fl1 = floats.TWOPI - uword @shared m1 = memory("eee", 200, 0) + uword @shared m1 = memory("ee", 200, 0) uword [2] nullwords ubyte [2] nullbytes