From b2e4972339711a9576ec309ecdd4f42eb664c2f9 Mon Sep 17 00:00:00 2001 From: Christian Gmeiner Date: Mon, 4 Sep 2023 15:18:15 +0200 Subject: [PATCH] isaspec: Add BitSetEnumValue object There might be cases where you describe an enum in isaspec and want it to use for decoding but also for codegen with e.g. mako. Lets have a look at the following exmaple: ... In the decoding case we want that nothing gets displayed if #cond has the value of "0". For codegen with mako this could result in the following C code: enum PACKED cond { COND_ = 0, COND_GT = 1, ... }; What you really want is this: enum PACKED cond { COND_ALWAYS = 0, COND_GT = 1, ... }; To make this possible introduce BitSetEnumValue class which represents an isaspec xml enum. It holds the value, displayname and now a name. With the __str__ method the old behaviour is still intact. Signed-off-by: Christian Gmeiner Reviewed-by: Rob Clark Part-of: --- src/compiler/isaspec/isa.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/compiler/isaspec/isa.py b/src/compiler/isaspec/isa.py index 32defb8..dda0b7c 100644 --- a/src/compiler/isaspec/isa.py +++ b/src/compiler/isaspec/isa.py @@ -405,18 +405,36 @@ class BitSetTemplate(object): self.display = xml.text.strip() dbg("found template '{}: {}'".format(self.name, self.display)) +class BitSetEnumValue(object): + """Class that encapsulates an enum value + """ + def __init__(self, isa, xml): + self.isa = isa + self.displayname = xml.attrib['display'] + self.value = xml.attrib['val'] + self.name = xml.attrib.get('name') + + def __str__(self): + return self.displayname + + def get_name(self): + return self.name or self.displayname + + def get_value(self): + return self.value + class BitSetEnum(object): """Class that encapsulates an enum declaration """ def __init__(self, isa, xml): self.isa = isa self.name = xml.attrib['name'] + # Table mapping value to name - # TODO currently just mapping to 'display' name, but if we - # need more attributes then maybe need BitSetEnumValue? self.values = {} for value in xml.findall('value'): - self.values[value.attrib['val']] = value.attrib['display'] + v = BitSetEnumValue(self, value) + self.values[v.get_value()] = v def get_c_name(self): return 'enum_' + get_c_name(self.name) -- 2.7.4