3 from __future__
import (
4 absolute_import
, division
, print_function
, unicode_literals
7 import xml
.parsers
.expat
13 * Copyright (C) 2016 Intel Corporation
15 * Permission is hereby granted, free of charge, to any person obtaining a
16 * copy of this software and associated documentation files (the "Software"),
17 * to deal in the Software without restriction, including without limitation
18 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 * and/or sell copies of the Software, and to permit persons to whom the
20 * Software is furnished to do so, subject to the following conditions:
22 * The above copyright notice and this permission notice (including the next
23 * paragraph) shall be included in all copies or substantial portions of the
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
29 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
36 pack_header
= """%(license)s
38 /* Instructions, enums and structures for %(platform)s.
40 * This file has been generated, do not hand edit.
52 #ifndef __gen_validate_value
53 #define __gen_validate_value(x)
56 #ifndef __gen_field_functions
57 #define __gen_field_functions
64 static inline uint64_t
65 __gen_mbo(uint32_t start, uint32_t end)
67 return (~0ull >> (64 - (end - start + 1))) << start;
70 static inline uint64_t
71 __gen_uint(uint64_t v, uint32_t start, uint32_t end)
73 __gen_validate_value(v);
76 const int width = end - start + 1;
78 const uint64_t max = (1ull << width) - 1;
86 static inline uint64_t
87 __gen_sint(int64_t v, uint32_t start, uint32_t end)
89 const int width = end - start + 1;
91 __gen_validate_value(v);
95 const int64_t max = (1ll << (width - 1)) - 1;
96 const int64_t min = -(1ll << (width - 1));
97 assert(min <= v && v <= max);
101 const uint64_t mask = ~0ull >> (64 - width);
103 return (v & mask) << start;
106 static inline uint64_t
107 __gen_offset(uint64_t v, uint32_t start, uint32_t end)
109 __gen_validate_value(v);
111 uint64_t mask = (~0ull >> (64 - (end - start + 1))) << start;
113 assert((v & ~mask) == 0);
119 static inline uint32_t
122 __gen_validate_value(v);
123 return ((union __gen_value) { .f = (v) }).dw;
126 static inline uint64_t
127 __gen_sfixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
129 __gen_validate_value(v);
131 const float factor = (1 << fract_bits);
134 const float max = ((1 << (end - start)) - 1) / factor;
135 const float min = -(1 << (end - start)) / factor;
136 assert(min <= v && v <= max);
139 const int64_t int_val = llroundf(v * factor);
140 const uint64_t mask = ~0ull >> (64 - (end - start + 1));
142 return (int_val & mask) << start;
145 static inline uint64_t
146 __gen_ufixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
148 __gen_validate_value(v);
150 const float factor = (1 << fract_bits);
153 const float max = ((1 << (end - start + 1)) - 1) / factor;
154 const float min = 0.0f;
155 assert(min <= v && v <= max);
158 const uint64_t uint_val = llroundf(v * factor);
160 return uint_val << start;
163 #ifndef __gen_address_type
164 #error #define __gen_address_type before including this file
167 #ifndef __gen_user_data
168 #error #define __gen_combine_address before including this file
175 def to_alphanum(name
):
198 for i
, j
in substitutions
.items():
199 name
= name
.replace(i
, j
)
204 name
= to_alphanum(name
)
205 if not name
[0].isalpha():
210 def num_from_str(num_str
):
211 if num_str
.lower().startswith('0x'):
212 return int(num_str
, base
=16)
214 assert(not num_str
.startswith('0') and 'octals numbers not allowed')
218 ufixed_pattern
= re
.compile(r
"u(\d+)\.(\d+)")
219 sfixed_pattern
= re
.compile(r
"s(\d+)\.(\d+)")
221 def __init__(self
, parser
, attrs
):
224 self
.name
= safe_name(attrs
["name"])
225 self
.start
= int(attrs
["start"])
226 self
.end
= int(attrs
["end"])
227 self
.type = attrs
["type"]
229 if "prefix" in attrs
:
230 self
.prefix
= attrs
["prefix"]
234 if "default" in attrs
:
235 self
.default
= int(attrs
["default"])
239 ufixed_match
= Field
.ufixed_pattern
.match(self
.type)
242 self
.fractional_size
= int(ufixed_match
.group(2))
244 sfixed_match
= Field
.sfixed_pattern
.match(self
.type)
247 self
.fractional_size
= int(sfixed_match
.group(2))
249 def emit_template_struct(self
, dim
):
250 if self
.type == 'address':
251 type = '__gen_address_type'
252 elif self
.type == 'bool':
254 elif self
.type == 'float':
256 elif self
.type == 'ufixed':
258 elif self
.type == 'sfixed':
260 elif self
.type == 'uint' and self
.end
- self
.start
> 32:
262 elif self
.type == 'offset':
264 elif self
.type == 'int':
266 elif self
.type == 'uint':
268 elif self
.type in self
.parser
.structs
:
269 type = 'struct ' + self
.parser
.gen_prefix(safe_name(self
.type))
270 elif self
.type in self
.parser
.enums
:
271 type = 'enum ' + self
.parser
.gen_prefix(safe_name(self
.type))
272 elif self
.type == 'mbo':
275 print("#error unhandled type: %s" % self
.type)
277 print(" %-36s %s%s;" % (type, self
.name
, dim
))
279 if len(self
.values
) > 0 and self
.default
== None:
281 prefix
= self
.prefix
+ "_"
285 for value
in self
.values
:
286 print("#define %-40s %d" % (prefix
+ value
.name
, value
.value
))
289 def __init__(self
, parser
, parent
, start
, count
, size
):
297 def emit_template_struct(self
, dim
):
299 print(" /* variable length fields follow */")
302 dim
= "%s[%d]" % (dim
, self
.count
)
304 for field
in self
.fields
:
305 field
.emit_template_struct(dim
)
313 def collect_dwords(self
, dwords
, start
, dim
):
314 for field
in self
.fields
:
315 if type(field
) is Group
:
317 field
.collect_dwords(dwords
, start
+ field
.start
, dim
)
319 for i
in range(field
.count
):
320 field
.collect_dwords(dwords
,
321 start
+ field
.start
+ i
* field
.size
,
325 index
= (start
+ field
.start
) // 32
326 if not index
in dwords
:
327 dwords
[index
] = self
.DWord()
329 clone
= copy
.copy(field
)
330 clone
.start
= clone
.start
+ start
331 clone
.end
= clone
.end
+ start
333 dwords
[index
].fields
.append(clone
)
335 if field
.type == "address":
336 # assert dwords[index].address == None
337 dwords
[index
].address
= field
339 # Coalesce all the dwords covered by this field. The two cases we
340 # handle are where multiple fields are in a 64 bit word (typically
341 # and address and a few bits) or where a single struct field
342 # completely covers multiple dwords.
343 while index
< (start
+ field
.end
) // 32:
344 if index
+ 1 in dwords
and not dwords
[index
] == dwords
[index
+ 1]:
345 dwords
[index
].fields
.extend(dwords
[index
+ 1].fields
)
346 dwords
[index
].size
= 64
347 dwords
[index
+ 1] = dwords
[index
]
350 def emit_pack_function(self
, start
):
352 self
.collect_dwords(dwords
, 0, "")
354 # Determine number of dwords in this group. If we have a size, use
355 # that, since that'll account for MBZ dwords at the end of a group
356 # (like dword 8 on BDW+ 3DSTATE_HS). Otherwise, use the largest dword
357 # index we've seen plus one.
359 length
= self
.size
// 32
361 length
= max(dwords
.keys()) + 1
363 for index
in range(length
):
365 if not index
in dwords
:
367 print(" dw[%d] = 0;" % index
)
370 # For 64 bit dwords, we aliased the two dword entries in the dword
371 # dict it occupies. Now that we're emitting the pack function,
372 # skip the duplicate entries.
374 if index
> 0 and index
- 1 in dwords
and dw
== dwords
[index
- 1]:
377 # Special case: only one field and it's a struct at the beginning
378 # of the dword. In this case we pack directly into the
379 # destination. This is the only way we handle embedded structs
380 # larger than 32 bits.
381 if len(dw
.fields
) == 1:
383 name
= field
.name
+ field
.dim
384 if field
.type in self
.parser
.structs
and field
.start
% 32 == 0:
386 print(" %s_pack(data, &dw[%d], &values->%s);" %
387 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, name
))
390 # Pack any fields of struct type first so we have integer values
391 # to the dword for those fields.
393 for field
in dw
.fields
:
394 if type(field
) is Field
and field
.type in self
.parser
.structs
:
395 name
= field
.name
+ field
.dim
397 print(" uint32_t v%d_%d;" % (index
, field_index
))
398 print(" %s_pack(data, &v%d_%d, &values->%s);" %
399 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, field_index
, name
))
400 field_index
= field_index
+ 1
403 dword_start
= index
* 32
404 if dw
.address
== None:
409 if dw
.size
== 32 and dw
.address
== None:
411 print(" dw[%d] =" % index
)
412 elif len(dw
.fields
) > address_count
:
414 print(" const uint%d_t %s =" % (dw
.size
, v
))
419 non_address_fields
= []
420 for field
in dw
.fields
:
421 if field
.type != "mbo":
422 name
= field
.name
+ field
.dim
424 if field
.type == "mbo":
425 non_address_fields
.append("__gen_mbo(%d, %d)" % \
426 (field
.start
- dword_start
, field
.end
- dword_start
))
427 elif field
.type == "address":
429 elif field
.type == "uint":
430 non_address_fields
.append("__gen_uint(values->%s, %d, %d)" % \
431 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
432 elif field
.type in self
.parser
.enums
:
433 non_address_fields
.append("__gen_uint(values->%s, %d, %d)" % \
434 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
435 elif field
.type == "int":
436 non_address_fields
.append("__gen_sint(values->%s, %d, %d)" % \
437 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
438 elif field
.type == "bool":
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 == "float":
442 non_address_fields
.append("__gen_float(values->%s)" % name
)
443 elif field
.type == "offset":
444 non_address_fields
.append("__gen_offset(values->%s, %d, %d)" % \
445 (name
, field
.start
- dword_start
, field
.end
- dword_start
))
446 elif field
.type == 'ufixed':
447 non_address_fields
.append("__gen_ufixed(values->%s, %d, %d, %d)" % \
448 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
))
449 elif field
.type == 'sfixed':
450 non_address_fields
.append("__gen_sfixed(values->%s, %d, %d, %d)" % \
451 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
))
452 elif field
.type in self
.parser
.structs
:
453 non_address_fields
.append("__gen_uint(v%d_%d, %d, %d)" % \
454 (index
, field_index
, field
.start
- dword_start
, field
.end
- dword_start
))
455 field_index
= field_index
+ 1
457 non_address_fields
.append("/* unhandled field %s, type %s */\n" % \
460 if len(non_address_fields
) > 0:
461 print(" |\n".join(" " + f
for f
in non_address_fields
) + ";")
465 print(" dw[%d] = __gen_combine_address(data, &dw[%d], values->%s, %s);" % (index
, index
, dw
.address
.name
, v
))
469 v_address
= "v%d_address" % index
470 print(" const uint64_t %s =\n __gen_combine_address(data, &dw[%d], values->%s, %s);" %
471 (v_address
, index
, dw
.address
.name
, v
))
474 print(" dw[%d] = %s;" % (index
, v
))
475 print(" dw[%d] = %s >> 32;" % (index
+ 1, v
))
478 def __init__(self
, attrs
):
479 self
.name
= safe_name(attrs
["name"])
480 self
.value
= ast
.literal_eval(attrs
["value"])
482 class Parser(object):
484 self
.parser
= xml
.parsers
.expat
.ParserCreate()
485 self
.parser
.StartElementHandler
= self
.start_element
486 self
.parser
.EndElementHandler
= self
.end_element
488 self
.instruction
= None
493 def gen_prefix(self
, name
):
495 return 'GEN%s%s' % (self
.gen
, name
)
497 return 'GEN%s_%s' % (self
.gen
, name
)
500 return self
.gen_prefix("PACK_H")
502 def start_element(self
, name
, attrs
):
504 self
.platform
= attrs
["name"]
505 self
.gen
= attrs
["gen"].replace('.', '')
506 print(pack_header
% {'license': license
, 'platform': self
.platform
, 'guard': self
.gen_guard()})
507 elif name
in ("instruction", "struct", "register"):
508 if name
== "instruction":
509 self
.instruction
= safe_name(attrs
["name"])
510 self
.length_bias
= int(attrs
["bias"])
511 elif name
== "struct":
512 self
.struct
= safe_name(attrs
["name"])
513 self
.structs
[attrs
["name"]] = 1
514 elif name
== "register":
515 self
.register
= safe_name(attrs
["name"])
516 self
.reg_num
= num_from_str(attrs
["num"])
517 self
.registers
[attrs
["name"]] = 1
518 if "length" in attrs
:
519 self
.length
= int(attrs
["length"])
520 size
= self
.length
* 32
524 self
.group
= Group(self
, None, 0, 1, size
)
526 elif name
== "group":
527 group
= Group(self
, self
.group
,
528 int(attrs
["start"]), int(attrs
["count"]), int(attrs
["size"]))
529 self
.group
.fields
.append(group
)
531 elif name
== "field":
532 self
.group
.fields
.append(Field(self
, attrs
))
536 self
.enum
= safe_name(attrs
["name"])
537 self
.enums
[attrs
["name"]] = 1
538 if "prefix" in attrs
:
539 self
.prefix
= safe_name(attrs
["prefix"])
542 elif name
== "value":
543 self
.values
.append(Value(attrs
))
545 def end_element(self
, name
):
546 if name
== "instruction":
547 self
.emit_instruction()
548 self
.instruction
= None
550 elif name
== "struct":
554 elif name
== "register":
559 elif name
== "group":
560 self
.group
= self
.group
.parent
561 elif name
== "field":
562 self
.group
.fields
[-1].values
= self
.values
566 elif name
== "genxml":
567 print('#endif /* %s */' % self
.gen_guard())
569 def emit_template_struct(self
, name
, group
):
570 print("struct %s {" % self
.gen_prefix(name
))
571 group
.emit_template_struct("")
574 def emit_pack_function(self
, name
, group
):
575 name
= self
.gen_prefix(name
)
576 print("static inline void\n%s_pack(__gen_user_data *data, void * restrict dst,\n%sconst struct %s * restrict values)\n{" %
577 (name
, ' ' * (len(name
) + 6), name
))
579 # Cast dst to make header C++ friendly
580 print(" uint32_t * restrict dw = (uint32_t * restrict) dst;")
582 group
.emit_pack_function(0)
586 def emit_instruction(self
):
587 name
= self
.instruction
588 if not self
.length
== None:
589 print('#define %-33s %6d' %
590 (self
.gen_prefix(name
+ "_length"), self
.length
))
591 print('#define %-33s %6d' %
592 (self
.gen_prefix(name
+ "_length_bias"), self
.length_bias
))
595 for field
in self
.group
.fields
:
596 if not type(field
) is Field
:
598 if field
.default
== None:
600 default_fields
.append(" .%-35s = %6d" % (field
.name
, field
.default
))
603 print('#define %-40s\\' % (self
.gen_prefix(name
+ '_header')))
604 print(", \\\n".join(default_fields
))
607 self
.emit_template_struct(self
.instruction
, self
.group
)
609 self
.emit_pack_function(self
.instruction
, self
.group
)
611 def emit_register(self
):
613 if not self
.reg_num
== None:
614 print('#define %-33s 0x%04x' %
615 (self
.gen_prefix(name
+ "_num"), self
.reg_num
))
617 if not self
.length
== None:
618 print('#define %-33s %6d' %
619 (self
.gen_prefix(name
+ "_length"), self
.length
))
621 self
.emit_template_struct(self
.register
, self
.group
)
622 self
.emit_pack_function(self
.register
, self
.group
)
624 def emit_struct(self
):
626 if not self
.length
== None:
627 print('#define %-33s %6d' %
628 (self
.gen_prefix(name
+ "_length"), self
.length
))
630 self
.emit_template_struct(self
.struct
, self
.group
)
631 self
.emit_pack_function(self
.struct
, self
.group
)
634 print('enum %s {' % self
.gen_prefix(self
.enum
))
635 for value
in self
.values
:
637 name
= self
.prefix
+ "_" + value
.name
640 print(' %-36s = %6d,' % (name
.upper(), value
.value
))
643 def parse(self
, filename
):
644 file = open(filename
, "rb")
645 self
.parser
.ParseFile(file)
648 if len(sys
.argv
) < 2:
649 print("No input xml file specified")
652 input_file
= sys
.argv
[1]