| 1 |
# encoding=utf-8 |
| 2 |
|
| 3 |
# Copyright © 2022 Imagination Technologies Ltd. |
| 4 |
|
| 5 |
# based on anv driver gen_pack_header.py which is: |
| 6 |
# Copyright © 2016 Intel Corporation |
| 7 |
|
| 8 |
# based on v3dv driver gen_pack_header.py which is: |
| 9 |
# Copyright (C) 2016 Broadcom |
| 10 |
|
| 11 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 |
# of this software and associated documentation files (the "Software"), to deal |
| 13 |
# in the Software without restriction, including without limitation the rights |
| 14 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 |
# copies of the Software, and to permit persons to whom the Software is |
| 16 |
# furnished to do so, subject to the following conditions: |
| 17 |
|
| 18 |
# The above copyright notice and this permission notice (including the next |
| 19 |
# paragraph) shall be included in all copies or substantial portions of the |
| 20 |
# Software. |
| 21 |
|
| 22 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 25 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 26 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 27 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 28 |
# SOFTWARE. |
| 29 |
|
| 30 |
from __future__ import annotations |
| 31 |
|
| 32 |
import copy |
| 33 |
import os |
| 34 |
import textwrap |
| 35 |
import typing as t |
| 36 |
import xml.parsers.expat as expat |
| 37 |
from abc import ABC |
| 38 |
from ast import literal_eval |
| 39 |
|
| 40 |
|
| 41 |
MIT_LICENSE_COMMENT = """/* |
| 42 |
* Copyright © %(copyright)s |
| 43 |
* |
| 44 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 45 |
* of this software and associated documentation files (the "Software"), to deal |
| 46 |
* in the Software without restriction, including without limitation the rights |
| 47 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 48 |
* copies of the Software, and to permit persons to whom the Software is |
| 49 |
* furnished to do so, subject to the following conditions: |
| 50 |
* |
| 51 |
* The above copyright notice and this permission notice (including the next |
| 52 |
* paragraph) shall be included in all copies or substantial portions of the |
| 53 |
* Software. |
| 54 |
* |
| 55 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 56 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 57 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 58 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 59 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 60 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 61 |
* SOFTWARE. |
| 62 |
*/""" |
| 63 |
|
| 64 |
PACK_FILE_HEADER = """%(license)s |
| 65 |
|
| 66 |
/* Enums, structures and pack functions for %(platform)s. |
| 67 |
* |
| 68 |
* This file has been generated, do not hand edit. |
| 69 |
*/ |
| 70 |
|
| 71 |
#ifndef %(guard)s |
| 72 |
#define %(guard)s |
| 73 |
|
| 74 |
#include "csbgen/pvr_packet_helpers.h" |
| 75 |
|
| 76 |
#ifndef __OPENCL_VERSION__ |
| 77 |
#define __constant |
| 78 |
#endif |
| 79 |
|
| 80 |
""" |
| 81 |
|
| 82 |
|
| 83 |
def safe_name(name: str) -> str: |
| 84 |
if not name[0].isalpha(): |
| 85 |
name = "_" + name |
| 86 |
|
| 87 |
return name |
| 88 |
|
| 89 |
|
| 90 |
def num_from_str(num_str: str) -> int: |
| 91 |
if num_str.lower().startswith("0x"): |
| 92 |
return int(num_str, base=16) |
| 93 |
|
| 94 |
if num_str.startswith("0") and len(num_str) > 1: |
| 95 |
raise ValueError("Octal numbers not allowed") |
| 96 |
|
| 97 |
return int(num_str) |
| 98 |
|
| 99 |
|
| 100 |
class Node(ABC): |
| 101 |
__slots__ = ["parent", "name"] |
| 102 |
|
| 103 |
parent: Node |
| 104 |
name: str |
| 105 |
|
| 106 |
def __init__(self, parent: Node, name: str, *, name_is_safe: bool = False) -> None: |
| 107 |
self.parent = parent |
| 108 |
if name_is_safe: |
| 109 |
self.name = name |
| 110 |
else: |
| 111 |
self.name = safe_name(name) |
| 112 |
|
| 113 |
@property |
| 114 |
def full_name(self) -> str: |
| 115 |
if self.name[0] == "_": |
| 116 |
return self.parent.prefix + self.name.upper() |
| 117 |
|
| 118 |
return self.parent.prefix + "_" + self.name.upper() |
| 119 |
|
| 120 |
@property |
| 121 |
def prefix(self) -> str: |
| 122 |
return self.parent.prefix |
| 123 |
|
| 124 |
def add(self, element: Node) -> None: |
| 125 |
raise RuntimeError("Element cannot be nested in %s. Element Type: %s" |
| 126 |
% (type(self).__name__.lower(), type(element).__name__)) |
| 127 |
|
| 128 |
|
| 129 |
class Csbgen(Node): |
| 130 |
__slots__ = ["prefix_field", "filename", "_defines", "_enums", "_structs", "_streams"] |
| 131 |
|
| 132 |
prefix_field: str |
| 133 |
filename: str |
| 134 |
_defines: t.List[Define] |
| 135 |
_enums: t.Dict[str, Enum] |
| 136 |
_structs: t.Dict[str, Struct] |
| 137 |
_streams: t.Dict[str, Stream] |
| 138 |
|
| 139 |
def __init__(self, name: str, prefix: str, filename: str) -> None: |
| 140 |
super().__init__(None, name.upper()) |
| 141 |
self.prefix_field = safe_name(prefix.upper()) |
| 142 |
self.filename = filename |
| 143 |
|
| 144 |
self._defines = [] |
| 145 |
self._enums = {} |
| 146 |
self._structs = {} |
| 147 |
self._streams = {} |
| 148 |
|
| 149 |
@property |
| 150 |
def full_name(self) -> str: |
| 151 |
return self.name + "_" + self.prefix_field |
| 152 |
|
| 153 |
@property |
| 154 |
def prefix(self) -> str: |
| 155 |
return self.full_name |
| 156 |
|
| 157 |
def add(self, element: Node) -> None: |
| 158 |
if isinstance(element, Enum): |
| 159 |
if element.name in self._enums: |
| 160 |
raise RuntimeError("Enum redefined. Enum: %s" % element.name) |
| 161 |
|
| 162 |
self._enums[element.name] = element |
| 163 |
elif isinstance(element, Struct): |
| 164 |
if element.name in self._structs: |
| 165 |
raise RuntimeError("Struct redefined. Struct: %s" % element.name) |
| 166 |
|
| 167 |
self._structs[element.name] = element |
| 168 |
elif isinstance(element, Stream): |
| 169 |
if element.name in self._streams: |
| 170 |
raise RuntimeError("Stream redefined. Stream: %s" % element.name) |
| 171 |
|
| 172 |
self._streams[element.name] = element |
| 173 |
elif isinstance(element, Define): |
| 174 |
define_names = [d.full_name for d in self._defines] |
| 175 |
if element.full_name in define_names: |
| 176 |
raise RuntimeError("Define redefined. Define: %s" % element.full_name) |
| 177 |
|
| 178 |
self._defines.append(element) |
| 179 |
else: |
| 180 |
super().add(element) |
| 181 |
|
| 182 |
def _gen_guard(self) -> str: |
| 183 |
return os.path.basename(self.filename).replace(".xml", "_h").upper() |
| 184 |
|
| 185 |
def emit(self) -> None: |
| 186 |
print(PACK_FILE_HEADER % { |
| 187 |
"license": MIT_LICENSE_COMMENT % {"copyright": "2022 Imagination Technologies Ltd."}, |
| 188 |
"platform": self.name, |
| 189 |
"guard": self._gen_guard(), |
| 190 |
}) |
| 191 |
|
| 192 |
for define in self._defines: |
| 193 |
define.emit() |
| 194 |
|
| 195 |
print() |
| 196 |
|
| 197 |
for enum in self._enums.values(): |
| 198 |
enum.emit() |
| 199 |
|
| 200 |
for struct in self._structs.values(): |
| 201 |
struct.emit(self) |
| 202 |
|
| 203 |
for stream in self._streams.values(): |
| 204 |
stream.emit(self) |
| 205 |
|
| 206 |
print("#endif /* %s */" % self._gen_guard()) |
| 207 |
|
| 208 |
def is_known_struct(self, struct_name: str) -> bool: |
| 209 |
return struct_name in self._structs.keys() |
| 210 |
|
| 211 |
def is_known_enum(self, enum_name: str) -> bool: |
| 212 |
return enum_name in self._enums.keys() |
| 213 |
|
| 214 |
def get_enum(self, enum_name: str) -> Enum: |
| 215 |
return self._enums[enum_name] |
| 216 |
|
| 217 |
def get_struct(self, struct_name: str) -> Struct: |
| 218 |
return self._structs[struct_name] |
| 219 |
|
| 220 |
|
| 221 |
class Enum(Node): |
| 222 |
__slots__ = ["_values"] |
| 223 |
|
| 224 |
_values: t.Dict[str, Value] |
| 225 |
|
| 226 |
def __init__(self, parent: Node, name: str) -> None: |
| 227 |
super().__init__(parent, name) |
| 228 |
|
| 229 |
self._values = {} |
| 230 |
|
| 231 |
self.parent.add(self) |
| 232 |
|
| 233 |
# We override prefix so that the values will contain the enum's name too. |
| 234 |
@property |
| 235 |
def prefix(self) -> str: |
| 236 |
return self.full_name |
| 237 |
|
| 238 |
def get_value(self, value_name: str) -> Value: |
| 239 |
return self._values[value_name] |
| 240 |
|
| 241 |
def add(self, element: Node) -> None: |
| 242 |
if not isinstance(element, Value): |
| 243 |
super().add(element) |
| 244 |
|
| 245 |
if element.name in self._values: |
| 246 |
raise RuntimeError("Value is being redefined. Value: '%s'" % element.name) |
| 247 |
|
| 248 |
if element.value in self._values.values(): |
| 249 |
raise RuntimeError("Ambiguous enum value detected. Value: '%s'" % element.value) |
| 250 |
|
| 251 |
self._values[element.name] = element |
| 252 |
|
| 253 |
def _emit_to_str(self) -> None: |
| 254 |
print(textwrap.dedent("""\ |
| 255 |
static __constant const char * |
| 256 |
%s_to_str(const enum %s value) |
| 257 |
{""") % (self.full_name, self.full_name)) |
| 258 |
|
| 259 |
print(" switch (value) {") |
| 260 |
for value in self._values.values(): |
| 261 |
print(" case %s: return \"%s\";" % (value.full_name, value.name)) |
| 262 |
print(" default: return NULL;") |
| 263 |
print(" }") |
| 264 |
|
| 265 |
print("}\n") |
| 266 |
|
| 267 |
def emit(self) -> None: |
| 268 |
# This check is invalid if tags other than Value can be nested within an enum. |
| 269 |
if not self._values.values(): |
| 270 |
raise RuntimeError("Enum definition is empty. Enum: '%s'" % self.full_name) |
| 271 |
|
| 272 |
print("enum %s {" % self.full_name) |
| 273 |
for value in self._values.values(): |
| 274 |
value.emit() |
| 275 |
print("};\n") |
| 276 |
|
| 277 |
self._emit_to_str() |
| 278 |
|
| 279 |
|
| 280 |
class Value(Node): |
| 281 |
__slots__ = ["value"] |
| 282 |
|
| 283 |
value: int |
| 284 |
|
| 285 |
def __init__(self, parent: Node, name: str, value: int) -> None: |
| 286 |
super().__init__(parent, name) |
| 287 |
|
| 288 |
self.value = value |
| 289 |
|
| 290 |
self.parent.add(self) |
| 291 |
|
| 292 |
def emit(self): |
| 293 |
print(" %-36s = %6d," % (self.full_name, self.value)) |
| 294 |
|
| 295 |
|
| 296 |
class Struct(Node): |
| 297 |
__slots__ = ["length", "size", "_children"] |
| 298 |
|
| 299 |
length: int |
| 300 |
size: int |
| 301 |
_children: t.Dict[str, t.Union[Condition, Field]] |
| 302 |
|
| 303 |
def __init__(self, parent: Node, name: str, length: int) -> None: |
| 304 |
super().__init__(parent, name) |
| 305 |
|
| 306 |
self.length = length |
| 307 |
self.size = self.length * 32 |
| 308 |
|
| 309 |
if self.length <= 0: |
| 310 |
raise ValueError("Struct length must be greater than 0. Struct: '%s'." % self.full_name) |
| 311 |
|
| 312 |
self._children = {} |
| 313 |
|
| 314 |
self.parent.add(self) |
| 315 |
|
| 316 |
@property |
| 317 |
def fields(self) -> t.List[Field]: |
| 318 |
# TODO: Should we cache? See TODO in equivalent Condition getter. |
| 319 |
|
| 320 |
fields = [] |
| 321 |
for child in self._children.values(): |
| 322 |
if isinstance(child, Condition): |
| 323 |
fields += child.fields |
| 324 |
else: |
| 325 |
fields.append(child) |
| 326 |
|
| 327 |
return fields |
| 328 |
|
| 329 |
@property |
| 330 |
def prefix(self) -> str: |
| 331 |
return self.full_name |
| 332 |
|
| 333 |
def add(self, element: Node) -> None: |
| 334 |
# We don't support conditions and field having the same name. |
| 335 |
if isinstance(element, Field): |
| 336 |
if element.name in self._children.keys(): |
| 337 |
raise ValueError("Field is being redefined. Field: '%s', Struct: '%s'" |
| 338 |
% (element.name, self.full_name)) |
| 339 |
|
| 340 |
self._children[element.name] = element |
| 341 |
|
| 342 |
elif isinstance(element, Condition): |
| 343 |
# We only save ifs, and ignore the rest. The rest will be linked to |
| 344 |
# the if condition so we just need to call emit() on the if and the |
| 345 |
# rest will also be emitted. |
| 346 |
if element.type == "if": |
| 347 |
self._children[element.name] = element |
| 348 |
else: |
| 349 |
if element.name not in self._children.keys(): |
| 350 |
raise RuntimeError("Unknown condition: '%s'" % element.name) |
| 351 |
|
| 352 |
else: |
| 353 |
super().add(element) |
| 354 |
|
| 355 |
def _emit_header(self, root: Csbgen) -> None: |
| 356 |
default_fields = [] |
| 357 |
for field in (f for f in self.fields if f.default is not None): |
| 358 |
if field.is_builtin_type: |
| 359 |
default_fields.append(" .%-35s = %6d" % (field.name, field.default)) |
| 360 |
else: |
| 361 |
if not root.is_known_enum(field.type): |
| 362 |
# Default values should not apply to structures |
| 363 |
raise RuntimeError( |
| 364 |
"Unknown type. Field: '%s' Type: '%s'" |
| 365 |
% (field.name, field.type) |
| 366 |
) |
| 367 |
|
| 368 |
enum = root.get_enum(field.type) |
| 369 |
|
| 370 |
try: |
| 371 |
value = enum.get_value(field.default) |
| 372 |
except KeyError: |
| 373 |
raise ValueError("Unknown enum value. Value: '%s', Enum: '%s', Field: '%s'" |
| 374 |
% (field.default, enum.full_name, field.name)) |
| 375 |
|
| 376 |
default_fields.append(" .%-35s = %s" % (field.name, value.full_name)) |
| 377 |
|
| 378 |
print("#define %-40s\\" % (self.full_name + "_header")) |
| 379 |
print(", \\\n".join(default_fields)) |
| 380 |
print("") |
| 381 |
|
| 382 |
def _emit_helper_macros(self) -> None: |
| 383 |
for field in (f for f in self.fields if f.defines): |
| 384 |
print("/* Helper macros for %s */" % field.name) |
| 385 |
|
| 386 |
for define in field.defines: |
| 387 |
define.emit() |
| 388 |
|
| 389 |
print() |
| 390 |
|
| 391 |
def _emit_pack_function(self, root: Csbgen) -> None: |
| 392 |
print(textwrap.dedent("""\ |
| 393 |
static inline __attribute__((always_inline)) void |
| 394 |
%s_pack(__attribute__((unused)) void * restrict dst, |
| 395 |
%s__attribute__((unused)) const struct %s * restrict values) |
| 396 |
{""") % (self.full_name, ' ' * len(self.full_name), self.full_name)) |
| 397 |
|
| 398 |
group = Group(0, 1, self.size, self.fields) |
| 399 |
dwords, length = group.collect_dwords_and_length() |
| 400 |
if length: |
| 401 |
# Cast dst to make header C++ friendly |
| 402 |
print(" uint32_t * restrict dw = (uint32_t * restrict) dst;") |
| 403 |
|
| 404 |
group.emit_pack_function(root, dwords, length) |
| 405 |
|
| 406 |
print("}\n") |
| 407 |
|
| 408 |
def _emit_unpack_function(self, root: Csbgen) -> None: |
| 409 |
print(textwrap.dedent("""\ |
| 410 |
static inline __attribute__((always_inline)) void |
| 411 |
%s_unpack(__attribute__((unused)) const void * restrict src, |
| 412 |
%s__attribute__((unused)) struct %s * restrict values) |
| 413 |
{""") % (self.full_name, ' ' * len(self.full_name), self.full_name)) |
| 414 |
|
| 415 |
group = Group(0, 1, self.size, self.fields) |
| 416 |
dwords, length = group.collect_dwords_and_length() |
| 417 |
if length: |
| 418 |
# Cast src to make header C++ friendly |
| 419 |
print(" const uint32_t * restrict dw = (const uint32_t * restrict) src;") |
| 420 |
|
| 421 |
group.emit_unpack_function(root, dwords, length) |
| 422 |
|
| 423 |
print("}\n") |
| 424 |
|
| 425 |
def emit(self, root: Csbgen) -> None: |
| 426 |
print("#define %-33s %6d" % (self.full_name + "_length", self.length)) |
| 427 |
|
| 428 |
self._emit_header(root) |
| 429 |
|
| 430 |
self._emit_helper_macros() |
| 431 |
|
| 432 |
print("struct %s {" % self.full_name) |
| 433 |
for child in self._children.values(): |
| 434 |
child.emit(root) |
| 435 |
print("};\n") |
| 436 |
|
| 437 |
self._emit_pack_function(root) |
| 438 |
self._emit_unpack_function(root) |
| 439 |
|
| 440 |
|
| 441 |
class Stream(Node): |
| 442 |
__slots__ = ["length", "size", "_children"] |
| 443 |
|
| 444 |
length: int |
| 445 |
size: int |
| 446 |
_children: t.Dict[str, t.Union[Condition, Field]] |
| 447 |
|
| 448 |
def __init__(self, parent: Node, name: str, length: int) -> None: |
| 449 |
self._children = {} |
| 450 |
|
| 451 |
super().__init__(parent, name) |
| 452 |
|
| 453 |
@property |
| 454 |
def fields(self) -> t.List[Field]: |
| 455 |
fields = [] |
| 456 |
|
| 457 |
@property |
| 458 |
def prefix(self) -> str: |
| 459 |
return self.full_name |
| 460 |
|
| 461 |
def add(self, element: Node) -> None: |
| 462 |
# We don't support conditions and field having the same name. |
| 463 |
if isinstance(element, Field): |
| 464 |
if element.name in self._children.keys(): |
| 465 |
raise ValueError("Field is being redefined. Field: '%s', Struct: '%s'" |
| 466 |
% (element.name, self.full_name)) |
| 467 |
|
| 468 |
self._children[element.name] = element |
| 469 |
|
| 470 |
elif isinstance(element, Condition): |
| 471 |
# We only save ifs, and ignore the rest. The rest will be linked to |
| 472 |
# the if condition so we just need to call emit() on the if and the |
| 473 |
# rest will also be emitted. |
| 474 |
if element.type == "if": |
| 475 |
self._children[element.name] = element |
| 476 |
else: |
| 477 |
if element.name not in self._children.keys(): |
| 478 |
raise RuntimeError("Unknown condition: '%s'" % element.name) |
| 479 |
|
| 480 |
else: |
| 481 |
super().add(element) |
| 482 |
|
| 483 |
def _emit_header(self, root: Csbgen) -> None: |
| 484 |
pass |
| 485 |
|
| 486 |
def _emit_helper_macros(self) -> None: |
| 487 |
pass |
| 488 |
|
| 489 |
def _emit_pack_function(self, root: Csbgen) -> None: |
| 490 |
pass |
| 491 |
|
| 492 |
def _emit_unpack_function(self, root: Csbgen) -> None: |
| 493 |
pass |
| 494 |
|
| 495 |
def emit(self, root: Csbgen) -> None: |
| 496 |
pass |
| 497 |
|
| 498 |
class Field(Node): |
| 499 |
__slots__ = ["start", "end", "type", "default", "shift", "_defines"] |
| 500 |
|
| 501 |
start: int |
| 502 |
end: int |
| 503 |
type: str |
| 504 |
default: t.Optional[t.Union[str, int]] |
| 505 |
shift: t.Optional[int] |
| 506 |
_defines: t.Dict[str, Define] |
| 507 |
|
| 508 |
def __init__(self, parent: Node, name: str, start: int, end: int, ty: str, *, |
| 509 |
default: t.Optional[str] = None, shift: t.Optional[int] = None) -> None: |
| 510 |
super().__init__(parent, name) |
| 511 |
|
| 512 |
self.start = start |
| 513 |
self.end = end |
| 514 |
self.type = ty |
| 515 |
|
| 516 |
self._defines = {} |
| 517 |
|
| 518 |
self.parent.add(self) |
| 519 |
|
| 520 |
if self.start > self.end: |
| 521 |
raise ValueError("Start cannot be after end. Start: %d, End: %d, Field: '%s'" |
| 522 |
% (self.start, self.end, self.name)) |
| 523 |
|
| 524 |
if self.type == "bool" and self.end != self.start: |
| 525 |
raise ValueError("Bool field can only be 1 bit long. Field '%s'" % self.name) |
| 526 |
|
| 527 |
if default is not None: |
| 528 |
if not self.is_builtin_type: |
| 529 |
# Assuming it's an enum type. |
| 530 |
self.default = safe_name(default) |
| 531 |
else: |
| 532 |
self.default = num_from_str(default) |
| 533 |
else: |
| 534 |
self.default = None |
| 535 |
|
| 536 |
if shift is not None: |
| 537 |
if self.type != "address": |
| 538 |
raise RuntimeError("Only address fields can have a shift attribute. Field: '%s'" % self.name) |
| 539 |
|
| 540 |
self.shift = int(shift) |
| 541 |
|
| 542 |
Define(self, "ALIGNMENT", 2**self.shift) |
| 543 |
else: |
| 544 |
if self.type == "address": |
| 545 |
raise RuntimeError("Field of address type requires a shift attribute. Field '%s'" % self.name) |
| 546 |
|
| 547 |
self.shift = None |
| 548 |
|
| 549 |
@property |
| 550 |
def defines(self) -> t.Iterator[Define]: |
| 551 |
return self._defines.values() |
| 552 |
|
| 553 |
# We override prefix so that the defines will contain the field's name too. |
| 554 |
@property |
| 555 |
def prefix(self) -> str: |
| 556 |
return self.full_name |
| 557 |
|
| 558 |
@property |
| 559 |
def is_builtin_type(self) -> bool: |
| 560 |
builtins = {"address", "bool", "float", "mbo", "offset", "int", "uint"} |
| 561 |
return self.type in builtins |
| 562 |
|
| 563 |
def _get_c_type(self, root: Csbgen) -> str: |
| 564 |
if self.type == "address": |
| 565 |
return "__pvr_address_type" |
| 566 |
elif self.type == "bool": |
| 567 |
return "bool" |
| 568 |
elif self.type == "float": |
| 569 |
return "float" |
| 570 |
elif self.type == "offset": |
| 571 |
return "uint64_t" |
| 572 |
elif self.type == "int": |
| 573 |
return "int32_t" |
| 574 |
elif self.type == "uint": |
| 575 |
if self.end - self.start <= 32: |
| 576 |
return "uint32_t" |
| 577 |
elif self.end - self.start <= 64: |
| 578 |
return "uint64_t" |
| 579 |
|
| 580 |
raise RuntimeError("No known C type found to hold %d bit sized value. Field: '%s'" |
| 581 |
% (self.end - self.start, self.name)) |
| 582 |
elif self.type == "uint_array": |
| 583 |
return "uint8_t" |
| 584 |
elif root.is_known_struct(self.type): |
| 585 |
return "struct " + self.type |
| 586 |
elif root.is_known_enum(self.type): |
| 587 |
return "enum " + root.get_enum(self.type).full_name |
| 588 |
raise RuntimeError("Unknown type. Type: '%s', Field: '%s'" % (self.type, self.name)) |
| 589 |
|
| 590 |
def add(self, element: Node) -> None: |
| 591 |
if self.type == "mbo": |
| 592 |
raise RuntimeError("No element can be nested in an mbo field. Element Type: %s, Field: %s" |
| 593 |
% (type(element).__name__, self.name)) |
| 594 |
|
| 595 |
if isinstance(element, Define): |
| 596 |
if element.name in self._defines: |
| 597 |
raise RuntimeError("Duplicate define. Define: '%s'" % element.name) |
| 598 |
|
| 599 |
self._defines[element.name] = element |
| 600 |
else: |
| 601 |
super().add(element) |
| 602 |
|
| 603 |
def emit(self, root: Csbgen) -> None: |
| 604 |
if self.type == "mbo": |
| 605 |
return |
| 606 |
|
| 607 |
if self.type == "uint_array": |
| 608 |
print(" %-36s %s[%u];" % (self._get_c_type(root), self.name, (self.end - self.start) / 8)) |
| 609 |
else: |
| 610 |
print(" %-36s %s;" % (self._get_c_type(root), self.name)) |
| 611 |
|
| 612 |
|
| 613 |
class Define(Node): |
| 614 |
__slots__ = ["value"] |
| 615 |
|
| 616 |
value: int |
| 617 |
|
| 618 |
def __init__(self, parent: Node, name: str, value: int) -> None: |
| 619 |
super().__init__(parent, name) |
| 620 |
|
| 621 |
self.value = value |
| 622 |
|
| 623 |
self.parent.add(self) |
| 624 |
|
| 625 |
def emit(self) -> None: |
| 626 |
print("#define %-40s %d" % (self.full_name, self.value)) |
| 627 |
|
| 628 |
|
| 629 |
class Condition(Node): |
| 630 |
__slots__ = ["type", "_children", "_child_branch"] |
| 631 |
|
| 632 |
type: str |
| 633 |
_children: t.Dict[str, t.Union[Condition, Field]] |
| 634 |
_child_branch: t.Optional[Condition] |
| 635 |
|
| 636 |
def __init__(self, parent: Node, name: str, ty: str) -> None: |
| 637 |
super().__init__(parent, name, name_is_safe=True) |
| 638 |
|
| 639 |
self.type = ty |
| 640 |
if not Condition._is_valid_type(self.type): |
| 641 |
raise RuntimeError("Unknown type: '%s'" % self.name) |
| 642 |
|
| 643 |
self._children = {} |
| 644 |
|
| 645 |
# This is the link to the next branch for the if statement so either |
| 646 |
# elif, else, or endif. They themselves will also have a link to the |
| 647 |
# next branch up until endif which terminates the chain. |
| 648 |
self._child_branch = None |
| 649 |
|
| 650 |
self.parent.add(self) |
| 651 |
|
| 652 |
@property |
| 653 |
def fields(self) -> t.List[Field]: |
| 654 |
# TODO: Should we use some kind of state to indicate the all of the |
| 655 |
# child nodes have been added and then cache the fields in here on the |
| 656 |
# first call so that we don't have to traverse them again per each call? |
| 657 |
# The state could be changed wither when we reach the endif and pop from |
| 658 |
# the context, or when we start emitting. |
| 659 |
|
| 660 |
fields = [] |
| 661 |
|
| 662 |
for child in self._children.values(): |
| 663 |
if isinstance(child, Condition): |
| 664 |
fields += child.fields |
| 665 |
else: |
| 666 |
fields.append(child) |
| 667 |
|
| 668 |
if self._child_branch is not None: |
| 669 |
fields += self._child_branch.fields |
| 670 |
|
| 671 |
return fields |
| 672 |
|
| 673 |
@staticmethod |
| 674 |
def _is_valid_type(ty: str) -> bool: |
| 675 |
types = {"if", "elif", "else", "endif"} |
| 676 |
return ty in types |
| 677 |
|
| 678 |
def _is_compatible_child_branch(self, branch): |
| 679 |
types = ["if", "elif", "else", "endif"] |
| 680 |
idx = types.index(self.type) |
| 681 |
return (branch.type in types[idx + 1:] or |
| 682 |
self.type == "elif" and branch.type == "elif") |
| 683 |
|
| 684 |
def _add_branch(self, branch: Condition) -> None: |
| 685 |
if branch.type == "elif" and branch.name == self.name: |
| 686 |
raise RuntimeError("Elif branch cannot have same check as previous branch. Check: '%s'" % branch.name) |
| 687 |
|
| 688 |
if not self._is_compatible_child_branch(branch): |
| 689 |
raise RuntimeError("Invalid branch. Check: '%s', Type: '%s'" % (branch.name, branch.type)) |
| 690 |
|
| 691 |
self._child_branch = branch |
| 692 |
|
| 693 |
# Returns the name of the if condition. This is used for elif branches since |
| 694 |
# they have a different name than the if condition thus we have to traverse |
| 695 |
# the chain of branches. |
| 696 |
# This is used to discriminate nested if conditions from branches since |
| 697 |
# branches like 'endif' and 'else' will have the same name as the 'if' (the |
| 698 |
# elif is an exception) while nested conditions will have different names. |
| 699 |
# |
| 700 |
# TODO: Redo this to improve speed? Would caching this be helpful? We could |
| 701 |
# just save the name of the if instead of having to walk towards it whenever |
| 702 |
# a new condition is being added. |
| 703 |
def _top_branch_name(self) -> str: |
| 704 |
if self.type == "if": |
| 705 |
return self.name |
| 706 |
|
| 707 |
# If we're not an 'if' condition, our parent must be another condition. |
| 708 |
assert isinstance(self.parent, Condition) |
| 709 |
return self.parent._top_branch_name() |
| 710 |
|
| 711 |
def add(self, element: Node) -> None: |
| 712 |
if isinstance(element, Field): |
| 713 |
if element.name in self._children.keys(): |
| 714 |
raise ValueError("Duplicate field. Field: '%s'" % element.name) |
| 715 |
|
| 716 |
self._children[element.name] = element |
| 717 |
elif isinstance(element, Condition): |
| 718 |
if element.type == "elif" or self._top_branch_name() == element.name: |
| 719 |
self._add_branch(element) |
| 720 |
else: |
| 721 |
if element.type != "if": |
| 722 |
raise RuntimeError("Branch of an unopened if condition. Check: '%s', Type: '%s'." |
| 723 |
% (element.name, element.type)) |
| 724 |
|
| 725 |
# This is a nested condition and we made sure that the name |
| 726 |
# doesn't match _top_branch_name() so we can recognize the else |
| 727 |
# and endif. |
| 728 |
# We recognized the elif by its type however its name differs |
| 729 |
# from the if condition thus when we add an if condition with |
| 730 |
# the same name as the elif nested in it, the _top_branch_name() |
| 731 |
# check doesn't hold true as the name matched the elif and not |
| 732 |
# the if statement which the elif was a branch of, thus the |
| 733 |
# nested if condition is not recognized as an invalid branch of |
| 734 |
# the outer if statement. |
| 735 |
# Sample: |
| 736 |
# <condition type="if" check="ROGUEXE"/> |
| 737 |
# <condition type="elif" check="COMPUTE"/> |
| 738 |
# <condition type="if" check="COMPUTE"/> |
| 739 |
# <condition type="endif" check="COMPUTE"/> |
| 740 |
# <condition type="endif" check="COMPUTE"/> |
| 741 |
# <condition type="endif" check="ROGUEXE"/> |
| 742 |
# |
| 743 |
# We fix this by checking the if condition name against its |
| 744 |
# parent. |
| 745 |
if element.name == self.name: |
| 746 |
raise RuntimeError("Invalid if condition. Check: '%s'" % element.name) |
| 747 |
|
| 748 |
self._children[element.name] = element |
| 749 |
else: |
| 750 |
super().add(element) |
| 751 |
|
| 752 |
def emit(self, root: Csbgen) -> None: |
| 753 |
if self.type == "if": |
| 754 |
print("/* if %s is supported use: */" % self.name) |
| 755 |
elif self.type == "elif": |
| 756 |
print("/* else if %s is supported use: */" % self.name) |
| 757 |
elif self.type == "else": |
| 758 |
print("/* else %s is not-supported use: */" % self.name) |
| 759 |
elif self.type == "endif": |
| 760 |
print("/* endif %s */" % self.name) |
| 761 |
return |
| 762 |
else: |
| 763 |
raise RuntimeError("Unknown condition type. Implementation error.") |
| 764 |
|
| 765 |
for child in self._children.values(): |
| 766 |
child.emit(root) |
| 767 |
|
| 768 |
self._child_branch.emit(root) |
| 769 |
|
| 770 |
|
| 771 |
class Group: |
| 772 |
__slots__ = ["start", "count", "size", "fields"] |
| 773 |
|
| 774 |
start: int |
| 775 |
count: int |
| 776 |
size: int |
| 777 |
fields: t.List[Field] |
| 778 |
|
| 779 |
def __init__(self, start: int, count: int, size: int, fields) -> None: |
| 780 |
self.start = start |
| 781 |
self.count = count |
| 782 |
self.size = size |
| 783 |
self.fields = fields |
| 784 |
|
| 785 |
class DWord: |
| 786 |
__slots__ = ["size", "fields", "addresses"] |
| 787 |
|
| 788 |
size: int |
| 789 |
fields: t.List[Field] |
| 790 |
addresses: t.List[Field] |
| 791 |
|
| 792 |
def __init__(self) -> None: |
| 793 |
self.size = 32 |
| 794 |
self.fields = [] |
| 795 |
self.addresses = [] |
| 796 |
|
| 797 |
def collect_dwords(self, dwords: t.Dict[int, Group.DWord], start: int) -> None: |
| 798 |
for field in self.fields: |
| 799 |
index = (start + field.start) // 32 |
| 800 |
if index not in dwords: |
| 801 |
dwords[index] = self.DWord() |
| 802 |
|
| 803 |
clone = copy.copy(field) |
| 804 |
clone.start = clone.start + start |
| 805 |
clone.end = clone.end + start |
| 806 |
dwords[index].fields.append(clone) |
| 807 |
|
| 808 |
if field.type == "address": |
| 809 |
# assert dwords[index].address == None |
| 810 |
dwords[index].addresses.append(clone) |
| 811 |
|
| 812 |
# Coalesce all the dwords covered by this field. The two cases we |
| 813 |
# handle are where multiple fields are in a 64 bit word (typically |
| 814 |
# and address and a few bits) or where a single struct field |
| 815 |
# completely covers multiple dwords. |
| 816 |
while index < (start + field.end) // 32: |
| 817 |
if index + 1 in dwords and not dwords[index] == dwords[index + 1]: |
| 818 |
dwords[index].fields.extend(dwords[index + 1].fields) |
| 819 |
dwords[index].addresses.extend(dwords[index + 1].addresses) |
| 820 |
dwords[index].size = 64 |
| 821 |
dwords[index + 1] = dwords[index] |
| 822 |
index = index + 1 |
| 823 |
|
| 824 |
def collect_dwords_and_length(self) -> t.Tuple[t.Dict[int, Group.DWord], int]: |
| 825 |
dwords = {} |
| 826 |
self.collect_dwords(dwords, 0) |
| 827 |
|
| 828 |
# Determine number of dwords in this group. If we have a size, use |
| 829 |
# that, since that'll account for MBZ dwords at the end of a group |
| 830 |
# (like dword 8 on BDW+ 3DSTATE_HS). Otherwise, use the largest dword |
| 831 |
# index we've seen plus one. |
| 832 |
if self.size > 0: |
| 833 |
length = self.size // 32 |
| 834 |
elif dwords: |
| 835 |
length = max(dwords.keys()) + 1 |
| 836 |
else: |
| 837 |
length = 0 |
| 838 |
|
| 839 |
return dwords, length |
| 840 |
|
| 841 |
def emit_pack_function(self, root: Csbgen, dwords: t.Dict[int, Group.DWord], length: int) -> None: |
| 842 |
for index in range(length): |
| 843 |
# Handle MBZ dwords |
| 844 |
if index not in dwords: |
| 845 |
print("") |
| 846 |
print(" dw[%d] = 0;" % index) |
| 847 |
continue |
| 848 |
|
| 849 |
# For 64 bit dwords, we aliased the two dword entries in the dword |
| 850 |
# dict it occupies. Now that we're emitting the pack function, |
| 851 |
# skip the duplicate entries. |
| 852 |
dw = dwords[index] |
| 853 |
if index > 0 and index - 1 in dwords and dw == dwords[index - 1]: |
| 854 |
continue |
| 855 |
|
| 856 |
# Special case: only one field and it's a struct at the beginning |
| 857 |
# of the dword. In this case we pack directly into the |
| 858 |
# destination. This is the only way we handle embedded structs |
| 859 |
# larger than 32 bits. |
| 860 |
if len(dw.fields) == 1: |
| 861 |
field = dw.fields[0] |
| 862 |
if root.is_known_struct(field.type) and field.start % 32 == 0: |
| 863 |
print("") |
| 864 |
print(" %s_pack(data, &dw[%d], &values->%s);" |
| 865 |
% (self.parser.gen_prefix(safe_name(field.type)), index, field.name)) |
| 866 |
continue |
| 867 |
|
| 868 |
# Pack any fields of struct type first so we have integer values |
| 869 |
# to the dword for those fields. |
| 870 |
field_index = 0 |
| 871 |
for field in dw.fields: |
| 872 |
if root.is_known_struct(field.type): |
| 873 |
print("") |
| 874 |
print(" uint32_t v%d_%d;" % (index, field_index)) |
| 875 |
print(" %s_pack(data, &v%d_%d, &values->%s);" |
| 876 |
% (self.parser.gen_prefix(safe_name(field.type)), index, field_index, field.name)) |
| 877 |
field_index = field_index + 1 |
| 878 |
|
| 879 |
print("") |
| 880 |
dword_start = index * 32 |
| 881 |
address_count = len(dw.addresses) |
| 882 |
|
| 883 |
if dw.size == 32 and not dw.addresses: |
| 884 |
v = None |
| 885 |
print(" dw[%d] =" % index) |
| 886 |
elif len(dw.fields) > address_count: |
| 887 |
v = "v%d" % index |
| 888 |
print(" const uint%d_t %s =" % (dw.size, v)) |
| 889 |
else: |
| 890 |
v = "0" |
| 891 |
|
| 892 |
field_index = 0 |
| 893 |
non_address_fields = [] |
| 894 |
for field in dw.fields: |
| 895 |
if field.type == "mbo": |
| 896 |
non_address_fields.append("__pvr_mbo(%d, %d)" |
| 897 |
% (field.start - dword_start, field.end - dword_start)) |
| 898 |
elif field.type == "address": |
| 899 |
pass |
| 900 |
elif field.type == "uint": |
| 901 |
non_address_fields.append("__pvr_uint(values->%s, %d, %d)" |
| 902 |
% (field.name, field.start - dword_start, field.end - dword_start)) |
| 903 |
elif root.is_known_enum(field.type): |
| 904 |
non_address_fields.append("__pvr_uint(values->%s, %d, %d)" |
| 905 |
% (field.name, field.start - dword_start, field.end - dword_start)) |
| 906 |
elif field.type == "int": |
| 907 |
non_address_fields.append("__pvr_sint(values->%s, %d, %d)" |
| 908 |
% (field.name, field.start - dword_start, field.end - dword_start)) |
| 909 |
elif field.type == "bool": |
| 910 |
non_address_fields.append("__pvr_uint(values->%s, %d, %d)" |
| 911 |
% (field.name, field.start - dword_start, field.end - dword_start)) |
| 912 |
elif field.type == "float": |
| 913 |
non_address_fields.append("__pvr_float(values->%s)" % field.name) |
| 914 |
elif field.type == "offset": |
| 915 |
non_address_fields.append("__pvr_offset(values->%s, %d, %d)" |
| 916 |
% (field.name, field.start - dword_start, field.end - dword_start)) |
| 917 |
elif field.type == "uint_array": |
| 918 |
pass |
| 919 |
elif field.is_struct_type(): |
| 920 |
non_address_fields.append("__pvr_uint(v%d_%d, %d, %d)" |
| 921 |
% (index, field_index, field.start - dword_start, |
| 922 |
field.end - dword_start)) |
| 923 |
field_index = field_index + 1 |
| 924 |
else: |
| 925 |
non_address_fields.append( |
| 926 |
"/* unhandled field %s," " type %s */\n" % (field.name, field.type) |
| 927 |
) |
| 928 |
|
| 929 |
if non_address_fields: |
| 930 |
print(" |\n".join(" " + f for f in non_address_fields) + ";") |
| 931 |
|
| 932 |
if dw.size == 32: |
| 933 |
for addr in dw.addresses: |
| 934 |
print(" dw[%d] = __pvr_address(values->%s, %d, %d, %d) | %s;" |
| 935 |
% (index, addr.name, addr.shift, addr.start - dword_start, |
| 936 |
addr.end - dword_start, v)) |
| 937 |
continue |
| 938 |
|
| 939 |
v_accumulated_addr = "" |
| 940 |
for i, addr in enumerate(dw.addresses): |
| 941 |
v_address = "v%d_address" % i |
| 942 |
v_accumulated_addr += "v%d_address" % i |
| 943 |
print(" const uint64_t %s =" % v_address) |
| 944 |
print(" __pvr_address(values->%s, %d, %d, %d);" |
| 945 |
% (addr.name, addr.shift, addr.start - dword_start, addr.end - dword_start)) |
| 946 |
if i < (address_count - 1): |
| 947 |
v_accumulated_addr += " |\n " |
| 948 |
|
| 949 |
if dw.addresses: |
| 950 |
if len(dw.fields) > address_count: |
| 951 |
print(" dw[%d] = %s | %s;" % (index, v_accumulated_addr, v)) |
| 952 |
print(" dw[%d] = ((%s) >> 32) | (%s >> 32);" % (index + 1, v_accumulated_addr, v)) |
| 953 |
continue |
| 954 |
else: |
| 955 |
v = v_accumulated_addr |
| 956 |
|
| 957 |
print(" dw[%d] = %s;" % (index, v)) |
| 958 |
print(" dw[%d] = %s >> 32;" % (index + 1, v)) |
| 959 |
|
| 960 |
def emit_unpack_function(self, root: Csbgen, dwords: t.Dict[int, Group.DWord], length: int) -> None: |
| 961 |
for index in range(length): |
| 962 |
# Ignore MBZ dwords |
| 963 |
if index not in dwords: |
| 964 |
continue |
| 965 |
|
| 966 |
# For 64 bit dwords, we aliased the two dword entries in the dword |
| 967 |
# dict it occupies. Now that we're emitting the unpack function, |
| 968 |
# skip the duplicate entries. |
| 969 |
dw = dwords[index] |
| 970 |
if index > 0 and index - 1 in dwords and dw == dwords[index - 1]: |
| 971 |
continue |
| 972 |
|
| 973 |
# Special case: only one field and it's a struct at the beginning |
| 974 |
# of the dword. In this case we unpack directly from the |
| 975 |
# source. This is the only way we handle embedded structs |
| 976 |
# larger than 32 bits. |
| 977 |
if len(dw.fields) == 1: |
| 978 |
field = dw.fields[0] |
| 979 |
if root.is_known_struct(field.type) and field.start % 32 == 0: |
| 980 |
prefix = root.get_struct(field.type) |
| 981 |
print("") |
| 982 |
print(" %s_unpack(data, &dw[%d], &values->%s);" % (prefix, index, field.name)) |
| 983 |
continue |
| 984 |
|
| 985 |
dword_start = index * 32 |
| 986 |
|
| 987 |
if dw.size == 32: |
| 988 |
v = "dw[%d]" % index |
| 989 |
elif dw.size == 64: |
| 990 |
v = "v%d" % index |
| 991 |
print(" const uint%d_t %s = dw[%d] | ((uint64_t)dw[%d] << 32);" % (dw.size, v, index, index + 1)) |
| 992 |
else: |
| 993 |
raise RuntimeError("Unsupported dword size %d" % dw.size) |
| 994 |
|
| 995 |
# Unpack any fields of struct type first. |
| 996 |
for field_index, field in enumerate(f for f in dw.fields if root.is_known_struct(f.type)): |
| 997 |
prefix = root.get_struct(field.type).prefix |
| 998 |
vname = "v%d_%d" % (index, field_index) |
| 999 |
print("") |
| 1000 |
print(" uint32_t %s = __pvr_uint_unpack(%s, %d, %d);" |
| 1001 |
% (vname, v, field.start - dword_start, field.end - dword_start)) |
| 1002 |
print(" %s_unpack(data, &%s, &values->%s);" % (prefix, vname, field.name)) |
| 1003 |
|
| 1004 |
for field in dw.fields: |
| 1005 |
dword_field_start = field.start - dword_start |
| 1006 |
dword_field_end = field.end - dword_start |
| 1007 |
|
| 1008 |
if field.type == "mbo" or root.is_known_struct(field.type): |
| 1009 |
continue |
| 1010 |
elif field.type == "uint" or root.is_known_enum(field.type) or field.type == "bool": |
| 1011 |
print(" values->%s = __pvr_uint_unpack(%s, %d, %d);" |
| 1012 |
% (field.name, v, dword_field_start, dword_field_end)) |
| 1013 |
elif field.type == "int": |
| 1014 |
print(" values->%s = __pvr_sint_unpack(%s, %d, %d);" |
| 1015 |
% (field.name, v, dword_field_start, dword_field_end)) |
| 1016 |
elif field.type == "float": |
| 1017 |
print(" values->%s = __pvr_float_unpack(%s);" % (field.name, v)) |
| 1018 |
elif field.type == "offset": |
| 1019 |
print(" values->%s = __pvr_offset_unpack(%s, %d, %d);" |
| 1020 |
% (field.name, v, dword_field_start, dword_field_end)) |
| 1021 |
elif field.type == "address": |
| 1022 |
print(" values->%s = __pvr_address_unpack(%s, %d, %d, %d);" |
| 1023 |
% (field.name, v, field.shift, dword_field_start, dword_field_end)) |
| 1024 |
else: |
| 1025 |
print("/* unhandled field %s, type %s */" % (field.name, field.type)) |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
class Parser: |
| 1030 |
__slots__ = ["parser", "context", "filename"] |
| 1031 |
|
| 1032 |
parser: expat.XMLParserType |
| 1033 |
context: t.List[Node] |
| 1034 |
filename: str |
| 1035 |
|
| 1036 |
def __init__(self) -> None: |
| 1037 |
self.parser = expat.ParserCreate() |
| 1038 |
self.parser.StartElementHandler = self.start_element |
| 1039 |
self.parser.EndElementHandler = self.end_element |
| 1040 |
|
| 1041 |
self.context = [] |
| 1042 |
self.filename = "" |
| 1043 |
|
| 1044 |
def start_element(self, name: str, attrs: t.Dict[str, str]) -> None: |
| 1045 |
if name == "csbgen": |
| 1046 |
if self.context: |
| 1047 |
raise RuntimeError( |
| 1048 |
"Can only have 1 csbgen block and it has " |
| 1049 |
+ "to contain all of the other elements." |
| 1050 |
) |
| 1051 |
|
| 1052 |
csbgen = Csbgen(attrs["name"], attrs["prefix"], self.filename) |
| 1053 |
self.context.append(csbgen) |
| 1054 |
return |
| 1055 |
|
| 1056 |
parent = self.context[-1] |
| 1057 |
|
| 1058 |
if name == "struct": |
| 1059 |
struct = Struct(parent, attrs["name"], int(attrs["length"])) |
| 1060 |
self.context.append(struct) |
| 1061 |
|
| 1062 |
elif name == "stream": |
| 1063 |
stream = Stream(parent, attrs["name"], int(attrs["length"])) |
| 1064 |
self.context.append(stream) |
| 1065 |
|
| 1066 |
elif name == "field": |
| 1067 |
default = None |
| 1068 |
if "default" in attrs.keys(): |
| 1069 |
default = attrs["default"] |
| 1070 |
|
| 1071 |
shift = None |
| 1072 |
if "shift" in attrs.keys(): |
| 1073 |
shift = attrs["shift"] |
| 1074 |
|
| 1075 |
if "start" in attrs.keys(): |
| 1076 |
if ":" in str(attrs["start"]): |
| 1077 |
(word, bit) = attrs["start"].split(":") |
| 1078 |
start = (int(word) * 32) + int(bit) |
| 1079 |
else: |
| 1080 |
start = int(attrs["start"]) |
| 1081 |
else: |
| 1082 |
element = self.context[-1] |
| 1083 |
if isinstance(element, Stream): |
| 1084 |
start = 0 |
| 1085 |
else: |
| 1086 |
raise RuntimeError("Field requires start attribute outside of stream.") |
| 1087 |
|
| 1088 |
if "size" in attrs.keys(): |
| 1089 |
end = start + int(attrs["size"]) |
| 1090 |
else: |
| 1091 |
end = int(attrs["end"]) |
| 1092 |
|
| 1093 |
field = Field(parent, name=attrs["name"], start=start, end=end, ty=attrs["type"], |
| 1094 |
default=default, shift=shift) |
| 1095 |
self.context.append(field) |
| 1096 |
|
| 1097 |
elif name == "enum": |
| 1098 |
enum = Enum(parent, attrs["name"]) |
| 1099 |
self.context.append(enum) |
| 1100 |
|
| 1101 |
elif name == "value": |
| 1102 |
value = Value(parent, attrs["name"], int(literal_eval(attrs["value"]))) |
| 1103 |
self.context.append(value) |
| 1104 |
|
| 1105 |
elif name == "define": |
| 1106 |
define = Define(parent, attrs["name"], int(literal_eval(attrs["value"]))) |
| 1107 |
self.context.append(define) |
| 1108 |
|
| 1109 |
elif name == "condition": |
| 1110 |
condition = Condition(parent, name=attrs["check"], ty=attrs["type"]) |
| 1111 |
|
| 1112 |
# Starting with the if statement we push it in the context. For each |
| 1113 |
# branch following (elif, and else) we assign the top of stack as |
| 1114 |
# its parent, pop() and push the new condition. So per branch we end |
| 1115 |
# up having [..., struct, condition]. We don't push an endif since |
| 1116 |
# it's not supposed to have any children and it's supposed to close |
| 1117 |
# the whole if statement. |
| 1118 |
|
| 1119 |
if condition.type != "if": |
| 1120 |
# Remove the parent condition from the context. We were peeking |
| 1121 |
# before, now we pop(). |
| 1122 |
self.context.pop() |
| 1123 |
|
| 1124 |
if condition.type == "endif": |
| 1125 |
if not isinstance(parent, Condition): |
| 1126 |
raise RuntimeError("Cannot close unopened or already closed condition. Condition: '%s'" |
| 1127 |
% condition.name) |
| 1128 |
else: |
| 1129 |
self.context.append(condition) |
| 1130 |
|
| 1131 |
else: |
| 1132 |
raise RuntimeError("Unknown tag: '%s'" % name) |
| 1133 |
|
| 1134 |
def end_element(self, name: str) -> None: |
| 1135 |
if name == "condition": |
| 1136 |
element = self.context[-1] |
| 1137 |
if not isinstance(element, Condition) and not isinstance(element, Struct): |
| 1138 |
raise RuntimeError("Expected condition or struct tag to be closed.") |
| 1139 |
|
| 1140 |
return |
| 1141 |
|
| 1142 |
element = self.context.pop() |
| 1143 |
|
| 1144 |
if name == "struct": |
| 1145 |
if not isinstance(element, Struct): |
| 1146 |
raise RuntimeError("Expected struct tag to be closed.") |
| 1147 |
elif name == "stream": |
| 1148 |
if not isinstance(element, Stream): |
| 1149 |
raise RuntimeError("Expected stream tag to be closed.") |
| 1150 |
elif name == "field": |
| 1151 |
if not isinstance(element, Field): |
| 1152 |
raise RuntimeError("Expected field tag to be closed.") |
| 1153 |
elif name == "enum": |
| 1154 |
if not isinstance(element, Enum): |
| 1155 |
raise RuntimeError("Expected enum tag to be closed.") |
| 1156 |
elif name == "value": |
| 1157 |
if not isinstance(element, Value): |
| 1158 |
raise RuntimeError("Expected value tag to be closed.") |
| 1159 |
elif name == "define": |
| 1160 |
if not isinstance(element, Define): |
| 1161 |
raise RuntimeError("Expected define tag to be closed.") |
| 1162 |
elif name == "csbgen": |
| 1163 |
if not isinstance(element, Csbgen): |
| 1164 |
raise RuntimeError("Expected csbgen tag to be closed.\nSome tags may have not been closed") |
| 1165 |
|
| 1166 |
element.emit() |
| 1167 |
else: |
| 1168 |
raise RuntimeError("Unknown closing element: '%s'" % name) |
| 1169 |
|
| 1170 |
def parse(self, filename: str) -> None: |
| 1171 |
file = open(filename, "rb") |
| 1172 |
self.filename = filename |
| 1173 |
self.parser.ParseFile(file) |
| 1174 |
file.close() |
| 1175 |
|
| 1176 |
|
| 1177 |
if __name__ == "__main__": |
| 1178 |
import sys |
| 1179 |
|
| 1180 |
if len(sys.argv) < 2: |
| 1181 |
print("No input xml file specified") |
| 1182 |
sys.exit(1) |
| 1183 |
|
| 1184 |
input_file = sys.argv[1] |
| 1185 |
|
| 1186 |
p = Parser() |
| 1187 |
p.parse(input_file) |