1 package Analysis.FlatIRGraph;
8 public class FlatIRGraph {
12 private BufferedWriter flatbw;
14 private HashSet<FlatNode> visited;
15 private HashSet<FlatNode> toVisit;
17 private int labelindex;
18 private Hashtable<FlatNode, Integer> flatnodetolabel;
20 public FlatIRGraph(State state, boolean tasks, boolean usermethods, boolean libmethods) throws java.io.IOException {
26 if( usermethods || libmethods )
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());
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());
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
53 flatnodetolabel=new Hashtable<FlatNode, Integer>();
54 visited=new HashSet<FlatNode>();
58 flatbw=new BufferedWriter( new FileWriter( graphname+"_flatIRGraph.dot" ) );
59 flatbw.write("digraph "+graphname+" {\n");
61 visited=new HashSet<FlatNode>();
62 toVisit=new HashSet<FlatNode>();
65 while( !toVisit.isEmpty() ) {
66 FlatNode fn=(FlatNode)toVisit.iterator().next();
70 if( fn.kind() == FKind.FlatMethod ) {
71 // FlatMethod does not have toString
72 flatbw.write( makeDotNodeDec( graphname, flatnodetolabel.get(fn), fn.getClass().getName(), "FlatMethod" ) );
74 flatbw.write( makeDotNodeDec( graphname, flatnodetolabel.get(fn), fn.getClass().getName(), fn.toString() ) );
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" );
81 if( !visited.contains( nn ) ) {
87 flatbw.write( "}\n" );
91 private void labelFlatNodes(FlatNode 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)) {
102 private String makeNodeName( String graphname, Integer id, String type ) {
103 String s = String.format( "%05d", id );
104 return "FN"+s+"_"+type;
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";
111 return " node"+id+"[label=\""+makeNodeName(graphname,id,type)+"\\n"+details+"\"];\n";