From: jjenista Date: Fri, 9 Nov 2007 20:53:03 +0000 (+0000) Subject: FlatIRGraph for creating dot files of Flat IR. X-Git-Tag: preEdgeChange~378 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=eceb071ef3be7f0b33212810ceea4ae24934f24e;p=IRC.git FlatIRGraph for creating dot files of Flat IR. --- diff --git a/Robust/src/Analysis/FlatIRGraph/FlatIRGraph.java b/Robust/src/Analysis/FlatIRGraph/FlatIRGraph.java new file mode 100644 index 00000000..fac5e59b --- /dev/null +++ b/Robust/src/Analysis/FlatIRGraph/FlatIRGraph.java @@ -0,0 +1,114 @@ +package Analysis.FlatIRGraph; + +import IR.*; +import IR.Flat.*; +import java.util.*; +import java.io.*; + +public class FlatIRGraph { + + private State state; + + private BufferedWriter flatbw; + + private HashSet visited; + private HashSet toVisit; + + private int labelindex; + private Hashtable flatnodetolabel; + + public FlatIRGraph(State state, boolean tasks, boolean usermethods, boolean libmethods) throws java.io.IOException { + this.state=state; + + if( tasks ) + graphTasks(); + + if( usermethods || libmethods ) + graphMethods(); + } + + private void graphTasks() throws java.io.IOException { + for(Iterator it_tasks=state.getTaskSymbolTable().getDescriptorsIterator();it_tasks.hasNext();) { + TaskDescriptor td = (TaskDescriptor)it_tasks.next(); + FlatMethod fm = state.getMethodFlat(td); + writeFlatIRGraph(fm,"task"+td.getSymbol()); + } + } + + private void graphMethods() throws java.io.IOException { + for(Iterator it_classes=state.getClassSymbolTable().getDescriptorsIterator();it_classes.hasNext();) { + ClassDescriptor cd = (ClassDescriptor)it_classes.next(); + for(Iterator it_methods=cd.getMethods();it_methods.hasNext();) { + MethodDescriptor md = (MethodDescriptor)it_methods.next(); + FlatMethod fm = state.getMethodFlat(md); + writeFlatIRGraph(fm,cd.getSymbol()+"."+md.getSymbol()); + } + } + } + + private void writeFlatIRGraph( FlatMethod fm, String graphname ) throws java.io.IOException { + // give every node in the flat IR graph a unique label + // so a human being can inspect the graph and verify + // correctness + flatnodetolabel=new Hashtable(); + visited=new HashSet(); + labelindex=0; + labelFlatNodes( fm ); + + flatbw=new BufferedWriter( new FileWriter( graphname+"_flatIRGraph.dot" ) ); + flatbw.write("digraph "+graphname+" {\n"); + + visited=new HashSet(); + toVisit=new HashSet(); + toVisit.add(fm); + + while( !toVisit.isEmpty() ) { + FlatNode fn=(FlatNode)toVisit.iterator().next(); + toVisit.remove(fn); + visited.add(fn); + + if( fn.kind() == FKind.FlatMethod ) { + // FlatMethod does not have toString + flatbw.write( makeDotNodeDec( graphname, flatnodetolabel.get(fn), fn.getClass().getName(), "FlatMethod" ) ); + } else { + flatbw.write( makeDotNodeDec( graphname, flatnodetolabel.get(fn), fn.getClass().getName(), fn.toString() ) ); + } + + for(int i=0;i node"+flatnodetolabel.get(nn)+";\n" ); + + if( !visited.contains( nn ) ) { + toVisit.add( nn ); + } + } + } + + flatbw.write( "}\n" ); + flatbw.close(); + } + + private void labelFlatNodes(FlatNode fn) { + visited.add(fn); + flatnodetolabel.put(fn,new Integer(labelindex++)); + for(int i=0;i