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 "llvm/ADT/StringSwitch.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCInst.h"
16 #include "llvm/MC/MCParser/MCAsmLexer.h"
17 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/MC/MCSubtargetInfo.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/MC/MCTargetAsmParser.h"
22 #include "llvm/Support/TargetRegistry.h"
23 #include "llvm/ADT/APInt.h"
28 class MipsAssemblerOptions {
30 MipsAssemblerOptions():
31 aTReg(1), reorder(true), macro(true) {
34 unsigned getATRegNum() {return aTReg;}
35 bool setATReg(unsigned Reg);
37 bool isReorder() {return reorder;}
38 void setReorder() {reorder = true;}
39 void setNoreorder() {reorder = false;}
41 bool isMacro() {return macro;}
42 void setMacro() {macro = true;}
43 void setNomacro() {macro = false;}
53 class MipsAsmParser : public MCTargetAsmParser {
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,
88 MipsAsmParser::OperandMatchResultTy
89 parseMemOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
91 MipsAsmParser::OperandMatchResultTy
92 parseGPR32(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
94 MipsAsmParser::OperandMatchResultTy
95 parseGPR64(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
97 MipsAsmParser::OperandMatchResultTy
98 parseHWRegs(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
100 MipsAsmParser::OperandMatchResultTy
101 parseCCRRegs(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
103 MipsAsmParser::OperandMatchResultTy
104 parseAFGR64Regs(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
106 MipsAsmParser::OperandMatchResultTy
107 parseFGR64Regs(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
109 MipsAsmParser::OperandMatchResultTy
110 parseFGR32Regs(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
112 MipsAsmParser::OperandMatchResultTy
113 parseFCCRegs(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
115 MipsAsmParser::OperandMatchResultTy
116 parseACC64DSP(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
118 MipsAsmParser::OperandMatchResultTy
119 parseLO32DSP(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
121 MipsAsmParser::OperandMatchResultTy
122 parseHI32DSP(SmallVectorImpl<MCParsedAsmOperand*> &Operands);
124 bool searchSymbolAlias(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
127 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &,
130 int tryParseRegister(bool is64BitReg);
132 bool tryParseRegisterOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
135 bool needsExpansion(MCInst &Inst);
137 void expandInstruction(MCInst &Inst, SMLoc IDLoc,
138 SmallVectorImpl<MCInst> &Instructions);
139 void expandLoadImm(MCInst &Inst, SMLoc IDLoc,
140 SmallVectorImpl<MCInst> &Instructions);
141 void expandLoadAddressImm(MCInst &Inst, SMLoc IDLoc,
142 SmallVectorImpl<MCInst> &Instructions);
143 void expandLoadAddressReg(MCInst &Inst, SMLoc IDLoc,
144 SmallVectorImpl<MCInst> &Instructions);
145 void expandMemInst(MCInst &Inst, SMLoc IDLoc,
146 SmallVectorImpl<MCInst> &Instructions,
147 bool isLoad,bool isImmOpnd);
148 bool reportParseError(StringRef ErrorMsg);
150 bool parseMemOffset(const MCExpr *&Res, bool isParenExpr);
151 bool parseRelocOperand(const MCExpr *&Res);
153 const MCExpr* evaluateRelocExpr(const MCExpr *Expr, StringRef RelocStr);
155 bool isEvaluated(const MCExpr *Expr);
156 bool parseDirectiveSet();
158 bool parseSetAtDirective();
159 bool parseSetNoAtDirective();
160 bool parseSetMacroDirective();
161 bool parseSetNoMacroDirective();
162 bool parseSetReorderDirective();
163 bool parseSetNoReorderDirective();
165 bool parseSetAssignment();
167 bool parseDirectiveWord(unsigned Size, SMLoc L);
169 MCSymbolRefExpr::VariantKind getVariantKind(StringRef Symbol);
171 bool isMips64() const {
172 return (STI.getFeatureBits() & Mips::FeatureMips64) != 0;
175 bool isFP64() const {
176 return (STI.getFeatureBits() & Mips::FeatureFP64Bit) != 0;
179 int matchRegisterName(StringRef Symbol, bool is64BitReg);
181 int matchCPURegisterName(StringRef Symbol);
183 int matchRegisterByNumber(unsigned RegNum, unsigned RegClass);
185 int matchFPURegisterName(StringRef Name);
187 int matchFCCRegisterName(StringRef Name);
189 int matchACRegisterName(StringRef Name);
191 int regKindToRegClass(int RegKind);
193 FpFormatTy getFpFormat() {return FpFormat;}
195 unsigned getReg(int RC, int RegNo);
199 bool processInstruction(MCInst &Inst, SMLoc IDLoc,
200 SmallVectorImpl<MCInst> &Instructions);
202 MipsAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
203 : MCTargetAsmParser(), STI(sti), Parser(parser), hasConsumedDollar(false) {
204 // Initialize the set of available features.
205 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
208 MCAsmParser &getParser() const { return Parser; }
209 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
216 /// MipsOperand - Instances of this class represent a parsed Mips machine
218 class MipsOperand : public MCParsedAsmOperand {
247 MipsOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
275 SMLoc StartLoc, EndLoc;
278 void addRegOperands(MCInst &Inst, unsigned N) const {
279 assert(N == 1 && "Invalid number of operands!");
280 Inst.addOperand(MCOperand::CreateReg(getReg()));
283 void addExpr(MCInst &Inst, const MCExpr *Expr) const{
284 // Add as immediate when possible. Null MCExpr = 0.
286 Inst.addOperand(MCOperand::CreateImm(0));
287 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
288 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
290 Inst.addOperand(MCOperand::CreateExpr(Expr));
293 void addImmOperands(MCInst &Inst, unsigned N) const {
294 assert(N == 1 && "Invalid number of operands!");
295 const MCExpr *Expr = getImm();
299 void addMemOperands(MCInst &Inst, unsigned N) const {
300 assert(N == 2 && "Invalid number of operands!");
302 Inst.addOperand(MCOperand::CreateReg(getMemBase()));
304 const MCExpr *Expr = getMemOff();
308 bool isReg() const { return Kind == k_Register; }
309 bool isImm() const { return Kind == k_Immediate; }
310 bool isToken() const { return Kind == k_Token; }
311 bool isMem() const { return Kind == k_Memory; }
313 StringRef getToken() const {
314 assert(Kind == k_Token && "Invalid access!");
315 return StringRef(Tok.Data, Tok.Length);
318 unsigned getReg() const {
319 assert((Kind == k_Register) && "Invalid access!");
323 void setRegKind(RegisterKind RegKind) {
324 assert((Kind == k_Register) && "Invalid access!");
328 const MCExpr *getImm() const {
329 assert((Kind == k_Immediate) && "Invalid access!");
333 unsigned getMemBase() const {
334 assert((Kind == k_Memory) && "Invalid access!");
338 const MCExpr *getMemOff() const {
339 assert((Kind == k_Memory) && "Invalid access!");
343 static MipsOperand *CreateToken(StringRef Str, SMLoc S) {
344 MipsOperand *Op = new MipsOperand(k_Token);
345 Op->Tok.Data = Str.data();
346 Op->Tok.Length = Str.size();
352 static MipsOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
353 MipsOperand *Op = new MipsOperand(k_Register);
354 Op->Reg.RegNum = RegNum;
360 static MipsOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
361 MipsOperand *Op = new MipsOperand(k_Immediate);
368 static MipsOperand *CreateMem(unsigned Base, const MCExpr *Off,
370 MipsOperand *Op = new MipsOperand(k_Memory);
378 bool isGPR32Asm() const {
379 return Kind == k_Register && Reg.Kind == Kind_GPR32;
381 void addRegAsmOperands(MCInst &Inst, unsigned N) const {
382 Inst.addOperand(MCOperand::CreateReg(Reg.RegNum));
385 bool isGPR64Asm() const {
386 return Kind == k_Register && Reg.Kind == Kind_GPR64;
389 bool isHWRegsAsm() const {
390 assert((Kind == k_Register) && "Invalid access!");
391 return Reg.Kind == Kind_HWRegs;
394 bool isCCRAsm() const {
395 assert((Kind == k_Register) && "Invalid access!");
396 return Reg.Kind == Kind_CCRRegs;
399 bool isAFGR64Asm() const {
400 return Kind == k_Register && Reg.Kind == Kind_AFGR64Regs;
403 bool isFGR64Asm() const {
404 return Kind == k_Register && Reg.Kind == Kind_FGR64Regs;
407 bool isFGR32Asm() const {
408 return (Kind == k_Register) && Reg.Kind == Kind_FGR32Regs;
411 bool isFCCRegsAsm() const {
412 return (Kind == k_Register) && Reg.Kind == Kind_FCCRegs;
415 bool isACC64DSPAsm() const {
416 return Kind == k_Register && Reg.Kind == Kind_ACC64DSP;
419 bool isLO32DSPAsm() const {
420 return Kind == k_Register && Reg.Kind == Kind_LO32DSP;
423 bool isHI32DSPAsm() const {
424 return Kind == k_Register && Reg.Kind == Kind_HI32DSP;
427 /// getStartLoc - Get the location of the first token of this operand.
428 SMLoc getStartLoc() const {
431 /// getEndLoc - Get the location of the last token of this operand.
432 SMLoc getEndLoc() const {
436 virtual void print(raw_ostream &OS) const {
437 llvm_unreachable("unimplemented!");
439 }; // class MipsOperand
443 extern const MCInstrDesc MipsInsts[];
445 static const MCInstrDesc &getInstDesc(unsigned Opcode) {
446 return MipsInsts[Opcode];
449 bool MipsAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
450 SmallVectorImpl<MCInst> &Instructions) {
451 const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
453 if (MCID.hasDelaySlot() && Options.isReorder()) {
454 // If this instruction has a delay slot and .set reorder is active,
455 // emit a NOP after it.
456 Instructions.push_back(Inst);
458 NopInst.setOpcode(Mips::SLL);
459 NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
460 NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
461 NopInst.addOperand(MCOperand::CreateImm(0));
462 Instructions.push_back(NopInst);
466 if (MCID.mayLoad() || MCID.mayStore()) {
467 // Check the offset of memory operand, if it is a symbol
468 // reference or immediate we may have to expand instructions.
469 for (unsigned i = 0; i < MCID.getNumOperands(); i++) {
470 const MCOperandInfo &OpInfo = MCID.OpInfo[i];
471 if ((OpInfo.OperandType == MCOI::OPERAND_MEMORY)
472 || (OpInfo.OperandType == MCOI::OPERAND_UNKNOWN)) {
473 MCOperand &Op = Inst.getOperand(i);
475 int MemOffset = Op.getImm();
476 if (MemOffset < -32768 || MemOffset > 32767) {
477 // Offset can't exceed 16bit value.
478 expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), true);
481 } else if (Op.isExpr()) {
482 const MCExpr *Expr = Op.getExpr();
483 if (Expr->getKind() == MCExpr::SymbolRef) {
484 const MCSymbolRefExpr *SR =
485 static_cast<const MCSymbolRefExpr*>(Expr);
486 if (SR->getKind() == MCSymbolRefExpr::VK_None) {
488 expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), false);
491 } else if (!isEvaluated(Expr)) {
492 expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), false);
500 if (needsExpansion(Inst))
501 expandInstruction(Inst, IDLoc, Instructions);
503 Instructions.push_back(Inst);
508 bool MipsAsmParser::needsExpansion(MCInst &Inst) {
510 switch (Inst.getOpcode()) {
511 case Mips::LoadImm32Reg:
512 case Mips::LoadAddr32Imm:
513 case Mips::LoadAddr32Reg:
520 void MipsAsmParser::expandInstruction(MCInst &Inst, SMLoc IDLoc,
521 SmallVectorImpl<MCInst> &Instructions) {
522 switch (Inst.getOpcode()) {
523 case Mips::LoadImm32Reg:
524 return expandLoadImm(Inst, IDLoc, Instructions);
525 case Mips::LoadAddr32Imm:
526 return expandLoadAddressImm(Inst, IDLoc, Instructions);
527 case Mips::LoadAddr32Reg:
528 return expandLoadAddressReg(Inst, IDLoc, Instructions);
532 void MipsAsmParser::expandLoadImm(MCInst &Inst, SMLoc IDLoc,
533 SmallVectorImpl<MCInst> &Instructions) {
535 const MCOperand &ImmOp = Inst.getOperand(1);
536 assert(ImmOp.isImm() && "expected immediate operand kind");
537 const MCOperand &RegOp = Inst.getOperand(0);
538 assert(RegOp.isReg() && "expected register operand kind");
540 int ImmValue = ImmOp.getImm();
541 tmpInst.setLoc(IDLoc);
542 if (0 <= ImmValue && ImmValue <= 65535) {
543 // For 0 <= j <= 65535.
544 // li d,j => ori d,$zero,j
545 tmpInst.setOpcode(Mips::ORi);
546 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
547 tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
548 tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
549 Instructions.push_back(tmpInst);
550 } else if (ImmValue < 0 && ImmValue >= -32768) {
551 // For -32768 <= j < 0.
552 // li d,j => addiu d,$zero,j
553 tmpInst.setOpcode(Mips::ADDiu);
554 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
555 tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
556 tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
557 Instructions.push_back(tmpInst);
559 // For any other value of j that is representable as a 32-bit integer.
560 // li d,j => lui d,hi16(j)
562 tmpInst.setOpcode(Mips::LUi);
563 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
564 tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
565 Instructions.push_back(tmpInst);
567 tmpInst.setOpcode(Mips::ORi);
568 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
569 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
570 tmpInst.addOperand(MCOperand::CreateImm(ImmValue & 0xffff));
571 tmpInst.setLoc(IDLoc);
572 Instructions.push_back(tmpInst);
576 void MipsAsmParser::expandLoadAddressReg(MCInst &Inst, SMLoc IDLoc,
577 SmallVectorImpl<MCInst> &Instructions) {
579 const MCOperand &ImmOp = Inst.getOperand(2);
580 assert(ImmOp.isImm() && "expected immediate operand kind");
581 const MCOperand &SrcRegOp = Inst.getOperand(1);
582 assert(SrcRegOp.isReg() && "expected register operand kind");
583 const MCOperand &DstRegOp = Inst.getOperand(0);
584 assert(DstRegOp.isReg() && "expected register operand kind");
585 int ImmValue = ImmOp.getImm();
586 if (-32768 <= ImmValue && ImmValue <= 65535) {
587 // For -32768 <= j <= 65535.
588 // la d,j(s) => addiu d,s,j
589 tmpInst.setOpcode(Mips::ADDiu);
590 tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
591 tmpInst.addOperand(MCOperand::CreateReg(SrcRegOp.getReg()));
592 tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
593 Instructions.push_back(tmpInst);
595 // For any other value of j that is representable as a 32-bit integer.
596 // la d,j(s) => lui d,hi16(j)
599 tmpInst.setOpcode(Mips::LUi);
600 tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
601 tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
602 Instructions.push_back(tmpInst);
604 tmpInst.setOpcode(Mips::ORi);
605 tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
606 tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
607 tmpInst.addOperand(MCOperand::CreateImm(ImmValue & 0xffff));
608 Instructions.push_back(tmpInst);
610 tmpInst.setOpcode(Mips::ADDu);
611 tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
612 tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
613 tmpInst.addOperand(MCOperand::CreateReg(SrcRegOp.getReg()));
614 Instructions.push_back(tmpInst);
618 void MipsAsmParser::expandLoadAddressImm(MCInst &Inst, SMLoc IDLoc,
619 SmallVectorImpl<MCInst> &Instructions) {
621 const MCOperand &ImmOp = Inst.getOperand(1);
622 assert(ImmOp.isImm() && "expected immediate operand kind");
623 const MCOperand &RegOp = Inst.getOperand(0);
624 assert(RegOp.isReg() && "expected register operand kind");
625 int ImmValue = ImmOp.getImm();
626 if (-32768 <= ImmValue && ImmValue <= 65535) {
627 // For -32768 <= j <= 65535.
628 // la d,j => addiu d,$zero,j
629 tmpInst.setOpcode(Mips::ADDiu);
630 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
631 tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
632 tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
633 Instructions.push_back(tmpInst);
635 // For any other value of j that is representable as a 32-bit integer.
636 // la d,j => lui d,hi16(j)
638 tmpInst.setOpcode(Mips::LUi);
639 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
640 tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
641 Instructions.push_back(tmpInst);
643 tmpInst.setOpcode(Mips::ORi);
644 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
645 tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
646 tmpInst.addOperand(MCOperand::CreateImm(ImmValue & 0xffff));
647 Instructions.push_back(tmpInst);
651 void MipsAsmParser::expandMemInst(MCInst &Inst, SMLoc IDLoc,
652 SmallVectorImpl<MCInst> &Instructions, bool isLoad, bool isImmOpnd) {
653 const MCSymbolRefExpr *SR;
655 unsigned ImmOffset, HiOffset, LoOffset;
656 const MCExpr *ExprOffset;
658 unsigned AtRegNum = getReg((isMips64()) ? Mips::GPR64RegClassID
659 : Mips::GPR32RegClassID, getATReg());
660 // 1st operand is either the source or destination register.
661 assert(Inst.getOperand(0).isReg() && "expected register operand kind");
662 unsigned RegOpNum = Inst.getOperand(0).getReg();
663 // 2nd operand is the base register.
664 assert(Inst.getOperand(1).isReg() && "expected register operand kind");
665 unsigned BaseRegNum = Inst.getOperand(1).getReg();
666 // 3rd operand is either an immediate or expression.
668 assert(Inst.getOperand(2).isImm() && "expected immediate operand kind");
669 ImmOffset = Inst.getOperand(2).getImm();
670 LoOffset = ImmOffset & 0x0000ffff;
671 HiOffset = (ImmOffset & 0xffff0000) >> 16;
672 // If msb of LoOffset is 1(negative number) we must increment HiOffset.
673 if (LoOffset & 0x8000)
676 ExprOffset = Inst.getOperand(2).getExpr();
677 // All instructions will have the same location.
678 TempInst.setLoc(IDLoc);
679 // 1st instruction in expansion is LUi. For load instruction we can use
680 // the dst register as a temporary if base and dst are different,
681 // but for stores we must use $at.
682 TmpRegNum = (isLoad && (BaseRegNum != RegOpNum)) ? RegOpNum : AtRegNum;
683 TempInst.setOpcode(Mips::LUi);
684 TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
686 TempInst.addOperand(MCOperand::CreateImm(HiOffset));
688 if (ExprOffset->getKind() == MCExpr::SymbolRef) {
689 SR = static_cast<const MCSymbolRefExpr*>(ExprOffset);
690 const MCSymbolRefExpr *HiExpr = MCSymbolRefExpr::Create(
691 SR->getSymbol().getName(), MCSymbolRefExpr::VK_Mips_ABS_HI,
693 TempInst.addOperand(MCOperand::CreateExpr(HiExpr));
695 const MCExpr *HiExpr = evaluateRelocExpr(ExprOffset, "hi");
696 TempInst.addOperand(MCOperand::CreateExpr(HiExpr));
699 // Add the instruction to the list.
700 Instructions.push_back(TempInst);
701 // Prepare TempInst for next instruction.
703 // Add temp register to base.
704 TempInst.setOpcode(Mips::ADDu);
705 TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
706 TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
707 TempInst.addOperand(MCOperand::CreateReg(BaseRegNum));
708 Instructions.push_back(TempInst);
710 // And finaly, create original instruction with low part
711 // of offset and new base.
712 TempInst.setOpcode(Inst.getOpcode());
713 TempInst.addOperand(MCOperand::CreateReg(RegOpNum));
714 TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
716 TempInst.addOperand(MCOperand::CreateImm(LoOffset));
718 if (ExprOffset->getKind() == MCExpr::SymbolRef) {
719 const MCSymbolRefExpr *LoExpr = MCSymbolRefExpr::Create(
720 SR->getSymbol().getName(), MCSymbolRefExpr::VK_Mips_ABS_LO,
722 TempInst.addOperand(MCOperand::CreateExpr(LoExpr));
724 const MCExpr *LoExpr = evaluateRelocExpr(ExprOffset, "lo");
725 TempInst.addOperand(MCOperand::CreateExpr(LoExpr));
728 Instructions.push_back(TempInst);
733 MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
734 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
735 MCStreamer &Out, unsigned &ErrorInfo,
736 bool MatchingInlineAsm) {
738 SmallVector<MCInst, 8> Instructions;
739 unsigned MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
742 switch (MatchResult) {
745 case Match_Success: {
746 if (processInstruction(Inst, IDLoc, Instructions))
748 for (unsigned i = 0; i < Instructions.size(); i++)
749 Out.EmitInstruction(Instructions[i]);
752 case Match_MissingFeature:
753 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
755 case Match_InvalidOperand: {
756 SMLoc ErrorLoc = IDLoc;
757 if (ErrorInfo != ~0U) {
758 if (ErrorInfo >= Operands.size())
759 return Error(IDLoc, "too few operands for instruction");
761 ErrorLoc = ((MipsOperand*) Operands[ErrorInfo])->getStartLoc();
762 if (ErrorLoc == SMLoc())
766 return Error(ErrorLoc, "invalid operand for instruction");
768 case Match_MnemonicFail:
769 return Error(IDLoc, "invalid instruction");
774 int MipsAsmParser::matchCPURegisterName(StringRef Name) {
780 CC = StringSwitch<unsigned>(Name)
814 // Although SGI documentation just cuts out t0-t3 for n32/n64,
815 // GNU pushes the values of t0-t3 to override the o32/o64 values for t4-t7
816 // We are supporting both cases, so for t0-t3 we'll just push them to t4-t7.
817 if (isMips64() && 8 <= CC && CC <= 11)
820 if (CC == -1 && isMips64())
821 CC = StringSwitch<unsigned>(Name)
834 int MipsAsmParser::matchFPURegisterName(StringRef Name) {
836 if (Name[0] == 'f') {
837 StringRef NumString = Name.substr(1);
839 if (NumString.getAsInteger(10, IntVal))
840 return -1; // This is not an integer.
841 if (IntVal > 31) // Maximum index for fpu register.
848 int MipsAsmParser::matchFCCRegisterName(StringRef Name) {
850 if (Name.startswith("fcc")) {
851 StringRef NumString = Name.substr(3);
853 if (NumString.getAsInteger(10, IntVal))
854 return -1; // This is not an integer.
855 if (IntVal > 7) // There are only 8 fcc registers.
862 int MipsAsmParser::matchACRegisterName(StringRef Name) {
864 if (Name.startswith("acc")) {
865 StringRef NumString = Name.substr(3);
867 if (NumString.getAsInteger(10, IntVal))
868 return -1; // This is not an integer.
869 if (IntVal > 3) // There are only 3 acc registers.
876 int MipsAsmParser::matchRegisterName(StringRef Name, bool is64BitReg) {
879 CC = matchCPURegisterName(Name);
881 return matchRegisterByNumber(CC, is64BitReg ? Mips::GPR64RegClassID
882 : Mips::GPR32RegClassID);
883 CC= matchFPURegisterName(Name);
884 //TODO: decide about fpu register class
885 return matchRegisterByNumber(CC, isFP64() ? Mips::FGR64RegClassID
886 : Mips::FGR32RegClassID);
889 int MipsAsmParser::regKindToRegClass(int RegKind) {
892 case MipsOperand::Kind_GPR32: return Mips::GPR32RegClassID;
893 case MipsOperand::Kind_GPR64: return Mips::GPR64RegClassID;
894 case MipsOperand::Kind_HWRegs: return Mips::HWRegsRegClassID;
895 case MipsOperand::Kind_FGR32Regs: return Mips::FGR32RegClassID;
896 case MipsOperand::Kind_FGR64Regs: return Mips::FGR64RegClassID;
897 case MipsOperand::Kind_AFGR64Regs: return Mips::AFGR64RegClassID;
898 case MipsOperand::Kind_CCRRegs: return Mips::CCRRegClassID;
899 case MipsOperand::Kind_ACC64DSP: return Mips::ACC64DSPRegClassID;
900 case MipsOperand::Kind_FCCRegs: return Mips::FCCRegClassID;
906 bool MipsAssemblerOptions::setATReg(unsigned Reg) {
914 int MipsAsmParser::getATReg() {
915 return Options.getATRegNum();
918 unsigned MipsAsmParser::getReg(int RC, int RegNo) {
919 return *(getContext().getRegisterInfo()->getRegClass(RC).begin() + RegNo);
922 int MipsAsmParser::matchRegisterByNumber(unsigned RegNum, unsigned RegClass) {
924 getContext().getRegisterInfo()->getRegClass(RegClass).getNumRegs())
927 return getReg(RegClass, RegNum);
930 int MipsAsmParser::tryParseRegister(bool is64BitReg) {
931 const AsmToken &Tok = Parser.getTok();
934 if (Tok.is(AsmToken::Identifier)) {
935 std::string lowerCase = Tok.getString().lower();
936 RegNum = matchRegisterName(lowerCase, is64BitReg);
937 } else if (Tok.is(AsmToken::Integer))
938 RegNum = matchRegisterByNumber(static_cast<unsigned>(Tok.getIntVal()),
939 is64BitReg ? Mips::GPR64RegClassID : Mips::GPR32RegClassID);
943 bool MipsAsmParser::tryParseRegisterOperand(
944 SmallVectorImpl<MCParsedAsmOperand*> &Operands, bool is64BitReg) {
946 SMLoc S = Parser.getTok().getLoc();
949 RegNo = tryParseRegister(is64BitReg);
953 Operands.push_back(MipsOperand::CreateReg(RegNo, S,
954 Parser.getTok().getLoc()));
955 Parser.Lex(); // Eat register token.
959 bool MipsAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*>&Operands,
960 StringRef Mnemonic) {
961 // Check if the current operand has a custom associated parser, if so, try to
962 // custom parse the operand, or fallback to the general approach.
963 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
964 if (ResTy == MatchOperand_Success)
966 // If there wasn't a custom match, try the generic matcher below. Otherwise,
967 // there was a match, but an error occurred, in which case, just return that
968 // the operand parsing failed.
969 if (ResTy == MatchOperand_ParseFail)
972 switch (getLexer().getKind()) {
974 Error(Parser.getTok().getLoc(), "unexpected token in operand");
976 case AsmToken::Dollar: {
977 // Parse the register.
978 SMLoc S = Parser.getTok().getLoc();
979 Parser.Lex(); // Eat dollar token.
980 // Parse the register operand.
981 if (!tryParseRegisterOperand(Operands, isMips64())) {
982 if (getLexer().is(AsmToken::LParen)) {
983 // Check if it is indexed addressing operand.
984 Operands.push_back(MipsOperand::CreateToken("(", S));
985 Parser.Lex(); // Eat the parenthesis.
986 if (getLexer().isNot(AsmToken::Dollar))
989 Parser.Lex(); // Eat the dollar
990 if (tryParseRegisterOperand(Operands, isMips64()))
993 if (!getLexer().is(AsmToken::RParen))
996 S = Parser.getTok().getLoc();
997 Operands.push_back(MipsOperand::CreateToken(")", S));
1002 // Maybe it is a symbol reference.
1003 StringRef Identifier;
1004 if (Parser.parseIdentifier(Identifier))
1007 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1009 MCSymbol *Sym = getContext().GetOrCreateSymbol("$" + Identifier);
1011 // Otherwise create a symbol reference.
1012 const MCExpr *Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
1015 Operands.push_back(MipsOperand::CreateImm(Res, S, E));
1018 case AsmToken::Identifier:
1019 // Look for the existing symbol, we should check if
1020 // we need to assigne the propper RegisterKind.
1021 if (searchSymbolAlias(Operands, MipsOperand::Kind_None))
1023 // Else drop to expression parsing.
1024 case AsmToken::LParen:
1025 case AsmToken::Minus:
1026 case AsmToken::Plus:
1027 case AsmToken::Integer:
1028 case AsmToken::String: {
1029 // Quoted label names.
1030 const MCExpr *IdVal;
1031 SMLoc S = Parser.getTok().getLoc();
1032 if (getParser().parseExpression(IdVal))
1034 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1035 Operands.push_back(MipsOperand::CreateImm(IdVal, S, E));
1038 case AsmToken::Percent: {
1039 // It is a symbol reference or constant expression.
1040 const MCExpr *IdVal;
1041 SMLoc S = Parser.getTok().getLoc(); // Start location of the operand.
1042 if (parseRelocOperand(IdVal))
1045 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1047 Operands.push_back(MipsOperand::CreateImm(IdVal, S, E));
1049 } // case AsmToken::Percent
1050 } // switch(getLexer().getKind())
1054 const MCExpr* MipsAsmParser::evaluateRelocExpr(const MCExpr *Expr,
1055 StringRef RelocStr) {
1057 // Check the type of the expression.
1058 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Expr)) {
1059 // It's a constant, evaluate lo or hi value.
1060 if (RelocStr == "lo") {
1061 short Val = MCE->getValue();
1062 Res = MCConstantExpr::Create(Val, getContext());
1063 } else if (RelocStr == "hi") {
1064 int Val = MCE->getValue();
1065 int LoSign = Val & 0x8000;
1066 Val = (Val & 0xffff0000) >> 16;
1067 // Lower part is treated as a signed int, so if it is negative
1068 // we must add 1 to the hi part to compensate.
1071 Res = MCConstantExpr::Create(Val, getContext());
1073 llvm_unreachable("Invalid RelocStr value");
1078 if (const MCSymbolRefExpr *MSRE = dyn_cast<MCSymbolRefExpr>(Expr)) {
1079 // It's a symbol, create a symbolic expression from the symbol.
1080 StringRef Symbol = MSRE->getSymbol().getName();
1081 MCSymbolRefExpr::VariantKind VK = getVariantKind(RelocStr);
1082 Res = MCSymbolRefExpr::Create(Symbol, VK, getContext());
1086 if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
1087 const MCExpr *LExp = evaluateRelocExpr(BE->getLHS(), RelocStr);
1088 const MCExpr *RExp = evaluateRelocExpr(BE->getRHS(), RelocStr);
1089 Res = MCBinaryExpr::Create(BE->getOpcode(), LExp, RExp, getContext());
1093 if (const MCUnaryExpr *UN = dyn_cast<MCUnaryExpr>(Expr)) {
1094 const MCExpr *UnExp = evaluateRelocExpr(UN->getSubExpr(), RelocStr);
1095 Res = MCUnaryExpr::Create(UN->getOpcode(), UnExp, getContext());
1098 // Just return the original expression.
1102 bool MipsAsmParser::isEvaluated(const MCExpr *Expr) {
1104 switch (Expr->getKind()) {
1105 case MCExpr::Constant:
1107 case MCExpr::SymbolRef:
1108 return (cast<MCSymbolRefExpr>(Expr)->getKind() != MCSymbolRefExpr::VK_None);
1109 case MCExpr::Binary:
1110 if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
1111 if (!isEvaluated(BE->getLHS()))
1113 return isEvaluated(BE->getRHS());
1116 return isEvaluated(cast<MCUnaryExpr>(Expr)->getSubExpr());
1123 bool MipsAsmParser::parseRelocOperand(const MCExpr *&Res) {
1124 Parser.Lex(); // Eat the % token.
1125 const AsmToken &Tok = Parser.getTok(); // Get next token, operation.
1126 if (Tok.isNot(AsmToken::Identifier))
1129 std::string Str = Tok.getIdentifier().str();
1131 Parser.Lex(); // Eat the identifier.
1132 // Now make an expression from the rest of the operand.
1133 const MCExpr *IdVal;
1136 if (getLexer().getKind() == AsmToken::LParen) {
1138 Parser.Lex(); // Eat the '(' token.
1139 if (getLexer().getKind() == AsmToken::Percent) {
1140 Parser.Lex(); // Eat the % token.
1141 const AsmToken &nextTok = Parser.getTok();
1142 if (nextTok.isNot(AsmToken::Identifier))
1145 Str += nextTok.getIdentifier();
1146 Parser.Lex(); // Eat the identifier.
1147 if (getLexer().getKind() != AsmToken::LParen)
1152 if (getParser().parseParenExpression(IdVal, EndLoc))
1155 while (getLexer().getKind() == AsmToken::RParen)
1156 Parser.Lex(); // Eat the ')' token.
1159 return true; // Parenthesis must follow the relocation operand.
1161 Res = evaluateRelocExpr(IdVal, Str);
1165 bool MipsAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
1167 StartLoc = Parser.getTok().getLoc();
1168 RegNo = tryParseRegister(isMips64());
1169 EndLoc = Parser.getTok().getLoc();
1170 return (RegNo == (unsigned) -1);
1173 bool MipsAsmParser::parseMemOffset(const MCExpr *&Res, bool isParenExpr) {
1177 while (getLexer().getKind() == AsmToken::LParen)
1180 switch (getLexer().getKind()) {
1183 case AsmToken::Identifier:
1184 case AsmToken::LParen:
1185 case AsmToken::Integer:
1186 case AsmToken::Minus:
1187 case AsmToken::Plus:
1189 Result = getParser().parseParenExpression(Res, S);
1191 Result = (getParser().parseExpression(Res));
1192 while (getLexer().getKind() == AsmToken::RParen)
1195 case AsmToken::Percent:
1196 Result = parseRelocOperand(Res);
1201 MipsAsmParser::OperandMatchResultTy MipsAsmParser::parseMemOperand(
1202 SmallVectorImpl<MCParsedAsmOperand*>&Operands) {
1204 const MCExpr *IdVal = 0;
1206 bool isParenExpr = false;
1207 MipsAsmParser::OperandMatchResultTy Res = MatchOperand_NoMatch;
1208 // First operand is the offset.
1209 S = Parser.getTok().getLoc();
1211 if (getLexer().getKind() == AsmToken::LParen) {
1216 if (getLexer().getKind() != AsmToken::Dollar) {
1217 if (parseMemOffset(IdVal, isParenExpr))
1218 return MatchOperand_ParseFail;
1220 const AsmToken &Tok = Parser.getTok(); // Get the next token.
1221 if (Tok.isNot(AsmToken::LParen)) {
1222 MipsOperand *Mnemonic = static_cast<MipsOperand*>(Operands[0]);
1223 if (Mnemonic->getToken() == "la") {
1224 SMLoc E = SMLoc::getFromPointer(
1225 Parser.getTok().getLoc().getPointer() - 1);
1226 Operands.push_back(MipsOperand::CreateImm(IdVal, S, E));
1227 return MatchOperand_Success;
1229 if (Tok.is(AsmToken::EndOfStatement)) {
1230 SMLoc E = SMLoc::getFromPointer(
1231 Parser.getTok().getLoc().getPointer() - 1);
1233 // Zero register assumed, add a memory operand with ZERO as its base.
1234 Operands.push_back(MipsOperand::CreateMem(isMips64() ? Mips::ZERO_64
1237 return MatchOperand_Success;
1239 Error(Parser.getTok().getLoc(), "'(' expected");
1240 return MatchOperand_ParseFail;
1243 Parser.Lex(); // Eat the '(' token.
1246 Res = parseRegs(Operands, isMips64()? (int) MipsOperand::Kind_GPR64:
1247 (int) MipsOperand::Kind_GPR32);
1248 if (Res != MatchOperand_Success)
1251 if (Parser.getTok().isNot(AsmToken::RParen)) {
1252 Error(Parser.getTok().getLoc(), "')' expected");
1253 return MatchOperand_ParseFail;
1256 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1258 Parser.Lex(); // Eat the ')' token.
1261 IdVal = MCConstantExpr::Create(0, getContext());
1263 // Replace the register operand with the memory operand.
1264 MipsOperand* op = static_cast<MipsOperand*>(Operands.back());
1265 int RegNo = op->getReg();
1266 // Remove the register from the operands.
1267 Operands.pop_back();
1268 // Add the memory operand.
1269 if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(IdVal)) {
1271 if (IdVal->EvaluateAsAbsolute(Imm))
1272 IdVal = MCConstantExpr::Create(Imm, getContext());
1273 else if (BE->getLHS()->getKind() != MCExpr::SymbolRef)
1274 IdVal = MCBinaryExpr::Create(BE->getOpcode(), BE->getRHS(), BE->getLHS(),
1278 Operands.push_back(MipsOperand::CreateMem(RegNo, IdVal, S, E));
1280 return MatchOperand_Success;
1283 MipsAsmParser::OperandMatchResultTy
1284 MipsAsmParser::parseRegs(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1286 MipsOperand::RegisterKind Kind = (MipsOperand::RegisterKind)RegKind;
1287 if (getLexer().getKind() == AsmToken::Identifier
1288 && !hasConsumedDollar) {
1289 if (searchSymbolAlias(Operands, Kind))
1290 return MatchOperand_Success;
1291 return MatchOperand_NoMatch;
1293 SMLoc S = Parser.getTok().getLoc();
1294 // If the first token is not '$', we have an error.
1295 if (Parser.getTok().isNot(AsmToken::Dollar) && !hasConsumedDollar)
1296 return MatchOperand_NoMatch;
1297 if (!hasConsumedDollar) {
1298 Parser.Lex(); // Eat the '$'
1299 hasConsumedDollar = true;
1301 if (getLexer().getKind() == AsmToken::Identifier) {
1303 std::string RegName = Parser.getTok().getString().lower();
1304 // Match register by name
1306 case MipsOperand::Kind_GPR32:
1307 case MipsOperand::Kind_GPR64:
1308 RegNum = matchCPURegisterName(RegName);
1310 case MipsOperand::Kind_AFGR64Regs:
1311 case MipsOperand::Kind_FGR64Regs:
1312 case MipsOperand::Kind_FGR32Regs:
1313 RegNum = matchFPURegisterName(RegName);
1314 if (RegKind == MipsOperand::Kind_AFGR64Regs)
1317 case MipsOperand::Kind_FCCRegs:
1318 RegNum = matchFCCRegisterName(RegName);
1320 case MipsOperand::Kind_ACC64DSP:
1321 RegNum = matchACRegisterName(RegName);
1323 default: break; // No match, value is set to -1.
1325 // No match found, return _NoMatch to give a chance to other round.
1327 return MatchOperand_NoMatch;
1329 int RegVal = getReg(regKindToRegClass(Kind), RegNum);
1331 return MatchOperand_NoMatch;
1333 MipsOperand *Op = MipsOperand::CreateReg(RegVal, S,
1334 Parser.getTok().getLoc());
1335 Op->setRegKind(Kind);
1336 Operands.push_back(Op);
1337 hasConsumedDollar = false;
1338 Parser.Lex(); // Eat the register name.
1339 if ((RegKind == MipsOperand::Kind_GPR32)
1340 && (getLexer().is(AsmToken::LParen))) {
1341 // Check if it is indexed addressing operand.
1342 Operands.push_back(MipsOperand::CreateToken("(", getLexer().getLoc()));
1343 Parser.Lex(); // Eat the parenthesis.
1344 if (parseRegs(Operands,RegKind) != MatchOperand_Success)
1345 return MatchOperand_NoMatch;
1346 if (getLexer().isNot(AsmToken::RParen))
1347 return MatchOperand_NoMatch;
1348 Operands.push_back(MipsOperand::CreateToken(")", getLexer().getLoc()));
1351 return MatchOperand_Success;
1352 } else if (getLexer().getKind() == AsmToken::Integer) {
1353 unsigned RegNum = Parser.getTok().getIntVal();
1354 if (Kind == MipsOperand::Kind_HWRegs) {
1356 return MatchOperand_NoMatch;
1357 // Only hwreg 29 is supported, found at index 0.
1360 int Reg = matchRegisterByNumber(RegNum, regKindToRegClass(Kind));
1362 return MatchOperand_NoMatch;
1363 MipsOperand *Op = MipsOperand::CreateReg(Reg, S, Parser.getTok().getLoc());
1364 Op->setRegKind(Kind);
1365 Operands.push_back(Op);
1366 hasConsumedDollar = false;
1367 Parser.Lex(); // Eat the register number.
1368 if ((RegKind == MipsOperand::Kind_GPR32)
1369 && (getLexer().is(AsmToken::LParen))) {
1370 // Check if it is indexed addressing operand.
1371 Operands.push_back(MipsOperand::CreateToken("(", getLexer().getLoc()));
1372 Parser.Lex(); // Eat the parenthesis.
1373 if (parseRegs(Operands,RegKind) != MatchOperand_Success)
1374 return MatchOperand_NoMatch;
1375 if (getLexer().isNot(AsmToken::RParen))
1376 return MatchOperand_NoMatch;
1377 Operands.push_back(MipsOperand::CreateToken(")", getLexer().getLoc()));
1380 return MatchOperand_Success;
1382 return MatchOperand_NoMatch;
1385 MipsAsmParser::OperandMatchResultTy
1386 MipsAsmParser::parseGPR64(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1389 return MatchOperand_NoMatch;
1390 return parseRegs(Operands, (int) MipsOperand::Kind_GPR64);
1393 MipsAsmParser::OperandMatchResultTy
1394 MipsAsmParser::parseGPR32(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1395 return parseRegs(Operands, (int) MipsOperand::Kind_GPR32);
1398 MipsAsmParser::OperandMatchResultTy
1399 MipsAsmParser::parseAFGR64Regs(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1402 return MatchOperand_NoMatch;
1403 return parseRegs(Operands, (int) MipsOperand::Kind_AFGR64Regs);
1406 MipsAsmParser::OperandMatchResultTy
1407 MipsAsmParser::parseFGR64Regs(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1409 return MatchOperand_NoMatch;
1410 return parseRegs(Operands, (int) MipsOperand::Kind_FGR64Regs);
1413 MipsAsmParser::OperandMatchResultTy
1414 MipsAsmParser::parseFGR32Regs(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1415 return parseRegs(Operands, (int) MipsOperand::Kind_FGR32Regs);
1418 MipsAsmParser::OperandMatchResultTy
1419 MipsAsmParser::parseFCCRegs(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1420 return parseRegs(Operands, (int) MipsOperand::Kind_FCCRegs);
1423 MipsAsmParser::OperandMatchResultTy
1424 MipsAsmParser::parseACC64DSP(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1425 return parseRegs(Operands, (int) MipsOperand::Kind_ACC64DSP);
1428 MipsAsmParser::OperandMatchResultTy
1429 MipsAsmParser::parseLO32DSP(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1430 // If the first token is not '$' we have an error.
1431 if (Parser.getTok().isNot(AsmToken::Dollar))
1432 return MatchOperand_NoMatch;
1434 SMLoc S = Parser.getTok().getLoc();
1435 Parser.Lex(); // Eat the '$'
1437 const AsmToken &Tok = Parser.getTok(); // Get next token.
1439 if (Tok.isNot(AsmToken::Identifier))
1440 return MatchOperand_NoMatch;
1442 if (!Tok.getIdentifier().startswith("ac"))
1443 return MatchOperand_NoMatch;
1445 StringRef NumString = Tok.getIdentifier().substr(2);
1448 if (NumString.getAsInteger(10, IntVal))
1449 return MatchOperand_NoMatch;
1451 unsigned Reg = matchRegisterByNumber(IntVal, Mips::LO32DSPRegClassID);
1453 MipsOperand *Op = MipsOperand::CreateReg(Reg, S, Parser.getTok().getLoc());
1454 Op->setRegKind(MipsOperand::Kind_LO32DSP);
1455 Operands.push_back(Op);
1457 Parser.Lex(); // Eat the register number.
1458 return MatchOperand_Success;
1461 MipsAsmParser::OperandMatchResultTy
1462 MipsAsmParser::parseHI32DSP(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1463 // If the first token is not '$' we have an error.
1464 if (Parser.getTok().isNot(AsmToken::Dollar))
1465 return MatchOperand_NoMatch;
1467 SMLoc S = Parser.getTok().getLoc();
1468 Parser.Lex(); // Eat the '$'
1470 const AsmToken &Tok = Parser.getTok(); // Get next token.
1472 if (Tok.isNot(AsmToken::Identifier))
1473 return MatchOperand_NoMatch;
1475 if (!Tok.getIdentifier().startswith("ac"))
1476 return MatchOperand_NoMatch;
1478 StringRef NumString = Tok.getIdentifier().substr(2);
1481 if (NumString.getAsInteger(10, IntVal))
1482 return MatchOperand_NoMatch;
1484 unsigned Reg = matchRegisterByNumber(IntVal, Mips::HI32DSPRegClassID);
1486 MipsOperand *Op = MipsOperand::CreateReg(Reg, S, Parser.getTok().getLoc());
1487 Op->setRegKind(MipsOperand::Kind_HI32DSP);
1488 Operands.push_back(Op);
1490 Parser.Lex(); // Eat the register number.
1491 return MatchOperand_Success;
1494 bool MipsAsmParser::searchSymbolAlias(
1495 SmallVectorImpl<MCParsedAsmOperand*> &Operands, unsigned RegKind) {
1497 MCSymbol *Sym = getContext().LookupSymbol(Parser.getTok().getIdentifier());
1499 SMLoc S = Parser.getTok().getLoc();
1501 if (Sym->isVariable())
1502 Expr = Sym->getVariableValue();
1505 if (Expr->getKind() == MCExpr::SymbolRef) {
1506 MipsOperand::RegisterKind Kind = (MipsOperand::RegisterKind) RegKind;
1507 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Expr);
1508 const StringRef DefSymbol = Ref->getSymbol().getName();
1509 if (DefSymbol.startswith("$")) {
1511 APInt IntVal(32, -1);
1512 if (!DefSymbol.substr(1).getAsInteger(10, IntVal))
1513 RegNum = matchRegisterByNumber(IntVal.getZExtValue(),
1515 ? Mips::GPR64RegClassID
1516 : Mips::GPR32RegClassID);
1518 // Lookup for the register with the corresponding name.
1520 case MipsOperand::Kind_AFGR64Regs:
1521 case MipsOperand::Kind_FGR64Regs:
1522 RegNum = matchFPURegisterName(DefSymbol.substr(1));
1524 case MipsOperand::Kind_FGR32Regs:
1525 RegNum = matchFPURegisterName(DefSymbol.substr(1));
1527 case MipsOperand::Kind_GPR64:
1528 case MipsOperand::Kind_GPR32:
1530 RegNum = matchCPURegisterName(DefSymbol.substr(1));
1534 RegNum = getReg(regKindToRegClass(Kind), RegNum);
1538 MipsOperand *op = MipsOperand::CreateReg(RegNum, S,
1539 Parser.getTok().getLoc());
1540 op->setRegKind(Kind);
1541 Operands.push_back(op);
1545 } else if (Expr->getKind() == MCExpr::Constant) {
1547 const MCConstantExpr *Const = static_cast<const MCConstantExpr*>(Expr);
1548 MipsOperand *op = MipsOperand::CreateImm(Const, S,
1549 Parser.getTok().getLoc());
1550 Operands.push_back(op);
1557 MipsAsmParser::OperandMatchResultTy
1558 MipsAsmParser::parseHWRegs(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1559 return parseRegs(Operands, (int) MipsOperand::Kind_HWRegs);
1562 MipsAsmParser::OperandMatchResultTy
1563 MipsAsmParser::parseCCRRegs(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1564 return parseRegs(Operands, (int) MipsOperand::Kind_CCRRegs);
1567 MCSymbolRefExpr::VariantKind MipsAsmParser::getVariantKind(StringRef Symbol) {
1569 MCSymbolRefExpr::VariantKind VK
1570 = StringSwitch<MCSymbolRefExpr::VariantKind>(Symbol)
1571 .Case("hi", MCSymbolRefExpr::VK_Mips_ABS_HI)
1572 .Case("lo", MCSymbolRefExpr::VK_Mips_ABS_LO)
1573 .Case("gp_rel", MCSymbolRefExpr::VK_Mips_GPREL)
1574 .Case("call16", MCSymbolRefExpr::VK_Mips_GOT_CALL)
1575 .Case("got", MCSymbolRefExpr::VK_Mips_GOT)
1576 .Case("tlsgd", MCSymbolRefExpr::VK_Mips_TLSGD)
1577 .Case("tlsldm", MCSymbolRefExpr::VK_Mips_TLSLDM)
1578 .Case("dtprel_hi", MCSymbolRefExpr::VK_Mips_DTPREL_HI)
1579 .Case("dtprel_lo", MCSymbolRefExpr::VK_Mips_DTPREL_LO)
1580 .Case("gottprel", MCSymbolRefExpr::VK_Mips_GOTTPREL)
1581 .Case("tprel_hi", MCSymbolRefExpr::VK_Mips_TPREL_HI)
1582 .Case("tprel_lo", MCSymbolRefExpr::VK_Mips_TPREL_LO)
1583 .Case("got_disp", MCSymbolRefExpr::VK_Mips_GOT_DISP)
1584 .Case("got_page", MCSymbolRefExpr::VK_Mips_GOT_PAGE)
1585 .Case("got_ofst", MCSymbolRefExpr::VK_Mips_GOT_OFST)
1586 .Case("hi(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_HI)
1587 .Case("lo(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_LO)
1588 .Default(MCSymbolRefExpr::VK_None);
1593 bool MipsAsmParser::
1594 ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
1595 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1596 // Check if we have valid mnemonic
1597 if (!mnemonicIsValid(Name, 0)) {
1598 Parser.eatToEndOfStatement();
1599 return Error(NameLoc, "Unknown instruction");
1601 // First operand in MCInst is instruction mnemonic.
1602 Operands.push_back(MipsOperand::CreateToken(Name, NameLoc));
1604 // Read the remaining operands.
1605 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1606 // Read the first operand.
1607 if (ParseOperand(Operands, Name)) {
1608 SMLoc Loc = getLexer().getLoc();
1609 Parser.eatToEndOfStatement();
1610 return Error(Loc, "unexpected token in argument list");
1613 while (getLexer().is(AsmToken::Comma)) {
1614 Parser.Lex(); // Eat the comma.
1615 // Parse and remember the operand.
1616 if (ParseOperand(Operands, Name)) {
1617 SMLoc Loc = getLexer().getLoc();
1618 Parser.eatToEndOfStatement();
1619 return Error(Loc, "unexpected token in argument list");
1623 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1624 SMLoc Loc = getLexer().getLoc();
1625 Parser.eatToEndOfStatement();
1626 return Error(Loc, "unexpected token in argument list");
1628 Parser.Lex(); // Consume the EndOfStatement.
1632 bool MipsAsmParser::reportParseError(StringRef ErrorMsg) {
1633 SMLoc Loc = getLexer().getLoc();
1634 Parser.eatToEndOfStatement();
1635 return Error(Loc, ErrorMsg);
1638 bool MipsAsmParser::parseSetNoAtDirective() {
1639 // Line should look like: ".set noat".
1641 Options.setATReg(0);
1644 // If this is not the end of the statement, report an error.
1645 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1646 reportParseError("unexpected token in statement");
1649 Parser.Lex(); // Consume the EndOfStatement.
1653 bool MipsAsmParser::parseSetAtDirective() {
1654 // Line can be .set at - defaults to $1
1658 if (getLexer().is(AsmToken::EndOfStatement)) {
1659 Options.setATReg(1);
1660 Parser.Lex(); // Consume the EndOfStatement.
1662 } else if (getLexer().is(AsmToken::Equal)) {
1663 getParser().Lex(); // Eat the '='.
1664 if (getLexer().isNot(AsmToken::Dollar)) {
1665 reportParseError("unexpected token in statement");
1668 Parser.Lex(); // Eat the '$'.
1669 const AsmToken &Reg = Parser.getTok();
1670 if (Reg.is(AsmToken::Identifier)) {
1671 AtRegNo = matchCPURegisterName(Reg.getIdentifier());
1672 } else if (Reg.is(AsmToken::Integer)) {
1673 AtRegNo = Reg.getIntVal();
1675 reportParseError("unexpected token in statement");
1679 if (AtRegNo < 1 || AtRegNo > 31) {
1680 reportParseError("unexpected token in statement");
1684 if (!Options.setATReg(AtRegNo)) {
1685 reportParseError("unexpected token in statement");
1688 getParser().Lex(); // Eat the register.
1690 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1691 reportParseError("unexpected token in statement");
1694 Parser.Lex(); // Consume the EndOfStatement.
1697 reportParseError("unexpected token in statement");
1702 bool MipsAsmParser::parseSetReorderDirective() {
1704 // If this is not the end of the statement, report an error.
1705 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1706 reportParseError("unexpected token in statement");
1709 Options.setReorder();
1710 Parser.Lex(); // Consume the EndOfStatement.
1714 bool MipsAsmParser::parseSetNoReorderDirective() {
1716 // If this is not the end of the statement, report an error.
1717 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1718 reportParseError("unexpected token in statement");
1721 Options.setNoreorder();
1722 Parser.Lex(); // Consume the EndOfStatement.
1726 bool MipsAsmParser::parseSetMacroDirective() {
1728 // If this is not the end of the statement, report an error.
1729 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1730 reportParseError("unexpected token in statement");
1734 Parser.Lex(); // Consume the EndOfStatement.
1738 bool MipsAsmParser::parseSetNoMacroDirective() {
1740 // If this is not the end of the statement, report an error.
1741 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1742 reportParseError("`noreorder' must be set before `nomacro'");
1745 if (Options.isReorder()) {
1746 reportParseError("`noreorder' must be set before `nomacro'");
1749 Options.setNomacro();
1750 Parser.Lex(); // Consume the EndOfStatement.
1754 bool MipsAsmParser::parseSetAssignment() {
1756 const MCExpr *Value;
1758 if (Parser.parseIdentifier(Name))
1759 reportParseError("expected identifier after .set");
1761 if (getLexer().isNot(AsmToken::Comma))
1762 return reportParseError("unexpected token in .set directive");
1765 if (getLexer().is(AsmToken::Dollar)) {
1767 SMLoc DollarLoc = getLexer().getLoc();
1768 // Consume the dollar sign, and check for a following identifier.
1770 // We have a '$' followed by something, make sure they are adjacent.
1771 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer())
1773 StringRef Res = StringRef(DollarLoc.getPointer(),
1774 getTok().getEndLoc().getPointer() - DollarLoc.getPointer());
1775 Symbol = getContext().GetOrCreateSymbol(Res);
1777 Value = MCSymbolRefExpr::Create(Symbol, MCSymbolRefExpr::VK_None,
1779 } else if (Parser.parseExpression(Value))
1780 return reportParseError("expected valid expression after comma");
1782 // Check if the Name already exists as a symbol.
1783 MCSymbol *Sym = getContext().LookupSymbol(Name);
1785 return reportParseError("symbol already defined");
1786 Sym = getContext().GetOrCreateSymbol(Name);
1787 Sym->setVariableValue(Value);
1792 bool MipsAsmParser::parseDirectiveSet() {
1794 // Get the next token.
1795 const AsmToken &Tok = Parser.getTok();
1797 if (Tok.getString() == "noat") {
1798 return parseSetNoAtDirective();
1799 } else if (Tok.getString() == "at") {
1800 return parseSetAtDirective();
1801 } else if (Tok.getString() == "reorder") {
1802 return parseSetReorderDirective();
1803 } else if (Tok.getString() == "noreorder") {
1804 return parseSetNoReorderDirective();
1805 } else if (Tok.getString() == "macro") {
1806 return parseSetMacroDirective();
1807 } else if (Tok.getString() == "nomacro") {
1808 return parseSetNoMacroDirective();
1809 } else if (Tok.getString() == "nomips16") {
1810 // Ignore this directive for now.
1811 Parser.eatToEndOfStatement();
1813 } else if (Tok.getString() == "nomicromips") {
1814 // Ignore this directive for now.
1815 Parser.eatToEndOfStatement();
1818 // It is just an identifier, look for an assignment.
1819 parseSetAssignment();
1826 /// parseDirectiveWord
1827 /// ::= .word [ expression (, expression)* ]
1828 bool MipsAsmParser::parseDirectiveWord(unsigned Size, SMLoc L) {
1829 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1831 const MCExpr *Value;
1832 if (getParser().parseExpression(Value))
1835 getParser().getStreamer().EmitValue(Value, Size);
1837 if (getLexer().is(AsmToken::EndOfStatement))
1840 // FIXME: Improve diagnostic.
1841 if (getLexer().isNot(AsmToken::Comma))
1842 return Error(L, "unexpected token in directive");
1851 bool MipsAsmParser::ParseDirective(AsmToken DirectiveID) {
1853 StringRef IDVal = DirectiveID.getString();
1855 if (IDVal == ".ent") {
1856 // Ignore this directive for now.
1861 if (IDVal == ".end") {
1862 // Ignore this directive for now.
1867 if (IDVal == ".frame") {
1868 // Ignore this directive for now.
1869 Parser.eatToEndOfStatement();
1873 if (IDVal == ".set") {
1874 return parseDirectiveSet();
1877 if (IDVal == ".fmask") {
1878 // Ignore this directive for now.
1879 Parser.eatToEndOfStatement();
1883 if (IDVal == ".mask") {
1884 // Ignore this directive for now.
1885 Parser.eatToEndOfStatement();
1889 if (IDVal == ".gpword") {
1890 // Ignore this directive for now.
1891 Parser.eatToEndOfStatement();
1895 if (IDVal == ".word") {
1896 parseDirectiveWord(4, DirectiveID.getLoc());
1903 extern "C" void LLVMInitializeMipsAsmParser() {
1904 RegisterMCAsmParser<MipsAsmParser> X(TheMipsTarget);
1905 RegisterMCAsmParser<MipsAsmParser> Y(TheMipselTarget);
1906 RegisterMCAsmParser<MipsAsmParser> A(TheMips64Target);
1907 RegisterMCAsmParser<MipsAsmParser> B(TheMips64elTarget);
1910 #define GET_REGISTER_MATCHER
1911 #define GET_MATCHER_IMPLEMENTATION
1912 #include "MipsGenAsmMatcher.inc"