about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorajax <devnull@localhost>2014-06-12 18:51:59 +0200
committerajax <devnull@localhost>2014-06-12 18:51:59 +0200
commita635b0185b9fe26453ceedb5d56aa9d59503b695 (patch)
treec5804894924f1d509c07bb492613d573f5e8e93d
parent0698cd3820a70182c362ba113849fe8ca5e0e032 (diff)
downloadmiasm-a635b0185b9fe26453ceedb5d56aa9d59503b695.tar.gz
miasm-a635b0185b9fe26453ceedb5d56aa9d59503b695.zip
Simplifications: Add symetric cond catching + corresponding tests
-rw-r--r--miasm2/expression/simplifications_cond.py40
-rw-r--r--test/expression/simplifications.py2
2 files changed, 40 insertions, 2 deletions
diff --git a/miasm2/expression/simplifications_cond.py b/miasm2/expression/simplifications_cond.py
index 8f41975b..ee23f6db 100644
--- a/miasm2/expression/simplifications_cond.py
+++ b/miasm2/expression/simplifications_cond.py
@@ -33,6 +33,8 @@ TOK_POS_STRICT = "Spos"
 jok1 = m2_expr.ExprId("jok1")
 jok2 = m2_expr.ExprId("jok2")
 jok3 = m2_expr.ExprId("jok3")
+jok_small = m2_expr.ExprId("jok_small", 1)
+
 
 # Constructors
 
@@ -134,10 +136,44 @@ def expr_simp_inverse(expr_simp, e):
     """(x <u y) ^ ((x ^ y) [31:32]) == x <s y,
     (x <s y) ^ ((x ^ y) [31:32]) == x <u y"""
 
-    if e.op != '^' or len(e.args) != 2:
+    to_match = (ExprOp_inf_unsigned(jok1, jok2) ^ jok_small)
+    r = __MatchExprWrap(e,
+                        to_match,
+                        [jok1, jok2, jok_small])
+
+    # Check for 2 symetric cases
+    if r is False:
+            to_match = (ExprOp_inf_signed(jok1, jok2) ^ jok_small)
+            r = __MatchExprWrap(e,
+                                to_match,
+                                [jok1, jok2, jok_small])
+
+            if r is False:
+                return e
+            cur_sig = TOK_INF_SIGNED
+    else:
+        cur_sig = TOK_INF_UNSIGNED
+
+
+    arg = __check_msb(r[jok_small])
+    if arg is False:
+        return e
+
+    if not isinstance(arg, m2_expr.ExprOp) or arg.op != "^":
+        return e
+
+    op_args = arg.args
+    if len(op_args) != 2:
+        return e
+
+    if r[jok1] not in op_args or r[jok2] not in op_args:
         return e
 
-    return e # TODO: Not Implemented
+    if cur_sig == TOK_INF_UNSIGNED:
+        return ExprOp_inf_signed(r[jok1], r[jok2])
+    else:
+        return ExprOp_inf_unsigned(r[jok1], r[jok2])
+
 
 # Compute conditions
 
diff --git a/test/expression/simplifications.py b/test/expression/simplifications.py
index 057ff1c7..ac6b796d 100644
--- a/test/expression/simplifications.py
+++ b/test/expression/simplifications.py
@@ -183,6 +183,8 @@ to_test = [
      ExprOp_inf_unsigned(a, b)),
     (ExprOp_inf_unsigned(ExprInt32(-1), ExprInt32(3)), ExprInt1(0)),
     (ExprOp_inf_signed(ExprInt32(-1), ExprInt32(3)), ExprInt1(1)),
+    (ExprOp_inf_unsigned(a, b) ^ (a ^ b).msb(), ExprOp_inf_signed(a, b)),
+    (ExprOp_inf_signed(a, b) ^ (a ^ b).msb(), ExprOp_inf_unsigned(a, b)),
 ]
 
 expr_simp_cond = ExpressionSimplifier()