diff options
| -rw-r--r-- | miasm/expression/simplifications.py | 2 | ||||
| -rw-r--r-- | miasm/expression/simplifications_common.py | 22 | ||||
| -rw-r--r-- | test/expression/simplifications.py | 6 |
3 files changed, 28 insertions, 2 deletions
diff --git a/miasm/expression/simplifications.py b/miasm/expression/simplifications.py index 585a9c6b..8f63ab91 100644 --- a/miasm/expression/simplifications.py +++ b/miasm/expression/simplifications.py @@ -58,7 +58,7 @@ class ExpressionSimplifier(object): simplifications_common.simp_test_signext_inf, simplifications_common.simp_test_zeroext_inf, simplifications_common.simp_cond_inf_eq_unsigned_zero, - + simplifications_common.simp_compose_and_mask, ], m2_expr.ExprSlice: [ diff --git a/miasm/expression/simplifications_common.py b/miasm/expression/simplifications_common.py index cda9c5e2..ddd21a63 100644 --- a/miasm/expression/simplifications_common.py +++ b/miasm/expression/simplifications_common.py @@ -607,7 +607,6 @@ def simp_compose(e_s, expr): return ExprCond(cond, arg1, arg2) return ExprCompose(*args) - def simp_cond(_, expr): """ Common simplifications on ExprCond. @@ -1554,3 +1553,24 @@ def simp_add_multiple(_, expr): if len(out) == 1: return out[0] return ExprOp('+', *out) + +def simp_compose_and_mask(_, expr): + """ + {X 0 8, Y 8 16} & 0xFF => X + {X 0 32} & 0xFFFF => X[0:16] + {X 0 8, Y 8 24, Z 24 32} & 0xFFFFFF => X|Y + """ + if not expr.is_op('&'): + return expr + # handle the case where arg2 = arg1.mask + if len(expr.args) != 2: + return expr + arg1, arg2 = expr.args + if not arg1.is_compose(): + return expr + if not arg2.is_int(): + return expr + for i in range(8, arg1.size, 8): + if arg2.arg == ExprInt(0, i).mask.arg: + return ExprSlice(arg1, 0, i) + return expr diff --git a/test/expression/simplifications.py b/test/expression/simplifications.py index 1a22c43d..ddaa2f20 100644 --- a/test/expression/simplifications.py +++ b/test/expression/simplifications.py @@ -192,6 +192,12 @@ to_test = [(ExprInt(1, 32) - ExprInt(1, 32), ExprInt(0, 32)), ExprOp('&', a, ExprInt(0x0FFFFFFF, 32))), (ExprOp('<<', ExprOp('>>', a, ExprInt(0x4, 32)), ExprInt(0x4, 32)), ExprOp('&', a, ExprInt(0xFFFFFFF0, 32))), + + (ExprCompose(ExprInt(0x1234, 16), ExprId("a", 16)) & ExprInt(0xFF, 32), ExprInt(0x34, 8)), + (ExprCompose(ExprInt(0x12, 8), ExprInt(0x34, 8)) & ExprInt(0xFFFF, 16), ExprInt(0x3412, 16)), + (ExprCompose(ExprInt(0x12, 8), ExprInt(0x3456, 16), ExprInt(0x78, 8)) & ExprInt(0xFFFFFF, 32), ExprInt(0x345612, 32)), + (ExprCompose(ExprInt(0x1234, 16), ExprId("a", 8), ExprInt(0x67, 8)) & ExprInt(0xFFFFFFFF, 32), ExprCompose(ExprInt(0x1234, 16), ExprId("a", 8), ExprInt(0x67, 8))), + (a[:32], a), (a[:8][:8], a[:8]), (a[:16][:8], a[:8]), |