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"
17*82085927SEugene Zelenko #include "llvm/ADT/ArrayRef.h"
18*82085927SEugene 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"
25*82085927SEugene Zelenko #include "llvm/MC/MCRegisterInfo.h"
2688ebb9e2SColin LeMahieu #include "llvm/MC/MCSubtargetInfo.h"
27*82085927SEugene Zelenko #include "llvm/Support/MathExtras.h"
28d9903888SChandler Carruth #include "llvm/Support/raw_ostream.h"
297cd08927SColin LeMahieu #include "llvm/Support/TargetRegistry.h"
30*82085927SEugene Zelenko #include <cassert>
31*82085927SEugene Zelenko #include <cstddef>
32*82085927SEugene Zelenko #include <cstdint>
33*82085927SEugene 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 {
41*82085927SEugene 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;
47*82085927SEugene 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 
617cd08927SColin LeMahieu   void adjustExtendedInstructions(MCInst &MCI, MCInst const &MCB) const;
627cd08927SColin LeMahieu   void addSubinstOperands(MCInst *MI, unsigned opcode, unsigned inst) const;
6388ebb9e2SColin LeMahieu };
64*82085927SEugene Zelenko 
65*82085927SEugene Zelenko } // end anonymous namespace
6688ebb9e2SColin LeMahieu 
677cd08927SColin LeMahieu // Forward declare these because the auto-generated code will reference them.
687cd08927SColin LeMahieu // Definitions are further down.
697cd08927SColin LeMahieu 
707cd08927SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
71be8c453dSColin LeMahieu                                                uint64_t Address,
72be8c453dSColin LeMahieu                                                const void *Decoder);
737c958713SColin LeMahieu static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
747c958713SColin LeMahieu                                                    uint64_t Address,
757c958713SColin LeMahieu                                                    const void *Decoder);
767c958713SColin LeMahieu static DecodeStatus DecodeVectorRegsRegisterClass(MCInst &Inst, unsigned RegNo,
777c958713SColin LeMahieu                                                   uint64_t Address,
787c958713SColin LeMahieu                                                   const void *Decoder);
797cd08927SColin LeMahieu static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
807cd08927SColin LeMahieu                                                   uint64_t Address,
817cd08927SColin LeMahieu                                                   const void *Decoder);
827c958713SColin LeMahieu static DecodeStatus DecodeVecDblRegsRegisterClass(MCInst &Inst, unsigned RegNo,
837c958713SColin LeMahieu                                                   uint64_t Address,
847c958713SColin LeMahieu                                                   const void *Decoder);
857cd08927SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
867cd08927SColin LeMahieu                                                 uint64_t Address,
877cd08927SColin LeMahieu                                                 const void *Decoder);
887c958713SColin LeMahieu static DecodeStatus DecodeVecPredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
897c958713SColin LeMahieu                                                    uint64_t Address,
907c958713SColin LeMahieu                                                    const void *Decoder);
91f3db884eSColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
92be8c453dSColin LeMahieu                                                uint64_t Address,
93be8c453dSColin LeMahieu                                                const void *Decoder);
947cd08927SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
957cd08927SColin LeMahieu                                                uint64_t Address,
967cd08927SColin LeMahieu                                                const void *Decoder);
97404d5b24SColin LeMahieu static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
98be8c453dSColin LeMahieu                                                  uint64_t Address,
997cd08927SColin LeMahieu                                                  const void *Decoder);
1007cd08927SColin LeMahieu 
1017cd08927SColin LeMahieu static DecodeStatus decodeSpecial(MCInst &MI, uint32_t insn);
1027cd08927SColin LeMahieu static DecodeStatus decodeImmext(MCInst &MI, uint32_t insn,
103be8c453dSColin LeMahieu                                  void const *Decoder);
104be8c453dSColin LeMahieu 
105be8c453dSColin LeMahieu static unsigned GetSubinstOpcode(unsigned IClass, unsigned inst, unsigned &op,
106be8c453dSColin LeMahieu                                  raw_ostream &os);
107f3db884eSColin LeMahieu 
1087cd08927SColin LeMahieu static unsigned getRegFromSubinstEncoding(unsigned encoded_reg);
1097cd08927SColin LeMahieu 
1107cd08927SColin LeMahieu static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
1117cd08927SColin LeMahieu                                        uint64_t Address, const void *Decoder);
112654dc11bSKrzysztof Parzyszek static DecodeStatus s16_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1131e9d1d76SColin LeMahieu                                   const void *Decoder);
114654dc11bSKrzysztof Parzyszek static DecodeStatus s12_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1151e9d1d76SColin LeMahieu                                   const void *Decoder);
1161e9d1d76SColin LeMahieu static DecodeStatus s11_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1171e9d1d76SColin LeMahieu                                     const void *Decoder);
1181e9d1d76SColin LeMahieu static DecodeStatus s11_1ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1191e9d1d76SColin LeMahieu                                     const void *Decoder);
1201e9d1d76SColin LeMahieu static DecodeStatus s11_2ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1211e9d1d76SColin LeMahieu                                     const void *Decoder);
1221e9d1d76SColin LeMahieu static DecodeStatus s11_3ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1231e9d1d76SColin LeMahieu                                     const void *Decoder);
124654dc11bSKrzysztof Parzyszek static DecodeStatus s10_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1251e9d1d76SColin LeMahieu                                   const void *Decoder);
126654dc11bSKrzysztof Parzyszek static DecodeStatus s8_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1271e9d1d76SColin LeMahieu                                  const void *Decoder);
1281e9d1d76SColin LeMahieu static DecodeStatus s6_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1291e9d1d76SColin LeMahieu                                    const void *Decoder);
1301e9d1d76SColin LeMahieu static DecodeStatus s4_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1311e9d1d76SColin LeMahieu                                    const void *Decoder);
1321e9d1d76SColin LeMahieu static DecodeStatus s4_1ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1331e9d1d76SColin LeMahieu                                    const void *Decoder);
1341e9d1d76SColin LeMahieu static DecodeStatus s4_2ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1351e9d1d76SColin LeMahieu                                    const void *Decoder);
1361e9d1d76SColin LeMahieu static DecodeStatus s4_3ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1371e9d1d76SColin LeMahieu                                    const void *Decoder);
1387c958713SColin LeMahieu static DecodeStatus s4_6ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1397c958713SColin LeMahieu                                    const void *Decoder);
1407c958713SColin LeMahieu static DecodeStatus s3_6ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1417c958713SColin LeMahieu                                    const void *Decoder);
1427cd08927SColin LeMahieu static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
1437cd08927SColin LeMahieu                                     const void *Decoder);
144efa74e02SColin LeMahieu 
14588ebb9e2SColin LeMahieu #include "HexagonGenDisassemblerTables.inc"
14688ebb9e2SColin LeMahieu 
1477cd08927SColin LeMahieu static MCDisassembler *createHexagonDisassembler(const Target &T,
1487cd08927SColin LeMahieu                                                  const MCSubtargetInfo &STI,
14988ebb9e2SColin LeMahieu                                                  MCContext &Ctx) {
1507cd08927SColin LeMahieu   return new HexagonDisassembler(STI, Ctx, T.createMCInstrInfo());
15188ebb9e2SColin LeMahieu }
15288ebb9e2SColin LeMahieu 
15388ebb9e2SColin LeMahieu extern "C" void LLVMInitializeHexagonDisassembler() {
154f42454b9SMehdi Amini   TargetRegistry::RegisterMCDisassembler(getTheHexagonTarget(),
15588ebb9e2SColin LeMahieu                                          createHexagonDisassembler);
15688ebb9e2SColin LeMahieu }
15788ebb9e2SColin LeMahieu 
15888ebb9e2SColin LeMahieu DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
1597fc5b874SRafael Espindola                                                  ArrayRef<uint8_t> Bytes,
16088ebb9e2SColin LeMahieu                                                  uint64_t Address,
16188ebb9e2SColin LeMahieu                                                  raw_ostream &os,
16288ebb9e2SColin LeMahieu                                                  raw_ostream &cs) const {
16368d967d9SColin LeMahieu   DecodeStatus Result = DecodeStatus::Success;
16468d967d9SColin LeMahieu   bool Complete = false;
16568d967d9SColin LeMahieu   Size = 0;
1664aa6bea7SRafael Espindola 
16768d967d9SColin LeMahieu   *CurrentBundle = &MI;
168f0af6e52SColin LeMahieu   MI = HexagonMCInstrInfo::createBundle();
169*82085927SEugene Zelenko   while (Result == Success && !Complete) {
17068d967d9SColin LeMahieu     if (Bytes.size() < HEXAGON_INSTR_SIZE)
17168d967d9SColin LeMahieu       return MCDisassembler::Fail;
17268d967d9SColin LeMahieu     MCInst *Inst = new (getContext()) MCInst;
17368d967d9SColin LeMahieu     Result = getSingleInstruction(*Inst, MI, Bytes, Address, os, cs, Complete);
17468d967d9SColin LeMahieu     MI.addOperand(MCOperand::createInst(Inst));
17568d967d9SColin LeMahieu     Size += HEXAGON_INSTR_SIZE;
17668d967d9SColin LeMahieu     Bytes = Bytes.slice(HEXAGON_INSTR_SIZE);
17768d967d9SColin LeMahieu   }
1787cd08927SColin LeMahieu   if(Result == MCDisassembler::Fail)
17968d967d9SColin LeMahieu     return Result;
1807cd08927SColin LeMahieu   HexagonMCChecker Checker (*MCII, STI, MI, MI, *getContext().getRegisterInfo());
1817cd08927SColin LeMahieu   if(!Checker.check())
1827cd08927SColin LeMahieu     return MCDisassembler::Fail;
1837cd08927SColin LeMahieu   return MCDisassembler::Success;
1847cd08927SColin LeMahieu }
1857cd08927SColin LeMahieu 
186*82085927SEugene Zelenko static HexagonDisassembler const &disassembler(void const *Decoder) {
1877cd08927SColin LeMahieu   return *static_cast<HexagonDisassembler const *>(Decoder);
1887cd08927SColin LeMahieu }
189*82085927SEugene Zelenko 
190*82085927SEugene Zelenko static MCContext &contextFromDecoder(void const *Decoder) {
1917cd08927SColin LeMahieu   return disassembler(Decoder).getContext();
1927cd08927SColin LeMahieu }
19368d967d9SColin LeMahieu 
19468d967d9SColin LeMahieu DecodeStatus HexagonDisassembler::getSingleInstruction(
19568d967d9SColin LeMahieu     MCInst &MI, MCInst &MCB, ArrayRef<uint8_t> Bytes, uint64_t Address,
19668d967d9SColin LeMahieu     raw_ostream &os, raw_ostream &cs, bool &Complete) const {
19768d967d9SColin LeMahieu   assert(Bytes.size() >= HEXAGON_INSTR_SIZE);
19868d967d9SColin LeMahieu 
19968d967d9SColin LeMahieu   uint32_t Instruction =
2007cd08927SColin LeMahieu       (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | (Bytes[0] << 0);
20188ebb9e2SColin LeMahieu 
20268d967d9SColin LeMahieu   auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB);
20368d967d9SColin LeMahieu   if ((Instruction & HexagonII::INST_PARSE_MASK) ==
20468d967d9SColin LeMahieu       HexagonII::INST_PARSE_LOOP_END) {
20568d967d9SColin LeMahieu     if (BundleSize == 0)
20668d967d9SColin LeMahieu       HexagonMCInstrInfo::setInnerLoop(MCB);
20768d967d9SColin LeMahieu     else if (BundleSize == 1)
20868d967d9SColin LeMahieu       HexagonMCInstrInfo::setOuterLoop(MCB);
20968d967d9SColin LeMahieu     else
21068d967d9SColin LeMahieu       return DecodeStatus::Fail;
21168d967d9SColin LeMahieu   }
21268d967d9SColin LeMahieu 
21368d967d9SColin LeMahieu   DecodeStatus Result = DecodeStatus::Success;
21468d967d9SColin LeMahieu   if ((Instruction & HexagonII::INST_PARSE_MASK) ==
215be8c453dSColin LeMahieu       HexagonII::INST_PARSE_DUPLEX) {
216be8c453dSColin LeMahieu     // Determine the instruction class of each instruction in the duplex.
217be8c453dSColin LeMahieu     unsigned duplexIClass, IClassLow, IClassHigh;
218be8c453dSColin LeMahieu 
219be8c453dSColin LeMahieu     duplexIClass = ((Instruction >> 28) & 0xe) | ((Instruction >> 13) & 0x1);
220be8c453dSColin LeMahieu     switch (duplexIClass) {
221be8c453dSColin LeMahieu     default:
222be8c453dSColin LeMahieu       return MCDisassembler::Fail;
223be8c453dSColin LeMahieu     case 0:
224be8c453dSColin LeMahieu       IClassLow = HexagonII::HSIG_L1;
225be8c453dSColin LeMahieu       IClassHigh = HexagonII::HSIG_L1;
226be8c453dSColin LeMahieu       break;
227be8c453dSColin LeMahieu     case 1:
228be8c453dSColin LeMahieu       IClassLow = HexagonII::HSIG_L2;
229be8c453dSColin LeMahieu       IClassHigh = HexagonII::HSIG_L1;
230be8c453dSColin LeMahieu       break;
231be8c453dSColin LeMahieu     case 2:
232be8c453dSColin LeMahieu       IClassLow = HexagonII::HSIG_L2;
233be8c453dSColin LeMahieu       IClassHigh = HexagonII::HSIG_L2;
234be8c453dSColin LeMahieu       break;
235be8c453dSColin LeMahieu     case 3:
236be8c453dSColin LeMahieu       IClassLow = HexagonII::HSIG_A;
237be8c453dSColin LeMahieu       IClassHigh = HexagonII::HSIG_A;
238be8c453dSColin LeMahieu       break;
239be8c453dSColin LeMahieu     case 4:
240be8c453dSColin LeMahieu       IClassLow = HexagonII::HSIG_L1;
241be8c453dSColin LeMahieu       IClassHigh = HexagonII::HSIG_A;
242be8c453dSColin LeMahieu       break;
243be8c453dSColin LeMahieu     case 5:
244be8c453dSColin LeMahieu       IClassLow = HexagonII::HSIG_L2;
245be8c453dSColin LeMahieu       IClassHigh = HexagonII::HSIG_A;
246be8c453dSColin LeMahieu       break;
247be8c453dSColin LeMahieu     case 6:
248be8c453dSColin LeMahieu       IClassLow = HexagonII::HSIG_S1;
249be8c453dSColin LeMahieu       IClassHigh = HexagonII::HSIG_A;
250be8c453dSColin LeMahieu       break;
251be8c453dSColin LeMahieu     case 7:
252be8c453dSColin LeMahieu       IClassLow = HexagonII::HSIG_S2;
253be8c453dSColin LeMahieu       IClassHigh = HexagonII::HSIG_A;
254be8c453dSColin LeMahieu       break;
255be8c453dSColin LeMahieu     case 8:
256be8c453dSColin LeMahieu       IClassLow = HexagonII::HSIG_S1;
257be8c453dSColin LeMahieu       IClassHigh = HexagonII::HSIG_L1;
258be8c453dSColin LeMahieu       break;
259be8c453dSColin LeMahieu     case 9:
260be8c453dSColin LeMahieu       IClassLow = HexagonII::HSIG_S1;
261be8c453dSColin LeMahieu       IClassHigh = HexagonII::HSIG_L2;
262be8c453dSColin LeMahieu       break;
263be8c453dSColin LeMahieu     case 10:
264be8c453dSColin LeMahieu       IClassLow = HexagonII::HSIG_S1;
265be8c453dSColin LeMahieu       IClassHigh = HexagonII::HSIG_S1;
266be8c453dSColin LeMahieu       break;
267be8c453dSColin LeMahieu     case 11:
268be8c453dSColin LeMahieu       IClassLow = HexagonII::HSIG_S2;
269be8c453dSColin LeMahieu       IClassHigh = HexagonII::HSIG_S1;
270be8c453dSColin LeMahieu       break;
271be8c453dSColin LeMahieu     case 12:
272be8c453dSColin LeMahieu       IClassLow = HexagonII::HSIG_S2;
273be8c453dSColin LeMahieu       IClassHigh = HexagonII::HSIG_L1;
274be8c453dSColin LeMahieu       break;
275be8c453dSColin LeMahieu     case 13:
276be8c453dSColin LeMahieu       IClassLow = HexagonII::HSIG_S2;
277be8c453dSColin LeMahieu       IClassHigh = HexagonII::HSIG_L2;
278be8c453dSColin LeMahieu       break;
279be8c453dSColin LeMahieu     case 14:
280be8c453dSColin LeMahieu       IClassLow = HexagonII::HSIG_S2;
281be8c453dSColin LeMahieu       IClassHigh = HexagonII::HSIG_S2;
282be8c453dSColin LeMahieu       break;
283be8c453dSColin LeMahieu     }
284be8c453dSColin LeMahieu 
285be8c453dSColin LeMahieu     // Set the MCInst to be a duplex instruction. Which one doesn't matter.
286be8c453dSColin LeMahieu     MI.setOpcode(Hexagon::DuplexIClass0);
287be8c453dSColin LeMahieu 
288be8c453dSColin LeMahieu     // Decode each instruction in the duplex.
289be8c453dSColin LeMahieu     // Create an MCInst for each instruction.
290be8c453dSColin LeMahieu     unsigned instLow = Instruction & 0x1fff;
291be8c453dSColin LeMahieu     unsigned instHigh = (Instruction >> 16) & 0x1fff;
292be8c453dSColin LeMahieu     unsigned opLow;
293be8c453dSColin LeMahieu     if (GetSubinstOpcode(IClassLow, instLow, opLow, os) !=
294be8c453dSColin LeMahieu         MCDisassembler::Success)
295be8c453dSColin LeMahieu       return MCDisassembler::Fail;
296be8c453dSColin LeMahieu     unsigned opHigh;
297be8c453dSColin LeMahieu     if (GetSubinstOpcode(IClassHigh, instHigh, opHigh, os) !=
298be8c453dSColin LeMahieu         MCDisassembler::Success)
299be8c453dSColin LeMahieu       return MCDisassembler::Fail;
300be8c453dSColin LeMahieu     MCInst *MILow = new (getContext()) MCInst;
301be8c453dSColin LeMahieu     MILow->setOpcode(opLow);
302be8c453dSColin LeMahieu     MCInst *MIHigh = new (getContext()) MCInst;
303be8c453dSColin LeMahieu     MIHigh->setOpcode(opHigh);
3047cd08927SColin LeMahieu     addSubinstOperands(MILow, opLow, instLow);
3057cd08927SColin LeMahieu     addSubinstOperands(MIHigh, opHigh, instHigh);
306be8c453dSColin LeMahieu     // see ConvertToSubInst() in
307be8c453dSColin LeMahieu     // lib/Target/Hexagon/MCTargetDesc/HexagonMCDuplexInfo.cpp
308be8c453dSColin LeMahieu 
309be8c453dSColin LeMahieu     // Add the duplex instruction MCInsts as operands to the passed in MCInst.
310be8c453dSColin LeMahieu     MCOperand OPLow = MCOperand::createInst(MILow);
311be8c453dSColin LeMahieu     MCOperand OPHigh = MCOperand::createInst(MIHigh);
312be8c453dSColin LeMahieu     MI.addOperand(OPLow);
313be8c453dSColin LeMahieu     MI.addOperand(OPHigh);
314be8c453dSColin LeMahieu     Complete = true;
315be8c453dSColin LeMahieu   } else {
316be8c453dSColin LeMahieu     if ((Instruction & HexagonII::INST_PARSE_MASK) ==
31768d967d9SColin LeMahieu         HexagonII::INST_PARSE_PACKET_END)
31868d967d9SColin LeMahieu       Complete = true;
31968d967d9SColin LeMahieu     // Calling the auto-generated decoder function.
32068d967d9SColin LeMahieu     Result =
32168d967d9SColin LeMahieu         decodeInstruction(DecoderTable32, MI, Instruction, Address, this, STI);
3227cd08927SColin LeMahieu 
3237cd08927SColin LeMahieu     // If a, "standard" insn isn't found check special cases.
3247cd08927SColin LeMahieu     if (MCDisassembler::Success != Result ||
3257cd08927SColin LeMahieu         MI.getOpcode() == Hexagon::A4_ext) {
3267cd08927SColin LeMahieu       Result = decodeImmext(MI, Instruction, this);
3277cd08927SColin LeMahieu       if (MCDisassembler::Success != Result) {
3287cd08927SColin LeMahieu         Result = decodeSpecial(MI, Instruction);
3297cd08927SColin LeMahieu       }
3307cd08927SColin LeMahieu     } else {
3317cd08927SColin LeMahieu       // If the instruction is a compound instruction, register values will
3327cd08927SColin LeMahieu       // follow the duplex model, so the register values in the MCInst are
3337cd08927SColin LeMahieu       // incorrect. If the instruction is a compound, loop through the
3347cd08927SColin LeMahieu       // operands and change registers appropriately.
335*82085927SEugene Zelenko       if (HexagonMCInstrInfo::getType(*MCII, MI) == HexagonII::TypeCOMPOUND) {
3367cd08927SColin LeMahieu         for (MCInst::iterator i = MI.begin(), last = MI.end(); i < last; ++i) {
3377cd08927SColin LeMahieu           if (i->isReg()) {
3387cd08927SColin LeMahieu             unsigned reg = i->getReg() - Hexagon::R0;
3397cd08927SColin LeMahieu             i->setReg(getRegFromSubinstEncoding(reg));
3407cd08927SColin LeMahieu           }
3417cd08927SColin LeMahieu         }
3427cd08927SColin LeMahieu       }
3437cd08927SColin LeMahieu     }
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:
37181707549SColin LeMahieu     MI.insert(MI.begin() + 1, MCOperand::createExpr(MCConstantExpr::create(-1, getContext())));
37281707549SColin LeMahieu     break;
37381707549SColin LeMahieu   default:
37481707549SColin LeMahieu     break;
37581707549SColin LeMahieu   }
37681707549SColin LeMahieu 
3777cd08927SColin LeMahieu   if (HexagonMCInstrInfo::isNewValue(*MCII, MI)) {
3787cd08927SColin LeMahieu     unsigned OpIndex = HexagonMCInstrInfo::getNewValueOp(*MCII, MI);
3797cd08927SColin LeMahieu     MCOperand &MCO = MI.getOperand(OpIndex);
3807cd08927SColin LeMahieu     assert(MCO.isReg() && "New value consumers must be registers");
3817cd08927SColin LeMahieu     unsigned Register =
3827cd08927SColin LeMahieu         getContext().getRegisterInfo()->getEncodingValue(MCO.getReg());
3837cd08927SColin LeMahieu     if ((Register & 0x6) == 0)
3847cd08927SColin LeMahieu       // HexagonPRM 10.11 Bit 1-2 == 0 is reserved
3857cd08927SColin LeMahieu       return MCDisassembler::Fail;
3867cd08927SColin LeMahieu     unsigned Lookback = (Register & 0x6) >> 1;
3877cd08927SColin LeMahieu     unsigned Offset = 1;
3887cd08927SColin LeMahieu     bool Vector = HexagonMCInstrInfo::isVector(*MCII, MI);
3897cd08927SColin LeMahieu     auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
3907cd08927SColin LeMahieu     auto i = Instructions.end() - 1;
3917cd08927SColin LeMahieu     for (auto n = Instructions.begin() - 1;; --i, ++Offset) {
3927cd08927SColin LeMahieu       if (i == n)
3937cd08927SColin LeMahieu         // Couldn't find producer
3947cd08927SColin LeMahieu         return MCDisassembler::Fail;
3957cd08927SColin LeMahieu       if (Vector && !HexagonMCInstrInfo::isVector(*MCII, *i->getInst()))
3967cd08927SColin LeMahieu         // Skip scalars when calculating distances for vectors
3977cd08927SColin LeMahieu         ++Lookback;
3987cd08927SColin LeMahieu       if (HexagonMCInstrInfo::isImmext(*i->getInst()))
3997cd08927SColin LeMahieu         ++Lookback;
4007cd08927SColin LeMahieu       if (Offset == Lookback)
4017cd08927SColin LeMahieu         break;
4027cd08927SColin LeMahieu     }
4037cd08927SColin LeMahieu     auto const &Inst = *i->getInst();
4047cd08927SColin LeMahieu     bool SubregBit = (Register & 0x1) != 0;
4057cd08927SColin LeMahieu     if (SubregBit && HexagonMCInstrInfo::hasNewValue2(*MCII, Inst)) {
4067cd08927SColin LeMahieu       // If subreg bit is set we're selecting the second produced newvalue
4077cd08927SColin LeMahieu       unsigned Producer =
4087cd08927SColin LeMahieu           HexagonMCInstrInfo::getNewValueOperand2(*MCII, Inst).getReg();
4097cd08927SColin LeMahieu       assert(Producer != Hexagon::NoRegister);
4107cd08927SColin LeMahieu       MCO.setReg(Producer);
4117cd08927SColin LeMahieu     } else if (HexagonMCInstrInfo::hasNewValue(*MCII, Inst)) {
4127cd08927SColin LeMahieu       unsigned Producer =
4137cd08927SColin LeMahieu           HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg();
4147cd08927SColin LeMahieu       if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
4157cd08927SColin LeMahieu         Producer = ((Producer - Hexagon::W0) << 1) + SubregBit + Hexagon::V0;
4167cd08927SColin LeMahieu       else if (SubregBit)
4176fdfa3dcSColin LeMahieu         // Hexagon PRM 10.11 New-value operands
4186fdfa3dcSColin LeMahieu         // Nt[0] is reserved and should always be encoded as zero.
4197cd08927SColin LeMahieu         return MCDisassembler::Fail;
4207cd08927SColin LeMahieu       assert(Producer != Hexagon::NoRegister);
4217cd08927SColin LeMahieu       MCO.setReg(Producer);
4227cd08927SColin LeMahieu     } else
4237cd08927SColin LeMahieu       return MCDisassembler::Fail;
4247cd08927SColin LeMahieu   }
4257cd08927SColin LeMahieu 
4267cd08927SColin LeMahieu   adjustExtendedInstructions(MI, MCB);
4277cd08927SColin LeMahieu   MCInst const *Extender =
4287cd08927SColin LeMahieu     HexagonMCInstrInfo::extenderForIndex(MCB,
4297cd08927SColin LeMahieu                                          HexagonMCInstrInfo::bundleSize(MCB));
4307cd08927SColin LeMahieu   if(Extender != nullptr) {
4317cd08927SColin LeMahieu     MCInst const & Inst = HexagonMCInstrInfo::isDuplex(*MCII, MI) ?
4327cd08927SColin LeMahieu                           *MI.getOperand(1).getInst() : 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 void HexagonDisassembler::adjustExtendedInstructions(MCInst &MCI,
4417cd08927SColin LeMahieu                                                      MCInst const &MCB) const {
4427cd08927SColin LeMahieu   if (!HexagonMCInstrInfo::hasExtenderForIndex(
4437cd08927SColin LeMahieu           MCB, HexagonMCInstrInfo::bundleSize(MCB))) {
4447cd08927SColin LeMahieu     unsigned opcode;
4457cd08927SColin LeMahieu     // This code is used by the disassembler to disambiguate between GP
4467cd08927SColin LeMahieu     // relative and absolute addressing instructions since they both have
4477cd08927SColin LeMahieu     // same encoding bits. However, an absolute addressing instruction must
4487cd08927SColin LeMahieu     // follow an immediate extender. Disassembler alwaus select absolute
4497cd08927SColin LeMahieu     // addressing instructions first and uses this code to change them into
4507cd08927SColin LeMahieu     // GP relative instruction in the absence of the corresponding immediate
4517cd08927SColin LeMahieu     // extender.
4527cd08927SColin LeMahieu     switch (MCI.getOpcode()) {
4539675de5bSColin LeMahieu     case Hexagon::PS_storerbabs:
4547cd08927SColin LeMahieu       opcode = Hexagon::S2_storerbgp;
4557cd08927SColin LeMahieu       break;
4569675de5bSColin LeMahieu     case Hexagon::PS_storerhabs:
4577cd08927SColin LeMahieu       opcode = Hexagon::S2_storerhgp;
4587cd08927SColin LeMahieu       break;
4599675de5bSColin LeMahieu     case Hexagon::PS_storerfabs:
4607cd08927SColin LeMahieu       opcode = Hexagon::S2_storerfgp;
4617cd08927SColin LeMahieu       break;
4629675de5bSColin LeMahieu     case Hexagon::PS_storeriabs:
4637cd08927SColin LeMahieu       opcode = Hexagon::S2_storerigp;
4647cd08927SColin LeMahieu       break;
4659675de5bSColin LeMahieu     case Hexagon::PS_storerbnewabs:
4667cd08927SColin LeMahieu       opcode = Hexagon::S2_storerbnewgp;
4677cd08927SColin LeMahieu       break;
4689675de5bSColin LeMahieu     case Hexagon::PS_storerhnewabs:
4697cd08927SColin LeMahieu       opcode = Hexagon::S2_storerhnewgp;
4707cd08927SColin LeMahieu       break;
4719675de5bSColin LeMahieu     case Hexagon::PS_storerinewabs:
4727cd08927SColin LeMahieu       opcode = Hexagon::S2_storerinewgp;
4737cd08927SColin LeMahieu       break;
4749675de5bSColin LeMahieu     case Hexagon::PS_storerdabs:
4757cd08927SColin LeMahieu       opcode = Hexagon::S2_storerdgp;
4767cd08927SColin LeMahieu       break;
4779675de5bSColin LeMahieu     case Hexagon::PS_loadrbabs:
4787cd08927SColin LeMahieu       opcode = Hexagon::L2_loadrbgp;
4797cd08927SColin LeMahieu       break;
4809675de5bSColin LeMahieu     case Hexagon::PS_loadrubabs:
4817cd08927SColin LeMahieu       opcode = Hexagon::L2_loadrubgp;
4827cd08927SColin LeMahieu       break;
4839675de5bSColin LeMahieu     case Hexagon::PS_loadrhabs:
4847cd08927SColin LeMahieu       opcode = Hexagon::L2_loadrhgp;
4857cd08927SColin LeMahieu       break;
4869675de5bSColin LeMahieu     case Hexagon::PS_loadruhabs:
4877cd08927SColin LeMahieu       opcode = Hexagon::L2_loadruhgp;
4887cd08927SColin LeMahieu       break;
4899675de5bSColin LeMahieu     case Hexagon::PS_loadriabs:
4907cd08927SColin LeMahieu       opcode = Hexagon::L2_loadrigp;
4917cd08927SColin LeMahieu       break;
4929675de5bSColin LeMahieu     case Hexagon::PS_loadrdabs:
4937cd08927SColin LeMahieu       opcode = Hexagon::L2_loadrdgp;
4947cd08927SColin LeMahieu       break;
4957cd08927SColin LeMahieu     default:
4967cd08927SColin LeMahieu       opcode = MCI.getOpcode();
4977cd08927SColin LeMahieu     }
4987cd08927SColin LeMahieu     MCI.setOpcode(opcode);
4997cd08927SColin LeMahieu   }
5007cd08927SColin LeMahieu }
5017cd08927SColin LeMahieu 
5027cd08927SColin LeMahieu static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,
503e5e035a3SCraig Topper                                         ArrayRef<MCPhysReg> Table) {
5043da000c0SCraig Topper   if (RegNo < Table.size()) {
5057cd08927SColin LeMahieu     Inst.addOperand(MCOperand::createReg(Table[RegNo]));
5067cd08927SColin LeMahieu     return MCDisassembler::Success;
5073da000c0SCraig Topper   }
5083da000c0SCraig Topper 
5097cd08927SColin LeMahieu   return MCDisassembler::Fail;
5107cd08927SColin LeMahieu }
5117cd08927SColin LeMahieu 
5127c958713SColin LeMahieu static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
5137c958713SColin LeMahieu                                                    uint64_t Address,
5147c958713SColin LeMahieu                                                    const void *Decoder) {
5157c958713SColin LeMahieu   return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
5167c958713SColin LeMahieu }
5177c958713SColin LeMahieu 
5187cd08927SColin LeMahieu static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5197cd08927SColin LeMahieu                                                uint64_t Address,
5207cd08927SColin LeMahieu                                                const void *Decoder) {
521e5e035a3SCraig Topper   static const MCPhysReg IntRegDecoderTable[] = {
5227cd08927SColin LeMahieu       Hexagon::R0,  Hexagon::R1,  Hexagon::R2,  Hexagon::R3,  Hexagon::R4,
5237cd08927SColin LeMahieu       Hexagon::R5,  Hexagon::R6,  Hexagon::R7,  Hexagon::R8,  Hexagon::R9,
5247cd08927SColin LeMahieu       Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
5257cd08927SColin LeMahieu       Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
5267cd08927SColin LeMahieu       Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
5277cd08927SColin LeMahieu       Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
5287cd08927SColin LeMahieu       Hexagon::R30, Hexagon::R31};
5297cd08927SColin LeMahieu 
530e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, IntRegDecoderTable);
5317cd08927SColin LeMahieu }
5327c958713SColin LeMahieu 
5337c958713SColin LeMahieu static DecodeStatus DecodeVectorRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5347c958713SColin LeMahieu                                                   uint64_t /*Address*/,
5357c958713SColin LeMahieu                                                   const void *Decoder) {
536e5e035a3SCraig Topper   static const MCPhysReg VecRegDecoderTable[] = {
5377c958713SColin LeMahieu       Hexagon::V0,  Hexagon::V1,  Hexagon::V2,  Hexagon::V3,  Hexagon::V4,
5387c958713SColin LeMahieu       Hexagon::V5,  Hexagon::V6,  Hexagon::V7,  Hexagon::V8,  Hexagon::V9,
5397c958713SColin LeMahieu       Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
5407c958713SColin LeMahieu       Hexagon::V15, Hexagon::V16, Hexagon::V17, Hexagon::V18, Hexagon::V19,
5417c958713SColin LeMahieu       Hexagon::V20, Hexagon::V21, Hexagon::V22, Hexagon::V23, Hexagon::V24,
5427c958713SColin LeMahieu       Hexagon::V25, Hexagon::V26, Hexagon::V27, Hexagon::V28, Hexagon::V29,
5437c958713SColin LeMahieu       Hexagon::V30, Hexagon::V31};
5447c958713SColin LeMahieu 
545e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, VecRegDecoderTable);
5467c958713SColin LeMahieu }
5477c958713SColin LeMahieu 
5487cd08927SColin LeMahieu static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5497cd08927SColin LeMahieu                                                   uint64_t /*Address*/,
5507cd08927SColin LeMahieu                                                   const void *Decoder) {
551e5e035a3SCraig Topper   static const MCPhysReg DoubleRegDecoderTable[] = {
5527cd08927SColin LeMahieu       Hexagon::D0,  Hexagon::D1,  Hexagon::D2,  Hexagon::D3,
5537cd08927SColin LeMahieu       Hexagon::D4,  Hexagon::D5,  Hexagon::D6,  Hexagon::D7,
5547cd08927SColin LeMahieu       Hexagon::D8,  Hexagon::D9,  Hexagon::D10, Hexagon::D11,
5557cd08927SColin LeMahieu       Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15};
5567cd08927SColin LeMahieu 
557e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo >> 1, DoubleRegDecoderTable);
5587cd08927SColin LeMahieu }
5597cd08927SColin LeMahieu 
5607c958713SColin LeMahieu static DecodeStatus DecodeVecDblRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5617c958713SColin LeMahieu                                                   uint64_t /*Address*/,
5627c958713SColin LeMahieu                                                   const void *Decoder) {
563e5e035a3SCraig Topper   static const MCPhysReg VecDblRegDecoderTable[] = {
5647c958713SColin LeMahieu       Hexagon::W0,  Hexagon::W1,  Hexagon::W2,  Hexagon::W3,
5657c958713SColin LeMahieu       Hexagon::W4,  Hexagon::W5,  Hexagon::W6,  Hexagon::W7,
5667c958713SColin LeMahieu       Hexagon::W8,  Hexagon::W9,  Hexagon::W10, Hexagon::W11,
5677c958713SColin LeMahieu       Hexagon::W12, Hexagon::W13, Hexagon::W14, Hexagon::W15};
5687c958713SColin LeMahieu 
5693da000c0SCraig Topper   return (DecodeRegisterClass(Inst, RegNo >> 1, VecDblRegDecoderTable));
5707c958713SColin LeMahieu }
5717c958713SColin LeMahieu 
5727cd08927SColin LeMahieu static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5737cd08927SColin LeMahieu                                                 uint64_t /*Address*/,
5747cd08927SColin LeMahieu                                                 const void *Decoder) {
575e5e035a3SCraig Topper   static const MCPhysReg PredRegDecoderTable[] = {Hexagon::P0, Hexagon::P1,
5767cd08927SColin LeMahieu                                                   Hexagon::P2, Hexagon::P3};
5777cd08927SColin LeMahieu 
578e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, PredRegDecoderTable);
5797cd08927SColin LeMahieu }
5807cd08927SColin LeMahieu 
5817c958713SColin LeMahieu static DecodeStatus DecodeVecPredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5827c958713SColin LeMahieu                                                    uint64_t /*Address*/,
5837c958713SColin LeMahieu                                                    const void *Decoder) {
584e5e035a3SCraig Topper   static const MCPhysReg VecPredRegDecoderTable[] = {Hexagon::Q0, Hexagon::Q1,
5857c958713SColin LeMahieu                                                      Hexagon::Q2, Hexagon::Q3};
5867c958713SColin LeMahieu 
587e5e035a3SCraig Topper   return DecodeRegisterClass(Inst, RegNo, VecPredRegDecoderTable);
5887c958713SColin LeMahieu }
589be8c453dSColin LeMahieu 
5907cd08927SColin LeMahieu static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
5917cd08927SColin LeMahieu                                                uint64_t /*Address*/,
5927cd08927SColin LeMahieu                                                const void *Decoder) {
593e5e035a3SCraig Topper   static const MCPhysReg CtrlRegDecoderTable[] = {
5947cd08927SColin LeMahieu     Hexagon::SA0, Hexagon::LC0, Hexagon::SA1, Hexagon::LC1,
5957cd08927SColin LeMahieu     Hexagon::P3_0, Hexagon::C5, Hexagon::C6, Hexagon::C7,
5967cd08927SColin LeMahieu     Hexagon::USR, Hexagon::PC, Hexagon::UGP, Hexagon::GP,
5977cd08927SColin LeMahieu     Hexagon::CS0, Hexagon::CS1, Hexagon::UPCL, Hexagon::UPC
5987cd08927SColin LeMahieu   };
5997cd08927SColin LeMahieu 
6006261e1b9SCraig Topper   if (RegNo >= array_lengthof(CtrlRegDecoderTable))
6017cd08927SColin LeMahieu     return MCDisassembler::Fail;
6027cd08927SColin LeMahieu 
6037cd08927SColin LeMahieu   if (CtrlRegDecoderTable[RegNo] == Hexagon::NoRegister)
6047cd08927SColin LeMahieu     return MCDisassembler::Fail;
6057cd08927SColin LeMahieu 
6067cd08927SColin LeMahieu   unsigned Register = CtrlRegDecoderTable[RegNo];
6077cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
6087cd08927SColin LeMahieu   return MCDisassembler::Success;
6097cd08927SColin LeMahieu }
6107cd08927SColin LeMahieu 
6117cd08927SColin LeMahieu static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
6127cd08927SColin LeMahieu                                                  uint64_t /*Address*/,
6137cd08927SColin LeMahieu                                                  const void *Decoder) {
614e5e035a3SCraig Topper   static const MCPhysReg CtrlReg64DecoderTable[] = {
6157cd08927SColin LeMahieu       Hexagon::C1_0,   Hexagon::NoRegister,
6167cd08927SColin LeMahieu       Hexagon::C3_2,   Hexagon::NoRegister,
6177cd08927SColin LeMahieu       Hexagon::C7_6,   Hexagon::NoRegister,
6187cd08927SColin LeMahieu       Hexagon::C9_8,   Hexagon::NoRegister,
6197cd08927SColin LeMahieu       Hexagon::C11_10, Hexagon::NoRegister,
6207cd08927SColin LeMahieu       Hexagon::CS,     Hexagon::NoRegister,
6217cd08927SColin LeMahieu       Hexagon::UPC,    Hexagon::NoRegister
6227cd08927SColin LeMahieu   };
6237cd08927SColin LeMahieu 
6246261e1b9SCraig Topper   if (RegNo >= array_lengthof(CtrlReg64DecoderTable))
6257cd08927SColin LeMahieu     return MCDisassembler::Fail;
6267cd08927SColin LeMahieu 
6277cd08927SColin LeMahieu   if (CtrlReg64DecoderTable[RegNo] == Hexagon::NoRegister)
6287cd08927SColin LeMahieu     return MCDisassembler::Fail;
6297cd08927SColin LeMahieu 
6307cd08927SColin LeMahieu   unsigned Register = CtrlReg64DecoderTable[RegNo];
6317cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
6327cd08927SColin LeMahieu   return MCDisassembler::Success;
6337cd08927SColin LeMahieu }
6347cd08927SColin LeMahieu 
6357cd08927SColin LeMahieu static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
6367cd08927SColin LeMahieu                                                uint64_t /*Address*/,
6377cd08927SColin LeMahieu                                                const void *Decoder) {
6387cd08927SColin LeMahieu   unsigned Register = 0;
6397cd08927SColin LeMahieu   switch (RegNo) {
6407cd08927SColin LeMahieu   case 0:
6417cd08927SColin LeMahieu     Register = Hexagon::M0;
6427cd08927SColin LeMahieu     break;
6437cd08927SColin LeMahieu   case 1:
6447cd08927SColin LeMahieu     Register = Hexagon::M1;
6457cd08927SColin LeMahieu     break;
6467cd08927SColin LeMahieu   default:
6477cd08927SColin LeMahieu     return MCDisassembler::Fail;
6487cd08927SColin LeMahieu   }
6497cd08927SColin LeMahieu   Inst.addOperand(MCOperand::createReg(Register));
6507cd08927SColin LeMahieu   return MCDisassembler::Success;
6517cd08927SColin LeMahieu }
6527cd08927SColin LeMahieu 
653*82085927SEugene Zelenko static uint32_t fullValue(MCInstrInfo const &MCII, MCInst &MCB, MCInst &MI,
6547cd08927SColin LeMahieu                           int64_t Value) {
6557cd08927SColin LeMahieu   MCInst const *Extender = HexagonMCInstrInfo::extenderForIndex(
6567cd08927SColin LeMahieu     MCB, HexagonMCInstrInfo::bundleSize(MCB));
6577cd08927SColin LeMahieu   if(!Extender || MI.size() != HexagonMCInstrInfo::getExtendableOp(MCII, MI))
6587cd08927SColin LeMahieu     return Value;
6597cd08927SColin LeMahieu   unsigned Alignment = HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
6607cd08927SColin LeMahieu   uint32_t Lower6 = static_cast<uint32_t>(Value >> Alignment) & 0x3f;
6617cd08927SColin LeMahieu   int64_t Bits;
6627cd08927SColin LeMahieu   bool Success = Extender->getOperand(0).getExpr()->evaluateAsAbsolute(Bits);
6637cd08927SColin LeMahieu   assert(Success);(void)Success;
6647cd08927SColin LeMahieu   uint32_t Upper26 = static_cast<uint32_t>(Bits);
6657cd08927SColin LeMahieu   uint32_t Operand = Upper26 | Lower6;
6667cd08927SColin LeMahieu   return Operand;
6677cd08927SColin LeMahieu }
668*82085927SEugene Zelenko 
6697cd08927SColin LeMahieu template <size_t T>
670*82085927SEugene Zelenko static void signedDecoder(MCInst &MI, unsigned tmp, const void *Decoder) {
6717cd08927SColin LeMahieu   HexagonDisassembler const &Disassembler = disassembler(Decoder);
6727cd08927SColin LeMahieu   int64_t FullValue = fullValue(*Disassembler.MCII,
6737cd08927SColin LeMahieu                                 **Disassembler.CurrentBundle,
6747cd08927SColin LeMahieu                                 MI, SignExtend64<T>(tmp));
6757cd08927SColin LeMahieu   int64_t Extended = SignExtend64<32>(FullValue);
6767cd08927SColin LeMahieu   HexagonMCInstrInfo::addConstant(MI, Extended,
6777cd08927SColin LeMahieu                                   Disassembler.getContext());
6787cd08927SColin LeMahieu }
6797cd08927SColin LeMahieu 
6807cd08927SColin LeMahieu static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
6817cd08927SColin LeMahieu                                        uint64_t /*Address*/,
6827cd08927SColin LeMahieu                                        const void *Decoder) {
6837cd08927SColin LeMahieu   HexagonDisassembler const &Disassembler = disassembler(Decoder);
6847cd08927SColin LeMahieu   int64_t FullValue = fullValue(*Disassembler.MCII,
6857cd08927SColin LeMahieu                                 **Disassembler.CurrentBundle,
6867cd08927SColin LeMahieu                                 MI, tmp);
6877cd08927SColin LeMahieu   assert(FullValue >= 0 && "Negative in unsigned decoder");
6887cd08927SColin LeMahieu   HexagonMCInstrInfo::addConstant(MI, FullValue, Disassembler.getContext());
6897cd08927SColin LeMahieu   return MCDisassembler::Success;
6907cd08927SColin LeMahieu }
6917cd08927SColin LeMahieu 
692654dc11bSKrzysztof Parzyszek static DecodeStatus s16_0ImmDecoder(MCInst &MI, unsigned tmp,
6931e9d1d76SColin LeMahieu                                   uint64_t /*Address*/, const void *Decoder) {
6947cd08927SColin LeMahieu   signedDecoder<16>(MI, tmp, Decoder);
6951e9d1d76SColin LeMahieu   return MCDisassembler::Success;
6961e9d1d76SColin LeMahieu }
6971e9d1d76SColin LeMahieu 
698654dc11bSKrzysztof Parzyszek static DecodeStatus s12_0ImmDecoder(MCInst &MI, unsigned tmp,
6991e9d1d76SColin LeMahieu                                   uint64_t /*Address*/, const void *Decoder) {
7007cd08927SColin LeMahieu   signedDecoder<12>(MI, tmp, Decoder);
7011e9d1d76SColin LeMahieu   return MCDisassembler::Success;
7021e9d1d76SColin LeMahieu }
7031e9d1d76SColin LeMahieu 
7041e9d1d76SColin LeMahieu static DecodeStatus s11_0ImmDecoder(MCInst &MI, unsigned tmp,
7051e9d1d76SColin LeMahieu                                     uint64_t /*Address*/, const void *Decoder) {
7067cd08927SColin LeMahieu   signedDecoder<11>(MI, tmp, Decoder);
7071e9d1d76SColin LeMahieu   return MCDisassembler::Success;
7081e9d1d76SColin LeMahieu }
7091e9d1d76SColin LeMahieu 
7101e9d1d76SColin LeMahieu static DecodeStatus s11_1ImmDecoder(MCInst &MI, unsigned tmp,
7111e9d1d76SColin LeMahieu                                     uint64_t /*Address*/, const void *Decoder) {
7127cd08927SColin LeMahieu   HexagonMCInstrInfo::addConstant(MI, SignExtend64<12>(tmp), contextFromDecoder(Decoder));
7131e9d1d76SColin LeMahieu   return MCDisassembler::Success;
7141e9d1d76SColin LeMahieu }
7151e9d1d76SColin LeMahieu 
7161e9d1d76SColin LeMahieu static DecodeStatus s11_2ImmDecoder(MCInst &MI, unsigned tmp,
7171e9d1d76SColin LeMahieu                                     uint64_t /*Address*/, const void *Decoder) {
7187cd08927SColin LeMahieu   signedDecoder<13>(MI, tmp, Decoder);
7191e9d1d76SColin LeMahieu   return MCDisassembler::Success;
7201e9d1d76SColin LeMahieu }
7211e9d1d76SColin LeMahieu 
7221e9d1d76SColin LeMahieu static DecodeStatus s11_3ImmDecoder(MCInst &MI, unsigned tmp,
7231e9d1d76SColin LeMahieu                                     uint64_t /*Address*/, const void *Decoder) {
7247cd08927SColin LeMahieu   signedDecoder<14>(MI, tmp, Decoder);
7251e9d1d76SColin LeMahieu   return MCDisassembler::Success;
7261e9d1d76SColin LeMahieu }
7271e9d1d76SColin LeMahieu 
728654dc11bSKrzysztof Parzyszek static DecodeStatus s10_0ImmDecoder(MCInst &MI, unsigned tmp,
7291e9d1d76SColin LeMahieu                                   uint64_t /*Address*/, const void *Decoder) {
7307cd08927SColin LeMahieu   signedDecoder<10>(MI, tmp, Decoder);
7311e9d1d76SColin LeMahieu   return MCDisassembler::Success;
7321e9d1d76SColin LeMahieu }
7331e9d1d76SColin LeMahieu 
734654dc11bSKrzysztof Parzyszek static DecodeStatus s8_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t /*Address*/,
7351e9d1d76SColin LeMahieu                                  const void *Decoder) {
7367cd08927SColin LeMahieu   signedDecoder<8>(MI, tmp, Decoder);
7371e9d1d76SColin LeMahieu   return MCDisassembler::Success;
7381e9d1d76SColin LeMahieu }
7391e9d1d76SColin LeMahieu 
7401e9d1d76SColin LeMahieu static DecodeStatus s6_0ImmDecoder(MCInst &MI, unsigned tmp,
7411e9d1d76SColin LeMahieu                                    uint64_t /*Address*/, const void *Decoder) {
7427cd08927SColin LeMahieu   signedDecoder<6>(MI, tmp, Decoder);
7431e9d1d76SColin LeMahieu   return MCDisassembler::Success;
7441e9d1d76SColin LeMahieu }
7451e9d1d76SColin LeMahieu 
7461e9d1d76SColin LeMahieu static DecodeStatus s4_0ImmDecoder(MCInst &MI, unsigned tmp,
7471e9d1d76SColin LeMahieu                                    uint64_t /*Address*/, const void *Decoder) {
7487cd08927SColin LeMahieu   signedDecoder<4>(MI, tmp, Decoder);
7491e9d1d76SColin LeMahieu   return MCDisassembler::Success;
7501e9d1d76SColin LeMahieu }
7511e9d1d76SColin LeMahieu 
7521e9d1d76SColin LeMahieu static DecodeStatus s4_1ImmDecoder(MCInst &MI, unsigned tmp,
7531e9d1d76SColin LeMahieu                                    uint64_t /*Address*/, const void *Decoder) {
7547cd08927SColin LeMahieu   signedDecoder<5>(MI, tmp, Decoder);
7551e9d1d76SColin LeMahieu   return MCDisassembler::Success;
7561e9d1d76SColin LeMahieu }
7571e9d1d76SColin LeMahieu 
7581e9d1d76SColin LeMahieu static DecodeStatus s4_2ImmDecoder(MCInst &MI, unsigned tmp,
7591e9d1d76SColin LeMahieu                                    uint64_t /*Address*/, const void *Decoder) {
7607cd08927SColin LeMahieu   signedDecoder<6>(MI, tmp, Decoder);
7611e9d1d76SColin LeMahieu   return MCDisassembler::Success;
7621e9d1d76SColin LeMahieu }
7631e9d1d76SColin LeMahieu 
7641e9d1d76SColin LeMahieu static DecodeStatus s4_3ImmDecoder(MCInst &MI, unsigned tmp,
7651e9d1d76SColin LeMahieu                                    uint64_t /*Address*/, const void *Decoder) {
7667cd08927SColin LeMahieu   signedDecoder<7>(MI, tmp, Decoder);
7671e9d1d76SColin LeMahieu   return MCDisassembler::Success;
7681e9d1d76SColin LeMahieu }
7691e9d1d76SColin LeMahieu 
7707c958713SColin LeMahieu static DecodeStatus s4_6ImmDecoder(MCInst &MI, unsigned tmp,
7717c958713SColin LeMahieu                                    uint64_t /*Address*/, const void *Decoder) {
7727cd08927SColin LeMahieu   signedDecoder<10>(MI, tmp, Decoder);
7737c958713SColin LeMahieu   return MCDisassembler::Success;
7747c958713SColin LeMahieu }
7757c958713SColin LeMahieu 
7767c958713SColin LeMahieu static DecodeStatus s3_6ImmDecoder(MCInst &MI, unsigned tmp,
7777c958713SColin LeMahieu                                    uint64_t /*Address*/, const void *Decoder) {
7787cd08927SColin LeMahieu   signedDecoder<19>(MI, tmp, Decoder);
7797c958713SColin LeMahieu   return MCDisassembler::Success;
7807c958713SColin LeMahieu }
7817c958713SColin LeMahieu 
7827cd08927SColin LeMahieu // custom decoder for various jump/call immediates
7837cd08927SColin LeMahieu static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
7847cd08927SColin LeMahieu                                     const void *Decoder) {
7857cd08927SColin LeMahieu   HexagonDisassembler const &Disassembler = disassembler(Decoder);
7867cd08927SColin LeMahieu   unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
7877cd08927SColin LeMahieu   // r13_2 is not extendable, so if there are no extent bits, it's r13_2
7887cd08927SColin LeMahieu   if (Bits == 0)
7897cd08927SColin LeMahieu     Bits = 15;
7907cd08927SColin LeMahieu   uint32_t FullValue = fullValue(*Disassembler.MCII,
7917cd08927SColin LeMahieu                                 **Disassembler.CurrentBundle,
7927cd08927SColin LeMahieu                                 MI, SignExtend64(tmp, Bits));
7937cd08927SColin LeMahieu   int64_t Extended = SignExtend64<32>(FullValue) + Address;
7947cd08927SColin LeMahieu   if (!Disassembler.tryAddingSymbolicOperand(MI, Extended, Address, true,
7957cd08927SColin LeMahieu                                               0, 4))
7967cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
7977cd08927SColin LeMahieu   return MCDisassembler::Success;
7987cd08927SColin LeMahieu }
7997cd08927SColin LeMahieu 
8007cd08927SColin LeMahieu // Addressing mode dependent load store opcode map.
8017cd08927SColin LeMahieu //   - If an insn is preceded by an extender the address is absolute.
8027cd08927SColin LeMahieu //      - memw(##symbol) = r0
8037cd08927SColin LeMahieu //   - If an insn is not preceded by an extender the address is GP relative.
8047cd08927SColin LeMahieu //      - memw(gp + #symbol) = r0
8057cd08927SColin LeMahieu // Please note that the instructions must be ordered in the descending order
8067cd08927SColin LeMahieu // of their opcode.
8077cd08927SColin LeMahieu // HexagonII::INST_ICLASS_ST
808d824f5f0SCraig Topper static const unsigned int StoreConditionalOpcodeData[][2] = {
8097cd08927SColin LeMahieu     {S4_pstorerdfnew_abs, 0xafc02084},
8107cd08927SColin LeMahieu     {S4_pstorerdtnew_abs, 0xafc02080},
8117cd08927SColin LeMahieu     {S4_pstorerdf_abs, 0xafc00084},
8127cd08927SColin LeMahieu     {S4_pstorerdt_abs, 0xafc00080},
8137cd08927SColin LeMahieu     {S4_pstorerinewfnew_abs, 0xafa03084},
8147cd08927SColin LeMahieu     {S4_pstorerinewtnew_abs, 0xafa03080},
8157cd08927SColin LeMahieu     {S4_pstorerhnewfnew_abs, 0xafa02884},
8167cd08927SColin LeMahieu     {S4_pstorerhnewtnew_abs, 0xafa02880},
8177cd08927SColin LeMahieu     {S4_pstorerbnewfnew_abs, 0xafa02084},
8187cd08927SColin LeMahieu     {S4_pstorerbnewtnew_abs, 0xafa02080},
8197cd08927SColin LeMahieu     {S4_pstorerinewf_abs, 0xafa01084},
8207cd08927SColin LeMahieu     {S4_pstorerinewt_abs, 0xafa01080},
8217cd08927SColin LeMahieu     {S4_pstorerhnewf_abs, 0xafa00884},
8227cd08927SColin LeMahieu     {S4_pstorerhnewt_abs, 0xafa00880},
8237cd08927SColin LeMahieu     {S4_pstorerbnewf_abs, 0xafa00084},
8247cd08927SColin LeMahieu     {S4_pstorerbnewt_abs, 0xafa00080},
8257cd08927SColin LeMahieu     {S4_pstorerifnew_abs, 0xaf802084},
8267cd08927SColin LeMahieu     {S4_pstoreritnew_abs, 0xaf802080},
8277cd08927SColin LeMahieu     {S4_pstorerif_abs, 0xaf800084},
8287cd08927SColin LeMahieu     {S4_pstorerit_abs, 0xaf800080},
8297cd08927SColin LeMahieu     {S4_pstorerhfnew_abs, 0xaf402084},
8307cd08927SColin LeMahieu     {S4_pstorerhtnew_abs, 0xaf402080},
8317cd08927SColin LeMahieu     {S4_pstorerhf_abs, 0xaf400084},
8327cd08927SColin LeMahieu     {S4_pstorerht_abs, 0xaf400080},
8337cd08927SColin LeMahieu     {S4_pstorerbfnew_abs, 0xaf002084},
8347cd08927SColin LeMahieu     {S4_pstorerbtnew_abs, 0xaf002080},
8357cd08927SColin LeMahieu     {S4_pstorerbf_abs, 0xaf000084},
8367cd08927SColin LeMahieu     {S4_pstorerbt_abs, 0xaf000080}};
8377cd08927SColin LeMahieu // HexagonII::INST_ICLASS_LD
8387cd08927SColin LeMahieu 
8397cd08927SColin LeMahieu // HexagonII::INST_ICLASS_LD_ST_2
8409675de5bSColin LeMahieu static unsigned int LoadStoreOpcodeData[][2] = {{PS_loadrdabs, 0x49c00000},
8419675de5bSColin LeMahieu                                                 {PS_loadriabs, 0x49800000},
8429675de5bSColin LeMahieu                                                 {PS_loadruhabs, 0x49600000},
8439675de5bSColin LeMahieu                                                 {PS_loadrhabs, 0x49400000},
8449675de5bSColin LeMahieu                                                 {PS_loadrubabs, 0x49200000},
8459675de5bSColin LeMahieu                                                 {PS_loadrbabs, 0x49000000},
8469675de5bSColin LeMahieu                                                 {PS_storerdabs, 0x48c00000},
8479675de5bSColin LeMahieu                                                 {PS_storerinewabs, 0x48a01000},
8489675de5bSColin LeMahieu                                                 {PS_storerhnewabs, 0x48a00800},
8499675de5bSColin LeMahieu                                                 {PS_storerbnewabs, 0x48a00000},
8509675de5bSColin LeMahieu                                                 {PS_storeriabs, 0x48800000},
8519675de5bSColin LeMahieu                                                 {PS_storerfabs, 0x48600000},
8529675de5bSColin LeMahieu                                                 {PS_storerhabs, 0x48400000},
8539675de5bSColin LeMahieu                                                 {PS_storerbabs, 0x48000000}};
854d824f5f0SCraig Topper static const size_t NumCondS = array_lengthof(StoreConditionalOpcodeData);
855d824f5f0SCraig Topper static const size_t NumLS = array_lengthof(LoadStoreOpcodeData);
8567cd08927SColin LeMahieu 
8577cd08927SColin LeMahieu static DecodeStatus decodeSpecial(MCInst &MI, uint32_t insn) {
8587cd08927SColin LeMahieu   unsigned MachineOpcode = 0;
8597cd08927SColin LeMahieu   unsigned LLVMOpcode = 0;
8607cd08927SColin LeMahieu 
8617cd08927SColin LeMahieu   if ((insn & HexagonII::INST_ICLASS_MASK) == HexagonII::INST_ICLASS_ST) {
862d824f5f0SCraig Topper     for (size_t i = 0; i < NumCondS; ++i) {
8637cd08927SColin LeMahieu       if ((insn & StoreConditionalOpcodeData[i][1]) ==
8647cd08927SColin LeMahieu           StoreConditionalOpcodeData[i][1]) {
8657cd08927SColin LeMahieu         MachineOpcode = StoreConditionalOpcodeData[i][1];
8667cd08927SColin LeMahieu         LLVMOpcode = StoreConditionalOpcodeData[i][0];
8677cd08927SColin LeMahieu         break;
8687cd08927SColin LeMahieu       }
8697cd08927SColin LeMahieu     }
8707cd08927SColin LeMahieu   }
8717cd08927SColin LeMahieu   if ((insn & HexagonII::INST_ICLASS_MASK) == HexagonII::INST_ICLASS_LD_ST_2) {
872d824f5f0SCraig Topper     for (size_t i = 0; i < NumLS; ++i) {
8737cd08927SColin LeMahieu       if ((insn & LoadStoreOpcodeData[i][1]) == LoadStoreOpcodeData[i][1]) {
8747cd08927SColin LeMahieu         MachineOpcode = LoadStoreOpcodeData[i][1];
8757cd08927SColin LeMahieu         LLVMOpcode = LoadStoreOpcodeData[i][0];
8767cd08927SColin LeMahieu         break;
8777cd08927SColin LeMahieu       }
8787cd08927SColin LeMahieu     }
8797cd08927SColin LeMahieu   }
8807cd08927SColin LeMahieu 
8817cd08927SColin LeMahieu   if (MachineOpcode) {
8827cd08927SColin LeMahieu     unsigned Value = 0;
8837cd08927SColin LeMahieu     unsigned shift = 0;
8847cd08927SColin LeMahieu     MI.setOpcode(LLVMOpcode);
8857cd08927SColin LeMahieu     // Remove the parse bits from the insn.
8867cd08927SColin LeMahieu     insn &= ~HexagonII::INST_PARSE_MASK;
8877cd08927SColin LeMahieu 
8887cd08927SColin LeMahieu     switch (LLVMOpcode) {
8897cd08927SColin LeMahieu     default:
8907cd08927SColin LeMahieu       return MCDisassembler::Fail;
8917cd08927SColin LeMahieu       break;
8927cd08927SColin LeMahieu 
8937cd08927SColin LeMahieu     case Hexagon::S4_pstorerdf_abs:
8947cd08927SColin LeMahieu     case Hexagon::S4_pstorerdt_abs:
8957cd08927SColin LeMahieu     case Hexagon::S4_pstorerdfnew_abs:
896*82085927SEugene Zelenko     case Hexagon::S4_pstorerdtnew_abs:
8977cd08927SColin LeMahieu       // op: Pv
8987cd08927SColin LeMahieu       Value = insn & UINT64_C(3);
899*82085927SEugene Zelenko       DecodePredRegsRegisterClass(MI, Value, 0, nullptr);
9007cd08927SColin LeMahieu       // op: u6
9017cd08927SColin LeMahieu       Value = (insn >> 12) & UINT64_C(48);
9027cd08927SColin LeMahieu       Value |= (insn >> 3) & UINT64_C(15);
9037cd08927SColin LeMahieu       MI.addOperand(MCOperand::createImm(Value));
9047cd08927SColin LeMahieu       // op: Rtt
9057cd08927SColin LeMahieu       Value = (insn >> 8) & UINT64_C(31);
906*82085927SEugene Zelenko       DecodeDoubleRegsRegisterClass(MI, Value, 0, nullptr);
9077cd08927SColin LeMahieu       break;
9087cd08927SColin LeMahieu 
9097cd08927SColin LeMahieu     case Hexagon::S4_pstorerbnewf_abs:
9107cd08927SColin LeMahieu     case Hexagon::S4_pstorerbnewt_abs:
9117cd08927SColin LeMahieu     case Hexagon::S4_pstorerbnewfnew_abs:
9127cd08927SColin LeMahieu     case Hexagon::S4_pstorerbnewtnew_abs:
9137cd08927SColin LeMahieu     case Hexagon::S4_pstorerhnewf_abs:
9147cd08927SColin LeMahieu     case Hexagon::S4_pstorerhnewt_abs:
9157cd08927SColin LeMahieu     case Hexagon::S4_pstorerhnewfnew_abs:
9167cd08927SColin LeMahieu     case Hexagon::S4_pstorerhnewtnew_abs:
9177cd08927SColin LeMahieu     case Hexagon::S4_pstorerinewf_abs:
9187cd08927SColin LeMahieu     case Hexagon::S4_pstorerinewt_abs:
9197cd08927SColin LeMahieu     case Hexagon::S4_pstorerinewfnew_abs:
920*82085927SEugene Zelenko     case Hexagon::S4_pstorerinewtnew_abs:
9217cd08927SColin LeMahieu       // op: Pv
9227cd08927SColin LeMahieu       Value = insn & UINT64_C(3);
923*82085927SEugene Zelenko       DecodePredRegsRegisterClass(MI, Value, 0, nullptr);
9247cd08927SColin LeMahieu       // op: u6
9257cd08927SColin LeMahieu       Value = (insn >> 12) & UINT64_C(48);
9267cd08927SColin LeMahieu       Value |= (insn >> 3) & UINT64_C(15);
9277cd08927SColin LeMahieu       MI.addOperand(MCOperand::createImm(Value));
9287cd08927SColin LeMahieu       // op: Nt
9297cd08927SColin LeMahieu       Value = (insn >> 8) & UINT64_C(7);
930*82085927SEugene Zelenko       DecodeIntRegsRegisterClass(MI, Value, 0, nullptr);
9317cd08927SColin LeMahieu       break;
9327cd08927SColin LeMahieu 
9337cd08927SColin LeMahieu     case Hexagon::S4_pstorerbf_abs:
9347cd08927SColin LeMahieu     case Hexagon::S4_pstorerbt_abs:
9357cd08927SColin LeMahieu     case Hexagon::S4_pstorerbfnew_abs:
9367cd08927SColin LeMahieu     case Hexagon::S4_pstorerbtnew_abs:
9377cd08927SColin LeMahieu     case Hexagon::S4_pstorerhf_abs:
9387cd08927SColin LeMahieu     case Hexagon::S4_pstorerht_abs:
9397cd08927SColin LeMahieu     case Hexagon::S4_pstorerhfnew_abs:
9407cd08927SColin LeMahieu     case Hexagon::S4_pstorerhtnew_abs:
9417cd08927SColin LeMahieu     case Hexagon::S4_pstorerif_abs:
9427cd08927SColin LeMahieu     case Hexagon::S4_pstorerit_abs:
9437cd08927SColin LeMahieu     case Hexagon::S4_pstorerifnew_abs:
944*82085927SEugene Zelenko     case Hexagon::S4_pstoreritnew_abs:
9457cd08927SColin LeMahieu       // op: Pv
9467cd08927SColin LeMahieu       Value = insn & UINT64_C(3);
947*82085927SEugene Zelenko       DecodePredRegsRegisterClass(MI, Value, 0, nullptr);
9487cd08927SColin LeMahieu       // op: u6
9497cd08927SColin LeMahieu       Value = (insn >> 12) & UINT64_C(48);
9507cd08927SColin LeMahieu       Value |= (insn >> 3) & UINT64_C(15);
9517cd08927SColin LeMahieu       MI.addOperand(MCOperand::createImm(Value));
9527cd08927SColin LeMahieu       // op: Rt
9537cd08927SColin LeMahieu       Value = (insn >> 8) & UINT64_C(31);
954*82085927SEugene Zelenko       DecodeIntRegsRegisterClass(MI, Value, 0, nullptr);
9557cd08927SColin LeMahieu       break;
9567cd08927SColin LeMahieu 
9577cd08927SColin LeMahieu     case Hexagon::L4_ploadrdf_abs:
9587cd08927SColin LeMahieu     case Hexagon::L4_ploadrdt_abs:
9597cd08927SColin LeMahieu     case Hexagon::L4_ploadrdfnew_abs:
960*82085927SEugene Zelenko     case Hexagon::L4_ploadrdtnew_abs:
9617cd08927SColin LeMahieu       // op: Rdd
9627cd08927SColin LeMahieu       Value = insn & UINT64_C(31);
963*82085927SEugene Zelenko       DecodeDoubleRegsRegisterClass(MI, Value, 0, nullptr);
9647cd08927SColin LeMahieu       // op: Pt
9657cd08927SColin LeMahieu       Value = ((insn >> 9) & UINT64_C(3));
966*82085927SEugene Zelenko       DecodePredRegsRegisterClass(MI, Value, 0, nullptr);
9677cd08927SColin LeMahieu       // op: u6
9687cd08927SColin LeMahieu       Value = ((insn >> 15) & UINT64_C(62));
9697cd08927SColin LeMahieu       Value |= ((insn >> 8) & UINT64_C(1));
9707cd08927SColin LeMahieu       MI.addOperand(MCOperand::createImm(Value));
9717cd08927SColin LeMahieu       break;
9727cd08927SColin LeMahieu 
9737cd08927SColin LeMahieu     case Hexagon::L4_ploadrbf_abs:
9747cd08927SColin LeMahieu     case Hexagon::L4_ploadrbt_abs:
9757cd08927SColin LeMahieu     case Hexagon::L4_ploadrbfnew_abs:
9767cd08927SColin LeMahieu     case Hexagon::L4_ploadrbtnew_abs:
9777cd08927SColin LeMahieu     case Hexagon::L4_ploadrhf_abs:
9787cd08927SColin LeMahieu     case Hexagon::L4_ploadrht_abs:
9797cd08927SColin LeMahieu     case Hexagon::L4_ploadrhfnew_abs:
9807cd08927SColin LeMahieu     case Hexagon::L4_ploadrhtnew_abs:
9817cd08927SColin LeMahieu     case Hexagon::L4_ploadrubf_abs:
9827cd08927SColin LeMahieu     case Hexagon::L4_ploadrubt_abs:
9837cd08927SColin LeMahieu     case Hexagon::L4_ploadrubfnew_abs:
9847cd08927SColin LeMahieu     case Hexagon::L4_ploadrubtnew_abs:
9857cd08927SColin LeMahieu     case Hexagon::L4_ploadruhf_abs:
9867cd08927SColin LeMahieu     case Hexagon::L4_ploadruht_abs:
9877cd08927SColin LeMahieu     case Hexagon::L4_ploadruhfnew_abs:
9887cd08927SColin LeMahieu     case Hexagon::L4_ploadruhtnew_abs:
9897cd08927SColin LeMahieu     case Hexagon::L4_ploadrif_abs:
9907cd08927SColin LeMahieu     case Hexagon::L4_ploadrit_abs:
9917cd08927SColin LeMahieu     case Hexagon::L4_ploadrifnew_abs:
9927cd08927SColin LeMahieu     case Hexagon::L4_ploadritnew_abs:
9937cd08927SColin LeMahieu       // op: Rd
9947cd08927SColin LeMahieu       Value = insn & UINT64_C(31);
995*82085927SEugene Zelenko       DecodeIntRegsRegisterClass(MI, Value, 0, nullptr);
9967cd08927SColin LeMahieu       // op: Pt
9977cd08927SColin LeMahieu       Value = (insn >> 9) & UINT64_C(3);
998*82085927SEugene Zelenko       DecodePredRegsRegisterClass(MI, Value, 0, nullptr);
9997cd08927SColin LeMahieu       // op: u6
10007cd08927SColin LeMahieu       Value = (insn >> 15) & UINT64_C(62);
10017cd08927SColin LeMahieu       Value |= (insn >> 8) & UINT64_C(1);
10027cd08927SColin LeMahieu       MI.addOperand(MCOperand::createImm(Value));
10037cd08927SColin LeMahieu       break;
10047cd08927SColin LeMahieu 
10057cd08927SColin LeMahieu     // op: g16_2
10069675de5bSColin LeMahieu     case (Hexagon::PS_loadriabs):
10077cd08927SColin LeMahieu       ++shift;
10087cd08927SColin LeMahieu     // op: g16_1
10099675de5bSColin LeMahieu     case Hexagon::PS_loadrhabs:
10109675de5bSColin LeMahieu     case Hexagon::PS_loadruhabs:
10117cd08927SColin LeMahieu       ++shift;
10127cd08927SColin LeMahieu     // op: g16_0
10139675de5bSColin LeMahieu     case Hexagon::PS_loadrbabs:
1014*82085927SEugene Zelenko     case Hexagon::PS_loadrubabs:
10157cd08927SColin LeMahieu       // op: Rd
10167cd08927SColin LeMahieu       Value |= insn & UINT64_C(31);
1017*82085927SEugene Zelenko       DecodeIntRegsRegisterClass(MI, Value, 0, nullptr);
10187cd08927SColin LeMahieu       Value = (insn >> 11) & UINT64_C(49152);
10197cd08927SColin LeMahieu       Value |= (insn >> 7) & UINT64_C(15872);
10207cd08927SColin LeMahieu       Value |= (insn >> 5) & UINT64_C(511);
10217cd08927SColin LeMahieu       MI.addOperand(MCOperand::createImm(Value << shift));
10227cd08927SColin LeMahieu       break;
10237cd08927SColin LeMahieu 
1024*82085927SEugene Zelenko     case Hexagon::PS_loadrdabs:
10257cd08927SColin LeMahieu       Value = insn & UINT64_C(31);
1026*82085927SEugene Zelenko       DecodeDoubleRegsRegisterClass(MI, Value, 0, nullptr);
10277cd08927SColin LeMahieu       Value = (insn >> 11) & UINT64_C(49152);
10287cd08927SColin LeMahieu       Value |= (insn >> 7) & UINT64_C(15872);
10297cd08927SColin LeMahieu       Value |= (insn >> 5) & UINT64_C(511);
10307cd08927SColin LeMahieu       MI.addOperand(MCOperand::createImm(Value << 3));
10317cd08927SColin LeMahieu       break;
10327cd08927SColin LeMahieu 
1033*82085927SEugene Zelenko     case Hexagon::PS_storerdabs:
10347cd08927SColin LeMahieu       // op: g16_3
10357cd08927SColin LeMahieu       Value = (insn >> 11) & UINT64_C(49152);
10367cd08927SColin LeMahieu       Value |= (insn >> 7) & UINT64_C(15872);
10377cd08927SColin LeMahieu       Value |= (insn >> 5) & UINT64_C(256);
10387cd08927SColin LeMahieu       Value |= insn & UINT64_C(255);
10397cd08927SColin LeMahieu       MI.addOperand(MCOperand::createImm(Value << 3));
10407cd08927SColin LeMahieu       // op: Rtt
10417cd08927SColin LeMahieu       Value = (insn >> 8) & UINT64_C(31);
1042*82085927SEugene Zelenko       DecodeDoubleRegsRegisterClass(MI, Value, 0, nullptr);
10437cd08927SColin LeMahieu       break;
10447cd08927SColin LeMahieu 
10457cd08927SColin LeMahieu     // op: g16_2
10469675de5bSColin LeMahieu     case Hexagon::PS_storerinewabs:
10477cd08927SColin LeMahieu       ++shift;
10487cd08927SColin LeMahieu     // op: g16_1
10499675de5bSColin LeMahieu     case Hexagon::PS_storerhnewabs:
10507cd08927SColin LeMahieu       ++shift;
10517cd08927SColin LeMahieu     // op: g16_0
1052*82085927SEugene Zelenko     case Hexagon::PS_storerbnewabs:
10537cd08927SColin LeMahieu       Value = (insn >> 11) & UINT64_C(49152);
10547cd08927SColin LeMahieu       Value |= (insn >> 7) & UINT64_C(15872);
10557cd08927SColin LeMahieu       Value |= (insn >> 5) & UINT64_C(256);
10567cd08927SColin LeMahieu       Value |= insn & UINT64_C(255);
10577cd08927SColin LeMahieu       MI.addOperand(MCOperand::createImm(Value << shift));
10587cd08927SColin LeMahieu       // op: Nt
10597cd08927SColin LeMahieu       Value = (insn >> 8) & UINT64_C(7);
1060*82085927SEugene Zelenko       DecodeIntRegsRegisterClass(MI, Value, 0, nullptr);
10617cd08927SColin LeMahieu       break;
10627cd08927SColin LeMahieu 
10637cd08927SColin LeMahieu     // op: g16_2
10649675de5bSColin LeMahieu     case Hexagon::PS_storeriabs:
10657cd08927SColin LeMahieu       ++shift;
10667cd08927SColin LeMahieu     // op: g16_1
10679675de5bSColin LeMahieu     case Hexagon::PS_storerhabs:
10689675de5bSColin LeMahieu     case Hexagon::PS_storerfabs:
10697cd08927SColin LeMahieu       ++shift;
10707cd08927SColin LeMahieu     // op: g16_0
1071*82085927SEugene Zelenko     case Hexagon::PS_storerbabs:
10727cd08927SColin LeMahieu       Value = (insn >> 11) & UINT64_C(49152);
10737cd08927SColin LeMahieu       Value |= (insn >> 7) & UINT64_C(15872);
10747cd08927SColin LeMahieu       Value |= (insn >> 5) & UINT64_C(256);
10757cd08927SColin LeMahieu       Value |= insn & UINT64_C(255);
10767cd08927SColin LeMahieu       MI.addOperand(MCOperand::createImm(Value << shift));
10777cd08927SColin LeMahieu       // op: Rt
10787cd08927SColin LeMahieu       Value = (insn >> 8) & UINT64_C(31);
1079*82085927SEugene Zelenko       DecodeIntRegsRegisterClass(MI, Value, 0, nullptr);
10807cd08927SColin LeMahieu       break;
10817cd08927SColin LeMahieu     }
10827cd08927SColin LeMahieu     return MCDisassembler::Success;
10837cd08927SColin LeMahieu   }
10847cd08927SColin LeMahieu   return MCDisassembler::Fail;
10857cd08927SColin LeMahieu }
10867cd08927SColin LeMahieu 
10877cd08927SColin LeMahieu static DecodeStatus decodeImmext(MCInst &MI, uint32_t insn,
10887cd08927SColin LeMahieu                                  void const *Decoder) {
10897cd08927SColin LeMahieu   // Instruction Class for a constant a extender: bits 31:28 = 0x0000
10907cd08927SColin LeMahieu   if ((~insn & 0xf0000000) == 0xf0000000) {
10917cd08927SColin LeMahieu     unsigned Value;
10927cd08927SColin LeMahieu     // 27:16 High 12 bits of 26-bit extender.
10937cd08927SColin LeMahieu     Value = (insn & 0x0fff0000) << 4;
10947cd08927SColin LeMahieu     // 13:0 Low 14 bits of 26-bit extender.
10957cd08927SColin LeMahieu     Value |= ((insn & 0x3fff) << 6);
10967cd08927SColin LeMahieu     MI.setOpcode(Hexagon::A4_ext);
10977cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(MI, Value, contextFromDecoder(Decoder));
10987cd08927SColin LeMahieu     return MCDisassembler::Success;
10997cd08927SColin LeMahieu   }
11007cd08927SColin LeMahieu   return MCDisassembler::Fail;
11017cd08927SColin LeMahieu }
11027cd08927SColin LeMahieu 
1103be8c453dSColin LeMahieu // These values are from HexagonGenMCCodeEmitter.inc and HexagonIsetDx.td
1104be8c453dSColin LeMahieu enum subInstBinaryValues {
11058ed1aee9SColin LeMahieu   SA1_addi_BITS = 0x0000,
11068ed1aee9SColin LeMahieu   SA1_addi_MASK = 0x1800,
11078ed1aee9SColin LeMahieu   SA1_addrx_BITS = 0x1800,
11088ed1aee9SColin LeMahieu   SA1_addrx_MASK = 0x1f00,
11098ed1aee9SColin LeMahieu   SA1_addsp_BITS = 0x0c00,
11108ed1aee9SColin LeMahieu   SA1_addsp_MASK = 0x1c00,
11118ed1aee9SColin LeMahieu   SA1_and1_BITS = 0x1200,
11128ed1aee9SColin LeMahieu   SA1_and1_MASK = 0x1f00,
11138ed1aee9SColin LeMahieu   SA1_clrf_BITS = 0x1a70,
11148ed1aee9SColin LeMahieu   SA1_clrf_MASK = 0x1e70,
11158ed1aee9SColin LeMahieu   SA1_clrfnew_BITS = 0x1a50,
11168ed1aee9SColin LeMahieu   SA1_clrfnew_MASK = 0x1e70,
11178ed1aee9SColin LeMahieu   SA1_clrt_BITS = 0x1a60,
11188ed1aee9SColin LeMahieu   SA1_clrt_MASK = 0x1e70,
11198ed1aee9SColin LeMahieu   SA1_clrtnew_BITS = 0x1a40,
11208ed1aee9SColin LeMahieu   SA1_clrtnew_MASK = 0x1e70,
11218ed1aee9SColin LeMahieu   SA1_cmpeqi_BITS = 0x1900,
11228ed1aee9SColin LeMahieu   SA1_cmpeqi_MASK = 0x1f00,
11238ed1aee9SColin LeMahieu   SA1_combine0i_BITS = 0x1c00,
11248ed1aee9SColin LeMahieu   SA1_combine0i_MASK = 0x1d18,
11258ed1aee9SColin LeMahieu   SA1_combine1i_BITS = 0x1c08,
11268ed1aee9SColin LeMahieu   SA1_combine1i_MASK = 0x1d18,
11278ed1aee9SColin LeMahieu   SA1_combine2i_BITS = 0x1c10,
11288ed1aee9SColin LeMahieu   SA1_combine2i_MASK = 0x1d18,
11298ed1aee9SColin LeMahieu   SA1_combine3i_BITS = 0x1c18,
11308ed1aee9SColin LeMahieu   SA1_combine3i_MASK = 0x1d18,
11318ed1aee9SColin LeMahieu   SA1_combinerz_BITS = 0x1d08,
11328ed1aee9SColin LeMahieu   SA1_combinerz_MASK = 0x1d08,
11338ed1aee9SColin LeMahieu   SA1_combinezr_BITS = 0x1d00,
11348ed1aee9SColin LeMahieu   SA1_combinezr_MASK = 0x1d08,
11358ed1aee9SColin LeMahieu   SA1_dec_BITS = 0x1300,
11368ed1aee9SColin LeMahieu   SA1_dec_MASK = 0x1f00,
11378ed1aee9SColin LeMahieu   SA1_inc_BITS = 0x1100,
11388ed1aee9SColin LeMahieu   SA1_inc_MASK = 0x1f00,
11398ed1aee9SColin LeMahieu   SA1_seti_BITS = 0x0800,
11408ed1aee9SColin LeMahieu   SA1_seti_MASK = 0x1c00,
11418ed1aee9SColin LeMahieu   SA1_setin1_BITS = 0x1a00,
11428ed1aee9SColin LeMahieu   SA1_setin1_MASK = 0x1e40,
11438ed1aee9SColin LeMahieu   SA1_sxtb_BITS = 0x1500,
11448ed1aee9SColin LeMahieu   SA1_sxtb_MASK = 0x1f00,
11458ed1aee9SColin LeMahieu   SA1_sxth_BITS = 0x1400,
11468ed1aee9SColin LeMahieu   SA1_sxth_MASK = 0x1f00,
11478ed1aee9SColin LeMahieu   SA1_tfr_BITS = 0x1000,
11488ed1aee9SColin LeMahieu   SA1_tfr_MASK = 0x1f00,
11498ed1aee9SColin LeMahieu   SA1_zxtb_BITS = 0x1700,
11508ed1aee9SColin LeMahieu   SA1_zxtb_MASK = 0x1f00,
11518ed1aee9SColin LeMahieu   SA1_zxth_BITS = 0x1600,
11528ed1aee9SColin LeMahieu   SA1_zxth_MASK = 0x1f00,
11538ed1aee9SColin LeMahieu   SL1_loadri_io_BITS = 0x0000,
11548ed1aee9SColin LeMahieu   SL1_loadri_io_MASK = 0x1000,
11558ed1aee9SColin LeMahieu   SL1_loadrub_io_BITS = 0x1000,
11568ed1aee9SColin LeMahieu   SL1_loadrub_io_MASK = 0x1000,
11578ed1aee9SColin LeMahieu   SL2_deallocframe_BITS = 0x1f00,
11588ed1aee9SColin LeMahieu   SL2_deallocframe_MASK = 0x1fc0,
11598ed1aee9SColin LeMahieu   SL2_jumpr31_BITS = 0x1fc0,
11608ed1aee9SColin LeMahieu   SL2_jumpr31_MASK = 0x1fc4,
11618ed1aee9SColin LeMahieu   SL2_jumpr31_f_BITS = 0x1fc5,
11628ed1aee9SColin LeMahieu   SL2_jumpr31_f_MASK = 0x1fc7,
11638ed1aee9SColin LeMahieu   SL2_jumpr31_fnew_BITS = 0x1fc7,
11648ed1aee9SColin LeMahieu   SL2_jumpr31_fnew_MASK = 0x1fc7,
11658ed1aee9SColin LeMahieu   SL2_jumpr31_t_BITS = 0x1fc4,
11668ed1aee9SColin LeMahieu   SL2_jumpr31_t_MASK = 0x1fc7,
11678ed1aee9SColin LeMahieu   SL2_jumpr31_tnew_BITS = 0x1fc6,
11688ed1aee9SColin LeMahieu   SL2_jumpr31_tnew_MASK = 0x1fc7,
11698ed1aee9SColin LeMahieu   SL2_loadrb_io_BITS = 0x1000,
11708ed1aee9SColin LeMahieu   SL2_loadrb_io_MASK = 0x1800,
11718ed1aee9SColin LeMahieu   SL2_loadrd_sp_BITS = 0x1e00,
11728ed1aee9SColin LeMahieu   SL2_loadrd_sp_MASK = 0x1f00,
11738ed1aee9SColin LeMahieu   SL2_loadrh_io_BITS = 0x0000,
11748ed1aee9SColin LeMahieu   SL2_loadrh_io_MASK = 0x1800,
11758ed1aee9SColin LeMahieu   SL2_loadri_sp_BITS = 0x1c00,
11768ed1aee9SColin LeMahieu   SL2_loadri_sp_MASK = 0x1e00,
11778ed1aee9SColin LeMahieu   SL2_loadruh_io_BITS = 0x0800,
11788ed1aee9SColin LeMahieu   SL2_loadruh_io_MASK = 0x1800,
11798ed1aee9SColin LeMahieu   SL2_return_BITS = 0x1f40,
11808ed1aee9SColin LeMahieu   SL2_return_MASK = 0x1fc4,
11818ed1aee9SColin LeMahieu   SL2_return_f_BITS = 0x1f45,
11828ed1aee9SColin LeMahieu   SL2_return_f_MASK = 0x1fc7,
11838ed1aee9SColin LeMahieu   SL2_return_fnew_BITS = 0x1f47,
11848ed1aee9SColin LeMahieu   SL2_return_fnew_MASK = 0x1fc7,
11858ed1aee9SColin LeMahieu   SL2_return_t_BITS = 0x1f44,
11868ed1aee9SColin LeMahieu   SL2_return_t_MASK = 0x1fc7,
11878ed1aee9SColin LeMahieu   SL2_return_tnew_BITS = 0x1f46,
11888ed1aee9SColin LeMahieu   SL2_return_tnew_MASK = 0x1fc7,
11898ed1aee9SColin LeMahieu   SS1_storeb_io_BITS = 0x1000,
11908ed1aee9SColin LeMahieu   SS1_storeb_io_MASK = 0x1000,
11918ed1aee9SColin LeMahieu   SS1_storew_io_BITS = 0x0000,
11928ed1aee9SColin LeMahieu   SS1_storew_io_MASK = 0x1000,
11938ed1aee9SColin LeMahieu   SS2_allocframe_BITS = 0x1c00,
11948ed1aee9SColin LeMahieu   SS2_allocframe_MASK = 0x1e00,
11958ed1aee9SColin LeMahieu   SS2_storebi0_BITS = 0x1200,
11968ed1aee9SColin LeMahieu   SS2_storebi0_MASK = 0x1f00,
11978ed1aee9SColin LeMahieu   SS2_storebi1_BITS = 0x1300,
11988ed1aee9SColin LeMahieu   SS2_storebi1_MASK = 0x1f00,
11998ed1aee9SColin LeMahieu   SS2_stored_sp_BITS = 0x0a00,
12008ed1aee9SColin LeMahieu   SS2_stored_sp_MASK = 0x1e00,
12018ed1aee9SColin LeMahieu   SS2_storeh_io_BITS = 0x0000,
12028ed1aee9SColin LeMahieu   SS2_storeh_io_MASK = 0x1800,
12038ed1aee9SColin LeMahieu   SS2_storew_sp_BITS = 0x0800,
12048ed1aee9SColin LeMahieu   SS2_storew_sp_MASK = 0x1e00,
12058ed1aee9SColin LeMahieu   SS2_storewi0_BITS = 0x1000,
12068ed1aee9SColin LeMahieu   SS2_storewi0_MASK = 0x1f00,
12078ed1aee9SColin LeMahieu   SS2_storewi1_BITS = 0x1100,
12088ed1aee9SColin LeMahieu   SS2_storewi1_MASK = 0x1f00
1209be8c453dSColin LeMahieu };
1210be8c453dSColin LeMahieu 
1211be8c453dSColin LeMahieu static unsigned GetSubinstOpcode(unsigned IClass, unsigned inst, unsigned &op,
1212be8c453dSColin LeMahieu                                  raw_ostream &os) {
1213be8c453dSColin LeMahieu   switch (IClass) {
1214be8c453dSColin LeMahieu   case HexagonII::HSIG_L1:
12158ed1aee9SColin LeMahieu     if ((inst & SL1_loadri_io_MASK) == SL1_loadri_io_BITS)
12168ed1aee9SColin LeMahieu       op = Hexagon::SL1_loadri_io;
12178ed1aee9SColin LeMahieu     else if ((inst & SL1_loadrub_io_MASK) == SL1_loadrub_io_BITS)
12188ed1aee9SColin LeMahieu       op = Hexagon::SL1_loadrub_io;
1219be8c453dSColin LeMahieu     else {
1220be8c453dSColin LeMahieu       os << "<unknown subinstruction>";
1221be8c453dSColin LeMahieu       return MCDisassembler::Fail;
1222be8c453dSColin LeMahieu     }
1223be8c453dSColin LeMahieu     break;
1224be8c453dSColin LeMahieu   case HexagonII::HSIG_L2:
12258ed1aee9SColin LeMahieu     if ((inst & SL2_deallocframe_MASK) == SL2_deallocframe_BITS)
12268ed1aee9SColin LeMahieu       op = Hexagon::SL2_deallocframe;
12278ed1aee9SColin LeMahieu     else if ((inst & SL2_jumpr31_MASK) == SL2_jumpr31_BITS)
12288ed1aee9SColin LeMahieu       op = Hexagon::SL2_jumpr31;
12298ed1aee9SColin LeMahieu     else if ((inst & SL2_jumpr31_f_MASK) == SL2_jumpr31_f_BITS)
12308ed1aee9SColin LeMahieu       op = Hexagon::SL2_jumpr31_f;
12318ed1aee9SColin LeMahieu     else if ((inst & SL2_jumpr31_fnew_MASK) == SL2_jumpr31_fnew_BITS)
12328ed1aee9SColin LeMahieu       op = Hexagon::SL2_jumpr31_fnew;
12338ed1aee9SColin LeMahieu     else if ((inst & SL2_jumpr31_t_MASK) == SL2_jumpr31_t_BITS)
12348ed1aee9SColin LeMahieu       op = Hexagon::SL2_jumpr31_t;
12358ed1aee9SColin LeMahieu     else if ((inst & SL2_jumpr31_tnew_MASK) == SL2_jumpr31_tnew_BITS)
12368ed1aee9SColin LeMahieu       op = Hexagon::SL2_jumpr31_tnew;
12378ed1aee9SColin LeMahieu     else if ((inst & SL2_loadrb_io_MASK) == SL2_loadrb_io_BITS)
12388ed1aee9SColin LeMahieu       op = Hexagon::SL2_loadrb_io;
12398ed1aee9SColin LeMahieu     else if ((inst & SL2_loadrd_sp_MASK) == SL2_loadrd_sp_BITS)
12408ed1aee9SColin LeMahieu       op = Hexagon::SL2_loadrd_sp;
12418ed1aee9SColin LeMahieu     else if ((inst & SL2_loadrh_io_MASK) == SL2_loadrh_io_BITS)
12428ed1aee9SColin LeMahieu       op = Hexagon::SL2_loadrh_io;
12438ed1aee9SColin LeMahieu     else if ((inst & SL2_loadri_sp_MASK) == SL2_loadri_sp_BITS)
12448ed1aee9SColin LeMahieu       op = Hexagon::SL2_loadri_sp;
12458ed1aee9SColin LeMahieu     else if ((inst & SL2_loadruh_io_MASK) == SL2_loadruh_io_BITS)
12468ed1aee9SColin LeMahieu       op = Hexagon::SL2_loadruh_io;
12478ed1aee9SColin LeMahieu     else if ((inst & SL2_return_MASK) == SL2_return_BITS)
12488ed1aee9SColin LeMahieu       op = Hexagon::SL2_return;
12498ed1aee9SColin LeMahieu     else if ((inst & SL2_return_f_MASK) == SL2_return_f_BITS)
12508ed1aee9SColin LeMahieu       op = Hexagon::SL2_return_f;
12518ed1aee9SColin LeMahieu     else if ((inst & SL2_return_fnew_MASK) == SL2_return_fnew_BITS)
12528ed1aee9SColin LeMahieu       op = Hexagon::SL2_return_fnew;
12538ed1aee9SColin LeMahieu     else if ((inst & SL2_return_t_MASK) == SL2_return_t_BITS)
12548ed1aee9SColin LeMahieu       op = Hexagon::SL2_return_t;
12558ed1aee9SColin LeMahieu     else if ((inst & SL2_return_tnew_MASK) == SL2_return_tnew_BITS)
12568ed1aee9SColin LeMahieu       op = Hexagon::SL2_return_tnew;
1257be8c453dSColin LeMahieu     else {
1258be8c453dSColin LeMahieu       os << "<unknown subinstruction>";
1259be8c453dSColin LeMahieu       return MCDisassembler::Fail;
1260be8c453dSColin LeMahieu     }
1261be8c453dSColin LeMahieu     break;
1262be8c453dSColin LeMahieu   case HexagonII::HSIG_A:
12638ed1aee9SColin LeMahieu     if ((inst & SA1_addi_MASK) == SA1_addi_BITS)
12648ed1aee9SColin LeMahieu       op = Hexagon::SA1_addi;
12658ed1aee9SColin LeMahieu     else if ((inst & SA1_addrx_MASK) == SA1_addrx_BITS)
12668ed1aee9SColin LeMahieu       op = Hexagon::SA1_addrx;
12678ed1aee9SColin LeMahieu     else if ((inst & SA1_addsp_MASK) == SA1_addsp_BITS)
12688ed1aee9SColin LeMahieu       op = Hexagon::SA1_addsp;
12698ed1aee9SColin LeMahieu     else if ((inst & SA1_and1_MASK) == SA1_and1_BITS)
12708ed1aee9SColin LeMahieu       op = Hexagon::SA1_and1;
12718ed1aee9SColin LeMahieu     else if ((inst & SA1_clrf_MASK) == SA1_clrf_BITS)
12728ed1aee9SColin LeMahieu       op = Hexagon::SA1_clrf;
12738ed1aee9SColin LeMahieu     else if ((inst & SA1_clrfnew_MASK) == SA1_clrfnew_BITS)
12748ed1aee9SColin LeMahieu       op = Hexagon::SA1_clrfnew;
12758ed1aee9SColin LeMahieu     else if ((inst & SA1_clrt_MASK) == SA1_clrt_BITS)
12768ed1aee9SColin LeMahieu       op = Hexagon::SA1_clrt;
12778ed1aee9SColin LeMahieu     else if ((inst & SA1_clrtnew_MASK) == SA1_clrtnew_BITS)
12788ed1aee9SColin LeMahieu       op = Hexagon::SA1_clrtnew;
12798ed1aee9SColin LeMahieu     else if ((inst & SA1_cmpeqi_MASK) == SA1_cmpeqi_BITS)
12808ed1aee9SColin LeMahieu       op = Hexagon::SA1_cmpeqi;
12818ed1aee9SColin LeMahieu     else if ((inst & SA1_combine0i_MASK) == SA1_combine0i_BITS)
12828ed1aee9SColin LeMahieu       op = Hexagon::SA1_combine0i;
12838ed1aee9SColin LeMahieu     else if ((inst & SA1_combine1i_MASK) == SA1_combine1i_BITS)
12848ed1aee9SColin LeMahieu       op = Hexagon::SA1_combine1i;
12858ed1aee9SColin LeMahieu     else if ((inst & SA1_combine2i_MASK) == SA1_combine2i_BITS)
12868ed1aee9SColin LeMahieu       op = Hexagon::SA1_combine2i;
12878ed1aee9SColin LeMahieu     else if ((inst & SA1_combine3i_MASK) == SA1_combine3i_BITS)
12888ed1aee9SColin LeMahieu       op = Hexagon::SA1_combine3i;
12898ed1aee9SColin LeMahieu     else if ((inst & SA1_combinerz_MASK) == SA1_combinerz_BITS)
12908ed1aee9SColin LeMahieu       op = Hexagon::SA1_combinerz;
12918ed1aee9SColin LeMahieu     else if ((inst & SA1_combinezr_MASK) == SA1_combinezr_BITS)
12928ed1aee9SColin LeMahieu       op = Hexagon::SA1_combinezr;
12938ed1aee9SColin LeMahieu     else if ((inst & SA1_dec_MASK) == SA1_dec_BITS)
12948ed1aee9SColin LeMahieu       op = Hexagon::SA1_dec;
12958ed1aee9SColin LeMahieu     else if ((inst & SA1_inc_MASK) == SA1_inc_BITS)
12968ed1aee9SColin LeMahieu       op = Hexagon::SA1_inc;
12978ed1aee9SColin LeMahieu     else if ((inst & SA1_seti_MASK) == SA1_seti_BITS)
12988ed1aee9SColin LeMahieu       op = Hexagon::SA1_seti;
12998ed1aee9SColin LeMahieu     else if ((inst & SA1_setin1_MASK) == SA1_setin1_BITS)
13008ed1aee9SColin LeMahieu       op = Hexagon::SA1_setin1;
13018ed1aee9SColin LeMahieu     else if ((inst & SA1_sxtb_MASK) == SA1_sxtb_BITS)
13028ed1aee9SColin LeMahieu       op = Hexagon::SA1_sxtb;
13038ed1aee9SColin LeMahieu     else if ((inst & SA1_sxth_MASK) == SA1_sxth_BITS)
13048ed1aee9SColin LeMahieu       op = Hexagon::SA1_sxth;
13058ed1aee9SColin LeMahieu     else if ((inst & SA1_tfr_MASK) == SA1_tfr_BITS)
13068ed1aee9SColin LeMahieu       op = Hexagon::SA1_tfr;
13078ed1aee9SColin LeMahieu     else if ((inst & SA1_zxtb_MASK) == SA1_zxtb_BITS)
13088ed1aee9SColin LeMahieu       op = Hexagon::SA1_zxtb;
13098ed1aee9SColin LeMahieu     else if ((inst & SA1_zxth_MASK) == SA1_zxth_BITS)
13108ed1aee9SColin LeMahieu       op = Hexagon::SA1_zxth;
1311be8c453dSColin LeMahieu     else {
1312be8c453dSColin LeMahieu       os << "<unknown subinstruction>";
1313be8c453dSColin LeMahieu       return MCDisassembler::Fail;
1314be8c453dSColin LeMahieu     }
1315be8c453dSColin LeMahieu     break;
1316be8c453dSColin LeMahieu   case HexagonII::HSIG_S1:
13178ed1aee9SColin LeMahieu     if ((inst & SS1_storeb_io_MASK) == SS1_storeb_io_BITS)
13188ed1aee9SColin LeMahieu       op = Hexagon::SS1_storeb_io;
13198ed1aee9SColin LeMahieu     else if ((inst & SS1_storew_io_MASK) == SS1_storew_io_BITS)
13208ed1aee9SColin LeMahieu       op = Hexagon::SS1_storew_io;
1321be8c453dSColin LeMahieu     else {
1322be8c453dSColin LeMahieu       os << "<unknown subinstruction>";
1323be8c453dSColin LeMahieu       return MCDisassembler::Fail;
1324be8c453dSColin LeMahieu     }
1325be8c453dSColin LeMahieu     break;
1326be8c453dSColin LeMahieu   case HexagonII::HSIG_S2:
13278ed1aee9SColin LeMahieu     if ((inst & SS2_allocframe_MASK) == SS2_allocframe_BITS)
13288ed1aee9SColin LeMahieu       op = Hexagon::SS2_allocframe;
13298ed1aee9SColin LeMahieu     else if ((inst & SS2_storebi0_MASK) == SS2_storebi0_BITS)
13308ed1aee9SColin LeMahieu       op = Hexagon::SS2_storebi0;
13318ed1aee9SColin LeMahieu     else if ((inst & SS2_storebi1_MASK) == SS2_storebi1_BITS)
13328ed1aee9SColin LeMahieu       op = Hexagon::SS2_storebi1;
13338ed1aee9SColin LeMahieu     else if ((inst & SS2_stored_sp_MASK) == SS2_stored_sp_BITS)
13348ed1aee9SColin LeMahieu       op = Hexagon::SS2_stored_sp;
13358ed1aee9SColin LeMahieu     else if ((inst & SS2_storeh_io_MASK) == SS2_storeh_io_BITS)
13368ed1aee9SColin LeMahieu       op = Hexagon::SS2_storeh_io;
13378ed1aee9SColin LeMahieu     else if ((inst & SS2_storew_sp_MASK) == SS2_storew_sp_BITS)
13388ed1aee9SColin LeMahieu       op = Hexagon::SS2_storew_sp;
13398ed1aee9SColin LeMahieu     else if ((inst & SS2_storewi0_MASK) == SS2_storewi0_BITS)
13408ed1aee9SColin LeMahieu       op = Hexagon::SS2_storewi0;
13418ed1aee9SColin LeMahieu     else if ((inst & SS2_storewi1_MASK) == SS2_storewi1_BITS)
13428ed1aee9SColin LeMahieu       op = Hexagon::SS2_storewi1;
1343be8c453dSColin LeMahieu     else {
1344be8c453dSColin LeMahieu       os << "<unknown subinstruction>";
1345be8c453dSColin LeMahieu       return MCDisassembler::Fail;
1346be8c453dSColin LeMahieu     }
1347be8c453dSColin LeMahieu     break;
1348be8c453dSColin LeMahieu   default:
1349be8c453dSColin LeMahieu     os << "<unknown>";
1350be8c453dSColin LeMahieu     return MCDisassembler::Fail;
1351be8c453dSColin LeMahieu   }
1352be8c453dSColin LeMahieu   return MCDisassembler::Success;
1353be8c453dSColin LeMahieu }
1354be8c453dSColin LeMahieu 
1355be8c453dSColin LeMahieu static unsigned getRegFromSubinstEncoding(unsigned encoded_reg) {
1356be8c453dSColin LeMahieu   if (encoded_reg < 8)
1357be8c453dSColin LeMahieu     return Hexagon::R0 + encoded_reg;
1358be8c453dSColin LeMahieu   else if (encoded_reg < 16)
1359be8c453dSColin LeMahieu     return Hexagon::R0 + encoded_reg + 8;
13607cd08927SColin LeMahieu 
13617cd08927SColin LeMahieu   // patently false value
1362be8c453dSColin LeMahieu   return Hexagon::NoRegister;
1363be8c453dSColin LeMahieu }
1364be8c453dSColin LeMahieu 
1365be8c453dSColin LeMahieu static unsigned getDRegFromSubinstEncoding(unsigned encoded_dreg) {
1366be8c453dSColin LeMahieu   if (encoded_dreg < 4)
1367be8c453dSColin LeMahieu     return Hexagon::D0 + encoded_dreg;
1368be8c453dSColin LeMahieu   else if (encoded_dreg < 8)
1369be8c453dSColin LeMahieu     return Hexagon::D0 + encoded_dreg + 4;
13707cd08927SColin LeMahieu 
13717cd08927SColin LeMahieu   // patently false value
1372be8c453dSColin LeMahieu   return Hexagon::NoRegister;
1373be8c453dSColin LeMahieu }
1374be8c453dSColin LeMahieu 
13757cd08927SColin LeMahieu void HexagonDisassembler::addSubinstOperands(MCInst *MI, unsigned opcode,
13767cd08927SColin LeMahieu                                              unsigned inst) const {
1377be8c453dSColin LeMahieu   int64_t operand;
1378be8c453dSColin LeMahieu   MCOperand Op;
1379be8c453dSColin LeMahieu   switch (opcode) {
13808ed1aee9SColin LeMahieu   case Hexagon::SL2_deallocframe:
13818ed1aee9SColin LeMahieu   case Hexagon::SL2_jumpr31:
13828ed1aee9SColin LeMahieu   case Hexagon::SL2_jumpr31_f:
13838ed1aee9SColin LeMahieu   case Hexagon::SL2_jumpr31_fnew:
13848ed1aee9SColin LeMahieu   case Hexagon::SL2_jumpr31_t:
13858ed1aee9SColin LeMahieu   case Hexagon::SL2_jumpr31_tnew:
13868ed1aee9SColin LeMahieu   case Hexagon::SL2_return:
13878ed1aee9SColin LeMahieu   case Hexagon::SL2_return_f:
13888ed1aee9SColin LeMahieu   case Hexagon::SL2_return_fnew:
13898ed1aee9SColin LeMahieu   case Hexagon::SL2_return_t:
13908ed1aee9SColin LeMahieu   case Hexagon::SL2_return_tnew:
1391be8c453dSColin LeMahieu     // no operands for these instructions
1392be8c453dSColin LeMahieu     break;
13938ed1aee9SColin LeMahieu   case Hexagon::SS2_allocframe:
1394be8c453dSColin LeMahieu     // u 8-4{5_3}
1395be8c453dSColin LeMahieu     operand = ((inst & 0x1f0) >> 4) << 3;
13967cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1397be8c453dSColin LeMahieu     break;
13988ed1aee9SColin LeMahieu   case Hexagon::SL1_loadri_io:
1399be8c453dSColin LeMahieu     // Rd 3-0, Rs 7-4, u 11-8{4_2}
1400be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding(inst & 0xf);
1401be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1402be8c453dSColin LeMahieu     MI->addOperand(Op);
1403be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding((inst & 0xf0) >> 4);
1404be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1405be8c453dSColin LeMahieu     MI->addOperand(Op);
1406be8c453dSColin LeMahieu     operand = (inst & 0xf00) >> 6;
14077cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1408be8c453dSColin LeMahieu     break;
14098ed1aee9SColin LeMahieu   case Hexagon::SL1_loadrub_io:
1410be8c453dSColin LeMahieu     // Rd 3-0, Rs 7-4, u 11-8
1411be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding(inst & 0xf);
1412be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1413be8c453dSColin LeMahieu     MI->addOperand(Op);
1414be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding((inst & 0xf0) >> 4);
1415be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1416be8c453dSColin LeMahieu     MI->addOperand(Op);
1417be8c453dSColin LeMahieu     operand = (inst & 0xf00) >> 8;
14187cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1419be8c453dSColin LeMahieu     break;
14208ed1aee9SColin LeMahieu   case Hexagon::SL2_loadrb_io:
1421be8c453dSColin LeMahieu     // Rd 3-0, Rs 7-4, u 10-8
1422be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding(inst & 0xf);
1423be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1424be8c453dSColin LeMahieu     MI->addOperand(Op);
1425be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding((inst & 0xf0) >> 4);
1426be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1427be8c453dSColin LeMahieu     MI->addOperand(Op);
1428be8c453dSColin LeMahieu     operand = (inst & 0x700) >> 8;
14297cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1430be8c453dSColin LeMahieu     break;
14318ed1aee9SColin LeMahieu   case Hexagon::SL2_loadrh_io:
14328ed1aee9SColin LeMahieu   case Hexagon::SL2_loadruh_io:
1433be8c453dSColin LeMahieu     // Rd 3-0, Rs 7-4, u 10-8{3_1}
1434be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding(inst & 0xf);
1435be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1436be8c453dSColin LeMahieu     MI->addOperand(Op);
1437be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding((inst & 0xf0) >> 4);
1438be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1439be8c453dSColin LeMahieu     MI->addOperand(Op);
1440be8c453dSColin LeMahieu     operand = ((inst & 0x700) >> 8) << 1;
14417cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1442be8c453dSColin LeMahieu     break;
14438ed1aee9SColin LeMahieu   case Hexagon::SL2_loadrd_sp:
1444be8c453dSColin LeMahieu     // Rdd 2-0, u 7-3{5_3}
1445be8c453dSColin LeMahieu     operand = getDRegFromSubinstEncoding(inst & 0x7);
1446be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1447be8c453dSColin LeMahieu     MI->addOperand(Op);
1448be8c453dSColin LeMahieu     operand = ((inst & 0x0f8) >> 3) << 3;
14497cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1450be8c453dSColin LeMahieu     break;
14518ed1aee9SColin LeMahieu   case Hexagon::SL2_loadri_sp:
1452be8c453dSColin LeMahieu     // Rd 3-0, u 8-4{5_2}
1453be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding(inst & 0xf);
1454be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1455be8c453dSColin LeMahieu     MI->addOperand(Op);
1456be8c453dSColin LeMahieu     operand = ((inst & 0x1f0) >> 4) << 2;
14577cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1458be8c453dSColin LeMahieu     break;
14598ed1aee9SColin LeMahieu   case Hexagon::SA1_addi:
1460be8c453dSColin LeMahieu     // Rx 3-0 (x2), s7 10-4
1461be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding(inst & 0xf);
1462be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1463be8c453dSColin LeMahieu     MI->addOperand(Op);
1464be8c453dSColin LeMahieu     MI->addOperand(Op);
1465be8c453dSColin LeMahieu     operand = SignExtend64<7>((inst & 0x7f0) >> 4);
14667cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1467be8c453dSColin LeMahieu     break;
14688ed1aee9SColin LeMahieu   case Hexagon::SA1_addrx:
1469be8c453dSColin LeMahieu     // Rx 3-0 (x2), Rs 7-4
1470be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding(inst & 0xf);
1471be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1472be8c453dSColin LeMahieu     MI->addOperand(Op);
1473be8c453dSColin LeMahieu     MI->addOperand(Op);
1474be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding((inst & 0xf0) >> 4);
1475be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1476be8c453dSColin LeMahieu     MI->addOperand(Op);
1477bb0cdfb9SColin LeMahieu     break;
14788ed1aee9SColin LeMahieu   case Hexagon::SA1_and1:
14798ed1aee9SColin LeMahieu   case Hexagon::SA1_dec:
14808ed1aee9SColin LeMahieu   case Hexagon::SA1_inc:
14818ed1aee9SColin LeMahieu   case Hexagon::SA1_sxtb:
14828ed1aee9SColin LeMahieu   case Hexagon::SA1_sxth:
14838ed1aee9SColin LeMahieu   case Hexagon::SA1_tfr:
14848ed1aee9SColin LeMahieu   case Hexagon::SA1_zxtb:
14858ed1aee9SColin LeMahieu   case Hexagon::SA1_zxth:
1486be8c453dSColin LeMahieu     // Rd 3-0, Rs 7-4
1487be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding(inst & 0xf);
1488be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1489be8c453dSColin LeMahieu     MI->addOperand(Op);
1490be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding((inst & 0xf0) >> 4);
1491be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1492be8c453dSColin LeMahieu     MI->addOperand(Op);
1493be8c453dSColin LeMahieu     break;
14948ed1aee9SColin LeMahieu   case Hexagon::SA1_addsp:
1495be8c453dSColin LeMahieu     // Rd 3-0, u 9-4{6_2}
1496be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding(inst & 0xf);
1497be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1498be8c453dSColin LeMahieu     MI->addOperand(Op);
1499be8c453dSColin LeMahieu     operand = ((inst & 0x3f0) >> 4) << 2;
15007cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1501be8c453dSColin LeMahieu     break;
15028ed1aee9SColin LeMahieu   case Hexagon::SA1_seti:
1503be8c453dSColin LeMahieu     // Rd 3-0, u 9-4
1504be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding(inst & 0xf);
1505be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1506be8c453dSColin LeMahieu     MI->addOperand(Op);
1507be8c453dSColin LeMahieu     operand = (inst & 0x3f0) >> 4;
15087cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1509be8c453dSColin LeMahieu     break;
15108ed1aee9SColin LeMahieu   case Hexagon::SA1_clrf:
15118ed1aee9SColin LeMahieu   case Hexagon::SA1_clrfnew:
15128ed1aee9SColin LeMahieu   case Hexagon::SA1_clrt:
15138ed1aee9SColin LeMahieu   case Hexagon::SA1_clrtnew:
15148ed1aee9SColin LeMahieu   case Hexagon::SA1_setin1:
1515be8c453dSColin LeMahieu     // Rd 3-0
1516be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding(inst & 0xf);
1517be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1518be8c453dSColin LeMahieu     MI->addOperand(Op);
15198ed1aee9SColin LeMahieu     if (opcode == Hexagon::SA1_setin1)
152073e0ad82SKrzysztof Parzyszek       break;
152173e0ad82SKrzysztof Parzyszek     MI->addOperand(MCOperand::createReg(Hexagon::P0));
1522be8c453dSColin LeMahieu     break;
15238ed1aee9SColin LeMahieu   case Hexagon::SA1_cmpeqi:
1524be8c453dSColin LeMahieu     // Rs 7-4, u 1-0
1525be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding((inst & 0xf0) >> 4);
1526be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1527be8c453dSColin LeMahieu     MI->addOperand(Op);
1528be8c453dSColin LeMahieu     operand = inst & 0x3;
15297cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1530be8c453dSColin LeMahieu     break;
15318ed1aee9SColin LeMahieu   case Hexagon::SA1_combine0i:
15328ed1aee9SColin LeMahieu   case Hexagon::SA1_combine1i:
15338ed1aee9SColin LeMahieu   case Hexagon::SA1_combine2i:
15348ed1aee9SColin LeMahieu   case Hexagon::SA1_combine3i:
1535be8c453dSColin LeMahieu     // Rdd 2-0, u 6-5
1536be8c453dSColin LeMahieu     operand = getDRegFromSubinstEncoding(inst & 0x7);
1537be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1538be8c453dSColin LeMahieu     MI->addOperand(Op);
1539be8c453dSColin LeMahieu     operand = (inst & 0x060) >> 5;
15407cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1541be8c453dSColin LeMahieu     break;
15428ed1aee9SColin LeMahieu   case Hexagon::SA1_combinerz:
15438ed1aee9SColin LeMahieu   case Hexagon::SA1_combinezr:
1544be8c453dSColin LeMahieu     // Rdd 2-0, Rs 7-4
1545be8c453dSColin LeMahieu     operand = getDRegFromSubinstEncoding(inst & 0x7);
1546be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1547be8c453dSColin LeMahieu     MI->addOperand(Op);
1548be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding((inst & 0xf0) >> 4);
1549be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1550be8c453dSColin LeMahieu     MI->addOperand(Op);
1551be8c453dSColin LeMahieu     break;
15528ed1aee9SColin LeMahieu   case Hexagon::SS1_storeb_io:
1553be8c453dSColin LeMahieu     // Rs 7-4, u 11-8, Rt 3-0
1554be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding((inst & 0xf0) >> 4);
1555be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1556be8c453dSColin LeMahieu     MI->addOperand(Op);
1557be8c453dSColin LeMahieu     operand = (inst & 0xf00) >> 8;
15587cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1559be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding(inst & 0xf);
1560be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1561be8c453dSColin LeMahieu     MI->addOperand(Op);
1562be8c453dSColin LeMahieu     break;
15638ed1aee9SColin LeMahieu   case Hexagon::SS1_storew_io:
1564be8c453dSColin LeMahieu     // Rs 7-4, u 11-8{4_2}, Rt 3-0
1565be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding((inst & 0xf0) >> 4);
1566be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1567be8c453dSColin LeMahieu     MI->addOperand(Op);
1568be8c453dSColin LeMahieu     operand = ((inst & 0xf00) >> 8) << 2;
15697cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1570be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding(inst & 0xf);
1571be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1572be8c453dSColin LeMahieu     MI->addOperand(Op);
1573be8c453dSColin LeMahieu     break;
15748ed1aee9SColin LeMahieu   case Hexagon::SS2_storebi0:
15758ed1aee9SColin LeMahieu   case Hexagon::SS2_storebi1:
1576be8c453dSColin LeMahieu     // Rs 7-4, u 3-0
1577be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding((inst & 0xf0) >> 4);
1578be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1579be8c453dSColin LeMahieu     MI->addOperand(Op);
1580be8c453dSColin LeMahieu     operand = inst & 0xf;
15817cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1582be8c453dSColin LeMahieu     break;
15838ed1aee9SColin LeMahieu   case Hexagon::SS2_storewi0:
15848ed1aee9SColin LeMahieu   case Hexagon::SS2_storewi1:
1585be8c453dSColin LeMahieu     // Rs 7-4, u 3-0{4_2}
1586be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding((inst & 0xf0) >> 4);
1587be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1588be8c453dSColin LeMahieu     MI->addOperand(Op);
1589be8c453dSColin LeMahieu     operand = (inst & 0xf) << 2;
15907cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1591be8c453dSColin LeMahieu     break;
15928ed1aee9SColin LeMahieu   case Hexagon::SS2_stored_sp:
1593be8c453dSColin LeMahieu     // s 8-3{6_3}, Rtt 2-0
1594be8c453dSColin LeMahieu     operand = SignExtend64<9>(((inst & 0x1f8) >> 3) << 3);
15957cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1596be8c453dSColin LeMahieu     operand = getDRegFromSubinstEncoding(inst & 0x7);
1597be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1598be8c453dSColin LeMahieu     MI->addOperand(Op);
1599160f73e3SColin LeMahieu     break;
16008ed1aee9SColin LeMahieu   case Hexagon::SS2_storeh_io:
1601be8c453dSColin LeMahieu     // Rs 7-4, u 10-8{3_1}, Rt 3-0
1602be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding((inst & 0xf0) >> 4);
1603be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1604be8c453dSColin LeMahieu     MI->addOperand(Op);
1605be8c453dSColin LeMahieu     operand = ((inst & 0x700) >> 8) << 1;
16067cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1607be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding(inst & 0xf);
1608be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1609be8c453dSColin LeMahieu     MI->addOperand(Op);
1610be8c453dSColin LeMahieu     break;
16118ed1aee9SColin LeMahieu   case Hexagon::SS2_storew_sp:
1612be8c453dSColin LeMahieu     // u 8-4{5_2}, Rd 3-0
1613be8c453dSColin LeMahieu     operand = ((inst & 0x1f0) >> 4) << 2;
16147cd08927SColin LeMahieu     HexagonMCInstrInfo::addConstant(*MI, operand, getContext());
1615be8c453dSColin LeMahieu     operand = getRegFromSubinstEncoding(inst & 0xf);
1616be8c453dSColin LeMahieu     Op = MCOperand::createReg(operand);
1617be8c453dSColin LeMahieu     MI->addOperand(Op);
1618be8c453dSColin LeMahieu     break;
1619be8c453dSColin LeMahieu   default:
1620be8c453dSColin LeMahieu     // don't crash with an invalid subinstruction
1621be8c453dSColin LeMahieu     // llvm_unreachable("Invalid subinstruction in duplex instruction");
1622be8c453dSColin LeMahieu     break;
1623be8c453dSColin LeMahieu   }
1624be8c453dSColin LeMahieu }
1625