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);
45 // make sure the graph name reflects the method signature so
46 // overloaded methods don't clobber one another
47 String graphName = cd.getSymbol()+"."+md.getSymbol();
48 for (int i = 0; i < fm.numParameters(); ++i) {
49 graphName += fm.getParameter(i).getSymbol();
51 writeFlatIRGraph(fm, graphName);
56 public void writeFlatIRGraph(FlatMethod fm, String graphname) throws java.io.IOException {
57 // give every node in the flat IR graph a unique label
58 // so a human being can inspect the graph and verify
60 flatnodetolabel=new Hashtable<FlatNode, Integer>();
61 visited=new HashSet<FlatNode>();
65 // take symbols out of graphname that cause dot to fail
66 graphname = graphname.replaceAll("[\\W]", "");
68 flatbw=new BufferedWriter(new FileWriter("FLATIR_"+graphname+".dot") );
69 flatbw.write("digraph "+graphname+" {\n");
71 visited=new HashSet<FlatNode>();
72 toVisit=new HashSet<FlatNode>();
75 while( !toVisit.isEmpty() ) {
76 FlatNode fn=(FlatNode)toVisit.iterator().next();
80 if( fn.kind() == FKind.FlatMethod ) {
81 // FlatMethod does not have toString
82 flatbw.write(makeDotNodeDec(graphname, flatnodetolabel.get(fn), fn.getClass().getName(), "FlatMethod") );
84 flatbw.write(makeDotNodeDec(graphname, flatnodetolabel.get(fn), fn.getClass().getName(), fn.toString() ) );
87 for(int i=0; i<fn.numNext(); i++) {
88 FlatNode nn=fn.getNext(i);
89 flatbw.write(" node"+flatnodetolabel.get(fn)+" -> node"+flatnodetolabel.get(nn)+";\n");
91 if( !visited.contains(nn) ) {
101 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)) {
112 private String makeNodeName(String graphname, Integer id, String type) {
113 String s = String.format("%05d", id);
114 return "FN"+s+"_"+type;
117 private String makeDotNodeDec(String graphname, Integer id, String type, String details) {
118 if( details == null ) {
119 return " node"+id+"[label=\""+makeNodeName(graphname,id,type)+"\"];\n";
121 return " node"+id+"[label=\""+makeNodeName(graphname,id,type)+"\\n"+details+"\"];\n";