intel/genxml: Convert a not-present-or-"1" dict to a set.
[mesa.git] / src / intel / genxml / gen_pack_header.py
1 #encoding=utf-8
2
3 from __future__ import (
4 absolute_import, division, print_function, unicode_literals
5 )
6 import ast
7 import xml.parsers.expat
8 import re
9 import sys
10 import copy
11 import textwrap
12
13 license = """/*
14 * Copyright (C) 2016 Intel Corporation
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
25 * Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 * IN THE SOFTWARE.
34 */
35 """
36
37 pack_header = """%(license)s
38
39 /* Instructions, enums and structures for %(platform)s.
40 *
41 * This file has been generated, do not hand edit.
42 */
43
44 #ifndef %(guard)s
45 #define %(guard)s
46
47 #include <stdio.h>
48 #include <stdint.h>
49 #include <stdbool.h>
50 #include <assert.h>
51 #include <math.h>
52
53 #ifndef __gen_validate_value
54 #define __gen_validate_value(x)
55 #endif
56
57 #ifndef __gen_field_functions
58 #define __gen_field_functions
59
60 union __gen_value {
61 float f;
62 uint32_t dw;
63 };
64
65 static inline uint64_t
66 __gen_mbo(uint32_t start, uint32_t end)
67 {
68 return (~0ull >> (64 - (end - start + 1))) << start;
69 }
70
71 static inline uint64_t
72 __gen_uint(uint64_t v, uint32_t start, uint32_t end)
73 {
74 __gen_validate_value(v);
75
76 #if DEBUG
77 const int width = end - start + 1;
78 if (width < 64) {
79 const uint64_t max = (1ull << width) - 1;
80 assert(v <= max);
81 }
82 #endif
83
84 return v << start;
85 }
86
87 static inline uint64_t
88 __gen_sint(int64_t v, uint32_t start, uint32_t end)
89 {
90 const int width = end - start + 1;
91
92 __gen_validate_value(v);
93
94 #if DEBUG
95 if (width < 64) {
96 const int64_t max = (1ll << (width - 1)) - 1;
97 const int64_t min = -(1ll << (width - 1));
98 assert(min <= v && v <= max);
99 }
100 #endif
101
102 const uint64_t mask = ~0ull >> (64 - width);
103
104 return (v & mask) << start;
105 }
106
107 static inline uint64_t
108 __gen_offset(uint64_t v, uint32_t start, uint32_t end)
109 {
110 __gen_validate_value(v);
111 #if DEBUG
112 uint64_t mask = (~0ull >> (64 - (end - start + 1))) << start;
113
114 assert((v & ~mask) == 0);
115 #endif
116
117 return v;
118 }
119
120 static inline uint32_t
121 __gen_float(float v)
122 {
123 __gen_validate_value(v);
124 return ((union __gen_value) { .f = (v) }).dw;
125 }
126
127 static inline uint64_t
128 __gen_sfixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
129 {
130 __gen_validate_value(v);
131
132 const float factor = (1 << fract_bits);
133
134 #if DEBUG
135 const float max = ((1 << (end - start)) - 1) / factor;
136 const float min = -(1 << (end - start)) / factor;
137 assert(min <= v && v <= max);
138 #endif
139
140 const int64_t int_val = llroundf(v * factor);
141 const uint64_t mask = ~0ull >> (64 - (end - start + 1));
142
143 return (int_val & mask) << start;
144 }
145
146 static inline uint64_t
147 __gen_ufixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
148 {
149 __gen_validate_value(v);
150
151 const float factor = (1 << fract_bits);
152
153 #if DEBUG
154 const float max = ((1 << (end - start + 1)) - 1) / factor;
155 const float min = 0.0f;
156 assert(min <= v && v <= max);
157 #endif
158
159 const uint64_t uint_val = llroundf(v * factor);
160
161 return uint_val << start;
162 }
163
164 #ifndef __gen_address_type
165 #error #define __gen_address_type before including this file
166 #endif
167
168 #ifndef __gen_user_data
169 #error #define __gen_combine_address before including this file
170 #endif
171
172 #endif
173
174 """
175
176 def to_alphanum(name):
177 substitutions = {
178 ' ': '',
179 '/': '',
180 '[': '',
181 ']': '',
182 '(': '',
183 ')': '',
184 '-': '',
185 ':': '',
186 '.': '',
187 ',': '',
188 '=': '',
189 '>': '',
190 '#': '',
191 'α': 'alpha',
192 '&': '',
193 '*': '',
194 '"': '',
195 '+': '',
196 '\'': '',
197 }
198
199 for i, j in substitutions.items():
200 name = name.replace(i, j)
201
202 return name
203
204 def safe_name(name):
205 name = to_alphanum(name)
206 if not name[0].isalpha():
207 name = '_' + name
208
209 return name
210
211 def num_from_str(num_str):
212 if num_str.lower().startswith('0x'):
213 return int(num_str, base=16)
214 else:
215 assert(not num_str.startswith('0') and 'octals numbers not allowed')
216 return int(num_str)
217
218 class Field(object):
219 ufixed_pattern = re.compile(r"u(\d+)\.(\d+)")
220 sfixed_pattern = re.compile(r"s(\d+)\.(\d+)")
221
222 def __init__(self, parser, attrs):
223 self.parser = parser
224 if "name" in attrs:
225 self.name = safe_name(attrs["name"])
226 self.start = int(attrs["start"])
227 self.end = int(attrs["end"])
228 self.type = attrs["type"]
229
230 if "prefix" in attrs:
231 self.prefix = attrs["prefix"]
232 else:
233 self.prefix = None
234
235 if "default" in attrs:
236 self.default = int(attrs["default"])
237 else:
238 self.default = None
239
240 ufixed_match = Field.ufixed_pattern.match(self.type)
241 if ufixed_match:
242 self.type = 'ufixed'
243 self.fractional_size = int(ufixed_match.group(2))
244
245 sfixed_match = Field.sfixed_pattern.match(self.type)
246 if sfixed_match:
247 self.type = 'sfixed'
248 self.fractional_size = int(sfixed_match.group(2))
249
250 def emit_template_struct(self, dim):
251 if self.type == 'address':
252 type = '__gen_address_type'
253 elif self.type == 'bool':
254 type = 'bool'
255 elif self.type == 'float':
256 type = 'float'
257 elif self.type == 'ufixed':
258 type = 'float'
259 elif self.type == 'sfixed':
260 type = 'float'
261 elif self.type == 'uint' and self.end - self.start > 32:
262 type = 'uint64_t'
263 elif self.type == 'offset':
264 type = 'uint64_t'
265 elif self.type == 'int':
266 type = 'int32_t'
267 elif self.type == 'uint':
268 type = 'uint32_t'
269 elif self.type in self.parser.structs:
270 type = 'struct ' + self.parser.gen_prefix(safe_name(self.type))
271 elif self.type in self.parser.enums:
272 type = 'enum ' + self.parser.gen_prefix(safe_name(self.type))
273 elif self.type == 'mbo':
274 return
275 else:
276 print("#error unhandled type: %s" % self.type)
277 return
278
279 print(" %-36s %s%s;" % (type, self.name, dim))
280
281 prefix = ""
282 if len(self.values) > 0 and self.default == None:
283 if self.prefix:
284 prefix = self.prefix + "_"
285
286 for value in self.values:
287 print("#define %-40s %d" % (prefix + value.name, value.value))
288
289 class Group(object):
290 def __init__(self, parser, parent, start, count, size):
291 self.parser = parser
292 self.parent = parent
293 self.start = start
294 self.count = count
295 self.size = size
296 self.fields = []
297
298 def emit_template_struct(self, dim):
299 if self.count == 0:
300 print(" /* variable length fields follow */")
301 else:
302 if self.count > 1:
303 dim = "%s[%d]" % (dim, self.count)
304
305 for field in self.fields:
306 field.emit_template_struct(dim)
307
308 class DWord:
309 def __init__(self):
310 self.size = 32
311 self.fields = []
312 self.address = None
313
314 def collect_dwords(self, dwords, start, dim):
315 for field in self.fields:
316 if type(field) is Group:
317 if field.count == 1:
318 field.collect_dwords(dwords, start + field.start, dim)
319 else:
320 for i in range(field.count):
321 field.collect_dwords(dwords,
322 start + field.start + i * field.size,
323 "%s[%d]" % (dim, i))
324 continue
325
326 index = (start + field.start) // 32
327 if not index in dwords:
328 dwords[index] = self.DWord()
329
330 clone = copy.copy(field)
331 clone.start = clone.start + start
332 clone.end = clone.end + start
333 clone.dim = dim
334 dwords[index].fields.append(clone)
335
336 if field.type == "address":
337 # assert dwords[index].address == None
338 dwords[index].address = field
339
340 # Coalesce all the dwords covered by this field. The two cases we
341 # handle are where multiple fields are in a 64 bit word (typically
342 # and address and a few bits) or where a single struct field
343 # completely covers multiple dwords.
344 while index < (start + field.end) // 32:
345 if index + 1 in dwords and not dwords[index] == dwords[index + 1]:
346 dwords[index].fields.extend(dwords[index + 1].fields)
347 dwords[index].size = 64
348 dwords[index + 1] = dwords[index]
349 index = index + 1
350
351 def collect_dwords_and_length(self):
352 dwords = {}
353 self.collect_dwords(dwords, 0, "")
354
355 # Determine number of dwords in this group. If we have a size, use
356 # that, since that'll account for MBZ dwords at the end of a group
357 # (like dword 8 on BDW+ 3DSTATE_HS). Otherwise, use the largest dword
358 # index we've seen plus one.
359 if self.size > 0:
360 length = self.size // 32
361 elif dwords:
362 length = max(dwords.keys()) + 1
363 else:
364 length = 0
365
366 return (dwords, length)
367
368 def emit_pack_function(self, dwords, length):
369 for index in range(length):
370 # Handle MBZ dwords
371 if not index in dwords:
372 print("")
373 print(" dw[%d] = 0;" % index)
374 continue
375
376 # For 64 bit dwords, we aliased the two dword entries in the dword
377 # dict it occupies. Now that we're emitting the pack function,
378 # skip the duplicate entries.
379 dw = dwords[index]
380 if index > 0 and index - 1 in dwords and dw == dwords[index - 1]:
381 continue
382
383 # Special case: only one field and it's a struct at the beginning
384 # of the dword. In this case we pack directly into the
385 # destination. This is the only way we handle embedded structs
386 # larger than 32 bits.
387 if len(dw.fields) == 1:
388 field = dw.fields[0]
389 name = field.name + field.dim
390 if field.type in self.parser.structs and field.start % 32 == 0:
391 print("")
392 print(" %s_pack(data, &dw[%d], &values->%s);" %
393 (self.parser.gen_prefix(safe_name(field.type)), index, name))
394 continue
395
396 # Pack any fields of struct type first so we have integer values
397 # to the dword for those fields.
398 field_index = 0
399 for field in dw.fields:
400 if type(field) is Field and field.type in self.parser.structs:
401 name = field.name + field.dim
402 print("")
403 print(" uint32_t v%d_%d;" % (index, field_index))
404 print(" %s_pack(data, &v%d_%d, &values->%s);" %
405 (self.parser.gen_prefix(safe_name(field.type)), index, field_index, name))
406 field_index = field_index + 1
407
408 print("")
409 dword_start = index * 32
410 if dw.address == None:
411 address_count = 0
412 else:
413 address_count = 1
414
415 if dw.size == 32 and dw.address == None:
416 v = None
417 print(" dw[%d] =" % index)
418 elif len(dw.fields) > address_count:
419 v = "v%d" % index
420 print(" const uint%d_t %s =" % (dw.size, v))
421 else:
422 v = "0"
423
424 field_index = 0
425 non_address_fields = []
426 for field in dw.fields:
427 if field.type != "mbo":
428 name = field.name + field.dim
429
430 if field.type == "mbo":
431 non_address_fields.append("__gen_mbo(%d, %d)" % \
432 (field.start - dword_start, field.end - dword_start))
433 elif field.type == "address":
434 pass
435 elif field.type == "uint":
436 non_address_fields.append("__gen_uint(values->%s, %d, %d)" % \
437 (name, field.start - dword_start, field.end - dword_start))
438 elif field.type in self.parser.enums:
439 non_address_fields.append("__gen_uint(values->%s, %d, %d)" % \
440 (name, field.start - dword_start, field.end - dword_start))
441 elif field.type == "int":
442 non_address_fields.append("__gen_sint(values->%s, %d, %d)" % \
443 (name, field.start - dword_start, field.end - dword_start))
444 elif field.type == "bool":
445 non_address_fields.append("__gen_uint(values->%s, %d, %d)" % \
446 (name, field.start - dword_start, field.end - dword_start))
447 elif field.type == "float":
448 non_address_fields.append("__gen_float(values->%s)" % name)
449 elif field.type == "offset":
450 non_address_fields.append("__gen_offset(values->%s, %d, %d)" % \
451 (name, field.start - dword_start, field.end - dword_start))
452 elif field.type == 'ufixed':
453 non_address_fields.append("__gen_ufixed(values->%s, %d, %d, %d)" % \
454 (name, field.start - dword_start, field.end - dword_start, field.fractional_size))
455 elif field.type == 'sfixed':
456 non_address_fields.append("__gen_sfixed(values->%s, %d, %d, %d)" % \
457 (name, field.start - dword_start, field.end - dword_start, field.fractional_size))
458 elif field.type in self.parser.structs:
459 non_address_fields.append("__gen_uint(v%d_%d, %d, %d)" % \
460 (index, field_index, field.start - dword_start, field.end - dword_start))
461 field_index = field_index + 1
462 else:
463 non_address_fields.append("/* unhandled field %s, type %s */\n" % \
464 (name, field.type))
465
466 if len(non_address_fields) > 0:
467 print(" |\n".join(" " + f for f in non_address_fields) + ";")
468
469 if dw.size == 32:
470 if dw.address:
471 print(" dw[%d] = __gen_combine_address(data, &dw[%d], values->%s, %s);" % (index, index, dw.address.name + field.dim, v))
472 continue
473
474 if dw.address:
475 v_address = "v%d_address" % index
476 print(" const uint64_t %s =\n __gen_combine_address(data, &dw[%d], values->%s, %s);" %
477 (v_address, index, dw.address.name + field.dim, v))
478 v = v_address
479
480 print(" dw[%d] = %s;" % (index, v))
481 print(" dw[%d] = %s >> 32;" % (index + 1, v))
482
483 class Value(object):
484 def __init__(self, attrs):
485 self.name = safe_name(attrs["name"])
486 self.value = ast.literal_eval(attrs["value"])
487
488 class Parser(object):
489 def __init__(self):
490 self.parser = xml.parsers.expat.ParserCreate()
491 self.parser.StartElementHandler = self.start_element
492 self.parser.EndElementHandler = self.end_element
493
494 self.instruction = None
495 self.structs = {}
496 # Set of enum names we've seen.
497 self.enums = set()
498 self.registers = {}
499
500 def gen_prefix(self, name):
501 if name[0] == "_":
502 return 'GEN%s%s' % (self.gen, name)
503 else:
504 return 'GEN%s_%s' % (self.gen, name)
505
506 def gen_guard(self):
507 return self.gen_prefix("PACK_H")
508
509 def start_element(self, name, attrs):
510 if name == "genxml":
511 self.platform = attrs["name"]
512 self.gen = attrs["gen"].replace('.', '')
513 print(pack_header % {'license': license, 'platform': self.platform, 'guard': self.gen_guard()})
514 elif name in ("instruction", "struct", "register"):
515 if name == "instruction":
516 self.instruction = safe_name(attrs["name"])
517 self.length_bias = int(attrs["bias"])
518 elif name == "struct":
519 self.struct = safe_name(attrs["name"])
520 self.structs[attrs["name"]] = 1
521 elif name == "register":
522 self.register = safe_name(attrs["name"])
523 self.reg_num = num_from_str(attrs["num"])
524 self.registers[attrs["name"]] = 1
525 if "length" in attrs:
526 self.length = int(attrs["length"])
527 size = self.length * 32
528 else:
529 self.length = None
530 size = 0
531 self.group = Group(self, None, 0, 1, size)
532
533 elif name == "group":
534 group = Group(self, self.group,
535 int(attrs["start"]), int(attrs["count"]), int(attrs["size"]))
536 self.group.fields.append(group)
537 self.group = group
538 elif name == "field":
539 self.group.fields.append(Field(self, attrs))
540 self.values = []
541 elif name == "enum":
542 self.values = []
543 self.enum = safe_name(attrs["name"])
544 self.enums.add(attrs["name"])
545 if "prefix" in attrs:
546 self.prefix = safe_name(attrs["prefix"])
547 else:
548 self.prefix= None
549 elif name == "value":
550 self.values.append(Value(attrs))
551
552 def end_element(self, name):
553 if name == "instruction":
554 self.emit_instruction()
555 self.instruction = None
556 self.group = None
557 elif name == "struct":
558 self.emit_struct()
559 self.struct = None
560 self.group = None
561 elif name == "register":
562 self.emit_register()
563 self.register = None
564 self.reg_num = None
565 self.group = None
566 elif name == "group":
567 self.group = self.group.parent
568 elif name == "field":
569 self.group.fields[-1].values = self.values
570 elif name == "enum":
571 self.emit_enum()
572 self.enum = None
573 elif name == "genxml":
574 print('#endif /* %s */' % self.gen_guard())
575
576 def emit_template_struct(self, name, group):
577 print("struct %s {" % self.gen_prefix(name))
578 group.emit_template_struct("")
579 print("};\n")
580
581 def emit_pack_function(self, name, group):
582 name = self.gen_prefix(name)
583 print(textwrap.dedent("""\
584 static inline void
585 %s_pack(__attribute__((unused)) __gen_user_data *data,
586 %s__attribute__((unused)) void * restrict dst,
587 %s__attribute__((unused)) const struct %s * restrict values)
588 {""") % (name, ' ' * len(name), ' ' * len(name), name))
589
590 (dwords, length) = group.collect_dwords_and_length()
591 if length:
592 # Cast dst to make header C++ friendly
593 print(" uint32_t * restrict dw = (uint32_t * restrict) dst;")
594
595 group.emit_pack_function(dwords, length)
596
597 print("}\n")
598
599 def emit_instruction(self):
600 name = self.instruction
601 if not self.length == None:
602 print('#define %-33s %6d' %
603 (self.gen_prefix(name + "_length"), self.length))
604 print('#define %-33s %6d' %
605 (self.gen_prefix(name + "_length_bias"), self.length_bias))
606
607 default_fields = []
608 for field in self.group.fields:
609 if not type(field) is Field:
610 continue
611 if field.default == None:
612 continue
613 default_fields.append(" .%-35s = %6d" % (field.name, field.default))
614
615 if default_fields:
616 print('#define %-40s\\' % (self.gen_prefix(name + '_header')))
617 print(", \\\n".join(default_fields))
618 print('')
619
620 self.emit_template_struct(self.instruction, self.group)
621
622 self.emit_pack_function(self.instruction, self.group)
623
624 def emit_register(self):
625 name = self.register
626 if not self.reg_num == None:
627 print('#define %-33s 0x%04x' %
628 (self.gen_prefix(name + "_num"), self.reg_num))
629
630 if not self.length == None:
631 print('#define %-33s %6d' %
632 (self.gen_prefix(name + "_length"), self.length))
633
634 self.emit_template_struct(self.register, self.group)
635 self.emit_pack_function(self.register, self.group)
636
637 def emit_struct(self):
638 name = self.struct
639 if not self.length == None:
640 print('#define %-33s %6d' %
641 (self.gen_prefix(name + "_length"), self.length))
642
643 self.emit_template_struct(self.struct, self.group)
644 self.emit_pack_function(self.struct, self.group)
645
646 def emit_enum(self):
647 print('enum %s {' % self.gen_prefix(self.enum))
648 for value in self.values:
649 if self.prefix:
650 name = self.prefix + "_" + value.name
651 else:
652 name = value.name
653 print(' %-36s = %6d,' % (name.upper(), value.value))
654 print('};\n')
655
656 def parse(self, filename):
657 file = open(filename, "rb")
658 self.parser.ParseFile(file)
659 file.close()
660
661 if len(sys.argv) < 2:
662 print("No input xml file specified")
663 sys.exit(1)
664
665 input_file = sys.argv[1]
666
667 p = Parser()
668 p.parse(input_file)