e6cea8646ff3aabc6db673adc27006d10fd6cb0e
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 is_builtin_type(self
):
251 builtins
= [ 'address', 'bool', 'float', 'ufixed',
252 'offset', 'sfixed', 'offset', 'int', 'uint', 'mbo' ]
253 return self
.type in builtins
255 def is_struct_type(self
):
256 return self
.type in self
.parser
.structs
258 def is_enum_type(self
):
259 return self
.type in self
.parser
.enums
261 def emit_template_struct(self
, dim
):
262 if self
.type == 'address':
263 type = '__gen_address_type'
264 elif self
.type == 'bool':
266 elif self
.type == 'float':
268 elif self
.type == 'ufixed':
270 elif self
.type == 'sfixed':
272 elif self
.type == 'uint' and self
.end
- self
.start
> 32:
274 elif self
.type == 'offset':
276 elif self
.type == 'int':
278 elif self
.type == 'uint':
280 elif self
.is_struct_type():
281 type = 'struct ' + self
.parser
.gen_prefix(safe_name(self
.type))
282 elif self
.is_enum_type():
283 type = 'enum ' + self
.parser
.gen_prefix(safe_name(self
.type))
284 elif self
.type == 'mbo':
287 print("#error unhandled type: %s" % self
.type)
290 print(" %-36s %s%s;" % (type, self
.name
, dim
))
293 if len(self
.values
) > 0 and self
.default
== None:
295 prefix
= self
.prefix
+ "_"
297 for value
in self
.values
:
298 print("#define %-40s %d" % (prefix
+ value
.name
, value
.value
))
301 def __init__(self
, parser
, parent
, start
, count
, size
):
309 def emit_template_struct(self
, dim
):
311 print(" /* variable length fields follow */")
314 dim
= "%s[%d]" % (dim
, self
.count
)
316 for field
in self
.fields
:
317 field
.emit_template_struct(dim
)
325 def collect_dwords(self
, dwords
, start
, dim
):
326 for field
in self
.fields
:
327 if type(field
) is Group
:
329 field
.collect_dwords(dwords
, start
+ field
.start
, dim
)
331 for i
in range(field
.count
):
332 field
.collect_dwords(dwords
,
333 start
+ field
.start
+ i
* field
.size
,
337 index
= (start
+ field
.start
) // 32
338 if not index
in dwords
:
339 dwords
[index
] = self
.DWord()
341 clone
= copy
.copy(field
)
342 clone
.start
= clone
.start
+ start
343 clone
.end
= clone
.end
+ start
345 dwords
[index
].fields
.append(clone
)
347 if field
.type == "address":
348 # assert dwords[index].address == None
349 dwords
[index
].address
= field
351 # Coalesce all the dwords covered by this field. The two cases we
352 # handle are where multiple fields are in a 64 bit word (typically
353 # and address and a few bits) or where a single struct field
354 # completely covers multiple dwords.
355 while index
< (start
+ field
.end
) // 32:
356 if index
+ 1 in dwords
and not dwords
[index
] == dwords
[index
+ 1]:
357 dwords
[index
].fields
.extend(dwords
[index
+ 1].fields
)
358 dwords
[index
].size
= 64
359 dwords
[index
+ 1] = dwords
[index
]
362 def collect_dwords_and_length(self
):
364 self
.collect_dwords(dwords
, 0, "")
366 # Determine number of dwords in this group. If we have a size, use
367 # that, since that'll account for MBZ dwords at the end of a group
368 # (like dword 8 on BDW+ 3DSTATE_HS). Otherwise, use the largest dword
369 # index we've seen plus one.
371 length
= self
.size
// 32
373 length
= max(dwords
.keys()) + 1
377 return (dwords
, length
)
379 def emit_pack_function(self
, dwords
, length
):
380 for index
in range(length
):
382 if not index
in dwords
:
384 print(" dw[%d] = 0;" % index
)
387 # For 64 bit dwords, we aliased the two dword entries in the dword
388 # dict it occupies. Now that we're emitting the pack function,
389 # skip the duplicate entries.
391 if index
> 0 and index
- 1 in dwords
and dw
== dwords
[index
- 1]:
394 # Special case: only one field and it's a struct at the beginning
395 # of the dword. In this case we pack directly into the
396 # destination. This is the only way we handle embedded structs
397 # larger than 32 bits.
398 if len(dw
.fields
) == 1:
400 name
= field
.name
+ field
.dim
401 if field
.is_struct_type() and field
.start
% 32 == 0:
403 print(" %s_pack(data, &dw[%d], &values->%s);" %
404 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, name
))
407 # Pack any fields of struct type first so we have integer values
408 # to the dword for those fields.
410 for field
in dw
.fields
:
411 if type(field
) is Field
and field
.is_struct_type():
412 name
= field
.name
+ field
.dim
414 print(" uint32_t v%d_%d;" % (index
, field_index
))
415 print(" %s_pack(data, &v%d_%d, &values->%s);" %
416 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, field_index
, name
))
417 field_index
= field_index
+ 1
420 dword_start
= index
* 32
421 if dw
.address
== None:
426 if dw
.size
== 32 and dw
.address
== None:
428 print(" dw[%d] =" % index
)
429 elif len(dw
.fields
) > address_count
:
431 print(" const uint%d_t %s =" % (dw
.size
, v
))
436 non_address_fields
= []
437 for field
in dw
.fields
:
438 if field
.type != "mbo":
439 name
= field
.name
+ field
.dim
441 if field
.type == "mbo":
442 non_address_fields
.append("__gen_mbo(%d, %d)" % \
443 (field
.start
- dword_start
, field
.end
- dword_start
))
444 elif field
.type == "address":
446 elif field
.type == "uint":
447 non_address_fields
.append("__gen_uint(values->%s, %d, %d)" % \
448 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
449 elif field
.is_enum_type():
450 non_address_fields
.append("__gen_uint(values->%s, %d, %d)" % \
451 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
452 elif field
.type == "int":
453 non_address_fields
.append("__gen_sint(values->%s, %d, %d)" % \
454 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
455 elif field
.type == "bool":
456 non_address_fields
.append("__gen_uint(values->%s, %d, %d)" % \
457 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
458 elif field
.type == "float":
459 non_address_fields
.append("__gen_float(values->%s)" % name
)
460 elif field
.type == "offset":
461 non_address_fields
.append("__gen_offset(values->%s, %d, %d)" % \
462 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
463 elif field
.type == 'ufixed':
464 non_address_fields
.append("__gen_ufixed(values->%s, %d, %d, %d)" % \
465 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
))
466 elif field
.type == 'sfixed':
467 non_address_fields
.append("__gen_sfixed(values->%s, %d, %d, %d)" % \
468 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
))
469 elif field
.is_struct_type():
470 non_address_fields
.append("__gen_uint(v%d_%d, %d, %d)" % \
471 (index
, field_index
, field
.start
- dword_start
, field
.end
- dword_start
))
472 field_index
= field_index
+ 1
474 non_address_fields
.append("/* unhandled field %s, type %s */\n" % \
477 if len(non_address_fields
) > 0:
478 print(" |\n".join(" " + f
for f
in non_address_fields
) + ";")
482 print(" dw[%d] = __gen_combine_address(data, &dw[%d], values->%s, %s);" % (index
, index
, dw
.address
.name
+ field
.dim
, v
))
486 v_address
= "v%d_address" % index
487 print(" const uint64_t %s =\n __gen_combine_address(data, &dw[%d], values->%s, %s);" %
488 (v_address
, index
, dw
.address
.name
+ field
.dim
, v
))
491 print(" dw[%d] = %s;" % (index
, v
))
492 print(" dw[%d] = %s >> 32;" % (index
+ 1, v
))
495 def __init__(self
, attrs
):
496 self
.name
= safe_name(attrs
["name"])
497 self
.value
= ast
.literal_eval(attrs
["value"])
499 class Parser(object):
501 self
.parser
= xml
.parsers
.expat
.ParserCreate()
502 self
.parser
.StartElementHandler
= self
.start_element
503 self
.parser
.EndElementHandler
= self
.end_element
505 self
.instruction
= None
507 # Set of enum names we've seen.
511 def gen_prefix(self
, name
):
513 return 'GEN%s%s' % (self
.gen
, name
)
515 return 'GEN%s_%s' % (self
.gen
, name
)
518 return self
.gen_prefix("PACK_H")
520 def start_element(self
, name
, attrs
):
522 self
.platform
= attrs
["name"]
523 self
.gen
= attrs
["gen"].replace('.', '')
524 print(pack_header
% {'license': license
, 'platform': self
.platform
, 'guard': self
.gen_guard()})
525 elif name
in ("instruction", "struct", "register"):
526 if name
== "instruction":
527 self
.instruction
= safe_name(attrs
["name"])
528 self
.length_bias
= int(attrs
["bias"])
529 elif name
== "struct":
530 self
.struct
= safe_name(attrs
["name"])
531 self
.structs
[attrs
["name"]] = 1
532 elif name
== "register":
533 self
.register
= safe_name(attrs
["name"])
534 self
.reg_num
= num_from_str(attrs
["num"])
535 self
.registers
[attrs
["name"]] = 1
536 if "length" in attrs
:
537 self
.length
= int(attrs
["length"])
538 size
= self
.length
* 32
542 self
.group
= Group(self
, None, 0, 1, size
)
544 elif name
== "group":
545 group
= Group(self
, self
.group
,
546 int(attrs
["start"]), int(attrs
["count"]), int(attrs
["size"]))
547 self
.group
.fields
.append(group
)
549 elif name
== "field":
550 self
.group
.fields
.append(Field(self
, attrs
))
554 self
.enum
= safe_name(attrs
["name"])
555 self
.enums
.add(attrs
["name"])
556 if "prefix" in attrs
:
557 self
.prefix
= safe_name(attrs
["prefix"])
560 elif name
== "value":
561 self
.values
.append(Value(attrs
))
563 def end_element(self
, name
):
564 if name
== "instruction":
565 self
.emit_instruction()
566 self
.instruction
= None
568 elif name
== "struct":
572 elif name
== "register":
577 elif name
== "group":
578 self
.group
= self
.group
.parent
579 elif name
== "field":
580 self
.group
.fields
[-1].values
= self
.values
584 elif name
== "genxml":
585 print('#endif /* %s */' % self
.gen_guard())
587 def emit_template_struct(self
, name
, group
):
588 print("struct %s {" % self
.gen_prefix(name
))
589 group
.emit_template_struct("")
592 def emit_pack_function(self
, name
, group
):
593 name
= self
.gen_prefix(name
)
594 print(textwrap
.dedent("""\
596 %s_pack(__attribute__((unused)) __gen_user_data *data,
597 %s__attribute__((unused)) void * restrict dst,
598 %s__attribute__((unused)) const struct %s * restrict values)
599 {""") % (name
, ' ' * len(name
), ' ' * len(name
), name
))
601 (dwords
, length
) = group
.collect_dwords_and_length()
603 # Cast dst to make header C++ friendly
604 print(" uint32_t * restrict dw = (uint32_t * restrict) dst;")
606 group
.emit_pack_function(dwords
, length
)
610 def emit_instruction(self
):
611 name
= self
.instruction
612 if not self
.length
== None:
613 print('#define %-33s %6d' %
614 (self
.gen_prefix(name
+ "_length"), self
.length
))
615 print('#define %-33s %6d' %
616 (self
.gen_prefix(name
+ "_length_bias"), self
.length_bias
))
619 for field
in self
.group
.fields
:
620 if not type(field
) is Field
:
622 if field
.default
== None:
624 default_fields
.append(" .%-35s = %6d" % (field
.name
, field
.default
))
627 print('#define %-40s\\' % (self
.gen_prefix(name
+ '_header')))
628 print(", \\\n".join(default_fields
))
631 self
.emit_template_struct(self
.instruction
, self
.group
)
633 self
.emit_pack_function(self
.instruction
, self
.group
)
635 def emit_register(self
):
637 if not self
.reg_num
== None:
638 print('#define %-33s 0x%04x' %
639 (self
.gen_prefix(name
+ "_num"), self
.reg_num
))
641 if not self
.length
== None:
642 print('#define %-33s %6d' %
643 (self
.gen_prefix(name
+ "_length"), self
.length
))
645 self
.emit_template_struct(self
.register
, self
.group
)
646 self
.emit_pack_function(self
.register
, self
.group
)
648 def emit_struct(self
):
650 if not self
.length
== None:
651 print('#define %-33s %6d' %
652 (self
.gen_prefix(name
+ "_length"), self
.length
))
654 self
.emit_template_struct(self
.struct
, self
.group
)
655 self
.emit_pack_function(self
.struct
, self
.group
)
658 print('enum %s {' % self
.gen_prefix(self
.enum
))
659 for value
in self
.values
:
661 name
= self
.prefix
+ "_" + value
.name
664 print(' %-36s = %6d,' % (name
.upper(), value
.value
))
667 def parse(self
, filename
):
668 file = open(filename
, "rb")
669 self
.parser
.ParseFile(file)
672 if len(sys
.argv
) < 2:
673 print("No input xml file specified")
676 input_file
= sys
.argv
[1]