Przeglądaj źródła

Moved pred/succ functions to dataflow.py for common usage.

Taddeus Kroes 14 lat temu
rodzic
commit
7a6c0ad901
2 zmienionych plików z 33 dodań i 19 usunięć
  1. 28 2
      src/dataflow.py
  2. 5 17
      src/liveness.py

+ 28 - 2
src/dataflow.py

@@ -1,3 +1,5 @@
+from copy import copy
+
 from statement import Block
 
 
@@ -83,12 +85,12 @@ def generate_flow_graph(blocks):
 
             # Compare the target to all leading labels, add an edge if the
             # label matches the jump target
-            target_found = False
+            #target_found = False
 
             for other in blocks:
                 if other[0].is_label(target):
                     b.add_edge_to(other)
-                    target_found = True
+                    #target_found = True
 
             # If the jump target is outside the program, add an edge to the
             # dummy block
@@ -102,3 +104,27 @@ def generate_flow_graph(blocks):
                 b.add_edge_to(blocks[i + 1])
         elif i < len(blocks) - 1:
             b.add_edge_to(blocks[i + 1])
+
+
+def pred(block, known=[]):
+    """Recursively find all predecessors of a node."""
+    direct = filter(lambda b: b != block and b not in known, block.edges_from)
+    p = copy(direct)
+
+    for predecessor in direct:
+        p += pred(predecessor, known + direct)
+        return p
+
+    return p
+
+
+def succ(block, known=[]):
+    """Recursively find all successors of a node."""
+    direct = filter(lambda b: b != block and b not in known, block.edges_to)
+    s = copy(direct)
+
+    for successor in direct:
+        s += succ(successor, known + direct)
+        return s
+
+    return s

+ 5 - 17
src/liveness.py

@@ -1,4 +1,4 @@
-from copy import copy
+from dataflow import succ
 
 
 RETURN_REGS = ['$2', '$3']
@@ -20,10 +20,10 @@ def is_reg_dead_after(reg, block, index, known_jump_targets=[]):
     # of reserved argument registers are not removed
     if reg in RESERVED_USE and block[-1].is_command('jal') \
             and block[-1][0] not in known_jump_targets:
-        if block.verbose:
-            block[index].set_inline_comment(
-                    ' Register %s cannot be removed due to "jal %s"'
-                    % (reg, block[-1][0]))
+        #if block.verbose:
+        #    block[index].set_inline_comment(
+        #            ' Register %s cannot be removed due to "jal %s"'
+        #            % (reg, block[-1][0]))
         return False
 
     if index < len(block) - 1:
@@ -72,18 +72,6 @@ def create_use_def(block):
                 block.def_set.add(reg)
 
 
-def succ(block, known=[]):
-    """Recursively find all successors of a node."""
-    direct = filter(lambda b: b != block and b not in known, block.edges_to)
-    s = copy(direct)
-
-    for successor in direct:
-        s += succ(successor, known + direct)
-        return s
-
-    return s
-
-
 def create_in_out(blocks):
     for b in blocks:
         create_use_def(b)