Debian Codegraph @

src/imagination/pco/pco_common.h.py

raw
1 # Copyright © 2024 Imagination Technologies Ltd.
2 # SPDX-License-Identifier: MIT
3
4 from mako.template import Template, exceptions
5 from pco_pygen_common import *
6 from pco_ops import *
7 from pco_isa import *
8 from pco_map import *
9
10 template = """/*
11 * Copyright © 2024 Imagination Technologies Ltd.
12 *
13 * SPDX-License-Identifier: MIT
14 */
15
16 #ifndef PCO_COMMON_H
17 #define PCO_COMMON_H
18
19 /**
20 * \\file pco_common.h
21 *
22 * \\brief PCO common definitions.
23 */
24
25 #include "util/macros.h"
26
27 #include <stdbool.h>
28
29 #ifndef __OPENCL_VERSION__
30 #define __constant
31 #endif
32
33 /** Enums. */
34 % for enum in [enum for enum in enums.values() if enum.parent is None]:
35 #define _${enum.name.upper()}_COUNT ${enum.unique_count}U
36 enum ${enum.name} {
37 % for elem in enum.elems.values():
38 ${elem.cname} = ${hex(elem.value) if enum.is_bitset else elem.value},
39 % endfor
40 };
41
42 static inline
43 __constant const char *${enum.name}_str(uint64_t val) {
44 switch (val) {
45 % for elem in enum.elems.values():
46 % if elem.string is not None:
47 case ${elem.cname}:
48 return "${elem.string}";
49 % endif
50 % endfor
51
52 default:
53 break;
54 }
55
56 UNREACHABLE("");
57 }
58
59 % endfor
60 /** Enum validation. */
61 % for enum in enums.values():
62 static bool ${enum.name}_valid(uint64_t val)
63 {
64 % if enum.is_bitset:
65 return !(val & ~(${enum.valid}ULL));
66 % else:
67 return ${' || '.join([f'val == {val}' for val in enum.valid])};
68 % endif
69 }
70
71 % endfor
72 /** Bit set variants. */
73 % for bit_set in bit_sets.values():
74 #define _${bit_set.name.upper()}_VARIANT_COUNT ${len(bit_set.variants) + 1}U
75 enum ${bit_set.name}_variant {
76 ${bit_set.name.upper()}_NONE,
77 % for variant in bit_set.variants:
78 ${variant.cname},
79 % endfor
80 };
81
82 % endfor
83 #endif /* PCO_COMMON_H */"""
84
85 def main():
86 try:
87 print(Template(template).render(enums=enums, bit_sets=bit_sets))
88 except:
89 raise Exception(exceptions.text_error_template().render())
90
91 if __name__ == '__main__':
92 main()