blob: 36b6e72992333af7de402b544d46a3c1b56f5250 [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 LeMahieu7cd08922015-11-09 04:07:48 +0000317
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000318 bool iss16_0Ext() const { return CheckImmRange(16 + 26, 0, true, true, true); }
319 bool iss12_0Ext() const { return CheckImmRange(12 + 26, 0, true, true, true); }
320 bool iss10_0Ext() const { return CheckImmRange(10 + 26, 0, true, true, true); }
321 bool iss9_0Ext() const { return CheckImmRange(9 + 26, 0, true, true, true); }
322 bool iss8_0Ext() const { return CheckImmRange(8 + 26, 0, true, true, true); }
323 bool iss7_0Ext() const { return CheckImmRange(7 + 26, 0, true, true, true); }
324 bool iss6_0Ext() const { return CheckImmRange(6 + 26, 0, true, true, true); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000325 bool iss11_0Ext() const {
326 return CheckImmRange(11 + 26, 0, true, true, true);
327 }
328 bool iss11_1Ext() const {
329 return CheckImmRange(11 + 26, 1, true, true, true);
330 }
331 bool iss11_2Ext() const {
332 return CheckImmRange(11 + 26, 2, true, true, true);
333 }
334 bool iss11_3Ext() const {
335 return CheckImmRange(11 + 26, 3, true, true, true);
336 }
337
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000338 bool isu7_0Ext() const { return CheckImmRange(7 + 26, 0, false, true, true); }
339 bool isu8_0Ext() const { return CheckImmRange(8 + 26, 0, false, true, true); }
340 bool isu9_0Ext() const { return CheckImmRange(9 + 26, 0, false, true, true); }
341 bool isu10_0Ext() const { return CheckImmRange(10 + 26, 0, false, true, true); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000342 bool isu6_0Ext() const { return CheckImmRange(6 + 26, 0, false, true, true); }
343 bool isu6_1Ext() const { return CheckImmRange(6 + 26, 1, false, true, true); }
344 bool isu6_2Ext() const { return CheckImmRange(6 + 26, 2, false, true, true); }
345 bool isu6_3Ext() const { return CheckImmRange(6 + 26, 3, false, true, true); }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000346 bool isu32_0MustExt() const { return isImm(); }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000347
348 void addRegOperands(MCInst &Inst, unsigned N) const {
349 assert(N == 1 && "Invalid number of operands!");
350 Inst.addOperand(MCOperand::createReg(getReg()));
351 }
352
353 void addImmOperands(MCInst &Inst, unsigned N) const {
354 assert(N == 1 && "Invalid number of operands!");
355 Inst.addOperand(MCOperand::createExpr(getImm()));
356 }
357
358 void addSignedImmOperands(MCInst &Inst, unsigned N) const {
359 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000360 HexagonMCExpr *Expr =
361 const_cast<HexagonMCExpr *>(cast<HexagonMCExpr>(getImm()));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000362 int64_t Value;
363 if (!Expr->evaluateAsAbsolute(Value)) {
364 Inst.addOperand(MCOperand::createExpr(Expr));
365 return;
366 }
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000367 int64_t Extended = SignExtend64(Value, 32);
368 if ((Extended < 0) != (Value < 0))
369 Expr->setSignMismatch();
370 Inst.addOperand(MCOperand::createExpr(Expr));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000371 }
372
373 void addf32ExtOperands(MCInst &Inst, unsigned N) const {
374 addImmOperands(Inst, N);
375 }
376
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000377 void adds32_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000378 addSignedImmOperands(Inst, N);
379 }
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000380 void adds23_2ImmOperands(MCInst &Inst, unsigned N) const {
381 addSignedImmOperands(Inst, N);
382 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000383 void adds8_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000384 addSignedImmOperands(Inst, N);
385 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000386 void adds8_0Imm64Operands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000387 addSignedImmOperands(Inst, N);
388 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000389 void adds6_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000390 addSignedImmOperands(Inst, N);
391 }
392 void adds4_0ImmOperands(MCInst &Inst, unsigned N) const {
393 addSignedImmOperands(Inst, N);
394 }
395 void adds4_1ImmOperands(MCInst &Inst, unsigned N) const {
396 addSignedImmOperands(Inst, N);
397 }
398 void adds4_2ImmOperands(MCInst &Inst, unsigned N) const {
399 addSignedImmOperands(Inst, N);
400 }
401 void adds4_3ImmOperands(MCInst &Inst, unsigned N) const {
402 addSignedImmOperands(Inst, N);
403 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000404 void adds3_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000405 addSignedImmOperands(Inst, N);
406 }
407
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000408 void addu64_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000409 addImmOperands(Inst, N);
410 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000411 void addu32_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000412 addImmOperands(Inst, N);
413 }
414 void addu26_6ImmOperands(MCInst &Inst, unsigned N) const {
415 addImmOperands(Inst, N);
416 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000417 void addu16_0ImmOperands(MCInst &Inst, unsigned N) const {
418 addImmOperands(Inst, N);
419 }
420 void addu16_1ImmOperands(MCInst &Inst, unsigned N) const {
421 addImmOperands(Inst, N);
422 }
423 void addu16_2ImmOperands(MCInst &Inst, unsigned N) const {
424 addImmOperands(Inst, N);
425 }
426 void addu16_3ImmOperands(MCInst &Inst, unsigned N) const {
427 addImmOperands(Inst, N);
428 }
429 void addu11_3ImmOperands(MCInst &Inst, unsigned N) const {
430 addImmOperands(Inst, N);
431 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000432 void addu10_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000433 addImmOperands(Inst, N);
434 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000435 void addu9_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000436 addImmOperands(Inst, N);
437 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000438 void addu8_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000439 addImmOperands(Inst, N);
440 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000441 void addu7_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000442 addImmOperands(Inst, N);
443 }
444 void addu6_0ImmOperands(MCInst &Inst, unsigned N) const {
445 addImmOperands(Inst, N);
446 }
447 void addu6_1ImmOperands(MCInst &Inst, unsigned N) const {
448 addImmOperands(Inst, N);
449 }
450 void addu6_2ImmOperands(MCInst &Inst, unsigned N) const {
451 addImmOperands(Inst, N);
452 }
453 void addu6_3ImmOperands(MCInst &Inst, unsigned N) const {
454 addImmOperands(Inst, N);
455 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000456 void addu5_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000457 addImmOperands(Inst, N);
458 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000459 void addu4_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000460 addImmOperands(Inst, N);
461 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000462 void addu3_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000463 addImmOperands(Inst, N);
464 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000465 void addu2_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000466 addImmOperands(Inst, N);
467 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000468 void addu1_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000469 addImmOperands(Inst, N);
470 }
471
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000472 void addm6_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000473 addImmOperands(Inst, N);
474 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000475 void addn8_0ImmOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000476 addImmOperands(Inst, N);
477 }
478
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000479 void adds16_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000480 addSignedImmOperands(Inst, N);
481 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000482 void adds12_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000483 addSignedImmOperands(Inst, N);
484 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000485 void adds10_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000486 addSignedImmOperands(Inst, N);
487 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000488 void adds9_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000489 addSignedImmOperands(Inst, N);
490 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000491 void adds8_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000492 addSignedImmOperands(Inst, N);
493 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000494 void adds6_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000495 addSignedImmOperands(Inst, N);
496 }
497 void adds11_0ExtOperands(MCInst &Inst, unsigned N) const {
498 addSignedImmOperands(Inst, N);
499 }
500 void adds11_1ExtOperands(MCInst &Inst, unsigned N) const {
501 addSignedImmOperands(Inst, N);
502 }
503 void adds11_2ExtOperands(MCInst &Inst, unsigned N) const {
504 addSignedImmOperands(Inst, N);
505 }
506 void adds11_3ExtOperands(MCInst &Inst, unsigned N) const {
507 addSignedImmOperands(Inst, N);
508 }
509
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000510 void addu7_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000511 addImmOperands(Inst, N);
512 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000513 void addu8_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000514 addImmOperands(Inst, N);
515 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000516 void addu9_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000517 addImmOperands(Inst, N);
518 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000519 void addu10_0ExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000520 addImmOperands(Inst, N);
521 }
522 void addu6_0ExtOperands(MCInst &Inst, unsigned N) const {
523 addImmOperands(Inst, N);
524 }
525 void addu6_1ExtOperands(MCInst &Inst, unsigned N) const {
526 addImmOperands(Inst, N);
527 }
528 void addu6_2ExtOperands(MCInst &Inst, unsigned N) const {
529 addImmOperands(Inst, N);
530 }
531 void addu6_3ExtOperands(MCInst &Inst, unsigned N) const {
532 addImmOperands(Inst, N);
533 }
Krzysztof Parzyszek654dc112016-11-01 19:02:10 +0000534 void addu32_0MustExtOperands(MCInst &Inst, unsigned N) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000535 addImmOperands(Inst, N);
536 }
537
538 void adds4_6ImmOperands(MCInst &Inst, unsigned N) const {
539 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000540 const MCConstantExpr *CE =
541 dyn_cast<MCConstantExpr>(&HexagonMCInstrInfo::getExpr(*getImm()));
Colin LeMahieu4c606e62015-12-04 15:48:45 +0000542 Inst.addOperand(MCOperand::createImm(CE->getValue() * 64));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000543 }
544
545 void adds3_6ImmOperands(MCInst &Inst, unsigned N) const {
546 assert(N == 1 && "Invalid number of operands!");
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000547 const MCConstantExpr *CE =
548 dyn_cast<MCConstantExpr>(&HexagonMCInstrInfo::getExpr(*getImm()));
Colin LeMahieu4c606e62015-12-04 15:48:45 +0000549 Inst.addOperand(MCOperand::createImm(CE->getValue() * 64));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000550 }
551
552 StringRef getToken() const {
553 assert(Kind == Token && "Invalid access!");
554 return StringRef(Tok.Data, Tok.Length);
555 }
556
557 virtual void print(raw_ostream &OS) const;
558
559 static std::unique_ptr<HexagonOperand> CreateToken(StringRef Str, SMLoc S) {
560 HexagonOperand *Op = new HexagonOperand(Token);
561 Op->Tok.Data = Str.data();
562 Op->Tok.Length = Str.size();
563 Op->StartLoc = S;
564 Op->EndLoc = S;
565 return std::unique_ptr<HexagonOperand>(Op);
566 }
567
568 static std::unique_ptr<HexagonOperand> CreateReg(unsigned RegNum, SMLoc S,
569 SMLoc E) {
570 HexagonOperand *Op = new HexagonOperand(Register);
571 Op->Reg.RegNum = RegNum;
572 Op->StartLoc = S;
573 Op->EndLoc = E;
574 return std::unique_ptr<HexagonOperand>(Op);
575 }
576
577 static std::unique_ptr<HexagonOperand> CreateImm(const MCExpr *Val, SMLoc S,
578 SMLoc E) {
579 HexagonOperand *Op = new HexagonOperand(Immediate);
580 Op->Imm.Val = Val;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000581 Op->StartLoc = S;
582 Op->EndLoc = E;
583 return std::unique_ptr<HexagonOperand>(Op);
584 }
585};
586
587} // end anonymous namespace.
588
589void HexagonOperand::print(raw_ostream &OS) const {
590 switch (Kind) {
591 case Immediate:
592 getImm()->print(OS, nullptr);
593 break;
594 case Register:
595 OS << "<register R";
596 OS << getReg() << ">";
597 break;
598 case Token:
599 OS << "'" << getToken() << "'";
600 break;
601 }
602}
603
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000604bool HexagonAsmParser::finishBundle(SMLoc IDLoc, MCStreamer &Out) {
605 DEBUG(dbgs() << "Bundle:");
606 DEBUG(MCB.dump_pretty(dbgs()));
607 DEBUG(dbgs() << "--\n");
608
609 // Check the bundle for errors.
610 const MCRegisterInfo *RI = getContext().getRegisterInfo();
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000611 HexagonMCChecker Check(MCII, getSTI(), MCB, MCB, *RI);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000612
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000613 bool CheckOk = HexagonMCInstrInfo::canonicalizePacket(MCII, getSTI(),
614 getContext(), MCB,
615 &Check);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000616
617 while (Check.getNextErrInfo() == true) {
618 unsigned Reg = Check.getErrRegister();
619 Twine R(RI->getName(Reg));
620
621 uint64_t Err = Check.getError();
622 if (Err != HexagonMCErrInfo::CHECK_SUCCESS) {
623 if (HexagonMCErrInfo::CHECK_ERROR_BRANCHES & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000624 return Error(
625 IDLoc,
626 "unconditional branch cannot precede another branch in packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000627
628 if (HexagonMCErrInfo::CHECK_ERROR_NEWP & Err ||
629 HexagonMCErrInfo::CHECK_ERROR_NEWV & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000630 return Error(IDLoc, "register `" + R +
631 "' used with `.new' "
632 "but not validly modified in the same packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000633
634 if (HexagonMCErrInfo::CHECK_ERROR_REGISTERS & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000635 return Error(IDLoc, "register `" + R + "' modified more than once");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000636
637 if (HexagonMCErrInfo::CHECK_ERROR_READONLY & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000638 return Error(IDLoc, "cannot write to read-only register `" + R + "'");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000639
640 if (HexagonMCErrInfo::CHECK_ERROR_LOOP & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000641 return Error(IDLoc, "loop-setup and some branch instructions "
642 "cannot be in the same packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000643
644 if (HexagonMCErrInfo::CHECK_ERROR_ENDLOOP & Err) {
645 Twine N(HexagonMCInstrInfo::isInnerLoop(MCB) ? '0' : '1');
Nirav Dave2364748a2016-09-16 18:30:20 +0000646 return Error(IDLoc,
647 "packet marked with `:endloop" + N + "' " +
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000648 "cannot contain instructions that modify register " +
649 "`" + R + "'");
650 }
651
652 if (HexagonMCErrInfo::CHECK_ERROR_SOLO & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000653 return Error(
654 IDLoc,
655 "instruction cannot appear in packet with other instructions");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000656
657 if (HexagonMCErrInfo::CHECK_ERROR_NOSLOTS & Err)
Nirav Dave2364748a2016-09-16 18:30:20 +0000658 return Error(IDLoc, "too many slots used in packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000659
660 if (Err & HexagonMCErrInfo::CHECK_ERROR_SHUFFLE) {
661 uint64_t Erm = Check.getShuffleError();
662
663 if (HexagonShuffler::SHUFFLE_ERROR_INVALID == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000664 return Error(IDLoc, "invalid instruction packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000665 else if (HexagonShuffler::SHUFFLE_ERROR_STORES == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000666 return Error(IDLoc, "invalid instruction packet: too many stores");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000667 else if (HexagonShuffler::SHUFFLE_ERROR_LOADS == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000668 return Error(IDLoc, "invalid instruction packet: too many loads");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000669 else if (HexagonShuffler::SHUFFLE_ERROR_BRANCHES == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000670 return Error(IDLoc, "too many branches in packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000671 else if (HexagonShuffler::SHUFFLE_ERROR_NOSLOTS == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000672 return Error(IDLoc, "invalid instruction packet: out of slots");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000673 else if (HexagonShuffler::SHUFFLE_ERROR_SLOTS == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000674 return Error(IDLoc, "invalid instruction packet: slot error");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000675 else if (HexagonShuffler::SHUFFLE_ERROR_ERRATA2 == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000676 return Error(IDLoc, "v60 packet violation");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000677 else if (HexagonShuffler::SHUFFLE_ERROR_STORE_LOAD_CONFLICT == Erm)
Nirav Dave2364748a2016-09-16 18:30:20 +0000678 return Error(IDLoc, "slot 0 instruction does not allow slot 1 store");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000679 else
Nirav Dave2364748a2016-09-16 18:30:20 +0000680 return Error(IDLoc, "unknown error in instruction packet");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000681 }
682 }
683
684 unsigned Warn = Check.getWarning();
685 if (Warn != HexagonMCErrInfo::CHECK_SUCCESS) {
686 if (HexagonMCErrInfo::CHECK_WARN_CURRENT & Warn)
687 Warning(IDLoc, "register `" + R + "' used with `.cur' "
688 "but not used in the same packet");
689 else if (HexagonMCErrInfo::CHECK_WARN_TEMPORARY & Warn)
690 Warning(IDLoc, "register `" + R + "' used with `.tmp' "
691 "but not used in the same packet");
692 }
693 }
694
695 if (CheckOk) {
696 MCB.setLoc(IDLoc);
697 if (HexagonMCInstrInfo::bundleSize(MCB) == 0) {
698 assert(!HexagonMCInstrInfo::isInnerLoop(MCB));
699 assert(!HexagonMCInstrInfo::isOuterLoop(MCB));
700 // Empty packets are valid yet aren't emitted
701 return false;
702 }
Akira Hatanakabd9fc282015-11-14 05:20:05 +0000703 Out.EmitInstruction(MCB, getSTI());
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000704 } else {
705 // If compounding and duplexing didn't reduce the size below
706 // 4 or less we have a packet that is too big.
707 if (HexagonMCInstrInfo::bundleSize(MCB) > HEXAGON_PACKET_SIZE) {
708 Error(IDLoc, "invalid instruction packet: out of slots");
709 return true; // Error
710 }
711 }
712
713 return false; // No error
714}
715
716bool HexagonAsmParser::matchBundleOptions() {
717 MCAsmParser &Parser = getParser();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000718 while (true) {
719 if (!Parser.getTok().is(AsmToken::Colon))
720 return false;
Nirav Davefd910412016-06-17 16:06:17 +0000721 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000722 StringRef Option = Parser.getTok().getString();
723 if (Option.compare_lower("endloop0") == 0)
724 HexagonMCInstrInfo::setInnerLoop(MCB);
725 else if (Option.compare_lower("endloop1") == 0)
726 HexagonMCInstrInfo::setOuterLoop(MCB);
727 else if (Option.compare_lower("mem_noshuf") == 0)
728 HexagonMCInstrInfo::setMemReorderDisabled(MCB);
729 else if (Option.compare_lower("mem_shuf") == 0)
730 HexagonMCInstrInfo::setMemStoreReorderEnabled(MCB);
731 else
732 return true;
Nirav Davefd910412016-06-17 16:06:17 +0000733 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000734 }
735}
736
737// For instruction aliases, immediates are generated rather than
738// MCConstantExpr. Convert them for uniform MCExpr.
739// Also check for signed/unsigned mismatches and warn
740void HexagonAsmParser::canonicalizeImmediates(MCInst &MCI) {
741 MCInst NewInst;
742 NewInst.setOpcode(MCI.getOpcode());
743 for (MCOperand &I : MCI)
744 if (I.isImm()) {
745 int64_t Value (I.getImm());
Colin LeMahieuc7b21242016-02-15 18:47:55 +0000746 NewInst.addOperand(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000747 MCConstantExpr::create(Value, getContext()), getContext())));
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000748 }
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000749 else {
750 if (I.isExpr() && cast<HexagonMCExpr>(I.getExpr())->signMismatch() &&
751 WarnSignedMismatch)
752 Warning (MCI.getLoc(), "Signed/Unsigned mismatch");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000753 NewInst.addOperand(I);
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000754 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000755 MCI = NewInst;
756}
757
758bool HexagonAsmParser::matchOneInstruction(MCInst &MCI, SMLoc IDLoc,
759 OperandVector &InstOperands,
760 uint64_t &ErrorInfo,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000761 bool MatchingInlineAsm) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000762 // Perform matching with tablegen asmmatcher generated function
763 int result =
764 MatchInstructionImpl(InstOperands, MCI, ErrorInfo, MatchingInlineAsm);
765 if (result == Match_Success) {
766 MCI.setLoc(IDLoc);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000767 canonicalizeImmediates(MCI);
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000768 result = processInstruction(MCI, InstOperands, IDLoc);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000769
770 DEBUG(dbgs() << "Insn:");
771 DEBUG(MCI.dump_pretty(dbgs()));
772 DEBUG(dbgs() << "\n\n");
773
774 MCI.setLoc(IDLoc);
775 }
776
777 // Create instruction operand for bundle instruction
778 // Break this into a separate function Code here is less readable
779 // Think about how to get an instruction error to report correctly.
780 // SMLoc will return the "{"
781 switch (result) {
782 default:
783 break;
784 case Match_Success:
785 return false;
786 case Match_MissingFeature:
787 return Error(IDLoc, "invalid instruction");
788 case Match_MnemonicFail:
789 return Error(IDLoc, "unrecognized instruction");
790 case Match_InvalidOperand:
791 SMLoc ErrorLoc = IDLoc;
792 if (ErrorInfo != ~0U) {
793 if (ErrorInfo >= InstOperands.size())
794 return Error(IDLoc, "too few operands for instruction");
795
796 ErrorLoc = (static_cast<HexagonOperand *>(InstOperands[ErrorInfo].get()))
797 ->getStartLoc();
798 if (ErrorLoc == SMLoc())
799 ErrorLoc = IDLoc;
800 }
801 return Error(ErrorLoc, "invalid operand for instruction");
802 }
803 llvm_unreachable("Implement any new match types added!");
804}
805
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000806bool HexagonAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
807 OperandVector &Operands,
808 MCStreamer &Out,
809 uint64_t &ErrorInfo,
810 bool MatchingInlineAsm) {
811 if (!InBrackets) {
812 MCB.clear();
813 MCB.addOperand(MCOperand::createImm(0));
814 }
815 HexagonOperand &FirstOperand = static_cast<HexagonOperand &>(*Operands[0]);
816 if (FirstOperand.isToken() && FirstOperand.getToken() == "{") {
817 assert(Operands.size() == 1 && "Brackets should be by themselves");
818 if (InBrackets) {
819 getParser().Error(IDLoc, "Already in a packet");
820 return true;
821 }
822 InBrackets = true;
823 return false;
824 }
825 if (FirstOperand.isToken() && FirstOperand.getToken() == "}") {
826 assert(Operands.size() == 1 && "Brackets should be by themselves");
827 if (!InBrackets) {
828 getParser().Error(IDLoc, "Not in a packet");
829 return true;
830 }
831 InBrackets = false;
832 if (matchBundleOptions())
833 return true;
834 return finishBundle(IDLoc, Out);
835 }
836 MCInst *SubInst = new (getParser().getContext()) MCInst;
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000837 if (matchOneInstruction(*SubInst, IDLoc, Operands, ErrorInfo,
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000838 MatchingInlineAsm))
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000839 return true;
840 HexagonMCInstrInfo::extendIfNeeded(
Colin LeMahieu73cd6862016-02-29 18:39:51 +0000841 getParser().getContext(), MCII, MCB, *SubInst);
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000842 MCB.addOperand(MCOperand::createInst(SubInst));
843 if (!InBrackets)
844 return finishBundle(IDLoc, Out);
845 return false;
846}
847
848/// ParseDirective parses the Hexagon specific directives
849bool HexagonAsmParser::ParseDirective(AsmToken DirectiveID) {
850 StringRef IDVal = DirectiveID.getIdentifier();
851 if ((IDVal.lower() == ".word") || (IDVal.lower() == ".4byte"))
852 return ParseDirectiveValue(4, DirectiveID.getLoc());
853 if (IDVal.lower() == ".short" || IDVal.lower() == ".hword" ||
854 IDVal.lower() == ".half")
855 return ParseDirectiveValue(2, DirectiveID.getLoc());
856 if (IDVal.lower() == ".falign")
857 return ParseDirectiveFalign(256, DirectiveID.getLoc());
858 if ((IDVal.lower() == ".lcomm") || (IDVal.lower() == ".lcommon"))
859 return ParseDirectiveComm(true, DirectiveID.getLoc());
860 if ((IDVal.lower() == ".comm") || (IDVal.lower() == ".common"))
861 return ParseDirectiveComm(false, DirectiveID.getLoc());
862 if (IDVal.lower() == ".subsection")
863 return ParseDirectiveSubsection(DirectiveID.getLoc());
864
865 return true;
866}
867bool HexagonAsmParser::ParseDirectiveSubsection(SMLoc L) {
868 const MCExpr *Subsection = 0;
869 int64_t Res;
870
871 assert((getLexer().isNot(AsmToken::EndOfStatement)) &&
872 "Invalid subsection directive");
873 getParser().parseExpression(Subsection);
874
875 if (!Subsection->evaluateAsAbsolute(Res))
876 return Error(L, "Cannot evaluate subsection number");
877
878 if (getLexer().isNot(AsmToken::EndOfStatement))
879 return TokError("unexpected token in directive");
880
881 // 0-8192 is the hard-coded range in MCObjectStreamper.cpp, this keeps the
882 // negative subsections together and in the same order but at the opposite
883 // end of the section. Only legacy hexagon-gcc created assembly code
884 // used negative subsections.
885 if ((Res < 0) && (Res > -8193))
Colin LeMahieuc7b21242016-02-15 18:47:55 +0000886 Subsection = HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000887 MCConstantExpr::create(8192 + Res, getContext()), getContext());
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000888
889 getStreamer().SubSection(Subsection);
890 return false;
891}
892
893/// ::= .falign [expression]
894bool HexagonAsmParser::ParseDirectiveFalign(unsigned Size, SMLoc L) {
895
896 int64_t MaxBytesToFill = 15;
897
898 // if there is an arguement
899 if (getLexer().isNot(AsmToken::EndOfStatement)) {
900 const MCExpr *Value;
901 SMLoc ExprLoc = L;
902
903 // Make sure we have a number (false is returned if expression is a number)
904 if (getParser().parseExpression(Value) == false) {
905 // Make sure this is a number that is in range
906 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
907 uint64_t IntValue = MCE->getValue();
908 if (!isUIntN(Size, IntValue) && !isIntN(Size, IntValue))
909 return Error(ExprLoc, "literal value out of range (256) for falign");
910 MaxBytesToFill = IntValue;
911 Lex();
912 } else {
913 return Error(ExprLoc, "not a valid expression for falign directive");
914 }
915 }
916
917 getTargetStreamer().emitFAlign(16, MaxBytesToFill);
918 Lex();
919
920 return false;
921}
922
923/// ::= .word [ expression (, expression)* ]
924bool HexagonAsmParser::ParseDirectiveValue(unsigned Size, SMLoc L) {
925 if (getLexer().isNot(AsmToken::EndOfStatement)) {
926
927 for (;;) {
928 const MCExpr *Value;
929 SMLoc ExprLoc = L;
930 if (getParser().parseExpression(Value))
931 return true;
932
933 // Special case constant expressions to match code generator.
934 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
935 assert(Size <= 8 && "Invalid size");
936 uint64_t IntValue = MCE->getValue();
937 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
938 return Error(ExprLoc, "literal value out of range for directive");
939 getStreamer().EmitIntValue(IntValue, Size);
940 } else
941 getStreamer().EmitValue(Value, Size);
942
943 if (getLexer().is(AsmToken::EndOfStatement))
944 break;
945
946 // FIXME: Improve diagnostic.
947 if (getLexer().isNot(AsmToken::Comma))
948 return TokError("unexpected token in directive");
949 Lex();
950 }
951 }
952
953 Lex();
954 return false;
955}
956
957// This is largely a copy of AsmParser's ParseDirectiveComm extended to
958// accept a 3rd argument, AccessAlignment which indicates the smallest
959// memory access made to the symbol, expressed in bytes. If no
960// AccessAlignment is specified it defaults to the Alignment Value.
961// Hexagon's .lcomm:
962// .lcomm Symbol, Length, Alignment, AccessAlignment
963bool HexagonAsmParser::ParseDirectiveComm(bool IsLocal, SMLoc Loc) {
964 // FIXME: need better way to detect if AsmStreamer (upstream removed
965 // getKind())
966 if (getStreamer().hasRawTextSupport())
967 return true; // Only object file output requires special treatment.
968
969 StringRef Name;
970 if (getParser().parseIdentifier(Name))
971 return TokError("expected identifier in directive");
972 // Handle the identifier as the key symbol.
973 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
974
975 if (getLexer().isNot(AsmToken::Comma))
976 return TokError("unexpected token in directive");
977 Lex();
978
979 int64_t Size;
980 SMLoc SizeLoc = getLexer().getLoc();
981 if (getParser().parseAbsoluteExpression(Size))
982 return true;
983
984 int64_t ByteAlignment = 1;
985 SMLoc ByteAlignmentLoc;
986 if (getLexer().is(AsmToken::Comma)) {
987 Lex();
988 ByteAlignmentLoc = getLexer().getLoc();
989 if (getParser().parseAbsoluteExpression(ByteAlignment))
990 return true;
991 if (!isPowerOf2_64(ByteAlignment))
992 return Error(ByteAlignmentLoc, "alignment must be a power of 2");
993 }
994
995 int64_t AccessAlignment = 0;
996 if (getLexer().is(AsmToken::Comma)) {
997 // The optional access argument specifies the size of the smallest memory
998 // access to be made to the symbol, expressed in bytes.
999 SMLoc AccessAlignmentLoc;
1000 Lex();
1001 AccessAlignmentLoc = getLexer().getLoc();
1002 if (getParser().parseAbsoluteExpression(AccessAlignment))
1003 return true;
1004
1005 if (!isPowerOf2_64(AccessAlignment))
1006 return Error(AccessAlignmentLoc, "access alignment must be a power of 2");
1007 }
1008
1009 if (getLexer().isNot(AsmToken::EndOfStatement))
1010 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1011
1012 Lex();
1013
1014 // NOTE: a size of zero for a .comm should create a undefined symbol
1015 // but a size of .lcomm creates a bss symbol of size zero.
1016 if (Size < 0)
1017 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1018 "be less than zero");
1019
1020 // NOTE: The alignment in the directive is a power of 2 value, the assembler
1021 // may internally end up wanting an alignment in bytes.
1022 // FIXME: Diagnose overflow.
1023 if (ByteAlignment < 0)
1024 return Error(ByteAlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1025 "alignment, can't be less than zero");
1026
1027 if (!Sym->isUndefined())
1028 return Error(Loc, "invalid symbol redefinition");
1029
1030 HexagonMCELFStreamer &HexagonELFStreamer =
1031 static_cast<HexagonMCELFStreamer &>(getStreamer());
1032 if (IsLocal) {
1033 HexagonELFStreamer.HexagonMCEmitLocalCommonSymbol(Sym, Size, ByteAlignment,
1034 AccessAlignment);
1035 return false;
1036 }
1037
1038 HexagonELFStreamer.HexagonMCEmitCommonSymbol(Sym, Size, ByteAlignment,
1039 AccessAlignment);
1040 return false;
1041}
1042
1043// validate register against architecture
1044bool HexagonAsmParser::RegisterMatchesArch(unsigned MatchNum) const {
1045 return true;
1046}
1047
1048// extern "C" void LLVMInitializeHexagonAsmLexer();
1049
1050/// Force static initialization.
1051extern "C" void LLVMInitializeHexagonAsmParser() {
Mehdi Aminif42454b2016-10-09 23:00:34 +00001052 RegisterMCAsmParser<HexagonAsmParser> X(getTheHexagonTarget());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001053}
1054
1055#define GET_MATCHER_IMPLEMENTATION
1056#define GET_REGISTER_MATCHER
1057#include "HexagonGenAsmMatcher.inc"
1058
1059namespace {
1060bool previousEqual(OperandVector &Operands, size_t Index, StringRef String) {
1061 if (Index >= Operands.size())
1062 return false;
1063 MCParsedAsmOperand &Operand = *Operands[Operands.size() - Index - 1];
1064 if (!Operand.isToken())
1065 return false;
1066 return static_cast<HexagonOperand &>(Operand).getToken().equals_lower(String);
1067}
1068bool previousIsLoop(OperandVector &Operands, size_t Index) {
1069 return previousEqual(Operands, Index, "loop0") ||
1070 previousEqual(Operands, Index, "loop1") ||
1071 previousEqual(Operands, Index, "sp1loop0") ||
1072 previousEqual(Operands, Index, "sp2loop0") ||
1073 previousEqual(Operands, Index, "sp3loop0");
1074}
1075}
1076
1077bool HexagonAsmParser::splitIdentifier(OperandVector &Operands) {
1078 AsmToken const &Token = getParser().getTok();
1079 StringRef String = Token.getString();
1080 SMLoc Loc = Token.getLoc();
Nirav Davefd910412016-06-17 16:06:17 +00001081 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001082 do {
1083 std::pair<StringRef, StringRef> HeadTail = String.split('.');
1084 if (!HeadTail.first.empty())
1085 Operands.push_back(HexagonOperand::CreateToken(HeadTail.first, Loc));
1086 if (!HeadTail.second.empty())
1087 Operands.push_back(HexagonOperand::CreateToken(
1088 String.substr(HeadTail.first.size(), 1), Loc));
1089 String = HeadTail.second;
1090 } while (!String.empty());
1091 return false;
1092}
1093
1094bool HexagonAsmParser::parseOperand(OperandVector &Operands) {
1095 unsigned Register;
1096 SMLoc Begin;
1097 SMLoc End;
1098 MCAsmLexer &Lexer = getLexer();
1099 if (!ParseRegister(Register, Begin, End)) {
1100 if (!ErrorMissingParenthesis)
1101 switch (Register) {
1102 default:
1103 break;
1104 case Hexagon::P0:
1105 case Hexagon::P1:
1106 case Hexagon::P2:
1107 case Hexagon::P3:
1108 if (previousEqual(Operands, 0, "if")) {
1109 if (WarnMissingParenthesis)
1110 Warning (Begin, "Missing parenthesis around predicate register");
1111 static char const *LParen = "(";
1112 static char const *RParen = ")";
1113 Operands.push_back(HexagonOperand::CreateToken(LParen, Begin));
1114 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End));
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +00001115 const AsmToken &MaybeDotNew = Lexer.getTok();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001116 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) &&
1117 MaybeDotNew.getString().equals_lower(".new"))
1118 splitIdentifier(Operands);
1119 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin));
1120 return false;
1121 }
1122 if (previousEqual(Operands, 0, "!") &&
1123 previousEqual(Operands, 1, "if")) {
1124 if (WarnMissingParenthesis)
1125 Warning (Begin, "Missing parenthesis around predicate register");
1126 static char const *LParen = "(";
1127 static char const *RParen = ")";
1128 Operands.insert(Operands.end () - 1,
1129 HexagonOperand::CreateToken(LParen, Begin));
1130 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End));
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +00001131 const AsmToken &MaybeDotNew = Lexer.getTok();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001132 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) &&
1133 MaybeDotNew.getString().equals_lower(".new"))
1134 splitIdentifier(Operands);
1135 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin));
1136 return false;
1137 }
1138 break;
1139 }
1140 Operands.push_back(HexagonOperand::CreateReg(
1141 Register, Begin, End));
1142 return false;
1143 }
1144 return splitIdentifier(Operands);
1145}
1146
1147bool HexagonAsmParser::isLabel(AsmToken &Token) {
1148 MCAsmLexer &Lexer = getLexer();
1149 AsmToken const &Second = Lexer.getTok();
1150 AsmToken Third = Lexer.peekTok();
1151 StringRef String = Token.getString();
1152 if (Token.is(AsmToken::TokenKind::LCurly) ||
1153 Token.is(AsmToken::TokenKind::RCurly))
1154 return false;
1155 if (!Token.is(AsmToken::TokenKind::Identifier))
1156 return true;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001157 if (!matchRegister(String.lower()))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001158 return true;
Colin LeMahieu9d851f02015-11-09 21:06:28 +00001159 (void)Second;
1160 assert(Second.is(AsmToken::Colon));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001161 StringRef Raw (String.data(), Third.getString().data() - String.data() +
1162 Third.getString().size());
1163 std::string Collapsed = Raw;
David Majnemerc7004902016-08-12 04:32:37 +00001164 Collapsed.erase(remove_if(Collapsed, isspace), Collapsed.end());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001165 StringRef Whole = Collapsed;
1166 std::pair<StringRef, StringRef> DotSplit = Whole.split('.');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001167 if (!matchRegister(DotSplit.first.lower()))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001168 return true;
1169 return false;
1170}
1171
1172bool HexagonAsmParser::handleNoncontigiousRegister(bool Contigious, SMLoc &Loc) {
1173 if (!Contigious && ErrorNoncontigiousRegister) {
1174 Error(Loc, "Register name is not contigious");
1175 return true;
1176 }
1177 if (!Contigious && WarnNoncontigiousRegister)
1178 Warning(Loc, "Register name is not contigious");
1179 return false;
1180}
1181
1182bool HexagonAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) {
1183 MCAsmLexer &Lexer = getLexer();
1184 StartLoc = getLexer().getLoc();
1185 SmallVector<AsmToken, 5> Lookahead;
1186 StringRef RawString(Lexer.getTok().getString().data(), 0);
1187 bool Again = Lexer.is(AsmToken::Identifier);
1188 bool NeededWorkaround = false;
1189 while (Again) {
1190 AsmToken const &Token = Lexer.getTok();
1191 RawString = StringRef(RawString.data(),
1192 Token.getString().data() - RawString.data () +
1193 Token.getString().size());
1194 Lookahead.push_back(Token);
1195 Lexer.Lex();
1196 bool Contigious = Lexer.getTok().getString().data() ==
1197 Lookahead.back().getString().data() +
1198 Lookahead.back().getString().size();
1199 bool Type = Lexer.is(AsmToken::Identifier) || Lexer.is(AsmToken::Dot) ||
1200 Lexer.is(AsmToken::Integer) || Lexer.is(AsmToken::Real) ||
1201 Lexer.is(AsmToken::Colon);
1202 bool Workaround = Lexer.is(AsmToken::Colon) ||
1203 Lookahead.back().is(AsmToken::Colon);
1204 Again = (Contigious && Type) || (Workaround && Type);
1205 NeededWorkaround = NeededWorkaround || (Again && !(Contigious && Type));
1206 }
1207 std::string Collapsed = RawString;
David Majnemerc7004902016-08-12 04:32:37 +00001208 Collapsed.erase(remove_if(Collapsed, isspace), Collapsed.end());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001209 StringRef FullString = Collapsed;
1210 std::pair<StringRef, StringRef> DotSplit = FullString.split('.');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001211 unsigned DotReg = matchRegister(DotSplit.first.lower());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001212 if (DotReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1213 if (DotSplit.second.empty()) {
1214 RegNo = DotReg;
1215 EndLoc = Lexer.getLoc();
1216 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1217 return true;
1218 return false;
1219 } else {
1220 RegNo = DotReg;
1221 size_t First = RawString.find('.');
1222 StringRef DotString (RawString.data() + First, RawString.size() - First);
1223 Lexer.UnLex(AsmToken(AsmToken::Identifier, DotString));
1224 EndLoc = Lexer.getLoc();
1225 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1226 return true;
1227 return false;
1228 }
1229 }
1230 std::pair<StringRef, StringRef> ColonSplit = StringRef(FullString).split(':');
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001231 unsigned ColonReg = matchRegister(ColonSplit.first.lower());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001232 if (ColonReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) {
1233 Lexer.UnLex(Lookahead.back());
1234 Lookahead.pop_back();
1235 Lexer.UnLex(Lookahead.back());
1236 Lookahead.pop_back();
1237 RegNo = ColonReg;
1238 EndLoc = Lexer.getLoc();
1239 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc))
1240 return true;
1241 return false;
1242 }
1243 while (!Lookahead.empty()) {
1244 Lexer.UnLex(Lookahead.back());
1245 Lookahead.pop_back();
1246 }
1247 return true;
1248}
1249
1250bool HexagonAsmParser::implicitExpressionLocation(OperandVector &Operands) {
1251 if (previousEqual(Operands, 0, "call"))
1252 return true;
1253 if (previousEqual(Operands, 0, "jump"))
1254 if (!getLexer().getTok().is(AsmToken::Colon))
1255 return true;
1256 if (previousEqual(Operands, 0, "(") && previousIsLoop(Operands, 1))
1257 return true;
1258 if (previousEqual(Operands, 1, ":") && previousEqual(Operands, 2, "jump") &&
1259 (previousEqual(Operands, 0, "nt") || previousEqual(Operands, 0, "t")))
1260 return true;
1261 return false;
1262}
1263
1264bool HexagonAsmParser::parseExpression(MCExpr const *& Expr) {
1265 llvm::SmallVector<AsmToken, 4> Tokens;
1266 MCAsmLexer &Lexer = getLexer();
1267 bool Done = false;
1268 static char const * Comma = ",";
1269 do {
1270 Tokens.emplace_back (Lexer.getTok());
Nirav Davefd910412016-06-17 16:06:17 +00001271 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001272 switch (Tokens.back().getKind())
1273 {
1274 case AsmToken::TokenKind::Hash:
1275 if (Tokens.size () > 1)
1276 if ((Tokens.end () - 2)->getKind() == AsmToken::TokenKind::Plus) {
1277 Tokens.insert(Tokens.end() - 2,
1278 AsmToken(AsmToken::TokenKind::Comma, Comma));
1279 Done = true;
1280 }
1281 break;
1282 case AsmToken::TokenKind::RCurly:
1283 case AsmToken::TokenKind::EndOfStatement:
1284 case AsmToken::TokenKind::Eof:
1285 Done = true;
1286 break;
1287 default:
1288 break;
1289 }
1290 } while (!Done);
1291 while (!Tokens.empty()) {
1292 Lexer.UnLex(Tokens.back());
1293 Tokens.pop_back();
1294 }
1295 return getParser().parseExpression(Expr);
1296}
1297
1298bool HexagonAsmParser::parseExpressionOrOperand(OperandVector &Operands) {
1299 if (implicitExpressionLocation(Operands)) {
1300 MCAsmParser &Parser = getParser();
1301 SMLoc Loc = Parser.getLexer().getLoc();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001302 MCExpr const *Expr = nullptr;
1303 bool Error = parseExpression(Expr);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001304 Expr = HexagonMCExpr::create(Expr, getContext());
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001305 if (!Error)
1306 Operands.push_back(HexagonOperand::CreateImm(Expr, Loc, Loc));
1307 return Error;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001308 }
1309 return parseOperand(Operands);
1310}
1311
1312/// Parse an instruction.
1313bool HexagonAsmParser::parseInstruction(OperandVector &Operands) {
1314 MCAsmParser &Parser = getParser();
1315 MCAsmLexer &Lexer = getLexer();
1316 while (true) {
1317 AsmToken const &Token = Parser.getTok();
1318 switch (Token.getKind()) {
1319 case AsmToken::EndOfStatement: {
Nirav Davefd910412016-06-17 16:06:17 +00001320 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001321 return false;
1322 }
1323 case AsmToken::LCurly: {
1324 if (!Operands.empty())
1325 return true;
1326 Operands.push_back(
1327 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001328 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001329 return false;
1330 }
1331 case AsmToken::RCurly: {
1332 if (Operands.empty()) {
1333 Operands.push_back(
1334 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001335 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001336 }
1337 return false;
1338 }
1339 case AsmToken::Comma: {
Nirav Davefd910412016-06-17 16:06:17 +00001340 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001341 continue;
1342 }
1343 case AsmToken::EqualEqual:
1344 case AsmToken::ExclaimEqual:
1345 case AsmToken::GreaterEqual:
1346 case AsmToken::GreaterGreater:
1347 case AsmToken::LessEqual:
1348 case AsmToken::LessLess: {
1349 Operands.push_back(HexagonOperand::CreateToken(
1350 Token.getString().substr(0, 1), Token.getLoc()));
1351 Operands.push_back(HexagonOperand::CreateToken(
1352 Token.getString().substr(1, 1), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001353 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001354 continue;
1355 }
1356 case AsmToken::Hash: {
1357 bool MustNotExtend = false;
1358 bool ImplicitExpression = implicitExpressionLocation(Operands);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001359 SMLoc ExprLoc = Lexer.getLoc();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001360 if (!ImplicitExpression)
1361 Operands.push_back(
1362 HexagonOperand::CreateToken(Token.getString(), Token.getLoc()));
Nirav Davefd910412016-06-17 16:06:17 +00001363 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001364 bool MustExtend = false;
1365 bool HiOnly = false;
1366 bool LoOnly = false;
1367 if (Lexer.is(AsmToken::Hash)) {
Nirav Davefd910412016-06-17 16:06:17 +00001368 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001369 MustExtend = true;
1370 } else if (ImplicitExpression)
1371 MustNotExtend = true;
1372 AsmToken const &Token = Parser.getTok();
1373 if (Token.is(AsmToken::Identifier)) {
1374 StringRef String = Token.getString();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001375 if (String.lower() == "hi") {
1376 HiOnly = true;
1377 } else if (String.lower() == "lo") {
1378 LoOnly = true;
1379 }
1380 if (HiOnly || LoOnly) {
1381 AsmToken LParen = Lexer.peekTok();
1382 if (!LParen.is(AsmToken::LParen)) {
1383 HiOnly = false;
1384 LoOnly = false;
1385 } else {
Nirav Davefd910412016-06-17 16:06:17 +00001386 Lex();
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001387 }
1388 }
1389 }
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001390 MCExpr const *Expr = nullptr;
1391 if (parseExpression(Expr))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001392 return true;
1393 int64_t Value;
1394 MCContext &Context = Parser.getContext();
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001395 assert(Expr != nullptr);
1396 if (Expr->evaluateAsAbsolute(Value)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001397 if (HiOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001398 Expr = MCBinaryExpr::createLShr(
1399 Expr, MCConstantExpr::create(16, Context), Context);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001400 if (HiOnly || LoOnly)
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001401 Expr = MCBinaryExpr::createAnd(Expr,
1402 MCConstantExpr::create(0xffff, Context),
1403 Context);
Colin LeMahieu5cb6eea2016-03-01 21:37:41 +00001404 } else {
1405 MCValue Value;
1406 if (Expr->evaluateAsRelocatable(Value, nullptr, nullptr)) {
1407 if (!Value.isAbsolute()) {
1408 switch(Value.getAccessVariant()) {
1409 case MCSymbolRefExpr::VariantKind::VK_TPREL:
1410 case MCSymbolRefExpr::VariantKind::VK_DTPREL:
1411 // Don't lazy extend these expression variants
1412 MustNotExtend = !MustExtend;
1413 break;
1414 default:
1415 break;
1416 }
1417 }
1418 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001419 }
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001420 Expr = HexagonMCExpr::create(Expr, Context);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001421 HexagonMCInstrInfo::setMustNotExtend(*Expr, MustNotExtend);
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001422 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001423 std::unique_ptr<HexagonOperand> Operand =
1424 HexagonOperand::CreateImm(Expr, ExprLoc, ExprLoc);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001425 Operands.push_back(std::move(Operand));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001426 continue;
1427 }
1428 default:
1429 break;
1430 }
1431 if (parseExpressionOrOperand(Operands))
1432 return true;
1433 }
1434}
1435
1436bool HexagonAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1437 StringRef Name,
1438 AsmToken ID,
1439 OperandVector &Operands) {
1440 getLexer().UnLex(ID);
1441 return parseInstruction(Operands);
1442}
1443
1444namespace {
1445MCInst makeCombineInst(int opCode, MCOperand &Rdd,
1446 MCOperand &MO1, MCOperand &MO2) {
1447 MCInst TmpInst;
1448 TmpInst.setOpcode(opCode);
1449 TmpInst.addOperand(Rdd);
1450 TmpInst.addOperand(MO1);
1451 TmpInst.addOperand(MO2);
1452
1453 return TmpInst;
1454}
1455}
1456
1457// Define this matcher function after the auto-generated include so we
1458// have the match class enum definitions.
1459unsigned HexagonAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
1460 unsigned Kind) {
1461 HexagonOperand *Op = static_cast<HexagonOperand *>(&AsmOp);
1462
1463 switch (Kind) {
1464 case MCK_0: {
1465 int64_t Value;
1466 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 0
1467 ? Match_Success
1468 : Match_InvalidOperand;
1469 }
1470 case MCK_1: {
1471 int64_t Value;
1472 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 1
1473 ? Match_Success
1474 : Match_InvalidOperand;
1475 }
1476 case MCK__MINUS_1: {
1477 int64_t Value;
1478 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == -1
1479 ? Match_Success
1480 : Match_InvalidOperand;
1481 }
1482 }
1483 if (Op->Kind == HexagonOperand::Token && Kind != InvalidMatchClass) {
1484 StringRef myStringRef = StringRef(Op->Tok.Data, Op->Tok.Length);
1485 if (matchTokenString(myStringRef.lower()) == (MatchClassKind)Kind)
1486 return Match_Success;
1487 if (matchTokenString(myStringRef.upper()) == (MatchClassKind)Kind)
1488 return Match_Success;
1489 }
1490
1491 DEBUG(dbgs() << "Unmatched Operand:");
1492 DEBUG(Op->dump());
1493 DEBUG(dbgs() << "\n");
1494
1495 return Match_InvalidOperand;
1496}
1497
Nirav Dave2364748a2016-09-16 18:30:20 +00001498// FIXME: Calls to OutOfRange shoudl propagate failure up to parseStatement.
1499bool HexagonAsmParser::OutOfRange(SMLoc IDLoc, long long Val, long long Max) {
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001500 std::string errStr;
1501 raw_string_ostream ES(errStr);
Alexey Samsonov44ff2042015-12-02 22:59:22 +00001502 ES << "value " << Val << "(" << format_hex(Val, 0) << ") out of range: ";
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001503 if (Max >= 0)
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001504 ES << "0-" << Max;
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001505 else
Alexey Samsonovbcfabaa2015-12-02 21:13:43 +00001506 ES << Max << "-" << (-Max - 1);
Malcolm Parsons06ac79c2016-11-02 16:43:50 +00001507 return Parser.printError(IDLoc, ES.str());
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001508}
1509
1510int HexagonAsmParser::processInstruction(MCInst &Inst,
1511 OperandVector const &Operands,
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001512 SMLoc IDLoc) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001513 MCContext &Context = getParser().getContext();
1514 const MCRegisterInfo *RI = getContext().getRegisterInfo();
1515 std::string r = "r";
1516 std::string v = "v";
1517 std::string Colon = ":";
1518
1519 bool is32bit = false; // used to distinguish between CONST32 and CONST64
1520 switch (Inst.getOpcode()) {
1521 default:
1522 break;
1523
Colin LeMahieuecef1d92016-02-16 20:38:17 +00001524 case Hexagon::A2_iconst: {
1525 Inst.setOpcode(Hexagon::A2_addi);
1526 MCOperand Reg = Inst.getOperand(0);
1527 MCOperand S16 = Inst.getOperand(1);
1528 HexagonMCInstrInfo::setMustNotExtend(*S16.getExpr());
1529 HexagonMCInstrInfo::setS23_2_reloc(*S16.getExpr());
1530 Inst.clear();
1531 Inst.addOperand(Reg);
1532 Inst.addOperand(MCOperand::createReg(Hexagon::R0));
1533 Inst.addOperand(S16);
1534 break;
1535 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001536 case Hexagon::M4_mpyrr_addr:
1537 case Hexagon::S4_addi_asl_ri:
1538 case Hexagon::S4_addi_lsr_ri:
1539 case Hexagon::S4_andi_asl_ri:
1540 case Hexagon::S4_andi_lsr_ri:
1541 case Hexagon::S4_ori_asl_ri:
1542 case Hexagon::S4_ori_lsr_ri:
1543 case Hexagon::S4_or_andix:
1544 case Hexagon::S4_subi_asl_ri:
1545 case Hexagon::S4_subi_lsr_ri: {
1546 MCOperand &Ry = Inst.getOperand(0);
1547 MCOperand &src = Inst.getOperand(2);
1548 if (RI->getEncodingValue(Ry.getReg()) != RI->getEncodingValue(src.getReg()))
1549 return Match_InvalidOperand;
1550 break;
1551 }
1552
1553 case Hexagon::C2_cmpgei: {
1554 MCOperand &MO = Inst.getOperand(2);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001555 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001556 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001557 Inst.setOpcode(Hexagon::C2_cmpgti);
1558 break;
1559 }
1560
1561 case Hexagon::C2_cmpgeui: {
1562 MCOperand &MO = Inst.getOperand(2);
1563 int64_t Value;
1564 bool Success = MO.getExpr()->evaluateAsAbsolute(Value);
Colin LeMahieu9d851f02015-11-09 21:06:28 +00001565 (void)Success;
1566 assert(Success && "Assured by matcher");
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001567 if (Value == 0) {
1568 MCInst TmpInst;
1569 MCOperand &Pd = Inst.getOperand(0);
1570 MCOperand &Rt = Inst.getOperand(1);
1571 TmpInst.setOpcode(Hexagon::C2_cmpeq);
1572 TmpInst.addOperand(Pd);
1573 TmpInst.addOperand(Rt);
1574 TmpInst.addOperand(Rt);
1575 Inst = TmpInst;
1576 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001577 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001578 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001579 Inst.setOpcode(Hexagon::C2_cmpgtui);
1580 }
1581 break;
1582 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001583
1584 // Translate a "$Rdd = $Rss" to "$Rdd = combine($Rs, $Rt)"
1585 case Hexagon::A2_tfrp: {
1586 MCOperand &MO = Inst.getOperand(1);
1587 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001588 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001589 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001590 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001591 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001592 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001593 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001594 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001595 Inst.setOpcode(Hexagon::A2_combinew);
1596 break;
1597 }
1598
1599 case Hexagon::A2_tfrpt:
1600 case Hexagon::A2_tfrpf: {
1601 MCOperand &MO = Inst.getOperand(2);
1602 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001603 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001604 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001605 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001606 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001607 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001608 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001609 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001610 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrpt)
1611 ? Hexagon::C2_ccombinewt
1612 : Hexagon::C2_ccombinewf);
1613 break;
1614 }
1615 case Hexagon::A2_tfrptnew:
1616 case Hexagon::A2_tfrpfnew: {
1617 MCOperand &MO = Inst.getOperand(2);
1618 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001619 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001620 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001621 MO.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001622 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001623 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001624 StringRef Reg2(R2);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001625 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001626 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrptnew)
1627 ? Hexagon::C2_ccombinewnewt
1628 : Hexagon::C2_ccombinewnewf);
1629 break;
1630 }
1631
Krzysztof Parzyszek0e7d2d32016-04-28 16:43:16 +00001632 // Translate a "$Vdd = $Vss" to "$Vdd = vcombine($Vs, $Vt)"
Krzysztof Parzyszekeabc0d02016-08-16 17:14:44 +00001633 case Hexagon::V6_vassignp: {
Krzysztof Parzyszek0e7d2d32016-04-28 16:43:16 +00001634 MCOperand &MO = Inst.getOperand(1);
1635 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg());
1636 std::string R1 = v + llvm::utostr(RegPairNum + 1);
1637 MO.setReg(MatchRegisterName(R1));
1638 // Add a new operand for the second register in the pair.
1639 std::string R2 = v + llvm::utostr(RegPairNum);
1640 Inst.addOperand(MCOperand::createReg(MatchRegisterName(R2)));
1641 Inst.setOpcode(Hexagon::V6_vcombine);
1642 break;
1643 }
1644
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001645 // Translate a "$Rx = CONST32(#imm)" to "$Rx = memw(gp+#LABEL) "
1646 case Hexagon::CONST32:
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001647 is32bit = true;
1648 // Translate a "$Rx:y = CONST64(#imm)" to "$Rx:y = memd(gp+#LABEL) "
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +00001649 case Hexagon::CONST64:
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001650 // FIXME: need better way to detect AsmStreamer (upstream removed getKind())
1651 if (!Parser.getStreamer().hasRawTextSupport()) {
1652 MCELFStreamer *MES = static_cast<MCELFStreamer *>(&Parser.getStreamer());
1653 MCOperand &MO_1 = Inst.getOperand(1);
1654 MCOperand &MO_0 = Inst.getOperand(0);
1655
1656 // push section onto section stack
1657 MES->PushSection();
1658
1659 std::string myCharStr;
1660 MCSectionELF *mySection;
1661
1662 // check if this as an immediate or a symbol
1663 int64_t Value;
1664 bool Absolute = MO_1.getExpr()->evaluateAsAbsolute(Value);
1665 if (Absolute) {
1666 // Create a new section - one for each constant
1667 // Some or all of the zeros are replaced with the given immediate.
1668 if (is32bit) {
1669 std::string myImmStr = utohexstr(static_cast<uint32_t>(Value));
1670 myCharStr = StringRef(".gnu.linkonce.l4.CONST_00000000")
1671 .drop_back(myImmStr.size())
1672 .str() +
1673 myImmStr;
1674 } else {
1675 std::string myImmStr = utohexstr(Value);
1676 myCharStr = StringRef(".gnu.linkonce.l8.CONST_0000000000000000")
1677 .drop_back(myImmStr.size())
1678 .str() +
1679 myImmStr;
1680 }
1681
1682 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1683 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1684 } else if (MO_1.isExpr()) {
1685 // .lita - for expressions
1686 myCharStr = ".lita";
1687 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS,
1688 ELF::SHF_ALLOC | ELF::SHF_WRITE);
1689 } else
1690 llvm_unreachable("unexpected type of machine operand!");
1691
1692 MES->SwitchSection(mySection);
1693 unsigned byteSize = is32bit ? 4 : 8;
1694 getStreamer().EmitCodeAlignment(byteSize, byteSize);
1695
1696 MCSymbol *Sym;
1697
1698 // for symbols, get rid of prepended ".gnu.linkonce.lx."
1699
1700 // emit symbol if needed
1701 if (Absolute) {
1702 Sym = getContext().getOrCreateSymbol(StringRef(myCharStr.c_str() + 16));
1703 if (Sym->isUndefined()) {
1704 getStreamer().EmitLabel(Sym);
1705 getStreamer().EmitSymbolAttribute(Sym, MCSA_Global);
1706 getStreamer().EmitIntValue(Value, byteSize);
1707 }
1708 } else if (MO_1.isExpr()) {
1709 const char *StringStart = 0;
1710 const char *StringEnd = 0;
1711 if (*Operands[4]->getStartLoc().getPointer() == '#') {
1712 StringStart = Operands[5]->getStartLoc().getPointer();
1713 StringEnd = Operands[6]->getStartLoc().getPointer();
1714 } else { // no pound
1715 StringStart = Operands[4]->getStartLoc().getPointer();
1716 StringEnd = Operands[5]->getStartLoc().getPointer();
1717 }
1718
1719 unsigned size = StringEnd - StringStart;
1720 std::string DotConst = ".CONST_";
1721 Sym = getContext().getOrCreateSymbol(DotConst +
1722 StringRef(StringStart, size));
1723
1724 if (Sym->isUndefined()) {
1725 // case where symbol is not yet defined: emit symbol
1726 getStreamer().EmitLabel(Sym);
1727 getStreamer().EmitSymbolAttribute(Sym, MCSA_Local);
1728 getStreamer().EmitValue(MO_1.getExpr(), 4);
1729 }
1730 } else
1731 llvm_unreachable("unexpected type of machine operand!");
1732
1733 MES->PopSection();
1734
1735 if (Sym) {
1736 MCInst TmpInst;
1737 if (is32bit) // 32 bit
1738 TmpInst.setOpcode(Hexagon::L2_loadrigp);
1739 else // 64 bit
1740 TmpInst.setOpcode(Hexagon::L2_loadrdgp);
1741
1742 TmpInst.addOperand(MO_0);
1743 TmpInst.addOperand(
1744 MCOperand::createExpr(MCSymbolRefExpr::create(Sym, getContext())));
1745 Inst = TmpInst;
1746 }
1747 }
1748 break;
1749
1750 // Translate a "$Rdd = #-imm" to "$Rdd = combine(#[-1,0], #-imm)"
1751 case Hexagon::A2_tfrpi: {
1752 MCOperand &Rdd = Inst.getOperand(0);
1753 MCOperand &MO = Inst.getOperand(1);
1754 int64_t Value;
1755 int sVal = (MO.getExpr()->evaluateAsAbsolute(Value) && Value < 0) ? -1 : 0;
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001756 MCOperand imm(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001757 HexagonMCExpr::create(MCConstantExpr::create(sVal, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001758 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, imm, MO);
1759 break;
1760 }
1761
1762 // Translate a "$Rdd = [#]#imm" to "$Rdd = combine(#, [#]#imm)"
1763 case Hexagon::TFRI64_V4: {
1764 MCOperand &Rdd = Inst.getOperand(0);
1765 MCOperand &MO = Inst.getOperand(1);
1766 int64_t Value;
1767 if (MO.getExpr()->evaluateAsAbsolute(Value)) {
David Majnemere61e4bf2016-06-21 05:10:24 +00001768 int s8 = Hi_32(Value);
1769 if (!isInt<8>(s8))
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001770 OutOfRange(IDLoc, s8, -128);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001771 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001772 MCConstantExpr::create(s8, Context), Context))); // upper 32
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001773 auto Expr = HexagonMCExpr::create(
David Majnemere61e4bf2016-06-21 05:10:24 +00001774 MCConstantExpr::create(Lo_32(Value), Context), Context);
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001775 HexagonMCInstrInfo::setMustExtend(*Expr, HexagonMCInstrInfo::mustExtend(*MO.getExpr()));
1776 MCOperand imm2(MCOperand::createExpr(Expr)); // lower 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001777 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, imm2);
1778 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001779 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001780 MCConstantExpr::create(0, Context), Context))); // upper 32
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001781 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, MO);
1782 }
1783 break;
1784 }
1785
1786 // Handle $Rdd = combine(##imm, #imm)"
1787 case Hexagon::TFRI64_V2_ext: {
1788 MCOperand &Rdd = Inst.getOperand(0);
1789 MCOperand &MO1 = Inst.getOperand(1);
1790 MCOperand &MO2 = Inst.getOperand(2);
1791 int64_t Value;
1792 if (MO2.getExpr()->evaluateAsAbsolute(Value)) {
1793 int s8 = Value;
1794 if (s8 < -128 || s8 > 127)
1795 OutOfRange(IDLoc, s8, -128);
1796 }
1797 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, MO1, MO2);
1798 break;
1799 }
1800
1801 // Handle $Rdd = combine(#imm, ##imm)"
1802 case Hexagon::A4_combineii: {
1803 MCOperand &Rdd = Inst.getOperand(0);
1804 MCOperand &MO1 = Inst.getOperand(1);
1805 int64_t Value;
1806 if (MO1.getExpr()->evaluateAsAbsolute(Value)) {
1807 int s8 = Value;
1808 if (s8 < -128 || s8 > 127)
1809 OutOfRange(IDLoc, s8, -128);
1810 }
1811 MCOperand &MO2 = Inst.getOperand(2);
1812 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, MO1, MO2);
1813 break;
1814 }
1815
1816 case Hexagon::S2_tableidxb_goodsyntax: {
1817 Inst.setOpcode(Hexagon::S2_tableidxb);
1818 break;
1819 }
1820
1821 case Hexagon::S2_tableidxh_goodsyntax: {
1822 MCInst TmpInst;
1823 MCOperand &Rx = Inst.getOperand(0);
1824 MCOperand &_dst_ = Inst.getOperand(1);
1825 MCOperand &Rs = Inst.getOperand(2);
1826 MCOperand &Imm4 = Inst.getOperand(3);
1827 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001828 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001829 Imm6.getExpr(), MCConstantExpr::create(1, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001830 TmpInst.setOpcode(Hexagon::S2_tableidxh);
1831 TmpInst.addOperand(Rx);
1832 TmpInst.addOperand(_dst_);
1833 TmpInst.addOperand(Rs);
1834 TmpInst.addOperand(Imm4);
1835 TmpInst.addOperand(Imm6);
1836 Inst = TmpInst;
1837 break;
1838 }
1839
1840 case Hexagon::S2_tableidxw_goodsyntax: {
1841 MCInst TmpInst;
1842 MCOperand &Rx = Inst.getOperand(0);
1843 MCOperand &_dst_ = Inst.getOperand(1);
1844 MCOperand &Rs = Inst.getOperand(2);
1845 MCOperand &Imm4 = Inst.getOperand(3);
1846 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001847 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001848 Imm6.getExpr(), MCConstantExpr::create(2, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001849 TmpInst.setOpcode(Hexagon::S2_tableidxw);
1850 TmpInst.addOperand(Rx);
1851 TmpInst.addOperand(_dst_);
1852 TmpInst.addOperand(Rs);
1853 TmpInst.addOperand(Imm4);
1854 TmpInst.addOperand(Imm6);
1855 Inst = TmpInst;
1856 break;
1857 }
1858
1859 case Hexagon::S2_tableidxd_goodsyntax: {
1860 MCInst TmpInst;
1861 MCOperand &Rx = Inst.getOperand(0);
1862 MCOperand &_dst_ = Inst.getOperand(1);
1863 MCOperand &Rs = Inst.getOperand(2);
1864 MCOperand &Imm4 = Inst.getOperand(3);
1865 MCOperand &Imm6 = Inst.getOperand(4);
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001866 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001867 Imm6.getExpr(), MCConstantExpr::create(3, Context), Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001868 TmpInst.setOpcode(Hexagon::S2_tableidxd);
1869 TmpInst.addOperand(Rx);
1870 TmpInst.addOperand(_dst_);
1871 TmpInst.addOperand(Rs);
1872 TmpInst.addOperand(Imm4);
1873 TmpInst.addOperand(Imm6);
1874 Inst = TmpInst;
1875 break;
1876 }
1877
1878 case Hexagon::M2_mpyui: {
1879 Inst.setOpcode(Hexagon::M2_mpyi);
1880 break;
1881 }
1882 case Hexagon::M2_mpysmi: {
1883 MCInst TmpInst;
1884 MCOperand &Rd = Inst.getOperand(0);
1885 MCOperand &Rs = Inst.getOperand(1);
1886 MCOperand &Imm = Inst.getOperand(2);
1887 int64_t Value;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001888 MCExpr const &Expr = *Imm.getExpr();
1889 bool Absolute = Expr.evaluateAsAbsolute(Value);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001890 assert(Absolute);
1891 (void)Absolute;
Colin LeMahieu73cd6862016-02-29 18:39:51 +00001892 if (!HexagonMCInstrInfo::mustExtend(Expr)) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001893 if (Value < 0 && Value > -256) {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001894 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001895 MCConstantExpr::create(Value * -1, Context), Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001896 TmpInst.setOpcode(Hexagon::M2_mpysin);
1897 } else if (Value < 256 && Value >= 0)
1898 TmpInst.setOpcode(Hexagon::M2_mpysip);
1899 else
1900 return Match_InvalidOperand;
1901 } else {
1902 if (Value >= 0)
1903 TmpInst.setOpcode(Hexagon::M2_mpysip);
1904 else
1905 return Match_InvalidOperand;
1906 }
1907 TmpInst.addOperand(Rd);
1908 TmpInst.addOperand(Rs);
1909 TmpInst.addOperand(Imm);
1910 Inst = TmpInst;
1911 break;
1912 }
1913
1914 case Hexagon::S2_asr_i_r_rnd_goodsyntax: {
1915 MCOperand &Imm = Inst.getOperand(2);
1916 MCInst TmpInst;
1917 int64_t Value;
1918 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1919 assert(Absolute);
1920 (void)Absolute;
1921 if (Value == 0) { // convert to $Rd = $Rs
1922 TmpInst.setOpcode(Hexagon::A2_tfr);
1923 MCOperand &Rd = Inst.getOperand(0);
1924 MCOperand &Rs = Inst.getOperand(1);
1925 TmpInst.addOperand(Rd);
1926 TmpInst.addOperand(Rs);
1927 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001928 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001929 MCBinaryExpr::createSub(Imm.getExpr(),
1930 MCConstantExpr::create(1, Context), Context),
1931 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001932 TmpInst.setOpcode(Hexagon::S2_asr_i_r_rnd);
1933 MCOperand &Rd = Inst.getOperand(0);
1934 MCOperand &Rs = Inst.getOperand(1);
1935 TmpInst.addOperand(Rd);
1936 TmpInst.addOperand(Rs);
1937 TmpInst.addOperand(Imm);
1938 }
1939 Inst = TmpInst;
1940 break;
1941 }
1942
1943 case Hexagon::S2_asr_i_p_rnd_goodsyntax: {
1944 MCOperand &Rdd = Inst.getOperand(0);
1945 MCOperand &Rss = Inst.getOperand(1);
1946 MCOperand &Imm = Inst.getOperand(2);
1947 int64_t Value;
1948 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
1949 assert(Absolute);
1950 (void)Absolute;
1951 if (Value == 0) { // convert to $Rdd = combine ($Rs[0], $Rs[1])
1952 MCInst TmpInst;
1953 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00001954 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001955 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001956 Rss.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001957 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00001958 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001959 StringRef Reg2(R2);
1960 TmpInst.setOpcode(Hexagon::A2_combinew);
1961 TmpInst.addOperand(Rdd);
1962 TmpInst.addOperand(Rss);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001963 TmpInst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001964 Inst = TmpInst;
1965 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00001966 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00001967 MCBinaryExpr::createSub(Imm.getExpr(),
1968 MCConstantExpr::create(1, Context), Context),
1969 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001970 Inst.setOpcode(Hexagon::S2_asr_i_p_rnd);
1971 }
1972 break;
1973 }
1974
1975 case Hexagon::A4_boundscheck: {
1976 MCOperand &Rs = Inst.getOperand(1);
1977 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
1978 if (RegNum & 1) { // Odd mapped to raw:hi, regpair is rodd:odd-1, like r3:2
1979 Inst.setOpcode(Hexagon::A4_boundscheck_hi);
1980 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00001981 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001982 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001983 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001984 } else { // raw:lo
1985 Inst.setOpcode(Hexagon::A4_boundscheck_lo);
1986 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00001987 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001988 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00001989 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001990 }
1991 break;
1992 }
1993
1994 case Hexagon::A2_addsp: {
1995 MCOperand &Rs = Inst.getOperand(1);
1996 unsigned int RegNum = RI->getEncodingValue(Rs.getReg());
1997 if (RegNum & 1) { // Odd mapped to raw:hi
1998 Inst.setOpcode(Hexagon::A2_addsph);
1999 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002000 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002001 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002002 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002003 } else { // Even mapped raw:lo
2004 Inst.setOpcode(Hexagon::A2_addspl);
2005 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002006 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002007 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002008 Rs.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002009 }
2010 break;
2011 }
2012
2013 case Hexagon::M2_vrcmpys_s1: {
2014 MCOperand &Rt = Inst.getOperand(2);
2015 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2016 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2017 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_h);
2018 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002019 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002020 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002021 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002022 } else { // Even mapped sat:raw:lo
2023 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_l);
2024 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002025 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002026 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002027 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002028 }
2029 break;
2030 }
2031
2032 case Hexagon::M2_vrcmpys_acc_s1: {
2033 MCInst TmpInst;
2034 MCOperand &Rxx = Inst.getOperand(0);
2035 MCOperand &Rss = Inst.getOperand(2);
2036 MCOperand &Rt = Inst.getOperand(3);
2037 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2038 if (RegNum & 1) { // Odd mapped to sat:raw:hi
2039 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_h);
2040 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002041 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002042 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002043 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002044 } else { // Even mapped sat:raw:lo
2045 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_l);
2046 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002047 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002048 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002049 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002050 }
2051 // Registers are in different positions
2052 TmpInst.addOperand(Rxx);
2053 TmpInst.addOperand(Rxx);
2054 TmpInst.addOperand(Rss);
2055 TmpInst.addOperand(Rt);
2056 Inst = TmpInst;
2057 break;
2058 }
2059
2060 case Hexagon::M2_vrcmpys_s1rp: {
2061 MCOperand &Rt = Inst.getOperand(2);
2062 unsigned int RegNum = RI->getEncodingValue(Rt.getReg());
2063 if (RegNum & 1) { // Odd mapped to rnd:sat:raw:hi
2064 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_h);
2065 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002066 r + llvm::utostr(RegNum) + Colon + llvm::utostr(RegNum - 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002067 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002068 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002069 } else { // Even mapped rnd:sat:raw:lo
2070 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_l);
2071 std::string Name =
Craig Topper3ef74f52016-01-31 20:00:24 +00002072 r + llvm::utostr(RegNum + 1) + Colon + llvm::utostr(RegNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002073 StringRef RegPair = Name;
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002074 Rt.setReg(matchRegister(RegPair));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002075 }
2076 break;
2077 }
2078
2079 case Hexagon::S5_asrhub_rnd_sat_goodsyntax: {
2080 MCOperand &Imm = Inst.getOperand(2);
2081 int64_t Value;
2082 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
2083 assert(Absolute);
2084 (void)Absolute;
2085 if (Value == 0)
2086 Inst.setOpcode(Hexagon::S2_vsathub);
2087 else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002088 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002089 MCBinaryExpr::createSub(Imm.getExpr(),
2090 MCConstantExpr::create(1, Context), Context),
2091 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002092 Inst.setOpcode(Hexagon::S5_asrhub_rnd_sat);
2093 }
2094 break;
2095 }
2096
2097 case Hexagon::S5_vasrhrnd_goodsyntax: {
2098 MCOperand &Rdd = Inst.getOperand(0);
2099 MCOperand &Rss = Inst.getOperand(1);
2100 MCOperand &Imm = Inst.getOperand(2);
2101 int64_t Value;
2102 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value);
2103 assert(Absolute);
2104 (void)Absolute;
2105 if (Value == 0) {
2106 MCInst TmpInst;
2107 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg());
Craig Topper3ef74f52016-01-31 20:00:24 +00002108 std::string R1 = r + llvm::utostr(RegPairNum + 1);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002109 StringRef Reg1(R1);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002110 Rss.setReg(matchRegister(Reg1));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002111 // Add a new operand for the second register in the pair.
Craig Topper3ef74f52016-01-31 20:00:24 +00002112 std::string R2 = r + llvm::utostr(RegPairNum);
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002113 StringRef Reg2(R2);
2114 TmpInst.setOpcode(Hexagon::A2_combinew);
2115 TmpInst.addOperand(Rdd);
2116 TmpInst.addOperand(Rss);
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002117 TmpInst.addOperand(MCOperand::createReg(matchRegister(Reg2)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002118 Inst = TmpInst;
2119 } else {
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002120 Imm.setExpr(HexagonMCExpr::create(
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002121 MCBinaryExpr::createSub(Imm.getExpr(),
2122 MCConstantExpr::create(1, Context), Context),
2123 Context));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002124 Inst.setOpcode(Hexagon::S5_vasrhrnd);
2125 }
2126 break;
2127 }
2128
2129 case Hexagon::A2_not: {
2130 MCInst TmpInst;
2131 MCOperand &Rd = Inst.getOperand(0);
2132 MCOperand &Rs = Inst.getOperand(1);
2133 TmpInst.setOpcode(Hexagon::A2_subri);
2134 TmpInst.addOperand(Rd);
Colin LeMahieu98c8e072016-02-15 18:42:07 +00002135 TmpInst.addOperand(MCOperand::createExpr(
Colin LeMahieuc7b21242016-02-15 18:47:55 +00002136 HexagonMCExpr::create(MCConstantExpr::create(-1, Context), Context)));
Colin LeMahieu7cd08922015-11-09 04:07:48 +00002137 TmpInst.addOperand(Rs);
2138 Inst = TmpInst;
2139 break;
2140 }
2141 } // switch
2142
2143 return Match_Success;
2144}
Krzysztof Parzyszekadf02ae2016-04-21 19:49:53 +00002145
2146
2147unsigned HexagonAsmParser::matchRegister(StringRef Name) {
2148 if (unsigned Reg = MatchRegisterName(Name))
2149 return Reg;
2150 return MatchRegisterAltName(Name);
2151}