about summary refs log tree commit diff stats
path: root/miasm2/ir/ir.py
diff options
context:
space:
mode:
Diffstat (limited to 'miasm2/ir/ir.py')
-rw-r--r--miasm2/ir/ir.py218
1 files changed, 107 insertions, 111 deletions
diff --git a/miasm2/ir/ir.py b/miasm2/ir/ir.py
index 0ea3a836..0a7d1d84 100644
--- a/miasm2/ir/ir.py
+++ b/miasm2/ir/ir.py
@@ -23,7 +23,7 @@ from itertools import chain
 
 import miasm2.expression.expression as m2_expr
 from miasm2.expression.expression_helper import get_missing_interval
-from miasm2.core.asmblock import AsmSymbolPool, AsmLabel, AsmBlock, \
+from miasm2.core.asmblock import AsmSymbolPool, AsmBlock, \
     AsmConstraint, AsmBlockBad
 from miasm2.core.graph import DiGraph
 
@@ -263,17 +263,16 @@ class IRBlock(object):
     Stand for an intermediate representation  basic block.
     """
 
-    __slots__ = ["label", "_assignblks", "_dst", "_dst_linenb"]
+    __slots__ = ["_loc_key", "_assignblks", "_dst", "_dst_linenb"]
 
-    def __init__(self, label, assignblks):
+    def __init__(self, loc_key, assignblks):
         """
-        @label: AsmLabel of the IR basic block
+        @loc_key: LocKey of the IR basic block
         @assignblks: list of AssignBlock
         """
 
-        #assert isinstance(label, AsmLabel)
-        assert isinstance(label, m2_expr.LocKey)
-        self.label = label
+        assert isinstance(loc_key, m2_expr.LocKey)
+        self._loc_key = loc_key
         for assignblk in assignblks:
             assert isinstance(assignblk, AssignBlock)
         self._assignblks = tuple(assignblks)
@@ -281,6 +280,13 @@ class IRBlock(object):
         self._dst_linenb = None
 
 
+    def get_label(self):
+        warnings.warn('DEPRECATION WARNING: use ".loc_key" instead of ".label"')
+        return self.loc_key
+
+    loc_key = property(lambda self:self._loc_key)
+    label = property(get_label)
+
     @property
     def assignblks(self):
         return self._assignblks
@@ -340,7 +346,7 @@ class IRBlock(object):
                 else:
                     new_assignblk[dst] = src
             irs.append(AssignBlock(new_assignblk, assignblk.instr))
-        return IRBlock(self.label, irs)
+        return IRBlock(self.loc_key, irs)
 
     @property
     def dst_linenb(self):
@@ -351,7 +357,7 @@ class IRBlock(object):
 
     def __str__(self):
         out = []
-        out.append('label_%s' % self.label.key)
+        out.append('loc_key_%s' % self.loc_key.key)
         for assignblk in self:
             for dst, src in assignblk.iteritems():
                 out.append('\t%s = %s' % (dst, src))
@@ -378,7 +384,7 @@ class IRBlock(object):
             for dst, src in assignblk.iteritems():
                 new_assignblk[mod_dst(dst)] = mod_src(src)
             assignblks.append(AssignBlock(new_assignblk, assignblk.instr))
-        return IRBlock(self.label, assignblks)
+        return IRBlock(self.loc_key, assignblks)
 
 
 class irbloc(IRBlock):
@@ -387,9 +393,9 @@ class irbloc(IRBlock):
     Use IRBlock instead of irbloc
     """
 
-    def __init__(self, label, irs, lines=None):
+    def __init__(self, loc_key, irs, lines=None):
         warnings.warn('DEPRECATION WARNING: use "IRBlock" instead of "irblock"')
-        super(irbloc, self).__init__(label, irs)
+        super(irbloc, self).__init__(loc_key, irs)
 
 
 class DiGraphIR(DiGraph):
@@ -404,20 +410,20 @@ class DiGraphIR(DiGraph):
         self._blocks = blocks
         super(DiGraphIR, self).__init__(*args, **kwargs)
 
-    def expr_loc_to_symb(self, expr):
-        if not expr.is_label():
+    def _expr_loc_to_symb(self, expr):
+        if not expr.is_loc():
             return expr
         if self.symbol_pool is None:
             name = str(expr)
         else:
-            name = self.symbol_pool.loc_key_to_label(expr.loc_key).name
+            name = self.symbol_pool.loc_key_to_name(expr.loc_key)
         return m2_expr.ExprId(name, expr.size)
 
     def node2lines(self, node):
         if self.symbol_pool is None:
             node_name = str(node)
         else:
-            node_name = self.symbol_pool.loc_key_to_label(node)
+            node_name = self.symbol_pool.loc_key_to_name(node)
         yield self.DotCellDescription(
             text="%s" % node_name,
             attr={
@@ -431,8 +437,8 @@ class DiGraphIR(DiGraph):
             raise StopIteration
         for i, assignblk in enumerate(self._blocks[node]):
             for dst, src in assignblk.iteritems():
-                new_src = src.visit(self.expr_loc_to_symb)
-                new_dst = dst.visit(self.expr_loc_to_symb)
+                new_src = src.visit(self._expr_loc_to_symb)
+                new_dst = dst.visit(self._expr_loc_to_symb)
                 line = "%s = %s" % (new_dst, new_src)
                 if self._dot_offset:
                     yield [self.DotCellDescription(text="%-4d" % i, attr={}),
@@ -448,9 +454,9 @@ class DiGraphIR(DiGraph):
         edge_color = "blue"
         if isinstance(src_irdst, m2_expr.ExprCond):
             src1, src2 = src_irdst.src1, src_irdst.src2
-            if src1.is_label(dst):
+            if src1.is_loc(dst):
                 edge_color = "limegreen"
-            elif src2.is_label(dst):
+            elif src2.is_loc(dst):
                 edge_color = "red"
         return {"color": edge_color}
 
@@ -501,24 +507,18 @@ class IntermediateRepresentation(object):
             irs = []
             for assignblk in irb:
                 irs.append(AssignBlock(assignblk, instr))
-            extra_irblocks[index] = IRBlock(irb.label, irs)
+            extra_irblocks[index] = IRBlock(irb.loc_key, irs)
         assignblk = AssignBlock(ir_bloc_cur, instr)
         return assignblk, extra_irblocks
 
-    def get_label(self, addr):
-        """Transforms an ExprId/ExprInt/label/int into a label
-        @addr: an ExprId/ExprInt/label/int"""
+    def get_loc_key(self, addr):
+        """Transforms an ExprId/ExprInt/loc_key/int into a loc_key
+        @addr: an ExprId/ExprInt/loc_key/int"""
 
-        if isinstance(addr, AsmLabel):
-            return addr
         if isinstance(addr, m2_expr.LocKey):
-            return self.symbol_pool.loc_key_to_label(addr)
+            return addr
         elif isinstance(addr, m2_expr.ExprLoc):
-            label = self.symbol_pool.loc_key_to_label(addr.loc_key)
-            if label is None:
-                return None
-            else:
-                return label
+            return addr.loc_key
 
         try:
             addr = int(addr)
@@ -528,13 +528,13 @@ class IntermediateRepresentation(object):
         return self.symbol_pool.getby_offset_create(addr)
 
     def get_block(self, addr):
-        """Returns the irbloc associated to an ExprId/ExprInt/label/int
-        @addr: an ExprId/ExprInt/label/int"""
+        """Returns the irbloc associated to an ExprId/ExprInt/loc_key/int
+        @addr: an ExprId/ExprInt/loc_key/int"""
 
-        label = self.get_label(addr)
-        if label is None:
+        loc_key = self.get_loc_key(addr)
+        if loc_key is None:
             return None
-        return self.blocks.get(label.loc_key, None)
+        return self.blocks.get(loc_key, None)
 
     def get_bloc(self, addr):
         """
@@ -544,18 +544,21 @@ class IntermediateRepresentation(object):
         warnings.warn('DEPRECATION WARNING: use "get_block" instead of "get_bloc"')
         return self.get_block(addr)
 
-    def add_instr(self, line, addr=0, gen_pc_updt=False):
-        lbl = self.gen_label()
-        block = AsmBlock(lbl)
+    def add_instr(self, line, loc_key=None, gen_pc_updt=False):
+        if loc_key is None:
+            loc_key = self.symbol_pool.gen_loc_key()
+        block = AsmBlock(loc_key)
         block.lines = [line]
         self.add_block(block, gen_pc_updt)
-        return lbl
+        return loc_key
 
     def getby_offset(self, offset):
         out = set()
         for irb in self.blocks.values():
             for assignblk in irb:
                 instr = assignblk.instr
+                if instr is None:
+                    continue
                 if instr.offset <= offset < instr.offset + instr.l:
                     out.add(irb)
         return out
@@ -621,28 +624,26 @@ class IntermediateRepresentation(object):
         @gen_pc_updt: insert PC update effects between instructions
         """
 
-        label = block.label
+        loc_key = block.loc_key
         ir_blocks_all = []
 
-        assert not isinstance(block, AsmBlockBad)
-
         assignments = []
         for instr in block.lines:
-            if label is None:
+            if loc_key is None:
                 assignments = []
-                label = self.get_instr_label(instr)
+                loc_key = self.get_loc_key_for_instr(instr)
             split = self.add_instr_to_irblock(block, instr, assignments,
                                               ir_blocks_all, gen_pc_updt)
             if split:
-                ir_blocks_all.append(IRBlock(label.loc_key, assignments))
-                label = None
+                ir_blocks_all.append(IRBlock(loc_key, assignments))
+                loc_key = None
                 assignments = []
-        if label is not None:
-            ir_blocks_all.append(IRBlock(label.loc_key, assignments))
+        if loc_key is not None:
+            ir_blocks_all.append(IRBlock(loc_key, assignments))
 
         new_ir_blocks_all = self.post_add_block(block, ir_blocks_all)
         for irblock in new_ir_blocks_all:
-            self.blocks[irblock.label] = irblock
+            self.blocks[irblock.loc_key] = irblock
         return new_ir_blocks_all
 
     def add_bloc(self, block, gen_pc_updt=False):
@@ -675,25 +676,25 @@ class IntermediateRepresentation(object):
         for index, irblock in enumerate(ir_blocks):
             if irblock.dst is not None:
                 continue
-            next_lbl = block.get_next()
-            if next_lbl is None:
-                lbl = None
+            next_loc_key = block.get_next()
+            if next_loc_key is None:
+                loc_key = None
                 if block.lines:
                     line = block.lines[-1]
                     if line.offset is not None:
-                        lbl = self.symbol_pool.getby_offset_create(line.offset + line.l)
-                if lbl is None:
-                    lbl = self.symbol_pool.gen_label()
-                block.add_cst(lbl, AsmConstraint.c_next, self.symbol_pool)
+                        loc_key = self.symbol_pool.getby_offset_create(line.offset + line.l)
+                if loc_key is None:
+                    loc_key = self.symbol_pool.gen_loc_key()
+                block.add_cst(loc_key, AsmConstraint.c_next, self.symbol_pool)
             else:
-                lbl = next_lbl
-            dst = m2_expr.ExprLoc(lbl.loc_key, self.pc.size)
+                loc_key = next_loc_key
+            dst = m2_expr.ExprLoc(loc_key, self.pc.size)
             if irblock.assignblks:
                 instr = irblock.assignblks[-1].instr
             else:
                 instr = None
             assignblk = AssignBlock({self.IRDst: dst}, instr)
-            ir_blocks[index] = IRBlock(irblock.label, list(irblock.assignblks) + [assignblk])
+            ir_blocks[index] = IRBlock(irblock.loc_key, list(irblock.assignblks) + [assignblk])
 
     def post_add_block(self, block, ir_blocks):
         self.set_empty_dst_to_next(block, ir_blocks)
@@ -701,7 +702,7 @@ class IntermediateRepresentation(object):
         new_irblocks = []
         for irblock in ir_blocks:
             new_irblock = self.irbloc_fix_regs_for_mode(irblock, self.attrib)
-            self.blocks[irblock.label] = new_irblock
+            self.blocks[irblock.loc_key] = new_irblock
             new_irblocks.append(new_irblock)
         # Forget graph if any
         self._graph = None
@@ -715,27 +716,22 @@ class IntermediateRepresentation(object):
         warnings.warn('DEPRECATION WARNING: use "post_add_block" instead of "post_add_bloc"')
         return self.post_add_block(block, ir_blocks)
 
-    def get_instr_label(self, instr):
-        """Returns the label associated to an instruction
+    def get_loc_key_for_instr(self, instr):
+        """Returns the loc_key associated to an instruction
         @instr: current instruction"""
         return self.symbol_pool.getby_offset_create(instr.offset)
 
-    def gen_label(self):
-        # TODO: fix hardcoded offset
-        label = self.symbol_pool.gen_label()
-        return label
-
-    def gen_label_and_expr(self, size):
+    def gen_loc_key_and_expr(self, size):
         """
-        Return a label and it's corresponding ExprLoc
+        Return a loc_key and it's corresponding ExprLoc
         @size: size of expression
         """
-        label = self.gen_label()
-        return label, m2_expr.ExprLoc(label.loc_key, size)
+        loc_key = self.symbol_pool.gen_loc_key()
+        return loc_key, m2_expr.ExprLoc(loc_key, size)
 
-    def get_next_label(self, instr):
-        label = self.symbol_pool.getby_offset_create(instr.offset + instr.l)
-        return label
+    def get_next_loc_key(self, instr):
+        loc_key = self.symbol_pool.getby_offset_create(instr.offset + instr.l)
+        return loc_key
 
     def simplify(self, simplifier):
         """
@@ -743,14 +739,14 @@ class IntermediateRepresentation(object):
         @simplifier: ExpressionSimplifier instance
         """
         modified = False
-        for label, block in self.blocks.iteritems():
+        for loc_key, block in self.blocks.iteritems():
             assignblks = []
             for assignblk in block:
                 new_assignblk = assignblk.simplify(simplifier)
                 if assignblk != new_assignblk:
                     modified = True
                 assignblks.append(new_assignblk)
-            self.blocks[label] = IRBlock(label, assignblks)
+            self.blocks[loc_key] = IRBlock(loc_key, assignblks)
         return modified
 
     def replace_expr_in_ir(self, bloc, rep):
@@ -777,7 +773,7 @@ class IntermediateRepresentation(object):
         out = set()
         while todo:
             dst = todo.pop()
-            if dst.is_label():
+            if dst.is_loc():
                 done.add(dst)
             elif dst.is_mem() or dst.is_int():
                 done.add(dst)
@@ -826,7 +822,7 @@ class IntermediateRepresentation(object):
                 if dst.is_int():
                     dst_lbl = self.symbol_pool.getby_offset_create(int(dst))
                     dst = m2_expr.ExprLoc(dst_lbl.loc_key, self.pc.size)
-                if dst.is_label():
+                if dst.is_loc():
                     self._graph.add_edge(lbl, dst.loc_key)
 
     @property
@@ -839,14 +835,14 @@ class IntermediateRepresentation(object):
 
     def remove_empty_assignblks(self):
         modified = False
-        for label, block in self.blocks.iteritems():
+        for loc_key, block in self.blocks.iteritems():
             irs = []
             for assignblk in block:
                 if len(assignblk):
                     irs.append(assignblk)
                 else:
                     modified = True
-            self.blocks[label] = IRBlock(label, irs)
+            self.blocks[loc_key] = IRBlock(loc_key, irs)
         return modified
 
     def remove_jmp_blocks(self):
@@ -864,62 +860,62 @@ class IntermediateRepresentation(object):
             if len(assignblk) > 1:
                 continue
             assert set(assignblk.keys()) == set([self.IRDst])
-            if len(self.graph.successors(block.label)) != 1:
+            if len(self.graph.successors(block.loc_key)) != 1:
                 continue
-            if not assignblk[self.IRDst].is_label():
+            if not assignblk[self.IRDst].is_loc():
                 continue
             dst = assignblk[self.IRDst].loc_key
-            if dst == block.label:
+            if dst == block.loc_key:
                 # Infinite loop block
                 continue
-            jmp_blocks.add(block.label)
+            jmp_blocks.add(block.loc_key)
 
         # Remove them, relink graph
         modified = False
-        for label in jmp_blocks:
-            block = self.blocks[label]
+        for loc_key in jmp_blocks:
+            block = self.blocks[loc_key]
             dst_loc_key = block.dst
-            parents = self.graph.predecessors(block.label)
+            parents = self.graph.predecessors(block.loc_key)
             for lbl in parents:
                 parent = self.blocks.get(lbl, None)
                 if parent is None:
                     continue
                 dst = parent.dst
-                if dst.is_id(block.label):
+                if dst.is_id(block.loc_key):
                     dst = m2_expr.ExprLoc(dst_loc_key, dst.size)
 
-                    self.graph.discard_edge(lbl, block.label)
-                    self.graph.discard_edge(block.label, dst_label)
+                    self.graph.discard_edge(lbl, block.loc_key)
+                    self.graph.discard_edge(block.loc_key, dst_loc_key)
 
-                    self.graph.add_uniq_edge(lbl, dst_label)
+                    self.graph.add_uniq_edge(lbl, dst_loc_key)
                     modified = True
                 elif dst.is_cond():
                     src1, src2 = dst.src1, dst.src2
-                    if src1.is_id(block.label):
-                        dst = m2_expr.ExprCond(dst.cond, m2_expr.ExprLoc(dst_label, dst.size), dst.src2)
-                        self.graph.discard_edge(lbl, block.label)
-                        self.graph.discard_edge(block.label, dst_label)
-                        self.graph.add_uniq_edge(lbl, dst_label)
+                    if src1.is_id(block.loc_key):
+                        dst = m2_expr.ExprCond(dst.cond, m2_expr.ExprLoc(dst_loc_key, dst.size), dst.src2)
+                        self.graph.discard_edge(lbl, block.loc_key)
+                        self.graph.discard_edge(block.loc_key, dst_loc_key)
+                        self.graph.add_uniq_edge(lbl, dst_loc_key)
                         modified = True
-                    if src2.is_id(block.label):
-                        dst = m2_expr.ExprCond(dst.cond, dst.src1, m2_expr.ExprLoc(dst_label, dst.size))
-                        self.graph.discard_edge(lbl, block.label)
-                        self.graph.discard_edge(block.label, dst_label)
-                        self.graph.add_uniq_edge(lbl, dst_label)
+                    if src2.is_id(block.loc_key):
+                        dst = m2_expr.ExprCond(dst.cond, dst.src1, m2_expr.ExprLoc(dst_loc_key, dst.size))
+                        self.graph.discard_edge(lbl, block.loc_key)
+                        self.graph.discard_edge(block.loc_key, dst_loc_key)
+                        self.graph.add_uniq_edge(lbl, dst_loc_key)
                         modified = True
                     if dst.src1 == dst.src2:
                         dst = dst.src1
                 else:
                     continue
                 new_parent = parent.set_dst(dst)
-                self.blocks[parent.label] = new_parent
+                self.blocks[parent.loc_key] = new_parent
 
         # Remove unlinked useless nodes
-        for label in jmp_blocks:
-            if (len(self.graph.predecessors(label)) == 0 and
-                len(self.graph.successors(label)) == 0):
-                self.graph.del_node(label)
-                del self.blocks[label]
+        for loc_key in jmp_blocks:
+            if (len(self.graph.predecessors(loc_key)) == 0 and
+                len(self.graph.successors(loc_key)) == 0):
+                self.graph.del_node(loc_key)
+                del self.blocks[loc_key]
         return modified
 
     def merge_blocks(self):
@@ -976,6 +972,6 @@ class ir(IntermediateRepresentation):
     Use IntermediateRepresentation instead of ir
     """
 
-    def __init__(self, label, irs, lines=None):
+    def __init__(self, loc_key, irs, lines=None):
         warnings.warn('DEPRECATION WARNING: use "IntermediateRepresentation" instead of "ir"')
-        super(ir, self).__init__(label, irs, lines)
+        super(ir, self).__init__(loc_key, irs, lines)