1 //===-- MipsAsmParser.cpp - Parse Mips assembly to MCInst instructions ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/MipsMCTargetDesc.h"
11 #include "MipsRegisterInfo.h"
12 #include "MipsTargetStreamer.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCParser/MCAsmLexer.h"
18 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/MC/MCTargetAsmParser.h"
23 #include "llvm/Support/TargetRegistry.h"
24 #include "llvm/ADT/APInt.h"
33 class MipsAssemblerOptions {
35 MipsAssemblerOptions() : aTReg(1), reorder(true), macro(true) {}
37 unsigned getATRegNum() { return aTReg; }
38 bool setATReg(unsigned Reg);
40 bool isReorder() { return reorder; }
41 void setReorder() { reorder = true; }
42 void setNoreorder() { reorder = false; }
44 bool isMacro() { return macro; }
45 void setMacro() { macro = true; }
46 void setNomacro() { macro = false; }
56 class MipsAsmParser : public MCTargetAsmParser {
58 MipsTargetStreamer &getTargetStreamer() {
59 MCTargetStreamer &TS = Parser.getStreamer().getTargetStreamer();
60 return static_cast<MipsTargetStreamer &>(TS);
65 MipsAssemblerOptions Options;
66 bool hasConsumedDollar;
68 #define GET_ASSEMBLER_HEADER
69 #include "MipsGenAsmMatcher.inc"
71 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
72 SmallVectorImpl<MCParsedAsmOperand *> &Operands,
73 MCStreamer &Out, unsigned &ErrorInfo,
74 bool MatchingInlineAsm);
76 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
78 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
80 SmallVectorImpl<MCParsedAsmOperand *> &Operands);
82 bool ParseDirective(AsmToken DirectiveID);
84 MipsAsmParser::OperandMatchResultTy
85 parseRegs(SmallVectorImpl<MCParsedAsmOperand *> &Operands, int RegKind);
87 MipsAsmParser::OperandMatchResultTy
88 parseMSARegs(SmallVectorImpl<MCParsedAsmOperand *> &Operands, int RegKind);
90 MipsAsmParser::OperandMatchResultTy
91 parseMSACtrlRegs(SmallVectorImpl<MCParsedAsmOperand *> &Operands,
94 MipsAsmParser::OperandMatchResultTy
95 parseMemOperand(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
97 bool parsePtrReg(SmallVectorImpl<MCParsedAsmOperand *> &Operands,
100 MipsAsmParser::OperandMatchResultTy
101 parsePtrReg(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
103 MipsAsmParser::OperandMatchResultTy
104 parseGPR32(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
106 MipsAsmParser::OperandMatchResultTy
107 parseGPR64(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
109 MipsAsmParser::OperandMatchResultTy
110 parseHWRegs(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
112 MipsAsmParser::OperandMatchResultTy
113 parseCCRRegs(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
115 MipsAsmParser::OperandMatchResultTy
116 parseAFGR64Regs(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
118 MipsAsmParser::OperandMatchResultTy
119 parseFGR64Regs(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
121 MipsAsmParser::OperandMatchResultTy
122 parseFGR32Regs(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
124 MipsAsmParser::OperandMatchResultTy
125 parseFGRH32Regs(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
127 MipsAsmParser::OperandMatchResultTy
128 parseFCCRegs(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
130 MipsAsmParser::OperandMatchResultTy
131 parseACC64DSP(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
133 MipsAsmParser::OperandMatchResultTy
134 parseLO32DSP(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
136 MipsAsmParser::OperandMatchResultTy
137 parseHI32DSP(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
139 MipsAsmParser::OperandMatchResultTy
140 parseCOP2(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
142 MipsAsmParser::OperandMatchResultTy
143 parseMSA128BRegs(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
145 MipsAsmParser::OperandMatchResultTy
146 parseMSA128HRegs(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
148 MipsAsmParser::OperandMatchResultTy
149 parseMSA128WRegs(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
151 MipsAsmParser::OperandMatchResultTy
152 parseMSA128DRegs(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
154 MipsAsmParser::OperandMatchResultTy
155 parseMSA128CtrlRegs(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
157 MipsAsmParser::OperandMatchResultTy
158 parseInvNum(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
160 bool searchSymbolAlias(SmallVectorImpl<MCParsedAsmOperand *> &Operands,
163 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand *> &,
166 int tryParseRegister(bool is64BitReg);
168 bool tryParseRegisterOperand(SmallVectorImpl<MCParsedAsmOperand *> &Operands,
171 bool needsExpansion(MCInst &Inst);
173 void expandInstruction(MCInst &Inst, SMLoc IDLoc,
174 SmallVectorImpl<MCInst> &Instructions);
175 void expandLoadImm(MCInst &Inst, SMLoc IDLoc,
176 SmallVectorImpl<MCInst> &Instructions);
177 void expandLoadAddressImm(MCInst &Inst, SMLoc IDLoc,
178 SmallVectorImpl<MCInst> &Instructions);
179 void expandLoadAddressReg(MCInst &Inst, SMLoc IDLoc,
180 SmallVectorImpl<MCInst> &Instructions);
181 void expandMemInst(MCInst &Inst, SMLoc IDLoc,
182 SmallVectorImpl<MCInst> &Instructions, bool isLoad,
184 bool reportParseError(StringRef ErrorMsg);
186 bool parseMemOffset(const MCExpr *&Res, bool isParenExpr);
187 bool parseRelocOperand(const MCExpr *&Res);
189 const MCExpr *evaluateRelocExpr(const MCExpr *Expr, StringRef RelocStr);
191 bool isEvaluated(const MCExpr *Expr);
192 bool parseDirectiveSet();
193 bool parseDirectiveMipsHackStocg();
194 bool parseDirectiveMipsHackELFFlags();
196 bool parseSetAtDirective();
197 bool parseSetNoAtDirective();
198 bool parseSetMacroDirective();
199 bool parseSetNoMacroDirective();
200 bool parseSetReorderDirective();
201 bool parseSetNoReorderDirective();
203 bool parseSetAssignment();
205 bool parseDirectiveWord(unsigned Size, SMLoc L);
206 bool parseDirectiveGpWord();
208 MCSymbolRefExpr::VariantKind getVariantKind(StringRef Symbol);
210 bool isMips64() const {
211 return (STI.getFeatureBits() & Mips::FeatureMips64) != 0;
214 bool isFP64() const {
215 return (STI.getFeatureBits() & Mips::FeatureFP64Bit) != 0;
218 bool isN64() const { return STI.getFeatureBits() & Mips::FeatureN64; }
220 int matchRegisterName(StringRef Symbol, bool is64BitReg);
222 int matchCPURegisterName(StringRef Symbol);
224 int matchRegisterByNumber(unsigned RegNum, unsigned RegClass);
226 int matchFPURegisterName(StringRef Name);
228 int matchFCCRegisterName(StringRef Name);
230 int matchACRegisterName(StringRef Name);
232 int matchMSA128RegisterName(StringRef Name);
234 int matchMSA128CtrlRegisterName(StringRef Name);
236 int regKindToRegClass(int RegKind);
238 unsigned getReg(int RC, int RegNo);
242 bool processInstruction(MCInst &Inst, SMLoc IDLoc,
243 SmallVectorImpl<MCInst> &Instructions);
245 // Helper function that checks if the value of a vector index is within the
246 // boundaries of accepted values for each RegisterKind
247 // Example: INSERT.B $w0[n], $1 => 16 > n >= 0
248 bool validateMSAIndex(int Val, int RegKind);
251 MipsAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
252 const MCInstrInfo &MII)
253 : MCTargetAsmParser(), STI(sti), Parser(parser),
254 hasConsumedDollar(false) {
255 // Initialize the set of available features.
256 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
259 MCAsmParser &getParser() const { return Parser; }
260 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
266 /// MipsOperand - Instances of this class represent a parsed Mips machine
268 class MipsOperand : public MCParsedAsmOperand {
305 MipsOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
333 SMLoc StartLoc, EndLoc;
336 void addRegOperands(MCInst &Inst, unsigned N) const {
337 assert(N == 1 && "Invalid number of operands!");
338 Inst.addOperand(MCOperand::CreateReg(getReg()));
341 void addPtrRegOperands(MCInst &Inst, unsigned N) const {
342 assert(N == 1 && "Invalid number of operands!");
343 Inst.addOperand(MCOperand::CreateReg(getPtrReg()));
346 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
347 // Add as immediate when possible. Null MCExpr = 0.
349 Inst.addOperand(MCOperand::CreateImm(0));
350 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
351 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
353 Inst.addOperand(MCOperand::CreateExpr(Expr));
356 void addImmOperands(MCInst &Inst, unsigned N) const {
357 assert(N == 1 && "Invalid number of operands!");
358 const MCExpr *Expr = getImm();
362 void addMemOperands(MCInst &Inst, unsigned N) const {
363 assert(N == 2 && "Invalid number of operands!");
365 Inst.addOperand(MCOperand::CreateReg(getMemBase()));
367 const MCExpr *Expr = getMemOff();
371 bool isReg() const { return Kind == k_Register; }
372 bool isImm() const { return Kind == k_Immediate; }
373 bool isToken() const { return Kind == k_Token; }
374 bool isMem() const { return Kind == k_Memory; }
375 bool isPtrReg() const { return Kind == k_PtrReg; }
376 bool isInvNum() const { return Kind == k_Immediate; }
378 StringRef getToken() const {
379 assert(Kind == k_Token && "Invalid access!");
380 return StringRef(Tok.Data, Tok.Length);
383 unsigned getReg() const {
384 assert((Kind == k_Register) && "Invalid access!");
388 unsigned getPtrReg() const {
389 assert((Kind == k_PtrReg) && "Invalid access!");
393 void setRegKind(RegisterKind RegKind) {
394 assert((Kind == k_Register || Kind == k_PtrReg) && "Invalid access!");
398 const MCExpr *getImm() const {
399 assert((Kind == k_Immediate) && "Invalid access!");
403 unsigned getMemBase() const {
404 assert((Kind == k_Memory) && "Invalid access!");
408 const MCExpr *getMemOff() const {
409 assert((Kind == k_Memory) && "Invalid access!");
413 static MipsOperand *CreateToken(StringRef Str, SMLoc S) {
414 MipsOperand *Op = new MipsOperand(k_Token);
415 Op->Tok.Data = Str.data();
416 Op->Tok.Length = Str.size();
422 static MipsOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
423 MipsOperand *Op = new MipsOperand(k_Register);
424 Op->Reg.RegNum = RegNum;
430 static MipsOperand *CreatePtrReg(unsigned RegNum, SMLoc S, SMLoc E) {
431 MipsOperand *Op = new MipsOperand(k_PtrReg);
432 Op->Reg.RegNum = RegNum;
438 static MipsOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
439 MipsOperand *Op = new MipsOperand(k_Immediate);
446 static MipsOperand *CreateMem(unsigned Base, const MCExpr *Off, SMLoc S,
448 MipsOperand *Op = new MipsOperand(k_Memory);
456 bool isGPR32Asm() const {
457 return Kind == k_Register && Reg.Kind == Kind_GPR32;
459 void addRegAsmOperands(MCInst &Inst, unsigned N) const {
460 Inst.addOperand(MCOperand::CreateReg(Reg.RegNum));
463 bool isGPR64Asm() const {
464 return Kind == k_Register && Reg.Kind == Kind_GPR64;
467 bool isHWRegsAsm() const {
468 assert((Kind == k_Register) && "Invalid access!");
469 return Reg.Kind == Kind_HWRegs;
472 bool isCCRAsm() const {
473 assert((Kind == k_Register) && "Invalid access!");
474 return Reg.Kind == Kind_CCRRegs;
477 bool isAFGR64Asm() const {
478 return Kind == k_Register && Reg.Kind == Kind_AFGR64Regs;
481 bool isFGR64Asm() const {
482 return Kind == k_Register && Reg.Kind == Kind_FGR64Regs;
485 bool isFGR32Asm() const {
486 return (Kind == k_Register) && Reg.Kind == Kind_FGR32Regs;
489 bool isFGRH32Asm() const {
490 return (Kind == k_Register) && Reg.Kind == Kind_FGRH32Regs;
493 bool isFCCRegsAsm() const {
494 return (Kind == k_Register) && Reg.Kind == Kind_FCCRegs;
497 bool isACC64DSPAsm() const {
498 return Kind == k_Register && Reg.Kind == Kind_ACC64DSP;
501 bool isLO32DSPAsm() const {
502 return Kind == k_Register && Reg.Kind == Kind_LO32DSP;
505 bool isHI32DSPAsm() const {
506 return Kind == k_Register && Reg.Kind == Kind_HI32DSP;
509 bool isCOP2Asm() const { return Kind == k_Register && Reg.Kind == Kind_COP2; }
511 bool isMSA128BAsm() const {
512 return Kind == k_Register && Reg.Kind == Kind_MSA128BRegs;
515 bool isMSA128HAsm() const {
516 return Kind == k_Register && Reg.Kind == Kind_MSA128HRegs;
519 bool isMSA128WAsm() const {
520 return Kind == k_Register && Reg.Kind == Kind_MSA128WRegs;
523 bool isMSA128DAsm() const {
524 return Kind == k_Register && Reg.Kind == Kind_MSA128DRegs;
527 bool isMSA128CRAsm() const {
528 return Kind == k_Register && Reg.Kind == Kind_MSA128CtrlRegs;
531 /// getStartLoc - Get the location of the first token of this operand.
532 SMLoc getStartLoc() const { return StartLoc; }
533 /// getEndLoc - Get the location of the last token of this operand.
534 SMLoc getEndLoc() const { return EndLoc; }
536 virtual void print(raw_ostream &OS) const {
537 llvm_unreachable("unimplemented!");
539 }; // class MipsOperand
543 extern const MCInstrDesc MipsInsts[];
545 static const MCInstrDesc &getInstDesc(unsigned Opcode) {
546 return MipsInsts[Opcode];
549 bool MipsAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
550 SmallVectorImpl<MCInst> &Instructions) {
551 const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
553 if (MCID.hasDelaySlot() && Options.isReorder()) {
554 // If this instruction has a delay slot and .set reorder is active,
555 // emit a NOP after it.
556 Instructions.push_back(Inst);
558 NopInst.setOpcode(Mips::SLL);
559 NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
560 NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
561 NopInst.addOperand(MCOperand::CreateImm(0));
562 Instructions.push_back(NopInst);
566 if (MCID.mayLoad() || MCID.mayStore()) {
567 // Check the offset of memory operand, if it is a symbol
568 // reference or immediate we may have to expand instructions.
569 for (unsigned i = 0; i < MCID.getNumOperands(); i++) {
570 const MCOperandInfo &OpInfo = MCID.OpInfo[i];
571 if ((OpInfo.OperandType == MCOI::OPERAND_MEMORY) ||
572 (OpInfo.OperandType == MCOI::OPERAND_UNKNOWN)) {
573 MCOperand &Op = Inst.getOperand(i);
575 int MemOffset = Op.getImm();
576 if (MemOffset < -32768 || MemOffset > 32767) {
577 // Offset can't exceed 16bit value.
578 expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), true);
581 } else if (Op.isExpr()) {
582 const MCExpr *Expr = Op.getExpr();
583 if (Expr->getKind() == MCExpr::SymbolRef) {
584 const MCSymbolRefExpr *SR =
585 static_cast<const MCSymbolRefExpr *>(Expr);
586 if (SR->getKind() == MCSymbolRefExpr::VK_None) {
588 expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), false);
591 } else if (!isEvaluated(Expr)) {
592 expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), false);
600 if (needsExpansion(Inst))
601 expandInstruction(Inst, IDLoc, Instructions);
603 Instructions.push_back(Inst);
608 bool MipsAsmParser::needsExpansion(MCInst &Inst) {
610 switch (Inst.getOpcode()) {
611 case Mips::LoadImm32Reg:
612 case Mips::LoadAddr32Imm:
613 case Mips::LoadAddr32Reg:
620 void MipsAsmParser::expandInstruction(MCInst &Inst, SMLoc IDLoc,
621 SmallVectorImpl<MCInst> &Instructions) {
622 switch (Inst.getOpcode()) {
623 case Mips::LoadImm32Reg:
624 return expandLoadImm(Inst, IDLoc, Instructions);
625 case Mips::LoadAddr32Imm:
626 return expandLoadAddressImm(Inst, IDLoc, Instructions);
627 case Mips::LoadAddr32Reg:
628 return expandLoadAddressReg(Inst, IDLoc, Instructions);
632 void MipsAsmParser::expandLoadImm(MCInst &Inst, SMLoc IDLoc,
633 SmallVectorImpl<MCInst> &Instructions) {
635 const MCOperand &ImmOp = Inst.getOperand(1);
636 assert(ImmOp.isImm() && "expected immediate operand kind");
637 const MCOperand &RegOp = Inst.getOperand(0);
638 assert(RegOp.isReg() && "expected register operand kind");
640 int ImmValue = ImmOp.getImm();
641 tmpInst.setLoc(IDLoc);
642 if (0 <= ImmValue && ImmValue <= 65535) {
643 // For 0 <= j <= 65535.
644 // li d,j => ori d,$zero,j
645 tmpInst.setOpcode(Mips::ORi);
646 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
647 tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
648 tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
649 Instructions.push_back(tmpInst);
650 } else if (ImmValue < 0 && ImmValue >= -32768) {
651 // For -32768 <= j < 0.
652 // li d,j => addiu d,$zero,j
653 tmpInst.setOpcode(Mips::ADDiu);
654 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
655 tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
656 tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
657 Instructions.push_back(tmpInst);
659 // For any other value of j that is representable as a 32-bit integer.
660 // li d,j => lui d,hi16(j)
662 tmpInst.setOpcode(Mips::LUi);
663 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
664 tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
665 Instructions.push_back(tmpInst);
667 tmpInst.setOpcode(Mips::ORi);
668 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
669 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
670 tmpInst.addOperand(MCOperand::CreateImm(ImmValue & 0xffff));
671 tmpInst.setLoc(IDLoc);
672 Instructions.push_back(tmpInst);
677 MipsAsmParser::expandLoadAddressReg(MCInst &Inst, SMLoc IDLoc,
678 SmallVectorImpl<MCInst> &Instructions) {
680 const MCOperand &ImmOp = Inst.getOperand(2);
681 assert(ImmOp.isImm() && "expected immediate operand kind");
682 const MCOperand &SrcRegOp = Inst.getOperand(1);
683 assert(SrcRegOp.isReg() && "expected register operand kind");
684 const MCOperand &DstRegOp = Inst.getOperand(0);
685 assert(DstRegOp.isReg() && "expected register operand kind");
686 int ImmValue = ImmOp.getImm();
687 if (-32768 <= ImmValue && ImmValue <= 65535) {
688 // For -32768 <= j <= 65535.
689 // la d,j(s) => addiu d,s,j
690 tmpInst.setOpcode(Mips::ADDiu);
691 tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
692 tmpInst.addOperand(MCOperand::CreateReg(SrcRegOp.getReg()));
693 tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
694 Instructions.push_back(tmpInst);
696 // For any other value of j that is representable as a 32-bit integer.
697 // la d,j(s) => lui d,hi16(j)
700 tmpInst.setOpcode(Mips::LUi);
701 tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
702 tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
703 Instructions.push_back(tmpInst);
705 tmpInst.setOpcode(Mips::ORi);
706 tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
707 tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
708 tmpInst.addOperand(MCOperand::CreateImm(ImmValue & 0xffff));
709 Instructions.push_back(tmpInst);
711 tmpInst.setOpcode(Mips::ADDu);
712 tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
713 tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
714 tmpInst.addOperand(MCOperand::CreateReg(SrcRegOp.getReg()));
715 Instructions.push_back(tmpInst);
720 MipsAsmParser::expandLoadAddressImm(MCInst &Inst, SMLoc IDLoc,
721 SmallVectorImpl<MCInst> &Instructions) {
723 const MCOperand &ImmOp = Inst.getOperand(1);
724 assert(ImmOp.isImm() && "expected immediate operand kind");
725 const MCOperand &RegOp = Inst.getOperand(0);
726 assert(RegOp.isReg() && "expected register operand kind");
727 int ImmValue = ImmOp.getImm();
728 if (-32768 <= ImmValue && ImmValue <= 65535) {
729 // For -32768 <= j <= 65535.
730 // la d,j => addiu d,$zero,j
731 tmpInst.setOpcode(Mips::ADDiu);
732 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
733 tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
734 tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
735 Instructions.push_back(tmpInst);
737 // For any other value of j that is representable as a 32-bit integer.
738 // la d,j => lui d,hi16(j)
740 tmpInst.setOpcode(Mips::LUi);
741 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
742 tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
743 Instructions.push_back(tmpInst);
745 tmpInst.setOpcode(Mips::ORi);
746 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
747 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
748 tmpInst.addOperand(MCOperand::CreateImm(ImmValue & 0xffff));
749 Instructions.push_back(tmpInst);
753 void MipsAsmParser::expandMemInst(MCInst &Inst, SMLoc IDLoc,
754 SmallVectorImpl<MCInst> &Instructions,
755 bool isLoad, bool isImmOpnd) {
756 const MCSymbolRefExpr *SR;
758 unsigned ImmOffset, HiOffset, LoOffset;
759 const MCExpr *ExprOffset;
761 unsigned AtRegNum = getReg(
762 (isMips64()) ? Mips::GPR64RegClassID : Mips::GPR32RegClassID, getATReg());
763 // 1st operand is either the source or destination register.
764 assert(Inst.getOperand(0).isReg() && "expected register operand kind");
765 unsigned RegOpNum = Inst.getOperand(0).getReg();
766 // 2nd operand is the base register.
767 assert(Inst.getOperand(1).isReg() && "expected register operand kind");
768 unsigned BaseRegNum = Inst.getOperand(1).getReg();
769 // 3rd operand is either an immediate or expression.
771 assert(Inst.getOperand(2).isImm() && "expected immediate operand kind");
772 ImmOffset = Inst.getOperand(2).getImm();
773 LoOffset = ImmOffset & 0x0000ffff;
774 HiOffset = (ImmOffset & 0xffff0000) >> 16;
775 // If msb of LoOffset is 1(negative number) we must increment HiOffset.
776 if (LoOffset & 0x8000)
779 ExprOffset = Inst.getOperand(2).getExpr();
780 // All instructions will have the same location.
781 TempInst.setLoc(IDLoc);
782 // 1st instruction in expansion is LUi. For load instruction we can use
783 // the dst register as a temporary if base and dst are different,
784 // but for stores we must use $at.
785 TmpRegNum = (isLoad && (BaseRegNum != RegOpNum)) ? RegOpNum : AtRegNum;
786 TempInst.setOpcode(Mips::LUi);
787 TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
789 TempInst.addOperand(MCOperand::CreateImm(HiOffset));
791 if (ExprOffset->getKind() == MCExpr::SymbolRef) {
792 SR = static_cast<const MCSymbolRefExpr *>(ExprOffset);
793 const MCSymbolRefExpr *HiExpr = MCSymbolRefExpr::Create(
794 SR->getSymbol().getName(), MCSymbolRefExpr::VK_Mips_ABS_HI,
796 TempInst.addOperand(MCOperand::CreateExpr(HiExpr));
798 const MCExpr *HiExpr = evaluateRelocExpr(ExprOffset, "hi");
799 TempInst.addOperand(MCOperand::CreateExpr(HiExpr));
802 // Add the instruction to the list.
803 Instructions.push_back(TempInst);
804 // Prepare TempInst for next instruction.
806 // Add temp register to base.
807 TempInst.setOpcode(Mips::ADDu);
808 TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
809 TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
810 TempInst.addOperand(MCOperand::CreateReg(BaseRegNum));
811 Instructions.push_back(TempInst);
813 // And finaly, create original instruction with low part
814 // of offset and new base.
815 TempInst.setOpcode(Inst.getOpcode());
816 TempInst.addOperand(MCOperand::CreateReg(RegOpNum));
817 TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
819 TempInst.addOperand(MCOperand::CreateImm(LoOffset));
821 if (ExprOffset->getKind() == MCExpr::SymbolRef) {
822 const MCSymbolRefExpr *LoExpr = MCSymbolRefExpr::Create(
823 SR->getSymbol().getName(), MCSymbolRefExpr::VK_Mips_ABS_LO,
825 TempInst.addOperand(MCOperand::CreateExpr(LoExpr));
827 const MCExpr *LoExpr = evaluateRelocExpr(ExprOffset, "lo");
828 TempInst.addOperand(MCOperand::CreateExpr(LoExpr));
831 Instructions.push_back(TempInst);
835 bool MipsAsmParser::MatchAndEmitInstruction(
836 SMLoc IDLoc, unsigned &Opcode,
837 SmallVectorImpl<MCParsedAsmOperand *> &Operands, MCStreamer &Out,
838 unsigned &ErrorInfo, bool MatchingInlineAsm) {
840 SmallVector<MCInst, 8> Instructions;
841 unsigned MatchResult =
842 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
844 switch (MatchResult) {
847 case Match_Success: {
848 if (processInstruction(Inst, IDLoc, Instructions))
850 for (unsigned i = 0; i < Instructions.size(); i++)
851 Out.EmitInstruction(Instructions[i]);
854 case Match_MissingFeature:
855 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
857 case Match_InvalidOperand: {
858 SMLoc ErrorLoc = IDLoc;
859 if (ErrorInfo != ~0U) {
860 if (ErrorInfo >= Operands.size())
861 return Error(IDLoc, "too few operands for instruction");
863 ErrorLoc = ((MipsOperand *)Operands[ErrorInfo])->getStartLoc();
864 if (ErrorLoc == SMLoc())
868 return Error(ErrorLoc, "invalid operand for instruction");
870 case Match_MnemonicFail:
871 return Error(IDLoc, "invalid instruction");
876 int MipsAsmParser::matchCPURegisterName(StringRef Name) {
882 CC = StringSwitch<unsigned>(Name)
916 // Although SGI documentation just cuts out t0-t3 for n32/n64,
917 // GNU pushes the values of t0-t3 to override the o32/o64 values for t4-t7
918 // We are supporting both cases, so for t0-t3 we'll just push them to t4-t7.
919 if (isMips64() && 8 <= CC && CC <= 11)
922 if (CC == -1 && isMips64())
923 CC = StringSwitch<unsigned>(Name)
936 int MipsAsmParser::matchFPURegisterName(StringRef Name) {
938 if (Name[0] == 'f') {
939 StringRef NumString = Name.substr(1);
941 if (NumString.getAsInteger(10, IntVal))
942 return -1; // This is not an integer.
943 if (IntVal > 31) // Maximum index for fpu register.
950 int MipsAsmParser::matchFCCRegisterName(StringRef Name) {
952 if (Name.startswith("fcc")) {
953 StringRef NumString = Name.substr(3);
955 if (NumString.getAsInteger(10, IntVal))
956 return -1; // This is not an integer.
957 if (IntVal > 7) // There are only 8 fcc registers.
964 int MipsAsmParser::matchACRegisterName(StringRef Name) {
966 if (Name.startswith("ac")) {
967 StringRef NumString = Name.substr(2);
969 if (NumString.getAsInteger(10, IntVal))
970 return -1; // This is not an integer.
971 if (IntVal > 3) // There are only 3 acc registers.
978 int MipsAsmParser::matchMSA128RegisterName(StringRef Name) {
981 if (Name.front() != 'w' || Name.drop_front(1).getAsInteger(10, IntVal))
990 int MipsAsmParser::matchMSA128CtrlRegisterName(StringRef Name) {
993 CC = StringSwitch<unsigned>(Name)
996 .Case("msaaccess", 2)
998 .Case("msamodify", 4)
999 .Case("msarequest", 5)
1001 .Case("msaunmap", 7)
1007 int MipsAsmParser::matchRegisterName(StringRef Name, bool is64BitReg) {
1010 CC = matchCPURegisterName(Name);
1012 return matchRegisterByNumber(CC, is64BitReg ? Mips::GPR64RegClassID
1013 : Mips::GPR32RegClassID);
1014 CC = matchFPURegisterName(Name);
1015 // TODO: decide about fpu register class
1017 return matchRegisterByNumber(CC, isFP64() ? Mips::FGR64RegClassID
1018 : Mips::FGR32RegClassID);
1019 return matchMSA128RegisterName(Name);
1022 int MipsAsmParser::regKindToRegClass(int RegKind) {
1025 case MipsOperand::Kind_GPR32:
1026 return Mips::GPR32RegClassID;
1027 case MipsOperand::Kind_GPR64:
1028 return Mips::GPR64RegClassID;
1029 case MipsOperand::Kind_HWRegs:
1030 return Mips::HWRegsRegClassID;
1031 case MipsOperand::Kind_FGR32Regs:
1032 return Mips::FGR32RegClassID;
1033 case MipsOperand::Kind_FGRH32Regs:
1034 return Mips::FGRH32RegClassID;
1035 case MipsOperand::Kind_FGR64Regs:
1036 return Mips::FGR64RegClassID;
1037 case MipsOperand::Kind_AFGR64Regs:
1038 return Mips::AFGR64RegClassID;
1039 case MipsOperand::Kind_CCRRegs:
1040 return Mips::CCRRegClassID;
1041 case MipsOperand::Kind_ACC64DSP:
1042 return Mips::ACC64DSPRegClassID;
1043 case MipsOperand::Kind_FCCRegs:
1044 return Mips::FCCRegClassID;
1045 case MipsOperand::Kind_MSA128BRegs:
1046 return Mips::MSA128BRegClassID;
1047 case MipsOperand::Kind_MSA128HRegs:
1048 return Mips::MSA128HRegClassID;
1049 case MipsOperand::Kind_MSA128WRegs:
1050 return Mips::MSA128WRegClassID;
1051 case MipsOperand::Kind_MSA128DRegs:
1052 return Mips::MSA128DRegClassID;
1053 case MipsOperand::Kind_MSA128CtrlRegs:
1054 return Mips::MSACtrlRegClassID;
1060 bool MipsAssemblerOptions::setATReg(unsigned Reg) {
1068 int MipsAsmParser::getATReg() { return Options.getATRegNum(); }
1070 unsigned MipsAsmParser::getReg(int RC, int RegNo) {
1071 return *(getContext().getRegisterInfo()->getRegClass(RC).begin() + RegNo);
1074 int MipsAsmParser::matchRegisterByNumber(unsigned RegNum, unsigned RegClass) {
1076 getContext().getRegisterInfo()->getRegClass(RegClass).getNumRegs())
1079 return getReg(RegClass, RegNum);
1082 int MipsAsmParser::tryParseRegister(bool is64BitReg) {
1083 const AsmToken &Tok = Parser.getTok();
1086 if (Tok.is(AsmToken::Identifier)) {
1087 std::string lowerCase = Tok.getString().lower();
1088 RegNum = matchRegisterName(lowerCase, is64BitReg);
1089 } else if (Tok.is(AsmToken::Integer))
1090 RegNum = matchRegisterByNumber(static_cast<unsigned>(Tok.getIntVal()),
1091 is64BitReg ? Mips::GPR64RegClassID
1092 : Mips::GPR32RegClassID);
1096 bool MipsAsmParser::tryParseRegisterOperand(
1097 SmallVectorImpl<MCParsedAsmOperand *> &Operands, bool is64BitReg) {
1099 SMLoc S = Parser.getTok().getLoc();
1102 RegNo = tryParseRegister(is64BitReg);
1107 MipsOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
1108 Parser.Lex(); // Eat register token.
1113 MipsAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand *> &Operands,
1114 StringRef Mnemonic) {
1115 // Check if the current operand has a custom associated parser, if so, try to
1116 // custom parse the operand, or fallback to the general approach.
1117 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1118 if (ResTy == MatchOperand_Success)
1120 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1121 // there was a match, but an error occurred, in which case, just return that
1122 // the operand parsing failed.
1123 if (ResTy == MatchOperand_ParseFail)
1126 switch (getLexer().getKind()) {
1128 Error(Parser.getTok().getLoc(), "unexpected token in operand");
1130 case AsmToken::Dollar: {
1131 // Parse the register.
1132 SMLoc S = Parser.getTok().getLoc();
1133 Parser.Lex(); // Eat dollar token.
1134 // Parse the register operand.
1135 if (!tryParseRegisterOperand(Operands, isMips64())) {
1136 if (getLexer().is(AsmToken::LParen)) {
1137 // Check if it is indexed addressing operand.
1138 Operands.push_back(MipsOperand::CreateToken("(", S));
1139 Parser.Lex(); // Eat the parenthesis.
1140 if (getLexer().isNot(AsmToken::Dollar))
1143 Parser.Lex(); // Eat the dollar
1144 if (tryParseRegisterOperand(Operands, isMips64()))
1147 if (!getLexer().is(AsmToken::RParen))
1150 S = Parser.getTok().getLoc();
1151 Operands.push_back(MipsOperand::CreateToken(")", S));
1156 // Maybe it is a symbol reference.
1157 StringRef Identifier;
1158 if (Parser.parseIdentifier(Identifier))
1161 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1163 MCSymbol *Sym = getContext().GetOrCreateSymbol("$" + Identifier);
1165 // Otherwise create a symbol reference.
1167 MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
1169 Operands.push_back(MipsOperand::CreateImm(Res, S, E));
1172 case AsmToken::Identifier:
1173 // Look for the existing symbol, we should check if
1174 // we need to assigne the propper RegisterKind.
1175 if (searchSymbolAlias(Operands, MipsOperand::Kind_None))
1177 // Else drop to expression parsing.
1178 case AsmToken::LParen:
1179 case AsmToken::Minus:
1180 case AsmToken::Plus:
1181 case AsmToken::Integer:
1182 case AsmToken::String: {
1183 // Quoted label names.
1184 const MCExpr *IdVal;
1185 SMLoc S = Parser.getTok().getLoc();
1186 if (getParser().parseExpression(IdVal))
1188 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1189 Operands.push_back(MipsOperand::CreateImm(IdVal, S, E));
1192 case AsmToken::Percent: {
1193 // It is a symbol reference or constant expression.
1194 const MCExpr *IdVal;
1195 SMLoc S = Parser.getTok().getLoc(); // Start location of the operand.
1196 if (parseRelocOperand(IdVal))
1199 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1201 Operands.push_back(MipsOperand::CreateImm(IdVal, S, E));
1203 } // case AsmToken::Percent
1204 } // switch(getLexer().getKind())
1208 const MCExpr *MipsAsmParser::evaluateRelocExpr(const MCExpr *Expr,
1209 StringRef RelocStr) {
1211 // Check the type of the expression.
1212 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Expr)) {
1213 // It's a constant, evaluate lo or hi value.
1214 if (RelocStr == "lo") {
1215 short Val = MCE->getValue();
1216 Res = MCConstantExpr::Create(Val, getContext());
1217 } else if (RelocStr == "hi") {
1218 int Val = MCE->getValue();
1219 int LoSign = Val & 0x8000;
1220 Val = (Val & 0xffff0000) >> 16;
1221 // Lower part is treated as a signed int, so if it is negative
1222 // we must add 1 to the hi part to compensate.
1225 Res = MCConstantExpr::Create(Val, getContext());
1227 llvm_unreachable("Invalid RelocStr value");
1232 if (const MCSymbolRefExpr *MSRE = dyn_cast<MCSymbolRefExpr>(Expr)) {
1233 // It's a symbol, create a symbolic expression from the symbol.
1234 StringRef Symbol = MSRE->getSymbol().getName();
1235 MCSymbolRefExpr::VariantKind VK = getVariantKind(RelocStr);
1236 Res = MCSymbolRefExpr::Create(Symbol, VK, getContext());
1240 if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
1241 const MCExpr *LExp = evaluateRelocExpr(BE->getLHS(), RelocStr);
1242 const MCExpr *RExp = evaluateRelocExpr(BE->getRHS(), RelocStr);
1243 Res = MCBinaryExpr::Create(BE->getOpcode(), LExp, RExp, getContext());
1247 if (const MCUnaryExpr *UN = dyn_cast<MCUnaryExpr>(Expr)) {
1248 const MCExpr *UnExp = evaluateRelocExpr(UN->getSubExpr(), RelocStr);
1249 Res = MCUnaryExpr::Create(UN->getOpcode(), UnExp, getContext());
1252 // Just return the original expression.
1256 bool MipsAsmParser::isEvaluated(const MCExpr *Expr) {
1258 switch (Expr->getKind()) {
1259 case MCExpr::Constant:
1261 case MCExpr::SymbolRef:
1262 return (cast<MCSymbolRefExpr>(Expr)->getKind() != MCSymbolRefExpr::VK_None);
1263 case MCExpr::Binary:
1264 if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
1265 if (!isEvaluated(BE->getLHS()))
1267 return isEvaluated(BE->getRHS());
1270 return isEvaluated(cast<MCUnaryExpr>(Expr)->getSubExpr());
1277 bool MipsAsmParser::parseRelocOperand(const MCExpr *&Res) {
1278 Parser.Lex(); // Eat the % token.
1279 const AsmToken &Tok = Parser.getTok(); // Get next token, operation.
1280 if (Tok.isNot(AsmToken::Identifier))
1283 std::string Str = Tok.getIdentifier().str();
1285 Parser.Lex(); // Eat the identifier.
1286 // Now make an expression from the rest of the operand.
1287 const MCExpr *IdVal;
1290 if (getLexer().getKind() == AsmToken::LParen) {
1292 Parser.Lex(); // Eat the '(' token.
1293 if (getLexer().getKind() == AsmToken::Percent) {
1294 Parser.Lex(); // Eat the % token.
1295 const AsmToken &nextTok = Parser.getTok();
1296 if (nextTok.isNot(AsmToken::Identifier))
1299 Str += nextTok.getIdentifier();
1300 Parser.Lex(); // Eat the identifier.
1301 if (getLexer().getKind() != AsmToken::LParen)
1306 if (getParser().parseParenExpression(IdVal, EndLoc))
1309 while (getLexer().getKind() == AsmToken::RParen)
1310 Parser.Lex(); // Eat the ')' token.
1313 return true; // Parenthesis must follow the relocation operand.
1315 Res = evaluateRelocExpr(IdVal, Str);
1319 bool MipsAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
1321 StartLoc = Parser.getTok().getLoc();
1322 RegNo = tryParseRegister(isMips64());
1323 EndLoc = Parser.getTok().getLoc();
1324 return (RegNo == (unsigned)-1);
1327 bool MipsAsmParser::parseMemOffset(const MCExpr *&Res, bool isParenExpr) {
1331 while (getLexer().getKind() == AsmToken::LParen)
1334 switch (getLexer().getKind()) {
1337 case AsmToken::Identifier:
1338 case AsmToken::LParen:
1339 case AsmToken::Integer:
1340 case AsmToken::Minus:
1341 case AsmToken::Plus:
1343 Result = getParser().parseParenExpression(Res, S);
1345 Result = (getParser().parseExpression(Res));
1346 while (getLexer().getKind() == AsmToken::RParen)
1349 case AsmToken::Percent:
1350 Result = parseRelocOperand(Res);
1355 MipsAsmParser::OperandMatchResultTy MipsAsmParser::parseMemOperand(
1356 SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1358 const MCExpr *IdVal = 0;
1360 bool isParenExpr = false;
1361 MipsAsmParser::OperandMatchResultTy Res = MatchOperand_NoMatch;
1362 // First operand is the offset.
1363 S = Parser.getTok().getLoc();
1365 if (getLexer().getKind() == AsmToken::LParen) {
1370 if (getLexer().getKind() != AsmToken::Dollar) {
1371 if (parseMemOffset(IdVal, isParenExpr))
1372 return MatchOperand_ParseFail;
1374 const AsmToken &Tok = Parser.getTok(); // Get the next token.
1375 if (Tok.isNot(AsmToken::LParen)) {
1376 MipsOperand *Mnemonic = static_cast<MipsOperand *>(Operands[0]);
1377 if (Mnemonic->getToken() == "la") {
1379 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1380 Operands.push_back(MipsOperand::CreateImm(IdVal, S, E));
1381 return MatchOperand_Success;
1383 if (Tok.is(AsmToken::EndOfStatement)) {
1385 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1387 // Zero register assumed, add a memory operand with ZERO as its base.
1388 Operands.push_back(MipsOperand::CreateMem(
1389 isMips64() ? Mips::ZERO_64 : Mips::ZERO, IdVal, S, E));
1390 return MatchOperand_Success;
1392 Error(Parser.getTok().getLoc(), "'(' expected");
1393 return MatchOperand_ParseFail;
1396 Parser.Lex(); // Eat the '(' token.
1399 Res = parseRegs(Operands, isMips64() ? (int)MipsOperand::Kind_GPR64
1400 : (int)MipsOperand::Kind_GPR32);
1401 if (Res != MatchOperand_Success)
1404 if (Parser.getTok().isNot(AsmToken::RParen)) {
1405 Error(Parser.getTok().getLoc(), "')' expected");
1406 return MatchOperand_ParseFail;
1409 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1411 Parser.Lex(); // Eat the ')' token.
1414 IdVal = MCConstantExpr::Create(0, getContext());
1416 // Replace the register operand with the memory operand.
1417 MipsOperand *op = static_cast<MipsOperand *>(Operands.back());
1418 int RegNo = op->getReg();
1419 // Remove the register from the operands.
1420 Operands.pop_back();
1421 // Add the memory operand.
1422 if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(IdVal)) {
1424 if (IdVal->EvaluateAsAbsolute(Imm))
1425 IdVal = MCConstantExpr::Create(Imm, getContext());
1426 else if (BE->getLHS()->getKind() != MCExpr::SymbolRef)
1427 IdVal = MCBinaryExpr::Create(BE->getOpcode(), BE->getRHS(), BE->getLHS(),
1431 Operands.push_back(MipsOperand::CreateMem(RegNo, IdVal, S, E));
1433 return MatchOperand_Success;
1436 bool MipsAsmParser::parsePtrReg(SmallVectorImpl<MCParsedAsmOperand *> &Operands,
1438 // If the first token is not '$' we have an error.
1439 if (Parser.getTok().isNot(AsmToken::Dollar))
1442 SMLoc S = Parser.getTok().getLoc();
1444 AsmToken::TokenKind TkKind = getLexer().getKind();
1447 if (TkKind == AsmToken::Integer) {
1448 Reg = matchRegisterByNumber(Parser.getTok().getIntVal(),
1449 regKindToRegClass(RegKind));
1452 } else if (TkKind == AsmToken::Identifier) {
1453 if ((Reg = matchCPURegisterName(Parser.getTok().getString().lower())) == -1)
1455 Reg = getReg(regKindToRegClass(RegKind), Reg);
1460 MipsOperand *Op = MipsOperand::CreatePtrReg(Reg, S, Parser.getTok().getLoc());
1461 Op->setRegKind((MipsOperand::RegisterKind)RegKind);
1462 Operands.push_back(Op);
1467 MipsAsmParser::OperandMatchResultTy
1468 MipsAsmParser::parsePtrReg(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1469 MipsOperand::RegisterKind RegKind =
1470 isN64() ? MipsOperand::Kind_GPR64 : MipsOperand::Kind_GPR32;
1472 // Parse index register.
1473 if (!parsePtrReg(Operands, RegKind))
1474 return MatchOperand_NoMatch;
1477 if (Parser.getTok().isNot(AsmToken::LParen))
1478 return MatchOperand_NoMatch;
1480 Operands.push_back(MipsOperand::CreateToken("(", getLexer().getLoc()));
1483 // Parse base register.
1484 if (!parsePtrReg(Operands, RegKind))
1485 return MatchOperand_NoMatch;
1488 if (Parser.getTok().isNot(AsmToken::RParen))
1489 return MatchOperand_NoMatch;
1491 Operands.push_back(MipsOperand::CreateToken(")", getLexer().getLoc()));
1494 return MatchOperand_Success;
1497 MipsAsmParser::OperandMatchResultTy
1498 MipsAsmParser::parseRegs(SmallVectorImpl<MCParsedAsmOperand *> &Operands,
1500 MipsOperand::RegisterKind Kind = (MipsOperand::RegisterKind)RegKind;
1501 if (getLexer().getKind() == AsmToken::Identifier && !hasConsumedDollar) {
1502 if (searchSymbolAlias(Operands, Kind))
1503 return MatchOperand_Success;
1504 return MatchOperand_NoMatch;
1506 SMLoc S = Parser.getTok().getLoc();
1507 // If the first token is not '$', we have an error.
1508 if (Parser.getTok().isNot(AsmToken::Dollar) && !hasConsumedDollar)
1509 return MatchOperand_NoMatch;
1510 if (!hasConsumedDollar) {
1511 Parser.Lex(); // Eat the '$'
1512 hasConsumedDollar = true;
1514 if (getLexer().getKind() == AsmToken::Identifier) {
1516 std::string RegName = Parser.getTok().getString().lower();
1517 // Match register by name
1519 case MipsOperand::Kind_GPR32:
1520 case MipsOperand::Kind_GPR64:
1521 RegNum = matchCPURegisterName(RegName);
1523 case MipsOperand::Kind_AFGR64Regs:
1524 case MipsOperand::Kind_FGR64Regs:
1525 case MipsOperand::Kind_FGR32Regs:
1526 case MipsOperand::Kind_FGRH32Regs:
1527 RegNum = matchFPURegisterName(RegName);
1528 if (RegKind == MipsOperand::Kind_AFGR64Regs)
1530 else if (RegKind == MipsOperand::Kind_FGRH32Regs && !isFP64())
1531 if (RegNum != -1 && RegNum % 2 != 0)
1532 Warning(S, "Float register should be even.");
1534 case MipsOperand::Kind_FCCRegs:
1535 RegNum = matchFCCRegisterName(RegName);
1537 case MipsOperand::Kind_ACC64DSP:
1538 RegNum = matchACRegisterName(RegName);
1541 break; // No match, value is set to -1.
1543 // No match found, return _NoMatch to give a chance to other round.
1545 return MatchOperand_NoMatch;
1547 int RegVal = getReg(regKindToRegClass(Kind), RegNum);
1549 return MatchOperand_NoMatch;
1552 MipsOperand::CreateReg(RegVal, S, Parser.getTok().getLoc());
1553 Op->setRegKind(Kind);
1554 Operands.push_back(Op);
1555 hasConsumedDollar = false;
1556 Parser.Lex(); // Eat the register name.
1557 return MatchOperand_Success;
1558 } else if (getLexer().getKind() == AsmToken::Integer) {
1559 unsigned RegNum = Parser.getTok().getIntVal();
1560 if (Kind == MipsOperand::Kind_HWRegs) {
1562 return MatchOperand_NoMatch;
1563 // Only hwreg 29 is supported, found at index 0.
1566 int Reg = matchRegisterByNumber(RegNum, regKindToRegClass(Kind));
1568 return MatchOperand_NoMatch;
1569 MipsOperand *Op = MipsOperand::CreateReg(Reg, S, Parser.getTok().getLoc());
1570 Op->setRegKind(Kind);
1571 Operands.push_back(Op);
1572 hasConsumedDollar = false;
1573 Parser.Lex(); // Eat the register number.
1574 if ((RegKind == MipsOperand::Kind_GPR32) &&
1575 (getLexer().is(AsmToken::LParen))) {
1576 // Check if it is indexed addressing operand.
1577 Operands.push_back(MipsOperand::CreateToken("(", getLexer().getLoc()));
1578 Parser.Lex(); // Eat the parenthesis.
1579 if (parseRegs(Operands, RegKind) != MatchOperand_Success)
1580 return MatchOperand_NoMatch;
1581 if (getLexer().isNot(AsmToken::RParen))
1582 return MatchOperand_NoMatch;
1583 Operands.push_back(MipsOperand::CreateToken(")", getLexer().getLoc()));
1586 return MatchOperand_Success;
1588 return MatchOperand_NoMatch;
1591 bool MipsAsmParser::validateMSAIndex(int Val, int RegKind) {
1592 MipsOperand::RegisterKind Kind = (MipsOperand::RegisterKind)RegKind;
1600 case MipsOperand::Kind_MSA128BRegs:
1602 case MipsOperand::Kind_MSA128HRegs:
1604 case MipsOperand::Kind_MSA128WRegs:
1606 case MipsOperand::Kind_MSA128DRegs:
1611 MipsAsmParser::OperandMatchResultTy
1612 MipsAsmParser::parseMSARegs(SmallVectorImpl<MCParsedAsmOperand *> &Operands,
1614 MipsOperand::RegisterKind Kind = (MipsOperand::RegisterKind)RegKind;
1615 SMLoc S = Parser.getTok().getLoc();
1616 std::string RegName;
1618 if (Parser.getTok().isNot(AsmToken::Dollar))
1619 return MatchOperand_NoMatch;
1623 return MatchOperand_ParseFail;
1624 case MipsOperand::Kind_MSA128BRegs:
1625 case MipsOperand::Kind_MSA128HRegs:
1626 case MipsOperand::Kind_MSA128WRegs:
1627 case MipsOperand::Kind_MSA128DRegs:
1631 Parser.Lex(); // Eat the '$'.
1632 if (getLexer().getKind() == AsmToken::Identifier)
1633 RegName = Parser.getTok().getString().lower();
1635 return MatchOperand_ParseFail;
1637 int RegNum = matchMSA128RegisterName(RegName);
1639 if (RegNum < 0 || RegNum > 31)
1640 return MatchOperand_ParseFail;
1642 int RegVal = getReg(regKindToRegClass(Kind), RegNum);
1644 return MatchOperand_ParseFail;
1646 MipsOperand *Op = MipsOperand::CreateReg(RegVal, S, Parser.getTok().getLoc());
1647 Op->setRegKind(Kind);
1648 Operands.push_back(Op);
1650 Parser.Lex(); // Eat the register identifier.
1652 // MSA registers may be suffixed with an index in the form of:
1653 // 1) Immediate expression.
1654 // 2) General Purpose Register.
1656 // 1) copy_s.b $29,$w0[0]
1657 // 2) sld.b $w0,$w1[$1]
1659 if (Parser.getTok().isNot(AsmToken::LBrac))
1660 return MatchOperand_Success;
1662 MipsOperand *Mnemonic = static_cast<MipsOperand *>(Operands[0]);
1664 Operands.push_back(MipsOperand::CreateToken("[", Parser.getTok().getLoc()));
1665 Parser.Lex(); // Parse the '[' token.
1667 if (Parser.getTok().is(AsmToken::Dollar)) {
1668 // This must be a GPR.
1670 SMLoc VIdx = Parser.getTok().getLoc();
1671 Parser.Lex(); // Parse the '$' token.
1673 // GPR have aliases and we must account for that. Example: $30 == $fp
1674 if (getLexer().getKind() == AsmToken::Integer) {
1675 unsigned RegNum = Parser.getTok().getIntVal();
1676 int Reg = matchRegisterByNumber(
1677 RegNum, regKindToRegClass(MipsOperand::Kind_GPR32));
1679 Error(VIdx, "invalid general purpose register");
1680 return MatchOperand_ParseFail;
1683 RegOp = MipsOperand::CreateReg(Reg, VIdx, Parser.getTok().getLoc());
1684 } else if (getLexer().getKind() == AsmToken::Identifier) {
1686 std::string RegName = Parser.getTok().getString().lower();
1688 RegNum = matchCPURegisterName(RegName);
1690 Error(VIdx, "general purpose register expected");
1691 return MatchOperand_ParseFail;
1693 RegNum = getReg(regKindToRegClass(MipsOperand::Kind_GPR32), RegNum);
1694 RegOp = MipsOperand::CreateReg(RegNum, VIdx, Parser.getTok().getLoc());
1696 return MatchOperand_ParseFail;
1698 RegOp->setRegKind(MipsOperand::Kind_GPR32);
1699 Operands.push_back(RegOp);
1700 Parser.Lex(); // Eat the register identifier.
1702 if (Parser.getTok().isNot(AsmToken::RBrac))
1703 return MatchOperand_ParseFail;
1705 Operands.push_back(MipsOperand::CreateToken("]", Parser.getTok().getLoc()));
1706 Parser.Lex(); // Parse the ']' token.
1708 return MatchOperand_Success;
1711 // The index must be a constant expression then.
1712 SMLoc VIdx = Parser.getTok().getLoc();
1713 const MCExpr *ImmVal;
1715 if (getParser().parseExpression(ImmVal))
1716 return MatchOperand_ParseFail;
1718 const MCConstantExpr *expr = dyn_cast<MCConstantExpr>(ImmVal);
1719 if (!expr || !validateMSAIndex((int)expr->getValue(), Kind)) {
1720 Error(VIdx, "invalid immediate value");
1721 return MatchOperand_ParseFail;
1724 SMLoc E = Parser.getTok().getEndLoc();
1726 if (Parser.getTok().isNot(AsmToken::RBrac))
1727 return MatchOperand_ParseFail;
1730 Mnemonic->getToken() == "insve.b" || Mnemonic->getToken() == "insve.h" ||
1731 Mnemonic->getToken() == "insve.w" || Mnemonic->getToken() == "insve.d";
1733 // The second vector index of insve instructions is always 0.
1734 if (insve && Operands.size() > 6) {
1735 if (expr->getValue() != 0) {
1736 Error(VIdx, "immediate value must be 0");
1737 return MatchOperand_ParseFail;
1739 Operands.push_back(MipsOperand::CreateToken("0", VIdx));
1741 Operands.push_back(MipsOperand::CreateImm(expr, VIdx, E));
1743 Operands.push_back(MipsOperand::CreateToken("]", Parser.getTok().getLoc()));
1745 Parser.Lex(); // Parse the ']' token.
1747 return MatchOperand_Success;
1750 MipsAsmParser::OperandMatchResultTy
1751 MipsAsmParser::parseMSACtrlRegs(SmallVectorImpl<MCParsedAsmOperand *> &Operands,
1753 MipsOperand::RegisterKind Kind = (MipsOperand::RegisterKind)RegKind;
1755 if (Kind != MipsOperand::Kind_MSA128CtrlRegs)
1756 return MatchOperand_NoMatch;
1758 if (Parser.getTok().isNot(AsmToken::Dollar))
1759 return MatchOperand_ParseFail;
1761 SMLoc S = Parser.getTok().getLoc();
1763 Parser.Lex(); // Eat the '$' symbol.
1766 if (getLexer().getKind() == AsmToken::Identifier)
1767 RegNum = matchMSA128CtrlRegisterName(Parser.getTok().getString().lower());
1768 else if (getLexer().getKind() == AsmToken::Integer)
1769 RegNum = Parser.getTok().getIntVal();
1771 return MatchOperand_ParseFail;
1773 if (RegNum < 0 || RegNum > 7)
1774 return MatchOperand_ParseFail;
1776 int RegVal = getReg(regKindToRegClass(Kind), RegNum);
1778 return MatchOperand_ParseFail;
1780 MipsOperand *RegOp =
1781 MipsOperand::CreateReg(RegVal, S, Parser.getTok().getLoc());
1782 RegOp->setRegKind(MipsOperand::Kind_MSA128CtrlRegs);
1783 Operands.push_back(RegOp);
1784 Parser.Lex(); // Eat the register identifier.
1786 return MatchOperand_Success;
1789 MipsAsmParser::OperandMatchResultTy
1790 MipsAsmParser::parseGPR64(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1793 return MatchOperand_NoMatch;
1794 return parseRegs(Operands, (int)MipsOperand::Kind_GPR64);
1797 MipsAsmParser::OperandMatchResultTy
1798 MipsAsmParser::parseGPR32(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1799 return parseRegs(Operands, (int)MipsOperand::Kind_GPR32);
1802 MipsAsmParser::OperandMatchResultTy MipsAsmParser::parseAFGR64Regs(
1803 SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1806 return MatchOperand_NoMatch;
1807 return parseRegs(Operands, (int)MipsOperand::Kind_AFGR64Regs);
1810 MipsAsmParser::OperandMatchResultTy
1811 MipsAsmParser::parseFGR64Regs(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1813 return MatchOperand_NoMatch;
1814 return parseRegs(Operands, (int)MipsOperand::Kind_FGR64Regs);
1817 MipsAsmParser::OperandMatchResultTy
1818 MipsAsmParser::parseFGR32Regs(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1819 return parseRegs(Operands, (int)MipsOperand::Kind_FGR32Regs);
1822 MipsAsmParser::OperandMatchResultTy MipsAsmParser::parseFGRH32Regs(
1823 SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1824 return parseRegs(Operands, (int)MipsOperand::Kind_FGRH32Regs);
1827 MipsAsmParser::OperandMatchResultTy
1828 MipsAsmParser::parseFCCRegs(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1829 return parseRegs(Operands, (int)MipsOperand::Kind_FCCRegs);
1832 MipsAsmParser::OperandMatchResultTy
1833 MipsAsmParser::parseACC64DSP(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1834 return parseRegs(Operands, (int)MipsOperand::Kind_ACC64DSP);
1837 MipsAsmParser::OperandMatchResultTy
1838 MipsAsmParser::parseLO32DSP(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1839 // If the first token is not '$' we have an error.
1840 if (Parser.getTok().isNot(AsmToken::Dollar))
1841 return MatchOperand_NoMatch;
1843 SMLoc S = Parser.getTok().getLoc();
1844 Parser.Lex(); // Eat the '$'
1846 const AsmToken &Tok = Parser.getTok(); // Get next token.
1848 if (Tok.isNot(AsmToken::Identifier))
1849 return MatchOperand_NoMatch;
1851 if (!Tok.getIdentifier().startswith("ac"))
1852 return MatchOperand_NoMatch;
1854 StringRef NumString = Tok.getIdentifier().substr(2);
1857 if (NumString.getAsInteger(10, IntVal))
1858 return MatchOperand_NoMatch;
1860 unsigned Reg = matchRegisterByNumber(IntVal, Mips::LO32DSPRegClassID);
1862 MipsOperand *Op = MipsOperand::CreateReg(Reg, S, Parser.getTok().getLoc());
1863 Op->setRegKind(MipsOperand::Kind_LO32DSP);
1864 Operands.push_back(Op);
1866 Parser.Lex(); // Eat the register number.
1867 return MatchOperand_Success;
1870 MipsAsmParser::OperandMatchResultTy
1871 MipsAsmParser::parseHI32DSP(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1872 // If the first token is not '$' we have an error.
1873 if (Parser.getTok().isNot(AsmToken::Dollar))
1874 return MatchOperand_NoMatch;
1876 SMLoc S = Parser.getTok().getLoc();
1877 Parser.Lex(); // Eat the '$'
1879 const AsmToken &Tok = Parser.getTok(); // Get next token.
1881 if (Tok.isNot(AsmToken::Identifier))
1882 return MatchOperand_NoMatch;
1884 if (!Tok.getIdentifier().startswith("ac"))
1885 return MatchOperand_NoMatch;
1887 StringRef NumString = Tok.getIdentifier().substr(2);
1890 if (NumString.getAsInteger(10, IntVal))
1891 return MatchOperand_NoMatch;
1893 unsigned Reg = matchRegisterByNumber(IntVal, Mips::HI32DSPRegClassID);
1895 MipsOperand *Op = MipsOperand::CreateReg(Reg, S, Parser.getTok().getLoc());
1896 Op->setRegKind(MipsOperand::Kind_HI32DSP);
1897 Operands.push_back(Op);
1899 Parser.Lex(); // Eat the register number.
1900 return MatchOperand_Success;
1903 MipsAsmParser::OperandMatchResultTy
1904 MipsAsmParser::parseCOP2(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1905 // If the first token is not '$' we have an error.
1906 if (Parser.getTok().isNot(AsmToken::Dollar))
1907 return MatchOperand_NoMatch;
1909 SMLoc S = Parser.getTok().getLoc();
1910 Parser.Lex(); // Eat the '$'
1912 const AsmToken &Tok = Parser.getTok(); // Get next token.
1914 if (Tok.isNot(AsmToken::Integer))
1915 return MatchOperand_NoMatch;
1917 unsigned IntVal = Tok.getIntVal();
1919 unsigned Reg = matchRegisterByNumber(IntVal, Mips::COP2RegClassID);
1921 MipsOperand *Op = MipsOperand::CreateReg(Reg, S, Parser.getTok().getLoc());
1922 Op->setRegKind(MipsOperand::Kind_COP2);
1923 Operands.push_back(Op);
1925 Parser.Lex(); // Eat the register number.
1926 return MatchOperand_Success;
1929 MipsAsmParser::OperandMatchResultTy MipsAsmParser::parseMSA128BRegs(
1930 SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1931 return parseMSARegs(Operands, (int)MipsOperand::Kind_MSA128BRegs);
1934 MipsAsmParser::OperandMatchResultTy MipsAsmParser::parseMSA128HRegs(
1935 SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1936 return parseMSARegs(Operands, (int)MipsOperand::Kind_MSA128HRegs);
1939 MipsAsmParser::OperandMatchResultTy MipsAsmParser::parseMSA128WRegs(
1940 SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1941 return parseMSARegs(Operands, (int)MipsOperand::Kind_MSA128WRegs);
1944 MipsAsmParser::OperandMatchResultTy MipsAsmParser::parseMSA128DRegs(
1945 SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1946 return parseMSARegs(Operands, (int)MipsOperand::Kind_MSA128DRegs);
1949 MipsAsmParser::OperandMatchResultTy MipsAsmParser::parseMSA128CtrlRegs(
1950 SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1951 return parseMSACtrlRegs(Operands, (int)MipsOperand::Kind_MSA128CtrlRegs);
1954 bool MipsAsmParser::searchSymbolAlias(
1955 SmallVectorImpl<MCParsedAsmOperand *> &Operands, unsigned RegKind) {
1957 MCSymbol *Sym = getContext().LookupSymbol(Parser.getTok().getIdentifier());
1959 SMLoc S = Parser.getTok().getLoc();
1961 if (Sym->isVariable())
1962 Expr = Sym->getVariableValue();
1965 if (Expr->getKind() == MCExpr::SymbolRef) {
1966 MipsOperand::RegisterKind Kind = (MipsOperand::RegisterKind)RegKind;
1967 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
1968 const StringRef DefSymbol = Ref->getSymbol().getName();
1969 if (DefSymbol.startswith("$")) {
1971 APInt IntVal(32, -1);
1972 if (!DefSymbol.substr(1).getAsInteger(10, IntVal))
1973 RegNum = matchRegisterByNumber(IntVal.getZExtValue(),
1974 isMips64() ? Mips::GPR64RegClassID
1975 : Mips::GPR32RegClassID);
1977 // Lookup for the register with the corresponding name.
1979 case MipsOperand::Kind_AFGR64Regs:
1980 case MipsOperand::Kind_FGR64Regs:
1981 RegNum = matchFPURegisterName(DefSymbol.substr(1));
1983 case MipsOperand::Kind_FGR32Regs:
1984 RegNum = matchFPURegisterName(DefSymbol.substr(1));
1986 case MipsOperand::Kind_GPR64:
1987 case MipsOperand::Kind_GPR32:
1989 RegNum = matchCPURegisterName(DefSymbol.substr(1));
1993 RegNum = getReg(regKindToRegClass(Kind), RegNum);
1998 MipsOperand::CreateReg(RegNum, S, Parser.getTok().getLoc());
1999 op->setRegKind(Kind);
2000 Operands.push_back(op);
2004 } else if (Expr->getKind() == MCExpr::Constant) {
2006 const MCConstantExpr *Const = static_cast<const MCConstantExpr *>(Expr);
2008 MipsOperand::CreateImm(Const, S, Parser.getTok().getLoc());
2009 Operands.push_back(op);
2016 MipsAsmParser::OperandMatchResultTy
2017 MipsAsmParser::parseHWRegs(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
2018 return parseRegs(Operands, (int)MipsOperand::Kind_HWRegs);
2021 MipsAsmParser::OperandMatchResultTy
2022 MipsAsmParser::parseCCRRegs(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
2023 return parseRegs(Operands, (int)MipsOperand::Kind_CCRRegs);
2026 MipsAsmParser::OperandMatchResultTy
2027 MipsAsmParser::parseInvNum(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
2028 const MCExpr *IdVal;
2029 // If the first token is '$' we may have register operand.
2030 if (Parser.getTok().is(AsmToken::Dollar))
2031 return MatchOperand_NoMatch;
2032 SMLoc S = Parser.getTok().getLoc();
2033 if (getParser().parseExpression(IdVal))
2034 return MatchOperand_ParseFail;
2035 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(IdVal);
2036 assert(MCE && "Unexpected MCExpr type.");
2037 int64_t Val = MCE->getValue();
2038 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
2039 Operands.push_back(MipsOperand::CreateImm(
2040 MCConstantExpr::Create(0 - Val, getContext()), S, E));
2041 return MatchOperand_Success;
2044 MCSymbolRefExpr::VariantKind MipsAsmParser::getVariantKind(StringRef Symbol) {
2046 MCSymbolRefExpr::VariantKind VK =
2047 StringSwitch<MCSymbolRefExpr::VariantKind>(Symbol)
2048 .Case("hi", MCSymbolRefExpr::VK_Mips_ABS_HI)
2049 .Case("lo", MCSymbolRefExpr::VK_Mips_ABS_LO)
2050 .Case("gp_rel", MCSymbolRefExpr::VK_Mips_GPREL)
2051 .Case("call16", MCSymbolRefExpr::VK_Mips_GOT_CALL)
2052 .Case("got", MCSymbolRefExpr::VK_Mips_GOT)
2053 .Case("tlsgd", MCSymbolRefExpr::VK_Mips_TLSGD)
2054 .Case("tlsldm", MCSymbolRefExpr::VK_Mips_TLSLDM)
2055 .Case("dtprel_hi", MCSymbolRefExpr::VK_Mips_DTPREL_HI)
2056 .Case("dtprel_lo", MCSymbolRefExpr::VK_Mips_DTPREL_LO)
2057 .Case("gottprel", MCSymbolRefExpr::VK_Mips_GOTTPREL)
2058 .Case("tprel_hi", MCSymbolRefExpr::VK_Mips_TPREL_HI)
2059 .Case("tprel_lo", MCSymbolRefExpr::VK_Mips_TPREL_LO)
2060 .Case("got_disp", MCSymbolRefExpr::VK_Mips_GOT_DISP)
2061 .Case("got_page", MCSymbolRefExpr::VK_Mips_GOT_PAGE)
2062 .Case("got_ofst", MCSymbolRefExpr::VK_Mips_GOT_OFST)
2063 .Case("hi(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_HI)
2064 .Case("lo(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_LO)
2065 .Default(MCSymbolRefExpr::VK_None);
2070 bool MipsAsmParser::ParseInstruction(
2071 ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
2072 SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
2073 // Check if we have valid mnemonic
2074 if (!mnemonicIsValid(Name, 0)) {
2075 Parser.eatToEndOfStatement();
2076 return Error(NameLoc, "Unknown instruction");
2078 // First operand in MCInst is instruction mnemonic.
2079 Operands.push_back(MipsOperand::CreateToken(Name, NameLoc));
2081 // Read the remaining operands.
2082 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2083 // Read the first operand.
2084 if (ParseOperand(Operands, Name)) {
2085 SMLoc Loc = getLexer().getLoc();
2086 Parser.eatToEndOfStatement();
2087 return Error(Loc, "unexpected token in argument list");
2090 while (getLexer().is(AsmToken::Comma)) {
2091 Parser.Lex(); // Eat the comma.
2092 // Parse and remember the operand.
2093 if (ParseOperand(Operands, Name)) {
2094 SMLoc Loc = getLexer().getLoc();
2095 Parser.eatToEndOfStatement();
2096 return Error(Loc, "unexpected token in argument list");
2100 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2101 SMLoc Loc = getLexer().getLoc();
2102 Parser.eatToEndOfStatement();
2103 return Error(Loc, "unexpected token in argument list");
2105 Parser.Lex(); // Consume the EndOfStatement.
2109 bool MipsAsmParser::reportParseError(StringRef ErrorMsg) {
2110 SMLoc Loc = getLexer().getLoc();
2111 Parser.eatToEndOfStatement();
2112 return Error(Loc, ErrorMsg);
2115 bool MipsAsmParser::parseSetNoAtDirective() {
2116 // Line should look like: ".set noat".
2118 Options.setATReg(0);
2121 // If this is not the end of the statement, report an error.
2122 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2123 reportParseError("unexpected token in statement");
2126 Parser.Lex(); // Consume the EndOfStatement.
2130 bool MipsAsmParser::parseSetAtDirective() {
2131 // Line can be .set at - defaults to $1
2135 if (getLexer().is(AsmToken::EndOfStatement)) {
2136 Options.setATReg(1);
2137 Parser.Lex(); // Consume the EndOfStatement.
2139 } else if (getLexer().is(AsmToken::Equal)) {
2140 getParser().Lex(); // Eat the '='.
2141 if (getLexer().isNot(AsmToken::Dollar)) {
2142 reportParseError("unexpected token in statement");
2145 Parser.Lex(); // Eat the '$'.
2146 const AsmToken &Reg = Parser.getTok();
2147 if (Reg.is(AsmToken::Identifier)) {
2148 AtRegNo = matchCPURegisterName(Reg.getIdentifier());
2149 } else if (Reg.is(AsmToken::Integer)) {
2150 AtRegNo = Reg.getIntVal();
2152 reportParseError("unexpected token in statement");
2156 if (AtRegNo < 1 || AtRegNo > 31) {
2157 reportParseError("unexpected token in statement");
2161 if (!Options.setATReg(AtRegNo)) {
2162 reportParseError("unexpected token in statement");
2165 getParser().Lex(); // Eat the register.
2167 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2168 reportParseError("unexpected token in statement");
2171 Parser.Lex(); // Consume the EndOfStatement.
2174 reportParseError("unexpected token in statement");
2179 bool MipsAsmParser::parseSetReorderDirective() {
2181 // If this is not the end of the statement, report an error.
2182 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2183 reportParseError("unexpected token in statement");
2186 Options.setReorder();
2187 Parser.Lex(); // Consume the EndOfStatement.
2191 bool MipsAsmParser::parseSetNoReorderDirective() {
2193 // If this is not the end of the statement, report an error.
2194 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2195 reportParseError("unexpected token in statement");
2198 Options.setNoreorder();
2199 Parser.Lex(); // Consume the EndOfStatement.
2203 bool MipsAsmParser::parseSetMacroDirective() {
2205 // If this is not the end of the statement, report an error.
2206 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2207 reportParseError("unexpected token in statement");
2211 Parser.Lex(); // Consume the EndOfStatement.
2215 bool MipsAsmParser::parseSetNoMacroDirective() {
2217 // If this is not the end of the statement, report an error.
2218 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2219 reportParseError("`noreorder' must be set before `nomacro'");
2222 if (Options.isReorder()) {
2223 reportParseError("`noreorder' must be set before `nomacro'");
2226 Options.setNomacro();
2227 Parser.Lex(); // Consume the EndOfStatement.
2231 bool MipsAsmParser::parseSetAssignment() {
2233 const MCExpr *Value;
2235 if (Parser.parseIdentifier(Name))
2236 reportParseError("expected identifier after .set");
2238 if (getLexer().isNot(AsmToken::Comma))
2239 return reportParseError("unexpected token in .set directive");
2242 if (getLexer().is(AsmToken::Dollar)) {
2244 SMLoc DollarLoc = getLexer().getLoc();
2245 // Consume the dollar sign, and check for a following identifier.
2247 // We have a '$' followed by something, make sure they are adjacent.
2248 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
2251 StringRef(DollarLoc.getPointer(),
2252 getTok().getEndLoc().getPointer() - DollarLoc.getPointer());
2253 Symbol = getContext().GetOrCreateSymbol(Res);
2256 MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_None, getContext());
2257 } else if (Parser.parseExpression(Value))
2258 return reportParseError("expected valid expression after comma");
2260 // Check if the Name already exists as a symbol.
2261 MCSymbol *Sym = getContext().LookupSymbol(Name);
2263 return reportParseError("symbol already defined");
2264 Sym = getContext().GetOrCreateSymbol(Name);
2265 Sym->setVariableValue(Value);
2270 bool MipsAsmParser::parseDirectiveSet() {
2272 // Get the next token.
2273 const AsmToken &Tok = Parser.getTok();
2275 if (Tok.getString() == "noat") {
2276 return parseSetNoAtDirective();
2277 } else if (Tok.getString() == "at") {
2278 return parseSetAtDirective();
2279 } else if (Tok.getString() == "reorder") {
2280 return parseSetReorderDirective();
2281 } else if (Tok.getString() == "noreorder") {
2282 return parseSetNoReorderDirective();
2283 } else if (Tok.getString() == "macro") {
2284 return parseSetMacroDirective();
2285 } else if (Tok.getString() == "nomacro") {
2286 return parseSetNoMacroDirective();
2287 } else if (Tok.getString() == "nomips16") {
2288 // Ignore this directive for now.
2289 Parser.eatToEndOfStatement();
2291 } else if (Tok.getString() == "nomicromips") {
2292 // Ignore this directive for now.
2293 Parser.eatToEndOfStatement();
2296 // It is just an identifier, look for an assignment.
2297 parseSetAssignment();
2304 bool MipsAsmParser::parseDirectiveMipsHackStocg() {
2305 MCAsmParser &Parser = getParser();
2307 if (Parser.parseIdentifier(Name))
2308 reportParseError("expected identifier");
2310 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2311 if (getLexer().isNot(AsmToken::Comma))
2312 return TokError("unexpected token");
2316 if (Parser.parseAbsoluteExpression(Flags))
2317 return TokError("unexpected token");
2319 getTargetStreamer().emitMipsHackSTOCG(Sym, Flags);
2323 bool MipsAsmParser::parseDirectiveMipsHackELFFlags() {
2325 if (Parser.parseAbsoluteExpression(Flags))
2326 return TokError("unexpected token");
2328 getTargetStreamer().emitMipsHackELFFlags(Flags);
2332 /// parseDirectiveWord
2333 /// ::= .word [ expression (, expression)* ]
2334 bool MipsAsmParser::parseDirectiveWord(unsigned Size, SMLoc L) {
2335 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2337 const MCExpr *Value;
2338 if (getParser().parseExpression(Value))
2341 getParser().getStreamer().EmitValue(Value, Size);
2343 if (getLexer().is(AsmToken::EndOfStatement))
2346 // FIXME: Improve diagnostic.
2347 if (getLexer().isNot(AsmToken::Comma))
2348 return Error(L, "unexpected token in directive");
2357 /// parseDirectiveGpWord
2358 /// ::= .gpword local_sym
2359 bool MipsAsmParser::parseDirectiveGpWord() {
2360 const MCExpr *Value;
2361 // EmitGPRel32Value requires an expression, so we are using base class
2362 // method to evaluate the expression.
2363 if (getParser().parseExpression(Value))
2366 getParser().getStreamer().EmitGPRel32Value(Value);
2367 Parser.Lex(); // Eat last token.
2369 if (getLexer().is(AsmToken::EndOfStatement))
2370 return Error(getLexer().getLoc(), "unexpected token in directive");
2375 bool MipsAsmParser::ParseDirective(AsmToken DirectiveID) {
2377 StringRef IDVal = DirectiveID.getString();
2379 if (IDVal == ".ent") {
2380 // Ignore this directive for now.
2385 if (IDVal == ".end") {
2386 // Ignore this directive for now.
2391 if (IDVal == ".frame") {
2392 // Ignore this directive for now.
2393 Parser.eatToEndOfStatement();
2397 if (IDVal == ".set") {
2398 return parseDirectiveSet();
2401 if (IDVal == ".fmask") {
2402 // Ignore this directive for now.
2403 Parser.eatToEndOfStatement();
2407 if (IDVal == ".mask") {
2408 // Ignore this directive for now.
2409 Parser.eatToEndOfStatement();
2413 if (IDVal == ".gpword") {
2414 // Ignore this directive for now.
2415 parseDirectiveGpWord();
2419 if (IDVal == ".word") {
2420 parseDirectiveWord(4, DirectiveID.getLoc());
2424 if (IDVal == ".mips_hack_stocg")
2425 return parseDirectiveMipsHackStocg();
2427 if (IDVal == ".mips_hack_elf_flags")
2428 return parseDirectiveMipsHackELFFlags();
2433 extern "C" void LLVMInitializeMipsAsmParser() {
2434 RegisterMCAsmParser<MipsAsmParser> X(TheMipsTarget);
2435 RegisterMCAsmParser<MipsAsmParser> Y(TheMipselTarget);
2436 RegisterMCAsmParser<MipsAsmParser> A(TheMips64Target);
2437 RegisterMCAsmParser<MipsAsmParser> B(TheMips64elTarget);
2440 #define GET_REGISTER_MATCHER
2441 #define GET_MATCHER_IMPLEMENTATION
2442 #include "MipsGenAsmMatcher.inc"