blob: 01862d2c0907fe5c8f22bfc23b3f675dfbc02abe [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
NAKAMURA Takumi729be142014-10-27 12:37:26 +00006//
7//===----------------------------------------------------------------------===//
8
Colin LeMahieu7cd08922015-11-09 04:07:48 +00009#define DEBUG_TYPE "hexagon-disassembler"
10
NAKAMURA Takumi729be142014-10-27 12:37:26 +000011#include "MCTargetDesc/HexagonBaseInfo.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000012#include "MCTargetDesc/HexagonMCChecker.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000013#include "MCTargetDesc/HexagonMCInstrInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "MCTargetDesc/HexagonMCTargetDesc.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000015#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/STLExtras.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000017#include "llvm/MC/MCContext.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000018#include "llvm/MC/MCDisassembler/MCDisassembler.h"
NAKAMURA Takumi729be142014-10-27 12:37:26 +000019#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCFixedLenDisassembler.h"
21#include "llvm/MC/MCInst.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000022#include "llvm/MC/MCInstrInfo.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000023#include "llvm/MC/MCRegisterInfo.h"
NAKAMURA Takumi729be142014-10-27 12:37:26 +000024#include "llvm/MC/MCSubtargetInfo.h"
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000025#include "llvm/Support/Endian.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000026#include "llvm/Support/MathExtras.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000027#include "llvm/Support/TargetRegistry.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000028#include "llvm/Support/raw_ostream.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000029#include <cassert>
30#include <cstddef>
31#include <cstdint>
32#include <memory>
NAKAMURA Takumi729be142014-10-27 12:37:26 +000033
34using namespace llvm;
Colin LeMahieu68d967d2015-05-29 14:44:13 +000035using namespace Hexagon;
NAKAMURA Takumi729be142014-10-27 12:37:26 +000036
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000037using DecodeStatus = MCDisassembler::DecodeStatus;
NAKAMURA Takumi729be142014-10-27 12:37:26 +000038
39namespace {
Eugene Zelenko82085922016-12-13 22:13:50 +000040
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000041/// Hexagon disassembler for all Hexagon platforms.
NAKAMURA Takumi729be142014-10-27 12:37:26 +000042class HexagonDisassembler : public MCDisassembler {
43public:
Colin LeMahieu7cd08922015-11-09 04:07:48 +000044 std::unique_ptr<MCInstrInfo const> const MCII;
Colin LeMahieu68d967d2015-05-29 14:44:13 +000045 std::unique_ptr<MCInst *> CurrentBundle;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000046 mutable MCInst const *CurrentExtender;
Eugene Zelenko82085922016-12-13 22:13:50 +000047
Colin LeMahieu7cd08922015-11-09 04:07:48 +000048 HexagonDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
49 MCInstrInfo const *MCII)
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000050 : MCDisassembler(STI, Ctx), MCII(MCII), CurrentBundle(new MCInst *),
51 CurrentExtender(nullptr) {}
NAKAMURA Takumi729be142014-10-27 12:37:26 +000052
Colin LeMahieu68d967d2015-05-29 14:44:13 +000053 DecodeStatus getSingleInstruction(MCInst &Instr, MCInst &MCB,
54 ArrayRef<uint8_t> Bytes, uint64_t Address,
55 raw_ostream &VStream, raw_ostream &CStream,
56 bool &Complete) const;
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000057 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +000058 ArrayRef<uint8_t> Bytes, uint64_t Address,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000059 raw_ostream &VStream,
60 raw_ostream &CStream) const override;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000061 void remapInstruction(MCInst &Instr) const;
NAKAMURA Takumi729be142014-10-27 12:37:26 +000062};
Eugene Zelenko82085922016-12-13 22:13:50 +000063
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000064static uint64_t fullValue(HexagonDisassembler const &Disassembler, MCInst &MI,
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000065 int64_t Value) {
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000066 MCInstrInfo MCII = *Disassembler.MCII;
67 if (!Disassembler.CurrentExtender ||
68 MI.size() != HexagonMCInstrInfo::getExtendableOp(MCII, MI))
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000069 return Value;
70 unsigned Alignment = HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
71 uint32_t Lower6 = static_cast<uint32_t>(Value >> Alignment) & 0x3f;
72 int64_t Bits;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000073 bool Success =
74 Disassembler.CurrentExtender->getOperand(0).getExpr()->evaluateAsAbsolute(
75 Bits);
76 assert(Success);
77 (void)Success;
78 uint64_t Upper26 = static_cast<uint64_t>(Bits);
79 uint64_t Operand = Upper26 | Lower6;
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000080 return Operand;
81}
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000082static HexagonDisassembler const &disassembler(void const *Decoder) {
83 return *static_cast<HexagonDisassembler const *>(Decoder);
84}
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000085template <size_t T>
86static void signedDecoder(MCInst &MI, unsigned tmp, const void *Decoder) {
87 HexagonDisassembler const &Disassembler = disassembler(Decoder);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000088 int64_t FullValue = fullValue(Disassembler, MI, SignExtend64<T>(tmp));
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000089 int64_t Extended = SignExtend64<32>(FullValue);
90 HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
91}
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000092}
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000093
Colin LeMahieu7cd08922015-11-09 04:07:48 +000094// Forward declare these because the auto-generated code will reference them.
95// Definitions are further down.
96
97static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
Colin LeMahieube8c4532015-06-05 16:00:11 +000098 uint64_t Address,
99 const void *Decoder);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000100static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
101 unsigned RegNo,
102 uint64_t Address,
103 const void *Decoder);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000104static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
105 uint64_t Address,
106 const void *Decoder);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000107static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000108 uint64_t Address,
109 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000110static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
111 uint64_t Address,
112 const void *Decoder);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000113static DecodeStatus
114DecodeGeneralDoubleLow8RegsRegisterClass(MCInst &Inst, unsigned RegNo,
115 uint64_t Address, const void *Decoder);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000116static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000117 uint64_t Address,
118 const void *Decoder);
Krzysztof Parzyszek13a9cf22018-12-05 20:18:09 +0000119static DecodeStatus DecodeHvxVQRRegisterClass(MCInst &Inst,
120 unsigned RegNo,
121 uint64_t Address,
122 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000123static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
124 uint64_t Address,
125 const void *Decoder);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000126static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000127 uint64_t Address,
128 const void *Decoder);
Colin LeMahieuf3db8842014-12-19 19:06:32 +0000129static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
Colin LeMahieube8c4532015-06-05 16:00:11 +0000130 uint64_t Address,
131 const void *Decoder);
Krzysztof Parzyszek22a21d42018-03-01 17:03:26 +0000132static DecodeStatus DecodeGuestRegsRegisterClass(MCInst &Inst, unsigned RegNo,
133 uint64_t Address,
134 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000135static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
136 uint64_t Address,
137 const void *Decoder);
Colin LeMahieu404d5b22015-02-10 16:59:36 +0000138static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
Colin LeMahieube8c4532015-06-05 16:00:11 +0000139 uint64_t Address,
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000140 const void *Decoder);
Krzysztof Parzyszek22a21d42018-03-01 17:03:26 +0000141static DecodeStatus DecodeGuestRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
142 uint64_t Address,
143 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000144
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000145static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
146 uint64_t Address, const void *Decoder);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000147static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
148 uint64_t /*Address*/, const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000149static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
150 const void *Decoder);
Richard Trieu8f6182f2019-01-31 21:58:42 +0000151#include "HexagonDepDecoders.inc"
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000152#include "HexagonGenDisassemblerTables.inc"
153
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000154static MCDisassembler *createHexagonDisassembler(const Target &T,
155 const MCSubtargetInfo &STI,
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000156 MCContext &Ctx) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000157 return new HexagonDisassembler(STI, Ctx, T.createMCInstrInfo());
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000158}
159
160extern "C" void LLVMInitializeHexagonDisassembler() {
Mehdi Aminif42454b2016-10-09 23:00:34 +0000161 TargetRegistry::RegisterMCDisassembler(getTheHexagonTarget(),
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000162 createHexagonDisassembler);
163}
164
165DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000166 ArrayRef<uint8_t> Bytes,
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000167 uint64_t Address,
168 raw_ostream &os,
169 raw_ostream &cs) const {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000170 DecodeStatus Result = DecodeStatus::Success;
171 bool Complete = false;
172 Size = 0;
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000173
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000174 *CurrentBundle = &MI;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000175 MI.setOpcode(Hexagon::BUNDLE);
176 MI.addOperand(MCOperand::createImm(0));
Eugene Zelenko82085922016-12-13 22:13:50 +0000177 while (Result == Success && !Complete) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000178 if (Bytes.size() < HEXAGON_INSTR_SIZE)
179 return MCDisassembler::Fail;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000180 MCInst *Inst = new (getContext()) MCInst;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000181 Result = getSingleInstruction(*Inst, MI, Bytes, Address, os, cs, Complete);
182 MI.addOperand(MCOperand::createInst(Inst));
183 Size += HEXAGON_INSTR_SIZE;
184 Bytes = Bytes.slice(HEXAGON_INSTR_SIZE);
185 }
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000186 if (Result == MCDisassembler::Fail)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000187 return Result;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000188 if (Size > HEXAGON_MAX_PACKET_SIZE)
189 return MCDisassembler::Fail;
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000190 HexagonMCChecker Checker(getContext(), *MCII, STI, MI,
191 *getContext().getRegisterInfo(), false);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000192 if (!Checker.check())
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000193 return MCDisassembler::Fail;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000194 remapInstruction(MI);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000195 return MCDisassembler::Success;
196}
197
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000198void HexagonDisassembler::remapInstruction(MCInst &Instr) const {
199 for (auto I: HexagonMCInstrInfo::bundleInstructions(Instr)) {
200 auto &MI = const_cast<MCInst &>(*I.getInst());
201 switch (MI.getOpcode()) {
202 case Hexagon::S2_allocframe:
203 if (MI.getOperand(0).getReg() == Hexagon::R29) {
204 MI.setOpcode(Hexagon::S6_allocframe_to_raw);
205 MI.erase(MI.begin () + 1);
206 MI.erase(MI.begin ());
207 }
208 break;
209 case Hexagon::L2_deallocframe:
210 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
211 MI.getOperand(1).getReg() == Hexagon::R30) {
212 MI.setOpcode(L6_deallocframe_map_to_raw);
213 MI.erase(MI.begin () + 1);
214 MI.erase(MI.begin ());
215 }
216 break;
217 case Hexagon::L4_return:
218 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
219 MI.getOperand(1).getReg() == Hexagon::R30) {
220 MI.setOpcode(L6_return_map_to_raw);
221 MI.erase(MI.begin () + 1);
222 MI.erase(MI.begin ());
223 }
224 break;
225 case Hexagon::L4_return_t:
226 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
227 MI.getOperand(2).getReg() == Hexagon::R30) {
228 MI.setOpcode(L4_return_map_to_raw_t);
229 MI.erase(MI.begin () + 2);
230 MI.erase(MI.begin ());
231 }
232 break;
233 case Hexagon::L4_return_f:
234 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
235 MI.getOperand(2).getReg() == Hexagon::R30) {
236 MI.setOpcode(L4_return_map_to_raw_f);
237 MI.erase(MI.begin () + 2);
238 MI.erase(MI.begin ());
239 }
240 break;
241 case Hexagon::L4_return_tnew_pt:
242 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
243 MI.getOperand(2).getReg() == Hexagon::R30) {
244 MI.setOpcode(L4_return_map_to_raw_tnew_pt);
245 MI.erase(MI.begin () + 2);
246 MI.erase(MI.begin ());
247 }
248 break;
249 case Hexagon::L4_return_fnew_pt:
250 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
251 MI.getOperand(2).getReg() == Hexagon::R30) {
252 MI.setOpcode(L4_return_map_to_raw_fnew_pt);
253 MI.erase(MI.begin () + 2);
254 MI.erase(MI.begin ());
255 }
256 break;
257 case Hexagon::L4_return_tnew_pnt:
258 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
259 MI.getOperand(2).getReg() == Hexagon::R30) {
260 MI.setOpcode(L4_return_map_to_raw_tnew_pnt);
261 MI.erase(MI.begin () + 2);
262 MI.erase(MI.begin ());
263 }
264 break;
265 case Hexagon::L4_return_fnew_pnt:
266 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
267 MI.getOperand(2).getReg() == Hexagon::R30) {
268 MI.setOpcode(L4_return_map_to_raw_fnew_pnt);
269 MI.erase(MI.begin () + 2);
270 MI.erase(MI.begin ());
271 }
272 break;
273 }
274 }
275}
276
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000277static void adjustDuplex(MCInst &MI, MCContext &Context) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000278 switch (MI.getOpcode()) {
279 case Hexagon::SA1_setin1:
280 MI.insert(MI.begin() + 1,
281 MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
282 break;
283 case Hexagon::SA1_dec:
284 MI.insert(MI.begin() + 2,
285 MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
286 break;
287 default:
288 break;
289 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000290}
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000291
292DecodeStatus HexagonDisassembler::getSingleInstruction(
293 MCInst &MI, MCInst &MCB, ArrayRef<uint8_t> Bytes, uint64_t Address,
294 raw_ostream &os, raw_ostream &cs, bool &Complete) const {
295 assert(Bytes.size() >= HEXAGON_INSTR_SIZE);
296
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000297 uint32_t Instruction = support::endian::read32le(Bytes.data());
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000298
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000299 auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB);
300 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
301 HexagonII::INST_PARSE_LOOP_END) {
302 if (BundleSize == 0)
303 HexagonMCInstrInfo::setInnerLoop(MCB);
304 else if (BundleSize == 1)
305 HexagonMCInstrInfo::setOuterLoop(MCB);
306 else
307 return DecodeStatus::Fail;
308 }
309
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000310 CurrentExtender = HexagonMCInstrInfo::extenderForIndex(
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000311 MCB, HexagonMCInstrInfo::bundleSize(MCB));
312
313 DecodeStatus Result = DecodeStatus::Fail;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000314 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
Colin LeMahieube8c4532015-06-05 16:00:11 +0000315 HexagonII::INST_PARSE_DUPLEX) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000316 unsigned duplexIClass;
317 uint8_t const *DecodeLow, *DecodeHigh;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000318 duplexIClass = ((Instruction >> 28) & 0xe) | ((Instruction >> 13) & 0x1);
319 switch (duplexIClass) {
320 default:
321 return MCDisassembler::Fail;
322 case 0:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000323 DecodeLow = DecoderTableSUBINSN_L132;
324 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000325 break;
326 case 1:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000327 DecodeLow = DecoderTableSUBINSN_L232;
328 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000329 break;
330 case 2:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000331 DecodeLow = DecoderTableSUBINSN_L232;
332 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000333 break;
334 case 3:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000335 DecodeLow = DecoderTableSUBINSN_A32;
336 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000337 break;
338 case 4:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000339 DecodeLow = DecoderTableSUBINSN_L132;
340 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000341 break;
342 case 5:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000343 DecodeLow = DecoderTableSUBINSN_L232;
344 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000345 break;
346 case 6:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000347 DecodeLow = DecoderTableSUBINSN_S132;
348 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000349 break;
350 case 7:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000351 DecodeLow = DecoderTableSUBINSN_S232;
352 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000353 break;
354 case 8:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000355 DecodeLow = DecoderTableSUBINSN_S132;
356 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000357 break;
358 case 9:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000359 DecodeLow = DecoderTableSUBINSN_S132;
360 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000361 break;
362 case 10:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000363 DecodeLow = DecoderTableSUBINSN_S132;
364 DecodeHigh = DecoderTableSUBINSN_S132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000365 break;
366 case 11:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000367 DecodeLow = DecoderTableSUBINSN_S232;
368 DecodeHigh = DecoderTableSUBINSN_S132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000369 break;
370 case 12:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000371 DecodeLow = DecoderTableSUBINSN_S232;
372 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000373 break;
374 case 13:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000375 DecodeLow = DecoderTableSUBINSN_S232;
376 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000377 break;
378 case 14:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000379 DecodeLow = DecoderTableSUBINSN_S232;
380 DecodeHigh = DecoderTableSUBINSN_S232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000381 break;
382 }
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000383 MI.setOpcode(Hexagon::DuplexIClass0 + duplexIClass);
Colin LeMahieube8c4532015-06-05 16:00:11 +0000384 MCInst *MILow = new (getContext()) MCInst;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000385 MCInst *MIHigh = new (getContext()) MCInst;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000386 auto TmpExtender = CurrentExtender;
387 CurrentExtender =
388 nullptr; // constant extenders in duplex must always be in slot 1
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000389 Result = decodeInstruction(DecodeLow, *MILow, Instruction & 0x1fff, Address,
390 this, STI);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000391 CurrentExtender = TmpExtender;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000392 if (Result != DecodeStatus::Success)
393 return DecodeStatus::Fail;
394 adjustDuplex(*MILow, getContext());
395 Result = decodeInstruction(
396 DecodeHigh, *MIHigh, (Instruction >> 16) & 0x1fff, Address, this, STI);
397 if (Result != DecodeStatus::Success)
398 return DecodeStatus::Fail;
399 adjustDuplex(*MIHigh, getContext());
Colin LeMahieube8c4532015-06-05 16:00:11 +0000400 MCOperand OPLow = MCOperand::createInst(MILow);
401 MCOperand OPHigh = MCOperand::createInst(MIHigh);
402 MI.addOperand(OPLow);
403 MI.addOperand(OPHigh);
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000404 Complete = true;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000405 } else {
406 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
407 HexagonII::INST_PARSE_PACKET_END)
408 Complete = true;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000409
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000410 if (CurrentExtender != nullptr)
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000411 Result = decodeInstruction(DecoderTableMustExtend32, MI, Instruction,
412 Address, this, STI);
413
414 if (Result != MCDisassembler::Success)
415 Result = decodeInstruction(DecoderTable32, MI, Instruction, Address, this,
416 STI);
417
418 if (Result != MCDisassembler::Success &&
419 STI.getFeatureBits()[Hexagon::ExtensionHVX])
420 Result = decodeInstruction(DecoderTableEXT_mmvec32, MI, Instruction,
421 Address, this, STI);
422
Colin LeMahieube8c4532015-06-05 16:00:11 +0000423 }
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000424
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000425 switch (MI.getOpcode()) {
Colin LeMahieu81707542016-12-05 04:29:00 +0000426 case Hexagon::J4_cmpeqn1_f_jumpnv_nt:
427 case Hexagon::J4_cmpeqn1_f_jumpnv_t:
428 case Hexagon::J4_cmpeqn1_fp0_jump_nt:
429 case Hexagon::J4_cmpeqn1_fp0_jump_t:
430 case Hexagon::J4_cmpeqn1_fp1_jump_nt:
431 case Hexagon::J4_cmpeqn1_fp1_jump_t:
432 case Hexagon::J4_cmpeqn1_t_jumpnv_nt:
433 case Hexagon::J4_cmpeqn1_t_jumpnv_t:
434 case Hexagon::J4_cmpeqn1_tp0_jump_nt:
435 case Hexagon::J4_cmpeqn1_tp0_jump_t:
436 case Hexagon::J4_cmpeqn1_tp1_jump_nt:
437 case Hexagon::J4_cmpeqn1_tp1_jump_t:
438 case Hexagon::J4_cmpgtn1_f_jumpnv_nt:
439 case Hexagon::J4_cmpgtn1_f_jumpnv_t:
440 case Hexagon::J4_cmpgtn1_fp0_jump_nt:
441 case Hexagon::J4_cmpgtn1_fp0_jump_t:
442 case Hexagon::J4_cmpgtn1_fp1_jump_nt:
443 case Hexagon::J4_cmpgtn1_fp1_jump_t:
444 case Hexagon::J4_cmpgtn1_t_jumpnv_nt:
445 case Hexagon::J4_cmpgtn1_t_jumpnv_t:
446 case Hexagon::J4_cmpgtn1_tp0_jump_nt:
447 case Hexagon::J4_cmpgtn1_tp0_jump_t:
448 case Hexagon::J4_cmpgtn1_tp1_jump_nt:
449 case Hexagon::J4_cmpgtn1_tp1_jump_t:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000450 MI.insert(MI.begin() + 1,
451 MCOperand::createExpr(MCConstantExpr::create(-1, getContext())));
Colin LeMahieu81707542016-12-05 04:29:00 +0000452 break;
453 default:
454 break;
455 }
456
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000457 if (HexagonMCInstrInfo::isNewValue(*MCII, MI)) {
458 unsigned OpIndex = HexagonMCInstrInfo::getNewValueOp(*MCII, MI);
459 MCOperand &MCO = MI.getOperand(OpIndex);
460 assert(MCO.isReg() && "New value consumers must be registers");
461 unsigned Register =
462 getContext().getRegisterInfo()->getEncodingValue(MCO.getReg());
463 if ((Register & 0x6) == 0)
464 // HexagonPRM 10.11 Bit 1-2 == 0 is reserved
465 return MCDisassembler::Fail;
466 unsigned Lookback = (Register & 0x6) >> 1;
467 unsigned Offset = 1;
468 bool Vector = HexagonMCInstrInfo::isVector(*MCII, MI);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000469 bool PrevVector = false;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000470 auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
471 auto i = Instructions.end() - 1;
472 for (auto n = Instructions.begin() - 1;; --i, ++Offset) {
473 if (i == n)
474 // Couldn't find producer
475 return MCDisassembler::Fail;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000476 bool CurrentVector = HexagonMCInstrInfo::isVector(*MCII, *i->getInst());
477 if (Vector && !CurrentVector)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000478 // Skip scalars when calculating distances for vectors
479 ++Lookback;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000480 if (HexagonMCInstrInfo::isImmext(*i->getInst()) && (Vector == PrevVector))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000481 ++Lookback;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000482 PrevVector = CurrentVector;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000483 if (Offset == Lookback)
484 break;
485 }
486 auto const &Inst = *i->getInst();
487 bool SubregBit = (Register & 0x1) != 0;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000488 if (HexagonMCInstrInfo::hasNewValue2(*MCII, Inst)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000489 // If subreg bit is set we're selecting the second produced newvalue
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000490 unsigned Producer = SubregBit ?
491 HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg() :
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000492 HexagonMCInstrInfo::getNewValueOperand2(*MCII, Inst).getReg();
493 assert(Producer != Hexagon::NoRegister);
494 MCO.setReg(Producer);
495 } else if (HexagonMCInstrInfo::hasNewValue(*MCII, Inst)) {
496 unsigned Producer =
497 HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg();
498 if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
499 Producer = ((Producer - Hexagon::W0) << 1) + SubregBit + Hexagon::V0;
500 else if (SubregBit)
Colin LeMahieu2d497a02016-03-01 22:05:03 +0000501 // Hexagon PRM 10.11 New-value operands
502 // Nt[0] is reserved and should always be encoded as zero.
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000503 return MCDisassembler::Fail;
504 assert(Producer != Hexagon::NoRegister);
505 MCO.setReg(Producer);
506 } else
507 return MCDisassembler::Fail;
508 }
509
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000510 if (CurrentExtender != nullptr) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000511 MCInst const &Inst = HexagonMCInstrInfo::isDuplex(*MCII, MI)
512 ? *MI.getOperand(1).getInst()
513 : MI;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000514 if (!HexagonMCInstrInfo::isExtendable(*MCII, Inst) &&
515 !HexagonMCInstrInfo::isExtended(*MCII, Inst))
516 return MCDisassembler::Fail;
517 }
Colin LeMahieu5d6f03b2014-12-04 03:41:21 +0000518 return Result;
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000519}
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000520
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000521static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,
Craig Toppere5e035a32015-12-05 07:13:35 +0000522 ArrayRef<MCPhysReg> Table) {
Craig Topper3da000c2015-12-01 06:13:04 +0000523 if (RegNo < Table.size()) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000524 Inst.addOperand(MCOperand::createReg(Table[RegNo]));
525 return MCDisassembler::Success;
Craig Topper3da000c2015-12-01 06:13:04 +0000526 }
527
528 return MCDisassembler::Fail;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000529}
530
Colin LeMahieu7c958712015-10-17 01:33:04 +0000531static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
532 uint64_t Address,
533 const void *Decoder) {
534 return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
535}
536
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000537static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
538 uint64_t Address,
539 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000540 static const MCPhysReg IntRegDecoderTable[] = {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000541 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
542 Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9,
543 Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
544 Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
545 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
546 Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
547 Hexagon::R30, Hexagon::R31};
548
Craig Toppere5e035a32015-12-05 07:13:35 +0000549 return DecodeRegisterClass(Inst, RegNo, IntRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000550}
Colin LeMahieu7c958712015-10-17 01:33:04 +0000551
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000552static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
553 unsigned RegNo,
554 uint64_t Address,
555 const void *Decoder) {
556 static const MCPhysReg GeneralSubRegDecoderTable[] = {
557 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3,
558 Hexagon::R4, Hexagon::R5, Hexagon::R6, Hexagon::R7,
559 Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
560 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23,
561 };
562
563 return DecodeRegisterClass(Inst, RegNo, GeneralSubRegDecoderTable);
564}
565
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000566static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,
567 uint64_t /*Address*/,
568 const void *Decoder) {
569 static const MCPhysReg HvxVRDecoderTable[] = {
Colin LeMahieu7c958712015-10-17 01:33:04 +0000570 Hexagon::V0, Hexagon::V1, Hexagon::V2, Hexagon::V3, Hexagon::V4,
571 Hexagon::V5, Hexagon::V6, Hexagon::V7, Hexagon::V8, Hexagon::V9,
572 Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
573 Hexagon::V15, Hexagon::V16, Hexagon::V17, Hexagon::V18, Hexagon::V19,
574 Hexagon::V20, Hexagon::V21, Hexagon::V22, Hexagon::V23, Hexagon::V24,
575 Hexagon::V25, Hexagon::V26, Hexagon::V27, Hexagon::V28, Hexagon::V29,
576 Hexagon::V30, Hexagon::V31};
577
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000578 return DecodeRegisterClass(Inst, RegNo, HvxVRDecoderTable);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000579}
580
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000581static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
582 uint64_t /*Address*/,
583 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000584 static const MCPhysReg DoubleRegDecoderTable[] = {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000585 Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,
586 Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7,
587 Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11,
588 Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15};
589
Craig Toppere5e035a32015-12-05 07:13:35 +0000590 return DecodeRegisterClass(Inst, RegNo >> 1, DoubleRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000591}
592
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000593static DecodeStatus DecodeGeneralDoubleLow8RegsRegisterClass(
594 MCInst &Inst, unsigned RegNo, uint64_t /*Address*/, const void *Decoder) {
595 static const MCPhysReg GeneralDoubleLow8RegDecoderTable[] = {
596 Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,
597 Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11};
598
599 return DecodeRegisterClass(Inst, RegNo, GeneralDoubleLow8RegDecoderTable);
600}
601
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000602static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,
603 uint64_t /*Address*/,
604 const void *Decoder) {
605 static const MCPhysReg HvxWRDecoderTable[] = {
Colin LeMahieu7c958712015-10-17 01:33:04 +0000606 Hexagon::W0, Hexagon::W1, Hexagon::W2, Hexagon::W3,
607 Hexagon::W4, Hexagon::W5, Hexagon::W6, Hexagon::W7,
608 Hexagon::W8, Hexagon::W9, Hexagon::W10, Hexagon::W11,
609 Hexagon::W12, Hexagon::W13, Hexagon::W14, Hexagon::W15};
610
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000611 return (DecodeRegisterClass(Inst, RegNo >> 1, HvxWRDecoderTable));
Colin LeMahieu7c958712015-10-17 01:33:04 +0000612}
613
Krzysztof Parzyszek13a9cf22018-12-05 20:18:09 +0000614LLVM_ATTRIBUTE_UNUSED // Suppress warning temporarily.
615static DecodeStatus DecodeHvxVQRRegisterClass(MCInst &Inst,
616 unsigned RegNo,
617 uint64_t /*Address*/,
618 const void *Decoder) {
619 static const MCPhysReg HvxVQRDecoderTable[] = {
620 Hexagon::VQ0, Hexagon::VQ1, Hexagon::VQ2, Hexagon::VQ3,
621 Hexagon::VQ4, Hexagon::VQ5, Hexagon::VQ6, Hexagon::VQ7};
622
623 return DecodeRegisterClass(Inst, RegNo >> 2, HvxVQRDecoderTable);
624}
625
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000626static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
627 uint64_t /*Address*/,
628 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000629 static const MCPhysReg PredRegDecoderTable[] = {Hexagon::P0, Hexagon::P1,
630 Hexagon::P2, Hexagon::P3};
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000631
Craig Toppere5e035a32015-12-05 07:13:35 +0000632 return DecodeRegisterClass(Inst, RegNo, PredRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000633}
634
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000635static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,
636 uint64_t /*Address*/,
637 const void *Decoder) {
638 static const MCPhysReg HvxQRDecoderTable[] = {Hexagon::Q0, Hexagon::Q1,
639 Hexagon::Q2, Hexagon::Q3};
Colin LeMahieu7c958712015-10-17 01:33:04 +0000640
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000641 return DecodeRegisterClass(Inst, RegNo, HvxQRDecoderTable);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000642}
Colin LeMahieube8c4532015-06-05 16:00:11 +0000643
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000644static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
645 uint64_t /*Address*/,
646 const void *Decoder) {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000647 using namespace Hexagon;
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000648
Craig Toppere5e035a32015-12-05 07:13:35 +0000649 static const MCPhysReg CtrlRegDecoderTable[] = {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000650 /* 0 */ SA0, LC0, SA1, LC1,
Krzysztof Parzyszeke2603322017-05-05 22:12:12 +0000651 /* 4 */ P3_0, C5, M0, M1,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000652 /* 8 */ USR, PC, UGP, GP,
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000653 /* 12 */ CS0, CS1, UPCYCLELO, UPCYCLEHI,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000654 /* 16 */ FRAMELIMIT, FRAMEKEY, PKTCOUNTLO, PKTCOUNTHI,
655 /* 20 */ 0, 0, 0, 0,
656 /* 24 */ 0, 0, 0, 0,
657 /* 28 */ 0, 0, UTIMERLO, UTIMERHI
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000658 };
659
Craig Topper6261e1b2015-12-01 06:13:06 +0000660 if (RegNo >= array_lengthof(CtrlRegDecoderTable))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000661 return MCDisassembler::Fail;
662
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000663 static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
664 if (CtrlRegDecoderTable[RegNo] == NoRegister)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000665 return MCDisassembler::Fail;
666
667 unsigned Register = CtrlRegDecoderTable[RegNo];
668 Inst.addOperand(MCOperand::createReg(Register));
669 return MCDisassembler::Success;
670}
671
672static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
673 uint64_t /*Address*/,
674 const void *Decoder) {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000675 using namespace Hexagon;
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000676
Craig Toppere5e035a32015-12-05 07:13:35 +0000677 static const MCPhysReg CtrlReg64DecoderTable[] = {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000678 /* 0 */ C1_0, 0, C3_2, 0,
679 /* 4 */ C5_4, 0, C7_6, 0,
680 /* 8 */ C9_8, 0, C11_10, 0,
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000681 /* 12 */ CS, 0, UPCYCLE, 0,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000682 /* 16 */ C17_16, 0, PKTCOUNT, 0,
683 /* 20 */ 0, 0, 0, 0,
684 /* 24 */ 0, 0, 0, 0,
685 /* 28 */ 0, 0, UTIMER, 0
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000686 };
687
Craig Topper6261e1b2015-12-01 06:13:06 +0000688 if (RegNo >= array_lengthof(CtrlReg64DecoderTable))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000689 return MCDisassembler::Fail;
690
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000691 static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
692 if (CtrlReg64DecoderTable[RegNo] == NoRegister)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000693 return MCDisassembler::Fail;
694
695 unsigned Register = CtrlReg64DecoderTable[RegNo];
696 Inst.addOperand(MCOperand::createReg(Register));
697 return MCDisassembler::Success;
698}
699
700static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
701 uint64_t /*Address*/,
702 const void *Decoder) {
703 unsigned Register = 0;
704 switch (RegNo) {
705 case 0:
706 Register = Hexagon::M0;
707 break;
708 case 1:
709 Register = Hexagon::M1;
710 break;
711 default:
712 return MCDisassembler::Fail;
713 }
714 Inst.addOperand(MCOperand::createReg(Register));
715 return MCDisassembler::Success;
716}
717
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000718static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
719 uint64_t /*Address*/,
720 const void *Decoder) {
721 HexagonDisassembler const &Disassembler = disassembler(Decoder);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000722 int64_t FullValue = fullValue(Disassembler, MI, tmp);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000723 assert(FullValue >= 0 && "Negative in unsigned decoder");
724 HexagonMCInstrInfo::addConstant(MI, FullValue, Disassembler.getContext());
725 return MCDisassembler::Success;
726}
727
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000728static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
729 uint64_t /*Address*/, const void *Decoder) {
730 HexagonDisassembler const &Disassembler = disassembler(Decoder);
731 unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
732 tmp = SignExtend64(tmp, Bits);
733 signedDecoder<32>(MI, tmp, Decoder);
734 return MCDisassembler::Success;
735}
736
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000737// custom decoder for various jump/call immediates
738static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
739 const void *Decoder) {
740 HexagonDisassembler const &Disassembler = disassembler(Decoder);
741 unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
742 // r13_2 is not extendable, so if there are no extent bits, it's r13_2
743 if (Bits == 0)
744 Bits = 15;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000745 uint64_t FullValue = fullValue(Disassembler, MI, SignExtend64(tmp, Bits));
746 uint32_t Extended = FullValue + Address;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000747 if (!Disassembler.tryAddingSymbolicOperand(MI, Extended, Address, true, 0, 4))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000748 HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
749 return MCDisassembler::Success;
750}
Krzysztof Parzyszek22a21d42018-03-01 17:03:26 +0000751
752static DecodeStatus DecodeGuestRegsRegisterClass(MCInst &Inst, unsigned RegNo,
753 uint64_t /*Address*/,
754 const void *Decoder) {
755 using namespace Hexagon;
756
757 static const MCPhysReg GuestRegDecoderTable[] = {
758 /* 0 */ GELR, GSR, GOSP, G3,
759 /* 4 */ G4, G5, G6, G7,
760 /* 8 */ G8, G9, G10, G11,
761 /* 12 */ G12, G13, G14, G15,
762 /* 16 */ GPMUCNT4, GPMUCNT5, GPMUCNT6, GPMUCNT7,
763 /* 20 */ G20, G21, G22, G23,
764 /* 24 */ GPCYCLELO, GPCYCLEHI, GPMUCNT0, GPMUCNT1,
765 /* 28 */ GPMUCNT2, GPMUCNT3, G30, G31
766 };
767
768 if (RegNo >= array_lengthof(GuestRegDecoderTable))
769 return MCDisassembler::Fail;
770 if (GuestRegDecoderTable[RegNo] == Hexagon::NoRegister)
771 return MCDisassembler::Fail;
772
773 unsigned Register = GuestRegDecoderTable[RegNo];
774 Inst.addOperand(MCOperand::createReg(Register));
775 return MCDisassembler::Success;
776}
777
778static DecodeStatus DecodeGuestRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
779 uint64_t /*Address*/,
780 const void *Decoder) {
781 using namespace Hexagon;
782
783 static const MCPhysReg GuestReg64DecoderTable[] = {
784 /* 0 */ G1_0, 0, G3_2, 0,
785 /* 4 */ G5_4, 0, G7_6, 0,
786 /* 8 */ G9_8, 0, G11_10, 0,
787 /* 12 */ G13_12, 0, G15_14, 0,
788 /* 16 */ G17_16, 0, G19_18, 0,
789 /* 20 */ G21_20, 0, G23_22, 0,
790 /* 24 */ G25_24, 0, G27_26, 0,
791 /* 28 */ G29_28, 0, G31_30, 0
792 };
793
794 if (RegNo >= array_lengthof(GuestReg64DecoderTable))
795 return MCDisassembler::Fail;
796 if (GuestReg64DecoderTable[RegNo] == Hexagon::NoRegister)
797 return MCDisassembler::Fail;
798
799 unsigned Register = GuestReg64DecoderTable[RegNo];
800 Inst.addOperand(MCOperand::createReg(Register));
801 return MCDisassembler::Success;
802}