blob: 8a6bb4756967f441302c7ee1c2d48ba0b6f354f7 [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
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000144#include "HexagonDepDecoders.h"
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000145#include "HexagonGenDisassemblerTables.inc"
146
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000147static MCDisassembler *createHexagonDisassembler(const Target &T,
148 const MCSubtargetInfo &STI,
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000149 MCContext &Ctx) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000150 return new HexagonDisassembler(STI, Ctx, T.createMCInstrInfo());
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000151}
152
153extern "C" void LLVMInitializeHexagonDisassembler() {
Mehdi Aminif42454b2016-10-09 23:00:34 +0000154 TargetRegistry::RegisterMCDisassembler(getTheHexagonTarget(),
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000155 createHexagonDisassembler);
156}
157
158DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000159 ArrayRef<uint8_t> Bytes,
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000160 uint64_t Address,
161 raw_ostream &os,
162 raw_ostream &cs) const {
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000163 (void)&s10_0ImmDecoder;
164 (void)&s10_6ImmDecoder;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000165 DecodeStatus Result = DecodeStatus::Success;
166 bool Complete = false;
167 Size = 0;
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000168
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000169 *CurrentBundle = &MI;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000170 MI.setOpcode(Hexagon::BUNDLE);
171 MI.addOperand(MCOperand::createImm(0));
Eugene Zelenko82085922016-12-13 22:13:50 +0000172 while (Result == Success && !Complete) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000173 if (Bytes.size() < HEXAGON_INSTR_SIZE)
174 return MCDisassembler::Fail;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000175 MCInst *Inst = new (getContext()) MCInst;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000176 Result = getSingleInstruction(*Inst, MI, Bytes, Address, os, cs, Complete);
177 MI.addOperand(MCOperand::createInst(Inst));
178 Size += HEXAGON_INSTR_SIZE;
179 Bytes = Bytes.slice(HEXAGON_INSTR_SIZE);
180 }
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000181 if (Result == MCDisassembler::Fail)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000182 return Result;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000183 if (Size > HEXAGON_MAX_PACKET_SIZE)
184 return MCDisassembler::Fail;
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000185 HexagonMCChecker Checker(getContext(), *MCII, STI, MI,
186 *getContext().getRegisterInfo(), false);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000187 if (!Checker.check())
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000188 return MCDisassembler::Fail;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000189 remapInstruction(MI);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000190 return MCDisassembler::Success;
191}
192
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000193void HexagonDisassembler::remapInstruction(MCInst &Instr) const {
194 for (auto I: HexagonMCInstrInfo::bundleInstructions(Instr)) {
195 auto &MI = const_cast<MCInst &>(*I.getInst());
196 switch (MI.getOpcode()) {
197 case Hexagon::S2_allocframe:
198 if (MI.getOperand(0).getReg() == Hexagon::R29) {
199 MI.setOpcode(Hexagon::S6_allocframe_to_raw);
200 MI.erase(MI.begin () + 1);
201 MI.erase(MI.begin ());
202 }
203 break;
204 case Hexagon::L2_deallocframe:
205 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
206 MI.getOperand(1).getReg() == Hexagon::R30) {
207 MI.setOpcode(L6_deallocframe_map_to_raw);
208 MI.erase(MI.begin () + 1);
209 MI.erase(MI.begin ());
210 }
211 break;
212 case Hexagon::L4_return:
213 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
214 MI.getOperand(1).getReg() == Hexagon::R30) {
215 MI.setOpcode(L6_return_map_to_raw);
216 MI.erase(MI.begin () + 1);
217 MI.erase(MI.begin ());
218 }
219 break;
220 case Hexagon::L4_return_t:
221 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
222 MI.getOperand(2).getReg() == Hexagon::R30) {
223 MI.setOpcode(L4_return_map_to_raw_t);
224 MI.erase(MI.begin () + 2);
225 MI.erase(MI.begin ());
226 }
227 break;
228 case Hexagon::L4_return_f:
229 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
230 MI.getOperand(2).getReg() == Hexagon::R30) {
231 MI.setOpcode(L4_return_map_to_raw_f);
232 MI.erase(MI.begin () + 2);
233 MI.erase(MI.begin ());
234 }
235 break;
236 case Hexagon::L4_return_tnew_pt:
237 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
238 MI.getOperand(2).getReg() == Hexagon::R30) {
239 MI.setOpcode(L4_return_map_to_raw_tnew_pt);
240 MI.erase(MI.begin () + 2);
241 MI.erase(MI.begin ());
242 }
243 break;
244 case Hexagon::L4_return_fnew_pt:
245 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
246 MI.getOperand(2).getReg() == Hexagon::R30) {
247 MI.setOpcode(L4_return_map_to_raw_fnew_pt);
248 MI.erase(MI.begin () + 2);
249 MI.erase(MI.begin ());
250 }
251 break;
252 case Hexagon::L4_return_tnew_pnt:
253 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
254 MI.getOperand(2).getReg() == Hexagon::R30) {
255 MI.setOpcode(L4_return_map_to_raw_tnew_pnt);
256 MI.erase(MI.begin () + 2);
257 MI.erase(MI.begin ());
258 }
259 break;
260 case Hexagon::L4_return_fnew_pnt:
261 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
262 MI.getOperand(2).getReg() == Hexagon::R30) {
263 MI.setOpcode(L4_return_map_to_raw_fnew_pnt);
264 MI.erase(MI.begin () + 2);
265 MI.erase(MI.begin ());
266 }
267 break;
268 }
269 }
270}
271
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000272static void adjustDuplex(MCInst &MI, MCContext &Context) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000273 switch (MI.getOpcode()) {
274 case Hexagon::SA1_setin1:
275 MI.insert(MI.begin() + 1,
276 MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
277 break;
278 case Hexagon::SA1_dec:
279 MI.insert(MI.begin() + 2,
280 MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
281 break;
282 default:
283 break;
284 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000285}
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000286
287DecodeStatus HexagonDisassembler::getSingleInstruction(
288 MCInst &MI, MCInst &MCB, ArrayRef<uint8_t> Bytes, uint64_t Address,
289 raw_ostream &os, raw_ostream &cs, bool &Complete) const {
290 assert(Bytes.size() >= HEXAGON_INSTR_SIZE);
291
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000292 uint32_t Instruction = support::endian::read32le(Bytes.data());
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000293
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000294 auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB);
295 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
296 HexagonII::INST_PARSE_LOOP_END) {
297 if (BundleSize == 0)
298 HexagonMCInstrInfo::setInnerLoop(MCB);
299 else if (BundleSize == 1)
300 HexagonMCInstrInfo::setOuterLoop(MCB);
301 else
302 return DecodeStatus::Fail;
303 }
304
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000305 CurrentExtender = HexagonMCInstrInfo::extenderForIndex(
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000306 MCB, HexagonMCInstrInfo::bundleSize(MCB));
307
308 DecodeStatus Result = DecodeStatus::Fail;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000309 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
Colin LeMahieube8c4532015-06-05 16:00:11 +0000310 HexagonII::INST_PARSE_DUPLEX) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000311 unsigned duplexIClass;
312 uint8_t const *DecodeLow, *DecodeHigh;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000313 duplexIClass = ((Instruction >> 28) & 0xe) | ((Instruction >> 13) & 0x1);
314 switch (duplexIClass) {
315 default:
316 return MCDisassembler::Fail;
317 case 0:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000318 DecodeLow = DecoderTableSUBINSN_L132;
319 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000320 break;
321 case 1:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000322 DecodeLow = DecoderTableSUBINSN_L232;
323 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000324 break;
325 case 2:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000326 DecodeLow = DecoderTableSUBINSN_L232;
327 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000328 break;
329 case 3:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000330 DecodeLow = DecoderTableSUBINSN_A32;
331 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000332 break;
333 case 4:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000334 DecodeLow = DecoderTableSUBINSN_L132;
335 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000336 break;
337 case 5:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000338 DecodeLow = DecoderTableSUBINSN_L232;
339 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000340 break;
341 case 6:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000342 DecodeLow = DecoderTableSUBINSN_S132;
343 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000344 break;
345 case 7:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000346 DecodeLow = DecoderTableSUBINSN_S232;
347 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000348 break;
349 case 8:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000350 DecodeLow = DecoderTableSUBINSN_S132;
351 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000352 break;
353 case 9:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000354 DecodeLow = DecoderTableSUBINSN_S132;
355 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000356 break;
357 case 10:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000358 DecodeLow = DecoderTableSUBINSN_S132;
359 DecodeHigh = DecoderTableSUBINSN_S132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000360 break;
361 case 11:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000362 DecodeLow = DecoderTableSUBINSN_S232;
363 DecodeHigh = DecoderTableSUBINSN_S132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000364 break;
365 case 12:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000366 DecodeLow = DecoderTableSUBINSN_S232;
367 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000368 break;
369 case 13:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000370 DecodeLow = DecoderTableSUBINSN_S232;
371 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000372 break;
373 case 14:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000374 DecodeLow = DecoderTableSUBINSN_S232;
375 DecodeHigh = DecoderTableSUBINSN_S232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000376 break;
377 }
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000378 MI.setOpcode(Hexagon::DuplexIClass0 + duplexIClass);
Colin LeMahieube8c4532015-06-05 16:00:11 +0000379 MCInst *MILow = new (getContext()) MCInst;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000380 MCInst *MIHigh = new (getContext()) MCInst;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000381 auto TmpExtender = CurrentExtender;
382 CurrentExtender =
383 nullptr; // constant extenders in duplex must always be in slot 1
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000384 Result = decodeInstruction(DecodeLow, *MILow, Instruction & 0x1fff, Address,
385 this, STI);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000386 CurrentExtender = TmpExtender;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000387 if (Result != DecodeStatus::Success)
388 return DecodeStatus::Fail;
389 adjustDuplex(*MILow, getContext());
390 Result = decodeInstruction(
391 DecodeHigh, *MIHigh, (Instruction >> 16) & 0x1fff, Address, this, STI);
392 if (Result != DecodeStatus::Success)
393 return DecodeStatus::Fail;
394 adjustDuplex(*MIHigh, getContext());
Colin LeMahieube8c4532015-06-05 16:00:11 +0000395 MCOperand OPLow = MCOperand::createInst(MILow);
396 MCOperand OPHigh = MCOperand::createInst(MIHigh);
397 MI.addOperand(OPLow);
398 MI.addOperand(OPHigh);
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000399 Complete = true;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000400 } else {
401 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
402 HexagonII::INST_PARSE_PACKET_END)
403 Complete = true;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000404
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000405 if (CurrentExtender != nullptr)
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000406 Result = decodeInstruction(DecoderTableMustExtend32, MI, Instruction,
407 Address, this, STI);
408
409 if (Result != MCDisassembler::Success)
410 Result = decodeInstruction(DecoderTable32, MI, Instruction, Address, this,
411 STI);
412
413 if (Result != MCDisassembler::Success &&
414 STI.getFeatureBits()[Hexagon::ExtensionHVX])
415 Result = decodeInstruction(DecoderTableEXT_mmvec32, MI, Instruction,
416 Address, this, STI);
417
Colin LeMahieube8c4532015-06-05 16:00:11 +0000418 }
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000419
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000420 switch (MI.getOpcode()) {
Colin LeMahieu81707542016-12-05 04:29:00 +0000421 case Hexagon::J4_cmpeqn1_f_jumpnv_nt:
422 case Hexagon::J4_cmpeqn1_f_jumpnv_t:
423 case Hexagon::J4_cmpeqn1_fp0_jump_nt:
424 case Hexagon::J4_cmpeqn1_fp0_jump_t:
425 case Hexagon::J4_cmpeqn1_fp1_jump_nt:
426 case Hexagon::J4_cmpeqn1_fp1_jump_t:
427 case Hexagon::J4_cmpeqn1_t_jumpnv_nt:
428 case Hexagon::J4_cmpeqn1_t_jumpnv_t:
429 case Hexagon::J4_cmpeqn1_tp0_jump_nt:
430 case Hexagon::J4_cmpeqn1_tp0_jump_t:
431 case Hexagon::J4_cmpeqn1_tp1_jump_nt:
432 case Hexagon::J4_cmpeqn1_tp1_jump_t:
433 case Hexagon::J4_cmpgtn1_f_jumpnv_nt:
434 case Hexagon::J4_cmpgtn1_f_jumpnv_t:
435 case Hexagon::J4_cmpgtn1_fp0_jump_nt:
436 case Hexagon::J4_cmpgtn1_fp0_jump_t:
437 case Hexagon::J4_cmpgtn1_fp1_jump_nt:
438 case Hexagon::J4_cmpgtn1_fp1_jump_t:
439 case Hexagon::J4_cmpgtn1_t_jumpnv_nt:
440 case Hexagon::J4_cmpgtn1_t_jumpnv_t:
441 case Hexagon::J4_cmpgtn1_tp0_jump_nt:
442 case Hexagon::J4_cmpgtn1_tp0_jump_t:
443 case Hexagon::J4_cmpgtn1_tp1_jump_nt:
444 case Hexagon::J4_cmpgtn1_tp1_jump_t:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000445 MI.insert(MI.begin() + 1,
446 MCOperand::createExpr(MCConstantExpr::create(-1, getContext())));
Colin LeMahieu81707542016-12-05 04:29:00 +0000447 break;
448 default:
449 break;
450 }
451
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000452 if (HexagonMCInstrInfo::isNewValue(*MCII, MI)) {
453 unsigned OpIndex = HexagonMCInstrInfo::getNewValueOp(*MCII, MI);
454 MCOperand &MCO = MI.getOperand(OpIndex);
455 assert(MCO.isReg() && "New value consumers must be registers");
456 unsigned Register =
457 getContext().getRegisterInfo()->getEncodingValue(MCO.getReg());
458 if ((Register & 0x6) == 0)
459 // HexagonPRM 10.11 Bit 1-2 == 0 is reserved
460 return MCDisassembler::Fail;
461 unsigned Lookback = (Register & 0x6) >> 1;
462 unsigned Offset = 1;
463 bool Vector = HexagonMCInstrInfo::isVector(*MCII, MI);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000464 bool PrevVector = false;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000465 auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
466 auto i = Instructions.end() - 1;
467 for (auto n = Instructions.begin() - 1;; --i, ++Offset) {
468 if (i == n)
469 // Couldn't find producer
470 return MCDisassembler::Fail;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000471 bool CurrentVector = HexagonMCInstrInfo::isVector(*MCII, *i->getInst());
472 if (Vector && !CurrentVector)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000473 // Skip scalars when calculating distances for vectors
474 ++Lookback;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000475 if (HexagonMCInstrInfo::isImmext(*i->getInst()) && (Vector == PrevVector))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000476 ++Lookback;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000477 PrevVector = CurrentVector;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000478 if (Offset == Lookback)
479 break;
480 }
481 auto const &Inst = *i->getInst();
482 bool SubregBit = (Register & 0x1) != 0;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000483 if (HexagonMCInstrInfo::hasNewValue2(*MCII, Inst)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000484 // If subreg bit is set we're selecting the second produced newvalue
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000485 unsigned Producer = SubregBit ?
486 HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg() :
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000487 HexagonMCInstrInfo::getNewValueOperand2(*MCII, Inst).getReg();
488 assert(Producer != Hexagon::NoRegister);
489 MCO.setReg(Producer);
490 } else if (HexagonMCInstrInfo::hasNewValue(*MCII, Inst)) {
491 unsigned Producer =
492 HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg();
493 if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
494 Producer = ((Producer - Hexagon::W0) << 1) + SubregBit + Hexagon::V0;
495 else if (SubregBit)
Colin LeMahieu2d497a02016-03-01 22:05:03 +0000496 // Hexagon PRM 10.11 New-value operands
497 // Nt[0] is reserved and should always be encoded as zero.
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000498 return MCDisassembler::Fail;
499 assert(Producer != Hexagon::NoRegister);
500 MCO.setReg(Producer);
501 } else
502 return MCDisassembler::Fail;
503 }
504
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000505 if (CurrentExtender != nullptr) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000506 MCInst const &Inst = HexagonMCInstrInfo::isDuplex(*MCII, MI)
507 ? *MI.getOperand(1).getInst()
508 : MI;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000509 if (!HexagonMCInstrInfo::isExtendable(*MCII, Inst) &&
510 !HexagonMCInstrInfo::isExtended(*MCII, Inst))
511 return MCDisassembler::Fail;
512 }
Colin LeMahieu5d6f03b2014-12-04 03:41:21 +0000513 return Result;
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000514}
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000515
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000516static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,
Craig Toppere5e035a32015-12-05 07:13:35 +0000517 ArrayRef<MCPhysReg> Table) {
Craig Topper3da000c2015-12-01 06:13:04 +0000518 if (RegNo < Table.size()) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000519 Inst.addOperand(MCOperand::createReg(Table[RegNo]));
520 return MCDisassembler::Success;
Craig Topper3da000c2015-12-01 06:13:04 +0000521 }
522
523 return MCDisassembler::Fail;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000524}
525
Colin LeMahieu7c958712015-10-17 01:33:04 +0000526static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
527 uint64_t Address,
528 const void *Decoder) {
529 return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
530}
531
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000532static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
533 uint64_t Address,
534 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000535 static const MCPhysReg IntRegDecoderTable[] = {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000536 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
537 Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9,
538 Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
539 Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
540 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
541 Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
542 Hexagon::R30, Hexagon::R31};
543
Craig Toppere5e035a32015-12-05 07:13:35 +0000544 return DecodeRegisterClass(Inst, RegNo, IntRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000545}
Colin LeMahieu7c958712015-10-17 01:33:04 +0000546
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000547static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
548 unsigned RegNo,
549 uint64_t Address,
550 const void *Decoder) {
551 static const MCPhysReg GeneralSubRegDecoderTable[] = {
552 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3,
553 Hexagon::R4, Hexagon::R5, Hexagon::R6, Hexagon::R7,
554 Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
555 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23,
556 };
557
558 return DecodeRegisterClass(Inst, RegNo, GeneralSubRegDecoderTable);
559}
560
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000561static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,
562 uint64_t /*Address*/,
563 const void *Decoder) {
564 static const MCPhysReg HvxVRDecoderTable[] = {
Colin LeMahieu7c958712015-10-17 01:33:04 +0000565 Hexagon::V0, Hexagon::V1, Hexagon::V2, Hexagon::V3, Hexagon::V4,
566 Hexagon::V5, Hexagon::V6, Hexagon::V7, Hexagon::V8, Hexagon::V9,
567 Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
568 Hexagon::V15, Hexagon::V16, Hexagon::V17, Hexagon::V18, Hexagon::V19,
569 Hexagon::V20, Hexagon::V21, Hexagon::V22, Hexagon::V23, Hexagon::V24,
570 Hexagon::V25, Hexagon::V26, Hexagon::V27, Hexagon::V28, Hexagon::V29,
571 Hexagon::V30, Hexagon::V31};
572
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000573 return DecodeRegisterClass(Inst, RegNo, HvxVRDecoderTable);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000574}
575
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000576static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
577 uint64_t /*Address*/,
578 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000579 static const MCPhysReg DoubleRegDecoderTable[] = {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000580 Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,
581 Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7,
582 Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11,
583 Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15};
584
Craig Toppere5e035a32015-12-05 07:13:35 +0000585 return DecodeRegisterClass(Inst, RegNo >> 1, DoubleRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000586}
587
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000588static DecodeStatus DecodeGeneralDoubleLow8RegsRegisterClass(
589 MCInst &Inst, unsigned RegNo, uint64_t /*Address*/, const void *Decoder) {
590 static const MCPhysReg GeneralDoubleLow8RegDecoderTable[] = {
591 Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,
592 Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11};
593
594 return DecodeRegisterClass(Inst, RegNo, GeneralDoubleLow8RegDecoderTable);
595}
596
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000597static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,
598 uint64_t /*Address*/,
599 const void *Decoder) {
600 static const MCPhysReg HvxWRDecoderTable[] = {
Colin LeMahieu7c958712015-10-17 01:33:04 +0000601 Hexagon::W0, Hexagon::W1, Hexagon::W2, Hexagon::W3,
602 Hexagon::W4, Hexagon::W5, Hexagon::W6, Hexagon::W7,
603 Hexagon::W8, Hexagon::W9, Hexagon::W10, Hexagon::W11,
604 Hexagon::W12, Hexagon::W13, Hexagon::W14, Hexagon::W15};
605
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000606 return (DecodeRegisterClass(Inst, RegNo >> 1, HvxWRDecoderTable));
Colin LeMahieu7c958712015-10-17 01:33:04 +0000607}
608
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000609static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
610 uint64_t /*Address*/,
611 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000612 static const MCPhysReg PredRegDecoderTable[] = {Hexagon::P0, Hexagon::P1,
613 Hexagon::P2, Hexagon::P3};
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000614
Craig Toppere5e035a32015-12-05 07:13:35 +0000615 return DecodeRegisterClass(Inst, RegNo, PredRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000616}
617
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000618static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,
619 uint64_t /*Address*/,
620 const void *Decoder) {
621 static const MCPhysReg HvxQRDecoderTable[] = {Hexagon::Q0, Hexagon::Q1,
622 Hexagon::Q2, Hexagon::Q3};
Colin LeMahieu7c958712015-10-17 01:33:04 +0000623
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000624 return DecodeRegisterClass(Inst, RegNo, HvxQRDecoderTable);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000625}
Colin LeMahieube8c4532015-06-05 16:00:11 +0000626
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000627static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
628 uint64_t /*Address*/,
629 const void *Decoder) {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000630 using namespace Hexagon;
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000631
Craig Toppere5e035a32015-12-05 07:13:35 +0000632 static const MCPhysReg CtrlRegDecoderTable[] = {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000633 /* 0 */ SA0, LC0, SA1, LC1,
Krzysztof Parzyszeke2603322017-05-05 22:12:12 +0000634 /* 4 */ P3_0, C5, M0, M1,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000635 /* 8 */ USR, PC, UGP, GP,
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000636 /* 12 */ CS0, CS1, UPCYCLELO, UPCYCLEHI,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000637 /* 16 */ FRAMELIMIT, FRAMEKEY, PKTCOUNTLO, PKTCOUNTHI,
638 /* 20 */ 0, 0, 0, 0,
639 /* 24 */ 0, 0, 0, 0,
640 /* 28 */ 0, 0, UTIMERLO, UTIMERHI
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000641 };
642
Craig Topper6261e1b2015-12-01 06:13:06 +0000643 if (RegNo >= array_lengthof(CtrlRegDecoderTable))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000644 return MCDisassembler::Fail;
645
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000646 static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
647 if (CtrlRegDecoderTable[RegNo] == NoRegister)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000648 return MCDisassembler::Fail;
649
650 unsigned Register = CtrlRegDecoderTable[RegNo];
651 Inst.addOperand(MCOperand::createReg(Register));
652 return MCDisassembler::Success;
653}
654
655static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
656 uint64_t /*Address*/,
657 const void *Decoder) {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000658 using namespace Hexagon;
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000659
Craig Toppere5e035a32015-12-05 07:13:35 +0000660 static const MCPhysReg CtrlReg64DecoderTable[] = {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000661 /* 0 */ C1_0, 0, C3_2, 0,
662 /* 4 */ C5_4, 0, C7_6, 0,
663 /* 8 */ C9_8, 0, C11_10, 0,
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000664 /* 12 */ CS, 0, UPCYCLE, 0,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000665 /* 16 */ C17_16, 0, PKTCOUNT, 0,
666 /* 20 */ 0, 0, 0, 0,
667 /* 24 */ 0, 0, 0, 0,
668 /* 28 */ 0, 0, UTIMER, 0
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000669 };
670
Craig Topper6261e1b2015-12-01 06:13:06 +0000671 if (RegNo >= array_lengthof(CtrlReg64DecoderTable))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000672 return MCDisassembler::Fail;
673
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000674 static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
675 if (CtrlReg64DecoderTable[RegNo] == NoRegister)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000676 return MCDisassembler::Fail;
677
678 unsigned Register = CtrlReg64DecoderTable[RegNo];
679 Inst.addOperand(MCOperand::createReg(Register));
680 return MCDisassembler::Success;
681}
682
683static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
684 uint64_t /*Address*/,
685 const void *Decoder) {
686 unsigned Register = 0;
687 switch (RegNo) {
688 case 0:
689 Register = Hexagon::M0;
690 break;
691 case 1:
692 Register = Hexagon::M1;
693 break;
694 default:
695 return MCDisassembler::Fail;
696 }
697 Inst.addOperand(MCOperand::createReg(Register));
698 return MCDisassembler::Success;
699}
700
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000701static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
702 uint64_t /*Address*/,
703 const void *Decoder) {
704 HexagonDisassembler const &Disassembler = disassembler(Decoder);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000705 int64_t FullValue = fullValue(Disassembler, MI, tmp);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000706 assert(FullValue >= 0 && "Negative in unsigned decoder");
707 HexagonMCInstrInfo::addConstant(MI, FullValue, Disassembler.getContext());
708 return MCDisassembler::Success;
709}
710
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000711static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
712 uint64_t /*Address*/, const void *Decoder) {
713 HexagonDisassembler const &Disassembler = disassembler(Decoder);
714 unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
715 tmp = SignExtend64(tmp, Bits);
716 signedDecoder<32>(MI, tmp, Decoder);
717 return MCDisassembler::Success;
718}
719
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000720// custom decoder for various jump/call immediates
721static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
722 const void *Decoder) {
723 HexagonDisassembler const &Disassembler = disassembler(Decoder);
724 unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
725 // r13_2 is not extendable, so if there are no extent bits, it's r13_2
726 if (Bits == 0)
727 Bits = 15;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000728 uint64_t FullValue = fullValue(Disassembler, MI, SignExtend64(tmp, Bits));
729 uint32_t Extended = FullValue + Address;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000730 if (!Disassembler.tryAddingSymbolicOperand(MI, Extended, Address, true, 0, 4))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000731 HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
732 return MCDisassembler::Success;
733}