Debian Codegraph @

src/imagination/pco/pco_isa.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_isa import *
6
7 template = """/*
8 * Copyright © 2024 Imagination Technologies Ltd.
9 *
10 * SPDX-License-Identifier: MIT
11 */
12
13 #ifndef PCO_ISA_H
14 #define PCO_ISA_H
15
16 /**
17 * \\file pco_isa.h
18 *
19 * \\brief PCO ISA definitions.
20 */
21
22 #include "pco_common.h"
23 #include "util/macros.h"
24
25 #include <stdbool.h>
26
27 % for _bit_set_name, bit_set in bit_sets.items():
28 /** ${_bit_set_name} */
29 enum ${bit_set.name}_field {
30 % for field in bit_set.fields.values():
31 ${field.cname},
32 % endfor
33 };
34
35 static unsigned ${bit_set.name}_bytes(enum ${bit_set.name}_variant variant)
36 {
37 switch (variant) {
38 case ${bit_set.name.upper()}_NONE: return 0;
39 % for variant in bit_set.variants:
40 case ${variant.cname}: return ${variant.bytes};
41 % endfor
42 default: break;
43 }
44
45 UNREACHABLE("");
46 }
47
48 static unsigned ${bit_set.name}_encode_field(uint8_t *bin, enum ${bit_set.name}_field field, uint64_t val)
49 {
50 uint64_t enc_val;
51
52 switch (field) {
53 % for field in bit_set.fields.values():
54 case ${field.cname}:
55 % if field.field_type.check is not None:
56 assert(${field.field_type.check.format('val')});
57 % endif
58 % if field.reserved is not None:
59 assert(val == ${field.reserved});
60 % endif
61 enc_val = ${field.field_type.encode.format('val') if field.field_type.encode is not None else 'val'};
62 assert(${field.validate.format('enc_val')});
63 % for encoding in field.encoding:
64 ${encoding.clear.format('bin')};
65 ${encoding.set.format('bin', 'enc_val')};
66 % endfor
67 return ${field.encoded_bits};
68
69 % endfor
70 default:
71 break;
72 }
73
74 UNREACHABLE("");
75 }
76
77 % for bit_struct in bit_set.bit_structs.values():
78 % if len(bit_struct.struct_fields) > 0:
79 struct ${bit_struct.name} {
80 % for struct_field in bit_struct.struct_fields.values():
81 ${struct_field.type.name} ${struct_field.field} : ${struct_field.bits};
82 % endfor
83 };
84 % endif
85
86 % if len(bit_struct.struct_fields) > 0:
87 #define ${bit_struct.name}_encode(bin, ...) _${bit_struct.name}_encode(bin, (const struct ${bit_struct.name}){0, ##__VA_ARGS__})
88 static unsigned _${bit_struct.name}_encode(uint8_t *bin, const struct ${bit_struct.name} s)
89 % else:
90 static unsigned ${bit_struct.name}_encode(uint8_t *bin)
91 % endif
92 {
93 unsigned bits_encoded = 0;
94
95 % for encode_field in bit_struct.encode_fields:
96 bits_encoded += ${bit_set.name}_encode_field(bin, ${encode_field.name}, ${encode_field.value});
97 % endfor
98
99 assert(!(bits_encoded % 8));
100 return bits_encoded / 8;
101 }
102
103 % endfor
104 % endfor
105 #endif /* PCO_ISA_H */"""
106
107 def main():
108 try:
109 print(Template(template).render(bit_sets=bit_sets))
110 except:
111 raise Exception(exceptions.text_error_template().render())
112
113 if __name__ == '__main__':
114 main()