Philippe Mathieu-Daudé | 3d004a3 | 2020-01-30 17:32:25 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 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. |
Richard Henderson | 3fdbf5d | 2019-02-23 13:00:10 -0800 | [diff] [blame] | 20 | # See the syntax and semantics in docs/devel/decodetree.rst. |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 21 | # |
| 22 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 23 | import os |
| 24 | import re |
| 25 | import sys |
| 26 | import getopt |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 27 | |
| 28 | insnwidth = 32 |
| 29 | insnmask = 0xffffffff |
Richard Henderson | 17560e9 | 2019-01-30 18:01:29 -0800 | [diff] [blame] | 30 | variablewidth = False |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 31 | fields = {} |
| 32 | arguments = {} |
| 33 | formats = {} |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 34 | allpatterns = [] |
Richard Henderson | c692079 | 2019-08-09 08:12:50 -0700 | [diff] [blame] | 35 | anyextern = False |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 36 | |
| 37 | translate_prefix = 'trans' |
| 38 | translate_scope = 'static ' |
| 39 | input_file = '' |
| 40 | output_file = None |
| 41 | output_fd = None |
| 42 | insntype = 'uint32_t' |
Richard Henderson | abd04f9 | 2018-10-23 10:26:25 +0100 | [diff] [blame] | 43 | decode_function = 'decode' |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 44 | |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 45 | # An identifier for C. |
| 46 | re_C_ident = '[a-zA-Z][a-zA-Z0-9_]*' |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 47 | |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 48 | # Identifiers for Arguments, Fields, Formats and Patterns. |
| 49 | re_arg_ident = '&[a-zA-Z0-9_]*' |
| 50 | re_fld_ident = '%[a-zA-Z0-9_]*' |
| 51 | re_fmt_ident = '@[a-zA-Z0-9_]*' |
| 52 | re_pat_ident = '[a-zA-Z0-9_]*' |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 53 | |
Richard Henderson | 6699ae6 | 2018-10-26 14:59:43 +0100 | [diff] [blame] | 54 | def error_with_file(file, lineno, *args): |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 55 | """Print an error message from file:line and args and exit.""" |
| 56 | global output_file |
| 57 | global output_fd |
| 58 | |
Richard Henderson | 2fd51b1 | 2020-05-15 14:48:54 -0700 | [diff] [blame] | 59 | prefix = '' |
| 60 | if file: |
| 61 | prefix += '{0}:'.format(file) |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 62 | if lineno: |
Richard Henderson | 2fd51b1 | 2020-05-15 14:48:54 -0700 | [diff] [blame] | 63 | prefix += '{0}:'.format(lineno) |
| 64 | if prefix: |
| 65 | prefix += ' ' |
| 66 | print(prefix, end='error: ', file=sys.stderr) |
| 67 | print(*args, file=sys.stderr) |
| 68 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 69 | if output_file and output_fd: |
| 70 | output_fd.close() |
| 71 | os.remove(output_file) |
| 72 | exit(1) |
Richard Henderson | 2fd51b1 | 2020-05-15 14:48:54 -0700 | [diff] [blame] | 73 | # end error_with_file |
| 74 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 75 | |
Richard Henderson | 6699ae6 | 2018-10-26 14:59:43 +0100 | [diff] [blame] | 76 | def error(lineno, *args): |
Richard Henderson | 2fd51b1 | 2020-05-15 14:48:54 -0700 | [diff] [blame] | 77 | error_with_file(input_file, lineno, *args) |
| 78 | # end error |
| 79 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 80 | |
| 81 | def output(*args): |
| 82 | global output_fd |
| 83 | for a in args: |
| 84 | output_fd.write(a) |
| 85 | |
| 86 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 87 | def output_autogen(): |
| 88 | output('/* This file is autogenerated by scripts/decodetree.py. */\n\n') |
| 89 | |
| 90 | |
| 91 | def str_indent(c): |
| 92 | """Return a string with C spaces""" |
| 93 | return ' ' * c |
| 94 | |
| 95 | |
| 96 | def str_fields(fields): |
zhaolichang | 65fdb3c | 2020-09-17 15:50:23 +0800 | [diff] [blame^] | 97 | """Return a string uniquely identifying FIELDS""" |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 98 | r = '' |
| 99 | for n in sorted(fields.keys()): |
| 100 | r += '_' + n |
| 101 | return r[1:] |
| 102 | |
| 103 | |
| 104 | def str_match_bits(bits, mask): |
| 105 | """Return a string pretty-printing BITS/MASK""" |
| 106 | global insnwidth |
| 107 | |
| 108 | i = 1 << (insnwidth - 1) |
| 109 | space = 0x01010100 |
| 110 | r = '' |
| 111 | while i != 0: |
| 112 | if i & mask: |
| 113 | if i & bits: |
| 114 | r += '1' |
| 115 | else: |
| 116 | r += '0' |
| 117 | else: |
| 118 | r += '.' |
| 119 | if i & space: |
| 120 | r += ' ' |
| 121 | i >>= 1 |
| 122 | return r |
| 123 | |
| 124 | |
| 125 | def is_pow2(x): |
| 126 | """Return true iff X is equal to a power of 2.""" |
| 127 | return (x & (x - 1)) == 0 |
| 128 | |
| 129 | |
| 130 | def ctz(x): |
| 131 | """Return the number of times 2 factors into X.""" |
Richard Henderson | b44b344 | 2020-05-16 13:15:02 -0700 | [diff] [blame] | 132 | assert x != 0 |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 133 | r = 0 |
| 134 | while ((x >> r) & 1) == 0: |
| 135 | r += 1 |
| 136 | return r |
| 137 | |
| 138 | |
| 139 | def is_contiguous(bits): |
Richard Henderson | b44b344 | 2020-05-16 13:15:02 -0700 | [diff] [blame] | 140 | if bits == 0: |
| 141 | return -1 |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 142 | shift = ctz(bits) |
| 143 | if is_pow2((bits >> shift) + 1): |
| 144 | return shift |
| 145 | else: |
| 146 | return -1 |
| 147 | |
| 148 | |
| 149 | def eq_fields_for_args(flds_a, flds_b): |
| 150 | if len(flds_a) != len(flds_b): |
| 151 | return False |
| 152 | for k, a in flds_a.items(): |
| 153 | if k not in flds_b: |
| 154 | return False |
| 155 | return True |
| 156 | |
| 157 | |
| 158 | def eq_fields_for_fmts(flds_a, flds_b): |
| 159 | if len(flds_a) != len(flds_b): |
| 160 | return False |
| 161 | for k, a in flds_a.items(): |
| 162 | if k not in flds_b: |
| 163 | return False |
| 164 | b = flds_b[k] |
| 165 | if a.__class__ != b.__class__ or a != b: |
| 166 | return False |
| 167 | return True |
| 168 | |
| 169 | |
| 170 | class Field: |
| 171 | """Class representing a simple instruction field""" |
| 172 | def __init__(self, sign, pos, len): |
| 173 | self.sign = sign |
| 174 | self.pos = pos |
| 175 | self.len = len |
| 176 | self.mask = ((1 << len) - 1) << pos |
| 177 | |
| 178 | def __str__(self): |
| 179 | if self.sign: |
| 180 | s = 's' |
| 181 | else: |
| 182 | s = '' |
Cleber Rosa | cbcdf1a | 2018-10-04 12:18:50 -0400 | [diff] [blame] | 183 | return str(self.pos) + ':' + s + str(self.len) |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 184 | |
| 185 | def str_extract(self): |
| 186 | if self.sign: |
| 187 | extr = 'sextract32' |
| 188 | else: |
| 189 | extr = 'extract32' |
| 190 | return '{0}(insn, {1}, {2})'.format(extr, self.pos, self.len) |
| 191 | |
| 192 | def __eq__(self, other): |
Richard Henderson | 2c7d442 | 2019-06-11 16:39:41 +0100 | [diff] [blame] | 193 | return self.sign == other.sign and self.mask == other.mask |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 194 | |
| 195 | def __ne__(self, other): |
| 196 | return not self.__eq__(other) |
| 197 | # end Field |
| 198 | |
| 199 | |
| 200 | class MultiField: |
| 201 | """Class representing a compound instruction field""" |
| 202 | def __init__(self, subs, mask): |
| 203 | self.subs = subs |
| 204 | self.sign = subs[0].sign |
| 205 | self.mask = mask |
| 206 | |
| 207 | def __str__(self): |
| 208 | return str(self.subs) |
| 209 | |
| 210 | def str_extract(self): |
| 211 | ret = '0' |
| 212 | pos = 0 |
| 213 | for f in reversed(self.subs): |
| 214 | if pos == 0: |
| 215 | ret = f.str_extract() |
| 216 | else: |
| 217 | ret = 'deposit32({0}, {1}, {2}, {3})' \ |
| 218 | .format(ret, pos, 32 - pos, f.str_extract()) |
| 219 | pos += f.len |
| 220 | return ret |
| 221 | |
| 222 | def __ne__(self, other): |
| 223 | if len(self.subs) != len(other.subs): |
| 224 | return True |
| 225 | for a, b in zip(self.subs, other.subs): |
| 226 | if a.__class__ != b.__class__ or a != b: |
| 227 | return True |
| 228 | return False |
| 229 | |
| 230 | def __eq__(self, other): |
| 231 | return not self.__ne__(other) |
| 232 | # end MultiField |
| 233 | |
| 234 | |
| 235 | class ConstField: |
| 236 | """Class representing an argument field with constant value""" |
| 237 | def __init__(self, value): |
| 238 | self.value = value |
| 239 | self.mask = 0 |
| 240 | self.sign = value < 0 |
| 241 | |
| 242 | def __str__(self): |
| 243 | return str(self.value) |
| 244 | |
| 245 | def str_extract(self): |
| 246 | return str(self.value) |
| 247 | |
| 248 | def __cmp__(self, other): |
| 249 | return self.value - other.value |
| 250 | # end ConstField |
| 251 | |
| 252 | |
| 253 | class FunctionField: |
Richard Henderson | 94597b6 | 2019-07-22 17:02:56 -0700 | [diff] [blame] | 254 | """Class representing a field passed through a function""" |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 255 | def __init__(self, func, base): |
| 256 | self.mask = base.mask |
| 257 | self.sign = base.sign |
| 258 | self.base = base |
| 259 | self.func = func |
| 260 | |
| 261 | def __str__(self): |
| 262 | return self.func + '(' + str(self.base) + ')' |
| 263 | |
| 264 | def str_extract(self): |
Richard Henderson | 451e4ff | 2019-03-20 19:21:31 -0700 | [diff] [blame] | 265 | return self.func + '(ctx, ' + self.base.str_extract() + ')' |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 266 | |
| 267 | def __eq__(self, other): |
| 268 | return self.func == other.func and self.base == other.base |
| 269 | |
| 270 | def __ne__(self, other): |
| 271 | return not self.__eq__(other) |
| 272 | # end FunctionField |
| 273 | |
| 274 | |
Richard Henderson | 94597b6 | 2019-07-22 17:02:56 -0700 | [diff] [blame] | 275 | class ParameterField: |
| 276 | """Class representing a pseudo-field read from a function""" |
| 277 | def __init__(self, func): |
| 278 | self.mask = 0 |
| 279 | self.sign = 0 |
| 280 | self.func = func |
| 281 | |
| 282 | def __str__(self): |
| 283 | return self.func |
| 284 | |
| 285 | def str_extract(self): |
| 286 | return self.func + '(ctx)' |
| 287 | |
| 288 | def __eq__(self, other): |
| 289 | return self.func == other.func |
| 290 | |
| 291 | def __ne__(self, other): |
| 292 | return not self.__eq__(other) |
| 293 | # end ParameterField |
| 294 | |
| 295 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 296 | class Arguments: |
| 297 | """Class representing the extracted fields of a format""" |
Richard Henderson | abd04f9 | 2018-10-23 10:26:25 +0100 | [diff] [blame] | 298 | def __init__(self, nm, flds, extern): |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 299 | self.name = nm |
Richard Henderson | abd04f9 | 2018-10-23 10:26:25 +0100 | [diff] [blame] | 300 | self.extern = extern |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 301 | self.fields = sorted(flds) |
| 302 | |
| 303 | def __str__(self): |
| 304 | return self.name + ' ' + str(self.fields) |
| 305 | |
| 306 | def struct_name(self): |
| 307 | return 'arg_' + self.name |
| 308 | |
| 309 | def output_def(self): |
Richard Henderson | abd04f9 | 2018-10-23 10:26:25 +0100 | [diff] [blame] | 310 | if not self.extern: |
| 311 | output('typedef struct {\n') |
| 312 | for n in self.fields: |
| 313 | output(' int ', n, ';\n') |
| 314 | output('} ', self.struct_name(), ';\n\n') |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 315 | # end Arguments |
| 316 | |
| 317 | |
| 318 | class General: |
| 319 | """Common code between instruction formats and instruction patterns""" |
Richard Henderson | 17560e9 | 2019-01-30 18:01:29 -0800 | [diff] [blame] | 320 | def __init__(self, name, lineno, base, fixb, fixm, udfm, fldm, flds, w): |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 321 | self.name = name |
Richard Henderson | 6699ae6 | 2018-10-26 14:59:43 +0100 | [diff] [blame] | 322 | self.file = input_file |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 323 | self.lineno = lineno |
| 324 | self.base = base |
| 325 | self.fixedbits = fixb |
| 326 | self.fixedmask = fixm |
| 327 | self.undefmask = udfm |
| 328 | self.fieldmask = fldm |
| 329 | self.fields = flds |
Richard Henderson | 17560e9 | 2019-01-30 18:01:29 -0800 | [diff] [blame] | 330 | self.width = w |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 331 | |
| 332 | def __str__(self): |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 333 | return self.name + ' ' + str_match_bits(self.fixedbits, self.fixedmask) |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 334 | |
| 335 | def str1(self, i): |
| 336 | return str_indent(i) + self.__str__() |
| 337 | # end General |
| 338 | |
| 339 | |
| 340 | class Format(General): |
| 341 | """Class representing an instruction format""" |
| 342 | |
| 343 | def extract_name(self): |
Richard Henderson | 71ecf79 | 2019-02-28 14:45:50 -0800 | [diff] [blame] | 344 | global decode_function |
| 345 | return decode_function + '_extract_' + self.name |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 346 | |
| 347 | def output_extract(self): |
Richard Henderson | 451e4ff | 2019-03-20 19:21:31 -0700 | [diff] [blame] | 348 | output('static void ', self.extract_name(), '(DisasContext *ctx, ', |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 349 | self.base.struct_name(), ' *a, ', insntype, ' insn)\n{\n') |
| 350 | for n, f in self.fields.items(): |
| 351 | output(' a->', n, ' = ', f.str_extract(), ';\n') |
| 352 | output('}\n\n') |
| 353 | # end Format |
| 354 | |
| 355 | |
| 356 | class Pattern(General): |
| 357 | """Class representing an instruction pattern""" |
| 358 | |
| 359 | def output_decl(self): |
| 360 | global translate_scope |
| 361 | global translate_prefix |
| 362 | output('typedef ', self.base.base.struct_name(), |
| 363 | ' arg_', self.name, ';\n') |
Richard Henderson | 7680559 | 2018-03-02 10:45:35 +0000 | [diff] [blame] | 364 | output(translate_scope, 'bool ', translate_prefix, '_', self.name, |
Richard Henderson | 3a7be55 | 2018-10-23 11:05:27 +0100 | [diff] [blame] | 365 | '(DisasContext *ctx, arg_', self.name, ' *a);\n') |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 366 | |
| 367 | def output_code(self, i, extracted, outerbits, outermask): |
| 368 | global translate_prefix |
| 369 | ind = str_indent(i) |
| 370 | arg = self.base.base.name |
Richard Henderson | 6699ae6 | 2018-10-26 14:59:43 +0100 | [diff] [blame] | 371 | output(ind, '/* ', self.file, ':', str(self.lineno), ' */\n') |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 372 | if not extracted: |
Richard Henderson | 451e4ff | 2019-03-20 19:21:31 -0700 | [diff] [blame] | 373 | output(ind, self.base.extract_name(), |
| 374 | '(ctx, &u.f_', arg, ', insn);\n') |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 375 | for n, f in self.fields.items(): |
| 376 | output(ind, 'u.f_', arg, '.', n, ' = ', f.str_extract(), ';\n') |
Richard Henderson | eb6b87f | 2019-02-23 08:57:46 -0800 | [diff] [blame] | 377 | output(ind, 'if (', translate_prefix, '_', self.name, |
| 378 | '(ctx, &u.f_', arg, ')) return true;\n') |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 379 | |
| 380 | # Normal patterns do not have children. |
| 381 | def build_tree(self): |
| 382 | return |
| 383 | def prop_masks(self): |
| 384 | return |
| 385 | def prop_format(self): |
| 386 | return |
| 387 | def prop_width(self): |
| 388 | return |
| 389 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 390 | # end Pattern |
| 391 | |
| 392 | |
Richard Henderson | df63044 | 2020-05-16 11:19:45 -0700 | [diff] [blame] | 393 | class MultiPattern(General): |
| 394 | """Class representing a set of instruction patterns""" |
| 395 | |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 396 | def __init__(self, lineno): |
Richard Henderson | df63044 | 2020-05-16 11:19:45 -0700 | [diff] [blame] | 397 | self.file = input_file |
| 398 | self.lineno = lineno |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 399 | self.pats = [] |
Richard Henderson | df63044 | 2020-05-16 11:19:45 -0700 | [diff] [blame] | 400 | self.base = None |
| 401 | self.fixedbits = 0 |
| 402 | self.fixedmask = 0 |
| 403 | self.undefmask = 0 |
| 404 | self.width = None |
| 405 | |
| 406 | def __str__(self): |
| 407 | r = 'group' |
| 408 | if self.fixedbits is not None: |
| 409 | r += ' ' + str_match_bits(self.fixedbits, self.fixedmask) |
| 410 | return r |
| 411 | |
| 412 | def output_decl(self): |
| 413 | for p in self.pats: |
| 414 | p.output_decl() |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 415 | |
| 416 | def prop_masks(self): |
| 417 | global insnmask |
| 418 | |
| 419 | fixedmask = insnmask |
| 420 | undefmask = insnmask |
| 421 | |
| 422 | # Collect fixedmask/undefmask for all of the children. |
| 423 | for p in self.pats: |
| 424 | p.prop_masks() |
| 425 | fixedmask &= p.fixedmask |
| 426 | undefmask &= p.undefmask |
| 427 | |
| 428 | # Widen fixedmask until all fixedbits match |
| 429 | repeat = True |
| 430 | fixedbits = 0 |
| 431 | while repeat and fixedmask != 0: |
| 432 | fixedbits = None |
| 433 | for p in self.pats: |
| 434 | thisbits = p.fixedbits & fixedmask |
| 435 | if fixedbits is None: |
| 436 | fixedbits = thisbits |
| 437 | elif fixedbits != thisbits: |
| 438 | fixedmask &= ~(fixedbits ^ thisbits) |
| 439 | break |
| 440 | else: |
| 441 | repeat = False |
| 442 | |
| 443 | self.fixedbits = fixedbits |
| 444 | self.fixedmask = fixedmask |
| 445 | self.undefmask = undefmask |
| 446 | |
| 447 | def build_tree(self): |
| 448 | for p in self.pats: |
| 449 | p.build_tree() |
| 450 | |
| 451 | def prop_format(self): |
| 452 | for p in self.pats: |
| 453 | p.build_tree() |
| 454 | |
| 455 | def prop_width(self): |
| 456 | width = None |
| 457 | for p in self.pats: |
| 458 | p.prop_width() |
| 459 | if width is None: |
| 460 | width = p.width |
| 461 | elif width != p.width: |
| 462 | error_with_file(self.file, self.lineno, |
| 463 | 'width mismatch in patterns within braces') |
| 464 | self.width = width |
| 465 | |
Richard Henderson | df63044 | 2020-05-16 11:19:45 -0700 | [diff] [blame] | 466 | # end MultiPattern |
| 467 | |
| 468 | |
| 469 | class IncMultiPattern(MultiPattern): |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 470 | """Class representing an overlapping set of instruction patterns""" |
| 471 | |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 472 | def output_code(self, i, extracted, outerbits, outermask): |
| 473 | global translate_prefix |
| 474 | ind = str_indent(i) |
| 475 | for p in self.pats: |
| 476 | if outermask != p.fixedmask: |
| 477 | innermask = p.fixedmask & ~outermask |
| 478 | innerbits = p.fixedbits & ~outermask |
| 479 | output(ind, 'if ((insn & ', |
| 480 | '0x{0:08x}) == 0x{1:08x}'.format(innermask, innerbits), |
| 481 | ') {\n') |
| 482 | output(ind, ' /* ', |
| 483 | str_match_bits(p.fixedbits, p.fixedmask), ' */\n') |
| 484 | p.output_code(i + 4, extracted, p.fixedbits, p.fixedmask) |
| 485 | output(ind, '}\n') |
| 486 | else: |
| 487 | p.output_code(i, extracted, p.fixedbits, p.fixedmask) |
Richard Henderson | 040145c | 2020-05-16 10:50:43 -0700 | [diff] [blame] | 488 | #end IncMultiPattern |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 489 | |
| 490 | |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 491 | class Tree: |
| 492 | """Class representing a node in a decode tree""" |
| 493 | |
| 494 | def __init__(self, fm, tm): |
| 495 | self.fixedmask = fm |
| 496 | self.thismask = tm |
| 497 | self.subs = [] |
| 498 | self.base = None |
| 499 | |
| 500 | def str1(self, i): |
| 501 | ind = str_indent(i) |
| 502 | r = '{0}{1:08x}'.format(ind, self.fixedmask) |
| 503 | if self.format: |
| 504 | r += ' ' + self.format.name |
| 505 | r += ' [\n' |
| 506 | for (b, s) in self.subs: |
| 507 | r += '{0} {1:08x}:\n'.format(ind, b) |
| 508 | r += s.str1(i + 4) + '\n' |
| 509 | r += ind + ']' |
| 510 | return r |
| 511 | |
| 512 | def __str__(self): |
| 513 | return self.str1(0) |
| 514 | |
| 515 | def output_code(self, i, extracted, outerbits, outermask): |
| 516 | ind = str_indent(i) |
| 517 | |
| 518 | # If we identified all nodes below have the same format, |
| 519 | # extract the fields now. |
| 520 | if not extracted and self.base: |
| 521 | output(ind, self.base.extract_name(), |
| 522 | '(ctx, &u.f_', self.base.base.name, ', insn);\n') |
| 523 | extracted = True |
| 524 | |
| 525 | # Attempt to aid the compiler in producing compact switch statements. |
| 526 | # If the bits in the mask are contiguous, extract them. |
| 527 | sh = is_contiguous(self.thismask) |
| 528 | if sh > 0: |
| 529 | # Propagate SH down into the local functions. |
| 530 | def str_switch(b, sh=sh): |
| 531 | return '(insn >> {0}) & 0x{1:x}'.format(sh, b >> sh) |
| 532 | |
| 533 | def str_case(b, sh=sh): |
| 534 | return '0x{0:x}'.format(b >> sh) |
| 535 | else: |
| 536 | def str_switch(b): |
| 537 | return 'insn & 0x{0:08x}'.format(b) |
| 538 | |
| 539 | def str_case(b): |
| 540 | return '0x{0:08x}'.format(b) |
| 541 | |
| 542 | output(ind, 'switch (', str_switch(self.thismask), ') {\n') |
| 543 | for b, s in sorted(self.subs): |
| 544 | assert (self.thismask & ~s.fixedmask) == 0 |
| 545 | innermask = outermask | self.thismask |
| 546 | innerbits = outerbits | b |
| 547 | output(ind, 'case ', str_case(b), ':\n') |
| 548 | output(ind, ' /* ', |
| 549 | str_match_bits(innerbits, innermask), ' */\n') |
| 550 | s.output_code(i + 4, extracted, innerbits, innermask) |
| 551 | output(ind, ' return false;\n') |
| 552 | output(ind, '}\n') |
| 553 | # end Tree |
| 554 | |
| 555 | |
| 556 | class ExcMultiPattern(MultiPattern): |
| 557 | """Class representing a non-overlapping set of instruction patterns""" |
| 558 | |
| 559 | def output_code(self, i, extracted, outerbits, outermask): |
| 560 | # Defer everything to our decomposed Tree node |
| 561 | self.tree.output_code(i, extracted, outerbits, outermask) |
| 562 | |
| 563 | @staticmethod |
| 564 | def __build_tree(pats, outerbits, outermask): |
| 565 | # Find the intersection of all remaining fixedmask. |
| 566 | innermask = ~outermask & insnmask |
| 567 | for i in pats: |
| 568 | innermask &= i.fixedmask |
| 569 | |
| 570 | if innermask == 0: |
| 571 | # Edge condition: One pattern covers the entire insnmask |
| 572 | if len(pats) == 1: |
| 573 | t = Tree(outermask, innermask) |
| 574 | t.subs.append((0, pats[0])) |
| 575 | return t |
| 576 | |
| 577 | text = 'overlapping patterns:' |
| 578 | for p in pats: |
| 579 | text += '\n' + p.file + ':' + str(p.lineno) + ': ' + str(p) |
| 580 | error_with_file(pats[0].file, pats[0].lineno, text) |
| 581 | |
| 582 | fullmask = outermask | innermask |
| 583 | |
| 584 | # Sort each element of pats into the bin selected by the mask. |
| 585 | bins = {} |
| 586 | for i in pats: |
| 587 | fb = i.fixedbits & innermask |
| 588 | if fb in bins: |
| 589 | bins[fb].append(i) |
| 590 | else: |
| 591 | bins[fb] = [i] |
| 592 | |
| 593 | # We must recurse if any bin has more than one element or if |
| 594 | # the single element in the bin has not been fully matched. |
| 595 | t = Tree(fullmask, innermask) |
| 596 | |
| 597 | for b, l in bins.items(): |
| 598 | s = l[0] |
| 599 | if len(l) > 1 or s.fixedmask & ~fullmask != 0: |
| 600 | s = ExcMultiPattern.__build_tree(l, b | outerbits, fullmask) |
| 601 | t.subs.append((b, s)) |
| 602 | |
| 603 | return t |
| 604 | |
| 605 | def build_tree(self): |
| 606 | super().prop_format() |
| 607 | self.tree = self.__build_tree(self.pats, self.fixedbits, |
| 608 | self.fixedmask) |
| 609 | |
| 610 | @staticmethod |
| 611 | def __prop_format(tree): |
| 612 | """Propagate Format objects into the decode tree""" |
| 613 | |
| 614 | # Depth first search. |
| 615 | for (b, s) in tree.subs: |
| 616 | if isinstance(s, Tree): |
| 617 | ExcMultiPattern.__prop_format(s) |
| 618 | |
| 619 | # If all entries in SUBS have the same format, then |
| 620 | # propagate that into the tree. |
| 621 | f = None |
| 622 | for (b, s) in tree.subs: |
| 623 | if f is None: |
| 624 | f = s.base |
| 625 | if f is None: |
| 626 | return |
| 627 | if f is not s.base: |
| 628 | return |
| 629 | tree.base = f |
| 630 | |
| 631 | def prop_format(self): |
| 632 | super().prop_format() |
| 633 | self.__prop_format(self.tree) |
| 634 | |
| 635 | # end ExcMultiPattern |
| 636 | |
| 637 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 638 | def parse_field(lineno, name, toks): |
| 639 | """Parse one instruction field from TOKS at LINENO""" |
| 640 | global fields |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 641 | global insnwidth |
| 642 | |
| 643 | # A "simple" field will have only one entry; |
| 644 | # a "multifield" will have several. |
| 645 | subs = [] |
| 646 | width = 0 |
| 647 | func = None |
| 648 | for t in toks: |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 649 | if re.match('^!function=', t): |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 650 | if func: |
| 651 | error(lineno, 'duplicate function') |
| 652 | func = t.split('=') |
| 653 | func = func[1] |
| 654 | continue |
| 655 | |
John Snow | 2d110c1 | 2020-05-13 23:52:30 -0400 | [diff] [blame] | 656 | if re.fullmatch('[0-9]+:s[0-9]+', t): |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 657 | # Signed field extract |
| 658 | subtoks = t.split(':s') |
| 659 | sign = True |
John Snow | 2d110c1 | 2020-05-13 23:52:30 -0400 | [diff] [blame] | 660 | elif re.fullmatch('[0-9]+:[0-9]+', t): |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 661 | # Unsigned field extract |
| 662 | subtoks = t.split(':') |
| 663 | sign = False |
| 664 | else: |
| 665 | error(lineno, 'invalid field token "{0}"'.format(t)) |
| 666 | po = int(subtoks[0]) |
| 667 | le = int(subtoks[1]) |
| 668 | if po + le > insnwidth: |
| 669 | error(lineno, 'field {0} too large'.format(t)) |
| 670 | f = Field(sign, po, le) |
| 671 | subs.append(f) |
| 672 | width += le |
| 673 | |
| 674 | if width > insnwidth: |
| 675 | error(lineno, 'field too large') |
Richard Henderson | 94597b6 | 2019-07-22 17:02:56 -0700 | [diff] [blame] | 676 | if len(subs) == 0: |
| 677 | if func: |
| 678 | f = ParameterField(func) |
| 679 | else: |
| 680 | error(lineno, 'field with no value') |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 681 | else: |
Richard Henderson | 94597b6 | 2019-07-22 17:02:56 -0700 | [diff] [blame] | 682 | if len(subs) == 1: |
| 683 | f = subs[0] |
| 684 | else: |
| 685 | mask = 0 |
| 686 | for s in subs: |
| 687 | if mask & s.mask: |
| 688 | error(lineno, 'field components overlap') |
| 689 | mask |= s.mask |
| 690 | f = MultiField(subs, mask) |
| 691 | if func: |
| 692 | f = FunctionField(func, f) |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 693 | |
| 694 | if name in fields: |
| 695 | error(lineno, 'duplicate field', name) |
| 696 | fields[name] = f |
| 697 | # end parse_field |
| 698 | |
| 699 | |
| 700 | def parse_arguments(lineno, name, toks): |
| 701 | """Parse one argument set from TOKS at LINENO""" |
| 702 | global arguments |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 703 | global re_C_ident |
Richard Henderson | c692079 | 2019-08-09 08:12:50 -0700 | [diff] [blame] | 704 | global anyextern |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 705 | |
| 706 | flds = [] |
Richard Henderson | abd04f9 | 2018-10-23 10:26:25 +0100 | [diff] [blame] | 707 | extern = False |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 708 | for t in toks: |
John Snow | 2d110c1 | 2020-05-13 23:52:30 -0400 | [diff] [blame] | 709 | if re.fullmatch('!extern', t): |
Richard Henderson | abd04f9 | 2018-10-23 10:26:25 +0100 | [diff] [blame] | 710 | extern = True |
Richard Henderson | c692079 | 2019-08-09 08:12:50 -0700 | [diff] [blame] | 711 | anyextern = True |
Richard Henderson | abd04f9 | 2018-10-23 10:26:25 +0100 | [diff] [blame] | 712 | continue |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 713 | if not re.fullmatch(re_C_ident, t): |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 714 | error(lineno, 'invalid argument set token "{0}"'.format(t)) |
| 715 | if t in flds: |
| 716 | error(lineno, 'duplicate argument "{0}"'.format(t)) |
| 717 | flds.append(t) |
| 718 | |
| 719 | if name in arguments: |
| 720 | error(lineno, 'duplicate argument set', name) |
Richard Henderson | abd04f9 | 2018-10-23 10:26:25 +0100 | [diff] [blame] | 721 | arguments[name] = Arguments(name, flds, extern) |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 722 | # end parse_arguments |
| 723 | |
| 724 | |
| 725 | def lookup_field(lineno, name): |
| 726 | global fields |
| 727 | if name in fields: |
| 728 | return fields[name] |
| 729 | error(lineno, 'undefined field', name) |
| 730 | |
| 731 | |
| 732 | def add_field(lineno, flds, new_name, f): |
| 733 | if new_name in flds: |
| 734 | error(lineno, 'duplicate field', new_name) |
| 735 | flds[new_name] = f |
| 736 | return flds |
| 737 | |
| 738 | |
| 739 | def add_field_byname(lineno, flds, new_name, old_name): |
| 740 | return add_field(lineno, flds, new_name, lookup_field(lineno, old_name)) |
| 741 | |
| 742 | |
| 743 | def infer_argument_set(flds): |
| 744 | global arguments |
Richard Henderson | abd04f9 | 2018-10-23 10:26:25 +0100 | [diff] [blame] | 745 | global decode_function |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 746 | |
| 747 | for arg in arguments.values(): |
| 748 | if eq_fields_for_args(flds, arg.fields): |
| 749 | return arg |
| 750 | |
Richard Henderson | abd04f9 | 2018-10-23 10:26:25 +0100 | [diff] [blame] | 751 | name = decode_function + str(len(arguments)) |
| 752 | arg = Arguments(name, flds.keys(), False) |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 753 | arguments[name] = arg |
| 754 | return arg |
| 755 | |
| 756 | |
Richard Henderson | 17560e9 | 2019-01-30 18:01:29 -0800 | [diff] [blame] | 757 | def infer_format(arg, fieldmask, flds, width): |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 758 | global arguments |
| 759 | global formats |
Richard Henderson | abd04f9 | 2018-10-23 10:26:25 +0100 | [diff] [blame] | 760 | global decode_function |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 761 | |
| 762 | const_flds = {} |
| 763 | var_flds = {} |
| 764 | for n, c in flds.items(): |
| 765 | if c is ConstField: |
| 766 | const_flds[n] = c |
| 767 | else: |
| 768 | var_flds[n] = c |
| 769 | |
| 770 | # Look for an existing format with the same argument set and fields |
| 771 | for fmt in formats.values(): |
| 772 | if arg and fmt.base != arg: |
| 773 | continue |
| 774 | if fieldmask != fmt.fieldmask: |
| 775 | continue |
Richard Henderson | 17560e9 | 2019-01-30 18:01:29 -0800 | [diff] [blame] | 776 | if width != fmt.width: |
| 777 | continue |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 778 | if not eq_fields_for_fmts(flds, fmt.fields): |
| 779 | continue |
| 780 | return (fmt, const_flds) |
| 781 | |
Richard Henderson | abd04f9 | 2018-10-23 10:26:25 +0100 | [diff] [blame] | 782 | name = decode_function + '_Fmt_' + str(len(formats)) |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 783 | if not arg: |
| 784 | arg = infer_argument_set(flds) |
| 785 | |
Richard Henderson | 17560e9 | 2019-01-30 18:01:29 -0800 | [diff] [blame] | 786 | fmt = Format(name, 0, arg, 0, 0, 0, fieldmask, var_flds, width) |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 787 | formats[name] = fmt |
| 788 | |
| 789 | return (fmt, const_flds) |
| 790 | # end infer_format |
| 791 | |
| 792 | |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 793 | def parse_generic(lineno, parent_pat, name, toks): |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 794 | """Parse one instruction format from TOKS at LINENO""" |
| 795 | global fields |
| 796 | global arguments |
| 797 | global formats |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 798 | global allpatterns |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 799 | global re_arg_ident |
| 800 | global re_fld_ident |
| 801 | global re_fmt_ident |
| 802 | global re_C_ident |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 803 | global insnwidth |
| 804 | global insnmask |
Richard Henderson | 17560e9 | 2019-01-30 18:01:29 -0800 | [diff] [blame] | 805 | global variablewidth |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 806 | |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 807 | is_format = parent_pat is None |
| 808 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 809 | fixedmask = 0 |
| 810 | fixedbits = 0 |
| 811 | undefmask = 0 |
| 812 | width = 0 |
| 813 | flds = {} |
| 814 | arg = None |
| 815 | fmt = None |
| 816 | for t in toks: |
zhaolichang | 65fdb3c | 2020-09-17 15:50:23 +0800 | [diff] [blame^] | 817 | # '&Foo' gives a format an explicit argument set. |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 818 | if re.fullmatch(re_arg_ident, t): |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 819 | tt = t[1:] |
| 820 | if arg: |
| 821 | error(lineno, 'multiple argument sets') |
| 822 | if tt in arguments: |
| 823 | arg = arguments[tt] |
| 824 | else: |
| 825 | error(lineno, 'undefined argument set', t) |
| 826 | continue |
| 827 | |
| 828 | # '@Foo' gives a pattern an explicit format. |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 829 | if re.fullmatch(re_fmt_ident, t): |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 830 | tt = t[1:] |
| 831 | if fmt: |
| 832 | error(lineno, 'multiple formats') |
| 833 | if tt in formats: |
| 834 | fmt = formats[tt] |
| 835 | else: |
| 836 | error(lineno, 'undefined format', t) |
| 837 | continue |
| 838 | |
| 839 | # '%Foo' imports a field. |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 840 | if re.fullmatch(re_fld_ident, t): |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 841 | tt = t[1:] |
| 842 | flds = add_field_byname(lineno, flds, tt, tt) |
| 843 | continue |
| 844 | |
| 845 | # 'Foo=%Bar' imports a field with a different name. |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 846 | if re.fullmatch(re_C_ident + '=' + re_fld_ident, t): |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 847 | (fname, iname) = t.split('=%') |
| 848 | flds = add_field_byname(lineno, flds, fname, iname) |
| 849 | continue |
| 850 | |
| 851 | # 'Foo=number' sets an argument field to a constant value |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 852 | if re.fullmatch(re_C_ident + '=[+-]?[0-9]+', t): |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 853 | (fname, value) = t.split('=') |
| 854 | value = int(value) |
| 855 | flds = add_field(lineno, flds, fname, ConstField(value)) |
| 856 | continue |
| 857 | |
| 858 | # Pattern of 0s, 1s, dots and dashes indicate required zeros, |
| 859 | # required ones, or dont-cares. |
John Snow | 2d110c1 | 2020-05-13 23:52:30 -0400 | [diff] [blame] | 860 | if re.fullmatch('[01.-]+', t): |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 861 | shift = len(t) |
| 862 | fms = t.replace('0', '1') |
| 863 | fms = fms.replace('.', '0') |
| 864 | fms = fms.replace('-', '0') |
| 865 | fbs = t.replace('.', '0') |
| 866 | fbs = fbs.replace('-', '0') |
| 867 | ubm = t.replace('1', '0') |
| 868 | ubm = ubm.replace('.', '0') |
| 869 | ubm = ubm.replace('-', '1') |
| 870 | fms = int(fms, 2) |
| 871 | fbs = int(fbs, 2) |
| 872 | ubm = int(ubm, 2) |
| 873 | fixedbits = (fixedbits << shift) | fbs |
| 874 | fixedmask = (fixedmask << shift) | fms |
| 875 | undefmask = (undefmask << shift) | ubm |
| 876 | # Otherwise, fieldname:fieldwidth |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 877 | elif re.fullmatch(re_C_ident + ':s?[0-9]+', t): |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 878 | (fname, flen) = t.split(':') |
| 879 | sign = False |
| 880 | if flen[0] == 's': |
| 881 | sign = True |
| 882 | flen = flen[1:] |
| 883 | shift = int(flen, 10) |
Richard Henderson | 2decfc9 | 2019-03-05 15:34:41 -0800 | [diff] [blame] | 884 | if shift + width > insnwidth: |
| 885 | error(lineno, 'field {0} exceeds insnwidth'.format(fname)) |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 886 | f = Field(sign, insnwidth - width - shift, shift) |
| 887 | flds = add_field(lineno, flds, fname, f) |
| 888 | fixedbits <<= shift |
| 889 | fixedmask <<= shift |
| 890 | undefmask <<= shift |
| 891 | else: |
| 892 | error(lineno, 'invalid token "{0}"'.format(t)) |
| 893 | width += shift |
| 894 | |
Richard Henderson | 17560e9 | 2019-01-30 18:01:29 -0800 | [diff] [blame] | 895 | if variablewidth and width < insnwidth and width % 8 == 0: |
| 896 | shift = insnwidth - width |
| 897 | fixedbits <<= shift |
| 898 | fixedmask <<= shift |
| 899 | undefmask <<= shift |
| 900 | undefmask |= (1 << shift) - 1 |
| 901 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 902 | # We should have filled in all of the bits of the instruction. |
Richard Henderson | 17560e9 | 2019-01-30 18:01:29 -0800 | [diff] [blame] | 903 | elif not (is_format and width == 0) and width != insnwidth: |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 904 | error(lineno, 'definition has {0} bits'.format(width)) |
| 905 | |
zhaolichang | 65fdb3c | 2020-09-17 15:50:23 +0800 | [diff] [blame^] | 906 | # Do not check for fields overlapping fields; one valid usage |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 907 | # is to be able to duplicate fields via import. |
| 908 | fieldmask = 0 |
| 909 | for f in flds.values(): |
| 910 | fieldmask |= f.mask |
| 911 | |
| 912 | # Fix up what we've parsed to match either a format or a pattern. |
| 913 | if is_format: |
| 914 | # Formats cannot reference formats. |
| 915 | if fmt: |
| 916 | error(lineno, 'format referencing format') |
| 917 | # If an argument set is given, then there should be no fields |
| 918 | # without a place to store it. |
| 919 | if arg: |
| 920 | for f in flds.keys(): |
| 921 | if f not in arg.fields: |
| 922 | error(lineno, 'field {0} not in argument set {1}' |
| 923 | .format(f, arg.name)) |
| 924 | else: |
| 925 | arg = infer_argument_set(flds) |
| 926 | if name in formats: |
| 927 | error(lineno, 'duplicate format name', name) |
| 928 | fmt = Format(name, lineno, arg, fixedbits, fixedmask, |
Richard Henderson | 17560e9 | 2019-01-30 18:01:29 -0800 | [diff] [blame] | 929 | undefmask, fieldmask, flds, width) |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 930 | formats[name] = fmt |
| 931 | else: |
| 932 | # Patterns can reference a format ... |
| 933 | if fmt: |
| 934 | # ... but not an argument simultaneously |
| 935 | if arg: |
| 936 | error(lineno, 'pattern specifies both format and argument set') |
| 937 | if fixedmask & fmt.fixedmask: |
| 938 | error(lineno, 'pattern fixed bits overlap format fixed bits') |
Richard Henderson | 17560e9 | 2019-01-30 18:01:29 -0800 | [diff] [blame] | 939 | if width != fmt.width: |
| 940 | error(lineno, 'pattern uses format of different width') |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 941 | fieldmask |= fmt.fieldmask |
| 942 | fixedbits |= fmt.fixedbits |
| 943 | fixedmask |= fmt.fixedmask |
| 944 | undefmask |= fmt.undefmask |
| 945 | else: |
Richard Henderson | 17560e9 | 2019-01-30 18:01:29 -0800 | [diff] [blame] | 946 | (fmt, flds) = infer_format(arg, fieldmask, flds, width) |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 947 | arg = fmt.base |
| 948 | for f in flds.keys(): |
| 949 | if f not in arg.fields: |
| 950 | error(lineno, 'field {0} not in argument set {1}' |
| 951 | .format(f, arg.name)) |
| 952 | if f in fmt.fields.keys(): |
| 953 | error(lineno, 'field {0} set by format and pattern'.format(f)) |
| 954 | for f in arg.fields: |
| 955 | if f not in flds.keys() and f not in fmt.fields.keys(): |
| 956 | error(lineno, 'field {0} not initialized'.format(f)) |
| 957 | pat = Pattern(name, lineno, fmt, fixedbits, fixedmask, |
Richard Henderson | 17560e9 | 2019-01-30 18:01:29 -0800 | [diff] [blame] | 958 | undefmask, fieldmask, flds, width) |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 959 | parent_pat.pats.append(pat) |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 960 | allpatterns.append(pat) |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 961 | |
| 962 | # Validate the masks that we have assembled. |
| 963 | if fieldmask & fixedmask: |
| 964 | error(lineno, 'fieldmask overlaps fixedmask (0x{0:08x} & 0x{1:08x})' |
| 965 | .format(fieldmask, fixedmask)) |
| 966 | if fieldmask & undefmask: |
| 967 | error(lineno, 'fieldmask overlaps undefmask (0x{0:08x} & 0x{1:08x})' |
| 968 | .format(fieldmask, undefmask)) |
| 969 | if fixedmask & undefmask: |
| 970 | error(lineno, 'fixedmask overlaps undefmask (0x{0:08x} & 0x{1:08x})' |
| 971 | .format(fixedmask, undefmask)) |
| 972 | if not is_format: |
| 973 | allbits = fieldmask | fixedmask | undefmask |
| 974 | if allbits != insnmask: |
| 975 | error(lineno, 'bits left unspecified (0x{0:08x})' |
| 976 | .format(allbits ^ insnmask)) |
| 977 | # end parse_general |
| 978 | |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 979 | |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 980 | def parse_file(f, parent_pat): |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 981 | """Parse all of the patterns within a file""" |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 982 | global re_arg_ident |
| 983 | global re_fld_ident |
| 984 | global re_fmt_ident |
| 985 | global re_pat_ident |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 986 | |
| 987 | # Read all of the lines of the file. Concatenate lines |
| 988 | # ending in backslash; discard empty lines and comments. |
| 989 | toks = [] |
| 990 | lineno = 0 |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 991 | nesting = 0 |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 992 | nesting_pats = [] |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 993 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 994 | for line in f: |
| 995 | lineno += 1 |
| 996 | |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 997 | # Expand and strip spaces, to find indent. |
| 998 | line = line.rstrip() |
| 999 | line = line.expandtabs() |
| 1000 | len1 = len(line) |
| 1001 | line = line.lstrip() |
| 1002 | len2 = len(line) |
| 1003 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1004 | # Discard comments |
| 1005 | end = line.find('#') |
| 1006 | if end >= 0: |
| 1007 | line = line[:end] |
| 1008 | |
| 1009 | t = line.split() |
| 1010 | if len(toks) != 0: |
| 1011 | # Next line after continuation |
| 1012 | toks.extend(t) |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1013 | else: |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 1014 | # Allow completely blank lines. |
| 1015 | if len1 == 0: |
| 1016 | continue |
| 1017 | indent = len1 - len2 |
| 1018 | # Empty line due to comment. |
| 1019 | if len(t) == 0: |
| 1020 | # Indentation must be correct, even for comment lines. |
| 1021 | if indent != nesting: |
| 1022 | error(lineno, 'indentation ', indent, ' != ', nesting) |
| 1023 | continue |
| 1024 | start_lineno = lineno |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1025 | toks = t |
| 1026 | |
| 1027 | # Continuation? |
| 1028 | if toks[-1] == '\\': |
| 1029 | toks.pop() |
| 1030 | continue |
| 1031 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1032 | name = toks[0] |
| 1033 | del toks[0] |
| 1034 | |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 1035 | # End nesting? |
Richard Henderson | 067e8b0 | 2020-05-18 08:45:32 -0700 | [diff] [blame] | 1036 | if name == '}' or name == ']': |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 1037 | if len(toks) != 0: |
| 1038 | error(start_lineno, 'extra tokens after close brace') |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 1039 | |
Richard Henderson | 067e8b0 | 2020-05-18 08:45:32 -0700 | [diff] [blame] | 1040 | # Make sure { } and [ ] nest properly. |
| 1041 | if (name == '}') != isinstance(parent_pat, IncMultiPattern): |
| 1042 | error(lineno, 'mismatched close brace') |
| 1043 | |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 1044 | try: |
| 1045 | parent_pat = nesting_pats.pop() |
| 1046 | except: |
Richard Henderson | 067e8b0 | 2020-05-18 08:45:32 -0700 | [diff] [blame] | 1047 | error(lineno, 'extra close brace') |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 1048 | |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 1049 | nesting -= 2 |
| 1050 | if indent != nesting: |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 1051 | error(lineno, 'indentation ', indent, ' != ', nesting) |
| 1052 | |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 1053 | toks = [] |
| 1054 | continue |
| 1055 | |
| 1056 | # Everything else should have current indentation. |
| 1057 | if indent != nesting: |
| 1058 | error(start_lineno, 'indentation ', indent, ' != ', nesting) |
| 1059 | |
| 1060 | # Start nesting? |
Richard Henderson | 067e8b0 | 2020-05-18 08:45:32 -0700 | [diff] [blame] | 1061 | if name == '{' or name == '[': |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 1062 | if len(toks) != 0: |
| 1063 | error(start_lineno, 'extra tokens after open brace') |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 1064 | |
Richard Henderson | 067e8b0 | 2020-05-18 08:45:32 -0700 | [diff] [blame] | 1065 | if name == '{': |
| 1066 | nested_pat = IncMultiPattern(start_lineno) |
| 1067 | else: |
| 1068 | nested_pat = ExcMultiPattern(start_lineno) |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 1069 | parent_pat.pats.append(nested_pat) |
| 1070 | nesting_pats.append(parent_pat) |
| 1071 | parent_pat = nested_pat |
| 1072 | |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 1073 | nesting += 2 |
| 1074 | toks = [] |
| 1075 | continue |
| 1076 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1077 | # Determine the type of object needing to be parsed. |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 1078 | if re.fullmatch(re_fld_ident, name): |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 1079 | parse_field(start_lineno, name[1:], toks) |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 1080 | elif re.fullmatch(re_arg_ident, name): |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 1081 | parse_arguments(start_lineno, name[1:], toks) |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 1082 | elif re.fullmatch(re_fmt_ident, name): |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 1083 | parse_generic(start_lineno, None, name[1:], toks) |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 1084 | elif re.fullmatch(re_pat_ident, name): |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 1085 | parse_generic(start_lineno, parent_pat, name, toks) |
Richard Henderson | acfdd23 | 2020-09-03 12:23:34 -0700 | [diff] [blame] | 1086 | else: |
| 1087 | error(lineno, 'invalid token "{0}"'.format(name)) |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1088 | toks = [] |
Richard Henderson | 067e8b0 | 2020-05-18 08:45:32 -0700 | [diff] [blame] | 1089 | |
| 1090 | if nesting != 0: |
| 1091 | error(lineno, 'missing close brace') |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1092 | # end parse_file |
| 1093 | |
| 1094 | |
Richard Henderson | 70e0711 | 2019-01-31 11:34:11 -0800 | [diff] [blame] | 1095 | class SizeTree: |
| 1096 | """Class representing a node in a size decode tree""" |
| 1097 | |
| 1098 | def __init__(self, m, w): |
| 1099 | self.mask = m |
| 1100 | self.subs = [] |
| 1101 | self.base = None |
| 1102 | self.width = w |
| 1103 | |
| 1104 | def str1(self, i): |
| 1105 | ind = str_indent(i) |
| 1106 | r = '{0}{1:08x}'.format(ind, self.mask) |
| 1107 | r += ' [\n' |
| 1108 | for (b, s) in self.subs: |
| 1109 | r += '{0} {1:08x}:\n'.format(ind, b) |
| 1110 | r += s.str1(i + 4) + '\n' |
| 1111 | r += ind + ']' |
| 1112 | return r |
| 1113 | |
| 1114 | def __str__(self): |
| 1115 | return self.str1(0) |
| 1116 | |
| 1117 | def output_code(self, i, extracted, outerbits, outermask): |
| 1118 | ind = str_indent(i) |
| 1119 | |
| 1120 | # If we need to load more bytes to test, do so now. |
| 1121 | if extracted < self.width: |
| 1122 | output(ind, 'insn = ', decode_function, |
| 1123 | '_load_bytes(ctx, insn, {0}, {1});\n' |
Philippe Mathieu-Daudé | b412378 | 2020-03-30 14:13:45 +0200 | [diff] [blame] | 1124 | .format(extracted // 8, self.width // 8)); |
Richard Henderson | 70e0711 | 2019-01-31 11:34:11 -0800 | [diff] [blame] | 1125 | extracted = self.width |
| 1126 | |
| 1127 | # Attempt to aid the compiler in producing compact switch statements. |
| 1128 | # If the bits in the mask are contiguous, extract them. |
| 1129 | sh = is_contiguous(self.mask) |
| 1130 | if sh > 0: |
| 1131 | # Propagate SH down into the local functions. |
| 1132 | def str_switch(b, sh=sh): |
| 1133 | return '(insn >> {0}) & 0x{1:x}'.format(sh, b >> sh) |
| 1134 | |
| 1135 | def str_case(b, sh=sh): |
| 1136 | return '0x{0:x}'.format(b >> sh) |
| 1137 | else: |
| 1138 | def str_switch(b): |
| 1139 | return 'insn & 0x{0:08x}'.format(b) |
| 1140 | |
| 1141 | def str_case(b): |
| 1142 | return '0x{0:08x}'.format(b) |
| 1143 | |
| 1144 | output(ind, 'switch (', str_switch(self.mask), ') {\n') |
| 1145 | for b, s in sorted(self.subs): |
| 1146 | innermask = outermask | self.mask |
| 1147 | innerbits = outerbits | b |
| 1148 | output(ind, 'case ', str_case(b), ':\n') |
| 1149 | output(ind, ' /* ', |
| 1150 | str_match_bits(innerbits, innermask), ' */\n') |
| 1151 | s.output_code(i + 4, extracted, innerbits, innermask) |
| 1152 | output(ind, '}\n') |
| 1153 | output(ind, 'return insn;\n') |
| 1154 | # end SizeTree |
| 1155 | |
| 1156 | class SizeLeaf: |
| 1157 | """Class representing a leaf node in a size decode tree""" |
| 1158 | |
| 1159 | def __init__(self, m, w): |
| 1160 | self.mask = m |
| 1161 | self.width = w |
| 1162 | |
| 1163 | def str1(self, i): |
| 1164 | ind = str_indent(i) |
| 1165 | return '{0}{1:08x}'.format(ind, self.mask) |
| 1166 | |
| 1167 | def __str__(self): |
| 1168 | return self.str1(0) |
| 1169 | |
| 1170 | def output_code(self, i, extracted, outerbits, outermask): |
| 1171 | global decode_function |
| 1172 | ind = str_indent(i) |
| 1173 | |
| 1174 | # If we need to load more bytes, do so now. |
| 1175 | if extracted < self.width: |
| 1176 | output(ind, 'insn = ', decode_function, |
| 1177 | '_load_bytes(ctx, insn, {0}, {1});\n' |
Philippe Mathieu-Daudé | b412378 | 2020-03-30 14:13:45 +0200 | [diff] [blame] | 1178 | .format(extracted // 8, self.width // 8)); |
Richard Henderson | 70e0711 | 2019-01-31 11:34:11 -0800 | [diff] [blame] | 1179 | extracted = self.width |
| 1180 | output(ind, 'return insn;\n') |
| 1181 | # end SizeLeaf |
| 1182 | |
| 1183 | |
| 1184 | def build_size_tree(pats, width, outerbits, outermask): |
| 1185 | global insnwidth |
| 1186 | |
| 1187 | # Collect the mask of bits that are fixed in this width |
| 1188 | innermask = 0xff << (insnwidth - width) |
| 1189 | innermask &= ~outermask |
| 1190 | minwidth = None |
| 1191 | onewidth = True |
| 1192 | for i in pats: |
| 1193 | innermask &= i.fixedmask |
| 1194 | if minwidth is None: |
| 1195 | minwidth = i.width |
| 1196 | elif minwidth != i.width: |
| 1197 | onewidth = False; |
| 1198 | if minwidth < i.width: |
| 1199 | minwidth = i.width |
| 1200 | |
| 1201 | if onewidth: |
| 1202 | return SizeLeaf(innermask, minwidth) |
| 1203 | |
| 1204 | if innermask == 0: |
| 1205 | if width < minwidth: |
| 1206 | return build_size_tree(pats, width + 8, outerbits, outermask) |
| 1207 | |
| 1208 | pnames = [] |
| 1209 | for p in pats: |
| 1210 | pnames.append(p.name + ':' + p.file + ':' + str(p.lineno)) |
| 1211 | error_with_file(pats[0].file, pats[0].lineno, |
| 1212 | 'overlapping patterns size {0}:'.format(width), pnames) |
| 1213 | |
| 1214 | bins = {} |
| 1215 | for i in pats: |
| 1216 | fb = i.fixedbits & innermask |
| 1217 | if fb in bins: |
| 1218 | bins[fb].append(i) |
| 1219 | else: |
| 1220 | bins[fb] = [i] |
| 1221 | |
| 1222 | fullmask = outermask | innermask |
| 1223 | lens = sorted(bins.keys()) |
| 1224 | if len(lens) == 1: |
| 1225 | b = lens[0] |
| 1226 | return build_size_tree(bins[b], width + 8, b | outerbits, fullmask) |
| 1227 | |
| 1228 | r = SizeTree(innermask, width) |
| 1229 | for b, l in bins.items(): |
| 1230 | s = build_size_tree(l, width, b | outerbits, fullmask) |
| 1231 | r.subs.append((b, s)) |
| 1232 | return r |
| 1233 | # end build_size_tree |
| 1234 | |
| 1235 | |
Richard Henderson | 70e0711 | 2019-01-31 11:34:11 -0800 | [diff] [blame] | 1236 | def prop_size(tree): |
| 1237 | """Propagate minimum widths up the decode size tree""" |
| 1238 | |
| 1239 | if isinstance(tree, SizeTree): |
| 1240 | min = None |
| 1241 | for (b, s) in tree.subs: |
| 1242 | width = prop_size(s) |
| 1243 | if min is None or min > width: |
| 1244 | min = width |
| 1245 | assert min >= tree.width |
| 1246 | tree.width = min |
| 1247 | else: |
| 1248 | min = tree.width |
| 1249 | return min |
| 1250 | # end prop_size |
| 1251 | |
| 1252 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1253 | def main(): |
| 1254 | global arguments |
| 1255 | global formats |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 1256 | global allpatterns |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1257 | global translate_scope |
| 1258 | global translate_prefix |
| 1259 | global output_fd |
| 1260 | global output_file |
| 1261 | global input_file |
| 1262 | global insnwidth |
| 1263 | global insntype |
Bastian Koppelmann | 83d7c40 | 2018-03-19 12:58:46 +0100 | [diff] [blame] | 1264 | global insnmask |
Richard Henderson | abd04f9 | 2018-10-23 10:26:25 +0100 | [diff] [blame] | 1265 | global decode_function |
Richard Henderson | 17560e9 | 2019-01-30 18:01:29 -0800 | [diff] [blame] | 1266 | global variablewidth |
Richard Henderson | c692079 | 2019-08-09 08:12:50 -0700 | [diff] [blame] | 1267 | global anyextern |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1268 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1269 | decode_scope = 'static ' |
| 1270 | |
Richard Henderson | cd3e7fc | 2019-02-23 17:44:31 -0800 | [diff] [blame] | 1271 | long_opts = ['decode=', 'translate=', 'output=', 'insnwidth=', |
Richard Henderson | 17560e9 | 2019-01-30 18:01:29 -0800 | [diff] [blame] | 1272 | 'static-decode=', 'varinsnwidth='] |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1273 | try: |
Paolo Bonzini | abff1ab | 2020-08-07 12:10:23 +0200 | [diff] [blame] | 1274 | (opts, args) = getopt.gnu_getopt(sys.argv[1:], 'o:vw:', long_opts) |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1275 | except getopt.GetoptError as err: |
| 1276 | error(0, err) |
| 1277 | for o, a in opts: |
| 1278 | if o in ('-o', '--output'): |
| 1279 | output_file = a |
| 1280 | elif o == '--decode': |
| 1281 | decode_function = a |
| 1282 | decode_scope = '' |
Richard Henderson | cd3e7fc | 2019-02-23 17:44:31 -0800 | [diff] [blame] | 1283 | elif o == '--static-decode': |
| 1284 | decode_function = a |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1285 | elif o == '--translate': |
| 1286 | translate_prefix = a |
| 1287 | translate_scope = '' |
Richard Henderson | 17560e9 | 2019-01-30 18:01:29 -0800 | [diff] [blame] | 1288 | elif o in ('-w', '--insnwidth', '--varinsnwidth'): |
| 1289 | if o == '--varinsnwidth': |
| 1290 | variablewidth = True |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1291 | insnwidth = int(a) |
| 1292 | if insnwidth == 16: |
| 1293 | insntype = 'uint16_t' |
| 1294 | insnmask = 0xffff |
| 1295 | elif insnwidth != 32: |
| 1296 | error(0, 'cannot handle insns of width', insnwidth) |
| 1297 | else: |
| 1298 | assert False, 'unhandled option' |
| 1299 | |
| 1300 | if len(args) < 1: |
| 1301 | error(0, 'missing input file') |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 1302 | |
| 1303 | toppat = ExcMultiPattern(0) |
| 1304 | |
Richard Henderson | 6699ae6 | 2018-10-26 14:59:43 +0100 | [diff] [blame] | 1305 | for filename in args: |
| 1306 | input_file = filename |
| 1307 | f = open(filename, 'r') |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 1308 | parse_file(f, toppat) |
Richard Henderson | 6699ae6 | 2018-10-26 14:59:43 +0100 | [diff] [blame] | 1309 | f.close() |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1310 | |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 1311 | # We do not want to compute masks for toppat, because those masks |
| 1312 | # are used as a starting point for build_tree. For toppat, we must |
| 1313 | # insist that decode begins from naught. |
| 1314 | for i in toppat.pats: |
| 1315 | i.prop_masks() |
Richard Henderson | 70e0711 | 2019-01-31 11:34:11 -0800 | [diff] [blame] | 1316 | |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 1317 | toppat.build_tree() |
| 1318 | toppat.prop_format() |
| 1319 | |
| 1320 | if variablewidth: |
| 1321 | for i in toppat.pats: |
| 1322 | i.prop_width() |
| 1323 | stree = build_size_tree(toppat.pats, 8, 0, 0) |
| 1324 | prop_size(stree) |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1325 | |
| 1326 | if output_file: |
| 1327 | output_fd = open(output_file, 'w') |
| 1328 | else: |
| 1329 | output_fd = sys.stdout |
| 1330 | |
| 1331 | output_autogen() |
| 1332 | for n in sorted(arguments.keys()): |
| 1333 | f = arguments[n] |
| 1334 | f.output_def() |
| 1335 | |
| 1336 | # A single translate function can be invoked for different patterns. |
| 1337 | # Make sure that the argument sets are the same, and declare the |
| 1338 | # function only once. |
Richard Henderson | c692079 | 2019-08-09 08:12:50 -0700 | [diff] [blame] | 1339 | # |
| 1340 | # If we're sharing formats, we're likely also sharing trans_* functions, |
| 1341 | # but we can't tell which ones. Prevent issues from the compiler by |
| 1342 | # suppressing redundant declaration warnings. |
| 1343 | if anyextern: |
Thomas Huth | 7aa12aa | 2020-07-08 20:19:44 +0200 | [diff] [blame] | 1344 | output("#pragma GCC diagnostic push\n", |
| 1345 | "#pragma GCC diagnostic ignored \"-Wredundant-decls\"\n", |
| 1346 | "#ifdef __clang__\n" |
Richard Henderson | c692079 | 2019-08-09 08:12:50 -0700 | [diff] [blame] | 1347 | "# pragma GCC diagnostic ignored \"-Wtypedef-redefinition\"\n", |
Richard Henderson | c692079 | 2019-08-09 08:12:50 -0700 | [diff] [blame] | 1348 | "#endif\n\n") |
| 1349 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1350 | out_pats = {} |
Richard Henderson | 0eff2df | 2019-02-23 11:35:36 -0800 | [diff] [blame] | 1351 | for i in allpatterns: |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1352 | if i.name in out_pats: |
| 1353 | p = out_pats[i.name] |
| 1354 | if i.base.base != p.base.base: |
| 1355 | error(0, i.name, ' has conflicting argument sets') |
| 1356 | else: |
| 1357 | i.output_decl() |
| 1358 | out_pats[i.name] = i |
| 1359 | output('\n') |
| 1360 | |
Richard Henderson | c692079 | 2019-08-09 08:12:50 -0700 | [diff] [blame] | 1361 | if anyextern: |
Thomas Huth | 7aa12aa | 2020-07-08 20:19:44 +0200 | [diff] [blame] | 1362 | output("#pragma GCC diagnostic pop\n\n") |
Richard Henderson | c692079 | 2019-08-09 08:12:50 -0700 | [diff] [blame] | 1363 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1364 | for n in sorted(formats.keys()): |
| 1365 | f = formats[n] |
| 1366 | f.output_extract() |
| 1367 | |
| 1368 | output(decode_scope, 'bool ', decode_function, |
| 1369 | '(DisasContext *ctx, ', insntype, ' insn)\n{\n') |
| 1370 | |
| 1371 | i4 = str_indent(4) |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1372 | |
Richard Henderson | 82bfac1 | 2019-02-27 21:37:32 -0800 | [diff] [blame] | 1373 | if len(allpatterns) != 0: |
| 1374 | output(i4, 'union {\n') |
| 1375 | for n in sorted(arguments.keys()): |
| 1376 | f = arguments[n] |
| 1377 | output(i4, i4, f.struct_name(), ' f_', f.name, ';\n') |
| 1378 | output(i4, '} u;\n\n') |
Richard Henderson | 08561fc | 2020-05-17 10:14:11 -0700 | [diff] [blame] | 1379 | toppat.output_code(4, False, 0, 0) |
Richard Henderson | 82bfac1 | 2019-02-27 21:37:32 -0800 | [diff] [blame] | 1380 | |
Richard Henderson | eb6b87f | 2019-02-23 08:57:46 -0800 | [diff] [blame] | 1381 | output(i4, 'return false;\n') |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1382 | output('}\n') |
| 1383 | |
Richard Henderson | 70e0711 | 2019-01-31 11:34:11 -0800 | [diff] [blame] | 1384 | if variablewidth: |
| 1385 | output('\n', decode_scope, insntype, ' ', decode_function, |
| 1386 | '_load(DisasContext *ctx)\n{\n', |
| 1387 | ' ', insntype, ' insn = 0;\n\n') |
| 1388 | stree.output_code(4, 0, 0, 0) |
| 1389 | output('}\n') |
| 1390 | |
Richard Henderson | 568ae7e | 2017-12-07 12:44:09 -0800 | [diff] [blame] | 1391 | if output_file: |
| 1392 | output_fd.close() |
| 1393 | # end main |
| 1394 | |
| 1395 | |
| 1396 | if __name__ == '__main__': |
| 1397 | main() |