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