Lines Matching +full:0 +full:- +full:9 +full:a +full:- +full:f
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # You should have received a copy of the GNU Lesser General Public
19 # Generate a decoding tree from a specification file.
31 insnmask = 0xffffffff
50 re_C_ident = '[a-zA-Z][a-zA-Z0-9_]*'
53 re_arg_ident = '&[a-zA-Z0-9_]*'
54 re_fld_ident = '%[a-zA-Z0-9_]*'
55 re_fmt_ident = '@[a-zA-Z0-9_]*'
56 re_pat_ident = '[a-zA-Z0-9_]*'
58 # Local implementation of a topological sort. We use the same API that
59 # the Python graphlib does, so that when QEMU moves forward to a
68 # create the sorter. graph is a dictionary whose keys are
70 # (That is, if graph contains "A" -> ["B", "C"] then we must output
71 # B and C before A.)
73 # returns a list of all the nodes in sorted order, or raises CycleError
76 # element in the args attribute is a list of nodes which form a
77 # cycle; the first and last element are the same, eg [a, b, c, a]
85 # https://code.activestate.com/recipes/578272-topological-sort/
93 """Topologically sort a graph"""
112 - set(data.keys()))
120 data = {item: (dep - ordered)
127 raise CycleError(f'nodes are in a cycle', list(data.keys()))
137 # For the test suite expected-errors case, don't print the
143 prefix += f'{file}:'
145 prefix += f'{lineno}:'
154 exit(0 if testforerror else 1)
165 for a in args:
166 output_fd.write(a)
174 """Return a string with C spaces"""
179 """Return a string uniquely identifying FIELDS"""
187 """Return a hex string for val padded for insnwidth"""
189 return f'0x{val:0{insnwidth // 4}x}'
193 """Return a hex string for val padded for insnwidth,
194 and with the proper suffix for a C constant."""
196 if val >= 0x100000000:
198 elif val >= 0x80000000:
204 """Return a string pretty-printing BITS/MASK"""
207 i = 1 << (insnwidth - 1)
208 space = 0x01010100
210 while i != 0:
215 r += '0'
225 """Return true iff X is equal to a power of 2."""
226 return (x & (x - 1)) == 0
231 assert x != 0
232 r = 0
233 while ((x >> r) & 1) == 0:
239 if bits == 0:
240 return -1
245 return -1
255 for k, a in flds_a.items():
264 for k, a in flds_a.items():
268 if a.__class__ != b.__class__ or a != b:
274 """Class representing a simple instruction field"""
279 self.mask = ((1 << len) - 1) << pos
291 return f'{s}extract{bitop_width}(insn, {self.pos}, {self.len})'
305 """Class representing a compound instruction field"""
308 self.sign = subs[0].sign
316 ret = '0'
317 pos = 0
318 for f in reversed(self.subs):
319 ext = f.str_extract(lvalue_formatter)
320 if pos == 0:
323 ret = f'deposit{bitop_width}({ret}, {pos}, {bitop_width - pos}, {ext})'
324 pos += f.len
329 for f in self.subs:
330 l.extend(f.referenced_fields())
336 for a, b in zip(self.subs, other.subs):
337 if a.__class__ != b.__class__ or a != b:
350 self.mask = 0
351 self.sign = value < 0
363 return self.value - other.value
368 """Class representing a field passed through a function"""
394 """Class representing a pseudo-field read from a function"""
396 self.mask = 0
397 self.sign = 0
417 """Class representing a field already named in the pattern"""
419 self.mask = 0
431 return f'{s}extract{bitop_width}({lvalue}, 0, {self.len})'
444 """Class representing the extracted fields of a format"""
461 output(f' {t} {n};\n')
487 # Return a list of all named references which aren't satisfied
489 # * a format referring to a field which is specified by the
491 # * a pattern referring to a field which is specified by the
493 # * a user error (referring to a field that doesn't exist at all)
497 for n, f in self.fields.items():
498 for r in f.referenced_fields():
505 # We use a topological sort to ensure that any use of NamedField
508 for n, f in self.fields.items():
509 refs = f.referenced_fields()
518 # a NamedField.
520 f = self.fields[n]
522 f.str_extract(lvalue_formatter), ';\n')
526 # The second element of args is a list of nodes which form
527 # a cycle (there might be others too, but only one is reported).
528 # Pretty-print it to tell the user.
530 error(self.lineno, 'field definitions form a cycle: ' + cycle)
543 self.base.struct_name(), ' *a, ', insntype, ' insn)\n{\n')
544 self.output_fields(str_indent(4), lambda n: 'a->' + n)
558 '(DisasContext *ctx, arg_', self.name, ' *a);\n')
571 # a nonexistent field.
575 error(self.lineno, f'format refers to undefined field {r}')
579 error(self.lineno, f'pattern refers to undefined field {r}')
612 """Class representing a set of instruction patterns"""
619 self.fixedbits = 0
620 self.fixedmask = 0
621 self.undefmask = 0
648 fixedbits = 0
649 while repeat and fixedmask != 0:
697 output(ind, f'if ((insn & {whexC(innermask)}) == {whexC(innerbits)}) {{\n')
698 output(ind, f' /* {str_match_bits(p.fixedbits, p.fixedmask)} */\n')
713 """Class representing a node in a decode tree"""
728 r += ind + f' {whex(b)}:\n'
734 return self.str1(0)
751 if sh > 0:
754 return f'(insn >> {sh}) & {b >> sh:#x}'
760 return f'insn & {whexC(b)}'
767 assert (self.thismask & ~s.fixedmask) == 0
780 """Class representing a non-overlapping set of instruction patterns"""
793 if innermask == 0:
797 t.subs.append((0, pats[0]))
803 error_with_file(pats[0].file, pats[0].lineno, text)
821 s = l[0]
822 if len(l) > 1 or s.fixedmask & ~fullmask != 0:
844 f = None
846 if f is None:
847 f = s.base
848 if f is None:
850 if f is not s.base:
852 tree.base = f
867 # A "simple" field will have only one entry;
868 # a "multifield" will have several.
870 width = 0
880 if re.fullmatch(re_C_ident + ':s[0-9]+', t):
883 n = subtoks[0]
885 f = NamedField(n, True, le)
886 subs.append(f)
889 if re.fullmatch(re_C_ident + ':[0-9]+', t):
892 n = subtoks[0]
894 f = NamedField(n, False, le)
895 subs.append(f)
899 if re.fullmatch('[0-9]+:s[0-9]+', t):
903 elif re.fullmatch('[0-9]+:[0-9]+', t):
908 error(lineno, f'invalid field token "{t}"')
909 po = int(subtoks[0])
912 error(lineno, f'field {t} too large')
913 f = Field(sign, po, le)
914 subs.append(f)
919 if len(subs) == 0:
921 f = ParameterField(func)
926 f = subs[0]
928 mask = 0
933 f = MultiField(subs, mask)
935 f = FunctionField(func, f)
939 fields[name] = f
962 error(lineno, f'invalid argument set token "{n}"')
964 error(lineno, f'duplicate argument "{n}"')
981 def add_field(lineno, flds, new_name, f): argument
984 flds[new_name] = f
1035 fmt = Format(name, 0, arg, 0, 0, 0, fieldmask, var_flds, width)
1058 fixedmask = 0
1059 fixedbits = 0
1060 undefmask = 0
1061 width = 0
1066 # '&Foo' gives a format an explicit argument set.
1077 # '@Foo' gives a pattern an explicit format.
1088 # '%Foo' imports a field.
1094 # 'Foo=%Bar' imports a field with a different name.
1100 # 'Foo=number' sets an argument field to a constant value
1101 if re.fullmatch(re_C_ident + '=[+-]?[0-9]+', t):
1107 # Pattern of 0s, 1s, dots and dashes indicate required zeros,
1108 # required ones, or dont-cares.
1109 if re.fullmatch('[01.-]+', t):
1111 fms = t.replace('0', '1')
1112 fms = fms.replace('.', '0')
1113 fms = fms.replace('-', '0')
1114 fbs = t.replace('.', '0')
1115 fbs = fbs.replace('-', '0')
1116 ubm = t.replace('1', '0')
1117 ubm = ubm.replace('.', '0')
1118 ubm = ubm.replace('-', '1')
1126 elif re.fullmatch(re_C_ident + ':s?[0-9]+', t):
1129 if flen[0] == 's':
1134 error(lineno, f'field {fname} exceeds insnwidth')
1135 f = Field(sign, insnwidth - width - shift, shift)
1136 flds = add_field(lineno, flds, fname, f)
1141 error(lineno, f'invalid token "{t}"')
1144 if variablewidth and width < insnwidth and width % 8 == 0:
1145 shift = insnwidth - width
1149 undefmask |= (1 << shift) - 1
1152 elif not (is_format and width == 0) and width != insnwidth:
1153 error(lineno, f'definition has {width} bits')
1157 fieldmask = 0
1158 for f in flds.values():
1159 fieldmask |= f.mask
1161 # Fix up what we've parsed to match either a format or a pattern.
1167 # without a place to store it.
1169 for f in flds.keys():
1170 if f not in arg.fields:
1171 error(lineno, f'field {f} not in argument set {arg.name}')
1180 # Patterns can reference a format ...
1196 for f in flds.keys():
1197 if f not in arg.fields:
1198 error(lineno, f'field {f} not in argument set {arg.name}')
1199 if f in fmt.fields.keys():
1200 error(lineno, f'field {f} set by format and pattern')
1201 for f in arg.fields:
1202 if f not in flds.keys() and f not in fmt.fields.keys():
1203 error(lineno, f'field {f} not initialized')
1212 f'({whex(fieldmask)} & {whex(fixedmask)})')
1215 f'({whex(fieldmask)} & {whex(undefmask)})')
1218 f'({whex(fixedmask)} & {whex(undefmask)})')
1223 f'({whex(allbits ^ insnmask)})')
1227 def parse_file(f, parent_pat): argument
1228 """Parse all of the patterns within a file"""
1237 lineno = 0
1238 nesting = 0
1241 for line in f:
1253 if end >= 0:
1257 if len(toks) != 0:
1262 if len1 == 0:
1264 indent = len1 - len2
1266 if len(t) == 0:
1275 if toks[-1] == '\\':
1279 name = toks[0]
1280 del toks[0]
1284 if len(toks) != 0:
1296 nesting -= 2
1309 if len(toks) != 0:
1334 error(lineno, f'invalid token "{name}"')
1337 if nesting != 0:
1343 """Class representing a node in a size decode tree"""
1355 r += ind + f' {whex(b)}:\n'
1361 return self.str1(0)
1368 output(ind, f'insn = {decode_function}_load_bytes',
1369 f'(ctx, insn, {extracted // 8}, {self.width // 8});\n')
1375 if sh > 0:
1378 return f'(insn >> {sh}) & {b >> sh:#x}'
1384 return f'insn & {whexC(b)}'
1402 """Class representing a leaf node in a size decode tree"""
1412 return self.str1(0)
1420 output(ind, f'insn = {decode_function}_load_bytes',
1421 f'(ctx, insn, {extracted // 8}, {self.width // 8});\n')
1431 innermask = 0xff << (insnwidth - width)
1447 if innermask == 0:
1454 error_with_file(pats[0].file, pats[0].lineno,
1455 f'overlapping patterns size {width}:', pnames)
1468 b = lens[0]
1518 'static-decode=', 'varinsnwidth=', 'test-for-error',
1519 'output-null']
1523 error(0, err)
1524 for o, a in opts:
1525 if o in ('-o', '--output'):
1526 output_file = a
1527 elif o == '--decode':
1528 decode_function = a
1530 elif o == '--static-decode':
1531 decode_function = a
1532 elif o == '--translate':
1533 translate_prefix = a
1535 elif o in ('-w', '--insnwidth', '--varinsnwidth'):
1536 if o == '--varinsnwidth':
1538 insnwidth = int(a)
1541 insnmask = 0xffff
1544 insnmask = 0xffffffffffffffff
1547 error(0, 'cannot handle insns of width', insnwidth)
1548 elif o == '--test-for-error':
1550 elif o == '--output-null':
1556 error(0, 'missing input file')
1558 toppat = ExcMultiPattern(0)
1562 f = open(filename, 'rt', encoding='utf-8')
1563 parse_file(f, toppat)
1564 f.close()
1567 # are used as a starting point for build_tree. For toppat, we must
1578 stree = build_size_tree(toppat.pats, 8, 0, 0)
1582 output_fd = open(os.devnull, 'wt', encoding='utf-8', errors="ignore")
1584 output_fd = open(output_file, 'wt', encoding='utf-8')
1592 f = arguments[n]
1593 f.output_def()
1595 # A single translate function can be invoked for different patterns.
1604 "#pragma GCC diagnostic ignored \"-Wredundant-decls\"\n",
1606 "# pragma GCC diagnostic ignored \"-Wtypedef-redefinition\"\n",
1614 error(0, i.name, ' has conflicting argument sets')
1624 f = formats[n]
1625 f.output_extract()
1632 if len(allpatterns) != 0:
1635 f = arguments[n]
1636 output(i4, i4, f.struct_name(), ' f_', f.name, ';\n')
1638 toppat.output_code(4, False, 0, 0)
1646 ' ', insntype, ' insn = 0;\n\n')
1647 stree.output_code(4, 0, 0, 0)
1652 exit(1 if testforerror else 0)