From 4d840c7db854fe5b30e545239286a88552317fbd Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 11 Aug 2022 22:51:09 +0200 Subject: [PATCH] optimized mkword(0, X) --- .../src/prog8/optimizer/ExpressionSimplifier.kt | 8 ++++++++ docs/source/todo.rst | 2 -- examples/test.p8 | 16 ++++++++-------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/codeOptimizers/src/prog8/optimizer/ExpressionSimplifier.kt b/codeOptimizers/src/prog8/optimizer/ExpressionSimplifier.kt index 0390cc691..6dcbded5d 100644 --- a/codeOptimizers/src/prog8/optimizer/ExpressionSimplifier.kt +++ b/codeOptimizers/src/prog8/optimizer/ExpressionSimplifier.kt @@ -331,6 +331,14 @@ class ExpressionSimplifier(private val program: Program) : AstWalker() { } } + if(functionCallExpr.target.nameInSource == listOf("mkword")) { + if(functionCallExpr.args[0].constValue(program)?.number==0.0) { + // just cast the lsb to uword + val cast = TypecastExpression(functionCallExpr.args[1], DataType.UWORD, true, functionCallExpr.position) + return listOf(IAstModification.ReplaceNode(functionCallExpr, cast, parent)) + } + } + return noModifications } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 0f9cd2502..ffee1d3bf 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -4,8 +4,6 @@ TODO For next release ^^^^^^^^^^^^^^^^ - crc ^= mkword(@(data), 0) produces faulty asm, as opposed to crc ^= mkword(variable, 0) -- optimize asm generated for mkword(0, X) and mkword(X, 0) -- check signed value >> and << .... do we calculate the right thing? ... diff --git a/examples/test.p8 b/examples/test.p8 index e86b65809..8edc1dc61 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -3,17 +3,17 @@ main { sub start() { - uword ww = $ff34 - ww = ww ^ ww<<8 + ubyte left = $12 + ubyte right = $34 + uword ww + + ww = mkword(left, right) txt.print_uwhex(ww, true) - ww = $ff34 - ww = ww ^ mkword(lsb(ww), 0) + ww = mkword(left, 0) txt.print_uwhex(ww, true) - ww = $ff34 - ww = ww ^ ww >> 8 + ww = mkword(0, right) txt.print_uwhex(ww, true) - ww = $ff34 - ww = ww ^ msb(ww) + ww = right as uword txt.print_uwhex(ww, true) }