From ab2d1122a98dd60dbb7476fdcaab10a010cdd546 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 14 Nov 2021 12:38:56 +0100 Subject: [PATCH] conditional expressions are optimized more intelligently (simple ones are not split off in separate assignments) --- .../compiler/BeforeAsmGenerationAstChanger.kt | 7 ++----- .../src/prog8/ast/expressions/AstExpressions.kt | 2 +- docs/source/todo.rst | 1 - examples/test.p8 | 17 +++++++++-------- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt b/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt index cebb6a8d8..e35ea8da1 100644 --- a/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt +++ b/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt @@ -240,10 +240,7 @@ internal class BeforeAsmGenerationAstChanger(val program: Program, private val o var rightAssignment: Assignment? = null var rightOperandReplacement: Expression? = null - - // TODO don't optimize simple conditionals that are just a function call - - if(!expr.left.isSimple) { + if(!expr.left.isSimple && expr.left !is IFunctionCall) { val dt = expr.left.inferType(program) val name = when { dt.istype(DataType.UBYTE) -> listOf("cx16","r9L") // assume (hope) cx16.r9 isn't used for anything else... @@ -259,7 +256,7 @@ internal class BeforeAsmGenerationAstChanger(val program: Program, private val o expr.position ) } - if(!expr.right.isSimple) { + if(!expr.right.isSimple && expr.right !is IFunctionCall) { val dt = expr.right.inferType(program) val name = when { dt.istype(DataType.UBYTE) -> listOf("prog8_lib","retval_interm_ub") diff --git a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt index ae954ea83..5d2654fee 100644 --- a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt +++ b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt @@ -118,7 +118,7 @@ class PrefixExpression(val operator: String, var expression: Expression, overrid } } - override val isSimple = false + override val isSimple = true override fun toString(): String { return "Prefix($operator $expression)" diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 6d3c9e15c..f86e92e5d 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,6 @@ TODO For next compiler release (7.3) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- simplifyConditionalExpression: don't change conditinal expressions that are just a single function call - add expression simplification to while and until loops as well. diff --git a/examples/test.p8 b/examples/test.p8 index 39cec14d6..197513e0f 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -5,15 +5,16 @@ main { sub start() { - float[] farr = [1.111,2.222,3.333,4.444,5.555,6.666] - float f2 = 9.999 - ubyte xx=1 - ubyte yy=2 + byte xx=1 - floats.print_f(farr[3]) - txt.nl() - floats.print_f(farr[xx+yy]) - txt.nl() + if -xx { + xx++ + } + + sub test() -> ubyte { + xx++ + return xx + } }