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