79a4414bdf07876bee45ba3c57e7d5d1442b2b80
[IRC.git] / Robust / src / Analysis / FlatIRGraph / FlatIRGraph.java
1 package Analysis.FlatIRGraph;
2
3 import IR.*;
4 import IR.Flat.*;
5 import java.util.*;
6 import java.io.*;
7
8 public class FlatIRGraph {
9
10   private State state;
11
12   public FlatIRGraph(State state, boolean tasks, boolean usermethods, boolean libmethods) throws java.io.IOException {
13     this.state=state;
14
15     if( tasks )
16       graphTasks();
17
18     if( usermethods || libmethods )
19       graphMethods();
20   }
21
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());
27     }
28   }
29
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());
37       }
38     }
39   }
40
41
42
43   static BufferedWriter flatbw;
44
45   static HashSet<FlatNode> visited;
46   static HashSet<FlatNode> toVisit;
47
48   static int labelindex;
49   static Hashtable<FlatNode, Integer> flatnodetolabel;
50
51   
52   static public void writeFlatIRGraph(FlatMethod fm, String graphname) throws java.io.IOException {
53
54
55
56     // give every node in the flat IR graph a unique label
57     // so a human being can inspect the graph and verify
58     // correctness
59     flatnodetolabel=new Hashtable<FlatNode, Integer>();
60     visited=new HashSet<FlatNode>();
61     labelindex=0;
62     labelFlatNodes(fm);
63
64     // take symbols out of graphname that cause dot to fail
65     graphname = graphname.replaceAll("[\\W]", "");
66
67     flatbw=new BufferedWriter(new FileWriter(graphname+"_flatIRGraph.dot") );
68     flatbw.write("digraph "+graphname+" {\n");
69
70     visited=new HashSet<FlatNode>();
71     toVisit=new HashSet<FlatNode>();
72     toVisit.add(fm);
73
74     while( !toVisit.isEmpty() ) {
75       FlatNode fn=(FlatNode)toVisit.iterator().next();
76       toVisit.remove(fn);
77       visited.add(fn);
78
79       if( fn.kind() == FKind.FlatMethod ) {
80         // FlatMethod does not have toString
81         flatbw.write(makeDotNodeDec(graphname, flatnodetolabel.get(fn), fn.getClass().getName(), "FlatMethod") );
82       } else {
83         flatbw.write(makeDotNodeDec(graphname, flatnodetolabel.get(fn), fn.getClass().getName(), fn.toString() ) );
84       }
85
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");
89
90         if( !visited.contains(nn) ) {
91           toVisit.add(nn);
92         }
93       }
94     }
95
96     flatbw.write("}\n");
97     flatbw.close();
98   }
99
100
101   static private void labelFlatNodes(FlatNode fn) {
102     visited.add(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)) {
107         labelFlatNodes(nn);
108       }
109     }
110   }
111
112
113   static private String makeNodeName(String graphname, Integer id, String type) {
114     String s = String.format("%05d", id);
115     return "FN"+s+"_"+type;
116   }
117
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";
121     } else {
122       return "  node"+id+"[label=\""+makeNodeName(graphname,id,type)+"\\n"+details+"\"];\n";
123     }
124   }
125 }