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;
48a8ab1b75SKrzysztof Parzyszek   mutable MCInst const *CurrentExtender;
4982085927SEugene Zelenko 
507cd08927SColin LeMahieu   HexagonDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
517cd08927SColin LeMahieu                       MCInstrInfo const *MCII)
52a8ab1b75SKrzysztof Parzyszek       : MCDisassembler(STI, Ctx), MCII(MCII), CurrentBundle(new MCInst *),
53a8ab1b75SKrzysztof Parzyszek         CurrentExtender(nullptr) {}
5488ebb9e2SColin LeMahieu 
5568d967d9SColin LeMahieu   DecodeStatus getSingleInstruction(MCInst &Instr, MCInst &MCB,
5668d967d9SColin LeMahieu                                     ArrayRef<uint8_t> Bytes, uint64_t Address,
5768d967d9SColin LeMahieu                                     raw_ostream &VStream, raw_ostream &CStream,
5868d967d9SColin LeMahieu                                     bool &Complete) const;
594aa6bea7SRafael Espindola   DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
607fc5b874SRafael Espindola                               ArrayRef<uint8_t> Bytes, uint64_t Address,
614aa6bea7SRafael Espindola                               raw_ostream &VStream,
624aa6bea7SRafael Espindola                               raw_ostream &CStream) const override;
63a8ab1b75SKrzysztof Parzyszek   void remapInstruction(MCInst &Instr) const;
6488ebb9e2SColin LeMahieu };
6582085927SEugene Zelenko 
66a8ab1b75SKrzysztof Parzyszek static uint64_t fullValue(HexagonDisassembler const &Disassembler, MCInst &MI,
67a72fad98SKrzysztof Parzyszek                           int64_t Value) {
68a8ab1b75SKrzysztof Parzyszek   MCInstrInfo MCII = *Disassembler.MCII;
69a8ab1b75SKrzysztof Parzyszek   if (!Disassembler.CurrentExtender ||
70a8ab1b75SKrzysztof Parzyszek       MI.size() != HexagonMCInstrInfo::getExtendableOp(MCII, MI))
71a72fad98SKrzysztof Parzyszek     return Value;
72a72fad98SKrzysztof Parzyszek   unsigned Alignment = HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
73a72fad98SKrzysztof Parzyszek   uint32_t Lower6 = static_cast<uint32_t>(Value >> Alignment) & 0x3f;
74a72fad98SKrzysztof Parzyszek   int64_t Bits;
75a8ab1b75SKrzysztof Parzyszek   bool Success =
76a8ab1b75SKrzysztof Parzyszek       Disassembler.CurrentExtender->getOperand(0).getExpr()->evaluateAsAbsolute(
77a8ab1b75SKrzysztof Parzyszek           Bits);
78a8ab1b75SKrzysztof Parzyszek   assert(Success);
79a8ab1b75SKrzysztof Parzyszek   (void)Success;
80a8ab1b75SKrzysztof Parzyszek   uint64_t Upper26 = static_cast<uint64_t>(Bits);
81a8ab1b75SKrzysztof Parzyszek   uint64_t Operand = Upper26 | Lower6;
82a72fad98SKrzysztof Parzyszek   return Operand;
83a72fad98SKrzysztof Parzyszek }
84e4fc6ee7SEugene Zelenko static HexagonDisassembler const &disassembler(void const *Decoder) {
85a72fad98SKrzysztof Parzyszek   return *static_cast<HexagonDisassembler const *>(Decoder);
86a72fad98SKrzysztof Parzyszek }
87a72fad98SKrzysztof Parzyszek template <size_t T>
88e4fc6ee7SEugene Zelenko static void signedDecoder(MCInst &MI, unsigned tmp, const void *Decoder) {
89a72fad98SKrzysztof Parzyszek   HexagonDisassembler const &Disassembler = disassembler(Decoder);
90a8ab1b75SKrzysztof Parzyszek   int64_t FullValue = fullValue(Disassembler, MI, SignExtend64<T>(tmp));
91a72fad98SKrzysztof Parzyszek   int64_t Extended = SignExtend64<32>(FullValue);
92a72fad98SKrzysztof Parzyszek   HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
93a72fad98SKrzysztof Parzyszek }
94a8ab1b75SKrzysztof Parzyszek }
9588ebb9e2SColin LeMahieu 
967cd08927SColin LeMahieu // Forward declare these because the auto-generated code will reference them.
977cd08927SColin LeMahieu // Definitions are further down.
987cd08927SColin LeMahieu 
997cd08927SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
100be8c453dSColin LeMahieu                                                uint64_t Address,
101be8c453dSColin LeMahieu                                                const void *Decoder);
102a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
103a72fad98SKrzysztof Parzyszek                                                       unsigned RegNo,
104a72fad98SKrzysztof Parzyszek                                                       uint64_t Address,
105a72fad98SKrzysztof Parzyszek                                                       const void *Decoder);
1067c958713SColin LeMahieu static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
1077c958713SColin LeMahieu                                                    uint64_t Address,
1087c958713SColin LeMahieu                                                    const void *Decoder);
10955772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,
1107c958713SColin LeMahieu                                              uint64_t Address,
1117c958713SColin LeMahieu                                              const void *Decoder);
1127cd08927SColin LeMahieu static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1137cd08927SColin LeMahieu                                                   uint64_t Address,
1147cd08927SColin LeMahieu                                                   const void *Decoder);
115a72fad98SKrzysztof Parzyszek static DecodeStatus
116a72fad98SKrzysztof Parzyszek DecodeGeneralDoubleLow8RegsRegisterClass(MCInst &Inst, unsigned RegNo,
117a72fad98SKrzysztof Parzyszek                                          uint64_t Address, const void *Decoder);
11855772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,
1197c958713SColin LeMahieu                                              uint64_t Address,
1207c958713SColin LeMahieu                                              const void *Decoder);
1217cd08927SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1227cd08927SColin LeMahieu                                                 uint64_t Address,
1237cd08927SColin LeMahieu                                                 const void *Decoder);
12455772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,
1257c958713SColin LeMahieu                                              uint64_t Address,
1267c958713SColin LeMahieu                                              const void *Decoder);
127f3db884eSColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
128be8c453dSColin LeMahieu                                                uint64_t Address,
129be8c453dSColin LeMahieu                                                const void *Decoder);
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*f157ad44SDavid Blaikie static DecodeStatus s4_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
145*f157ad44SDavid Blaikie                                    const void *Decoder) {
146*f157ad44SDavid Blaikie   signedDecoder<4>(MI, tmp, Decoder);
147*f157ad44SDavid Blaikie   return MCDisassembler::Success;
148*f157ad44SDavid Blaikie }
149*f157ad44SDavid Blaikie static DecodeStatus s29_3ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
150*f157ad44SDavid Blaikie                                     const void *Decoder) {
151*f157ad44SDavid Blaikie   signedDecoder<14>(MI, tmp, Decoder);
152*f157ad44SDavid Blaikie   return MCDisassembler::Success;
153*f157ad44SDavid Blaikie }
154*f157ad44SDavid Blaikie static DecodeStatus s8_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
155*f157ad44SDavid Blaikie                                    const void *Decoder) {
156*f157ad44SDavid Blaikie   signedDecoder<8>(MI, tmp, Decoder);
157*f157ad44SDavid Blaikie   return MCDisassembler::Success;
158*f157ad44SDavid Blaikie }
159*f157ad44SDavid Blaikie static DecodeStatus s4_3ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
160*f157ad44SDavid Blaikie                                    const void *Decoder) {
161*f157ad44SDavid Blaikie   signedDecoder<7>(MI, tmp, Decoder);
162*f157ad44SDavid Blaikie   return MCDisassembler::Success;
163*f157ad44SDavid Blaikie }
164*f157ad44SDavid Blaikie static DecodeStatus s31_1ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
165*f157ad44SDavid Blaikie                                     const void *Decoder) {
166*f157ad44SDavid Blaikie   signedDecoder<12>(MI, tmp, Decoder);
167*f157ad44SDavid Blaikie   return MCDisassembler::Success;
168*f157ad44SDavid Blaikie }
169*f157ad44SDavid Blaikie static DecodeStatus s3_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
170*f157ad44SDavid Blaikie                                    const void *Decoder) {
171*f157ad44SDavid Blaikie   signedDecoder<3>(MI, tmp, Decoder);
172*f157ad44SDavid Blaikie   return MCDisassembler::Success;
173*f157ad44SDavid Blaikie }
174*f157ad44SDavid Blaikie static DecodeStatus s30_2ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
175*f157ad44SDavid Blaikie                                     const void *Decoder) {
176*f157ad44SDavid Blaikie   signedDecoder<13>(MI, tmp, Decoder);
177*f157ad44SDavid Blaikie   return MCDisassembler::Success;
178*f157ad44SDavid Blaikie }
179*f157ad44SDavid Blaikie static DecodeStatus s6_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
180*f157ad44SDavid Blaikie                                    const void *Decoder) {
181*f157ad44SDavid Blaikie   signedDecoder<6>(MI, tmp, Decoder);
182*f157ad44SDavid Blaikie   return MCDisassembler::Success;
183*f157ad44SDavid Blaikie }
184*f157ad44SDavid Blaikie static DecodeStatus s6_3ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
185*f157ad44SDavid Blaikie                                    const void *Decoder) {
186*f157ad44SDavid Blaikie   signedDecoder<9>(MI, tmp, Decoder);
187*f157ad44SDavid Blaikie   return MCDisassembler::Success;
188*f157ad44SDavid Blaikie }
189*f157ad44SDavid Blaikie static DecodeStatus s4_1ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
190*f157ad44SDavid Blaikie                                    const void *Decoder) {
191*f157ad44SDavid Blaikie   signedDecoder<5>(MI, tmp, Decoder);
192*f157ad44SDavid Blaikie   return MCDisassembler::Success;
193*f157ad44SDavid Blaikie }
194*f157ad44SDavid Blaikie static DecodeStatus s4_2ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
195*f157ad44SDavid Blaikie                                    const void *Decoder) {
196*f157ad44SDavid Blaikie   signedDecoder<6>(MI, tmp, Decoder);
197*f157ad44SDavid Blaikie   return MCDisassembler::Success;
198*f157ad44SDavid Blaikie }
19988ebb9e2SColin LeMahieu #include "HexagonGenDisassemblerTables.inc"
20088ebb9e2SColin LeMahieu 
2017cd08927SColin LeMahieu static MCDisassembler *createHexagonDisassembler(const Target &T,
2027cd08927SColin LeMahieu                                                  const MCSubtargetInfo &STI,
20388ebb9e2SColin LeMahieu                                                  MCContext &Ctx) {
2047cd08927SColin LeMahieu   return new HexagonDisassembler(STI, Ctx, T.createMCInstrInfo());
20588ebb9e2SColin LeMahieu }
20688ebb9e2SColin LeMahieu 
20788ebb9e2SColin LeMahieu extern "C" void LLVMInitializeHexagonDisassembler() {
208f42454b9SMehdi Amini   TargetRegistry::RegisterMCDisassembler(getTheHexagonTarget(),
20988ebb9e2SColin LeMahieu                                          createHexagonDisassembler);
21088ebb9e2SColin LeMahieu }
21188ebb9e2SColin LeMahieu 
21288ebb9e2SColin LeMahieu DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
2137fc5b874SRafael Espindola                                                  ArrayRef<uint8_t> Bytes,
21488ebb9e2SColin LeMahieu                                                  uint64_t Address,
21588ebb9e2SColin LeMahieu                                                  raw_ostream &os,
21688ebb9e2SColin LeMahieu                                                  raw_ostream &cs) const {
21768d967d9SColin LeMahieu   DecodeStatus Result = DecodeStatus::Success;
21868d967d9SColin LeMahieu   bool Complete = false;
21968d967d9SColin LeMahieu   Size = 0;
2204aa6bea7SRafael Espindola 
22168d967d9SColin LeMahieu   *CurrentBundle = &MI;
222a8ab1b75SKrzysztof Parzyszek   MI.setOpcode(Hexagon::BUNDLE);
223a8ab1b75SKrzysztof Parzyszek   MI.addOperand(MCOperand::createImm(0));
22482085927SEugene Zelenko   while (Result == Success && !Complete) {
22568d967d9SColin LeMahieu     if (Bytes.size() < HEXAGON_INSTR_SIZE)
22668d967d9SColin LeMahieu       return MCDisassembler::Fail;
22768d967d9SColin LeMahieu     MCInst *Inst = new (getContext()) MCInst;
22868d967d9SColin LeMahieu     Result = getSingleInstruction(*Inst, MI, Bytes, Address, os, cs, Complete);
22968d967d9SColin LeMahieu     MI.addOperand(MCOperand::createInst(Inst));
23068d967d9SColin LeMahieu     Size += HEXAGON_INSTR_SIZE;
23168d967d9SColin LeMahieu     Bytes = Bytes.slice(HEXAGON_INSTR_SIZE);
23268d967d9SColin LeMahieu   }
2337cd08927SColin LeMahieu   if (Result == MCDisassembler::Fail)
23468d967d9SColin LeMahieu     return Result;
235a72fad98SKrzysztof Parzyszek   if (Size > HEXAGON_MAX_PACKET_SIZE)
236a72fad98SKrzysztof Parzyszek     return MCDisassembler::Fail;
237e12d1e70SKrzysztof Parzyszek   HexagonMCChecker Checker(getContext(), *MCII, STI, MI,
238e12d1e70SKrzysztof Parzyszek                            *getContext().getRegisterInfo(), false);
2397cd08927SColin LeMahieu   if (!Checker.check())
2407cd08927SColin LeMahieu     return MCDisassembler::Fail;
241a8ab1b75SKrzysztof Parzyszek   remapInstruction(MI);
2427cd08927SColin LeMahieu   return MCDisassembler::Success;
2437cd08927SColin LeMahieu }
2447cd08927SColin LeMahieu 
245a8ab1b75SKrzysztof Parzyszek void HexagonDisassembler::remapInstruction(MCInst &Instr) const {
246a8ab1b75SKrzysztof Parzyszek   for (auto I: HexagonMCInstrInfo::bundleInstructions(Instr)) {
247a8ab1b75SKrzysztof Parzyszek     auto &MI = const_cast<MCInst &>(*I.getInst());
248a8ab1b75SKrzysztof Parzyszek     switch (MI.getOpcode()) {
249a8ab1b75SKrzysztof Parzyszek     case Hexagon::S2_allocframe:
250a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::R29) {
251a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(Hexagon::S6_allocframe_to_raw);
252a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 1);
253a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
254a8ab1b75SKrzysztof Parzyszek       }
255a8ab1b75SKrzysztof Parzyszek       break;
256a8ab1b75SKrzysztof Parzyszek     case Hexagon::L2_deallocframe:
257a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
258a8ab1b75SKrzysztof Parzyszek           MI.getOperand(1).getReg() == Hexagon::R30) {
259a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L6_deallocframe_map_to_raw);
260a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 1);
261a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
262a8ab1b75SKrzysztof Parzyszek       }
263a8ab1b75SKrzysztof Parzyszek       break;
264a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return:
265a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
266a8ab1b75SKrzysztof Parzyszek           MI.getOperand(1).getReg() == Hexagon::R30) {
267a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L6_return_map_to_raw);
268a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 1);
269a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
270a8ab1b75SKrzysztof Parzyszek       }
271a8ab1b75SKrzysztof Parzyszek       break;
272a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_t:
273a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
274a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
275a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_t);
276a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
277a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
278a8ab1b75SKrzysztof Parzyszek       }
279a8ab1b75SKrzysztof Parzyszek       break;
280a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_f:
281a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
282a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
283a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_f);
284a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
285a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
286a8ab1b75SKrzysztof Parzyszek       }
287a8ab1b75SKrzysztof Parzyszek       break;
288a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_tnew_pt:
289a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
290a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
291a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_tnew_pt);
292a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
293a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
294a8ab1b75SKrzysztof Parzyszek       }
295a8ab1b75SKrzysztof Parzyszek       break;
296a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_fnew_pt:
297a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
298a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
299a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_fnew_pt);
300a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
301a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
302a8ab1b75SKrzysztof Parzyszek       }
303a8ab1b75SKrzysztof Parzyszek       break;
304a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_tnew_pnt:
305a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
306a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
307a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_tnew_pnt);
308a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
309a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
310a8ab1b75SKrzysztof Parzyszek       }
311a8ab1b75SKrzysztof Parzyszek       break;
312a8ab1b75SKrzysztof Parzyszek     case Hexagon::L4_return_fnew_pnt:
313a8ab1b75SKrzysztof Parzyszek       if (MI.getOperand(0).getReg() == Hexagon::D15 &&
314a8ab1b75SKrzysztof Parzyszek           MI.getOperand(2).getReg() == Hexagon::R30) {
315a8ab1b75SKrzysztof Parzyszek         MI.setOpcode(L4_return_map_to_raw_fnew_pnt);
316a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin () + 2);
317a8ab1b75SKrzysztof Parzyszek         MI.erase(MI.begin ());
318a8ab1b75SKrzysztof Parzyszek       }
319a8ab1b75SKrzysztof Parzyszek       break;
320a8ab1b75SKrzysztof Parzyszek     }
321a8ab1b75SKrzysztof Parzyszek   }
322a8ab1b75SKrzysztof Parzyszek }
323a8ab1b75SKrzysztof Parzyszek 
324e4fc6ee7SEugene Zelenko static void adjustDuplex(MCInst &MI, MCContext &Context) {
325a72fad98SKrzysztof Parzyszek   switch (MI.getOpcode()) {
326a72fad98SKrzysztof Parzyszek   case Hexagon::SA1_setin1:
327a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 1,
328a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
329a72fad98SKrzysztof Parzyszek     break;
330a72fad98SKrzysztof Parzyszek   case Hexagon::SA1_dec:
331a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 2,
332a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
333a72fad98SKrzysztof Parzyszek     break;
334a72fad98SKrzysztof Parzyszek   default:
335a72fad98SKrzysztof Parzyszek     break;
3367cd08927SColin LeMahieu   }
337a72fad98SKrzysztof Parzyszek }
33868d967d9SColin LeMahieu 
33968d967d9SColin LeMahieu DecodeStatus HexagonDisassembler::getSingleInstruction(
34068d967d9SColin LeMahieu     MCInst &MI, MCInst &MCB, ArrayRef<uint8_t> Bytes, uint64_t Address,
34168d967d9SColin LeMahieu     raw_ostream &os, raw_ostream &cs, bool &Complete) const {
34268d967d9SColin LeMahieu   assert(Bytes.size() >= HEXAGON_INSTR_SIZE);
34368d967d9SColin LeMahieu 
344a72fad98SKrzysztof Parzyszek   uint32_t Instruction = support::endian::read32le(Bytes.data());
34588ebb9e2SColin LeMahieu 
34668d967d9SColin LeMahieu   auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB);
34768d967d9SColin LeMahieu   if ((Instruction & HexagonII::INST_PARSE_MASK) ==
34868d967d9SColin LeMahieu       HexagonII::INST_PARSE_LOOP_END) {
34968d967d9SColin LeMahieu     if (BundleSize == 0)
35068d967d9SColin LeMahieu       HexagonMCInstrInfo::setInnerLoop(MCB);
35168d967d9SColin LeMahieu     else if (BundleSize == 1)
35268d967d9SColin LeMahieu       HexagonMCInstrInfo::setOuterLoop(MCB);
35368d967d9SColin LeMahieu     else
35468d967d9SColin LeMahieu       return DecodeStatus::Fail;
35568d967d9SColin LeMahieu   }
35668d967d9SColin LeMahieu 
357a8ab1b75SKrzysztof Parzyszek   CurrentExtender = HexagonMCInstrInfo::extenderForIndex(
358a72fad98SKrzysztof Parzyszek       MCB, HexagonMCInstrInfo::bundleSize(MCB));
359a72fad98SKrzysztof Parzyszek 
360a72fad98SKrzysztof Parzyszek   DecodeStatus Result = DecodeStatus::Fail;
36168d967d9SColin LeMahieu   if ((Instruction & HexagonII::INST_PARSE_MASK) ==
362be8c453dSColin LeMahieu       HexagonII::INST_PARSE_DUPLEX) {
363a72fad98SKrzysztof Parzyszek     unsigned duplexIClass;
364a72fad98SKrzysztof Parzyszek     uint8_t const *DecodeLow, *DecodeHigh;
365be8c453dSColin LeMahieu     duplexIClass = ((Instruction >> 28) & 0xe) | ((Instruction >> 13) & 0x1);
366be8c453dSColin LeMahieu     switch (duplexIClass) {
367be8c453dSColin LeMahieu     default:
368be8c453dSColin LeMahieu       return MCDisassembler::Fail;
369be8c453dSColin LeMahieu     case 0:
370a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L132;
371a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
372be8c453dSColin LeMahieu       break;
373be8c453dSColin LeMahieu     case 1:
374a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
375a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
376be8c453dSColin LeMahieu       break;
377be8c453dSColin LeMahieu     case 2:
378a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
379a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
380be8c453dSColin LeMahieu       break;
381be8c453dSColin LeMahieu     case 3:
382a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_A32;
383a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
384be8c453dSColin LeMahieu       break;
385be8c453dSColin LeMahieu     case 4:
386a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L132;
387a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
388be8c453dSColin LeMahieu       break;
389be8c453dSColin LeMahieu     case 5:
390a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
391a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
392be8c453dSColin LeMahieu       break;
393be8c453dSColin LeMahieu     case 6:
394a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
395a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
396be8c453dSColin LeMahieu       break;
397be8c453dSColin LeMahieu     case 7:
398a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
399a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
400be8c453dSColin LeMahieu       break;
401be8c453dSColin LeMahieu     case 8:
402a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
403a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
404be8c453dSColin LeMahieu       break;
405be8c453dSColin LeMahieu     case 9:
406a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
407a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
408be8c453dSColin LeMahieu       break;
409be8c453dSColin LeMahieu     case 10:
410a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
411a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S132;
412be8c453dSColin LeMahieu       break;
413be8c453dSColin LeMahieu     case 11:
414a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
415a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S132;
416be8c453dSColin LeMahieu       break;
417be8c453dSColin LeMahieu     case 12:
418a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
419a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
420be8c453dSColin LeMahieu       break;
421be8c453dSColin LeMahieu     case 13:
422a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
423a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
424be8c453dSColin LeMahieu       break;
425be8c453dSColin LeMahieu     case 14:
426a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
427a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S232;
428be8c453dSColin LeMahieu       break;
429be8c453dSColin LeMahieu     }
430a72fad98SKrzysztof Parzyszek     MI.setOpcode(Hexagon::DuplexIClass0 + duplexIClass);
431be8c453dSColin LeMahieu     MCInst *MILow = new (getContext()) MCInst;
432be8c453dSColin LeMahieu     MCInst *MIHigh = new (getContext()) MCInst;
433a8ab1b75SKrzysztof Parzyszek     auto TmpExtender = CurrentExtender;
434a8ab1b75SKrzysztof Parzyszek     CurrentExtender =
435a8ab1b75SKrzysztof Parzyszek         nullptr; // constant extenders in duplex must always be in slot 1
436a72fad98SKrzysztof Parzyszek     Result = decodeInstruction(DecodeLow, *MILow, Instruction & 0x1fff, Address,
437a72fad98SKrzysztof Parzyszek                                this, STI);
438a8ab1b75SKrzysztof Parzyszek     CurrentExtender = TmpExtender;
439a72fad98SKrzysztof Parzyszek     if (Result != DecodeStatus::Success)
440a72fad98SKrzysztof Parzyszek       return DecodeStatus::Fail;
441a72fad98SKrzysztof Parzyszek     adjustDuplex(*MILow, getContext());
442a72fad98SKrzysztof Parzyszek     Result = decodeInstruction(
443a72fad98SKrzysztof Parzyszek         DecodeHigh, *MIHigh, (Instruction >> 16) & 0x1fff, Address, this, STI);
444a72fad98SKrzysztof Parzyszek     if (Result != DecodeStatus::Success)
445a72fad98SKrzysztof Parzyszek       return DecodeStatus::Fail;
446a72fad98SKrzysztof Parzyszek     adjustDuplex(*MIHigh, getContext());
447be8c453dSColin LeMahieu     MCOperand OPLow = MCOperand::createInst(MILow);
448be8c453dSColin LeMahieu     MCOperand OPHigh = MCOperand::createInst(MIHigh);
449be8c453dSColin LeMahieu     MI.addOperand(OPLow);
450be8c453dSColin LeMahieu     MI.addOperand(OPHigh);
451be8c453dSColin LeMahieu     Complete = true;
452be8c453dSColin LeMahieu   } else {
453be8c453dSColin LeMahieu     if ((Instruction & HexagonII::INST_PARSE_MASK) ==
45468d967d9SColin LeMahieu         HexagonII::INST_PARSE_PACKET_END)
45568d967d9SColin LeMahieu       Complete = true;
4567cd08927SColin LeMahieu 
457a8ab1b75SKrzysztof Parzyszek     if (CurrentExtender != nullptr)
458a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTableMustExtend32, MI, Instruction,
459a72fad98SKrzysztof Parzyszek                                  Address, this, STI);
460a72fad98SKrzysztof Parzyszek 
461a72fad98SKrzysztof Parzyszek     if (Result != MCDisassembler::Success)
462a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTable32, MI, Instruction, Address, this,
463a72fad98SKrzysztof Parzyszek                                  STI);
464a72fad98SKrzysztof Parzyszek 
465a72fad98SKrzysztof Parzyszek     if (Result != MCDisassembler::Success &&
466a72fad98SKrzysztof Parzyszek         STI.getFeatureBits()[Hexagon::ExtensionHVX])
467a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTableEXT_mmvec32, MI, Instruction,
468a72fad98SKrzysztof Parzyszek                                  Address, this, STI);
469a72fad98SKrzysztof Parzyszek 
470be8c453dSColin LeMahieu   }
47168d967d9SColin LeMahieu 
47281707549SColin LeMahieu   switch (MI.getOpcode()) {
47381707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_f_jumpnv_nt:
47481707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_f_jumpnv_t:
47581707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp0_jump_nt:
47681707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp0_jump_t:
47781707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp1_jump_nt:
47881707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp1_jump_t:
47981707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_t_jumpnv_nt:
48081707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_t_jumpnv_t:
48181707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp0_jump_nt:
48281707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp0_jump_t:
48381707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp1_jump_nt:
48481707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp1_jump_t:
48581707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_f_jumpnv_nt:
48681707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_f_jumpnv_t:
48781707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp0_jump_nt:
48881707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp0_jump_t:
48981707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp1_jump_nt:
49081707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp1_jump_t:
49181707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_t_jumpnv_nt:
49281707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_t_jumpnv_t:
49381707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp0_jump_nt:
49481707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp0_jump_t:
49581707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp1_jump_nt:
49681707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp1_jump_t:
497a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 1,
498a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, getContext())));
49981707549SColin LeMahieu     break;
50081707549SColin LeMahieu   default:
50181707549SColin LeMahieu     break;
50281707549SColin LeMahieu   }
50381707549SColin LeMahieu 
5047cd08927SColin LeMahieu   if (HexagonMCInstrInfo::isNewValue(*MCII, MI)) {
5057cd08927SColin LeMahieu     unsigned OpIndex = HexagonMCInstrInfo::getNewValueOp(*MCII, MI);
5067cd08927SColin LeMahieu     MCOperand &MCO = MI.getOperand(OpIndex);
5077cd08927SColin LeMahieu     assert(MCO.isReg() && "New value consumers must be registers");
5087cd08927SColin LeMahieu     unsigned Register =
5097cd08927SColin LeMahieu         getContext().getRegisterInfo()->getEncodingValue(MCO.getReg());
5107cd08927SColin LeMahieu     if ((Register & 0x6) == 0)
5117cd08927SColin LeMahieu       // HexagonPRM 10.11 Bit 1-2 == 0 is reserved
5127cd08927SColin LeMahieu       return MCDisassembler::Fail;
5137cd08927SColin LeMahieu     unsigned Lookback = (Register & 0x6) >> 1;
5147cd08927SColin LeMahieu     unsigned Offset = 1;
5157cd08927SColin LeMahieu     bool Vector = HexagonMCInstrInfo::isVector(*MCII, MI);
516a8ab1b75SKrzysztof Parzyszek     bool PrevVector = false;
5177cd08927SColin LeMahieu     auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
5187cd08927SColin LeMahieu     auto i = Instructions.end() - 1;
5197cd08927SColin LeMahieu     for (auto n = Instructions.begin() - 1;; --i, ++Offset) {
5207cd08927SColin LeMahieu       if (i == n)
5217cd08927SColin LeMahieu         // Couldn't find producer
5227cd08927SColin LeMahieu         return MCDisassembler::Fail;
523a8ab1b75SKrzysztof Parzyszek       bool CurrentVector = HexagonMCInstrInfo::isVector(*MCII, *i->getInst());
524a8ab1b75SKrzysztof Parzyszek       if (Vector && !CurrentVector)
5257cd08927SColin LeMahieu         // Skip scalars when calculating distances for vectors
5267cd08927SColin LeMahieu         ++Lookback;
527a8ab1b75SKrzysztof Parzyszek       if (HexagonMCInstrInfo::isImmext(*i->getInst()) && (Vector == PrevVector))
5287cd08927SColin LeMahieu         ++Lookback;
529a8ab1b75SKrzysztof Parzyszek       PrevVector = CurrentVector;
5307cd08927SColin LeMahieu       if (Offset == Lookback)
5317cd08927SColin LeMahieu         break;
5327cd08927SColin LeMahieu     }
5337cd08927SColin LeMahieu     auto const &Inst = *i->getInst();
5347cd08927SColin LeMahieu     bool SubregBit = (Register & 0x1) != 0;
535a8ab1b75SKrzysztof Parzyszek     if (HexagonMCInstrInfo::hasNewValue2(*MCII, Inst)) {
5367cd08927SColin LeMahieu       // If subreg bit is set we're selecting the second produced newvalue
537a8ab1b75SKrzysztof Parzyszek       unsigned Producer = SubregBit ?
538a8ab1b75SKrzysztof Parzyszek           HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg() :
5397cd08927SColin LeMahieu           HexagonMCInstrInfo::getNewValueOperand2(*MCII, Inst).getReg();
5407cd08927SColin LeMahieu       assert(Producer != Hexagon::NoRegister);
5417cd08927SColin LeMahieu       MCO.setReg(Producer);
5427cd08927SColin LeMahieu     } else if (HexagonMCInstrInfo::hasNewValue(*MCII, Inst)) {
5437cd08927SColin LeMahieu       unsigned Producer =
5447cd08927SColin LeMahieu           HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg();
5457cd08927SColin LeMahieu       if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
5467cd08927SColin LeMahieu         Producer = ((Producer - Hexagon::W0) << 1) + SubregBit + Hexagon::V0;
5477cd08927SColin LeMahieu       else if (SubregBit)
5486fdfa3dcSColin LeMahieu         // Hexagon PRM 10.11 New-value operands
5496fdfa3dcSColin LeMahieu         // Nt[0] is reserved and should always be encoded as zero.
5507cd08927SColin LeMahieu         return MCDisassembler::Fail;
5517cd08927SColin LeMahieu       assert(Producer != Hexagon::NoRegister);
5527cd08927SColin LeMahieu       MCO.setReg(Producer);
5537cd08927SColin LeMahieu     } else
5547cd08927SColin LeMahieu       return MCDisassembler::Fail;
5557cd08927SColin LeMahieu   }
5567cd08927SColin LeMahieu 
557a8ab1b75SKrzysztof Parzyszek   if (CurrentExtender != nullptr) {
558a72fad98SKrzysztof Parzyszek     MCInst const &Inst = HexagonMCInstrInfo::isDuplex(*MCII, MI)
559a72fad98SKrzysztof Parzyszek                              ? *MI.getOperand(1).getInst()
560a72fad98SKrzysztof Parzyszek                              : MI;
5617cd08927SColin LeMahieu     if (!HexagonMCInstrInfo::isExtendable(*MCII, Inst) &&
5627cd08927SColin LeMahieu         !HexagonMCInstrInfo::isExtended(*MCII, Inst))
5637cd08927SColin LeMahieu       return MCDisassembler::Fail;
5647cd08927SColin LeMahieu   }
5655d6f03bdSColin LeMahieu   return Result;
56688ebb9e2SColin LeMahieu }
5677cd08927SColin LeMahieu 
5687cd08927SColin LeMahieu static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,
569e5e035a3SCraig Topper                                         ArrayRef<MCPhysReg> Table) {
5703da000c0SCraig Topper   if (RegNo < Table.size()) {
5717cd08927SColin LeMahieu     Inst.addOperand(MCOperand::createReg(Table[RegNo]));
5727cd08927SColin LeMahieu     return MCDisassembler::Success;
5733da000c0SCraig Topper   }
5743da000c0SCraig Topper 
5757cd08927SColin LeMahieu   return MCDisassembler::Fail;
5767cd08927SColin LeMahieu }
5777cd08927SColin LeMahieu 
5787c958713SColin LeMahieu static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
5797c958713SColin LeMahieu                                                    uint64_t Address,
5807c958713SColin LeMahieu                                                    const void *Decoder) {
5817c958713SColin LeMahieu   return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
5827c958713SColin LeMahieu }
5837c958713SColin LeMahieu 
5847cd08927SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5857cd08927SColin LeMahieu                                                uint64_t Address,
5867cd08927SColin LeMahieu                                                const void *Decoder) {
587e5e035a3SCraig Topper   static const MCPhysReg IntRegDecoderTable[] = {
5887cd08927SColin LeMahieu       Hexagon::R0,  Hexagon::R1,  Hexagon::R2,  Hexagon::R3,  Hexagon::R4,
5897cd08927SColin LeMahieu       Hexagon::R5,  Hexagon::R6,  Hexagon::R7,  Hexagon::R8,  Hexagon::R9,
5907cd08927SColin LeMahieu       Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
5917cd08927SColin LeMahieu       Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
5927cd08927SColin LeMahieu       Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
5937cd08927SColin LeMahieu       Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
5947cd08927SColin LeMahieu       Hexagon::R30, Hexagon::R31};
5957cd08927SColin LeMahieu 
596e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, IntRegDecoderTable);
5977cd08927SColin LeMahieu }
5987c958713SColin LeMahieu 
599a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
600a72fad98SKrzysztof Parzyszek                                                       unsigned RegNo,
601a72fad98SKrzysztof Parzyszek                                                       uint64_t Address,
602a72fad98SKrzysztof Parzyszek                                                       const void *Decoder) {
603a72fad98SKrzysztof Parzyszek   static const MCPhysReg GeneralSubRegDecoderTable[] = {
604a72fad98SKrzysztof Parzyszek       Hexagon::R0,  Hexagon::R1,  Hexagon::R2,  Hexagon::R3,
605a72fad98SKrzysztof Parzyszek       Hexagon::R4,  Hexagon::R5,  Hexagon::R6,  Hexagon::R7,
606a72fad98SKrzysztof Parzyszek       Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
607a72fad98SKrzysztof Parzyszek       Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23,
608a72fad98SKrzysztof Parzyszek   };
609a72fad98SKrzysztof Parzyszek 
610a72fad98SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, GeneralSubRegDecoderTable);
611a72fad98SKrzysztof Parzyszek }
612a72fad98SKrzysztof Parzyszek 
61355772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,
6147c958713SColin LeMahieu                                              uint64_t /*Address*/,
6157c958713SColin LeMahieu                                              const void *Decoder) {
61655772976SKrzysztof Parzyszek   static const MCPhysReg HvxVRDecoderTable[] = {
6177c958713SColin LeMahieu       Hexagon::V0,  Hexagon::V1,  Hexagon::V2,  Hexagon::V3,  Hexagon::V4,
6187c958713SColin LeMahieu       Hexagon::V5,  Hexagon::V6,  Hexagon::V7,  Hexagon::V8,  Hexagon::V9,
6197c958713SColin LeMahieu       Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
6207c958713SColin LeMahieu       Hexagon::V15, Hexagon::V16, Hexagon::V17, Hexagon::V18, Hexagon::V19,
6217c958713SColin LeMahieu       Hexagon::V20, Hexagon::V21, Hexagon::V22, Hexagon::V23, Hexagon::V24,
6227c958713SColin LeMahieu       Hexagon::V25, Hexagon::V26, Hexagon::V27, Hexagon::V28, Hexagon::V29,
6237c958713SColin LeMahieu       Hexagon::V30, Hexagon::V31};
6247c958713SColin LeMahieu 
62555772976SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, HvxVRDecoderTable);
6267c958713SColin LeMahieu }
6277c958713SColin LeMahieu 
6287cd08927SColin LeMahieu static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
6297cd08927SColin LeMahieu                                                   uint64_t /*Address*/,
6307cd08927SColin LeMahieu                                                   const void *Decoder) {
631e5e035a3SCraig Topper   static const MCPhysReg DoubleRegDecoderTable[] = {
6327cd08927SColin LeMahieu       Hexagon::D0,  Hexagon::D1,  Hexagon::D2,  Hexagon::D3,
6337cd08927SColin LeMahieu       Hexagon::D4,  Hexagon::D5,  Hexagon::D6,  Hexagon::D7,
6347cd08927SColin LeMahieu       Hexagon::D8,  Hexagon::D9,  Hexagon::D10, Hexagon::D11,
6357cd08927SColin LeMahieu       Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15};
6367cd08927SColin LeMahieu 
637e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo >> 1, DoubleRegDecoderTable);
6387cd08927SColin LeMahieu }
6397cd08927SColin LeMahieu 
640a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralDoubleLow8RegsRegisterClass(
641a72fad98SKrzysztof Parzyszek     MCInst &Inst, unsigned RegNo, uint64_t /*Address*/, const void *Decoder) {
642a72fad98SKrzysztof Parzyszek   static const MCPhysReg GeneralDoubleLow8RegDecoderTable[] = {
643a72fad98SKrzysztof Parzyszek       Hexagon::D0, Hexagon::D1, Hexagon::D2,  Hexagon::D3,
644a72fad98SKrzysztof Parzyszek       Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11};
645a72fad98SKrzysztof Parzyszek 
646a72fad98SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, GeneralDoubleLow8RegDecoderTable);
647a72fad98SKrzysztof Parzyszek }
648a72fad98SKrzysztof Parzyszek 
64955772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,
6507c958713SColin LeMahieu                                              uint64_t /*Address*/,
6517c958713SColin LeMahieu                                              const void *Decoder) {
65255772976SKrzysztof Parzyszek   static const MCPhysReg HvxWRDecoderTable[] = {
6537c958713SColin LeMahieu       Hexagon::W0,  Hexagon::W1,  Hexagon::W2,  Hexagon::W3,
6547c958713SColin LeMahieu       Hexagon::W4,  Hexagon::W5,  Hexagon::W6,  Hexagon::W7,
6557c958713SColin LeMahieu       Hexagon::W8,  Hexagon::W9,  Hexagon::W10, Hexagon::W11,
6567c958713SColin LeMahieu       Hexagon::W12, Hexagon::W13, Hexagon::W14, Hexagon::W15};
6577c958713SColin LeMahieu 
65855772976SKrzysztof Parzyszek   return (DecodeRegisterClass(Inst, RegNo >> 1, HvxWRDecoderTable));
6597c958713SColin LeMahieu }
6607c958713SColin LeMahieu 
6617cd08927SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
6627cd08927SColin LeMahieu                                                 uint64_t /*Address*/,
6637cd08927SColin LeMahieu                                                 const void *Decoder) {
664e5e035a3SCraig Topper   static const MCPhysReg PredRegDecoderTable[] = {Hexagon::P0, Hexagon::P1,
6657cd08927SColin LeMahieu                                                   Hexagon::P2, Hexagon::P3};
6667cd08927SColin LeMahieu 
667e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, PredRegDecoderTable);
6687cd08927SColin LeMahieu }
6697cd08927SColin LeMahieu 
67055772976SKrzysztof Parzyszek static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,
6717c958713SColin LeMahieu                                              uint64_t /*Address*/,
6727c958713SColin LeMahieu                                              const void *Decoder) {
67355772976SKrzysztof Parzyszek   static const MCPhysReg HvxQRDecoderTable[] = {Hexagon::Q0, Hexagon::Q1,
6747c958713SColin LeMahieu                                                 Hexagon::Q2, Hexagon::Q3};
6757c958713SColin LeMahieu 
67655772976SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, HvxQRDecoderTable);
6777c958713SColin LeMahieu }
678be8c453dSColin LeMahieu 
6797cd08927SColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
6807cd08927SColin LeMahieu                                                uint64_t /*Address*/,
6817cd08927SColin LeMahieu                                                const void *Decoder) {
682f9015e62SKrzysztof Parzyszek   using namespace Hexagon;
683e4fc6ee7SEugene Zelenko 
684e5e035a3SCraig Topper   static const MCPhysReg CtrlRegDecoderTable[] = {
685f9015e62SKrzysztof Parzyszek     /*  0 */  SA0,        LC0,        SA1,        LC1,
686e2603328SKrzysztof Parzyszek     /*  4 */  P3_0,       C5,         M0,         M1,
687f9015e62SKrzysztof Parzyszek     /*  8 */  USR,        PC,         UGP,        GP,
688ab57c2baSKrzysztof Parzyszek     /* 12 */  CS0,        CS1,        UPCYCLELO,  UPCYCLEHI,
689f9015e62SKrzysztof Parzyszek     /* 16 */  FRAMELIMIT, FRAMEKEY,   PKTCOUNTLO, PKTCOUNTHI,
690f9015e62SKrzysztof Parzyszek     /* 20 */  0,          0,          0,          0,
691f9015e62SKrzysztof Parzyszek     /* 24 */  0,          0,          0,          0,
692f9015e62SKrzysztof Parzyszek     /* 28 */  0,          0,          UTIMERLO,   UTIMERHI
6937cd08927SColin LeMahieu   };
6947cd08927SColin LeMahieu 
6956261e1b9SCraig Topper   if (RegNo >= array_lengthof(CtrlRegDecoderTable))
6967cd08927SColin LeMahieu     return MCDisassembler::Fail;
6977cd08927SColin LeMahieu 
698f9015e62SKrzysztof Parzyszek   static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
699f9015e62SKrzysztof Parzyszek   if (CtrlRegDecoderTable[RegNo] == NoRegister)
7007cd08927SColin LeMahieu     return MCDisassembler::Fail;
7017cd08927SColin LeMahieu 
7027cd08927SColin LeMahieu   unsigned Register = CtrlRegDecoderTable[RegNo];
7037cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
7047cd08927SColin LeMahieu   return MCDisassembler::Success;
7057cd08927SColin LeMahieu }
7067cd08927SColin LeMahieu 
7077cd08927SColin LeMahieu static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
7087cd08927SColin LeMahieu                                                  uint64_t /*Address*/,
7097cd08927SColin LeMahieu                                                  const void *Decoder) {
710f9015e62SKrzysztof Parzyszek   using namespace Hexagon;
711e4fc6ee7SEugene Zelenko 
712e5e035a3SCraig Topper   static const MCPhysReg CtrlReg64DecoderTable[] = {
713f9015e62SKrzysztof Parzyszek     /*  0 */  C1_0,       0,          C3_2,       0,
714f9015e62SKrzysztof Parzyszek     /*  4 */  C5_4,       0,          C7_6,       0,
715f9015e62SKrzysztof Parzyszek     /*  8 */  C9_8,       0,          C11_10,     0,
716ab57c2baSKrzysztof Parzyszek     /* 12 */  CS,         0,          UPCYCLE,    0,
717f9015e62SKrzysztof Parzyszek     /* 16 */  C17_16,     0,          PKTCOUNT,   0,
718f9015e62SKrzysztof Parzyszek     /* 20 */  0,          0,          0,          0,
719f9015e62SKrzysztof Parzyszek     /* 24 */  0,          0,          0,          0,
720f9015e62SKrzysztof Parzyszek     /* 28 */  0,          0,          UTIMER,     0
7217cd08927SColin LeMahieu   };
7227cd08927SColin LeMahieu 
7236261e1b9SCraig Topper   if (RegNo >= array_lengthof(CtrlReg64DecoderTable))
7247cd08927SColin LeMahieu     return MCDisassembler::Fail;
7257cd08927SColin LeMahieu 
726f9015e62SKrzysztof Parzyszek   static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
727f9015e62SKrzysztof Parzyszek   if (CtrlReg64DecoderTable[RegNo] == NoRegister)
7287cd08927SColin LeMahieu     return MCDisassembler::Fail;
7297cd08927SColin LeMahieu 
7307cd08927SColin LeMahieu   unsigned Register = CtrlReg64DecoderTable[RegNo];
7317cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
7327cd08927SColin LeMahieu   return MCDisassembler::Success;
7337cd08927SColin LeMahieu }
7347cd08927SColin LeMahieu 
7357cd08927SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
7367cd08927SColin LeMahieu                                                uint64_t /*Address*/,
7377cd08927SColin LeMahieu                                                const void *Decoder) {
7387cd08927SColin LeMahieu   unsigned Register = 0;
7397cd08927SColin LeMahieu   switch (RegNo) {
7407cd08927SColin LeMahieu   case 0:
7417cd08927SColin LeMahieu     Register = Hexagon::M0;
7427cd08927SColin LeMahieu     break;
7437cd08927SColin LeMahieu   case 1:
7447cd08927SColin LeMahieu     Register = Hexagon::M1;
7457cd08927SColin LeMahieu     break;
7467cd08927SColin LeMahieu   default:
7477cd08927SColin LeMahieu     return MCDisassembler::Fail;
7487cd08927SColin LeMahieu   }
7497cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
7507cd08927SColin LeMahieu   return MCDisassembler::Success;
7517cd08927SColin LeMahieu }
7527cd08927SColin LeMahieu 
7537cd08927SColin LeMahieu static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
7547cd08927SColin LeMahieu                                        uint64_t /*Address*/,
7557cd08927SColin LeMahieu                                        const void *Decoder) {
7567cd08927SColin LeMahieu   HexagonDisassembler const &Disassembler = disassembler(Decoder);
757a8ab1b75SKrzysztof Parzyszek   int64_t FullValue = fullValue(Disassembler, MI, tmp);
7587cd08927SColin LeMahieu   assert(FullValue >= 0 && "Negative in unsigned decoder");
7597cd08927SColin LeMahieu   HexagonMCInstrInfo::addConstant(MI, FullValue, Disassembler.getContext());
7607cd08927SColin LeMahieu   return MCDisassembler::Success;
7617cd08927SColin LeMahieu }
7627cd08927SColin LeMahieu 
763a72fad98SKrzysztof Parzyszek static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
764a72fad98SKrzysztof Parzyszek                                     uint64_t /*Address*/, const void *Decoder) {
765a72fad98SKrzysztof Parzyszek   HexagonDisassembler const &Disassembler = disassembler(Decoder);
766a72fad98SKrzysztof Parzyszek   unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
767a72fad98SKrzysztof Parzyszek   tmp = SignExtend64(tmp, Bits);
768a72fad98SKrzysztof Parzyszek   signedDecoder<32>(MI, tmp, Decoder);
769a72fad98SKrzysztof Parzyszek   return MCDisassembler::Success;
770a72fad98SKrzysztof Parzyszek }
771a72fad98SKrzysztof Parzyszek 
7727cd08927SColin LeMahieu // custom decoder for various jump/call immediates
7737cd08927SColin LeMahieu static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
7747cd08927SColin LeMahieu                                     const void *Decoder) {
7757cd08927SColin LeMahieu   HexagonDisassembler const &Disassembler = disassembler(Decoder);
7767cd08927SColin LeMahieu   unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
7777cd08927SColin LeMahieu   // r13_2 is not extendable, so if there are no extent bits, it's r13_2
7787cd08927SColin LeMahieu   if (Bits == 0)
7797cd08927SColin LeMahieu     Bits = 15;
780a8ab1b75SKrzysztof Parzyszek   uint64_t FullValue = fullValue(Disassembler, MI, SignExtend64(tmp, Bits));
781a8ab1b75SKrzysztof Parzyszek   uint32_t Extended = FullValue + Address;
782a72fad98SKrzysztof Parzyszek   if (!Disassembler.tryAddingSymbolicOperand(MI, Extended, Address, true, 0, 4))
7837cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
7847cd08927SColin LeMahieu   return MCDisassembler::Success;
7857cd08927SColin LeMahieu }
786