| skipped 22 lines |
23 | 23 | | public class DotGraphVisitor extends AbstractVisitor { |
24 | 24 | | |
25 | 25 | | private static final String NL = "\\l"; |
26 | | - | private static final boolean PRINT_REGISTERS_STATES = false; |
27 | 26 | | |
28 | 27 | | private final File dir; |
29 | 28 | | private final boolean useRegions; |
30 | 29 | | private final boolean rawInsn; |
31 | | - | |
32 | | - | private CodeWriter dot; |
33 | | - | private CodeWriter conn; |
34 | 30 | | |
35 | 31 | | public DotGraphVisitor(File outDir, boolean useRegions, boolean rawInsn) { |
36 | 32 | | this.dir = outDir; |
| skipped 10 lines |
47 | 43 | | if (mth.isNoCode()) { |
48 | 44 | | return; |
49 | 45 | | } |
50 | | - | dot = new CodeWriter(); |
51 | | - | conn = new CodeWriter(); |
| 46 | + | new DumpDotGraph().process(mth); |
| 47 | + | } |
| 48 | + | |
| 49 | + | private class DumpDotGraph { |
| 50 | + | private CodeWriter dot = new CodeWriter(); |
| 51 | + | private CodeWriter conn = new CodeWriter(); |
52 | 52 | | |
53 | | - | dot.startLine("digraph \"CFG for"); |
54 | | - | dot.add(escape(mth.getParentClass().getFullName() + "." + mth.getMethodInfo().getShortId())); |
55 | | - | dot.add("\" {"); |
| 53 | + | public void process(MethodNode mth) { |
| 54 | + | dot.startLine("digraph \"CFG for"); |
| 55 | + | dot.add(escape(mth.getParentClass().getFullName() + "." + mth.getMethodInfo().getShortId())); |
| 56 | + | dot.add("\" {"); |
56 | 57 | | |
57 | | - | if (useRegions) { |
58 | | - | if (mth.getRegion() == null) { |
59 | | - | return; |
60 | | - | } |
61 | | - | processMethodRegion(mth); |
62 | | - | } else { |
63 | | - | for (BlockNode block : mth.getBasicBlocks()) { |
64 | | - | processBlock(mth, block); |
| 58 | + | if (useRegions) { |
| 59 | + | if (mth.getRegion() == null) { |
| 60 | + | return; |
| 61 | + | } |
| 62 | + | processMethodRegion(mth); |
| 63 | + | } else { |
| 64 | + | for (BlockNode block : mth.getBasicBlocks()) { |
| 65 | + | processBlock(mth, block, false); |
| 66 | + | } |
65 | 67 | | } |
66 | | - | } |
67 | 68 | | |
68 | | - | dot.startLine("MethodNode[shape=record,label=\"{"); |
69 | | - | dot.add(escape(mth.getAccessFlags().makeString())); |
70 | | - | dot.add(escape(mth.getReturnType() + " " |
71 | | - | + mth.getParentClass().getFullName() + "." + mth.getName() |
72 | | - | + "(" + Utils.listToString(mth.getArguments(true)) + ") ")); |
| 69 | + | dot.startLine("MethodNode[shape=record,label=\"{"); |
| 70 | + | dot.add(escape(mth.getAccessFlags().makeString())); |
| 71 | + | dot.add(escape(mth.getReturnType() + " " |
| 72 | + | + mth.getParentClass().getFullName() + "." + mth.getName() |
| 73 | + | + "(" + Utils.listToString(mth.getArguments(true)) + ") ")); |
73 | 74 | | |
74 | | - | String attrs = attributesString(mth); |
75 | | - | if (attrs.length() != 0) { |
76 | | - | dot.add(" | ").add(attrs); |
77 | | - | } |
78 | | - | dot.add("}\"];"); |
| 75 | + | String attrs = attributesString(mth); |
| 76 | + | if (attrs.length() != 0) { |
| 77 | + | dot.add(" | ").add(attrs); |
| 78 | + | } |
| 79 | + | dot.add("}\"];"); |
79 | 80 | | |
80 | | - | dot.startLine("MethodNode -> ").add(makeName(mth.getEnterBlock())).add(';'); |
| 81 | + | dot.startLine("MethodNode -> ").add(makeName(mth.getEnterBlock())).add(';'); |
81 | 82 | | |
82 | | - | dot.add(conn.toString()); |
| 83 | + | dot.add(conn.toString()); |
83 | 84 | | |
84 | | - | dot.startLine('}'); |
85 | | - | dot.startLine(); |
| 85 | + | dot.startLine('}'); |
| 86 | + | dot.startLine(); |
86 | 87 | | |
87 | | - | String fileName = Utils.escape(mth.getMethodInfo().getShortId()) |
88 | | - | + (useRegions ? ".regions" : "") |
89 | | - | + (rawInsn ? ".raw" : "") |
90 | | - | + ".dot"; |
91 | | - | dot.save(dir, mth.getParentClass().getClassInfo().getFullPath() + "_graphs", fileName); |
92 | | - | } |
| 88 | + | String fileName = Utils.escape(mth.getMethodInfo().getShortId()) |
| 89 | + | + (useRegions ? ".regions" : "") |
| 90 | + | + (rawInsn ? ".raw" : "") |
| 91 | + | + ".dot"; |
| 92 | + | dot.save(dir, mth.getParentClass().getClassInfo().getFullPath() + "_graphs", fileName); |
| 93 | + | } |
93 | 94 | | |
94 | | - | private void processMethodRegion(MethodNode mth) { |
95 | | - | processRegion(mth, mth.getRegion()); |
96 | | - | for (ExceptionHandler h : mth.getExceptionHandlers()) { |
97 | | - | if (h.getHandlerRegion() != null) { |
98 | | - | processRegion(mth, h.getHandlerRegion()); |
| 95 | + | private void processMethodRegion(MethodNode mth) { |
| 96 | + | processRegion(mth, mth.getRegion()); |
| 97 | + | for (ExceptionHandler h : mth.getExceptionHandlers()) { |
| 98 | + | if (h.getHandlerRegion() != null) { |
| 99 | + | processRegion(mth, h.getHandlerRegion()); |
| 100 | + | } |
| 101 | + | } |
| 102 | + | Set<BlockNode> regionsBlocks = new HashSet<BlockNode>(mth.getBasicBlocks().size()); |
| 103 | + | RegionUtils.getAllRegionBlocks(mth.getRegion(), regionsBlocks); |
| 104 | + | for (ExceptionHandler handler : mth.getExceptionHandlers()) { |
| 105 | + | IContainer handlerRegion = handler.getHandlerRegion(); |
| 106 | + | if (handlerRegion != null) { |
| 107 | + | RegionUtils.getAllRegionBlocks(handlerRegion, regionsBlocks); |
| 108 | + | } |
99 | 109 | | } |
100 | | - | } |
101 | | - | Set<BlockNode> regionsBlocks = new HashSet<BlockNode>(mth.getBasicBlocks().size()); |
102 | | - | RegionUtils.getAllRegionBlocks(mth.getRegion(), regionsBlocks); |
103 | | - | for (ExceptionHandler handler : mth.getExceptionHandlers()) { |
104 | | - | IContainer handlerRegion = handler.getHandlerRegion(); |
105 | | - | if (handlerRegion != null) { |
106 | | - | RegionUtils.getAllRegionBlocks(handlerRegion, regionsBlocks); |
| 110 | + | for (BlockNode block : mth.getBasicBlocks()) { |
| 111 | + | if (!regionsBlocks.contains(block)) { |
| 112 | + | processBlock(mth, block, true); |
| 113 | + | } |
107 | 114 | | } |
108 | 115 | | } |
109 | | - | for (BlockNode block : mth.getBasicBlocks()) { |
110 | | - | if (!regionsBlocks.contains(block)) { |
111 | | - | processBlock(mth, block, true); |
| 116 | + | |
| 117 | + | private void processRegion(MethodNode mth, IContainer region) { |
| 118 | + | if (region instanceof IRegion) { |
| 119 | + | IRegion r = (IRegion) region; |
| 120 | + | dot.startLine("subgraph " + makeName(region) + " {"); |
| 121 | + | dot.startLine("label = \"").add(r); |
| 122 | + | String attrs = attributesString(r); |
| 123 | + | if (attrs.length() != 0) { |
| 124 | + | dot.add(" | ").add(attrs); |
| 125 | + | } |
| 126 | + | dot.add("\";"); |
| 127 | + | dot.startLine("node [shape=record,color=blue];"); |
| 128 | + | |
| 129 | + | for (IContainer c : r.getSubBlocks()) { |
| 130 | + | processRegion(mth, c); |
| 131 | + | } |
| 132 | + | |
| 133 | + | dot.startLine('}'); |
| 134 | + | } else if (region instanceof BlockNode) { |
| 135 | + | processBlock(mth, (BlockNode) region, false); |
112 | 136 | | } |
113 | 137 | | } |
114 | | - | } |
115 | 138 | | |
116 | | - | private void processRegion(MethodNode mth, IContainer region) { |
117 | | - | if (region instanceof IRegion) { |
118 | | - | IRegion r = (IRegion) region; |
119 | | - | dot.startLine("subgraph " + makeName(region) + " {"); |
120 | | - | dot.startLine("label = \"").add(r); |
121 | | - | String attrs = attributesString(r); |
| 139 | + | private void processBlock(MethodNode mth, BlockNode block, boolean error) { |
| 140 | + | String attrs = attributesString(block); |
| 141 | + | dot.startLine(makeName(block)); |
| 142 | + | dot.add(" [shape=record,"); |
| 143 | + | if (error) { |
| 144 | + | dot.add("color=red,"); |
| 145 | + | } |
| 146 | + | dot.add("label=\"{"); |
| 147 | + | dot.add(block.getId()).add("\\:\\ "); |
| 148 | + | dot.add(InsnUtils.formatOffset(block.getStartOffset())); |
122 | 149 | | if (attrs.length() != 0) { |
123 | | - | dot.add(" | ").add(attrs); |
| 150 | + | dot.add('|').add(attrs); |
124 | 151 | | } |
125 | | - | dot.add("\";"); |
126 | | - | dot.startLine("node [shape=record,color=blue];"); |
| 152 | + | String insns = insertInsns(mth, block); |
| 153 | + | if (insns.length() != 0) { |
| 154 | + | dot.add('|').add(insns); |
| 155 | + | } |
| 156 | + | dot.add("}\"];"); |
127 | 157 | | |
128 | | - | for (IContainer c : r.getSubBlocks()) { |
129 | | - | processRegion(mth, c); |
| 158 | + | BlockNode falsePath = null; |
| 159 | + | List<InsnNode> list = block.getInstructions(); |
| 160 | + | if (!list.isEmpty() && list.get(0).getType() == InsnType.IF) { |
| 161 | + | falsePath = ((IfNode) list.get(0)).getElseBlock(); |
130 | 162 | | } |
131 | | - | |
132 | | - | dot.startLine('}'); |
133 | | - | } else if (region instanceof BlockNode) { |
134 | | - | processBlock(mth, (BlockNode) region); |
135 | | - | } |
136 | | - | } |
137 | | - | |
138 | | - | private void processBlock(MethodNode mth, BlockNode block) { |
139 | | - | processBlock(mth, block, false); |
140 | | - | } |
141 | | - | |
142 | | - | private void processBlock(MethodNode mth, BlockNode block, boolean error) { |
143 | | - | String attrs = attributesString(block); |
144 | | - | if (PRINT_REGISTERS_STATES) { |
145 | | - | if (block.getStartState() != null) { |
146 | | - | if (attrs.length() != 0) { |
147 | | - | attrs += "|"; |
| 163 | + | for (BlockNode next : block.getSuccessors()) { |
| 164 | + | conn.startLine(makeName(block)).add(" -> ").add(makeName(next)); |
| 165 | + | if (next == falsePath) { |
| 166 | + | conn.add("[style=dotted]"); |
148 | 167 | | } |
149 | | - | attrs += escape("RS: " + block.getStartState()) + NL; |
150 | | - | attrs += escape("RE: " + block.getEndState()) + NL; |
| 168 | + | conn.add(';'); |
151 | 169 | | } |
152 | 170 | | } |
153 | | - | dot.startLine(makeName(block)); |
154 | | - | dot.add(" [shape=record,"); |
155 | | - | if (error) { |
156 | | - | dot.add("color=red,"); |
157 | | - | } |
158 | | - | dot.add("label=\"{"); |
159 | | - | dot.add(block.getId()).add("\\:\\ "); |
160 | | - | dot.add(InsnUtils.formatOffset(block.getStartOffset())); |
161 | | - | if (attrs.length() != 0) { |
162 | | - | dot.add('|').add(attrs); |
163 | | - | } |
164 | | - | String insns = insertInsns(mth, block); |
165 | | - | if (insns.length() != 0) { |
166 | | - | dot.add('|').add(insns); |
167 | | - | } |
168 | | - | dot.add("}\"];"); |
169 | 171 | | |
170 | | - | BlockNode falsePath = null; |
171 | | - | List<InsnNode> list = block.getInstructions(); |
172 | | - | if (!list.isEmpty() && list.get(0).getType() == InsnType.IF) { |
173 | | - | falsePath = ((IfNode) list.get(0)).getElseBlock(); |
174 | | - | } |
175 | | - | for (BlockNode next : block.getSuccessors()) { |
176 | | - | conn.startLine(makeName(block)).add(" -> ").add(makeName(next)); |
177 | | - | if (next == falsePath) { |
178 | | - | conn.add("[style=dotted]"); |
| 172 | + | private String attributesString(IAttributeNode block) { |
| 173 | + | StringBuilder attrs = new StringBuilder(); |
| 174 | + | for (String attr : block.getAttributes().getAttributeStrings()) { |
| 175 | + | attrs.append(escape(attr)).append(NL); |
179 | 176 | | } |
180 | | - | conn.add(';'); |
| 177 | + | return attrs.toString(); |
181 | 178 | | } |
182 | | - | } |
183 | 179 | | |
184 | | - | private String attributesString(IAttributeNode block) { |
185 | | - | StringBuilder attrs = new StringBuilder(); |
186 | | - | for (String attr : block.getAttributes().getAttributeStrings()) { |
187 | | - | attrs.append(escape(attr)).append(NL); |
| 180 | + | private String makeName(IContainer c) { |
| 181 | + | String name; |
| 182 | + | if (c instanceof BlockNode) { |
| 183 | + | name = "Node_" + ((BlockNode) c).getId(); |
| 184 | + | } else { |
| 185 | + | name = "cluster_" + c.getClass().getSimpleName() + "_" + c.hashCode(); |
| 186 | + | } |
| 187 | + | return name; |
188 | 188 | | } |
189 | | - | return attrs.toString(); |
190 | | - | } |
191 | 189 | | |
192 | | - | private static String makeName(IContainer c) { |
193 | | - | String name; |
194 | | - | if (c instanceof BlockNode) { |
195 | | - | name = "Node_" + ((BlockNode) c).getId(); |
196 | | - | } else { |
197 | | - | name = "cluster_" + c.getClass().getSimpleName() + "_" + c.hashCode(); |
| 190 | + | private String insertInsns(MethodNode mth, BlockNode block) { |
| 191 | + | if (rawInsn) { |
| 192 | + | StringBuilder str = new StringBuilder(); |
| 193 | + | for (InsnNode insn : block.getInstructions()) { |
| 194 | + | str.append(escape(insn + " " + insn.getAttributes())); |
| 195 | + | str.append(NL); |
| 196 | + | } |
| 197 | + | return str.toString(); |
| 198 | + | } else { |
| 199 | + | CodeWriter code = new CodeWriter(0); |
| 200 | + | MethodGen.addFallbackInsns(code, mth, block.getInstructions(), false); |
| 201 | + | String str = escape(code.newLine().toString()); |
| 202 | + | if (str.startsWith(NL)) { |
| 203 | + | str = str.substring(NL.length()); |
| 204 | + | } |
| 205 | + | return str; |
| 206 | + | } |
198 | 207 | | } |
199 | | - | return name; |
200 | | - | } |
201 | 208 | | |
202 | | - | private String insertInsns(MethodNode mth, BlockNode block) { |
203 | | - | if (rawInsn) { |
204 | | - | StringBuilder str = new StringBuilder(); |
205 | | - | for (InsnNode insn : block.getInstructions()) { |
206 | | - | str.append(escape(insn + " " + insn.getAttributes())); |
207 | | - | str.append(NL); |
208 | | - | } |
209 | | - | return str.toString(); |
210 | | - | } else { |
211 | | - | CodeWriter code = new CodeWriter(0); |
212 | | - | MethodGen.addFallbackInsns(code, mth, block.getInstructions(), false); |
213 | | - | String str = escape(code.newLine().toString()); |
214 | | - | if (str.startsWith(NL)) { |
215 | | - | str = str.substring(NL.length()); |
216 | | - | } |
217 | | - | return str; |
| 209 | + | private String escape(String string) { |
| 210 | + | return string |
| 211 | + | .replace("\\", "") // TODO replace \" |
| 212 | + | .replace("/", "\\/") |
| 213 | + | .replace(">", "\\>").replace("<", "\\<") |
| 214 | + | .replace("{", "\\{").replace("}", "\\}") |
| 215 | + | .replace("\"", "\\\"") |
| 216 | + | .replace("-", "\\-") |
| 217 | + | .replace("|", "\\|") |
| 218 | + | .replace("\n", NL); |
218 | 219 | | } |
219 | | - | } |
220 | | - | |
221 | | - | private static String escape(String string) { |
222 | | - | return string |
223 | | - | .replace("\\", "") // TODO replace \" |
224 | | - | .replace("/", "\\/") |
225 | | - | .replace(">", "\\>").replace("<", "\\<") |
226 | | - | .replace("{", "\\{").replace("}", "\\}") |
227 | | - | .replace("\"", "\\\"") |
228 | | - | .replace("-", "\\-") |
229 | | - | .replace("|", "\\|") |
230 | | - | .replace("\n", NL); |
231 | 220 | | } |
232 | 221 | | } |
233 | 222 | | |