blob: 6cf5f505fea8ff0d82f972921e9dba9fa55c1325 [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
154 bool mustExtend(OperandVector &Operands);
155 bool splitIdentifier(OperandVector &Operands);
156 bool parseOperand(OperandVector &Operands);
157 bool parseInstruction(OperandVector &Operands);
158 bool implicitExpressionLocation(OperandVector &Operands);
159 bool parseExpressionOrOperand(OperandVector &Operands);
160 bool parseExpression(MCExpr const *& Expr);
161 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
Colin LeMahieu9ea507e2015-11-09 07:10:24 +0000162 SMLoc NameLoc, OperandVector &Operands) override
163 {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000164 llvm_unreachable("Unimplemented");
165 }
166 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
Colin LeMahieu9ea507e2015-11-09 07:10:24 +0000167 AsmToken ID, OperandVector &Operands) override;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000168
Colin LeMahieu9ea507e2015-11-09 07:10:24 +0000169 virtual bool ParseDirective(AsmToken DirectiveID) override;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000170};
171
172/// HexagonOperand - Instances of this class represent a parsed Hexagon machine
173/// instruction.
174struct HexagonOperand : public MCParsedAsmOperand {
175 enum KindTy { Token, Immediate, Register } Kind;
176
177 SMLoc StartLoc, EndLoc;
178
179 struct TokTy {
180 const char *Data;
181 unsigned Length;
182 };
183
184 struct RegTy {
185 unsigned RegNum;
186 };
187
188 struct ImmTy {
189 const MCExpr *Val;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000190 };
191
192 struct InstTy {
193 OperandVector *SubInsts;
194 };
195
196 union {
197 struct TokTy Tok;
198 struct RegTy Reg;
199 struct ImmTy Imm;
200 };
201
202 HexagonOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
203
204public:
205 HexagonOperand(const HexagonOperand &o) : MCParsedAsmOperand() {
206 Kind = o.Kind;
207 StartLoc = o.StartLoc;
208 EndLoc = o.EndLoc;
209 switch (Kind) {
210 case Register:
211 Reg = o.Reg;
212 break;
213 case Immediate:
214 Imm = o.Imm;
215 break;
216 case Token:
217 Tok = o.Tok;
218 break;
219 }
220 }
221
222 /// getStartLoc - Get the location of the first token of this operand.
223 SMLoc getStartLoc() const { return StartLoc; }
224
225 /// getEndLoc - Get the location of the last token of this operand.
226 SMLoc getEndLoc() const { return EndLoc; }
227
228 unsigned getReg() const {
229 assert(Kind == Register && "Invalid access!");
230 return Reg.RegNum;
231 }
232
233 const MCExpr *getImm() const {
234 assert(Kind == Immediate && "Invalid access!");
235 return Imm.Val;
236 }
237
238 bool isToken() const { return Kind == Token; }
239 bool isImm() const { return Kind == Immediate; }
240 bool isMem() const { llvm_unreachable("No isMem"); }
241 bool isReg() const { return Kind == Register; }
242
243 bool CheckImmRange(int immBits, int zeroBits, bool isSigned,
244 bool isRelocatable, bool Extendable) const {
245 if (Kind == Immediate) {
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000246 const MCExpr *myMCExpr = &HexagonMCInstrInfo::getExpr(*getImm());
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000247 if (HexagonMCInstrInfo::mustExtend(*Imm.Val) && !Extendable)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000248 return false;
249 int64_t Res;
250 if (myMCExpr->evaluateAsAbsolute(Res)) {
251 int bits = immBits + zeroBits;
252 // Field bit range is zerobits + bits
253 // zeroBits must be 0
254 if (Res & ((1 << zeroBits) - 1))
255 return false;
256 if (isSigned) {
257 if (Res < (1LL << (bits - 1)) && Res >= -(1LL << (bits - 1)))
258 return true;
259 } else {
260 if (bits == 64)
261 return true;
262 if (Res >= 0)
263 return ((uint64_t)Res < (uint64_t)(1ULL << bits)) ? true : false;
264 else {
265 const int64_t high_bit_set = 1ULL << 63;
266 const uint64_t mask = (high_bit_set >> (63 - bits));
267 return (((uint64_t)Res & mask) == mask) ? true : false;
268 }
269 }
270 } else if (myMCExpr->getKind() == MCExpr::SymbolRef && isRelocatable)
271 return true;
272 else if (myMCExpr->getKind() == MCExpr::Binary ||
273 myMCExpr->getKind() == MCExpr::Unary)
274 return true;
275 }
276 return false;
277 }
278
279 bool isf32Ext() const { return false; }
280 bool iss32Imm() const { return CheckImmRange(32, 0, true, true, false); }
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000281 bool iss23_2Imm() const { return CheckImmRange(23, 2, true, true, false); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000282 bool iss8Imm() const { return CheckImmRange(8, 0, true, false, false); }
283 bool iss8Imm64() const { return CheckImmRange(8, 0, true, true, false); }
284 bool iss7Imm() const { return CheckImmRange(7, 0, true, false, false); }
285 bool iss6Imm() const { return CheckImmRange(6, 0, true, false, false); }
286 bool iss4Imm() const { return CheckImmRange(4, 0, true, false, false); }
287 bool iss4_0Imm() const { return CheckImmRange(4, 0, true, false, false); }
288 bool iss4_1Imm() const { return CheckImmRange(4, 1, true, false, false); }
289 bool iss4_2Imm() const { return CheckImmRange(4, 2, true, false, false); }
290 bool iss4_3Imm() const { return CheckImmRange(4, 3, true, false, false); }
291 bool iss4_6Imm() const { return CheckImmRange(4, 0, true, false, false); }
292 bool iss3_6Imm() const { return CheckImmRange(3, 0, true, false, false); }
293 bool iss3Imm() const { return CheckImmRange(3, 0, true, false, false); }
294
295 bool isu64Imm() const { return CheckImmRange(64, 0, false, true, true); }
296 bool isu32Imm() const { return CheckImmRange(32, 0, false, true, false); }
297 bool isu26_6Imm() const { return CheckImmRange(26, 6, false, true, false); }
298 bool isu16Imm() const { return CheckImmRange(16, 0, false, true, false); }
299 bool isu16_0Imm() const { return CheckImmRange(16, 0, false, true, false); }
300 bool isu16_1Imm() const { return CheckImmRange(16, 1, false, true, false); }
301 bool isu16_2Imm() const { return CheckImmRange(16, 2, false, true, false); }
302 bool isu16_3Imm() const { return CheckImmRange(16, 3, false, true, false); }
303 bool isu11_3Imm() const { return CheckImmRange(11, 3, false, false, false); }
304 bool isu6_0Imm() const { return CheckImmRange(6, 0, false, false, false); }
305 bool isu6_1Imm() const { return CheckImmRange(6, 1, false, false, false); }
306 bool isu6_2Imm() const { return CheckImmRange(6, 2, false, false, false); }
307 bool isu6_3Imm() const { return CheckImmRange(6, 3, false, false, false); }
308 bool isu10Imm() const { return CheckImmRange(10, 0, false, false, false); }
309 bool isu9Imm() const { return CheckImmRange(9, 0, false, false, false); }
310 bool isu8Imm() const { return CheckImmRange(8, 0, false, false, false); }
311 bool isu7Imm() const { return CheckImmRange(7, 0, false, false, false); }
312 bool isu6Imm() const { return CheckImmRange(6, 0, false, false, false); }
313 bool isu5Imm() const { return CheckImmRange(5, 0, false, false, false); }
314 bool isu4Imm() const { return CheckImmRange(4, 0, false, false, false); }
315 bool isu3Imm() const { return CheckImmRange(3, 0, false, false, false); }
316 bool isu2Imm() const { return CheckImmRange(2, 0, false, false, false); }
317 bool isu1Imm() const { return CheckImmRange(1, 0, false, false, false); }
318
319 bool ism6Imm() const { return CheckImmRange(6, 0, false, false, false); }
320 bool isn8Imm() const { return CheckImmRange(8, 0, false, false, false); }
321
322 bool iss16Ext() const { return CheckImmRange(16 + 26, 0, true, true, true); }
323 bool iss12Ext() const { return CheckImmRange(12 + 26, 0, true, true, true); }
324 bool iss10Ext() const { return CheckImmRange(10 + 26, 0, true, true, true); }
325 bool iss9Ext() const { return CheckImmRange(9 + 26, 0, true, true, true); }
326 bool iss8Ext() const { return CheckImmRange(8 + 26, 0, true, true, true); }
327 bool iss7Ext() const { return CheckImmRange(7 + 26, 0, true, true, true); }
328 bool iss6Ext() const { return CheckImmRange(6 + 26, 0, true, true, true); }
329 bool iss11_0Ext() const {
330 return CheckImmRange(11 + 26, 0, true, true, true);
331 }
332 bool iss11_1Ext() const {
333 return CheckImmRange(11 + 26, 1, true, true, true);
334 }
335 bool iss11_2Ext() const {
336 return CheckImmRange(11 + 26, 2, true, true, true);
337 }
338 bool iss11_3Ext() const {
339 return CheckImmRange(11 + 26, 3, true, true, true);
340 }
341
342 bool isu6Ext() const { return CheckImmRange(6 + 26, 0, false, true, true); }
343 bool isu7Ext() const { return CheckImmRange(7 + 26, 0, false, true, true); }
344 bool isu8Ext() const { return CheckImmRange(8 + 26, 0, false, true, true); }
345 bool isu9Ext() const { return CheckImmRange(9 + 26, 0, false, true, true); }
346 bool isu10Ext() const { return CheckImmRange(10 + 26, 0, false, true, true); }
347 bool isu6_0Ext() const { return CheckImmRange(6 + 26, 0, false, true, true); }
348 bool isu6_1Ext() const { return CheckImmRange(6 + 26, 1, false, true, true); }
349 bool isu6_2Ext() const { return CheckImmRange(6 + 26, 2, false, true, true); }
350 bool isu6_3Ext() const { return CheckImmRange(6 + 26, 3, false, true, true); }
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000351 bool isu32MustExt() const { return isImm(); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000352
353 void addRegOperands(MCInst &Inst, unsigned N) const {
354 assert(N == 1 && "Invalid number of operands!");
355 Inst.addOperand(MCOperand::createReg(getReg()));
356 }
357
358 void addImmOperands(MCInst &Inst, unsigned N) const {
359 assert(N == 1 && "Invalid number of operands!");
360 Inst.addOperand(MCOperand::createExpr(getImm()));
361 }
362
363 void addSignedImmOperands(MCInst &Inst, unsigned N) const {
364 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000365 HexagonMCExpr *Expr =
366 const_cast<HexagonMCExpr *>(cast<HexagonMCExpr>(getImm()));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000367 int64_t Value;
368 if (!Expr->evaluateAsAbsolute(Value)) {
369 Inst.addOperand(MCOperand::createExpr(Expr));
370 return;
371 }
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000372 int64_t Extended = SignExtend64(Value, 32);
373 if ((Extended < 0) != (Value < 0))
374 Expr->setSignMismatch();
375 Inst.addOperand(MCOperand::createExpr(Expr));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000376 }
377
378 void addf32ExtOperands(MCInst &Inst, unsigned N) const {
379 addImmOperands(Inst, N);
380 }
381
382 void adds32ImmOperands(MCInst &Inst, unsigned N) const {
383 addSignedImmOperands(Inst, N);
384 }
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000385 void adds23_2ImmOperands(MCInst &Inst, unsigned N) const {
386 addSignedImmOperands(Inst, N);
387 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000388 void adds8ImmOperands(MCInst &Inst, unsigned N) const {
389 addSignedImmOperands(Inst, N);
390 }
391 void adds8Imm64Operands(MCInst &Inst, unsigned N) const {
392 addSignedImmOperands(Inst, N);
393 }
394 void adds6ImmOperands(MCInst &Inst, unsigned N) const {
395 addSignedImmOperands(Inst, N);
396 }
397 void adds4ImmOperands(MCInst &Inst, unsigned N) const {
398 addSignedImmOperands(Inst, N);
399 }
400 void adds4_0ImmOperands(MCInst &Inst, unsigned N) const {
401 addSignedImmOperands(Inst, N);
402 }
403 void adds4_1ImmOperands(MCInst &Inst, unsigned N) const {
404 addSignedImmOperands(Inst, N);
405 }
406 void adds4_2ImmOperands(MCInst &Inst, unsigned N) const {
407 addSignedImmOperands(Inst, N);
408 }
409 void adds4_3ImmOperands(MCInst &Inst, unsigned N) const {
410 addSignedImmOperands(Inst, N);
411 }
412 void adds3ImmOperands(MCInst &Inst, unsigned N) const {
413 addSignedImmOperands(Inst, N);
414 }
415
416 void addu64ImmOperands(MCInst &Inst, unsigned N) const {
417 addImmOperands(Inst, N);
418 }
419 void addu32ImmOperands(MCInst &Inst, unsigned N) const {
420 addImmOperands(Inst, N);
421 }
422 void addu26_6ImmOperands(MCInst &Inst, unsigned N) const {
423 addImmOperands(Inst, N);
424 }
425 void addu16ImmOperands(MCInst &Inst, unsigned N) const {
426 addImmOperands(Inst, N);
427 }
428 void addu16_0ImmOperands(MCInst &Inst, unsigned N) const {
429 addImmOperands(Inst, N);
430 }
431 void addu16_1ImmOperands(MCInst &Inst, unsigned N) const {
432 addImmOperands(Inst, N);
433 }
434 void addu16_2ImmOperands(MCInst &Inst, unsigned N) const {
435 addImmOperands(Inst, N);
436 }
437 void addu16_3ImmOperands(MCInst &Inst, unsigned N) const {
438 addImmOperands(Inst, N);
439 }
440 void addu11_3ImmOperands(MCInst &Inst, unsigned N) const {
441 addImmOperands(Inst, N);
442 }
443 void addu10ImmOperands(MCInst &Inst, unsigned N) const {
444 addImmOperands(Inst, N);
445 }
446 void addu9ImmOperands(MCInst &Inst, unsigned N) const {
447 addImmOperands(Inst, N);
448 }
449 void addu8ImmOperands(MCInst &Inst, unsigned N) const {
450 addImmOperands(Inst, N);
451 }
452 void addu7ImmOperands(MCInst &Inst, unsigned N) const {
453 addImmOperands(Inst, N);
454 }
455 void addu6ImmOperands(MCInst &Inst, unsigned N) const {
456 addImmOperands(Inst, N);
457 }
458 void addu6_0ImmOperands(MCInst &Inst, unsigned N) const {
459 addImmOperands(Inst, N);
460 }
461 void addu6_1ImmOperands(MCInst &Inst, unsigned N) const {
462 addImmOperands(Inst, N);
463 }
464 void addu6_2ImmOperands(MCInst &Inst, unsigned N) const {
465 addImmOperands(Inst, N);
466 }
467 void addu6_3ImmOperands(MCInst &Inst, unsigned N) const {
468 addImmOperands(Inst, N);
469 }
470 void addu5ImmOperands(MCInst &Inst, unsigned N) const {
471 addImmOperands(Inst, N);
472 }
473 void addu4ImmOperands(MCInst &Inst, unsigned N) const {
474 addImmOperands(Inst, N);
475 }
476 void addu3ImmOperands(MCInst &Inst, unsigned N) const {
477 addImmOperands(Inst, N);
478 }
479 void addu2ImmOperands(MCInst &Inst, unsigned N) const {
480 addImmOperands(Inst, N);
481 }
482 void addu1ImmOperands(MCInst &Inst, unsigned N) const {
483 addImmOperands(Inst, N);
484 }
485
486 void addm6ImmOperands(MCInst &Inst, unsigned N) const {
487 addImmOperands(Inst, N);
488 }
489 void addn8ImmOperands(MCInst &Inst, unsigned N) const {
490 addImmOperands(Inst, N);
491 }
492
493 void adds16ExtOperands(MCInst &Inst, unsigned N) const {
494 addSignedImmOperands(Inst, N);
495 }
496 void adds12ExtOperands(MCInst &Inst, unsigned N) const {
497 addSignedImmOperands(Inst, N);
498 }
499 void adds10ExtOperands(MCInst &Inst, unsigned N) const {
500 addSignedImmOperands(Inst, N);
501 }
502 void adds9ExtOperands(MCInst &Inst, unsigned N) const {
503 addSignedImmOperands(Inst, N);
504 }
505 void adds8ExtOperands(MCInst &Inst, unsigned N) const {
506 addSignedImmOperands(Inst, N);
507 }
508 void adds6ExtOperands(MCInst &Inst, unsigned N) const {
509 addSignedImmOperands(Inst, N);
510 }
511 void adds11_0ExtOperands(MCInst &Inst, unsigned N) const {
512 addSignedImmOperands(Inst, N);
513 }
514 void adds11_1ExtOperands(MCInst &Inst, unsigned N) const {
515 addSignedImmOperands(Inst, N);
516 }
517 void adds11_2ExtOperands(MCInst &Inst, unsigned N) const {
518 addSignedImmOperands(Inst, N);
519 }
520 void adds11_3ExtOperands(MCInst &Inst, unsigned N) const {
521 addSignedImmOperands(Inst, N);
522 }
523
524 void addu6ExtOperands(MCInst &Inst, unsigned N) const {
525 addImmOperands(Inst, N);
526 }
527 void addu7ExtOperands(MCInst &Inst, unsigned N) const {
528 addImmOperands(Inst, N);
529 }
530 void addu8ExtOperands(MCInst &Inst, unsigned N) const {
531 addImmOperands(Inst, N);
532 }
533 void addu9ExtOperands(MCInst &Inst, unsigned N) const {
534 addImmOperands(Inst, N);
535 }
536 void addu10ExtOperands(MCInst &Inst, unsigned N) const {
537 addImmOperands(Inst, N);
538 }
539 void addu6_0ExtOperands(MCInst &Inst, unsigned N) const {
540 addImmOperands(Inst, N);
541 }
542 void addu6_1ExtOperands(MCInst &Inst, unsigned N) const {
543 addImmOperands(Inst, N);
544 }
545 void addu6_2ExtOperands(MCInst &Inst, unsigned N) const {
546 addImmOperands(Inst, N);
547 }
548 void addu6_3ExtOperands(MCInst &Inst, unsigned N) const {
549 addImmOperands(Inst, N);
550 }
551 void addu32MustExtOperands(MCInst &Inst, unsigned N) const {
552 addImmOperands(Inst, N);
553 }
554
555 void adds4_6ImmOperands(MCInst &Inst, unsigned N) const {
556 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000557 const MCConstantExpr *CE =
558 dyn_cast<MCConstantExpr>(&HexagonMCInstrInfo::getExpr(*getImm()));
Colin LeMahieu4c606e62015-12-04 15:48:45 +0000559 Inst.addOperand(MCOperand::createImm(CE->getValue() * 64));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000560 }
561
562 void adds3_6ImmOperands(MCInst &Inst, unsigned N) const {
563 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000564 const MCConstantExpr *CE =
565 dyn_cast<MCConstantExpr>(&HexagonMCInstrInfo::getExpr(*getImm()));
Colin LeMahieu4c606e62015-12-04 15:48:45 +0000566 Inst.addOperand(MCOperand::createImm(CE->getValue() * 64));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000567 }
568
569 StringRef getToken() const {
570 assert(Kind == Token && "Invalid access!");
571 return StringRef(Tok.Data, Tok.Length);
572 }
573
574 virtual void print(raw_ostream &OS) const;
575
576 static std::unique_ptr<HexagonOperand> CreateToken(StringRef Str, SMLoc S) {
577 HexagonOperand *Op = new HexagonOperand(Token);
578 Op->Tok.Data = Str.data();
579 Op->Tok.Length = Str.size();
580 Op->StartLoc = S;
581 Op->EndLoc = S;
582 return std::unique_ptr<HexagonOperand>(Op);
583 }
584
585 static std::unique_ptr<HexagonOperand> CreateReg(unsigned RegNum, SMLoc S,
586 SMLoc E) {
587 HexagonOperand *Op = new HexagonOperand(Register);
588 Op->Reg.RegNum = RegNum;
589 Op->StartLoc = S;
590 Op->EndLoc = E;
591 return std::unique_ptr<HexagonOperand>(Op);
592 }
593
594 static std::unique_ptr<HexagonOperand> CreateImm(const MCExpr *Val, SMLoc S,
595 SMLoc E) {
596 HexagonOperand *Op = new HexagonOperand(Immediate);
597 Op->Imm.Val = Val;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000598 Op->StartLoc = S;
599 Op->EndLoc = E;
600 return std::unique_ptr<HexagonOperand>(Op);
601 }
602};
603
604} // end anonymous namespace.
605
606void HexagonOperand::print(raw_ostream &OS) const {
607 switch (Kind) {
608 case Immediate:
609 getImm()->print(OS, nullptr);
610 break;
611 case Register:
612 OS << "<register R";
613 OS << getReg() << ">";
614 break;
615 case Token:
616 OS << "'" << getToken() << "'";
617 break;
618 }
619}
620
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000621bool HexagonAsmParser::finishBundle(SMLoc IDLoc, MCStreamer &Out) {
622 DEBUG(dbgs() << "Bundle:");
623 DEBUG(MCB.dump_pretty(dbgs()));
624 DEBUG(dbgs() << "--\n");
625
626 // Check the bundle for errors.
627 const MCRegisterInfo *RI = getContext().getRegisterInfo();
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000628 HexagonMCChecker Check(MCII, getSTI(), MCB, MCB, *RI);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000629
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000630 bool CheckOk = HexagonMCInstrInfo::canonicalizePacket(MCII, getSTI(),
631 getContext(), MCB,
632 &Check);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000633
634 while (Check.getNextErrInfo() == true) {
635 unsigned Reg = Check.getErrRegister();
636 Twine R(RI->getName(Reg));
637
638 uint64_t Err = Check.getError();
639 if (Err != HexagonMCErrInfo::CHECK_SUCCESS) {
640 if (HexagonMCErrInfo::CHECK_ERROR_BRANCHES & Err)
641 Error(IDLoc,
642 "unconditional branch cannot precede another branch in packet");
643
644 if (HexagonMCErrInfo::CHECK_ERROR_NEWP & Err ||
645 HexagonMCErrInfo::CHECK_ERROR_NEWV & Err)
646 Error(IDLoc, "register `" + R +
647 "' used with `.new' "
648 "but not validly modified in the same packet");
649
650 if (HexagonMCErrInfo::CHECK_ERROR_REGISTERS & Err)
651 Error(IDLoc, "register `" + R + "' modified more than once");
652
653 if (HexagonMCErrInfo::CHECK_ERROR_READONLY & Err)
654 Error(IDLoc, "cannot write to read-only register `" + R + "'");
655
656 if (HexagonMCErrInfo::CHECK_ERROR_LOOP & Err)
657 Error(IDLoc, "loop-setup and some branch instructions "
658 "cannot be in the same packet");
659
660 if (HexagonMCErrInfo::CHECK_ERROR_ENDLOOP & Err) {
661 Twine N(HexagonMCInstrInfo::isInnerLoop(MCB) ? '0' : '1');
662 Error(IDLoc, "packet marked with `:endloop" + N + "' " +
663 "cannot contain instructions that modify register " +
664 "`" + R + "'");
665 }
666
667 if (HexagonMCErrInfo::CHECK_ERROR_SOLO & Err)
668 Error(IDLoc,
669 "instruction cannot appear in packet with other instructions");
670
671 if (HexagonMCErrInfo::CHECK_ERROR_NOSLOTS & Err)
672 Error(IDLoc, "too many slots used in packet");
673
674 if (Err & HexagonMCErrInfo::CHECK_ERROR_SHUFFLE) {
675 uint64_t Erm = Check.getShuffleError();
676
677 if (HexagonShuffler::SHUFFLE_ERROR_INVALID == Erm)
678 Error(IDLoc, "invalid instruction packet");
679 else if (HexagonShuffler::SHUFFLE_ERROR_STORES == Erm)
680 Error(IDLoc, "invalid instruction packet: too many stores");
681 else if (HexagonShuffler::SHUFFLE_ERROR_LOADS == Erm)
682 Error(IDLoc, "invalid instruction packet: too many loads");
683 else if (HexagonShuffler::SHUFFLE_ERROR_BRANCHES == Erm)
684 Error(IDLoc, "too many branches in packet");
685 else if (HexagonShuffler::SHUFFLE_ERROR_NOSLOTS == Erm)
686 Error(IDLoc, "invalid instruction packet: out of slots");
687 else if (HexagonShuffler::SHUFFLE_ERROR_SLOTS == Erm)
688 Error(IDLoc, "invalid instruction packet: slot error");
689 else if (HexagonShuffler::SHUFFLE_ERROR_ERRATA2 == Erm)
690 Error(IDLoc, "v60 packet violation");
691 else if (HexagonShuffler::SHUFFLE_ERROR_STORE_LOAD_CONFLICT == Erm)
692 Error(IDLoc, "slot 0 instruction does not allow slot 1 store");
693 else
694 Error(IDLoc, "unknown error in instruction packet");
695 }
696 }
697
698 unsigned Warn = Check.getWarning();
699 if (Warn != HexagonMCErrInfo::CHECK_SUCCESS) {
700 if (HexagonMCErrInfo::CHECK_WARN_CURRENT & Warn)
701 Warning(IDLoc, "register `" + R + "' used with `.cur' "
702 "but not used in the same packet");
703 else if (HexagonMCErrInfo::CHECK_WARN_TEMPORARY & Warn)
704 Warning(IDLoc, "register `" + R + "' used with `.tmp' "
705 "but not used in the same packet");
706 }
707 }
708
709 if (CheckOk) {
710 MCB.setLoc(IDLoc);
711 if (HexagonMCInstrInfo::bundleSize(MCB) == 0) {
712 assert(!HexagonMCInstrInfo::isInnerLoop(MCB));
713 assert(!HexagonMCInstrInfo::isOuterLoop(MCB));
714 // Empty packets are valid yet aren't emitted
715 return false;
716 }
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000717 Out.EmitInstruction(MCB, getSTI());
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000718 } else {
719 // If compounding and duplexing didn't reduce the size below
720 // 4 or less we have a packet that is too big.
721 if (HexagonMCInstrInfo::bundleSize(MCB) > HEXAGON_PACKET_SIZE) {
722 Error(IDLoc, "invalid instruction packet: out of slots");
723 return true; // Error
724 }
725 }
726
727 return false; // No error
728}
729
730bool HexagonAsmParser::matchBundleOptions() {
731 MCAsmParser &Parser = getParser();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000732 while (true) {
733 if (!Parser.getTok().is(AsmToken::Colon))
734 return false;
Nirav Davefd910412016-06-17 16:06:17 +0000735 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000736 StringRef Option = Parser.getTok().getString();
737 if (Option.compare_lower("endloop0") == 0)
738 HexagonMCInstrInfo::setInnerLoop(MCB);
739 else if (Option.compare_lower("endloop1") == 0)
740 HexagonMCInstrInfo::setOuterLoop(MCB);
741 else if (Option.compare_lower("mem_noshuf") == 0)
742 HexagonMCInstrInfo::setMemReorderDisabled(MCB);
743 else if (Option.compare_lower("mem_shuf") == 0)
744 HexagonMCInstrInfo::setMemStoreReorderEnabled(MCB);
745 else
746 return true;
Nirav Davefd910412016-06-17 16:06:17 +0000747 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000748 }
749}
750
751// For instruction aliases, immediates are generated rather than
752// MCConstantExpr. Convert them for uniform MCExpr.
753// Also check for signed/unsigned mismatches and warn
754void HexagonAsmParser::canonicalizeImmediates(MCInst &MCI) {
755 MCInst NewInst;
756 NewInst.setOpcode(MCI.getOpcode());
757 for (MCOperand &I : MCI)
758 if (I.isImm()) {
759 int64_t Value (I.getImm());
Colin LeMahieuc7b21242016-02-15 18:47:55 +0000760 NewInst.addOperand(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000761 MCConstantExpr::create(Value, getContext()), getContext())));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000762 }
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000763 else {
764 if (I.isExpr() && cast<HexagonMCExpr>(I.getExpr())->signMismatch() &&
765 WarnSignedMismatch)
766 Warning (MCI.getLoc(), "Signed/Unsigned mismatch");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000767 NewInst.addOperand(I);
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000768 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000769 MCI = NewInst;
770}
771
772bool HexagonAsmParser::matchOneInstruction(MCInst &MCI, SMLoc IDLoc,
773 OperandVector &InstOperands,
774 uint64_t &ErrorInfo,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000775 bool MatchingInlineAsm) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000776 // Perform matching with tablegen asmmatcher generated function
777 int result =
778 MatchInstructionImpl(InstOperands, MCI, ErrorInfo, MatchingInlineAsm);
779 if (result == Match_Success) {
780 MCI.setLoc(IDLoc);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000781 canonicalizeImmediates(MCI);
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000782 result = processInstruction(MCI, InstOperands, IDLoc);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000783
784 DEBUG(dbgs() << "Insn:");
785 DEBUG(MCI.dump_pretty(dbgs()));
786 DEBUG(dbgs() << "\n\n");
787
788 MCI.setLoc(IDLoc);
789 }
790
791 // Create instruction operand for bundle instruction
792 // Break this into a separate function Code here is less readable
793 // Think about how to get an instruction error to report correctly.
794 // SMLoc will return the "{"
795 switch (result) {
796 default:
797 break;
798 case Match_Success:
799 return false;
800 case Match_MissingFeature:
801 return Error(IDLoc, "invalid instruction");
802 case Match_MnemonicFail:
803 return Error(IDLoc, "unrecognized instruction");
804 case Match_InvalidOperand:
805 SMLoc ErrorLoc = IDLoc;
806 if (ErrorInfo != ~0U) {
807 if (ErrorInfo >= InstOperands.size())
808 return Error(IDLoc, "too few operands for instruction");
809
810 ErrorLoc = (static_cast<HexagonOperand *>(InstOperands[ErrorInfo].get()))
811 ->getStartLoc();
812 if (ErrorLoc == SMLoc())
813 ErrorLoc = IDLoc;
814 }
815 return Error(ErrorLoc, "invalid operand for instruction");
816 }
817 llvm_unreachable("Implement any new match types added!");
818}
819
820bool HexagonAsmParser::mustExtend(OperandVector &Operands) {
821 unsigned Count = 0;
822 for (std::unique_ptr<MCParsedAsmOperand> &i : Operands)
823 if (i->isImm())
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000824 if (HexagonMCInstrInfo::mustExtend(
825 *static_cast<HexagonOperand *>(i.get())->Imm.Val))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000826 ++Count;
827 // Multiple extenders should have been filtered by iss9Ext et. al.
828 assert(Count < 2 && "Multiple extenders");
829 return Count == 1;
830}
831
832bool HexagonAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
833 OperandVector &Operands,
834 MCStreamer &Out,
835 uint64_t &ErrorInfo,
836 bool MatchingInlineAsm) {
837 if (!InBrackets) {
838 MCB.clear();
839 MCB.addOperand(MCOperand::createImm(0));
840 }
841 HexagonOperand &FirstOperand = static_cast<HexagonOperand &>(*Operands[0]);
842 if (FirstOperand.isToken() && FirstOperand.getToken() == "{") {
843 assert(Operands.size() == 1 && "Brackets should be by themselves");
844 if (InBrackets) {
845 getParser().Error(IDLoc, "Already in a packet");
846 return true;
847 }
848 InBrackets = true;
849 return false;
850 }
851 if (FirstOperand.isToken() && FirstOperand.getToken() == "}") {
852 assert(Operands.size() == 1 && "Brackets should be by themselves");
853 if (!InBrackets) {
854 getParser().Error(IDLoc, "Not in a packet");
855 return true;
856 }
857 InBrackets = false;
858 if (matchBundleOptions())
859 return true;
860 return finishBundle(IDLoc, Out);
861 }
862 MCInst *SubInst = new (getParser().getContext()) MCInst;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000863 if (matchOneInstruction(*SubInst, IDLoc, Operands, ErrorInfo,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000864 MatchingInlineAsm))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000865 return true;
866 HexagonMCInstrInfo::extendIfNeeded(
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000867 getParser().getContext(), MCII, MCB, *SubInst);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000868 MCB.addOperand(MCOperand::createInst(SubInst));
869 if (!InBrackets)
870 return finishBundle(IDLoc, Out);
871 return false;
872}
873
874/// ParseDirective parses the Hexagon specific directives
875bool HexagonAsmParser::ParseDirective(AsmToken DirectiveID) {
876 StringRef IDVal = DirectiveID.getIdentifier();
877 if ((IDVal.lower() == ".word") || (IDVal.lower() == ".4byte"))
878 return ParseDirectiveValue(4, DirectiveID.getLoc());
879 if (IDVal.lower() == ".short" || IDVal.lower() == ".hword" ||
880 IDVal.lower() == ".half")
881 return ParseDirectiveValue(2, DirectiveID.getLoc());
882 if (IDVal.lower() == ".falign")
883 return ParseDirectiveFalign(256, DirectiveID.getLoc());
884 if ((IDVal.lower() == ".lcomm") || (IDVal.lower() == ".lcommon"))
885 return ParseDirectiveComm(true, DirectiveID.getLoc());
886 if ((IDVal.lower() == ".comm") || (IDVal.lower() == ".common"))
887 return ParseDirectiveComm(false, DirectiveID.getLoc());
888 if (IDVal.lower() == ".subsection")
889 return ParseDirectiveSubsection(DirectiveID.getLoc());
890
891 return true;
892}
893bool HexagonAsmParser::ParseDirectiveSubsection(SMLoc L) {
894 const MCExpr *Subsection = 0;
895 int64_t Res;
896
897 assert((getLexer().isNot(AsmToken::EndOfStatement)) &&
898 "Invalid subsection directive");
899 getParser().parseExpression(Subsection);
900
901 if (!Subsection->evaluateAsAbsolute(Res))
902 return Error(L, "Cannot evaluate subsection number");
903
904 if (getLexer().isNot(AsmToken::EndOfStatement))
905 return TokError("unexpected token in directive");
906
907 // 0-8192 is the hard-coded range in MCObjectStreamper.cpp, this keeps the
908 // negative subsections together and in the same order but at the opposite
909 // end of the section. Only legacy hexagon-gcc created assembly code
910 // used negative subsections.
911 if ((Res < 0) && (Res > -8193))
Colin LeMahieuc7b21242016-02-15 18:47:55 +0000912 Subsection = HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000913 MCConstantExpr::create(8192 + Res, getContext()), getContext());
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000914
915 getStreamer().SubSection(Subsection);
916 return false;
917}
918
919/// ::= .falign [expression]
920bool HexagonAsmParser::ParseDirectiveFalign(unsigned Size, SMLoc L) {
921
922 int64_t MaxBytesToFill = 15;
923
924 // if there is an arguement
925 if (getLexer().isNot(AsmToken::EndOfStatement)) {
926 const MCExpr *Value;
927 SMLoc ExprLoc = L;
928
929 // Make sure we have a number (false is returned if expression is a number)
930 if (getParser().parseExpression(Value) == false) {
931 // Make sure this is a number that is in range
932 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
933 uint64_t IntValue = MCE->getValue();
934 if (!isUIntN(Size, IntValue) && !isIntN(Size, IntValue))
935 return Error(ExprLoc, "literal value out of range (256) for falign");
936 MaxBytesToFill = IntValue;
937 Lex();
938 } else {
939 return Error(ExprLoc, "not a valid expression for falign directive");
940 }
941 }
942
943 getTargetStreamer().emitFAlign(16, MaxBytesToFill);
944 Lex();
945
946 return false;
947}
948
949/// ::= .word [ expression (, expression)* ]
950bool HexagonAsmParser::ParseDirectiveValue(unsigned Size, SMLoc L) {
951 if (getLexer().isNot(AsmToken::EndOfStatement)) {
952
953 for (;;) {
954 const MCExpr *Value;
955 SMLoc ExprLoc = L;
956 if (getParser().parseExpression(Value))
957 return true;
958
959 // Special case constant expressions to match code generator.
960 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
961 assert(Size <= 8 && "Invalid size");
962 uint64_t IntValue = MCE->getValue();
963 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
964 return Error(ExprLoc, "literal value out of range for directive");
965 getStreamer().EmitIntValue(IntValue, Size);
966 } else
967 getStreamer().EmitValue(Value, Size);
968
969 if (getLexer().is(AsmToken::EndOfStatement))
970 break;
971
972 // FIXME: Improve diagnostic.
973 if (getLexer().isNot(AsmToken::Comma))
974 return TokError("unexpected token in directive");
975 Lex();
976 }
977 }
978
979 Lex();
980 return false;
981}
982
983// This is largely a copy of AsmParser's ParseDirectiveComm extended to
984// accept a 3rd argument, AccessAlignment which indicates the smallest
985// memory access made to the symbol, expressed in bytes. If no
986// AccessAlignment is specified it defaults to the Alignment Value.
987// Hexagon's .lcomm:
988// .lcomm Symbol, Length, Alignment, AccessAlignment
989bool HexagonAsmParser::ParseDirectiveComm(bool IsLocal, SMLoc Loc) {
990 // FIXME: need better way to detect if AsmStreamer (upstream removed
991 // getKind())
992 if (getStreamer().hasRawTextSupport())
993 return true; // Only object file output requires special treatment.
994
995 StringRef Name;
996 if (getParser().parseIdentifier(Name))
997 return TokError("expected identifier in directive");
998 // Handle the identifier as the key symbol.
999 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
1000
1001 if (getLexer().isNot(AsmToken::Comma))
1002 return TokError("unexpected token in directive");
1003 Lex();
1004
1005 int64_t Size;
1006 SMLoc SizeLoc = getLexer().getLoc();
1007 if (getParser().parseAbsoluteExpression(Size))
1008 return true;
1009
1010 int64_t ByteAlignment = 1;
1011 SMLoc ByteAlignmentLoc;
1012 if (getLexer().is(AsmToken::Comma)) {
1013 Lex();
1014 ByteAlignmentLoc = getLexer().getLoc();
1015 if (getParser().parseAbsoluteExpression(ByteAlignment))
1016 return true;
1017 if (!isPowerOf2_64(ByteAlignment))
1018 return Error(ByteAlignmentLoc, "alignment must be a power of 2");
1019 }
1020
1021 int64_t AccessAlignment = 0;
1022 if (getLexer().is(AsmToken::Comma)) {
1023 // The optional access argument specifies the size of the smallest memory
1024 // access to be made to the symbol, expressed in bytes.
1025 SMLoc AccessAlignmentLoc;
1026 Lex();
1027 AccessAlignmentLoc = getLexer().getLoc();
1028 if (getParser().parseAbsoluteExpression(AccessAlignment))
1029 return true;
1030
1031 if (!isPowerOf2_64(AccessAlignment))
1032 return Error(AccessAlignmentLoc, "access alignment must be a power of 2");
1033 }
1034
1035 if (getLexer().isNot(AsmToken::EndOfStatement))
1036 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1037
1038 Lex();
1039
1040 // NOTE: a size of zero for a .comm should create a undefined symbol
1041 // but a size of .lcomm creates a bss symbol of size zero.
1042 if (Size < 0)
1043 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1044 "be less than zero");
1045
1046 // NOTE: The alignment in the directive is a power of 2 value, the assembler
1047 // may internally end up wanting an alignment in bytes.
1048 // FIXME: Diagnose overflow.
1049 if (ByteAlignment < 0)
1050 return Error(ByteAlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1051 "alignment, can't be less than zero");
1052
1053 if (!Sym->isUndefined())
1054 return Error(Loc, "invalid symbol redefinition");
1055
1056 HexagonMCELFStreamer &HexagonELFStreamer =
1057 static_cast<HexagonMCELFStreamer &>(getStreamer());
1058 if (IsLocal) {
1059 HexagonELFStreamer.HexagonMCEmitLocalCommonSymbol(Sym, Size, ByteAlignment,
1060 AccessAlignment);
1061 return false;
1062 }
1063
1064 HexagonELFStreamer.HexagonMCEmitCommonSymbol(Sym, Size, ByteAlignment,
1065 AccessAlignment);
1066 return false;
1067}
1068
1069// validate register against architecture
1070bool HexagonAsmParser::RegisterMatchesArch(unsigned MatchNum) const {
1071 return true;
1072}
1073
1074// extern "C" void LLVMInitializeHexagonAsmLexer();
1075
1076/// Force static initialization.
1077extern "C" void LLVMInitializeHexagonAsmParser() {
1078 RegisterMCAsmParser<HexagonAsmParser> X(TheHexagonTarget);
1079}
1080
1081#define GET_MATCHER_IMPLEMENTATION
1082#define GET_REGISTER_MATCHER
1083#include "HexagonGenAsmMatcher.inc"
1084
1085namespace {
1086bool previousEqual(OperandVector &Operands, size_t Index, StringRef String) {
1087 if (Index >= Operands.size())
1088 return false;
1089 MCParsedAsmOperand &Operand = *Operands[Operands.size() - Index - 1];
1090 if (!Operand.isToken())
1091 return false;
1092 return static_cast<HexagonOperand &>(Operand).getToken().equals_lower(String);
1093}
1094bool previousIsLoop(OperandVector &Operands, size_t Index) {
1095 return previousEqual(Operands, Index, "loop0") ||
1096 previousEqual(Operands, Index, "loop1") ||
1097 previousEqual(Operands, Index, "sp1loop0") ||
1098 previousEqual(Operands, Index, "sp2loop0") ||
1099 previousEqual(Operands, Index, "sp3loop0");
1100}
1101}
1102
1103bool HexagonAsmParser::splitIdentifier(OperandVector &Operands) {
1104 AsmToken const &Token = getParser().getTok();
1105 StringRef String = Token.getString();
1106 SMLoc Loc = Token.getLoc();
Nirav Davefd910412016-06-17 16:06:17 +00001107 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001108 do {
1109 std::pair<StringRef, StringRef> HeadTail = String.split('.');
1110 if (!HeadTail.first.empty())
1111 Operands.push_back(HexagonOperand::CreateToken(HeadTail.first, Loc));
1112 if (!HeadTail.second.empty())
1113 Operands.push_back(HexagonOperand::CreateToken(
1114 String.substr(HeadTail.first.size(), 1), Loc));
1115 String = HeadTail.second;
1116 } while (!String.empty());
1117 return false;
1118}
1119
1120bool HexagonAsmParser::parseOperand(OperandVector &Operands) {
1121 unsigned Register;
1122 SMLoc Begin;
1123 SMLoc End;
1124 MCAsmLexer &Lexer = getLexer();
1125 if (!ParseRegister(Register, Begin, End)) {
1126 if (!ErrorMissingParenthesis)
1127 switch (Register) {
1128 default:
1129 break;
1130 case Hexagon::P0:
1131 case Hexagon::P1:
1132 case Hexagon::P2:
1133 case Hexagon::P3:
1134 if (previousEqual(Operands, 0, "if")) {
1135 if (WarnMissingParenthesis)
1136 Warning (Begin, "Missing parenthesis around predicate register");
1137 static char const *LParen = "(";
1138 static char const *RParen = ")";
1139 Operands.push_back(HexagonOperand::CreateToken(LParen, Begin));
1140 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End));
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +00001141 const AsmToken &MaybeDotNew = Lexer.getTok();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001142 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) &&
1143 MaybeDotNew.getString().equals_lower(".new"))
1144 splitIdentifier(Operands);
1145 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin));
1146 return false;
1147 }
1148 if (previousEqual(Operands, 0, "!") &&
1149 previousEqual(Operands, 1, "if")) {
1150 if (WarnMissingParenthesis)
1151 Warning (Begin, "Missing parenthesis around predicate register");
1152 static char const *LParen = "(";
1153 static char const *RParen = ")";
1154 Operands.insert(Operands.end () - 1,
1155 HexagonOperand::CreateToken(LParen, Begin));
1156 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End));
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +00001157 const AsmToken &MaybeDotNew = Lexer.getTok();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001158 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) &&
1159 MaybeDotNew.getString().equals_lower(".new"))
1160 splitIdentifier(Operands);
1161 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin));
1162 return false;
1163 }
1164 break;
1165 }
1166 Operands.push_back(HexagonOperand::CreateReg(
1167 Register, Begin, End));
1168 return false;
1169 }
1170 return splitIdentifier(Operands);
1171}
1172
1173bool HexagonAsmParser::isLabel(AsmToken &Token) {
1174 MCAsmLexer &Lexer = getLexer();
1175 AsmToken const &Second = Lexer.getTok();
1176 AsmToken Third = Lexer.peekTok();
1177 StringRef String = Token.getString();
1178 if (Token.is(AsmToken::TokenKind::LCurly) ||
1179 Token.is(AsmToken::TokenKind::RCurly))
1180 return false;
1181 if (!Token.is(AsmToken::TokenKind::Identifier))
1182 return true;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001183 if (!matchRegister(String.lower()))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001184 return true;
Colin LeMahieu9d851f02015-11-09 21:06:28 +00001185 (void)Second;
1186 assert(Second.is(AsmToken::Colon));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001187 StringRef Raw (String.data(), Third.getString().data() - String.data() +
1188 Third.getString().size());
1189 std::string Collapsed = Raw;
1190 Collapsed.erase(std::remove_if(Collapsed.begin(), Collapsed.end(), isspace),
1191 Collapsed.end());
1192 StringRef Whole = Collapsed;
1193 std::pair<StringRef, StringRef> DotSplit = Whole.split('.');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001194 if (!matchRegister(DotSplit.first.lower()))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001195 return true;
1196 return false;
1197}
1198
1199bool HexagonAsmParser::handleNoncontigiousRegister(bool Contigious, SMLoc &Loc) {
1200 if (!Contigious && ErrorNoncontigiousRegister) {
1201 Error(Loc, "Register name is not contigious");
1202 return true;
1203 }
1204 if (!Contigious && WarnNoncontigiousRegister)
1205 Warning(Loc, "Register name is not contigious");
1206 return false;
1207}
1208
1209bool HexagonAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) {
1210 MCAsmLexer &Lexer = getLexer();
1211 StartLoc = getLexer().getLoc();
1212 SmallVector<AsmToken, 5> Lookahead;
1213 StringRef RawString(Lexer.getTok().getString().data(), 0);
1214 bool Again = Lexer.is(AsmToken::Identifier);
1215 bool NeededWorkaround = false;
1216 while (Again) {
1217 AsmToken const &Token = Lexer.getTok();
1218 RawString = StringRef(RawString.data(),
1219 Token.getString().data() - RawString.data () +
1220 Token.getString().size());
1221 Lookahead.push_back(Token);
1222 Lexer.Lex();
1223 bool Contigious = Lexer.getTok().getString().data() ==
1224 Lookahead.back().getString().data() +
1225 Lookahead.back().getString().size();
1226 bool Type = Lexer.is(AsmToken::Identifier) || Lexer.is(AsmToken::Dot) ||
1227 Lexer.is(AsmToken::Integer) || Lexer.is(AsmToken::Real) ||
1228 Lexer.is(AsmToken::Colon);
1229 bool Workaround = Lexer.is(AsmToken::Colon) ||
1230 Lookahead.back().is(AsmToken::Colon);
1231 Again = (Contigious && Type) || (Workaround && Type);
1232 NeededWorkaround = NeededWorkaround || (Again && !(Contigious && Type));
1233 }
1234 std::string Collapsed = RawString;
1235 Collapsed.erase(std::remove_if(Collapsed.begin(), Collapsed.end(), isspace),
1236 Collapsed.end());
1237 StringRef FullString = Collapsed;
1238 std::pair<StringRef, StringRef> DotSplit = FullString.split('.');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001239 unsigned DotReg = matchRegister(DotSplit.first.lower());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001240 if (DotReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1241 if (DotSplit.second.empty()) {
1242 RegNo = DotReg;
1243 EndLoc = Lexer.getLoc();
1244 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1245 return true;
1246 return false;
1247 } else {
1248 RegNo = DotReg;
1249 size_t First = RawString.find('.');
1250 StringRef DotString (RawString.data() + First, RawString.size() - First);
1251 Lexer.UnLex(AsmToken(AsmToken::Identifier, DotString));
1252 EndLoc = Lexer.getLoc();
1253 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1254 return true;
1255 return false;
1256 }
1257 }
1258 std::pair<StringRef, StringRef> ColonSplit = StringRef(FullString).split(':');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001259 unsigned ColonReg = matchRegister(ColonSplit.first.lower());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001260 if (ColonReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1261 Lexer.UnLex(Lookahead.back());
1262 Lookahead.pop_back();
1263 Lexer.UnLex(Lookahead.back());
1264 Lookahead.pop_back();
1265 RegNo = ColonReg;
1266 EndLoc = Lexer.getLoc();
1267 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1268 return true;
1269 return false;
1270 }
1271 while (!Lookahead.empty()) {
1272 Lexer.UnLex(Lookahead.back());
1273 Lookahead.pop_back();
1274 }
1275 return true;
1276}
1277
1278bool HexagonAsmParser::implicitExpressionLocation(OperandVector &Operands) {
1279 if (previousEqual(Operands, 0, "call"))
1280 return true;
1281 if (previousEqual(Operands, 0, "jump"))
1282 if (!getLexer().getTok().is(AsmToken::Colon))
1283 return true;
1284 if (previousEqual(Operands, 0, "(") && previousIsLoop(Operands, 1))
1285 return true;
1286 if (previousEqual(Operands, 1, ":") && previousEqual(Operands, 2, "jump") &&
1287 (previousEqual(Operands, 0, "nt") || previousEqual(Operands, 0, "t")))
1288 return true;
1289 return false;
1290}
1291
1292bool HexagonAsmParser::parseExpression(MCExpr const *& Expr) {
1293 llvm::SmallVector<AsmToken, 4> Tokens;
1294 MCAsmLexer &Lexer = getLexer();
1295 bool Done = false;
1296 static char const * Comma = ",";
1297 do {
1298 Tokens.emplace_back (Lexer.getTok());
Nirav Davefd910412016-06-17 16:06:17 +00001299 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001300 switch (Tokens.back().getKind())
1301 {
1302 case AsmToken::TokenKind::Hash:
1303 if (Tokens.size () > 1)
1304 if ((Tokens.end () - 2)->getKind() == AsmToken::TokenKind::Plus) {
1305 Tokens.insert(Tokens.end() - 2,
1306 AsmToken(AsmToken::TokenKind::Comma, Comma));
1307 Done = true;
1308 }
1309 break;
1310 case AsmToken::TokenKind::RCurly:
1311 case AsmToken::TokenKind::EndOfStatement:
1312 case AsmToken::TokenKind::Eof:
1313 Done = true;
1314 break;
1315 default:
1316 break;
1317 }
1318 } while (!Done);
1319 while (!Tokens.empty()) {
1320 Lexer.UnLex(Tokens.back());
1321 Tokens.pop_back();
1322 }
1323 return getParser().parseExpression(Expr);
1324}
1325
1326bool HexagonAsmParser::parseExpressionOrOperand(OperandVector &Operands) {
1327 if (implicitExpressionLocation(Operands)) {
1328 MCAsmParser &Parser = getParser();
1329 SMLoc Loc = Parser.getLexer().getLoc();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001330 MCExpr const *Expr = nullptr;
1331 bool Error = parseExpression(Expr);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001332 Expr = HexagonMCExpr::create(Expr, getContext());
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001333 if (!Error)
1334 Operands.push_back(HexagonOperand::CreateImm(Expr, Loc, Loc));
1335 return Error;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001336 }
1337 return parseOperand(Operands);
1338}
1339
1340/// Parse an instruction.
1341bool HexagonAsmParser::parseInstruction(OperandVector &Operands) {
1342 MCAsmParser &Parser = getParser();
1343 MCAsmLexer &Lexer = getLexer();
1344 while (true) {
1345 AsmToken const &Token = Parser.getTok();
1346 switch (Token.getKind()) {
1347 case AsmToken::EndOfStatement: {
Nirav Davefd910412016-06-17 16:06:17 +00001348 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001349 return false;
1350 }
1351 case AsmToken::LCurly: {
1352 if (!Operands.empty())
1353 return true;
1354 Operands.push_back(
1355 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001356 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001357 return false;
1358 }
1359 case AsmToken::RCurly: {
1360 if (Operands.empty()) {
1361 Operands.push_back(
1362 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001363 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001364 }
1365 return false;
1366 }
1367 case AsmToken::Comma: {
Nirav Davefd910412016-06-17 16:06:17 +00001368 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001369 continue;
1370 }
1371 case AsmToken::EqualEqual:
1372 case AsmToken::ExclaimEqual:
1373 case AsmToken::GreaterEqual:
1374 case AsmToken::GreaterGreater:
1375 case AsmToken::LessEqual:
1376 case AsmToken::LessLess: {
1377 Operands.push_back(HexagonOperand::CreateToken(
1378 Token.getString().substr(0, 1), Token.getLoc()));
1379 Operands.push_back(HexagonOperand::CreateToken(
1380 Token.getString().substr(1, 1), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001381 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001382 continue;
1383 }
1384 case AsmToken::Hash: {
1385 bool MustNotExtend = false;
1386 bool ImplicitExpression = implicitExpressionLocation(Operands);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001387 SMLoc ExprLoc = Lexer.getLoc();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001388 if (!ImplicitExpression)
1389 Operands.push_back(
1390 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001391 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001392 bool MustExtend = false;
1393 bool HiOnly = false;
1394 bool LoOnly = false;
1395 if (Lexer.is(AsmToken::Hash)) {
Nirav Davefd910412016-06-17 16:06:17 +00001396 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001397 MustExtend = true;
1398 } else if (ImplicitExpression)
1399 MustNotExtend = true;
1400 AsmToken const &Token = Parser.getTok();
1401 if (Token.is(AsmToken::Identifier)) {
1402 StringRef String = Token.getString();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001403 if (String.lower() == "hi") {
1404 HiOnly = true;
1405 } else if (String.lower() == "lo") {
1406 LoOnly = true;
1407 }
1408 if (HiOnly || LoOnly) {
1409 AsmToken LParen = Lexer.peekTok();
1410 if (!LParen.is(AsmToken::LParen)) {
1411 HiOnly = false;
1412 LoOnly = false;
1413 } else {
Nirav Davefd910412016-06-17 16:06:17 +00001414 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001415 }
1416 }
1417 }
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001418 MCExpr const *Expr = nullptr;
1419 if (parseExpression(Expr))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001420 return true;
1421 int64_t Value;
1422 MCContext &Context = Parser.getContext();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001423 assert(Expr != nullptr);
1424 if (Expr->evaluateAsAbsolute(Value)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001425 if (HiOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001426 Expr = MCBinaryExpr::createLShr(
1427 Expr, MCConstantExpr::create(16, Context), Context);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001428 if (HiOnly || LoOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001429 Expr = MCBinaryExpr::createAnd(Expr,
1430 MCConstantExpr::create(0xffff, Context),
1431 Context);
Colin LeMahieu5cb6eea2016-03-01 21:37:41 +00001432 } else {
1433 MCValue Value;
1434 if (Expr->evaluateAsRelocatable(Value, nullptr, nullptr)) {
1435 if (!Value.isAbsolute()) {
1436 switch(Value.getAccessVariant()) {
1437 case MCSymbolRefExpr::VariantKind::VK_TPREL:
1438 case MCSymbolRefExpr::VariantKind::VK_DTPREL:
1439 // Don't lazy extend these expression variants
1440 MustNotExtend = !MustExtend;
1441 break;
1442 default:
1443 break;
1444 }
1445 }
1446 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001447 }
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001448 Expr = HexagonMCExpr::create(Expr, Context);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001449 HexagonMCInstrInfo::setMustNotExtend(*Expr, MustNotExtend);
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001450 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001451 std::unique_ptr<HexagonOperand> Operand =
1452 HexagonOperand::CreateImm(Expr, ExprLoc, ExprLoc);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001453 Operands.push_back(std::move(Operand));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001454 continue;
1455 }
1456 default:
1457 break;
1458 }
1459 if (parseExpressionOrOperand(Operands))
1460 return true;
1461 }
1462}
1463
1464bool HexagonAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1465 StringRef Name,
1466 AsmToken ID,
1467 OperandVector &Operands) {
1468 getLexer().UnLex(ID);
1469 return parseInstruction(Operands);
1470}
1471
1472namespace {
1473MCInst makeCombineInst(int opCode, MCOperand &Rdd,
1474 MCOperand &MO1, MCOperand &MO2) {
1475 MCInst TmpInst;
1476 TmpInst.setOpcode(opCode);
1477 TmpInst.addOperand(Rdd);
1478 TmpInst.addOperand(MO1);
1479 TmpInst.addOperand(MO2);
1480
1481 return TmpInst;
1482}
1483}
1484
1485// Define this matcher function after the auto-generated include so we
1486// have the match class enum definitions.
1487unsigned HexagonAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
1488 unsigned Kind) {
1489 HexagonOperand *Op = static_cast<HexagonOperand *>(&AsmOp);
1490
1491 switch (Kind) {
1492 case MCK_0: {
1493 int64_t Value;
1494 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 0
1495 ? Match_Success
1496 : Match_InvalidOperand;
1497 }
1498 case MCK_1: {
1499 int64_t Value;
1500 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 1
1501 ? Match_Success
1502 : Match_InvalidOperand;
1503 }
1504 case MCK__MINUS_1: {
1505 int64_t Value;
1506 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == -1
1507 ? Match_Success
1508 : Match_InvalidOperand;
1509 }
1510 }
1511 if (Op->Kind == HexagonOperand::Token && Kind != InvalidMatchClass) {
1512 StringRef myStringRef = StringRef(Op->Tok.Data, Op->Tok.Length);
1513 if (matchTokenString(myStringRef.lower()) == (MatchClassKind)Kind)
1514 return Match_Success;
1515 if (matchTokenString(myStringRef.upper()) == (MatchClassKind)Kind)
1516 return Match_Success;
1517 }
1518
1519 DEBUG(dbgs() << "Unmatched Operand:");
1520 DEBUG(Op->dump());
1521 DEBUG(dbgs() << "\n");
1522
1523 return Match_InvalidOperand;
1524}
1525
1526void HexagonAsmParser::OutOfRange(SMLoc IDLoc, long long Val, long long Max) {
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001527 std::string errStr;
1528 raw_string_ostream ES(errStr);
Alexey Samsonov44ff2042015-12-02 22:59:22 +00001529 ES << "value " << Val << "(" << format_hex(Val, 0) << ") out of range: ";
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001530 if (Max >= 0)
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001531 ES << "0-" << Max;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001532 else
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001533 ES << Max << "-" << (-Max - 1);
1534 Error(IDLoc, ES.str().c_str());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001535}
1536
1537int HexagonAsmParser::processInstruction(MCInst &Inst,
1538 OperandVector const &Operands,
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001539 SMLoc IDLoc) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001540 MCContext &Context = getParser().getContext();
1541 const MCRegisterInfo *RI = getContext().getRegisterInfo();
1542 std::string r = "r";
1543 std::string v = "v";
1544 std::string Colon = ":";
1545
1546 bool is32bit = false; // used to distinguish between CONST32 and CONST64
1547 switch (Inst.getOpcode()) {
1548 default:
1549 break;
1550
Colin LeMahieuecef1d92016-02-16 20:38:17 +00001551 case Hexagon::A2_iconst: {
1552 Inst.setOpcode(Hexagon::A2_addi);
1553 MCOperand Reg = Inst.getOperand(0);
1554 MCOperand S16 = Inst.getOperand(1);
1555 HexagonMCInstrInfo::setMustNotExtend(*S16.getExpr());
1556 HexagonMCInstrInfo::setS23_2_reloc(*S16.getExpr());
1557 Inst.clear();
1558 Inst.addOperand(Reg);
1559 Inst.addOperand(MCOperand::createReg(Hexagon::R0));
1560 Inst.addOperand(S16);
1561 break;
1562 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001563 case Hexagon::M4_mpyrr_addr:
1564 case Hexagon::S4_addi_asl_ri:
1565 case Hexagon::S4_addi_lsr_ri:
1566 case Hexagon::S4_andi_asl_ri:
1567 case Hexagon::S4_andi_lsr_ri:
1568 case Hexagon::S4_ori_asl_ri:
1569 case Hexagon::S4_ori_lsr_ri:
1570 case Hexagon::S4_or_andix:
1571 case Hexagon::S4_subi_asl_ri:
1572 case Hexagon::S4_subi_lsr_ri: {
1573 MCOperand &Ry = Inst.getOperand(0);
1574 MCOperand &src = Inst.getOperand(2);
1575 if (RI->getEncodingValue(Ry.getReg()) != RI->getEncodingValue(src.getReg()))
1576 return Match_InvalidOperand;
1577 break;
1578 }
1579
1580 case Hexagon::C2_cmpgei: {
1581 MCOperand &MO = Inst.getOperand(2);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001582 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001583 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001584 Inst.setOpcode(Hexagon::C2_cmpgti);
1585 break;
1586 }
1587
1588 case Hexagon::C2_cmpgeui: {
1589 MCOperand &MO = Inst.getOperand(2);
1590 int64_t Value;
1591 bool Success = MO.getExpr()->evaluateAsAbsolute(Value);
Colin LeMahieu9d851f02015-11-09 21:06:28 +00001592 (void)Success;
1593 assert(Success && "Assured by matcher");
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001594 if (Value == 0) {
1595 MCInst TmpInst;
1596 MCOperand &Pd = Inst.getOperand(0);
1597 MCOperand &Rt = Inst.getOperand(1);
1598 TmpInst.setOpcode(Hexagon::C2_cmpeq);
1599 TmpInst.addOperand(Pd);
1600 TmpInst.addOperand(Rt);
1601 TmpInst.addOperand(Rt);
1602 Inst = TmpInst;
1603 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001604 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001605 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001606 Inst.setOpcode(Hexagon::C2_cmpgtui);
1607 }
1608 break;
1609 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001610
1611 // Translate a "$Rdd = $Rss" to "$Rdd = combine($Rs, $Rt)"
1612 case Hexagon::A2_tfrp: {
1613 MCOperand &MO = Inst.getOperand(1);
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(Hexagon::A2_combinew);
1623 break;
1624 }
1625
1626 case Hexagon::A2_tfrpt:
1627 case Hexagon::A2_tfrpf: {
1628 MCOperand &MO = Inst.getOperand(2);
1629 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001630 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001631 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001632 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001633 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001634 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001635 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001636 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001637 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrpt)
1638 ? Hexagon::C2_ccombinewt
1639 : Hexagon::C2_ccombinewf);
1640 break;
1641 }
1642 case Hexagon::A2_tfrptnew:
1643 case Hexagon::A2_tfrpfnew: {
1644 MCOperand &MO = Inst.getOperand(2);
1645 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001646 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001647 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001648 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001649 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001650 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001651 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001652 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001653 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrptnew)
1654 ? Hexagon::C2_ccombinewnewt
1655 : Hexagon::C2_ccombinewnewf);
1656 break;
1657 }
1658
Krzysztof Parzyszek0e7d2d32016-04-28 16:43:16 +00001659 // Translate a "$Vdd = $Vss" to "$Vdd = vcombine($Vs, $Vt)"
1660 case Hexagon::HEXAGON_V6_vassignpair: {
1661 MCOperand &MO = Inst.getOperand(1);
1662 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
1663 std::string R1 = v + llvm::utostr(RegPairNum + 1);
1664 MO.setReg(MatchRegisterName(R1));
1665 // Add a new operand for the second register in the pair.
1666 std::string R2 = v + llvm::utostr(RegPairNum);
1667 Inst.addOperand(MCOperand::createReg(MatchRegisterName(R2)));
1668 Inst.setOpcode(Hexagon::V6_vcombine);
1669 break;
1670 }
1671
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001672 // Translate a "$Rx = CONST32(#imm)" to "$Rx = memw(gp+#LABEL) "
1673 case Hexagon::CONST32:
1674 case Hexagon::CONST32_Float_Real:
1675 case Hexagon::CONST32_Int_Real:
1676 case Hexagon::FCONST32_nsdata:
1677 is32bit = true;
1678 // Translate a "$Rx:y = CONST64(#imm)" to "$Rx:y = memd(gp+#LABEL) "
1679 case Hexagon::CONST64_Float_Real:
1680 case Hexagon::CONST64_Int_Real:
1681
1682 // FIXME: need better way to detect AsmStreamer (upstream removed getKind())
1683 if (!Parser.getStreamer().hasRawTextSupport()) {
1684 MCELFStreamer *MES = static_cast<MCELFStreamer *>(&Parser.getStreamer());
1685 MCOperand &MO_1 = Inst.getOperand(1);
1686 MCOperand &MO_0 = Inst.getOperand(0);
1687
1688 // push section onto section stack
1689 MES->PushSection();
1690
1691 std::string myCharStr;
1692 MCSectionELF *mySection;
1693
1694 // check if this as an immediate or a symbol
1695 int64_t Value;
1696 bool Absolute = MO_1.getExpr()->evaluateAsAbsolute(Value);
1697 if (Absolute) {
1698 // Create a new section - one for each constant
1699 // Some or all of the zeros are replaced with the given immediate.
1700 if (is32bit) {
1701 std::string myImmStr = utohexstr(static_cast<uint32_t>(Value));
1702 myCharStr = StringRef(".gnu.linkonce.l4.CONST_00000000")
1703 .drop_back(myImmStr.size())
1704 .str() +
1705 myImmStr;
1706 } else {
1707 std::string myImmStr = utohexstr(Value);
1708 myCharStr = StringRef(".gnu.linkonce.l8.CONST_0000000000000000")
1709 .drop_back(myImmStr.size())
1710 .str() +
1711 myImmStr;
1712 }
1713
1714 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1715 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1716 } else if (MO_1.isExpr()) {
1717 // .lita - for expressions
1718 myCharStr = ".lita";
1719 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1720 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1721 } else
1722 llvm_unreachable("unexpected type of machine operand!");
1723
1724 MES->SwitchSection(mySection);
1725 unsigned byteSize = is32bit ? 4 : 8;
1726 getStreamer().EmitCodeAlignment(byteSize, byteSize);
1727
1728 MCSymbol *Sym;
1729
1730 // for symbols, get rid of prepended ".gnu.linkonce.lx."
1731
1732 // emit symbol if needed
1733 if (Absolute) {
1734 Sym = getContext().getOrCreateSymbol(StringRef(myCharStr.c_str() + 16));
1735 if (Sym->isUndefined()) {
1736 getStreamer().EmitLabel(Sym);
1737 getStreamer().EmitSymbolAttribute(Sym, MCSA_Global);
1738 getStreamer().EmitIntValue(Value, byteSize);
1739 }
1740 } else if (MO_1.isExpr()) {
1741 const char *StringStart = 0;
1742 const char *StringEnd = 0;
1743 if (*Operands[4]->getStartLoc().getPointer() == '#') {
1744 StringStart = Operands[5]->getStartLoc().getPointer();
1745 StringEnd = Operands[6]->getStartLoc().getPointer();
1746 } else { // no pound
1747 StringStart = Operands[4]->getStartLoc().getPointer();
1748 StringEnd = Operands[5]->getStartLoc().getPointer();
1749 }
1750
1751 unsigned size = StringEnd - StringStart;
1752 std::string DotConst = ".CONST_";
1753 Sym = getContext().getOrCreateSymbol(DotConst +
1754 StringRef(StringStart, size));
1755
1756 if (Sym->isUndefined()) {
1757 // case where symbol is not yet defined: emit symbol
1758 getStreamer().EmitLabel(Sym);
1759 getStreamer().EmitSymbolAttribute(Sym, MCSA_Local);
1760 getStreamer().EmitValue(MO_1.getExpr(), 4);
1761 }
1762 } else
1763 llvm_unreachable("unexpected type of machine operand!");
1764
1765 MES->PopSection();
1766
1767 if (Sym) {
1768 MCInst TmpInst;
1769 if (is32bit) // 32 bit
1770 TmpInst.setOpcode(Hexagon::L2_loadrigp);
1771 else // 64 bit
1772 TmpInst.setOpcode(Hexagon::L2_loadrdgp);
1773
1774 TmpInst.addOperand(MO_0);
1775 TmpInst.addOperand(
1776 MCOperand::createExpr(MCSymbolRefExpr::create(Sym, getContext())));
1777 Inst = TmpInst;
1778 }
1779 }
1780 break;
1781
1782 // Translate a "$Rdd = #-imm" to "$Rdd = combine(#[-1,0], #-imm)"
1783 case Hexagon::A2_tfrpi: {
1784 MCOperand &Rdd = Inst.getOperand(0);
1785 MCOperand &MO = Inst.getOperand(1);
1786 int64_t Value;
1787 int sVal = (MO.getExpr()->evaluateAsAbsolute(Value) && Value < 0) ? -1 : 0;
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001788 MCOperand imm(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001789 HexagonMCExpr::create(MCConstantExpr::create(sVal, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001790 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, imm, MO);
1791 break;
1792 }
1793
1794 // Translate a "$Rdd = [#]#imm" to "$Rdd = combine(#, [#]#imm)"
1795 case Hexagon::TFRI64_V4: {
1796 MCOperand &Rdd = Inst.getOperand(0);
1797 MCOperand &MO = Inst.getOperand(1);
1798 int64_t Value;
1799 if (MO.getExpr()->evaluateAsAbsolute(Value)) {
David Majnemere61e4bf2016-06-21 05:10:24 +00001800 int s8 = Hi_32(Value);
1801 if (!isInt<8>(s8))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001802 OutOfRange(IDLoc, s8, -128);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001803 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001804 MCConstantExpr::create(s8, Context), Context))); // upper 32
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001805 auto Expr = HexagonMCExpr::create(
David Majnemere61e4bf2016-06-21 05:10:24 +00001806 MCConstantExpr::create(Lo_32(Value), Context), Context);
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001807 HexagonMCInstrInfo::setMustExtend(*Expr, HexagonMCInstrInfo::mustExtend(*MO.getExpr()));
1808 MCOperand imm2(MCOperand::createExpr(Expr)); // lower 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001809 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, imm2);
1810 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001811 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001812 MCConstantExpr::create(0, Context), Context))); // upper 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001813 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, MO);
1814 }
1815 break;
1816 }
1817
1818 // Handle $Rdd = combine(##imm, #imm)"
1819 case Hexagon::TFRI64_V2_ext: {
1820 MCOperand &Rdd = Inst.getOperand(0);
1821 MCOperand &MO1 = Inst.getOperand(1);
1822 MCOperand &MO2 = Inst.getOperand(2);
1823 int64_t Value;
1824 if (MO2.getExpr()->evaluateAsAbsolute(Value)) {
1825 int s8 = Value;
1826 if (s8 < -128 || s8 > 127)
1827 OutOfRange(IDLoc, s8, -128);
1828 }
1829 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, MO1, MO2);
1830 break;
1831 }
1832
1833 // Handle $Rdd = combine(#imm, ##imm)"
1834 case Hexagon::A4_combineii: {
1835 MCOperand &Rdd = Inst.getOperand(0);
1836 MCOperand &MO1 = Inst.getOperand(1);
1837 int64_t Value;
1838 if (MO1.getExpr()->evaluateAsAbsolute(Value)) {
1839 int s8 = Value;
1840 if (s8 < -128 || s8 > 127)
1841 OutOfRange(IDLoc, s8, -128);
1842 }
1843 MCOperand &MO2 = Inst.getOperand(2);
1844 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, MO1, MO2);
1845 break;
1846 }
1847
1848 case Hexagon::S2_tableidxb_goodsyntax: {
1849 Inst.setOpcode(Hexagon::S2_tableidxb);
1850 break;
1851 }
1852
1853 case Hexagon::S2_tableidxh_goodsyntax: {
1854 MCInst TmpInst;
1855 MCOperand &Rx = Inst.getOperand(0);
1856 MCOperand &_dst_ = Inst.getOperand(1);
1857 MCOperand &Rs = Inst.getOperand(2);
1858 MCOperand &Imm4 = Inst.getOperand(3);
1859 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001860 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001861 Imm6.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001862 TmpInst.setOpcode(Hexagon::S2_tableidxh);
1863 TmpInst.addOperand(Rx);
1864 TmpInst.addOperand(_dst_);
1865 TmpInst.addOperand(Rs);
1866 TmpInst.addOperand(Imm4);
1867 TmpInst.addOperand(Imm6);
1868 Inst = TmpInst;
1869 break;
1870 }
1871
1872 case Hexagon::S2_tableidxw_goodsyntax: {
1873 MCInst TmpInst;
1874 MCOperand &Rx = Inst.getOperand(0);
1875 MCOperand &_dst_ = Inst.getOperand(1);
1876 MCOperand &Rs = Inst.getOperand(2);
1877 MCOperand &Imm4 = Inst.getOperand(3);
1878 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001879 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001880 Imm6.getExpr(), MCConstantExpr::create(2, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001881 TmpInst.setOpcode(Hexagon::S2_tableidxw);
1882 TmpInst.addOperand(Rx);
1883 TmpInst.addOperand(_dst_);
1884 TmpInst.addOperand(Rs);
1885 TmpInst.addOperand(Imm4);
1886 TmpInst.addOperand(Imm6);
1887 Inst = TmpInst;
1888 break;
1889 }
1890
1891 case Hexagon::S2_tableidxd_goodsyntax: {
1892 MCInst TmpInst;
1893 MCOperand &Rx = Inst.getOperand(0);
1894 MCOperand &_dst_ = Inst.getOperand(1);
1895 MCOperand &Rs = Inst.getOperand(2);
1896 MCOperand &Imm4 = Inst.getOperand(3);
1897 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001898 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001899 Imm6.getExpr(), MCConstantExpr::create(3, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001900 TmpInst.setOpcode(Hexagon::S2_tableidxd);
1901 TmpInst.addOperand(Rx);
1902 TmpInst.addOperand(_dst_);
1903 TmpInst.addOperand(Rs);
1904 TmpInst.addOperand(Imm4);
1905 TmpInst.addOperand(Imm6);
1906 Inst = TmpInst;
1907 break;
1908 }
1909
1910 case Hexagon::M2_mpyui: {
1911 Inst.setOpcode(Hexagon::M2_mpyi);
1912 break;
1913 }
1914 case Hexagon::M2_mpysmi: {
1915 MCInst TmpInst;
1916 MCOperand &Rd = Inst.getOperand(0);
1917 MCOperand &Rs = Inst.getOperand(1);
1918 MCOperand &Imm = Inst.getOperand(2);
1919 int64_t Value;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001920 MCExpr const &Expr = *Imm.getExpr();
1921 bool Absolute = Expr.evaluateAsAbsolute(Value);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001922 assert(Absolute);
1923 (void)Absolute;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001924 if (!HexagonMCInstrInfo::mustExtend(Expr)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001925 if (Value < 0 && Value > -256) {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001926 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001927 MCConstantExpr::create(Value * -1, Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001928 TmpInst.setOpcode(Hexagon::M2_mpysin);
1929 } else if (Value < 256 && Value >= 0)
1930 TmpInst.setOpcode(Hexagon::M2_mpysip);
1931 else
1932 return Match_InvalidOperand;
1933 } else {
1934 if (Value >= 0)
1935 TmpInst.setOpcode(Hexagon::M2_mpysip);
1936 else
1937 return Match_InvalidOperand;
1938 }
1939 TmpInst.addOperand(Rd);
1940 TmpInst.addOperand(Rs);
1941 TmpInst.addOperand(Imm);
1942 Inst = TmpInst;
1943 break;
1944 }
1945
1946 case Hexagon::S2_asr_i_r_rnd_goodsyntax: {
1947 MCOperand &Imm = Inst.getOperand(2);
1948 MCInst TmpInst;
1949 int64_t Value;
1950 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1951 assert(Absolute);
1952 (void)Absolute;
1953 if (Value == 0) { // convert to $Rd = $Rs
1954 TmpInst.setOpcode(Hexagon::A2_tfr);
1955 MCOperand &Rd = Inst.getOperand(0);
1956 MCOperand &Rs = Inst.getOperand(1);
1957 TmpInst.addOperand(Rd);
1958 TmpInst.addOperand(Rs);
1959 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001960 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001961 MCBinaryExpr::createSub(Imm.getExpr(),
1962 MCConstantExpr::create(1, Context), Context),
1963 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001964 TmpInst.setOpcode(Hexagon::S2_asr_i_r_rnd);
1965 MCOperand &Rd = Inst.getOperand(0);
1966 MCOperand &Rs = Inst.getOperand(1);
1967 TmpInst.addOperand(Rd);
1968 TmpInst.addOperand(Rs);
1969 TmpInst.addOperand(Imm);
1970 }
1971 Inst = TmpInst;
1972 break;
1973 }
1974
1975 case Hexagon::S2_asr_i_p_rnd_goodsyntax: {
1976 MCOperand &Rdd = Inst.getOperand(0);
1977 MCOperand &Rss = Inst.getOperand(1);
1978 MCOperand &Imm = Inst.getOperand(2);
1979 int64_t Value;
1980 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1981 assert(Absolute);
1982 (void)Absolute;
1983 if (Value == 0) { // convert to $Rdd = combine ($Rs[0], $Rs[1])
1984 MCInst TmpInst;
1985 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001986 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001987 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001988 Rss.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001989 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001990 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001991 StringRef Reg2(R2);
1992 TmpInst.setOpcode(Hexagon::A2_combinew);
1993 TmpInst.addOperand(Rdd);
1994 TmpInst.addOperand(Rss);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001995 TmpInst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001996 Inst = TmpInst;
1997 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001998 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001999 MCBinaryExpr::createSub(Imm.getExpr(),
2000 MCConstantExpr::create(1, Context), Context),
2001 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002002 Inst.setOpcode(Hexagon::S2_asr_i_p_rnd);
2003 }
2004 break;
2005 }
2006
2007 case Hexagon::A4_boundscheck: {
2008 MCOperand &Rs = Inst.getOperand(1);
2009 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
2010 if (RegNum & 1) { // Odd mapped to raw:hi, regpair is rodd:odd-1, like r3:2
2011 Inst.setOpcode(Hexagon::A4_boundscheck_hi);
2012 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002013 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002014 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002015 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002016 } else { // raw:lo
2017 Inst.setOpcode(Hexagon::A4_boundscheck_lo);
2018 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002019 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002020 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002021 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002022 }
2023 break;
2024 }
2025
2026 case Hexagon::A2_addsp: {
2027 MCOperand &Rs = Inst.getOperand(1);
2028 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
2029 if (RegNum & 1) { // Odd mapped to raw:hi
2030 Inst.setOpcode(Hexagon::A2_addsph);
2031 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002032 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002033 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002034 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002035 } else { // Even mapped raw:lo
2036 Inst.setOpcode(Hexagon::A2_addspl);
2037 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002038 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002039 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002040 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002041 }
2042 break;
2043 }
2044
2045 case Hexagon::M2_vrcmpys_s1: {
2046 MCOperand &Rt = Inst.getOperand(2);
2047 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2048 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2049 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_h);
2050 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002051 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002052 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002053 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002054 } else { // Even mapped sat:raw:lo
2055 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_l);
2056 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002057 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002058 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002059 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002060 }
2061 break;
2062 }
2063
2064 case Hexagon::M2_vrcmpys_acc_s1: {
2065 MCInst TmpInst;
2066 MCOperand &Rxx = Inst.getOperand(0);
2067 MCOperand &Rss = Inst.getOperand(2);
2068 MCOperand &Rt = Inst.getOperand(3);
2069 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2070 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2071 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_h);
2072 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002073 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002074 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002075 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002076 } else { // Even mapped sat:raw:lo
2077 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_l);
2078 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002079 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002080 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002081 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002082 }
2083 // Registers are in different positions
2084 TmpInst.addOperand(Rxx);
2085 TmpInst.addOperand(Rxx);
2086 TmpInst.addOperand(Rss);
2087 TmpInst.addOperand(Rt);
2088 Inst = TmpInst;
2089 break;
2090 }
2091
2092 case Hexagon::M2_vrcmpys_s1rp: {
2093 MCOperand &Rt = Inst.getOperand(2);
2094 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2095 if (RegNum & 1) { // Odd mapped to rnd:sat:raw:hi
2096 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_h);
2097 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002098 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002099 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002100 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002101 } else { // Even mapped rnd:sat:raw:lo
2102 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_l);
2103 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002104 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002105 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002106 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002107 }
2108 break;
2109 }
2110
2111 case Hexagon::S5_asrhub_rnd_sat_goodsyntax: {
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 Inst.setOpcode(Hexagon::S2_vsathub);
2119 else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002120 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002121 MCBinaryExpr::createSub(Imm.getExpr(),
2122 MCConstantExpr::create(1, Context), Context),
2123 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002124 Inst.setOpcode(Hexagon::S5_asrhub_rnd_sat);
2125 }
2126 break;
2127 }
2128
2129 case Hexagon::S5_vasrhrnd_goodsyntax: {
2130 MCOperand &Rdd = Inst.getOperand(0);
2131 MCOperand &Rss = Inst.getOperand(1);
2132 MCOperand &Imm = Inst.getOperand(2);
2133 int64_t Value;
2134 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
2135 assert(Absolute);
2136 (void)Absolute;
2137 if (Value == 0) {
2138 MCInst TmpInst;
2139 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00002140 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002141 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002142 Rss.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002143 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00002144 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002145 StringRef Reg2(R2);
2146 TmpInst.setOpcode(Hexagon::A2_combinew);
2147 TmpInst.addOperand(Rdd);
2148 TmpInst.addOperand(Rss);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002149 TmpInst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002150 Inst = TmpInst;
2151 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002152 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002153 MCBinaryExpr::createSub(Imm.getExpr(),
2154 MCConstantExpr::create(1, Context), Context),
2155 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002156 Inst.setOpcode(Hexagon::S5_vasrhrnd);
2157 }
2158 break;
2159 }
2160
2161 case Hexagon::A2_not: {
2162 MCInst TmpInst;
2163 MCOperand &Rd = Inst.getOperand(0);
2164 MCOperand &Rs = Inst.getOperand(1);
2165 TmpInst.setOpcode(Hexagon::A2_subri);
2166 TmpInst.addOperand(Rd);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002167 TmpInst.addOperand(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002168 HexagonMCExpr::create(MCConstantExpr::create(-1, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002169 TmpInst.addOperand(Rs);
2170 Inst = TmpInst;
2171 break;
2172 }
2173 } // switch
2174
2175 return Match_Success;
2176}
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002177
2178
2179unsigned HexagonAsmParser::matchRegister(StringRef Name) {
2180 if (unsigned Reg = MatchRegisterName(Name))
2181 return Reg;
2182 return MatchRegisterAltName(Name);
2183}