1 2 3 package org.quilt.cl; 4 5 import java.util.*; 6 import org.quilt.graph.*; 7 8 16 public class SortedBlocks { 17 18 private ControlFlowGraph graph; private SortedMap blox; 21 22 public SortedBlocks () { 23 blox = new TreeMap(); 24 } 25 26 36 public boolean add (final CodeVertex v) { 37 int pos = -1; 38 if (v == null) { 39 throw new IllegalArgumentException ( 40 "attempt to add null vertex"); 41 } 42 pos = v.getPosition(); 43 if (pos < 0) { 44 throw new IllegalArgumentException ( 45 "vertex has invalid position"); 46 } 47 Integer p = new Integer (pos); 48 if (blox.containsKey (p) ) { 49 return false; } else { 51 blox.put (p, v); 52 return true; 53 } 54 } 55 56 64 public CodeVertex find ( final int pos, ControlFlowGraph currGraph, 65 Edge e) { 66 Integer p = new Integer (pos); 67 if (blox.containsKey (p) ) { 68 CodeVertex v = (CodeVertex) blox.get(p); 69 ControlFlowGraph vGraph = (ControlFlowGraph)v.getGraph(); 70 Entry x; 71 if ( vGraph == currGraph ) { 72 e.setTarget(v); } else if ( (x = currGraph.closestEntry(vGraph)) != null) { 74 e.setTarget(x); 77 } else { 78 e.setTarget(currGraph.getExit()); 81 } 82 return v; 83 } else { 84 return add (pos, e); 85 } 86 } 87 95 public CodeVertex add (final int pos, final Edge e) { 96 Integer p = new Integer (pos); 102 Vertex source_ = e.getSource(); 103 CodeVertex v; 104 if ( source_ instanceof Exit) { 105 v = ((ControlFlowGraph)e.getTarget().getGraph()) 106 .insertCodeVertex(e); 107 } else { 108 v = ((ControlFlowGraph)source_.getGraph()) 109 .insertCodeVertex(e); 110 } 111 v.setPos(pos); 112 blox.put (p, v); 114 return v; 120 } 121 122 public boolean exists (final int pos) { 123 Integer p = new Integer (pos); 124 return blox.containsKey(p); 125 } 126 134 public CodeVertex get (final int pos) { 135 Integer p = new Integer (pos); 136 if (!blox.containsKey (p) ) { 137 throw new GraphBuildException ( 138 "INTERNAL ERROR - no vertex at " + pos); 139 } 140 return (CodeVertex) blox.get(p); 141 } 142 147 public int size() { 148 return blox.size(); 149 } 150 151 156 public String toString() { 157 Iterator vertices = blox.keySet().iterator(); 159 String s = "vertex position / instructions\n"; 160 while (vertices.hasNext()) { 161 Integer position = (Integer ) vertices.next(); 162 CodeVertex v = (CodeVertex) blox.get( position ); 163 s += " " + v.getIndex() 164 + " " + position + "\n" 165 + v ; 167 } 168 return s; 169 } 170 } 171 | Popular Tags |