1 //===-- HexagonDisassembler.cpp - Disassembler for Hexagon ISA ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/HexagonBaseInfo.h"
11 #include "MCTargetDesc/HexagonMCInst.h"
12 #include "MCTargetDesc/HexagonMCTargetDesc.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCDisassembler.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCFixedLenDisassembler.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCInstrDesc.h"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/LEB128.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Support/TargetRegistry.h"
26 #include "llvm/Support/Endian.h"
33 #define DEBUG_TYPE "hexagon-disassembler"
35 // Pull DecodeStatus and its enum values into the global namespace.
36 typedef llvm::MCDisassembler::DecodeStatus DecodeStatus;
39 /// \brief Hexagon disassembler for all Hexagon platforms.
40 class HexagonDisassembler : public MCDisassembler {
42 HexagonDisassembler(MCSubtargetInfo const &STI, MCContext &Ctx)
43 : MCDisassembler(STI, Ctx) {}
45 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
46 ArrayRef<uint8_t> Bytes, uint64_t Address,
48 raw_ostream &CStream) const override;
52 static const uint16_t IntRegDecoderTable[] = {
53 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
54 Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9,
55 Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
56 Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
57 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
58 Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
59 Hexagon::R30, Hexagon::R31 };
61 static const uint16_t PredRegDecoderTable[] = { Hexagon::P0, Hexagon::P1,
62 Hexagon::P2, Hexagon::P3 };
64 static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,
65 const uint16_t Table[], size_t Size) {
67 Inst.addOperand(MCOperand::CreateReg(Table[RegNo]));
68 return MCDisassembler::Success;
71 return MCDisassembler::Fail;
74 static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
76 void const *Decoder) {
78 return MCDisassembler::Fail;
80 unsigned Register = IntRegDecoderTable[RegNo];
81 Inst.addOperand(MCOperand::CreateReg(Register));
82 return MCDisassembler::Success;
85 static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
86 uint64_t /*Address*/, const void *Decoder) {
87 static const uint16_t DoubleRegDecoderTable[] = {
88 Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,
89 Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7,
90 Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11,
91 Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15
94 return (DecodeRegisterClass(Inst, RegNo >> 1,
95 DoubleRegDecoderTable,
96 sizeof (DoubleRegDecoderTable)));
99 static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
100 uint64_t /*Address*/,
101 void const *Decoder) {
103 return MCDisassembler::Fail;
105 unsigned Register = PredRegDecoderTable[RegNo];
106 Inst.addOperand(MCOperand::CreateReg(Register));
107 return MCDisassembler::Success;
110 #include "HexagonGenDisassemblerTables.inc"
112 static MCDisassembler *createHexagonDisassembler(Target const &T,
113 MCSubtargetInfo const &STI,
115 return new HexagonDisassembler(STI, Ctx);
118 extern "C" void LLVMInitializeHexagonDisassembler() {
119 TargetRegistry::RegisterMCDisassembler(TheHexagonTarget,
120 createHexagonDisassembler);
123 DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
124 ArrayRef<uint8_t> Bytes,
127 raw_ostream &cs) const {
129 if (Bytes.size() < 4)
130 return MCDisassembler::Fail;
133 llvm::support::endian::read<uint32_t, llvm::support::little,
134 llvm::support::unaligned>(Bytes.data());
136 // Remove parse bits.
137 insn &= ~static_cast<uint32_t>(HexagonII::InstParseBits::INST_PARSE_MASK);
138 DecodeStatus Result = decodeInstruction(DecoderTable32, MI, insn, Address, this, STI);
139 HexagonMCInst::AppendImplicitOperands(MI);