4 import xml
.parsers
.expat
10 * Copyright (C) 2016 Intel Corporation
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice (including the next
20 * paragraph) shall be included in all copies or substantial portions of the
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
33 pack_header
= """%(license)s
35 /* Instructions, enums and structures for %(platform)s.
37 * This file has been generated, do not hand edit.
48 #ifndef __gen_validate_value
49 #define __gen_validate_value(x)
52 #ifndef __gen_field_functions
53 #define __gen_field_functions
60 static inline uint64_t
61 __gen_mbo(uint32_t start, uint32_t end)
63 return (~0ull >> (64 - (end - start + 1))) << start;
66 static inline uint64_t
67 __gen_uint(uint64_t v, uint32_t start, uint32_t end)
69 __gen_validate_value(v);
72 const int width = end - start + 1;
74 const uint64_t max = (1ull << width) - 1;
82 static inline uint64_t
83 __gen_sint(int64_t v, uint32_t start, uint32_t end)
85 const int width = end - start + 1;
87 __gen_validate_value(v);
91 const int64_t max = (1ll << (width - 1)) - 1;
92 const int64_t min = -(1ll << (width - 1));
93 assert(min <= v && v <= max);
97 const uint64_t mask = ~0ull >> (64 - width);
99 return (v & mask) << start;
102 static inline uint64_t
103 __gen_offset(uint64_t v, uint32_t start, uint32_t end)
105 __gen_validate_value(v);
107 uint64_t mask = (~0ull >> (64 - (end - start + 1))) << start;
109 assert((v & ~mask) == 0);
115 static inline uint32_t
118 __gen_validate_value(v);
119 return ((union __gen_value) { .f = (v) }).dw;
122 static inline uint64_t
123 __gen_sfixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
125 __gen_validate_value(v);
127 const float factor = (1 << fract_bits);
130 const float max = ((1 << (end - start)) - 1) / factor;
131 const float min = -(1 << (end - start)) / factor;
132 assert(min <= v && v <= max);
135 const int64_t int_val = llroundf(v * factor);
136 const uint64_t mask = ~0ull >> (64 - (end - start + 1));
138 return (int_val & mask) << start;
141 static inline uint64_t
142 __gen_ufixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
144 __gen_validate_value(v);
146 const float factor = (1 << fract_bits);
149 const float max = ((1 << (end - start + 1)) - 1) / factor;
150 const float min = 0.0f;
151 assert(min <= v && v <= max);
154 const uint64_t uint_val = llroundf(v * factor);
156 return uint_val << start;
159 #ifndef __gen_address_type
160 #error #define __gen_address_type before including this file
163 #ifndef __gen_user_data
164 #error #define __gen_combine_address before including this file
171 def to_alphanum(name
):
194 for i
, j
in substitutions
.items():
195 name
= name
.replace(i
, j
)
200 name
= to_alphanum(name
)
201 if not str.isalpha(name
[0]):
206 def num_from_str(num_str
):
207 if num_str
.lower().startswith('0x'):
208 return int(num_str
, base
=16)
210 assert(not num_str
.startswith('0') and 'octals numbers not allowed')
214 ufixed_pattern
= re
.compile("u(\d+)\.(\d+)")
215 sfixed_pattern
= re
.compile("s(\d+)\.(\d+)")
217 def __init__(self
, parser
, attrs
):
220 self
.name
= safe_name(attrs
["name"])
221 self
.start
= int(attrs
["start"])
222 self
.end
= int(attrs
["end"])
223 self
.type = attrs
["type"]
225 if "prefix" in attrs
:
226 self
.prefix
= attrs
["prefix"]
230 if "default" in attrs
:
231 self
.default
= int(attrs
["default"])
235 ufixed_match
= Field
.ufixed_pattern
.match(self
.type)
238 self
.fractional_size
= int(ufixed_match
.group(2))
240 sfixed_match
= Field
.sfixed_pattern
.match(self
.type)
243 self
.fractional_size
= int(sfixed_match
.group(2))
245 def emit_template_struct(self
, dim
):
246 if self
.type == 'address':
247 type = '__gen_address_type'
248 elif self
.type == 'bool':
250 elif self
.type == 'float':
252 elif self
.type == 'ufixed':
254 elif self
.type == 'sfixed':
256 elif self
.type == 'uint' and self
.end
- self
.start
> 32:
258 elif self
.type == 'offset':
260 elif self
.type == 'int':
262 elif self
.type == 'uint':
264 elif self
.type in self
.parser
.structs
:
265 type = 'struct ' + self
.parser
.gen_prefix(safe_name(self
.type))
266 elif self
.type == 'mbo':
269 print("#error unhandled type: %s" % self
.type)
271 print(" %-36s %s%s;" % (type, self
.name
, dim
))
273 if len(self
.values
) > 0 and self
.default
== None:
275 prefix
= self
.prefix
+ "_"
279 for value
in self
.values
:
280 print("#define %-40s %d" % (prefix
+ value
.name
, value
.value
))
283 def __init__(self
, parser
, parent
, start
, count
, size
):
291 def emit_template_struct(self
, dim
):
293 print(" /* variable length fields follow */")
296 dim
= "%s[%d]" % (dim
, self
.count
)
298 for field
in self
.fields
:
299 field
.emit_template_struct(dim
)
307 def collect_dwords(self
, dwords
, start
, dim
):
308 for field
in self
.fields
:
309 if type(field
) is Group
:
311 field
.collect_dwords(dwords
, start
+ field
.start
, dim
)
313 for i
in range(field
.count
):
314 field
.collect_dwords(dwords
,
315 start
+ field
.start
+ i
* field
.size
,
319 index
= (start
+ field
.start
) // 32
320 if not index
in dwords
:
321 dwords
[index
] = self
.DWord()
323 clone
= copy
.copy(field
)
324 clone
.start
= clone
.start
+ start
325 clone
.end
= clone
.end
+ start
327 dwords
[index
].fields
.append(clone
)
329 if field
.type == "address":
330 # assert dwords[index].address == None
331 dwords
[index
].address
= field
333 # Coalesce all the dwords covered by this field. The two cases we
334 # handle are where multiple fields are in a 64 bit word (typically
335 # and address and a few bits) or where a single struct field
336 # completely covers multiple dwords.
337 while index
< (start
+ field
.end
) // 32:
338 if index
+ 1 in dwords
and not dwords
[index
] == dwords
[index
+ 1]:
339 dwords
[index
].fields
.extend(dwords
[index
+ 1].fields
)
340 dwords
[index
].size
= 64
341 dwords
[index
+ 1] = dwords
[index
]
344 def emit_pack_function(self
, start
):
346 self
.collect_dwords(dwords
, 0, "")
348 # Determine number of dwords in this group. If we have a size, use
349 # that, since that'll account for MBZ dwords at the end of a group
350 # (like dword 8 on BDW+ 3DSTATE_HS). Otherwise, use the largest dword
351 # index we've seen plus one.
353 length
= self
.size
// 32
355 length
= max(dwords
.keys()) + 1
357 for index
in range(length
):
359 if not index
in dwords
:
361 print(" dw[%d] = 0;" % index
)
364 # For 64 bit dwords, we aliased the two dword entries in the dword
365 # dict it occupies. Now that we're emitting the pack function,
366 # skip the duplicate entries.
368 if index
> 0 and index
- 1 in dwords
and dw
== dwords
[index
- 1]:
371 # Special case: only one field and it's a struct at the beginning
372 # of the dword. In this case we pack directly into the
373 # destination. This is the only way we handle embedded structs
374 # larger than 32 bits.
375 if len(dw
.fields
) == 1:
377 name
= field
.name
+ field
.dim
378 if field
.type in self
.parser
.structs
and field
.start
% 32 == 0:
380 print(" %s_pack(data, &dw[%d], &values->%s);" %
381 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, name
))
384 # Pack any fields of struct type first so we have integer values
385 # to the dword for those fields.
387 for field
in dw
.fields
:
388 if type(field
) is Field
and field
.type in self
.parser
.structs
:
389 name
= field
.name
+ field
.dim
391 print(" uint32_t v%d_%d;" % (index
, field_index
))
392 print(" %s_pack(data, &v%d_%d, &values->%s);" %
393 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, field_index
, name
))
394 field_index
= field_index
+ 1
397 dword_start
= index
* 32
398 if dw
.address
== None:
403 if dw
.size
== 32 and dw
.address
== None:
405 print(" dw[%d] =" % index
)
406 elif len(dw
.fields
) > address_count
:
408 print(" const uint%d_t %s =" % (dw
.size
, v
))
413 for field
in dw
.fields
:
414 if field
.type != "mbo":
415 name
= field
.name
+ field
.dim
417 if field
.type == "mbo":
418 s
= "__gen_mbo(%d, %d)" % \
419 (field
.start
- dword_start
, field
.end
- dword_start
)
420 elif field
.type == "address":
422 elif field
.type == "uint":
423 s
= "__gen_uint(values->%s, %d, %d)" % \
424 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
425 elif field
.type == "int":
426 s
= "__gen_sint(values->%s, %d, %d)" % \
427 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
428 elif field
.type == "bool":
429 s
= "__gen_uint(values->%s, %d, %d)" % \
430 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
431 elif field
.type == "float":
432 s
= "__gen_float(values->%s)" % name
433 elif field
.type == "offset":
434 s
= "__gen_offset(values->%s, %d, %d)" % \
435 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
436 elif field
.type == 'ufixed':
437 s
= "__gen_ufixed(values->%s, %d, %d, %d)" % \
438 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
)
439 elif field
.type == 'sfixed':
440 s
= "__gen_sfixed(values->%s, %d, %d, %d)" % \
441 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
)
442 elif field
.type in self
.parser
.structs
:
443 s
= "__gen_uint(v%d_%d, %d, %d)" % \
444 (index
, field_index
, field
.start
- dword_start
, field
.end
- dword_start
)
445 field_index
= field_index
+ 1
447 print("/* unhandled field %s, type %s */\n" % (name
, field
.type))
451 if field
== dw
.fields
[-1]:
458 print(" dw[%d] = __gen_combine_address(data, &dw[%d], values->%s, %s);" % (index
, index
, dw
.address
.name
, v
))
462 v_address
= "v%d_address" % index
463 print(" const uint64_t %s =\n __gen_combine_address(data, &dw[%d], values->%s, %s);" %
464 (v_address
, index
, dw
.address
.name
, v
))
467 print(" dw[%d] = %s;" % (index
, v
))
468 print(" dw[%d] = %s >> 32;" % (index
+ 1, v
))
471 def __init__(self
, attrs
):
472 self
.name
= safe_name(attrs
["name"])
473 self
.value
= int(attrs
["value"])
475 class Parser(object):
477 self
.parser
= xml
.parsers
.expat
.ParserCreate()
478 self
.parser
.StartElementHandler
= self
.start_element
479 self
.parser
.EndElementHandler
= self
.end_element
481 self
.instruction
= None
485 def start_element(self
, name
, attrs
):
487 self
.platform
= attrs
["name"]
488 self
.gen
= attrs
["gen"].replace('.', '')
489 print(pack_header
% {'license': license
, 'platform': self
.platform
})
490 elif name
in ("instruction", "struct", "register"):
491 if name
== "instruction":
492 self
.instruction
= safe_name(attrs
["name"])
493 self
.length_bias
= int(attrs
["bias"])
494 elif name
== "struct":
495 self
.struct
= safe_name(attrs
["name"])
496 self
.structs
[attrs
["name"]] = 1
497 elif name
== "register":
498 self
.register
= safe_name(attrs
["name"])
499 self
.reg_num
= num_from_str(attrs
["num"])
500 self
.registers
[attrs
["name"]] = 1
501 if "length" in attrs
:
502 self
.length
= int(attrs
["length"])
503 size
= self
.length
* 32
507 self
.group
= Group(self
, None, 0, 1, size
)
509 elif name
== "group":
510 group
= Group(self
, self
.group
,
511 int(attrs
["start"]), int(attrs
["count"]), int(attrs
["size"]))
512 self
.group
.fields
.append(group
)
514 elif name
== "field":
515 self
.group
.fields
.append(Field(self
, attrs
))
519 self
.enum
= safe_name(attrs
["name"])
520 if "prefix" in attrs
:
521 self
.prefix
= safe_name(attrs
["prefix"])
524 elif name
== "value":
525 self
.values
.append(Value(attrs
))
527 def end_element(self
, name
):
528 if name
== "instruction":
529 self
.emit_instruction()
530 self
.instruction
= None
532 elif name
== "struct":
536 elif name
== "register":
541 elif name
== "group":
542 self
.group
= self
.group
.parent
543 elif name
== "field":
544 self
.group
.fields
[-1].values
= self
.values
549 def gen_prefix(self
, name
):
551 return 'GEN%s%s' % (self
.gen
, name
)
553 return 'GEN%s_%s' % (self
.gen
, name
)
555 def emit_template_struct(self
, name
, group
):
556 print("struct %s {" % self
.gen_prefix(name
))
557 group
.emit_template_struct("")
560 def emit_pack_function(self
, name
, group
):
561 name
= self
.gen_prefix(name
)
562 print("static inline void\n%s_pack(__gen_user_data *data, void * restrict dst,\n%sconst struct %s * restrict values)\n{" %
563 (name
, ' ' * (len(name
) + 6), name
))
565 # Cast dst to make header C++ friendly
566 print(" uint32_t * restrict dw = (uint32_t * restrict) dst;")
568 group
.emit_pack_function(0)
572 def emit_instruction(self
):
573 name
= self
.instruction
574 if not self
.length
== None:
575 print('#define %-33s %6d' %
576 (self
.gen_prefix(name
+ "_length"), self
.length
))
577 print('#define %-33s %6d' %
578 (self
.gen_prefix(name
+ "_length_bias"), self
.length_bias
))
581 for field
in self
.group
.fields
:
582 if not type(field
) is Field
:
584 if field
.default
== None:
586 default_fields
.append(" .%-35s = %6d" % (field
.name
, field
.default
))
589 print('#define %-40s\\' % (self
.gen_prefix(name
+ '_header')))
590 print(", \\\n".join(default_fields
))
593 self
.emit_template_struct(self
.instruction
, self
.group
)
595 self
.emit_pack_function(self
.instruction
, self
.group
)
597 def emit_register(self
):
599 if not self
.reg_num
== None:
600 print('#define %-33s 0x%04x' %
601 (self
.gen_prefix(name
+ "_num"), self
.reg_num
))
603 if not self
.length
== None:
604 print('#define %-33s %6d' %
605 (self
.gen_prefix(name
+ "_length"), self
.length
))
607 self
.emit_template_struct(self
.register
, self
.group
)
608 self
.emit_pack_function(self
.register
, self
.group
)
610 def emit_struct(self
):
612 if not self
.length
== None:
613 print('#define %-33s %6d' %
614 (self
.gen_prefix(name
+ "_length"), self
.length
))
616 self
.emit_template_struct(self
.struct
, self
.group
)
617 self
.emit_pack_function(self
.struct
, self
.group
)
620 print('/* enum %s */' % self
.gen_prefix(self
.enum
))
621 for value
in self
.values
:
623 name
= self
.prefix
+ "_" + value
.name
626 print('#define %-36s %6d' % (name
.upper(), value
.value
))
629 def parse(self
, filename
):
630 file = open(filename
, "rb")
631 self
.parser
.ParseFile(file)
634 if len(sys
.argv
) < 2:
635 print("No input xml file specified")
638 input_file
= sys
.argv
[1]