diff options
Diffstat (limited to 'miasm2/expression')
| -rw-r--r-- | miasm2/expression/expression.py | 38 | ||||
| -rw-r--r-- | miasm2/expression/expression_helper.py | 9 | ||||
| -rw-r--r-- | miasm2/expression/simplifications_common.py | 22 |
3 files changed, 35 insertions, 34 deletions
diff --git a/miasm2/expression/expression.py b/miasm2/expression/expression.py index 1b9e0a91..46b7737e 100644 --- a/miasm2/expression/expression.py +++ b/miasm2/expression/expression.py @@ -288,7 +288,7 @@ class Expr(object): if self.size == size: return self ad_size = size - self.size - n = ExprInt_fromsize(ad_size, 0) + n = ExprInt(0, ad_size) return ExprCompose([(self, 0, self.size), (n, self.size, size)]) @@ -302,9 +302,8 @@ class Expr(object): ad_size = size - self.size c = ExprCompose([(self, 0, self.size), (ExprCond(self.msb(), - ExprInt_fromsize( - ad_size, size2mask(ad_size)), - ExprInt_fromsize(ad_size, 0)), + ExprInt(size2mask(ad_size), ad_size), + ExprInt(0, ad_size)), self.size, size) ]) return c @@ -331,7 +330,7 @@ class Expr(object): def set_mask(self, value): raise ValueError('mask is not mutable') - mask = property(lambda self: ExprInt_fromsize(self.size, -1)) + mask = property(lambda self: ExprInt(-1, self.size)) class ExprInt(Expr): @@ -344,15 +343,21 @@ class ExprInt(Expr): - Constant 0x12345678 on 32bits """ - def __init__(self, arg): - """Create an ExprInt from a numpy int - @arg: numpy int""" - - if not is_modint(arg): - raise ValueError('arg must by numpy int! %s' % arg) - - self._arg = arg - self._size = self.arg.size + def __init__(self, num, size=None): + """Create an ExprInt from a modint or num/size + @arg: modint or num + @size: (optionnal) int size""" + + if is_modint(num): + self._arg = num + self._size = self.arg.size + if size is not None and num.size != size: + raise RuntimeError("size must match modint size") + elif size is not None: + self._arg = mod_size2uint[size](num) + self._size = self.arg.size + else: + raise ValueError('arg must by modint or (int,size)! %s' % num) arg = property(lambda self: self._arg) @@ -1162,11 +1167,6 @@ def ExprInt_from(e, i): return ExprInt(mod_size2uint[e.size](i)) -def ExprInt_fromsize(size, i): - "Generate ExprInt with a given size" - return ExprInt(mod_size2uint[size](i)) - - def get_expr_ids_visit(e, ids): if isinstance(e, ExprId): ids.add(e) diff --git a/miasm2/expression/expression_helper.py b/miasm2/expression/expression_helper.py index 196ad5cd..ece720e0 100644 --- a/miasm2/expression/expression_helper.py +++ b/miasm2/expression/expression_helper.py @@ -43,9 +43,8 @@ def merge_sliceto_slice(args): # sources_int[a.start] = a # copy ExprInt because we will inplace modify arg just below # /!\ TODO XXX never ever modify inplace args... - sources_int[a[1]] = (m2_expr.ExprInt_fromsize(a[2] - a[1], - a[0].arg.__class__( - a[0].arg)), + sources_int[a[1]] = (m2_expr.ExprInt(int(a[0].arg), + a[2] - a[1]), a[1], a[2]) elif isinstance(a[0], m2_expr.ExprSlice): @@ -86,7 +85,7 @@ def merge_sliceto_slice(args): out[0] = m2_expr.ExprInt(a) sorted_s.pop() out[1] = s_start - out[0] = m2_expr.ExprInt_fromsize(size, out[0].arg) + out[0] = m2_expr.ExprInt(int(out[0].arg), size) final_sources.append((start, out)) final_sources_int = final_sources @@ -409,7 +408,7 @@ class ExprRandom(object): @size: (optional) number max bits """ num = random.randint(0, cls.number_max % (2**size)) - return m2_expr.ExprInt_fromsize(size, num) + return m2_expr.ExprInt(num, size) @classmethod def atomic(cls, size=32): diff --git a/miasm2/expression/simplifications_common.py b/miasm2/expression/simplifications_common.py index d89b7518..d50e81a1 100644 --- a/miasm2/expression/simplifications_common.py +++ b/miasm2/expression/simplifications_common.py @@ -76,7 +76,7 @@ def simp_cst_propagation(e_s, e): - o = ExprInt_fromsize(i1.size, o) + o = ExprInt(o, i1.size) args.append(o) # bsf(int) => int @@ -281,7 +281,7 @@ def simp_cst_propagation(e_s, e): filter_args.append((expr, start, stop)) min_index = min(start, min_index) # create entry 0 - expr = ExprInt_fromsize(min_index, 0) + expr = ExprInt(0, min_index) filter_args = [(expr, 0, min_index)] + filter_args return ExprCompose(filter_args) @@ -305,7 +305,7 @@ def simp_cst_propagation(e_s, e): filter_args.append((expr, start, stop)) max_index = max(stop, max_index) # create entry 0 - expr = ExprInt_fromsize(final_size - max_index, 0) + expr = ExprInt(0, final_size - max_index) filter_args += [(expr, max_index, final_size)] return ExprCompose(filter_args) @@ -401,7 +401,7 @@ def simp_slice(e_s, e): elif isinstance(e.arg, ExprInt): total_bit = e.stop - e.start mask = (1 << (e.stop - e.start)) - 1 - return ExprInt_fromsize(total_bit, (e.arg.arg >> e.start) & mask) + return ExprInt(int((e.arg.arg >> e.start) & mask), total_bit) # Slice(Slice(A, x), y) => Slice(A, z) elif isinstance(e.arg, ExprSlice): if e.stop - e.start > e.arg.stop - e.arg.start: @@ -511,18 +511,20 @@ def simp_compose(e_s, e): if len(args) == 1 and args[0][1] == 0 and args[0][2] == e.size: return args[0][0] - # {(X[X.size-z, 0, z), (0, z, X.size)} => (X >> x) + # {(X[z:], 0, X.size-z), (0, X.size-z, X.size)} => (X >> z) if (len(args) == 2 and isinstance(args[1][0], ExprInt) and args[1][0].arg == 0): a1 = args[0] a2 = args[1] if (isinstance(a1[0], ExprSlice) and - a1[1] == 0 and a1[0].stop == a1[0].arg.size): - if a2[1] == a1[0].size and a2[2] == a1[0].arg.size: - new_e = a1[0].arg >> ExprInt_fromsize( - a1[0].arg.size, a1[0].start) - return new_e + a1[1] == 0 and + a1[0].stop == a1[0].arg.size and + a2[1] == a1[0].size and + a2[2] == a1[0].arg.size): + new_e = a1[0].arg >> ExprInt( + a1[0].start, a1[0].arg.size) + return new_e # Compose with ExprCond with integers for src1/src2 and intergers => # propagage integers |