KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > uka > ipd > coverage > natures > all_uses > SSABasicBlockBuilder


1 /*
2  * Created on Sep 16, 2004
3  * @author Matthias Kempka
4  */

5 package de.uka.ipd.coverage.natures.all_uses;
6
7 import java.util.*;
8
9 import org.apache.bcel.generic.*;
10
11 import de.uka.ipd.coverage.recording.BasicBlock;
12 import de.uka.ipd.coverage.recording.RegisteredMethod;
13 import de.uka.ipd.coverage.utils.Logger;
14
15 /**
16  *
17  * Builder for SsaBasicBlockWrapper objects. Call order is:
18  * buildNewSSABasicBlockWrappers()
19  * generateSSAForm()
20  * getSSABasicBlockWrappers()
21  * @author Matthias Kempka
22  *
23  */

24 public class SSABasicBlockBuilder {
25
26     private static ConstantPoolGen constantPoolGen;
27     private static MethodGen methodGen;
28     private static InstructionList il;
29     private static BasicBlock[] originalBlocks;
30     private static Map basicBlockMap;
31     private static SsaBasicBlockWrapper[] ssaBlocks;
32
33     /**
34      *
35      */

36     public SSABasicBlockBuilder() {
37         super();
38     }
39
40     public static SsaBasicBlockWrapper[] getSSABasicBlockWrappers() {
41         return ssaBlocks;
42     }
43
44     /**
45      *
46      */

47     public static void generateSSAForm() {
48         for (int i = 0; i < ssaBlocks.length; i++) {
49             ssaBlocks[i].generateSSAForm();
50         }
51          
52         for (int i = 0; i < ssaBlocks.length; i++) {
53             ssaBlocks[i].triggerSSAForm();
54             SsaBasicBlockWrapper[] succs = ssaBlocks[i].getSuccessorWrappers();
55             for (int j = 0; j < succs.length; j++) {
56                 succs[j].tryFinishing();
57             }
58         }
59
60         assert ensureSSAForm(methodGen) :
61             "SSA Form generator did not create SSA Form in method " + methodGen.getName(); //$NON-NLS-1$
62

63     }
64
65     /**
66      * @param callback
67      */

68     public static void buildSSABasicBlockWrappers(RegisteredMethod callback) {
69         constantPoolGen = new ConstantPoolGen(callback.getMethod().getConstantPool());
70         methodGen = new MethodGen(
71                         callback.getMethod(),
72                         callback.getJavaClass().getClassName(),
73                         constantPoolGen);
74         il = methodGen.getInstructionList();
75         originalBlocks = callback.getBasicBlocks();
76         basicBlockMap = new HashMap(originalBlocks.length * 3);
77         ssaBlocks = new SsaBasicBlockWrapper[originalBlocks.length];
78         for (int i = 0; i < ssaBlocks.length; i++) {
79             ssaBlocks[i] = new SsaBasicBlockWrapper(originalBlocks[i],
80                     il,
81                     methodGen);
82             basicBlockMap.put(originalBlocks[i], ssaBlocks[i]);
83         }
84         
85         Map fieldMap = new HashMap();
86         
87         // set variables that are shared from all blocks in a method.
88
for (int i = 0; i < ssaBlocks.length; i++) {
89             ssaBlocks[i].setBasicBlockMap(basicBlockMap);
90             ssaBlocks[i].setFieldMap(fieldMap);
91         }
92         
93     }
94
95     private static boolean ensureSSAForm(MethodGen method) {
96         InstructionHandle runningHandle = method.getInstructionList().getStart();
97         List storedVars = new ArrayList();
98         while (runningHandle !=
99             method.getInstructionList().getEnd()) {
100             if (runningHandle.getInstruction() instanceof StoreInstruction) {
101                 StoreInstruction store = (StoreInstruction) runningHandle.getInstruction();
102                 Integer JavaDoc integer = new Integer JavaDoc(store.getIndex());
103                 if (storedVars.contains(integer)) {
104                     Logger.getInstance().debug("SSA Form failed for " + method.getClassName() + "." + method.getName()); //$NON-NLS-1$ //$NON-NLS-2$
105
return false;
106                 }
107                 storedVars.add(integer);
108             }
109             runningHandle = runningHandle.getNext();
110         }
111         Logger.getInstance().debug("SSA Form ok for " + method.getClassName() + "." + method.getName()); //$NON-NLS-1$ //$NON-NLS-2$
112
return true;
113     }
114
115 }
116
Popular Tags