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 
1288ebb9e2SColin LeMahieu #include "MCTargetDesc/HexagonBaseInfo.h"
137cd08927SColin LeMahieu #include "MCTargetDesc/HexagonMCChecker.h"
147cd08927SColin LeMahieu #include "MCTargetDesc/HexagonMCInstrInfo.h"
156bda14b3SChandler Carruth #include "MCTargetDesc/HexagonMCTargetDesc.h"
1682085927SEugene Zelenko #include "llvm/ADT/ArrayRef.h"
1782085927SEugene Zelenko #include "llvm/ADT/STLExtras.h"
187cd08927SColin LeMahieu #include "llvm/MC/MCContext.h"
196bda14b3SChandler Carruth #include "llvm/MC/MCDisassembler/MCDisassembler.h"
2088ebb9e2SColin LeMahieu #include "llvm/MC/MCExpr.h"
2188ebb9e2SColin LeMahieu #include "llvm/MC/MCFixedLenDisassembler.h"
2288ebb9e2SColin LeMahieu #include "llvm/MC/MCInst.h"
237cd08927SColin LeMahieu #include "llvm/MC/MCInstrInfo.h"
2482085927SEugene Zelenko #include "llvm/MC/MCRegisterInfo.h"
2588ebb9e2SColin LeMahieu #include "llvm/MC/MCSubtargetInfo.h"
26e4fc6ee7SEugene Zelenko #include "llvm/Support/Endian.h"
2782085927SEugene Zelenko #include "llvm/Support/MathExtras.h"
287cd08927SColin LeMahieu #include "llvm/Support/TargetRegistry.h"
296bda14b3SChandler Carruth #include "llvm/Support/raw_ostream.h"
3082085927SEugene Zelenko #include <cassert>
3182085927SEugene Zelenko #include <cstddef>
3282085927SEugene Zelenko #include <cstdint>
3382085927SEugene Zelenko #include <memory>
3488ebb9e2SColin LeMahieu 
3588ebb9e2SColin LeMahieu using namespace llvm;
3668d967d9SColin LeMahieu using namespace Hexagon;
3788ebb9e2SColin LeMahieu 
38e4fc6ee7SEugene Zelenko using DecodeStatus = MCDisassembler::DecodeStatus;
3988ebb9e2SColin LeMahieu 
4088ebb9e2SColin LeMahieu namespace {
4182085927SEugene Zelenko 
425f8f34e4SAdrian Prantl /// Hexagon disassembler for all Hexagon platforms.
4388ebb9e2SColin LeMahieu class HexagonDisassembler : public MCDisassembler {
4488ebb9e2SColin LeMahieu public:
457cd08927SColin LeMahieu   std::unique_ptr<MCInstrInfo const> const MCII;
4668d967d9SColin LeMahieu   std::unique_ptr<MCInst *> CurrentBundle;
47a8ab1b75SKrzysztof Parzyszek   mutable MCInst const *CurrentExtender;
4882085927SEugene Zelenko 
497cd08927SColin LeMahieu   HexagonDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
507cd08927SColin LeMahieu                       MCInstrInfo const *MCII)
51a8ab1b75SKrzysztof Parzyszek       : MCDisassembler(STI, Ctx), MCII(MCII), CurrentBundle(new MCInst *),
52a8ab1b75SKrzysztof Parzyszek         CurrentExtender(nullptr) {}
5388ebb9e2SColin LeMahieu 
5468d967d9SColin LeMahieu   DecodeStatus getSingleInstruction(MCInst &Instr, MCInst &MCB,
5568d967d9SColin LeMahieu                                     ArrayRef<uint8_t> Bytes, uint64_t Address,
5668d967d9SColin LeMahieu                                     raw_ostream &VStream, raw_ostream &CStream,
5768d967d9SColin LeMahieu                                     bool &Complete) const;
584aa6bea7SRafael Espindola   DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
597fc5b874SRafael Espindola                               ArrayRef<uint8_t> Bytes, uint64_t Address,
604aa6bea7SRafael Espindola                               raw_ostream &VStream,
614aa6bea7SRafael Espindola                               raw_ostream &CStream) const override;
62a8ab1b75SKrzysztof Parzyszek   void remapInstruction(MCInst &Instr) const;
6388ebb9e2SColin LeMahieu };
6482085927SEugene Zelenko 
65a8ab1b75SKrzysztof Parzyszek static uint64_t fullValue(HexagonDisassembler const &Disassembler, MCInst &MI,
66a72fad98SKrzysztof Parzyszek                           int64_t Value) {
67a8ab1b75SKrzysztof Parzyszek   MCInstrInfo MCII = *Disassembler.MCII;
68a8ab1b75SKrzysztof Parzyszek   if (!Disassembler.CurrentExtender ||
69a8ab1b75SKrzysztof Parzyszek       MI.size() != HexagonMCInstrInfo::getExtendableOp(MCII, MI))
70a72fad98SKrzysztof Parzyszek     return Value;
71a72fad98SKrzysztof Parzyszek   unsigned Alignment = HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
72a72fad98SKrzysztof Parzyszek   uint32_t Lower6 = static_cast<uint32_t>(Value >> Alignment) & 0x3f;
73a72fad98SKrzysztof Parzyszek   int64_t Bits;
74a8ab1b75SKrzysztof Parzyszek   bool Success =
75a8ab1b75SKrzysztof Parzyszek       Disassembler.CurrentExtender->getOperand(0).getExpr()->evaluateAsAbsolute(
76a8ab1b75SKrzysztof Parzyszek           Bits);
77a8ab1b75SKrzysztof Parzyszek   assert(Success);
78a8ab1b75SKrzysztof Parzyszek   (void)Success;
79a8ab1b75SKrzysztof Parzyszek   uint64_t Upper26 = static_cast<uint64_t>(Bits);
80a8ab1b75SKrzysztof Parzyszek   uint64_t Operand = Upper26 | Lower6;
81a72fad98SKrzysztof Parzyszek   return Operand;
82a72fad98SKrzysztof Parzyszek }
83e4fc6ee7SEugene Zelenko static HexagonDisassembler const &disassembler(void const *Decoder) {
84a72fad98SKrzysztof Parzyszek   return *static_cast<HexagonDisassembler const *>(Decoder);
85a72fad98SKrzysztof Parzyszek }
86a72fad98SKrzysztof Parzyszek template <size_t T>
87e4fc6ee7SEugene Zelenko static void signedDecoder(MCInst &MI, unsigned tmp, const void *Decoder) {
88a72fad98SKrzysztof Parzyszek   HexagonDisassembler const &Disassembler = disassembler(Decoder);
89a8ab1b75SKrzysztof Parzyszek   int64_t FullValue = fullValue(Disassembler, MI, SignExtend64<T>(tmp));
90a72fad98SKrzysztof Parzyszek   int64_t Extended = SignExtend64<32>(FullValue);
91a72fad98SKrzysztof Parzyszek   HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
92a72fad98SKrzysztof Parzyszek }
93a8ab1b75SKrzysztof Parzyszek }
9488ebb9e2SColin LeMahieu 
957cd08927SColin LeMahieu // Forward declare these because the auto-generated code will reference them.
967cd08927SColin LeMahieu // Definitions are further down.
977cd08927SColin LeMahieu 
987cd08927SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
99be8c453dSColin LeMahieu                                                uint64_t Address,
100be8c453dSColin LeMahieu                                                const void *Decoder);
101a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
102a72fad98SKrzysztof Parzyszek                                                       unsigned RegNo,
103a72fad98SKrzysztof Parzyszek                                                       uint64_t Address,
104a72fad98SKrzysztof Parzyszek                                                       const void *Decoder);
1057c958713SColin LeMahieu static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
1067c958713SColin LeMahieu                                                    uint64_t Address,
1077c958713SColin LeMahieu                                                    const void *Decoder);
10855772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,
1097c958713SColin LeMahieu                                              uint64_t Address,
1107c958713SColin LeMahieu                                              const void *Decoder);
1117cd08927SColin LeMahieu static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1127cd08927SColin LeMahieu                                                   uint64_t Address,
1137cd08927SColin LeMahieu                                                   const void *Decoder);
114a72fad98SKrzysztof Parzyszek static DecodeStatus
115a72fad98SKrzysztof Parzyszek DecodeGeneralDoubleLow8RegsRegisterClass(MCInst &Inst, unsigned RegNo,
116a72fad98SKrzysztof Parzyszek                                          uint64_t Address, const void *Decoder);
11755772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,
1187c958713SColin LeMahieu                                              uint64_t Address,
1197c958713SColin LeMahieu                                              const void *Decoder);
1207cd08927SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1217cd08927SColin LeMahieu                                                 uint64_t Address,
1227cd08927SColin LeMahieu                                                 const void *Decoder);
12355772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,
1247c958713SColin LeMahieu                                              uint64_t Address,
1257c958713SColin LeMahieu                                              const void *Decoder);
126f3db884eSColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
127be8c453dSColin LeMahieu                                                uint64_t Address,
128be8c453dSColin LeMahieu                                                const void *Decoder);
12922a21d4cSKrzysztof Parzyszek static DecodeStatus DecodeGuestRegsRegisterClass(MCInst &Inst, unsigned RegNo,
13022a21d4cSKrzysztof Parzyszek                                                  uint64_t Address,
13122a21d4cSKrzysztof Parzyszek                                                  const void *Decoder);
1327cd08927SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1337cd08927SColin LeMahieu                                                uint64_t Address,
1347cd08927SColin LeMahieu                                                const void *Decoder);
135404d5b24SColin LeMahieu static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
136be8c453dSColin LeMahieu                                                  uint64_t Address,
1377cd08927SColin LeMahieu                                                  const void *Decoder);
13822a21d4cSKrzysztof Parzyszek static DecodeStatus DecodeGuestRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
13922a21d4cSKrzysztof Parzyszek                                                    uint64_t Address,
14022a21d4cSKrzysztof Parzyszek                                                    const void *Decoder);
1417cd08927SColin LeMahieu 
1427cd08927SColin LeMahieu static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
1437cd08927SColin LeMahieu                                        uint64_t Address, const void *Decoder);
144a72fad98SKrzysztof Parzyszek static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
145a72fad98SKrzysztof Parzyszek                                     uint64_t /*Address*/, const void *Decoder);
1467cd08927SColin LeMahieu static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1477cd08927SColin LeMahieu                                     const void *Decoder);
148*9dafa8a2SKrzysztof Parzyszek #include "HexagonDepDecoders.h"
14988ebb9e2SColin LeMahieu #include "HexagonGenDisassemblerTables.inc"
15088ebb9e2SColin LeMahieu 
1517cd08927SColin LeMahieu static MCDisassembler *createHexagonDisassembler(const Target &T,
1527cd08927SColin LeMahieu                                                  const MCSubtargetInfo &STI,
15388ebb9e2SColin LeMahieu                                                  MCContext &Ctx) {
1547cd08927SColin LeMahieu   return new HexagonDisassembler(STI, Ctx, T.createMCInstrInfo());
15588ebb9e2SColin LeMahieu }
15688ebb9e2SColin LeMahieu 
15788ebb9e2SColin LeMahieu extern "C" void LLVMInitializeHexagonDisassembler() {
158f42454b9SMehdi Amini   TargetRegistry::RegisterMCDisassembler(getTheHexagonTarget(),
15988ebb9e2SColin LeMahieu                                          createHexagonDisassembler);
16088ebb9e2SColin LeMahieu }
16188ebb9e2SColin LeMahieu 
16288ebb9e2SColin LeMahieu DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
1637fc5b874SRafael Espindola                                                  ArrayRef<uint8_t> Bytes,
16488ebb9e2SColin LeMahieu                                                  uint64_t Address,
16588ebb9e2SColin LeMahieu                                                  raw_ostream &os,
16688ebb9e2SColin LeMahieu                                                  raw_ostream &cs) const {
16768d967d9SColin LeMahieu   DecodeStatus Result = DecodeStatus::Success;
16868d967d9SColin LeMahieu   bool Complete = false;
16968d967d9SColin LeMahieu   Size = 0;
1704aa6bea7SRafael Espindola 
17168d967d9SColin LeMahieu   *CurrentBundle = &MI;
172a8ab1b75SKrzysztof Parzyszek   MI.setOpcode(Hexagon::BUNDLE);
173a8ab1b75SKrzysztof Parzyszek   MI.addOperand(MCOperand::createImm(0));
17482085927SEugene Zelenko   while (Result == Success && !Complete) {
17568d967d9SColin LeMahieu     if (Bytes.size() < HEXAGON_INSTR_SIZE)
17668d967d9SColin LeMahieu       return MCDisassembler::Fail;
17768d967d9SColin LeMahieu     MCInst *Inst = new (getContext()) MCInst;
17868d967d9SColin LeMahieu     Result = getSingleInstruction(*Inst, MI, Bytes, Address, os, cs, Complete);
17968d967d9SColin LeMahieu     MI.addOperand(MCOperand::createInst(Inst));
18068d967d9SColin LeMahieu     Size += HEXAGON_INSTR_SIZE;
18168d967d9SColin LeMahieu     Bytes = Bytes.slice(HEXAGON_INSTR_SIZE);
18268d967d9SColin LeMahieu   }
1837cd08927SColin LeMahieu   if (Result == MCDisassembler::Fail)
18468d967d9SColin LeMahieu     return Result;
185a72fad98SKrzysztof Parzyszek   if (Size > HEXAGON_MAX_PACKET_SIZE)
186a72fad98SKrzysztof Parzyszek     return MCDisassembler::Fail;
187e12d1e70SKrzysztof Parzyszek   HexagonMCChecker Checker(getContext(), *MCII, STI, MI,
188e12d1e70SKrzysztof Parzyszek                            *getContext().getRegisterInfo(), false);
1897cd08927SColin LeMahieu   if (!Checker.check())
1907cd08927SColin LeMahieu     return MCDisassembler::Fail;
191a8ab1b75SKrzysztof Parzyszek   remapInstruction(MI);
1927cd08927SColin LeMahieu   return MCDisassembler::Success;
1937cd08927SColin LeMahieu }
1947cd08927SColin LeMahieu 
195a8ab1b75SKrzysztof Parzyszek void HexagonDisassembler::remapInstruction(MCInst &Instr) const {
196a8ab1b75SKrzysztof Parzyszek   for (auto I: HexagonMCInstrInfo::bundleInstructions(Instr)) {
197a8ab1b75SKrzysztof Parzyszek     auto &MI = const_cast<MCInst &>(*I.getInst());
198a8ab1b75SKrzysztof Parzyszek     switch (MI.getOpcode()) {
199a8ab1b75SKrzysztof Parzyszek     case Hexagon::S2_allocframe:
200a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::R29) {
201a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(Hexagon::S6_allocframe_to_raw);
202a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 1);
203a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
204a8ab1b75SKrzysztof Parzyszek       }
205a8ab1b75SKrzysztof Parzyszek       break;
206a8ab1b75SKrzysztof Parzyszek     case Hexagon::L2_deallocframe:
207a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
208a8ab1b75SKrzysztof Parzyszek           MI.getOperand(1).getReg() == Hexagon::R30) {
209a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L6_deallocframe_map_to_raw);
210a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 1);
211a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
212a8ab1b75SKrzysztof Parzyszek       }
213a8ab1b75SKrzysztof Parzyszek       break;
214a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return:
215a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
216a8ab1b75SKrzysztof Parzyszek           MI.getOperand(1).getReg() == Hexagon::R30) {
217a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L6_return_map_to_raw);
218a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 1);
219a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
220a8ab1b75SKrzysztof Parzyszek       }
221a8ab1b75SKrzysztof Parzyszek       break;
222a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_t:
223a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
224a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
225a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_t);
226a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
227a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
228a8ab1b75SKrzysztof Parzyszek       }
229a8ab1b75SKrzysztof Parzyszek       break;
230a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_f:
231a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
232a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
233a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_f);
234a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
235a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
236a8ab1b75SKrzysztof Parzyszek       }
237a8ab1b75SKrzysztof Parzyszek       break;
238a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_tnew_pt:
239a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
240a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
241a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_tnew_pt);
242a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
243a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
244a8ab1b75SKrzysztof Parzyszek       }
245a8ab1b75SKrzysztof Parzyszek       break;
246a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_fnew_pt:
247a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
248a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
249a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_fnew_pt);
250a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
251a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
252a8ab1b75SKrzysztof Parzyszek       }
253a8ab1b75SKrzysztof Parzyszek       break;
254a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_tnew_pnt:
255a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
256a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
257a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_tnew_pnt);
258a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
259a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
260a8ab1b75SKrzysztof Parzyszek       }
261a8ab1b75SKrzysztof Parzyszek       break;
262a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_fnew_pnt:
263a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
264a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
265a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_fnew_pnt);
266a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
267a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
268a8ab1b75SKrzysztof Parzyszek       }
269a8ab1b75SKrzysztof Parzyszek       break;
270a8ab1b75SKrzysztof Parzyszek     }
271a8ab1b75SKrzysztof Parzyszek   }
272a8ab1b75SKrzysztof Parzyszek }
273a8ab1b75SKrzysztof Parzyszek 
274e4fc6ee7SEugene Zelenko static void adjustDuplex(MCInst &MI, MCContext &Context) {
275a72fad98SKrzysztof Parzyszek   switch (MI.getOpcode()) {
276a72fad98SKrzysztof Parzyszek   case Hexagon::SA1_setin1:
277a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 1,
278a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
279a72fad98SKrzysztof Parzyszek     break;
280a72fad98SKrzysztof Parzyszek   case Hexagon::SA1_dec:
281a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 2,
282a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
283a72fad98SKrzysztof Parzyszek     break;
284a72fad98SKrzysztof Parzyszek   default:
285a72fad98SKrzysztof Parzyszek     break;
2867cd08927SColin LeMahieu   }
287a72fad98SKrzysztof Parzyszek }
28868d967d9SColin LeMahieu 
28968d967d9SColin LeMahieu DecodeStatus HexagonDisassembler::getSingleInstruction(
29068d967d9SColin LeMahieu     MCInst &MI, MCInst &MCB, ArrayRef<uint8_t> Bytes, uint64_t Address,
29168d967d9SColin LeMahieu     raw_ostream &os, raw_ostream &cs, bool &Complete) const {
29268d967d9SColin LeMahieu   assert(Bytes.size() >= HEXAGON_INSTR_SIZE);
29368d967d9SColin LeMahieu 
294a72fad98SKrzysztof Parzyszek   uint32_t Instruction = support::endian::read32le(Bytes.data());
29588ebb9e2SColin LeMahieu 
29668d967d9SColin LeMahieu   auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB);
29768d967d9SColin LeMahieu   if ((Instruction & HexagonII::INST_PARSE_MASK) ==
29868d967d9SColin LeMahieu       HexagonII::INST_PARSE_LOOP_END) {
29968d967d9SColin LeMahieu     if (BundleSize == 0)
30068d967d9SColin LeMahieu       HexagonMCInstrInfo::setInnerLoop(MCB);
30168d967d9SColin LeMahieu     else if (BundleSize == 1)
30268d967d9SColin LeMahieu       HexagonMCInstrInfo::setOuterLoop(MCB);
30368d967d9SColin LeMahieu     else
30468d967d9SColin LeMahieu       return DecodeStatus::Fail;
30568d967d9SColin LeMahieu   }
30668d967d9SColin LeMahieu 
307a8ab1b75SKrzysztof Parzyszek   CurrentExtender = HexagonMCInstrInfo::extenderForIndex(
308a72fad98SKrzysztof Parzyszek       MCB, HexagonMCInstrInfo::bundleSize(MCB));
309a72fad98SKrzysztof Parzyszek 
310a72fad98SKrzysztof Parzyszek   DecodeStatus Result = DecodeStatus::Fail;
31168d967d9SColin LeMahieu   if ((Instruction & HexagonII::INST_PARSE_MASK) ==
312be8c453dSColin LeMahieu       HexagonII::INST_PARSE_DUPLEX) {
313a72fad98SKrzysztof Parzyszek     unsigned duplexIClass;
314a72fad98SKrzysztof Parzyszek     uint8_t const *DecodeLow, *DecodeHigh;
315be8c453dSColin LeMahieu     duplexIClass = ((Instruction >> 28) & 0xe) | ((Instruction >> 13) & 0x1);
316be8c453dSColin LeMahieu     switch (duplexIClass) {
317be8c453dSColin LeMahieu     default:
318be8c453dSColin LeMahieu       return MCDisassembler::Fail;
319be8c453dSColin LeMahieu     case 0:
320a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L132;
321a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
322be8c453dSColin LeMahieu       break;
323be8c453dSColin LeMahieu     case 1:
324a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
325a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
326be8c453dSColin LeMahieu       break;
327be8c453dSColin LeMahieu     case 2:
328a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
329a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
330be8c453dSColin LeMahieu       break;
331be8c453dSColin LeMahieu     case 3:
332a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_A32;
333a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
334be8c453dSColin LeMahieu       break;
335be8c453dSColin LeMahieu     case 4:
336a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L132;
337a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
338be8c453dSColin LeMahieu       break;
339be8c453dSColin LeMahieu     case 5:
340a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
341a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
342be8c453dSColin LeMahieu       break;
343be8c453dSColin LeMahieu     case 6:
344a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
345a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
346be8c453dSColin LeMahieu       break;
347be8c453dSColin LeMahieu     case 7:
348a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
349a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
350be8c453dSColin LeMahieu       break;
351be8c453dSColin LeMahieu     case 8:
352a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
353a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
354be8c453dSColin LeMahieu       break;
355be8c453dSColin LeMahieu     case 9:
356a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
357a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
358be8c453dSColin LeMahieu       break;
359be8c453dSColin LeMahieu     case 10:
360a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
361a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S132;
362be8c453dSColin LeMahieu       break;
363be8c453dSColin LeMahieu     case 11:
364a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
365a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S132;
366be8c453dSColin LeMahieu       break;
367be8c453dSColin LeMahieu     case 12:
368a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
369a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
370be8c453dSColin LeMahieu       break;
371be8c453dSColin LeMahieu     case 13:
372a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
373a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
374be8c453dSColin LeMahieu       break;
375be8c453dSColin LeMahieu     case 14:
376a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
377a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S232;
378be8c453dSColin LeMahieu       break;
379be8c453dSColin LeMahieu     }
380a72fad98SKrzysztof Parzyszek     MI.setOpcode(Hexagon::DuplexIClass0 + duplexIClass);
381be8c453dSColin LeMahieu     MCInst *MILow = new (getContext()) MCInst;
382be8c453dSColin LeMahieu     MCInst *MIHigh = new (getContext()) MCInst;
383a8ab1b75SKrzysztof Parzyszek     auto TmpExtender = CurrentExtender;
384a8ab1b75SKrzysztof Parzyszek     CurrentExtender =
385a8ab1b75SKrzysztof Parzyszek         nullptr; // constant extenders in duplex must always be in slot 1
386a72fad98SKrzysztof Parzyszek     Result = decodeInstruction(DecodeLow, *MILow, Instruction & 0x1fff, Address,
387a72fad98SKrzysztof Parzyszek                                this, STI);
388a8ab1b75SKrzysztof Parzyszek     CurrentExtender = TmpExtender;
389a72fad98SKrzysztof Parzyszek     if (Result != DecodeStatus::Success)
390a72fad98SKrzysztof Parzyszek       return DecodeStatus::Fail;
391a72fad98SKrzysztof Parzyszek     adjustDuplex(*MILow, getContext());
392a72fad98SKrzysztof Parzyszek     Result = decodeInstruction(
393a72fad98SKrzysztof Parzyszek         DecodeHigh, *MIHigh, (Instruction >> 16) & 0x1fff, Address, this, STI);
394a72fad98SKrzysztof Parzyszek     if (Result != DecodeStatus::Success)
395a72fad98SKrzysztof Parzyszek       return DecodeStatus::Fail;
396a72fad98SKrzysztof Parzyszek     adjustDuplex(*MIHigh, getContext());
397be8c453dSColin LeMahieu     MCOperand OPLow = MCOperand::createInst(MILow);
398be8c453dSColin LeMahieu     MCOperand OPHigh = MCOperand::createInst(MIHigh);
399be8c453dSColin LeMahieu     MI.addOperand(OPLow);
400be8c453dSColin LeMahieu     MI.addOperand(OPHigh);
401be8c453dSColin LeMahieu     Complete = true;
402be8c453dSColin LeMahieu   } else {
403be8c453dSColin LeMahieu     if ((Instruction & HexagonII::INST_PARSE_MASK) ==
40468d967d9SColin LeMahieu         HexagonII::INST_PARSE_PACKET_END)
40568d967d9SColin LeMahieu       Complete = true;
4067cd08927SColin LeMahieu 
407a8ab1b75SKrzysztof Parzyszek     if (CurrentExtender != nullptr)
408a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTableMustExtend32, MI, Instruction,
409a72fad98SKrzysztof Parzyszek                                  Address, this, STI);
410a72fad98SKrzysztof Parzyszek 
411a72fad98SKrzysztof Parzyszek     if (Result != MCDisassembler::Success)
412a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTable32, MI, Instruction, Address, this,
413a72fad98SKrzysztof Parzyszek                                  STI);
414a72fad98SKrzysztof Parzyszek 
415a72fad98SKrzysztof Parzyszek     if (Result != MCDisassembler::Success &&
416a72fad98SKrzysztof Parzyszek         STI.getFeatureBits()[Hexagon::ExtensionHVX])
417a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTableEXT_mmvec32, MI, Instruction,
418a72fad98SKrzysztof Parzyszek                                  Address, this, STI);
419a72fad98SKrzysztof Parzyszek 
420be8c453dSColin LeMahieu   }
42168d967d9SColin LeMahieu 
42281707549SColin LeMahieu   switch (MI.getOpcode()) {
42381707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_f_jumpnv_nt:
42481707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_f_jumpnv_t:
42581707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp0_jump_nt:
42681707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp0_jump_t:
42781707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp1_jump_nt:
42881707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp1_jump_t:
42981707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_t_jumpnv_nt:
43081707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_t_jumpnv_t:
43181707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp0_jump_nt:
43281707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp0_jump_t:
43381707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp1_jump_nt:
43481707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp1_jump_t:
43581707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_f_jumpnv_nt:
43681707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_f_jumpnv_t:
43781707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp0_jump_nt:
43881707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp0_jump_t:
43981707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp1_jump_nt:
44081707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp1_jump_t:
44181707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_t_jumpnv_nt:
44281707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_t_jumpnv_t:
44381707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp0_jump_nt:
44481707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp0_jump_t:
44581707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp1_jump_nt:
44681707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp1_jump_t:
447a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 1,
448a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, getContext())));
44981707549SColin LeMahieu     break;
45081707549SColin LeMahieu   default:
45181707549SColin LeMahieu     break;
45281707549SColin LeMahieu   }
45381707549SColin LeMahieu 
4547cd08927SColin LeMahieu   if (HexagonMCInstrInfo::isNewValue(*MCII, MI)) {
4557cd08927SColin LeMahieu     unsigned OpIndex = HexagonMCInstrInfo::getNewValueOp(*MCII, MI);
4567cd08927SColin LeMahieu     MCOperand &MCO = MI.getOperand(OpIndex);
4577cd08927SColin LeMahieu     assert(MCO.isReg() && "New value consumers must be registers");
4587cd08927SColin LeMahieu     unsigned Register =
4597cd08927SColin LeMahieu         getContext().getRegisterInfo()->getEncodingValue(MCO.getReg());
4607cd08927SColin LeMahieu     if ((Register & 0x6) == 0)
4617cd08927SColin LeMahieu       // HexagonPRM 10.11 Bit 1-2 == 0 is reserved
4627cd08927SColin LeMahieu       return MCDisassembler::Fail;
4637cd08927SColin LeMahieu     unsigned Lookback = (Register & 0x6) >> 1;
4647cd08927SColin LeMahieu     unsigned Offset = 1;
4657cd08927SColin LeMahieu     bool Vector = HexagonMCInstrInfo::isVector(*MCII, MI);
466a8ab1b75SKrzysztof Parzyszek     bool PrevVector = false;
4677cd08927SColin LeMahieu     auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
4687cd08927SColin LeMahieu     auto i = Instructions.end() - 1;
4697cd08927SColin LeMahieu     for (auto n = Instructions.begin() - 1;; --i, ++Offset) {
4707cd08927SColin LeMahieu       if (i == n)
4717cd08927SColin LeMahieu         // Couldn't find producer
4727cd08927SColin LeMahieu         return MCDisassembler::Fail;
473a8ab1b75SKrzysztof Parzyszek       bool CurrentVector = HexagonMCInstrInfo::isVector(*MCII, *i->getInst());
474a8ab1b75SKrzysztof Parzyszek       if (Vector && !CurrentVector)
4757cd08927SColin LeMahieu         // Skip scalars when calculating distances for vectors
4767cd08927SColin LeMahieu         ++Lookback;
477a8ab1b75SKrzysztof Parzyszek       if (HexagonMCInstrInfo::isImmext(*i->getInst()) && (Vector == PrevVector))
4787cd08927SColin LeMahieu         ++Lookback;
479a8ab1b75SKrzysztof Parzyszek       PrevVector = CurrentVector;
4807cd08927SColin LeMahieu       if (Offset == Lookback)
4817cd08927SColin LeMahieu         break;
4827cd08927SColin LeMahieu     }
4837cd08927SColin LeMahieu     auto const &Inst = *i->getInst();
4847cd08927SColin LeMahieu     bool SubregBit = (Register & 0x1) != 0;
485a8ab1b75SKrzysztof Parzyszek     if (HexagonMCInstrInfo::hasNewValue2(*MCII, Inst)) {
4867cd08927SColin LeMahieu       // If subreg bit is set we're selecting the second produced newvalue
487a8ab1b75SKrzysztof Parzyszek       unsigned Producer = SubregBit ?
488a8ab1b75SKrzysztof Parzyszek           HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg() :
4897cd08927SColin LeMahieu           HexagonMCInstrInfo::getNewValueOperand2(*MCII, Inst).getReg();
4907cd08927SColin LeMahieu       assert(Producer != Hexagon::NoRegister);
4917cd08927SColin LeMahieu       MCO.setReg(Producer);
4927cd08927SColin LeMahieu     } else if (HexagonMCInstrInfo::hasNewValue(*MCII, Inst)) {
4937cd08927SColin LeMahieu       unsigned Producer =
4947cd08927SColin LeMahieu           HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg();
4957cd08927SColin LeMahieu       if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
4967cd08927SColin LeMahieu         Producer = ((Producer - Hexagon::W0) << 1) + SubregBit + Hexagon::V0;
4977cd08927SColin LeMahieu       else if (SubregBit)
4986fdfa3dcSColin LeMahieu         // Hexagon PRM 10.11 New-value operands
4996fdfa3dcSColin LeMahieu         // Nt[0] is reserved and should always be encoded as zero.
5007cd08927SColin LeMahieu         return MCDisassembler::Fail;
5017cd08927SColin LeMahieu       assert(Producer != Hexagon::NoRegister);
5027cd08927SColin LeMahieu       MCO.setReg(Producer);
5037cd08927SColin LeMahieu     } else
5047cd08927SColin LeMahieu       return MCDisassembler::Fail;
5057cd08927SColin LeMahieu   }
5067cd08927SColin LeMahieu 
507a8ab1b75SKrzysztof Parzyszek   if (CurrentExtender != nullptr) {
508a72fad98SKrzysztof Parzyszek     MCInst const &Inst = HexagonMCInstrInfo::isDuplex(*MCII, MI)
509a72fad98SKrzysztof Parzyszek                              ? *MI.getOperand(1).getInst()
510a72fad98SKrzysztof Parzyszek                              : MI;
5117cd08927SColin LeMahieu     if (!HexagonMCInstrInfo::isExtendable(*MCII, Inst) &&
5127cd08927SColin LeMahieu         !HexagonMCInstrInfo::isExtended(*MCII, Inst))
5137cd08927SColin LeMahieu       return MCDisassembler::Fail;
5147cd08927SColin LeMahieu   }
5155d6f03bdSColin LeMahieu   return Result;
51688ebb9e2SColin LeMahieu }
5177cd08927SColin LeMahieu 
5187cd08927SColin LeMahieu static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,
519e5e035a3SCraig Topper                                         ArrayRef<MCPhysReg> Table) {
5203da000c0SCraig Topper   if (RegNo < Table.size()) {
5217cd08927SColin LeMahieu     Inst.addOperand(MCOperand::createReg(Table[RegNo]));
5227cd08927SColin LeMahieu     return MCDisassembler::Success;
5233da000c0SCraig Topper   }
5243da000c0SCraig Topper 
5257cd08927SColin LeMahieu   return MCDisassembler::Fail;
5267cd08927SColin LeMahieu }
5277cd08927SColin LeMahieu 
5287c958713SColin LeMahieu static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
5297c958713SColin LeMahieu                                                    uint64_t Address,
5307c958713SColin LeMahieu                                                    const void *Decoder) {
5317c958713SColin LeMahieu   return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
5327c958713SColin LeMahieu }
5337c958713SColin LeMahieu 
5347cd08927SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5357cd08927SColin LeMahieu                                                uint64_t Address,
5367cd08927SColin LeMahieu                                                const void *Decoder) {
537e5e035a3SCraig Topper   static const MCPhysReg IntRegDecoderTable[] = {
5387cd08927SColin LeMahieu       Hexagon::R0,  Hexagon::R1,  Hexagon::R2,  Hexagon::R3,  Hexagon::R4,
5397cd08927SColin LeMahieu       Hexagon::R5,  Hexagon::R6,  Hexagon::R7,  Hexagon::R8,  Hexagon::R9,
5407cd08927SColin LeMahieu       Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
5417cd08927SColin LeMahieu       Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
5427cd08927SColin LeMahieu       Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
5437cd08927SColin LeMahieu       Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
5447cd08927SColin LeMahieu       Hexagon::R30, Hexagon::R31};
5457cd08927SColin LeMahieu 
546e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, IntRegDecoderTable);
5477cd08927SColin LeMahieu }
5487c958713SColin LeMahieu 
549a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
550a72fad98SKrzysztof Parzyszek                                                       unsigned RegNo,
551a72fad98SKrzysztof Parzyszek                                                       uint64_t Address,
552a72fad98SKrzysztof Parzyszek                                                       const void *Decoder) {
553a72fad98SKrzysztof Parzyszek   static const MCPhysReg GeneralSubRegDecoderTable[] = {
554a72fad98SKrzysztof Parzyszek       Hexagon::R0,  Hexagon::R1,  Hexagon::R2,  Hexagon::R3,
555a72fad98SKrzysztof Parzyszek       Hexagon::R4,  Hexagon::R5,  Hexagon::R6,  Hexagon::R7,
556a72fad98SKrzysztof Parzyszek       Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
557a72fad98SKrzysztof Parzyszek       Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23,
558a72fad98SKrzysztof Parzyszek   };
559a72fad98SKrzysztof Parzyszek 
560a72fad98SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, GeneralSubRegDecoderTable);
561a72fad98SKrzysztof Parzyszek }
562a72fad98SKrzysztof Parzyszek 
56355772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,
5647c958713SColin LeMahieu                                              uint64_t /*Address*/,
5657c958713SColin LeMahieu                                              const void *Decoder) {
56655772976SKrzysztof Parzyszek   static const MCPhysReg HvxVRDecoderTable[] = {
5677c958713SColin LeMahieu       Hexagon::V0,  Hexagon::V1,  Hexagon::V2,  Hexagon::V3,  Hexagon::V4,
5687c958713SColin LeMahieu       Hexagon::V5,  Hexagon::V6,  Hexagon::V7,  Hexagon::V8,  Hexagon::V9,
5697c958713SColin LeMahieu       Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
5707c958713SColin LeMahieu       Hexagon::V15, Hexagon::V16, Hexagon::V17, Hexagon::V18, Hexagon::V19,
5717c958713SColin LeMahieu       Hexagon::V20, Hexagon::V21, Hexagon::V22, Hexagon::V23, Hexagon::V24,
5727c958713SColin LeMahieu       Hexagon::V25, Hexagon::V26, Hexagon::V27, Hexagon::V28, Hexagon::V29,
5737c958713SColin LeMahieu       Hexagon::V30, Hexagon::V31};
5747c958713SColin LeMahieu 
57555772976SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, HvxVRDecoderTable);
5767c958713SColin LeMahieu }
5777c958713SColin LeMahieu 
5787cd08927SColin LeMahieu static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5797cd08927SColin LeMahieu                                                   uint64_t /*Address*/,
5807cd08927SColin LeMahieu                                                   const void *Decoder) {
581e5e035a3SCraig Topper   static const MCPhysReg DoubleRegDecoderTable[] = {
5827cd08927SColin LeMahieu       Hexagon::D0,  Hexagon::D1,  Hexagon::D2,  Hexagon::D3,
5837cd08927SColin LeMahieu       Hexagon::D4,  Hexagon::D5,  Hexagon::D6,  Hexagon::D7,
5847cd08927SColin LeMahieu       Hexagon::D8,  Hexagon::D9,  Hexagon::D10, Hexagon::D11,
5857cd08927SColin LeMahieu       Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15};
5867cd08927SColin LeMahieu 
587e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo >> 1, DoubleRegDecoderTable);
5887cd08927SColin LeMahieu }
5897cd08927SColin LeMahieu 
590a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralDoubleLow8RegsRegisterClass(
591a72fad98SKrzysztof Parzyszek     MCInst &Inst, unsigned RegNo, uint64_t /*Address*/, const void *Decoder) {
592a72fad98SKrzysztof Parzyszek   static const MCPhysReg GeneralDoubleLow8RegDecoderTable[] = {
593a72fad98SKrzysztof Parzyszek       Hexagon::D0, Hexagon::D1, Hexagon::D2,  Hexagon::D3,
594a72fad98SKrzysztof Parzyszek       Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11};
595a72fad98SKrzysztof Parzyszek 
596a72fad98SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, GeneralDoubleLow8RegDecoderTable);
597a72fad98SKrzysztof Parzyszek }
598a72fad98SKrzysztof Parzyszek 
59955772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,
6007c958713SColin LeMahieu                                              uint64_t /*Address*/,
6017c958713SColin LeMahieu                                              const void *Decoder) {
60255772976SKrzysztof Parzyszek   static const MCPhysReg HvxWRDecoderTable[] = {
6037c958713SColin LeMahieu       Hexagon::W0,  Hexagon::W1,  Hexagon::W2,  Hexagon::W3,
6047c958713SColin LeMahieu       Hexagon::W4,  Hexagon::W5,  Hexagon::W6,  Hexagon::W7,
6057c958713SColin LeMahieu       Hexagon::W8,  Hexagon::W9,  Hexagon::W10, Hexagon::W11,
6067c958713SColin LeMahieu       Hexagon::W12, Hexagon::W13, Hexagon::W14, Hexagon::W15};
6077c958713SColin LeMahieu 
60855772976SKrzysztof Parzyszek   return (DecodeRegisterClass(Inst, RegNo >> 1, HvxWRDecoderTable));
6097c958713SColin LeMahieu }
6107c958713SColin LeMahieu 
6117cd08927SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
6127cd08927SColin LeMahieu                                                 uint64_t /*Address*/,
6137cd08927SColin LeMahieu                                                 const void *Decoder) {
614e5e035a3SCraig Topper   static const MCPhysReg PredRegDecoderTable[] = {Hexagon::P0, Hexagon::P1,
6157cd08927SColin LeMahieu                                                   Hexagon::P2, Hexagon::P3};
6167cd08927SColin LeMahieu 
617e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, PredRegDecoderTable);
6187cd08927SColin LeMahieu }
6197cd08927SColin LeMahieu 
62055772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,
6217c958713SColin LeMahieu                                              uint64_t /*Address*/,
6227c958713SColin LeMahieu                                              const void *Decoder) {
62355772976SKrzysztof Parzyszek   static const MCPhysReg HvxQRDecoderTable[] = {Hexagon::Q0, Hexagon::Q1,
6247c958713SColin LeMahieu                                                 Hexagon::Q2, Hexagon::Q3};
6257c958713SColin LeMahieu 
62655772976SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, HvxQRDecoderTable);
6277c958713SColin LeMahieu }
628be8c453dSColin LeMahieu 
6297cd08927SColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
6307cd08927SColin LeMahieu                                                uint64_t /*Address*/,
6317cd08927SColin LeMahieu                                                const void *Decoder) {
632f9015e62SKrzysztof Parzyszek   using namespace Hexagon;
633e4fc6ee7SEugene Zelenko 
634e5e035a3SCraig Topper   static const MCPhysReg CtrlRegDecoderTable[] = {
635f9015e62SKrzysztof Parzyszek     /*  0 */  SA0,        LC0,        SA1,        LC1,
636e2603328SKrzysztof Parzyszek     /*  4 */  P3_0,       C5,         M0,         M1,
637f9015e62SKrzysztof Parzyszek     /*  8 */  USR,        PC,         UGP,        GP,
638ab57c2baSKrzysztof Parzyszek     /* 12 */  CS0,        CS1,        UPCYCLELO,  UPCYCLEHI,
639f9015e62SKrzysztof Parzyszek     /* 16 */  FRAMELIMIT, FRAMEKEY,   PKTCOUNTLO, PKTCOUNTHI,
640f9015e62SKrzysztof Parzyszek     /* 20 */  0,          0,          0,          0,
641f9015e62SKrzysztof Parzyszek     /* 24 */  0,          0,          0,          0,
642f9015e62SKrzysztof Parzyszek     /* 28 */  0,          0,          UTIMERLO,   UTIMERHI
6437cd08927SColin LeMahieu   };
6447cd08927SColin LeMahieu 
6456261e1b9SCraig Topper   if (RegNo >= array_lengthof(CtrlRegDecoderTable))
6467cd08927SColin LeMahieu     return MCDisassembler::Fail;
6477cd08927SColin LeMahieu 
648f9015e62SKrzysztof Parzyszek   static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
649f9015e62SKrzysztof Parzyszek   if (CtrlRegDecoderTable[RegNo] == NoRegister)
6507cd08927SColin LeMahieu     return MCDisassembler::Fail;
6517cd08927SColin LeMahieu 
6527cd08927SColin LeMahieu   unsigned Register = CtrlRegDecoderTable[RegNo];
6537cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
6547cd08927SColin LeMahieu   return MCDisassembler::Success;
6557cd08927SColin LeMahieu }
6567cd08927SColin LeMahieu 
6577cd08927SColin LeMahieu static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
6587cd08927SColin LeMahieu                                                  uint64_t /*Address*/,
6597cd08927SColin LeMahieu                                                  const void *Decoder) {
660f9015e62SKrzysztof Parzyszek   using namespace Hexagon;
661e4fc6ee7SEugene Zelenko 
662e5e035a3SCraig Topper   static const MCPhysReg CtrlReg64DecoderTable[] = {
663f9015e62SKrzysztof Parzyszek     /*  0 */  C1_0,       0,          C3_2,       0,
664f9015e62SKrzysztof Parzyszek     /*  4 */  C5_4,       0,          C7_6,       0,
665f9015e62SKrzysztof Parzyszek     /*  8 */  C9_8,       0,          C11_10,     0,
666ab57c2baSKrzysztof Parzyszek     /* 12 */  CS,         0,          UPCYCLE,    0,
667f9015e62SKrzysztof Parzyszek     /* 16 */  C17_16,     0,          PKTCOUNT,   0,
668f9015e62SKrzysztof Parzyszek     /* 20 */  0,          0,          0,          0,
669f9015e62SKrzysztof Parzyszek     /* 24 */  0,          0,          0,          0,
670f9015e62SKrzysztof Parzyszek     /* 28 */  0,          0,          UTIMER,     0
6717cd08927SColin LeMahieu   };
6727cd08927SColin LeMahieu 
6736261e1b9SCraig Topper   if (RegNo >= array_lengthof(CtrlReg64DecoderTable))
6747cd08927SColin LeMahieu     return MCDisassembler::Fail;
6757cd08927SColin LeMahieu 
676f9015e62SKrzysztof Parzyszek   static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
677f9015e62SKrzysztof Parzyszek   if (CtrlReg64DecoderTable[RegNo] == NoRegister)
6787cd08927SColin LeMahieu     return MCDisassembler::Fail;
6797cd08927SColin LeMahieu 
6807cd08927SColin LeMahieu   unsigned Register = CtrlReg64DecoderTable[RegNo];
6817cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
6827cd08927SColin LeMahieu   return MCDisassembler::Success;
6837cd08927SColin LeMahieu }
6847cd08927SColin LeMahieu 
6857cd08927SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
6867cd08927SColin LeMahieu                                                uint64_t /*Address*/,
6877cd08927SColin LeMahieu                                                const void *Decoder) {
6887cd08927SColin LeMahieu   unsigned Register = 0;
6897cd08927SColin LeMahieu   switch (RegNo) {
6907cd08927SColin LeMahieu   case 0:
6917cd08927SColin LeMahieu     Register = Hexagon::M0;
6927cd08927SColin LeMahieu     break;
6937cd08927SColin LeMahieu   case 1:
6947cd08927SColin LeMahieu     Register = Hexagon::M1;
6957cd08927SColin LeMahieu     break;
6967cd08927SColin LeMahieu   default:
6977cd08927SColin LeMahieu     return MCDisassembler::Fail;
6987cd08927SColin LeMahieu   }
6997cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
7007cd08927SColin LeMahieu   return MCDisassembler::Success;
7017cd08927SColin LeMahieu }
7027cd08927SColin LeMahieu 
7037cd08927SColin LeMahieu static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
7047cd08927SColin LeMahieu                                        uint64_t /*Address*/,
7057cd08927SColin LeMahieu                                        const void *Decoder) {
7067cd08927SColin LeMahieu   HexagonDisassembler const &Disassembler = disassembler(Decoder);
707a8ab1b75SKrzysztof Parzyszek   int64_t FullValue = fullValue(Disassembler, MI, tmp);
7087cd08927SColin LeMahieu   assert(FullValue >= 0 && "Negative in unsigned decoder");
7097cd08927SColin LeMahieu   HexagonMCInstrInfo::addConstant(MI, FullValue, Disassembler.getContext());
7107cd08927SColin LeMahieu   return MCDisassembler::Success;
7117cd08927SColin LeMahieu }
7127cd08927SColin LeMahieu 
713a72fad98SKrzysztof Parzyszek static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
714a72fad98SKrzysztof Parzyszek                                     uint64_t /*Address*/, const void *Decoder) {
715a72fad98SKrzysztof Parzyszek   HexagonDisassembler const &Disassembler = disassembler(Decoder);
716a72fad98SKrzysztof Parzyszek   unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
717a72fad98SKrzysztof Parzyszek   tmp = SignExtend64(tmp, Bits);
718a72fad98SKrzysztof Parzyszek   signedDecoder<32>(MI, tmp, Decoder);
719a72fad98SKrzysztof Parzyszek   return MCDisassembler::Success;
720a72fad98SKrzysztof Parzyszek }
721a72fad98SKrzysztof Parzyszek 
7227cd08927SColin LeMahieu // custom decoder for various jump/call immediates
7237cd08927SColin LeMahieu static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
7247cd08927SColin LeMahieu                                     const void *Decoder) {
7257cd08927SColin LeMahieu   HexagonDisassembler const &Disassembler = disassembler(Decoder);
7267cd08927SColin LeMahieu   unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
7277cd08927SColin LeMahieu   // r13_2 is not extendable, so if there are no extent bits, it's r13_2
7287cd08927SColin LeMahieu   if (Bits == 0)
7297cd08927SColin LeMahieu     Bits = 15;
730a8ab1b75SKrzysztof Parzyszek   uint64_t FullValue = fullValue(Disassembler, MI, SignExtend64(tmp, Bits));
731a8ab1b75SKrzysztof Parzyszek   uint32_t Extended = FullValue + Address;
732a72fad98SKrzysztof Parzyszek   if (!Disassembler.tryAddingSymbolicOperand(MI, Extended, Address, true, 0, 4))
7337cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
7347cd08927SColin LeMahieu   return MCDisassembler::Success;
7357cd08927SColin LeMahieu }
73622a21d4cSKrzysztof Parzyszek 
73722a21d4cSKrzysztof Parzyszek static DecodeStatus DecodeGuestRegsRegisterClass(MCInst &Inst, unsigned RegNo,
73822a21d4cSKrzysztof Parzyszek                                                  uint64_t /*Address*/,
73922a21d4cSKrzysztof Parzyszek                                                  const void *Decoder) {
74022a21d4cSKrzysztof Parzyszek   using namespace Hexagon;
74122a21d4cSKrzysztof Parzyszek 
74222a21d4cSKrzysztof Parzyszek   static const MCPhysReg GuestRegDecoderTable[] = {
74322a21d4cSKrzysztof Parzyszek     /*  0 */ GELR,      GSR,        GOSP,       G3,
74422a21d4cSKrzysztof Parzyszek     /*  4 */ G4,        G5,         G6,         G7,
74522a21d4cSKrzysztof Parzyszek     /*  8 */ G8,        G9,         G10,        G11,
74622a21d4cSKrzysztof Parzyszek     /* 12 */ G12,       G13,        G14,        G15,
74722a21d4cSKrzysztof Parzyszek     /* 16 */ GPMUCNT4,  GPMUCNT5,   GPMUCNT6,   GPMUCNT7,
74822a21d4cSKrzysztof Parzyszek     /* 20 */ G20,       G21,        G22,        G23,
74922a21d4cSKrzysztof Parzyszek     /* 24 */ GPCYCLELO, GPCYCLEHI,  GPMUCNT0,   GPMUCNT1,
75022a21d4cSKrzysztof Parzyszek     /* 28 */ GPMUCNT2,  GPMUCNT3,   G30,        G31
75122a21d4cSKrzysztof Parzyszek   };
75222a21d4cSKrzysztof Parzyszek 
75322a21d4cSKrzysztof Parzyszek   if (RegNo >= array_lengthof(GuestRegDecoderTable))
75422a21d4cSKrzysztof Parzyszek     return MCDisassembler::Fail;
75522a21d4cSKrzysztof Parzyszek   if (GuestRegDecoderTable[RegNo] == Hexagon::NoRegister)
75622a21d4cSKrzysztof Parzyszek     return MCDisassembler::Fail;
75722a21d4cSKrzysztof Parzyszek 
75822a21d4cSKrzysztof Parzyszek   unsigned Register = GuestRegDecoderTable[RegNo];
75922a21d4cSKrzysztof Parzyszek   Inst.addOperand(MCOperand::createReg(Register));
76022a21d4cSKrzysztof Parzyszek   return MCDisassembler::Success;
76122a21d4cSKrzysztof Parzyszek }
76222a21d4cSKrzysztof Parzyszek 
76322a21d4cSKrzysztof Parzyszek static DecodeStatus DecodeGuestRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
76422a21d4cSKrzysztof Parzyszek                                                    uint64_t /*Address*/,
76522a21d4cSKrzysztof Parzyszek                                                    const void *Decoder) {
76622a21d4cSKrzysztof Parzyszek   using namespace Hexagon;
76722a21d4cSKrzysztof Parzyszek 
76822a21d4cSKrzysztof Parzyszek   static const MCPhysReg GuestReg64DecoderTable[] = {
76922a21d4cSKrzysztof Parzyszek     /*  0 */ G1_0,      0,          G3_2,       0,
77022a21d4cSKrzysztof Parzyszek     /*  4 */ G5_4,      0,          G7_6,       0,
77122a21d4cSKrzysztof Parzyszek     /*  8 */ G9_8,      0,          G11_10,     0,
77222a21d4cSKrzysztof Parzyszek     /* 12 */ G13_12,    0,          G15_14,     0,
77322a21d4cSKrzysztof Parzyszek     /* 16 */ G17_16,    0,          G19_18,     0,
77422a21d4cSKrzysztof Parzyszek     /* 20 */ G21_20,    0,          G23_22,     0,
77522a21d4cSKrzysztof Parzyszek     /* 24 */ G25_24,    0,          G27_26,     0,
77622a21d4cSKrzysztof Parzyszek     /* 28 */ G29_28,    0,          G31_30,     0
77722a21d4cSKrzysztof Parzyszek   };
77822a21d4cSKrzysztof Parzyszek 
77922a21d4cSKrzysztof Parzyszek   if (RegNo >= array_lengthof(GuestReg64DecoderTable))
78022a21d4cSKrzysztof Parzyszek     return MCDisassembler::Fail;
78122a21d4cSKrzysztof Parzyszek   if (GuestReg64DecoderTable[RegNo] == Hexagon::NoRegister)
78222a21d4cSKrzysztof Parzyszek     return MCDisassembler::Fail;
78322a21d4cSKrzysztof Parzyszek 
78422a21d4cSKrzysztof Parzyszek   unsigned Register = GuestReg64DecoderTable[RegNo];
78522a21d4cSKrzysztof Parzyszek   Inst.addOperand(MCOperand::createReg(Register));
78622a21d4cSKrzysztof Parzyszek   return MCDisassembler::Success;
78722a21d4cSKrzysztof Parzyszek }
788