blob: 99e3ee8715708f89c32c0198553d035bd483bdef [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"
Richard Trieu7ef17292019-05-14 23:04:55 +000015#include "TargetInfo/HexagonTargetInfo.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000016#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/STLExtras.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000018#include "llvm/MC/MCContext.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "llvm/MC/MCDisassembler/MCDisassembler.h"
NAKAMURA Takumi729be142014-10-27 12:37:26 +000020#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCFixedLenDisassembler.h"
22#include "llvm/MC/MCInst.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000023#include "llvm/MC/MCInstrInfo.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000024#include "llvm/MC/MCRegisterInfo.h"
NAKAMURA Takumi729be142014-10-27 12:37:26 +000025#include "llvm/MC/MCSubtargetInfo.h"
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000026#include "llvm/Support/Endian.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000027#include "llvm/Support/MathExtras.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000028#include "llvm/Support/TargetRegistry.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000029#include "llvm/Support/raw_ostream.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000030#include <cassert>
31#include <cstddef>
32#include <cstdint>
33#include <memory>
NAKAMURA Takumi729be142014-10-27 12:37:26 +000034
35using namespace llvm;
Colin LeMahieu68d967d2015-05-29 14:44:13 +000036using namespace Hexagon;
NAKAMURA Takumi729be142014-10-27 12:37:26 +000037
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000038using DecodeStatus = MCDisassembler::DecodeStatus;
NAKAMURA Takumi729be142014-10-27 12:37:26 +000039
40namespace {
Eugene Zelenko82085922016-12-13 22:13:50 +000041
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000042/// Hexagon disassembler for all Hexagon platforms.
NAKAMURA Takumi729be142014-10-27 12:37:26 +000043class HexagonDisassembler : public MCDisassembler {
44public:
Colin LeMahieu7cd08922015-11-09 04:07:48 +000045 std::unique_ptr<MCInstrInfo const> const MCII;
Colin LeMahieu68d967d2015-05-29 14:44:13 +000046 std::unique_ptr<MCInst *> CurrentBundle;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000047 mutable MCInst const *CurrentExtender;
Eugene Zelenko82085922016-12-13 22:13:50 +000048
Colin LeMahieu7cd08922015-11-09 04:07:48 +000049 HexagonDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
50 MCInstrInfo const *MCII)
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000051 : MCDisassembler(STI, Ctx), MCII(MCII), CurrentBundle(new MCInst *),
52 CurrentExtender(nullptr) {}
NAKAMURA Takumi729be142014-10-27 12:37:26 +000053
Colin LeMahieu68d967d2015-05-29 14:44:13 +000054 DecodeStatus getSingleInstruction(MCInst &Instr, MCInst &MCB,
55 ArrayRef<uint8_t> Bytes, uint64_t Address,
56 raw_ostream &VStream, raw_ostream &CStream,
57 bool &Complete) const;
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000058 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +000059 ArrayRef<uint8_t> Bytes, uint64_t Address,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000060 raw_ostream &VStream,
61 raw_ostream &CStream) const override;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000062 void remapInstruction(MCInst &Instr) const;
NAKAMURA Takumi729be142014-10-27 12:37:26 +000063};
Eugene Zelenko82085922016-12-13 22:13:50 +000064
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000065static uint64_t fullValue(HexagonDisassembler const &Disassembler, MCInst &MI,
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000066 int64_t Value) {
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000067 MCInstrInfo MCII = *Disassembler.MCII;
68 if (!Disassembler.CurrentExtender ||
69 MI.size() != HexagonMCInstrInfo::getExtendableOp(MCII, MI))
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000070 return Value;
71 unsigned Alignment = HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
72 uint32_t Lower6 = static_cast<uint32_t>(Value >> Alignment) & 0x3f;
73 int64_t Bits;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000074 bool Success =
75 Disassembler.CurrentExtender->getOperand(0).getExpr()->evaluateAsAbsolute(
76 Bits);
77 assert(Success);
78 (void)Success;
79 uint64_t Upper26 = static_cast<uint64_t>(Bits);
80 uint64_t Operand = Upper26 | Lower6;
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000081 return Operand;
82}
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000083static HexagonDisassembler const &disassembler(void const *Decoder) {
84 return *static_cast<HexagonDisassembler const *>(Decoder);
85}
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000086template <size_t T>
87static void signedDecoder(MCInst &MI, unsigned tmp, const void *Decoder) {
88 HexagonDisassembler const &Disassembler = disassembler(Decoder);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000089 int64_t FullValue = fullValue(Disassembler, MI, SignExtend64<T>(tmp));
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000090 int64_t Extended = SignExtend64<32>(FullValue);
91 HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
92}
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000093}
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000094
Colin LeMahieu7cd08922015-11-09 04:07:48 +000095// Forward declare these because the auto-generated code will reference them.
96// Definitions are further down.
97
98static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
Colin LeMahieube8c4532015-06-05 16:00:11 +000099 uint64_t Address,
100 const void *Decoder);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000101static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
102 unsigned RegNo,
103 uint64_t Address,
104 const void *Decoder);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000105static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
106 uint64_t Address,
107 const void *Decoder);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000108static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000109 uint64_t Address,
110 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000111static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
112 uint64_t Address,
113 const void *Decoder);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000114static DecodeStatus
115DecodeGeneralDoubleLow8RegsRegisterClass(MCInst &Inst, unsigned RegNo,
116 uint64_t Address, const void *Decoder);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000117static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000118 uint64_t Address,
119 const void *Decoder);
Krzysztof Parzyszek13a9cf22018-12-05 20:18:09 +0000120static DecodeStatus DecodeHvxVQRRegisterClass(MCInst &Inst,
121 unsigned RegNo,
122 uint64_t Address,
123 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000124static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
125 uint64_t Address,
126 const void *Decoder);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000127static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000128 uint64_t Address,
129 const void *Decoder);
Colin LeMahieuf3db8842014-12-19 19:06:32 +0000130static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
Colin LeMahieube8c4532015-06-05 16:00:11 +0000131 uint64_t Address,
132 const void *Decoder);
Krzysztof Parzyszek22a21d42018-03-01 17:03:26 +0000133static DecodeStatus DecodeGuestRegsRegisterClass(MCInst &Inst, unsigned RegNo,
134 uint64_t Address,
135 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000136static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
137 uint64_t Address,
138 const void *Decoder);
Colin LeMahieu404d5b22015-02-10 16:59:36 +0000139static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
Colin LeMahieube8c4532015-06-05 16:00:11 +0000140 uint64_t Address,
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000141 const void *Decoder);
Krzysztof Parzyszek22a21d42018-03-01 17:03:26 +0000142static DecodeStatus DecodeGuestRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
143 uint64_t Address,
144 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000145
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000146static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
147 uint64_t Address, const void *Decoder);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000148static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
149 uint64_t /*Address*/, const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000150static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
151 const void *Decoder);
Richard Trieu8f6182f2019-01-31 21:58:42 +0000152#include "HexagonDepDecoders.inc"
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000153#include "HexagonGenDisassemblerTables.inc"
154
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000155static MCDisassembler *createHexagonDisassembler(const Target &T,
156 const MCSubtargetInfo &STI,
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000157 MCContext &Ctx) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000158 return new HexagonDisassembler(STI, Ctx, T.createMCInstrInfo());
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000159}
160
Tom Stellard4b0b2612019-06-11 03:21:13 +0000161extern "C" void LLVMInitializeHexagonDisassembler() {
Mehdi Aminif42454b2016-10-09 23:00:34 +0000162 TargetRegistry::RegisterMCDisassembler(getTheHexagonTarget(),
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000163 createHexagonDisassembler);
164}
165
166DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000167 ArrayRef<uint8_t> Bytes,
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000168 uint64_t Address,
169 raw_ostream &os,
170 raw_ostream &cs) const {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000171 DecodeStatus Result = DecodeStatus::Success;
172 bool Complete = false;
173 Size = 0;
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000174
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000175 *CurrentBundle = &MI;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000176 MI.setOpcode(Hexagon::BUNDLE);
177 MI.addOperand(MCOperand::createImm(0));
Eugene Zelenko82085922016-12-13 22:13:50 +0000178 while (Result == Success && !Complete) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000179 if (Bytes.size() < HEXAGON_INSTR_SIZE)
180 return MCDisassembler::Fail;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000181 MCInst *Inst = new (getContext()) MCInst;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000182 Result = getSingleInstruction(*Inst, MI, Bytes, Address, os, cs, Complete);
183 MI.addOperand(MCOperand::createInst(Inst));
184 Size += HEXAGON_INSTR_SIZE;
185 Bytes = Bytes.slice(HEXAGON_INSTR_SIZE);
186 }
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000187 if (Result == MCDisassembler::Fail)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000188 return Result;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000189 if (Size > HEXAGON_MAX_PACKET_SIZE)
190 return MCDisassembler::Fail;
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000191 HexagonMCChecker Checker(getContext(), *MCII, STI, MI,
192 *getContext().getRegisterInfo(), false);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000193 if (!Checker.check())
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000194 return MCDisassembler::Fail;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000195 remapInstruction(MI);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000196 return MCDisassembler::Success;
197}
198
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000199void HexagonDisassembler::remapInstruction(MCInst &Instr) const {
200 for (auto I: HexagonMCInstrInfo::bundleInstructions(Instr)) {
201 auto &MI = const_cast<MCInst &>(*I.getInst());
202 switch (MI.getOpcode()) {
203 case Hexagon::S2_allocframe:
204 if (MI.getOperand(0).getReg() == Hexagon::R29) {
205 MI.setOpcode(Hexagon::S6_allocframe_to_raw);
206 MI.erase(MI.begin () + 1);
207 MI.erase(MI.begin ());
208 }
209 break;
210 case Hexagon::L2_deallocframe:
211 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
212 MI.getOperand(1).getReg() == Hexagon::R30) {
213 MI.setOpcode(L6_deallocframe_map_to_raw);
214 MI.erase(MI.begin () + 1);
215 MI.erase(MI.begin ());
216 }
217 break;
218 case Hexagon::L4_return:
219 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
220 MI.getOperand(1).getReg() == Hexagon::R30) {
221 MI.setOpcode(L6_return_map_to_raw);
222 MI.erase(MI.begin () + 1);
223 MI.erase(MI.begin ());
224 }
225 break;
226 case Hexagon::L4_return_t:
227 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
228 MI.getOperand(2).getReg() == Hexagon::R30) {
229 MI.setOpcode(L4_return_map_to_raw_t);
230 MI.erase(MI.begin () + 2);
231 MI.erase(MI.begin ());
232 }
233 break;
234 case Hexagon::L4_return_f:
235 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
236 MI.getOperand(2).getReg() == Hexagon::R30) {
237 MI.setOpcode(L4_return_map_to_raw_f);
238 MI.erase(MI.begin () + 2);
239 MI.erase(MI.begin ());
240 }
241 break;
242 case Hexagon::L4_return_tnew_pt:
243 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
244 MI.getOperand(2).getReg() == Hexagon::R30) {
245 MI.setOpcode(L4_return_map_to_raw_tnew_pt);
246 MI.erase(MI.begin () + 2);
247 MI.erase(MI.begin ());
248 }
249 break;
250 case Hexagon::L4_return_fnew_pt:
251 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
252 MI.getOperand(2).getReg() == Hexagon::R30) {
253 MI.setOpcode(L4_return_map_to_raw_fnew_pt);
254 MI.erase(MI.begin () + 2);
255 MI.erase(MI.begin ());
256 }
257 break;
258 case Hexagon::L4_return_tnew_pnt:
259 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
260 MI.getOperand(2).getReg() == Hexagon::R30) {
261 MI.setOpcode(L4_return_map_to_raw_tnew_pnt);
262 MI.erase(MI.begin () + 2);
263 MI.erase(MI.begin ());
264 }
265 break;
266 case Hexagon::L4_return_fnew_pnt:
267 if (MI.getOperand(0).getReg() == Hexagon::D15 &&
268 MI.getOperand(2).getReg() == Hexagon::R30) {
269 MI.setOpcode(L4_return_map_to_raw_fnew_pnt);
270 MI.erase(MI.begin () + 2);
271 MI.erase(MI.begin ());
272 }
273 break;
274 }
275 }
276}
277
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000278static void adjustDuplex(MCInst &MI, MCContext &Context) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000279 switch (MI.getOpcode()) {
280 case Hexagon::SA1_setin1:
281 MI.insert(MI.begin() + 1,
282 MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
283 break;
284 case Hexagon::SA1_dec:
285 MI.insert(MI.begin() + 2,
286 MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
287 break;
288 default:
289 break;
290 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000291}
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000292
293DecodeStatus HexagonDisassembler::getSingleInstruction(
294 MCInst &MI, MCInst &MCB, ArrayRef<uint8_t> Bytes, uint64_t Address,
295 raw_ostream &os, raw_ostream &cs, bool &Complete) const {
296 assert(Bytes.size() >= HEXAGON_INSTR_SIZE);
297
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000298 uint32_t Instruction = support::endian::read32le(Bytes.data());
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000299
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000300 auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB);
301 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
302 HexagonII::INST_PARSE_LOOP_END) {
303 if (BundleSize == 0)
304 HexagonMCInstrInfo::setInnerLoop(MCB);
305 else if (BundleSize == 1)
306 HexagonMCInstrInfo::setOuterLoop(MCB);
307 else
308 return DecodeStatus::Fail;
309 }
310
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000311 CurrentExtender = HexagonMCInstrInfo::extenderForIndex(
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000312 MCB, HexagonMCInstrInfo::bundleSize(MCB));
313
314 DecodeStatus Result = DecodeStatus::Fail;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000315 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
Colin LeMahieube8c4532015-06-05 16:00:11 +0000316 HexagonII::INST_PARSE_DUPLEX) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000317 unsigned duplexIClass;
318 uint8_t const *DecodeLow, *DecodeHigh;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000319 duplexIClass = ((Instruction >> 28) & 0xe) | ((Instruction >> 13) & 0x1);
320 switch (duplexIClass) {
321 default:
322 return MCDisassembler::Fail;
323 case 0:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000324 DecodeLow = DecoderTableSUBINSN_L132;
325 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000326 break;
327 case 1:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000328 DecodeLow = DecoderTableSUBINSN_L232;
329 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000330 break;
331 case 2:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000332 DecodeLow = DecoderTableSUBINSN_L232;
333 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000334 break;
335 case 3:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000336 DecodeLow = DecoderTableSUBINSN_A32;
337 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000338 break;
339 case 4:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000340 DecodeLow = DecoderTableSUBINSN_L132;
341 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000342 break;
343 case 5:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000344 DecodeLow = DecoderTableSUBINSN_L232;
345 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000346 break;
347 case 6:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000348 DecodeLow = DecoderTableSUBINSN_S132;
349 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000350 break;
351 case 7:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000352 DecodeLow = DecoderTableSUBINSN_S232;
353 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000354 break;
355 case 8:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000356 DecodeLow = DecoderTableSUBINSN_S132;
357 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000358 break;
359 case 9:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000360 DecodeLow = DecoderTableSUBINSN_S132;
361 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000362 break;
363 case 10:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000364 DecodeLow = DecoderTableSUBINSN_S132;
365 DecodeHigh = DecoderTableSUBINSN_S132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000366 break;
367 case 11:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000368 DecodeLow = DecoderTableSUBINSN_S232;
369 DecodeHigh = DecoderTableSUBINSN_S132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000370 break;
371 case 12:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000372 DecodeLow = DecoderTableSUBINSN_S232;
373 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000374 break;
375 case 13:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000376 DecodeLow = DecoderTableSUBINSN_S232;
377 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000378 break;
379 case 14:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000380 DecodeLow = DecoderTableSUBINSN_S232;
381 DecodeHigh = DecoderTableSUBINSN_S232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000382 break;
383 }
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000384 MI.setOpcode(Hexagon::DuplexIClass0 + duplexIClass);
Colin LeMahieube8c4532015-06-05 16:00:11 +0000385 MCInst *MILow = new (getContext()) MCInst;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000386 MCInst *MIHigh = new (getContext()) MCInst;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000387 auto TmpExtender = CurrentExtender;
388 CurrentExtender =
389 nullptr; // constant extenders in duplex must always be in slot 1
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000390 Result = decodeInstruction(DecodeLow, *MILow, Instruction & 0x1fff, Address,
391 this, STI);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000392 CurrentExtender = TmpExtender;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000393 if (Result != DecodeStatus::Success)
394 return DecodeStatus::Fail;
395 adjustDuplex(*MILow, getContext());
396 Result = decodeInstruction(
397 DecodeHigh, *MIHigh, (Instruction >> 16) & 0x1fff, Address, this, STI);
398 if (Result != DecodeStatus::Success)
399 return DecodeStatus::Fail;
400 adjustDuplex(*MIHigh, getContext());
Colin LeMahieube8c4532015-06-05 16:00:11 +0000401 MCOperand OPLow = MCOperand::createInst(MILow);
402 MCOperand OPHigh = MCOperand::createInst(MIHigh);
403 MI.addOperand(OPLow);
404 MI.addOperand(OPHigh);
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000405 Complete = true;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000406 } else {
407 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
408 HexagonII::INST_PARSE_PACKET_END)
409 Complete = true;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000410
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000411 if (CurrentExtender != nullptr)
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000412 Result = decodeInstruction(DecoderTableMustExtend32, MI, Instruction,
413 Address, this, STI);
414
415 if (Result != MCDisassembler::Success)
416 Result = decodeInstruction(DecoderTable32, MI, Instruction, Address, this,
417 STI);
418
419 if (Result != MCDisassembler::Success &&
420 STI.getFeatureBits()[Hexagon::ExtensionHVX])
421 Result = decodeInstruction(DecoderTableEXT_mmvec32, MI, Instruction,
422 Address, this, STI);
423
Colin LeMahieube8c4532015-06-05 16:00:11 +0000424 }
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000425
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000426 switch (MI.getOpcode()) {
Colin LeMahieu81707542016-12-05 04:29:00 +0000427 case Hexagon::J4_cmpeqn1_f_jumpnv_nt:
428 case Hexagon::J4_cmpeqn1_f_jumpnv_t:
429 case Hexagon::J4_cmpeqn1_fp0_jump_nt:
430 case Hexagon::J4_cmpeqn1_fp0_jump_t:
431 case Hexagon::J4_cmpeqn1_fp1_jump_nt:
432 case Hexagon::J4_cmpeqn1_fp1_jump_t:
433 case Hexagon::J4_cmpeqn1_t_jumpnv_nt:
434 case Hexagon::J4_cmpeqn1_t_jumpnv_t:
435 case Hexagon::J4_cmpeqn1_tp0_jump_nt:
436 case Hexagon::J4_cmpeqn1_tp0_jump_t:
437 case Hexagon::J4_cmpeqn1_tp1_jump_nt:
438 case Hexagon::J4_cmpeqn1_tp1_jump_t:
439 case Hexagon::J4_cmpgtn1_f_jumpnv_nt:
440 case Hexagon::J4_cmpgtn1_f_jumpnv_t:
441 case Hexagon::J4_cmpgtn1_fp0_jump_nt:
442 case Hexagon::J4_cmpgtn1_fp0_jump_t:
443 case Hexagon::J4_cmpgtn1_fp1_jump_nt:
444 case Hexagon::J4_cmpgtn1_fp1_jump_t:
445 case Hexagon::J4_cmpgtn1_t_jumpnv_nt:
446 case Hexagon::J4_cmpgtn1_t_jumpnv_t:
447 case Hexagon::J4_cmpgtn1_tp0_jump_nt:
448 case Hexagon::J4_cmpgtn1_tp0_jump_t:
449 case Hexagon::J4_cmpgtn1_tp1_jump_nt:
450 case Hexagon::J4_cmpgtn1_tp1_jump_t:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000451 MI.insert(MI.begin() + 1,
452 MCOperand::createExpr(MCConstantExpr::create(-1, getContext())));
Colin LeMahieu81707542016-12-05 04:29:00 +0000453 break;
454 default:
455 break;
456 }
457
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000458 if (HexagonMCInstrInfo::isNewValue(*MCII, MI)) {
459 unsigned OpIndex = HexagonMCInstrInfo::getNewValueOp(*MCII, MI);
460 MCOperand &MCO = MI.getOperand(OpIndex);
461 assert(MCO.isReg() && "New value consumers must be registers");
462 unsigned Register =
463 getContext().getRegisterInfo()->getEncodingValue(MCO.getReg());
464 if ((Register & 0x6) == 0)
465 // HexagonPRM 10.11 Bit 1-2 == 0 is reserved
466 return MCDisassembler::Fail;
467 unsigned Lookback = (Register & 0x6) >> 1;
468 unsigned Offset = 1;
469 bool Vector = HexagonMCInstrInfo::isVector(*MCII, MI);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000470 bool PrevVector = false;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000471 auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
472 auto i = Instructions.end() - 1;
473 for (auto n = Instructions.begin() - 1;; --i, ++Offset) {
474 if (i == n)
475 // Couldn't find producer
476 return MCDisassembler::Fail;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000477 bool CurrentVector = HexagonMCInstrInfo::isVector(*MCII, *i->getInst());
478 if (Vector && !CurrentVector)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000479 // Skip scalars when calculating distances for vectors
480 ++Lookback;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000481 if (HexagonMCInstrInfo::isImmext(*i->getInst()) && (Vector == PrevVector))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000482 ++Lookback;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000483 PrevVector = CurrentVector;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000484 if (Offset == Lookback)
485 break;
486 }
487 auto const &Inst = *i->getInst();
488 bool SubregBit = (Register & 0x1) != 0;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000489 if (HexagonMCInstrInfo::hasNewValue2(*MCII, Inst)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000490 // If subreg bit is set we're selecting the second produced newvalue
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000491 unsigned Producer = SubregBit ?
492 HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg() :
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000493 HexagonMCInstrInfo::getNewValueOperand2(*MCII, Inst).getReg();
494 assert(Producer != Hexagon::NoRegister);
495 MCO.setReg(Producer);
496 } else if (HexagonMCInstrInfo::hasNewValue(*MCII, Inst)) {
497 unsigned Producer =
498 HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg();
499 if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
500 Producer = ((Producer - Hexagon::W0) << 1) + SubregBit + Hexagon::V0;
501 else if (SubregBit)
Colin LeMahieu2d497a02016-03-01 22:05:03 +0000502 // Hexagon PRM 10.11 New-value operands
503 // Nt[0] is reserved and should always be encoded as zero.
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000504 return MCDisassembler::Fail;
505 assert(Producer != Hexagon::NoRegister);
506 MCO.setReg(Producer);
507 } else
508 return MCDisassembler::Fail;
509 }
510
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000511 if (CurrentExtender != nullptr) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000512 MCInst const &Inst = HexagonMCInstrInfo::isDuplex(*MCII, MI)
513 ? *MI.getOperand(1).getInst()
514 : MI;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000515 if (!HexagonMCInstrInfo::isExtendable(*MCII, Inst) &&
516 !HexagonMCInstrInfo::isExtended(*MCII, Inst))
517 return MCDisassembler::Fail;
518 }
Colin LeMahieu5d6f03b2014-12-04 03:41:21 +0000519 return Result;
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000520}
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000521
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000522static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,
Craig Toppere5e035a32015-12-05 07:13:35 +0000523 ArrayRef<MCPhysReg> Table) {
Craig Topper3da000c2015-12-01 06:13:04 +0000524 if (RegNo < Table.size()) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000525 Inst.addOperand(MCOperand::createReg(Table[RegNo]));
526 return MCDisassembler::Success;
Craig Topper3da000c2015-12-01 06:13:04 +0000527 }
528
529 return MCDisassembler::Fail;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000530}
531
Colin LeMahieu7c958712015-10-17 01:33:04 +0000532static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
533 uint64_t Address,
534 const void *Decoder) {
535 return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
536}
537
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000538static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
539 uint64_t Address,
540 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000541 static const MCPhysReg IntRegDecoderTable[] = {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000542 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
543 Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9,
544 Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
545 Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
546 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
547 Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
548 Hexagon::R30, Hexagon::R31};
549
Craig Toppere5e035a32015-12-05 07:13:35 +0000550 return DecodeRegisterClass(Inst, RegNo, IntRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000551}
Colin LeMahieu7c958712015-10-17 01:33:04 +0000552
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000553static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
554 unsigned RegNo,
555 uint64_t Address,
556 const void *Decoder) {
557 static const MCPhysReg GeneralSubRegDecoderTable[] = {
558 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3,
559 Hexagon::R4, Hexagon::R5, Hexagon::R6, Hexagon::R7,
560 Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
561 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23,
562 };
563
564 return DecodeRegisterClass(Inst, RegNo, GeneralSubRegDecoderTable);
565}
566
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000567static DecodeStatus DecodeHvxVRRegisterClass(MCInst &Inst, unsigned RegNo,
568 uint64_t /*Address*/,
569 const void *Decoder) {
570 static const MCPhysReg HvxVRDecoderTable[] = {
Colin LeMahieu7c958712015-10-17 01:33:04 +0000571 Hexagon::V0, Hexagon::V1, Hexagon::V2, Hexagon::V3, Hexagon::V4,
572 Hexagon::V5, Hexagon::V6, Hexagon::V7, Hexagon::V8, Hexagon::V9,
573 Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
574 Hexagon::V15, Hexagon::V16, Hexagon::V17, Hexagon::V18, Hexagon::V19,
575 Hexagon::V20, Hexagon::V21, Hexagon::V22, Hexagon::V23, Hexagon::V24,
576 Hexagon::V25, Hexagon::V26, Hexagon::V27, Hexagon::V28, Hexagon::V29,
577 Hexagon::V30, Hexagon::V31};
578
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000579 return DecodeRegisterClass(Inst, RegNo, HvxVRDecoderTable);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000580}
581
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000582static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
583 uint64_t /*Address*/,
584 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000585 static const MCPhysReg DoubleRegDecoderTable[] = {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000586 Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,
587 Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7,
588 Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11,
589 Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15};
590
Craig Toppere5e035a32015-12-05 07:13:35 +0000591 return DecodeRegisterClass(Inst, RegNo >> 1, DoubleRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000592}
593
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000594static DecodeStatus DecodeGeneralDoubleLow8RegsRegisterClass(
595 MCInst &Inst, unsigned RegNo, uint64_t /*Address*/, const void *Decoder) {
596 static const MCPhysReg GeneralDoubleLow8RegDecoderTable[] = {
597 Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,
598 Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11};
599
600 return DecodeRegisterClass(Inst, RegNo, GeneralDoubleLow8RegDecoderTable);
601}
602
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000603static DecodeStatus DecodeHvxWRRegisterClass(MCInst &Inst, unsigned RegNo,
604 uint64_t /*Address*/,
605 const void *Decoder) {
606 static const MCPhysReg HvxWRDecoderTable[] = {
Colin LeMahieu7c958712015-10-17 01:33:04 +0000607 Hexagon::W0, Hexagon::W1, Hexagon::W2, Hexagon::W3,
608 Hexagon::W4, Hexagon::W5, Hexagon::W6, Hexagon::W7,
609 Hexagon::W8, Hexagon::W9, Hexagon::W10, Hexagon::W11,
610 Hexagon::W12, Hexagon::W13, Hexagon::W14, Hexagon::W15};
611
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000612 return (DecodeRegisterClass(Inst, RegNo >> 1, HvxWRDecoderTable));
Colin LeMahieu7c958712015-10-17 01:33:04 +0000613}
614
Krzysztof Parzyszek13a9cf22018-12-05 20:18:09 +0000615LLVM_ATTRIBUTE_UNUSED // Suppress warning temporarily.
616static DecodeStatus DecodeHvxVQRRegisterClass(MCInst &Inst,
617 unsigned RegNo,
618 uint64_t /*Address*/,
619 const void *Decoder) {
620 static const MCPhysReg HvxVQRDecoderTable[] = {
621 Hexagon::VQ0, Hexagon::VQ1, Hexagon::VQ2, Hexagon::VQ3,
622 Hexagon::VQ4, Hexagon::VQ5, Hexagon::VQ6, Hexagon::VQ7};
623
624 return DecodeRegisterClass(Inst, RegNo >> 2, HvxVQRDecoderTable);
625}
626
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000627static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
628 uint64_t /*Address*/,
629 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000630 static const MCPhysReg PredRegDecoderTable[] = {Hexagon::P0, Hexagon::P1,
631 Hexagon::P2, Hexagon::P3};
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000632
Craig Toppere5e035a32015-12-05 07:13:35 +0000633 return DecodeRegisterClass(Inst, RegNo, PredRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000634}
635
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000636static DecodeStatus DecodeHvxQRRegisterClass(MCInst &Inst, unsigned RegNo,
637 uint64_t /*Address*/,
638 const void *Decoder) {
639 static const MCPhysReg HvxQRDecoderTable[] = {Hexagon::Q0, Hexagon::Q1,
640 Hexagon::Q2, Hexagon::Q3};
Colin LeMahieu7c958712015-10-17 01:33:04 +0000641
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000642 return DecodeRegisterClass(Inst, RegNo, HvxQRDecoderTable);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000643}
Colin LeMahieube8c4532015-06-05 16:00:11 +0000644
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000645static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
646 uint64_t /*Address*/,
647 const void *Decoder) {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000648 using namespace Hexagon;
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000649
Craig Toppere5e035a32015-12-05 07:13:35 +0000650 static const MCPhysReg CtrlRegDecoderTable[] = {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000651 /* 0 */ SA0, LC0, SA1, LC1,
Krzysztof Parzyszeke2603322017-05-05 22:12:12 +0000652 /* 4 */ P3_0, C5, M0, M1,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000653 /* 8 */ USR, PC, UGP, GP,
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000654 /* 12 */ CS0, CS1, UPCYCLELO, UPCYCLEHI,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000655 /* 16 */ FRAMELIMIT, FRAMEKEY, PKTCOUNTLO, PKTCOUNTHI,
656 /* 20 */ 0, 0, 0, 0,
657 /* 24 */ 0, 0, 0, 0,
658 /* 28 */ 0, 0, UTIMERLO, UTIMERHI
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000659 };
660
Craig Topper6261e1b2015-12-01 06:13:06 +0000661 if (RegNo >= array_lengthof(CtrlRegDecoderTable))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000662 return MCDisassembler::Fail;
663
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000664 static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
665 if (CtrlRegDecoderTable[RegNo] == NoRegister)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000666 return MCDisassembler::Fail;
667
668 unsigned Register = CtrlRegDecoderTable[RegNo];
669 Inst.addOperand(MCOperand::createReg(Register));
670 return MCDisassembler::Success;
671}
672
673static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
674 uint64_t /*Address*/,
675 const void *Decoder) {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000676 using namespace Hexagon;
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +0000677
Craig Toppere5e035a32015-12-05 07:13:35 +0000678 static const MCPhysReg CtrlReg64DecoderTable[] = {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000679 /* 0 */ C1_0, 0, C3_2, 0,
680 /* 4 */ C5_4, 0, C7_6, 0,
681 /* 8 */ C9_8, 0, C11_10, 0,
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000682 /* 12 */ CS, 0, UPCYCLE, 0,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000683 /* 16 */ C17_16, 0, PKTCOUNT, 0,
684 /* 20 */ 0, 0, 0, 0,
685 /* 24 */ 0, 0, 0, 0,
686 /* 28 */ 0, 0, UTIMER, 0
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000687 };
688
Craig Topper6261e1b2015-12-01 06:13:06 +0000689 if (RegNo >= array_lengthof(CtrlReg64DecoderTable))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000690 return MCDisassembler::Fail;
691
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000692 static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
693 if (CtrlReg64DecoderTable[RegNo] == NoRegister)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000694 return MCDisassembler::Fail;
695
696 unsigned Register = CtrlReg64DecoderTable[RegNo];
697 Inst.addOperand(MCOperand::createReg(Register));
698 return MCDisassembler::Success;
699}
700
701static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
702 uint64_t /*Address*/,
703 const void *Decoder) {
704 unsigned Register = 0;
705 switch (RegNo) {
706 case 0:
707 Register = Hexagon::M0;
708 break;
709 case 1:
710 Register = Hexagon::M1;
711 break;
712 default:
713 return MCDisassembler::Fail;
714 }
715 Inst.addOperand(MCOperand::createReg(Register));
716 return MCDisassembler::Success;
717}
718
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000719static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
720 uint64_t /*Address*/,
721 const void *Decoder) {
722 HexagonDisassembler const &Disassembler = disassembler(Decoder);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000723 int64_t FullValue = fullValue(Disassembler, MI, tmp);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000724 assert(FullValue >= 0 && "Negative in unsigned decoder");
725 HexagonMCInstrInfo::addConstant(MI, FullValue, Disassembler.getContext());
726 return MCDisassembler::Success;
727}
728
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000729static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
730 uint64_t /*Address*/, const void *Decoder) {
731 HexagonDisassembler const &Disassembler = disassembler(Decoder);
732 unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
733 tmp = SignExtend64(tmp, Bits);
734 signedDecoder<32>(MI, tmp, Decoder);
735 return MCDisassembler::Success;
736}
737
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000738// custom decoder for various jump/call immediates
739static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
740 const void *Decoder) {
741 HexagonDisassembler const &Disassembler = disassembler(Decoder);
742 unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
743 // r13_2 is not extendable, so if there are no extent bits, it's r13_2
744 if (Bits == 0)
745 Bits = 15;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000746 uint64_t FullValue = fullValue(Disassembler, MI, SignExtend64(tmp, Bits));
747 uint32_t Extended = FullValue + Address;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000748 if (!Disassembler.tryAddingSymbolicOperand(MI, Extended, Address, true, 0, 4))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000749 HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
750 return MCDisassembler::Success;
751}
Krzysztof Parzyszek22a21d42018-03-01 17:03:26 +0000752
753static DecodeStatus DecodeGuestRegsRegisterClass(MCInst &Inst, unsigned RegNo,
754 uint64_t /*Address*/,
755 const void *Decoder) {
756 using namespace Hexagon;
757
758 static const MCPhysReg GuestRegDecoderTable[] = {
759 /* 0 */ GELR, GSR, GOSP, G3,
760 /* 4 */ G4, G5, G6, G7,
761 /* 8 */ G8, G9, G10, G11,
762 /* 12 */ G12, G13, G14, G15,
763 /* 16 */ GPMUCNT4, GPMUCNT5, GPMUCNT6, GPMUCNT7,
764 /* 20 */ G20, G21, G22, G23,
765 /* 24 */ GPCYCLELO, GPCYCLEHI, GPMUCNT0, GPMUCNT1,
766 /* 28 */ GPMUCNT2, GPMUCNT3, G30, G31
767 };
768
769 if (RegNo >= array_lengthof(GuestRegDecoderTable))
770 return MCDisassembler::Fail;
771 if (GuestRegDecoderTable[RegNo] == Hexagon::NoRegister)
772 return MCDisassembler::Fail;
773
774 unsigned Register = GuestRegDecoderTable[RegNo];
775 Inst.addOperand(MCOperand::createReg(Register));
776 return MCDisassembler::Success;
777}
778
779static DecodeStatus DecodeGuestRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
780 uint64_t /*Address*/,
781 const void *Decoder) {
782 using namespace Hexagon;
783
784 static const MCPhysReg GuestReg64DecoderTable[] = {
785 /* 0 */ G1_0, 0, G3_2, 0,
786 /* 4 */ G5_4, 0, G7_6, 0,
787 /* 8 */ G9_8, 0, G11_10, 0,
788 /* 12 */ G13_12, 0, G15_14, 0,
789 /* 16 */ G17_16, 0, G19_18, 0,
790 /* 20 */ G21_20, 0, G23_22, 0,
791 /* 24 */ G25_24, 0, G27_26, 0,
792 /* 28 */ G29_28, 0, G31_30, 0
793 };
794
795 if (RegNo >= array_lengthof(GuestReg64DecoderTable))
796 return MCDisassembler::Fail;
797 if (GuestReg64DecoderTable[RegNo] == Hexagon::NoRegister)
798 return MCDisassembler::Fail;
799
800 unsigned Register = GuestReg64DecoderTable[RegNo];
801 Inst.addOperand(MCOperand::createReg(Register));
802 return MCDisassembler::Success;
803}