-
Notifications
You must be signed in to change notification settings - Fork 23
/
asm.py
447 lines (397 loc) · 22.6 KB
/
asm.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import copy
import itertools
import math
import re
import string
import xml.etree.cElementTree as ET
import pyleri
from pyleri import Grammar, Regex, Choice, Sequence, Token, Keyword, Repeat, Ref
from pyleri.node import Node
import explanations
from explanations import sanitize, asl_bitexpr_to_sail, bitlit_re, Explanation
def expand_dontcares(s):
if len(s) == 0:
return []
elif len(s) == 1:
if s in 'xX':
return ['0', '1']
elif s in '10':
return s
else:
assert False, 'expand_dontcares() saw non-1|0|x'
elif s[0] in 'xX':
rest = expand_dontcares(s[1:])
return (['0' + r for r in rest] +
['1' + r for r in rest])
elif s[0] in '10':
return [s[0] + r for r in expand_dontcares(s[1:])]
else:
assert False, 'expand_dontcares() saw non-1|0|x'
def name_or_const(guards, hi, lo, nm, split, consts, actual_consts):
if nm in guards and 'x' not in guards[nm]:
return '0b{}'.format(guards[nm], nm)
elif actual_consts != 'x' * (hi - lo + 1) and '!=' not in actual_consts:
return '0b{}'.format(actual_consts.replace('(', '').replace(')', ''), nm) # unpred
else:
return nm
guard_re = re.compile(r'([A-Za-z0-9]+) == \(?([01x]+)\)?')
neg_guard_re = re.compile(r'([A-Za-z0-9]+) != \(?([01x]+)\)?')
def parse_guards(s):
if s is None:
return ({}, {})
pos = {}
neg = {}
guards = s.split(' && ')
for g in guards:
m = guard_re.match(g)
if m:
pos[m.group(1)] = m.group(2)
else:
m = neg_guard_re.match(g)
if m:
neg[m.group(1)] = m.group(2)
else:
assert False
return (pos, neg)
def emit_sail_asm(file, enc):
enc_name, enc_iset, enc_fields, enc_asl, enc_asms = enc
for (gs, rhs) in enc_asms:
pos_guards, neg_guards = parse_guards(gs)
fields = [name_or_const(pos_guards, *f) for f in enc_fields if f[2] != '_']
lhs = '{}({})'.format(sanitize(enc_name), ', '.join(fields))
pos_guards = {k: expand_dontcares(v) for k, v in pos_guards.items()}
neg_guards = {k: expand_dontcares(v) for k, v in neg_guards.items()}
pos_sail_guards = ' & '.join(['(' + ' | '.join('{} == 0b{}'.format(k, v) for v in vs) + ')' for k, vs in pos_guards.items()])
neg_sail_guards = ' & '.join(['(' + ' & '.join('{} != 0b{}'.format(k, v) for v in vs) + ')' for k, vs in neg_guards.items()])
clause = 'mapping clause assembly = {}{}{} <-> {}'.format(lhs,
' if ' if neg_sail_guards else '',
neg_sail_guards,
rhs.replace(':', '@'))
print(clause, file=file)
class ASMTemplateGrammar(Grammar):
doublespace = Regex('\s\s+')
space = Regex('\s')
link = Regex('<[A-Za-z0-9_|()+]+>')
text = Regex('[A-Za-z0-9_[\]!,#.]+')
optional = Ref()
optional = Sequence('{', Repeat(Choice(link, text, optional, space), mi=1), '}')
bracket_alternative = Sequence('(', Repeat(Choice(link, text, space), mi=1), '|', Repeat(Choice(link, text, space), mi=1), ')')
# unbracket_alternative = Sequence(Choice(link, text), mi=1), '|', Repeat(Choice(link, text), mi=1))
optional_alternative = Sequence('{', Repeat(Choice(link, text, space), mi=1), '|', Repeat(Choice(link, text, space), mi=1), '}')
START = Repeat(Choice(doublespace, space, link, text, optional_alternative, bracket_alternative, optional), mi=1)
def _walk(self, element, pos, tree, rule, is_required):
if self._pos != pos:
self._s = self._string[pos:] #.lstrip() # don't strip whitespace
self._pos = self._len_string - len(self._s)
node = Node(element, self._string, self._pos)
self._expecting.set_mode_required(node.start, is_required)
return element._get_node_result(self, tree, rule, self._s, node)
asm_grammar = ASMTemplateGrammar()
class BitConcatsGrammar(Grammar):
START = Ref()
arg = Regex('[A-Za-z][A-Za-z0-9]*')
brackets = Sequence('(', START, ')')
literal = Regex('0b[01]+')
concat = pyleri.List(Choice(brackets, arg, literal), delimiter='@')
START = Choice(brackets, arg, literal, concat)
bit_concats_grammar = BitConcatsGrammar()
def fst_by_snd(pairs, target):
for fst, snd in pairs:
if target == snd:
return fst
raise KeyError(target)
def process_bitconcat_node_get_bits(types, node):
assert hasattr(node.element, 'name')
if node.element.name == 'START':
return process_bitconcat_node_get_bits(types, node.children[0])
elif node.element.name == 'literal':
return len(node.string) - 2 # remove the '0b'
elif node.element.name == 'brackets':
return process_bitconcat_node_get_bits(types, node.children[1])
elif node.element.name == 'arg':
return bits_type_to_n(types[node.string])
elif node.element.name == 'concat':
return sum(process_bitconcat_node_get_bits(types, n.children[0]) for n in node.children if str(n.element) != '@')
else:
assert False, 'unknown element type in process_bitconcat_node_get_bits'
def get_bitconcat_n_bits(types, bc):
parse = bit_concats_grammar.parse(bc)
assert parse.is_valid
start = parse.tree.children[0] if parse.tree.children else parse.tree # pyleri bug workaround?
return process_bitconcat_node_get_bits(types, start)
def process_bitconcat_node_typing(types, node):
assert hasattr(node.element, 'name')
if node.element.name == 'START':
return process_bitconcat_node_typing(types, node.children[0])
elif node.element.name == 'literal':
return '{}'.format(node.string, len(node.string) - 2) # (remove 0b) Type annotating this shouldn't be necessary but sail bug
elif node.element.name == 'brackets':
return '({})'.format(process_bitconcat_node_typing(types, node.children[1]))
elif node.element.name == 'arg':
return '({}:{})'.format(node.string, types[node.string])
elif node.element.name == 'concat':
return '@'.join(process_bitconcat_node_typing(types, n.children[0]) for n in node.children if str(n.element) != '@')
else:
assert False, 'unknown element type in process_bitconcat_node_typing'
def type_bitconcat(types, bc):
parse = bit_concats_grammar.parse(bc)
assert parse.is_valid
start = parse.tree.children[0] if parse.tree.children else parse.tree # pyleri bug workaround?
return process_bitconcat_node_typing(types, start)
class NoDefaultException(Exception): pass
def default_clause(explanations, types, arg, link):
exp = explanations[link]
try:
default = exp.props['default']
except KeyError as e:
if link.startswith('<extend>') and link.endswith('_32_addsub_ext') or link.endswith('_32S_addsub_ext'):
default = 'UXTW'
elif link.startswith('<extend>') and link.endswith('_64_addsub_ext') or link.endswith('_64S_addsub_ext'):
default = 'UXTX'
elif exp.type == 'asm_constant' and 'expr' in exp.props and exp.props['expr'] == 'PRESENCE':
default = '0'
else:
raise NoDefaultException() from e
m = bitlit_re.match(default)
if m:
return asl_bitexpr_to_sail(default)
elif ' ' in default:
assert exp.type == 'TABLE'
return '{}'.format(asl_bitexpr_to_sail(fst_by_snd(exp.values, default)))
elif default.isdigit():
return '0b{:0{}b}'.format(int(default), get_bitconcat_n_bits(types, arg))
elif default.isalnum():
assert exp.type == 'TABLE'
return '{}'.format(asl_bitexpr_to_sail(fst_by_snd(exp.values, '"' + default + '"')))
else:
assert False, "default_clause doesn't know how to handle {!r} for {!r}".format(default, link)
def generate_presence_explanation(instr_name, enc_name, explanations, types, el):
link = el.string + '_' + enc_name
el_exp = explanations[link]
values = [
('0b0', '"" if false /* hack */'),
('0b1', '"{}"'.format(el_exp.props['constant'])),
('0b0', '""'),
]
props = {
'encoded_in': '({})'.format(', '.join(el_exp.props['encoded_in'])),
'arg_type': 'bits({})'.format(get_bitconcat_n_bits(types, el_exp.props['encoded_in'])),
}
name = '{}_presence_{}'.format(enc_name, sanitize(el_exp.props['encoded_in']))
assert name not in explanations
explanations[name] = Explanation('TABLE', props, values)
return (name, [el_exp.props['encoded_in']], [link])
def generate_optional_explanation(instr_name, enc_name, explanations, types, children):
els_args = [process_element(instr_name, enc_name, explanations, types, child.children[0]) for child in children[1].children]
elements = list(itertools.chain.from_iterable([el_arg[0] for el_arg in els_args])) # flatten
args = list(itertools.chain.from_iterable([el_arg[1] for el_arg in els_args])) # flatten
links = list(itertools.chain.from_iterable([el_arg[2] for el_arg in els_args])) # flatten
values = [
('({})'.format(', '.join(default_clause(explanations, types, arg, link) for arg, link in zip(args, links))), '"" if false /* hack */'),
('({})'.format(', '.join(type_bitconcat(types, arg) for arg in args)), ' ^ '.join(elements)),
('({})'.format(', '.join(default_clause(explanations, types, arg, link) for arg, link in zip(args, links))), '""'),
]
props = {
'encoded_in': '({})'.format(', '.join(args)),
'arg_type': '({})'.format(', '.join('bits({})'.format(get_bitconcat_n_bits(types, arg)) for arg in args)) if len(args) > 0 else 'unit',
}
name = '{}_optional_{}'.format(enc_name, sanitize('_'.join(args)))
assert name not in explanations
explanations[name] = Explanation('TABLE', props, values)
return (name, args, links)
def generate_alternative_explanation(instr_name, enc_name, explanations, types, children1, children2):
els_args1 = [process_element(instr_name, enc_name, explanations, types, child.children[0]) for child in children1]
elements1 = list(itertools.chain.from_iterable([el_arg[0] for el_arg in els_args1])) # flatten
args1 = list(itertools.chain.from_iterable([el_arg[1] for el_arg in els_args1])) # flatten
links1 = list(itertools.chain.from_iterable([el_arg[2] for el_arg in els_args1])) # flatten
els_args2 = [process_element(instr_name, enc_name, explanations, types, child.children[0]) for child in children2]
elements2 = list(itertools.chain.from_iterable([el_arg[0] for el_arg in els_args2])) # flatten
args2 = list(itertools.chain.from_iterable([el_arg[1] for el_arg in els_args2])) # flatten
links2 = list(itertools.chain.from_iterable([el_arg[2] for el_arg in els_args2])) # flatten
if args1[0].split('@') == args2:
args1 = args2
elif args1 == args2[0].split('@'):
args2 = args1
assert args1 == args2
# what to do about links? they're almost certainly different
values = [
('({})'.format(', '.join(type_bitconcat(types, arg) for arg in args1)), ' ^ '.join(elements1)),
('({})'.format(', '.join(type_bitconcat(types, arg) for arg in args2)), ' ^ '.join(elements2)),
]
props = {
'encoded_in': '({})'.format(', '.join(args1)),
'arg_type': '({})'.format(', '.join('bits({})'.format(get_bitconcat_n_bits(types, arg)) for arg in args1)),
}
name = '{}_alternative_{}'.format(enc_name, sanitize('_'.join(args1)))
assert name not in explanations
explanations[name] = Explanation('TABLE', props, values)
return (name, args1, links1)
def generate_optional_alternative_explanation(instr_name, enc_name, explanations, types, children1, children2):
els_args1 = [process_element(instr_name, enc_name, explanations, types, child.children[0]) for child in children1]
elements1 = list(itertools.chain.from_iterable([el_arg[0] for el_arg in els_args1])) # flatten
args1 = list(itertools.chain.from_iterable([el_arg[1] for el_arg in els_args1])) # flatten
links1 = list(itertools.chain.from_iterable([el_arg[2] for el_arg in els_args1])) # flatten
els_args2 = [process_element(instr_name, enc_name, explanations, types, child.children[0]) for child in children2]
elements2 = list(itertools.chain.from_iterable([el_arg[0] for el_arg in els_args2])) # flatten
args2 = list(itertools.chain.from_iterable([el_arg[1] for el_arg in els_args2])) # flatten
links2 = list(itertools.chain.from_iterable([el_arg[2] for el_arg in els_args2])) # flatten
alt_name, alt_args, alt_links = generate_alternative_explanation(instr_name, enc_name, explanations, types, children1, children2)
assert args1 == args2 == alt_args
# what to do about links? they're almost certainly different
# see if either side has a default
try:
default_args = [default_clause(explanations, types, arg, link) for arg, link in zip(args1, links1)]
values = [
('({})'.format(', '.join(default_args)), '"" if false /* hack */'),
('({})'.format(', '.join(type_bitconcat(types, arg) for arg in args1)), '{}({})'.format(alt_name, ', '.join(args1))),
('({})'.format(', '.join(default_args)), '""'),
]
props = {
'encoded_in': '({})'.format(', '.join(args1)),
'arg_type': '({})'.format(', '.join('bits({})'.format(get_bitconcat_n_bits(types, arg)) for arg in args1)),
}
except NoDefaultException:
default_args = [default_clause(explanations, types, arg, link) for arg, link in zip(args2, links2)]
values = [
('({})'.format(', '.join(default_args)), '"" if false /* hack */'),
('({})'.format(', '.join(type_bitconcat(types, arg) for arg in args2)), '{}({})'.format(alt_name, ', '.join(args2))),
('({})'.format(', '.join(default_args)), '""'),
]
props = {
'encoded_in': '({})'.format(', '.join(args2)),
'arg_type': '({})'.format(', '.join('bits({})'.format(get_bitconcat_n_bits(types, arg)) for arg in args2)),
}
name = '{}_optional_{}'.format(enc_name, sanitize('_'.join(args1)))
assert name not in explanations
explanations[name] = Explanation('TABLE', props, values)
return (name, args1, links1)
def bits_type_to_n(t):
assert t.startswith('bits(')
assert t.endswith(')')
return int(t[5:-1])
# returns (list of sail string, list of arguments for optional, list of links for optional)
def process_element(instr_name, enc_name, explanations, types, el):
if type(el.element) is Token and str(el.element) in '{}()':
return ([], [])
elif el.element.name == 'text':
els = ['"{}"'.format(el.string)]
return (els, [], [])
elif el.element.name == 'doublespace':
return (['spc()'], [], [])
elif el.element.name == 'space':
return (['def_spc()'], [], [])
elif el.element.name == 'link':
link = el.string + '_' + enc_name
exp = explanations[link]
if (exp.type == 'asm_immediate' or exp.type == 'asm_signed_immediate'):
n_bits = get_bitconcat_n_bits(types, exp.props['encoded_in'])
return (['hex_bits_{}({})'.format(n_bits, exp.props['encoded_in'])], [exp.props['encoded_in']], [link])
# TODO FIXME SIGNED
# elif exp.type == 'asm_signed_immediate':
# n_bits = get_bitconcat_n_bits(types, exp.props['encoded_in'])
# return (['hex_bits_{}({})'.format(n_bits, exp.props['enc
elif exp.type == 'asm_extendedreg_hack_oneSP_64':
return (['asm_extendedreg_hack_oneSP_64(Rn, option, Rm, imm3)'], ['Rn', 'option', 'Rm', 'imm3'], [link])
elif exp.type == 'asm_extendedreg_hack_twoSP_64':
return (['asm_extendedreg_hack_twoSP_64(Rd, Rn, option, Rm, imm3)'], ['Rd', 'Rn', 'option', 'Rm', 'imm3'], [link])
elif exp.type == 'asm_extendedreg_hack_oneSP_32':
return (['asm_extendedreg_hack_oneSP_32(Rn, option, Rm, imm3)'], ['Rn', 'option', 'Rm', 'imm3'], [link])
elif exp.type == 'asm_extendedreg_hack_twoSP_32':
return (['asm_extendedreg_hack_twoSP_32(Rd, Rn, option, Rm, imm3)'], ['Rd', 'Rn', 'option', 'Rm', 'imm3'], [link])
elif exp.type == 'lsl_shift_hack_32':
return (['lsl_shift_hack_32(immr, imms)'], ['immr', 'imms'], [link])
elif exp.type == 'lsl_shift_hack_64':
return (['lsl_shift_hack_64(immr, imms)'], ['immr', 'imms'], [link])
# elif exp.type == 'lsb_width_hack_32':
# return (['lsb_width_hack_32(immr, imms)'], ['immr', 'imms'], [link])
elif exp.type == 'lsb_width_hack':
return (['lsb_width_hack(immr, imms)'], ['immr', 'imms'], [link])
elif exp.type == 'lsb_mod_hack_32':
return (['lsb_mod_hack_32(immr, imms)'], ['immr', 'imms'], [link])
elif exp.type == 'lsb_mod_hack_64':
return (['lsb_mod_hack_64(immr, imms)'], ['immr', 'imms'], [link])
elif exp.type == 'matching_Wn':
return (['matching_Wn(Rn, Rm)'], ['Rn', 'Rm'], [link])
elif exp.type == 'matching_Xn':
return (['matching_Xn(Rn, Rm)'], ['Rn', 'Rm'], [link])
elif exp.type == 'movewide_imm_hack_32':
return (['movewide_imm_hack_32(imm16, hw)'], ['imm16', 'hw'], [link])
elif exp.type == 'movewide_imm_hack_64':
return (['movewide_imm_hack_64(imm16, hw)'], ['imm16', 'hw'], [link])
elif exp.type == 'movewide_inverted_imm_hack_32':
return (['movewide_inverted_imm_hack_32(imm16, hw)'], ['imm16', 'hw'], [link])
elif exp.type == 'movewide_inverted_imm_hack_64':
return (['movewide_inverted_imm_hack_64(imm16, hw)'], ['imm16', 'hw'], [link])
elif 'expr' in exp.props and exp.type == 'asm_constant' and exp.props['expr'] == 'PRESENCE':
name, args, links = generate_presence_explanation(instr_name, enc_name, explanations, types, el)
return (['{}({})'.format(name, ', '.join(args))], args, links)
elif exp.type == 'TABLE':
return (['{}({})'.format(sanitize(link), exp.props['encoded_in'])], [exp.props['encoded_in']], [link])
else:
return (['{}({})'.format(exp.type, exp.props['encoded_in'])], [exp.props['encoded_in']], [link])
elif el.element.name == 'bracket_alternative':
name, args, links = generate_alternative_explanation(instr_name, enc_name, explanations, types, el.children[1].children, el.children[3].children)
return (['{}({})'.format(name, ', '.join(args))], args, links)
elif el.element.name == 'optional_alternative':
name, args, links = generate_optional_alternative_explanation(instr_name, enc_name, explanations, types, el.children[1].children, el.children[3].children)
return (['{}({})'.format(name, ', '.join(args))], args, links)
elif el.element.name == 'optional':
name, args, links = generate_optional_explanation(instr_name, enc_name, explanations, types, el.children)
return (['{}({})'.format(name, ', '.join(args))], args, links)
else:
assert False, 'unknown element name in grammar for asm: ' + el.element.name
def linearize_parse(instr_name, enc_name, explanations, types, parse):
start = parse.tree.children[0] if parse.tree.children else parse.tree # pyleri bug workaround?
elements = [process_element(instr_name, enc_name, explanations, types, el.children[0])[0] for el in start.children]
return itertools.chain.from_iterable(elements) # flatten
asm_rewrites = [
(r'^([A-Z]+)(\s+)<(.+?)>\|#<(.+?)>$', r'\1\2(<\3>|#<\4>)'), # unbracketed alternatives (DSB etc)
(r'<Xd\|SP>, <Xn\|SP>, <R><m>{, <extend> {#<amount>}}$', r'<extendedreg_hack>'),
(r'<Xn\|SP>, <R><m>{, <extend> {#<amount>}}$', r'<extendedreg_hack>'),
(r'<Wd\|WSP>, <Wn\|WSP>, <Wm>{, <extend> {#<amount>}}$', r'<extendedreg_hack>'),
(r'<Wn\|WSP>, <Wm>{, <extend> {#<amount>}}$', r'<extendedreg_hack>'),
(r'<label>', '<label_hack>'),
(r'(<systemreg>|S<op0>_<op1>_<Cn>_<Cm>_<op2>)', '<systemreg>'),
(r'<Ws>, <W\(s\+1\)>', '<casp_hack_ws>'),
(r'<Wt>, <W\(t\+1\)>', '<casp_hack_wt>'),
(r'<Xs>, <X\(s\+1\)>', '<casp_hack_xs>'),
(r'<Xt>, <X\(t\+1\)>', '<casp_hack_xt>'),
(r'^LSL <Wd>, <Wn>, #<shift>$', 'LSL <Wd>, <Wn>, #<lsl_shift_hack>'),
(r'^LSL <Xd>, <Xn>, #<shift>$', 'LSL <Xd>, <Xn>, #<lsl_shift_hack>'),
(r'^CNEG <Wd>, <Wn>, <cond>$', 'CNEG <Wd>, <matching_Wn>, <cond>'),
(r'^CNEG <Xd>, <Xn>, <cond>$', 'CNEG <Xd>, <matching_Xn>, <cond>'),
(r'^CINC <Wd>, <Wn>, <cond>$', 'CINC <Wd>, <matching_Wn>, <cond>'),
(r'^CINC <Xd>, <Xn>, <cond>$', 'CINC <Xd>, <matching_Xn>, <cond>'),
(r'^CINV <Wd>, <Wn>, <cond>$', 'CINV <Wd>, <matching_Wn>, <cond>'),
(r'^CINV <Xd>, <Xn>, <cond>$', 'CINV <Xd>, <matching_Xn>, <cond>'),
(r'^ROR <Wd>, <Ws>, #<shift>$', 'ROR <Wd>, <matching_Wn>, #<shift>'),
(r'^ROR <Xd>, <Xs>, #<shift>$', 'ROR <Xd>, <matching_Xn>, #<shift>'),
(r'^BFXIL <Wd>, <Wn>, #<lsb>, #<width>$', 'BFXIL <Wd>, <Wn>, <lsb_width_hack>'),
(r'^BFXIL <Xd>, <Xn>, #<lsb>, #<width>$', 'BFXIL <Xd>, <Xn>, <lsb_width_hack>'),
(r'^UBFX <Wd>, <Wn>, #<lsb>, #<width>$', 'UBFX <Wd>, <Wn>, <lsb_width_hack>'),
(r'^UBFX <Xd>, <Xn>, #<lsb>, #<width>$', 'UBFX <Xd>, <Xn>, <lsb_width_hack>'),
(r'^UBFIZ <Wd>, <Wn>, #<lsb>, #<width>$', 'UBFIZ <Wd>, <Wn>, <lsb_width_hack>'),
(r'^UBFIZ <Xd>, <Xn>, #<lsb>, #<width>$', 'UBFIZ <Xd>, <Xn>, <lsb_width_hack>'),
(r'^SBFX <Wd>, <Wn>, #<lsb>, #<width>$', 'SBFX <Wd>, <Wn>, <lsb_width_hack>'),
(r'^SBFX <Xd>, <Xn>, #<lsb>, #<width>$', 'SBFX <Xd>, <Xn>, <lsb_width_hack>'),
(r'^SBFIZ <Wd>, <Wn>, #<lsb>, #<width>$', 'SBFIZ <Wd>, <Wn>, <lsb_width_hack>'),
(r'^SBFIZ <Xd>, <Xn>, #<lsb>, #<width>$', 'SBFIZ <Xd>, <Xn>, <lsb_width_hack>'),
(r'^BFC <Wd>, #<lsb>, #<width>$', 'BFC <Wd>, <lsb_width_hack>'),
(r'^BFC <Xd>, #<lsb>, #<width>$', 'BFC <Xd>, <lsb_width_hack>'),
(r'^BFI <Wd>, <Wn>, #<lsb>, #<width>$', 'BFI <Wd>, <Wn>, <lsb_width_hack>'),
(r'^BFI <Xd>, <Xn>, #<lsb>, #<width>$', 'BFI <Xd>, <Xn>, <lsb_width_hack>'),
]
asm_rewrites = [(re.compile(regex), rep) for regex, rep in asm_rewrites]
def read_asm_encoding(name, explanations, types, xml):
elements = []
enc_name = xml.get('name')
orig_template = template = ''.join(xml.find('asmtemplate').itertext())
for regex, rep in asm_rewrites:
template = regex.sub(rep, template)
# hack for packed (imm+shift) immediates in MOV aliases
if enc_name in ('MOV_MOVZ_32_movewide', 'MOV_MOVZ_64_movewide',
'MOV_MOVN_32_movewide', 'MOV_MOVN_64_movewide'):
template = re.sub(r'^MOV <(Xd|Wd)>, #<imm>$', r'MOV <\1>, #<movewide_imm_hack>', template)
parse = asm_grammar.parse(template)
assert parse.is_valid
return (xml.get('bitdiffs'), ' ^ '.join(linearize_parse(name, enc_name, explanations, types, parse)))