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(const 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 parseMImmOperand(OperandVector &Operands);
72 ParseStatus parseOperand(OperandVector &Operands, StringRef Name);
73 ParseStatus parseVEAsmOperand(std::unique_ptr<VEOperand> &Operand);
74
75 // Helper function to parse expression with a symbol.
76 const MCExpr *extractSpecifier(const MCExpr *E, VE::Specifier &Variant);
77 bool parseExpression(const MCExpr *&EVal);
78
79 // Split the mnemonic stripping conditional code and quantifiers
80 StringRef splitMnemonic(StringRef Name, SMLoc NameLoc,
82
83 bool parseLiteralValues(unsigned Size, SMLoc L);
84
85public:
86 VEAsmParser(const MCSubtargetInfo &sti, MCAsmParser &parser,
87 const MCInstrInfo &MII)
88 : MCTargetAsmParser(sti, MII), Parser(parser) {
89 // Initialize the set of available features.
90 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
91 }
92};
93
94} // end anonymous namespace
95
96static const MCPhysReg MISCRegs[31] = {
97 VE::USRCC, VE::PSW, VE::SAR, VE::NoRegister,
98 VE::NoRegister, VE::NoRegister, VE::NoRegister, VE::PMMR,
99 VE::PMCR0, VE::PMCR1, VE::PMCR2, VE::PMCR3,
100 VE::NoRegister, VE::NoRegister, VE::NoRegister, VE::NoRegister,
101 VE::PMC0, VE::PMC1, VE::PMC2, VE::PMC3,
102 VE::PMC4, VE::PMC5, VE::PMC6, VE::PMC7,
103 VE::PMC8, VE::PMC9, VE::PMC10, VE::PMC11,
104 VE::PMC12, VE::PMC13, VE::PMC14};
105
106namespace {
107
108/// VEOperand - Instances of this class represent a parsed VE machine
109/// instruction.
110class VEOperand : public MCParsedAsmOperand {
111private:
112 enum KindTy {
113 k_Token,
114 k_Register,
115 k_Immediate,
116 // SX-Aurora ASX form is disp(index, base).
117 k_MemoryRegRegImm, // base=reg, index=reg, disp=imm
118 k_MemoryRegImmImm, // base=reg, index=imm, disp=imm
119 k_MemoryZeroRegImm, // base=0, index=reg, disp=imm
120 k_MemoryZeroImmImm, // base=0, index=imm, disp=imm
121 // SX-Aurora AS form is disp(base).
122 k_MemoryRegImm, // base=reg, disp=imm
123 k_MemoryZeroImm, // base=0, disp=imm
124 // Other special cases for Aurora VE
125 k_CCOp, // condition code
126 k_RDOp, // rounding mode
127 k_MImmOp, // Special immediate value of sequential bit stream of 0 or 1.
128 } Kind;
129
130 SMLoc StartLoc, EndLoc;
131
132 struct Token {
133 const char *Data;
134 unsigned Length;
135 };
136
137 struct RegOp {
138 MCRegister Reg;
139 };
140
141 struct ImmOp {
142 const MCExpr *Val;
143 };
144
145 struct MemOp {
146 MCRegister Base;
147 MCRegister IndexReg;
148 const MCExpr *Index;
149 const MCExpr *Offset;
150 };
151
152 struct CCOp {
153 unsigned CCVal;
154 };
155
156 struct RDOp {
157 unsigned RDVal;
158 };
159
160 struct MImmOp {
161 const MCExpr *Val;
162 bool M0Flag;
163 };
164
165 union {
166 struct Token Tok;
167 struct RegOp Reg;
168 struct ImmOp Imm;
169 struct MemOp Mem;
170 struct CCOp CC;
171 struct RDOp RD;
172 struct MImmOp MImm;
173 };
174
175public:
176 VEOperand(KindTy K) : Kind(K) {}
177
178 bool isToken() const override { return Kind == k_Token; }
179 bool isReg() const override { return Kind == k_Register; }
180 bool isImm() const override { return Kind == k_Immediate; }
181 bool isMem() const override {
182 return isMEMrri() || isMEMrii() || isMEMzri() || isMEMzii() || isMEMri() ||
183 isMEMzi();
184 }
185 bool isMEMrri() const { return Kind == k_MemoryRegRegImm; }
186 bool isMEMrii() const { return Kind == k_MemoryRegImmImm; }
187 bool isMEMzri() const { return Kind == k_MemoryZeroRegImm; }
188 bool isMEMzii() const { return Kind == k_MemoryZeroImmImm; }
189 bool isMEMri() const { return Kind == k_MemoryRegImm; }
190 bool isMEMzi() const { return Kind == k_MemoryZeroImm; }
191 bool isCCOp() const { return Kind == k_CCOp; }
192 bool isRDOp() const { return Kind == k_RDOp; }
193 bool isZero() const {
194 if (!isImm())
195 return false;
196
197 // Constant case
198 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
199 int64_t Value = ConstExpr->getValue();
200 return Value == 0;
201 }
202 return false;
203 }
204 bool isUImm0to2() const {
205 if (!isImm())
206 return false;
207
208 // Constant case
209 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
210 int64_t Value = ConstExpr->getValue();
211 return Value >= 0 && Value < 3;
212 }
213 return false;
214 }
215 bool isUImm1() const {
216 if (!isImm())
217 return false;
218
219 // Constant case
220 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
221 int64_t Value = ConstExpr->getValue();
222 return isUInt<1>(Value);
223 }
224 return false;
225 }
226 bool isUImm2() const {
227 if (!isImm())
228 return false;
229
230 // Constant case
231 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
232 int64_t Value = ConstExpr->getValue();
233 return isUInt<2>(Value);
234 }
235 return false;
236 }
237 bool isUImm3() const {
238 if (!isImm())
239 return false;
240
241 // Constant case
242 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
243 int64_t Value = ConstExpr->getValue();
244 return isUInt<3>(Value);
245 }
246 return false;
247 }
248 bool isUImm4() const {
249 if (!isImm())
250 return false;
251
252 // Constant case
253 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
254 int64_t Value = ConstExpr->getValue();
255 return isUInt<4>(Value);
256 }
257 return false;
258 }
259 bool isUImm6() const {
260 if (!isImm())
261 return false;
262
263 // Constant case
264 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
265 int64_t Value = ConstExpr->getValue();
266 return isUInt<6>(Value);
267 }
268 return false;
269 }
270 bool isUImm7() const {
271 if (!isImm())
272 return false;
273
274 // Constant case
275 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
276 int64_t Value = ConstExpr->getValue();
277 return isUInt<7>(Value);
278 }
279 return false;
280 }
281 bool isSImm7() const {
282 if (!isImm())
283 return false;
284
285 // Constant case
286 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(Imm.Val)) {
287 int64_t Value = ConstExpr->getValue();
288 return isInt<7>(Value);
289 }
290 return false;
291 }
292 bool isMImm() const {
293 if (Kind != k_MImmOp)
294 return false;
295
296 // Constant case
297 if (const auto *ConstExpr = dyn_cast<MCConstantExpr>(MImm.Val)) {
298 int64_t Value = ConstExpr->getValue();
299 return isUInt<6>(Value);
300 }
301 return false;
302 }
303
304 StringRef getToken() const {
305 assert(Kind == k_Token && "Invalid access!");
306 return StringRef(Tok.Data, Tok.Length);
307 }
308
309 MCRegister getReg() const override {
310 assert((Kind == k_Register) && "Invalid access!");
311 return Reg.Reg;
312 }
313
314 const MCExpr *getImm() const {
315 assert((Kind == k_Immediate) && "Invalid access!");
316 return Imm.Val;
317 }
318
319 MCRegister getMemBase() const {
320 assert((Kind == k_MemoryRegRegImm || Kind == k_MemoryRegImmImm ||
321 Kind == k_MemoryRegImm) &&
322 "Invalid access!");
323 return Mem.Base;
324 }
325
326 MCRegister getMemIndexReg() const {
327 assert((Kind == k_MemoryRegRegImm || Kind == k_MemoryZeroRegImm) &&
328 "Invalid access!");
329 return Mem.IndexReg;
330 }
331
332 const MCExpr *getMemIndex() const {
333 assert((Kind == k_MemoryRegImmImm || Kind == k_MemoryZeroImmImm) &&
334 "Invalid access!");
335 return Mem.Index;
336 }
337
338 const MCExpr *getMemOffset() const {
339 assert((Kind == k_MemoryRegRegImm || Kind == k_MemoryRegImmImm ||
340 Kind == k_MemoryZeroImmImm || Kind == k_MemoryZeroRegImm ||
341 Kind == k_MemoryRegImm || Kind == k_MemoryZeroImm) &&
342 "Invalid access!");
343 return Mem.Offset;
344 }
345
346 void setMemOffset(const MCExpr *off) {
347 assert((Kind == k_MemoryRegRegImm || Kind == k_MemoryRegImmImm ||
348 Kind == k_MemoryZeroImmImm || Kind == k_MemoryZeroRegImm ||
349 Kind == k_MemoryRegImm || Kind == k_MemoryZeroImm) &&
350 "Invalid access!");
351 Mem.Offset = off;
352 }
353
354 unsigned getCCVal() const {
355 assert((Kind == k_CCOp) && "Invalid access!");
356 return CC.CCVal;
357 }
358
359 unsigned getRDVal() const {
360 assert((Kind == k_RDOp) && "Invalid access!");
361 return RD.RDVal;
362 }
363
364 const MCExpr *getMImmVal() const {
365 assert((Kind == k_MImmOp) && "Invalid access!");
366 return MImm.Val;
367 }
368 bool getM0Flag() const {
369 assert((Kind == k_MImmOp) && "Invalid access!");
370 return MImm.M0Flag;
371 }
372
373 /// getStartLoc - Get the location of the first token of this operand.
374 SMLoc getStartLoc() const override { return StartLoc; }
375 /// getEndLoc - Get the location of the last token of this operand.
376 SMLoc getEndLoc() const override { return EndLoc; }
377
378 void print(raw_ostream &OS, const MCAsmInfo &MAI) const override {
379 switch (Kind) {
380 case k_Token:
381 OS << "Token: " << getToken() << "\n";
382 break;
383 case k_Register:
384 OS << "Reg: #" << getReg().id() << "\n";
385 break;
386 case k_Immediate:
387 OS << "Imm: " << getImm() << "\n";
388 break;
389 case k_MemoryRegRegImm:
390 assert(getMemOffset() != nullptr);
391 OS << "Mem: #" << getMemBase().id() << "+#" << getMemIndexReg().id()
392 << "+";
393 MAI.printExpr(OS, *getMemOffset());
394 OS << "\n";
395 break;
396 case k_MemoryRegImmImm:
397 assert(getMemIndex() != nullptr && getMemOffset() != nullptr);
398 OS << "Mem: #" << getMemBase().id() << "+";
399 MAI.printExpr(OS, *getMemIndex());
400 OS << "+";
401 MAI.printExpr(OS, *getMemOffset());
402 OS << "\n";
403 break;
404 case k_MemoryZeroRegImm:
405 assert(getMemOffset() != nullptr);
406 OS << "Mem: 0+#" << getMemIndexReg().id() << "+";
407 MAI.printExpr(OS, *getMemOffset());
408 OS << "\n";
409 break;
410 case k_MemoryZeroImmImm:
411 assert(getMemIndex() != nullptr && getMemOffset() != nullptr);
412 OS << "Mem: 0+";
413 MAI.printExpr(OS, *getMemIndex());
414 OS << "+";
415 MAI.printExpr(OS, *getMemOffset());
416 OS << "\n";
417 break;
418 case k_MemoryRegImm:
419 assert(getMemOffset() != nullptr);
420 OS << "Mem: #" << getMemBase().id() << "+";
421 MAI.printExpr(OS, *getMemOffset());
422 OS << "\n";
423 break;
424 case k_MemoryZeroImm:
425 assert(getMemOffset() != nullptr);
426 OS << "Mem: 0+";
427 MAI.printExpr(OS, *getMemOffset());
428 OS << "\n";
429 break;
430 case k_CCOp:
431 OS << "CCOp: " << getCCVal() << "\n";
432 break;
433 case k_RDOp:
434 OS << "RDOp: " << getRDVal() << "\n";
435 break;
436 case k_MImmOp:
437 OS << "MImm: (" << getMImmVal() << (getM0Flag() ? ")0" : ")1") << "\n";
438 break;
439 }
440 }
441
442 void addRegOperands(MCInst &Inst, unsigned N) const {
443 assert(N == 1 && "Invalid number of operands!");
445 }
446
447 void addMISCRegOperands(MCInst &Inst, unsigned N) const {
448 assert(N == 1 && "Invalid number of operands!");
449 MCRegister R = isImm() ? getMISCReg() : getReg();
450 assert(R && "Invalid MISC register operand!");
452 }
453
454 void addImmOperands(MCInst &Inst, unsigned N) const {
455 assert(N == 1 && "Invalid number of operands!");
456 const MCExpr *Expr = getImm();
457 addExpr(Inst, Expr);
458 }
459
460 void addZeroOperands(MCInst &Inst, unsigned N) const {
461 addImmOperands(Inst, N);
462 }
463
464 void addUImm0to2Operands(MCInst &Inst, unsigned N) const {
465 addImmOperands(Inst, N);
466 }
467
468 void addUImm1Operands(MCInst &Inst, unsigned N) const {
469 addImmOperands(Inst, N);
470 }
471
472 void addUImm2Operands(MCInst &Inst, unsigned N) const {
473 addImmOperands(Inst, N);
474 }
475
476 void addUImm3Operands(MCInst &Inst, unsigned N) const {
477 addImmOperands(Inst, N);
478 }
479
480 void addUImm4Operands(MCInst &Inst, unsigned N) const {
481 addImmOperands(Inst, N);
482 }
483
484 void addUImm6Operands(MCInst &Inst, unsigned N) const {
485 addImmOperands(Inst, N);
486 }
487
488 void addUImm7Operands(MCInst &Inst, unsigned N) const {
489 addImmOperands(Inst, N);
490 }
491
492 void addSImm7Operands(MCInst &Inst, unsigned N) const {
493 addImmOperands(Inst, N);
494 }
495
496 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
497 // Add as immediate when possible. Null MCExpr = 0.
498 if (!Expr)
500 else if (const auto *CE = dyn_cast<MCConstantExpr>(Expr))
501 Inst.addOperand(MCOperand::createImm(CE->getValue()));
502 else
504 }
505
506 void addMEMrriOperands(MCInst &Inst, unsigned N) const {
507 assert(N == 3 && "Invalid number of operands!");
508
509 Inst.addOperand(MCOperand::createReg(getMemBase()));
510 Inst.addOperand(MCOperand::createReg(getMemIndexReg()));
511 addExpr(Inst, getMemOffset());
512 }
513
514 void addMEMriiOperands(MCInst &Inst, unsigned N) const {
515 assert(N == 3 && "Invalid number of operands!");
516
517 Inst.addOperand(MCOperand::createReg(getMemBase()));
518 addExpr(Inst, getMemIndex());
519 addExpr(Inst, getMemOffset());
520 }
521
522 void addMEMzriOperands(MCInst &Inst, unsigned N) const {
523 assert(N == 3 && "Invalid number of operands!");
524
526 Inst.addOperand(MCOperand::createReg(getMemIndexReg()));
527 addExpr(Inst, getMemOffset());
528 }
529
530 void addMEMziiOperands(MCInst &Inst, unsigned N) const {
531 assert(N == 3 && "Invalid number of operands!");
532
534 addExpr(Inst, getMemIndex());
535 addExpr(Inst, getMemOffset());
536 }
537
538 void addMEMriOperands(MCInst &Inst, unsigned N) const {
539 assert(N == 2 && "Invalid number of operands!");
540
541 Inst.addOperand(MCOperand::createReg(getMemBase()));
542 addExpr(Inst, getMemOffset());
543 }
544
545 void addMEMziOperands(MCInst &Inst, unsigned N) const {
546 assert(N == 2 && "Invalid number of operands!");
547
549 addExpr(Inst, getMemOffset());
550 }
551
552 void addCCOpOperands(MCInst &Inst, unsigned N) const {
553 assert(N == 1 && "Invalid number of operands!");
554
555 Inst.addOperand(MCOperand::createImm(getCCVal()));
556 }
557
558 void addRDOpOperands(MCInst &Inst, unsigned N) const {
559 assert(N == 1 && "Invalid number of operands!");
560
561 Inst.addOperand(MCOperand::createImm(getRDVal()));
562 }
563
564 void addMImmOperands(MCInst &Inst, unsigned N) const {
565 assert(N == 1 && "Invalid number of operands!");
566 const auto *ConstExpr = dyn_cast<MCConstantExpr>(getMImmVal());
567 assert(ConstExpr && "Null operands!");
568 int64_t Value = ConstExpr->getValue();
569 if (getM0Flag())
570 Value += 64;
572 }
573
574 static std::unique_ptr<VEOperand> CreateToken(StringRef Str, SMLoc S) {
575 auto Op = std::make_unique<VEOperand>(k_Token);
576 Op->Tok.Data = Str.data();
577 Op->Tok.Length = Str.size();
578 Op->StartLoc = S;
579 Op->EndLoc = S;
580 return Op;
581 }
582
583 static std::unique_ptr<VEOperand> CreateReg(MCRegister Reg, SMLoc S,
584 SMLoc E) {
585 auto Op = std::make_unique<VEOperand>(k_Register);
586 Op->Reg.Reg = Reg;
587 Op->StartLoc = S;
588 Op->EndLoc = E;
589 return Op;
590 }
591
592 static std::unique_ptr<VEOperand> CreateImm(const MCExpr *Val, SMLoc S,
593 SMLoc E) {
594 auto Op = std::make_unique<VEOperand>(k_Immediate);
595 Op->Imm.Val = Val;
596 Op->StartLoc = S;
597 Op->EndLoc = E;
598 return Op;
599 }
600
601 static std::unique_ptr<VEOperand> CreateCCOp(unsigned CCVal, SMLoc S,
602 SMLoc E) {
603 auto Op = std::make_unique<VEOperand>(k_CCOp);
604 Op->CC.CCVal = CCVal;
605 Op->StartLoc = S;
606 Op->EndLoc = E;
607 return Op;
608 }
609
610 static std::unique_ptr<VEOperand> CreateRDOp(unsigned RDVal, SMLoc S,
611 SMLoc E) {
612 auto Op = std::make_unique<VEOperand>(k_RDOp);
613 Op->RD.RDVal = RDVal;
614 Op->StartLoc = S;
615 Op->EndLoc = E;
616 return Op;
617 }
618
619 static std::unique_ptr<VEOperand> CreateMImm(const MCExpr *Val, bool Flag,
620 SMLoc S, SMLoc E) {
621 auto Op = std::make_unique<VEOperand>(k_MImmOp);
622 Op->MImm.Val = Val;
623 Op->MImm.M0Flag = Flag;
624 Op->StartLoc = S;
625 Op->EndLoc = E;
626 return Op;
627 }
628
629 MCRegister getMISCReg() const {
630 assert(isImm());
631 const auto *ConstExpr = dyn_cast<MCConstantExpr>(getImm());
632 if (!ConstExpr)
633 return VE::NoRegister;
634 uint64_t regIdx = ConstExpr->getValue();
635 if (regIdx >= std::size(MISCRegs))
636 return VE::NoRegister;
637 return MISCRegs[regIdx];
638 }
639
640 static std::unique_ptr<VEOperand>
641 MorphToMEMri(MCRegister Base, std::unique_ptr<VEOperand> Op) {
642 const MCExpr *Imm = Op->getImm();
643 Op->Kind = k_MemoryRegImm;
644 Op->Mem.Base = Base;
645 Op->Mem.IndexReg = MCRegister();
646 Op->Mem.Index = nullptr;
647 Op->Mem.Offset = Imm;
648 return Op;
649 }
650
651 static std::unique_ptr<VEOperand>
652 MorphToMEMzi(std::unique_ptr<VEOperand> Op) {
653 const MCExpr *Imm = Op->getImm();
654 Op->Kind = k_MemoryZeroImm;
655 Op->Mem.Base = MCRegister();
656 Op->Mem.IndexReg = MCRegister();
657 Op->Mem.Index = nullptr;
658 Op->Mem.Offset = Imm;
659 return Op;
660 }
661
662 static std::unique_ptr<VEOperand>
663 MorphToMEMrri(MCRegister Base, MCRegister Index,
664 std::unique_ptr<VEOperand> Op) {
665 const MCExpr *Imm = Op->getImm();
666 Op->Kind = k_MemoryRegRegImm;
667 Op->Mem.Base = Base;
668 Op->Mem.IndexReg = Index;
669 Op->Mem.Index = nullptr;
670 Op->Mem.Offset = Imm;
671 return Op;
672 }
673
674 static std::unique_ptr<VEOperand>
675 MorphToMEMrii(MCRegister Base, const MCExpr *Index,
676 std::unique_ptr<VEOperand> Op) {
677 const MCExpr *Imm = Op->getImm();
678 Op->Kind = k_MemoryRegImmImm;
679 Op->Mem.Base = Base;
680 Op->Mem.IndexReg = MCRegister();
681 Op->Mem.Index = Index;
682 Op->Mem.Offset = Imm;
683 return Op;
684 }
685
686 static std::unique_ptr<VEOperand>
687 MorphToMEMzri(MCRegister Index, std::unique_ptr<VEOperand> Op) {
688 const MCExpr *Imm = Op->getImm();
689 Op->Kind = k_MemoryZeroRegImm;
690 Op->Mem.Base = MCRegister();
691 Op->Mem.IndexReg = Index;
692 Op->Mem.Index = nullptr;
693 Op->Mem.Offset = Imm;
694 return Op;
695 }
696
697 static std::unique_ptr<VEOperand>
698 MorphToMEMzii(const MCExpr *Index, std::unique_ptr<VEOperand> Op) {
699 const MCExpr *Imm = Op->getImm();
700 Op->Kind = k_MemoryZeroImmImm;
701 Op->Mem.Base = MCRegister();
702 Op->Mem.IndexReg = MCRegister();
703 Op->Mem.Index = Index;
704 Op->Mem.Offset = Imm;
705 return Op;
706 }
707};
708
709} // end anonymous namespace
710
711bool VEAsmParser::matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
714 bool MatchingInlineAsm) {
715 MCInst Inst;
716 unsigned MatchResult =
717 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
718 switch (MatchResult) {
719 case Match_Success:
720 Inst.setLoc(IDLoc);
721 Out.emitInstruction(Inst, getSTI());
722 return false;
723
724 case Match_MissingFeature:
725 return Error(IDLoc,
726 "instruction requires a CPU feature not currently enabled");
727
728 case Match_InvalidOperand: {
729 SMLoc ErrorLoc = IDLoc;
730 if (ErrorInfo != ~0ULL) {
731 if (ErrorInfo >= Operands.size())
732 return Error(IDLoc, "too few operands for instruction");
733
734 ErrorLoc = ((VEOperand &)*Operands[ErrorInfo]).getStartLoc();
735 if (ErrorLoc == SMLoc())
736 ErrorLoc = IDLoc;
737 }
738
739 return Error(ErrorLoc, "invalid operand for instruction");
740 }
741 case Match_MnemonicFail:
742 return Error(IDLoc, "invalid instruction mnemonic");
743 }
744 llvm_unreachable("Implement any new match types added!");
745}
746
747bool VEAsmParser::parseRegister(MCRegister &Reg, SMLoc &StartLoc,
748 SMLoc &EndLoc) {
749 if (!tryParseRegister(Reg, StartLoc, EndLoc).isSuccess())
750 return Error(StartLoc, "invalid register name");
751 return false;
752}
753
754/// Parses a register name using a given matching function.
755/// Checks for lowercase or uppercase if necessary.
756MCRegister VEAsmParser::parseRegisterName(MCRegister (*matchFn)(StringRef)) {
757 StringRef Name = Parser.getTok().getString();
758
759 MCRegister RegNum = matchFn(Name);
760
761 // GCC supports case insensitive register names. All of the VE registers
762 // are all lower case.
763 if (!RegNum) {
764 RegNum = matchFn(Name.lower());
765 }
766
767 return RegNum;
768}
769
770/// Maps from the set of all register names to a register number.
771/// \note Generated by TableGen.
773
774/// Maps from the set of all alternative registernames to a register number.
775/// \note Generated by TableGen.
777
778ParseStatus VEAsmParser::tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
779 SMLoc &EndLoc) {
780 const AsmToken Tok = Parser.getTok();
781 StartLoc = Tok.getLoc();
782 EndLoc = Tok.getEndLoc();
783 Reg = VE::NoRegister;
784 if (getLexer().getKind() != AsmToken::Percent)
786 Parser.Lex();
787
788 Reg = parseRegisterName(&MatchRegisterName);
789 if (Reg == VE::NoRegister)
790 Reg = parseRegisterName(&MatchRegisterAltName);
791
792 if (Reg != VE::NoRegister) {
793 Parser.Lex();
795 }
796
797 getLexer().UnLex(Tok);
799}
800
801static StringRef parseCC(StringRef Name, unsigned Prefix, unsigned Suffix,
802 bool IntegerCC, bool OmitCC, SMLoc NameLoc,
804 // Parse instructions with a conditional code. For example, 'bne' is
805 // converted into two operands 'b' and 'ne'.
806 StringRef Cond = Name.slice(Prefix, Suffix);
807 VECC::CondCode CondCode =
809
810 // If OmitCC is enabled, CC_AT and CC_AF is treated as a part of mnemonic.
811 if (CondCode != VECC::UNKNOWN &&
812 (!OmitCC || (CondCode != VECC::CC_AT && CondCode != VECC::CC_AF))) {
813 StringRef SuffixStr = Name.substr(Suffix);
814 // Push "b".
815 Name = Name.slice(0, Prefix);
816 Operands->push_back(VEOperand::CreateToken(Name, NameLoc));
817 // Push $cond part.
818 SMLoc CondLoc = SMLoc::getFromPointer(NameLoc.getPointer() + Prefix);
819 SMLoc SuffixLoc = SMLoc::getFromPointer(NameLoc.getPointer() + Suffix);
820 Operands->push_back(VEOperand::CreateCCOp(CondCode, CondLoc, SuffixLoc));
821 // push suffix like ".l.t"
822 if (!SuffixStr.empty())
823 Operands->push_back(VEOperand::CreateToken(SuffixStr, SuffixLoc));
824 } else {
825 Operands->push_back(VEOperand::CreateToken(Name, NameLoc));
826 }
827 return Name;
828}
829
830static StringRef parseRD(StringRef Name, unsigned Prefix, SMLoc NameLoc,
832 // Parse instructions with a conditional code. For example, 'cvt.w.d.sx.rz'
833 // is converted into two operands 'cvt.w.d.sx' and '.rz'.
834 StringRef RD = Name.substr(Prefix);
836
838 Name = Name.slice(0, Prefix);
839 // push 1st like `cvt.w.d.sx`
840 Operands->push_back(VEOperand::CreateToken(Name, NameLoc));
841 SMLoc SuffixLoc =
842 SMLoc::getFromPointer(NameLoc.getPointer() + (RD.data() - Name.data()));
844 SMLoc::getFromPointer(NameLoc.getPointer() + (RD.end() - Name.data()));
845 // push $round if it has rounding mode
846 Operands->push_back(
847 VEOperand::CreateRDOp(RoundingMode, SuffixLoc, SuffixEnd));
848 } else {
849 Operands->push_back(VEOperand::CreateToken(Name, NameLoc));
850 }
851 return Name;
852}
853
854// Split the mnemonic into ASM operand, conditional code and instruction
855// qualifier (half-word, byte).
856StringRef VEAsmParser::splitMnemonic(StringRef Name, SMLoc NameLoc,
858 // Create the leading tokens for the mnemonic
859 StringRef Mnemonic = Name;
860
861 if (Name[0] == 'b') {
862 // Match b?? or br??.
863 size_t Start = 1;
864 size_t Next = Name.find('.');
865 // Adjust position of CondCode.
866 if (Name.size() > 1 && Name[1] == 'r')
867 Start = 2;
868 // Check suffix.
869 bool ICC = true;
870 if (Next + 1 < Name.size() &&
871 (Name[Next + 1] == 'd' || Name[Next + 1] == 's'))
872 ICC = false;
873 Mnemonic = parseCC(Name, Start, Next, ICC, true, NameLoc, Operands);
874 } else if (Name.starts_with("cmov.l.") || Name.starts_with("cmov.w.") ||
875 Name.starts_with("cmov.d.") || Name.starts_with("cmov.s.")) {
876 bool ICC = Name[5] == 'l' || Name[5] == 'w';
877 Mnemonic = parseCC(Name, 7, Name.size(), ICC, false, NameLoc, Operands);
878 } else if (Name.starts_with("cvt.w.d.sx") || Name.starts_with("cvt.w.d.zx") ||
879 Name.starts_with("cvt.w.s.sx") || Name.starts_with("cvt.w.s.zx")) {
880 Mnemonic = parseRD(Name, 10, NameLoc, Operands);
881 } else if (Name.starts_with("cvt.l.d")) {
882 Mnemonic = parseRD(Name, 7, NameLoc, Operands);
883 } else if (Name.starts_with("vcvt.w.d.sx") ||
884 Name.starts_with("vcvt.w.d.zx") ||
885 Name.starts_with("vcvt.w.s.sx") ||
886 Name.starts_with("vcvt.w.s.zx")) {
887 Mnemonic = parseRD(Name, 11, NameLoc, Operands);
888 } else if (Name.starts_with("vcvt.l.d")) {
889 Mnemonic = parseRD(Name, 8, NameLoc, Operands);
890 } else if (Name.starts_with("pvcvt.w.s.lo") ||
891 Name.starts_with("pvcvt.w.s.up")) {
892 Mnemonic = parseRD(Name, 12, NameLoc, Operands);
893 } else if (Name.starts_with("pvcvt.w.s")) {
894 Mnemonic = parseRD(Name, 9, NameLoc, Operands);
895 } else if (Name.starts_with("vfmk.l.") || Name.starts_with("vfmk.w.") ||
896 Name.starts_with("vfmk.d.") || Name.starts_with("vfmk.s.")) {
897 bool ICC = Name[5] == 'l' || Name[5] == 'w' ? true : false;
898 Mnemonic = parseCC(Name, 7, Name.size(), ICC, true, NameLoc, Operands);
899 } else if (Name.starts_with("pvfmk.w.lo.") ||
900 Name.starts_with("pvfmk.w.up.") ||
901 Name.starts_with("pvfmk.s.lo.") ||
902 Name.starts_with("pvfmk.s.up.")) {
903 bool ICC = Name[6] == 'l' || Name[6] == 'w' ? true : false;
904 Mnemonic = parseCC(Name, 11, Name.size(), ICC, true, NameLoc, Operands);
905 } else {
906 Operands->push_back(VEOperand::CreateToken(Mnemonic, NameLoc));
907 }
908
909 return Mnemonic;
910}
911
912static void applyMnemonicAliases(StringRef &Mnemonic,
913 const FeatureBitset &Features,
914 unsigned VariantID);
915
916bool VEAsmParser::parseInstruction(ParseInstructionInfo &Info, StringRef Name,
917 SMLoc NameLoc, OperandVector &Operands) {
918 // If the target architecture uses MnemonicAlias, call it here to parse
919 // operands correctly.
920 applyMnemonicAliases(Name, getAvailableFeatures(), 0);
921
922 // Split name to first token and the rest, e.g. "bgt.l.t" to "b", "gt", and
923 // ".l.t". We treat "b" as a mnemonic, "gt" as first operand, and ".l.t"
924 // as second operand.
925 StringRef Mnemonic = splitMnemonic(Name, NameLoc, &Operands);
926
927 if (getLexer().isNot(AsmToken::EndOfStatement)) {
928 // Read the first operand.
929 if (!parseOperand(Operands, Mnemonic).isSuccess()) {
930 SMLoc Loc = getLexer().getLoc();
931 return Error(Loc, "unexpected token");
932 }
933
934 while (getLexer().is(AsmToken::Comma)) {
935 Parser.Lex(); // Eat the comma.
936 // Parse and remember the operand.
937 if (!parseOperand(Operands, Mnemonic).isSuccess()) {
938 SMLoc Loc = getLexer().getLoc();
939 return Error(Loc, "unexpected token");
940 }
941 }
942 }
943 if (getLexer().isNot(AsmToken::EndOfStatement)) {
944 SMLoc Loc = getLexer().getLoc();
945 return Error(Loc, "unexpected token");
946 }
947 Parser.Lex(); // Consume the EndOfStatement.
948 return false;
949}
950
951ParseStatus VEAsmParser::parseDirective(AsmToken DirectiveID) {
952 std::string IDVal = DirectiveID.getIdentifier().lower();
953
954 // Defines VE specific directives. Reference is "Vector Engine Assembly
955 // Language Reference Manual":
956 // https://www.hpc.nec/documents/sdk/pdfs/VectorEngine-as-manual-v1.3.pdf
957
958 // The .word is 4 bytes long on VE.
959 if (IDVal == ".word")
960 return parseLiteralValues(4, DirectiveID.getLoc());
961
962 // The .long is 8 bytes long on VE.
963 if (IDVal == ".long")
964 return parseLiteralValues(8, DirectiveID.getLoc());
965
966 // The .llong is 8 bytes long on VE.
967 if (IDVal == ".llong")
968 return parseLiteralValues(8, DirectiveID.getLoc());
969
970 // Let the MC layer to handle other directives.
972}
973
974/// parseLiteralValues
975/// ::= .word expression [, expression]*
976/// ::= .long expression [, expression]*
977/// ::= .llong expression [, expression]*
978bool VEAsmParser::parseLiteralValues(unsigned Size, SMLoc L) {
979 auto parseOne = [&]() -> bool {
980 const MCExpr *Value;
981 if (getParser().parseExpression(Value))
982 return true;
983 getParser().getStreamer().emitValue(Value, Size, L);
984 return false;
985 };
986 return (parseMany(parseOne));
987}
988
989/// Extract \code @lo32/@hi32/etc \endcode specifier from expression.
990/// Recursively scan the expression and check for VK_HI32/LO32/etc
991/// symbol variants. If all symbols with modifier use the same
992/// variant, return the corresponding VE::Specifier,
993/// and a modified expression using the default symbol variant.
994/// Otherwise, return NULL.
995const MCExpr *VEAsmParser::extractSpecifier(const MCExpr *E,
996 VE::Specifier &Variant) {
997 MCContext &Context = getParser().getContext();
999
1000 switch (E->getKind()) {
1001 case MCExpr::Target:
1002 case MCExpr::Constant:
1003 return nullptr;
1004 case MCExpr::Specifier:
1005 llvm_unreachable("unused by this backend");
1006
1007 case MCExpr::SymbolRef: {
1008 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1009
1010 switch (SRE->getSpecifier()) {
1011 case VE::S_None:
1012 // Use VK_REFLONG to a symbol without modifiers.
1014 break;
1015 case VE::S_HI32:
1017 break;
1018 case VE::S_LO32:
1020 break;
1021 case VE::S_PC_HI32:
1023 break;
1024 case VE::S_PC_LO32:
1026 break;
1027 case VE::S_GOT_HI32:
1029 break;
1030 case VE::S_GOT_LO32:
1032 break;
1033 case VE::S_GOTOFF_HI32:
1035 break;
1036 case VE::S_GOTOFF_LO32:
1038 break;
1039 case VE::S_PLT_HI32:
1041 break;
1042 case VE::S_PLT_LO32:
1044 break;
1045 case VE::S_TLS_GD_HI32:
1047 break;
1048 case VE::S_TLS_GD_LO32:
1050 break;
1051 case VE::S_TPOFF_HI32:
1053 break;
1054 case VE::S_TPOFF_LO32:
1056 break;
1057 default:
1058 return nullptr;
1059 }
1060
1061 return MCSymbolRefExpr::create(&SRE->getSymbol(), Context);
1062 }
1063
1064 case MCExpr::Unary: {
1065 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
1066 const MCExpr *Sub = extractSpecifier(UE->getSubExpr(), Variant);
1067 if (!Sub)
1068 return nullptr;
1069 return MCUnaryExpr::create(UE->getOpcode(), Sub, Context);
1070 }
1071
1072 case MCExpr::Binary: {
1073 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1074 VE::Specifier LHSVariant, RHSVariant;
1075 const MCExpr *LHS = extractSpecifier(BE->getLHS(), LHSVariant);
1076 const MCExpr *RHS = extractSpecifier(BE->getRHS(), RHSVariant);
1077
1078 if (!LHS && !RHS)
1079 return nullptr;
1080
1081 if (!LHS)
1082 LHS = BE->getLHS();
1083 if (!RHS)
1084 RHS = BE->getRHS();
1085
1086 if (LHSVariant == VE::S_None)
1087 Variant = RHSVariant;
1088 else if (RHSVariant == VE::S_None)
1089 Variant = LHSVariant;
1090 else if (LHSVariant == RHSVariant)
1091 Variant = LHSVariant;
1092 else
1093 return nullptr;
1094
1095 return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, Context);
1096 }
1097 }
1098
1099 llvm_unreachable("Invalid expression kind!");
1100}
1101
1102/// This differs from the default "parseExpression" in that it handles
1103/// relocation specifiers.
1104bool VEAsmParser::parseExpression(const MCExpr *&EVal) {
1105 // Handle \code symbol @lo32/@hi32/etc \endcode.
1106 if (getParser().parseExpression(EVal))
1107 return true;
1108
1109 // Convert MCSymbolRefExpr with specifier to MCSpecifierExpr.
1111 const MCExpr *E = extractSpecifier(EVal, Specifier);
1112 if (E)
1113 EVal = MCSpecifierExpr::create(E, Specifier, getParser().getContext());
1114
1115 return false;
1116}
1117
1118ParseStatus VEAsmParser::parseMEMOperand(OperandVector &Operands) {
1119 LLVM_DEBUG(dbgs() << "parseMEMOperand\n");
1120 const AsmToken &Tok = Parser.getTok();
1121 SMLoc S = Tok.getLoc();
1122 SMLoc E = Tok.getEndLoc();
1123 // Parse ASX format
1124 // disp
1125 // disp(, base)
1126 // disp(index)
1127 // disp(index, base)
1128 // (, base)
1129 // (index)
1130 // (index, base)
1131
1132 std::unique_ptr<VEOperand> Offset;
1133 switch (getLexer().getKind()) {
1134 default:
1135 return ParseStatus::NoMatch;
1136
1137 case AsmToken::Minus:
1138 case AsmToken::Integer:
1139 case AsmToken::Dot:
1140 case AsmToken::Identifier: {
1141 const MCExpr *EVal;
1142 if (!parseExpression(EVal))
1143 Offset = VEOperand::CreateImm(EVal, S, E);
1144 else
1145 return ParseStatus::NoMatch;
1146 break;
1147 }
1148
1149 case AsmToken::LParen:
1150 // empty disp (= 0)
1151 Offset =
1152 VEOperand::CreateImm(MCConstantExpr::create(0, getContext()), S, E);
1153 break;
1154 }
1155
1156 switch (getLexer().getKind()) {
1157 default:
1158 return ParseStatus::Failure;
1159
1161 Operands.push_back(VEOperand::MorphToMEMzii(
1162 MCConstantExpr::create(0, getContext()), std::move(Offset)));
1163 return ParseStatus::Success;
1164
1165 case AsmToken::LParen:
1166 Parser.Lex(); // Eat the (
1167 break;
1168 }
1169
1170 const MCExpr *IndexValue = nullptr;
1171 MCRegister IndexReg;
1172
1173 switch (getLexer().getKind()) {
1174 default:
1175 if (parseRegister(IndexReg, S, E))
1176 return ParseStatus::Failure;
1177 break;
1178
1179 case AsmToken::Minus:
1180 case AsmToken::Integer:
1181 case AsmToken::Dot:
1182 if (getParser().parseExpression(IndexValue, E))
1183 return ParseStatus::Failure;
1184 break;
1185
1186 case AsmToken::Comma:
1187 // empty index
1188 IndexValue = MCConstantExpr::create(0, getContext());
1189 break;
1190 }
1191
1192 switch (getLexer().getKind()) {
1193 default:
1194 return ParseStatus::Failure;
1195
1196 case AsmToken::RParen:
1197 Parser.Lex(); // Eat the )
1198 Operands.push_back(
1199 IndexValue ? VEOperand::MorphToMEMzii(IndexValue, std::move(Offset))
1200 : VEOperand::MorphToMEMzri(IndexReg, std::move(Offset)));
1201 return ParseStatus::Success;
1202
1203 case AsmToken::Comma:
1204 Parser.Lex(); // Eat the ,
1205 break;
1206 }
1207
1208 MCRegister BaseReg;
1209 if (parseRegister(BaseReg, S, E))
1210 return ParseStatus::Failure;
1211
1212 if (!Parser.getTok().is(AsmToken::RParen))
1213 return ParseStatus::Failure;
1214
1215 Parser.Lex(); // Eat the )
1216 Operands.push_back(
1217 IndexValue
1218 ? VEOperand::MorphToMEMrii(BaseReg, IndexValue, std::move(Offset))
1219 : VEOperand::MorphToMEMrri(BaseReg, IndexReg, std::move(Offset)));
1220
1221 return ParseStatus::Success;
1222}
1223
1224ParseStatus VEAsmParser::parseMEMAsOperand(OperandVector &Operands) {
1225 LLVM_DEBUG(dbgs() << "parseMEMAsOperand\n");
1226 const AsmToken &Tok = Parser.getTok();
1227 SMLoc S = Tok.getLoc();
1228 SMLoc E = Tok.getEndLoc();
1229 // Parse AS format
1230 // disp
1231 // disp(, base)
1232 // disp(base)
1233 // disp()
1234 // (, base)
1235 // (base)
1236 // base
1237
1238 MCRegister BaseReg;
1239 std::unique_ptr<VEOperand> Offset;
1240 switch (getLexer().getKind()) {
1241 default:
1242 return ParseStatus::NoMatch;
1243
1244 case AsmToken::Minus:
1245 case AsmToken::Integer:
1246 case AsmToken::Dot:
1247 case AsmToken::Identifier: {
1248 const MCExpr *EVal;
1249 if (!parseExpression(EVal))
1250 Offset = VEOperand::CreateImm(EVal, S, E);
1251 else
1252 return ParseStatus::NoMatch;
1253 break;
1254 }
1255
1256 case AsmToken::Percent:
1257 if (parseRegister(BaseReg, S, E))
1258 return ParseStatus::NoMatch;
1259 Offset =
1260 VEOperand::CreateImm(MCConstantExpr::create(0, getContext()), S, E);
1261 break;
1262
1263 case AsmToken::LParen:
1264 // empty disp (= 0)
1265 Offset =
1266 VEOperand::CreateImm(MCConstantExpr::create(0, getContext()), S, E);
1267 break;
1268 }
1269
1270 switch (getLexer().getKind()) {
1271 default:
1272 return ParseStatus::Failure;
1273
1275 case AsmToken::Comma:
1276 Operands.push_back(BaseReg != VE::NoRegister
1277 ? VEOperand::MorphToMEMri(BaseReg, std::move(Offset))
1278 : VEOperand::MorphToMEMzi(std::move(Offset)));
1279 return ParseStatus::Success;
1280
1281 case AsmToken::LParen:
1282 if (BaseReg != VE::NoRegister)
1283 return ParseStatus::Failure;
1284 Parser.Lex(); // Eat the (
1285 break;
1286 }
1287
1288 switch (getLexer().getKind()) {
1289 default:
1290 if (parseRegister(BaseReg, S, E))
1291 return ParseStatus::Failure;
1292 break;
1293
1294 case AsmToken::Comma:
1295 Parser.Lex(); // Eat the ,
1296 if (parseRegister(BaseReg, S, E))
1297 return ParseStatus::Failure;
1298 break;
1299
1300 case AsmToken::RParen:
1301 break;
1302 }
1303
1304 if (!Parser.getTok().is(AsmToken::RParen))
1305 return ParseStatus::Failure;
1306
1307 Parser.Lex(); // Eat the )
1308 Operands.push_back(BaseReg != VE::NoRegister
1309 ? VEOperand::MorphToMEMri(BaseReg, std::move(Offset))
1310 : VEOperand::MorphToMEMzi(std::move(Offset)));
1311
1312 return ParseStatus::Success;
1313}
1314
1315ParseStatus VEAsmParser::parseMImmOperand(OperandVector &Operands) {
1316 LLVM_DEBUG(dbgs() << "parseMImmOperand\n");
1317
1318 // Parsing "(" + number + ")0/1"
1319 const AsmToken Tok1 = Parser.getTok();
1320 if (!Tok1.is(AsmToken::LParen))
1321 return ParseStatus::NoMatch;
1322
1323 Parser.Lex(); // Eat the '('.
1324
1325 const AsmToken Tok2 = Parser.getTok();
1326 SMLoc E;
1327 const MCExpr *EVal;
1328 if (!Tok2.is(AsmToken::Integer) || getParser().parseExpression(EVal, E)) {
1329 getLexer().UnLex(Tok1);
1330 return ParseStatus::NoMatch;
1331 }
1332
1333 const AsmToken Tok3 = Parser.getTok();
1334 if (!Tok3.is(AsmToken::RParen)) {
1335 getLexer().UnLex(Tok2);
1336 getLexer().UnLex(Tok1);
1337 return ParseStatus::NoMatch;
1338 }
1339 Parser.Lex(); // Eat the ')'.
1340
1341 const AsmToken &Tok4 = Parser.getTok();
1342 StringRef Suffix = Tok4.getString();
1343 if (Suffix != "1" && Suffix != "0") {
1344 getLexer().UnLex(Tok3);
1345 getLexer().UnLex(Tok2);
1346 getLexer().UnLex(Tok1);
1347 return ParseStatus::NoMatch;
1348 }
1349 Parser.Lex(); // Eat the value.
1350 SMLoc EndLoc = SMLoc::getFromPointer(Suffix.end());
1351 Operands.push_back(
1352 VEOperand::CreateMImm(EVal, Suffix == "0", Tok1.getLoc(), EndLoc));
1353 return ParseStatus::Success;
1354}
1355
1356ParseStatus VEAsmParser::parseOperand(OperandVector &Operands,
1357 StringRef Mnemonic) {
1358 LLVM_DEBUG(dbgs() << "parseOperand\n");
1359 ParseStatus Res = MatchOperandParserImpl(Operands, Mnemonic);
1360
1361 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1362 // there was a match, but an error occurred, in which case, just return that
1363 // the operand parsing failed.
1364 if (Res.isSuccess() || Res.isFailure())
1365 return Res;
1366
1367 switch (getLexer().getKind()) {
1368 case AsmToken::LParen: {
1369 // Parsing "(" + %vreg + ", " + %vreg + ")"
1370 const AsmToken Tok1 = Parser.getTok();
1371 Parser.Lex(); // Eat the '('.
1372
1373 MCRegister Reg1;
1374 SMLoc S1, E1;
1375 if (!tryParseRegister(Reg1, S1, E1).isSuccess()) {
1376 getLexer().UnLex(Tok1);
1377 return ParseStatus::NoMatch;
1378 }
1379
1380 if (!Parser.getTok().is(AsmToken::Comma))
1381 return ParseStatus::Failure;
1382 Parser.Lex(); // Eat the ','.
1383
1384 MCRegister Reg2;
1385 SMLoc S2, E2;
1386 if (!tryParseRegister(Reg2, S2, E2).isSuccess())
1387 return ParseStatus::Failure;
1388
1389 if (!Parser.getTok().is(AsmToken::RParen))
1390 return ParseStatus::Failure;
1391
1392 Operands.push_back(VEOperand::CreateToken(Tok1.getString(), Tok1.getLoc()));
1393 Operands.push_back(VEOperand::CreateReg(Reg1, S1, E1));
1394 Operands.push_back(VEOperand::CreateReg(Reg2, S2, E2));
1395 Operands.push_back(VEOperand::CreateToken(Parser.getTok().getString(),
1396 Parser.getTok().getLoc()));
1397 Parser.Lex(); // Eat the ')'.
1398 break;
1399 }
1400 default: {
1401 std::unique_ptr<VEOperand> Op;
1402 Res = parseVEAsmOperand(Op);
1403 if (!Res.isSuccess() || !Op)
1404 return ParseStatus::Failure;
1405
1406 // Push the parsed operand into the list of operands
1407 Operands.push_back(std::move(Op));
1408
1409 if (!Parser.getTok().is(AsmToken::LParen))
1410 break;
1411
1412 // Parsing %vec-reg + "(" + %sclar-reg/number + ")"
1413 std::unique_ptr<VEOperand> Op1 = VEOperand::CreateToken(
1414 Parser.getTok().getString(), Parser.getTok().getLoc());
1415 Parser.Lex(); // Eat the '('.
1416
1417 std::unique_ptr<VEOperand> Op2;
1418 Res = parseVEAsmOperand(Op2);
1419 if (!Res.isSuccess() || !Op2)
1420 return ParseStatus::Failure;
1421
1422 if (!Parser.getTok().is(AsmToken::RParen))
1423 return ParseStatus::Failure;
1424
1425 Operands.push_back(std::move(Op1));
1426 Operands.push_back(std::move(Op2));
1427 Operands.push_back(VEOperand::CreateToken(Parser.getTok().getString(),
1428 Parser.getTok().getLoc()));
1429 Parser.Lex(); // Eat the ')'.
1430 break;
1431 }
1432 }
1433
1434 return ParseStatus::Success;
1435}
1436
1437ParseStatus VEAsmParser::parseVEAsmOperand(std::unique_ptr<VEOperand> &Op) {
1438 LLVM_DEBUG(dbgs() << "parseVEAsmOperand\n");
1439 SMLoc S = Parser.getTok().getLoc();
1440 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1441 const MCExpr *EVal;
1442
1443 Op = nullptr;
1444 switch (getLexer().getKind()) {
1445 default:
1446 break;
1447
1448 case AsmToken::Percent: {
1449 MCRegister Reg;
1450 if (tryParseRegister(Reg, S, E).isSuccess())
1451 Op = VEOperand::CreateReg(Reg, S, E);
1452 break;
1453 }
1454 case AsmToken::Minus:
1455 case AsmToken::Integer:
1456 case AsmToken::Dot:
1458 if (!parseExpression(EVal))
1459 Op = VEOperand::CreateImm(EVal, S, E);
1460 break;
1461 }
1463}
1464
1465// Force static initialization.
1469
1470#define GET_REGISTER_MATCHER
1471#define GET_MATCHER_IMPLEMENTATION
1472#include "VEGenAsmMatcher.inc"
1473
1474unsigned VEAsmParser::validateTargetOperandClass(const MCParsedAsmOperand &GOp,
1475 unsigned Kind) {
1476 const VEOperand &Op = static_cast<const VEOperand &>(GOp);
1477
1478 if (Kind == MCK_MISC && Op.isImm() && Op.getMISCReg())
1480 return Match_InvalidOperand;
1481}
static MCRegister MatchRegisterName(StringRef Name)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static void applyMnemonicAliases(StringRef &Mnemonic, const FeatureBitset &Features, unsigned VariantID)
uint64_t uint64_t
uint64_t Imm
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:211
#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:550
SmallVector< MachineOperand, 4 > Cond
Register Reg
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static bool isReg(const MCInst &MI, unsigned OpNo)
RISCVCC::CondCode CC
static bool isUImm2(const MachineOperand &MO)
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 StringRef parseCC(StringRef Name, unsigned Prefix, unsigned Suffix, bool IntegerCC, bool OmitCC, SMLoc NameLoc, OperandVector *Operands)
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:442
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition MCExpr.h:445
Opcode getOpcode() const
Get the kind of this binary expression.
Definition MCExpr.h:439
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:31
@ Unary
Unary expressions.
Definition MCExpr.h:41
@ Constant
Constant expressions.
Definition MCExpr.h:39
@ SymbolRef
References to labels and assigned expressions.
Definition MCExpr.h:40
@ Target
Target specific expression.
Definition MCExpr.h:43
@ Specifier
Expression with a relocation specifier.
Definition MCExpr.h:42
@ Binary
Binary expressions.
Definition MCExpr.h:38
void setLoc(SMLoc loc)
Definition MCInst.h:206
void addOperand(const MCOperand Op)
Definition MCInst.h:214
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:165
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:137
static MCOperand createImm(int64_t Val)
Definition MCInst.h:144
MCParsedAsmOperand - This abstract class represents a source-level assembly instruction operand.
Wrapper class representing physical registers and target specific virtual registers.
Definition MCRegister.h:42
constexpr unsigned id() const
Definition MCRegister.h:85
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:221
Generic base class for all target subtargets.
const MCSymbol & getSymbol() const
Definition MCExpr.h:223
uint16_t getSpecifier() const
Definition MCExpr.h:229
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:210
MCTargetAsmParser - Generic interface to target specific assembly parsers.
Opcode getOpcode() const
Get the kind of this unary expression.
Definition MCExpr.h:282
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:285
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:60
@ CC_AF
Definition VE.h:70
@ UNKNOWN
Definition VE.h:86
@ CC_AT
Definition VE.h:85
RoundingMode
Definition VE.h:92
@ UNKNOWN
Definition VE.h:99
@ S_GOTOFF_LO32
Definition VEMCAsmInfo.h:42
@ S_TLS_GD_LO32
Definition VEMCAsmInfo.h:46
@ S_TLS_GD_HI32
Definition VEMCAsmInfo.h:45
@ S_GOTOFF_HI32
Definition VEMCAsmInfo.h:41
@ 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:577
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:146
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:301
static VECC::CondCode stringToVEICondCode(StringRef S)
Definition VE.h:132
#define N
RegisterMCAsmParser - Helper template for registering a target specific assembly parser,...