blob: b97707e59c640ed931305f8ab85cbe4c9a9489d7 [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;
Nirav Dave2364748a2016-09-16 18:30:20 +0000117 bool OutOfRange(SMLoc IDLoc, long long Val, long long Max);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000118 int processInstruction(MCInst &Inst, OperandVector const &Operands,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000119 SMLoc IDLoc);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000120
121 // Check if we have an assembler and, if so, set the ELF e_header flags.
122 void chksetELFHeaderEFlags(unsigned flags) {
123 if (getAssembler())
124 getAssembler()->setELFHeaderEFlags(flags);
125 }
126
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +0000127 unsigned matchRegister(StringRef Name);
128
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000129/// @name Auto-generated Match Functions
130/// {
131
132#define GET_ASSEMBLER_HEADER
133#include "HexagonGenAsmMatcher.inc"
134
135 /// }
136
137public:
Akira Hatanakab11ef082015-11-14 06:35:56 +0000138 HexagonAsmParser(const MCSubtargetInfo &_STI, MCAsmParser &_Parser,
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000139 const MCInstrInfo &MII, const MCTargetOptions &Options)
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000140 : MCTargetAsmParser(Options, _STI), Parser(_Parser),
Colin LeMahieuf0af6e52015-11-13 17:42:46 +0000141 MCII (MII), MCB(HexagonMCInstrInfo::createBundle()), InBrackets(false) {
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000142 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000143
144 MCAsmParserExtension::Initialize(_Parser);
145
146 Assembler = nullptr;
147 // FIXME: need better way to detect AsmStreamer (upstream removed getKind())
148 if (!Parser.getStreamer().hasRawTextSupport()) {
149 MCELFStreamer *MES = static_cast<MCELFStreamer *>(&Parser.getStreamer());
150 Assembler = &MES->getAssembler();
151 }
152 }
153
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000154 bool splitIdentifier(OperandVector &Operands);
155 bool parseOperand(OperandVector &Operands);
156 bool parseInstruction(OperandVector &Operands);
157 bool implicitExpressionLocation(OperandVector &Operands);
158 bool parseExpressionOrOperand(OperandVector &Operands);
159 bool parseExpression(MCExpr const *& Expr);
160 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
Colin LeMahieu9ea507e2015-11-09 07:10:24 +0000161 SMLoc NameLoc, OperandVector &Operands) override
162 {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000163 llvm_unreachable("Unimplemented");
164 }
165 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
Colin LeMahieu9ea507e2015-11-09 07:10:24 +0000166 AsmToken ID, OperandVector &Operands) override;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000167
Colin LeMahieu9ea507e2015-11-09 07:10:24 +0000168 virtual bool ParseDirective(AsmToken DirectiveID) override;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000169};
170
171/// HexagonOperand - Instances of this class represent a parsed Hexagon machine
172/// instruction.
173struct HexagonOperand : public MCParsedAsmOperand {
174 enum KindTy { Token, Immediate, Register } Kind;
175
176 SMLoc StartLoc, EndLoc;
177
178 struct TokTy {
179 const char *Data;
180 unsigned Length;
181 };
182
183 struct RegTy {
184 unsigned RegNum;
185 };
186
187 struct ImmTy {
188 const MCExpr *Val;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000189 };
190
191 struct InstTy {
192 OperandVector *SubInsts;
193 };
194
195 union {
196 struct TokTy Tok;
197 struct RegTy Reg;
198 struct ImmTy Imm;
199 };
200
201 HexagonOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
202
203public:
204 HexagonOperand(const HexagonOperand &o) : MCParsedAsmOperand() {
205 Kind = o.Kind;
206 StartLoc = o.StartLoc;
207 EndLoc = o.EndLoc;
208 switch (Kind) {
209 case Register:
210 Reg = o.Reg;
211 break;
212 case Immediate:
213 Imm = o.Imm;
214 break;
215 case Token:
216 Tok = o.Tok;
217 break;
218 }
219 }
220
221 /// getStartLoc - Get the location of the first token of this operand.
222 SMLoc getStartLoc() const { return StartLoc; }
223
224 /// getEndLoc - Get the location of the last token of this operand.
Peter Collingbournecc723cc2016-10-09 04:39:13 +0000225 SMLoc getEndLoc() { return EndLoc; }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000226
227 unsigned getReg() const {
228 assert(Kind == Register && "Invalid access!");
229 return Reg.RegNum;
230 }
231
232 const MCExpr *getImm() const {
233 assert(Kind == Immediate && "Invalid access!");
234 return Imm.Val;
235 }
236
237 bool isToken() const { return Kind == Token; }
238 bool isImm() const { return Kind == Immediate; }
239 bool isMem() const { llvm_unreachable("No isMem"); }
240 bool isReg() const { return Kind == Register; }
241
242 bool CheckImmRange(int immBits, int zeroBits, bool isSigned,
243 bool isRelocatable, bool Extendable) const {
244 if (Kind == Immediate) {
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000245 const MCExpr *myMCExpr = &HexagonMCInstrInfo::getExpr(*getImm());
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000246 if (HexagonMCInstrInfo::mustExtend(*Imm.Val) && !Extendable)
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000247 return false;
248 int64_t Res;
249 if (myMCExpr->evaluateAsAbsolute(Res)) {
250 int bits = immBits + zeroBits;
251 // Field bit range is zerobits + bits
252 // zeroBits must be 0
253 if (Res & ((1 << zeroBits) - 1))
254 return false;
255 if (isSigned) {
256 if (Res < (1LL << (bits - 1)) && Res >= -(1LL << (bits - 1)))
257 return true;
258 } else {
259 if (bits == 64)
260 return true;
261 if (Res >= 0)
262 return ((uint64_t)Res < (uint64_t)(1ULL << bits)) ? true : false;
263 else {
264 const int64_t high_bit_set = 1ULL << 63;
265 const uint64_t mask = (high_bit_set >> (63 - bits));
266 return (((uint64_t)Res & mask) == mask) ? true : false;
267 }
268 }
269 } else if (myMCExpr->getKind() == MCExpr::SymbolRef && isRelocatable)
270 return true;
271 else if (myMCExpr->getKind() == MCExpr::Binary ||
272 myMCExpr->getKind() == MCExpr::Unary)
273 return true;
274 }
275 return false;
276 }
277
278 bool isf32Ext() const { return false; }
279 bool iss32Imm() const { return CheckImmRange(32, 0, true, true, false); }
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000280 bool iss23_2Imm() const { return CheckImmRange(23, 2, true, true, false); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000281 bool iss8Imm() const { return CheckImmRange(8, 0, true, false, false); }
282 bool iss8Imm64() const { return CheckImmRange(8, 0, true, true, false); }
283 bool iss7Imm() const { return CheckImmRange(7, 0, true, false, false); }
284 bool iss6Imm() const { return CheckImmRange(6, 0, true, false, false); }
285 bool iss4Imm() const { return CheckImmRange(4, 0, true, false, false); }
286 bool iss4_0Imm() const { return CheckImmRange(4, 0, true, false, false); }
287 bool iss4_1Imm() const { return CheckImmRange(4, 1, true, false, false); }
288 bool iss4_2Imm() const { return CheckImmRange(4, 2, true, false, false); }
289 bool iss4_3Imm() const { return CheckImmRange(4, 3, true, false, false); }
290 bool iss4_6Imm() const { return CheckImmRange(4, 0, true, false, false); }
291 bool iss3_6Imm() const { return CheckImmRange(3, 0, true, false, false); }
292 bool iss3Imm() const { return CheckImmRange(3, 0, true, false, false); }
293
294 bool isu64Imm() const { return CheckImmRange(64, 0, false, true, true); }
295 bool isu32Imm() const { return CheckImmRange(32, 0, false, true, false); }
296 bool isu26_6Imm() const { return CheckImmRange(26, 6, false, true, false); }
297 bool isu16Imm() const { return CheckImmRange(16, 0, false, true, false); }
298 bool isu16_0Imm() const { return CheckImmRange(16, 0, false, true, false); }
299 bool isu16_1Imm() const { return CheckImmRange(16, 1, false, true, false); }
300 bool isu16_2Imm() const { return CheckImmRange(16, 2, false, true, false); }
301 bool isu16_3Imm() const { return CheckImmRange(16, 3, false, true, false); }
302 bool isu11_3Imm() const { return CheckImmRange(11, 3, false, false, false); }
303 bool isu6_0Imm() const { return CheckImmRange(6, 0, false, false, false); }
304 bool isu6_1Imm() const { return CheckImmRange(6, 1, false, false, false); }
305 bool isu6_2Imm() const { return CheckImmRange(6, 2, false, false, false); }
306 bool isu6_3Imm() const { return CheckImmRange(6, 3, false, false, false); }
307 bool isu10Imm() const { return CheckImmRange(10, 0, false, false, false); }
308 bool isu9Imm() const { return CheckImmRange(9, 0, false, false, false); }
309 bool isu8Imm() const { return CheckImmRange(8, 0, false, false, false); }
310 bool isu7Imm() const { return CheckImmRange(7, 0, false, false, false); }
311 bool isu6Imm() const { return CheckImmRange(6, 0, false, false, false); }
312 bool isu5Imm() const { return CheckImmRange(5, 0, false, false, false); }
313 bool isu4Imm() const { return CheckImmRange(4, 0, false, false, false); }
314 bool isu3Imm() const { return CheckImmRange(3, 0, false, false, false); }
315 bool isu2Imm() const { return CheckImmRange(2, 0, false, false, false); }
316 bool isu1Imm() const { return CheckImmRange(1, 0, false, false, false); }
317
318 bool ism6Imm() const { return CheckImmRange(6, 0, false, false, false); }
319 bool isn8Imm() const { return CheckImmRange(8, 0, false, false, false); }
320
321 bool iss16Ext() const { return CheckImmRange(16 + 26, 0, true, true, true); }
322 bool iss12Ext() const { return CheckImmRange(12 + 26, 0, true, true, true); }
323 bool iss10Ext() const { return CheckImmRange(10 + 26, 0, true, true, true); }
324 bool iss9Ext() const { return CheckImmRange(9 + 26, 0, true, true, true); }
325 bool iss8Ext() const { return CheckImmRange(8 + 26, 0, true, true, true); }
326 bool iss7Ext() const { return CheckImmRange(7 + 26, 0, true, true, true); }
327 bool iss6Ext() const { return CheckImmRange(6 + 26, 0, true, true, true); }
328 bool iss11_0Ext() const {
329 return CheckImmRange(11 + 26, 0, true, true, true);
330 }
331 bool iss11_1Ext() const {
332 return CheckImmRange(11 + 26, 1, true, true, true);
333 }
334 bool iss11_2Ext() const {
335 return CheckImmRange(11 + 26, 2, true, true, true);
336 }
337 bool iss11_3Ext() const {
338 return CheckImmRange(11 + 26, 3, true, true, true);
339 }
340
341 bool isu6Ext() const { return CheckImmRange(6 + 26, 0, false, true, true); }
342 bool isu7Ext() const { return CheckImmRange(7 + 26, 0, false, true, true); }
343 bool isu8Ext() const { return CheckImmRange(8 + 26, 0, false, true, true); }
344 bool isu9Ext() const { return CheckImmRange(9 + 26, 0, false, true, true); }
345 bool isu10Ext() const { return CheckImmRange(10 + 26, 0, false, true, true); }
346 bool isu6_0Ext() const { return CheckImmRange(6 + 26, 0, false, true, true); }
347 bool isu6_1Ext() const { return CheckImmRange(6 + 26, 1, false, true, true); }
348 bool isu6_2Ext() const { return CheckImmRange(6 + 26, 2, false, true, true); }
349 bool isu6_3Ext() const { return CheckImmRange(6 + 26, 3, false, true, true); }
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000350 bool isu32MustExt() const { return isImm(); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000351
352 void addRegOperands(MCInst &Inst, unsigned N) const {
353 assert(N == 1 && "Invalid number of operands!");
354 Inst.addOperand(MCOperand::createReg(getReg()));
355 }
356
357 void addImmOperands(MCInst &Inst, unsigned N) const {
358 assert(N == 1 && "Invalid number of operands!");
359 Inst.addOperand(MCOperand::createExpr(getImm()));
360 }
361
362 void addSignedImmOperands(MCInst &Inst, unsigned N) const {
363 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000364 HexagonMCExpr *Expr =
365 const_cast<HexagonMCExpr *>(cast<HexagonMCExpr>(getImm()));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000366 int64_t Value;
367 if (!Expr->evaluateAsAbsolute(Value)) {
368 Inst.addOperand(MCOperand::createExpr(Expr));
369 return;
370 }
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000371 int64_t Extended = SignExtend64(Value, 32);
372 if ((Extended < 0) != (Value < 0))
373 Expr->setSignMismatch();
374 Inst.addOperand(MCOperand::createExpr(Expr));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000375 }
376
377 void addf32ExtOperands(MCInst &Inst, unsigned N) const {
378 addImmOperands(Inst, N);
379 }
380
381 void adds32ImmOperands(MCInst &Inst, unsigned N) const {
382 addSignedImmOperands(Inst, N);
383 }
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000384 void adds23_2ImmOperands(MCInst &Inst, unsigned N) const {
385 addSignedImmOperands(Inst, N);
386 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000387 void adds8ImmOperands(MCInst &Inst, unsigned N) const {
388 addSignedImmOperands(Inst, N);
389 }
390 void adds8Imm64Operands(MCInst &Inst, unsigned N) const {
391 addSignedImmOperands(Inst, N);
392 }
393 void adds6ImmOperands(MCInst &Inst, unsigned N) const {
394 addSignedImmOperands(Inst, N);
395 }
396 void adds4ImmOperands(MCInst &Inst, unsigned N) const {
397 addSignedImmOperands(Inst, N);
398 }
399 void adds4_0ImmOperands(MCInst &Inst, unsigned N) const {
400 addSignedImmOperands(Inst, N);
401 }
402 void adds4_1ImmOperands(MCInst &Inst, unsigned N) const {
403 addSignedImmOperands(Inst, N);
404 }
405 void adds4_2ImmOperands(MCInst &Inst, unsigned N) const {
406 addSignedImmOperands(Inst, N);
407 }
408 void adds4_3ImmOperands(MCInst &Inst, unsigned N) const {
409 addSignedImmOperands(Inst, N);
410 }
411 void adds3ImmOperands(MCInst &Inst, unsigned N) const {
412 addSignedImmOperands(Inst, N);
413 }
414
415 void addu64ImmOperands(MCInst &Inst, unsigned N) const {
416 addImmOperands(Inst, N);
417 }
418 void addu32ImmOperands(MCInst &Inst, unsigned N) const {
419 addImmOperands(Inst, N);
420 }
421 void addu26_6ImmOperands(MCInst &Inst, unsigned N) const {
422 addImmOperands(Inst, N);
423 }
424 void addu16ImmOperands(MCInst &Inst, unsigned N) const {
425 addImmOperands(Inst, N);
426 }
427 void addu16_0ImmOperands(MCInst &Inst, unsigned N) const {
428 addImmOperands(Inst, N);
429 }
430 void addu16_1ImmOperands(MCInst &Inst, unsigned N) const {
431 addImmOperands(Inst, N);
432 }
433 void addu16_2ImmOperands(MCInst &Inst, unsigned N) const {
434 addImmOperands(Inst, N);
435 }
436 void addu16_3ImmOperands(MCInst &Inst, unsigned N) const {
437 addImmOperands(Inst, N);
438 }
439 void addu11_3ImmOperands(MCInst &Inst, unsigned N) const {
440 addImmOperands(Inst, N);
441 }
442 void addu10ImmOperands(MCInst &Inst, unsigned N) const {
443 addImmOperands(Inst, N);
444 }
445 void addu9ImmOperands(MCInst &Inst, unsigned N) const {
446 addImmOperands(Inst, N);
447 }
448 void addu8ImmOperands(MCInst &Inst, unsigned N) const {
449 addImmOperands(Inst, N);
450 }
451 void addu7ImmOperands(MCInst &Inst, unsigned N) const {
452 addImmOperands(Inst, N);
453 }
454 void addu6ImmOperands(MCInst &Inst, unsigned N) const {
455 addImmOperands(Inst, N);
456 }
457 void addu6_0ImmOperands(MCInst &Inst, unsigned N) const {
458 addImmOperands(Inst, N);
459 }
460 void addu6_1ImmOperands(MCInst &Inst, unsigned N) const {
461 addImmOperands(Inst, N);
462 }
463 void addu6_2ImmOperands(MCInst &Inst, unsigned N) const {
464 addImmOperands(Inst, N);
465 }
466 void addu6_3ImmOperands(MCInst &Inst, unsigned N) const {
467 addImmOperands(Inst, N);
468 }
469 void addu5ImmOperands(MCInst &Inst, unsigned N) const {
470 addImmOperands(Inst, N);
471 }
472 void addu4ImmOperands(MCInst &Inst, unsigned N) const {
473 addImmOperands(Inst, N);
474 }
475 void addu3ImmOperands(MCInst &Inst, unsigned N) const {
476 addImmOperands(Inst, N);
477 }
478 void addu2ImmOperands(MCInst &Inst, unsigned N) const {
479 addImmOperands(Inst, N);
480 }
481 void addu1ImmOperands(MCInst &Inst, unsigned N) const {
482 addImmOperands(Inst, N);
483 }
484
485 void addm6ImmOperands(MCInst &Inst, unsigned N) const {
486 addImmOperands(Inst, N);
487 }
488 void addn8ImmOperands(MCInst &Inst, unsigned N) const {
489 addImmOperands(Inst, N);
490 }
491
492 void adds16ExtOperands(MCInst &Inst, unsigned N) const {
493 addSignedImmOperands(Inst, N);
494 }
495 void adds12ExtOperands(MCInst &Inst, unsigned N) const {
496 addSignedImmOperands(Inst, N);
497 }
498 void adds10ExtOperands(MCInst &Inst, unsigned N) const {
499 addSignedImmOperands(Inst, N);
500 }
501 void adds9ExtOperands(MCInst &Inst, unsigned N) const {
502 addSignedImmOperands(Inst, N);
503 }
504 void adds8ExtOperands(MCInst &Inst, unsigned N) const {
505 addSignedImmOperands(Inst, N);
506 }
507 void adds6ExtOperands(MCInst &Inst, unsigned N) const {
508 addSignedImmOperands(Inst, N);
509 }
510 void adds11_0ExtOperands(MCInst &Inst, unsigned N) const {
511 addSignedImmOperands(Inst, N);
512 }
513 void adds11_1ExtOperands(MCInst &Inst, unsigned N) const {
514 addSignedImmOperands(Inst, N);
515 }
516 void adds11_2ExtOperands(MCInst &Inst, unsigned N) const {
517 addSignedImmOperands(Inst, N);
518 }
519 void adds11_3ExtOperands(MCInst &Inst, unsigned N) const {
520 addSignedImmOperands(Inst, N);
521 }
522
523 void addu6ExtOperands(MCInst &Inst, unsigned N) const {
524 addImmOperands(Inst, N);
525 }
526 void addu7ExtOperands(MCInst &Inst, unsigned N) const {
527 addImmOperands(Inst, N);
528 }
529 void addu8ExtOperands(MCInst &Inst, unsigned N) const {
530 addImmOperands(Inst, N);
531 }
532 void addu9ExtOperands(MCInst &Inst, unsigned N) const {
533 addImmOperands(Inst, N);
534 }
535 void addu10ExtOperands(MCInst &Inst, unsigned N) const {
536 addImmOperands(Inst, N);
537 }
538 void addu6_0ExtOperands(MCInst &Inst, unsigned N) const {
539 addImmOperands(Inst, N);
540 }
541 void addu6_1ExtOperands(MCInst &Inst, unsigned N) const {
542 addImmOperands(Inst, N);
543 }
544 void addu6_2ExtOperands(MCInst &Inst, unsigned N) const {
545 addImmOperands(Inst, N);
546 }
547 void addu6_3ExtOperands(MCInst &Inst, unsigned N) const {
548 addImmOperands(Inst, N);
549 }
550 void addu32MustExtOperands(MCInst &Inst, unsigned N) const {
551 addImmOperands(Inst, N);
552 }
553
554 void adds4_6ImmOperands(MCInst &Inst, unsigned N) const {
555 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000556 const MCConstantExpr *CE =
557 dyn_cast<MCConstantExpr>(&HexagonMCInstrInfo::getExpr(*getImm()));
Colin LeMahieu4c606e62015-12-04 15:48:45 +0000558 Inst.addOperand(MCOperand::createImm(CE->getValue() * 64));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000559 }
560
561 void adds3_6ImmOperands(MCInst &Inst, unsigned N) const {
562 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000563 const MCConstantExpr *CE =
564 dyn_cast<MCConstantExpr>(&HexagonMCInstrInfo::getExpr(*getImm()));
Colin LeMahieu4c606e62015-12-04 15:48:45 +0000565 Inst.addOperand(MCOperand::createImm(CE->getValue() * 64));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000566 }
567
568 StringRef getToken() const {
569 assert(Kind == Token && "Invalid access!");
570 return StringRef(Tok.Data, Tok.Length);
571 }
572
573 virtual void print(raw_ostream &OS) const;
574
575 static std::unique_ptr<HexagonOperand> CreateToken(StringRef Str, SMLoc S) {
576 HexagonOperand *Op = new HexagonOperand(Token);
577 Op->Tok.Data = Str.data();
578 Op->Tok.Length = Str.size();
579 Op->StartLoc = S;
580 Op->EndLoc = S;
581 return std::unique_ptr<HexagonOperand>(Op);
582 }
583
584 static std::unique_ptr<HexagonOperand> CreateReg(unsigned RegNum, SMLoc S,
585 SMLoc E) {
586 HexagonOperand *Op = new HexagonOperand(Register);
587 Op->Reg.RegNum = RegNum;
588 Op->StartLoc = S;
589 Op->EndLoc = E;
590 return std::unique_ptr<HexagonOperand>(Op);
591 }
592
593 static std::unique_ptr<HexagonOperand> CreateImm(const MCExpr *Val, SMLoc S,
594 SMLoc E) {
595 HexagonOperand *Op = new HexagonOperand(Immediate);
596 Op->Imm.Val = Val;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000597 Op->StartLoc = S;
598 Op->EndLoc = E;
599 return std::unique_ptr<HexagonOperand>(Op);
600 }
601};
602
603} // end anonymous namespace.
604
605void HexagonOperand::print(raw_ostream &OS) const {
606 switch (Kind) {
607 case Immediate:
608 getImm()->print(OS, nullptr);
609 break;
610 case Register:
611 OS << "<register R";
612 OS << getReg() << ">";
613 break;
614 case Token:
615 OS << "'" << getToken() << "'";
616 break;
617 }
618}
619
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000620bool HexagonAsmParser::finishBundle(SMLoc IDLoc, MCStreamer &Out) {
621 DEBUG(dbgs() << "Bundle:");
622 DEBUG(MCB.dump_pretty(dbgs()));
623 DEBUG(dbgs() << "--\n");
624
625 // Check the bundle for errors.
626 const MCRegisterInfo *RI = getContext().getRegisterInfo();
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000627 HexagonMCChecker Check(MCII, getSTI(), MCB, MCB, *RI);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000628
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000629 bool CheckOk = HexagonMCInstrInfo::canonicalizePacket(MCII, getSTI(),
630 getContext(), MCB,
631 &Check);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000632
633 while (Check.getNextErrInfo() == true) {
634 unsigned Reg = Check.getErrRegister();
635 Twine R(RI->getName(Reg));
636
637 uint64_t Err = Check.getError();
638 if (Err != HexagonMCErrInfo::CHECK_SUCCESS) {
639 if (HexagonMCErrInfo::CHECK_ERROR_BRANCHES & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000640 return Error(
641 IDLoc,
642 "unconditional branch cannot precede another branch in packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000643
644 if (HexagonMCErrInfo::CHECK_ERROR_NEWP & Err ||
645 HexagonMCErrInfo::CHECK_ERROR_NEWV & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000646 return Error(IDLoc, "register `" + R +
647 "' used with `.new' "
648 "but not validly modified in the same packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000649
650 if (HexagonMCErrInfo::CHECK_ERROR_REGISTERS & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000651 return Error(IDLoc, "register `" + R + "' modified more than once");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000652
653 if (HexagonMCErrInfo::CHECK_ERROR_READONLY & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000654 return Error(IDLoc, "cannot write to read-only register `" + R + "'");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000655
656 if (HexagonMCErrInfo::CHECK_ERROR_LOOP & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000657 return Error(IDLoc, "loop-setup and some branch instructions "
658 "cannot be in the same packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000659
660 if (HexagonMCErrInfo::CHECK_ERROR_ENDLOOP & Err) {
661 Twine N(HexagonMCInstrInfo::isInnerLoop(MCB) ? '0' : '1');
Nirav Dave2364748a2016-09-16 18:30:20 +0000662 return Error(IDLoc,
663 "packet marked with `:endloop" + N + "' " +
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000664 "cannot contain instructions that modify register " +
665 "`" + R + "'");
666 }
667
668 if (HexagonMCErrInfo::CHECK_ERROR_SOLO & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000669 return Error(
670 IDLoc,
671 "instruction cannot appear in packet with other instructions");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000672
673 if (HexagonMCErrInfo::CHECK_ERROR_NOSLOTS & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000674 return Error(IDLoc, "too many slots used in packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000675
676 if (Err & HexagonMCErrInfo::CHECK_ERROR_SHUFFLE) {
677 uint64_t Erm = Check.getShuffleError();
678
679 if (HexagonShuffler::SHUFFLE_ERROR_INVALID == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000680 return Error(IDLoc, "invalid instruction packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000681 else if (HexagonShuffler::SHUFFLE_ERROR_STORES == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000682 return Error(IDLoc, "invalid instruction packet: too many stores");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000683 else if (HexagonShuffler::SHUFFLE_ERROR_LOADS == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000684 return Error(IDLoc, "invalid instruction packet: too many loads");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000685 else if (HexagonShuffler::SHUFFLE_ERROR_BRANCHES == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000686 return Error(IDLoc, "too many branches in packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000687 else if (HexagonShuffler::SHUFFLE_ERROR_NOSLOTS == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000688 return Error(IDLoc, "invalid instruction packet: out of slots");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000689 else if (HexagonShuffler::SHUFFLE_ERROR_SLOTS == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000690 return Error(IDLoc, "invalid instruction packet: slot error");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000691 else if (HexagonShuffler::SHUFFLE_ERROR_ERRATA2 == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000692 return Error(IDLoc, "v60 packet violation");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000693 else if (HexagonShuffler::SHUFFLE_ERROR_STORE_LOAD_CONFLICT == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000694 return Error(IDLoc, "slot 0 instruction does not allow slot 1 store");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000695 else
Nirav Dave2364748a2016-09-16 18:30:20 +0000696 return Error(IDLoc, "unknown error in instruction packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000697 }
698 }
699
700 unsigned Warn = Check.getWarning();
701 if (Warn != HexagonMCErrInfo::CHECK_SUCCESS) {
702 if (HexagonMCErrInfo::CHECK_WARN_CURRENT & Warn)
703 Warning(IDLoc, "register `" + R + "' used with `.cur' "
704 "but not used in the same packet");
705 else if (HexagonMCErrInfo::CHECK_WARN_TEMPORARY & Warn)
706 Warning(IDLoc, "register `" + R + "' used with `.tmp' "
707 "but not used in the same packet");
708 }
709 }
710
711 if (CheckOk) {
712 MCB.setLoc(IDLoc);
713 if (HexagonMCInstrInfo::bundleSize(MCB) == 0) {
714 assert(!HexagonMCInstrInfo::isInnerLoop(MCB));
715 assert(!HexagonMCInstrInfo::isOuterLoop(MCB));
716 // Empty packets are valid yet aren't emitted
717 return false;
718 }
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000719 Out.EmitInstruction(MCB, getSTI());
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000720 } else {
721 // If compounding and duplexing didn't reduce the size below
722 // 4 or less we have a packet that is too big.
723 if (HexagonMCInstrInfo::bundleSize(MCB) > HEXAGON_PACKET_SIZE) {
724 Error(IDLoc, "invalid instruction packet: out of slots");
725 return true; // Error
726 }
727 }
728
729 return false; // No error
730}
731
732bool HexagonAsmParser::matchBundleOptions() {
733 MCAsmParser &Parser = getParser();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000734 while (true) {
735 if (!Parser.getTok().is(AsmToken::Colon))
736 return false;
Nirav Davefd910412016-06-17 16:06:17 +0000737 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000738 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;
Nirav Davefd910412016-06-17 16:06:17 +0000749 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000750 }
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
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000822bool HexagonAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
823 OperandVector &Operands,
824 MCStreamer &Out,
825 uint64_t &ErrorInfo,
826 bool MatchingInlineAsm) {
827 if (!InBrackets) {
828 MCB.clear();
829 MCB.addOperand(MCOperand::createImm(0));
830 }
831 HexagonOperand &FirstOperand = static_cast<HexagonOperand &>(*Operands[0]);
832 if (FirstOperand.isToken() && FirstOperand.getToken() == "{") {
833 assert(Operands.size() == 1 && "Brackets should be by themselves");
834 if (InBrackets) {
835 getParser().Error(IDLoc, "Already in a packet");
836 return true;
837 }
838 InBrackets = true;
839 return false;
840 }
841 if (FirstOperand.isToken() && FirstOperand.getToken() == "}") {
842 assert(Operands.size() == 1 && "Brackets should be by themselves");
843 if (!InBrackets) {
844 getParser().Error(IDLoc, "Not in a packet");
845 return true;
846 }
847 InBrackets = false;
848 if (matchBundleOptions())
849 return true;
850 return finishBundle(IDLoc, Out);
851 }
852 MCInst *SubInst = new (getParser().getContext()) MCInst;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000853 if (matchOneInstruction(*SubInst, IDLoc, Operands, ErrorInfo,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000854 MatchingInlineAsm))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000855 return true;
856 HexagonMCInstrInfo::extendIfNeeded(
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000857 getParser().getContext(), MCII, MCB, *SubInst);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000858 MCB.addOperand(MCOperand::createInst(SubInst));
859 if (!InBrackets)
860 return finishBundle(IDLoc, Out);
861 return false;
862}
863
864/// ParseDirective parses the Hexagon specific directives
865bool HexagonAsmParser::ParseDirective(AsmToken DirectiveID) {
866 StringRef IDVal = DirectiveID.getIdentifier();
867 if ((IDVal.lower() == ".word") || (IDVal.lower() == ".4byte"))
868 return ParseDirectiveValue(4, DirectiveID.getLoc());
869 if (IDVal.lower() == ".short" || IDVal.lower() == ".hword" ||
870 IDVal.lower() == ".half")
871 return ParseDirectiveValue(2, DirectiveID.getLoc());
872 if (IDVal.lower() == ".falign")
873 return ParseDirectiveFalign(256, DirectiveID.getLoc());
874 if ((IDVal.lower() == ".lcomm") || (IDVal.lower() == ".lcommon"))
875 return ParseDirectiveComm(true, DirectiveID.getLoc());
876 if ((IDVal.lower() == ".comm") || (IDVal.lower() == ".common"))
877 return ParseDirectiveComm(false, DirectiveID.getLoc());
878 if (IDVal.lower() == ".subsection")
879 return ParseDirectiveSubsection(DirectiveID.getLoc());
880
881 return true;
882}
883bool HexagonAsmParser::ParseDirectiveSubsection(SMLoc L) {
884 const MCExpr *Subsection = 0;
885 int64_t Res;
886
887 assert((getLexer().isNot(AsmToken::EndOfStatement)) &&
888 "Invalid subsection directive");
889 getParser().parseExpression(Subsection);
890
891 if (!Subsection->evaluateAsAbsolute(Res))
892 return Error(L, "Cannot evaluate subsection number");
893
894 if (getLexer().isNot(AsmToken::EndOfStatement))
895 return TokError("unexpected token in directive");
896
897 // 0-8192 is the hard-coded range in MCObjectStreamper.cpp, this keeps the
898 // negative subsections together and in the same order but at the opposite
899 // end of the section. Only legacy hexagon-gcc created assembly code
900 // used negative subsections.
901 if ((Res < 0) && (Res > -8193))
Colin LeMahieuc7b21242016-02-15 18:47:55 +0000902 Subsection = HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000903 MCConstantExpr::create(8192 + Res, getContext()), getContext());
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000904
905 getStreamer().SubSection(Subsection);
906 return false;
907}
908
909/// ::= .falign [expression]
910bool HexagonAsmParser::ParseDirectiveFalign(unsigned Size, SMLoc L) {
911
912 int64_t MaxBytesToFill = 15;
913
914 // if there is an arguement
915 if (getLexer().isNot(AsmToken::EndOfStatement)) {
916 const MCExpr *Value;
917 SMLoc ExprLoc = L;
918
919 // Make sure we have a number (false is returned if expression is a number)
920 if (getParser().parseExpression(Value) == false) {
921 // Make sure this is a number that is in range
922 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
923 uint64_t IntValue = MCE->getValue();
924 if (!isUIntN(Size, IntValue) && !isIntN(Size, IntValue))
925 return Error(ExprLoc, "literal value out of range (256) for falign");
926 MaxBytesToFill = IntValue;
927 Lex();
928 } else {
929 return Error(ExprLoc, "not a valid expression for falign directive");
930 }
931 }
932
933 getTargetStreamer().emitFAlign(16, MaxBytesToFill);
934 Lex();
935
936 return false;
937}
938
939/// ::= .word [ expression (, expression)* ]
940bool HexagonAsmParser::ParseDirectiveValue(unsigned Size, SMLoc L) {
941 if (getLexer().isNot(AsmToken::EndOfStatement)) {
942
943 for (;;) {
944 const MCExpr *Value;
945 SMLoc ExprLoc = L;
946 if (getParser().parseExpression(Value))
947 return true;
948
949 // Special case constant expressions to match code generator.
950 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
951 assert(Size <= 8 && "Invalid size");
952 uint64_t IntValue = MCE->getValue();
953 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
954 return Error(ExprLoc, "literal value out of range for directive");
955 getStreamer().EmitIntValue(IntValue, Size);
956 } else
957 getStreamer().EmitValue(Value, Size);
958
959 if (getLexer().is(AsmToken::EndOfStatement))
960 break;
961
962 // FIXME: Improve diagnostic.
963 if (getLexer().isNot(AsmToken::Comma))
964 return TokError("unexpected token in directive");
965 Lex();
966 }
967 }
968
969 Lex();
970 return false;
971}
972
973// This is largely a copy of AsmParser's ParseDirectiveComm extended to
974// accept a 3rd argument, AccessAlignment which indicates the smallest
975// memory access made to the symbol, expressed in bytes. If no
976// AccessAlignment is specified it defaults to the Alignment Value.
977// Hexagon's .lcomm:
978// .lcomm Symbol, Length, Alignment, AccessAlignment
979bool HexagonAsmParser::ParseDirectiveComm(bool IsLocal, SMLoc Loc) {
980 // FIXME: need better way to detect if AsmStreamer (upstream removed
981 // getKind())
982 if (getStreamer().hasRawTextSupport())
983 return true; // Only object file output requires special treatment.
984
985 StringRef Name;
986 if (getParser().parseIdentifier(Name))
987 return TokError("expected identifier in directive");
988 // Handle the identifier as the key symbol.
989 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
990
991 if (getLexer().isNot(AsmToken::Comma))
992 return TokError("unexpected token in directive");
993 Lex();
994
995 int64_t Size;
996 SMLoc SizeLoc = getLexer().getLoc();
997 if (getParser().parseAbsoluteExpression(Size))
998 return true;
999
1000 int64_t ByteAlignment = 1;
1001 SMLoc ByteAlignmentLoc;
1002 if (getLexer().is(AsmToken::Comma)) {
1003 Lex();
1004 ByteAlignmentLoc = getLexer().getLoc();
1005 if (getParser().parseAbsoluteExpression(ByteAlignment))
1006 return true;
1007 if (!isPowerOf2_64(ByteAlignment))
1008 return Error(ByteAlignmentLoc, "alignment must be a power of 2");
1009 }
1010
1011 int64_t AccessAlignment = 0;
1012 if (getLexer().is(AsmToken::Comma)) {
1013 // The optional access argument specifies the size of the smallest memory
1014 // access to be made to the symbol, expressed in bytes.
1015 SMLoc AccessAlignmentLoc;
1016 Lex();
1017 AccessAlignmentLoc = getLexer().getLoc();
1018 if (getParser().parseAbsoluteExpression(AccessAlignment))
1019 return true;
1020
1021 if (!isPowerOf2_64(AccessAlignment))
1022 return Error(AccessAlignmentLoc, "access alignment must be a power of 2");
1023 }
1024
1025 if (getLexer().isNot(AsmToken::EndOfStatement))
1026 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1027
1028 Lex();
1029
1030 // NOTE: a size of zero for a .comm should create a undefined symbol
1031 // but a size of .lcomm creates a bss symbol of size zero.
1032 if (Size < 0)
1033 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1034 "be less than zero");
1035
1036 // NOTE: The alignment in the directive is a power of 2 value, the assembler
1037 // may internally end up wanting an alignment in bytes.
1038 // FIXME: Diagnose overflow.
1039 if (ByteAlignment < 0)
1040 return Error(ByteAlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1041 "alignment, can't be less than zero");
1042
1043 if (!Sym->isUndefined())
1044 return Error(Loc, "invalid symbol redefinition");
1045
1046 HexagonMCELFStreamer &HexagonELFStreamer =
1047 static_cast<HexagonMCELFStreamer &>(getStreamer());
1048 if (IsLocal) {
1049 HexagonELFStreamer.HexagonMCEmitLocalCommonSymbol(Sym, Size, ByteAlignment,
1050 AccessAlignment);
1051 return false;
1052 }
1053
1054 HexagonELFStreamer.HexagonMCEmitCommonSymbol(Sym, Size, ByteAlignment,
1055 AccessAlignment);
1056 return false;
1057}
1058
1059// validate register against architecture
1060bool HexagonAsmParser::RegisterMatchesArch(unsigned MatchNum) const {
1061 return true;
1062}
1063
1064// extern "C" void LLVMInitializeHexagonAsmLexer();
1065
1066/// Force static initialization.
1067extern "C" void LLVMInitializeHexagonAsmParser() {
1068 RegisterMCAsmParser<HexagonAsmParser> X(TheHexagonTarget);
1069}
1070
1071#define GET_MATCHER_IMPLEMENTATION
1072#define GET_REGISTER_MATCHER
1073#include "HexagonGenAsmMatcher.inc"
1074
1075namespace {
1076bool previousEqual(OperandVector &Operands, size_t Index, StringRef String) {
1077 if (Index >= Operands.size())
1078 return false;
1079 MCParsedAsmOperand &Operand = *Operands[Operands.size() - Index - 1];
1080 if (!Operand.isToken())
1081 return false;
1082 return static_cast<HexagonOperand &>(Operand).getToken().equals_lower(String);
1083}
1084bool previousIsLoop(OperandVector &Operands, size_t Index) {
1085 return previousEqual(Operands, Index, "loop0") ||
1086 previousEqual(Operands, Index, "loop1") ||
1087 previousEqual(Operands, Index, "sp1loop0") ||
1088 previousEqual(Operands, Index, "sp2loop0") ||
1089 previousEqual(Operands, Index, "sp3loop0");
1090}
1091}
1092
1093bool HexagonAsmParser::splitIdentifier(OperandVector &Operands) {
1094 AsmToken const &Token = getParser().getTok();
1095 StringRef String = Token.getString();
1096 SMLoc Loc = Token.getLoc();
Nirav Davefd910412016-06-17 16:06:17 +00001097 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001098 do {
1099 std::pair<StringRef, StringRef> HeadTail = String.split('.');
1100 if (!HeadTail.first.empty())
1101 Operands.push_back(HexagonOperand::CreateToken(HeadTail.first, Loc));
1102 if (!HeadTail.second.empty())
1103 Operands.push_back(HexagonOperand::CreateToken(
1104 String.substr(HeadTail.first.size(), 1), Loc));
1105 String = HeadTail.second;
1106 } while (!String.empty());
1107 return false;
1108}
1109
1110bool HexagonAsmParser::parseOperand(OperandVector &Operands) {
1111 unsigned Register;
1112 SMLoc Begin;
1113 SMLoc End;
1114 MCAsmLexer &Lexer = getLexer();
1115 if (!ParseRegister(Register, Begin, End)) {
1116 if (!ErrorMissingParenthesis)
1117 switch (Register) {
1118 default:
1119 break;
1120 case Hexagon::P0:
1121 case Hexagon::P1:
1122 case Hexagon::P2:
1123 case Hexagon::P3:
1124 if (previousEqual(Operands, 0, "if")) {
1125 if (WarnMissingParenthesis)
1126 Warning (Begin, "Missing parenthesis around predicate register");
1127 static char const *LParen = "(";
1128 static char const *RParen = ")";
1129 Operands.push_back(HexagonOperand::CreateToken(LParen, Begin));
1130 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End));
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +00001131 const AsmToken &MaybeDotNew = Lexer.getTok();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001132 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) &&
1133 MaybeDotNew.getString().equals_lower(".new"))
1134 splitIdentifier(Operands);
1135 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin));
1136 return false;
1137 }
1138 if (previousEqual(Operands, 0, "!") &&
1139 previousEqual(Operands, 1, "if")) {
1140 if (WarnMissingParenthesis)
1141 Warning (Begin, "Missing parenthesis around predicate register");
1142 static char const *LParen = "(";
1143 static char const *RParen = ")";
1144 Operands.insert(Operands.end () - 1,
1145 HexagonOperand::CreateToken(LParen, Begin));
1146 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End));
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +00001147 const AsmToken &MaybeDotNew = Lexer.getTok();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001148 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) &&
1149 MaybeDotNew.getString().equals_lower(".new"))
1150 splitIdentifier(Operands);
1151 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin));
1152 return false;
1153 }
1154 break;
1155 }
1156 Operands.push_back(HexagonOperand::CreateReg(
1157 Register, Begin, End));
1158 return false;
1159 }
1160 return splitIdentifier(Operands);
1161}
1162
1163bool HexagonAsmParser::isLabel(AsmToken &Token) {
1164 MCAsmLexer &Lexer = getLexer();
1165 AsmToken const &Second = Lexer.getTok();
1166 AsmToken Third = Lexer.peekTok();
1167 StringRef String = Token.getString();
1168 if (Token.is(AsmToken::TokenKind::LCurly) ||
1169 Token.is(AsmToken::TokenKind::RCurly))
1170 return false;
1171 if (!Token.is(AsmToken::TokenKind::Identifier))
1172 return true;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001173 if (!matchRegister(String.lower()))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001174 return true;
Colin LeMahieu9d851f02015-11-09 21:06:28 +00001175 (void)Second;
1176 assert(Second.is(AsmToken::Colon));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001177 StringRef Raw (String.data(), Third.getString().data() - String.data() +
1178 Third.getString().size());
1179 std::string Collapsed = Raw;
David Majnemerc7004902016-08-12 04:32:37 +00001180 Collapsed.erase(remove_if(Collapsed, isspace), Collapsed.end());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001181 StringRef Whole = Collapsed;
1182 std::pair<StringRef, StringRef> DotSplit = Whole.split('.');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001183 if (!matchRegister(DotSplit.first.lower()))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001184 return true;
1185 return false;
1186}
1187
1188bool HexagonAsmParser::handleNoncontigiousRegister(bool Contigious, SMLoc &Loc) {
1189 if (!Contigious && ErrorNoncontigiousRegister) {
1190 Error(Loc, "Register name is not contigious");
1191 return true;
1192 }
1193 if (!Contigious && WarnNoncontigiousRegister)
1194 Warning(Loc, "Register name is not contigious");
1195 return false;
1196}
1197
1198bool HexagonAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) {
1199 MCAsmLexer &Lexer = getLexer();
1200 StartLoc = getLexer().getLoc();
1201 SmallVector<AsmToken, 5> Lookahead;
1202 StringRef RawString(Lexer.getTok().getString().data(), 0);
1203 bool Again = Lexer.is(AsmToken::Identifier);
1204 bool NeededWorkaround = false;
1205 while (Again) {
1206 AsmToken const &Token = Lexer.getTok();
1207 RawString = StringRef(RawString.data(),
1208 Token.getString().data() - RawString.data () +
1209 Token.getString().size());
1210 Lookahead.push_back(Token);
1211 Lexer.Lex();
1212 bool Contigious = Lexer.getTok().getString().data() ==
1213 Lookahead.back().getString().data() +
1214 Lookahead.back().getString().size();
1215 bool Type = Lexer.is(AsmToken::Identifier) || Lexer.is(AsmToken::Dot) ||
1216 Lexer.is(AsmToken::Integer) || Lexer.is(AsmToken::Real) ||
1217 Lexer.is(AsmToken::Colon);
1218 bool Workaround = Lexer.is(AsmToken::Colon) ||
1219 Lookahead.back().is(AsmToken::Colon);
1220 Again = (Contigious && Type) || (Workaround && Type);
1221 NeededWorkaround = NeededWorkaround || (Again && !(Contigious && Type));
1222 }
1223 std::string Collapsed = RawString;
David Majnemerc7004902016-08-12 04:32:37 +00001224 Collapsed.erase(remove_if(Collapsed, isspace), Collapsed.end());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001225 StringRef FullString = Collapsed;
1226 std::pair<StringRef, StringRef> DotSplit = FullString.split('.');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001227 unsigned DotReg = matchRegister(DotSplit.first.lower());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001228 if (DotReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1229 if (DotSplit.second.empty()) {
1230 RegNo = DotReg;
1231 EndLoc = Lexer.getLoc();
1232 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1233 return true;
1234 return false;
1235 } else {
1236 RegNo = DotReg;
1237 size_t First = RawString.find('.');
1238 StringRef DotString (RawString.data() + First, RawString.size() - First);
1239 Lexer.UnLex(AsmToken(AsmToken::Identifier, DotString));
1240 EndLoc = Lexer.getLoc();
1241 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1242 return true;
1243 return false;
1244 }
1245 }
1246 std::pair<StringRef, StringRef> ColonSplit = StringRef(FullString).split(':');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001247 unsigned ColonReg = matchRegister(ColonSplit.first.lower());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001248 if (ColonReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1249 Lexer.UnLex(Lookahead.back());
1250 Lookahead.pop_back();
1251 Lexer.UnLex(Lookahead.back());
1252 Lookahead.pop_back();
1253 RegNo = ColonReg;
1254 EndLoc = Lexer.getLoc();
1255 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1256 return true;
1257 return false;
1258 }
1259 while (!Lookahead.empty()) {
1260 Lexer.UnLex(Lookahead.back());
1261 Lookahead.pop_back();
1262 }
1263 return true;
1264}
1265
1266bool HexagonAsmParser::implicitExpressionLocation(OperandVector &Operands) {
1267 if (previousEqual(Operands, 0, "call"))
1268 return true;
1269 if (previousEqual(Operands, 0, "jump"))
1270 if (!getLexer().getTok().is(AsmToken::Colon))
1271 return true;
1272 if (previousEqual(Operands, 0, "(") && previousIsLoop(Operands, 1))
1273 return true;
1274 if (previousEqual(Operands, 1, ":") && previousEqual(Operands, 2, "jump") &&
1275 (previousEqual(Operands, 0, "nt") || previousEqual(Operands, 0, "t")))
1276 return true;
1277 return false;
1278}
1279
1280bool HexagonAsmParser::parseExpression(MCExpr const *& Expr) {
1281 llvm::SmallVector<AsmToken, 4> Tokens;
1282 MCAsmLexer &Lexer = getLexer();
1283 bool Done = false;
1284 static char const * Comma = ",";
1285 do {
1286 Tokens.emplace_back (Lexer.getTok());
Nirav Davefd910412016-06-17 16:06:17 +00001287 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001288 switch (Tokens.back().getKind())
1289 {
1290 case AsmToken::TokenKind::Hash:
1291 if (Tokens.size () > 1)
1292 if ((Tokens.end () - 2)->getKind() == AsmToken::TokenKind::Plus) {
1293 Tokens.insert(Tokens.end() - 2,
1294 AsmToken(AsmToken::TokenKind::Comma, Comma));
1295 Done = true;
1296 }
1297 break;
1298 case AsmToken::TokenKind::RCurly:
1299 case AsmToken::TokenKind::EndOfStatement:
1300 case AsmToken::TokenKind::Eof:
1301 Done = true;
1302 break;
1303 default:
1304 break;
1305 }
1306 } while (!Done);
1307 while (!Tokens.empty()) {
1308 Lexer.UnLex(Tokens.back());
1309 Tokens.pop_back();
1310 }
1311 return getParser().parseExpression(Expr);
1312}
1313
1314bool HexagonAsmParser::parseExpressionOrOperand(OperandVector &Operands) {
1315 if (implicitExpressionLocation(Operands)) {
1316 MCAsmParser &Parser = getParser();
1317 SMLoc Loc = Parser.getLexer().getLoc();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001318 MCExpr const *Expr = nullptr;
1319 bool Error = parseExpression(Expr);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001320 Expr = HexagonMCExpr::create(Expr, getContext());
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001321 if (!Error)
1322 Operands.push_back(HexagonOperand::CreateImm(Expr, Loc, Loc));
1323 return Error;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001324 }
1325 return parseOperand(Operands);
1326}
1327
1328/// Parse an instruction.
1329bool HexagonAsmParser::parseInstruction(OperandVector &Operands) {
1330 MCAsmParser &Parser = getParser();
1331 MCAsmLexer &Lexer = getLexer();
1332 while (true) {
1333 AsmToken const &Token = Parser.getTok();
1334 switch (Token.getKind()) {
1335 case AsmToken::EndOfStatement: {
Nirav Davefd910412016-06-17 16:06:17 +00001336 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001337 return false;
1338 }
1339 case AsmToken::LCurly: {
1340 if (!Operands.empty())
1341 return true;
1342 Operands.push_back(
1343 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001344 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001345 return false;
1346 }
1347 case AsmToken::RCurly: {
1348 if (Operands.empty()) {
1349 Operands.push_back(
1350 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001351 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001352 }
1353 return false;
1354 }
1355 case AsmToken::Comma: {
Nirav Davefd910412016-06-17 16:06:17 +00001356 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001357 continue;
1358 }
1359 case AsmToken::EqualEqual:
1360 case AsmToken::ExclaimEqual:
1361 case AsmToken::GreaterEqual:
1362 case AsmToken::GreaterGreater:
1363 case AsmToken::LessEqual:
1364 case AsmToken::LessLess: {
1365 Operands.push_back(HexagonOperand::CreateToken(
1366 Token.getString().substr(0, 1), Token.getLoc()));
1367 Operands.push_back(HexagonOperand::CreateToken(
1368 Token.getString().substr(1, 1), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001369 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001370 continue;
1371 }
1372 case AsmToken::Hash: {
1373 bool MustNotExtend = false;
1374 bool ImplicitExpression = implicitExpressionLocation(Operands);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001375 SMLoc ExprLoc = Lexer.getLoc();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001376 if (!ImplicitExpression)
1377 Operands.push_back(
1378 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001379 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001380 bool MustExtend = false;
1381 bool HiOnly = false;
1382 bool LoOnly = false;
1383 if (Lexer.is(AsmToken::Hash)) {
Nirav Davefd910412016-06-17 16:06:17 +00001384 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001385 MustExtend = true;
1386 } else if (ImplicitExpression)
1387 MustNotExtend = true;
1388 AsmToken const &Token = Parser.getTok();
1389 if (Token.is(AsmToken::Identifier)) {
1390 StringRef String = Token.getString();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001391 if (String.lower() == "hi") {
1392 HiOnly = true;
1393 } else if (String.lower() == "lo") {
1394 LoOnly = true;
1395 }
1396 if (HiOnly || LoOnly) {
1397 AsmToken LParen = Lexer.peekTok();
1398 if (!LParen.is(AsmToken::LParen)) {
1399 HiOnly = false;
1400 LoOnly = false;
1401 } else {
Nirav Davefd910412016-06-17 16:06:17 +00001402 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001403 }
1404 }
1405 }
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001406 MCExpr const *Expr = nullptr;
1407 if (parseExpression(Expr))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001408 return true;
1409 int64_t Value;
1410 MCContext &Context = Parser.getContext();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001411 assert(Expr != nullptr);
1412 if (Expr->evaluateAsAbsolute(Value)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001413 if (HiOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001414 Expr = MCBinaryExpr::createLShr(
1415 Expr, MCConstantExpr::create(16, Context), Context);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001416 if (HiOnly || LoOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001417 Expr = MCBinaryExpr::createAnd(Expr,
1418 MCConstantExpr::create(0xffff, Context),
1419 Context);
Colin LeMahieu5cb6eea2016-03-01 21:37:41 +00001420 } else {
1421 MCValue Value;
1422 if (Expr->evaluateAsRelocatable(Value, nullptr, nullptr)) {
1423 if (!Value.isAbsolute()) {
1424 switch(Value.getAccessVariant()) {
1425 case MCSymbolRefExpr::VariantKind::VK_TPREL:
1426 case MCSymbolRefExpr::VariantKind::VK_DTPREL:
1427 // Don't lazy extend these expression variants
1428 MustNotExtend = !MustExtend;
1429 break;
1430 default:
1431 break;
1432 }
1433 }
1434 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001435 }
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001436 Expr = HexagonMCExpr::create(Expr, Context);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001437 HexagonMCInstrInfo::setMustNotExtend(*Expr, MustNotExtend);
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001438 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001439 std::unique_ptr<HexagonOperand> Operand =
1440 HexagonOperand::CreateImm(Expr, ExprLoc, ExprLoc);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001441 Operands.push_back(std::move(Operand));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001442 continue;
1443 }
1444 default:
1445 break;
1446 }
1447 if (parseExpressionOrOperand(Operands))
1448 return true;
1449 }
1450}
1451
1452bool HexagonAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1453 StringRef Name,
1454 AsmToken ID,
1455 OperandVector &Operands) {
1456 getLexer().UnLex(ID);
1457 return parseInstruction(Operands);
1458}
1459
1460namespace {
1461MCInst makeCombineInst(int opCode, MCOperand &Rdd,
1462 MCOperand &MO1, MCOperand &MO2) {
1463 MCInst TmpInst;
1464 TmpInst.setOpcode(opCode);
1465 TmpInst.addOperand(Rdd);
1466 TmpInst.addOperand(MO1);
1467 TmpInst.addOperand(MO2);
1468
1469 return TmpInst;
1470}
1471}
1472
1473// Define this matcher function after the auto-generated include so we
1474// have the match class enum definitions.
1475unsigned HexagonAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
1476 unsigned Kind) {
1477 HexagonOperand *Op = static_cast<HexagonOperand *>(&AsmOp);
1478
1479 switch (Kind) {
1480 case MCK_0: {
1481 int64_t Value;
1482 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 0
1483 ? Match_Success
1484 : Match_InvalidOperand;
1485 }
1486 case MCK_1: {
1487 int64_t Value;
1488 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 1
1489 ? Match_Success
1490 : Match_InvalidOperand;
1491 }
1492 case MCK__MINUS_1: {
1493 int64_t Value;
1494 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == -1
1495 ? Match_Success
1496 : Match_InvalidOperand;
1497 }
1498 }
1499 if (Op->Kind == HexagonOperand::Token && Kind != InvalidMatchClass) {
1500 StringRef myStringRef = StringRef(Op->Tok.Data, Op->Tok.Length);
1501 if (matchTokenString(myStringRef.lower()) == (MatchClassKind)Kind)
1502 return Match_Success;
1503 if (matchTokenString(myStringRef.upper()) == (MatchClassKind)Kind)
1504 return Match_Success;
1505 }
1506
1507 DEBUG(dbgs() << "Unmatched Operand:");
1508 DEBUG(Op->dump());
1509 DEBUG(dbgs() << "\n");
1510
1511 return Match_InvalidOperand;
1512}
1513
Nirav Dave2364748a2016-09-16 18:30:20 +00001514// FIXME: Calls to OutOfRange shoudl propagate failure up to parseStatement.
1515bool HexagonAsmParser::OutOfRange(SMLoc IDLoc, long long Val, long long Max) {
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001516 std::string errStr;
1517 raw_string_ostream ES(errStr);
Alexey Samsonov44ff2042015-12-02 22:59:22 +00001518 ES << "value " << Val << "(" << format_hex(Val, 0) << ") out of range: ";
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001519 if (Max >= 0)
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001520 ES << "0-" << Max;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001521 else
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001522 ES << Max << "-" << (-Max - 1);
Nirav Dave2364748a2016-09-16 18:30:20 +00001523 return Parser.printError(IDLoc, ES.str().c_str());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001524}
1525
1526int HexagonAsmParser::processInstruction(MCInst &Inst,
1527 OperandVector const &Operands,
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001528 SMLoc IDLoc) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001529 MCContext &Context = getParser().getContext();
1530 const MCRegisterInfo *RI = getContext().getRegisterInfo();
1531 std::string r = "r";
1532 std::string v = "v";
1533 std::string Colon = ":";
1534
1535 bool is32bit = false; // used to distinguish between CONST32 and CONST64
1536 switch (Inst.getOpcode()) {
1537 default:
1538 break;
1539
Colin LeMahieuecef1d92016-02-16 20:38:17 +00001540 case Hexagon::A2_iconst: {
1541 Inst.setOpcode(Hexagon::A2_addi);
1542 MCOperand Reg = Inst.getOperand(0);
1543 MCOperand S16 = Inst.getOperand(1);
1544 HexagonMCInstrInfo::setMustNotExtend(*S16.getExpr());
1545 HexagonMCInstrInfo::setS23_2_reloc(*S16.getExpr());
1546 Inst.clear();
1547 Inst.addOperand(Reg);
1548 Inst.addOperand(MCOperand::createReg(Hexagon::R0));
1549 Inst.addOperand(S16);
1550 break;
1551 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001552 case Hexagon::M4_mpyrr_addr:
1553 case Hexagon::S4_addi_asl_ri:
1554 case Hexagon::S4_addi_lsr_ri:
1555 case Hexagon::S4_andi_asl_ri:
1556 case Hexagon::S4_andi_lsr_ri:
1557 case Hexagon::S4_ori_asl_ri:
1558 case Hexagon::S4_ori_lsr_ri:
1559 case Hexagon::S4_or_andix:
1560 case Hexagon::S4_subi_asl_ri:
1561 case Hexagon::S4_subi_lsr_ri: {
1562 MCOperand &Ry = Inst.getOperand(0);
1563 MCOperand &src = Inst.getOperand(2);
1564 if (RI->getEncodingValue(Ry.getReg()) != RI->getEncodingValue(src.getReg()))
1565 return Match_InvalidOperand;
1566 break;
1567 }
1568
1569 case Hexagon::C2_cmpgei: {
1570 MCOperand &MO = Inst.getOperand(2);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001571 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001572 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001573 Inst.setOpcode(Hexagon::C2_cmpgti);
1574 break;
1575 }
1576
1577 case Hexagon::C2_cmpgeui: {
1578 MCOperand &MO = Inst.getOperand(2);
1579 int64_t Value;
1580 bool Success = MO.getExpr()->evaluateAsAbsolute(Value);
Colin LeMahieu9d851f02015-11-09 21:06:28 +00001581 (void)Success;
1582 assert(Success && "Assured by matcher");
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001583 if (Value == 0) {
1584 MCInst TmpInst;
1585 MCOperand &Pd = Inst.getOperand(0);
1586 MCOperand &Rt = Inst.getOperand(1);
1587 TmpInst.setOpcode(Hexagon::C2_cmpeq);
1588 TmpInst.addOperand(Pd);
1589 TmpInst.addOperand(Rt);
1590 TmpInst.addOperand(Rt);
1591 Inst = TmpInst;
1592 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001593 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001594 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001595 Inst.setOpcode(Hexagon::C2_cmpgtui);
1596 }
1597 break;
1598 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001599
1600 // Translate a "$Rdd = $Rss" to "$Rdd = combine($Rs, $Rt)"
1601 case Hexagon::A2_tfrp: {
1602 MCOperand &MO = Inst.getOperand(1);
1603 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001604 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001605 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001606 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001607 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001608 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001609 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001610 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001611 Inst.setOpcode(Hexagon::A2_combinew);
1612 break;
1613 }
1614
1615 case Hexagon::A2_tfrpt:
1616 case Hexagon::A2_tfrpf: {
1617 MCOperand &MO = Inst.getOperand(2);
1618 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001619 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001620 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001621 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001622 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001623 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001624 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001625 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001626 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrpt)
1627 ? Hexagon::C2_ccombinewt
1628 : Hexagon::C2_ccombinewf);
1629 break;
1630 }
1631 case Hexagon::A2_tfrptnew:
1632 case Hexagon::A2_tfrpfnew: {
1633 MCOperand &MO = Inst.getOperand(2);
1634 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001635 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001636 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001637 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001638 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001639 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001640 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001641 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001642 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrptnew)
1643 ? Hexagon::C2_ccombinewnewt
1644 : Hexagon::C2_ccombinewnewf);
1645 break;
1646 }
1647
Krzysztof Parzyszek0e7d2d32016-04-28 16:43:16 +00001648 // Translate a "$Vdd = $Vss" to "$Vdd = vcombine($Vs, $Vt)"
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001649 case Hexagon::V6_vassignp: {
Krzysztof Parzyszek0e7d2d32016-04-28 16:43:16 +00001650 MCOperand &MO = Inst.getOperand(1);
1651 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
1652 std::string R1 = v + llvm::utostr(RegPairNum + 1);
1653 MO.setReg(MatchRegisterName(R1));
1654 // Add a new operand for the second register in the pair.
1655 std::string R2 = v + llvm::utostr(RegPairNum);
1656 Inst.addOperand(MCOperand::createReg(MatchRegisterName(R2)));
1657 Inst.setOpcode(Hexagon::V6_vcombine);
1658 break;
1659 }
1660
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001661 // Translate a "$Rx = CONST32(#imm)" to "$Rx = memw(gp+#LABEL) "
1662 case Hexagon::CONST32:
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001663 is32bit = true;
1664 // Translate a "$Rx:y = CONST64(#imm)" to "$Rx:y = memd(gp+#LABEL) "
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001665 case Hexagon::CONST64:
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001666 // FIXME: need better way to detect AsmStreamer (upstream removed getKind())
1667 if (!Parser.getStreamer().hasRawTextSupport()) {
1668 MCELFStreamer *MES = static_cast<MCELFStreamer *>(&Parser.getStreamer());
1669 MCOperand &MO_1 = Inst.getOperand(1);
1670 MCOperand &MO_0 = Inst.getOperand(0);
1671
1672 // push section onto section stack
1673 MES->PushSection();
1674
1675 std::string myCharStr;
1676 MCSectionELF *mySection;
1677
1678 // check if this as an immediate or a symbol
1679 int64_t Value;
1680 bool Absolute = MO_1.getExpr()->evaluateAsAbsolute(Value);
1681 if (Absolute) {
1682 // Create a new section - one for each constant
1683 // Some or all of the zeros are replaced with the given immediate.
1684 if (is32bit) {
1685 std::string myImmStr = utohexstr(static_cast<uint32_t>(Value));
1686 myCharStr = StringRef(".gnu.linkonce.l4.CONST_00000000")
1687 .drop_back(myImmStr.size())
1688 .str() +
1689 myImmStr;
1690 } else {
1691 std::string myImmStr = utohexstr(Value);
1692 myCharStr = StringRef(".gnu.linkonce.l8.CONST_0000000000000000")
1693 .drop_back(myImmStr.size())
1694 .str() +
1695 myImmStr;
1696 }
1697
1698 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1699 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1700 } else if (MO_1.isExpr()) {
1701 // .lita - for expressions
1702 myCharStr = ".lita";
1703 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1704 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1705 } else
1706 llvm_unreachable("unexpected type of machine operand!");
1707
1708 MES->SwitchSection(mySection);
1709 unsigned byteSize = is32bit ? 4 : 8;
1710 getStreamer().EmitCodeAlignment(byteSize, byteSize);
1711
1712 MCSymbol *Sym;
1713
1714 // for symbols, get rid of prepended ".gnu.linkonce.lx."
1715
1716 // emit symbol if needed
1717 if (Absolute) {
1718 Sym = getContext().getOrCreateSymbol(StringRef(myCharStr.c_str() + 16));
1719 if (Sym->isUndefined()) {
1720 getStreamer().EmitLabel(Sym);
1721 getStreamer().EmitSymbolAttribute(Sym, MCSA_Global);
1722 getStreamer().EmitIntValue(Value, byteSize);
1723 }
1724 } else if (MO_1.isExpr()) {
1725 const char *StringStart = 0;
1726 const char *StringEnd = 0;
1727 if (*Operands[4]->getStartLoc().getPointer() == '#') {
1728 StringStart = Operands[5]->getStartLoc().getPointer();
1729 StringEnd = Operands[6]->getStartLoc().getPointer();
1730 } else { // no pound
1731 StringStart = Operands[4]->getStartLoc().getPointer();
1732 StringEnd = Operands[5]->getStartLoc().getPointer();
1733 }
1734
1735 unsigned size = StringEnd - StringStart;
1736 std::string DotConst = ".CONST_";
1737 Sym = getContext().getOrCreateSymbol(DotConst +
1738 StringRef(StringStart, size));
1739
1740 if (Sym->isUndefined()) {
1741 // case where symbol is not yet defined: emit symbol
1742 getStreamer().EmitLabel(Sym);
1743 getStreamer().EmitSymbolAttribute(Sym, MCSA_Local);
1744 getStreamer().EmitValue(MO_1.getExpr(), 4);
1745 }
1746 } else
1747 llvm_unreachable("unexpected type of machine operand!");
1748
1749 MES->PopSection();
1750
1751 if (Sym) {
1752 MCInst TmpInst;
1753 if (is32bit) // 32 bit
1754 TmpInst.setOpcode(Hexagon::L2_loadrigp);
1755 else // 64 bit
1756 TmpInst.setOpcode(Hexagon::L2_loadrdgp);
1757
1758 TmpInst.addOperand(MO_0);
1759 TmpInst.addOperand(
1760 MCOperand::createExpr(MCSymbolRefExpr::create(Sym, getContext())));
1761 Inst = TmpInst;
1762 }
1763 }
1764 break;
1765
1766 // Translate a "$Rdd = #-imm" to "$Rdd = combine(#[-1,0], #-imm)"
1767 case Hexagon::A2_tfrpi: {
1768 MCOperand &Rdd = Inst.getOperand(0);
1769 MCOperand &MO = Inst.getOperand(1);
1770 int64_t Value;
1771 int sVal = (MO.getExpr()->evaluateAsAbsolute(Value) && Value < 0) ? -1 : 0;
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001772 MCOperand imm(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001773 HexagonMCExpr::create(MCConstantExpr::create(sVal, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001774 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, imm, MO);
1775 break;
1776 }
1777
1778 // Translate a "$Rdd = [#]#imm" to "$Rdd = combine(#, [#]#imm)"
1779 case Hexagon::TFRI64_V4: {
1780 MCOperand &Rdd = Inst.getOperand(0);
1781 MCOperand &MO = Inst.getOperand(1);
1782 int64_t Value;
1783 if (MO.getExpr()->evaluateAsAbsolute(Value)) {
David Majnemere61e4bf2016-06-21 05:10:24 +00001784 int s8 = Hi_32(Value);
1785 if (!isInt<8>(s8))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001786 OutOfRange(IDLoc, s8, -128);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001787 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001788 MCConstantExpr::create(s8, Context), Context))); // upper 32
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001789 auto Expr = HexagonMCExpr::create(
David Majnemere61e4bf2016-06-21 05:10:24 +00001790 MCConstantExpr::create(Lo_32(Value), Context), Context);
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001791 HexagonMCInstrInfo::setMustExtend(*Expr, HexagonMCInstrInfo::mustExtend(*MO.getExpr()));
1792 MCOperand imm2(MCOperand::createExpr(Expr)); // lower 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001793 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, imm2);
1794 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001795 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001796 MCConstantExpr::create(0, Context), Context))); // upper 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001797 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, MO);
1798 }
1799 break;
1800 }
1801
1802 // Handle $Rdd = combine(##imm, #imm)"
1803 case Hexagon::TFRI64_V2_ext: {
1804 MCOperand &Rdd = Inst.getOperand(0);
1805 MCOperand &MO1 = Inst.getOperand(1);
1806 MCOperand &MO2 = Inst.getOperand(2);
1807 int64_t Value;
1808 if (MO2.getExpr()->evaluateAsAbsolute(Value)) {
1809 int s8 = Value;
1810 if (s8 < -128 || s8 > 127)
1811 OutOfRange(IDLoc, s8, -128);
1812 }
1813 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, MO1, MO2);
1814 break;
1815 }
1816
1817 // Handle $Rdd = combine(#imm, ##imm)"
1818 case Hexagon::A4_combineii: {
1819 MCOperand &Rdd = Inst.getOperand(0);
1820 MCOperand &MO1 = Inst.getOperand(1);
1821 int64_t Value;
1822 if (MO1.getExpr()->evaluateAsAbsolute(Value)) {
1823 int s8 = Value;
1824 if (s8 < -128 || s8 > 127)
1825 OutOfRange(IDLoc, s8, -128);
1826 }
1827 MCOperand &MO2 = Inst.getOperand(2);
1828 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, MO1, MO2);
1829 break;
1830 }
1831
1832 case Hexagon::S2_tableidxb_goodsyntax: {
1833 Inst.setOpcode(Hexagon::S2_tableidxb);
1834 break;
1835 }
1836
1837 case Hexagon::S2_tableidxh_goodsyntax: {
1838 MCInst TmpInst;
1839 MCOperand &Rx = Inst.getOperand(0);
1840 MCOperand &_dst_ = Inst.getOperand(1);
1841 MCOperand &Rs = Inst.getOperand(2);
1842 MCOperand &Imm4 = Inst.getOperand(3);
1843 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001844 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001845 Imm6.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001846 TmpInst.setOpcode(Hexagon::S2_tableidxh);
1847 TmpInst.addOperand(Rx);
1848 TmpInst.addOperand(_dst_);
1849 TmpInst.addOperand(Rs);
1850 TmpInst.addOperand(Imm4);
1851 TmpInst.addOperand(Imm6);
1852 Inst = TmpInst;
1853 break;
1854 }
1855
1856 case Hexagon::S2_tableidxw_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(2, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001865 TmpInst.setOpcode(Hexagon::S2_tableidxw);
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_tableidxd_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(3, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001884 TmpInst.setOpcode(Hexagon::S2_tableidxd);
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::M2_mpyui: {
1895 Inst.setOpcode(Hexagon::M2_mpyi);
1896 break;
1897 }
1898 case Hexagon::M2_mpysmi: {
1899 MCInst TmpInst;
1900 MCOperand &Rd = Inst.getOperand(0);
1901 MCOperand &Rs = Inst.getOperand(1);
1902 MCOperand &Imm = Inst.getOperand(2);
1903 int64_t Value;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001904 MCExpr const &Expr = *Imm.getExpr();
1905 bool Absolute = Expr.evaluateAsAbsolute(Value);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001906 assert(Absolute);
1907 (void)Absolute;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001908 if (!HexagonMCInstrInfo::mustExtend(Expr)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001909 if (Value < 0 && Value > -256) {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001910 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001911 MCConstantExpr::create(Value * -1, Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001912 TmpInst.setOpcode(Hexagon::M2_mpysin);
1913 } else if (Value < 256 && Value >= 0)
1914 TmpInst.setOpcode(Hexagon::M2_mpysip);
1915 else
1916 return Match_InvalidOperand;
1917 } else {
1918 if (Value >= 0)
1919 TmpInst.setOpcode(Hexagon::M2_mpysip);
1920 else
1921 return Match_InvalidOperand;
1922 }
1923 TmpInst.addOperand(Rd);
1924 TmpInst.addOperand(Rs);
1925 TmpInst.addOperand(Imm);
1926 Inst = TmpInst;
1927 break;
1928 }
1929
1930 case Hexagon::S2_asr_i_r_rnd_goodsyntax: {
1931 MCOperand &Imm = Inst.getOperand(2);
1932 MCInst TmpInst;
1933 int64_t Value;
1934 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1935 assert(Absolute);
1936 (void)Absolute;
1937 if (Value == 0) { // convert to $Rd = $Rs
1938 TmpInst.setOpcode(Hexagon::A2_tfr);
1939 MCOperand &Rd = Inst.getOperand(0);
1940 MCOperand &Rs = Inst.getOperand(1);
1941 TmpInst.addOperand(Rd);
1942 TmpInst.addOperand(Rs);
1943 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001944 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001945 MCBinaryExpr::createSub(Imm.getExpr(),
1946 MCConstantExpr::create(1, Context), Context),
1947 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001948 TmpInst.setOpcode(Hexagon::S2_asr_i_r_rnd);
1949 MCOperand &Rd = Inst.getOperand(0);
1950 MCOperand &Rs = Inst.getOperand(1);
1951 TmpInst.addOperand(Rd);
1952 TmpInst.addOperand(Rs);
1953 TmpInst.addOperand(Imm);
1954 }
1955 Inst = TmpInst;
1956 break;
1957 }
1958
1959 case Hexagon::S2_asr_i_p_rnd_goodsyntax: {
1960 MCOperand &Rdd = Inst.getOperand(0);
1961 MCOperand &Rss = Inst.getOperand(1);
1962 MCOperand &Imm = Inst.getOperand(2);
1963 int64_t Value;
1964 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1965 assert(Absolute);
1966 (void)Absolute;
1967 if (Value == 0) { // convert to $Rdd = combine ($Rs[0], $Rs[1])
1968 MCInst TmpInst;
1969 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001970 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001971 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001972 Rss.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001973 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001974 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001975 StringRef Reg2(R2);
1976 TmpInst.setOpcode(Hexagon::A2_combinew);
1977 TmpInst.addOperand(Rdd);
1978 TmpInst.addOperand(Rss);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001979 TmpInst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001980 Inst = TmpInst;
1981 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001982 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001983 MCBinaryExpr::createSub(Imm.getExpr(),
1984 MCConstantExpr::create(1, Context), Context),
1985 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001986 Inst.setOpcode(Hexagon::S2_asr_i_p_rnd);
1987 }
1988 break;
1989 }
1990
1991 case Hexagon::A4_boundscheck: {
1992 MCOperand &Rs = Inst.getOperand(1);
1993 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
1994 if (RegNum & 1) { // Odd mapped to raw:hi, regpair is rodd:odd-1, like r3:2
1995 Inst.setOpcode(Hexagon::A4_boundscheck_hi);
1996 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00001997 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001998 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001999 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002000 } else { // raw:lo
2001 Inst.setOpcode(Hexagon::A4_boundscheck_lo);
2002 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002003 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002004 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002005 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002006 }
2007 break;
2008 }
2009
2010 case Hexagon::A2_addsp: {
2011 MCOperand &Rs = Inst.getOperand(1);
2012 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
2013 if (RegNum & 1) { // Odd mapped to raw:hi
2014 Inst.setOpcode(Hexagon::A2_addsph);
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 { // Even mapped raw:lo
2020 Inst.setOpcode(Hexagon::A2_addspl);
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::M2_vrcmpys_s1: {
2030 MCOperand &Rt = Inst.getOperand(2);
2031 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2032 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2033 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_h);
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 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002038 } else { // Even mapped sat:raw:lo
2039 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_l);
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 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002044 }
2045 break;
2046 }
2047
2048 case Hexagon::M2_vrcmpys_acc_s1: {
2049 MCInst TmpInst;
2050 MCOperand &Rxx = Inst.getOperand(0);
2051 MCOperand &Rss = Inst.getOperand(2);
2052 MCOperand &Rt = Inst.getOperand(3);
2053 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2054 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2055 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_h);
2056 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002057 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002058 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002059 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002060 } else { // Even mapped sat:raw:lo
2061 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_l);
2062 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002063 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002064 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002065 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002066 }
2067 // Registers are in different positions
2068 TmpInst.addOperand(Rxx);
2069 TmpInst.addOperand(Rxx);
2070 TmpInst.addOperand(Rss);
2071 TmpInst.addOperand(Rt);
2072 Inst = TmpInst;
2073 break;
2074 }
2075
2076 case Hexagon::M2_vrcmpys_s1rp: {
2077 MCOperand &Rt = Inst.getOperand(2);
2078 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2079 if (RegNum & 1) { // Odd mapped to rnd:sat:raw:hi
2080 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_h);
2081 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002082 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
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 } else { // Even mapped rnd:sat:raw:lo
2086 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_l);
2087 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002088 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002089 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002090 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002091 }
2092 break;
2093 }
2094
2095 case Hexagon::S5_asrhub_rnd_sat_goodsyntax: {
2096 MCOperand &Imm = Inst.getOperand(2);
2097 int64_t Value;
2098 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
2099 assert(Absolute);
2100 (void)Absolute;
2101 if (Value == 0)
2102 Inst.setOpcode(Hexagon::S2_vsathub);
2103 else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002104 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002105 MCBinaryExpr::createSub(Imm.getExpr(),
2106 MCConstantExpr::create(1, Context), Context),
2107 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002108 Inst.setOpcode(Hexagon::S5_asrhub_rnd_sat);
2109 }
2110 break;
2111 }
2112
2113 case Hexagon::S5_vasrhrnd_goodsyntax: {
2114 MCOperand &Rdd = Inst.getOperand(0);
2115 MCOperand &Rss = Inst.getOperand(1);
2116 MCOperand &Imm = Inst.getOperand(2);
2117 int64_t Value;
2118 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
2119 assert(Absolute);
2120 (void)Absolute;
2121 if (Value == 0) {
2122 MCInst TmpInst;
2123 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00002124 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002125 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002126 Rss.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002127 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00002128 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002129 StringRef Reg2(R2);
2130 TmpInst.setOpcode(Hexagon::A2_combinew);
2131 TmpInst.addOperand(Rdd);
2132 TmpInst.addOperand(Rss);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002133 TmpInst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002134 Inst = TmpInst;
2135 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002136 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002137 MCBinaryExpr::createSub(Imm.getExpr(),
2138 MCConstantExpr::create(1, Context), Context),
2139 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002140 Inst.setOpcode(Hexagon::S5_vasrhrnd);
2141 }
2142 break;
2143 }
2144
2145 case Hexagon::A2_not: {
2146 MCInst TmpInst;
2147 MCOperand &Rd = Inst.getOperand(0);
2148 MCOperand &Rs = Inst.getOperand(1);
2149 TmpInst.setOpcode(Hexagon::A2_subri);
2150 TmpInst.addOperand(Rd);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002151 TmpInst.addOperand(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002152 HexagonMCExpr::create(MCConstantExpr::create(-1, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002153 TmpInst.addOperand(Rs);
2154 Inst = TmpInst;
2155 break;
2156 }
2157 } // switch
2158
2159 return Match_Success;
2160}
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002161
2162
2163unsigned HexagonAsmParser::matchRegister(StringRef Name) {
2164 if (unsigned Reg = MatchRegisterName(Name))
2165 return Reg;
2166 return MatchRegisterAltName(Name);
2167}