optimized uword <<8 and >>8

This commit is contained in:
Irmen de Jong 2022-08-11 22:25:15 +02:00
parent 2d34fdd28f
commit 4d2b21816d
3 changed files with 31 additions and 21 deletions

View File

@ -564,7 +564,14 @@ class ExpressionSimplifier(private val program: Program) : AstWalker() {
DataType.UWORD, DataType.WORD -> {
if (amount >= 16) {
return NumericLiteral(targetDt, 0.0, expr.position)
} else if (amount > 8) {
}
else if(amount==8) {
// shift left by 8 bits is just a byte operation: mkword(lsb(X), 0)
val lsb = FunctionCallExpression(IdentifierReference(listOf("lsb"), expr.position), mutableListOf(expr.left), expr.position)
return FunctionCallExpression(IdentifierReference(listOf("mkword"), expr.position), mutableListOf(lsb, NumericLiteral(DataType.UBYTE, 0.0, expr.position)), expr.position)
}
else if (amount > 8) {
// same as above but with residual shifts.
val lsb = FunctionCallExpression(IdentifierReference(listOf("lsb"), expr.position), mutableListOf(expr.left), expr.position)
val shifted = BinaryExpression(lsb, "<<", NumericLiteral.optimalInteger(amount - 8, expr.position), expr.position)
return FunctionCallExpression(IdentifierReference(listOf("mkword"), expr.position), mutableListOf(shifted, NumericLiteral.optimalInteger(0, expr.position)), expr.position)
@ -603,12 +610,19 @@ class ExpressionSimplifier(private val program: Program) : AstWalker() {
if (amount >= 16) {
return NumericLiteral.optimalInteger(0, expr.position)
}
else if(amount==8) {
// shift right by 8 bits is just a byte operation: msb(X) as uword
val msb = FunctionCallExpression(IdentifierReference(listOf("msb"), expr.position), mutableListOf(expr.left), expr.position)
return TypecastExpression(msb, DataType.UWORD, true, expr.position)
}
else if (amount > 8) {
// same as above but with residual shifts.
val msb = FunctionCallExpression(IdentifierReference(listOf("msb"), expr.position), mutableListOf(expr.left), expr.position)
return TypecastExpression(BinaryExpression(msb, ">>", NumericLiteral.optimalInteger(amount - 8, expr.position), expr.position), DataType.UWORD, true, expr.position)
}
}
DataType.WORD -> {
// bit-shifting a signed value shouldn't be allowed by the compiler but here we go...
if (amount > 16) {
expr.right = NumericLiteral.optimalInteger(16, expr.right.position)
return null

View File

@ -3,6 +3,10 @@ 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?
...

View File

@ -3,7 +3,18 @@
main {
sub start() {
txt.print("yo")
uword ww = $ff34
ww = ww ^ ww<<8
txt.print_uwhex(ww, true)
ww = $ff34
ww = ww ^ mkword(lsb(ww), 0)
txt.print_uwhex(ww, true)
ww = $ff34
ww = ww ^ ww >> 8
txt.print_uwhex(ww, true)
ww = $ff34
ww = ww ^ msb(ww)
txt.print_uwhex(ww, true)
}
; sub start2() {
@ -19,22 +30,3 @@ main {
; txt.print_ub(arr[3])
; }
}
optional {
%option force_output
ubyte value
sub sub1() {
value++
}
sub sub2() {
value++
}
sub sub3() {
value++
}
}