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