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 //===----------------------------------------------------------------------===//
11 #include "llvm/MC/MCDisassembler.h"
12 #include "llvm/MC/MCFixedLenDisassembler.h"
13 #include "llvm/MC/MCInst.h"
14 #include "llvm/MC/MCSubtargetInfo.h"
15 #include "llvm/Support/TargetRegistry.h"
19 #define DEBUG_TYPE "ppc-disassembler"
21 typedef MCDisassembler::DecodeStatus DecodeStatus;
24 class PPCDisassembler : public MCDisassembler {
26 PPCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
27 : MCDisassembler(STI, Ctx) {}
28 virtual ~PPCDisassembler() {}
30 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
31 ArrayRef<uint8_t> Bytes, uint64_t Address,
33 raw_ostream &CStream) const override;
35 } // end anonymous namespace
37 static MCDisassembler *createPPCDisassembler(const Target &T,
38 const MCSubtargetInfo &STI,
40 return new PPCDisassembler(STI, Ctx);
43 extern "C" void LLVMInitializePowerPCDisassembler() {
44 // Register the disassembler for each target.
45 TargetRegistry::RegisterMCDisassembler(ThePPC32Target,
46 createPPCDisassembler);
47 TargetRegistry::RegisterMCDisassembler(ThePPC64Target,
48 createPPCDisassembler);
49 TargetRegistry::RegisterMCDisassembler(ThePPC64LETarget,
50 createPPCDisassembler);
53 // FIXME: These can be generated by TableGen from the existing register
56 static const unsigned CRRegs[] = {
57 PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
58 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7
61 static const unsigned CRBITRegs[] = {
62 PPC::CR0LT, PPC::CR0GT, PPC::CR0EQ, PPC::CR0UN,
63 PPC::CR1LT, PPC::CR1GT, PPC::CR1EQ, PPC::CR1UN,
64 PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
65 PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
66 PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
67 PPC::CR5LT, PPC::CR5GT, PPC::CR5EQ, PPC::CR5UN,
68 PPC::CR6LT, PPC::CR6GT, PPC::CR6EQ, PPC::CR6UN,
69 PPC::CR7LT, PPC::CR7GT, PPC::CR7EQ, PPC::CR7UN
72 static const unsigned FRegs[] = {
73 PPC::F0, PPC::F1, PPC::F2, PPC::F3,
74 PPC::F4, PPC::F5, PPC::F6, PPC::F7,
75 PPC::F8, PPC::F9, PPC::F10, PPC::F11,
76 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
77 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
78 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
79 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
80 PPC::F28, PPC::F29, PPC::F30, PPC::F31
83 static const unsigned VRegs[] = {
84 PPC::V0, PPC::V1, PPC::V2, PPC::V3,
85 PPC::V4, PPC::V5, PPC::V6, PPC::V7,
86 PPC::V8, PPC::V9, PPC::V10, PPC::V11,
87 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
88 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
89 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
90 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
91 PPC::V28, PPC::V29, PPC::V30, PPC::V31
94 static const unsigned VSRegs[] = {
95 PPC::VSL0, PPC::VSL1, PPC::VSL2, PPC::VSL3,
96 PPC::VSL4, PPC::VSL5, PPC::VSL6, PPC::VSL7,
97 PPC::VSL8, PPC::VSL9, PPC::VSL10, PPC::VSL11,
98 PPC::VSL12, PPC::VSL13, PPC::VSL14, PPC::VSL15,
99 PPC::VSL16, PPC::VSL17, PPC::VSL18, PPC::VSL19,
100 PPC::VSL20, PPC::VSL21, PPC::VSL22, PPC::VSL23,
101 PPC::VSL24, PPC::VSL25, PPC::VSL26, PPC::VSL27,
102 PPC::VSL28, PPC::VSL29, PPC::VSL30, PPC::VSL31,
104 PPC::VSH0, PPC::VSH1, PPC::VSH2, PPC::VSH3,
105 PPC::VSH4, PPC::VSH5, PPC::VSH6, PPC::VSH7,
106 PPC::VSH8, PPC::VSH9, PPC::VSH10, PPC::VSH11,
107 PPC::VSH12, PPC::VSH13, PPC::VSH14, PPC::VSH15,
108 PPC::VSH16, PPC::VSH17, PPC::VSH18, PPC::VSH19,
109 PPC::VSH20, PPC::VSH21, PPC::VSH22, PPC::VSH23,
110 PPC::VSH24, PPC::VSH25, PPC::VSH26, PPC::VSH27,
111 PPC::VSH28, PPC::VSH29, PPC::VSH30, PPC::VSH31
114 static const unsigned VSFRegs[] = {
115 PPC::F0, PPC::F1, PPC::F2, PPC::F3,
116 PPC::F4, PPC::F5, PPC::F6, PPC::F7,
117 PPC::F8, PPC::F9, PPC::F10, PPC::F11,
118 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
119 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
120 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
121 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
122 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
124 PPC::VF0, PPC::VF1, PPC::VF2, PPC::VF3,
125 PPC::VF4, PPC::VF5, PPC::VF6, PPC::VF7,
126 PPC::VF8, PPC::VF9, PPC::VF10, PPC::VF11,
127 PPC::VF12, PPC::VF13, PPC::VF14, PPC::VF15,
128 PPC::VF16, PPC::VF17, PPC::VF18, PPC::VF19,
129 PPC::VF20, PPC::VF21, PPC::VF22, PPC::VF23,
130 PPC::VF24, PPC::VF25, PPC::VF26, PPC::VF27,
131 PPC::VF28, PPC::VF29, PPC::VF30, PPC::VF31
134 static const unsigned GPRegs[] = {
135 PPC::R0, PPC::R1, PPC::R2, PPC::R3,
136 PPC::R4, PPC::R5, PPC::R6, PPC::R7,
137 PPC::R8, PPC::R9, PPC::R10, PPC::R11,
138 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
139 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
140 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
141 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
142 PPC::R28, PPC::R29, PPC::R30, PPC::R31
145 static const unsigned GP0Regs[] = {
146 PPC::ZERO, PPC::R1, PPC::R2, PPC::R3,
147 PPC::R4, PPC::R5, PPC::R6, PPC::R7,
148 PPC::R8, PPC::R9, PPC::R10, PPC::R11,
149 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
150 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
151 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
152 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
153 PPC::R28, PPC::R29, PPC::R30, PPC::R31
156 static const unsigned G8Regs[] = {
157 PPC::X0, PPC::X1, PPC::X2, PPC::X3,
158 PPC::X4, PPC::X5, PPC::X6, PPC::X7,
159 PPC::X8, PPC::X9, PPC::X10, PPC::X11,
160 PPC::X12, PPC::X13, PPC::X14, PPC::X15,
161 PPC::X16, PPC::X17, PPC::X18, PPC::X19,
162 PPC::X20, PPC::X21, PPC::X22, PPC::X23,
163 PPC::X24, PPC::X25, PPC::X26, PPC::X27,
164 PPC::X28, PPC::X29, PPC::X30, PPC::X31
167 static const unsigned QFRegs[] = {
168 PPC::QF0, PPC::QF1, PPC::QF2, PPC::QF3,
169 PPC::QF4, PPC::QF5, PPC::QF6, PPC::QF7,
170 PPC::QF8, PPC::QF9, PPC::QF10, PPC::QF11,
171 PPC::QF12, PPC::QF13, PPC::QF14, PPC::QF15,
172 PPC::QF16, PPC::QF17, PPC::QF18, PPC::QF19,
173 PPC::QF20, PPC::QF21, PPC::QF22, PPC::QF23,
174 PPC::QF24, PPC::QF25, PPC::QF26, PPC::QF27,
175 PPC::QF28, PPC::QF29, PPC::QF30, PPC::QF31
178 template <std::size_t N>
179 static DecodeStatus decodeRegisterClass(MCInst &Inst, uint64_t RegNo,
180 const unsigned (&Regs)[N]) {
181 assert(RegNo < N && "Invalid register number");
182 Inst.addOperand(MCOperand::CreateReg(Regs[RegNo]));
183 return MCDisassembler::Success;
186 static DecodeStatus DecodeCRRCRegisterClass(MCInst &Inst, uint64_t RegNo,
188 const void *Decoder) {
189 return decodeRegisterClass(Inst, RegNo, CRRegs);
192 static DecodeStatus DecodeCRBITRCRegisterClass(MCInst &Inst, uint64_t RegNo,
194 const void *Decoder) {
195 return decodeRegisterClass(Inst, RegNo, CRBITRegs);
198 static DecodeStatus DecodeF4RCRegisterClass(MCInst &Inst, uint64_t RegNo,
200 const void *Decoder) {
201 return decodeRegisterClass(Inst, RegNo, FRegs);
204 static DecodeStatus DecodeF8RCRegisterClass(MCInst &Inst, uint64_t RegNo,
206 const void *Decoder) {
207 return decodeRegisterClass(Inst, RegNo, FRegs);
210 static DecodeStatus DecodeVRRCRegisterClass(MCInst &Inst, uint64_t RegNo,
212 const void *Decoder) {
213 return decodeRegisterClass(Inst, RegNo, VRegs);
216 static DecodeStatus DecodeVSRCRegisterClass(MCInst &Inst, uint64_t RegNo,
218 const void *Decoder) {
219 return decodeRegisterClass(Inst, RegNo, VSRegs);
222 static DecodeStatus DecodeVSFRCRegisterClass(MCInst &Inst, uint64_t RegNo,
224 const void *Decoder) {
225 return decodeRegisterClass(Inst, RegNo, VSFRegs);
228 static DecodeStatus DecodeGPRCRegisterClass(MCInst &Inst, uint64_t RegNo,
230 const void *Decoder) {
231 return decodeRegisterClass(Inst, RegNo, GPRegs);
234 static DecodeStatus DecodeGPRC_NOR0RegisterClass(MCInst &Inst, uint64_t RegNo,
236 const void *Decoder) {
237 return decodeRegisterClass(Inst, RegNo, GP0Regs);
240 static DecodeStatus DecodeG8RCRegisterClass(MCInst &Inst, uint64_t RegNo,
242 const void *Decoder) {
243 return decodeRegisterClass(Inst, RegNo, G8Regs);
246 #define DecodePointerLikeRegClass0 DecodeGPRCRegisterClass
247 #define DecodePointerLikeRegClass1 DecodeGPRC_NOR0RegisterClass
249 static DecodeStatus DecodeQFRCRegisterClass(MCInst &Inst, uint64_t RegNo,
251 const void *Decoder) {
252 return decodeRegisterClass(Inst, RegNo, QFRegs);
255 #define DecodeQSRCRegisterClass DecodeQFRCRegisterClass
256 #define DecodeQBRCRegisterClass DecodeQFRCRegisterClass
259 static DecodeStatus decodeUImmOperand(MCInst &Inst, uint64_t Imm,
260 int64_t Address, const void *Decoder) {
261 assert(isUInt<N>(Imm) && "Invalid immediate");
262 Inst.addOperand(MCOperand::CreateImm(Imm));
263 return MCDisassembler::Success;
267 static DecodeStatus decodeSImmOperand(MCInst &Inst, uint64_t Imm,
268 int64_t Address, const void *Decoder) {
269 assert(isUInt<N>(Imm) && "Invalid immediate");
270 Inst.addOperand(MCOperand::CreateImm(SignExtend64<N>(Imm)));
271 return MCDisassembler::Success;
274 static DecodeStatus decodeMemRIOperands(MCInst &Inst, uint64_t Imm,
275 int64_t Address, const void *Decoder) {
276 // Decode the memri field (imm, reg), which has the low 16-bits as the
277 // displacement and the next 5 bits as the register #.
279 uint64_t Base = Imm >> 16;
280 uint64_t Disp = Imm & 0xFFFF;
282 assert(Base < 32 && "Invalid base register");
284 switch (Inst.getOpcode()) {
292 // Add the tied output operand.
293 Inst.addOperand(MCOperand::CreateReg(GP0Regs[Base]));
300 Inst.insert(Inst.begin(), MCOperand::CreateReg(GP0Regs[Base]));
304 Inst.addOperand(MCOperand::CreateImm(SignExtend64<16>(Disp)));
305 Inst.addOperand(MCOperand::CreateReg(GP0Regs[Base]));
306 return MCDisassembler::Success;
309 static DecodeStatus decodeMemRIXOperands(MCInst &Inst, uint64_t Imm,
310 int64_t Address, const void *Decoder) {
311 // Decode the memrix field (imm, reg), which has the low 14-bits as the
312 // displacement and the next 5 bits as the register #.
314 uint64_t Base = Imm >> 14;
315 uint64_t Disp = Imm & 0x3FFF;
317 assert(Base < 32 && "Invalid base register");
319 if (Inst.getOpcode() == PPC::LDU)
320 // Add the tied output operand.
321 Inst.addOperand(MCOperand::CreateReg(GP0Regs[Base]));
322 else if (Inst.getOpcode() == PPC::STDU)
323 Inst.insert(Inst.begin(), MCOperand::CreateReg(GP0Regs[Base]));
325 Inst.addOperand(MCOperand::CreateImm(SignExtend64<16>(Disp << 2)));
326 Inst.addOperand(MCOperand::CreateReg(GP0Regs[Base]));
327 return MCDisassembler::Success;
330 static DecodeStatus decodeCRBitMOperand(MCInst &Inst, uint64_t Imm,
331 int64_t Address, const void *Decoder) {
332 // The cr bit encoding is 0x80 >> cr_reg_num.
334 unsigned Zeros = countTrailingZeros(Imm);
335 assert(Zeros < 8 && "Invalid CR bit value");
337 Inst.addOperand(MCOperand::CreateReg(CRRegs[7 - Zeros]));
338 return MCDisassembler::Success;
341 #include "PPCGenDisassemblerTables.inc"
343 DecodeStatus PPCDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
344 ArrayRef<uint8_t> Bytes,
345 uint64_t Address, raw_ostream &OS,
346 raw_ostream &CS) const {
347 // Get the four bytes of the instruction.
349 if (Bytes.size() < 4) {
351 return MCDisassembler::Fail;
354 // The instruction is big-endian encoded.
356 (Bytes[0] << 24) | (Bytes[1] << 16) | (Bytes[2] << 8) | (Bytes[3] << 0);
358 if ((STI.getFeatureBits() & PPC::FeatureQPX) != 0) {
359 DecodeStatus result =
360 decodeInstruction(DecoderTableQPX32, MI, Inst, Address, this, STI);
361 if (result != MCDisassembler::Fail)
367 return decodeInstruction(DecoderTable32, MI, Inst, Address, this, STI);