1 package Analysis.FlatIRGraph;
8 public class FlatIRGraph {
12 public FlatIRGraph(State state, boolean tasks, boolean usermethods, boolean libmethods) throws java.io.IOException {
18 if( usermethods || libmethods )
22 private void graphTasks() throws java.io.IOException {
23 for(Iterator it_tasks=state.getTaskSymbolTable().getDescriptorsIterator(); it_tasks.hasNext(); ) {
24 TaskDescriptor td = (TaskDescriptor)it_tasks.next();
25 FlatMethod fm = state.getMethodFlat(td);
26 writeFlatIRGraph(fm,"task"+td.getSymbol());
30 private void graphMethods() throws java.io.IOException {
31 for(Iterator it_classes=state.getClassSymbolTable().getDescriptorsIterator(); it_classes.hasNext(); ) {
32 ClassDescriptor cd = (ClassDescriptor)it_classes.next();
33 for(Iterator it_methods=cd.getMethods(); it_methods.hasNext(); ) {
34 MethodDescriptor md = (MethodDescriptor)it_methods.next();
35 FlatMethod fm = state.getMethodFlat(md);
36 writeFlatIRGraph(fm,cd.getSymbol()+"."+md.getSymbol());
43 static BufferedWriter flatbw;
45 static HashSet<FlatNode> visited;
46 static HashSet<FlatNode> toVisit;
48 static int labelindex;
49 static Hashtable<FlatNode, Integer> flatnodetolabel;
52 static public void writeFlatIRGraph(FlatMethod fm, String graphname) throws java.io.IOException {
56 // give every node in the flat IR graph a unique label
57 // so a human being can inspect the graph and verify
59 flatnodetolabel=new Hashtable<FlatNode, Integer>();
60 visited=new HashSet<FlatNode>();
64 // take symbols out of graphname that cause dot to fail
65 graphname = graphname.replaceAll("[\\W]", "");
67 flatbw=new BufferedWriter(new FileWriter(graphname+"_flatIRGraph.dot") );
68 flatbw.write("digraph "+graphname+" {\n");
70 visited=new HashSet<FlatNode>();
71 toVisit=new HashSet<FlatNode>();
74 while( !toVisit.isEmpty() ) {
75 FlatNode fn=(FlatNode)toVisit.iterator().next();
79 if( fn.kind() == FKind.FlatMethod ) {
80 // FlatMethod does not have toString
81 flatbw.write(makeDotNodeDec(graphname, flatnodetolabel.get(fn), fn.getClass().getName(), "FlatMethod") );
83 flatbw.write(makeDotNodeDec(graphname, flatnodetolabel.get(fn), fn.getClass().getName(), fn.toString() ) );
86 for(int i=0; i<fn.numNext(); i++) {
87 FlatNode nn=fn.getNext(i);
88 flatbw.write(" node"+flatnodetolabel.get(fn)+" -> node"+flatnodetolabel.get(nn)+";\n");
90 if( !visited.contains(nn) ) {
101 static private void labelFlatNodes(FlatNode fn) {
103 flatnodetolabel.put(fn,new Integer(labelindex++));
104 for(int i=0; i<fn.numNext(); i++) {
105 FlatNode nn=fn.getNext(i);
106 if(!visited.contains(nn)) {
113 static private String makeNodeName(String graphname, Integer id, String type) {
114 String s = String.format("%05d", id);
115 return "FN"+s+"_"+type;
118 static private String makeDotNodeDec(String graphname, Integer id, String type, String details) {
119 if( details == null ) {
120 return " node"+id+"[label=\""+makeNodeName(graphname,id,type)+"\"];\n";
122 return " node"+id+"[label=\""+makeNodeName(graphname,id,type)+"\\n"+details+"\"];\n";