blob: 024a634f1a8e8ecba3adc90bb2c48b4d98a45928 [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 Collingbourne0da86302016-10-10 22:49:37 +0000225 SMLoc getEndLoc() const { 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; }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000279 bool iss32_0Imm() 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); }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000281 bool iss8_0Imm() const { return CheckImmRange(8, 0, true, false, false); }
282 bool iss8_0Imm64() const { return CheckImmRange(8, 0, true, true, false); }
283 bool iss7_0Imm() const { return CheckImmRange(7, 0, true, false, false); }
284 bool iss6_0Imm() const { return CheckImmRange(6, 0, true, false, false); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000285 bool iss4_0Imm() const { return CheckImmRange(4, 0, true, false, false); }
286 bool iss4_1Imm() const { return CheckImmRange(4, 1, true, false, false); }
287 bool iss4_2Imm() const { return CheckImmRange(4, 2, true, false, false); }
288 bool iss4_3Imm() const { return CheckImmRange(4, 3, true, false, false); }
289 bool iss4_6Imm() const { return CheckImmRange(4, 0, true, false, false); }
290 bool iss3_6Imm() const { return CheckImmRange(3, 0, true, false, false); }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000291 bool iss3_0Imm() const { return CheckImmRange(3, 0, true, false, false); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000292
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000293 bool isu64_0Imm() const { return CheckImmRange(64, 0, false, true, true); }
294 bool isu32_0Imm() const { return CheckImmRange(32, 0, false, true, false); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000295 bool isu26_6Imm() const { return CheckImmRange(26, 6, false, true, false); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000296 bool isu16_0Imm() const { return CheckImmRange(16, 0, false, true, false); }
297 bool isu16_1Imm() const { return CheckImmRange(16, 1, false, true, false); }
298 bool isu16_2Imm() const { return CheckImmRange(16, 2, false, true, false); }
299 bool isu16_3Imm() const { return CheckImmRange(16, 3, false, true, false); }
300 bool isu11_3Imm() const { return CheckImmRange(11, 3, false, false, false); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000301 bool isu6_1Imm() const { return CheckImmRange(6, 1, false, false, false); }
302 bool isu6_2Imm() const { return CheckImmRange(6, 2, false, false, false); }
303 bool isu6_3Imm() const { return CheckImmRange(6, 3, false, false, false); }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000304 bool isu10_0Imm() const { return CheckImmRange(10, 0, false, false, false); }
305 bool isu9_0Imm() const { return CheckImmRange(9, 0, false, false, false); }
306 bool isu8_0Imm() const { return CheckImmRange(8, 0, false, false, false); }
307 bool isu7_0Imm() const { return CheckImmRange(7, 0, false, false, false); }
308 bool isu6_0Imm() const { return CheckImmRange(6, 0, false, false, false); }
309 bool isu5_0Imm() const { return CheckImmRange(5, 0, false, false, false); }
310 bool isu4_0Imm() const { return CheckImmRange(4, 0, false, false, false); }
311 bool isu3_0Imm() const { return CheckImmRange(3, 0, false, false, false); }
312 bool isu2_0Imm() const { return CheckImmRange(2, 0, false, false, false); }
313 bool isu1_0Imm() const { return CheckImmRange(1, 0, false, false, false); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000314
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000315 bool ism6_0Imm() const { return CheckImmRange(6, 0, false, false, false); }
316 bool isn8_0Imm() const { return CheckImmRange(8, 0, false, false, false); }
Colin LeMahieu81707542016-12-05 04:29:00 +0000317 bool isn1Const() const {
318 if (!isImm())
319 return false;
320 int64_t Value;
321 if (!getImm()->evaluateAsAbsolute(Value))
322 return false;
323 return Value == -1;
324 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000325
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000326 bool iss16_0Ext() const { return CheckImmRange(16 + 26, 0, true, true, true); }
327 bool iss12_0Ext() const { return CheckImmRange(12 + 26, 0, true, true, true); }
328 bool iss10_0Ext() const { return CheckImmRange(10 + 26, 0, true, true, true); }
329 bool iss9_0Ext() const { return CheckImmRange(9 + 26, 0, true, true, true); }
330 bool iss8_0Ext() const { return CheckImmRange(8 + 26, 0, true, true, true); }
331 bool iss7_0Ext() const { return CheckImmRange(7 + 26, 0, true, true, true); }
332 bool iss6_0Ext() const { return CheckImmRange(6 + 26, 0, true, true, true); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000333 bool iss11_0Ext() const {
334 return CheckImmRange(11 + 26, 0, true, true, true);
335 }
336 bool iss11_1Ext() const {
337 return CheckImmRange(11 + 26, 1, true, true, true);
338 }
339 bool iss11_2Ext() const {
340 return CheckImmRange(11 + 26, 2, true, true, true);
341 }
342 bool iss11_3Ext() const {
343 return CheckImmRange(11 + 26, 3, true, true, true);
344 }
345
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000346 bool isu7_0Ext() const { return CheckImmRange(7 + 26, 0, false, true, true); }
347 bool isu8_0Ext() const { return CheckImmRange(8 + 26, 0, false, true, true); }
348 bool isu9_0Ext() const { return CheckImmRange(9 + 26, 0, false, true, true); }
349 bool isu10_0Ext() const { return CheckImmRange(10 + 26, 0, false, true, true); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000350 bool isu6_0Ext() const { return CheckImmRange(6 + 26, 0, false, true, true); }
351 bool isu6_1Ext() const { return CheckImmRange(6 + 26, 1, false, true, true); }
352 bool isu6_2Ext() const { return CheckImmRange(6 + 26, 2, false, true, true); }
353 bool isu6_3Ext() const { return CheckImmRange(6 + 26, 3, false, true, true); }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000354 bool isu32_0MustExt() const { return isImm(); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000355
356 void addRegOperands(MCInst &Inst, unsigned N) const {
357 assert(N == 1 && "Invalid number of operands!");
358 Inst.addOperand(MCOperand::createReg(getReg()));
359 }
360
361 void addImmOperands(MCInst &Inst, unsigned N) const {
362 assert(N == 1 && "Invalid number of operands!");
363 Inst.addOperand(MCOperand::createExpr(getImm()));
364 }
365
366 void addSignedImmOperands(MCInst &Inst, unsigned N) const {
367 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000368 HexagonMCExpr *Expr =
369 const_cast<HexagonMCExpr *>(cast<HexagonMCExpr>(getImm()));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000370 int64_t Value;
371 if (!Expr->evaluateAsAbsolute(Value)) {
372 Inst.addOperand(MCOperand::createExpr(Expr));
373 return;
374 }
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000375 int64_t Extended = SignExtend64(Value, 32);
376 if ((Extended < 0) != (Value < 0))
377 Expr->setSignMismatch();
378 Inst.addOperand(MCOperand::createExpr(Expr));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000379 }
380
381 void addf32ExtOperands(MCInst &Inst, unsigned N) const {
382 addImmOperands(Inst, N);
383 }
384
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000385 void adds32_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000386 addSignedImmOperands(Inst, N);
387 }
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000388 void adds23_2ImmOperands(MCInst &Inst, unsigned N) const {
389 addSignedImmOperands(Inst, N);
390 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000391 void adds8_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000392 addSignedImmOperands(Inst, N);
393 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000394 void adds8_0Imm64Operands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000395 addSignedImmOperands(Inst, N);
396 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000397 void adds6_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000398 addSignedImmOperands(Inst, N);
399 }
400 void adds4_0ImmOperands(MCInst &Inst, unsigned N) const {
401 addSignedImmOperands(Inst, N);
402 }
403 void adds4_1ImmOperands(MCInst &Inst, unsigned N) const {
404 addSignedImmOperands(Inst, N);
405 }
406 void adds4_2ImmOperands(MCInst &Inst, unsigned N) const {
407 addSignedImmOperands(Inst, N);
408 }
409 void adds4_3ImmOperands(MCInst &Inst, unsigned N) const {
410 addSignedImmOperands(Inst, N);
411 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000412 void adds3_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000413 addSignedImmOperands(Inst, N);
414 }
415
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000416 void addu64_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000417 addImmOperands(Inst, N);
418 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000419 void addu32_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000420 addImmOperands(Inst, N);
421 }
422 void addu26_6ImmOperands(MCInst &Inst, unsigned N) const {
423 addImmOperands(Inst, N);
424 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000425 void addu16_0ImmOperands(MCInst &Inst, unsigned N) const {
426 addImmOperands(Inst, N);
427 }
428 void addu16_1ImmOperands(MCInst &Inst, unsigned N) const {
429 addImmOperands(Inst, N);
430 }
431 void addu16_2ImmOperands(MCInst &Inst, unsigned N) const {
432 addImmOperands(Inst, N);
433 }
434 void addu16_3ImmOperands(MCInst &Inst, unsigned N) const {
435 addImmOperands(Inst, N);
436 }
437 void addu11_3ImmOperands(MCInst &Inst, unsigned N) const {
438 addImmOperands(Inst, N);
439 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000440 void addu10_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000441 addImmOperands(Inst, N);
442 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000443 void addu9_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000444 addImmOperands(Inst, N);
445 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000446 void addu8_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000447 addImmOperands(Inst, N);
448 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000449 void addu7_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000450 addImmOperands(Inst, N);
451 }
452 void addu6_0ImmOperands(MCInst &Inst, unsigned N) const {
453 addImmOperands(Inst, N);
454 }
455 void addu6_1ImmOperands(MCInst &Inst, unsigned N) const {
456 addImmOperands(Inst, N);
457 }
458 void addu6_2ImmOperands(MCInst &Inst, unsigned N) const {
459 addImmOperands(Inst, N);
460 }
461 void addu6_3ImmOperands(MCInst &Inst, unsigned N) const {
462 addImmOperands(Inst, N);
463 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000464 void addu5_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000465 addImmOperands(Inst, N);
466 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000467 void addu4_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000468 addImmOperands(Inst, N);
469 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000470 void addu3_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000471 addImmOperands(Inst, N);
472 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000473 void addu2_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000474 addImmOperands(Inst, N);
475 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000476 void addu1_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000477 addImmOperands(Inst, N);
478 }
479
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000480 void addm6_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000481 addImmOperands(Inst, N);
482 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000483 void addn8_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000484 addImmOperands(Inst, N);
485 }
486
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000487 void adds16_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000488 addSignedImmOperands(Inst, N);
489 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000490 void adds12_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000491 addSignedImmOperands(Inst, N);
492 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000493 void adds10_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000494 addSignedImmOperands(Inst, N);
495 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000496 void adds9_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000497 addSignedImmOperands(Inst, N);
498 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000499 void adds8_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000500 addSignedImmOperands(Inst, N);
501 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000502 void adds6_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000503 addSignedImmOperands(Inst, N);
504 }
505 void adds11_0ExtOperands(MCInst &Inst, unsigned N) const {
506 addSignedImmOperands(Inst, N);
507 }
508 void adds11_1ExtOperands(MCInst &Inst, unsigned N) const {
509 addSignedImmOperands(Inst, N);
510 }
511 void adds11_2ExtOperands(MCInst &Inst, unsigned N) const {
512 addSignedImmOperands(Inst, N);
513 }
514 void adds11_3ExtOperands(MCInst &Inst, unsigned N) const {
515 addSignedImmOperands(Inst, N);
516 }
Colin LeMahieu81707542016-12-05 04:29:00 +0000517 void addn1ConstOperands(MCInst &Inst, unsigned N) const {
518 addImmOperands(Inst, N);
519 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000520
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000521 void addu7_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000522 addImmOperands(Inst, N);
523 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000524 void addu8_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000525 addImmOperands(Inst, N);
526 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000527 void addu9_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000528 addImmOperands(Inst, N);
529 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000530 void addu10_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000531 addImmOperands(Inst, N);
532 }
533 void addu6_0ExtOperands(MCInst &Inst, unsigned N) const {
534 addImmOperands(Inst, N);
535 }
536 void addu6_1ExtOperands(MCInst &Inst, unsigned N) const {
537 addImmOperands(Inst, N);
538 }
539 void addu6_2ExtOperands(MCInst &Inst, unsigned N) const {
540 addImmOperands(Inst, N);
541 }
542 void addu6_3ExtOperands(MCInst &Inst, unsigned N) const {
543 addImmOperands(Inst, N);
544 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000545 void addu32_0MustExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000546 addImmOperands(Inst, N);
547 }
548
549 void adds4_6ImmOperands(MCInst &Inst, unsigned N) const {
550 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000551 const MCConstantExpr *CE =
552 dyn_cast<MCConstantExpr>(&HexagonMCInstrInfo::getExpr(*getImm()));
Colin LeMahieu4c606e62015-12-04 15:48:45 +0000553 Inst.addOperand(MCOperand::createImm(CE->getValue() * 64));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000554 }
555
556 void adds3_6ImmOperands(MCInst &Inst, unsigned N) const {
557 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000558 const MCConstantExpr *CE =
559 dyn_cast<MCConstantExpr>(&HexagonMCInstrInfo::getExpr(*getImm()));
Colin LeMahieu4c606e62015-12-04 15:48:45 +0000560 Inst.addOperand(MCOperand::createImm(CE->getValue() * 64));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000561 }
562
563 StringRef getToken() const {
564 assert(Kind == Token && "Invalid access!");
565 return StringRef(Tok.Data, Tok.Length);
566 }
567
568 virtual void print(raw_ostream &OS) const;
569
570 static std::unique_ptr<HexagonOperand> CreateToken(StringRef Str, SMLoc S) {
571 HexagonOperand *Op = new HexagonOperand(Token);
572 Op->Tok.Data = Str.data();
573 Op->Tok.Length = Str.size();
574 Op->StartLoc = S;
575 Op->EndLoc = S;
576 return std::unique_ptr<HexagonOperand>(Op);
577 }
578
579 static std::unique_ptr<HexagonOperand> CreateReg(unsigned RegNum, SMLoc S,
580 SMLoc E) {
581 HexagonOperand *Op = new HexagonOperand(Register);
582 Op->Reg.RegNum = RegNum;
583 Op->StartLoc = S;
584 Op->EndLoc = E;
585 return std::unique_ptr<HexagonOperand>(Op);
586 }
587
588 static std::unique_ptr<HexagonOperand> CreateImm(const MCExpr *Val, SMLoc S,
589 SMLoc E) {
590 HexagonOperand *Op = new HexagonOperand(Immediate);
591 Op->Imm.Val = Val;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000592 Op->StartLoc = S;
593 Op->EndLoc = E;
594 return std::unique_ptr<HexagonOperand>(Op);
595 }
596};
597
598} // end anonymous namespace.
599
600void HexagonOperand::print(raw_ostream &OS) const {
601 switch (Kind) {
602 case Immediate:
603 getImm()->print(OS, nullptr);
604 break;
605 case Register:
606 OS << "<register R";
607 OS << getReg() << ">";
608 break;
609 case Token:
610 OS << "'" << getToken() << "'";
611 break;
612 }
613}
614
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000615bool HexagonAsmParser::finishBundle(SMLoc IDLoc, MCStreamer &Out) {
616 DEBUG(dbgs() << "Bundle:");
617 DEBUG(MCB.dump_pretty(dbgs()));
618 DEBUG(dbgs() << "--\n");
619
620 // Check the bundle for errors.
621 const MCRegisterInfo *RI = getContext().getRegisterInfo();
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000622 HexagonMCChecker Check(MCII, getSTI(), MCB, MCB, *RI);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000623
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000624 bool CheckOk = HexagonMCInstrInfo::canonicalizePacket(MCII, getSTI(),
625 getContext(), MCB,
626 &Check);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000627
628 while (Check.getNextErrInfo() == true) {
629 unsigned Reg = Check.getErrRegister();
630 Twine R(RI->getName(Reg));
631
632 uint64_t Err = Check.getError();
633 if (Err != HexagonMCErrInfo::CHECK_SUCCESS) {
634 if (HexagonMCErrInfo::CHECK_ERROR_BRANCHES & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000635 return Error(
636 IDLoc,
637 "unconditional branch cannot precede another branch in packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000638
639 if (HexagonMCErrInfo::CHECK_ERROR_NEWP & Err ||
640 HexagonMCErrInfo::CHECK_ERROR_NEWV & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000641 return Error(IDLoc, "register `" + R +
642 "' used with `.new' "
643 "but not validly modified in the same packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000644
645 if (HexagonMCErrInfo::CHECK_ERROR_REGISTERS & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000646 return Error(IDLoc, "register `" + R + "' modified more than once");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000647
648 if (HexagonMCErrInfo::CHECK_ERROR_READONLY & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000649 return Error(IDLoc, "cannot write to read-only register `" + R + "'");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000650
651 if (HexagonMCErrInfo::CHECK_ERROR_LOOP & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000652 return Error(IDLoc, "loop-setup and some branch instructions "
653 "cannot be in the same packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000654
655 if (HexagonMCErrInfo::CHECK_ERROR_ENDLOOP & Err) {
656 Twine N(HexagonMCInstrInfo::isInnerLoop(MCB) ? '0' : '1');
Nirav Dave2364748a2016-09-16 18:30:20 +0000657 return Error(IDLoc,
658 "packet marked with `:endloop" + N + "' " +
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000659 "cannot contain instructions that modify register " +
660 "`" + R + "'");
661 }
662
663 if (HexagonMCErrInfo::CHECK_ERROR_SOLO & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000664 return Error(
665 IDLoc,
666 "instruction cannot appear in packet with other instructions");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000667
668 if (HexagonMCErrInfo::CHECK_ERROR_NOSLOTS & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000669 return Error(IDLoc, "too many slots used in packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000670
671 if (Err & HexagonMCErrInfo::CHECK_ERROR_SHUFFLE) {
672 uint64_t Erm = Check.getShuffleError();
673
674 if (HexagonShuffler::SHUFFLE_ERROR_INVALID == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000675 return Error(IDLoc, "invalid instruction packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000676 else if (HexagonShuffler::SHUFFLE_ERROR_STORES == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000677 return Error(IDLoc, "invalid instruction packet: too many stores");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000678 else if (HexagonShuffler::SHUFFLE_ERROR_LOADS == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000679 return Error(IDLoc, "invalid instruction packet: too many loads");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000680 else if (HexagonShuffler::SHUFFLE_ERROR_BRANCHES == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000681 return Error(IDLoc, "too many branches in packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000682 else if (HexagonShuffler::SHUFFLE_ERROR_NOSLOTS == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000683 return Error(IDLoc, "invalid instruction packet: out of slots");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000684 else if (HexagonShuffler::SHUFFLE_ERROR_SLOTS == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000685 return Error(IDLoc, "invalid instruction packet: slot error");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000686 else if (HexagonShuffler::SHUFFLE_ERROR_ERRATA2 == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000687 return Error(IDLoc, "v60 packet violation");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000688 else if (HexagonShuffler::SHUFFLE_ERROR_STORE_LOAD_CONFLICT == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000689 return Error(IDLoc, "slot 0 instruction does not allow slot 1 store");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000690 else
Nirav Dave2364748a2016-09-16 18:30:20 +0000691 return Error(IDLoc, "unknown error in instruction packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000692 }
693 }
694
695 unsigned Warn = Check.getWarning();
696 if (Warn != HexagonMCErrInfo::CHECK_SUCCESS) {
697 if (HexagonMCErrInfo::CHECK_WARN_CURRENT & Warn)
698 Warning(IDLoc, "register `" + R + "' used with `.cur' "
699 "but not used in the same packet");
700 else if (HexagonMCErrInfo::CHECK_WARN_TEMPORARY & Warn)
701 Warning(IDLoc, "register `" + R + "' used with `.tmp' "
702 "but not used in the same packet");
703 }
704 }
705
706 if (CheckOk) {
707 MCB.setLoc(IDLoc);
708 if (HexagonMCInstrInfo::bundleSize(MCB) == 0) {
709 assert(!HexagonMCInstrInfo::isInnerLoop(MCB));
710 assert(!HexagonMCInstrInfo::isOuterLoop(MCB));
711 // Empty packets are valid yet aren't emitted
712 return false;
713 }
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000714 Out.EmitInstruction(MCB, getSTI());
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000715 } else {
716 // If compounding and duplexing didn't reduce the size below
717 // 4 or less we have a packet that is too big.
718 if (HexagonMCInstrInfo::bundleSize(MCB) > HEXAGON_PACKET_SIZE) {
719 Error(IDLoc, "invalid instruction packet: out of slots");
720 return true; // Error
721 }
722 }
723
724 return false; // No error
725}
726
727bool HexagonAsmParser::matchBundleOptions() {
728 MCAsmParser &Parser = getParser();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000729 while (true) {
730 if (!Parser.getTok().is(AsmToken::Colon))
731 return false;
Nirav Davefd910412016-06-17 16:06:17 +0000732 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000733 StringRef Option = Parser.getTok().getString();
734 if (Option.compare_lower("endloop0") == 0)
735 HexagonMCInstrInfo::setInnerLoop(MCB);
736 else if (Option.compare_lower("endloop1") == 0)
737 HexagonMCInstrInfo::setOuterLoop(MCB);
738 else if (Option.compare_lower("mem_noshuf") == 0)
739 HexagonMCInstrInfo::setMemReorderDisabled(MCB);
740 else if (Option.compare_lower("mem_shuf") == 0)
741 HexagonMCInstrInfo::setMemStoreReorderEnabled(MCB);
742 else
743 return true;
Nirav Davefd910412016-06-17 16:06:17 +0000744 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000745 }
746}
747
748// For instruction aliases, immediates are generated rather than
749// MCConstantExpr. Convert them for uniform MCExpr.
750// Also check for signed/unsigned mismatches and warn
751void HexagonAsmParser::canonicalizeImmediates(MCInst &MCI) {
752 MCInst NewInst;
753 NewInst.setOpcode(MCI.getOpcode());
754 for (MCOperand &I : MCI)
755 if (I.isImm()) {
756 int64_t Value (I.getImm());
Colin LeMahieuc7b21242016-02-15 18:47:55 +0000757 NewInst.addOperand(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000758 MCConstantExpr::create(Value, getContext()), getContext())));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000759 }
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000760 else {
761 if (I.isExpr() && cast<HexagonMCExpr>(I.getExpr())->signMismatch() &&
762 WarnSignedMismatch)
763 Warning (MCI.getLoc(), "Signed/Unsigned mismatch");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000764 NewInst.addOperand(I);
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000765 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000766 MCI = NewInst;
767}
768
769bool HexagonAsmParser::matchOneInstruction(MCInst &MCI, SMLoc IDLoc,
770 OperandVector &InstOperands,
771 uint64_t &ErrorInfo,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000772 bool MatchingInlineAsm) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000773 // Perform matching with tablegen asmmatcher generated function
774 int result =
775 MatchInstructionImpl(InstOperands, MCI, ErrorInfo, MatchingInlineAsm);
776 if (result == Match_Success) {
777 MCI.setLoc(IDLoc);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000778 canonicalizeImmediates(MCI);
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000779 result = processInstruction(MCI, InstOperands, IDLoc);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000780
781 DEBUG(dbgs() << "Insn:");
782 DEBUG(MCI.dump_pretty(dbgs()));
783 DEBUG(dbgs() << "\n\n");
784
785 MCI.setLoc(IDLoc);
786 }
787
788 // Create instruction operand for bundle instruction
789 // Break this into a separate function Code here is less readable
790 // Think about how to get an instruction error to report correctly.
791 // SMLoc will return the "{"
792 switch (result) {
793 default:
794 break;
795 case Match_Success:
796 return false;
797 case Match_MissingFeature:
798 return Error(IDLoc, "invalid instruction");
799 case Match_MnemonicFail:
800 return Error(IDLoc, "unrecognized instruction");
801 case Match_InvalidOperand:
802 SMLoc ErrorLoc = IDLoc;
803 if (ErrorInfo != ~0U) {
804 if (ErrorInfo >= InstOperands.size())
805 return Error(IDLoc, "too few operands for instruction");
806
807 ErrorLoc = (static_cast<HexagonOperand *>(InstOperands[ErrorInfo].get()))
808 ->getStartLoc();
809 if (ErrorLoc == SMLoc())
810 ErrorLoc = IDLoc;
811 }
812 return Error(ErrorLoc, "invalid operand for instruction");
813 }
814 llvm_unreachable("Implement any new match types added!");
815}
816
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000817bool HexagonAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
818 OperandVector &Operands,
819 MCStreamer &Out,
820 uint64_t &ErrorInfo,
821 bool MatchingInlineAsm) {
822 if (!InBrackets) {
823 MCB.clear();
824 MCB.addOperand(MCOperand::createImm(0));
825 }
826 HexagonOperand &FirstOperand = static_cast<HexagonOperand &>(*Operands[0]);
827 if (FirstOperand.isToken() && FirstOperand.getToken() == "{") {
828 assert(Operands.size() == 1 && "Brackets should be by themselves");
829 if (InBrackets) {
830 getParser().Error(IDLoc, "Already in a packet");
831 return true;
832 }
833 InBrackets = true;
834 return false;
835 }
836 if (FirstOperand.isToken() && FirstOperand.getToken() == "}") {
837 assert(Operands.size() == 1 && "Brackets should be by themselves");
838 if (!InBrackets) {
839 getParser().Error(IDLoc, "Not in a packet");
840 return true;
841 }
842 InBrackets = false;
843 if (matchBundleOptions())
844 return true;
845 return finishBundle(IDLoc, Out);
846 }
847 MCInst *SubInst = new (getParser().getContext()) MCInst;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000848 if (matchOneInstruction(*SubInst, IDLoc, Operands, ErrorInfo,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000849 MatchingInlineAsm))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000850 return true;
851 HexagonMCInstrInfo::extendIfNeeded(
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000852 getParser().getContext(), MCII, MCB, *SubInst);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000853 MCB.addOperand(MCOperand::createInst(SubInst));
854 if (!InBrackets)
855 return finishBundle(IDLoc, Out);
856 return false;
857}
858
859/// ParseDirective parses the Hexagon specific directives
860bool HexagonAsmParser::ParseDirective(AsmToken DirectiveID) {
861 StringRef IDVal = DirectiveID.getIdentifier();
862 if ((IDVal.lower() == ".word") || (IDVal.lower() == ".4byte"))
863 return ParseDirectiveValue(4, DirectiveID.getLoc());
864 if (IDVal.lower() == ".short" || IDVal.lower() == ".hword" ||
865 IDVal.lower() == ".half")
866 return ParseDirectiveValue(2, DirectiveID.getLoc());
867 if (IDVal.lower() == ".falign")
868 return ParseDirectiveFalign(256, DirectiveID.getLoc());
869 if ((IDVal.lower() == ".lcomm") || (IDVal.lower() == ".lcommon"))
870 return ParseDirectiveComm(true, DirectiveID.getLoc());
871 if ((IDVal.lower() == ".comm") || (IDVal.lower() == ".common"))
872 return ParseDirectiveComm(false, DirectiveID.getLoc());
873 if (IDVal.lower() == ".subsection")
874 return ParseDirectiveSubsection(DirectiveID.getLoc());
875
876 return true;
877}
878bool HexagonAsmParser::ParseDirectiveSubsection(SMLoc L) {
879 const MCExpr *Subsection = 0;
880 int64_t Res;
881
882 assert((getLexer().isNot(AsmToken::EndOfStatement)) &&
883 "Invalid subsection directive");
884 getParser().parseExpression(Subsection);
885
886 if (!Subsection->evaluateAsAbsolute(Res))
887 return Error(L, "Cannot evaluate subsection number");
888
889 if (getLexer().isNot(AsmToken::EndOfStatement))
890 return TokError("unexpected token in directive");
891
892 // 0-8192 is the hard-coded range in MCObjectStreamper.cpp, this keeps the
893 // negative subsections together and in the same order but at the opposite
894 // end of the section. Only legacy hexagon-gcc created assembly code
895 // used negative subsections.
896 if ((Res < 0) && (Res > -8193))
Colin LeMahieuc7b21242016-02-15 18:47:55 +0000897 Subsection = HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000898 MCConstantExpr::create(8192 + Res, getContext()), getContext());
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000899
900 getStreamer().SubSection(Subsection);
901 return false;
902}
903
904/// ::= .falign [expression]
905bool HexagonAsmParser::ParseDirectiveFalign(unsigned Size, SMLoc L) {
906
907 int64_t MaxBytesToFill = 15;
908
Simon Pilgrim6ba672e2016-11-17 19:21:20 +0000909 // if there is an argument
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000910 if (getLexer().isNot(AsmToken::EndOfStatement)) {
911 const MCExpr *Value;
912 SMLoc ExprLoc = L;
913
914 // Make sure we have a number (false is returned if expression is a number)
915 if (getParser().parseExpression(Value) == false) {
916 // Make sure this is a number that is in range
917 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
918 uint64_t IntValue = MCE->getValue();
919 if (!isUIntN(Size, IntValue) && !isIntN(Size, IntValue))
920 return Error(ExprLoc, "literal value out of range (256) for falign");
921 MaxBytesToFill = IntValue;
922 Lex();
923 } else {
924 return Error(ExprLoc, "not a valid expression for falign directive");
925 }
926 }
927
928 getTargetStreamer().emitFAlign(16, MaxBytesToFill);
929 Lex();
930
931 return false;
932}
933
934/// ::= .word [ expression (, expression)* ]
935bool HexagonAsmParser::ParseDirectiveValue(unsigned Size, SMLoc L) {
936 if (getLexer().isNot(AsmToken::EndOfStatement)) {
937
938 for (;;) {
939 const MCExpr *Value;
940 SMLoc ExprLoc = L;
941 if (getParser().parseExpression(Value))
942 return true;
943
944 // Special case constant expressions to match code generator.
945 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
946 assert(Size <= 8 && "Invalid size");
947 uint64_t IntValue = MCE->getValue();
948 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
949 return Error(ExprLoc, "literal value out of range for directive");
950 getStreamer().EmitIntValue(IntValue, Size);
951 } else
952 getStreamer().EmitValue(Value, Size);
953
954 if (getLexer().is(AsmToken::EndOfStatement))
955 break;
956
957 // FIXME: Improve diagnostic.
958 if (getLexer().isNot(AsmToken::Comma))
959 return TokError("unexpected token in directive");
960 Lex();
961 }
962 }
963
964 Lex();
965 return false;
966}
967
968// This is largely a copy of AsmParser's ParseDirectiveComm extended to
969// accept a 3rd argument, AccessAlignment which indicates the smallest
970// memory access made to the symbol, expressed in bytes. If no
971// AccessAlignment is specified it defaults to the Alignment Value.
972// Hexagon's .lcomm:
973// .lcomm Symbol, Length, Alignment, AccessAlignment
974bool HexagonAsmParser::ParseDirectiveComm(bool IsLocal, SMLoc Loc) {
975 // FIXME: need better way to detect if AsmStreamer (upstream removed
976 // getKind())
977 if (getStreamer().hasRawTextSupport())
978 return true; // Only object file output requires special treatment.
979
980 StringRef Name;
981 if (getParser().parseIdentifier(Name))
982 return TokError("expected identifier in directive");
983 // Handle the identifier as the key symbol.
984 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
985
986 if (getLexer().isNot(AsmToken::Comma))
987 return TokError("unexpected token in directive");
988 Lex();
989
990 int64_t Size;
991 SMLoc SizeLoc = getLexer().getLoc();
992 if (getParser().parseAbsoluteExpression(Size))
993 return true;
994
995 int64_t ByteAlignment = 1;
996 SMLoc ByteAlignmentLoc;
997 if (getLexer().is(AsmToken::Comma)) {
998 Lex();
999 ByteAlignmentLoc = getLexer().getLoc();
1000 if (getParser().parseAbsoluteExpression(ByteAlignment))
1001 return true;
1002 if (!isPowerOf2_64(ByteAlignment))
1003 return Error(ByteAlignmentLoc, "alignment must be a power of 2");
1004 }
1005
1006 int64_t AccessAlignment = 0;
1007 if (getLexer().is(AsmToken::Comma)) {
1008 // The optional access argument specifies the size of the smallest memory
1009 // access to be made to the symbol, expressed in bytes.
1010 SMLoc AccessAlignmentLoc;
1011 Lex();
1012 AccessAlignmentLoc = getLexer().getLoc();
1013 if (getParser().parseAbsoluteExpression(AccessAlignment))
1014 return true;
1015
1016 if (!isPowerOf2_64(AccessAlignment))
1017 return Error(AccessAlignmentLoc, "access alignment must be a power of 2");
1018 }
1019
1020 if (getLexer().isNot(AsmToken::EndOfStatement))
1021 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1022
1023 Lex();
1024
1025 // NOTE: a size of zero for a .comm should create a undefined symbol
1026 // but a size of .lcomm creates a bss symbol of size zero.
1027 if (Size < 0)
1028 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1029 "be less than zero");
1030
1031 // NOTE: The alignment in the directive is a power of 2 value, the assembler
1032 // may internally end up wanting an alignment in bytes.
1033 // FIXME: Diagnose overflow.
1034 if (ByteAlignment < 0)
1035 return Error(ByteAlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1036 "alignment, can't be less than zero");
1037
1038 if (!Sym->isUndefined())
1039 return Error(Loc, "invalid symbol redefinition");
1040
1041 HexagonMCELFStreamer &HexagonELFStreamer =
1042 static_cast<HexagonMCELFStreamer &>(getStreamer());
1043 if (IsLocal) {
1044 HexagonELFStreamer.HexagonMCEmitLocalCommonSymbol(Sym, Size, ByteAlignment,
1045 AccessAlignment);
1046 return false;
1047 }
1048
1049 HexagonELFStreamer.HexagonMCEmitCommonSymbol(Sym, Size, ByteAlignment,
1050 AccessAlignment);
1051 return false;
1052}
1053
1054// validate register against architecture
1055bool HexagonAsmParser::RegisterMatchesArch(unsigned MatchNum) const {
1056 return true;
1057}
1058
1059// extern "C" void LLVMInitializeHexagonAsmLexer();
1060
1061/// Force static initialization.
1062extern "C" void LLVMInitializeHexagonAsmParser() {
Mehdi Aminif42454b2016-10-09 23:00:34 +00001063 RegisterMCAsmParser<HexagonAsmParser> X(getTheHexagonTarget());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001064}
1065
1066#define GET_MATCHER_IMPLEMENTATION
1067#define GET_REGISTER_MATCHER
1068#include "HexagonGenAsmMatcher.inc"
1069
1070namespace {
1071bool previousEqual(OperandVector &Operands, size_t Index, StringRef String) {
1072 if (Index >= Operands.size())
1073 return false;
1074 MCParsedAsmOperand &Operand = *Operands[Operands.size() - Index - 1];
1075 if (!Operand.isToken())
1076 return false;
1077 return static_cast<HexagonOperand &>(Operand).getToken().equals_lower(String);
1078}
1079bool previousIsLoop(OperandVector &Operands, size_t Index) {
1080 return previousEqual(Operands, Index, "loop0") ||
1081 previousEqual(Operands, Index, "loop1") ||
1082 previousEqual(Operands, Index, "sp1loop0") ||
1083 previousEqual(Operands, Index, "sp2loop0") ||
1084 previousEqual(Operands, Index, "sp3loop0");
1085}
1086}
1087
1088bool HexagonAsmParser::splitIdentifier(OperandVector &Operands) {
1089 AsmToken const &Token = getParser().getTok();
1090 StringRef String = Token.getString();
1091 SMLoc Loc = Token.getLoc();
Nirav Davefd910412016-06-17 16:06:17 +00001092 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001093 do {
1094 std::pair<StringRef, StringRef> HeadTail = String.split('.');
1095 if (!HeadTail.first.empty())
1096 Operands.push_back(HexagonOperand::CreateToken(HeadTail.first, Loc));
1097 if (!HeadTail.second.empty())
1098 Operands.push_back(HexagonOperand::CreateToken(
1099 String.substr(HeadTail.first.size(), 1), Loc));
1100 String = HeadTail.second;
1101 } while (!String.empty());
1102 return false;
1103}
1104
1105bool HexagonAsmParser::parseOperand(OperandVector &Operands) {
1106 unsigned Register;
1107 SMLoc Begin;
1108 SMLoc End;
1109 MCAsmLexer &Lexer = getLexer();
1110 if (!ParseRegister(Register, Begin, End)) {
1111 if (!ErrorMissingParenthesis)
1112 switch (Register) {
1113 default:
1114 break;
1115 case Hexagon::P0:
1116 case Hexagon::P1:
1117 case Hexagon::P2:
1118 case Hexagon::P3:
1119 if (previousEqual(Operands, 0, "if")) {
1120 if (WarnMissingParenthesis)
1121 Warning (Begin, "Missing parenthesis around predicate register");
1122 static char const *LParen = "(";
1123 static char const *RParen = ")";
1124 Operands.push_back(HexagonOperand::CreateToken(LParen, Begin));
1125 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End));
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +00001126 const AsmToken &MaybeDotNew = Lexer.getTok();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001127 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) &&
1128 MaybeDotNew.getString().equals_lower(".new"))
1129 splitIdentifier(Operands);
1130 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin));
1131 return false;
1132 }
1133 if (previousEqual(Operands, 0, "!") &&
1134 previousEqual(Operands, 1, "if")) {
1135 if (WarnMissingParenthesis)
1136 Warning (Begin, "Missing parenthesis around predicate register");
1137 static char const *LParen = "(";
1138 static char const *RParen = ")";
1139 Operands.insert(Operands.end () - 1,
1140 HexagonOperand::CreateToken(LParen, Begin));
1141 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End));
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +00001142 const AsmToken &MaybeDotNew = Lexer.getTok();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001143 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) &&
1144 MaybeDotNew.getString().equals_lower(".new"))
1145 splitIdentifier(Operands);
1146 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin));
1147 return false;
1148 }
1149 break;
1150 }
1151 Operands.push_back(HexagonOperand::CreateReg(
1152 Register, Begin, End));
1153 return false;
1154 }
1155 return splitIdentifier(Operands);
1156}
1157
1158bool HexagonAsmParser::isLabel(AsmToken &Token) {
1159 MCAsmLexer &Lexer = getLexer();
1160 AsmToken const &Second = Lexer.getTok();
1161 AsmToken Third = Lexer.peekTok();
1162 StringRef String = Token.getString();
1163 if (Token.is(AsmToken::TokenKind::LCurly) ||
1164 Token.is(AsmToken::TokenKind::RCurly))
1165 return false;
1166 if (!Token.is(AsmToken::TokenKind::Identifier))
1167 return true;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001168 if (!matchRegister(String.lower()))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001169 return true;
Colin LeMahieu9d851f02015-11-09 21:06:28 +00001170 (void)Second;
1171 assert(Second.is(AsmToken::Colon));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001172 StringRef Raw (String.data(), Third.getString().data() - String.data() +
1173 Third.getString().size());
1174 std::string Collapsed = Raw;
David Majnemerc7004902016-08-12 04:32:37 +00001175 Collapsed.erase(remove_if(Collapsed, isspace), Collapsed.end());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001176 StringRef Whole = Collapsed;
1177 std::pair<StringRef, StringRef> DotSplit = Whole.split('.');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001178 if (!matchRegister(DotSplit.first.lower()))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001179 return true;
1180 return false;
1181}
1182
1183bool HexagonAsmParser::handleNoncontigiousRegister(bool Contigious, SMLoc &Loc) {
1184 if (!Contigious && ErrorNoncontigiousRegister) {
1185 Error(Loc, "Register name is not contigious");
1186 return true;
1187 }
1188 if (!Contigious && WarnNoncontigiousRegister)
1189 Warning(Loc, "Register name is not contigious");
1190 return false;
1191}
1192
1193bool HexagonAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) {
1194 MCAsmLexer &Lexer = getLexer();
1195 StartLoc = getLexer().getLoc();
1196 SmallVector<AsmToken, 5> Lookahead;
1197 StringRef RawString(Lexer.getTok().getString().data(), 0);
1198 bool Again = Lexer.is(AsmToken::Identifier);
1199 bool NeededWorkaround = false;
1200 while (Again) {
1201 AsmToken const &Token = Lexer.getTok();
1202 RawString = StringRef(RawString.data(),
1203 Token.getString().data() - RawString.data () +
1204 Token.getString().size());
1205 Lookahead.push_back(Token);
1206 Lexer.Lex();
1207 bool Contigious = Lexer.getTok().getString().data() ==
1208 Lookahead.back().getString().data() +
1209 Lookahead.back().getString().size();
1210 bool Type = Lexer.is(AsmToken::Identifier) || Lexer.is(AsmToken::Dot) ||
1211 Lexer.is(AsmToken::Integer) || Lexer.is(AsmToken::Real) ||
1212 Lexer.is(AsmToken::Colon);
1213 bool Workaround = Lexer.is(AsmToken::Colon) ||
1214 Lookahead.back().is(AsmToken::Colon);
1215 Again = (Contigious && Type) || (Workaround && Type);
1216 NeededWorkaround = NeededWorkaround || (Again && !(Contigious && Type));
1217 }
1218 std::string Collapsed = RawString;
David Majnemerc7004902016-08-12 04:32:37 +00001219 Collapsed.erase(remove_if(Collapsed, isspace), Collapsed.end());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001220 StringRef FullString = Collapsed;
1221 std::pair<StringRef, StringRef> DotSplit = FullString.split('.');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001222 unsigned DotReg = matchRegister(DotSplit.first.lower());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001223 if (DotReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1224 if (DotSplit.second.empty()) {
1225 RegNo = DotReg;
1226 EndLoc = Lexer.getLoc();
1227 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1228 return true;
1229 return false;
1230 } else {
1231 RegNo = DotReg;
1232 size_t First = RawString.find('.');
1233 StringRef DotString (RawString.data() + First, RawString.size() - First);
1234 Lexer.UnLex(AsmToken(AsmToken::Identifier, DotString));
1235 EndLoc = Lexer.getLoc();
1236 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1237 return true;
1238 return false;
1239 }
1240 }
1241 std::pair<StringRef, StringRef> ColonSplit = StringRef(FullString).split(':');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001242 unsigned ColonReg = matchRegister(ColonSplit.first.lower());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001243 if (ColonReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1244 Lexer.UnLex(Lookahead.back());
1245 Lookahead.pop_back();
1246 Lexer.UnLex(Lookahead.back());
1247 Lookahead.pop_back();
1248 RegNo = ColonReg;
1249 EndLoc = Lexer.getLoc();
1250 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1251 return true;
1252 return false;
1253 }
1254 while (!Lookahead.empty()) {
1255 Lexer.UnLex(Lookahead.back());
1256 Lookahead.pop_back();
1257 }
1258 return true;
1259}
1260
1261bool HexagonAsmParser::implicitExpressionLocation(OperandVector &Operands) {
1262 if (previousEqual(Operands, 0, "call"))
1263 return true;
1264 if (previousEqual(Operands, 0, "jump"))
1265 if (!getLexer().getTok().is(AsmToken::Colon))
1266 return true;
1267 if (previousEqual(Operands, 0, "(") && previousIsLoop(Operands, 1))
1268 return true;
1269 if (previousEqual(Operands, 1, ":") && previousEqual(Operands, 2, "jump") &&
1270 (previousEqual(Operands, 0, "nt") || previousEqual(Operands, 0, "t")))
1271 return true;
1272 return false;
1273}
1274
1275bool HexagonAsmParser::parseExpression(MCExpr const *& Expr) {
1276 llvm::SmallVector<AsmToken, 4> Tokens;
1277 MCAsmLexer &Lexer = getLexer();
1278 bool Done = false;
1279 static char const * Comma = ",";
1280 do {
1281 Tokens.emplace_back (Lexer.getTok());
Nirav Davefd910412016-06-17 16:06:17 +00001282 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001283 switch (Tokens.back().getKind())
1284 {
1285 case AsmToken::TokenKind::Hash:
1286 if (Tokens.size () > 1)
1287 if ((Tokens.end () - 2)->getKind() == AsmToken::TokenKind::Plus) {
1288 Tokens.insert(Tokens.end() - 2,
1289 AsmToken(AsmToken::TokenKind::Comma, Comma));
1290 Done = true;
1291 }
1292 break;
1293 case AsmToken::TokenKind::RCurly:
1294 case AsmToken::TokenKind::EndOfStatement:
1295 case AsmToken::TokenKind::Eof:
1296 Done = true;
1297 break;
1298 default:
1299 break;
1300 }
1301 } while (!Done);
1302 while (!Tokens.empty()) {
1303 Lexer.UnLex(Tokens.back());
1304 Tokens.pop_back();
1305 }
1306 return getParser().parseExpression(Expr);
1307}
1308
1309bool HexagonAsmParser::parseExpressionOrOperand(OperandVector &Operands) {
1310 if (implicitExpressionLocation(Operands)) {
1311 MCAsmParser &Parser = getParser();
1312 SMLoc Loc = Parser.getLexer().getLoc();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001313 MCExpr const *Expr = nullptr;
1314 bool Error = parseExpression(Expr);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001315 Expr = HexagonMCExpr::create(Expr, getContext());
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001316 if (!Error)
1317 Operands.push_back(HexagonOperand::CreateImm(Expr, Loc, Loc));
1318 return Error;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001319 }
1320 return parseOperand(Operands);
1321}
1322
1323/// Parse an instruction.
1324bool HexagonAsmParser::parseInstruction(OperandVector &Operands) {
1325 MCAsmParser &Parser = getParser();
1326 MCAsmLexer &Lexer = getLexer();
1327 while (true) {
1328 AsmToken const &Token = Parser.getTok();
1329 switch (Token.getKind()) {
1330 case AsmToken::EndOfStatement: {
Nirav Davefd910412016-06-17 16:06:17 +00001331 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001332 return false;
1333 }
1334 case AsmToken::LCurly: {
1335 if (!Operands.empty())
1336 return true;
1337 Operands.push_back(
1338 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001339 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001340 return false;
1341 }
1342 case AsmToken::RCurly: {
1343 if (Operands.empty()) {
1344 Operands.push_back(
1345 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001346 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001347 }
1348 return false;
1349 }
1350 case AsmToken::Comma: {
Nirav Davefd910412016-06-17 16:06:17 +00001351 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001352 continue;
1353 }
1354 case AsmToken::EqualEqual:
1355 case AsmToken::ExclaimEqual:
1356 case AsmToken::GreaterEqual:
1357 case AsmToken::GreaterGreater:
1358 case AsmToken::LessEqual:
1359 case AsmToken::LessLess: {
1360 Operands.push_back(HexagonOperand::CreateToken(
1361 Token.getString().substr(0, 1), Token.getLoc()));
1362 Operands.push_back(HexagonOperand::CreateToken(
1363 Token.getString().substr(1, 1), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001364 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001365 continue;
1366 }
1367 case AsmToken::Hash: {
1368 bool MustNotExtend = false;
1369 bool ImplicitExpression = implicitExpressionLocation(Operands);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001370 SMLoc ExprLoc = Lexer.getLoc();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001371 if (!ImplicitExpression)
1372 Operands.push_back(
1373 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001374 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001375 bool MustExtend = false;
1376 bool HiOnly = false;
1377 bool LoOnly = false;
1378 if (Lexer.is(AsmToken::Hash)) {
Nirav Davefd910412016-06-17 16:06:17 +00001379 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001380 MustExtend = true;
1381 } else if (ImplicitExpression)
1382 MustNotExtend = true;
1383 AsmToken const &Token = Parser.getTok();
1384 if (Token.is(AsmToken::Identifier)) {
1385 StringRef String = Token.getString();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001386 if (String.lower() == "hi") {
1387 HiOnly = true;
1388 } else if (String.lower() == "lo") {
1389 LoOnly = true;
1390 }
1391 if (HiOnly || LoOnly) {
1392 AsmToken LParen = Lexer.peekTok();
1393 if (!LParen.is(AsmToken::LParen)) {
1394 HiOnly = false;
1395 LoOnly = false;
1396 } else {
Nirav Davefd910412016-06-17 16:06:17 +00001397 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001398 }
1399 }
1400 }
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001401 MCExpr const *Expr = nullptr;
1402 if (parseExpression(Expr))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001403 return true;
1404 int64_t Value;
1405 MCContext &Context = Parser.getContext();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001406 assert(Expr != nullptr);
1407 if (Expr->evaluateAsAbsolute(Value)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001408 if (HiOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001409 Expr = MCBinaryExpr::createLShr(
1410 Expr, MCConstantExpr::create(16, Context), Context);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001411 if (HiOnly || LoOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001412 Expr = MCBinaryExpr::createAnd(Expr,
1413 MCConstantExpr::create(0xffff, Context),
1414 Context);
Colin LeMahieu5cb6eea2016-03-01 21:37:41 +00001415 } else {
1416 MCValue Value;
1417 if (Expr->evaluateAsRelocatable(Value, nullptr, nullptr)) {
1418 if (!Value.isAbsolute()) {
1419 switch(Value.getAccessVariant()) {
1420 case MCSymbolRefExpr::VariantKind::VK_TPREL:
1421 case MCSymbolRefExpr::VariantKind::VK_DTPREL:
1422 // Don't lazy extend these expression variants
1423 MustNotExtend = !MustExtend;
1424 break;
1425 default:
1426 break;
1427 }
1428 }
1429 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001430 }
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001431 Expr = HexagonMCExpr::create(Expr, Context);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001432 HexagonMCInstrInfo::setMustNotExtend(*Expr, MustNotExtend);
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001433 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001434 std::unique_ptr<HexagonOperand> Operand =
1435 HexagonOperand::CreateImm(Expr, ExprLoc, ExprLoc);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001436 Operands.push_back(std::move(Operand));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001437 continue;
1438 }
1439 default:
1440 break;
1441 }
1442 if (parseExpressionOrOperand(Operands))
1443 return true;
1444 }
1445}
1446
1447bool HexagonAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1448 StringRef Name,
1449 AsmToken ID,
1450 OperandVector &Operands) {
1451 getLexer().UnLex(ID);
1452 return parseInstruction(Operands);
1453}
1454
1455namespace {
1456MCInst makeCombineInst(int opCode, MCOperand &Rdd,
1457 MCOperand &MO1, MCOperand &MO2) {
1458 MCInst TmpInst;
1459 TmpInst.setOpcode(opCode);
1460 TmpInst.addOperand(Rdd);
1461 TmpInst.addOperand(MO1);
1462 TmpInst.addOperand(MO2);
1463
1464 return TmpInst;
1465}
1466}
1467
1468// Define this matcher function after the auto-generated include so we
1469// have the match class enum definitions.
1470unsigned HexagonAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
1471 unsigned Kind) {
1472 HexagonOperand *Op = static_cast<HexagonOperand *>(&AsmOp);
1473
1474 switch (Kind) {
1475 case MCK_0: {
1476 int64_t Value;
1477 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 0
1478 ? Match_Success
1479 : Match_InvalidOperand;
1480 }
1481 case MCK_1: {
1482 int64_t Value;
1483 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 1
1484 ? Match_Success
1485 : Match_InvalidOperand;
1486 }
1487 case MCK__MINUS_1: {
1488 int64_t Value;
1489 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == -1
1490 ? Match_Success
1491 : Match_InvalidOperand;
1492 }
1493 }
1494 if (Op->Kind == HexagonOperand::Token && Kind != InvalidMatchClass) {
1495 StringRef myStringRef = StringRef(Op->Tok.Data, Op->Tok.Length);
1496 if (matchTokenString(myStringRef.lower()) == (MatchClassKind)Kind)
1497 return Match_Success;
1498 if (matchTokenString(myStringRef.upper()) == (MatchClassKind)Kind)
1499 return Match_Success;
1500 }
1501
1502 DEBUG(dbgs() << "Unmatched Operand:");
1503 DEBUG(Op->dump());
1504 DEBUG(dbgs() << "\n");
1505
1506 return Match_InvalidOperand;
1507}
1508
Nirav Dave2364748a2016-09-16 18:30:20 +00001509// FIXME: Calls to OutOfRange shoudl propagate failure up to parseStatement.
1510bool HexagonAsmParser::OutOfRange(SMLoc IDLoc, long long Val, long long Max) {
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001511 std::string errStr;
1512 raw_string_ostream ES(errStr);
Alexey Samsonov44ff2042015-12-02 22:59:22 +00001513 ES << "value " << Val << "(" << format_hex(Val, 0) << ") out of range: ";
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001514 if (Max >= 0)
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001515 ES << "0-" << Max;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001516 else
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001517 ES << Max << "-" << (-Max - 1);
Malcolm Parsons06ac79c2016-11-02 16:43:50 +00001518 return Parser.printError(IDLoc, ES.str());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001519}
1520
1521int HexagonAsmParser::processInstruction(MCInst &Inst,
1522 OperandVector const &Operands,
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001523 SMLoc IDLoc) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001524 MCContext &Context = getParser().getContext();
1525 const MCRegisterInfo *RI = getContext().getRegisterInfo();
1526 std::string r = "r";
1527 std::string v = "v";
1528 std::string Colon = ":";
1529
1530 bool is32bit = false; // used to distinguish between CONST32 and CONST64
1531 switch (Inst.getOpcode()) {
1532 default:
1533 break;
1534
Colin LeMahieuecef1d92016-02-16 20:38:17 +00001535 case Hexagon::A2_iconst: {
1536 Inst.setOpcode(Hexagon::A2_addi);
1537 MCOperand Reg = Inst.getOperand(0);
1538 MCOperand S16 = Inst.getOperand(1);
1539 HexagonMCInstrInfo::setMustNotExtend(*S16.getExpr());
1540 HexagonMCInstrInfo::setS23_2_reloc(*S16.getExpr());
1541 Inst.clear();
1542 Inst.addOperand(Reg);
1543 Inst.addOperand(MCOperand::createReg(Hexagon::R0));
1544 Inst.addOperand(S16);
1545 break;
1546 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001547 case Hexagon::M4_mpyrr_addr:
1548 case Hexagon::S4_addi_asl_ri:
1549 case Hexagon::S4_addi_lsr_ri:
1550 case Hexagon::S4_andi_asl_ri:
1551 case Hexagon::S4_andi_lsr_ri:
1552 case Hexagon::S4_ori_asl_ri:
1553 case Hexagon::S4_ori_lsr_ri:
1554 case Hexagon::S4_or_andix:
1555 case Hexagon::S4_subi_asl_ri:
1556 case Hexagon::S4_subi_lsr_ri: {
1557 MCOperand &Ry = Inst.getOperand(0);
1558 MCOperand &src = Inst.getOperand(2);
1559 if (RI->getEncodingValue(Ry.getReg()) != RI->getEncodingValue(src.getReg()))
1560 return Match_InvalidOperand;
1561 break;
1562 }
1563
1564 case Hexagon::C2_cmpgei: {
1565 MCOperand &MO = Inst.getOperand(2);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001566 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001567 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001568 Inst.setOpcode(Hexagon::C2_cmpgti);
1569 break;
1570 }
1571
1572 case Hexagon::C2_cmpgeui: {
1573 MCOperand &MO = Inst.getOperand(2);
1574 int64_t Value;
1575 bool Success = MO.getExpr()->evaluateAsAbsolute(Value);
Colin LeMahieu9d851f02015-11-09 21:06:28 +00001576 (void)Success;
1577 assert(Success && "Assured by matcher");
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001578 if (Value == 0) {
1579 MCInst TmpInst;
1580 MCOperand &Pd = Inst.getOperand(0);
1581 MCOperand &Rt = Inst.getOperand(1);
1582 TmpInst.setOpcode(Hexagon::C2_cmpeq);
1583 TmpInst.addOperand(Pd);
1584 TmpInst.addOperand(Rt);
1585 TmpInst.addOperand(Rt);
1586 Inst = TmpInst;
1587 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001588 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001589 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001590 Inst.setOpcode(Hexagon::C2_cmpgtui);
1591 }
1592 break;
1593 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001594
1595 // Translate a "$Rdd = $Rss" to "$Rdd = combine($Rs, $Rt)"
1596 case Hexagon::A2_tfrp: {
1597 MCOperand &MO = Inst.getOperand(1);
1598 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001599 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001600 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001601 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001602 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001603 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001604 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001605 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001606 Inst.setOpcode(Hexagon::A2_combinew);
1607 break;
1608 }
1609
1610 case Hexagon::A2_tfrpt:
1611 case Hexagon::A2_tfrpf: {
1612 MCOperand &MO = Inst.getOperand(2);
1613 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001614 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001615 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001616 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001617 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001618 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001619 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001620 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001621 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrpt)
1622 ? Hexagon::C2_ccombinewt
1623 : Hexagon::C2_ccombinewf);
1624 break;
1625 }
1626 case Hexagon::A2_tfrptnew:
1627 case Hexagon::A2_tfrpfnew: {
1628 MCOperand &MO = Inst.getOperand(2);
1629 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001630 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001631 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001632 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001633 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001634 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001635 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001636 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001637 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrptnew)
1638 ? Hexagon::C2_ccombinewnewt
1639 : Hexagon::C2_ccombinewnewf);
1640 break;
1641 }
1642
Krzysztof Parzyszek0e7d2d32016-04-28 16:43:16 +00001643 // Translate a "$Vdd = $Vss" to "$Vdd = vcombine($Vs, $Vt)"
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001644 case Hexagon::V6_vassignp: {
Krzysztof Parzyszek0e7d2d32016-04-28 16:43:16 +00001645 MCOperand &MO = Inst.getOperand(1);
1646 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
1647 std::string R1 = v + llvm::utostr(RegPairNum + 1);
1648 MO.setReg(MatchRegisterName(R1));
1649 // Add a new operand for the second register in the pair.
1650 std::string R2 = v + llvm::utostr(RegPairNum);
1651 Inst.addOperand(MCOperand::createReg(MatchRegisterName(R2)));
1652 Inst.setOpcode(Hexagon::V6_vcombine);
1653 break;
1654 }
1655
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001656 // Translate a "$Rx = CONST32(#imm)" to "$Rx = memw(gp+#LABEL) "
1657 case Hexagon::CONST32:
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001658 is32bit = true;
1659 // Translate a "$Rx:y = CONST64(#imm)" to "$Rx:y = memd(gp+#LABEL) "
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001660 case Hexagon::CONST64:
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001661 // FIXME: need better way to detect AsmStreamer (upstream removed getKind())
1662 if (!Parser.getStreamer().hasRawTextSupport()) {
1663 MCELFStreamer *MES = static_cast<MCELFStreamer *>(&Parser.getStreamer());
1664 MCOperand &MO_1 = Inst.getOperand(1);
1665 MCOperand &MO_0 = Inst.getOperand(0);
1666
1667 // push section onto section stack
1668 MES->PushSection();
1669
1670 std::string myCharStr;
1671 MCSectionELF *mySection;
1672
1673 // check if this as an immediate or a symbol
1674 int64_t Value;
1675 bool Absolute = MO_1.getExpr()->evaluateAsAbsolute(Value);
1676 if (Absolute) {
1677 // Create a new section - one for each constant
1678 // Some or all of the zeros are replaced with the given immediate.
1679 if (is32bit) {
1680 std::string myImmStr = utohexstr(static_cast<uint32_t>(Value));
1681 myCharStr = StringRef(".gnu.linkonce.l4.CONST_00000000")
1682 .drop_back(myImmStr.size())
1683 .str() +
1684 myImmStr;
1685 } else {
1686 std::string myImmStr = utohexstr(Value);
1687 myCharStr = StringRef(".gnu.linkonce.l8.CONST_0000000000000000")
1688 .drop_back(myImmStr.size())
1689 .str() +
1690 myImmStr;
1691 }
1692
1693 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1694 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1695 } else if (MO_1.isExpr()) {
1696 // .lita - for expressions
1697 myCharStr = ".lita";
1698 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1699 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1700 } else
1701 llvm_unreachable("unexpected type of machine operand!");
1702
1703 MES->SwitchSection(mySection);
1704 unsigned byteSize = is32bit ? 4 : 8;
1705 getStreamer().EmitCodeAlignment(byteSize, byteSize);
1706
1707 MCSymbol *Sym;
1708
1709 // for symbols, get rid of prepended ".gnu.linkonce.lx."
1710
1711 // emit symbol if needed
1712 if (Absolute) {
1713 Sym = getContext().getOrCreateSymbol(StringRef(myCharStr.c_str() + 16));
1714 if (Sym->isUndefined()) {
1715 getStreamer().EmitLabel(Sym);
1716 getStreamer().EmitSymbolAttribute(Sym, MCSA_Global);
1717 getStreamer().EmitIntValue(Value, byteSize);
1718 }
1719 } else if (MO_1.isExpr()) {
1720 const char *StringStart = 0;
1721 const char *StringEnd = 0;
1722 if (*Operands[4]->getStartLoc().getPointer() == '#') {
1723 StringStart = Operands[5]->getStartLoc().getPointer();
1724 StringEnd = Operands[6]->getStartLoc().getPointer();
1725 } else { // no pound
1726 StringStart = Operands[4]->getStartLoc().getPointer();
1727 StringEnd = Operands[5]->getStartLoc().getPointer();
1728 }
1729
1730 unsigned size = StringEnd - StringStart;
1731 std::string DotConst = ".CONST_";
1732 Sym = getContext().getOrCreateSymbol(DotConst +
1733 StringRef(StringStart, size));
1734
1735 if (Sym->isUndefined()) {
1736 // case where symbol is not yet defined: emit symbol
1737 getStreamer().EmitLabel(Sym);
1738 getStreamer().EmitSymbolAttribute(Sym, MCSA_Local);
1739 getStreamer().EmitValue(MO_1.getExpr(), 4);
1740 }
1741 } else
1742 llvm_unreachable("unexpected type of machine operand!");
1743
1744 MES->PopSection();
1745
1746 if (Sym) {
1747 MCInst TmpInst;
1748 if (is32bit) // 32 bit
1749 TmpInst.setOpcode(Hexagon::L2_loadrigp);
1750 else // 64 bit
1751 TmpInst.setOpcode(Hexagon::L2_loadrdgp);
1752
1753 TmpInst.addOperand(MO_0);
1754 TmpInst.addOperand(
1755 MCOperand::createExpr(MCSymbolRefExpr::create(Sym, getContext())));
1756 Inst = TmpInst;
1757 }
1758 }
1759 break;
1760
1761 // Translate a "$Rdd = #-imm" to "$Rdd = combine(#[-1,0], #-imm)"
1762 case Hexagon::A2_tfrpi: {
1763 MCOperand &Rdd = Inst.getOperand(0);
1764 MCOperand &MO = Inst.getOperand(1);
1765 int64_t Value;
1766 int sVal = (MO.getExpr()->evaluateAsAbsolute(Value) && Value < 0) ? -1 : 0;
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001767 MCOperand imm(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001768 HexagonMCExpr::create(MCConstantExpr::create(sVal, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001769 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, imm, MO);
1770 break;
1771 }
1772
1773 // Translate a "$Rdd = [#]#imm" to "$Rdd = combine(#, [#]#imm)"
1774 case Hexagon::TFRI64_V4: {
1775 MCOperand &Rdd = Inst.getOperand(0);
1776 MCOperand &MO = Inst.getOperand(1);
1777 int64_t Value;
1778 if (MO.getExpr()->evaluateAsAbsolute(Value)) {
David Majnemere61e4bf2016-06-21 05:10:24 +00001779 int s8 = Hi_32(Value);
1780 if (!isInt<8>(s8))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001781 OutOfRange(IDLoc, s8, -128);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001782 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001783 MCConstantExpr::create(s8, Context), Context))); // upper 32
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001784 auto Expr = HexagonMCExpr::create(
David Majnemere61e4bf2016-06-21 05:10:24 +00001785 MCConstantExpr::create(Lo_32(Value), Context), Context);
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001786 HexagonMCInstrInfo::setMustExtend(*Expr, HexagonMCInstrInfo::mustExtend(*MO.getExpr()));
1787 MCOperand imm2(MCOperand::createExpr(Expr)); // lower 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001788 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, imm2);
1789 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001790 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001791 MCConstantExpr::create(0, Context), Context))); // upper 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001792 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, MO);
1793 }
1794 break;
1795 }
1796
1797 // Handle $Rdd = combine(##imm, #imm)"
1798 case Hexagon::TFRI64_V2_ext: {
1799 MCOperand &Rdd = Inst.getOperand(0);
1800 MCOperand &MO1 = Inst.getOperand(1);
1801 MCOperand &MO2 = Inst.getOperand(2);
1802 int64_t Value;
1803 if (MO2.getExpr()->evaluateAsAbsolute(Value)) {
1804 int s8 = Value;
1805 if (s8 < -128 || s8 > 127)
1806 OutOfRange(IDLoc, s8, -128);
1807 }
1808 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, MO1, MO2);
1809 break;
1810 }
1811
1812 // Handle $Rdd = combine(#imm, ##imm)"
1813 case Hexagon::A4_combineii: {
1814 MCOperand &Rdd = Inst.getOperand(0);
1815 MCOperand &MO1 = Inst.getOperand(1);
1816 int64_t Value;
1817 if (MO1.getExpr()->evaluateAsAbsolute(Value)) {
1818 int s8 = Value;
1819 if (s8 < -128 || s8 > 127)
1820 OutOfRange(IDLoc, s8, -128);
1821 }
1822 MCOperand &MO2 = Inst.getOperand(2);
1823 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, MO1, MO2);
1824 break;
1825 }
1826
1827 case Hexagon::S2_tableidxb_goodsyntax: {
1828 Inst.setOpcode(Hexagon::S2_tableidxb);
1829 break;
1830 }
1831
1832 case Hexagon::S2_tableidxh_goodsyntax: {
1833 MCInst TmpInst;
1834 MCOperand &Rx = Inst.getOperand(0);
1835 MCOperand &_dst_ = Inst.getOperand(1);
1836 MCOperand &Rs = Inst.getOperand(2);
1837 MCOperand &Imm4 = Inst.getOperand(3);
1838 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001839 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001840 Imm6.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001841 TmpInst.setOpcode(Hexagon::S2_tableidxh);
1842 TmpInst.addOperand(Rx);
1843 TmpInst.addOperand(_dst_);
1844 TmpInst.addOperand(Rs);
1845 TmpInst.addOperand(Imm4);
1846 TmpInst.addOperand(Imm6);
1847 Inst = TmpInst;
1848 break;
1849 }
1850
1851 case Hexagon::S2_tableidxw_goodsyntax: {
1852 MCInst TmpInst;
1853 MCOperand &Rx = Inst.getOperand(0);
1854 MCOperand &_dst_ = Inst.getOperand(1);
1855 MCOperand &Rs = Inst.getOperand(2);
1856 MCOperand &Imm4 = Inst.getOperand(3);
1857 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001858 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001859 Imm6.getExpr(), MCConstantExpr::create(2, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001860 TmpInst.setOpcode(Hexagon::S2_tableidxw);
1861 TmpInst.addOperand(Rx);
1862 TmpInst.addOperand(_dst_);
1863 TmpInst.addOperand(Rs);
1864 TmpInst.addOperand(Imm4);
1865 TmpInst.addOperand(Imm6);
1866 Inst = TmpInst;
1867 break;
1868 }
1869
1870 case Hexagon::S2_tableidxd_goodsyntax: {
1871 MCInst TmpInst;
1872 MCOperand &Rx = Inst.getOperand(0);
1873 MCOperand &_dst_ = Inst.getOperand(1);
1874 MCOperand &Rs = Inst.getOperand(2);
1875 MCOperand &Imm4 = Inst.getOperand(3);
1876 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001877 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001878 Imm6.getExpr(), MCConstantExpr::create(3, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001879 TmpInst.setOpcode(Hexagon::S2_tableidxd);
1880 TmpInst.addOperand(Rx);
1881 TmpInst.addOperand(_dst_);
1882 TmpInst.addOperand(Rs);
1883 TmpInst.addOperand(Imm4);
1884 TmpInst.addOperand(Imm6);
1885 Inst = TmpInst;
1886 break;
1887 }
1888
1889 case Hexagon::M2_mpyui: {
1890 Inst.setOpcode(Hexagon::M2_mpyi);
1891 break;
1892 }
1893 case Hexagon::M2_mpysmi: {
1894 MCInst TmpInst;
1895 MCOperand &Rd = Inst.getOperand(0);
1896 MCOperand &Rs = Inst.getOperand(1);
1897 MCOperand &Imm = Inst.getOperand(2);
1898 int64_t Value;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001899 MCExpr const &Expr = *Imm.getExpr();
1900 bool Absolute = Expr.evaluateAsAbsolute(Value);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001901 assert(Absolute);
1902 (void)Absolute;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001903 if (!HexagonMCInstrInfo::mustExtend(Expr)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001904 if (Value < 0 && Value > -256) {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001905 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001906 MCConstantExpr::create(Value * -1, Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001907 TmpInst.setOpcode(Hexagon::M2_mpysin);
1908 } else if (Value < 256 && Value >= 0)
1909 TmpInst.setOpcode(Hexagon::M2_mpysip);
1910 else
1911 return Match_InvalidOperand;
1912 } else {
1913 if (Value >= 0)
1914 TmpInst.setOpcode(Hexagon::M2_mpysip);
1915 else
1916 return Match_InvalidOperand;
1917 }
1918 TmpInst.addOperand(Rd);
1919 TmpInst.addOperand(Rs);
1920 TmpInst.addOperand(Imm);
1921 Inst = TmpInst;
1922 break;
1923 }
1924
1925 case Hexagon::S2_asr_i_r_rnd_goodsyntax: {
1926 MCOperand &Imm = Inst.getOperand(2);
1927 MCInst TmpInst;
1928 int64_t Value;
1929 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1930 assert(Absolute);
1931 (void)Absolute;
1932 if (Value == 0) { // convert to $Rd = $Rs
1933 TmpInst.setOpcode(Hexagon::A2_tfr);
1934 MCOperand &Rd = Inst.getOperand(0);
1935 MCOperand &Rs = Inst.getOperand(1);
1936 TmpInst.addOperand(Rd);
1937 TmpInst.addOperand(Rs);
1938 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001939 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001940 MCBinaryExpr::createSub(Imm.getExpr(),
1941 MCConstantExpr::create(1, Context), Context),
1942 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001943 TmpInst.setOpcode(Hexagon::S2_asr_i_r_rnd);
1944 MCOperand &Rd = Inst.getOperand(0);
1945 MCOperand &Rs = Inst.getOperand(1);
1946 TmpInst.addOperand(Rd);
1947 TmpInst.addOperand(Rs);
1948 TmpInst.addOperand(Imm);
1949 }
1950 Inst = TmpInst;
1951 break;
1952 }
1953
1954 case Hexagon::S2_asr_i_p_rnd_goodsyntax: {
1955 MCOperand &Rdd = Inst.getOperand(0);
1956 MCOperand &Rss = Inst.getOperand(1);
1957 MCOperand &Imm = Inst.getOperand(2);
1958 int64_t Value;
1959 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1960 assert(Absolute);
1961 (void)Absolute;
1962 if (Value == 0) { // convert to $Rdd = combine ($Rs[0], $Rs[1])
1963 MCInst TmpInst;
1964 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001965 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001966 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001967 Rss.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001968 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001969 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001970 StringRef Reg2(R2);
1971 TmpInst.setOpcode(Hexagon::A2_combinew);
1972 TmpInst.addOperand(Rdd);
1973 TmpInst.addOperand(Rss);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001974 TmpInst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001975 Inst = TmpInst;
1976 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001977 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001978 MCBinaryExpr::createSub(Imm.getExpr(),
1979 MCConstantExpr::create(1, Context), Context),
1980 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001981 Inst.setOpcode(Hexagon::S2_asr_i_p_rnd);
1982 }
1983 break;
1984 }
1985
1986 case Hexagon::A4_boundscheck: {
1987 MCOperand &Rs = Inst.getOperand(1);
1988 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
1989 if (RegNum & 1) { // Odd mapped to raw:hi, regpair is rodd:odd-1, like r3:2
1990 Inst.setOpcode(Hexagon::A4_boundscheck_hi);
1991 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00001992 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001993 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001994 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001995 } else { // raw:lo
1996 Inst.setOpcode(Hexagon::A4_boundscheck_lo);
1997 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00001998 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001999 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002000 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002001 }
2002 break;
2003 }
2004
2005 case Hexagon::A2_addsp: {
2006 MCOperand &Rs = Inst.getOperand(1);
2007 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
2008 if (RegNum & 1) { // Odd mapped to raw:hi
2009 Inst.setOpcode(Hexagon::A2_addsph);
2010 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002011 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002012 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002013 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002014 } else { // Even mapped raw:lo
2015 Inst.setOpcode(Hexagon::A2_addspl);
2016 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002017 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002018 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002019 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002020 }
2021 break;
2022 }
2023
2024 case Hexagon::M2_vrcmpys_s1: {
2025 MCOperand &Rt = Inst.getOperand(2);
2026 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2027 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2028 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_h);
2029 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002030 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002031 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002032 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002033 } else { // Even mapped sat:raw:lo
2034 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_l);
2035 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002036 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002037 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002038 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002039 }
2040 break;
2041 }
2042
2043 case Hexagon::M2_vrcmpys_acc_s1: {
2044 MCInst TmpInst;
2045 MCOperand &Rxx = Inst.getOperand(0);
2046 MCOperand &Rss = Inst.getOperand(2);
2047 MCOperand &Rt = Inst.getOperand(3);
2048 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2049 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2050 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_h);
2051 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002052 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002053 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002054 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002055 } else { // Even mapped sat:raw:lo
2056 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_l);
2057 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002058 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002059 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002060 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002061 }
2062 // Registers are in different positions
2063 TmpInst.addOperand(Rxx);
2064 TmpInst.addOperand(Rxx);
2065 TmpInst.addOperand(Rss);
2066 TmpInst.addOperand(Rt);
2067 Inst = TmpInst;
2068 break;
2069 }
2070
2071 case Hexagon::M2_vrcmpys_s1rp: {
2072 MCOperand &Rt = Inst.getOperand(2);
2073 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2074 if (RegNum & 1) { // Odd mapped to rnd:sat:raw:hi
2075 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_h);
2076 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002077 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002078 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002079 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002080 } else { // Even mapped rnd:sat:raw:lo
2081 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_l);
2082 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002083 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002084 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002085 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002086 }
2087 break;
2088 }
2089
2090 case Hexagon::S5_asrhub_rnd_sat_goodsyntax: {
2091 MCOperand &Imm = Inst.getOperand(2);
2092 int64_t Value;
2093 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
2094 assert(Absolute);
2095 (void)Absolute;
2096 if (Value == 0)
2097 Inst.setOpcode(Hexagon::S2_vsathub);
2098 else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002099 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002100 MCBinaryExpr::createSub(Imm.getExpr(),
2101 MCConstantExpr::create(1, Context), Context),
2102 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002103 Inst.setOpcode(Hexagon::S5_asrhub_rnd_sat);
2104 }
2105 break;
2106 }
2107
2108 case Hexagon::S5_vasrhrnd_goodsyntax: {
2109 MCOperand &Rdd = Inst.getOperand(0);
2110 MCOperand &Rss = Inst.getOperand(1);
2111 MCOperand &Imm = Inst.getOperand(2);
2112 int64_t Value;
2113 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
2114 assert(Absolute);
2115 (void)Absolute;
2116 if (Value == 0) {
2117 MCInst TmpInst;
2118 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00002119 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002120 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002121 Rss.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002122 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00002123 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002124 StringRef Reg2(R2);
2125 TmpInst.setOpcode(Hexagon::A2_combinew);
2126 TmpInst.addOperand(Rdd);
2127 TmpInst.addOperand(Rss);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002128 TmpInst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002129 Inst = TmpInst;
2130 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002131 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002132 MCBinaryExpr::createSub(Imm.getExpr(),
2133 MCConstantExpr::create(1, Context), Context),
2134 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002135 Inst.setOpcode(Hexagon::S5_vasrhrnd);
2136 }
2137 break;
2138 }
2139
2140 case Hexagon::A2_not: {
2141 MCInst TmpInst;
2142 MCOperand &Rd = Inst.getOperand(0);
2143 MCOperand &Rs = Inst.getOperand(1);
2144 TmpInst.setOpcode(Hexagon::A2_subri);
2145 TmpInst.addOperand(Rd);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002146 TmpInst.addOperand(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002147 HexagonMCExpr::create(MCConstantExpr::create(-1, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002148 TmpInst.addOperand(Rs);
2149 Inst = TmpInst;
2150 break;
2151 }
2152 } // switch
2153
2154 return Match_Success;
2155}
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002156
2157
2158unsigned HexagonAsmParser::matchRegister(StringRef Name) {
2159 if (unsigned Reg = MatchRegisterName(Name))
2160 return Reg;
2161 return MatchRegisterAltName(Name);
2162}