3 from __future__
import (
4 absolute_import
, division
, print_function
, unicode_literals
6 import xml
.parsers
.expat
12 * Copyright (C) 2016 Intel Corporation
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
35 pack_header
= """%(license)s
37 /* Instructions, enums and structures for %(platform)s.
39 * This file has been generated, do not hand edit.
51 #ifndef __gen_validate_value
52 #define __gen_validate_value(x)
55 #ifndef __gen_field_functions
56 #define __gen_field_functions
63 static inline uint64_t
64 __gen_mbo(uint32_t start, uint32_t end)
66 return (~0ull >> (64 - (end - start + 1))) << start;
69 static inline uint64_t
70 __gen_uint(uint64_t v, uint32_t start, uint32_t end)
72 __gen_validate_value(v);
75 const int width = end - start + 1;
77 const uint64_t max = (1ull << width) - 1;
85 static inline uint64_t
86 __gen_sint(int64_t v, uint32_t start, uint32_t end)
88 const int width = end - start + 1;
90 __gen_validate_value(v);
94 const int64_t max = (1ll << (width - 1)) - 1;
95 const int64_t min = -(1ll << (width - 1));
96 assert(min <= v && v <= max);
100 const uint64_t mask = ~0ull >> (64 - width);
102 return (v & mask) << start;
105 static inline uint64_t
106 __gen_offset(uint64_t v, uint32_t start, uint32_t end)
108 __gen_validate_value(v);
110 uint64_t mask = (~0ull >> (64 - (end - start + 1))) << start;
112 assert((v & ~mask) == 0);
118 static inline uint32_t
121 __gen_validate_value(v);
122 return ((union __gen_value) { .f = (v) }).dw;
125 static inline uint64_t
126 __gen_sfixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
128 __gen_validate_value(v);
130 const float factor = (1 << fract_bits);
133 const float max = ((1 << (end - start)) - 1) / factor;
134 const float min = -(1 << (end - start)) / factor;
135 assert(min <= v && v <= max);
138 const int64_t int_val = llroundf(v * factor);
139 const uint64_t mask = ~0ull >> (64 - (end - start + 1));
141 return (int_val & mask) << start;
144 static inline uint64_t
145 __gen_ufixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
147 __gen_validate_value(v);
149 const float factor = (1 << fract_bits);
152 const float max = ((1 << (end - start + 1)) - 1) / factor;
153 const float min = 0.0f;
154 assert(min <= v && v <= max);
157 const uint64_t uint_val = llroundf(v * factor);
159 return uint_val << start;
162 #ifndef __gen_address_type
163 #error #define __gen_address_type before including this file
166 #ifndef __gen_user_data
167 #error #define __gen_combine_address before including this file
174 def to_alphanum(name
):
197 for i
, j
in substitutions
.items():
198 name
= name
.replace(i
, j
)
203 name
= to_alphanum(name
)
204 if not name
[0].isalpha():
209 def num_from_str(num_str
):
210 if num_str
.lower().startswith('0x'):
211 return int(num_str
, base
=16)
213 assert(not num_str
.startswith('0') and 'octals numbers not allowed')
217 ufixed_pattern
= re
.compile(r
"u(\d+)\.(\d+)")
218 sfixed_pattern
= re
.compile(r
"s(\d+)\.(\d+)")
220 def __init__(self
, parser
, attrs
):
223 self
.name
= safe_name(attrs
["name"])
224 self
.start
= int(attrs
["start"])
225 self
.end
= int(attrs
["end"])
226 self
.type = attrs
["type"]
228 if "prefix" in attrs
:
229 self
.prefix
= attrs
["prefix"]
233 if "default" in attrs
:
234 self
.default
= int(attrs
["default"])
238 ufixed_match
= Field
.ufixed_pattern
.match(self
.type)
241 self
.fractional_size
= int(ufixed_match
.group(2))
243 sfixed_match
= Field
.sfixed_pattern
.match(self
.type)
246 self
.fractional_size
= int(sfixed_match
.group(2))
248 def emit_template_struct(self
, dim
):
249 if self
.type == 'address':
250 type = '__gen_address_type'
251 elif self
.type == 'bool':
253 elif self
.type == 'float':
255 elif self
.type == 'ufixed':
257 elif self
.type == 'sfixed':
259 elif self
.type == 'uint' and self
.end
- self
.start
> 32:
261 elif self
.type == 'offset':
263 elif self
.type == 'int':
265 elif self
.type == 'uint':
267 elif self
.type in self
.parser
.structs
:
268 type = 'struct ' + self
.parser
.gen_prefix(safe_name(self
.type))
269 elif self
.type in self
.parser
.enums
:
270 type = 'enum ' + self
.parser
.gen_prefix(safe_name(self
.type))
271 elif self
.type == 'mbo':
274 print("#error unhandled type: %s" % self
.type)
276 print(" %-36s %s%s;" % (type, self
.name
, dim
))
278 if len(self
.values
) > 0 and self
.default
== None:
280 prefix
= self
.prefix
+ "_"
284 for value
in self
.values
:
285 print("#define %-40s %d" % (prefix
+ value
.name
, value
.value
))
288 def __init__(self
, parser
, parent
, start
, count
, size
):
296 def emit_template_struct(self
, dim
):
298 print(" /* variable length fields follow */")
301 dim
= "%s[%d]" % (dim
, self
.count
)
303 for field
in self
.fields
:
304 field
.emit_template_struct(dim
)
312 def collect_dwords(self
, dwords
, start
, dim
):
313 for field
in self
.fields
:
314 if type(field
) is Group
:
316 field
.collect_dwords(dwords
, start
+ field
.start
, dim
)
318 for i
in range(field
.count
):
319 field
.collect_dwords(dwords
,
320 start
+ field
.start
+ i
* field
.size
,
324 index
= (start
+ field
.start
) // 32
325 if not index
in dwords
:
326 dwords
[index
] = self
.DWord()
328 clone
= copy
.copy(field
)
329 clone
.start
= clone
.start
+ start
330 clone
.end
= clone
.end
+ start
332 dwords
[index
].fields
.append(clone
)
334 if field
.type == "address":
335 # assert dwords[index].address == None
336 dwords
[index
].address
= field
338 # Coalesce all the dwords covered by this field. The two cases we
339 # handle are where multiple fields are in a 64 bit word (typically
340 # and address and a few bits) or where a single struct field
341 # completely covers multiple dwords.
342 while index
< (start
+ field
.end
) // 32:
343 if index
+ 1 in dwords
and not dwords
[index
] == dwords
[index
+ 1]:
344 dwords
[index
].fields
.extend(dwords
[index
+ 1].fields
)
345 dwords
[index
].size
= 64
346 dwords
[index
+ 1] = dwords
[index
]
349 def emit_pack_function(self
, start
):
351 self
.collect_dwords(dwords
, 0, "")
353 # Determine number of dwords in this group. If we have a size, use
354 # that, since that'll account for MBZ dwords at the end of a group
355 # (like dword 8 on BDW+ 3DSTATE_HS). Otherwise, use the largest dword
356 # index we've seen plus one.
358 length
= self
.size
// 32
360 length
= max(dwords
.keys()) + 1
362 for index
in range(length
):
364 if not index
in dwords
:
366 print(" dw[%d] = 0;" % index
)
369 # For 64 bit dwords, we aliased the two dword entries in the dword
370 # dict it occupies. Now that we're emitting the pack function,
371 # skip the duplicate entries.
373 if index
> 0 and index
- 1 in dwords
and dw
== dwords
[index
- 1]:
376 # Special case: only one field and it's a struct at the beginning
377 # of the dword. In this case we pack directly into the
378 # destination. This is the only way we handle embedded structs
379 # larger than 32 bits.
380 if len(dw
.fields
) == 1:
382 name
= field
.name
+ field
.dim
383 if field
.type in self
.parser
.structs
and field
.start
% 32 == 0:
385 print(" %s_pack(data, &dw[%d], &values->%s);" %
386 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, name
))
389 # Pack any fields of struct type first so we have integer values
390 # to the dword for those fields.
392 for field
in dw
.fields
:
393 if type(field
) is Field
and field
.type in self
.parser
.structs
:
394 name
= field
.name
+ field
.dim
396 print(" uint32_t v%d_%d;" % (index
, field_index
))
397 print(" %s_pack(data, &v%d_%d, &values->%s);" %
398 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, field_index
, name
))
399 field_index
= field_index
+ 1
402 dword_start
= index
* 32
403 if dw
.address
== None:
408 if dw
.size
== 32 and dw
.address
== None:
410 print(" dw[%d] =" % index
)
411 elif len(dw
.fields
) > address_count
:
413 print(" const uint%d_t %s =" % (dw
.size
, v
))
418 for field
in dw
.fields
:
419 if field
.type != "mbo":
420 name
= field
.name
+ field
.dim
422 if field
.type == "mbo":
423 s
= "__gen_mbo(%d, %d)" % \
424 (field
.start
- dword_start
, field
.end
- dword_start
)
425 elif field
.type == "address":
427 elif field
.type == "uint":
428 s
= "__gen_uint(values->%s, %d, %d)" % \
429 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
430 elif field
.type in self
.parser
.enums
:
431 s
= "__gen_uint(values->%s, %d, %d)" % \
432 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
433 elif field
.type == "int":
434 s
= "__gen_sint(values->%s, %d, %d)" % \
435 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
436 elif field
.type == "bool":
437 s
= "__gen_uint(values->%s, %d, %d)" % \
438 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
439 elif field
.type == "float":
440 s
= "__gen_float(values->%s)" % name
441 elif field
.type == "offset":
442 s
= "__gen_offset(values->%s, %d, %d)" % \
443 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
444 elif field
.type == 'ufixed':
445 s
= "__gen_ufixed(values->%s, %d, %d, %d)" % \
446 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
)
447 elif field
.type == 'sfixed':
448 s
= "__gen_sfixed(values->%s, %d, %d, %d)" % \
449 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
)
450 elif field
.type in self
.parser
.structs
:
451 s
= "__gen_uint(v%d_%d, %d, %d)" % \
452 (index
, field_index
, field
.start
- dword_start
, field
.end
- dword_start
)
453 field_index
= field_index
+ 1
455 print("/* unhandled field %s, type %s */\n" % (name
, field
.type))
459 if field
== dw
.fields
[-1]:
466 print(" dw[%d] = __gen_combine_address(data, &dw[%d], values->%s, %s);" % (index
, index
, dw
.address
.name
, v
))
470 v_address
= "v%d_address" % index
471 print(" const uint64_t %s =\n __gen_combine_address(data, &dw[%d], values->%s, %s);" %
472 (v_address
, index
, dw
.address
.name
, v
))
475 print(" dw[%d] = %s;" % (index
, v
))
476 print(" dw[%d] = %s >> 32;" % (index
+ 1, v
))
479 def __init__(self
, attrs
):
480 self
.name
= safe_name(attrs
["name"])
481 self
.value
= int(attrs
["value"])
483 class Parser(object):
485 self
.parser
= xml
.parsers
.expat
.ParserCreate()
486 self
.parser
.StartElementHandler
= self
.start_element
487 self
.parser
.EndElementHandler
= self
.end_element
489 self
.instruction
= None
494 def gen_prefix(self
, name
):
496 return 'GEN%s%s' % (self
.gen
, name
)
498 return 'GEN%s_%s' % (self
.gen
, name
)
501 return self
.gen_prefix("PACK_H")
503 def start_element(self
, name
, attrs
):
505 self
.platform
= attrs
["name"]
506 self
.gen
= attrs
["gen"].replace('.', '')
507 print(pack_header
% {'license': license
, 'platform': self
.platform
, 'guard': self
.gen_guard()})
508 elif name
in ("instruction", "struct", "register"):
509 if name
== "instruction":
510 self
.instruction
= safe_name(attrs
["name"])
511 self
.length_bias
= int(attrs
["bias"])
512 elif name
== "struct":
513 self
.struct
= safe_name(attrs
["name"])
514 self
.structs
[attrs
["name"]] = 1
515 elif name
== "register":
516 self
.register
= safe_name(attrs
["name"])
517 self
.reg_num
= num_from_str(attrs
["num"])
518 self
.registers
[attrs
["name"]] = 1
519 if "length" in attrs
:
520 self
.length
= int(attrs
["length"])
521 size
= self
.length
* 32
525 self
.group
= Group(self
, None, 0, 1, size
)
527 elif name
== "group":
528 group
= Group(self
, self
.group
,
529 int(attrs
["start"]), int(attrs
["count"]), int(attrs
["size"]))
530 self
.group
.fields
.append(group
)
532 elif name
== "field":
533 self
.group
.fields
.append(Field(self
, attrs
))
537 self
.enum
= safe_name(attrs
["name"])
538 self
.enums
[attrs
["name"]] = 1
539 if "prefix" in attrs
:
540 self
.prefix
= safe_name(attrs
["prefix"])
543 elif name
== "value":
544 self
.values
.append(Value(attrs
))
546 def end_element(self
, name
):
547 if name
== "instruction":
548 self
.emit_instruction()
549 self
.instruction
= None
551 elif name
== "struct":
555 elif name
== "register":
560 elif name
== "group":
561 self
.group
= self
.group
.parent
562 elif name
== "field":
563 self
.group
.fields
[-1].values
= self
.values
567 elif name
== "genxml":
568 print('#endif /* %s */' % self
.gen_guard())
570 def emit_template_struct(self
, name
, group
):
571 print("struct %s {" % self
.gen_prefix(name
))
572 group
.emit_template_struct("")
575 def emit_pack_function(self
, name
, group
):
576 name
= self
.gen_prefix(name
)
577 print("static inline void\n%s_pack(__gen_user_data *data, void * restrict dst,\n%sconst struct %s * restrict values)\n{" %
578 (name
, ' ' * (len(name
) + 6), name
))
580 # Cast dst to make header C++ friendly
581 print(" uint32_t * restrict dw = (uint32_t * restrict) dst;")
583 group
.emit_pack_function(0)
587 def emit_instruction(self
):
588 name
= self
.instruction
589 if not self
.length
== None:
590 print('#define %-33s %6d' %
591 (self
.gen_prefix(name
+ "_length"), self
.length
))
592 print('#define %-33s %6d' %
593 (self
.gen_prefix(name
+ "_length_bias"), self
.length_bias
))
596 for field
in self
.group
.fields
:
597 if not type(field
) is Field
:
599 if field
.default
== None:
601 default_fields
.append(" .%-35s = %6d" % (field
.name
, field
.default
))
604 print('#define %-40s\\' % (self
.gen_prefix(name
+ '_header')))
605 print(", \\\n".join(default_fields
))
608 self
.emit_template_struct(self
.instruction
, self
.group
)
610 self
.emit_pack_function(self
.instruction
, self
.group
)
612 def emit_register(self
):
614 if not self
.reg_num
== None:
615 print('#define %-33s 0x%04x' %
616 (self
.gen_prefix(name
+ "_num"), self
.reg_num
))
618 if not self
.length
== None:
619 print('#define %-33s %6d' %
620 (self
.gen_prefix(name
+ "_length"), self
.length
))
622 self
.emit_template_struct(self
.register
, self
.group
)
623 self
.emit_pack_function(self
.register
, self
.group
)
625 def emit_struct(self
):
627 if not self
.length
== None:
628 print('#define %-33s %6d' %
629 (self
.gen_prefix(name
+ "_length"), self
.length
))
631 self
.emit_template_struct(self
.struct
, self
.group
)
632 self
.emit_pack_function(self
.struct
, self
.group
)
635 print('enum %s {' % self
.gen_prefix(self
.enum
))
636 for value
in self
.values
:
638 name
= self
.prefix
+ "_" + value
.name
641 print(' %-36s = %6d,' % (name
.upper(), value
.value
))
644 def parse(self
, filename
):
645 file = open(filename
, "rb")
646 self
.parser
.ParseFile(file)
649 if len(sys
.argv
) < 2:
650 print("No input xml file specified")
653 input_file
= sys
.argv
[1]