blob: 9051c0fafa9b2c47ac5a1401f38f52d3e732794b [file] [log] [blame]
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +00001//===- HexagonDisassembler.cpp - Disassembler for Hexagon ISA -------------===//
NAKAMURA Takumi729be142014-10-27 12:37:26 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Colin LeMahieu7cd08922015-11-09 04:07:48 +000010#define DEBUG_TYPE "hexagon-disassembler"
11
NAKAMURA Takumi729be142014-10-27 12:37:26 +000012#include "MCTargetDesc/HexagonBaseInfo.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000013#include "MCTargetDesc/HexagonMCChecker.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000014#include "MCTargetDesc/HexagonMCInstrInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "MCTargetDesc/HexagonMCTargetDesc.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000016#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/STLExtras.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000018#include "llvm/MC/MCContext.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "llvm/MC/MCDisassembler/MCDisassembler.h"
NAKAMURA Takumi729be142014-10-27 12:37:26 +000020#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCFixedLenDisassembler.h"
22#include "llvm/MC/MCInst.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000023#include "llvm/MC/MCInstrInfo.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000024#include "llvm/MC/MCRegisterInfo.h"
NAKAMURA Takumi729be142014-10-27 12:37:26 +000025#include "llvm/MC/MCSubtargetInfo.h"
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000026#include "llvm/Support/Endian.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000027#include "llvm/Support/MathExtras.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000028#include "llvm/Support/TargetRegistry.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000029#include "llvm/Support/raw_ostream.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000030#include <cassert>
31#include <cstddef>
32#include <cstdint>
33#include <memory>
NAKAMURA Takumi729be142014-10-27 12:37:26 +000034
35using namespace llvm;
Colin LeMahieu68d967d2015-05-29 14:44:13 +000036using namespace Hexagon;
NAKAMURA Takumi729be142014-10-27 12:37:26 +000037
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000038using DecodeStatus = MCDisassembler::DecodeStatus;
NAKAMURA Takumi729be142014-10-27 12:37:26 +000039
40namespace {
Eugene Zelenko82085922016-12-13 22:13:50 +000041
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000042/// Hexagon disassembler for all Hexagon platforms.
NAKAMURA Takumi729be142014-10-27 12:37:26 +000043class HexagonDisassembler : public MCDisassembler {
44public:
Colin LeMahieu7cd08922015-11-09 04:07:48 +000045 std::unique_ptr<MCInstrInfo const> const MCII;
Colin LeMahieu68d967d2015-05-29 14:44:13 +000046 std::unique_ptr<MCInst *> CurrentBundle;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000047 mutable MCInst const *CurrentExtender;
Eugene Zelenko82085922016-12-13 22:13:50 +000048
Colin LeMahieu7cd08922015-11-09 04:07:48 +000049 HexagonDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
50 MCInstrInfo const *MCII)
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000051 : MCDisassembler(STI, Ctx), MCII(MCII), CurrentBundle(new MCInst *),
52 CurrentExtender(nullptr) {}
NAKAMURA Takumi729be142014-10-27 12:37:26 +000053
Colin LeMahieu68d967d2015-05-29 14:44:13 +000054 DecodeStatus getSingleInstruction(MCInst &Instr, MCInst &MCB,
55 ArrayRef<uint8_t> Bytes, uint64_t Address,
56 raw_ostream &VStream, raw_ostream &CStream,
57 bool &Complete) const;
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000058 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +000059 ArrayRef<uint8_t> Bytes, uint64_t Address,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000060 raw_ostream &VStream,
61 raw_ostream &CStream) const override;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000062 void remapInstruction(MCInst &Instr) const;
NAKAMURA Takumi729be142014-10-27 12:37:26 +000063};
Eugene Zelenko82085922016-12-13 22:13:50 +000064
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000065static uint64_t fullValue(HexagonDisassembler const &Disassembler, MCInst &MI,
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000066 int64_t Value) {
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000067 MCInstrInfo MCII = *Disassembler.MCII;
68 if (!Disassembler.CurrentExtender ||
69 MI.size() != HexagonMCInstrInfo::getExtendableOp(MCII, MI))
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000070 return Value;
71 unsigned Alignment = HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
72 uint32_t Lower6 = static_cast<uint32_t>(Value >> Alignment) & 0x3f;
73 int64_t Bits;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000074 bool Success =
75 Disassembler.CurrentExtender->getOperand(0).getExpr()->evaluateAsAbsolute(
76 Bits);
77 assert(Success);
78 (void)Success;
79 uint64_t Upper26 = static_cast<uint64_t>(Bits);
80 uint64_t Operand = Upper26 | Lower6;
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000081 return Operand;
82}
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000083static HexagonDisassembler const &disassembler(void const *Decoder) {
84 return *static_cast<HexagonDisassembler const *>(Decoder);
85}
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000086template <size_t T>
87static void signedDecoder(MCInst &MI, unsigned tmp, const void *Decoder) {
88 HexagonDisassembler const &Disassembler = disassembler(Decoder);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000089 int64_t FullValue = fullValue(Disassembler, MI, SignExtend64<T>(tmp));
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000090 int64_t Extended = SignExtend64<32>(FullValue);
91 HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
92}
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000093}
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000094
Colin LeMahieu7cd08922015-11-09 04:07:48 +000095// Forward declare these because the auto-generated code will reference them.
96// Definitions are further down.
97
98static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
Colin LeMahieube8c4532015-06-05 16:00:11 +000099 uint64_t Address,
100 const void *Decoder);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000101static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
102 unsigned RegNo,
103 uint64_t Address,
104 const void *Decoder);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000105static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
106 uint64_t Address,
107 const void *Decoder);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000108static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000109 uint64_t Address,
110 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000111static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
112 uint64_t Address,
113 const void *Decoder);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000114static DecodeStatus
115DecodeGeneralDoubleLow8RegsRegisterClass(MCInst &Inst, unsigned RegNo,
116 uint64_t Address, const void *Decoder);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000117static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000118 uint64_t Address,
119 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000120static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
121 uint64_t Address,
122 const void *Decoder);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000123static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000124 uint64_t Address,
125 const void *Decoder);
Colin LeMahieuf3db8842014-12-19 19:06:32 +0000126static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
Colin LeMahieube8c4532015-06-05 16:00:11 +0000127 uint64_t Address,
128 const void *Decoder);
Krzysztof Parzyszek22a21d42018-03-01 17:03:26 +0000129static DecodeStatus DecodeGuestRegsRegisterClass(MCInst &Inst, unsigned RegNo,
130 uint64_t Address,
131 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000132static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
133 uint64_t Address,
134 const void *Decoder);
Colin LeMahieu404d5b22015-02-10 16:59:36 +0000135static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
Colin LeMahieube8c4532015-06-05 16:00:11 +0000136 uint64_t Address,
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000137 const void *Decoder);
Krzysztof Parzyszek22a21d42018-03-01 17:03:26 +0000138static DecodeStatus DecodeGuestRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
139 uint64_t Address,
140 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000141
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000142static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
143 uint64_t Address, const void *Decoder);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000144static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
145 uint64_t /*Address*/, const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000146static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
147 const void *Decoder);
Krzysztof Parzyszek9dafa8a2018-12-03 21:59:21 +0000148#include "HexagonDepDecoders.h"
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000149#include "HexagonGenDisassemblerTables.inc"
150
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000151static MCDisassembler *createHexagonDisassembler(const Target &T,
152 const MCSubtargetInfo &STI,
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000153 MCContext &Ctx) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000154 return new HexagonDisassembler(STI, Ctx, T.createMCInstrInfo());
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000155}
156
157extern "C" void LLVMInitializeHexagonDisassembler() {
Mehdi Aminif42454b2016-10-09 23:00:34 +0000158 TargetRegistry::RegisterMCDisassembler(getTheHexagonTarget(),
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000159 createHexagonDisassembler);
160}
161
162DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000163 ArrayRef<uint8_t> Bytes,
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000164 uint64_t Address,
165 raw_ostream &os,
166 raw_ostream &cs) const {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000167 DecodeStatus Result = DecodeStatus::Success;
168 bool Complete = false;
169 Size = 0;
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000170
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000171 *CurrentBundle = &MI;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000172 MI.setOpcode(Hexagon::BUNDLE);
173 MI.addOperand(MCOperand::createImm(0));
Eugene Zelenko82085922016-12-13 22:13:50 +0000174 while (Result == Success && !Complete) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000175 if (Bytes.size() < HEXAGON_INSTR_SIZE)
176 return MCDisassembler::Fail;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000177 MCInst *Inst = new (getContext()) MCInst;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000178 Result = getSingleInstruction(*Inst, MI, Bytes, Address, os, cs, Complete);
179 MI.addOperand(MCOperand::createInst(Inst));
180 Size += HEXAGON_INSTR_SIZE;
181 Bytes = Bytes.slice(HEXAGON_INSTR_SIZE);
182 }
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000183 if (Result == MCDisassembler::Fail)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000184 return Result;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000185 if (Size > HEXAGON_MAX_PACKET_SIZE)
186 return MCDisassembler::Fail;
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000187 HexagonMCChecker Checker(getContext(), *MCII, STI, MI,
188 *getContext().getRegisterInfo(), false);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000189 if (!Checker.check())
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000190 return MCDisassembler::Fail;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000191 remapInstruction(MI);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000192 return MCDisassembler::Success;
193}
194
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000195void HexagonDisassembler::remapInstruction(MCInst &Instr) const {
196 for (auto I: HexagonMCInstrInfo::bundleInstructions(Instr)) {
197 auto &MI = const_cast<MCInst &>(*I.getInst());
198 switch (MI.getOpcode()) {
199 case Hexagon::S2_allocframe:
200 if (MI.getOperand(0).getReg() == Hexagon::R29) {
201 MI.setOpcode(Hexagon::S6_allocframe_to_raw);
202 MI.erase(MI.begin () + 1);
203 MI.erase(MI.begin ());
204 }
205 break;
206 case Hexagon::L2_deallocframe:
207 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
208 MI.getOperand(1).getReg() == Hexagon::R30) {
209 MI.setOpcode(L6_deallocframe_map_to_raw);
210 MI.erase(MI.begin () + 1);
211 MI.erase(MI.begin ());
212 }
213 break;
214 case Hexagon::L4_return:
215 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
216 MI.getOperand(1).getReg() == Hexagon::R30) {
217 MI.setOpcode(L6_return_map_to_raw);
218 MI.erase(MI.begin () + 1);
219 MI.erase(MI.begin ());
220 }
221 break;
222 case Hexagon::L4_return_t:
223 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
224 MI.getOperand(2).getReg() == Hexagon::R30) {
225 MI.setOpcode(L4_return_map_to_raw_t);
226 MI.erase(MI.begin () + 2);
227 MI.erase(MI.begin ());
228 }
229 break;
230 case Hexagon::L4_return_f:
231 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
232 MI.getOperand(2).getReg() == Hexagon::R30) {
233 MI.setOpcode(L4_return_map_to_raw_f);
234 MI.erase(MI.begin () + 2);
235 MI.erase(MI.begin ());
236 }
237 break;
238 case Hexagon::L4_return_tnew_pt:
239 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
240 MI.getOperand(2).getReg() == Hexagon::R30) {
241 MI.setOpcode(L4_return_map_to_raw_tnew_pt);
242 MI.erase(MI.begin () + 2);
243 MI.erase(MI.begin ());
244 }
245 break;
246 case Hexagon::L4_return_fnew_pt:
247 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
248 MI.getOperand(2).getReg() == Hexagon::R30) {
249 MI.setOpcode(L4_return_map_to_raw_fnew_pt);
250 MI.erase(MI.begin () + 2);
251 MI.erase(MI.begin ());
252 }
253 break;
254 case Hexagon::L4_return_tnew_pnt:
255 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
256 MI.getOperand(2).getReg() == Hexagon::R30) {
257 MI.setOpcode(L4_return_map_to_raw_tnew_pnt);
258 MI.erase(MI.begin () + 2);
259 MI.erase(MI.begin ());
260 }
261 break;
262 case Hexagon::L4_return_fnew_pnt:
263 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
264 MI.getOperand(2).getReg() == Hexagon::R30) {
265 MI.setOpcode(L4_return_map_to_raw_fnew_pnt);
266 MI.erase(MI.begin () + 2);
267 MI.erase(MI.begin ());
268 }
269 break;
270 }
271 }
272}
273
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000274static void adjustDuplex(MCInst &MI, MCContext &Context) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000275 switch (MI.getOpcode()) {
276 case Hexagon::SA1_setin1:
277 MI.insert(MI.begin() + 1,
278 MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
279 break;
280 case Hexagon::SA1_dec:
281 MI.insert(MI.begin() + 2,
282 MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
283 break;
284 default:
285 break;
286 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000287}
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000288
289DecodeStatus HexagonDisassembler::getSingleInstruction(
290 MCInst &MI, MCInst &MCB, ArrayRef<uint8_t> Bytes, uint64_t Address,
291 raw_ostream &os, raw_ostream &cs, bool &Complete) const {
292 assert(Bytes.size() >= HEXAGON_INSTR_SIZE);
293
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000294 uint32_t Instruction = support::endian::read32le(Bytes.data());
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000295
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000296 auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB);
297 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
298 HexagonII::INST_PARSE_LOOP_END) {
299 if (BundleSize == 0)
300 HexagonMCInstrInfo::setInnerLoop(MCB);
301 else if (BundleSize == 1)
302 HexagonMCInstrInfo::setOuterLoop(MCB);
303 else
304 return DecodeStatus::Fail;
305 }
306
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000307 CurrentExtender = HexagonMCInstrInfo::extenderForIndex(
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000308 MCB, HexagonMCInstrInfo::bundleSize(MCB));
309
310 DecodeStatus Result = DecodeStatus::Fail;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000311 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
Colin LeMahieube8c4532015-06-05 16:00:11 +0000312 HexagonII::INST_PARSE_DUPLEX) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000313 unsigned duplexIClass;
314 uint8_t const *DecodeLow, *DecodeHigh;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000315 duplexIClass = ((Instruction >> 28) & 0xe) | ((Instruction >> 13) & 0x1);
316 switch (duplexIClass) {
317 default:
318 return MCDisassembler::Fail;
319 case 0:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000320 DecodeLow = DecoderTableSUBINSN_L132;
321 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000322 break;
323 case 1:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000324 DecodeLow = DecoderTableSUBINSN_L232;
325 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000326 break;
327 case 2:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000328 DecodeLow = DecoderTableSUBINSN_L232;
329 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000330 break;
331 case 3:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000332 DecodeLow = DecoderTableSUBINSN_A32;
333 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000334 break;
335 case 4:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000336 DecodeLow = DecoderTableSUBINSN_L132;
337 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000338 break;
339 case 5:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000340 DecodeLow = DecoderTableSUBINSN_L232;
341 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000342 break;
343 case 6:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000344 DecodeLow = DecoderTableSUBINSN_S132;
345 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000346 break;
347 case 7:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000348 DecodeLow = DecoderTableSUBINSN_S232;
349 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000350 break;
351 case 8:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000352 DecodeLow = DecoderTableSUBINSN_S132;
353 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000354 break;
355 case 9:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000356 DecodeLow = DecoderTableSUBINSN_S132;
357 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000358 break;
359 case 10:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000360 DecodeLow = DecoderTableSUBINSN_S132;
361 DecodeHigh = DecoderTableSUBINSN_S132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000362 break;
363 case 11:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000364 DecodeLow = DecoderTableSUBINSN_S232;
365 DecodeHigh = DecoderTableSUBINSN_S132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000366 break;
367 case 12:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000368 DecodeLow = DecoderTableSUBINSN_S232;
369 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000370 break;
371 case 13:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000372 DecodeLow = DecoderTableSUBINSN_S232;
373 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000374 break;
375 case 14:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000376 DecodeLow = DecoderTableSUBINSN_S232;
377 DecodeHigh = DecoderTableSUBINSN_S232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000378 break;
379 }
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000380 MI.setOpcode(Hexagon::DuplexIClass0 + duplexIClass);
Colin LeMahieube8c4532015-06-05 16:00:11 +0000381 MCInst *MILow = new (getContext()) MCInst;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000382 MCInst *MIHigh = new (getContext()) MCInst;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000383 auto TmpExtender = CurrentExtender;
384 CurrentExtender =
385 nullptr; // constant extenders in duplex must always be in slot 1
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000386 Result = decodeInstruction(DecodeLow, *MILow, Instruction & 0x1fff, Address,
387 this, STI);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000388 CurrentExtender = TmpExtender;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000389 if (Result != DecodeStatus::Success)
390 return DecodeStatus::Fail;
391 adjustDuplex(*MILow, getContext());
392 Result = decodeInstruction(
393 DecodeHigh, *MIHigh, (Instruction >> 16) & 0x1fff, Address, this, STI);
394 if (Result != DecodeStatus::Success)
395 return DecodeStatus::Fail;
396 adjustDuplex(*MIHigh, getContext());
Colin LeMahieube8c4532015-06-05 16:00:11 +0000397 MCOperand OPLow = MCOperand::createInst(MILow);
398 MCOperand OPHigh = MCOperand::createInst(MIHigh);
399 MI.addOperand(OPLow);
400 MI.addOperand(OPHigh);
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000401 Complete = true;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000402 } else {
403 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
404 HexagonII::INST_PARSE_PACKET_END)
405 Complete = true;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000406
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000407 if (CurrentExtender != nullptr)
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000408 Result = decodeInstruction(DecoderTableMustExtend32, MI, Instruction,
409 Address, this, STI);
410
411 if (Result != MCDisassembler::Success)
412 Result = decodeInstruction(DecoderTable32, MI, Instruction, Address, this,
413 STI);
414
415 if (Result != MCDisassembler::Success &&
416 STI.getFeatureBits()[Hexagon::ExtensionHVX])
417 Result = decodeInstruction(DecoderTableEXT_mmvec32, MI, Instruction,
418 Address, this, STI);
419
Colin LeMahieube8c4532015-06-05 16:00:11 +0000420 }
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000421
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000422 switch (MI.getOpcode()) {
Colin LeMahieu81707542016-12-05 04:29:00 +0000423 case Hexagon::J4_cmpeqn1_f_jumpnv_nt:
424 case Hexagon::J4_cmpeqn1_f_jumpnv_t:
425 case Hexagon::J4_cmpeqn1_fp0_jump_nt:
426 case Hexagon::J4_cmpeqn1_fp0_jump_t:
427 case Hexagon::J4_cmpeqn1_fp1_jump_nt:
428 case Hexagon::J4_cmpeqn1_fp1_jump_t:
429 case Hexagon::J4_cmpeqn1_t_jumpnv_nt:
430 case Hexagon::J4_cmpeqn1_t_jumpnv_t:
431 case Hexagon::J4_cmpeqn1_tp0_jump_nt:
432 case Hexagon::J4_cmpeqn1_tp0_jump_t:
433 case Hexagon::J4_cmpeqn1_tp1_jump_nt:
434 case Hexagon::J4_cmpeqn1_tp1_jump_t:
435 case Hexagon::J4_cmpgtn1_f_jumpnv_nt:
436 case Hexagon::J4_cmpgtn1_f_jumpnv_t:
437 case Hexagon::J4_cmpgtn1_fp0_jump_nt:
438 case Hexagon::J4_cmpgtn1_fp0_jump_t:
439 case Hexagon::J4_cmpgtn1_fp1_jump_nt:
440 case Hexagon::J4_cmpgtn1_fp1_jump_t:
441 case Hexagon::J4_cmpgtn1_t_jumpnv_nt:
442 case Hexagon::J4_cmpgtn1_t_jumpnv_t:
443 case Hexagon::J4_cmpgtn1_tp0_jump_nt:
444 case Hexagon::J4_cmpgtn1_tp0_jump_t:
445 case Hexagon::J4_cmpgtn1_tp1_jump_nt:
446 case Hexagon::J4_cmpgtn1_tp1_jump_t:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000447 MI.insert(MI.begin() + 1,
448 MCOperand::createExpr(MCConstantExpr::create(-1, getContext())));
Colin LeMahieu81707542016-12-05 04:29:00 +0000449 break;
450 default:
451 break;
452 }
453
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000454 if (HexagonMCInstrInfo::isNewValue(*MCII, MI)) {
455 unsigned OpIndex = HexagonMCInstrInfo::getNewValueOp(*MCII, MI);
456 MCOperand &MCO = MI.getOperand(OpIndex);
457 assert(MCO.isReg() && "New value consumers must be registers");
458 unsigned Register =
459 getContext().getRegisterInfo()->getEncodingValue(MCO.getReg());
460 if ((Register & 0x6) == 0)
461 // HexagonPRM 10.11 Bit 1-2 == 0 is reserved
462 return MCDisassembler::Fail;
463 unsigned Lookback = (Register & 0x6) >> 1;
464 unsigned Offset = 1;
465 bool Vector = HexagonMCInstrInfo::isVector(*MCII, MI);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000466 bool PrevVector = false;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000467 auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
468 auto i = Instructions.end() - 1;
469 for (auto n = Instructions.begin() - 1;; --i, ++Offset) {
470 if (i == n)
471 // Couldn't find producer
472 return MCDisassembler::Fail;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000473 bool CurrentVector = HexagonMCInstrInfo::isVector(*MCII, *i->getInst());
474 if (Vector && !CurrentVector)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000475 // Skip scalars when calculating distances for vectors
476 ++Lookback;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000477 if (HexagonMCInstrInfo::isImmext(*i->getInst()) && (Vector == PrevVector))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000478 ++Lookback;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000479 PrevVector = CurrentVector;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000480 if (Offset == Lookback)
481 break;
482 }
483 auto const &Inst = *i->getInst();
484 bool SubregBit = (Register & 0x1) != 0;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000485 if (HexagonMCInstrInfo::hasNewValue2(*MCII, Inst)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000486 // If subreg bit is set we're selecting the second produced newvalue
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000487 unsigned Producer = SubregBit ?
488 HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg() :
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000489 HexagonMCInstrInfo::getNewValueOperand2(*MCII, Inst).getReg();
490 assert(Producer != Hexagon::NoRegister);
491 MCO.setReg(Producer);
492 } else if (HexagonMCInstrInfo::hasNewValue(*MCII, Inst)) {
493 unsigned Producer =
494 HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg();
495 if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
496 Producer = ((Producer - Hexagon::W0) << 1) + SubregBit + Hexagon::V0;
497 else if (SubregBit)
Colin LeMahieu2d497a02016-03-01 22:05:03 +0000498 // Hexagon PRM 10.11 New-value operands
499 // Nt[0] is reserved and should always be encoded as zero.
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000500 return MCDisassembler::Fail;
501 assert(Producer != Hexagon::NoRegister);
502 MCO.setReg(Producer);
503 } else
504 return MCDisassembler::Fail;
505 }
506
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000507 if (CurrentExtender != nullptr) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000508 MCInst const &Inst = HexagonMCInstrInfo::isDuplex(*MCII, MI)
509 ? *MI.getOperand(1).getInst()
510 : MI;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000511 if (!HexagonMCInstrInfo::isExtendable(*MCII, Inst) &&
512 !HexagonMCInstrInfo::isExtended(*MCII, Inst))
513 return MCDisassembler::Fail;
514 }
Colin LeMahieu5d6f03b2014-12-04 03:41:21 +0000515 return Result;
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000516}
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000517
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000518static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,
Craig Toppere5e035a32015-12-05 07:13:35 +0000519 ArrayRef<MCPhysReg> Table) {
Craig Topper3da000c2015-12-01 06:13:04 +0000520 if (RegNo < Table.size()) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000521 Inst.addOperand(MCOperand::createReg(Table[RegNo]));
522 return MCDisassembler::Success;
Craig Topper3da000c2015-12-01 06:13:04 +0000523 }
524
525 return MCDisassembler::Fail;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000526}
527
Colin LeMahieu7c958712015-10-17 01:33:04 +0000528static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
529 uint64_t Address,
530 const void *Decoder) {
531 return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
532}
533
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000534static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
535 uint64_t Address,
536 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000537 static const MCPhysReg IntRegDecoderTable[] = {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000538 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
539 Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9,
540 Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
541 Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
542 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
543 Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
544 Hexagon::R30, Hexagon::R31};
545
Craig Toppere5e035a32015-12-05 07:13:35 +0000546 return DecodeRegisterClass(Inst, RegNo, IntRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000547}
Colin LeMahieu7c958712015-10-17 01:33:04 +0000548
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000549static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
550 unsigned RegNo,
551 uint64_t Address,
552 const void *Decoder) {
553 static const MCPhysReg GeneralSubRegDecoderTable[] = {
554 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3,
555 Hexagon::R4, Hexagon::R5, Hexagon::R6, Hexagon::R7,
556 Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
557 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23,
558 };
559
560 return DecodeRegisterClass(Inst, RegNo, GeneralSubRegDecoderTable);
561}
562
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000563static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,
564 uint64_t /*Address*/,
565 const void *Decoder) {
566 static const MCPhysReg HvxVRDecoderTable[] = {
Colin LeMahieu7c958712015-10-17 01:33:04 +0000567 Hexagon::V0, Hexagon::V1, Hexagon::V2, Hexagon::V3, Hexagon::V4,
568 Hexagon::V5, Hexagon::V6, Hexagon::V7, Hexagon::V8, Hexagon::V9,
569 Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
570 Hexagon::V15, Hexagon::V16, Hexagon::V17, Hexagon::V18, Hexagon::V19,
571 Hexagon::V20, Hexagon::V21, Hexagon::V22, Hexagon::V23, Hexagon::V24,
572 Hexagon::V25, Hexagon::V26, Hexagon::V27, Hexagon::V28, Hexagon::V29,
573 Hexagon::V30, Hexagon::V31};
574
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000575 return DecodeRegisterClass(Inst, RegNo, HvxVRDecoderTable);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000576}
577
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000578static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
579 uint64_t /*Address*/,
580 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000581 static const MCPhysReg DoubleRegDecoderTable[] = {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000582 Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,
583 Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7,
584 Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11,
585 Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15};
586
Craig Toppere5e035a32015-12-05 07:13:35 +0000587 return DecodeRegisterClass(Inst, RegNo >> 1, DoubleRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000588}
589
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000590static DecodeStatus DecodeGeneralDoubleLow8RegsRegisterClass(
591 MCInst &Inst, unsigned RegNo, uint64_t /*Address*/, const void *Decoder) {
592 static const MCPhysReg GeneralDoubleLow8RegDecoderTable[] = {
593 Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,
594 Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11};
595
596 return DecodeRegisterClass(Inst, RegNo, GeneralDoubleLow8RegDecoderTable);
597}
598
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000599static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,
600 uint64_t /*Address*/,
601 const void *Decoder) {
602 static const MCPhysReg HvxWRDecoderTable[] = {
Colin LeMahieu7c958712015-10-17 01:33:04 +0000603 Hexagon::W0, Hexagon::W1, Hexagon::W2, Hexagon::W3,
604 Hexagon::W4, Hexagon::W5, Hexagon::W6, Hexagon::W7,
605 Hexagon::W8, Hexagon::W9, Hexagon::W10, Hexagon::W11,
606 Hexagon::W12, Hexagon::W13, Hexagon::W14, Hexagon::W15};
607
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000608 return (DecodeRegisterClass(Inst, RegNo >> 1, HvxWRDecoderTable));
Colin LeMahieu7c958712015-10-17 01:33:04 +0000609}
610
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000611static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
612 uint64_t /*Address*/,
613 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000614 static const MCPhysReg PredRegDecoderTable[] = {Hexagon::P0, Hexagon::P1,
615 Hexagon::P2, Hexagon::P3};
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000616
Craig Toppere5e035a32015-12-05 07:13:35 +0000617 return DecodeRegisterClass(Inst, RegNo, PredRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000618}
619
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000620static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,
621 uint64_t /*Address*/,
622 const void *Decoder) {
623 static const MCPhysReg HvxQRDecoderTable[] = {Hexagon::Q0, Hexagon::Q1,
624 Hexagon::Q2, Hexagon::Q3};
Colin LeMahieu7c958712015-10-17 01:33:04 +0000625
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000626 return DecodeRegisterClass(Inst, RegNo, HvxQRDecoderTable);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000627}
Colin LeMahieube8c4532015-06-05 16:00:11 +0000628
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000629static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
630 uint64_t /*Address*/,
631 const void *Decoder) {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000632 using namespace Hexagon;
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000633
Craig Toppere5e035a32015-12-05 07:13:35 +0000634 static const MCPhysReg CtrlRegDecoderTable[] = {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000635 /* 0 */ SA0, LC0, SA1, LC1,
Krzysztof Parzyszeke2603322017-05-05 22:12:12 +0000636 /* 4 */ P3_0, C5, M0, M1,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000637 /* 8 */ USR, PC, UGP, GP,
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000638 /* 12 */ CS0, CS1, UPCYCLELO, UPCYCLEHI,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000639 /* 16 */ FRAMELIMIT, FRAMEKEY, PKTCOUNTLO, PKTCOUNTHI,
640 /* 20 */ 0, 0, 0, 0,
641 /* 24 */ 0, 0, 0, 0,
642 /* 28 */ 0, 0, UTIMERLO, UTIMERHI
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000643 };
644
Craig Topper6261e1b2015-12-01 06:13:06 +0000645 if (RegNo >= array_lengthof(CtrlRegDecoderTable))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000646 return MCDisassembler::Fail;
647
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000648 static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
649 if (CtrlRegDecoderTable[RegNo] == NoRegister)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000650 return MCDisassembler::Fail;
651
652 unsigned Register = CtrlRegDecoderTable[RegNo];
653 Inst.addOperand(MCOperand::createReg(Register));
654 return MCDisassembler::Success;
655}
656
657static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
658 uint64_t /*Address*/,
659 const void *Decoder) {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000660 using namespace Hexagon;
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000661
Craig Toppere5e035a32015-12-05 07:13:35 +0000662 static const MCPhysReg CtrlReg64DecoderTable[] = {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000663 /* 0 */ C1_0, 0, C3_2, 0,
664 /* 4 */ C5_4, 0, C7_6, 0,
665 /* 8 */ C9_8, 0, C11_10, 0,
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000666 /* 12 */ CS, 0, UPCYCLE, 0,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000667 /* 16 */ C17_16, 0, PKTCOUNT, 0,
668 /* 20 */ 0, 0, 0, 0,
669 /* 24 */ 0, 0, 0, 0,
670 /* 28 */ 0, 0, UTIMER, 0
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000671 };
672
Craig Topper6261e1b2015-12-01 06:13:06 +0000673 if (RegNo >= array_lengthof(CtrlReg64DecoderTable))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000674 return MCDisassembler::Fail;
675
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000676 static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
677 if (CtrlReg64DecoderTable[RegNo] == NoRegister)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000678 return MCDisassembler::Fail;
679
680 unsigned Register = CtrlReg64DecoderTable[RegNo];
681 Inst.addOperand(MCOperand::createReg(Register));
682 return MCDisassembler::Success;
683}
684
685static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
686 uint64_t /*Address*/,
687 const void *Decoder) {
688 unsigned Register = 0;
689 switch (RegNo) {
690 case 0:
691 Register = Hexagon::M0;
692 break;
693 case 1:
694 Register = Hexagon::M1;
695 break;
696 default:
697 return MCDisassembler::Fail;
698 }
699 Inst.addOperand(MCOperand::createReg(Register));
700 return MCDisassembler::Success;
701}
702
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000703static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
704 uint64_t /*Address*/,
705 const void *Decoder) {
706 HexagonDisassembler const &Disassembler = disassembler(Decoder);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000707 int64_t FullValue = fullValue(Disassembler, MI, tmp);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000708 assert(FullValue >= 0 && "Negative in unsigned decoder");
709 HexagonMCInstrInfo::addConstant(MI, FullValue, Disassembler.getContext());
710 return MCDisassembler::Success;
711}
712
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000713static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
714 uint64_t /*Address*/, const void *Decoder) {
715 HexagonDisassembler const &Disassembler = disassembler(Decoder);
716 unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
717 tmp = SignExtend64(tmp, Bits);
718 signedDecoder<32>(MI, tmp, Decoder);
719 return MCDisassembler::Success;
720}
721
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000722// custom decoder for various jump/call immediates
723static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
724 const void *Decoder) {
725 HexagonDisassembler const &Disassembler = disassembler(Decoder);
726 unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
727 // r13_2 is not extendable, so if there are no extent bits, it's r13_2
728 if (Bits == 0)
729 Bits = 15;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000730 uint64_t FullValue = fullValue(Disassembler, MI, SignExtend64(tmp, Bits));
731 uint32_t Extended = FullValue + Address;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000732 if (!Disassembler.tryAddingSymbolicOperand(MI, Extended, Address, true, 0, 4))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000733 HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
734 return MCDisassembler::Success;
735}
Krzysztof Parzyszek22a21d42018-03-01 17:03:26 +0000736
737static DecodeStatus DecodeGuestRegsRegisterClass(MCInst &Inst, unsigned RegNo,
738 uint64_t /*Address*/,
739 const void *Decoder) {
740 using namespace Hexagon;
741
742 static const MCPhysReg GuestRegDecoderTable[] = {
743 /* 0 */ GELR, GSR, GOSP, G3,
744 /* 4 */ G4, G5, G6, G7,
745 /* 8 */ G8, G9, G10, G11,
746 /* 12 */ G12, G13, G14, G15,
747 /* 16 */ GPMUCNT4, GPMUCNT5, GPMUCNT6, GPMUCNT7,
748 /* 20 */ G20, G21, G22, G23,
749 /* 24 */ GPCYCLELO, GPCYCLEHI, GPMUCNT0, GPMUCNT1,
750 /* 28 */ GPMUCNT2, GPMUCNT3, G30, G31
751 };
752
753 if (RegNo >= array_lengthof(GuestRegDecoderTable))
754 return MCDisassembler::Fail;
755 if (GuestRegDecoderTable[RegNo] == Hexagon::NoRegister)
756 return MCDisassembler::Fail;
757
758 unsigned Register = GuestRegDecoderTable[RegNo];
759 Inst.addOperand(MCOperand::createReg(Register));
760 return MCDisassembler::Success;
761}
762
763static DecodeStatus DecodeGuestRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
764 uint64_t /*Address*/,
765 const void *Decoder) {
766 using namespace Hexagon;
767
768 static const MCPhysReg GuestReg64DecoderTable[] = {
769 /* 0 */ G1_0, 0, G3_2, 0,
770 /* 4 */ G5_4, 0, G7_6, 0,
771 /* 8 */ G9_8, 0, G11_10, 0,
772 /* 12 */ G13_12, 0, G15_14, 0,
773 /* 16 */ G17_16, 0, G19_18, 0,
774 /* 20 */ G21_20, 0, G23_22, 0,
775 /* 24 */ G25_24, 0, G27_26, 0,
776 /* 28 */ G29_28, 0, G31_30, 0
777 };
778
779 if (RegNo >= array_lengthof(GuestReg64DecoderTable))
780 return MCDisassembler::Fail;
781 if (GuestReg64DecoderTable[RegNo] == Hexagon::NoRegister)
782 return MCDisassembler::Fail;
783
784 unsigned Register = GuestReg64DecoderTable[RegNo];
785 Inst.addOperand(MCOperand::createReg(Register));
786 return MCDisassembler::Success;
787}