-
Notifications
You must be signed in to change notification settings - Fork 6
/
ddx2dot
executable file
·215 lines (174 loc) · 5.08 KB
/
ddx2dot
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python
import os
import sys
import re
import os.path
import pydot
# pydot has horrible quoting bugs. Monkey-patch away their quoting
# infrastructure with our own.
class QuotedString(str):
"""A string that has already been quoted for dot.
pydot assumes that it can quote a string multiple times and the
second and future calls will be no-ops. We implement this using a
simple subclass of `str' that lets us know we've already quoted
this string.
"""
pass
def do_quote(s):
if isinstance(s, QuotedString):
return s
if not isinstance(s, basestring):
return s
s = s.replace(' ', r'\ ').replace('"', r'\"')
return QuotedString('"%s"' % (s,))
pydot.quote_if_necessary = do_quote
def as_html(lines):
if not lines:
return ""
out = []
out.append('<<table border="0" cellborder="0" align="left">')
for line in lines:
out.append('<tr><td align="left">')
out.append(line[:-1].replace('\t', ' ').replace('<', '<').replace('>', '>'))
out.append('</td></tr>')
out.append('</table>>')
return QuotedString(''.join(out))
if len(sys.argv) != 4:
print >>sys.stderr, "Usage: %s FILE.ddx FUNCTION OUT-FILE" % (sys.argv[0],)
print >>sys.stderr
print >>sys.stderr, "Output format will be auto-detected from the extension on OUT-FILE."
print >>sys.stderr, "FUNCTION may contain the encoded type signature:"
print >>sys.stderr, " (e.g. 'doIt(IILjava/lang/String;)')"
sys.exit(1)
(ddx, func, out) = sys.argv[1:]
lines = []
infile = open(ddx, 'r')
for line in infile:
m = re.search(r'^\.method [a-z ]+ ([a-zA-Z0-9_<>]+\(.*)', line)
if m:
if m.group(1).startswith(func):
break
header = []
for line in infile:
if line.startswith(' ') or line.startswith('\t'):
lines.append(line)
break
else:
header.append(line)
for line in infile:
if line.startswith('.end method'):
break
lines.append(line)
gensym_cnt = 0
def gensym():
global gensym_cnt
gensym_cnt += 1
return "tmp_" + str(gensym_cnt)
def findExit(lines):
this_label = None
candidate = False
return_blocks = dict()
code = []
for line in lines:
if not line.strip(): continue
m = re.search(r'^\s*(:\w+)', line)
if m:
if candidate:
return_blocks[this_label] = code
code = []
this_label = m.group(1)
candidate = False
continue
code.append(line)
if line.strip().startswith('return'):
candidate = True
else:
candidate = False
return return_blocks
exitBlocks = findExit(lines)
graph = pydot.Dot()
was_goto = False
in_switch = False
code = header
block = 'header'
def switchBlock(to):
global block
if not was_goto:
graph.add_edge(pydot.Edge(block, to))
exitBlock()
block = to
def exitBlock():
node = pydot.Node(block, shape='box', fontname='monospace')
node.set_label(as_html(code))
graph.add_node(node)
del code[:]
def addEdgeTo(to,label='jmp'):
if to in exitBlocks:
tmp = gensym()
node = pydot.Node(tmp, label=as_html(exitBlocks[to]))
graph.add_node(node)
to = tmp
graph.add_edge(pydot.Edge(block, to, taillabel=label))
switchBlock('entry')
(NONE, SPARSE, PACKED) = range(3)
switch_table = {}
switch_idx = None
for line in lines:
if not line.strip(): continue
code.append(line)
if in_switch:
l = line.strip()
if l.startswith('.end'):
was_goto = True
in_switch = False
code = []
continue
else:
if in_switch == PACKED:
lbl = l
case = hex(switch_idx)
switch_idx += 1
else: # SPARSE
m = re.search(r'(0x[a-fA-F0-9]+)\s*->\s*(:\w+)', l)
lbl = m.group(2)
case = m.group(1)
addEdgeTo(lbl, case)
continue
m = re.search(r'^\s*(:\w+)', line)
if m:
del code[-1]
switchBlock(m.group(1))
continue
was_goto = False
insn = line.strip()
if insn.startswith('if-'):
to = insn.split()[-1]
addEdgeTo(to, 'true')
switchBlock(gensym())
if insn.startswith('return'):
addEdgeTo('return', '')
was_goto = True
if insn.startswith('throw'):
was_goto = True
elif insn.startswith('packed-switch') or \
insn.startswith('sparse-switch'):
label = insn.split()[-1]
switch_table[label] = block
switchBlock(gensym())
elif insn.startswith('goto'):
addEdgeTo(insn.split()[-1], 'goto')
was_goto = True
elif insn.startswith('.sparse-switch'):
in_switch = SPARSE
block = switch_table[block]
elif insn.startswith('.packed-switch'):
in_switch = PACKED
switch_idx = int(insn.split()[-1], 16)
block = switch_table[block]
if code:
exitBlock()
if '.' not in out:
extn = 'dot'
else:
extn = out[out.rindex(".")+1:]
graph.write(out, format=extn)