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
):
209 self
.name
= safe_name(attrs
["name"])
210 self
.start
= int(attrs
["start"])
211 self
.end
= int(attrs
["end"])
212 self
.type = attrs
["type"]
214 if "prefix" in attrs
:
215 self
.prefix
= attrs
["prefix"]
219 if "default" in attrs
:
220 self
.default
= int(attrs
["default"])
224 ufixed_match
= Field
.ufixed_pattern
.match(self
.type)
227 self
.fractional_size
= int(ufixed_match
.group(2))
229 sfixed_match
= Field
.sfixed_pattern
.match(self
.type)
232 self
.fractional_size
= int(sfixed_match
.group(2))
234 def emit_template_struct(self
, dim
):
235 if self
.type == 'address':
236 type = '__gen_address_type'
237 elif self
.type == 'bool':
239 elif self
.type == 'float':
241 elif self
.type == 'ufixed':
243 elif self
.type == 'sfixed':
245 elif self
.type == 'uint' and self
.end
- self
.start
> 32:
247 elif self
.type == 'offset':
249 elif self
.type == 'int':
251 elif self
.type == 'uint':
253 elif self
.type in self
.parser
.structs
:
254 type = 'struct ' + self
.parser
.gen_prefix(safe_name(self
.type))
256 print("#error unhandled type: %s" % self
.type)
258 print(" %-36s %s%s;" % (type, self
.name
, dim
))
260 if len(self
.values
) > 0 and self
.default
== None:
262 prefix
= self
.prefix
+ "_"
266 for value
in self
.values
:
267 print("#define %-40s %d" % (prefix
+ value
.name
, value
.value
))
270 def __init__(self
, parser
, parent
, start
, count
, size
):
278 def emit_template_struct(self
, dim
):
280 print(" /* variable length fields follow */")
283 dim
= "%s[%d]" % (dim
, self
.count
)
285 for field
in self
.fields
:
286 field
.emit_template_struct(dim
)
294 def collect_dwords(self
, dwords
, start
, dim
):
295 for field
in self
.fields
:
296 if type(field
) is Group
:
298 field
.collect_dwords(dwords
, start
+ field
.start
, dim
)
300 for i
in range(field
.count
):
301 field
.collect_dwords(dwords
,
302 start
+ field
.start
+ i
* field
.size
,
306 index
= (start
+ field
.start
) // 32
307 if not index
in dwords
:
308 dwords
[index
] = self
.DWord()
310 clone
= copy
.copy(field
)
311 clone
.start
= clone
.start
+ start
312 clone
.end
= clone
.end
+ start
314 dwords
[index
].fields
.append(clone
)
316 if field
.type == "address":
317 # assert dwords[index].address == None
318 dwords
[index
].address
= field
320 # Does this field extend into the next dword?
321 if index
< field
.end
// 32 and dwords
[index
].size
== 32:
322 if index
+ 1 in dwords
:
323 assert dwords
[index
+ 1].size
== 32
324 dwords
[index
].fields
.extend(dwords
[index
+ 1].fields
)
325 dwords
[index
].size
= 64
326 dwords
[index
+ 1] = dwords
[index
]
328 def emit_pack_function(self
, start
):
330 self
.collect_dwords(dwords
, 0, "")
332 for index
, dw
in dwords
.items():
333 # For 64 bit dwords, we aliased the two dword entries in the dword
334 # dict it occupies. Now that we're emitting the pack function,
335 # skip the duplicate entries.
336 if index
> 0 and index
- 1 in dwords
and dwords
[index
- 1] == dwords
[index
]:
339 # Special case: only one field and it's a struct at the beginning
340 # of the dword. In this case we pack directly into the
341 # destination. This is the only way we handle embedded structs
342 # larger than 32 bits.
343 if len(dw
.fields
) == 1:
345 name
= field
.name
+ field
.dim
346 if field
.type in self
.parser
.structs
and field
.start
% 32 == 0:
348 print(" %s_pack(data, &dw[%d], &values->%s);" %
349 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, name
))
352 # Pack any fields of struct type first so we have integer values
353 # to the dword for those fields.
355 for field
in dw
.fields
:
356 if type(field
) is Field
and field
.type in self
.parser
.structs
:
357 name
= field
.name
+ field
.dim
359 print(" uint32_t v%d_%d;" % (index
, field_index
))
360 print(" %s_pack(data, &v%d_%d, &values->%s);" %
361 (self
.parser
.gen_prefix(safe_name(field
.type)), index
, field_index
, name
))
362 field_index
= field_index
+ 1
365 dword_start
= index
* 32
366 if dw
.address
== None:
371 if dw
.size
== 32 and dw
.address
== None:
373 print(" dw[%d] =" % index
)
374 elif len(dw
.fields
) > address_count
:
376 print(" const uint%d_t %s =" % (dw
.size
, v
))
381 for field
in dw
.fields
:
382 name
= field
.name
+ field
.dim
383 if field
.type == "mbo":
384 s
= "__gen_mbo(%d, %d)" % \
385 (field
.start
- dword_start
, field
.end
- dword_start
)
386 elif field
.type == "address":
388 elif field
.type == "uint":
389 s
= "__gen_uint(values->%s, %d, %d)" % \
390 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
391 elif field
.type == "int":
392 s
= "__gen_sint(values->%s, %d, %d)" % \
393 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
394 elif field
.type == "bool":
395 s
= "__gen_uint(values->%s, %d, %d)" % \
396 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
397 elif field
.type == "float":
398 s
= "__gen_float(values->%s)" % name
399 elif field
.type == "offset":
400 s
= "__gen_offset(values->%s, %d, %d)" % \
401 (name
, field
.start
- dword_start
, field
.end
- dword_start
)
402 elif field
.type == 'ufixed':
403 s
= "__gen_ufixed(values->%s, %d, %d, %d)" % \
404 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
)
405 elif field
.type == 'sfixed':
406 s
= "__gen_sfixed(values->%s, %d, %d, %d)" % \
407 (name
, field
.start
- dword_start
, field
.end
- dword_start
, field
.fractional_size
)
408 elif field
.type in self
.parser
.structs
:
409 s
= "__gen_uint(v%d_%d, %d, %d)" % \
410 (index
, field_index
, field
.start
- dword_start
, field
.end
- dword_start
)
411 field_index
= field_index
+ 1
413 print("/* unhandled field %s, type %s */\n" % (name
, field
.type))
417 if field
== dw
.fields
[-1]:
424 print(" dw[%d] = __gen_combine_address(data, &dw[%d], values->%s, %s);" % (index
, index
, dw
.address
.name
, v
))
428 v_address
= "v%d_address" % index
429 print(" const uint64_t %s =\n __gen_combine_address(data, &dw[%d], values->%s, %s);" %
430 (v_address
, index
, dw
.address
.name
, v
))
433 print(" dw[%d] = %s;" % (index
, v
))
434 print(" dw[%d] = %s >> 32;" % (index
+ 1, v
))
437 def __init__(self
, attrs
):
438 self
.name
= safe_name(attrs
["name"])
439 self
.value
= int(attrs
["value"])
443 self
.parser
= xml
.parsers
.expat
.ParserCreate()
444 self
.parser
.StartElementHandler
= self
.start_element
445 self
.parser
.EndElementHandler
= self
.end_element
447 self
.instruction
= None
450 def start_element(self
, name
, attrs
):
452 self
.platform
= attrs
["name"]
453 self
.gen
= attrs
["gen"].replace('.', '')
454 print(pack_header
% {'license': license
, 'platform': self
.platform
})
455 elif name
== "instruction":
456 self
.group
= Group(self
, None, 0, 1, 0)
457 self
.instruction
= safe_name(attrs
["name"])
458 self
.length_bias
= int(attrs
["bias"])
459 if "length" in attrs
:
460 self
.length
= int(attrs
["length"])
463 elif name
== "struct":
464 self
.group
= Group(self
, None, 0, 1, 0)
465 self
.struct
= safe_name(attrs
["name"])
466 self
.structs
[attrs
["name"]] = 1
467 if "length" in attrs
:
468 self
.length
= int(attrs
["length"])
471 elif name
== "group":
472 group
= Group(self
, self
.group
,
473 int(attrs
["start"]), int(attrs
["count"]), int(attrs
["size"]))
474 self
.group
.fields
.append(group
)
476 elif name
== "field":
477 self
.group
.fields
.append(Field(self
, attrs
))
481 self
.enum
= safe_name(attrs
["name"])
482 if "prefix" in attrs
:
483 self
.prefix
= safe_name(attrs
["prefix"])
486 elif name
== "value":
487 self
.values
.append(Value(attrs
))
489 def end_element(self
, name
):
490 if name
== "instruction":
491 self
.emit_instruction()
492 self
.instruction
= None
494 elif name
== "struct":
498 elif name
== "group":
499 self
.group
= self
.group
.parent
500 elif name
== "field":
501 self
.group
.fields
[-1].values
= self
.values
506 def gen_prefix(self
, name
):
508 return 'GEN%s%s' % (self
.gen
, name
)
510 return 'GEN%s_%s' % (self
.gen
, name
)
512 def emit_template_struct(self
, name
, group
):
513 print("struct %s {" % self
.gen_prefix(name
))
514 group
.emit_template_struct("")
517 def emit_pack_function(self
, name
, group
):
518 name
= self
.gen_prefix(name
)
519 print("static inline void\n%s_pack(__gen_user_data *data, void * restrict dst,\n%sconst struct %s * restrict values)\n{" %
520 (name
, ' ' * (len(name
) + 6), name
))
522 # Cast dst to make header C++ friendly
523 print(" uint32_t * restrict dw = (uint32_t * restrict) dst;")
525 group
.emit_pack_function(0)
529 def emit_instruction(self
):
530 name
= self
.instruction
531 if not self
.length
== None:
532 print('#define %-33s %4d' %
533 (self
.gen_prefix(name
+ "_length"), self
.length
))
534 print('#define %-33s %4d' %
535 (self
.gen_prefix(name
+ "_length_bias"), self
.length_bias
))
538 for field
in self
.group
.fields
:
539 if not type(field
) is Field
:
541 if field
.default
== None:
543 default_fields
.append(" .%-35s = %4d" % (field
.name
, field
.default
))
546 print('#define %-40s\\' % (self
.gen_prefix(name
+ '_header')))
547 print(", \\\n".join(default_fields
))
550 self
.emit_template_struct(self
.instruction
, self
.group
)
552 self
.emit_pack_function(self
.instruction
, self
.group
)
554 def emit_struct(self
):
556 if not self
.length
== None:
557 print('#define %-33s %4d' %
558 (self
.gen_prefix(name
+ "_length"), self
.length
))
560 self
.emit_template_struct(self
.struct
, self
.group
)
561 self
.emit_pack_function(self
.struct
, self
.group
)
564 print('/* enum %s */' % self
.gen_prefix(self
.enum
))
565 for value
in self
.values
:
567 name
= self
.prefix
+ "_" + value
.name
570 print('#define %-36s %4d' % (name
.upper(), value
.value
))
573 def parse(self
, filename
):
574 file = open(filename
, "rb")
575 self
.parser
.ParseFile(file)
578 if len(sys
.argv
) < 2:
579 print("No input xml file specified")
582 input_file
= sys
.argv
[1]