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 1088ebb9e2SColin LeMahieu #include "MCTargetDesc/HexagonBaseInfo.h" 111174fea3SColin LeMahieu #include "MCTargetDesc/HexagonMCInstrInfo.h" 1288ebb9e2SColin LeMahieu #include "MCTargetDesc/HexagonMCTargetDesc.h" 1388ebb9e2SColin LeMahieu #include "llvm/MC/MCContext.h" 1488ebb9e2SColin LeMahieu #include "llvm/MC/MCDisassembler.h" 1588ebb9e2SColin LeMahieu #include "llvm/MC/MCExpr.h" 1688ebb9e2SColin LeMahieu #include "llvm/MC/MCFixedLenDisassembler.h" 1788ebb9e2SColin LeMahieu #include "llvm/MC/MCInst.h" 1888ebb9e2SColin LeMahieu #include "llvm/MC/MCInstrDesc.h" 1988ebb9e2SColin LeMahieu #include "llvm/MC/MCSubtargetInfo.h" 2088ebb9e2SColin LeMahieu #include "llvm/Support/Debug.h" 21d9903888SChandler Carruth #include "llvm/Support/Endian.h" 2288ebb9e2SColin LeMahieu #include "llvm/Support/ErrorHandling.h" 2388ebb9e2SColin LeMahieu #include "llvm/Support/LEB128.h" 2488ebb9e2SColin LeMahieu #include "llvm/Support/TargetRegistry.h" 25d9903888SChandler Carruth #include "llvm/Support/raw_ostream.h" 2688ebb9e2SColin LeMahieu #include <array> 27d9903888SChandler Carruth #include <vector> 2888ebb9e2SColin LeMahieu 2988ebb9e2SColin LeMahieu using namespace llvm; 3088ebb9e2SColin LeMahieu 3188ebb9e2SColin LeMahieu #define DEBUG_TYPE "hexagon-disassembler" 3288ebb9e2SColin LeMahieu 33db085665SHans Wennborg // Pull DecodeStatus and its enum values into the global namespace. 34db085665SHans Wennborg typedef llvm::MCDisassembler::DecodeStatus DecodeStatus; 3588ebb9e2SColin LeMahieu 3688ebb9e2SColin LeMahieu namespace { 3788ebb9e2SColin LeMahieu /// \brief Hexagon disassembler for all Hexagon platforms. 3888ebb9e2SColin LeMahieu class HexagonDisassembler : public MCDisassembler { 3988ebb9e2SColin LeMahieu public: 4088ebb9e2SColin LeMahieu HexagonDisassembler(MCSubtargetInfo const &STI, MCContext &Ctx) 4188ebb9e2SColin LeMahieu : MCDisassembler(STI, Ctx) {} 4288ebb9e2SColin LeMahieu 434aa6bea7SRafael Espindola DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, 447fc5b874SRafael Espindola ArrayRef<uint8_t> Bytes, uint64_t Address, 454aa6bea7SRafael Espindola raw_ostream &VStream, 464aa6bea7SRafael Espindola raw_ostream &CStream) const override; 4788ebb9e2SColin LeMahieu }; 4888ebb9e2SColin LeMahieu } 4988ebb9e2SColin LeMahieu 50ff370ed9SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo, 51ff370ed9SColin LeMahieu uint64_t Address, const void *Decoder); 52f3db884eSColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo, 53f3db884eSColin LeMahieu uint64_t Address, const void *Decoder); 54404d5b24SColin LeMahieu static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo, 55404d5b24SColin LeMahieu uint64_t Address, void const *Decoder); 56f3db884eSColin LeMahieu 57efa74e02SColin LeMahieu static const uint16_t IntRegDecoderTable[] = { 58efa74e02SColin LeMahieu Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4, 59efa74e02SColin LeMahieu Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9, 60efa74e02SColin LeMahieu Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14, 61efa74e02SColin LeMahieu Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19, 62efa74e02SColin LeMahieu Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24, 63efa74e02SColin LeMahieu Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29, 64efa74e02SColin LeMahieu Hexagon::R30, Hexagon::R31 }; 65efa74e02SColin LeMahieu 66efa74e02SColin LeMahieu static const uint16_t PredRegDecoderTable[] = { Hexagon::P0, Hexagon::P1, 67efa74e02SColin LeMahieu Hexagon::P2, Hexagon::P3 }; 68efa74e02SColin LeMahieu 69383c36e3SColin LeMahieu static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo, 70383c36e3SColin LeMahieu const uint16_t Table[], size_t Size) { 71383c36e3SColin LeMahieu if (RegNo < Size) { 72*e9119e41SJim Grosbach Inst.addOperand(MCOperand::createReg(Table[RegNo])); 73383c36e3SColin LeMahieu return MCDisassembler::Success; 74383c36e3SColin LeMahieu } 75383c36e3SColin LeMahieu else 76383c36e3SColin LeMahieu return MCDisassembler::Fail; 77383c36e3SColin LeMahieu } 78383c36e3SColin LeMahieu 79efa74e02SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo, 80efa74e02SColin LeMahieu uint64_t /*Address*/, 81efa74e02SColin LeMahieu void const *Decoder) { 82efa74e02SColin LeMahieu if (RegNo > 31) 83efa74e02SColin LeMahieu return MCDisassembler::Fail; 84efa74e02SColin LeMahieu 85efa74e02SColin LeMahieu unsigned Register = IntRegDecoderTable[RegNo]; 86*e9119e41SJim Grosbach Inst.addOperand(MCOperand::createReg(Register)); 87efa74e02SColin LeMahieu return MCDisassembler::Success; 88efa74e02SColin LeMahieu } 89efa74e02SColin LeMahieu 90f3db884eSColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo, 91f3db884eSColin LeMahieu uint64_t /*Address*/, const void *Decoder) { 92f3db884eSColin LeMahieu static const uint16_t CtrlRegDecoderTable[] = { 93f3db884eSColin LeMahieu Hexagon::SA0, Hexagon::LC0, Hexagon::SA1, Hexagon::LC1, 94f3db884eSColin LeMahieu Hexagon::P3_0, Hexagon::NoRegister, Hexagon::C6, Hexagon::C7, 95f3db884eSColin LeMahieu Hexagon::USR, Hexagon::PC, Hexagon::UGP, Hexagon::GP, 96f3db884eSColin LeMahieu Hexagon::CS0, Hexagon::CS1, Hexagon::UPCL, Hexagon::UPCH 97f3db884eSColin LeMahieu }; 98f3db884eSColin LeMahieu 99f3db884eSColin LeMahieu if (RegNo >= sizeof(CtrlRegDecoderTable) / sizeof(CtrlRegDecoderTable[0])) 100f3db884eSColin LeMahieu return MCDisassembler::Fail; 101f3db884eSColin LeMahieu 102f3db884eSColin LeMahieu if (CtrlRegDecoderTable[RegNo] == Hexagon::NoRegister) 103f3db884eSColin LeMahieu return MCDisassembler::Fail; 104f3db884eSColin LeMahieu 105f3db884eSColin LeMahieu unsigned Register = CtrlRegDecoderTable[RegNo]; 106*e9119e41SJim Grosbach Inst.addOperand(MCOperand::createReg(Register)); 107f3db884eSColin LeMahieu return MCDisassembler::Success; 108f3db884eSColin LeMahieu } 109f3db884eSColin LeMahieu 110404d5b24SColin LeMahieu static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo, 111404d5b24SColin LeMahieu uint64_t /*Address*/, void const *Decoder) { 112404d5b24SColin LeMahieu static const uint16_t CtrlReg64DecoderTable[] = { 113404d5b24SColin LeMahieu Hexagon::C1_0, Hexagon::NoRegister, 114404d5b24SColin LeMahieu Hexagon::C3_2, Hexagon::NoRegister, 115404d5b24SColin LeMahieu Hexagon::NoRegister, Hexagon::NoRegister, 116404d5b24SColin LeMahieu Hexagon::C7_6, Hexagon::NoRegister, 117404d5b24SColin LeMahieu Hexagon::C9_8, Hexagon::NoRegister, 118404d5b24SColin LeMahieu Hexagon::C11_10, Hexagon::NoRegister, 119404d5b24SColin LeMahieu Hexagon::CS, Hexagon::NoRegister, 120404d5b24SColin LeMahieu Hexagon::UPC, Hexagon::NoRegister 121404d5b24SColin LeMahieu }; 122404d5b24SColin LeMahieu 123404d5b24SColin LeMahieu if (RegNo >= sizeof(CtrlReg64DecoderTable) / sizeof(CtrlReg64DecoderTable[0])) 124404d5b24SColin LeMahieu return MCDisassembler::Fail; 125404d5b24SColin LeMahieu 126404d5b24SColin LeMahieu if (CtrlReg64DecoderTable[RegNo] == Hexagon::NoRegister) 127404d5b24SColin LeMahieu return MCDisassembler::Fail; 128404d5b24SColin LeMahieu 129404d5b24SColin LeMahieu unsigned Register = CtrlReg64DecoderTable[RegNo]; 130*e9119e41SJim Grosbach Inst.addOperand(MCOperand::createReg(Register)); 131404d5b24SColin LeMahieu return MCDisassembler::Success; 132404d5b24SColin LeMahieu } 133404d5b24SColin LeMahieu 134ff370ed9SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo, 135ff370ed9SColin LeMahieu uint64_t /*Address*/, const void *Decoder) { 136ff370ed9SColin LeMahieu unsigned Register = 0; 137ff370ed9SColin LeMahieu switch (RegNo) { 138ff370ed9SColin LeMahieu case 0: 139ff370ed9SColin LeMahieu Register = Hexagon::M0; 140ff370ed9SColin LeMahieu break; 141ff370ed9SColin LeMahieu case 1: 142ff370ed9SColin LeMahieu Register = Hexagon::M1; 143ff370ed9SColin LeMahieu break; 144ff370ed9SColin LeMahieu default: 145ff370ed9SColin LeMahieu return MCDisassembler::Fail; 146ff370ed9SColin LeMahieu } 147*e9119e41SJim Grosbach Inst.addOperand(MCOperand::createReg(Register)); 148ff370ed9SColin LeMahieu return MCDisassembler::Success; 149ff370ed9SColin LeMahieu } 150ff370ed9SColin LeMahieu 151383c36e3SColin LeMahieu static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo, 152383c36e3SColin LeMahieu uint64_t /*Address*/, const void *Decoder) { 153383c36e3SColin LeMahieu static const uint16_t DoubleRegDecoderTable[] = { 154383c36e3SColin LeMahieu Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3, 155383c36e3SColin LeMahieu Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7, 156383c36e3SColin LeMahieu Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11, 157383c36e3SColin LeMahieu Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15 158383c36e3SColin LeMahieu }; 159383c36e3SColin LeMahieu 160383c36e3SColin LeMahieu return (DecodeRegisterClass(Inst, RegNo >> 1, 161383c36e3SColin LeMahieu DoubleRegDecoderTable, 162383c36e3SColin LeMahieu sizeof (DoubleRegDecoderTable))); 163383c36e3SColin LeMahieu } 164383c36e3SColin LeMahieu 165efa74e02SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo, 166efa74e02SColin LeMahieu uint64_t /*Address*/, 167efa74e02SColin LeMahieu void const *Decoder) { 168efa74e02SColin LeMahieu if (RegNo > 3) 169efa74e02SColin LeMahieu return MCDisassembler::Fail; 170efa74e02SColin LeMahieu 171efa74e02SColin LeMahieu unsigned Register = PredRegDecoderTable[RegNo]; 172*e9119e41SJim Grosbach Inst.addOperand(MCOperand::createReg(Register)); 173efa74e02SColin LeMahieu return MCDisassembler::Success; 174efa74e02SColin LeMahieu } 175efa74e02SColin LeMahieu 17688ebb9e2SColin LeMahieu #include "HexagonGenDisassemblerTables.inc" 17788ebb9e2SColin LeMahieu 17888ebb9e2SColin LeMahieu static MCDisassembler *createHexagonDisassembler(Target const &T, 17988ebb9e2SColin LeMahieu MCSubtargetInfo const &STI, 18088ebb9e2SColin LeMahieu MCContext &Ctx) { 18188ebb9e2SColin LeMahieu return new HexagonDisassembler(STI, Ctx); 18288ebb9e2SColin LeMahieu } 18388ebb9e2SColin LeMahieu 18488ebb9e2SColin LeMahieu extern "C" void LLVMInitializeHexagonDisassembler() { 18588ebb9e2SColin LeMahieu TargetRegistry::RegisterMCDisassembler(TheHexagonTarget, 18688ebb9e2SColin LeMahieu createHexagonDisassembler); 18788ebb9e2SColin LeMahieu } 18888ebb9e2SColin LeMahieu 18988ebb9e2SColin LeMahieu DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size, 1907fc5b874SRafael Espindola ArrayRef<uint8_t> Bytes, 19188ebb9e2SColin LeMahieu uint64_t Address, 19288ebb9e2SColin LeMahieu raw_ostream &os, 19388ebb9e2SColin LeMahieu raw_ostream &cs) const { 19488ebb9e2SColin LeMahieu Size = 4; 1957fc5b874SRafael Espindola if (Bytes.size() < 4) 19688ebb9e2SColin LeMahieu return MCDisassembler::Fail; 1974aa6bea7SRafael Espindola 19888ebb9e2SColin LeMahieu uint32_t insn = 19988ebb9e2SColin LeMahieu llvm::support::endian::read<uint32_t, llvm::support::little, 20088ebb9e2SColin LeMahieu llvm::support::unaligned>(Bytes.data()); 20188ebb9e2SColin LeMahieu 20288ebb9e2SColin LeMahieu // Remove parse bits. 20388ebb9e2SColin LeMahieu insn &= ~static_cast<uint32_t>(HexagonII::InstParseBits::INST_PARSE_MASK); 2045d6f03bdSColin LeMahieu DecodeStatus Result = decodeInstruction(DecoderTable32, MI, insn, Address, this, STI); 2051174fea3SColin LeMahieu HexagonMCInstrInfo::AppendImplicitOperands(MI); 2065d6f03bdSColin LeMahieu return Result; 20788ebb9e2SColin LeMahieu } 208