2 import argparse
as _argparse
3 import dataclasses
as _dataclasses
6 from openpower
.decoder
.power_enums
import (
12 CROutSel
as _CROutSel
,
18 from openpower
.consts
import SVP64MODE
as _SVP64MODE
19 from openpower
.decoder
.power_svp64
import SVP64RM
as _SVP64RM
24 " * this file is auto-generated, do not edit",
25 " * https://git.libre-soc.org/?p=openpower-isa.git;a=blob;f=src/openpower/sv/sv_binutils.py",
26 " * part of Libre-SOC, sponsored by NLnet",
32 return map(lambda string
: (" " + string
), strings
)
38 def c_decl(self
, name
):
42 def c_value(self
, prefix
="", suffix
=""):
47 def c_var(self
, name
):
51 class EnumMeta(_enum
.EnumMeta
):
52 def __call__(metacls
, name
, entries
, tag
=None, **kwargs
):
53 if isinstance(entries
, type) and issubclass(entries
, _enum
.Enum
):
54 entries
= dict(entries
.__members
__)
55 if isinstance(entries
, dict):
56 entries
= tuple(entries
.items())
58 tag
= f
"svp64_{name.lower()}"
60 cls
= super().__call
__(value
=name
, names
=entries
, **kwargs
)
69 class Enum(CType
, _enum
.Enum
, metaclass
=EnumMeta
):
72 yield f
"enum {cls.c_tag} {{"
74 yield from indent(item
.c_value(suffix
=","))
77 def c_value(self
, prefix
="", suffix
=""):
78 yield f
"{prefix}{self.__class__.c_tag.upper()}_{self.name.upper()}{suffix}"
82 yield f
"enum {cls.c_tag} {name}"
85 In1Sel
= Enum("In1Sel", _In1Sel
)
86 In2Sel
= Enum("In2Sel", _In2Sel
)
87 In3Sel
= Enum("In3Sel", _In3Sel
)
88 OutSel
= Enum("OutSel", _OutSel
)
89 CRInSel
= Enum("CRInSel", _CRInSel
)
90 CROutSel
= Enum("CROutSel", _CROutSel
)
91 SVPType
= Enum("SVPType", _SVPtype
)
92 SVEType
= Enum("SVEType", _SVEtype
)
93 SVEXTRA
= Enum("SVEXTRA", _SVEXTRA
)
96 class Constant(CType
, _enum
.Enum
, metaclass
=EnumMeta
):
99 yield f
"/* {cls.c_tag.upper()} constants */"
100 for (key
, item
) in cls
.__members
__.items():
101 key
= f
"{cls.c_tag.upper()}_{key.upper()}"
102 value
= f
"0x{item.value:08x}U"
103 yield f
"#define {key} {value}"
105 def c_value(self
, prefix
="", suffix
=""):
106 yield f
"{prefix}{self.__class__.c_tag.upper()}_{self.name.upper()}{suffix}"
109 Mode
= Constant("Mode", _SVP64MODE
)
113 def __init__(self
, value
, mask
, bits
):
118 return super().__init
__()
133 fmt
= f
"{{value:0{self.bits}b}}:{{mask:0{self.bits}b}}"
134 return fmt
.format(value
=self
.value
, mask
=self
.mask
)
136 def __lt__(self
, other
):
137 if not isinstance(other
, self
.__class
__):
138 return NotImplemented
140 return self
.__value
< other
.__value
144 yield f
"struct svp64_opcode {{"
151 def c_value(self
, prefix
="", suffix
=""):
154 f
".value = UINT32_C(0x{self.value:08X}),",
155 f
".mask = UINT32_C(0x{self.mask:08X}),",
160 def c_var(cls
, name
):
161 yield f
"struct svp64_opcode {name}"
164 class IntegerOpcode(Opcode
):
165 def __init__(self
, integer
):
166 value
= int(integer
, 0)
167 bits
= max(1, value
.bit_length())
168 mask
= int(("1" * bits
), 2)
170 return super().__init
__(value
=value
, mask
=mask
, bits
=bits
)
173 class PatternOpcode(Opcode
):
174 def __init__(self
, pattern
):
179 value |
= (bit
== "1")
186 return super().__init
__(value
=value
, mask
=mask
, bits
=bits
)
189 class Name(CType
, str):
191 escaped
= self
.replace("\"", "\\\"")
192 return f
"\"{escaped}\""
194 def c_value(self
, prefix
="", suffix
=""):
195 yield f
"{prefix}{self!r}{suffix}"
198 def c_var(cls
, name
):
199 yield f
"const char *{name}"
202 @_dataclasses.dataclass(eq
=True, frozen
=True)
224 yield f
"struct svp64_record {{"
225 for field
in _dataclasses
.fields(cls
):
226 bits
= len(field
.type).bit_length()
227 yield from indent([f
"uint64_t {field.name} : {bits};"])
229 bits_rsvd
= (64 - (bits_all
% 64))
231 yield from indent([f
"uint64_t : {bits_rsvd};"])
234 def c_value(self
, prefix
="", suffix
=""):
236 for field
in _dataclasses
.fields(self
):
238 attr
= getattr(self
, name
)
239 yield from indent(attr
.c_value(prefix
=f
".{name} = ", suffix
=","))
243 def c_var(cls
, name
):
244 yield f
"struct svp64_record {name}"
247 @_dataclasses.dataclass(eq
=True, frozen
=True)
252 def __lt__(self
, other
):
253 if not isinstance(other
, self
.__class
__):
254 return NotImplemented
256 return self
.name
< other
.name
260 yield f
"struct svp64_entry {{"
261 for field
in _dataclasses
.fields(cls
):
262 yield from indent(field
.type.c_var(name
=f
"{field.name};"))
265 def c_value(self
, prefix
="", suffix
=""):
267 for field
in _dataclasses
.fields(self
):
269 attr
= getattr(self
, name
)
270 yield from indent(attr
.c_value(prefix
=f
".{name} = ", suffix
=","))
274 def c_var(cls
, name
):
275 yield f
"struct svp64_entry {name}"
278 class Codegen(_enum
.Enum
):
279 PPC_SVP64_H
= _enum
.auto()
280 PPC_SVP64_OPC_C
= _enum
.auto()
283 def _missing_(cls
, value
):
285 "ppc-svp64.h": Codegen
.PPC_SVP64_H
,
286 "ppc-svp64-opc.c": Codegen
.PPC_SVP64_OPC_C
,
291 Codegen
.PPC_SVP64_H
: "ppc-svp64.h",
292 Codegen
.PPC_SVP64_OPC_C
: "ppc-svp64-opc.c",
295 def generate(self
, entries
):
296 def ppc_svp64_h(entries
):
297 yield from DISCLAIMER
300 yield f
"#ifndef {self.name}"
301 yield f
"#define {self.name}"
304 yield "#include <stdint.h>"
307 yield "#ifdef __cplusplus"
308 yield "extern \"C\" {"
313 In1Sel
, In2Sel
, In3Sel
, OutSel
,
315 SVPType
, SVEType
, SVEXTRA
,
319 yield from enum
.c_decl()
322 yield from Record
.c_decl()
325 for name
in ("in1", "in2", "in3", "out", "out2", "cr_in", "cr_out"):
326 yield "unsigned char"
327 yield f
"svp64_record_{name}_opsel(const struct svp64_record *record);"
330 yield from Entry
.c_decl()
333 yield "extern const struct svp64_entry svp64_entries[];"
334 yield "extern const unsigned int svp64_num_entries;"
337 yield f
"#define SVP64_NAME_MAX {max(map(lambda entry: len(entry.name), entries))}"
340 yield "#ifdef __cplusplus"
345 yield f
"#endif /* {self.name} */"
348 def ppc_svp64_opc_c(entries
):
349 yield from DISCLAIMER
352 yield "#include \"opcode/ppc-svp64.h\""
355 def opsel(enum
, name
, table
):
356 sep
= (max(map(len, list(table
.values()) + ["UNUSED"])) + 1)
357 c_tag
= f
"svp64_{enum.__name__.lower()}"
358 yield "unsigned char"
359 yield f
"svp64_record_{name}_opsel(const struct svp64_record *record)"
361 yield from indent(["static const unsigned char table[] = {"])
363 value
= table
.get(key
, "UNUSED")
364 c_value
= f
"{c_tag.upper()}_{key.name.upper()}"
365 yield from indent(indent([f
"{value:{sep}}, /* {c_value} */"]))
366 yield from indent(["};"])
368 yield from indent([f
"return table[record->{name}];"])
372 yield from opsel(In1Sel
, "in1", {
374 In1Sel
.RA_OR_ZERO
: "RA",
380 yield from opsel(In2Sel
, "in2", {
386 yield from opsel(In3Sel
, "in3", {
394 for name
in ("out", "out2"):
395 yield from opsel(OutSel
, name
, {
399 OutSel
.RT_OR_ZERO
: "RT",
403 yield from opsel(CRInSel
, "cr_in", {
407 CRInSel
.WHOLE_REG
: "FXM",
409 yield from opsel(CROutSel
, "cr_out", {
412 CROutSel
.WHOLE_REG
: "FXM",
415 yield "const struct svp64_entry svp64_entries[] = {"
416 for (index
, entry
) in enumerate(entries
):
417 yield from indent(entry
.c_value(prefix
=f
"[{index}] = ", suffix
=","))
421 yield "const unsigned int svp64_num_entries = \\"
422 yield " sizeof (svp64_entries) / sizeof (svp64_entries[0]);"
426 Codegen
.PPC_SVP64_H
: ppc_svp64_h
,
427 Codegen
.PPC_SVP64_OPC_C
: ppc_svp64_opc_c
,
432 FIELDS
= {field
.name
:field
.type for field
in _dataclasses
.fields(Record
)}
433 FIELDS
.update({field
.name
:field
.type for field
in _dataclasses
.fields(Entry
)})
435 def parse(path
, opcode_cls
):
438 def name_filter(name
):
439 if name
.startswith("l") and name
.endswith("br"):
441 if name
in {"mcrxr", "mcrxrx", "darn"}:
443 if name
in {"bctar", "bcctr"}:
447 if name
in {"setvl"}:
456 def item_mapper(item
):
458 key
= key
.lower().replace(" ", "_")
459 cls
= FIELDS
.get(key
, object)
460 if not isinstance(value
, cls
):
461 if issubclass(cls
, _enum
.Enum
):
462 value
= {item
.name
:item
for item
in cls
}[value
]
467 def item_filter(item
):
469 return (key
in FIELDS
)
471 for record
in ISA
.get_svp64_csv(path
):
472 opcode
= opcode_cls(record
.pop("opcode"))
473 names
= record
.pop("comment").split("=")[-1].split("/")
474 names
= set(filter(name_filter
, names
))
476 rc
= _RC
[record
["rc"] if record
["rc"] else "NONE"]
478 names
.update({f
"{name}." for name
in names
})
479 record
= dict(filter(item_filter
, map(item_mapper
, record
.items())))
480 for name
in map(Name
, names
):
481 yield Entry(name
=name
, record
=Record(**record
))
487 "minor_19.csv": IntegerOpcode
,
488 "minor_30.csv": IntegerOpcode
,
489 "minor_31.csv": IntegerOpcode
,
490 "minor_58.csv": IntegerOpcode
,
491 "minor_62.csv": IntegerOpcode
,
492 "minor_22.csv": IntegerOpcode
,
493 "minor_5.csv": PatternOpcode
,
494 "minor_63.csv": PatternOpcode
,
495 "minor_59.csv": PatternOpcode
,
496 "major.csv": IntegerOpcode
,
497 "extra.csv": PatternOpcode
,
499 for (path
, opcode_cls
) in table
.items():
500 entries
.extend(parse(path
, opcode_cls
))
501 entries
= sorted(frozenset(entries
))
503 for line
in codegen
.generate(entries
):
507 if __name__
== "__main__":
508 parser
= _argparse
.ArgumentParser()
509 parser
.add_argument("codegen", type=Codegen
, choices
=Codegen
, help="code generator")
511 args
= vars(parser
.parse_args())