From 6da1f7eb4ce28abe4a60ee295f3422aa6016d69d Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 22 Oct 2024 21:17:02 +0200 Subject: [PATCH] don't remove essential subroutines even though they seem unused --- .../src/prog8/optimizer/UnusedCodeRemover.kt | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/codeOptimizers/src/prog8/optimizer/UnusedCodeRemover.kt b/codeOptimizers/src/prog8/optimizer/UnusedCodeRemover.kt index 23fa3ff23..a5182fb02 100644 --- a/codeOptimizers/src/prog8/optimizer/UnusedCodeRemover.kt +++ b/codeOptimizers/src/prog8/optimizer/UnusedCodeRemover.kt @@ -21,6 +21,31 @@ class UnusedCodeRemover(private val program: Program, ): AstWalker() { private lateinit var callgraph: CallGraph + private val neverRemoveSubroutines = mutableListOf() + + init { + neverRemoveSubroutines.add(program.entrypoint) + + program.allBlocks.singleOrNull { it.name=="sys" } ?.let { + val subroutines = it.statements.filterIsInstance() + val push = subroutines.single { it.name == "push" } + val pushw = subroutines.single { it.name == "pushw" } + val pop = subroutines.single { it.name == "pop" } + val popw = subroutines.single { it.name == "popw" } + neverRemoveSubroutines.add(push) + neverRemoveSubroutines.add(pushw) + neverRemoveSubroutines.add(pop) + neverRemoveSubroutines.add(popw) + } + + program.allBlocks.singleOrNull { it.name=="floats" } ?.let { + val subroutines = it.statements.filterIsInstance() + val push = subroutines.single { it.name == "push" } + val pop = subroutines.single { it.name == "pop" } + neverRemoveSubroutines.add(push) + neverRemoveSubroutines.add(pop) + } + } override fun before(program: Program): Iterable { callgraph = CallGraph(program) @@ -92,7 +117,7 @@ class UnusedCodeRemover(private val program: Program, override fun after(subroutine: Subroutine, parent: Node): Iterable { val forceOutput = "force_output" in subroutine.definingBlock.options() - if (subroutine !== program.entrypoint && !forceOutput && !subroutine.isAsmSubroutine) { + if (subroutine !in neverRemoveSubroutines && !forceOutput && !subroutine.isAsmSubroutine) { if(callgraph.unused(subroutine)) { if(subroutine.containsNoCodeNorVars) { if("ignore_unused" !in subroutine.definingBlock.options())