about summary refs log tree commit diff stats
path: root/miasm/expression/expression_helper.py
diff options
context:
space:
mode:
authorserpilliere <devnull@localhost>2012-06-21 10:08:40 +0200
committerserpilliere <devnull@localhost>2012-06-21 10:08:40 +0200
commit50c79519af9c768eb1cddc02792b5c68a433bd4c (patch)
tree8f0341ce93bf9409d12cb067c04bc8f04211478b /miasm/expression/expression_helper.py
parent662b6860e39e15e79940b92868eb2b5b659af26c (diff)
downloadfocaccia-miasm-50c79519af9c768eb1cddc02792b5c68a433bd4c.tar.gz
focaccia-miasm-50c79519af9c768eb1cddc02792b5c68a433bd4c.zip
expression_helper: add exprcond simpl
Diffstat (limited to 'miasm/expression/expression_helper.py')
-rw-r--r--miasm/expression/expression_helper.py21
1 files changed, 18 insertions, 3 deletions
diff --git a/miasm/expression/expression_helper.py b/miasm/expression/expression_helper.py
index 94b6f32b..ffa8cbd3 100644
--- a/miasm/expression/expression_helper.py
+++ b/miasm/expression/expression_helper.py
@@ -130,7 +130,11 @@ def merge_sliceto_slice(args):
 
 op_assoc = ['+', '*', '^', '&', '|']
 
+
 def expr_simp(e):
+    return e.visit(_expr_simp)
+
+def _expr_simp(e):
     if isinstance(e, ExprOp):
         # merge associatif op
         # ((a+b) + c) => (a + b + c)
@@ -168,7 +172,6 @@ def expr_simp(e):
 
                 o = ExprInt(tab_size_int[i1.get_size()](o))
                 args.append(o)
-
         # --(A) => A
         if op == '-' and len(args) == 1 and isinstance(args[0], ExprOp) and \
                 args[0].op == '-' and len(args[0].args) == 1:
@@ -243,7 +246,7 @@ def expr_simp(e):
             args = [args0, args1]
 
 
-        #! (!X + int) => X - int
+        # ! (!X + int) => X - int
         # TODO
 
         # ((A & mask) >> shift) whith mask < 2**shift => 0
@@ -297,7 +300,7 @@ def expr_simp(e):
                 if a[1] <= e.start and a[2]>=e.stop:
                     new_e = a[0][e.start-a[1]:e.stop-a[1]]
                     return new_e
-        #XXXX todo hum, is it safe?
+        # XXXX todo hum, is it safe?
         elif isinstance(e.arg, ExprMem) and e.start == 0 and e.arg.size > e.stop and e.stop %8 == 0:
             e = ExprMem(e.arg.arg, size = e.stop)
             return e
@@ -312,6 +315,18 @@ def expr_simp(e):
 
         return ExprCompose(args)
 
+
+    elif isinstance(e, ExprCond):
+        # -A ? B:C => A ? B:C
+        if isinstance(e.cond, ExprOp) and e.cond.op == '-' and len(e.cond.args) == 1:
+            e = ExprCond(e.cond.args[0], e.src1, e.src2)
+        # int ? A:B => A or B
+        elif isinstance(e.cond, ExprInt):
+            if e.cond.arg == 0:
+                e = e.src2
+            else:
+                e = e.src1
+        return e
     else:
         return e