1 19 20 package edu.umd.cs.findbugs.ba; 21 22 import java.util.BitSet ; 23 24 import org.apache.bcel.classfile.Method; 25 import org.apache.bcel.generic.IINC; 26 import org.apache.bcel.generic.IndexedInstruction; 27 import org.apache.bcel.generic.Instruction; 28 import org.apache.bcel.generic.InstructionHandle; 29 import org.apache.bcel.generic.LoadInstruction; 30 import org.apache.bcel.generic.LocalVariableInstruction; 31 import org.apache.bcel.generic.MethodGen; 32 import org.apache.bcel.generic.RET; 33 import org.apache.bcel.generic.StoreInstruction; 34 35 47 public class LiveLocalStoreAnalysis extends BackwardDataflowAnalysis<BitSet > 48 implements Debug { 49 private int topBit ; 50 private int killedByStoreOffset; 51 52 public LiveLocalStoreAnalysis(MethodGen methodGen, ReverseDepthFirstSearch rdfs, DepthFirstSearch dfs) { 53 super(rdfs, dfs); 54 this.topBit = methodGen.getMaxLocals() * 2; 55 this.killedByStoreOffset = methodGen.getMaxLocals(); 56 } 57 58 public BitSet createFact() { 59 return new BitSet (); 60 } 61 62 public void copy(BitSet source, BitSet dest) { 63 dest.clear(); 64 dest.or(source); 65 } 66 67 public void initEntryFact(BitSet result) throws DataflowAnalysisException { 68 result.clear(); 69 } 70 71 public void initResultFact(BitSet result) { 72 makeFactTop(result); 73 } 74 75 public void makeFactTop(BitSet fact) { 76 fact.clear(); 77 fact.set(topBit); 78 } 79 80 public boolean same(BitSet fact1, BitSet fact2) { 81 return fact1.equals(fact2); 82 } 83 84 public void meetInto(BitSet fact, Edge edge, BitSet result) throws DataflowAnalysisException { 85 isFactValid(fact); 86 isFactValid(result); 87 88 if (isTop(fact)) { 89 } else if (isTop(result)) { 91 copy(fact, result); 93 } else { 94 result.or(fact); 96 } 97 98 isFactValid(result); 99 } 100 101 @Override 102 public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock, BitSet fact) 103 throws DataflowAnalysisException { 104 isFactValid(fact); 105 106 Instruction ins = handle.getInstruction(); 107 108 if (ins instanceof StoreInstruction || ins instanceof IINC) { 109 112 LocalVariableInstruction store = (LocalVariableInstruction) ins; 113 int local = store.getIndex(); 114 fact.clear(local); 115 fact.set(local + killedByStoreOffset); 116 } 117 118 if (ins instanceof LoadInstruction || ins instanceof IINC || ins instanceof RET) { 119 122 IndexedInstruction load = (IndexedInstruction) ins; 123 int local = load.getIndex(); 124 fact.set(local); 125 fact.clear(local + killedByStoreOffset); 126 } 127 128 isFactValid(fact); 129 } 130 131 @Override 132 public boolean isFactValid(BitSet fact) { 133 if (VERIFY_INTEGRITY) { 134 if (isTop(fact) && fact.nextSetBit(0) < topBit) 135 throw new IllegalStateException (); 136 } 137 return !isTop(fact); 138 } 139 140 @Override 141 public String factToString(BitSet fact) { 142 if (isTop(fact)) 143 return "[TOP]"; 144 else 145 return fact.toString(); 146 } 147 148 151 public boolean isTop(BitSet fact) { 152 return fact.get(topBit); 153 } 154 155 161 public boolean isStoreAlive(BitSet fact, int local) { 162 return fact.get(local); 163 } 164 165 169 public boolean killedByStore(BitSet fact, int local) { 170 return fact.get(local + killedByStoreOffset); 171 } 172 173 public static void main(String [] argv) throws Exception { 174 if (argv.length != 1) { 175 System.err.println("Usage: " + LiveLocalStoreAnalysis.class.getName() + 176 " <classfile>"); 177 System.exit(1); 178 } 179 180 String filename = argv[0]; 181 182 DataflowTestDriver<BitSet ,LiveLocalStoreAnalysis> driver = 183 new DataflowTestDriver<BitSet , LiveLocalStoreAnalysis>() { 184 185 @Override 186 public Dataflow<BitSet , LiveLocalStoreAnalysis> createDataflow(ClassContext classContext, Method method) 187 throws CFGBuilderException, DataflowAnalysisException { 188 return classContext.getLiveLocalStoreDataflow(method); 189 } 190 }; 191 192 driver.execute(filename); 193 } 194 } 195 196 | Popular Tags |