variables init subroutine must never be optimized away (fixes primes example)

This commit is contained in:
Irmen de Jong 2019-06-21 23:56:45 +02:00
parent a86852874f
commit 560047adee
4 changed files with 6 additions and 8 deletions

View File

@ -1695,6 +1695,7 @@ class Subroutine(override val name: String,
val isAsmSubroutine: Boolean,
override var statements: MutableList<IStatement>,
override val position: Position) : IStatement, INameScope {
var keepAlways: Boolean = false
override lateinit var parent: Node
val calledBy = mutableSetOf<INameScope>()
val calls = mutableSetOf<Subroutine>()

View File

@ -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)

View File

@ -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<Block>()

View File

@ -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