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 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.
37 translate_prefix = 'trans'
38 translate_scope = 'static '
43 decode_function = 'decode'
45 # An identifier for C.
46 re_C_ident = '[a-zA-Z][a-zA-Z0-9_]*'
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_]*'
54 def error_with_file(file, lineno, *args):
55 """Print an error message from file:line and args and exit."""
61 prefix += '{0}:'.format(file)
63 prefix += '{0}:'.format(lineno)
66 print(prefix, end='error: ', file=sys.stderr)
67 print(*args, file=sys.stderr)
69 if output_file and output_fd:
71 os.remove(output_file)
76 def error(lineno, *args):
77 error_with_file(input_file, lineno, *args)
88 output('/* This file is autogenerated by scripts/decodetree.py. */\n\n')
92 """Return a string with C spaces"""
96 def str_fields(fields):
97 """Return a string uniquely identifying FIELDS"""
99 for n in sorted(fields.keys()):
104 def str_match_bits(bits, mask):
105 """Return a string pretty-printing BITS/MASK"""
108 i = 1 << (insnwidth - 1)
126 """Return true iff X is equal to a power of 2."""
127 return (x & (x - 1)) == 0
131 """Return the number of times 2 factors into X."""
134 while ((x >> r) & 1) == 0:
139 def is_contiguous(bits):
143 if is_pow2((bits >> shift) + 1):
149 def eq_fields_for_args(flds_a, flds_b):
150 if len(flds_a) != len(flds_b):
152 for k, a in flds_a.items():
158 def eq_fields_for_fmts(flds_a, flds_b):
159 if len(flds_a) != len(flds_b):
161 for k, a in flds_a.items():
165 if a.__class__ != b.__class__ or a != b:
171 """Class representing a simple instruction field"""
172 def __init__(self, sign, pos, len):
176 self.mask = ((1 << len) - 1) << pos
183 return str(self.pos) + ':' + s + str(self.len)
185 def str_extract(self):
190 return '{0}(insn, {1}, {2})'.format(extr, self.pos, self.len)
192 def __eq__(self, other):
193 return self.sign == other.sign and self.mask == other.mask
195 def __ne__(self, other):
196 return not self.__eq__(other)
201 """Class representing a compound instruction field"""
202 def __init__(self, subs, mask):
204 self.sign = subs[0].sign
208 return str(self.subs)
210 def str_extract(self):
213 for f in reversed(self.subs):
215 ret = f.str_extract()
217 ret = 'deposit32({0}, {1}, {2}, {3})' \
218 .format(ret, pos, 32 - pos, f.str_extract())
222 def __ne__(self, other):
223 if len(self.subs) != len(other.subs):
225 for a, b in zip(self.subs, other.subs):
226 if a.__class__ != b.__class__ or a != b:
230 def __eq__(self, other):
231 return not self.__ne__(other)
236 """Class representing an argument field with constant value"""
237 def __init__(self, value):
240 self.sign = value < 0
243 return str(self.value)
245 def str_extract(self):
246 return str(self.value)
248 def __cmp__(self, other):
249 return self.value - other.value
254 """Class representing a field passed through a function"""
255 def __init__(self, func, base):
256 self.mask = base.mask
257 self.sign = base.sign
262 return self.func + '(' + str(self.base) + ')'
264 def str_extract(self):
265 return self.func + '(ctx, ' + self.base.str_extract() + ')'
267 def __eq__(self, other):
268 return self.func == other.func and self.base == other.base
270 def __ne__(self, other):
271 return not self.__eq__(other)
275 class ParameterField:
276 """Class representing a pseudo-field read from a function"""
277 def __init__(self, func):
285 def str_extract(self):
286 return self.func + '(ctx)'
288 def __eq__(self, other):
289 return self.func == other.func
291 def __ne__(self, other):
292 return not self.__eq__(other)
297 """Class representing the extracted fields of a format"""
298 def __init__(self, nm, flds, extern):
301 self.fields = sorted(flds)
304 return self.name + ' ' + str(self.fields)
306 def struct_name(self):
307 return 'arg_' + self.name
309 def output_def(self):
311 output('typedef struct {\n')
312 for n in self.fields:
313 output(' int ', n, ';\n')
314 output('} ', self.struct_name(), ';\n\n')
319 """Common code between instruction formats and instruction patterns"""
320 def __init__(self, name, lineno, base, fixb, fixm, udfm, fldm, flds, w):
322 self.file = input_file
325 self.fixedbits = fixb
326 self.fixedmask = fixm
327 self.undefmask = udfm
328 self.fieldmask = fldm
333 return self.name + ' ' + str_match_bits(self.fixedbits, self.fixedmask)
336 return str_indent(i) + self.__str__()
340 class Format(General):
341 """Class representing an instruction format"""
343 def extract_name(self):
344 global decode_function
345 return decode_function + '_extract_' + self.name
347 def output_extract(self):
348 output('static void ', self.extract_name(), '(DisasContext *ctx, ',
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')
356 class Pattern(General):
357 """Class representing an instruction pattern"""
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')
364 output(translate_scope, 'bool ', translate_prefix, '_', self.name,
365 '(DisasContext *ctx, arg_', self.name, ' *a);\n')
367 def output_code(self, i, extracted, outerbits, outermask):
368 global translate_prefix
370 arg = self.base.base.name
371 output(ind, '/* ', self.file, ':', str(self.lineno), ' */\n')
373 output(ind, self.base.extract_name(),
374 '(ctx, &u.f_', arg, ', insn);\n')
375 for n, f in self.fields.items():
376 output(ind, 'u.f_', arg, '.', n, ' = ', f.str_extract(), ';\n')
377 output(ind, 'if (', translate_prefix, '_', self.name,
378 '(ctx, &u.f_', arg, ')) return true;\n')
380 # Normal patterns do not have children.
381 def build_tree(self):
383 def prop_masks(self):
385 def prop_format(self):
387 def prop_width(self):
393 class MultiPattern(General):
394 """Class representing a set of instruction patterns"""
396 def __init__(self, lineno):
397 self.file = input_file
408 if self.fixedbits is not None:
409 r += ' ' + str_match_bits(self.fixedbits, self.fixedmask)
412 def output_decl(self):
416 def prop_masks(self):
422 # Collect fixedmask/undefmask for all of the children.
425 fixedmask &= p.fixedmask
426 undefmask &= p.undefmask
428 # Widen fixedmask until all fixedbits match
431 while repeat and fixedmask != 0:
434 thisbits = p.fixedbits & fixedmask
435 if fixedbits is None:
437 elif fixedbits != thisbits:
438 fixedmask &= ~(fixedbits ^ thisbits)
443 self.fixedbits = fixedbits
444 self.fixedmask = fixedmask
445 self.undefmask = undefmask
447 def build_tree(self):
451 def prop_format(self):
455 def prop_width(self):
461 elif width != p.width:
462 error_with_file(self.file, self.lineno,
463 'width mismatch in patterns within braces')
469 class IncMultiPattern(MultiPattern):
470 """Class representing an overlapping set of instruction patterns"""
472 def output_code(self, i, extracted, outerbits, outermask):
473 global translate_prefix
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),
483 str_match_bits(p.fixedbits, p.fixedmask), ' */\n')
484 p.output_code(i + 4, extracted, p.fixedbits, p.fixedmask)
487 p.output_code(i, extracted, p.fixedbits, p.fixedmask)
492 """Class representing a node in a decode tree"""
494 def __init__(self, fm, tm):
502 r = '{0}{1:08x}'.format(ind, self.fixedmask)
504 r += ' ' + self.format.name
506 for (b, s) in self.subs:
507 r += '{0} {1:08x}:\n'.format(ind, b)
508 r += s.str1(i + 4) + '\n'
515 def output_code(self, i, extracted, outerbits, outermask):
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')
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)
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)
533 def str_case(b, sh=sh):
534 return '0x{0:x}'.format(b >> sh)
537 return 'insn & 0x{0:08x}'.format(b)
540 return '0x{0:08x}'.format(b)
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')
549 str_match_bits(innerbits, innermask), ' */\n')
550 s.output_code(i + 4, extracted, innerbits, innermask)
551 output(ind, ' return false;\n')
556 class ExcMultiPattern(MultiPattern):
557 """Class representing a non-overlapping set of instruction patterns"""
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)
564 def __build_tree(pats, outerbits, outermask):
565 # Find the intersection of all remaining fixedmask.
566 innermask = ~outermask & insnmask
568 innermask &= i.fixedmask
571 # Edge condition: One pattern covers the entire insnmask
573 t = Tree(outermask, innermask)
574 t.subs.append((0, pats[0]))
577 text = 'overlapping patterns:'
579 text += '\n' + p.file + ':' + str(p.lineno) + ': ' + str(p)
580 error_with_file(pats[0].file, pats[0].lineno, text)
582 fullmask = outermask | innermask
584 # Sort each element of pats into the bin selected by the mask.
587 fb = i.fixedbits & innermask
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)
597 for b, l in bins.items():
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))
605 def build_tree(self):
606 super().prop_format()
607 self.tree = self.__build_tree(self.pats, self.fixedbits,
611 def __prop_format(tree):
612 """Propagate Format objects into the decode tree"""
614 # Depth first search.
615 for (b, s) in tree.subs:
616 if isinstance(s, Tree):
617 ExcMultiPattern.__prop_format(s)
619 # If all entries in SUBS have the same format, then
620 # propagate that into the tree.
622 for (b, s) in tree.subs:
631 def prop_format(self):
632 super().prop_format()
633 self.__prop_format(self.tree)
635 # end ExcMultiPattern
638 def parse_field(lineno, name, toks):
639 """Parse one instruction field from TOKS at LINENO"""
643 # A "simple" field will have only one entry;
644 # a "multifield" will have several.
649 if re.match('^!function=', t):
651 error(lineno, 'duplicate function')
656 if re.fullmatch('[0-9]+:s[0-9]+', t):
657 # Signed field extract
658 subtoks = t.split(':s')
660 elif re.fullmatch('[0-9]+:[0-9]+', t):
661 # Unsigned field extract
662 subtoks = t.split(':')
665 error(lineno, 'invalid field token "{0}"'.format(t))
668 if po + le > insnwidth:
669 error(lineno, 'field {0} too large'.format(t))
670 f = Field(sign, po, le)
674 if width > insnwidth:
675 error(lineno, 'field too large')
678 f = ParameterField(func)
680 error(lineno, 'field with no value')
688 error(lineno, 'field components overlap')
690 f = MultiField(subs, mask)
692 f = FunctionField(func, f)
695 error(lineno, 'duplicate field', name)
700 def parse_arguments(lineno, name, toks):
701 """Parse one argument set from TOKS at LINENO"""
709 if re.fullmatch('!extern', t):
713 if not re.fullmatch(re_C_ident, t):
714 error(lineno, 'invalid argument set token "{0}"'.format(t))
716 error(lineno, 'duplicate argument "{0}"'.format(t))
719 if name in arguments:
720 error(lineno, 'duplicate argument set', name)
721 arguments[name] = Arguments(name, flds, extern)
722 # end parse_arguments
725 def lookup_field(lineno, name):
729 error(lineno, 'undefined field', name)
732 def add_field(lineno, flds, new_name, f):
734 error(lineno, 'duplicate field', new_name)
739 def add_field_byname(lineno, flds, new_name, old_name):
740 return add_field(lineno, flds, new_name, lookup_field(lineno, old_name))
743 def infer_argument_set(flds):
745 global decode_function
747 for arg in arguments.values():
748 if eq_fields_for_args(flds, arg.fields):
751 name = decode_function + str(len(arguments))
752 arg = Arguments(name, flds.keys(), False)
753 arguments[name] = arg
757 def infer_format(arg, fieldmask, flds, width):
760 global decode_function
764 for n, c in flds.items():
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:
774 if fieldmask != fmt.fieldmask:
776 if width != fmt.width:
778 if not eq_fields_for_fmts(flds, fmt.fields):
780 return (fmt, const_flds)
782 name = decode_function + '_Fmt_' + str(len(formats))
784 arg = infer_argument_set(flds)
786 fmt = Format(name, 0, arg, 0, 0, 0, fieldmask, var_flds, width)
789 return (fmt, const_flds)
793 def parse_generic(lineno, parent_pat, name, toks):
794 """Parse one instruction format from TOKS at LINENO"""
807 is_format = parent_pat is None
817 # '&Foo' gives a format an explicit argument set.
818 if re.fullmatch(re_arg_ident, t):
821 error(lineno, 'multiple argument sets')
825 error(lineno, 'undefined argument set', t)
828 # '@Foo' gives a pattern an explicit format.
829 if re.fullmatch(re_fmt_ident, t):
832 error(lineno, 'multiple formats')
836 error(lineno, 'undefined format', t)
839 # '%Foo' imports a field.
840 if re.fullmatch(re_fld_ident, t):
842 flds = add_field_byname(lineno, flds, tt, tt)
845 # 'Foo=%Bar' imports a field with a different name.
846 if re.fullmatch(re_C_ident + '=' + re_fld_ident, t):
847 (fname, iname) = t.split('=%')
848 flds = add_field_byname(lineno, flds, fname, iname)
851 # 'Foo=number' sets an argument field to a constant value
852 if re.fullmatch(re_C_ident + '=[+-]?[0-9]+', t):
853 (fname, value) = t.split('=')
855 flds = add_field(lineno, flds, fname, ConstField(value))
858 # Pattern of 0s, 1s, dots and dashes indicate required zeros,
859 # required ones, or dont-cares.
860 if re.fullmatch('[01.-]+', 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')
873 fixedbits = (fixedbits << shift) | fbs
874 fixedmask = (fixedmask << shift) | fms
875 undefmask = (undefmask << shift) | ubm
876 # Otherwise, fieldname:fieldwidth
877 elif re.fullmatch(re_C_ident + ':s?[0-9]+', t):
878 (fname, flen) = t.split(':')
883 shift = int(flen, 10)
884 if shift + width > insnwidth:
885 error(lineno, 'field {0} exceeds insnwidth'.format(fname))
886 f = Field(sign, insnwidth - width - shift, shift)
887 flds = add_field(lineno, flds, fname, f)
892 error(lineno, 'invalid token "{0}"'.format(t))
895 if variablewidth and width < insnwidth and width % 8 == 0:
896 shift = insnwidth - width
900 undefmask |= (1 << shift) - 1
902 # We should have filled in all of the bits of the instruction.
903 elif not (is_format and width == 0) and width != insnwidth:
904 error(lineno, 'definition has {0} bits'.format(width))
906 # Do not check for fields overlapping fields; one valid usage
907 # is to be able to duplicate fields via import.
909 for f in flds.values():
912 # Fix up what we've parsed to match either a format or a pattern.
914 # Formats cannot reference formats.
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.
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))
925 arg = infer_argument_set(flds)
927 error(lineno, 'duplicate format name', name)
928 fmt = Format(name, lineno, arg, fixedbits, fixedmask,
929 undefmask, fieldmask, flds, width)
932 # Patterns can reference a format ...
934 # ... but not an argument simultaneously
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')
939 if width != fmt.width:
940 error(lineno, 'pattern uses format of different width')
941 fieldmask |= fmt.fieldmask
942 fixedbits |= fmt.fixedbits
943 fixedmask |= fmt.fixedmask
944 undefmask |= fmt.undefmask
946 (fmt, flds) = infer_format(arg, fieldmask, flds, width)
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))
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,
958 undefmask, fieldmask, flds, width)
959 parent_pat.pats.append(pat)
960 allpatterns.append(pat)
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))
973 allbits = fieldmask | fixedmask | undefmask
974 if allbits != insnmask:
975 error(lineno, 'bits left unspecified (0x{0:08x})'
976 .format(allbits ^ insnmask))
980 def parse_file(f, parent_pat):
981 """Parse all of the patterns within a file"""
987 # Read all of the lines of the file. Concatenate lines
988 # ending in backslash; discard empty lines and comments.
997 # Expand and strip spaces, to find indent.
999 line = line.expandtabs()
1001 line = line.lstrip()
1005 end = line.find('#')
1011 # Next line after continuation
1014 # Allow completely blank lines.
1017 indent = len1 - len2
1018 # Empty line due to comment.
1020 # Indentation must be correct, even for comment lines.
1021 if indent != nesting:
1022 error(lineno, 'indentation ', indent, ' != ', nesting)
1024 start_lineno = lineno
1028 if toks[-1] == '\\':
1036 if name == '}' or name == ']':
1038 error(start_lineno, 'extra tokens after close brace')
1040 # Make sure { } and [ ] nest properly.
1041 if (name == '}') != isinstance(parent_pat, IncMultiPattern):
1042 error(lineno, 'mismatched close brace')
1045 parent_pat = nesting_pats.pop()
1047 error(lineno, 'extra close brace')
1050 if indent != nesting:
1051 error(lineno, 'indentation ', indent, ' != ', nesting)
1056 # Everything else should have current indentation.
1057 if indent != nesting:
1058 error(start_lineno, 'indentation ', indent, ' != ', nesting)
1061 if name == '{' or name == '[':
1063 error(start_lineno, 'extra tokens after open brace')
1066 nested_pat = IncMultiPattern(start_lineno)
1068 nested_pat = ExcMultiPattern(start_lineno)
1069 parent_pat.pats.append(nested_pat)
1070 nesting_pats.append(parent_pat)
1071 parent_pat = nested_pat
1077 # Determine the type of object needing to be parsed.
1078 if re.fullmatch(re_fld_ident, name):
1079 parse_field(start_lineno, name[1:], toks)
1080 elif re.fullmatch(re_arg_ident, name):
1081 parse_arguments(start_lineno, name[1:], toks)
1082 elif re.fullmatch(re_fmt_ident, name):
1083 parse_generic(start_lineno, None, name[1:], toks)
1084 elif re.fullmatch(re_pat_ident, name):
1085 parse_generic(start_lineno, parent_pat, name, toks)
1087 error(lineno, 'invalid token "{0}"'.format(name))
1091 error(lineno, 'missing close brace')
1096 """Class representing a node in a size decode tree"""
1098 def __init__(self, m, w):
1106 r = '{0}{1:08x}'.format(ind, self.mask)
1108 for (b, s) in self.subs:
1109 r += '{0} {1:08x}:\n'.format(ind, b)
1110 r += s.str1(i + 4) + '\n'
1117 def output_code(self, i, extracted, outerbits, outermask):
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'
1124 .format(extracted // 8, self.width // 8));
1125 extracted = self.width
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)
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)
1135 def str_case(b, sh=sh):
1136 return '0x{0:x}'.format(b >> sh)
1139 return 'insn & 0x{0:08x}'.format(b)
1142 return '0x{0:08x}'.format(b)
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')
1150 str_match_bits(innerbits, innermask), ' */\n')
1151 s.output_code(i + 4, extracted, innerbits, innermask)
1153 output(ind, 'return insn;\n')
1157 """Class representing a leaf node in a size decode tree"""
1159 def __init__(self, m, w):
1165 return '{0}{1:08x}'.format(ind, self.mask)
1170 def output_code(self, i, extracted, outerbits, outermask):
1171 global decode_function
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'
1178 .format(extracted // 8, self.width // 8));
1179 extracted = self.width
1180 output(ind, 'return insn;\n')
1184 def build_size_tree(pats, width, outerbits, outermask):
1187 # Collect the mask of bits that are fixed in this width
1188 innermask = 0xff << (insnwidth - width)
1189 innermask &= ~outermask
1193 innermask &= i.fixedmask
1194 if minwidth is None:
1196 elif minwidth != i.width:
1198 if minwidth < i.width:
1202 return SizeLeaf(innermask, minwidth)
1205 if width < minwidth:
1206 return build_size_tree(pats, width + 8, outerbits, outermask)
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)
1216 fb = i.fixedbits & innermask
1222 fullmask = outermask | innermask
1223 lens = sorted(bins.keys())
1226 return build_size_tree(bins[b], width + 8, b | outerbits, fullmask)
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))
1233 # end build_size_tree
1236 def prop_size(tree):
1237 """Propagate minimum widths up the decode size tree"""
1239 if isinstance(tree, SizeTree):
1241 for (b, s) in tree.subs:
1242 width = prop_size(s)
1243 if min is None or min > width:
1245 assert min >= tree.width
1257 global translate_scope
1258 global translate_prefix
1265 global decode_function
1266 global variablewidth
1269 decode_scope = 'static '
1271 long_opts = ['decode=', 'translate=', 'output=', 'insnwidth=',
1272 'static-decode=', 'varinsnwidth=']
1274 (opts, args) = getopt.gnu_getopt(sys.argv[1:], 'o:vw:', long_opts)
1275 except getopt.GetoptError as err:
1278 if o in ('-o', '--output'):
1280 elif o == '--decode':
1283 elif o == '--static-decode':
1285 elif o == '--translate':
1286 translate_prefix = a
1287 translate_scope = ''
1288 elif o in ('-w', '--insnwidth', '--varinsnwidth'):
1289 if o == '--varinsnwidth':
1290 variablewidth = True
1293 insntype = 'uint16_t'
1295 elif insnwidth != 32:
1296 error(0, 'cannot handle insns of width', insnwidth)
1298 assert False, 'unhandled option'
1301 error(0, 'missing input file')
1303 toppat = ExcMultiPattern(0)
1305 for filename in args:
1306 input_file = filename
1307 f = open(filename, 'r')
1308 parse_file(f, toppat)
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:
1318 toppat.prop_format()
1321 for i in toppat.pats:
1323 stree = build_size_tree(toppat.pats, 8, 0, 0)
1327 output_fd = open(output_file, 'w')
1329 output_fd = sys.stdout
1332 for n in sorted(arguments.keys()):
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.
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.
1344 output("#pragma GCC diagnostic push\n",
1345 "#pragma GCC diagnostic ignored \"-Wredundant-decls\"\n",
1346 "#ifdef __clang__\n"
1347 "# pragma GCC diagnostic ignored \"-Wtypedef-redefinition\"\n",
1351 for i in allpatterns:
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')
1358 out_pats[i.name] = i
1362 output("#pragma GCC diagnostic pop\n\n")
1364 for n in sorted(formats.keys()):
1368 output(decode_scope, 'bool ', decode_function,
1369 '(DisasContext *ctx, ', insntype, ' insn)\n{\n')
1373 if len(allpatterns) != 0:
1374 output(i4, 'union {\n')
1375 for n in sorted(arguments.keys()):
1377 output(i4, i4, f.struct_name(), ' f_', f.name, ';\n')
1378 output(i4, '} u;\n\n')
1379 toppat.output_code(4, False, 0, 0)
1381 output(i4, 'return false;\n')
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)
1396 if __name__ == '__main__':