optimize msb(lsw(longvar)) into @(&longvar+1)

This commit is contained in:
Irmen de Jong
2026-01-06 22:58:45 +01:00
parent bc4470aeda
commit 3a0add60fd
4 changed files with 41 additions and 29 deletions

View File

@@ -616,7 +616,7 @@ class ExpressionSimplifier(private val program: Program, private val errors: IEr
return listOf(IAstModification.ReplaceNode(functionCallExpr, cast, parent))
}
} else if(arg is FunctionCallExpression && arg.target.nameInSource == listOf("msw")) {
// lsb(msb(longvar)) --> @(&longvar+2) ; get the bank byte from a long variable
// lsb(msw(longvar)) --> @(&longvar+2) ; get the bank byte from a long variable
val longvar = arg.args[0] as? IdentifierReference
if(longvar!=null && longvar.inferType(program).isLong) {
val address = AddressOf(longvar, null, null, false, false, functionCallExpr.position)
@@ -656,6 +656,15 @@ class ExpressionSimplifier(private val program: Program, private val errors: IEr
NumericLiteral(valueDt.getOr(DataType.UBYTE).base, 0.0, arg.expression.position),
parent))
}
} else if(arg is FunctionCallExpression && arg.target.nameInSource == listOf("lsw")) {
// msb(lsw(longvar)) --> @(&longvar+1) ; get the second byte from a long variable
val longvar = arg.args[0] as? IdentifierReference
if(longvar!=null && longvar.inferType(program).isLong) {
val address = AddressOf(longvar, null, null, false, false, functionCallExpr.position)
val plus2 = BinaryExpression(address, "+", NumericLiteral(BaseDataType.UWORD, 1.0, functionCallExpr.position), functionCallExpr.position)
val memread = DirectMemoryRead(plus2, functionCallExpr.position)
return listOf(IAstModification.ReplaceNode(functionCallExpr, memread, parent))
}
} else {
if(arg is IdentifierReference && arg.nameInSource.size==2
&& arg.nameInSource[0]=="cx16" && arg.nameInSource[1].uppercase() in RegisterOrPair.names) {