1e4fc6ee7SEugene Zelenko //===- 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 
107cd08927SColin LeMahieu #define DEBUG_TYPE "hexagon-disassembler"
117cd08927SColin LeMahieu 
1268d967d9SColin LeMahieu #include "Hexagon.h"
1388ebb9e2SColin LeMahieu #include "MCTargetDesc/HexagonBaseInfo.h"
147cd08927SColin LeMahieu #include "MCTargetDesc/HexagonMCChecker.h"
157cd08927SColin LeMahieu #include "MCTargetDesc/HexagonMCInstrInfo.h"
166bda14b3SChandler Carruth #include "MCTargetDesc/HexagonMCTargetDesc.h"
1782085927SEugene Zelenko #include "llvm/ADT/ArrayRef.h"
1882085927SEugene Zelenko #include "llvm/ADT/STLExtras.h"
197cd08927SColin LeMahieu #include "llvm/MC/MCContext.h"
206bda14b3SChandler Carruth #include "llvm/MC/MCDisassembler/MCDisassembler.h"
2188ebb9e2SColin LeMahieu #include "llvm/MC/MCExpr.h"
2288ebb9e2SColin LeMahieu #include "llvm/MC/MCFixedLenDisassembler.h"
2388ebb9e2SColin LeMahieu #include "llvm/MC/MCInst.h"
247cd08927SColin LeMahieu #include "llvm/MC/MCInstrInfo.h"
2582085927SEugene Zelenko #include "llvm/MC/MCRegisterInfo.h"
2688ebb9e2SColin LeMahieu #include "llvm/MC/MCSubtargetInfo.h"
27e4fc6ee7SEugene Zelenko #include "llvm/Support/Endian.h"
2882085927SEugene Zelenko #include "llvm/Support/MathExtras.h"
297cd08927SColin LeMahieu #include "llvm/Support/TargetRegistry.h"
306bda14b3SChandler Carruth #include "llvm/Support/raw_ostream.h"
3182085927SEugene Zelenko #include <cassert>
3282085927SEugene Zelenko #include <cstddef>
3382085927SEugene Zelenko #include <cstdint>
3482085927SEugene Zelenko #include <memory>
3588ebb9e2SColin LeMahieu 
3688ebb9e2SColin LeMahieu using namespace llvm;
3768d967d9SColin LeMahieu using namespace Hexagon;
3888ebb9e2SColin LeMahieu 
39e4fc6ee7SEugene Zelenko using DecodeStatus = MCDisassembler::DecodeStatus;
4088ebb9e2SColin LeMahieu 
4188ebb9e2SColin LeMahieu namespace {
4282085927SEugene Zelenko 
43*5f8f34e4SAdrian Prantl /// Hexagon disassembler for all Hexagon platforms.
4488ebb9e2SColin LeMahieu class HexagonDisassembler : public MCDisassembler {
4588ebb9e2SColin LeMahieu public:
467cd08927SColin LeMahieu   std::unique_ptr<MCInstrInfo const> const MCII;
4768d967d9SColin LeMahieu   std::unique_ptr<MCInst *> CurrentBundle;
48a8ab1b75SKrzysztof Parzyszek   mutable MCInst const *CurrentExtender;
4982085927SEugene Zelenko 
507cd08927SColin LeMahieu   HexagonDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
517cd08927SColin LeMahieu                       MCInstrInfo const *MCII)
52a8ab1b75SKrzysztof Parzyszek       : MCDisassembler(STI, Ctx), MCII(MCII), CurrentBundle(new MCInst *),
53a8ab1b75SKrzysztof Parzyszek         CurrentExtender(nullptr) {}
5488ebb9e2SColin LeMahieu 
5568d967d9SColin LeMahieu   DecodeStatus getSingleInstruction(MCInst &Instr, MCInst &MCB,
5668d967d9SColin LeMahieu                                     ArrayRef<uint8_t> Bytes, uint64_t Address,
5768d967d9SColin LeMahieu                                     raw_ostream &VStream, raw_ostream &CStream,
5868d967d9SColin LeMahieu                                     bool &Complete) const;
594aa6bea7SRafael Espindola   DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
607fc5b874SRafael Espindola                               ArrayRef<uint8_t> Bytes, uint64_t Address,
614aa6bea7SRafael Espindola                               raw_ostream &VStream,
624aa6bea7SRafael Espindola                               raw_ostream &CStream) const override;
63a8ab1b75SKrzysztof Parzyszek   void remapInstruction(MCInst &Instr) const;
6488ebb9e2SColin LeMahieu };
6582085927SEugene Zelenko 
66a8ab1b75SKrzysztof Parzyszek static uint64_t fullValue(HexagonDisassembler const &Disassembler, MCInst &MI,
67a72fad98SKrzysztof Parzyszek                           int64_t Value) {
68a8ab1b75SKrzysztof Parzyszek   MCInstrInfo MCII = *Disassembler.MCII;
69a8ab1b75SKrzysztof Parzyszek   if (!Disassembler.CurrentExtender ||
70a8ab1b75SKrzysztof Parzyszek       MI.size() != HexagonMCInstrInfo::getExtendableOp(MCII, MI))
71a72fad98SKrzysztof Parzyszek     return Value;
72a72fad98SKrzysztof Parzyszek   unsigned Alignment = HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
73a72fad98SKrzysztof Parzyszek   uint32_t Lower6 = static_cast<uint32_t>(Value >> Alignment) & 0x3f;
74a72fad98SKrzysztof Parzyszek   int64_t Bits;
75a8ab1b75SKrzysztof Parzyszek   bool Success =
76a8ab1b75SKrzysztof Parzyszek       Disassembler.CurrentExtender->getOperand(0).getExpr()->evaluateAsAbsolute(
77a8ab1b75SKrzysztof Parzyszek           Bits);
78a8ab1b75SKrzysztof Parzyszek   assert(Success);
79a8ab1b75SKrzysztof Parzyszek   (void)Success;
80a8ab1b75SKrzysztof Parzyszek   uint64_t Upper26 = static_cast<uint64_t>(Bits);
81a8ab1b75SKrzysztof Parzyszek   uint64_t Operand = Upper26 | Lower6;
82a72fad98SKrzysztof Parzyszek   return Operand;
83a72fad98SKrzysztof Parzyszek }
84e4fc6ee7SEugene Zelenko static HexagonDisassembler const &disassembler(void const *Decoder) {
85a72fad98SKrzysztof Parzyszek   return *static_cast<HexagonDisassembler const *>(Decoder);
86a72fad98SKrzysztof Parzyszek }
87a72fad98SKrzysztof Parzyszek template <size_t T>
88e4fc6ee7SEugene Zelenko static void signedDecoder(MCInst &MI, unsigned tmp, const void *Decoder) {
89a72fad98SKrzysztof Parzyszek   HexagonDisassembler const &Disassembler = disassembler(Decoder);
90a8ab1b75SKrzysztof Parzyszek   int64_t FullValue = fullValue(Disassembler, MI, SignExtend64<T>(tmp));
91a72fad98SKrzysztof Parzyszek   int64_t Extended = SignExtend64<32>(FullValue);
92a72fad98SKrzysztof Parzyszek   HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
93a72fad98SKrzysztof Parzyszek }
94a8ab1b75SKrzysztof Parzyszek }
9588ebb9e2SColin LeMahieu 
967cd08927SColin LeMahieu // Forward declare these because the auto-generated code will reference them.
977cd08927SColin LeMahieu // Definitions are further down.
987cd08927SColin LeMahieu 
997cd08927SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
100be8c453dSColin LeMahieu                                                uint64_t Address,
101be8c453dSColin LeMahieu                                                const void *Decoder);
102a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
103a72fad98SKrzysztof Parzyszek                                                       unsigned RegNo,
104a72fad98SKrzysztof Parzyszek                                                       uint64_t Address,
105a72fad98SKrzysztof Parzyszek                                                       const void *Decoder);
1067c958713SColin LeMahieu static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
1077c958713SColin LeMahieu                                                    uint64_t Address,
1087c958713SColin LeMahieu                                                    const void *Decoder);
10955772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,
1107c958713SColin LeMahieu                                              uint64_t Address,
1117c958713SColin LeMahieu                                              const void *Decoder);
1127cd08927SColin LeMahieu static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1137cd08927SColin LeMahieu                                                   uint64_t Address,
1147cd08927SColin LeMahieu                                                   const void *Decoder);
115a72fad98SKrzysztof Parzyszek static DecodeStatus
116a72fad98SKrzysztof Parzyszek DecodeGeneralDoubleLow8RegsRegisterClass(MCInst &Inst, unsigned RegNo,
117a72fad98SKrzysztof Parzyszek                                          uint64_t Address, const void *Decoder);
11855772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,
1197c958713SColin LeMahieu                                              uint64_t Address,
1207c958713SColin LeMahieu                                              const void *Decoder);
1217cd08927SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1227cd08927SColin LeMahieu                                                 uint64_t Address,
1237cd08927SColin LeMahieu                                                 const void *Decoder);
12455772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,
1257c958713SColin LeMahieu                                              uint64_t Address,
1267c958713SColin LeMahieu                                              const void *Decoder);
127f3db884eSColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
128be8c453dSColin LeMahieu                                                uint64_t Address,
129be8c453dSColin LeMahieu                                                const void *Decoder);
13022a21d4cSKrzysztof Parzyszek static DecodeStatus DecodeGuestRegsRegisterClass(MCInst &Inst, unsigned RegNo,
13122a21d4cSKrzysztof Parzyszek                                                  uint64_t Address,
13222a21d4cSKrzysztof Parzyszek                                                  const void *Decoder);
1337cd08927SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1347cd08927SColin LeMahieu                                                uint64_t Address,
1357cd08927SColin LeMahieu                                                const void *Decoder);
136404d5b24SColin LeMahieu static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
137be8c453dSColin LeMahieu                                                  uint64_t Address,
1387cd08927SColin LeMahieu                                                  const void *Decoder);
13922a21d4cSKrzysztof Parzyszek static DecodeStatus DecodeGuestRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
14022a21d4cSKrzysztof Parzyszek                                                    uint64_t Address,
14122a21d4cSKrzysztof Parzyszek                                                    const void *Decoder);
1427cd08927SColin LeMahieu 
1437cd08927SColin LeMahieu static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
1447cd08927SColin LeMahieu                                        uint64_t Address, const void *Decoder);
145a72fad98SKrzysztof Parzyszek static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
146a72fad98SKrzysztof Parzyszek                                     uint64_t /*Address*/, const void *Decoder);
1477cd08927SColin LeMahieu static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1487cd08927SColin LeMahieu                                     const void *Decoder);
149efa74e02SColin LeMahieu 
150f157ad44SDavid Blaikie static DecodeStatus s4_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
151f157ad44SDavid Blaikie                                    const void *Decoder) {
152f157ad44SDavid Blaikie   signedDecoder<4>(MI, tmp, Decoder);
153f157ad44SDavid Blaikie   return MCDisassembler::Success;
154f157ad44SDavid Blaikie }
155f157ad44SDavid Blaikie static DecodeStatus s29_3ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
156f157ad44SDavid Blaikie                                     const void *Decoder) {
157f157ad44SDavid Blaikie   signedDecoder<14>(MI, tmp, Decoder);
158f157ad44SDavid Blaikie   return MCDisassembler::Success;
159f157ad44SDavid Blaikie }
160f157ad44SDavid Blaikie static DecodeStatus s8_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
161f157ad44SDavid Blaikie                                    const void *Decoder) {
162f157ad44SDavid Blaikie   signedDecoder<8>(MI, tmp, Decoder);
163f157ad44SDavid Blaikie   return MCDisassembler::Success;
164f157ad44SDavid Blaikie }
165f157ad44SDavid Blaikie static DecodeStatus s4_3ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
166f157ad44SDavid Blaikie                                    const void *Decoder) {
167f157ad44SDavid Blaikie   signedDecoder<7>(MI, tmp, Decoder);
168f157ad44SDavid Blaikie   return MCDisassembler::Success;
169f157ad44SDavid Blaikie }
170f157ad44SDavid Blaikie static DecodeStatus s31_1ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
171f157ad44SDavid Blaikie                                     const void *Decoder) {
172f157ad44SDavid Blaikie   signedDecoder<12>(MI, tmp, Decoder);
173f157ad44SDavid Blaikie   return MCDisassembler::Success;
174f157ad44SDavid Blaikie }
175f157ad44SDavid Blaikie static DecodeStatus s3_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
176f157ad44SDavid Blaikie                                    const void *Decoder) {
177f157ad44SDavid Blaikie   signedDecoder<3>(MI, tmp, Decoder);
178f157ad44SDavid Blaikie   return MCDisassembler::Success;
179f157ad44SDavid Blaikie }
180f157ad44SDavid Blaikie static DecodeStatus s30_2ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
181f157ad44SDavid Blaikie                                     const void *Decoder) {
182f157ad44SDavid Blaikie   signedDecoder<13>(MI, tmp, Decoder);
183f157ad44SDavid Blaikie   return MCDisassembler::Success;
184f157ad44SDavid Blaikie }
185f157ad44SDavid Blaikie static DecodeStatus s6_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
186f157ad44SDavid Blaikie                                    const void *Decoder) {
187f157ad44SDavid Blaikie   signedDecoder<6>(MI, tmp, Decoder);
188f157ad44SDavid Blaikie   return MCDisassembler::Success;
189f157ad44SDavid Blaikie }
190f157ad44SDavid Blaikie static DecodeStatus s6_3ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
191f157ad44SDavid Blaikie                                    const void *Decoder) {
192f157ad44SDavid Blaikie   signedDecoder<9>(MI, tmp, Decoder);
193f157ad44SDavid Blaikie   return MCDisassembler::Success;
194f157ad44SDavid Blaikie }
195f157ad44SDavid Blaikie static DecodeStatus s4_1ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
196f157ad44SDavid Blaikie                                    const void *Decoder) {
197f157ad44SDavid Blaikie   signedDecoder<5>(MI, tmp, Decoder);
198f157ad44SDavid Blaikie   return MCDisassembler::Success;
199f157ad44SDavid Blaikie }
200f157ad44SDavid Blaikie static DecodeStatus s4_2ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
201f157ad44SDavid Blaikie                                    const void *Decoder) {
202f157ad44SDavid Blaikie   signedDecoder<6>(MI, tmp, Decoder);
203f157ad44SDavid Blaikie   return MCDisassembler::Success;
204f157ad44SDavid Blaikie }
20588ebb9e2SColin LeMahieu #include "HexagonGenDisassemblerTables.inc"
20688ebb9e2SColin LeMahieu 
2077cd08927SColin LeMahieu static MCDisassembler *createHexagonDisassembler(const Target &T,
2087cd08927SColin LeMahieu                                                  const MCSubtargetInfo &STI,
20988ebb9e2SColin LeMahieu                                                  MCContext &Ctx) {
2107cd08927SColin LeMahieu   return new HexagonDisassembler(STI, Ctx, T.createMCInstrInfo());
21188ebb9e2SColin LeMahieu }
21288ebb9e2SColin LeMahieu 
21388ebb9e2SColin LeMahieu extern "C" void LLVMInitializeHexagonDisassembler() {
214f42454b9SMehdi Amini   TargetRegistry::RegisterMCDisassembler(getTheHexagonTarget(),
21588ebb9e2SColin LeMahieu                                          createHexagonDisassembler);
21688ebb9e2SColin LeMahieu }
21788ebb9e2SColin LeMahieu 
21888ebb9e2SColin LeMahieu DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
2197fc5b874SRafael Espindola                                                  ArrayRef<uint8_t> Bytes,
22088ebb9e2SColin LeMahieu                                                  uint64_t Address,
22188ebb9e2SColin LeMahieu                                                  raw_ostream &os,
22288ebb9e2SColin LeMahieu                                                  raw_ostream &cs) const {
22368d967d9SColin LeMahieu   DecodeStatus Result = DecodeStatus::Success;
22468d967d9SColin LeMahieu   bool Complete = false;
22568d967d9SColin LeMahieu   Size = 0;
2264aa6bea7SRafael Espindola 
22768d967d9SColin LeMahieu   *CurrentBundle = &MI;
228a8ab1b75SKrzysztof Parzyszek   MI.setOpcode(Hexagon::BUNDLE);
229a8ab1b75SKrzysztof Parzyszek   MI.addOperand(MCOperand::createImm(0));
23082085927SEugene Zelenko   while (Result == Success && !Complete) {
23168d967d9SColin LeMahieu     if (Bytes.size() < HEXAGON_INSTR_SIZE)
23268d967d9SColin LeMahieu       return MCDisassembler::Fail;
23368d967d9SColin LeMahieu     MCInst *Inst = new (getContext()) MCInst;
23468d967d9SColin LeMahieu     Result = getSingleInstruction(*Inst, MI, Bytes, Address, os, cs, Complete);
23568d967d9SColin LeMahieu     MI.addOperand(MCOperand::createInst(Inst));
23668d967d9SColin LeMahieu     Size += HEXAGON_INSTR_SIZE;
23768d967d9SColin LeMahieu     Bytes = Bytes.slice(HEXAGON_INSTR_SIZE);
23868d967d9SColin LeMahieu   }
2397cd08927SColin LeMahieu   if (Result == MCDisassembler::Fail)
24068d967d9SColin LeMahieu     return Result;
241a72fad98SKrzysztof Parzyszek   if (Size > HEXAGON_MAX_PACKET_SIZE)
242a72fad98SKrzysztof Parzyszek     return MCDisassembler::Fail;
243e12d1e70SKrzysztof Parzyszek   HexagonMCChecker Checker(getContext(), *MCII, STI, MI,
244e12d1e70SKrzysztof Parzyszek                            *getContext().getRegisterInfo(), false);
2457cd08927SColin LeMahieu   if (!Checker.check())
2467cd08927SColin LeMahieu     return MCDisassembler::Fail;
247a8ab1b75SKrzysztof Parzyszek   remapInstruction(MI);
2487cd08927SColin LeMahieu   return MCDisassembler::Success;
2497cd08927SColin LeMahieu }
2507cd08927SColin LeMahieu 
251a8ab1b75SKrzysztof Parzyszek void HexagonDisassembler::remapInstruction(MCInst &Instr) const {
252a8ab1b75SKrzysztof Parzyszek   for (auto I: HexagonMCInstrInfo::bundleInstructions(Instr)) {
253a8ab1b75SKrzysztof Parzyszek     auto &MI = const_cast<MCInst &>(*I.getInst());
254a8ab1b75SKrzysztof Parzyszek     switch (MI.getOpcode()) {
255a8ab1b75SKrzysztof Parzyszek     case Hexagon::S2_allocframe:
256a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::R29) {
257a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(Hexagon::S6_allocframe_to_raw);
258a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 1);
259a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
260a8ab1b75SKrzysztof Parzyszek       }
261a8ab1b75SKrzysztof Parzyszek       break;
262a8ab1b75SKrzysztof Parzyszek     case Hexagon::L2_deallocframe:
263a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
264a8ab1b75SKrzysztof Parzyszek           MI.getOperand(1).getReg() == Hexagon::R30) {
265a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L6_deallocframe_map_to_raw);
266a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 1);
267a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
268a8ab1b75SKrzysztof Parzyszek       }
269a8ab1b75SKrzysztof Parzyszek       break;
270a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return:
271a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
272a8ab1b75SKrzysztof Parzyszek           MI.getOperand(1).getReg() == Hexagon::R30) {
273a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L6_return_map_to_raw);
274a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 1);
275a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
276a8ab1b75SKrzysztof Parzyszek       }
277a8ab1b75SKrzysztof Parzyszek       break;
278a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_t:
279a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
280a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
281a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_t);
282a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
283a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
284a8ab1b75SKrzysztof Parzyszek       }
285a8ab1b75SKrzysztof Parzyszek       break;
286a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_f:
287a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
288a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
289a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_f);
290a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
291a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
292a8ab1b75SKrzysztof Parzyszek       }
293a8ab1b75SKrzysztof Parzyszek       break;
294a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_tnew_pt:
295a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
296a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
297a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_tnew_pt);
298a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
299a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
300a8ab1b75SKrzysztof Parzyszek       }
301a8ab1b75SKrzysztof Parzyszek       break;
302a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_fnew_pt:
303a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
304a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
305a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_fnew_pt);
306a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
307a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
308a8ab1b75SKrzysztof Parzyszek       }
309a8ab1b75SKrzysztof Parzyszek       break;
310a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_tnew_pnt:
311a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
312a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
313a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_tnew_pnt);
314a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
315a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
316a8ab1b75SKrzysztof Parzyszek       }
317a8ab1b75SKrzysztof Parzyszek       break;
318a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_fnew_pnt:
319a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
320a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
321a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_fnew_pnt);
322a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
323a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
324a8ab1b75SKrzysztof Parzyszek       }
325a8ab1b75SKrzysztof Parzyszek       break;
326a8ab1b75SKrzysztof Parzyszek     }
327a8ab1b75SKrzysztof Parzyszek   }
328a8ab1b75SKrzysztof Parzyszek }
329a8ab1b75SKrzysztof Parzyszek 
330e4fc6ee7SEugene Zelenko static void adjustDuplex(MCInst &MI, MCContext &Context) {
331a72fad98SKrzysztof Parzyszek   switch (MI.getOpcode()) {
332a72fad98SKrzysztof Parzyszek   case Hexagon::SA1_setin1:
333a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 1,
334a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
335a72fad98SKrzysztof Parzyszek     break;
336a72fad98SKrzysztof Parzyszek   case Hexagon::SA1_dec:
337a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 2,
338a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
339a72fad98SKrzysztof Parzyszek     break;
340a72fad98SKrzysztof Parzyszek   default:
341a72fad98SKrzysztof Parzyszek     break;
3427cd08927SColin LeMahieu   }
343a72fad98SKrzysztof Parzyszek }
34468d967d9SColin LeMahieu 
34568d967d9SColin LeMahieu DecodeStatus HexagonDisassembler::getSingleInstruction(
34668d967d9SColin LeMahieu     MCInst &MI, MCInst &MCB, ArrayRef<uint8_t> Bytes, uint64_t Address,
34768d967d9SColin LeMahieu     raw_ostream &os, raw_ostream &cs, bool &Complete) const {
34868d967d9SColin LeMahieu   assert(Bytes.size() >= HEXAGON_INSTR_SIZE);
34968d967d9SColin LeMahieu 
350a72fad98SKrzysztof Parzyszek   uint32_t Instruction = support::endian::read32le(Bytes.data());
35188ebb9e2SColin LeMahieu 
35268d967d9SColin LeMahieu   auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB);
35368d967d9SColin LeMahieu   if ((Instruction & HexagonII::INST_PARSE_MASK) ==
35468d967d9SColin LeMahieu       HexagonII::INST_PARSE_LOOP_END) {
35568d967d9SColin LeMahieu     if (BundleSize == 0)
35668d967d9SColin LeMahieu       HexagonMCInstrInfo::setInnerLoop(MCB);
35768d967d9SColin LeMahieu     else if (BundleSize == 1)
35868d967d9SColin LeMahieu       HexagonMCInstrInfo::setOuterLoop(MCB);
35968d967d9SColin LeMahieu     else
36068d967d9SColin LeMahieu       return DecodeStatus::Fail;
36168d967d9SColin LeMahieu   }
36268d967d9SColin LeMahieu 
363a8ab1b75SKrzysztof Parzyszek   CurrentExtender = HexagonMCInstrInfo::extenderForIndex(
364a72fad98SKrzysztof Parzyszek       MCB, HexagonMCInstrInfo::bundleSize(MCB));
365a72fad98SKrzysztof Parzyszek 
366a72fad98SKrzysztof Parzyszek   DecodeStatus Result = DecodeStatus::Fail;
36768d967d9SColin LeMahieu   if ((Instruction & HexagonII::INST_PARSE_MASK) ==
368be8c453dSColin LeMahieu       HexagonII::INST_PARSE_DUPLEX) {
369a72fad98SKrzysztof Parzyszek     unsigned duplexIClass;
370a72fad98SKrzysztof Parzyszek     uint8_t const *DecodeLow, *DecodeHigh;
371be8c453dSColin LeMahieu     duplexIClass = ((Instruction >> 28) & 0xe) | ((Instruction >> 13) & 0x1);
372be8c453dSColin LeMahieu     switch (duplexIClass) {
373be8c453dSColin LeMahieu     default:
374be8c453dSColin LeMahieu       return MCDisassembler::Fail;
375be8c453dSColin LeMahieu     case 0:
376a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L132;
377a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
378be8c453dSColin LeMahieu       break;
379be8c453dSColin LeMahieu     case 1:
380a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
381a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
382be8c453dSColin LeMahieu       break;
383be8c453dSColin LeMahieu     case 2:
384a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
385a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
386be8c453dSColin LeMahieu       break;
387be8c453dSColin LeMahieu     case 3:
388a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_A32;
389a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
390be8c453dSColin LeMahieu       break;
391be8c453dSColin LeMahieu     case 4:
392a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L132;
393a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
394be8c453dSColin LeMahieu       break;
395be8c453dSColin LeMahieu     case 5:
396a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
397a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
398be8c453dSColin LeMahieu       break;
399be8c453dSColin LeMahieu     case 6:
400a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
401a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
402be8c453dSColin LeMahieu       break;
403be8c453dSColin LeMahieu     case 7:
404a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
405a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
406be8c453dSColin LeMahieu       break;
407be8c453dSColin LeMahieu     case 8:
408a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
409a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
410be8c453dSColin LeMahieu       break;
411be8c453dSColin LeMahieu     case 9:
412a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
413a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
414be8c453dSColin LeMahieu       break;
415be8c453dSColin LeMahieu     case 10:
416a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
417a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S132;
418be8c453dSColin LeMahieu       break;
419be8c453dSColin LeMahieu     case 11:
420a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
421a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S132;
422be8c453dSColin LeMahieu       break;
423be8c453dSColin LeMahieu     case 12:
424a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
425a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
426be8c453dSColin LeMahieu       break;
427be8c453dSColin LeMahieu     case 13:
428a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
429a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
430be8c453dSColin LeMahieu       break;
431be8c453dSColin LeMahieu     case 14:
432a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
433a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S232;
434be8c453dSColin LeMahieu       break;
435be8c453dSColin LeMahieu     }
436a72fad98SKrzysztof Parzyszek     MI.setOpcode(Hexagon::DuplexIClass0 + duplexIClass);
437be8c453dSColin LeMahieu     MCInst *MILow = new (getContext()) MCInst;
438be8c453dSColin LeMahieu     MCInst *MIHigh = new (getContext()) MCInst;
439a8ab1b75SKrzysztof Parzyszek     auto TmpExtender = CurrentExtender;
440a8ab1b75SKrzysztof Parzyszek     CurrentExtender =
441a8ab1b75SKrzysztof Parzyszek         nullptr; // constant extenders in duplex must always be in slot 1
442a72fad98SKrzysztof Parzyszek     Result = decodeInstruction(DecodeLow, *MILow, Instruction & 0x1fff, Address,
443a72fad98SKrzysztof Parzyszek                                this, STI);
444a8ab1b75SKrzysztof Parzyszek     CurrentExtender = TmpExtender;
445a72fad98SKrzysztof Parzyszek     if (Result != DecodeStatus::Success)
446a72fad98SKrzysztof Parzyszek       return DecodeStatus::Fail;
447a72fad98SKrzysztof Parzyszek     adjustDuplex(*MILow, getContext());
448a72fad98SKrzysztof Parzyszek     Result = decodeInstruction(
449a72fad98SKrzysztof Parzyszek         DecodeHigh, *MIHigh, (Instruction >> 16) & 0x1fff, Address, this, STI);
450a72fad98SKrzysztof Parzyszek     if (Result != DecodeStatus::Success)
451a72fad98SKrzysztof Parzyszek       return DecodeStatus::Fail;
452a72fad98SKrzysztof Parzyszek     adjustDuplex(*MIHigh, getContext());
453be8c453dSColin LeMahieu     MCOperand OPLow = MCOperand::createInst(MILow);
454be8c453dSColin LeMahieu     MCOperand OPHigh = MCOperand::createInst(MIHigh);
455be8c453dSColin LeMahieu     MI.addOperand(OPLow);
456be8c453dSColin LeMahieu     MI.addOperand(OPHigh);
457be8c453dSColin LeMahieu     Complete = true;
458be8c453dSColin LeMahieu   } else {
459be8c453dSColin LeMahieu     if ((Instruction & HexagonII::INST_PARSE_MASK) ==
46068d967d9SColin LeMahieu         HexagonII::INST_PARSE_PACKET_END)
46168d967d9SColin LeMahieu       Complete = true;
4627cd08927SColin LeMahieu 
463a8ab1b75SKrzysztof Parzyszek     if (CurrentExtender != nullptr)
464a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTableMustExtend32, MI, Instruction,
465a72fad98SKrzysztof Parzyszek                                  Address, this, STI);
466a72fad98SKrzysztof Parzyszek 
467a72fad98SKrzysztof Parzyszek     if (Result != MCDisassembler::Success)
468a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTable32, MI, Instruction, Address, this,
469a72fad98SKrzysztof Parzyszek                                  STI);
470a72fad98SKrzysztof Parzyszek 
471a72fad98SKrzysztof Parzyszek     if (Result != MCDisassembler::Success &&
472a72fad98SKrzysztof Parzyszek         STI.getFeatureBits()[Hexagon::ExtensionHVX])
473a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTableEXT_mmvec32, MI, Instruction,
474a72fad98SKrzysztof Parzyszek                                  Address, this, STI);
475a72fad98SKrzysztof Parzyszek 
476be8c453dSColin LeMahieu   }
47768d967d9SColin LeMahieu 
47881707549SColin LeMahieu   switch (MI.getOpcode()) {
47981707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_f_jumpnv_nt:
48081707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_f_jumpnv_t:
48181707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp0_jump_nt:
48281707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp0_jump_t:
48381707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp1_jump_nt:
48481707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp1_jump_t:
48581707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_t_jumpnv_nt:
48681707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_t_jumpnv_t:
48781707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp0_jump_nt:
48881707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp0_jump_t:
48981707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp1_jump_nt:
49081707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp1_jump_t:
49181707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_f_jumpnv_nt:
49281707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_f_jumpnv_t:
49381707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp0_jump_nt:
49481707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp0_jump_t:
49581707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp1_jump_nt:
49681707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp1_jump_t:
49781707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_t_jumpnv_nt:
49881707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_t_jumpnv_t:
49981707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp0_jump_nt:
50081707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp0_jump_t:
50181707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp1_jump_nt:
50281707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp1_jump_t:
503a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 1,
504a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, getContext())));
50581707549SColin LeMahieu     break;
50681707549SColin LeMahieu   default:
50781707549SColin LeMahieu     break;
50881707549SColin LeMahieu   }
50981707549SColin LeMahieu 
5107cd08927SColin LeMahieu   if (HexagonMCInstrInfo::isNewValue(*MCII, MI)) {
5117cd08927SColin LeMahieu     unsigned OpIndex = HexagonMCInstrInfo::getNewValueOp(*MCII, MI);
5127cd08927SColin LeMahieu     MCOperand &MCO = MI.getOperand(OpIndex);
5137cd08927SColin LeMahieu     assert(MCO.isReg() && "New value consumers must be registers");
5147cd08927SColin LeMahieu     unsigned Register =
5157cd08927SColin LeMahieu         getContext().getRegisterInfo()->getEncodingValue(MCO.getReg());
5167cd08927SColin LeMahieu     if ((Register & 0x6) == 0)
5177cd08927SColin LeMahieu       // HexagonPRM 10.11 Bit 1-2 == 0 is reserved
5187cd08927SColin LeMahieu       return MCDisassembler::Fail;
5197cd08927SColin LeMahieu     unsigned Lookback = (Register & 0x6) >> 1;
5207cd08927SColin LeMahieu     unsigned Offset = 1;
5217cd08927SColin LeMahieu     bool Vector = HexagonMCInstrInfo::isVector(*MCII, MI);
522a8ab1b75SKrzysztof Parzyszek     bool PrevVector = false;
5237cd08927SColin LeMahieu     auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
5247cd08927SColin LeMahieu     auto i = Instructions.end() - 1;
5257cd08927SColin LeMahieu     for (auto n = Instructions.begin() - 1;; --i, ++Offset) {
5267cd08927SColin LeMahieu       if (i == n)
5277cd08927SColin LeMahieu         // Couldn't find producer
5287cd08927SColin LeMahieu         return MCDisassembler::Fail;
529a8ab1b75SKrzysztof Parzyszek       bool CurrentVector = HexagonMCInstrInfo::isVector(*MCII, *i->getInst());
530a8ab1b75SKrzysztof Parzyszek       if (Vector && !CurrentVector)
5317cd08927SColin LeMahieu         // Skip scalars when calculating distances for vectors
5327cd08927SColin LeMahieu         ++Lookback;
533a8ab1b75SKrzysztof Parzyszek       if (HexagonMCInstrInfo::isImmext(*i->getInst()) && (Vector == PrevVector))
5347cd08927SColin LeMahieu         ++Lookback;
535a8ab1b75SKrzysztof Parzyszek       PrevVector = CurrentVector;
5367cd08927SColin LeMahieu       if (Offset == Lookback)
5377cd08927SColin LeMahieu         break;
5387cd08927SColin LeMahieu     }
5397cd08927SColin LeMahieu     auto const &Inst = *i->getInst();
5407cd08927SColin LeMahieu     bool SubregBit = (Register & 0x1) != 0;
541a8ab1b75SKrzysztof Parzyszek     if (HexagonMCInstrInfo::hasNewValue2(*MCII, Inst)) {
5427cd08927SColin LeMahieu       // If subreg bit is set we're selecting the second produced newvalue
543a8ab1b75SKrzysztof Parzyszek       unsigned Producer = SubregBit ?
544a8ab1b75SKrzysztof Parzyszek           HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg() :
5457cd08927SColin LeMahieu           HexagonMCInstrInfo::getNewValueOperand2(*MCII, Inst).getReg();
5467cd08927SColin LeMahieu       assert(Producer != Hexagon::NoRegister);
5477cd08927SColin LeMahieu       MCO.setReg(Producer);
5487cd08927SColin LeMahieu     } else if (HexagonMCInstrInfo::hasNewValue(*MCII, Inst)) {
5497cd08927SColin LeMahieu       unsigned Producer =
5507cd08927SColin LeMahieu           HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg();
5517cd08927SColin LeMahieu       if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
5527cd08927SColin LeMahieu         Producer = ((Producer - Hexagon::W0) << 1) + SubregBit + Hexagon::V0;
5537cd08927SColin LeMahieu       else if (SubregBit)
5546fdfa3dcSColin LeMahieu         // Hexagon PRM 10.11 New-value operands
5556fdfa3dcSColin LeMahieu         // Nt[0] is reserved and should always be encoded as zero.
5567cd08927SColin LeMahieu         return MCDisassembler::Fail;
5577cd08927SColin LeMahieu       assert(Producer != Hexagon::NoRegister);
5587cd08927SColin LeMahieu       MCO.setReg(Producer);
5597cd08927SColin LeMahieu     } else
5607cd08927SColin LeMahieu       return MCDisassembler::Fail;
5617cd08927SColin LeMahieu   }
5627cd08927SColin LeMahieu 
563a8ab1b75SKrzysztof Parzyszek   if (CurrentExtender != nullptr) {
564a72fad98SKrzysztof Parzyszek     MCInst const &Inst = HexagonMCInstrInfo::isDuplex(*MCII, MI)
565a72fad98SKrzysztof Parzyszek                              ? *MI.getOperand(1).getInst()
566a72fad98SKrzysztof Parzyszek                              : MI;
5677cd08927SColin LeMahieu     if (!HexagonMCInstrInfo::isExtendable(*MCII, Inst) &&
5687cd08927SColin LeMahieu         !HexagonMCInstrInfo::isExtended(*MCII, Inst))
5697cd08927SColin LeMahieu       return MCDisassembler::Fail;
5707cd08927SColin LeMahieu   }
5715d6f03bdSColin LeMahieu   return Result;
57288ebb9e2SColin LeMahieu }
5737cd08927SColin LeMahieu 
5747cd08927SColin LeMahieu static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,
575e5e035a3SCraig Topper                                         ArrayRef<MCPhysReg> Table) {
5763da000c0SCraig Topper   if (RegNo < Table.size()) {
5777cd08927SColin LeMahieu     Inst.addOperand(MCOperand::createReg(Table[RegNo]));
5787cd08927SColin LeMahieu     return MCDisassembler::Success;
5793da000c0SCraig Topper   }
5803da000c0SCraig Topper 
5817cd08927SColin LeMahieu   return MCDisassembler::Fail;
5827cd08927SColin LeMahieu }
5837cd08927SColin LeMahieu 
5847c958713SColin LeMahieu static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
5857c958713SColin LeMahieu                                                    uint64_t Address,
5867c958713SColin LeMahieu                                                    const void *Decoder) {
5877c958713SColin LeMahieu   return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
5887c958713SColin LeMahieu }
5897c958713SColin LeMahieu 
5907cd08927SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5917cd08927SColin LeMahieu                                                uint64_t Address,
5927cd08927SColin LeMahieu                                                const void *Decoder) {
593e5e035a3SCraig Topper   static const MCPhysReg IntRegDecoderTable[] = {
5947cd08927SColin LeMahieu       Hexagon::R0,  Hexagon::R1,  Hexagon::R2,  Hexagon::R3,  Hexagon::R4,
5957cd08927SColin LeMahieu       Hexagon::R5,  Hexagon::R6,  Hexagon::R7,  Hexagon::R8,  Hexagon::R9,
5967cd08927SColin LeMahieu       Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
5977cd08927SColin LeMahieu       Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
5987cd08927SColin LeMahieu       Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
5997cd08927SColin LeMahieu       Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
6007cd08927SColin LeMahieu       Hexagon::R30, Hexagon::R31};
6017cd08927SColin LeMahieu 
602e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, IntRegDecoderTable);
6037cd08927SColin LeMahieu }
6047c958713SColin LeMahieu 
605a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
606a72fad98SKrzysztof Parzyszek                                                       unsigned RegNo,
607a72fad98SKrzysztof Parzyszek                                                       uint64_t Address,
608a72fad98SKrzysztof Parzyszek                                                       const void *Decoder) {
609a72fad98SKrzysztof Parzyszek   static const MCPhysReg GeneralSubRegDecoderTable[] = {
610a72fad98SKrzysztof Parzyszek       Hexagon::R0,  Hexagon::R1,  Hexagon::R2,  Hexagon::R3,
611a72fad98SKrzysztof Parzyszek       Hexagon::R4,  Hexagon::R5,  Hexagon::R6,  Hexagon::R7,
612a72fad98SKrzysztof Parzyszek       Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
613a72fad98SKrzysztof Parzyszek       Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23,
614a72fad98SKrzysztof Parzyszek   };
615a72fad98SKrzysztof Parzyszek 
616a72fad98SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, GeneralSubRegDecoderTable);
617a72fad98SKrzysztof Parzyszek }
618a72fad98SKrzysztof Parzyszek 
61955772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,
6207c958713SColin LeMahieu                                              uint64_t /*Address*/,
6217c958713SColin LeMahieu                                              const void *Decoder) {
62255772976SKrzysztof Parzyszek   static const MCPhysReg HvxVRDecoderTable[] = {
6237c958713SColin LeMahieu       Hexagon::V0,  Hexagon::V1,  Hexagon::V2,  Hexagon::V3,  Hexagon::V4,
6247c958713SColin LeMahieu       Hexagon::V5,  Hexagon::V6,  Hexagon::V7,  Hexagon::V8,  Hexagon::V9,
6257c958713SColin LeMahieu       Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
6267c958713SColin LeMahieu       Hexagon::V15, Hexagon::V16, Hexagon::V17, Hexagon::V18, Hexagon::V19,
6277c958713SColin LeMahieu       Hexagon::V20, Hexagon::V21, Hexagon::V22, Hexagon::V23, Hexagon::V24,
6287c958713SColin LeMahieu       Hexagon::V25, Hexagon::V26, Hexagon::V27, Hexagon::V28, Hexagon::V29,
6297c958713SColin LeMahieu       Hexagon::V30, Hexagon::V31};
6307c958713SColin LeMahieu 
63155772976SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, HvxVRDecoderTable);
6327c958713SColin LeMahieu }
6337c958713SColin LeMahieu 
6347cd08927SColin LeMahieu static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
6357cd08927SColin LeMahieu                                                   uint64_t /*Address*/,
6367cd08927SColin LeMahieu                                                   const void *Decoder) {
637e5e035a3SCraig Topper   static const MCPhysReg DoubleRegDecoderTable[] = {
6387cd08927SColin LeMahieu       Hexagon::D0,  Hexagon::D1,  Hexagon::D2,  Hexagon::D3,
6397cd08927SColin LeMahieu       Hexagon::D4,  Hexagon::D5,  Hexagon::D6,  Hexagon::D7,
6407cd08927SColin LeMahieu       Hexagon::D8,  Hexagon::D9,  Hexagon::D10, Hexagon::D11,
6417cd08927SColin LeMahieu       Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15};
6427cd08927SColin LeMahieu 
643e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo >> 1, DoubleRegDecoderTable);
6447cd08927SColin LeMahieu }
6457cd08927SColin LeMahieu 
646a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralDoubleLow8RegsRegisterClass(
647a72fad98SKrzysztof Parzyszek     MCInst &Inst, unsigned RegNo, uint64_t /*Address*/, const void *Decoder) {
648a72fad98SKrzysztof Parzyszek   static const MCPhysReg GeneralDoubleLow8RegDecoderTable[] = {
649a72fad98SKrzysztof Parzyszek       Hexagon::D0, Hexagon::D1, Hexagon::D2,  Hexagon::D3,
650a72fad98SKrzysztof Parzyszek       Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11};
651a72fad98SKrzysztof Parzyszek 
652a72fad98SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, GeneralDoubleLow8RegDecoderTable);
653a72fad98SKrzysztof Parzyszek }
654a72fad98SKrzysztof Parzyszek 
65555772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,
6567c958713SColin LeMahieu                                              uint64_t /*Address*/,
6577c958713SColin LeMahieu                                              const void *Decoder) {
65855772976SKrzysztof Parzyszek   static const MCPhysReg HvxWRDecoderTable[] = {
6597c958713SColin LeMahieu       Hexagon::W0,  Hexagon::W1,  Hexagon::W2,  Hexagon::W3,
6607c958713SColin LeMahieu       Hexagon::W4,  Hexagon::W5,  Hexagon::W6,  Hexagon::W7,
6617c958713SColin LeMahieu       Hexagon::W8,  Hexagon::W9,  Hexagon::W10, Hexagon::W11,
6627c958713SColin LeMahieu       Hexagon::W12, Hexagon::W13, Hexagon::W14, Hexagon::W15};
6637c958713SColin LeMahieu 
66455772976SKrzysztof Parzyszek   return (DecodeRegisterClass(Inst, RegNo >> 1, HvxWRDecoderTable));
6657c958713SColin LeMahieu }
6667c958713SColin LeMahieu 
6677cd08927SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
6687cd08927SColin LeMahieu                                                 uint64_t /*Address*/,
6697cd08927SColin LeMahieu                                                 const void *Decoder) {
670e5e035a3SCraig Topper   static const MCPhysReg PredRegDecoderTable[] = {Hexagon::P0, Hexagon::P1,
6717cd08927SColin LeMahieu                                                   Hexagon::P2, Hexagon::P3};
6727cd08927SColin LeMahieu 
673e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, PredRegDecoderTable);
6747cd08927SColin LeMahieu }
6757cd08927SColin LeMahieu 
67655772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,
6777c958713SColin LeMahieu                                              uint64_t /*Address*/,
6787c958713SColin LeMahieu                                              const void *Decoder) {
67955772976SKrzysztof Parzyszek   static const MCPhysReg HvxQRDecoderTable[] = {Hexagon::Q0, Hexagon::Q1,
6807c958713SColin LeMahieu                                                 Hexagon::Q2, Hexagon::Q3};
6817c958713SColin LeMahieu 
68255772976SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, HvxQRDecoderTable);
6837c958713SColin LeMahieu }
684be8c453dSColin LeMahieu 
6857cd08927SColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
6867cd08927SColin LeMahieu                                                uint64_t /*Address*/,
6877cd08927SColin LeMahieu                                                const void *Decoder) {
688f9015e62SKrzysztof Parzyszek   using namespace Hexagon;
689e4fc6ee7SEugene Zelenko 
690e5e035a3SCraig Topper   static const MCPhysReg CtrlRegDecoderTable[] = {
691f9015e62SKrzysztof Parzyszek     /*  0 */  SA0,        LC0,        SA1,        LC1,
692e2603328SKrzysztof Parzyszek     /*  4 */  P3_0,       C5,         M0,         M1,
693f9015e62SKrzysztof Parzyszek     /*  8 */  USR,        PC,         UGP,        GP,
694ab57c2baSKrzysztof Parzyszek     /* 12 */  CS0,        CS1,        UPCYCLELO,  UPCYCLEHI,
695f9015e62SKrzysztof Parzyszek     /* 16 */  FRAMELIMIT, FRAMEKEY,   PKTCOUNTLO, PKTCOUNTHI,
696f9015e62SKrzysztof Parzyszek     /* 20 */  0,          0,          0,          0,
697f9015e62SKrzysztof Parzyszek     /* 24 */  0,          0,          0,          0,
698f9015e62SKrzysztof Parzyszek     /* 28 */  0,          0,          UTIMERLO,   UTIMERHI
6997cd08927SColin LeMahieu   };
7007cd08927SColin LeMahieu 
7016261e1b9SCraig Topper   if (RegNo >= array_lengthof(CtrlRegDecoderTable))
7027cd08927SColin LeMahieu     return MCDisassembler::Fail;
7037cd08927SColin LeMahieu 
704f9015e62SKrzysztof Parzyszek   static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
705f9015e62SKrzysztof Parzyszek   if (CtrlRegDecoderTable[RegNo] == NoRegister)
7067cd08927SColin LeMahieu     return MCDisassembler::Fail;
7077cd08927SColin LeMahieu 
7087cd08927SColin LeMahieu   unsigned Register = CtrlRegDecoderTable[RegNo];
7097cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
7107cd08927SColin LeMahieu   return MCDisassembler::Success;
7117cd08927SColin LeMahieu }
7127cd08927SColin LeMahieu 
7137cd08927SColin LeMahieu static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
7147cd08927SColin LeMahieu                                                  uint64_t /*Address*/,
7157cd08927SColin LeMahieu                                                  const void *Decoder) {
716f9015e62SKrzysztof Parzyszek   using namespace Hexagon;
717e4fc6ee7SEugene Zelenko 
718e5e035a3SCraig Topper   static const MCPhysReg CtrlReg64DecoderTable[] = {
719f9015e62SKrzysztof Parzyszek     /*  0 */  C1_0,       0,          C3_2,       0,
720f9015e62SKrzysztof Parzyszek     /*  4 */  C5_4,       0,          C7_6,       0,
721f9015e62SKrzysztof Parzyszek     /*  8 */  C9_8,       0,          C11_10,     0,
722ab57c2baSKrzysztof Parzyszek     /* 12 */  CS,         0,          UPCYCLE,    0,
723f9015e62SKrzysztof Parzyszek     /* 16 */  C17_16,     0,          PKTCOUNT,   0,
724f9015e62SKrzysztof Parzyszek     /* 20 */  0,          0,          0,          0,
725f9015e62SKrzysztof Parzyszek     /* 24 */  0,          0,          0,          0,
726f9015e62SKrzysztof Parzyszek     /* 28 */  0,          0,          UTIMER,     0
7277cd08927SColin LeMahieu   };
7287cd08927SColin LeMahieu 
7296261e1b9SCraig Topper   if (RegNo >= array_lengthof(CtrlReg64DecoderTable))
7307cd08927SColin LeMahieu     return MCDisassembler::Fail;
7317cd08927SColin LeMahieu 
732f9015e62SKrzysztof Parzyszek   static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
733f9015e62SKrzysztof Parzyszek   if (CtrlReg64DecoderTable[RegNo] == NoRegister)
7347cd08927SColin LeMahieu     return MCDisassembler::Fail;
7357cd08927SColin LeMahieu 
7367cd08927SColin LeMahieu   unsigned Register = CtrlReg64DecoderTable[RegNo];
7377cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
7387cd08927SColin LeMahieu   return MCDisassembler::Success;
7397cd08927SColin LeMahieu }
7407cd08927SColin LeMahieu 
7417cd08927SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
7427cd08927SColin LeMahieu                                                uint64_t /*Address*/,
7437cd08927SColin LeMahieu                                                const void *Decoder) {
7447cd08927SColin LeMahieu   unsigned Register = 0;
7457cd08927SColin LeMahieu   switch (RegNo) {
7467cd08927SColin LeMahieu   case 0:
7477cd08927SColin LeMahieu     Register = Hexagon::M0;
7487cd08927SColin LeMahieu     break;
7497cd08927SColin LeMahieu   case 1:
7507cd08927SColin LeMahieu     Register = Hexagon::M1;
7517cd08927SColin LeMahieu     break;
7527cd08927SColin LeMahieu   default:
7537cd08927SColin LeMahieu     return MCDisassembler::Fail;
7547cd08927SColin LeMahieu   }
7557cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
7567cd08927SColin LeMahieu   return MCDisassembler::Success;
7577cd08927SColin LeMahieu }
7587cd08927SColin LeMahieu 
7597cd08927SColin LeMahieu static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
7607cd08927SColin LeMahieu                                        uint64_t /*Address*/,
7617cd08927SColin LeMahieu                                        const void *Decoder) {
7627cd08927SColin LeMahieu   HexagonDisassembler const &Disassembler = disassembler(Decoder);
763a8ab1b75SKrzysztof Parzyszek   int64_t FullValue = fullValue(Disassembler, MI, tmp);
7647cd08927SColin LeMahieu   assert(FullValue >= 0 && "Negative in unsigned decoder");
7657cd08927SColin LeMahieu   HexagonMCInstrInfo::addConstant(MI, FullValue, Disassembler.getContext());
7667cd08927SColin LeMahieu   return MCDisassembler::Success;
7677cd08927SColin LeMahieu }
7687cd08927SColin LeMahieu 
769a72fad98SKrzysztof Parzyszek static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
770a72fad98SKrzysztof Parzyszek                                     uint64_t /*Address*/, const void *Decoder) {
771a72fad98SKrzysztof Parzyszek   HexagonDisassembler const &Disassembler = disassembler(Decoder);
772a72fad98SKrzysztof Parzyszek   unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
773a72fad98SKrzysztof Parzyszek   tmp = SignExtend64(tmp, Bits);
774a72fad98SKrzysztof Parzyszek   signedDecoder<32>(MI, tmp, Decoder);
775a72fad98SKrzysztof Parzyszek   return MCDisassembler::Success;
776a72fad98SKrzysztof Parzyszek }
777a72fad98SKrzysztof Parzyszek 
7787cd08927SColin LeMahieu // custom decoder for various jump/call immediates
7797cd08927SColin LeMahieu static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
7807cd08927SColin LeMahieu                                     const void *Decoder) {
7817cd08927SColin LeMahieu   HexagonDisassembler const &Disassembler = disassembler(Decoder);
7827cd08927SColin LeMahieu   unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
7837cd08927SColin LeMahieu   // r13_2 is not extendable, so if there are no extent bits, it's r13_2
7847cd08927SColin LeMahieu   if (Bits == 0)
7857cd08927SColin LeMahieu     Bits = 15;
786a8ab1b75SKrzysztof Parzyszek   uint64_t FullValue = fullValue(Disassembler, MI, SignExtend64(tmp, Bits));
787a8ab1b75SKrzysztof Parzyszek   uint32_t Extended = FullValue + Address;
788a72fad98SKrzysztof Parzyszek   if (!Disassembler.tryAddingSymbolicOperand(MI, Extended, Address, true, 0, 4))
7897cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
7907cd08927SColin LeMahieu   return MCDisassembler::Success;
7917cd08927SColin LeMahieu }
79222a21d4cSKrzysztof Parzyszek 
79322a21d4cSKrzysztof Parzyszek static DecodeStatus DecodeGuestRegsRegisterClass(MCInst &Inst, unsigned RegNo,
79422a21d4cSKrzysztof Parzyszek                                                  uint64_t /*Address*/,
79522a21d4cSKrzysztof Parzyszek                                                  const void *Decoder) {
79622a21d4cSKrzysztof Parzyszek   using namespace Hexagon;
79722a21d4cSKrzysztof Parzyszek 
79822a21d4cSKrzysztof Parzyszek   static const MCPhysReg GuestRegDecoderTable[] = {
79922a21d4cSKrzysztof Parzyszek     /*  0 */ GELR,      GSR,        GOSP,       G3,
80022a21d4cSKrzysztof Parzyszek     /*  4 */ G4,        G5,         G6,         G7,
80122a21d4cSKrzysztof Parzyszek     /*  8 */ G8,        G9,         G10,        G11,
80222a21d4cSKrzysztof Parzyszek     /* 12 */ G12,       G13,        G14,        G15,
80322a21d4cSKrzysztof Parzyszek     /* 16 */ GPMUCNT4,  GPMUCNT5,   GPMUCNT6,   GPMUCNT7,
80422a21d4cSKrzysztof Parzyszek     /* 20 */ G20,       G21,        G22,        G23,
80522a21d4cSKrzysztof Parzyszek     /* 24 */ GPCYCLELO, GPCYCLEHI,  GPMUCNT0,   GPMUCNT1,
80622a21d4cSKrzysztof Parzyszek     /* 28 */ GPMUCNT2,  GPMUCNT3,   G30,        G31
80722a21d4cSKrzysztof Parzyszek   };
80822a21d4cSKrzysztof Parzyszek 
80922a21d4cSKrzysztof Parzyszek   if (RegNo >= array_lengthof(GuestRegDecoderTable))
81022a21d4cSKrzysztof Parzyszek     return MCDisassembler::Fail;
81122a21d4cSKrzysztof Parzyszek   if (GuestRegDecoderTable[RegNo] == Hexagon::NoRegister)
81222a21d4cSKrzysztof Parzyszek     return MCDisassembler::Fail;
81322a21d4cSKrzysztof Parzyszek 
81422a21d4cSKrzysztof Parzyszek   unsigned Register = GuestRegDecoderTable[RegNo];
81522a21d4cSKrzysztof Parzyszek   Inst.addOperand(MCOperand::createReg(Register));
81622a21d4cSKrzysztof Parzyszek   return MCDisassembler::Success;
81722a21d4cSKrzysztof Parzyszek }
81822a21d4cSKrzysztof Parzyszek 
81922a21d4cSKrzysztof Parzyszek static DecodeStatus DecodeGuestRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
82022a21d4cSKrzysztof Parzyszek                                                    uint64_t /*Address*/,
82122a21d4cSKrzysztof Parzyszek                                                    const void *Decoder) {
82222a21d4cSKrzysztof Parzyszek   using namespace Hexagon;
82322a21d4cSKrzysztof Parzyszek 
82422a21d4cSKrzysztof Parzyszek   static const MCPhysReg GuestReg64DecoderTable[] = {
82522a21d4cSKrzysztof Parzyszek     /*  0 */ G1_0,      0,          G3_2,       0,
82622a21d4cSKrzysztof Parzyszek     /*  4 */ G5_4,      0,          G7_6,       0,
82722a21d4cSKrzysztof Parzyszek     /*  8 */ G9_8,      0,          G11_10,     0,
82822a21d4cSKrzysztof Parzyszek     /* 12 */ G13_12,    0,          G15_14,     0,
82922a21d4cSKrzysztof Parzyszek     /* 16 */ G17_16,    0,          G19_18,     0,
83022a21d4cSKrzysztof Parzyszek     /* 20 */ G21_20,    0,          G23_22,     0,
83122a21d4cSKrzysztof Parzyszek     /* 24 */ G25_24,    0,          G27_26,     0,
83222a21d4cSKrzysztof Parzyszek     /* 28 */ G29_28,    0,          G31_30,     0
83322a21d4cSKrzysztof Parzyszek   };
83422a21d4cSKrzysztof Parzyszek 
83522a21d4cSKrzysztof Parzyszek   if (RegNo >= array_lengthof(GuestReg64DecoderTable))
83622a21d4cSKrzysztof Parzyszek     return MCDisassembler::Fail;
83722a21d4cSKrzysztof Parzyszek   if (GuestReg64DecoderTable[RegNo] == Hexagon::NoRegister)
83822a21d4cSKrzysztof Parzyszek     return MCDisassembler::Fail;
83922a21d4cSKrzysztof Parzyszek 
84022a21d4cSKrzysztof Parzyszek   unsigned Register = GuestReg64DecoderTable[RegNo];
84122a21d4cSKrzysztof Parzyszek   Inst.addOperand(MCOperand::createReg(Register));
84222a21d4cSKrzysztof Parzyszek   return MCDisassembler::Success;
84322a21d4cSKrzysztof Parzyszek }
844