diff options
| author | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2018-08-29 18:09:52 +0200 |
|---|---|---|
| committer | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2018-08-29 18:49:47 +0200 |
| commit | caec47dc1871782ca445ea34229cd2a5ee600a7f (patch) | |
| tree | ca23e85b3e65801f4680657d10e853661da2b114 | |
| parent | 60f3cb9a31a86b28eeb5abd357608f91652cb75e (diff) | |
| download | miasm-caec47dc1871782ca445ea34229cd2a5ee600a7f.tar.gz miasm-caec47dc1871782ca445ea34229cd2a5ee600a7f.zip | |
Expr: use TOK define instead of strings
| -rw-r--r-- | miasm2/arch/mips32/sem.py | 28 | ||||
| -rw-r--r-- | miasm2/expression/expression.py | 14 | ||||
| -rw-r--r-- | miasm2/expression/expression_helper.py | 2 | ||||
| -rw-r--r-- | miasm2/expression/simplifications_common.py | 101 | ||||
| -rw-r--r-- | miasm2/expression/simplifications_explicit.py | 5 | ||||
| -rw-r--r-- | miasm2/ir/translators/C.py | 5 | ||||
| -rwxr-xr-x | test/ir/ir2C.py | 4 |
7 files changed, 98 insertions, 61 deletions
diff --git a/miasm2/arch/mips32/sem.py b/miasm2/arch/mips32/sem.py index 695e26a4..92001280 100644 --- a/miasm2/arch/mips32/sem.py +++ b/miasm2/arch/mips32/sem.py @@ -7,10 +7,14 @@ from miasm2.jitter.csts import EXCEPT_DIV_BY_ZERO # SemBuilder context -ctx = {"R_LO": R_LO, - "R_HI": R_HI, - "PC": PC, - "RA": RA} +ctx = { + "R_LO": R_LO, + "R_HI": R_HI, + "PC": PC, + "RA": RA, + "m2_expr": m2_expr +} + sbuild = SemBuilder(ctx) @@ -76,7 +80,7 @@ def lb(arg1, arg2): @sbuild.parse def beq(arg1, arg2, arg3): "Branches on @arg3 if the quantities of two registers @arg1, @arg2 are eq" - dst = arg3 if ExprOp("==", arg1, arg2) else ExprLoc(ir.get_next_break_loc_key(instr), ir.IRDst.size) + dst = arg3 if ExprOp(m2_expr.TOK_EQUAL, arg1, arg2) else ExprLoc(ir.get_next_break_loc_key(instr), ir.IRDst.size) PC = dst ir.IRDst = dst @@ -84,7 +88,7 @@ def beq(arg1, arg2, arg3): def bgez(arg1, arg2): """Branches on @arg2 if the quantities of register @arg1 is greater than or equal to zero""" - dst = ExprLoc(ir.get_next_break_loc_key(instr), ir.IRDst.size) if ExprOp('<s', arg1, ExprInt(0, arg1.size)) else arg2 + dst = ExprLoc(ir.get_next_break_loc_key(instr), ir.IRDst.size) if ExprOp(m2_expr.TOK_INF_SIGNED, arg1, ExprInt(0, arg1.size)) else arg2 PC = dst ir.IRDst = dst @@ -92,7 +96,7 @@ def bgez(arg1, arg2): def bne(arg1, arg2, arg3): """Branches on @arg3 if the quantities of two registers @arg1, @arg2 are NOT equal""" - dst = ExprLoc(ir.get_next_break_loc_key(instr), ir.IRDst.size) if ExprOp('==', arg1, arg2) else arg3 + dst = ExprLoc(ir.get_next_break_loc_key(instr), ir.IRDst.size) if ExprOp(m2_expr.TOK_EQUAL, arg1, arg2) else arg3 PC = dst ir.IRDst = dst @@ -146,7 +150,7 @@ def sltu(arg1, arg2, arg3): """If @arg2 is less than @arg3 (unsigned), @arg1 is set to one. It gets zero otherwise.""" arg1 = ExprCond( - ExprOp("<u", arg2, arg3), + ExprOp(m2_expr.TOK_INF_UNSIGNED, arg2, arg3), ExprInt(1, arg1.size), ExprInt(0, arg1.size) ) @@ -156,7 +160,7 @@ def slt(arg1, arg2, arg3): """If @arg2 is less than @arg3 (signed), @arg1 is set to one. It gets zero otherwise.""" arg1 = ExprCond( - ExprOp("<s", arg2, arg3), + ExprOp(m2_expr.TOK_INF_SIGNED, arg2, arg3), ExprInt(1, arg1.size), ExprInt(0, arg1.size) ) @@ -239,14 +243,14 @@ def seh(arg1, arg2): @sbuild.parse def bltz(arg1, arg2): """Branches on @arg2 if the register @arg1 is less than zero""" - dst_o = arg2 if ExprOp('<s', arg1, ExprInt(0, arg1.size)) else ExprLoc(ir.get_next_break_loc_key(instr), ir.IRDst.size) + dst_o = arg2 if ExprOp(m2_expr.TOK_INF_SIGNED, arg1, ExprInt(0, arg1.size)) else ExprLoc(ir.get_next_break_loc_key(instr), ir.IRDst.size) PC = dst_o ir.IRDst = dst_o @sbuild.parse def blez(arg1, arg2): """Branches on @arg2 if the register @arg1 is less than or equal to zero""" - cond = ExprOp("<=s", arg1, ExprInt(0, arg1.size)) + cond = ExprOp(m2_expr.TOK_INF_EQUAL_SIGNED, arg1, ExprInt(0, arg1.size)) dst_o = arg2 if cond else ExprLoc(ir.get_next_break_loc_key(instr), ir.IRDst.size) PC = dst_o ir.IRDst = dst_o @@ -254,7 +258,7 @@ def blez(arg1, arg2): @sbuild.parse def bgtz(arg1, arg2): """Branches on @arg2 if the register @arg1 is greater than zero""" - cond = ExprOp("<=s", arg1, ExprInt(0, arg1.size)) + cond = ExprOp(m2_expr.TOK_INF_EQUAL_SIGNED, arg1, ExprInt(0, arg1.size)) dst_o = ExprLoc(ir.get_next_break_loc_key(instr), ir.IRDst.size) if cond else arg2 PC = dst_o ir.IRDst = dst_o diff --git a/miasm2/expression/expression.py b/miasm2/expression/expression.py index 954ba00a..ec423f83 100644 --- a/miasm2/expression/expression.py +++ b/miasm2/expression/expression.py @@ -1009,7 +1009,7 @@ class ExprOp(Expr): # Set size for special cases if self._op in [ - '==', 'parity', 'fcom_c0', 'fcom_c1', 'fcom_c2', 'fcom_c3', + TOK_EQUAL, 'parity', 'fcom_c0', 'fcom_c1', 'fcom_c2', 'fcom_c3', 'fxam_c0', 'fxam_c1', 'fxam_c2', 'fxam_c3', "access_segment_ok", "load_segment_limit_ok", "bcdadd_cf", "ucomiss_zf", "ucomiss_pf", "ucomiss_cf", @@ -1102,9 +1102,15 @@ class ExprOp(Expr): return self._op.startswith('call') def is_infix(self): - return self._op in [ '-', '+', '*', '^', '&', '|', '>>', '<<', - 'a>>', '>>>', '<<<', '/', '%', '**', - '<u', '<s', '<=u', '<=s', '==' ] + return self._op in [ + '-', '+', '*', '^', '&', '|', '>>', '<<', + 'a>>', '>>>', '<<<', '/', '%', '**', + TOK_INF_UNSIGNED, + TOK_INF_SIGNED, + TOK_INF_EQUAL_UNSIGNED, + TOK_INF_EQUAL_SIGNED, + TOK_EQUAL + ] def is_associative(self): "Return True iff current operation is associative" diff --git a/miasm2/expression/expression_helper.py b/miasm2/expression/expression_helper.py index bb0d5adf..5de9e04f 100644 --- a/miasm2/expression/expression_helper.py +++ b/miasm2/expression/expression_helper.py @@ -512,7 +512,7 @@ class CondConstraint(object): class CondConstraintZero(CondConstraint): """Stand for a constraint like 'A == 0'""" - operator = "==" + operator = m2_expr.TOK_EQUAL def to_constraint(self): return m2_expr.ExprAff(self.expr, m2_expr.ExprInt(0, self.expr.size)) diff --git a/miasm2/expression/simplifications_common.py b/miasm2/expression/simplifications_common.py index 2bdccdbd..e7dacc91 100644 --- a/miasm2/expression/simplifications_common.py +++ b/miasm2/expression/simplifications_common.py @@ -4,8 +4,11 @@ from miasm2.expression.modint import mod_size2int, mod_size2uint -from miasm2.expression.expression import ExprInt, ExprSlice, ExprMem, ExprCond, ExprOp, ExprCompose -from miasm2.expression.expression_helper import parity, op_propag_cst, merge_sliceto_slice +from miasm2.expression.expression import ExprInt, ExprSlice, ExprMem, \ + ExprCond, ExprOp, ExprCompose, TOK_INF_SIGNED, TOK_INF_UNSIGNED, \ + TOK_INF_EQUAL_SIGNED, TOK_INF_EQUAL_UNSIGNED, TOK_EQUAL +from miasm2.expression.expression_helper import parity, op_propag_cst, \ + merge_sliceto_slice def simp_cst_propagation(e_s, expr): @@ -692,7 +695,7 @@ def simp_cc_conds(expr_simp, expr): "FLAG_SUB_CF" )): expr = ExprCond( - ExprOp("<u", *expr.args[0].args), + ExprOp(TOK_INF_UNSIGNED, *expr.args[0].args), ExprInt(0, 1), ExprInt(1, 1)) @@ -701,14 +704,14 @@ def simp_cc_conds(expr_simp, expr): expr, "FLAG_SUB_CF" )): - expr = ExprOp("<u", *expr.args[0].args) + expr = ExprOp(TOK_INF_UNSIGNED, *expr.args[0].args) elif (expr.is_op("CC_NEG") and test_cc_eq_args( expr, "FLAG_SIGN_SUB" )): - expr = ExprOp("<s", *expr.args[0].args) + expr = ExprOp(TOK_INF_SIGNED, *expr.args[0].args) elif (expr.is_op("CC_POS") and test_cc_eq_args( @@ -716,7 +719,7 @@ def simp_cc_conds(expr_simp, expr): "FLAG_SIGN_SUB" )): expr = ExprCond( - ExprOp("<s", *expr.args[0].args), + ExprOp(TOK_INF_SIGNED, *expr.args[0].args), ExprInt(0, 1), ExprInt(1, 1) ) @@ -727,7 +730,7 @@ def simp_cc_conds(expr_simp, expr): "FLAG_EQ" )): arg = expr.args[0].args[0] - expr = ExprOp("==", arg, ExprInt(0, arg.size)) + expr = ExprOp(TOK_EQUAL, arg, ExprInt(0, arg.size)) elif (expr.is_op("CC_NE") and test_cc_eq_args( @@ -736,7 +739,7 @@ def simp_cc_conds(expr_simp, expr): )): arg = expr.args[0].args[0] expr = ExprCond( - ExprOp("==",arg, ExprInt(0, arg.size)), + ExprOp(TOK_EQUAL,arg, ExprInt(0, arg.size)), ExprInt(0, 1), ExprInt(1, 1) ) @@ -746,7 +749,7 @@ def simp_cc_conds(expr_simp, expr): "FLAG_EQ_CMP" )): expr = ExprCond( - ExprOp("==", *expr.args[0].args), + ExprOp(TOK_EQUAL, *expr.args[0].args), ExprInt(0, 1), ExprInt(1, 1) ) @@ -756,7 +759,7 @@ def simp_cc_conds(expr_simp, expr): expr, "FLAG_EQ_CMP" )): - expr = ExprOp("==", *expr.args[0].args) + expr = ExprOp(TOK_EQUAL, *expr.args[0].args) elif (expr.is_op("CC_NE") and test_cc_eq_args( @@ -784,7 +787,7 @@ def simp_cc_conds(expr_simp, expr): "FLAG_EQ_CMP", )): expr = ExprCond( - ExprOp("<=s", *expr.args[0].args), + ExprOp(TOK_INF_EQUAL_SIGNED, *expr.args[0].args), ExprInt(0, 1), ExprInt(1, 1) ) @@ -796,7 +799,7 @@ def simp_cc_conds(expr_simp, expr): expr.args[0].args == expr.args[2].args and expr.args[1].is_int(0)): expr = ExprCond( - ExprOp("<=s", *expr.args[0].args), + ExprOp(TOK_INF_EQUAL_SIGNED, *expr.args[0].args), ExprInt(0, 1), ExprInt(1, 1) ) @@ -810,7 +813,7 @@ def simp_cc_conds(expr_simp, expr): "FLAG_SUB_OF" )): expr = ExprCond( - ExprOp("<s", *expr.args[0].args), + ExprOp(TOK_INF_SIGNED, *expr.args[0].args), ExprInt(0, 1), ExprInt(1, 1) ) @@ -821,7 +824,7 @@ def simp_cc_conds(expr_simp, expr): "FLAG_SIGN_SUB", "FLAG_SUB_OF" )): - expr = ExprOp("<s", *expr.args[0].args) + expr = ExprOp(TOK_INF_SIGNED, *expr.args[0].args) elif (expr.is_op("CC_S<=") and test_cc_eq_args( @@ -830,7 +833,7 @@ def simp_cc_conds(expr_simp, expr): "FLAG_SUB_OF", "FLAG_EQ_CMP", )): - expr = ExprOp("<=s", *expr.args[0].args) + expr = ExprOp(TOK_INF_EQUAL_SIGNED, *expr.args[0].args) elif (expr.is_op("CC_S<=") and len(expr.args) == 3 and @@ -838,7 +841,7 @@ def simp_cc_conds(expr_simp, expr): expr.args[2].is_op("FLAG_EQ_CMP") and expr.args[0].args == expr.args[2].args and expr.args[1].is_int(0)): - expr = ExprOp("<=s", *expr.args[0].args) + expr = ExprOp(TOK_INF_EQUAL_SIGNED, *expr.args[0].args) elif (expr.is_op("CC_U<=") and test_cc_eq_args( @@ -846,7 +849,7 @@ def simp_cc_conds(expr_simp, expr): "FLAG_SUB_CF", "FLAG_EQ_CMP", )): - expr = ExprOp("<=u", *expr.args[0].args) + expr = ExprOp(TOK_INF_EQUAL_UNSIGNED, *expr.args[0].args) elif (expr.is_op("CC_U>") and test_cc_eq_args( @@ -855,7 +858,7 @@ def simp_cc_conds(expr_simp, expr): "FLAG_EQ_CMP", )): expr = ExprCond( - ExprOp("<=u", *expr.args[0].args), + ExprOp(TOK_INF_EQUAL_UNSIGNED, *expr.args[0].args), ExprInt(0, 1), ExprInt(1, 1) ) @@ -867,7 +870,7 @@ def simp_cc_conds(expr_simp, expr): "FLAG_ADD_OF" )): arg0, arg1 = expr.args[0].args - expr = ExprOp("<s", arg0, -arg1) + expr = ExprOp(TOK_INF_SIGNED, arg0, -arg1) return expr @@ -877,12 +880,12 @@ def simp_cond_flag(expr_simp, expr): # FLAG_EQ_CMP(X, Y)?A:B => (X == Y)?A:B cond = expr.cond if cond.is_op("FLAG_EQ_CMP"): - return ExprCond(ExprOp("==", *cond.args), expr.src1, expr.src2) + return ExprCond(ExprOp(TOK_EQUAL, *cond.args), expr.src1, expr.src2) return expr def simp_cond_int(expr_simp, expr): - if (expr.cond.is_op('==') and + if (expr.cond.is_op(TOK_EQUAL) and expr.cond.args[1].is_int() and expr.cond.args[0].is_compose() and len(expr.cond.args[0].args) == 2 and @@ -891,9 +894,20 @@ def simp_cond_int(expr_simp, expr): src = expr.cond.args[0].args[0] int_val = int(expr.cond.args[1]) new_int = ExprInt(int_val, src.size) - expr = expr_simp(ExprCond(ExprOp("==", src, new_int), expr.src1, expr.src2)) + expr = expr_simp( + ExprCond( + ExprOp(TOK_EQUAL, src, new_int), + expr.src1, + expr.src2) + ) elif (expr.cond.is_op() and - expr.cond.op in ['==', '<s', '<=s', '<u', '<=u'] and + expr.cond.op in [ + TOK_EQUAL, + TOK_INF_SIGNED, + TOK_INF_EQUAL_SIGNED, + TOK_INF_UNSIGNED, + TOK_INF_EQUAL_UNSIGNED + ] and expr.cond.args[1].is_int() and expr.cond.args[0].is_op("+") and expr.cond.args[0].args[-1].is_int()): @@ -905,7 +919,12 @@ def simp_cond_int(expr_simp, expr): else: left = ExprOp('+', *left) new_int = expr_simp(right - int_diff) - expr = expr_simp(ExprCond(ExprOp(expr.cond.op, left, new_int), expr.src1, expr.src2)) + expr = expr_simp( + ExprCond( + ExprOp(expr.cond.op, left, new_int), + expr.src1, + expr.src2) + ) return expr @@ -920,7 +939,13 @@ def simp_cmp_int_arg(expr_simp, expr): if not cond.is_op(): return expr op = cond.op - if op not in ['==', '<s', '<=s', '<u', '<=u']: + if op not in [ + TOK_EQUAL, + TOK_INF_SIGNED, + TOK_INF_EQUAL_SIGNED, + TOK_INF_UNSIGNED, + TOK_INF_EQUAL_UNSIGNED + ]: return expr arg1, arg2 = cond.args if arg2.is_int(): @@ -928,19 +953,19 @@ def simp_cmp_int_arg(expr_simp, expr): if not arg1.is_int(): return expr src1, src2 = expr.src1, expr.src2 - if op == "==": - return ExprCond(ExprOp('==', arg2, arg1), src1, src2) + if op == TOK_EQUAL: + return ExprCond(ExprOp(TOK_EQUAL, arg2, arg1), src1, src2) arg1, arg2 = arg2, arg1 src1, src2 = src2, src1 - if op == '<s': - op = '<=s' - elif op == '<=s': - op = '<s' - elif op == '<u': - op = '<=u' - elif op == '<=u': - op = '<u' + if op == TOK_INF_SIGNED: + op = TOK_INF_EQUAL_SIGNED + elif op == TOK_INF_EQUAL_SIGNED: + op = TOK_INF_SIGNED + elif op == TOK_INF_UNSIGNED: + op = TOK_INF_EQUAL_UNSIGNED + elif op == TOK_INF_EQUAL_UNSIGNED: + op = TOK_INF_UNSIGNED return ExprCond(ExprOp(op, arg1, arg2), src1, src2) @@ -1009,7 +1034,7 @@ def simp_double_signext(expr_s, expr): def simp_zeroext_eq_cst(expr_s, expr): # A.zeroExt(X) == int => A == int[:A.size] - if not expr.is_op("=="): + if not expr.is_op(TOK_EQUAL): return expr arg1, arg2 = expr.args if not arg2.is_int(): @@ -1020,13 +1045,13 @@ def simp_zeroext_eq_cst(expr_s, expr): if int(arg2) > (1 << src.size): # Always false return ExprInt(0, 1) - return ExprOp("==", src, ExprInt(int(arg2), src.size)) + return ExprOp(TOK_EQUAL, src, ExprInt(int(arg2), src.size)) def simp_cond_eq_zero(expr_s, expr): # (X == 0)?(A:B) => X?(B:A) cond = expr.cond - if not cond.is_op('=='): + if not cond.is_op(TOK_EQUAL): return expr arg1, arg2 = cond.args if not arg2.is_int(0): diff --git a/miasm2/expression/simplifications_explicit.py b/miasm2/expression/simplifications_explicit.py index 34ba287e..4c5dde3e 100644 --- a/miasm2/expression/simplifications_explicit.py +++ b/miasm2/expression/simplifications_explicit.py @@ -1,5 +1,6 @@ from miasm2.expression.modint import size2mask -from miasm2.expression.expression import ExprInt, ExprCond, ExprCompose +from miasm2.expression.expression import ExprInt, ExprCond, ExprCompose, \ + TOK_EQUAL def simp_ext(_, expr): @@ -154,7 +155,7 @@ def simp_flags(_, expr): op_nf, = args return ~op_nf - elif expr.is_op("=="): + elif expr.is_op(TOK_EQUAL): arg1, arg2 = args return ExprCond( arg1 - arg2, diff --git a/miasm2/ir/translators/C.py b/miasm2/ir/translators/C.py index 89a2ac84..01aa7d7a 100644 --- a/miasm2/ir/translators/C.py +++ b/miasm2/ir/translators/C.py @@ -1,6 +1,7 @@ from miasm2.ir.translators.translator import Translator from miasm2.expression.modint import size2mask -from miasm2.expression.expression import ExprInt, ExprCond, ExprCompose +from miasm2.expression.expression import ExprInt, ExprCond, ExprCompose, \ + TOK_EQUAL def int_size_to_bn(value, size): if size < 32: @@ -243,7 +244,7 @@ class TranslatorC(Translator): raise NotImplementedError('Unknown op: %r' % expr.op) elif len(expr.args) == 2: - if expr.op == "==": + if expr.op == TOK_EQUAL: return '(((%s&%s) == (%s&%s))?1:0)' % ( self.from_expr(expr.args[0]), self._size2mask(expr.args[0].size), diff --git a/test/ir/ir2C.py b/test/ir/ir2C.py index 375dedb8..6df439c2 100755 --- a/test/ir/ir2C.py +++ b/test/ir/ir2C.py @@ -2,7 +2,7 @@ #-*- coding:utf-8 -*- import unittest - +from miasm2.expression.expression import TOK_EQUAL class TestIrIr2C(unittest.TestCase): @@ -32,7 +32,7 @@ class TestIrIr2C(unittest.TestCase): # Binary operators self.translationTest( - ExprOp('==', *args[:2]), r'(((0x0&0xffffffff) == (0x1&0xffffffff))?1:0)') + ExprOp(TOK_EQUAL, *args[:2]), r'(((0x0&0xffffffff) == (0x1&0xffffffff))?1:0)') self.translationTest( ExprOp('%', *args[:2]), r'(((0x0&0xffffffff)%(0x1&0xffffffff))&0xffffffff)') self.translationTest( |