diff --git a/compiler/src/prog8/ast/base/Extensions.kt b/compiler/src/prog8/ast/base/Extensions.kt index 236fe2e83..5d1ab1471 100644 --- a/compiler/src/prog8/ast/base/Extensions.kt +++ b/compiler/src/prog8/ast/base/Extensions.kt @@ -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) diff --git a/compiler/src/prog8/compiler/Main.kt b/compiler/src/prog8/compiler/Main.kt index 707dfa6e4..1e9917632 100644 --- a/compiler/src/prog8/compiler/Main.kt +++ b/compiler/src/prog8/compiler/Main.kt @@ -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) { diff --git a/compiler/src/prog8/optimizer/StatementOptimizer.kt b/compiler/src/prog8/optimizer/StatementOptimizer.kt index 2fe561b4d..15c48254f 100644 --- a/compiler/src/prog8/optimizer/StatementOptimizer.kt +++ b/compiler/src/prog8/optimizer/StatementOptimizer.kt @@ -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() - val nopStatements = mutableListOf() 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() + + 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) + } +} diff --git a/examples/cube3d-sprites.p8 b/examples/cube3d-sprites.p8 index 5555cf70d..8d629596c 100644 --- a/examples/cube3d-sprites.p8 +++ b/examples/cube3d-sprites.p8 @@ -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 } } }