[mips] Renamed ParseAnyRegisterWithoutDollar to MatchAnyRegisterWithoutDollar
[oota-llvm.git] / lib / Target / Mips / AsmParser / MipsAsmParser.cpp
1 //===-- MipsAsmParser.cpp - Parse Mips assembly to MCInst instructions ----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "MCTargetDesc/MipsMCExpr.h"
11 #include "MCTargetDesc/MipsMCTargetDesc.h"
12 #include "MipsRegisterInfo.h"
13 #include "MipsTargetStreamer.h"
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCInstBuilder.h"
20 #include "llvm/MC/MCParser/MCAsmLexer.h"
21 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/MC/MCTargetAsmParser.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/TargetRegistry.h"
29
30 using namespace llvm;
31
32 namespace llvm {
33 class MCInstrInfo;
34 }
35
36 namespace {
37 class MipsAssemblerOptions {
38 public:
39   MipsAssemblerOptions() : aTReg(1), reorder(true), macro(true) {}
40
41   unsigned getATRegNum() { return aTReg; }
42   bool setATReg(unsigned Reg);
43
44   bool isReorder() { return reorder; }
45   void setReorder() { reorder = true; }
46   void setNoreorder() { reorder = false; }
47
48   bool isMacro() { return macro; }
49   void setMacro() { macro = true; }
50   void setNomacro() { macro = false; }
51
52 private:
53   unsigned aTReg;
54   bool reorder;
55   bool macro;
56 };
57 }
58
59 namespace {
60 class MipsAsmParser : public MCTargetAsmParser {
61   MipsTargetStreamer &getTargetStreamer() {
62     MCTargetStreamer &TS = *Parser.getStreamer().getTargetStreamer();
63     return static_cast<MipsTargetStreamer &>(TS);
64   }
65
66   MCSubtargetInfo &STI;
67   MCAsmParser &Parser;
68   MipsAssemblerOptions Options;
69
70 #define GET_ASSEMBLER_HEADER
71 #include "MipsGenAsmMatcher.inc"
72
73   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
74                                SmallVectorImpl<MCParsedAsmOperand *> &Operands,
75                                MCStreamer &Out, unsigned &ErrorInfo,
76                                bool MatchingInlineAsm);
77
78   /// Parse a register as used in CFI directives
79   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
80
81   bool ParseParenSuffix(StringRef Name,
82                         SmallVectorImpl<MCParsedAsmOperand *> &Operands);
83
84   bool ParseBracketSuffix(StringRef Name,
85                           SmallVectorImpl<MCParsedAsmOperand *> &Operands);
86
87   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
88                         SMLoc NameLoc,
89                         SmallVectorImpl<MCParsedAsmOperand *> &Operands);
90
91   bool ParseDirective(AsmToken DirectiveID);
92
93   MipsAsmParser::OperandMatchResultTy
94   parseMemOperand(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
95
96   MipsAsmParser::OperandMatchResultTy MatchAnyRegisterNameWithoutDollar(
97       SmallVectorImpl<MCParsedAsmOperand *> &Operands, StringRef Identifier,
98       SMLoc S);
99
100   MipsAsmParser::OperandMatchResultTy
101   MatchAnyRegisterWithoutDollar(SmallVectorImpl<MCParsedAsmOperand *> &Operands,
102                                 SMLoc S);
103
104   MipsAsmParser::OperandMatchResultTy
105   ParseAnyRegister(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
106
107   MipsAsmParser::OperandMatchResultTy
108   ParseImm(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
109
110   MipsAsmParser::OperandMatchResultTy
111   ParseJumpTarget(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
112
113   MipsAsmParser::OperandMatchResultTy
114   parseInvNum(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
115
116   MipsAsmParser::OperandMatchResultTy
117   ParseLSAImm(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
118
119   bool searchSymbolAlias(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
120
121   bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand *> &,
122                     StringRef Mnemonic);
123
124   bool needsExpansion(MCInst &Inst);
125
126   void expandInstruction(MCInst &Inst, SMLoc IDLoc,
127                          SmallVectorImpl<MCInst> &Instructions);
128   void expandLoadImm(MCInst &Inst, SMLoc IDLoc,
129                      SmallVectorImpl<MCInst> &Instructions);
130   void expandLoadAddressImm(MCInst &Inst, SMLoc IDLoc,
131                             SmallVectorImpl<MCInst> &Instructions);
132   void expandLoadAddressReg(MCInst &Inst, SMLoc IDLoc,
133                             SmallVectorImpl<MCInst> &Instructions);
134   void expandMemInst(MCInst &Inst, SMLoc IDLoc,
135                      SmallVectorImpl<MCInst> &Instructions, bool isLoad,
136                      bool isImmOpnd);
137   bool reportParseError(StringRef ErrorMsg);
138
139   bool parseMemOffset(const MCExpr *&Res, bool isParenExpr);
140   bool parseRelocOperand(const MCExpr *&Res);
141
142   const MCExpr *evaluateRelocExpr(const MCExpr *Expr, StringRef RelocStr);
143
144   bool isEvaluated(const MCExpr *Expr);
145   bool parseSetFeature(uint64_t Feature);
146   bool parseDirectiveCPSetup();
147   bool parseDirectiveSet();
148   bool parseDirectiveOption();
149
150   bool parseSetAtDirective();
151   bool parseSetNoAtDirective();
152   bool parseSetMacroDirective();
153   bool parseSetNoMacroDirective();
154   bool parseSetReorderDirective();
155   bool parseSetNoReorderDirective();
156   bool parseSetNoMips16Directive();
157
158   bool parseSetAssignment();
159
160   bool parseDataDirective(unsigned Size, SMLoc L);
161   bool parseDirectiveGpWord();
162   bool parseDirectiveGpDWord();
163
164   MCSymbolRefExpr::VariantKind getVariantKind(StringRef Symbol);
165
166   bool isGP64() const {
167     return (STI.getFeatureBits() & Mips::FeatureGP64Bit) != 0;
168   }
169
170   bool isFP64() const {
171     return (STI.getFeatureBits() & Mips::FeatureFP64Bit) != 0;
172   }
173
174   bool isN32() const { return STI.getFeatureBits() & Mips::FeatureN32; }
175   bool isN64() const { return STI.getFeatureBits() & Mips::FeatureN64; }
176
177   bool isMicroMips() const {
178     return STI.getFeatureBits() & Mips::FeatureMicroMips;
179   }
180
181   bool parseRegister(unsigned &RegNum);
182
183   bool eatComma(StringRef ErrorStr);
184
185   int matchCPURegisterName(StringRef Symbol);
186
187   int matchRegisterByNumber(unsigned RegNum, unsigned RegClass);
188
189   int matchFPURegisterName(StringRef Name);
190
191   int matchFCCRegisterName(StringRef Name);
192
193   int matchACRegisterName(StringRef Name);
194
195   int matchMSA128RegisterName(StringRef Name);
196
197   int matchMSA128CtrlRegisterName(StringRef Name);
198
199   unsigned getReg(int RC, int RegNo);
200
201   unsigned getGPR(int RegNo);
202
203   int getATReg();
204
205   bool processInstruction(MCInst &Inst, SMLoc IDLoc,
206                           SmallVectorImpl<MCInst> &Instructions);
207
208   // Helper function that checks if the value of a vector index is within the
209   // boundaries of accepted values for each RegisterKind
210   // Example: INSERT.B $w0[n], $1 => 16 > n >= 0
211   bool validateMSAIndex(int Val, int RegKind);
212
213   void setFeatureBits(unsigned Feature, StringRef FeatureString) {
214     if (!(STI.getFeatureBits() & Feature)) {
215       setAvailableFeatures(ComputeAvailableFeatures(
216                            STI.ToggleFeature(FeatureString)));
217     }
218   }
219
220   void clearFeatureBits(unsigned Feature, StringRef FeatureString) {
221     if (STI.getFeatureBits() & Feature) {
222      setAvailableFeatures(ComputeAvailableFeatures(
223                            STI.ToggleFeature(FeatureString)));
224     }
225   }
226
227 public:
228   MipsAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
229                 const MCInstrInfo &MII)
230       : MCTargetAsmParser(), STI(sti), Parser(parser) {
231     // Initialize the set of available features.
232     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
233
234     // Assert exactly one ABI was chosen.
235     assert((((STI.getFeatureBits() & Mips::FeatureO32) != 0) +
236             ((STI.getFeatureBits() & Mips::FeatureEABI) != 0) +
237             ((STI.getFeatureBits() & Mips::FeatureN32) != 0) +
238             ((STI.getFeatureBits() & Mips::FeatureN64) != 0)) == 1);
239   }
240
241   MCAsmParser &getParser() const { return Parser; }
242   MCAsmLexer &getLexer() const { return Parser.getLexer(); }
243
244   /// Warn if RegNo is the current assembler temporary.
245   void WarnIfAssemblerTemporary(int RegNo, SMLoc Loc);
246 };
247 }
248
249 namespace {
250
251 /// MipsOperand - Instances of this class represent a parsed Mips machine
252 /// instruction.
253 class MipsOperand : public MCParsedAsmOperand {
254 public:
255   /// Broad categories of register classes
256   /// The exact class is finalized by the render method.
257   enum RegKind {
258     RegKind_GPR = 1,      /// GPR32 and GPR64 (depending on isGP64())
259     RegKind_FGR = 2,      /// FGR32, FGR64, AFGR64 (depending on context and
260                           /// isFP64())
261     RegKind_FCC = 4,      /// FCC
262     RegKind_MSA128 = 8,   /// MSA128[BHWD] (makes no difference which)
263     RegKind_MSACtrl = 16, /// MSA control registers
264     RegKind_COP2 = 32,    /// COP2
265     RegKind_ACC = 64,     /// HI32DSP, LO32DSP, and ACC64DSP (depending on
266                           /// context).
267     RegKind_CCR = 128,    /// CCR
268     RegKind_HWRegs = 256, /// HWRegs
269
270     /// Potentially any (e.g. $1)
271     RegKind_Numeric = RegKind_GPR | RegKind_FGR | RegKind_FCC | RegKind_MSA128 |
272                       RegKind_MSACtrl | RegKind_COP2 | RegKind_ACC |
273                       RegKind_CCR | RegKind_HWRegs
274   };
275
276 private:
277   enum KindTy {
278     k_Immediate,     /// An immediate (possibly involving symbol references)
279     k_Memory,        /// Base + Offset Memory Address
280     k_PhysRegister,  /// A physical register from the Mips namespace
281     k_RegisterIndex, /// A register index in one or more RegKind.
282     k_Token          /// A simple token
283   } Kind;
284
285   MipsOperand(KindTy K, MipsAsmParser &Parser)
286       : MCParsedAsmOperand(), Kind(K), AsmParser(Parser) {}
287
288   /// For diagnostics, and checking the assembler temporary
289   MipsAsmParser &AsmParser;
290
291   struct Token {
292     const char *Data;
293     unsigned Length;
294   };
295
296   struct PhysRegOp {
297     unsigned Num; /// Register Number
298   };
299
300   struct RegIdxOp {
301     unsigned Index; /// Index into the register class
302     RegKind Kind;   /// Bitfield of the kinds it could possibly be
303     const MCRegisterInfo *RegInfo;
304   };
305
306   struct ImmOp {
307     const MCExpr *Val;
308   };
309
310   struct MemOp {
311     MipsOperand *Base;
312     const MCExpr *Off;
313   };
314
315   union {
316     struct Token Tok;
317     struct PhysRegOp PhysReg;
318     struct RegIdxOp RegIdx;
319     struct ImmOp Imm;
320     struct MemOp Mem;
321   };
322
323   SMLoc StartLoc, EndLoc;
324
325   /// Internal constructor for register kinds
326   static MipsOperand *CreateReg(unsigned Index, RegKind RegKind,
327                                 const MCRegisterInfo *RegInfo, SMLoc S, SMLoc E,
328                                 MipsAsmParser &Parser) {
329     MipsOperand *Op = new MipsOperand(k_RegisterIndex, Parser);
330     Op->RegIdx.Index = Index;
331     Op->RegIdx.RegInfo = RegInfo;
332     Op->RegIdx.Kind = RegKind;
333     Op->StartLoc = S;
334     Op->EndLoc = E;
335     return Op;
336   }
337
338 public:
339   /// Coerce the register to GPR32 and return the real register for the current
340   /// target.
341   unsigned getGPR32Reg() const {
342     assert(isRegIdx() && (RegIdx.Kind & RegKind_GPR) && "Invalid access!");
343     AsmParser.WarnIfAssemblerTemporary(RegIdx.Index, StartLoc);
344     unsigned ClassID = Mips::GPR32RegClassID;
345     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
346   }
347
348   /// Coerce the register to GPR64 and return the real register for the current
349   /// target.
350   unsigned getGPR64Reg() const {
351     assert(isRegIdx() && (RegIdx.Kind & RegKind_GPR) && "Invalid access!");
352     unsigned ClassID = Mips::GPR64RegClassID;
353     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
354   }
355
356 private:
357   /// Coerce the register to AFGR64 and return the real register for the current
358   /// target.
359   unsigned getAFGR64Reg() const {
360     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
361     if (RegIdx.Index % 2 != 0)
362       AsmParser.Warning(StartLoc, "Float register should be even.");
363     return RegIdx.RegInfo->getRegClass(Mips::AFGR64RegClassID)
364         .getRegister(RegIdx.Index / 2);
365   }
366
367   /// Coerce the register to FGR64 and return the real register for the current
368   /// target.
369   unsigned getFGR64Reg() const {
370     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
371     return RegIdx.RegInfo->getRegClass(Mips::FGR64RegClassID)
372         .getRegister(RegIdx.Index);
373   }
374
375   /// Coerce the register to FGR32 and return the real register for the current
376   /// target.
377   unsigned getFGR32Reg() const {
378     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
379     return RegIdx.RegInfo->getRegClass(Mips::FGR32RegClassID)
380         .getRegister(RegIdx.Index);
381   }
382
383   /// Coerce the register to FGRH32 and return the real register for the current
384   /// target.
385   unsigned getFGRH32Reg() const {
386     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
387     return RegIdx.RegInfo->getRegClass(Mips::FGRH32RegClassID)
388         .getRegister(RegIdx.Index);
389   }
390
391   /// Coerce the register to FCC and return the real register for the current
392   /// target.
393   unsigned getFCCReg() const {
394     assert(isRegIdx() && (RegIdx.Kind & RegKind_FCC) && "Invalid access!");
395     return RegIdx.RegInfo->getRegClass(Mips::FCCRegClassID)
396         .getRegister(RegIdx.Index);
397   }
398
399   /// Coerce the register to MSA128 and return the real register for the current
400   /// target.
401   unsigned getMSA128Reg() const {
402     assert(isRegIdx() && (RegIdx.Kind & RegKind_MSA128) && "Invalid access!");
403     // It doesn't matter which of the MSA128[BHWD] classes we use. They are all
404     // identical
405     unsigned ClassID = Mips::MSA128BRegClassID;
406     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
407   }
408
409   /// Coerce the register to MSACtrl and return the real register for the
410   /// current target.
411   unsigned getMSACtrlReg() const {
412     assert(isRegIdx() && (RegIdx.Kind & RegKind_MSACtrl) && "Invalid access!");
413     unsigned ClassID = Mips::MSACtrlRegClassID;
414     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
415   }
416
417   /// Coerce the register to COP2 and return the real register for the
418   /// current target.
419   unsigned getCOP2Reg() const {
420     assert(isRegIdx() && (RegIdx.Kind & RegKind_COP2) && "Invalid access!");
421     unsigned ClassID = Mips::COP2RegClassID;
422     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
423   }
424
425   /// Coerce the register to ACC64DSP and return the real register for the
426   /// current target.
427   unsigned getACC64DSPReg() const {
428     assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!");
429     unsigned ClassID = Mips::ACC64DSPRegClassID;
430     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
431   }
432
433   /// Coerce the register to HI32DSP and return the real register for the
434   /// current target.
435   unsigned getHI32DSPReg() const {
436     assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!");
437     unsigned ClassID = Mips::HI32DSPRegClassID;
438     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
439   }
440
441   /// Coerce the register to LO32DSP and return the real register for the
442   /// current target.
443   unsigned getLO32DSPReg() const {
444     assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!");
445     unsigned ClassID = Mips::LO32DSPRegClassID;
446     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
447   }
448
449   /// Coerce the register to CCR and return the real register for the
450   /// current target.
451   unsigned getCCRReg() const {
452     assert(isRegIdx() && (RegIdx.Kind & RegKind_CCR) && "Invalid access!");
453     unsigned ClassID = Mips::CCRRegClassID;
454     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
455   }
456
457   /// Coerce the register to HWRegs and return the real register for the
458   /// current target.
459   unsigned getHWRegsReg() const {
460     assert(isRegIdx() && (RegIdx.Kind & RegKind_HWRegs) && "Invalid access!");
461     unsigned ClassID = Mips::HWRegsRegClassID;
462     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
463   }
464
465 public:
466   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
467     // Add as immediate when possible.  Null MCExpr = 0.
468     if (Expr == 0)
469       Inst.addOperand(MCOperand::CreateImm(0));
470     else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
471       Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
472     else
473       Inst.addOperand(MCOperand::CreateExpr(Expr));
474   }
475
476   void addRegOperands(MCInst &Inst, unsigned N) const {
477     llvm_unreachable("Use a custom parser instead");
478   }
479
480   /// Render the operand to an MCInst as a GPR32
481   /// Asserts if the wrong number of operands are requested, or the operand
482   /// is not a k_RegisterIndex compatible with RegKind_GPR
483   void addGPR32AsmRegOperands(MCInst &Inst, unsigned N) const {
484     assert(N == 1 && "Invalid number of operands!");
485     Inst.addOperand(MCOperand::CreateReg(getGPR32Reg()));
486   }
487
488   /// Render the operand to an MCInst as a GPR64
489   /// Asserts if the wrong number of operands are requested, or the operand
490   /// is not a k_RegisterIndex compatible with RegKind_GPR
491   void addGPR64AsmRegOperands(MCInst &Inst, unsigned N) const {
492     assert(N == 1 && "Invalid number of operands!");
493     Inst.addOperand(MCOperand::CreateReg(getGPR64Reg()));
494   }
495
496   void addAFGR64AsmRegOperands(MCInst &Inst, unsigned N) const {
497     assert(N == 1 && "Invalid number of operands!");
498     Inst.addOperand(MCOperand::CreateReg(getAFGR64Reg()));
499   }
500
501   void addFGR64AsmRegOperands(MCInst &Inst, unsigned N) const {
502     assert(N == 1 && "Invalid number of operands!");
503     Inst.addOperand(MCOperand::CreateReg(getFGR64Reg()));
504   }
505
506   void addFGR32AsmRegOperands(MCInst &Inst, unsigned N) const {
507     assert(N == 1 && "Invalid number of operands!");
508     Inst.addOperand(MCOperand::CreateReg(getFGR32Reg()));
509   }
510
511   void addFGRH32AsmRegOperands(MCInst &Inst, unsigned N) const {
512     assert(N == 1 && "Invalid number of operands!");
513     Inst.addOperand(MCOperand::CreateReg(getFGRH32Reg()));
514   }
515
516   void addFCCAsmRegOperands(MCInst &Inst, unsigned N) const {
517     assert(N == 1 && "Invalid number of operands!");
518     Inst.addOperand(MCOperand::CreateReg(getFCCReg()));
519   }
520
521   void addMSA128AsmRegOperands(MCInst &Inst, unsigned N) const {
522     assert(N == 1 && "Invalid number of operands!");
523     Inst.addOperand(MCOperand::CreateReg(getMSA128Reg()));
524   }
525
526   void addMSACtrlAsmRegOperands(MCInst &Inst, unsigned N) const {
527     assert(N == 1 && "Invalid number of operands!");
528     Inst.addOperand(MCOperand::CreateReg(getMSACtrlReg()));
529   }
530
531   void addCOP2AsmRegOperands(MCInst &Inst, unsigned N) const {
532     assert(N == 1 && "Invalid number of operands!");
533     Inst.addOperand(MCOperand::CreateReg(getCOP2Reg()));
534   }
535
536   void addACC64DSPAsmRegOperands(MCInst &Inst, unsigned N) const {
537     assert(N == 1 && "Invalid number of operands!");
538     Inst.addOperand(MCOperand::CreateReg(getACC64DSPReg()));
539   }
540
541   void addHI32DSPAsmRegOperands(MCInst &Inst, unsigned N) const {
542     assert(N == 1 && "Invalid number of operands!");
543     Inst.addOperand(MCOperand::CreateReg(getHI32DSPReg()));
544   }
545
546   void addLO32DSPAsmRegOperands(MCInst &Inst, unsigned N) const {
547     assert(N == 1 && "Invalid number of operands!");
548     Inst.addOperand(MCOperand::CreateReg(getLO32DSPReg()));
549   }
550
551   void addCCRAsmRegOperands(MCInst &Inst, unsigned N) const {
552     assert(N == 1 && "Invalid number of operands!");
553     Inst.addOperand(MCOperand::CreateReg(getCCRReg()));
554   }
555
556   void addHWRegsAsmRegOperands(MCInst &Inst, unsigned N) const {
557     assert(N == 1 && "Invalid number of operands!");
558     Inst.addOperand(MCOperand::CreateReg(getHWRegsReg()));
559   }
560
561   void addImmOperands(MCInst &Inst, unsigned N) const {
562     assert(N == 1 && "Invalid number of operands!");
563     const MCExpr *Expr = getImm();
564     addExpr(Inst, Expr);
565   }
566
567   void addMemOperands(MCInst &Inst, unsigned N) const {
568     assert(N == 2 && "Invalid number of operands!");
569
570     Inst.addOperand(MCOperand::CreateReg(getMemBase()->getGPR32Reg()));
571
572     const MCExpr *Expr = getMemOff();
573     addExpr(Inst, Expr);
574   }
575
576   bool isReg() const {
577     // As a special case until we sort out the definition of div/divu, pretend
578     // that $0/$zero are k_PhysRegister so that MCK_ZERO works correctly.
579     if (isGPRAsmReg() && RegIdx.Index == 0)
580       return true;
581
582     return Kind == k_PhysRegister;
583   }
584   bool isRegIdx() const { return Kind == k_RegisterIndex; }
585   bool isImm() const { return Kind == k_Immediate; }
586   bool isConstantImm() const {
587     return isImm() && dyn_cast<MCConstantExpr>(getImm());
588   }
589   bool isToken() const {
590     // Note: It's not possible to pretend that other operand kinds are tokens.
591     // The matcher emitter checks tokens first.
592     return Kind == k_Token;
593   }
594   bool isMem() const { return Kind == k_Memory; }
595   bool isInvNum() const { return Kind == k_Immediate; }
596   bool isLSAImm() const {
597     if (!isConstantImm())
598       return false;
599     int64_t Val = getConstantImm();
600     return 1 <= Val && Val <= 4;
601   }
602
603   StringRef getToken() const {
604     assert(Kind == k_Token && "Invalid access!");
605     return StringRef(Tok.Data, Tok.Length);
606   }
607
608   unsigned getReg() const {
609     // As a special case until we sort out the definition of div/divu, pretend
610     // that $0/$zero are k_PhysRegister so that MCK_ZERO works correctly.
611     if (Kind == k_RegisterIndex && RegIdx.Index == 0 &&
612         RegIdx.Kind & RegKind_GPR)
613       return getGPR32Reg(); // FIXME: GPR64 too
614
615     assert(Kind == k_PhysRegister && "Invalid access!");
616     return PhysReg.Num;
617   }
618
619   const MCExpr *getImm() const {
620     assert((Kind == k_Immediate) && "Invalid access!");
621     return Imm.Val;
622   }
623
624   int64_t getConstantImm() const {
625     const MCExpr *Val = getImm();
626     return static_cast<const MCConstantExpr *>(Val)->getValue();
627   }
628
629   MipsOperand *getMemBase() const {
630     assert((Kind == k_Memory) && "Invalid access!");
631     return Mem.Base;
632   }
633
634   const MCExpr *getMemOff() const {
635     assert((Kind == k_Memory) && "Invalid access!");
636     return Mem.Off;
637   }
638
639   static MipsOperand *CreateToken(StringRef Str, SMLoc S,
640                                   MipsAsmParser &Parser) {
641     MipsOperand *Op = new MipsOperand(k_Token, Parser);
642     Op->Tok.Data = Str.data();
643     Op->Tok.Length = Str.size();
644     Op->StartLoc = S;
645     Op->EndLoc = S;
646     return Op;
647   }
648
649   /// Create a numeric register (e.g. $1). The exact register remains
650   /// unresolved until an instruction successfully matches
651   static MipsOperand *CreateNumericReg(unsigned Index,
652                                        const MCRegisterInfo *RegInfo, SMLoc S,
653                                        SMLoc E, MipsAsmParser &Parser) {
654     DEBUG(dbgs() << "CreateNumericReg(" << Index << ", ...)\n");
655     return CreateReg(Index, RegKind_Numeric, RegInfo, S, E, Parser);
656   }
657
658   /// Create a register that is definitely a GPR.
659   /// This is typically only used for named registers such as $gp.
660   static MipsOperand *CreateGPRReg(unsigned Index,
661                                    const MCRegisterInfo *RegInfo, SMLoc S,
662                                    SMLoc E, MipsAsmParser &Parser) {
663     return CreateReg(Index, RegKind_GPR, RegInfo, S, E, Parser);
664   }
665
666   /// Create a register that is definitely a FGR.
667   /// This is typically only used for named registers such as $f0.
668   static MipsOperand *CreateFGRReg(unsigned Index,
669                                    const MCRegisterInfo *RegInfo, SMLoc S,
670                                    SMLoc E, MipsAsmParser &Parser) {
671     return CreateReg(Index, RegKind_FGR, RegInfo, S, E, Parser);
672   }
673
674   /// Create a register that is definitely an FCC.
675   /// This is typically only used for named registers such as $fcc0.
676   static MipsOperand *CreateFCCReg(unsigned Index,
677                                    const MCRegisterInfo *RegInfo, SMLoc S,
678                                    SMLoc E, MipsAsmParser &Parser) {
679     return CreateReg(Index, RegKind_FCC, RegInfo, S, E, Parser);
680   }
681
682   /// Create a register that is definitely an ACC.
683   /// This is typically only used for named registers such as $ac0.
684   static MipsOperand *CreateACCReg(unsigned Index,
685                                    const MCRegisterInfo *RegInfo, SMLoc S,
686                                    SMLoc E, MipsAsmParser &Parser) {
687     return CreateReg(Index, RegKind_ACC, RegInfo, S, E, Parser);
688   }
689
690   /// Create a register that is definitely an MSA128.
691   /// This is typically only used for named registers such as $w0.
692   static MipsOperand *CreateMSA128Reg(unsigned Index,
693                                       const MCRegisterInfo *RegInfo, SMLoc S,
694                                       SMLoc E, MipsAsmParser &Parser) {
695     return CreateReg(Index, RegKind_MSA128, RegInfo, S, E, Parser);
696   }
697
698   /// Create a register that is definitely an MSACtrl.
699   /// This is typically only used for named registers such as $msaaccess.
700   static MipsOperand *CreateMSACtrlReg(unsigned Index,
701                                        const MCRegisterInfo *RegInfo, SMLoc S,
702                                        SMLoc E, MipsAsmParser &Parser) {
703     return CreateReg(Index, RegKind_MSACtrl, RegInfo, S, E, Parser);
704   }
705
706   static MipsOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E,
707                                 MipsAsmParser &Parser) {
708     MipsOperand *Op = new MipsOperand(k_Immediate, Parser);
709     Op->Imm.Val = Val;
710     Op->StartLoc = S;
711     Op->EndLoc = E;
712     return Op;
713   }
714
715   static MipsOperand *CreateMem(MipsOperand *Base, const MCExpr *Off, SMLoc S,
716                                 SMLoc E, MipsAsmParser &Parser) {
717     MipsOperand *Op = new MipsOperand(k_Memory, Parser);
718     Op->Mem.Base = Base;
719     Op->Mem.Off = Off;
720     Op->StartLoc = S;
721     Op->EndLoc = E;
722     return Op;
723   }
724
725   bool isGPRAsmReg() const {
726     return isRegIdx() && RegIdx.Kind & RegKind_GPR && RegIdx.Index <= 31;
727   }
728   bool isFGRAsmReg() const {
729     // AFGR64 is $0-$15 but we handle this in getAFGR64()
730     return isRegIdx() && RegIdx.Kind & RegKind_FGR && RegIdx.Index <= 31;
731   }
732   bool isHWRegsAsmReg() const {
733     return isRegIdx() && RegIdx.Kind & RegKind_HWRegs && RegIdx.Index <= 31;
734   }
735   bool isCCRAsmReg() const {
736     return isRegIdx() && RegIdx.Kind & RegKind_CCR && RegIdx.Index <= 31;
737   }
738   bool isFCCAsmReg() const {
739     return isRegIdx() && RegIdx.Kind & RegKind_FCC && RegIdx.Index <= 7;
740   }
741   bool isACCAsmReg() const {
742     return isRegIdx() && RegIdx.Kind & RegKind_ACC && RegIdx.Index <= 3;
743   }
744   bool isCOP2AsmReg() const {
745     return isRegIdx() && RegIdx.Kind & RegKind_COP2 && RegIdx.Index <= 31;
746   }
747   bool isMSA128AsmReg() const {
748     return isRegIdx() && RegIdx.Kind & RegKind_MSA128 && RegIdx.Index <= 31;
749   }
750   bool isMSACtrlAsmReg() const {
751     return isRegIdx() && RegIdx.Kind & RegKind_MSACtrl && RegIdx.Index <= 7;
752   }
753
754   /// getStartLoc - Get the location of the first token of this operand.
755   SMLoc getStartLoc() const { return StartLoc; }
756   /// getEndLoc - Get the location of the last token of this operand.
757   SMLoc getEndLoc() const { return EndLoc; }
758
759   virtual void print(raw_ostream &OS) const {
760     switch (Kind) {
761     case k_Immediate:
762       OS << "Imm<";
763       Imm.Val->print(OS);
764       OS << ">";
765       break;
766     case k_Memory:
767       OS << "Mem<";
768       Mem.Base->print(OS);
769       OS << ", ";
770       Mem.Off->print(OS);
771       OS << ">";
772       break;
773     case k_PhysRegister:
774       OS << "PhysReg<" << PhysReg.Num << ">";
775       break;
776     case k_RegisterIndex:
777       OS << "RegIdx<" << RegIdx.Index << ":" << RegIdx.Kind << ">";
778       break;
779     case k_Token:
780       OS << Tok.Data;
781       break;
782     }
783   }
784 }; // class MipsOperand
785 } // namespace
786
787 namespace llvm {
788 extern const MCInstrDesc MipsInsts[];
789 }
790 static const MCInstrDesc &getInstDesc(unsigned Opcode) {
791   return MipsInsts[Opcode];
792 }
793
794 bool MipsAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
795                                        SmallVectorImpl<MCInst> &Instructions) {
796   const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
797
798   Inst.setLoc(IDLoc);
799
800   if (MCID.isBranch() || MCID.isCall()) {
801     const unsigned Opcode = Inst.getOpcode();
802     MCOperand Offset;
803
804     switch (Opcode) {
805     default:
806       break;
807     case Mips::BEQ:
808     case Mips::BNE:
809     case Mips::BEQ_MM:
810     case Mips::BNE_MM:
811       assert(MCID.getNumOperands() == 3 && "unexpected number of operands");
812       Offset = Inst.getOperand(2);
813       if (!Offset.isImm())
814         break; // We'll deal with this situation later on when applying fixups.
815       if (!isIntN(isMicroMips() ? 17 : 18, Offset.getImm()))
816         return Error(IDLoc, "branch target out of range");
817       if (OffsetToAlignment(Offset.getImm(), 1LL << (isMicroMips() ? 1 : 2)))
818         return Error(IDLoc, "branch to misaligned address");
819       break;
820     case Mips::BGEZ:
821     case Mips::BGTZ:
822     case Mips::BLEZ:
823     case Mips::BLTZ:
824     case Mips::BGEZAL:
825     case Mips::BLTZAL:
826     case Mips::BC1F:
827     case Mips::BC1T:
828     case Mips::BGEZ_MM:
829     case Mips::BGTZ_MM:
830     case Mips::BLEZ_MM:
831     case Mips::BLTZ_MM:
832     case Mips::BGEZAL_MM:
833     case Mips::BLTZAL_MM:
834     case Mips::BC1F_MM:
835     case Mips::BC1T_MM:
836       assert(MCID.getNumOperands() == 2 && "unexpected number of operands");
837       Offset = Inst.getOperand(1);
838       if (!Offset.isImm())
839         break; // We'll deal with this situation later on when applying fixups.
840       if (!isIntN(isMicroMips() ? 17 : 18, Offset.getImm()))
841         return Error(IDLoc, "branch target out of range");
842       if (OffsetToAlignment(Offset.getImm(), 1LL << (isMicroMips() ? 1 : 2)))
843         return Error(IDLoc, "branch to misaligned address");
844       break;
845     }
846   }
847
848   if (MCID.hasDelaySlot() && Options.isReorder()) {
849     // If this instruction has a delay slot and .set reorder is active,
850     // emit a NOP after it.
851     Instructions.push_back(Inst);
852     MCInst NopInst;
853     NopInst.setOpcode(Mips::SLL);
854     NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
855     NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
856     NopInst.addOperand(MCOperand::CreateImm(0));
857     Instructions.push_back(NopInst);
858     return false;
859   }
860
861   if (MCID.mayLoad() || MCID.mayStore()) {
862     // Check the offset of memory operand, if it is a symbol
863     // reference or immediate we may have to expand instructions.
864     for (unsigned i = 0; i < MCID.getNumOperands(); i++) {
865       const MCOperandInfo &OpInfo = MCID.OpInfo[i];
866       if ((OpInfo.OperandType == MCOI::OPERAND_MEMORY) ||
867           (OpInfo.OperandType == MCOI::OPERAND_UNKNOWN)) {
868         MCOperand &Op = Inst.getOperand(i);
869         if (Op.isImm()) {
870           int MemOffset = Op.getImm();
871           if (MemOffset < -32768 || MemOffset > 32767) {
872             // Offset can't exceed 16bit value.
873             expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), true);
874             return false;
875           }
876         } else if (Op.isExpr()) {
877           const MCExpr *Expr = Op.getExpr();
878           if (Expr->getKind() == MCExpr::SymbolRef) {
879             const MCSymbolRefExpr *SR =
880                 static_cast<const MCSymbolRefExpr *>(Expr);
881             if (SR->getKind() == MCSymbolRefExpr::VK_None) {
882               // Expand symbol.
883               expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), false);
884               return false;
885             }
886           } else if (!isEvaluated(Expr)) {
887             expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), false);
888             return false;
889           }
890         }
891       }
892     } // for
893   }   // if load/store
894
895   if (needsExpansion(Inst))
896     expandInstruction(Inst, IDLoc, Instructions);
897   else
898     Instructions.push_back(Inst);
899
900   return false;
901 }
902
903 bool MipsAsmParser::needsExpansion(MCInst &Inst) {
904
905   switch (Inst.getOpcode()) {
906   case Mips::LoadImm32Reg:
907   case Mips::LoadAddr32Imm:
908   case Mips::LoadAddr32Reg:
909   case Mips::SUBi:
910   case Mips::SUBiu:
911   case Mips::DSUBi:
912   case Mips::DSUBiu:
913     return true;
914   default:
915     return false;
916   }
917 }
918
919 void MipsAsmParser::expandInstruction(MCInst &Inst, SMLoc IDLoc,
920                                       SmallVectorImpl<MCInst> &Instructions) {
921   switch (Inst.getOpcode()) {
922   case Mips::LoadImm32Reg:
923     return expandLoadImm(Inst, IDLoc, Instructions);
924   case Mips::LoadAddr32Imm:
925     return expandLoadAddressImm(Inst, IDLoc, Instructions);
926   case Mips::LoadAddr32Reg:
927     return expandLoadAddressReg(Inst, IDLoc, Instructions);
928   case Mips::SUBi:
929     Instructions.push_back(MCInstBuilder(Mips::ADDi)
930                                .addReg(Inst.getOperand(0).getReg())
931                                .addReg(Inst.getOperand(1).getReg())
932                                .addImm(-Inst.getOperand(2).getImm()));
933     return;
934   case Mips::SUBiu:
935     Instructions.push_back(MCInstBuilder(Mips::ADDiu)
936                                .addReg(Inst.getOperand(0).getReg())
937                                .addReg(Inst.getOperand(1).getReg())
938                                .addImm(-Inst.getOperand(2).getImm()));
939     return;
940   case Mips::DSUBi:
941     Instructions.push_back(MCInstBuilder(Mips::DADDi)
942                                .addReg(Inst.getOperand(0).getReg())
943                                .addReg(Inst.getOperand(1).getReg())
944                                .addImm(-Inst.getOperand(2).getImm()));
945     return;
946   case Mips::DSUBiu:
947     Instructions.push_back(MCInstBuilder(Mips::DADDiu)
948                                .addReg(Inst.getOperand(0).getReg())
949                                .addReg(Inst.getOperand(1).getReg())
950                                .addImm(-Inst.getOperand(2).getImm()));
951     return;
952   }
953 }
954
955 void MipsAsmParser::expandLoadImm(MCInst &Inst, SMLoc IDLoc,
956                                   SmallVectorImpl<MCInst> &Instructions) {
957   MCInst tmpInst;
958   const MCOperand &ImmOp = Inst.getOperand(1);
959   assert(ImmOp.isImm() && "expected immediate operand kind");
960   const MCOperand &RegOp = Inst.getOperand(0);
961   assert(RegOp.isReg() && "expected register operand kind");
962
963   int ImmValue = ImmOp.getImm();
964   tmpInst.setLoc(IDLoc);
965   if (0 <= ImmValue && ImmValue <= 65535) {
966     // For 0 <= j <= 65535.
967     // li d,j => ori d,$zero,j
968     tmpInst.setOpcode(Mips::ORi);
969     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
970     tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
971     tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
972     Instructions.push_back(tmpInst);
973   } else if (ImmValue < 0 && ImmValue >= -32768) {
974     // For -32768 <= j < 0.
975     // li d,j => addiu d,$zero,j
976     tmpInst.setOpcode(Mips::ADDiu);
977     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
978     tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
979     tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
980     Instructions.push_back(tmpInst);
981   } else {
982     // For any other value of j that is representable as a 32-bit integer.
983     // li d,j => lui d,hi16(j)
984     //           ori d,d,lo16(j)
985     tmpInst.setOpcode(Mips::LUi);
986     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
987     tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
988     Instructions.push_back(tmpInst);
989     tmpInst.clear();
990     tmpInst.setOpcode(Mips::ORi);
991     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
992     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
993     tmpInst.addOperand(MCOperand::CreateImm(ImmValue & 0xffff));
994     tmpInst.setLoc(IDLoc);
995     Instructions.push_back(tmpInst);
996   }
997 }
998
999 void
1000 MipsAsmParser::expandLoadAddressReg(MCInst &Inst, SMLoc IDLoc,
1001                                     SmallVectorImpl<MCInst> &Instructions) {
1002   MCInst tmpInst;
1003   const MCOperand &ImmOp = Inst.getOperand(2);
1004   assert(ImmOp.isImm() && "expected immediate operand kind");
1005   const MCOperand &SrcRegOp = Inst.getOperand(1);
1006   assert(SrcRegOp.isReg() && "expected register operand kind");
1007   const MCOperand &DstRegOp = Inst.getOperand(0);
1008   assert(DstRegOp.isReg() && "expected register operand kind");
1009   int ImmValue = ImmOp.getImm();
1010   if (-32768 <= ImmValue && ImmValue <= 65535) {
1011     // For -32768 <= j <= 65535.
1012     // la d,j(s) => addiu d,s,j
1013     tmpInst.setOpcode(Mips::ADDiu);
1014     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1015     tmpInst.addOperand(MCOperand::CreateReg(SrcRegOp.getReg()));
1016     tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
1017     Instructions.push_back(tmpInst);
1018   } else {
1019     // For any other value of j that is representable as a 32-bit integer.
1020     // la d,j(s) => lui d,hi16(j)
1021     //              ori d,d,lo16(j)
1022     //              addu d,d,s
1023     tmpInst.setOpcode(Mips::LUi);
1024     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1025     tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
1026     Instructions.push_back(tmpInst);
1027     tmpInst.clear();
1028     tmpInst.setOpcode(Mips::ORi);
1029     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1030     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1031     tmpInst.addOperand(MCOperand::CreateImm(ImmValue & 0xffff));
1032     Instructions.push_back(tmpInst);
1033     tmpInst.clear();
1034     tmpInst.setOpcode(Mips::ADDu);
1035     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1036     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1037     tmpInst.addOperand(MCOperand::CreateReg(SrcRegOp.getReg()));
1038     Instructions.push_back(tmpInst);
1039   }
1040 }
1041
1042 void
1043 MipsAsmParser::expandLoadAddressImm(MCInst &Inst, SMLoc IDLoc,
1044                                     SmallVectorImpl<MCInst> &Instructions) {
1045   MCInst tmpInst;
1046   const MCOperand &ImmOp = Inst.getOperand(1);
1047   assert(ImmOp.isImm() && "expected immediate operand kind");
1048   const MCOperand &RegOp = Inst.getOperand(0);
1049   assert(RegOp.isReg() && "expected register operand kind");
1050   int ImmValue = ImmOp.getImm();
1051   if (-32768 <= ImmValue && ImmValue <= 65535) {
1052     // For -32768 <= j <= 65535.
1053     // la d,j => addiu d,$zero,j
1054     tmpInst.setOpcode(Mips::ADDiu);
1055     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1056     tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1057     tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
1058     Instructions.push_back(tmpInst);
1059   } else {
1060     // For any other value of j that is representable as a 32-bit integer.
1061     // la d,j => lui d,hi16(j)
1062     //           ori d,d,lo16(j)
1063     tmpInst.setOpcode(Mips::LUi);
1064     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1065     tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
1066     Instructions.push_back(tmpInst);
1067     tmpInst.clear();
1068     tmpInst.setOpcode(Mips::ORi);
1069     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1070     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1071     tmpInst.addOperand(MCOperand::CreateImm(ImmValue & 0xffff));
1072     Instructions.push_back(tmpInst);
1073   }
1074 }
1075
1076 void MipsAsmParser::expandMemInst(MCInst &Inst, SMLoc IDLoc,
1077                                   SmallVectorImpl<MCInst> &Instructions,
1078                                   bool isLoad, bool isImmOpnd) {
1079   const MCSymbolRefExpr *SR;
1080   MCInst TempInst;
1081   unsigned ImmOffset, HiOffset, LoOffset;
1082   const MCExpr *ExprOffset;
1083   unsigned TmpRegNum;
1084   unsigned AtRegNum = getReg(
1085       (isGP64()) ? Mips::GPR64RegClassID : Mips::GPR32RegClassID, getATReg());
1086   // 1st operand is either the source or destination register.
1087   assert(Inst.getOperand(0).isReg() && "expected register operand kind");
1088   unsigned RegOpNum = Inst.getOperand(0).getReg();
1089   // 2nd operand is the base register.
1090   assert(Inst.getOperand(1).isReg() && "expected register operand kind");
1091   unsigned BaseRegNum = Inst.getOperand(1).getReg();
1092   // 3rd operand is either an immediate or expression.
1093   if (isImmOpnd) {
1094     assert(Inst.getOperand(2).isImm() && "expected immediate operand kind");
1095     ImmOffset = Inst.getOperand(2).getImm();
1096     LoOffset = ImmOffset & 0x0000ffff;
1097     HiOffset = (ImmOffset & 0xffff0000) >> 16;
1098     // If msb of LoOffset is 1(negative number) we must increment HiOffset.
1099     if (LoOffset & 0x8000)
1100       HiOffset++;
1101   } else
1102     ExprOffset = Inst.getOperand(2).getExpr();
1103   // All instructions will have the same location.
1104   TempInst.setLoc(IDLoc);
1105   // 1st instruction in expansion is LUi. For load instruction we can use
1106   // the dst register as a temporary if base and dst are different,
1107   // but for stores we must use $at.
1108   TmpRegNum = (isLoad && (BaseRegNum != RegOpNum)) ? RegOpNum : AtRegNum;
1109   TempInst.setOpcode(Mips::LUi);
1110   TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
1111   if (isImmOpnd)
1112     TempInst.addOperand(MCOperand::CreateImm(HiOffset));
1113   else {
1114     if (ExprOffset->getKind() == MCExpr::SymbolRef) {
1115       SR = static_cast<const MCSymbolRefExpr *>(ExprOffset);
1116       const MCSymbolRefExpr *HiExpr = MCSymbolRefExpr::Create(
1117           SR->getSymbol().getName(), MCSymbolRefExpr::VK_Mips_ABS_HI,
1118           getContext());
1119       TempInst.addOperand(MCOperand::CreateExpr(HiExpr));
1120     } else {
1121       const MCExpr *HiExpr = evaluateRelocExpr(ExprOffset, "hi");
1122       TempInst.addOperand(MCOperand::CreateExpr(HiExpr));
1123     }
1124   }
1125   // Add the instruction to the list.
1126   Instructions.push_back(TempInst);
1127   // Prepare TempInst for next instruction.
1128   TempInst.clear();
1129   // Add temp register to base.
1130   TempInst.setOpcode(Mips::ADDu);
1131   TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
1132   TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
1133   TempInst.addOperand(MCOperand::CreateReg(BaseRegNum));
1134   Instructions.push_back(TempInst);
1135   TempInst.clear();
1136   // And finally, create original instruction with low part
1137   // of offset and new base.
1138   TempInst.setOpcode(Inst.getOpcode());
1139   TempInst.addOperand(MCOperand::CreateReg(RegOpNum));
1140   TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
1141   if (isImmOpnd)
1142     TempInst.addOperand(MCOperand::CreateImm(LoOffset));
1143   else {
1144     if (ExprOffset->getKind() == MCExpr::SymbolRef) {
1145       const MCSymbolRefExpr *LoExpr = MCSymbolRefExpr::Create(
1146           SR->getSymbol().getName(), MCSymbolRefExpr::VK_Mips_ABS_LO,
1147           getContext());
1148       TempInst.addOperand(MCOperand::CreateExpr(LoExpr));
1149     } else {
1150       const MCExpr *LoExpr = evaluateRelocExpr(ExprOffset, "lo");
1151       TempInst.addOperand(MCOperand::CreateExpr(LoExpr));
1152     }
1153   }
1154   Instructions.push_back(TempInst);
1155   TempInst.clear();
1156 }
1157
1158 bool MipsAsmParser::MatchAndEmitInstruction(
1159     SMLoc IDLoc, unsigned &Opcode,
1160     SmallVectorImpl<MCParsedAsmOperand *> &Operands, MCStreamer &Out,
1161     unsigned &ErrorInfo, bool MatchingInlineAsm) {
1162   MCInst Inst;
1163   SmallVector<MCInst, 8> Instructions;
1164   unsigned MatchResult =
1165       MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
1166
1167   switch (MatchResult) {
1168   default:
1169     break;
1170   case Match_Success: {
1171     if (processInstruction(Inst, IDLoc, Instructions))
1172       return true;
1173     for (unsigned i = 0; i < Instructions.size(); i++)
1174       Out.EmitInstruction(Instructions[i], STI);
1175     return false;
1176   }
1177   case Match_MissingFeature:
1178     Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1179     return true;
1180   case Match_InvalidOperand: {
1181     SMLoc ErrorLoc = IDLoc;
1182     if (ErrorInfo != ~0U) {
1183       if (ErrorInfo >= Operands.size())
1184         return Error(IDLoc, "too few operands for instruction");
1185
1186       ErrorLoc = ((MipsOperand *)Operands[ErrorInfo])->getStartLoc();
1187       if (ErrorLoc == SMLoc())
1188         ErrorLoc = IDLoc;
1189     }
1190
1191     return Error(ErrorLoc, "invalid operand for instruction");
1192   }
1193   case Match_MnemonicFail:
1194     return Error(IDLoc, "invalid instruction");
1195   }
1196   return true;
1197 }
1198
1199 void MipsAsmParser::WarnIfAssemblerTemporary(int RegIndex, SMLoc Loc) {
1200   if ((RegIndex != 0) && ((int)Options.getATRegNum() == RegIndex)) {
1201     if (RegIndex == 1)
1202       Warning(Loc, "Used $at without \".set noat\"");
1203     else
1204       Warning(Loc, Twine("Used $") + Twine(RegIndex) + " with \".set at=$" +
1205                        Twine(RegIndex) + "\"");
1206   }
1207 }
1208
1209 int MipsAsmParser::matchCPURegisterName(StringRef Name) {
1210   int CC;
1211
1212   CC = StringSwitch<unsigned>(Name)
1213            .Case("zero", 0)
1214            .Case("at", 1)
1215            .Case("a0", 4)
1216            .Case("a1", 5)
1217            .Case("a2", 6)
1218            .Case("a3", 7)
1219            .Case("v0", 2)
1220            .Case("v1", 3)
1221            .Case("s0", 16)
1222            .Case("s1", 17)
1223            .Case("s2", 18)
1224            .Case("s3", 19)
1225            .Case("s4", 20)
1226            .Case("s5", 21)
1227            .Case("s6", 22)
1228            .Case("s7", 23)
1229            .Case("k0", 26)
1230            .Case("k1", 27)
1231            .Case("gp", 28)
1232            .Case("sp", 29)
1233            .Case("fp", 30)
1234            .Case("s8", 30)
1235            .Case("ra", 31)
1236            .Case("t0", 8)
1237            .Case("t1", 9)
1238            .Case("t2", 10)
1239            .Case("t3", 11)
1240            .Case("t4", 12)
1241            .Case("t5", 13)
1242            .Case("t6", 14)
1243            .Case("t7", 15)
1244            .Case("t8", 24)
1245            .Case("t9", 25)
1246            .Default(-1);
1247
1248   if (isN32() || isN64()) {
1249     // Although SGI documentation just cuts out t0-t3 for n32/n64,
1250     // GNU pushes the values of t0-t3 to override the o32/o64 values for t4-t7
1251     // We are supporting both cases, so for t0-t3 we'll just push them to t4-t7.
1252     if (8 <= CC && CC <= 11)
1253       CC += 4;
1254
1255     if (CC == -1)
1256       CC = StringSwitch<unsigned>(Name)
1257                .Case("a4", 8)
1258                .Case("a5", 9)
1259                .Case("a6", 10)
1260                .Case("a7", 11)
1261                .Case("kt0", 26)
1262                .Case("kt1", 27)
1263                .Default(-1);
1264   }
1265
1266   return CC;
1267 }
1268
1269 int MipsAsmParser::matchFPURegisterName(StringRef Name) {
1270
1271   if (Name[0] == 'f') {
1272     StringRef NumString = Name.substr(1);
1273     unsigned IntVal;
1274     if (NumString.getAsInteger(10, IntVal))
1275       return -1;     // This is not an integer.
1276     if (IntVal > 31) // Maximum index for fpu register.
1277       return -1;
1278     return IntVal;
1279   }
1280   return -1;
1281 }
1282
1283 int MipsAsmParser::matchFCCRegisterName(StringRef Name) {
1284
1285   if (Name.startswith("fcc")) {
1286     StringRef NumString = Name.substr(3);
1287     unsigned IntVal;
1288     if (NumString.getAsInteger(10, IntVal))
1289       return -1;    // This is not an integer.
1290     if (IntVal > 7) // There are only 8 fcc registers.
1291       return -1;
1292     return IntVal;
1293   }
1294   return -1;
1295 }
1296
1297 int MipsAsmParser::matchACRegisterName(StringRef Name) {
1298
1299   if (Name.startswith("ac")) {
1300     StringRef NumString = Name.substr(2);
1301     unsigned IntVal;
1302     if (NumString.getAsInteger(10, IntVal))
1303       return -1;    // This is not an integer.
1304     if (IntVal > 3) // There are only 3 acc registers.
1305       return -1;
1306     return IntVal;
1307   }
1308   return -1;
1309 }
1310
1311 int MipsAsmParser::matchMSA128RegisterName(StringRef Name) {
1312   unsigned IntVal;
1313
1314   if (Name.front() != 'w' || Name.drop_front(1).getAsInteger(10, IntVal))
1315     return -1;
1316
1317   if (IntVal > 31)
1318     return -1;
1319
1320   return IntVal;
1321 }
1322
1323 int MipsAsmParser::matchMSA128CtrlRegisterName(StringRef Name) {
1324   int CC;
1325
1326   CC = StringSwitch<unsigned>(Name)
1327            .Case("msair", 0)
1328            .Case("msacsr", 1)
1329            .Case("msaaccess", 2)
1330            .Case("msasave", 3)
1331            .Case("msamodify", 4)
1332            .Case("msarequest", 5)
1333            .Case("msamap", 6)
1334            .Case("msaunmap", 7)
1335            .Default(-1);
1336
1337   return CC;
1338 }
1339
1340 bool MipsAssemblerOptions::setATReg(unsigned Reg) {
1341   if (Reg > 31)
1342     return false;
1343
1344   aTReg = Reg;
1345   return true;
1346 }
1347
1348 int MipsAsmParser::getATReg() {
1349   int AT = Options.getATRegNum();
1350   if (AT == 0)
1351     TokError("Pseudo instruction requires $at, which is not available");
1352   return AT;
1353 }
1354
1355 unsigned MipsAsmParser::getReg(int RC, int RegNo) {
1356   return *(getContext().getRegisterInfo()->getRegClass(RC).begin() + RegNo);
1357 }
1358
1359 unsigned MipsAsmParser::getGPR(int RegNo) {
1360   return getReg(isGP64() ? Mips::GPR64RegClassID : Mips::GPR32RegClassID,
1361                 RegNo);
1362 }
1363
1364 int MipsAsmParser::matchRegisterByNumber(unsigned RegNum, unsigned RegClass) {
1365   if (RegNum >
1366       getContext().getRegisterInfo()->getRegClass(RegClass).getNumRegs() - 1)
1367     return -1;
1368
1369   return getReg(RegClass, RegNum);
1370 }
1371
1372 bool
1373 MipsAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand *> &Operands,
1374                             StringRef Mnemonic) {
1375   DEBUG(dbgs() << "ParseOperand\n");
1376
1377   // Check if the current operand has a custom associated parser, if so, try to
1378   // custom parse the operand, or fallback to the general approach.
1379   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1380   if (ResTy == MatchOperand_Success)
1381     return false;
1382   // If there wasn't a custom match, try the generic matcher below. Otherwise,
1383   // there was a match, but an error occurred, in which case, just return that
1384   // the operand parsing failed.
1385   if (ResTy == MatchOperand_ParseFail)
1386     return true;
1387
1388   DEBUG(dbgs() << ".. Generic Parser\n");
1389
1390   switch (getLexer().getKind()) {
1391   default:
1392     Error(Parser.getTok().getLoc(), "unexpected token in operand");
1393     return true;
1394   case AsmToken::Dollar: {
1395     // Parse the register.
1396     SMLoc S = Parser.getTok().getLoc();
1397
1398     // Almost all registers have been parsed by custom parsers. There is only
1399     // one exception to this. $zero (and it's alias $0) will reach this point
1400     // for div, divu, and similar instructions because it is not an operand
1401     // to the instruction definition but an explicit register. Special case
1402     // this situation for now.
1403     if (ParseAnyRegister(Operands) != MatchOperand_NoMatch)
1404       return false;
1405
1406     // Maybe it is a symbol reference.
1407     StringRef Identifier;
1408     if (Parser.parseIdentifier(Identifier))
1409       return true;
1410
1411     SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1412     MCSymbol *Sym = getContext().GetOrCreateSymbol("$" + Identifier);
1413     // Otherwise create a symbol reference.
1414     const MCExpr *Res =
1415         MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
1416
1417     Operands.push_back(MipsOperand::CreateImm(Res, S, E, *this));
1418     return false;
1419   }
1420   // Else drop to expression parsing.
1421   case AsmToken::LParen:
1422   case AsmToken::Minus:
1423   case AsmToken::Plus:
1424   case AsmToken::Integer:
1425   case AsmToken::String: {
1426     DEBUG(dbgs() << ".. generic integer\n");
1427     OperandMatchResultTy ResTy = ParseImm(Operands);
1428     return ResTy != MatchOperand_Success;
1429   }
1430   case AsmToken::Percent: {
1431     // It is a symbol reference or constant expression.
1432     const MCExpr *IdVal;
1433     SMLoc S = Parser.getTok().getLoc(); // Start location of the operand.
1434     if (parseRelocOperand(IdVal))
1435       return true;
1436
1437     SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1438
1439     Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
1440     return false;
1441   } // case AsmToken::Percent
1442   } // switch(getLexer().getKind())
1443   return true;
1444 }
1445
1446 const MCExpr *MipsAsmParser::evaluateRelocExpr(const MCExpr *Expr,
1447                                                StringRef RelocStr) {
1448   const MCExpr *Res;
1449   // Check the type of the expression.
1450   if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Expr)) {
1451     // It's a constant, evaluate lo or hi value.
1452     if (RelocStr == "lo") {
1453       short Val = MCE->getValue();
1454       Res = MCConstantExpr::Create(Val, getContext());
1455     } else if (RelocStr == "hi") {
1456       int Val = MCE->getValue();
1457       int LoSign = Val & 0x8000;
1458       Val = (Val & 0xffff0000) >> 16;
1459       // Lower part is treated as a signed int, so if it is negative
1460       // we must add 1 to the hi part to compensate.
1461       if (LoSign)
1462         Val++;
1463       Res = MCConstantExpr::Create(Val, getContext());
1464     } else {
1465       llvm_unreachable("Invalid RelocStr value");
1466     }
1467     return Res;
1468   }
1469
1470   if (const MCSymbolRefExpr *MSRE = dyn_cast<MCSymbolRefExpr>(Expr)) {
1471     // It's a symbol, create a symbolic expression from the symbol.
1472     StringRef Symbol = MSRE->getSymbol().getName();
1473     MCSymbolRefExpr::VariantKind VK = getVariantKind(RelocStr);
1474     Res = MCSymbolRefExpr::Create(Symbol, VK, getContext());
1475     return Res;
1476   }
1477
1478   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
1479     MCSymbolRefExpr::VariantKind VK = getVariantKind(RelocStr);
1480
1481     // Check for %hi(sym1-sym2) and %lo(sym1-sym2) expressions.
1482     if (isa<MCSymbolRefExpr>(BE->getLHS()) && isa<MCSymbolRefExpr>(BE->getRHS())
1483         && (VK == MCSymbolRefExpr::VK_Mips_ABS_HI
1484             || VK == MCSymbolRefExpr::VK_Mips_ABS_LO)) {
1485       // Create target expression for %hi(sym1-sym2) and %lo(sym1-sym2).
1486       if (VK == MCSymbolRefExpr::VK_Mips_ABS_HI)
1487         return MipsMCExpr::CreateHi(Expr, getContext());
1488       return MipsMCExpr::CreateLo(Expr, getContext());
1489     }
1490
1491     const MCExpr *LExp = evaluateRelocExpr(BE->getLHS(), RelocStr);
1492     const MCExpr *RExp = evaluateRelocExpr(BE->getRHS(), RelocStr);
1493     Res = MCBinaryExpr::Create(BE->getOpcode(), LExp, RExp, getContext());
1494     return Res;
1495   }
1496
1497   if (const MCUnaryExpr *UN = dyn_cast<MCUnaryExpr>(Expr)) {
1498     const MCExpr *UnExp = evaluateRelocExpr(UN->getSubExpr(), RelocStr);
1499     Res = MCUnaryExpr::Create(UN->getOpcode(), UnExp, getContext());
1500     return Res;
1501   }
1502   // Just return the original expression.
1503   return Expr;
1504 }
1505
1506 bool MipsAsmParser::isEvaluated(const MCExpr *Expr) {
1507
1508   switch (Expr->getKind()) {
1509   case MCExpr::Constant:
1510     return true;
1511   case MCExpr::SymbolRef:
1512     return (cast<MCSymbolRefExpr>(Expr)->getKind() != MCSymbolRefExpr::VK_None);
1513   case MCExpr::Binary:
1514     if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
1515       if (!isEvaluated(BE->getLHS()))
1516         return false;
1517       return isEvaluated(BE->getRHS());
1518     }
1519   case MCExpr::Unary:
1520     return isEvaluated(cast<MCUnaryExpr>(Expr)->getSubExpr());
1521   case MCExpr::Target:
1522     return true;
1523   }
1524   return false;
1525 }
1526
1527 bool MipsAsmParser::parseRelocOperand(const MCExpr *&Res) {
1528   Parser.Lex();                          // Eat the % token.
1529   const AsmToken &Tok = Parser.getTok(); // Get next token, operation.
1530   if (Tok.isNot(AsmToken::Identifier))
1531     return true;
1532
1533   std::string Str = Tok.getIdentifier().str();
1534
1535   Parser.Lex(); // Eat the identifier.
1536   // Now make an expression from the rest of the operand.
1537   const MCExpr *IdVal;
1538   SMLoc EndLoc;
1539
1540   if (getLexer().getKind() == AsmToken::LParen) {
1541     while (1) {
1542       Parser.Lex(); // Eat the '(' token.
1543       if (getLexer().getKind() == AsmToken::Percent) {
1544         Parser.Lex(); // Eat the % token.
1545         const AsmToken &nextTok = Parser.getTok();
1546         if (nextTok.isNot(AsmToken::Identifier))
1547           return true;
1548         Str += "(%";
1549         Str += nextTok.getIdentifier();
1550         Parser.Lex(); // Eat the identifier.
1551         if (getLexer().getKind() != AsmToken::LParen)
1552           return true;
1553       } else
1554         break;
1555     }
1556     if (getParser().parseParenExpression(IdVal, EndLoc))
1557       return true;
1558
1559     while (getLexer().getKind() == AsmToken::RParen)
1560       Parser.Lex(); // Eat the ')' token.
1561
1562   } else
1563     return true; // Parenthesis must follow the relocation operand.
1564
1565   Res = evaluateRelocExpr(IdVal, Str);
1566   return false;
1567 }
1568
1569 bool MipsAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
1570                                   SMLoc &EndLoc) {
1571   SmallVector<MCParsedAsmOperand *, 1> Operands;
1572   OperandMatchResultTy ResTy = ParseAnyRegister(Operands);
1573   if (ResTy == MatchOperand_Success) {
1574     assert(Operands.size() == 1);
1575     MipsOperand &Operand = *static_cast<MipsOperand *>(Operands.front());
1576     StartLoc = Operand.getStartLoc();
1577     EndLoc = Operand.getEndLoc();
1578
1579     // AFAIK, we only support numeric registers and named GPR's in CFI
1580     // directives.
1581     // Don't worry about eating tokens before failing. Using an unrecognised
1582     // register is a parse error.
1583     if (Operand.isGPRAsmReg()) {
1584       // Resolve to GPR32 or GPR64 appropriately.
1585       RegNo = isGP64() ? Operand.getGPR64Reg() : Operand.getGPR32Reg();
1586     }
1587
1588     return (RegNo == (unsigned)-1);
1589   }
1590
1591   assert(Operands.size() == 0);
1592   return (RegNo == (unsigned)-1);
1593 }
1594
1595 bool MipsAsmParser::parseMemOffset(const MCExpr *&Res, bool isParenExpr) {
1596   SMLoc S;
1597   bool Result = true;
1598
1599   while (getLexer().getKind() == AsmToken::LParen)
1600     Parser.Lex();
1601
1602   switch (getLexer().getKind()) {
1603   default:
1604     return true;
1605   case AsmToken::Identifier:
1606   case AsmToken::LParen:
1607   case AsmToken::Integer:
1608   case AsmToken::Minus:
1609   case AsmToken::Plus:
1610     if (isParenExpr)
1611       Result = getParser().parseParenExpression(Res, S);
1612     else
1613       Result = (getParser().parseExpression(Res));
1614     while (getLexer().getKind() == AsmToken::RParen)
1615       Parser.Lex();
1616     break;
1617   case AsmToken::Percent:
1618     Result = parseRelocOperand(Res);
1619   }
1620   return Result;
1621 }
1622
1623 MipsAsmParser::OperandMatchResultTy MipsAsmParser::parseMemOperand(
1624     SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1625   DEBUG(dbgs() << "parseMemOperand\n");
1626   const MCExpr *IdVal = 0;
1627   SMLoc S;
1628   bool isParenExpr = false;
1629   MipsAsmParser::OperandMatchResultTy Res = MatchOperand_NoMatch;
1630   // First operand is the offset.
1631   S = Parser.getTok().getLoc();
1632
1633   if (getLexer().getKind() == AsmToken::LParen) {
1634     Parser.Lex();
1635     isParenExpr = true;
1636   }
1637
1638   if (getLexer().getKind() != AsmToken::Dollar) {
1639     if (parseMemOffset(IdVal, isParenExpr))
1640       return MatchOperand_ParseFail;
1641
1642     const AsmToken &Tok = Parser.getTok(); // Get the next token.
1643     if (Tok.isNot(AsmToken::LParen)) {
1644       MipsOperand *Mnemonic = static_cast<MipsOperand *>(Operands[0]);
1645       if (Mnemonic->getToken() == "la") {
1646         SMLoc E =
1647             SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1648         Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
1649         return MatchOperand_Success;
1650       }
1651       if (Tok.is(AsmToken::EndOfStatement)) {
1652         SMLoc E =
1653             SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1654
1655         // Zero register assumed, add a memory operand with ZERO as its base.
1656         MipsOperand *Base = MipsOperand::CreateGPRReg(
1657             0, getContext().getRegisterInfo(), S, E, *this);
1658         Operands.push_back(MipsOperand::CreateMem(Base, IdVal, S, E, *this));
1659         return MatchOperand_Success;
1660       }
1661       Error(Parser.getTok().getLoc(), "'(' expected");
1662       return MatchOperand_ParseFail;
1663     }
1664
1665     Parser.Lex(); // Eat the '(' token.
1666   }
1667
1668   Res = ParseAnyRegister(Operands);
1669   if (Res != MatchOperand_Success)
1670     return Res;
1671
1672   if (Parser.getTok().isNot(AsmToken::RParen)) {
1673     Error(Parser.getTok().getLoc(), "')' expected");
1674     return MatchOperand_ParseFail;
1675   }
1676
1677   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1678
1679   Parser.Lex(); // Eat the ')' token.
1680
1681   if (IdVal == 0)
1682     IdVal = MCConstantExpr::Create(0, getContext());
1683
1684   // Replace the register operand with the memory operand.
1685   MipsOperand *op = static_cast<MipsOperand *>(Operands.back());
1686   // Remove the register from the operands.
1687   Operands.pop_back();
1688   // Add the memory operand.
1689   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(IdVal)) {
1690     int64_t Imm;
1691     if (IdVal->EvaluateAsAbsolute(Imm))
1692       IdVal = MCConstantExpr::Create(Imm, getContext());
1693     else if (BE->getLHS()->getKind() != MCExpr::SymbolRef)
1694       IdVal = MCBinaryExpr::Create(BE->getOpcode(), BE->getRHS(), BE->getLHS(),
1695                                    getContext());
1696   }
1697
1698   Operands.push_back(MipsOperand::CreateMem(op, IdVal, S, E, *this));
1699   return MatchOperand_Success;
1700 }
1701
1702 bool MipsAsmParser::searchSymbolAlias(
1703     SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1704
1705   MCSymbol *Sym = getContext().LookupSymbol(Parser.getTok().getIdentifier());
1706   if (Sym) {
1707     SMLoc S = Parser.getTok().getLoc();
1708     const MCExpr *Expr;
1709     if (Sym->isVariable())
1710       Expr = Sym->getVariableValue();
1711     else
1712       return false;
1713     if (Expr->getKind() == MCExpr::SymbolRef) {
1714       const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
1715       const StringRef DefSymbol = Ref->getSymbol().getName();
1716       if (DefSymbol.startswith("$")) {
1717         OperandMatchResultTy ResTy =
1718             MatchAnyRegisterNameWithoutDollar(Operands, DefSymbol.substr(1), S);
1719         if (ResTy == MatchOperand_Success) {
1720           Parser.Lex();
1721           return true;
1722         } else if (ResTy == MatchOperand_ParseFail)
1723           llvm_unreachable("Should never ParseFail");
1724         return false;
1725       }
1726     } else if (Expr->getKind() == MCExpr::Constant) {
1727       Parser.Lex();
1728       const MCConstantExpr *Const = static_cast<const MCConstantExpr *>(Expr);
1729       MipsOperand *op =
1730           MipsOperand::CreateImm(Const, S, Parser.getTok().getLoc(), *this);
1731       Operands.push_back(op);
1732       return true;
1733     }
1734   }
1735   return false;
1736 }
1737
1738 MipsAsmParser::OperandMatchResultTy
1739 MipsAsmParser::MatchAnyRegisterNameWithoutDollar(
1740     SmallVectorImpl<MCParsedAsmOperand *> &Operands, StringRef Identifier,
1741     SMLoc S) {
1742   int Index = matchCPURegisterName(Identifier);
1743   if (Index != -1) {
1744     Operands.push_back(MipsOperand::CreateGPRReg(
1745         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
1746     return MatchOperand_Success;
1747   }
1748
1749   Index = matchFPURegisterName(Identifier);
1750   if (Index != -1) {
1751     Operands.push_back(MipsOperand::CreateFGRReg(
1752         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
1753     return MatchOperand_Success;
1754   }
1755
1756   Index = matchFCCRegisterName(Identifier);
1757   if (Index != -1) {
1758     Operands.push_back(MipsOperand::CreateFCCReg(
1759         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
1760     return MatchOperand_Success;
1761   }
1762
1763   Index = matchACRegisterName(Identifier);
1764   if (Index != -1) {
1765     Operands.push_back(MipsOperand::CreateACCReg(
1766         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
1767     return MatchOperand_Success;
1768   }
1769
1770   Index = matchMSA128RegisterName(Identifier);
1771   if (Index != -1) {
1772     Operands.push_back(MipsOperand::CreateMSA128Reg(
1773         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
1774     return MatchOperand_Success;
1775   }
1776
1777   Index = matchMSA128CtrlRegisterName(Identifier);
1778   if (Index != -1) {
1779     Operands.push_back(MipsOperand::CreateMSACtrlReg(
1780         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
1781     return MatchOperand_Success;
1782   }
1783
1784   return MatchOperand_NoMatch;
1785 }
1786
1787 MipsAsmParser::OperandMatchResultTy
1788 MipsAsmParser::MatchAnyRegisterWithoutDollar(
1789     SmallVectorImpl<MCParsedAsmOperand *> &Operands, SMLoc S) {
1790   auto Token = Parser.getLexer().peekTok(false);
1791
1792   if (Token.is(AsmToken::Identifier)) {
1793     DEBUG(dbgs() << ".. identifier\n");
1794     StringRef Identifier = Token.getIdentifier();
1795     OperandMatchResultTy ResTy =
1796         MatchAnyRegisterNameWithoutDollar(Operands, Identifier, S);
1797     return ResTy;
1798   } else if (Token.is(AsmToken::Integer)) {
1799     DEBUG(dbgs() << ".. integer\n");
1800     Operands.push_back(MipsOperand::CreateNumericReg(
1801         Token.getIntVal(), getContext().getRegisterInfo(), S, Token.getLoc(),
1802         *this));
1803     return MatchOperand_Success;
1804   }
1805
1806   DEBUG(dbgs() << Parser.getTok().getKind() << "\n");
1807
1808   return MatchOperand_NoMatch;
1809 }
1810
1811 MipsAsmParser::OperandMatchResultTy MipsAsmParser::ParseAnyRegister(
1812     SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1813   DEBUG(dbgs() << "ParseAnyRegister\n");
1814
1815   auto Token = Parser.getTok();
1816
1817   SMLoc S = Token.getLoc();
1818
1819   if (Token.isNot(AsmToken::Dollar)) {
1820     DEBUG(dbgs() << ".. !$ -> try sym aliasing\n");
1821     if (Token.is(AsmToken::Identifier)) {
1822       if (searchSymbolAlias(Operands))
1823         return MatchOperand_Success;
1824     }
1825     DEBUG(dbgs() << ".. !symalias -> NoMatch\n");
1826     return MatchOperand_NoMatch;
1827   }
1828   DEBUG(dbgs() << ".. $\n");
1829
1830   OperandMatchResultTy ResTy = MatchAnyRegisterWithoutDollar(Operands, S);
1831   if (ResTy == MatchOperand_Success) {
1832     Parser.Lex(); // $
1833     Parser.Lex(); // identifier
1834   }
1835   return ResTy;
1836 }
1837
1838 MipsAsmParser::OperandMatchResultTy
1839 MipsAsmParser::ParseImm(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1840   switch (getLexer().getKind()) {
1841   default:
1842     return MatchOperand_NoMatch;
1843   case AsmToken::LParen:
1844   case AsmToken::Minus:
1845   case AsmToken::Plus:
1846   case AsmToken::Integer:
1847   case AsmToken::String:
1848     break;
1849   }
1850
1851   const MCExpr *IdVal;
1852   SMLoc S = Parser.getTok().getLoc();
1853   if (getParser().parseExpression(IdVal))
1854     return MatchOperand_ParseFail;
1855
1856   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1857   Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
1858   return MatchOperand_Success;
1859 }
1860
1861 MipsAsmParser::OperandMatchResultTy MipsAsmParser::ParseJumpTarget(
1862     SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1863   DEBUG(dbgs() << "ParseJumpTarget\n");
1864
1865   SMLoc S = getLexer().getLoc();
1866
1867   // Integers and expressions are acceptable
1868   OperandMatchResultTy ResTy = ParseImm(Operands);
1869   if (ResTy != MatchOperand_NoMatch)
1870     return ResTy;
1871
1872   // Registers are a valid target and have priority over symbols.
1873   ResTy = ParseAnyRegister(Operands);
1874   if (ResTy != MatchOperand_NoMatch)
1875     return ResTy;
1876
1877   const MCExpr *Expr = nullptr;
1878   if (Parser.parseExpression(Expr)) {
1879     // We have no way of knowing if a symbol was consumed so we must ParseFail
1880     return MatchOperand_ParseFail;
1881   }
1882   Operands.push_back(
1883       MipsOperand::CreateImm(Expr, S, getLexer().getLoc(), *this));
1884   return MatchOperand_Success;
1885 }
1886
1887 MipsAsmParser::OperandMatchResultTy
1888 MipsAsmParser::parseInvNum(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1889   const MCExpr *IdVal;
1890   // If the first token is '$' we may have register operand.
1891   if (Parser.getTok().is(AsmToken::Dollar))
1892     return MatchOperand_NoMatch;
1893   SMLoc S = Parser.getTok().getLoc();
1894   if (getParser().parseExpression(IdVal))
1895     return MatchOperand_ParseFail;
1896   const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(IdVal);
1897   assert(MCE && "Unexpected MCExpr type.");
1898   int64_t Val = MCE->getValue();
1899   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1900   Operands.push_back(MipsOperand::CreateImm(
1901       MCConstantExpr::Create(0 - Val, getContext()), S, E, *this));
1902   return MatchOperand_Success;
1903 }
1904
1905 MipsAsmParser::OperandMatchResultTy
1906 MipsAsmParser::ParseLSAImm(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1907   switch (getLexer().getKind()) {
1908   default:
1909     return MatchOperand_NoMatch;
1910   case AsmToken::LParen:
1911   case AsmToken::Plus:
1912   case AsmToken::Minus:
1913   case AsmToken::Integer:
1914     break;
1915   }
1916
1917   const MCExpr *Expr;
1918   SMLoc S = Parser.getTok().getLoc();
1919
1920   if (getParser().parseExpression(Expr))
1921     return MatchOperand_ParseFail;
1922
1923   int64_t Val;
1924   if (!Expr->EvaluateAsAbsolute(Val)) {
1925     Error(S, "expected immediate value");
1926     return MatchOperand_ParseFail;
1927   }
1928
1929   // The LSA instruction allows a 2-bit unsigned immediate. For this reason
1930   // and because the CPU always adds one to the immediate field, the allowed
1931   // range becomes 1..4. We'll only check the range here and will deal
1932   // with the addition/subtraction when actually decoding/encoding
1933   // the instruction.
1934   if (Val < 1 || Val > 4) {
1935     Error(S, "immediate not in range (1..4)");
1936     return MatchOperand_ParseFail;
1937   }
1938
1939   Operands.push_back(
1940       MipsOperand::CreateImm(Expr, S, Parser.getTok().getLoc(), *this));
1941   return MatchOperand_Success;
1942 }
1943
1944 MCSymbolRefExpr::VariantKind MipsAsmParser::getVariantKind(StringRef Symbol) {
1945
1946   MCSymbolRefExpr::VariantKind VK =
1947       StringSwitch<MCSymbolRefExpr::VariantKind>(Symbol)
1948           .Case("hi", MCSymbolRefExpr::VK_Mips_ABS_HI)
1949           .Case("lo", MCSymbolRefExpr::VK_Mips_ABS_LO)
1950           .Case("gp_rel", MCSymbolRefExpr::VK_Mips_GPREL)
1951           .Case("call16", MCSymbolRefExpr::VK_Mips_GOT_CALL)
1952           .Case("got", MCSymbolRefExpr::VK_Mips_GOT)
1953           .Case("tlsgd", MCSymbolRefExpr::VK_Mips_TLSGD)
1954           .Case("tlsldm", MCSymbolRefExpr::VK_Mips_TLSLDM)
1955           .Case("dtprel_hi", MCSymbolRefExpr::VK_Mips_DTPREL_HI)
1956           .Case("dtprel_lo", MCSymbolRefExpr::VK_Mips_DTPREL_LO)
1957           .Case("gottprel", MCSymbolRefExpr::VK_Mips_GOTTPREL)
1958           .Case("tprel_hi", MCSymbolRefExpr::VK_Mips_TPREL_HI)
1959           .Case("tprel_lo", MCSymbolRefExpr::VK_Mips_TPREL_LO)
1960           .Case("got_disp", MCSymbolRefExpr::VK_Mips_GOT_DISP)
1961           .Case("got_page", MCSymbolRefExpr::VK_Mips_GOT_PAGE)
1962           .Case("got_ofst", MCSymbolRefExpr::VK_Mips_GOT_OFST)
1963           .Case("hi(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_HI)
1964           .Case("lo(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_LO)
1965           .Case("got_hi", MCSymbolRefExpr::VK_Mips_GOT_HI16)
1966           .Case("got_lo", MCSymbolRefExpr::VK_Mips_GOT_LO16)
1967           .Case("call_hi", MCSymbolRefExpr::VK_Mips_CALL_HI16)
1968           .Case("call_lo", MCSymbolRefExpr::VK_Mips_CALL_LO16)
1969           .Case("higher", MCSymbolRefExpr::VK_Mips_HIGHER)
1970           .Case("highest", MCSymbolRefExpr::VK_Mips_HIGHEST)
1971           .Default(MCSymbolRefExpr::VK_None);
1972
1973   assert (VK != MCSymbolRefExpr::VK_None);
1974
1975   return VK;
1976 }
1977
1978 /// Sometimes (i.e. load/stores) the operand may be followed immediately by
1979 /// either this.
1980 /// ::= '(', register, ')'
1981 /// handle it before we iterate so we don't get tripped up by the lack of
1982 /// a comma.
1983 bool MipsAsmParser::ParseParenSuffix(
1984     StringRef Name, SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1985   if (getLexer().is(AsmToken::LParen)) {
1986     Operands.push_back(
1987         MipsOperand::CreateToken("(", getLexer().getLoc(), *this));
1988     Parser.Lex();
1989     if (ParseOperand(Operands, Name)) {
1990       SMLoc Loc = getLexer().getLoc();
1991       Parser.eatToEndOfStatement();
1992       return Error(Loc, "unexpected token in argument list");
1993     }
1994     if (Parser.getTok().isNot(AsmToken::RParen)) {
1995       SMLoc Loc = getLexer().getLoc();
1996       Parser.eatToEndOfStatement();
1997       return Error(Loc, "unexpected token, expected ')'");
1998     }
1999     Operands.push_back(
2000         MipsOperand::CreateToken(")", getLexer().getLoc(), *this));
2001     Parser.Lex();
2002   }
2003   return false;
2004 }
2005
2006 /// Sometimes (i.e. in MSA) the operand may be followed immediately by
2007 /// either one of these.
2008 /// ::= '[', register, ']'
2009 /// ::= '[', integer, ']'
2010 /// handle it before we iterate so we don't get tripped up by the lack of
2011 /// a comma.
2012 bool MipsAsmParser::ParseBracketSuffix(
2013     StringRef Name, SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
2014   if (getLexer().is(AsmToken::LBrac)) {
2015     Operands.push_back(
2016         MipsOperand::CreateToken("[", getLexer().getLoc(), *this));
2017     Parser.Lex();
2018     if (ParseOperand(Operands, Name)) {
2019       SMLoc Loc = getLexer().getLoc();
2020       Parser.eatToEndOfStatement();
2021       return Error(Loc, "unexpected token in argument list");
2022     }
2023     if (Parser.getTok().isNot(AsmToken::RBrac)) {
2024       SMLoc Loc = getLexer().getLoc();
2025       Parser.eatToEndOfStatement();
2026       return Error(Loc, "unexpected token, expected ']'");
2027     }
2028     Operands.push_back(
2029         MipsOperand::CreateToken("]", getLexer().getLoc(), *this));
2030     Parser.Lex();
2031   }
2032   return false;
2033 }
2034
2035 bool MipsAsmParser::ParseInstruction(
2036     ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
2037     SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
2038   DEBUG(dbgs() << "ParseInstruction\n");
2039   // Check if we have valid mnemonic
2040   if (!mnemonicIsValid(Name, 0)) {
2041     Parser.eatToEndOfStatement();
2042     return Error(NameLoc, "Unknown instruction");
2043   }
2044   // First operand in MCInst is instruction mnemonic.
2045   Operands.push_back(MipsOperand::CreateToken(Name, NameLoc, *this));
2046
2047   // Read the remaining operands.
2048   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2049     // Read the first operand.
2050     if (ParseOperand(Operands, Name)) {
2051       SMLoc Loc = getLexer().getLoc();
2052       Parser.eatToEndOfStatement();
2053       return Error(Loc, "unexpected token in argument list");
2054     }
2055     if (getLexer().is(AsmToken::LBrac) && ParseBracketSuffix(Name, Operands))
2056       return true;
2057     // AFAIK, parenthesis suffixes are never on the first operand
2058
2059     while (getLexer().is(AsmToken::Comma)) {
2060       Parser.Lex(); // Eat the comma.
2061       // Parse and remember the operand.
2062       if (ParseOperand(Operands, Name)) {
2063         SMLoc Loc = getLexer().getLoc();
2064         Parser.eatToEndOfStatement();
2065         return Error(Loc, "unexpected token in argument list");
2066       }
2067       // Parse bracket and parenthesis suffixes before we iterate
2068       if (getLexer().is(AsmToken::LBrac)) {
2069         if (ParseBracketSuffix(Name, Operands))
2070           return true;
2071       } else if (getLexer().is(AsmToken::LParen) &&
2072                  ParseParenSuffix(Name, Operands))
2073         return true;
2074     }
2075   }
2076   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2077     SMLoc Loc = getLexer().getLoc();
2078     Parser.eatToEndOfStatement();
2079     return Error(Loc, "unexpected token in argument list");
2080   }
2081   Parser.Lex(); // Consume the EndOfStatement.
2082   return false;
2083 }
2084
2085 bool MipsAsmParser::reportParseError(StringRef ErrorMsg) {
2086   SMLoc Loc = getLexer().getLoc();
2087   Parser.eatToEndOfStatement();
2088   return Error(Loc, ErrorMsg);
2089 }
2090
2091 bool MipsAsmParser::parseSetNoAtDirective() {
2092   // Line should look like: ".set noat".
2093   // set at reg to 0.
2094   Options.setATReg(0);
2095   // eat noat
2096   Parser.Lex();
2097   // If this is not the end of the statement, report an error.
2098   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2099     reportParseError("unexpected token in statement");
2100     return false;
2101   }
2102   Parser.Lex(); // Consume the EndOfStatement.
2103   return false;
2104 }
2105
2106 bool MipsAsmParser::parseSetAtDirective() {
2107   // Line can be .set at - defaults to $1
2108   // or .set at=$reg
2109   int AtRegNo;
2110   getParser().Lex();
2111   if (getLexer().is(AsmToken::EndOfStatement)) {
2112     Options.setATReg(1);
2113     Parser.Lex(); // Consume the EndOfStatement.
2114     return false;
2115   } else if (getLexer().is(AsmToken::Equal)) {
2116     getParser().Lex(); // Eat the '='.
2117     if (getLexer().isNot(AsmToken::Dollar)) {
2118       reportParseError("unexpected token in statement");
2119       return false;
2120     }
2121     Parser.Lex(); // Eat the '$'.
2122     const AsmToken &Reg = Parser.getTok();
2123     if (Reg.is(AsmToken::Identifier)) {
2124       AtRegNo = matchCPURegisterName(Reg.getIdentifier());
2125     } else if (Reg.is(AsmToken::Integer)) {
2126       AtRegNo = Reg.getIntVal();
2127     } else {
2128       reportParseError("unexpected token in statement");
2129       return false;
2130     }
2131
2132     if (AtRegNo < 0 || AtRegNo > 31) {
2133       reportParseError("unexpected token in statement");
2134       return false;
2135     }
2136
2137     if (!Options.setATReg(AtRegNo)) {
2138       reportParseError("unexpected token in statement");
2139       return false;
2140     }
2141     getParser().Lex(); // Eat the register.
2142
2143     if (getLexer().isNot(AsmToken::EndOfStatement)) {
2144       reportParseError("unexpected token in statement");
2145       return false;
2146     }
2147     Parser.Lex(); // Consume the EndOfStatement.
2148     return false;
2149   } else {
2150     reportParseError("unexpected token in statement");
2151     return false;
2152   }
2153 }
2154
2155 bool MipsAsmParser::parseSetReorderDirective() {
2156   Parser.Lex();
2157   // If this is not the end of the statement, report an error.
2158   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2159     reportParseError("unexpected token in statement");
2160     return false;
2161   }
2162   Options.setReorder();
2163   getTargetStreamer().emitDirectiveSetReorder();
2164   Parser.Lex(); // Consume the EndOfStatement.
2165   return false;
2166 }
2167
2168 bool MipsAsmParser::parseSetNoReorderDirective() {
2169   Parser.Lex();
2170   // If this is not the end of the statement, report an error.
2171   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2172     reportParseError("unexpected token in statement");
2173     return false;
2174   }
2175   Options.setNoreorder();
2176   getTargetStreamer().emitDirectiveSetNoReorder();
2177   Parser.Lex(); // Consume the EndOfStatement.
2178   return false;
2179 }
2180
2181 bool MipsAsmParser::parseSetMacroDirective() {
2182   Parser.Lex();
2183   // If this is not the end of the statement, report an error.
2184   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2185     reportParseError("unexpected token in statement");
2186     return false;
2187   }
2188   Options.setMacro();
2189   Parser.Lex(); // Consume the EndOfStatement.
2190   return false;
2191 }
2192
2193 bool MipsAsmParser::parseSetNoMacroDirective() {
2194   Parser.Lex();
2195   // If this is not the end of the statement, report an error.
2196   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2197     reportParseError("`noreorder' must be set before `nomacro'");
2198     return false;
2199   }
2200   if (Options.isReorder()) {
2201     reportParseError("`noreorder' must be set before `nomacro'");
2202     return false;
2203   }
2204   Options.setNomacro();
2205   Parser.Lex(); // Consume the EndOfStatement.
2206   return false;
2207 }
2208
2209 bool MipsAsmParser::parseSetNoMips16Directive() {
2210   Parser.Lex();
2211   // If this is not the end of the statement, report an error.
2212   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2213     reportParseError("unexpected token in statement");
2214     return false;
2215   }
2216   // For now do nothing.
2217   Parser.Lex(); // Consume the EndOfStatement.
2218   return false;
2219 }
2220
2221 bool MipsAsmParser::parseSetAssignment() {
2222   StringRef Name;
2223   const MCExpr *Value;
2224
2225   if (Parser.parseIdentifier(Name))
2226     reportParseError("expected identifier after .set");
2227
2228   if (getLexer().isNot(AsmToken::Comma))
2229     return reportParseError("unexpected token in .set directive");
2230   Lex(); // Eat comma
2231
2232   if (Parser.parseExpression(Value))
2233     return reportParseError("expected valid expression after comma");
2234
2235   // Check if the Name already exists as a symbol.
2236   MCSymbol *Sym = getContext().LookupSymbol(Name);
2237   if (Sym)
2238     return reportParseError("symbol already defined");
2239   Sym = getContext().GetOrCreateSymbol(Name);
2240   Sym->setVariableValue(Value);
2241
2242   return false;
2243 }
2244
2245 bool MipsAsmParser::parseSetFeature(uint64_t Feature) {
2246   Parser.Lex();
2247   if (getLexer().isNot(AsmToken::EndOfStatement))
2248     return reportParseError("unexpected token in .set directive");
2249
2250   switch(Feature) {
2251     default: llvm_unreachable("Unimplemented feature");
2252     case Mips::FeatureDSP:
2253       setFeatureBits(Mips::FeatureDSP, "dsp");
2254       getTargetStreamer().emitDirectiveSetDsp();
2255     break;
2256     case Mips::FeatureMicroMips:
2257       getTargetStreamer().emitDirectiveSetMicroMips();
2258     break;
2259     case Mips::FeatureMips16:
2260       getTargetStreamer().emitDirectiveSetMips16();
2261     break;
2262     case Mips::FeatureMips32r2:
2263       setFeatureBits(Mips::FeatureMips32r2, "mips32r2");
2264       getTargetStreamer().emitDirectiveSetMips32R2();
2265     break;
2266     case Mips::FeatureMips64:
2267       setFeatureBits(Mips::FeatureMips64, "mips64");
2268       getTargetStreamer().emitDirectiveSetMips64();
2269     break;
2270     case Mips::FeatureMips64r2:
2271       setFeatureBits(Mips::FeatureMips64r2, "mips64r2");
2272       getTargetStreamer().emitDirectiveSetMips64R2();
2273     break;
2274   }
2275   return false;
2276 }
2277
2278 bool MipsAsmParser::parseRegister(unsigned &RegNum) {
2279   if (!getLexer().is(AsmToken::Dollar))
2280     return false;
2281
2282   Parser.Lex();
2283
2284   const AsmToken &Reg = Parser.getTok();
2285   if (Reg.is(AsmToken::Identifier)) {
2286     RegNum = matchCPURegisterName(Reg.getIdentifier());
2287   } else if (Reg.is(AsmToken::Integer)) {
2288     RegNum = Reg.getIntVal();
2289   } else {
2290     return false;
2291   }
2292
2293   Parser.Lex();
2294   return true;
2295 }
2296
2297 bool MipsAsmParser::eatComma(StringRef ErrorStr) {
2298   if (getLexer().isNot(AsmToken::Comma)) {
2299     SMLoc Loc = getLexer().getLoc();
2300     Parser.eatToEndOfStatement();
2301     return Error(Loc, ErrorStr);
2302   }
2303
2304   Parser.Lex();  // Eat the comma.
2305   return true;
2306 }
2307
2308 bool MipsAsmParser::parseDirectiveCPSetup() {
2309   unsigned FuncReg;
2310   unsigned Save;
2311   bool SaveIsReg = true;
2312
2313   if (!parseRegister(FuncReg))
2314     return reportParseError("expected register containing function address");
2315   FuncReg = getGPR(FuncReg);
2316
2317   if (!eatComma("expected comma parsing directive"))
2318     return true;
2319
2320   if (!parseRegister(Save)) {
2321     const AsmToken &Tok = Parser.getTok();
2322     if (Tok.is(AsmToken::Integer)) {
2323       Save = Tok.getIntVal();
2324       SaveIsReg = false;
2325       Parser.Lex();
2326     } else
2327       return reportParseError("expected save register or stack offset");
2328   } else
2329     Save = getGPR(Save);
2330
2331   if (!eatComma("expected comma parsing directive"))
2332     return true;
2333
2334   StringRef Name;
2335   if (Parser.parseIdentifier(Name))
2336     reportParseError("expected identifier");
2337   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2338   unsigned GPReg = getGPR(matchCPURegisterName("gp"));
2339
2340   // FIXME: The code below this point should be in the TargetStreamers.
2341   // Only N32 and N64 emit anything for .cpsetup
2342   // FIXME: We should only emit something for PIC mode too.
2343   if (!isN32() && !isN64())
2344     return false;
2345
2346   MCStreamer &TS = getStreamer();
2347   MCInst Inst;
2348   // Either store the old $gp in a register or on the stack
2349   if (SaveIsReg) {
2350     // move $save, $gpreg
2351     Inst.setOpcode(Mips::DADDu);
2352     Inst.addOperand(MCOperand::CreateReg(Save));
2353     Inst.addOperand(MCOperand::CreateReg(GPReg));
2354     Inst.addOperand(MCOperand::CreateReg(getGPR(0)));
2355   } else {
2356     // sd $gpreg, offset($sp)
2357     Inst.setOpcode(Mips::SD);
2358     Inst.addOperand(MCOperand::CreateReg(GPReg));
2359     Inst.addOperand(MCOperand::CreateReg(getGPR(matchCPURegisterName("sp"))));
2360     Inst.addOperand(MCOperand::CreateImm(Save));
2361   }
2362   TS.EmitInstruction(Inst, STI);
2363   Inst.clear();
2364
2365   const MCSymbolRefExpr *HiExpr = MCSymbolRefExpr::Create(
2366       Sym->getName(), MCSymbolRefExpr::VK_Mips_GPOFF_HI,
2367       getContext());
2368   const MCSymbolRefExpr *LoExpr = MCSymbolRefExpr::Create(
2369       Sym->getName(), MCSymbolRefExpr::VK_Mips_GPOFF_LO,
2370       getContext());
2371   // lui $gp, %hi(%neg(%gp_rel(funcSym)))
2372   Inst.setOpcode(Mips::LUi);
2373   Inst.addOperand(MCOperand::CreateReg(GPReg));
2374   Inst.addOperand(MCOperand::CreateExpr(HiExpr));
2375   TS.EmitInstruction(Inst, STI);
2376   Inst.clear();
2377
2378   // addiu  $gp, $gp, %lo(%neg(%gp_rel(funcSym)))
2379   Inst.setOpcode(Mips::ADDiu);
2380   Inst.addOperand(MCOperand::CreateReg(GPReg));
2381   Inst.addOperand(MCOperand::CreateReg(GPReg));
2382   Inst.addOperand(MCOperand::CreateExpr(LoExpr));
2383   TS.EmitInstruction(Inst, STI);
2384   Inst.clear();
2385
2386   // daddu  $gp, $gp, $funcreg
2387   Inst.setOpcode(Mips::DADDu);
2388   Inst.addOperand(MCOperand::CreateReg(GPReg));
2389   Inst.addOperand(MCOperand::CreateReg(GPReg));
2390   Inst.addOperand(MCOperand::CreateReg(FuncReg));
2391   TS.EmitInstruction(Inst, STI);
2392   return false;
2393 }
2394
2395 bool MipsAsmParser::parseDirectiveSet() {
2396
2397   // Get the next token.
2398   const AsmToken &Tok = Parser.getTok();
2399
2400   if (Tok.getString() == "noat") {
2401     return parseSetNoAtDirective();
2402   } else if (Tok.getString() == "at") {
2403     return parseSetAtDirective();
2404   } else if (Tok.getString() == "reorder") {
2405     return parseSetReorderDirective();
2406   } else if (Tok.getString() == "noreorder") {
2407     return parseSetNoReorderDirective();
2408   } else if (Tok.getString() == "macro") {
2409     return parseSetMacroDirective();
2410   } else if (Tok.getString() == "nomacro") {
2411     return parseSetNoMacroDirective();
2412   } else if (Tok.getString() == "mips16") {
2413     return parseSetFeature(Mips::FeatureMips16);
2414   } else if (Tok.getString() == "nomips16") {
2415     return parseSetNoMips16Directive();
2416   } else if (Tok.getString() == "nomicromips") {
2417     getTargetStreamer().emitDirectiveSetNoMicroMips();
2418     Parser.eatToEndOfStatement();
2419     return false;
2420   } else if (Tok.getString() == "micromips") {
2421       return parseSetFeature(Mips::FeatureMicroMips);
2422   } else if (Tok.getString() == "mips32r2") {
2423       return parseSetFeature(Mips::FeatureMips32r2);
2424   } else if (Tok.getString() == "mips64") {
2425       return parseSetFeature(Mips::FeatureMips64);
2426   } else if (Tok.getString() == "mips64r2") {
2427       return parseSetFeature(Mips::FeatureMips64r2);
2428   } else if (Tok.getString() == "dsp") {
2429       return parseSetFeature(Mips::FeatureDSP);
2430   } else {
2431     // It is just an identifier, look for an assignment.
2432     parseSetAssignment();
2433     return false;
2434   }
2435
2436   return true;
2437 }
2438
2439 /// parseDataDirective
2440 ///  ::= .word [ expression (, expression)* ]
2441 bool MipsAsmParser::parseDataDirective(unsigned Size, SMLoc L) {
2442   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2443     for (;;) {
2444       const MCExpr *Value;
2445       if (getParser().parseExpression(Value))
2446         return true;
2447
2448       getParser().getStreamer().EmitValue(Value, Size);
2449
2450       if (getLexer().is(AsmToken::EndOfStatement))
2451         break;
2452
2453       // FIXME: Improve diagnostic.
2454       if (getLexer().isNot(AsmToken::Comma))
2455         return Error(L, "unexpected token in directive");
2456       Parser.Lex();
2457     }
2458   }
2459
2460   Parser.Lex();
2461   return false;
2462 }
2463
2464 /// parseDirectiveGpWord
2465 ///  ::= .gpword local_sym
2466 bool MipsAsmParser::parseDirectiveGpWord() {
2467   const MCExpr *Value;
2468   // EmitGPRel32Value requires an expression, so we are using base class
2469   // method to evaluate the expression.
2470   if (getParser().parseExpression(Value))
2471     return true;
2472   getParser().getStreamer().EmitGPRel32Value(Value);
2473
2474   if (getLexer().isNot(AsmToken::EndOfStatement))
2475     return Error(getLexer().getLoc(), "unexpected token in directive");
2476   Parser.Lex(); // Eat EndOfStatement token.
2477   return false;
2478 }
2479
2480 /// parseDirectiveGpDWord
2481 ///  ::= .gpdword local_sym
2482 bool MipsAsmParser::parseDirectiveGpDWord() {
2483   const MCExpr *Value;
2484   // EmitGPRel64Value requires an expression, so we are using base class
2485   // method to evaluate the expression.
2486   if (getParser().parseExpression(Value))
2487     return true;
2488   getParser().getStreamer().EmitGPRel64Value(Value);
2489
2490   if (getLexer().isNot(AsmToken::EndOfStatement))
2491     return Error(getLexer().getLoc(), "unexpected token in directive");
2492   Parser.Lex(); // Eat EndOfStatement token.
2493   return false;
2494 }
2495
2496 bool MipsAsmParser::parseDirectiveOption() {
2497   // Get the option token.
2498   AsmToken Tok = Parser.getTok();
2499   // At the moment only identifiers are supported.
2500   if (Tok.isNot(AsmToken::Identifier)) {
2501     Error(Parser.getTok().getLoc(), "unexpected token in .option directive");
2502     Parser.eatToEndOfStatement();
2503     return false;
2504   }
2505
2506   StringRef Option = Tok.getIdentifier();
2507
2508   if (Option == "pic0") {
2509     getTargetStreamer().emitDirectiveOptionPic0();
2510     Parser.Lex();
2511     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
2512       Error(Parser.getTok().getLoc(),
2513             "unexpected token in .option pic0 directive");
2514       Parser.eatToEndOfStatement();
2515     }
2516     return false;
2517   }
2518
2519   if (Option == "pic2") {
2520     getTargetStreamer().emitDirectiveOptionPic2();
2521     Parser.Lex();
2522     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
2523       Error(Parser.getTok().getLoc(),
2524             "unexpected token in .option pic2 directive");
2525       Parser.eatToEndOfStatement();
2526     }
2527     return false;
2528   }
2529
2530   // Unknown option.
2531   Warning(Parser.getTok().getLoc(), "unknown option in .option directive");
2532   Parser.eatToEndOfStatement();
2533   return false;
2534 }
2535
2536 bool MipsAsmParser::ParseDirective(AsmToken DirectiveID) {
2537   StringRef IDVal = DirectiveID.getString();
2538
2539   if (IDVal == ".dword") {
2540     parseDataDirective(8, DirectiveID.getLoc());
2541     return false;
2542   }
2543
2544   if (IDVal == ".ent") {
2545     // Ignore this directive for now.
2546     Parser.Lex();
2547     return false;
2548   }
2549
2550   if (IDVal == ".end") {
2551     // Ignore this directive for now.
2552     Parser.Lex();
2553     return false;
2554   }
2555
2556   if (IDVal == ".frame") {
2557     // Ignore this directive for now.
2558     Parser.eatToEndOfStatement();
2559     return false;
2560   }
2561
2562   if (IDVal == ".set") {
2563     return parseDirectiveSet();
2564   }
2565
2566   if (IDVal == ".fmask") {
2567     // Ignore this directive for now.
2568     Parser.eatToEndOfStatement();
2569     return false;
2570   }
2571
2572   if (IDVal == ".mask") {
2573     // Ignore this directive for now.
2574     Parser.eatToEndOfStatement();
2575     return false;
2576   }
2577
2578   if (IDVal == ".gpword") {
2579     parseDirectiveGpWord();
2580     return false;
2581   }
2582
2583   if (IDVal == ".gpdword") {
2584     parseDirectiveGpDWord();
2585     return false;
2586   }
2587
2588   if (IDVal == ".word") {
2589     parseDataDirective(4, DirectiveID.getLoc());
2590     return false;
2591   }
2592
2593   if (IDVal == ".option")
2594     return parseDirectiveOption();
2595
2596   if (IDVal == ".abicalls") {
2597     getTargetStreamer().emitDirectiveAbiCalls();
2598     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
2599       Error(Parser.getTok().getLoc(), "unexpected token in directive");
2600       // Clear line
2601       Parser.eatToEndOfStatement();
2602     }
2603     return false;
2604   }
2605
2606   if (IDVal == ".cpsetup")
2607     return parseDirectiveCPSetup();
2608
2609   return true;
2610 }
2611
2612 extern "C" void LLVMInitializeMipsAsmParser() {
2613   RegisterMCAsmParser<MipsAsmParser> X(TheMipsTarget);
2614   RegisterMCAsmParser<MipsAsmParser> Y(TheMipselTarget);
2615   RegisterMCAsmParser<MipsAsmParser> A(TheMips64Target);
2616   RegisterMCAsmParser<MipsAsmParser> B(TheMips64elTarget);
2617 }
2618
2619 #define GET_REGISTER_MATCHER
2620 #define GET_MATCHER_IMPLEMENTATION
2621 #include "MipsGenAsmMatcher.inc"