blob: ba4dfab4629e8e9a513bd3d0307ab398b94a8d91 [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"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000023#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/Twine.h"
26#include "llvm/MC/MCContext.h"
27#include "llvm/MC/MCELFStreamer.h"
28#include "llvm/MC/MCExpr.h"
29#include "llvm/MC/MCInst.h"
30#include "llvm/MC/MCParser/MCAsmLexer.h"
31#include "llvm/MC/MCParser/MCAsmParser.h"
32#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000033#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000034#include "llvm/MC/MCSectionELF.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000035#include "llvm/MC/MCStreamer.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000036#include "llvm/MC/MCSubtargetInfo.h"
Colin LeMahieu5cb6eea2016-03-01 21:37:41 +000037#include "llvm/MC/MCValue.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"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000046
47using namespace llvm;
48
49static cl::opt<bool> EnableFutureRegs("mfuture-regs",
50 cl::desc("Enable future registers"));
51
52static cl::opt<bool> WarnMissingParenthesis("mwarn-missing-parenthesis",
53cl::desc("Warn for missing parenthesis around predicate registers"),
54cl::init(true));
55static cl::opt<bool> ErrorMissingParenthesis("merror-missing-parenthesis",
56cl::desc("Error for missing parenthesis around predicate registers"),
57cl::init(false));
58static cl::opt<bool> WarnSignedMismatch("mwarn-sign-mismatch",
59cl::desc("Warn for mismatching a signed and unsigned value"),
60cl::init(true));
61static cl::opt<bool> WarnNoncontigiousRegister("mwarn-noncontigious-register",
62cl::desc("Warn for register names that arent contigious"),
63cl::init(true));
64static cl::opt<bool> ErrorNoncontigiousRegister("merror-noncontigious-register",
65cl::desc("Error for register names that aren't contigious"),
66cl::init(false));
67
68
69namespace {
70struct HexagonOperand;
71
72class HexagonAsmParser : public MCTargetAsmParser {
73
74 HexagonTargetStreamer &getTargetStreamer() {
75 MCTargetStreamer &TS = *Parser.getStreamer().getTargetStreamer();
76 return static_cast<HexagonTargetStreamer &>(TS);
77 }
78
Colin LeMahieu7cd08922015-11-09 04:07:48 +000079 MCAsmParser &Parser;
80 MCAssembler *Assembler;
81 MCInstrInfo const &MCII;
82 MCInst MCB;
83 bool InBrackets;
84
85 MCAsmParser &getParser() const { return Parser; }
86 MCAssembler *getAssembler() const { return Assembler; }
87 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
88
Colin LeMahieu7cd08922015-11-09 04:07:48 +000089 bool equalIsAsmAssignment() override { return false; }
90 bool isLabel(AsmToken &Token) override;
91
92 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
93 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
94 bool ParseDirectiveFalign(unsigned Size, SMLoc L);
95
96 virtual bool ParseRegister(unsigned &RegNo,
97 SMLoc &StartLoc,
98 SMLoc &EndLoc) override;
99 bool ParseDirectiveSubsection(SMLoc L);
100 bool ParseDirectiveValue(unsigned Size, SMLoc L);
101 bool ParseDirectiveComm(bool IsLocal, SMLoc L);
102 bool RegisterMatchesArch(unsigned MatchNum) const;
103
104 bool matchBundleOptions();
105 bool handleNoncontigiousRegister(bool Contigious, SMLoc &Loc);
106 bool finishBundle(SMLoc IDLoc, MCStreamer &Out);
107 void canonicalizeImmediates(MCInst &MCI);
108 bool matchOneInstruction(MCInst &MCB, SMLoc IDLoc,
109 OperandVector &InstOperands, uint64_t &ErrorInfo,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000110 bool MatchingInlineAsm);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000111
112 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
113 OperandVector &Operands, MCStreamer &Out,
Colin LeMahieu9ea507e2015-11-09 07:10:24 +0000114 uint64_t &ErrorInfo, bool MatchingInlineAsm) override;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000115
116 unsigned validateTargetOperandClass(MCParsedAsmOperand &Op, unsigned Kind) override;
117 void OutOfRange(SMLoc IDLoc, long long Val, long long Max);
118 int processInstruction(MCInst &Inst, OperandVector const &Operands,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000119 SMLoc IDLoc);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000120
121 // Check if we have an assembler and, if so, set the ELF e_header flags.
122 void chksetELFHeaderEFlags(unsigned flags) {
123 if (getAssembler())
124 getAssembler()->setELFHeaderEFlags(flags);
125 }
126
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +0000127 unsigned matchRegister(StringRef Name);
128
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000129/// @name Auto-generated Match Functions
130/// {
131
132#define GET_ASSEMBLER_HEADER
133#include "HexagonGenAsmMatcher.inc"
134
135 /// }
136
137public:
Akira Hatanakab11ef082015-11-14 06:35:56 +0000138 HexagonAsmParser(const MCSubtargetInfo &_STI, MCAsmParser &_Parser,
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000139 const MCInstrInfo &MII, const MCTargetOptions &Options)
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000140 : MCTargetAsmParser(Options, _STI), Parser(_Parser),
Colin LeMahieuf0af6e52015-11-13 17:42:46 +0000141 MCII (MII), MCB(HexagonMCInstrInfo::createBundle()), InBrackets(false) {
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000142 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000143
144 MCAsmParserExtension::Initialize(_Parser);
145
146 Assembler = nullptr;
147 // FIXME: need better way to detect AsmStreamer (upstream removed getKind())
148 if (!Parser.getStreamer().hasRawTextSupport()) {
149 MCELFStreamer *MES = static_cast<MCELFStreamer *>(&Parser.getStreamer());
150 Assembler = &MES->getAssembler();
151 }
152 }
153
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000154 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!");
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000364 HexagonMCExpr *Expr =
365 const_cast<HexagonMCExpr *>(cast<HexagonMCExpr>(getImm()));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000366 int64_t Value;
367 if (!Expr->evaluateAsAbsolute(Value)) {
368 Inst.addOperand(MCOperand::createExpr(Expr));
369 return;
370 }
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000371 int64_t Extended = SignExtend64(Value, 32);
372 if ((Extended < 0) != (Value < 0))
373 Expr->setSignMismatch();
374 Inst.addOperand(MCOperand::createExpr(Expr));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000375 }
376
377 void addf32ExtOperands(MCInst &Inst, unsigned N) const {
378 addImmOperands(Inst, N);
379 }
380
381 void adds32ImmOperands(MCInst &Inst, unsigned N) const {
382 addSignedImmOperands(Inst, N);
383 }
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000384 void adds23_2ImmOperands(MCInst &Inst, unsigned N) const {
385 addSignedImmOperands(Inst, N);
386 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000387 void adds8ImmOperands(MCInst &Inst, unsigned N) const {
388 addSignedImmOperands(Inst, N);
389 }
390 void adds8Imm64Operands(MCInst &Inst, unsigned N) const {
391 addSignedImmOperands(Inst, N);
392 }
393 void adds6ImmOperands(MCInst &Inst, unsigned N) const {
394 addSignedImmOperands(Inst, N);
395 }
396 void adds4ImmOperands(MCInst &Inst, unsigned N) const {
397 addSignedImmOperands(Inst, N);
398 }
399 void adds4_0ImmOperands(MCInst &Inst, unsigned N) const {
400 addSignedImmOperands(Inst, N);
401 }
402 void adds4_1ImmOperands(MCInst &Inst, unsigned N) const {
403 addSignedImmOperands(Inst, N);
404 }
405 void adds4_2ImmOperands(MCInst &Inst, unsigned N) const {
406 addSignedImmOperands(Inst, N);
407 }
408 void adds4_3ImmOperands(MCInst &Inst, unsigned N) const {
409 addSignedImmOperands(Inst, N);
410 }
411 void adds3ImmOperands(MCInst &Inst, unsigned N) const {
412 addSignedImmOperands(Inst, N);
413 }
414
415 void addu64ImmOperands(MCInst &Inst, unsigned N) const {
416 addImmOperands(Inst, N);
417 }
418 void addu32ImmOperands(MCInst &Inst, unsigned N) const {
419 addImmOperands(Inst, N);
420 }
421 void addu26_6ImmOperands(MCInst &Inst, unsigned N) const {
422 addImmOperands(Inst, N);
423 }
424 void addu16ImmOperands(MCInst &Inst, unsigned N) const {
425 addImmOperands(Inst, N);
426 }
427 void addu16_0ImmOperands(MCInst &Inst, unsigned N) const {
428 addImmOperands(Inst, N);
429 }
430 void addu16_1ImmOperands(MCInst &Inst, unsigned N) const {
431 addImmOperands(Inst, N);
432 }
433 void addu16_2ImmOperands(MCInst &Inst, unsigned N) const {
434 addImmOperands(Inst, N);
435 }
436 void addu16_3ImmOperands(MCInst &Inst, unsigned N) const {
437 addImmOperands(Inst, N);
438 }
439 void addu11_3ImmOperands(MCInst &Inst, unsigned N) const {
440 addImmOperands(Inst, N);
441 }
442 void addu10ImmOperands(MCInst &Inst, unsigned N) const {
443 addImmOperands(Inst, N);
444 }
445 void addu9ImmOperands(MCInst &Inst, unsigned N) const {
446 addImmOperands(Inst, N);
447 }
448 void addu8ImmOperands(MCInst &Inst, unsigned N) const {
449 addImmOperands(Inst, N);
450 }
451 void addu7ImmOperands(MCInst &Inst, unsigned N) const {
452 addImmOperands(Inst, N);
453 }
454 void addu6ImmOperands(MCInst &Inst, unsigned N) const {
455 addImmOperands(Inst, N);
456 }
457 void addu6_0ImmOperands(MCInst &Inst, unsigned N) const {
458 addImmOperands(Inst, N);
459 }
460 void addu6_1ImmOperands(MCInst &Inst, unsigned N) const {
461 addImmOperands(Inst, N);
462 }
463 void addu6_2ImmOperands(MCInst &Inst, unsigned N) const {
464 addImmOperands(Inst, N);
465 }
466 void addu6_3ImmOperands(MCInst &Inst, unsigned N) const {
467 addImmOperands(Inst, N);
468 }
469 void addu5ImmOperands(MCInst &Inst, unsigned N) const {
470 addImmOperands(Inst, N);
471 }
472 void addu4ImmOperands(MCInst &Inst, unsigned N) const {
473 addImmOperands(Inst, N);
474 }
475 void addu3ImmOperands(MCInst &Inst, unsigned N) const {
476 addImmOperands(Inst, N);
477 }
478 void addu2ImmOperands(MCInst &Inst, unsigned N) const {
479 addImmOperands(Inst, N);
480 }
481 void addu1ImmOperands(MCInst &Inst, unsigned N) const {
482 addImmOperands(Inst, N);
483 }
484
485 void addm6ImmOperands(MCInst &Inst, unsigned N) const {
486 addImmOperands(Inst, N);
487 }
488 void addn8ImmOperands(MCInst &Inst, unsigned N) const {
489 addImmOperands(Inst, N);
490 }
491
492 void adds16ExtOperands(MCInst &Inst, unsigned N) const {
493 addSignedImmOperands(Inst, N);
494 }
495 void adds12ExtOperands(MCInst &Inst, unsigned N) const {
496 addSignedImmOperands(Inst, N);
497 }
498 void adds10ExtOperands(MCInst &Inst, unsigned N) const {
499 addSignedImmOperands(Inst, N);
500 }
501 void adds9ExtOperands(MCInst &Inst, unsigned N) const {
502 addSignedImmOperands(Inst, N);
503 }
504 void adds8ExtOperands(MCInst &Inst, unsigned N) const {
505 addSignedImmOperands(Inst, N);
506 }
507 void adds6ExtOperands(MCInst &Inst, unsigned N) const {
508 addSignedImmOperands(Inst, N);
509 }
510 void adds11_0ExtOperands(MCInst &Inst, unsigned N) const {
511 addSignedImmOperands(Inst, N);
512 }
513 void adds11_1ExtOperands(MCInst &Inst, unsigned N) const {
514 addSignedImmOperands(Inst, N);
515 }
516 void adds11_2ExtOperands(MCInst &Inst, unsigned N) const {
517 addSignedImmOperands(Inst, N);
518 }
519 void adds11_3ExtOperands(MCInst &Inst, unsigned N) const {
520 addSignedImmOperands(Inst, N);
521 }
522
523 void addu6ExtOperands(MCInst &Inst, unsigned N) const {
524 addImmOperands(Inst, N);
525 }
526 void addu7ExtOperands(MCInst &Inst, unsigned N) const {
527 addImmOperands(Inst, N);
528 }
529 void addu8ExtOperands(MCInst &Inst, unsigned N) const {
530 addImmOperands(Inst, N);
531 }
532 void addu9ExtOperands(MCInst &Inst, unsigned N) const {
533 addImmOperands(Inst, N);
534 }
535 void addu10ExtOperands(MCInst &Inst, unsigned N) const {
536 addImmOperands(Inst, N);
537 }
538 void addu6_0ExtOperands(MCInst &Inst, unsigned N) const {
539 addImmOperands(Inst, N);
540 }
541 void addu6_1ExtOperands(MCInst &Inst, unsigned N) const {
542 addImmOperands(Inst, N);
543 }
544 void addu6_2ExtOperands(MCInst &Inst, unsigned N) const {
545 addImmOperands(Inst, N);
546 }
547 void addu6_3ExtOperands(MCInst &Inst, unsigned N) const {
548 addImmOperands(Inst, N);
549 }
550 void addu32MustExtOperands(MCInst &Inst, unsigned N) const {
551 addImmOperands(Inst, N);
552 }
553
554 void adds4_6ImmOperands(MCInst &Inst, unsigned N) const {
555 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000556 const MCConstantExpr *CE =
557 dyn_cast<MCConstantExpr>(&HexagonMCInstrInfo::getExpr(*getImm()));
Colin LeMahieu4c606e62015-12-04 15:48:45 +0000558 Inst.addOperand(MCOperand::createImm(CE->getValue() * 64));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000559 }
560
561 void adds3_6ImmOperands(MCInst &Inst, unsigned N) const {
562 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000563 const MCConstantExpr *CE =
564 dyn_cast<MCConstantExpr>(&HexagonMCInstrInfo::getExpr(*getImm()));
Colin LeMahieu4c606e62015-12-04 15:48:45 +0000565 Inst.addOperand(MCOperand::createImm(CE->getValue() * 64));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000566 }
567
568 StringRef getToken() const {
569 assert(Kind == Token && "Invalid access!");
570 return StringRef(Tok.Data, Tok.Length);
571 }
572
573 virtual void print(raw_ostream &OS) const;
574
575 static std::unique_ptr<HexagonOperand> CreateToken(StringRef Str, SMLoc S) {
576 HexagonOperand *Op = new HexagonOperand(Token);
577 Op->Tok.Data = Str.data();
578 Op->Tok.Length = Str.size();
579 Op->StartLoc = S;
580 Op->EndLoc = S;
581 return std::unique_ptr<HexagonOperand>(Op);
582 }
583
584 static std::unique_ptr<HexagonOperand> CreateReg(unsigned RegNum, SMLoc S,
585 SMLoc E) {
586 HexagonOperand *Op = new HexagonOperand(Register);
587 Op->Reg.RegNum = RegNum;
588 Op->StartLoc = S;
589 Op->EndLoc = E;
590 return std::unique_ptr<HexagonOperand>(Op);
591 }
592
593 static std::unique_ptr<HexagonOperand> CreateImm(const MCExpr *Val, SMLoc S,
594 SMLoc E) {
595 HexagonOperand *Op = new HexagonOperand(Immediate);
596 Op->Imm.Val = Val;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000597 Op->StartLoc = S;
598 Op->EndLoc = E;
599 return std::unique_ptr<HexagonOperand>(Op);
600 }
601};
602
603} // end anonymous namespace.
604
605void HexagonOperand::print(raw_ostream &OS) const {
606 switch (Kind) {
607 case Immediate:
608 getImm()->print(OS, nullptr);
609 break;
610 case Register:
611 OS << "<register R";
612 OS << getReg() << ">";
613 break;
614 case Token:
615 OS << "'" << getToken() << "'";
616 break;
617 }
618}
619
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000620bool HexagonAsmParser::finishBundle(SMLoc IDLoc, MCStreamer &Out) {
621 DEBUG(dbgs() << "Bundle:");
622 DEBUG(MCB.dump_pretty(dbgs()));
623 DEBUG(dbgs() << "--\n");
624
625 // Check the bundle for errors.
626 const MCRegisterInfo *RI = getContext().getRegisterInfo();
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000627 HexagonMCChecker Check(MCII, getSTI(), MCB, MCB, *RI);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000628
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000629 bool CheckOk = HexagonMCInstrInfo::canonicalizePacket(MCII, getSTI(),
630 getContext(), MCB,
631 &Check);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000632
633 while (Check.getNextErrInfo() == true) {
634 unsigned Reg = Check.getErrRegister();
635 Twine R(RI->getName(Reg));
636
637 uint64_t Err = Check.getError();
638 if (Err != HexagonMCErrInfo::CHECK_SUCCESS) {
639 if (HexagonMCErrInfo::CHECK_ERROR_BRANCHES & Err)
640 Error(IDLoc,
641 "unconditional branch cannot precede another branch in packet");
642
643 if (HexagonMCErrInfo::CHECK_ERROR_NEWP & Err ||
644 HexagonMCErrInfo::CHECK_ERROR_NEWV & Err)
645 Error(IDLoc, "register `" + R +
646 "' used with `.new' "
647 "but not validly modified in the same packet");
648
649 if (HexagonMCErrInfo::CHECK_ERROR_REGISTERS & Err)
650 Error(IDLoc, "register `" + R + "' modified more than once");
651
652 if (HexagonMCErrInfo::CHECK_ERROR_READONLY & Err)
653 Error(IDLoc, "cannot write to read-only register `" + R + "'");
654
655 if (HexagonMCErrInfo::CHECK_ERROR_LOOP & Err)
656 Error(IDLoc, "loop-setup and some branch instructions "
657 "cannot be in the same packet");
658
659 if (HexagonMCErrInfo::CHECK_ERROR_ENDLOOP & Err) {
660 Twine N(HexagonMCInstrInfo::isInnerLoop(MCB) ? '0' : '1');
661 Error(IDLoc, "packet marked with `:endloop" + N + "' " +
662 "cannot contain instructions that modify register " +
663 "`" + R + "'");
664 }
665
666 if (HexagonMCErrInfo::CHECK_ERROR_SOLO & Err)
667 Error(IDLoc,
668 "instruction cannot appear in packet with other instructions");
669
670 if (HexagonMCErrInfo::CHECK_ERROR_NOSLOTS & Err)
671 Error(IDLoc, "too many slots used in packet");
672
673 if (Err & HexagonMCErrInfo::CHECK_ERROR_SHUFFLE) {
674 uint64_t Erm = Check.getShuffleError();
675
676 if (HexagonShuffler::SHUFFLE_ERROR_INVALID == Erm)
677 Error(IDLoc, "invalid instruction packet");
678 else if (HexagonShuffler::SHUFFLE_ERROR_STORES == Erm)
679 Error(IDLoc, "invalid instruction packet: too many stores");
680 else if (HexagonShuffler::SHUFFLE_ERROR_LOADS == Erm)
681 Error(IDLoc, "invalid instruction packet: too many loads");
682 else if (HexagonShuffler::SHUFFLE_ERROR_BRANCHES == Erm)
683 Error(IDLoc, "too many branches in packet");
684 else if (HexagonShuffler::SHUFFLE_ERROR_NOSLOTS == Erm)
685 Error(IDLoc, "invalid instruction packet: out of slots");
686 else if (HexagonShuffler::SHUFFLE_ERROR_SLOTS == Erm)
687 Error(IDLoc, "invalid instruction packet: slot error");
688 else if (HexagonShuffler::SHUFFLE_ERROR_ERRATA2 == Erm)
689 Error(IDLoc, "v60 packet violation");
690 else if (HexagonShuffler::SHUFFLE_ERROR_STORE_LOAD_CONFLICT == Erm)
691 Error(IDLoc, "slot 0 instruction does not allow slot 1 store");
692 else
693 Error(IDLoc, "unknown error in instruction packet");
694 }
695 }
696
697 unsigned Warn = Check.getWarning();
698 if (Warn != HexagonMCErrInfo::CHECK_SUCCESS) {
699 if (HexagonMCErrInfo::CHECK_WARN_CURRENT & Warn)
700 Warning(IDLoc, "register `" + R + "' used with `.cur' "
701 "but not used in the same packet");
702 else if (HexagonMCErrInfo::CHECK_WARN_TEMPORARY & Warn)
703 Warning(IDLoc, "register `" + R + "' used with `.tmp' "
704 "but not used in the same packet");
705 }
706 }
707
708 if (CheckOk) {
709 MCB.setLoc(IDLoc);
710 if (HexagonMCInstrInfo::bundleSize(MCB) == 0) {
711 assert(!HexagonMCInstrInfo::isInnerLoop(MCB));
712 assert(!HexagonMCInstrInfo::isOuterLoop(MCB));
713 // Empty packets are valid yet aren't emitted
714 return false;
715 }
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000716 Out.EmitInstruction(MCB, getSTI());
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000717 } else {
718 // If compounding and duplexing didn't reduce the size below
719 // 4 or less we have a packet that is too big.
720 if (HexagonMCInstrInfo::bundleSize(MCB) > HEXAGON_PACKET_SIZE) {
721 Error(IDLoc, "invalid instruction packet: out of slots");
722 return true; // Error
723 }
724 }
725
726 return false; // No error
727}
728
729bool HexagonAsmParser::matchBundleOptions() {
730 MCAsmParser &Parser = getParser();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000731 while (true) {
732 if (!Parser.getTok().is(AsmToken::Colon))
733 return false;
Nirav Davefd910412016-06-17 16:06:17 +0000734 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000735 StringRef Option = Parser.getTok().getString();
736 if (Option.compare_lower("endloop0") == 0)
737 HexagonMCInstrInfo::setInnerLoop(MCB);
738 else if (Option.compare_lower("endloop1") == 0)
739 HexagonMCInstrInfo::setOuterLoop(MCB);
740 else if (Option.compare_lower("mem_noshuf") == 0)
741 HexagonMCInstrInfo::setMemReorderDisabled(MCB);
742 else if (Option.compare_lower("mem_shuf") == 0)
743 HexagonMCInstrInfo::setMemStoreReorderEnabled(MCB);
744 else
745 return true;
Nirav Davefd910412016-06-17 16:06:17 +0000746 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000747 }
748}
749
750// For instruction aliases, immediates are generated rather than
751// MCConstantExpr. Convert them for uniform MCExpr.
752// Also check for signed/unsigned mismatches and warn
753void HexagonAsmParser::canonicalizeImmediates(MCInst &MCI) {
754 MCInst NewInst;
755 NewInst.setOpcode(MCI.getOpcode());
756 for (MCOperand &I : MCI)
757 if (I.isImm()) {
758 int64_t Value (I.getImm());
Colin LeMahieuc7b21242016-02-15 18:47:55 +0000759 NewInst.addOperand(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000760 MCConstantExpr::create(Value, getContext()), getContext())));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000761 }
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000762 else {
763 if (I.isExpr() && cast<HexagonMCExpr>(I.getExpr())->signMismatch() &&
764 WarnSignedMismatch)
765 Warning (MCI.getLoc(), "Signed/Unsigned mismatch");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000766 NewInst.addOperand(I);
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000767 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000768 MCI = NewInst;
769}
770
771bool HexagonAsmParser::matchOneInstruction(MCInst &MCI, SMLoc IDLoc,
772 OperandVector &InstOperands,
773 uint64_t &ErrorInfo,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000774 bool MatchingInlineAsm) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000775 // Perform matching with tablegen asmmatcher generated function
776 int result =
777 MatchInstructionImpl(InstOperands, MCI, ErrorInfo, MatchingInlineAsm);
778 if (result == Match_Success) {
779 MCI.setLoc(IDLoc);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000780 canonicalizeImmediates(MCI);
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000781 result = processInstruction(MCI, InstOperands, IDLoc);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000782
783 DEBUG(dbgs() << "Insn:");
784 DEBUG(MCI.dump_pretty(dbgs()));
785 DEBUG(dbgs() << "\n\n");
786
787 MCI.setLoc(IDLoc);
788 }
789
790 // Create instruction operand for bundle instruction
791 // Break this into a separate function Code here is less readable
792 // Think about how to get an instruction error to report correctly.
793 // SMLoc will return the "{"
794 switch (result) {
795 default:
796 break;
797 case Match_Success:
798 return false;
799 case Match_MissingFeature:
800 return Error(IDLoc, "invalid instruction");
801 case Match_MnemonicFail:
802 return Error(IDLoc, "unrecognized instruction");
803 case Match_InvalidOperand:
804 SMLoc ErrorLoc = IDLoc;
805 if (ErrorInfo != ~0U) {
806 if (ErrorInfo >= InstOperands.size())
807 return Error(IDLoc, "too few operands for instruction");
808
809 ErrorLoc = (static_cast<HexagonOperand *>(InstOperands[ErrorInfo].get()))
810 ->getStartLoc();
811 if (ErrorLoc == SMLoc())
812 ErrorLoc = IDLoc;
813 }
814 return Error(ErrorLoc, "invalid operand for instruction");
815 }
816 llvm_unreachable("Implement any new match types added!");
817}
818
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000819bool HexagonAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
820 OperandVector &Operands,
821 MCStreamer &Out,
822 uint64_t &ErrorInfo,
823 bool MatchingInlineAsm) {
824 if (!InBrackets) {
825 MCB.clear();
826 MCB.addOperand(MCOperand::createImm(0));
827 }
828 HexagonOperand &FirstOperand = static_cast<HexagonOperand &>(*Operands[0]);
829 if (FirstOperand.isToken() && FirstOperand.getToken() == "{") {
830 assert(Operands.size() == 1 && "Brackets should be by themselves");
831 if (InBrackets) {
832 getParser().Error(IDLoc, "Already in a packet");
833 return true;
834 }
835 InBrackets = true;
836 return false;
837 }
838 if (FirstOperand.isToken() && FirstOperand.getToken() == "}") {
839 assert(Operands.size() == 1 && "Brackets should be by themselves");
840 if (!InBrackets) {
841 getParser().Error(IDLoc, "Not in a packet");
842 return true;
843 }
844 InBrackets = false;
845 if (matchBundleOptions())
846 return true;
847 return finishBundle(IDLoc, Out);
848 }
849 MCInst *SubInst = new (getParser().getContext()) MCInst;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000850 if (matchOneInstruction(*SubInst, IDLoc, Operands, ErrorInfo,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000851 MatchingInlineAsm))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000852 return true;
853 HexagonMCInstrInfo::extendIfNeeded(
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000854 getParser().getContext(), MCII, MCB, *SubInst);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000855 MCB.addOperand(MCOperand::createInst(SubInst));
856 if (!InBrackets)
857 return finishBundle(IDLoc, Out);
858 return false;
859}
860
861/// ParseDirective parses the Hexagon specific directives
862bool HexagonAsmParser::ParseDirective(AsmToken DirectiveID) {
863 StringRef IDVal = DirectiveID.getIdentifier();
864 if ((IDVal.lower() == ".word") || (IDVal.lower() == ".4byte"))
865 return ParseDirectiveValue(4, DirectiveID.getLoc());
866 if (IDVal.lower() == ".short" || IDVal.lower() == ".hword" ||
867 IDVal.lower() == ".half")
868 return ParseDirectiveValue(2, DirectiveID.getLoc());
869 if (IDVal.lower() == ".falign")
870 return ParseDirectiveFalign(256, DirectiveID.getLoc());
871 if ((IDVal.lower() == ".lcomm") || (IDVal.lower() == ".lcommon"))
872 return ParseDirectiveComm(true, DirectiveID.getLoc());
873 if ((IDVal.lower() == ".comm") || (IDVal.lower() == ".common"))
874 return ParseDirectiveComm(false, DirectiveID.getLoc());
875 if (IDVal.lower() == ".subsection")
876 return ParseDirectiveSubsection(DirectiveID.getLoc());
877
878 return true;
879}
880bool HexagonAsmParser::ParseDirectiveSubsection(SMLoc L) {
881 const MCExpr *Subsection = 0;
882 int64_t Res;
883
884 assert((getLexer().isNot(AsmToken::EndOfStatement)) &&
885 "Invalid subsection directive");
886 getParser().parseExpression(Subsection);
887
888 if (!Subsection->evaluateAsAbsolute(Res))
889 return Error(L, "Cannot evaluate subsection number");
890
891 if (getLexer().isNot(AsmToken::EndOfStatement))
892 return TokError("unexpected token in directive");
893
894 // 0-8192 is the hard-coded range in MCObjectStreamper.cpp, this keeps the
895 // negative subsections together and in the same order but at the opposite
896 // end of the section. Only legacy hexagon-gcc created assembly code
897 // used negative subsections.
898 if ((Res < 0) && (Res > -8193))
Colin LeMahieuc7b21242016-02-15 18:47:55 +0000899 Subsection = HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000900 MCConstantExpr::create(8192 + Res, getContext()), getContext());
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000901
902 getStreamer().SubSection(Subsection);
903 return false;
904}
905
906/// ::= .falign [expression]
907bool HexagonAsmParser::ParseDirectiveFalign(unsigned Size, SMLoc L) {
908
909 int64_t MaxBytesToFill = 15;
910
911 // if there is an arguement
912 if (getLexer().isNot(AsmToken::EndOfStatement)) {
913 const MCExpr *Value;
914 SMLoc ExprLoc = L;
915
916 // Make sure we have a number (false is returned if expression is a number)
917 if (getParser().parseExpression(Value) == false) {
918 // Make sure this is a number that is in range
919 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
920 uint64_t IntValue = MCE->getValue();
921 if (!isUIntN(Size, IntValue) && !isIntN(Size, IntValue))
922 return Error(ExprLoc, "literal value out of range (256) for falign");
923 MaxBytesToFill = IntValue;
924 Lex();
925 } else {
926 return Error(ExprLoc, "not a valid expression for falign directive");
927 }
928 }
929
930 getTargetStreamer().emitFAlign(16, MaxBytesToFill);
931 Lex();
932
933 return false;
934}
935
936/// ::= .word [ expression (, expression)* ]
937bool HexagonAsmParser::ParseDirectiveValue(unsigned Size, SMLoc L) {
938 if (getLexer().isNot(AsmToken::EndOfStatement)) {
939
940 for (;;) {
941 const MCExpr *Value;
942 SMLoc ExprLoc = L;
943 if (getParser().parseExpression(Value))
944 return true;
945
946 // Special case constant expressions to match code generator.
947 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
948 assert(Size <= 8 && "Invalid size");
949 uint64_t IntValue = MCE->getValue();
950 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
951 return Error(ExprLoc, "literal value out of range for directive");
952 getStreamer().EmitIntValue(IntValue, Size);
953 } else
954 getStreamer().EmitValue(Value, Size);
955
956 if (getLexer().is(AsmToken::EndOfStatement))
957 break;
958
959 // FIXME: Improve diagnostic.
960 if (getLexer().isNot(AsmToken::Comma))
961 return TokError("unexpected token in directive");
962 Lex();
963 }
964 }
965
966 Lex();
967 return false;
968}
969
970// This is largely a copy of AsmParser's ParseDirectiveComm extended to
971// accept a 3rd argument, AccessAlignment which indicates the smallest
972// memory access made to the symbol, expressed in bytes. If no
973// AccessAlignment is specified it defaults to the Alignment Value.
974// Hexagon's .lcomm:
975// .lcomm Symbol, Length, Alignment, AccessAlignment
976bool HexagonAsmParser::ParseDirectiveComm(bool IsLocal, SMLoc Loc) {
977 // FIXME: need better way to detect if AsmStreamer (upstream removed
978 // getKind())
979 if (getStreamer().hasRawTextSupport())
980 return true; // Only object file output requires special treatment.
981
982 StringRef Name;
983 if (getParser().parseIdentifier(Name))
984 return TokError("expected identifier in directive");
985 // Handle the identifier as the key symbol.
986 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
987
988 if (getLexer().isNot(AsmToken::Comma))
989 return TokError("unexpected token in directive");
990 Lex();
991
992 int64_t Size;
993 SMLoc SizeLoc = getLexer().getLoc();
994 if (getParser().parseAbsoluteExpression(Size))
995 return true;
996
997 int64_t ByteAlignment = 1;
998 SMLoc ByteAlignmentLoc;
999 if (getLexer().is(AsmToken::Comma)) {
1000 Lex();
1001 ByteAlignmentLoc = getLexer().getLoc();
1002 if (getParser().parseAbsoluteExpression(ByteAlignment))
1003 return true;
1004 if (!isPowerOf2_64(ByteAlignment))
1005 return Error(ByteAlignmentLoc, "alignment must be a power of 2");
1006 }
1007
1008 int64_t AccessAlignment = 0;
1009 if (getLexer().is(AsmToken::Comma)) {
1010 // The optional access argument specifies the size of the smallest memory
1011 // access to be made to the symbol, expressed in bytes.
1012 SMLoc AccessAlignmentLoc;
1013 Lex();
1014 AccessAlignmentLoc = getLexer().getLoc();
1015 if (getParser().parseAbsoluteExpression(AccessAlignment))
1016 return true;
1017
1018 if (!isPowerOf2_64(AccessAlignment))
1019 return Error(AccessAlignmentLoc, "access alignment must be a power of 2");
1020 }
1021
1022 if (getLexer().isNot(AsmToken::EndOfStatement))
1023 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1024
1025 Lex();
1026
1027 // NOTE: a size of zero for a .comm should create a undefined symbol
1028 // but a size of .lcomm creates a bss symbol of size zero.
1029 if (Size < 0)
1030 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1031 "be less than zero");
1032
1033 // NOTE: The alignment in the directive is a power of 2 value, the assembler
1034 // may internally end up wanting an alignment in bytes.
1035 // FIXME: Diagnose overflow.
1036 if (ByteAlignment < 0)
1037 return Error(ByteAlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1038 "alignment, can't be less than zero");
1039
1040 if (!Sym->isUndefined())
1041 return Error(Loc, "invalid symbol redefinition");
1042
1043 HexagonMCELFStreamer &HexagonELFStreamer =
1044 static_cast<HexagonMCELFStreamer &>(getStreamer());
1045 if (IsLocal) {
1046 HexagonELFStreamer.HexagonMCEmitLocalCommonSymbol(Sym, Size, ByteAlignment,
1047 AccessAlignment);
1048 return false;
1049 }
1050
1051 HexagonELFStreamer.HexagonMCEmitCommonSymbol(Sym, Size, ByteAlignment,
1052 AccessAlignment);
1053 return false;
1054}
1055
1056// validate register against architecture
1057bool HexagonAsmParser::RegisterMatchesArch(unsigned MatchNum) const {
1058 return true;
1059}
1060
1061// extern "C" void LLVMInitializeHexagonAsmLexer();
1062
1063/// Force static initialization.
1064extern "C" void LLVMInitializeHexagonAsmParser() {
1065 RegisterMCAsmParser<HexagonAsmParser> X(TheHexagonTarget);
1066}
1067
1068#define GET_MATCHER_IMPLEMENTATION
1069#define GET_REGISTER_MATCHER
1070#include "HexagonGenAsmMatcher.inc"
1071
1072namespace {
1073bool previousEqual(OperandVector &Operands, size_t Index, StringRef String) {
1074 if (Index >= Operands.size())
1075 return false;
1076 MCParsedAsmOperand &Operand = *Operands[Operands.size() - Index - 1];
1077 if (!Operand.isToken())
1078 return false;
1079 return static_cast<HexagonOperand &>(Operand).getToken().equals_lower(String);
1080}
1081bool previousIsLoop(OperandVector &Operands, size_t Index) {
1082 return previousEqual(Operands, Index, "loop0") ||
1083 previousEqual(Operands, Index, "loop1") ||
1084 previousEqual(Operands, Index, "sp1loop0") ||
1085 previousEqual(Operands, Index, "sp2loop0") ||
1086 previousEqual(Operands, Index, "sp3loop0");
1087}
1088}
1089
1090bool HexagonAsmParser::splitIdentifier(OperandVector &Operands) {
1091 AsmToken const &Token = getParser().getTok();
1092 StringRef String = Token.getString();
1093 SMLoc Loc = Token.getLoc();
Nirav Davefd910412016-06-17 16:06:17 +00001094 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001095 do {
1096 std::pair<StringRef, StringRef> HeadTail = String.split('.');
1097 if (!HeadTail.first.empty())
1098 Operands.push_back(HexagonOperand::CreateToken(HeadTail.first, Loc));
1099 if (!HeadTail.second.empty())
1100 Operands.push_back(HexagonOperand::CreateToken(
1101 String.substr(HeadTail.first.size(), 1), Loc));
1102 String = HeadTail.second;
1103 } while (!String.empty());
1104 return false;
1105}
1106
1107bool HexagonAsmParser::parseOperand(OperandVector &Operands) {
1108 unsigned Register;
1109 SMLoc Begin;
1110 SMLoc End;
1111 MCAsmLexer &Lexer = getLexer();
1112 if (!ParseRegister(Register, Begin, End)) {
1113 if (!ErrorMissingParenthesis)
1114 switch (Register) {
1115 default:
1116 break;
1117 case Hexagon::P0:
1118 case Hexagon::P1:
1119 case Hexagon::P2:
1120 case Hexagon::P3:
1121 if (previousEqual(Operands, 0, "if")) {
1122 if (WarnMissingParenthesis)
1123 Warning (Begin, "Missing parenthesis around predicate register");
1124 static char const *LParen = "(";
1125 static char const *RParen = ")";
1126 Operands.push_back(HexagonOperand::CreateToken(LParen, Begin));
1127 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End));
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +00001128 const AsmToken &MaybeDotNew = Lexer.getTok();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001129 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) &&
1130 MaybeDotNew.getString().equals_lower(".new"))
1131 splitIdentifier(Operands);
1132 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin));
1133 return false;
1134 }
1135 if (previousEqual(Operands, 0, "!") &&
1136 previousEqual(Operands, 1, "if")) {
1137 if (WarnMissingParenthesis)
1138 Warning (Begin, "Missing parenthesis around predicate register");
1139 static char const *LParen = "(";
1140 static char const *RParen = ")";
1141 Operands.insert(Operands.end () - 1,
1142 HexagonOperand::CreateToken(LParen, Begin));
1143 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End));
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +00001144 const AsmToken &MaybeDotNew = Lexer.getTok();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001145 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) &&
1146 MaybeDotNew.getString().equals_lower(".new"))
1147 splitIdentifier(Operands);
1148 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin));
1149 return false;
1150 }
1151 break;
1152 }
1153 Operands.push_back(HexagonOperand::CreateReg(
1154 Register, Begin, End));
1155 return false;
1156 }
1157 return splitIdentifier(Operands);
1158}
1159
1160bool HexagonAsmParser::isLabel(AsmToken &Token) {
1161 MCAsmLexer &Lexer = getLexer();
1162 AsmToken const &Second = Lexer.getTok();
1163 AsmToken Third = Lexer.peekTok();
1164 StringRef String = Token.getString();
1165 if (Token.is(AsmToken::TokenKind::LCurly) ||
1166 Token.is(AsmToken::TokenKind::RCurly))
1167 return false;
1168 if (!Token.is(AsmToken::TokenKind::Identifier))
1169 return true;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001170 if (!matchRegister(String.lower()))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001171 return true;
Colin LeMahieu9d851f02015-11-09 21:06:28 +00001172 (void)Second;
1173 assert(Second.is(AsmToken::Colon));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001174 StringRef Raw (String.data(), Third.getString().data() - String.data() +
1175 Third.getString().size());
1176 std::string Collapsed = Raw;
David Majnemerc7004902016-08-12 04:32:37 +00001177 Collapsed.erase(remove_if(Collapsed, isspace), Collapsed.end());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001178 StringRef Whole = Collapsed;
1179 std::pair<StringRef, StringRef> DotSplit = Whole.split('.');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001180 if (!matchRegister(DotSplit.first.lower()))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001181 return true;
1182 return false;
1183}
1184
1185bool HexagonAsmParser::handleNoncontigiousRegister(bool Contigious, SMLoc &Loc) {
1186 if (!Contigious && ErrorNoncontigiousRegister) {
1187 Error(Loc, "Register name is not contigious");
1188 return true;
1189 }
1190 if (!Contigious && WarnNoncontigiousRegister)
1191 Warning(Loc, "Register name is not contigious");
1192 return false;
1193}
1194
1195bool HexagonAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) {
1196 MCAsmLexer &Lexer = getLexer();
1197 StartLoc = getLexer().getLoc();
1198 SmallVector<AsmToken, 5> Lookahead;
1199 StringRef RawString(Lexer.getTok().getString().data(), 0);
1200 bool Again = Lexer.is(AsmToken::Identifier);
1201 bool NeededWorkaround = false;
1202 while (Again) {
1203 AsmToken const &Token = Lexer.getTok();
1204 RawString = StringRef(RawString.data(),
1205 Token.getString().data() - RawString.data () +
1206 Token.getString().size());
1207 Lookahead.push_back(Token);
1208 Lexer.Lex();
1209 bool Contigious = Lexer.getTok().getString().data() ==
1210 Lookahead.back().getString().data() +
1211 Lookahead.back().getString().size();
1212 bool Type = Lexer.is(AsmToken::Identifier) || Lexer.is(AsmToken::Dot) ||
1213 Lexer.is(AsmToken::Integer) || Lexer.is(AsmToken::Real) ||
1214 Lexer.is(AsmToken::Colon);
1215 bool Workaround = Lexer.is(AsmToken::Colon) ||
1216 Lookahead.back().is(AsmToken::Colon);
1217 Again = (Contigious && Type) || (Workaround && Type);
1218 NeededWorkaround = NeededWorkaround || (Again && !(Contigious && Type));
1219 }
1220 std::string Collapsed = RawString;
David Majnemerc7004902016-08-12 04:32:37 +00001221 Collapsed.erase(remove_if(Collapsed, isspace), Collapsed.end());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001222 StringRef FullString = Collapsed;
1223 std::pair<StringRef, StringRef> DotSplit = FullString.split('.');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001224 unsigned DotReg = matchRegister(DotSplit.first.lower());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001225 if (DotReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1226 if (DotSplit.second.empty()) {
1227 RegNo = DotReg;
1228 EndLoc = Lexer.getLoc();
1229 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1230 return true;
1231 return false;
1232 } else {
1233 RegNo = DotReg;
1234 size_t First = RawString.find('.');
1235 StringRef DotString (RawString.data() + First, RawString.size() - First);
1236 Lexer.UnLex(AsmToken(AsmToken::Identifier, DotString));
1237 EndLoc = Lexer.getLoc();
1238 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1239 return true;
1240 return false;
1241 }
1242 }
1243 std::pair<StringRef, StringRef> ColonSplit = StringRef(FullString).split(':');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001244 unsigned ColonReg = matchRegister(ColonSplit.first.lower());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001245 if (ColonReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1246 Lexer.UnLex(Lookahead.back());
1247 Lookahead.pop_back();
1248 Lexer.UnLex(Lookahead.back());
1249 Lookahead.pop_back();
1250 RegNo = ColonReg;
1251 EndLoc = Lexer.getLoc();
1252 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1253 return true;
1254 return false;
1255 }
1256 while (!Lookahead.empty()) {
1257 Lexer.UnLex(Lookahead.back());
1258 Lookahead.pop_back();
1259 }
1260 return true;
1261}
1262
1263bool HexagonAsmParser::implicitExpressionLocation(OperandVector &Operands) {
1264 if (previousEqual(Operands, 0, "call"))
1265 return true;
1266 if (previousEqual(Operands, 0, "jump"))
1267 if (!getLexer().getTok().is(AsmToken::Colon))
1268 return true;
1269 if (previousEqual(Operands, 0, "(") && previousIsLoop(Operands, 1))
1270 return true;
1271 if (previousEqual(Operands, 1, ":") && previousEqual(Operands, 2, "jump") &&
1272 (previousEqual(Operands, 0, "nt") || previousEqual(Operands, 0, "t")))
1273 return true;
1274 return false;
1275}
1276
1277bool HexagonAsmParser::parseExpression(MCExpr const *& Expr) {
1278 llvm::SmallVector<AsmToken, 4> Tokens;
1279 MCAsmLexer &Lexer = getLexer();
1280 bool Done = false;
1281 static char const * Comma = ",";
1282 do {
1283 Tokens.emplace_back (Lexer.getTok());
Nirav Davefd910412016-06-17 16:06:17 +00001284 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001285 switch (Tokens.back().getKind())
1286 {
1287 case AsmToken::TokenKind::Hash:
1288 if (Tokens.size () > 1)
1289 if ((Tokens.end () - 2)->getKind() == AsmToken::TokenKind::Plus) {
1290 Tokens.insert(Tokens.end() - 2,
1291 AsmToken(AsmToken::TokenKind::Comma, Comma));
1292 Done = true;
1293 }
1294 break;
1295 case AsmToken::TokenKind::RCurly:
1296 case AsmToken::TokenKind::EndOfStatement:
1297 case AsmToken::TokenKind::Eof:
1298 Done = true;
1299 break;
1300 default:
1301 break;
1302 }
1303 } while (!Done);
1304 while (!Tokens.empty()) {
1305 Lexer.UnLex(Tokens.back());
1306 Tokens.pop_back();
1307 }
1308 return getParser().parseExpression(Expr);
1309}
1310
1311bool HexagonAsmParser::parseExpressionOrOperand(OperandVector &Operands) {
1312 if (implicitExpressionLocation(Operands)) {
1313 MCAsmParser &Parser = getParser();
1314 SMLoc Loc = Parser.getLexer().getLoc();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001315 MCExpr const *Expr = nullptr;
1316 bool Error = parseExpression(Expr);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001317 Expr = HexagonMCExpr::create(Expr, getContext());
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001318 if (!Error)
1319 Operands.push_back(HexagonOperand::CreateImm(Expr, Loc, Loc));
1320 return Error;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001321 }
1322 return parseOperand(Operands);
1323}
1324
1325/// Parse an instruction.
1326bool HexagonAsmParser::parseInstruction(OperandVector &Operands) {
1327 MCAsmParser &Parser = getParser();
1328 MCAsmLexer &Lexer = getLexer();
1329 while (true) {
1330 AsmToken const &Token = Parser.getTok();
1331 switch (Token.getKind()) {
1332 case AsmToken::EndOfStatement: {
Nirav Davefd910412016-06-17 16:06:17 +00001333 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001334 return false;
1335 }
1336 case AsmToken::LCurly: {
1337 if (!Operands.empty())
1338 return true;
1339 Operands.push_back(
1340 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001341 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001342 return false;
1343 }
1344 case AsmToken::RCurly: {
1345 if (Operands.empty()) {
1346 Operands.push_back(
1347 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001348 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001349 }
1350 return false;
1351 }
1352 case AsmToken::Comma: {
Nirav Davefd910412016-06-17 16:06:17 +00001353 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001354 continue;
1355 }
1356 case AsmToken::EqualEqual:
1357 case AsmToken::ExclaimEqual:
1358 case AsmToken::GreaterEqual:
1359 case AsmToken::GreaterGreater:
1360 case AsmToken::LessEqual:
1361 case AsmToken::LessLess: {
1362 Operands.push_back(HexagonOperand::CreateToken(
1363 Token.getString().substr(0, 1), Token.getLoc()));
1364 Operands.push_back(HexagonOperand::CreateToken(
1365 Token.getString().substr(1, 1), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001366 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001367 continue;
1368 }
1369 case AsmToken::Hash: {
1370 bool MustNotExtend = false;
1371 bool ImplicitExpression = implicitExpressionLocation(Operands);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001372 SMLoc ExprLoc = Lexer.getLoc();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001373 if (!ImplicitExpression)
1374 Operands.push_back(
1375 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001376 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001377 bool MustExtend = false;
1378 bool HiOnly = false;
1379 bool LoOnly = false;
1380 if (Lexer.is(AsmToken::Hash)) {
Nirav Davefd910412016-06-17 16:06:17 +00001381 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001382 MustExtend = true;
1383 } else if (ImplicitExpression)
1384 MustNotExtend = true;
1385 AsmToken const &Token = Parser.getTok();
1386 if (Token.is(AsmToken::Identifier)) {
1387 StringRef String = Token.getString();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001388 if (String.lower() == "hi") {
1389 HiOnly = true;
1390 } else if (String.lower() == "lo") {
1391 LoOnly = true;
1392 }
1393 if (HiOnly || LoOnly) {
1394 AsmToken LParen = Lexer.peekTok();
1395 if (!LParen.is(AsmToken::LParen)) {
1396 HiOnly = false;
1397 LoOnly = false;
1398 } else {
Nirav Davefd910412016-06-17 16:06:17 +00001399 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001400 }
1401 }
1402 }
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001403 MCExpr const *Expr = nullptr;
1404 if (parseExpression(Expr))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001405 return true;
1406 int64_t Value;
1407 MCContext &Context = Parser.getContext();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001408 assert(Expr != nullptr);
1409 if (Expr->evaluateAsAbsolute(Value)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001410 if (HiOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001411 Expr = MCBinaryExpr::createLShr(
1412 Expr, MCConstantExpr::create(16, Context), Context);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001413 if (HiOnly || LoOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001414 Expr = MCBinaryExpr::createAnd(Expr,
1415 MCConstantExpr::create(0xffff, Context),
1416 Context);
Colin LeMahieu5cb6eea2016-03-01 21:37:41 +00001417 } else {
1418 MCValue Value;
1419 if (Expr->evaluateAsRelocatable(Value, nullptr, nullptr)) {
1420 if (!Value.isAbsolute()) {
1421 switch(Value.getAccessVariant()) {
1422 case MCSymbolRefExpr::VariantKind::VK_TPREL:
1423 case MCSymbolRefExpr::VariantKind::VK_DTPREL:
1424 // Don't lazy extend these expression variants
1425 MustNotExtend = !MustExtend;
1426 break;
1427 default:
1428 break;
1429 }
1430 }
1431 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001432 }
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001433 Expr = HexagonMCExpr::create(Expr, Context);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001434 HexagonMCInstrInfo::setMustNotExtend(*Expr, MustNotExtend);
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001435 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001436 std::unique_ptr<HexagonOperand> Operand =
1437 HexagonOperand::CreateImm(Expr, ExprLoc, ExprLoc);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001438 Operands.push_back(std::move(Operand));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001439 continue;
1440 }
1441 default:
1442 break;
1443 }
1444 if (parseExpressionOrOperand(Operands))
1445 return true;
1446 }
1447}
1448
1449bool HexagonAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1450 StringRef Name,
1451 AsmToken ID,
1452 OperandVector &Operands) {
1453 getLexer().UnLex(ID);
1454 return parseInstruction(Operands);
1455}
1456
1457namespace {
1458MCInst makeCombineInst(int opCode, MCOperand &Rdd,
1459 MCOperand &MO1, MCOperand &MO2) {
1460 MCInst TmpInst;
1461 TmpInst.setOpcode(opCode);
1462 TmpInst.addOperand(Rdd);
1463 TmpInst.addOperand(MO1);
1464 TmpInst.addOperand(MO2);
1465
1466 return TmpInst;
1467}
1468}
1469
1470// Define this matcher function after the auto-generated include so we
1471// have the match class enum definitions.
1472unsigned HexagonAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
1473 unsigned Kind) {
1474 HexagonOperand *Op = static_cast<HexagonOperand *>(&AsmOp);
1475
1476 switch (Kind) {
1477 case MCK_0: {
1478 int64_t Value;
1479 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 0
1480 ? Match_Success
1481 : Match_InvalidOperand;
1482 }
1483 case MCK_1: {
1484 int64_t Value;
1485 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 1
1486 ? Match_Success
1487 : Match_InvalidOperand;
1488 }
1489 case MCK__MINUS_1: {
1490 int64_t Value;
1491 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == -1
1492 ? Match_Success
1493 : Match_InvalidOperand;
1494 }
1495 }
1496 if (Op->Kind == HexagonOperand::Token && Kind != InvalidMatchClass) {
1497 StringRef myStringRef = StringRef(Op->Tok.Data, Op->Tok.Length);
1498 if (matchTokenString(myStringRef.lower()) == (MatchClassKind)Kind)
1499 return Match_Success;
1500 if (matchTokenString(myStringRef.upper()) == (MatchClassKind)Kind)
1501 return Match_Success;
1502 }
1503
1504 DEBUG(dbgs() << "Unmatched Operand:");
1505 DEBUG(Op->dump());
1506 DEBUG(dbgs() << "\n");
1507
1508 return Match_InvalidOperand;
1509}
1510
1511void HexagonAsmParser::OutOfRange(SMLoc IDLoc, long long Val, long long Max) {
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001512 std::string errStr;
1513 raw_string_ostream ES(errStr);
Alexey Samsonov44ff2042015-12-02 22:59:22 +00001514 ES << "value " << Val << "(" << format_hex(Val, 0) << ") out of range: ";
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001515 if (Max >= 0)
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001516 ES << "0-" << Max;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001517 else
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001518 ES << Max << "-" << (-Max - 1);
1519 Error(IDLoc, ES.str().c_str());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001520}
1521
1522int HexagonAsmParser::processInstruction(MCInst &Inst,
1523 OperandVector const &Operands,
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001524 SMLoc IDLoc) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001525 MCContext &Context = getParser().getContext();
1526 const MCRegisterInfo *RI = getContext().getRegisterInfo();
1527 std::string r = "r";
1528 std::string v = "v";
1529 std::string Colon = ":";
1530
1531 bool is32bit = false; // used to distinguish between CONST32 and CONST64
1532 switch (Inst.getOpcode()) {
1533 default:
1534 break;
1535
Colin LeMahieuecef1d92016-02-16 20:38:17 +00001536 case Hexagon::A2_iconst: {
1537 Inst.setOpcode(Hexagon::A2_addi);
1538 MCOperand Reg = Inst.getOperand(0);
1539 MCOperand S16 = Inst.getOperand(1);
1540 HexagonMCInstrInfo::setMustNotExtend(*S16.getExpr());
1541 HexagonMCInstrInfo::setS23_2_reloc(*S16.getExpr());
1542 Inst.clear();
1543 Inst.addOperand(Reg);
1544 Inst.addOperand(MCOperand::createReg(Hexagon::R0));
1545 Inst.addOperand(S16);
1546 break;
1547 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001548 case Hexagon::M4_mpyrr_addr:
1549 case Hexagon::S4_addi_asl_ri:
1550 case Hexagon::S4_addi_lsr_ri:
1551 case Hexagon::S4_andi_asl_ri:
1552 case Hexagon::S4_andi_lsr_ri:
1553 case Hexagon::S4_ori_asl_ri:
1554 case Hexagon::S4_ori_lsr_ri:
1555 case Hexagon::S4_or_andix:
1556 case Hexagon::S4_subi_asl_ri:
1557 case Hexagon::S4_subi_lsr_ri: {
1558 MCOperand &Ry = Inst.getOperand(0);
1559 MCOperand &src = Inst.getOperand(2);
1560 if (RI->getEncodingValue(Ry.getReg()) != RI->getEncodingValue(src.getReg()))
1561 return Match_InvalidOperand;
1562 break;
1563 }
1564
1565 case Hexagon::C2_cmpgei: {
1566 MCOperand &MO = Inst.getOperand(2);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001567 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001568 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001569 Inst.setOpcode(Hexagon::C2_cmpgti);
1570 break;
1571 }
1572
1573 case Hexagon::C2_cmpgeui: {
1574 MCOperand &MO = Inst.getOperand(2);
1575 int64_t Value;
1576 bool Success = MO.getExpr()->evaluateAsAbsolute(Value);
Colin LeMahieu9d851f02015-11-09 21:06:28 +00001577 (void)Success;
1578 assert(Success && "Assured by matcher");
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001579 if (Value == 0) {
1580 MCInst TmpInst;
1581 MCOperand &Pd = Inst.getOperand(0);
1582 MCOperand &Rt = Inst.getOperand(1);
1583 TmpInst.setOpcode(Hexagon::C2_cmpeq);
1584 TmpInst.addOperand(Pd);
1585 TmpInst.addOperand(Rt);
1586 TmpInst.addOperand(Rt);
1587 Inst = TmpInst;
1588 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001589 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001590 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001591 Inst.setOpcode(Hexagon::C2_cmpgtui);
1592 }
1593 break;
1594 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001595
1596 // Translate a "$Rdd = $Rss" to "$Rdd = combine($Rs, $Rt)"
1597 case Hexagon::A2_tfrp: {
1598 MCOperand &MO = Inst.getOperand(1);
1599 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001600 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001601 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001602 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001603 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001604 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001605 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001606 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001607 Inst.setOpcode(Hexagon::A2_combinew);
1608 break;
1609 }
1610
1611 case Hexagon::A2_tfrpt:
1612 case Hexagon::A2_tfrpf: {
1613 MCOperand &MO = Inst.getOperand(2);
1614 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001615 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001616 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001617 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001618 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001619 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001620 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001621 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001622 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrpt)
1623 ? Hexagon::C2_ccombinewt
1624 : Hexagon::C2_ccombinewf);
1625 break;
1626 }
1627 case Hexagon::A2_tfrptnew:
1628 case Hexagon::A2_tfrpfnew: {
1629 MCOperand &MO = Inst.getOperand(2);
1630 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001631 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001632 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001633 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001634 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001635 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001636 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001637 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001638 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrptnew)
1639 ? Hexagon::C2_ccombinewnewt
1640 : Hexagon::C2_ccombinewnewf);
1641 break;
1642 }
1643
Krzysztof Parzyszek0e7d2d32016-04-28 16:43:16 +00001644 // Translate a "$Vdd = $Vss" to "$Vdd = vcombine($Vs, $Vt)"
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001645 case Hexagon::V6_vassignp: {
Krzysztof Parzyszek0e7d2d32016-04-28 16:43:16 +00001646 MCOperand &MO = Inst.getOperand(1);
1647 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
1648 std::string R1 = v + llvm::utostr(RegPairNum + 1);
1649 MO.setReg(MatchRegisterName(R1));
1650 // Add a new operand for the second register in the pair.
1651 std::string R2 = v + llvm::utostr(RegPairNum);
1652 Inst.addOperand(MCOperand::createReg(MatchRegisterName(R2)));
1653 Inst.setOpcode(Hexagon::V6_vcombine);
1654 break;
1655 }
1656
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001657 // Translate a "$Rx = CONST32(#imm)" to "$Rx = memw(gp+#LABEL) "
1658 case Hexagon::CONST32:
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001659 is32bit = true;
1660 // Translate a "$Rx:y = CONST64(#imm)" to "$Rx:y = memd(gp+#LABEL) "
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001661 case Hexagon::CONST64:
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001662 // FIXME: need better way to detect AsmStreamer (upstream removed getKind())
1663 if (!Parser.getStreamer().hasRawTextSupport()) {
1664 MCELFStreamer *MES = static_cast<MCELFStreamer *>(&Parser.getStreamer());
1665 MCOperand &MO_1 = Inst.getOperand(1);
1666 MCOperand &MO_0 = Inst.getOperand(0);
1667
1668 // push section onto section stack
1669 MES->PushSection();
1670
1671 std::string myCharStr;
1672 MCSectionELF *mySection;
1673
1674 // check if this as an immediate or a symbol
1675 int64_t Value;
1676 bool Absolute = MO_1.getExpr()->evaluateAsAbsolute(Value);
1677 if (Absolute) {
1678 // Create a new section - one for each constant
1679 // Some or all of the zeros are replaced with the given immediate.
1680 if (is32bit) {
1681 std::string myImmStr = utohexstr(static_cast<uint32_t>(Value));
1682 myCharStr = StringRef(".gnu.linkonce.l4.CONST_00000000")
1683 .drop_back(myImmStr.size())
1684 .str() +
1685 myImmStr;
1686 } else {
1687 std::string myImmStr = utohexstr(Value);
1688 myCharStr = StringRef(".gnu.linkonce.l8.CONST_0000000000000000")
1689 .drop_back(myImmStr.size())
1690 .str() +
1691 myImmStr;
1692 }
1693
1694 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1695 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1696 } else if (MO_1.isExpr()) {
1697 // .lita - for expressions
1698 myCharStr = ".lita";
1699 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1700 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1701 } else
1702 llvm_unreachable("unexpected type of machine operand!");
1703
1704 MES->SwitchSection(mySection);
1705 unsigned byteSize = is32bit ? 4 : 8;
1706 getStreamer().EmitCodeAlignment(byteSize, byteSize);
1707
1708 MCSymbol *Sym;
1709
1710 // for symbols, get rid of prepended ".gnu.linkonce.lx."
1711
1712 // emit symbol if needed
1713 if (Absolute) {
1714 Sym = getContext().getOrCreateSymbol(StringRef(myCharStr.c_str() + 16));
1715 if (Sym->isUndefined()) {
1716 getStreamer().EmitLabel(Sym);
1717 getStreamer().EmitSymbolAttribute(Sym, MCSA_Global);
1718 getStreamer().EmitIntValue(Value, byteSize);
1719 }
1720 } else if (MO_1.isExpr()) {
1721 const char *StringStart = 0;
1722 const char *StringEnd = 0;
1723 if (*Operands[4]->getStartLoc().getPointer() == '#') {
1724 StringStart = Operands[5]->getStartLoc().getPointer();
1725 StringEnd = Operands[6]->getStartLoc().getPointer();
1726 } else { // no pound
1727 StringStart = Operands[4]->getStartLoc().getPointer();
1728 StringEnd = Operands[5]->getStartLoc().getPointer();
1729 }
1730
1731 unsigned size = StringEnd - StringStart;
1732 std::string DotConst = ".CONST_";
1733 Sym = getContext().getOrCreateSymbol(DotConst +
1734 StringRef(StringStart, size));
1735
1736 if (Sym->isUndefined()) {
1737 // case where symbol is not yet defined: emit symbol
1738 getStreamer().EmitLabel(Sym);
1739 getStreamer().EmitSymbolAttribute(Sym, MCSA_Local);
1740 getStreamer().EmitValue(MO_1.getExpr(), 4);
1741 }
1742 } else
1743 llvm_unreachable("unexpected type of machine operand!");
1744
1745 MES->PopSection();
1746
1747 if (Sym) {
1748 MCInst TmpInst;
1749 if (is32bit) // 32 bit
1750 TmpInst.setOpcode(Hexagon::L2_loadrigp);
1751 else // 64 bit
1752 TmpInst.setOpcode(Hexagon::L2_loadrdgp);
1753
1754 TmpInst.addOperand(MO_0);
1755 TmpInst.addOperand(
1756 MCOperand::createExpr(MCSymbolRefExpr::create(Sym, getContext())));
1757 Inst = TmpInst;
1758 }
1759 }
1760 break;
1761
1762 // Translate a "$Rdd = #-imm" to "$Rdd = combine(#[-1,0], #-imm)"
1763 case Hexagon::A2_tfrpi: {
1764 MCOperand &Rdd = Inst.getOperand(0);
1765 MCOperand &MO = Inst.getOperand(1);
1766 int64_t Value;
1767 int sVal = (MO.getExpr()->evaluateAsAbsolute(Value) && Value < 0) ? -1 : 0;
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001768 MCOperand imm(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001769 HexagonMCExpr::create(MCConstantExpr::create(sVal, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001770 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, imm, MO);
1771 break;
1772 }
1773
1774 // Translate a "$Rdd = [#]#imm" to "$Rdd = combine(#, [#]#imm)"
1775 case Hexagon::TFRI64_V4: {
1776 MCOperand &Rdd = Inst.getOperand(0);
1777 MCOperand &MO = Inst.getOperand(1);
1778 int64_t Value;
1779 if (MO.getExpr()->evaluateAsAbsolute(Value)) {
David Majnemere61e4bf2016-06-21 05:10:24 +00001780 int s8 = Hi_32(Value);
1781 if (!isInt<8>(s8))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001782 OutOfRange(IDLoc, s8, -128);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001783 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001784 MCConstantExpr::create(s8, Context), Context))); // upper 32
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001785 auto Expr = HexagonMCExpr::create(
David Majnemere61e4bf2016-06-21 05:10:24 +00001786 MCConstantExpr::create(Lo_32(Value), Context), Context);
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001787 HexagonMCInstrInfo::setMustExtend(*Expr, HexagonMCInstrInfo::mustExtend(*MO.getExpr()));
1788 MCOperand imm2(MCOperand::createExpr(Expr)); // lower 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001789 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, imm2);
1790 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001791 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001792 MCConstantExpr::create(0, Context), Context))); // upper 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001793 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, MO);
1794 }
1795 break;
1796 }
1797
1798 // Handle $Rdd = combine(##imm, #imm)"
1799 case Hexagon::TFRI64_V2_ext: {
1800 MCOperand &Rdd = Inst.getOperand(0);
1801 MCOperand &MO1 = Inst.getOperand(1);
1802 MCOperand &MO2 = Inst.getOperand(2);
1803 int64_t Value;
1804 if (MO2.getExpr()->evaluateAsAbsolute(Value)) {
1805 int s8 = Value;
1806 if (s8 < -128 || s8 > 127)
1807 OutOfRange(IDLoc, s8, -128);
1808 }
1809 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, MO1, MO2);
1810 break;
1811 }
1812
1813 // Handle $Rdd = combine(#imm, ##imm)"
1814 case Hexagon::A4_combineii: {
1815 MCOperand &Rdd = Inst.getOperand(0);
1816 MCOperand &MO1 = Inst.getOperand(1);
1817 int64_t Value;
1818 if (MO1.getExpr()->evaluateAsAbsolute(Value)) {
1819 int s8 = Value;
1820 if (s8 < -128 || s8 > 127)
1821 OutOfRange(IDLoc, s8, -128);
1822 }
1823 MCOperand &MO2 = Inst.getOperand(2);
1824 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, MO1, MO2);
1825 break;
1826 }
1827
1828 case Hexagon::S2_tableidxb_goodsyntax: {
1829 Inst.setOpcode(Hexagon::S2_tableidxb);
1830 break;
1831 }
1832
1833 case Hexagon::S2_tableidxh_goodsyntax: {
1834 MCInst TmpInst;
1835 MCOperand &Rx = Inst.getOperand(0);
1836 MCOperand &_dst_ = Inst.getOperand(1);
1837 MCOperand &Rs = Inst.getOperand(2);
1838 MCOperand &Imm4 = Inst.getOperand(3);
1839 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001840 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001841 Imm6.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001842 TmpInst.setOpcode(Hexagon::S2_tableidxh);
1843 TmpInst.addOperand(Rx);
1844 TmpInst.addOperand(_dst_);
1845 TmpInst.addOperand(Rs);
1846 TmpInst.addOperand(Imm4);
1847 TmpInst.addOperand(Imm6);
1848 Inst = TmpInst;
1849 break;
1850 }
1851
1852 case Hexagon::S2_tableidxw_goodsyntax: {
1853 MCInst TmpInst;
1854 MCOperand &Rx = Inst.getOperand(0);
1855 MCOperand &_dst_ = Inst.getOperand(1);
1856 MCOperand &Rs = Inst.getOperand(2);
1857 MCOperand &Imm4 = Inst.getOperand(3);
1858 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001859 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001860 Imm6.getExpr(), MCConstantExpr::create(2, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001861 TmpInst.setOpcode(Hexagon::S2_tableidxw);
1862 TmpInst.addOperand(Rx);
1863 TmpInst.addOperand(_dst_);
1864 TmpInst.addOperand(Rs);
1865 TmpInst.addOperand(Imm4);
1866 TmpInst.addOperand(Imm6);
1867 Inst = TmpInst;
1868 break;
1869 }
1870
1871 case Hexagon::S2_tableidxd_goodsyntax: {
1872 MCInst TmpInst;
1873 MCOperand &Rx = Inst.getOperand(0);
1874 MCOperand &_dst_ = Inst.getOperand(1);
1875 MCOperand &Rs = Inst.getOperand(2);
1876 MCOperand &Imm4 = Inst.getOperand(3);
1877 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001878 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001879 Imm6.getExpr(), MCConstantExpr::create(3, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001880 TmpInst.setOpcode(Hexagon::S2_tableidxd);
1881 TmpInst.addOperand(Rx);
1882 TmpInst.addOperand(_dst_);
1883 TmpInst.addOperand(Rs);
1884 TmpInst.addOperand(Imm4);
1885 TmpInst.addOperand(Imm6);
1886 Inst = TmpInst;
1887 break;
1888 }
1889
1890 case Hexagon::M2_mpyui: {
1891 Inst.setOpcode(Hexagon::M2_mpyi);
1892 break;
1893 }
1894 case Hexagon::M2_mpysmi: {
1895 MCInst TmpInst;
1896 MCOperand &Rd = Inst.getOperand(0);
1897 MCOperand &Rs = Inst.getOperand(1);
1898 MCOperand &Imm = Inst.getOperand(2);
1899 int64_t Value;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001900 MCExpr const &Expr = *Imm.getExpr();
1901 bool Absolute = Expr.evaluateAsAbsolute(Value);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001902 assert(Absolute);
1903 (void)Absolute;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001904 if (!HexagonMCInstrInfo::mustExtend(Expr)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001905 if (Value < 0 && Value > -256) {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001906 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001907 MCConstantExpr::create(Value * -1, Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001908 TmpInst.setOpcode(Hexagon::M2_mpysin);
1909 } else if (Value < 256 && Value >= 0)
1910 TmpInst.setOpcode(Hexagon::M2_mpysip);
1911 else
1912 return Match_InvalidOperand;
1913 } else {
1914 if (Value >= 0)
1915 TmpInst.setOpcode(Hexagon::M2_mpysip);
1916 else
1917 return Match_InvalidOperand;
1918 }
1919 TmpInst.addOperand(Rd);
1920 TmpInst.addOperand(Rs);
1921 TmpInst.addOperand(Imm);
1922 Inst = TmpInst;
1923 break;
1924 }
1925
1926 case Hexagon::S2_asr_i_r_rnd_goodsyntax: {
1927 MCOperand &Imm = Inst.getOperand(2);
1928 MCInst TmpInst;
1929 int64_t Value;
1930 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1931 assert(Absolute);
1932 (void)Absolute;
1933 if (Value == 0) { // convert to $Rd = $Rs
1934 TmpInst.setOpcode(Hexagon::A2_tfr);
1935 MCOperand &Rd = Inst.getOperand(0);
1936 MCOperand &Rs = Inst.getOperand(1);
1937 TmpInst.addOperand(Rd);
1938 TmpInst.addOperand(Rs);
1939 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001940 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001941 MCBinaryExpr::createSub(Imm.getExpr(),
1942 MCConstantExpr::create(1, Context), Context),
1943 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001944 TmpInst.setOpcode(Hexagon::S2_asr_i_r_rnd);
1945 MCOperand &Rd = Inst.getOperand(0);
1946 MCOperand &Rs = Inst.getOperand(1);
1947 TmpInst.addOperand(Rd);
1948 TmpInst.addOperand(Rs);
1949 TmpInst.addOperand(Imm);
1950 }
1951 Inst = TmpInst;
1952 break;
1953 }
1954
1955 case Hexagon::S2_asr_i_p_rnd_goodsyntax: {
1956 MCOperand &Rdd = Inst.getOperand(0);
1957 MCOperand &Rss = Inst.getOperand(1);
1958 MCOperand &Imm = Inst.getOperand(2);
1959 int64_t Value;
1960 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1961 assert(Absolute);
1962 (void)Absolute;
1963 if (Value == 0) { // convert to $Rdd = combine ($Rs[0], $Rs[1])
1964 MCInst TmpInst;
1965 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001966 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001967 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001968 Rss.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001969 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001970 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001971 StringRef Reg2(R2);
1972 TmpInst.setOpcode(Hexagon::A2_combinew);
1973 TmpInst.addOperand(Rdd);
1974 TmpInst.addOperand(Rss);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001975 TmpInst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001976 Inst = TmpInst;
1977 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001978 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001979 MCBinaryExpr::createSub(Imm.getExpr(),
1980 MCConstantExpr::create(1, Context), Context),
1981 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001982 Inst.setOpcode(Hexagon::S2_asr_i_p_rnd);
1983 }
1984 break;
1985 }
1986
1987 case Hexagon::A4_boundscheck: {
1988 MCOperand &Rs = Inst.getOperand(1);
1989 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
1990 if (RegNum & 1) { // Odd mapped to raw:hi, regpair is rodd:odd-1, like r3:2
1991 Inst.setOpcode(Hexagon::A4_boundscheck_hi);
1992 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00001993 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001994 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001995 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001996 } else { // raw:lo
1997 Inst.setOpcode(Hexagon::A4_boundscheck_lo);
1998 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00001999 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002000 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002001 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002002 }
2003 break;
2004 }
2005
2006 case Hexagon::A2_addsp: {
2007 MCOperand &Rs = Inst.getOperand(1);
2008 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
2009 if (RegNum & 1) { // Odd mapped to raw:hi
2010 Inst.setOpcode(Hexagon::A2_addsph);
2011 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002012 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002013 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002014 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002015 } else { // Even mapped raw:lo
2016 Inst.setOpcode(Hexagon::A2_addspl);
2017 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002018 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002019 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002020 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002021 }
2022 break;
2023 }
2024
2025 case Hexagon::M2_vrcmpys_s1: {
2026 MCOperand &Rt = Inst.getOperand(2);
2027 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2028 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2029 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_h);
2030 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002031 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002032 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002033 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002034 } else { // Even mapped sat:raw:lo
2035 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_l);
2036 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002037 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002038 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002039 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002040 }
2041 break;
2042 }
2043
2044 case Hexagon::M2_vrcmpys_acc_s1: {
2045 MCInst TmpInst;
2046 MCOperand &Rxx = Inst.getOperand(0);
2047 MCOperand &Rss = Inst.getOperand(2);
2048 MCOperand &Rt = Inst.getOperand(3);
2049 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2050 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2051 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_h);
2052 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002053 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002054 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002055 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002056 } else { // Even mapped sat:raw:lo
2057 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_l);
2058 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002059 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002060 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002061 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002062 }
2063 // Registers are in different positions
2064 TmpInst.addOperand(Rxx);
2065 TmpInst.addOperand(Rxx);
2066 TmpInst.addOperand(Rss);
2067 TmpInst.addOperand(Rt);
2068 Inst = TmpInst;
2069 break;
2070 }
2071
2072 case Hexagon::M2_vrcmpys_s1rp: {
2073 MCOperand &Rt = Inst.getOperand(2);
2074 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2075 if (RegNum & 1) { // Odd mapped to rnd:sat:raw:hi
2076 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_h);
2077 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002078 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002079 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002080 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002081 } else { // Even mapped rnd:sat:raw:lo
2082 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_l);
2083 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002084 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002085 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002086 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002087 }
2088 break;
2089 }
2090
2091 case Hexagon::S5_asrhub_rnd_sat_goodsyntax: {
2092 MCOperand &Imm = Inst.getOperand(2);
2093 int64_t Value;
2094 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
2095 assert(Absolute);
2096 (void)Absolute;
2097 if (Value == 0)
2098 Inst.setOpcode(Hexagon::S2_vsathub);
2099 else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002100 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002101 MCBinaryExpr::createSub(Imm.getExpr(),
2102 MCConstantExpr::create(1, Context), Context),
2103 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002104 Inst.setOpcode(Hexagon::S5_asrhub_rnd_sat);
2105 }
2106 break;
2107 }
2108
2109 case Hexagon::S5_vasrhrnd_goodsyntax: {
2110 MCOperand &Rdd = Inst.getOperand(0);
2111 MCOperand &Rss = Inst.getOperand(1);
2112 MCOperand &Imm = Inst.getOperand(2);
2113 int64_t Value;
2114 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
2115 assert(Absolute);
2116 (void)Absolute;
2117 if (Value == 0) {
2118 MCInst TmpInst;
2119 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00002120 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002121 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002122 Rss.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002123 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00002124 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002125 StringRef Reg2(R2);
2126 TmpInst.setOpcode(Hexagon::A2_combinew);
2127 TmpInst.addOperand(Rdd);
2128 TmpInst.addOperand(Rss);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002129 TmpInst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002130 Inst = TmpInst;
2131 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002132 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002133 MCBinaryExpr::createSub(Imm.getExpr(),
2134 MCConstantExpr::create(1, Context), Context),
2135 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002136 Inst.setOpcode(Hexagon::S5_vasrhrnd);
2137 }
2138 break;
2139 }
2140
2141 case Hexagon::A2_not: {
2142 MCInst TmpInst;
2143 MCOperand &Rd = Inst.getOperand(0);
2144 MCOperand &Rs = Inst.getOperand(1);
2145 TmpInst.setOpcode(Hexagon::A2_subri);
2146 TmpInst.addOperand(Rd);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002147 TmpInst.addOperand(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002148 HexagonMCExpr::create(MCConstantExpr::create(-1, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002149 TmpInst.addOperand(Rs);
2150 Inst = TmpInst;
2151 break;
2152 }
2153 } // switch
2154
2155 return Match_Success;
2156}
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002157
2158
2159unsigned HexagonAsmParser::matchRegister(StringRef Name) {
2160 if (unsigned Reg = MatchRegisterName(Name))
2161 return Reg;
2162 return MatchRegisterAltName(Name);
2163}