blob: 428e371a31c3dd4f37cbdf1c831198d6705c8bcc [file] [log] [blame]
NAKAMURA Takumi729be142014-10-27 12:37:26 +00001//===-- HexagonDisassembler.cpp - Disassembler for Hexagon ISA ------------===//
2//
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"
NAKAMURA Takumi729be142014-10-27 12:37:26 +000015#include "MCTargetDesc/HexagonMCTargetDesc.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000016#include "MCTargetDesc/HexagonMCInstrInfo.h"
Eugene Zelenko82085922016-12-13 22:13:50 +000017#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/STLExtras.h"
Benjamin Kramerf57c1972016-01-26 16:44:37 +000019#include "llvm/MC/MCDisassembler/MCDisassembler.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000020#include "llvm/MC/MCContext.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 Zelenko82085922016-12-13 22:13:50 +000027#include "llvm/Support/MathExtras.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000028#include "llvm/Support/raw_ostream.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000029#include "llvm/Support/TargetRegistry.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
Colin LeMahieu7cd08922015-11-09 04:07:48 +000038typedef MCDisassembler::DecodeStatus DecodeStatus;
NAKAMURA Takumi729be142014-10-27 12:37:26 +000039
40namespace {
Eugene Zelenko82085922016-12-13 22:13:50 +000041
NAKAMURA Takumi729be142014-10-27 12:37:26 +000042/// \brief Hexagon disassembler for all Hexagon platforms.
43class 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;
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)
50 : MCDisassembler(STI, Ctx), MCII(MCII), CurrentBundle(new MCInst *) {}
NAKAMURA Takumi729be142014-10-27 12:37:26 +000051
Colin LeMahieu68d967d2015-05-29 14:44:13 +000052 DecodeStatus getSingleInstruction(MCInst &Instr, MCInst &MCB,
53 ArrayRef<uint8_t> Bytes, uint64_t Address,
54 raw_ostream &VStream, raw_ostream &CStream,
55 bool &Complete) const;
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000056 DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +000057 ArrayRef<uint8_t> Bytes, uint64_t Address,
Rafael Espindola4aa6bea2014-11-10 18:11:10 +000058 raw_ostream &VStream,
59 raw_ostream &CStream) const override;
Colin LeMahieu7cd08922015-11-09 04:07:48 +000060 void addSubinstOperands(MCInst *MI, unsigned opcode, unsigned inst) const;
NAKAMURA Takumi729be142014-10-27 12:37:26 +000061};
Eugene Zelenko82085922016-12-13 22:13:50 +000062
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +000063namespace {
64 uint32_t fullValue(MCInstrInfo const &MCII, MCInst &MCB, MCInst &MI,
65 int64_t Value) {
66 MCInst const *Extender = HexagonMCInstrInfo::extenderForIndex(
67 MCB, HexagonMCInstrInfo::bundleSize(MCB));
68 if (!Extender || MI.size() != HexagonMCInstrInfo::getExtendableOp(MCII, MI))
69 return Value;
70 unsigned Alignment = HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
71 uint32_t Lower6 = static_cast<uint32_t>(Value >> Alignment) & 0x3f;
72 int64_t Bits;
73 bool Success = Extender->getOperand(0).getExpr()->evaluateAsAbsolute(Bits);
74 assert(Success); (void)Success;
75 uint32_t Upper26 = static_cast<uint32_t>(Bits);
76 uint32_t Operand = Upper26 | Lower6;
77 return Operand;
78 }
79 HexagonDisassembler const &disassembler(void const *Decoder) {
80 return *static_cast<HexagonDisassembler const *>(Decoder);
81 }
82 template <size_t T>
83 void signedDecoder(MCInst &MI, unsigned tmp, const void *Decoder) {
84 HexagonDisassembler const &Disassembler = disassembler(Decoder);
85 int64_t FullValue =
86 fullValue(*Disassembler.MCII, **Disassembler.CurrentBundle, MI,
87 SignExtend64<T>(tmp));
88 int64_t Extended = SignExtend64<32>(FullValue);
89 HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
90 }
91}
Eugene Zelenko82085922016-12-13 22:13:50 +000092} // end anonymous namespace
NAKAMURA Takumi729be142014-10-27 12:37:26 +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);
107static DecodeStatus DecodeVectorRegsRegisterClass(MCInst &Inst, unsigned RegNo,
108 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);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000116static DecodeStatus DecodeVecDblRegsRegisterClass(MCInst &Inst, unsigned RegNo,
117 uint64_t Address,
118 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000119static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
120 uint64_t Address,
121 const void *Decoder);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000122static DecodeStatus DecodeVecPredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
123 uint64_t Address,
124 const void *Decoder);
Colin LeMahieuf3db8842014-12-19 19:06:32 +0000125static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
Colin LeMahieube8c4532015-06-05 16:00:11 +0000126 uint64_t Address,
127 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000128static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
129 uint64_t Address,
130 const void *Decoder);
Colin LeMahieu404d5b22015-02-10 16:59:36 +0000131static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
Colin LeMahieube8c4532015-06-05 16:00:11 +0000132 uint64_t Address,
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000133 const void *Decoder);
134
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000135static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
136 uint64_t Address, const void *Decoder);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000137static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
138 uint64_t /*Address*/, const void *Decoder);
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000139static DecodeStatus s8_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
Colin LeMahieu1e9d1d72015-06-10 16:52:32 +0000140 const void *Decoder);
141static DecodeStatus s6_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
142 const void *Decoder);
143static DecodeStatus s4_0ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
144 const void *Decoder);
145static DecodeStatus s4_1ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
146 const void *Decoder);
147static DecodeStatus s4_2ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
148 const void *Decoder);
149static DecodeStatus s4_3ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
150 const void *Decoder);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000151static DecodeStatus s4_6ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
152 const void *Decoder);
153static DecodeStatus s3_6ImmDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
154 const void *Decoder);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000155static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
156 const void *Decoder);
Colin LeMahieuefa74e02014-11-18 20:28:11 +0000157
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000158#include "HexagonDepDecoders.h"
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000159#include "HexagonGenDisassemblerTables.inc"
160
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000161static MCDisassembler *createHexagonDisassembler(const Target &T,
162 const MCSubtargetInfo &STI,
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000163 MCContext &Ctx) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000164 return new HexagonDisassembler(STI, Ctx, T.createMCInstrInfo());
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000165}
166
167extern "C" void LLVMInitializeHexagonDisassembler() {
Mehdi Aminif42454b2016-10-09 23:00:34 +0000168 TargetRegistry::RegisterMCDisassembler(getTheHexagonTarget(),
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000169 createHexagonDisassembler);
170}
171
172DecodeStatus HexagonDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000173 ArrayRef<uint8_t> Bytes,
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000174 uint64_t Address,
175 raw_ostream &os,
176 raw_ostream &cs) const {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000177 DecodeStatus Result = DecodeStatus::Success;
178 bool Complete = false;
179 Size = 0;
Rafael Espindola4aa6bea2014-11-10 18:11:10 +0000180
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000181 *CurrentBundle = &MI;
Colin LeMahieuf0af6e52015-11-13 17:42:46 +0000182 MI = HexagonMCInstrInfo::createBundle();
Eugene Zelenko82085922016-12-13 22:13:50 +0000183 while (Result == Success && !Complete) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000184 if (Bytes.size() < HEXAGON_INSTR_SIZE)
185 return MCDisassembler::Fail;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000186 MCInst *Inst = new (getContext()) MCInst;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000187 Result = getSingleInstruction(*Inst, MI, Bytes, Address, os, cs, Complete);
188 MI.addOperand(MCOperand::createInst(Inst));
189 Size += HEXAGON_INSTR_SIZE;
190 Bytes = Bytes.slice(HEXAGON_INSTR_SIZE);
191 }
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000192 if (Result == MCDisassembler::Fail)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000193 return Result;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000194 if (Size > HEXAGON_MAX_PACKET_SIZE)
195 return MCDisassembler::Fail;
196 HexagonMCChecker Checker(*MCII, STI, MI, MI, *getContext().getRegisterInfo());
197 if (!Checker.check())
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000198 return MCDisassembler::Fail;
199 return MCDisassembler::Success;
200}
201
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000202namespace {
203void adjustDuplex(MCInst &MI, MCContext &Context) {
204 switch (MI.getOpcode()) {
205 case Hexagon::SA1_setin1:
206 MI.insert(MI.begin() + 1,
207 MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
208 break;
209 case Hexagon::SA1_dec:
210 MI.insert(MI.begin() + 2,
211 MCOperand::createExpr(MCConstantExpr::create(-1, Context)));
212 break;
213 default:
214 break;
215 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000216}
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000217}
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000218
219DecodeStatus HexagonDisassembler::getSingleInstruction(
220 MCInst &MI, MCInst &MCB, ArrayRef<uint8_t> Bytes, uint64_t Address,
221 raw_ostream &os, raw_ostream &cs, bool &Complete) const {
222 assert(Bytes.size() >= HEXAGON_INSTR_SIZE);
223
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000224 uint32_t Instruction = support::endian::read32le(Bytes.data());
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000225
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000226 auto BundleSize = HexagonMCInstrInfo::bundleSize(MCB);
227 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
228 HexagonII::INST_PARSE_LOOP_END) {
229 if (BundleSize == 0)
230 HexagonMCInstrInfo::setInnerLoop(MCB);
231 else if (BundleSize == 1)
232 HexagonMCInstrInfo::setOuterLoop(MCB);
233 else
234 return DecodeStatus::Fail;
235 }
236
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000237 MCInst const *Extender = HexagonMCInstrInfo::extenderForIndex(
238 MCB, HexagonMCInstrInfo::bundleSize(MCB));
239
240 DecodeStatus Result = DecodeStatus::Fail;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000241 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
Colin LeMahieube8c4532015-06-05 16:00:11 +0000242 HexagonII::INST_PARSE_DUPLEX) {
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000243 unsigned duplexIClass;
244 uint8_t const *DecodeLow, *DecodeHigh;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000245 duplexIClass = ((Instruction >> 28) & 0xe) | ((Instruction >> 13) & 0x1);
246 switch (duplexIClass) {
247 default:
248 return MCDisassembler::Fail;
249 case 0:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000250 DecodeLow = DecoderTableSUBINSN_L132;
251 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000252 break;
253 case 1:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000254 DecodeLow = DecoderTableSUBINSN_L232;
255 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000256 break;
257 case 2:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000258 DecodeLow = DecoderTableSUBINSN_L232;
259 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000260 break;
261 case 3:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000262 DecodeLow = DecoderTableSUBINSN_A32;
263 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000264 break;
265 case 4:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000266 DecodeLow = DecoderTableSUBINSN_L132;
267 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000268 break;
269 case 5:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000270 DecodeLow = DecoderTableSUBINSN_L232;
271 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000272 break;
273 case 6:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000274 DecodeLow = DecoderTableSUBINSN_S132;
275 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000276 break;
277 case 7:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000278 DecodeLow = DecoderTableSUBINSN_S232;
279 DecodeHigh = DecoderTableSUBINSN_A32;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000280 break;
281 case 8:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000282 DecodeLow = DecoderTableSUBINSN_S132;
283 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000284 break;
285 case 9:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000286 DecodeLow = DecoderTableSUBINSN_S132;
287 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000288 break;
289 case 10:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000290 DecodeLow = DecoderTableSUBINSN_S132;
291 DecodeHigh = DecoderTableSUBINSN_S132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000292 break;
293 case 11:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000294 DecodeLow = DecoderTableSUBINSN_S232;
295 DecodeHigh = DecoderTableSUBINSN_S132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000296 break;
297 case 12:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000298 DecodeLow = DecoderTableSUBINSN_S232;
299 DecodeHigh = DecoderTableSUBINSN_L132;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000300 break;
301 case 13:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000302 DecodeLow = DecoderTableSUBINSN_S232;
303 DecodeHigh = DecoderTableSUBINSN_L232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000304 break;
305 case 14:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000306 DecodeLow = DecoderTableSUBINSN_S232;
307 DecodeHigh = DecoderTableSUBINSN_S232;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000308 break;
309 }
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000310 MI.setOpcode(Hexagon::DuplexIClass0 + duplexIClass);
Colin LeMahieube8c4532015-06-05 16:00:11 +0000311 MCInst *MILow = new (getContext()) MCInst;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000312 MCInst *MIHigh = new (getContext()) MCInst;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000313 Result = decodeInstruction(DecodeLow, *MILow, Instruction & 0x1fff, Address,
314 this, STI);
315 if (Result != DecodeStatus::Success)
316 return DecodeStatus::Fail;
317 adjustDuplex(*MILow, getContext());
318 Result = decodeInstruction(
319 DecodeHigh, *MIHigh, (Instruction >> 16) & 0x1fff, Address, this, STI);
320 if (Result != DecodeStatus::Success)
321 return DecodeStatus::Fail;
322 adjustDuplex(*MIHigh, getContext());
Colin LeMahieube8c4532015-06-05 16:00:11 +0000323 MCOperand OPLow = MCOperand::createInst(MILow);
324 MCOperand OPHigh = MCOperand::createInst(MIHigh);
325 MI.addOperand(OPLow);
326 MI.addOperand(OPHigh);
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000327 Complete = true;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000328 } else {
329 if ((Instruction & HexagonII::INST_PARSE_MASK) ==
330 HexagonII::INST_PARSE_PACKET_END)
331 Complete = true;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000332
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000333 if (Extender != nullptr)
334 Result = decodeInstruction(DecoderTableMustExtend32, MI, Instruction,
335 Address, this, STI);
336
337 if (Result != MCDisassembler::Success)
338 Result = decodeInstruction(DecoderTable32, MI, Instruction, Address, this,
339 STI);
340
341 if (Result != MCDisassembler::Success &&
342 STI.getFeatureBits()[Hexagon::ExtensionHVX])
343 Result = decodeInstruction(DecoderTableEXT_mmvec32, MI, Instruction,
344 Address, this, STI);
345
Colin LeMahieube8c4532015-06-05 16:00:11 +0000346 }
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000347
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000348 switch (MI.getOpcode()) {
Colin LeMahieu81707542016-12-05 04:29:00 +0000349 case Hexagon::J4_cmpeqn1_f_jumpnv_nt:
350 case Hexagon::J4_cmpeqn1_f_jumpnv_t:
351 case Hexagon::J4_cmpeqn1_fp0_jump_nt:
352 case Hexagon::J4_cmpeqn1_fp0_jump_t:
353 case Hexagon::J4_cmpeqn1_fp1_jump_nt:
354 case Hexagon::J4_cmpeqn1_fp1_jump_t:
355 case Hexagon::J4_cmpeqn1_t_jumpnv_nt:
356 case Hexagon::J4_cmpeqn1_t_jumpnv_t:
357 case Hexagon::J4_cmpeqn1_tp0_jump_nt:
358 case Hexagon::J4_cmpeqn1_tp0_jump_t:
359 case Hexagon::J4_cmpeqn1_tp1_jump_nt:
360 case Hexagon::J4_cmpeqn1_tp1_jump_t:
361 case Hexagon::J4_cmpgtn1_f_jumpnv_nt:
362 case Hexagon::J4_cmpgtn1_f_jumpnv_t:
363 case Hexagon::J4_cmpgtn1_fp0_jump_nt:
364 case Hexagon::J4_cmpgtn1_fp0_jump_t:
365 case Hexagon::J4_cmpgtn1_fp1_jump_nt:
366 case Hexagon::J4_cmpgtn1_fp1_jump_t:
367 case Hexagon::J4_cmpgtn1_t_jumpnv_nt:
368 case Hexagon::J4_cmpgtn1_t_jumpnv_t:
369 case Hexagon::J4_cmpgtn1_tp0_jump_nt:
370 case Hexagon::J4_cmpgtn1_tp0_jump_t:
371 case Hexagon::J4_cmpgtn1_tp1_jump_nt:
372 case Hexagon::J4_cmpgtn1_tp1_jump_t:
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000373 MI.insert(MI.begin() + 1,
374 MCOperand::createExpr(MCConstantExpr::create(-1, getContext())));
Colin LeMahieu81707542016-12-05 04:29:00 +0000375 break;
376 default:
377 break;
378 }
379
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000380 if (HexagonMCInstrInfo::isNewValue(*MCII, MI)) {
381 unsigned OpIndex = HexagonMCInstrInfo::getNewValueOp(*MCII, MI);
382 MCOperand &MCO = MI.getOperand(OpIndex);
383 assert(MCO.isReg() && "New value consumers must be registers");
384 unsigned Register =
385 getContext().getRegisterInfo()->getEncodingValue(MCO.getReg());
386 if ((Register & 0x6) == 0)
387 // HexagonPRM 10.11 Bit 1-2 == 0 is reserved
388 return MCDisassembler::Fail;
389 unsigned Lookback = (Register & 0x6) >> 1;
390 unsigned Offset = 1;
391 bool Vector = HexagonMCInstrInfo::isVector(*MCII, MI);
392 auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
393 auto i = Instructions.end() - 1;
394 for (auto n = Instructions.begin() - 1;; --i, ++Offset) {
395 if (i == n)
396 // Couldn't find producer
397 return MCDisassembler::Fail;
398 if (Vector && !HexagonMCInstrInfo::isVector(*MCII, *i->getInst()))
399 // Skip scalars when calculating distances for vectors
400 ++Lookback;
401 if (HexagonMCInstrInfo::isImmext(*i->getInst()))
402 ++Lookback;
403 if (Offset == Lookback)
404 break;
405 }
406 auto const &Inst = *i->getInst();
407 bool SubregBit = (Register & 0x1) != 0;
408 if (SubregBit && HexagonMCInstrInfo::hasNewValue2(*MCII, Inst)) {
409 // If subreg bit is set we're selecting the second produced newvalue
410 unsigned Producer =
411 HexagonMCInstrInfo::getNewValueOperand2(*MCII, Inst).getReg();
412 assert(Producer != Hexagon::NoRegister);
413 MCO.setReg(Producer);
414 } else if (HexagonMCInstrInfo::hasNewValue(*MCII, Inst)) {
415 unsigned Producer =
416 HexagonMCInstrInfo::getNewValueOperand(*MCII, Inst).getReg();
417 if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
418 Producer = ((Producer - Hexagon::W0) << 1) + SubregBit + Hexagon::V0;
419 else if (SubregBit)
Colin LeMahieu2d497a02016-03-01 22:05:03 +0000420 // Hexagon PRM 10.11 New-value operands
421 // Nt[0] is reserved and should always be encoded as zero.
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000422 return MCDisassembler::Fail;
423 assert(Producer != Hexagon::NoRegister);
424 MCO.setReg(Producer);
425 } else
426 return MCDisassembler::Fail;
427 }
428
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000429 if (Extender != nullptr) {
430 MCInst const &Inst = HexagonMCInstrInfo::isDuplex(*MCII, MI)
431 ? *MI.getOperand(1).getInst()
432 : MI;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000433 if (!HexagonMCInstrInfo::isExtendable(*MCII, Inst) &&
434 !HexagonMCInstrInfo::isExtended(*MCII, Inst))
435 return MCDisassembler::Fail;
436 }
Colin LeMahieu5d6f03b2014-12-04 03:41:21 +0000437 return Result;
NAKAMURA Takumi729be142014-10-27 12:37:26 +0000438}
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000439
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000440static DecodeStatus DecodeRegisterClass(MCInst &Inst, unsigned RegNo,
Craig Toppere5e035a32015-12-05 07:13:35 +0000441 ArrayRef<MCPhysReg> Table) {
Craig Topper3da000c2015-12-01 06:13:04 +0000442 if (RegNo < Table.size()) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000443 Inst.addOperand(MCOperand::createReg(Table[RegNo]));
444 return MCDisassembler::Success;
Craig Topper3da000c2015-12-01 06:13:04 +0000445 }
446
447 return MCDisassembler::Fail;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000448}
449
Colin LeMahieu7c958712015-10-17 01:33:04 +0000450static DecodeStatus DecodeIntRegsLow8RegisterClass(MCInst &Inst, unsigned RegNo,
451 uint64_t Address,
452 const void *Decoder) {
453 return DecodeIntRegsRegisterClass(Inst, RegNo, Address, Decoder);
454}
455
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000456static DecodeStatus DecodeIntRegsRegisterClass(MCInst &Inst, unsigned RegNo,
457 uint64_t Address,
458 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000459 static const MCPhysReg IntRegDecoderTable[] = {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000460 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3, Hexagon::R4,
461 Hexagon::R5, Hexagon::R6, Hexagon::R7, Hexagon::R8, Hexagon::R9,
462 Hexagon::R10, Hexagon::R11, Hexagon::R12, Hexagon::R13, Hexagon::R14,
463 Hexagon::R15, Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
464 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23, Hexagon::R24,
465 Hexagon::R25, Hexagon::R26, Hexagon::R27, Hexagon::R28, Hexagon::R29,
466 Hexagon::R30, Hexagon::R31};
467
Craig Toppere5e035a32015-12-05 07:13:35 +0000468 return DecodeRegisterClass(Inst, RegNo, IntRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000469}
Colin LeMahieu7c958712015-10-17 01:33:04 +0000470
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000471static DecodeStatus DecodeGeneralSubRegsRegisterClass(MCInst &Inst,
472 unsigned RegNo,
473 uint64_t Address,
474 const void *Decoder) {
475 static const MCPhysReg GeneralSubRegDecoderTable[] = {
476 Hexagon::R0, Hexagon::R1, Hexagon::R2, Hexagon::R3,
477 Hexagon::R4, Hexagon::R5, Hexagon::R6, Hexagon::R7,
478 Hexagon::R16, Hexagon::R17, Hexagon::R18, Hexagon::R19,
479 Hexagon::R20, Hexagon::R21, Hexagon::R22, Hexagon::R23,
480 };
481
482 return DecodeRegisterClass(Inst, RegNo, GeneralSubRegDecoderTable);
483}
484
Colin LeMahieu7c958712015-10-17 01:33:04 +0000485static DecodeStatus DecodeVectorRegsRegisterClass(MCInst &Inst, unsigned RegNo,
486 uint64_t /*Address*/,
487 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000488 static const MCPhysReg VecRegDecoderTable[] = {
Colin LeMahieu7c958712015-10-17 01:33:04 +0000489 Hexagon::V0, Hexagon::V1, Hexagon::V2, Hexagon::V3, Hexagon::V4,
490 Hexagon::V5, Hexagon::V6, Hexagon::V7, Hexagon::V8, Hexagon::V9,
491 Hexagon::V10, Hexagon::V11, Hexagon::V12, Hexagon::V13, Hexagon::V14,
492 Hexagon::V15, Hexagon::V16, Hexagon::V17, Hexagon::V18, Hexagon::V19,
493 Hexagon::V20, Hexagon::V21, Hexagon::V22, Hexagon::V23, Hexagon::V24,
494 Hexagon::V25, Hexagon::V26, Hexagon::V27, Hexagon::V28, Hexagon::V29,
495 Hexagon::V30, Hexagon::V31};
496
Craig Toppere5e035a32015-12-05 07:13:35 +0000497 return DecodeRegisterClass(Inst, RegNo, VecRegDecoderTable);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000498}
499
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000500static DecodeStatus DecodeDoubleRegsRegisterClass(MCInst &Inst, unsigned RegNo,
501 uint64_t /*Address*/,
502 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000503 static const MCPhysReg DoubleRegDecoderTable[] = {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000504 Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,
505 Hexagon::D4, Hexagon::D5, Hexagon::D6, Hexagon::D7,
506 Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11,
507 Hexagon::D12, Hexagon::D13, Hexagon::D14, Hexagon::D15};
508
Craig Toppere5e035a32015-12-05 07:13:35 +0000509 return DecodeRegisterClass(Inst, RegNo >> 1, DoubleRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000510}
511
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000512static DecodeStatus DecodeGeneralDoubleLow8RegsRegisterClass(
513 MCInst &Inst, unsigned RegNo, uint64_t /*Address*/, const void *Decoder) {
514 static const MCPhysReg GeneralDoubleLow8RegDecoderTable[] = {
515 Hexagon::D0, Hexagon::D1, Hexagon::D2, Hexagon::D3,
516 Hexagon::D8, Hexagon::D9, Hexagon::D10, Hexagon::D11};
517
518 return DecodeRegisterClass(Inst, RegNo, GeneralDoubleLow8RegDecoderTable);
519}
520
Colin LeMahieu7c958712015-10-17 01:33:04 +0000521static DecodeStatus DecodeVecDblRegsRegisterClass(MCInst &Inst, unsigned RegNo,
522 uint64_t /*Address*/,
523 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000524 static const MCPhysReg VecDblRegDecoderTable[] = {
Colin LeMahieu7c958712015-10-17 01:33:04 +0000525 Hexagon::W0, Hexagon::W1, Hexagon::W2, Hexagon::W3,
526 Hexagon::W4, Hexagon::W5, Hexagon::W6, Hexagon::W7,
527 Hexagon::W8, Hexagon::W9, Hexagon::W10, Hexagon::W11,
528 Hexagon::W12, Hexagon::W13, Hexagon::W14, Hexagon::W15};
529
Craig Topper3da000c2015-12-01 06:13:04 +0000530 return (DecodeRegisterClass(Inst, RegNo >> 1, VecDblRegDecoderTable));
Colin LeMahieu7c958712015-10-17 01:33:04 +0000531}
532
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000533static DecodeStatus DecodePredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
534 uint64_t /*Address*/,
535 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000536 static const MCPhysReg PredRegDecoderTable[] = {Hexagon::P0, Hexagon::P1,
537 Hexagon::P2, Hexagon::P3};
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000538
Craig Toppere5e035a32015-12-05 07:13:35 +0000539 return DecodeRegisterClass(Inst, RegNo, PredRegDecoderTable);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000540}
541
Colin LeMahieu7c958712015-10-17 01:33:04 +0000542static DecodeStatus DecodeVecPredRegsRegisterClass(MCInst &Inst, unsigned RegNo,
543 uint64_t /*Address*/,
544 const void *Decoder) {
Craig Toppere5e035a32015-12-05 07:13:35 +0000545 static const MCPhysReg VecPredRegDecoderTable[] = {Hexagon::Q0, Hexagon::Q1,
546 Hexagon::Q2, Hexagon::Q3};
Colin LeMahieu7c958712015-10-17 01:33:04 +0000547
Craig Toppere5e035a32015-12-05 07:13:35 +0000548 return DecodeRegisterClass(Inst, RegNo, VecPredRegDecoderTable);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000549}
Colin LeMahieube8c4532015-06-05 16:00:11 +0000550
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000551static DecodeStatus DecodeCtrRegsRegisterClass(MCInst &Inst, unsigned RegNo,
552 uint64_t /*Address*/,
553 const void *Decoder) {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000554 using namespace Hexagon;
Craig Toppere5e035a32015-12-05 07:13:35 +0000555 static const MCPhysReg CtrlRegDecoderTable[] = {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000556 /* 0 */ SA0, LC0, SA1, LC1,
557 /* 4 */ P3_0, C5, C6, C7,
558 /* 8 */ USR, PC, UGP, GP,
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000559 /* 12 */ CS0, CS1, UPCYCLELO, UPCYCLEHI,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000560 /* 16 */ FRAMELIMIT, FRAMEKEY, PKTCOUNTLO, PKTCOUNTHI,
561 /* 20 */ 0, 0, 0, 0,
562 /* 24 */ 0, 0, 0, 0,
563 /* 28 */ 0, 0, UTIMERLO, UTIMERHI
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000564 };
565
Craig Topper6261e1b2015-12-01 06:13:06 +0000566 if (RegNo >= array_lengthof(CtrlRegDecoderTable))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000567 return MCDisassembler::Fail;
568
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000569 static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
570 if (CtrlRegDecoderTable[RegNo] == NoRegister)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000571 return MCDisassembler::Fail;
572
573 unsigned Register = CtrlRegDecoderTable[RegNo];
574 Inst.addOperand(MCOperand::createReg(Register));
575 return MCDisassembler::Success;
576}
577
578static DecodeStatus DecodeCtrRegs64RegisterClass(MCInst &Inst, unsigned RegNo,
579 uint64_t /*Address*/,
580 const void *Decoder) {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000581 using namespace Hexagon;
Craig Toppere5e035a32015-12-05 07:13:35 +0000582 static const MCPhysReg CtrlReg64DecoderTable[] = {
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000583 /* 0 */ C1_0, 0, C3_2, 0,
584 /* 4 */ C5_4, 0, C7_6, 0,
585 /* 8 */ C9_8, 0, C11_10, 0,
Krzysztof Parzyszekab57c2b2017-02-22 22:28:47 +0000586 /* 12 */ CS, 0, UPCYCLE, 0,
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000587 /* 16 */ C17_16, 0, PKTCOUNT, 0,
588 /* 20 */ 0, 0, 0, 0,
589 /* 24 */ 0, 0, 0, 0,
590 /* 28 */ 0, 0, UTIMER, 0
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000591 };
592
Craig Topper6261e1b2015-12-01 06:13:06 +0000593 if (RegNo >= array_lengthof(CtrlReg64DecoderTable))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000594 return MCDisassembler::Fail;
595
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000596 static_assert(NoRegister == 0, "Expecting NoRegister to be 0");
597 if (CtrlReg64DecoderTable[RegNo] == NoRegister)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000598 return MCDisassembler::Fail;
599
600 unsigned Register = CtrlReg64DecoderTable[RegNo];
601 Inst.addOperand(MCOperand::createReg(Register));
602 return MCDisassembler::Success;
603}
604
605static DecodeStatus DecodeModRegsRegisterClass(MCInst &Inst, unsigned RegNo,
606 uint64_t /*Address*/,
607 const void *Decoder) {
608 unsigned Register = 0;
609 switch (RegNo) {
610 case 0:
611 Register = Hexagon::M0;
612 break;
613 case 1:
614 Register = Hexagon::M1;
615 break;
616 default:
617 return MCDisassembler::Fail;
618 }
619 Inst.addOperand(MCOperand::createReg(Register));
620 return MCDisassembler::Success;
621}
622
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000623static DecodeStatus unsignedImmDecoder(MCInst &MI, unsigned tmp,
624 uint64_t /*Address*/,
625 const void *Decoder) {
626 HexagonDisassembler const &Disassembler = disassembler(Decoder);
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000627 int64_t FullValue =
628 fullValue(*Disassembler.MCII, **Disassembler.CurrentBundle, MI, tmp);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000629 assert(FullValue >= 0 && "Negative in unsigned decoder");
630 HexagonMCInstrInfo::addConstant(MI, FullValue, Disassembler.getContext());
631 return MCDisassembler::Success;
632}
633
Colin LeMahieu7c958712015-10-17 01:33:04 +0000634static DecodeStatus s4_6ImmDecoder(MCInst &MI, unsigned tmp,
635 uint64_t /*Address*/, const void *Decoder) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000636 signedDecoder<10>(MI, tmp, Decoder);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000637 return MCDisassembler::Success;
638}
639
640static DecodeStatus s3_6ImmDecoder(MCInst &MI, unsigned tmp,
641 uint64_t /*Address*/, const void *Decoder) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000642 signedDecoder<19>(MI, tmp, Decoder);
Colin LeMahieu7c958712015-10-17 01:33:04 +0000643 return MCDisassembler::Success;
644}
645
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000646static DecodeStatus s32_0ImmDecoder(MCInst &MI, unsigned tmp,
647 uint64_t /*Address*/, const void *Decoder) {
648 HexagonDisassembler const &Disassembler = disassembler(Decoder);
649 unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
650 tmp = SignExtend64(tmp, Bits);
651 signedDecoder<32>(MI, tmp, Decoder);
652 return MCDisassembler::Success;
653}
654
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000655// custom decoder for various jump/call immediates
656static DecodeStatus brtargetDecoder(MCInst &MI, unsigned tmp, uint64_t Address,
657 const void *Decoder) {
658 HexagonDisassembler const &Disassembler = disassembler(Decoder);
659 unsigned Bits = HexagonMCInstrInfo::getExtentBits(*Disassembler.MCII, MI);
660 // r13_2 is not extendable, so if there are no extent bits, it's r13_2
661 if (Bits == 0)
662 Bits = 15;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000663 uint32_t FullValue =
664 fullValue(*Disassembler.MCII, **Disassembler.CurrentBundle, MI,
665 SignExtend64(tmp, Bits));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000666 int64_t Extended = SignExtend64<32>(FullValue) + Address;
Krzysztof Parzyszeka72fad92017-02-10 15:33:13 +0000667 if (!Disassembler.tryAddingSymbolicOperand(MI, Extended, Address, true, 0, 4))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000668 HexagonMCInstrInfo::addConstant(MI, Extended, Disassembler.getContext());
669 return MCDisassembler::Success;
670}
671
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000672