pco, pygen: generate string representations of enum elements

Signed-off-by: Simon Perretta <simon.perretta@imgtec.com>
Acked-by: Frank Binns <frank.binns@imgtec.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/32258>
This commit is contained in:
Simon Perretta
2024-05-13 21:38:24 +01:00
committed by Marge Bot
parent f014a14e2c
commit 8b193b4dd2
3 changed files with 24 additions and 2 deletions
+17
View File
@@ -34,6 +34,23 @@ enum ${enum.name} {
% endfor
};
static inline
const char *${enum.name}_str(enum ${enum.name} val) {
switch (val) {
% for cname, _, string in enum.elems.values():
% if string is not None:
case ${cname}:
return "${string}";
% endif
% endfor
default:
break;
}
unreachable();
}
% endfor
/** Enum validation. */
% for enum in enums.values():
+2 -2
View File
@@ -4,11 +4,11 @@
from pco_pygen_common import *
OP_PHASE = enum_type('op_phase', [
('ctrl', 0),
('0', 0),
('ctrl', 0),
('1', 1),
('2', 2),
('2_pck', 2),
('2', 2),
('2_tst', 3),
('2_mov', 4),
('backend', 5),
+5
View File
@@ -87,9 +87,14 @@ def enum_type(name, elems, is_bitset=False, num_bits=None, *args, **kwargs):
assert isinstance(elem, str) and isinstance(value, int) and isinstance(string, str)
assert not num_bits or val_fits_in_bits(value, num_bits), f'Element "{elem}" in elem "{name}" with value "{value}" does not fit into {num_bits} bits.'
# Collect valid values, ensure that elements with repeated values only have one string set.
if is_bitset:
if (_valid_valmask & value) != 0:
string = None
_valid_valmask |= value
else:
if value in _valid_vals:
string = None
_valid_vals.add(value)
assert elem not in _elems.keys(), f'Duplicate element "{elem}" in enum "".'