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