LLVM 24.0.0git
VEAsmParser.cpp
Go to the documentation of this file.
1//===-- VEAsmParser.cpp - Parse VE assembly to MCInst instructions --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
12#include "VE.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/Twine.h"
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCInst.h"
19#include "llvm/MC/MCInstrInfo.h"
25#include "llvm/MC/MCStreamer.h"
27#include "llvm/MC/MCSymbol.h"
31#include <memory>
32
33using namespace llvm;
34
35#define DEBUG_TYPE "ve-asmparser"
36
37namespace {
38
39class VEOperand;
40
41class VEAsmParser : public MCTargetAsmParser {
42 MCAsmParser &Parser;
43
44 /// @name Auto-generated Match Functions
45 /// {
46
47#define GET_ASSEMBLER_HEADER
48#include "VEGenAsmMatcher.inc"
49
50 /// }
51
52 // public interface of the MCTargetAsmParser.
53 bool matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
56 bool MatchingInlineAsm) override;
57 bool parseRegister(MCRegister &Reg, SMLoc &StartLoc, SMLoc &EndLoc) override;
58 MCRegister parseRegisterName(MCRegister (*matchFn)(StringRef));
59 ParseStatus tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
60 SMLoc &EndLoc) override;
61 bool parseInstruction(ParseInstructionInfo &Info, StringRef Name,
62 SMLoc NameLoc, OperandVector &Operands) override;
63 ParseStatus parseDirective(AsmToken DirectiveID) override;
64
65 unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
66 unsigned Kind) override;
67
68 // Custom parse functions for VE specific operands.
69 ParseStatus parseMEMOperand(OperandVector &Operands);
70 ParseStatus parseMEMAsOperand(OperandVector &Operands);
71 ParseStatus parseCCOpOperand(OperandVector &Operands);
72 ParseStatus parseRDOpOperand(OperandVector &Operands);
73 ParseStatus parseMImmOperand(OperandVector &Operands);
74 ParseStatus parseOperand(OperandVector &Operands, StringRef Name);
75 ParseStatus parseVEAsmOperand(std::unique_ptr<VEOperand> &Operand);
76
77 // Helper function to parse expression with a symbol.
78 const MCExpr *extractSpecifier(const MCExpr *E, VE::Specifier &Variant);
79 bool parseExpression(const MCExpr *&EVal);
80
81 // Split the mnemonic stripping conditional code and quantifiers
82 StringRef splitMnemonic(StringRef Name, SMLoc NameLoc,
84
85 bool parseLiteralValues(unsigned Size, SMLoc L);
86
87public:
88 VEAsmParser(const MCSubtargetInfo &sti, MCAsmParser &parser,
89 const MCInstrInfo &MII)
90 : MCTargetAsmParser(sti, MII), Parser(parser) {
91 // Initialize the set of available features.
92 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
93 }
94};
95
96} // end anonymous namespace
97
98static const MCPhysReg I32Regs[64] = {
99 VE::SW0, VE::SW1, VE::SW2, VE::SW3, VE::SW4, VE::SW5, VE::SW6,
100 VE::SW7, VE::SW8, VE::SW9, VE::SW10, VE::SW11, VE::SW12, VE::SW13,
101 VE::SW14, VE::SW15, VE::SW16, VE::SW17, VE::SW18, VE::SW19, VE::SW20,
102 VE::SW21, VE::SW22, VE::SW23, VE::SW24, VE::SW25, VE::SW26, VE::SW27,
103 VE::SW28, VE::SW29, VE::SW30, VE::SW31, VE::SW32, VE::SW33, VE::SW34,
104 VE::SW35, VE::SW36, VE::SW37, VE::SW38, VE::SW39, VE::SW40, VE::SW41,
105 VE::SW42, VE::SW43, VE::SW44, VE::SW45, VE::SW46, VE::SW47, VE::SW48,
106 VE::SW49, VE::SW50, VE::SW51, VE::SW52, VE::SW53, VE::SW54, VE::SW55,
107 VE::SW56, VE::SW57, VE::SW58, VE::SW59, VE::SW60, VE::SW61, VE::SW62,
108 VE::SW63};
109
110static const MCPhysReg F32Regs[64] = {
111 VE::SF0, VE::SF1, VE::SF2, VE::SF3, VE::SF4, VE::SF5, VE::SF6,
112 VE::SF7, VE::SF8, VE::SF9, VE::SF10, VE::SF11, VE::SF12, VE::SF13,
113 VE::SF14, VE::SF15, VE::SF16, VE::SF17, VE::SF18, VE::SF19, VE::SF20,
114 VE::SF21, VE::SF22, VE::SF23, VE::SF24, VE::SF25, VE::SF26, VE::SF27,
115 VE::SF28, VE::SF29, VE::SF30, VE::SF31, VE::SF32, VE::SF33, VE::SF34,
116 VE::SF35, VE::SF36, VE::SF37, VE::SF38, VE::SF39, VE::SF40, VE::SF41,
117 VE::SF42, VE::SF43, VE::SF44, VE::SF45, VE::SF46, VE::SF47, VE::SF48,
118 VE::SF49, VE::SF50, VE::SF51, VE::SF52, VE::SF53, VE::SF54, VE::SF55,
119 VE::SF56, VE::SF57, VE::SF58, VE::SF59, VE::SF60, VE::SF61, VE::SF62,
120 VE::SF63};
121
122static const MCPhysReg F128Regs[32] = {
123 VE::Q0, VE::Q1, VE::Q2, VE::Q3, VE::Q4, VE::Q5, VE::Q6, VE::Q7,
124 VE::Q8, VE::Q9, VE::Q10, VE::Q11, VE::Q12, VE::Q13, VE::Q14, VE::Q15,
125 VE::Q16, VE::Q17, VE::Q18, VE::Q19, VE::Q20, VE::Q21, VE::Q22, VE::Q23,
126 VE::Q24, VE::Q25, VE::Q26, VE::Q27, VE::Q28, VE::Q29, VE::Q30, VE::Q31};
127
128static const MCPhysReg VM512Regs[8] = {VE::VMP0, VE::VMP1, VE::VMP2, VE::VMP3,
129 VE::VMP4, VE::VMP5, VE::VMP6, VE::VMP7};
130
131static const MCPhysReg MISCRegs[31] = {
132 VE::USRCC, VE::PSW, VE::SAR, VE::NoRegister,
133 VE::NoRegister, VE::NoRegister, VE::NoRegister, VE::PMMR,
134 VE::PMCR0, VE::PMCR1, VE::PMCR2, VE::PMCR3,
135 VE::NoRegister, VE::NoRegister, VE::NoRegister, VE::NoRegister,
136 VE::PMC0, VE::PMC1, VE::PMC2, VE::PMC3,
137 VE::PMC4, VE::PMC5, VE::PMC6, VE::PMC7,
138 VE::PMC8, VE::PMC9, VE::PMC10, VE::PMC11,
139 VE::PMC12, VE::PMC13, VE::PMC14};
140
141namespace {
142
143/// VEOperand - Instances of this class represent a parsed VE machine
144/// instruction.
145class VEOperand : public MCParsedAsmOperand {
146private:
147 enum KindTy {
148 k_Token,
149 k_Register,
150 k_Immediate,
151 // SX-Aurora ASX form is disp(index, base).
152 k_MemoryRegRegImm, // base=reg, index=reg, disp=imm
153 k_MemoryRegImmImm, // base=reg, index=imm, disp=imm
154 k_MemoryZeroRegImm, // base=0, index=reg, disp=imm
155 k_MemoryZeroImmImm, // base=0, index=imm, disp=imm
156 // SX-Aurora AS form is disp(base).
157 k_MemoryRegImm, // base=reg, disp=imm
158 k_MemoryZeroImm, // base=0, disp=imm
159 // Other special cases for Aurora VE
160 k_CCOp, // condition code
161 k_RDOp, // rounding mode
162 k_MImmOp, // Special immediate value of sequential bit stream of 0 or 1.
163 } Kind;
164
165 SMLoc StartLoc, EndLoc;
166
167 struct Token {
168 const char *Data;
169 unsigned Length;
170 };
171
172 struct RegOp {
173 MCRegister Reg;
174 };
175
176 struct ImmOp {
177 const MCExpr *Val;
178 };
179
180 struct MemOp {
181 MCRegister Base;
182 MCRegister IndexReg;
183 const MCExpr *Index;
184 const MCExpr *Offset;
185 };
186
187 struct CCOp {
188 unsigned CCVal;
189 };
190
191 struct RDOp {
192 unsigned RDVal;
193 };
194
195 struct MImmOp {
196 const MCExpr *Val;
197 bool M0Flag;
198 };
199
200 union {
201 struct Token Tok;
202 struct RegOp Reg;
203 struct ImmOp Imm;
204 struct MemOp Mem;
205 struct CCOp CC;
206 struct RDOp RD;
207 struct MImmOp MImm;
208 };
209
210public:
211 VEOperand(KindTy K) : Kind(K) {}
212
213 bool isToken() const override { return Kind == k_Token; }
214 bool isReg() const override { return Kind == k_Register; }
215 bool isImm() const override { return Kind == k_Immediate; }
216 bool isMem() const override {
217 return isMEMrri() || isMEMrii() || isMEMzri() || isMEMzii() || isMEMri() ||
218 isMEMzi();
219 }
220 bool isMEMrri() const { return Kind == k_MemoryRegRegImm; }
221 bool isMEMrii() const { return Kind == k_MemoryRegImmImm; }
222 bool isMEMzri() const { return Kind == k_MemoryZeroRegImm; }
223 bool isMEMzii() const { return Kind == k_MemoryZeroImmImm; }
224 bool isMEMri() const { return Kind == k_MemoryRegImm; }
225 bool isMEMzi() const { return Kind == k_MemoryZeroImm; }
226 bool isCCOp() const { return Kind == k_CCOp; }
227 bool isRDOp() const { return Kind == k_RDOp; }
228 bool isZero() {
229 if (!isImm())
230 return false;
231
232 // Constant case
233 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
234 int64_t Value = ConstExpr->getValue();
235 return Value == 0;
236 }
237 return false;
238 }
239 bool isUImm0to2() {
240 if (!isImm())
241 return false;
242
243 // Constant case
244 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
245 int64_t Value = ConstExpr->getValue();
246 return Value >= 0 && Value < 3;
247 }
248 return false;
249 }
250 bool isUImm1() {
251 if (!isImm())
252 return false;
253
254 // Constant case
255 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
256 int64_t Value = ConstExpr->getValue();
257 return isUInt<1>(Value);
258 }
259 return false;
260 }
261 bool isUImm2() {
262 if (!isImm())
263 return false;
264
265 // Constant case
266 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
267 int64_t Value = ConstExpr->getValue();
268 return isUInt<2>(Value);
269 }
270 return false;
271 }
272 bool isUImm3() {
273 if (!isImm())
274 return false;
275
276 // Constant case
277 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
278 int64_t Value = ConstExpr->getValue();
279 return isUInt<3>(Value);
280 }
281 return false;
282 }
283 bool isUImm4() {
284 if (!isImm())
285 return false;
286
287 // Constant case
288 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
289 int64_t Value = ConstExpr->getValue();
290 return isUInt<4>(Value);
291 }
292 return false;
293 }
294 bool isUImm6() {
295 if (!isImm())
296 return false;
297
298 // Constant case
299 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
300 int64_t Value = ConstExpr->getValue();
301 return isUInt<6>(Value);
302 }
303 return false;
304 }
305 bool isUImm7() {
306 if (!isImm())
307 return false;
308
309 // Constant case
310 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
311 int64_t Value = ConstExpr->getValue();
312 return isUInt<7>(Value);
313 }
314 return false;
315 }
316 bool isSImm7() {
317 if (!isImm())
318 return false;
319
320 // Constant case
321 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
322 int64_t Value = ConstExpr->getValue();
323 return isInt<7>(Value);
324 }
325 return false;
326 }
327 bool isMImm() const {
328 if (Kind != k_MImmOp)
329 return false;
330
331 // Constant case
332 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(MImm.Val)) {
333 int64_t Value = ConstExpr->getValue();
334 return isUInt<6>(Value);
335 }
336 return false;
337 }
338
339 StringRef getToken() const {
340 assert(Kind == k_Token && "Invalid access!");
341 return StringRef(Tok.Data, Tok.Length);
342 }
343
344 MCRegister getReg() const override {
345 assert((Kind == k_Register) && "Invalid access!");
346 return Reg.Reg;
347 }
348
349 const MCExpr *getImm() const {
350 assert((Kind == k_Immediate) && "Invalid access!");
351 return Imm.Val;
352 }
353
354 MCRegister getMemBase() const {
355 assert((Kind == k_MemoryRegRegImm || Kind == k_MemoryRegImmImm ||
356 Kind == k_MemoryRegImm) &&
357 "Invalid access!");
358 return Mem.Base;
359 }
360
361 MCRegister getMemIndexReg() const {
362 assert((Kind == k_MemoryRegRegImm || Kind == k_MemoryZeroRegImm) &&
363 "Invalid access!");
364 return Mem.IndexReg;
365 }
366
367 const MCExpr *getMemIndex() const {
368 assert((Kind == k_MemoryRegImmImm || Kind == k_MemoryZeroImmImm) &&
369 "Invalid access!");
370 return Mem.Index;
371 }
372
373 const MCExpr *getMemOffset() const {
374 assert((Kind == k_MemoryRegRegImm || Kind == k_MemoryRegImmImm ||
375 Kind == k_MemoryZeroImmImm || Kind == k_MemoryZeroRegImm ||
376 Kind == k_MemoryRegImm || Kind == k_MemoryZeroImm) &&
377 "Invalid access!");
378 return Mem.Offset;
379 }
380
381 void setMemOffset(const MCExpr *off) {
382 assert((Kind == k_MemoryRegRegImm || Kind == k_MemoryRegImmImm ||
383 Kind == k_MemoryZeroImmImm || Kind == k_MemoryZeroRegImm ||
384 Kind == k_MemoryRegImm || Kind == k_MemoryZeroImm) &&
385 "Invalid access!");
386 Mem.Offset = off;
387 }
388
389 unsigned getCCVal() const {
390 assert((Kind == k_CCOp) && "Invalid access!");
391 return CC.CCVal;
392 }
393
394 unsigned getRDVal() const {
395 assert((Kind == k_RDOp) && "Invalid access!");
396 return RD.RDVal;
397 }
398
399 const MCExpr *getMImmVal() const {
400 assert((Kind == k_MImmOp) && "Invalid access!");
401 return MImm.Val;
402 }
403 bool getM0Flag() const {
404 assert((Kind == k_MImmOp) && "Invalid access!");
405 return MImm.M0Flag;
406 }
407
408 /// getStartLoc - Get the location of the first token of this operand.
409 SMLoc getStartLoc() const override { return StartLoc; }
410 /// getEndLoc - Get the location of the last token of this operand.
411 SMLoc getEndLoc() const override { return EndLoc; }
412
413 void print(raw_ostream &OS, const MCAsmInfo &MAI) const override {
414 switch (Kind) {
415 case k_Token:
416 OS << "Token: " << getToken() << "\n";
417 break;
418 case k_Register:
419 OS << "Reg: #" << getReg().id() << "\n";
420 break;
421 case k_Immediate:
422 OS << "Imm: " << getImm() << "\n";
423 break;
424 case k_MemoryRegRegImm:
425 assert(getMemOffset() != nullptr);
426 OS << "Mem: #" << getMemBase().id() << "+#" << getMemIndexReg().id()
427 << "+";
428 MAI.printExpr(OS, *getMemOffset());
429 OS << "\n";
430 break;
431 case k_MemoryRegImmImm:
432 assert(getMemIndex() != nullptr && getMemOffset() != nullptr);
433 OS << "Mem: #" << getMemBase().id() << "+";
434 MAI.printExpr(OS, *getMemIndex());
435 OS << "+";
436 MAI.printExpr(OS, *getMemOffset());
437 OS << "\n";
438 break;
439 case k_MemoryZeroRegImm:
440 assert(getMemOffset() != nullptr);
441 OS << "Mem: 0+#" << getMemIndexReg().id() << "+";
442 MAI.printExpr(OS, *getMemOffset());
443 OS << "\n";
444 break;
445 case k_MemoryZeroImmImm:
446 assert(getMemIndex() != nullptr && getMemOffset() != nullptr);
447 OS << "Mem: 0+";
448 MAI.printExpr(OS, *getMemIndex());
449 OS << "+";
450 MAI.printExpr(OS, *getMemOffset());
451 OS << "\n";
452 break;
453 case k_MemoryRegImm:
454 assert(getMemOffset() != nullptr);
455 OS << "Mem: #" << getMemBase().id() << "+";
456 MAI.printExpr(OS, *getMemOffset());
457 OS << "\n";
458 break;
459 case k_MemoryZeroImm:
460 assert(getMemOffset() != nullptr);
461 OS << "Mem: 0+";
462 MAI.printExpr(OS, *getMemOffset());
463 OS << "\n";
464 break;
465 case k_CCOp:
466 OS << "CCOp: " << getCCVal() << "\n";
467 break;
468 case k_RDOp:
469 OS << "RDOp: " << getRDVal() << "\n";
470 break;
471 case k_MImmOp:
472 OS << "MImm: (" << getMImmVal() << (getM0Flag() ? ")0" : ")1") << "\n";
473 break;
474 }
475 }
476
477 void addRegOperands(MCInst &Inst, unsigned N) const {
478 assert(N == 1 && "Invalid number of operands!");
480 }
481
482 void addImmOperands(MCInst &Inst, unsigned N) const {
483 assert(N == 1 && "Invalid number of operands!");
484 const MCExpr *Expr = getImm();
485 addExpr(Inst, Expr);
486 }
487
488 void addZeroOperands(MCInst &Inst, unsigned N) const {
489 addImmOperands(Inst, N);
490 }
491
492 void addUImm0to2Operands(MCInst &Inst, unsigned N) const {
493 addImmOperands(Inst, N);
494 }
495
496 void addUImm1Operands(MCInst &Inst, unsigned N) const {
497 addImmOperands(Inst, N);
498 }
499
500 void addUImm2Operands(MCInst &Inst, unsigned N) const {
501 addImmOperands(Inst, N);
502 }
503
504 void addUImm3Operands(MCInst &Inst, unsigned N) const {
505 addImmOperands(Inst, N);
506 }
507
508 void addUImm4Operands(MCInst &Inst, unsigned N) const {
509 addImmOperands(Inst, N);
510 }
511
512 void addUImm6Operands(MCInst &Inst, unsigned N) const {
513 addImmOperands(Inst, N);
514 }
515
516 void addUImm7Operands(MCInst &Inst, unsigned N) const {
517 addImmOperands(Inst, N);
518 }
519
520 void addSImm7Operands(MCInst &Inst, unsigned N) const {
521 addImmOperands(Inst, N);
522 }
523
524 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
525 // Add as immediate when possible. Null MCExpr = 0.
526 if (!Expr)
528 else if (const auto *CE = dyn_cast<MCConstantExpr>(Expr))
529 Inst.addOperand(MCOperand::createImm(CE->getValue()));
530 else
532 }
533
534 void addMEMrriOperands(MCInst &Inst, unsigned N) const {
535 assert(N == 3 && "Invalid number of operands!");
536
537 Inst.addOperand(MCOperand::createReg(getMemBase()));
538 Inst.addOperand(MCOperand::createReg(getMemIndexReg()));
539 addExpr(Inst, getMemOffset());
540 }
541
542 void addMEMriiOperands(MCInst &Inst, unsigned N) const {
543 assert(N == 3 && "Invalid number of operands!");
544
545 Inst.addOperand(MCOperand::createReg(getMemBase()));
546 addExpr(Inst, getMemIndex());
547 addExpr(Inst, getMemOffset());
548 }
549
550 void addMEMzriOperands(MCInst &Inst, unsigned N) const {
551 assert(N == 3 && "Invalid number of operands!");
552
554 Inst.addOperand(MCOperand::createReg(getMemIndexReg()));
555 addExpr(Inst, getMemOffset());
556 }
557
558 void addMEMziiOperands(MCInst &Inst, unsigned N) const {
559 assert(N == 3 && "Invalid number of operands!");
560
562 addExpr(Inst, getMemIndex());
563 addExpr(Inst, getMemOffset());
564 }
565
566 void addMEMriOperands(MCInst &Inst, unsigned N) const {
567 assert(N == 2 && "Invalid number of operands!");
568
569 Inst.addOperand(MCOperand::createReg(getMemBase()));
570 addExpr(Inst, getMemOffset());
571 }
572
573 void addMEMziOperands(MCInst &Inst, unsigned N) const {
574 assert(N == 2 && "Invalid number of operands!");
575
577 addExpr(Inst, getMemOffset());
578 }
579
580 void addCCOpOperands(MCInst &Inst, unsigned N) const {
581 assert(N == 1 && "Invalid number of operands!");
582
583 Inst.addOperand(MCOperand::createImm(getCCVal()));
584 }
585
586 void addRDOpOperands(MCInst &Inst, unsigned N) const {
587 assert(N == 1 && "Invalid number of operands!");
588
589 Inst.addOperand(MCOperand::createImm(getRDVal()));
590 }
591
592 void addMImmOperands(MCInst &Inst, unsigned N) const {
593 assert(N == 1 && "Invalid number of operands!");
594 const auto *ConstExpr = dyn_cast<MCConstantExpr>(getMImmVal());
595 assert(ConstExpr && "Null operands!");
596 int64_t Value = ConstExpr->getValue();
597 if (getM0Flag())
598 Value += 64;
600 }
601
602 static std::unique_ptr<VEOperand> CreateToken(StringRef Str, SMLoc S) {
603 auto Op = std::make_unique<VEOperand>(k_Token);
604 Op->Tok.Data = Str.data();
605 Op->Tok.Length = Str.size();
606 Op->StartLoc = S;
607 Op->EndLoc = S;
608 return Op;
609 }
610
611 static std::unique_ptr<VEOperand> CreateReg(MCRegister Reg, SMLoc S,
612 SMLoc E) {
613 auto Op = std::make_unique<VEOperand>(k_Register);
614 Op->Reg.Reg = Reg;
615 Op->StartLoc = S;
616 Op->EndLoc = E;
617 return Op;
618 }
619
620 static std::unique_ptr<VEOperand> CreateImm(const MCExpr *Val, SMLoc S,
621 SMLoc E) {
622 auto Op = std::make_unique<VEOperand>(k_Immediate);
623 Op->Imm.Val = Val;
624 Op->StartLoc = S;
625 Op->EndLoc = E;
626 return Op;
627 }
628
629 static std::unique_ptr<VEOperand> CreateCCOp(unsigned CCVal, SMLoc S,
630 SMLoc E) {
631 auto Op = std::make_unique<VEOperand>(k_CCOp);
632 Op->CC.CCVal = CCVal;
633 Op->StartLoc = S;
634 Op->EndLoc = E;
635 return Op;
636 }
637
638 static std::unique_ptr<VEOperand> CreateRDOp(unsigned RDVal, SMLoc S,
639 SMLoc E) {
640 auto Op = std::make_unique<VEOperand>(k_RDOp);
641 Op->RD.RDVal = RDVal;
642 Op->StartLoc = S;
643 Op->EndLoc = E;
644 return Op;
645 }
646
647 static std::unique_ptr<VEOperand> CreateMImm(const MCExpr *Val, bool Flag,
648 SMLoc S, SMLoc E) {
649 auto Op = std::make_unique<VEOperand>(k_MImmOp);
650 Op->MImm.Val = Val;
651 Op->MImm.M0Flag = Flag;
652 Op->StartLoc = S;
653 Op->EndLoc = E;
654 return Op;
655 }
656
657 static bool MorphToI32Reg(VEOperand &Op) {
658 MCRegister Reg = Op.getReg();
659 unsigned regIdx = Reg - VE::SX0;
660 if (regIdx > 63)
661 return false;
662 Op.Reg.Reg = I32Regs[regIdx];
663 return true;
664 }
665
666 static bool MorphToF32Reg(VEOperand &Op) {
667 MCRegister Reg = Op.getReg();
668 unsigned regIdx = Reg - VE::SX0;
669 if (regIdx > 63)
670 return false;
671 Op.Reg.Reg = F32Regs[regIdx];
672 return true;
673 }
674
675 static bool MorphToF128Reg(VEOperand &Op) {
676 MCRegister Reg = Op.getReg();
677 unsigned regIdx = Reg - VE::SX0;
678 if (regIdx % 2 || regIdx > 63)
679 return false;
680 Op.Reg.Reg = F128Regs[regIdx / 2];
681 return true;
682 }
683
684 static bool MorphToVM512Reg(VEOperand &Op) {
685 MCRegister Reg = Op.getReg();
686 unsigned regIdx = Reg - VE::VM0;
687 if (regIdx % 2 || regIdx > 15)
688 return false;
689 Op.Reg.Reg = VM512Regs[regIdx / 2];
690 return true;
691 }
692
693 static bool MorphToMISCReg(VEOperand &Op) {
694 const auto *ConstExpr = dyn_cast<MCConstantExpr>(Op.getImm());
695 if (!ConstExpr)
696 return false;
697 unsigned regIdx = ConstExpr->getValue();
698 if (regIdx >= std::size(MISCRegs) || MISCRegs[regIdx] == VE::NoRegister)
699 return false;
700 Op.Kind = k_Register;
701 Op.Reg.Reg = MISCRegs[regIdx];
702 return true;
703 }
704
705 static std::unique_ptr<VEOperand>
706 MorphToMEMri(MCRegister Base, std::unique_ptr<VEOperand> Op) {
707 const MCExpr *Imm = Op->getImm();
708 Op->Kind = k_MemoryRegImm;
709 Op->Mem.Base = Base;
710 Op->Mem.IndexReg = MCRegister();
711 Op->Mem.Index = nullptr;
712 Op->Mem.Offset = Imm;
713 return Op;
714 }
715
716 static std::unique_ptr<VEOperand>
717 MorphToMEMzi(std::unique_ptr<VEOperand> Op) {
718 const MCExpr *Imm = Op->getImm();
719 Op->Kind = k_MemoryZeroImm;
720 Op->Mem.Base = MCRegister();
721 Op->Mem.IndexReg = MCRegister();
722 Op->Mem.Index = nullptr;
723 Op->Mem.Offset = Imm;
724 return Op;
725 }
726
727 static std::unique_ptr<VEOperand>
728 MorphToMEMrri(MCRegister Base, MCRegister Index,
729 std::unique_ptr<VEOperand> Op) {
730 const MCExpr *Imm = Op->getImm();
731 Op->Kind = k_MemoryRegRegImm;
732 Op->Mem.Base = Base;
733 Op->Mem.IndexReg = Index;
734 Op->Mem.Index = nullptr;
735 Op->Mem.Offset = Imm;
736 return Op;
737 }
738
739 static std::unique_ptr<VEOperand>
740 MorphToMEMrii(MCRegister Base, const MCExpr *Index,
741 std::unique_ptr<VEOperand> Op) {
742 const MCExpr *Imm = Op->getImm();
743 Op->Kind = k_MemoryRegImmImm;
744 Op->Mem.Base = Base;
745 Op->Mem.IndexReg = MCRegister();
746 Op->Mem.Index = Index;
747 Op->Mem.Offset = Imm;
748 return Op;
749 }
750
751 static std::unique_ptr<VEOperand>
752 MorphToMEMzri(MCRegister Index, std::unique_ptr<VEOperand> Op) {
753 const MCExpr *Imm = Op->getImm();
754 Op->Kind = k_MemoryZeroRegImm;
755 Op->Mem.Base = MCRegister();
756 Op->Mem.IndexReg = Index;
757 Op->Mem.Index = nullptr;
758 Op->Mem.Offset = Imm;
759 return Op;
760 }
761
762 static std::unique_ptr<VEOperand>
763 MorphToMEMzii(const MCExpr *Index, std::unique_ptr<VEOperand> Op) {
764 const MCExpr *Imm = Op->getImm();
765 Op->Kind = k_MemoryZeroImmImm;
766 Op->Mem.Base = MCRegister();
767 Op->Mem.IndexReg = MCRegister();
768 Op->Mem.Index = Index;
769 Op->Mem.Offset = Imm;
770 return Op;
771 }
772};
773
774} // end anonymous namespace
775
776bool VEAsmParser::matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
779 bool MatchingInlineAsm) {
780 MCInst Inst;
781 unsigned MatchResult =
782 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
783 switch (MatchResult) {
784 case Match_Success:
785 Inst.setLoc(IDLoc);
786 Out.emitInstruction(Inst, getSTI());
787 return false;
788
789 case Match_MissingFeature:
790 return Error(IDLoc,
791 "instruction requires a CPU feature not currently enabled");
792
793 case Match_InvalidOperand: {
794 SMLoc ErrorLoc = IDLoc;
795 if (ErrorInfo != ~0ULL) {
796 if (ErrorInfo >= Operands.size())
797 return Error(IDLoc, "too few operands for instruction");
798
799 ErrorLoc = ((VEOperand &)*Operands[ErrorInfo]).getStartLoc();
800 if (ErrorLoc == SMLoc())
801 ErrorLoc = IDLoc;
802 }
803
804 return Error(ErrorLoc, "invalid operand for instruction");
805 }
806 case Match_MnemonicFail:
807 return Error(IDLoc, "invalid instruction mnemonic");
808 }
809 llvm_unreachable("Implement any new match types added!");
810}
811
812bool VEAsmParser::parseRegister(MCRegister &Reg, SMLoc &StartLoc,
813 SMLoc &EndLoc) {
814 if (!tryParseRegister(Reg, StartLoc, EndLoc).isSuccess())
815 return Error(StartLoc, "invalid register name");
816 return false;
817}
818
819/// Parses a register name using a given matching function.
820/// Checks for lowercase or uppercase if necessary.
821MCRegister VEAsmParser::parseRegisterName(MCRegister (*matchFn)(StringRef)) {
822 StringRef Name = Parser.getTok().getString();
823
824 MCRegister RegNum = matchFn(Name);
825
826 // GCC supports case insensitive register names. All of the VE registers
827 // are all lower case.
828 if (!RegNum) {
829 RegNum = matchFn(Name.lower());
830 }
831
832 return RegNum;
833}
834
835/// Maps from the set of all register names to a register number.
836/// \note Generated by TableGen.
838
839/// Maps from the set of all alternative registernames to a register number.
840/// \note Generated by TableGen.
842
843ParseStatus VEAsmParser::tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
844 SMLoc &EndLoc) {
845 const AsmToken Tok = Parser.getTok();
846 StartLoc = Tok.getLoc();
847 EndLoc = Tok.getEndLoc();
848 Reg = VE::NoRegister;
849 if (getLexer().getKind() != AsmToken::Percent)
851 Parser.Lex();
852
853 Reg = parseRegisterName(&MatchRegisterName);
854 if (Reg == VE::NoRegister)
855 Reg = parseRegisterName(&MatchRegisterAltName);
856
857 if (Reg != VE::NoRegister) {
858 Parser.Lex();
860 }
861
862 getLexer().UnLex(Tok);
864}
865
866static StringRef parseCC(StringRef Name, unsigned Prefix, unsigned Suffix,
867 bool IntegerCC, bool OmitCC, SMLoc NameLoc,
869 // Parse instructions with a conditional code. For example, 'bne' is
870 // converted into two operands 'b' and 'ne'.
871 StringRef Cond = Name.slice(Prefix, Suffix);
872 VECC::CondCode CondCode =
874
875 // If OmitCC is enabled, CC_AT and CC_AF is treated as a part of mnemonic.
876 if (CondCode != VECC::UNKNOWN &&
877 (!OmitCC || (CondCode != VECC::CC_AT && CondCode != VECC::CC_AF))) {
878 StringRef SuffixStr = Name.substr(Suffix);
879 // Push "b".
880 Name = Name.slice(0, Prefix);
881 Operands->push_back(VEOperand::CreateToken(Name, NameLoc));
882 // Push $cond part.
883 SMLoc CondLoc = SMLoc::getFromPointer(NameLoc.getPointer() + Prefix);
884 SMLoc SuffixLoc = SMLoc::getFromPointer(NameLoc.getPointer() + Suffix);
885 Operands->push_back(VEOperand::CreateCCOp(CondCode, CondLoc, SuffixLoc));
886 // push suffix like ".l.t"
887 if (!SuffixStr.empty())
888 Operands->push_back(VEOperand::CreateToken(SuffixStr, SuffixLoc));
889 } else {
890 Operands->push_back(VEOperand::CreateToken(Name, NameLoc));
891 }
892 return Name;
893}
894
895static StringRef parseRD(StringRef Name, unsigned Prefix, SMLoc NameLoc,
897 // Parse instructions with a conditional code. For example, 'cvt.w.d.sx.rz'
898 // is converted into two operands 'cvt.w.d.sx' and '.rz'.
899 StringRef RD = Name.substr(Prefix);
901
903 Name = Name.slice(0, Prefix);
904 // push 1st like `cvt.w.d.sx`
905 Operands->push_back(VEOperand::CreateToken(Name, NameLoc));
906 SMLoc SuffixLoc =
907 SMLoc::getFromPointer(NameLoc.getPointer() + (RD.data() - Name.data()));
909 SMLoc::getFromPointer(NameLoc.getPointer() + (RD.end() - Name.data()));
910 // push $round if it has rounding mode
911 Operands->push_back(
912 VEOperand::CreateRDOp(RoundingMode, SuffixLoc, SuffixEnd));
913 } else {
914 Operands->push_back(VEOperand::CreateToken(Name, NameLoc));
915 }
916 return Name;
917}
918
919// Split the mnemonic into ASM operand, conditional code and instruction
920// qualifier (half-word, byte).
921StringRef VEAsmParser::splitMnemonic(StringRef Name, SMLoc NameLoc,
923 // Create the leading tokens for the mnemonic
924 StringRef Mnemonic = Name;
925
926 if (Name[0] == 'b') {
927 // Match b?? or br??.
928 size_t Start = 1;
929 size_t Next = Name.find('.');
930 // Adjust position of CondCode.
931 if (Name.size() > 1 && Name[1] == 'r')
932 Start = 2;
933 // Check suffix.
934 bool ICC = true;
935 if (Next + 1 < Name.size() &&
936 (Name[Next + 1] == 'd' || Name[Next + 1] == 's'))
937 ICC = false;
938 Mnemonic = parseCC(Name, Start, Next, ICC, true, NameLoc, Operands);
939 } else if (Name.starts_with("cmov.l.") || Name.starts_with("cmov.w.") ||
940 Name.starts_with("cmov.d.") || Name.starts_with("cmov.s.")) {
941 bool ICC = Name[5] == 'l' || Name[5] == 'w';
942 Mnemonic = parseCC(Name, 7, Name.size(), ICC, false, NameLoc, Operands);
943 } else if (Name.starts_with("cvt.w.d.sx") || Name.starts_with("cvt.w.d.zx") ||
944 Name.starts_with("cvt.w.s.sx") || Name.starts_with("cvt.w.s.zx")) {
945 Mnemonic = parseRD(Name, 10, NameLoc, Operands);
946 } else if (Name.starts_with("cvt.l.d")) {
947 Mnemonic = parseRD(Name, 7, NameLoc, Operands);
948 } else if (Name.starts_with("vcvt.w.d.sx") ||
949 Name.starts_with("vcvt.w.d.zx") ||
950 Name.starts_with("vcvt.w.s.sx") ||
951 Name.starts_with("vcvt.w.s.zx")) {
952 Mnemonic = parseRD(Name, 11, NameLoc, Operands);
953 } else if (Name.starts_with("vcvt.l.d")) {
954 Mnemonic = parseRD(Name, 8, NameLoc, Operands);
955 } else if (Name.starts_with("pvcvt.w.s.lo") ||
956 Name.starts_with("pvcvt.w.s.up")) {
957 Mnemonic = parseRD(Name, 12, NameLoc, Operands);
958 } else if (Name.starts_with("pvcvt.w.s")) {
959 Mnemonic = parseRD(Name, 9, NameLoc, Operands);
960 } else if (Name.starts_with("vfmk.l.") || Name.starts_with("vfmk.w.") ||
961 Name.starts_with("vfmk.d.") || Name.starts_with("vfmk.s.")) {
962 bool ICC = Name[5] == 'l' || Name[5] == 'w' ? true : false;
963 Mnemonic = parseCC(Name, 7, Name.size(), ICC, true, NameLoc, Operands);
964 } else if (Name.starts_with("pvfmk.w.lo.") ||
965 Name.starts_with("pvfmk.w.up.") ||
966 Name.starts_with("pvfmk.s.lo.") ||
967 Name.starts_with("pvfmk.s.up.")) {
968 bool ICC = Name[6] == 'l' || Name[6] == 'w' ? true : false;
969 Mnemonic = parseCC(Name, 11, Name.size(), ICC, true, NameLoc, Operands);
970 } else {
971 Operands->push_back(VEOperand::CreateToken(Mnemonic, NameLoc));
972 }
973
974 return Mnemonic;
975}
976
977static void applyMnemonicAliases(StringRef &Mnemonic,
978 const FeatureBitset &Features,
979 unsigned VariantID);
980
981bool VEAsmParser::parseInstruction(ParseInstructionInfo &Info, StringRef Name,
982 SMLoc NameLoc, OperandVector &Operands) {
983 // If the target architecture uses MnemonicAlias, call it here to parse
984 // operands correctly.
985 applyMnemonicAliases(Name, getAvailableFeatures(), 0);
986
987 // Split name to first token and the rest, e.g. "bgt.l.t" to "b", "gt", and
988 // ".l.t". We treat "b" as a mnemonic, "gt" as first operand, and ".l.t"
989 // as second operand.
990 StringRef Mnemonic = splitMnemonic(Name, NameLoc, &Operands);
991
992 if (getLexer().isNot(AsmToken::EndOfStatement)) {
993 // Read the first operand.
994 if (!parseOperand(Operands, Mnemonic).isSuccess()) {
995 SMLoc Loc = getLexer().getLoc();
996 return Error(Loc, "unexpected token");
997 }
998
999 while (getLexer().is(AsmToken::Comma)) {
1000 Parser.Lex(); // Eat the comma.
1001 // Parse and remember the operand.
1002 if (!parseOperand(Operands, Mnemonic).isSuccess()) {
1003 SMLoc Loc = getLexer().getLoc();
1004 return Error(Loc, "unexpected token");
1005 }
1006 }
1007 }
1008 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1009 SMLoc Loc = getLexer().getLoc();
1010 return Error(Loc, "unexpected token");
1011 }
1012 Parser.Lex(); // Consume the EndOfStatement.
1013 return false;
1014}
1015
1016ParseStatus VEAsmParser::parseDirective(AsmToken DirectiveID) {
1017 std::string IDVal = DirectiveID.getIdentifier().lower();
1018
1019 // Defines VE specific directives. Reference is "Vector Engine Assembly
1020 // Language Reference Manual":
1021 // https://www.hpc.nec/documents/sdk/pdfs/VectorEngine-as-manual-v1.3.pdf
1022
1023 // The .word is 4 bytes long on VE.
1024 if (IDVal == ".word")
1025 return parseLiteralValues(4, DirectiveID.getLoc());
1026
1027 // The .long is 8 bytes long on VE.
1028 if (IDVal == ".long")
1029 return parseLiteralValues(8, DirectiveID.getLoc());
1030
1031 // The .llong is 8 bytes long on VE.
1032 if (IDVal == ".llong")
1033 return parseLiteralValues(8, DirectiveID.getLoc());
1034
1035 // Let the MC layer to handle other directives.
1036 return ParseStatus::NoMatch;
1037}
1038
1039/// parseLiteralValues
1040/// ::= .word expression [, expression]*
1041/// ::= .long expression [, expression]*
1042/// ::= .llong expression [, expression]*
1043bool VEAsmParser::parseLiteralValues(unsigned Size, SMLoc L) {
1044 auto parseOne = [&]() -> bool {
1045 const MCExpr *Value;
1046 if (getParser().parseExpression(Value))
1047 return true;
1048 getParser().getStreamer().emitValue(Value, Size, L);
1049 return false;
1050 };
1051 return (parseMany(parseOne));
1052}
1053
1054/// Extract \code @lo32/@hi32/etc \endcode specifier from expression.
1055/// Recursively scan the expression and check for VK_HI32/LO32/etc
1056/// symbol variants. If all symbols with modifier use the same
1057/// variant, return the corresponding VE::Specifier,
1058/// and a modified expression using the default symbol variant.
1059/// Otherwise, return NULL.
1060const MCExpr *VEAsmParser::extractSpecifier(const MCExpr *E,
1061 VE::Specifier &Variant) {
1062 MCContext &Context = getParser().getContext();
1064
1065 switch (E->getKind()) {
1066 case MCExpr::Target:
1067 case MCExpr::Constant:
1068 return nullptr;
1069 case MCExpr::Specifier:
1070 llvm_unreachable("unused by this backend");
1071
1072 case MCExpr::SymbolRef: {
1073 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1074
1075 switch (SRE->getSpecifier()) {
1076 case VE::S_None:
1077 // Use VK_REFLONG to a symbol without modifiers.
1079 break;
1080 case VE::S_HI32:
1082 break;
1083 case VE::S_LO32:
1085 break;
1086 case VE::S_PC_HI32:
1088 break;
1089 case VE::S_PC_LO32:
1091 break;
1092 case VE::S_GOT_HI32:
1094 break;
1095 case VE::S_GOT_LO32:
1097 break;
1098 case VE::S_GOTOFF_HI32:
1100 break;
1101 case VE::S_GOTOFF_LO32:
1103 break;
1104 case VE::S_PLT_HI32:
1106 break;
1107 case VE::S_PLT_LO32:
1109 break;
1110 case VE::S_TLS_GD_HI32:
1112 break;
1113 case VE::S_TLS_GD_LO32:
1115 break;
1116 case VE::S_TPOFF_HI32:
1118 break;
1119 case VE::S_TPOFF_LO32:
1121 break;
1122 default:
1123 return nullptr;
1124 }
1125
1126 return MCSymbolRefExpr::create(&SRE->getSymbol(), Context);
1127 }
1128
1129 case MCExpr::Unary: {
1130 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
1131 const MCExpr *Sub = extractSpecifier(UE->getSubExpr(), Variant);
1132 if (!Sub)
1133 return nullptr;
1134 return MCUnaryExpr::create(UE->getOpcode(), Sub, Context);
1135 }
1136
1137 case MCExpr::Binary: {
1138 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1139 VE::Specifier LHSVariant, RHSVariant;
1140 const MCExpr *LHS = extractSpecifier(BE->getLHS(), LHSVariant);
1141 const MCExpr *RHS = extractSpecifier(BE->getRHS(), RHSVariant);
1142
1143 if (!LHS && !RHS)
1144 return nullptr;
1145
1146 if (!LHS)
1147 LHS = BE->getLHS();
1148 if (!RHS)
1149 RHS = BE->getRHS();
1150
1151 if (LHSVariant == VE::S_None)
1152 Variant = RHSVariant;
1153 else if (RHSVariant == VE::S_None)
1154 Variant = LHSVariant;
1155 else if (LHSVariant == RHSVariant)
1156 Variant = LHSVariant;
1157 else
1158 return nullptr;
1159
1160 return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, Context);
1161 }
1162 }
1163
1164 llvm_unreachable("Invalid expression kind!");
1165}
1166
1167/// This differs from the default "parseExpression" in that it handles
1168/// relocation specifiers.
1169bool VEAsmParser::parseExpression(const MCExpr *&EVal) {
1170 // Handle \code symbol @lo32/@hi32/etc \endcode.
1171 if (getParser().parseExpression(EVal))
1172 return true;
1173
1174 // Convert MCSymbolRefExpr with specifier to MCSpecifierExpr.
1176 const MCExpr *E = extractSpecifier(EVal, Specifier);
1177 if (E)
1178 EVal = MCSpecifierExpr::create(E, Specifier, getParser().getContext());
1179
1180 return false;
1181}
1182
1183ParseStatus VEAsmParser::parseMEMOperand(OperandVector &Operands) {
1184 LLVM_DEBUG(dbgs() << "parseMEMOperand\n");
1185 const AsmToken &Tok = Parser.getTok();
1186 SMLoc S = Tok.getLoc();
1187 SMLoc E = Tok.getEndLoc();
1188 // Parse ASX format
1189 // disp
1190 // disp(, base)
1191 // disp(index)
1192 // disp(index, base)
1193 // (, base)
1194 // (index)
1195 // (index, base)
1196
1197 std::unique_ptr<VEOperand> Offset;
1198 switch (getLexer().getKind()) {
1199 default:
1200 return ParseStatus::NoMatch;
1201
1202 case AsmToken::Minus:
1203 case AsmToken::Integer:
1204 case AsmToken::Dot:
1205 case AsmToken::Identifier: {
1206 const MCExpr *EVal;
1207 if (!parseExpression(EVal))
1208 Offset = VEOperand::CreateImm(EVal, S, E);
1209 else
1210 return ParseStatus::NoMatch;
1211 break;
1212 }
1213
1214 case AsmToken::LParen:
1215 // empty disp (= 0)
1216 Offset =
1217 VEOperand::CreateImm(MCConstantExpr::create(0, getContext()), S, E);
1218 break;
1219 }
1220
1221 switch (getLexer().getKind()) {
1222 default:
1223 return ParseStatus::Failure;
1224
1226 Operands.push_back(VEOperand::MorphToMEMzii(
1227 MCConstantExpr::create(0, getContext()), std::move(Offset)));
1228 return ParseStatus::Success;
1229
1230 case AsmToken::LParen:
1231 Parser.Lex(); // Eat the (
1232 break;
1233 }
1234
1235 const MCExpr *IndexValue = nullptr;
1236 MCRegister IndexReg;
1237
1238 switch (getLexer().getKind()) {
1239 default:
1240 if (parseRegister(IndexReg, S, E))
1241 return ParseStatus::Failure;
1242 break;
1243
1244 case AsmToken::Minus:
1245 case AsmToken::Integer:
1246 case AsmToken::Dot:
1247 if (getParser().parseExpression(IndexValue, E))
1248 return ParseStatus::Failure;
1249 break;
1250
1251 case AsmToken::Comma:
1252 // empty index
1253 IndexValue = MCConstantExpr::create(0, getContext());
1254 break;
1255 }
1256
1257 switch (getLexer().getKind()) {
1258 default:
1259 return ParseStatus::Failure;
1260
1261 case AsmToken::RParen:
1262 Parser.Lex(); // Eat the )
1263 Operands.push_back(
1264 IndexValue ? VEOperand::MorphToMEMzii(IndexValue, std::move(Offset))
1265 : VEOperand::MorphToMEMzri(IndexReg, std::move(Offset)));
1266 return ParseStatus::Success;
1267
1268 case AsmToken::Comma:
1269 Parser.Lex(); // Eat the ,
1270 break;
1271 }
1272
1273 MCRegister BaseReg;
1274 if (parseRegister(BaseReg, S, E))
1275 return ParseStatus::Failure;
1276
1277 if (!Parser.getTok().is(AsmToken::RParen))
1278 return ParseStatus::Failure;
1279
1280 Parser.Lex(); // Eat the )
1281 Operands.push_back(
1282 IndexValue
1283 ? VEOperand::MorphToMEMrii(BaseReg, IndexValue, std::move(Offset))
1284 : VEOperand::MorphToMEMrri(BaseReg, IndexReg, std::move(Offset)));
1285
1286 return ParseStatus::Success;
1287}
1288
1289ParseStatus VEAsmParser::parseMEMAsOperand(OperandVector &Operands) {
1290 LLVM_DEBUG(dbgs() << "parseMEMAsOperand\n");
1291 const AsmToken &Tok = Parser.getTok();
1292 SMLoc S = Tok.getLoc();
1293 SMLoc E = Tok.getEndLoc();
1294 // Parse AS format
1295 // disp
1296 // disp(, base)
1297 // disp(base)
1298 // disp()
1299 // (, base)
1300 // (base)
1301 // base
1302
1303 MCRegister BaseReg;
1304 std::unique_ptr<VEOperand> Offset;
1305 switch (getLexer().getKind()) {
1306 default:
1307 return ParseStatus::NoMatch;
1308
1309 case AsmToken::Minus:
1310 case AsmToken::Integer:
1311 case AsmToken::Dot:
1312 case AsmToken::Identifier: {
1313 const MCExpr *EVal;
1314 if (!parseExpression(EVal))
1315 Offset = VEOperand::CreateImm(EVal, S, E);
1316 else
1317 return ParseStatus::NoMatch;
1318 break;
1319 }
1320
1321 case AsmToken::Percent:
1322 if (parseRegister(BaseReg, S, E))
1323 return ParseStatus::NoMatch;
1324 Offset =
1325 VEOperand::CreateImm(MCConstantExpr::create(0, getContext()), S, E);
1326 break;
1327
1328 case AsmToken::LParen:
1329 // empty disp (= 0)
1330 Offset =
1331 VEOperand::CreateImm(MCConstantExpr::create(0, getContext()), S, E);
1332 break;
1333 }
1334
1335 switch (getLexer().getKind()) {
1336 default:
1337 return ParseStatus::Failure;
1338
1340 case AsmToken::Comma:
1341 Operands.push_back(BaseReg != VE::NoRegister
1342 ? VEOperand::MorphToMEMri(BaseReg, std::move(Offset))
1343 : VEOperand::MorphToMEMzi(std::move(Offset)));
1344 return ParseStatus::Success;
1345
1346 case AsmToken::LParen:
1347 if (BaseReg != VE::NoRegister)
1348 return ParseStatus::Failure;
1349 Parser.Lex(); // Eat the (
1350 break;
1351 }
1352
1353 switch (getLexer().getKind()) {
1354 default:
1355 if (parseRegister(BaseReg, S, E))
1356 return ParseStatus::Failure;
1357 break;
1358
1359 case AsmToken::Comma:
1360 Parser.Lex(); // Eat the ,
1361 if (parseRegister(BaseReg, S, E))
1362 return ParseStatus::Failure;
1363 break;
1364
1365 case AsmToken::RParen:
1366 break;
1367 }
1368
1369 if (!Parser.getTok().is(AsmToken::RParen))
1370 return ParseStatus::Failure;
1371
1372 Parser.Lex(); // Eat the )
1373 Operands.push_back(BaseReg != VE::NoRegister
1374 ? VEOperand::MorphToMEMri(BaseReg, std::move(Offset))
1375 : VEOperand::MorphToMEMzi(std::move(Offset)));
1376
1377 return ParseStatus::Success;
1378}
1379
1380ParseStatus VEAsmParser::parseMImmOperand(OperandVector &Operands) {
1381 LLVM_DEBUG(dbgs() << "parseMImmOperand\n");
1382
1383 // Parsing "(" + number + ")0/1"
1384 const AsmToken Tok1 = Parser.getTok();
1385 if (!Tok1.is(AsmToken::LParen))
1386 return ParseStatus::NoMatch;
1387
1388 Parser.Lex(); // Eat the '('.
1389
1390 const AsmToken Tok2 = Parser.getTok();
1391 SMLoc E;
1392 const MCExpr *EVal;
1393 if (!Tok2.is(AsmToken::Integer) || getParser().parseExpression(EVal, E)) {
1394 getLexer().UnLex(Tok1);
1395 return ParseStatus::NoMatch;
1396 }
1397
1398 const AsmToken Tok3 = Parser.getTok();
1399 if (!Tok3.is(AsmToken::RParen)) {
1400 getLexer().UnLex(Tok2);
1401 getLexer().UnLex(Tok1);
1402 return ParseStatus::NoMatch;
1403 }
1404 Parser.Lex(); // Eat the ')'.
1405
1406 const AsmToken &Tok4 = Parser.getTok();
1407 StringRef Suffix = Tok4.getString();
1408 if (Suffix != "1" && Suffix != "0") {
1409 getLexer().UnLex(Tok3);
1410 getLexer().UnLex(Tok2);
1411 getLexer().UnLex(Tok1);
1412 return ParseStatus::NoMatch;
1413 }
1414 Parser.Lex(); // Eat the value.
1415 SMLoc EndLoc = SMLoc::getFromPointer(Suffix.end());
1416 Operands.push_back(
1417 VEOperand::CreateMImm(EVal, Suffix == "0", Tok1.getLoc(), EndLoc));
1418 return ParseStatus::Success;
1419}
1420
1421ParseStatus VEAsmParser::parseOperand(OperandVector &Operands,
1422 StringRef Mnemonic) {
1423 LLVM_DEBUG(dbgs() << "parseOperand\n");
1424 ParseStatus Res = MatchOperandParserImpl(Operands, Mnemonic);
1425
1426 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1427 // there was a match, but an error occurred, in which case, just return that
1428 // the operand parsing failed.
1429 if (Res.isSuccess() || Res.isFailure())
1430 return Res;
1431
1432 switch (getLexer().getKind()) {
1433 case AsmToken::LParen: {
1434 // Parsing "(" + %vreg + ", " + %vreg + ")"
1435 const AsmToken Tok1 = Parser.getTok();
1436 Parser.Lex(); // Eat the '('.
1437
1438 MCRegister Reg1;
1439 SMLoc S1, E1;
1440 if (!tryParseRegister(Reg1, S1, E1).isSuccess()) {
1441 getLexer().UnLex(Tok1);
1442 return ParseStatus::NoMatch;
1443 }
1444
1445 if (!Parser.getTok().is(AsmToken::Comma))
1446 return ParseStatus::Failure;
1447 Parser.Lex(); // Eat the ','.
1448
1449 MCRegister Reg2;
1450 SMLoc S2, E2;
1451 if (!tryParseRegister(Reg2, S2, E2).isSuccess())
1452 return ParseStatus::Failure;
1453
1454 if (!Parser.getTok().is(AsmToken::RParen))
1455 return ParseStatus::Failure;
1456
1457 Operands.push_back(VEOperand::CreateToken(Tok1.getString(), Tok1.getLoc()));
1458 Operands.push_back(VEOperand::CreateReg(Reg1, S1, E1));
1459 Operands.push_back(VEOperand::CreateReg(Reg2, S2, E2));
1460 Operands.push_back(VEOperand::CreateToken(Parser.getTok().getString(),
1461 Parser.getTok().getLoc()));
1462 Parser.Lex(); // Eat the ')'.
1463 break;
1464 }
1465 default: {
1466 std::unique_ptr<VEOperand> Op;
1467 Res = parseVEAsmOperand(Op);
1468 if (!Res.isSuccess() || !Op)
1469 return ParseStatus::Failure;
1470
1471 // Push the parsed operand into the list of operands
1472 Operands.push_back(std::move(Op));
1473
1474 if (!Parser.getTok().is(AsmToken::LParen))
1475 break;
1476
1477 // Parsing %vec-reg + "(" + %sclar-reg/number + ")"
1478 std::unique_ptr<VEOperand> Op1 = VEOperand::CreateToken(
1479 Parser.getTok().getString(), Parser.getTok().getLoc());
1480 Parser.Lex(); // Eat the '('.
1481
1482 std::unique_ptr<VEOperand> Op2;
1483 Res = parseVEAsmOperand(Op2);
1484 if (!Res.isSuccess() || !Op2)
1485 return ParseStatus::Failure;
1486
1487 if (!Parser.getTok().is(AsmToken::RParen))
1488 return ParseStatus::Failure;
1489
1490 Operands.push_back(std::move(Op1));
1491 Operands.push_back(std::move(Op2));
1492 Operands.push_back(VEOperand::CreateToken(Parser.getTok().getString(),
1493 Parser.getTok().getLoc()));
1494 Parser.Lex(); // Eat the ')'.
1495 break;
1496 }
1497 }
1498
1499 return ParseStatus::Success;
1500}
1501
1502ParseStatus VEAsmParser::parseVEAsmOperand(std::unique_ptr<VEOperand> &Op) {
1503 LLVM_DEBUG(dbgs() << "parseVEAsmOperand\n");
1504 SMLoc S = Parser.getTok().getLoc();
1505 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1506 const MCExpr *EVal;
1507
1508 Op = nullptr;
1509 switch (getLexer().getKind()) {
1510 default:
1511 break;
1512
1513 case AsmToken::Percent: {
1514 MCRegister Reg;
1515 if (tryParseRegister(Reg, S, E).isSuccess())
1516 Op = VEOperand::CreateReg(Reg, S, E);
1517 break;
1518 }
1519 case AsmToken::Minus:
1520 case AsmToken::Integer:
1521 case AsmToken::Dot:
1523 if (!parseExpression(EVal))
1524 Op = VEOperand::CreateImm(EVal, S, E);
1525 break;
1526 }
1528}
1529
1530// Force static initialization.
1534
1535#define GET_REGISTER_MATCHER
1536#define GET_MATCHER_IMPLEMENTATION
1537#include "VEGenAsmMatcher.inc"
1538
1539unsigned VEAsmParser::validateTargetOperandClass(MCParsedAsmOperand &GOp,
1540 unsigned Kind) {
1541 VEOperand &Op = (VEOperand &)GOp;
1542
1543 // VE uses identical register name for all registers like both
1544 // F32 and I32 uses "%s23". Need to convert the name of them
1545 // for validation.
1546 switch (Kind) {
1547 default:
1548 break;
1549 case MCK_F32:
1550 if (Op.isReg() && VEOperand::MorphToF32Reg(Op))
1552 break;
1553 case MCK_I32:
1554 if (Op.isReg() && VEOperand::MorphToI32Reg(Op))
1556 break;
1557 case MCK_F128:
1558 if (Op.isReg() && VEOperand::MorphToF128Reg(Op))
1560 break;
1561 case MCK_VM512:
1562 if (Op.isReg() && VEOperand::MorphToVM512Reg(Op))
1564 break;
1565 case MCK_MISC:
1566 if (Op.isImm() && VEOperand::MorphToMISCReg(Op))
1568 break;
1569 }
1570 return Match_InvalidOperand;
1571}
static MCRegister MatchRegisterName(StringRef Name)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static void applyMnemonicAliases(StringRef &Mnemonic, const FeatureBitset &Features, unsigned VariantID)
constexpr LLT S1
static bool isNot(const MachineRegisterInfo &MRI, const MachineInstr &MI)
static MCRegister MatchRegisterAltName(StringRef Name)
Maps from the set of all alternative registernames to a register number.
basic Basic Alias true
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition Compiler.h:215
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
constexpr char SuffixEnd
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:539
Register Reg
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static bool isReg(const MCInst &MI, unsigned OpNo)
static bool isUImm2(const MachineOperand &MO)
const SmallVectorImpl< MachineOperand > & Cond
SI Fold Operands
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
static const MCPhysReg MISCRegs[31]
static const MCPhysReg F128Regs[32]
static const MCPhysReg F32Regs[64]
static StringRef parseCC(StringRef Name, unsigned Prefix, unsigned Suffix, bool IntegerCC, bool OmitCC, SMLoc NameLoc, OperandVector *Operands)
static const MCPhysReg VM512Regs[8]
static const MCPhysReg I32Regs[64]
static StringRef parseRD(StringRef Name, unsigned Prefix, SMLoc NameLoc, OperandVector *Operands)
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeVEAsmParser()
static bool isMImm(SDValue V)
Value * RHS
Value * LHS
Target independent representation for an assembler token.
Definition MCAsmMacro.h:22
LLVM_ABI SMLoc getLoc() const
Definition AsmLexer.cpp:31
StringRef getString() const
Get the string for the current token, this includes all characters (for example, the quotes on string...
Definition MCAsmMacro.h:103
bool is(TokenKind K) const
Definition MCAsmMacro.h:75
LLVM_ABI SMLoc getEndLoc() const
Definition AsmLexer.cpp:33
StringRef getIdentifier() const
Get the identifier string for the current token, which should be an identifier or a string.
Definition MCAsmMacro.h:92
Base class for user error types.
Definition Error.h:354
Container class for subtarget features.
void printExpr(raw_ostream &, const MCExpr &) const
Generic assembler parser interface, for use by target specific assembly parsers.
const AsmToken & getTok() const
Get the current AsmToken from the stream.
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition MCExpr.h:445
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition MCExpr.h:448
Opcode getOpcode() const
Get the kind of this binary expression.
Definition MCExpr.h:442
static LLVM_ABI const MCBinaryExpr * create(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.cpp:201
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
@ Unary
Unary expressions.
Definition MCExpr.h:44
@ Constant
Constant expressions.
Definition MCExpr.h:42
@ SymbolRef
References to labels and assigned expressions.
Definition MCExpr.h:43
@ Target
Target specific expression.
Definition MCExpr.h:46
@ Specifier
Expression with a relocation specifier.
Definition MCExpr.h:45
@ Binary
Binary expressions.
Definition MCExpr.h:41
void setLoc(SMLoc loc)
Definition MCInst.h:207
void addOperand(const MCOperand Op)
Definition MCInst.h:215
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:166
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
MCParsedAsmOperand - This abstract class represents a source-level assembly instruction operand.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
constexpr unsigned id() const
Definition MCRegister.h:82
static const MCSpecifierExpr * create(const MCExpr *Expr, Spec S, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.cpp:743
Streaming machine code generation interface.
Definition MCStreamer.h:222
virtual void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI)
Emit the given Instruction into the current section.
Generic base class for all target subtargets.
const MCSymbol & getSymbol() const
Definition MCExpr.h:226
uint16_t getSpecifier() const
Definition MCExpr.h:232
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:213
MCTargetAsmParser - Generic interface to target specific assembly parsers.
Opcode getOpcode() const
Get the kind of this unary expression.
Definition MCExpr.h:285
static LLVM_ABI const MCUnaryExpr * create(Opcode Op, const MCExpr *Expr, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.cpp:207
const MCExpr * getSubExpr() const
Get the child of this unary expression.
Definition MCExpr.h:288
Ternary parse status returned by various parse* methods.
constexpr bool isFailure() const
static constexpr StatusTy Failure
constexpr bool isSuccess() const
static constexpr StatusTy Success
static constexpr StatusTy NoMatch
Represents a location in source code.
Definition SMLoc.h:22
static SMLoc getFromPointer(const char *Ptr)
Definition SMLoc.h:35
constexpr const char * getPointer() const
Definition SMLoc.h:33
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:138
iterator end() const
Definition StringRef.h:116
LLVM_ABI std::string lower() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::variant< std::monostate, Loc::Single, Loc::Multi, Loc::MMI, Loc::EntryValue > Variant
Alias for the std::variant specialization base class of DbgVariable.
Definition DwarfDebug.h:190
Flag
These should be considered private to the implementation of the MCInstrDesc class.
CondCode
Definition VE.h:43
@ CC_AF
Definition VE.h:53
@ UNKNOWN
Definition VE.h:69
@ CC_AT
Definition VE.h:68
RoundingMode
Definition VE.h:75
@ UNKNOWN
Definition VE.h:82
@ S_GOTOFF_LO32
Definition VEMCAsmInfo.h:48
@ S_TLS_GD_LO32
Definition VEMCAsmInfo.h:52
@ S_TLS_GD_HI32
Definition VEMCAsmInfo.h:51
@ S_GOTOFF_HI32
Definition VEMCAsmInfo.h:47
@ CE
Windows NT (Windows on ARM)
Definition MCAsmInfo.h:51
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
Definition SFrame.h:77
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:578
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
static bool isMem(const MachineInstr &MI, unsigned Op)
LLVM_ABI std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \t\n\v\f\r")
getToken - This function extracts one token from source, ignoring any leading characters that appear ...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
Target & getTheVETarget()
SmallVectorImpl< std::unique_ptr< MCParsedAsmOperand > > OperandVector
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
static VECC::CondCode stringToVEFCondCode(StringRef S)
Definition VE.h:129
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
@ Sub
Subtraction of integers.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
DWARFExpression::Operation Op
RoundingMode
Rounding mode.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147
static VERD::RoundingMode stringToVERD(StringRef S)
Definition VE.h:284
static VECC::CondCode stringToVEICondCode(StringRef S)
Definition VE.h:115
#define N
RegisterMCAsmParser - Helper template for registering a target specific assembly parser,...