188ebb9e2SColin LeMahieu //===-- HexagonDisassembler.cpp - Disassembler for Hexagon ISA ------------===// 288ebb9e2SColin LeMahieu // 388ebb9e2SColin LeMahieu // The LLVM Compiler Infrastructure 488ebb9e2SColin LeMahieu // 588ebb9e2SColin LeMahieu // This file is distributed under the University of Illinois Open Source 688ebb9e2SColin LeMahieu // License. See LICENSE.TXT for details. 788ebb9e2SColin LeMahieu // 888ebb9e2SColin LeMahieu //===----------------------------------------------------------------------===// 988ebb9e2SColin LeMahieu 10*68d967d9SColin LeMahieu #include "Hexagon.h" 1188ebb9e2SColin LeMahieu #include "MCTargetDesc/HexagonBaseInfo.h" 121174fea3SColin LeMahieu #include "MCTargetDesc/HexagonMCInstrInfo.h" 1388ebb9e2SColin LeMahieu #include "MCTargetDesc/HexagonMCTargetDesc.h" 14*68d967d9SColin LeMahieu 1588ebb9e2SColin LeMahieu #include "llvm/MC/MCContext.h" 1688ebb9e2SColin LeMahieu #include "llvm/MC/MCDisassembler.h" 1788ebb9e2SColin LeMahieu #include "llvm/MC/MCExpr.h" 1888ebb9e2SColin LeMahieu #include "llvm/MC/MCFixedLenDisassembler.h" 1988ebb9e2SColin LeMahieu #include "llvm/MC/MCInst.h" 2088ebb9e2SColin LeMahieu #include "llvm/MC/MCInstrDesc.h" 2188ebb9e2SColin LeMahieu #include "llvm/MC/MCSubtargetInfo.h" 2288ebb9e2SColin LeMahieu #include "llvm/Support/Debug.h" 23d9903888SChandler Carruth #include "llvm/Support/Endian.h" 2488ebb9e2SColin LeMahieu #include "llvm/Support/ErrorHandling.h" 2588ebb9e2SColin LeMahieu #include "llvm/Support/LEB128.h" 2688ebb9e2SColin LeMahieu #include "llvm/Support/TargetRegistry.h" 27d9903888SChandler Carruth #include "llvm/Support/raw_ostream.h" 2888ebb9e2SColin LeMahieu #include <array> 29d9903888SChandler Carruth #include <vector> 3088ebb9e2SColin LeMahieu 3188ebb9e2SColin LeMahieu using namespace llvm; 32*68d967d9SColin LeMahieu using namespace Hexagon; 3388ebb9e2SColin LeMahieu 3488ebb9e2SColin LeMahieu #define DEBUG_TYPE "hexagon-disassembler" 3588ebb9e2SColin LeMahieu 36db085665SHans Wennborg // Pull DecodeStatus and its enum values into the global namespace. 37db085665SHans Wennborg typedef llvm::MCDisassembler::DecodeStatus DecodeStatus; 3888ebb9e2SColin LeMahieu 3988ebb9e2SColin LeMahieu namespace { 4088ebb9e2SColin LeMahieu /// \brief Hexagon disassembler for all Hexagon platforms. 4188ebb9e2SColin LeMahieu class HexagonDisassembler : public MCDisassembler { 4288ebb9e2SColin LeMahieu public: 43*68d967d9SColin LeMahieu std::unique_ptr<MCInst *> CurrentBundle; 4488ebb9e2SColin LeMahieu HexagonDisassembler(MCSubtargetInfo const &STI, MCContext &Ctx) 45*68d967d9SColin LeMahieu : MCDisassembler(STI, Ctx), CurrentBundle(new MCInst *) {} 4688ebb9e2SColin LeMahieu 47*68d967d9SColin LeMahieu DecodeStatus getSingleInstruction(MCInst &Instr, MCInst &MCB, 48*68d967d9SColin LeMahieu ArrayRef<uint8_t> Bytes, uint64_t Address, 49*68d967d9SColin LeMahieu raw_ostream &VStream, raw_ostream &CStream, 50*68d967d9SColin LeMahieu bool &Complete) const; 514aa6bea7SRafael Espindola DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, 527fc5b874SRafael Espindola ArrayRef<uint8_t> Bytes, uint64_t Address, 534aa6bea7SRafael Espindola raw_ostream &VStream, 544aa6bea7SRafael Espindola raw_ostream &CStream) const override; 5588ebb9e2SColin LeMahieu }; 5688ebb9e2SColin LeMahieu } 5788ebb9e2SColin LeMahieu 58ff370ed9SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo, 59ff370ed9SColin LeMahieu uint64_t Address, const void *Decoder); 60f3db884eSColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo, 61f3db884eSColin LeMahieu uint64_t Address, const void *Decoder); 62404d5b24SColin LeMahieu static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo, 63404d5b24SColin LeMahieu uint64_t Address, void const *Decoder); 64f3db884eSColin LeMahieu 65efa74e02SColin LeMahieu static const uint16_t IntRegDecoderTable[] = { 66efa74e02SColin LeMahieu Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4, 67efa74e02SColin LeMahieu Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9, 68efa74e02SColin LeMahieu Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14, 69efa74e02SColin LeMahieu Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19, 70efa74e02SColin LeMahieu Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24, 71efa74e02SColin LeMahieu Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29, 72efa74e02SColin LeMahieu Hexagon::R30, Hexagon::R31 }; 73efa74e02SColin LeMahieu 74efa74e02SColin LeMahieu static const uint16_t PredRegDecoderTable[] = { Hexagon::P0, Hexagon::P1, 75efa74e02SColin LeMahieu Hexagon::P2, Hexagon::P3 }; 76efa74e02SColin LeMahieu 77383c36e3SColin LeMahieu static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo, 78383c36e3SColin LeMahieu const uint16_t Table[], size_t Size) { 79383c36e3SColin LeMahieu if (RegNo < Size) { 80e9119e41SJim Grosbach Inst.addOperand(MCOperand::createReg(Table[RegNo])); 81383c36e3SColin LeMahieu return MCDisassembler::Success; 82383c36e3SColin LeMahieu } 83383c36e3SColin LeMahieu else 84383c36e3SColin LeMahieu return MCDisassembler::Fail; 85383c36e3SColin LeMahieu } 86383c36e3SColin LeMahieu 87efa74e02SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo, 88efa74e02SColin LeMahieu uint64_t /*Address*/, 89efa74e02SColin LeMahieu void const *Decoder) { 90efa74e02SColin LeMahieu if (RegNo > 31) 91efa74e02SColin LeMahieu return MCDisassembler::Fail; 92efa74e02SColin LeMahieu 93efa74e02SColin LeMahieu unsigned Register = IntRegDecoderTable[RegNo]; 94e9119e41SJim Grosbach Inst.addOperand(MCOperand::createReg(Register)); 95efa74e02SColin LeMahieu return MCDisassembler::Success; 96efa74e02SColin LeMahieu } 97efa74e02SColin LeMahieu 98f3db884eSColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo, 99f3db884eSColin LeMahieu uint64_t /*Address*/, const void *Decoder) { 100f3db884eSColin LeMahieu static const uint16_t CtrlRegDecoderTable[] = { 101f3db884eSColin LeMahieu Hexagon::SA0, Hexagon::LC0, Hexagon::SA1, Hexagon::LC1, 102f3db884eSColin LeMahieu Hexagon::P3_0, Hexagon::NoRegister, Hexagon::C6, Hexagon::C7, 103f3db884eSColin LeMahieu Hexagon::USR, Hexagon::PC, Hexagon::UGP, Hexagon::GP, 104f3db884eSColin LeMahieu Hexagon::CS0, Hexagon::CS1, Hexagon::UPCL, Hexagon::UPCH 105f3db884eSColin LeMahieu }; 106f3db884eSColin LeMahieu 107f3db884eSColin LeMahieu if (RegNo >= sizeof(CtrlRegDecoderTable) / sizeof(CtrlRegDecoderTable[0])) 108f3db884eSColin LeMahieu return MCDisassembler::Fail; 109f3db884eSColin LeMahieu 110f3db884eSColin LeMahieu if (CtrlRegDecoderTable[RegNo] == Hexagon::NoRegister) 111f3db884eSColin LeMahieu return MCDisassembler::Fail; 112f3db884eSColin LeMahieu 113f3db884eSColin LeMahieu unsigned Register = CtrlRegDecoderTable[RegNo]; 114e9119e41SJim Grosbach Inst.addOperand(MCOperand::createReg(Register)); 115f3db884eSColin LeMahieu return MCDisassembler::Success; 116f3db884eSColin LeMahieu } 117f3db884eSColin LeMahieu 118404d5b24SColin LeMahieu static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo, 119404d5b24SColin LeMahieu uint64_t /*Address*/, void const *Decoder) { 120404d5b24SColin LeMahieu static const uint16_t CtrlReg64DecoderTable[] = { 121404d5b24SColin LeMahieu Hexagon::C1_0, Hexagon::NoRegister, 122404d5b24SColin LeMahieu Hexagon::C3_2, Hexagon::NoRegister, 123404d5b24SColin LeMahieu Hexagon::NoRegister, Hexagon::NoRegister, 124404d5b24SColin LeMahieu Hexagon::C7_6, Hexagon::NoRegister, 125404d5b24SColin LeMahieu Hexagon::C9_8, Hexagon::NoRegister, 126404d5b24SColin LeMahieu Hexagon::C11_10, Hexagon::NoRegister, 127404d5b24SColin LeMahieu Hexagon::CS, Hexagon::NoRegister, 128404d5b24SColin LeMahieu Hexagon::UPC, Hexagon::NoRegister 129404d5b24SColin LeMahieu }; 130404d5b24SColin LeMahieu 131404d5b24SColin LeMahieu if (RegNo >= sizeof(CtrlReg64DecoderTable) / sizeof(CtrlReg64DecoderTable[0])) 132404d5b24SColin LeMahieu return MCDisassembler::Fail; 133404d5b24SColin LeMahieu 134404d5b24SColin LeMahieu if (CtrlReg64DecoderTable[RegNo] == Hexagon::NoRegister) 135404d5b24SColin LeMahieu return MCDisassembler::Fail; 136404d5b24SColin LeMahieu 137404d5b24SColin LeMahieu unsigned Register = CtrlReg64DecoderTable[RegNo]; 138e9119e41SJim Grosbach Inst.addOperand(MCOperand::createReg(Register)); 139404d5b24SColin LeMahieu return MCDisassembler::Success; 140404d5b24SColin LeMahieu } 141404d5b24SColin LeMahieu 142ff370ed9SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo, 143ff370ed9SColin LeMahieu uint64_t /*Address*/, const void *Decoder) { 144ff370ed9SColin LeMahieu unsigned Register = 0; 145ff370ed9SColin LeMahieu switch (RegNo) { 146ff370ed9SColin LeMahieu case 0: 147ff370ed9SColin LeMahieu Register = Hexagon::M0; 148ff370ed9SColin LeMahieu break; 149ff370ed9SColin LeMahieu case 1: 150ff370ed9SColin LeMahieu Register = Hexagon::M1; 151ff370ed9SColin LeMahieu break; 152ff370ed9SColin LeMahieu default: 153ff370ed9SColin LeMahieu return MCDisassembler::Fail; 154ff370ed9SColin LeMahieu } 155e9119e41SJim Grosbach Inst.addOperand(MCOperand::createReg(Register)); 156ff370ed9SColin LeMahieu return MCDisassembler::Success; 157ff370ed9SColin LeMahieu } 158ff370ed9SColin LeMahieu 159383c36e3SColin LeMahieu static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo, 160383c36e3SColin LeMahieu uint64_t /*Address*/, const void *Decoder) { 161383c36e3SColin LeMahieu static const uint16_t DoubleRegDecoderTable[] = { 162383c36e3SColin LeMahieu Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3, 163383c36e3SColin LeMahieu Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7, 164383c36e3SColin LeMahieu Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11, 165383c36e3SColin LeMahieu Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15 166383c36e3SColin LeMahieu }; 167383c36e3SColin LeMahieu 168383c36e3SColin LeMahieu return (DecodeRegisterClass(Inst, RegNo >> 1, 169383c36e3SColin LeMahieu DoubleRegDecoderTable, 170383c36e3SColin LeMahieu sizeof (DoubleRegDecoderTable))); 171383c36e3SColin LeMahieu } 172383c36e3SColin LeMahieu 173efa74e02SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo, 174efa74e02SColin LeMahieu uint64_t /*Address*/, 175efa74e02SColin LeMahieu void const *Decoder) { 176efa74e02SColin LeMahieu if (RegNo > 3) 177efa74e02SColin LeMahieu return MCDisassembler::Fail; 178efa74e02SColin LeMahieu 179efa74e02SColin LeMahieu unsigned Register = PredRegDecoderTable[RegNo]; 180e9119e41SJim Grosbach Inst.addOperand(MCOperand::createReg(Register)); 181efa74e02SColin LeMahieu return MCDisassembler::Success; 182efa74e02SColin LeMahieu } 183efa74e02SColin LeMahieu 18488ebb9e2SColin LeMahieu #include "HexagonGenDisassemblerTables.inc" 18588ebb9e2SColin LeMahieu 18688ebb9e2SColin LeMahieu static MCDisassembler *createHexagonDisassembler(Target const &T, 18788ebb9e2SColin LeMahieu MCSubtargetInfo const &STI, 18888ebb9e2SColin LeMahieu MCContext &Ctx) { 18988ebb9e2SColin LeMahieu return new HexagonDisassembler(STI, Ctx); 19088ebb9e2SColin LeMahieu } 19188ebb9e2SColin LeMahieu 19288ebb9e2SColin LeMahieu extern "C" void LLVMInitializeHexagonDisassembler() { 19388ebb9e2SColin LeMahieu TargetRegistry::RegisterMCDisassembler(TheHexagonTarget, 19488ebb9e2SColin LeMahieu createHexagonDisassembler); 19588ebb9e2SColin LeMahieu } 19688ebb9e2SColin LeMahieu 19788ebb9e2SColin LeMahieu DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size, 1987fc5b874SRafael Espindola ArrayRef<uint8_t> Bytes, 19988ebb9e2SColin LeMahieu uint64_t Address, 20088ebb9e2SColin LeMahieu raw_ostream &os, 20188ebb9e2SColin LeMahieu raw_ostream &cs) const { 202*68d967d9SColin LeMahieu DecodeStatus Result = DecodeStatus::Success; 203*68d967d9SColin LeMahieu bool Complete = false; 204*68d967d9SColin LeMahieu Size = 0; 2054aa6bea7SRafael Espindola 206*68d967d9SColin LeMahieu *CurrentBundle = &MI; 207*68d967d9SColin LeMahieu MI.setOpcode(Hexagon::BUNDLE); 208*68d967d9SColin LeMahieu MI.addOperand(MCOperand::createImm(0)); 209*68d967d9SColin LeMahieu while (Result == Success && Complete == false) 210*68d967d9SColin LeMahieu { 211*68d967d9SColin LeMahieu if (Bytes.size() < HEXAGON_INSTR_SIZE) 212*68d967d9SColin LeMahieu return MCDisassembler::Fail; 213*68d967d9SColin LeMahieu MCInst * Inst = new (getContext()) MCInst; 214*68d967d9SColin LeMahieu Result = getSingleInstruction(*Inst, MI, Bytes, Address, os, cs, Complete); 215*68d967d9SColin LeMahieu MI.addOperand(MCOperand::createInst(Inst)); 216*68d967d9SColin LeMahieu Size += HEXAGON_INSTR_SIZE; 217*68d967d9SColin LeMahieu Bytes = Bytes.slice(HEXAGON_INSTR_SIZE); 218*68d967d9SColin LeMahieu } 219*68d967d9SColin LeMahieu return Result; 220*68d967d9SColin LeMahieu } 221*68d967d9SColin LeMahieu 222*68d967d9SColin LeMahieu DecodeStatus HexagonDisassembler::getSingleInstruction( 223*68d967d9SColin LeMahieu MCInst &MI, MCInst &MCB, ArrayRef<uint8_t> Bytes, uint64_t Address, 224*68d967d9SColin LeMahieu raw_ostream &os, raw_ostream &cs, bool &Complete) const { 225*68d967d9SColin LeMahieu assert(Bytes.size() >= HEXAGON_INSTR_SIZE); 226*68d967d9SColin LeMahieu 227*68d967d9SColin LeMahieu uint32_t Instruction = 22888ebb9e2SColin LeMahieu llvm::support::endian::read<uint32_t, llvm::support::little, 22988ebb9e2SColin LeMahieu llvm::support::unaligned>(Bytes.data()); 23088ebb9e2SColin LeMahieu 231*68d967d9SColin LeMahieu auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB); 232*68d967d9SColin LeMahieu if ((Instruction & HexagonII::INST_PARSE_MASK) == 233*68d967d9SColin LeMahieu HexagonII::INST_PARSE_LOOP_END) { 234*68d967d9SColin LeMahieu if (BundleSize == 0) 235*68d967d9SColin LeMahieu HexagonMCInstrInfo::setInnerLoop(MCB); 236*68d967d9SColin LeMahieu else if (BundleSize == 1) 237*68d967d9SColin LeMahieu HexagonMCInstrInfo::setOuterLoop(MCB); 238*68d967d9SColin LeMahieu else 239*68d967d9SColin LeMahieu return DecodeStatus::Fail; 240*68d967d9SColin LeMahieu } 241*68d967d9SColin LeMahieu 242*68d967d9SColin LeMahieu DecodeStatus Result = DecodeStatus::Success; 243*68d967d9SColin LeMahieu if ((Instruction & HexagonII::INST_PARSE_MASK) == 244*68d967d9SColin LeMahieu HexagonII::INST_PARSE_PACKET_END) 245*68d967d9SColin LeMahieu Complete = true; 246*68d967d9SColin LeMahieu // Calling the auto-generated decoder function. 247*68d967d9SColin LeMahieu Result = 248*68d967d9SColin LeMahieu decodeInstruction(DecoderTable32, MI, Instruction, Address, this, STI); 249*68d967d9SColin LeMahieu 2505d6f03bdSColin LeMahieu return Result; 25188ebb9e2SColin LeMahieu } 252