001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 2004 Jennifer Lhotak
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: package ca.mcgill.sable.soot.cfg;
021:
022: import org.eclipse.ui.*;
023: import org.eclipse.jface.action.*;
024: import org.eclipse.jface.viewers.*;
025: import org.eclipse.swt.widgets.Shell;
026: import org.eclipse.swt.widgets.Display;
027: import org.eclipse.draw2d.*;
028: import org.eclipse.draw2d.graph.*;
029: import org.eclipse.draw2d.geometry.*;
030: import java.util.*;
031: import ca.mcgill.sable.soot.launching.*;
032: import soot.toolkits.graph.*;
033: import org.eclipse.swt.SWT;
034:
035: public class CFGViewer {
036:
037: public void run(Object sootGraph) {
038: Shell shell = new Shell();
039: shell.open();
040: shell.setText("CFG Test");
041:
042: LightweightSystem lws = new LightweightSystem(shell);
043:
044: Panel p = new Panel();
045: HashMap nodeMap = new HashMap();
046: org.eclipse.draw2d.graph.DirectedGraph dg = makeSootGraph((soot.toolkits.graph.DirectedGraph) sootGraph);
047: Iterator nIt = dg.nodes.iterator();
048: while (nIt.hasNext()) {
049: Node nextNode = (Node) nIt.next();
050: IFigure node = new RectangleFigure();
051: IFigure label = new Label((String) nextNode.data);
052: label.setSize(nextNode.width, 36);
053: node.add(label);
054: int len = ((String) nextNode.data).length() * 5;
055: node.setLocation(new Point(nextNode.x, nextNode.y));
056: node.setSize(nextNode.width, 36);
057: p.add(node);
058: nodeMap.put(nextNode, node);
059: }
060: Iterator eIt = dg.edges.iterator();
061: while (eIt.hasNext()) {
062: Edge nextEdge = (Edge) eIt.next();
063: PolylineConnection edge = new PolylineConnection();
064: ChopboxAnchor ca1 = new ChopboxAnchor((IFigure) nodeMap
065: .get(nextEdge.source));
066: ChopboxAnchor ca2 = new ChopboxAnchor((IFigure) nodeMap
067: .get(nextEdge.target));
068:
069: edge.setSourceAnchor(ca1);
070: edge.setTargetAnchor(ca2);
071: edge.setTargetDecoration(new PolygonDecoration());
072: p.add(edge);
073: }
074:
075: lws.setContents(p);
076: Display display = Display.getDefault();
077: while (!shell.isDisposed()) {
078: if (!display.readAndDispatch())
079: display.sleep();
080: }
081: }
082:
083: public org.eclipse.draw2d.graph.DirectedGraph makeSootGraph(
084: soot.toolkits.graph.DirectedGraph sootGraph) {
085: org.eclipse.draw2d.graph.DirectedGraph dg = new org.eclipse.draw2d.graph.DirectedGraph();
086: NodeList nodes = new NodeList();
087: EdgeList edges = new EdgeList();
088: HashMap nodeMap = new HashMap();
089:
090: Iterator it = sootGraph.iterator();
091: while (it.hasNext()) {
092: Object node = it.next();
093: Node n;
094: if (!nodeMap.containsKey(node)) {
095: n = new Node(node.toString());
096: n.width = node.toString().length() * 7;
097: nodes.add(n);
098: nodeMap.put(node, n);
099: } else {
100: n = (Node) nodeMap.get(node);
101: }
102: Iterator succIt = sootGraph.getSuccsOf(node).iterator();
103: while (succIt.hasNext()) {
104: Object succ = succIt.next();
105: Node s;
106: if (!nodeMap.containsKey(succ)) {
107: s = new Node(succ.toString());
108: s.width = s.toString().length() * 7;
109: nodes.add(s);
110: nodeMap.put(succ, s);
111: } else {
112: s = (Node) nodeMap.get(succ);
113: }
114: Edge e = new Edge(n, s);
115: edges.add(e);
116: }
117: }
118:
119: dg.nodes = nodes;
120: dg.edges = edges;
121: DirectedGraphLayout dgl = new DirectedGraphLayout();
122: dgl.visit(dg);
123: return dg;
124: }
125:
126: public org.eclipse.draw2d.graph.DirectedGraph makeSimpleGraph() {
127: NodeList nl = new NodeList();
128: Node n1 = new Node();
129: String data = "y = 3";
130: n1.data = data;
131: n1.width = data.length() * 7;
132: nl.add(n1);
133: Node n2 = new Node();
134: data = "if i >= 10 goto L0";
135: n2.data = data;
136: n2.width = data.length() * 7;
137: nl.add(n2);
138: Node n3 = new Node();
139: data = "if i != 0 goto L1";
140: n3.data = data;
141: n3.width = data.length() * 7;
142: nl.add(n3);
143: Node n4 = new Node();
144: data = "x = 5";
145: n4.data = data;
146: n4.width = data.length() * 7;
147: nl.add(n4);
148: EdgeList el = new EdgeList();
149: Edge e1 = new Edge(n1, n2);
150: el.add(e1);
151: Edge e2 = new Edge(n2, n3);
152: el.add(e2);
153: Edge e3 = new Edge(n2, n4);
154: el.add(e3);
155: org.eclipse.draw2d.graph.DirectedGraph dg = new org.eclipse.draw2d.graph.DirectedGraph();
156: dg.edges = el;
157: dg.nodes = nl;
158: DirectedGraphLayout dgl = new DirectedGraphLayout();
159: dgl.visit(dg);
160: return dg;
161: }
162:
163: public void selectionChanged(IAction action, ISelection selection) {
164:
165: }
166:
167: public void dispose() {
168:
169: }
170:
171: public void init(IWorkbenchWindow window) {
172:
173: }
174:
175: public CFGViewer() {
176: super();
177: }
178:
179: }
|