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"
1188ebb9e2SColin LeMahieu #include "MCTargetDesc/HexagonMCTargetDesc.h"
1288ebb9e2SColin LeMahieu 
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"
2188ebb9e2SColin LeMahieu #include "llvm/Support/ErrorHandling.h"
2288ebb9e2SColin LeMahieu #include "llvm/Support/LEB128.h"
2388ebb9e2SColin LeMahieu #include "llvm/Support/raw_ostream.h"
2488ebb9e2SColin LeMahieu #include "llvm/Support/TargetRegistry.h"
2588ebb9e2SColin LeMahieu #include "llvm/Support/Endian.h"
2688ebb9e2SColin LeMahieu 
2788ebb9e2SColin LeMahieu #include <vector>
2888ebb9e2SColin LeMahieu #include <array>
2988ebb9e2SColin LeMahieu 
3088ebb9e2SColin LeMahieu using namespace llvm;
3188ebb9e2SColin LeMahieu 
3288ebb9e2SColin LeMahieu #define DEBUG_TYPE "hexagon-disassembler"
3388ebb9e2SColin LeMahieu 
34db085665SHans Wennborg // Pull DecodeStatus and its enum values into the global namespace.
35db085665SHans Wennborg typedef llvm::MCDisassembler::DecodeStatus DecodeStatus;
3688ebb9e2SColin LeMahieu 
3788ebb9e2SColin LeMahieu namespace {
3888ebb9e2SColin LeMahieu /// \brief Hexagon disassembler for all Hexagon platforms.
3988ebb9e2SColin LeMahieu class HexagonDisassembler : public MCDisassembler {
4088ebb9e2SColin LeMahieu public:
4188ebb9e2SColin LeMahieu   HexagonDisassembler(MCSubtargetInfo const &STI, MCContext &Ctx)
4288ebb9e2SColin LeMahieu       : MCDisassembler(STI, Ctx) {}
4388ebb9e2SColin LeMahieu 
444aa6bea7SRafael Espindola   DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
457fc5b874SRafael Espindola                               ArrayRef<uint8_t> Bytes, uint64_t Address,
464aa6bea7SRafael Espindola                               raw_ostream &VStream,
474aa6bea7SRafael Espindola                               raw_ostream &CStream) const override;
4888ebb9e2SColin LeMahieu };
4988ebb9e2SColin LeMahieu }
5088ebb9e2SColin LeMahieu 
51*efa74e02SColin LeMahieu static const uint16_t IntRegDecoderTable[] = {
52*efa74e02SColin LeMahieu   Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
53*efa74e02SColin LeMahieu   Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9,
54*efa74e02SColin LeMahieu   Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
55*efa74e02SColin LeMahieu   Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
56*efa74e02SColin LeMahieu   Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
57*efa74e02SColin LeMahieu   Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
58*efa74e02SColin LeMahieu   Hexagon::R30, Hexagon::R31 };
59*efa74e02SColin LeMahieu 
60*efa74e02SColin LeMahieu static const uint16_t PredRegDecoderTable[] = { Hexagon::P0, Hexagon::P1,
61*efa74e02SColin LeMahieu Hexagon::P2, Hexagon::P3 };
62*efa74e02SColin LeMahieu 
63*efa74e02SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
64*efa74e02SColin LeMahieu   uint64_t /*Address*/,
65*efa74e02SColin LeMahieu   void const *Decoder) {
66*efa74e02SColin LeMahieu   if (RegNo > 31)
67*efa74e02SColin LeMahieu     return MCDisassembler::Fail;
68*efa74e02SColin LeMahieu 
69*efa74e02SColin LeMahieu   unsigned Register = IntRegDecoderTable[RegNo];
70*efa74e02SColin LeMahieu   Inst.addOperand(MCOperand::CreateReg(Register));
71*efa74e02SColin LeMahieu   return MCDisassembler::Success;
72*efa74e02SColin LeMahieu }
73*efa74e02SColin LeMahieu 
74*efa74e02SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
75*efa74e02SColin LeMahieu   uint64_t /*Address*/,
76*efa74e02SColin LeMahieu   void const *Decoder) {
77*efa74e02SColin LeMahieu   if (RegNo > 3)
78*efa74e02SColin LeMahieu     return MCDisassembler::Fail;
79*efa74e02SColin LeMahieu 
80*efa74e02SColin LeMahieu   unsigned Register = PredRegDecoderTable[RegNo];
81*efa74e02SColin LeMahieu   Inst.addOperand(MCOperand::CreateReg(Register));
82*efa74e02SColin LeMahieu   return MCDisassembler::Success;
83*efa74e02SColin LeMahieu }
84*efa74e02SColin LeMahieu 
8588ebb9e2SColin LeMahieu #include "HexagonGenDisassemblerTables.inc"
8688ebb9e2SColin LeMahieu 
8788ebb9e2SColin LeMahieu static MCDisassembler *createHexagonDisassembler(Target const &T,
8888ebb9e2SColin LeMahieu                                                  MCSubtargetInfo const &STI,
8988ebb9e2SColin LeMahieu                                                  MCContext &Ctx) {
9088ebb9e2SColin LeMahieu   return new HexagonDisassembler(STI, Ctx);
9188ebb9e2SColin LeMahieu }
9288ebb9e2SColin LeMahieu 
9388ebb9e2SColin LeMahieu extern "C" void LLVMInitializeHexagonDisassembler() {
9488ebb9e2SColin LeMahieu   TargetRegistry::RegisterMCDisassembler(TheHexagonTarget,
9588ebb9e2SColin LeMahieu                                          createHexagonDisassembler);
9688ebb9e2SColin LeMahieu }
9788ebb9e2SColin LeMahieu 
9888ebb9e2SColin LeMahieu DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
997fc5b874SRafael Espindola                                                  ArrayRef<uint8_t> Bytes,
10088ebb9e2SColin LeMahieu                                                  uint64_t Address,
10188ebb9e2SColin LeMahieu                                                  raw_ostream &os,
10288ebb9e2SColin LeMahieu                                                  raw_ostream &cs) const {
10388ebb9e2SColin LeMahieu   Size = 4;
1047fc5b874SRafael Espindola   if (Bytes.size() < 4)
10588ebb9e2SColin LeMahieu     return MCDisassembler::Fail;
1064aa6bea7SRafael Espindola 
10788ebb9e2SColin LeMahieu   uint32_t insn =
10888ebb9e2SColin LeMahieu       llvm::support::endian::read<uint32_t, llvm::support::little,
10988ebb9e2SColin LeMahieu                                   llvm::support::unaligned>(Bytes.data());
11088ebb9e2SColin LeMahieu 
11188ebb9e2SColin LeMahieu   // Remove parse bits.
11288ebb9e2SColin LeMahieu   insn &= ~static_cast<uint32_t>(HexagonII::InstParseBits::INST_PARSE_MASK);
11388ebb9e2SColin LeMahieu   return decodeInstruction(DecoderTable32, MI, insn, Address, this, STI);
11488ebb9e2SColin LeMahieu }
115