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