blob: 67efa206a40666ebc3fc782d7b1c3ef877787d14 [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();
732 MCAsmLexer &Lexer = getLexer();
733 while (true) {
734 if (!Parser.getTok().is(AsmToken::Colon))
735 return false;
736 Lexer.Lex();
737 StringRef Option = Parser.getTok().getString();
738 if (Option.compare_lower("endloop0") == 0)
739 HexagonMCInstrInfo::setInnerLoop(MCB);
740 else if (Option.compare_lower("endloop1") == 0)
741 HexagonMCInstrInfo::setOuterLoop(MCB);
742 else if (Option.compare_lower("mem_noshuf") == 0)
743 HexagonMCInstrInfo::setMemReorderDisabled(MCB);
744 else if (Option.compare_lower("mem_shuf") == 0)
745 HexagonMCInstrInfo::setMemStoreReorderEnabled(MCB);
746 else
747 return true;
748 Lexer.Lex();
749 }
750}
751
752// For instruction aliases, immediates are generated rather than
753// MCConstantExpr. Convert them for uniform MCExpr.
754// Also check for signed/unsigned mismatches and warn
755void HexagonAsmParser::canonicalizeImmediates(MCInst &MCI) {
756 MCInst NewInst;
757 NewInst.setOpcode(MCI.getOpcode());
758 for (MCOperand &I : MCI)
759 if (I.isImm()) {
760 int64_t Value (I.getImm());
Colin LeMahieuc7b21242016-02-15 18:47:55 +0000761 NewInst.addOperand(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000762 MCConstantExpr::create(Value, getContext()), getContext())));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000763 }
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000764 else {
765 if (I.isExpr() && cast<HexagonMCExpr>(I.getExpr())->signMismatch() &&
766 WarnSignedMismatch)
767 Warning (MCI.getLoc(), "Signed/Unsigned mismatch");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000768 NewInst.addOperand(I);
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000769 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000770 MCI = NewInst;
771}
772
773bool HexagonAsmParser::matchOneInstruction(MCInst &MCI, SMLoc IDLoc,
774 OperandVector &InstOperands,
775 uint64_t &ErrorInfo,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000776 bool MatchingInlineAsm) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000777 // Perform matching with tablegen asmmatcher generated function
778 int result =
779 MatchInstructionImpl(InstOperands, MCI, ErrorInfo, MatchingInlineAsm);
780 if (result == Match_Success) {
781 MCI.setLoc(IDLoc);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000782 canonicalizeImmediates(MCI);
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000783 result = processInstruction(MCI, InstOperands, IDLoc);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000784
785 DEBUG(dbgs() << "Insn:");
786 DEBUG(MCI.dump_pretty(dbgs()));
787 DEBUG(dbgs() << "\n\n");
788
789 MCI.setLoc(IDLoc);
790 }
791
792 // Create instruction operand for bundle instruction
793 // Break this into a separate function Code here is less readable
794 // Think about how to get an instruction error to report correctly.
795 // SMLoc will return the "{"
796 switch (result) {
797 default:
798 break;
799 case Match_Success:
800 return false;
801 case Match_MissingFeature:
802 return Error(IDLoc, "invalid instruction");
803 case Match_MnemonicFail:
804 return Error(IDLoc, "unrecognized instruction");
805 case Match_InvalidOperand:
806 SMLoc ErrorLoc = IDLoc;
807 if (ErrorInfo != ~0U) {
808 if (ErrorInfo >= InstOperands.size())
809 return Error(IDLoc, "too few operands for instruction");
810
811 ErrorLoc = (static_cast<HexagonOperand *>(InstOperands[ErrorInfo].get()))
812 ->getStartLoc();
813 if (ErrorLoc == SMLoc())
814 ErrorLoc = IDLoc;
815 }
816 return Error(ErrorLoc, "invalid operand for instruction");
817 }
818 llvm_unreachable("Implement any new match types added!");
819}
820
821bool HexagonAsmParser::mustExtend(OperandVector &Operands) {
822 unsigned Count = 0;
823 for (std::unique_ptr<MCParsedAsmOperand> &i : Operands)
824 if (i->isImm())
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000825 if (HexagonMCInstrInfo::mustExtend(
826 *static_cast<HexagonOperand *>(i.get())->Imm.Val))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000827 ++Count;
828 // Multiple extenders should have been filtered by iss9Ext et. al.
829 assert(Count < 2 && "Multiple extenders");
830 return Count == 1;
831}
832
833bool HexagonAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
834 OperandVector &Operands,
835 MCStreamer &Out,
836 uint64_t &ErrorInfo,
837 bool MatchingInlineAsm) {
838 if (!InBrackets) {
839 MCB.clear();
840 MCB.addOperand(MCOperand::createImm(0));
841 }
842 HexagonOperand &FirstOperand = static_cast<HexagonOperand &>(*Operands[0]);
843 if (FirstOperand.isToken() && FirstOperand.getToken() == "{") {
844 assert(Operands.size() == 1 && "Brackets should be by themselves");
845 if (InBrackets) {
846 getParser().Error(IDLoc, "Already in a packet");
847 return true;
848 }
849 InBrackets = true;
850 return false;
851 }
852 if (FirstOperand.isToken() && FirstOperand.getToken() == "}") {
853 assert(Operands.size() == 1 && "Brackets should be by themselves");
854 if (!InBrackets) {
855 getParser().Error(IDLoc, "Not in a packet");
856 return true;
857 }
858 InBrackets = false;
859 if (matchBundleOptions())
860 return true;
861 return finishBundle(IDLoc, Out);
862 }
863 MCInst *SubInst = new (getParser().getContext()) MCInst;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000864 if (matchOneInstruction(*SubInst, IDLoc, Operands, ErrorInfo,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000865 MatchingInlineAsm))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000866 return true;
867 HexagonMCInstrInfo::extendIfNeeded(
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000868 getParser().getContext(), MCII, MCB, *SubInst);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000869 MCB.addOperand(MCOperand::createInst(SubInst));
870 if (!InBrackets)
871 return finishBundle(IDLoc, Out);
872 return false;
873}
874
875/// ParseDirective parses the Hexagon specific directives
876bool HexagonAsmParser::ParseDirective(AsmToken DirectiveID) {
877 StringRef IDVal = DirectiveID.getIdentifier();
878 if ((IDVal.lower() == ".word") || (IDVal.lower() == ".4byte"))
879 return ParseDirectiveValue(4, DirectiveID.getLoc());
880 if (IDVal.lower() == ".short" || IDVal.lower() == ".hword" ||
881 IDVal.lower() == ".half")
882 return ParseDirectiveValue(2, DirectiveID.getLoc());
883 if (IDVal.lower() == ".falign")
884 return ParseDirectiveFalign(256, DirectiveID.getLoc());
885 if ((IDVal.lower() == ".lcomm") || (IDVal.lower() == ".lcommon"))
886 return ParseDirectiveComm(true, DirectiveID.getLoc());
887 if ((IDVal.lower() == ".comm") || (IDVal.lower() == ".common"))
888 return ParseDirectiveComm(false, DirectiveID.getLoc());
889 if (IDVal.lower() == ".subsection")
890 return ParseDirectiveSubsection(DirectiveID.getLoc());
891
892 return true;
893}
894bool HexagonAsmParser::ParseDirectiveSubsection(SMLoc L) {
895 const MCExpr *Subsection = 0;
896 int64_t Res;
897
898 assert((getLexer().isNot(AsmToken::EndOfStatement)) &&
899 "Invalid subsection directive");
900 getParser().parseExpression(Subsection);
901
902 if (!Subsection->evaluateAsAbsolute(Res))
903 return Error(L, "Cannot evaluate subsection number");
904
905 if (getLexer().isNot(AsmToken::EndOfStatement))
906 return TokError("unexpected token in directive");
907
908 // 0-8192 is the hard-coded range in MCObjectStreamper.cpp, this keeps the
909 // negative subsections together and in the same order but at the opposite
910 // end of the section. Only legacy hexagon-gcc created assembly code
911 // used negative subsections.
912 if ((Res < 0) && (Res > -8193))
Colin LeMahieuc7b21242016-02-15 18:47:55 +0000913 Subsection = HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000914 MCConstantExpr::create(8192 + Res, getContext()), getContext());
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000915
916 getStreamer().SubSection(Subsection);
917 return false;
918}
919
920/// ::= .falign [expression]
921bool HexagonAsmParser::ParseDirectiveFalign(unsigned Size, SMLoc L) {
922
923 int64_t MaxBytesToFill = 15;
924
925 // if there is an arguement
926 if (getLexer().isNot(AsmToken::EndOfStatement)) {
927 const MCExpr *Value;
928 SMLoc ExprLoc = L;
929
930 // Make sure we have a number (false is returned if expression is a number)
931 if (getParser().parseExpression(Value) == false) {
932 // Make sure this is a number that is in range
933 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
934 uint64_t IntValue = MCE->getValue();
935 if (!isUIntN(Size, IntValue) && !isIntN(Size, IntValue))
936 return Error(ExprLoc, "literal value out of range (256) for falign");
937 MaxBytesToFill = IntValue;
938 Lex();
939 } else {
940 return Error(ExprLoc, "not a valid expression for falign directive");
941 }
942 }
943
944 getTargetStreamer().emitFAlign(16, MaxBytesToFill);
945 Lex();
946
947 return false;
948}
949
950/// ::= .word [ expression (, expression)* ]
951bool HexagonAsmParser::ParseDirectiveValue(unsigned Size, SMLoc L) {
952 if (getLexer().isNot(AsmToken::EndOfStatement)) {
953
954 for (;;) {
955 const MCExpr *Value;
956 SMLoc ExprLoc = L;
957 if (getParser().parseExpression(Value))
958 return true;
959
960 // Special case constant expressions to match code generator.
961 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
962 assert(Size <= 8 && "Invalid size");
963 uint64_t IntValue = MCE->getValue();
964 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
965 return Error(ExprLoc, "literal value out of range for directive");
966 getStreamer().EmitIntValue(IntValue, Size);
967 } else
968 getStreamer().EmitValue(Value, Size);
969
970 if (getLexer().is(AsmToken::EndOfStatement))
971 break;
972
973 // FIXME: Improve diagnostic.
974 if (getLexer().isNot(AsmToken::Comma))
975 return TokError("unexpected token in directive");
976 Lex();
977 }
978 }
979
980 Lex();
981 return false;
982}
983
984// This is largely a copy of AsmParser's ParseDirectiveComm extended to
985// accept a 3rd argument, AccessAlignment which indicates the smallest
986// memory access made to the symbol, expressed in bytes. If no
987// AccessAlignment is specified it defaults to the Alignment Value.
988// Hexagon's .lcomm:
989// .lcomm Symbol, Length, Alignment, AccessAlignment
990bool HexagonAsmParser::ParseDirectiveComm(bool IsLocal, SMLoc Loc) {
991 // FIXME: need better way to detect if AsmStreamer (upstream removed
992 // getKind())
993 if (getStreamer().hasRawTextSupport())
994 return true; // Only object file output requires special treatment.
995
996 StringRef Name;
997 if (getParser().parseIdentifier(Name))
998 return TokError("expected identifier in directive");
999 // Handle the identifier as the key symbol.
1000 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
1001
1002 if (getLexer().isNot(AsmToken::Comma))
1003 return TokError("unexpected token in directive");
1004 Lex();
1005
1006 int64_t Size;
1007 SMLoc SizeLoc = getLexer().getLoc();
1008 if (getParser().parseAbsoluteExpression(Size))
1009 return true;
1010
1011 int64_t ByteAlignment = 1;
1012 SMLoc ByteAlignmentLoc;
1013 if (getLexer().is(AsmToken::Comma)) {
1014 Lex();
1015 ByteAlignmentLoc = getLexer().getLoc();
1016 if (getParser().parseAbsoluteExpression(ByteAlignment))
1017 return true;
1018 if (!isPowerOf2_64(ByteAlignment))
1019 return Error(ByteAlignmentLoc, "alignment must be a power of 2");
1020 }
1021
1022 int64_t AccessAlignment = 0;
1023 if (getLexer().is(AsmToken::Comma)) {
1024 // The optional access argument specifies the size of the smallest memory
1025 // access to be made to the symbol, expressed in bytes.
1026 SMLoc AccessAlignmentLoc;
1027 Lex();
1028 AccessAlignmentLoc = getLexer().getLoc();
1029 if (getParser().parseAbsoluteExpression(AccessAlignment))
1030 return true;
1031
1032 if (!isPowerOf2_64(AccessAlignment))
1033 return Error(AccessAlignmentLoc, "access alignment must be a power of 2");
1034 }
1035
1036 if (getLexer().isNot(AsmToken::EndOfStatement))
1037 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1038
1039 Lex();
1040
1041 // NOTE: a size of zero for a .comm should create a undefined symbol
1042 // but a size of .lcomm creates a bss symbol of size zero.
1043 if (Size < 0)
1044 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1045 "be less than zero");
1046
1047 // NOTE: The alignment in the directive is a power of 2 value, the assembler
1048 // may internally end up wanting an alignment in bytes.
1049 // FIXME: Diagnose overflow.
1050 if (ByteAlignment < 0)
1051 return Error(ByteAlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1052 "alignment, can't be less than zero");
1053
1054 if (!Sym->isUndefined())
1055 return Error(Loc, "invalid symbol redefinition");
1056
1057 HexagonMCELFStreamer &HexagonELFStreamer =
1058 static_cast<HexagonMCELFStreamer &>(getStreamer());
1059 if (IsLocal) {
1060 HexagonELFStreamer.HexagonMCEmitLocalCommonSymbol(Sym, Size, ByteAlignment,
1061 AccessAlignment);
1062 return false;
1063 }
1064
1065 HexagonELFStreamer.HexagonMCEmitCommonSymbol(Sym, Size, ByteAlignment,
1066 AccessAlignment);
1067 return false;
1068}
1069
1070// validate register against architecture
1071bool HexagonAsmParser::RegisterMatchesArch(unsigned MatchNum) const {
1072 return true;
1073}
1074
1075// extern "C" void LLVMInitializeHexagonAsmLexer();
1076
1077/// Force static initialization.
1078extern "C" void LLVMInitializeHexagonAsmParser() {
1079 RegisterMCAsmParser<HexagonAsmParser> X(TheHexagonTarget);
1080}
1081
1082#define GET_MATCHER_IMPLEMENTATION
1083#define GET_REGISTER_MATCHER
1084#include "HexagonGenAsmMatcher.inc"
1085
1086namespace {
1087bool previousEqual(OperandVector &Operands, size_t Index, StringRef String) {
1088 if (Index >= Operands.size())
1089 return false;
1090 MCParsedAsmOperand &Operand = *Operands[Operands.size() - Index - 1];
1091 if (!Operand.isToken())
1092 return false;
1093 return static_cast<HexagonOperand &>(Operand).getToken().equals_lower(String);
1094}
1095bool previousIsLoop(OperandVector &Operands, size_t Index) {
1096 return previousEqual(Operands, Index, "loop0") ||
1097 previousEqual(Operands, Index, "loop1") ||
1098 previousEqual(Operands, Index, "sp1loop0") ||
1099 previousEqual(Operands, Index, "sp2loop0") ||
1100 previousEqual(Operands, Index, "sp3loop0");
1101}
1102}
1103
1104bool HexagonAsmParser::splitIdentifier(OperandVector &Operands) {
1105 AsmToken const &Token = getParser().getTok();
1106 StringRef String = Token.getString();
1107 SMLoc Loc = Token.getLoc();
1108 getLexer().Lex();
1109 do {
1110 std::pair<StringRef, StringRef> HeadTail = String.split('.');
1111 if (!HeadTail.first.empty())
1112 Operands.push_back(HexagonOperand::CreateToken(HeadTail.first, Loc));
1113 if (!HeadTail.second.empty())
1114 Operands.push_back(HexagonOperand::CreateToken(
1115 String.substr(HeadTail.first.size(), 1), Loc));
1116 String = HeadTail.second;
1117 } while (!String.empty());
1118 return false;
1119}
1120
1121bool HexagonAsmParser::parseOperand(OperandVector &Operands) {
1122 unsigned Register;
1123 SMLoc Begin;
1124 SMLoc End;
1125 MCAsmLexer &Lexer = getLexer();
1126 if (!ParseRegister(Register, Begin, End)) {
1127 if (!ErrorMissingParenthesis)
1128 switch (Register) {
1129 default:
1130 break;
1131 case Hexagon::P0:
1132 case Hexagon::P1:
1133 case Hexagon::P2:
1134 case Hexagon::P3:
1135 if (previousEqual(Operands, 0, "if")) {
1136 if (WarnMissingParenthesis)
1137 Warning (Begin, "Missing parenthesis around predicate register");
1138 static char const *LParen = "(";
1139 static char const *RParen = ")";
1140 Operands.push_back(HexagonOperand::CreateToken(LParen, Begin));
1141 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End));
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +00001142 const AsmToken &MaybeDotNew = Lexer.getTok();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001143 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) &&
1144 MaybeDotNew.getString().equals_lower(".new"))
1145 splitIdentifier(Operands);
1146 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin));
1147 return false;
1148 }
1149 if (previousEqual(Operands, 0, "!") &&
1150 previousEqual(Operands, 1, "if")) {
1151 if (WarnMissingParenthesis)
1152 Warning (Begin, "Missing parenthesis around predicate register");
1153 static char const *LParen = "(";
1154 static char const *RParen = ")";
1155 Operands.insert(Operands.end () - 1,
1156 HexagonOperand::CreateToken(LParen, Begin));
1157 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End));
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +00001158 const AsmToken &MaybeDotNew = Lexer.getTok();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001159 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) &&
1160 MaybeDotNew.getString().equals_lower(".new"))
1161 splitIdentifier(Operands);
1162 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin));
1163 return false;
1164 }
1165 break;
1166 }
1167 Operands.push_back(HexagonOperand::CreateReg(
1168 Register, Begin, End));
1169 return false;
1170 }
1171 return splitIdentifier(Operands);
1172}
1173
1174bool HexagonAsmParser::isLabel(AsmToken &Token) {
1175 MCAsmLexer &Lexer = getLexer();
1176 AsmToken const &Second = Lexer.getTok();
1177 AsmToken Third = Lexer.peekTok();
1178 StringRef String = Token.getString();
1179 if (Token.is(AsmToken::TokenKind::LCurly) ||
1180 Token.is(AsmToken::TokenKind::RCurly))
1181 return false;
1182 if (!Token.is(AsmToken::TokenKind::Identifier))
1183 return true;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001184 if (!matchRegister(String.lower()))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001185 return true;
Colin LeMahieu9d851f02015-11-09 21:06:28 +00001186 (void)Second;
1187 assert(Second.is(AsmToken::Colon));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001188 StringRef Raw (String.data(), Third.getString().data() - String.data() +
1189 Third.getString().size());
1190 std::string Collapsed = Raw;
1191 Collapsed.erase(std::remove_if(Collapsed.begin(), Collapsed.end(), isspace),
1192 Collapsed.end());
1193 StringRef Whole = Collapsed;
1194 std::pair<StringRef, StringRef> DotSplit = Whole.split('.');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001195 if (!matchRegister(DotSplit.first.lower()))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001196 return true;
1197 return false;
1198}
1199
1200bool HexagonAsmParser::handleNoncontigiousRegister(bool Contigious, SMLoc &Loc) {
1201 if (!Contigious && ErrorNoncontigiousRegister) {
1202 Error(Loc, "Register name is not contigious");
1203 return true;
1204 }
1205 if (!Contigious && WarnNoncontigiousRegister)
1206 Warning(Loc, "Register name is not contigious");
1207 return false;
1208}
1209
1210bool HexagonAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) {
1211 MCAsmLexer &Lexer = getLexer();
1212 StartLoc = getLexer().getLoc();
1213 SmallVector<AsmToken, 5> Lookahead;
1214 StringRef RawString(Lexer.getTok().getString().data(), 0);
1215 bool Again = Lexer.is(AsmToken::Identifier);
1216 bool NeededWorkaround = false;
1217 while (Again) {
1218 AsmToken const &Token = Lexer.getTok();
1219 RawString = StringRef(RawString.data(),
1220 Token.getString().data() - RawString.data () +
1221 Token.getString().size());
1222 Lookahead.push_back(Token);
1223 Lexer.Lex();
1224 bool Contigious = Lexer.getTok().getString().data() ==
1225 Lookahead.back().getString().data() +
1226 Lookahead.back().getString().size();
1227 bool Type = Lexer.is(AsmToken::Identifier) || Lexer.is(AsmToken::Dot) ||
1228 Lexer.is(AsmToken::Integer) || Lexer.is(AsmToken::Real) ||
1229 Lexer.is(AsmToken::Colon);
1230 bool Workaround = Lexer.is(AsmToken::Colon) ||
1231 Lookahead.back().is(AsmToken::Colon);
1232 Again = (Contigious && Type) || (Workaround && Type);
1233 NeededWorkaround = NeededWorkaround || (Again && !(Contigious && Type));
1234 }
1235 std::string Collapsed = RawString;
1236 Collapsed.erase(std::remove_if(Collapsed.begin(), Collapsed.end(), isspace),
1237 Collapsed.end());
1238 StringRef FullString = Collapsed;
1239 std::pair<StringRef, StringRef> DotSplit = FullString.split('.');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001240 unsigned DotReg = matchRegister(DotSplit.first.lower());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001241 if (DotReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1242 if (DotSplit.second.empty()) {
1243 RegNo = DotReg;
1244 EndLoc = Lexer.getLoc();
1245 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1246 return true;
1247 return false;
1248 } else {
1249 RegNo = DotReg;
1250 size_t First = RawString.find('.');
1251 StringRef DotString (RawString.data() + First, RawString.size() - First);
1252 Lexer.UnLex(AsmToken(AsmToken::Identifier, DotString));
1253 EndLoc = Lexer.getLoc();
1254 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1255 return true;
1256 return false;
1257 }
1258 }
1259 std::pair<StringRef, StringRef> ColonSplit = StringRef(FullString).split(':');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001260 unsigned ColonReg = matchRegister(ColonSplit.first.lower());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001261 if (ColonReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1262 Lexer.UnLex(Lookahead.back());
1263 Lookahead.pop_back();
1264 Lexer.UnLex(Lookahead.back());
1265 Lookahead.pop_back();
1266 RegNo = ColonReg;
1267 EndLoc = Lexer.getLoc();
1268 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1269 return true;
1270 return false;
1271 }
1272 while (!Lookahead.empty()) {
1273 Lexer.UnLex(Lookahead.back());
1274 Lookahead.pop_back();
1275 }
1276 return true;
1277}
1278
1279bool HexagonAsmParser::implicitExpressionLocation(OperandVector &Operands) {
1280 if (previousEqual(Operands, 0, "call"))
1281 return true;
1282 if (previousEqual(Operands, 0, "jump"))
1283 if (!getLexer().getTok().is(AsmToken::Colon))
1284 return true;
1285 if (previousEqual(Operands, 0, "(") && previousIsLoop(Operands, 1))
1286 return true;
1287 if (previousEqual(Operands, 1, ":") && previousEqual(Operands, 2, "jump") &&
1288 (previousEqual(Operands, 0, "nt") || previousEqual(Operands, 0, "t")))
1289 return true;
1290 return false;
1291}
1292
1293bool HexagonAsmParser::parseExpression(MCExpr const *& Expr) {
1294 llvm::SmallVector<AsmToken, 4> Tokens;
1295 MCAsmLexer &Lexer = getLexer();
1296 bool Done = false;
1297 static char const * Comma = ",";
1298 do {
1299 Tokens.emplace_back (Lexer.getTok());
1300 Lexer.Lex();
1301 switch (Tokens.back().getKind())
1302 {
1303 case AsmToken::TokenKind::Hash:
1304 if (Tokens.size () > 1)
1305 if ((Tokens.end () - 2)->getKind() == AsmToken::TokenKind::Plus) {
1306 Tokens.insert(Tokens.end() - 2,
1307 AsmToken(AsmToken::TokenKind::Comma, Comma));
1308 Done = true;
1309 }
1310 break;
1311 case AsmToken::TokenKind::RCurly:
1312 case AsmToken::TokenKind::EndOfStatement:
1313 case AsmToken::TokenKind::Eof:
1314 Done = true;
1315 break;
1316 default:
1317 break;
1318 }
1319 } while (!Done);
1320 while (!Tokens.empty()) {
1321 Lexer.UnLex(Tokens.back());
1322 Tokens.pop_back();
1323 }
1324 return getParser().parseExpression(Expr);
1325}
1326
1327bool HexagonAsmParser::parseExpressionOrOperand(OperandVector &Operands) {
1328 if (implicitExpressionLocation(Operands)) {
1329 MCAsmParser &Parser = getParser();
1330 SMLoc Loc = Parser.getLexer().getLoc();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001331 MCExpr const *Expr = nullptr;
1332 bool Error = parseExpression(Expr);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001333 Expr = HexagonMCExpr::create(Expr, getContext());
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001334 if (!Error)
1335 Operands.push_back(HexagonOperand::CreateImm(Expr, Loc, Loc));
1336 return Error;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001337 }
1338 return parseOperand(Operands);
1339}
1340
1341/// Parse an instruction.
1342bool HexagonAsmParser::parseInstruction(OperandVector &Operands) {
1343 MCAsmParser &Parser = getParser();
1344 MCAsmLexer &Lexer = getLexer();
1345 while (true) {
1346 AsmToken const &Token = Parser.getTok();
1347 switch (Token.getKind()) {
1348 case AsmToken::EndOfStatement: {
1349 Lexer.Lex();
1350 return false;
1351 }
1352 case AsmToken::LCurly: {
1353 if (!Operands.empty())
1354 return true;
1355 Operands.push_back(
1356 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
1357 Lexer.Lex();
1358 return false;
1359 }
1360 case AsmToken::RCurly: {
1361 if (Operands.empty()) {
1362 Operands.push_back(
1363 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
1364 Lexer.Lex();
1365 }
1366 return false;
1367 }
1368 case AsmToken::Comma: {
1369 Lexer.Lex();
1370 continue;
1371 }
1372 case AsmToken::EqualEqual:
1373 case AsmToken::ExclaimEqual:
1374 case AsmToken::GreaterEqual:
1375 case AsmToken::GreaterGreater:
1376 case AsmToken::LessEqual:
1377 case AsmToken::LessLess: {
1378 Operands.push_back(HexagonOperand::CreateToken(
1379 Token.getString().substr(0, 1), Token.getLoc()));
1380 Operands.push_back(HexagonOperand::CreateToken(
1381 Token.getString().substr(1, 1), Token.getLoc()));
1382 Lexer.Lex();
1383 continue;
1384 }
1385 case AsmToken::Hash: {
1386 bool MustNotExtend = false;
1387 bool ImplicitExpression = implicitExpressionLocation(Operands);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001388 SMLoc ExprLoc = Lexer.getLoc();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001389 if (!ImplicitExpression)
1390 Operands.push_back(
1391 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
1392 Lexer.Lex();
1393 bool MustExtend = false;
1394 bool HiOnly = false;
1395 bool LoOnly = false;
1396 if (Lexer.is(AsmToken::Hash)) {
1397 Lexer.Lex();
1398 MustExtend = true;
1399 } else if (ImplicitExpression)
1400 MustNotExtend = true;
1401 AsmToken const &Token = Parser.getTok();
1402 if (Token.is(AsmToken::Identifier)) {
1403 StringRef String = Token.getString();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001404 if (String.lower() == "hi") {
1405 HiOnly = true;
1406 } else if (String.lower() == "lo") {
1407 LoOnly = true;
1408 }
1409 if (HiOnly || LoOnly) {
1410 AsmToken LParen = Lexer.peekTok();
1411 if (!LParen.is(AsmToken::LParen)) {
1412 HiOnly = false;
1413 LoOnly = false;
1414 } else {
1415 Lexer.Lex();
1416 }
1417 }
1418 }
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001419 MCExpr const *Expr = nullptr;
1420 if (parseExpression(Expr))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001421 return true;
1422 int64_t Value;
1423 MCContext &Context = Parser.getContext();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001424 assert(Expr != nullptr);
1425 if (Expr->evaluateAsAbsolute(Value)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001426 if (HiOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001427 Expr = MCBinaryExpr::createLShr(
1428 Expr, MCConstantExpr::create(16, Context), Context);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001429 if (HiOnly || LoOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001430 Expr = MCBinaryExpr::createAnd(Expr,
1431 MCConstantExpr::create(0xffff, Context),
1432 Context);
Colin LeMahieu5cb6eea2016-03-01 21:37:41 +00001433 } else {
1434 MCValue Value;
1435 if (Expr->evaluateAsRelocatable(Value, nullptr, nullptr)) {
1436 if (!Value.isAbsolute()) {
1437 switch(Value.getAccessVariant()) {
1438 case MCSymbolRefExpr::VariantKind::VK_TPREL:
1439 case MCSymbolRefExpr::VariantKind::VK_DTPREL:
1440 // Don't lazy extend these expression variants
1441 MustNotExtend = !MustExtend;
1442 break;
1443 default:
1444 break;
1445 }
1446 }
1447 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001448 }
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001449 Expr = HexagonMCExpr::create(Expr, Context);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001450 HexagonMCInstrInfo::setMustNotExtend(*Expr, MustNotExtend);
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001451 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001452 std::unique_ptr<HexagonOperand> Operand =
1453 HexagonOperand::CreateImm(Expr, ExprLoc, ExprLoc);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001454 Operands.push_back(std::move(Operand));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001455 continue;
1456 }
1457 default:
1458 break;
1459 }
1460 if (parseExpressionOrOperand(Operands))
1461 return true;
1462 }
1463}
1464
1465bool HexagonAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1466 StringRef Name,
1467 AsmToken ID,
1468 OperandVector &Operands) {
1469 getLexer().UnLex(ID);
1470 return parseInstruction(Operands);
1471}
1472
1473namespace {
1474MCInst makeCombineInst(int opCode, MCOperand &Rdd,
1475 MCOperand &MO1, MCOperand &MO2) {
1476 MCInst TmpInst;
1477 TmpInst.setOpcode(opCode);
1478 TmpInst.addOperand(Rdd);
1479 TmpInst.addOperand(MO1);
1480 TmpInst.addOperand(MO2);
1481
1482 return TmpInst;
1483}
1484}
1485
1486// Define this matcher function after the auto-generated include so we
1487// have the match class enum definitions.
1488unsigned HexagonAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
1489 unsigned Kind) {
1490 HexagonOperand *Op = static_cast<HexagonOperand *>(&AsmOp);
1491
1492 switch (Kind) {
1493 case MCK_0: {
1494 int64_t Value;
1495 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 0
1496 ? Match_Success
1497 : Match_InvalidOperand;
1498 }
1499 case MCK_1: {
1500 int64_t Value;
1501 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 1
1502 ? Match_Success
1503 : Match_InvalidOperand;
1504 }
1505 case MCK__MINUS_1: {
1506 int64_t Value;
1507 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == -1
1508 ? Match_Success
1509 : Match_InvalidOperand;
1510 }
1511 }
1512 if (Op->Kind == HexagonOperand::Token && Kind != InvalidMatchClass) {
1513 StringRef myStringRef = StringRef(Op->Tok.Data, Op->Tok.Length);
1514 if (matchTokenString(myStringRef.lower()) == (MatchClassKind)Kind)
1515 return Match_Success;
1516 if (matchTokenString(myStringRef.upper()) == (MatchClassKind)Kind)
1517 return Match_Success;
1518 }
1519
1520 DEBUG(dbgs() << "Unmatched Operand:");
1521 DEBUG(Op->dump());
1522 DEBUG(dbgs() << "\n");
1523
1524 return Match_InvalidOperand;
1525}
1526
1527void HexagonAsmParser::OutOfRange(SMLoc IDLoc, long long Val, long long Max) {
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001528 std::string errStr;
1529 raw_string_ostream ES(errStr);
Alexey Samsonov44ff2042015-12-02 22:59:22 +00001530 ES << "value " << Val << "(" << format_hex(Val, 0) << ") out of range: ";
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001531 if (Max >= 0)
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001532 ES << "0-" << Max;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001533 else
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001534 ES << Max << "-" << (-Max - 1);
1535 Error(IDLoc, ES.str().c_str());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001536}
1537
1538int HexagonAsmParser::processInstruction(MCInst &Inst,
1539 OperandVector const &Operands,
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001540 SMLoc IDLoc) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001541 MCContext &Context = getParser().getContext();
1542 const MCRegisterInfo *RI = getContext().getRegisterInfo();
1543 std::string r = "r";
1544 std::string v = "v";
1545 std::string Colon = ":";
1546
1547 bool is32bit = false; // used to distinguish between CONST32 and CONST64
1548 switch (Inst.getOpcode()) {
1549 default:
1550 break;
1551
Colin LeMahieuecef1d92016-02-16 20:38:17 +00001552 case Hexagon::A2_iconst: {
1553 Inst.setOpcode(Hexagon::A2_addi);
1554 MCOperand Reg = Inst.getOperand(0);
1555 MCOperand S16 = Inst.getOperand(1);
1556 HexagonMCInstrInfo::setMustNotExtend(*S16.getExpr());
1557 HexagonMCInstrInfo::setS23_2_reloc(*S16.getExpr());
1558 Inst.clear();
1559 Inst.addOperand(Reg);
1560 Inst.addOperand(MCOperand::createReg(Hexagon::R0));
1561 Inst.addOperand(S16);
1562 break;
1563 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001564 case Hexagon::M4_mpyrr_addr:
1565 case Hexagon::S4_addi_asl_ri:
1566 case Hexagon::S4_addi_lsr_ri:
1567 case Hexagon::S4_andi_asl_ri:
1568 case Hexagon::S4_andi_lsr_ri:
1569 case Hexagon::S4_ori_asl_ri:
1570 case Hexagon::S4_ori_lsr_ri:
1571 case Hexagon::S4_or_andix:
1572 case Hexagon::S4_subi_asl_ri:
1573 case Hexagon::S4_subi_lsr_ri: {
1574 MCOperand &Ry = Inst.getOperand(0);
1575 MCOperand &src = Inst.getOperand(2);
1576 if (RI->getEncodingValue(Ry.getReg()) != RI->getEncodingValue(src.getReg()))
1577 return Match_InvalidOperand;
1578 break;
1579 }
1580
1581 case Hexagon::C2_cmpgei: {
1582 MCOperand &MO = Inst.getOperand(2);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001583 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001584 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001585 Inst.setOpcode(Hexagon::C2_cmpgti);
1586 break;
1587 }
1588
1589 case Hexagon::C2_cmpgeui: {
1590 MCOperand &MO = Inst.getOperand(2);
1591 int64_t Value;
1592 bool Success = MO.getExpr()->evaluateAsAbsolute(Value);
Colin LeMahieu9d851f02015-11-09 21:06:28 +00001593 (void)Success;
1594 assert(Success && "Assured by matcher");
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001595 if (Value == 0) {
1596 MCInst TmpInst;
1597 MCOperand &Pd = Inst.getOperand(0);
1598 MCOperand &Rt = Inst.getOperand(1);
1599 TmpInst.setOpcode(Hexagon::C2_cmpeq);
1600 TmpInst.addOperand(Pd);
1601 TmpInst.addOperand(Rt);
1602 TmpInst.addOperand(Rt);
1603 Inst = TmpInst;
1604 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001605 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001606 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001607 Inst.setOpcode(Hexagon::C2_cmpgtui);
1608 }
1609 break;
1610 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001611
1612 // Translate a "$Rdd = $Rss" to "$Rdd = combine($Rs, $Rt)"
1613 case Hexagon::A2_tfrp: {
1614 MCOperand &MO = Inst.getOperand(1);
1615 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001616 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001617 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001618 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001619 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001620 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001621 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001622 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001623 Inst.setOpcode(Hexagon::A2_combinew);
1624 break;
1625 }
1626
1627 case Hexagon::A2_tfrpt:
1628 case Hexagon::A2_tfrpf: {
1629 MCOperand &MO = Inst.getOperand(2);
1630 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001631 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001632 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001633 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001634 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001635 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001636 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001637 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001638 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrpt)
1639 ? Hexagon::C2_ccombinewt
1640 : Hexagon::C2_ccombinewf);
1641 break;
1642 }
1643 case Hexagon::A2_tfrptnew:
1644 case Hexagon::A2_tfrpfnew: {
1645 MCOperand &MO = Inst.getOperand(2);
1646 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001647 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001648 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001649 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001650 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001651 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001652 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001653 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001654 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrptnew)
1655 ? Hexagon::C2_ccombinewnewt
1656 : Hexagon::C2_ccombinewnewf);
1657 break;
1658 }
1659
Krzysztof Parzyszek0e7d2d32016-04-28 16:43:16 +00001660 // Translate a "$Vdd = $Vss" to "$Vdd = vcombine($Vs, $Vt)"
1661 case Hexagon::HEXAGON_V6_vassignpair: {
1662 MCOperand &MO = Inst.getOperand(1);
1663 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
1664 std::string R1 = v + llvm::utostr(RegPairNum + 1);
1665 MO.setReg(MatchRegisterName(R1));
1666 // Add a new operand for the second register in the pair.
1667 std::string R2 = v + llvm::utostr(RegPairNum);
1668 Inst.addOperand(MCOperand::createReg(MatchRegisterName(R2)));
1669 Inst.setOpcode(Hexagon::V6_vcombine);
1670 break;
1671 }
1672
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001673 // Translate a "$Rx = CONST32(#imm)" to "$Rx = memw(gp+#LABEL) "
1674 case Hexagon::CONST32:
1675 case Hexagon::CONST32_Float_Real:
1676 case Hexagon::CONST32_Int_Real:
1677 case Hexagon::FCONST32_nsdata:
1678 is32bit = true;
1679 // Translate a "$Rx:y = CONST64(#imm)" to "$Rx:y = memd(gp+#LABEL) "
1680 case Hexagon::CONST64_Float_Real:
1681 case Hexagon::CONST64_Int_Real:
1682
1683 // FIXME: need better way to detect AsmStreamer (upstream removed getKind())
1684 if (!Parser.getStreamer().hasRawTextSupport()) {
1685 MCELFStreamer *MES = static_cast<MCELFStreamer *>(&Parser.getStreamer());
1686 MCOperand &MO_1 = Inst.getOperand(1);
1687 MCOperand &MO_0 = Inst.getOperand(0);
1688
1689 // push section onto section stack
1690 MES->PushSection();
1691
1692 std::string myCharStr;
1693 MCSectionELF *mySection;
1694
1695 // check if this as an immediate or a symbol
1696 int64_t Value;
1697 bool Absolute = MO_1.getExpr()->evaluateAsAbsolute(Value);
1698 if (Absolute) {
1699 // Create a new section - one for each constant
1700 // Some or all of the zeros are replaced with the given immediate.
1701 if (is32bit) {
1702 std::string myImmStr = utohexstr(static_cast<uint32_t>(Value));
1703 myCharStr = StringRef(".gnu.linkonce.l4.CONST_00000000")
1704 .drop_back(myImmStr.size())
1705 .str() +
1706 myImmStr;
1707 } else {
1708 std::string myImmStr = utohexstr(Value);
1709 myCharStr = StringRef(".gnu.linkonce.l8.CONST_0000000000000000")
1710 .drop_back(myImmStr.size())
1711 .str() +
1712 myImmStr;
1713 }
1714
1715 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1716 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1717 } else if (MO_1.isExpr()) {
1718 // .lita - for expressions
1719 myCharStr = ".lita";
1720 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1721 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1722 } else
1723 llvm_unreachable("unexpected type of machine operand!");
1724
1725 MES->SwitchSection(mySection);
1726 unsigned byteSize = is32bit ? 4 : 8;
1727 getStreamer().EmitCodeAlignment(byteSize, byteSize);
1728
1729 MCSymbol *Sym;
1730
1731 // for symbols, get rid of prepended ".gnu.linkonce.lx."
1732
1733 // emit symbol if needed
1734 if (Absolute) {
1735 Sym = getContext().getOrCreateSymbol(StringRef(myCharStr.c_str() + 16));
1736 if (Sym->isUndefined()) {
1737 getStreamer().EmitLabel(Sym);
1738 getStreamer().EmitSymbolAttribute(Sym, MCSA_Global);
1739 getStreamer().EmitIntValue(Value, byteSize);
1740 }
1741 } else if (MO_1.isExpr()) {
1742 const char *StringStart = 0;
1743 const char *StringEnd = 0;
1744 if (*Operands[4]->getStartLoc().getPointer() == '#') {
1745 StringStart = Operands[5]->getStartLoc().getPointer();
1746 StringEnd = Operands[6]->getStartLoc().getPointer();
1747 } else { // no pound
1748 StringStart = Operands[4]->getStartLoc().getPointer();
1749 StringEnd = Operands[5]->getStartLoc().getPointer();
1750 }
1751
1752 unsigned size = StringEnd - StringStart;
1753 std::string DotConst = ".CONST_";
1754 Sym = getContext().getOrCreateSymbol(DotConst +
1755 StringRef(StringStart, size));
1756
1757 if (Sym->isUndefined()) {
1758 // case where symbol is not yet defined: emit symbol
1759 getStreamer().EmitLabel(Sym);
1760 getStreamer().EmitSymbolAttribute(Sym, MCSA_Local);
1761 getStreamer().EmitValue(MO_1.getExpr(), 4);
1762 }
1763 } else
1764 llvm_unreachable("unexpected type of machine operand!");
1765
1766 MES->PopSection();
1767
1768 if (Sym) {
1769 MCInst TmpInst;
1770 if (is32bit) // 32 bit
1771 TmpInst.setOpcode(Hexagon::L2_loadrigp);
1772 else // 64 bit
1773 TmpInst.setOpcode(Hexagon::L2_loadrdgp);
1774
1775 TmpInst.addOperand(MO_0);
1776 TmpInst.addOperand(
1777 MCOperand::createExpr(MCSymbolRefExpr::create(Sym, getContext())));
1778 Inst = TmpInst;
1779 }
1780 }
1781 break;
1782
1783 // Translate a "$Rdd = #-imm" to "$Rdd = combine(#[-1,0], #-imm)"
1784 case Hexagon::A2_tfrpi: {
1785 MCOperand &Rdd = Inst.getOperand(0);
1786 MCOperand &MO = Inst.getOperand(1);
1787 int64_t Value;
1788 int sVal = (MO.getExpr()->evaluateAsAbsolute(Value) && Value < 0) ? -1 : 0;
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001789 MCOperand imm(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001790 HexagonMCExpr::create(MCConstantExpr::create(sVal, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001791 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, imm, MO);
1792 break;
1793 }
1794
1795 // Translate a "$Rdd = [#]#imm" to "$Rdd = combine(#, [#]#imm)"
1796 case Hexagon::TFRI64_V4: {
1797 MCOperand &Rdd = Inst.getOperand(0);
1798 MCOperand &MO = Inst.getOperand(1);
1799 int64_t Value;
1800 if (MO.getExpr()->evaluateAsAbsolute(Value)) {
1801 unsigned long long u64 = Value;
1802 signed int s8 = (u64 >> 32) & 0xFFFFFFFF;
1803 if (s8 < -128 || s8 > 127)
1804 OutOfRange(IDLoc, s8, -128);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001805 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001806 MCConstantExpr::create(s8, Context), Context))); // upper 32
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001807 auto Expr = HexagonMCExpr::create(
1808 MCConstantExpr::create(u64 & 0xFFFFFFFF, Context),
1809 Context);
1810 HexagonMCInstrInfo::setMustExtend(*Expr, HexagonMCInstrInfo::mustExtend(*MO.getExpr()));
1811 MCOperand imm2(MCOperand::createExpr(Expr)); // lower 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001812 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, imm2);
1813 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001814 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001815 MCConstantExpr::create(0, Context), Context))); // upper 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001816 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, MO);
1817 }
1818 break;
1819 }
1820
1821 // Handle $Rdd = combine(##imm, #imm)"
1822 case Hexagon::TFRI64_V2_ext: {
1823 MCOperand &Rdd = Inst.getOperand(0);
1824 MCOperand &MO1 = Inst.getOperand(1);
1825 MCOperand &MO2 = Inst.getOperand(2);
1826 int64_t Value;
1827 if (MO2.getExpr()->evaluateAsAbsolute(Value)) {
1828 int s8 = Value;
1829 if (s8 < -128 || s8 > 127)
1830 OutOfRange(IDLoc, s8, -128);
1831 }
1832 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, MO1, MO2);
1833 break;
1834 }
1835
1836 // Handle $Rdd = combine(#imm, ##imm)"
1837 case Hexagon::A4_combineii: {
1838 MCOperand &Rdd = Inst.getOperand(0);
1839 MCOperand &MO1 = Inst.getOperand(1);
1840 int64_t Value;
1841 if (MO1.getExpr()->evaluateAsAbsolute(Value)) {
1842 int s8 = Value;
1843 if (s8 < -128 || s8 > 127)
1844 OutOfRange(IDLoc, s8, -128);
1845 }
1846 MCOperand &MO2 = Inst.getOperand(2);
1847 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, MO1, MO2);
1848 break;
1849 }
1850
1851 case Hexagon::S2_tableidxb_goodsyntax: {
1852 Inst.setOpcode(Hexagon::S2_tableidxb);
1853 break;
1854 }
1855
1856 case Hexagon::S2_tableidxh_goodsyntax: {
1857 MCInst TmpInst;
1858 MCOperand &Rx = Inst.getOperand(0);
1859 MCOperand &_dst_ = Inst.getOperand(1);
1860 MCOperand &Rs = Inst.getOperand(2);
1861 MCOperand &Imm4 = Inst.getOperand(3);
1862 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001863 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001864 Imm6.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001865 TmpInst.setOpcode(Hexagon::S2_tableidxh);
1866 TmpInst.addOperand(Rx);
1867 TmpInst.addOperand(_dst_);
1868 TmpInst.addOperand(Rs);
1869 TmpInst.addOperand(Imm4);
1870 TmpInst.addOperand(Imm6);
1871 Inst = TmpInst;
1872 break;
1873 }
1874
1875 case Hexagon::S2_tableidxw_goodsyntax: {
1876 MCInst TmpInst;
1877 MCOperand &Rx = Inst.getOperand(0);
1878 MCOperand &_dst_ = Inst.getOperand(1);
1879 MCOperand &Rs = Inst.getOperand(2);
1880 MCOperand &Imm4 = Inst.getOperand(3);
1881 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001882 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001883 Imm6.getExpr(), MCConstantExpr::create(2, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001884 TmpInst.setOpcode(Hexagon::S2_tableidxw);
1885 TmpInst.addOperand(Rx);
1886 TmpInst.addOperand(_dst_);
1887 TmpInst.addOperand(Rs);
1888 TmpInst.addOperand(Imm4);
1889 TmpInst.addOperand(Imm6);
1890 Inst = TmpInst;
1891 break;
1892 }
1893
1894 case Hexagon::S2_tableidxd_goodsyntax: {
1895 MCInst TmpInst;
1896 MCOperand &Rx = Inst.getOperand(0);
1897 MCOperand &_dst_ = Inst.getOperand(1);
1898 MCOperand &Rs = Inst.getOperand(2);
1899 MCOperand &Imm4 = Inst.getOperand(3);
1900 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001901 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001902 Imm6.getExpr(), MCConstantExpr::create(3, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001903 TmpInst.setOpcode(Hexagon::S2_tableidxd);
1904 TmpInst.addOperand(Rx);
1905 TmpInst.addOperand(_dst_);
1906 TmpInst.addOperand(Rs);
1907 TmpInst.addOperand(Imm4);
1908 TmpInst.addOperand(Imm6);
1909 Inst = TmpInst;
1910 break;
1911 }
1912
1913 case Hexagon::M2_mpyui: {
1914 Inst.setOpcode(Hexagon::M2_mpyi);
1915 break;
1916 }
1917 case Hexagon::M2_mpysmi: {
1918 MCInst TmpInst;
1919 MCOperand &Rd = Inst.getOperand(0);
1920 MCOperand &Rs = Inst.getOperand(1);
1921 MCOperand &Imm = Inst.getOperand(2);
1922 int64_t Value;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001923 MCExpr const &Expr = *Imm.getExpr();
1924 bool Absolute = Expr.evaluateAsAbsolute(Value);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001925 assert(Absolute);
1926 (void)Absolute;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001927 if (!HexagonMCInstrInfo::mustExtend(Expr)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001928 if (Value < 0 && Value > -256) {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001929 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001930 MCConstantExpr::create(Value * -1, Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001931 TmpInst.setOpcode(Hexagon::M2_mpysin);
1932 } else if (Value < 256 && Value >= 0)
1933 TmpInst.setOpcode(Hexagon::M2_mpysip);
1934 else
1935 return Match_InvalidOperand;
1936 } else {
1937 if (Value >= 0)
1938 TmpInst.setOpcode(Hexagon::M2_mpysip);
1939 else
1940 return Match_InvalidOperand;
1941 }
1942 TmpInst.addOperand(Rd);
1943 TmpInst.addOperand(Rs);
1944 TmpInst.addOperand(Imm);
1945 Inst = TmpInst;
1946 break;
1947 }
1948
1949 case Hexagon::S2_asr_i_r_rnd_goodsyntax: {
1950 MCOperand &Imm = Inst.getOperand(2);
1951 MCInst TmpInst;
1952 int64_t Value;
1953 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1954 assert(Absolute);
1955 (void)Absolute;
1956 if (Value == 0) { // convert to $Rd = $Rs
1957 TmpInst.setOpcode(Hexagon::A2_tfr);
1958 MCOperand &Rd = Inst.getOperand(0);
1959 MCOperand &Rs = Inst.getOperand(1);
1960 TmpInst.addOperand(Rd);
1961 TmpInst.addOperand(Rs);
1962 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001963 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001964 MCBinaryExpr::createSub(Imm.getExpr(),
1965 MCConstantExpr::create(1, Context), Context),
1966 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001967 TmpInst.setOpcode(Hexagon::S2_asr_i_r_rnd);
1968 MCOperand &Rd = Inst.getOperand(0);
1969 MCOperand &Rs = Inst.getOperand(1);
1970 TmpInst.addOperand(Rd);
1971 TmpInst.addOperand(Rs);
1972 TmpInst.addOperand(Imm);
1973 }
1974 Inst = TmpInst;
1975 break;
1976 }
1977
1978 case Hexagon::S2_asr_i_p_rnd_goodsyntax: {
1979 MCOperand &Rdd = Inst.getOperand(0);
1980 MCOperand &Rss = Inst.getOperand(1);
1981 MCOperand &Imm = Inst.getOperand(2);
1982 int64_t Value;
1983 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1984 assert(Absolute);
1985 (void)Absolute;
1986 if (Value == 0) { // convert to $Rdd = combine ($Rs[0], $Rs[1])
1987 MCInst TmpInst;
1988 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001989 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001990 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001991 Rss.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001992 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001993 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001994 StringRef Reg2(R2);
1995 TmpInst.setOpcode(Hexagon::A2_combinew);
1996 TmpInst.addOperand(Rdd);
1997 TmpInst.addOperand(Rss);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001998 TmpInst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001999 Inst = TmpInst;
2000 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002001 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002002 MCBinaryExpr::createSub(Imm.getExpr(),
2003 MCConstantExpr::create(1, Context), Context),
2004 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002005 Inst.setOpcode(Hexagon::S2_asr_i_p_rnd);
2006 }
2007 break;
2008 }
2009
2010 case Hexagon::A4_boundscheck: {
2011 MCOperand &Rs = Inst.getOperand(1);
2012 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
2013 if (RegNum & 1) { // Odd mapped to raw:hi, regpair is rodd:odd-1, like r3:2
2014 Inst.setOpcode(Hexagon::A4_boundscheck_hi);
2015 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002016 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002017 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002018 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002019 } else { // raw:lo
2020 Inst.setOpcode(Hexagon::A4_boundscheck_lo);
2021 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002022 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002023 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002024 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002025 }
2026 break;
2027 }
2028
2029 case Hexagon::A2_addsp: {
2030 MCOperand &Rs = Inst.getOperand(1);
2031 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
2032 if (RegNum & 1) { // Odd mapped to raw:hi
2033 Inst.setOpcode(Hexagon::A2_addsph);
2034 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002035 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002036 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002037 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002038 } else { // Even mapped raw:lo
2039 Inst.setOpcode(Hexagon::A2_addspl);
2040 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002041 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002042 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002043 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002044 }
2045 break;
2046 }
2047
2048 case Hexagon::M2_vrcmpys_s1: {
2049 MCOperand &Rt = Inst.getOperand(2);
2050 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2051 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2052 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_h);
2053 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002054 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002055 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002056 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002057 } else { // Even mapped sat:raw:lo
2058 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_l);
2059 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002060 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002061 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002062 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002063 }
2064 break;
2065 }
2066
2067 case Hexagon::M2_vrcmpys_acc_s1: {
2068 MCInst TmpInst;
2069 MCOperand &Rxx = Inst.getOperand(0);
2070 MCOperand &Rss = Inst.getOperand(2);
2071 MCOperand &Rt = Inst.getOperand(3);
2072 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2073 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2074 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_h);
2075 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002076 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002077 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002078 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002079 } else { // Even mapped sat:raw:lo
2080 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_l);
2081 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002082 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002083 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002084 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002085 }
2086 // Registers are in different positions
2087 TmpInst.addOperand(Rxx);
2088 TmpInst.addOperand(Rxx);
2089 TmpInst.addOperand(Rss);
2090 TmpInst.addOperand(Rt);
2091 Inst = TmpInst;
2092 break;
2093 }
2094
2095 case Hexagon::M2_vrcmpys_s1rp: {
2096 MCOperand &Rt = Inst.getOperand(2);
2097 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2098 if (RegNum & 1) { // Odd mapped to rnd:sat:raw:hi
2099 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_h);
2100 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002101 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002102 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002103 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002104 } else { // Even mapped rnd:sat:raw:lo
2105 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_l);
2106 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002107 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002108 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002109 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002110 }
2111 break;
2112 }
2113
2114 case Hexagon::S5_asrhub_rnd_sat_goodsyntax: {
2115 MCOperand &Imm = Inst.getOperand(2);
2116 int64_t Value;
2117 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
2118 assert(Absolute);
2119 (void)Absolute;
2120 if (Value == 0)
2121 Inst.setOpcode(Hexagon::S2_vsathub);
2122 else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002123 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002124 MCBinaryExpr::createSub(Imm.getExpr(),
2125 MCConstantExpr::create(1, Context), Context),
2126 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002127 Inst.setOpcode(Hexagon::S5_asrhub_rnd_sat);
2128 }
2129 break;
2130 }
2131
2132 case Hexagon::S5_vasrhrnd_goodsyntax: {
2133 MCOperand &Rdd = Inst.getOperand(0);
2134 MCOperand &Rss = Inst.getOperand(1);
2135 MCOperand &Imm = Inst.getOperand(2);
2136 int64_t Value;
2137 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
2138 assert(Absolute);
2139 (void)Absolute;
2140 if (Value == 0) {
2141 MCInst TmpInst;
2142 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00002143 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002144 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002145 Rss.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002146 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00002147 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002148 StringRef Reg2(R2);
2149 TmpInst.setOpcode(Hexagon::A2_combinew);
2150 TmpInst.addOperand(Rdd);
2151 TmpInst.addOperand(Rss);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002152 TmpInst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002153 Inst = TmpInst;
2154 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002155 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002156 MCBinaryExpr::createSub(Imm.getExpr(),
2157 MCConstantExpr::create(1, Context), Context),
2158 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002159 Inst.setOpcode(Hexagon::S5_vasrhrnd);
2160 }
2161 break;
2162 }
2163
2164 case Hexagon::A2_not: {
2165 MCInst TmpInst;
2166 MCOperand &Rd = Inst.getOperand(0);
2167 MCOperand &Rs = Inst.getOperand(1);
2168 TmpInst.setOpcode(Hexagon::A2_subri);
2169 TmpInst.addOperand(Rd);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002170 TmpInst.addOperand(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002171 HexagonMCExpr::create(MCConstantExpr::create(-1, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002172 TmpInst.addOperand(Rs);
2173 Inst = TmpInst;
2174 break;
2175 }
2176 } // switch
2177
2178 return Match_Success;
2179}
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002180
2181
2182unsigned HexagonAsmParser::matchRegister(StringRef Name) {
2183 if (unsigned Reg = MatchRegisterName(Name))
2184 return Reg;
2185 return MatchRegisterAltName(Name);
2186}