diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt index 4d9c431a9..a5bccb256 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/IRCodeGen.kt @@ -58,7 +58,7 @@ class IRCodeGen( val optimizer = IRPeepholeOptimizer(irProg) optimizer.optimize() - val remover = IRUnusedCodeRemover(irProg, errors) + val remover = IRUnusedCodeRemover(irProg, irSymbolTable, errors) do { val numRemoved = remover.optimize() } while(numRemoved>0 && errors.noErrors()) @@ -115,10 +115,10 @@ class IRCodeGen( block.children.firstOrNull { it is IRInlineAsmChunk }?.let { first-> first as IRInlineAsmChunk if(first.label==null) { - val replacement = IRInlineAsmChunk(block.name, first.assembly, first.isIR, first.next) + val replacement = IRInlineAsmChunk(block.label, first.assembly, first.isIR, first.next) block.children.removeAt(0) block.children.add(0, replacement) - } else if(first.label != block.name) { + } else if(first.label != block.label) { throw AssemblyError("first chunk in block has label that differs from block name") } } diff --git a/codeGenIntermediate/src/prog8/codegen/intermediate/IRUnusedCodeRemover.kt b/codeGenIntermediate/src/prog8/codegen/intermediate/IRUnusedCodeRemover.kt index cadd5c128..affbeb016 100644 --- a/codeGenIntermediate/src/prog8/codegen/intermediate/IRUnusedCodeRemover.kt +++ b/codeGenIntermediate/src/prog8/codegen/intermediate/IRUnusedCodeRemover.kt @@ -5,7 +5,11 @@ import prog8.code.core.SourceCode.Companion.libraryFilePrefix import prog8.intermediate.* -internal class IRUnusedCodeRemover(private val irprog: IRProgram, private val errors: IErrorReporter) { +internal class IRUnusedCodeRemover( + private val irprog: IRProgram, + private val symbolTable: IRSymbolTable, + private val errors: IErrorReporter +) { fun optimize(): Int { val allLabeledChunks = mutableMapOf() @@ -25,6 +29,7 @@ internal class IRUnusedCodeRemover(private val irprog: IRProgram, private val er errors.warn("unused subroutine ${sub.label}", sub.position) } block.children.remove(sub) + symbolTable.removeTree(sub.label) numRemoved++ } } @@ -34,6 +39,7 @@ internal class IRUnusedCodeRemover(private val irprog: IRProgram, private val er irprog.blocks.reversed().forEach { block -> if(block.isEmpty()) { irprog.blocks.remove(block) + symbolTable.removeTree(block.label) numRemoved++ } } @@ -42,7 +48,7 @@ internal class IRUnusedCodeRemover(private val irprog: IRProgram, private val er } private fun removeUnreachable(allLabeledChunks: MutableMap): Int { - val entrypointSub = irprog.blocks.single { it.name=="main" }.children.single { it is IRSubroutine && it.label=="main.start" } + val entrypointSub = irprog.blocks.single { it.label=="main" }.children.single { it is IRSubroutine && it.label=="main.start" } val reachable = mutableSetOf((entrypointSub as IRSubroutine).chunks.first()) fun grow() { diff --git a/docs/source/todo.rst b/docs/source/todo.rst index fc19a6fa1..666de4ae0 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,7 @@ TODO For next minor release ^^^^^^^^^^^^^^^^^^^^^^ -remove unused variables from IR output, such as sys.wait.jiffies when never calling sys.wait + ... diff --git a/intermediate/src/prog8/intermediate/IRFileWriter.kt b/intermediate/src/prog8/intermediate/IRFileWriter.kt index a16e511ea..2503724c4 100644 --- a/intermediate/src/prog8/intermediate/IRFileWriter.kt +++ b/intermediate/src/prog8/intermediate/IRFileWriter.kt @@ -55,7 +55,7 @@ class IRFileWriter(private val irProgram: IRProgram, outfileOverride: Path?) { private fun writeBlocks() { irProgram.blocks.forEach { block -> xml.writeStartElement("BLOCK") - xml.writeAttribute("NAME", block.name) + xml.writeAttribute("NAME", block.label) xml.writeAttribute("ADDRESS", block.address?.toHex() ?: "") xml.writeAttribute("ALIGN", block.alignment.toString()) xml.writeAttribute("POS", block.position.toString()) diff --git a/intermediate/src/prog8/intermediate/IRProgram.kt b/intermediate/src/prog8/intermediate/IRProgram.kt index 47b33c650..417a802f1 100644 --- a/intermediate/src/prog8/intermediate/IRProgram.kt +++ b/intermediate/src/prog8/intermediate/IRProgram.kt @@ -60,7 +60,7 @@ class IRProgram(val name: String, } fun addBlock(block: IRBlock) { - require(blocks.all { it.name != block.name}) { "duplicate block ${block.name} ${block.position}" } + require(blocks.all { it.label != block.label}) { "duplicate block ${block.label} ${block.position}" } blocks.add(block) } @@ -238,7 +238,7 @@ class IRProgram(val name: String, } class IRBlock( - val name: String, + val label: String, val address: UInt?, val alignment: BlockAlignment, val position: Position diff --git a/intermediate/src/prog8/intermediate/IRSymbolTable.kt b/intermediate/src/prog8/intermediate/IRSymbolTable.kt index 0fb4a7ecb..80222f76a 100644 --- a/intermediate/src/prog8/intermediate/IRSymbolTable.kt +++ b/intermediate/src/prog8/intermediate/IRSymbolTable.kt @@ -4,6 +4,7 @@ import prog8.code.* import prog8.code.ast.PtVariable import prog8.code.core.DataType import prog8.code.core.ZeropageWish +import prog8.code.core.internedStringsModuleName // In the Intermediate Representation, all nesting has been removed. @@ -120,4 +121,15 @@ class IRSymbolTable(sourceSt: SymbolTable?) { } fun getAsmSymbols(): Map = asmSymbols + + fun removeTree(label: String) { + val prefix = "$label." + val vars = table.filter { it.key.startsWith(prefix) } + vars.forEach { + // check if attempt is made to delete interned strings, if so, refuse that. + if(!it.key.startsWith(internedStringsModuleName)) { + table.remove(it.key) + } + } + } } diff --git a/virtualmachine/src/prog8/vm/VmProgramLoader.kt b/virtualmachine/src/prog8/vm/VmProgramLoader.kt index f399977f6..ff8dbce1f 100644 --- a/virtualmachine/src/prog8/vm/VmProgramLoader.kt +++ b/virtualmachine/src/prog8/vm/VmProgramLoader.kt @@ -36,7 +36,7 @@ class VmProgramLoader { val chunkReplacements = mutableListOf>() irProgram.blocks.forEach { block -> if(block.address!=null) - throw IRParseException("blocks cannot have a load address for vm: ${block.name}") + throw IRParseException("blocks cannot have a load address for vm: ${block.label}") block.children.forEach { child -> when(child) {