NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 1 | //===-- HexagonDisassembler.cpp - Disassembler for Hexagon ISA ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "MCTargetDesc/HexagonBaseInfo.h" |
Colin LeMahieu | 5d6f03b | 2014-12-04 03:41:21 +0000 | [diff] [blame] | 11 | #include "MCTargetDesc/HexagonMCInst.h" |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 12 | #include "MCTargetDesc/HexagonMCTargetDesc.h" |
| 13 | |
| 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" |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include "llvm/Support/TargetRegistry.h" |
| 26 | #include "llvm/Support/Endian.h" |
| 27 | |
| 28 | #include <vector> |
| 29 | #include <array> |
| 30 | |
| 31 | using namespace llvm; |
| 32 | |
| 33 | #define DEBUG_TYPE "hexagon-disassembler" |
| 34 | |
| 35 | // Pull DecodeStatus and its enum values into the global namespace. |
| 36 | typedef llvm::MCDisassembler::DecodeStatus DecodeStatus; |
| 37 | |
| 38 | namespace { |
| 39 | /// \brief Hexagon disassembler for all Hexagon platforms. |
| 40 | class HexagonDisassembler : public MCDisassembler { |
| 41 | public: |
| 42 | HexagonDisassembler(MCSubtargetInfo const &STI, MCContext &Ctx) |
| 43 | : MCDisassembler(STI, Ctx) {} |
| 44 | |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 45 | DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 46 | ArrayRef<uint8_t> Bytes, uint64_t Address, |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 47 | raw_ostream &VStream, |
| 48 | raw_ostream &CStream) const override; |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 49 | }; |
| 50 | } |
| 51 | |
Colin LeMahieu | efa74e0 | 2014-11-18 20:28:11 +0000 | [diff] [blame] | 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 }; |
| 60 | |
| 61 | static const uint16_t PredRegDecoderTable[] = { Hexagon::P0, Hexagon::P1, |
| 62 | Hexagon::P2, Hexagon::P3 }; |
| 63 | |
Colin LeMahieu | 383c36e | 2014-12-05 18:24:06 +0000 | [diff] [blame] | 64 | static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo, |
| 65 | const uint16_t Table[], size_t Size) { |
| 66 | if (RegNo < Size) { |
| 67 | Inst.addOperand(MCOperand::CreateReg(Table[RegNo])); |
| 68 | return MCDisassembler::Success; |
| 69 | } |
| 70 | else |
| 71 | return MCDisassembler::Fail; |
| 72 | } |
| 73 | |
Colin LeMahieu | efa74e0 | 2014-11-18 20:28:11 +0000 | [diff] [blame] | 74 | static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
| 75 | uint64_t /*Address*/, |
| 76 | void const *Decoder) { |
| 77 | if (RegNo > 31) |
| 78 | return MCDisassembler::Fail; |
| 79 | |
| 80 | unsigned Register = IntRegDecoderTable[RegNo]; |
| 81 | Inst.addOperand(MCOperand::CreateReg(Register)); |
| 82 | return MCDisassembler::Success; |
| 83 | } |
| 84 | |
Colin LeMahieu | 383c36e | 2014-12-05 18:24:06 +0000 | [diff] [blame] | 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 |
| 92 | }; |
| 93 | |
| 94 | return (DecodeRegisterClass(Inst, RegNo >> 1, |
| 95 | DoubleRegDecoderTable, |
| 96 | sizeof (DoubleRegDecoderTable))); |
| 97 | } |
| 98 | |
Colin LeMahieu | efa74e0 | 2014-11-18 20:28:11 +0000 | [diff] [blame] | 99 | static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
| 100 | uint64_t /*Address*/, |
| 101 | void const *Decoder) { |
| 102 | if (RegNo > 3) |
| 103 | return MCDisassembler::Fail; |
| 104 | |
| 105 | unsigned Register = PredRegDecoderTable[RegNo]; |
| 106 | Inst.addOperand(MCOperand::CreateReg(Register)); |
| 107 | return MCDisassembler::Success; |
| 108 | } |
| 109 | |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 110 | #include "HexagonGenDisassemblerTables.inc" |
| 111 | |
| 112 | static MCDisassembler *createHexagonDisassembler(Target const &T, |
| 113 | MCSubtargetInfo const &STI, |
| 114 | MCContext &Ctx) { |
| 115 | return new HexagonDisassembler(STI, Ctx); |
| 116 | } |
| 117 | |
| 118 | extern "C" void LLVMInitializeHexagonDisassembler() { |
| 119 | TargetRegistry::RegisterMCDisassembler(TheHexagonTarget, |
| 120 | createHexagonDisassembler); |
| 121 | } |
| 122 | |
| 123 | DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size, |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 124 | ArrayRef<uint8_t> Bytes, |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 125 | uint64_t Address, |
| 126 | raw_ostream &os, |
| 127 | raw_ostream &cs) const { |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 128 | Size = 4; |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 129 | if (Bytes.size() < 4) |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 130 | return MCDisassembler::Fail; |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 131 | |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 132 | uint32_t insn = |
| 133 | llvm::support::endian::read<uint32_t, llvm::support::little, |
| 134 | llvm::support::unaligned>(Bytes.data()); |
| 135 | |
| 136 | // Remove parse bits. |
| 137 | insn &= ~static_cast<uint32_t>(HexagonII::InstParseBits::INST_PARSE_MASK); |
Colin LeMahieu | 5d6f03b | 2014-12-04 03:41:21 +0000 | [diff] [blame] | 138 | DecodeStatus Result = decodeInstruction(DecoderTable32, MI, insn, Address, this, STI); |
| 139 | HexagonMCInst::AppendImplicitOperands(MI); |
| 140 | return Result; |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 141 | } |