about summary refs log tree commit diff stats
path: root/miasm2/expression
diff options
context:
space:
mode:
authorserpilliere <serpilliere@users.noreply.github.com>2015-11-16 10:42:40 +0100
committerserpilliere <serpilliere@users.noreply.github.com>2015-11-16 10:42:40 +0100
commit1f892fabed6300aeb592121af5b2137ac35c07f6 (patch)
tree1ff49eb5b7fd59fcdbcd6602ba72764566113884 /miasm2/expression
parent3eed941e88361f811496311014079c172e058a68 (diff)
parent55a81cc899599ae24a4c5322a6d812a24e28a292 (diff)
downloadmiasm-1f892fabed6300aeb592121af5b2137ac35c07f6.tar.gz
miasm-1f892fabed6300aeb592121af5b2137ac35c07f6.zip
Merge pull request #273 from commial/fix-python-emul
Fix python emul
Diffstat (limited to '')
-rw-r--r--miasm2/expression/modint.py8
-rw-r--r--miasm2/expression/simplifications_common.py33
2 files changed, 37 insertions, 4 deletions
diff --git a/miasm2/expression/modint.py b/miasm2/expression/modint.py
index 8bad5ccb..76896eb9 100644
--- a/miasm2/expression/modint.py
+++ b/miasm2/expression/modint.py
@@ -44,9 +44,9 @@ class moduint(object):
     def __div__(self, y):
         if isinstance(y, moduint):
             cls = self.maxcast(y)
-            return cls(self.arg / y.arg)
+            return cls(int(float(self.arg) / y.arg))
         else:
-            return self.__class__(self.arg / y)
+            return self.__class__(int(float(self.arg) / y))
 
     def __int__(self):
         return int(self.arg)
@@ -67,9 +67,9 @@ class moduint(object):
     def __mod__(self, y):
         if isinstance(y, moduint):
             cls = self.maxcast(y)
-            return cls(self.arg % y.arg)
+            return cls(self.arg - (y.arg * int(float(self.arg)/y.arg)))
         else:
-            return self.__class__(self.arg % y)
+            return self.__class__(self.arg - (y * int(float(self.arg)/y)))
 
     def __mul__(self, y):
         if isinstance(y, moduint):
diff --git a/miasm2/expression/simplifications_common.py b/miasm2/expression/simplifications_common.py
index fe69d0b9..a52debe6 100644
--- a/miasm2/expression/simplifications_common.py
+++ b/miasm2/expression/simplifications_common.py
@@ -327,6 +327,39 @@ def simp_cst_propagation(e_s, e):
                 new_args[i] = ExprOp(op, *arg), bound[i][0], bound[i][1]
             return ExprCompose(new_args)
 
+    # <<<c_rez, >>>c_rez
+    if op in [">>>c_rez", "<<<c_rez"]:
+        assert len(args) == 3
+        dest, rounds, cf = args
+        # Skipped if rounds is 0
+        if (isinstance(rounds, ExprInt) and
+            int(rounds.arg) == 0):
+            return dest
+        elif all(map(lambda x: isinstance(x, ExprInt), args)):
+            # The expression can be resolved
+            tmp = int(dest.arg)
+            cf = int(cf.arg)
+            size = dest.size
+            tmp_count = (int(rounds.arg) &
+                         (0x3f if size == 64 else 0x1f)) % (size + 1)
+            if op == ">>>c_rez":
+                while (tmp_count != 0):
+                    tmp_cf = tmp & 1;
+                    tmp = (tmp >> 1) + (cf << (size - 1))
+                    cf = tmp_cf
+                    tmp_count -= 1
+                    tmp &= int(dest.mask.arg)
+            elif op == "<<<c_rez":
+                while (tmp_count != 0):
+                    tmp_cf = (tmp >> (size - 1)) & 1
+                    tmp = (tmp << 1) + cf
+                    cf = tmp_cf
+                    tmp_count -= 1
+                    tmp &= int(dest.mask.arg)
+            else:
+                raise RuntimeError("Unknown operation: %s" % op)
+            return ExprInt(tmp, size=dest.size)
+
     return ExprOp(op, *args)