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 
63a72fad98SKrzysztof Parzyszek namespace {
64a72fad98SKrzysztof Parzyszek   uint32_t fullValue(MCInstrInfo const &MCII, MCInst &MCB, MCInst &MI,
65a72fad98SKrzysztof Parzyszek                      int64_t Value) {
66a72fad98SKrzysztof Parzyszek     MCInst const *Extender = HexagonMCInstrInfo::extenderForIndex(
67a72fad98SKrzysztof Parzyszek       MCB, HexagonMCInstrInfo::bundleSize(MCB));
68a72fad98SKrzysztof Parzyszek     if (!Extender || MI.size() != HexagonMCInstrInfo::getExtendableOp(MCII, MI))
69a72fad98SKrzysztof Parzyszek       return Value;
70a72fad98SKrzysztof Parzyszek     unsigned Alignment = HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
71a72fad98SKrzysztof Parzyszek     uint32_t Lower6 = static_cast<uint32_t>(Value >> Alignment) & 0x3f;
72a72fad98SKrzysztof Parzyszek     int64_t Bits;
73a72fad98SKrzysztof Parzyszek     bool Success = Extender->getOperand(0).getExpr()->evaluateAsAbsolute(Bits);
74a72fad98SKrzysztof Parzyszek     assert(Success); (void)Success;
75a72fad98SKrzysztof Parzyszek     uint32_t Upper26 = static_cast<uint32_t>(Bits);
76a72fad98SKrzysztof Parzyszek     uint32_t Operand = Upper26 | Lower6;
77a72fad98SKrzysztof Parzyszek     return Operand;
78a72fad98SKrzysztof Parzyszek   }
79a72fad98SKrzysztof Parzyszek   HexagonDisassembler const &disassembler(void const *Decoder) {
80a72fad98SKrzysztof Parzyszek     return *static_cast<HexagonDisassembler const *>(Decoder);
81a72fad98SKrzysztof Parzyszek   }
82a72fad98SKrzysztof Parzyszek   template <size_t T>
83a72fad98SKrzysztof Parzyszek   void signedDecoder(MCInst &MI, unsigned tmp, const void *Decoder) {
84a72fad98SKrzysztof Parzyszek     HexagonDisassembler const &Disassembler = disassembler(Decoder);
85a72fad98SKrzysztof Parzyszek     int64_t FullValue =
86a72fad98SKrzysztof Parzyszek         fullValue(*Disassembler.MCII, **Disassembler.CurrentBundle, MI,
87a72fad98SKrzysztof Parzyszek                   SignExtend64<T>(tmp));
88a72fad98SKrzysztof Parzyszek     int64_t Extended = SignExtend64<32>(FullValue);
89a72fad98SKrzysztof Parzyszek     HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
90a72fad98SKrzysztof Parzyszek   }
91a72fad98SKrzysztof 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);
100a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
101a72fad98SKrzysztof Parzyszek                                                       unsigned RegNo,
102a72fad98SKrzysztof Parzyszek                                                       uint64_t Address,
103a72fad98SKrzysztof 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);
113a72fad98SKrzysztof Parzyszek static DecodeStatus
114a72fad98SKrzysztof Parzyszek DecodeGeneralDoubleLow8RegsRegisterClass(MCInst &Inst, unsigned RegNo,
115a72fad98SKrzysztof 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);
137a72fad98SKrzysztof Parzyszek static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
138a72fad98SKrzysztof 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 
158a72fad98SKrzysztof 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;
194a72fad98SKrzysztof Parzyszek   if (Size > HEXAGON_MAX_PACKET_SIZE)
195a72fad98SKrzysztof 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 
202a72fad98SKrzysztof Parzyszek namespace {
203a72fad98SKrzysztof Parzyszek void adjustDuplex(MCInst &MI, MCContext &Context) {
204a72fad98SKrzysztof Parzyszek   switch (MI.getOpcode()) {
205a72fad98SKrzysztof Parzyszek   case Hexagon::SA1_setin1:
206a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 1,
207a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
208a72fad98SKrzysztof Parzyszek     break;
209a72fad98SKrzysztof Parzyszek   case Hexagon::SA1_dec:
210a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 2,
211a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
212a72fad98SKrzysztof Parzyszek     break;
213a72fad98SKrzysztof Parzyszek   default:
214a72fad98SKrzysztof Parzyszek     break;
2157cd08927SColin LeMahieu   }
216a72fad98SKrzysztof 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 
224a72fad98SKrzysztof 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 
237a72fad98SKrzysztof Parzyszek   MCInst const *Extender = HexagonMCInstrInfo::extenderForIndex(
238a72fad98SKrzysztof Parzyszek       MCB, HexagonMCInstrInfo::bundleSize(MCB));
239a72fad98SKrzysztof Parzyszek 
240a72fad98SKrzysztof Parzyszek   DecodeStatus Result = DecodeStatus::Fail;
24168d967d9SColin LeMahieu   if ((Instruction & HexagonII::INST_PARSE_MASK) ==
242be8c453dSColin LeMahieu       HexagonII::INST_PARSE_DUPLEX) {
243a72fad98SKrzysztof Parzyszek     unsigned duplexIClass;
244a72fad98SKrzysztof 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:
250a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L132;
251a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
252be8c453dSColin LeMahieu       break;
253be8c453dSColin LeMahieu     case 1:
254a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
255a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
256be8c453dSColin LeMahieu       break;
257be8c453dSColin LeMahieu     case 2:
258a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
259a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
260be8c453dSColin LeMahieu       break;
261be8c453dSColin LeMahieu     case 3:
262a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_A32;
263a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
264be8c453dSColin LeMahieu       break;
265be8c453dSColin LeMahieu     case 4:
266a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L132;
267a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
268be8c453dSColin LeMahieu       break;
269be8c453dSColin LeMahieu     case 5:
270a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
271a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
272be8c453dSColin LeMahieu       break;
273be8c453dSColin LeMahieu     case 6:
274a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
275a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
276be8c453dSColin LeMahieu       break;
277be8c453dSColin LeMahieu     case 7:
278a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
279a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
280be8c453dSColin LeMahieu       break;
281be8c453dSColin LeMahieu     case 8:
282a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
283a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
284be8c453dSColin LeMahieu       break;
285be8c453dSColin LeMahieu     case 9:
286a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
287a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
288be8c453dSColin LeMahieu       break;
289be8c453dSColin LeMahieu     case 10:
290a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
291a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S132;
292be8c453dSColin LeMahieu       break;
293be8c453dSColin LeMahieu     case 11:
294a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
295a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S132;
296be8c453dSColin LeMahieu       break;
297be8c453dSColin LeMahieu     case 12:
298a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
299a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
300be8c453dSColin LeMahieu       break;
301be8c453dSColin LeMahieu     case 13:
302a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
303a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
304be8c453dSColin LeMahieu       break;
305be8c453dSColin LeMahieu     case 14:
306a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
307a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S232;
308be8c453dSColin LeMahieu       break;
309be8c453dSColin LeMahieu     }
310a72fad98SKrzysztof Parzyszek     MI.setOpcode(Hexagon::DuplexIClass0 + duplexIClass);
311be8c453dSColin LeMahieu     MCInst *MILow = new (getContext()) MCInst;
312be8c453dSColin LeMahieu     MCInst *MIHigh = new (getContext()) MCInst;
313a72fad98SKrzysztof Parzyszek     Result = decodeInstruction(DecodeLow, *MILow, Instruction & 0x1fff, Address,
314a72fad98SKrzysztof Parzyszek                                this, STI);
315a72fad98SKrzysztof Parzyszek     if (Result != DecodeStatus::Success)
316a72fad98SKrzysztof Parzyszek       return DecodeStatus::Fail;
317a72fad98SKrzysztof Parzyszek     adjustDuplex(*MILow, getContext());
318a72fad98SKrzysztof Parzyszek     Result = decodeInstruction(
319a72fad98SKrzysztof Parzyszek         DecodeHigh, *MIHigh, (Instruction >> 16) & 0x1fff, Address, this, STI);
320a72fad98SKrzysztof Parzyszek     if (Result != DecodeStatus::Success)
321a72fad98SKrzysztof Parzyszek       return DecodeStatus::Fail;
322a72fad98SKrzysztof 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 
333a72fad98SKrzysztof Parzyszek     if (Extender != nullptr)
334a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTableMustExtend32, MI, Instruction,
335a72fad98SKrzysztof Parzyszek                                  Address, this, STI);
336a72fad98SKrzysztof Parzyszek 
337a72fad98SKrzysztof Parzyszek     if (Result != MCDisassembler::Success)
338a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTable32, MI, Instruction, Address, this,
339a72fad98SKrzysztof Parzyszek                                  STI);
340a72fad98SKrzysztof Parzyszek 
341a72fad98SKrzysztof Parzyszek     if (Result != MCDisassembler::Success &&
342a72fad98SKrzysztof Parzyszek         STI.getFeatureBits()[Hexagon::ExtensionHVX])
343a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTableEXT_mmvec32, MI, Instruction,
344a72fad98SKrzysztof Parzyszek                                  Address, this, STI);
345a72fad98SKrzysztof 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:
373a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 1,
374a72fad98SKrzysztof 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) {
430a72fad98SKrzysztof Parzyszek     MCInst const &Inst = HexagonMCInstrInfo::isDuplex(*MCII, MI)
431a72fad98SKrzysztof Parzyszek                              ? *MI.getOperand(1).getInst()
432a72fad98SKrzysztof 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 
471a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
472a72fad98SKrzysztof Parzyszek                                                       unsigned RegNo,
473a72fad98SKrzysztof Parzyszek                                                       uint64_t Address,
474a72fad98SKrzysztof Parzyszek                                                       const void *Decoder) {
475a72fad98SKrzysztof Parzyszek   static const MCPhysReg GeneralSubRegDecoderTable[] = {
476a72fad98SKrzysztof Parzyszek       Hexagon::R0,  Hexagon::R1,  Hexagon::R2,  Hexagon::R3,
477a72fad98SKrzysztof Parzyszek       Hexagon::R4,  Hexagon::R5,  Hexagon::R6,  Hexagon::R7,
478a72fad98SKrzysztof Parzyszek       Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
479a72fad98SKrzysztof Parzyszek       Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23,
480a72fad98SKrzysztof Parzyszek   };
481a72fad98SKrzysztof Parzyszek 
482a72fad98SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, GeneralSubRegDecoderTable);
483a72fad98SKrzysztof Parzyszek }
484a72fad98SKrzysztof 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 
512a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralDoubleLow8RegsRegisterClass(
513a72fad98SKrzysztof Parzyszek     MCInst &Inst, unsigned RegNo, uint64_t /*Address*/, const void *Decoder) {
514a72fad98SKrzysztof Parzyszek   static const MCPhysReg GeneralDoubleLow8RegDecoderTable[] = {
515a72fad98SKrzysztof Parzyszek       Hexagon::D0, Hexagon::D1, Hexagon::D2,  Hexagon::D3,
516a72fad98SKrzysztof Parzyszek       Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11};
517a72fad98SKrzysztof Parzyszek 
518a72fad98SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, GeneralDoubleLow8RegDecoderTable);
519a72fad98SKrzysztof Parzyszek }
520a72fad98SKrzysztof 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) {
554*f9015e62SKrzysztof Parzyszek   using namespace Hexagon;
555e5e035a3SCraig Topper   static const MCPhysReg CtrlRegDecoderTable[] = {
556*f9015e62SKrzysztof Parzyszek     /*  0 */  SA0,        LC0,        SA1,        LC1,
557*f9015e62SKrzysztof Parzyszek     /*  4 */  P3_0,       C5,         C6,         C7,
558*f9015e62SKrzysztof Parzyszek     /*  8 */  USR,        PC,         UGP,        GP,
559*f9015e62SKrzysztof Parzyszek     /* 12 */  CS0,        CS1,        UPCL,       UPCH,
560*f9015e62SKrzysztof Parzyszek     /* 16 */  FRAMELIMIT, FRAMEKEY,   PKTCOUNTLO, PKTCOUNTHI,
561*f9015e62SKrzysztof Parzyszek     /* 20 */  0,          0,          0,          0,
562*f9015e62SKrzysztof Parzyszek     /* 24 */  0,          0,          0,          0,
563*f9015e62SKrzysztof Parzyszek     /* 28 */  0,          0,          UTIMERLO,   UTIMERHI
5647cd08927SColin LeMahieu   };
5657cd08927SColin LeMahieu 
5666261e1b9SCraig Topper   if (RegNo >= array_lengthof(CtrlRegDecoderTable))
5677cd08927SColin LeMahieu     return MCDisassembler::Fail;
5687cd08927SColin LeMahieu 
569*f9015e62SKrzysztof Parzyszek   static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
570*f9015e62SKrzysztof Parzyszek   if (CtrlRegDecoderTable[RegNo] == NoRegister)
5717cd08927SColin LeMahieu     return MCDisassembler::Fail;
5727cd08927SColin LeMahieu 
5737cd08927SColin LeMahieu   unsigned Register = CtrlRegDecoderTable[RegNo];
5747cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
5757cd08927SColin LeMahieu   return MCDisassembler::Success;
5767cd08927SColin LeMahieu }
5777cd08927SColin LeMahieu 
5787cd08927SColin LeMahieu static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
5797cd08927SColin LeMahieu                                                  uint64_t /*Address*/,
5807cd08927SColin LeMahieu                                                  const void *Decoder) {
581*f9015e62SKrzysztof Parzyszek   using namespace Hexagon;
582e5e035a3SCraig Topper   static const MCPhysReg CtrlReg64DecoderTable[] = {
583*f9015e62SKrzysztof Parzyszek     /*  0 */  C1_0,       0,          C3_2,       0,
584*f9015e62SKrzysztof Parzyszek     /*  4 */  C5_4,       0,          C7_6,       0,
585*f9015e62SKrzysztof Parzyszek     /*  8 */  C9_8,       0,          C11_10,     0,
586*f9015e62SKrzysztof Parzyszek     /* 12 */  CS,         0,          UPC,        0,
587*f9015e62SKrzysztof Parzyszek     /* 16 */  C17_16,     0,          PKTCOUNT,   0,
588*f9015e62SKrzysztof Parzyszek     /* 20 */  0,          0,          0,          0,
589*f9015e62SKrzysztof Parzyszek     /* 24 */  0,          0,          0,          0,
590*f9015e62SKrzysztof Parzyszek     /* 28 */  0,          0,          UTIMER,     0
5917cd08927SColin LeMahieu   };
5927cd08927SColin LeMahieu 
5936261e1b9SCraig Topper   if (RegNo >= array_lengthof(CtrlReg64DecoderTable))
5947cd08927SColin LeMahieu     return MCDisassembler::Fail;
5957cd08927SColin LeMahieu 
596*f9015e62SKrzysztof Parzyszek   static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
597*f9015e62SKrzysztof Parzyszek   if (CtrlReg64DecoderTable[RegNo] == NoRegister)
5987cd08927SColin LeMahieu     return MCDisassembler::Fail;
5997cd08927SColin LeMahieu 
6007cd08927SColin LeMahieu   unsigned Register = CtrlReg64DecoderTable[RegNo];
6017cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
6027cd08927SColin LeMahieu   return MCDisassembler::Success;
6037cd08927SColin LeMahieu }
6047cd08927SColin LeMahieu 
6057cd08927SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
6067cd08927SColin LeMahieu                                                uint64_t /*Address*/,
6077cd08927SColin LeMahieu                                                const void *Decoder) {
6087cd08927SColin LeMahieu   unsigned Register = 0;
6097cd08927SColin LeMahieu   switch (RegNo) {
6107cd08927SColin LeMahieu   case 0:
6117cd08927SColin LeMahieu     Register = Hexagon::M0;
6127cd08927SColin LeMahieu     break;
6137cd08927SColin LeMahieu   case 1:
6147cd08927SColin LeMahieu     Register = Hexagon::M1;
6157cd08927SColin LeMahieu     break;
6167cd08927SColin LeMahieu   default:
6177cd08927SColin LeMahieu     return MCDisassembler::Fail;
6187cd08927SColin LeMahieu   }
6197cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
6207cd08927SColin LeMahieu   return MCDisassembler::Success;
6217cd08927SColin LeMahieu }
6227cd08927SColin LeMahieu 
6237cd08927SColin LeMahieu static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
6247cd08927SColin LeMahieu                                        uint64_t /*Address*/,
6257cd08927SColin LeMahieu                                        const void *Decoder) {
6267cd08927SColin LeMahieu   HexagonDisassembler const &Disassembler = disassembler(Decoder);
627a72fad98SKrzysztof Parzyszek   int64_t FullValue =
628a72fad98SKrzysztof Parzyszek       fullValue(*Disassembler.MCII, **Disassembler.CurrentBundle, MI, tmp);
6297cd08927SColin LeMahieu   assert(FullValue >= 0 && "Negative in unsigned decoder");
6307cd08927SColin LeMahieu   HexagonMCInstrInfo::addConstant(MI, FullValue, Disassembler.getContext());
6317cd08927SColin LeMahieu   return MCDisassembler::Success;
6327cd08927SColin LeMahieu }
6337cd08927SColin LeMahieu 
6347c958713SColin LeMahieu static DecodeStatus s4_6ImmDecoder(MCInst &MI, unsigned tmp,
6357c958713SColin LeMahieu                                    uint64_t /*Address*/, const void *Decoder) {
6367cd08927SColin LeMahieu   signedDecoder<10>(MI, tmp, Decoder);
6377c958713SColin LeMahieu   return MCDisassembler::Success;
6387c958713SColin LeMahieu }
6397c958713SColin LeMahieu 
6407c958713SColin LeMahieu static DecodeStatus s3_6ImmDecoder(MCInst &MI, unsigned tmp,
6417c958713SColin LeMahieu                                    uint64_t /*Address*/, const void *Decoder) {
6427cd08927SColin LeMahieu   signedDecoder<19>(MI, tmp, Decoder);
6437c958713SColin LeMahieu   return MCDisassembler::Success;
6447c958713SColin LeMahieu }
6457c958713SColin LeMahieu 
646a72fad98SKrzysztof Parzyszek static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
647a72fad98SKrzysztof Parzyszek                                     uint64_t /*Address*/, const void *Decoder) {
648a72fad98SKrzysztof Parzyszek   HexagonDisassembler const &Disassembler = disassembler(Decoder);
649a72fad98SKrzysztof Parzyszek   unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
650a72fad98SKrzysztof Parzyszek   tmp = SignExtend64(tmp, Bits);
651a72fad98SKrzysztof Parzyszek   signedDecoder<32>(MI, tmp, Decoder);
652a72fad98SKrzysztof Parzyszek   return MCDisassembler::Success;
653a72fad98SKrzysztof Parzyszek }
654a72fad98SKrzysztof Parzyszek 
6557cd08927SColin LeMahieu // custom decoder for various jump/call immediates
6567cd08927SColin LeMahieu static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
6577cd08927SColin LeMahieu                                     const void *Decoder) {
6587cd08927SColin LeMahieu   HexagonDisassembler const &Disassembler = disassembler(Decoder);
6597cd08927SColin LeMahieu   unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
6607cd08927SColin LeMahieu   // r13_2 is not extendable, so if there are no extent bits, it's r13_2
6617cd08927SColin LeMahieu   if (Bits == 0)
6627cd08927SColin LeMahieu     Bits = 15;
663a72fad98SKrzysztof Parzyszek   uint32_t FullValue =
664a72fad98SKrzysztof Parzyszek       fullValue(*Disassembler.MCII, **Disassembler.CurrentBundle, MI,
665a72fad98SKrzysztof Parzyszek                 SignExtend64(tmp, Bits));
6667cd08927SColin LeMahieu   int64_t Extended = SignExtend64<32>(FullValue) + Address;
667a72fad98SKrzysztof Parzyszek   if (!Disassembler.tryAddingSymbolicOperand(MI, Extended, Address, true, 0, 4))
6687cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
6697cd08927SColin LeMahieu   return MCDisassembler::Success;
6707cd08927SColin LeMahieu }
6717cd08927SColin LeMahieu 
6727cd08927SColin LeMahieu 
673