From 4aba0c740543114b196b2cb2a7301b97ec43428f Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 29 Apr 2021 00:48:16 +0200 Subject: [PATCH] unused variables are removed more aggressively (no longer checking asm blocks for their names) --- compiler/src/prog8/compiler/Compiler.kt | 2 +- compiler/src/prog8/optimizer/CallGraph.kt | 2 +- compiler/src/prog8/optimizer/UnusedCodeRemover.kt | 14 ++++++++------ docs/source/todo.rst | 1 - examples/test.p8 | 6 ------ 5 files changed, 10 insertions(+), 15 deletions(-) diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index 8aa9275ae..d133dc3d7 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -317,7 +317,7 @@ private fun writeAssembly(programAst: Program, programAst.processAstBeforeAsmGeneration(errors, compilerOptions.compTarget) errors.report() - printAst(programAst) + // printAst(programAst) compilerOptions.compTarget.machine.initializeZeropage(compilerOptions) val assembly = asmGeneratorFor(compilerOptions.compTarget, diff --git a/compiler/src/prog8/optimizer/CallGraph.kt b/compiler/src/prog8/optimizer/CallGraph.kt index 1e670a104..58b71c242 100644 --- a/compiler/src/prog8/optimizer/CallGraph.kt +++ b/compiler/src/prog8/optimizer/CallGraph.kt @@ -182,7 +182,7 @@ class CallGraph(private val program: Program) : IAstVisitor { return false val allReferencedVardecls = allIdentifiersAndTargets.filter { it.value is VarDecl }.map { it.value }.toSet() - return decl !in allReferencedVardecls && !nameInAssemblyCode(decl.name) + return decl !in allReferencedVardecls // Don't check assembly just for occurrences of variables, if they're not used in prog8 itself, just kill them } private fun nameInAssemblyCode(name: String) = allAssemblyNodes.any { it.assembly.contains(name) } diff --git a/compiler/src/prog8/optimizer/UnusedCodeRemover.kt b/compiler/src/prog8/optimizer/UnusedCodeRemover.kt index 8a365cbb5..bee3d85bc 100644 --- a/compiler/src/prog8/optimizer/UnusedCodeRemover.kt +++ b/compiler/src/prog8/optimizer/UnusedCodeRemover.kt @@ -104,12 +104,14 @@ internal class UnusedCodeRemover(private val program: Program, } override fun after(decl: VarDecl, parent: Node): Iterable { - val forceOutput = "force_output" in decl.definingBlock().options() - if(!forceOutput && !decl.autogeneratedDontRemove && callgraph.unused(decl)) { - if(decl.type == VarDeclType.VAR && !decl.definingBlock().isInLibrary) - errors.warn("removing unused variable '${decl.name}'", decl.position) - - return listOf(IAstModification.Remove(decl, decl.definingScope())) + if(decl.type==VarDeclType.VAR) { + val forceOutput = "force_output" in decl.definingBlock().options() + if (!forceOutput && !decl.autogeneratedDontRemove && !decl.definingBlock().isInLibrary) { + if (callgraph.unused(decl)) { + errors.warn("removing unused variable '${decl.name}'", decl.position) + return listOf(IAstModification.Remove(decl, decl.definingScope())) + } + } } return noModifications diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 95b87878f..42e9f395a 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,7 +2,6 @@ TODO ==== -- fix problem that unused vars are no longer properly removed - BeforeAsmGenerationAstChanger no longer needs to move vardecls?? - simplify cx16.joystick_get2() once this cx16 rom issue is resolved: https://github.com/commanderx16/x16-rom/issues/203 diff --git a/examples/test.p8 b/examples/test.p8 index 223d2415e..55b2e47c5 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -3,12 +3,6 @@ main { sub start() { - ; TODO WHY AREN'T UNUSED VARS REMOVED ANY LONGER??? - - ubyte xx1 - ubyte yy1=22 - ubyte zz1=33 - repeat { ubyte xx ubyte yy=xx