LLVM 22.0.0git
SparcDisassembler.cpp
Go to the documentation of this file.
1//===- SparcDisassembler.cpp - Disassembler for Sparc -----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is part of the Sparc Disassembler.
10//
11//===----------------------------------------------------------------------===//
12
15#include "llvm/MC/MCAsmInfo.h"
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCDecoder.h"
20#include "llvm/MC/MCInst.h"
23#include "llvm/Support/Endian.h"
24
25using namespace llvm;
26using namespace llvm::MCD;
27
28#define DEBUG_TYPE "sparc-disassembler"
29
31
32namespace {
33
34/// A disassembler class for Sparc.
35class SparcDisassembler : public MCDisassembler {
36public:
37 SparcDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
38 : MCDisassembler(STI, Ctx) {}
39 virtual ~SparcDisassembler() = default;
40
41 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
42 ArrayRef<uint8_t> Bytes, uint64_t Address,
43 raw_ostream &CStream) const override;
44};
45}
46
48 const MCSubtargetInfo &STI,
49 MCContext &Ctx) {
50 return new SparcDisassembler(STI, Ctx);
51}
52
63
64// clang-format off
65static constexpr unsigned IntRegDecoderTable[] = {
66 SP::G0, SP::G1, SP::G2, SP::G3,
67 SP::G4, SP::G5, SP::G6, SP::G7,
68 SP::O0, SP::O1, SP::O2, SP::O3,
69 SP::O4, SP::O5, SP::O6, SP::O7,
70 SP::L0, SP::L1, SP::L2, SP::L3,
71 SP::L4, SP::L5, SP::L6, SP::L7,
72 SP::I0, SP::I1, SP::I2, SP::I3,
73 SP::I4, SP::I5, SP::I6, SP::I7 };
74
75static constexpr unsigned FPRegDecoderTable[] = {
76 SP::F0, SP::F1, SP::F2, SP::F3,
77 SP::F4, SP::F5, SP::F6, SP::F7,
78 SP::F8, SP::F9, SP::F10, SP::F11,
79 SP::F12, SP::F13, SP::F14, SP::F15,
80 SP::F16, SP::F17, SP::F18, SP::F19,
81 SP::F20, SP::F21, SP::F22, SP::F23,
82 SP::F24, SP::F25, SP::F26, SP::F27,
83 SP::F28, SP::F29, SP::F30, SP::F31 };
84
85static constexpr unsigned DFPRegDecoderTable[] = {
86 SP::D0, SP::D16, SP::D1, SP::D17,
87 SP::D2, SP::D18, SP::D3, SP::D19,
88 SP::D4, SP::D20, SP::D5, SP::D21,
89 SP::D6, SP::D22, SP::D7, SP::D23,
90 SP::D8, SP::D24, SP::D9, SP::D25,
91 SP::D10, SP::D26, SP::D11, SP::D27,
92 SP::D12, SP::D28, SP::D13, SP::D29,
93 SP::D14, SP::D30, SP::D15, SP::D31 };
94
95static constexpr unsigned QFPRegDecoderTable[] = {
96 SP::Q0, SP::Q8, ~0U, ~0U,
97 SP::Q1, SP::Q9, ~0U, ~0U,
98 SP::Q2, SP::Q10, ~0U, ~0U,
99 SP::Q3, SP::Q11, ~0U, ~0U,
100 SP::Q4, SP::Q12, ~0U, ~0U,
101 SP::Q5, SP::Q13, ~0U, ~0U,
102 SP::Q6, SP::Q14, ~0U, ~0U,
103 SP::Q7, SP::Q15, ~0U, ~0U } ;
104
105static constexpr unsigned FCCRegDecoderTable[] = {
106 SP::FCC0, SP::FCC1, SP::FCC2, SP::FCC3 };
107
108static constexpr unsigned ASRRegDecoderTable[] = {
109 SP::Y, SP::ASR1, SP::ASR2, SP::ASR3, SP::ASR4, SP::ASR5, SP::ASR6,
110 SP::ASR7, SP::ASR8, SP::ASR9, SP::ASR10, SP::ASR11, SP::ASR12, SP::ASR13,
111 SP::ASR14, SP::ASR15, SP::ASR16, SP::ASR17, SP::ASR18, SP::ASR19, SP::ASR20,
112 SP::ASR21, SP::ASR22, SP::ASR23, SP::ASR24, SP::ASR25, SP::ASR26, SP::ASR27,
113 SP::ASR28, SP::ASR29, SP::ASR30, SP::ASR31};
114
115static constexpr unsigned PRRegDecoderTable[] = {
116 SP::TPC, SP::TNPC, SP::TSTATE, SP::TT, SP::TICK,
117 SP::TBA, SP::PSTATE, SP::TL, SP::PIL, SP::CWP,
118 SP::CANSAVE, SP::CANRESTORE, SP::CLEANWIN, SP::OTHERWIN, SP::WSTATE};
119
120static constexpr uint16_t IntPairDecoderTable[] = {
121 SP::G0_G1, SP::G2_G3, SP::G4_G5, SP::G6_G7,
122 SP::O0_O1, SP::O2_O3, SP::O4_O5, SP::O6_O7,
123 SP::L0_L1, SP::L2_L3, SP::L4_L5, SP::L6_L7,
124 SP::I0_I1, SP::I2_I3, SP::I4_I5, SP::I6_I7,
125};
126
127static constexpr unsigned CPRegDecoderTable[] = {
128 SP::C0, SP::C1, SP::C2, SP::C3,
129 SP::C4, SP::C5, SP::C6, SP::C7,
130 SP::C8, SP::C9, SP::C10, SP::C11,
131 SP::C12, SP::C13, SP::C14, SP::C15,
132 SP::C16, SP::C17, SP::C18, SP::C19,
133 SP::C20, SP::C21, SP::C22, SP::C23,
134 SP::C24, SP::C25, SP::C26, SP::C27,
135 SP::C28, SP::C29, SP::C30, SP::C31
136};
137
138static constexpr uint16_t CPPairDecoderTable[] = {
139 SP::C0_C1, SP::C2_C3, SP::C4_C5, SP::C6_C7,
140 SP::C8_C9, SP::C10_C11, SP::C12_C13, SP::C14_C15,
141 SP::C16_C17, SP::C18_C19, SP::C20_C21, SP::C22_C23,
142 SP::C24_C25, SP::C26_C27, SP::C28_C29, SP::C30_C31
143};
144// clang-format on
145
146static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
147 uint64_t Address,
148 const MCDisassembler *Decoder) {
149 if (RegNo >= std::size(IntRegDecoderTable))
151 unsigned Reg = IntRegDecoderTable[RegNo];
154}
155
156static DecodeStatus DecodeI64RegsRegisterClass(MCInst &Inst, unsigned RegNo,
157 uint64_t Address,
158 const MCDisassembler *Decoder) {
159 return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
160}
161
162// This is used for the type "ptr_rc", which is either IntRegs or I64Regs
163// depending on SparcRegisterInfo::getPointerRegClass.
164static DecodeStatus DecodePointerLikeRegClass0(MCInst &Inst, unsigned RegNo,
165 uint64_t Address,
166 const MCDisassembler *Decoder) {
167 return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
168}
169
170static DecodeStatus DecodeFPRegsRegisterClass(MCInst &Inst, unsigned RegNo,
171 uint64_t Address,
172 const MCDisassembler *Decoder) {
173 if (RegNo >= std::size(FPRegDecoderTable))
175 unsigned Reg = FPRegDecoderTable[RegNo];
178}
179
180static DecodeStatus DecodeDFPRegsRegisterClass(MCInst &Inst, unsigned RegNo,
181 uint64_t Address,
182 const MCDisassembler *Decoder) {
183 if (RegNo >= std::size(DFPRegDecoderTable))
185 unsigned Reg = DFPRegDecoderTable[RegNo];
188}
189
190static DecodeStatus DecodeQFPRegsRegisterClass(MCInst &Inst, unsigned RegNo,
191 uint64_t Address,
192 const MCDisassembler *Decoder) {
193 if (RegNo >= std::size(QFPRegDecoderTable))
195
196 unsigned Reg = QFPRegDecoderTable[RegNo];
197 if (Reg == ~0U)
201}
202
203static DecodeStatus
204DecodeCoprocRegsRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address,
205 const MCDisassembler *Decoder) {
206 if (RegNo >= std::size(CPRegDecoderTable))
208 unsigned Reg = CPRegDecoderTable[RegNo];
211}
212
213static DecodeStatus DecodeFCCRegsRegisterClass(MCInst &Inst, unsigned RegNo,
214 uint64_t Address,
215 const MCDisassembler *Decoder) {
216 if (RegNo >= std::size(FCCRegDecoderTable))
220}
221
222static DecodeStatus DecodeASRRegsRegisterClass(MCInst &Inst, unsigned RegNo,
223 uint64_t Address,
224 const MCDisassembler *Decoder) {
225 if (RegNo >= std::size(ASRRegDecoderTable))
229}
230
231static DecodeStatus DecodePRRegsRegisterClass(MCInst &Inst, unsigned RegNo,
232 uint64_t Address,
233 const MCDisassembler *Decoder) {
234 if (RegNo >= std::size(PRRegDecoderTable))
238}
239
240static DecodeStatus DecodeIntPairRegisterClass(MCInst &Inst, unsigned RegNo,
241 uint64_t Address,
242 const MCDisassembler *Decoder) {
244
245 if ((RegNo & 1))
247
248 RegNo = RegNo / 2;
249 if (RegNo >= std::size(IntPairDecoderTable))
251
252 unsigned RegisterPair = IntPairDecoderTable[RegNo];
253 Inst.addOperand(MCOperand::createReg(RegisterPair));
254 return S;
255}
256
257static DecodeStatus
258DecodeCoprocPairRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address,
259 const MCDisassembler *Decoder) {
260 RegNo = RegNo / 2;
261 if (RegNo >= std::size(CPPairDecoderTable))
263
264 unsigned RegisterPair = CPPairDecoderTable[RegNo];
265 Inst.addOperand(MCOperand::createReg(RegisterPair));
267}
268
269static bool tryAddingSymbolicOperand(int64_t Value, bool isBranch,
270 uint64_t Address, uint64_t Offset,
271 uint64_t Width, MCInst &MI,
272 const MCDisassembler *Decoder) {
273 return Decoder->tryAddingSymbolicOperand(MI, Value, Address, isBranch, Offset,
274 Width, /*InstSize=*/4);
275}
276
277static DecodeStatus DecodeCall(MCInst &MI, unsigned insn, uint64_t Address,
278 const MCDisassembler *Decoder) {
279 int64_t CallOffset = SignExtend64(fieldFromInstruction(insn, 0, 30), 30) * 4;
280 if (!tryAddingSymbolicOperand(Address + CallOffset, false, Address, 0, 30, MI,
281 Decoder))
282 MI.addOperand(MCOperand::createImm(CallOffset));
284}
285
286static DecodeStatus DecodeSIMM5(MCInst &MI, unsigned insn, uint64_t Address,
287 const MCDisassembler *Decoder) {
288 assert(isUInt<5>(insn));
289 MI.addOperand(MCOperand::createImm(SignExtend64<5>(insn)));
291}
292
293static DecodeStatus DecodeSIMM13(MCInst &MI, unsigned insn, uint64_t Address,
294 const MCDisassembler *Decoder) {
295 assert(isUInt<13>(insn));
296 MI.addOperand(MCOperand::createImm(SignExtend64<13>(insn)));
298}
299
300template <unsigned N>
302 const MCDisassembler *Decoder) {
303 int64_t BranchOffset = SignExtend64(ImmVal, N) * 4;
304 if (!tryAddingSymbolicOperand(Address + BranchOffset, true, Address, 0, N, MI,
305 Decoder))
306 MI.addOperand(MCOperand::createImm(BranchOffset));
308}
309
310#include "SparcGenDisassemblerTables.inc"
311
312/// Read four bytes from the ArrayRef and return 32 bit word.
314 uint64_t &Size, uint32_t &Insn,
315 bool IsLittleEndian) {
316 // We want to read exactly 4 Bytes of data.
317 if (Bytes.size() < 4) {
318 Size = 0;
320 }
321
322 Size = 4;
324 Bytes.data(), IsLittleEndian ? endianness::little : endianness::big);
326}
327
328DecodeStatus SparcDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
329 ArrayRef<uint8_t> Bytes,
330 uint64_t Address,
331 raw_ostream &CStream) const {
332 uint32_t Insn;
333 bool isLittleEndian = getContext().getAsmInfo()->isLittleEndian();
335 readInstruction32(Bytes, Address, Size, Insn, isLittleEndian);
336 if (Result == MCDisassembler::Fail)
338
339 // Calling the auto-generated decoder function.
340
341 if (STI.hasFeature(Sparc::FeatureV9))
342 Result = decodeInstruction(DecoderTableSparcV932, Instr, Insn, Address,
343 this, STI);
344 else
345 Result = decodeInstruction(DecoderTableSparcV832, Instr, Insn, Address, this, STI);
346
347 if (Result != MCDisassembler::Fail)
348 return Result;
349
350 Result =
351 decodeInstruction(DecoderTableSparc32, Instr, Insn, Address, this, STI);
352
353 return Result;
354}
MCDisassembler::DecodeStatus DecodeStatus
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool readInstruction32(ArrayRef< uint8_t > Bytes, uint64_t Address, uint64_t &Size, uint32_t &Insn)
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
IRTranslator LLVM IR MI
Register Reg
#define T
#define DecodePointerLikeRegClass0
static bool isBranch(unsigned Opcode)
static DecodeStatus DecodeFPRegsRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodePRRegsRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address, const MCDisassembler *Decoder)
static constexpr unsigned FCCRegDecoderTable[]
static DecodeStatus DecodeCall(MCInst &MI, unsigned insn, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeDFPRegsRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeDisp(MCInst &MI, uint32_t ImmVal, uint64_t Address, const MCDisassembler *Decoder)
static constexpr unsigned QFPRegDecoderTable[]
static MCDisassembler * createSparcDisassembler(const Target &T, const MCSubtargetInfo &STI, MCContext &Ctx)
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSparcDisassembler()
static DecodeStatus DecodeCoprocPairRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeSIMM5(MCInst &MI, unsigned insn, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeIntPairRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address, const MCDisassembler *Decoder)
static constexpr unsigned CPRegDecoderTable[]
static constexpr uint16_t CPPairDecoderTable[]
static constexpr unsigned PRRegDecoderTable[]
static DecodeStatus DecodeSIMM13(MCInst &MI, unsigned insn, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus readInstruction32(ArrayRef< uint8_t > Bytes, uint64_t Address, uint64_t &Size, uint32_t &Insn, bool IsLittleEndian)
Read four bytes from the ArrayRef and return 32 bit word.
static DecodeStatus DecodeFCCRegsRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeCoprocRegsRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address, const MCDisassembler *Decoder)
static constexpr uint16_t IntPairDecoderTable[]
static bool tryAddingSymbolicOperand(int64_t Value, bool isBranch, uint64_t Address, uint64_t Offset, uint64_t Width, MCInst &MI, const MCDisassembler *Decoder)
static DecodeStatus DecodeI64RegsRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address, const MCDisassembler *Decoder)
static constexpr unsigned IntRegDecoderTable[]
static DecodeStatus DecodeASRRegsRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address, const MCDisassembler *Decoder)
static constexpr unsigned DFPRegDecoderTable[]
static constexpr unsigned FPRegDecoderTable[]
static constexpr unsigned ASRRegDecoderTable[]
static DecodeStatus DecodeQFPRegsRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address, const MCDisassembler *Decoder)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
const T * data() const
Definition ArrayRef.h:144
Context object for machine code objects.
Definition MCContext.h:83
Superclass for all disassemblers.
bool tryAddingSymbolicOperand(MCInst &Inst, int64_t Value, uint64_t Address, bool IsBranch, uint64_t Offset, uint64_t OpSize, uint64_t InstSize) const
DecodeStatus
Ternary decode status.
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
void addOperand(const MCOperand Op)
Definition MCInst.h:215
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
Generic base class for all target subtargets.
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
std::enable_if_t< std::is_integral_v< IntType >, IntType > fieldFromInstruction(const IntType &Insn, unsigned StartBit, unsigned NumBits)
Definition MCDecoder.h:37
Context & getContext() const
Definition BasicBlock.h:99
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition Endian.h:58
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
Target & getTheSparcTarget()
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:198
Target & getTheSparcV9Target()
Target & getTheSparcelTarget()
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:577
#define N
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.