import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Vector;
import Analysis.CallGraph.CallGraph;
import Analysis.Loops.GlobalFieldType;
-import Analysis.Loops.LoopFinder;
import Analysis.Loops.LoopOptimize;
import Analysis.Loops.LoopTerminate;
import IR.AnnotationDescriptor;
import IR.Descriptor;
import IR.FieldDescriptor;
import IR.MethodDescriptor;
+import IR.NameDescriptor;
import IR.State;
import IR.SymbolTable;
import IR.TypeUtil;
public static final String THISLOC = "THISLOC";
public static final String GLOBALLOC = "GLOBALLOC";
public static final String RETURNLOC = "RETURNLOC";
+ public static final String PCLOC = "PCLOC";
public static final String LOC = "LOC";
public static final String DELTA = "DELTA";
public static final String TERMINATE = "TERMINATE";
// the set of method descriptors annotated as "TRUST"
Set<MethodDescriptor> trustWorthyMDSet;
+ // method -> the initial program counter location
+ Map<MethodDescriptor, CompositeLocation> md2pcLoc;
+
// points to method containing SSJAVA Loop
private MethodDescriptor methodContainingSSJavaLoop;
private LinkedList<MethodDescriptor> sortedDescriptors;
+ private Map<Location, Set<Descriptor>> mapSharedLocToDescSet;
+
+ private Map<Descriptor, SSJavaLattice<String>> mapDescToCompleteLattice;
+ public Map<Descriptor, Integer> mapNumLocsMapManual;
+ public Map<Descriptor, Integer> mapNumPathsMapManual;
+
public SSJavaAnalysis(State state, TypeUtil tu, BuildFlat bf, CallGraph callgraph) {
this.state = state;
this.tu = tu;
this.sameHeightWriteFlatNodeSet = new HashSet<FlatNode>();
this.mapDescriptorToSetDependents = new Hashtable<Descriptor, Set<MethodDescriptor>>();
this.sortedDescriptors = new LinkedList<MethodDescriptor>();
+ this.md2pcLoc = new HashMap<MethodDescriptor, CompositeLocation>();
+ this.mapSharedLocToDescSet = new HashMap<Location, Set<Descriptor>>();
+ this.mapDescToCompleteLattice = new HashMap<Descriptor, SSJavaLattice<String>>();
+ this.mapNumLocsMapManual = new HashMap<Descriptor, Integer>();
+ this.mapNumPathsMapManual = new HashMap<Descriptor, Integer>();
}
public void doCheck() {
doMethodAnnotationCheck();
- computeLinearTypeCheckMethodSet();
- doLinearTypeCheck();
- init();
+ if (state.SSJAVA && !state.SSJAVAINFER) {
+ init();
+ computeLinearTypeCheckMethodSet();
+ doLinearTypeCheck();
+ }
if (state.SSJAVADEBUG) {
- // debugPrint();
+ // debug_printAnnotationRequiredSet();
}
if (state.SSJAVAINFER) {
inference();
+ System.exit(0);
} else {
parseLocationAnnotation();
+ debug_countNumLocations();
+ // System.exit(0);
doFlowDownCheck();
doDefinitelyWrittenCheck();
doLoopCheck();
}
+
+ for (Iterator iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
+ MethodDescriptor md = (MethodDescriptor) iterator.next();
+ MethodLattice<String> locOrder = getMethodLattice(md);
+ writeLatticeDotFile(md.getClassDesc(), md, getMethodLattice(md));
+ System.out.println("~~~\t" + md.getClassDesc() + "_" + md + "\t"
+ + locOrder.getKeySet().size());
+ }
+
+ }
+
+ private void debug_countNumLocations() {
+
+ BuildLattice buildLattice = new BuildLattice();
+
+ for (Iterator iterator = cd2lattice.keySet().iterator(); iterator.hasNext();) {
+ ClassDescriptor cd = (ClassDescriptor) iterator.next();
+ SSJavaLattice<String> lattice = cd2lattice.get(cd).clone();
+ SSJavaLattice<String> completeLattice = debug_buildCompleteLattice(buildLattice, cd, lattice);
+ mapDescToCompleteLattice.put(cd, completeLattice);
+ writeLatticeDotFile(cd, null, completeLattice);
+ }
+
+ for (Iterator iterator = md2lattice.keySet().iterator(); iterator.hasNext();) {
+ MethodDescriptor md = (MethodDescriptor) iterator.next();
+ SSJavaLattice<String> lattice = md2lattice.get(md).clone();
+ SSJavaLattice<String> completeLattice = debug_buildCompleteLattice(buildLattice, md, lattice);
+ mapDescToCompleteLattice.put(md, completeLattice);
+ writeLatticeDotFile(md.getClassDesc(), md, completeLattice);
+ }
+
+ for (Iterator iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
+ MethodDescriptor md = (MethodDescriptor) iterator.next();
+ SSJavaLattice<String> lattice = getMethodLattice(md).clone();
+ if (!mapDescToCompleteLattice.containsKey(md)) {
+ System.out.println("@NOT FOUND!");
+ SSJavaLattice<String> completeLattice =
+ debug_buildCompleteLattice(buildLattice, md, lattice);
+ mapDescToCompleteLattice.put(md, completeLattice);
+ writeLatticeDotFile(md.getClassDesc(), md, completeLattice);
+ }
+ }
+
+ writeNumLocsPathsCSVFile();
+
+ }
+
+ public SSJavaLattice<String> debug_buildCompleteLattice(BuildLattice buildLattice,
+ Descriptor desc, SSJavaLattice<String> lattice) {
+
+ // First, create a hierarchy graph
+ HierarchyGraph hierarchyGraph = new HierarchyGraph();
+ Set<String> keySet = lattice.getKeySet();
+
+ Map<String, Descriptor> mapLocNameToDesc = new HashMap<String, Descriptor>();
+
+ for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
+ String higher = (String) iterator2.next();
+ Set<String> lowerSet = lattice.get(higher);
+ if (!mapLocNameToDesc.containsKey(higher)) {
+ mapLocNameToDesc.put(higher, new NameDescriptor(higher));
+ }
+
+ Descriptor higherDesc = mapLocNameToDesc.get(higher);
+
+ for (Iterator iterator3 = lowerSet.iterator(); iterator3.hasNext();) {
+ String lower = (String) iterator3.next();
+ if (!mapLocNameToDesc.containsKey(lower)) {
+ mapLocNameToDesc.put(lower, new NameDescriptor(lower));
+ }
+ Descriptor lowerDesc = mapLocNameToDesc.get(lower);
+ hierarchyGraph.addEdge(higherDesc, lowerDesc);
+ }
+ }
+
+ BasisSet basisSet = hierarchyGraph.computeBasisSet(new HashSet<HNode>());
+
+ SSJavaLattice<String> completeLattice = buildLattice.buildLattice(hierarchyGraph);
+
+ int numLocs = completeLattice.getKeySet().size() + 1;
+ LocationInference.numLocationsSInfer += numLocs;
+ mapNumLocsMapManual.put(desc, new Integer(numLocs));
+
+ System.out.println(desc + "::" + "lattice=" + lattice.getKeySet().size() + " complete="
+ + numLocs);
+
+ int numPaths = completeLattice.countPaths();
+ LocationInference.numLocationsSInfer += numPaths;
+ mapNumPathsMapManual.put(desc, new Integer(numPaths));
+
+ return completeLattice;
}
- private void init() {
+ public void writeNumLocsPathsCSVFile() {
+
+ try {
+ BufferedWriter bw = new BufferedWriter(new FileWriter("manualnumbers.csv"));
+
+ Set<Descriptor> keySet = mapNumLocsMapManual.keySet();
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ Descriptor desc = (Descriptor) iterator.next();
+ int numLocs = mapNumLocsMapManual.get(desc);
+ int numPaths = mapNumPathsMapManual.get(desc);
+ bw.write(desc.getSymbol().replaceAll("[,]", "") + "," + numLocs + "," + numPaths + "\n");
+ }
+ bw.close();
+
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ }
+
+ public void init() {
// perform topological sort over the set of methods accessed by the main
// event loop
Set<MethodDescriptor> methodDescriptorsToAnalyze = new HashSet<MethodDescriptor>();
return (LinkedList<MethodDescriptor>) sortedDescriptors.clone();
}
+ public void addSharedDesc(Location loc, Descriptor fd) {
+ if (!mapSharedLocToDescSet.containsKey(loc)) {
+ mapSharedLocToDescSet.put(loc, new HashSet<Descriptor>());
+ }
+ mapSharedLocToDescSet.get(loc).add(fd);
+ }
+
+ public Set<Descriptor> getSharedDescSet(Location loc) {
+ return mapSharedLocToDescSet.get(loc);
+ }
+
private void inference() {
- LocationInference inferEngine = new LocationInference(this, state);
+ LocationInference inferEngine = new LocationInference(this, state, tu);
inferEngine.inference();
}
}
}
+ linearTypeCheckMethodSet.addAll(sortedDescriptors);
+
}
private void doLinearTypeCheck() {
checker.linearTypeCheck();
}
- public void debugPrint() {
+ public void debug_printAnnotationRequiredSet() {
System.out.println("SSJAVA: SSJava is checking the following methods:");
for (Iterator<MethodDescriptor> iterator = annotationRequireSet.iterator(); iterator.hasNext();) {
MethodDescriptor md = iterator.next();
- System.out.print(" " + md);
+ System.out.println(md);
}
System.out.println();
}
methodAnnotationChecker = new MethodAnnotationCheck(this, state, tu);
methodAnnotationChecker.methodAnnoatationCheck();
methodAnnotationChecker.methodAnnoataionInheritanceCheck();
+ if (state.SSJAVAINFER) {
+ annotationRequireClassSet.add(methodContainingSSJavaLoop.getClassDesc());
+ annotationRequireSet.add(methodContainingSSJavaLoop);
+ }
state.setAnnotationRequireSet(annotationRequireSet);
}
if (state.SSJAVADEBUG) {
// generate lattice dot file
writeLatticeDotFile(cd, null, locOrder);
+ System.out.println("~~~\t" + cd + "\t" + locOrder.getKeySet().size());
}
} else if (marker.equals(METHODDEFAULT)) {
new MethodLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
cd2methodDefault.put(cd, locOrder);
parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
+ // writeLatticeDotFile(cd, null, locOrder, "METHOD_DEFAULT");
}
}
MethodLattice<String> locOrder =
new MethodLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
md2lattice.put(md, locOrder);
+ System.out.println("parsing method lattice=" + md);
parseMethodDefaultLatticeDefinition(cd, an.getValue(), locOrder);
+ writeLatticeDotFile(cd, md, locOrder, "");
} else if (an.getMarker().equals(TERMINATE)) {
// developer explicitly wants to skip loop termination analysis
String value = an.getValue();
public <T> void writeLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
SSJavaLattice<T> locOrder) {
+ writeLatticeDotFile(cd, md, locOrder, "");
+
+ }
+
+ public <T> void writeLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
+ SSJavaLattice<T> locOrder, String nameSuffix) {
String fileName = "lattice_";
if (md != null) {
fileName +=
- cd.getSymbol().replaceAll("[\\W_]", "") + "_" + md.getSymbol().replaceAll("[\\W_]", "");
+ cd.getSymbol().replaceAll("[\\W_]", "") + "_" + md.toString().replaceAll("[\\W_]", "");
} else {
fileName += cd.getSymbol().replaceAll("[\\W_]", "");
}
+ fileName += nameSuffix;
+
Set<Pair<T, T>> pairSet = locOrder.getOrderingPairSet();
if (pairSet.size() > 0) {
} else {
lowLocStr = lowLocId.toString();
}
- bw.write(highLocId + " -> " + lowLocId + ";\n");
+ bw.write(highLocStr + " -> " + lowLocStr + ";\n");
}
bw.write("}\n");
bw.close();
locOrder.put(higherLoc, lowerLoc);
if (locOrder.isIntroducingCycle(higherLoc)) {
throw new Error("Error: the order relation " + lowerLoc + " < " + higherLoc
- + " introduces a cycle.");
+ + " introduces a cycle in the class lattice " + cd);
}
} else if (orderElement.contains("*")) {
// spin loc definition
}
}
+ public CompositeLocation getPCLocation(MethodDescriptor md) {
+ if (!md2pcLoc.containsKey(md)) {
+ // by default, the initial pc location is TOP
+ CompositeLocation pcLoc = new CompositeLocation(new Location(md, Location.TOP));
+ md2pcLoc.put(md, pcLoc);
+ }
+ return md2pcLoc.get(md);
+ }
+
+ public void setPCLocation(MethodDescriptor md, CompositeLocation pcLoc) {
+ md2pcLoc.put(md, pcLoc);
+ }
+
public boolean needToCheckLinearType(MethodDescriptor md) {
return linearTypeCheckMethodSet.contains(md);
}