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 | |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 10 | #include "Hexagon.h" |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 11 | #include "MCTargetDesc/HexagonBaseInfo.h" |
Colin LeMahieu | 1174fea | 2015-02-19 21:10:50 +0000 | [diff] [blame] | 12 | #include "MCTargetDesc/HexagonMCInstrInfo.h" |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 13 | #include "MCTargetDesc/HexagonMCTargetDesc.h" |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 14 | |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCContext.h" |
| 16 | #include "llvm/MC/MCDisassembler.h" |
| 17 | #include "llvm/MC/MCExpr.h" |
| 18 | #include "llvm/MC/MCFixedLenDisassembler.h" |
| 19 | #include "llvm/MC/MCInst.h" |
| 20 | #include "llvm/MC/MCInstrDesc.h" |
| 21 | #include "llvm/MC/MCSubtargetInfo.h" |
| 22 | #include "llvm/Support/Debug.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Endian.h" |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
| 25 | #include "llvm/Support/LEB128.h" |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 26 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 28 | #include <array> |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 29 | #include <vector> |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace llvm; |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 32 | using namespace Hexagon; |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 33 | |
| 34 | #define DEBUG_TYPE "hexagon-disassembler" |
| 35 | |
| 36 | // Pull DecodeStatus and its enum values into the global namespace. |
| 37 | typedef llvm::MCDisassembler::DecodeStatus DecodeStatus; |
| 38 | |
| 39 | namespace { |
| 40 | /// \brief Hexagon disassembler for all Hexagon platforms. |
| 41 | class HexagonDisassembler : public MCDisassembler { |
| 42 | public: |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 43 | std::unique_ptr<MCInst *> CurrentBundle; |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 44 | HexagonDisassembler(MCSubtargetInfo const &STI, MCContext &Ctx) |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 45 | : MCDisassembler(STI, Ctx), CurrentBundle(new MCInst *) {} |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 46 | |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 47 | DecodeStatus getSingleInstruction(MCInst &Instr, MCInst &MCB, |
| 48 | ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 49 | raw_ostream &VStream, raw_ostream &CStream, |
| 50 | bool &Complete) const; |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 51 | DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 52 | ArrayRef<uint8_t> Bytes, uint64_t Address, |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 53 | raw_ostream &VStream, |
| 54 | raw_ostream &CStream) const override; |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 55 | }; |
| 56 | } |
| 57 | |
Colin LeMahieu | ff370ed | 2014-12-26 20:30:58 +0000 | [diff] [blame] | 58 | static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 59 | uint64_t Address, const void *Decoder); |
Colin LeMahieu | f3db884 | 2014-12-19 19:06:32 +0000 | [diff] [blame] | 60 | static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 61 | uint64_t Address, const void *Decoder); |
Colin LeMahieu | 404d5b2 | 2015-02-10 16:59:36 +0000 | [diff] [blame] | 62 | static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo, |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 63 | uint64_t Address, void const *Decoder); |
Colin LeMahieu | f3db884 | 2014-12-19 19:06:32 +0000 | [diff] [blame] | 64 | |
Colin LeMahieu | efa74e0 | 2014-11-18 20:28:11 +0000 | [diff] [blame] | 65 | static const uint16_t IntRegDecoderTable[] = { |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 66 | Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4, |
| 67 | Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9, |
| 68 | Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14, |
| 69 | Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19, |
| 70 | Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24, |
| 71 | Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29, |
| 72 | Hexagon::R30, Hexagon::R31 }; |
Colin LeMahieu | efa74e0 | 2014-11-18 20:28:11 +0000 | [diff] [blame] | 73 | |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 74 | static const uint16_t PredRegDecoderTable[] = { Hexagon::P0, Hexagon::P1, |
| 75 | Hexagon::P2, Hexagon::P3 }; |
Colin LeMahieu | efa74e0 | 2014-11-18 20:28:11 +0000 | [diff] [blame] | 76 | |
Colin LeMahieu | 383c36e | 2014-12-05 18:24:06 +0000 | [diff] [blame] | 77 | static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo, |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 78 | const uint16_t Table[], size_t Size) { |
Colin LeMahieu | 383c36e | 2014-12-05 18:24:06 +0000 | [diff] [blame] | 79 | if (RegNo < Size) { |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 80 | Inst.addOperand(MCOperand::createReg(Table[RegNo])); |
Colin LeMahieu | 383c36e | 2014-12-05 18:24:06 +0000 | [diff] [blame] | 81 | return MCDisassembler::Success; |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 82 | } |
| 83 | else |
Colin LeMahieu | 383c36e | 2014-12-05 18:24:06 +0000 | [diff] [blame] | 84 | return MCDisassembler::Fail; |
| 85 | } |
| 86 | |
Colin LeMahieu | efa74e0 | 2014-11-18 20:28:11 +0000 | [diff] [blame] | 87 | static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 88 | uint64_t /*Address*/, |
| 89 | void const *Decoder) { |
Colin LeMahieu | efa74e0 | 2014-11-18 20:28:11 +0000 | [diff] [blame] | 90 | if (RegNo > 31) |
| 91 | return MCDisassembler::Fail; |
| 92 | |
| 93 | unsigned Register = IntRegDecoderTable[RegNo]; |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 94 | Inst.addOperand(MCOperand::createReg(Register)); |
Colin LeMahieu | efa74e0 | 2014-11-18 20:28:11 +0000 | [diff] [blame] | 95 | return MCDisassembler::Success; |
| 96 | } |
| 97 | |
Colin LeMahieu | f3db884 | 2014-12-19 19:06:32 +0000 | [diff] [blame] | 98 | static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 99 | uint64_t /*Address*/, const void *Decoder) { |
Colin LeMahieu | f3db884 | 2014-12-19 19:06:32 +0000 | [diff] [blame] | 100 | static const uint16_t CtrlRegDecoderTable[] = { |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 101 | Hexagon::SA0, Hexagon::LC0, Hexagon::SA1, Hexagon::LC1, |
| 102 | Hexagon::P3_0, Hexagon::NoRegister, Hexagon::C6, Hexagon::C7, |
| 103 | Hexagon::USR, Hexagon::PC, Hexagon::UGP, Hexagon::GP, |
| 104 | Hexagon::CS0, Hexagon::CS1, Hexagon::UPCL, Hexagon::UPCH |
| 105 | }; |
Colin LeMahieu | f3db884 | 2014-12-19 19:06:32 +0000 | [diff] [blame] | 106 | |
| 107 | if (RegNo >= sizeof(CtrlRegDecoderTable) / sizeof(CtrlRegDecoderTable[0])) |
| 108 | return MCDisassembler::Fail; |
| 109 | |
| 110 | if (CtrlRegDecoderTable[RegNo] == Hexagon::NoRegister) |
| 111 | return MCDisassembler::Fail; |
| 112 | |
| 113 | unsigned Register = CtrlRegDecoderTable[RegNo]; |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 114 | Inst.addOperand(MCOperand::createReg(Register)); |
Colin LeMahieu | f3db884 | 2014-12-19 19:06:32 +0000 | [diff] [blame] | 115 | return MCDisassembler::Success; |
| 116 | } |
| 117 | |
Colin LeMahieu | 404d5b2 | 2015-02-10 16:59:36 +0000 | [diff] [blame] | 118 | static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo, |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 119 | uint64_t /*Address*/, void const *Decoder) { |
Colin LeMahieu | 404d5b2 | 2015-02-10 16:59:36 +0000 | [diff] [blame] | 120 | static const uint16_t CtrlReg64DecoderTable[] = { |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 121 | Hexagon::C1_0, Hexagon::NoRegister, |
| 122 | Hexagon::C3_2, Hexagon::NoRegister, |
| 123 | Hexagon::NoRegister, Hexagon::NoRegister, |
| 124 | Hexagon::C7_6, Hexagon::NoRegister, |
| 125 | Hexagon::C9_8, Hexagon::NoRegister, |
| 126 | Hexagon::C11_10, Hexagon::NoRegister, |
| 127 | Hexagon::CS, Hexagon::NoRegister, |
| 128 | Hexagon::UPC, Hexagon::NoRegister |
| 129 | }; |
Colin LeMahieu | 404d5b2 | 2015-02-10 16:59:36 +0000 | [diff] [blame] | 130 | |
| 131 | if (RegNo >= sizeof(CtrlReg64DecoderTable) / sizeof(CtrlReg64DecoderTable[0])) |
| 132 | return MCDisassembler::Fail; |
| 133 | |
| 134 | if (CtrlReg64DecoderTable[RegNo] == Hexagon::NoRegister) |
| 135 | return MCDisassembler::Fail; |
| 136 | |
| 137 | unsigned Register = CtrlReg64DecoderTable[RegNo]; |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 138 | Inst.addOperand(MCOperand::createReg(Register)); |
Colin LeMahieu | 404d5b2 | 2015-02-10 16:59:36 +0000 | [diff] [blame] | 139 | return MCDisassembler::Success; |
| 140 | } |
| 141 | |
Colin LeMahieu | ff370ed | 2014-12-26 20:30:58 +0000 | [diff] [blame] | 142 | static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 143 | uint64_t /*Address*/, const void *Decoder) { |
Colin LeMahieu | ff370ed | 2014-12-26 20:30:58 +0000 | [diff] [blame] | 144 | unsigned Register = 0; |
| 145 | switch (RegNo) { |
| 146 | case 0: |
| 147 | Register = Hexagon::M0; |
| 148 | break; |
| 149 | case 1: |
| 150 | Register = Hexagon::M1; |
| 151 | break; |
| 152 | default: |
| 153 | return MCDisassembler::Fail; |
| 154 | } |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 155 | Inst.addOperand(MCOperand::createReg(Register)); |
Colin LeMahieu | ff370ed | 2014-12-26 20:30:58 +0000 | [diff] [blame] | 156 | return MCDisassembler::Success; |
| 157 | } |
| 158 | |
Colin LeMahieu | 383c36e | 2014-12-05 18:24:06 +0000 | [diff] [blame] | 159 | static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 160 | uint64_t /*Address*/, const void *Decoder) { |
Colin LeMahieu | 383c36e | 2014-12-05 18:24:06 +0000 | [diff] [blame] | 161 | static const uint16_t DoubleRegDecoderTable[] = { |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 162 | Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3, |
| 163 | Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7, |
| 164 | Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11, |
| 165 | Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15 |
| 166 | }; |
Colin LeMahieu | 383c36e | 2014-12-05 18:24:06 +0000 | [diff] [blame] | 167 | |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 168 | return (DecodeRegisterClass(Inst, RegNo >> 1, |
| 169 | DoubleRegDecoderTable, |
| 170 | sizeof (DoubleRegDecoderTable))); |
Colin LeMahieu | 383c36e | 2014-12-05 18:24:06 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Colin LeMahieu | efa74e0 | 2014-11-18 20:28:11 +0000 | [diff] [blame] | 173 | static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 174 | uint64_t /*Address*/, |
| 175 | void const *Decoder) { |
Colin LeMahieu | efa74e0 | 2014-11-18 20:28:11 +0000 | [diff] [blame] | 176 | if (RegNo > 3) |
| 177 | return MCDisassembler::Fail; |
| 178 | |
| 179 | unsigned Register = PredRegDecoderTable[RegNo]; |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 180 | Inst.addOperand(MCOperand::createReg(Register)); |
Colin LeMahieu | efa74e0 | 2014-11-18 20:28:11 +0000 | [diff] [blame] | 181 | return MCDisassembler::Success; |
| 182 | } |
| 183 | |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 184 | #include "HexagonGenDisassemblerTables.inc" |
| 185 | |
| 186 | static MCDisassembler *createHexagonDisassembler(Target const &T, |
| 187 | MCSubtargetInfo const &STI, |
| 188 | MCContext &Ctx) { |
| 189 | return new HexagonDisassembler(STI, Ctx); |
| 190 | } |
| 191 | |
| 192 | extern "C" void LLVMInitializeHexagonDisassembler() { |
| 193 | TargetRegistry::RegisterMCDisassembler(TheHexagonTarget, |
| 194 | createHexagonDisassembler); |
| 195 | } |
| 196 | |
| 197 | DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size, |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 198 | ArrayRef<uint8_t> Bytes, |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 199 | uint64_t Address, |
| 200 | raw_ostream &os, |
| 201 | raw_ostream &cs) const { |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 202 | DecodeStatus Result = DecodeStatus::Success; |
| 203 | bool Complete = false; |
| 204 | Size = 0; |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 205 | |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 206 | *CurrentBundle = &MI; |
| 207 | MI.setOpcode(Hexagon::BUNDLE); |
| 208 | MI.addOperand(MCOperand::createImm(0)); |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 209 | while (Result == Success && Complete == false) |
| 210 | { |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 211 | if (Bytes.size() < HEXAGON_INSTR_SIZE) |
| 212 | return MCDisassembler::Fail; |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 213 | MCInst * Inst = new (getContext()) MCInst; |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 214 | Result = getSingleInstruction(*Inst, MI, Bytes, Address, os, cs, Complete); |
| 215 | MI.addOperand(MCOperand::createInst(Inst)); |
| 216 | Size += HEXAGON_INSTR_SIZE; |
| 217 | Bytes = Bytes.slice(HEXAGON_INSTR_SIZE); |
| 218 | } |
| 219 | return Result; |
| 220 | } |
| 221 | |
| 222 | DecodeStatus HexagonDisassembler::getSingleInstruction( |
| 223 | MCInst &MI, MCInst &MCB, ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 224 | raw_ostream &os, raw_ostream &cs, bool &Complete) const { |
| 225 | assert(Bytes.size() >= HEXAGON_INSTR_SIZE); |
| 226 | |
| 227 | uint32_t Instruction = |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 228 | llvm::support::endian::read<uint32_t, llvm::support::little, |
| 229 | llvm::support::unaligned>(Bytes.data()); |
| 230 | |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 231 | auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB); |
| 232 | if ((Instruction & HexagonII::INST_PARSE_MASK) == |
| 233 | HexagonII::INST_PARSE_LOOP_END) { |
| 234 | if (BundleSize == 0) |
| 235 | HexagonMCInstrInfo::setInnerLoop(MCB); |
| 236 | else if (BundleSize == 1) |
| 237 | HexagonMCInstrInfo::setOuterLoop(MCB); |
| 238 | else |
| 239 | return DecodeStatus::Fail; |
| 240 | } |
| 241 | |
| 242 | DecodeStatus Result = DecodeStatus::Success; |
| 243 | if ((Instruction & HexagonII::INST_PARSE_MASK) == |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 244 | HexagonII::INST_PARSE_PACKET_END) |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 245 | Complete = true; |
Colin LeMahieu | c40be85 | 2015-06-04 21:32:42 +0000 | [diff] [blame^] | 246 | // Calling the auto-generated decoder function. |
| 247 | Result = |
| 248 | decodeInstruction(DecoderTable32, MI, Instruction, Address, this, STI); |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 249 | |
Colin LeMahieu | 5d6f03b | 2014-12-04 03:41:21 +0000 | [diff] [blame] | 250 | return Result; |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 251 | } |