9ef7122ae9e260cee5853766729a838c8ef21858
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]):
205 def num_from_str(num_str
):
206 if num_str
.lower().startswith('0x'):
207 return int(num_str
, base
=16)
209 assert(not num_str
.startswith('0') and 'octals numbers not allowed')
213 ufixed_pattern
= re
.compile("u(\d+)\.(\d+)")
214 sfixed_pattern
= re
.compile("s(\d+)\.(\d+)")
216 def __init__(self
, parser
, attrs
):
219 self
.name
= safe_name(attrs
["name"])
220 self
.start
= int(attrs
["start"])
221 self
.end
= int(attrs
["end"])
222 self
.type = attrs
["type"]
224 if "prefix" in attrs
:
225 self
.prefix
= attrs
["prefix"]
229 if "default" in attrs
:
230 self
.default
= int(attrs
["default"])
234 ufixed_match
= Field
.ufixed_pattern
.match(self
.type)
237 self
.fractional_size
= int(ufixed_match
.group(2))
239 sfixed_match
= Field
.sfixed_pattern
.match(self
.type)
242 self
.fractional_size
= int(sfixed_match
.group(2))
244 def emit_template_struct(self
, dim
):
245 if self
.type == 'address':
246 type = '__gen_address_type'
247 elif self
.type == 'bool':
249 elif self
.type == 'float':
251 elif self
.type == 'ufixed':
253 elif self
.type == 'sfixed':
255 elif self
.type == 'uint' and self
.end
- self
.start
> 32:
257 elif self
.type == 'offset':
259 elif self
.type == 'int':
261 elif self
.type == 'uint':
263 elif self
.type in self
.parser
.structs
:
264 type = 'struct ' + self
.parser
.gen_prefix(safe_name(self
.type))
265 elif self
.type == 'mbo':
268 print("#error unhandled type: %s" % self
.type)
270 print(" %-36s %s%s;" % (type, self
.name
, dim
))
272 if len(self
.values
) > 0 and self
.default
== None:
274 prefix
= self
.prefix
+ "_"
278 for value
in self
.values
:
279 print("#define %-40s %d" % (prefix
+ value
.name
, value
.value
))
282 def __init__(self
, parser
, parent
, start
, count
, size
):
290 def emit_template_struct(self
, dim
):
292 print(" /* variable length fields follow */")
295 dim
= "%s[%d]" % (dim
, self
.count
)
297 for field
in self
.fields
:
298 field
.emit_template_struct(dim
)
306 def collect_dwords(self
, dwords
, start
, dim
):
307 for field
in self
.fields
:
308 if type(field
) is Group
:
310 field
.collect_dwords(dwords
, start
+ field
.start
, dim
)
312 for i
in range(field
.count
):
313 field
.collect_dwords(dwords
,
314 start
+ field
.start
+ i
* field
.size
,
318 index
= (start
+ field
.start
) // 32
319 if not index
in dwords
:
320 dwords
[index
] = self
.DWord()
322 clone
= copy
.copy(field
)
323 clone
.start
= clone
.start
+ start
324 clone
.end
= clone
.end
+ start
326 dwords
[index
].fields
.append(clone
)
328 if field
.type == "address":
329 # assert dwords[index].address == None
330 dwords
[index
].address
= field
332 # Coalesce all the dwords covered by this field. The two cases we
333 # handle are where multiple fields are in a 64 bit word (typically
334 # and address and a few bits) or where a single struct field
335 # completely covers multiple dwords.
336 while index
< (start
+ field
.end
) // 32:
337 if index
+ 1 in dwords
and not dwords
[index
] == dwords
[index
+ 1]:
338 dwords
[index
].fields
.extend(dwords
[index
+ 1].fields
)
339 dwords
[index
].size
= 64
340 dwords
[index
+ 1] = dwords
[index
]
343 def emit_pack_function(self
, start
):
345 self
.collect_dwords(dwords
, 0, "")
347 # Determine number of dwords in this group. If we have a size, use
348 # that, since that'll account for MBZ dwords at the end of a group
349 # (like dword 8 on BDW+ 3DSTATE_HS). Otherwise, use the largest dword
350 # index we've seen plus one.
352 length
= self
.size
// 32
354 length
= max(dwords
.keys()) + 1
356 for index
in range(length
):
358 if not index
in dwords
:
360 print(" dw[%d] = 0;" % index
)
363 # For 64 bit dwords, we aliased the two dword entries in the dword
364 # dict it occupies. Now that we're emitting the pack function,
365 # skip the duplicate entries.
367 if index
> 0 and index
- 1 in dwords
and dw
== dwords
[index
- 1]:
370 # Special case: only one field and it's a struct at the beginning
371 # of the dword. In this case we pack directly into the
372 # destination. This is the only way we handle embedded structs
373 # larger than 32 bits.
374 if len(dw
.fields
) == 1:
376 name
= field
.name
+ field
.dim
377 if field
.type in self
.parser
.structs
and field
.start
% 32 == 0:
379 print(" %s_pack(data, &dw[%d], &values->%s);" %
380 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, name
))
383 # Pack any fields of struct type first so we have integer values
384 # to the dword for those fields.
386 for field
in dw
.fields
:
387 if type(field
) is Field
and field
.type in self
.parser
.structs
:
388 name
= field
.name
+ field
.dim
390 print(" uint32_t v%d_%d;" % (index
, field_index
))
391 print(" %s_pack(data, &v%d_%d, &values->%s);" %
392 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, field_index
, name
))
393 field_index
= field_index
+ 1
396 dword_start
= index
* 32
397 if dw
.address
== None:
402 if dw
.size
== 32 and dw
.address
== None:
404 print(" dw[%d] =" % index
)
405 elif len(dw
.fields
) > address_count
:
407 print(" const uint%d_t %s =" % (dw
.size
, v
))
412 for field
in dw
.fields
:
413 if field
.type != "mbo":
414 name
= field
.name
+ field
.dim
416 if field
.type == "mbo":
417 s
= "__gen_mbo(%d, %d)" % \
418 (field
.start
- dword_start
, field
.end
- dword_start
)
419 elif field
.type == "address":
421 elif field
.type == "uint":
422 s
= "__gen_uint(values->%s, %d, %d)" % \
423 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
424 elif field
.type == "int":
425 s
= "__gen_sint(values->%s, %d, %d)" % \
426 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
427 elif field
.type == "bool":
428 s
= "__gen_uint(values->%s, %d, %d)" % \
429 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
430 elif field
.type == "float":
431 s
= "__gen_float(values->%s)" % name
432 elif field
.type == "offset":
433 s
= "__gen_offset(values->%s, %d, %d)" % \
434 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
435 elif field
.type == 'ufixed':
436 s
= "__gen_ufixed(values->%s, %d, %d, %d)" % \
437 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
)
438 elif field
.type == 'sfixed':
439 s
= "__gen_sfixed(values->%s, %d, %d, %d)" % \
440 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
)
441 elif field
.type in self
.parser
.structs
:
442 s
= "__gen_uint(v%d_%d, %d, %d)" % \
443 (index
, field_index
, field
.start
- dword_start
, field
.end
- dword_start
)
444 field_index
= field_index
+ 1
446 print("/* unhandled field %s, type %s */\n" % (name
, field
.type))
450 if field
== dw
.fields
[-1]:
457 print(" dw[%d] = __gen_combine_address(data, &dw[%d], values->%s, %s);" % (index
, index
, dw
.address
.name
, v
))
461 v_address
= "v%d_address" % index
462 print(" const uint64_t %s =\n __gen_combine_address(data, &dw[%d], values->%s, %s);" %
463 (v_address
, index
, dw
.address
.name
, v
))
466 print(" dw[%d] = %s;" % (index
, v
))
467 print(" dw[%d] = %s >> 32;" % (index
+ 1, v
))
470 def __init__(self
, attrs
):
471 self
.name
= safe_name(attrs
["name"])
472 self
.value
= int(attrs
["value"])
476 self
.parser
= xml
.parsers
.expat
.ParserCreate()
477 self
.parser
.StartElementHandler
= self
.start_element
478 self
.parser
.EndElementHandler
= self
.end_element
480 self
.instruction
= None
484 def start_element(self
, name
, attrs
):
486 self
.platform
= attrs
["name"]
487 self
.gen
= attrs
["gen"].replace('.', '')
488 print(pack_header
% {'license': license
, 'platform': self
.platform
})
489 elif name
in ("instruction", "struct", "register"):
490 if name
== "instruction":
491 self
.instruction
= safe_name(attrs
["name"])
492 self
.length_bias
= int(attrs
["bias"])
493 elif name
== "struct":
494 self
.struct
= safe_name(attrs
["name"])
495 self
.structs
[attrs
["name"]] = 1
496 elif name
== "register":
497 self
.register
= safe_name(attrs
["name"])
498 self
.reg_num
= num_from_str(attrs
["num"])
499 self
.registers
[attrs
["name"]] = 1
500 if "length" in attrs
:
501 self
.length
= int(attrs
["length"])
502 size
= self
.length
* 32
506 self
.group
= Group(self
, None, 0, 1, size
)
508 elif name
== "group":
509 group
= Group(self
, self
.group
,
510 int(attrs
["start"]), int(attrs
["count"]), int(attrs
["size"]))
511 self
.group
.fields
.append(group
)
513 elif name
== "field":
514 self
.group
.fields
.append(Field(self
, attrs
))
518 self
.enum
= safe_name(attrs
["name"])
519 if "prefix" in attrs
:
520 self
.prefix
= safe_name(attrs
["prefix"])
523 elif name
== "value":
524 self
.values
.append(Value(attrs
))
526 def end_element(self
, name
):
527 if name
== "instruction":
528 self
.emit_instruction()
529 self
.instruction
= None
531 elif name
== "struct":
535 elif name
== "register":
540 elif name
== "group":
541 self
.group
= self
.group
.parent
542 elif name
== "field":
543 self
.group
.fields
[-1].values
= self
.values
548 def gen_prefix(self
, name
):
550 return 'GEN%s%s' % (self
.gen
, name
)
552 return 'GEN%s_%s' % (self
.gen
, name
)
554 def emit_template_struct(self
, name
, group
):
555 print("struct %s {" % self
.gen_prefix(name
))
556 group
.emit_template_struct("")
559 def emit_pack_function(self
, name
, group
):
560 name
= self
.gen_prefix(name
)
561 print("static inline void\n%s_pack(__gen_user_data *data, void * restrict dst,\n%sconst struct %s * restrict values)\n{" %
562 (name
, ' ' * (len(name
) + 6), name
))
564 # Cast dst to make header C++ friendly
565 print(" uint32_t * restrict dw = (uint32_t * restrict) dst;")
567 group
.emit_pack_function(0)
571 def emit_instruction(self
):
572 name
= self
.instruction
573 if not self
.length
== None:
574 print('#define %-33s %6d' %
575 (self
.gen_prefix(name
+ "_length"), self
.length
))
576 print('#define %-33s %6d' %
577 (self
.gen_prefix(name
+ "_length_bias"), self
.length_bias
))
580 for field
in self
.group
.fields
:
581 if not type(field
) is Field
:
583 if field
.default
== None:
585 default_fields
.append(" .%-35s = %6d" % (field
.name
, field
.default
))
588 print('#define %-40s\\' % (self
.gen_prefix(name
+ '_header')))
589 print(", \\\n".join(default_fields
))
592 self
.emit_template_struct(self
.instruction
, self
.group
)
594 self
.emit_pack_function(self
.instruction
, self
.group
)
596 def emit_register(self
):
598 if not self
.reg_num
== None:
599 print('#define %-33s 0x%04x' %
600 (self
.gen_prefix(name
+ "_num"), self
.reg_num
))
602 if not self
.length
== None:
603 print('#define %-33s %6d' %
604 (self
.gen_prefix(name
+ "_length"), self
.length
))
606 self
.emit_template_struct(self
.register
, self
.group
)
607 self
.emit_pack_function(self
.register
, self
.group
)
609 def emit_struct(self
):
611 if not self
.length
== None:
612 print('#define %-33s %6d' %
613 (self
.gen_prefix(name
+ "_length"), self
.length
))
615 self
.emit_template_struct(self
.struct
, self
.group
)
616 self
.emit_pack_function(self
.struct
, self
.group
)
619 print('/* enum %s */' % self
.gen_prefix(self
.enum
))
620 for value
in self
.values
:
622 name
= self
.prefix
+ "_" + value
.name
625 print('#define %-36s %6d' % (name
.upper(), value
.value
))
628 def parse(self
, filename
):
629 file = open(filename
, "rb")
630 self
.parser
.ParseFile(file)
633 if len(sys
.argv
) < 2:
634 print("No input xml file specified")
637 input_file
= sys
.argv
[1]