Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 1 | //===- HexagonDisassembler.cpp - Disassembler for Hexagon ISA -------------===// |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 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 | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 10 | #define DEBUG_TYPE "hexagon-disassembler" |
| 11 | |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 12 | #include "Hexagon.h" |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 13 | #include "MCTargetDesc/HexagonBaseInfo.h" |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 14 | #include "MCTargetDesc/HexagonMCChecker.h" |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/HexagonMCInstrInfo.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include "MCTargetDesc/HexagonMCTargetDesc.h" |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/ArrayRef.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCContext.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCExpr.h" |
| 22 | #include "llvm/MC/MCFixedLenDisassembler.h" |
| 23 | #include "llvm/MC/MCInst.h" |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCInstrInfo.h" |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCRegisterInfo.h" |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCSubtargetInfo.h" |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Endian.h" |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MathExtras.h" |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 29 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 31 | #include <cassert> |
| 32 | #include <cstddef> |
| 33 | #include <cstdint> |
| 34 | #include <memory> |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace llvm; |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 37 | using namespace Hexagon; |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 38 | |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 39 | using DecodeStatus = MCDisassembler::DecodeStatus; |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 40 | |
| 41 | namespace { |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 42 | |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 43 | /// \brief Hexagon disassembler for all Hexagon platforms. |
| 44 | class HexagonDisassembler : public MCDisassembler { |
| 45 | public: |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 46 | std::unique_ptr<MCInstrInfo const> const MCII; |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 47 | std::unique_ptr<MCInst *> CurrentBundle; |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 48 | mutable MCInst const *CurrentExtender; |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 49 | |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 50 | HexagonDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, |
| 51 | MCInstrInfo const *MCII) |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 52 | : MCDisassembler(STI, Ctx), MCII(MCII), CurrentBundle(new MCInst *), |
| 53 | CurrentExtender(nullptr) {} |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 54 | |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 55 | DecodeStatus getSingleInstruction(MCInst &Instr, MCInst &MCB, |
| 56 | ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 57 | raw_ostream &VStream, raw_ostream &CStream, |
| 58 | bool &Complete) const; |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 59 | DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 60 | ArrayRef<uint8_t> Bytes, uint64_t Address, |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 61 | raw_ostream &VStream, |
| 62 | raw_ostream &CStream) const override; |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 63 | void remapInstruction(MCInst &Instr) const; |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 64 | }; |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 65 | |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 66 | static uint64_t fullValue(HexagonDisassembler const &Disassembler, MCInst &MI, |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 67 | int64_t Value) { |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 68 | MCInstrInfo MCII = *Disassembler.MCII; |
| 69 | if (!Disassembler.CurrentExtender || |
| 70 | MI.size() != HexagonMCInstrInfo::getExtendableOp(MCII, MI)) |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 71 | return Value; |
| 72 | unsigned Alignment = HexagonMCInstrInfo::getExtentAlignment(MCII, MI); |
| 73 | uint32_t Lower6 = static_cast<uint32_t>(Value >> Alignment) & 0x3f; |
| 74 | int64_t Bits; |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 75 | bool Success = |
| 76 | Disassembler.CurrentExtender->getOperand(0).getExpr()->evaluateAsAbsolute( |
| 77 | Bits); |
| 78 | assert(Success); |
| 79 | (void)Success; |
| 80 | uint64_t Upper26 = static_cast<uint64_t>(Bits); |
| 81 | uint64_t Operand = Upper26 | Lower6; |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 82 | return Operand; |
| 83 | } |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 84 | static HexagonDisassembler const &disassembler(void const *Decoder) { |
| 85 | return *static_cast<HexagonDisassembler const *>(Decoder); |
| 86 | } |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 87 | template <size_t T> |
| 88 | static void signedDecoder(MCInst &MI, unsigned tmp, const void *Decoder) { |
| 89 | HexagonDisassembler const &Disassembler = disassembler(Decoder); |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 90 | int64_t FullValue = fullValue(Disassembler, MI, SignExtend64<T>(tmp)); |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 91 | int64_t Extended = SignExtend64<32>(FullValue); |
| 92 | HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext()); |
| 93 | } |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 94 | } |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 95 | |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 96 | // Forward declare these because the auto-generated code will reference them. |
| 97 | // Definitions are further down. |
| 98 | |
| 99 | static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 100 | uint64_t Address, |
| 101 | const void *Decoder); |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 102 | static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst, |
| 103 | unsigned RegNo, |
| 104 | uint64_t Address, |
| 105 | const void *Decoder); |
Colin LeMahieu | 7c95871 | 2015-10-17 01:33:04 +0000 | [diff] [blame] | 106 | static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo, |
| 107 | uint64_t Address, |
| 108 | const void *Decoder); |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 109 | static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo, |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 110 | uint64_t Address, |
| 111 | const void *Decoder); |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 112 | static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
| 113 | uint64_t Address, |
| 114 | const void *Decoder); |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 115 | static DecodeStatus |
| 116 | DecodeGeneralDoubleLow8RegsRegisterClass(MCInst &Inst, unsigned RegNo, |
| 117 | uint64_t Address, const void *Decoder); |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 118 | static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo, |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 119 | uint64_t Address, |
| 120 | const void *Decoder); |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 121 | static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
| 122 | uint64_t Address, |
| 123 | const void *Decoder); |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 124 | static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo, |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 125 | uint64_t Address, |
| 126 | const void *Decoder); |
Colin LeMahieu | f3db884 | 2014-12-19 19:06:32 +0000 | [diff] [blame] | 127 | static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 128 | uint64_t Address, |
| 129 | const void *Decoder); |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 130 | static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
| 131 | uint64_t Address, |
| 132 | const void *Decoder); |
Colin LeMahieu | 404d5b2 | 2015-02-10 16:59:36 +0000 | [diff] [blame] | 133 | static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo, |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 134 | uint64_t Address, |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 135 | const void *Decoder); |
| 136 | |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 137 | static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp, |
| 138 | uint64_t Address, const void *Decoder); |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 139 | static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp, |
| 140 | uint64_t /*Address*/, const void *Decoder); |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 141 | static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address, |
| 142 | const void *Decoder); |
Colin LeMahieu | efa74e0 | 2014-11-18 20:28:11 +0000 | [diff] [blame] | 143 | |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 144 | #include "HexagonDepDecoders.h" |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 145 | #include "HexagonGenDisassemblerTables.inc" |
| 146 | |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 147 | static MCDisassembler *createHexagonDisassembler(const Target &T, |
| 148 | const MCSubtargetInfo &STI, |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 149 | MCContext &Ctx) { |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 150 | return new HexagonDisassembler(STI, Ctx, T.createMCInstrInfo()); |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | extern "C" void LLVMInitializeHexagonDisassembler() { |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 154 | TargetRegistry::RegisterMCDisassembler(getTheHexagonTarget(), |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 155 | createHexagonDisassembler); |
| 156 | } |
| 157 | |
| 158 | DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size, |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 159 | ArrayRef<uint8_t> Bytes, |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 160 | uint64_t Address, |
| 161 | raw_ostream &os, |
| 162 | raw_ostream &cs) const { |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 163 | (void)&s10_0ImmDecoder; |
| 164 | (void)&s10_6ImmDecoder; |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 165 | DecodeStatus Result = DecodeStatus::Success; |
| 166 | bool Complete = false; |
| 167 | Size = 0; |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 168 | |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 169 | *CurrentBundle = &MI; |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 170 | MI.setOpcode(Hexagon::BUNDLE); |
| 171 | MI.addOperand(MCOperand::createImm(0)); |
Eugene Zelenko | 8208592 | 2016-12-13 22:13:50 +0000 | [diff] [blame] | 172 | while (Result == Success && !Complete) { |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 173 | if (Bytes.size() < HEXAGON_INSTR_SIZE) |
| 174 | return MCDisassembler::Fail; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 175 | MCInst *Inst = new (getContext()) MCInst; |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 176 | Result = getSingleInstruction(*Inst, MI, Bytes, Address, os, cs, Complete); |
| 177 | MI.addOperand(MCOperand::createInst(Inst)); |
| 178 | Size += HEXAGON_INSTR_SIZE; |
| 179 | Bytes = Bytes.slice(HEXAGON_INSTR_SIZE); |
| 180 | } |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 181 | if (Result == MCDisassembler::Fail) |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 182 | return Result; |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 183 | if (Size > HEXAGON_MAX_PACKET_SIZE) |
| 184 | return MCDisassembler::Fail; |
Krzysztof Parzyszek | e12d1e7 | 2017-05-01 19:41:43 +0000 | [diff] [blame] | 185 | HexagonMCChecker Checker(getContext(), *MCII, STI, MI, |
| 186 | *getContext().getRegisterInfo(), false); |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 187 | if (!Checker.check()) |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 188 | return MCDisassembler::Fail; |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 189 | remapInstruction(MI); |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 190 | return MCDisassembler::Success; |
| 191 | } |
| 192 | |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 193 | void HexagonDisassembler::remapInstruction(MCInst &Instr) const { |
| 194 | for (auto I: HexagonMCInstrInfo::bundleInstructions(Instr)) { |
| 195 | auto &MI = const_cast<MCInst &>(*I.getInst()); |
| 196 | switch (MI.getOpcode()) { |
| 197 | case Hexagon::S2_allocframe: |
| 198 | if (MI.getOperand(0).getReg() == Hexagon::R29) { |
| 199 | MI.setOpcode(Hexagon::S6_allocframe_to_raw); |
| 200 | MI.erase(MI.begin () + 1); |
| 201 | MI.erase(MI.begin ()); |
| 202 | } |
| 203 | break; |
| 204 | case Hexagon::L2_deallocframe: |
| 205 | if (MI.getOperand(0).getReg() == Hexagon::D15 && |
| 206 | MI.getOperand(1).getReg() == Hexagon::R30) { |
| 207 | MI.setOpcode(L6_deallocframe_map_to_raw); |
| 208 | MI.erase(MI.begin () + 1); |
| 209 | MI.erase(MI.begin ()); |
| 210 | } |
| 211 | break; |
| 212 | case Hexagon::L4_return: |
| 213 | if (MI.getOperand(0).getReg() == Hexagon::D15 && |
| 214 | MI.getOperand(1).getReg() == Hexagon::R30) { |
| 215 | MI.setOpcode(L6_return_map_to_raw); |
| 216 | MI.erase(MI.begin () + 1); |
| 217 | MI.erase(MI.begin ()); |
| 218 | } |
| 219 | break; |
| 220 | case Hexagon::L4_return_t: |
| 221 | if (MI.getOperand(0).getReg() == Hexagon::D15 && |
| 222 | MI.getOperand(2).getReg() == Hexagon::R30) { |
| 223 | MI.setOpcode(L4_return_map_to_raw_t); |
| 224 | MI.erase(MI.begin () + 2); |
| 225 | MI.erase(MI.begin ()); |
| 226 | } |
| 227 | break; |
| 228 | case Hexagon::L4_return_f: |
| 229 | if (MI.getOperand(0).getReg() == Hexagon::D15 && |
| 230 | MI.getOperand(2).getReg() == Hexagon::R30) { |
| 231 | MI.setOpcode(L4_return_map_to_raw_f); |
| 232 | MI.erase(MI.begin () + 2); |
| 233 | MI.erase(MI.begin ()); |
| 234 | } |
| 235 | break; |
| 236 | case Hexagon::L4_return_tnew_pt: |
| 237 | if (MI.getOperand(0).getReg() == Hexagon::D15 && |
| 238 | MI.getOperand(2).getReg() == Hexagon::R30) { |
| 239 | MI.setOpcode(L4_return_map_to_raw_tnew_pt); |
| 240 | MI.erase(MI.begin () + 2); |
| 241 | MI.erase(MI.begin ()); |
| 242 | } |
| 243 | break; |
| 244 | case Hexagon::L4_return_fnew_pt: |
| 245 | if (MI.getOperand(0).getReg() == Hexagon::D15 && |
| 246 | MI.getOperand(2).getReg() == Hexagon::R30) { |
| 247 | MI.setOpcode(L4_return_map_to_raw_fnew_pt); |
| 248 | MI.erase(MI.begin () + 2); |
| 249 | MI.erase(MI.begin ()); |
| 250 | } |
| 251 | break; |
| 252 | case Hexagon::L4_return_tnew_pnt: |
| 253 | if (MI.getOperand(0).getReg() == Hexagon::D15 && |
| 254 | MI.getOperand(2).getReg() == Hexagon::R30) { |
| 255 | MI.setOpcode(L4_return_map_to_raw_tnew_pnt); |
| 256 | MI.erase(MI.begin () + 2); |
| 257 | MI.erase(MI.begin ()); |
| 258 | } |
| 259 | break; |
| 260 | case Hexagon::L4_return_fnew_pnt: |
| 261 | if (MI.getOperand(0).getReg() == Hexagon::D15 && |
| 262 | MI.getOperand(2).getReg() == Hexagon::R30) { |
| 263 | MI.setOpcode(L4_return_map_to_raw_fnew_pnt); |
| 264 | MI.erase(MI.begin () + 2); |
| 265 | MI.erase(MI.begin ()); |
| 266 | } |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 272 | static void adjustDuplex(MCInst &MI, MCContext &Context) { |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 273 | switch (MI.getOpcode()) { |
| 274 | case Hexagon::SA1_setin1: |
| 275 | MI.insert(MI.begin() + 1, |
| 276 | MCOperand::createExpr(MCConstantExpr::create(-1, Context))); |
| 277 | break; |
| 278 | case Hexagon::SA1_dec: |
| 279 | MI.insert(MI.begin() + 2, |
| 280 | MCOperand::createExpr(MCConstantExpr::create(-1, Context))); |
| 281 | break; |
| 282 | default: |
| 283 | break; |
| 284 | } |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 285 | } |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 286 | |
| 287 | DecodeStatus HexagonDisassembler::getSingleInstruction( |
| 288 | MCInst &MI, MCInst &MCB, ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 289 | raw_ostream &os, raw_ostream &cs, bool &Complete) const { |
| 290 | assert(Bytes.size() >= HEXAGON_INSTR_SIZE); |
| 291 | |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 292 | uint32_t Instruction = support::endian::read32le(Bytes.data()); |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 293 | |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 294 | auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB); |
| 295 | if ((Instruction & HexagonII::INST_PARSE_MASK) == |
| 296 | HexagonII::INST_PARSE_LOOP_END) { |
| 297 | if (BundleSize == 0) |
| 298 | HexagonMCInstrInfo::setInnerLoop(MCB); |
| 299 | else if (BundleSize == 1) |
| 300 | HexagonMCInstrInfo::setOuterLoop(MCB); |
| 301 | else |
| 302 | return DecodeStatus::Fail; |
| 303 | } |
| 304 | |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 305 | CurrentExtender = HexagonMCInstrInfo::extenderForIndex( |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 306 | MCB, HexagonMCInstrInfo::bundleSize(MCB)); |
| 307 | |
| 308 | DecodeStatus Result = DecodeStatus::Fail; |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 309 | if ((Instruction & HexagonII::INST_PARSE_MASK) == |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 310 | HexagonII::INST_PARSE_DUPLEX) { |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 311 | unsigned duplexIClass; |
| 312 | uint8_t const *DecodeLow, *DecodeHigh; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 313 | duplexIClass = ((Instruction >> 28) & 0xe) | ((Instruction >> 13) & 0x1); |
| 314 | switch (duplexIClass) { |
| 315 | default: |
| 316 | return MCDisassembler::Fail; |
| 317 | case 0: |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 318 | DecodeLow = DecoderTableSUBINSN_L132; |
| 319 | DecodeHigh = DecoderTableSUBINSN_L132; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 320 | break; |
| 321 | case 1: |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 322 | DecodeLow = DecoderTableSUBINSN_L232; |
| 323 | DecodeHigh = DecoderTableSUBINSN_L132; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 324 | break; |
| 325 | case 2: |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 326 | DecodeLow = DecoderTableSUBINSN_L232; |
| 327 | DecodeHigh = DecoderTableSUBINSN_L232; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 328 | break; |
| 329 | case 3: |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 330 | DecodeLow = DecoderTableSUBINSN_A32; |
| 331 | DecodeHigh = DecoderTableSUBINSN_A32; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 332 | break; |
| 333 | case 4: |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 334 | DecodeLow = DecoderTableSUBINSN_L132; |
| 335 | DecodeHigh = DecoderTableSUBINSN_A32; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 336 | break; |
| 337 | case 5: |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 338 | DecodeLow = DecoderTableSUBINSN_L232; |
| 339 | DecodeHigh = DecoderTableSUBINSN_A32; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 340 | break; |
| 341 | case 6: |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 342 | DecodeLow = DecoderTableSUBINSN_S132; |
| 343 | DecodeHigh = DecoderTableSUBINSN_A32; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 344 | break; |
| 345 | case 7: |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 346 | DecodeLow = DecoderTableSUBINSN_S232; |
| 347 | DecodeHigh = DecoderTableSUBINSN_A32; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 348 | break; |
| 349 | case 8: |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 350 | DecodeLow = DecoderTableSUBINSN_S132; |
| 351 | DecodeHigh = DecoderTableSUBINSN_L132; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 352 | break; |
| 353 | case 9: |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 354 | DecodeLow = DecoderTableSUBINSN_S132; |
| 355 | DecodeHigh = DecoderTableSUBINSN_L232; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 356 | break; |
| 357 | case 10: |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 358 | DecodeLow = DecoderTableSUBINSN_S132; |
| 359 | DecodeHigh = DecoderTableSUBINSN_S132; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 360 | break; |
| 361 | case 11: |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 362 | DecodeLow = DecoderTableSUBINSN_S232; |
| 363 | DecodeHigh = DecoderTableSUBINSN_S132; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 364 | break; |
| 365 | case 12: |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 366 | DecodeLow = DecoderTableSUBINSN_S232; |
| 367 | DecodeHigh = DecoderTableSUBINSN_L132; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 368 | break; |
| 369 | case 13: |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 370 | DecodeLow = DecoderTableSUBINSN_S232; |
| 371 | DecodeHigh = DecoderTableSUBINSN_L232; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 372 | break; |
| 373 | case 14: |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 374 | DecodeLow = DecoderTableSUBINSN_S232; |
| 375 | DecodeHigh = DecoderTableSUBINSN_S232; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 376 | break; |
| 377 | } |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 378 | MI.setOpcode(Hexagon::DuplexIClass0 + duplexIClass); |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 379 | MCInst *MILow = new (getContext()) MCInst; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 380 | MCInst *MIHigh = new (getContext()) MCInst; |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 381 | auto TmpExtender = CurrentExtender; |
| 382 | CurrentExtender = |
| 383 | nullptr; // constant extenders in duplex must always be in slot 1 |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 384 | Result = decodeInstruction(DecodeLow, *MILow, Instruction & 0x1fff, Address, |
| 385 | this, STI); |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 386 | CurrentExtender = TmpExtender; |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 387 | if (Result != DecodeStatus::Success) |
| 388 | return DecodeStatus::Fail; |
| 389 | adjustDuplex(*MILow, getContext()); |
| 390 | Result = decodeInstruction( |
| 391 | DecodeHigh, *MIHigh, (Instruction >> 16) & 0x1fff, Address, this, STI); |
| 392 | if (Result != DecodeStatus::Success) |
| 393 | return DecodeStatus::Fail; |
| 394 | adjustDuplex(*MIHigh, getContext()); |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 395 | MCOperand OPLow = MCOperand::createInst(MILow); |
| 396 | MCOperand OPHigh = MCOperand::createInst(MIHigh); |
| 397 | MI.addOperand(OPLow); |
| 398 | MI.addOperand(OPHigh); |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 399 | Complete = true; |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 400 | } else { |
| 401 | if ((Instruction & HexagonII::INST_PARSE_MASK) == |
| 402 | HexagonII::INST_PARSE_PACKET_END) |
| 403 | Complete = true; |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 404 | |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 405 | if (CurrentExtender != nullptr) |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 406 | Result = decodeInstruction(DecoderTableMustExtend32, MI, Instruction, |
| 407 | Address, this, STI); |
| 408 | |
| 409 | if (Result != MCDisassembler::Success) |
| 410 | Result = decodeInstruction(DecoderTable32, MI, Instruction, Address, this, |
| 411 | STI); |
| 412 | |
| 413 | if (Result != MCDisassembler::Success && |
| 414 | STI.getFeatureBits()[Hexagon::ExtensionHVX]) |
| 415 | Result = decodeInstruction(DecoderTableEXT_mmvec32, MI, Instruction, |
| 416 | Address, this, STI); |
| 417 | |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 418 | } |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 419 | |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 420 | switch (MI.getOpcode()) { |
Colin LeMahieu | 8170754 | 2016-12-05 04:29:00 +0000 | [diff] [blame] | 421 | case Hexagon::J4_cmpeqn1_f_jumpnv_nt: |
| 422 | case Hexagon::J4_cmpeqn1_f_jumpnv_t: |
| 423 | case Hexagon::J4_cmpeqn1_fp0_jump_nt: |
| 424 | case Hexagon::J4_cmpeqn1_fp0_jump_t: |
| 425 | case Hexagon::J4_cmpeqn1_fp1_jump_nt: |
| 426 | case Hexagon::J4_cmpeqn1_fp1_jump_t: |
| 427 | case Hexagon::J4_cmpeqn1_t_jumpnv_nt: |
| 428 | case Hexagon::J4_cmpeqn1_t_jumpnv_t: |
| 429 | case Hexagon::J4_cmpeqn1_tp0_jump_nt: |
| 430 | case Hexagon::J4_cmpeqn1_tp0_jump_t: |
| 431 | case Hexagon::J4_cmpeqn1_tp1_jump_nt: |
| 432 | case Hexagon::J4_cmpeqn1_tp1_jump_t: |
| 433 | case Hexagon::J4_cmpgtn1_f_jumpnv_nt: |
| 434 | case Hexagon::J4_cmpgtn1_f_jumpnv_t: |
| 435 | case Hexagon::J4_cmpgtn1_fp0_jump_nt: |
| 436 | case Hexagon::J4_cmpgtn1_fp0_jump_t: |
| 437 | case Hexagon::J4_cmpgtn1_fp1_jump_nt: |
| 438 | case Hexagon::J4_cmpgtn1_fp1_jump_t: |
| 439 | case Hexagon::J4_cmpgtn1_t_jumpnv_nt: |
| 440 | case Hexagon::J4_cmpgtn1_t_jumpnv_t: |
| 441 | case Hexagon::J4_cmpgtn1_tp0_jump_nt: |
| 442 | case Hexagon::J4_cmpgtn1_tp0_jump_t: |
| 443 | case Hexagon::J4_cmpgtn1_tp1_jump_nt: |
| 444 | case Hexagon::J4_cmpgtn1_tp1_jump_t: |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 445 | MI.insert(MI.begin() + 1, |
| 446 | MCOperand::createExpr(MCConstantExpr::create(-1, getContext()))); |
Colin LeMahieu | 8170754 | 2016-12-05 04:29:00 +0000 | [diff] [blame] | 447 | break; |
| 448 | default: |
| 449 | break; |
| 450 | } |
| 451 | |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 452 | if (HexagonMCInstrInfo::isNewValue(*MCII, MI)) { |
| 453 | unsigned OpIndex = HexagonMCInstrInfo::getNewValueOp(*MCII, MI); |
| 454 | MCOperand &MCO = MI.getOperand(OpIndex); |
| 455 | assert(MCO.isReg() && "New value consumers must be registers"); |
| 456 | unsigned Register = |
| 457 | getContext().getRegisterInfo()->getEncodingValue(MCO.getReg()); |
| 458 | if ((Register & 0x6) == 0) |
| 459 | // HexagonPRM 10.11 Bit 1-2 == 0 is reserved |
| 460 | return MCDisassembler::Fail; |
| 461 | unsigned Lookback = (Register & 0x6) >> 1; |
| 462 | unsigned Offset = 1; |
| 463 | bool Vector = HexagonMCInstrInfo::isVector(*MCII, MI); |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 464 | bool PrevVector = false; |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 465 | auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle); |
| 466 | auto i = Instructions.end() - 1; |
| 467 | for (auto n = Instructions.begin() - 1;; --i, ++Offset) { |
| 468 | if (i == n) |
| 469 | // Couldn't find producer |
| 470 | return MCDisassembler::Fail; |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 471 | bool CurrentVector = HexagonMCInstrInfo::isVector(*MCII, *i->getInst()); |
| 472 | if (Vector && !CurrentVector) |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 473 | // Skip scalars when calculating distances for vectors |
| 474 | ++Lookback; |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 475 | if (HexagonMCInstrInfo::isImmext(*i->getInst()) && (Vector == PrevVector)) |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 476 | ++Lookback; |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 477 | PrevVector = CurrentVector; |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 478 | if (Offset == Lookback) |
| 479 | break; |
| 480 | } |
| 481 | auto const &Inst = *i->getInst(); |
| 482 | bool SubregBit = (Register & 0x1) != 0; |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 483 | if (HexagonMCInstrInfo::hasNewValue2(*MCII, Inst)) { |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 484 | // If subreg bit is set we're selecting the second produced newvalue |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 485 | unsigned Producer = SubregBit ? |
| 486 | HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg() : |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 487 | HexagonMCInstrInfo::getNewValueOperand2(*MCII, Inst).getReg(); |
| 488 | assert(Producer != Hexagon::NoRegister); |
| 489 | MCO.setReg(Producer); |
| 490 | } else if (HexagonMCInstrInfo::hasNewValue(*MCII, Inst)) { |
| 491 | unsigned Producer = |
| 492 | HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg(); |
| 493 | if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15) |
| 494 | Producer = ((Producer - Hexagon::W0) << 1) + SubregBit + Hexagon::V0; |
| 495 | else if (SubregBit) |
Colin LeMahieu | 2d497a0 | 2016-03-01 22:05:03 +0000 | [diff] [blame] | 496 | // Hexagon PRM 10.11 New-value operands |
| 497 | // Nt[0] is reserved and should always be encoded as zero. |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 498 | return MCDisassembler::Fail; |
| 499 | assert(Producer != Hexagon::NoRegister); |
| 500 | MCO.setReg(Producer); |
| 501 | } else |
| 502 | return MCDisassembler::Fail; |
| 503 | } |
| 504 | |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 505 | if (CurrentExtender != nullptr) { |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 506 | MCInst const &Inst = HexagonMCInstrInfo::isDuplex(*MCII, MI) |
| 507 | ? *MI.getOperand(1).getInst() |
| 508 | : MI; |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 509 | if (!HexagonMCInstrInfo::isExtendable(*MCII, Inst) && |
| 510 | !HexagonMCInstrInfo::isExtended(*MCII, Inst)) |
| 511 | return MCDisassembler::Fail; |
| 512 | } |
Colin LeMahieu | 5d6f03b | 2014-12-04 03:41:21 +0000 | [diff] [blame] | 513 | return Result; |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 514 | } |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 515 | |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 516 | static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo, |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 517 | ArrayRef<MCPhysReg> Table) { |
Craig Topper | 3da000c | 2015-12-01 06:13:04 +0000 | [diff] [blame] | 518 | if (RegNo < Table.size()) { |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 519 | Inst.addOperand(MCOperand::createReg(Table[RegNo])); |
| 520 | return MCDisassembler::Success; |
Craig Topper | 3da000c | 2015-12-01 06:13:04 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | return MCDisassembler::Fail; |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Colin LeMahieu | 7c95871 | 2015-10-17 01:33:04 +0000 | [diff] [blame] | 526 | static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo, |
| 527 | uint64_t Address, |
| 528 | const void *Decoder) { |
| 529 | return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder); |
| 530 | } |
| 531 | |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 532 | static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
| 533 | uint64_t Address, |
| 534 | const void *Decoder) { |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 535 | static const MCPhysReg IntRegDecoderTable[] = { |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 536 | Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4, |
| 537 | Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9, |
| 538 | Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14, |
| 539 | Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19, |
| 540 | Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24, |
| 541 | Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29, |
| 542 | Hexagon::R30, Hexagon::R31}; |
| 543 | |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 544 | return DecodeRegisterClass(Inst, RegNo, IntRegDecoderTable); |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 545 | } |
Colin LeMahieu | 7c95871 | 2015-10-17 01:33:04 +0000 | [diff] [blame] | 546 | |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 547 | static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst, |
| 548 | unsigned RegNo, |
| 549 | uint64_t Address, |
| 550 | const void *Decoder) { |
| 551 | static const MCPhysReg GeneralSubRegDecoderTable[] = { |
| 552 | Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, |
| 553 | Hexagon::R4, Hexagon::R5, Hexagon::R6, Hexagon::R7, |
| 554 | Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19, |
| 555 | Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, |
| 556 | }; |
| 557 | |
| 558 | return DecodeRegisterClass(Inst, RegNo, GeneralSubRegDecoderTable); |
| 559 | } |
| 560 | |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 561 | static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo, |
| 562 | uint64_t /*Address*/, |
| 563 | const void *Decoder) { |
| 564 | static const MCPhysReg HvxVRDecoderTable[] = { |
Colin LeMahieu | 7c95871 | 2015-10-17 01:33:04 +0000 | [diff] [blame] | 565 | Hexagon::V0, Hexagon::V1, Hexagon::V2, Hexagon::V3, Hexagon::V4, |
| 566 | Hexagon::V5, Hexagon::V6, Hexagon::V7, Hexagon::V8, Hexagon::V9, |
| 567 | Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14, |
| 568 | Hexagon::V15, Hexagon::V16, Hexagon::V17, Hexagon::V18, Hexagon::V19, |
| 569 | Hexagon::V20, Hexagon::V21, Hexagon::V22, Hexagon::V23, Hexagon::V24, |
| 570 | Hexagon::V25, Hexagon::V26, Hexagon::V27, Hexagon::V28, Hexagon::V29, |
| 571 | Hexagon::V30, Hexagon::V31}; |
| 572 | |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 573 | return DecodeRegisterClass(Inst, RegNo, HvxVRDecoderTable); |
Colin LeMahieu | 7c95871 | 2015-10-17 01:33:04 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 576 | static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
| 577 | uint64_t /*Address*/, |
| 578 | const void *Decoder) { |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 579 | static const MCPhysReg DoubleRegDecoderTable[] = { |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 580 | Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3, |
| 581 | Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7, |
| 582 | Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11, |
| 583 | Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15}; |
| 584 | |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 585 | return DecodeRegisterClass(Inst, RegNo >> 1, DoubleRegDecoderTable); |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 588 | static DecodeStatus DecodeGeneralDoubleLow8RegsRegisterClass( |
| 589 | MCInst &Inst, unsigned RegNo, uint64_t /*Address*/, const void *Decoder) { |
| 590 | static const MCPhysReg GeneralDoubleLow8RegDecoderTable[] = { |
| 591 | Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3, |
| 592 | Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11}; |
| 593 | |
| 594 | return DecodeRegisterClass(Inst, RegNo, GeneralDoubleLow8RegDecoderTable); |
| 595 | } |
| 596 | |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 597 | static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo, |
| 598 | uint64_t /*Address*/, |
| 599 | const void *Decoder) { |
| 600 | static const MCPhysReg HvxWRDecoderTable[] = { |
Colin LeMahieu | 7c95871 | 2015-10-17 01:33:04 +0000 | [diff] [blame] | 601 | Hexagon::W0, Hexagon::W1, Hexagon::W2, Hexagon::W3, |
| 602 | Hexagon::W4, Hexagon::W5, Hexagon::W6, Hexagon::W7, |
| 603 | Hexagon::W8, Hexagon::W9, Hexagon::W10, Hexagon::W11, |
| 604 | Hexagon::W12, Hexagon::W13, Hexagon::W14, Hexagon::W15}; |
| 605 | |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 606 | return (DecodeRegisterClass(Inst, RegNo >> 1, HvxWRDecoderTable)); |
Colin LeMahieu | 7c95871 | 2015-10-17 01:33:04 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 609 | static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
| 610 | uint64_t /*Address*/, |
| 611 | const void *Decoder) { |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 612 | static const MCPhysReg PredRegDecoderTable[] = {Hexagon::P0, Hexagon::P1, |
| 613 | Hexagon::P2, Hexagon::P3}; |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 614 | |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 615 | return DecodeRegisterClass(Inst, RegNo, PredRegDecoderTable); |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 618 | static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo, |
| 619 | uint64_t /*Address*/, |
| 620 | const void *Decoder) { |
| 621 | static const MCPhysReg HvxQRDecoderTable[] = {Hexagon::Q0, Hexagon::Q1, |
| 622 | Hexagon::Q2, Hexagon::Q3}; |
Colin LeMahieu | 7c95871 | 2015-10-17 01:33:04 +0000 | [diff] [blame] | 623 | |
Krzysztof Parzyszek | 5577297 | 2017-09-15 15:46:05 +0000 | [diff] [blame] | 624 | return DecodeRegisterClass(Inst, RegNo, HvxQRDecoderTable); |
Colin LeMahieu | 7c95871 | 2015-10-17 01:33:04 +0000 | [diff] [blame] | 625 | } |
Colin LeMahieu | be8c453 | 2015-06-05 16:00:11 +0000 | [diff] [blame] | 626 | |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 627 | static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
| 628 | uint64_t /*Address*/, |
| 629 | const void *Decoder) { |
Krzysztof Parzyszek | f9015e6 | 2017-02-10 23:46:45 +0000 | [diff] [blame] | 630 | using namespace Hexagon; |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 631 | |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 632 | static const MCPhysReg CtrlRegDecoderTable[] = { |
Krzysztof Parzyszek | f9015e6 | 2017-02-10 23:46:45 +0000 | [diff] [blame] | 633 | /* 0 */ SA0, LC0, SA1, LC1, |
Krzysztof Parzyszek | e260332 | 2017-05-05 22:12:12 +0000 | [diff] [blame] | 634 | /* 4 */ P3_0, C5, M0, M1, |
Krzysztof Parzyszek | f9015e6 | 2017-02-10 23:46:45 +0000 | [diff] [blame] | 635 | /* 8 */ USR, PC, UGP, GP, |
Krzysztof Parzyszek | ab57c2b | 2017-02-22 22:28:47 +0000 | [diff] [blame] | 636 | /* 12 */ CS0, CS1, UPCYCLELO, UPCYCLEHI, |
Krzysztof Parzyszek | f9015e6 | 2017-02-10 23:46:45 +0000 | [diff] [blame] | 637 | /* 16 */ FRAMELIMIT, FRAMEKEY, PKTCOUNTLO, PKTCOUNTHI, |
| 638 | /* 20 */ 0, 0, 0, 0, |
| 639 | /* 24 */ 0, 0, 0, 0, |
| 640 | /* 28 */ 0, 0, UTIMERLO, UTIMERHI |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 641 | }; |
| 642 | |
Craig Topper | 6261e1b | 2015-12-01 06:13:06 +0000 | [diff] [blame] | 643 | if (RegNo >= array_lengthof(CtrlRegDecoderTable)) |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 644 | return MCDisassembler::Fail; |
| 645 | |
Krzysztof Parzyszek | f9015e6 | 2017-02-10 23:46:45 +0000 | [diff] [blame] | 646 | static_assert(NoRegister == 0, "Expecting NoRegister to be 0"); |
| 647 | if (CtrlRegDecoderTable[RegNo] == NoRegister) |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 648 | return MCDisassembler::Fail; |
| 649 | |
| 650 | unsigned Register = CtrlRegDecoderTable[RegNo]; |
| 651 | Inst.addOperand(MCOperand::createReg(Register)); |
| 652 | return MCDisassembler::Success; |
| 653 | } |
| 654 | |
| 655 | static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo, |
| 656 | uint64_t /*Address*/, |
| 657 | const void *Decoder) { |
Krzysztof Parzyszek | f9015e6 | 2017-02-10 23:46:45 +0000 | [diff] [blame] | 658 | using namespace Hexagon; |
Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 659 | |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 660 | static const MCPhysReg CtrlReg64DecoderTable[] = { |
Krzysztof Parzyszek | f9015e6 | 2017-02-10 23:46:45 +0000 | [diff] [blame] | 661 | /* 0 */ C1_0, 0, C3_2, 0, |
| 662 | /* 4 */ C5_4, 0, C7_6, 0, |
| 663 | /* 8 */ C9_8, 0, C11_10, 0, |
Krzysztof Parzyszek | ab57c2b | 2017-02-22 22:28:47 +0000 | [diff] [blame] | 664 | /* 12 */ CS, 0, UPCYCLE, 0, |
Krzysztof Parzyszek | f9015e6 | 2017-02-10 23:46:45 +0000 | [diff] [blame] | 665 | /* 16 */ C17_16, 0, PKTCOUNT, 0, |
| 666 | /* 20 */ 0, 0, 0, 0, |
| 667 | /* 24 */ 0, 0, 0, 0, |
| 668 | /* 28 */ 0, 0, UTIMER, 0 |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 669 | }; |
| 670 | |
Craig Topper | 6261e1b | 2015-12-01 06:13:06 +0000 | [diff] [blame] | 671 | if (RegNo >= array_lengthof(CtrlReg64DecoderTable)) |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 672 | return MCDisassembler::Fail; |
| 673 | |
Krzysztof Parzyszek | f9015e6 | 2017-02-10 23:46:45 +0000 | [diff] [blame] | 674 | static_assert(NoRegister == 0, "Expecting NoRegister to be 0"); |
| 675 | if (CtrlReg64DecoderTable[RegNo] == NoRegister) |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 676 | return MCDisassembler::Fail; |
| 677 | |
| 678 | unsigned Register = CtrlReg64DecoderTable[RegNo]; |
| 679 | Inst.addOperand(MCOperand::createReg(Register)); |
| 680 | return MCDisassembler::Success; |
| 681 | } |
| 682 | |
| 683 | static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo, |
| 684 | uint64_t /*Address*/, |
| 685 | const void *Decoder) { |
| 686 | unsigned Register = 0; |
| 687 | switch (RegNo) { |
| 688 | case 0: |
| 689 | Register = Hexagon::M0; |
| 690 | break; |
| 691 | case 1: |
| 692 | Register = Hexagon::M1; |
| 693 | break; |
| 694 | default: |
| 695 | return MCDisassembler::Fail; |
| 696 | } |
| 697 | Inst.addOperand(MCOperand::createReg(Register)); |
| 698 | return MCDisassembler::Success; |
| 699 | } |
| 700 | |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 701 | static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp, |
| 702 | uint64_t /*Address*/, |
| 703 | const void *Decoder) { |
| 704 | HexagonDisassembler const &Disassembler = disassembler(Decoder); |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 705 | int64_t FullValue = fullValue(Disassembler, MI, tmp); |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 706 | assert(FullValue >= 0 && "Negative in unsigned decoder"); |
| 707 | HexagonMCInstrInfo::addConstant(MI, FullValue, Disassembler.getContext()); |
| 708 | return MCDisassembler::Success; |
| 709 | } |
| 710 | |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 711 | static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp, |
| 712 | uint64_t /*Address*/, const void *Decoder) { |
| 713 | HexagonDisassembler const &Disassembler = disassembler(Decoder); |
| 714 | unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI); |
| 715 | tmp = SignExtend64(tmp, Bits); |
| 716 | signedDecoder<32>(MI, tmp, Decoder); |
| 717 | return MCDisassembler::Success; |
| 718 | } |
| 719 | |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 720 | // custom decoder for various jump/call immediates |
| 721 | static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address, |
| 722 | const void *Decoder) { |
| 723 | HexagonDisassembler const &Disassembler = disassembler(Decoder); |
| 724 | unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI); |
| 725 | // r13_2 is not extendable, so if there are no extent bits, it's r13_2 |
| 726 | if (Bits == 0) |
| 727 | Bits = 15; |
Krzysztof Parzyszek | a8ab1b7 | 2017-12-11 18:57:54 +0000 | [diff] [blame] | 728 | uint64_t FullValue = fullValue(Disassembler, MI, SignExtend64(tmp, Bits)); |
| 729 | uint32_t Extended = FullValue + Address; |
Krzysztof Parzyszek | a72fad9 | 2017-02-10 15:33:13 +0000 | [diff] [blame] | 730 | if (!Disassembler.tryAddingSymbolicOperand(MI, Extended, Address, true, 0, 4)) |
Colin LeMahieu | 7cd0892 | 2015-11-09 04:07:48 +0000 | [diff] [blame] | 731 | HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext()); |
| 732 | return MCDisassembler::Success; |
| 733 | } |