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 634af7560bSFangrui Song static DecodeStatus decodeCondBrTarget(MCInst &Inst, unsigned Imm, 644af7560bSFangrui Song uint64_t /*Address*/, 654af7560bSFangrui Song const void * /*Decoder*/) { 664af7560bSFangrui Song Inst.addOperand(MCOperand::createImm(SignExtend32<14>(Imm))); 674af7560bSFangrui Song return MCDisassembler::Success; 684af7560bSFangrui Song } 694af7560bSFangrui Song 7085adce3dSFangrui Song static DecodeStatus decodeDirectBrTarget(MCInst &Inst, unsigned Imm, 7185adce3dSFangrui Song uint64_t /*Address*/, 7285adce3dSFangrui Song const void * /*Decoder*/) { 73c0694520SSean Fertile int32_t Offset = SignExtend32<24>(Imm); 74c0694520SSean Fertile Inst.addOperand(MCOperand::createImm(Offset)); 75c0694520SSean Fertile return MCDisassembler::Success; 76c0694520SSean Fertile } 77c0694520SSean Fertile 782345347eSHal Finkel // FIXME: These can be generated by TableGen from the existing register 792345347eSHal Finkel // encoding values! 802345347eSHal Finkel 812345347eSHal Finkel template <std::size_t N> 822345347eSHal Finkel static DecodeStatus decodeRegisterClass(MCInst &Inst, uint64_t RegNo, 830dad994aSNemanja Ivanovic const MCPhysReg (&Regs)[N]) { 842345347eSHal Finkel assert(RegNo < N && "Invalid register number"); 85e9119e41SJim Grosbach Inst.addOperand(MCOperand::createReg(Regs[RegNo])); 862345347eSHal Finkel return MCDisassembler::Success; 872345347eSHal Finkel } 882345347eSHal Finkel 892345347eSHal Finkel static DecodeStatus DecodeCRRCRegisterClass(MCInst &Inst, uint64_t RegNo, 902345347eSHal Finkel uint64_t Address, 912345347eSHal Finkel const void *Decoder) { 922345347eSHal Finkel return decodeRegisterClass(Inst, RegNo, CRRegs); 932345347eSHal Finkel } 942345347eSHal Finkel 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 170d52990c7SJustin Hibbits static DecodeStatus DecodeSPERCRegisterClass(MCInst &Inst, uint64_t RegNo, 171d52990c7SJustin Hibbits uint64_t Address, 172d52990c7SJustin Hibbits const void *Decoder) { 173d52990c7SJustin Hibbits return decodeRegisterClass(Inst, RegNo, SPERegs); 174d52990c7SJustin Hibbits } 175d52990c7SJustin Hibbits 1769b86b700SBaptiste Saleil static DecodeStatus DecodeACCRCRegisterClass(MCInst &Inst, uint64_t RegNo, 1779b86b700SBaptiste Saleil uint64_t Address, 1789b86b700SBaptiste Saleil const void *Decoder) { 1799b86b700SBaptiste Saleil return decodeRegisterClass(Inst, RegNo, ACCRegs); 1809b86b700SBaptiste Saleil } 1819b86b700SBaptiste Saleil 1821372e23cSBaptiste Saleil static DecodeStatus DecodeVSRpRCRegisterClass(MCInst &Inst, uint64_t RegNo, 1831372e23cSBaptiste Saleil uint64_t Address, 1841372e23cSBaptiste Saleil const void *Decoder) { 1851372e23cSBaptiste Saleil return decodeRegisterClass(Inst, RegNo, VSRpRegs); 1861372e23cSBaptiste Saleil } 1871372e23cSBaptiste Saleil 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 2075cee3401SVictor Huang static DecodeStatus decodeImmZeroOperand(MCInst &Inst, uint64_t Imm, 2085cee3401SVictor Huang int64_t Address, const void *Decoder) { 2095cee3401SVictor Huang if (Imm != 0) 2105cee3401SVictor Huang return MCDisassembler::Fail; 2115cee3401SVictor Huang Inst.addOperand(MCOperand::createImm(Imm)); 2125cee3401SVictor Huang return MCDisassembler::Success; 2135cee3401SVictor Huang } 2145cee3401SVictor Huang 215*66d2e3f4SAhsan Saghir static DecodeStatus decodeVSRpEvenOperands(MCInst &Inst, uint64_t RegNo, 216*66d2e3f4SAhsan Saghir uint64_t Address, 217*66d2e3f4SAhsan Saghir const void *Decoder) { 218*66d2e3f4SAhsan Saghir if (RegNo & 1) 219*66d2e3f4SAhsan Saghir return MCDisassembler::Fail; 220*66d2e3f4SAhsan Saghir Inst.addOperand(MCOperand::createReg(VSRpRegs[RegNo >> 1])); 221*66d2e3f4SAhsan Saghir return MCDisassembler::Success; 222*66d2e3f4SAhsan Saghir } 223*66d2e3f4SAhsan Saghir 2242345347eSHal Finkel static DecodeStatus decodeMemRIOperands(MCInst &Inst, uint64_t Imm, 2252345347eSHal Finkel int64_t Address, const void *Decoder) { 2262345347eSHal Finkel // Decode the memri field (imm, reg), which has the low 16-bits as the 2272345347eSHal Finkel // displacement and the next 5 bits as the register #. 2282345347eSHal Finkel 2292345347eSHal Finkel uint64_t Base = Imm >> 16; 2302345347eSHal Finkel uint64_t Disp = Imm & 0xFFFF; 2312345347eSHal Finkel 2322345347eSHal Finkel assert(Base < 32 && "Invalid base register"); 2332345347eSHal Finkel 2342345347eSHal Finkel switch (Inst.getOpcode()) { 2352345347eSHal Finkel default: break; 2362345347eSHal Finkel case PPC::LBZU: 2372345347eSHal Finkel case PPC::LHAU: 2382345347eSHal Finkel case PPC::LHZU: 2392345347eSHal Finkel case PPC::LWZU: 2402345347eSHal Finkel case PPC::LFSU: 2412345347eSHal Finkel case PPC::LFDU: 2422345347eSHal Finkel // Add the tied output operand. 2430dad994aSNemanja Ivanovic Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base])); 2442345347eSHal Finkel break; 2452345347eSHal Finkel case PPC::STBU: 2462345347eSHal Finkel case PPC::STHU: 2472345347eSHal Finkel case PPC::STWU: 2482345347eSHal Finkel case PPC::STFSU: 2492345347eSHal Finkel case PPC::STFDU: 2500dad994aSNemanja Ivanovic Inst.insert(Inst.begin(), MCOperand::createReg(RRegsNoR0[Base])); 2512345347eSHal Finkel break; 2522345347eSHal Finkel } 2532345347eSHal Finkel 254e9119e41SJim Grosbach Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp))); 2550dad994aSNemanja Ivanovic Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base])); 2562345347eSHal Finkel return MCDisassembler::Success; 2572345347eSHal Finkel } 2582345347eSHal Finkel 2592345347eSHal Finkel static DecodeStatus decodeMemRIXOperands(MCInst &Inst, uint64_t Imm, 2602345347eSHal Finkel int64_t Address, const void *Decoder) { 2612345347eSHal Finkel // Decode the memrix field (imm, reg), which has the low 14-bits as the 2622345347eSHal Finkel // displacement and the next 5 bits as the register #. 2632345347eSHal Finkel 2642345347eSHal Finkel uint64_t Base = Imm >> 14; 2652345347eSHal Finkel uint64_t Disp = Imm & 0x3FFF; 2662345347eSHal Finkel 2672345347eSHal Finkel assert(Base < 32 && "Invalid base register"); 2682345347eSHal Finkel 2692345347eSHal Finkel if (Inst.getOpcode() == PPC::LDU) 2702345347eSHal Finkel // Add the tied output operand. 2710dad994aSNemanja Ivanovic Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base])); 2722345347eSHal Finkel else if (Inst.getOpcode() == PPC::STDU) 2730dad994aSNemanja Ivanovic Inst.insert(Inst.begin(), MCOperand::createReg(RRegsNoR0[Base])); 2742345347eSHal Finkel 275e9119e41SJim Grosbach Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp << 2))); 2760dad994aSNemanja Ivanovic Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base])); 2772345347eSHal Finkel return MCDisassembler::Success; 2782345347eSHal Finkel } 2792345347eSHal Finkel 280ba532dc8SKit Barton static DecodeStatus decodeMemRIX16Operands(MCInst &Inst, uint64_t Imm, 281ba532dc8SKit Barton int64_t Address, const void *Decoder) { 282ba532dc8SKit Barton // Decode the memrix16 field (imm, reg), which has the low 12-bits as the 283ba532dc8SKit Barton // displacement with 16-byte aligned, and the next 5 bits as the register #. 284ba532dc8SKit Barton 285ba532dc8SKit Barton uint64_t Base = Imm >> 12; 286ba532dc8SKit Barton uint64_t Disp = Imm & 0xFFF; 287ba532dc8SKit Barton 288ba532dc8SKit Barton assert(Base < 32 && "Invalid base register"); 289ba532dc8SKit Barton 290ba532dc8SKit Barton Inst.addOperand(MCOperand::createImm(SignExtend64<16>(Disp << 4))); 2910dad994aSNemanja Ivanovic Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base])); 292ba532dc8SKit Barton return MCDisassembler::Success; 293ba532dc8SKit Barton } 294ba532dc8SKit Barton 2954b414d9aSVictor Huang static DecodeStatus decodeMemRI34PCRelOperands(MCInst &Inst, uint64_t Imm, 2964b414d9aSVictor Huang int64_t Address, 2974b414d9aSVictor Huang const void *Decoder) { 2984b414d9aSVictor Huang // Decode the memri34_pcrel field (imm, reg), which has the low 34-bits as the 2994b414d9aSVictor Huang // displacement, and the next 5 bits as an immediate 0. 3004b414d9aSVictor Huang uint64_t Base = Imm >> 34; 3014b414d9aSVictor Huang uint64_t Disp = Imm & 0x3FFFFFFFFUL; 3024b414d9aSVictor Huang 3034b414d9aSVictor Huang assert(Base < 32 && "Invalid base register"); 3044b414d9aSVictor Huang 3054b414d9aSVictor Huang Inst.addOperand(MCOperand::createImm(SignExtend64<34>(Disp))); 3064b414d9aSVictor Huang return decodeImmZeroOperand(Inst, Base, Address, Decoder); 3074b414d9aSVictor Huang } 3084b414d9aSVictor Huang 3094b414d9aSVictor Huang static DecodeStatus decodeMemRI34Operands(MCInst &Inst, uint64_t Imm, 3104b414d9aSVictor Huang int64_t Address, 3114b414d9aSVictor Huang const void *Decoder) { 3124b414d9aSVictor Huang // Decode the memri34 field (imm, reg), which has the low 34-bits as the 3134b414d9aSVictor Huang // displacement, and the next 5 bits as the register #. 3144b414d9aSVictor Huang uint64_t Base = Imm >> 34; 3154b414d9aSVictor Huang uint64_t Disp = Imm & 0x3FFFFFFFFUL; 3164b414d9aSVictor Huang 3174b414d9aSVictor Huang assert(Base < 32 && "Invalid base register"); 3184b414d9aSVictor Huang 3194b414d9aSVictor Huang Inst.addOperand(MCOperand::createImm(SignExtend64<34>(Disp))); 3204b414d9aSVictor Huang Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base])); 3214b414d9aSVictor Huang return MCDisassembler::Success; 3224b414d9aSVictor Huang } 3234b414d9aSVictor Huang 3244fa4fa6aSJustin Hibbits static DecodeStatus decodeSPE8Operands(MCInst &Inst, uint64_t Imm, 3254fa4fa6aSJustin Hibbits int64_t Address, const void *Decoder) { 3264fa4fa6aSJustin Hibbits // Decode the spe8disp field (imm, reg), which has the low 5-bits as the 3274fa4fa6aSJustin Hibbits // displacement with 8-byte aligned, and the next 5 bits as the register #. 3284fa4fa6aSJustin Hibbits 3294fa4fa6aSJustin Hibbits uint64_t Base = Imm >> 5; 3304fa4fa6aSJustin Hibbits uint64_t Disp = Imm & 0x1F; 3314fa4fa6aSJustin Hibbits 3324fa4fa6aSJustin Hibbits assert(Base < 32 && "Invalid base register"); 3334fa4fa6aSJustin Hibbits 3344fa4fa6aSJustin Hibbits Inst.addOperand(MCOperand::createImm(Disp << 3)); 3350dad994aSNemanja Ivanovic Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base])); 3364fa4fa6aSJustin Hibbits return MCDisassembler::Success; 3374fa4fa6aSJustin Hibbits } 3384fa4fa6aSJustin Hibbits 3394fa4fa6aSJustin Hibbits static DecodeStatus decodeSPE4Operands(MCInst &Inst, uint64_t Imm, 3404fa4fa6aSJustin Hibbits int64_t Address, const void *Decoder) { 3414fa4fa6aSJustin Hibbits // Decode the spe4disp field (imm, reg), which has the low 5-bits as the 3424fa4fa6aSJustin Hibbits // displacement with 4-byte aligned, and the next 5 bits as the register #. 3434fa4fa6aSJustin Hibbits 3444fa4fa6aSJustin Hibbits uint64_t Base = Imm >> 5; 3454fa4fa6aSJustin Hibbits uint64_t Disp = Imm & 0x1F; 3464fa4fa6aSJustin Hibbits 3474fa4fa6aSJustin Hibbits assert(Base < 32 && "Invalid base register"); 3484fa4fa6aSJustin Hibbits 3494fa4fa6aSJustin Hibbits Inst.addOperand(MCOperand::createImm(Disp << 2)); 3500dad994aSNemanja Ivanovic Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base])); 3514fa4fa6aSJustin Hibbits return MCDisassembler::Success; 3524fa4fa6aSJustin Hibbits } 3534fa4fa6aSJustin Hibbits 3544fa4fa6aSJustin Hibbits static DecodeStatus decodeSPE2Operands(MCInst &Inst, uint64_t Imm, 3554fa4fa6aSJustin Hibbits int64_t Address, const void *Decoder) { 3564fa4fa6aSJustin Hibbits // Decode the spe2disp field (imm, reg), which has the low 5-bits as the 3574fa4fa6aSJustin Hibbits // displacement with 2-byte aligned, and the next 5 bits as the register #. 3584fa4fa6aSJustin Hibbits 3594fa4fa6aSJustin Hibbits uint64_t Base = Imm >> 5; 3604fa4fa6aSJustin Hibbits uint64_t Disp = Imm & 0x1F; 3614fa4fa6aSJustin Hibbits 3624fa4fa6aSJustin Hibbits assert(Base < 32 && "Invalid base register"); 3634fa4fa6aSJustin Hibbits 3644fa4fa6aSJustin Hibbits Inst.addOperand(MCOperand::createImm(Disp << 1)); 3650dad994aSNemanja Ivanovic Inst.addOperand(MCOperand::createReg(RRegsNoR0[Base])); 3664fa4fa6aSJustin Hibbits return MCDisassembler::Success; 3674fa4fa6aSJustin Hibbits } 3684fa4fa6aSJustin Hibbits 3692345347eSHal Finkel static DecodeStatus decodeCRBitMOperand(MCInst &Inst, uint64_t Imm, 3702345347eSHal Finkel int64_t Address, const void *Decoder) { 3712345347eSHal Finkel // The cr bit encoding is 0x80 >> cr_reg_num. 3722345347eSHal Finkel 3732345347eSHal Finkel unsigned Zeros = countTrailingZeros(Imm); 3742345347eSHal Finkel assert(Zeros < 8 && "Invalid CR bit value"); 3752345347eSHal Finkel 376e9119e41SJim Grosbach Inst.addOperand(MCOperand::createReg(CRRegs[7 - Zeros])); 3772345347eSHal Finkel return MCDisassembler::Success; 3782345347eSHal Finkel } 3792345347eSHal Finkel 3802345347eSHal Finkel #include "PPCGenDisassemblerTables.inc" 3812345347eSHal Finkel 3822345347eSHal Finkel DecodeStatus PPCDisassembler::getInstruction(MCInst &MI, uint64_t &Size, 3837fc5b874SRafael Espindola ArrayRef<uint8_t> Bytes, 3846fdd6a7bSFangrui Song uint64_t Address, 3854aa6bea7SRafael Espindola raw_ostream &CS) const { 3865cee3401SVictor Huang auto *ReadFunc = IsLittleEndian ? support::endian::read32le 3875cee3401SVictor Huang : support::endian::read32be; 3885cee3401SVictor Huang 3895cee3401SVictor Huang // If this is an 8-byte prefixed instruction, handle it here. 3905cee3401SVictor Huang // Note: prefixed instructions aren't technically 8-byte entities - the prefix 3915cee3401SVictor Huang // appears in memory at an address 4 bytes prior to that of the base 3925cee3401SVictor Huang // instruction regardless of endianness. So we read the two pieces and 3935cee3401SVictor Huang // rebuild the 8-byte instruction. 3945cee3401SVictor Huang // TODO: In this function we call decodeInstruction several times with 3955cee3401SVictor Huang // different decoder tables. It may be possible to only call once by 3965cee3401SVictor Huang // looking at the top 6 bits of the instruction. 3975cee3401SVictor Huang if (STI.getFeatureBits()[PPC::FeaturePrefixInstrs] && Bytes.size() >= 8) { 3985cee3401SVictor Huang uint32_t Prefix = ReadFunc(Bytes.data()); 3995cee3401SVictor Huang uint32_t BaseInst = ReadFunc(Bytes.data() + 4); 4005cee3401SVictor Huang uint64_t Inst = BaseInst | (uint64_t)Prefix << 32; 4015cee3401SVictor Huang DecodeStatus result = decodeInstruction(DecoderTable64, MI, Inst, Address, 4025cee3401SVictor Huang this, STI); 4035cee3401SVictor Huang if (result != MCDisassembler::Fail) { 4045cee3401SVictor Huang Size = 8; 4055cee3401SVictor Huang return result; 4065cee3401SVictor Huang } 4075cee3401SVictor Huang } 4085cee3401SVictor Huang 4092345347eSHal Finkel // Get the four bytes of the instruction. 4102345347eSHal Finkel Size = 4; 4117fc5b874SRafael Espindola if (Bytes.size() < 4) { 4122345347eSHal Finkel Size = 0; 4132345347eSHal Finkel return MCDisassembler::Fail; 4142345347eSHal Finkel } 4152345347eSHal Finkel 416c11fd3e7SBenjamin Kramer // Read the instruction in the proper endianness. 4175cee3401SVictor Huang uint64_t Inst = ReadFunc(Bytes.data()); 4182345347eSHal Finkel 419d28f8672SJinsong Ji if (STI.getFeatureBits()[PPC::FeatureSPE]) { 4204fa4fa6aSJustin Hibbits DecodeStatus result = 4214fa4fa6aSJustin Hibbits decodeInstruction(DecoderTableSPE32, MI, Inst, Address, this, STI); 4224fa4fa6aSJustin Hibbits if (result != MCDisassembler::Fail) 4234fa4fa6aSJustin Hibbits return result; 424c93a9a2cSHal Finkel } 425c93a9a2cSHal Finkel 4262345347eSHal Finkel return decodeInstruction(DecoderTable32, MI, Inst, Address, this, STI); 4272345347eSHal Finkel } 428