bug fixes
[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     private BufferedWriter flatbw;
13
14     private HashSet<FlatNode> visited;
15     private HashSet<FlatNode> toVisit;
16     
17     private int labelindex;
18     private Hashtable<FlatNode, Integer> flatnodetolabel;
19
20     public FlatIRGraph(State state, boolean tasks, boolean usermethods, boolean libmethods) throws java.io.IOException {
21         this.state=state;
22
23         if( tasks )
24             graphTasks();
25
26         if( usermethods || libmethods )
27             graphMethods();
28     }
29
30     private void graphTasks() throws java.io.IOException {
31         for(Iterator it_tasks=state.getTaskSymbolTable().getDescriptorsIterator();it_tasks.hasNext();) {
32             TaskDescriptor td = (TaskDescriptor)it_tasks.next();
33             FlatMethod fm = state.getMethodFlat(td);
34             writeFlatIRGraph(fm,"task"+td.getSymbol());
35         }       
36     }
37
38     private void graphMethods() throws java.io.IOException {
39         for(Iterator it_classes=state.getClassSymbolTable().getDescriptorsIterator();it_classes.hasNext();) {
40             ClassDescriptor cd = (ClassDescriptor)it_classes.next();
41             for(Iterator it_methods=cd.getMethods();it_methods.hasNext();) {
42                 MethodDescriptor md = (MethodDescriptor)it_methods.next();
43                 FlatMethod fm = state.getMethodFlat(md);
44                 writeFlatIRGraph(fm,cd.getSymbol()+"."+md.getSymbol());
45             }
46         }       
47     }
48
49     private void writeFlatIRGraph( FlatMethod fm, String graphname ) throws java.io.IOException {
50         // give every node in the flat IR graph a unique label
51         // so a human being can inspect the graph and verify
52         // correctness
53         flatnodetolabel=new Hashtable<FlatNode, Integer>();
54         visited=new HashSet<FlatNode>();
55         labelindex=0;
56         labelFlatNodes( fm );
57
58         flatbw=new BufferedWriter( new FileWriter( graphname+"_flatIRGraph.dot" ) );
59         flatbw.write("digraph "+graphname+" {\n");
60
61         visited=new HashSet<FlatNode>();
62         toVisit=new HashSet<FlatNode>();
63         toVisit.add(fm);
64
65         while( !toVisit.isEmpty() ) {
66             FlatNode fn=(FlatNode)toVisit.iterator().next();
67             toVisit.remove(fn);
68             visited.add(fn);
69
70             if( fn.kind() == FKind.FlatMethod ) {
71                 // FlatMethod does not have toString
72                 flatbw.write( makeDotNodeDec( graphname, flatnodetolabel.get(fn), fn.getClass().getName(), "FlatMethod" ) );
73             } else {
74                 flatbw.write( makeDotNodeDec( graphname, flatnodetolabel.get(fn), fn.getClass().getName(), fn.toString() ) );
75             }
76             
77             for(int i=0;i<fn.numNext();i++) {
78                 FlatNode nn=fn.getNext(i);
79                 flatbw.write( "  node"+flatnodetolabel.get(fn)+" -> node"+flatnodetolabel.get(nn)+";\n" );
80
81                 if( !visited.contains( nn ) ) {
82                     toVisit.add( nn );
83                 }
84             }
85         }
86
87         flatbw.write( "}\n" );
88         flatbw.close();
89     }
90
91     private void labelFlatNodes(FlatNode fn) {
92         visited.add(fn);
93         flatnodetolabel.put(fn,new Integer(labelindex++));
94         for(int i=0;i<fn.numNext();i++) {
95             FlatNode nn=fn.getNext(i);
96             if(!visited.contains(nn)) {
97                 labelFlatNodes(nn);
98             }
99         }
100     }
101
102     private String makeNodeName( String graphname, Integer id, String type ) {
103         String s = String.format( "%05d", id );
104         return "FN"+s+"_"+type;
105     }
106
107     private String makeDotNodeDec( String graphname, Integer id, String type, String details ) {
108         if( details == null ) {
109             return "  node"+id+"[label=\""+makeNodeName(graphname,id,type)+"\"];\n";
110         } else {
111             return "  node"+id+"[label=\""+makeNodeName(graphname,id,type)+"\\n"+details+"\"];\n";
112         }
113     }
114 }