about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorCamille Mougey <commial@gmail.com>2015-07-24 12:05:58 +0200
committerCamille Mougey <commial@gmail.com>2015-07-24 12:05:58 +0200
commitaf4dad71abdbfdbdb4d371f0e9a4dc6ac937289c (patch)
treead7e60389b03b532b9c8eb44d484470a87b45a5f
parent97463558ea31744a24e3330ab3227e2098878d03 (diff)
parent310cc708474650a567eb099386373638b67daad7 (diff)
downloadmiasm-af4dad71abdbfdbdb4d371f0e9a4dc6ac937289c.tar.gz
miasm-af4dad71abdbfdbdb4d371f0e9a4dc6ac937289c.zip
Merge pull request #197 from serpilliere/simp_shr_slice
Simplification of shift/slice
-rw-r--r--miasm2/expression/simplifications_common.py15
-rw-r--r--test/expression/simplifications.py8
2 files changed, 23 insertions, 0 deletions
diff --git a/miasm2/expression/simplifications_common.py b/miasm2/expression/simplifications_common.py
index 82f2cf75..d89b7518 100644
--- a/miasm2/expression/simplifications_common.py
+++ b/miasm2/expression/simplifications_common.py
@@ -477,6 +477,21 @@ def simp_slice(e_s, e):
         args = [e_s.expr_simp_wrapper(a[e.start:e.stop]) for a in e.arg.args]
         e = ExprOp(e.arg.op, *args)
 
+    # (a >> int)[x:y] => a[x+int:y+int] with int+y <= a.size
+    # (a << int)[x:y] => a[x-int:y-int] with x-int >= 0
+    elif (isinstance(e.arg, ExprOp) and e.arg.op in [">>", "<<"] and
+          isinstance(e.arg.args[1], ExprInt)):
+        arg, shift = e.arg.args
+        shift = int(shift.arg)
+        if e.arg.op == ">>":
+            if shift + e.stop <= arg.size:
+                return arg[e.start + shift:e.stop + shift]
+        elif e.arg.op == "<<":
+            if e.start - shift >= 0:
+                return arg[e.start - shift:e.stop - shift]
+        else:
+            raise ValueError('Bad case')
+
     return e
 
 
diff --git a/test/expression/simplifications.py b/test/expression/simplifications.py
index df7fdceb..1f5a5c5b 100644
--- a/test/expression/simplifications.py
+++ b/test/expression/simplifications.py
@@ -256,6 +256,14 @@ to_test = [(ExprInt32(1) - ExprInt32(1), ExprInt32(0)),
        ),
     (ExprCompose([(a[:16], 0, 16), (b[:16], 16, 32)])[8:32],
      ExprCompose([(a[8:16], 0, 8), (b[:16], 8, 24)])),
+    ((a >> ExprInt32(16))[:16],
+     a[16:32]),
+    ((a >> ExprInt32(16))[8:16],
+     a[24:32]),
+    ((a << ExprInt32(16))[16:32],
+     a[:16]),
+    ((a << ExprInt32(16))[24:32],
+     a[8:16]),
 
 ]