3 import xml
.parsers
.expat
9 * Copyright (C) 2016 Intel Corporation
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice (including the next
19 * paragraph) shall be included in all copies or substantial portions of the
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 pack_header
= """%(license)s
34 /* Instructions, enums and structures for %(platform)s.
36 * This file has been generated, do not hand edit.
47 #ifndef __gen_validate_value
48 #define __gen_validate_value(x)
51 #ifndef __gen_field_functions
52 #define __gen_field_functions
59 static inline uint64_t
60 __gen_mbo(uint32_t start, uint32_t end)
62 return (~0ull >> (64 - (end - start + 1))) << start;
65 static inline uint64_t
66 __gen_uint(uint64_t v, uint32_t start, uint32_t end)
68 __gen_validate_value(v);
71 const int width = end - start + 1;
73 const uint64_t max = (1ull << width) - 1;
81 static inline uint64_t
82 __gen_sint(int64_t v, uint32_t start, uint32_t end)
84 const int width = end - start + 1;
86 __gen_validate_value(v);
90 const int64_t max = (1ll << (width - 1)) - 1;
91 const int64_t min = -(1ll << (width - 1));
92 assert(min <= v && v <= max);
96 const uint64_t mask = ~0ull >> (64 - width);
98 return (v & mask) << start;
101 static inline uint64_t
102 __gen_offset(uint64_t v, uint32_t start, uint32_t end)
104 __gen_validate_value(v);
106 uint64_t mask = (~0ull >> (64 - (end - start + 1))) << start;
108 assert((v & ~mask) == 0);
114 static inline uint32_t
117 __gen_validate_value(v);
118 return ((union __gen_value) { .f = (v) }).dw;
121 static inline uint64_t
122 __gen_sfixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
124 __gen_validate_value(v);
126 const float factor = (1 << fract_bits);
129 const float max = ((1 << (end - start)) - 1) / factor;
130 const float min = -(1 << (end - start)) / factor;
131 assert(min <= v && v <= max);
134 const int32_t int_val = roundf(v * factor);
135 const uint64_t mask = ~0ull >> (64 - (end - start + 1));
137 return (int_val & mask) << start;
140 static inline uint64_t
141 __gen_ufixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
143 __gen_validate_value(v);
145 const float factor = (1 << fract_bits);
148 const float max = ((1 << (end - start + 1)) - 1) / factor;
149 const float min = 0.0f;
150 assert(min <= v && v <= max);
153 const uint32_t uint_val = roundf(v * factor);
155 return uint_val << start;
158 #ifndef __gen_address_type
159 #error #define __gen_address_type before including this file
162 #ifndef __gen_user_data
163 #error #define __gen_combine_address before including this file
170 def to_alphanum(name
):
193 for i
, j
in substitutions
.items():
194 name
= name
.replace(i
, j
)
199 name
= to_alphanum(name
)
200 if not str.isalpha(name
[0]):
206 ufixed_pattern
= re
.compile("u(\d+)\.(\d+)")
207 sfixed_pattern
= re
.compile("s(\d+)\.(\d+)")
209 def __init__(self
, parser
, attrs
):
212 self
.name
= safe_name(attrs
["name"])
213 self
.start
= int(attrs
["start"])
214 self
.end
= int(attrs
["end"])
215 self
.type = attrs
["type"]
217 if "prefix" in attrs
:
218 self
.prefix
= attrs
["prefix"]
222 if "default" in attrs
:
223 self
.default
= int(attrs
["default"])
227 ufixed_match
= Field
.ufixed_pattern
.match(self
.type)
230 self
.fractional_size
= int(ufixed_match
.group(2))
232 sfixed_match
= Field
.sfixed_pattern
.match(self
.type)
235 self
.fractional_size
= int(sfixed_match
.group(2))
237 def emit_template_struct(self
, dim
):
238 if self
.type == 'address':
239 type = '__gen_address_type'
240 elif self
.type == 'bool':
242 elif self
.type == 'float':
244 elif self
.type == 'ufixed':
246 elif self
.type == 'sfixed':
248 elif self
.type == 'uint' and self
.end
- self
.start
> 32:
250 elif self
.type == 'offset':
252 elif self
.type == 'int':
254 elif self
.type == 'uint':
256 elif self
.type in self
.parser
.structs
:
257 type = 'struct ' + self
.parser
.gen_prefix(safe_name(self
.type))
258 elif self
.type == 'mbo':
261 print("#error unhandled type: %s" % self
.type)
263 print(" %-36s %s%s;" % (type, self
.name
, dim
))
265 if len(self
.values
) > 0 and self
.default
== None:
267 prefix
= self
.prefix
+ "_"
271 for value
in self
.values
:
272 print("#define %-40s %d" % (prefix
+ value
.name
, value
.value
))
275 def __init__(self
, parser
, parent
, start
, count
, size
):
283 def emit_template_struct(self
, dim
):
285 print(" /* variable length fields follow */")
288 dim
= "%s[%d]" % (dim
, self
.count
)
290 for field
in self
.fields
:
291 field
.emit_template_struct(dim
)
299 def collect_dwords(self
, dwords
, start
, dim
):
300 for field
in self
.fields
:
301 if type(field
) is Group
:
303 field
.collect_dwords(dwords
, start
+ field
.start
, dim
)
305 for i
in range(field
.count
):
306 field
.collect_dwords(dwords
,
307 start
+ field
.start
+ i
* field
.size
,
311 index
= (start
+ field
.start
) // 32
312 if not index
in dwords
:
313 dwords
[index
] = self
.DWord()
315 clone
= copy
.copy(field
)
316 clone
.start
= clone
.start
+ start
317 clone
.end
= clone
.end
+ start
319 dwords
[index
].fields
.append(clone
)
321 if field
.type == "address":
322 # assert dwords[index].address == None
323 dwords
[index
].address
= field
325 # Coalesce all the dwords covered by this field. The two cases we
326 # handle are where multiple fields are in a 64 bit word (typically
327 # and address and a few bits) or where a single struct field
328 # completely covers multiple dwords.
329 while index
< (start
+ field
.end
) // 32:
330 if index
+ 1 in dwords
and not dwords
[index
] == dwords
[index
+ 1]:
331 dwords
[index
].fields
.extend(dwords
[index
+ 1].fields
)
332 dwords
[index
].size
= 64
333 dwords
[index
+ 1] = dwords
[index
]
336 def emit_pack_function(self
, start
):
338 self
.collect_dwords(dwords
, 0, "")
340 # Determine number of dwords in this group. If we have a size, use
341 # that, since that'll account for MBZ dwords at the end of a group
342 # (like dword 8 on BDW+ 3DSTATE_HS). Otherwise, use the largest dword
343 # index we've seen plus one.
345 length
= self
.size
// 32
347 length
= max(dwords
.keys()) + 1
349 for index
in range(length
):
351 if not index
in dwords
:
353 print(" dw[%d] = 0;" % index
)
356 # For 64 bit dwords, we aliased the two dword entries in the dword
357 # dict it occupies. Now that we're emitting the pack function,
358 # skip the duplicate entries.
360 if index
> 0 and index
- 1 in dwords
and dw
== dwords
[index
- 1]:
363 # Special case: only one field and it's a struct at the beginning
364 # of the dword. In this case we pack directly into the
365 # destination. This is the only way we handle embedded structs
366 # larger than 32 bits.
367 if len(dw
.fields
) == 1:
369 name
= field
.name
+ field
.dim
370 if field
.type in self
.parser
.structs
and field
.start
% 32 == 0:
372 print(" %s_pack(data, &dw[%d], &values->%s);" %
373 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, name
))
376 # Pack any fields of struct type first so we have integer values
377 # to the dword for those fields.
379 for field
in dw
.fields
:
380 if type(field
) is Field
and field
.type in self
.parser
.structs
:
381 name
= field
.name
+ field
.dim
383 print(" uint32_t v%d_%d;" % (index
, field_index
))
384 print(" %s_pack(data, &v%d_%d, &values->%s);" %
385 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, field_index
, name
))
386 field_index
= field_index
+ 1
389 dword_start
= index
* 32
390 if dw
.address
== None:
395 if dw
.size
== 32 and dw
.address
== None:
397 print(" dw[%d] =" % index
)
398 elif len(dw
.fields
) > address_count
:
400 print(" const uint%d_t %s =" % (dw
.size
, v
))
405 for field
in dw
.fields
:
406 if field
.type != "mbo":
407 name
= field
.name
+ field
.dim
409 if field
.type == "mbo":
410 s
= "__gen_mbo(%d, %d)" % \
411 (field
.start
- dword_start
, field
.end
- dword_start
)
412 elif field
.type == "address":
414 elif field
.type == "uint":
415 s
= "__gen_uint(values->%s, %d, %d)" % \
416 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
417 elif field
.type == "int":
418 s
= "__gen_sint(values->%s, %d, %d)" % \
419 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
420 elif field
.type == "bool":
421 s
= "__gen_uint(values->%s, %d, %d)" % \
422 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
423 elif field
.type == "float":
424 s
= "__gen_float(values->%s)" % name
425 elif field
.type == "offset":
426 s
= "__gen_offset(values->%s, %d, %d)" % \
427 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
428 elif field
.type == 'ufixed':
429 s
= "__gen_ufixed(values->%s, %d, %d, %d)" % \
430 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
)
431 elif field
.type == 'sfixed':
432 s
= "__gen_sfixed(values->%s, %d, %d, %d)" % \
433 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
)
434 elif field
.type in self
.parser
.structs
:
435 s
= "__gen_uint(v%d_%d, %d, %d)" % \
436 (index
, field_index
, field
.start
- dword_start
, field
.end
- dword_start
)
437 field_index
= field_index
+ 1
439 print("/* unhandled field %s, type %s */\n" % (name
, field
.type))
443 if field
== dw
.fields
[-1]:
450 print(" dw[%d] = __gen_combine_address(data, &dw[%d], values->%s, %s);" % (index
, index
, dw
.address
.name
, v
))
454 v_address
= "v%d_address" % index
455 print(" const uint64_t %s =\n __gen_combine_address(data, &dw[%d], values->%s, %s);" %
456 (v_address
, index
, dw
.address
.name
, v
))
459 print(" dw[%d] = %s;" % (index
, v
))
460 print(" dw[%d] = %s >> 32;" % (index
+ 1, v
))
463 def __init__(self
, attrs
):
464 self
.name
= safe_name(attrs
["name"])
465 self
.value
= int(attrs
["value"])
469 self
.parser
= xml
.parsers
.expat
.ParserCreate()
470 self
.parser
.StartElementHandler
= self
.start_element
471 self
.parser
.EndElementHandler
= self
.end_element
473 self
.instruction
= None
476 def start_element(self
, name
, attrs
):
478 self
.platform
= attrs
["name"]
479 self
.gen
= attrs
["gen"].replace('.', '')
480 print(pack_header
% {'license': license
, 'platform': self
.platform
})
481 elif name
== "instruction":
482 self
.instruction
= safe_name(attrs
["name"])
483 self
.length_bias
= int(attrs
["bias"])
484 if "length" in attrs
:
485 self
.length
= int(attrs
["length"])
486 size
= self
.length
* 32
490 self
.group
= Group(self
, None, 0, 1, size
)
491 elif name
== "struct":
492 self
.struct
= safe_name(attrs
["name"])
493 self
.structs
[attrs
["name"]] = 1
494 if "length" in attrs
:
495 self
.length
= int(attrs
["length"])
496 size
= self
.length
* 32
500 self
.group
= Group(self
, None, 0, 1, size
)
502 elif name
== "group":
503 group
= Group(self
, self
.group
,
504 int(attrs
["start"]), int(attrs
["count"]), int(attrs
["size"]))
505 self
.group
.fields
.append(group
)
507 elif name
== "field":
508 self
.group
.fields
.append(Field(self
, attrs
))
512 self
.enum
= safe_name(attrs
["name"])
513 if "prefix" in attrs
:
514 self
.prefix
= safe_name(attrs
["prefix"])
517 elif name
== "value":
518 self
.values
.append(Value(attrs
))
520 def end_element(self
, name
):
521 if name
== "instruction":
522 self
.emit_instruction()
523 self
.instruction
= None
525 elif name
== "struct":
529 elif name
== "group":
530 self
.group
= self
.group
.parent
531 elif name
== "field":
532 self
.group
.fields
[-1].values
= self
.values
537 def gen_prefix(self
, name
):
539 return 'GEN%s%s' % (self
.gen
, name
)
541 return 'GEN%s_%s' % (self
.gen
, name
)
543 def emit_template_struct(self
, name
, group
):
544 print("struct %s {" % self
.gen_prefix(name
))
545 group
.emit_template_struct("")
548 def emit_pack_function(self
, name
, group
):
549 name
= self
.gen_prefix(name
)
550 print("static inline void\n%s_pack(__gen_user_data *data, void * restrict dst,\n%sconst struct %s * restrict values)\n{" %
551 (name
, ' ' * (len(name
) + 6), name
))
553 # Cast dst to make header C++ friendly
554 print(" uint32_t * restrict dw = (uint32_t * restrict) dst;")
556 group
.emit_pack_function(0)
560 def emit_instruction(self
):
561 name
= self
.instruction
562 if not self
.length
== None:
563 print('#define %-33s %4d' %
564 (self
.gen_prefix(name
+ "_length"), self
.length
))
565 print('#define %-33s %4d' %
566 (self
.gen_prefix(name
+ "_length_bias"), self
.length_bias
))
569 for field
in self
.group
.fields
:
570 if not type(field
) is Field
:
572 if field
.default
== None:
574 default_fields
.append(" .%-35s = %4d" % (field
.name
, field
.default
))
577 print('#define %-40s\\' % (self
.gen_prefix(name
+ '_header')))
578 print(", \\\n".join(default_fields
))
581 self
.emit_template_struct(self
.instruction
, self
.group
)
583 self
.emit_pack_function(self
.instruction
, self
.group
)
585 def emit_struct(self
):
587 if not self
.length
== None:
588 print('#define %-33s %4d' %
589 (self
.gen_prefix(name
+ "_length"), self
.length
))
591 self
.emit_template_struct(self
.struct
, self
.group
)
592 self
.emit_pack_function(self
.struct
, self
.group
)
595 print('/* enum %s */' % self
.gen_prefix(self
.enum
))
596 for value
in self
.values
:
598 name
= self
.prefix
+ "_" + value
.name
601 print('#define %-36s %4d' % (name
.upper(), value
.value
))
604 def parse(self
, filename
):
605 file = open(filename
, "rb")
606 self
.parser
.ParseFile(file)
609 if len(sys
.argv
) < 2:
610 print("No input xml file specified")
613 input_file
= sys
.argv
[1]