From 4bf4771f08d2e54e522bac6f34b6d1f7103002a5 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 23 Jan 2022 13:42:52 +0100 Subject: [PATCH] fix @requirezp in astToSource. Fix sometimes allocating zeropage variables in normal ram. --- .../codegen/target/cpu6502/codegen/AsmGen.kt | 5 +++- .../src/prog8/ast/AstToSourceTextConverter.kt | 4 +++- .../prog8/ast/expressions/AstExpressions.kt | 3 ++- .../src/prog8/compilerinterface/CallGraph.kt | 2 +- docs/source/todo.rst | 1 + examples/test.p8 | 24 +++++++------------ 6 files changed, 19 insertions(+), 20 deletions(-) diff --git a/codeGeneration/src/prog8/codegen/target/cpu6502/codegen/AsmGen.kt b/codeGeneration/src/prog8/codegen/target/cpu6502/codegen/AsmGen.kt index edfd66161..f3b74c7c5 100644 --- a/codeGeneration/src/prog8/codegen/target/cpu6502/codegen/AsmGen.kt +++ b/codeGeneration/src/prog8/codegen/target/cpu6502/codegen/AsmGen.kt @@ -494,7 +494,9 @@ class AsmGen(private val program: Program, val vars = statements.asSequence() .filterIsInstance() .filter { - it.type==VarDeclType.VAR && zeropage.allocatedZeropageVariable(it.scopedName)==null + it.type==VarDeclType.VAR + && it.zeropage!=ZeropageWish.REQUIRE_ZEROPAGE + && zeropage.allocatedZeropageVariable(it.scopedName)==null } vars.filter { it.datatype == DataType.STR && shouldActuallyOutputStringVar(it) } @@ -504,6 +506,7 @@ class AsmGen(private val program: Program, val blockname = inBlock?.name vars.filter{ it.datatype != DataType.STR }.sortedBy { it.datatype }.forEach { + require(it.zeropage!=ZeropageWish.REQUIRE_ZEROPAGE) if(!isZpVar(it.scopedName)) { if(blockname!="prog8_lib" || !it.name.startsWith("P8ZP_SCRATCH_")) // the "hooks" to the temp vars are not generated as new variables vardecl2asm(it) diff --git a/compilerAst/src/prog8/ast/AstToSourceTextConverter.kt b/compilerAst/src/prog8/ast/AstToSourceTextConverter.kt index 4adbee302..8259f3c60 100644 --- a/compilerAst/src/prog8/ast/AstToSourceTextConverter.kt +++ b/compilerAst/src/prog8/ast/AstToSourceTextConverter.kt @@ -122,7 +122,9 @@ class AstToSourceTextConverter(val output: (text: String) -> Unit, val program: if(decl.isArray) output("]") - if(decl.zeropage == ZeropageWish.REQUIRE_ZEROPAGE || decl.zeropage==ZeropageWish.PREFER_ZEROPAGE) + if(decl.zeropage == ZeropageWish.REQUIRE_ZEROPAGE) + output(" @requirezp") + else if(decl.zeropage == ZeropageWish.PREFER_ZEROPAGE) output(" @zp") if(decl.sharedWithAsm) output(" @shared") diff --git a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt index 8cf270e14..3f65ebd14 100644 --- a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt +++ b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt @@ -861,7 +861,8 @@ data class IdentifierReference(val nameInSource: List, override val posi fun targetVarDecl(program: Program): VarDecl? = targetStatement(program) as? VarDecl fun targetSubroutine(program: Program): Subroutine? = targetStatement(program) as? Subroutine - override fun equals(other: Any?) = other is IdentifierReference && other.nameInSource==nameInSource // NOTE: only compare by the name, not the position! + // TODO equality also includes position, compare nameInSource explicitly if you only want name equality + override fun equals(other: Any?) = other is IdentifierReference && other.nameInSource==nameInSource override fun hashCode() = nameInSource.hashCode() override fun linkParents(parent: Node) { diff --git a/compilerInterfaces/src/prog8/compilerinterface/CallGraph.kt b/compilerInterfaces/src/prog8/compilerinterface/CallGraph.kt index 0a04a6dde..919fda3f2 100644 --- a/compilerInterfaces/src/prog8/compilerinterface/CallGraph.kt +++ b/compilerInterfaces/src/prog8/compilerinterface/CallGraph.kt @@ -16,7 +16,7 @@ class CallGraph(private val program: Program) : IAstVisitor { val importedBy = mutableMapOf>().withDefault { setOf() } val calls = mutableMapOf>().withDefault { setOf() } val calledBy = mutableMapOf>().withDefault { setOf() } - private val allIdentifiersAndTargets = mutableMapOf, Statement>() + private val allIdentifiersAndTargets = mutableMapOf, Statement>() // note: identifier+location as key, because currently identifier equality is only on name private val allAssemblyNodes = mutableListOf() init { diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 623d5dbf1..7375d7f5b 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -38,6 +38,7 @@ Blocked by an official Commander-x16 r39 release Future Things and Ideas ^^^^^^^^^^^^^^^^^^^^^^^ +- IdentifierReference: fix equality to also include position. CallGraph can then also only store IdentifierRef instead of pair(ident, position) as keys. - Fix: don't report as recursion if code assign address of its own subroutine to something, rather than calling it - allow "xxx" * constexpr (where constexpr is not a number literal, now gives expression error not same type) - can we promise a left-to-right function call argument evaluation? without sacrificing performance diff --git a/examples/test.p8 b/examples/test.p8 index 408417803..4a8a71e39 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,22 +1,14 @@ %import textio -%import floats +%zeropage basicsafe main { - ubyte[4] @shared @requirezp arr1 = [1,2,3,4] - uword[2] @shared @requirezp arr2 = [111,222] - float[2] @shared @requirezp arr3 = [1.111, 2.222] + ; TODO why allocated not cleaned???? + ubyte[255] @shared @requirezp arr1 + uword[100] @shared @requirezp arr2 sub start() { - txt.print_ub(arr1[3]) - txt.spc() - txt.print_uw(arr2[1]) - txt.spc() - floats.print_f(arr3[1]) - repeat { - } +; txt.print_ub(arr1[3]) +; txt.spc() +; txt.print_uw(arr2[1]) +; txt.spc() } } - -sprites { - word[3] sprites_x = [111,222,333] - word[3] sprites_y = [666,777,888] -}