12345347eSHal Finkel //===------ PPCDisassembler.cpp - Disassembler for PowerPC ------*- C++ -*-===//
22345347eSHal Finkel //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62345347eSHal Finkel //
72345347eSHal Finkel //===----------------------------------------------------------------------===//
82345347eSHal Finkel 
927c769d2SBenjamin Kramer #include "MCTargetDesc/PPCMCTargetDesc.h"
10ee6ced19SRichard Trieu #include "TargetInfo/PowerPCTargetInfo.h"
11f57c1977SBenjamin Kramer #include "llvm/MC/MCDisassembler/MCDisassembler.h"
122345347eSHal Finkel #include "llvm/MC/MCFixedLenDisassembler.h"
132345347eSHal Finkel #include "llvm/MC/MCInst.h"
142345347eSHal Finkel #include "llvm/MC/MCSubtargetInfo.h"
15c11fd3e7SBenjamin Kramer #include "llvm/Support/Endian.h"
162345347eSHal Finkel #include "llvm/Support/TargetRegistry.h"
172345347eSHal Finkel 
182345347eSHal Finkel using namespace llvm;
192345347eSHal Finkel 
200dad994aSNemanja Ivanovic DEFINE_PPC_REGCLASSES;
210dad994aSNemanja Ivanovic 
22e96dd897SChandler Carruth #define DEBUG_TYPE "ppc-disassembler"
23e96dd897SChandler Carruth 
242345347eSHal Finkel typedef MCDisassembler::DecodeStatus DecodeStatus;
252345347eSHal Finkel 
262345347eSHal Finkel namespace {
272345347eSHal Finkel class PPCDisassembler : public MCDisassembler {
28c11fd3e7SBenjamin Kramer   bool IsLittleEndian;
29c11fd3e7SBenjamin Kramer 
302345347eSHal Finkel public:
31c11fd3e7SBenjamin Kramer   PPCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
32c11fd3e7SBenjamin Kramer                   bool IsLittleEndian)
33c11fd3e7SBenjamin Kramer       : MCDisassembler(STI, Ctx), IsLittleEndian(IsLittleEndian) {}
342345347eSHal Finkel 
354aa6bea7SRafael Espindola   DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
367fc5b874SRafael Espindola                               ArrayRef<uint8_t> Bytes, uint64_t Address,
374aa6bea7SRafael Espindola                               raw_ostream &CStream) const override;
382345347eSHal Finkel };
392345347eSHal Finkel } // end anonymous namespace
402345347eSHal Finkel 
412345347eSHal Finkel static MCDisassembler *createPPCDisassembler(const Target &T,
42a1bc0f56SLang Hames                                              const MCSubtargetInfo &STI,
43a1bc0f56SLang Hames                                              MCContext &Ctx) {
44c11fd3e7SBenjamin Kramer   return new PPCDisassembler(STI, Ctx, /*IsLittleEndian=*/false);
45c11fd3e7SBenjamin Kramer }
46c11fd3e7SBenjamin Kramer 
47c11fd3e7SBenjamin Kramer static MCDisassembler *createPPCLEDisassembler(const Target &T,
48c11fd3e7SBenjamin Kramer                                                const MCSubtargetInfo &STI,
49c11fd3e7SBenjamin Kramer                                                MCContext &Ctx) {
50c11fd3e7SBenjamin Kramer   return new PPCDisassembler(STI, Ctx, /*IsLittleEndian=*/true);
512345347eSHal Finkel }
522345347eSHal Finkel 
530dbcb363STom Stellard extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCDisassembler() {
542345347eSHal Finkel   // Register the disassembler for each target.
55f42454b9SMehdi Amini   TargetRegistry::RegisterMCDisassembler(getThePPC32Target(),
562345347eSHal Finkel                                          createPPCDisassembler);
57f42454b9SMehdi Amini   TargetRegistry::RegisterMCDisassembler(getThePPC64Target(),
582345347eSHal Finkel                                          createPPCDisassembler);
59f42454b9SMehdi Amini   TargetRegistry::RegisterMCDisassembler(getThePPC64LETarget(),
60c11fd3e7SBenjamin Kramer                                          createPPCLEDisassembler);
612345347eSHal Finkel }
622345347eSHal Finkel 
63c0694520SSean Fertile static DecodeStatus DecodePCRel24BranchTarget(MCInst &Inst, unsigned Imm,
64c0694520SSean Fertile                                               uint64_t Addr,
65c0694520SSean Fertile                                               const void *Decoder) {
66c0694520SSean Fertile   int32_t Offset = SignExtend32<24>(Imm);
67c0694520SSean Fertile   Inst.addOperand(MCOperand::createImm(Offset));
68c0694520SSean Fertile   return MCDisassembler::Success;
69c0694520SSean Fertile }
70c0694520SSean Fertile 
712345347eSHal Finkel // FIXME: These can be generated by TableGen from the existing register
722345347eSHal Finkel // encoding values!
732345347eSHal Finkel 
742345347eSHal Finkel template <std::size_t N>
752345347eSHal Finkel static DecodeStatus decodeRegisterClass(MCInst &Inst, uint64_t RegNo,
760dad994aSNemanja Ivanovic                                         const MCPhysReg (&Regs)[N]) {
772345347eSHal Finkel   assert(RegNo < N && "Invalid register number");
78e9119e41SJim Grosbach   Inst.addOperand(MCOperand::createReg(Regs[RegNo]));
792345347eSHal Finkel   return MCDisassembler::Success;
802345347eSHal Finkel }
812345347eSHal Finkel 
822345347eSHal Finkel static DecodeStatus DecodeCRRCRegisterClass(MCInst &Inst, uint64_t RegNo,
832345347eSHal Finkel                                             uint64_t Address,
842345347eSHal Finkel                                             const void *Decoder) {
852345347eSHal Finkel   return decodeRegisterClass(Inst, RegNo, CRRegs);
862345347eSHal Finkel }
872345347eSHal Finkel 
882345347eSHal Finkel static DecodeStatus DecodeCRBITRCRegisterClass(MCInst &Inst, uint64_t RegNo,
892345347eSHal Finkel                                             uint64_t Address,
902345347eSHal Finkel                                             const void *Decoder) {
912345347eSHal Finkel   return decodeRegisterClass(Inst, RegNo, CRBITRegs);
922345347eSHal Finkel }
932345347eSHal Finkel 
942345347eSHal Finkel static DecodeStatus DecodeF4RCRegisterClass(MCInst &Inst, uint64_t RegNo,
952345347eSHal Finkel                                             uint64_t Address,
962345347eSHal Finkel                                             const void *Decoder) {
972345347eSHal Finkel   return decodeRegisterClass(Inst, RegNo, FRegs);
982345347eSHal Finkel }
992345347eSHal Finkel 
1002345347eSHal Finkel static DecodeStatus DecodeF8RCRegisterClass(MCInst &Inst, uint64_t RegNo,
1012345347eSHal Finkel                                             uint64_t Address,
1022345347eSHal Finkel                                             const void *Decoder) {
1032345347eSHal Finkel   return decodeRegisterClass(Inst, RegNo, FRegs);
1042345347eSHal Finkel }
1052345347eSHal Finkel 
10611049f8fSNemanja Ivanovic static DecodeStatus DecodeVFRCRegisterClass(MCInst &Inst, uint64_t RegNo,
10711049f8fSNemanja Ivanovic                                             uint64_t Address,
10811049f8fSNemanja Ivanovic                                             const void *Decoder) {
10911049f8fSNemanja Ivanovic   return decodeRegisterClass(Inst, RegNo, VFRegs);
11011049f8fSNemanja Ivanovic }
11111049f8fSNemanja Ivanovic 
1122345347eSHal Finkel static DecodeStatus DecodeVRRCRegisterClass(MCInst &Inst, uint64_t RegNo,
1132345347eSHal Finkel                                             uint64_t Address,
1142345347eSHal Finkel                                             const void *Decoder) {
1152345347eSHal Finkel   return decodeRegisterClass(Inst, RegNo, VRegs);
1162345347eSHal Finkel }
1172345347eSHal Finkel 
11827774d92SHal Finkel static DecodeStatus DecodeVSRCRegisterClass(MCInst &Inst, uint64_t RegNo,
11927774d92SHal Finkel                                             uint64_t Address,
12027774d92SHal Finkel                                             const void *Decoder) {
12127774d92SHal Finkel   return decodeRegisterClass(Inst, RegNo, VSRegs);
12227774d92SHal Finkel }
12327774d92SHal Finkel 
12419be506aSHal Finkel static DecodeStatus DecodeVSFRCRegisterClass(MCInst &Inst, uint64_t RegNo,
12519be506aSHal Finkel                                             uint64_t Address,
12619be506aSHal Finkel                                             const void *Decoder) {
12719be506aSHal Finkel   return decodeRegisterClass(Inst, RegNo, VSFRegs);
12819be506aSHal Finkel }
12919be506aSHal Finkel 
130f3c94b1eSNemanja Ivanovic static DecodeStatus DecodeVSSRCRegisterClass(MCInst &Inst, uint64_t RegNo,
131f3c94b1eSNemanja Ivanovic                                             uint64_t Address,
132f3c94b1eSNemanja Ivanovic                                             const void *Decoder) {
133f3c94b1eSNemanja Ivanovic   return decodeRegisterClass(Inst, RegNo, VSSRegs);
134f3c94b1eSNemanja Ivanovic }
135f3c94b1eSNemanja Ivanovic 
1362345347eSHal Finkel static DecodeStatus DecodeGPRCRegisterClass(MCInst &Inst, uint64_t RegNo,
1372345347eSHal Finkel                                             uint64_t Address,
1382345347eSHal Finkel                                             const void *Decoder) {
1390dad994aSNemanja Ivanovic   return decodeRegisterClass(Inst, RegNo, RRegs);
1402345347eSHal Finkel }
1412345347eSHal Finkel 
1422345347eSHal Finkel static DecodeStatus DecodeGPRC_NOR0RegisterClass(MCInst &Inst, uint64_t RegNo,
1432345347eSHal Finkel                                             uint64_t Address,
1442345347eSHal Finkel                                             const void *Decoder) {
1450dad994aSNemanja Ivanovic   return decodeRegisterClass(Inst, RegNo, RRegsNoR0);
1462345347eSHal Finkel }
1472345347eSHal Finkel 
1482345347eSHal Finkel static DecodeStatus DecodeG8RCRegisterClass(MCInst &Inst, uint64_t RegNo,
1492345347eSHal Finkel                                             uint64_t Address,
1502345347eSHal Finkel                                             const void *Decoder) {
1510dad994aSNemanja Ivanovic   return decodeRegisterClass(Inst, RegNo, XRegs);
1522345347eSHal Finkel }
1532345347eSHal Finkel 
15422e7da95SGuozhi Wei static DecodeStatus DecodeG8RC_NOX0RegisterClass(MCInst &Inst, uint64_t RegNo,
15522e7da95SGuozhi Wei                                             uint64_t Address,
15622e7da95SGuozhi Wei                                             const void *Decoder) {
1570dad994aSNemanja Ivanovic   return decodeRegisterClass(Inst, RegNo, XRegsNoX0);
15822e7da95SGuozhi Wei }
15922e7da95SGuozhi Wei 
1602345347eSHal Finkel #define DecodePointerLikeRegClass0 DecodeGPRCRegisterClass
1612345347eSHal Finkel #define DecodePointerLikeRegClass1 DecodeGPRC_NOR0RegisterClass
1622345347eSHal Finkel 
163c93a9a2cSHal Finkel static DecodeStatus DecodeQFRCRegisterClass(MCInst &Inst, uint64_t RegNo,
164c93a9a2cSHal Finkel                                             uint64_t Address,
165c93a9a2cSHal Finkel                                             const void *Decoder) {
166c93a9a2cSHal Finkel   return decodeRegisterClass(Inst, RegNo, QFRegs);
167c93a9a2cSHal Finkel }
168c93a9a2cSHal Finkel 
169d52990c7SJustin Hibbits static DecodeStatus DecodeSPERCRegisterClass(MCInst &Inst, uint64_t RegNo,
170d52990c7SJustin Hibbits                                             uint64_t Address,
171d52990c7SJustin Hibbits                                             const void *Decoder) {
172d52990c7SJustin Hibbits   return decodeRegisterClass(Inst, RegNo, SPERegs);
173d52990c7SJustin Hibbits }
174d52990c7SJustin Hibbits 
175c93a9a2cSHal Finkel #define DecodeQSRCRegisterClass DecodeQFRCRegisterClass
176c93a9a2cSHal Finkel #define DecodeQBRCRegisterClass DecodeQFRCRegisterClass
177c93a9a2cSHal Finkel 
1782345347eSHal Finkel template<unsigned N>
1792345347eSHal Finkel static DecodeStatus decodeUImmOperand(MCInst &Inst, uint64_t Imm,
1802345347eSHal Finkel                                       int64_t Address, const void *Decoder) {
1812345347eSHal Finkel   assert(isUInt<N>(Imm) && "Invalid immediate");
182e9119e41SJim Grosbach   Inst.addOperand(MCOperand::createImm(Imm));
1832345347eSHal Finkel   return MCDisassembler::Success;
1842345347eSHal Finkel }
1852345347eSHal Finkel 
1862345347eSHal Finkel template<unsigned N>
1872345347eSHal Finkel static DecodeStatus decodeSImmOperand(MCInst &Inst, uint64_t Imm,
1882345347eSHal Finkel                                       int64_t Address, const void *Decoder) {
1892345347eSHal Finkel   assert(isUInt<N>(Imm) && "Invalid immediate");
190e9119e41SJim Grosbach   Inst.addOperand(MCOperand::createImm(SignExtend64<N>(Imm)));
1912345347eSHal Finkel   return MCDisassembler::Success;
1922345347eSHal Finkel }
1932345347eSHal Finkel 
1945cee3401SVictor Huang static DecodeStatus decodeImmZeroOperand(MCInst &Inst, uint64_t Imm,
1955cee3401SVictor Huang                                          int64_t Address, const void *Decoder) {
1965cee3401SVictor Huang   if (Imm != 0)
1975cee3401SVictor Huang     return MCDisassembler::Fail;
1985cee3401SVictor Huang   Inst.addOperand(MCOperand::createImm(Imm));
1995cee3401SVictor Huang   return MCDisassembler::Success;
2005cee3401SVictor Huang }
2015cee3401SVictor Huang 
2022345347eSHal Finkel static DecodeStatus decodeMemRIOperands(MCInst &Inst, uint64_t Imm,
2032345347eSHal Finkel                                         int64_t Address, const void *Decoder) {
2042345347eSHal Finkel   // Decode the memri field (imm, reg), which has the low 16-bits as the
2052345347eSHal Finkel   // displacement and the next 5 bits as the register #.
2062345347eSHal Finkel 
2072345347eSHal Finkel   uint64_t Base = Imm >> 16;
2082345347eSHal Finkel   uint64_t Disp = Imm & 0xFFFF;
2092345347eSHal Finkel 
2102345347eSHal Finkel   assert(Base < 32 && "Invalid base register");
2112345347eSHal Finkel 
2122345347eSHal Finkel   switch (Inst.getOpcode()) {
2132345347eSHal Finkel   default: break;
2142345347eSHal Finkel   case PPC::LBZU:
2152345347eSHal Finkel   case PPC::LHAU:
2162345347eSHal Finkel   case PPC::LHZU:
2172345347eSHal Finkel   case PPC::LWZU:
2182345347eSHal Finkel   case PPC::LFSU:
2192345347eSHal Finkel   case PPC::LFDU:
2202345347eSHal Finkel     // Add the tied output operand.
2210dad994aSNemanja Ivanovic     Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
2222345347eSHal Finkel     break;
2232345347eSHal Finkel   case PPC::STBU:
2242345347eSHal Finkel   case PPC::STHU:
2252345347eSHal Finkel   case PPC::STWU:
2262345347eSHal Finkel   case PPC::STFSU:
2272345347eSHal Finkel   case PPC::STFDU:
2280dad994aSNemanja Ivanovic     Inst.insert(Inst.begin(), MCOperand::createReg(RRegsNoR0[Base]));
2292345347eSHal Finkel     break;
2302345347eSHal Finkel   }
2312345347eSHal Finkel 
232e9119e41SJim Grosbach   Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp)));
2330dad994aSNemanja Ivanovic   Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
2342345347eSHal Finkel   return MCDisassembler::Success;
2352345347eSHal Finkel }
2362345347eSHal Finkel 
2372345347eSHal Finkel static DecodeStatus decodeMemRIXOperands(MCInst &Inst, uint64_t Imm,
2382345347eSHal Finkel                                          int64_t Address, const void *Decoder) {
2392345347eSHal Finkel   // Decode the memrix field (imm, reg), which has the low 14-bits as the
2402345347eSHal Finkel   // displacement and the next 5 bits as the register #.
2412345347eSHal Finkel 
2422345347eSHal Finkel   uint64_t Base = Imm >> 14;
2432345347eSHal Finkel   uint64_t Disp = Imm & 0x3FFF;
2442345347eSHal Finkel 
2452345347eSHal Finkel   assert(Base < 32 && "Invalid base register");
2462345347eSHal Finkel 
2472345347eSHal Finkel   if (Inst.getOpcode() == PPC::LDU)
2482345347eSHal Finkel     // Add the tied output operand.
2490dad994aSNemanja Ivanovic     Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
2502345347eSHal Finkel   else if (Inst.getOpcode() == PPC::STDU)
2510dad994aSNemanja Ivanovic     Inst.insert(Inst.begin(), MCOperand::createReg(RRegsNoR0[Base]));
2522345347eSHal Finkel 
253e9119e41SJim Grosbach   Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp << 2)));
2540dad994aSNemanja Ivanovic   Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
2552345347eSHal Finkel   return MCDisassembler::Success;
2562345347eSHal Finkel }
2572345347eSHal Finkel 
258ba532dc8SKit Barton static DecodeStatus decodeMemRIX16Operands(MCInst &Inst, uint64_t Imm,
259ba532dc8SKit Barton                                          int64_t Address, const void *Decoder) {
260ba532dc8SKit Barton   // Decode the memrix16 field (imm, reg), which has the low 12-bits as the
261ba532dc8SKit Barton   // displacement with 16-byte aligned, and the next 5 bits as the register #.
262ba532dc8SKit Barton 
263ba532dc8SKit Barton   uint64_t Base = Imm >> 12;
264ba532dc8SKit Barton   uint64_t Disp = Imm & 0xFFF;
265ba532dc8SKit Barton 
266ba532dc8SKit Barton   assert(Base < 32 && "Invalid base register");
267ba532dc8SKit Barton 
268ba532dc8SKit Barton   Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp << 4)));
2690dad994aSNemanja Ivanovic   Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
270ba532dc8SKit Barton   return MCDisassembler::Success;
271ba532dc8SKit Barton }
272ba532dc8SKit Barton 
273*4b414d9aSVictor Huang static DecodeStatus decodeMemRI34PCRelOperands(MCInst &Inst, uint64_t Imm,
274*4b414d9aSVictor Huang                                                int64_t Address,
275*4b414d9aSVictor Huang                                                const void *Decoder) {
276*4b414d9aSVictor Huang   // Decode the memri34_pcrel field (imm, reg), which has the low 34-bits as the
277*4b414d9aSVictor Huang   // displacement, and the next 5 bits as an immediate 0.
278*4b414d9aSVictor Huang   uint64_t Base = Imm >> 34;
279*4b414d9aSVictor Huang   uint64_t Disp = Imm & 0x3FFFFFFFFUL;
280*4b414d9aSVictor Huang 
281*4b414d9aSVictor Huang   assert(Base < 32 && "Invalid base register");
282*4b414d9aSVictor Huang 
283*4b414d9aSVictor Huang   Inst.addOperand(MCOperand::createImm(SignExtend64<34>(Disp)));
284*4b414d9aSVictor Huang   return decodeImmZeroOperand(Inst, Base, Address, Decoder);
285*4b414d9aSVictor Huang }
286*4b414d9aSVictor Huang 
287*4b414d9aSVictor Huang static DecodeStatus decodeMemRI34Operands(MCInst &Inst, uint64_t Imm,
288*4b414d9aSVictor Huang                                           int64_t Address,
289*4b414d9aSVictor Huang                                           const void *Decoder) {
290*4b414d9aSVictor Huang   // Decode the memri34 field (imm, reg), which has the low 34-bits as the
291*4b414d9aSVictor Huang   // displacement, and the next 5 bits as the register #.
292*4b414d9aSVictor Huang   uint64_t Base = Imm >> 34;
293*4b414d9aSVictor Huang   uint64_t Disp = Imm & 0x3FFFFFFFFUL;
294*4b414d9aSVictor Huang 
295*4b414d9aSVictor Huang   assert(Base < 32 && "Invalid base register");
296*4b414d9aSVictor Huang 
297*4b414d9aSVictor Huang   Inst.addOperand(MCOperand::createImm(SignExtend64<34>(Disp)));
298*4b414d9aSVictor Huang   Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
299*4b414d9aSVictor Huang   return MCDisassembler::Success;
300*4b414d9aSVictor Huang }
301*4b414d9aSVictor Huang 
3024fa4fa6aSJustin Hibbits static DecodeStatus decodeSPE8Operands(MCInst &Inst, uint64_t Imm,
3034fa4fa6aSJustin Hibbits                                          int64_t Address, const void *Decoder) {
3044fa4fa6aSJustin Hibbits   // Decode the spe8disp field (imm, reg), which has the low 5-bits as the
3054fa4fa6aSJustin Hibbits   // displacement with 8-byte aligned, and the next 5 bits as the register #.
3064fa4fa6aSJustin Hibbits 
3074fa4fa6aSJustin Hibbits   uint64_t Base = Imm >> 5;
3084fa4fa6aSJustin Hibbits   uint64_t Disp = Imm & 0x1F;
3094fa4fa6aSJustin Hibbits 
3104fa4fa6aSJustin Hibbits   assert(Base < 32 && "Invalid base register");
3114fa4fa6aSJustin Hibbits 
3124fa4fa6aSJustin Hibbits   Inst.addOperand(MCOperand::createImm(Disp << 3));
3130dad994aSNemanja Ivanovic   Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
3144fa4fa6aSJustin Hibbits   return MCDisassembler::Success;
3154fa4fa6aSJustin Hibbits }
3164fa4fa6aSJustin Hibbits 
3174fa4fa6aSJustin Hibbits static DecodeStatus decodeSPE4Operands(MCInst &Inst, uint64_t Imm,
3184fa4fa6aSJustin Hibbits                                          int64_t Address, const void *Decoder) {
3194fa4fa6aSJustin Hibbits   // Decode the spe4disp field (imm, reg), which has the low 5-bits as the
3204fa4fa6aSJustin Hibbits   // displacement with 4-byte aligned, and the next 5 bits as the register #.
3214fa4fa6aSJustin Hibbits 
3224fa4fa6aSJustin Hibbits   uint64_t Base = Imm >> 5;
3234fa4fa6aSJustin Hibbits   uint64_t Disp = Imm & 0x1F;
3244fa4fa6aSJustin Hibbits 
3254fa4fa6aSJustin Hibbits   assert(Base < 32 && "Invalid base register");
3264fa4fa6aSJustin Hibbits 
3274fa4fa6aSJustin Hibbits   Inst.addOperand(MCOperand::createImm(Disp << 2));
3280dad994aSNemanja Ivanovic   Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
3294fa4fa6aSJustin Hibbits   return MCDisassembler::Success;
3304fa4fa6aSJustin Hibbits }
3314fa4fa6aSJustin Hibbits 
3324fa4fa6aSJustin Hibbits static DecodeStatus decodeSPE2Operands(MCInst &Inst, uint64_t Imm,
3334fa4fa6aSJustin Hibbits                                          int64_t Address, const void *Decoder) {
3344fa4fa6aSJustin Hibbits   // Decode the spe2disp field (imm, reg), which has the low 5-bits as the
3354fa4fa6aSJustin Hibbits   // displacement with 2-byte aligned, and the next 5 bits as the register #.
3364fa4fa6aSJustin Hibbits 
3374fa4fa6aSJustin Hibbits   uint64_t Base = Imm >> 5;
3384fa4fa6aSJustin Hibbits   uint64_t Disp = Imm & 0x1F;
3394fa4fa6aSJustin Hibbits 
3404fa4fa6aSJustin Hibbits   assert(Base < 32 && "Invalid base register");
3414fa4fa6aSJustin Hibbits 
3424fa4fa6aSJustin Hibbits   Inst.addOperand(MCOperand::createImm(Disp << 1));
3430dad994aSNemanja Ivanovic   Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
3444fa4fa6aSJustin Hibbits   return MCDisassembler::Success;
3454fa4fa6aSJustin Hibbits }
3464fa4fa6aSJustin Hibbits 
3472345347eSHal Finkel static DecodeStatus decodeCRBitMOperand(MCInst &Inst, uint64_t Imm,
3482345347eSHal Finkel                                         int64_t Address, const void *Decoder) {
3492345347eSHal Finkel   // The cr bit encoding is 0x80 >> cr_reg_num.
3502345347eSHal Finkel 
3512345347eSHal Finkel   unsigned Zeros = countTrailingZeros(Imm);
3522345347eSHal Finkel   assert(Zeros < 8 && "Invalid CR bit value");
3532345347eSHal Finkel 
354e9119e41SJim Grosbach   Inst.addOperand(MCOperand::createReg(CRRegs[7 - Zeros]));
3552345347eSHal Finkel   return MCDisassembler::Success;
3562345347eSHal Finkel }
3572345347eSHal Finkel 
3582345347eSHal Finkel #include "PPCGenDisassemblerTables.inc"
3592345347eSHal Finkel 
3602345347eSHal Finkel DecodeStatus PPCDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
3617fc5b874SRafael Espindola                                              ArrayRef<uint8_t> Bytes,
3626fdd6a7bSFangrui Song                                              uint64_t Address,
3634aa6bea7SRafael Espindola                                              raw_ostream &CS) const {
3645cee3401SVictor Huang   auto *ReadFunc = IsLittleEndian ? support::endian::read32le
3655cee3401SVictor Huang                                   : support::endian::read32be;
3665cee3401SVictor Huang 
3675cee3401SVictor Huang   // If this is an 8-byte prefixed instruction, handle it here.
3685cee3401SVictor Huang   // Note: prefixed instructions aren't technically 8-byte entities - the prefix
3695cee3401SVictor Huang   //       appears in memory at an address 4 bytes prior to that of the base
3705cee3401SVictor Huang   //       instruction regardless of endianness. So we read the two pieces and
3715cee3401SVictor Huang   //       rebuild the 8-byte instruction.
3725cee3401SVictor Huang   // TODO: In this function we call decodeInstruction several times with
3735cee3401SVictor Huang   //       different decoder tables. It may be possible to only call once by
3745cee3401SVictor Huang   //       looking at the top 6 bits of the instruction.
3755cee3401SVictor Huang   if (STI.getFeatureBits()[PPC::FeaturePrefixInstrs] && Bytes.size() >= 8) {
3765cee3401SVictor Huang     uint32_t Prefix = ReadFunc(Bytes.data());
3775cee3401SVictor Huang     uint32_t BaseInst = ReadFunc(Bytes.data() + 4);
3785cee3401SVictor Huang     uint64_t Inst = BaseInst | (uint64_t)Prefix << 32;
3795cee3401SVictor Huang     DecodeStatus result = decodeInstruction(DecoderTable64, MI, Inst, Address,
3805cee3401SVictor Huang                                             this, STI);
3815cee3401SVictor Huang     if (result != MCDisassembler::Fail) {
3825cee3401SVictor Huang       Size = 8;
3835cee3401SVictor Huang       return result;
3845cee3401SVictor Huang     }
3855cee3401SVictor Huang   }
3865cee3401SVictor Huang 
3872345347eSHal Finkel   // Get the four bytes of the instruction.
3882345347eSHal Finkel   Size = 4;
3897fc5b874SRafael Espindola   if (Bytes.size() < 4) {
3902345347eSHal Finkel     Size = 0;
3912345347eSHal Finkel     return MCDisassembler::Fail;
3922345347eSHal Finkel   }
3932345347eSHal Finkel 
394c11fd3e7SBenjamin Kramer   // Read the instruction in the proper endianness.
3955cee3401SVictor Huang   uint64_t Inst = ReadFunc(Bytes.data());
3962345347eSHal Finkel 
397db0712f9SMichael Kuperstein   if (STI.getFeatureBits()[PPC::FeatureQPX]) {
398c93a9a2cSHal Finkel     DecodeStatus result =
399c93a9a2cSHal Finkel       decodeInstruction(DecoderTableQPX32, MI, Inst, Address, this, STI);
400c93a9a2cSHal Finkel     if (result != MCDisassembler::Fail)
401c93a9a2cSHal Finkel       return result;
4024fa4fa6aSJustin Hibbits   } else if (STI.getFeatureBits()[PPC::FeatureSPE]) {
4034fa4fa6aSJustin Hibbits     DecodeStatus result =
4044fa4fa6aSJustin Hibbits       decodeInstruction(DecoderTableSPE32, MI, Inst, Address, this, STI);
4054fa4fa6aSJustin Hibbits     if (result != MCDisassembler::Fail)
4064fa4fa6aSJustin Hibbits       return result;
407c93a9a2cSHal Finkel   }
408c93a9a2cSHal Finkel 
4092345347eSHal Finkel   return decodeInstruction(DecoderTable32, MI, Inst, Address, this, STI);
4102345347eSHal Finkel }
411