1 //===------ PPCDisassembler.cpp - Disassembler for PowerPC ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/PPCMCTargetDesc.h"
11 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
12 #include "llvm/MC/MCFixedLenDisassembler.h"
13 #include "llvm/MC/MCInst.h"
14 #include "llvm/MC/MCSubtargetInfo.h"
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/TargetRegistry.h"
20 DEFINE_PPC_REGCLASSES;
22 #define DEBUG_TYPE "ppc-disassembler"
24 typedef MCDisassembler::DecodeStatus DecodeStatus;
27 class PPCDisassembler : public MCDisassembler {
31 PPCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
33 : MCDisassembler(STI, Ctx), IsLittleEndian(IsLittleEndian) {}
35 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
36 ArrayRef<uint8_t> Bytes, uint64_t Address,
38 raw_ostream &CStream) const override;
40 } // end anonymous namespace
42 static MCDisassembler *createPPCDisassembler(const Target &T,
43 const MCSubtargetInfo &STI,
45 return new PPCDisassembler(STI, Ctx, /*IsLittleEndian=*/false);
48 static MCDisassembler *createPPCLEDisassembler(const Target &T,
49 const MCSubtargetInfo &STI,
51 return new PPCDisassembler(STI, Ctx, /*IsLittleEndian=*/true);
54 extern "C" void LLVMInitializePowerPCDisassembler() {
55 // Register the disassembler for each target.
56 TargetRegistry::RegisterMCDisassembler(getThePPC32Target(),
57 createPPCDisassembler);
58 TargetRegistry::RegisterMCDisassembler(getThePPC64Target(),
59 createPPCDisassembler);
60 TargetRegistry::RegisterMCDisassembler(getThePPC64LETarget(),
61 createPPCLEDisassembler);
64 // FIXME: These can be generated by TableGen from the existing register
67 template <std::size_t N>
68 static DecodeStatus decodeRegisterClass(MCInst &Inst, uint64_t RegNo,
69 const MCPhysReg (&Regs)[N]) {
70 assert(RegNo < N && "Invalid register number");
71 Inst.addOperand(MCOperand::createReg(Regs[RegNo]));
72 return MCDisassembler::Success;
75 static DecodeStatus DecodeCRRCRegisterClass(MCInst &Inst, uint64_t RegNo,
77 const void *Decoder) {
78 return decodeRegisterClass(Inst, RegNo, CRRegs);
81 static DecodeStatus DecodeCRRC0RegisterClass(MCInst &Inst, uint64_t RegNo,
83 const void *Decoder) {
84 return decodeRegisterClass(Inst, RegNo, CRRegs);
87 static DecodeStatus DecodeCRBITRCRegisterClass(MCInst &Inst, uint64_t RegNo,
89 const void *Decoder) {
90 return decodeRegisterClass(Inst, RegNo, CRBITRegs);
93 static DecodeStatus DecodeF4RCRegisterClass(MCInst &Inst, uint64_t RegNo,
95 const void *Decoder) {
96 return decodeRegisterClass(Inst, RegNo, FRegs);
99 static DecodeStatus DecodeF8RCRegisterClass(MCInst &Inst, uint64_t RegNo,
101 const void *Decoder) {
102 return decodeRegisterClass(Inst, RegNo, FRegs);
105 static DecodeStatus DecodeVFRCRegisterClass(MCInst &Inst, uint64_t RegNo,
107 const void *Decoder) {
108 return decodeRegisterClass(Inst, RegNo, VFRegs);
111 static DecodeStatus DecodeVRRCRegisterClass(MCInst &Inst, uint64_t RegNo,
113 const void *Decoder) {
114 return decodeRegisterClass(Inst, RegNo, VRegs);
117 static DecodeStatus DecodeVSRCRegisterClass(MCInst &Inst, uint64_t RegNo,
119 const void *Decoder) {
120 return decodeRegisterClass(Inst, RegNo, VSRegs);
123 static DecodeStatus DecodeVSFRCRegisterClass(MCInst &Inst, uint64_t RegNo,
125 const void *Decoder) {
126 return decodeRegisterClass(Inst, RegNo, VSFRegs);
129 static DecodeStatus DecodeVSSRCRegisterClass(MCInst &Inst, uint64_t RegNo,
131 const void *Decoder) {
132 return decodeRegisterClass(Inst, RegNo, VSSRegs);
135 static DecodeStatus DecodeGPRCRegisterClass(MCInst &Inst, uint64_t RegNo,
137 const void *Decoder) {
138 return decodeRegisterClass(Inst, RegNo, RRegs);
141 static DecodeStatus DecodeGPRC_NOR0RegisterClass(MCInst &Inst, uint64_t RegNo,
143 const void *Decoder) {
144 return decodeRegisterClass(Inst, RegNo, RRegsNoR0);
147 static DecodeStatus DecodeG8RCRegisterClass(MCInst &Inst, uint64_t RegNo,
149 const void *Decoder) {
150 return decodeRegisterClass(Inst, RegNo, XRegs);
153 static DecodeStatus DecodeG8RC_NOX0RegisterClass(MCInst &Inst, uint64_t RegNo,
155 const void *Decoder) {
156 return decodeRegisterClass(Inst, RegNo, XRegsNoX0);
159 #define DecodePointerLikeRegClass0 DecodeGPRCRegisterClass
160 #define DecodePointerLikeRegClass1 DecodeGPRC_NOR0RegisterClass
162 static DecodeStatus DecodeQFRCRegisterClass(MCInst &Inst, uint64_t RegNo,
164 const void *Decoder) {
165 return decodeRegisterClass(Inst, RegNo, QFRegs);
168 static DecodeStatus DecodeSPE4RCRegisterClass(MCInst &Inst, uint64_t RegNo,
170 const void *Decoder) {
171 return decodeRegisterClass(Inst, RegNo, RRegs);
174 static DecodeStatus DecodeSPERCRegisterClass(MCInst &Inst, uint64_t RegNo,
176 const void *Decoder) {
177 return decodeRegisterClass(Inst, RegNo, SPERegs);
180 #define DecodeQSRCRegisterClass DecodeQFRCRegisterClass
181 #define DecodeQBRCRegisterClass DecodeQFRCRegisterClass
184 static DecodeStatus decodeUImmOperand(MCInst &Inst, uint64_t Imm,
185 int64_t Address, const void *Decoder) {
186 assert(isUInt<N>(Imm) && "Invalid immediate");
187 Inst.addOperand(MCOperand::createImm(Imm));
188 return MCDisassembler::Success;
192 static DecodeStatus decodeSImmOperand(MCInst &Inst, uint64_t Imm,
193 int64_t Address, const void *Decoder) {
194 assert(isUInt<N>(Imm) && "Invalid immediate");
195 Inst.addOperand(MCOperand::createImm(SignExtend64<N>(Imm)));
196 return MCDisassembler::Success;
199 static DecodeStatus decodeMemRIOperands(MCInst &Inst, uint64_t Imm,
200 int64_t Address, const void *Decoder) {
201 // Decode the memri field (imm, reg), which has the low 16-bits as the
202 // displacement and the next 5 bits as the register #.
204 uint64_t Base = Imm >> 16;
205 uint64_t Disp = Imm & 0xFFFF;
207 assert(Base < 32 && "Invalid base register");
209 switch (Inst.getOpcode()) {
217 // Add the tied output operand.
218 Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
225 Inst.insert(Inst.begin(), MCOperand::createReg(RRegsNoR0[Base]));
229 Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp)));
230 Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
231 return MCDisassembler::Success;
234 static DecodeStatus decodeMemRIXOperands(MCInst &Inst, uint64_t Imm,
235 int64_t Address, const void *Decoder) {
236 // Decode the memrix field (imm, reg), which has the low 14-bits as the
237 // displacement and the next 5 bits as the register #.
239 uint64_t Base = Imm >> 14;
240 uint64_t Disp = Imm & 0x3FFF;
242 assert(Base < 32 && "Invalid base register");
244 if (Inst.getOpcode() == PPC::LDU)
245 // Add the tied output operand.
246 Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
247 else if (Inst.getOpcode() == PPC::STDU)
248 Inst.insert(Inst.begin(), MCOperand::createReg(RRegsNoR0[Base]));
250 Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp << 2)));
251 Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
252 return MCDisassembler::Success;
255 static DecodeStatus decodeMemRIX16Operands(MCInst &Inst, uint64_t Imm,
256 int64_t Address, const void *Decoder) {
257 // Decode the memrix16 field (imm, reg), which has the low 12-bits as the
258 // displacement with 16-byte aligned, and the next 5 bits as the register #.
260 uint64_t Base = Imm >> 12;
261 uint64_t Disp = Imm & 0xFFF;
263 assert(Base < 32 && "Invalid base register");
265 Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp << 4)));
266 Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
267 return MCDisassembler::Success;
270 static DecodeStatus decodeSPE8Operands(MCInst &Inst, uint64_t Imm,
271 int64_t Address, const void *Decoder) {
272 // Decode the spe8disp field (imm, reg), which has the low 5-bits as the
273 // displacement with 8-byte aligned, and the next 5 bits as the register #.
275 uint64_t Base = Imm >> 5;
276 uint64_t Disp = Imm & 0x1F;
278 assert(Base < 32 && "Invalid base register");
280 Inst.addOperand(MCOperand::createImm(Disp << 3));
281 Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
282 return MCDisassembler::Success;
285 static DecodeStatus decodeSPE4Operands(MCInst &Inst, uint64_t Imm,
286 int64_t Address, const void *Decoder) {
287 // Decode the spe4disp field (imm, reg), which has the low 5-bits as the
288 // displacement with 4-byte aligned, and the next 5 bits as the register #.
290 uint64_t Base = Imm >> 5;
291 uint64_t Disp = Imm & 0x1F;
293 assert(Base < 32 && "Invalid base register");
295 Inst.addOperand(MCOperand::createImm(Disp << 2));
296 Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
297 return MCDisassembler::Success;
300 static DecodeStatus decodeSPE2Operands(MCInst &Inst, uint64_t Imm,
301 int64_t Address, const void *Decoder) {
302 // Decode the spe2disp field (imm, reg), which has the low 5-bits as the
303 // displacement with 2-byte aligned, and the next 5 bits as the register #.
305 uint64_t Base = Imm >> 5;
306 uint64_t Disp = Imm & 0x1F;
308 assert(Base < 32 && "Invalid base register");
310 Inst.addOperand(MCOperand::createImm(Disp << 1));
311 Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
312 return MCDisassembler::Success;
315 static DecodeStatus decodeCRBitMOperand(MCInst &Inst, uint64_t Imm,
316 int64_t Address, const void *Decoder) {
317 // The cr bit encoding is 0x80 >> cr_reg_num.
319 unsigned Zeros = countTrailingZeros(Imm);
320 assert(Zeros < 8 && "Invalid CR bit value");
322 Inst.addOperand(MCOperand::createReg(CRRegs[7 - Zeros]));
323 return MCDisassembler::Success;
326 #include "PPCGenDisassemblerTables.inc"
328 DecodeStatus PPCDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
329 ArrayRef<uint8_t> Bytes,
330 uint64_t Address, raw_ostream &OS,
331 raw_ostream &CS) const {
332 // Get the four bytes of the instruction.
334 if (Bytes.size() < 4) {
336 return MCDisassembler::Fail;
339 // Read the instruction in the proper endianness.
340 uint32_t Inst = IsLittleEndian ? support::endian::read32le(Bytes.data())
341 : support::endian::read32be(Bytes.data());
343 if (STI.getFeatureBits()[PPC::FeatureQPX]) {
344 DecodeStatus result =
345 decodeInstruction(DecoderTableQPX32, MI, Inst, Address, this, STI);
346 if (result != MCDisassembler::Fail)
348 } else if (STI.getFeatureBits()[PPC::FeatureSPE]) {
349 DecodeStatus result =
350 decodeInstruction(DecoderTableSPE32, MI, Inst, Address, this, STI);
351 if (result != MCDisassembler::Fail)
355 return decodeInstruction(DecoderTable32, MI, Inst, Address, this, STI);