3 from __future__
import (
4 absolute_import
, division
, print_function
, unicode_literals
7 import xml
.parsers
.expat
14 * Copyright (C) 2016 Intel Corporation
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
37 pack_header
= """%(license)s
39 /* Instructions, enums and structures for %(platform)s.
41 * This file has been generated, do not hand edit.
53 #ifndef __gen_validate_value
54 #define __gen_validate_value(x)
57 #ifndef __gen_field_functions
58 #define __gen_field_functions
65 static inline uint64_t
66 __gen_mbo(uint32_t start, uint32_t end)
68 return (~0ull >> (64 - (end - start + 1))) << start;
71 static inline uint64_t
72 __gen_uint(uint64_t v, uint32_t start, uint32_t end)
74 __gen_validate_value(v);
77 const int width = end - start + 1;
79 const uint64_t max = (1ull << width) - 1;
87 static inline uint64_t
88 __gen_sint(int64_t v, uint32_t start, uint32_t end)
90 const int width = end - start + 1;
92 __gen_validate_value(v);
96 const int64_t max = (1ll << (width - 1)) - 1;
97 const int64_t min = -(1ll << (width - 1));
98 assert(min <= v && v <= max);
102 const uint64_t mask = ~0ull >> (64 - width);
104 return (v & mask) << start;
107 static inline uint64_t
108 __gen_offset(uint64_t v, uint32_t start, uint32_t end)
110 __gen_validate_value(v);
112 uint64_t mask = (~0ull >> (64 - (end - start + 1))) << start;
114 assert((v & ~mask) == 0);
120 static inline uint32_t
123 __gen_validate_value(v);
124 return ((union __gen_value) { .f = (v) }).dw;
127 static inline uint64_t
128 __gen_sfixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
130 __gen_validate_value(v);
132 const float factor = (1 << fract_bits);
135 const float max = ((1 << (end - start)) - 1) / factor;
136 const float min = -(1 << (end - start)) / factor;
137 assert(min <= v && v <= max);
140 const int64_t int_val = llroundf(v * factor);
141 const uint64_t mask = ~0ull >> (64 - (end - start + 1));
143 return (int_val & mask) << start;
146 static inline uint64_t
147 __gen_ufixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
149 __gen_validate_value(v);
151 const float factor = (1 << fract_bits);
154 const float max = ((1 << (end - start + 1)) - 1) / factor;
155 const float min = 0.0f;
156 assert(min <= v && v <= max);
159 const uint64_t uint_val = llroundf(v * factor);
161 return uint_val << start;
164 #ifndef __gen_address_type
165 #error #define __gen_address_type before including this file
168 #ifndef __gen_user_data
169 #error #define __gen_combine_address before including this file
176 def to_alphanum(name
):
199 for i
, j
in substitutions
.items():
200 name
= name
.replace(i
, j
)
205 name
= to_alphanum(name
)
206 if not name
[0].isalpha():
211 def num_from_str(num_str
):
212 if num_str
.lower().startswith('0x'):
213 return int(num_str
, base
=16)
215 assert(not num_str
.startswith('0') and 'octals numbers not allowed')
219 ufixed_pattern
= re
.compile(r
"u(\d+)\.(\d+)")
220 sfixed_pattern
= re
.compile(r
"s(\d+)\.(\d+)")
222 def __init__(self
, parser
, attrs
):
225 self
.name
= safe_name(attrs
["name"])
226 self
.start
= int(attrs
["start"])
227 self
.end
= int(attrs
["end"])
228 self
.type = attrs
["type"]
230 if "prefix" in attrs
:
231 self
.prefix
= attrs
["prefix"]
235 if "default" in attrs
:
236 self
.default
= int(attrs
["default"])
240 ufixed_match
= Field
.ufixed_pattern
.match(self
.type)
243 self
.fractional_size
= int(ufixed_match
.group(2))
245 sfixed_match
= Field
.sfixed_pattern
.match(self
.type)
248 self
.fractional_size
= int(sfixed_match
.group(2))
250 def emit_template_struct(self
, dim
):
251 if self
.type == 'address':
252 type = '__gen_address_type'
253 elif self
.type == 'bool':
255 elif self
.type == 'float':
257 elif self
.type == 'ufixed':
259 elif self
.type == 'sfixed':
261 elif self
.type == 'uint' and self
.end
- self
.start
> 32:
263 elif self
.type == 'offset':
265 elif self
.type == 'int':
267 elif self
.type == 'uint':
269 elif self
.type in self
.parser
.structs
:
270 type = 'struct ' + self
.parser
.gen_prefix(safe_name(self
.type))
271 elif self
.type in self
.parser
.enums
:
272 type = 'enum ' + self
.parser
.gen_prefix(safe_name(self
.type))
273 elif self
.type == 'mbo':
276 print("#error unhandled type: %s" % self
.type)
279 print(" %-36s %s%s;" % (type, self
.name
, dim
))
282 if len(self
.values
) > 0 and self
.default
== None:
284 prefix
= self
.prefix
+ "_"
286 for value
in self
.values
:
287 print("#define %-40s %d" % (prefix
+ value
.name
, value
.value
))
290 def __init__(self
, parser
, parent
, start
, count
, size
):
298 def emit_template_struct(self
, dim
):
300 print(" /* variable length fields follow */")
303 dim
= "%s[%d]" % (dim
, self
.count
)
305 for field
in self
.fields
:
306 field
.emit_template_struct(dim
)
314 def collect_dwords(self
, dwords
, start
, dim
):
315 for field
in self
.fields
:
316 if type(field
) is Group
:
318 field
.collect_dwords(dwords
, start
+ field
.start
, dim
)
320 for i
in range(field
.count
):
321 field
.collect_dwords(dwords
,
322 start
+ field
.start
+ i
* field
.size
,
326 index
= (start
+ field
.start
) // 32
327 if not index
in dwords
:
328 dwords
[index
] = self
.DWord()
330 clone
= copy
.copy(field
)
331 clone
.start
= clone
.start
+ start
332 clone
.end
= clone
.end
+ start
334 dwords
[index
].fields
.append(clone
)
336 if field
.type == "address":
337 # assert dwords[index].address == None
338 dwords
[index
].address
= field
340 # Coalesce all the dwords covered by this field. The two cases we
341 # handle are where multiple fields are in a 64 bit word (typically
342 # and address and a few bits) or where a single struct field
343 # completely covers multiple dwords.
344 while index
< (start
+ field
.end
) // 32:
345 if index
+ 1 in dwords
and not dwords
[index
] == dwords
[index
+ 1]:
346 dwords
[index
].fields
.extend(dwords
[index
+ 1].fields
)
347 dwords
[index
].size
= 64
348 dwords
[index
+ 1] = dwords
[index
]
351 def collect_dwords_and_length(self
):
353 self
.collect_dwords(dwords
, 0, "")
355 # Determine number of dwords in this group. If we have a size, use
356 # that, since that'll account for MBZ dwords at the end of a group
357 # (like dword 8 on BDW+ 3DSTATE_HS). Otherwise, use the largest dword
358 # index we've seen plus one.
360 length
= self
.size
// 32
362 length
= max(dwords
.keys()) + 1
366 return (dwords
, length
)
368 def emit_pack_function(self
, dwords
, length
):
369 for index
in range(length
):
371 if not index
in dwords
:
373 print(" dw[%d] = 0;" % index
)
376 # For 64 bit dwords, we aliased the two dword entries in the dword
377 # dict it occupies. Now that we're emitting the pack function,
378 # skip the duplicate entries.
380 if index
> 0 and index
- 1 in dwords
and dw
== dwords
[index
- 1]:
383 # Special case: only one field and it's a struct at the beginning
384 # of the dword. In this case we pack directly into the
385 # destination. This is the only way we handle embedded structs
386 # larger than 32 bits.
387 if len(dw
.fields
) == 1:
389 name
= field
.name
+ field
.dim
390 if field
.type in self
.parser
.structs
and field
.start
% 32 == 0:
392 print(" %s_pack(data, &dw[%d], &values->%s);" %
393 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, name
))
396 # Pack any fields of struct type first so we have integer values
397 # to the dword for those fields.
399 for field
in dw
.fields
:
400 if type(field
) is Field
and field
.type in self
.parser
.structs
:
401 name
= field
.name
+ field
.dim
403 print(" uint32_t v%d_%d;" % (index
, field_index
))
404 print(" %s_pack(data, &v%d_%d, &values->%s);" %
405 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, field_index
, name
))
406 field_index
= field_index
+ 1
409 dword_start
= index
* 32
410 if dw
.address
== None:
415 if dw
.size
== 32 and dw
.address
== None:
417 print(" dw[%d] =" % index
)
418 elif len(dw
.fields
) > address_count
:
420 print(" const uint%d_t %s =" % (dw
.size
, v
))
425 non_address_fields
= []
426 for field
in dw
.fields
:
427 if field
.type != "mbo":
428 name
= field
.name
+ field
.dim
430 if field
.type == "mbo":
431 non_address_fields
.append("__gen_mbo(%d, %d)" % \
432 (field
.start
- dword_start
, field
.end
- dword_start
))
433 elif field
.type == "address":
435 elif field
.type == "uint":
436 non_address_fields
.append("__gen_uint(values->%s, %d, %d)" % \
437 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
438 elif field
.type in self
.parser
.enums
:
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 == "int":
442 non_address_fields
.append("__gen_sint(values->%s, %d, %d)" % \
443 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
444 elif field
.type == "bool":
445 non_address_fields
.append("__gen_uint(values->%s, %d, %d)" % \
446 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
447 elif field
.type == "float":
448 non_address_fields
.append("__gen_float(values->%s)" % name
)
449 elif field
.type == "offset":
450 non_address_fields
.append("__gen_offset(values->%s, %d, %d)" % \
451 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
452 elif field
.type == 'ufixed':
453 non_address_fields
.append("__gen_ufixed(values->%s, %d, %d, %d)" % \
454 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
))
455 elif field
.type == 'sfixed':
456 non_address_fields
.append("__gen_sfixed(values->%s, %d, %d, %d)" % \
457 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
))
458 elif field
.type in self
.parser
.structs
:
459 non_address_fields
.append("__gen_uint(v%d_%d, %d, %d)" % \
460 (index
, field_index
, field
.start
- dword_start
, field
.end
- dword_start
))
461 field_index
= field_index
+ 1
463 non_address_fields
.append("/* unhandled field %s, type %s */\n" % \
466 if len(non_address_fields
) > 0:
467 print(" |\n".join(" " + f
for f
in non_address_fields
) + ";")
471 print(" dw[%d] = __gen_combine_address(data, &dw[%d], values->%s, %s);" % (index
, index
, dw
.address
.name
+ field
.dim
, v
))
475 v_address
= "v%d_address" % index
476 print(" const uint64_t %s =\n __gen_combine_address(data, &dw[%d], values->%s, %s);" %
477 (v_address
, index
, dw
.address
.name
+ field
.dim
, v
))
480 print(" dw[%d] = %s;" % (index
, v
))
481 print(" dw[%d] = %s >> 32;" % (index
+ 1, v
))
484 def __init__(self
, attrs
):
485 self
.name
= safe_name(attrs
["name"])
486 self
.value
= ast
.literal_eval(attrs
["value"])
488 class Parser(object):
490 self
.parser
= xml
.parsers
.expat
.ParserCreate()
491 self
.parser
.StartElementHandler
= self
.start_element
492 self
.parser
.EndElementHandler
= self
.end_element
494 self
.instruction
= None
496 # Set of enum names we've seen.
500 def gen_prefix(self
, name
):
502 return 'GEN%s%s' % (self
.gen
, name
)
504 return 'GEN%s_%s' % (self
.gen
, name
)
507 return self
.gen_prefix("PACK_H")
509 def start_element(self
, name
, attrs
):
511 self
.platform
= attrs
["name"]
512 self
.gen
= attrs
["gen"].replace('.', '')
513 print(pack_header
% {'license': license
, 'platform': self
.platform
, 'guard': self
.gen_guard()})
514 elif name
in ("instruction", "struct", "register"):
515 if name
== "instruction":
516 self
.instruction
= safe_name(attrs
["name"])
517 self
.length_bias
= int(attrs
["bias"])
518 elif name
== "struct":
519 self
.struct
= safe_name(attrs
["name"])
520 self
.structs
[attrs
["name"]] = 1
521 elif name
== "register":
522 self
.register
= safe_name(attrs
["name"])
523 self
.reg_num
= num_from_str(attrs
["num"])
524 self
.registers
[attrs
["name"]] = 1
525 if "length" in attrs
:
526 self
.length
= int(attrs
["length"])
527 size
= self
.length
* 32
531 self
.group
= Group(self
, None, 0, 1, size
)
533 elif name
== "group":
534 group
= Group(self
, self
.group
,
535 int(attrs
["start"]), int(attrs
["count"]), int(attrs
["size"]))
536 self
.group
.fields
.append(group
)
538 elif name
== "field":
539 self
.group
.fields
.append(Field(self
, attrs
))
543 self
.enum
= safe_name(attrs
["name"])
544 self
.enums
.add(attrs
["name"])
545 if "prefix" in attrs
:
546 self
.prefix
= safe_name(attrs
["prefix"])
549 elif name
== "value":
550 self
.values
.append(Value(attrs
))
552 def end_element(self
, name
):
553 if name
== "instruction":
554 self
.emit_instruction()
555 self
.instruction
= None
557 elif name
== "struct":
561 elif name
== "register":
566 elif name
== "group":
567 self
.group
= self
.group
.parent
568 elif name
== "field":
569 self
.group
.fields
[-1].values
= self
.values
573 elif name
== "genxml":
574 print('#endif /* %s */' % self
.gen_guard())
576 def emit_template_struct(self
, name
, group
):
577 print("struct %s {" % self
.gen_prefix(name
))
578 group
.emit_template_struct("")
581 def emit_pack_function(self
, name
, group
):
582 name
= self
.gen_prefix(name
)
583 print(textwrap
.dedent("""\
585 %s_pack(__attribute__((unused)) __gen_user_data *data,
586 %s__attribute__((unused)) void * restrict dst,
587 %s__attribute__((unused)) const struct %s * restrict values)
588 {""") % (name
, ' ' * len(name
), ' ' * len(name
), name
))
590 (dwords
, length
) = group
.collect_dwords_and_length()
592 # Cast dst to make header C++ friendly
593 print(" uint32_t * restrict dw = (uint32_t * restrict) dst;")
595 group
.emit_pack_function(dwords
, length
)
599 def emit_instruction(self
):
600 name
= self
.instruction
601 if not self
.length
== None:
602 print('#define %-33s %6d' %
603 (self
.gen_prefix(name
+ "_length"), self
.length
))
604 print('#define %-33s %6d' %
605 (self
.gen_prefix(name
+ "_length_bias"), self
.length_bias
))
608 for field
in self
.group
.fields
:
609 if not type(field
) is Field
:
611 if field
.default
== None:
613 default_fields
.append(" .%-35s = %6d" % (field
.name
, field
.default
))
616 print('#define %-40s\\' % (self
.gen_prefix(name
+ '_header')))
617 print(", \\\n".join(default_fields
))
620 self
.emit_template_struct(self
.instruction
, self
.group
)
622 self
.emit_pack_function(self
.instruction
, self
.group
)
624 def emit_register(self
):
626 if not self
.reg_num
== None:
627 print('#define %-33s 0x%04x' %
628 (self
.gen_prefix(name
+ "_num"), self
.reg_num
))
630 if not self
.length
== None:
631 print('#define %-33s %6d' %
632 (self
.gen_prefix(name
+ "_length"), self
.length
))
634 self
.emit_template_struct(self
.register
, self
.group
)
635 self
.emit_pack_function(self
.register
, self
.group
)
637 def emit_struct(self
):
639 if not self
.length
== None:
640 print('#define %-33s %6d' %
641 (self
.gen_prefix(name
+ "_length"), self
.length
))
643 self
.emit_template_struct(self
.struct
, self
.group
)
644 self
.emit_pack_function(self
.struct
, self
.group
)
647 print('enum %s {' % self
.gen_prefix(self
.enum
))
648 for value
in self
.values
:
650 name
= self
.prefix
+ "_" + value
.name
653 print(' %-36s = %6d,' % (name
.upper(), value
.value
))
656 def parse(self
, filename
):
657 file = open(filename
, "rb")
658 self
.parser
.ParseFile(file)
661 if len(sys
.argv
) < 2:
662 print("No input xml file specified")
665 input_file
= sys
.argv
[1]