optimize w=msb(w) => w>>=8, w=lsb(w) ==> w&=$00ff

This commit is contained in:
Irmen de Jong 2022-01-28 15:22:26 +01:00
parent 1815cb1bc3
commit ec078eba72
3 changed files with 42 additions and 25 deletions

View File

@ -431,6 +431,28 @@ class StatementOptimizer(private val program: Program,
} }
} }
// word = msb(word) , word=lsb(word)
if(assignment.target.inferType(program).isWords) {
var fcall = assignment.value as? FunctionCallExpression
if (fcall == null)
fcall = (assignment.value as? TypecastExpression)?.expression as? FunctionCallExpression
if (fcall != null && (fcall.target.nameInSource == listOf("lsb") || fcall.target.nameInSource == listOf("msb"))) {
if (fcall.args.single() isSameAs assignment.target) {
return if (fcall.target.nameInSource == listOf("lsb")) {
// optimize word=lsb(word) ==> word &= $00ff
val and255 = BinaryExpression(fcall.args[0], "&", NumericLiteralValue(DataType.UWORD, 255.0, fcall.position), fcall.position)
val newAssign = Assignment(assignment.target, and255, AssignmentOrigin.OPTIMIZER, fcall.position)
listOf(IAstModification.ReplaceNode(assignment, newAssign, parent))
} else {
// optimize word=msb(word) ==> word >>= 8
val shift8 = BinaryExpression(fcall.args[0], ">>", NumericLiteralValue(DataType.UBYTE, 8.0, fcall.position), fcall.position)
val newAssign = Assignment(assignment.target, shift8, AssignmentOrigin.OPTIMIZER, fcall.position)
listOf(IAstModification.ReplaceNode(assignment, newAssign, parent))
}
}
}
}
return noModifications return noModifications
} }

View File

@ -3,8 +3,6 @@ TODO
For next release For next release
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^
- optimize w=msb(w) => w >>=8, w=lsb(w) ==> w &= $00ff
fix the value of ww being wrong (with optimizations enabled) in : fix the value of ww being wrong (with optimizations enabled) in :
sub start() { sub start() {
byte ub1 = -50 byte ub1 = -50
@ -20,9 +18,6 @@ fix the value of ww being wrong (with optimizations enabled) in :
} }
...
Need help with Need help with
^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
- c128 target: various machine specific things (free zp locations, how banking works, getting the floating point routines working, ...) - c128 target: various machine specific things (free zp locations, how banking works, getting the floating point routines working, ...)

View File

@ -3,25 +3,25 @@
main { main {
sub start() { sub start() {
uword @shared xx=$ea31 byte bb1 = -50
;xx &= $00ff byte bb2 = -51
;xx = lsb(xx)
;txt.print_uwhex(xx, true) word ww = func(bb1, bb2)
;xx = $ea31 txt.print_w(ww)
;xx &= $ff00 txt.print(" <- must be -50\n") ; TODO fix this with noopt (prints 0) !
; xx = msb(xx)
; %asm {{ ubyte ub1 = 50
; nop ubyte ub2 = 51
; nop uword uw = funcu(ub1, ub2)
; }} txt.print_uw(uw)
xx >>= 8 txt.print(" <- must be 50\n") ; TODO fix this with noopt (prints 0) !
%asm {{ }
nop
}} sub func(word x1, word y1) -> word {
xx <<= 8 return x1
%asm {{ }
nop
}} sub funcu(uword x1, uword y1) -> uword {
txt.print_uwhex(xx, true) return x1
} }
} }