2 # Copyright (c) 2018 Linaro Limited
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.1 of the License, or (at your option) any later version.
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.
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/>.
19 # Generate a decoding tree from a specification file.
20 # See the syntax and semantics in docs/devel/decodetree.rst.
38 translate_prefix = 'trans'
39 translate_scope = 'static '
44 decode_function = 'decode'
46 # An identifier for C.
47 re_C_ident = '[a-zA-Z][a-zA-Z0-9_]*'
49 # Identifiers for Arguments, Fields, Formats and Patterns.
50 re_arg_ident = '&[a-zA-Z0-9_]*'
51 re_fld_ident = '%[a-zA-Z0-9_]*'
52 re_fmt_ident = '@[a-zA-Z0-9_]*'
53 re_pat_ident = '[a-zA-Z0-9_]*'
55 def error_with_file(file, lineno, *args):
56 """Print an error message from file:line and args and exit."""
62 prefix += '{0}:'.format(file)
64 prefix += '{0}:'.format(lineno)
67 print(prefix, end='error: ', file=sys.stderr)
68 print(*args, file=sys.stderr)
70 if output_file and output_fd:
72 os.remove(output_file)
77 def error(lineno, *args):
78 error_with_file(input_file, lineno, *args)
89 output('/* This file is autogenerated by scripts/decodetree.py. */\n\n')
93 """Return a string with C spaces"""
97 def str_fields(fields):
98 """Return a string uniquely identifying FIELDS"""
100 for n in sorted(fields.keys()):
105 def str_match_bits(bits, mask):
106 """Return a string pretty-printing BITS/MASK"""
109 i = 1 << (insnwidth - 1)
127 """Return true iff X is equal to a power of 2."""
128 return (x & (x - 1)) == 0
132 """Return the number of times 2 factors into X."""
135 while ((x >> r) & 1) == 0:
140 def is_contiguous(bits):
144 if is_pow2((bits >> shift) + 1):
150 def eq_fields_for_args(flds_a, flds_b):
151 if len(flds_a) != len(flds_b):
153 for k, a in flds_a.items():
159 def eq_fields_for_fmts(flds_a, flds_b):
160 if len(flds_a) != len(flds_b):
162 for k, a in flds_a.items():
166 if a.__class__ != b.__class__ or a != b:
172 """Class representing a simple instruction field"""
173 def __init__(self, sign, pos, len):
177 self.mask = ((1 << len) - 1) << pos
184 return str(self.pos) + ':' + s + str(self.len)
186 def str_extract(self):
191 return '{0}(insn, {1}, {2})'.format(extr, self.pos, self.len)
193 def __eq__(self, other):
194 return self.sign == other.sign and self.mask == other.mask
196 def __ne__(self, other):
197 return not self.__eq__(other)
202 """Class representing a compound instruction field"""
203 def __init__(self, subs, mask):
205 self.sign = subs[0].sign
209 return str(self.subs)
211 def str_extract(self):
214 for f in reversed(self.subs):
216 ret = f.str_extract()
218 ret = 'deposit32({0}, {1}, {2}, {3})' \
219 .format(ret, pos, 32 - pos, f.str_extract())
223 def __ne__(self, other):
224 if len(self.subs) != len(other.subs):
226 for a, b in zip(self.subs, other.subs):
227 if a.__class__ != b.__class__ or a != b:
231 def __eq__(self, other):
232 return not self.__ne__(other)
237 """Class representing an argument field with constant value"""
238 def __init__(self, value):
241 self.sign = value < 0
244 return str(self.value)
246 def str_extract(self):
247 return str(self.value)
249 def __cmp__(self, other):
250 return self.value - other.value
255 """Class representing a field passed through a function"""
256 def __init__(self, func, base):
257 self.mask = base.mask
258 self.sign = base.sign
263 return self.func + '(' + str(self.base) + ')'
265 def str_extract(self):
266 return self.func + '(ctx, ' + self.base.str_extract() + ')'
268 def __eq__(self, other):
269 return self.func == other.func and self.base == other.base
271 def __ne__(self, other):
272 return not self.__eq__(other)
276 class ParameterField:
277 """Class representing a pseudo-field read from a function"""
278 def __init__(self, func):
286 def str_extract(self):
287 return self.func + '(ctx)'
289 def __eq__(self, other):
290 return self.func == other.func
292 def __ne__(self, other):
293 return not self.__eq__(other)
298 """Class representing the extracted fields of a format"""
299 def __init__(self, nm, flds, extern):
302 self.fields = sorted(flds)
305 return self.name + ' ' + str(self.fields)
307 def struct_name(self):
308 return 'arg_' + self.name
310 def output_def(self):
312 output('typedef struct {\n')
313 for n in self.fields:
314 output(' int ', n, ';\n')
315 output('} ', self.struct_name(), ';\n\n')
320 """Common code between instruction formats and instruction patterns"""
321 def __init__(self, name, lineno, base, fixb, fixm, udfm, fldm, flds, w):
323 self.file = input_file
326 self.fixedbits = fixb
327 self.fixedmask = fixm
328 self.undefmask = udfm
329 self.fieldmask = fldm
334 return self.name + ' ' + str_match_bits(self.fixedbits, self.fixedmask)
337 return str_indent(i) + self.__str__()
341 class Format(General):
342 """Class representing an instruction format"""
344 def extract_name(self):
345 global decode_function
346 return decode_function + '_extract_' + self.name
348 def output_extract(self):
349 output('static void ', self.extract_name(), '(DisasContext *ctx, ',
350 self.base.struct_name(), ' *a, ', insntype, ' insn)\n{\n')
351 for n, f in self.fields.items():
352 output(' a->', n, ' = ', f.str_extract(), ';\n')
357 class Pattern(General):
358 """Class representing an instruction pattern"""
360 def output_decl(self):
361 global translate_scope
362 global translate_prefix
363 output('typedef ', self.base.base.struct_name(),
364 ' arg_', self.name, ';\n')
365 output(translate_scope, 'bool ', translate_prefix, '_', self.name,
366 '(DisasContext *ctx, arg_', self.name, ' *a);\n')
368 def output_code(self, i, extracted, outerbits, outermask):
369 global translate_prefix
371 arg = self.base.base.name
372 output(ind, '/* ', self.file, ':', str(self.lineno), ' */\n')
374 output(ind, self.base.extract_name(),
375 '(ctx, &u.f_', arg, ', insn);\n')
376 for n, f in self.fields.items():
377 output(ind, 'u.f_', arg, '.', n, ' = ', f.str_extract(), ';\n')
378 output(ind, 'if (', translate_prefix, '_', self.name,
379 '(ctx, &u.f_', arg, ')) return true;\n')
381 # Normal patterns do not have children.
382 def build_tree(self):
384 def prop_masks(self):
386 def prop_format(self):
388 def prop_width(self):
394 class MultiPattern(General):
395 """Class representing a set of instruction patterns"""
397 def __init__(self, lineno):
398 self.file = input_file
409 if self.fixedbits is not None:
410 r += ' ' + str_match_bits(self.fixedbits, self.fixedmask)
413 def output_decl(self):
417 def prop_masks(self):
423 # Collect fixedmask/undefmask for all of the children.
426 fixedmask &= p.fixedmask
427 undefmask &= p.undefmask
429 # Widen fixedmask until all fixedbits match
432 while repeat and fixedmask != 0:
435 thisbits = p.fixedbits & fixedmask
436 if fixedbits is None:
438 elif fixedbits != thisbits:
439 fixedmask &= ~(fixedbits ^ thisbits)
444 self.fixedbits = fixedbits
445 self.fixedmask = fixedmask
446 self.undefmask = undefmask
448 def build_tree(self):
452 def prop_format(self):
456 def prop_width(self):
462 elif width != p.width:
463 error_with_file(self.file, self.lineno,
464 'width mismatch in patterns within braces')
470 class IncMultiPattern(MultiPattern):
471 """Class representing an overlapping set of instruction patterns"""
473 def output_code(self, i, extracted, outerbits, outermask):
474 global translate_prefix
477 if outermask != p.fixedmask:
478 innermask = p.fixedmask & ~outermask
479 innerbits = p.fixedbits & ~outermask
480 output(ind, 'if ((insn & ',
481 '0x{0:08x}) == 0x{1:08x}'.format(innermask, innerbits),
484 str_match_bits(p.fixedbits, p.fixedmask), ' */\n')
485 p.output_code(i + 4, extracted, p.fixedbits, p.fixedmask)
488 p.output_code(i, extracted, p.fixedbits, p.fixedmask)
493 """Class representing a node in a decode tree"""
495 def __init__(self, fm, tm):
503 r = '{0}{1:08x}'.format(ind, self.fixedmask)
505 r += ' ' + self.format.name
507 for (b, s) in self.subs:
508 r += '{0} {1:08x}:\n'.format(ind, b)
509 r += s.str1(i + 4) + '\n'
516 def output_code(self, i, extracted, outerbits, outermask):
519 # If we identified all nodes below have the same format,
520 # extract the fields now.
521 if not extracted and self.base:
522 output(ind, self.base.extract_name(),
523 '(ctx, &u.f_', self.base.base.name, ', insn);\n')
526 # Attempt to aid the compiler in producing compact switch statements.
527 # If the bits in the mask are contiguous, extract them.
528 sh = is_contiguous(self.thismask)
530 # Propagate SH down into the local functions.
531 def str_switch(b, sh=sh):
532 return '(insn >> {0}) & 0x{1:x}'.format(sh, b >> sh)
534 def str_case(b, sh=sh):
535 return '0x{0:x}'.format(b >> sh)
538 return 'insn & 0x{0:08x}'.format(b)
541 return '0x{0:08x}'.format(b)
543 output(ind, 'switch (', str_switch(self.thismask), ') {\n')
544 for b, s in sorted(self.subs):
545 assert (self.thismask & ~s.fixedmask) == 0
546 innermask = outermask | self.thismask
547 innerbits = outerbits | b
548 output(ind, 'case ', str_case(b), ':\n')
550 str_match_bits(innerbits, innermask), ' */\n')
551 s.output_code(i + 4, extracted, innerbits, innermask)
552 output(ind, ' break;\n')
557 class ExcMultiPattern(MultiPattern):
558 """Class representing a non-overlapping set of instruction patterns"""
560 def output_code(self, i, extracted, outerbits, outermask):
561 # Defer everything to our decomposed Tree node
562 self.tree.output_code(i, extracted, outerbits, outermask)
565 def __build_tree(pats, outerbits, outermask):
566 # Find the intersection of all remaining fixedmask.
567 innermask = ~outermask & insnmask
569 innermask &= i.fixedmask
572 # Edge condition: One pattern covers the entire insnmask
574 t = Tree(outermask, innermask)
575 t.subs.append((0, pats[0]))
578 text = 'overlapping patterns:'
580 text += '\n' + p.file + ':' + str(p.lineno) + ': ' + str(p)
581 error_with_file(pats[0].file, pats[0].lineno, text)
583 fullmask = outermask | innermask
585 # Sort each element of pats into the bin selected by the mask.
588 fb = i.fixedbits & innermask
594 # We must recurse if any bin has more than one element or if
595 # the single element in the bin has not been fully matched.
596 t = Tree(fullmask, innermask)
598 for b, l in bins.items():
600 if len(l) > 1 or s.fixedmask & ~fullmask != 0:
601 s = ExcMultiPattern.__build_tree(l, b | outerbits, fullmask)
602 t.subs.append((b, s))
606 def build_tree(self):
607 super().prop_format()
608 self.tree = self.__build_tree(self.pats, self.fixedbits,
612 def __prop_format(tree):
613 """Propagate Format objects into the decode tree"""
615 # Depth first search.
616 for (b, s) in tree.subs:
617 if isinstance(s, Tree):
618 ExcMultiPattern.__prop_format(s)
620 # If all entries in SUBS have the same format, then
621 # propagate that into the tree.
623 for (b, s) in tree.subs:
632 def prop_format(self):
633 super().prop_format()
634 self.__prop_format(self.tree)
636 # end ExcMultiPattern
639 def parse_field(lineno, name, toks):
640 """Parse one instruction field from TOKS at LINENO"""
644 # A "simple" field will have only one entry;
645 # a "multifield" will have several.
650 if re.match('^!function=', t):
652 error(lineno, 'duplicate function')
657 if re.fullmatch('[0-9]+:s[0-9]+', t):
658 # Signed field extract
659 subtoks = t.split(':s')
661 elif re.fullmatch('[0-9]+:[0-9]+', t):
662 # Unsigned field extract
663 subtoks = t.split(':')
666 error(lineno, 'invalid field token "{0}"'.format(t))
669 if po + le > insnwidth:
670 error(lineno, 'field {0} too large'.format(t))
671 f = Field(sign, po, le)
675 if width > insnwidth:
676 error(lineno, 'field too large')
679 f = ParameterField(func)
681 error(lineno, 'field with no value')
689 error(lineno, 'field components overlap')
691 f = MultiField(subs, mask)
693 f = FunctionField(func, f)
696 error(lineno, 'duplicate field', name)
701 def parse_arguments(lineno, name, toks):
702 """Parse one argument set from TOKS at LINENO"""
710 if re.fullmatch('!extern', t):
714 if not re.fullmatch(re_C_ident, t):
715 error(lineno, 'invalid argument set token "{0}"'.format(t))
717 error(lineno, 'duplicate argument "{0}"'.format(t))
720 if name in arguments:
721 error(lineno, 'duplicate argument set', name)
722 arguments[name] = Arguments(name, flds, extern)
723 # end parse_arguments
726 def lookup_field(lineno, name):
730 error(lineno, 'undefined field', name)
733 def add_field(lineno, flds, new_name, f):
735 error(lineno, 'duplicate field', new_name)
740 def add_field_byname(lineno, flds, new_name, old_name):
741 return add_field(lineno, flds, new_name, lookup_field(lineno, old_name))
744 def infer_argument_set(flds):
746 global decode_function
748 for arg in arguments.values():
749 if eq_fields_for_args(flds, arg.fields):
752 name = decode_function + str(len(arguments))
753 arg = Arguments(name, flds.keys(), False)
754 arguments[name] = arg
758 def infer_format(arg, fieldmask, flds, width):
761 global decode_function
765 for n, c in flds.items():
771 # Look for an existing format with the same argument set and fields
772 for fmt in formats.values():
773 if arg and fmt.base != arg:
775 if fieldmask != fmt.fieldmask:
777 if width != fmt.width:
779 if not eq_fields_for_fmts(flds, fmt.fields):
781 return (fmt, const_flds)
783 name = decode_function + '_Fmt_' + str(len(formats))
785 arg = infer_argument_set(flds)
787 fmt = Format(name, 0, arg, 0, 0, 0, fieldmask, var_flds, width)
790 return (fmt, const_flds)
794 def parse_generic(lineno, parent_pat, name, toks):
795 """Parse one instruction format from TOKS at LINENO"""
808 is_format = parent_pat is None
818 # '&Foo' gives a format an explicit argument set.
819 if re.fullmatch(re_arg_ident, t):
822 error(lineno, 'multiple argument sets')
826 error(lineno, 'undefined argument set', t)
829 # '@Foo' gives a pattern an explicit format.
830 if re.fullmatch(re_fmt_ident, t):
833 error(lineno, 'multiple formats')
837 error(lineno, 'undefined format', t)
840 # '%Foo' imports a field.
841 if re.fullmatch(re_fld_ident, t):
843 flds = add_field_byname(lineno, flds, tt, tt)
846 # 'Foo=%Bar' imports a field with a different name.
847 if re.fullmatch(re_C_ident + '=' + re_fld_ident, t):
848 (fname, iname) = t.split('=%')
849 flds = add_field_byname(lineno, flds, fname, iname)
852 # 'Foo=number' sets an argument field to a constant value
853 if re.fullmatch(re_C_ident + '=[+-]?[0-9]+', t):
854 (fname, value) = t.split('=')
856 flds = add_field(lineno, flds, fname, ConstField(value))
859 # Pattern of 0s, 1s, dots and dashes indicate required zeros,
860 # required ones, or dont-cares.
861 if re.fullmatch('[01.-]+', t):
863 fms = t.replace('0', '1')
864 fms = fms.replace('.', '0')
865 fms = fms.replace('-', '0')
866 fbs = t.replace('.', '0')
867 fbs = fbs.replace('-', '0')
868 ubm = t.replace('1', '0')
869 ubm = ubm.replace('.', '0')
870 ubm = ubm.replace('-', '1')
874 fixedbits = (fixedbits << shift) | fbs
875 fixedmask = (fixedmask << shift) | fms
876 undefmask = (undefmask << shift) | ubm
877 # Otherwise, fieldname:fieldwidth
878 elif re.fullmatch(re_C_ident + ':s?[0-9]+', t):
879 (fname, flen) = t.split(':')
884 shift = int(flen, 10)
885 if shift + width > insnwidth:
886 error(lineno, 'field {0} exceeds insnwidth'.format(fname))
887 f = Field(sign, insnwidth - width - shift, shift)
888 flds = add_field(lineno, flds, fname, f)
893 error(lineno, 'invalid token "{0}"'.format(t))
896 if variablewidth and width < insnwidth and width % 8 == 0:
897 shift = insnwidth - width
901 undefmask |= (1 << shift) - 1
903 # We should have filled in all of the bits of the instruction.
904 elif not (is_format and width == 0) and width != insnwidth:
905 error(lineno, 'definition has {0} bits'.format(width))
907 # Do not check for fields overlapping fields; one valid usage
908 # is to be able to duplicate fields via import.
910 for f in flds.values():
913 # Fix up what we've parsed to match either a format or a pattern.
915 # Formats cannot reference formats.
917 error(lineno, 'format referencing format')
918 # If an argument set is given, then there should be no fields
919 # without a place to store it.
921 for f in flds.keys():
922 if f not in arg.fields:
923 error(lineno, 'field {0} not in argument set {1}'
924 .format(f, arg.name))
926 arg = infer_argument_set(flds)
928 error(lineno, 'duplicate format name', name)
929 fmt = Format(name, lineno, arg, fixedbits, fixedmask,
930 undefmask, fieldmask, flds, width)
933 # Patterns can reference a format ...
935 # ... but not an argument simultaneously
937 error(lineno, 'pattern specifies both format and argument set')
938 if fixedmask & fmt.fixedmask:
939 error(lineno, 'pattern fixed bits overlap format fixed bits')
940 if width != fmt.width:
941 error(lineno, 'pattern uses format of different width')
942 fieldmask |= fmt.fieldmask
943 fixedbits |= fmt.fixedbits
944 fixedmask |= fmt.fixedmask
945 undefmask |= fmt.undefmask
947 (fmt, flds) = infer_format(arg, fieldmask, flds, width)
949 for f in flds.keys():
950 if f not in arg.fields:
951 error(lineno, 'field {0} not in argument set {1}'
952 .format(f, arg.name))
953 if f in fmt.fields.keys():
954 error(lineno, 'field {0} set by format and pattern'.format(f))
956 if f not in flds.keys() and f not in fmt.fields.keys():
957 error(lineno, 'field {0} not initialized'.format(f))
958 pat = Pattern(name, lineno, fmt, fixedbits, fixedmask,
959 undefmask, fieldmask, flds, width)
960 parent_pat.pats.append(pat)
961 allpatterns.append(pat)
963 # Validate the masks that we have assembled.
964 if fieldmask & fixedmask:
965 error(lineno, 'fieldmask overlaps fixedmask (0x{0:08x} & 0x{1:08x})'
966 .format(fieldmask, fixedmask))
967 if fieldmask & undefmask:
968 error(lineno, 'fieldmask overlaps undefmask (0x{0:08x} & 0x{1:08x})'
969 .format(fieldmask, undefmask))
970 if fixedmask & undefmask:
971 error(lineno, 'fixedmask overlaps undefmask (0x{0:08x} & 0x{1:08x})'
972 .format(fixedmask, undefmask))
974 allbits = fieldmask | fixedmask | undefmask
975 if allbits != insnmask:
976 error(lineno, 'bits left unspecified (0x{0:08x})'
977 .format(allbits ^ insnmask))
981 def parse_file(f, parent_pat):
982 """Parse all of the patterns within a file"""
988 # Read all of the lines of the file. Concatenate lines
989 # ending in backslash; discard empty lines and comments.
998 # Expand and strip spaces, to find indent.
1000 line = line.expandtabs()
1002 line = line.lstrip()
1006 end = line.find('#')
1012 # Next line after continuation
1015 # Allow completely blank lines.
1018 indent = len1 - len2
1019 # Empty line due to comment.
1021 # Indentation must be correct, even for comment lines.
1022 if indent != nesting:
1023 error(lineno, 'indentation ', indent, ' != ', nesting)
1025 start_lineno = lineno
1029 if toks[-1] == '\\':
1037 if name == '}' or name == ']':
1039 error(start_lineno, 'extra tokens after close brace')
1041 # Make sure { } and [ ] nest properly.
1042 if (name == '}') != isinstance(parent_pat, IncMultiPattern):
1043 error(lineno, 'mismatched close brace')
1046 parent_pat = nesting_pats.pop()
1048 error(lineno, 'extra close brace')
1051 if indent != nesting:
1052 error(lineno, 'indentation ', indent, ' != ', nesting)
1057 # Everything else should have current indentation.
1058 if indent != nesting:
1059 error(start_lineno, 'indentation ', indent, ' != ', nesting)
1062 if name == '{' or name == '[':
1064 error(start_lineno, 'extra tokens after open brace')
1067 nested_pat = IncMultiPattern(start_lineno)
1069 nested_pat = ExcMultiPattern(start_lineno)
1070 parent_pat.pats.append(nested_pat)
1071 nesting_pats.append(parent_pat)
1072 parent_pat = nested_pat
1078 # Determine the type of object needing to be parsed.
1079 if re.fullmatch(re_fld_ident, name):
1080 parse_field(start_lineno, name[1:], toks)
1081 elif re.fullmatch(re_arg_ident, name):
1082 parse_arguments(start_lineno, name[1:], toks)
1083 elif re.fullmatch(re_fmt_ident, name):
1084 parse_generic(start_lineno, None, name[1:], toks)
1085 elif re.fullmatch(re_pat_ident, name):
1086 parse_generic(start_lineno, parent_pat, name, toks)
1088 error(lineno, 'invalid token "{0}"'.format(name))
1092 error(lineno, 'missing close brace')
1097 """Class representing a node in a size decode tree"""
1099 def __init__(self, m, w):
1107 r = '{0}{1:08x}'.format(ind, self.mask)
1109 for (b, s) in self.subs:
1110 r += '{0} {1:08x}:\n'.format(ind, b)
1111 r += s.str1(i + 4) + '\n'
1118 def output_code(self, i, extracted, outerbits, outermask):
1121 # If we need to load more bytes to test, do so now.
1122 if extracted < self.width:
1123 output(ind, 'insn = ', decode_function,
1124 '_load_bytes(ctx, insn, {0}, {1});\n'
1125 .format(extracted // 8, self.width // 8));
1126 extracted = self.width
1128 # Attempt to aid the compiler in producing compact switch statements.
1129 # If the bits in the mask are contiguous, extract them.
1130 sh = is_contiguous(self.mask)
1132 # Propagate SH down into the local functions.
1133 def str_switch(b, sh=sh):
1134 return '(insn >> {0}) & 0x{1:x}'.format(sh, b >> sh)
1136 def str_case(b, sh=sh):
1137 return '0x{0:x}'.format(b >> sh)
1140 return 'insn & 0x{0:08x}'.format(b)
1143 return '0x{0:08x}'.format(b)
1145 output(ind, 'switch (', str_switch(self.mask), ') {\n')
1146 for b, s in sorted(self.subs):
1147 innermask = outermask | self.mask
1148 innerbits = outerbits | b
1149 output(ind, 'case ', str_case(b), ':\n')
1151 str_match_bits(innerbits, innermask), ' */\n')
1152 s.output_code(i + 4, extracted, innerbits, innermask)
1154 output(ind, 'return insn;\n')
1158 """Class representing a leaf node in a size decode tree"""
1160 def __init__(self, m, w):
1166 return '{0}{1:08x}'.format(ind, self.mask)
1171 def output_code(self, i, extracted, outerbits, outermask):
1172 global decode_function
1175 # If we need to load more bytes, do so now.
1176 if extracted < self.width:
1177 output(ind, 'insn = ', decode_function,
1178 '_load_bytes(ctx, insn, {0}, {1});\n'
1179 .format(extracted // 8, self.width // 8));
1180 extracted = self.width
1181 output(ind, 'return insn;\n')
1185 def build_size_tree(pats, width, outerbits, outermask):
1188 # Collect the mask of bits that are fixed in this width
1189 innermask = 0xff << (insnwidth - width)
1190 innermask &= ~outermask
1194 innermask &= i.fixedmask
1195 if minwidth is None:
1197 elif minwidth != i.width:
1199 if minwidth < i.width:
1203 return SizeLeaf(innermask, minwidth)
1206 if width < minwidth:
1207 return build_size_tree(pats, width + 8, outerbits, outermask)
1211 pnames.append(p.name + ':' + p.file + ':' + str(p.lineno))
1212 error_with_file(pats[0].file, pats[0].lineno,
1213 'overlapping patterns size {0}:'.format(width), pnames)
1217 fb = i.fixedbits & innermask
1223 fullmask = outermask | innermask
1224 lens = sorted(bins.keys())
1227 return build_size_tree(bins[b], width + 8, b | outerbits, fullmask)
1229 r = SizeTree(innermask, width)
1230 for b, l in bins.items():
1231 s = build_size_tree(l, width, b | outerbits, fullmask)
1232 r.subs.append((b, s))
1234 # end build_size_tree
1237 def prop_size(tree):
1238 """Propagate minimum widths up the decode size tree"""
1240 if isinstance(tree, SizeTree):
1242 for (b, s) in tree.subs:
1243 width = prop_size(s)
1244 if min is None or min > width:
1246 assert min >= tree.width
1258 global translate_scope
1259 global translate_prefix
1266 global decode_function
1267 global variablewidth
1270 decode_scope = 'static '
1272 long_opts = ['decode=', 'translate=', 'output=', 'insnwidth=',
1273 'static-decode=', 'varinsnwidth=']
1275 (opts, args) = getopt.gnu_getopt(sys.argv[1:], 'o:vw:', long_opts)
1276 except getopt.GetoptError as err:
1279 if o in ('-o', '--output'):
1281 elif o == '--decode':
1284 elif o == '--static-decode':
1286 elif o == '--translate':
1287 translate_prefix = a
1288 translate_scope = ''
1289 elif o in ('-w', '--insnwidth', '--varinsnwidth'):
1290 if o == '--varinsnwidth':
1291 variablewidth = True
1294 insntype = 'uint16_t'
1296 elif insnwidth != 32:
1297 error(0, 'cannot handle insns of width', insnwidth)
1299 assert False, 'unhandled option'
1302 error(0, 'missing input file')
1304 toppat = ExcMultiPattern(0)
1306 for filename in args:
1307 input_file = filename
1308 f = open(filename, 'rt', encoding='utf-8')
1309 parse_file(f, toppat)
1312 # We do not want to compute masks for toppat, because those masks
1313 # are used as a starting point for build_tree. For toppat, we must
1314 # insist that decode begins from naught.
1315 for i in toppat.pats:
1319 toppat.prop_format()
1322 for i in toppat.pats:
1324 stree = build_size_tree(toppat.pats, 8, 0, 0)
1328 output_fd = open(output_file, 'wt', encoding='utf-8')
1330 output_fd = io.TextIOWrapper(sys.stdout.buffer,
1331 encoding=sys.stdout.encoding,
1335 for n in sorted(arguments.keys()):
1339 # A single translate function can be invoked for different patterns.
1340 # Make sure that the argument sets are the same, and declare the
1341 # function only once.
1343 # If we're sharing formats, we're likely also sharing trans_* functions,
1344 # but we can't tell which ones. Prevent issues from the compiler by
1345 # suppressing redundant declaration warnings.
1347 output("#pragma GCC diagnostic push\n",
1348 "#pragma GCC diagnostic ignored \"-Wredundant-decls\"\n",
1349 "#ifdef __clang__\n"
1350 "# pragma GCC diagnostic ignored \"-Wtypedef-redefinition\"\n",
1354 for i in allpatterns:
1355 if i.name in out_pats:
1356 p = out_pats[i.name]
1357 if i.base.base != p.base.base:
1358 error(0, i.name, ' has conflicting argument sets')
1361 out_pats[i.name] = i
1365 output("#pragma GCC diagnostic pop\n\n")
1367 for n in sorted(formats.keys()):
1371 output(decode_scope, 'bool ', decode_function,
1372 '(DisasContext *ctx, ', insntype, ' insn)\n{\n')
1376 if len(allpatterns) != 0:
1377 output(i4, 'union {\n')
1378 for n in sorted(arguments.keys()):
1380 output(i4, i4, f.struct_name(), ' f_', f.name, ';\n')
1381 output(i4, '} u;\n\n')
1382 toppat.output_code(4, False, 0, 0)
1384 output(i4, 'return false;\n')
1388 output('\n', decode_scope, insntype, ' ', decode_function,
1389 '_load(DisasContext *ctx)\n{\n',
1390 ' ', insntype, ' insn = 0;\n\n')
1391 stree.output_code(4, 0, 0, 0)
1399 if __name__ == '__main__':