fix reporting of (not) unused code after GoSub jump

This commit is contained in:
Irmen de Jong 2021-11-27 21:22:34 +01:00
parent c838821615
commit 69dcb4dbda
2 changed files with 17 additions and 5 deletions

View File

@ -28,27 +28,28 @@ class UnusedCodeRemover(private val program: Program,
} }
override fun before(breakStmt: Break, parent: Node): Iterable<IAstModification> { override fun before(breakStmt: Break, parent: Node): Iterable<IAstModification> {
reportUnreachable(breakStmt, parent as IStatementContainer) reportUnreachable(breakStmt)
return emptyList() return emptyList()
} }
override fun before(jump: Jump, parent: Node): Iterable<IAstModification> { override fun before(jump: Jump, parent: Node): Iterable<IAstModification> {
reportUnreachable(jump, parent as IStatementContainer) if(!jump.isGosub)
reportUnreachable(jump)
return emptyList() return emptyList()
} }
override fun before(returnStmt: Return, parent: Node): Iterable<IAstModification> { override fun before(returnStmt: Return, parent: Node): Iterable<IAstModification> {
reportUnreachable(returnStmt, parent as IStatementContainer) reportUnreachable(returnStmt)
return emptyList() return emptyList()
} }
override fun before(functionCallStatement: FunctionCallStatement, parent: Node): Iterable<IAstModification> { override fun before(functionCallStatement: FunctionCallStatement, parent: Node): Iterable<IAstModification> {
if(functionCallStatement.target.nameInSource.last() == "exit") if(functionCallStatement.target.nameInSource.last() == "exit")
reportUnreachable(functionCallStatement, parent as IStatementContainer) reportUnreachable(functionCallStatement)
return emptyList() return emptyList()
} }
private fun reportUnreachable(stmt: Statement, parent: IStatementContainer) { private fun reportUnreachable(stmt: Statement) {
when(val next = stmt.nextSibling()) { when(val next = stmt.nextSibling()) {
null, is Label, is Directive, is VarDecl, is InlineAssembly, is Subroutine -> {} null, is Label, is Directive, is VarDecl, is InlineAssembly, is Subroutine -> {}
else -> errors.warn("unreachable code", next.position) else -> errors.warn("unreachable code", next.position)

View File

@ -21,6 +21,13 @@ main {
; void sys.pop() ; void sys.pop()
routine2(uw, 11,22, true, 33) routine2(uw, 11,22, true, 33)
routine2(uw, 11,22, true, 33)
routine2(uw, 11,22, true, 33)
routine2(uw, 11,22, true, 33)
blerp(22)
blerp(22)
blerp(22)
blerp(22)
test_stack.test() test_stack.test()
@ -29,6 +36,10 @@ main {
} }
sub blerp(uword z) {
z++
}
asmsub routine2(uword num @AY, ubyte a1 @R1, ubyte a2 @R2, ubyte switch @Pc, ubyte a3 @X) { asmsub routine2(uword num @AY, ubyte a1 @R1, ubyte a2 @R2, ubyte switch @Pc, ubyte a3 @X) {
%asm {{ %asm {{
adc #20 adc #20