optimizer removes unreachable code following call to exit()

This commit is contained in:
Irmen de Jong 2020-03-13 02:31:53 +01:00
parent e6be428589
commit b182f7e693
4 changed files with 27 additions and 6 deletions

View File

@ -1049,7 +1049,7 @@ internal class AstChecker(private val program: Program,
private fun visitStatements(statements: List<Statement>) {
for((index, stmt) in statements.withIndex()) {
if(stmt is FunctionCallStatement && stmt.target.nameInSource.last()=="exit") {
if(index < statements.size-1) {
if(index < statements.lastIndex) {
printWarning("unreachable code", statements[index+1].position, "exit call above never returns")
}
}

View File

@ -116,6 +116,7 @@ internal class StatementOptimizer(private val program: Program) : IAstModifyingV
return NopStatement.insteadOf(subroutine)
}
visitStatements(subroutine.statements)
return subroutine
}
@ -564,6 +565,7 @@ internal class StatementOptimizer(private val program: Program) : IAstModifyingV
if(linesToRemove.isNotEmpty()) {
linesToRemove.reversed().forEach{scope.statements.removeAt(it)}
}
visitStatements(scope.statements)
return super.visit(scope)
}
@ -571,11 +573,23 @@ internal class StatementOptimizer(private val program: Program) : IAstModifyingV
// remove duplicate labels
val stmts = label.definingScope().statements
val startIdx = stmts.indexOf(label)
if(startIdx<(stmts.size-1) && stmts[startIdx+1] == label)
if(startIdx< stmts.lastIndex && stmts[startIdx+1] == label)
return NopStatement.insteadOf(label)
return super.visit(label)
}
private fun visitStatements(statements: MutableList<Statement>) {
// remove all statements following the call to exit()
val exitCallIndex = statements.indexOfFirst { it is FunctionCallStatement && it.target.nameInSource.last()=="exit" }
if(exitCallIndex>=0) {
while(exitCallIndex < statements.lastIndex) {
statements.removeAt(statements.lastIndex)
}
}
}
}

View File

@ -3,7 +3,6 @@ TODO
====
- option to load library files from a directory instead of the embedded ones
- code optimizer: remove all statements following an exit() function call
- vector inc/dec/add/sub/mul/div...?
arrayvar++ / arrayvar-- / arrayvar += 2 / arrayvar -= 2 / arrayvar *= 3 / arrayvar /= 3

View File

@ -21,9 +21,17 @@ main {
c64scr.print("sp2:")
print_stackpointer()
exit(65)
sub3() ; TODO remove the code in optimizer
sub3() ; TODO remove the code in optimizer
sub3() ; TODO remove the code in optimizer
sub3()
sub3()
sub3()
sub3()
sub3()
sub3()
sub3()
sub3()
sub3()
sub3()
sub3()
}
sub sub3() {