1 import argparse
as _argparse
2 import dataclasses
as _dataclasses
4 import functools
as _functools
7 from openpower
.decoder
.power_enums
import (
8 FPTRANS_INSNS
as _FPTRANS_INSNS
,
9 find_wiki_dir
as _find_wiki_dir
,
11 from openpower
.decoder
.power_insn
import (
12 Database
as _Database
,
13 StaticOperand
as _StaticOperand
,
14 WordInstruction
as _WordInstruction
,
19 fptrans
= tuple(_FPTRANS_INSNS
)
20 fptrans_Rc
= tuple(map(lambda name
: f
"{name}.", fptrans
))
22 def fptrans_match(insn
):
23 return ((insn
.name
in fptrans
) or (insn
.name
in fptrans_Rc
))
25 for record
in filter(fptrans_match
, db
):
26 if len(record
.opcodes
) > 1:
27 raise NotImplementedError(record
.opcodes
)
33 operands
= entry
.dynamic_operands
34 operands
= ", ".join(operand
.name
for operand
in operands
)
37 f
"XRC({entry.PO},{entry.XO},{entry.Rc})",
43 return f
"{{{string}}},"
46 def asm(record
, binutils
=False, regex
=False):
47 operands
= tuple(record
.dynamic_operands
)
48 for (idx
, operand
) in enumerate(operands
):
51 if binutils
and each
.name
in ("FRT", "FRA", "FRB"):
53 elif binutils
and each
.name
in ("RT", "RA", "RB"):
57 value
= str((1 << len(operand
.span
)) - 1)
58 if binutils
and operand
.name
in ("FRT", "FRA", "FRB"):
60 elif binutils
and operand
.name
in ("RT", "RA", "RB"):
63 sep
= "\s+" if regex
else " "
64 yield f
"{record.name}{sep}{','.join(values)}"
67 def dis(record
, binutils
=True):
71 asm_plain
= tuple(asm(record
, binutils
=binutils
, regex
=False))
72 asm_regex
= tuple(asm(record
, binutils
=binutils
, regex
=True))
73 for (idx
, dynamic_operand
) in enumerate(record
.dynamic_operands
):
74 insn
= _WordInstruction
.integer(value
=0)
75 for static_operand
in record
.static_operands
:
76 insn
[static_operand
.span
] = static_operand
.value
77 span
= dynamic_operand
.span
78 insn
[span
] = ((1 << len(span
)) - 1)
80 big
= " ".join(map(objdump
, insn
.bytes(byteorder
="big")))
81 little
= " ".join(map(objdump
, insn
.bytes(byteorder
="little")))
82 yield f
".*:\s+({big}|{little})\s+{asm_regex[idx]}"
87 class Mode(_enum
.Enum
):
93 return self
.name
.lower()
96 if __name__
== "__main__":
97 parser
= _argparse
.ArgumentParser()
98 parser
.add_argument("mode", type=Mode
, choices
=Mode
)
99 args
= dict(vars(parser
.parse_args()))
102 db
= _Database(_find_wiki_dir())
103 entries
= sorted(collect(db
))
106 Mode
.OPCODES
: opcodes
,
111 print("#as: -mlibresoc")
112 print("#objdump: -dr -Mlibresoc")
114 print(".*: file format .*")
117 print("Disassembly of section \\.text:")
118 print("0+ <\.text>:")
120 if mode
in {Mode
.ASM
, Mode
.DIS
}:
121 for subgenerator
in map(generator
, entries
):
122 for line
in subgenerator
:
125 for line
in map(generator
, entries
):