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 re_ident
= '[a-zA-Z][a-zA-Z0-9_]*'
48 def error_with_file(file, lineno
, *args
):
49 """Print an error message from file:line and args and exit."""
55 prefix
+= '{0}:'.format(file)
57 prefix
+= '{0}:'.format(lineno
)
60 print(prefix
, end
='error: ', file=sys
.stderr
)
61 print(*args
, file=sys
.stderr
)
63 if output_file
and output_fd
:
65 os
.remove(output_file
)
70 def error(lineno
, *args
):
71 error_with_file(input_file
, lineno
, *args
)
82 output('/* This file is autogenerated by scripts/decodetree.py. */\n\n')
86 """Return a string with C spaces"""
90 def str_fields(fields
):
91 """Return a string uniquely identifing FIELDS"""
93 for n
in sorted(fields
.keys()):
98 def str_match_bits(bits
, mask
):
99 """Return a string pretty-printing BITS/MASK"""
102 i
= 1 << (insnwidth
- 1)
120 """Return true iff X is equal to a power of 2."""
121 return (x
& (x
- 1)) == 0
125 """Return the number of times 2 factors into X."""
128 while ((x
>> r
) & 1) == 0:
133 def is_contiguous(bits
):
137 if is_pow2((bits
>> shift
) + 1):
143 def eq_fields_for_args(flds_a
, flds_b
):
144 if len(flds_a
) != len(flds_b
):
146 for k
, a
in flds_a
.items():
152 def eq_fields_for_fmts(flds_a
, flds_b
):
153 if len(flds_a
) != len(flds_b
):
155 for k
, a
in flds_a
.items():
159 if a
.__class
__ != b
.__class
__ or a
!= b
:
165 """Class representing a simple instruction field"""
166 def __init__(self
, sign
, pos
, len):
170 self
.mask
= ((1 << len) - 1) << pos
177 return str(self
.pos
) + ':' + s
+ str(self
.len)
179 def str_extract(self
):
184 return '{0}(insn, {1}, {2})'.format(extr
, self
.pos
, self
.len)
186 def __eq__(self
, other
):
187 return self
.sign
== other
.sign
and self
.mask
== other
.mask
189 def __ne__(self
, other
):
190 return not self
.__eq
__(other
)
195 """Class representing a compound instruction field"""
196 def __init__(self
, subs
, mask
):
198 self
.sign
= subs
[0].sign
202 return str(self
.subs
)
204 def str_extract(self
):
207 for f
in reversed(self
.subs
):
209 ret
= f
.str_extract()
211 ret
= 'deposit32({0}, {1}, {2}, {3})' \
212 .format(ret
, pos
, 32 - pos
, f
.str_extract())
216 def __ne__(self
, other
):
217 if len(self
.subs
) != len(other
.subs
):
219 for a
, b
in zip(self
.subs
, other
.subs
):
220 if a
.__class
__ != b
.__class
__ or a
!= b
:
224 def __eq__(self
, other
):
225 return not self
.__ne
__(other
)
230 """Class representing an argument field with constant value"""
231 def __init__(self
, value
):
234 self
.sign
= value
< 0
237 return str(self
.value
)
239 def str_extract(self
):
240 return str(self
.value
)
242 def __cmp__(self
, other
):
243 return self
.value
- other
.value
248 """Class representing a field passed through a function"""
249 def __init__(self
, func
, base
):
250 self
.mask
= base
.mask
251 self
.sign
= base
.sign
256 return self
.func
+ '(' + str(self
.base
) + ')'
258 def str_extract(self
):
259 return self
.func
+ '(ctx, ' + self
.base
.str_extract() + ')'
261 def __eq__(self
, other
):
262 return self
.func
== other
.func
and self
.base
== other
.base
264 def __ne__(self
, other
):
265 return not self
.__eq
__(other
)
269 class ParameterField
:
270 """Class representing a pseudo-field read from a function"""
271 def __init__(self
, func
):
279 def str_extract(self
):
280 return self
.func
+ '(ctx)'
282 def __eq__(self
, other
):
283 return self
.func
== other
.func
285 def __ne__(self
, other
):
286 return not self
.__eq
__(other
)
291 """Class representing the extracted fields of a format"""
292 def __init__(self
, nm
, flds
, extern
):
295 self
.fields
= sorted(flds
)
298 return self
.name
+ ' ' + str(self
.fields
)
300 def struct_name(self
):
301 return 'arg_' + self
.name
303 def output_def(self
):
305 output('typedef struct {\n')
306 for n
in self
.fields
:
307 output(' int ', n
, ';\n')
308 output('} ', self
.struct_name(), ';\n\n')
313 """Common code between instruction formats and instruction patterns"""
314 def __init__(self
, name
, lineno
, base
, fixb
, fixm
, udfm
, fldm
, flds
, w
):
316 self
.file = input_file
319 self
.fixedbits
= fixb
320 self
.fixedmask
= fixm
321 self
.undefmask
= udfm
322 self
.fieldmask
= fldm
327 return self
.name
+ ' ' + str_match_bits(self
.fixedbits
, self
.fixedmask
)
330 return str_indent(i
) + self
.__str
__()
334 class Format(General
):
335 """Class representing an instruction format"""
337 def extract_name(self
):
338 global decode_function
339 return decode_function
+ '_extract_' + self
.name
341 def output_extract(self
):
342 output('static void ', self
.extract_name(), '(DisasContext *ctx, ',
343 self
.base
.struct_name(), ' *a, ', insntype
, ' insn)\n{\n')
344 for n
, f
in self
.fields
.items():
345 output(' a->', n
, ' = ', f
.str_extract(), ';\n')
350 class Pattern(General
):
351 """Class representing an instruction pattern"""
353 def output_decl(self
):
354 global translate_scope
355 global translate_prefix
356 output('typedef ', self
.base
.base
.struct_name(),
357 ' arg_', self
.name
, ';\n')
358 output(translate_scope
, 'bool ', translate_prefix
, '_', self
.name
,
359 '(DisasContext *ctx, arg_', self
.name
, ' *a);\n')
361 def output_code(self
, i
, extracted
, outerbits
, outermask
):
362 global translate_prefix
364 arg
= self
.base
.base
.name
365 output(ind
, '/* ', self
.file, ':', str(self
.lineno
), ' */\n')
367 output(ind
, self
.base
.extract_name(),
368 '(ctx, &u.f_', arg
, ', insn);\n')
369 for n
, f
in self
.fields
.items():
370 output(ind
, 'u.f_', arg
, '.', n
, ' = ', f
.str_extract(), ';\n')
371 output(ind
, 'if (', translate_prefix
, '_', self
.name
,
372 '(ctx, &u.f_', arg
, ')) return true;\n')
374 # Normal patterns do not have children.
375 def build_tree(self
):
377 def prop_masks(self
):
379 def prop_format(self
):
381 def prop_width(self
):
387 class MultiPattern(General
):
388 """Class representing a set of instruction patterns"""
390 def __init__(self
, lineno
):
391 self
.file = input_file
402 if self
.fixedbits
is not None:
403 r
+= ' ' + str_match_bits(self
.fixedbits
, self
.fixedmask
)
406 def output_decl(self
):
410 def prop_masks(self
):
416 # Collect fixedmask/undefmask for all of the children.
419 fixedmask
&= p
.fixedmask
420 undefmask
&= p
.undefmask
422 # Widen fixedmask until all fixedbits match
425 while repeat
and fixedmask
!= 0:
428 thisbits
= p
.fixedbits
& fixedmask
429 if fixedbits
is None:
431 elif fixedbits
!= thisbits
:
432 fixedmask
&= ~
(fixedbits ^ thisbits
)
437 self
.fixedbits
= fixedbits
438 self
.fixedmask
= fixedmask
439 self
.undefmask
= undefmask
441 def build_tree(self
):
445 def prop_format(self
):
449 def prop_width(self
):
455 elif width
!= p
.width
:
456 error_with_file(self
.file, self
.lineno
,
457 'width mismatch in patterns within braces')
463 class IncMultiPattern(MultiPattern
):
464 """Class representing an overlapping set of instruction patterns"""
466 def output_code(self
, i
, extracted
, outerbits
, outermask
):
467 global translate_prefix
470 if outermask
!= p
.fixedmask
:
471 innermask
= p
.fixedmask
& ~outermask
472 innerbits
= p
.fixedbits
& ~outermask
473 output(ind
, 'if ((insn & ',
474 '0x{0:08x}) == 0x{1:08x}'.format(innermask
, innerbits
),
477 str_match_bits(p
.fixedbits
, p
.fixedmask
), ' */\n')
478 p
.output_code(i
+ 4, extracted
, p
.fixedbits
, p
.fixedmask
)
481 p
.output_code(i
, extracted
, p
.fixedbits
, p
.fixedmask
)
486 """Class representing a node in a decode tree"""
488 def __init__(self
, fm
, tm
):
496 r
= '{0}{1:08x}'.format(ind
, self
.fixedmask
)
498 r
+= ' ' + self
.format
.name
500 for (b
, s
) in self
.subs
:
501 r
+= '{0} {1:08x}:\n'.format(ind
, b
)
502 r
+= s
.str1(i
+ 4) + '\n'
509 def output_code(self
, i
, extracted
, outerbits
, outermask
):
512 # If we identified all nodes below have the same format,
513 # extract the fields now.
514 if not extracted
and self
.base
:
515 output(ind
, self
.base
.extract_name(),
516 '(ctx, &u.f_', self
.base
.base
.name
, ', insn);\n')
519 # Attempt to aid the compiler in producing compact switch statements.
520 # If the bits in the mask are contiguous, extract them.
521 sh
= is_contiguous(self
.thismask
)
523 # Propagate SH down into the local functions.
524 def str_switch(b
, sh
=sh
):
525 return '(insn >> {0}) & 0x{1:x}'.format(sh
, b
>> sh
)
527 def str_case(b
, sh
=sh
):
528 return '0x{0:x}'.format(b
>> sh
)
531 return 'insn & 0x{0:08x}'.format(b
)
534 return '0x{0:08x}'.format(b
)
536 output(ind
, 'switch (', str_switch(self
.thismask
), ') {\n')
537 for b
, s
in sorted(self
.subs
):
538 assert (self
.thismask
& ~s
.fixedmask
) == 0
539 innermask
= outermask | self
.thismask
540 innerbits
= outerbits | b
541 output(ind
, 'case ', str_case(b
), ':\n')
543 str_match_bits(innerbits
, innermask
), ' */\n')
544 s
.output_code(i
+ 4, extracted
, innerbits
, innermask
)
545 output(ind
, ' return false;\n')
550 class ExcMultiPattern(MultiPattern
):
551 """Class representing a non-overlapping set of instruction patterns"""
553 def output_code(self
, i
, extracted
, outerbits
, outermask
):
554 # Defer everything to our decomposed Tree node
555 self
.tree
.output_code(i
, extracted
, outerbits
, outermask
)
558 def __build_tree(pats
, outerbits
, outermask
):
559 # Find the intersection of all remaining fixedmask.
560 innermask
= ~outermask
& insnmask
562 innermask
&= i
.fixedmask
565 # Edge condition: One pattern covers the entire insnmask
567 t
= Tree(outermask
, innermask
)
568 t
.subs
.append((0, pats
[0]))
571 text
= 'overlapping patterns:'
573 text
+= '\n' + p
.file + ':' + str(p
.lineno
) + ': ' + str(p
)
574 error_with_file(pats
[0].file, pats
[0].lineno
, text
)
576 fullmask
= outermask | innermask
578 # Sort each element of pats into the bin selected by the mask.
581 fb
= i
.fixedbits
& innermask
587 # We must recurse if any bin has more than one element or if
588 # the single element in the bin has not been fully matched.
589 t
= Tree(fullmask
, innermask
)
591 for b
, l
in bins
.items():
593 if len(l
) > 1 or s
.fixedmask
& ~fullmask
!= 0:
594 s
= ExcMultiPattern
.__build
_tree
(l
, b | outerbits
, fullmask
)
595 t
.subs
.append((b
, s
))
599 def build_tree(self
):
600 super().prop_format()
601 self
.tree
= self
.__build
_tree
(self
.pats
, self
.fixedbits
,
605 def __prop_format(tree
):
606 """Propagate Format objects into the decode tree"""
608 # Depth first search.
609 for (b
, s
) in tree
.subs
:
610 if isinstance(s
, Tree
):
611 ExcMultiPattern
.__prop
_format
(s
)
613 # If all entries in SUBS have the same format, then
614 # propagate that into the tree.
616 for (b
, s
) in tree
.subs
:
625 def prop_format(self
):
626 super().prop_format()
627 self
.__prop
_format
(self
.tree
)
629 # end ExcMultiPattern
632 def parse_field(lineno
, name
, toks
):
633 """Parse one instruction field from TOKS at LINENO"""
638 # A "simple" field will have only one entry;
639 # a "multifield" will have several.
644 if re
.fullmatch('!function=' + re_ident
, t
):
646 error(lineno
, 'duplicate function')
651 if re
.fullmatch('[0-9]+:s[0-9]+', t
):
652 # Signed field extract
653 subtoks
= t
.split(':s')
655 elif re
.fullmatch('[0-9]+:[0-9]+', t
):
656 # Unsigned field extract
657 subtoks
= t
.split(':')
660 error(lineno
, 'invalid field token "{0}"'.format(t
))
663 if po
+ le
> insnwidth
:
664 error(lineno
, 'field {0} too large'.format(t
))
665 f
= Field(sign
, po
, le
)
669 if width
> insnwidth
:
670 error(lineno
, 'field too large')
673 f
= ParameterField(func
)
675 error(lineno
, 'field with no value')
683 error(lineno
, 'field components overlap')
685 f
= MultiField(subs
, mask
)
687 f
= FunctionField(func
, f
)
690 error(lineno
, 'duplicate field', name
)
695 def parse_arguments(lineno
, name
, toks
):
696 """Parse one argument set from TOKS at LINENO"""
704 if re
.fullmatch('!extern', t
):
708 if not re
.fullmatch(re_ident
, t
):
709 error(lineno
, 'invalid argument set token "{0}"'.format(t
))
711 error(lineno
, 'duplicate argument "{0}"'.format(t
))
714 if name
in arguments
:
715 error(lineno
, 'duplicate argument set', name
)
716 arguments
[name
] = Arguments(name
, flds
, extern
)
717 # end parse_arguments
720 def lookup_field(lineno
, name
):
724 error(lineno
, 'undefined field', name
)
727 def add_field(lineno
, flds
, new_name
, f
):
729 error(lineno
, 'duplicate field', new_name
)
734 def add_field_byname(lineno
, flds
, new_name
, old_name
):
735 return add_field(lineno
, flds
, new_name
, lookup_field(lineno
, old_name
))
738 def infer_argument_set(flds
):
740 global decode_function
742 for arg
in arguments
.values():
743 if eq_fields_for_args(flds
, arg
.fields
):
746 name
= decode_function
+ str(len(arguments
))
747 arg
= Arguments(name
, flds
.keys(), False)
748 arguments
[name
] = arg
752 def infer_format(arg
, fieldmask
, flds
, width
):
755 global decode_function
759 for n
, c
in flds
.items():
765 # Look for an existing format with the same argument set and fields
766 for fmt
in formats
.values():
767 if arg
and fmt
.base
!= arg
:
769 if fieldmask
!= fmt
.fieldmask
:
771 if width
!= fmt
.width
:
773 if not eq_fields_for_fmts(flds
, fmt
.fields
):
775 return (fmt
, const_flds
)
777 name
= decode_function
+ '_Fmt_' + str(len(formats
))
779 arg
= infer_argument_set(flds
)
781 fmt
= Format(name
, 0, arg
, 0, 0, 0, fieldmask
, var_flds
, width
)
784 return (fmt
, const_flds
)
788 def parse_generic(lineno
, parent_pat
, name
, toks
):
789 """Parse one instruction format from TOKS at LINENO"""
799 is_format
= parent_pat
is None
809 # '&Foo' gives a format an explcit argument set.
813 error(lineno
, 'multiple argument sets')
817 error(lineno
, 'undefined argument set', t
)
820 # '@Foo' gives a pattern an explicit format.
824 error(lineno
, 'multiple formats')
828 error(lineno
, 'undefined format', t
)
831 # '%Foo' imports a field.
834 flds
= add_field_byname(lineno
, flds
, tt
, tt
)
837 # 'Foo=%Bar' imports a field with a different name.
838 if re
.fullmatch(re_ident
+ '=%' + re_ident
, t
):
839 (fname
, iname
) = t
.split('=%')
840 flds
= add_field_byname(lineno
, flds
, fname
, iname
)
843 # 'Foo=number' sets an argument field to a constant value
844 if re
.fullmatch(re_ident
+ '=[+-]?[0-9]+', t
):
845 (fname
, value
) = t
.split('=')
847 flds
= add_field(lineno
, flds
, fname
, ConstField(value
))
850 # Pattern of 0s, 1s, dots and dashes indicate required zeros,
851 # required ones, or dont-cares.
852 if re
.fullmatch('[01.-]+', t
):
854 fms
= t
.replace('0', '1')
855 fms
= fms
.replace('.', '0')
856 fms
= fms
.replace('-', '0')
857 fbs
= t
.replace('.', '0')
858 fbs
= fbs
.replace('-', '0')
859 ubm
= t
.replace('1', '0')
860 ubm
= ubm
.replace('.', '0')
861 ubm
= ubm
.replace('-', '1')
865 fixedbits
= (fixedbits
<< shift
) | fbs
866 fixedmask
= (fixedmask
<< shift
) | fms
867 undefmask
= (undefmask
<< shift
) | ubm
868 # Otherwise, fieldname:fieldwidth
869 elif re
.fullmatch(re_ident
+ ':s?[0-9]+', t
):
870 (fname
, flen
) = t
.split(':')
875 shift
= int(flen
, 10)
876 if shift
+ width
> insnwidth
:
877 error(lineno
, 'field {0} exceeds insnwidth'.format(fname
))
878 f
= Field(sign
, insnwidth
- width
- shift
, shift
)
879 flds
= add_field(lineno
, flds
, fname
, f
)
884 error(lineno
, 'invalid token "{0}"'.format(t
))
887 if variablewidth
and width
< insnwidth
and width
% 8 == 0:
888 shift
= insnwidth
- width
892 undefmask |
= (1 << shift
) - 1
894 # We should have filled in all of the bits of the instruction.
895 elif not (is_format
and width
== 0) and width
!= insnwidth
:
896 error(lineno
, 'definition has {0} bits'.format(width
))
898 # Do not check for fields overlaping fields; one valid usage
899 # is to be able to duplicate fields via import.
901 for f
in flds
.values():
904 # Fix up what we've parsed to match either a format or a pattern.
906 # Formats cannot reference formats.
908 error(lineno
, 'format referencing format')
909 # If an argument set is given, then there should be no fields
910 # without a place to store it.
912 for f
in flds
.keys():
913 if f
not in arg
.fields
:
914 error(lineno
, 'field {0} not in argument set {1}'
915 .format(f
, arg
.name
))
917 arg
= infer_argument_set(flds
)
919 error(lineno
, 'duplicate format name', name
)
920 fmt
= Format(name
, lineno
, arg
, fixedbits
, fixedmask
,
921 undefmask
, fieldmask
, flds
, width
)
924 # Patterns can reference a format ...
926 # ... but not an argument simultaneously
928 error(lineno
, 'pattern specifies both format and argument set')
929 if fixedmask
& fmt
.fixedmask
:
930 error(lineno
, 'pattern fixed bits overlap format fixed bits')
931 if width
!= fmt
.width
:
932 error(lineno
, 'pattern uses format of different width')
933 fieldmask |
= fmt
.fieldmask
934 fixedbits |
= fmt
.fixedbits
935 fixedmask |
= fmt
.fixedmask
936 undefmask |
= fmt
.undefmask
938 (fmt
, flds
) = infer_format(arg
, fieldmask
, flds
, width
)
940 for f
in flds
.keys():
941 if f
not in arg
.fields
:
942 error(lineno
, 'field {0} not in argument set {1}'
943 .format(f
, arg
.name
))
944 if f
in fmt
.fields
.keys():
945 error(lineno
, 'field {0} set by format and pattern'.format(f
))
947 if f
not in flds
.keys() and f
not in fmt
.fields
.keys():
948 error(lineno
, 'field {0} not initialized'.format(f
))
949 pat
= Pattern(name
, lineno
, fmt
, fixedbits
, fixedmask
,
950 undefmask
, fieldmask
, flds
, width
)
951 parent_pat
.pats
.append(pat
)
952 allpatterns
.append(pat
)
954 # Validate the masks that we have assembled.
955 if fieldmask
& fixedmask
:
956 error(lineno
, 'fieldmask overlaps fixedmask (0x{0:08x} & 0x{1:08x})'
957 .format(fieldmask
, fixedmask
))
958 if fieldmask
& undefmask
:
959 error(lineno
, 'fieldmask overlaps undefmask (0x{0:08x} & 0x{1:08x})'
960 .format(fieldmask
, undefmask
))
961 if fixedmask
& undefmask
:
962 error(lineno
, 'fixedmask overlaps undefmask (0x{0:08x} & 0x{1:08x})'
963 .format(fixedmask
, undefmask
))
965 allbits
= fieldmask | fixedmask | undefmask
966 if allbits
!= insnmask
:
967 error(lineno
, 'bits left unspecified (0x{0:08x})'
968 .format(allbits ^ insnmask
))
972 def parse_file(f
, parent_pat
):
973 """Parse all of the patterns within a file"""
975 # Read all of the lines of the file. Concatenate lines
976 # ending in backslash; discard empty lines and comments.
985 # Expand and strip spaces, to find indent.
987 line
= line
.expandtabs()
999 # Next line after continuation
1002 # Allow completely blank lines.
1005 indent
= len1
- len2
1006 # Empty line due to comment.
1008 # Indentation must be correct, even for comment lines.
1009 if indent
!= nesting
:
1010 error(lineno
, 'indentation ', indent
, ' != ', nesting
)
1012 start_lineno
= lineno
1016 if toks
[-1] == '\\':
1024 if name
== '}' or name
== ']':
1026 error(start_lineno
, 'extra tokens after close brace')
1028 # Make sure { } and [ ] nest properly.
1029 if (name
== '}') != isinstance(parent_pat
, IncMultiPattern
):
1030 error(lineno
, 'mismatched close brace')
1033 parent_pat
= nesting_pats
.pop()
1035 error(lineno
, 'extra close brace')
1038 if indent
!= nesting
:
1039 error(lineno
, 'indentation ', indent
, ' != ', nesting
)
1044 # Everything else should have current indentation.
1045 if indent
!= nesting
:
1046 error(start_lineno
, 'indentation ', indent
, ' != ', nesting
)
1049 if name
== '{' or name
== '[':
1051 error(start_lineno
, 'extra tokens after open brace')
1054 nested_pat
= IncMultiPattern(start_lineno
)
1056 nested_pat
= ExcMultiPattern(start_lineno
)
1057 parent_pat
.pats
.append(nested_pat
)
1058 nesting_pats
.append(parent_pat
)
1059 parent_pat
= nested_pat
1065 # Determine the type of object needing to be parsed.
1067 parse_field(start_lineno
, name
[1:], toks
)
1068 elif name
[0] == '&':
1069 parse_arguments(start_lineno
, name
[1:], toks
)
1070 elif name
[0] == '@':
1071 parse_generic(start_lineno
, None, name
[1:], toks
)
1073 parse_generic(start_lineno
, parent_pat
, name
, toks
)
1077 error(lineno
, 'missing close brace')
1082 """Class representing a node in a size decode tree"""
1084 def __init__(self
, m
, w
):
1092 r
= '{0}{1:08x}'.format(ind
, self
.mask
)
1094 for (b
, s
) in self
.subs
:
1095 r
+= '{0} {1:08x}:\n'.format(ind
, b
)
1096 r
+= s
.str1(i
+ 4) + '\n'
1103 def output_code(self
, i
, extracted
, outerbits
, outermask
):
1106 # If we need to load more bytes to test, do so now.
1107 if extracted
< self
.width
:
1108 output(ind
, 'insn = ', decode_function
,
1109 '_load_bytes(ctx, insn, {0}, {1});\n'
1110 .format(extracted
// 8, self
.width
// 8));
1111 extracted
= self
.width
1113 # Attempt to aid the compiler in producing compact switch statements.
1114 # If the bits in the mask are contiguous, extract them.
1115 sh
= is_contiguous(self
.mask
)
1117 # Propagate SH down into the local functions.
1118 def str_switch(b
, sh
=sh
):
1119 return '(insn >> {0}) & 0x{1:x}'.format(sh
, b
>> sh
)
1121 def str_case(b
, sh
=sh
):
1122 return '0x{0:x}'.format(b
>> sh
)
1125 return 'insn & 0x{0:08x}'.format(b
)
1128 return '0x{0:08x}'.format(b
)
1130 output(ind
, 'switch (', str_switch(self
.mask
), ') {\n')
1131 for b
, s
in sorted(self
.subs
):
1132 innermask
= outermask | self
.mask
1133 innerbits
= outerbits | b
1134 output(ind
, 'case ', str_case(b
), ':\n')
1136 str_match_bits(innerbits
, innermask
), ' */\n')
1137 s
.output_code(i
+ 4, extracted
, innerbits
, innermask
)
1139 output(ind
, 'return insn;\n')
1143 """Class representing a leaf node in a size decode tree"""
1145 def __init__(self
, m
, w
):
1151 return '{0}{1:08x}'.format(ind
, self
.mask
)
1156 def output_code(self
, i
, extracted
, outerbits
, outermask
):
1157 global decode_function
1160 # If we need to load more bytes, do so now.
1161 if extracted
< self
.width
:
1162 output(ind
, 'insn = ', decode_function
,
1163 '_load_bytes(ctx, insn, {0}, {1});\n'
1164 .format(extracted
// 8, self
.width
// 8));
1165 extracted
= self
.width
1166 output(ind
, 'return insn;\n')
1170 def build_size_tree(pats
, width
, outerbits
, outermask
):
1173 # Collect the mask of bits that are fixed in this width
1174 innermask
= 0xff << (insnwidth
- width
)
1175 innermask
&= ~outermask
1179 innermask
&= i
.fixedmask
1180 if minwidth
is None:
1182 elif minwidth
!= i
.width
:
1184 if minwidth
< i
.width
:
1188 return SizeLeaf(innermask
, minwidth
)
1191 if width
< minwidth
:
1192 return build_size_tree(pats
, width
+ 8, outerbits
, outermask
)
1196 pnames
.append(p
.name
+ ':' + p
.file + ':' + str(p
.lineno
))
1197 error_with_file(pats
[0].file, pats
[0].lineno
,
1198 'overlapping patterns size {0}:'.format(width
), pnames
)
1202 fb
= i
.fixedbits
& innermask
1208 fullmask
= outermask | innermask
1209 lens
= sorted(bins
.keys())
1212 return build_size_tree(bins
[b
], width
+ 8, b | outerbits
, fullmask
)
1214 r
= SizeTree(innermask
, width
)
1215 for b
, l
in bins
.items():
1216 s
= build_size_tree(l
, width
, b | outerbits
, fullmask
)
1217 r
.subs
.append((b
, s
))
1219 # end build_size_tree
1222 def prop_size(tree
):
1223 """Propagate minimum widths up the decode size tree"""
1225 if isinstance(tree
, SizeTree
):
1227 for (b
, s
) in tree
.subs
:
1228 width
= prop_size(s
)
1229 if min is None or min > width
:
1231 assert min >= tree
.width
1243 global translate_scope
1244 global translate_prefix
1251 global decode_function
1252 global variablewidth
1255 decode_scope
= 'static '
1257 long_opts
= ['decode=', 'translate=', 'output=', 'insnwidth=',
1258 'static-decode=', 'varinsnwidth=']
1260 (opts
, args
) = getopt
.gnu_getopt(sys
.argv
[1:], 'o:vw:', long_opts
)
1261 except getopt
.GetoptError
as err
:
1264 if o
in ('-o', '--output'):
1266 elif o
== '--decode':
1269 elif o
== '--static-decode':
1271 elif o
== '--translate':
1272 translate_prefix
= a
1273 translate_scope
= ''
1274 elif o
in ('-w', '--insnwidth', '--varinsnwidth'):
1275 if o
== '--varinsnwidth':
1276 variablewidth
= True
1279 insntype
= 'uint16_t'
1281 elif insnwidth
!= 32:
1282 error(0, 'cannot handle insns of width', insnwidth
)
1284 assert False, 'unhandled option'
1287 error(0, 'missing input file')
1289 toppat
= ExcMultiPattern(0)
1291 for filename
in args
:
1292 input_file
= filename
1293 f
= open(filename
, 'r')
1294 parse_file(f
, toppat
)
1297 # We do not want to compute masks for toppat, because those masks
1298 # are used as a starting point for build_tree. For toppat, we must
1299 # insist that decode begins from naught.
1300 for i
in toppat
.pats
:
1304 toppat
.prop_format()
1307 for i
in toppat
.pats
:
1309 stree
= build_size_tree(toppat
.pats
, 8, 0, 0)
1313 output_fd
= open(output_file
, 'w')
1315 output_fd
= sys
.stdout
1318 for n
in sorted(arguments
.keys()):
1322 # A single translate function can be invoked for different patterns.
1323 # Make sure that the argument sets are the same, and declare the
1324 # function only once.
1326 # If we're sharing formats, we're likely also sharing trans_* functions,
1327 # but we can't tell which ones. Prevent issues from the compiler by
1328 # suppressing redundant declaration warnings.
1330 output("#pragma GCC diagnostic push\n",
1331 "#pragma GCC diagnostic ignored \"-Wredundant-decls\"\n",
1332 "#ifdef __clang__\n"
1333 "# pragma GCC diagnostic ignored \"-Wtypedef-redefinition\"\n",
1337 for i
in allpatterns
:
1338 if i
.name
in out_pats
:
1339 p
= out_pats
[i
.name
]
1340 if i
.base
.base
!= p
.base
.base
:
1341 error(0, i
.name
, ' has conflicting argument sets')
1344 out_pats
[i
.name
] = i
1348 output("#pragma GCC diagnostic pop\n\n")
1350 for n
in sorted(formats
.keys()):
1354 output(decode_scope
, 'bool ', decode_function
,
1355 '(DisasContext *ctx, ', insntype
, ' insn)\n{\n')
1359 if len(allpatterns
) != 0:
1360 output(i4
, 'union {\n')
1361 for n
in sorted(arguments
.keys()):
1363 output(i4
, i4
, f
.struct_name(), ' f_', f
.name
, ';\n')
1364 output(i4
, '} u;\n\n')
1365 toppat
.output_code(4, False, 0, 0)
1367 output(i4
, 'return false;\n')
1371 output('\n', decode_scope
, insntype
, ' ', decode_function
,
1372 '_load(DisasContext *ctx)\n{\n',
1373 ' ', insntype
, ' insn = 0;\n\n')
1374 stree
.output_code(4, 0, 0, 0)
1382 if __name__
== '__main__':