diff --git a/compiler/src/prog8/compiler/Main.kt b/compiler/src/prog8/compiler/Main.kt index 76f30d600..c96fe45a3 100644 --- a/compiler/src/prog8/compiler/Main.kt +++ b/compiler/src/prog8/compiler/Main.kt @@ -223,7 +223,7 @@ private fun writeAssembly(programAst: Program, errors: ErrorReporter, outputDir: programAst.processAstBeforeAsmGeneration(errors) errors.handle() - printAst(programAst) + // printAst(programAst) CompilationTarget.instance.machine.initializeZeropage(compilerOptions) val assembly = CompilationTarget.instance.asmGenerator( diff --git a/compiler/src/prog8/optimizer/ExpressionSimplifier.kt b/compiler/src/prog8/optimizer/ExpressionSimplifier.kt index 13ea4345e..095bf8ef9 100644 --- a/compiler/src/prog8/optimizer/ExpressionSimplifier.kt +++ b/compiler/src/prog8/optimizer/ExpressionSimplifier.kt @@ -6,6 +6,7 @@ import prog8.ast.base.* import prog8.ast.expressions.* import prog8.ast.processing.AstWalker import prog8.ast.processing.IAstModification +import prog8.ast.statements.Assignment import kotlin.math.abs import kotlin.math.log2 import kotlin.math.pow @@ -98,6 +99,12 @@ internal class ExpressionSimplifier(private val program: Program) : AstWalker() if (leftVal != null && expr.operator in associativeOperators && rightVal == null) return listOf(IAstModification.SwapOperands(expr)) + // NonBinaryExpression BinaryExpression --> BinaryExpression NonBinaryExpression + if (expr.operator in associativeOperators && expr.left !is BinaryExpression && expr.right is BinaryExpression) { + if(parent !is Assignment || !(expr.left isSameAs parent.target)) + return listOf(IAstModification.SwapOperands(expr)) + } + // X + (-A) --> X - A if (expr.operator == "+" && (expr.right as? PrefixExpression)?.operator == "-") { return listOf(IAstModification.ReplaceNode( @@ -338,7 +345,7 @@ internal class ExpressionSimplifier(private val program: Program) : AstWalker() if (leftVal == null && rightVal == null) return null - val (expr2, _, rightVal2) = reorderAssociative(expr, leftVal) + val (expr2, _, rightVal2) = reorderAssociativeWithConstant(expr, leftVal) if (rightVal2 != null) { // right value is a constant, see if we can optimize val rightConst: NumericLiteralValue = rightVal2 @@ -562,7 +569,7 @@ internal class ExpressionSimplifier(private val program: Program) : AstWalker() if (leftVal == null && rightVal == null) return null - val (expr2, _, rightVal2) = reorderAssociative(expr, leftVal) + val (expr2, _, rightVal2) = reorderAssociativeWithConstant(expr, leftVal) if (rightVal2 != null) { // right value is a constant, see if we can optimize val leftValue: Expression = expr2.left @@ -682,17 +689,17 @@ internal class ExpressionSimplifier(private val program: Program) : AstWalker() return null } - private fun reorderAssociative(expr: BinaryExpression, leftVal: NumericLiteralValue?): ReorderedAssociativeBinaryExpr { + private fun reorderAssociativeWithConstant(expr: BinaryExpression, leftVal: NumericLiteralValue?): BinExprWithConstants { if (expr.operator in associativeOperators && leftVal != null) { // swap left and right so that right is always the constant val tmp = expr.left expr.left = expr.right expr.right = tmp - return ReorderedAssociativeBinaryExpr(expr, expr.right.constValue(program), leftVal) + return BinExprWithConstants(expr, expr.right.constValue(program), leftVal) } - return ReorderedAssociativeBinaryExpr(expr, leftVal, expr.right.constValue(program)) + return BinExprWithConstants(expr, leftVal, expr.right.constValue(program)) } - private data class ReorderedAssociativeBinaryExpr(val expr: BinaryExpression, val leftVal: NumericLiteralValue?, val rightVal: NumericLiteralValue?) + private data class BinExprWithConstants(val expr: BinaryExpression, val leftVal: NumericLiteralValue?, val rightVal: NumericLiteralValue?) } diff --git a/compiler/src/prog8/optimizer/StatementOptimizer.kt b/compiler/src/prog8/optimizer/StatementOptimizer.kt index a4ba937c6..8ffa815f7 100644 --- a/compiler/src/prog8/optimizer/StatementOptimizer.kt +++ b/compiler/src/prog8/optimizer/StatementOptimizer.kt @@ -323,7 +323,7 @@ internal class StatementOptimizer(private val program: Program, val op1 = binExpr.operator val op2 = rExpr.operator - if(rExpr.left is NumericLiteralValue && op2 in setOf("+", "*", "&", "|")) { + if(rExpr.left is NumericLiteralValue && op2 in associativeOperators) { // associative operator, make sure the constant numeric value is second (right) return listOf(IAstModification.SwapOperands(rExpr)) } diff --git a/examples/cx16/simplegraphics.p8 b/examples/cx16/simplegraphics.p8 deleted file mode 100644 index 861fefd5e..000000000 --- a/examples/cx16/simplegraphics.p8 +++ /dev/null @@ -1,40 +0,0 @@ -; CommanderX16 simple graphics example! - -%target cx16 -%zeropage basicsafe - - -main { - - sub start() { - - void cx16.screen_set_mode($80) - cx16.r0=0 - void cx16.screen_set_mode(0) - - cx16.FB_init() - cx16.GRAPH_init() - cx16.r0 = 0 - cx16.r1 = 0 - cx16.FB_cursor_position() - ubyte color - repeat 320*199 { - cx16.FB_set_pixel(color) - color++ - } - - uword xx - for xx in 0 to 319 step 32 { - ubyte q - for q in 0 to 31 { - cx16.GRAPH_set_colors(q, 2, 0) - cx16.r0 = xx+q - cx16.r1=0 - cx16.r2=rnd() - cx16.r3=199 - cx16.GRAPH_draw_line() - } - } - } -} - diff --git a/examples/dirlist.p8 b/examples/dirlist.p8 index 36b485370..ba1bf390d 100644 --- a/examples/dirlist.p8 +++ b/examples/dirlist.p8 @@ -7,7 +7,7 @@ main { sub start() { txt.print("showing the full directory of drive 8:\n") - diskio.directory(8) + void diskio.directory(8) txt.chrout('\n') if diskio.lf_start_list(8, "cub", false) { diff --git a/examples/test.p8 b/examples/test.p8 index dc1692e8e..bc044e4e6 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -9,10 +9,6 @@ main { sub start () { - uword scanline_data_ptr= $6000 - uword pixptr = xx/8 + scanline_data_ptr ; TODO why is this code so much larger than the following line: - uword pixptr2 = scanline_data_ptr + xx/8 - test_stack.test() } }