-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
executable file
·375 lines (286 loc) · 10.2 KB
/
parse.py
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""
parse.py
A module for parsing assembly language to identify mnemonics, labels, sections, and operands, registers.
@author: Rob Whitaker
@date: March 2017
"""
from flask import g
import re, json
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from format import DivFormatter, OpLexer
#**********************************************************************
# Module constants
#**********************************************************************
# Number of different colors to use in matching
ncolors = 5
# Allowed size suffixes and values
sizes = {'q': 8, 'l' : 4, 'w' : 2, 'b' : 1}
# Load dictionary of mnemonic tooltips
with open('ref.json') as file:
ref = json.load(file)
# Allowed directives
whitelist = ['.section', '.globl', '.byte', '.word', \
'.long', '.quad', '.type', '.asciz', '.ascii', '.string', '.skip']
# Disallowed labels
badlabel = re.compile('((.LF)|(.Ltemp)|(.Ltmp)|(.Ltext)|(.LVL)|(.Lfunc)|(.Letext)).*')
#**********************************************************************
# Markup format strings
#**********************************************************************
# Format string for a mnemonic tooltip
tooltip = """
<div class="m-tt">
<div class="tt-row">
<span class="tt-title"> {entry[name]} </span>
<span class="tt-syn">
<span class="tt-mnem"> {mnem} </span> {entry[syn]}
</span>
</div>
<div class="tt-row">
<span class="tt-desc">{entry[desc]}</span>
</div>
<div class="tt-row">
Flags affected:
<span class="tt-flags"> {entry[flags]} </span>
</div>
</div><!-- /.tt -->
"""
location = """
<div class="v-tt">
<div class="tt-row">
<span class="tt-type">{entry[type]}</span>
<span class="tt-mnem">{entry[name]}</span>
<span class="tt-linum tt-right">(decl. line {entry[line]})</span>
</div>
<div class="tt-row">
<span class="tt-linum tt-left">{entry[role]} in </span>
<span class="tt-mnem"> {fcn}()</span>
</div>
</div>
"""
linelabel = """
<div class="line-label">(line {})</div>
"""
colordiv = """
<div id="for-line-{}" class="loc color-{}">
"""
# Format string for a classed div
div = """
<div id="{d}" class="{cl}">{cx}</div>
"""
# Format string for a classed span
span = """
<span class="{cl}">{cx}</span>
"""
#**********************************************************************
# lexers and formatters
#**********************************************************************
srclexer = get_lexer_by_name('c')
oplexer = OpLexer(stripall=True)
srcfmtr = DivFormatter(
cssclass='c-line',
classprefix='c-token-'
)
opformatter = DivFormatter(
cssclass="operand-text",
classprefix="token-op-",
spanwrap=True
)
#**********************************************************************
# Markup format functions
#**********************************************************************
def handle_mnemonic(token, cl):
""" Process a token corresponding to a mnemonic. Return a formatted
element including tooltip generated from ref.json.
"""
# look up in dictionary
if token in ref:
# mnemnoic without size options
entry = ref[token]
elif token[:-1] in ref:
# search for base form of mnemonic
entry = ref[token[:-1]]
suffix = token[-1]
# suffix not recognized
if suffix not in sizes:
return wrap_token(token, cl)
# edit size list appropriately
sz = sizes[suffix]
entry['size'] = [(sz if s == 0 else s) for s in entry['size']]
else:
# tooltip for mnemonic not supported
return wrap_token(token, cl)
# Wrap text with span
token_wrapped = span.format(cl="token-text", cx=token)
# Append tooltip
cx = token_wrapped + tooltip.format(mnem=token, entry=entry)
# Wrap in div and return
return div.format(d="", cl="asm-mnemonic asm-token", cx=cx)
def wrap_token(token, cl):
""" Process an token in assembly and return the default formatting.
"""
inner = span.format(cl="token-text", cx=token)
return div.format(d="", cl=cl, cx=inner)
#**********************************************************************
# Main processing functions
#**********************************************************************
def process_asm(asm):
""" Process an entire assembly file asm, represented as a list
of one string for each line. Return a marked up version for
rendering with jinja2 """
# TODO: REFACTOR THIS FUNCTION INTO MORE MANAGEABLE PIECES
# Open markup with non colored block
markup = '<div class="asm-header">'
block_is_open = True
# Initialize dictionary of line numbers and colors
colors = {}
# Keep track of blocks and lines of assembly
blocknum = 0
asmline = 0
g.fnc = None
# Iterate over each line
for line in asm:
# first escape html tags
line = line.replace('<', '<').replace('>', '>')
# convert tabs to spaces
line = line.replace('\t', ' '*4)
# split into tokens
tokens = tokenize(line)
if not tokens[0]:
continue
# handle formatting for special lines
if tokens[0] == '.loc':
# do line-matching
cline = int(tokens[2])
# determine color-class of line
if cline not in colors:
colors[cline] = (blocknum % ncolors) + 1
blocknum += 1
# close previous div
if block_is_open:
markup += '</div><!-- /.loc -->\n'
# open new div of appropriate color class
markup += colordiv.format(cline, colors[cline])
block_is_open = True
# add label for the line
markup += linelabel.format(cline)
# don't actually output the text of the line
continue
if tokens[0].startswith('.'):
# don't output non-essential directives
if not tokens[0].endswith(':') and tokens[0] not in whitelist:
continue
elif block_is_open:
# close the current block
markup += '</div><!-- /.loc -->\n'
block_is_open = False
if tokens[0].endswith(':'):
if badlabel.match(tokens[0]):
# don't output debugging labels
continue
elif block_is_open:
# close the current block
markup += '</div><!-- /.loc -->\n'
block_is_open = False
if len(tokens) > 1 and '.debug' in tokens[1]:
break
if '@function' in tokens:
# get current function name
g.fcn = tokens[1].strip(', ')
# output line number
asmline += 1
markup += '<div class="asm-line">'
markup += div.format(d="asm-line-" + str(asmline), cl="asm-no", cx=asmline)
# handle formatting for general lines
markup += process_tokens(tokens)
markup += '</div>'
markup += '</div><!-- /.loc -->'
return markup, colors
def tokenize(line):
""" Take a ilne of assembly as a string and return a list of
the assembly language tokens that it contains.
"""
# Remove comments
line = line.split('#')[0]
# Remove leading and trailing spaces
line = line.strip()
# If no quotes, split on spaces or commas
if '"' not in line:
return re.split(r'[\s,]+', line)
# Process quotes and tokens separately
tokens = []
quote = '"{}"'
for i, bit in enumerate(line.split('"')):
if i % 2 == 0:
# Outside quote
tokens += bit.split()
else:
# Inside quote
tokens.append(quote.format(bit))
return tokens
def process_tokens(tokens):
""" Handle the markup for the first token in a line.
Can be a label, directive, or mnemonic.
"""
markup = ''
if tokens[0] == 'rep':
tokens[0] += ' ' + tokens.pop(1)
if tokens[0].endswith(':'):
return wrap_token(tokens[0], 'asm-token asm-label')
elif tokens[0].startswith('.'):
markup += wrap_token(tokens[0], 'asm-token asm-directive')
else:
markup += handle_mnemonic(tokens[0], 'asm-token asm-mnemonic')
# For mnemonic operands
for token in tokens[1:]:
markup += process_operand(token)
return markup
# For directive operands
for token in tokens[1:]:
markup += wrap_token(token, 'asm-token')
return markup
def process_operand(token):
""" Handle the markup for the subsequent tokens in a line.
Can be a directive, register, addressing mode, .
"""
# Skip comments
if token.startswith('#'): return ''
# generic token class and operand class
cl = 'asm-token asm-operand'
# lex operand with custom operand lexer
cx = highlight(token, oplexer, opformatter)
# do lookup in address table and add location tooltip to markup
entry = g.locs[g.fcn].get(token.rstrip(','), None)
if entry is not None:
cx += location.format(entry=entry, fcn=g.fcn)
return wrap_token(cx, cl)
def format_c(c, colors=[]):
""" Take a list of lines of c code and generate the corresponding
markup. If colors is provided, include color classes in markup.
"""
if c is None:
return None
markup = ''
for i, line in enumerate(c):
# 1-based line numbering
l = i+1
# syntax highlighting with pygments
line = highlight(line, srclexer, srcfmtr)
# LOOK FOR THOSE DECLARATIONS
# Number
no = div.format(d="", cl="c-no", cx=l)
cl = "src-line"
# Add appropriate color class
if l in colors:
cl += " loc color-" + str(colors[l])
markup += div.format(d="src-line-"+str(l), cl=cl, cx=no+line)
return markup
def annotate_declarations(markup):
""" Search markup for all the variable declarations predicted from
the locations dictionary. Add a tooltip with the variable's location
as a child of the container for each declaration.
"""
# Make the declaration dictionary
# Search the markup for the appropriate declarations
# Could use the
pass