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