fixed improper optimization of word<<8 and word>>8

This commit is contained in:
Irmen de Jong 2022-01-28 13:54:06 +01:00
parent 06128b5d07
commit 7b3cd71085
3 changed files with 14 additions and 22 deletions

View File

@ -680,11 +680,8 @@ class ExpressionSimplifier(private val program: Program, private val errors: IEr
DataType.UWORD, DataType.WORD -> {
if (amount >= 16) {
return NumericLiteralValue(targetDt, 0.0, expr.position)
} else if (amount >= 8) {
} else if (amount > 8) {
val lsb = FunctionCallExpression(IdentifierReference(listOf("lsb"), expr.position), mutableListOf(expr.left), expr.position)
if (amount == 8) {
return FunctionCallExpression(IdentifierReference(listOf("mkword"), expr.position), mutableListOf(lsb, NumericLiteralValue.optimalInteger(0, expr.position)), expr.position)
}
val shifted = BinaryExpression(lsb, "<<", NumericLiteralValue.optimalInteger(amount - 8, expr.position), expr.position)
return FunctionCallExpression(IdentifierReference(listOf("mkword"), expr.position), mutableListOf(shifted, NumericLiteralValue.optimalInteger(0, expr.position)), expr.position)
}
@ -722,13 +719,8 @@ class ExpressionSimplifier(private val program: Program, private val errors: IEr
if (amount >= 16) {
return NumericLiteralValue.optimalInteger(0, expr.position)
}
else if (amount >= 8) {
else if (amount > 8) {
val msb = FunctionCallExpression(IdentifierReference(listOf("msb"), expr.position), mutableListOf(expr.left), expr.position)
if (amount == 8) {
// mkword(0, msb(v))
val zero = NumericLiteralValue(DataType.UBYTE, 0.0, expr.position)
return FunctionCallExpression(IdentifierReference(listOf("mkword"), expr.position), mutableListOf(zero, msb), expr.position)
}
return TypecastExpression(BinaryExpression(msb, ">>", NumericLiteralValue.optimalInteger(amount - 8, expr.position), expr.position), DataType.UWORD, true, expr.position)
}
}

View File

@ -3,8 +3,6 @@ TODO
For next release
^^^^^^^^^^^^^^^^
- fix optimization of w >>= 8 , it generates bad code when opt=on, ok when opt=off
- fix w <<= 8 generates weird code with pha/pla in it when opt=on, ok when opt=off
- optimize w=msb(w) => w >>=8, w=lsb(w) ==> w &= $00ff
fix the value of ww being wrong (with optimizations enabled) in :

View File

@ -4,22 +4,24 @@
main {
sub start() {
uword @shared xx=$ea31
xx &= $00ff
;xx &= $00ff
;xx = lsb(xx)
txt.print_uwhex(xx, true)
xx = $ea31
xx &= $ff00
;txt.print_uwhex(xx, true)
;xx = $ea31
;xx &= $ff00
; xx = msb(xx)
; %asm {{
; nop
; nop
; }}
; xx >>= 8
; %asm {{
; nop
; nop
; }}
; xx <<= 8
xx >>= 8
%asm {{
nop
}}
xx <<= 8
%asm {{
nop
}}
txt.print_uwhex(xx, true)
}
}