LLVM 24.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"
24#include "llvm/Support/Endian.h"
25
26using namespace llvm;
27using namespace llvm::MCD;
28
29#define DEBUG_TYPE "sparc-disassembler"
30
32
33namespace {
34
35/// A disassembler class for Sparc.
36class SparcDisassembler : public MCDisassembler {
37public:
38 SparcDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
39 : MCDisassembler(STI, Ctx) {}
40 ~SparcDisassembler() override = default;
41
42 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
43 ArrayRef<uint8_t> Bytes, uint64_t Address,
44 raw_ostream &CStream) const override;
45};
46}
47
49 const MCSubtargetInfo &STI,
50 MCContext &Ctx) {
51 return new SparcDisassembler(STI, Ctx);
52}
53
64
65// clang-format off
66static constexpr unsigned IntRegDecoderTable[] = {
67 SP::G0, SP::G1, SP::G2, SP::G3,
68 SP::G4, SP::G5, SP::G6, SP::G7,
69 SP::O0, SP::O1, SP::O2, SP::O3,
70 SP::O4, SP::O5, SP::O6, SP::O7,
71 SP::L0, SP::L1, SP::L2, SP::L3,
72 SP::L4, SP::L5, SP::L6, SP::L7,
73 SP::I0, SP::I1, SP::I2, SP::I3,
74 SP::I4, SP::I5, SP::I6, SP::I7 };
75
76static constexpr unsigned FPRegDecoderTable[] = {
77 SP::F0, SP::F1, SP::F2, SP::F3,
78 SP::F4, SP::F5, SP::F6, SP::F7,
79 SP::F8, SP::F9, SP::F10, SP::F11,
80 SP::F12, SP::F13, SP::F14, SP::F15,
81 SP::F16, SP::F17, SP::F18, SP::F19,
82 SP::F20, SP::F21, SP::F22, SP::F23,
83 SP::F24, SP::F25, SP::F26, SP::F27,
84 SP::F28, SP::F29, SP::F30, SP::F31 };
85
86static constexpr unsigned DFPRegDecoderTable[] = {
87 SP::D0, SP::D16, SP::D1, SP::D17,
88 SP::D2, SP::D18, SP::D3, SP::D19,
89 SP::D4, SP::D20, SP::D5, SP::D21,
90 SP::D6, SP::D22, SP::D7, SP::D23,
91 SP::D8, SP::D24, SP::D9, SP::D25,
92 SP::D10, SP::D26, SP::D11, SP::D27,
93 SP::D12, SP::D28, SP::D13, SP::D29,
94 SP::D14, SP::D30, SP::D15, SP::D31 };
95
96static constexpr unsigned QFPRegDecoderTable[] = {
97 SP::Q0, SP::Q8, ~0U, ~0U,
98 SP::Q1, SP::Q9, ~0U, ~0U,
99 SP::Q2, SP::Q10, ~0U, ~0U,
100 SP::Q3, SP::Q11, ~0U, ~0U,
101 SP::Q4, SP::Q12, ~0U, ~0U,
102 SP::Q5, SP::Q13, ~0U, ~0U,
103 SP::Q6, SP::Q14, ~0U, ~0U,
104 SP::Q7, SP::Q15, ~0U, ~0U } ;
105
106static constexpr unsigned FCCRegDecoderTable[] = {
107 SP::FCC0, SP::FCC1, SP::FCC2, SP::FCC3 };
108
109static constexpr unsigned ASRRegDecoderTable[] = {
110 SP::Y, SP::ASR1, SP::ASR2, SP::ASR3, SP::ASR4, SP::ASR5, SP::ASR6,
111 SP::ASR7, SP::ASR8, SP::ASR9, SP::ASR10, SP::ASR11, SP::ASR12, SP::ASR13,
112 SP::ASR14, SP::ASR15, SP::ASR16, SP::ASR17, SP::ASR18, SP::ASR19, SP::ASR20,
113 SP::ASR21, SP::ASR22, SP::ASR23, SP::ASR24, SP::ASR25, SP::ASR26, SP::ASR27,
114 SP::ASR28, SP::ASR29, SP::ASR30, SP::ASR31};
115
116static constexpr unsigned PRRegDecoderTable[] = {
117 SP::TPC, SP::TNPC, SP::TSTATE, SP::TT, SP::TICK,
118 SP::TBA, SP::PSTATE, SP::TL, SP::PIL, SP::CWP,
119 SP::CANSAVE, SP::CANRESTORE, SP::CLEANWIN, SP::OTHERWIN, SP::WSTATE};
120
121static constexpr uint16_t IntPairDecoderTable[] = {
122 SP::G0_G1, SP::G2_G3, SP::G4_G5, SP::G6_G7,
123 SP::O0_O1, SP::O2_O3, SP::O4_O5, SP::O6_O7,
124 SP::L0_L1, SP::L2_L3, SP::L4_L5, SP::L6_L7,
125 SP::I0_I1, SP::I2_I3, SP::I4_I5, SP::I6_I7,
126};
127
128static constexpr unsigned CPRegDecoderTable[] = {
129 SP::C0, SP::C1, SP::C2, SP::C3,
130 SP::C4, SP::C5, SP::C6, SP::C7,
131 SP::C8, SP::C9, SP::C10, SP::C11,
132 SP::C12, SP::C13, SP::C14, SP::C15,
133 SP::C16, SP::C17, SP::C18, SP::C19,
134 SP::C20, SP::C21, SP::C22, SP::C23,
135 SP::C24, SP::C25, SP::C26, SP::C27,
136 SP::C28, SP::C29, SP::C30, SP::C31
137};
138
139static constexpr uint16_t CPPairDecoderTable[] = {
140 SP::C0_C1, SP::C2_C3, SP::C4_C5, SP::C6_C7,
141 SP::C8_C9, SP::C10_C11, SP::C12_C13, SP::C14_C15,
142 SP::C16_C17, SP::C18_C19, SP::C20_C21, SP::C22_C23,
143 SP::C24_C25, SP::C26_C27, SP::C28_C29, SP::C30_C31
144};
145// clang-format on
146
147static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
148 uint64_t Address,
149 const MCDisassembler *Decoder) {
150 if (RegNo >= std::size(IntRegDecoderTable))
152 unsigned Reg = IntRegDecoderTable[RegNo];
155}
156
157static DecodeStatus DecodeI64RegsRegisterClass(MCInst &Inst, unsigned RegNo,
158 uint64_t Address,
159 const MCDisassembler *Decoder) {
160 return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
161}
162
163static DecodeStatus DecodeFPRegsRegisterClass(MCInst &Inst, unsigned RegNo,
164 uint64_t Address,
165 const MCDisassembler *Decoder) {
166 if (RegNo >= std::size(FPRegDecoderTable))
168 unsigned Reg = FPRegDecoderTable[RegNo];
171}
172
173static DecodeStatus DecodeDFPRegsRegisterClass(MCInst &Inst, unsigned RegNo,
174 uint64_t Address,
175 const MCDisassembler *Decoder) {
176 if (RegNo >= std::size(DFPRegDecoderTable))
178 unsigned Reg = DFPRegDecoderTable[RegNo];
181}
182
183static DecodeStatus DecodeQFPRegsRegisterClass(MCInst &Inst, unsigned RegNo,
184 uint64_t Address,
185 const MCDisassembler *Decoder) {
186 if (RegNo >= std::size(QFPRegDecoderTable))
188
189 unsigned Reg = QFPRegDecoderTable[RegNo];
190 if (Reg == ~0U)
194}
195
196static DecodeStatus
197DecodeCoprocRegsRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address,
198 const MCDisassembler *Decoder) {
199 if (RegNo >= std::size(CPRegDecoderTable))
201 unsigned Reg = CPRegDecoderTable[RegNo];
204}
205
206static DecodeStatus DecodeFCCRegsRegisterClass(MCInst &Inst, unsigned RegNo,
207 uint64_t Address,
208 const MCDisassembler *Decoder) {
209 if (RegNo >= std::size(FCCRegDecoderTable))
211 // V8 has only %fcc0, and the field is reserved there.
212 if (RegNo != 0 && !Decoder->getSubtargetInfo().hasFeature(Sparc::FeatureV9))
216}
217
218static DecodeStatus DecodeASRRegsRegisterClass(MCInst &Inst, unsigned RegNo,
219 uint64_t Address,
220 const MCDisassembler *Decoder) {
221 if (RegNo >= std::size(ASRRegDecoderTable))
225}
226
227static DecodeStatus DecodePRRegsRegisterClass(MCInst &Inst, unsigned RegNo,
228 uint64_t Address,
229 const MCDisassembler *Decoder) {
230 if (RegNo >= std::size(PRRegDecoderTable))
234}
235
236static DecodeStatus DecodeIntPairRegisterClass(MCInst &Inst, unsigned RegNo,
237 uint64_t Address,
238 const MCDisassembler *Decoder) {
240
241 if ((RegNo & 1))
243
244 RegNo = RegNo / 2;
245 if (RegNo >= std::size(IntPairDecoderTable))
247
248 unsigned RegisterPair = IntPairDecoderTable[RegNo];
249 Inst.addOperand(MCOperand::createReg(RegisterPair));
250 return S;
251}
252
253static DecodeStatus
254DecodeCoprocPairRegisterClass(MCInst &Inst, unsigned RegNo, uint64_t Address,
255 const MCDisassembler *Decoder) {
256 RegNo = RegNo / 2;
257 if (RegNo >= std::size(CPPairDecoderTable))
259
260 unsigned RegisterPair = CPPairDecoderTable[RegNo];
261 Inst.addOperand(MCOperand::createReg(RegisterPair));
263}
264
265static bool tryAddingSymbolicOperand(int64_t Value, bool isBranch,
266 uint64_t Address, uint64_t Offset,
267 uint64_t Width, MCInst &MI,
268 const MCDisassembler *Decoder) {
269 return Decoder->tryAddingSymbolicOperand(MI, Value, Address, isBranch, Offset,
270 Width, /*InstSize=*/4);
271}
272
273static DecodeStatus DecodeCall(MCInst &MI, unsigned insn, uint64_t Address,
274 const MCDisassembler *Decoder) {
275 int64_t CallOffset = SignExtend64(fieldFromInstruction(insn, 0, 30), 30) * 4;
276 if (!tryAddingSymbolicOperand(Address + CallOffset, false, Address, 0, 30, MI,
277 Decoder))
278 MI.addOperand(MCOperand::createImm(CallOffset));
280}
281
282template <unsigned N>
283static DecodeStatus DecodeSIMM(MCInst &MI, unsigned insn, uint64_t Address,
284 const MCDisassembler *Decoder) {
285 assert(isUInt<N>(insn));
286 MI.addOperand(MCOperand::createImm(SignExtend64<N>(insn)));
288}
289
290template <unsigned N>
292 const MCDisassembler *Decoder) {
293 int64_t BranchOffset = SignExtend64(ImmVal, N) * 4;
294 if (!tryAddingSymbolicOperand(Address + BranchOffset, true, Address, 0, N, MI,
295 Decoder))
296 MI.addOperand(MCOperand::createImm(BranchOffset));
298}
299
300#include "SparcGenDisassemblerTables.inc"
301
302/// Read four bytes from the ArrayRef and return 32 bit word.
304 uint64_t &Size, uint32_t &Insn,
305 bool IsLittleEndian) {
306 // We want to read exactly 4 Bytes of data.
307 if (Bytes.size() < 4) {
308 Size = 0;
310 }
311
312 Size = 4;
314 Bytes.data(), IsLittleEndian ? endianness::little : endianness::big);
316}
317
318DecodeStatus SparcDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,
319 ArrayRef<uint8_t> Bytes,
320 uint64_t Address,
321 raw_ostream &CStream) const {
322 uint32_t Insn;
323 bool isLittleEndian = getContext().getAsmInfo().isLittleEndian();
325 readInstruction32(Bytes, Address, Size, Insn, isLittleEndian);
326 if (Result == MCDisassembler::Fail)
328
329 // Calling the auto-generated decoder function.
330
331 if (STI.hasFeature(Sparc::FeatureV9))
332 Result = decodeInstruction(DecoderTableSparcV932, Instr, Insn, Address,
333 this, STI);
334 else
335 Result = decodeInstruction(DecoderTableSparcV832, Instr, Insn, Address, this, STI);
336
337 if (Result != MCDisassembler::Fail)
338 return Result;
339
340 Result =
341 decodeInstruction(DecoderTableSparc32, Instr, Insn, Address, this, STI);
342
343 return Result;
344}
MCDisassembler::DecodeStatus DecodeStatus
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
uint64_t const MCDisassembler * Decoder
static bool readInstruction32(ArrayRef< uint8_t > Bytes, uint64_t Address, uint64_t &Size, uint32_t &Insn)
#define LLVM_ABI
Definition Compiler.h:211
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
IRTranslator LLVM IR MI
Register Reg
#define T
static bool isBranch(unsigned Opcode)
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
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 DecodeSIMM(MCInst &MI, unsigned insn, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeCoprocPairRegisterClass(MCInst &Inst, unsigned RegNo, 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 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)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
const T * data() const
Definition ArrayRef.h:138
Context object for machine code objects.
Definition MCContext.h:84
Superclass for all disassemblers.
DecodeStatus
Ternary decode status.
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:187
void addOperand(const MCOperand Op)
Definition MCInst.h:214
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:137
static MCOperand createImm(int64_t Val)
Definition MCInst.h:144
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
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition Endian.h:53
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
Target & getTheSparcTarget()
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
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:572
#define N
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.