diff options
| author | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2018-10-09 07:40:52 +0200 |
|---|---|---|
| committer | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2018-10-09 08:17:50 +0200 |
| commit | e5c439d3b4ee7120fae3d15ae7106cce8d0dcbde (patch) | |
| tree | 4cd9776c937960779dd213f20a002a8a8880b92a /miasm2/expression/simplifications_common.py | |
| parent | a11964b49954c7bba71264b5b27e31cec01e8481 (diff) | |
| download | miasm-e5c439d3b4ee7120fae3d15ae7106cce8d0dcbde.tar.gz miasm-e5c439d3b4ee7120fae3d15ae7106cce8d0dcbde.zip | |
Simplifications: simplify by default high level op
Simplifify by default high level operators (<u, ...) with pure cst as arguments
Diffstat (limited to 'miasm2/expression/simplifications_common.py')
| -rw-r--r-- | miasm2/expression/simplifications_common.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/miasm2/expression/simplifications_common.py b/miasm2/expression/simplifications_common.py index 2eeabd9b..676501a0 100644 --- a/miasm2/expression/simplifications_common.py +++ b/miasm2/expression/simplifications_common.py @@ -1060,3 +1060,73 @@ def simp_cond_eq_zero(expr_s, expr): return expr new_expr = ExprCond(arg1, expr.src2, expr.src1) return new_expr + + +def simp_cmp_int_int(expr_s, expr): + # IntA <s IntB => int + # IntA <u IntB => int + # IntA <=s IntB => int + # IntA <=u IntB => int + # IntA == IntB => int + if expr.op not in [ + TOK_EQUAL, + TOK_INF_SIGNED, TOK_INF_UNSIGNED, + TOK_INF_EQUAL_SIGNED, TOK_INF_EQUAL_UNSIGNED, + ]: + return expr + if not all(arg.is_int() for arg in expr.args): + return expr + int_a, int_b = expr.args + if expr.is_op(TOK_EQUAL): + if int_a == int_b: + return ExprInt(1, 1) + else: + return ExprInt(0, 1) + + if expr.op in [TOK_INF_SIGNED, TOK_INF_EQUAL_SIGNED]: + int_a = int(mod_size2int[int_a.size](int(int_a))) + int_b = int(mod_size2int[int_b.size](int(int_b))) + else: + int_a = int(mod_size2uint[int_a.size](int(int_a))) + int_b = int(mod_size2uint[int_b.size](int(int_b))) + + if expr.op in [TOK_INF_SIGNED, TOK_INF_UNSIGNED]: + ret = int_a < int_b + else: + ret = int_a <= int_b + + if ret: + ret = 1 + else: + ret = 0 + return ExprInt(ret, 1) + + +def simp_ext_cst(expr_s, expr): + # Int.zeroExt(X) => Int + # Int.signExt(X) => Int + if not (expr.op.startswith("zeroExt") or expr.op.startswith("signExt")): + return expr + arg = expr.args[0] + if not arg.is_int(): + return expr + if expr.op.startswith("zeroExt"): + ret = int(arg) + else: + ret = int(mod_size2int[arg.size](int(arg))) + ret = ExprInt(ret, expr.size) + return ret + + +def simp_slice_of_ext(expr_s, expr): + # zeroExt(X)[0:size(X)] => X + if expr.start != 0: + return expr + if not expr.arg.is_op(): + return expr + if not expr.arg.op.startswith("zeroExt"): + return expr + arg = expr.arg.args[0] + if arg.size != expr.size: + return expr + return arg |