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 1488ebb9e2SColin LeMahieu #include "llvm/MC/MCContext.h" 1588ebb9e2SColin LeMahieu #include "llvm/MC/MCDisassembler.h" 1688ebb9e2SColin LeMahieu #include "llvm/MC/MCExpr.h" 1788ebb9e2SColin LeMahieu #include "llvm/MC/MCFixedLenDisassembler.h" 1888ebb9e2SColin LeMahieu #include "llvm/MC/MCInst.h" 1988ebb9e2SColin LeMahieu #include "llvm/MC/MCInstrDesc.h" 2088ebb9e2SColin LeMahieu #include "llvm/MC/MCSubtargetInfo.h" 2188ebb9e2SColin LeMahieu #include "llvm/Support/Debug.h" 2288ebb9e2SColin LeMahieu #include "llvm/Support/ErrorHandling.h" 2388ebb9e2SColin LeMahieu #include "llvm/Support/LEB128.h" 2488ebb9e2SColin LeMahieu #include "llvm/Support/raw_ostream.h" 2588ebb9e2SColin LeMahieu #include "llvm/Support/TargetRegistry.h" 2688ebb9e2SColin LeMahieu #include "llvm/Support/Endian.h" 2788ebb9e2SColin LeMahieu 2888ebb9e2SColin LeMahieu #include <vector> 2988ebb9e2SColin LeMahieu #include <array> 3088ebb9e2SColin LeMahieu 3188ebb9e2SColin LeMahieu using namespace llvm; 3288ebb9e2SColin LeMahieu 3388ebb9e2SColin LeMahieu #define DEBUG_TYPE "hexagon-disassembler" 3488ebb9e2SColin LeMahieu 35db085665SHans Wennborg // Pull DecodeStatus and its enum values into the global namespace. 36db085665SHans Wennborg typedef llvm::MCDisassembler::DecodeStatus DecodeStatus; 3788ebb9e2SColin LeMahieu 3888ebb9e2SColin LeMahieu namespace { 3988ebb9e2SColin LeMahieu /// \brief Hexagon disassembler for all Hexagon platforms. 4088ebb9e2SColin LeMahieu class HexagonDisassembler : public MCDisassembler { 4188ebb9e2SColin LeMahieu public: 4288ebb9e2SColin LeMahieu HexagonDisassembler(MCSubtargetInfo const &STI, MCContext &Ctx) 4388ebb9e2SColin LeMahieu : MCDisassembler(STI, Ctx) {} 4488ebb9e2SColin LeMahieu 454aa6bea7SRafael Espindola DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, 467fc5b874SRafael Espindola ArrayRef<uint8_t> Bytes, uint64_t Address, 474aa6bea7SRafael Espindola raw_ostream &VStream, 484aa6bea7SRafael Espindola raw_ostream &CStream) const override; 4988ebb9e2SColin LeMahieu }; 5088ebb9e2SColin LeMahieu } 5188ebb9e2SColin LeMahieu 52*ff370ed9SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo, 53*ff370ed9SColin LeMahieu uint64_t Address, const void *Decoder); 54f3db884eSColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo, 55f3db884eSColin LeMahieu uint64_t Address, const void *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) { 72383c36e3SColin LeMahieu 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]; 86efa74e02SColin LeMahieu 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]; 106f3db884eSColin LeMahieu Inst.addOperand(MCOperand::CreateReg(Register)); 107f3db884eSColin LeMahieu return MCDisassembler::Success; 108f3db884eSColin LeMahieu } 109f3db884eSColin LeMahieu 110*ff370ed9SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo, 111*ff370ed9SColin LeMahieu uint64_t /*Address*/, const void *Decoder) { 112*ff370ed9SColin LeMahieu unsigned Register = 0; 113*ff370ed9SColin LeMahieu switch (RegNo) { 114*ff370ed9SColin LeMahieu case 0: 115*ff370ed9SColin LeMahieu Register = Hexagon::M0; 116*ff370ed9SColin LeMahieu break; 117*ff370ed9SColin LeMahieu case 1: 118*ff370ed9SColin LeMahieu Register = Hexagon::M1; 119*ff370ed9SColin LeMahieu break; 120*ff370ed9SColin LeMahieu default: 121*ff370ed9SColin LeMahieu return MCDisassembler::Fail; 122*ff370ed9SColin LeMahieu } 123*ff370ed9SColin LeMahieu Inst.addOperand(MCOperand::CreateReg(Register)); 124*ff370ed9SColin LeMahieu return MCDisassembler::Success; 125*ff370ed9SColin LeMahieu } 126*ff370ed9SColin LeMahieu 127383c36e3SColin LeMahieu static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo, 128383c36e3SColin LeMahieu uint64_t /*Address*/, const void *Decoder) { 129383c36e3SColin LeMahieu static const uint16_t DoubleRegDecoderTable[] = { 130383c36e3SColin LeMahieu Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3, 131383c36e3SColin LeMahieu Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7, 132383c36e3SColin LeMahieu Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11, 133383c36e3SColin LeMahieu Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15 134383c36e3SColin LeMahieu }; 135383c36e3SColin LeMahieu 136383c36e3SColin LeMahieu return (DecodeRegisterClass(Inst, RegNo >> 1, 137383c36e3SColin LeMahieu DoubleRegDecoderTable, 138383c36e3SColin LeMahieu sizeof (DoubleRegDecoderTable))); 139383c36e3SColin LeMahieu } 140383c36e3SColin LeMahieu 141efa74e02SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo, 142efa74e02SColin LeMahieu uint64_t /*Address*/, 143efa74e02SColin LeMahieu void const *Decoder) { 144efa74e02SColin LeMahieu if (RegNo > 3) 145efa74e02SColin LeMahieu return MCDisassembler::Fail; 146efa74e02SColin LeMahieu 147efa74e02SColin LeMahieu unsigned Register = PredRegDecoderTable[RegNo]; 148efa74e02SColin LeMahieu Inst.addOperand(MCOperand::CreateReg(Register)); 149efa74e02SColin LeMahieu return MCDisassembler::Success; 150efa74e02SColin LeMahieu } 151efa74e02SColin LeMahieu 15288ebb9e2SColin LeMahieu #include "HexagonGenDisassemblerTables.inc" 15388ebb9e2SColin LeMahieu 15488ebb9e2SColin LeMahieu static MCDisassembler *createHexagonDisassembler(Target const &T, 15588ebb9e2SColin LeMahieu MCSubtargetInfo const &STI, 15688ebb9e2SColin LeMahieu MCContext &Ctx) { 15788ebb9e2SColin LeMahieu return new HexagonDisassembler(STI, Ctx); 15888ebb9e2SColin LeMahieu } 15988ebb9e2SColin LeMahieu 16088ebb9e2SColin LeMahieu extern "C" void LLVMInitializeHexagonDisassembler() { 16188ebb9e2SColin LeMahieu TargetRegistry::RegisterMCDisassembler(TheHexagonTarget, 16288ebb9e2SColin LeMahieu createHexagonDisassembler); 16388ebb9e2SColin LeMahieu } 16488ebb9e2SColin LeMahieu 16588ebb9e2SColin LeMahieu DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size, 1667fc5b874SRafael Espindola ArrayRef<uint8_t> Bytes, 16788ebb9e2SColin LeMahieu uint64_t Address, 16888ebb9e2SColin LeMahieu raw_ostream &os, 16988ebb9e2SColin LeMahieu raw_ostream &cs) const { 17088ebb9e2SColin LeMahieu Size = 4; 1717fc5b874SRafael Espindola if (Bytes.size() < 4) 17288ebb9e2SColin LeMahieu return MCDisassembler::Fail; 1734aa6bea7SRafael Espindola 17488ebb9e2SColin LeMahieu uint32_t insn = 17588ebb9e2SColin LeMahieu llvm::support::endian::read<uint32_t, llvm::support::little, 17688ebb9e2SColin LeMahieu llvm::support::unaligned>(Bytes.data()); 17788ebb9e2SColin LeMahieu 17888ebb9e2SColin LeMahieu // Remove parse bits. 17988ebb9e2SColin LeMahieu insn &= ~static_cast<uint32_t>(HexagonII::InstParseBits::INST_PARSE_MASK); 1805d6f03bdSColin LeMahieu DecodeStatus Result = decodeInstruction(DecoderTable32, MI, insn, Address, this, STI); 1815d6f03bdSColin LeMahieu HexagonMCInst::AppendImplicitOperands(MI); 1825d6f03bdSColin LeMahieu return Result; 18388ebb9e2SColin LeMahieu } 184