3 import xml
.parsers
.expat
9 * Copyright © 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.
44 #ifndef __gen_validate_value
45 #define __gen_validate_value(x)
48 #ifndef __gen_field_functions
49 #define __gen_field_functions
56 static inline uint64_t
57 __gen_mbo(uint32_t start, uint32_t end)
59 return (~0ull >> (64 - (end - start + 1))) << start;
62 static inline uint64_t
63 __gen_uint(uint64_t v, uint32_t start, uint32_t end)
65 const int width = end - start + 1;
67 __gen_validate_value(v);
71 const uint64_t max = (1ull << width) - 1;
79 static inline uint64_t
80 __gen_sint(int64_t v, uint32_t start, uint32_t end)
82 const int width = end - start + 1;
84 __gen_validate_value(v);
88 const int64_t max = (1ll << (width - 1)) - 1;
89 const int64_t min = -(1ll << (width - 1));
90 assert(min <= v && v <= max);
94 const uint64_t mask = ~0ull >> (64 - width);
96 return (v & mask) << start;
99 static inline uint64_t
100 __gen_offset(uint64_t v, uint32_t start, uint32_t end)
102 __gen_validate_value(v);
104 uint64_t mask = (~0ull >> (64 - (end - start + 1))) << start;
106 assert((v & ~mask) == 0);
112 static inline uint32_t
115 __gen_validate_value(v);
116 return ((union __gen_value) { .f = (v) }).dw;
119 static inline uint64_t
120 __gen_sfixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
122 __gen_validate_value(v);
124 const float factor = (1 << fract_bits);
127 const float max = ((1 << (end - start)) - 1) / factor;
128 const float min = -(1 << (end - start)) / factor;
129 assert(min <= v && v <= max);
132 const int32_t int_val = roundf(v * factor);
133 const uint64_t mask = ~0ull >> (64 - (end - start + 1));
135 return (int_val & mask) << start;
138 static inline uint64_t
139 __gen_ufixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
141 __gen_validate_value(v);
143 const float factor = (1 << fract_bits);
146 const float max = ((1 << (end - start + 1)) - 1) / factor;
147 const float min = 0.0f;
148 assert(min <= v && v <= max);
151 const uint32_t uint_val = roundf(v * factor);
153 return uint_val << start;
156 #ifndef __gen_address_type
157 #error #define __gen_address_type before including this file
160 #ifndef __gen_user_data
161 #error #define __gen_combine_address before including this file
168 def to_alphanum(name
):
191 for i
, j
in substitutions
.items():
192 name
= name
.replace(i
, j
)
197 name
= to_alphanum(name
)
198 if not str.isalpha(name
[0]):
204 ufixed_pattern
= re
.compile("u(\d+)\.(\d+)")
205 sfixed_pattern
= re
.compile("s(\d+)\.(\d+)")
207 def __init__(self
, parser
, attrs
):
210 self
.name
= safe_name(attrs
["name"])
211 self
.start
= int(attrs
["start"])
212 self
.end
= int(attrs
["end"])
213 self
.type = attrs
["type"]
215 if "prefix" in attrs
:
216 self
.prefix
= attrs
["prefix"]
220 if "default" in attrs
:
221 self
.default
= int(attrs
["default"])
225 ufixed_match
= Field
.ufixed_pattern
.match(self
.type)
228 self
.fractional_size
= int(ufixed_match
.group(2))
230 sfixed_match
= Field
.sfixed_pattern
.match(self
.type)
233 self
.fractional_size
= int(sfixed_match
.group(2))
235 def emit_template_struct(self
, dim
):
236 if self
.type == 'address':
237 type = '__gen_address_type'
238 elif self
.type == 'bool':
240 elif self
.type == 'float':
242 elif self
.type == 'ufixed':
244 elif self
.type == 'sfixed':
246 elif self
.type == 'uint' and self
.end
- self
.start
> 32:
248 elif self
.type == 'offset':
250 elif self
.type == 'int':
252 elif self
.type == 'uint':
254 elif self
.type in self
.parser
.structs
:
255 type = 'struct ' + self
.parser
.gen_prefix(safe_name(self
.type))
256 elif self
.type == 'mbo':
259 print("#error unhandled type: %s" % self
.type)
261 print(" %-36s %s%s;" % (type, self
.name
, dim
))
263 if len(self
.values
) > 0 and self
.default
== None:
265 prefix
= self
.prefix
+ "_"
269 for value
in self
.values
:
270 print("#define %-40s %d" % (prefix
+ value
.name
, value
.value
))
273 def __init__(self
, parser
, parent
, start
, count
, size
):
281 def emit_template_struct(self
, dim
):
283 print(" /* variable length fields follow */")
286 dim
= "%s[%d]" % (dim
, self
.count
)
288 for field
in self
.fields
:
289 field
.emit_template_struct(dim
)
297 def collect_dwords(self
, dwords
, start
, dim
):
298 for field
in self
.fields
:
299 if type(field
) is Group
:
301 field
.collect_dwords(dwords
, start
+ field
.start
, dim
)
303 for i
in range(field
.count
):
304 field
.collect_dwords(dwords
,
305 start
+ field
.start
+ i
* field
.size
,
309 index
= (start
+ field
.start
) // 32
310 if not index
in dwords
:
311 dwords
[index
] = self
.DWord()
313 clone
= copy
.copy(field
)
314 clone
.start
= clone
.start
+ start
315 clone
.end
= clone
.end
+ start
317 dwords
[index
].fields
.append(clone
)
319 if field
.type == "address":
320 # assert dwords[index].address == None
321 dwords
[index
].address
= field
323 # Coalesce all the dwords covered by this field. The two cases we
324 # handle are where multiple fields are in a 64 bit word (typically
325 # and address and a few bits) or where a single struct field
326 # completely covers multiple dwords.
327 while index
< (start
+ field
.end
) // 32:
328 if index
+ 1 in dwords
and not dwords
[index
] == dwords
[index
+ 1]:
329 dwords
[index
].fields
.extend(dwords
[index
+ 1].fields
)
330 dwords
[index
].size
= 64
331 dwords
[index
+ 1] = dwords
[index
]
334 def emit_pack_function(self
, start
):
336 self
.collect_dwords(dwords
, 0, "")
338 # Determine number of dwords in this group. If we have a size, use
339 # that, since that'll account for MBZ dwords at the end of a group
340 # (like dword 8 on BDW+ 3DSTATE_HS). Otherwise, use the largest dword
341 # index we've seen plus one.
343 length
= self
.size
// 32
345 length
= max(dwords
.keys()) + 1
347 for index
in range(length
):
349 if not index
in dwords
:
351 print(" dw[%d] = 0;" % index
)
354 # For 64 bit dwords, we aliased the two dword entries in the dword
355 # dict it occupies. Now that we're emitting the pack function,
356 # skip the duplicate entries.
358 if index
> 0 and index
- 1 in dwords
and dw
== dwords
[index
- 1]:
361 # Special case: only one field and it's a struct at the beginning
362 # of the dword. In this case we pack directly into the
363 # destination. This is the only way we handle embedded structs
364 # larger than 32 bits.
365 if len(dw
.fields
) == 1:
367 name
= field
.name
+ field
.dim
368 if field
.type in self
.parser
.structs
and field
.start
% 32 == 0:
370 print(" %s_pack(data, &dw[%d], &values->%s);" %
371 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, name
))
374 # Pack any fields of struct type first so we have integer values
375 # to the dword for those fields.
377 for field
in dw
.fields
:
378 if type(field
) is Field
and field
.type in self
.parser
.structs
:
379 name
= field
.name
+ field
.dim
381 print(" uint32_t v%d_%d;" % (index
, field_index
))
382 print(" %s_pack(data, &v%d_%d, &values->%s);" %
383 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, field_index
, name
))
384 field_index
= field_index
+ 1
387 dword_start
= index
* 32
388 if dw
.address
== None:
393 if dw
.size
== 32 and dw
.address
== None:
395 print(" dw[%d] =" % index
)
396 elif len(dw
.fields
) > address_count
:
398 print(" const uint%d_t %s =" % (dw
.size
, v
))
403 for field
in dw
.fields
:
404 if field
.type != "mbo":
405 name
= field
.name
+ field
.dim
407 if field
.type == "mbo":
408 s
= "__gen_mbo(%d, %d)" % \
409 (field
.start
- dword_start
, field
.end
- dword_start
)
410 elif field
.type == "address":
412 elif field
.type == "uint":
413 s
= "__gen_uint(values->%s, %d, %d)" % \
414 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
415 elif field
.type == "int":
416 s
= "__gen_sint(values->%s, %d, %d)" % \
417 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
418 elif field
.type == "bool":
419 s
= "__gen_uint(values->%s, %d, %d)" % \
420 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
421 elif field
.type == "float":
422 s
= "__gen_float(values->%s)" % name
423 elif field
.type == "offset":
424 s
= "__gen_offset(values->%s, %d, %d)" % \
425 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
426 elif field
.type == 'ufixed':
427 s
= "__gen_ufixed(values->%s, %d, %d, %d)" % \
428 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
)
429 elif field
.type == 'sfixed':
430 s
= "__gen_sfixed(values->%s, %d, %d, %d)" % \
431 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
)
432 elif field
.type in self
.parser
.structs
:
433 s
= "__gen_uint(v%d_%d, %d, %d)" % \
434 (index
, field_index
, field
.start
- dword_start
, field
.end
- dword_start
)
435 field_index
= field_index
+ 1
437 print("/* unhandled field %s, type %s */\n" % (name
, field
.type))
441 if field
== dw
.fields
[-1]:
448 print(" dw[%d] = __gen_combine_address(data, &dw[%d], values->%s, %s);" % (index
, index
, dw
.address
.name
, v
))
452 v_address
= "v%d_address" % index
453 print(" const uint64_t %s =\n __gen_combine_address(data, &dw[%d], values->%s, %s);" %
454 (v_address
, index
, dw
.address
.name
, v
))
457 print(" dw[%d] = %s;" % (index
, v
))
458 print(" dw[%d] = %s >> 32;" % (index
+ 1, v
))
461 def __init__(self
, attrs
):
462 self
.name
= safe_name(attrs
["name"])
463 self
.value
= int(attrs
["value"])
467 self
.parser
= xml
.parsers
.expat
.ParserCreate()
468 self
.parser
.StartElementHandler
= self
.start_element
469 self
.parser
.EndElementHandler
= self
.end_element
471 self
.instruction
= None
474 def start_element(self
, name
, attrs
):
476 self
.platform
= attrs
["name"]
477 self
.gen
= attrs
["gen"].replace('.', '')
478 print(pack_header
% {'license': license
, 'platform': self
.platform
})
479 elif name
== "instruction":
480 self
.instruction
= safe_name(attrs
["name"])
481 self
.length_bias
= int(attrs
["bias"])
482 if "length" in attrs
:
483 self
.length
= int(attrs
["length"])
484 size
= self
.length
* 32
488 self
.group
= Group(self
, None, 0, 1, size
)
489 elif name
== "struct":
490 self
.struct
= safe_name(attrs
["name"])
491 self
.structs
[attrs
["name"]] = 1
492 if "length" in attrs
:
493 self
.length
= int(attrs
["length"])
494 size
= self
.length
* 32
498 self
.group
= Group(self
, None, 0, 1, size
)
500 elif name
== "group":
501 group
= Group(self
, self
.group
,
502 int(attrs
["start"]), int(attrs
["count"]), int(attrs
["size"]))
503 self
.group
.fields
.append(group
)
505 elif name
== "field":
506 self
.group
.fields
.append(Field(self
, attrs
))
510 self
.enum
= safe_name(attrs
["name"])
511 if "prefix" in attrs
:
512 self
.prefix
= safe_name(attrs
["prefix"])
515 elif name
== "value":
516 self
.values
.append(Value(attrs
))
518 def end_element(self
, name
):
519 if name
== "instruction":
520 self
.emit_instruction()
521 self
.instruction
= None
523 elif name
== "struct":
527 elif name
== "group":
528 self
.group
= self
.group
.parent
529 elif name
== "field":
530 self
.group
.fields
[-1].values
= self
.values
535 def gen_prefix(self
, name
):
537 return 'GEN%s%s' % (self
.gen
, name
)
539 return 'GEN%s_%s' % (self
.gen
, name
)
541 def emit_template_struct(self
, name
, group
):
542 print("struct %s {" % self
.gen_prefix(name
))
543 group
.emit_template_struct("")
546 def emit_pack_function(self
, name
, group
):
547 name
= self
.gen_prefix(name
)
548 print("static inline void\n%s_pack(__gen_user_data *data, void * restrict dst,\n%sconst struct %s * restrict values)\n{" %
549 (name
, ' ' * (len(name
) + 6), name
))
551 # Cast dst to make header C++ friendly
552 print(" uint32_t * restrict dw = (uint32_t * restrict) dst;")
554 group
.emit_pack_function(0)
558 def emit_instruction(self
):
559 name
= self
.instruction
560 if not self
.length
== None:
561 print('#define %-33s %4d' %
562 (self
.gen_prefix(name
+ "_length"), self
.length
))
563 print('#define %-33s %4d' %
564 (self
.gen_prefix(name
+ "_length_bias"), self
.length_bias
))
567 for field
in self
.group
.fields
:
568 if not type(field
) is Field
:
570 if field
.default
== None:
572 default_fields
.append(" .%-35s = %4d" % (field
.name
, field
.default
))
575 print('#define %-40s\\' % (self
.gen_prefix(name
+ '_header')))
576 print(", \\\n".join(default_fields
))
579 self
.emit_template_struct(self
.instruction
, self
.group
)
581 self
.emit_pack_function(self
.instruction
, self
.group
)
583 def emit_struct(self
):
585 if not self
.length
== None:
586 print('#define %-33s %4d' %
587 (self
.gen_prefix(name
+ "_length"), self
.length
))
589 self
.emit_template_struct(self
.struct
, self
.group
)
590 self
.emit_pack_function(self
.struct
, self
.group
)
593 print('/* enum %s */' % self
.gen_prefix(self
.enum
))
594 for value
in self
.values
:
596 name
= self
.prefix
+ "_" + value
.name
599 print('#define %-36s %4d' % (name
.upper(), value
.value
))
602 def parse(self
, filename
):
603 file = open(filename
, "rb")
604 self
.parser
.ParseFile(file)
607 if len(sys
.argv
) < 2:
608 print("No input xml file specified")
611 input_file
= sys
.argv
[1]