IR: fix invalid asm name matching that resulted in not removing subs with a name matching an IR asm instruction

This commit is contained in:
Irmen de Jong 2024-11-30 00:06:02 +01:00
parent feb8aa435e
commit 6aaa0f928e
5 changed files with 42 additions and 73 deletions

View File

@ -94,18 +94,18 @@ class UnusedCodeRemover(private val program: Program,
override fun after(block: Block, parent: Node): Iterable<IAstModification> {
if("force_output" !in block.options()) {
if (block.containsNoCodeNorVars) {
if(block.name != internedStringsModuleName && "ignore_unused" !in block.options()) {
if(!block.statements.any { it is Subroutine && it.hasBeenInlined })
if (block.name != internedStringsModuleName && "ignore_unused" !in block.options()) {
if (!block.statements.any { it is Subroutine && it.hasBeenInlined })
errors.info("removing unused block '${block.name}'", block.position)
}
return listOf(IAstModification.Remove(block, parent as IStatementContainer))
}
if(callgraph.unused(block)) {
if(block.statements.any{ it !is VarDecl || it.type== VarDeclType.VAR} && "ignore_unused" !in block.options()) {
if(!block.statements.any { it is Subroutine && it.hasBeenInlined })
if (callgraph.unused(block)) {
if (block.statements.any { it !is VarDecl || it.type == VarDeclType.VAR } && "ignore_unused" !in block.options()) {
if (!block.statements.any { it is Subroutine && it.hasBeenInlined })
errors.info("removing unused block '${block.name}'", block.position)
}
if(!block.statements.any { it is Subroutine && it.hasBeenInlined }) {
if (!block.statements.any { it is Subroutine && it.hasBeenInlined }) {
program.removeInternedStringsFromRemovedBlock(block)
}
return listOf(IAstModification.Remove(block, parent as IStatementContainer))

View File

@ -285,4 +285,26 @@ main {
val virtfile = result.compilationOptions.outputDir.resolve(result.compilerAst.name + ".p8ir")
VmRunner().runProgram(virtfile.readText())
}
test("also remove subroutines with names matching IR asm instruction") {
var src="""
main {
sub start() {
}
}
xyz {
uword buffer_ptr = memory("buffers_stack", 8192, 0)
sub pop() -> ubyte { ; pop is also an IR instruction
return buffer_ptr[2]
}
}"""
val result = compileText(VMTarget(), true, src, writeAssembly = true)!!
val blocks = result.codegenAst!!.allBlocks().toList()
blocks.any { it.name=="xyz" } shouldBe false
val result2 = compileText(C64Target(), true, src, writeAssembly = true)!!
val blocks2 = result2.codegenAst!!.allBlocks().toList()
blocks2.any { it.name=="xyz" } shouldBe false
}
})

View File

@ -753,11 +753,14 @@ class InlineAssembly(val assembly: String, val isIR: Boolean, override val posit
val names: Set<String> by lazy {
// A cache of all the words (identifiers) present in this block of assembly code
// this is used when checking if prog8 names are referenced from assembly code
val wordPattern = Regex("""\b([_a-zA-Z]\w+?)\b""")
val wordPattern = if(isIR)
Regex("""\b([_a-zA-Z]\w+?)(?!.[a-zA-Z]\b)\b""") // words not ending with a dot and a single letter (like "pop.b")
else
Regex("""\b([_a-zA-Z]\w+?)\b""")
assembly.splitToSequence('\n')
.map {
val everythintBeforeComment = it.substringBefore(';')
wordPattern.findAll(everythintBeforeComment)
val everythingBeforeComment = it.substringBefore(';')
wordPattern.findAll(everythingBeforeComment)
}
.flatMap { it.map { mr -> mr.value } }
.toSet()

View File

@ -1,8 +1,6 @@
TODO
====
virtual: importing buffers in main results in a vm error?
document the @R0 - @R15 register support for normal subroutine parameters (footgun!)
make a compiler switch to disable footgun warnings

View File

@ -1,66 +1,12 @@
%import monogfx
%option no_sysinit
%zeropage basicsafe
main {
sub start() {
monogfx.lores()
uword x
for x in 128 to 319 step 40 {
monogfx.line(x, 0, x-100, 200, true)
}
for x in 32 to 200 step 40 {
monogfx.line(x, 239, x+100, 20, true)
}
monogfx.fill(310, 230, true, 44)
sys.wait(100)
}
; sub start() {
; monogfx.hires()
;
; uword x
; for x in 128 to 639 step 64 {
; monogfx.line(x, 0, x-100, 400, true)
; }
; for x in 32 to 500 step 64 {
; monogfx.line(x, 479, x+100, 100, true)
; }
; monogfx.fill(630, 440, true, 44)
;
; sys.wait(100)
; }
; sub start() {
; gfx_hires.graphics_mode()
;
; uword x
; for x in 128 to 639 step 64 {
; gfx_hires.line(x, 0, x-100, 400, 1)
; }
; for x in 32 to 500 step 64 {
; gfx_hires.line(x, 479, x+100, 100, 1)
; }
; gfx_hires.fill(630, 440, 2, 44)
;
; sys.wait(100)
; }
; sub start() {
; gfx_lores.graphics_mode()
;
; uword x
; for x in 128 to 319 step 40 {
; gfx_lores.line(x, 0, x-100, 200, 1)
; }
; for x in 32 to 200 step 40 {
; gfx_lores.line(x, 239, x+100, 20, 1)
; }
; gfx_lores.fill(310, 230, 2, 44)
;
; sys.wait(100)
; }
}
xyz {
uword buffer_ptr = memory("buffers_stack", 8192, 0)
sub pop() -> ubyte {
return buffer_ptr[2]
}
}