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 
52efa74e02SColin LeMahieu static const uint16_t IntRegDecoderTable[] = {
53efa74e02SColin LeMahieu   Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
54efa74e02SColin LeMahieu   Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9,
55efa74e02SColin LeMahieu   Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
56efa74e02SColin LeMahieu   Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
57efa74e02SColin LeMahieu   Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
58efa74e02SColin LeMahieu   Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
59efa74e02SColin LeMahieu   Hexagon::R30, Hexagon::R31 };
60efa74e02SColin LeMahieu 
61efa74e02SColin LeMahieu static const uint16_t PredRegDecoderTable[] = { Hexagon::P0, Hexagon::P1,
62efa74e02SColin LeMahieu Hexagon::P2, Hexagon::P3 };
63efa74e02SColin LeMahieu 
64*383c36e3SColin LeMahieu static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,
65*383c36e3SColin LeMahieu   const uint16_t Table[], size_t Size) {
66*383c36e3SColin LeMahieu   if (RegNo < Size) {
67*383c36e3SColin LeMahieu     Inst.addOperand(MCOperand::CreateReg(Table[RegNo]));
68*383c36e3SColin LeMahieu     return MCDisassembler::Success;
69*383c36e3SColin LeMahieu   }
70*383c36e3SColin LeMahieu   else
71*383c36e3SColin LeMahieu     return MCDisassembler::Fail;
72*383c36e3SColin LeMahieu }
73*383c36e3SColin LeMahieu 
74efa74e02SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
75efa74e02SColin LeMahieu   uint64_t /*Address*/,
76efa74e02SColin LeMahieu   void const *Decoder) {
77efa74e02SColin LeMahieu   if (RegNo > 31)
78efa74e02SColin LeMahieu     return MCDisassembler::Fail;
79efa74e02SColin LeMahieu 
80efa74e02SColin LeMahieu   unsigned Register = IntRegDecoderTable[RegNo];
81efa74e02SColin LeMahieu   Inst.addOperand(MCOperand::CreateReg(Register));
82efa74e02SColin LeMahieu   return MCDisassembler::Success;
83efa74e02SColin LeMahieu }
84efa74e02SColin LeMahieu 
85*383c36e3SColin LeMahieu static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
86*383c36e3SColin LeMahieu   uint64_t /*Address*/, const void *Decoder) {
87*383c36e3SColin LeMahieu   static const uint16_t DoubleRegDecoderTable[] = {
88*383c36e3SColin LeMahieu     Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,
89*383c36e3SColin LeMahieu     Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7,
90*383c36e3SColin LeMahieu     Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11,
91*383c36e3SColin LeMahieu     Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15
92*383c36e3SColin LeMahieu   };
93*383c36e3SColin LeMahieu 
94*383c36e3SColin LeMahieu   return (DecodeRegisterClass(Inst, RegNo >> 1,
95*383c36e3SColin LeMahieu     DoubleRegDecoderTable,
96*383c36e3SColin LeMahieu     sizeof (DoubleRegDecoderTable)));
97*383c36e3SColin LeMahieu }
98*383c36e3SColin LeMahieu 
99efa74e02SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
100efa74e02SColin LeMahieu   uint64_t /*Address*/,
101efa74e02SColin LeMahieu   void const *Decoder) {
102efa74e02SColin LeMahieu   if (RegNo > 3)
103efa74e02SColin LeMahieu     return MCDisassembler::Fail;
104efa74e02SColin LeMahieu 
105efa74e02SColin LeMahieu   unsigned Register = PredRegDecoderTable[RegNo];
106efa74e02SColin LeMahieu   Inst.addOperand(MCOperand::CreateReg(Register));
107efa74e02SColin LeMahieu   return MCDisassembler::Success;
108efa74e02SColin LeMahieu }
109efa74e02SColin LeMahieu 
11088ebb9e2SColin LeMahieu #include "HexagonGenDisassemblerTables.inc"
11188ebb9e2SColin LeMahieu 
11288ebb9e2SColin LeMahieu static MCDisassembler *createHexagonDisassembler(Target const &T,
11388ebb9e2SColin LeMahieu                                                  MCSubtargetInfo const &STI,
11488ebb9e2SColin LeMahieu                                                  MCContext &Ctx) {
11588ebb9e2SColin LeMahieu   return new HexagonDisassembler(STI, Ctx);
11688ebb9e2SColin LeMahieu }
11788ebb9e2SColin LeMahieu 
11888ebb9e2SColin LeMahieu extern "C" void LLVMInitializeHexagonDisassembler() {
11988ebb9e2SColin LeMahieu   TargetRegistry::RegisterMCDisassembler(TheHexagonTarget,
12088ebb9e2SColin LeMahieu                                          createHexagonDisassembler);
12188ebb9e2SColin LeMahieu }
12288ebb9e2SColin LeMahieu 
12388ebb9e2SColin LeMahieu DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
1247fc5b874SRafael Espindola                                                  ArrayRef<uint8_t> Bytes,
12588ebb9e2SColin LeMahieu                                                  uint64_t Address,
12688ebb9e2SColin LeMahieu                                                  raw_ostream &os,
12788ebb9e2SColin LeMahieu                                                  raw_ostream &cs) const {
12888ebb9e2SColin LeMahieu   Size = 4;
1297fc5b874SRafael Espindola   if (Bytes.size() < 4)
13088ebb9e2SColin LeMahieu     return MCDisassembler::Fail;
1314aa6bea7SRafael Espindola 
13288ebb9e2SColin LeMahieu   uint32_t insn =
13388ebb9e2SColin LeMahieu       llvm::support::endian::read<uint32_t, llvm::support::little,
13488ebb9e2SColin LeMahieu                                   llvm::support::unaligned>(Bytes.data());
13588ebb9e2SColin LeMahieu 
13688ebb9e2SColin LeMahieu   // Remove parse bits.
13788ebb9e2SColin LeMahieu   insn &= ~static_cast<uint32_t>(HexagonII::InstParseBits::INST_PARSE_MASK);
1385d6f03bdSColin LeMahieu   DecodeStatus Result = decodeInstruction(DecoderTable32, MI, insn, Address, this, STI);
1395d6f03bdSColin LeMahieu   HexagonMCInst::AppendImplicitOperands(MI);
1405d6f03bdSColin LeMahieu   return Result;
14188ebb9e2SColin LeMahieu }
142