188ebb9e2SColin LeMahieu //===-- 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"
1588ebb9e2SColin LeMahieu #include "MCTargetDesc/HexagonMCTargetDesc.h"
167cd08927SColin LeMahieu #include "MCTargetDesc/HexagonMCInstrInfo.h"
1782085927SEugene Zelenko #include "llvm/ADT/ArrayRef.h"
1882085927SEugene Zelenko #include "llvm/ADT/STLExtras.h"
19f57c1977SBenjamin Kramer #include "llvm/MC/MCDisassembler/MCDisassembler.h"
207cd08927SColin LeMahieu #include "llvm/MC/MCContext.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"
2782085927SEugene Zelenko #include "llvm/Support/MathExtras.h"
28d9903888SChandler Carruth #include "llvm/Support/raw_ostream.h"
297cd08927SColin LeMahieu #include "llvm/Support/TargetRegistry.h"
3082085927SEugene Zelenko #include <cassert>
3182085927SEugene Zelenko #include <cstddef>
3282085927SEugene Zelenko #include <cstdint>
3382085927SEugene Zelenko #include <memory>
3488ebb9e2SColin LeMahieu 
3588ebb9e2SColin LeMahieu using namespace llvm;
3668d967d9SColin LeMahieu using namespace Hexagon;
3788ebb9e2SColin LeMahieu 
387cd08927SColin LeMahieu typedef MCDisassembler::DecodeStatus DecodeStatus;
3988ebb9e2SColin LeMahieu 
4088ebb9e2SColin LeMahieu namespace {
4182085927SEugene Zelenko 
4288ebb9e2SColin LeMahieu /// \brief Hexagon disassembler for all Hexagon platforms.
4388ebb9e2SColin LeMahieu class HexagonDisassembler : public MCDisassembler {
4488ebb9e2SColin LeMahieu public:
457cd08927SColin LeMahieu   std::unique_ptr<MCInstrInfo const> const MCII;
4668d967d9SColin LeMahieu   std::unique_ptr<MCInst *> CurrentBundle;
4782085927SEugene Zelenko 
487cd08927SColin LeMahieu   HexagonDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
497cd08927SColin LeMahieu                       MCInstrInfo const *MCII)
507cd08927SColin LeMahieu       : MCDisassembler(STI, Ctx), MCII(MCII), CurrentBundle(new MCInst *) {}
5188ebb9e2SColin LeMahieu 
5268d967d9SColin LeMahieu   DecodeStatus getSingleInstruction(MCInst &Instr, MCInst &MCB,
5368d967d9SColin LeMahieu                                     ArrayRef<uint8_t> Bytes, uint64_t Address,
5468d967d9SColin LeMahieu                                     raw_ostream &VStream, raw_ostream &CStream,
5568d967d9SColin LeMahieu                                     bool &Complete) const;
564aa6bea7SRafael Espindola   DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
577fc5b874SRafael Espindola                               ArrayRef<uint8_t> Bytes, uint64_t Address,
584aa6bea7SRafael Espindola                               raw_ostream &VStream,
594aa6bea7SRafael Espindola                               raw_ostream &CStream) const override;
607cd08927SColin LeMahieu   void addSubinstOperands(MCInst *MI, unsigned opcode, unsigned inst) const;
6188ebb9e2SColin LeMahieu };
6282085927SEugene Zelenko 
63*a72fad98SKrzysztof Parzyszek namespace {
64*a72fad98SKrzysztof Parzyszek   uint32_t fullValue(MCInstrInfo const &MCII, MCInst &MCB, MCInst &MI,
65*a72fad98SKrzysztof Parzyszek                      int64_t Value) {
66*a72fad98SKrzysztof Parzyszek     MCInst const *Extender = HexagonMCInstrInfo::extenderForIndex(
67*a72fad98SKrzysztof Parzyszek       MCB, HexagonMCInstrInfo::bundleSize(MCB));
68*a72fad98SKrzysztof Parzyszek     if (!Extender || MI.size() != HexagonMCInstrInfo::getExtendableOp(MCII, MI))
69*a72fad98SKrzysztof Parzyszek       return Value;
70*a72fad98SKrzysztof Parzyszek     unsigned Alignment = HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
71*a72fad98SKrzysztof Parzyszek     uint32_t Lower6 = static_cast<uint32_t>(Value >> Alignment) & 0x3f;
72*a72fad98SKrzysztof Parzyszek     int64_t Bits;
73*a72fad98SKrzysztof Parzyszek     bool Success = Extender->getOperand(0).getExpr()->evaluateAsAbsolute(Bits);
74*a72fad98SKrzysztof Parzyszek     assert(Success); (void)Success;
75*a72fad98SKrzysztof Parzyszek     uint32_t Upper26 = static_cast<uint32_t>(Bits);
76*a72fad98SKrzysztof Parzyszek     uint32_t Operand = Upper26 | Lower6;
77*a72fad98SKrzysztof Parzyszek     return Operand;
78*a72fad98SKrzysztof Parzyszek   }
79*a72fad98SKrzysztof Parzyszek   HexagonDisassembler const &disassembler(void const *Decoder) {
80*a72fad98SKrzysztof Parzyszek     return *static_cast<HexagonDisassembler const *>(Decoder);
81*a72fad98SKrzysztof Parzyszek   }
82*a72fad98SKrzysztof Parzyszek   template <size_t T>
83*a72fad98SKrzysztof Parzyszek   void signedDecoder(MCInst &MI, unsigned tmp, const void *Decoder) {
84*a72fad98SKrzysztof Parzyszek     HexagonDisassembler const &Disassembler = disassembler(Decoder);
85*a72fad98SKrzysztof Parzyszek     int64_t FullValue =
86*a72fad98SKrzysztof Parzyszek         fullValue(*Disassembler.MCII, **Disassembler.CurrentBundle, MI,
87*a72fad98SKrzysztof Parzyszek                   SignExtend64<T>(tmp));
88*a72fad98SKrzysztof Parzyszek     int64_t Extended = SignExtend64<32>(FullValue);
89*a72fad98SKrzysztof Parzyszek     HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
90*a72fad98SKrzysztof Parzyszek   }
91*a72fad98SKrzysztof Parzyszek }
9282085927SEugene Zelenko } // end anonymous namespace
9388ebb9e2SColin LeMahieu 
947cd08927SColin LeMahieu // Forward declare these because the auto-generated code will reference them.
957cd08927SColin LeMahieu // Definitions are further down.
967cd08927SColin LeMahieu 
977cd08927SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
98be8c453dSColin LeMahieu                                                uint64_t Address,
99be8c453dSColin LeMahieu                                                const void *Decoder);
100*a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
101*a72fad98SKrzysztof Parzyszek                                                       unsigned RegNo,
102*a72fad98SKrzysztof Parzyszek                                                       uint64_t Address,
103*a72fad98SKrzysztof Parzyszek                                                       const void *Decoder);
1047c958713SColin LeMahieu static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
1057c958713SColin LeMahieu                                                    uint64_t Address,
1067c958713SColin LeMahieu                                                    const void *Decoder);
1077c958713SColin LeMahieu static DecodeStatus DecodeVectorRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1087c958713SColin LeMahieu                                                   uint64_t Address,
1097c958713SColin LeMahieu                                                   const void *Decoder);
1107cd08927SColin LeMahieu static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1117cd08927SColin LeMahieu                                                   uint64_t Address,
1127cd08927SColin LeMahieu                                                   const void *Decoder);
113*a72fad98SKrzysztof Parzyszek static DecodeStatus
114*a72fad98SKrzysztof Parzyszek DecodeGeneralDoubleLow8RegsRegisterClass(MCInst &Inst, unsigned RegNo,
115*a72fad98SKrzysztof Parzyszek                                          uint64_t Address, const void *Decoder);
1167c958713SColin LeMahieu static DecodeStatus DecodeVecDblRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1177c958713SColin LeMahieu                                                   uint64_t Address,
1187c958713SColin LeMahieu                                                   const void *Decoder);
1197cd08927SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1207cd08927SColin LeMahieu                                                 uint64_t Address,
1217cd08927SColin LeMahieu                                                 const void *Decoder);
1227c958713SColin LeMahieu static DecodeStatus DecodeVecPredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1237c958713SColin LeMahieu                                                    uint64_t Address,
1247c958713SColin LeMahieu                                                    const void *Decoder);
125f3db884eSColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
126be8c453dSColin LeMahieu                                                uint64_t Address,
127be8c453dSColin LeMahieu                                                const void *Decoder);
1287cd08927SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
1297cd08927SColin LeMahieu                                                uint64_t Address,
1307cd08927SColin LeMahieu                                                const void *Decoder);
131404d5b24SColin LeMahieu static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
132be8c453dSColin LeMahieu                                                  uint64_t Address,
1337cd08927SColin LeMahieu                                                  const void *Decoder);
1347cd08927SColin LeMahieu 
1357cd08927SColin LeMahieu static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
1367cd08927SColin LeMahieu                                        uint64_t Address, const void *Decoder);
137*a72fad98SKrzysztof Parzyszek static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
138*a72fad98SKrzysztof Parzyszek                                     uint64_t /*Address*/, const void *Decoder);
139654dc11bSKrzysztof Parzyszek static DecodeStatus s8_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1401e9d1d76SColin LeMahieu                                  const void *Decoder);
1411e9d1d76SColin LeMahieu static DecodeStatus s6_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1421e9d1d76SColin LeMahieu                                    const void *Decoder);
1431e9d1d76SColin LeMahieu static DecodeStatus s4_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1441e9d1d76SColin LeMahieu                                    const void *Decoder);
1451e9d1d76SColin LeMahieu static DecodeStatus s4_1ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1461e9d1d76SColin LeMahieu                                    const void *Decoder);
1471e9d1d76SColin LeMahieu static DecodeStatus s4_2ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1481e9d1d76SColin LeMahieu                                    const void *Decoder);
1491e9d1d76SColin LeMahieu static DecodeStatus s4_3ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1501e9d1d76SColin LeMahieu                                    const void *Decoder);
1517c958713SColin LeMahieu static DecodeStatus s4_6ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1527c958713SColin LeMahieu                                    const void *Decoder);
1537c958713SColin LeMahieu static DecodeStatus s3_6ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1547c958713SColin LeMahieu                                    const void *Decoder);
1557cd08927SColin LeMahieu static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1567cd08927SColin LeMahieu                                     const void *Decoder);
157efa74e02SColin LeMahieu 
158*a72fad98SKrzysztof Parzyszek #include "HexagonDepDecoders.h"
15988ebb9e2SColin LeMahieu #include "HexagonGenDisassemblerTables.inc"
16088ebb9e2SColin LeMahieu 
1617cd08927SColin LeMahieu static MCDisassembler *createHexagonDisassembler(const Target &T,
1627cd08927SColin LeMahieu                                                  const MCSubtargetInfo &STI,
16388ebb9e2SColin LeMahieu                                                  MCContext &Ctx) {
1647cd08927SColin LeMahieu   return new HexagonDisassembler(STI, Ctx, T.createMCInstrInfo());
16588ebb9e2SColin LeMahieu }
16688ebb9e2SColin LeMahieu 
16788ebb9e2SColin LeMahieu extern "C" void LLVMInitializeHexagonDisassembler() {
168f42454b9SMehdi Amini   TargetRegistry::RegisterMCDisassembler(getTheHexagonTarget(),
16988ebb9e2SColin LeMahieu                                          createHexagonDisassembler);
17088ebb9e2SColin LeMahieu }
17188ebb9e2SColin LeMahieu 
17288ebb9e2SColin LeMahieu DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
1737fc5b874SRafael Espindola                                                  ArrayRef<uint8_t> Bytes,
17488ebb9e2SColin LeMahieu                                                  uint64_t Address,
17588ebb9e2SColin LeMahieu                                                  raw_ostream &os,
17688ebb9e2SColin LeMahieu                                                  raw_ostream &cs) const {
17768d967d9SColin LeMahieu   DecodeStatus Result = DecodeStatus::Success;
17868d967d9SColin LeMahieu   bool Complete = false;
17968d967d9SColin LeMahieu   Size = 0;
1804aa6bea7SRafael Espindola 
18168d967d9SColin LeMahieu   *CurrentBundle = &MI;
182f0af6e52SColin LeMahieu   MI = HexagonMCInstrInfo::createBundle();
18382085927SEugene Zelenko   while (Result == Success && !Complete) {
18468d967d9SColin LeMahieu     if (Bytes.size() < HEXAGON_INSTR_SIZE)
18568d967d9SColin LeMahieu       return MCDisassembler::Fail;
18668d967d9SColin LeMahieu     MCInst *Inst = new (getContext()) MCInst;
18768d967d9SColin LeMahieu     Result = getSingleInstruction(*Inst, MI, Bytes, Address, os, cs, Complete);
18868d967d9SColin LeMahieu     MI.addOperand(MCOperand::createInst(Inst));
18968d967d9SColin LeMahieu     Size += HEXAGON_INSTR_SIZE;
19068d967d9SColin LeMahieu     Bytes = Bytes.slice(HEXAGON_INSTR_SIZE);
19168d967d9SColin LeMahieu   }
1927cd08927SColin LeMahieu   if (Result == MCDisassembler::Fail)
19368d967d9SColin LeMahieu     return Result;
194*a72fad98SKrzysztof Parzyszek   if (Size > HEXAGON_MAX_PACKET_SIZE)
195*a72fad98SKrzysztof Parzyszek     return MCDisassembler::Fail;
1967cd08927SColin LeMahieu   HexagonMCChecker Checker(*MCII, STI, MI, MI, *getContext().getRegisterInfo());
1977cd08927SColin LeMahieu   if (!Checker.check())
1987cd08927SColin LeMahieu     return MCDisassembler::Fail;
1997cd08927SColin LeMahieu   return MCDisassembler::Success;
2007cd08927SColin LeMahieu }
2017cd08927SColin LeMahieu 
202*a72fad98SKrzysztof Parzyszek namespace {
203*a72fad98SKrzysztof Parzyszek void adjustDuplex(MCInst &MI, MCContext &Context) {
204*a72fad98SKrzysztof Parzyszek   switch (MI.getOpcode()) {
205*a72fad98SKrzysztof Parzyszek   case Hexagon::SA1_setin1:
206*a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 1,
207*a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
208*a72fad98SKrzysztof Parzyszek     break;
209*a72fad98SKrzysztof Parzyszek   case Hexagon::SA1_dec:
210*a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 2,
211*a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
212*a72fad98SKrzysztof Parzyszek     break;
213*a72fad98SKrzysztof Parzyszek   default:
214*a72fad98SKrzysztof Parzyszek     break;
2157cd08927SColin LeMahieu   }
216*a72fad98SKrzysztof Parzyszek }
2177cd08927SColin LeMahieu }
21868d967d9SColin LeMahieu 
21968d967d9SColin LeMahieu DecodeStatus HexagonDisassembler::getSingleInstruction(
22068d967d9SColin LeMahieu     MCInst &MI, MCInst &MCB, ArrayRef<uint8_t> Bytes, uint64_t Address,
22168d967d9SColin LeMahieu     raw_ostream &os, raw_ostream &cs, bool &Complete) const {
22268d967d9SColin LeMahieu   assert(Bytes.size() >= HEXAGON_INSTR_SIZE);
22368d967d9SColin LeMahieu 
224*a72fad98SKrzysztof Parzyszek   uint32_t Instruction = support::endian::read32le(Bytes.data());
22588ebb9e2SColin LeMahieu 
22668d967d9SColin LeMahieu   auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB);
22768d967d9SColin LeMahieu   if ((Instruction & HexagonII::INST_PARSE_MASK) ==
22868d967d9SColin LeMahieu       HexagonII::INST_PARSE_LOOP_END) {
22968d967d9SColin LeMahieu     if (BundleSize == 0)
23068d967d9SColin LeMahieu       HexagonMCInstrInfo::setInnerLoop(MCB);
23168d967d9SColin LeMahieu     else if (BundleSize == 1)
23268d967d9SColin LeMahieu       HexagonMCInstrInfo::setOuterLoop(MCB);
23368d967d9SColin LeMahieu     else
23468d967d9SColin LeMahieu       return DecodeStatus::Fail;
23568d967d9SColin LeMahieu   }
23668d967d9SColin LeMahieu 
237*a72fad98SKrzysztof Parzyszek   MCInst const *Extender = HexagonMCInstrInfo::extenderForIndex(
238*a72fad98SKrzysztof Parzyszek       MCB, HexagonMCInstrInfo::bundleSize(MCB));
239*a72fad98SKrzysztof Parzyszek 
240*a72fad98SKrzysztof Parzyszek   DecodeStatus Result = DecodeStatus::Fail;
24168d967d9SColin LeMahieu   if ((Instruction & HexagonII::INST_PARSE_MASK) ==
242be8c453dSColin LeMahieu       HexagonII::INST_PARSE_DUPLEX) {
243*a72fad98SKrzysztof Parzyszek     unsigned duplexIClass;
244*a72fad98SKrzysztof Parzyszek     uint8_t const *DecodeLow, *DecodeHigh;
245be8c453dSColin LeMahieu     duplexIClass = ((Instruction >> 28) & 0xe) | ((Instruction >> 13) & 0x1);
246be8c453dSColin LeMahieu     switch (duplexIClass) {
247be8c453dSColin LeMahieu     default:
248be8c453dSColin LeMahieu       return MCDisassembler::Fail;
249be8c453dSColin LeMahieu     case 0:
250*a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L132;
251*a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
252be8c453dSColin LeMahieu       break;
253be8c453dSColin LeMahieu     case 1:
254*a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
255*a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
256be8c453dSColin LeMahieu       break;
257be8c453dSColin LeMahieu     case 2:
258*a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
259*a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
260be8c453dSColin LeMahieu       break;
261be8c453dSColin LeMahieu     case 3:
262*a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_A32;
263*a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
264be8c453dSColin LeMahieu       break;
265be8c453dSColin LeMahieu     case 4:
266*a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L132;
267*a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
268be8c453dSColin LeMahieu       break;
269be8c453dSColin LeMahieu     case 5:
270*a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
271*a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
272be8c453dSColin LeMahieu       break;
273be8c453dSColin LeMahieu     case 6:
274*a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
275*a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
276be8c453dSColin LeMahieu       break;
277be8c453dSColin LeMahieu     case 7:
278*a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
279*a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
280be8c453dSColin LeMahieu       break;
281be8c453dSColin LeMahieu     case 8:
282*a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
283*a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
284be8c453dSColin LeMahieu       break;
285be8c453dSColin LeMahieu     case 9:
286*a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
287*a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
288be8c453dSColin LeMahieu       break;
289be8c453dSColin LeMahieu     case 10:
290*a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
291*a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S132;
292be8c453dSColin LeMahieu       break;
293be8c453dSColin LeMahieu     case 11:
294*a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
295*a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S132;
296be8c453dSColin LeMahieu       break;
297be8c453dSColin LeMahieu     case 12:
298*a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
299*a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
300be8c453dSColin LeMahieu       break;
301be8c453dSColin LeMahieu     case 13:
302*a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
303*a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
304be8c453dSColin LeMahieu       break;
305be8c453dSColin LeMahieu     case 14:
306*a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
307*a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S232;
308be8c453dSColin LeMahieu       break;
309be8c453dSColin LeMahieu     }
310*a72fad98SKrzysztof Parzyszek     MI.setOpcode(Hexagon::DuplexIClass0 + duplexIClass);
311be8c453dSColin LeMahieu     MCInst *MILow = new (getContext()) MCInst;
312be8c453dSColin LeMahieu     MCInst *MIHigh = new (getContext()) MCInst;
313*a72fad98SKrzysztof Parzyszek     Result = decodeInstruction(DecodeLow, *MILow, Instruction & 0x1fff, Address,
314*a72fad98SKrzysztof Parzyszek                                this, STI);
315*a72fad98SKrzysztof Parzyszek     if (Result != DecodeStatus::Success)
316*a72fad98SKrzysztof Parzyszek       return DecodeStatus::Fail;
317*a72fad98SKrzysztof Parzyszek     adjustDuplex(*MILow, getContext());
318*a72fad98SKrzysztof Parzyszek     Result = decodeInstruction(
319*a72fad98SKrzysztof Parzyszek         DecodeHigh, *MIHigh, (Instruction >> 16) & 0x1fff, Address, this, STI);
320*a72fad98SKrzysztof Parzyszek     if (Result != DecodeStatus::Success)
321*a72fad98SKrzysztof Parzyszek       return DecodeStatus::Fail;
322*a72fad98SKrzysztof Parzyszek     adjustDuplex(*MIHigh, getContext());
323be8c453dSColin LeMahieu     MCOperand OPLow = MCOperand::createInst(MILow);
324be8c453dSColin LeMahieu     MCOperand OPHigh = MCOperand::createInst(MIHigh);
325be8c453dSColin LeMahieu     MI.addOperand(OPLow);
326be8c453dSColin LeMahieu     MI.addOperand(OPHigh);
327be8c453dSColin LeMahieu     Complete = true;
328be8c453dSColin LeMahieu   } else {
329be8c453dSColin LeMahieu     if ((Instruction & HexagonII::INST_PARSE_MASK) ==
33068d967d9SColin LeMahieu         HexagonII::INST_PARSE_PACKET_END)
33168d967d9SColin LeMahieu       Complete = true;
3327cd08927SColin LeMahieu 
333*a72fad98SKrzysztof Parzyszek     if (Extender != nullptr)
334*a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTableMustExtend32, MI, Instruction,
335*a72fad98SKrzysztof Parzyszek                                  Address, this, STI);
336*a72fad98SKrzysztof Parzyszek 
337*a72fad98SKrzysztof Parzyszek     if (Result != MCDisassembler::Success)
338*a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTable32, MI, Instruction, Address, this,
339*a72fad98SKrzysztof Parzyszek                                  STI);
340*a72fad98SKrzysztof Parzyszek 
341*a72fad98SKrzysztof Parzyszek     if (Result != MCDisassembler::Success &&
342*a72fad98SKrzysztof Parzyszek         STI.getFeatureBits()[Hexagon::ExtensionHVX])
343*a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTableEXT_mmvec32, MI, Instruction,
344*a72fad98SKrzysztof Parzyszek                                  Address, this, STI);
345*a72fad98SKrzysztof Parzyszek 
346be8c453dSColin LeMahieu   }
34768d967d9SColin LeMahieu 
34881707549SColin LeMahieu   switch (MI.getOpcode()) {
34981707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_f_jumpnv_nt:
35081707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_f_jumpnv_t:
35181707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp0_jump_nt:
35281707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp0_jump_t:
35381707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp1_jump_nt:
35481707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp1_jump_t:
35581707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_t_jumpnv_nt:
35681707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_t_jumpnv_t:
35781707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp0_jump_nt:
35881707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp0_jump_t:
35981707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp1_jump_nt:
36081707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp1_jump_t:
36181707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_f_jumpnv_nt:
36281707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_f_jumpnv_t:
36381707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp0_jump_nt:
36481707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp0_jump_t:
36581707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp1_jump_nt:
36681707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp1_jump_t:
36781707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_t_jumpnv_nt:
36881707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_t_jumpnv_t:
36981707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp0_jump_nt:
37081707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp0_jump_t:
37181707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp1_jump_nt:
37281707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp1_jump_t:
373*a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 1,
374*a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, getContext())));
37581707549SColin LeMahieu     break;
37681707549SColin LeMahieu   default:
37781707549SColin LeMahieu     break;
37881707549SColin LeMahieu   }
37981707549SColin LeMahieu 
3807cd08927SColin LeMahieu   if (HexagonMCInstrInfo::isNewValue(*MCII, MI)) {
3817cd08927SColin LeMahieu     unsigned OpIndex = HexagonMCInstrInfo::getNewValueOp(*MCII, MI);
3827cd08927SColin LeMahieu     MCOperand &MCO = MI.getOperand(OpIndex);
3837cd08927SColin LeMahieu     assert(MCO.isReg() && "New value consumers must be registers");
3847cd08927SColin LeMahieu     unsigned Register =
3857cd08927SColin LeMahieu         getContext().getRegisterInfo()->getEncodingValue(MCO.getReg());
3867cd08927SColin LeMahieu     if ((Register & 0x6) == 0)
3877cd08927SColin LeMahieu       // HexagonPRM 10.11 Bit 1-2 == 0 is reserved
3887cd08927SColin LeMahieu       return MCDisassembler::Fail;
3897cd08927SColin LeMahieu     unsigned Lookback = (Register & 0x6) >> 1;
3907cd08927SColin LeMahieu     unsigned Offset = 1;
3917cd08927SColin LeMahieu     bool Vector = HexagonMCInstrInfo::isVector(*MCII, MI);
3927cd08927SColin LeMahieu     auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
3937cd08927SColin LeMahieu     auto i = Instructions.end() - 1;
3947cd08927SColin LeMahieu     for (auto n = Instructions.begin() - 1;; --i, ++Offset) {
3957cd08927SColin LeMahieu       if (i == n)
3967cd08927SColin LeMahieu         // Couldn't find producer
3977cd08927SColin LeMahieu         return MCDisassembler::Fail;
3987cd08927SColin LeMahieu       if (Vector && !HexagonMCInstrInfo::isVector(*MCII, *i->getInst()))
3997cd08927SColin LeMahieu         // Skip scalars when calculating distances for vectors
4007cd08927SColin LeMahieu         ++Lookback;
4017cd08927SColin LeMahieu       if (HexagonMCInstrInfo::isImmext(*i->getInst()))
4027cd08927SColin LeMahieu         ++Lookback;
4037cd08927SColin LeMahieu       if (Offset == Lookback)
4047cd08927SColin LeMahieu         break;
4057cd08927SColin LeMahieu     }
4067cd08927SColin LeMahieu     auto const &Inst = *i->getInst();
4077cd08927SColin LeMahieu     bool SubregBit = (Register & 0x1) != 0;
4087cd08927SColin LeMahieu     if (SubregBit && HexagonMCInstrInfo::hasNewValue2(*MCII, Inst)) {
4097cd08927SColin LeMahieu       // If subreg bit is set we're selecting the second produced newvalue
4107cd08927SColin LeMahieu       unsigned Producer =
4117cd08927SColin LeMahieu           HexagonMCInstrInfo::getNewValueOperand2(*MCII, Inst).getReg();
4127cd08927SColin LeMahieu       assert(Producer != Hexagon::NoRegister);
4137cd08927SColin LeMahieu       MCO.setReg(Producer);
4147cd08927SColin LeMahieu     } else if (HexagonMCInstrInfo::hasNewValue(*MCII, Inst)) {
4157cd08927SColin LeMahieu       unsigned Producer =
4167cd08927SColin LeMahieu           HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg();
4177cd08927SColin LeMahieu       if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
4187cd08927SColin LeMahieu         Producer = ((Producer - Hexagon::W0) << 1) + SubregBit + Hexagon::V0;
4197cd08927SColin LeMahieu       else if (SubregBit)
4206fdfa3dcSColin LeMahieu         // Hexagon PRM 10.11 New-value operands
4216fdfa3dcSColin LeMahieu         // Nt[0] is reserved and should always be encoded as zero.
4227cd08927SColin LeMahieu         return MCDisassembler::Fail;
4237cd08927SColin LeMahieu       assert(Producer != Hexagon::NoRegister);
4247cd08927SColin LeMahieu       MCO.setReg(Producer);
4257cd08927SColin LeMahieu     } else
4267cd08927SColin LeMahieu       return MCDisassembler::Fail;
4277cd08927SColin LeMahieu   }
4287cd08927SColin LeMahieu 
4297cd08927SColin LeMahieu   if (Extender != nullptr) {
430*a72fad98SKrzysztof Parzyszek     MCInst const &Inst = HexagonMCInstrInfo::isDuplex(*MCII, MI)
431*a72fad98SKrzysztof Parzyszek                              ? *MI.getOperand(1).getInst()
432*a72fad98SKrzysztof Parzyszek                              : MI;
4337cd08927SColin LeMahieu     if (!HexagonMCInstrInfo::isExtendable(*MCII, Inst) &&
4347cd08927SColin LeMahieu         !HexagonMCInstrInfo::isExtended(*MCII, Inst))
4357cd08927SColin LeMahieu       return MCDisassembler::Fail;
4367cd08927SColin LeMahieu   }
4375d6f03bdSColin LeMahieu   return Result;
43888ebb9e2SColin LeMahieu }
4397cd08927SColin LeMahieu 
4407cd08927SColin LeMahieu static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,
441e5e035a3SCraig Topper                                         ArrayRef<MCPhysReg> Table) {
4423da000c0SCraig Topper   if (RegNo < Table.size()) {
4437cd08927SColin LeMahieu     Inst.addOperand(MCOperand::createReg(Table[RegNo]));
4447cd08927SColin LeMahieu     return MCDisassembler::Success;
4453da000c0SCraig Topper   }
4463da000c0SCraig Topper 
4477cd08927SColin LeMahieu   return MCDisassembler::Fail;
4487cd08927SColin LeMahieu }
4497cd08927SColin LeMahieu 
4507c958713SColin LeMahieu static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
4517c958713SColin LeMahieu                                                    uint64_t Address,
4527c958713SColin LeMahieu                                                    const void *Decoder) {
4537c958713SColin LeMahieu   return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
4547c958713SColin LeMahieu }
4557c958713SColin LeMahieu 
4567cd08927SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
4577cd08927SColin LeMahieu                                                uint64_t Address,
4587cd08927SColin LeMahieu                                                const void *Decoder) {
459e5e035a3SCraig Topper   static const MCPhysReg IntRegDecoderTable[] = {
4607cd08927SColin LeMahieu       Hexagon::R0,  Hexagon::R1,  Hexagon::R2,  Hexagon::R3,  Hexagon::R4,
4617cd08927SColin LeMahieu       Hexagon::R5,  Hexagon::R6,  Hexagon::R7,  Hexagon::R8,  Hexagon::R9,
4627cd08927SColin LeMahieu       Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
4637cd08927SColin LeMahieu       Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
4647cd08927SColin LeMahieu       Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
4657cd08927SColin LeMahieu       Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
4667cd08927SColin LeMahieu       Hexagon::R30, Hexagon::R31};
4677cd08927SColin LeMahieu 
468e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, IntRegDecoderTable);
4697cd08927SColin LeMahieu }
4707c958713SColin LeMahieu 
471*a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
472*a72fad98SKrzysztof Parzyszek                                                       unsigned RegNo,
473*a72fad98SKrzysztof Parzyszek                                                       uint64_t Address,
474*a72fad98SKrzysztof Parzyszek                                                       const void *Decoder) {
475*a72fad98SKrzysztof Parzyszek   static const MCPhysReg GeneralSubRegDecoderTable[] = {
476*a72fad98SKrzysztof Parzyszek       Hexagon::R0,  Hexagon::R1,  Hexagon::R2,  Hexagon::R3,
477*a72fad98SKrzysztof Parzyszek       Hexagon::R4,  Hexagon::R5,  Hexagon::R6,  Hexagon::R7,
478*a72fad98SKrzysztof Parzyszek       Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
479*a72fad98SKrzysztof Parzyszek       Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23,
480*a72fad98SKrzysztof Parzyszek   };
481*a72fad98SKrzysztof Parzyszek 
482*a72fad98SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, GeneralSubRegDecoderTable);
483*a72fad98SKrzysztof Parzyszek }
484*a72fad98SKrzysztof Parzyszek 
4857c958713SColin LeMahieu static DecodeStatus DecodeVectorRegsRegisterClass(MCInst &Inst, unsigned RegNo,
4867c958713SColin LeMahieu                                                   uint64_t /*Address*/,
4877c958713SColin LeMahieu                                                   const void *Decoder) {
488e5e035a3SCraig Topper   static const MCPhysReg VecRegDecoderTable[] = {
4897c958713SColin LeMahieu       Hexagon::V0,  Hexagon::V1,  Hexagon::V2,  Hexagon::V3,  Hexagon::V4,
4907c958713SColin LeMahieu       Hexagon::V5,  Hexagon::V6,  Hexagon::V7,  Hexagon::V8,  Hexagon::V9,
4917c958713SColin LeMahieu       Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
4927c958713SColin LeMahieu       Hexagon::V15, Hexagon::V16, Hexagon::V17, Hexagon::V18, Hexagon::V19,
4937c958713SColin LeMahieu       Hexagon::V20, Hexagon::V21, Hexagon::V22, Hexagon::V23, Hexagon::V24,
4947c958713SColin LeMahieu       Hexagon::V25, Hexagon::V26, Hexagon::V27, Hexagon::V28, Hexagon::V29,
4957c958713SColin LeMahieu       Hexagon::V30, Hexagon::V31};
4967c958713SColin LeMahieu 
497e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, VecRegDecoderTable);
4987c958713SColin LeMahieu }
4997c958713SColin LeMahieu 
5007cd08927SColin LeMahieu static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5017cd08927SColin LeMahieu                                                   uint64_t /*Address*/,
5027cd08927SColin LeMahieu                                                   const void *Decoder) {
503e5e035a3SCraig Topper   static const MCPhysReg DoubleRegDecoderTable[] = {
5047cd08927SColin LeMahieu       Hexagon::D0,  Hexagon::D1,  Hexagon::D2,  Hexagon::D3,
5057cd08927SColin LeMahieu       Hexagon::D4,  Hexagon::D5,  Hexagon::D6,  Hexagon::D7,
5067cd08927SColin LeMahieu       Hexagon::D8,  Hexagon::D9,  Hexagon::D10, Hexagon::D11,
5077cd08927SColin LeMahieu       Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15};
5087cd08927SColin LeMahieu 
509e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo >> 1, DoubleRegDecoderTable);
5107cd08927SColin LeMahieu }
5117cd08927SColin LeMahieu 
512*a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralDoubleLow8RegsRegisterClass(
513*a72fad98SKrzysztof Parzyszek     MCInst &Inst, unsigned RegNo, uint64_t /*Address*/, const void *Decoder) {
514*a72fad98SKrzysztof Parzyszek   static const MCPhysReg GeneralDoubleLow8RegDecoderTable[] = {
515*a72fad98SKrzysztof Parzyszek       Hexagon::D0, Hexagon::D1, Hexagon::D2,  Hexagon::D3,
516*a72fad98SKrzysztof Parzyszek       Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11};
517*a72fad98SKrzysztof Parzyszek 
518*a72fad98SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, GeneralDoubleLow8RegDecoderTable);
519*a72fad98SKrzysztof Parzyszek }
520*a72fad98SKrzysztof Parzyszek 
5217c958713SColin LeMahieu static DecodeStatus DecodeVecDblRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5227c958713SColin LeMahieu                                                   uint64_t /*Address*/,
5237c958713SColin LeMahieu                                                   const void *Decoder) {
524e5e035a3SCraig Topper   static const MCPhysReg VecDblRegDecoderTable[] = {
5257c958713SColin LeMahieu       Hexagon::W0,  Hexagon::W1,  Hexagon::W2,  Hexagon::W3,
5267c958713SColin LeMahieu       Hexagon::W4,  Hexagon::W5,  Hexagon::W6,  Hexagon::W7,
5277c958713SColin LeMahieu       Hexagon::W8,  Hexagon::W9,  Hexagon::W10, Hexagon::W11,
5287c958713SColin LeMahieu       Hexagon::W12, Hexagon::W13, Hexagon::W14, Hexagon::W15};
5297c958713SColin LeMahieu 
5303da000c0SCraig Topper   return (DecodeRegisterClass(Inst, RegNo >> 1, VecDblRegDecoderTable));
5317c958713SColin LeMahieu }
5327c958713SColin LeMahieu 
5337cd08927SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5347cd08927SColin LeMahieu                                                 uint64_t /*Address*/,
5357cd08927SColin LeMahieu                                                 const void *Decoder) {
536e5e035a3SCraig Topper   static const MCPhysReg PredRegDecoderTable[] = {Hexagon::P0, Hexagon::P1,
5377cd08927SColin LeMahieu                                                   Hexagon::P2, Hexagon::P3};
5387cd08927SColin LeMahieu 
539e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, PredRegDecoderTable);
5407cd08927SColin LeMahieu }
5417cd08927SColin LeMahieu 
5427c958713SColin LeMahieu static DecodeStatus DecodeVecPredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5437c958713SColin LeMahieu                                                    uint64_t /*Address*/,
5447c958713SColin LeMahieu                                                    const void *Decoder) {
545e5e035a3SCraig Topper   static const MCPhysReg VecPredRegDecoderTable[] = {Hexagon::Q0, Hexagon::Q1,
5467c958713SColin LeMahieu                                                      Hexagon::Q2, Hexagon::Q3};
5477c958713SColin LeMahieu 
548e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, VecPredRegDecoderTable);
5497c958713SColin LeMahieu }
550be8c453dSColin LeMahieu 
5517cd08927SColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5527cd08927SColin LeMahieu                                                uint64_t /*Address*/,
5537cd08927SColin LeMahieu                                                const void *Decoder) {
554e5e035a3SCraig Topper   static const MCPhysReg CtrlRegDecoderTable[] = {
555*a72fad98SKrzysztof Parzyszek     Hexagon::SA0,        Hexagon::LC0,        Hexagon::SA1,
556*a72fad98SKrzysztof Parzyszek     Hexagon::LC1,        Hexagon::P3_0,       Hexagon::C5,
557*a72fad98SKrzysztof Parzyszek     Hexagon::C6,         Hexagon::C7,         Hexagon::USR,
558*a72fad98SKrzysztof Parzyszek     Hexagon::PC,         Hexagon::UGP,        Hexagon::GP,
559*a72fad98SKrzysztof Parzyszek     Hexagon::CS0,        Hexagon::CS1,        Hexagon::UPCL,
560*a72fad98SKrzysztof Parzyszek     Hexagon::UPC
5617cd08927SColin LeMahieu   };
5627cd08927SColin LeMahieu 
5636261e1b9SCraig Topper   if (RegNo >= array_lengthof(CtrlRegDecoderTable))
5647cd08927SColin LeMahieu     return MCDisassembler::Fail;
5657cd08927SColin LeMahieu 
5667cd08927SColin LeMahieu   if (CtrlRegDecoderTable[RegNo] == Hexagon::NoRegister)
5677cd08927SColin LeMahieu     return MCDisassembler::Fail;
5687cd08927SColin LeMahieu 
5697cd08927SColin LeMahieu   unsigned Register = CtrlRegDecoderTable[RegNo];
5707cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
5717cd08927SColin LeMahieu   return MCDisassembler::Success;
5727cd08927SColin LeMahieu }
5737cd08927SColin LeMahieu 
5747cd08927SColin LeMahieu static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
5757cd08927SColin LeMahieu                                                  uint64_t /*Address*/,
5767cd08927SColin LeMahieu                                                  const void *Decoder) {
577e5e035a3SCraig Topper   static const MCPhysReg CtrlReg64DecoderTable[] = {
578*a72fad98SKrzysztof Parzyszek     Hexagon::C1_0,       Hexagon::NoRegister, Hexagon::C3_2,
579*a72fad98SKrzysztof Parzyszek     Hexagon::NoRegister,
580*a72fad98SKrzysztof Parzyszek     Hexagon::C7_6,       Hexagon::NoRegister, Hexagon::C9_8,
581*a72fad98SKrzysztof Parzyszek     Hexagon::NoRegister, Hexagon::C11_10,     Hexagon::NoRegister,
582*a72fad98SKrzysztof Parzyszek     Hexagon::CS,         Hexagon::NoRegister, Hexagon::UPC,
583*a72fad98SKrzysztof Parzyszek     Hexagon::NoRegister
5847cd08927SColin LeMahieu   };
5857cd08927SColin LeMahieu 
5866261e1b9SCraig Topper   if (RegNo >= array_lengthof(CtrlReg64DecoderTable))
5877cd08927SColin LeMahieu     return MCDisassembler::Fail;
5887cd08927SColin LeMahieu 
5897cd08927SColin LeMahieu   if (CtrlReg64DecoderTable[RegNo] == Hexagon::NoRegister)
5907cd08927SColin LeMahieu     return MCDisassembler::Fail;
5917cd08927SColin LeMahieu 
5927cd08927SColin LeMahieu   unsigned Register = CtrlReg64DecoderTable[RegNo];
5937cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
5947cd08927SColin LeMahieu   return MCDisassembler::Success;
5957cd08927SColin LeMahieu }
5967cd08927SColin LeMahieu 
5977cd08927SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5987cd08927SColin LeMahieu                                                uint64_t /*Address*/,
5997cd08927SColin LeMahieu                                                const void *Decoder) {
6007cd08927SColin LeMahieu   unsigned Register = 0;
6017cd08927SColin LeMahieu   switch (RegNo) {
6027cd08927SColin LeMahieu   case 0:
6037cd08927SColin LeMahieu     Register = Hexagon::M0;
6047cd08927SColin LeMahieu     break;
6057cd08927SColin LeMahieu   case 1:
6067cd08927SColin LeMahieu     Register = Hexagon::M1;
6077cd08927SColin LeMahieu     break;
6087cd08927SColin LeMahieu   default:
6097cd08927SColin LeMahieu     return MCDisassembler::Fail;
6107cd08927SColin LeMahieu   }
6117cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
6127cd08927SColin LeMahieu   return MCDisassembler::Success;
6137cd08927SColin LeMahieu }
6147cd08927SColin LeMahieu 
6157cd08927SColin LeMahieu static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
6167cd08927SColin LeMahieu                                        uint64_t /*Address*/,
6177cd08927SColin LeMahieu                                        const void *Decoder) {
6187cd08927SColin LeMahieu   HexagonDisassembler const &Disassembler = disassembler(Decoder);
619*a72fad98SKrzysztof Parzyszek   int64_t FullValue =
620*a72fad98SKrzysztof Parzyszek       fullValue(*Disassembler.MCII, **Disassembler.CurrentBundle, MI, tmp);
6217cd08927SColin LeMahieu   assert(FullValue >= 0 && "Negative in unsigned decoder");
6227cd08927SColin LeMahieu   HexagonMCInstrInfo::addConstant(MI, FullValue, Disassembler.getContext());
6237cd08927SColin LeMahieu   return MCDisassembler::Success;
6247cd08927SColin LeMahieu }
6257cd08927SColin LeMahieu 
6267c958713SColin LeMahieu static DecodeStatus s4_6ImmDecoder(MCInst &MI, unsigned tmp,
6277c958713SColin LeMahieu                                    uint64_t /*Address*/, const void *Decoder) {
6287cd08927SColin LeMahieu   signedDecoder<10>(MI, tmp, Decoder);
6297c958713SColin LeMahieu   return MCDisassembler::Success;
6307c958713SColin LeMahieu }
6317c958713SColin LeMahieu 
6327c958713SColin LeMahieu static DecodeStatus s3_6ImmDecoder(MCInst &MI, unsigned tmp,
6337c958713SColin LeMahieu                                    uint64_t /*Address*/, const void *Decoder) {
6347cd08927SColin LeMahieu   signedDecoder<19>(MI, tmp, Decoder);
6357c958713SColin LeMahieu   return MCDisassembler::Success;
6367c958713SColin LeMahieu }
6377c958713SColin LeMahieu 
638*a72fad98SKrzysztof Parzyszek static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
639*a72fad98SKrzysztof Parzyszek                                     uint64_t /*Address*/, const void *Decoder) {
640*a72fad98SKrzysztof Parzyszek   HexagonDisassembler const &Disassembler = disassembler(Decoder);
641*a72fad98SKrzysztof Parzyszek   unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
642*a72fad98SKrzysztof Parzyszek   tmp = SignExtend64(tmp, Bits);
643*a72fad98SKrzysztof Parzyszek   signedDecoder<32>(MI, tmp, Decoder);
644*a72fad98SKrzysztof Parzyszek   return MCDisassembler::Success;
645*a72fad98SKrzysztof Parzyszek }
646*a72fad98SKrzysztof Parzyszek 
6477cd08927SColin LeMahieu // custom decoder for various jump/call immediates
6487cd08927SColin LeMahieu static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
6497cd08927SColin LeMahieu                                     const void *Decoder) {
6507cd08927SColin LeMahieu   HexagonDisassembler const &Disassembler = disassembler(Decoder);
6517cd08927SColin LeMahieu   unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
6527cd08927SColin LeMahieu   // r13_2 is not extendable, so if there are no extent bits, it's r13_2
6537cd08927SColin LeMahieu   if (Bits == 0)
6547cd08927SColin LeMahieu     Bits = 15;
655*a72fad98SKrzysztof Parzyszek   uint32_t FullValue =
656*a72fad98SKrzysztof Parzyszek       fullValue(*Disassembler.MCII, **Disassembler.CurrentBundle, MI,
657*a72fad98SKrzysztof Parzyszek                 SignExtend64(tmp, Bits));
6587cd08927SColin LeMahieu   int64_t Extended = SignExtend64<32>(FullValue) + Address;
659*a72fad98SKrzysztof Parzyszek   if (!Disassembler.tryAddingSymbolicOperand(MI, Extended, Address, true, 0, 4))
6607cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
6617cd08927SColin LeMahieu   return MCDisassembler::Success;
6627cd08927SColin LeMahieu }
6637cd08927SColin LeMahieu 
6647cd08927SColin LeMahieu 
665