--- /dev/null
+//===-- HexagonDisassembler.cpp - Disassembler for Hexagon ISA ------------===//\r
+//\r
+// The LLVM Compiler Infrastructure\r
+//\r
+// This file is distributed under the University of Illinois Open Source\r
+// License. See LICENSE.TXT for details.\r
+//\r
+//===----------------------------------------------------------------------===//\r
+\r
+#include "MCTargetDesc/HexagonBaseInfo.h"\r
+#include "MCTargetDesc/HexagonMCTargetDesc.h"\r
+\r
+#include "llvm/MC/MCContext.h"\r
+#include "llvm/MC/MCDisassembler.h"\r
+#include "llvm/MC/MCExpr.h"\r
+#include "llvm/MC/MCFixedLenDisassembler.h"\r
+#include "llvm/MC/MCInst.h"\r
+#include "llvm/MC/MCInstrDesc.h"\r
+#include "llvm/MC/MCSubtargetInfo.h"\r
+#include "llvm/Support/Debug.h"\r
+#include "llvm/Support/ErrorHandling.h"\r
+#include "llvm/Support/LEB128.h"\r
+#include "llvm/Support/MemoryObject.h"\r
+#include "llvm/Support/raw_ostream.h"\r
+#include "llvm/Support/TargetRegistry.h"\r
+#include "llvm/Support/Endian.h"\r
+\r
+#include <vector>\r
+#include <array>\r
+\r
+using namespace llvm;\r
+\r
+#define DEBUG_TYPE "hexagon-disassembler"\r
+\r
+using DecodeStatus = MCDisassembler::DecodeStatus;\r
+\r
+namespace {\r
+/// \brief Hexagon disassembler for all Hexagon platforms.\r
+class HexagonDisassembler : public MCDisassembler {\r
+public:\r
+ HexagonDisassembler(MCSubtargetInfo const &STI, MCContext &Ctx)\r
+ : MCDisassembler(STI, Ctx) {}\r
+\r
+ DecodeStatus getInstruction(MCInst &instr, uint64_t &size,\r
+ MemoryObject const ®ion, uint64_t address,\r
+ raw_ostream &vStream, raw_ostream &cStream) const override;\r
+};\r
+}\r
+\r
+static const uint16_t IntRegDecoderTable[] = {\r
+ Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,\r
+ Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9,\r
+ Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,\r
+ Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,\r
+ Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,\r
+ Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,\r
+ Hexagon::R30, Hexagon::R31};\r
+\r
+static const uint16_t DoubleRegDecoderTable[] = {\r
+ Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,\r
+ Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7,\r
+ Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11,\r
+ Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15};\r
+\r
+static const uint16_t PredRegDecoderTable[] = {Hexagon::P0, Hexagon::P1,\r
+ Hexagon::P2, Hexagon::P3};\r
+\r
+static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,\r
+ uint64_t /*Address*/,\r
+ void const *Decoder) {\r
+ if (RegNo > 31)\r
+ return MCDisassembler::Fail;\r
+\r
+ unsigned Register = IntRegDecoderTable[RegNo];\r
+ Inst.addOperand(MCOperand::CreateReg(Register));\r
+ return MCDisassembler::Success;\r
+}\r
+\r
+static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,\r
+ uint64_t /*Address*/,\r
+ void const *Decoder) {\r
+ if (RegNo > 15)\r
+ return MCDisassembler::Fail;\r
+\r
+ unsigned Register = DoubleRegDecoderTable[RegNo];\r
+ Inst.addOperand(MCOperand::CreateReg(Register));\r
+ return MCDisassembler::Success;\r
+}\r
+\r
+static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,\r
+ uint64_t /*Address*/,\r
+ void const *Decoder) {\r
+ if (RegNo > 3)\r
+ return MCDisassembler::Fail;\r
+\r
+ unsigned Register = PredRegDecoderTable[RegNo];\r
+ Inst.addOperand(MCOperand::CreateReg(Register));\r
+ return MCDisassembler::Success;\r
+}\r
+\r
+#include "HexagonGenDisassemblerTables.inc"\r
+\r
+static MCDisassembler *createHexagonDisassembler(Target const &T,\r
+ MCSubtargetInfo const &STI,\r
+ MCContext &Ctx) {\r
+ return new HexagonDisassembler(STI, Ctx);\r
+}\r
+\r
+extern "C" void LLVMInitializeHexagonDisassembler() {\r
+ TargetRegistry::RegisterMCDisassembler(TheHexagonTarget,\r
+ createHexagonDisassembler);\r
+}\r
+\r
+DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,\r
+ MemoryObject const &Region,\r
+ uint64_t Address,\r
+ raw_ostream &os,\r
+ raw_ostream &cs) const {\r
+ std::array<uint8_t, 4> Bytes;\r
+ Size = 4;\r
+ if (Region.readBytes(Address, Bytes.size(), Bytes.data()) == -1) {\r
+ return MCDisassembler::Fail;\r
+ }\r
+ uint32_t insn =\r
+ llvm::support::endian::read<uint32_t, llvm::support::little,\r
+ llvm::support::unaligned>(Bytes.data());\r
+\r
+ // Remove parse bits.\r
+ insn &= ~static_cast<uint32_t>(HexagonII::InstParseBits::INST_PARSE_MASK);\r
+ return decodeInstruction(DecoderTable32, MI, insn, Address, this, STI);\r
+}\r