From 446fc35d5cbe50c4a08e410c1a5e5c54ad2852ad Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 27 Sep 2020 03:55:59 +0200 Subject: [PATCH] avoid excessive comparisons for certain comparison expressions against zero --- .../target/c64/codegen/ExpressionsAsmGen.kt | 66 +++++++++++++++++++ examples/balloonflight.p8 | 3 + 2 files changed, 69 insertions(+) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/ExpressionsAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/ExpressionsAsmGen.kt index 37ab239f4..a3752e946 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/ExpressionsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/ExpressionsAsmGen.kt @@ -53,6 +53,39 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge val dt = left.inferType(program).typeOrElse(DataType.STRUCT) when (operator) { "==" -> { + // if the left operand is an expression, and the right is 0, we can just evaluate that expression. + // (the extra comparison is not required as the result of the expression is already the required boolean value) + if(rightConstVal?.number?.toDouble() == 0.0) { + when(left) { + is PrefixExpression, + is BinaryExpression, + is ArrayIndexedExpression, + is TypecastExpression, + is AddressOf, + is RangeExpr, + is FunctionCall -> { + translateExpression(left) + if(dt in ByteDatatypes) { + asmgen.out(""" + inx + lda P8ESTACK_LO,x + bne $jumpIfFalseLabel""") + return + } + else if(dt in WordDatatypes) { + asmgen.out(""" + inx + lda P8ESTACK_LO,x + clc + adc P8ESTACK_HI,x + bne $jumpIfFalseLabel""") + return + } + } + else -> {} + } + } + when (dt) { in ByteDatatypes -> translateByteEquals(left, right, leftConstVal, rightConstVal, jumpIfFalseLabel) in WordDatatypes -> translateWordEquals(left, right, leftConstVal, rightConstVal, jumpIfFalseLabel) @@ -61,6 +94,39 @@ internal class ExpressionsAsmGen(private val program: Program, private val asmge } } "!=" -> { + // if the left operand is an expression, and the right is 0, we can just evaluate that expression. + // (the extra comparison is not required as the result of the expression is already the required boolean value) + if(rightConstVal?.number?.toDouble() == 0.0) { + when(left) { + is PrefixExpression, + is BinaryExpression, + is ArrayIndexedExpression, + is TypecastExpression, + is AddressOf, + is RangeExpr, + is FunctionCall -> { + translateExpression(left) + if(dt in ByteDatatypes) { + asmgen.out(""" + inx + lda P8ESTACK_LO,x + beq $jumpIfFalseLabel""") + return + } + else if(dt in WordDatatypes) { + asmgen.out(""" + inx + lda P8ESTACK_LO,x + clc + adc P8ESTACK_HI,x + beq $jumpIfFalseLabel""") + return + } + } + else -> {} + } + } + when (dt) { in ByteDatatypes -> translateByteNotEquals(left, right, leftConstVal, rightConstVal, jumpIfFalseLabel) in WordDatatypes -> translateWordNotEquals(left, right, leftConstVal, rightConstVal, jumpIfFalseLabel) diff --git a/examples/balloonflight.p8 b/examples/balloonflight.p8 index 7c73c20e9..a1d964d7c 100644 --- a/examples/balloonflight.p8 +++ b/examples/balloonflight.p8 @@ -3,6 +3,9 @@ %import textio %zeropage basicsafe +; TODO balloon only goes down now + + main { ubyte perform_scroll = false