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