mirror of
https://github.com/irmen/prog8.git
synced 2025-02-28 09:29:26 +00:00
proper NOP removal
This commit is contained in:
parent
7eed1ebbf8
commit
1f89571aa5
@ -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)
|
||||
|
@ -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) {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user