blob: 574a8d44616c317798860b539dd9e46d94fa51e2 [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
Colin LeMahieu68d967d2015-05-29 14:44:13 +000012#include "Hexagon.h"
NAKAMURA Takumi729be142014-10-27 12:37:26 +000013#include "MCTargetDesc/HexagonBaseInfo.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000014#include "MCTargetDesc/HexagonMCChecker.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000015#include "MCTargetDesc/HexagonMCInstrInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "MCTargetDesc/HexagonMCTargetDesc.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000017#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/STLExtras.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000019#include "llvm/MC/MCContext.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000020#include "llvm/MC/MCDisassembler/MCDisassembler.h"
NAKAMURA Takumi729be142014-10-27 12:37:26 +000021#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCFixedLenDisassembler.h"
23#include "llvm/MC/MCInst.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000024#include "llvm/MC/MCInstrInfo.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000025#include "llvm/MC/MCRegisterInfo.h"
NAKAMURA Takumi729be142014-10-27 12:37:26 +000026#include "llvm/MC/MCSubtargetInfo.h"
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000027#include "llvm/Support/Endian.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000028#include "llvm/Support/MathExtras.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000029#include "llvm/Support/TargetRegistry.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000030#include "llvm/Support/raw_ostream.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000031#include <cassert>
32#include <cstddef>
33#include <cstdint>
34#include <memory>
NAKAMURA Takumi729be142014-10-27 12:37:26 +000035
36using namespace llvm;
Colin LeMahieu68d967d2015-05-29 14:44:13 +000037using namespace Hexagon;
NAKAMURA Takumi729be142014-10-27 12:37:26 +000038
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000039using DecodeStatus = MCDisassembler::DecodeStatus;
NAKAMURA Takumi729be142014-10-27 12:37:26 +000040
41namespace {
Eugene Zelenko82085922016-12-13 22:13:50 +000042
NAKAMURA Takumi729be142014-10-27 12:37:26 +000043/// \brief Hexagon disassembler for all Hexagon platforms.
44class HexagonDisassembler : public MCDisassembler {
45public:
Colin LeMahieu7cd08922015-11-09 04:07:48 +000046 std::unique_ptr<MCInstrInfo const> const MCII;
Colin LeMahieu68d967d2015-05-29 14:44:13 +000047 std::unique_ptr<MCInst *> CurrentBundle;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000048 mutable MCInst const *CurrentExtender;
Eugene Zelenko82085922016-12-13 22:13:50 +000049
Colin LeMahieu7cd08922015-11-09 04:07:48 +000050 HexagonDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
51 MCInstrInfo const *MCII)
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000052 : MCDisassembler(STI, Ctx), MCII(MCII), CurrentBundle(new MCInst *),
53 CurrentExtender(nullptr) {}
NAKAMURA Takumi729be142014-10-27 12:37:26 +000054
Colin LeMahieu68d967d2015-05-29 14:44:13 +000055 DecodeStatus getSingleInstruction(MCInst &Instr, MCInst &MCB,
56 ArrayRef<uint8_t> Bytes, uint64_t Address,
57 raw_ostream &VStream, raw_ostream &CStream,
58 bool &Complete) const;
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000059 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +000060 ArrayRef<uint8_t> Bytes, uint64_t Address,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000061 raw_ostream &VStream,
62 raw_ostream &CStream) const override;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000063 void remapInstruction(MCInst &Instr) const;
NAKAMURA Takumi729be142014-10-27 12:37:26 +000064};
Eugene Zelenko82085922016-12-13 22:13:50 +000065
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000066static uint64_t fullValue(HexagonDisassembler const &Disassembler, MCInst &MI,
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000067 int64_t Value) {
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000068 MCInstrInfo MCII = *Disassembler.MCII;
69 if (!Disassembler.CurrentExtender ||
70 MI.size() != HexagonMCInstrInfo::getExtendableOp(MCII, MI))
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000071 return Value;
72 unsigned Alignment = HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
73 uint32_t Lower6 = static_cast<uint32_t>(Value >> Alignment) & 0x3f;
74 int64_t Bits;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000075 bool Success =
76 Disassembler.CurrentExtender->getOperand(0).getExpr()->evaluateAsAbsolute(
77 Bits);
78 assert(Success);
79 (void)Success;
80 uint64_t Upper26 = static_cast<uint64_t>(Bits);
81 uint64_t Operand = Upper26 | Lower6;
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000082 return Operand;
83}
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000084static HexagonDisassembler const &disassembler(void const *Decoder) {
85 return *static_cast<HexagonDisassembler const *>(Decoder);
86}
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000087template <size_t T>
88static void signedDecoder(MCInst &MI, unsigned tmp, const void *Decoder) {
89 HexagonDisassembler const &Disassembler = disassembler(Decoder);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000090 int64_t FullValue = fullValue(Disassembler, MI, SignExtend64<T>(tmp));
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000091 int64_t Extended = SignExtend64<32>(FullValue);
92 HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
93}
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000094}
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000095
Colin LeMahieu7cd08922015-11-09 04:07:48 +000096// Forward declare these because the auto-generated code will reference them.
97// Definitions are further down.
98
99static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
Colin LeMahieube8c4532015-06-05 16:00:11 +0000100 uint64_t Address,
101 const void *Decoder);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000102static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
103 unsigned RegNo,
104 uint64_t Address,
105 const void *Decoder);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000106static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
107 uint64_t Address,
108 const void *Decoder);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000109static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000110 uint64_t Address,
111 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000112static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
113 uint64_t Address,
114 const void *Decoder);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000115static DecodeStatus
116DecodeGeneralDoubleLow8RegsRegisterClass(MCInst &Inst, unsigned RegNo,
117 uint64_t Address, const void *Decoder);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000118static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000119 uint64_t Address,
120 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000121static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
122 uint64_t Address,
123 const void *Decoder);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000124static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000125 uint64_t Address,
126 const void *Decoder);
Colin LeMahieuf3db8842014-12-19 19:06:32 +0000127static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
Colin LeMahieube8c4532015-06-05 16:00:11 +0000128 uint64_t Address,
129 const void *Decoder);
Krzysztof Parzyszek22a21d42018-03-01 17:03:26 +0000130static DecodeStatus DecodeGuestRegsRegisterClass(MCInst &Inst, unsigned RegNo,
131 uint64_t Address,
132 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000133static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
134 uint64_t Address,
135 const void *Decoder);
Colin LeMahieu404d5b22015-02-10 16:59:36 +0000136static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
Colin LeMahieube8c4532015-06-05 16:00:11 +0000137 uint64_t Address,
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000138 const void *Decoder);
Krzysztof Parzyszek22a21d42018-03-01 17:03:26 +0000139static DecodeStatus DecodeGuestRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
140 uint64_t Address,
141 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000142
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000143static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
144 uint64_t Address, const void *Decoder);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000145static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
146 uint64_t /*Address*/, const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000147static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
148 const void *Decoder);
Colin LeMahieuefa74e02014-11-18 20:28:11 +0000149
David Blaikief157ad42017-12-14 23:45:58 +0000150static DecodeStatus s4_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
151 const void *Decoder) {
152 signedDecoder<4>(MI, tmp, Decoder);
153 return MCDisassembler::Success;
154}
155static DecodeStatus s29_3ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
156 const void *Decoder) {
157 signedDecoder<14>(MI, tmp, Decoder);
158 return MCDisassembler::Success;
159}
160static DecodeStatus s8_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
161 const void *Decoder) {
162 signedDecoder<8>(MI, tmp, Decoder);
163 return MCDisassembler::Success;
164}
165static DecodeStatus s4_3ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
166 const void *Decoder) {
167 signedDecoder<7>(MI, tmp, Decoder);
168 return MCDisassembler::Success;
169}
170static DecodeStatus s31_1ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
171 const void *Decoder) {
172 signedDecoder<12>(MI, tmp, Decoder);
173 return MCDisassembler::Success;
174}
175static DecodeStatus s3_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
176 const void *Decoder) {
177 signedDecoder<3>(MI, tmp, Decoder);
178 return MCDisassembler::Success;
179}
180static DecodeStatus s30_2ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
181 const void *Decoder) {
182 signedDecoder<13>(MI, tmp, Decoder);
183 return MCDisassembler::Success;
184}
185static DecodeStatus s6_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
186 const void *Decoder) {
187 signedDecoder<6>(MI, tmp, Decoder);
188 return MCDisassembler::Success;
189}
190static DecodeStatus s6_3ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
191 const void *Decoder) {
192 signedDecoder<9>(MI, tmp, Decoder);
193 return MCDisassembler::Success;
194}
195static DecodeStatus s4_1ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
196 const void *Decoder) {
197 signedDecoder<5>(MI, tmp, Decoder);
198 return MCDisassembler::Success;
199}
200static DecodeStatus s4_2ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
201 const void *Decoder) {
202 signedDecoder<6>(MI, tmp, Decoder);
203 return MCDisassembler::Success;
204}
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000205#include "HexagonGenDisassemblerTables.inc"
206
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000207static MCDisassembler *createHexagonDisassembler(const Target &T,
208 const MCSubtargetInfo &STI,
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000209 MCContext &Ctx) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000210 return new HexagonDisassembler(STI, Ctx, T.createMCInstrInfo());
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000211}
212
213extern "C" void LLVMInitializeHexagonDisassembler() {
Mehdi Aminif42454b2016-10-09 23:00:34 +0000214 TargetRegistry::RegisterMCDisassembler(getTheHexagonTarget(),
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000215 createHexagonDisassembler);
216}
217
218DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000219 ArrayRef<uint8_t> Bytes,
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000220 uint64_t Address,
221 raw_ostream &os,
222 raw_ostream &cs) const {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000223 DecodeStatus Result = DecodeStatus::Success;
224 bool Complete = false;
225 Size = 0;
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000226
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000227 *CurrentBundle = &MI;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000228 MI.setOpcode(Hexagon::BUNDLE);
229 MI.addOperand(MCOperand::createImm(0));
Eugene Zelenko82085922016-12-13 22:13:50 +0000230 while (Result == Success && !Complete) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000231 if (Bytes.size() < HEXAGON_INSTR_SIZE)
232 return MCDisassembler::Fail;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000233 MCInst *Inst = new (getContext()) MCInst;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000234 Result = getSingleInstruction(*Inst, MI, Bytes, Address, os, cs, Complete);
235 MI.addOperand(MCOperand::createInst(Inst));
236 Size += HEXAGON_INSTR_SIZE;
237 Bytes = Bytes.slice(HEXAGON_INSTR_SIZE);
238 }
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000239 if (Result == MCDisassembler::Fail)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000240 return Result;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000241 if (Size > HEXAGON_MAX_PACKET_SIZE)
242 return MCDisassembler::Fail;
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000243 HexagonMCChecker Checker(getContext(), *MCII, STI, MI,
244 *getContext().getRegisterInfo(), false);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000245 if (!Checker.check())
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000246 return MCDisassembler::Fail;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000247 remapInstruction(MI);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000248 return MCDisassembler::Success;
249}
250
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000251void HexagonDisassembler::remapInstruction(MCInst &Instr) const {
252 for (auto I: HexagonMCInstrInfo::bundleInstructions(Instr)) {
253 auto &MI = const_cast<MCInst &>(*I.getInst());
254 switch (MI.getOpcode()) {
255 case Hexagon::S2_allocframe:
256 if (MI.getOperand(0).getReg() == Hexagon::R29) {
257 MI.setOpcode(Hexagon::S6_allocframe_to_raw);
258 MI.erase(MI.begin () + 1);
259 MI.erase(MI.begin ());
260 }
261 break;
262 case Hexagon::L2_deallocframe:
263 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
264 MI.getOperand(1).getReg() == Hexagon::R30) {
265 MI.setOpcode(L6_deallocframe_map_to_raw);
266 MI.erase(MI.begin () + 1);
267 MI.erase(MI.begin ());
268 }
269 break;
270 case Hexagon::L4_return:
271 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
272 MI.getOperand(1).getReg() == Hexagon::R30) {
273 MI.setOpcode(L6_return_map_to_raw);
274 MI.erase(MI.begin () + 1);
275 MI.erase(MI.begin ());
276 }
277 break;
278 case Hexagon::L4_return_t:
279 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
280 MI.getOperand(2).getReg() == Hexagon::R30) {
281 MI.setOpcode(L4_return_map_to_raw_t);
282 MI.erase(MI.begin () + 2);
283 MI.erase(MI.begin ());
284 }
285 break;
286 case Hexagon::L4_return_f:
287 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
288 MI.getOperand(2).getReg() == Hexagon::R30) {
289 MI.setOpcode(L4_return_map_to_raw_f);
290 MI.erase(MI.begin () + 2);
291 MI.erase(MI.begin ());
292 }
293 break;
294 case Hexagon::L4_return_tnew_pt:
295 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
296 MI.getOperand(2).getReg() == Hexagon::R30) {
297 MI.setOpcode(L4_return_map_to_raw_tnew_pt);
298 MI.erase(MI.begin () + 2);
299 MI.erase(MI.begin ());
300 }
301 break;
302 case Hexagon::L4_return_fnew_pt:
303 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
304 MI.getOperand(2).getReg() == Hexagon::R30) {
305 MI.setOpcode(L4_return_map_to_raw_fnew_pt);
306 MI.erase(MI.begin () + 2);
307 MI.erase(MI.begin ());
308 }
309 break;
310 case Hexagon::L4_return_tnew_pnt:
311 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
312 MI.getOperand(2).getReg() == Hexagon::R30) {
313 MI.setOpcode(L4_return_map_to_raw_tnew_pnt);
314 MI.erase(MI.begin () + 2);
315 MI.erase(MI.begin ());
316 }
317 break;
318 case Hexagon::L4_return_fnew_pnt:
319 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
320 MI.getOperand(2).getReg() == Hexagon::R30) {
321 MI.setOpcode(L4_return_map_to_raw_fnew_pnt);
322 MI.erase(MI.begin () + 2);
323 MI.erase(MI.begin ());
324 }
325 break;
326 }
327 }
328}
329
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000330static void adjustDuplex(MCInst &MI, MCContext &Context) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000331 switch (MI.getOpcode()) {
332 case Hexagon::SA1_setin1:
333 MI.insert(MI.begin() + 1,
334 MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
335 break;
336 case Hexagon::SA1_dec:
337 MI.insert(MI.begin() + 2,
338 MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
339 break;
340 default:
341 break;
342 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000343}
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000344
345DecodeStatus HexagonDisassembler::getSingleInstruction(
346 MCInst &MI, MCInst &MCB, ArrayRef<uint8_t> Bytes, uint64_t Address,
347 raw_ostream &os, raw_ostream &cs, bool &Complete) const {
348 assert(Bytes.size() >= HEXAGON_INSTR_SIZE);
349
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000350 uint32_t Instruction = support::endian::read32le(Bytes.data());
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000351
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000352 auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB);
353 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
354 HexagonII::INST_PARSE_LOOP_END) {
355 if (BundleSize == 0)
356 HexagonMCInstrInfo::setInnerLoop(MCB);
357 else if (BundleSize == 1)
358 HexagonMCInstrInfo::setOuterLoop(MCB);
359 else
360 return DecodeStatus::Fail;
361 }
362
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000363 CurrentExtender = HexagonMCInstrInfo::extenderForIndex(
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000364 MCB, HexagonMCInstrInfo::bundleSize(MCB));
365
366 DecodeStatus Result = DecodeStatus::Fail;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000367 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
Colin LeMahieube8c4532015-06-05 16:00:11 +0000368 HexagonII::INST_PARSE_DUPLEX) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000369 unsigned duplexIClass;
370 uint8_t const *DecodeLow, *DecodeHigh;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000371 duplexIClass = ((Instruction >> 28) & 0xe) | ((Instruction >> 13) & 0x1);
372 switch (duplexIClass) {
373 default:
374 return MCDisassembler::Fail;
375 case 0:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000376 DecodeLow = DecoderTableSUBINSN_L132;
377 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000378 break;
379 case 1:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000380 DecodeLow = DecoderTableSUBINSN_L232;
381 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000382 break;
383 case 2:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000384 DecodeLow = DecoderTableSUBINSN_L232;
385 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000386 break;
387 case 3:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000388 DecodeLow = DecoderTableSUBINSN_A32;
389 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000390 break;
391 case 4:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000392 DecodeLow = DecoderTableSUBINSN_L132;
393 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000394 break;
395 case 5:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000396 DecodeLow = DecoderTableSUBINSN_L232;
397 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000398 break;
399 case 6:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000400 DecodeLow = DecoderTableSUBINSN_S132;
401 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000402 break;
403 case 7:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000404 DecodeLow = DecoderTableSUBINSN_S232;
405 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000406 break;
407 case 8:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000408 DecodeLow = DecoderTableSUBINSN_S132;
409 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000410 break;
411 case 9:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000412 DecodeLow = DecoderTableSUBINSN_S132;
413 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000414 break;
415 case 10:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000416 DecodeLow = DecoderTableSUBINSN_S132;
417 DecodeHigh = DecoderTableSUBINSN_S132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000418 break;
419 case 11:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000420 DecodeLow = DecoderTableSUBINSN_S232;
421 DecodeHigh = DecoderTableSUBINSN_S132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000422 break;
423 case 12:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000424 DecodeLow = DecoderTableSUBINSN_S232;
425 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000426 break;
427 case 13:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000428 DecodeLow = DecoderTableSUBINSN_S232;
429 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000430 break;
431 case 14:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000432 DecodeLow = DecoderTableSUBINSN_S232;
433 DecodeHigh = DecoderTableSUBINSN_S232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000434 break;
435 }
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000436 MI.setOpcode(Hexagon::DuplexIClass0 + duplexIClass);
Colin LeMahieube8c4532015-06-05 16:00:11 +0000437 MCInst *MILow = new (getContext()) MCInst;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000438 MCInst *MIHigh = new (getContext()) MCInst;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000439 auto TmpExtender = CurrentExtender;
440 CurrentExtender =
441 nullptr; // constant extenders in duplex must always be in slot 1
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000442 Result = decodeInstruction(DecodeLow, *MILow, Instruction & 0x1fff, Address,
443 this, STI);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000444 CurrentExtender = TmpExtender;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000445 if (Result != DecodeStatus::Success)
446 return DecodeStatus::Fail;
447 adjustDuplex(*MILow, getContext());
448 Result = decodeInstruction(
449 DecodeHigh, *MIHigh, (Instruction >> 16) & 0x1fff, Address, this, STI);
450 if (Result != DecodeStatus::Success)
451 return DecodeStatus::Fail;
452 adjustDuplex(*MIHigh, getContext());
Colin LeMahieube8c4532015-06-05 16:00:11 +0000453 MCOperand OPLow = MCOperand::createInst(MILow);
454 MCOperand OPHigh = MCOperand::createInst(MIHigh);
455 MI.addOperand(OPLow);
456 MI.addOperand(OPHigh);
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000457 Complete = true;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000458 } else {
459 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
460 HexagonII::INST_PARSE_PACKET_END)
461 Complete = true;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000462
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000463 if (CurrentExtender != nullptr)
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000464 Result = decodeInstruction(DecoderTableMustExtend32, MI, Instruction,
465 Address, this, STI);
466
467 if (Result != MCDisassembler::Success)
468 Result = decodeInstruction(DecoderTable32, MI, Instruction, Address, this,
469 STI);
470
471 if (Result != MCDisassembler::Success &&
472 STI.getFeatureBits()[Hexagon::ExtensionHVX])
473 Result = decodeInstruction(DecoderTableEXT_mmvec32, MI, Instruction,
474 Address, this, STI);
475
Colin LeMahieube8c4532015-06-05 16:00:11 +0000476 }
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000477
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000478 switch (MI.getOpcode()) {
Colin LeMahieu81707542016-12-05 04:29:00 +0000479 case Hexagon::J4_cmpeqn1_f_jumpnv_nt:
480 case Hexagon::J4_cmpeqn1_f_jumpnv_t:
481 case Hexagon::J4_cmpeqn1_fp0_jump_nt:
482 case Hexagon::J4_cmpeqn1_fp0_jump_t:
483 case Hexagon::J4_cmpeqn1_fp1_jump_nt:
484 case Hexagon::J4_cmpeqn1_fp1_jump_t:
485 case Hexagon::J4_cmpeqn1_t_jumpnv_nt:
486 case Hexagon::J4_cmpeqn1_t_jumpnv_t:
487 case Hexagon::J4_cmpeqn1_tp0_jump_nt:
488 case Hexagon::J4_cmpeqn1_tp0_jump_t:
489 case Hexagon::J4_cmpeqn1_tp1_jump_nt:
490 case Hexagon::J4_cmpeqn1_tp1_jump_t:
491 case Hexagon::J4_cmpgtn1_f_jumpnv_nt:
492 case Hexagon::J4_cmpgtn1_f_jumpnv_t:
493 case Hexagon::J4_cmpgtn1_fp0_jump_nt:
494 case Hexagon::J4_cmpgtn1_fp0_jump_t:
495 case Hexagon::J4_cmpgtn1_fp1_jump_nt:
496 case Hexagon::J4_cmpgtn1_fp1_jump_t:
497 case Hexagon::J4_cmpgtn1_t_jumpnv_nt:
498 case Hexagon::J4_cmpgtn1_t_jumpnv_t:
499 case Hexagon::J4_cmpgtn1_tp0_jump_nt:
500 case Hexagon::J4_cmpgtn1_tp0_jump_t:
501 case Hexagon::J4_cmpgtn1_tp1_jump_nt:
502 case Hexagon::J4_cmpgtn1_tp1_jump_t:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000503 MI.insert(MI.begin() + 1,
504 MCOperand::createExpr(MCConstantExpr::create(-1, getContext())));
Colin LeMahieu81707542016-12-05 04:29:00 +0000505 break;
506 default:
507 break;
508 }
509
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000510 if (HexagonMCInstrInfo::isNewValue(*MCII, MI)) {
511 unsigned OpIndex = HexagonMCInstrInfo::getNewValueOp(*MCII, MI);
512 MCOperand &MCO = MI.getOperand(OpIndex);
513 assert(MCO.isReg() && "New value consumers must be registers");
514 unsigned Register =
515 getContext().getRegisterInfo()->getEncodingValue(MCO.getReg());
516 if ((Register & 0x6) == 0)
517 // HexagonPRM 10.11 Bit 1-2 == 0 is reserved
518 return MCDisassembler::Fail;
519 unsigned Lookback = (Register & 0x6) >> 1;
520 unsigned Offset = 1;
521 bool Vector = HexagonMCInstrInfo::isVector(*MCII, MI);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000522 bool PrevVector = false;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000523 auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
524 auto i = Instructions.end() - 1;
525 for (auto n = Instructions.begin() - 1;; --i, ++Offset) {
526 if (i == n)
527 // Couldn't find producer
528 return MCDisassembler::Fail;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000529 bool CurrentVector = HexagonMCInstrInfo::isVector(*MCII, *i->getInst());
530 if (Vector && !CurrentVector)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000531 // Skip scalars when calculating distances for vectors
532 ++Lookback;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000533 if (HexagonMCInstrInfo::isImmext(*i->getInst()) && (Vector == PrevVector))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000534 ++Lookback;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000535 PrevVector = CurrentVector;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000536 if (Offset == Lookback)
537 break;
538 }
539 auto const &Inst = *i->getInst();
540 bool SubregBit = (Register & 0x1) != 0;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000541 if (HexagonMCInstrInfo::hasNewValue2(*MCII, Inst)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000542 // If subreg bit is set we're selecting the second produced newvalue
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000543 unsigned Producer = SubregBit ?
544 HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg() :
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000545 HexagonMCInstrInfo::getNewValueOperand2(*MCII, Inst).getReg();
546 assert(Producer != Hexagon::NoRegister);
547 MCO.setReg(Producer);
548 } else if (HexagonMCInstrInfo::hasNewValue(*MCII, Inst)) {
549 unsigned Producer =
550 HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg();
551 if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
552 Producer = ((Producer - Hexagon::W0) << 1) + SubregBit + Hexagon::V0;
553 else if (SubregBit)
Colin LeMahieu2d497a02016-03-01 22:05:03 +0000554 // Hexagon PRM 10.11 New-value operands
555 // Nt[0] is reserved and should always be encoded as zero.
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000556 return MCDisassembler::Fail;
557 assert(Producer != Hexagon::NoRegister);
558 MCO.setReg(Producer);
559 } else
560 return MCDisassembler::Fail;
561 }
562
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000563 if (CurrentExtender != nullptr) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000564 MCInst const &Inst = HexagonMCInstrInfo::isDuplex(*MCII, MI)
565 ? *MI.getOperand(1).getInst()
566 : MI;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000567 if (!HexagonMCInstrInfo::isExtendable(*MCII, Inst) &&
568 !HexagonMCInstrInfo::isExtended(*MCII, Inst))
569 return MCDisassembler::Fail;
570 }
Colin LeMahieu5d6f03b2014-12-04 03:41:21 +0000571 return Result;
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000572}
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000573
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000574static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,
Craig Toppere5e035a32015-12-05 07:13:35 +0000575 ArrayRef<MCPhysReg> Table) {
Craig Topper3da000c2015-12-01 06:13:04 +0000576 if (RegNo < Table.size()) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000577 Inst.addOperand(MCOperand::createReg(Table[RegNo]));
578 return MCDisassembler::Success;
Craig Topper3da000c2015-12-01 06:13:04 +0000579 }
580
581 return MCDisassembler::Fail;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000582}
583
Colin LeMahieu7c958712015-10-17 01:33:04 +0000584static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
585 uint64_t Address,
586 const void *Decoder) {
587 return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
588}
589
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000590static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
591 uint64_t Address,
592 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000593 static const MCPhysReg IntRegDecoderTable[] = {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000594 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
595 Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9,
596 Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
597 Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
598 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
599 Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
600 Hexagon::R30, Hexagon::R31};
601
Craig Toppere5e035a32015-12-05 07:13:35 +0000602 return DecodeRegisterClass(Inst, RegNo, IntRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000603}
Colin LeMahieu7c958712015-10-17 01:33:04 +0000604
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000605static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
606 unsigned RegNo,
607 uint64_t Address,
608 const void *Decoder) {
609 static const MCPhysReg GeneralSubRegDecoderTable[] = {
610 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3,
611 Hexagon::R4, Hexagon::R5, Hexagon::R6, Hexagon::R7,
612 Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
613 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23,
614 };
615
616 return DecodeRegisterClass(Inst, RegNo, GeneralSubRegDecoderTable);
617}
618
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000619static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,
620 uint64_t /*Address*/,
621 const void *Decoder) {
622 static const MCPhysReg HvxVRDecoderTable[] = {
Colin LeMahieu7c958712015-10-17 01:33:04 +0000623 Hexagon::V0, Hexagon::V1, Hexagon::V2, Hexagon::V3, Hexagon::V4,
624 Hexagon::V5, Hexagon::V6, Hexagon::V7, Hexagon::V8, Hexagon::V9,
625 Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
626 Hexagon::V15, Hexagon::V16, Hexagon::V17, Hexagon::V18, Hexagon::V19,
627 Hexagon::V20, Hexagon::V21, Hexagon::V22, Hexagon::V23, Hexagon::V24,
628 Hexagon::V25, Hexagon::V26, Hexagon::V27, Hexagon::V28, Hexagon::V29,
629 Hexagon::V30, Hexagon::V31};
630
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000631 return DecodeRegisterClass(Inst, RegNo, HvxVRDecoderTable);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000632}
633
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000634static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
635 uint64_t /*Address*/,
636 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000637 static const MCPhysReg DoubleRegDecoderTable[] = {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000638 Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,
639 Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7,
640 Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11,
641 Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15};
642
Craig Toppere5e035a32015-12-05 07:13:35 +0000643 return DecodeRegisterClass(Inst, RegNo >> 1, DoubleRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000644}
645
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000646static DecodeStatus DecodeGeneralDoubleLow8RegsRegisterClass(
647 MCInst &Inst, unsigned RegNo, uint64_t /*Address*/, const void *Decoder) {
648 static const MCPhysReg GeneralDoubleLow8RegDecoderTable[] = {
649 Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,
650 Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11};
651
652 return DecodeRegisterClass(Inst, RegNo, GeneralDoubleLow8RegDecoderTable);
653}
654
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000655static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,
656 uint64_t /*Address*/,
657 const void *Decoder) {
658 static const MCPhysReg HvxWRDecoderTable[] = {
Colin LeMahieu7c958712015-10-17 01:33:04 +0000659 Hexagon::W0, Hexagon::W1, Hexagon::W2, Hexagon::W3,
660 Hexagon::W4, Hexagon::W5, Hexagon::W6, Hexagon::W7,
661 Hexagon::W8, Hexagon::W9, Hexagon::W10, Hexagon::W11,
662 Hexagon::W12, Hexagon::W13, Hexagon::W14, Hexagon::W15};
663
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000664 return (DecodeRegisterClass(Inst, RegNo >> 1, HvxWRDecoderTable));
Colin LeMahieu7c958712015-10-17 01:33:04 +0000665}
666
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000667static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
668 uint64_t /*Address*/,
669 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000670 static const MCPhysReg PredRegDecoderTable[] = {Hexagon::P0, Hexagon::P1,
671 Hexagon::P2, Hexagon::P3};
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000672
Craig Toppere5e035a32015-12-05 07:13:35 +0000673 return DecodeRegisterClass(Inst, RegNo, PredRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000674}
675
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000676static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,
677 uint64_t /*Address*/,
678 const void *Decoder) {
679 static const MCPhysReg HvxQRDecoderTable[] = {Hexagon::Q0, Hexagon::Q1,
680 Hexagon::Q2, Hexagon::Q3};
Colin LeMahieu7c958712015-10-17 01:33:04 +0000681
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000682 return DecodeRegisterClass(Inst, RegNo, HvxQRDecoderTable);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000683}
Colin LeMahieube8c4532015-06-05 16:00:11 +0000684
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000685static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
686 uint64_t /*Address*/,
687 const void *Decoder) {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000688 using namespace Hexagon;
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000689
Craig Toppere5e035a32015-12-05 07:13:35 +0000690 static const MCPhysReg CtrlRegDecoderTable[] = {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000691 /* 0 */ SA0, LC0, SA1, LC1,
Krzysztof Parzyszeke2603322017-05-05 22:12:12 +0000692 /* 4 */ P3_0, C5, M0, M1,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000693 /* 8 */ USR, PC, UGP, GP,
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000694 /* 12 */ CS0, CS1, UPCYCLELO, UPCYCLEHI,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000695 /* 16 */ FRAMELIMIT, FRAMEKEY, PKTCOUNTLO, PKTCOUNTHI,
696 /* 20 */ 0, 0, 0, 0,
697 /* 24 */ 0, 0, 0, 0,
698 /* 28 */ 0, 0, UTIMERLO, UTIMERHI
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000699 };
700
Craig Topper6261e1b2015-12-01 06:13:06 +0000701 if (RegNo >= array_lengthof(CtrlRegDecoderTable))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000702 return MCDisassembler::Fail;
703
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000704 static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
705 if (CtrlRegDecoderTable[RegNo] == NoRegister)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000706 return MCDisassembler::Fail;
707
708 unsigned Register = CtrlRegDecoderTable[RegNo];
709 Inst.addOperand(MCOperand::createReg(Register));
710 return MCDisassembler::Success;
711}
712
713static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
714 uint64_t /*Address*/,
715 const void *Decoder) {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000716 using namespace Hexagon;
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000717
Craig Toppere5e035a32015-12-05 07:13:35 +0000718 static const MCPhysReg CtrlReg64DecoderTable[] = {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000719 /* 0 */ C1_0, 0, C3_2, 0,
720 /* 4 */ C5_4, 0, C7_6, 0,
721 /* 8 */ C9_8, 0, C11_10, 0,
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000722 /* 12 */ CS, 0, UPCYCLE, 0,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000723 /* 16 */ C17_16, 0, PKTCOUNT, 0,
724 /* 20 */ 0, 0, 0, 0,
725 /* 24 */ 0, 0, 0, 0,
726 /* 28 */ 0, 0, UTIMER, 0
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000727 };
728
Craig Topper6261e1b2015-12-01 06:13:06 +0000729 if (RegNo >= array_lengthof(CtrlReg64DecoderTable))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000730 return MCDisassembler::Fail;
731
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000732 static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
733 if (CtrlReg64DecoderTable[RegNo] == NoRegister)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000734 return MCDisassembler::Fail;
735
736 unsigned Register = CtrlReg64DecoderTable[RegNo];
737 Inst.addOperand(MCOperand::createReg(Register));
738 return MCDisassembler::Success;
739}
740
741static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
742 uint64_t /*Address*/,
743 const void *Decoder) {
744 unsigned Register = 0;
745 switch (RegNo) {
746 case 0:
747 Register = Hexagon::M0;
748 break;
749 case 1:
750 Register = Hexagon::M1;
751 break;
752 default:
753 return MCDisassembler::Fail;
754 }
755 Inst.addOperand(MCOperand::createReg(Register));
756 return MCDisassembler::Success;
757}
758
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000759static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
760 uint64_t /*Address*/,
761 const void *Decoder) {
762 HexagonDisassembler const &Disassembler = disassembler(Decoder);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000763 int64_t FullValue = fullValue(Disassembler, MI, tmp);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000764 assert(FullValue >= 0 && "Negative in unsigned decoder");
765 HexagonMCInstrInfo::addConstant(MI, FullValue, Disassembler.getContext());
766 return MCDisassembler::Success;
767}
768
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000769static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
770 uint64_t /*Address*/, const void *Decoder) {
771 HexagonDisassembler const &Disassembler = disassembler(Decoder);
772 unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
773 tmp = SignExtend64(tmp, Bits);
774 signedDecoder<32>(MI, tmp, Decoder);
775 return MCDisassembler::Success;
776}
777
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000778// custom decoder for various jump/call immediates
779static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
780 const void *Decoder) {
781 HexagonDisassembler const &Disassembler = disassembler(Decoder);
782 unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
783 // r13_2 is not extendable, so if there are no extent bits, it's r13_2
784 if (Bits == 0)
785 Bits = 15;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000786 uint64_t FullValue = fullValue(Disassembler, MI, SignExtend64(tmp, Bits));
787 uint32_t Extended = FullValue + Address;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000788 if (!Disassembler.tryAddingSymbolicOperand(MI, Extended, Address, true, 0, 4))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000789 HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
790 return MCDisassembler::Success;
791}
Krzysztof Parzyszek22a21d42018-03-01 17:03:26 +0000792
793static DecodeStatus DecodeGuestRegsRegisterClass(MCInst &Inst, unsigned RegNo,
794 uint64_t /*Address*/,
795 const void *Decoder) {
796 using namespace Hexagon;
797
798 static const MCPhysReg GuestRegDecoderTable[] = {
799 /* 0 */ GELR, GSR, GOSP, G3,
800 /* 4 */ G4, G5, G6, G7,
801 /* 8 */ G8, G9, G10, G11,
802 /* 12 */ G12, G13, G14, G15,
803 /* 16 */ GPMUCNT4, GPMUCNT5, GPMUCNT6, GPMUCNT7,
804 /* 20 */ G20, G21, G22, G23,
805 /* 24 */ GPCYCLELO, GPCYCLEHI, GPMUCNT0, GPMUCNT1,
806 /* 28 */ GPMUCNT2, GPMUCNT3, G30, G31
807 };
808
809 if (RegNo >= array_lengthof(GuestRegDecoderTable))
810 return MCDisassembler::Fail;
811 if (GuestRegDecoderTable[RegNo] == Hexagon::NoRegister)
812 return MCDisassembler::Fail;
813
814 unsigned Register = GuestRegDecoderTable[RegNo];
815 Inst.addOperand(MCOperand::createReg(Register));
816 return MCDisassembler::Success;
817}
818
819static DecodeStatus DecodeGuestRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
820 uint64_t /*Address*/,
821 const void *Decoder) {
822 using namespace Hexagon;
823
824 static const MCPhysReg GuestReg64DecoderTable[] = {
825 /* 0 */ G1_0, 0, G3_2, 0,
826 /* 4 */ G5_4, 0, G7_6, 0,
827 /* 8 */ G9_8, 0, G11_10, 0,
828 /* 12 */ G13_12, 0, G15_14, 0,
829 /* 16 */ G17_16, 0, G19_18, 0,
830 /* 20 */ G21_20, 0, G23_22, 0,
831 /* 24 */ G25_24, 0, G27_26, 0,
832 /* 28 */ G29_28, 0, G31_30, 0
833 };
834
835 if (RegNo >= array_lengthof(GuestReg64DecoderTable))
836 return MCDisassembler::Fail;
837 if (GuestReg64DecoderTable[RegNo] == Hexagon::NoRegister)
838 return MCDisassembler::Fail;
839
840 unsigned Register = GuestReg64DecoderTable[RegNo];
841 Inst.addOperand(MCOperand::createReg(Register));
842 return MCDisassembler::Success;
843}