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