blob: 481b692ae8bf359c4cd88e9c1871b36f1a3ecc70 [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);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000130static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
131 uint64_t Address,
132 const void *Decoder);
Colin LeMahieu404d5b22015-02-10 16:59:36 +0000133static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
Colin LeMahieube8c4532015-06-05 16:00:11 +0000134 uint64_t Address,
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000135 const void *Decoder);
136
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000137static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
138 uint64_t Address, const void *Decoder);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000139static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
140 uint64_t /*Address*/, const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000141static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
142 const void *Decoder);
Colin LeMahieuefa74e02014-11-18 20:28:11 +0000143
David Blaikief157ad42017-12-14 23:45:58 +0000144static DecodeStatus s4_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
145 const void *Decoder) {
146 signedDecoder<4>(MI, tmp, Decoder);
147 return MCDisassembler::Success;
148}
149static DecodeStatus s29_3ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
150 const void *Decoder) {
151 signedDecoder<14>(MI, tmp, Decoder);
152 return MCDisassembler::Success;
153}
154static DecodeStatus s8_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
155 const void *Decoder) {
156 signedDecoder<8>(MI, tmp, Decoder);
157 return MCDisassembler::Success;
158}
159static DecodeStatus s4_3ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
160 const void *Decoder) {
161 signedDecoder<7>(MI, tmp, Decoder);
162 return MCDisassembler::Success;
163}
164static DecodeStatus s31_1ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
165 const void *Decoder) {
166 signedDecoder<12>(MI, tmp, Decoder);
167 return MCDisassembler::Success;
168}
169static DecodeStatus s3_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
170 const void *Decoder) {
171 signedDecoder<3>(MI, tmp, Decoder);
172 return MCDisassembler::Success;
173}
174static DecodeStatus s30_2ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
175 const void *Decoder) {
176 signedDecoder<13>(MI, tmp, Decoder);
177 return MCDisassembler::Success;
178}
179static DecodeStatus s6_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
180 const void *Decoder) {
181 signedDecoder<6>(MI, tmp, Decoder);
182 return MCDisassembler::Success;
183}
184static DecodeStatus s6_3ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
185 const void *Decoder) {
186 signedDecoder<9>(MI, tmp, Decoder);
187 return MCDisassembler::Success;
188}
189static DecodeStatus s4_1ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
190 const void *Decoder) {
191 signedDecoder<5>(MI, tmp, Decoder);
192 return MCDisassembler::Success;
193}
194static DecodeStatus s4_2ImmDecoder(MCInst &MI, unsigned tmp, uint64_t,
195 const void *Decoder) {
196 signedDecoder<6>(MI, tmp, Decoder);
197 return MCDisassembler::Success;
198}
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000199#include "HexagonGenDisassemblerTables.inc"
200
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000201static MCDisassembler *createHexagonDisassembler(const Target &T,
202 const MCSubtargetInfo &STI,
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000203 MCContext &Ctx) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000204 return new HexagonDisassembler(STI, Ctx, T.createMCInstrInfo());
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000205}
206
207extern "C" void LLVMInitializeHexagonDisassembler() {
Mehdi Aminif42454b2016-10-09 23:00:34 +0000208 TargetRegistry::RegisterMCDisassembler(getTheHexagonTarget(),
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000209 createHexagonDisassembler);
210}
211
212DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000213 ArrayRef<uint8_t> Bytes,
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000214 uint64_t Address,
215 raw_ostream &os,
216 raw_ostream &cs) const {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000217 DecodeStatus Result = DecodeStatus::Success;
218 bool Complete = false;
219 Size = 0;
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000220
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000221 *CurrentBundle = &MI;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000222 MI.setOpcode(Hexagon::BUNDLE);
223 MI.addOperand(MCOperand::createImm(0));
Eugene Zelenko82085922016-12-13 22:13:50 +0000224 while (Result == Success && !Complete) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000225 if (Bytes.size() < HEXAGON_INSTR_SIZE)
226 return MCDisassembler::Fail;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000227 MCInst *Inst = new (getContext()) MCInst;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000228 Result = getSingleInstruction(*Inst, MI, Bytes, Address, os, cs, Complete);
229 MI.addOperand(MCOperand::createInst(Inst));
230 Size += HEXAGON_INSTR_SIZE;
231 Bytes = Bytes.slice(HEXAGON_INSTR_SIZE);
232 }
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000233 if (Result == MCDisassembler::Fail)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000234 return Result;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000235 if (Size > HEXAGON_MAX_PACKET_SIZE)
236 return MCDisassembler::Fail;
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000237 HexagonMCChecker Checker(getContext(), *MCII, STI, MI,
238 *getContext().getRegisterInfo(), false);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000239 if (!Checker.check())
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000240 return MCDisassembler::Fail;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000241 remapInstruction(MI);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000242 return MCDisassembler::Success;
243}
244
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000245void HexagonDisassembler::remapInstruction(MCInst &Instr) const {
246 for (auto I: HexagonMCInstrInfo::bundleInstructions(Instr)) {
247 auto &MI = const_cast<MCInst &>(*I.getInst());
248 switch (MI.getOpcode()) {
249 case Hexagon::S2_allocframe:
250 if (MI.getOperand(0).getReg() == Hexagon::R29) {
251 MI.setOpcode(Hexagon::S6_allocframe_to_raw);
252 MI.erase(MI.begin () + 1);
253 MI.erase(MI.begin ());
254 }
255 break;
256 case Hexagon::L2_deallocframe:
257 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
258 MI.getOperand(1).getReg() == Hexagon::R30) {
259 MI.setOpcode(L6_deallocframe_map_to_raw);
260 MI.erase(MI.begin () + 1);
261 MI.erase(MI.begin ());
262 }
263 break;
264 case Hexagon::L4_return:
265 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
266 MI.getOperand(1).getReg() == Hexagon::R30) {
267 MI.setOpcode(L6_return_map_to_raw);
268 MI.erase(MI.begin () + 1);
269 MI.erase(MI.begin ());
270 }
271 break;
272 case Hexagon::L4_return_t:
273 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
274 MI.getOperand(2).getReg() == Hexagon::R30) {
275 MI.setOpcode(L4_return_map_to_raw_t);
276 MI.erase(MI.begin () + 2);
277 MI.erase(MI.begin ());
278 }
279 break;
280 case Hexagon::L4_return_f:
281 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
282 MI.getOperand(2).getReg() == Hexagon::R30) {
283 MI.setOpcode(L4_return_map_to_raw_f);
284 MI.erase(MI.begin () + 2);
285 MI.erase(MI.begin ());
286 }
287 break;
288 case Hexagon::L4_return_tnew_pt:
289 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
290 MI.getOperand(2).getReg() == Hexagon::R30) {
291 MI.setOpcode(L4_return_map_to_raw_tnew_pt);
292 MI.erase(MI.begin () + 2);
293 MI.erase(MI.begin ());
294 }
295 break;
296 case Hexagon::L4_return_fnew_pt:
297 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
298 MI.getOperand(2).getReg() == Hexagon::R30) {
299 MI.setOpcode(L4_return_map_to_raw_fnew_pt);
300 MI.erase(MI.begin () + 2);
301 MI.erase(MI.begin ());
302 }
303 break;
304 case Hexagon::L4_return_tnew_pnt:
305 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
306 MI.getOperand(2).getReg() == Hexagon::R30) {
307 MI.setOpcode(L4_return_map_to_raw_tnew_pnt);
308 MI.erase(MI.begin () + 2);
309 MI.erase(MI.begin ());
310 }
311 break;
312 case Hexagon::L4_return_fnew_pnt:
313 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
314 MI.getOperand(2).getReg() == Hexagon::R30) {
315 MI.setOpcode(L4_return_map_to_raw_fnew_pnt);
316 MI.erase(MI.begin () + 2);
317 MI.erase(MI.begin ());
318 }
319 break;
320 }
321 }
322}
323
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000324static void adjustDuplex(MCInst &MI, MCContext &Context) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000325 switch (MI.getOpcode()) {
326 case Hexagon::SA1_setin1:
327 MI.insert(MI.begin() + 1,
328 MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
329 break;
330 case Hexagon::SA1_dec:
331 MI.insert(MI.begin() + 2,
332 MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
333 break;
334 default:
335 break;
336 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000337}
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000338
339DecodeStatus HexagonDisassembler::getSingleInstruction(
340 MCInst &MI, MCInst &MCB, ArrayRef<uint8_t> Bytes, uint64_t Address,
341 raw_ostream &os, raw_ostream &cs, bool &Complete) const {
342 assert(Bytes.size() >= HEXAGON_INSTR_SIZE);
343
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000344 uint32_t Instruction = support::endian::read32le(Bytes.data());
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000345
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000346 auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB);
347 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
348 HexagonII::INST_PARSE_LOOP_END) {
349 if (BundleSize == 0)
350 HexagonMCInstrInfo::setInnerLoop(MCB);
351 else if (BundleSize == 1)
352 HexagonMCInstrInfo::setOuterLoop(MCB);
353 else
354 return DecodeStatus::Fail;
355 }
356
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000357 CurrentExtender = HexagonMCInstrInfo::extenderForIndex(
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000358 MCB, HexagonMCInstrInfo::bundleSize(MCB));
359
360 DecodeStatus Result = DecodeStatus::Fail;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000361 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
Colin LeMahieube8c4532015-06-05 16:00:11 +0000362 HexagonII::INST_PARSE_DUPLEX) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000363 unsigned duplexIClass;
364 uint8_t const *DecodeLow, *DecodeHigh;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000365 duplexIClass = ((Instruction >> 28) & 0xe) | ((Instruction >> 13) & 0x1);
366 switch (duplexIClass) {
367 default:
368 return MCDisassembler::Fail;
369 case 0:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000370 DecodeLow = DecoderTableSUBINSN_L132;
371 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000372 break;
373 case 1:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000374 DecodeLow = DecoderTableSUBINSN_L232;
375 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000376 break;
377 case 2:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000378 DecodeLow = DecoderTableSUBINSN_L232;
379 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000380 break;
381 case 3:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000382 DecodeLow = DecoderTableSUBINSN_A32;
383 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000384 break;
385 case 4:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000386 DecodeLow = DecoderTableSUBINSN_L132;
387 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000388 break;
389 case 5:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000390 DecodeLow = DecoderTableSUBINSN_L232;
391 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000392 break;
393 case 6:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000394 DecodeLow = DecoderTableSUBINSN_S132;
395 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000396 break;
397 case 7:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000398 DecodeLow = DecoderTableSUBINSN_S232;
399 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000400 break;
401 case 8:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000402 DecodeLow = DecoderTableSUBINSN_S132;
403 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000404 break;
405 case 9:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000406 DecodeLow = DecoderTableSUBINSN_S132;
407 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000408 break;
409 case 10:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000410 DecodeLow = DecoderTableSUBINSN_S132;
411 DecodeHigh = DecoderTableSUBINSN_S132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000412 break;
413 case 11:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000414 DecodeLow = DecoderTableSUBINSN_S232;
415 DecodeHigh = DecoderTableSUBINSN_S132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000416 break;
417 case 12:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000418 DecodeLow = DecoderTableSUBINSN_S232;
419 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000420 break;
421 case 13:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000422 DecodeLow = DecoderTableSUBINSN_S232;
423 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000424 break;
425 case 14:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000426 DecodeLow = DecoderTableSUBINSN_S232;
427 DecodeHigh = DecoderTableSUBINSN_S232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000428 break;
429 }
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000430 MI.setOpcode(Hexagon::DuplexIClass0 + duplexIClass);
Colin LeMahieube8c4532015-06-05 16:00:11 +0000431 MCInst *MILow = new (getContext()) MCInst;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000432 MCInst *MIHigh = new (getContext()) MCInst;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000433 auto TmpExtender = CurrentExtender;
434 CurrentExtender =
435 nullptr; // constant extenders in duplex must always be in slot 1
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000436 Result = decodeInstruction(DecodeLow, *MILow, Instruction & 0x1fff, Address,
437 this, STI);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000438 CurrentExtender = TmpExtender;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000439 if (Result != DecodeStatus::Success)
440 return DecodeStatus::Fail;
441 adjustDuplex(*MILow, getContext());
442 Result = decodeInstruction(
443 DecodeHigh, *MIHigh, (Instruction >> 16) & 0x1fff, Address, this, STI);
444 if (Result != DecodeStatus::Success)
445 return DecodeStatus::Fail;
446 adjustDuplex(*MIHigh, getContext());
Colin LeMahieube8c4532015-06-05 16:00:11 +0000447 MCOperand OPLow = MCOperand::createInst(MILow);
448 MCOperand OPHigh = MCOperand::createInst(MIHigh);
449 MI.addOperand(OPLow);
450 MI.addOperand(OPHigh);
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000451 Complete = true;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000452 } else {
453 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
454 HexagonII::INST_PARSE_PACKET_END)
455 Complete = true;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000456
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000457 if (CurrentExtender != nullptr)
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000458 Result = decodeInstruction(DecoderTableMustExtend32, MI, Instruction,
459 Address, this, STI);
460
461 if (Result != MCDisassembler::Success)
462 Result = decodeInstruction(DecoderTable32, MI, Instruction, Address, this,
463 STI);
464
465 if (Result != MCDisassembler::Success &&
466 STI.getFeatureBits()[Hexagon::ExtensionHVX])
467 Result = decodeInstruction(DecoderTableEXT_mmvec32, MI, Instruction,
468 Address, this, STI);
469
Colin LeMahieube8c4532015-06-05 16:00:11 +0000470 }
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000471
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000472 switch (MI.getOpcode()) {
Colin LeMahieu81707542016-12-05 04:29:00 +0000473 case Hexagon::J4_cmpeqn1_f_jumpnv_nt:
474 case Hexagon::J4_cmpeqn1_f_jumpnv_t:
475 case Hexagon::J4_cmpeqn1_fp0_jump_nt:
476 case Hexagon::J4_cmpeqn1_fp0_jump_t:
477 case Hexagon::J4_cmpeqn1_fp1_jump_nt:
478 case Hexagon::J4_cmpeqn1_fp1_jump_t:
479 case Hexagon::J4_cmpeqn1_t_jumpnv_nt:
480 case Hexagon::J4_cmpeqn1_t_jumpnv_t:
481 case Hexagon::J4_cmpeqn1_tp0_jump_nt:
482 case Hexagon::J4_cmpeqn1_tp0_jump_t:
483 case Hexagon::J4_cmpeqn1_tp1_jump_nt:
484 case Hexagon::J4_cmpeqn1_tp1_jump_t:
485 case Hexagon::J4_cmpgtn1_f_jumpnv_nt:
486 case Hexagon::J4_cmpgtn1_f_jumpnv_t:
487 case Hexagon::J4_cmpgtn1_fp0_jump_nt:
488 case Hexagon::J4_cmpgtn1_fp0_jump_t:
489 case Hexagon::J4_cmpgtn1_fp1_jump_nt:
490 case Hexagon::J4_cmpgtn1_fp1_jump_t:
491 case Hexagon::J4_cmpgtn1_t_jumpnv_nt:
492 case Hexagon::J4_cmpgtn1_t_jumpnv_t:
493 case Hexagon::J4_cmpgtn1_tp0_jump_nt:
494 case Hexagon::J4_cmpgtn1_tp0_jump_t:
495 case Hexagon::J4_cmpgtn1_tp1_jump_nt:
496 case Hexagon::J4_cmpgtn1_tp1_jump_t:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000497 MI.insert(MI.begin() + 1,
498 MCOperand::createExpr(MCConstantExpr::create(-1, getContext())));
Colin LeMahieu81707542016-12-05 04:29:00 +0000499 break;
500 default:
501 break;
502 }
503
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000504 if (HexagonMCInstrInfo::isNewValue(*MCII, MI)) {
505 unsigned OpIndex = HexagonMCInstrInfo::getNewValueOp(*MCII, MI);
506 MCOperand &MCO = MI.getOperand(OpIndex);
507 assert(MCO.isReg() && "New value consumers must be registers");
508 unsigned Register =
509 getContext().getRegisterInfo()->getEncodingValue(MCO.getReg());
510 if ((Register & 0x6) == 0)
511 // HexagonPRM 10.11 Bit 1-2 == 0 is reserved
512 return MCDisassembler::Fail;
513 unsigned Lookback = (Register & 0x6) >> 1;
514 unsigned Offset = 1;
515 bool Vector = HexagonMCInstrInfo::isVector(*MCII, MI);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000516 bool PrevVector = false;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000517 auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
518 auto i = Instructions.end() - 1;
519 for (auto n = Instructions.begin() - 1;; --i, ++Offset) {
520 if (i == n)
521 // Couldn't find producer
522 return MCDisassembler::Fail;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000523 bool CurrentVector = HexagonMCInstrInfo::isVector(*MCII, *i->getInst());
524 if (Vector && !CurrentVector)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000525 // Skip scalars when calculating distances for vectors
526 ++Lookback;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000527 if (HexagonMCInstrInfo::isImmext(*i->getInst()) && (Vector == PrevVector))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000528 ++Lookback;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000529 PrevVector = CurrentVector;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000530 if (Offset == Lookback)
531 break;
532 }
533 auto const &Inst = *i->getInst();
534 bool SubregBit = (Register & 0x1) != 0;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000535 if (HexagonMCInstrInfo::hasNewValue2(*MCII, Inst)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000536 // If subreg bit is set we're selecting the second produced newvalue
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000537 unsigned Producer = SubregBit ?
538 HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg() :
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000539 HexagonMCInstrInfo::getNewValueOperand2(*MCII, Inst).getReg();
540 assert(Producer != Hexagon::NoRegister);
541 MCO.setReg(Producer);
542 } else if (HexagonMCInstrInfo::hasNewValue(*MCII, Inst)) {
543 unsigned Producer =
544 HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg();
545 if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
546 Producer = ((Producer - Hexagon::W0) << 1) + SubregBit + Hexagon::V0;
547 else if (SubregBit)
Colin LeMahieu2d497a02016-03-01 22:05:03 +0000548 // Hexagon PRM 10.11 New-value operands
549 // Nt[0] is reserved and should always be encoded as zero.
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000550 return MCDisassembler::Fail;
551 assert(Producer != Hexagon::NoRegister);
552 MCO.setReg(Producer);
553 } else
554 return MCDisassembler::Fail;
555 }
556
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000557 if (CurrentExtender != nullptr) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000558 MCInst const &Inst = HexagonMCInstrInfo::isDuplex(*MCII, MI)
559 ? *MI.getOperand(1).getInst()
560 : MI;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000561 if (!HexagonMCInstrInfo::isExtendable(*MCII, Inst) &&
562 !HexagonMCInstrInfo::isExtended(*MCII, Inst))
563 return MCDisassembler::Fail;
564 }
Colin LeMahieu5d6f03b2014-12-04 03:41:21 +0000565 return Result;
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000566}
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000567
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000568static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,
Craig Toppere5e035a32015-12-05 07:13:35 +0000569 ArrayRef<MCPhysReg> Table) {
Craig Topper3da000c2015-12-01 06:13:04 +0000570 if (RegNo < Table.size()) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000571 Inst.addOperand(MCOperand::createReg(Table[RegNo]));
572 return MCDisassembler::Success;
Craig Topper3da000c2015-12-01 06:13:04 +0000573 }
574
575 return MCDisassembler::Fail;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000576}
577
Colin LeMahieu7c958712015-10-17 01:33:04 +0000578static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
579 uint64_t Address,
580 const void *Decoder) {
581 return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
582}
583
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000584static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
585 uint64_t Address,
586 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000587 static const MCPhysReg IntRegDecoderTable[] = {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000588 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
589 Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9,
590 Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
591 Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
592 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
593 Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
594 Hexagon::R30, Hexagon::R31};
595
Craig Toppere5e035a32015-12-05 07:13:35 +0000596 return DecodeRegisterClass(Inst, RegNo, IntRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000597}
Colin LeMahieu7c958712015-10-17 01:33:04 +0000598
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000599static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
600 unsigned RegNo,
601 uint64_t Address,
602 const void *Decoder) {
603 static const MCPhysReg GeneralSubRegDecoderTable[] = {
604 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3,
605 Hexagon::R4, Hexagon::R5, Hexagon::R6, Hexagon::R7,
606 Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
607 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23,
608 };
609
610 return DecodeRegisterClass(Inst, RegNo, GeneralSubRegDecoderTable);
611}
612
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000613static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,
614 uint64_t /*Address*/,
615 const void *Decoder) {
616 static const MCPhysReg HvxVRDecoderTable[] = {
Colin LeMahieu7c958712015-10-17 01:33:04 +0000617 Hexagon::V0, Hexagon::V1, Hexagon::V2, Hexagon::V3, Hexagon::V4,
618 Hexagon::V5, Hexagon::V6, Hexagon::V7, Hexagon::V8, Hexagon::V9,
619 Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
620 Hexagon::V15, Hexagon::V16, Hexagon::V17, Hexagon::V18, Hexagon::V19,
621 Hexagon::V20, Hexagon::V21, Hexagon::V22, Hexagon::V23, Hexagon::V24,
622 Hexagon::V25, Hexagon::V26, Hexagon::V27, Hexagon::V28, Hexagon::V29,
623 Hexagon::V30, Hexagon::V31};
624
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000625 return DecodeRegisterClass(Inst, RegNo, HvxVRDecoderTable);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000626}
627
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000628static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
629 uint64_t /*Address*/,
630 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000631 static const MCPhysReg DoubleRegDecoderTable[] = {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000632 Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,
633 Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7,
634 Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11,
635 Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15};
636
Craig Toppere5e035a32015-12-05 07:13:35 +0000637 return DecodeRegisterClass(Inst, RegNo >> 1, DoubleRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000638}
639
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000640static DecodeStatus DecodeGeneralDoubleLow8RegsRegisterClass(
641 MCInst &Inst, unsigned RegNo, uint64_t /*Address*/, const void *Decoder) {
642 static const MCPhysReg GeneralDoubleLow8RegDecoderTable[] = {
643 Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,
644 Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11};
645
646 return DecodeRegisterClass(Inst, RegNo, GeneralDoubleLow8RegDecoderTable);
647}
648
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000649static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,
650 uint64_t /*Address*/,
651 const void *Decoder) {
652 static const MCPhysReg HvxWRDecoderTable[] = {
Colin LeMahieu7c958712015-10-17 01:33:04 +0000653 Hexagon::W0, Hexagon::W1, Hexagon::W2, Hexagon::W3,
654 Hexagon::W4, Hexagon::W5, Hexagon::W6, Hexagon::W7,
655 Hexagon::W8, Hexagon::W9, Hexagon::W10, Hexagon::W11,
656 Hexagon::W12, Hexagon::W13, Hexagon::W14, Hexagon::W15};
657
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000658 return (DecodeRegisterClass(Inst, RegNo >> 1, HvxWRDecoderTable));
Colin LeMahieu7c958712015-10-17 01:33:04 +0000659}
660
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000661static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
662 uint64_t /*Address*/,
663 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000664 static const MCPhysReg PredRegDecoderTable[] = {Hexagon::P0, Hexagon::P1,
665 Hexagon::P2, Hexagon::P3};
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000666
Craig Toppere5e035a32015-12-05 07:13:35 +0000667 return DecodeRegisterClass(Inst, RegNo, PredRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000668}
669
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000670static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,
671 uint64_t /*Address*/,
672 const void *Decoder) {
673 static const MCPhysReg HvxQRDecoderTable[] = {Hexagon::Q0, Hexagon::Q1,
674 Hexagon::Q2, Hexagon::Q3};
Colin LeMahieu7c958712015-10-17 01:33:04 +0000675
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000676 return DecodeRegisterClass(Inst, RegNo, HvxQRDecoderTable);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000677}
Colin LeMahieube8c4532015-06-05 16:00:11 +0000678
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000679static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
680 uint64_t /*Address*/,
681 const void *Decoder) {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000682 using namespace Hexagon;
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000683
Craig Toppere5e035a32015-12-05 07:13:35 +0000684 static const MCPhysReg CtrlRegDecoderTable[] = {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000685 /* 0 */ SA0, LC0, SA1, LC1,
Krzysztof Parzyszeke2603322017-05-05 22:12:12 +0000686 /* 4 */ P3_0, C5, M0, M1,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000687 /* 8 */ USR, PC, UGP, GP,
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000688 /* 12 */ CS0, CS1, UPCYCLELO, UPCYCLEHI,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000689 /* 16 */ FRAMELIMIT, FRAMEKEY, PKTCOUNTLO, PKTCOUNTHI,
690 /* 20 */ 0, 0, 0, 0,
691 /* 24 */ 0, 0, 0, 0,
692 /* 28 */ 0, 0, UTIMERLO, UTIMERHI
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000693 };
694
Craig Topper6261e1b2015-12-01 06:13:06 +0000695 if (RegNo >= array_lengthof(CtrlRegDecoderTable))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000696 return MCDisassembler::Fail;
697
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000698 static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
699 if (CtrlRegDecoderTable[RegNo] == NoRegister)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000700 return MCDisassembler::Fail;
701
702 unsigned Register = CtrlRegDecoderTable[RegNo];
703 Inst.addOperand(MCOperand::createReg(Register));
704 return MCDisassembler::Success;
705}
706
707static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
708 uint64_t /*Address*/,
709 const void *Decoder) {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000710 using namespace Hexagon;
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000711
Craig Toppere5e035a32015-12-05 07:13:35 +0000712 static const MCPhysReg CtrlReg64DecoderTable[] = {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000713 /* 0 */ C1_0, 0, C3_2, 0,
714 /* 4 */ C5_4, 0, C7_6, 0,
715 /* 8 */ C9_8, 0, C11_10, 0,
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000716 /* 12 */ CS, 0, UPCYCLE, 0,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000717 /* 16 */ C17_16, 0, PKTCOUNT, 0,
718 /* 20 */ 0, 0, 0, 0,
719 /* 24 */ 0, 0, 0, 0,
720 /* 28 */ 0, 0, UTIMER, 0
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000721 };
722
Craig Topper6261e1b2015-12-01 06:13:06 +0000723 if (RegNo >= array_lengthof(CtrlReg64DecoderTable))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000724 return MCDisassembler::Fail;
725
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000726 static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
727 if (CtrlReg64DecoderTable[RegNo] == NoRegister)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000728 return MCDisassembler::Fail;
729
730 unsigned Register = CtrlReg64DecoderTable[RegNo];
731 Inst.addOperand(MCOperand::createReg(Register));
732 return MCDisassembler::Success;
733}
734
735static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
736 uint64_t /*Address*/,
737 const void *Decoder) {
738 unsigned Register = 0;
739 switch (RegNo) {
740 case 0:
741 Register = Hexagon::M0;
742 break;
743 case 1:
744 Register = Hexagon::M1;
745 break;
746 default:
747 return MCDisassembler::Fail;
748 }
749 Inst.addOperand(MCOperand::createReg(Register));
750 return MCDisassembler::Success;
751}
752
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000753static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
754 uint64_t /*Address*/,
755 const void *Decoder) {
756 HexagonDisassembler const &Disassembler = disassembler(Decoder);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000757 int64_t FullValue = fullValue(Disassembler, MI, tmp);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000758 assert(FullValue >= 0 && "Negative in unsigned decoder");
759 HexagonMCInstrInfo::addConstant(MI, FullValue, Disassembler.getContext());
760 return MCDisassembler::Success;
761}
762
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000763static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
764 uint64_t /*Address*/, const void *Decoder) {
765 HexagonDisassembler const &Disassembler = disassembler(Decoder);
766 unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
767 tmp = SignExtend64(tmp, Bits);
768 signedDecoder<32>(MI, tmp, Decoder);
769 return MCDisassembler::Success;
770}
771
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000772// custom decoder for various jump/call immediates
773static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
774 const void *Decoder) {
775 HexagonDisassembler const &Disassembler = disassembler(Decoder);
776 unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
777 // r13_2 is not extendable, so if there are no extent bits, it's r13_2
778 if (Bits == 0)
779 Bits = 15;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000780 uint64_t FullValue = fullValue(Disassembler, MI, SignExtend64(tmp, Bits));
781 uint32_t Extended = FullValue + Address;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000782 if (!Disassembler.tryAddingSymbolicOperand(MI, Extended, Address, true, 0, 4))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000783 HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
784 return MCDisassembler::Success;
785}