From 547b1d37206f02227813d6130b4a8a081909c80e Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 6 Dec 2021 21:43:17 +0100 Subject: [PATCH] comment corrections --- .../compiler/BeforeAsmGenerationAstChanger.kt | 11 ++++--- docs/source/todo.rst | 2 +- examples/test.p8 | 29 ++++++++++++------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt b/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt index f390ff346..e69b425ac 100644 --- a/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt +++ b/compiler/src/prog8/compiler/BeforeAsmGenerationAstChanger.kt @@ -219,6 +219,10 @@ internal class BeforeAsmGenerationAstChanger(val program: Program, private val o // TODO: somehow figure out if the expr will result in stack-evaluation STILL after being split off, // in that case: do *not* split it off but just keep it as it is (otherwise code size increases) + // TODO: do NOT move this to an earler ast transform phase (such as StatementReorderer or StatementOptimizer) + // it WILL result in larger code. + // TODO: this should be replaced by a general expression-evaluation optimization step. + // the actual conditional expression in the statement should be no more than VARIABLE SIMPLE-EXPRESSION var leftAssignment: Assignment? = null var leftOperandReplacement: Expression? = null @@ -317,6 +321,7 @@ internal class BeforeAsmGenerationAstChanger(val program: Program, private val o throw FatalAstException("0==X should have been swapped to if X==0") // TODO simplify the conditional expression, introduce simple assignments if required. + // make sure to evaluate it only once, but also right at entry of the while loop // NOTE: sometimes this increases code size because additional stores/loads are generated for the // intermediate variables. We assume these are optimized away from the resulting assembly code later. // NOTE: this is nasty for a while-statement as the condition occurs at the top of the loop @@ -408,10 +413,8 @@ internal class BeforeAsmGenerationAstChanger(val program: Program, private val o val modifications = mutableListOf() val statement = expr.containingStatement val dt = expr.indexer.indexExpr.inferType(program) - val register = if(dt istype DataType.UBYTE || dt istype DataType.BYTE ) "retval_interm_ub" else "retval_interm_b" - // replace the indexer with just the variable (simply use a cx16 virtual register r9, that we HOPE is not used for other things in the expression...) - // assign the indexing expression to the helper variable, but only if that hasn't been done already - val target = AssignTarget(IdentifierReference(listOf("prog8_lib", register), expr.indexer.position), null, null, expr.indexer.position) + val tempvar = if(dt.isBytes) listOf("prog8_lib","retval_interm_ub") else listOf("prog8_lib","retval_interm_b") + val target = AssignTarget(IdentifierReference(tempvar, expr.indexer.position), null, null, expr.indexer.position) val assign = Assignment(target, expr.indexer.indexExpr, expr.indexer.position) modifications.add(IAstModification.InsertBefore(statement, assign, statement.parent as IStatementContainer)) modifications.add(IAstModification.ReplaceNode(expr.indexer.indexExpr, target.identifier!!.copy(), expr.indexer)) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index cc91a4db3..87e75a91e 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -41,5 +41,5 @@ More code optimization ideas but the result should not produce larger code ofcourse! - while-expression should now also get the simplifyConditionalExpression() treatment - rewrite expression tree evaluation such that it doesn't use an eval stack but flatten the tree into linear code that uses a fixed number of predetermined value 'variables' -- this removes the need for the BinExprSplitter (which is problematic now) +- this removes the need for the BinExprSplitter? (which is problematic and very limited now) - introduce byte-index operator to avoid index multiplications in loops over arrays? see github issue #4 diff --git a/examples/test.p8 b/examples/test.p8 index c7d33e8ea..a3f515bd4 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -6,16 +6,25 @@ main { sub start() { ubyte @shared bb - when bb { - 0,1,2 -> { - bb ++ - } - 3 -> { - bb-- - } - else -> { - bb=0 - } + if bb + sin8u(bb) > 100-bb { + bb++ } + + while bb + sin8u(bb) > 100-bb { + bb++ + } + + do { + bb++ + } until bb + sin8u(bb) > 100-bb + + const ubyte EN_TYPE=2 + uword eRef = $c000 + ubyte chance = rnd() % 100 + + if eRef[EN_TYPE] and chance < (eRef[EN_TYPE] << 1) { + bb++ + } + } }