about summary refs log tree commit diff stats
path: root/miasm2/arch/arm/sem.py
diff options
context:
space:
mode:
Diffstat (limited to 'miasm2/arch/arm/sem.py')
-rw-r--r--miasm2/arch/arm/sem.py127
1 files changed, 67 insertions, 60 deletions
diff --git a/miasm2/arch/arm/sem.py b/miasm2/arch/arm/sem.py
index 9e4da3f6..00250157 100644
--- a/miasm2/arch/arm/sem.py
+++ b/miasm2/arch/arm/sem.py
@@ -441,16 +441,16 @@ def sdiv(ir, instr, a, b, c=None):
     if c is None:
         b, c = a, b
 
-    lbl_div = ExprId(ir.gen_label(), ir.IRDst.size)
-    lbl_except = ExprId(ir.gen_label(), ir.IRDst.size)
-    lbl_next = ExprId(ir.get_next_label(instr), ir.IRDst.size)
+    loc_div = ExprLoc(ir.loc_db.add_location(), ir.IRDst.size)
+    loc_except = ExprId(ir.loc_db.add_location(), ir.IRDst.size)
+    loc_next = ExprLoc(ir.get_next_loc_key(instr), ir.IRDst.size)
 
-    e.append(ExprAff(ir.IRDst, ExprCond(c, lbl_div, lbl_except)))
+    e.append(ExprAff(ir.IRDst, ExprCond(c, loc_div, loc_except)))
 
     do_except = []
     do_except.append(ExprAff(exception_flags, ExprInt(EXCEPT_DIV_BY_ZERO, exception_flags.size)))
-    do_except.append(ExprAff(ir.IRDst, lbl_next))
-    blk_except = IRBlock(lbl_except.name, [AssignBlock(do_except, instr)])
+    do_except.append(ExprAff(ir.IRDst, loc_next))
+    blk_except = IRBlock(loc_except.loc_key, [AssignBlock(do_except, instr)])
 
 
 
@@ -461,8 +461,8 @@ def sdiv(ir, instr, a, b, c=None):
     if dst is not None:
         do_div.append(ExprAff(ir.IRDst, r))
 
-    do_div.append(ExprAff(ir.IRDst, lbl_next))
-    blk_div = IRBlock(lbl_div.name, [AssignBlock(do_div, instr)])
+    do_div.append(ExprAff(ir.IRDst, loc_next))
+    blk_div = IRBlock(loc_div.loc_key, [AssignBlock(do_div, instr)])
 
     return e, [blk_div, blk_except]
 
@@ -474,16 +474,16 @@ def udiv(ir, instr, a, b, c=None):
 
 
 
-    lbl_div = ExprId(ir.gen_label(), ir.IRDst.size)
-    lbl_except = ExprId(ir.gen_label(), ir.IRDst.size)
-    lbl_next = ExprId(ir.get_next_label(instr), ir.IRDst.size)
+    loc_div = ExprLoc(ir.loc_db.add_location(), ir.IRDst.size)
+    loc_except = ExprLoc(ir.loc_db.add_location(), ir.IRDst.size)
+    loc_next = ExprLoc(ir.get_next_loc_key(instr), ir.IRDst.size)
 
-    e.append(ExprAff(ir.IRDst, ExprCond(c, lbl_div, lbl_except)))
+    e.append(ExprAff(ir.IRDst, ExprCond(c, loc_div, loc_except)))
 
     do_except = []
     do_except.append(ExprAff(exception_flags, ExprInt(EXCEPT_DIV_BY_ZERO, exception_flags.size)))
-    do_except.append(ExprAff(ir.IRDst, lbl_next))
-    blk_except = IRBlock(lbl_except.name, [AssignBlock(do_except, instr)])
+    do_except.append(ExprAff(ir.IRDst, loc_next))
+    blk_except = IRBlock(loc_except.loc_key, [AssignBlock(do_except, instr)])
 
 
     r = ExprOp("udiv", b, c)
@@ -493,8 +493,8 @@ def udiv(ir, instr, a, b, c=None):
     if dst is not None:
         do_div.append(ExprAff(ir.IRDst, r))
 
-    do_div.append(ExprAff(ir.IRDst, lbl_next))
-    blk_div = IRBlock(lbl_div.name, [AssignBlock(do_div, instr)])
+    do_div.append(ExprAff(ir.IRDst, loc_next))
+    blk_div = IRBlock(loc_div.loc_key, [AssignBlock(do_div, instr)])
 
     return e, [blk_div, blk_except]
 
@@ -635,7 +635,6 @@ def st_ld_r(ir, instr, a, a2, b, store=False, size=32, s_ext=False, z_ext=False)
         base, off = b.args[0],  b.args[1]  # ExprInt(size/8, 32)
     else:
         base, off = b, ExprInt(0, 32)
-    # print a, wb, base, off, postinc
     if postinc:
         ad = base
     else:
@@ -734,13 +733,11 @@ def ldrsh(ir, instr, a, b):
 def st_ld_m(ir, instr, a, b, store=False, postinc=False, updown=False):
     e = []
     wb = False
-    # sb = False
     dst = None
     if isinstance(a, ExprOp) and a.op == 'wback':
         wb = True
         a = a.args[0]
     if isinstance(b, ExprOp) and b.op == 'sbit':
-        # sb = True
         b = b.args[0]
     regs = b.args
     base = a
@@ -932,19 +929,20 @@ def pop(ir, instr, a):
 
 def cbz(ir, instr, a, b):
     e = []
-    lbl_next = ExprId(ir.get_next_label(instr), 32)
-    e.append(ExprAff(ir.IRDst, ExprCond(a, lbl_next, b)))
+    loc_next = ir.get_next_loc_key(instr)
+    loc_next_expr = ExprLoc(loc_next, 32)
+    e.append(ExprAff(ir.IRDst, ExprCond(a, loc_next_expr, b)))
     return e, []
 
 
 def cbnz(ir, instr, a, b):
     e = []
-    lbl_next = ExprId(ir.get_next_label(instr), 32)
-    e.append(ExprAff(ir.IRDst, ExprCond(a, b, lbl_next)))
+    loc_next = ir.get_next_loc_key(instr)
+    loc_next_expr = ExprLoc(loc_next, 32)
+    e.append(ir.IRDst, ExprCond(a, b, loc_next_expr))
     return e, []
 
 
-
 def uxtb(ir, instr, a, b):
     e = []
     r = b[:8].zeroExtend(32)
@@ -1038,7 +1036,7 @@ def pldw(ir, instr, a):
 
 def clz(ir, instr, a, b):
     e = []
-    e.append(ExprAff(a, ExprOp('clz', b)))
+    e.append(ExprAff(a, ExprOp('cntleadzeros', b)))
     return e, []
 
 def uxtab(ir, instr, a, b, c):
@@ -1264,10 +1262,14 @@ def add_condition_expr(ir, instr, cond, instr_ir, extra_ir):
         raise ValueError('unknown condition %r' % cond)
     cond = tab_cond[cond]
 
-    lbl_next = ExprId(ir.get_next_label(instr), 32)
-    lbl_do = ExprId(ir.gen_label(), 32)
 
-    dst_cond = ExprCond(cond, lbl_do, lbl_next)
+
+    loc_next = ir.get_next_loc_key(instr)
+    loc_next_expr = ExprLoc(loc_next, 32)
+    loc_do = ir.loc_db.add_location()
+    loc_do_expr = ExprLoc(loc_do, 32)
+
+    dst_cond = ExprCond(cond, loc_do_expr, loc_next_expr)
     assert(isinstance(instr_ir, list))
 
     has_irdst = False
@@ -1276,8 +1278,8 @@ def add_condition_expr(ir, instr, cond, instr_ir, extra_ir):
             has_irdst = True
             break
     if not has_irdst:
-        instr_ir.append(ExprAff(ir.IRDst, lbl_next))
-    e_do = IRBlock(lbl_do.name, [AssignBlock(instr_ir, instr)])
+        instr_ir.append(ExprAff(ir.IRDst, loc_next_expr))
+    e_do = IRBlock(loc_do, [AssignBlock(instr_ir, instr)])
     e = [ExprAff(ir.IRDst, dst_cond)]
     return e, [e_do] + extra_ir
 
@@ -1472,8 +1474,8 @@ class arminfo:
 
 
 class ir_arml(IntermediateRepresentation):
-    def __init__(self, symbol_pool=None):
-        IntermediateRepresentation.__init__(self, mn_arm, "l", symbol_pool)
+    def __init__(self, loc_db=None):
+        IntermediateRepresentation.__init__(self, mn_arm, "l", loc_db)
         self.pc = PC
         self.sp = SP
         self.IRDst = ExprId('IRDst', 32)
@@ -1527,7 +1529,7 @@ class ir_arml(IntermediateRepresentation):
                 raise ValueError("IT name invalid %s" % instr)
         return out, instr.args[0]
 
-    def do_it_block(self, label, index, block, assignments, gen_pc_updt):
+    def do_it_block(self, loc, index, block, assignments, gen_pc_updt):
         instr = block.lines[index]
         it_hints, it_cond = self.parse_itt(instr)
         cond_num = cond_dct_inv[it_cond.name]
@@ -1539,14 +1541,14 @@ class ir_arml(IntermediateRepresentation):
         ir_blocks_all = []
 
         # Gen dummy irblock for IT instr
-        label_next = self.get_next_label(instr)
-        dst = ExprAff(self.IRDst, ExprId(label_next, 32))
+        loc_next = self.get_next_loc_key(instr)
+        dst = ExprAff(self.IRDst, ExprId(loc_next, 32))
         dst_blk = AssignBlock([dst], instr)
         assignments.append(dst_blk)
-        irblock = IRBlock(label, assignments)
+        irblock = IRBlock(loc, assignments)
         ir_blocks_all.append([irblock])
 
-        label = label_next
+        loc = loc_next
         assignments = []
         for hint in it_hints:
             irblocks = []
@@ -1554,38 +1556,40 @@ class ir_arml(IntermediateRepresentation):
             instr = block.lines[index]
 
             # Add conditionnal jump to current irblock
-            label_do = self.symbol_pool.gen_label()
-            label_next = self.get_next_label(instr)
+            loc_do = self.loc_db.add_location()
+            loc_next = self.get_next_loc_key(instr)
 
             if hint:
                 local_cond = ~cond_eq
             else:
                 local_cond = cond_eq
-            dst = ExprAff(self.IRDst, ExprCond(local_cond, ExprId(label_do, 32), ExprId(label_next, 32)))
+            dst = ExprAff(self.IRDst, ExprCond(local_cond, ExprLoc(loc_do, 32), ExprLoc(loc_next, 32)))
             dst_blk = AssignBlock([dst], instr)
             assignments.append(dst_blk)
-            irblock = IRBlock(label, assignments)
+            irblock = IRBlock(loc, assignments)
 
             irblocks.append(irblock)
 
             assignments = []
-            label = label_do
-            split = self.add_instr_to_irblock(block, instr, assignments,
-                                              irblocks, gen_pc_updt)
+            loc = loc_do
+            split = self.add_instr_to_current_state(
+                instr, block, assignments,
+                irblocks, gen_pc_updt
+            )
             if split:
                 raise NotImplementedError("Unsupported instr in IT block (%s)" % instr)
 
-            dst = ExprAff(self.IRDst, ExprId(label_next, 32))
+            dst = ExprAff(self.IRDst, ExprId(loc_next, 32))
             dst_blk = AssignBlock([dst], instr)
             assignments.append(dst_blk)
-            irblock = IRBlock(label, assignments)
+            irblock = IRBlock(loc, assignments)
             irblocks.append(irblock)
-            label = label_next
+            loc = loc_next
             assignments = []
             ir_blocks_all.append(irblocks)
         return index, ir_blocks_all
 
-    def add_block(self, block, gen_pc_updt=False):
+    def add_asmblock_to_ircfg(self, block, ircfg, gen_pc_updt=False):
         """
         Add a native block to the current IR
         @block: native assembly block
@@ -1594,7 +1598,8 @@ class ir_arml(IntermediateRepresentation):
 
         it_hints = None
         it_cond = None
-        label = None
+        label = block.loc_key
+        assignments = []
         ir_blocks_all = []
         index = -1
         while index + 1 < len(block.lines):
@@ -1602,7 +1607,7 @@ class ir_arml(IntermediateRepresentation):
             instr = block.lines[index]
             if label is None:
                 assignments = []
-                label = self.get_instr_label(instr)
+                label = self.get_loc_key_for_instr(instr)
             if instr.name.startswith("IT"):
                 index, irblocks_it = self.do_it_block(label, index, block, assignments, gen_pc_updt)
                 for irblocks in irblocks_it:
@@ -1610,8 +1615,10 @@ class ir_arml(IntermediateRepresentation):
                 label = None
                 continue
 
-            split = self.add_instr_to_irblock(block, instr, assignments,
-                                              ir_blocks_all, gen_pc_updt)
+            split = self.add_instr_to_current_state(
+                instr, block, assignments,
+                ir_blocks_all, gen_pc_updt
+            )
             if split:
                 ir_blocks_all.append(IRBlock(label, assignments))
                 label = None
@@ -1619,16 +1626,16 @@ class ir_arml(IntermediateRepresentation):
         if label is not None:
             ir_blocks_all.append(IRBlock(label, assignments))
 
-        new_ir_blocks_all = self.post_add_block(block, ir_blocks_all)
+        new_ir_blocks_all = self.post_add_asmblock_to_ircfg(block, ircfg, ir_blocks_all)
         for irblock in new_ir_blocks_all:
-            self.blocks[irblock.label] = irblock
+            ircfg.add_irblock(irblock)
         return new_ir_blocks_all
 
 
 
 class ir_armb(ir_arml):
-    def __init__(self, symbol_pool=None):
-        IntermediateRepresentation.__init__(self, mn_arm, "b", symbol_pool)
+    def __init__(self, loc_db=None):
+        IntermediateRepresentation.__init__(self, mn_arm, "b", loc_db)
         self.pc = PC
         self.sp = SP
         self.IRDst = ExprId('IRDst', 32)
@@ -1636,8 +1643,8 @@ class ir_armb(ir_arml):
 
 
 class ir_armtl(ir_arml):
-    def __init__(self, symbol_pool=None):
-        IntermediateRepresentation.__init__(self, mn_armt, "l", symbol_pool)
+    def __init__(self, loc_db=None):
+        IntermediateRepresentation.__init__(self, mn_armt, "l", loc_db)
         self.pc = PC
         self.sp = SP
         self.IRDst = ExprId('IRDst', 32)
@@ -1662,8 +1669,8 @@ class ir_armtl(ir_arml):
 
 
 class ir_armtb(ir_armtl):
-    def __init__(self, symbol_pool=None):
-        IntermediateRepresentation.__init__(self, mn_armt, "b", symbol_pool)
+    def __init__(self, loc_db=None):
+        IntermediateRepresentation.__init__(self, mn_armt, "b", loc_db)
         self.pc = PC
         self.sp = SP
         self.IRDst = ExprId('IRDst', 32)