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.
39 translate_prefix
= 'trans'
40 translate_scope
= 'static '
45 decode_function
= 'decode'
47 # An identifier for C.
48 re_C_ident
= '[a-zA-Z][a-zA-Z0-9_]*'
50 # Identifiers for Arguments, Fields, Formats and Patterns.
51 re_arg_ident
= '&[a-zA-Z0-9_]*'
52 re_fld_ident
= '%[a-zA-Z0-9_]*'
53 re_fmt_ident
= '@[a-zA-Z0-9_]*'
54 re_pat_ident
= '[a-zA-Z0-9_]*'
56 def error_with_file(file, lineno
, *args
):
57 """Print an error message from file:line and args and exit."""
65 prefix
+= f
'{lineno}:'
68 print(prefix
, end
='error: ', file=sys
.stderr
)
69 print(*args
, file=sys
.stderr
)
71 if output_file
and output_fd
:
73 os
.remove(output_file
)
78 def error(lineno
, *args
):
79 error_with_file(input_file
, lineno
, *args
)
90 output('/* This file is autogenerated by scripts/decodetree.py. */\n\n')
94 """Return a string with C spaces"""
98 def str_fields(fields
):
99 """Return a string uniquely identifying FIELDS"""
101 for n
in sorted(fields
.keys()):
107 """Return a hex string for val padded for insnwidth"""
109 return f
'0x{val:0{insnwidth // 4}x}'
113 """Return a hex string for val padded for insnwidth,
114 and with the proper suffix for a C constant."""
116 if val
>= 0x100000000:
118 elif val
>= 0x80000000:
120 return whex(val
) + suffix
123 def str_match_bits(bits
, mask
):
124 """Return a string pretty-printing BITS/MASK"""
127 i
= 1 << (insnwidth
- 1)
145 """Return true iff X is equal to a power of 2."""
146 return (x
& (x
- 1)) == 0
150 """Return the number of times 2 factors into X."""
153 while ((x
>> r
) & 1) == 0:
158 def is_contiguous(bits
):
162 if is_pow2((bits
>> shift
) + 1):
168 def eq_fields_for_args(flds_a
, arg
):
169 if len(flds_a
) != len(arg
.fields
):
171 # Only allow inference on default types
175 for k
, a
in flds_a
.items():
176 if k
not in arg
.fields
:
181 def eq_fields_for_fmts(flds_a
, flds_b
):
182 if len(flds_a
) != len(flds_b
):
184 for k
, a
in flds_a
.items():
188 if a
.__class
__ != b
.__class
__ or a
!= b
:
194 """Class representing a simple instruction field"""
195 def __init__(self
, sign
, pos
, len):
199 self
.mask
= ((1 << len) - 1) << pos
206 return str(self
.pos
) + ':' + s
+ str(self
.len)
208 def str_extract(self
):
210 s
= 's' if self
.sign
else ''
211 return f
'{s}extract{bitop_width}(insn, {self.pos}, {self.len})'
213 def __eq__(self
, other
):
214 return self
.sign
== other
.sign
and self
.mask
== other
.mask
216 def __ne__(self
, other
):
217 return not self
.__eq
__(other
)
222 """Class representing a compound instruction field"""
223 def __init__(self
, subs
, mask
):
225 self
.sign
= subs
[0].sign
229 return str(self
.subs
)
231 def str_extract(self
):
235 for f
in reversed(self
.subs
):
236 ext
= f
.str_extract()
240 ret
= f
'deposit{bitop_width}({ret}, {pos}, {bitop_width - pos}, {ext})'
244 def __ne__(self
, other
):
245 if len(self
.subs
) != len(other
.subs
):
247 for a
, b
in zip(self
.subs
, other
.subs
):
248 if a
.__class
__ != b
.__class
__ or a
!= b
:
252 def __eq__(self
, other
):
253 return not self
.__ne
__(other
)
258 """Class representing an argument field with constant value"""
259 def __init__(self
, value
):
262 self
.sign
= value
< 0
265 return str(self
.value
)
267 def str_extract(self
):
268 return str(self
.value
)
270 def __cmp__(self
, other
):
271 return self
.value
- other
.value
276 """Class representing a field passed through a function"""
277 def __init__(self
, func
, base
):
278 self
.mask
= base
.mask
279 self
.sign
= base
.sign
284 return self
.func
+ '(' + str(self
.base
) + ')'
286 def str_extract(self
):
287 return self
.func
+ '(ctx, ' + self
.base
.str_extract() + ')'
289 def __eq__(self
, other
):
290 return self
.func
== other
.func
and self
.base
== other
.base
292 def __ne__(self
, other
):
293 return not self
.__eq
__(other
)
297 class ParameterField
:
298 """Class representing a pseudo-field read from a function"""
299 def __init__(self
, func
):
307 def str_extract(self
):
308 return self
.func
+ '(ctx)'
310 def __eq__(self
, other
):
311 return self
.func
== other
.func
313 def __ne__(self
, other
):
314 return not self
.__eq
__(other
)
319 """Class representing the extracted fields of a format"""
320 def __init__(self
, nm
, flds
, types
, extern
):
327 return self
.name
+ ' ' + str(self
.fields
)
329 def struct_name(self
):
330 return 'arg_' + self
.name
332 def output_def(self
):
334 output('typedef struct {\n')
335 for (n
, t
) in zip(self
.fields
, self
.types
):
336 output(f
' {t} {n};\n')
337 output('} ', self
.struct_name(), ';\n\n')
342 """Common code between instruction formats and instruction patterns"""
343 def __init__(self
, name
, lineno
, base
, fixb
, fixm
, udfm
, fldm
, flds
, w
):
345 self
.file = input_file
348 self
.fixedbits
= fixb
349 self
.fixedmask
= fixm
350 self
.undefmask
= udfm
351 self
.fieldmask
= fldm
356 return self
.name
+ ' ' + str_match_bits(self
.fixedbits
, self
.fixedmask
)
359 return str_indent(i
) + self
.__str
__()
363 class Format(General
):
364 """Class representing an instruction format"""
366 def extract_name(self
):
367 global decode_function
368 return decode_function
+ '_extract_' + self
.name
370 def output_extract(self
):
371 output('static void ', self
.extract_name(), '(DisasContext *ctx, ',
372 self
.base
.struct_name(), ' *a, ', insntype
, ' insn)\n{\n')
373 for n
, f
in self
.fields
.items():
374 output(' a->', n
, ' = ', f
.str_extract(), ';\n')
379 class Pattern(General
):
380 """Class representing an instruction pattern"""
382 def output_decl(self
):
383 global translate_scope
384 global translate_prefix
385 output('typedef ', self
.base
.base
.struct_name(),
386 ' arg_', self
.name
, ';\n')
387 output(translate_scope
, 'bool ', translate_prefix
, '_', self
.name
,
388 '(DisasContext *ctx, arg_', self
.name
, ' *a);\n')
390 def output_code(self
, i
, extracted
, outerbits
, outermask
):
391 global translate_prefix
393 arg
= self
.base
.base
.name
394 output(ind
, '/* ', self
.file, ':', str(self
.lineno
), ' */\n')
396 output(ind
, self
.base
.extract_name(),
397 '(ctx, &u.f_', arg
, ', insn);\n')
398 for n
, f
in self
.fields
.items():
399 output(ind
, 'u.f_', arg
, '.', n
, ' = ', f
.str_extract(), ';\n')
400 output(ind
, 'if (', translate_prefix
, '_', self
.name
,
401 '(ctx, &u.f_', arg
, ')) return true;\n')
403 # Normal patterns do not have children.
404 def build_tree(self
):
406 def prop_masks(self
):
408 def prop_format(self
):
410 def prop_width(self
):
416 class MultiPattern(General
):
417 """Class representing a set of instruction patterns"""
419 def __init__(self
, lineno
):
420 self
.file = input_file
431 if self
.fixedbits
is not None:
432 r
+= ' ' + str_match_bits(self
.fixedbits
, self
.fixedmask
)
435 def output_decl(self
):
439 def prop_masks(self
):
445 # Collect fixedmask/undefmask for all of the children.
448 fixedmask
&= p
.fixedmask
449 undefmask
&= p
.undefmask
451 # Widen fixedmask until all fixedbits match
454 while repeat
and fixedmask
!= 0:
457 thisbits
= p
.fixedbits
& fixedmask
458 if fixedbits
is None:
460 elif fixedbits
!= thisbits
:
461 fixedmask
&= ~
(fixedbits ^ thisbits
)
466 self
.fixedbits
= fixedbits
467 self
.fixedmask
= fixedmask
468 self
.undefmask
= undefmask
470 def build_tree(self
):
474 def prop_format(self
):
478 def prop_width(self
):
484 elif width
!= p
.width
:
485 error_with_file(self
.file, self
.lineno
,
486 'width mismatch in patterns within braces')
492 class IncMultiPattern(MultiPattern
):
493 """Class representing an overlapping set of instruction patterns"""
495 def output_code(self
, i
, extracted
, outerbits
, outermask
):
496 global translate_prefix
499 if outermask
!= p
.fixedmask
:
500 innermask
= p
.fixedmask
& ~outermask
501 innerbits
= p
.fixedbits
& ~outermask
502 output(ind
, f
'if ((insn & {whexC(innermask)}) == {whexC(innerbits)}) {{\n')
503 output(ind
, f
' /* {str_match_bits(p.fixedbits, p.fixedmask)} */\n')
504 p
.output_code(i
+ 4, extracted
, p
.fixedbits
, p
.fixedmask
)
507 p
.output_code(i
, extracted
, p
.fixedbits
, p
.fixedmask
)
512 """Class representing a node in a decode tree"""
514 def __init__(self
, fm
, tm
):
522 r
= ind
+ whex(self
.fixedmask
)
524 r
+= ' ' + self
.format
.name
526 for (b
, s
) in self
.subs
:
527 r
+= ind
+ f
' {whex(b)}:\n'
528 r
+= s
.str1(i
+ 4) + '\n'
535 def output_code(self
, i
, extracted
, outerbits
, outermask
):
538 # If we identified all nodes below have the same format,
539 # extract the fields now.
540 if not extracted
and self
.base
:
541 output(ind
, self
.base
.extract_name(),
542 '(ctx, &u.f_', self
.base
.base
.name
, ', insn);\n')
545 # Attempt to aid the compiler in producing compact switch statements.
546 # If the bits in the mask are contiguous, extract them.
547 sh
= is_contiguous(self
.thismask
)
549 # Propagate SH down into the local functions.
550 def str_switch(b
, sh
=sh
):
551 return f
'(insn >> {sh}) & {b >> sh:#x}'
553 def str_case(b
, sh
=sh
):
557 return f
'insn & {whexC(b)}'
562 output(ind
, 'switch (', str_switch(self
.thismask
), ') {\n')
563 for b
, s
in sorted(self
.subs
):
564 assert (self
.thismask
& ~s
.fixedmask
) == 0
565 innermask
= outermask | self
.thismask
566 innerbits
= outerbits | b
567 output(ind
, 'case ', str_case(b
), ':\n')
569 str_match_bits(innerbits
, innermask
), ' */\n')
570 s
.output_code(i
+ 4, extracted
, innerbits
, innermask
)
571 output(ind
, ' break;\n')
576 class ExcMultiPattern(MultiPattern
):
577 """Class representing a non-overlapping set of instruction patterns"""
579 def output_code(self
, i
, extracted
, outerbits
, outermask
):
580 # Defer everything to our decomposed Tree node
581 self
.tree
.output_code(i
, extracted
, outerbits
, outermask
)
584 def __build_tree(pats
, outerbits
, outermask
):
585 # Find the intersection of all remaining fixedmask.
586 innermask
= ~outermask
& insnmask
588 innermask
&= i
.fixedmask
591 # Edge condition: One pattern covers the entire insnmask
593 t
= Tree(outermask
, innermask
)
594 t
.subs
.append((0, pats
[0]))
597 text
= 'overlapping patterns:'
599 text
+= '\n' + p
.file + ':' + str(p
.lineno
) + ': ' + str(p
)
600 error_with_file(pats
[0].file, pats
[0].lineno
, text
)
602 fullmask
= outermask | innermask
604 # Sort each element of pats into the bin selected by the mask.
607 fb
= i
.fixedbits
& innermask
613 # We must recurse if any bin has more than one element or if
614 # the single element in the bin has not been fully matched.
615 t
= Tree(fullmask
, innermask
)
617 for b
, l
in bins
.items():
619 if len(l
) > 1 or s
.fixedmask
& ~fullmask
!= 0:
620 s
= ExcMultiPattern
.__build
_tree
(l
, b | outerbits
, fullmask
)
621 t
.subs
.append((b
, s
))
625 def build_tree(self
):
626 super().prop_format()
627 self
.tree
= self
.__build
_tree
(self
.pats
, self
.fixedbits
,
631 def __prop_format(tree
):
632 """Propagate Format objects into the decode tree"""
634 # Depth first search.
635 for (b
, s
) in tree
.subs
:
636 if isinstance(s
, Tree
):
637 ExcMultiPattern
.__prop
_format
(s
)
639 # If all entries in SUBS have the same format, then
640 # propagate that into the tree.
642 for (b
, s
) in tree
.subs
:
651 def prop_format(self
):
652 super().prop_format()
653 self
.__prop
_format
(self
.tree
)
655 # end ExcMultiPattern
658 def parse_field(lineno
, name
, toks
):
659 """Parse one instruction field from TOKS at LINENO"""
663 # A "simple" field will have only one entry;
664 # a "multifield" will have several.
669 if re
.match('^!function=', t
):
671 error(lineno
, 'duplicate function')
676 if re
.fullmatch('[0-9]+:s[0-9]+', t
):
677 # Signed field extract
678 subtoks
= t
.split(':s')
680 elif re
.fullmatch('[0-9]+:[0-9]+', t
):
681 # Unsigned field extract
682 subtoks
= t
.split(':')
685 error(lineno
, f
'invalid field token "{t}"')
688 if po
+ le
> insnwidth
:
689 error(lineno
, f
'field {t} too large')
690 f
= Field(sign
, po
, le
)
694 if width
> insnwidth
:
695 error(lineno
, 'field too large')
698 f
= ParameterField(func
)
700 error(lineno
, 'field with no value')
708 error(lineno
, 'field components overlap')
710 f
= MultiField(subs
, mask
)
712 f
= FunctionField(func
, f
)
715 error(lineno
, 'duplicate field', name
)
720 def parse_arguments(lineno
, name
, toks
):
721 """Parse one argument set from TOKS at LINENO"""
730 if re
.fullmatch('!extern', n
):
734 if re
.fullmatch(re_C_ident
+ ':' + re_C_ident
, n
):
735 (n
, t
) = n
.split(':')
736 elif re
.fullmatch(re_C_ident
, n
):
739 error(lineno
, f
'invalid argument set token "{n}"')
741 error(lineno
, f
'duplicate argument "{n}"')
745 if name
in arguments
:
746 error(lineno
, 'duplicate argument set', name
)
747 arguments
[name
] = Arguments(name
, flds
, types
, extern
)
748 # end parse_arguments
751 def lookup_field(lineno
, name
):
755 error(lineno
, 'undefined field', name
)
758 def add_field(lineno
, flds
, new_name
, f
):
760 error(lineno
, 'duplicate field', new_name
)
765 def add_field_byname(lineno
, flds
, new_name
, old_name
):
766 return add_field(lineno
, flds
, new_name
, lookup_field(lineno
, old_name
))
769 def infer_argument_set(flds
):
771 global decode_function
773 for arg
in arguments
.values():
774 if eq_fields_for_args(flds
, arg
):
777 name
= decode_function
+ str(len(arguments
))
778 arg
= Arguments(name
, flds
.keys(), ['int'] * len(flds
), False)
779 arguments
[name
] = arg
783 def infer_format(arg
, fieldmask
, flds
, width
):
786 global decode_function
790 for n
, c
in flds
.items():
796 # Look for an existing format with the same argument set and fields
797 for fmt
in formats
.values():
798 if arg
and fmt
.base
!= arg
:
800 if fieldmask
!= fmt
.fieldmask
:
802 if width
!= fmt
.width
:
804 if not eq_fields_for_fmts(flds
, fmt
.fields
):
806 return (fmt
, const_flds
)
808 name
= decode_function
+ '_Fmt_' + str(len(formats
))
810 arg
= infer_argument_set(flds
)
812 fmt
= Format(name
, 0, arg
, 0, 0, 0, fieldmask
, var_flds
, width
)
815 return (fmt
, const_flds
)
819 def parse_generic(lineno
, parent_pat
, name
, toks
):
820 """Parse one instruction format from TOKS at LINENO"""
833 is_format
= parent_pat
is None
843 # '&Foo' gives a format an explicit argument set.
844 if re
.fullmatch(re_arg_ident
, t
):
847 error(lineno
, 'multiple argument sets')
851 error(lineno
, 'undefined argument set', t
)
854 # '@Foo' gives a pattern an explicit format.
855 if re
.fullmatch(re_fmt_ident
, t
):
858 error(lineno
, 'multiple formats')
862 error(lineno
, 'undefined format', t
)
865 # '%Foo' imports a field.
866 if re
.fullmatch(re_fld_ident
, t
):
868 flds
= add_field_byname(lineno
, flds
, tt
, tt
)
871 # 'Foo=%Bar' imports a field with a different name.
872 if re
.fullmatch(re_C_ident
+ '=' + re_fld_ident
, t
):
873 (fname
, iname
) = t
.split('=%')
874 flds
= add_field_byname(lineno
, flds
, fname
, iname
)
877 # 'Foo=number' sets an argument field to a constant value
878 if re
.fullmatch(re_C_ident
+ '=[+-]?[0-9]+', t
):
879 (fname
, value
) = t
.split('=')
881 flds
= add_field(lineno
, flds
, fname
, ConstField(value
))
884 # Pattern of 0s, 1s, dots and dashes indicate required zeros,
885 # required ones, or dont-cares.
886 if re
.fullmatch('[01.-]+', t
):
888 fms
= t
.replace('0', '1')
889 fms
= fms
.replace('.', '0')
890 fms
= fms
.replace('-', '0')
891 fbs
= t
.replace('.', '0')
892 fbs
= fbs
.replace('-', '0')
893 ubm
= t
.replace('1', '0')
894 ubm
= ubm
.replace('.', '0')
895 ubm
= ubm
.replace('-', '1')
899 fixedbits
= (fixedbits
<< shift
) | fbs
900 fixedmask
= (fixedmask
<< shift
) | fms
901 undefmask
= (undefmask
<< shift
) | ubm
902 # Otherwise, fieldname:fieldwidth
903 elif re
.fullmatch(re_C_ident
+ ':s?[0-9]+', t
):
904 (fname
, flen
) = t
.split(':')
909 shift
= int(flen
, 10)
910 if shift
+ width
> insnwidth
:
911 error(lineno
, f
'field {fname} exceeds insnwidth')
912 f
= Field(sign
, insnwidth
- width
- shift
, shift
)
913 flds
= add_field(lineno
, flds
, fname
, f
)
918 error(lineno
, f
'invalid token "{t}"')
921 if variablewidth
and width
< insnwidth
and width
% 8 == 0:
922 shift
= insnwidth
- width
926 undefmask |
= (1 << shift
) - 1
928 # We should have filled in all of the bits of the instruction.
929 elif not (is_format
and width
== 0) and width
!= insnwidth
:
930 error(lineno
, f
'definition has {width} bits')
932 # Do not check for fields overlapping fields; one valid usage
933 # is to be able to duplicate fields via import.
935 for f
in flds
.values():
938 # Fix up what we've parsed to match either a format or a pattern.
940 # Formats cannot reference formats.
942 error(lineno
, 'format referencing format')
943 # If an argument set is given, then there should be no fields
944 # without a place to store it.
946 for f
in flds
.keys():
947 if f
not in arg
.fields
:
948 error(lineno
, f
'field {f} not in argument set {arg.name}')
950 arg
= infer_argument_set(flds
)
952 error(lineno
, 'duplicate format name', name
)
953 fmt
= Format(name
, lineno
, arg
, fixedbits
, fixedmask
,
954 undefmask
, fieldmask
, flds
, width
)
957 # Patterns can reference a format ...
959 # ... but not an argument simultaneously
961 error(lineno
, 'pattern specifies both format and argument set')
962 if fixedmask
& fmt
.fixedmask
:
963 error(lineno
, 'pattern fixed bits overlap format fixed bits')
964 if width
!= fmt
.width
:
965 error(lineno
, 'pattern uses format of different width')
966 fieldmask |
= fmt
.fieldmask
967 fixedbits |
= fmt
.fixedbits
968 fixedmask |
= fmt
.fixedmask
969 undefmask |
= fmt
.undefmask
971 (fmt
, flds
) = infer_format(arg
, fieldmask
, flds
, width
)
973 for f
in flds
.keys():
974 if f
not in arg
.fields
:
975 error(lineno
, f
'field {f} not in argument set {arg.name}')
976 if f
in fmt
.fields
.keys():
977 error(lineno
, f
'field {f} set by format and pattern')
979 if f
not in flds
.keys() and f
not in fmt
.fields
.keys():
980 error(lineno
, f
'field {f} not initialized')
981 pat
= Pattern(name
, lineno
, fmt
, fixedbits
, fixedmask
,
982 undefmask
, fieldmask
, flds
, width
)
983 parent_pat
.pats
.append(pat
)
984 allpatterns
.append(pat
)
986 # Validate the masks that we have assembled.
987 if fieldmask
& fixedmask
:
988 error(lineno
, 'fieldmask overlaps fixedmask ',
989 f
'({whex(fieldmask)} & {whex(fixedmask)})')
990 if fieldmask
& undefmask
:
991 error(lineno
, 'fieldmask overlaps undefmask ',
992 f
'({whex(fieldmask)} & {whex(undefmask)})')
993 if fixedmask
& undefmask
:
994 error(lineno
, 'fixedmask overlaps undefmask ',
995 f
'({whex(fixedmask)} & {whex(undefmask)})')
997 allbits
= fieldmask | fixedmask | undefmask
998 if allbits
!= insnmask
:
999 error(lineno
, 'bits left unspecified ',
1000 f
'({whex(allbits ^ insnmask)})')
1004 def parse_file(f
, parent_pat
):
1005 """Parse all of the patterns within a file"""
1011 # Read all of the lines of the file. Concatenate lines
1012 # ending in backslash; discard empty lines and comments.
1021 # Expand and strip spaces, to find indent.
1022 line
= line
.rstrip()
1023 line
= line
.expandtabs()
1025 line
= line
.lstrip()
1029 end
= line
.find('#')
1035 # Next line after continuation
1038 # Allow completely blank lines.
1041 indent
= len1
- len2
1042 # Empty line due to comment.
1044 # Indentation must be correct, even for comment lines.
1045 if indent
!= nesting
:
1046 error(lineno
, 'indentation ', indent
, ' != ', nesting
)
1048 start_lineno
= lineno
1052 if toks
[-1] == '\\':
1060 if name
== '}' or name
== ']':
1062 error(start_lineno
, 'extra tokens after close brace')
1064 # Make sure { } and [ ] nest properly.
1065 if (name
== '}') != isinstance(parent_pat
, IncMultiPattern
):
1066 error(lineno
, 'mismatched close brace')
1069 parent_pat
= nesting_pats
.pop()
1071 error(lineno
, 'extra close brace')
1074 if indent
!= nesting
:
1075 error(lineno
, 'indentation ', indent
, ' != ', nesting
)
1080 # Everything else should have current indentation.
1081 if indent
!= nesting
:
1082 error(start_lineno
, 'indentation ', indent
, ' != ', nesting
)
1085 if name
== '{' or name
== '[':
1087 error(start_lineno
, 'extra tokens after open brace')
1090 nested_pat
= IncMultiPattern(start_lineno
)
1092 nested_pat
= ExcMultiPattern(start_lineno
)
1093 parent_pat
.pats
.append(nested_pat
)
1094 nesting_pats
.append(parent_pat
)
1095 parent_pat
= nested_pat
1101 # Determine the type of object needing to be parsed.
1102 if re
.fullmatch(re_fld_ident
, name
):
1103 parse_field(start_lineno
, name
[1:], toks
)
1104 elif re
.fullmatch(re_arg_ident
, name
):
1105 parse_arguments(start_lineno
, name
[1:], toks
)
1106 elif re
.fullmatch(re_fmt_ident
, name
):
1107 parse_generic(start_lineno
, None, name
[1:], toks
)
1108 elif re
.fullmatch(re_pat_ident
, name
):
1109 parse_generic(start_lineno
, parent_pat
, name
, toks
)
1111 error(lineno
, f
'invalid token "{name}"')
1115 error(lineno
, 'missing close brace')
1120 """Class representing a node in a size decode tree"""
1122 def __init__(self
, m
, w
):
1130 r
= ind
+ whex(self
.mask
) + ' [\n'
1131 for (b
, s
) in self
.subs
:
1132 r
+= ind
+ f
' {whex(b)}:\n'
1133 r
+= s
.str1(i
+ 4) + '\n'
1140 def output_code(self
, i
, extracted
, outerbits
, outermask
):
1143 # If we need to load more bytes to test, do so now.
1144 if extracted
< self
.width
:
1145 output(ind
, f
'insn = {decode_function}_load_bytes',
1146 f
'(ctx, insn, {extracted // 8}, {self.width // 8});\n')
1147 extracted
= self
.width
1149 # Attempt to aid the compiler in producing compact switch statements.
1150 # If the bits in the mask are contiguous, extract them.
1151 sh
= is_contiguous(self
.mask
)
1153 # Propagate SH down into the local functions.
1154 def str_switch(b
, sh
=sh
):
1155 return f
'(insn >> {sh}) & {b >> sh:#x}'
1157 def str_case(b
, sh
=sh
):
1161 return f
'insn & {whexC(b)}'
1166 output(ind
, 'switch (', str_switch(self
.mask
), ') {\n')
1167 for b
, s
in sorted(self
.subs
):
1168 innermask
= outermask | self
.mask
1169 innerbits
= outerbits | b
1170 output(ind
, 'case ', str_case(b
), ':\n')
1172 str_match_bits(innerbits
, innermask
), ' */\n')
1173 s
.output_code(i
+ 4, extracted
, innerbits
, innermask
)
1175 output(ind
, 'return insn;\n')
1179 """Class representing a leaf node in a size decode tree"""
1181 def __init__(self
, m
, w
):
1186 return str_indent(i
) + whex(self
.mask
)
1191 def output_code(self
, i
, extracted
, outerbits
, outermask
):
1192 global decode_function
1195 # If we need to load more bytes, do so now.
1196 if extracted
< self
.width
:
1197 output(ind
, f
'insn = {decode_function}_load_bytes',
1198 f
'(ctx, insn, {extracted // 8}, {self.width // 8});\n')
1199 extracted
= self
.width
1200 output(ind
, 'return insn;\n')
1204 def build_size_tree(pats
, width
, outerbits
, outermask
):
1207 # Collect the mask of bits that are fixed in this width
1208 innermask
= 0xff << (insnwidth
- width
)
1209 innermask
&= ~outermask
1213 innermask
&= i
.fixedmask
1214 if minwidth
is None:
1216 elif minwidth
!= i
.width
:
1218 if minwidth
< i
.width
:
1222 return SizeLeaf(innermask
, minwidth
)
1225 if width
< minwidth
:
1226 return build_size_tree(pats
, width
+ 8, outerbits
, outermask
)
1230 pnames
.append(p
.name
+ ':' + p
.file + ':' + str(p
.lineno
))
1231 error_with_file(pats
[0].file, pats
[0].lineno
,
1232 f
'overlapping patterns size {width}:', pnames
)
1236 fb
= i
.fixedbits
& innermask
1242 fullmask
= outermask | innermask
1243 lens
= sorted(bins
.keys())
1246 return build_size_tree(bins
[b
], width
+ 8, b | outerbits
, fullmask
)
1248 r
= SizeTree(innermask
, width
)
1249 for b
, l
in bins
.items():
1250 s
= build_size_tree(l
, width
, b | outerbits
, fullmask
)
1251 r
.subs
.append((b
, s
))
1253 # end build_size_tree
1256 def prop_size(tree
):
1257 """Propagate minimum widths up the decode size tree"""
1259 if isinstance(tree
, SizeTree
):
1261 for (b
, s
) in tree
.subs
:
1262 width
= prop_size(s
)
1263 if min is None or min > width
:
1265 assert min >= tree
.width
1277 global translate_scope
1278 global translate_prefix
1285 global decode_function
1287 global variablewidth
1290 decode_scope
= 'static '
1292 long_opts
= ['decode=', 'translate=', 'output=', 'insnwidth=',
1293 'static-decode=', 'varinsnwidth=']
1295 (opts
, args
) = getopt
.gnu_getopt(sys
.argv
[1:], 'o:vw:', long_opts
)
1296 except getopt
.GetoptError
as err
:
1299 if o
in ('-o', '--output'):
1301 elif o
== '--decode':
1304 elif o
== '--static-decode':
1306 elif o
== '--translate':
1307 translate_prefix
= a
1308 translate_scope
= ''
1309 elif o
in ('-w', '--insnwidth', '--varinsnwidth'):
1310 if o
== '--varinsnwidth':
1311 variablewidth
= True
1314 insntype
= 'uint16_t'
1316 elif insnwidth
== 64:
1317 insntype
= 'uint64_t'
1318 insnmask
= 0xffffffffffffffff
1320 elif insnwidth
!= 32:
1321 error(0, 'cannot handle insns of width', insnwidth
)
1323 assert False, 'unhandled option'
1326 error(0, 'missing input file')
1328 toppat
= ExcMultiPattern(0)
1330 for filename
in args
:
1331 input_file
= filename
1332 f
= open(filename
, 'rt', encoding
='utf-8')
1333 parse_file(f
, toppat
)
1336 # We do not want to compute masks for toppat, because those masks
1337 # are used as a starting point for build_tree. For toppat, we must
1338 # insist that decode begins from naught.
1339 for i
in toppat
.pats
:
1343 toppat
.prop_format()
1346 for i
in toppat
.pats
:
1348 stree
= build_size_tree(toppat
.pats
, 8, 0, 0)
1352 output_fd
= open(output_file
, 'wt', encoding
='utf-8')
1354 output_fd
= io
.TextIOWrapper(sys
.stdout
.buffer,
1355 encoding
=sys
.stdout
.encoding
,
1359 for n
in sorted(arguments
.keys()):
1363 # A single translate function can be invoked for different patterns.
1364 # Make sure that the argument sets are the same, and declare the
1365 # function only once.
1367 # If we're sharing formats, we're likely also sharing trans_* functions,
1368 # but we can't tell which ones. Prevent issues from the compiler by
1369 # suppressing redundant declaration warnings.
1371 output("#pragma GCC diagnostic push\n",
1372 "#pragma GCC diagnostic ignored \"-Wredundant-decls\"\n",
1373 "#ifdef __clang__\n"
1374 "# pragma GCC diagnostic ignored \"-Wtypedef-redefinition\"\n",
1378 for i
in allpatterns
:
1379 if i
.name
in out_pats
:
1380 p
= out_pats
[i
.name
]
1381 if i
.base
.base
!= p
.base
.base
:
1382 error(0, i
.name
, ' has conflicting argument sets')
1385 out_pats
[i
.name
] = i
1389 output("#pragma GCC diagnostic pop\n\n")
1391 for n
in sorted(formats
.keys()):
1395 output(decode_scope
, 'bool ', decode_function
,
1396 '(DisasContext *ctx, ', insntype
, ' insn)\n{\n')
1400 if len(allpatterns
) != 0:
1401 output(i4
, 'union {\n')
1402 for n
in sorted(arguments
.keys()):
1404 output(i4
, i4
, f
.struct_name(), ' f_', f
.name
, ';\n')
1405 output(i4
, '} u;\n\n')
1406 toppat
.output_code(4, False, 0, 0)
1408 output(i4
, 'return false;\n')
1412 output('\n', decode_scope
, insntype
, ' ', decode_function
,
1413 '_load(DisasContext *ctx)\n{\n',
1414 ' ', insntype
, ' insn = 0;\n\n')
1415 stree
.output_code(4, 0, 0, 0)
1423 if __name__
== '__main__':