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 | f3db884 | 2014-12-19 19:06:32 +0000 | [diff] [blame^] | 52 | static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
| 53 | uint64_t Address, const void *Decoder); |
| 54 | |
Colin LeMahieu | efa74e0 | 2014-11-18 20:28:11 +0000 | [diff] [blame] | 55 | static const uint16_t IntRegDecoderTable[] = { |
| 56 | Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4, |
| 57 | Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9, |
| 58 | Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14, |
| 59 | Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19, |
| 60 | Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24, |
| 61 | Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29, |
| 62 | Hexagon::R30, Hexagon::R31 }; |
| 63 | |
| 64 | static const uint16_t PredRegDecoderTable[] = { Hexagon::P0, Hexagon::P1, |
| 65 | Hexagon::P2, Hexagon::P3 }; |
| 66 | |
Colin LeMahieu | 383c36e | 2014-12-05 18:24:06 +0000 | [diff] [blame] | 67 | static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo, |
| 68 | const uint16_t Table[], size_t Size) { |
| 69 | if (RegNo < Size) { |
| 70 | Inst.addOperand(MCOperand::CreateReg(Table[RegNo])); |
| 71 | return MCDisassembler::Success; |
| 72 | } |
| 73 | else |
| 74 | return MCDisassembler::Fail; |
| 75 | } |
| 76 | |
Colin LeMahieu | efa74e0 | 2014-11-18 20:28:11 +0000 | [diff] [blame] | 77 | static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
| 78 | uint64_t /*Address*/, |
| 79 | void const *Decoder) { |
| 80 | if (RegNo > 31) |
| 81 | return MCDisassembler::Fail; |
| 82 | |
| 83 | unsigned Register = IntRegDecoderTable[RegNo]; |
| 84 | Inst.addOperand(MCOperand::CreateReg(Register)); |
| 85 | return MCDisassembler::Success; |
| 86 | } |
| 87 | |
Colin LeMahieu | f3db884 | 2014-12-19 19:06:32 +0000 | [diff] [blame^] | 88 | static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
| 89 | uint64_t /*Address*/, const void *Decoder) { |
| 90 | static const uint16_t CtrlRegDecoderTable[] = { |
| 91 | Hexagon::SA0, Hexagon::LC0, Hexagon::SA1, Hexagon::LC1, |
| 92 | Hexagon::P3_0, Hexagon::NoRegister, Hexagon::C6, Hexagon::C7, |
| 93 | Hexagon::USR, Hexagon::PC, Hexagon::UGP, Hexagon::GP, |
| 94 | Hexagon::CS0, Hexagon::CS1, Hexagon::UPCL, Hexagon::UPCH |
| 95 | }; |
| 96 | |
| 97 | if (RegNo >= sizeof(CtrlRegDecoderTable) / sizeof(CtrlRegDecoderTable[0])) |
| 98 | return MCDisassembler::Fail; |
| 99 | |
| 100 | if (CtrlRegDecoderTable[RegNo] == Hexagon::NoRegister) |
| 101 | return MCDisassembler::Fail; |
| 102 | |
| 103 | unsigned Register = CtrlRegDecoderTable[RegNo]; |
| 104 | Inst.addOperand(MCOperand::CreateReg(Register)); |
| 105 | return MCDisassembler::Success; |
| 106 | } |
| 107 | |
Colin LeMahieu | 383c36e | 2014-12-05 18:24:06 +0000 | [diff] [blame] | 108 | static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
| 109 | uint64_t /*Address*/, const void *Decoder) { |
| 110 | static const uint16_t DoubleRegDecoderTable[] = { |
| 111 | Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3, |
| 112 | Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7, |
| 113 | Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11, |
| 114 | Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15 |
| 115 | }; |
| 116 | |
| 117 | return (DecodeRegisterClass(Inst, RegNo >> 1, |
| 118 | DoubleRegDecoderTable, |
| 119 | sizeof (DoubleRegDecoderTable))); |
| 120 | } |
| 121 | |
Colin LeMahieu | efa74e0 | 2014-11-18 20:28:11 +0000 | [diff] [blame] | 122 | static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
| 123 | uint64_t /*Address*/, |
| 124 | void const *Decoder) { |
| 125 | if (RegNo > 3) |
| 126 | return MCDisassembler::Fail; |
| 127 | |
| 128 | unsigned Register = PredRegDecoderTable[RegNo]; |
| 129 | Inst.addOperand(MCOperand::CreateReg(Register)); |
| 130 | return MCDisassembler::Success; |
| 131 | } |
| 132 | |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 133 | #include "HexagonGenDisassemblerTables.inc" |
| 134 | |
| 135 | static MCDisassembler *createHexagonDisassembler(Target const &T, |
| 136 | MCSubtargetInfo const &STI, |
| 137 | MCContext &Ctx) { |
| 138 | return new HexagonDisassembler(STI, Ctx); |
| 139 | } |
| 140 | |
| 141 | extern "C" void LLVMInitializeHexagonDisassembler() { |
| 142 | TargetRegistry::RegisterMCDisassembler(TheHexagonTarget, |
| 143 | createHexagonDisassembler); |
| 144 | } |
| 145 | |
| 146 | DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size, |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 147 | ArrayRef<uint8_t> Bytes, |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 148 | uint64_t Address, |
| 149 | raw_ostream &os, |
| 150 | raw_ostream &cs) const { |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 151 | Size = 4; |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 152 | if (Bytes.size() < 4) |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 153 | return MCDisassembler::Fail; |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 154 | |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 155 | uint32_t insn = |
| 156 | llvm::support::endian::read<uint32_t, llvm::support::little, |
| 157 | llvm::support::unaligned>(Bytes.data()); |
| 158 | |
| 159 | // Remove parse bits. |
| 160 | insn &= ~static_cast<uint32_t>(HexagonII::InstParseBits::INST_PARSE_MASK); |
Colin LeMahieu | 5d6f03b | 2014-12-04 03:41:21 +0000 | [diff] [blame] | 161 | DecodeStatus Result = decodeInstruction(DecoderTable32, MI, insn, Address, this, STI); |
| 162 | HexagonMCInst::AppendImplicitOperands(MI); |
| 163 | return Result; |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 164 | } |