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 
4388ebb9e2SColin LeMahieu /// \brief 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;
48*a8ab1b75SKrzysztof Parzyszek   mutable MCInst const *CurrentExtender;
4982085927SEugene Zelenko 
507cd08927SColin LeMahieu   HexagonDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
517cd08927SColin LeMahieu                       MCInstrInfo const *MCII)
52*a8ab1b75SKrzysztof Parzyszek       : MCDisassembler(STI, Ctx), MCII(MCII), CurrentBundle(new MCInst *),
53*a8ab1b75SKrzysztof 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;
63*a8ab1b75SKrzysztof Parzyszek   void remapInstruction(MCInst &Instr) const;
6488ebb9e2SColin LeMahieu };
6582085927SEugene Zelenko 
66*a8ab1b75SKrzysztof Parzyszek static uint64_t fullValue(HexagonDisassembler const &Disassembler, MCInst &MI,
67a72fad98SKrzysztof Parzyszek                           int64_t Value) {
68*a8ab1b75SKrzysztof Parzyszek   MCInstrInfo MCII = *Disassembler.MCII;
69*a8ab1b75SKrzysztof Parzyszek   if (!Disassembler.CurrentExtender ||
70*a8ab1b75SKrzysztof 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;
75*a8ab1b75SKrzysztof Parzyszek   bool Success =
76*a8ab1b75SKrzysztof Parzyszek       Disassembler.CurrentExtender->getOperand(0).getExpr()->evaluateAsAbsolute(
77*a8ab1b75SKrzysztof Parzyszek           Bits);
78*a8ab1b75SKrzysztof Parzyszek   assert(Success);
79*a8ab1b75SKrzysztof Parzyszek   (void)Success;
80*a8ab1b75SKrzysztof Parzyszek   uint64_t Upper26 = static_cast<uint64_t>(Bits);
81*a8ab1b75SKrzysztof 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);
90*a8ab1b75SKrzysztof 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 }
94*a8ab1b75SKrzysztof 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);
1307cd08927SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1317cd08927SColin LeMahieu                                                uint64_t Address,
1327cd08927SColin LeMahieu                                                const void *Decoder);
133404d5b24SColin LeMahieu static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
134be8c453dSColin LeMahieu                                                  uint64_t Address,
1357cd08927SColin LeMahieu                                                  const void *Decoder);
1367cd08927SColin LeMahieu 
1377cd08927SColin LeMahieu static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
1387cd08927SColin LeMahieu                                        uint64_t Address, const void *Decoder);
139a72fad98SKrzysztof Parzyszek static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
140a72fad98SKrzysztof Parzyszek                                     uint64_t /*Address*/, const void *Decoder);
1417cd08927SColin LeMahieu static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1427cd08927SColin LeMahieu                                     const void *Decoder);
143efa74e02SColin LeMahieu 
144*a8ab1b75SKrzysztof Parzyszek #include "HexagonDepDecoders.h"
14588ebb9e2SColin LeMahieu #include "HexagonGenDisassemblerTables.inc"
14688ebb9e2SColin LeMahieu 
1477cd08927SColin LeMahieu static MCDisassembler *createHexagonDisassembler(const Target &T,
1487cd08927SColin LeMahieu                                                  const MCSubtargetInfo &STI,
14988ebb9e2SColin LeMahieu                                                  MCContext &Ctx) {
1507cd08927SColin LeMahieu   return new HexagonDisassembler(STI, Ctx, T.createMCInstrInfo());
15188ebb9e2SColin LeMahieu }
15288ebb9e2SColin LeMahieu 
15388ebb9e2SColin LeMahieu extern "C" void LLVMInitializeHexagonDisassembler() {
154f42454b9SMehdi Amini   TargetRegistry::RegisterMCDisassembler(getTheHexagonTarget(),
15588ebb9e2SColin LeMahieu                                          createHexagonDisassembler);
15688ebb9e2SColin LeMahieu }
15788ebb9e2SColin LeMahieu 
15888ebb9e2SColin LeMahieu DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
1597fc5b874SRafael Espindola                                                  ArrayRef<uint8_t> Bytes,
16088ebb9e2SColin LeMahieu                                                  uint64_t Address,
16188ebb9e2SColin LeMahieu                                                  raw_ostream &os,
16288ebb9e2SColin LeMahieu                                                  raw_ostream &cs) const {
163*a8ab1b75SKrzysztof Parzyszek   (void)&s10_0ImmDecoder;
164*a8ab1b75SKrzysztof Parzyszek   (void)&s10_6ImmDecoder;
16568d967d9SColin LeMahieu   DecodeStatus Result = DecodeStatus::Success;
16668d967d9SColin LeMahieu   bool Complete = false;
16768d967d9SColin LeMahieu   Size = 0;
1684aa6bea7SRafael Espindola 
16968d967d9SColin LeMahieu   *CurrentBundle = &MI;
170*a8ab1b75SKrzysztof Parzyszek   MI.setOpcode(Hexagon::BUNDLE);
171*a8ab1b75SKrzysztof Parzyszek   MI.addOperand(MCOperand::createImm(0));
17282085927SEugene Zelenko   while (Result == Success && !Complete) {
17368d967d9SColin LeMahieu     if (Bytes.size() < HEXAGON_INSTR_SIZE)
17468d967d9SColin LeMahieu       return MCDisassembler::Fail;
17568d967d9SColin LeMahieu     MCInst *Inst = new (getContext()) MCInst;
17668d967d9SColin LeMahieu     Result = getSingleInstruction(*Inst, MI, Bytes, Address, os, cs, Complete);
17768d967d9SColin LeMahieu     MI.addOperand(MCOperand::createInst(Inst));
17868d967d9SColin LeMahieu     Size += HEXAGON_INSTR_SIZE;
17968d967d9SColin LeMahieu     Bytes = Bytes.slice(HEXAGON_INSTR_SIZE);
18068d967d9SColin LeMahieu   }
1817cd08927SColin LeMahieu   if (Result == MCDisassembler::Fail)
18268d967d9SColin LeMahieu     return Result;
183a72fad98SKrzysztof Parzyszek   if (Size > HEXAGON_MAX_PACKET_SIZE)
184a72fad98SKrzysztof Parzyszek     return MCDisassembler::Fail;
185e12d1e70SKrzysztof Parzyszek   HexagonMCChecker Checker(getContext(), *MCII, STI, MI,
186e12d1e70SKrzysztof Parzyszek                            *getContext().getRegisterInfo(), false);
1877cd08927SColin LeMahieu   if (!Checker.check())
1887cd08927SColin LeMahieu     return MCDisassembler::Fail;
189*a8ab1b75SKrzysztof Parzyszek   remapInstruction(MI);
1907cd08927SColin LeMahieu   return MCDisassembler::Success;
1917cd08927SColin LeMahieu }
1927cd08927SColin LeMahieu 
193*a8ab1b75SKrzysztof Parzyszek void HexagonDisassembler::remapInstruction(MCInst &Instr) const {
194*a8ab1b75SKrzysztof Parzyszek   for (auto I: HexagonMCInstrInfo::bundleInstructions(Instr)) {
195*a8ab1b75SKrzysztof Parzyszek     auto &MI = const_cast<MCInst &>(*I.getInst());
196*a8ab1b75SKrzysztof Parzyszek     switch (MI.getOpcode()) {
197*a8ab1b75SKrzysztof Parzyszek     case Hexagon::S2_allocframe:
198*a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::R29) {
199*a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(Hexagon::S6_allocframe_to_raw);
200*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 1);
201*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
202*a8ab1b75SKrzysztof Parzyszek       }
203*a8ab1b75SKrzysztof Parzyszek       break;
204*a8ab1b75SKrzysztof Parzyszek     case Hexagon::L2_deallocframe:
205*a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
206*a8ab1b75SKrzysztof Parzyszek           MI.getOperand(1).getReg() == Hexagon::R30) {
207*a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L6_deallocframe_map_to_raw);
208*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 1);
209*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
210*a8ab1b75SKrzysztof Parzyszek       }
211*a8ab1b75SKrzysztof Parzyszek       break;
212*a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return:
213*a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
214*a8ab1b75SKrzysztof Parzyszek           MI.getOperand(1).getReg() == Hexagon::R30) {
215*a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L6_return_map_to_raw);
216*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 1);
217*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
218*a8ab1b75SKrzysztof Parzyszek       }
219*a8ab1b75SKrzysztof Parzyszek       break;
220*a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_t:
221*a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
222*a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
223*a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_t);
224*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
225*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
226*a8ab1b75SKrzysztof Parzyszek       }
227*a8ab1b75SKrzysztof Parzyszek       break;
228*a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_f:
229*a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
230*a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
231*a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_f);
232*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
233*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
234*a8ab1b75SKrzysztof Parzyszek       }
235*a8ab1b75SKrzysztof Parzyszek       break;
236*a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_tnew_pt:
237*a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
238*a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
239*a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_tnew_pt);
240*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
241*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
242*a8ab1b75SKrzysztof Parzyszek       }
243*a8ab1b75SKrzysztof Parzyszek       break;
244*a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_fnew_pt:
245*a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
246*a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
247*a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_fnew_pt);
248*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
249*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
250*a8ab1b75SKrzysztof Parzyszek       }
251*a8ab1b75SKrzysztof Parzyszek       break;
252*a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_tnew_pnt:
253*a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
254*a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
255*a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_tnew_pnt);
256*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
257*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
258*a8ab1b75SKrzysztof Parzyszek       }
259*a8ab1b75SKrzysztof Parzyszek       break;
260*a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_fnew_pnt:
261*a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
262*a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
263*a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_fnew_pnt);
264*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
265*a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
266*a8ab1b75SKrzysztof Parzyszek       }
267*a8ab1b75SKrzysztof Parzyszek       break;
268*a8ab1b75SKrzysztof Parzyszek     }
269*a8ab1b75SKrzysztof Parzyszek   }
270*a8ab1b75SKrzysztof Parzyszek }
271*a8ab1b75SKrzysztof Parzyszek 
272e4fc6ee7SEugene Zelenko static void adjustDuplex(MCInst &MI, MCContext &Context) {
273a72fad98SKrzysztof Parzyszek   switch (MI.getOpcode()) {
274a72fad98SKrzysztof Parzyszek   case Hexagon::SA1_setin1:
275a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 1,
276a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
277a72fad98SKrzysztof Parzyszek     break;
278a72fad98SKrzysztof Parzyszek   case Hexagon::SA1_dec:
279a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 2,
280a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
281a72fad98SKrzysztof Parzyszek     break;
282a72fad98SKrzysztof Parzyszek   default:
283a72fad98SKrzysztof Parzyszek     break;
2847cd08927SColin LeMahieu   }
285a72fad98SKrzysztof Parzyszek }
28668d967d9SColin LeMahieu 
28768d967d9SColin LeMahieu DecodeStatus HexagonDisassembler::getSingleInstruction(
28868d967d9SColin LeMahieu     MCInst &MI, MCInst &MCB, ArrayRef<uint8_t> Bytes, uint64_t Address,
28968d967d9SColin LeMahieu     raw_ostream &os, raw_ostream &cs, bool &Complete) const {
29068d967d9SColin LeMahieu   assert(Bytes.size() >= HEXAGON_INSTR_SIZE);
29168d967d9SColin LeMahieu 
292a72fad98SKrzysztof Parzyszek   uint32_t Instruction = support::endian::read32le(Bytes.data());
29388ebb9e2SColin LeMahieu 
29468d967d9SColin LeMahieu   auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB);
29568d967d9SColin LeMahieu   if ((Instruction & HexagonII::INST_PARSE_MASK) ==
29668d967d9SColin LeMahieu       HexagonII::INST_PARSE_LOOP_END) {
29768d967d9SColin LeMahieu     if (BundleSize == 0)
29868d967d9SColin LeMahieu       HexagonMCInstrInfo::setInnerLoop(MCB);
29968d967d9SColin LeMahieu     else if (BundleSize == 1)
30068d967d9SColin LeMahieu       HexagonMCInstrInfo::setOuterLoop(MCB);
30168d967d9SColin LeMahieu     else
30268d967d9SColin LeMahieu       return DecodeStatus::Fail;
30368d967d9SColin LeMahieu   }
30468d967d9SColin LeMahieu 
305*a8ab1b75SKrzysztof Parzyszek   CurrentExtender = HexagonMCInstrInfo::extenderForIndex(
306a72fad98SKrzysztof Parzyszek       MCB, HexagonMCInstrInfo::bundleSize(MCB));
307a72fad98SKrzysztof Parzyszek 
308a72fad98SKrzysztof Parzyszek   DecodeStatus Result = DecodeStatus::Fail;
30968d967d9SColin LeMahieu   if ((Instruction & HexagonII::INST_PARSE_MASK) ==
310be8c453dSColin LeMahieu       HexagonII::INST_PARSE_DUPLEX) {
311a72fad98SKrzysztof Parzyszek     unsigned duplexIClass;
312a72fad98SKrzysztof Parzyszek     uint8_t const *DecodeLow, *DecodeHigh;
313be8c453dSColin LeMahieu     duplexIClass = ((Instruction >> 28) & 0xe) | ((Instruction >> 13) & 0x1);
314be8c453dSColin LeMahieu     switch (duplexIClass) {
315be8c453dSColin LeMahieu     default:
316be8c453dSColin LeMahieu       return MCDisassembler::Fail;
317be8c453dSColin LeMahieu     case 0:
318a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L132;
319a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
320be8c453dSColin LeMahieu       break;
321be8c453dSColin LeMahieu     case 1:
322a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
323a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
324be8c453dSColin LeMahieu       break;
325be8c453dSColin LeMahieu     case 2:
326a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
327a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
328be8c453dSColin LeMahieu       break;
329be8c453dSColin LeMahieu     case 3:
330a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_A32;
331a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
332be8c453dSColin LeMahieu       break;
333be8c453dSColin LeMahieu     case 4:
334a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L132;
335a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
336be8c453dSColin LeMahieu       break;
337be8c453dSColin LeMahieu     case 5:
338a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
339a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
340be8c453dSColin LeMahieu       break;
341be8c453dSColin LeMahieu     case 6:
342a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
343a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
344be8c453dSColin LeMahieu       break;
345be8c453dSColin LeMahieu     case 7:
346a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
347a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
348be8c453dSColin LeMahieu       break;
349be8c453dSColin LeMahieu     case 8:
350a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
351a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
352be8c453dSColin LeMahieu       break;
353be8c453dSColin LeMahieu     case 9:
354a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
355a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
356be8c453dSColin LeMahieu       break;
357be8c453dSColin LeMahieu     case 10:
358a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
359a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S132;
360be8c453dSColin LeMahieu       break;
361be8c453dSColin LeMahieu     case 11:
362a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
363a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S132;
364be8c453dSColin LeMahieu       break;
365be8c453dSColin LeMahieu     case 12:
366a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
367a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
368be8c453dSColin LeMahieu       break;
369be8c453dSColin LeMahieu     case 13:
370a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
371a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
372be8c453dSColin LeMahieu       break;
373be8c453dSColin LeMahieu     case 14:
374a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
375a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S232;
376be8c453dSColin LeMahieu       break;
377be8c453dSColin LeMahieu     }
378a72fad98SKrzysztof Parzyszek     MI.setOpcode(Hexagon::DuplexIClass0 + duplexIClass);
379be8c453dSColin LeMahieu     MCInst *MILow = new (getContext()) MCInst;
380be8c453dSColin LeMahieu     MCInst *MIHigh = new (getContext()) MCInst;
381*a8ab1b75SKrzysztof Parzyszek     auto TmpExtender = CurrentExtender;
382*a8ab1b75SKrzysztof Parzyszek     CurrentExtender =
383*a8ab1b75SKrzysztof Parzyszek         nullptr; // constant extenders in duplex must always be in slot 1
384a72fad98SKrzysztof Parzyszek     Result = decodeInstruction(DecodeLow, *MILow, Instruction & 0x1fff, Address,
385a72fad98SKrzysztof Parzyszek                                this, STI);
386*a8ab1b75SKrzysztof Parzyszek     CurrentExtender = TmpExtender;
387a72fad98SKrzysztof Parzyszek     if (Result != DecodeStatus::Success)
388a72fad98SKrzysztof Parzyszek       return DecodeStatus::Fail;
389a72fad98SKrzysztof Parzyszek     adjustDuplex(*MILow, getContext());
390a72fad98SKrzysztof Parzyszek     Result = decodeInstruction(
391a72fad98SKrzysztof Parzyszek         DecodeHigh, *MIHigh, (Instruction >> 16) & 0x1fff, Address, this, STI);
392a72fad98SKrzysztof Parzyszek     if (Result != DecodeStatus::Success)
393a72fad98SKrzysztof Parzyszek       return DecodeStatus::Fail;
394a72fad98SKrzysztof Parzyszek     adjustDuplex(*MIHigh, getContext());
395be8c453dSColin LeMahieu     MCOperand OPLow = MCOperand::createInst(MILow);
396be8c453dSColin LeMahieu     MCOperand OPHigh = MCOperand::createInst(MIHigh);
397be8c453dSColin LeMahieu     MI.addOperand(OPLow);
398be8c453dSColin LeMahieu     MI.addOperand(OPHigh);
399be8c453dSColin LeMahieu     Complete = true;
400be8c453dSColin LeMahieu   } else {
401be8c453dSColin LeMahieu     if ((Instruction & HexagonII::INST_PARSE_MASK) ==
40268d967d9SColin LeMahieu         HexagonII::INST_PARSE_PACKET_END)
40368d967d9SColin LeMahieu       Complete = true;
4047cd08927SColin LeMahieu 
405*a8ab1b75SKrzysztof Parzyszek     if (CurrentExtender != nullptr)
406a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTableMustExtend32, MI, Instruction,
407a72fad98SKrzysztof Parzyszek                                  Address, this, STI);
408a72fad98SKrzysztof Parzyszek 
409a72fad98SKrzysztof Parzyszek     if (Result != MCDisassembler::Success)
410a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTable32, MI, Instruction, Address, this,
411a72fad98SKrzysztof Parzyszek                                  STI);
412a72fad98SKrzysztof Parzyszek 
413a72fad98SKrzysztof Parzyszek     if (Result != MCDisassembler::Success &&
414a72fad98SKrzysztof Parzyszek         STI.getFeatureBits()[Hexagon::ExtensionHVX])
415a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTableEXT_mmvec32, MI, Instruction,
416a72fad98SKrzysztof Parzyszek                                  Address, this, STI);
417a72fad98SKrzysztof Parzyszek 
418be8c453dSColin LeMahieu   }
41968d967d9SColin LeMahieu 
42081707549SColin LeMahieu   switch (MI.getOpcode()) {
42181707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_f_jumpnv_nt:
42281707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_f_jumpnv_t:
42381707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp0_jump_nt:
42481707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp0_jump_t:
42581707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp1_jump_nt:
42681707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp1_jump_t:
42781707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_t_jumpnv_nt:
42881707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_t_jumpnv_t:
42981707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp0_jump_nt:
43081707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp0_jump_t:
43181707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp1_jump_nt:
43281707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp1_jump_t:
43381707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_f_jumpnv_nt:
43481707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_f_jumpnv_t:
43581707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp0_jump_nt:
43681707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp0_jump_t:
43781707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp1_jump_nt:
43881707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp1_jump_t:
43981707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_t_jumpnv_nt:
44081707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_t_jumpnv_t:
44181707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp0_jump_nt:
44281707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp0_jump_t:
44381707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp1_jump_nt:
44481707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp1_jump_t:
445a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 1,
446a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, getContext())));
44781707549SColin LeMahieu     break;
44881707549SColin LeMahieu   default:
44981707549SColin LeMahieu     break;
45081707549SColin LeMahieu   }
45181707549SColin LeMahieu 
4527cd08927SColin LeMahieu   if (HexagonMCInstrInfo::isNewValue(*MCII, MI)) {
4537cd08927SColin LeMahieu     unsigned OpIndex = HexagonMCInstrInfo::getNewValueOp(*MCII, MI);
4547cd08927SColin LeMahieu     MCOperand &MCO = MI.getOperand(OpIndex);
4557cd08927SColin LeMahieu     assert(MCO.isReg() && "New value consumers must be registers");
4567cd08927SColin LeMahieu     unsigned Register =
4577cd08927SColin LeMahieu         getContext().getRegisterInfo()->getEncodingValue(MCO.getReg());
4587cd08927SColin LeMahieu     if ((Register & 0x6) == 0)
4597cd08927SColin LeMahieu       // HexagonPRM 10.11 Bit 1-2 == 0 is reserved
4607cd08927SColin LeMahieu       return MCDisassembler::Fail;
4617cd08927SColin LeMahieu     unsigned Lookback = (Register & 0x6) >> 1;
4627cd08927SColin LeMahieu     unsigned Offset = 1;
4637cd08927SColin LeMahieu     bool Vector = HexagonMCInstrInfo::isVector(*MCII, MI);
464*a8ab1b75SKrzysztof Parzyszek     bool PrevVector = false;
4657cd08927SColin LeMahieu     auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
4667cd08927SColin LeMahieu     auto i = Instructions.end() - 1;
4677cd08927SColin LeMahieu     for (auto n = Instructions.begin() - 1;; --i, ++Offset) {
4687cd08927SColin LeMahieu       if (i == n)
4697cd08927SColin LeMahieu         // Couldn't find producer
4707cd08927SColin LeMahieu         return MCDisassembler::Fail;
471*a8ab1b75SKrzysztof Parzyszek       bool CurrentVector = HexagonMCInstrInfo::isVector(*MCII, *i->getInst());
472*a8ab1b75SKrzysztof Parzyszek       if (Vector && !CurrentVector)
4737cd08927SColin LeMahieu         // Skip scalars when calculating distances for vectors
4747cd08927SColin LeMahieu         ++Lookback;
475*a8ab1b75SKrzysztof Parzyszek       if (HexagonMCInstrInfo::isImmext(*i->getInst()) && (Vector == PrevVector))
4767cd08927SColin LeMahieu         ++Lookback;
477*a8ab1b75SKrzysztof Parzyszek       PrevVector = CurrentVector;
4787cd08927SColin LeMahieu       if (Offset == Lookback)
4797cd08927SColin LeMahieu         break;
4807cd08927SColin LeMahieu     }
4817cd08927SColin LeMahieu     auto const &Inst = *i->getInst();
4827cd08927SColin LeMahieu     bool SubregBit = (Register & 0x1) != 0;
483*a8ab1b75SKrzysztof Parzyszek     if (HexagonMCInstrInfo::hasNewValue2(*MCII, Inst)) {
4847cd08927SColin LeMahieu       // If subreg bit is set we're selecting the second produced newvalue
485*a8ab1b75SKrzysztof Parzyszek       unsigned Producer = SubregBit ?
486*a8ab1b75SKrzysztof Parzyszek           HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg() :
4877cd08927SColin LeMahieu           HexagonMCInstrInfo::getNewValueOperand2(*MCII, Inst).getReg();
4887cd08927SColin LeMahieu       assert(Producer != Hexagon::NoRegister);
4897cd08927SColin LeMahieu       MCO.setReg(Producer);
4907cd08927SColin LeMahieu     } else if (HexagonMCInstrInfo::hasNewValue(*MCII, Inst)) {
4917cd08927SColin LeMahieu       unsigned Producer =
4927cd08927SColin LeMahieu           HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg();
4937cd08927SColin LeMahieu       if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
4947cd08927SColin LeMahieu         Producer = ((Producer - Hexagon::W0) << 1) + SubregBit + Hexagon::V0;
4957cd08927SColin LeMahieu       else if (SubregBit)
4966fdfa3dcSColin LeMahieu         // Hexagon PRM 10.11 New-value operands
4976fdfa3dcSColin LeMahieu         // Nt[0] is reserved and should always be encoded as zero.
4987cd08927SColin LeMahieu         return MCDisassembler::Fail;
4997cd08927SColin LeMahieu       assert(Producer != Hexagon::NoRegister);
5007cd08927SColin LeMahieu       MCO.setReg(Producer);
5017cd08927SColin LeMahieu     } else
5027cd08927SColin LeMahieu       return MCDisassembler::Fail;
5037cd08927SColin LeMahieu   }
5047cd08927SColin LeMahieu 
505*a8ab1b75SKrzysztof Parzyszek   if (CurrentExtender != nullptr) {
506a72fad98SKrzysztof Parzyszek     MCInst const &Inst = HexagonMCInstrInfo::isDuplex(*MCII, MI)
507a72fad98SKrzysztof Parzyszek                              ? *MI.getOperand(1).getInst()
508a72fad98SKrzysztof Parzyszek                              : MI;
5097cd08927SColin LeMahieu     if (!HexagonMCInstrInfo::isExtendable(*MCII, Inst) &&
5107cd08927SColin LeMahieu         !HexagonMCInstrInfo::isExtended(*MCII, Inst))
5117cd08927SColin LeMahieu       return MCDisassembler::Fail;
5127cd08927SColin LeMahieu   }
5135d6f03bdSColin LeMahieu   return Result;
51488ebb9e2SColin LeMahieu }
5157cd08927SColin LeMahieu 
5167cd08927SColin LeMahieu static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,
517e5e035a3SCraig Topper                                         ArrayRef<MCPhysReg> Table) {
5183da000c0SCraig Topper   if (RegNo < Table.size()) {
5197cd08927SColin LeMahieu     Inst.addOperand(MCOperand::createReg(Table[RegNo]));
5207cd08927SColin LeMahieu     return MCDisassembler::Success;
5213da000c0SCraig Topper   }
5223da000c0SCraig Topper 
5237cd08927SColin LeMahieu   return MCDisassembler::Fail;
5247cd08927SColin LeMahieu }
5257cd08927SColin LeMahieu 
5267c958713SColin LeMahieu static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
5277c958713SColin LeMahieu                                                    uint64_t Address,
5287c958713SColin LeMahieu                                                    const void *Decoder) {
5297c958713SColin LeMahieu   return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
5307c958713SColin LeMahieu }
5317c958713SColin LeMahieu 
5327cd08927SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5337cd08927SColin LeMahieu                                                uint64_t Address,
5347cd08927SColin LeMahieu                                                const void *Decoder) {
535e5e035a3SCraig Topper   static const MCPhysReg IntRegDecoderTable[] = {
5367cd08927SColin LeMahieu       Hexagon::R0,  Hexagon::R1,  Hexagon::R2,  Hexagon::R3,  Hexagon::R4,
5377cd08927SColin LeMahieu       Hexagon::R5,  Hexagon::R6,  Hexagon::R7,  Hexagon::R8,  Hexagon::R9,
5387cd08927SColin LeMahieu       Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
5397cd08927SColin LeMahieu       Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
5407cd08927SColin LeMahieu       Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
5417cd08927SColin LeMahieu       Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
5427cd08927SColin LeMahieu       Hexagon::R30, Hexagon::R31};
5437cd08927SColin LeMahieu 
544e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, IntRegDecoderTable);
5457cd08927SColin LeMahieu }
5467c958713SColin LeMahieu 
547a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
548a72fad98SKrzysztof Parzyszek                                                       unsigned RegNo,
549a72fad98SKrzysztof Parzyszek                                                       uint64_t Address,
550a72fad98SKrzysztof Parzyszek                                                       const void *Decoder) {
551a72fad98SKrzysztof Parzyszek   static const MCPhysReg GeneralSubRegDecoderTable[] = {
552a72fad98SKrzysztof Parzyszek       Hexagon::R0,  Hexagon::R1,  Hexagon::R2,  Hexagon::R3,
553a72fad98SKrzysztof Parzyszek       Hexagon::R4,  Hexagon::R5,  Hexagon::R6,  Hexagon::R7,
554a72fad98SKrzysztof Parzyszek       Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
555a72fad98SKrzysztof Parzyszek       Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23,
556a72fad98SKrzysztof Parzyszek   };
557a72fad98SKrzysztof Parzyszek 
558a72fad98SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, GeneralSubRegDecoderTable);
559a72fad98SKrzysztof Parzyszek }
560a72fad98SKrzysztof Parzyszek 
56155772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,
5627c958713SColin LeMahieu                                              uint64_t /*Address*/,
5637c958713SColin LeMahieu                                              const void *Decoder) {
56455772976SKrzysztof Parzyszek   static const MCPhysReg HvxVRDecoderTable[] = {
5657c958713SColin LeMahieu       Hexagon::V0,  Hexagon::V1,  Hexagon::V2,  Hexagon::V3,  Hexagon::V4,
5667c958713SColin LeMahieu       Hexagon::V5,  Hexagon::V6,  Hexagon::V7,  Hexagon::V8,  Hexagon::V9,
5677c958713SColin LeMahieu       Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
5687c958713SColin LeMahieu       Hexagon::V15, Hexagon::V16, Hexagon::V17, Hexagon::V18, Hexagon::V19,
5697c958713SColin LeMahieu       Hexagon::V20, Hexagon::V21, Hexagon::V22, Hexagon::V23, Hexagon::V24,
5707c958713SColin LeMahieu       Hexagon::V25, Hexagon::V26, Hexagon::V27, Hexagon::V28, Hexagon::V29,
5717c958713SColin LeMahieu       Hexagon::V30, Hexagon::V31};
5727c958713SColin LeMahieu 
57355772976SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, HvxVRDecoderTable);
5747c958713SColin LeMahieu }
5757c958713SColin LeMahieu 
5767cd08927SColin LeMahieu static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5777cd08927SColin LeMahieu                                                   uint64_t /*Address*/,
5787cd08927SColin LeMahieu                                                   const void *Decoder) {
579e5e035a3SCraig Topper   static const MCPhysReg DoubleRegDecoderTable[] = {
5807cd08927SColin LeMahieu       Hexagon::D0,  Hexagon::D1,  Hexagon::D2,  Hexagon::D3,
5817cd08927SColin LeMahieu       Hexagon::D4,  Hexagon::D5,  Hexagon::D6,  Hexagon::D7,
5827cd08927SColin LeMahieu       Hexagon::D8,  Hexagon::D9,  Hexagon::D10, Hexagon::D11,
5837cd08927SColin LeMahieu       Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15};
5847cd08927SColin LeMahieu 
585e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo >> 1, DoubleRegDecoderTable);
5867cd08927SColin LeMahieu }
5877cd08927SColin LeMahieu 
588a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralDoubleLow8RegsRegisterClass(
589a72fad98SKrzysztof Parzyszek     MCInst &Inst, unsigned RegNo, uint64_t /*Address*/, const void *Decoder) {
590a72fad98SKrzysztof Parzyszek   static const MCPhysReg GeneralDoubleLow8RegDecoderTable[] = {
591a72fad98SKrzysztof Parzyszek       Hexagon::D0, Hexagon::D1, Hexagon::D2,  Hexagon::D3,
592a72fad98SKrzysztof Parzyszek       Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11};
593a72fad98SKrzysztof Parzyszek 
594a72fad98SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, GeneralDoubleLow8RegDecoderTable);
595a72fad98SKrzysztof Parzyszek }
596a72fad98SKrzysztof Parzyszek 
59755772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,
5987c958713SColin LeMahieu                                              uint64_t /*Address*/,
5997c958713SColin LeMahieu                                              const void *Decoder) {
60055772976SKrzysztof Parzyszek   static const MCPhysReg HvxWRDecoderTable[] = {
6017c958713SColin LeMahieu       Hexagon::W0,  Hexagon::W1,  Hexagon::W2,  Hexagon::W3,
6027c958713SColin LeMahieu       Hexagon::W4,  Hexagon::W5,  Hexagon::W6,  Hexagon::W7,
6037c958713SColin LeMahieu       Hexagon::W8,  Hexagon::W9,  Hexagon::W10, Hexagon::W11,
6047c958713SColin LeMahieu       Hexagon::W12, Hexagon::W13, Hexagon::W14, Hexagon::W15};
6057c958713SColin LeMahieu 
60655772976SKrzysztof Parzyszek   return (DecodeRegisterClass(Inst, RegNo >> 1, HvxWRDecoderTable));
6077c958713SColin LeMahieu }
6087c958713SColin LeMahieu 
6097cd08927SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
6107cd08927SColin LeMahieu                                                 uint64_t /*Address*/,
6117cd08927SColin LeMahieu                                                 const void *Decoder) {
612e5e035a3SCraig Topper   static const MCPhysReg PredRegDecoderTable[] = {Hexagon::P0, Hexagon::P1,
6137cd08927SColin LeMahieu                                                   Hexagon::P2, Hexagon::P3};
6147cd08927SColin LeMahieu 
615e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, PredRegDecoderTable);
6167cd08927SColin LeMahieu }
6177cd08927SColin LeMahieu 
61855772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,
6197c958713SColin LeMahieu                                              uint64_t /*Address*/,
6207c958713SColin LeMahieu                                              const void *Decoder) {
62155772976SKrzysztof Parzyszek   static const MCPhysReg HvxQRDecoderTable[] = {Hexagon::Q0, Hexagon::Q1,
6227c958713SColin LeMahieu                                                 Hexagon::Q2, Hexagon::Q3};
6237c958713SColin LeMahieu 
62455772976SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, HvxQRDecoderTable);
6257c958713SColin LeMahieu }
626be8c453dSColin LeMahieu 
6277cd08927SColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
6287cd08927SColin LeMahieu                                                uint64_t /*Address*/,
6297cd08927SColin LeMahieu                                                const void *Decoder) {
630f9015e62SKrzysztof Parzyszek   using namespace Hexagon;
631e4fc6ee7SEugene Zelenko 
632e5e035a3SCraig Topper   static const MCPhysReg CtrlRegDecoderTable[] = {
633f9015e62SKrzysztof Parzyszek     /*  0 */  SA0,        LC0,        SA1,        LC1,
634e2603328SKrzysztof Parzyszek     /*  4 */  P3_0,       C5,         M0,         M1,
635f9015e62SKrzysztof Parzyszek     /*  8 */  USR,        PC,         UGP,        GP,
636ab57c2baSKrzysztof Parzyszek     /* 12 */  CS0,        CS1,        UPCYCLELO,  UPCYCLEHI,
637f9015e62SKrzysztof Parzyszek     /* 16 */  FRAMELIMIT, FRAMEKEY,   PKTCOUNTLO, PKTCOUNTHI,
638f9015e62SKrzysztof Parzyszek     /* 20 */  0,          0,          0,          0,
639f9015e62SKrzysztof Parzyszek     /* 24 */  0,          0,          0,          0,
640f9015e62SKrzysztof Parzyszek     /* 28 */  0,          0,          UTIMERLO,   UTIMERHI
6417cd08927SColin LeMahieu   };
6427cd08927SColin LeMahieu 
6436261e1b9SCraig Topper   if (RegNo >= array_lengthof(CtrlRegDecoderTable))
6447cd08927SColin LeMahieu     return MCDisassembler::Fail;
6457cd08927SColin LeMahieu 
646f9015e62SKrzysztof Parzyszek   static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
647f9015e62SKrzysztof Parzyszek   if (CtrlRegDecoderTable[RegNo] == NoRegister)
6487cd08927SColin LeMahieu     return MCDisassembler::Fail;
6497cd08927SColin LeMahieu 
6507cd08927SColin LeMahieu   unsigned Register = CtrlRegDecoderTable[RegNo];
6517cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
6527cd08927SColin LeMahieu   return MCDisassembler::Success;
6537cd08927SColin LeMahieu }
6547cd08927SColin LeMahieu 
6557cd08927SColin LeMahieu static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
6567cd08927SColin LeMahieu                                                  uint64_t /*Address*/,
6577cd08927SColin LeMahieu                                                  const void *Decoder) {
658f9015e62SKrzysztof Parzyszek   using namespace Hexagon;
659e4fc6ee7SEugene Zelenko 
660e5e035a3SCraig Topper   static const MCPhysReg CtrlReg64DecoderTable[] = {
661f9015e62SKrzysztof Parzyszek     /*  0 */  C1_0,       0,          C3_2,       0,
662f9015e62SKrzysztof Parzyszek     /*  4 */  C5_4,       0,          C7_6,       0,
663f9015e62SKrzysztof Parzyszek     /*  8 */  C9_8,       0,          C11_10,     0,
664ab57c2baSKrzysztof Parzyszek     /* 12 */  CS,         0,          UPCYCLE,    0,
665f9015e62SKrzysztof Parzyszek     /* 16 */  C17_16,     0,          PKTCOUNT,   0,
666f9015e62SKrzysztof Parzyszek     /* 20 */  0,          0,          0,          0,
667f9015e62SKrzysztof Parzyszek     /* 24 */  0,          0,          0,          0,
668f9015e62SKrzysztof Parzyszek     /* 28 */  0,          0,          UTIMER,     0
6697cd08927SColin LeMahieu   };
6707cd08927SColin LeMahieu 
6716261e1b9SCraig Topper   if (RegNo >= array_lengthof(CtrlReg64DecoderTable))
6727cd08927SColin LeMahieu     return MCDisassembler::Fail;
6737cd08927SColin LeMahieu 
674f9015e62SKrzysztof Parzyszek   static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
675f9015e62SKrzysztof Parzyszek   if (CtrlReg64DecoderTable[RegNo] == NoRegister)
6767cd08927SColin LeMahieu     return MCDisassembler::Fail;
6777cd08927SColin LeMahieu 
6787cd08927SColin LeMahieu   unsigned Register = CtrlReg64DecoderTable[RegNo];
6797cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
6807cd08927SColin LeMahieu   return MCDisassembler::Success;
6817cd08927SColin LeMahieu }
6827cd08927SColin LeMahieu 
6837cd08927SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
6847cd08927SColin LeMahieu                                                uint64_t /*Address*/,
6857cd08927SColin LeMahieu                                                const void *Decoder) {
6867cd08927SColin LeMahieu   unsigned Register = 0;
6877cd08927SColin LeMahieu   switch (RegNo) {
6887cd08927SColin LeMahieu   case 0:
6897cd08927SColin LeMahieu     Register = Hexagon::M0;
6907cd08927SColin LeMahieu     break;
6917cd08927SColin LeMahieu   case 1:
6927cd08927SColin LeMahieu     Register = Hexagon::M1;
6937cd08927SColin LeMahieu     break;
6947cd08927SColin LeMahieu   default:
6957cd08927SColin LeMahieu     return MCDisassembler::Fail;
6967cd08927SColin LeMahieu   }
6977cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
6987cd08927SColin LeMahieu   return MCDisassembler::Success;
6997cd08927SColin LeMahieu }
7007cd08927SColin LeMahieu 
7017cd08927SColin LeMahieu static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
7027cd08927SColin LeMahieu                                        uint64_t /*Address*/,
7037cd08927SColin LeMahieu                                        const void *Decoder) {
7047cd08927SColin LeMahieu   HexagonDisassembler const &Disassembler = disassembler(Decoder);
705*a8ab1b75SKrzysztof Parzyszek   int64_t FullValue = fullValue(Disassembler, MI, tmp);
7067cd08927SColin LeMahieu   assert(FullValue >= 0 && "Negative in unsigned decoder");
7077cd08927SColin LeMahieu   HexagonMCInstrInfo::addConstant(MI, FullValue, Disassembler.getContext());
7087cd08927SColin LeMahieu   return MCDisassembler::Success;
7097cd08927SColin LeMahieu }
7107cd08927SColin LeMahieu 
711a72fad98SKrzysztof Parzyszek static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
712a72fad98SKrzysztof Parzyszek                                     uint64_t /*Address*/, const void *Decoder) {
713a72fad98SKrzysztof Parzyszek   HexagonDisassembler const &Disassembler = disassembler(Decoder);
714a72fad98SKrzysztof Parzyszek   unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
715a72fad98SKrzysztof Parzyszek   tmp = SignExtend64(tmp, Bits);
716a72fad98SKrzysztof Parzyszek   signedDecoder<32>(MI, tmp, Decoder);
717a72fad98SKrzysztof Parzyszek   return MCDisassembler::Success;
718a72fad98SKrzysztof Parzyszek }
719a72fad98SKrzysztof Parzyszek 
7207cd08927SColin LeMahieu // custom decoder for various jump/call immediates
7217cd08927SColin LeMahieu static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
7227cd08927SColin LeMahieu                                     const void *Decoder) {
7237cd08927SColin LeMahieu   HexagonDisassembler const &Disassembler = disassembler(Decoder);
7247cd08927SColin LeMahieu   unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
7257cd08927SColin LeMahieu   // r13_2 is not extendable, so if there are no extent bits, it's r13_2
7267cd08927SColin LeMahieu   if (Bits == 0)
7277cd08927SColin LeMahieu     Bits = 15;
728*a8ab1b75SKrzysztof Parzyszek   uint64_t FullValue = fullValue(Disassembler, MI, SignExtend64(tmp, Bits));
729*a8ab1b75SKrzysztof Parzyszek   uint32_t Extended = FullValue + Address;
730a72fad98SKrzysztof Parzyszek   if (!Disassembler.tryAddingSymbolicOperand(MI, Extended, Address, true, 0, 4))
7317cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
7327cd08927SColin LeMahieu   return MCDisassembler::Success;
7337cd08927SColin LeMahieu }
734