mirror of
https://github.com/irmen/prog8.git
synced 2025-02-03 11:32:41 +00:00
ir: fix unused code remover
This commit is contained in:
parent
e094785cbd
commit
3603140114
@ -2,8 +2,7 @@ package prog8.codegen.intermediate
|
|||||||
|
|
||||||
import prog8.code.core.IErrorReporter
|
import prog8.code.core.IErrorReporter
|
||||||
import prog8.code.core.SourceCode.Companion.libraryFilePrefix
|
import prog8.code.core.SourceCode.Companion.libraryFilePrefix
|
||||||
import prog8.intermediate.IRCodeChunkBase
|
import prog8.intermediate.*
|
||||||
import prog8.intermediate.IRProgram
|
|
||||||
|
|
||||||
|
|
||||||
internal class IRUnusedCodeRemover(private val irprog: IRProgram, private val errors: IErrorReporter) {
|
internal class IRUnusedCodeRemover(private val irprog: IRProgram, private val errors: IErrorReporter) {
|
||||||
@ -40,7 +39,7 @@ internal class IRUnusedCodeRemover(private val irprog: IRProgram, private val er
|
|||||||
val new = mutableSetOf<IRCodeChunkBase>()
|
val new = mutableSetOf<IRCodeChunkBase>()
|
||||||
reachable.forEach {
|
reachable.forEach {
|
||||||
it.next?.let { next -> new += next }
|
it.next?.let { next -> new += next }
|
||||||
it.instructions.forEach { it.branchTarget?.let { target -> new += target} }
|
it.instructions.forEach { instr -> instr.branchTarget?.let { target -> new += target} }
|
||||||
}
|
}
|
||||||
reachable += new
|
reachable += new
|
||||||
}
|
}
|
||||||
@ -76,16 +75,24 @@ internal class IRUnusedCodeRemover(private val irprog: IRProgram, private val er
|
|||||||
): Int {
|
): Int {
|
||||||
var numRemoved = 0
|
var numRemoved = 0
|
||||||
irprog.blocks.asSequence().flatMap { it.subroutines }.forEach { sub ->
|
irprog.blocks.asSequence().flatMap { it.subroutines }.forEach { sub ->
|
||||||
sub.chunks.reversed().forEach { chunk ->
|
sub.chunks.withIndex().reversed().forEach { (index, chunk) ->
|
||||||
if (chunk !in linkedChunks) {
|
if (chunk !in linkedChunks) {
|
||||||
if (chunk === sub.chunks[0]) {
|
if (chunk === sub.chunks[0]) {
|
||||||
if (chunk.instructions.isNotEmpty()) {
|
when(chunk) {
|
||||||
// don't remove the first chunk of the sub itself because it has to have the name of the sub as label
|
is IRCodeChunk -> {
|
||||||
chunk.instructions.clear()
|
if (chunk.isNotEmpty()) {
|
||||||
numRemoved++
|
// don't remove the first chunk of the sub itself because it has to have the name of the sub as label
|
||||||
|
chunk.instructions.clear()
|
||||||
|
numRemoved++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is IRInlineAsmChunk, is IRInlineBinaryChunk -> {
|
||||||
|
sub.chunks[index] = IRCodeChunk(chunk.label, chunk.next)
|
||||||
|
numRemoved++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
sub.chunks.remove(chunk)
|
sub.chunks.removeAt(index)
|
||||||
numRemoved++
|
numRemoved++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user