blob: 06a8b96bd2c5530c1b61ff45b32cd891c6c374be [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
127/// @name Auto-generated Match Functions
128/// {
129
130#define GET_ASSEMBLER_HEADER
131#include "HexagonGenAsmMatcher.inc"
132
133 /// }
134
135public:
Akira Hatanakab11ef082015-11-14 06:35:56 +0000136 HexagonAsmParser(const MCSubtargetInfo &_STI, MCAsmParser &_Parser,
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000137 const MCInstrInfo &MII, const MCTargetOptions &Options)
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000138 : MCTargetAsmParser(Options, _STI), Parser(_Parser),
Colin LeMahieuf0af6e52015-11-13 17:42:46 +0000139 MCII (MII), MCB(HexagonMCInstrInfo::createBundle()), InBrackets(false) {
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000140 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000141
142 MCAsmParserExtension::Initialize(_Parser);
143
144 Assembler = nullptr;
145 // FIXME: need better way to detect AsmStreamer (upstream removed getKind())
146 if (!Parser.getStreamer().hasRawTextSupport()) {
147 MCELFStreamer *MES = static_cast<MCELFStreamer *>(&Parser.getStreamer());
148 Assembler = &MES->getAssembler();
149 }
150 }
151
152 bool mustExtend(OperandVector &Operands);
153 bool splitIdentifier(OperandVector &Operands);
154 bool parseOperand(OperandVector &Operands);
155 bool parseInstruction(OperandVector &Operands);
156 bool implicitExpressionLocation(OperandVector &Operands);
157 bool parseExpressionOrOperand(OperandVector &Operands);
158 bool parseExpression(MCExpr const *& Expr);
159 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
Colin LeMahieu9ea507e2015-11-09 07:10:24 +0000160 SMLoc NameLoc, OperandVector &Operands) override
161 {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000162 llvm_unreachable("Unimplemented");
163 }
164 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
Colin LeMahieu9ea507e2015-11-09 07:10:24 +0000165 AsmToken ID, OperandVector &Operands) override;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000166
Colin LeMahieu9ea507e2015-11-09 07:10:24 +0000167 virtual bool ParseDirective(AsmToken DirectiveID) override;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000168};
169
170/// HexagonOperand - Instances of this class represent a parsed Hexagon machine
171/// instruction.
172struct HexagonOperand : public MCParsedAsmOperand {
173 enum KindTy { Token, Immediate, Register } Kind;
174
175 SMLoc StartLoc, EndLoc;
176
177 struct TokTy {
178 const char *Data;
179 unsigned Length;
180 };
181
182 struct RegTy {
183 unsigned RegNum;
184 };
185
186 struct ImmTy {
187 const MCExpr *Val;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000188 };
189
190 struct InstTy {
191 OperandVector *SubInsts;
192 };
193
194 union {
195 struct TokTy Tok;
196 struct RegTy Reg;
197 struct ImmTy Imm;
198 };
199
200 HexagonOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
201
202public:
203 HexagonOperand(const HexagonOperand &o) : MCParsedAsmOperand() {
204 Kind = o.Kind;
205 StartLoc = o.StartLoc;
206 EndLoc = o.EndLoc;
207 switch (Kind) {
208 case Register:
209 Reg = o.Reg;
210 break;
211 case Immediate:
212 Imm = o.Imm;
213 break;
214 case Token:
215 Tok = o.Tok;
216 break;
217 }
218 }
219
220 /// getStartLoc - Get the location of the first token of this operand.
221 SMLoc getStartLoc() const { return StartLoc; }
222
223 /// getEndLoc - Get the location of the last token of this operand.
224 SMLoc getEndLoc() const { return EndLoc; }
225
226 unsigned getReg() const {
227 assert(Kind == Register && "Invalid access!");
228 return Reg.RegNum;
229 }
230
231 const MCExpr *getImm() const {
232 assert(Kind == Immediate && "Invalid access!");
233 return Imm.Val;
234 }
235
236 bool isToken() const { return Kind == Token; }
237 bool isImm() const { return Kind == Immediate; }
238 bool isMem() const { llvm_unreachable("No isMem"); }
239 bool isReg() const { return Kind == Register; }
240
241 bool CheckImmRange(int immBits, int zeroBits, bool isSigned,
242 bool isRelocatable, bool Extendable) const {
243 if (Kind == Immediate) {
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000244 const MCExpr *myMCExpr = &HexagonMCInstrInfo::getExpr(*getImm());
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000245 if (HexagonMCInstrInfo::mustExtend(*Imm.Val) && !Extendable)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000246 return false;
247 int64_t Res;
248 if (myMCExpr->evaluateAsAbsolute(Res)) {
249 int bits = immBits + zeroBits;
250 // Field bit range is zerobits + bits
251 // zeroBits must be 0
252 if (Res & ((1 << zeroBits) - 1))
253 return false;
254 if (isSigned) {
255 if (Res < (1LL << (bits - 1)) && Res >= -(1LL << (bits - 1)))
256 return true;
257 } else {
258 if (bits == 64)
259 return true;
260 if (Res >= 0)
261 return ((uint64_t)Res < (uint64_t)(1ULL << bits)) ? true : false;
262 else {
263 const int64_t high_bit_set = 1ULL << 63;
264 const uint64_t mask = (high_bit_set >> (63 - bits));
265 return (((uint64_t)Res & mask) == mask) ? true : false;
266 }
267 }
268 } else if (myMCExpr->getKind() == MCExpr::SymbolRef && isRelocatable)
269 return true;
270 else if (myMCExpr->getKind() == MCExpr::Binary ||
271 myMCExpr->getKind() == MCExpr::Unary)
272 return true;
273 }
274 return false;
275 }
276
277 bool isf32Ext() const { return false; }
278 bool iss32Imm() const { return CheckImmRange(32, 0, true, true, false); }
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000279 bool iss23_2Imm() const { return CheckImmRange(23, 2, true, true, false); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000280 bool iss8Imm() const { return CheckImmRange(8, 0, true, false, false); }
281 bool iss8Imm64() const { return CheckImmRange(8, 0, true, true, false); }
282 bool iss7Imm() const { return CheckImmRange(7, 0, true, false, false); }
283 bool iss6Imm() const { return CheckImmRange(6, 0, true, false, false); }
284 bool iss4Imm() const { return CheckImmRange(4, 0, true, false, false); }
285 bool iss4_0Imm() const { return CheckImmRange(4, 0, true, false, false); }
286 bool iss4_1Imm() const { return CheckImmRange(4, 1, true, false, false); }
287 bool iss4_2Imm() const { return CheckImmRange(4, 2, true, false, false); }
288 bool iss4_3Imm() const { return CheckImmRange(4, 3, true, false, false); }
289 bool iss4_6Imm() const { return CheckImmRange(4, 0, true, false, false); }
290 bool iss3_6Imm() const { return CheckImmRange(3, 0, true, false, false); }
291 bool iss3Imm() const { return CheckImmRange(3, 0, true, false, false); }
292
293 bool isu64Imm() const { return CheckImmRange(64, 0, false, true, true); }
294 bool isu32Imm() const { return CheckImmRange(32, 0, false, true, false); }
295 bool isu26_6Imm() const { return CheckImmRange(26, 6, false, true, false); }
296 bool isu16Imm() const { return CheckImmRange(16, 0, false, true, false); }
297 bool isu16_0Imm() const { return CheckImmRange(16, 0, false, true, false); }
298 bool isu16_1Imm() const { return CheckImmRange(16, 1, false, true, false); }
299 bool isu16_2Imm() const { return CheckImmRange(16, 2, false, true, false); }
300 bool isu16_3Imm() const { return CheckImmRange(16, 3, false, true, false); }
301 bool isu11_3Imm() const { return CheckImmRange(11, 3, false, false, false); }
302 bool isu6_0Imm() const { return CheckImmRange(6, 0, false, false, false); }
303 bool isu6_1Imm() const { return CheckImmRange(6, 1, false, false, false); }
304 bool isu6_2Imm() const { return CheckImmRange(6, 2, false, false, false); }
305 bool isu6_3Imm() const { return CheckImmRange(6, 3, false, false, false); }
306 bool isu10Imm() const { return CheckImmRange(10, 0, false, false, false); }
307 bool isu9Imm() const { return CheckImmRange(9, 0, false, false, false); }
308 bool isu8Imm() const { return CheckImmRange(8, 0, false, false, false); }
309 bool isu7Imm() const { return CheckImmRange(7, 0, false, false, false); }
310 bool isu6Imm() const { return CheckImmRange(6, 0, false, false, false); }
311 bool isu5Imm() const { return CheckImmRange(5, 0, false, false, false); }
312 bool isu4Imm() const { return CheckImmRange(4, 0, false, false, false); }
313 bool isu3Imm() const { return CheckImmRange(3, 0, false, false, false); }
314 bool isu2Imm() const { return CheckImmRange(2, 0, false, false, false); }
315 bool isu1Imm() const { return CheckImmRange(1, 0, false, false, false); }
316
317 bool ism6Imm() const { return CheckImmRange(6, 0, false, false, false); }
318 bool isn8Imm() const { return CheckImmRange(8, 0, false, false, false); }
319
320 bool iss16Ext() const { return CheckImmRange(16 + 26, 0, true, true, true); }
321 bool iss12Ext() const { return CheckImmRange(12 + 26, 0, true, true, true); }
322 bool iss10Ext() const { return CheckImmRange(10 + 26, 0, true, true, true); }
323 bool iss9Ext() const { return CheckImmRange(9 + 26, 0, true, true, true); }
324 bool iss8Ext() const { return CheckImmRange(8 + 26, 0, true, true, true); }
325 bool iss7Ext() const { return CheckImmRange(7 + 26, 0, true, true, true); }
326 bool iss6Ext() const { return CheckImmRange(6 + 26, 0, true, true, true); }
327 bool iss11_0Ext() const {
328 return CheckImmRange(11 + 26, 0, true, true, true);
329 }
330 bool iss11_1Ext() const {
331 return CheckImmRange(11 + 26, 1, true, true, true);
332 }
333 bool iss11_2Ext() const {
334 return CheckImmRange(11 + 26, 2, true, true, true);
335 }
336 bool iss11_3Ext() const {
337 return CheckImmRange(11 + 26, 3, true, true, true);
338 }
339
340 bool isu6Ext() const { return CheckImmRange(6 + 26, 0, false, true, true); }
341 bool isu7Ext() const { return CheckImmRange(7 + 26, 0, false, true, true); }
342 bool isu8Ext() const { return CheckImmRange(8 + 26, 0, false, true, true); }
343 bool isu9Ext() const { return CheckImmRange(9 + 26, 0, false, true, true); }
344 bool isu10Ext() const { return CheckImmRange(10 + 26, 0, false, true, true); }
345 bool isu6_0Ext() const { return CheckImmRange(6 + 26, 0, false, true, true); }
346 bool isu6_1Ext() const { return CheckImmRange(6 + 26, 1, false, true, true); }
347 bool isu6_2Ext() const { return CheckImmRange(6 + 26, 2, false, true, true); }
348 bool isu6_3Ext() const { return CheckImmRange(6 + 26, 3, false, true, true); }
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000349 bool isu32MustExt() const { return isImm(); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000350
351 void addRegOperands(MCInst &Inst, unsigned N) const {
352 assert(N == 1 && "Invalid number of operands!");
353 Inst.addOperand(MCOperand::createReg(getReg()));
354 }
355
356 void addImmOperands(MCInst &Inst, unsigned N) const {
357 assert(N == 1 && "Invalid number of operands!");
358 Inst.addOperand(MCOperand::createExpr(getImm()));
359 }
360
361 void addSignedImmOperands(MCInst &Inst, unsigned N) const {
362 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000363 HexagonMCExpr *Expr =
364 const_cast<HexagonMCExpr *>(cast<HexagonMCExpr>(getImm()));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000365 int64_t Value;
366 if (!Expr->evaluateAsAbsolute(Value)) {
367 Inst.addOperand(MCOperand::createExpr(Expr));
368 return;
369 }
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000370 int64_t Extended = SignExtend64(Value, 32);
371 if ((Extended < 0) != (Value < 0))
372 Expr->setSignMismatch();
373 Inst.addOperand(MCOperand::createExpr(Expr));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000374 }
375
376 void addf32ExtOperands(MCInst &Inst, unsigned N) const {
377 addImmOperands(Inst, N);
378 }
379
380 void adds32ImmOperands(MCInst &Inst, unsigned N) const {
381 addSignedImmOperands(Inst, N);
382 }
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000383 void adds23_2ImmOperands(MCInst &Inst, unsigned N) const {
384 addSignedImmOperands(Inst, N);
385 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000386 void adds8ImmOperands(MCInst &Inst, unsigned N) const {
387 addSignedImmOperands(Inst, N);
388 }
389 void adds8Imm64Operands(MCInst &Inst, unsigned N) const {
390 addSignedImmOperands(Inst, N);
391 }
392 void adds6ImmOperands(MCInst &Inst, unsigned N) const {
393 addSignedImmOperands(Inst, N);
394 }
395 void adds4ImmOperands(MCInst &Inst, unsigned N) const {
396 addSignedImmOperands(Inst, N);
397 }
398 void adds4_0ImmOperands(MCInst &Inst, unsigned N) const {
399 addSignedImmOperands(Inst, N);
400 }
401 void adds4_1ImmOperands(MCInst &Inst, unsigned N) const {
402 addSignedImmOperands(Inst, N);
403 }
404 void adds4_2ImmOperands(MCInst &Inst, unsigned N) const {
405 addSignedImmOperands(Inst, N);
406 }
407 void adds4_3ImmOperands(MCInst &Inst, unsigned N) const {
408 addSignedImmOperands(Inst, N);
409 }
410 void adds3ImmOperands(MCInst &Inst, unsigned N) const {
411 addSignedImmOperands(Inst, N);
412 }
413
414 void addu64ImmOperands(MCInst &Inst, unsigned N) const {
415 addImmOperands(Inst, N);
416 }
417 void addu32ImmOperands(MCInst &Inst, unsigned N) const {
418 addImmOperands(Inst, N);
419 }
420 void addu26_6ImmOperands(MCInst &Inst, unsigned N) const {
421 addImmOperands(Inst, N);
422 }
423 void addu16ImmOperands(MCInst &Inst, unsigned N) const {
424 addImmOperands(Inst, N);
425 }
426 void addu16_0ImmOperands(MCInst &Inst, unsigned N) const {
427 addImmOperands(Inst, N);
428 }
429 void addu16_1ImmOperands(MCInst &Inst, unsigned N) const {
430 addImmOperands(Inst, N);
431 }
432 void addu16_2ImmOperands(MCInst &Inst, unsigned N) const {
433 addImmOperands(Inst, N);
434 }
435 void addu16_3ImmOperands(MCInst &Inst, unsigned N) const {
436 addImmOperands(Inst, N);
437 }
438 void addu11_3ImmOperands(MCInst &Inst, unsigned N) const {
439 addImmOperands(Inst, N);
440 }
441 void addu10ImmOperands(MCInst &Inst, unsigned N) const {
442 addImmOperands(Inst, N);
443 }
444 void addu9ImmOperands(MCInst &Inst, unsigned N) const {
445 addImmOperands(Inst, N);
446 }
447 void addu8ImmOperands(MCInst &Inst, unsigned N) const {
448 addImmOperands(Inst, N);
449 }
450 void addu7ImmOperands(MCInst &Inst, unsigned N) const {
451 addImmOperands(Inst, N);
452 }
453 void addu6ImmOperands(MCInst &Inst, unsigned N) const {
454 addImmOperands(Inst, N);
455 }
456 void addu6_0ImmOperands(MCInst &Inst, unsigned N) const {
457 addImmOperands(Inst, N);
458 }
459 void addu6_1ImmOperands(MCInst &Inst, unsigned N) const {
460 addImmOperands(Inst, N);
461 }
462 void addu6_2ImmOperands(MCInst &Inst, unsigned N) const {
463 addImmOperands(Inst, N);
464 }
465 void addu6_3ImmOperands(MCInst &Inst, unsigned N) const {
466 addImmOperands(Inst, N);
467 }
468 void addu5ImmOperands(MCInst &Inst, unsigned N) const {
469 addImmOperands(Inst, N);
470 }
471 void addu4ImmOperands(MCInst &Inst, unsigned N) const {
472 addImmOperands(Inst, N);
473 }
474 void addu3ImmOperands(MCInst &Inst, unsigned N) const {
475 addImmOperands(Inst, N);
476 }
477 void addu2ImmOperands(MCInst &Inst, unsigned N) const {
478 addImmOperands(Inst, N);
479 }
480 void addu1ImmOperands(MCInst &Inst, unsigned N) const {
481 addImmOperands(Inst, N);
482 }
483
484 void addm6ImmOperands(MCInst &Inst, unsigned N) const {
485 addImmOperands(Inst, N);
486 }
487 void addn8ImmOperands(MCInst &Inst, unsigned N) const {
488 addImmOperands(Inst, N);
489 }
490
491 void adds16ExtOperands(MCInst &Inst, unsigned N) const {
492 addSignedImmOperands(Inst, N);
493 }
494 void adds12ExtOperands(MCInst &Inst, unsigned N) const {
495 addSignedImmOperands(Inst, N);
496 }
497 void adds10ExtOperands(MCInst &Inst, unsigned N) const {
498 addSignedImmOperands(Inst, N);
499 }
500 void adds9ExtOperands(MCInst &Inst, unsigned N) const {
501 addSignedImmOperands(Inst, N);
502 }
503 void adds8ExtOperands(MCInst &Inst, unsigned N) const {
504 addSignedImmOperands(Inst, N);
505 }
506 void adds6ExtOperands(MCInst &Inst, unsigned N) const {
507 addSignedImmOperands(Inst, N);
508 }
509 void adds11_0ExtOperands(MCInst &Inst, unsigned N) const {
510 addSignedImmOperands(Inst, N);
511 }
512 void adds11_1ExtOperands(MCInst &Inst, unsigned N) const {
513 addSignedImmOperands(Inst, N);
514 }
515 void adds11_2ExtOperands(MCInst &Inst, unsigned N) const {
516 addSignedImmOperands(Inst, N);
517 }
518 void adds11_3ExtOperands(MCInst &Inst, unsigned N) const {
519 addSignedImmOperands(Inst, N);
520 }
521
522 void addu6ExtOperands(MCInst &Inst, unsigned N) const {
523 addImmOperands(Inst, N);
524 }
525 void addu7ExtOperands(MCInst &Inst, unsigned N) const {
526 addImmOperands(Inst, N);
527 }
528 void addu8ExtOperands(MCInst &Inst, unsigned N) const {
529 addImmOperands(Inst, N);
530 }
531 void addu9ExtOperands(MCInst &Inst, unsigned N) const {
532 addImmOperands(Inst, N);
533 }
534 void addu10ExtOperands(MCInst &Inst, unsigned N) const {
535 addImmOperands(Inst, N);
536 }
537 void addu6_0ExtOperands(MCInst &Inst, unsigned N) const {
538 addImmOperands(Inst, N);
539 }
540 void addu6_1ExtOperands(MCInst &Inst, unsigned N) const {
541 addImmOperands(Inst, N);
542 }
543 void addu6_2ExtOperands(MCInst &Inst, unsigned N) const {
544 addImmOperands(Inst, N);
545 }
546 void addu6_3ExtOperands(MCInst &Inst, unsigned N) const {
547 addImmOperands(Inst, N);
548 }
549 void addu32MustExtOperands(MCInst &Inst, unsigned N) const {
550 addImmOperands(Inst, N);
551 }
552
553 void adds4_6ImmOperands(MCInst &Inst, unsigned N) const {
554 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000555 const MCConstantExpr *CE =
556 dyn_cast<MCConstantExpr>(&HexagonMCInstrInfo::getExpr(*getImm()));
Colin LeMahieu4c606e62015-12-04 15:48:45 +0000557 Inst.addOperand(MCOperand::createImm(CE->getValue() * 64));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000558 }
559
560 void adds3_6ImmOperands(MCInst &Inst, unsigned N) const {
561 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000562 const MCConstantExpr *CE =
563 dyn_cast<MCConstantExpr>(&HexagonMCInstrInfo::getExpr(*getImm()));
Colin LeMahieu4c606e62015-12-04 15:48:45 +0000564 Inst.addOperand(MCOperand::createImm(CE->getValue() * 64));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000565 }
566
567 StringRef getToken() const {
568 assert(Kind == Token && "Invalid access!");
569 return StringRef(Tok.Data, Tok.Length);
570 }
571
572 virtual void print(raw_ostream &OS) const;
573
574 static std::unique_ptr<HexagonOperand> CreateToken(StringRef Str, SMLoc S) {
575 HexagonOperand *Op = new HexagonOperand(Token);
576 Op->Tok.Data = Str.data();
577 Op->Tok.Length = Str.size();
578 Op->StartLoc = S;
579 Op->EndLoc = S;
580 return std::unique_ptr<HexagonOperand>(Op);
581 }
582
583 static std::unique_ptr<HexagonOperand> CreateReg(unsigned RegNum, SMLoc S,
584 SMLoc E) {
585 HexagonOperand *Op = new HexagonOperand(Register);
586 Op->Reg.RegNum = RegNum;
587 Op->StartLoc = S;
588 Op->EndLoc = E;
589 return std::unique_ptr<HexagonOperand>(Op);
590 }
591
592 static std::unique_ptr<HexagonOperand> CreateImm(const MCExpr *Val, SMLoc S,
593 SMLoc E) {
594 HexagonOperand *Op = new HexagonOperand(Immediate);
595 Op->Imm.Val = Val;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000596 Op->StartLoc = S;
597 Op->EndLoc = E;
598 return std::unique_ptr<HexagonOperand>(Op);
599 }
600};
601
602} // end anonymous namespace.
603
604void HexagonOperand::print(raw_ostream &OS) const {
605 switch (Kind) {
606 case Immediate:
607 getImm()->print(OS, nullptr);
608 break;
609 case Register:
610 OS << "<register R";
611 OS << getReg() << ">";
612 break;
613 case Token:
614 OS << "'" << getToken() << "'";
615 break;
616 }
617}
618
619/// @name Auto-generated Match Functions
620static unsigned MatchRegisterName(StringRef Name);
621
622bool HexagonAsmParser::finishBundle(SMLoc IDLoc, MCStreamer &Out) {
623 DEBUG(dbgs() << "Bundle:");
624 DEBUG(MCB.dump_pretty(dbgs()));
625 DEBUG(dbgs() << "--\n");
626
627 // Check the bundle for errors.
628 const MCRegisterInfo *RI = getContext().getRegisterInfo();
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000629 HexagonMCChecker Check(MCII, getSTI(), MCB, MCB, *RI);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000630
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000631 bool CheckOk = HexagonMCInstrInfo::canonicalizePacket(MCII, getSTI(),
632 getContext(), MCB,
633 &Check);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000634
635 while (Check.getNextErrInfo() == true) {
636 unsigned Reg = Check.getErrRegister();
637 Twine R(RI->getName(Reg));
638
639 uint64_t Err = Check.getError();
640 if (Err != HexagonMCErrInfo::CHECK_SUCCESS) {
641 if (HexagonMCErrInfo::CHECK_ERROR_BRANCHES & Err)
642 Error(IDLoc,
643 "unconditional branch cannot precede another branch in packet");
644
645 if (HexagonMCErrInfo::CHECK_ERROR_NEWP & Err ||
646 HexagonMCErrInfo::CHECK_ERROR_NEWV & Err)
647 Error(IDLoc, "register `" + R +
648 "' used with `.new' "
649 "but not validly modified in the same packet");
650
651 if (HexagonMCErrInfo::CHECK_ERROR_REGISTERS & Err)
652 Error(IDLoc, "register `" + R + "' modified more than once");
653
654 if (HexagonMCErrInfo::CHECK_ERROR_READONLY & Err)
655 Error(IDLoc, "cannot write to read-only register `" + R + "'");
656
657 if (HexagonMCErrInfo::CHECK_ERROR_LOOP & Err)
658 Error(IDLoc, "loop-setup and some branch instructions "
659 "cannot be in the same packet");
660
661 if (HexagonMCErrInfo::CHECK_ERROR_ENDLOOP & Err) {
662 Twine N(HexagonMCInstrInfo::isInnerLoop(MCB) ? '0' : '1');
663 Error(IDLoc, "packet marked with `:endloop" + N + "' " +
664 "cannot contain instructions that modify register " +
665 "`" + R + "'");
666 }
667
668 if (HexagonMCErrInfo::CHECK_ERROR_SOLO & Err)
669 Error(IDLoc,
670 "instruction cannot appear in packet with other instructions");
671
672 if (HexagonMCErrInfo::CHECK_ERROR_NOSLOTS & Err)
673 Error(IDLoc, "too many slots used in packet");
674
675 if (Err & HexagonMCErrInfo::CHECK_ERROR_SHUFFLE) {
676 uint64_t Erm = Check.getShuffleError();
677
678 if (HexagonShuffler::SHUFFLE_ERROR_INVALID == Erm)
679 Error(IDLoc, "invalid instruction packet");
680 else if (HexagonShuffler::SHUFFLE_ERROR_STORES == Erm)
681 Error(IDLoc, "invalid instruction packet: too many stores");
682 else if (HexagonShuffler::SHUFFLE_ERROR_LOADS == Erm)
683 Error(IDLoc, "invalid instruction packet: too many loads");
684 else if (HexagonShuffler::SHUFFLE_ERROR_BRANCHES == Erm)
685 Error(IDLoc, "too many branches in packet");
686 else if (HexagonShuffler::SHUFFLE_ERROR_NOSLOTS == Erm)
687 Error(IDLoc, "invalid instruction packet: out of slots");
688 else if (HexagonShuffler::SHUFFLE_ERROR_SLOTS == Erm)
689 Error(IDLoc, "invalid instruction packet: slot error");
690 else if (HexagonShuffler::SHUFFLE_ERROR_ERRATA2 == Erm)
691 Error(IDLoc, "v60 packet violation");
692 else if (HexagonShuffler::SHUFFLE_ERROR_STORE_LOAD_CONFLICT == Erm)
693 Error(IDLoc, "slot 0 instruction does not allow slot 1 store");
694 else
695 Error(IDLoc, "unknown error in instruction packet");
696 }
697 }
698
699 unsigned Warn = Check.getWarning();
700 if (Warn != HexagonMCErrInfo::CHECK_SUCCESS) {
701 if (HexagonMCErrInfo::CHECK_WARN_CURRENT & Warn)
702 Warning(IDLoc, "register `" + R + "' used with `.cur' "
703 "but not used in the same packet");
704 else if (HexagonMCErrInfo::CHECK_WARN_TEMPORARY & Warn)
705 Warning(IDLoc, "register `" + R + "' used with `.tmp' "
706 "but not used in the same packet");
707 }
708 }
709
710 if (CheckOk) {
711 MCB.setLoc(IDLoc);
712 if (HexagonMCInstrInfo::bundleSize(MCB) == 0) {
713 assert(!HexagonMCInstrInfo::isInnerLoop(MCB));
714 assert(!HexagonMCInstrInfo::isOuterLoop(MCB));
715 // Empty packets are valid yet aren't emitted
716 return false;
717 }
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000718 Out.EmitInstruction(MCB, getSTI());
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000719 } else {
720 // If compounding and duplexing didn't reduce the size below
721 // 4 or less we have a packet that is too big.
722 if (HexagonMCInstrInfo::bundleSize(MCB) > HEXAGON_PACKET_SIZE) {
723 Error(IDLoc, "invalid instruction packet: out of slots");
724 return true; // Error
725 }
726 }
727
728 return false; // No error
729}
730
731bool HexagonAsmParser::matchBundleOptions() {
732 MCAsmParser &Parser = getParser();
733 MCAsmLexer &Lexer = getLexer();
734 while (true) {
735 if (!Parser.getTok().is(AsmToken::Colon))
736 return false;
737 Lexer.Lex();
738 StringRef Option = Parser.getTok().getString();
739 if (Option.compare_lower("endloop0") == 0)
740 HexagonMCInstrInfo::setInnerLoop(MCB);
741 else if (Option.compare_lower("endloop1") == 0)
742 HexagonMCInstrInfo::setOuterLoop(MCB);
743 else if (Option.compare_lower("mem_noshuf") == 0)
744 HexagonMCInstrInfo::setMemReorderDisabled(MCB);
745 else if (Option.compare_lower("mem_shuf") == 0)
746 HexagonMCInstrInfo::setMemStoreReorderEnabled(MCB);
747 else
748 return true;
749 Lexer.Lex();
750 }
751}
752
753// For instruction aliases, immediates are generated rather than
754// MCConstantExpr. Convert them for uniform MCExpr.
755// Also check for signed/unsigned mismatches and warn
756void HexagonAsmParser::canonicalizeImmediates(MCInst &MCI) {
757 MCInst NewInst;
758 NewInst.setOpcode(MCI.getOpcode());
759 for (MCOperand &I : MCI)
760 if (I.isImm()) {
761 int64_t Value (I.getImm());
Colin LeMahieuc7b21242016-02-15 18:47:55 +0000762 NewInst.addOperand(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000763 MCConstantExpr::create(Value, getContext()), getContext())));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000764 }
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000765 else {
766 if (I.isExpr() && cast<HexagonMCExpr>(I.getExpr())->signMismatch() &&
767 WarnSignedMismatch)
768 Warning (MCI.getLoc(), "Signed/Unsigned mismatch");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000769 NewInst.addOperand(I);
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000770 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000771 MCI = NewInst;
772}
773
774bool HexagonAsmParser::matchOneInstruction(MCInst &MCI, SMLoc IDLoc,
775 OperandVector &InstOperands,
776 uint64_t &ErrorInfo,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000777 bool MatchingInlineAsm) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000778 // Perform matching with tablegen asmmatcher generated function
779 int result =
780 MatchInstructionImpl(InstOperands, MCI, ErrorInfo, MatchingInlineAsm);
781 if (result == Match_Success) {
782 MCI.setLoc(IDLoc);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000783 canonicalizeImmediates(MCI);
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000784 result = processInstruction(MCI, InstOperands, IDLoc);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000785
786 DEBUG(dbgs() << "Insn:");
787 DEBUG(MCI.dump_pretty(dbgs()));
788 DEBUG(dbgs() << "\n\n");
789
790 MCI.setLoc(IDLoc);
791 }
792
793 // Create instruction operand for bundle instruction
794 // Break this into a separate function Code here is less readable
795 // Think about how to get an instruction error to report correctly.
796 // SMLoc will return the "{"
797 switch (result) {
798 default:
799 break;
800 case Match_Success:
801 return false;
802 case Match_MissingFeature:
803 return Error(IDLoc, "invalid instruction");
804 case Match_MnemonicFail:
805 return Error(IDLoc, "unrecognized instruction");
806 case Match_InvalidOperand:
807 SMLoc ErrorLoc = IDLoc;
808 if (ErrorInfo != ~0U) {
809 if (ErrorInfo >= InstOperands.size())
810 return Error(IDLoc, "too few operands for instruction");
811
812 ErrorLoc = (static_cast<HexagonOperand *>(InstOperands[ErrorInfo].get()))
813 ->getStartLoc();
814 if (ErrorLoc == SMLoc())
815 ErrorLoc = IDLoc;
816 }
817 return Error(ErrorLoc, "invalid operand for instruction");
818 }
819 llvm_unreachable("Implement any new match types added!");
820}
821
822bool HexagonAsmParser::mustExtend(OperandVector &Operands) {
823 unsigned Count = 0;
824 for (std::unique_ptr<MCParsedAsmOperand> &i : Operands)
825 if (i->isImm())
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000826 if (HexagonMCInstrInfo::mustExtend(
827 *static_cast<HexagonOperand *>(i.get())->Imm.Val))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000828 ++Count;
829 // Multiple extenders should have been filtered by iss9Ext et. al.
830 assert(Count < 2 && "Multiple extenders");
831 return Count == 1;
832}
833
834bool HexagonAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
835 OperandVector &Operands,
836 MCStreamer &Out,
837 uint64_t &ErrorInfo,
838 bool MatchingInlineAsm) {
839 if (!InBrackets) {
840 MCB.clear();
841 MCB.addOperand(MCOperand::createImm(0));
842 }
843 HexagonOperand &FirstOperand = static_cast<HexagonOperand &>(*Operands[0]);
844 if (FirstOperand.isToken() && FirstOperand.getToken() == "{") {
845 assert(Operands.size() == 1 && "Brackets should be by themselves");
846 if (InBrackets) {
847 getParser().Error(IDLoc, "Already in a packet");
848 return true;
849 }
850 InBrackets = true;
851 return false;
852 }
853 if (FirstOperand.isToken() && FirstOperand.getToken() == "}") {
854 assert(Operands.size() == 1 && "Brackets should be by themselves");
855 if (!InBrackets) {
856 getParser().Error(IDLoc, "Not in a packet");
857 return true;
858 }
859 InBrackets = false;
860 if (matchBundleOptions())
861 return true;
862 return finishBundle(IDLoc, Out);
863 }
864 MCInst *SubInst = new (getParser().getContext()) MCInst;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000865 if (matchOneInstruction(*SubInst, IDLoc, Operands, ErrorInfo,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000866 MatchingInlineAsm))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000867 return true;
868 HexagonMCInstrInfo::extendIfNeeded(
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000869 getParser().getContext(), MCII, MCB, *SubInst);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000870 MCB.addOperand(MCOperand::createInst(SubInst));
871 if (!InBrackets)
872 return finishBundle(IDLoc, Out);
873 return false;
874}
875
876/// ParseDirective parses the Hexagon specific directives
877bool HexagonAsmParser::ParseDirective(AsmToken DirectiveID) {
878 StringRef IDVal = DirectiveID.getIdentifier();
879 if ((IDVal.lower() == ".word") || (IDVal.lower() == ".4byte"))
880 return ParseDirectiveValue(4, DirectiveID.getLoc());
881 if (IDVal.lower() == ".short" || IDVal.lower() == ".hword" ||
882 IDVal.lower() == ".half")
883 return ParseDirectiveValue(2, DirectiveID.getLoc());
884 if (IDVal.lower() == ".falign")
885 return ParseDirectiveFalign(256, DirectiveID.getLoc());
886 if ((IDVal.lower() == ".lcomm") || (IDVal.lower() == ".lcommon"))
887 return ParseDirectiveComm(true, DirectiveID.getLoc());
888 if ((IDVal.lower() == ".comm") || (IDVal.lower() == ".common"))
889 return ParseDirectiveComm(false, DirectiveID.getLoc());
890 if (IDVal.lower() == ".subsection")
891 return ParseDirectiveSubsection(DirectiveID.getLoc());
892
893 return true;
894}
895bool HexagonAsmParser::ParseDirectiveSubsection(SMLoc L) {
896 const MCExpr *Subsection = 0;
897 int64_t Res;
898
899 assert((getLexer().isNot(AsmToken::EndOfStatement)) &&
900 "Invalid subsection directive");
901 getParser().parseExpression(Subsection);
902
903 if (!Subsection->evaluateAsAbsolute(Res))
904 return Error(L, "Cannot evaluate subsection number");
905
906 if (getLexer().isNot(AsmToken::EndOfStatement))
907 return TokError("unexpected token in directive");
908
909 // 0-8192 is the hard-coded range in MCObjectStreamper.cpp, this keeps the
910 // negative subsections together and in the same order but at the opposite
911 // end of the section. Only legacy hexagon-gcc created assembly code
912 // used negative subsections.
913 if ((Res < 0) && (Res > -8193))
Colin LeMahieuc7b21242016-02-15 18:47:55 +0000914 Subsection = HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000915 MCConstantExpr::create(8192 + Res, getContext()), getContext());
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000916
917 getStreamer().SubSection(Subsection);
918 return false;
919}
920
921/// ::= .falign [expression]
922bool HexagonAsmParser::ParseDirectiveFalign(unsigned Size, SMLoc L) {
923
924 int64_t MaxBytesToFill = 15;
925
926 // if there is an arguement
927 if (getLexer().isNot(AsmToken::EndOfStatement)) {
928 const MCExpr *Value;
929 SMLoc ExprLoc = L;
930
931 // Make sure we have a number (false is returned if expression is a number)
932 if (getParser().parseExpression(Value) == false) {
933 // Make sure this is a number that is in range
934 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
935 uint64_t IntValue = MCE->getValue();
936 if (!isUIntN(Size, IntValue) && !isIntN(Size, IntValue))
937 return Error(ExprLoc, "literal value out of range (256) for falign");
938 MaxBytesToFill = IntValue;
939 Lex();
940 } else {
941 return Error(ExprLoc, "not a valid expression for falign directive");
942 }
943 }
944
945 getTargetStreamer().emitFAlign(16, MaxBytesToFill);
946 Lex();
947
948 return false;
949}
950
951/// ::= .word [ expression (, expression)* ]
952bool HexagonAsmParser::ParseDirectiveValue(unsigned Size, SMLoc L) {
953 if (getLexer().isNot(AsmToken::EndOfStatement)) {
954
955 for (;;) {
956 const MCExpr *Value;
957 SMLoc ExprLoc = L;
958 if (getParser().parseExpression(Value))
959 return true;
960
961 // Special case constant expressions to match code generator.
962 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
963 assert(Size <= 8 && "Invalid size");
964 uint64_t IntValue = MCE->getValue();
965 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
966 return Error(ExprLoc, "literal value out of range for directive");
967 getStreamer().EmitIntValue(IntValue, Size);
968 } else
969 getStreamer().EmitValue(Value, Size);
970
971 if (getLexer().is(AsmToken::EndOfStatement))
972 break;
973
974 // FIXME: Improve diagnostic.
975 if (getLexer().isNot(AsmToken::Comma))
976 return TokError("unexpected token in directive");
977 Lex();
978 }
979 }
980
981 Lex();
982 return false;
983}
984
985// This is largely a copy of AsmParser's ParseDirectiveComm extended to
986// accept a 3rd argument, AccessAlignment which indicates the smallest
987// memory access made to the symbol, expressed in bytes. If no
988// AccessAlignment is specified it defaults to the Alignment Value.
989// Hexagon's .lcomm:
990// .lcomm Symbol, Length, Alignment, AccessAlignment
991bool HexagonAsmParser::ParseDirectiveComm(bool IsLocal, SMLoc Loc) {
992 // FIXME: need better way to detect if AsmStreamer (upstream removed
993 // getKind())
994 if (getStreamer().hasRawTextSupport())
995 return true; // Only object file output requires special treatment.
996
997 StringRef Name;
998 if (getParser().parseIdentifier(Name))
999 return TokError("expected identifier in directive");
1000 // Handle the identifier as the key symbol.
1001 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
1002
1003 if (getLexer().isNot(AsmToken::Comma))
1004 return TokError("unexpected token in directive");
1005 Lex();
1006
1007 int64_t Size;
1008 SMLoc SizeLoc = getLexer().getLoc();
1009 if (getParser().parseAbsoluteExpression(Size))
1010 return true;
1011
1012 int64_t ByteAlignment = 1;
1013 SMLoc ByteAlignmentLoc;
1014 if (getLexer().is(AsmToken::Comma)) {
1015 Lex();
1016 ByteAlignmentLoc = getLexer().getLoc();
1017 if (getParser().parseAbsoluteExpression(ByteAlignment))
1018 return true;
1019 if (!isPowerOf2_64(ByteAlignment))
1020 return Error(ByteAlignmentLoc, "alignment must be a power of 2");
1021 }
1022
1023 int64_t AccessAlignment = 0;
1024 if (getLexer().is(AsmToken::Comma)) {
1025 // The optional access argument specifies the size of the smallest memory
1026 // access to be made to the symbol, expressed in bytes.
1027 SMLoc AccessAlignmentLoc;
1028 Lex();
1029 AccessAlignmentLoc = getLexer().getLoc();
1030 if (getParser().parseAbsoluteExpression(AccessAlignment))
1031 return true;
1032
1033 if (!isPowerOf2_64(AccessAlignment))
1034 return Error(AccessAlignmentLoc, "access alignment must be a power of 2");
1035 }
1036
1037 if (getLexer().isNot(AsmToken::EndOfStatement))
1038 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1039
1040 Lex();
1041
1042 // NOTE: a size of zero for a .comm should create a undefined symbol
1043 // but a size of .lcomm creates a bss symbol of size zero.
1044 if (Size < 0)
1045 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1046 "be less than zero");
1047
1048 // NOTE: The alignment in the directive is a power of 2 value, the assembler
1049 // may internally end up wanting an alignment in bytes.
1050 // FIXME: Diagnose overflow.
1051 if (ByteAlignment < 0)
1052 return Error(ByteAlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1053 "alignment, can't be less than zero");
1054
1055 if (!Sym->isUndefined())
1056 return Error(Loc, "invalid symbol redefinition");
1057
1058 HexagonMCELFStreamer &HexagonELFStreamer =
1059 static_cast<HexagonMCELFStreamer &>(getStreamer());
1060 if (IsLocal) {
1061 HexagonELFStreamer.HexagonMCEmitLocalCommonSymbol(Sym, Size, ByteAlignment,
1062 AccessAlignment);
1063 return false;
1064 }
1065
1066 HexagonELFStreamer.HexagonMCEmitCommonSymbol(Sym, Size, ByteAlignment,
1067 AccessAlignment);
1068 return false;
1069}
1070
1071// validate register against architecture
1072bool HexagonAsmParser::RegisterMatchesArch(unsigned MatchNum) const {
1073 return true;
1074}
1075
1076// extern "C" void LLVMInitializeHexagonAsmLexer();
1077
1078/// Force static initialization.
1079extern "C" void LLVMInitializeHexagonAsmParser() {
1080 RegisterMCAsmParser<HexagonAsmParser> X(TheHexagonTarget);
1081}
1082
1083#define GET_MATCHER_IMPLEMENTATION
1084#define GET_REGISTER_MATCHER
1085#include "HexagonGenAsmMatcher.inc"
1086
1087namespace {
1088bool previousEqual(OperandVector &Operands, size_t Index, StringRef String) {
1089 if (Index >= Operands.size())
1090 return false;
1091 MCParsedAsmOperand &Operand = *Operands[Operands.size() - Index - 1];
1092 if (!Operand.isToken())
1093 return false;
1094 return static_cast<HexagonOperand &>(Operand).getToken().equals_lower(String);
1095}
1096bool previousIsLoop(OperandVector &Operands, size_t Index) {
1097 return previousEqual(Operands, Index, "loop0") ||
1098 previousEqual(Operands, Index, "loop1") ||
1099 previousEqual(Operands, Index, "sp1loop0") ||
1100 previousEqual(Operands, Index, "sp2loop0") ||
1101 previousEqual(Operands, Index, "sp3loop0");
1102}
1103}
1104
1105bool HexagonAsmParser::splitIdentifier(OperandVector &Operands) {
1106 AsmToken const &Token = getParser().getTok();
1107 StringRef String = Token.getString();
1108 SMLoc Loc = Token.getLoc();
1109 getLexer().Lex();
1110 do {
1111 std::pair<StringRef, StringRef> HeadTail = String.split('.');
1112 if (!HeadTail.first.empty())
1113 Operands.push_back(HexagonOperand::CreateToken(HeadTail.first, Loc));
1114 if (!HeadTail.second.empty())
1115 Operands.push_back(HexagonOperand::CreateToken(
1116 String.substr(HeadTail.first.size(), 1), Loc));
1117 String = HeadTail.second;
1118 } while (!String.empty());
1119 return false;
1120}
1121
1122bool HexagonAsmParser::parseOperand(OperandVector &Operands) {
1123 unsigned Register;
1124 SMLoc Begin;
1125 SMLoc End;
1126 MCAsmLexer &Lexer = getLexer();
1127 if (!ParseRegister(Register, Begin, End)) {
1128 if (!ErrorMissingParenthesis)
1129 switch (Register) {
1130 default:
1131 break;
1132 case Hexagon::P0:
1133 case Hexagon::P1:
1134 case Hexagon::P2:
1135 case Hexagon::P3:
1136 if (previousEqual(Operands, 0, "if")) {
1137 if (WarnMissingParenthesis)
1138 Warning (Begin, "Missing parenthesis around predicate register");
1139 static char const *LParen = "(";
1140 static char const *RParen = ")";
1141 Operands.push_back(HexagonOperand::CreateToken(LParen, Begin));
1142 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End));
1143 AsmToken MaybeDotNew = Lexer.getTok();
1144 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) &&
1145 MaybeDotNew.getString().equals_lower(".new"))
1146 splitIdentifier(Operands);
1147 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin));
1148 return false;
1149 }
1150 if (previousEqual(Operands, 0, "!") &&
1151 previousEqual(Operands, 1, "if")) {
1152 if (WarnMissingParenthesis)
1153 Warning (Begin, "Missing parenthesis around predicate register");
1154 static char const *LParen = "(";
1155 static char const *RParen = ")";
1156 Operands.insert(Operands.end () - 1,
1157 HexagonOperand::CreateToken(LParen, Begin));
1158 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End));
1159 AsmToken MaybeDotNew = Lexer.getTok();
1160 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) &&
1161 MaybeDotNew.getString().equals_lower(".new"))
1162 splitIdentifier(Operands);
1163 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin));
1164 return false;
1165 }
1166 break;
1167 }
1168 Operands.push_back(HexagonOperand::CreateReg(
1169 Register, Begin, End));
1170 return false;
1171 }
1172 return splitIdentifier(Operands);
1173}
1174
1175bool HexagonAsmParser::isLabel(AsmToken &Token) {
1176 MCAsmLexer &Lexer = getLexer();
1177 AsmToken const &Second = Lexer.getTok();
1178 AsmToken Third = Lexer.peekTok();
1179 StringRef String = Token.getString();
1180 if (Token.is(AsmToken::TokenKind::LCurly) ||
1181 Token.is(AsmToken::TokenKind::RCurly))
1182 return false;
1183 if (!Token.is(AsmToken::TokenKind::Identifier))
1184 return true;
1185 if (!MatchRegisterName(String.lower()))
1186 return true;
Colin LeMahieu9d851f02015-11-09 21:06:28 +00001187 (void)Second;
1188 assert(Second.is(AsmToken::Colon));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001189 StringRef Raw (String.data(), Third.getString().data() - String.data() +
1190 Third.getString().size());
1191 std::string Collapsed = Raw;
1192 Collapsed.erase(std::remove_if(Collapsed.begin(), Collapsed.end(), isspace),
1193 Collapsed.end());
1194 StringRef Whole = Collapsed;
1195 std::pair<StringRef, StringRef> DotSplit = Whole.split('.');
1196 if (!MatchRegisterName(DotSplit.first.lower()))
1197 return true;
1198 return false;
1199}
1200
1201bool HexagonAsmParser::handleNoncontigiousRegister(bool Contigious, SMLoc &Loc) {
1202 if (!Contigious && ErrorNoncontigiousRegister) {
1203 Error(Loc, "Register name is not contigious");
1204 return true;
1205 }
1206 if (!Contigious && WarnNoncontigiousRegister)
1207 Warning(Loc, "Register name is not contigious");
1208 return false;
1209}
1210
1211bool HexagonAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) {
1212 MCAsmLexer &Lexer = getLexer();
1213 StartLoc = getLexer().getLoc();
1214 SmallVector<AsmToken, 5> Lookahead;
1215 StringRef RawString(Lexer.getTok().getString().data(), 0);
1216 bool Again = Lexer.is(AsmToken::Identifier);
1217 bool NeededWorkaround = false;
1218 while (Again) {
1219 AsmToken const &Token = Lexer.getTok();
1220 RawString = StringRef(RawString.data(),
1221 Token.getString().data() - RawString.data () +
1222 Token.getString().size());
1223 Lookahead.push_back(Token);
1224 Lexer.Lex();
1225 bool Contigious = Lexer.getTok().getString().data() ==
1226 Lookahead.back().getString().data() +
1227 Lookahead.back().getString().size();
1228 bool Type = Lexer.is(AsmToken::Identifier) || Lexer.is(AsmToken::Dot) ||
1229 Lexer.is(AsmToken::Integer) || Lexer.is(AsmToken::Real) ||
1230 Lexer.is(AsmToken::Colon);
1231 bool Workaround = Lexer.is(AsmToken::Colon) ||
1232 Lookahead.back().is(AsmToken::Colon);
1233 Again = (Contigious && Type) || (Workaround && Type);
1234 NeededWorkaround = NeededWorkaround || (Again && !(Contigious && Type));
1235 }
1236 std::string Collapsed = RawString;
1237 Collapsed.erase(std::remove_if(Collapsed.begin(), Collapsed.end(), isspace),
1238 Collapsed.end());
1239 StringRef FullString = Collapsed;
1240 std::pair<StringRef, StringRef> DotSplit = FullString.split('.');
1241 unsigned DotReg = MatchRegisterName(DotSplit.first.lower());
1242 if (DotReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1243 if (DotSplit.second.empty()) {
1244 RegNo = DotReg;
1245 EndLoc = Lexer.getLoc();
1246 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1247 return true;
1248 return false;
1249 } else {
1250 RegNo = DotReg;
1251 size_t First = RawString.find('.');
1252 StringRef DotString (RawString.data() + First, RawString.size() - First);
1253 Lexer.UnLex(AsmToken(AsmToken::Identifier, DotString));
1254 EndLoc = Lexer.getLoc();
1255 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1256 return true;
1257 return false;
1258 }
1259 }
1260 std::pair<StringRef, StringRef> ColonSplit = StringRef(FullString).split(':');
1261 unsigned ColonReg = MatchRegisterName(ColonSplit.first.lower());
1262 if (ColonReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1263 Lexer.UnLex(Lookahead.back());
1264 Lookahead.pop_back();
1265 Lexer.UnLex(Lookahead.back());
1266 Lookahead.pop_back();
1267 RegNo = ColonReg;
1268 EndLoc = Lexer.getLoc();
1269 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1270 return true;
1271 return false;
1272 }
1273 while (!Lookahead.empty()) {
1274 Lexer.UnLex(Lookahead.back());
1275 Lookahead.pop_back();
1276 }
1277 return true;
1278}
1279
1280bool HexagonAsmParser::implicitExpressionLocation(OperandVector &Operands) {
1281 if (previousEqual(Operands, 0, "call"))
1282 return true;
1283 if (previousEqual(Operands, 0, "jump"))
1284 if (!getLexer().getTok().is(AsmToken::Colon))
1285 return true;
1286 if (previousEqual(Operands, 0, "(") && previousIsLoop(Operands, 1))
1287 return true;
1288 if (previousEqual(Operands, 1, ":") && previousEqual(Operands, 2, "jump") &&
1289 (previousEqual(Operands, 0, "nt") || previousEqual(Operands, 0, "t")))
1290 return true;
1291 return false;
1292}
1293
1294bool HexagonAsmParser::parseExpression(MCExpr const *& Expr) {
1295 llvm::SmallVector<AsmToken, 4> Tokens;
1296 MCAsmLexer &Lexer = getLexer();
1297 bool Done = false;
1298 static char const * Comma = ",";
1299 do {
1300 Tokens.emplace_back (Lexer.getTok());
1301 Lexer.Lex();
1302 switch (Tokens.back().getKind())
1303 {
1304 case AsmToken::TokenKind::Hash:
1305 if (Tokens.size () > 1)
1306 if ((Tokens.end () - 2)->getKind() == AsmToken::TokenKind::Plus) {
1307 Tokens.insert(Tokens.end() - 2,
1308 AsmToken(AsmToken::TokenKind::Comma, Comma));
1309 Done = true;
1310 }
1311 break;
1312 case AsmToken::TokenKind::RCurly:
1313 case AsmToken::TokenKind::EndOfStatement:
1314 case AsmToken::TokenKind::Eof:
1315 Done = true;
1316 break;
1317 default:
1318 break;
1319 }
1320 } while (!Done);
1321 while (!Tokens.empty()) {
1322 Lexer.UnLex(Tokens.back());
1323 Tokens.pop_back();
1324 }
1325 return getParser().parseExpression(Expr);
1326}
1327
1328bool HexagonAsmParser::parseExpressionOrOperand(OperandVector &Operands) {
1329 if (implicitExpressionLocation(Operands)) {
1330 MCAsmParser &Parser = getParser();
1331 SMLoc Loc = Parser.getLexer().getLoc();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001332 MCExpr const *Expr = nullptr;
1333 bool Error = parseExpression(Expr);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001334 Expr = HexagonMCExpr::create(Expr, getContext());
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001335 if (!Error)
1336 Operands.push_back(HexagonOperand::CreateImm(Expr, Loc, Loc));
1337 return Error;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001338 }
1339 return parseOperand(Operands);
1340}
1341
1342/// Parse an instruction.
1343bool HexagonAsmParser::parseInstruction(OperandVector &Operands) {
1344 MCAsmParser &Parser = getParser();
1345 MCAsmLexer &Lexer = getLexer();
1346 while (true) {
1347 AsmToken const &Token = Parser.getTok();
1348 switch (Token.getKind()) {
1349 case AsmToken::EndOfStatement: {
1350 Lexer.Lex();
1351 return false;
1352 }
1353 case AsmToken::LCurly: {
1354 if (!Operands.empty())
1355 return true;
1356 Operands.push_back(
1357 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
1358 Lexer.Lex();
1359 return false;
1360 }
1361 case AsmToken::RCurly: {
1362 if (Operands.empty()) {
1363 Operands.push_back(
1364 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
1365 Lexer.Lex();
1366 }
1367 return false;
1368 }
1369 case AsmToken::Comma: {
1370 Lexer.Lex();
1371 continue;
1372 }
1373 case AsmToken::EqualEqual:
1374 case AsmToken::ExclaimEqual:
1375 case AsmToken::GreaterEqual:
1376 case AsmToken::GreaterGreater:
1377 case AsmToken::LessEqual:
1378 case AsmToken::LessLess: {
1379 Operands.push_back(HexagonOperand::CreateToken(
1380 Token.getString().substr(0, 1), Token.getLoc()));
1381 Operands.push_back(HexagonOperand::CreateToken(
1382 Token.getString().substr(1, 1), Token.getLoc()));
1383 Lexer.Lex();
1384 continue;
1385 }
1386 case AsmToken::Hash: {
1387 bool MustNotExtend = false;
1388 bool ImplicitExpression = implicitExpressionLocation(Operands);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001389 SMLoc ExprLoc = Lexer.getLoc();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001390 if (!ImplicitExpression)
1391 Operands.push_back(
1392 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
1393 Lexer.Lex();
1394 bool MustExtend = false;
1395 bool HiOnly = false;
1396 bool LoOnly = false;
1397 if (Lexer.is(AsmToken::Hash)) {
1398 Lexer.Lex();
1399 MustExtend = true;
1400 } else if (ImplicitExpression)
1401 MustNotExtend = true;
1402 AsmToken const &Token = Parser.getTok();
1403 if (Token.is(AsmToken::Identifier)) {
1404 StringRef String = Token.getString();
1405 AsmToken IDToken = Token;
1406 if (String.lower() == "hi") {
1407 HiOnly = true;
1408 } else if (String.lower() == "lo") {
1409 LoOnly = true;
1410 }
1411 if (HiOnly || LoOnly) {
1412 AsmToken LParen = Lexer.peekTok();
1413 if (!LParen.is(AsmToken::LParen)) {
1414 HiOnly = false;
1415 LoOnly = false;
1416 } else {
1417 Lexer.Lex();
1418 }
1419 }
1420 }
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001421 MCExpr const *Expr = nullptr;
1422 if (parseExpression(Expr))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001423 return true;
1424 int64_t Value;
1425 MCContext &Context = Parser.getContext();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001426 assert(Expr != nullptr);
1427 if (Expr->evaluateAsAbsolute(Value)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001428 if (HiOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001429 Expr = MCBinaryExpr::createLShr(
1430 Expr, MCConstantExpr::create(16, Context), Context);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001431 if (HiOnly || LoOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001432 Expr = MCBinaryExpr::createAnd(Expr,
1433 MCConstantExpr::create(0xffff, Context),
1434 Context);
Colin LeMahieu5cb6eea2016-03-01 21:37:41 +00001435 } else {
1436 MCValue Value;
1437 if (Expr->evaluateAsRelocatable(Value, nullptr, nullptr)) {
1438 if (!Value.isAbsolute()) {
1439 switch(Value.getAccessVariant()) {
1440 case MCSymbolRefExpr::VariantKind::VK_TPREL:
1441 case MCSymbolRefExpr::VariantKind::VK_DTPREL:
1442 // Don't lazy extend these expression variants
1443 MustNotExtend = !MustExtend;
1444 break;
1445 default:
1446 break;
1447 }
1448 }
1449 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001450 }
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001451 Expr = HexagonMCExpr::create(Expr, Context);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001452 HexagonMCInstrInfo::setMustNotExtend(*Expr, MustNotExtend);
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001453 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001454 std::unique_ptr<HexagonOperand> Operand =
1455 HexagonOperand::CreateImm(Expr, ExprLoc, ExprLoc);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001456 Operands.push_back(std::move(Operand));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001457 continue;
1458 }
1459 default:
1460 break;
1461 }
1462 if (parseExpressionOrOperand(Operands))
1463 return true;
1464 }
1465}
1466
1467bool HexagonAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1468 StringRef Name,
1469 AsmToken ID,
1470 OperandVector &Operands) {
1471 getLexer().UnLex(ID);
1472 return parseInstruction(Operands);
1473}
1474
1475namespace {
1476MCInst makeCombineInst(int opCode, MCOperand &Rdd,
1477 MCOperand &MO1, MCOperand &MO2) {
1478 MCInst TmpInst;
1479 TmpInst.setOpcode(opCode);
1480 TmpInst.addOperand(Rdd);
1481 TmpInst.addOperand(MO1);
1482 TmpInst.addOperand(MO2);
1483
1484 return TmpInst;
1485}
1486}
1487
1488// Define this matcher function after the auto-generated include so we
1489// have the match class enum definitions.
1490unsigned HexagonAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
1491 unsigned Kind) {
1492 HexagonOperand *Op = static_cast<HexagonOperand *>(&AsmOp);
1493
1494 switch (Kind) {
1495 case MCK_0: {
1496 int64_t Value;
1497 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 0
1498 ? Match_Success
1499 : Match_InvalidOperand;
1500 }
1501 case MCK_1: {
1502 int64_t Value;
1503 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 1
1504 ? Match_Success
1505 : Match_InvalidOperand;
1506 }
1507 case MCK__MINUS_1: {
1508 int64_t Value;
1509 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == -1
1510 ? Match_Success
1511 : Match_InvalidOperand;
1512 }
1513 }
1514 if (Op->Kind == HexagonOperand::Token && Kind != InvalidMatchClass) {
1515 StringRef myStringRef = StringRef(Op->Tok.Data, Op->Tok.Length);
1516 if (matchTokenString(myStringRef.lower()) == (MatchClassKind)Kind)
1517 return Match_Success;
1518 if (matchTokenString(myStringRef.upper()) == (MatchClassKind)Kind)
1519 return Match_Success;
1520 }
1521
1522 DEBUG(dbgs() << "Unmatched Operand:");
1523 DEBUG(Op->dump());
1524 DEBUG(dbgs() << "\n");
1525
1526 return Match_InvalidOperand;
1527}
1528
1529void HexagonAsmParser::OutOfRange(SMLoc IDLoc, long long Val, long long Max) {
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001530 std::string errStr;
1531 raw_string_ostream ES(errStr);
Alexey Samsonov44ff2042015-12-02 22:59:22 +00001532 ES << "value " << Val << "(" << format_hex(Val, 0) << ") out of range: ";
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001533 if (Max >= 0)
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001534 ES << "0-" << Max;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001535 else
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001536 ES << Max << "-" << (-Max - 1);
1537 Error(IDLoc, ES.str().c_str());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001538}
1539
1540int HexagonAsmParser::processInstruction(MCInst &Inst,
1541 OperandVector const &Operands,
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001542 SMLoc IDLoc) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001543 MCContext &Context = getParser().getContext();
1544 const MCRegisterInfo *RI = getContext().getRegisterInfo();
1545 std::string r = "r";
1546 std::string v = "v";
1547 std::string Colon = ":";
1548
1549 bool is32bit = false; // used to distinguish between CONST32 and CONST64
1550 switch (Inst.getOpcode()) {
1551 default:
1552 break;
1553
Colin LeMahieuecef1d92016-02-16 20:38:17 +00001554 case Hexagon::A2_iconst: {
1555 Inst.setOpcode(Hexagon::A2_addi);
1556 MCOperand Reg = Inst.getOperand(0);
1557 MCOperand S16 = Inst.getOperand(1);
1558 HexagonMCInstrInfo::setMustNotExtend(*S16.getExpr());
1559 HexagonMCInstrInfo::setS23_2_reloc(*S16.getExpr());
1560 Inst.clear();
1561 Inst.addOperand(Reg);
1562 Inst.addOperand(MCOperand::createReg(Hexagon::R0));
1563 Inst.addOperand(S16);
1564 break;
1565 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001566 case Hexagon::M4_mpyrr_addr:
1567 case Hexagon::S4_addi_asl_ri:
1568 case Hexagon::S4_addi_lsr_ri:
1569 case Hexagon::S4_andi_asl_ri:
1570 case Hexagon::S4_andi_lsr_ri:
1571 case Hexagon::S4_ori_asl_ri:
1572 case Hexagon::S4_ori_lsr_ri:
1573 case Hexagon::S4_or_andix:
1574 case Hexagon::S4_subi_asl_ri:
1575 case Hexagon::S4_subi_lsr_ri: {
1576 MCOperand &Ry = Inst.getOperand(0);
1577 MCOperand &src = Inst.getOperand(2);
1578 if (RI->getEncodingValue(Ry.getReg()) != RI->getEncodingValue(src.getReg()))
1579 return Match_InvalidOperand;
1580 break;
1581 }
1582
1583 case Hexagon::C2_cmpgei: {
1584 MCOperand &MO = Inst.getOperand(2);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001585 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001586 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001587 Inst.setOpcode(Hexagon::C2_cmpgti);
1588 break;
1589 }
1590
1591 case Hexagon::C2_cmpgeui: {
1592 MCOperand &MO = Inst.getOperand(2);
1593 int64_t Value;
1594 bool Success = MO.getExpr()->evaluateAsAbsolute(Value);
Colin LeMahieu9d851f02015-11-09 21:06:28 +00001595 (void)Success;
1596 assert(Success && "Assured by matcher");
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001597 if (Value == 0) {
1598 MCInst TmpInst;
1599 MCOperand &Pd = Inst.getOperand(0);
1600 MCOperand &Rt = Inst.getOperand(1);
1601 TmpInst.setOpcode(Hexagon::C2_cmpeq);
1602 TmpInst.addOperand(Pd);
1603 TmpInst.addOperand(Rt);
1604 TmpInst.addOperand(Rt);
1605 Inst = TmpInst;
1606 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001607 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001608 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001609 Inst.setOpcode(Hexagon::C2_cmpgtui);
1610 }
1611 break;
1612 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001613
1614 // Translate a "$Rdd = $Rss" to "$Rdd = combine($Rs, $Rt)"
1615 case Hexagon::A2_tfrp: {
1616 MCOperand &MO = Inst.getOperand(1);
1617 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001618 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001619 StringRef Reg1(R1);
1620 MO.setReg(MatchRegisterName(Reg1));
1621 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001622 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001623 StringRef Reg2(R2);
1624 Inst.addOperand(MCOperand::createReg(MatchRegisterName(Reg2)));
1625 Inst.setOpcode(Hexagon::A2_combinew);
1626 break;
1627 }
1628
1629 case Hexagon::A2_tfrpt:
1630 case Hexagon::A2_tfrpf: {
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);
1635 MO.setReg(MatchRegisterName(Reg1));
1636 // 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);
1639 Inst.addOperand(MCOperand::createReg(MatchRegisterName(Reg2)));
1640 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrpt)
1641 ? Hexagon::C2_ccombinewt
1642 : Hexagon::C2_ccombinewf);
1643 break;
1644 }
1645 case Hexagon::A2_tfrptnew:
1646 case Hexagon::A2_tfrpfnew: {
1647 MCOperand &MO = Inst.getOperand(2);
1648 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001649 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001650 StringRef Reg1(R1);
1651 MO.setReg(MatchRegisterName(Reg1));
1652 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001653 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001654 StringRef Reg2(R2);
1655 Inst.addOperand(MCOperand::createReg(MatchRegisterName(Reg2)));
1656 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrptnew)
1657 ? Hexagon::C2_ccombinewnewt
1658 : Hexagon::C2_ccombinewnewf);
1659 break;
1660 }
1661
1662 // Translate a "$Rx = CONST32(#imm)" to "$Rx = memw(gp+#LABEL) "
1663 case Hexagon::CONST32:
1664 case Hexagon::CONST32_Float_Real:
1665 case Hexagon::CONST32_Int_Real:
1666 case Hexagon::FCONST32_nsdata:
1667 is32bit = true;
1668 // Translate a "$Rx:y = CONST64(#imm)" to "$Rx:y = memd(gp+#LABEL) "
1669 case Hexagon::CONST64_Float_Real:
1670 case Hexagon::CONST64_Int_Real:
1671
1672 // FIXME: need better way to detect AsmStreamer (upstream removed getKind())
1673 if (!Parser.getStreamer().hasRawTextSupport()) {
1674 MCELFStreamer *MES = static_cast<MCELFStreamer *>(&Parser.getStreamer());
1675 MCOperand &MO_1 = Inst.getOperand(1);
1676 MCOperand &MO_0 = Inst.getOperand(0);
1677
1678 // push section onto section stack
1679 MES->PushSection();
1680
1681 std::string myCharStr;
1682 MCSectionELF *mySection;
1683
1684 // check if this as an immediate or a symbol
1685 int64_t Value;
1686 bool Absolute = MO_1.getExpr()->evaluateAsAbsolute(Value);
1687 if (Absolute) {
1688 // Create a new section - one for each constant
1689 // Some or all of the zeros are replaced with the given immediate.
1690 if (is32bit) {
1691 std::string myImmStr = utohexstr(static_cast<uint32_t>(Value));
1692 myCharStr = StringRef(".gnu.linkonce.l4.CONST_00000000")
1693 .drop_back(myImmStr.size())
1694 .str() +
1695 myImmStr;
1696 } else {
1697 std::string myImmStr = utohexstr(Value);
1698 myCharStr = StringRef(".gnu.linkonce.l8.CONST_0000000000000000")
1699 .drop_back(myImmStr.size())
1700 .str() +
1701 myImmStr;
1702 }
1703
1704 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1705 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1706 } else if (MO_1.isExpr()) {
1707 // .lita - for expressions
1708 myCharStr = ".lita";
1709 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1710 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1711 } else
1712 llvm_unreachable("unexpected type of machine operand!");
1713
1714 MES->SwitchSection(mySection);
1715 unsigned byteSize = is32bit ? 4 : 8;
1716 getStreamer().EmitCodeAlignment(byteSize, byteSize);
1717
1718 MCSymbol *Sym;
1719
1720 // for symbols, get rid of prepended ".gnu.linkonce.lx."
1721
1722 // emit symbol if needed
1723 if (Absolute) {
1724 Sym = getContext().getOrCreateSymbol(StringRef(myCharStr.c_str() + 16));
1725 if (Sym->isUndefined()) {
1726 getStreamer().EmitLabel(Sym);
1727 getStreamer().EmitSymbolAttribute(Sym, MCSA_Global);
1728 getStreamer().EmitIntValue(Value, byteSize);
1729 }
1730 } else if (MO_1.isExpr()) {
1731 const char *StringStart = 0;
1732 const char *StringEnd = 0;
1733 if (*Operands[4]->getStartLoc().getPointer() == '#') {
1734 StringStart = Operands[5]->getStartLoc().getPointer();
1735 StringEnd = Operands[6]->getStartLoc().getPointer();
1736 } else { // no pound
1737 StringStart = Operands[4]->getStartLoc().getPointer();
1738 StringEnd = Operands[5]->getStartLoc().getPointer();
1739 }
1740
1741 unsigned size = StringEnd - StringStart;
1742 std::string DotConst = ".CONST_";
1743 Sym = getContext().getOrCreateSymbol(DotConst +
1744 StringRef(StringStart, size));
1745
1746 if (Sym->isUndefined()) {
1747 // case where symbol is not yet defined: emit symbol
1748 getStreamer().EmitLabel(Sym);
1749 getStreamer().EmitSymbolAttribute(Sym, MCSA_Local);
1750 getStreamer().EmitValue(MO_1.getExpr(), 4);
1751 }
1752 } else
1753 llvm_unreachable("unexpected type of machine operand!");
1754
1755 MES->PopSection();
1756
1757 if (Sym) {
1758 MCInst TmpInst;
1759 if (is32bit) // 32 bit
1760 TmpInst.setOpcode(Hexagon::L2_loadrigp);
1761 else // 64 bit
1762 TmpInst.setOpcode(Hexagon::L2_loadrdgp);
1763
1764 TmpInst.addOperand(MO_0);
1765 TmpInst.addOperand(
1766 MCOperand::createExpr(MCSymbolRefExpr::create(Sym, getContext())));
1767 Inst = TmpInst;
1768 }
1769 }
1770 break;
1771
1772 // Translate a "$Rdd = #-imm" to "$Rdd = combine(#[-1,0], #-imm)"
1773 case Hexagon::A2_tfrpi: {
1774 MCOperand &Rdd = Inst.getOperand(0);
1775 MCOperand &MO = Inst.getOperand(1);
1776 int64_t Value;
1777 int sVal = (MO.getExpr()->evaluateAsAbsolute(Value) && Value < 0) ? -1 : 0;
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001778 MCOperand imm(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001779 HexagonMCExpr::create(MCConstantExpr::create(sVal, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001780 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, imm, MO);
1781 break;
1782 }
1783
1784 // Translate a "$Rdd = [#]#imm" to "$Rdd = combine(#, [#]#imm)"
1785 case Hexagon::TFRI64_V4: {
1786 MCOperand &Rdd = Inst.getOperand(0);
1787 MCOperand &MO = Inst.getOperand(1);
1788 int64_t Value;
1789 if (MO.getExpr()->evaluateAsAbsolute(Value)) {
1790 unsigned long long u64 = Value;
1791 signed int s8 = (u64 >> 32) & 0xFFFFFFFF;
1792 if (s8 < -128 || s8 > 127)
1793 OutOfRange(IDLoc, s8, -128);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001794 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001795 MCConstantExpr::create(s8, Context), Context))); // upper 32
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001796 auto Expr = HexagonMCExpr::create(
1797 MCConstantExpr::create(u64 & 0xFFFFFFFF, Context),
1798 Context);
1799 HexagonMCInstrInfo::setMustExtend(*Expr, HexagonMCInstrInfo::mustExtend(*MO.getExpr()));
1800 MCOperand imm2(MCOperand::createExpr(Expr)); // lower 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001801 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, imm2);
1802 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001803 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001804 MCConstantExpr::create(0, Context), Context))); // upper 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001805 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, MO);
1806 }
1807 break;
1808 }
1809
1810 // Handle $Rdd = combine(##imm, #imm)"
1811 case Hexagon::TFRI64_V2_ext: {
1812 MCOperand &Rdd = Inst.getOperand(0);
1813 MCOperand &MO1 = Inst.getOperand(1);
1814 MCOperand &MO2 = Inst.getOperand(2);
1815 int64_t Value;
1816 if (MO2.getExpr()->evaluateAsAbsolute(Value)) {
1817 int s8 = Value;
1818 if (s8 < -128 || s8 > 127)
1819 OutOfRange(IDLoc, s8, -128);
1820 }
1821 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, MO1, MO2);
1822 break;
1823 }
1824
1825 // Handle $Rdd = combine(#imm, ##imm)"
1826 case Hexagon::A4_combineii: {
1827 MCOperand &Rdd = Inst.getOperand(0);
1828 MCOperand &MO1 = Inst.getOperand(1);
1829 int64_t Value;
1830 if (MO1.getExpr()->evaluateAsAbsolute(Value)) {
1831 int s8 = Value;
1832 if (s8 < -128 || s8 > 127)
1833 OutOfRange(IDLoc, s8, -128);
1834 }
1835 MCOperand &MO2 = Inst.getOperand(2);
1836 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, MO1, MO2);
1837 break;
1838 }
1839
1840 case Hexagon::S2_tableidxb_goodsyntax: {
1841 Inst.setOpcode(Hexagon::S2_tableidxb);
1842 break;
1843 }
1844
1845 case Hexagon::S2_tableidxh_goodsyntax: {
1846 MCInst TmpInst;
1847 MCOperand &Rx = Inst.getOperand(0);
1848 MCOperand &_dst_ = Inst.getOperand(1);
1849 MCOperand &Rs = Inst.getOperand(2);
1850 MCOperand &Imm4 = Inst.getOperand(3);
1851 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001852 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001853 Imm6.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001854 TmpInst.setOpcode(Hexagon::S2_tableidxh);
1855 TmpInst.addOperand(Rx);
1856 TmpInst.addOperand(_dst_);
1857 TmpInst.addOperand(Rs);
1858 TmpInst.addOperand(Imm4);
1859 TmpInst.addOperand(Imm6);
1860 Inst = TmpInst;
1861 break;
1862 }
1863
1864 case Hexagon::S2_tableidxw_goodsyntax: {
1865 MCInst TmpInst;
1866 MCOperand &Rx = Inst.getOperand(0);
1867 MCOperand &_dst_ = Inst.getOperand(1);
1868 MCOperand &Rs = Inst.getOperand(2);
1869 MCOperand &Imm4 = Inst.getOperand(3);
1870 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001871 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001872 Imm6.getExpr(), MCConstantExpr::create(2, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001873 TmpInst.setOpcode(Hexagon::S2_tableidxw);
1874 TmpInst.addOperand(Rx);
1875 TmpInst.addOperand(_dst_);
1876 TmpInst.addOperand(Rs);
1877 TmpInst.addOperand(Imm4);
1878 TmpInst.addOperand(Imm6);
1879 Inst = TmpInst;
1880 break;
1881 }
1882
1883 case Hexagon::S2_tableidxd_goodsyntax: {
1884 MCInst TmpInst;
1885 MCOperand &Rx = Inst.getOperand(0);
1886 MCOperand &_dst_ = Inst.getOperand(1);
1887 MCOperand &Rs = Inst.getOperand(2);
1888 MCOperand &Imm4 = Inst.getOperand(3);
1889 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001890 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001891 Imm6.getExpr(), MCConstantExpr::create(3, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001892 TmpInst.setOpcode(Hexagon::S2_tableidxd);
1893 TmpInst.addOperand(Rx);
1894 TmpInst.addOperand(_dst_);
1895 TmpInst.addOperand(Rs);
1896 TmpInst.addOperand(Imm4);
1897 TmpInst.addOperand(Imm6);
1898 Inst = TmpInst;
1899 break;
1900 }
1901
1902 case Hexagon::M2_mpyui: {
1903 Inst.setOpcode(Hexagon::M2_mpyi);
1904 break;
1905 }
1906 case Hexagon::M2_mpysmi: {
1907 MCInst TmpInst;
1908 MCOperand &Rd = Inst.getOperand(0);
1909 MCOperand &Rs = Inst.getOperand(1);
1910 MCOperand &Imm = Inst.getOperand(2);
1911 int64_t Value;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001912 MCExpr const &Expr = *Imm.getExpr();
1913 bool Absolute = Expr.evaluateAsAbsolute(Value);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001914 assert(Absolute);
1915 (void)Absolute;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001916 if (!HexagonMCInstrInfo::mustExtend(Expr)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001917 if (Value < 0 && Value > -256) {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001918 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001919 MCConstantExpr::create(Value * -1, Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001920 TmpInst.setOpcode(Hexagon::M2_mpysin);
1921 } else if (Value < 256 && Value >= 0)
1922 TmpInst.setOpcode(Hexagon::M2_mpysip);
1923 else
1924 return Match_InvalidOperand;
1925 } else {
1926 if (Value >= 0)
1927 TmpInst.setOpcode(Hexagon::M2_mpysip);
1928 else
1929 return Match_InvalidOperand;
1930 }
1931 TmpInst.addOperand(Rd);
1932 TmpInst.addOperand(Rs);
1933 TmpInst.addOperand(Imm);
1934 Inst = TmpInst;
1935 break;
1936 }
1937
1938 case Hexagon::S2_asr_i_r_rnd_goodsyntax: {
1939 MCOperand &Imm = Inst.getOperand(2);
1940 MCInst TmpInst;
1941 int64_t Value;
1942 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1943 assert(Absolute);
1944 (void)Absolute;
1945 if (Value == 0) { // convert to $Rd = $Rs
1946 TmpInst.setOpcode(Hexagon::A2_tfr);
1947 MCOperand &Rd = Inst.getOperand(0);
1948 MCOperand &Rs = Inst.getOperand(1);
1949 TmpInst.addOperand(Rd);
1950 TmpInst.addOperand(Rs);
1951 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001952 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001953 MCBinaryExpr::createSub(Imm.getExpr(),
1954 MCConstantExpr::create(1, Context), Context),
1955 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001956 TmpInst.setOpcode(Hexagon::S2_asr_i_r_rnd);
1957 MCOperand &Rd = Inst.getOperand(0);
1958 MCOperand &Rs = Inst.getOperand(1);
1959 TmpInst.addOperand(Rd);
1960 TmpInst.addOperand(Rs);
1961 TmpInst.addOperand(Imm);
1962 }
1963 Inst = TmpInst;
1964 break;
1965 }
1966
1967 case Hexagon::S2_asr_i_p_rnd_goodsyntax: {
1968 MCOperand &Rdd = Inst.getOperand(0);
1969 MCOperand &Rss = Inst.getOperand(1);
1970 MCOperand &Imm = Inst.getOperand(2);
1971 int64_t Value;
1972 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1973 assert(Absolute);
1974 (void)Absolute;
1975 if (Value == 0) { // convert to $Rdd = combine ($Rs[0], $Rs[1])
1976 MCInst TmpInst;
1977 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001978 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001979 StringRef Reg1(R1);
1980 Rss.setReg(MatchRegisterName(Reg1));
1981 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001982 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001983 StringRef Reg2(R2);
1984 TmpInst.setOpcode(Hexagon::A2_combinew);
1985 TmpInst.addOperand(Rdd);
1986 TmpInst.addOperand(Rss);
1987 TmpInst.addOperand(MCOperand::createReg(MatchRegisterName(Reg2)));
1988 Inst = TmpInst;
1989 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001990 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001991 MCBinaryExpr::createSub(Imm.getExpr(),
1992 MCConstantExpr::create(1, Context), Context),
1993 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001994 Inst.setOpcode(Hexagon::S2_asr_i_p_rnd);
1995 }
1996 break;
1997 }
1998
1999 case Hexagon::A4_boundscheck: {
2000 MCOperand &Rs = Inst.getOperand(1);
2001 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
2002 if (RegNum & 1) { // Odd mapped to raw:hi, regpair is rodd:odd-1, like r3:2
2003 Inst.setOpcode(Hexagon::A4_boundscheck_hi);
2004 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002005 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002006 StringRef RegPair = Name;
2007 Rs.setReg(MatchRegisterName(RegPair));
2008 } else { // raw:lo
2009 Inst.setOpcode(Hexagon::A4_boundscheck_lo);
2010 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002011 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002012 StringRef RegPair = Name;
2013 Rs.setReg(MatchRegisterName(RegPair));
2014 }
2015 break;
2016 }
2017
2018 case Hexagon::A2_addsp: {
2019 MCOperand &Rs = Inst.getOperand(1);
2020 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
2021 if (RegNum & 1) { // Odd mapped to raw:hi
2022 Inst.setOpcode(Hexagon::A2_addsph);
2023 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002024 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002025 StringRef RegPair = Name;
2026 Rs.setReg(MatchRegisterName(RegPair));
2027 } else { // Even mapped raw:lo
2028 Inst.setOpcode(Hexagon::A2_addspl);
2029 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002030 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002031 StringRef RegPair = Name;
2032 Rs.setReg(MatchRegisterName(RegPair));
2033 }
2034 break;
2035 }
2036
2037 case Hexagon::M2_vrcmpys_s1: {
2038 MCOperand &Rt = Inst.getOperand(2);
2039 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2040 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2041 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_h);
2042 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002043 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002044 StringRef RegPair = Name;
2045 Rt.setReg(MatchRegisterName(RegPair));
2046 } else { // Even mapped sat:raw:lo
2047 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_l);
2048 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002049 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002050 StringRef RegPair = Name;
2051 Rt.setReg(MatchRegisterName(RegPair));
2052 }
2053 break;
2054 }
2055
2056 case Hexagon::M2_vrcmpys_acc_s1: {
2057 MCInst TmpInst;
2058 MCOperand &Rxx = Inst.getOperand(0);
2059 MCOperand &Rss = Inst.getOperand(2);
2060 MCOperand &Rt = Inst.getOperand(3);
2061 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2062 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2063 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_h);
2064 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002065 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002066 StringRef RegPair = Name;
2067 Rt.setReg(MatchRegisterName(RegPair));
2068 } else { // Even mapped sat:raw:lo
2069 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_l);
2070 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002071 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002072 StringRef RegPair = Name;
2073 Rt.setReg(MatchRegisterName(RegPair));
2074 }
2075 // Registers are in different positions
2076 TmpInst.addOperand(Rxx);
2077 TmpInst.addOperand(Rxx);
2078 TmpInst.addOperand(Rss);
2079 TmpInst.addOperand(Rt);
2080 Inst = TmpInst;
2081 break;
2082 }
2083
2084 case Hexagon::M2_vrcmpys_s1rp: {
2085 MCOperand &Rt = Inst.getOperand(2);
2086 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2087 if (RegNum & 1) { // Odd mapped to rnd:sat:raw:hi
2088 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_h);
2089 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002090 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002091 StringRef RegPair = Name;
2092 Rt.setReg(MatchRegisterName(RegPair));
2093 } else { // Even mapped rnd:sat:raw:lo
2094 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_l);
2095 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002096 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002097 StringRef RegPair = Name;
2098 Rt.setReg(MatchRegisterName(RegPair));
2099 }
2100 break;
2101 }
2102
2103 case Hexagon::S5_asrhub_rnd_sat_goodsyntax: {
2104 MCOperand &Imm = Inst.getOperand(2);
2105 int64_t Value;
2106 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
2107 assert(Absolute);
2108 (void)Absolute;
2109 if (Value == 0)
2110 Inst.setOpcode(Hexagon::S2_vsathub);
2111 else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002112 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002113 MCBinaryExpr::createSub(Imm.getExpr(),
2114 MCConstantExpr::create(1, Context), Context),
2115 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002116 Inst.setOpcode(Hexagon::S5_asrhub_rnd_sat);
2117 }
2118 break;
2119 }
2120
2121 case Hexagon::S5_vasrhrnd_goodsyntax: {
2122 MCOperand &Rdd = Inst.getOperand(0);
2123 MCOperand &Rss = Inst.getOperand(1);
2124 MCOperand &Imm = Inst.getOperand(2);
2125 int64_t Value;
2126 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
2127 assert(Absolute);
2128 (void)Absolute;
2129 if (Value == 0) {
2130 MCInst TmpInst;
2131 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00002132 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002133 StringRef Reg1(R1);
2134 Rss.setReg(MatchRegisterName(Reg1));
2135 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00002136 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002137 StringRef Reg2(R2);
2138 TmpInst.setOpcode(Hexagon::A2_combinew);
2139 TmpInst.addOperand(Rdd);
2140 TmpInst.addOperand(Rss);
2141 TmpInst.addOperand(MCOperand::createReg(MatchRegisterName(Reg2)));
2142 Inst = TmpInst;
2143 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002144 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002145 MCBinaryExpr::createSub(Imm.getExpr(),
2146 MCConstantExpr::create(1, Context), Context),
2147 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002148 Inst.setOpcode(Hexagon::S5_vasrhrnd);
2149 }
2150 break;
2151 }
2152
2153 case Hexagon::A2_not: {
2154 MCInst TmpInst;
2155 MCOperand &Rd = Inst.getOperand(0);
2156 MCOperand &Rs = Inst.getOperand(1);
2157 TmpInst.setOpcode(Hexagon::A2_subri);
2158 TmpInst.addOperand(Rd);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002159 TmpInst.addOperand(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002160 HexagonMCExpr::create(MCConstantExpr::create(-1, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002161 TmpInst.addOperand(Rs);
2162 Inst = TmpInst;
2163 break;
2164 }
2165 } // switch
2166
2167 return Match_Success;
2168}