From 96ecbc9fe4f9d0e3912e4804eacf02461ee9f89b Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 27 Feb 2019 23:07:12 +0100 Subject: [PATCH] fixed too eager expression operand type adjustment --- .../prog8/optimizing/SimplifyExpressions.kt | 35 ++++++++++++------- examples/test.p8 | 11 ++---- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/compiler/src/prog8/optimizing/SimplifyExpressions.kt b/compiler/src/prog8/optimizing/SimplifyExpressions.kt index 85046facf..69fcf9311 100644 --- a/compiler/src/prog8/optimizing/SimplifyExpressions.kt +++ b/compiler/src/prog8/optimizing/SimplifyExpressions.kt @@ -75,7 +75,7 @@ class SimplifyExpressions(private val namespace: INameScope, private val heap: H val leftDt = expr.left.resultingDatatype(namespace, heap) val rightDt = expr.right.resultingDatatype(namespace, heap) if (leftDt != null && rightDt != null && leftDt != rightDt) { - // try to convert a datatype into the other + // try to convert a datatype into the other (where ddd if (adjustDatatypes(expr, leftVal, leftDt, rightVal, rightDt)) { optimizationsDone++ return expr @@ -347,26 +347,37 @@ class SimplifyExpressions(private val namespace: INameScope, private val heap: H } if(leftConstVal==null && rightConstVal!=null) { - val (adjusted, newValue) = adjust(rightConstVal, leftDt) - if(adjusted) { - expr.right = newValue - optimizationsDone++ - return true + if(isBiggerType(leftDt, rightDt)) { + val (adjusted, newValue) = adjust(rightConstVal, leftDt) + if (adjusted) { + expr.right = newValue + optimizationsDone++ + return true + } } return false } else if(leftConstVal!=null && rightConstVal==null) { - val (adjusted, newValue) = adjust(leftConstVal, rightDt) - if(adjusted) { - expr.left = newValue - optimizationsDone++ - return true + if(isBiggerType(rightDt, leftDt)) { + val (adjusted, newValue) = adjust(leftConstVal, rightDt) + if (adjusted) { + expr.left = newValue + optimizationsDone++ + return true + } } return false } else { - return false + return false // two const values, don't adjust (should have been const-folded away) } } + private fun isBiggerType(type: DataType, other: DataType) = + when(type) { + in ByteDatatypes -> false + in WordDatatypes -> other in ByteDatatypes + else -> true + } + private data class ReorderedAssociativeBinaryExpr(val expr: BinaryExpression, val leftVal: LiteralValue?, val rightVal: LiteralValue?) diff --git a/examples/test.p8 b/examples/test.p8 index d34ba4f73..118b74d5f 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -8,8 +8,8 @@ ; @todo see problem in looplabelproblem.p8 ; @todo add docs for '@zp' tag in variable datatype declarations (including forloop loopvars) + ; @todo gradle fatJar should include the antlr runtime jar - ; word ww2; byte bb; ww2 = bb * 55.w ; @todo why is this compiling, but resulting in a byte? ; uword x = sin8u(bb) as uword + 50 ; @todo fix "cannot assign word to uword" ; uword ypos=4; ypos += 5000 ; @todo fix "cannot assign word to uword" @@ -26,16 +26,9 @@ bb2 = bb*55 ww2 = ww*55 - ;uword x = sin8u(bb) as uword + 50 ; @todo fix "cannot assign word to uword" + uword x = sin8u(bb) as uword + 50 ; @todo fix "cannot assign word to uword" ;ypos += 5000 ; @todo fix "cannot assign word to uword" - ww2 = bb * 55.w ; @todo why is this compiling, but resulting in a byte? - c64scr.print_w(ww2) - c64.CHROUT('\n') - ww2 = (bb as word)*55 - c64scr.print_w(ww2) - c64.CHROUT('\n') - ; ; memset($0400+(ypos+0)*40, 40, 1) ; memset($0400+(ypos+1)*40, 40, 2)