blob: 44d1dd64e12a23de0de090a4acefcde2446cd1a8 [file] [log] [blame]
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001//===-- HexagonAsmParser.cpp - Parse Hexagon asm to MCInst instructions----===//
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
10#define DEBUG_TYPE "mcasmparser"
11
12#include "Hexagon.h"
13#include "HexagonRegisterInfo.h"
14#include "HexagonTargetStreamer.h"
15#include "MCTargetDesc/HexagonBaseInfo.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000016#include "MCTargetDesc/HexagonMCAsmInfo.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000017#include "MCTargetDesc/HexagonMCChecker.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000018#include "MCTargetDesc/HexagonMCELFStreamer.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000019#include "MCTargetDesc/HexagonMCExpr.h"
20#include "MCTargetDesc/HexagonMCShuffler.h"
21#include "MCTargetDesc/HexagonMCTargetDesc.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000022#include "MCTargetDesc/HexagonShuffler.h"
23#include "llvm/ADT/SmallString.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/StringExtras.h"
26#include "llvm/ADT/Twine.h"
27#include "llvm/MC/MCContext.h"
28#include "llvm/MC/MCELFStreamer.h"
29#include "llvm/MC/MCExpr.h"
30#include "llvm/MC/MCInst.h"
31#include "llvm/MC/MCParser/MCAsmLexer.h"
32#include "llvm/MC/MCParser/MCAsmParser.h"
33#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000034#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000035#include "llvm/MC/MCSectionELF.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000036#include "llvm/MC/MCStreamer.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000037#include "llvm/MC/MCSubtargetInfo.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000038#include "llvm/Support/CommandLine.h"
39#include "llvm/Support/Debug.h"
40#include "llvm/Support/ELF.h"
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +000041#include "llvm/Support/Format.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000042#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000043#include "llvm/Support/SourceMgr.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000044#include "llvm/Support/TargetRegistry.h"
45#include "llvm/Support/raw_ostream.h"
46#include <sstream>
47
48using namespace llvm;
49
50static cl::opt<bool> EnableFutureRegs("mfuture-regs",
51 cl::desc("Enable future registers"));
52
53static cl::opt<bool> WarnMissingParenthesis("mwarn-missing-parenthesis",
54cl::desc("Warn for missing parenthesis around predicate registers"),
55cl::init(true));
56static cl::opt<bool> ErrorMissingParenthesis("merror-missing-parenthesis",
57cl::desc("Error for missing parenthesis around predicate registers"),
58cl::init(false));
59static cl::opt<bool> WarnSignedMismatch("mwarn-sign-mismatch",
60cl::desc("Warn for mismatching a signed and unsigned value"),
61cl::init(true));
62static cl::opt<bool> WarnNoncontigiousRegister("mwarn-noncontigious-register",
63cl::desc("Warn for register names that arent contigious"),
64cl::init(true));
65static cl::opt<bool> ErrorNoncontigiousRegister("merror-noncontigious-register",
66cl::desc("Error for register names that aren't contigious"),
67cl::init(false));
68
69
70namespace {
71struct HexagonOperand;
72
73class HexagonAsmParser : public MCTargetAsmParser {
74
75 HexagonTargetStreamer &getTargetStreamer() {
76 MCTargetStreamer &TS = *Parser.getStreamer().getTargetStreamer();
77 return static_cast<HexagonTargetStreamer &>(TS);
78 }
79
Colin LeMahieu7cd08922015-11-09 04:07:48 +000080 MCAsmParser &Parser;
81 MCAssembler *Assembler;
82 MCInstrInfo const &MCII;
83 MCInst MCB;
84 bool InBrackets;
85
86 MCAsmParser &getParser() const { return Parser; }
87 MCAssembler *getAssembler() const { return Assembler; }
88 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
89
Colin LeMahieu7cd08922015-11-09 04:07:48 +000090 bool equalIsAsmAssignment() override { return false; }
91 bool isLabel(AsmToken &Token) override;
92
93 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
94 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
95 bool ParseDirectiveFalign(unsigned Size, SMLoc L);
96
97 virtual bool ParseRegister(unsigned &RegNo,
98 SMLoc &StartLoc,
99 SMLoc &EndLoc) override;
100 bool ParseDirectiveSubsection(SMLoc L);
101 bool ParseDirectiveValue(unsigned Size, SMLoc L);
102 bool ParseDirectiveComm(bool IsLocal, SMLoc L);
103 bool RegisterMatchesArch(unsigned MatchNum) const;
104
105 bool matchBundleOptions();
106 bool handleNoncontigiousRegister(bool Contigious, SMLoc &Loc);
107 bool finishBundle(SMLoc IDLoc, MCStreamer &Out);
108 void canonicalizeImmediates(MCInst &MCI);
109 bool matchOneInstruction(MCInst &MCB, SMLoc IDLoc,
110 OperandVector &InstOperands, uint64_t &ErrorInfo,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000111 bool MatchingInlineAsm);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000112
113 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
114 OperandVector &Operands, MCStreamer &Out,
Colin LeMahieu9ea507e2015-11-09 07:10:24 +0000115 uint64_t &ErrorInfo, bool MatchingInlineAsm) override;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000116
117 unsigned validateTargetOperandClass(MCParsedAsmOperand &Op, unsigned Kind) override;
118 void OutOfRange(SMLoc IDLoc, long long Val, long long Max);
119 int processInstruction(MCInst &Inst, OperandVector const &Operands,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000120 SMLoc IDLoc);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000121
122 // Check if we have an assembler and, if so, set the ELF e_header flags.
123 void chksetELFHeaderEFlags(unsigned flags) {
124 if (getAssembler())
125 getAssembler()->setELFHeaderEFlags(flags);
126 }
127
128/// @name Auto-generated Match Functions
129/// {
130
131#define GET_ASSEMBLER_HEADER
132#include "HexagonGenAsmMatcher.inc"
133
134 /// }
135
136public:
Akira Hatanakab11ef082015-11-14 06:35:56 +0000137 HexagonAsmParser(const MCSubtargetInfo &_STI, MCAsmParser &_Parser,
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000138 const MCInstrInfo &MII, const MCTargetOptions &Options)
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000139 : MCTargetAsmParser(Options, _STI), Parser(_Parser),
Colin LeMahieuf0af6e52015-11-13 17:42:46 +0000140 MCII (MII), MCB(HexagonMCInstrInfo::createBundle()), InBrackets(false) {
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000141 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000142
143 MCAsmParserExtension::Initialize(_Parser);
144
145 Assembler = nullptr;
146 // FIXME: need better way to detect AsmStreamer (upstream removed getKind())
147 if (!Parser.getStreamer().hasRawTextSupport()) {
148 MCELFStreamer *MES = static_cast<MCELFStreamer *>(&Parser.getStreamer());
149 Assembler = &MES->getAssembler();
150 }
151 }
152
153 bool mustExtend(OperandVector &Operands);
154 bool splitIdentifier(OperandVector &Operands);
155 bool parseOperand(OperandVector &Operands);
156 bool parseInstruction(OperandVector &Operands);
157 bool implicitExpressionLocation(OperandVector &Operands);
158 bool parseExpressionOrOperand(OperandVector &Operands);
159 bool parseExpression(MCExpr const *& Expr);
160 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
Colin LeMahieu9ea507e2015-11-09 07:10:24 +0000161 SMLoc NameLoc, OperandVector &Operands) override
162 {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000163 llvm_unreachable("Unimplemented");
164 }
165 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
Colin LeMahieu9ea507e2015-11-09 07:10:24 +0000166 AsmToken ID, OperandVector &Operands) override;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000167
Colin LeMahieu9ea507e2015-11-09 07:10:24 +0000168 virtual bool ParseDirective(AsmToken DirectiveID) override;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000169};
170
171/// HexagonOperand - Instances of this class represent a parsed Hexagon machine
172/// instruction.
173struct HexagonOperand : public MCParsedAsmOperand {
174 enum KindTy { Token, Immediate, Register } Kind;
175
176 SMLoc StartLoc, EndLoc;
177
178 struct TokTy {
179 const char *Data;
180 unsigned Length;
181 };
182
183 struct RegTy {
184 unsigned RegNum;
185 };
186
187 struct ImmTy {
188 const MCExpr *Val;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000189 };
190
191 struct InstTy {
192 OperandVector *SubInsts;
193 };
194
195 union {
196 struct TokTy Tok;
197 struct RegTy Reg;
198 struct ImmTy Imm;
199 };
200
201 HexagonOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
202
203public:
204 HexagonOperand(const HexagonOperand &o) : MCParsedAsmOperand() {
205 Kind = o.Kind;
206 StartLoc = o.StartLoc;
207 EndLoc = o.EndLoc;
208 switch (Kind) {
209 case Register:
210 Reg = o.Reg;
211 break;
212 case Immediate:
213 Imm = o.Imm;
214 break;
215 case Token:
216 Tok = o.Tok;
217 break;
218 }
219 }
220
221 /// getStartLoc - Get the location of the first token of this operand.
222 SMLoc getStartLoc() const { return StartLoc; }
223
224 /// getEndLoc - Get the location of the last token of this operand.
225 SMLoc getEndLoc() const { return EndLoc; }
226
227 unsigned getReg() const {
228 assert(Kind == Register && "Invalid access!");
229 return Reg.RegNum;
230 }
231
232 const MCExpr *getImm() const {
233 assert(Kind == Immediate && "Invalid access!");
234 return Imm.Val;
235 }
236
237 bool isToken() const { return Kind == Token; }
238 bool isImm() const { return Kind == Immediate; }
239 bool isMem() const { llvm_unreachable("No isMem"); }
240 bool isReg() const { return Kind == Register; }
241
242 bool CheckImmRange(int immBits, int zeroBits, bool isSigned,
243 bool isRelocatable, bool Extendable) const {
244 if (Kind == Immediate) {
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000245 const MCExpr *myMCExpr = &HexagonMCInstrInfo::getExpr(*getImm());
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000246 if (HexagonMCInstrInfo::mustExtend(*Imm.Val) && !Extendable)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000247 return false;
248 int64_t Res;
249 if (myMCExpr->evaluateAsAbsolute(Res)) {
250 int bits = immBits + zeroBits;
251 // Field bit range is zerobits + bits
252 // zeroBits must be 0
253 if (Res & ((1 << zeroBits) - 1))
254 return false;
255 if (isSigned) {
256 if (Res < (1LL << (bits - 1)) && Res >= -(1LL << (bits - 1)))
257 return true;
258 } else {
259 if (bits == 64)
260 return true;
261 if (Res >= 0)
262 return ((uint64_t)Res < (uint64_t)(1ULL << bits)) ? true : false;
263 else {
264 const int64_t high_bit_set = 1ULL << 63;
265 const uint64_t mask = (high_bit_set >> (63 - bits));
266 return (((uint64_t)Res & mask) == mask) ? true : false;
267 }
268 }
269 } else if (myMCExpr->getKind() == MCExpr::SymbolRef && isRelocatable)
270 return true;
271 else if (myMCExpr->getKind() == MCExpr::Binary ||
272 myMCExpr->getKind() == MCExpr::Unary)
273 return true;
274 }
275 return false;
276 }
277
278 bool isf32Ext() const { return false; }
279 bool iss32Imm() const { return CheckImmRange(32, 0, true, true, false); }
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000280 bool iss23_2Imm() const { return CheckImmRange(23, 2, true, true, false); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000281 bool iss8Imm() const { return CheckImmRange(8, 0, true, false, false); }
282 bool iss8Imm64() const { return CheckImmRange(8, 0, true, true, false); }
283 bool iss7Imm() const { return CheckImmRange(7, 0, true, false, false); }
284 bool iss6Imm() const { return CheckImmRange(6, 0, true, false, false); }
285 bool iss4Imm() const { return CheckImmRange(4, 0, true, false, false); }
286 bool iss4_0Imm() const { return CheckImmRange(4, 0, true, false, false); }
287 bool iss4_1Imm() const { return CheckImmRange(4, 1, true, false, false); }
288 bool iss4_2Imm() const { return CheckImmRange(4, 2, true, false, false); }
289 bool iss4_3Imm() const { return CheckImmRange(4, 3, true, false, false); }
290 bool iss4_6Imm() const { return CheckImmRange(4, 0, true, false, false); }
291 bool iss3_6Imm() const { return CheckImmRange(3, 0, true, false, false); }
292 bool iss3Imm() const { return CheckImmRange(3, 0, true, false, false); }
293
294 bool isu64Imm() const { return CheckImmRange(64, 0, false, true, true); }
295 bool isu32Imm() const { return CheckImmRange(32, 0, false, true, false); }
296 bool isu26_6Imm() const { return CheckImmRange(26, 6, false, true, false); }
297 bool isu16Imm() const { return CheckImmRange(16, 0, false, true, false); }
298 bool isu16_0Imm() const { return CheckImmRange(16, 0, false, true, false); }
299 bool isu16_1Imm() const { return CheckImmRange(16, 1, false, true, false); }
300 bool isu16_2Imm() const { return CheckImmRange(16, 2, false, true, false); }
301 bool isu16_3Imm() const { return CheckImmRange(16, 3, false, true, false); }
302 bool isu11_3Imm() const { return CheckImmRange(11, 3, false, false, false); }
303 bool isu6_0Imm() const { return CheckImmRange(6, 0, false, false, false); }
304 bool isu6_1Imm() const { return CheckImmRange(6, 1, false, false, false); }
305 bool isu6_2Imm() const { return CheckImmRange(6, 2, false, false, false); }
306 bool isu6_3Imm() const { return CheckImmRange(6, 3, false, false, false); }
307 bool isu10Imm() const { return CheckImmRange(10, 0, false, false, false); }
308 bool isu9Imm() const { return CheckImmRange(9, 0, false, false, false); }
309 bool isu8Imm() const { return CheckImmRange(8, 0, false, false, false); }
310 bool isu7Imm() const { return CheckImmRange(7, 0, false, false, false); }
311 bool isu6Imm() const { return CheckImmRange(6, 0, false, false, false); }
312 bool isu5Imm() const { return CheckImmRange(5, 0, false, false, false); }
313 bool isu4Imm() const { return CheckImmRange(4, 0, false, false, false); }
314 bool isu3Imm() const { return CheckImmRange(3, 0, false, false, false); }
315 bool isu2Imm() const { return CheckImmRange(2, 0, false, false, false); }
316 bool isu1Imm() const { return CheckImmRange(1, 0, false, false, false); }
317
318 bool ism6Imm() const { return CheckImmRange(6, 0, false, false, false); }
319 bool isn8Imm() const { return CheckImmRange(8, 0, false, false, false); }
320
321 bool iss16Ext() const { return CheckImmRange(16 + 26, 0, true, true, true); }
322 bool iss12Ext() const { return CheckImmRange(12 + 26, 0, true, true, true); }
323 bool iss10Ext() const { return CheckImmRange(10 + 26, 0, true, true, true); }
324 bool iss9Ext() const { return CheckImmRange(9 + 26, 0, true, true, true); }
325 bool iss8Ext() const { return CheckImmRange(8 + 26, 0, true, true, true); }
326 bool iss7Ext() const { return CheckImmRange(7 + 26, 0, true, true, true); }
327 bool iss6Ext() const { return CheckImmRange(6 + 26, 0, true, true, true); }
328 bool iss11_0Ext() const {
329 return CheckImmRange(11 + 26, 0, true, true, true);
330 }
331 bool iss11_1Ext() const {
332 return CheckImmRange(11 + 26, 1, true, true, true);
333 }
334 bool iss11_2Ext() const {
335 return CheckImmRange(11 + 26, 2, true, true, true);
336 }
337 bool iss11_3Ext() const {
338 return CheckImmRange(11 + 26, 3, true, true, true);
339 }
340
341 bool isu6Ext() const { return CheckImmRange(6 + 26, 0, false, true, true); }
342 bool isu7Ext() const { return CheckImmRange(7 + 26, 0, false, true, true); }
343 bool isu8Ext() const { return CheckImmRange(8 + 26, 0, false, true, true); }
344 bool isu9Ext() const { return CheckImmRange(9 + 26, 0, false, true, true); }
345 bool isu10Ext() const { return CheckImmRange(10 + 26, 0, false, true, true); }
346 bool isu6_0Ext() const { return CheckImmRange(6 + 26, 0, false, true, true); }
347 bool isu6_1Ext() const { return CheckImmRange(6 + 26, 1, false, true, true); }
348 bool isu6_2Ext() const { return CheckImmRange(6 + 26, 2, false, true, true); }
349 bool isu6_3Ext() const { return CheckImmRange(6 + 26, 3, false, true, true); }
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000350 bool isu32MustExt() const { return isImm(); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000351
352 void addRegOperands(MCInst &Inst, unsigned N) const {
353 assert(N == 1 && "Invalid number of operands!");
354 Inst.addOperand(MCOperand::createReg(getReg()));
355 }
356
357 void addImmOperands(MCInst &Inst, unsigned N) const {
358 assert(N == 1 && "Invalid number of operands!");
359 Inst.addOperand(MCOperand::createExpr(getImm()));
360 }
361
362 void addSignedImmOperands(MCInst &Inst, unsigned N) const {
363 assert(N == 1 && "Invalid number of operands!");
364 MCExpr const *Expr = getImm();
365 int64_t Value;
366 if (!Expr->evaluateAsAbsolute(Value)) {
367 Inst.addOperand(MCOperand::createExpr(Expr));
368 return;
369 }
370 int64_t Extended = SignExtend64 (Value, 32);
371 if ((Extended < 0) == (Value < 0)) {
372 Inst.addOperand(MCOperand::createExpr(Expr));
373 return;
374 }
375 // Flip bit 33 to signal signed unsigned mismatch
376 Extended ^= 0x100000000;
377 Inst.addOperand(MCOperand::createImm(Extended));
378 }
379
380 void addf32ExtOperands(MCInst &Inst, unsigned N) const {
381 addImmOperands(Inst, N);
382 }
383
384 void adds32ImmOperands(MCInst &Inst, unsigned N) const {
385 addSignedImmOperands(Inst, N);
386 }
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000387 void adds23_2ImmOperands(MCInst &Inst, unsigned N) const {
388 addSignedImmOperands(Inst, N);
389 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000390 void adds8ImmOperands(MCInst &Inst, unsigned N) const {
391 addSignedImmOperands(Inst, N);
392 }
393 void adds8Imm64Operands(MCInst &Inst, unsigned N) const {
394 addSignedImmOperands(Inst, N);
395 }
396 void adds6ImmOperands(MCInst &Inst, unsigned N) const {
397 addSignedImmOperands(Inst, N);
398 }
399 void adds4ImmOperands(MCInst &Inst, unsigned N) const {
400 addSignedImmOperands(Inst, N);
401 }
402 void adds4_0ImmOperands(MCInst &Inst, unsigned N) const {
403 addSignedImmOperands(Inst, N);
404 }
405 void adds4_1ImmOperands(MCInst &Inst, unsigned N) const {
406 addSignedImmOperands(Inst, N);
407 }
408 void adds4_2ImmOperands(MCInst &Inst, unsigned N) const {
409 addSignedImmOperands(Inst, N);
410 }
411 void adds4_3ImmOperands(MCInst &Inst, unsigned N) const {
412 addSignedImmOperands(Inst, N);
413 }
414 void adds3ImmOperands(MCInst &Inst, unsigned N) const {
415 addSignedImmOperands(Inst, N);
416 }
417
418 void addu64ImmOperands(MCInst &Inst, unsigned N) const {
419 addImmOperands(Inst, N);
420 }
421 void addu32ImmOperands(MCInst &Inst, unsigned N) const {
422 addImmOperands(Inst, N);
423 }
424 void addu26_6ImmOperands(MCInst &Inst, unsigned N) const {
425 addImmOperands(Inst, N);
426 }
427 void addu16ImmOperands(MCInst &Inst, unsigned N) const {
428 addImmOperands(Inst, N);
429 }
430 void addu16_0ImmOperands(MCInst &Inst, unsigned N) const {
431 addImmOperands(Inst, N);
432 }
433 void addu16_1ImmOperands(MCInst &Inst, unsigned N) const {
434 addImmOperands(Inst, N);
435 }
436 void addu16_2ImmOperands(MCInst &Inst, unsigned N) const {
437 addImmOperands(Inst, N);
438 }
439 void addu16_3ImmOperands(MCInst &Inst, unsigned N) const {
440 addImmOperands(Inst, N);
441 }
442 void addu11_3ImmOperands(MCInst &Inst, unsigned N) const {
443 addImmOperands(Inst, N);
444 }
445 void addu10ImmOperands(MCInst &Inst, unsigned N) const {
446 addImmOperands(Inst, N);
447 }
448 void addu9ImmOperands(MCInst &Inst, unsigned N) const {
449 addImmOperands(Inst, N);
450 }
451 void addu8ImmOperands(MCInst &Inst, unsigned N) const {
452 addImmOperands(Inst, N);
453 }
454 void addu7ImmOperands(MCInst &Inst, unsigned N) const {
455 addImmOperands(Inst, N);
456 }
457 void addu6ImmOperands(MCInst &Inst, unsigned N) const {
458 addImmOperands(Inst, N);
459 }
460 void addu6_0ImmOperands(MCInst &Inst, unsigned N) const {
461 addImmOperands(Inst, N);
462 }
463 void addu6_1ImmOperands(MCInst &Inst, unsigned N) const {
464 addImmOperands(Inst, N);
465 }
466 void addu6_2ImmOperands(MCInst &Inst, unsigned N) const {
467 addImmOperands(Inst, N);
468 }
469 void addu6_3ImmOperands(MCInst &Inst, unsigned N) const {
470 addImmOperands(Inst, N);
471 }
472 void addu5ImmOperands(MCInst &Inst, unsigned N) const {
473 addImmOperands(Inst, N);
474 }
475 void addu4ImmOperands(MCInst &Inst, unsigned N) const {
476 addImmOperands(Inst, N);
477 }
478 void addu3ImmOperands(MCInst &Inst, unsigned N) const {
479 addImmOperands(Inst, N);
480 }
481 void addu2ImmOperands(MCInst &Inst, unsigned N) const {
482 addImmOperands(Inst, N);
483 }
484 void addu1ImmOperands(MCInst &Inst, unsigned N) const {
485 addImmOperands(Inst, N);
486 }
487
488 void addm6ImmOperands(MCInst &Inst, unsigned N) const {
489 addImmOperands(Inst, N);
490 }
491 void addn8ImmOperands(MCInst &Inst, unsigned N) const {
492 addImmOperands(Inst, N);
493 }
494
495 void adds16ExtOperands(MCInst &Inst, unsigned N) const {
496 addSignedImmOperands(Inst, N);
497 }
498 void adds12ExtOperands(MCInst &Inst, unsigned N) const {
499 addSignedImmOperands(Inst, N);
500 }
501 void adds10ExtOperands(MCInst &Inst, unsigned N) const {
502 addSignedImmOperands(Inst, N);
503 }
504 void adds9ExtOperands(MCInst &Inst, unsigned N) const {
505 addSignedImmOperands(Inst, N);
506 }
507 void adds8ExtOperands(MCInst &Inst, unsigned N) const {
508 addSignedImmOperands(Inst, N);
509 }
510 void adds6ExtOperands(MCInst &Inst, unsigned N) const {
511 addSignedImmOperands(Inst, N);
512 }
513 void adds11_0ExtOperands(MCInst &Inst, unsigned N) const {
514 addSignedImmOperands(Inst, N);
515 }
516 void adds11_1ExtOperands(MCInst &Inst, unsigned N) const {
517 addSignedImmOperands(Inst, N);
518 }
519 void adds11_2ExtOperands(MCInst &Inst, unsigned N) const {
520 addSignedImmOperands(Inst, N);
521 }
522 void adds11_3ExtOperands(MCInst &Inst, unsigned N) const {
523 addSignedImmOperands(Inst, N);
524 }
525
526 void addu6ExtOperands(MCInst &Inst, unsigned N) const {
527 addImmOperands(Inst, N);
528 }
529 void addu7ExtOperands(MCInst &Inst, unsigned N) const {
530 addImmOperands(Inst, N);
531 }
532 void addu8ExtOperands(MCInst &Inst, unsigned N) const {
533 addImmOperands(Inst, N);
534 }
535 void addu9ExtOperands(MCInst &Inst, unsigned N) const {
536 addImmOperands(Inst, N);
537 }
538 void addu10ExtOperands(MCInst &Inst, unsigned N) const {
539 addImmOperands(Inst, N);
540 }
541 void addu6_0ExtOperands(MCInst &Inst, unsigned N) const {
542 addImmOperands(Inst, N);
543 }
544 void addu6_1ExtOperands(MCInst &Inst, unsigned N) const {
545 addImmOperands(Inst, N);
546 }
547 void addu6_2ExtOperands(MCInst &Inst, unsigned N) const {
548 addImmOperands(Inst, N);
549 }
550 void addu6_3ExtOperands(MCInst &Inst, unsigned N) const {
551 addImmOperands(Inst, N);
552 }
553 void addu32MustExtOperands(MCInst &Inst, unsigned N) const {
554 addImmOperands(Inst, N);
555 }
556
557 void adds4_6ImmOperands(MCInst &Inst, unsigned N) const {
558 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000559 const MCConstantExpr *CE =
560 dyn_cast<MCConstantExpr>(&HexagonMCInstrInfo::getExpr(*getImm()));
Colin LeMahieu4c606e62015-12-04 15:48:45 +0000561 Inst.addOperand(MCOperand::createImm(CE->getValue() * 64));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000562 }
563
564 void adds3_6ImmOperands(MCInst &Inst, unsigned N) const {
565 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000566 const MCConstantExpr *CE =
567 dyn_cast<MCConstantExpr>(&HexagonMCInstrInfo::getExpr(*getImm()));
Colin LeMahieu4c606e62015-12-04 15:48:45 +0000568 Inst.addOperand(MCOperand::createImm(CE->getValue() * 64));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000569 }
570
571 StringRef getToken() const {
572 assert(Kind == Token && "Invalid access!");
573 return StringRef(Tok.Data, Tok.Length);
574 }
575
576 virtual void print(raw_ostream &OS) const;
577
578 static std::unique_ptr<HexagonOperand> CreateToken(StringRef Str, SMLoc S) {
579 HexagonOperand *Op = new HexagonOperand(Token);
580 Op->Tok.Data = Str.data();
581 Op->Tok.Length = Str.size();
582 Op->StartLoc = S;
583 Op->EndLoc = S;
584 return std::unique_ptr<HexagonOperand>(Op);
585 }
586
587 static std::unique_ptr<HexagonOperand> CreateReg(unsigned RegNum, SMLoc S,
588 SMLoc E) {
589 HexagonOperand *Op = new HexagonOperand(Register);
590 Op->Reg.RegNum = RegNum;
591 Op->StartLoc = S;
592 Op->EndLoc = E;
593 return std::unique_ptr<HexagonOperand>(Op);
594 }
595
596 static std::unique_ptr<HexagonOperand> CreateImm(const MCExpr *Val, SMLoc S,
597 SMLoc E) {
598 HexagonOperand *Op = new HexagonOperand(Immediate);
599 Op->Imm.Val = Val;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000600 Op->StartLoc = S;
601 Op->EndLoc = E;
602 return std::unique_ptr<HexagonOperand>(Op);
603 }
604};
605
606} // end anonymous namespace.
607
608void HexagonOperand::print(raw_ostream &OS) const {
609 switch (Kind) {
610 case Immediate:
611 getImm()->print(OS, nullptr);
612 break;
613 case Register:
614 OS << "<register R";
615 OS << getReg() << ">";
616 break;
617 case Token:
618 OS << "'" << getToken() << "'";
619 break;
620 }
621}
622
623/// @name Auto-generated Match Functions
624static unsigned MatchRegisterName(StringRef Name);
625
626bool HexagonAsmParser::finishBundle(SMLoc IDLoc, MCStreamer &Out) {
627 DEBUG(dbgs() << "Bundle:");
628 DEBUG(MCB.dump_pretty(dbgs()));
629 DEBUG(dbgs() << "--\n");
630
631 // Check the bundle for errors.
632 const MCRegisterInfo *RI = getContext().getRegisterInfo();
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000633 HexagonMCChecker Check(MCII, getSTI(), MCB, MCB, *RI);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000634
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000635 bool CheckOk = HexagonMCInstrInfo::canonicalizePacket(MCII, getSTI(),
636 getContext(), MCB,
637 &Check);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000638
639 while (Check.getNextErrInfo() == true) {
640 unsigned Reg = Check.getErrRegister();
641 Twine R(RI->getName(Reg));
642
643 uint64_t Err = Check.getError();
644 if (Err != HexagonMCErrInfo::CHECK_SUCCESS) {
645 if (HexagonMCErrInfo::CHECK_ERROR_BRANCHES & Err)
646 Error(IDLoc,
647 "unconditional branch cannot precede another branch in packet");
648
649 if (HexagonMCErrInfo::CHECK_ERROR_NEWP & Err ||
650 HexagonMCErrInfo::CHECK_ERROR_NEWV & Err)
651 Error(IDLoc, "register `" + R +
652 "' used with `.new' "
653 "but not validly modified in the same packet");
654
655 if (HexagonMCErrInfo::CHECK_ERROR_REGISTERS & Err)
656 Error(IDLoc, "register `" + R + "' modified more than once");
657
658 if (HexagonMCErrInfo::CHECK_ERROR_READONLY & Err)
659 Error(IDLoc, "cannot write to read-only register `" + R + "'");
660
661 if (HexagonMCErrInfo::CHECK_ERROR_LOOP & Err)
662 Error(IDLoc, "loop-setup and some branch instructions "
663 "cannot be in the same packet");
664
665 if (HexagonMCErrInfo::CHECK_ERROR_ENDLOOP & Err) {
666 Twine N(HexagonMCInstrInfo::isInnerLoop(MCB) ? '0' : '1');
667 Error(IDLoc, "packet marked with `:endloop" + N + "' " +
668 "cannot contain instructions that modify register " +
669 "`" + R + "'");
670 }
671
672 if (HexagonMCErrInfo::CHECK_ERROR_SOLO & Err)
673 Error(IDLoc,
674 "instruction cannot appear in packet with other instructions");
675
676 if (HexagonMCErrInfo::CHECK_ERROR_NOSLOTS & Err)
677 Error(IDLoc, "too many slots used in packet");
678
679 if (Err & HexagonMCErrInfo::CHECK_ERROR_SHUFFLE) {
680 uint64_t Erm = Check.getShuffleError();
681
682 if (HexagonShuffler::SHUFFLE_ERROR_INVALID == Erm)
683 Error(IDLoc, "invalid instruction packet");
684 else if (HexagonShuffler::SHUFFLE_ERROR_STORES == Erm)
685 Error(IDLoc, "invalid instruction packet: too many stores");
686 else if (HexagonShuffler::SHUFFLE_ERROR_LOADS == Erm)
687 Error(IDLoc, "invalid instruction packet: too many loads");
688 else if (HexagonShuffler::SHUFFLE_ERROR_BRANCHES == Erm)
689 Error(IDLoc, "too many branches in packet");
690 else if (HexagonShuffler::SHUFFLE_ERROR_NOSLOTS == Erm)
691 Error(IDLoc, "invalid instruction packet: out of slots");
692 else if (HexagonShuffler::SHUFFLE_ERROR_SLOTS == Erm)
693 Error(IDLoc, "invalid instruction packet: slot error");
694 else if (HexagonShuffler::SHUFFLE_ERROR_ERRATA2 == Erm)
695 Error(IDLoc, "v60 packet violation");
696 else if (HexagonShuffler::SHUFFLE_ERROR_STORE_LOAD_CONFLICT == Erm)
697 Error(IDLoc, "slot 0 instruction does not allow slot 1 store");
698 else
699 Error(IDLoc, "unknown error in instruction packet");
700 }
701 }
702
703 unsigned Warn = Check.getWarning();
704 if (Warn != HexagonMCErrInfo::CHECK_SUCCESS) {
705 if (HexagonMCErrInfo::CHECK_WARN_CURRENT & Warn)
706 Warning(IDLoc, "register `" + R + "' used with `.cur' "
707 "but not used in the same packet");
708 else if (HexagonMCErrInfo::CHECK_WARN_TEMPORARY & Warn)
709 Warning(IDLoc, "register `" + R + "' used with `.tmp' "
710 "but not used in the same packet");
711 }
712 }
713
714 if (CheckOk) {
715 MCB.setLoc(IDLoc);
716 if (HexagonMCInstrInfo::bundleSize(MCB) == 0) {
717 assert(!HexagonMCInstrInfo::isInnerLoop(MCB));
718 assert(!HexagonMCInstrInfo::isOuterLoop(MCB));
719 // Empty packets are valid yet aren't emitted
720 return false;
721 }
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000722 Out.EmitInstruction(MCB, getSTI());
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000723 } else {
724 // If compounding and duplexing didn't reduce the size below
725 // 4 or less we have a packet that is too big.
726 if (HexagonMCInstrInfo::bundleSize(MCB) > HEXAGON_PACKET_SIZE) {
727 Error(IDLoc, "invalid instruction packet: out of slots");
728 return true; // Error
729 }
730 }
731
732 return false; // No error
733}
734
735bool HexagonAsmParser::matchBundleOptions() {
736 MCAsmParser &Parser = getParser();
737 MCAsmLexer &Lexer = getLexer();
738 while (true) {
739 if (!Parser.getTok().is(AsmToken::Colon))
740 return false;
741 Lexer.Lex();
742 StringRef Option = Parser.getTok().getString();
743 if (Option.compare_lower("endloop0") == 0)
744 HexagonMCInstrInfo::setInnerLoop(MCB);
745 else if (Option.compare_lower("endloop1") == 0)
746 HexagonMCInstrInfo::setOuterLoop(MCB);
747 else if (Option.compare_lower("mem_noshuf") == 0)
748 HexagonMCInstrInfo::setMemReorderDisabled(MCB);
749 else if (Option.compare_lower("mem_shuf") == 0)
750 HexagonMCInstrInfo::setMemStoreReorderEnabled(MCB);
751 else
752 return true;
753 Lexer.Lex();
754 }
755}
756
757// For instruction aliases, immediates are generated rather than
758// MCConstantExpr. Convert them for uniform MCExpr.
759// Also check for signed/unsigned mismatches and warn
760void HexagonAsmParser::canonicalizeImmediates(MCInst &MCI) {
761 MCInst NewInst;
762 NewInst.setOpcode(MCI.getOpcode());
763 for (MCOperand &I : MCI)
764 if (I.isImm()) {
765 int64_t Value (I.getImm());
766 if ((Value & 0x100000000) != (Value & 0x80000000)) {
767 // Detect flipped bit 33 wrt bit 32 and signal warning
768 Value ^= 0x100000000;
769 if (WarnSignedMismatch)
770 Warning (MCI.getLoc(), "Signed/Unsigned mismatch");
771 }
Colin LeMahieuc7b21242016-02-15 18:47:55 +0000772 NewInst.addOperand(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000773 MCConstantExpr::create(Value, getContext()), getContext())));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000774 }
775 else
776 NewInst.addOperand(I);
777 MCI = NewInst;
778}
779
780bool HexagonAsmParser::matchOneInstruction(MCInst &MCI, SMLoc IDLoc,
781 OperandVector &InstOperands,
782 uint64_t &ErrorInfo,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000783 bool MatchingInlineAsm) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000784 // Perform matching with tablegen asmmatcher generated function
785 int result =
786 MatchInstructionImpl(InstOperands, MCI, ErrorInfo, MatchingInlineAsm);
787 if (result == Match_Success) {
788 MCI.setLoc(IDLoc);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000789 canonicalizeImmediates(MCI);
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000790 result = processInstruction(MCI, InstOperands, IDLoc);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000791
792 DEBUG(dbgs() << "Insn:");
793 DEBUG(MCI.dump_pretty(dbgs()));
794 DEBUG(dbgs() << "\n\n");
795
796 MCI.setLoc(IDLoc);
797 }
798
799 // Create instruction operand for bundle instruction
800 // Break this into a separate function Code here is less readable
801 // Think about how to get an instruction error to report correctly.
802 // SMLoc will return the "{"
803 switch (result) {
804 default:
805 break;
806 case Match_Success:
807 return false;
808 case Match_MissingFeature:
809 return Error(IDLoc, "invalid instruction");
810 case Match_MnemonicFail:
811 return Error(IDLoc, "unrecognized instruction");
812 case Match_InvalidOperand:
813 SMLoc ErrorLoc = IDLoc;
814 if (ErrorInfo != ~0U) {
815 if (ErrorInfo >= InstOperands.size())
816 return Error(IDLoc, "too few operands for instruction");
817
818 ErrorLoc = (static_cast<HexagonOperand *>(InstOperands[ErrorInfo].get()))
819 ->getStartLoc();
820 if (ErrorLoc == SMLoc())
821 ErrorLoc = IDLoc;
822 }
823 return Error(ErrorLoc, "invalid operand for instruction");
824 }
825 llvm_unreachable("Implement any new match types added!");
826}
827
828bool HexagonAsmParser::mustExtend(OperandVector &Operands) {
829 unsigned Count = 0;
830 for (std::unique_ptr<MCParsedAsmOperand> &i : Operands)
831 if (i->isImm())
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000832 if (HexagonMCInstrInfo::mustExtend(
833 *static_cast<HexagonOperand *>(i.get())->Imm.Val))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000834 ++Count;
835 // Multiple extenders should have been filtered by iss9Ext et. al.
836 assert(Count < 2 && "Multiple extenders");
837 return Count == 1;
838}
839
840bool HexagonAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
841 OperandVector &Operands,
842 MCStreamer &Out,
843 uint64_t &ErrorInfo,
844 bool MatchingInlineAsm) {
845 if (!InBrackets) {
846 MCB.clear();
847 MCB.addOperand(MCOperand::createImm(0));
848 }
849 HexagonOperand &FirstOperand = static_cast<HexagonOperand &>(*Operands[0]);
850 if (FirstOperand.isToken() && FirstOperand.getToken() == "{") {
851 assert(Operands.size() == 1 && "Brackets should be by themselves");
852 if (InBrackets) {
853 getParser().Error(IDLoc, "Already in a packet");
854 return true;
855 }
856 InBrackets = true;
857 return false;
858 }
859 if (FirstOperand.isToken() && FirstOperand.getToken() == "}") {
860 assert(Operands.size() == 1 && "Brackets should be by themselves");
861 if (!InBrackets) {
862 getParser().Error(IDLoc, "Not in a packet");
863 return true;
864 }
865 InBrackets = false;
866 if (matchBundleOptions())
867 return true;
868 return finishBundle(IDLoc, Out);
869 }
870 MCInst *SubInst = new (getParser().getContext()) MCInst;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000871 if (matchOneInstruction(*SubInst, IDLoc, Operands, ErrorInfo,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000872 MatchingInlineAsm))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000873 return true;
874 HexagonMCInstrInfo::extendIfNeeded(
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000875 getParser().getContext(), MCII, MCB, *SubInst);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000876 MCB.addOperand(MCOperand::createInst(SubInst));
877 if (!InBrackets)
878 return finishBundle(IDLoc, Out);
879 return false;
880}
881
882/// ParseDirective parses the Hexagon specific directives
883bool HexagonAsmParser::ParseDirective(AsmToken DirectiveID) {
884 StringRef IDVal = DirectiveID.getIdentifier();
885 if ((IDVal.lower() == ".word") || (IDVal.lower() == ".4byte"))
886 return ParseDirectiveValue(4, DirectiveID.getLoc());
887 if (IDVal.lower() == ".short" || IDVal.lower() == ".hword" ||
888 IDVal.lower() == ".half")
889 return ParseDirectiveValue(2, DirectiveID.getLoc());
890 if (IDVal.lower() == ".falign")
891 return ParseDirectiveFalign(256, DirectiveID.getLoc());
892 if ((IDVal.lower() == ".lcomm") || (IDVal.lower() == ".lcommon"))
893 return ParseDirectiveComm(true, DirectiveID.getLoc());
894 if ((IDVal.lower() == ".comm") || (IDVal.lower() == ".common"))
895 return ParseDirectiveComm(false, DirectiveID.getLoc());
896 if (IDVal.lower() == ".subsection")
897 return ParseDirectiveSubsection(DirectiveID.getLoc());
898
899 return true;
900}
901bool HexagonAsmParser::ParseDirectiveSubsection(SMLoc L) {
902 const MCExpr *Subsection = 0;
903 int64_t Res;
904
905 assert((getLexer().isNot(AsmToken::EndOfStatement)) &&
906 "Invalid subsection directive");
907 getParser().parseExpression(Subsection);
908
909 if (!Subsection->evaluateAsAbsolute(Res))
910 return Error(L, "Cannot evaluate subsection number");
911
912 if (getLexer().isNot(AsmToken::EndOfStatement))
913 return TokError("unexpected token in directive");
914
915 // 0-8192 is the hard-coded range in MCObjectStreamper.cpp, this keeps the
916 // negative subsections together and in the same order but at the opposite
917 // end of the section. Only legacy hexagon-gcc created assembly code
918 // used negative subsections.
919 if ((Res < 0) && (Res > -8193))
Colin LeMahieuc7b21242016-02-15 18:47:55 +0000920 Subsection = HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000921 MCConstantExpr::create(8192 + Res, getContext()), getContext());
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000922
923 getStreamer().SubSection(Subsection);
924 return false;
925}
926
927/// ::= .falign [expression]
928bool HexagonAsmParser::ParseDirectiveFalign(unsigned Size, SMLoc L) {
929
930 int64_t MaxBytesToFill = 15;
931
932 // if there is an arguement
933 if (getLexer().isNot(AsmToken::EndOfStatement)) {
934 const MCExpr *Value;
935 SMLoc ExprLoc = L;
936
937 // Make sure we have a number (false is returned if expression is a number)
938 if (getParser().parseExpression(Value) == false) {
939 // Make sure this is a number that is in range
940 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
941 uint64_t IntValue = MCE->getValue();
942 if (!isUIntN(Size, IntValue) && !isIntN(Size, IntValue))
943 return Error(ExprLoc, "literal value out of range (256) for falign");
944 MaxBytesToFill = IntValue;
945 Lex();
946 } else {
947 return Error(ExprLoc, "not a valid expression for falign directive");
948 }
949 }
950
951 getTargetStreamer().emitFAlign(16, MaxBytesToFill);
952 Lex();
953
954 return false;
955}
956
957/// ::= .word [ expression (, expression)* ]
958bool HexagonAsmParser::ParseDirectiveValue(unsigned Size, SMLoc L) {
959 if (getLexer().isNot(AsmToken::EndOfStatement)) {
960
961 for (;;) {
962 const MCExpr *Value;
963 SMLoc ExprLoc = L;
964 if (getParser().parseExpression(Value))
965 return true;
966
967 // Special case constant expressions to match code generator.
968 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
969 assert(Size <= 8 && "Invalid size");
970 uint64_t IntValue = MCE->getValue();
971 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
972 return Error(ExprLoc, "literal value out of range for directive");
973 getStreamer().EmitIntValue(IntValue, Size);
974 } else
975 getStreamer().EmitValue(Value, Size);
976
977 if (getLexer().is(AsmToken::EndOfStatement))
978 break;
979
980 // FIXME: Improve diagnostic.
981 if (getLexer().isNot(AsmToken::Comma))
982 return TokError("unexpected token in directive");
983 Lex();
984 }
985 }
986
987 Lex();
988 return false;
989}
990
991// This is largely a copy of AsmParser's ParseDirectiveComm extended to
992// accept a 3rd argument, AccessAlignment which indicates the smallest
993// memory access made to the symbol, expressed in bytes. If no
994// AccessAlignment is specified it defaults to the Alignment Value.
995// Hexagon's .lcomm:
996// .lcomm Symbol, Length, Alignment, AccessAlignment
997bool HexagonAsmParser::ParseDirectiveComm(bool IsLocal, SMLoc Loc) {
998 // FIXME: need better way to detect if AsmStreamer (upstream removed
999 // getKind())
1000 if (getStreamer().hasRawTextSupport())
1001 return true; // Only object file output requires special treatment.
1002
1003 StringRef Name;
1004 if (getParser().parseIdentifier(Name))
1005 return TokError("expected identifier in directive");
1006 // Handle the identifier as the key symbol.
1007 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
1008
1009 if (getLexer().isNot(AsmToken::Comma))
1010 return TokError("unexpected token in directive");
1011 Lex();
1012
1013 int64_t Size;
1014 SMLoc SizeLoc = getLexer().getLoc();
1015 if (getParser().parseAbsoluteExpression(Size))
1016 return true;
1017
1018 int64_t ByteAlignment = 1;
1019 SMLoc ByteAlignmentLoc;
1020 if (getLexer().is(AsmToken::Comma)) {
1021 Lex();
1022 ByteAlignmentLoc = getLexer().getLoc();
1023 if (getParser().parseAbsoluteExpression(ByteAlignment))
1024 return true;
1025 if (!isPowerOf2_64(ByteAlignment))
1026 return Error(ByteAlignmentLoc, "alignment must be a power of 2");
1027 }
1028
1029 int64_t AccessAlignment = 0;
1030 if (getLexer().is(AsmToken::Comma)) {
1031 // The optional access argument specifies the size of the smallest memory
1032 // access to be made to the symbol, expressed in bytes.
1033 SMLoc AccessAlignmentLoc;
1034 Lex();
1035 AccessAlignmentLoc = getLexer().getLoc();
1036 if (getParser().parseAbsoluteExpression(AccessAlignment))
1037 return true;
1038
1039 if (!isPowerOf2_64(AccessAlignment))
1040 return Error(AccessAlignmentLoc, "access alignment must be a power of 2");
1041 }
1042
1043 if (getLexer().isNot(AsmToken::EndOfStatement))
1044 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1045
1046 Lex();
1047
1048 // NOTE: a size of zero for a .comm should create a undefined symbol
1049 // but a size of .lcomm creates a bss symbol of size zero.
1050 if (Size < 0)
1051 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1052 "be less than zero");
1053
1054 // NOTE: The alignment in the directive is a power of 2 value, the assembler
1055 // may internally end up wanting an alignment in bytes.
1056 // FIXME: Diagnose overflow.
1057 if (ByteAlignment < 0)
1058 return Error(ByteAlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1059 "alignment, can't be less than zero");
1060
1061 if (!Sym->isUndefined())
1062 return Error(Loc, "invalid symbol redefinition");
1063
1064 HexagonMCELFStreamer &HexagonELFStreamer =
1065 static_cast<HexagonMCELFStreamer &>(getStreamer());
1066 if (IsLocal) {
1067 HexagonELFStreamer.HexagonMCEmitLocalCommonSymbol(Sym, Size, ByteAlignment,
1068 AccessAlignment);
1069 return false;
1070 }
1071
1072 HexagonELFStreamer.HexagonMCEmitCommonSymbol(Sym, Size, ByteAlignment,
1073 AccessAlignment);
1074 return false;
1075}
1076
1077// validate register against architecture
1078bool HexagonAsmParser::RegisterMatchesArch(unsigned MatchNum) const {
1079 return true;
1080}
1081
1082// extern "C" void LLVMInitializeHexagonAsmLexer();
1083
1084/// Force static initialization.
1085extern "C" void LLVMInitializeHexagonAsmParser() {
1086 RegisterMCAsmParser<HexagonAsmParser> X(TheHexagonTarget);
1087}
1088
1089#define GET_MATCHER_IMPLEMENTATION
1090#define GET_REGISTER_MATCHER
1091#include "HexagonGenAsmMatcher.inc"
1092
1093namespace {
1094bool previousEqual(OperandVector &Operands, size_t Index, StringRef String) {
1095 if (Index >= Operands.size())
1096 return false;
1097 MCParsedAsmOperand &Operand = *Operands[Operands.size() - Index - 1];
1098 if (!Operand.isToken())
1099 return false;
1100 return static_cast<HexagonOperand &>(Operand).getToken().equals_lower(String);
1101}
1102bool previousIsLoop(OperandVector &Operands, size_t Index) {
1103 return previousEqual(Operands, Index, "loop0") ||
1104 previousEqual(Operands, Index, "loop1") ||
1105 previousEqual(Operands, Index, "sp1loop0") ||
1106 previousEqual(Operands, Index, "sp2loop0") ||
1107 previousEqual(Operands, Index, "sp3loop0");
1108}
1109}
1110
1111bool HexagonAsmParser::splitIdentifier(OperandVector &Operands) {
1112 AsmToken const &Token = getParser().getTok();
1113 StringRef String = Token.getString();
1114 SMLoc Loc = Token.getLoc();
1115 getLexer().Lex();
1116 do {
1117 std::pair<StringRef, StringRef> HeadTail = String.split('.');
1118 if (!HeadTail.first.empty())
1119 Operands.push_back(HexagonOperand::CreateToken(HeadTail.first, Loc));
1120 if (!HeadTail.second.empty())
1121 Operands.push_back(HexagonOperand::CreateToken(
1122 String.substr(HeadTail.first.size(), 1), Loc));
1123 String = HeadTail.second;
1124 } while (!String.empty());
1125 return false;
1126}
1127
1128bool HexagonAsmParser::parseOperand(OperandVector &Operands) {
1129 unsigned Register;
1130 SMLoc Begin;
1131 SMLoc End;
1132 MCAsmLexer &Lexer = getLexer();
1133 if (!ParseRegister(Register, Begin, End)) {
1134 if (!ErrorMissingParenthesis)
1135 switch (Register) {
1136 default:
1137 break;
1138 case Hexagon::P0:
1139 case Hexagon::P1:
1140 case Hexagon::P2:
1141 case Hexagon::P3:
1142 if (previousEqual(Operands, 0, "if")) {
1143 if (WarnMissingParenthesis)
1144 Warning (Begin, "Missing parenthesis around predicate register");
1145 static char const *LParen = "(";
1146 static char const *RParen = ")";
1147 Operands.push_back(HexagonOperand::CreateToken(LParen, Begin));
1148 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End));
1149 AsmToken MaybeDotNew = Lexer.getTok();
1150 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) &&
1151 MaybeDotNew.getString().equals_lower(".new"))
1152 splitIdentifier(Operands);
1153 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin));
1154 return false;
1155 }
1156 if (previousEqual(Operands, 0, "!") &&
1157 previousEqual(Operands, 1, "if")) {
1158 if (WarnMissingParenthesis)
1159 Warning (Begin, "Missing parenthesis around predicate register");
1160 static char const *LParen = "(";
1161 static char const *RParen = ")";
1162 Operands.insert(Operands.end () - 1,
1163 HexagonOperand::CreateToken(LParen, Begin));
1164 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End));
1165 AsmToken MaybeDotNew = Lexer.getTok();
1166 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) &&
1167 MaybeDotNew.getString().equals_lower(".new"))
1168 splitIdentifier(Operands);
1169 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin));
1170 return false;
1171 }
1172 break;
1173 }
1174 Operands.push_back(HexagonOperand::CreateReg(
1175 Register, Begin, End));
1176 return false;
1177 }
1178 return splitIdentifier(Operands);
1179}
1180
1181bool HexagonAsmParser::isLabel(AsmToken &Token) {
1182 MCAsmLexer &Lexer = getLexer();
1183 AsmToken const &Second = Lexer.getTok();
1184 AsmToken Third = Lexer.peekTok();
1185 StringRef String = Token.getString();
1186 if (Token.is(AsmToken::TokenKind::LCurly) ||
1187 Token.is(AsmToken::TokenKind::RCurly))
1188 return false;
1189 if (!Token.is(AsmToken::TokenKind::Identifier))
1190 return true;
1191 if (!MatchRegisterName(String.lower()))
1192 return true;
Colin LeMahieu9d851f02015-11-09 21:06:28 +00001193 (void)Second;
1194 assert(Second.is(AsmToken::Colon));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001195 StringRef Raw (String.data(), Third.getString().data() - String.data() +
1196 Third.getString().size());
1197 std::string Collapsed = Raw;
1198 Collapsed.erase(std::remove_if(Collapsed.begin(), Collapsed.end(), isspace),
1199 Collapsed.end());
1200 StringRef Whole = Collapsed;
1201 std::pair<StringRef, StringRef> DotSplit = Whole.split('.');
1202 if (!MatchRegisterName(DotSplit.first.lower()))
1203 return true;
1204 return false;
1205}
1206
1207bool HexagonAsmParser::handleNoncontigiousRegister(bool Contigious, SMLoc &Loc) {
1208 if (!Contigious && ErrorNoncontigiousRegister) {
1209 Error(Loc, "Register name is not contigious");
1210 return true;
1211 }
1212 if (!Contigious && WarnNoncontigiousRegister)
1213 Warning(Loc, "Register name is not contigious");
1214 return false;
1215}
1216
1217bool HexagonAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) {
1218 MCAsmLexer &Lexer = getLexer();
1219 StartLoc = getLexer().getLoc();
1220 SmallVector<AsmToken, 5> Lookahead;
1221 StringRef RawString(Lexer.getTok().getString().data(), 0);
1222 bool Again = Lexer.is(AsmToken::Identifier);
1223 bool NeededWorkaround = false;
1224 while (Again) {
1225 AsmToken const &Token = Lexer.getTok();
1226 RawString = StringRef(RawString.data(),
1227 Token.getString().data() - RawString.data () +
1228 Token.getString().size());
1229 Lookahead.push_back(Token);
1230 Lexer.Lex();
1231 bool Contigious = Lexer.getTok().getString().data() ==
1232 Lookahead.back().getString().data() +
1233 Lookahead.back().getString().size();
1234 bool Type = Lexer.is(AsmToken::Identifier) || Lexer.is(AsmToken::Dot) ||
1235 Lexer.is(AsmToken::Integer) || Lexer.is(AsmToken::Real) ||
1236 Lexer.is(AsmToken::Colon);
1237 bool Workaround = Lexer.is(AsmToken::Colon) ||
1238 Lookahead.back().is(AsmToken::Colon);
1239 Again = (Contigious && Type) || (Workaround && Type);
1240 NeededWorkaround = NeededWorkaround || (Again && !(Contigious && Type));
1241 }
1242 std::string Collapsed = RawString;
1243 Collapsed.erase(std::remove_if(Collapsed.begin(), Collapsed.end(), isspace),
1244 Collapsed.end());
1245 StringRef FullString = Collapsed;
1246 std::pair<StringRef, StringRef> DotSplit = FullString.split('.');
1247 unsigned DotReg = MatchRegisterName(DotSplit.first.lower());
1248 if (DotReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1249 if (DotSplit.second.empty()) {
1250 RegNo = DotReg;
1251 EndLoc = Lexer.getLoc();
1252 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1253 return true;
1254 return false;
1255 } else {
1256 RegNo = DotReg;
1257 size_t First = RawString.find('.');
1258 StringRef DotString (RawString.data() + First, RawString.size() - First);
1259 Lexer.UnLex(AsmToken(AsmToken::Identifier, DotString));
1260 EndLoc = Lexer.getLoc();
1261 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1262 return true;
1263 return false;
1264 }
1265 }
1266 std::pair<StringRef, StringRef> ColonSplit = StringRef(FullString).split(':');
1267 unsigned ColonReg = MatchRegisterName(ColonSplit.first.lower());
1268 if (ColonReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1269 Lexer.UnLex(Lookahead.back());
1270 Lookahead.pop_back();
1271 Lexer.UnLex(Lookahead.back());
1272 Lookahead.pop_back();
1273 RegNo = ColonReg;
1274 EndLoc = Lexer.getLoc();
1275 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1276 return true;
1277 return false;
1278 }
1279 while (!Lookahead.empty()) {
1280 Lexer.UnLex(Lookahead.back());
1281 Lookahead.pop_back();
1282 }
1283 return true;
1284}
1285
1286bool HexagonAsmParser::implicitExpressionLocation(OperandVector &Operands) {
1287 if (previousEqual(Operands, 0, "call"))
1288 return true;
1289 if (previousEqual(Operands, 0, "jump"))
1290 if (!getLexer().getTok().is(AsmToken::Colon))
1291 return true;
1292 if (previousEqual(Operands, 0, "(") && previousIsLoop(Operands, 1))
1293 return true;
1294 if (previousEqual(Operands, 1, ":") && previousEqual(Operands, 2, "jump") &&
1295 (previousEqual(Operands, 0, "nt") || previousEqual(Operands, 0, "t")))
1296 return true;
1297 return false;
1298}
1299
1300bool HexagonAsmParser::parseExpression(MCExpr const *& Expr) {
1301 llvm::SmallVector<AsmToken, 4> Tokens;
1302 MCAsmLexer &Lexer = getLexer();
1303 bool Done = false;
1304 static char const * Comma = ",";
1305 do {
1306 Tokens.emplace_back (Lexer.getTok());
1307 Lexer.Lex();
1308 switch (Tokens.back().getKind())
1309 {
1310 case AsmToken::TokenKind::Hash:
1311 if (Tokens.size () > 1)
1312 if ((Tokens.end () - 2)->getKind() == AsmToken::TokenKind::Plus) {
1313 Tokens.insert(Tokens.end() - 2,
1314 AsmToken(AsmToken::TokenKind::Comma, Comma));
1315 Done = true;
1316 }
1317 break;
1318 case AsmToken::TokenKind::RCurly:
1319 case AsmToken::TokenKind::EndOfStatement:
1320 case AsmToken::TokenKind::Eof:
1321 Done = true;
1322 break;
1323 default:
1324 break;
1325 }
1326 } while (!Done);
1327 while (!Tokens.empty()) {
1328 Lexer.UnLex(Tokens.back());
1329 Tokens.pop_back();
1330 }
1331 return getParser().parseExpression(Expr);
1332}
1333
1334bool HexagonAsmParser::parseExpressionOrOperand(OperandVector &Operands) {
1335 if (implicitExpressionLocation(Operands)) {
1336 MCAsmParser &Parser = getParser();
1337 SMLoc Loc = Parser.getLexer().getLoc();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001338 MCExpr const *Expr = nullptr;
1339 bool Error = parseExpression(Expr);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001340 Expr = HexagonMCExpr::create(Expr, getContext());
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001341 if (!Error)
1342 Operands.push_back(HexagonOperand::CreateImm(Expr, Loc, Loc));
1343 return Error;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001344 }
1345 return parseOperand(Operands);
1346}
1347
1348/// Parse an instruction.
1349bool HexagonAsmParser::parseInstruction(OperandVector &Operands) {
1350 MCAsmParser &Parser = getParser();
1351 MCAsmLexer &Lexer = getLexer();
1352 while (true) {
1353 AsmToken const &Token = Parser.getTok();
1354 switch (Token.getKind()) {
1355 case AsmToken::EndOfStatement: {
1356 Lexer.Lex();
1357 return false;
1358 }
1359 case AsmToken::LCurly: {
1360 if (!Operands.empty())
1361 return true;
1362 Operands.push_back(
1363 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
1364 Lexer.Lex();
1365 return false;
1366 }
1367 case AsmToken::RCurly: {
1368 if (Operands.empty()) {
1369 Operands.push_back(
1370 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
1371 Lexer.Lex();
1372 }
1373 return false;
1374 }
1375 case AsmToken::Comma: {
1376 Lexer.Lex();
1377 continue;
1378 }
1379 case AsmToken::EqualEqual:
1380 case AsmToken::ExclaimEqual:
1381 case AsmToken::GreaterEqual:
1382 case AsmToken::GreaterGreater:
1383 case AsmToken::LessEqual:
1384 case AsmToken::LessLess: {
1385 Operands.push_back(HexagonOperand::CreateToken(
1386 Token.getString().substr(0, 1), Token.getLoc()));
1387 Operands.push_back(HexagonOperand::CreateToken(
1388 Token.getString().substr(1, 1), Token.getLoc()));
1389 Lexer.Lex();
1390 continue;
1391 }
1392 case AsmToken::Hash: {
1393 bool MustNotExtend = false;
1394 bool ImplicitExpression = implicitExpressionLocation(Operands);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001395 SMLoc ExprLoc = Lexer.getLoc();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001396 if (!ImplicitExpression)
1397 Operands.push_back(
1398 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
1399 Lexer.Lex();
1400 bool MustExtend = false;
1401 bool HiOnly = false;
1402 bool LoOnly = false;
1403 if (Lexer.is(AsmToken::Hash)) {
1404 Lexer.Lex();
1405 MustExtend = true;
1406 } else if (ImplicitExpression)
1407 MustNotExtend = true;
1408 AsmToken const &Token = Parser.getTok();
1409 if (Token.is(AsmToken::Identifier)) {
1410 StringRef String = Token.getString();
1411 AsmToken IDToken = Token;
1412 if (String.lower() == "hi") {
1413 HiOnly = true;
1414 } else if (String.lower() == "lo") {
1415 LoOnly = true;
1416 }
1417 if (HiOnly || LoOnly) {
1418 AsmToken LParen = Lexer.peekTok();
1419 if (!LParen.is(AsmToken::LParen)) {
1420 HiOnly = false;
1421 LoOnly = false;
1422 } else {
1423 Lexer.Lex();
1424 }
1425 }
1426 }
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001427 MCExpr const *Expr = nullptr;
1428 if (parseExpression(Expr))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001429 return true;
1430 int64_t Value;
1431 MCContext &Context = Parser.getContext();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001432 assert(Expr != nullptr);
1433 if (Expr->evaluateAsAbsolute(Value)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001434 if (HiOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001435 Expr = MCBinaryExpr::createLShr(
1436 Expr, MCConstantExpr::create(16, Context), Context);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001437 if (HiOnly || LoOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001438 Expr = MCBinaryExpr::createAnd(Expr,
1439 MCConstantExpr::create(0xffff, Context),
1440 Context);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001441 }
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001442 Expr = HexagonMCExpr::create(Expr, Context);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001443 HexagonMCInstrInfo::setMustNotExtend(*Expr, MustNotExtend);
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001444 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001445 std::unique_ptr<HexagonOperand> Operand =
1446 HexagonOperand::CreateImm(Expr, ExprLoc, ExprLoc);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001447 Operands.push_back(std::move(Operand));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001448 continue;
1449 }
1450 default:
1451 break;
1452 }
1453 if (parseExpressionOrOperand(Operands))
1454 return true;
1455 }
1456}
1457
1458bool HexagonAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1459 StringRef Name,
1460 AsmToken ID,
1461 OperandVector &Operands) {
1462 getLexer().UnLex(ID);
1463 return parseInstruction(Operands);
1464}
1465
1466namespace {
1467MCInst makeCombineInst(int opCode, MCOperand &Rdd,
1468 MCOperand &MO1, MCOperand &MO2) {
1469 MCInst TmpInst;
1470 TmpInst.setOpcode(opCode);
1471 TmpInst.addOperand(Rdd);
1472 TmpInst.addOperand(MO1);
1473 TmpInst.addOperand(MO2);
1474
1475 return TmpInst;
1476}
1477}
1478
1479// Define this matcher function after the auto-generated include so we
1480// have the match class enum definitions.
1481unsigned HexagonAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
1482 unsigned Kind) {
1483 HexagonOperand *Op = static_cast<HexagonOperand *>(&AsmOp);
1484
1485 switch (Kind) {
1486 case MCK_0: {
1487 int64_t Value;
1488 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 0
1489 ? Match_Success
1490 : Match_InvalidOperand;
1491 }
1492 case MCK_1: {
1493 int64_t Value;
1494 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 1
1495 ? Match_Success
1496 : Match_InvalidOperand;
1497 }
1498 case MCK__MINUS_1: {
1499 int64_t Value;
1500 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == -1
1501 ? Match_Success
1502 : Match_InvalidOperand;
1503 }
1504 }
1505 if (Op->Kind == HexagonOperand::Token && Kind != InvalidMatchClass) {
1506 StringRef myStringRef = StringRef(Op->Tok.Data, Op->Tok.Length);
1507 if (matchTokenString(myStringRef.lower()) == (MatchClassKind)Kind)
1508 return Match_Success;
1509 if (matchTokenString(myStringRef.upper()) == (MatchClassKind)Kind)
1510 return Match_Success;
1511 }
1512
1513 DEBUG(dbgs() << "Unmatched Operand:");
1514 DEBUG(Op->dump());
1515 DEBUG(dbgs() << "\n");
1516
1517 return Match_InvalidOperand;
1518}
1519
1520void HexagonAsmParser::OutOfRange(SMLoc IDLoc, long long Val, long long Max) {
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001521 std::string errStr;
1522 raw_string_ostream ES(errStr);
Alexey Samsonov44ff2042015-12-02 22:59:22 +00001523 ES << "value " << Val << "(" << format_hex(Val, 0) << ") out of range: ";
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001524 if (Max >= 0)
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001525 ES << "0-" << Max;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001526 else
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001527 ES << Max << "-" << (-Max - 1);
1528 Error(IDLoc, ES.str().c_str());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001529}
1530
1531int HexagonAsmParser::processInstruction(MCInst &Inst,
1532 OperandVector const &Operands,
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001533 SMLoc IDLoc) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001534 MCContext &Context = getParser().getContext();
1535 const MCRegisterInfo *RI = getContext().getRegisterInfo();
1536 std::string r = "r";
1537 std::string v = "v";
1538 std::string Colon = ":";
1539
1540 bool is32bit = false; // used to distinguish between CONST32 and CONST64
1541 switch (Inst.getOpcode()) {
1542 default:
1543 break;
1544
Colin LeMahieuecef1d92016-02-16 20:38:17 +00001545 case Hexagon::A2_iconst: {
1546 Inst.setOpcode(Hexagon::A2_addi);
1547 MCOperand Reg = Inst.getOperand(0);
1548 MCOperand S16 = Inst.getOperand(1);
1549 HexagonMCInstrInfo::setMustNotExtend(*S16.getExpr());
1550 HexagonMCInstrInfo::setS23_2_reloc(*S16.getExpr());
1551 Inst.clear();
1552 Inst.addOperand(Reg);
1553 Inst.addOperand(MCOperand::createReg(Hexagon::R0));
1554 Inst.addOperand(S16);
1555 break;
1556 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001557 case Hexagon::M4_mpyrr_addr:
1558 case Hexagon::S4_addi_asl_ri:
1559 case Hexagon::S4_addi_lsr_ri:
1560 case Hexagon::S4_andi_asl_ri:
1561 case Hexagon::S4_andi_lsr_ri:
1562 case Hexagon::S4_ori_asl_ri:
1563 case Hexagon::S4_ori_lsr_ri:
1564 case Hexagon::S4_or_andix:
1565 case Hexagon::S4_subi_asl_ri:
1566 case Hexagon::S4_subi_lsr_ri: {
1567 MCOperand &Ry = Inst.getOperand(0);
1568 MCOperand &src = Inst.getOperand(2);
1569 if (RI->getEncodingValue(Ry.getReg()) != RI->getEncodingValue(src.getReg()))
1570 return Match_InvalidOperand;
1571 break;
1572 }
1573
1574 case Hexagon::C2_cmpgei: {
1575 MCOperand &MO = Inst.getOperand(2);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001576 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001577 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001578 Inst.setOpcode(Hexagon::C2_cmpgti);
1579 break;
1580 }
1581
1582 case Hexagon::C2_cmpgeui: {
1583 MCOperand &MO = Inst.getOperand(2);
1584 int64_t Value;
1585 bool Success = MO.getExpr()->evaluateAsAbsolute(Value);
Colin LeMahieu9d851f02015-11-09 21:06:28 +00001586 (void)Success;
1587 assert(Success && "Assured by matcher");
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001588 if (Value == 0) {
1589 MCInst TmpInst;
1590 MCOperand &Pd = Inst.getOperand(0);
1591 MCOperand &Rt = Inst.getOperand(1);
1592 TmpInst.setOpcode(Hexagon::C2_cmpeq);
1593 TmpInst.addOperand(Pd);
1594 TmpInst.addOperand(Rt);
1595 TmpInst.addOperand(Rt);
1596 Inst = TmpInst;
1597 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001598 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001599 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001600 Inst.setOpcode(Hexagon::C2_cmpgtui);
1601 }
1602 break;
1603 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001604
1605 // Translate a "$Rdd = $Rss" to "$Rdd = combine($Rs, $Rt)"
1606 case Hexagon::A2_tfrp: {
1607 MCOperand &MO = Inst.getOperand(1);
1608 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001609 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001610 StringRef Reg1(R1);
1611 MO.setReg(MatchRegisterName(Reg1));
1612 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001613 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001614 StringRef Reg2(R2);
1615 Inst.addOperand(MCOperand::createReg(MatchRegisterName(Reg2)));
1616 Inst.setOpcode(Hexagon::A2_combinew);
1617 break;
1618 }
1619
1620 case Hexagon::A2_tfrpt:
1621 case Hexagon::A2_tfrpf: {
1622 MCOperand &MO = Inst.getOperand(2);
1623 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001624 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001625 StringRef Reg1(R1);
1626 MO.setReg(MatchRegisterName(Reg1));
1627 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001628 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001629 StringRef Reg2(R2);
1630 Inst.addOperand(MCOperand::createReg(MatchRegisterName(Reg2)));
1631 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrpt)
1632 ? Hexagon::C2_ccombinewt
1633 : Hexagon::C2_ccombinewf);
1634 break;
1635 }
1636 case Hexagon::A2_tfrptnew:
1637 case Hexagon::A2_tfrpfnew: {
1638 MCOperand &MO = Inst.getOperand(2);
1639 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001640 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001641 StringRef Reg1(R1);
1642 MO.setReg(MatchRegisterName(Reg1));
1643 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001644 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001645 StringRef Reg2(R2);
1646 Inst.addOperand(MCOperand::createReg(MatchRegisterName(Reg2)));
1647 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrptnew)
1648 ? Hexagon::C2_ccombinewnewt
1649 : Hexagon::C2_ccombinewnewf);
1650 break;
1651 }
1652
1653 // Translate a "$Rx = CONST32(#imm)" to "$Rx = memw(gp+#LABEL) "
1654 case Hexagon::CONST32:
1655 case Hexagon::CONST32_Float_Real:
1656 case Hexagon::CONST32_Int_Real:
1657 case Hexagon::FCONST32_nsdata:
1658 is32bit = true;
1659 // Translate a "$Rx:y = CONST64(#imm)" to "$Rx:y = memd(gp+#LABEL) "
1660 case Hexagon::CONST64_Float_Real:
1661 case Hexagon::CONST64_Int_Real:
1662
1663 // FIXME: need better way to detect AsmStreamer (upstream removed getKind())
1664 if (!Parser.getStreamer().hasRawTextSupport()) {
1665 MCELFStreamer *MES = static_cast<MCELFStreamer *>(&Parser.getStreamer());
1666 MCOperand &MO_1 = Inst.getOperand(1);
1667 MCOperand &MO_0 = Inst.getOperand(0);
1668
1669 // push section onto section stack
1670 MES->PushSection();
1671
1672 std::string myCharStr;
1673 MCSectionELF *mySection;
1674
1675 // check if this as an immediate or a symbol
1676 int64_t Value;
1677 bool Absolute = MO_1.getExpr()->evaluateAsAbsolute(Value);
1678 if (Absolute) {
1679 // Create a new section - one for each constant
1680 // Some or all of the zeros are replaced with the given immediate.
1681 if (is32bit) {
1682 std::string myImmStr = utohexstr(static_cast<uint32_t>(Value));
1683 myCharStr = StringRef(".gnu.linkonce.l4.CONST_00000000")
1684 .drop_back(myImmStr.size())
1685 .str() +
1686 myImmStr;
1687 } else {
1688 std::string myImmStr = utohexstr(Value);
1689 myCharStr = StringRef(".gnu.linkonce.l8.CONST_0000000000000000")
1690 .drop_back(myImmStr.size())
1691 .str() +
1692 myImmStr;
1693 }
1694
1695 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1696 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1697 } else if (MO_1.isExpr()) {
1698 // .lita - for expressions
1699 myCharStr = ".lita";
1700 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1701 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1702 } else
1703 llvm_unreachable("unexpected type of machine operand!");
1704
1705 MES->SwitchSection(mySection);
1706 unsigned byteSize = is32bit ? 4 : 8;
1707 getStreamer().EmitCodeAlignment(byteSize, byteSize);
1708
1709 MCSymbol *Sym;
1710
1711 // for symbols, get rid of prepended ".gnu.linkonce.lx."
1712
1713 // emit symbol if needed
1714 if (Absolute) {
1715 Sym = getContext().getOrCreateSymbol(StringRef(myCharStr.c_str() + 16));
1716 if (Sym->isUndefined()) {
1717 getStreamer().EmitLabel(Sym);
1718 getStreamer().EmitSymbolAttribute(Sym, MCSA_Global);
1719 getStreamer().EmitIntValue(Value, byteSize);
1720 }
1721 } else if (MO_1.isExpr()) {
1722 const char *StringStart = 0;
1723 const char *StringEnd = 0;
1724 if (*Operands[4]->getStartLoc().getPointer() == '#') {
1725 StringStart = Operands[5]->getStartLoc().getPointer();
1726 StringEnd = Operands[6]->getStartLoc().getPointer();
1727 } else { // no pound
1728 StringStart = Operands[4]->getStartLoc().getPointer();
1729 StringEnd = Operands[5]->getStartLoc().getPointer();
1730 }
1731
1732 unsigned size = StringEnd - StringStart;
1733 std::string DotConst = ".CONST_";
1734 Sym = getContext().getOrCreateSymbol(DotConst +
1735 StringRef(StringStart, size));
1736
1737 if (Sym->isUndefined()) {
1738 // case where symbol is not yet defined: emit symbol
1739 getStreamer().EmitLabel(Sym);
1740 getStreamer().EmitSymbolAttribute(Sym, MCSA_Local);
1741 getStreamer().EmitValue(MO_1.getExpr(), 4);
1742 }
1743 } else
1744 llvm_unreachable("unexpected type of machine operand!");
1745
1746 MES->PopSection();
1747
1748 if (Sym) {
1749 MCInst TmpInst;
1750 if (is32bit) // 32 bit
1751 TmpInst.setOpcode(Hexagon::L2_loadrigp);
1752 else // 64 bit
1753 TmpInst.setOpcode(Hexagon::L2_loadrdgp);
1754
1755 TmpInst.addOperand(MO_0);
1756 TmpInst.addOperand(
1757 MCOperand::createExpr(MCSymbolRefExpr::create(Sym, getContext())));
1758 Inst = TmpInst;
1759 }
1760 }
1761 break;
1762
1763 // Translate a "$Rdd = #-imm" to "$Rdd = combine(#[-1,0], #-imm)"
1764 case Hexagon::A2_tfrpi: {
1765 MCOperand &Rdd = Inst.getOperand(0);
1766 MCOperand &MO = Inst.getOperand(1);
1767 int64_t Value;
1768 int sVal = (MO.getExpr()->evaluateAsAbsolute(Value) && Value < 0) ? -1 : 0;
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001769 MCOperand imm(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001770 HexagonMCExpr::create(MCConstantExpr::create(sVal, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001771 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, imm, MO);
1772 break;
1773 }
1774
1775 // Translate a "$Rdd = [#]#imm" to "$Rdd = combine(#, [#]#imm)"
1776 case Hexagon::TFRI64_V4: {
1777 MCOperand &Rdd = Inst.getOperand(0);
1778 MCOperand &MO = Inst.getOperand(1);
1779 int64_t Value;
1780 if (MO.getExpr()->evaluateAsAbsolute(Value)) {
1781 unsigned long long u64 = Value;
1782 signed int s8 = (u64 >> 32) & 0xFFFFFFFF;
1783 if (s8 < -128 || s8 > 127)
1784 OutOfRange(IDLoc, s8, -128);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001785 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001786 MCConstantExpr::create(s8, Context), Context))); // upper 32
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001787 auto Expr = HexagonMCExpr::create(
1788 MCConstantExpr::create(u64 & 0xFFFFFFFF, Context),
1789 Context);
1790 HexagonMCInstrInfo::setMustExtend(*Expr, HexagonMCInstrInfo::mustExtend(*MO.getExpr()));
1791 MCOperand imm2(MCOperand::createExpr(Expr)); // lower 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001792 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, imm2);
1793 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001794 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001795 MCConstantExpr::create(0, Context), Context))); // upper 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001796 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, MO);
1797 }
1798 break;
1799 }
1800
1801 // Handle $Rdd = combine(##imm, #imm)"
1802 case Hexagon::TFRI64_V2_ext: {
1803 MCOperand &Rdd = Inst.getOperand(0);
1804 MCOperand &MO1 = Inst.getOperand(1);
1805 MCOperand &MO2 = Inst.getOperand(2);
1806 int64_t Value;
1807 if (MO2.getExpr()->evaluateAsAbsolute(Value)) {
1808 int s8 = Value;
1809 if (s8 < -128 || s8 > 127)
1810 OutOfRange(IDLoc, s8, -128);
1811 }
1812 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, MO1, MO2);
1813 break;
1814 }
1815
1816 // Handle $Rdd = combine(#imm, ##imm)"
1817 case Hexagon::A4_combineii: {
1818 MCOperand &Rdd = Inst.getOperand(0);
1819 MCOperand &MO1 = Inst.getOperand(1);
1820 int64_t Value;
1821 if (MO1.getExpr()->evaluateAsAbsolute(Value)) {
1822 int s8 = Value;
1823 if (s8 < -128 || s8 > 127)
1824 OutOfRange(IDLoc, s8, -128);
1825 }
1826 MCOperand &MO2 = Inst.getOperand(2);
1827 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, MO1, MO2);
1828 break;
1829 }
1830
1831 case Hexagon::S2_tableidxb_goodsyntax: {
1832 Inst.setOpcode(Hexagon::S2_tableidxb);
1833 break;
1834 }
1835
1836 case Hexagon::S2_tableidxh_goodsyntax: {
1837 MCInst TmpInst;
1838 MCOperand &Rx = Inst.getOperand(0);
1839 MCOperand &_dst_ = Inst.getOperand(1);
1840 MCOperand &Rs = Inst.getOperand(2);
1841 MCOperand &Imm4 = Inst.getOperand(3);
1842 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001843 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001844 Imm6.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001845 TmpInst.setOpcode(Hexagon::S2_tableidxh);
1846 TmpInst.addOperand(Rx);
1847 TmpInst.addOperand(_dst_);
1848 TmpInst.addOperand(Rs);
1849 TmpInst.addOperand(Imm4);
1850 TmpInst.addOperand(Imm6);
1851 Inst = TmpInst;
1852 break;
1853 }
1854
1855 case Hexagon::S2_tableidxw_goodsyntax: {
1856 MCInst TmpInst;
1857 MCOperand &Rx = Inst.getOperand(0);
1858 MCOperand &_dst_ = Inst.getOperand(1);
1859 MCOperand &Rs = Inst.getOperand(2);
1860 MCOperand &Imm4 = Inst.getOperand(3);
1861 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001862 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001863 Imm6.getExpr(), MCConstantExpr::create(2, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001864 TmpInst.setOpcode(Hexagon::S2_tableidxw);
1865 TmpInst.addOperand(Rx);
1866 TmpInst.addOperand(_dst_);
1867 TmpInst.addOperand(Rs);
1868 TmpInst.addOperand(Imm4);
1869 TmpInst.addOperand(Imm6);
1870 Inst = TmpInst;
1871 break;
1872 }
1873
1874 case Hexagon::S2_tableidxd_goodsyntax: {
1875 MCInst TmpInst;
1876 MCOperand &Rx = Inst.getOperand(0);
1877 MCOperand &_dst_ = Inst.getOperand(1);
1878 MCOperand &Rs = Inst.getOperand(2);
1879 MCOperand &Imm4 = Inst.getOperand(3);
1880 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001881 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001882 Imm6.getExpr(), MCConstantExpr::create(3, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001883 TmpInst.setOpcode(Hexagon::S2_tableidxd);
1884 TmpInst.addOperand(Rx);
1885 TmpInst.addOperand(_dst_);
1886 TmpInst.addOperand(Rs);
1887 TmpInst.addOperand(Imm4);
1888 TmpInst.addOperand(Imm6);
1889 Inst = TmpInst;
1890 break;
1891 }
1892
1893 case Hexagon::M2_mpyui: {
1894 Inst.setOpcode(Hexagon::M2_mpyi);
1895 break;
1896 }
1897 case Hexagon::M2_mpysmi: {
1898 MCInst TmpInst;
1899 MCOperand &Rd = Inst.getOperand(0);
1900 MCOperand &Rs = Inst.getOperand(1);
1901 MCOperand &Imm = Inst.getOperand(2);
1902 int64_t Value;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001903 MCExpr const &Expr = *Imm.getExpr();
1904 bool Absolute = Expr.evaluateAsAbsolute(Value);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001905 assert(Absolute);
1906 (void)Absolute;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001907 if (!HexagonMCInstrInfo::mustExtend(Expr)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001908 if (Value < 0 && Value > -256) {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001909 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001910 MCConstantExpr::create(Value * -1, Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001911 TmpInst.setOpcode(Hexagon::M2_mpysin);
1912 } else if (Value < 256 && Value >= 0)
1913 TmpInst.setOpcode(Hexagon::M2_mpysip);
1914 else
1915 return Match_InvalidOperand;
1916 } else {
1917 if (Value >= 0)
1918 TmpInst.setOpcode(Hexagon::M2_mpysip);
1919 else
1920 return Match_InvalidOperand;
1921 }
1922 TmpInst.addOperand(Rd);
1923 TmpInst.addOperand(Rs);
1924 TmpInst.addOperand(Imm);
1925 Inst = TmpInst;
1926 break;
1927 }
1928
1929 case Hexagon::S2_asr_i_r_rnd_goodsyntax: {
1930 MCOperand &Imm = Inst.getOperand(2);
1931 MCInst TmpInst;
1932 int64_t Value;
1933 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1934 assert(Absolute);
1935 (void)Absolute;
1936 if (Value == 0) { // convert to $Rd = $Rs
1937 TmpInst.setOpcode(Hexagon::A2_tfr);
1938 MCOperand &Rd = Inst.getOperand(0);
1939 MCOperand &Rs = Inst.getOperand(1);
1940 TmpInst.addOperand(Rd);
1941 TmpInst.addOperand(Rs);
1942 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001943 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001944 MCBinaryExpr::createSub(Imm.getExpr(),
1945 MCConstantExpr::create(1, Context), Context),
1946 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001947 TmpInst.setOpcode(Hexagon::S2_asr_i_r_rnd);
1948 MCOperand &Rd = Inst.getOperand(0);
1949 MCOperand &Rs = Inst.getOperand(1);
1950 TmpInst.addOperand(Rd);
1951 TmpInst.addOperand(Rs);
1952 TmpInst.addOperand(Imm);
1953 }
1954 Inst = TmpInst;
1955 break;
1956 }
1957
1958 case Hexagon::S2_asr_i_p_rnd_goodsyntax: {
1959 MCOperand &Rdd = Inst.getOperand(0);
1960 MCOperand &Rss = Inst.getOperand(1);
1961 MCOperand &Imm = Inst.getOperand(2);
1962 int64_t Value;
1963 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1964 assert(Absolute);
1965 (void)Absolute;
1966 if (Value == 0) { // convert to $Rdd = combine ($Rs[0], $Rs[1])
1967 MCInst TmpInst;
1968 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001969 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001970 StringRef Reg1(R1);
1971 Rss.setReg(MatchRegisterName(Reg1));
1972 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001973 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001974 StringRef Reg2(R2);
1975 TmpInst.setOpcode(Hexagon::A2_combinew);
1976 TmpInst.addOperand(Rdd);
1977 TmpInst.addOperand(Rss);
1978 TmpInst.addOperand(MCOperand::createReg(MatchRegisterName(Reg2)));
1979 Inst = TmpInst;
1980 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001981 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001982 MCBinaryExpr::createSub(Imm.getExpr(),
1983 MCConstantExpr::create(1, Context), Context),
1984 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001985 Inst.setOpcode(Hexagon::S2_asr_i_p_rnd);
1986 }
1987 break;
1988 }
1989
1990 case Hexagon::A4_boundscheck: {
1991 MCOperand &Rs = Inst.getOperand(1);
1992 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
1993 if (RegNum & 1) { // Odd mapped to raw:hi, regpair is rodd:odd-1, like r3:2
1994 Inst.setOpcode(Hexagon::A4_boundscheck_hi);
1995 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00001996 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001997 StringRef RegPair = Name;
1998 Rs.setReg(MatchRegisterName(RegPair));
1999 } else { // raw:lo
2000 Inst.setOpcode(Hexagon::A4_boundscheck_lo);
2001 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002002 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002003 StringRef RegPair = Name;
2004 Rs.setReg(MatchRegisterName(RegPair));
2005 }
2006 break;
2007 }
2008
2009 case Hexagon::A2_addsp: {
2010 MCOperand &Rs = Inst.getOperand(1);
2011 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
2012 if (RegNum & 1) { // Odd mapped to raw:hi
2013 Inst.setOpcode(Hexagon::A2_addsph);
2014 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002015 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002016 StringRef RegPair = Name;
2017 Rs.setReg(MatchRegisterName(RegPair));
2018 } else { // Even mapped raw:lo
2019 Inst.setOpcode(Hexagon::A2_addspl);
2020 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002021 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002022 StringRef RegPair = Name;
2023 Rs.setReg(MatchRegisterName(RegPair));
2024 }
2025 break;
2026 }
2027
2028 case Hexagon::M2_vrcmpys_s1: {
2029 MCOperand &Rt = Inst.getOperand(2);
2030 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2031 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2032 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_h);
2033 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002034 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002035 StringRef RegPair = Name;
2036 Rt.setReg(MatchRegisterName(RegPair));
2037 } else { // Even mapped sat:raw:lo
2038 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_l);
2039 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002040 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002041 StringRef RegPair = Name;
2042 Rt.setReg(MatchRegisterName(RegPair));
2043 }
2044 break;
2045 }
2046
2047 case Hexagon::M2_vrcmpys_acc_s1: {
2048 MCInst TmpInst;
2049 MCOperand &Rxx = Inst.getOperand(0);
2050 MCOperand &Rss = Inst.getOperand(2);
2051 MCOperand &Rt = Inst.getOperand(3);
2052 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2053 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2054 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_h);
2055 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002056 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002057 StringRef RegPair = Name;
2058 Rt.setReg(MatchRegisterName(RegPair));
2059 } else { // Even mapped sat:raw:lo
2060 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_l);
2061 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002062 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002063 StringRef RegPair = Name;
2064 Rt.setReg(MatchRegisterName(RegPair));
2065 }
2066 // Registers are in different positions
2067 TmpInst.addOperand(Rxx);
2068 TmpInst.addOperand(Rxx);
2069 TmpInst.addOperand(Rss);
2070 TmpInst.addOperand(Rt);
2071 Inst = TmpInst;
2072 break;
2073 }
2074
2075 case Hexagon::M2_vrcmpys_s1rp: {
2076 MCOperand &Rt = Inst.getOperand(2);
2077 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2078 if (RegNum & 1) { // Odd mapped to rnd:sat:raw:hi
2079 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_h);
2080 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002081 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002082 StringRef RegPair = Name;
2083 Rt.setReg(MatchRegisterName(RegPair));
2084 } else { // Even mapped rnd:sat:raw:lo
2085 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_l);
2086 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002087 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002088 StringRef RegPair = Name;
2089 Rt.setReg(MatchRegisterName(RegPair));
2090 }
2091 break;
2092 }
2093
2094 case Hexagon::S5_asrhub_rnd_sat_goodsyntax: {
2095 MCOperand &Imm = Inst.getOperand(2);
2096 int64_t Value;
2097 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
2098 assert(Absolute);
2099 (void)Absolute;
2100 if (Value == 0)
2101 Inst.setOpcode(Hexagon::S2_vsathub);
2102 else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002103 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002104 MCBinaryExpr::createSub(Imm.getExpr(),
2105 MCConstantExpr::create(1, Context), Context),
2106 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002107 Inst.setOpcode(Hexagon::S5_asrhub_rnd_sat);
2108 }
2109 break;
2110 }
2111
2112 case Hexagon::S5_vasrhrnd_goodsyntax: {
2113 MCOperand &Rdd = Inst.getOperand(0);
2114 MCOperand &Rss = Inst.getOperand(1);
2115 MCOperand &Imm = Inst.getOperand(2);
2116 int64_t Value;
2117 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
2118 assert(Absolute);
2119 (void)Absolute;
2120 if (Value == 0) {
2121 MCInst TmpInst;
2122 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00002123 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002124 StringRef Reg1(R1);
2125 Rss.setReg(MatchRegisterName(Reg1));
2126 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00002127 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002128 StringRef Reg2(R2);
2129 TmpInst.setOpcode(Hexagon::A2_combinew);
2130 TmpInst.addOperand(Rdd);
2131 TmpInst.addOperand(Rss);
2132 TmpInst.addOperand(MCOperand::createReg(MatchRegisterName(Reg2)));
2133 Inst = TmpInst;
2134 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002135 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002136 MCBinaryExpr::createSub(Imm.getExpr(),
2137 MCConstantExpr::create(1, Context), Context),
2138 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002139 Inst.setOpcode(Hexagon::S5_vasrhrnd);
2140 }
2141 break;
2142 }
2143
2144 case Hexagon::A2_not: {
2145 MCInst TmpInst;
2146 MCOperand &Rd = Inst.getOperand(0);
2147 MCOperand &Rs = Inst.getOperand(1);
2148 TmpInst.setOpcode(Hexagon::A2_subri);
2149 TmpInst.addOperand(Rd);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002150 TmpInst.addOperand(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002151 HexagonMCExpr::create(MCConstantExpr::create(-1, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002152 TmpInst.addOperand(Rs);
2153 Inst = TmpInst;
2154 break;
2155 }
2156 } // switch
2157
2158 return Match_Success;
2159}