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"
11*5d6f03bdSColin 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 
64efa74e02SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
65efa74e02SColin LeMahieu   uint64_t /*Address*/,
66efa74e02SColin LeMahieu   void const *Decoder) {
67efa74e02SColin LeMahieu   if (RegNo > 31)
68efa74e02SColin LeMahieu     return MCDisassembler::Fail;
69efa74e02SColin LeMahieu 
70efa74e02SColin LeMahieu   unsigned Register = IntRegDecoderTable[RegNo];
71efa74e02SColin LeMahieu   Inst.addOperand(MCOperand::CreateReg(Register));
72efa74e02SColin LeMahieu   return MCDisassembler::Success;
73efa74e02SColin LeMahieu }
74efa74e02SColin LeMahieu 
75efa74e02SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
76efa74e02SColin LeMahieu   uint64_t /*Address*/,
77efa74e02SColin LeMahieu   void const *Decoder) {
78efa74e02SColin LeMahieu   if (RegNo > 3)
79efa74e02SColin LeMahieu     return MCDisassembler::Fail;
80efa74e02SColin LeMahieu 
81efa74e02SColin LeMahieu   unsigned Register = PredRegDecoderTable[RegNo];
82efa74e02SColin LeMahieu   Inst.addOperand(MCOperand::CreateReg(Register));
83efa74e02SColin LeMahieu   return MCDisassembler::Success;
84efa74e02SColin LeMahieu }
85efa74e02SColin LeMahieu 
8688ebb9e2SColin LeMahieu #include "HexagonGenDisassemblerTables.inc"
8788ebb9e2SColin LeMahieu 
8888ebb9e2SColin LeMahieu static MCDisassembler *createHexagonDisassembler(Target const &T,
8988ebb9e2SColin LeMahieu                                                  MCSubtargetInfo const &STI,
9088ebb9e2SColin LeMahieu                                                  MCContext &Ctx) {
9188ebb9e2SColin LeMahieu   return new HexagonDisassembler(STI, Ctx);
9288ebb9e2SColin LeMahieu }
9388ebb9e2SColin LeMahieu 
9488ebb9e2SColin LeMahieu extern "C" void LLVMInitializeHexagonDisassembler() {
9588ebb9e2SColin LeMahieu   TargetRegistry::RegisterMCDisassembler(TheHexagonTarget,
9688ebb9e2SColin LeMahieu                                          createHexagonDisassembler);
9788ebb9e2SColin LeMahieu }
9888ebb9e2SColin LeMahieu 
9988ebb9e2SColin LeMahieu DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
1007fc5b874SRafael Espindola                                                  ArrayRef<uint8_t> Bytes,
10188ebb9e2SColin LeMahieu                                                  uint64_t Address,
10288ebb9e2SColin LeMahieu                                                  raw_ostream &os,
10388ebb9e2SColin LeMahieu                                                  raw_ostream &cs) const {
10488ebb9e2SColin LeMahieu   Size = 4;
1057fc5b874SRafael Espindola   if (Bytes.size() < 4)
10688ebb9e2SColin LeMahieu     return MCDisassembler::Fail;
1074aa6bea7SRafael Espindola 
10888ebb9e2SColin LeMahieu   uint32_t insn =
10988ebb9e2SColin LeMahieu       llvm::support::endian::read<uint32_t, llvm::support::little,
11088ebb9e2SColin LeMahieu                                   llvm::support::unaligned>(Bytes.data());
11188ebb9e2SColin LeMahieu 
11288ebb9e2SColin LeMahieu   // Remove parse bits.
11388ebb9e2SColin LeMahieu   insn &= ~static_cast<uint32_t>(HexagonII::InstParseBits::INST_PARSE_MASK);
114*5d6f03bdSColin LeMahieu   DecodeStatus Result = decodeInstruction(DecoderTable32, MI, insn, Address, this, STI);
115*5d6f03bdSColin LeMahieu   HexagonMCInst::AppendImplicitOperands(MI);
116*5d6f03bdSColin LeMahieu   return Result;
11788ebb9e2SColin LeMahieu }
118