proper NOP removal

This commit is contained in:
Irmen de Jong 2019-07-10 03:06:31 +02:00
parent 7eed1ebbf8
commit 1f89571aa5
4 changed files with 27 additions and 13 deletions

View File

@ -6,6 +6,7 @@ import prog8.ast.processing.*
import prog8.ast.statements.Assignment
import prog8.ast.statements.ForLoop
import prog8.compiler.CompilationOptions
import prog8.optimizer.RemoveNops
// the name of the subroutine that should be called for every block to initialize its variables
@ -16,6 +17,12 @@ internal const val initvarsSubName="prog8_init_vars"
internal const val autoHeapValuePrefix = "auto_heap_value_"
internal fun Program.removeNops() {
val remover = RemoveNops()
remover.visit(this)
}
internal fun Program.checkValid(compilerOptions: CompilationOptions) {
val checker = AstChecker(this, compilerOptions)
checker.visit(this)

View File

@ -84,10 +84,11 @@ fun compileProgram(filepath: Path,
}
}
programAst.removeNops()
programAst.checkValid(compilerOptions) // check if final tree is valid
programAst.checkRecursion() // check if there are recursive subroutine calls
printAst(programAst)
// printAst(programAst)
// namespace.debugPrint()
if(generateVmCode) {

View File

@ -4,6 +4,7 @@ import prog8.ast.*
import prog8.ast.base.*
import prog8.ast.expressions.*
import prog8.ast.processing.IAstModifyingVisitor
import prog8.ast.processing.IAstVisitor
import prog8.ast.statements.*
import prog8.compiler.target.c64.Petscii
import prog8.functions.BuiltinFunctions
@ -15,11 +16,11 @@ import kotlin.math.floor
todo analyse for unreachable code and remove that (f.i. code after goto or return that has no label so can never be jumped to) + print warning about this
*/
internal class StatementOptimizer(private val program: Program, private val optimizeInlining: Boolean) : IAstModifyingVisitor {
var optimizationsDone: Int = 0
private set
var scopesToFlatten = mutableListOf<INameScope>()
val nopStatements = mutableListOf<NopStatement>()
private val pureBuiltinFunctions = BuiltinFunctions.filter { it.value.pure }
private val callgraph = CallGraph(program)
@ -34,11 +35,6 @@ internal class StatementOptimizer(private val program: Program, private val opti
inlineSubroutines(callgraph)
}
super.visit(program)
// at the end, remove the encountered NOP statements
this.nopStatements.forEach {
it.definingScope().remove(it)
}
}
private fun inlineSubroutines(callgraph: CallGraph) {
@ -432,11 +428,6 @@ internal class StatementOptimizer(private val program: Program, private val opti
return repeatLoop
}
override fun visit(nopStatement: NopStatement): IStatement {
this.nopStatements.add(nopStatement)
return nopStatement
}
override fun visit(whenStatement: WhenStatement): IStatement {
val choices = whenStatement.choices.toList()
for(choice in choices) {
@ -654,3 +645,18 @@ internal class StatementOptimizer(private val program: Program, private val opti
internal class RemoveNops(): IAstVisitor {
val nopStatements = mutableListOf<NopStatement>()
override fun visit(program: Program) {
super.visit(program)
// at the end, remove the encountered NOP statements
this.nopStatements.forEach {
it.definingScope().remove(it)
}
}
override fun visit(nopStatement: NopStatement) {
nopStatements.add(nopStatement)
}
}

View File

@ -161,7 +161,7 @@
else
c64.SPRPTR[i] = $2000/64 ; small ball
c64.SPCOL[i] = spritecolors[zc>>13 as byte + 4] ; further away=darker color TODO doesn't work anymore? ">>" seems broken.
c64.SPCOL[i] = spritecolors[zc>>13 as byte + 4] ; further away=darker color
}
}
}