1 2 package org.quilt.cover.stmt; 3 4 import org.apache.bcel.Constants; 5 import org.apache.bcel.classfile.ConstantPool; 6 import org.apache.bcel.generic.*; 7 import org.quilt.cl.*; 8 import org.quilt.graph.*; 9 10 18 public class GraphAction implements GraphXformer { 19 20 private static StmtRegistry stmtReg = null; 21 private ClassGen clazz_ = null; 22 private MethodGen method_ = null; 23 24 25 private static String name_ = null; 26 27 private InstructionFactory factory_; 28 29 private ConstantPoolGen cpGen_; 30 31 32 private int counterCount = 0; 33 34 35 private ClassAction classAct_ = null; 36 37 38 public GraphAction () {} 39 40 public GraphAction (StmtRegistry reg) { 41 stmtReg = reg; 42 setName(this.getClass().getName()); 43 } 44 45 52 public GraphAction (StmtRegistry reg, ClassAction classAct) { 53 if (reg == null || classAct == null) { 54 throw new IllegalArgumentException ("null argument"); 55 } 56 stmtReg = reg; 57 classAct_ = classAct; 58 setName(this.getClass().getName()); 59 } 60 61 private class LampLighter implements org.quilt.graph.Visitor, 62 org.apache.bcel.Constants, 63 org.apache.bcel.generic.InstructionConstants { 64 private ControlFlowGraph graph = null; 65 public LampLighter() {} 66 public void discoverGraph(Directed g) { 67 graph = (ControlFlowGraph)g; 68 } 69 public void discoverVertex(Vertex v) { 70 } 71 74 public void discoverEdge(Edge e) { 75 Vertex source = e.getSource(); 76 Vertex target = e.getTarget(); 77 ControlFlowGraph srcGraph = (ControlFlowGraph)source.getGraph(); 78 ControlFlowGraph tgtGraph = (ControlFlowGraph)target.getGraph(); 79 if (tgtGraph != graph) { 81 System.out.println("GraphAction.discoverEdge:\n" 82 + " current graph is " + graph.getIndex() 83 + " but edge is " + e); 84 } 85 boolean addingCounter = true; 87 if ( (target instanceof CodeVertex) 88 && !(target instanceof CounterVertex) 89 && !(source instanceof CounterVertex) ) { 90 Instruction srcConnInst = null; 91 if (source instanceof CodeVertex) { 92 srcConnInst = ((CodeVertex)source).getConnInst(); 93 if (srcConnInst != null 94 && srcConnInst instanceof GotoInstruction 95 && (source.getGraph() != tgtGraph)) { 96 addingCounter = false; 101 } 102 } 103 if (srcGraph != graph) { 105 System.out.println("GraphAction.discoverEdge WARNING: " 106 + "graph index " + graph.getIndex() 107 + "\n but edge is " + e 108 + " - not adding counter"); 109 addingCounter = false; 110 } 111 if (addingCounter) { 112 CounterVertex cv; 113 if (source instanceof Exit) { 114 cv = (CounterVertex) tgtGraph.insertCodeVertex( 118 (CodeVertex)new CounterVertex(tgtGraph), e); 119 120 } else { 121 cv = (CounterVertex) graph.insertCodeVertex( 122 (CodeVertex)new CounterVertex(graph), e); 123 } 124 InstructionList ilist = cv.getInstructionList(); 125 InstructionHandle ih = ilist.append ( 126 factory_.createFieldAccess(clazz_.getClassName(), "q$$q", 127 new ArrayType(Type.INT,1), Constants.GETSTATIC)); 128 ilist.append(new PUSH(cpGen_, counterCount++)); 129 ilist.append(InstructionConstants.DUP2); 130 ilist.append(InstructionConstants.IALOAD); 131 ilist.append(new PUSH(cpGen_, 1)); 132 ilist.append(InstructionConstants.IADD); 133 ilist.append(InstructionConstants.IASTORE); 134 136 if (source instanceof CodeVertex) { 138 if (srcConnInst != null) { 139 141 if (srcConnInst instanceof GotoInstruction) { 143 if (!( e == source.getEdge() )) { 144 ((CodeVertex)source).moveGoto(cv); 145 } 146 } else if (srcConnInst instanceof IfInstruction 147 || srcConnInst instanceof JsrInstruction ) { 148 Edge otherEdge 149 = ((BinaryConnector)source.getConnector()) 150 .getOtherEdge(); 151 if (e == otherEdge) { 152 BranchInstruction bi 156 = (BranchInstruction) srcConnInst; 157 InstructionHandle targ = bi.getTarget(); 158 bi.setTarget(ih); 159 cv.setConnInst( new GOTO (targ) ); 160 cv.makeBinary(); 162 BinaryConnector bc = (BinaryConnector) 163 cv.getConnector(); 164 Vertex myTarget = bc.getTarget(); 165 if (graph.getExit() == null) { 168 System.out.println( 169 "GRAPH HAS NULL EXIT"); 170 } 171 bc.setTarget(graph.getExit()); 173 bc.setOtherTarget(myTarget); 174 } 175 } 176 178 } } 180 } } 183 } 184 public void finishEdge(Edge e) { 185 } 186 public void finishVertex(Vertex v) { 187 } 188 public void finishGraph(Directed g) { 189 } 190 } 191 public void xform (final ClassGen cg, final MethodGen method, 193 final ControlFlowGraph cfg) { 194 clazz_ = cg; 195 method_ = method; 196 cpGen_ = cg.getConstantPool() ; 197 factory_ = new InstructionFactory (clazz_, cpGen_); 198 199 String className = clazz_.getClassName(); 200 Ephemera eph = stmtReg.getEphemera(className); 201 if (eph == null) 203 System.out.println("GraphAction.xform: eph is null!"); 204 counterCount = eph.getCounterCount(); 206 207 int cfgSize = cfg.size(); 208 Walker walker = new Walker(); 209 walker.visit(cfg, new LampLighter()); 210 211 215 eph.setEndCount(method.getName(), counterCount); 216 217 218 } 219 221 public static String getName() { 222 return name_; 223 } 224 228 public static void setName(String name) { 229 name_ = name; 230 } 231 } 232 | Popular Tags |