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
61 #define NDEBUG_UNUSED __attribute__((unused))
71 static inline uint64_t
72 __gen_mbo(uint32_t start, uint32_t end)
74 return (~0ull >> (64 - (end - start + 1))) << start;
77 static inline uint64_t
78 __gen_uint(uint64_t v, uint32_t start, NDEBUG_UNUSED uint32_t end)
80 __gen_validate_value(v);
83 const int width = end - start + 1;
85 const uint64_t max = (1ull << width) - 1;
93 static inline uint64_t
94 __gen_sint(int64_t v, uint32_t start, uint32_t end)
96 const int width = end - start + 1;
98 __gen_validate_value(v);
102 const int64_t max = (1ll << (width - 1)) - 1;
103 const int64_t min = -(1ll << (width - 1));
104 assert(min <= v && v <= max);
108 const uint64_t mask = ~0ull >> (64 - width);
110 return (v & mask) << start;
113 static inline uint64_t
114 __gen_offset(uint64_t v, NDEBUG_UNUSED uint32_t start, NDEBUG_UNUSED uint32_t end)
116 __gen_validate_value(v);
118 uint64_t mask = (~0ull >> (64 - (end - start + 1))) << start;
120 assert((v & ~mask) == 0);
126 static inline uint32_t
129 __gen_validate_value(v);
130 return ((union __gen_value) { .f = (v) }).dw;
133 static inline uint64_t
134 __gen_sfixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
136 __gen_validate_value(v);
138 const float factor = (1 << fract_bits);
141 const float max = ((1 << (end - start)) - 1) / factor;
142 const float min = -(1 << (end - start)) / factor;
143 assert(min <= v && v <= max);
146 const int64_t int_val = llroundf(v * factor);
147 const uint64_t mask = ~0ull >> (64 - (end - start + 1));
149 return (int_val & mask) << start;
152 static inline uint64_t
153 __gen_ufixed(float v, uint32_t start, NDEBUG_UNUSED uint32_t end, uint32_t fract_bits)
155 __gen_validate_value(v);
157 const float factor = (1 << fract_bits);
160 const float max = ((1 << (end - start + 1)) - 1) / factor;
161 const float min = 0.0f;
162 assert(min <= v && v <= max);
165 const uint64_t uint_val = llroundf(v * factor);
167 return uint_val << start;
170 #ifndef __gen_address_type
171 #error #define __gen_address_type before including this file
174 #ifndef __gen_user_data
175 #error #define __gen_combine_address before including this file
184 def to_alphanum(name
):
207 for i
, j
in substitutions
.items():
208 name
= name
.replace(i
, j
)
213 name
= to_alphanum(name
)
214 if not name
[0].isalpha():
219 def num_from_str(num_str
):
220 if num_str
.lower().startswith('0x'):
221 return int(num_str
, base
=16)
223 assert not num_str
.startswith('0'), 'octals numbers not allowed'
227 ufixed_pattern
= re
.compile(r
"u(\d+)\.(\d+)")
228 sfixed_pattern
= re
.compile(r
"s(\d+)\.(\d+)")
230 def __init__(self
, parser
, attrs
):
233 self
.name
= safe_name(attrs
["name"])
234 self
.start
= int(attrs
["start"])
235 self
.end
= int(attrs
["end"])
236 self
.type = attrs
["type"]
238 if "prefix" in attrs
:
239 self
.prefix
= attrs
["prefix"]
243 if "default" in attrs
:
244 # Base 0 recognizes 0x, 0o, 0b prefixes in addition to decimal ints.
245 self
.default
= int(attrs
["default"], base
=0)
249 ufixed_match
= Field
.ufixed_pattern
.match(self
.type)
252 self
.fractional_size
= int(ufixed_match
.group(2))
254 sfixed_match
= Field
.sfixed_pattern
.match(self
.type)
257 self
.fractional_size
= int(sfixed_match
.group(2))
259 def is_builtin_type(self
):
260 builtins
= [ 'address', 'bool', 'float', 'ufixed',
261 'offset', 'sfixed', 'offset', 'int', 'uint', 'mbo' ]
262 return self
.type in builtins
264 def is_struct_type(self
):
265 return self
.type in self
.parser
.structs
267 def is_enum_type(self
):
268 return self
.type in self
.parser
.enums
270 def emit_template_struct(self
, dim
):
271 if self
.type == 'address':
272 type = '__gen_address_type'
273 elif self
.type == 'bool':
275 elif self
.type == 'float':
277 elif self
.type == 'ufixed':
279 elif self
.type == 'sfixed':
281 elif self
.type == 'uint' and self
.end
- self
.start
> 32:
283 elif self
.type == 'offset':
285 elif self
.type == 'int':
287 elif self
.type == 'uint':
289 elif self
.is_struct_type():
290 type = 'struct ' + self
.parser
.gen_prefix(safe_name(self
.type))
291 elif self
.is_enum_type():
292 type = 'enum ' + self
.parser
.gen_prefix(safe_name(self
.type))
293 elif self
.type == 'mbo':
296 print("#error unhandled type: %s" % self
.type)
299 print(" %-36s %s%s;" % (type, self
.name
, dim
))
302 if len(self
.values
) > 0 and self
.default
== None:
304 prefix
= self
.prefix
+ "_"
306 for value
in self
.values
:
307 print("#define %-40s %d" % (prefix
+ value
.name
, value
.value
))
310 def __init__(self
, parser
, parent
, start
, count
, size
):
318 def emit_template_struct(self
, dim
):
320 print(" /* variable length fields follow */")
323 dim
= "%s[%d]" % (dim
, self
.count
)
325 for field
in self
.fields
:
326 field
.emit_template_struct(dim
)
334 def collect_dwords(self
, dwords
, start
, dim
):
335 for field
in self
.fields
:
336 if type(field
) is Group
:
338 field
.collect_dwords(dwords
, start
+ field
.start
, dim
)
340 for i
in range(field
.count
):
341 field
.collect_dwords(dwords
,
342 start
+ field
.start
+ i
* field
.size
,
346 index
= (start
+ field
.start
) // 32
347 if not index
in dwords
:
348 dwords
[index
] = self
.DWord()
350 clone
= copy
.copy(field
)
351 clone
.start
= clone
.start
+ start
352 clone
.end
= clone
.end
+ start
354 dwords
[index
].fields
.append(clone
)
356 if field
.type == "address":
357 # assert dwords[index].address == None
358 dwords
[index
].address
= field
360 # Coalesce all the dwords covered by this field. The two cases we
361 # handle are where multiple fields are in a 64 bit word (typically
362 # and address and a few bits) or where a single struct field
363 # completely covers multiple dwords.
364 while index
< (start
+ field
.end
) // 32:
365 if index
+ 1 in dwords
and not dwords
[index
] == dwords
[index
+ 1]:
366 dwords
[index
].fields
.extend(dwords
[index
+ 1].fields
)
367 dwords
[index
].size
= 64
368 dwords
[index
+ 1] = dwords
[index
]
371 def collect_dwords_and_length(self
):
373 self
.collect_dwords(dwords
, 0, "")
375 # Determine number of dwords in this group. If we have a size, use
376 # that, since that'll account for MBZ dwords at the end of a group
377 # (like dword 8 on BDW+ 3DSTATE_HS). Otherwise, use the largest dword
378 # index we've seen plus one.
380 length
= self
.size
// 32
382 length
= max(dwords
.keys()) + 1
386 return (dwords
, length
)
388 def emit_pack_function(self
, dwords
, length
):
389 for index
in range(length
):
391 if not index
in dwords
:
393 print(" dw[%d] = 0;" % index
)
396 # For 64 bit dwords, we aliased the two dword entries in the dword
397 # dict it occupies. Now that we're emitting the pack function,
398 # skip the duplicate entries.
400 if index
> 0 and index
- 1 in dwords
and dw
== dwords
[index
- 1]:
403 # Special case: only one field and it's a struct at the beginning
404 # of the dword. In this case we pack directly into the
405 # destination. This is the only way we handle embedded structs
406 # larger than 32 bits.
407 if len(dw
.fields
) == 1:
409 name
= field
.name
+ field
.dim
410 if field
.is_struct_type() and field
.start
% 32 == 0:
412 print(" %s_pack(data, &dw[%d], &values->%s);" %
413 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, name
))
416 # Pack any fields of struct type first so we have integer values
417 # to the dword for those fields.
419 for field
in dw
.fields
:
420 if type(field
) is Field
and field
.is_struct_type():
421 name
= field
.name
+ field
.dim
423 print(" uint32_t v%d_%d;" % (index
, field_index
))
424 print(" %s_pack(data, &v%d_%d, &values->%s);" %
425 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, field_index
, name
))
426 field_index
= field_index
+ 1
429 dword_start
= index
* 32
430 if dw
.address
== None:
435 if dw
.size
== 32 and dw
.address
== None:
437 print(" dw[%d] =" % index
)
438 elif len(dw
.fields
) > address_count
:
440 print(" const uint%d_t %s =" % (dw
.size
, v
))
445 non_address_fields
= []
446 for field
in dw
.fields
:
447 if field
.type != "mbo":
448 name
= field
.name
+ field
.dim
450 if field
.type == "mbo":
451 non_address_fields
.append("__gen_mbo(%d, %d)" % \
452 (field
.start
- dword_start
, field
.end
- dword_start
))
453 elif field
.type == "address":
455 elif field
.type == "uint":
456 non_address_fields
.append("__gen_uint(values->%s, %d, %d)" % \
457 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
458 elif field
.is_enum_type():
459 non_address_fields
.append("__gen_uint(values->%s, %d, %d)" % \
460 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
461 elif field
.type == "int":
462 non_address_fields
.append("__gen_sint(values->%s, %d, %d)" % \
463 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
464 elif field
.type == "bool":
465 non_address_fields
.append("__gen_uint(values->%s, %d, %d)" % \
466 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
467 elif field
.type == "float":
468 non_address_fields
.append("__gen_float(values->%s)" % name
)
469 elif field
.type == "offset":
470 non_address_fields
.append("__gen_offset(values->%s, %d, %d)" % \
471 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
472 elif field
.type == 'ufixed':
473 non_address_fields
.append("__gen_ufixed(values->%s, %d, %d, %d)" % \
474 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
))
475 elif field
.type == 'sfixed':
476 non_address_fields
.append("__gen_sfixed(values->%s, %d, %d, %d)" % \
477 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
))
478 elif field
.is_struct_type():
479 non_address_fields
.append("__gen_uint(v%d_%d, %d, %d)" % \
480 (index
, field_index
, field
.start
- dword_start
, field
.end
- dword_start
))
481 field_index
= field_index
+ 1
483 non_address_fields
.append("/* unhandled field %s, type %s */\n" % \
486 if len(non_address_fields
) > 0:
487 print(" |\n".join(" " + f
for f
in non_address_fields
) + ";")
491 print(" dw[%d] = __gen_combine_address(data, &dw[%d], values->%s, %s);" % (index
, index
, dw
.address
.name
+ field
.dim
, v
))
495 v_address
= "v%d_address" % index
496 print(" const uint64_t %s =\n __gen_combine_address(data, &dw[%d], values->%s, %s);" %
497 (v_address
, index
, dw
.address
.name
+ field
.dim
, v
))
498 if len(dw
.fields
) > address_count
:
499 print(" dw[%d] = %s;" % (index
, v_address
))
500 print(" dw[%d] = (%s >> 32) | (%s >> 32);" % (index
+ 1, v_address
, v
))
504 print(" dw[%d] = %s;" % (index
, v
))
505 print(" dw[%d] = %s >> 32;" % (index
+ 1, v
))
508 def __init__(self
, attrs
):
509 self
.name
= safe_name(attrs
["name"])
510 self
.value
= ast
.literal_eval(attrs
["value"])
512 class Parser(object):
514 self
.parser
= xml
.parsers
.expat
.ParserCreate()
515 self
.parser
.StartElementHandler
= self
.start_element
516 self
.parser
.EndElementHandler
= self
.end_element
518 self
.instruction
= None
520 # Set of enum names we've seen.
524 def gen_prefix(self
, name
):
526 return 'GEN%s%s' % (self
.gen
, name
)
528 return 'GEN%s_%s' % (self
.gen
, name
)
531 return self
.gen_prefix("PACK_H")
533 def start_element(self
, name
, attrs
):
535 self
.platform
= attrs
["name"]
536 self
.gen
= attrs
["gen"].replace('.', '')
537 print(pack_header
% {'license': license
, 'platform': self
.platform
, 'guard': self
.gen_guard()})
538 elif name
in ("instruction", "struct", "register"):
539 if name
== "instruction":
540 self
.instruction
= safe_name(attrs
["name"])
541 self
.length_bias
= int(attrs
["bias"])
542 elif name
== "struct":
543 self
.struct
= safe_name(attrs
["name"])
544 self
.structs
[attrs
["name"]] = 1
545 elif name
== "register":
546 self
.register
= safe_name(attrs
["name"])
547 self
.reg_num
= num_from_str(attrs
["num"])
548 self
.registers
[attrs
["name"]] = 1
549 if "length" in attrs
:
550 self
.length
= int(attrs
["length"])
551 size
= self
.length
* 32
555 self
.group
= Group(self
, None, 0, 1, size
)
557 elif name
== "group":
558 group
= Group(self
, self
.group
,
559 int(attrs
["start"]), int(attrs
["count"]), int(attrs
["size"]))
560 self
.group
.fields
.append(group
)
562 elif name
== "field":
563 self
.group
.fields
.append(Field(self
, attrs
))
567 self
.enum
= safe_name(attrs
["name"])
568 self
.enums
.add(attrs
["name"])
569 if "prefix" in attrs
:
570 self
.prefix
= safe_name(attrs
["prefix"])
573 elif name
== "value":
574 self
.values
.append(Value(attrs
))
576 def end_element(self
, name
):
577 if name
== "instruction":
578 self
.emit_instruction()
579 self
.instruction
= None
581 elif name
== "struct":
585 elif name
== "register":
590 elif name
== "group":
591 self
.group
= self
.group
.parent
592 elif name
== "field":
593 self
.group
.fields
[-1].values
= self
.values
597 elif name
== "genxml":
598 print('#endif /* %s */' % self
.gen_guard())
600 def emit_template_struct(self
, name
, group
):
601 print("struct %s {" % self
.gen_prefix(name
))
602 group
.emit_template_struct("")
605 def emit_pack_function(self
, name
, group
):
606 name
= self
.gen_prefix(name
)
607 print(textwrap
.dedent("""\
609 %s_pack(__attribute__((unused)) __gen_user_data *data,
610 %s__attribute__((unused)) void * restrict dst,
611 %s__attribute__((unused)) const struct %s * restrict values)
612 {""") % (name
, ' ' * len(name
), ' ' * len(name
), name
))
614 (dwords
, length
) = group
.collect_dwords_and_length()
616 # Cast dst to make header C++ friendly
617 print(" uint32_t * restrict dw = (uint32_t * restrict) dst;")
619 group
.emit_pack_function(dwords
, length
)
623 def emit_instruction(self
):
624 name
= self
.instruction
625 if not self
.length
== None:
626 print('#define %-33s %6d' %
627 (self
.gen_prefix(name
+ "_length"), self
.length
))
628 print('#define %-33s %6d' %
629 (self
.gen_prefix(name
+ "_length_bias"), self
.length_bias
))
632 for field
in self
.group
.fields
:
633 if not type(field
) is Field
:
635 if field
.default
== None:
637 default_fields
.append(" .%-35s = %6d" % (field
.name
, field
.default
))
640 print('#define %-40s\\' % (self
.gen_prefix(name
+ '_header')))
641 print(", \\\n".join(default_fields
))
644 self
.emit_template_struct(self
.instruction
, self
.group
)
646 self
.emit_pack_function(self
.instruction
, self
.group
)
648 def emit_register(self
):
650 if not self
.reg_num
== None:
651 print('#define %-33s 0x%04x' %
652 (self
.gen_prefix(name
+ "_num"), self
.reg_num
))
654 if not self
.length
== None:
655 print('#define %-33s %6d' %
656 (self
.gen_prefix(name
+ "_length"), self
.length
))
658 self
.emit_template_struct(self
.register
, self
.group
)
659 self
.emit_pack_function(self
.register
, self
.group
)
661 def emit_struct(self
):
663 if not self
.length
== None:
664 print('#define %-33s %6d' %
665 (self
.gen_prefix(name
+ "_length"), self
.length
))
667 self
.emit_template_struct(self
.struct
, self
.group
)
668 self
.emit_pack_function(self
.struct
, self
.group
)
671 print('enum %s {' % self
.gen_prefix(self
.enum
))
672 for value
in self
.values
:
674 name
= self
.prefix
+ "_" + value
.name
677 print(' %-36s = %6d,' % (name
.upper(), value
.value
))
680 def parse(self
, filename
):
681 file = open(filename
, "rb")
682 self
.parser
.ParseFile(file)
685 if len(sys
.argv
) < 2:
686 print("No input xml file specified")
689 input_file
= sys
.argv
[1]