1 //===-- VEAsmParser.cpp - Parse VE assembly to MCInst instructions --------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "MCTargetDesc/VEMCExpr.h"
10 #include "MCTargetDesc/VEMCTargetDesc.h"
11 #include "TargetInfo/VETargetInfo.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCParser/MCAsmLexer.h"
21 #include "llvm/MC/MCParser/MCAsmParser.h"
22 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
23 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
24 #include "llvm/MC/MCRegisterInfo.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/MC/MCSubtargetInfo.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/Support/TargetRegistry.h"
29 #include "llvm/Support/raw_ostream.h"
35 #define DEBUG_TYPE "ve-asmparser"
41 class VEAsmParser
: public MCTargetAsmParser
{
44 /// @name Auto-generated Match Functions
47 #define GET_ASSEMBLER_HEADER
48 #include "VEGenAsmMatcher.inc"
52 // public interface of the MCTargetAsmParser.
53 bool MatchAndEmitInstruction(SMLoc IDLoc
, unsigned &Opcode
,
54 OperandVector
&Operands
, MCStreamer
&Out
,
56 bool MatchingInlineAsm
) override
;
57 bool ParseRegister(unsigned &RegNo
, SMLoc
&StartLoc
, SMLoc
&EndLoc
) override
;
58 int parseRegisterName(unsigned (*matchFn
)(StringRef
));
59 OperandMatchResultTy
tryParseRegister(unsigned &RegNo
, SMLoc
&StartLoc
,
60 SMLoc
&EndLoc
) override
;
61 bool ParseInstruction(ParseInstructionInfo
&Info
, StringRef Name
,
62 SMLoc NameLoc
, OperandVector
&Operands
) override
;
63 bool ParseDirective(AsmToken DirectiveID
) override
;
65 unsigned validateTargetOperandClass(MCParsedAsmOperand
&Op
,
66 unsigned Kind
) override
;
68 // Custom parse functions for VE specific operands.
69 OperandMatchResultTy
parseMEMOperand(OperandVector
&Operands
);
70 OperandMatchResultTy
parseMEMAsOperand(OperandVector
&Operands
);
71 OperandMatchResultTy
parseCCOpOperand(OperandVector
&Operands
);
72 OperandMatchResultTy
parseRDOpOperand(OperandVector
&Operands
);
73 OperandMatchResultTy
parseMImmOperand(OperandVector
&Operands
);
74 OperandMatchResultTy
parseOperand(OperandVector
&Operands
, StringRef Name
);
75 OperandMatchResultTy
parseVEAsmOperand(std::unique_ptr
<VEOperand
> &Operand
);
77 // Helper function to parse expression with a symbol.
78 const MCExpr
*extractModifierFromExpr(const MCExpr
*E
,
79 VEMCExpr::VariantKind
&Variant
);
80 const MCExpr
*fixupVariantKind(const MCExpr
*E
);
81 bool parseExpression(const MCExpr
*&EVal
);
83 // Split the mnemonic stripping conditional code and quantifiers
84 StringRef
splitMnemonic(StringRef Name
, SMLoc NameLoc
,
85 OperandVector
*Operands
);
88 VEAsmParser(const MCSubtargetInfo
&sti
, MCAsmParser
&parser
,
89 const MCInstrInfo
&MII
, const MCTargetOptions
&Options
)
90 : MCTargetAsmParser(Options
, sti
, MII
), Parser(parser
) {
91 // Initialize the set of available features.
92 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
96 } // end anonymous namespace
98 static 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
,
110 static 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
,
122 static 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
};
128 static const MCPhysReg VM512Regs
[8] = {VE::VMP0
, VE::VMP1
, VE::VMP2
, VE::VMP3
,
129 VE::VMP4
, VE::VMP5
, VE::VMP6
, VE::VMP7
};
131 static 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
};
143 /// VEOperand - Instances of this class represent a parsed VE machine
145 class VEOperand
: public MCParsedAsmOperand
{
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.
165 SMLoc StartLoc
, EndLoc
;
184 const MCExpr
*Offset
;
211 VEOperand(KindTy K
) : MCParsedAsmOperand(), Kind(K
) {}
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() ||
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
; }
233 if (const auto *ConstExpr
= dyn_cast
<MCConstantExpr
>(Imm
.Val
)) {
234 int64_t Value
= ConstExpr
->getValue();
244 if (const auto *ConstExpr
= dyn_cast
<MCConstantExpr
>(Imm
.Val
)) {
245 int64_t Value
= ConstExpr
->getValue();
246 return Value
>= 0 && Value
< 3;
255 if (const auto *ConstExpr
= dyn_cast
<MCConstantExpr
>(Imm
.Val
)) {
256 int64_t Value
= ConstExpr
->getValue();
257 return isUInt
<1>(Value
);
266 if (const auto *ConstExpr
= dyn_cast
<MCConstantExpr
>(Imm
.Val
)) {
267 int64_t Value
= ConstExpr
->getValue();
268 return isUInt
<2>(Value
);
277 if (const auto *ConstExpr
= dyn_cast
<MCConstantExpr
>(Imm
.Val
)) {
278 int64_t Value
= ConstExpr
->getValue();
279 return isUInt
<3>(Value
);
288 if (const auto *ConstExpr
= dyn_cast
<MCConstantExpr
>(Imm
.Val
)) {
289 int64_t Value
= ConstExpr
->getValue();
290 return isUInt
<4>(Value
);
299 if (const auto *ConstExpr
= dyn_cast
<MCConstantExpr
>(Imm
.Val
)) {
300 int64_t Value
= ConstExpr
->getValue();
301 return isUInt
<6>(Value
);
310 if (const auto *ConstExpr
= dyn_cast
<MCConstantExpr
>(Imm
.Val
)) {
311 int64_t Value
= ConstExpr
->getValue();
312 return isUInt
<7>(Value
);
321 if (const auto *ConstExpr
= dyn_cast
<MCConstantExpr
>(Imm
.Val
)) {
322 int64_t Value
= ConstExpr
->getValue();
323 return isInt
<7>(Value
);
327 bool isMImm() const {
328 if (Kind
!= k_MImmOp
)
332 if (const auto *ConstExpr
= dyn_cast
<MCConstantExpr
>(MImm
.Val
)) {
333 int64_t Value
= ConstExpr
->getValue();
334 return isUInt
<6>(Value
);
339 StringRef
getToken() const {
340 assert(Kind
== k_Token
&& "Invalid access!");
341 return StringRef(Tok
.Data
, Tok
.Length
);
344 unsigned getReg() const override
{
345 assert((Kind
== k_Register
) && "Invalid access!");
349 const MCExpr
*getImm() const {
350 assert((Kind
== k_Immediate
) && "Invalid access!");
354 unsigned getMemBase() const {
355 assert((Kind
== k_MemoryRegRegImm
|| Kind
== k_MemoryRegImmImm
||
356 Kind
== k_MemoryRegImm
) &&
361 unsigned getMemIndexReg() const {
362 assert((Kind
== k_MemoryRegRegImm
|| Kind
== k_MemoryZeroRegImm
) &&
367 const MCExpr
*getMemIndex() const {
368 assert((Kind
== k_MemoryRegImmImm
|| Kind
== k_MemoryZeroImmImm
) &&
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
) &&
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
) &&
389 unsigned getCCVal() const {
390 assert((Kind
== k_CCOp
) && "Invalid access!");
394 unsigned getRDVal() const {
395 assert((Kind
== k_RDOp
) && "Invalid access!");
399 const MCExpr
*getMImmVal() const {
400 assert((Kind
== k_MImmOp
) && "Invalid access!");
403 bool getM0Flag() const {
404 assert((Kind
== k_MImmOp
) && "Invalid access!");
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
; }
413 void print(raw_ostream
&OS
) const override
{
416 OS
<< "Token: " << getToken() << "\n";
419 OS
<< "Reg: #" << getReg() << "\n";
422 OS
<< "Imm: " << getImm() << "\n";
424 case k_MemoryRegRegImm
:
425 assert(getMemOffset() != nullptr);
426 OS
<< "Mem: #" << getMemBase() << "+#" << getMemIndexReg() << "+"
427 << *getMemOffset() << "\n";
429 case k_MemoryRegImmImm
:
430 assert(getMemIndex() != nullptr && getMemOffset() != nullptr);
431 OS
<< "Mem: #" << getMemBase() << "+" << *getMemIndex() << "+"
432 << *getMemOffset() << "\n";
434 case k_MemoryZeroRegImm
:
435 assert(getMemOffset() != nullptr);
436 OS
<< "Mem: 0+#" << getMemIndexReg() << "+" << *getMemOffset() << "\n";
438 case k_MemoryZeroImmImm
:
439 assert(getMemIndex() != nullptr && getMemOffset() != nullptr);
440 OS
<< "Mem: 0+" << *getMemIndex() << "+" << *getMemOffset() << "\n";
443 assert(getMemOffset() != nullptr);
444 OS
<< "Mem: #" << getMemBase() << "+" << *getMemOffset() << "\n";
446 case k_MemoryZeroImm
:
447 assert(getMemOffset() != nullptr);
448 OS
<< "Mem: 0+" << *getMemOffset() << "\n";
451 OS
<< "CCOp: " << getCCVal() << "\n";
454 OS
<< "RDOp: " << getRDVal() << "\n";
457 OS
<< "MImm: (" << getMImmVal() << (getM0Flag() ? ")0" : ")1") << "\n";
462 void addRegOperands(MCInst
&Inst
, unsigned N
) const {
463 assert(N
== 1 && "Invalid number of operands!");
464 Inst
.addOperand(MCOperand::createReg(getReg()));
467 void addImmOperands(MCInst
&Inst
, unsigned N
) const {
468 assert(N
== 1 && "Invalid number of operands!");
469 const MCExpr
*Expr
= getImm();
473 void addZeroOperands(MCInst
&Inst
, unsigned N
) const {
474 addImmOperands(Inst
, N
);
477 void addUImm0to2Operands(MCInst
&Inst
, unsigned N
) const {
478 addImmOperands(Inst
, N
);
481 void addUImm1Operands(MCInst
&Inst
, unsigned N
) const {
482 addImmOperands(Inst
, N
);
485 void addUImm2Operands(MCInst
&Inst
, unsigned N
) const {
486 addImmOperands(Inst
, N
);
489 void addUImm3Operands(MCInst
&Inst
, unsigned N
) const {
490 addImmOperands(Inst
, N
);
493 void addUImm4Operands(MCInst
&Inst
, unsigned N
) const {
494 addImmOperands(Inst
, N
);
497 void addUImm6Operands(MCInst
&Inst
, unsigned N
) const {
498 addImmOperands(Inst
, N
);
501 void addUImm7Operands(MCInst
&Inst
, unsigned N
) const {
502 addImmOperands(Inst
, N
);
505 void addSImm7Operands(MCInst
&Inst
, unsigned N
) const {
506 addImmOperands(Inst
, N
);
509 void addExpr(MCInst
&Inst
, const MCExpr
*Expr
) const {
510 // Add as immediate when possible. Null MCExpr = 0.
512 Inst
.addOperand(MCOperand::createImm(0));
513 else if (const auto *CE
= dyn_cast
<MCConstantExpr
>(Expr
))
514 Inst
.addOperand(MCOperand::createImm(CE
->getValue()));
516 Inst
.addOperand(MCOperand::createExpr(Expr
));
519 void addMEMrriOperands(MCInst
&Inst
, unsigned N
) const {
520 assert(N
== 3 && "Invalid number of operands!");
522 Inst
.addOperand(MCOperand::createReg(getMemBase()));
523 Inst
.addOperand(MCOperand::createReg(getMemIndexReg()));
524 addExpr(Inst
, getMemOffset());
527 void addMEMriiOperands(MCInst
&Inst
, unsigned N
) const {
528 assert(N
== 3 && "Invalid number of operands!");
530 Inst
.addOperand(MCOperand::createReg(getMemBase()));
531 addExpr(Inst
, getMemIndex());
532 addExpr(Inst
, getMemOffset());
535 void addMEMzriOperands(MCInst
&Inst
, unsigned N
) const {
536 assert(N
== 3 && "Invalid number of operands!");
538 Inst
.addOperand(MCOperand::createImm(0));
539 Inst
.addOperand(MCOperand::createReg(getMemIndexReg()));
540 addExpr(Inst
, getMemOffset());
543 void addMEMziiOperands(MCInst
&Inst
, unsigned N
) const {
544 assert(N
== 3 && "Invalid number of operands!");
546 Inst
.addOperand(MCOperand::createImm(0));
547 addExpr(Inst
, getMemIndex());
548 addExpr(Inst
, getMemOffset());
551 void addMEMriOperands(MCInst
&Inst
, unsigned N
) const {
552 assert(N
== 2 && "Invalid number of operands!");
554 Inst
.addOperand(MCOperand::createReg(getMemBase()));
555 addExpr(Inst
, getMemOffset());
558 void addMEMziOperands(MCInst
&Inst
, unsigned N
) const {
559 assert(N
== 2 && "Invalid number of operands!");
561 Inst
.addOperand(MCOperand::createImm(0));
562 addExpr(Inst
, getMemOffset());
565 void addCCOpOperands(MCInst
&Inst
, unsigned N
) const {
566 assert(N
== 1 && "Invalid number of operands!");
568 Inst
.addOperand(MCOperand::createImm(getCCVal()));
571 void addRDOpOperands(MCInst
&Inst
, unsigned N
) const {
572 assert(N
== 1 && "Invalid number of operands!");
574 Inst
.addOperand(MCOperand::createImm(getRDVal()));
577 void addMImmOperands(MCInst
&Inst
, unsigned N
) const {
578 assert(N
== 1 && "Invalid number of operands!");
579 const auto *ConstExpr
= dyn_cast
<MCConstantExpr
>(getMImmVal());
580 assert(ConstExpr
&& "Null operands!");
581 int64_t Value
= ConstExpr
->getValue();
584 Inst
.addOperand(MCOperand::createImm(Value
));
587 static std::unique_ptr
<VEOperand
> CreateToken(StringRef Str
, SMLoc S
) {
588 auto Op
= std::make_unique
<VEOperand
>(k_Token
);
589 Op
->Tok
.Data
= Str
.data();
590 Op
->Tok
.Length
= Str
.size();
596 static std::unique_ptr
<VEOperand
> CreateReg(unsigned RegNum
, SMLoc S
,
598 auto Op
= std::make_unique
<VEOperand
>(k_Register
);
599 Op
->Reg
.RegNum
= RegNum
;
605 static std::unique_ptr
<VEOperand
> CreateImm(const MCExpr
*Val
, SMLoc S
,
607 auto Op
= std::make_unique
<VEOperand
>(k_Immediate
);
614 static std::unique_ptr
<VEOperand
> CreateCCOp(unsigned CCVal
, SMLoc S
,
616 auto Op
= std::make_unique
<VEOperand
>(k_CCOp
);
617 Op
->CC
.CCVal
= CCVal
;
623 static std::unique_ptr
<VEOperand
> CreateRDOp(unsigned RDVal
, SMLoc S
,
625 auto Op
= std::make_unique
<VEOperand
>(k_RDOp
);
626 Op
->RD
.RDVal
= RDVal
;
632 static std::unique_ptr
<VEOperand
> CreateMImm(const MCExpr
*Val
, bool Flag
,
634 auto Op
= std::make_unique
<VEOperand
>(k_MImmOp
);
636 Op
->MImm
.M0Flag
= Flag
;
642 static bool MorphToI32Reg(VEOperand
&Op
) {
643 unsigned Reg
= Op
.getReg();
644 unsigned regIdx
= Reg
- VE::SX0
;
647 Op
.Reg
.RegNum
= I32Regs
[regIdx
];
651 static bool MorphToF32Reg(VEOperand
&Op
) {
652 unsigned Reg
= Op
.getReg();
653 unsigned regIdx
= Reg
- VE::SX0
;
656 Op
.Reg
.RegNum
= F32Regs
[regIdx
];
660 static bool MorphToF128Reg(VEOperand
&Op
) {
661 unsigned Reg
= Op
.getReg();
662 unsigned regIdx
= Reg
- VE::SX0
;
663 if (regIdx
% 2 || regIdx
> 63)
665 Op
.Reg
.RegNum
= F128Regs
[regIdx
/ 2];
669 static bool MorphToVM512Reg(VEOperand
&Op
) {
670 unsigned Reg
= Op
.getReg();
671 unsigned regIdx
= Reg
- VE::VM0
;
672 if (regIdx
% 2 || regIdx
> 15)
674 Op
.Reg
.RegNum
= VM512Regs
[regIdx
/ 2];
678 static bool MorphToMISCReg(VEOperand
&Op
) {
679 const auto *ConstExpr
= dyn_cast
<MCConstantExpr
>(Op
.getImm());
682 unsigned regIdx
= ConstExpr
->getValue();
683 if (regIdx
> 31 || MISCRegs
[regIdx
] == VE::NoRegister
)
685 Op
.Kind
= k_Register
;
686 Op
.Reg
.RegNum
= MISCRegs
[regIdx
];
690 static std::unique_ptr
<VEOperand
>
691 MorphToMEMri(unsigned Base
, std::unique_ptr
<VEOperand
> Op
) {
692 const MCExpr
*Imm
= Op
->getImm();
693 Op
->Kind
= k_MemoryRegImm
;
695 Op
->Mem
.IndexReg
= 0;
696 Op
->Mem
.Index
= nullptr;
697 Op
->Mem
.Offset
= Imm
;
701 static std::unique_ptr
<VEOperand
>
702 MorphToMEMzi(std::unique_ptr
<VEOperand
> Op
) {
703 const MCExpr
*Imm
= Op
->getImm();
704 Op
->Kind
= k_MemoryZeroImm
;
706 Op
->Mem
.IndexReg
= 0;
707 Op
->Mem
.Index
= nullptr;
708 Op
->Mem
.Offset
= Imm
;
712 static std::unique_ptr
<VEOperand
>
713 MorphToMEMrri(unsigned Base
, unsigned Index
, std::unique_ptr
<VEOperand
> Op
) {
714 const MCExpr
*Imm
= Op
->getImm();
715 Op
->Kind
= k_MemoryRegRegImm
;
717 Op
->Mem
.IndexReg
= Index
;
718 Op
->Mem
.Index
= nullptr;
719 Op
->Mem
.Offset
= Imm
;
723 static std::unique_ptr
<VEOperand
>
724 MorphToMEMrii(unsigned Base
, const MCExpr
*Index
,
725 std::unique_ptr
<VEOperand
> Op
) {
726 const MCExpr
*Imm
= Op
->getImm();
727 Op
->Kind
= k_MemoryRegImmImm
;
729 Op
->Mem
.IndexReg
= 0;
730 Op
->Mem
.Index
= Index
;
731 Op
->Mem
.Offset
= Imm
;
735 static std::unique_ptr
<VEOperand
>
736 MorphToMEMzri(unsigned Index
, std::unique_ptr
<VEOperand
> Op
) {
737 const MCExpr
*Imm
= Op
->getImm();
738 Op
->Kind
= k_MemoryZeroRegImm
;
740 Op
->Mem
.IndexReg
= Index
;
741 Op
->Mem
.Index
= nullptr;
742 Op
->Mem
.Offset
= Imm
;
746 static std::unique_ptr
<VEOperand
>
747 MorphToMEMzii(const MCExpr
*Index
, std::unique_ptr
<VEOperand
> Op
) {
748 const MCExpr
*Imm
= Op
->getImm();
749 Op
->Kind
= k_MemoryZeroImmImm
;
751 Op
->Mem
.IndexReg
= 0;
752 Op
->Mem
.Index
= Index
;
753 Op
->Mem
.Offset
= Imm
;
758 } // end anonymous namespace
760 bool VEAsmParser::MatchAndEmitInstruction(SMLoc IDLoc
, unsigned &Opcode
,
761 OperandVector
&Operands
,
762 MCStreamer
&Out
, uint64_t &ErrorInfo
,
763 bool MatchingInlineAsm
) {
765 unsigned MatchResult
=
766 MatchInstructionImpl(Operands
, Inst
, ErrorInfo
, MatchingInlineAsm
);
767 switch (MatchResult
) {
770 Out
.emitInstruction(Inst
, getSTI());
773 case Match_MissingFeature
:
775 "instruction requires a CPU feature not currently enabled");
777 case Match_InvalidOperand
: {
778 SMLoc ErrorLoc
= IDLoc
;
779 if (ErrorInfo
!= ~0ULL) {
780 if (ErrorInfo
>= Operands
.size())
781 return Error(IDLoc
, "too few operands for instruction");
783 ErrorLoc
= ((VEOperand
&)*Operands
[ErrorInfo
]).getStartLoc();
784 if (ErrorLoc
== SMLoc())
788 return Error(ErrorLoc
, "invalid operand for instruction");
790 case Match_MnemonicFail
:
791 return Error(IDLoc
, "invalid instruction mnemonic");
793 llvm_unreachable("Implement any new match types added!");
796 bool VEAsmParser::ParseRegister(unsigned &RegNo
, SMLoc
&StartLoc
,
798 if (tryParseRegister(RegNo
, StartLoc
, EndLoc
) != MatchOperand_Success
)
799 return Error(StartLoc
, "invalid register name");
803 /// Parses a register name using a given matching function.
804 /// Checks for lowercase or uppercase if necessary.
805 int VEAsmParser::parseRegisterName(unsigned (*matchFn
)(StringRef
)) {
806 StringRef Name
= Parser
.getTok().getString();
808 int RegNum
= matchFn(Name
);
810 // GCC supports case insensitive register names. All of the VE registers
811 // are all lower case.
812 if (RegNum
== VE::NoRegister
) {
813 RegNum
= matchFn(Name
.lower());
819 /// Maps from the set of all register names to a register number.
820 /// \note Generated by TableGen.
821 static unsigned MatchRegisterName(StringRef Name
);
823 /// Maps from the set of all alternative registernames to a register number.
824 /// \note Generated by TableGen.
825 static unsigned MatchRegisterAltName(StringRef Name
);
828 VEAsmParser::tryParseRegister(unsigned &RegNo
, SMLoc
&StartLoc
, SMLoc
&EndLoc
) {
829 const AsmToken Tok
= Parser
.getTok();
830 StartLoc
= Tok
.getLoc();
831 EndLoc
= Tok
.getEndLoc();
833 if (getLexer().getKind() != AsmToken::Percent
)
834 return MatchOperand_NoMatch
;
837 RegNo
= parseRegisterName(&MatchRegisterName
);
838 if (RegNo
== VE::NoRegister
)
839 RegNo
= parseRegisterName(&MatchRegisterAltName
);
841 if (RegNo
!= VE::NoRegister
) {
843 return MatchOperand_Success
;
846 getLexer().UnLex(Tok
);
847 return MatchOperand_NoMatch
;
850 static StringRef
parseCC(StringRef Name
, unsigned Prefix
, unsigned Suffix
,
851 bool IntegerCC
, bool OmitCC
, SMLoc NameLoc
,
852 OperandVector
*Operands
) {
853 // Parse instructions with a conditional code. For example, 'bne' is
854 // converted into two operands 'b' and 'ne'.
855 StringRef Cond
= Name
.slice(Prefix
, Suffix
);
856 VECC::CondCode CondCode
=
857 IntegerCC
? stringToVEICondCode(Cond
) : stringToVEFCondCode(Cond
);
859 // If OmitCC is enabled, CC_AT and CC_AF is treated as a part of mnemonic.
860 if (CondCode
!= VECC::UNKNOWN
&&
861 (!OmitCC
|| (CondCode
!= VECC::CC_AT
&& CondCode
!= VECC::CC_AF
))) {
862 StringRef SuffixStr
= Name
.substr(Suffix
);
864 Name
= Name
.slice(0, Prefix
);
865 Operands
->push_back(VEOperand::CreateToken(Name
, NameLoc
));
867 SMLoc CondLoc
= SMLoc::getFromPointer(NameLoc
.getPointer() + Prefix
);
868 SMLoc SuffixLoc
= SMLoc::getFromPointer(NameLoc
.getPointer() + Suffix
);
869 Operands
->push_back(VEOperand::CreateCCOp(CondCode
, CondLoc
, SuffixLoc
));
870 // push suffix like ".l.t"
871 if (!SuffixStr
.empty())
872 Operands
->push_back(VEOperand::CreateToken(SuffixStr
, SuffixLoc
));
874 Operands
->push_back(VEOperand::CreateToken(Name
, NameLoc
));
879 static StringRef
parseRD(StringRef Name
, unsigned Prefix
, SMLoc NameLoc
,
880 OperandVector
*Operands
) {
881 // Parse instructions with a conditional code. For example, 'cvt.w.d.sx.rz'
882 // is converted into two operands 'cvt.w.d.sx' and '.rz'.
883 StringRef RD
= Name
.substr(Prefix
);
884 VERD::RoundingMode RoundingMode
= stringToVERD(RD
);
886 if (RoundingMode
!= VERD::UNKNOWN
) {
887 Name
= Name
.slice(0, Prefix
);
888 // push 1st like `cvt.w.d.sx`
889 Operands
->push_back(VEOperand::CreateToken(Name
, NameLoc
));
891 SMLoc::getFromPointer(NameLoc
.getPointer() + (RD
.data() - Name
.data()));
893 SMLoc::getFromPointer(NameLoc
.getPointer() + (RD
.end() - Name
.data()));
894 // push $round if it has rounding mode
896 VEOperand::CreateRDOp(RoundingMode
, SuffixLoc
, SuffixEnd
));
898 Operands
->push_back(VEOperand::CreateToken(Name
, NameLoc
));
903 // Split the mnemonic into ASM operand, conditional code and instruction
904 // qualifier (half-word, byte).
905 StringRef
VEAsmParser::splitMnemonic(StringRef Name
, SMLoc NameLoc
,
906 OperandVector
*Operands
) {
907 // Create the leading tokens for the mnemonic
908 StringRef Mnemonic
= Name
;
910 if (Name
[0] == 'b') {
911 // Match b?? or br??.
913 size_t Next
= Name
.find('.');
914 // Adjust position of CondCode.
915 if (Name
.size() > 1 && Name
[1] == 'r')
919 if (Next
+ 1 < Name
.size() &&
920 (Name
[Next
+ 1] == 'd' || Name
[Next
+ 1] == 's'))
922 Mnemonic
= parseCC(Name
, Start
, Next
, ICC
, true, NameLoc
, Operands
);
923 } else if (Name
.startswith("cmov.l.") || Name
.startswith("cmov.w.") ||
924 Name
.startswith("cmov.d.") || Name
.startswith("cmov.s.")) {
925 bool ICC
= Name
[5] == 'l' || Name
[5] == 'w';
926 Mnemonic
= parseCC(Name
, 7, Name
.size(), ICC
, false, NameLoc
, Operands
);
927 } else if (Name
.startswith("cvt.w.d.sx") || Name
.startswith("cvt.w.d.zx") ||
928 Name
.startswith("cvt.w.s.sx") || Name
.startswith("cvt.w.s.zx")) {
929 Mnemonic
= parseRD(Name
, 10, NameLoc
, Operands
);
930 } else if (Name
.startswith("cvt.l.d")) {
931 Mnemonic
= parseRD(Name
, 7, NameLoc
, Operands
);
932 } else if (Name
.startswith("vcvt.w.d.sx") || Name
.startswith("vcvt.w.d.zx") ||
933 Name
.startswith("vcvt.w.s.sx") || Name
.startswith("vcvt.w.s.zx")) {
934 Mnemonic
= parseRD(Name
, 11, NameLoc
, Operands
);
935 } else if (Name
.startswith("vcvt.l.d")) {
936 Mnemonic
= parseRD(Name
, 8, NameLoc
, Operands
);
937 } else if (Name
.startswith("pvcvt.w.s.lo") ||
938 Name
.startswith("pvcvt.w.s.up")) {
939 Mnemonic
= parseRD(Name
, 12, NameLoc
, Operands
);
940 } else if (Name
.startswith("pvcvt.w.s")) {
941 Mnemonic
= parseRD(Name
, 9, NameLoc
, Operands
);
942 } else if (Name
.startswith("vfmk.l.") || Name
.startswith("vfmk.w.") ||
943 Name
.startswith("vfmk.d.") || Name
.startswith("vfmk.s.")) {
944 bool ICC
= Name
[5] == 'l' || Name
[5] == 'w' ? true : false;
945 Mnemonic
= parseCC(Name
, 7, Name
.size(), ICC
, true, NameLoc
, Operands
);
946 } else if (Name
.startswith("pvfmk.w.lo.") || Name
.startswith("pvfmk.w.up.") ||
947 Name
.startswith("pvfmk.s.lo.") || Name
.startswith("pvfmk.s.up.")) {
948 bool ICC
= Name
[6] == 'l' || Name
[6] == 'w' ? true : false;
949 Mnemonic
= parseCC(Name
, 11, Name
.size(), ICC
, true, NameLoc
, Operands
);
951 Operands
->push_back(VEOperand::CreateToken(Mnemonic
, NameLoc
));
957 static void applyMnemonicAliases(StringRef
&Mnemonic
,
958 const FeatureBitset
&Features
,
961 bool VEAsmParser::ParseInstruction(ParseInstructionInfo
&Info
, StringRef Name
,
962 SMLoc NameLoc
, OperandVector
&Operands
) {
963 // If the target architecture uses MnemonicAlias, call it here to parse
964 // operands correctly.
965 applyMnemonicAliases(Name
, getAvailableFeatures(), 0);
967 // Split name to first token and the rest, e.g. "bgt.l.t" to "b", "gt", and
968 // ".l.t". We treat "b" as a mnemonic, "gt" as first operand, and ".l.t"
969 // as second operand.
970 StringRef Mnemonic
= splitMnemonic(Name
, NameLoc
, &Operands
);
972 if (getLexer().isNot(AsmToken::EndOfStatement
)) {
973 // Read the first operand.
974 if (parseOperand(Operands
, Mnemonic
) != MatchOperand_Success
) {
975 SMLoc Loc
= getLexer().getLoc();
976 return Error(Loc
, "unexpected token");
979 while (getLexer().is(AsmToken::Comma
)) {
980 Parser
.Lex(); // Eat the comma.
981 // Parse and remember the operand.
982 if (parseOperand(Operands
, Mnemonic
) != MatchOperand_Success
) {
983 SMLoc Loc
= getLexer().getLoc();
984 return Error(Loc
, "unexpected token");
988 if (getLexer().isNot(AsmToken::EndOfStatement
)) {
989 SMLoc Loc
= getLexer().getLoc();
990 return Error(Loc
, "unexpected token");
992 Parser
.Lex(); // Consume the EndOfStatement.
996 bool VEAsmParser::ParseDirective(AsmToken DirectiveID
) {
997 // Let the MC layer to handle other directives.
1001 /// Extract \code @lo32/@hi32/etc \endcode modifier from expression.
1002 /// Recursively scan the expression and check for VK_VE_HI32/LO32/etc
1003 /// symbol variants. If all symbols with modifier use the same
1004 /// variant, return the corresponding VEMCExpr::VariantKind,
1005 /// and a modified expression using the default symbol variant.
1006 /// Otherwise, return NULL.
1008 VEAsmParser::extractModifierFromExpr(const MCExpr
*E
,
1009 VEMCExpr::VariantKind
&Variant
) {
1010 MCContext
&Context
= getParser().getContext();
1011 Variant
= VEMCExpr::VK_VE_None
;
1013 switch (E
->getKind()) {
1014 case MCExpr::Target
:
1015 case MCExpr::Constant
:
1018 case MCExpr::SymbolRef
: {
1019 const MCSymbolRefExpr
*SRE
= cast
<MCSymbolRefExpr
>(E
);
1021 switch (SRE
->getKind()) {
1022 case MCSymbolRefExpr::VK_None
:
1023 // Use VK_VE_REFLONG to a symbol without modifiers.
1024 Variant
= VEMCExpr::VK_VE_REFLONG
;
1026 case MCSymbolRefExpr::VK_VE_HI32
:
1027 Variant
= VEMCExpr::VK_VE_HI32
;
1029 case MCSymbolRefExpr::VK_VE_LO32
:
1030 Variant
= VEMCExpr::VK_VE_LO32
;
1032 case MCSymbolRefExpr::VK_VE_PC_HI32
:
1033 Variant
= VEMCExpr::VK_VE_PC_HI32
;
1035 case MCSymbolRefExpr::VK_VE_PC_LO32
:
1036 Variant
= VEMCExpr::VK_VE_PC_LO32
;
1038 case MCSymbolRefExpr::VK_VE_GOT_HI32
:
1039 Variant
= VEMCExpr::VK_VE_GOT_HI32
;
1041 case MCSymbolRefExpr::VK_VE_GOT_LO32
:
1042 Variant
= VEMCExpr::VK_VE_GOT_LO32
;
1044 case MCSymbolRefExpr::VK_VE_GOTOFF_HI32
:
1045 Variant
= VEMCExpr::VK_VE_GOTOFF_HI32
;
1047 case MCSymbolRefExpr::VK_VE_GOTOFF_LO32
:
1048 Variant
= VEMCExpr::VK_VE_GOTOFF_LO32
;
1050 case MCSymbolRefExpr::VK_VE_PLT_HI32
:
1051 Variant
= VEMCExpr::VK_VE_PLT_HI32
;
1053 case MCSymbolRefExpr::VK_VE_PLT_LO32
:
1054 Variant
= VEMCExpr::VK_VE_PLT_LO32
;
1056 case MCSymbolRefExpr::VK_VE_TLS_GD_HI32
:
1057 Variant
= VEMCExpr::VK_VE_TLS_GD_HI32
;
1059 case MCSymbolRefExpr::VK_VE_TLS_GD_LO32
:
1060 Variant
= VEMCExpr::VK_VE_TLS_GD_LO32
;
1062 case MCSymbolRefExpr::VK_VE_TPOFF_HI32
:
1063 Variant
= VEMCExpr::VK_VE_TPOFF_HI32
;
1065 case MCSymbolRefExpr::VK_VE_TPOFF_LO32
:
1066 Variant
= VEMCExpr::VK_VE_TPOFF_LO32
;
1072 return MCSymbolRefExpr::create(&SRE
->getSymbol(), Context
);
1075 case MCExpr::Unary
: {
1076 const MCUnaryExpr
*UE
= cast
<MCUnaryExpr
>(E
);
1077 const MCExpr
*Sub
= extractModifierFromExpr(UE
->getSubExpr(), Variant
);
1080 return MCUnaryExpr::create(UE
->getOpcode(), Sub
, Context
);
1083 case MCExpr::Binary
: {
1084 const MCBinaryExpr
*BE
= cast
<MCBinaryExpr
>(E
);
1085 VEMCExpr::VariantKind LHSVariant
, RHSVariant
;
1086 const MCExpr
*LHS
= extractModifierFromExpr(BE
->getLHS(), LHSVariant
);
1087 const MCExpr
*RHS
= extractModifierFromExpr(BE
->getRHS(), RHSVariant
);
1097 if (LHSVariant
== VEMCExpr::VK_VE_None
)
1098 Variant
= RHSVariant
;
1099 else if (RHSVariant
== VEMCExpr::VK_VE_None
)
1100 Variant
= LHSVariant
;
1101 else if (LHSVariant
== RHSVariant
)
1102 Variant
= LHSVariant
;
1106 return MCBinaryExpr::create(BE
->getOpcode(), LHS
, RHS
, Context
);
1110 llvm_unreachable("Invalid expression kind!");
1113 const MCExpr
*VEAsmParser::fixupVariantKind(const MCExpr
*E
) {
1114 MCContext
&Context
= getParser().getContext();
1116 switch (E
->getKind()) {
1117 case MCExpr::Target
:
1118 case MCExpr::Constant
:
1119 case MCExpr::SymbolRef
:
1122 case MCExpr::Unary
: {
1123 const MCUnaryExpr
*UE
= cast
<MCUnaryExpr
>(E
);
1124 const MCExpr
*Sub
= fixupVariantKind(UE
->getSubExpr());
1125 if (Sub
== UE
->getSubExpr())
1127 return MCUnaryExpr::create(UE
->getOpcode(), Sub
, Context
);
1130 case MCExpr::Binary
: {
1131 const MCBinaryExpr
*BE
= cast
<MCBinaryExpr
>(E
);
1132 const MCExpr
*LHS
= fixupVariantKind(BE
->getLHS());
1133 const MCExpr
*RHS
= fixupVariantKind(BE
->getRHS());
1134 if (LHS
== BE
->getLHS() && RHS
== BE
->getRHS())
1136 return MCBinaryExpr::create(BE
->getOpcode(), LHS
, RHS
, Context
);
1140 llvm_unreachable("Invalid expression kind!");
1143 /// ParseExpression. This differs from the default "parseExpression" in that
1144 /// it handles modifiers.
1145 bool VEAsmParser::parseExpression(const MCExpr
*&EVal
) {
1146 // Handle \code symbol @lo32/@hi32/etc \endcode.
1147 if (getParser().parseExpression(EVal
))
1150 // Convert MCSymbolRefExpr with VK_* to MCExpr with VK_*.
1151 EVal
= fixupVariantKind(EVal
);
1152 VEMCExpr::VariantKind Variant
;
1153 const MCExpr
*E
= extractModifierFromExpr(EVal
, Variant
);
1155 EVal
= VEMCExpr::create(Variant
, E
, getParser().getContext());
1160 OperandMatchResultTy
VEAsmParser::parseMEMOperand(OperandVector
&Operands
) {
1161 LLVM_DEBUG(dbgs() << "parseMEMOperand\n");
1162 const AsmToken
&Tok
= Parser
.getTok();
1163 SMLoc S
= Tok
.getLoc();
1164 SMLoc E
= Tok
.getEndLoc();
1169 // disp(index, base)
1174 std::unique_ptr
<VEOperand
> Offset
;
1175 switch (getLexer().getKind()) {
1177 return MatchOperand_NoMatch
;
1179 case AsmToken::Minus
:
1180 case AsmToken::Integer
:
1182 case AsmToken::Identifier
: {
1184 if (!parseExpression(EVal
))
1185 Offset
= VEOperand::CreateImm(EVal
, S
, E
);
1187 return MatchOperand_NoMatch
;
1191 case AsmToken::LParen
:
1194 VEOperand::CreateImm(MCConstantExpr::create(0, getContext()), S
, E
);
1198 switch (getLexer().getKind()) {
1200 return MatchOperand_ParseFail
;
1202 case AsmToken::EndOfStatement
:
1203 Operands
.push_back(VEOperand::MorphToMEMzii(
1204 MCConstantExpr::create(0, getContext()), std::move(Offset
)));
1205 return MatchOperand_Success
;
1207 case AsmToken::LParen
:
1208 Parser
.Lex(); // Eat the (
1212 const MCExpr
*IndexValue
= nullptr;
1213 unsigned IndexReg
= 0;
1215 switch (getLexer().getKind()) {
1217 if (ParseRegister(IndexReg
, S
, E
))
1218 return MatchOperand_ParseFail
;
1221 case AsmToken::Minus
:
1222 case AsmToken::Integer
:
1224 if (getParser().parseExpression(IndexValue
, E
))
1225 return MatchOperand_ParseFail
;
1228 case AsmToken::Comma
:
1230 IndexValue
= MCConstantExpr::create(0, getContext());
1234 switch (getLexer().getKind()) {
1236 return MatchOperand_ParseFail
;
1238 case AsmToken::RParen
:
1239 Parser
.Lex(); // Eat the )
1241 IndexValue
? VEOperand::MorphToMEMzii(IndexValue
, std::move(Offset
))
1242 : VEOperand::MorphToMEMzri(IndexReg
, std::move(Offset
)));
1243 return MatchOperand_Success
;
1245 case AsmToken::Comma
:
1246 Parser
.Lex(); // Eat the ,
1250 unsigned BaseReg
= 0;
1251 if (ParseRegister(BaseReg
, S
, E
))
1252 return MatchOperand_ParseFail
;
1254 if (!Parser
.getTok().is(AsmToken::RParen
))
1255 return MatchOperand_ParseFail
;
1257 Parser
.Lex(); // Eat the )
1260 ? VEOperand::MorphToMEMrii(BaseReg
, IndexValue
, std::move(Offset
))
1261 : VEOperand::MorphToMEMrri(BaseReg
, IndexReg
, std::move(Offset
)));
1263 return MatchOperand_Success
;
1266 OperandMatchResultTy
VEAsmParser::parseMEMAsOperand(OperandVector
&Operands
) {
1267 LLVM_DEBUG(dbgs() << "parseMEMAsOperand\n");
1268 const AsmToken
&Tok
= Parser
.getTok();
1269 SMLoc S
= Tok
.getLoc();
1270 SMLoc E
= Tok
.getEndLoc();
1280 unsigned BaseReg
= VE::NoRegister
;
1281 std::unique_ptr
<VEOperand
> Offset
;
1282 switch (getLexer().getKind()) {
1284 return MatchOperand_NoMatch
;
1286 case AsmToken::Minus
:
1287 case AsmToken::Integer
:
1289 case AsmToken::Identifier
: {
1291 if (!parseExpression(EVal
))
1292 Offset
= VEOperand::CreateImm(EVal
, S
, E
);
1294 return MatchOperand_NoMatch
;
1298 case AsmToken::Percent
:
1299 if (ParseRegister(BaseReg
, S
, E
))
1300 return MatchOperand_NoMatch
;
1302 VEOperand::CreateImm(MCConstantExpr::create(0, getContext()), S
, E
);
1305 case AsmToken::LParen
:
1308 VEOperand::CreateImm(MCConstantExpr::create(0, getContext()), S
, E
);
1312 switch (getLexer().getKind()) {
1314 return MatchOperand_ParseFail
;
1316 case AsmToken::EndOfStatement
:
1317 case AsmToken::Comma
:
1318 Operands
.push_back(BaseReg
!= VE::NoRegister
1319 ? VEOperand::MorphToMEMri(BaseReg
, std::move(Offset
))
1320 : VEOperand::MorphToMEMzi(std::move(Offset
)));
1321 return MatchOperand_Success
;
1323 case AsmToken::LParen
:
1324 if (BaseReg
!= VE::NoRegister
)
1325 return MatchOperand_ParseFail
;
1326 Parser
.Lex(); // Eat the (
1330 switch (getLexer().getKind()) {
1332 if (ParseRegister(BaseReg
, S
, E
))
1333 return MatchOperand_ParseFail
;
1336 case AsmToken::Comma
:
1337 Parser
.Lex(); // Eat the ,
1338 if (ParseRegister(BaseReg
, S
, E
))
1339 return MatchOperand_ParseFail
;
1342 case AsmToken::RParen
:
1346 if (!Parser
.getTok().is(AsmToken::RParen
))
1347 return MatchOperand_ParseFail
;
1349 Parser
.Lex(); // Eat the )
1350 Operands
.push_back(BaseReg
!= VE::NoRegister
1351 ? VEOperand::MorphToMEMri(BaseReg
, std::move(Offset
))
1352 : VEOperand::MorphToMEMzi(std::move(Offset
)));
1354 return MatchOperand_Success
;
1357 OperandMatchResultTy
VEAsmParser::parseMImmOperand(OperandVector
&Operands
) {
1358 LLVM_DEBUG(dbgs() << "parseMImmOperand\n");
1360 // Parsing "(" + number + ")0/1"
1361 const AsmToken Tok1
= Parser
.getTok();
1362 if (!Tok1
.is(AsmToken::LParen
))
1363 return MatchOperand_NoMatch
;
1365 Parser
.Lex(); // Eat the '('.
1367 const AsmToken Tok2
= Parser
.getTok();
1370 if (!Tok2
.is(AsmToken::Integer
) || getParser().parseExpression(EVal
, E
)) {
1371 getLexer().UnLex(Tok1
);
1372 return MatchOperand_NoMatch
;
1375 const AsmToken Tok3
= Parser
.getTok();
1376 if (!Tok3
.is(AsmToken::RParen
)) {
1377 getLexer().UnLex(Tok2
);
1378 getLexer().UnLex(Tok1
);
1379 return MatchOperand_NoMatch
;
1381 Parser
.Lex(); // Eat the ')'.
1383 const AsmToken
&Tok4
= Parser
.getTok();
1384 StringRef Suffix
= Tok4
.getString();
1385 if (Suffix
!= "1" && Suffix
!= "0") {
1386 getLexer().UnLex(Tok3
);
1387 getLexer().UnLex(Tok2
);
1388 getLexer().UnLex(Tok1
);
1389 return MatchOperand_NoMatch
;
1391 Parser
.Lex(); // Eat the value.
1392 SMLoc EndLoc
= SMLoc::getFromPointer(Suffix
.end());
1394 VEOperand::CreateMImm(EVal
, Suffix
== "0", Tok1
.getLoc(), EndLoc
));
1395 return MatchOperand_Success
;
1398 OperandMatchResultTy
VEAsmParser::parseOperand(OperandVector
&Operands
,
1399 StringRef Mnemonic
) {
1400 LLVM_DEBUG(dbgs() << "parseOperand\n");
1401 OperandMatchResultTy ResTy
= MatchOperandParserImpl(Operands
, Mnemonic
);
1403 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1404 // there was a match, but an error occurred, in which case, just return that
1405 // the operand parsing failed.
1406 if (ResTy
== MatchOperand_Success
|| ResTy
== MatchOperand_ParseFail
)
1409 switch (getLexer().getKind()) {
1410 case AsmToken::LParen
: {
1411 // Parsing "(" + %vreg + ", " + %vreg + ")"
1412 const AsmToken Tok1
= Parser
.getTok();
1413 Parser
.Lex(); // Eat the '('.
1417 if (tryParseRegister(RegNo1
, S1
, E1
) != MatchOperand_Success
) {
1418 getLexer().UnLex(Tok1
);
1419 return MatchOperand_NoMatch
;
1422 if (!Parser
.getTok().is(AsmToken::Comma
))
1423 return MatchOperand_ParseFail
;
1424 Parser
.Lex(); // Eat the ','.
1428 if (tryParseRegister(RegNo2
, S2
, E2
) != MatchOperand_Success
)
1429 return MatchOperand_ParseFail
;
1431 if (!Parser
.getTok().is(AsmToken::RParen
))
1432 return MatchOperand_ParseFail
;
1434 Operands
.push_back(VEOperand::CreateToken(Tok1
.getString(), Tok1
.getLoc()));
1435 Operands
.push_back(VEOperand::CreateReg(RegNo1
, S1
, E1
));
1436 Operands
.push_back(VEOperand::CreateReg(RegNo2
, S2
, E2
));
1437 Operands
.push_back(VEOperand::CreateToken(Parser
.getTok().getString(),
1438 Parser
.getTok().getLoc()));
1439 Parser
.Lex(); // Eat the ')'.
1443 std::unique_ptr
<VEOperand
> Op
;
1444 ResTy
= parseVEAsmOperand(Op
);
1445 if (ResTy
!= MatchOperand_Success
|| !Op
)
1446 return MatchOperand_ParseFail
;
1448 // Push the parsed operand into the list of operands
1449 Operands
.push_back(std::move(Op
));
1451 if (!Parser
.getTok().is(AsmToken::LParen
))
1454 // Parsing %vec-reg + "(" + %sclar-reg/number + ")"
1455 std::unique_ptr
<VEOperand
> Op1
= VEOperand::CreateToken(
1456 Parser
.getTok().getString(), Parser
.getTok().getLoc());
1457 Parser
.Lex(); // Eat the '('.
1459 std::unique_ptr
<VEOperand
> Op2
;
1460 ResTy
= parseVEAsmOperand(Op2
);
1461 if (ResTy
!= MatchOperand_Success
|| !Op2
)
1462 return MatchOperand_ParseFail
;
1464 if (!Parser
.getTok().is(AsmToken::RParen
))
1465 return MatchOperand_ParseFail
;
1467 Operands
.push_back(std::move(Op1
));
1468 Operands
.push_back(std::move(Op2
));
1469 Operands
.push_back(VEOperand::CreateToken(Parser
.getTok().getString(),
1470 Parser
.getTok().getLoc()));
1471 Parser
.Lex(); // Eat the ')'.
1476 return MatchOperand_Success
;
1479 OperandMatchResultTy
1480 VEAsmParser::parseVEAsmOperand(std::unique_ptr
<VEOperand
> &Op
) {
1481 LLVM_DEBUG(dbgs() << "parseVEAsmOperand\n");
1482 SMLoc S
= Parser
.getTok().getLoc();
1483 SMLoc E
= SMLoc::getFromPointer(Parser
.getTok().getLoc().getPointer() - 1);
1487 switch (getLexer().getKind()) {
1491 case AsmToken::Percent
:
1493 if (tryParseRegister(RegNo
, S
, E
) == MatchOperand_Success
)
1494 Op
= VEOperand::CreateReg(RegNo
, S
, E
);
1497 case AsmToken::Minus
:
1498 case AsmToken::Integer
:
1500 case AsmToken::Identifier
:
1501 if (!parseExpression(EVal
))
1502 Op
= VEOperand::CreateImm(EVal
, S
, E
);
1505 return (Op
) ? MatchOperand_Success
: MatchOperand_ParseFail
;
1508 // Force static initialization.
1509 extern "C" LLVM_EXTERNAL_VISIBILITY
void LLVMInitializeVEAsmParser() {
1510 RegisterMCAsmParser
<VEAsmParser
> A(getTheVETarget());
1513 #define GET_REGISTER_MATCHER
1514 #define GET_MATCHER_IMPLEMENTATION
1515 #include "VEGenAsmMatcher.inc"
1517 unsigned VEAsmParser::validateTargetOperandClass(MCParsedAsmOperand
&GOp
,
1519 VEOperand
&Op
= (VEOperand
&)GOp
;
1521 // VE uses identical register name for all registers like both
1522 // F32 and I32 uses "%s23". Need to convert the name of them
1528 if (Op
.isReg() && VEOperand::MorphToF32Reg(Op
))
1529 return MCTargetAsmParser::Match_Success
;
1532 if (Op
.isReg() && VEOperand::MorphToI32Reg(Op
))
1533 return MCTargetAsmParser::Match_Success
;
1536 if (Op
.isReg() && VEOperand::MorphToF128Reg(Op
))
1537 return MCTargetAsmParser::Match_Success
;
1540 if (Op
.isReg() && VEOperand::MorphToVM512Reg(Op
))
1541 return MCTargetAsmParser::Match_Success
;
1544 if (Op
.isImm() && VEOperand::MorphToMISCReg(Op
))
1545 return MCTargetAsmParser::Match_Success
;
1548 return Match_InvalidOperand
;