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" 115d6f03bdSColin LeMahieu #include "MCTargetDesc/HexagonMCInst.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" 21*d9903888SChandler 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" 25*d9903888SChandler Carruth #include "llvm/Support/raw_ostream.h" 2688ebb9e2SColin LeMahieu #include <array> 27*d9903888SChandler 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); 54f3db884eSColin LeMahieu 55efa74e02SColin LeMahieu static const uint16_t IntRegDecoderTable[] = { 56efa74e02SColin LeMahieu Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4, 57efa74e02SColin LeMahieu Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9, 58efa74e02SColin LeMahieu Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14, 59efa74e02SColin LeMahieu Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19, 60efa74e02SColin LeMahieu Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24, 61efa74e02SColin LeMahieu Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29, 62efa74e02SColin LeMahieu Hexagon::R30, Hexagon::R31 }; 63efa74e02SColin LeMahieu 64efa74e02SColin LeMahieu static const uint16_t PredRegDecoderTable[] = { Hexagon::P0, Hexagon::P1, 65efa74e02SColin LeMahieu Hexagon::P2, Hexagon::P3 }; 66efa74e02SColin LeMahieu 67383c36e3SColin LeMahieu static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo, 68383c36e3SColin LeMahieu const uint16_t Table[], size_t Size) { 69383c36e3SColin LeMahieu if (RegNo < Size) { 70383c36e3SColin LeMahieu Inst.addOperand(MCOperand::CreateReg(Table[RegNo])); 71383c36e3SColin LeMahieu return MCDisassembler::Success; 72383c36e3SColin LeMahieu } 73383c36e3SColin LeMahieu else 74383c36e3SColin LeMahieu return MCDisassembler::Fail; 75383c36e3SColin LeMahieu } 76383c36e3SColin LeMahieu 77efa74e02SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo, 78efa74e02SColin LeMahieu uint64_t /*Address*/, 79efa74e02SColin LeMahieu void const *Decoder) { 80efa74e02SColin LeMahieu if (RegNo > 31) 81efa74e02SColin LeMahieu return MCDisassembler::Fail; 82efa74e02SColin LeMahieu 83efa74e02SColin LeMahieu unsigned Register = IntRegDecoderTable[RegNo]; 84efa74e02SColin LeMahieu Inst.addOperand(MCOperand::CreateReg(Register)); 85efa74e02SColin LeMahieu return MCDisassembler::Success; 86efa74e02SColin LeMahieu } 87efa74e02SColin LeMahieu 88f3db884eSColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo, 89f3db884eSColin LeMahieu uint64_t /*Address*/, const void *Decoder) { 90f3db884eSColin LeMahieu static const uint16_t CtrlRegDecoderTable[] = { 91f3db884eSColin LeMahieu Hexagon::SA0, Hexagon::LC0, Hexagon::SA1, Hexagon::LC1, 92f3db884eSColin LeMahieu Hexagon::P3_0, Hexagon::NoRegister, Hexagon::C6, Hexagon::C7, 93f3db884eSColin LeMahieu Hexagon::USR, Hexagon::PC, Hexagon::UGP, Hexagon::GP, 94f3db884eSColin LeMahieu Hexagon::CS0, Hexagon::CS1, Hexagon::UPCL, Hexagon::UPCH 95f3db884eSColin LeMahieu }; 96f3db884eSColin LeMahieu 97f3db884eSColin LeMahieu if (RegNo >= sizeof(CtrlRegDecoderTable) / sizeof(CtrlRegDecoderTable[0])) 98f3db884eSColin LeMahieu return MCDisassembler::Fail; 99f3db884eSColin LeMahieu 100f3db884eSColin LeMahieu if (CtrlRegDecoderTable[RegNo] == Hexagon::NoRegister) 101f3db884eSColin LeMahieu return MCDisassembler::Fail; 102f3db884eSColin LeMahieu 103f3db884eSColin LeMahieu unsigned Register = CtrlRegDecoderTable[RegNo]; 104f3db884eSColin LeMahieu Inst.addOperand(MCOperand::CreateReg(Register)); 105f3db884eSColin LeMahieu return MCDisassembler::Success; 106f3db884eSColin LeMahieu } 107f3db884eSColin LeMahieu 108ff370ed9SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo, 109ff370ed9SColin LeMahieu uint64_t /*Address*/, const void *Decoder) { 110ff370ed9SColin LeMahieu unsigned Register = 0; 111ff370ed9SColin LeMahieu switch (RegNo) { 112ff370ed9SColin LeMahieu case 0: 113ff370ed9SColin LeMahieu Register = Hexagon::M0; 114ff370ed9SColin LeMahieu break; 115ff370ed9SColin LeMahieu case 1: 116ff370ed9SColin LeMahieu Register = Hexagon::M1; 117ff370ed9SColin LeMahieu break; 118ff370ed9SColin LeMahieu default: 119ff370ed9SColin LeMahieu return MCDisassembler::Fail; 120ff370ed9SColin LeMahieu } 121ff370ed9SColin LeMahieu Inst.addOperand(MCOperand::CreateReg(Register)); 122ff370ed9SColin LeMahieu return MCDisassembler::Success; 123ff370ed9SColin LeMahieu } 124ff370ed9SColin LeMahieu 125383c36e3SColin LeMahieu static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo, 126383c36e3SColin LeMahieu uint64_t /*Address*/, const void *Decoder) { 127383c36e3SColin LeMahieu static const uint16_t DoubleRegDecoderTable[] = { 128383c36e3SColin LeMahieu Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3, 129383c36e3SColin LeMahieu Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7, 130383c36e3SColin LeMahieu Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11, 131383c36e3SColin LeMahieu Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15 132383c36e3SColin LeMahieu }; 133383c36e3SColin LeMahieu 134383c36e3SColin LeMahieu return (DecodeRegisterClass(Inst, RegNo >> 1, 135383c36e3SColin LeMahieu DoubleRegDecoderTable, 136383c36e3SColin LeMahieu sizeof (DoubleRegDecoderTable))); 137383c36e3SColin LeMahieu } 138383c36e3SColin LeMahieu 139efa74e02SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo, 140efa74e02SColin LeMahieu uint64_t /*Address*/, 141efa74e02SColin LeMahieu void const *Decoder) { 142efa74e02SColin LeMahieu if (RegNo > 3) 143efa74e02SColin LeMahieu return MCDisassembler::Fail; 144efa74e02SColin LeMahieu 145efa74e02SColin LeMahieu unsigned Register = PredRegDecoderTable[RegNo]; 146efa74e02SColin LeMahieu Inst.addOperand(MCOperand::CreateReg(Register)); 147efa74e02SColin LeMahieu return MCDisassembler::Success; 148efa74e02SColin LeMahieu } 149efa74e02SColin LeMahieu 15088ebb9e2SColin LeMahieu #include "HexagonGenDisassemblerTables.inc" 15188ebb9e2SColin LeMahieu 15288ebb9e2SColin LeMahieu static MCDisassembler *createHexagonDisassembler(Target const &T, 15388ebb9e2SColin LeMahieu MCSubtargetInfo const &STI, 15488ebb9e2SColin LeMahieu MCContext &Ctx) { 15588ebb9e2SColin LeMahieu return new HexagonDisassembler(STI, Ctx); 15688ebb9e2SColin LeMahieu } 15788ebb9e2SColin LeMahieu 15888ebb9e2SColin LeMahieu extern "C" void LLVMInitializeHexagonDisassembler() { 15988ebb9e2SColin LeMahieu TargetRegistry::RegisterMCDisassembler(TheHexagonTarget, 16088ebb9e2SColin LeMahieu createHexagonDisassembler); 16188ebb9e2SColin LeMahieu } 16288ebb9e2SColin LeMahieu 16388ebb9e2SColin LeMahieu DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size, 1647fc5b874SRafael Espindola ArrayRef<uint8_t> Bytes, 16588ebb9e2SColin LeMahieu uint64_t Address, 16688ebb9e2SColin LeMahieu raw_ostream &os, 16788ebb9e2SColin LeMahieu raw_ostream &cs) const { 16888ebb9e2SColin LeMahieu Size = 4; 1697fc5b874SRafael Espindola if (Bytes.size() < 4) 17088ebb9e2SColin LeMahieu return MCDisassembler::Fail; 1714aa6bea7SRafael Espindola 17288ebb9e2SColin LeMahieu uint32_t insn = 17388ebb9e2SColin LeMahieu llvm::support::endian::read<uint32_t, llvm::support::little, 17488ebb9e2SColin LeMahieu llvm::support::unaligned>(Bytes.data()); 17588ebb9e2SColin LeMahieu 17688ebb9e2SColin LeMahieu // Remove parse bits. 17788ebb9e2SColin LeMahieu insn &= ~static_cast<uint32_t>(HexagonII::InstParseBits::INST_PARSE_MASK); 1785d6f03bdSColin LeMahieu DecodeStatus Result = decodeInstruction(DecoderTable32, MI, insn, Address, this, STI); 1795d6f03bdSColin LeMahieu HexagonMCInst::AppendImplicitOperands(MI); 1805d6f03bdSColin LeMahieu return Result; 18188ebb9e2SColin LeMahieu } 182