| 1 |
#encoding=utf-8 |
| 2 |
|
| 3 |
# Copyright (C) 2016 Intel Corporation |
| 4 |
# Copyright (C) 2016 Broadcom |
| 5 |
# Copyright (C) 2020 Collabora, Ltd. |
| 6 |
# SPDX-License-Identifier: MIT |
| 7 |
|
| 8 |
import argparse |
| 9 |
import xml.parsers.expat |
| 10 |
import sys |
| 11 |
import operator |
| 12 |
from functools import reduce |
| 13 |
|
| 14 |
global_prefix = "mali" |
| 15 |
|
| 16 |
v6_format_printer = """ |
| 17 |
#define mali_pixel_format_print(fp, format) \\ |
| 18 |
fprintf(fp, "%*sFormat (v6): %s%s%s %s%s%s%s\\n", indent, "", \\ |
| 19 |
mali_format_as_str((enum mali_format)((format >> 12) & 0xFF)), \\ |
| 20 |
(format & (1 << 20)) ? " sRGB" : "", \\ |
| 21 |
(format & (1 << 21)) ? " big-endian" : "", \\ |
| 22 |
mali_channel_as_str((enum mali_channel)((format >> 0) & 0x7)), \\ |
| 23 |
mali_channel_as_str((enum mali_channel)((format >> 3) & 0x7)), \\ |
| 24 |
mali_channel_as_str((enum mali_channel)((format >> 6) & 0x7)), \\ |
| 25 |
mali_channel_as_str((enum mali_channel)((format >> 9) & 0x7))); |
| 26 |
""" |
| 27 |
|
| 28 |
v7_format_printer = """ |
| 29 |
#define mali_pixel_format_print(fp, format) \\ |
| 30 |
fprintf(fp, "%*sFormat (v7): %s%s %s%s\\n", indent, "", \\ |
| 31 |
mali_format_as_str((enum mali_format)((format >> 12) & 0xFF)), \\ |
| 32 |
(format & (1 << 20)) ? " sRGB" : "", \\ |
| 33 |
mali_rgb_component_order_as_str((enum mali_rgb_component_order)(format & ((1 << 12) - 1))), \\ |
| 34 |
(format & (1 << 21)) ? " XXX BAD BIT" : ""); |
| 35 |
""" |
| 36 |
|
| 37 |
def to_alphanum(name): |
| 38 |
substitutions = { |
| 39 |
' ': '_', |
| 40 |
'/': '_', |
| 41 |
'[': '', |
| 42 |
']': '', |
| 43 |
'(': '', |
| 44 |
')': '', |
| 45 |
'-': '_', |
| 46 |
':': '', |
| 47 |
'.': '', |
| 48 |
',': '', |
| 49 |
'=': '', |
| 50 |
'>': '', |
| 51 |
'#': '', |
| 52 |
'&': '', |
| 53 |
'%': '', |
| 54 |
'*': '', |
| 55 |
'"': '', |
| 56 |
'+': '', |
| 57 |
'\'': '', |
| 58 |
} |
| 59 |
|
| 60 |
for i, j in substitutions.items(): |
| 61 |
name = name.replace(i, j) |
| 62 |
|
| 63 |
return name |
| 64 |
|
| 65 |
def safe_name(name): |
| 66 |
name = to_alphanum(name) |
| 67 |
if not name[0].isalpha(): |
| 68 |
name = '_' + name |
| 69 |
|
| 70 |
return name |
| 71 |
|
| 72 |
def prefixed_upper_name(prefix, name): |
| 73 |
if prefix: |
| 74 |
name = prefix + "_" + name |
| 75 |
return safe_name(name).upper() |
| 76 |
|
| 77 |
def enum_name(name): |
| 78 |
return "{}_{}".format(global_prefix, safe_name(name)).lower() |
| 79 |
|
| 80 |
MODIFIERS = ["shr", "minus", "align", "log2"] |
| 81 |
|
| 82 |
def parse_modifier(modifier): |
| 83 |
if modifier is None: |
| 84 |
return None |
| 85 |
|
| 86 |
for mod in MODIFIERS: |
| 87 |
if modifier[0:len(mod)] == mod: |
| 88 |
if mod == "log2": |
| 89 |
assert(len(mod) == len(modifier)) |
| 90 |
return [mod] |
| 91 |
|
| 92 |
if modifier[len(mod)] == '(' and modifier[-1] == ')': |
| 93 |
ret = [mod, int(modifier[(len(mod) + 1):-1])] |
| 94 |
if ret[0] == 'align': |
| 95 |
align = ret[1] |
| 96 |
# Make sure the alignment is a power of 2 |
| 97 |
assert(align > 0 and not(align & (align - 1))); |
| 98 |
|
| 99 |
return ret |
| 100 |
|
| 101 |
print("Invalid modifier") |
| 102 |
assert(False) |
| 103 |
|
| 104 |
class Aggregate(object): |
| 105 |
def __init__(self, parser, name, attrs): |
| 106 |
self.parser = parser |
| 107 |
self.sections = [] |
| 108 |
self.name = name |
| 109 |
self.explicit_size = int(attrs["size"]) if "size" in attrs else 0 |
| 110 |
self.size = 0 |
| 111 |
self.align = int(attrs["align"]) if "align" in attrs else None |
| 112 |
|
| 113 |
class Section: |
| 114 |
def __init__(self, name): |
| 115 |
self.name = name |
| 116 |
|
| 117 |
def get_size(self): |
| 118 |
if self.size > 0: |
| 119 |
return self.size |
| 120 |
|
| 121 |
size = 0 |
| 122 |
for section in self.sections: |
| 123 |
size = max(size, section.offset + section.type.get_length()) |
| 124 |
|
| 125 |
if self.explicit_size > 0: |
| 126 |
assert(self.explicit_size >= size) |
| 127 |
self.size = self.explicit_size |
| 128 |
else: |
| 129 |
self.size = size |
| 130 |
return self.size |
| 131 |
|
| 132 |
def add_section(self, type_name, attrs): |
| 133 |
assert("name" in attrs) |
| 134 |
section = self.Section(safe_name(attrs["name"]).lower()) |
| 135 |
section.human_name = attrs["name"] |
| 136 |
section.offset = int(attrs["offset"]) |
| 137 |
assert(section.offset % 4 == 0) |
| 138 |
section.type = self.parser.structs[attrs["type"]] |
| 139 |
section.type_name = type_name |
| 140 |
self.sections.append(section) |
| 141 |
|
| 142 |
class Field(object): |
| 143 |
def __init__(self, parser, attrs): |
| 144 |
self.parser = parser |
| 145 |
if "name" in attrs: |
| 146 |
self.name = safe_name(attrs["name"]).lower() |
| 147 |
self.human_name = attrs["name"] |
| 148 |
|
| 149 |
if ":" in str(attrs["start"]): |
| 150 |
(word, bit) = attrs["start"].split(":") |
| 151 |
self.start = (int(word) * 32) + int(bit) |
| 152 |
else: |
| 153 |
self.start = int(attrs["start"]) |
| 154 |
|
| 155 |
self.end = self.start + int(attrs["size"]) - 1 |
| 156 |
self.type = attrs["type"] |
| 157 |
|
| 158 |
if self.type == 'bool' and self.start != self.end: |
| 159 |
print("#error Field {} has bool type but more than one bit of size".format(self.name)); |
| 160 |
|
| 161 |
if "prefix" in attrs: |
| 162 |
self.prefix = safe_name(attrs["prefix"]).upper() |
| 163 |
else: |
| 164 |
self.prefix = None |
| 165 |
|
| 166 |
self.default = attrs.get("default") |
| 167 |
|
| 168 |
# Map enum values |
| 169 |
if self.type in self.parser.enums and self.default is not None: |
| 170 |
self.default = safe_name('{}_{}_{}'.format(global_prefix, self.type, self.default)).upper() |
| 171 |
|
| 172 |
self.modifier = parse_modifier(attrs.get("modifier")) |
| 173 |
|
| 174 |
def emit_template_struct(self, dim): |
| 175 |
if self.type == 'address': |
| 176 |
type = 'uint64_t' |
| 177 |
elif self.type == 'bool': |
| 178 |
type = 'bool' |
| 179 |
elif self.type in ['float', 'ulod', 'slod']: |
| 180 |
type = 'float' |
| 181 |
elif self.type in ['uint', 'hex'] and self.end - self.start >= 31: |
| 182 |
type = 'uint64_t' |
| 183 |
elif self.type == 'int': |
| 184 |
type = 'int64_t' if self.end - self.start >= 31 else 'int32_t' |
| 185 |
elif self.type in ['uint', 'hex', 'uint/float', 'padded', 'Pixel Format', 'Component Swizzle']: |
| 186 |
type = 'uint32_t' |
| 187 |
elif self.type in self.parser.structs: |
| 188 |
type = 'struct ' + self.parser.gen_prefix(safe_name(self.type.upper())) |
| 189 |
elif self.type in self.parser.enums: |
| 190 |
type = 'enum ' + enum_name(self.type) |
| 191 |
else: |
| 192 |
print("#error unhandled type: %s" % self.type) |
| 193 |
type = "uint32_t" |
| 194 |
|
| 195 |
print(" %-36s %s%s;" % (type, self.name, dim)) |
| 196 |
|
| 197 |
for value in self.values: |
| 198 |
name = prefixed_upper_name(self.prefix, value.name) |
| 199 |
print("#define %-40s %d" % (name, value.value)) |
| 200 |
|
| 201 |
def overlaps(self, field): |
| 202 |
return self != field and max(self.start, field.start) <= min(self.end, field.end) |
| 203 |
|
| 204 |
class Group(object): |
| 205 |
def __init__(self, parser, parent, start, count, label): |
| 206 |
self.parser = parser |
| 207 |
self.parent = parent |
| 208 |
self.start = start |
| 209 |
self.count = count |
| 210 |
self.label = label |
| 211 |
self.size = 0 |
| 212 |
self.length = 0 |
| 213 |
self.fields = [] |
| 214 |
|
| 215 |
def get_length(self): |
| 216 |
# Determine number of bytes in this group. |
| 217 |
calculated = max(field.end // 8 for field in self.fields) + 1 if len(self.fields) > 0 else 0 |
| 218 |
if self.length > 0: |
| 219 |
assert(self.length >= calculated) |
| 220 |
else: |
| 221 |
self.length = calculated |
| 222 |
return self.length |
| 223 |
|
| 224 |
|
| 225 |
def emit_template_struct(self, dim): |
| 226 |
if self.count == 0: |
| 227 |
print(" /* variable length fields follow */") |
| 228 |
else: |
| 229 |
if self.count > 1: |
| 230 |
dim = "%s[%d]" % (dim, self.count) |
| 231 |
|
| 232 |
if len(self.fields) == 0: |
| 233 |
print(" int dummy;") |
| 234 |
|
| 235 |
for field in self.fields: |
| 236 |
field.emit_template_struct(dim) |
| 237 |
|
| 238 |
class Word: |
| 239 |
def __init__(self): |
| 240 |
self.size = 32 |
| 241 |
self.contributors = [] |
| 242 |
|
| 243 |
class FieldRef: |
| 244 |
def __init__(self, field, path, start, end): |
| 245 |
self.field = field |
| 246 |
self.path = path |
| 247 |
self.start = start |
| 248 |
self.end = end |
| 249 |
|
| 250 |
def collect_fields(self, fields, offset, path, all_fields): |
| 251 |
for field in fields: |
| 252 |
field_path = '{}{}'.format(path, field.name) |
| 253 |
field_offset = offset + field.start |
| 254 |
|
| 255 |
if field.type in self.parser.structs: |
| 256 |
sub_struct = self.parser.structs[field.type] |
| 257 |
self.collect_fields(sub_struct.fields, field_offset, field_path + '.', all_fields) |
| 258 |
continue |
| 259 |
|
| 260 |
start = field_offset |
| 261 |
end = offset + field.end |
| 262 |
all_fields.append(self.FieldRef(field, field_path, start, end)) |
| 263 |
|
| 264 |
def collect_words(self, fields, offset, path, words): |
| 265 |
for field in fields: |
| 266 |
field_path = '{}{}'.format(path, field.name) |
| 267 |
start = offset + field.start |
| 268 |
|
| 269 |
if field.type in self.parser.structs: |
| 270 |
sub_fields = self.parser.structs[field.type].fields |
| 271 |
self.collect_words(sub_fields, start, field_path + '.', words) |
| 272 |
continue |
| 273 |
|
| 274 |
end = offset + field.end |
| 275 |
contributor = self.FieldRef(field, field_path, start, end) |
| 276 |
first_word = contributor.start // 32 |
| 277 |
last_word = contributor.end // 32 |
| 278 |
for b in range(first_word, last_word + 1): |
| 279 |
if not b in words: |
| 280 |
words[b] = self.Word() |
| 281 |
words[b].contributors.append(contributor) |
| 282 |
|
| 283 |
def emit_pack_function(self): |
| 284 |
self.get_length() |
| 285 |
|
| 286 |
words = {} |
| 287 |
self.collect_words(self.fields, 0, '', words) |
| 288 |
|
| 289 |
# Validate the modifier is lossless |
| 290 |
for field in self.fields: |
| 291 |
if field.modifier is None: |
| 292 |
continue |
| 293 |
|
| 294 |
if field.modifier[0] == "shr": |
| 295 |
shift = field.modifier[1] |
| 296 |
mask = hex((1 << shift) - 1) |
| 297 |
print(" assert(((__unpacked)->{} & {}) == 0); \\".format(field.name, mask)) |
| 298 |
elif field.modifier[0] == "minus": |
| 299 |
print(" assert((__unpacked)->{} >= {}); \\".format(field.name, field.modifier[1])) |
| 300 |
elif field.modifier[0] == "log2": |
| 301 |
print(" assert(IS_POT_NONZERO((__unpacked)->{})); \\".format(field.name)) |
| 302 |
|
| 303 |
for index in range(self.length // 4): |
| 304 |
# Handle MBZ words |
| 305 |
if not index in words: |
| 306 |
print(" __tmp_packed.opaque[%2d] = 0; \\" % index) |
| 307 |
continue |
| 308 |
|
| 309 |
word = words[index] |
| 310 |
|
| 311 |
word_start = index * 32 |
| 312 |
|
| 313 |
v = None |
| 314 |
prefix = " __tmp_packed.opaque[%2d] =" % index |
| 315 |
|
| 316 |
for contributor in word.contributors: |
| 317 |
field = contributor.field |
| 318 |
name = field.name |
| 319 |
start = contributor.start |
| 320 |
end = contributor.end |
| 321 |
contrib_word_start = (start // 32) * 32 |
| 322 |
start -= contrib_word_start |
| 323 |
end -= contrib_word_start |
| 324 |
|
| 325 |
value = "(__unpacked)->{}".format(contributor.path) |
| 326 |
if field.modifier is not None: |
| 327 |
if field.modifier[0] == "shr": |
| 328 |
value = "{} >> {}".format(value, field.modifier[1]) |
| 329 |
elif field.modifier[0] == "minus": |
| 330 |
value = "{} - {}".format(value, field.modifier[1]) |
| 331 |
elif field.modifier[0] == "align": |
| 332 |
value = "ALIGN_POT({}, {})".format(value, field.modifier[1]) |
| 333 |
elif field.modifier[0] == "log2": |
| 334 |
value = "util_logbase2({})".format(value) |
| 335 |
|
| 336 |
if field.type in ["uint", "hex", "uint/float", "address", "Pixel Format", "Component Swizzle"]: |
| 337 |
s = "util_bitpack_uint(%s, %d, %d)" % \ |
| 338 |
(value, start, end) |
| 339 |
elif field.type == "padded": |
| 340 |
s = "__gen_padded(%s, %d, %d)" % \ |
| 341 |
(value, start, end) |
| 342 |
elif field.type in self.parser.enums: |
| 343 |
s = "util_bitpack_uint(%s, %d, %d)" % \ |
| 344 |
(value, start, end) |
| 345 |
elif field.type == "int": |
| 346 |
s = "util_bitpack_sint(%s, %d, %d)" % \ |
| 347 |
(value, start, end) |
| 348 |
elif field.type == "bool": |
| 349 |
s = "util_bitpack_uint(%s, %d, %d)" % \ |
| 350 |
(value, start, end) |
| 351 |
elif field.type == "float": |
| 352 |
assert(start == 0 and end == 31) |
| 353 |
s = "util_bitpack_float({})".format(value) |
| 354 |
elif field.type == "ulod": |
| 355 |
s = "util_bitpack_ufixed_clamp({}, {}, {}, 8)".format(value, |
| 356 |
start, |
| 357 |
end) |
| 358 |
elif field.type == "slod": |
| 359 |
s = "util_bitpack_sfixed_clamp({}, {}, {}, 8)".format(value, |
| 360 |
start, |
| 361 |
end) |
| 362 |
else: |
| 363 |
s = "#error unhandled field {}, type {}".format(contributor.path, field.type) |
| 364 |
|
| 365 |
if not s == None: |
| 366 |
shift = word_start - contrib_word_start |
| 367 |
if shift: |
| 368 |
s = "%s >> %d" % (s, shift) |
| 369 |
|
| 370 |
if contributor == word.contributors[-1]: |
| 371 |
print("%s %s; \\" % (prefix, s)) |
| 372 |
else: |
| 373 |
print("%s %s | \\" % (prefix, s)) |
| 374 |
prefix = " " |
| 375 |
|
| 376 |
continue |
| 377 |
|
| 378 |
# Given a field (start, end) contained in word `index`, generate the 32-bit |
| 379 |
# mask of present bits relative to the word |
| 380 |
def mask_for_word(self, index, start, end): |
| 381 |
field_word_start = index * 32 |
| 382 |
start -= field_word_start |
| 383 |
end -= field_word_start |
| 384 |
# Cap multiword at one word |
| 385 |
start = max(start, 0) |
| 386 |
end = min(end, 32 - 1) |
| 387 |
count = (end - start + 1) |
| 388 |
return (((1 << count) - 1) << start) |
| 389 |
|
| 390 |
def emit_unpack_function(self): |
| 391 |
# First, verify there is no garbage in unused bits |
| 392 |
words = {} |
| 393 |
self.collect_words(self.fields, 0, '', words) |
| 394 |
|
| 395 |
for index in range(self.length // 4): |
| 396 |
base = index * 32 |
| 397 |
word = words.get(index, self.Word()) |
| 398 |
masks = [self.mask_for_word(index, c.start, c.end) for c in word.contributors] |
| 399 |
mask = reduce(lambda x,y: x | y, masks, 0) |
| 400 |
|
| 401 |
ALL_ONES = 0xffffffff |
| 402 |
|
| 403 |
if mask != ALL_ONES: |
| 404 |
TMPL = ' if (__tmp_packed.opaque[{}] & {}) fprintf(stderr, "XXX: Invalid field of {} unpacked at word {}\\n"); \\' |
| 405 |
print(TMPL.format(index, hex(mask ^ ALL_ONES), self.label, index)) |
| 406 |
|
| 407 |
fieldrefs = [] |
| 408 |
self.collect_fields(self.fields, 0, '', fieldrefs) |
| 409 |
for fieldref in fieldrefs: |
| 410 |
field = fieldref.field |
| 411 |
convert = None |
| 412 |
|
| 413 |
args = [] |
| 414 |
args.append('(__unpacked)->{}'.format(fieldref.path)) |
| 415 |
args.append('&__tmp_packed.opaque[0]') |
| 416 |
args.append(str(fieldref.start)) |
| 417 |
args.append(str(fieldref.end)) |
| 418 |
|
| 419 |
if field.type in set(["uint", "hex", "uint/float", "address", "Pixel Format", "Component Swizzle"]): |
| 420 |
convert = "__gen_unpack_uint" |
| 421 |
elif field.type in self.parser.enums: |
| 422 |
convert = "__gen_unpack_uint" |
| 423 |
elif field.type == "int": |
| 424 |
convert = "__gen_unpack_sint" |
| 425 |
elif field.type == "padded": |
| 426 |
convert = "__gen_unpack_padded" |
| 427 |
elif field.type == "bool": |
| 428 |
convert = "__gen_unpack_uint" |
| 429 |
elif field.type == "float": |
| 430 |
convert = "__gen_unpack_float" |
| 431 |
elif field.type == "ulod": |
| 432 |
convert = "__gen_unpack_ulod" |
| 433 |
elif field.type == "slod": |
| 434 |
convert = "__gen_unpack_slod" |
| 435 |
else: |
| 436 |
s = "/* unhandled field %s, type %s */\n" % (field.name, field.type) |
| 437 |
|
| 438 |
suffix = "" |
| 439 |
prefix = "" |
| 440 |
if field.modifier: |
| 441 |
if field.modifier[0] == "minus": |
| 442 |
suffix = " + {}".format(field.modifier[1]) |
| 443 |
elif field.modifier[0] == "shr": |
| 444 |
suffix = " << {}".format(field.modifier[1]) |
| 445 |
if field.modifier[0] == "log2": |
| 446 |
prefix = "1U << " |
| 447 |
|
| 448 |
print(' {}({}); \\'.format(convert, ', '.join(args))) |
| 449 |
|
| 450 |
if len(prefix) != 0 or len(suffix) != 0: |
| 451 |
print(' (__unpacked)->{} = {}(__unpacked)->{}{}; \\'.format(fieldref.path, prefix, fieldref.path, suffix)) |
| 452 |
|
| 453 |
|
| 454 |
if field.modifier and field.modifier[0] == "align": |
| 455 |
mask = hex(field.modifier[1] - 1) |
| 456 |
print(' assert(!((__unpacked)->{} & {})); \\'.format(fieldref.path, mask)) |
| 457 |
|
| 458 |
def emit_print_function(self): |
| 459 |
for field in self.fields: |
| 460 |
convert = None |
| 461 |
name, val = field.human_name, 'values->{}'.format(field.name) |
| 462 |
|
| 463 |
if field.type in self.parser.structs: |
| 464 |
pack_name = self.parser.gen_prefix(safe_name(field.type)).upper() |
| 465 |
print(' fprintf(fp, "%*s{}:\\n", indent, "");'.format(field.human_name)) |
| 466 |
print(" {}_print(fp, &values->{}, indent + 2);".format(pack_name, field.name)) |
| 467 |
elif field.type == "address": |
| 468 |
# TODO resolve to name |
| 469 |
print(' fprintf(fp, "%*s{}: 0x%" PRIx64 "\\n", indent, "", {});'.format(name, val)) |
| 470 |
elif field.type in self.parser.enums: |
| 471 |
print(' fprintf(fp, "%*s{}: %s\\n", indent, "", {}_as_str({}));'.format(name, enum_name(field.type), val)) |
| 472 |
elif field.type == "int": |
| 473 |
print(' fprintf(fp, "%*s{}: %" PRId64 "\\n", indent, "", (int64_t){});'.format(name, val)) |
| 474 |
elif field.type == "bool": |
| 475 |
print(' fprintf(fp, "%*s{}: %s\\n", indent, "", {} ? "true" : "false");'.format(name, val)) |
| 476 |
elif field.type in ["float", "ulod", "slod"]: |
| 477 |
print(' fprintf(fp, "%*s{}: %f\\n", indent, "", {});'.format(name, val)) |
| 478 |
elif field.type in ["uint", "hex"] and (field.end - field.start) >= 31: |
| 479 |
print(' fprintf(fp, "%*s{}: 0x%" PRIx64 "\\n", indent, "", {});'.format(name, val)) |
| 480 |
elif field.type == "hex": |
| 481 |
print(' fprintf(fp, "%*s{}: 0x%x\\n", indent, "", {});'.format(name, val)) |
| 482 |
elif field.type == "uint/float": |
| 483 |
print(' fprintf(fp, "%*s{}: 0x%X (%f)\\n", indent, "", {}, uif({}));'.format(name, val, val)) |
| 484 |
elif field.type == "Pixel Format": |
| 485 |
print(' mali_pixel_format_print(fp, {});'.format(val)) |
| 486 |
elif field.type == "Component Swizzle": |
| 487 |
print(' fprintf(fp, "%*s{}: %u (%s)\\n", indent, "", {}, mali_component_swizzle({}));'.format(name, val, val)) |
| 488 |
else: |
| 489 |
print(' fprintf(fp, "%*s{}: %u\\n", indent, "", {});'.format(name, val)) |
| 490 |
|
| 491 |
class Value(object): |
| 492 |
def __init__(self, attrs): |
| 493 |
self.name = attrs["name"] |
| 494 |
self.value = int(attrs["value"], 0) |
| 495 |
|
| 496 |
pack_header = """/* Autogenerated file, do not edit */ |
| 497 |
/* |
| 498 |
* Copyright 2024 Collabora Ltd. |
| 499 |
* SPDX-License-Identifier: MIT |
| 500 |
*/ |
| 501 |
|
| 502 |
#ifndef PAN_PACK_H |
| 503 |
#define PAN_PACK_H |
| 504 |
|
| 505 |
#include "genxml/pan_pack_helpers.h" |
| 506 |
""" |
| 507 |
|
| 508 |
class Parser(object): |
| 509 |
def __init__(self): |
| 510 |
self.parser = xml.parsers.expat.ParserCreate() |
| 511 |
self.parser.StartElementHandler = self.start_element |
| 512 |
self.parser.EndElementHandler = self.end_element |
| 513 |
|
| 514 |
self.struct = None |
| 515 |
self.structs = {} |
| 516 |
# Set of enum names we've seen. |
| 517 |
self.enums = set() |
| 518 |
self.aggregate = None |
| 519 |
self.aggregates = {} |
| 520 |
|
| 521 |
def gen_prefix(self, name): |
| 522 |
return '{}_{}'.format(global_prefix.upper(), name) |
| 523 |
|
| 524 |
def start_element(self, name, attrs): |
| 525 |
if name == "panxml": |
| 526 |
print(pack_header) |
| 527 |
if "arch" in attrs: |
| 528 |
arch = int(attrs["arch"]) |
| 529 |
if arch <= 6: |
| 530 |
print(v6_format_printer) |
| 531 |
else: |
| 532 |
print(v7_format_printer) |
| 533 |
elif name == "struct": |
| 534 |
name = attrs["name"] |
| 535 |
self.no_direct_packing = attrs.get("no-direct-packing", False) |
| 536 |
object_name = self.gen_prefix(safe_name(name.upper())) |
| 537 |
self.struct = object_name |
| 538 |
|
| 539 |
self.group = Group(self, None, 0, 1, name) |
| 540 |
if "size" in attrs: |
| 541 |
self.group.length = int(attrs["size"]) * 4 |
| 542 |
self.group.align = int(attrs["align"]) if "align" in attrs else None |
| 543 |
self.structs[attrs["name"]] = self.group |
| 544 |
elif name == "field": |
| 545 |
self.group.fields.append(Field(self, attrs)) |
| 546 |
self.values = [] |
| 547 |
elif name == "enum": |
| 548 |
self.values = [] |
| 549 |
self.enum = safe_name(attrs["name"]) |
| 550 |
self.enums.add(attrs["name"]) |
| 551 |
if "prefix" in attrs: |
| 552 |
self.prefix = attrs["prefix"] |
| 553 |
else: |
| 554 |
self.prefix= None |
| 555 |
elif name == "value": |
| 556 |
self.values.append(Value(attrs)) |
| 557 |
elif name == "aggregate": |
| 558 |
aggregate_name = self.gen_prefix(safe_name(attrs["name"].upper())) |
| 559 |
self.aggregate = Aggregate(self, aggregate_name, attrs) |
| 560 |
self.aggregates[attrs['name']] = self.aggregate |
| 561 |
elif name == "section": |
| 562 |
type_name = self.gen_prefix(safe_name(attrs["type"].upper())) |
| 563 |
self.aggregate.add_section(type_name, attrs) |
| 564 |
|
| 565 |
def end_element(self, name): |
| 566 |
if name == "struct": |
| 567 |
self.emit_struct() |
| 568 |
self.struct = None |
| 569 |
self.group = None |
| 570 |
elif name == "field": |
| 571 |
self.group.fields[-1].values = self.values |
| 572 |
elif name == "enum": |
| 573 |
self.emit_enum() |
| 574 |
self.enum = None |
| 575 |
elif name == "aggregate": |
| 576 |
self.emit_aggregate() |
| 577 |
self.aggregate = None |
| 578 |
elif name == "panxml": |
| 579 |
# Include at the end so it can depend on us but not the converse |
| 580 |
print('#endif') |
| 581 |
|
| 582 |
def emit_header(self, name): |
| 583 |
default_fields = [] |
| 584 |
for field in self.group.fields: |
| 585 |
if not type(field) is Field: |
| 586 |
continue |
| 587 |
if field.default is not None: |
| 588 |
default_fields.append(" .{} = {}".format(field.name, field.default)) |
| 589 |
elif field.type in self.structs: |
| 590 |
default_fields.append(" .{} = {{ {}_header }}".format(field.name, self.gen_prefix(safe_name(field.type.upper())))) |
| 591 |
|
| 592 |
print('#define %-40s\\' % (name + '_header')) |
| 593 |
if default_fields: |
| 594 |
print(", \\\n".join(default_fields)) |
| 595 |
else: |
| 596 |
print(' 0') |
| 597 |
print('') |
| 598 |
|
| 599 |
def emit_template_struct(self, name, group): |
| 600 |
print("struct %s {" % name) |
| 601 |
group.emit_template_struct("") |
| 602 |
print("};\n") |
| 603 |
|
| 604 |
def emit_aggregate(self): |
| 605 |
aggregate = self.aggregate |
| 606 |
if aggregate.sections: |
| 607 |
print("struct %s_packed {" % aggregate.name.lower()) |
| 608 |
print(" union {") |
| 609 |
print(" uint32_t opaque[{}];".format(aggregate.get_size() // 4)) |
| 610 |
print(" struct {") |
| 611 |
sec_start = 0; |
| 612 |
for section in aggregate.sections: |
| 613 |
while sec_start < section.offset: |
| 614 |
print(" uint32_t __pad{};".format(sec_start)) |
| 615 |
sec_start += 4 |
| 616 |
assert sec_start == section.offset |
| 617 |
print(" struct {}_packed {};".format(section.type_name.lower(), section.name.upper())) |
| 618 |
sec_start += section.type.get_length() |
| 619 |
print(" };") |
| 620 |
print(" };") |
| 621 |
print("};") |
| 622 |
for section in aggregate.sections: |
| 623 |
print('static_assert(offsetof(struct {}_packed, {}) == {}, "");'.format(aggregate.name.lower(), section.name.upper(), section.offset)) |
| 624 |
print("") |
| 625 |
else: |
| 626 |
print("struct %s_packed {" % aggregate.name.lower()) |
| 627 |
print(" uint32_t opaque[{}];".format(aggregate.get_size() // 4)) |
| 628 |
print("};\n") |
| 629 |
print('#define {}_PACKED_T struct {}_packed'.format(aggregate.name.upper(), aggregate.name.lower())) |
| 630 |
print('#define {}_LENGTH {}'.format(aggregate.name.upper(), aggregate.size)) |
| 631 |
if aggregate.align != None: |
| 632 |
print('#define {}_ALIGN {}'.format(aggregate.name.upper(), aggregate.align)) |
| 633 |
for section in aggregate.sections: |
| 634 |
print('#define {}_SECTION_{}_TYPE struct {}'.format(aggregate.name.upper(), section.name.upper(), section.type_name)) |
| 635 |
print('#define {}_SECTION_{}_PACKED_TYPE {}_PACKED_T'.format(aggregate.name.upper(), section.name.upper(), section.type_name.upper())) |
| 636 |
print('#define {}_SECTION_{}_header {}_header'.format(aggregate.name.upper(), section.name.upper(), section.type_name)) |
| 637 |
print('#define {}_SECTION_{}_pack {}_pack'.format(aggregate.name.upper(), section.name.upper(), section.type_name)) |
| 638 |
print('#define {}_SECTION_{}_unpack {}_unpack'.format(aggregate.name.upper(), section.name.upper(), section.type_name)) |
| 639 |
print('#define {}_SECTION_{}_print {}_print'.format(aggregate.name.upper(), section.name.upper(), section.type_name)) |
| 640 |
print('#define {}_SECTION_{}_OFFSET {}'.format(aggregate.name.upper(), section.name.upper(), section.offset)) |
| 641 |
print("") |
| 642 |
|
| 643 |
def emit_struct_detail(self, name, group): |
| 644 |
group.get_length() |
| 645 |
|
| 646 |
# Should be a whole number of words |
| 647 |
assert((self.group.length % 4) == 0) |
| 648 |
|
| 649 |
print('#define {} {}'.format (name + "_LENGTH", self.group.length)) |
| 650 |
if self.group.align != None: |
| 651 |
print('#define {} {}'.format (name + "_ALIGN", self.group.align)) |
| 652 |
print('struct {}_packed {{ uint32_t opaque[{}]; }};'.format(name.lower(), self.group.length // 4)) |
| 653 |
print('#define {}_PACKED_T struct {}_packed'.format(name.upper(), name.lower())) |
| 654 |
|
| 655 |
def emit_pack_function(self, name, group): |
| 656 |
print("#define {}_pack(__packed, __unpacked) \\".format(name)) |
| 657 |
print("do { \\") |
| 658 |
print(" {}_PACKED_T __tmp_packed; \\".format(name.upper())) |
| 659 |
group.emit_pack_function() |
| 660 |
print(' *(__packed) = __tmp_packed; \\') |
| 661 |
print("} while (0);\n") |
| 662 |
|
| 663 |
def emit_unpack_function(self, name, group): |
| 664 |
print("#define {}_unpack(__packed, __unpacked) \\".format(name)) |
| 665 |
print("do { \\") |
| 666 |
print(" {}_PACKED_T __tmp_packed = *(__packed); \\".format(name)) |
| 667 |
group.emit_unpack_function() |
| 668 |
print("} while (0);\n") |
| 669 |
|
| 670 |
def emit_print_function(self, name, group): |
| 671 |
print("#ifndef __OPENCL_VERSION__") |
| 672 |
print("static inline void") |
| 673 |
print("{}_print(FILE *fp, const struct {} * values, unsigned indent)\n{{".format(name.upper(), name)) |
| 674 |
|
| 675 |
group.emit_print_function() |
| 676 |
|
| 677 |
print("}\n") |
| 678 |
print("#endif") |
| 679 |
|
| 680 |
def emit_struct(self): |
| 681 |
name = self.struct |
| 682 |
|
| 683 |
self.emit_template_struct(self.struct, self.group) |
| 684 |
self.emit_header(name) |
| 685 |
if self.no_direct_packing == False: |
| 686 |
self.emit_struct_detail(self.struct, self.group) |
| 687 |
self.emit_pack_function(self.struct, self.group) |
| 688 |
self.emit_unpack_function(self.struct, self.group) |
| 689 |
self.emit_print_function(self.struct, self.group) |
| 690 |
|
| 691 |
def enum_prefix(self, name): |
| 692 |
return |
| 693 |
|
| 694 |
def emit_enum(self): |
| 695 |
e_name = enum_name(self.enum) |
| 696 |
prefix = e_name if self.enum != 'Format' else global_prefix |
| 697 |
print('enum {} {{'.format(e_name)) |
| 698 |
|
| 699 |
for value in self.values: |
| 700 |
name = '{}_{}'.format(prefix, value.name) |
| 701 |
name = safe_name(name).upper() |
| 702 |
print(' % -36s = %6d,' % (name, value.value)) |
| 703 |
print('};\n') |
| 704 |
|
| 705 |
print("#ifndef __OPENCL_VERSION__") |
| 706 |
print("static inline const char *") |
| 707 |
print("{}_as_str(enum {} imm)\n{{".format(e_name.lower(), e_name)) |
| 708 |
print(" switch (imm) {") |
| 709 |
for value in self.values: |
| 710 |
name = '{}_{}'.format(prefix, value.name) |
| 711 |
name = safe_name(name).upper() |
| 712 |
print(' case {}: return "{}";'.format(name, value.name)) |
| 713 |
print(' default: return "XXX: INVALID";') |
| 714 |
print(" }") |
| 715 |
print("}\n") |
| 716 |
print("#endif\n") |
| 717 |
|
| 718 |
def parse(self, filename): |
| 719 |
file = open(filename, "rb") |
| 720 |
self.parser.ParseFile(file) |
| 721 |
file.close() |
| 722 |
|
| 723 |
|
| 724 |
parser = argparse.ArgumentParser() |
| 725 |
parser.add_argument('input_file') |
| 726 |
args = parser.parse_args() |
| 727 |
|
| 728 |
p = Parser() |
| 729 |
p.parse(args.input_file) |