blob: ba223a73afe0cd9666bf356339611a91b62299ee [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;
1177 Collapsed.erase(std::remove_if(Collapsed.begin(), Collapsed.end(), isspace),
1178 Collapsed.end());
1179 StringRef Whole = Collapsed;
1180 std::pair<StringRef, StringRef> DotSplit = Whole.split('.');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001181 if (!matchRegister(DotSplit.first.lower()))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001182 return true;
1183 return false;
1184}
1185
1186bool HexagonAsmParser::handleNoncontigiousRegister(bool Contigious, SMLoc &Loc) {
1187 if (!Contigious && ErrorNoncontigiousRegister) {
1188 Error(Loc, "Register name is not contigious");
1189 return true;
1190 }
1191 if (!Contigious && WarnNoncontigiousRegister)
1192 Warning(Loc, "Register name is not contigious");
1193 return false;
1194}
1195
1196bool HexagonAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) {
1197 MCAsmLexer &Lexer = getLexer();
1198 StartLoc = getLexer().getLoc();
1199 SmallVector<AsmToken, 5> Lookahead;
1200 StringRef RawString(Lexer.getTok().getString().data(), 0);
1201 bool Again = Lexer.is(AsmToken::Identifier);
1202 bool NeededWorkaround = false;
1203 while (Again) {
1204 AsmToken const &Token = Lexer.getTok();
1205 RawString = StringRef(RawString.data(),
1206 Token.getString().data() - RawString.data () +
1207 Token.getString().size());
1208 Lookahead.push_back(Token);
1209 Lexer.Lex();
1210 bool Contigious = Lexer.getTok().getString().data() ==
1211 Lookahead.back().getString().data() +
1212 Lookahead.back().getString().size();
1213 bool Type = Lexer.is(AsmToken::Identifier) || Lexer.is(AsmToken::Dot) ||
1214 Lexer.is(AsmToken::Integer) || Lexer.is(AsmToken::Real) ||
1215 Lexer.is(AsmToken::Colon);
1216 bool Workaround = Lexer.is(AsmToken::Colon) ||
1217 Lookahead.back().is(AsmToken::Colon);
1218 Again = (Contigious && Type) || (Workaround && Type);
1219 NeededWorkaround = NeededWorkaround || (Again && !(Contigious && Type));
1220 }
1221 std::string Collapsed = RawString;
1222 Collapsed.erase(std::remove_if(Collapsed.begin(), Collapsed.end(), isspace),
1223 Collapsed.end());
1224 StringRef FullString = Collapsed;
1225 std::pair<StringRef, StringRef> DotSplit = FullString.split('.');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001226 unsigned DotReg = matchRegister(DotSplit.first.lower());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001227 if (DotReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1228 if (DotSplit.second.empty()) {
1229 RegNo = DotReg;
1230 EndLoc = Lexer.getLoc();
1231 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1232 return true;
1233 return false;
1234 } else {
1235 RegNo = DotReg;
1236 size_t First = RawString.find('.');
1237 StringRef DotString (RawString.data() + First, RawString.size() - First);
1238 Lexer.UnLex(AsmToken(AsmToken::Identifier, DotString));
1239 EndLoc = Lexer.getLoc();
1240 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1241 return true;
1242 return false;
1243 }
1244 }
1245 std::pair<StringRef, StringRef> ColonSplit = StringRef(FullString).split(':');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001246 unsigned ColonReg = matchRegister(ColonSplit.first.lower());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001247 if (ColonReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1248 Lexer.UnLex(Lookahead.back());
1249 Lookahead.pop_back();
1250 Lexer.UnLex(Lookahead.back());
1251 Lookahead.pop_back();
1252 RegNo = ColonReg;
1253 EndLoc = Lexer.getLoc();
1254 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1255 return true;
1256 return false;
1257 }
1258 while (!Lookahead.empty()) {
1259 Lexer.UnLex(Lookahead.back());
1260 Lookahead.pop_back();
1261 }
1262 return true;
1263}
1264
1265bool HexagonAsmParser::implicitExpressionLocation(OperandVector &Operands) {
1266 if (previousEqual(Operands, 0, "call"))
1267 return true;
1268 if (previousEqual(Operands, 0, "jump"))
1269 if (!getLexer().getTok().is(AsmToken::Colon))
1270 return true;
1271 if (previousEqual(Operands, 0, "(") && previousIsLoop(Operands, 1))
1272 return true;
1273 if (previousEqual(Operands, 1, ":") && previousEqual(Operands, 2, "jump") &&
1274 (previousEqual(Operands, 0, "nt") || previousEqual(Operands, 0, "t")))
1275 return true;
1276 return false;
1277}
1278
1279bool HexagonAsmParser::parseExpression(MCExpr const *& Expr) {
1280 llvm::SmallVector<AsmToken, 4> Tokens;
1281 MCAsmLexer &Lexer = getLexer();
1282 bool Done = false;
1283 static char const * Comma = ",";
1284 do {
1285 Tokens.emplace_back (Lexer.getTok());
Nirav Davefd910412016-06-17 16:06:17 +00001286 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001287 switch (Tokens.back().getKind())
1288 {
1289 case AsmToken::TokenKind::Hash:
1290 if (Tokens.size () > 1)
1291 if ((Tokens.end () - 2)->getKind() == AsmToken::TokenKind::Plus) {
1292 Tokens.insert(Tokens.end() - 2,
1293 AsmToken(AsmToken::TokenKind::Comma, Comma));
1294 Done = true;
1295 }
1296 break;
1297 case AsmToken::TokenKind::RCurly:
1298 case AsmToken::TokenKind::EndOfStatement:
1299 case AsmToken::TokenKind::Eof:
1300 Done = true;
1301 break;
1302 default:
1303 break;
1304 }
1305 } while (!Done);
1306 while (!Tokens.empty()) {
1307 Lexer.UnLex(Tokens.back());
1308 Tokens.pop_back();
1309 }
1310 return getParser().parseExpression(Expr);
1311}
1312
1313bool HexagonAsmParser::parseExpressionOrOperand(OperandVector &Operands) {
1314 if (implicitExpressionLocation(Operands)) {
1315 MCAsmParser &Parser = getParser();
1316 SMLoc Loc = Parser.getLexer().getLoc();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001317 MCExpr const *Expr = nullptr;
1318 bool Error = parseExpression(Expr);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001319 Expr = HexagonMCExpr::create(Expr, getContext());
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001320 if (!Error)
1321 Operands.push_back(HexagonOperand::CreateImm(Expr, Loc, Loc));
1322 return Error;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001323 }
1324 return parseOperand(Operands);
1325}
1326
1327/// Parse an instruction.
1328bool HexagonAsmParser::parseInstruction(OperandVector &Operands) {
1329 MCAsmParser &Parser = getParser();
1330 MCAsmLexer &Lexer = getLexer();
1331 while (true) {
1332 AsmToken const &Token = Parser.getTok();
1333 switch (Token.getKind()) {
1334 case AsmToken::EndOfStatement: {
Nirav Davefd910412016-06-17 16:06:17 +00001335 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001336 return false;
1337 }
1338 case AsmToken::LCurly: {
1339 if (!Operands.empty())
1340 return true;
1341 Operands.push_back(
1342 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001343 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001344 return false;
1345 }
1346 case AsmToken::RCurly: {
1347 if (Operands.empty()) {
1348 Operands.push_back(
1349 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001350 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001351 }
1352 return false;
1353 }
1354 case AsmToken::Comma: {
Nirav Davefd910412016-06-17 16:06:17 +00001355 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001356 continue;
1357 }
1358 case AsmToken::EqualEqual:
1359 case AsmToken::ExclaimEqual:
1360 case AsmToken::GreaterEqual:
1361 case AsmToken::GreaterGreater:
1362 case AsmToken::LessEqual:
1363 case AsmToken::LessLess: {
1364 Operands.push_back(HexagonOperand::CreateToken(
1365 Token.getString().substr(0, 1), Token.getLoc()));
1366 Operands.push_back(HexagonOperand::CreateToken(
1367 Token.getString().substr(1, 1), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001368 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001369 continue;
1370 }
1371 case AsmToken::Hash: {
1372 bool MustNotExtend = false;
1373 bool ImplicitExpression = implicitExpressionLocation(Operands);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001374 SMLoc ExprLoc = Lexer.getLoc();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001375 if (!ImplicitExpression)
1376 Operands.push_back(
1377 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001378 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001379 bool MustExtend = false;
1380 bool HiOnly = false;
1381 bool LoOnly = false;
1382 if (Lexer.is(AsmToken::Hash)) {
Nirav Davefd910412016-06-17 16:06:17 +00001383 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001384 MustExtend = true;
1385 } else if (ImplicitExpression)
1386 MustNotExtend = true;
1387 AsmToken const &Token = Parser.getTok();
1388 if (Token.is(AsmToken::Identifier)) {
1389 StringRef String = Token.getString();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001390 if (String.lower() == "hi") {
1391 HiOnly = true;
1392 } else if (String.lower() == "lo") {
1393 LoOnly = true;
1394 }
1395 if (HiOnly || LoOnly) {
1396 AsmToken LParen = Lexer.peekTok();
1397 if (!LParen.is(AsmToken::LParen)) {
1398 HiOnly = false;
1399 LoOnly = false;
1400 } else {
Nirav Davefd910412016-06-17 16:06:17 +00001401 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001402 }
1403 }
1404 }
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001405 MCExpr const *Expr = nullptr;
1406 if (parseExpression(Expr))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001407 return true;
1408 int64_t Value;
1409 MCContext &Context = Parser.getContext();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001410 assert(Expr != nullptr);
1411 if (Expr->evaluateAsAbsolute(Value)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001412 if (HiOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001413 Expr = MCBinaryExpr::createLShr(
1414 Expr, MCConstantExpr::create(16, Context), Context);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001415 if (HiOnly || LoOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001416 Expr = MCBinaryExpr::createAnd(Expr,
1417 MCConstantExpr::create(0xffff, Context),
1418 Context);
Colin LeMahieu5cb6eea2016-03-01 21:37:41 +00001419 } else {
1420 MCValue Value;
1421 if (Expr->evaluateAsRelocatable(Value, nullptr, nullptr)) {
1422 if (!Value.isAbsolute()) {
1423 switch(Value.getAccessVariant()) {
1424 case MCSymbolRefExpr::VariantKind::VK_TPREL:
1425 case MCSymbolRefExpr::VariantKind::VK_DTPREL:
1426 // Don't lazy extend these expression variants
1427 MustNotExtend = !MustExtend;
1428 break;
1429 default:
1430 break;
1431 }
1432 }
1433 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001434 }
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001435 Expr = HexagonMCExpr::create(Expr, Context);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001436 HexagonMCInstrInfo::setMustNotExtend(*Expr, MustNotExtend);
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001437 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001438 std::unique_ptr<HexagonOperand> Operand =
1439 HexagonOperand::CreateImm(Expr, ExprLoc, ExprLoc);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001440 Operands.push_back(std::move(Operand));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001441 continue;
1442 }
1443 default:
1444 break;
1445 }
1446 if (parseExpressionOrOperand(Operands))
1447 return true;
1448 }
1449}
1450
1451bool HexagonAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1452 StringRef Name,
1453 AsmToken ID,
1454 OperandVector &Operands) {
1455 getLexer().UnLex(ID);
1456 return parseInstruction(Operands);
1457}
1458
1459namespace {
1460MCInst makeCombineInst(int opCode, MCOperand &Rdd,
1461 MCOperand &MO1, MCOperand &MO2) {
1462 MCInst TmpInst;
1463 TmpInst.setOpcode(opCode);
1464 TmpInst.addOperand(Rdd);
1465 TmpInst.addOperand(MO1);
1466 TmpInst.addOperand(MO2);
1467
1468 return TmpInst;
1469}
1470}
1471
1472// Define this matcher function after the auto-generated include so we
1473// have the match class enum definitions.
1474unsigned HexagonAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
1475 unsigned Kind) {
1476 HexagonOperand *Op = static_cast<HexagonOperand *>(&AsmOp);
1477
1478 switch (Kind) {
1479 case MCK_0: {
1480 int64_t Value;
1481 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 0
1482 ? Match_Success
1483 : Match_InvalidOperand;
1484 }
1485 case MCK_1: {
1486 int64_t Value;
1487 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 1
1488 ? Match_Success
1489 : Match_InvalidOperand;
1490 }
1491 case MCK__MINUS_1: {
1492 int64_t Value;
1493 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == -1
1494 ? Match_Success
1495 : Match_InvalidOperand;
1496 }
1497 }
1498 if (Op->Kind == HexagonOperand::Token && Kind != InvalidMatchClass) {
1499 StringRef myStringRef = StringRef(Op->Tok.Data, Op->Tok.Length);
1500 if (matchTokenString(myStringRef.lower()) == (MatchClassKind)Kind)
1501 return Match_Success;
1502 if (matchTokenString(myStringRef.upper()) == (MatchClassKind)Kind)
1503 return Match_Success;
1504 }
1505
1506 DEBUG(dbgs() << "Unmatched Operand:");
1507 DEBUG(Op->dump());
1508 DEBUG(dbgs() << "\n");
1509
1510 return Match_InvalidOperand;
1511}
1512
1513void HexagonAsmParser::OutOfRange(SMLoc IDLoc, long long Val, long long Max) {
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001514 std::string errStr;
1515 raw_string_ostream ES(errStr);
Alexey Samsonov44ff2042015-12-02 22:59:22 +00001516 ES << "value " << Val << "(" << format_hex(Val, 0) << ") out of range: ";
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001517 if (Max >= 0)
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001518 ES << "0-" << Max;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001519 else
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001520 ES << Max << "-" << (-Max - 1);
1521 Error(IDLoc, ES.str().c_str());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001522}
1523
1524int HexagonAsmParser::processInstruction(MCInst &Inst,
1525 OperandVector const &Operands,
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001526 SMLoc IDLoc) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001527 MCContext &Context = getParser().getContext();
1528 const MCRegisterInfo *RI = getContext().getRegisterInfo();
1529 std::string r = "r";
1530 std::string v = "v";
1531 std::string Colon = ":";
1532
1533 bool is32bit = false; // used to distinguish between CONST32 and CONST64
1534 switch (Inst.getOpcode()) {
1535 default:
1536 break;
1537
Colin LeMahieuecef1d92016-02-16 20:38:17 +00001538 case Hexagon::A2_iconst: {
1539 Inst.setOpcode(Hexagon::A2_addi);
1540 MCOperand Reg = Inst.getOperand(0);
1541 MCOperand S16 = Inst.getOperand(1);
1542 HexagonMCInstrInfo::setMustNotExtend(*S16.getExpr());
1543 HexagonMCInstrInfo::setS23_2_reloc(*S16.getExpr());
1544 Inst.clear();
1545 Inst.addOperand(Reg);
1546 Inst.addOperand(MCOperand::createReg(Hexagon::R0));
1547 Inst.addOperand(S16);
1548 break;
1549 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001550 case Hexagon::M4_mpyrr_addr:
1551 case Hexagon::S4_addi_asl_ri:
1552 case Hexagon::S4_addi_lsr_ri:
1553 case Hexagon::S4_andi_asl_ri:
1554 case Hexagon::S4_andi_lsr_ri:
1555 case Hexagon::S4_ori_asl_ri:
1556 case Hexagon::S4_ori_lsr_ri:
1557 case Hexagon::S4_or_andix:
1558 case Hexagon::S4_subi_asl_ri:
1559 case Hexagon::S4_subi_lsr_ri: {
1560 MCOperand &Ry = Inst.getOperand(0);
1561 MCOperand &src = Inst.getOperand(2);
1562 if (RI->getEncodingValue(Ry.getReg()) != RI->getEncodingValue(src.getReg()))
1563 return Match_InvalidOperand;
1564 break;
1565 }
1566
1567 case Hexagon::C2_cmpgei: {
1568 MCOperand &MO = Inst.getOperand(2);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001569 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001570 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001571 Inst.setOpcode(Hexagon::C2_cmpgti);
1572 break;
1573 }
1574
1575 case Hexagon::C2_cmpgeui: {
1576 MCOperand &MO = Inst.getOperand(2);
1577 int64_t Value;
1578 bool Success = MO.getExpr()->evaluateAsAbsolute(Value);
Colin LeMahieu9d851f02015-11-09 21:06:28 +00001579 (void)Success;
1580 assert(Success && "Assured by matcher");
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001581 if (Value == 0) {
1582 MCInst TmpInst;
1583 MCOperand &Pd = Inst.getOperand(0);
1584 MCOperand &Rt = Inst.getOperand(1);
1585 TmpInst.setOpcode(Hexagon::C2_cmpeq);
1586 TmpInst.addOperand(Pd);
1587 TmpInst.addOperand(Rt);
1588 TmpInst.addOperand(Rt);
1589 Inst = TmpInst;
1590 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001591 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001592 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001593 Inst.setOpcode(Hexagon::C2_cmpgtui);
1594 }
1595 break;
1596 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001597
1598 // Translate a "$Rdd = $Rss" to "$Rdd = combine($Rs, $Rt)"
1599 case Hexagon::A2_tfrp: {
1600 MCOperand &MO = Inst.getOperand(1);
1601 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001602 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001603 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001604 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001605 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001606 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001607 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001608 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001609 Inst.setOpcode(Hexagon::A2_combinew);
1610 break;
1611 }
1612
1613 case Hexagon::A2_tfrpt:
1614 case Hexagon::A2_tfrpf: {
1615 MCOperand &MO = Inst.getOperand(2);
1616 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001617 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001618 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001619 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001620 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001621 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001622 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001623 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001624 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrpt)
1625 ? Hexagon::C2_ccombinewt
1626 : Hexagon::C2_ccombinewf);
1627 break;
1628 }
1629 case Hexagon::A2_tfrptnew:
1630 case Hexagon::A2_tfrpfnew: {
1631 MCOperand &MO = Inst.getOperand(2);
1632 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001633 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001634 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001635 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001636 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001637 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001638 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001639 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001640 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrptnew)
1641 ? Hexagon::C2_ccombinewnewt
1642 : Hexagon::C2_ccombinewnewf);
1643 break;
1644 }
1645
Krzysztof Parzyszek0e7d2d32016-04-28 16:43:16 +00001646 // Translate a "$Vdd = $Vss" to "$Vdd = vcombine($Vs, $Vt)"
1647 case Hexagon::HEXAGON_V6_vassignpair: {
1648 MCOperand &MO = Inst.getOperand(1);
1649 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
1650 std::string R1 = v + llvm::utostr(RegPairNum + 1);
1651 MO.setReg(MatchRegisterName(R1));
1652 // Add a new operand for the second register in the pair.
1653 std::string R2 = v + llvm::utostr(RegPairNum);
1654 Inst.addOperand(MCOperand::createReg(MatchRegisterName(R2)));
1655 Inst.setOpcode(Hexagon::V6_vcombine);
1656 break;
1657 }
1658
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001659 // Translate a "$Rx = CONST32(#imm)" to "$Rx = memw(gp+#LABEL) "
1660 case Hexagon::CONST32:
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001661 is32bit = true;
1662 // Translate a "$Rx:y = CONST64(#imm)" to "$Rx:y = memd(gp+#LABEL) "
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001663 case Hexagon::CONST64:
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001664 // FIXME: need better way to detect AsmStreamer (upstream removed getKind())
1665 if (!Parser.getStreamer().hasRawTextSupport()) {
1666 MCELFStreamer *MES = static_cast<MCELFStreamer *>(&Parser.getStreamer());
1667 MCOperand &MO_1 = Inst.getOperand(1);
1668 MCOperand &MO_0 = Inst.getOperand(0);
1669
1670 // push section onto section stack
1671 MES->PushSection();
1672
1673 std::string myCharStr;
1674 MCSectionELF *mySection;
1675
1676 // check if this as an immediate or a symbol
1677 int64_t Value;
1678 bool Absolute = MO_1.getExpr()->evaluateAsAbsolute(Value);
1679 if (Absolute) {
1680 // Create a new section - one for each constant
1681 // Some or all of the zeros are replaced with the given immediate.
1682 if (is32bit) {
1683 std::string myImmStr = utohexstr(static_cast<uint32_t>(Value));
1684 myCharStr = StringRef(".gnu.linkonce.l4.CONST_00000000")
1685 .drop_back(myImmStr.size())
1686 .str() +
1687 myImmStr;
1688 } else {
1689 std::string myImmStr = utohexstr(Value);
1690 myCharStr = StringRef(".gnu.linkonce.l8.CONST_0000000000000000")
1691 .drop_back(myImmStr.size())
1692 .str() +
1693 myImmStr;
1694 }
1695
1696 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1697 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1698 } else if (MO_1.isExpr()) {
1699 // .lita - for expressions
1700 myCharStr = ".lita";
1701 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1702 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1703 } else
1704 llvm_unreachable("unexpected type of machine operand!");
1705
1706 MES->SwitchSection(mySection);
1707 unsigned byteSize = is32bit ? 4 : 8;
1708 getStreamer().EmitCodeAlignment(byteSize, byteSize);
1709
1710 MCSymbol *Sym;
1711
1712 // for symbols, get rid of prepended ".gnu.linkonce.lx."
1713
1714 // emit symbol if needed
1715 if (Absolute) {
1716 Sym = getContext().getOrCreateSymbol(StringRef(myCharStr.c_str() + 16));
1717 if (Sym->isUndefined()) {
1718 getStreamer().EmitLabel(Sym);
1719 getStreamer().EmitSymbolAttribute(Sym, MCSA_Global);
1720 getStreamer().EmitIntValue(Value, byteSize);
1721 }
1722 } else if (MO_1.isExpr()) {
1723 const char *StringStart = 0;
1724 const char *StringEnd = 0;
1725 if (*Operands[4]->getStartLoc().getPointer() == '#') {
1726 StringStart = Operands[5]->getStartLoc().getPointer();
1727 StringEnd = Operands[6]->getStartLoc().getPointer();
1728 } else { // no pound
1729 StringStart = Operands[4]->getStartLoc().getPointer();
1730 StringEnd = Operands[5]->getStartLoc().getPointer();
1731 }
1732
1733 unsigned size = StringEnd - StringStart;
1734 std::string DotConst = ".CONST_";
1735 Sym = getContext().getOrCreateSymbol(DotConst +
1736 StringRef(StringStart, size));
1737
1738 if (Sym->isUndefined()) {
1739 // case where symbol is not yet defined: emit symbol
1740 getStreamer().EmitLabel(Sym);
1741 getStreamer().EmitSymbolAttribute(Sym, MCSA_Local);
1742 getStreamer().EmitValue(MO_1.getExpr(), 4);
1743 }
1744 } else
1745 llvm_unreachable("unexpected type of machine operand!");
1746
1747 MES->PopSection();
1748
1749 if (Sym) {
1750 MCInst TmpInst;
1751 if (is32bit) // 32 bit
1752 TmpInst.setOpcode(Hexagon::L2_loadrigp);
1753 else // 64 bit
1754 TmpInst.setOpcode(Hexagon::L2_loadrdgp);
1755
1756 TmpInst.addOperand(MO_0);
1757 TmpInst.addOperand(
1758 MCOperand::createExpr(MCSymbolRefExpr::create(Sym, getContext())));
1759 Inst = TmpInst;
1760 }
1761 }
1762 break;
1763
1764 // Translate a "$Rdd = #-imm" to "$Rdd = combine(#[-1,0], #-imm)"
1765 case Hexagon::A2_tfrpi: {
1766 MCOperand &Rdd = Inst.getOperand(0);
1767 MCOperand &MO = Inst.getOperand(1);
1768 int64_t Value;
1769 int sVal = (MO.getExpr()->evaluateAsAbsolute(Value) && Value < 0) ? -1 : 0;
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001770 MCOperand imm(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001771 HexagonMCExpr::create(MCConstantExpr::create(sVal, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001772 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, imm, MO);
1773 break;
1774 }
1775
1776 // Translate a "$Rdd = [#]#imm" to "$Rdd = combine(#, [#]#imm)"
1777 case Hexagon::TFRI64_V4: {
1778 MCOperand &Rdd = Inst.getOperand(0);
1779 MCOperand &MO = Inst.getOperand(1);
1780 int64_t Value;
1781 if (MO.getExpr()->evaluateAsAbsolute(Value)) {
David Majnemere61e4bf2016-06-21 05:10:24 +00001782 int s8 = Hi_32(Value);
1783 if (!isInt<8>(s8))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001784 OutOfRange(IDLoc, s8, -128);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001785 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001786 MCConstantExpr::create(s8, Context), Context))); // upper 32
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001787 auto Expr = HexagonMCExpr::create(
David Majnemere61e4bf2016-06-21 05:10:24 +00001788 MCConstantExpr::create(Lo_32(Value), Context), Context);
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001789 HexagonMCInstrInfo::setMustExtend(*Expr, HexagonMCInstrInfo::mustExtend(*MO.getExpr()));
1790 MCOperand imm2(MCOperand::createExpr(Expr)); // lower 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001791 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, imm2);
1792 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001793 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001794 MCConstantExpr::create(0, Context), Context))); // upper 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001795 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, MO);
1796 }
1797 break;
1798 }
1799
1800 // Handle $Rdd = combine(##imm, #imm)"
1801 case Hexagon::TFRI64_V2_ext: {
1802 MCOperand &Rdd = Inst.getOperand(0);
1803 MCOperand &MO1 = Inst.getOperand(1);
1804 MCOperand &MO2 = Inst.getOperand(2);
1805 int64_t Value;
1806 if (MO2.getExpr()->evaluateAsAbsolute(Value)) {
1807 int s8 = Value;
1808 if (s8 < -128 || s8 > 127)
1809 OutOfRange(IDLoc, s8, -128);
1810 }
1811 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, MO1, MO2);
1812 break;
1813 }
1814
1815 // Handle $Rdd = combine(#imm, ##imm)"
1816 case Hexagon::A4_combineii: {
1817 MCOperand &Rdd = Inst.getOperand(0);
1818 MCOperand &MO1 = Inst.getOperand(1);
1819 int64_t Value;
1820 if (MO1.getExpr()->evaluateAsAbsolute(Value)) {
1821 int s8 = Value;
1822 if (s8 < -128 || s8 > 127)
1823 OutOfRange(IDLoc, s8, -128);
1824 }
1825 MCOperand &MO2 = Inst.getOperand(2);
1826 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, MO1, MO2);
1827 break;
1828 }
1829
1830 case Hexagon::S2_tableidxb_goodsyntax: {
1831 Inst.setOpcode(Hexagon::S2_tableidxb);
1832 break;
1833 }
1834
1835 case Hexagon::S2_tableidxh_goodsyntax: {
1836 MCInst TmpInst;
1837 MCOperand &Rx = Inst.getOperand(0);
1838 MCOperand &_dst_ = Inst.getOperand(1);
1839 MCOperand &Rs = Inst.getOperand(2);
1840 MCOperand &Imm4 = Inst.getOperand(3);
1841 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001842 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001843 Imm6.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001844 TmpInst.setOpcode(Hexagon::S2_tableidxh);
1845 TmpInst.addOperand(Rx);
1846 TmpInst.addOperand(_dst_);
1847 TmpInst.addOperand(Rs);
1848 TmpInst.addOperand(Imm4);
1849 TmpInst.addOperand(Imm6);
1850 Inst = TmpInst;
1851 break;
1852 }
1853
1854 case Hexagon::S2_tableidxw_goodsyntax: {
1855 MCInst TmpInst;
1856 MCOperand &Rx = Inst.getOperand(0);
1857 MCOperand &_dst_ = Inst.getOperand(1);
1858 MCOperand &Rs = Inst.getOperand(2);
1859 MCOperand &Imm4 = Inst.getOperand(3);
1860 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001861 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001862 Imm6.getExpr(), MCConstantExpr::create(2, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001863 TmpInst.setOpcode(Hexagon::S2_tableidxw);
1864 TmpInst.addOperand(Rx);
1865 TmpInst.addOperand(_dst_);
1866 TmpInst.addOperand(Rs);
1867 TmpInst.addOperand(Imm4);
1868 TmpInst.addOperand(Imm6);
1869 Inst = TmpInst;
1870 break;
1871 }
1872
1873 case Hexagon::S2_tableidxd_goodsyntax: {
1874 MCInst TmpInst;
1875 MCOperand &Rx = Inst.getOperand(0);
1876 MCOperand &_dst_ = Inst.getOperand(1);
1877 MCOperand &Rs = Inst.getOperand(2);
1878 MCOperand &Imm4 = Inst.getOperand(3);
1879 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001880 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001881 Imm6.getExpr(), MCConstantExpr::create(3, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001882 TmpInst.setOpcode(Hexagon::S2_tableidxd);
1883 TmpInst.addOperand(Rx);
1884 TmpInst.addOperand(_dst_);
1885 TmpInst.addOperand(Rs);
1886 TmpInst.addOperand(Imm4);
1887 TmpInst.addOperand(Imm6);
1888 Inst = TmpInst;
1889 break;
1890 }
1891
1892 case Hexagon::M2_mpyui: {
1893 Inst.setOpcode(Hexagon::M2_mpyi);
1894 break;
1895 }
1896 case Hexagon::M2_mpysmi: {
1897 MCInst TmpInst;
1898 MCOperand &Rd = Inst.getOperand(0);
1899 MCOperand &Rs = Inst.getOperand(1);
1900 MCOperand &Imm = Inst.getOperand(2);
1901 int64_t Value;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001902 MCExpr const &Expr = *Imm.getExpr();
1903 bool Absolute = Expr.evaluateAsAbsolute(Value);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001904 assert(Absolute);
1905 (void)Absolute;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001906 if (!HexagonMCInstrInfo::mustExtend(Expr)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001907 if (Value < 0 && Value > -256) {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001908 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001909 MCConstantExpr::create(Value * -1, Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001910 TmpInst.setOpcode(Hexagon::M2_mpysin);
1911 } else if (Value < 256 && Value >= 0)
1912 TmpInst.setOpcode(Hexagon::M2_mpysip);
1913 else
1914 return Match_InvalidOperand;
1915 } else {
1916 if (Value >= 0)
1917 TmpInst.setOpcode(Hexagon::M2_mpysip);
1918 else
1919 return Match_InvalidOperand;
1920 }
1921 TmpInst.addOperand(Rd);
1922 TmpInst.addOperand(Rs);
1923 TmpInst.addOperand(Imm);
1924 Inst = TmpInst;
1925 break;
1926 }
1927
1928 case Hexagon::S2_asr_i_r_rnd_goodsyntax: {
1929 MCOperand &Imm = Inst.getOperand(2);
1930 MCInst TmpInst;
1931 int64_t Value;
1932 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1933 assert(Absolute);
1934 (void)Absolute;
1935 if (Value == 0) { // convert to $Rd = $Rs
1936 TmpInst.setOpcode(Hexagon::A2_tfr);
1937 MCOperand &Rd = Inst.getOperand(0);
1938 MCOperand &Rs = Inst.getOperand(1);
1939 TmpInst.addOperand(Rd);
1940 TmpInst.addOperand(Rs);
1941 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001942 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001943 MCBinaryExpr::createSub(Imm.getExpr(),
1944 MCConstantExpr::create(1, Context), Context),
1945 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001946 TmpInst.setOpcode(Hexagon::S2_asr_i_r_rnd);
1947 MCOperand &Rd = Inst.getOperand(0);
1948 MCOperand &Rs = Inst.getOperand(1);
1949 TmpInst.addOperand(Rd);
1950 TmpInst.addOperand(Rs);
1951 TmpInst.addOperand(Imm);
1952 }
1953 Inst = TmpInst;
1954 break;
1955 }
1956
1957 case Hexagon::S2_asr_i_p_rnd_goodsyntax: {
1958 MCOperand &Rdd = Inst.getOperand(0);
1959 MCOperand &Rss = Inst.getOperand(1);
1960 MCOperand &Imm = Inst.getOperand(2);
1961 int64_t Value;
1962 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1963 assert(Absolute);
1964 (void)Absolute;
1965 if (Value == 0) { // convert to $Rdd = combine ($Rs[0], $Rs[1])
1966 MCInst TmpInst;
1967 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001968 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001969 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001970 Rss.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001971 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001972 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001973 StringRef Reg2(R2);
1974 TmpInst.setOpcode(Hexagon::A2_combinew);
1975 TmpInst.addOperand(Rdd);
1976 TmpInst.addOperand(Rss);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001977 TmpInst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001978 Inst = TmpInst;
1979 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001980 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001981 MCBinaryExpr::createSub(Imm.getExpr(),
1982 MCConstantExpr::create(1, Context), Context),
1983 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001984 Inst.setOpcode(Hexagon::S2_asr_i_p_rnd);
1985 }
1986 break;
1987 }
1988
1989 case Hexagon::A4_boundscheck: {
1990 MCOperand &Rs = Inst.getOperand(1);
1991 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
1992 if (RegNum & 1) { // Odd mapped to raw:hi, regpair is rodd:odd-1, like r3:2
1993 Inst.setOpcode(Hexagon::A4_boundscheck_hi);
1994 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00001995 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001996 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001997 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001998 } else { // raw:lo
1999 Inst.setOpcode(Hexagon::A4_boundscheck_lo);
2000 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002001 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002002 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002003 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002004 }
2005 break;
2006 }
2007
2008 case Hexagon::A2_addsp: {
2009 MCOperand &Rs = Inst.getOperand(1);
2010 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
2011 if (RegNum & 1) { // Odd mapped to raw:hi
2012 Inst.setOpcode(Hexagon::A2_addsph);
2013 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002014 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002015 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002016 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002017 } else { // Even mapped raw:lo
2018 Inst.setOpcode(Hexagon::A2_addspl);
2019 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002020 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002021 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002022 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002023 }
2024 break;
2025 }
2026
2027 case Hexagon::M2_vrcmpys_s1: {
2028 MCOperand &Rt = Inst.getOperand(2);
2029 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2030 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2031 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_h);
2032 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002033 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002034 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002035 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002036 } else { // Even mapped sat:raw:lo
2037 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_l);
2038 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002039 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002040 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002041 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002042 }
2043 break;
2044 }
2045
2046 case Hexagon::M2_vrcmpys_acc_s1: {
2047 MCInst TmpInst;
2048 MCOperand &Rxx = Inst.getOperand(0);
2049 MCOperand &Rss = Inst.getOperand(2);
2050 MCOperand &Rt = Inst.getOperand(3);
2051 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2052 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2053 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_h);
2054 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002055 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002056 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002057 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002058 } else { // Even mapped sat:raw:lo
2059 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_l);
2060 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002061 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002062 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002063 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002064 }
2065 // Registers are in different positions
2066 TmpInst.addOperand(Rxx);
2067 TmpInst.addOperand(Rxx);
2068 TmpInst.addOperand(Rss);
2069 TmpInst.addOperand(Rt);
2070 Inst = TmpInst;
2071 break;
2072 }
2073
2074 case Hexagon::M2_vrcmpys_s1rp: {
2075 MCOperand &Rt = Inst.getOperand(2);
2076 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2077 if (RegNum & 1) { // Odd mapped to rnd:sat:raw:hi
2078 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_h);
2079 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002080 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002081 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002082 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002083 } else { // Even mapped rnd:sat:raw:lo
2084 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_l);
2085 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002086 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002087 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002088 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002089 }
2090 break;
2091 }
2092
2093 case Hexagon::S5_asrhub_rnd_sat_goodsyntax: {
2094 MCOperand &Imm = Inst.getOperand(2);
2095 int64_t Value;
2096 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
2097 assert(Absolute);
2098 (void)Absolute;
2099 if (Value == 0)
2100 Inst.setOpcode(Hexagon::S2_vsathub);
2101 else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002102 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002103 MCBinaryExpr::createSub(Imm.getExpr(),
2104 MCConstantExpr::create(1, Context), Context),
2105 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002106 Inst.setOpcode(Hexagon::S5_asrhub_rnd_sat);
2107 }
2108 break;
2109 }
2110
2111 case Hexagon::S5_vasrhrnd_goodsyntax: {
2112 MCOperand &Rdd = Inst.getOperand(0);
2113 MCOperand &Rss = Inst.getOperand(1);
2114 MCOperand &Imm = Inst.getOperand(2);
2115 int64_t Value;
2116 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
2117 assert(Absolute);
2118 (void)Absolute;
2119 if (Value == 0) {
2120 MCInst TmpInst;
2121 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00002122 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002123 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002124 Rss.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002125 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00002126 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002127 StringRef Reg2(R2);
2128 TmpInst.setOpcode(Hexagon::A2_combinew);
2129 TmpInst.addOperand(Rdd);
2130 TmpInst.addOperand(Rss);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002131 TmpInst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002132 Inst = TmpInst;
2133 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002134 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002135 MCBinaryExpr::createSub(Imm.getExpr(),
2136 MCConstantExpr::create(1, Context), Context),
2137 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002138 Inst.setOpcode(Hexagon::S5_vasrhrnd);
2139 }
2140 break;
2141 }
2142
2143 case Hexagon::A2_not: {
2144 MCInst TmpInst;
2145 MCOperand &Rd = Inst.getOperand(0);
2146 MCOperand &Rs = Inst.getOperand(1);
2147 TmpInst.setOpcode(Hexagon::A2_subri);
2148 TmpInst.addOperand(Rd);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002149 TmpInst.addOperand(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002150 HexagonMCExpr::create(MCConstantExpr::create(-1, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002151 TmpInst.addOperand(Rs);
2152 Inst = TmpInst;
2153 break;
2154 }
2155 } // switch
2156
2157 return Match_Success;
2158}
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002159
2160
2161unsigned HexagonAsmParser::matchRegister(StringRef Name) {
2162 if (unsigned Reg = MatchRegisterName(Name))
2163 return Reg;
2164 return MatchRegisterAltName(Name);
2165}