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"
10*ee6ced19SRichard 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 &VStream,
384aa6bea7SRafael Espindola                               raw_ostream &CStream) const override;
392345347eSHal Finkel };
402345347eSHal Finkel } // end anonymous namespace
412345347eSHal Finkel 
422345347eSHal Finkel static MCDisassembler *createPPCDisassembler(const Target &T,
43a1bc0f56SLang Hames                                              const MCSubtargetInfo &STI,
44a1bc0f56SLang Hames                                              MCContext &Ctx) {
45c11fd3e7SBenjamin Kramer   return new PPCDisassembler(STI, Ctx, /*IsLittleEndian=*/false);
46c11fd3e7SBenjamin Kramer }
47c11fd3e7SBenjamin Kramer 
48c11fd3e7SBenjamin Kramer static MCDisassembler *createPPCLEDisassembler(const Target &T,
49c11fd3e7SBenjamin Kramer                                                const MCSubtargetInfo &STI,
50c11fd3e7SBenjamin Kramer                                                MCContext &Ctx) {
51c11fd3e7SBenjamin Kramer   return new PPCDisassembler(STI, Ctx, /*IsLittleEndian=*/true);
522345347eSHal Finkel }
532345347eSHal Finkel 
542345347eSHal Finkel extern "C" void LLVMInitializePowerPCDisassembler() {
552345347eSHal Finkel   // Register the disassembler for each target.
56f42454b9SMehdi Amini   TargetRegistry::RegisterMCDisassembler(getThePPC32Target(),
572345347eSHal Finkel                                          createPPCDisassembler);
58f42454b9SMehdi Amini   TargetRegistry::RegisterMCDisassembler(getThePPC64Target(),
592345347eSHal Finkel                                          createPPCDisassembler);
60f42454b9SMehdi Amini   TargetRegistry::RegisterMCDisassembler(getThePPC64LETarget(),
61c11fd3e7SBenjamin Kramer                                          createPPCLEDisassembler);
622345347eSHal Finkel }
632345347eSHal Finkel 
64c0694520SSean Fertile static DecodeStatus DecodePCRel24BranchTarget(MCInst &Inst, unsigned Imm,
65c0694520SSean Fertile                                               uint64_t Addr,
66c0694520SSean Fertile                                               const void *Decoder) {
67c0694520SSean Fertile   int32_t Offset = SignExtend32<24>(Imm);
68c0694520SSean Fertile   Inst.addOperand(MCOperand::createImm(Offset));
69c0694520SSean Fertile   return MCDisassembler::Success;
70c0694520SSean Fertile }
71c0694520SSean Fertile 
722345347eSHal Finkel // FIXME: These can be generated by TableGen from the existing register
732345347eSHal Finkel // encoding values!
742345347eSHal Finkel 
752345347eSHal Finkel template <std::size_t N>
762345347eSHal Finkel static DecodeStatus decodeRegisterClass(MCInst &Inst, uint64_t RegNo,
770dad994aSNemanja Ivanovic                                         const MCPhysReg (&Regs)[N]) {
782345347eSHal Finkel   assert(RegNo < N && "Invalid register number");
79e9119e41SJim Grosbach   Inst.addOperand(MCOperand::createReg(Regs[RegNo]));
802345347eSHal Finkel   return MCDisassembler::Success;
812345347eSHal Finkel }
822345347eSHal Finkel 
832345347eSHal Finkel static DecodeStatus DecodeCRRCRegisterClass(MCInst &Inst, uint64_t RegNo,
842345347eSHal Finkel                                             uint64_t Address,
852345347eSHal Finkel                                             const void *Decoder) {
862345347eSHal Finkel   return decodeRegisterClass(Inst, RegNo, CRRegs);
872345347eSHal Finkel }
882345347eSHal Finkel 
89535e69deSKit Barton static DecodeStatus DecodeCRRC0RegisterClass(MCInst &Inst, uint64_t RegNo,
90535e69deSKit Barton                                             uint64_t Address,
91535e69deSKit Barton                                             const void *Decoder) {
92535e69deSKit Barton   return decodeRegisterClass(Inst, RegNo, CRRegs);
93535e69deSKit Barton }
94535e69deSKit Barton 
952345347eSHal Finkel static DecodeStatus DecodeCRBITRCRegisterClass(MCInst &Inst, uint64_t RegNo,
962345347eSHal Finkel                                             uint64_t Address,
972345347eSHal Finkel                                             const void *Decoder) {
982345347eSHal Finkel   return decodeRegisterClass(Inst, RegNo, CRBITRegs);
992345347eSHal Finkel }
1002345347eSHal Finkel 
1012345347eSHal Finkel static DecodeStatus DecodeF4RCRegisterClass(MCInst &Inst, uint64_t RegNo,
1022345347eSHal Finkel                                             uint64_t Address,
1032345347eSHal Finkel                                             const void *Decoder) {
1042345347eSHal Finkel   return decodeRegisterClass(Inst, RegNo, FRegs);
1052345347eSHal Finkel }
1062345347eSHal Finkel 
1072345347eSHal Finkel static DecodeStatus DecodeF8RCRegisterClass(MCInst &Inst, uint64_t RegNo,
1082345347eSHal Finkel                                             uint64_t Address,
1092345347eSHal Finkel                                             const void *Decoder) {
1102345347eSHal Finkel   return decodeRegisterClass(Inst, RegNo, FRegs);
1112345347eSHal Finkel }
1122345347eSHal Finkel 
11311049f8fSNemanja Ivanovic static DecodeStatus DecodeVFRCRegisterClass(MCInst &Inst, uint64_t RegNo,
11411049f8fSNemanja Ivanovic                                             uint64_t Address,
11511049f8fSNemanja Ivanovic                                             const void *Decoder) {
11611049f8fSNemanja Ivanovic   return decodeRegisterClass(Inst, RegNo, VFRegs);
11711049f8fSNemanja Ivanovic }
11811049f8fSNemanja Ivanovic 
1192345347eSHal Finkel static DecodeStatus DecodeVRRCRegisterClass(MCInst &Inst, uint64_t RegNo,
1202345347eSHal Finkel                                             uint64_t Address,
1212345347eSHal Finkel                                             const void *Decoder) {
1222345347eSHal Finkel   return decodeRegisterClass(Inst, RegNo, VRegs);
1232345347eSHal Finkel }
1242345347eSHal Finkel 
12527774d92SHal Finkel static DecodeStatus DecodeVSRCRegisterClass(MCInst &Inst, uint64_t RegNo,
12627774d92SHal Finkel                                             uint64_t Address,
12727774d92SHal Finkel                                             const void *Decoder) {
12827774d92SHal Finkel   return decodeRegisterClass(Inst, RegNo, VSRegs);
12927774d92SHal Finkel }
13027774d92SHal Finkel 
13119be506aSHal Finkel static DecodeStatus DecodeVSFRCRegisterClass(MCInst &Inst, uint64_t RegNo,
13219be506aSHal Finkel                                             uint64_t Address,
13319be506aSHal Finkel                                             const void *Decoder) {
13419be506aSHal Finkel   return decodeRegisterClass(Inst, RegNo, VSFRegs);
13519be506aSHal Finkel }
13619be506aSHal Finkel 
137f3c94b1eSNemanja Ivanovic static DecodeStatus DecodeVSSRCRegisterClass(MCInst &Inst, uint64_t RegNo,
138f3c94b1eSNemanja Ivanovic                                             uint64_t Address,
139f3c94b1eSNemanja Ivanovic                                             const void *Decoder) {
140f3c94b1eSNemanja Ivanovic   return decodeRegisterClass(Inst, RegNo, VSSRegs);
141f3c94b1eSNemanja Ivanovic }
142f3c94b1eSNemanja Ivanovic 
1432345347eSHal Finkel static DecodeStatus DecodeGPRCRegisterClass(MCInst &Inst, uint64_t RegNo,
1442345347eSHal Finkel                                             uint64_t Address,
1452345347eSHal Finkel                                             const void *Decoder) {
1460dad994aSNemanja Ivanovic   return decodeRegisterClass(Inst, RegNo, RRegs);
1472345347eSHal Finkel }
1482345347eSHal Finkel 
1492345347eSHal Finkel static DecodeStatus DecodeGPRC_NOR0RegisterClass(MCInst &Inst, uint64_t RegNo,
1502345347eSHal Finkel                                             uint64_t Address,
1512345347eSHal Finkel                                             const void *Decoder) {
1520dad994aSNemanja Ivanovic   return decodeRegisterClass(Inst, RegNo, RRegsNoR0);
1532345347eSHal Finkel }
1542345347eSHal Finkel 
1552345347eSHal Finkel static DecodeStatus DecodeG8RCRegisterClass(MCInst &Inst, uint64_t RegNo,
1562345347eSHal Finkel                                             uint64_t Address,
1572345347eSHal Finkel                                             const void *Decoder) {
1580dad994aSNemanja Ivanovic   return decodeRegisterClass(Inst, RegNo, XRegs);
1592345347eSHal Finkel }
1602345347eSHal Finkel 
16122e7da95SGuozhi Wei static DecodeStatus DecodeG8RC_NOX0RegisterClass(MCInst &Inst, uint64_t RegNo,
16222e7da95SGuozhi Wei                                             uint64_t Address,
16322e7da95SGuozhi Wei                                             const void *Decoder) {
1640dad994aSNemanja Ivanovic   return decodeRegisterClass(Inst, RegNo, XRegsNoX0);
16522e7da95SGuozhi Wei }
16622e7da95SGuozhi Wei 
1672345347eSHal Finkel #define DecodePointerLikeRegClass0 DecodeGPRCRegisterClass
1682345347eSHal Finkel #define DecodePointerLikeRegClass1 DecodeGPRC_NOR0RegisterClass
1692345347eSHal Finkel 
170c93a9a2cSHal Finkel static DecodeStatus DecodeQFRCRegisterClass(MCInst &Inst, uint64_t RegNo,
171c93a9a2cSHal Finkel                                             uint64_t Address,
172c93a9a2cSHal Finkel                                             const void *Decoder) {
173c93a9a2cSHal Finkel   return decodeRegisterClass(Inst, RegNo, QFRegs);
174c93a9a2cSHal Finkel }
175c93a9a2cSHal Finkel 
176d52990c7SJustin Hibbits static DecodeStatus DecodeSPE4RCRegisterClass(MCInst &Inst, uint64_t RegNo,
177d52990c7SJustin Hibbits                                             uint64_t Address,
178d52990c7SJustin Hibbits                                             const void *Decoder) {
1790dad994aSNemanja Ivanovic   return decodeRegisterClass(Inst, RegNo, RRegs);
180d52990c7SJustin Hibbits }
181d52990c7SJustin Hibbits 
182d52990c7SJustin Hibbits static DecodeStatus DecodeSPERCRegisterClass(MCInst &Inst, uint64_t RegNo,
183d52990c7SJustin Hibbits                                             uint64_t Address,
184d52990c7SJustin Hibbits                                             const void *Decoder) {
185d52990c7SJustin Hibbits   return decodeRegisterClass(Inst, RegNo, SPERegs);
186d52990c7SJustin Hibbits }
187d52990c7SJustin Hibbits 
188c93a9a2cSHal Finkel #define DecodeQSRCRegisterClass DecodeQFRCRegisterClass
189c93a9a2cSHal Finkel #define DecodeQBRCRegisterClass DecodeQFRCRegisterClass
190c93a9a2cSHal Finkel 
1912345347eSHal Finkel template<unsigned N>
1922345347eSHal Finkel static DecodeStatus decodeUImmOperand(MCInst &Inst, uint64_t Imm,
1932345347eSHal Finkel                                       int64_t Address, const void *Decoder) {
1942345347eSHal Finkel   assert(isUInt<N>(Imm) && "Invalid immediate");
195e9119e41SJim Grosbach   Inst.addOperand(MCOperand::createImm(Imm));
1962345347eSHal Finkel   return MCDisassembler::Success;
1972345347eSHal Finkel }
1982345347eSHal Finkel 
1992345347eSHal Finkel template<unsigned N>
2002345347eSHal Finkel static DecodeStatus decodeSImmOperand(MCInst &Inst, uint64_t Imm,
2012345347eSHal Finkel                                       int64_t Address, const void *Decoder) {
2022345347eSHal Finkel   assert(isUInt<N>(Imm) && "Invalid immediate");
203e9119e41SJim Grosbach   Inst.addOperand(MCOperand::createImm(SignExtend64<N>(Imm)));
2042345347eSHal Finkel   return MCDisassembler::Success;
2052345347eSHal Finkel }
2062345347eSHal Finkel 
2072345347eSHal Finkel static DecodeStatus decodeMemRIOperands(MCInst &Inst, uint64_t Imm,
2082345347eSHal Finkel                                         int64_t Address, const void *Decoder) {
2092345347eSHal Finkel   // Decode the memri field (imm, reg), which has the low 16-bits as the
2102345347eSHal Finkel   // displacement and the next 5 bits as the register #.
2112345347eSHal Finkel 
2122345347eSHal Finkel   uint64_t Base = Imm >> 16;
2132345347eSHal Finkel   uint64_t Disp = Imm & 0xFFFF;
2142345347eSHal Finkel 
2152345347eSHal Finkel   assert(Base < 32 && "Invalid base register");
2162345347eSHal Finkel 
2172345347eSHal Finkel   switch (Inst.getOpcode()) {
2182345347eSHal Finkel   default: break;
2192345347eSHal Finkel   case PPC::LBZU:
2202345347eSHal Finkel   case PPC::LHAU:
2212345347eSHal Finkel   case PPC::LHZU:
2222345347eSHal Finkel   case PPC::LWZU:
2232345347eSHal Finkel   case PPC::LFSU:
2242345347eSHal Finkel   case PPC::LFDU:
2252345347eSHal Finkel     // Add the tied output operand.
2260dad994aSNemanja Ivanovic     Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
2272345347eSHal Finkel     break;
2282345347eSHal Finkel   case PPC::STBU:
2292345347eSHal Finkel   case PPC::STHU:
2302345347eSHal Finkel   case PPC::STWU:
2312345347eSHal Finkel   case PPC::STFSU:
2322345347eSHal Finkel   case PPC::STFDU:
2330dad994aSNemanja Ivanovic     Inst.insert(Inst.begin(), MCOperand::createReg(RRegsNoR0[Base]));
2342345347eSHal Finkel     break;
2352345347eSHal Finkel   }
2362345347eSHal Finkel 
237e9119e41SJim Grosbach   Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp)));
2380dad994aSNemanja Ivanovic   Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
2392345347eSHal Finkel   return MCDisassembler::Success;
2402345347eSHal Finkel }
2412345347eSHal Finkel 
2422345347eSHal Finkel static DecodeStatus decodeMemRIXOperands(MCInst &Inst, uint64_t Imm,
2432345347eSHal Finkel                                          int64_t Address, const void *Decoder) {
2442345347eSHal Finkel   // Decode the memrix field (imm, reg), which has the low 14-bits as the
2452345347eSHal Finkel   // displacement and the next 5 bits as the register #.
2462345347eSHal Finkel 
2472345347eSHal Finkel   uint64_t Base = Imm >> 14;
2482345347eSHal Finkel   uint64_t Disp = Imm & 0x3FFF;
2492345347eSHal Finkel 
2502345347eSHal Finkel   assert(Base < 32 && "Invalid base register");
2512345347eSHal Finkel 
2522345347eSHal Finkel   if (Inst.getOpcode() == PPC::LDU)
2532345347eSHal Finkel     // Add the tied output operand.
2540dad994aSNemanja Ivanovic     Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
2552345347eSHal Finkel   else if (Inst.getOpcode() == PPC::STDU)
2560dad994aSNemanja Ivanovic     Inst.insert(Inst.begin(), MCOperand::createReg(RRegsNoR0[Base]));
2572345347eSHal Finkel 
258e9119e41SJim Grosbach   Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp << 2)));
2590dad994aSNemanja Ivanovic   Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
2602345347eSHal Finkel   return MCDisassembler::Success;
2612345347eSHal Finkel }
2622345347eSHal Finkel 
263ba532dc8SKit Barton static DecodeStatus decodeMemRIX16Operands(MCInst &Inst, uint64_t Imm,
264ba532dc8SKit Barton                                          int64_t Address, const void *Decoder) {
265ba532dc8SKit Barton   // Decode the memrix16 field (imm, reg), which has the low 12-bits as the
266ba532dc8SKit Barton   // displacement with 16-byte aligned, and the next 5 bits as the register #.
267ba532dc8SKit Barton 
268ba532dc8SKit Barton   uint64_t Base = Imm >> 12;
269ba532dc8SKit Barton   uint64_t Disp = Imm & 0xFFF;
270ba532dc8SKit Barton 
271ba532dc8SKit Barton   assert(Base < 32 && "Invalid base register");
272ba532dc8SKit Barton 
273ba532dc8SKit Barton   Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp << 4)));
2740dad994aSNemanja Ivanovic   Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
275ba532dc8SKit Barton   return MCDisassembler::Success;
276ba532dc8SKit Barton }
277ba532dc8SKit Barton 
2784fa4fa6aSJustin Hibbits static DecodeStatus decodeSPE8Operands(MCInst &Inst, uint64_t Imm,
2794fa4fa6aSJustin Hibbits                                          int64_t Address, const void *Decoder) {
2804fa4fa6aSJustin Hibbits   // Decode the spe8disp field (imm, reg), which has the low 5-bits as the
2814fa4fa6aSJustin Hibbits   // displacement with 8-byte aligned, and the next 5 bits as the register #.
2824fa4fa6aSJustin Hibbits 
2834fa4fa6aSJustin Hibbits   uint64_t Base = Imm >> 5;
2844fa4fa6aSJustin Hibbits   uint64_t Disp = Imm & 0x1F;
2854fa4fa6aSJustin Hibbits 
2864fa4fa6aSJustin Hibbits   assert(Base < 32 && "Invalid base register");
2874fa4fa6aSJustin Hibbits 
2884fa4fa6aSJustin Hibbits   Inst.addOperand(MCOperand::createImm(Disp << 3));
2890dad994aSNemanja Ivanovic   Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
2904fa4fa6aSJustin Hibbits   return MCDisassembler::Success;
2914fa4fa6aSJustin Hibbits }
2924fa4fa6aSJustin Hibbits 
2934fa4fa6aSJustin Hibbits static DecodeStatus decodeSPE4Operands(MCInst &Inst, uint64_t Imm,
2944fa4fa6aSJustin Hibbits                                          int64_t Address, const void *Decoder) {
2954fa4fa6aSJustin Hibbits   // Decode the spe4disp field (imm, reg), which has the low 5-bits as the
2964fa4fa6aSJustin Hibbits   // displacement with 4-byte aligned, and the next 5 bits as the register #.
2974fa4fa6aSJustin Hibbits 
2984fa4fa6aSJustin Hibbits   uint64_t Base = Imm >> 5;
2994fa4fa6aSJustin Hibbits   uint64_t Disp = Imm & 0x1F;
3004fa4fa6aSJustin Hibbits 
3014fa4fa6aSJustin Hibbits   assert(Base < 32 && "Invalid base register");
3024fa4fa6aSJustin Hibbits 
3034fa4fa6aSJustin Hibbits   Inst.addOperand(MCOperand::createImm(Disp << 2));
3040dad994aSNemanja Ivanovic   Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
3054fa4fa6aSJustin Hibbits   return MCDisassembler::Success;
3064fa4fa6aSJustin Hibbits }
3074fa4fa6aSJustin Hibbits 
3084fa4fa6aSJustin Hibbits static DecodeStatus decodeSPE2Operands(MCInst &Inst, uint64_t Imm,
3094fa4fa6aSJustin Hibbits                                          int64_t Address, const void *Decoder) {
3104fa4fa6aSJustin Hibbits   // Decode the spe2disp field (imm, reg), which has the low 5-bits as the
3114fa4fa6aSJustin Hibbits   // displacement with 2-byte aligned, and the next 5 bits as the register #.
3124fa4fa6aSJustin Hibbits 
3134fa4fa6aSJustin Hibbits   uint64_t Base = Imm >> 5;
3144fa4fa6aSJustin Hibbits   uint64_t Disp = Imm & 0x1F;
3154fa4fa6aSJustin Hibbits 
3164fa4fa6aSJustin Hibbits   assert(Base < 32 && "Invalid base register");
3174fa4fa6aSJustin Hibbits 
3184fa4fa6aSJustin Hibbits   Inst.addOperand(MCOperand::createImm(Disp << 1));
3190dad994aSNemanja Ivanovic   Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base]));
3204fa4fa6aSJustin Hibbits   return MCDisassembler::Success;
3214fa4fa6aSJustin Hibbits }
3224fa4fa6aSJustin Hibbits 
3232345347eSHal Finkel static DecodeStatus decodeCRBitMOperand(MCInst &Inst, uint64_t Imm,
3242345347eSHal Finkel                                         int64_t Address, const void *Decoder) {
3252345347eSHal Finkel   // The cr bit encoding is 0x80 >> cr_reg_num.
3262345347eSHal Finkel 
3272345347eSHal Finkel   unsigned Zeros = countTrailingZeros(Imm);
3282345347eSHal Finkel   assert(Zeros < 8 && "Invalid CR bit value");
3292345347eSHal Finkel 
330e9119e41SJim Grosbach   Inst.addOperand(MCOperand::createReg(CRRegs[7 - Zeros]));
3312345347eSHal Finkel   return MCDisassembler::Success;
3322345347eSHal Finkel }
3332345347eSHal Finkel 
3342345347eSHal Finkel #include "PPCGenDisassemblerTables.inc"
3352345347eSHal Finkel 
3362345347eSHal Finkel DecodeStatus PPCDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
3377fc5b874SRafael Espindola                                              ArrayRef<uint8_t> Bytes,
3384aa6bea7SRafael Espindola                                              uint64_t Address, raw_ostream &OS,
3394aa6bea7SRafael Espindola                                              raw_ostream &CS) const {
3402345347eSHal Finkel   // Get the four bytes of the instruction.
3412345347eSHal Finkel   Size = 4;
3427fc5b874SRafael Espindola   if (Bytes.size() < 4) {
3432345347eSHal Finkel     Size = 0;
3442345347eSHal Finkel     return MCDisassembler::Fail;
3452345347eSHal Finkel   }
3462345347eSHal Finkel 
347c11fd3e7SBenjamin Kramer   // Read the instruction in the proper endianness.
348c11fd3e7SBenjamin Kramer   uint32_t Inst = IsLittleEndian ? support::endian::read32le(Bytes.data())
349c11fd3e7SBenjamin Kramer                                  : support::endian::read32be(Bytes.data());
3502345347eSHal Finkel 
351db0712f9SMichael Kuperstein   if (STI.getFeatureBits()[PPC::FeatureQPX]) {
352c93a9a2cSHal Finkel     DecodeStatus result =
353c93a9a2cSHal Finkel       decodeInstruction(DecoderTableQPX32, MI, Inst, Address, this, STI);
354c93a9a2cSHal Finkel     if (result != MCDisassembler::Fail)
355c93a9a2cSHal Finkel       return result;
3564fa4fa6aSJustin Hibbits   } else if (STI.getFeatureBits()[PPC::FeatureSPE]) {
3574fa4fa6aSJustin Hibbits     DecodeStatus result =
3584fa4fa6aSJustin Hibbits       decodeInstruction(DecoderTableSPE32, MI, Inst, Address, this, STI);
3594fa4fa6aSJustin Hibbits     if (result != MCDisassembler::Fail)
3604fa4fa6aSJustin Hibbits       return result;
361c93a9a2cSHal Finkel   }
362c93a9a2cSHal Finkel 
3632345347eSHal Finkel   return decodeInstruction(DecoderTable32, MI, Inst, Address, this, STI);
3642345347eSHal Finkel }
3652345347eSHal Finkel 
366