summaryrefslogtreecommitdiffstats
path: root/test/test/util/DotExporter.java
blob: 61ee5ebf965f130bb0b3fc3baba3b27ee4f11163 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package test.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;


import de.fernflower.code.cfg.BasicBlock;
import de.fernflower.code.cfg.ControlFlowGraph;
import de.fernflower.modules.decompiler.StatEdge;
import de.fernflower.modules.decompiler.sforms.DirectGraph;
import de.fernflower.modules.decompiler.sforms.DirectNode;
import de.fernflower.modules.decompiler.stats.Statement;
import de.fernflower.modules.decompiler.vars.VarVersionEdge;
import de.fernflower.modules.decompiler.vars.VarVersionNode;
import de.fernflower.modules.decompiler.vars.VarVersionsGraph;

public class DotExporter {

	
	public static String toDotFormat(Statement stat) {
		
		StringBuffer buffer = new StringBuffer();
		
		buffer.append("digraph G {\r\n");
		
		for(Statement st : stat.getStats()) {
			
			String sourceid = st.id + (st.getSuccessorEdges(StatEdge.TYPE_EXCEPTION).isEmpty()?"":"000000");
			
			buffer.append(sourceid+" [shape=box,label=\""+sourceid+"\"];\r\n");
			
			for(StatEdge edge : st.getSuccessorEdges(Statement.STATEDGE_DIRECT_ALL)) {
				String destid = edge.getDestination().id + (edge.getDestination().getSuccessorEdges(StatEdge.TYPE_EXCEPTION).isEmpty()?"":"000000");

				buffer.append(sourceid+"->"+destid+";\r\n");
				
				if(!stat.getStats().contains(edge.getDestination())) {
					buffer.append(destid+" [label=\""+destid+"\"];\r\n");
				}
			}

			for(StatEdge edge : st.getSuccessorEdges(StatEdge.TYPE_EXCEPTION)) {
				String destid = edge.getDestination().id + (edge.getDestination().getSuccessorEdges(StatEdge.TYPE_EXCEPTION).isEmpty()?"":"000000");

				buffer.append(sourceid+" -> "+destid+" [style=dotted];\r\n");

				if(!stat.getStats().contains(edge.getDestination())) {
					buffer.append(destid+" [label=\""+destid+"\"];\r\n");
				}
			}
		}
		
		buffer.append("}");
		
		return buffer.toString(); 
	}
	
	
	public static String toDotFormat(ControlFlowGraph graph, boolean showMultipleEdges) {
		
		StringBuffer buffer = new StringBuffer();
		
		buffer.append("digraph G {\r\n");
		
		List<BasicBlock> blocks = graph.getBlocks(); 
		for(int i=0;i<blocks.size();i++) {
			BasicBlock block = (BasicBlock)blocks.get(i);
			
			buffer.append(block.id+" [shape=box,label=\""+block.id+"\"];\r\n");
			
			
			List<BasicBlock> suc = block.getSuccs();
			if(!showMultipleEdges) {
				HashSet<BasicBlock> set = new HashSet<BasicBlock>();
				set.addAll(suc);
				suc = Collections.list(Collections.enumeration(set));
			}
			for(int j=0;j<suc.size();j++) {
				buffer.append(block.id+"->"+((BasicBlock)suc.get(j)).id+";\r\n");
			}

			
			suc = block.getSuccExceptions();
			if(!showMultipleEdges) {
				HashSet<BasicBlock> set = new HashSet<BasicBlock>();
				set.addAll(suc);
				suc = Collections.list(Collections.enumeration(set));
			}
			for(int j=0;j<suc.size();j++) {
				buffer.append(block.id+" -> "+((BasicBlock)suc.get(j)).id+" [style=dotted];\r\n");
			}
		}
		
		buffer.append("}");
		
		return buffer.toString(); 
	}

	public static String toDotFormat(VarVersionsGraph graph) {
		
		StringBuffer buffer = new StringBuffer();
		
		buffer.append("digraph G {\r\n");
		
		List<VarVersionNode> blocks = graph.nodes; 
		for(int i=0;i<blocks.size();i++) {
			VarVersionNode block = blocks.get(i);
			
			buffer.append((block.var*1000+block.version)+" [shape=box,label=\""+block.var+"_"+block.version+"\"];\r\n");
			
			for(VarVersionEdge edge: block.succs) {
				VarVersionNode dest = edge.dest;
				buffer.append((block.var*1000+block.version)+"->"+(dest.var*1000+dest.version)+(edge.type==VarVersionEdge.EDGE_PHANTOM?" [style=dotted]":"")+";\r\n");
			}
		}
		
		buffer.append("}");
		
		return buffer.toString(); 
	}

	public static String toDotFormat(DirectGraph graph) {
		
		StringBuffer buffer = new StringBuffer();
		
		buffer.append("digraph G {\r\n");
		
		List<DirectNode> blocks = graph.nodes; 
		for(int i=0;i<blocks.size();i++) {
			DirectNode block = blocks.get(i);
			
			buffer.append(directBlockIdToDot(block.id)+" [shape=box,label=\""+directBlockIdToDot(block.id)+"\"];\r\n");
			
			for(DirectNode dest: block.succs) {
				buffer.append(directBlockIdToDot(block.id)+"->"+directBlockIdToDot(dest.id)+";\r\n");
			}
		}
		
		buffer.append("}");
		
		return buffer.toString(); 
	}
	
	private static String directBlockIdToDot(String id) {
		id = id.replaceAll("_try", "999");
		id = id.replaceAll("_tail", "888");

		id = id.replaceAll("_init", "111");
		id = id.replaceAll("_cond", "222");
		id = id.replaceAll("_inc", "333");
		return id;
	}

	public static void toDotFile(ControlFlowGraph graph, File file, boolean showMultipleEdges) throws FileNotFoundException, IOException {
		BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); 
		out.write(toDotFormat(graph, showMultipleEdges).getBytes());
		out.close(); 
	}

	public static void toDotFile(VarVersionsGraph graph, File file) throws FileNotFoundException, IOException {
		
		BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); 
		out.write(toDotFormat(graph).getBytes());
		out.close(); 
	}

	public static void toDotFile(DirectGraph graph, File file) throws FileNotFoundException, IOException {
		
		BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); 
		out.write(toDotFormat(graph).getBytes());
		out.close(); 
	}

	public static void toDotFile(Statement stat, File file) throws FileNotFoundException, IOException {
		
		BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); 
		out.write(toDotFormat(stat).getBytes());
		out.close(); 
	}
	
}