]>
Commit | Line | Data |
---|---|---|
3d004a37 | 1 | #!/usr/bin/env python3 |
568ae7ef RH |
2 | # Copyright (c) 2018 Linaro Limited |
3 | # | |
4 | # This library is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU Lesser General Public | |
6 | # License as published by the Free Software Foundation; either | |
7 | # version 2 of the License, or (at your option) any later version. | |
8 | # | |
9 | # This library is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | # Lesser General Public License for more details. | |
13 | # | |
14 | # You should have received a copy of the GNU Lesser General Public | |
15 | # License along with this library; if not, see <http://www.gnu.org/licenses/>. | |
16 | # | |
17 | ||
18 | # | |
19 | # Generate a decoding tree from a specification file. | |
3fdbf5d6 | 20 | # See the syntax and semantics in docs/devel/decodetree.rst. |
568ae7ef RH |
21 | # |
22 | ||
568ae7ef RH |
23 | import os |
24 | import re | |
25 | import sys | |
26 | import getopt | |
568ae7ef RH |
27 | |
28 | insnwidth = 32 | |
29 | insnmask = 0xffffffff | |
17560e93 | 30 | variablewidth = False |
568ae7ef RH |
31 | fields = {} |
32 | arguments = {} | |
33 | formats = {} | |
0eff2df4 | 34 | allpatterns = [] |
c6920795 | 35 | anyextern = False |
568ae7ef RH |
36 | |
37 | translate_prefix = 'trans' | |
38 | translate_scope = 'static ' | |
39 | input_file = '' | |
40 | output_file = None | |
41 | output_fd = None | |
42 | insntype = 'uint32_t' | |
abd04f92 | 43 | decode_function = 'decode' |
568ae7ef | 44 | |
acfdd239 RH |
45 | # An identifier for C. |
46 | re_C_ident = '[a-zA-Z][a-zA-Z0-9_]*' | |
568ae7ef | 47 | |
acfdd239 RH |
48 | # Identifiers for Arguments, Fields, Formats and Patterns. |
49 | re_arg_ident = '&[a-zA-Z0-9_]*' | |
50 | re_fld_ident = '%[a-zA-Z0-9_]*' | |
51 | re_fmt_ident = '@[a-zA-Z0-9_]*' | |
52 | re_pat_ident = '[a-zA-Z0-9_]*' | |
568ae7ef | 53 | |
6699ae6a | 54 | def error_with_file(file, lineno, *args): |
568ae7ef RH |
55 | """Print an error message from file:line and args and exit.""" |
56 | global output_file | |
57 | global output_fd | |
58 | ||
2fd51b19 RH |
59 | prefix = '' |
60 | if file: | |
61 | prefix += '{0}:'.format(file) | |
568ae7ef | 62 | if lineno: |
2fd51b19 RH |
63 | prefix += '{0}:'.format(lineno) |
64 | if prefix: | |
65 | prefix += ' ' | |
66 | print(prefix, end='error: ', file=sys.stderr) | |
67 | print(*args, file=sys.stderr) | |
68 | ||
568ae7ef RH |
69 | if output_file and output_fd: |
70 | output_fd.close() | |
71 | os.remove(output_file) | |
72 | exit(1) | |
2fd51b19 RH |
73 | # end error_with_file |
74 | ||
568ae7ef | 75 | |
6699ae6a | 76 | def error(lineno, *args): |
2fd51b19 RH |
77 | error_with_file(input_file, lineno, *args) |
78 | # end error | |
79 | ||
568ae7ef RH |
80 | |
81 | def output(*args): | |
82 | global output_fd | |
83 | for a in args: | |
84 | output_fd.write(a) | |
85 | ||
86 | ||
568ae7ef RH |
87 | def output_autogen(): |
88 | output('/* This file is autogenerated by scripts/decodetree.py. */\n\n') | |
89 | ||
90 | ||
91 | def str_indent(c): | |
92 | """Return a string with C spaces""" | |
93 | return ' ' * c | |
94 | ||
95 | ||
96 | def str_fields(fields): | |
65fdb3cc | 97 | """Return a string uniquely identifying FIELDS""" |
568ae7ef RH |
98 | r = '' |
99 | for n in sorted(fields.keys()): | |
100 | r += '_' + n | |
101 | return r[1:] | |
102 | ||
103 | ||
104 | def str_match_bits(bits, mask): | |
105 | """Return a string pretty-printing BITS/MASK""" | |
106 | global insnwidth | |
107 | ||
108 | i = 1 << (insnwidth - 1) | |
109 | space = 0x01010100 | |
110 | r = '' | |
111 | while i != 0: | |
112 | if i & mask: | |
113 | if i & bits: | |
114 | r += '1' | |
115 | else: | |
116 | r += '0' | |
117 | else: | |
118 | r += '.' | |
119 | if i & space: | |
120 | r += ' ' | |
121 | i >>= 1 | |
122 | return r | |
123 | ||
124 | ||
125 | def is_pow2(x): | |
126 | """Return true iff X is equal to a power of 2.""" | |
127 | return (x & (x - 1)) == 0 | |
128 | ||
129 | ||
130 | def ctz(x): | |
131 | """Return the number of times 2 factors into X.""" | |
b44b3449 | 132 | assert x != 0 |
568ae7ef RH |
133 | r = 0 |
134 | while ((x >> r) & 1) == 0: | |
135 | r += 1 | |
136 | return r | |
137 | ||
138 | ||
139 | def is_contiguous(bits): | |
b44b3449 RH |
140 | if bits == 0: |
141 | return -1 | |
568ae7ef RH |
142 | shift = ctz(bits) |
143 | if is_pow2((bits >> shift) + 1): | |
144 | return shift | |
145 | else: | |
146 | return -1 | |
147 | ||
148 | ||
149 | def eq_fields_for_args(flds_a, flds_b): | |
150 | if len(flds_a) != len(flds_b): | |
151 | return False | |
152 | for k, a in flds_a.items(): | |
153 | if k not in flds_b: | |
154 | return False | |
155 | return True | |
156 | ||
157 | ||
158 | def eq_fields_for_fmts(flds_a, flds_b): | |
159 | if len(flds_a) != len(flds_b): | |
160 | return False | |
161 | for k, a in flds_a.items(): | |
162 | if k not in flds_b: | |
163 | return False | |
164 | b = flds_b[k] | |
165 | if a.__class__ != b.__class__ or a != b: | |
166 | return False | |
167 | return True | |
168 | ||
169 | ||
170 | class Field: | |
171 | """Class representing a simple instruction field""" | |
172 | def __init__(self, sign, pos, len): | |
173 | self.sign = sign | |
174 | self.pos = pos | |
175 | self.len = len | |
176 | self.mask = ((1 << len) - 1) << pos | |
177 | ||
178 | def __str__(self): | |
179 | if self.sign: | |
180 | s = 's' | |
181 | else: | |
182 | s = '' | |
cbcdf1a9 | 183 | return str(self.pos) + ':' + s + str(self.len) |
568ae7ef RH |
184 | |
185 | def str_extract(self): | |
186 | if self.sign: | |
187 | extr = 'sextract32' | |
188 | else: | |
189 | extr = 'extract32' | |
190 | return '{0}(insn, {1}, {2})'.format(extr, self.pos, self.len) | |
191 | ||
192 | def __eq__(self, other): | |
2c7d4427 | 193 | return self.sign == other.sign and self.mask == other.mask |
568ae7ef RH |
194 | |
195 | def __ne__(self, other): | |
196 | return not self.__eq__(other) | |
197 | # end Field | |
198 | ||
199 | ||
200 | class MultiField: | |
201 | """Class representing a compound instruction field""" | |
202 | def __init__(self, subs, mask): | |
203 | self.subs = subs | |
204 | self.sign = subs[0].sign | |
205 | self.mask = mask | |
206 | ||
207 | def __str__(self): | |
208 | return str(self.subs) | |
209 | ||
210 | def str_extract(self): | |
211 | ret = '0' | |
212 | pos = 0 | |
213 | for f in reversed(self.subs): | |
214 | if pos == 0: | |
215 | ret = f.str_extract() | |
216 | else: | |
217 | ret = 'deposit32({0}, {1}, {2}, {3})' \ | |
218 | .format(ret, pos, 32 - pos, f.str_extract()) | |
219 | pos += f.len | |
220 | return ret | |
221 | ||
222 | def __ne__(self, other): | |
223 | if len(self.subs) != len(other.subs): | |
224 | return True | |
225 | for a, b in zip(self.subs, other.subs): | |
226 | if a.__class__ != b.__class__ or a != b: | |
227 | return True | |
228 | return False | |
229 | ||
230 | def __eq__(self, other): | |
231 | return not self.__ne__(other) | |
232 | # end MultiField | |
233 | ||
234 | ||
235 | class ConstField: | |
236 | """Class representing an argument field with constant value""" | |
237 | def __init__(self, value): | |
238 | self.value = value | |
239 | self.mask = 0 | |
240 | self.sign = value < 0 | |
241 | ||
242 | def __str__(self): | |
243 | return str(self.value) | |
244 | ||
245 | def str_extract(self): | |
246 | return str(self.value) | |
247 | ||
248 | def __cmp__(self, other): | |
249 | return self.value - other.value | |
250 | # end ConstField | |
251 | ||
252 | ||
253 | class FunctionField: | |
94597b61 | 254 | """Class representing a field passed through a function""" |
568ae7ef RH |
255 | def __init__(self, func, base): |
256 | self.mask = base.mask | |
257 | self.sign = base.sign | |
258 | self.base = base | |
259 | self.func = func | |
260 | ||
261 | def __str__(self): | |
262 | return self.func + '(' + str(self.base) + ')' | |
263 | ||
264 | def str_extract(self): | |
451e4ffd | 265 | return self.func + '(ctx, ' + self.base.str_extract() + ')' |
568ae7ef RH |
266 | |
267 | def __eq__(self, other): | |
268 | return self.func == other.func and self.base == other.base | |
269 | ||
270 | def __ne__(self, other): | |
271 | return not self.__eq__(other) | |
272 | # end FunctionField | |
273 | ||
274 | ||
94597b61 RH |
275 | class ParameterField: |
276 | """Class representing a pseudo-field read from a function""" | |
277 | def __init__(self, func): | |
278 | self.mask = 0 | |
279 | self.sign = 0 | |
280 | self.func = func | |
281 | ||
282 | def __str__(self): | |
283 | return self.func | |
284 | ||
285 | def str_extract(self): | |
286 | return self.func + '(ctx)' | |
287 | ||
288 | def __eq__(self, other): | |
289 | return self.func == other.func | |
290 | ||
291 | def __ne__(self, other): | |
292 | return not self.__eq__(other) | |
293 | # end ParameterField | |
294 | ||
295 | ||
568ae7ef RH |
296 | class Arguments: |
297 | """Class representing the extracted fields of a format""" | |
abd04f92 | 298 | def __init__(self, nm, flds, extern): |
568ae7ef | 299 | self.name = nm |
abd04f92 | 300 | self.extern = extern |
568ae7ef RH |
301 | self.fields = sorted(flds) |
302 | ||
303 | def __str__(self): | |
304 | return self.name + ' ' + str(self.fields) | |
305 | ||
306 | def struct_name(self): | |
307 | return 'arg_' + self.name | |
308 | ||
309 | def output_def(self): | |
abd04f92 RH |
310 | if not self.extern: |
311 | output('typedef struct {\n') | |
312 | for n in self.fields: | |
313 | output(' int ', n, ';\n') | |
314 | output('} ', self.struct_name(), ';\n\n') | |
568ae7ef RH |
315 | # end Arguments |
316 | ||
317 | ||
318 | class General: | |
319 | """Common code between instruction formats and instruction patterns""" | |
17560e93 | 320 | def __init__(self, name, lineno, base, fixb, fixm, udfm, fldm, flds, w): |
568ae7ef | 321 | self.name = name |
6699ae6a | 322 | self.file = input_file |
568ae7ef RH |
323 | self.lineno = lineno |
324 | self.base = base | |
325 | self.fixedbits = fixb | |
326 | self.fixedmask = fixm | |
327 | self.undefmask = udfm | |
328 | self.fieldmask = fldm | |
329 | self.fields = flds | |
17560e93 | 330 | self.width = w |
568ae7ef RH |
331 | |
332 | def __str__(self): | |
0eff2df4 | 333 | return self.name + ' ' + str_match_bits(self.fixedbits, self.fixedmask) |
568ae7ef RH |
334 | |
335 | def str1(self, i): | |
336 | return str_indent(i) + self.__str__() | |
337 | # end General | |
338 | ||
339 | ||
340 | class Format(General): | |
341 | """Class representing an instruction format""" | |
342 | ||
343 | def extract_name(self): | |
71ecf79b RH |
344 | global decode_function |
345 | return decode_function + '_extract_' + self.name | |
568ae7ef RH |
346 | |
347 | def output_extract(self): | |
451e4ffd | 348 | output('static void ', self.extract_name(), '(DisasContext *ctx, ', |
568ae7ef RH |
349 | self.base.struct_name(), ' *a, ', insntype, ' insn)\n{\n') |
350 | for n, f in self.fields.items(): | |
351 | output(' a->', n, ' = ', f.str_extract(), ';\n') | |
352 | output('}\n\n') | |
353 | # end Format | |
354 | ||
355 | ||
356 | class Pattern(General): | |
357 | """Class representing an instruction pattern""" | |
358 | ||
359 | def output_decl(self): | |
360 | global translate_scope | |
361 | global translate_prefix | |
362 | output('typedef ', self.base.base.struct_name(), | |
363 | ' arg_', self.name, ';\n') | |
76805598 | 364 | output(translate_scope, 'bool ', translate_prefix, '_', self.name, |
3a7be554 | 365 | '(DisasContext *ctx, arg_', self.name, ' *a);\n') |
568ae7ef RH |
366 | |
367 | def output_code(self, i, extracted, outerbits, outermask): | |
368 | global translate_prefix | |
369 | ind = str_indent(i) | |
370 | arg = self.base.base.name | |
6699ae6a | 371 | output(ind, '/* ', self.file, ':', str(self.lineno), ' */\n') |
568ae7ef | 372 | if not extracted: |
451e4ffd RH |
373 | output(ind, self.base.extract_name(), |
374 | '(ctx, &u.f_', arg, ', insn);\n') | |
568ae7ef RH |
375 | for n, f in self.fields.items(): |
376 | output(ind, 'u.f_', arg, '.', n, ' = ', f.str_extract(), ';\n') | |
eb6b87fa RH |
377 | output(ind, 'if (', translate_prefix, '_', self.name, |
378 | '(ctx, &u.f_', arg, ')) return true;\n') | |
08561fc1 RH |
379 | |
380 | # Normal patterns do not have children. | |
381 | def build_tree(self): | |
382 | return | |
383 | def prop_masks(self): | |
384 | return | |
385 | def prop_format(self): | |
386 | return | |
387 | def prop_width(self): | |
388 | return | |
389 | ||
568ae7ef RH |
390 | # end Pattern |
391 | ||
392 | ||
df63044d RH |
393 | class MultiPattern(General): |
394 | """Class representing a set of instruction patterns""" | |
0eff2df4 | 395 | |
08561fc1 | 396 | def __init__(self, lineno): |
0eff2df4 RH |
397 | self.file = input_file |
398 | self.lineno = lineno | |
08561fc1 | 399 | self.pats = [] |
0eff2df4 | 400 | self.base = None |
df63044d RH |
401 | self.fixedbits = 0 |
402 | self.fixedmask = 0 | |
403 | self.undefmask = 0 | |
404 | self.width = None | |
0eff2df4 RH |
405 | |
406 | def __str__(self): | |
df63044d RH |
407 | r = 'group' |
408 | if self.fixedbits is not None: | |
409 | r += ' ' + str_match_bits(self.fixedbits, self.fixedmask) | |
410 | return r | |
0eff2df4 RH |
411 | |
412 | def output_decl(self): | |
413 | for p in self.pats: | |
414 | p.output_decl() | |
08561fc1 RH |
415 | |
416 | def prop_masks(self): | |
417 | global insnmask | |
418 | ||
419 | fixedmask = insnmask | |
420 | undefmask = insnmask | |
421 | ||
422 | # Collect fixedmask/undefmask for all of the children. | |
423 | for p in self.pats: | |
424 | p.prop_masks() | |
425 | fixedmask &= p.fixedmask | |
426 | undefmask &= p.undefmask | |
427 | ||
428 | # Widen fixedmask until all fixedbits match | |
429 | repeat = True | |
430 | fixedbits = 0 | |
431 | while repeat and fixedmask != 0: | |
432 | fixedbits = None | |
433 | for p in self.pats: | |
434 | thisbits = p.fixedbits & fixedmask | |
435 | if fixedbits is None: | |
436 | fixedbits = thisbits | |
437 | elif fixedbits != thisbits: | |
438 | fixedmask &= ~(fixedbits ^ thisbits) | |
439 | break | |
440 | else: | |
441 | repeat = False | |
442 | ||
443 | self.fixedbits = fixedbits | |
444 | self.fixedmask = fixedmask | |
445 | self.undefmask = undefmask | |
446 | ||
447 | def build_tree(self): | |
448 | for p in self.pats: | |
449 | p.build_tree() | |
450 | ||
451 | def prop_format(self): | |
452 | for p in self.pats: | |
453 | p.build_tree() | |
454 | ||
455 | def prop_width(self): | |
456 | width = None | |
457 | for p in self.pats: | |
458 | p.prop_width() | |
459 | if width is None: | |
460 | width = p.width | |
461 | elif width != p.width: | |
462 | error_with_file(self.file, self.lineno, | |
463 | 'width mismatch in patterns within braces') | |
464 | self.width = width | |
465 | ||
df63044d RH |
466 | # end MultiPattern |
467 | ||
468 | ||
469 | class IncMultiPattern(MultiPattern): | |
470 | """Class representing an overlapping set of instruction patterns""" | |
471 | ||
0eff2df4 RH |
472 | def output_code(self, i, extracted, outerbits, outermask): |
473 | global translate_prefix | |
474 | ind = str_indent(i) | |
475 | for p in self.pats: | |
476 | if outermask != p.fixedmask: | |
477 | innermask = p.fixedmask & ~outermask | |
478 | innerbits = p.fixedbits & ~outermask | |
479 | output(ind, 'if ((insn & ', | |
480 | '0x{0:08x}) == 0x{1:08x}'.format(innermask, innerbits), | |
481 | ') {\n') | |
482 | output(ind, ' /* ', | |
483 | str_match_bits(p.fixedbits, p.fixedmask), ' */\n') | |
484 | p.output_code(i + 4, extracted, p.fixedbits, p.fixedmask) | |
485 | output(ind, '}\n') | |
486 | else: | |
487 | p.output_code(i, extracted, p.fixedbits, p.fixedmask) | |
040145c4 | 488 | #end IncMultiPattern |
0eff2df4 RH |
489 | |
490 | ||
08561fc1 RH |
491 | class Tree: |
492 | """Class representing a node in a decode tree""" | |
493 | ||
494 | def __init__(self, fm, tm): | |
495 | self.fixedmask = fm | |
496 | self.thismask = tm | |
497 | self.subs = [] | |
498 | self.base = None | |
499 | ||
500 | def str1(self, i): | |
501 | ind = str_indent(i) | |
502 | r = '{0}{1:08x}'.format(ind, self.fixedmask) | |
503 | if self.format: | |
504 | r += ' ' + self.format.name | |
505 | r += ' [\n' | |
506 | for (b, s) in self.subs: | |
507 | r += '{0} {1:08x}:\n'.format(ind, b) | |
508 | r += s.str1(i + 4) + '\n' | |
509 | r += ind + ']' | |
510 | return r | |
511 | ||
512 | def __str__(self): | |
513 | return self.str1(0) | |
514 | ||
515 | def output_code(self, i, extracted, outerbits, outermask): | |
516 | ind = str_indent(i) | |
517 | ||
518 | # If we identified all nodes below have the same format, | |
519 | # extract the fields now. | |
520 | if not extracted and self.base: | |
521 | output(ind, self.base.extract_name(), | |
522 | '(ctx, &u.f_', self.base.base.name, ', insn);\n') | |
523 | extracted = True | |
524 | ||
525 | # Attempt to aid the compiler in producing compact switch statements. | |
526 | # If the bits in the mask are contiguous, extract them. | |
527 | sh = is_contiguous(self.thismask) | |
528 | if sh > 0: | |
529 | # Propagate SH down into the local functions. | |
530 | def str_switch(b, sh=sh): | |
531 | return '(insn >> {0}) & 0x{1:x}'.format(sh, b >> sh) | |
532 | ||
533 | def str_case(b, sh=sh): | |
534 | return '0x{0:x}'.format(b >> sh) | |
535 | else: | |
536 | def str_switch(b): | |
537 | return 'insn & 0x{0:08x}'.format(b) | |
538 | ||
539 | def str_case(b): | |
540 | return '0x{0:08x}'.format(b) | |
541 | ||
542 | output(ind, 'switch (', str_switch(self.thismask), ') {\n') | |
543 | for b, s in sorted(self.subs): | |
544 | assert (self.thismask & ~s.fixedmask) == 0 | |
545 | innermask = outermask | self.thismask | |
546 | innerbits = outerbits | b | |
547 | output(ind, 'case ', str_case(b), ':\n') | |
548 | output(ind, ' /* ', | |
549 | str_match_bits(innerbits, innermask), ' */\n') | |
550 | s.output_code(i + 4, extracted, innerbits, innermask) | |
514101c0 | 551 | output(ind, ' break;\n') |
08561fc1 RH |
552 | output(ind, '}\n') |
553 | # end Tree | |
554 | ||
555 | ||
556 | class ExcMultiPattern(MultiPattern): | |
557 | """Class representing a non-overlapping set of instruction patterns""" | |
558 | ||
559 | def output_code(self, i, extracted, outerbits, outermask): | |
560 | # Defer everything to our decomposed Tree node | |
561 | self.tree.output_code(i, extracted, outerbits, outermask) | |
562 | ||
563 | @staticmethod | |
564 | def __build_tree(pats, outerbits, outermask): | |
565 | # Find the intersection of all remaining fixedmask. | |
566 | innermask = ~outermask & insnmask | |
567 | for i in pats: | |
568 | innermask &= i.fixedmask | |
569 | ||
570 | if innermask == 0: | |
571 | # Edge condition: One pattern covers the entire insnmask | |
572 | if len(pats) == 1: | |
573 | t = Tree(outermask, innermask) | |
574 | t.subs.append((0, pats[0])) | |
575 | return t | |
576 | ||
577 | text = 'overlapping patterns:' | |
578 | for p in pats: | |
579 | text += '\n' + p.file + ':' + str(p.lineno) + ': ' + str(p) | |
580 | error_with_file(pats[0].file, pats[0].lineno, text) | |
581 | ||
582 | fullmask = outermask | innermask | |
583 | ||
584 | # Sort each element of pats into the bin selected by the mask. | |
585 | bins = {} | |
586 | for i in pats: | |
587 | fb = i.fixedbits & innermask | |
588 | if fb in bins: | |
589 | bins[fb].append(i) | |
590 | else: | |
591 | bins[fb] = [i] | |
592 | ||
593 | # We must recurse if any bin has more than one element or if | |
594 | # the single element in the bin has not been fully matched. | |
595 | t = Tree(fullmask, innermask) | |
596 | ||
597 | for b, l in bins.items(): | |
598 | s = l[0] | |
599 | if len(l) > 1 or s.fixedmask & ~fullmask != 0: | |
600 | s = ExcMultiPattern.__build_tree(l, b | outerbits, fullmask) | |
601 | t.subs.append((b, s)) | |
602 | ||
603 | return t | |
604 | ||
605 | def build_tree(self): | |
606 | super().prop_format() | |
607 | self.tree = self.__build_tree(self.pats, self.fixedbits, | |
608 | self.fixedmask) | |
609 | ||
610 | @staticmethod | |
611 | def __prop_format(tree): | |
612 | """Propagate Format objects into the decode tree""" | |
613 | ||
614 | # Depth first search. | |
615 | for (b, s) in tree.subs: | |
616 | if isinstance(s, Tree): | |
617 | ExcMultiPattern.__prop_format(s) | |
618 | ||
619 | # If all entries in SUBS have the same format, then | |
620 | # propagate that into the tree. | |
621 | f = None | |
622 | for (b, s) in tree.subs: | |
623 | if f is None: | |
624 | f = s.base | |
625 | if f is None: | |
626 | return | |
627 | if f is not s.base: | |
628 | return | |
629 | tree.base = f | |
630 | ||
631 | def prop_format(self): | |
632 | super().prop_format() | |
633 | self.__prop_format(self.tree) | |
634 | ||
635 | # end ExcMultiPattern | |
636 | ||
637 | ||
568ae7ef RH |
638 | def parse_field(lineno, name, toks): |
639 | """Parse one instruction field from TOKS at LINENO""" | |
640 | global fields | |
568ae7ef RH |
641 | global insnwidth |
642 | ||
643 | # A "simple" field will have only one entry; | |
644 | # a "multifield" will have several. | |
645 | subs = [] | |
646 | width = 0 | |
647 | func = None | |
648 | for t in toks: | |
acfdd239 | 649 | if re.match('^!function=', t): |
568ae7ef RH |
650 | if func: |
651 | error(lineno, 'duplicate function') | |
652 | func = t.split('=') | |
653 | func = func[1] | |
654 | continue | |
655 | ||
2d110c11 | 656 | if re.fullmatch('[0-9]+:s[0-9]+', t): |
568ae7ef RH |
657 | # Signed field extract |
658 | subtoks = t.split(':s') | |
659 | sign = True | |
2d110c11 | 660 | elif re.fullmatch('[0-9]+:[0-9]+', t): |
568ae7ef RH |
661 | # Unsigned field extract |
662 | subtoks = t.split(':') | |
663 | sign = False | |
664 | else: | |
665 | error(lineno, 'invalid field token "{0}"'.format(t)) | |
666 | po = int(subtoks[0]) | |
667 | le = int(subtoks[1]) | |
668 | if po + le > insnwidth: | |
669 | error(lineno, 'field {0} too large'.format(t)) | |
670 | f = Field(sign, po, le) | |
671 | subs.append(f) | |
672 | width += le | |
673 | ||
674 | if width > insnwidth: | |
675 | error(lineno, 'field too large') | |
94597b61 RH |
676 | if len(subs) == 0: |
677 | if func: | |
678 | f = ParameterField(func) | |
679 | else: | |
680 | error(lineno, 'field with no value') | |
568ae7ef | 681 | else: |
94597b61 RH |
682 | if len(subs) == 1: |
683 | f = subs[0] | |
684 | else: | |
685 | mask = 0 | |
686 | for s in subs: | |
687 | if mask & s.mask: | |
688 | error(lineno, 'field components overlap') | |
689 | mask |= s.mask | |
690 | f = MultiField(subs, mask) | |
691 | if func: | |
692 | f = FunctionField(func, f) | |
568ae7ef RH |
693 | |
694 | if name in fields: | |
695 | error(lineno, 'duplicate field', name) | |
696 | fields[name] = f | |
697 | # end parse_field | |
698 | ||
699 | ||
700 | def parse_arguments(lineno, name, toks): | |
701 | """Parse one argument set from TOKS at LINENO""" | |
702 | global arguments | |
acfdd239 | 703 | global re_C_ident |
c6920795 | 704 | global anyextern |
568ae7ef RH |
705 | |
706 | flds = [] | |
abd04f92 | 707 | extern = False |
568ae7ef | 708 | for t in toks: |
2d110c11 | 709 | if re.fullmatch('!extern', t): |
abd04f92 | 710 | extern = True |
c6920795 | 711 | anyextern = True |
abd04f92 | 712 | continue |
acfdd239 | 713 | if not re.fullmatch(re_C_ident, t): |
568ae7ef RH |
714 | error(lineno, 'invalid argument set token "{0}"'.format(t)) |
715 | if t in flds: | |
716 | error(lineno, 'duplicate argument "{0}"'.format(t)) | |
717 | flds.append(t) | |
718 | ||
719 | if name in arguments: | |
720 | error(lineno, 'duplicate argument set', name) | |
abd04f92 | 721 | arguments[name] = Arguments(name, flds, extern) |
568ae7ef RH |
722 | # end parse_arguments |
723 | ||
724 | ||
725 | def lookup_field(lineno, name): | |
726 | global fields | |
727 | if name in fields: | |
728 | return fields[name] | |
729 | error(lineno, 'undefined field', name) | |
730 | ||
731 | ||
732 | def add_field(lineno, flds, new_name, f): | |
733 | if new_name in flds: | |
734 | error(lineno, 'duplicate field', new_name) | |
735 | flds[new_name] = f | |
736 | return flds | |
737 | ||
738 | ||
739 | def add_field_byname(lineno, flds, new_name, old_name): | |
740 | return add_field(lineno, flds, new_name, lookup_field(lineno, old_name)) | |
741 | ||
742 | ||
743 | def infer_argument_set(flds): | |
744 | global arguments | |
abd04f92 | 745 | global decode_function |
568ae7ef RH |
746 | |
747 | for arg in arguments.values(): | |
748 | if eq_fields_for_args(flds, arg.fields): | |
749 | return arg | |
750 | ||
abd04f92 RH |
751 | name = decode_function + str(len(arguments)) |
752 | arg = Arguments(name, flds.keys(), False) | |
568ae7ef RH |
753 | arguments[name] = arg |
754 | return arg | |
755 | ||
756 | ||
17560e93 | 757 | def infer_format(arg, fieldmask, flds, width): |
568ae7ef RH |
758 | global arguments |
759 | global formats | |
abd04f92 | 760 | global decode_function |
568ae7ef RH |
761 | |
762 | const_flds = {} | |
763 | var_flds = {} | |
764 | for n, c in flds.items(): | |
765 | if c is ConstField: | |
766 | const_flds[n] = c | |
767 | else: | |
768 | var_flds[n] = c | |
769 | ||
770 | # Look for an existing format with the same argument set and fields | |
771 | for fmt in formats.values(): | |
772 | if arg and fmt.base != arg: | |
773 | continue | |
774 | if fieldmask != fmt.fieldmask: | |
775 | continue | |
17560e93 RH |
776 | if width != fmt.width: |
777 | continue | |
568ae7ef RH |
778 | if not eq_fields_for_fmts(flds, fmt.fields): |
779 | continue | |
780 | return (fmt, const_flds) | |
781 | ||
abd04f92 | 782 | name = decode_function + '_Fmt_' + str(len(formats)) |
568ae7ef RH |
783 | if not arg: |
784 | arg = infer_argument_set(flds) | |
785 | ||
17560e93 | 786 | fmt = Format(name, 0, arg, 0, 0, 0, fieldmask, var_flds, width) |
568ae7ef RH |
787 | formats[name] = fmt |
788 | ||
789 | return (fmt, const_flds) | |
790 | # end infer_format | |
791 | ||
792 | ||
08561fc1 | 793 | def parse_generic(lineno, parent_pat, name, toks): |
568ae7ef RH |
794 | """Parse one instruction format from TOKS at LINENO""" |
795 | global fields | |
796 | global arguments | |
797 | global formats | |
0eff2df4 | 798 | global allpatterns |
acfdd239 RH |
799 | global re_arg_ident |
800 | global re_fld_ident | |
801 | global re_fmt_ident | |
802 | global re_C_ident | |
568ae7ef RH |
803 | global insnwidth |
804 | global insnmask | |
17560e93 | 805 | global variablewidth |
568ae7ef | 806 | |
08561fc1 RH |
807 | is_format = parent_pat is None |
808 | ||
568ae7ef RH |
809 | fixedmask = 0 |
810 | fixedbits = 0 | |
811 | undefmask = 0 | |
812 | width = 0 | |
813 | flds = {} | |
814 | arg = None | |
815 | fmt = None | |
816 | for t in toks: | |
65fdb3cc | 817 | # '&Foo' gives a format an explicit argument set. |
acfdd239 | 818 | if re.fullmatch(re_arg_ident, t): |
568ae7ef RH |
819 | tt = t[1:] |
820 | if arg: | |
821 | error(lineno, 'multiple argument sets') | |
822 | if tt in arguments: | |
823 | arg = arguments[tt] | |
824 | else: | |
825 | error(lineno, 'undefined argument set', t) | |
826 | continue | |
827 | ||
828 | # '@Foo' gives a pattern an explicit format. | |
acfdd239 | 829 | if re.fullmatch(re_fmt_ident, t): |
568ae7ef RH |
830 | tt = t[1:] |
831 | if fmt: | |
832 | error(lineno, 'multiple formats') | |
833 | if tt in formats: | |
834 | fmt = formats[tt] | |
835 | else: | |
836 | error(lineno, 'undefined format', t) | |
837 | continue | |
838 | ||
839 | # '%Foo' imports a field. | |
acfdd239 | 840 | if re.fullmatch(re_fld_ident, t): |
568ae7ef RH |
841 | tt = t[1:] |
842 | flds = add_field_byname(lineno, flds, tt, tt) | |
843 | continue | |
844 | ||
845 | # 'Foo=%Bar' imports a field with a different name. | |
acfdd239 | 846 | if re.fullmatch(re_C_ident + '=' + re_fld_ident, t): |
568ae7ef RH |
847 | (fname, iname) = t.split('=%') |
848 | flds = add_field_byname(lineno, flds, fname, iname) | |
849 | continue | |
850 | ||
851 | # 'Foo=number' sets an argument field to a constant value | |
acfdd239 | 852 | if re.fullmatch(re_C_ident + '=[+-]?[0-9]+', t): |
568ae7ef RH |
853 | (fname, value) = t.split('=') |
854 | value = int(value) | |
855 | flds = add_field(lineno, flds, fname, ConstField(value)) | |
856 | continue | |
857 | ||
858 | # Pattern of 0s, 1s, dots and dashes indicate required zeros, | |
859 | # required ones, or dont-cares. | |
2d110c11 | 860 | if re.fullmatch('[01.-]+', t): |
568ae7ef RH |
861 | shift = len(t) |
862 | fms = t.replace('0', '1') | |
863 | fms = fms.replace('.', '0') | |
864 | fms = fms.replace('-', '0') | |
865 | fbs = t.replace('.', '0') | |
866 | fbs = fbs.replace('-', '0') | |
867 | ubm = t.replace('1', '0') | |
868 | ubm = ubm.replace('.', '0') | |
869 | ubm = ubm.replace('-', '1') | |
870 | fms = int(fms, 2) | |
871 | fbs = int(fbs, 2) | |
872 | ubm = int(ubm, 2) | |
873 | fixedbits = (fixedbits << shift) | fbs | |
874 | fixedmask = (fixedmask << shift) | fms | |
875 | undefmask = (undefmask << shift) | ubm | |
876 | # Otherwise, fieldname:fieldwidth | |
acfdd239 | 877 | elif re.fullmatch(re_C_ident + ':s?[0-9]+', t): |
568ae7ef RH |
878 | (fname, flen) = t.split(':') |
879 | sign = False | |
880 | if flen[0] == 's': | |
881 | sign = True | |
882 | flen = flen[1:] | |
883 | shift = int(flen, 10) | |
2decfc95 RH |
884 | if shift + width > insnwidth: |
885 | error(lineno, 'field {0} exceeds insnwidth'.format(fname)) | |
568ae7ef RH |
886 | f = Field(sign, insnwidth - width - shift, shift) |
887 | flds = add_field(lineno, flds, fname, f) | |
888 | fixedbits <<= shift | |
889 | fixedmask <<= shift | |
890 | undefmask <<= shift | |
891 | else: | |
892 | error(lineno, 'invalid token "{0}"'.format(t)) | |
893 | width += shift | |
894 | ||
17560e93 RH |
895 | if variablewidth and width < insnwidth and width % 8 == 0: |
896 | shift = insnwidth - width | |
897 | fixedbits <<= shift | |
898 | fixedmask <<= shift | |
899 | undefmask <<= shift | |
900 | undefmask |= (1 << shift) - 1 | |
901 | ||
568ae7ef | 902 | # We should have filled in all of the bits of the instruction. |
17560e93 | 903 | elif not (is_format and width == 0) and width != insnwidth: |
568ae7ef RH |
904 | error(lineno, 'definition has {0} bits'.format(width)) |
905 | ||
65fdb3cc | 906 | # Do not check for fields overlapping fields; one valid usage |
568ae7ef RH |
907 | # is to be able to duplicate fields via import. |
908 | fieldmask = 0 | |
909 | for f in flds.values(): | |
910 | fieldmask |= f.mask | |
911 | ||
912 | # Fix up what we've parsed to match either a format or a pattern. | |
913 | if is_format: | |
914 | # Formats cannot reference formats. | |
915 | if fmt: | |
916 | error(lineno, 'format referencing format') | |
917 | # If an argument set is given, then there should be no fields | |
918 | # without a place to store it. | |
919 | if arg: | |
920 | for f in flds.keys(): | |
921 | if f not in arg.fields: | |
922 | error(lineno, 'field {0} not in argument set {1}' | |
923 | .format(f, arg.name)) | |
924 | else: | |
925 | arg = infer_argument_set(flds) | |
926 | if name in formats: | |
927 | error(lineno, 'duplicate format name', name) | |
928 | fmt = Format(name, lineno, arg, fixedbits, fixedmask, | |
17560e93 | 929 | undefmask, fieldmask, flds, width) |
568ae7ef RH |
930 | formats[name] = fmt |
931 | else: | |
932 | # Patterns can reference a format ... | |
933 | if fmt: | |
934 | # ... but not an argument simultaneously | |
935 | if arg: | |
936 | error(lineno, 'pattern specifies both format and argument set') | |
937 | if fixedmask & fmt.fixedmask: | |
938 | error(lineno, 'pattern fixed bits overlap format fixed bits') | |
17560e93 RH |
939 | if width != fmt.width: |
940 | error(lineno, 'pattern uses format of different width') | |
568ae7ef RH |
941 | fieldmask |= fmt.fieldmask |
942 | fixedbits |= fmt.fixedbits | |
943 | fixedmask |= fmt.fixedmask | |
944 | undefmask |= fmt.undefmask | |
945 | else: | |
17560e93 | 946 | (fmt, flds) = infer_format(arg, fieldmask, flds, width) |
568ae7ef RH |
947 | arg = fmt.base |
948 | for f in flds.keys(): | |
949 | if f not in arg.fields: | |
950 | error(lineno, 'field {0} not in argument set {1}' | |
951 | .format(f, arg.name)) | |
952 | if f in fmt.fields.keys(): | |
953 | error(lineno, 'field {0} set by format and pattern'.format(f)) | |
954 | for f in arg.fields: | |
955 | if f not in flds.keys() and f not in fmt.fields.keys(): | |
956 | error(lineno, 'field {0} not initialized'.format(f)) | |
957 | pat = Pattern(name, lineno, fmt, fixedbits, fixedmask, | |
17560e93 | 958 | undefmask, fieldmask, flds, width) |
08561fc1 | 959 | parent_pat.pats.append(pat) |
0eff2df4 | 960 | allpatterns.append(pat) |
568ae7ef RH |
961 | |
962 | # Validate the masks that we have assembled. | |
963 | if fieldmask & fixedmask: | |
964 | error(lineno, 'fieldmask overlaps fixedmask (0x{0:08x} & 0x{1:08x})' | |
965 | .format(fieldmask, fixedmask)) | |
966 | if fieldmask & undefmask: | |
967 | error(lineno, 'fieldmask overlaps undefmask (0x{0:08x} & 0x{1:08x})' | |
968 | .format(fieldmask, undefmask)) | |
969 | if fixedmask & undefmask: | |
970 | error(lineno, 'fixedmask overlaps undefmask (0x{0:08x} & 0x{1:08x})' | |
971 | .format(fixedmask, undefmask)) | |
972 | if not is_format: | |
973 | allbits = fieldmask | fixedmask | undefmask | |
974 | if allbits != insnmask: | |
975 | error(lineno, 'bits left unspecified (0x{0:08x})' | |
976 | .format(allbits ^ insnmask)) | |
977 | # end parse_general | |
978 | ||
0eff2df4 | 979 | |
08561fc1 | 980 | def parse_file(f, parent_pat): |
568ae7ef | 981 | """Parse all of the patterns within a file""" |
acfdd239 RH |
982 | global re_arg_ident |
983 | global re_fld_ident | |
984 | global re_fmt_ident | |
985 | global re_pat_ident | |
568ae7ef RH |
986 | |
987 | # Read all of the lines of the file. Concatenate lines | |
988 | # ending in backslash; discard empty lines and comments. | |
989 | toks = [] | |
990 | lineno = 0 | |
0eff2df4 | 991 | nesting = 0 |
08561fc1 | 992 | nesting_pats = [] |
0eff2df4 | 993 | |
568ae7ef RH |
994 | for line in f: |
995 | lineno += 1 | |
996 | ||
0eff2df4 RH |
997 | # Expand and strip spaces, to find indent. |
998 | line = line.rstrip() | |
999 | line = line.expandtabs() | |
1000 | len1 = len(line) | |
1001 | line = line.lstrip() | |
1002 | len2 = len(line) | |
1003 | ||
568ae7ef RH |
1004 | # Discard comments |
1005 | end = line.find('#') | |
1006 | if end >= 0: | |
1007 | line = line[:end] | |
1008 | ||
1009 | t = line.split() | |
1010 | if len(toks) != 0: | |
1011 | # Next line after continuation | |
1012 | toks.extend(t) | |
568ae7ef | 1013 | else: |
0eff2df4 RH |
1014 | # Allow completely blank lines. |
1015 | if len1 == 0: | |
1016 | continue | |
1017 | indent = len1 - len2 | |
1018 | # Empty line due to comment. | |
1019 | if len(t) == 0: | |
1020 | # Indentation must be correct, even for comment lines. | |
1021 | if indent != nesting: | |
1022 | error(lineno, 'indentation ', indent, ' != ', nesting) | |
1023 | continue | |
1024 | start_lineno = lineno | |
568ae7ef RH |
1025 | toks = t |
1026 | ||
1027 | # Continuation? | |
1028 | if toks[-1] == '\\': | |
1029 | toks.pop() | |
1030 | continue | |
1031 | ||
568ae7ef RH |
1032 | name = toks[0] |
1033 | del toks[0] | |
1034 | ||
0eff2df4 | 1035 | # End nesting? |
067e8b0f | 1036 | if name == '}' or name == ']': |
0eff2df4 RH |
1037 | if len(toks) != 0: |
1038 | error(start_lineno, 'extra tokens after close brace') | |
08561fc1 | 1039 | |
067e8b0f RH |
1040 | # Make sure { } and [ ] nest properly. |
1041 | if (name == '}') != isinstance(parent_pat, IncMultiPattern): | |
1042 | error(lineno, 'mismatched close brace') | |
1043 | ||
08561fc1 RH |
1044 | try: |
1045 | parent_pat = nesting_pats.pop() | |
1046 | except: | |
067e8b0f | 1047 | error(lineno, 'extra close brace') |
08561fc1 | 1048 | |
0eff2df4 RH |
1049 | nesting -= 2 |
1050 | if indent != nesting: | |
08561fc1 RH |
1051 | error(lineno, 'indentation ', indent, ' != ', nesting) |
1052 | ||
0eff2df4 RH |
1053 | toks = [] |
1054 | continue | |
1055 | ||
1056 | # Everything else should have current indentation. | |
1057 | if indent != nesting: | |
1058 | error(start_lineno, 'indentation ', indent, ' != ', nesting) | |
1059 | ||
1060 | # Start nesting? | |
067e8b0f | 1061 | if name == '{' or name == '[': |
0eff2df4 RH |
1062 | if len(toks) != 0: |
1063 | error(start_lineno, 'extra tokens after open brace') | |
08561fc1 | 1064 | |
067e8b0f RH |
1065 | if name == '{': |
1066 | nested_pat = IncMultiPattern(start_lineno) | |
1067 | else: | |
1068 | nested_pat = ExcMultiPattern(start_lineno) | |
08561fc1 RH |
1069 | parent_pat.pats.append(nested_pat) |
1070 | nesting_pats.append(parent_pat) | |
1071 | parent_pat = nested_pat | |
1072 | ||
0eff2df4 RH |
1073 | nesting += 2 |
1074 | toks = [] | |
1075 | continue | |
1076 | ||
568ae7ef | 1077 | # Determine the type of object needing to be parsed. |
acfdd239 | 1078 | if re.fullmatch(re_fld_ident, name): |
0eff2df4 | 1079 | parse_field(start_lineno, name[1:], toks) |
acfdd239 | 1080 | elif re.fullmatch(re_arg_ident, name): |
0eff2df4 | 1081 | parse_arguments(start_lineno, name[1:], toks) |
acfdd239 | 1082 | elif re.fullmatch(re_fmt_ident, name): |
08561fc1 | 1083 | parse_generic(start_lineno, None, name[1:], toks) |
acfdd239 | 1084 | elif re.fullmatch(re_pat_ident, name): |
08561fc1 | 1085 | parse_generic(start_lineno, parent_pat, name, toks) |
acfdd239 RH |
1086 | else: |
1087 | error(lineno, 'invalid token "{0}"'.format(name)) | |
568ae7ef | 1088 | toks = [] |
067e8b0f RH |
1089 | |
1090 | if nesting != 0: | |
1091 | error(lineno, 'missing close brace') | |
568ae7ef RH |
1092 | # end parse_file |
1093 | ||
1094 | ||
70e0711a RH |
1095 | class SizeTree: |
1096 | """Class representing a node in a size decode tree""" | |
1097 | ||
1098 | def __init__(self, m, w): | |
1099 | self.mask = m | |
1100 | self.subs = [] | |
1101 | self.base = None | |
1102 | self.width = w | |
1103 | ||
1104 | def str1(self, i): | |
1105 | ind = str_indent(i) | |
1106 | r = '{0}{1:08x}'.format(ind, self.mask) | |
1107 | r += ' [\n' | |
1108 | for (b, s) in self.subs: | |
1109 | r += '{0} {1:08x}:\n'.format(ind, b) | |
1110 | r += s.str1(i + 4) + '\n' | |
1111 | r += ind + ']' | |
1112 | return r | |
1113 | ||
1114 | def __str__(self): | |
1115 | return self.str1(0) | |
1116 | ||
1117 | def output_code(self, i, extracted, outerbits, outermask): | |
1118 | ind = str_indent(i) | |
1119 | ||
1120 | # If we need to load more bytes to test, do so now. | |
1121 | if extracted < self.width: | |
1122 | output(ind, 'insn = ', decode_function, | |
1123 | '_load_bytes(ctx, insn, {0}, {1});\n' | |
b4123787 | 1124 | .format(extracted // 8, self.width // 8)); |
70e0711a RH |
1125 | extracted = self.width |
1126 | ||
1127 | # Attempt to aid the compiler in producing compact switch statements. | |
1128 | # If the bits in the mask are contiguous, extract them. | |
1129 | sh = is_contiguous(self.mask) | |
1130 | if sh > 0: | |
1131 | # Propagate SH down into the local functions. | |
1132 | def str_switch(b, sh=sh): | |
1133 | return '(insn >> {0}) & 0x{1:x}'.format(sh, b >> sh) | |
1134 | ||
1135 | def str_case(b, sh=sh): | |
1136 | return '0x{0:x}'.format(b >> sh) | |
1137 | else: | |
1138 | def str_switch(b): | |
1139 | return 'insn & 0x{0:08x}'.format(b) | |
1140 | ||
1141 | def str_case(b): | |
1142 | return '0x{0:08x}'.format(b) | |
1143 | ||
1144 | output(ind, 'switch (', str_switch(self.mask), ') {\n') | |
1145 | for b, s in sorted(self.subs): | |
1146 | innermask = outermask | self.mask | |
1147 | innerbits = outerbits | b | |
1148 | output(ind, 'case ', str_case(b), ':\n') | |
1149 | output(ind, ' /* ', | |
1150 | str_match_bits(innerbits, innermask), ' */\n') | |
1151 | s.output_code(i + 4, extracted, innerbits, innermask) | |
1152 | output(ind, '}\n') | |
1153 | output(ind, 'return insn;\n') | |
1154 | # end SizeTree | |
1155 | ||
1156 | class SizeLeaf: | |
1157 | """Class representing a leaf node in a size decode tree""" | |
1158 | ||
1159 | def __init__(self, m, w): | |
1160 | self.mask = m | |
1161 | self.width = w | |
1162 | ||
1163 | def str1(self, i): | |
1164 | ind = str_indent(i) | |
1165 | return '{0}{1:08x}'.format(ind, self.mask) | |
1166 | ||
1167 | def __str__(self): | |
1168 | return self.str1(0) | |
1169 | ||
1170 | def output_code(self, i, extracted, outerbits, outermask): | |
1171 | global decode_function | |
1172 | ind = str_indent(i) | |
1173 | ||
1174 | # If we need to load more bytes, do so now. | |
1175 | if extracted < self.width: | |
1176 | output(ind, 'insn = ', decode_function, | |
1177 | '_load_bytes(ctx, insn, {0}, {1});\n' | |
b4123787 | 1178 | .format(extracted // 8, self.width // 8)); |
70e0711a RH |
1179 | extracted = self.width |
1180 | output(ind, 'return insn;\n') | |
1181 | # end SizeLeaf | |
1182 | ||
1183 | ||
1184 | def build_size_tree(pats, width, outerbits, outermask): | |
1185 | global insnwidth | |
1186 | ||
1187 | # Collect the mask of bits that are fixed in this width | |
1188 | innermask = 0xff << (insnwidth - width) | |
1189 | innermask &= ~outermask | |
1190 | minwidth = None | |
1191 | onewidth = True | |
1192 | for i in pats: | |
1193 | innermask &= i.fixedmask | |
1194 | if minwidth is None: | |
1195 | minwidth = i.width | |
1196 | elif minwidth != i.width: | |
1197 | onewidth = False; | |
1198 | if minwidth < i.width: | |
1199 | minwidth = i.width | |
1200 | ||
1201 | if onewidth: | |
1202 | return SizeLeaf(innermask, minwidth) | |
1203 | ||
1204 | if innermask == 0: | |
1205 | if width < minwidth: | |
1206 | return build_size_tree(pats, width + 8, outerbits, outermask) | |
1207 | ||
1208 | pnames = [] | |
1209 | for p in pats: | |
1210 | pnames.append(p.name + ':' + p.file + ':' + str(p.lineno)) | |
1211 | error_with_file(pats[0].file, pats[0].lineno, | |
1212 | 'overlapping patterns size {0}:'.format(width), pnames) | |
1213 | ||
1214 | bins = {} | |
1215 | for i in pats: | |
1216 | fb = i.fixedbits & innermask | |
1217 | if fb in bins: | |
1218 | bins[fb].append(i) | |
1219 | else: | |
1220 | bins[fb] = [i] | |
1221 | ||
1222 | fullmask = outermask | innermask | |
1223 | lens = sorted(bins.keys()) | |
1224 | if len(lens) == 1: | |
1225 | b = lens[0] | |
1226 | return build_size_tree(bins[b], width + 8, b | outerbits, fullmask) | |
1227 | ||
1228 | r = SizeTree(innermask, width) | |
1229 | for b, l in bins.items(): | |
1230 | s = build_size_tree(l, width, b | outerbits, fullmask) | |
1231 | r.subs.append((b, s)) | |
1232 | return r | |
1233 | # end build_size_tree | |
1234 | ||
1235 | ||
70e0711a RH |
1236 | def prop_size(tree): |
1237 | """Propagate minimum widths up the decode size tree""" | |
1238 | ||
1239 | if isinstance(tree, SizeTree): | |
1240 | min = None | |
1241 | for (b, s) in tree.subs: | |
1242 | width = prop_size(s) | |
1243 | if min is None or min > width: | |
1244 | min = width | |
1245 | assert min >= tree.width | |
1246 | tree.width = min | |
1247 | else: | |
1248 | min = tree.width | |
1249 | return min | |
1250 | # end prop_size | |
1251 | ||
1252 | ||
568ae7ef RH |
1253 | def main(): |
1254 | global arguments | |
1255 | global formats | |
0eff2df4 | 1256 | global allpatterns |
568ae7ef RH |
1257 | global translate_scope |
1258 | global translate_prefix | |
1259 | global output_fd | |
1260 | global output_file | |
1261 | global input_file | |
1262 | global insnwidth | |
1263 | global insntype | |
83d7c40c | 1264 | global insnmask |
abd04f92 | 1265 | global decode_function |
17560e93 | 1266 | global variablewidth |
c6920795 | 1267 | global anyextern |
568ae7ef | 1268 | |
568ae7ef RH |
1269 | decode_scope = 'static ' |
1270 | ||
cd3e7fc1 | 1271 | long_opts = ['decode=', 'translate=', 'output=', 'insnwidth=', |
17560e93 | 1272 | 'static-decode=', 'varinsnwidth='] |
568ae7ef | 1273 | try: |
abff1abf | 1274 | (opts, args) = getopt.gnu_getopt(sys.argv[1:], 'o:vw:', long_opts) |
568ae7ef RH |
1275 | except getopt.GetoptError as err: |
1276 | error(0, err) | |
1277 | for o, a in opts: | |
1278 | if o in ('-o', '--output'): | |
1279 | output_file = a | |
1280 | elif o == '--decode': | |
1281 | decode_function = a | |
1282 | decode_scope = '' | |
cd3e7fc1 RH |
1283 | elif o == '--static-decode': |
1284 | decode_function = a | |
568ae7ef RH |
1285 | elif o == '--translate': |
1286 | translate_prefix = a | |
1287 | translate_scope = '' | |
17560e93 RH |
1288 | elif o in ('-w', '--insnwidth', '--varinsnwidth'): |
1289 | if o == '--varinsnwidth': | |
1290 | variablewidth = True | |
568ae7ef RH |
1291 | insnwidth = int(a) |
1292 | if insnwidth == 16: | |
1293 | insntype = 'uint16_t' | |
1294 | insnmask = 0xffff | |
1295 | elif insnwidth != 32: | |
1296 | error(0, 'cannot handle insns of width', insnwidth) | |
1297 | else: | |
1298 | assert False, 'unhandled option' | |
1299 | ||
1300 | if len(args) < 1: | |
1301 | error(0, 'missing input file') | |
08561fc1 RH |
1302 | |
1303 | toppat = ExcMultiPattern(0) | |
1304 | ||
6699ae6a RH |
1305 | for filename in args: |
1306 | input_file = filename | |
1307 | f = open(filename, 'r') | |
08561fc1 | 1308 | parse_file(f, toppat) |
6699ae6a | 1309 | f.close() |
568ae7ef | 1310 | |
08561fc1 RH |
1311 | # We do not want to compute masks for toppat, because those masks |
1312 | # are used as a starting point for build_tree. For toppat, we must | |
1313 | # insist that decode begins from naught. | |
1314 | for i in toppat.pats: | |
1315 | i.prop_masks() | |
1316 | ||
1317 | toppat.build_tree() | |
1318 | toppat.prop_format() | |
1319 | ||
70e0711a | 1320 | if variablewidth: |
08561fc1 RH |
1321 | for i in toppat.pats: |
1322 | i.prop_width() | |
1323 | stree = build_size_tree(toppat.pats, 8, 0, 0) | |
70e0711a RH |
1324 | prop_size(stree) |
1325 | ||
568ae7ef RH |
1326 | if output_file: |
1327 | output_fd = open(output_file, 'w') | |
1328 | else: | |
1329 | output_fd = sys.stdout | |
1330 | ||
1331 | output_autogen() | |
1332 | for n in sorted(arguments.keys()): | |
1333 | f = arguments[n] | |
1334 | f.output_def() | |
1335 | ||
1336 | # A single translate function can be invoked for different patterns. | |
1337 | # Make sure that the argument sets are the same, and declare the | |
1338 | # function only once. | |
c6920795 RH |
1339 | # |
1340 | # If we're sharing formats, we're likely also sharing trans_* functions, | |
1341 | # but we can't tell which ones. Prevent issues from the compiler by | |
1342 | # suppressing redundant declaration warnings. | |
1343 | if anyextern: | |
7aa12aa2 TH |
1344 | output("#pragma GCC diagnostic push\n", |
1345 | "#pragma GCC diagnostic ignored \"-Wredundant-decls\"\n", | |
1346 | "#ifdef __clang__\n" | |
c6920795 | 1347 | "# pragma GCC diagnostic ignored \"-Wtypedef-redefinition\"\n", |
c6920795 RH |
1348 | "#endif\n\n") |
1349 | ||
568ae7ef | 1350 | out_pats = {} |
0eff2df4 | 1351 | for i in allpatterns: |
568ae7ef RH |
1352 | if i.name in out_pats: |
1353 | p = out_pats[i.name] | |
1354 | if i.base.base != p.base.base: | |
1355 | error(0, i.name, ' has conflicting argument sets') | |
1356 | else: | |
1357 | i.output_decl() | |
1358 | out_pats[i.name] = i | |
1359 | output('\n') | |
1360 | ||
c6920795 | 1361 | if anyextern: |
7aa12aa2 | 1362 | output("#pragma GCC diagnostic pop\n\n") |
c6920795 | 1363 | |
568ae7ef RH |
1364 | for n in sorted(formats.keys()): |
1365 | f = formats[n] | |
1366 | f.output_extract() | |
1367 | ||
1368 | output(decode_scope, 'bool ', decode_function, | |
1369 | '(DisasContext *ctx, ', insntype, ' insn)\n{\n') | |
1370 | ||
1371 | i4 = str_indent(4) | |
568ae7ef | 1372 | |
82bfac1c RH |
1373 | if len(allpatterns) != 0: |
1374 | output(i4, 'union {\n') | |
1375 | for n in sorted(arguments.keys()): | |
1376 | f = arguments[n] | |
1377 | output(i4, i4, f.struct_name(), ' f_', f.name, ';\n') | |
1378 | output(i4, '} u;\n\n') | |
08561fc1 | 1379 | toppat.output_code(4, False, 0, 0) |
568ae7ef | 1380 | |
82bfac1c | 1381 | output(i4, 'return false;\n') |
568ae7ef RH |
1382 | output('}\n') |
1383 | ||
70e0711a RH |
1384 | if variablewidth: |
1385 | output('\n', decode_scope, insntype, ' ', decode_function, | |
1386 | '_load(DisasContext *ctx)\n{\n', | |
1387 | ' ', insntype, ' insn = 0;\n\n') | |
1388 | stree.output_code(4, 0, 0, 0) | |
1389 | output('}\n') | |
1390 | ||
568ae7ef RH |
1391 | if output_file: |
1392 | output_fd.close() | |
1393 | # end main | |
1394 | ||
1395 | ||
1396 | if __name__ == '__main__': | |
1397 | main() |