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);
151*058abf1aSKrzysztof Parzyszek static DecodeStatus s3_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1527c958713SColin LeMahieu                                    const void *Decoder);
1537cd08927SColin LeMahieu static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1547cd08927SColin LeMahieu                                     const void *Decoder);
155efa74e02SColin LeMahieu 
156a72fad98SKrzysztof Parzyszek #include "HexagonDepDecoders.h"
15788ebb9e2SColin LeMahieu #include "HexagonGenDisassemblerTables.inc"
15888ebb9e2SColin LeMahieu 
1597cd08927SColin LeMahieu static MCDisassembler *createHexagonDisassembler(const Target &T,
1607cd08927SColin LeMahieu                                                  const MCSubtargetInfo &STI,
16188ebb9e2SColin LeMahieu                                                  MCContext &Ctx) {
1627cd08927SColin LeMahieu   return new HexagonDisassembler(STI, Ctx, T.createMCInstrInfo());
16388ebb9e2SColin LeMahieu }
16488ebb9e2SColin LeMahieu 
16588ebb9e2SColin LeMahieu extern "C" void LLVMInitializeHexagonDisassembler() {
166f42454b9SMehdi Amini   TargetRegistry::RegisterMCDisassembler(getTheHexagonTarget(),
16788ebb9e2SColin LeMahieu                                          createHexagonDisassembler);
16888ebb9e2SColin LeMahieu }
16988ebb9e2SColin LeMahieu 
17088ebb9e2SColin LeMahieu DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
1717fc5b874SRafael Espindola                                                  ArrayRef<uint8_t> Bytes,
17288ebb9e2SColin LeMahieu                                                  uint64_t Address,
17388ebb9e2SColin LeMahieu                                                  raw_ostream &os,
17488ebb9e2SColin LeMahieu                                                  raw_ostream &cs) const {
17568d967d9SColin LeMahieu   DecodeStatus Result = DecodeStatus::Success;
17668d967d9SColin LeMahieu   bool Complete = false;
17768d967d9SColin LeMahieu   Size = 0;
1784aa6bea7SRafael Espindola 
17968d967d9SColin LeMahieu   *CurrentBundle = &MI;
180f0af6e52SColin LeMahieu   MI = HexagonMCInstrInfo::createBundle();
18182085927SEugene Zelenko   while (Result == Success && !Complete) {
18268d967d9SColin LeMahieu     if (Bytes.size() < HEXAGON_INSTR_SIZE)
18368d967d9SColin LeMahieu       return MCDisassembler::Fail;
18468d967d9SColin LeMahieu     MCInst *Inst = new (getContext()) MCInst;
18568d967d9SColin LeMahieu     Result = getSingleInstruction(*Inst, MI, Bytes, Address, os, cs, Complete);
18668d967d9SColin LeMahieu     MI.addOperand(MCOperand::createInst(Inst));
18768d967d9SColin LeMahieu     Size += HEXAGON_INSTR_SIZE;
18868d967d9SColin LeMahieu     Bytes = Bytes.slice(HEXAGON_INSTR_SIZE);
18968d967d9SColin LeMahieu   }
1907cd08927SColin LeMahieu   if (Result == MCDisassembler::Fail)
19168d967d9SColin LeMahieu     return Result;
192a72fad98SKrzysztof Parzyszek   if (Size > HEXAGON_MAX_PACKET_SIZE)
193a72fad98SKrzysztof Parzyszek     return MCDisassembler::Fail;
1947cd08927SColin LeMahieu   HexagonMCChecker Checker(*MCII, STI, MI, MI, *getContext().getRegisterInfo());
1957cd08927SColin LeMahieu   if (!Checker.check())
1967cd08927SColin LeMahieu     return MCDisassembler::Fail;
1977cd08927SColin LeMahieu   return MCDisassembler::Success;
1987cd08927SColin LeMahieu }
1997cd08927SColin LeMahieu 
200a72fad98SKrzysztof Parzyszek namespace {
201a72fad98SKrzysztof Parzyszek void adjustDuplex(MCInst &MI, MCContext &Context) {
202a72fad98SKrzysztof Parzyszek   switch (MI.getOpcode()) {
203a72fad98SKrzysztof Parzyszek   case Hexagon::SA1_setin1:
204a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 1,
205a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
206a72fad98SKrzysztof Parzyszek     break;
207a72fad98SKrzysztof Parzyszek   case Hexagon::SA1_dec:
208a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 2,
209a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
210a72fad98SKrzysztof Parzyszek     break;
211a72fad98SKrzysztof Parzyszek   default:
212a72fad98SKrzysztof Parzyszek     break;
2137cd08927SColin LeMahieu   }
214a72fad98SKrzysztof Parzyszek }
2157cd08927SColin LeMahieu }
21668d967d9SColin LeMahieu 
21768d967d9SColin LeMahieu DecodeStatus HexagonDisassembler::getSingleInstruction(
21868d967d9SColin LeMahieu     MCInst &MI, MCInst &MCB, ArrayRef<uint8_t> Bytes, uint64_t Address,
21968d967d9SColin LeMahieu     raw_ostream &os, raw_ostream &cs, bool &Complete) const {
22068d967d9SColin LeMahieu   assert(Bytes.size() >= HEXAGON_INSTR_SIZE);
22168d967d9SColin LeMahieu 
222a72fad98SKrzysztof Parzyszek   uint32_t Instruction = support::endian::read32le(Bytes.data());
22388ebb9e2SColin LeMahieu 
22468d967d9SColin LeMahieu   auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB);
22568d967d9SColin LeMahieu   if ((Instruction & HexagonII::INST_PARSE_MASK) ==
22668d967d9SColin LeMahieu       HexagonII::INST_PARSE_LOOP_END) {
22768d967d9SColin LeMahieu     if (BundleSize == 0)
22868d967d9SColin LeMahieu       HexagonMCInstrInfo::setInnerLoop(MCB);
22968d967d9SColin LeMahieu     else if (BundleSize == 1)
23068d967d9SColin LeMahieu       HexagonMCInstrInfo::setOuterLoop(MCB);
23168d967d9SColin LeMahieu     else
23268d967d9SColin LeMahieu       return DecodeStatus::Fail;
23368d967d9SColin LeMahieu   }
23468d967d9SColin LeMahieu 
235a72fad98SKrzysztof Parzyszek   MCInst const *Extender = HexagonMCInstrInfo::extenderForIndex(
236a72fad98SKrzysztof Parzyszek       MCB, HexagonMCInstrInfo::bundleSize(MCB));
237a72fad98SKrzysztof Parzyszek 
238a72fad98SKrzysztof Parzyszek   DecodeStatus Result = DecodeStatus::Fail;
23968d967d9SColin LeMahieu   if ((Instruction & HexagonII::INST_PARSE_MASK) ==
240be8c453dSColin LeMahieu       HexagonII::INST_PARSE_DUPLEX) {
241a72fad98SKrzysztof Parzyszek     unsigned duplexIClass;
242a72fad98SKrzysztof Parzyszek     uint8_t const *DecodeLow, *DecodeHigh;
243be8c453dSColin LeMahieu     duplexIClass = ((Instruction >> 28) & 0xe) | ((Instruction >> 13) & 0x1);
244be8c453dSColin LeMahieu     switch (duplexIClass) {
245be8c453dSColin LeMahieu     default:
246be8c453dSColin LeMahieu       return MCDisassembler::Fail;
247be8c453dSColin LeMahieu     case 0:
248a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L132;
249a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
250be8c453dSColin LeMahieu       break;
251be8c453dSColin LeMahieu     case 1:
252a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
253a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
254be8c453dSColin LeMahieu       break;
255be8c453dSColin LeMahieu     case 2:
256a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
257a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
258be8c453dSColin LeMahieu       break;
259be8c453dSColin LeMahieu     case 3:
260a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_A32;
261a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
262be8c453dSColin LeMahieu       break;
263be8c453dSColin LeMahieu     case 4:
264a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L132;
265a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
266be8c453dSColin LeMahieu       break;
267be8c453dSColin LeMahieu     case 5:
268a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_L232;
269a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
270be8c453dSColin LeMahieu       break;
271be8c453dSColin LeMahieu     case 6:
272a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
273a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
274be8c453dSColin LeMahieu       break;
275be8c453dSColin LeMahieu     case 7:
276a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
277a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_A32;
278be8c453dSColin LeMahieu       break;
279be8c453dSColin LeMahieu     case 8:
280a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
281a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
282be8c453dSColin LeMahieu       break;
283be8c453dSColin LeMahieu     case 9:
284a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
285a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
286be8c453dSColin LeMahieu       break;
287be8c453dSColin LeMahieu     case 10:
288a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S132;
289a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S132;
290be8c453dSColin LeMahieu       break;
291be8c453dSColin LeMahieu     case 11:
292a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
293a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S132;
294be8c453dSColin LeMahieu       break;
295be8c453dSColin LeMahieu     case 12:
296a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
297a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L132;
298be8c453dSColin LeMahieu       break;
299be8c453dSColin LeMahieu     case 13:
300a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
301a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_L232;
302be8c453dSColin LeMahieu       break;
303be8c453dSColin LeMahieu     case 14:
304a72fad98SKrzysztof Parzyszek       DecodeLow = DecoderTableSUBINSN_S232;
305a72fad98SKrzysztof Parzyszek       DecodeHigh = DecoderTableSUBINSN_S232;
306be8c453dSColin LeMahieu       break;
307be8c453dSColin LeMahieu     }
308a72fad98SKrzysztof Parzyszek     MI.setOpcode(Hexagon::DuplexIClass0 + duplexIClass);
309be8c453dSColin LeMahieu     MCInst *MILow = new (getContext()) MCInst;
310be8c453dSColin LeMahieu     MCInst *MIHigh = new (getContext()) MCInst;
311a72fad98SKrzysztof Parzyszek     Result = decodeInstruction(DecodeLow, *MILow, Instruction & 0x1fff, Address,
312a72fad98SKrzysztof Parzyszek                                this, STI);
313a72fad98SKrzysztof Parzyszek     if (Result != DecodeStatus::Success)
314a72fad98SKrzysztof Parzyszek       return DecodeStatus::Fail;
315a72fad98SKrzysztof Parzyszek     adjustDuplex(*MILow, getContext());
316a72fad98SKrzysztof Parzyszek     Result = decodeInstruction(
317a72fad98SKrzysztof Parzyszek         DecodeHigh, *MIHigh, (Instruction >> 16) & 0x1fff, Address, this, STI);
318a72fad98SKrzysztof Parzyszek     if (Result != DecodeStatus::Success)
319a72fad98SKrzysztof Parzyszek       return DecodeStatus::Fail;
320a72fad98SKrzysztof Parzyszek     adjustDuplex(*MIHigh, getContext());
321be8c453dSColin LeMahieu     MCOperand OPLow = MCOperand::createInst(MILow);
322be8c453dSColin LeMahieu     MCOperand OPHigh = MCOperand::createInst(MIHigh);
323be8c453dSColin LeMahieu     MI.addOperand(OPLow);
324be8c453dSColin LeMahieu     MI.addOperand(OPHigh);
325be8c453dSColin LeMahieu     Complete = true;
326be8c453dSColin LeMahieu   } else {
327be8c453dSColin LeMahieu     if ((Instruction & HexagonII::INST_PARSE_MASK) ==
32868d967d9SColin LeMahieu         HexagonII::INST_PARSE_PACKET_END)
32968d967d9SColin LeMahieu       Complete = true;
3307cd08927SColin LeMahieu 
331a72fad98SKrzysztof Parzyszek     if (Extender != nullptr)
332a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTableMustExtend32, MI, Instruction,
333a72fad98SKrzysztof Parzyszek                                  Address, this, STI);
334a72fad98SKrzysztof Parzyszek 
335a72fad98SKrzysztof Parzyszek     if (Result != MCDisassembler::Success)
336a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTable32, MI, Instruction, Address, this,
337a72fad98SKrzysztof Parzyszek                                  STI);
338a72fad98SKrzysztof Parzyszek 
339a72fad98SKrzysztof Parzyszek     if (Result != MCDisassembler::Success &&
340a72fad98SKrzysztof Parzyszek         STI.getFeatureBits()[Hexagon::ExtensionHVX])
341a72fad98SKrzysztof Parzyszek       Result = decodeInstruction(DecoderTableEXT_mmvec32, MI, Instruction,
342a72fad98SKrzysztof Parzyszek                                  Address, this, STI);
343a72fad98SKrzysztof Parzyszek 
344be8c453dSColin LeMahieu   }
34568d967d9SColin LeMahieu 
34681707549SColin LeMahieu   switch (MI.getOpcode()) {
34781707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_f_jumpnv_nt:
34881707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_f_jumpnv_t:
34981707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp0_jump_nt:
35081707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp0_jump_t:
35181707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp1_jump_nt:
35281707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_fp1_jump_t:
35381707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_t_jumpnv_nt:
35481707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_t_jumpnv_t:
35581707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp0_jump_nt:
35681707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp0_jump_t:
35781707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp1_jump_nt:
35881707549SColin LeMahieu   case Hexagon::J4_cmpeqn1_tp1_jump_t:
35981707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_f_jumpnv_nt:
36081707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_f_jumpnv_t:
36181707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp0_jump_nt:
36281707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp0_jump_t:
36381707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp1_jump_nt:
36481707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_fp1_jump_t:
36581707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_t_jumpnv_nt:
36681707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_t_jumpnv_t:
36781707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp0_jump_nt:
36881707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp0_jump_t:
36981707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp1_jump_nt:
37081707549SColin LeMahieu   case Hexagon::J4_cmpgtn1_tp1_jump_t:
371a72fad98SKrzysztof Parzyszek     MI.insert(MI.begin() + 1,
372a72fad98SKrzysztof Parzyszek               MCOperand::createExpr(MCConstantExpr::create(-1, getContext())));
37381707549SColin LeMahieu     break;
37481707549SColin LeMahieu   default:
37581707549SColin LeMahieu     break;
37681707549SColin LeMahieu   }
37781707549SColin LeMahieu 
3787cd08927SColin LeMahieu   if (HexagonMCInstrInfo::isNewValue(*MCII, MI)) {
3797cd08927SColin LeMahieu     unsigned OpIndex = HexagonMCInstrInfo::getNewValueOp(*MCII, MI);
3807cd08927SColin LeMahieu     MCOperand &MCO = MI.getOperand(OpIndex);
3817cd08927SColin LeMahieu     assert(MCO.isReg() && "New value consumers must be registers");
3827cd08927SColin LeMahieu     unsigned Register =
3837cd08927SColin LeMahieu         getContext().getRegisterInfo()->getEncodingValue(MCO.getReg());
3847cd08927SColin LeMahieu     if ((Register & 0x6) == 0)
3857cd08927SColin LeMahieu       // HexagonPRM 10.11 Bit 1-2 == 0 is reserved
3867cd08927SColin LeMahieu       return MCDisassembler::Fail;
3877cd08927SColin LeMahieu     unsigned Lookback = (Register & 0x6) >> 1;
3887cd08927SColin LeMahieu     unsigned Offset = 1;
3897cd08927SColin LeMahieu     bool Vector = HexagonMCInstrInfo::isVector(*MCII, MI);
3907cd08927SColin LeMahieu     auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
3917cd08927SColin LeMahieu     auto i = Instructions.end() - 1;
3927cd08927SColin LeMahieu     for (auto n = Instructions.begin() - 1;; --i, ++Offset) {
3937cd08927SColin LeMahieu       if (i == n)
3947cd08927SColin LeMahieu         // Couldn't find producer
3957cd08927SColin LeMahieu         return MCDisassembler::Fail;
3967cd08927SColin LeMahieu       if (Vector && !HexagonMCInstrInfo::isVector(*MCII, *i->getInst()))
3977cd08927SColin LeMahieu         // Skip scalars when calculating distances for vectors
3987cd08927SColin LeMahieu         ++Lookback;
3997cd08927SColin LeMahieu       if (HexagonMCInstrInfo::isImmext(*i->getInst()))
4007cd08927SColin LeMahieu         ++Lookback;
4017cd08927SColin LeMahieu       if (Offset == Lookback)
4027cd08927SColin LeMahieu         break;
4037cd08927SColin LeMahieu     }
4047cd08927SColin LeMahieu     auto const &Inst = *i->getInst();
4057cd08927SColin LeMahieu     bool SubregBit = (Register & 0x1) != 0;
4067cd08927SColin LeMahieu     if (SubregBit && HexagonMCInstrInfo::hasNewValue2(*MCII, Inst)) {
4077cd08927SColin LeMahieu       // If subreg bit is set we're selecting the second produced newvalue
4087cd08927SColin LeMahieu       unsigned Producer =
4097cd08927SColin LeMahieu           HexagonMCInstrInfo::getNewValueOperand2(*MCII, Inst).getReg();
4107cd08927SColin LeMahieu       assert(Producer != Hexagon::NoRegister);
4117cd08927SColin LeMahieu       MCO.setReg(Producer);
4127cd08927SColin LeMahieu     } else if (HexagonMCInstrInfo::hasNewValue(*MCII, Inst)) {
4137cd08927SColin LeMahieu       unsigned Producer =
4147cd08927SColin LeMahieu           HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg();
4157cd08927SColin LeMahieu       if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
4167cd08927SColin LeMahieu         Producer = ((Producer - Hexagon::W0) << 1) + SubregBit + Hexagon::V0;
4177cd08927SColin LeMahieu       else if (SubregBit)
4186fdfa3dcSColin LeMahieu         // Hexagon PRM 10.11 New-value operands
4196fdfa3dcSColin LeMahieu         // Nt[0] is reserved and should always be encoded as zero.
4207cd08927SColin LeMahieu         return MCDisassembler::Fail;
4217cd08927SColin LeMahieu       assert(Producer != Hexagon::NoRegister);
4227cd08927SColin LeMahieu       MCO.setReg(Producer);
4237cd08927SColin LeMahieu     } else
4247cd08927SColin LeMahieu       return MCDisassembler::Fail;
4257cd08927SColin LeMahieu   }
4267cd08927SColin LeMahieu 
4277cd08927SColin LeMahieu   if (Extender != nullptr) {
428a72fad98SKrzysztof Parzyszek     MCInst const &Inst = HexagonMCInstrInfo::isDuplex(*MCII, MI)
429a72fad98SKrzysztof Parzyszek                              ? *MI.getOperand(1).getInst()
430a72fad98SKrzysztof Parzyszek                              : MI;
4317cd08927SColin LeMahieu     if (!HexagonMCInstrInfo::isExtendable(*MCII, Inst) &&
4327cd08927SColin LeMahieu         !HexagonMCInstrInfo::isExtended(*MCII, Inst))
4337cd08927SColin LeMahieu       return MCDisassembler::Fail;
4347cd08927SColin LeMahieu   }
4355d6f03bdSColin LeMahieu   return Result;
43688ebb9e2SColin LeMahieu }
4377cd08927SColin LeMahieu 
4387cd08927SColin LeMahieu static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,
439e5e035a3SCraig Topper                                         ArrayRef<MCPhysReg> Table) {
4403da000c0SCraig Topper   if (RegNo < Table.size()) {
4417cd08927SColin LeMahieu     Inst.addOperand(MCOperand::createReg(Table[RegNo]));
4427cd08927SColin LeMahieu     return MCDisassembler::Success;
4433da000c0SCraig Topper   }
4443da000c0SCraig Topper 
4457cd08927SColin LeMahieu   return MCDisassembler::Fail;
4467cd08927SColin LeMahieu }
4477cd08927SColin LeMahieu 
4487c958713SColin LeMahieu static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
4497c958713SColin LeMahieu                                                    uint64_t Address,
4507c958713SColin LeMahieu                                                    const void *Decoder) {
4517c958713SColin LeMahieu   return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
4527c958713SColin LeMahieu }
4537c958713SColin LeMahieu 
4547cd08927SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
4557cd08927SColin LeMahieu                                                uint64_t Address,
4567cd08927SColin LeMahieu                                                const void *Decoder) {
457e5e035a3SCraig Topper   static const MCPhysReg IntRegDecoderTable[] = {
4587cd08927SColin LeMahieu       Hexagon::R0,  Hexagon::R1,  Hexagon::R2,  Hexagon::R3,  Hexagon::R4,
4597cd08927SColin LeMahieu       Hexagon::R5,  Hexagon::R6,  Hexagon::R7,  Hexagon::R8,  Hexagon::R9,
4607cd08927SColin LeMahieu       Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
4617cd08927SColin LeMahieu       Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
4627cd08927SColin LeMahieu       Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
4637cd08927SColin LeMahieu       Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
4647cd08927SColin LeMahieu       Hexagon::R30, Hexagon::R31};
4657cd08927SColin LeMahieu 
466e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, IntRegDecoderTable);
4677cd08927SColin LeMahieu }
4687c958713SColin LeMahieu 
469a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
470a72fad98SKrzysztof Parzyszek                                                       unsigned RegNo,
471a72fad98SKrzysztof Parzyszek                                                       uint64_t Address,
472a72fad98SKrzysztof Parzyszek                                                       const void *Decoder) {
473a72fad98SKrzysztof Parzyszek   static const MCPhysReg GeneralSubRegDecoderTable[] = {
474a72fad98SKrzysztof Parzyszek       Hexagon::R0,  Hexagon::R1,  Hexagon::R2,  Hexagon::R3,
475a72fad98SKrzysztof Parzyszek       Hexagon::R4,  Hexagon::R5,  Hexagon::R6,  Hexagon::R7,
476a72fad98SKrzysztof Parzyszek       Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
477a72fad98SKrzysztof Parzyszek       Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23,
478a72fad98SKrzysztof Parzyszek   };
479a72fad98SKrzysztof Parzyszek 
480a72fad98SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, GeneralSubRegDecoderTable);
481a72fad98SKrzysztof Parzyszek }
482a72fad98SKrzysztof Parzyszek 
4837c958713SColin LeMahieu static DecodeStatus DecodeVectorRegsRegisterClass(MCInst &Inst, unsigned RegNo,
4847c958713SColin LeMahieu                                                   uint64_t /*Address*/,
4857c958713SColin LeMahieu                                                   const void *Decoder) {
486e5e035a3SCraig Topper   static const MCPhysReg VecRegDecoderTable[] = {
4877c958713SColin LeMahieu       Hexagon::V0,  Hexagon::V1,  Hexagon::V2,  Hexagon::V3,  Hexagon::V4,
4887c958713SColin LeMahieu       Hexagon::V5,  Hexagon::V6,  Hexagon::V7,  Hexagon::V8,  Hexagon::V9,
4897c958713SColin LeMahieu       Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
4907c958713SColin LeMahieu       Hexagon::V15, Hexagon::V16, Hexagon::V17, Hexagon::V18, Hexagon::V19,
4917c958713SColin LeMahieu       Hexagon::V20, Hexagon::V21, Hexagon::V22, Hexagon::V23, Hexagon::V24,
4927c958713SColin LeMahieu       Hexagon::V25, Hexagon::V26, Hexagon::V27, Hexagon::V28, Hexagon::V29,
4937c958713SColin LeMahieu       Hexagon::V30, Hexagon::V31};
4947c958713SColin LeMahieu 
495e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, VecRegDecoderTable);
4967c958713SColin LeMahieu }
4977c958713SColin LeMahieu 
4987cd08927SColin LeMahieu static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
4997cd08927SColin LeMahieu                                                   uint64_t /*Address*/,
5007cd08927SColin LeMahieu                                                   const void *Decoder) {
501e5e035a3SCraig Topper   static const MCPhysReg DoubleRegDecoderTable[] = {
5027cd08927SColin LeMahieu       Hexagon::D0,  Hexagon::D1,  Hexagon::D2,  Hexagon::D3,
5037cd08927SColin LeMahieu       Hexagon::D4,  Hexagon::D5,  Hexagon::D6,  Hexagon::D7,
5047cd08927SColin LeMahieu       Hexagon::D8,  Hexagon::D9,  Hexagon::D10, Hexagon::D11,
5057cd08927SColin LeMahieu       Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15};
5067cd08927SColin LeMahieu 
507e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo >> 1, DoubleRegDecoderTable);
5087cd08927SColin LeMahieu }
5097cd08927SColin LeMahieu 
510a72fad98SKrzysztof Parzyszek static DecodeStatus DecodeGeneralDoubleLow8RegsRegisterClass(
511a72fad98SKrzysztof Parzyszek     MCInst &Inst, unsigned RegNo, uint64_t /*Address*/, const void *Decoder) {
512a72fad98SKrzysztof Parzyszek   static const MCPhysReg GeneralDoubleLow8RegDecoderTable[] = {
513a72fad98SKrzysztof Parzyszek       Hexagon::D0, Hexagon::D1, Hexagon::D2,  Hexagon::D3,
514a72fad98SKrzysztof Parzyszek       Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11};
515a72fad98SKrzysztof Parzyszek 
516a72fad98SKrzysztof Parzyszek   return DecodeRegisterClass(Inst, RegNo, GeneralDoubleLow8RegDecoderTable);
517a72fad98SKrzysztof Parzyszek }
518a72fad98SKrzysztof Parzyszek 
5197c958713SColin LeMahieu static DecodeStatus DecodeVecDblRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5207c958713SColin LeMahieu                                                   uint64_t /*Address*/,
5217c958713SColin LeMahieu                                                   const void *Decoder) {
522e5e035a3SCraig Topper   static const MCPhysReg VecDblRegDecoderTable[] = {
5237c958713SColin LeMahieu       Hexagon::W0,  Hexagon::W1,  Hexagon::W2,  Hexagon::W3,
5247c958713SColin LeMahieu       Hexagon::W4,  Hexagon::W5,  Hexagon::W6,  Hexagon::W7,
5257c958713SColin LeMahieu       Hexagon::W8,  Hexagon::W9,  Hexagon::W10, Hexagon::W11,
5267c958713SColin LeMahieu       Hexagon::W12, Hexagon::W13, Hexagon::W14, Hexagon::W15};
5277c958713SColin LeMahieu 
5283da000c0SCraig Topper   return (DecodeRegisterClass(Inst, RegNo >> 1, VecDblRegDecoderTable));
5297c958713SColin LeMahieu }
5307c958713SColin LeMahieu 
5317cd08927SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5327cd08927SColin LeMahieu                                                 uint64_t /*Address*/,
5337cd08927SColin LeMahieu                                                 const void *Decoder) {
534e5e035a3SCraig Topper   static const MCPhysReg PredRegDecoderTable[] = {Hexagon::P0, Hexagon::P1,
5357cd08927SColin LeMahieu                                                   Hexagon::P2, Hexagon::P3};
5367cd08927SColin LeMahieu 
537e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, PredRegDecoderTable);
5387cd08927SColin LeMahieu }
5397cd08927SColin LeMahieu 
5407c958713SColin LeMahieu static DecodeStatus DecodeVecPredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5417c958713SColin LeMahieu                                                    uint64_t /*Address*/,
5427c958713SColin LeMahieu                                                    const void *Decoder) {
543e5e035a3SCraig Topper   static const MCPhysReg VecPredRegDecoderTable[] = {Hexagon::Q0, Hexagon::Q1,
5447c958713SColin LeMahieu                                                      Hexagon::Q2, Hexagon::Q3};
5457c958713SColin LeMahieu 
546e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, VecPredRegDecoderTable);
5477c958713SColin LeMahieu }
548be8c453dSColin LeMahieu 
5497cd08927SColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5507cd08927SColin LeMahieu                                                uint64_t /*Address*/,
5517cd08927SColin LeMahieu                                                const void *Decoder) {
552f9015e62SKrzysztof Parzyszek   using namespace Hexagon;
553e5e035a3SCraig Topper   static const MCPhysReg CtrlRegDecoderTable[] = {
554f9015e62SKrzysztof Parzyszek     /*  0 */  SA0,        LC0,        SA1,        LC1,
555f9015e62SKrzysztof Parzyszek     /*  4 */  P3_0,       C5,         C6,         C7,
556f9015e62SKrzysztof Parzyszek     /*  8 */  USR,        PC,         UGP,        GP,
557ab57c2baSKrzysztof Parzyszek     /* 12 */  CS0,        CS1,        UPCYCLELO,  UPCYCLEHI,
558f9015e62SKrzysztof Parzyszek     /* 16 */  FRAMELIMIT, FRAMEKEY,   PKTCOUNTLO, PKTCOUNTHI,
559f9015e62SKrzysztof Parzyszek     /* 20 */  0,          0,          0,          0,
560f9015e62SKrzysztof Parzyszek     /* 24 */  0,          0,          0,          0,
561f9015e62SKrzysztof Parzyszek     /* 28 */  0,          0,          UTIMERLO,   UTIMERHI
5627cd08927SColin LeMahieu   };
5637cd08927SColin LeMahieu 
5646261e1b9SCraig Topper   if (RegNo >= array_lengthof(CtrlRegDecoderTable))
5657cd08927SColin LeMahieu     return MCDisassembler::Fail;
5667cd08927SColin LeMahieu 
567f9015e62SKrzysztof Parzyszek   static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
568f9015e62SKrzysztof Parzyszek   if (CtrlRegDecoderTable[RegNo] == NoRegister)
5697cd08927SColin LeMahieu     return MCDisassembler::Fail;
5707cd08927SColin LeMahieu 
5717cd08927SColin LeMahieu   unsigned Register = CtrlRegDecoderTable[RegNo];
5727cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
5737cd08927SColin LeMahieu   return MCDisassembler::Success;
5747cd08927SColin LeMahieu }
5757cd08927SColin LeMahieu 
5767cd08927SColin LeMahieu static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
5777cd08927SColin LeMahieu                                                  uint64_t /*Address*/,
5787cd08927SColin LeMahieu                                                  const void *Decoder) {
579f9015e62SKrzysztof Parzyszek   using namespace Hexagon;
580e5e035a3SCraig Topper   static const MCPhysReg CtrlReg64DecoderTable[] = {
581f9015e62SKrzysztof Parzyszek     /*  0 */  C1_0,       0,          C3_2,       0,
582f9015e62SKrzysztof Parzyszek     /*  4 */  C5_4,       0,          C7_6,       0,
583f9015e62SKrzysztof Parzyszek     /*  8 */  C9_8,       0,          C11_10,     0,
584ab57c2baSKrzysztof Parzyszek     /* 12 */  CS,         0,          UPCYCLE,    0,
585f9015e62SKrzysztof Parzyszek     /* 16 */  C17_16,     0,          PKTCOUNT,   0,
586f9015e62SKrzysztof Parzyszek     /* 20 */  0,          0,          0,          0,
587f9015e62SKrzysztof Parzyszek     /* 24 */  0,          0,          0,          0,
588f9015e62SKrzysztof Parzyszek     /* 28 */  0,          0,          UTIMER,     0
5897cd08927SColin LeMahieu   };
5907cd08927SColin LeMahieu 
5916261e1b9SCraig Topper   if (RegNo >= array_lengthof(CtrlReg64DecoderTable))
5927cd08927SColin LeMahieu     return MCDisassembler::Fail;
5937cd08927SColin LeMahieu 
594f9015e62SKrzysztof Parzyszek   static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
595f9015e62SKrzysztof Parzyszek   if (CtrlReg64DecoderTable[RegNo] == NoRegister)
5967cd08927SColin LeMahieu     return MCDisassembler::Fail;
5977cd08927SColin LeMahieu 
5987cd08927SColin LeMahieu   unsigned Register = CtrlReg64DecoderTable[RegNo];
5997cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
6007cd08927SColin LeMahieu   return MCDisassembler::Success;
6017cd08927SColin LeMahieu }
6027cd08927SColin LeMahieu 
6037cd08927SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
6047cd08927SColin LeMahieu                                                uint64_t /*Address*/,
6057cd08927SColin LeMahieu                                                const void *Decoder) {
6067cd08927SColin LeMahieu   unsigned Register = 0;
6077cd08927SColin LeMahieu   switch (RegNo) {
6087cd08927SColin LeMahieu   case 0:
6097cd08927SColin LeMahieu     Register = Hexagon::M0;
6107cd08927SColin LeMahieu     break;
6117cd08927SColin LeMahieu   case 1:
6127cd08927SColin LeMahieu     Register = Hexagon::M1;
6137cd08927SColin LeMahieu     break;
6147cd08927SColin LeMahieu   default:
6157cd08927SColin LeMahieu     return MCDisassembler::Fail;
6167cd08927SColin LeMahieu   }
6177cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
6187cd08927SColin LeMahieu   return MCDisassembler::Success;
6197cd08927SColin LeMahieu }
6207cd08927SColin LeMahieu 
6217cd08927SColin LeMahieu static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
6227cd08927SColin LeMahieu                                        uint64_t /*Address*/,
6237cd08927SColin LeMahieu                                        const void *Decoder) {
6247cd08927SColin LeMahieu   HexagonDisassembler const &Disassembler = disassembler(Decoder);
625a72fad98SKrzysztof Parzyszek   int64_t FullValue =
626a72fad98SKrzysztof Parzyszek       fullValue(*Disassembler.MCII, **Disassembler.CurrentBundle, MI, tmp);
6277cd08927SColin LeMahieu   assert(FullValue >= 0 && "Negative in unsigned decoder");
6287cd08927SColin LeMahieu   HexagonMCInstrInfo::addConstant(MI, FullValue, Disassembler.getContext());
6297cd08927SColin LeMahieu   return MCDisassembler::Success;
6307cd08927SColin LeMahieu }
6317cd08927SColin LeMahieu 
632a72fad98SKrzysztof Parzyszek static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
633a72fad98SKrzysztof Parzyszek                                     uint64_t /*Address*/, const void *Decoder) {
634a72fad98SKrzysztof Parzyszek   HexagonDisassembler const &Disassembler = disassembler(Decoder);
635a72fad98SKrzysztof Parzyszek   unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
636a72fad98SKrzysztof Parzyszek   tmp = SignExtend64(tmp, Bits);
637a72fad98SKrzysztof Parzyszek   signedDecoder<32>(MI, tmp, Decoder);
638a72fad98SKrzysztof Parzyszek   return MCDisassembler::Success;
639a72fad98SKrzysztof Parzyszek }
640a72fad98SKrzysztof Parzyszek 
6417cd08927SColin LeMahieu // custom decoder for various jump/call immediates
6427cd08927SColin LeMahieu static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
6437cd08927SColin LeMahieu                                     const void *Decoder) {
6447cd08927SColin LeMahieu   HexagonDisassembler const &Disassembler = disassembler(Decoder);
6457cd08927SColin LeMahieu   unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
6467cd08927SColin LeMahieu   // r13_2 is not extendable, so if there are no extent bits, it's r13_2
6477cd08927SColin LeMahieu   if (Bits == 0)
6487cd08927SColin LeMahieu     Bits = 15;
649a72fad98SKrzysztof Parzyszek   uint32_t FullValue =
650a72fad98SKrzysztof Parzyszek       fullValue(*Disassembler.MCII, **Disassembler.CurrentBundle, MI,
651a72fad98SKrzysztof Parzyszek                 SignExtend64(tmp, Bits));
6527cd08927SColin LeMahieu   int64_t Extended = SignExtend64<32>(FullValue) + Address;
653a72fad98SKrzysztof Parzyszek   if (!Disassembler.tryAddingSymbolicOperand(MI, Extended, Address, true, 0, 4))
6547cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
6557cd08927SColin LeMahieu   return MCDisassembler::Success;
6567cd08927SColin LeMahieu }
6577cd08927SColin LeMahieu 
6587cd08927SColin LeMahieu 
659