LLVM 22.0.0git
PPCDisassembler.cpp
Go to the documentation of this file.
1//===------ PPCDisassembler.cpp - Disassembler for PowerPC ------*- 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
11#include "llvm/MC/MCDecoder.h"
14#include "llvm/MC/MCInst.h"
18#include "llvm/Support/Endian.h"
19
20using namespace llvm;
21
23
24#define DEBUG_TYPE "ppc-disassembler"
25
27
28namespace {
29class PPCDisassembler : public MCDisassembler {
30 bool IsLittleEndian;
31
32public:
33 PPCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
34 bool IsLittleEndian)
35 : MCDisassembler(STI, Ctx), IsLittleEndian(IsLittleEndian) {}
36
39 raw_ostream &CStream) const override;
40};
41} // end anonymous namespace
42
44 const MCSubtargetInfo &STI,
45 MCContext &Ctx) {
46 return new PPCDisassembler(STI, Ctx, /*IsLittleEndian=*/false);
47}
48
50 const MCSubtargetInfo &STI,
51 MCContext &Ctx) {
52 return new PPCDisassembler(STI, Ctx, /*IsLittleEndian=*/true);
53}
54
57 // Register the disassembler for each target.
66}
67
68static DecodeStatus decodeCondBrTarget(MCInst &Inst, unsigned Imm,
69 uint64_t /*Address*/,
70 const MCDisassembler * /*Decoder*/) {
71 Inst.addOperand(MCOperand::createImm(SignExtend32<14>(Imm)));
73}
74
75static DecodeStatus decodeDirectBrTarget(MCInst &Inst, unsigned Imm,
76 uint64_t /*Address*/,
77 const MCDisassembler * /*Decoder*/) {
78 int32_t Offset = SignExtend32<24>(Imm);
81}
82
83// FIXME: These can be generated by TableGen from the existing register
84// encoding values!
85
86template <std::size_t N>
88 const MCPhysReg (&Regs)[N]) {
89 if (RegNo >= N)
91 Inst.addOperand(MCOperand::createReg(Regs[RegNo]));
93}
94
96 uint64_t Address,
97 const MCDisassembler *Decoder) {
98 return decodeRegisterClass(Inst, RegNo, CRRegs);
99}
100
102 uint64_t Address,
103 const MCDisassembler *Decoder) {
104 return decodeRegisterClass(Inst, RegNo, CRBITRegs);
105}
106
108 uint64_t Address,
109 const MCDisassembler *Decoder) {
110 return decodeRegisterClass(Inst, RegNo, FRegs);
111}
112
114 uint64_t Address,
115 const MCDisassembler *Decoder) {
116 return decodeRegisterClass(Inst, RegNo, FRegs);
117}
118
120 uint64_t Address,
121 const MCDisassembler *Decoder) {
122 if (RegNo > 30 || (RegNo & 1))
124 return decodeRegisterClass(Inst, RegNo >> 1, FpRegs);
125}
126
128 uint64_t Address,
129 const MCDisassembler *Decoder) {
130 return decodeRegisterClass(Inst, RegNo, VFRegs);
131}
132
134 uint64_t Address,
135 const MCDisassembler *Decoder) {
136 return decodeRegisterClass(Inst, RegNo, VRegs);
137}
138
140 uint64_t Address,
141 const MCDisassembler *Decoder) {
142 return decodeRegisterClass(Inst, RegNo, VSRegs);
143}
144
146 uint64_t Address,
147 const MCDisassembler *Decoder) {
148 return decodeRegisterClass(Inst, RegNo, VSFRegs);
149}
150
152 uint64_t Address,
153 const MCDisassembler *Decoder) {
154 return decodeRegisterClass(Inst, RegNo, VSSRegs);
155}
156
158 uint64_t Address,
159 const MCDisassembler *Decoder) {
160 return decodeRegisterClass(Inst, RegNo, RRegs);
161}
162
163static DecodeStatus
165 const MCDisassembler *Decoder) {
166 return decodeRegisterClass(Inst, RegNo, RRegsNoR0);
167}
168
170 uint64_t Address,
171 const MCDisassembler *Decoder) {
172 return decodeRegisterClass(Inst, RegNo, XRegs);
173}
174
176 uint64_t Address,
177 const MCDisassembler *Decoder) {
178 return decodeRegisterClass(Inst, RegNo, XRegs);
179}
180
181static DecodeStatus
183 const MCDisassembler *Decoder) {
184 return decodeRegisterClass(Inst, RegNo, XRegsNoX0);
185}
186
187#define DecodePointerLikeRegClass0 DecodeGPRCRegisterClass
188#define DecodePointerLikeRegClass1 DecodeGPRC_NOR0RegisterClass
189
191 uint64_t Address,
192 const MCDisassembler *Decoder) {
193 return decodeRegisterClass(Inst, RegNo, SPERegs);
194}
195
197 uint64_t Address,
198 const MCDisassembler *Decoder) {
199 return decodeRegisterClass(Inst, RegNo, ACCRegs);
200}
201
203 uint64_t Address,
204 const void *Decoder) {
205 return decodeRegisterClass(Inst, RegNo, WACCRegs);
206}
207
209 uint64_t Address,
210 const void *Decoder) {
211 return decodeRegisterClass(Inst, RegNo, WACC_HIRegs);
212}
213
214// TODO: Make this function static when the register class is used by a new
215// instruction.
217 uint64_t Address,
218 const void *Decoder) {
219 return decodeRegisterClass(Inst, RegNo, DMRROWRegs);
220}
221
223 uint64_t Address,
224 const void *Decoder) {
225 return decodeRegisterClass(Inst, RegNo, DMRROWpRegs);
226}
227
229 uint64_t Address,
230 const void *Decoder) {
231 return decodeRegisterClass(Inst, RegNo, DMRRegs);
232}
233
234// TODO: Make this function static when the register class is used by a new
235// instruction.
237 uint64_t Address, const void *Decoder) {
238 return decodeRegisterClass(Inst, RegNo, DMRpRegs);
239}
240
242 uint64_t Address,
243 const MCDisassembler *Decoder) {
244 return decodeRegisterClass(Inst, RegNo, VSRpRegs);
245}
246
247#define DecodeQSRCRegisterClass DecodeQFRCRegisterClass
248#define DecodeQBRCRegisterClass DecodeQFRCRegisterClass
249
250template <unsigned N>
252 int64_t Address,
253 const MCDisassembler *Decoder) {
254 if (!isUInt<N>(Imm))
258}
259
260template <unsigned N>
262 int64_t Address,
263 const MCDisassembler *Decoder) {
264 if (!isUInt<N>(Imm))
266 Inst.addOperand(MCOperand::createImm(SignExtend64<N>(Imm)));
268}
269
271 int64_t Address,
272 const MCDisassembler *Decoder) {
273 if (Imm != 0)
277}
278
280 uint64_t Address,
281 const MCDisassembler *Decoder) {
282 if (RegNo & 1)
284 Inst.addOperand(MCOperand::createReg(VSRpRegs[RegNo >> 1]));
286}
287
289 int64_t Address,
290 const MCDisassembler *Decoder) {
291 // The rix displacement is an immediate shifted by 2
292 Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Imm << 2)));
294}
295
297 int64_t Address,
298 const MCDisassembler *Decoder) {
299 // Decode the disp field for a hash store or hash check operation.
300 // The field is composed of an immediate value that is 6 bits
301 // and covers the range -8 to -512. The immediate is always negative and 2s
302 // complement which is why we sign extend a 7 bit value.
303 const int64_t Disp = SignExtend64<7>((Imm & 0x3F) + 64) * 8;
304
307}
308
310 int64_t Address,
311 const MCDisassembler *Decoder) {
312 // The rix16 displacement has 12-bits which are shifted by 4.
313 Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Imm << 4)));
315}
316
318 int64_t Address,
319 const MCDisassembler *Decoder) {
320 // Decode the dispSPE8 field, which has 5-bits, 8-byte aligned.
321
322 uint64_t Disp = Imm & 0x1F;
323
324 Inst.addOperand(MCOperand::createImm(Disp << 3));
326}
327
329 int64_t Address,
330 const MCDisassembler *Decoder) {
331 // Decode the dispSPE8 field, which has 5-bits, 4-byte aligned.
332
333 uint64_t Disp = Imm & 0x1F;
334
335 Inst.addOperand(MCOperand::createImm(Disp << 2));
337}
338
340 int64_t Address,
341 const MCDisassembler *Decoder) {
342 // Decode the dispSPE8 field, which has 5-bits, 2-byte aligned.
343
344 uint64_t Disp = Imm & 0x1F;
345 Inst.addOperand(MCOperand::createImm(Disp << 1));
347}
348
350 int64_t Address,
351 const MCDisassembler *Decoder) {
352 // The cr bit encoding is 0x80 >> cr_reg_num.
353
354 unsigned Zeros = llvm::countr_zero(Imm);
355 if (Zeros >= 8)
357
358 Inst.addOperand(MCOperand::createReg(CRRegs[7 - Zeros]));
360}
361
362#include "PPCGenDisassemblerTables.inc"
363
364DecodeStatus PPCDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
365 ArrayRef<uint8_t> Bytes,
366 uint64_t Address,
367 raw_ostream &CS) const {
368 auto *ReadFunc = IsLittleEndian ? support::endian::read32le
370
371 // If this is an 8-byte prefixed instruction, handle it here.
372 // Note: prefixed instructions aren't technically 8-byte entities - the prefix
373 // appears in memory at an address 4 bytes prior to that of the base
374 // instruction regardless of endianness. So we read the two pieces and
375 // rebuild the 8-byte instruction.
376 // TODO: In this function we call decodeInstruction several times with
377 // different decoder tables. It may be possible to only call once by
378 // looking at the top 6 bits of the instruction.
379 if (STI.hasFeature(PPC::FeaturePrefixInstrs) && Bytes.size() >= 8) {
380 uint32_t Prefix = ReadFunc(Bytes.data());
381 uint32_t BaseInst = ReadFunc(Bytes.data() + 4);
382 uint64_t Inst = BaseInst | (uint64_t)Prefix << 32;
383 DecodeStatus result = decodeInstruction(DecoderTable64, MI, Inst, Address,
384 this, STI);
385 if (result != MCDisassembler::Fail) {
386 Size = 8;
387 return result;
388 }
389 }
390
391 // Get the four bytes of the instruction.
392 Size = 4;
393 if (Bytes.size() < 4) {
394 Size = 0;
396 }
397
398 // Read the instruction in the proper endianness.
399 uint64_t Inst = ReadFunc(Bytes.data());
400
401 if (STI.hasFeature(PPC::FeatureSPE)) {
402 DecodeStatus result =
403 decodeInstruction(DecoderTableSPE32, MI, Inst, Address, this, STI);
404 if (result != MCDisassembler::Fail)
405 return result;
406 }
407
408 return decodeInstruction(DecoderTable32, MI, Inst, Address, this, STI);
409}
#define LLVM_ABI
Definition: Compiler.h:213
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:132
uint64_t Size
IRTranslator LLVM IR MI
static DecodeStatus decodeRegisterClass(MCInst &Inst, uint64_t RegNo, const MCPhysReg(&Regs)[N])
static DecodeStatus decodeVSRpEvenOperands(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeVSFRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeDispSPE4Operand(MCInst &Inst, uint64_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeDMRRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const void *Decoder)
MCDisassembler::DecodeStatus DecodeStatus
static DecodeStatus decodeCRBitMOperand(MCInst &Inst, uint64_t Imm, int64_t Address, const MCDisassembler *Decoder)
DecodeStatus DecodeDMRpRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const void *Decoder)
static DecodeStatus decodeImmZeroOperand(MCInst &Inst, uint64_t Imm, int64_t Address, const MCDisassembler *Decoder)
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCDisassembler()
static DecodeStatus DecodeCRBITRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeGPRC_NOR0RegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeG8pRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeDispRIHashOperand(MCInst &Inst, uint64_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeGPRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeFpRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static MCDisassembler * createPPCDisassembler(const Target &T, const MCSubtargetInfo &STI, MCContext &Ctx)
static DecodeStatus decodeDispSPE2Operand(MCInst &Inst, uint64_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeSImmOperand(MCInst &Inst, uint64_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeVSRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeCondBrTarget(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *)
static DecodeStatus decodeDispSPE8Operand(MCInst &Inst, uint64_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeVRRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static MCDisassembler * createPPCLEDisassembler(const Target &T, const MCSubtargetInfo &STI, MCContext &Ctx)
static DecodeStatus decodeDispRIX16Operand(MCInst &Inst, uint64_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeCRRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeUImmOperand(MCInst &Inst, uint64_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeG8RC_NOX0RegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeACCRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeVSSRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeWACC_HIRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const void *Decoder)
static DecodeStatus DecodeVFRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeF8RCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeSPERCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
DecodeStatus DecodeDMRROWRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const void *Decoder)
static DecodeStatus DecodeWACCRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const void *Decoder)
static DecodeStatus DecodeG8RCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeDMRROWpRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const void *Decoder)
static DecodeStatus decodeDispRIXOperand(MCInst &Inst, uint64_t Imm, int64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeF4RCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus DecodeVSRpRCRegisterClass(MCInst &Inst, uint64_t RegNo, uint64_t Address, const MCDisassembler *Decoder)
static DecodeStatus decodeDirectBrTarget(MCInst &Inst, unsigned Imm, uint64_t, const MCDisassembler *)
#define DEFINE_PPC_REGCLASSES
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.
DecodeStatus
Ternary decode status.
virtual DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, ArrayRef< uint8_t > Bytes, uint64_t Address, raw_ostream &CStream) const =0
Returns the disassembly of a single instruction.
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.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
uint32_t read32be(const void *P)
Definition: Endian.h:438
uint32_t read32le(const void *P)
Definition: Endian.h:429
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
Target & getThePPC64LETarget()
Target & getThePPC32Target()
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition: bit.h:157
Target & getThePPC64Target()
Target & getThePPC32LETarget()
#define N
static void RegisterMCDisassembler(Target &T, Target::MCDisassemblerCtorTy Fn)
RegisterMCDisassembler - Register a MCDisassembler implementation for the given target.