diff --git a/compiler/src/prog8/ast/AST.kt b/compiler/src/prog8/ast/AST.kt index 416d31684..af188c938 100644 --- a/compiler/src/prog8/ast/AST.kt +++ b/compiler/src/prog8/ast/AST.kt @@ -1695,6 +1695,7 @@ class Subroutine(override val name: String, val isAsmSubroutine: Boolean, override var statements: MutableList, override val position: Position) : IStatement, INameScope { + var keepAlways: Boolean = false override lateinit var parent: Node val calledBy = mutableSetOf() val calls = mutableSetOf() diff --git a/compiler/src/prog8/ast/StmtReorderer.kt b/compiler/src/prog8/ast/StmtReorderer.kt index d0732f744..5b6d0c71e 100644 --- a/compiler/src/prog8/ast/StmtReorderer.kt +++ b/compiler/src/prog8/ast/StmtReorderer.kt @@ -110,6 +110,7 @@ private class StatementReorderer(private val program: Program): IAstProcessor { val statements = varInits.map{it.value}.toMutableList() val varInitSub = Subroutine(initvarsSubName, emptyList(), emptyList(), emptyList(), emptyList(), emptySet(), null, false, statements, block.position) + varInitSub.keepAlways = true varInitSub.linkParents(block) block.statements.add(varInitSub) diff --git a/compiler/src/prog8/optimizing/StatementOptimizer.kt b/compiler/src/prog8/optimizing/StatementOptimizer.kt index 180fd05c3..00727c678 100644 --- a/compiler/src/prog8/optimizing/StatementOptimizer.kt +++ b/compiler/src/prog8/optimizing/StatementOptimizer.kt @@ -7,9 +7,6 @@ import kotlin.math.floor /* - TODO FIX THE OPTIMIZER: RESULTS IN WRONG CODE FOR THE primes.p8 EXAMPLE - - todo: subroutines with 1 or 2 byte args or 1 word arg can be converted to asm sub calling convention (args in registers) todo: implement usage counters for variables (locals and heap), blocks. Remove if count is zero. @@ -40,13 +37,15 @@ class StatementOptimizer(private val program: Program) : IAstProcessor { val entrypoint = program.entrypoint() program.modules.forEach { callgraph.forAllSubroutines(it) { sub -> - if(sub !== entrypoint && (sub.calledBy.isEmpty() || (sub.containsNoCodeNorVars() && !sub.isAsmSubroutine))) + if(sub !== entrypoint && !sub.keepAlways && (sub.calledBy.isEmpty() || (sub.containsNoCodeNorVars() && !sub.isAsmSubroutine))) removeSubroutines.add(sub) } } if(removeSubroutines.isNotEmpty()) { - removeSubroutines.forEach { it.definingScope().statements.remove(it) } + removeSubroutines.forEach { + it.definingScope().statements.remove(it) + } } val removeBlocks = mutableSetOf() diff --git a/examples/primes.p8 b/examples/primes.p8 index 51e40c1b6..f89672ed3 100644 --- a/examples/primes.p8 +++ b/examples/primes.p8 @@ -3,9 +3,6 @@ ~ main { - ; TODO FIX THE COMPILER OPTIMIZER ; RESULTS IN WRONG CODE FOR THIS PROGRAM - - ubyte[256] sieve ubyte candidate_prime = 2 ; is increased in the loop