From 8b193b4dd205cc716907f1ca9773de9372175496 Mon Sep 17 00:00:00 2001 From: Simon Perretta Date: Mon, 13 May 2024 21:38:24 +0100 Subject: [PATCH] pco, pygen: generate string representations of enum elements Signed-off-by: Simon Perretta Acked-by: Frank Binns Part-of: --- src/imagination/pco/pco_common.h.py | 17 +++++++++++++++++ src/imagination/pco/pco_isa.py | 4 ++-- src/imagination/pco/pco_pygen_common.py | 5 +++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/imagination/pco/pco_common.h.py b/src/imagination/pco/pco_common.h.py index b8d271a5760..1fbea61dff8 100644 --- a/src/imagination/pco/pco_common.h.py +++ b/src/imagination/pco/pco_common.h.py @@ -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(): diff --git a/src/imagination/pco/pco_isa.py b/src/imagination/pco/pco_isa.py index 57ece76cfed..7ae38fb6301 100644 --- a/src/imagination/pco/pco_isa.py +++ b/src/imagination/pco/pco_isa.py @@ -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), diff --git a/src/imagination/pco/pco_pygen_common.py b/src/imagination/pco/pco_pygen_common.py index de95ffe983b..11e9248ac12 100644 --- a/src/imagination/pco/pco_pygen_common.py +++ b/src/imagination/pco/pco_pygen_common.py @@ -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 "".'