]> git.proxmox.com Git - mirror_qemu.git/blame - scripts/decodetree.py
decodetree: Prefix extract function names with decode_function
[mirror_qemu.git] / scripts / decodetree.py
CommitLineData
568ae7ef
RH
1#!/usr/bin/env python
2# Copyright (c) 2018 Linaro Limited
3#
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.
8#
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.
13#
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/>.
16#
17
18#
19# Generate a decoding tree from a specification file.
3fdbf5d6 20# See the syntax and semantics in docs/devel/decodetree.rst.
568ae7ef
RH
21#
22
568ae7ef
RH
23import os
24import re
25import sys
26import getopt
568ae7ef
RH
27
28insnwidth = 32
29insnmask = 0xffffffff
30fields = {}
31arguments = {}
32formats = {}
33patterns = []
0eff2df4 34allpatterns = []
568ae7ef
RH
35
36translate_prefix = 'trans'
37translate_scope = 'static '
38input_file = ''
39output_file = None
40output_fd = None
41insntype = 'uint32_t'
abd04f92 42decode_function = 'decode'
568ae7ef
RH
43
44re_ident = '[a-zA-Z][a-zA-Z0-9_]*'
45
46
6699ae6a 47def error_with_file(file, lineno, *args):
568ae7ef
RH
48 """Print an error message from file:line and args and exit."""
49 global output_file
50 global output_fd
51
52 if lineno:
6699ae6a 53 r = '{0}:{1}: error:'.format(file, lineno)
568ae7ef 54 elif input_file:
6699ae6a 55 r = '{0}: error:'.format(file)
568ae7ef
RH
56 else:
57 r = 'error:'
58 for a in args:
59 r += ' ' + str(a)
60 r += '\n'
61 sys.stderr.write(r)
62 if output_file and output_fd:
63 output_fd.close()
64 os.remove(output_file)
65 exit(1)
66
6699ae6a
RH
67def error(lineno, *args):
68 error_with_file(input_file, lineno, args)
568ae7ef
RH
69
70def output(*args):
71 global output_fd
72 for a in args:
73 output_fd.write(a)
74
75
651514df 76if sys.version_info >= (3, 4):
568ae7ef
RH
77 re_fullmatch = re.fullmatch
78else:
79 def re_fullmatch(pat, str):
80 return re.match('^' + pat + '$', str)
81
82
83def output_autogen():
84 output('/* This file is autogenerated by scripts/decodetree.py. */\n\n')
85
86
87def str_indent(c):
88 """Return a string with C spaces"""
89 return ' ' * c
90
91
92def str_fields(fields):
93 """Return a string uniquely identifing FIELDS"""
94 r = ''
95 for n in sorted(fields.keys()):
96 r += '_' + n
97 return r[1:]
98
99
100def str_match_bits(bits, mask):
101 """Return a string pretty-printing BITS/MASK"""
102 global insnwidth
103
104 i = 1 << (insnwidth - 1)
105 space = 0x01010100
106 r = ''
107 while i != 0:
108 if i & mask:
109 if i & bits:
110 r += '1'
111 else:
112 r += '0'
113 else:
114 r += '.'
115 if i & space:
116 r += ' '
117 i >>= 1
118 return r
119
120
121def is_pow2(x):
122 """Return true iff X is equal to a power of 2."""
123 return (x & (x - 1)) == 0
124
125
126def ctz(x):
127 """Return the number of times 2 factors into X."""
128 r = 0
129 while ((x >> r) & 1) == 0:
130 r += 1
131 return r
132
133
134def is_contiguous(bits):
135 shift = ctz(bits)
136 if is_pow2((bits >> shift) + 1):
137 return shift
138 else:
139 return -1
140
141
142def eq_fields_for_args(flds_a, flds_b):
143 if len(flds_a) != len(flds_b):
144 return False
145 for k, a in flds_a.items():
146 if k not in flds_b:
147 return False
148 return True
149
150
151def eq_fields_for_fmts(flds_a, flds_b):
152 if len(flds_a) != len(flds_b):
153 return False
154 for k, a in flds_a.items():
155 if k not in flds_b:
156 return False
157 b = flds_b[k]
158 if a.__class__ != b.__class__ or a != b:
159 return False
160 return True
161
162
163class Field:
164 """Class representing a simple instruction field"""
165 def __init__(self, sign, pos, len):
166 self.sign = sign
167 self.pos = pos
168 self.len = len
169 self.mask = ((1 << len) - 1) << pos
170
171 def __str__(self):
172 if self.sign:
173 s = 's'
174 else:
175 s = ''
cbcdf1a9 176 return str(self.pos) + ':' + s + str(self.len)
568ae7ef
RH
177
178 def str_extract(self):
179 if self.sign:
180 extr = 'sextract32'
181 else:
182 extr = 'extract32'
183 return '{0}(insn, {1}, {2})'.format(extr, self.pos, self.len)
184
185 def __eq__(self, other):
186 return self.sign == other.sign and self.sign == other.sign
187
188 def __ne__(self, other):
189 return not self.__eq__(other)
190# end Field
191
192
193class MultiField:
194 """Class representing a compound instruction field"""
195 def __init__(self, subs, mask):
196 self.subs = subs
197 self.sign = subs[0].sign
198 self.mask = mask
199
200 def __str__(self):
201 return str(self.subs)
202
203 def str_extract(self):
204 ret = '0'
205 pos = 0
206 for f in reversed(self.subs):
207 if pos == 0:
208 ret = f.str_extract()
209 else:
210 ret = 'deposit32({0}, {1}, {2}, {3})' \
211 .format(ret, pos, 32 - pos, f.str_extract())
212 pos += f.len
213 return ret
214
215 def __ne__(self, other):
216 if len(self.subs) != len(other.subs):
217 return True
218 for a, b in zip(self.subs, other.subs):
219 if a.__class__ != b.__class__ or a != b:
220 return True
221 return False
222
223 def __eq__(self, other):
224 return not self.__ne__(other)
225# end MultiField
226
227
228class ConstField:
229 """Class representing an argument field with constant value"""
230 def __init__(self, value):
231 self.value = value
232 self.mask = 0
233 self.sign = value < 0
234
235 def __str__(self):
236 return str(self.value)
237
238 def str_extract(self):
239 return str(self.value)
240
241 def __cmp__(self, other):
242 return self.value - other.value
243# end ConstField
244
245
246class FunctionField:
247 """Class representing a field passed through an expander"""
248 def __init__(self, func, base):
249 self.mask = base.mask
250 self.sign = base.sign
251 self.base = base
252 self.func = func
253
254 def __str__(self):
255 return self.func + '(' + str(self.base) + ')'
256
257 def str_extract(self):
258 return self.func + '(' + self.base.str_extract() + ')'
259
260 def __eq__(self, other):
261 return self.func == other.func and self.base == other.base
262
263 def __ne__(self, other):
264 return not self.__eq__(other)
265# end FunctionField
266
267
268class Arguments:
269 """Class representing the extracted fields of a format"""
abd04f92 270 def __init__(self, nm, flds, extern):
568ae7ef 271 self.name = nm
abd04f92 272 self.extern = extern
568ae7ef
RH
273 self.fields = sorted(flds)
274
275 def __str__(self):
276 return self.name + ' ' + str(self.fields)
277
278 def struct_name(self):
279 return 'arg_' + self.name
280
281 def output_def(self):
abd04f92
RH
282 if not self.extern:
283 output('typedef struct {\n')
284 for n in self.fields:
285 output(' int ', n, ';\n')
286 output('} ', self.struct_name(), ';\n\n')
568ae7ef
RH
287# end Arguments
288
289
290class General:
291 """Common code between instruction formats and instruction patterns"""
292 def __init__(self, name, lineno, base, fixb, fixm, udfm, fldm, flds):
293 self.name = name
6699ae6a 294 self.file = input_file
568ae7ef
RH
295 self.lineno = lineno
296 self.base = base
297 self.fixedbits = fixb
298 self.fixedmask = fixm
299 self.undefmask = udfm
300 self.fieldmask = fldm
301 self.fields = flds
302
303 def __str__(self):
0eff2df4 304 return self.name + ' ' + str_match_bits(self.fixedbits, self.fixedmask)
568ae7ef
RH
305
306 def str1(self, i):
307 return str_indent(i) + self.__str__()
308# end General
309
310
311class Format(General):
312 """Class representing an instruction format"""
313
314 def extract_name(self):
71ecf79b
RH
315 global decode_function
316 return decode_function + '_extract_' + self.name
568ae7ef
RH
317
318 def output_extract(self):
319 output('static void ', self.extract_name(), '(',
320 self.base.struct_name(), ' *a, ', insntype, ' insn)\n{\n')
321 for n, f in self.fields.items():
322 output(' a->', n, ' = ', f.str_extract(), ';\n')
323 output('}\n\n')
324# end Format
325
326
327class Pattern(General):
328 """Class representing an instruction pattern"""
329
330 def output_decl(self):
331 global translate_scope
332 global translate_prefix
333 output('typedef ', self.base.base.struct_name(),
334 ' arg_', self.name, ';\n')
76805598 335 output(translate_scope, 'bool ', translate_prefix, '_', self.name,
3a7be554 336 '(DisasContext *ctx, arg_', self.name, ' *a);\n')
568ae7ef
RH
337
338 def output_code(self, i, extracted, outerbits, outermask):
339 global translate_prefix
340 ind = str_indent(i)
341 arg = self.base.base.name
6699ae6a 342 output(ind, '/* ', self.file, ':', str(self.lineno), ' */\n')
568ae7ef
RH
343 if not extracted:
344 output(ind, self.base.extract_name(), '(&u.f_', arg, ', insn);\n')
345 for n, f in self.fields.items():
346 output(ind, 'u.f_', arg, '.', n, ' = ', f.str_extract(), ';\n')
eb6b87fa
RH
347 output(ind, 'if (', translate_prefix, '_', self.name,
348 '(ctx, &u.f_', arg, ')) return true;\n')
568ae7ef
RH
349# end Pattern
350
351
0eff2df4
RH
352class MultiPattern(General):
353 """Class representing an overlapping set of instruction patterns"""
354
355 def __init__(self, lineno, pats, fixb, fixm, udfm):
356 self.file = input_file
357 self.lineno = lineno
358 self.pats = pats
359 self.base = None
360 self.fixedbits = fixb
361 self.fixedmask = fixm
362 self.undefmask = udfm
363
364 def __str__(self):
365 r = "{"
366 for p in self.pats:
367 r = r + ' ' + str(p)
368 return r + "}"
369
370 def output_decl(self):
371 for p in self.pats:
372 p.output_decl()
373
374 def output_code(self, i, extracted, outerbits, outermask):
375 global translate_prefix
376 ind = str_indent(i)
377 for p in self.pats:
378 if outermask != p.fixedmask:
379 innermask = p.fixedmask & ~outermask
380 innerbits = p.fixedbits & ~outermask
381 output(ind, 'if ((insn & ',
382 '0x{0:08x}) == 0x{1:08x}'.format(innermask, innerbits),
383 ') {\n')
384 output(ind, ' /* ',
385 str_match_bits(p.fixedbits, p.fixedmask), ' */\n')
386 p.output_code(i + 4, extracted, p.fixedbits, p.fixedmask)
387 output(ind, '}\n')
388 else:
389 p.output_code(i, extracted, p.fixedbits, p.fixedmask)
390#end MultiPattern
391
392
568ae7ef
RH
393def parse_field(lineno, name, toks):
394 """Parse one instruction field from TOKS at LINENO"""
395 global fields
396 global re_ident
397 global insnwidth
398
399 # A "simple" field will have only one entry;
400 # a "multifield" will have several.
401 subs = []
402 width = 0
403 func = None
404 for t in toks:
405 if re_fullmatch('!function=' + re_ident, t):
406 if func:
407 error(lineno, 'duplicate function')
408 func = t.split('=')
409 func = func[1]
410 continue
411
412 if re_fullmatch('[0-9]+:s[0-9]+', t):
413 # Signed field extract
414 subtoks = t.split(':s')
415 sign = True
416 elif re_fullmatch('[0-9]+:[0-9]+', t):
417 # Unsigned field extract
418 subtoks = t.split(':')
419 sign = False
420 else:
421 error(lineno, 'invalid field token "{0}"'.format(t))
422 po = int(subtoks[0])
423 le = int(subtoks[1])
424 if po + le > insnwidth:
425 error(lineno, 'field {0} too large'.format(t))
426 f = Field(sign, po, le)
427 subs.append(f)
428 width += le
429
430 if width > insnwidth:
431 error(lineno, 'field too large')
432 if len(subs) == 1:
433 f = subs[0]
434 else:
435 mask = 0
436 for s in subs:
437 if mask & s.mask:
438 error(lineno, 'field components overlap')
439 mask |= s.mask
440 f = MultiField(subs, mask)
441 if func:
442 f = FunctionField(func, f)
443
444 if name in fields:
445 error(lineno, 'duplicate field', name)
446 fields[name] = f
447# end parse_field
448
449
450def parse_arguments(lineno, name, toks):
451 """Parse one argument set from TOKS at LINENO"""
452 global arguments
453 global re_ident
454
455 flds = []
abd04f92 456 extern = False
568ae7ef 457 for t in toks:
abd04f92
RH
458 if re_fullmatch('!extern', t):
459 extern = True
460 continue
568ae7ef
RH
461 if not re_fullmatch(re_ident, t):
462 error(lineno, 'invalid argument set token "{0}"'.format(t))
463 if t in flds:
464 error(lineno, 'duplicate argument "{0}"'.format(t))
465 flds.append(t)
466
467 if name in arguments:
468 error(lineno, 'duplicate argument set', name)
abd04f92 469 arguments[name] = Arguments(name, flds, extern)
568ae7ef
RH
470# end parse_arguments
471
472
473def lookup_field(lineno, name):
474 global fields
475 if name in fields:
476 return fields[name]
477 error(lineno, 'undefined field', name)
478
479
480def add_field(lineno, flds, new_name, f):
481 if new_name in flds:
482 error(lineno, 'duplicate field', new_name)
483 flds[new_name] = f
484 return flds
485
486
487def add_field_byname(lineno, flds, new_name, old_name):
488 return add_field(lineno, flds, new_name, lookup_field(lineno, old_name))
489
490
491def infer_argument_set(flds):
492 global arguments
abd04f92 493 global decode_function
568ae7ef
RH
494
495 for arg in arguments.values():
496 if eq_fields_for_args(flds, arg.fields):
497 return arg
498
abd04f92
RH
499 name = decode_function + str(len(arguments))
500 arg = Arguments(name, flds.keys(), False)
568ae7ef
RH
501 arguments[name] = arg
502 return arg
503
504
505def infer_format(arg, fieldmask, flds):
506 global arguments
507 global formats
abd04f92 508 global decode_function
568ae7ef
RH
509
510 const_flds = {}
511 var_flds = {}
512 for n, c in flds.items():
513 if c is ConstField:
514 const_flds[n] = c
515 else:
516 var_flds[n] = c
517
518 # Look for an existing format with the same argument set and fields
519 for fmt in formats.values():
520 if arg and fmt.base != arg:
521 continue
522 if fieldmask != fmt.fieldmask:
523 continue
524 if not eq_fields_for_fmts(flds, fmt.fields):
525 continue
526 return (fmt, const_flds)
527
abd04f92 528 name = decode_function + '_Fmt_' + str(len(formats))
568ae7ef
RH
529 if not arg:
530 arg = infer_argument_set(flds)
531
532 fmt = Format(name, 0, arg, 0, 0, 0, fieldmask, var_flds)
533 formats[name] = fmt
534
535 return (fmt, const_flds)
536# end infer_format
537
538
539def parse_generic(lineno, is_format, name, toks):
540 """Parse one instruction format from TOKS at LINENO"""
541 global fields
542 global arguments
543 global formats
544 global patterns
0eff2df4 545 global allpatterns
568ae7ef
RH
546 global re_ident
547 global insnwidth
548 global insnmask
549
550 fixedmask = 0
551 fixedbits = 0
552 undefmask = 0
553 width = 0
554 flds = {}
555 arg = None
556 fmt = None
557 for t in toks:
558 # '&Foo' gives a format an explcit argument set.
559 if t[0] == '&':
560 tt = t[1:]
561 if arg:
562 error(lineno, 'multiple argument sets')
563 if tt in arguments:
564 arg = arguments[tt]
565 else:
566 error(lineno, 'undefined argument set', t)
567 continue
568
569 # '@Foo' gives a pattern an explicit format.
570 if t[0] == '@':
571 tt = t[1:]
572 if fmt:
573 error(lineno, 'multiple formats')
574 if tt in formats:
575 fmt = formats[tt]
576 else:
577 error(lineno, 'undefined format', t)
578 continue
579
580 # '%Foo' imports a field.
581 if t[0] == '%':
582 tt = t[1:]
583 flds = add_field_byname(lineno, flds, tt, tt)
584 continue
585
586 # 'Foo=%Bar' imports a field with a different name.
587 if re_fullmatch(re_ident + '=%' + re_ident, t):
588 (fname, iname) = t.split('=%')
589 flds = add_field_byname(lineno, flds, fname, iname)
590 continue
591
592 # 'Foo=number' sets an argument field to a constant value
263ac638 593 if re_fullmatch(re_ident + '=[+-]?[0-9]+', t):
568ae7ef
RH
594 (fname, value) = t.split('=')
595 value = int(value)
596 flds = add_field(lineno, flds, fname, ConstField(value))
597 continue
598
599 # Pattern of 0s, 1s, dots and dashes indicate required zeros,
600 # required ones, or dont-cares.
601 if re_fullmatch('[01.-]+', t):
602 shift = len(t)
603 fms = t.replace('0', '1')
604 fms = fms.replace('.', '0')
605 fms = fms.replace('-', '0')
606 fbs = t.replace('.', '0')
607 fbs = fbs.replace('-', '0')
608 ubm = t.replace('1', '0')
609 ubm = ubm.replace('.', '0')
610 ubm = ubm.replace('-', '1')
611 fms = int(fms, 2)
612 fbs = int(fbs, 2)
613 ubm = int(ubm, 2)
614 fixedbits = (fixedbits << shift) | fbs
615 fixedmask = (fixedmask << shift) | fms
616 undefmask = (undefmask << shift) | ubm
617 # Otherwise, fieldname:fieldwidth
618 elif re_fullmatch(re_ident + ':s?[0-9]+', t):
619 (fname, flen) = t.split(':')
620 sign = False
621 if flen[0] == 's':
622 sign = True
623 flen = flen[1:]
624 shift = int(flen, 10)
625 f = Field(sign, insnwidth - width - shift, shift)
626 flds = add_field(lineno, flds, fname, f)
627 fixedbits <<= shift
628 fixedmask <<= shift
629 undefmask <<= shift
630 else:
631 error(lineno, 'invalid token "{0}"'.format(t))
632 width += shift
633
634 # We should have filled in all of the bits of the instruction.
635 if not (is_format and width == 0) and width != insnwidth:
636 error(lineno, 'definition has {0} bits'.format(width))
637
638 # Do not check for fields overlaping fields; one valid usage
639 # is to be able to duplicate fields via import.
640 fieldmask = 0
641 for f in flds.values():
642 fieldmask |= f.mask
643
644 # Fix up what we've parsed to match either a format or a pattern.
645 if is_format:
646 # Formats cannot reference formats.
647 if fmt:
648 error(lineno, 'format referencing format')
649 # If an argument set is given, then there should be no fields
650 # without a place to store it.
651 if arg:
652 for f in flds.keys():
653 if f not in arg.fields:
654 error(lineno, 'field {0} not in argument set {1}'
655 .format(f, arg.name))
656 else:
657 arg = infer_argument_set(flds)
658 if name in formats:
659 error(lineno, 'duplicate format name', name)
660 fmt = Format(name, lineno, arg, fixedbits, fixedmask,
661 undefmask, fieldmask, flds)
662 formats[name] = fmt
663 else:
664 # Patterns can reference a format ...
665 if fmt:
666 # ... but not an argument simultaneously
667 if arg:
668 error(lineno, 'pattern specifies both format and argument set')
669 if fixedmask & fmt.fixedmask:
670 error(lineno, 'pattern fixed bits overlap format fixed bits')
671 fieldmask |= fmt.fieldmask
672 fixedbits |= fmt.fixedbits
673 fixedmask |= fmt.fixedmask
674 undefmask |= fmt.undefmask
675 else:
676 (fmt, flds) = infer_format(arg, fieldmask, flds)
677 arg = fmt.base
678 for f in flds.keys():
679 if f not in arg.fields:
680 error(lineno, 'field {0} not in argument set {1}'
681 .format(f, arg.name))
682 if f in fmt.fields.keys():
683 error(lineno, 'field {0} set by format and pattern'.format(f))
684 for f in arg.fields:
685 if f not in flds.keys() and f not in fmt.fields.keys():
686 error(lineno, 'field {0} not initialized'.format(f))
687 pat = Pattern(name, lineno, fmt, fixedbits, fixedmask,
688 undefmask, fieldmask, flds)
689 patterns.append(pat)
0eff2df4 690 allpatterns.append(pat)
568ae7ef
RH
691
692 # Validate the masks that we have assembled.
693 if fieldmask & fixedmask:
694 error(lineno, 'fieldmask overlaps fixedmask (0x{0:08x} & 0x{1:08x})'
695 .format(fieldmask, fixedmask))
696 if fieldmask & undefmask:
697 error(lineno, 'fieldmask overlaps undefmask (0x{0:08x} & 0x{1:08x})'
698 .format(fieldmask, undefmask))
699 if fixedmask & undefmask:
700 error(lineno, 'fixedmask overlaps undefmask (0x{0:08x} & 0x{1:08x})'
701 .format(fixedmask, undefmask))
702 if not is_format:
703 allbits = fieldmask | fixedmask | undefmask
704 if allbits != insnmask:
705 error(lineno, 'bits left unspecified (0x{0:08x})'
706 .format(allbits ^ insnmask))
707# end parse_general
708
0eff2df4
RH
709def build_multi_pattern(lineno, pats):
710 """Validate the Patterns going into a MultiPattern."""
711 global patterns
712 global insnmask
713
714 if len(pats) < 2:
715 error(lineno, 'less than two patterns within braces')
716
717 fixedmask = insnmask
718 undefmask = insnmask
719
720 # Collect fixed/undefmask for all of the children.
721 # Move the defining lineno back to that of the first child.
722 for p in pats:
723 fixedmask &= p.fixedmask
724 undefmask &= p.undefmask
725 if p.lineno < lineno:
726 lineno = p.lineno
727
728 repeat = True
729 while repeat:
730 if fixedmask == 0:
731 error(lineno, 'no overlap in patterns within braces')
732 fixedbits = None
733 for p in pats:
734 thisbits = p.fixedbits & fixedmask
735 if fixedbits is None:
736 fixedbits = thisbits
737 elif fixedbits != thisbits:
738 fixedmask &= ~(fixedbits ^ thisbits)
739 break
740 else:
741 repeat = False
742
743 mp = MultiPattern(lineno, pats, fixedbits, fixedmask, undefmask)
744 patterns.append(mp)
745# end build_multi_pattern
568ae7ef
RH
746
747def parse_file(f):
748 """Parse all of the patterns within a file"""
749
0eff2df4
RH
750 global patterns
751
568ae7ef
RH
752 # Read all of the lines of the file. Concatenate lines
753 # ending in backslash; discard empty lines and comments.
754 toks = []
755 lineno = 0
0eff2df4
RH
756 nesting = 0
757 saved_pats = []
758
568ae7ef
RH
759 for line in f:
760 lineno += 1
761
0eff2df4
RH
762 # Expand and strip spaces, to find indent.
763 line = line.rstrip()
764 line = line.expandtabs()
765 len1 = len(line)
766 line = line.lstrip()
767 len2 = len(line)
768
568ae7ef
RH
769 # Discard comments
770 end = line.find('#')
771 if end >= 0:
772 line = line[:end]
773
774 t = line.split()
775 if len(toks) != 0:
776 # Next line after continuation
777 toks.extend(t)
568ae7ef 778 else:
0eff2df4
RH
779 # Allow completely blank lines.
780 if len1 == 0:
781 continue
782 indent = len1 - len2
783 # Empty line due to comment.
784 if len(t) == 0:
785 # Indentation must be correct, even for comment lines.
786 if indent != nesting:
787 error(lineno, 'indentation ', indent, ' != ', nesting)
788 continue
789 start_lineno = lineno
568ae7ef
RH
790 toks = t
791
792 # Continuation?
793 if toks[-1] == '\\':
794 toks.pop()
795 continue
796
568ae7ef
RH
797 name = toks[0]
798 del toks[0]
799
0eff2df4
RH
800 # End nesting?
801 if name == '}':
802 if nesting == 0:
803 error(start_lineno, 'mismatched close brace')
804 if len(toks) != 0:
805 error(start_lineno, 'extra tokens after close brace')
806 nesting -= 2
807 if indent != nesting:
808 error(start_lineno, 'indentation ', indent, ' != ', nesting)
809 pats = patterns
810 patterns = saved_pats.pop()
811 build_multi_pattern(lineno, pats)
812 toks = []
813 continue
814
815 # Everything else should have current indentation.
816 if indent != nesting:
817 error(start_lineno, 'indentation ', indent, ' != ', nesting)
818
819 # Start nesting?
820 if name == '{':
821 if len(toks) != 0:
822 error(start_lineno, 'extra tokens after open brace')
823 saved_pats.append(patterns)
824 patterns = []
825 nesting += 2
826 toks = []
827 continue
828
568ae7ef
RH
829 # Determine the type of object needing to be parsed.
830 if name[0] == '%':
0eff2df4 831 parse_field(start_lineno, name[1:], toks)
568ae7ef 832 elif name[0] == '&':
0eff2df4 833 parse_arguments(start_lineno, name[1:], toks)
568ae7ef 834 elif name[0] == '@':
0eff2df4 835 parse_generic(start_lineno, True, name[1:], toks)
568ae7ef 836 else:
0eff2df4 837 parse_generic(start_lineno, False, name, toks)
568ae7ef
RH
838 toks = []
839# end parse_file
840
841
842class Tree:
843 """Class representing a node in a decode tree"""
844
845 def __init__(self, fm, tm):
846 self.fixedmask = fm
847 self.thismask = tm
848 self.subs = []
849 self.base = None
850
851 def str1(self, i):
852 ind = str_indent(i)
853 r = '{0}{1:08x}'.format(ind, self.fixedmask)
854 if self.format:
855 r += ' ' + self.format.name
856 r += ' [\n'
857 for (b, s) in self.subs:
858 r += '{0} {1:08x}:\n'.format(ind, b)
859 r += s.str1(i + 4) + '\n'
860 r += ind + ']'
861 return r
862
863 def __str__(self):
864 return self.str1(0)
865
866 def output_code(self, i, extracted, outerbits, outermask):
867 ind = str_indent(i)
868
869 # If we identified all nodes below have the same format,
870 # extract the fields now.
871 if not extracted and self.base:
872 output(ind, self.base.extract_name(),
873 '(&u.f_', self.base.base.name, ', insn);\n')
874 extracted = True
875
876 # Attempt to aid the compiler in producing compact switch statements.
877 # If the bits in the mask are contiguous, extract them.
878 sh = is_contiguous(self.thismask)
879 if sh > 0:
880 # Propagate SH down into the local functions.
881 def str_switch(b, sh=sh):
882 return '(insn >> {0}) & 0x{1:x}'.format(sh, b >> sh)
883
884 def str_case(b, sh=sh):
885 return '0x{0:x}'.format(b >> sh)
886 else:
887 def str_switch(b):
888 return 'insn & 0x{0:08x}'.format(b)
889
890 def str_case(b):
891 return '0x{0:08x}'.format(b)
892
893 output(ind, 'switch (', str_switch(self.thismask), ') {\n')
894 for b, s in sorted(self.subs):
895 assert (self.thismask & ~s.fixedmask) == 0
896 innermask = outermask | self.thismask
897 innerbits = outerbits | b
898 output(ind, 'case ', str_case(b), ':\n')
899 output(ind, ' /* ',
900 str_match_bits(innerbits, innermask), ' */\n')
901 s.output_code(i + 4, extracted, innerbits, innermask)
eb6b87fa 902 output(ind, ' return false;\n')
568ae7ef 903 output(ind, '}\n')
568ae7ef
RH
904# end Tree
905
906
907def build_tree(pats, outerbits, outermask):
908 # Find the intersection of all remaining fixedmask.
9b3186e3 909 innermask = ~outermask & insnmask
568ae7ef
RH
910 for i in pats:
911 innermask &= i.fixedmask
912
913 if innermask == 0:
0eff2df4 914 text = 'overlapping patterns:'
568ae7ef 915 for p in pats:
0eff2df4
RH
916 text += '\n' + p.file + ':' + str(p.lineno) + ': ' + str(p)
917 error_with_file(pats[0].file, pats[0].lineno, text)
568ae7ef
RH
918
919 fullmask = outermask | innermask
920
921 # Sort each element of pats into the bin selected by the mask.
922 bins = {}
923 for i in pats:
924 fb = i.fixedbits & innermask
925 if fb in bins:
926 bins[fb].append(i)
927 else:
928 bins[fb] = [i]
929
930 # We must recurse if any bin has more than one element or if
931 # the single element in the bin has not been fully matched.
932 t = Tree(fullmask, innermask)
933
934 for b, l in bins.items():
935 s = l[0]
936 if len(l) > 1 or s.fixedmask & ~fullmask != 0:
937 s = build_tree(l, b | outerbits, fullmask)
938 t.subs.append((b, s))
939
940 return t
941# end build_tree
942
943
944def prop_format(tree):
945 """Propagate Format objects into the decode tree"""
946
947 # Depth first search.
948 for (b, s) in tree.subs:
949 if isinstance(s, Tree):
950 prop_format(s)
951
952 # If all entries in SUBS have the same format, then
953 # propagate that into the tree.
954 f = None
955 for (b, s) in tree.subs:
956 if f is None:
957 f = s.base
958 if f is None:
959 return
960 if f is not s.base:
961 return
962 tree.base = f
963# end prop_format
964
965
966def main():
967 global arguments
968 global formats
969 global patterns
0eff2df4 970 global allpatterns
568ae7ef
RH
971 global translate_scope
972 global translate_prefix
973 global output_fd
974 global output_file
975 global input_file
976 global insnwidth
977 global insntype
83d7c40c 978 global insnmask
abd04f92 979 global decode_function
568ae7ef 980
568ae7ef
RH
981 decode_scope = 'static '
982
cd3e7fc1
RH
983 long_opts = ['decode=', 'translate=', 'output=', 'insnwidth=',
984 'static-decode=']
568ae7ef
RH
985 try:
986 (opts, args) = getopt.getopt(sys.argv[1:], 'o:w:', long_opts)
987 except getopt.GetoptError as err:
988 error(0, err)
989 for o, a in opts:
990 if o in ('-o', '--output'):
991 output_file = a
992 elif o == '--decode':
993 decode_function = a
994 decode_scope = ''
cd3e7fc1
RH
995 elif o == '--static-decode':
996 decode_function = a
568ae7ef
RH
997 elif o == '--translate':
998 translate_prefix = a
999 translate_scope = ''
1000 elif o in ('-w', '--insnwidth'):
1001 insnwidth = int(a)
1002 if insnwidth == 16:
1003 insntype = 'uint16_t'
1004 insnmask = 0xffff
1005 elif insnwidth != 32:
1006 error(0, 'cannot handle insns of width', insnwidth)
1007 else:
1008 assert False, 'unhandled option'
1009
1010 if len(args) < 1:
1011 error(0, 'missing input file')
6699ae6a
RH
1012 for filename in args:
1013 input_file = filename
1014 f = open(filename, 'r')
1015 parse_file(f)
1016 f.close()
568ae7ef
RH
1017
1018 t = build_tree(patterns, 0, 0)
1019 prop_format(t)
1020
1021 if output_file:
1022 output_fd = open(output_file, 'w')
1023 else:
1024 output_fd = sys.stdout
1025
1026 output_autogen()
1027 for n in sorted(arguments.keys()):
1028 f = arguments[n]
1029 f.output_def()
1030
1031 # A single translate function can be invoked for different patterns.
1032 # Make sure that the argument sets are the same, and declare the
1033 # function only once.
1034 out_pats = {}
0eff2df4 1035 for i in allpatterns:
568ae7ef
RH
1036 if i.name in out_pats:
1037 p = out_pats[i.name]
1038 if i.base.base != p.base.base:
1039 error(0, i.name, ' has conflicting argument sets')
1040 else:
1041 i.output_decl()
1042 out_pats[i.name] = i
1043 output('\n')
1044
1045 for n in sorted(formats.keys()):
1046 f = formats[n]
1047 f.output_extract()
1048
1049 output(decode_scope, 'bool ', decode_function,
1050 '(DisasContext *ctx, ', insntype, ' insn)\n{\n')
1051
1052 i4 = str_indent(4)
568ae7ef 1053
82bfac1c
RH
1054 if len(allpatterns) != 0:
1055 output(i4, 'union {\n')
1056 for n in sorted(arguments.keys()):
1057 f = arguments[n]
1058 output(i4, i4, f.struct_name(), ' f_', f.name, ';\n')
1059 output(i4, '} u;\n\n')
1060 t.output_code(4, False, 0, 0)
568ae7ef 1061
82bfac1c 1062 output(i4, 'return false;\n')
568ae7ef
RH
1063 output('}\n')
1064
1065 if output_file:
1066 output_fd.close()
1067# end main
1068
1069
1070if __name__ == '__main__':
1071 main()