Silencing a -Wunused-variable warning in non-asserts builds; NFC.
[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/SmallVector.h"
16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCInstBuilder.h"
21 #include "llvm/MC/MCParser/MCAsmLexer.h"
22 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/MC/MCTargetAsmParser.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/MathExtras.h"
29 #include "llvm/Support/SourceMgr.h"
30 #include "llvm/Support/TargetRegistry.h"
31 #include <memory>
32
33 using namespace llvm;
34
35 #define DEBUG_TYPE "mips-asm-parser"
36
37 namespace llvm {
38 class MCInstrInfo;
39 }
40
41 namespace {
42 class MipsAssemblerOptions {
43 public:
44   MipsAssemblerOptions(uint64_t Features_) : 
45     ATReg(1), Reorder(true), Macro(true), Features(Features_) {}
46
47   MipsAssemblerOptions(const MipsAssemblerOptions *Opts) {
48     ATReg = Opts->getATRegNum();
49     Reorder = Opts->isReorder();
50     Macro = Opts->isMacro();
51     Features = Opts->getFeatures();
52   }
53
54   unsigned getATRegNum() const { return ATReg; }
55   bool setATReg(unsigned Reg);
56
57   bool isReorder() const { return Reorder; }
58   void setReorder() { Reorder = true; }
59   void setNoReorder() { Reorder = false; }
60
61   bool isMacro() const { return Macro; }
62   void setMacro() { Macro = true; }
63   void setNoMacro() { Macro = false; }
64
65   uint64_t getFeatures() const { return Features; }
66   void setFeatures(uint64_t Features_) { Features = Features_; }
67
68   // Set of features that are either architecture features or referenced
69   // by them (e.g.: FeatureNaN2008 implied by FeatureMips32r6).
70   // The full table can be found in MipsGenSubtargetInfo.inc (MipsFeatureKV[]).
71   // The reason we need this mask is explained in the selectArch function.
72   // FIXME: Ideally we would like TableGen to generate this information.
73   static const uint64_t AllArchRelatedMask =
74       Mips::FeatureMips1 | Mips::FeatureMips2 | Mips::FeatureMips3 |
75       Mips::FeatureMips3_32 | Mips::FeatureMips3_32r2 | Mips::FeatureMips4 |
76       Mips::FeatureMips4_32 | Mips::FeatureMips4_32r2 | Mips::FeatureMips5 |
77       Mips::FeatureMips5_32r2 | Mips::FeatureMips32 | Mips::FeatureMips32r2 |
78       Mips::FeatureMips32r6 | Mips::FeatureMips64 | Mips::FeatureMips64r2 |
79       Mips::FeatureMips64r6 | Mips::FeatureCnMips | Mips::FeatureFP64Bit |
80       Mips::FeatureGP64Bit | Mips::FeatureNaN2008;
81
82 private:
83   unsigned ATReg;
84   bool Reorder;
85   bool Macro;
86   uint64_t Features;
87 };
88 }
89
90 namespace {
91 class MipsAsmParser : public MCTargetAsmParser {
92   MipsTargetStreamer &getTargetStreamer() {
93     MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
94     return static_cast<MipsTargetStreamer &>(TS);
95   }
96
97   MCSubtargetInfo &STI;
98   SmallVector<std::unique_ptr<MipsAssemblerOptions>, 2> AssemblerOptions;
99   MCSymbol *CurrentFn; // Pointer to the function being parsed. It may be a
100                        // nullptr, which indicates that no function is currently
101                        // selected. This usually happens after an '.end func'
102                        // directive.
103
104   // Print a warning along with its fix-it message at the given range.
105   void printWarningWithFixIt(const Twine &Msg, const Twine &FixMsg,
106                              SMRange Range, bool ShowColors = true);
107
108 #define GET_ASSEMBLER_HEADER
109 #include "MipsGenAsmMatcher.inc"
110
111   unsigned checkTargetMatchPredicate(MCInst &Inst) override;
112
113   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
114                                OperandVector &Operands, MCStreamer &Out,
115                                uint64_t &ErrorInfo,
116                                bool MatchingInlineAsm) override;
117
118   /// Parse a register as used in CFI directives
119   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
120
121   bool parseParenSuffix(StringRef Name, OperandVector &Operands);
122
123   bool parseBracketSuffix(StringRef Name, OperandVector &Operands);
124
125   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
126                         SMLoc NameLoc, OperandVector &Operands) override;
127
128   bool ParseDirective(AsmToken DirectiveID) override;
129
130   MipsAsmParser::OperandMatchResultTy parseMemOperand(OperandVector &Operands);
131
132   MipsAsmParser::OperandMatchResultTy
133   matchAnyRegisterNameWithoutDollar(OperandVector &Operands,
134                                     StringRef Identifier, SMLoc S);
135
136   MipsAsmParser::OperandMatchResultTy
137   matchAnyRegisterWithoutDollar(OperandVector &Operands, SMLoc S);
138
139   MipsAsmParser::OperandMatchResultTy parseAnyRegister(OperandVector &Operands);
140
141   MipsAsmParser::OperandMatchResultTy parseImm(OperandVector &Operands);
142
143   MipsAsmParser::OperandMatchResultTy parseJumpTarget(OperandVector &Operands);
144
145   MipsAsmParser::OperandMatchResultTy parseInvNum(OperandVector &Operands);
146
147   MipsAsmParser::OperandMatchResultTy parseLSAImm(OperandVector &Operands);
148
149   MipsAsmParser::OperandMatchResultTy
150   parseRegisterPair (OperandVector &Operands);
151
152   MipsAsmParser::OperandMatchResultTy
153   parseRegisterList (OperandVector  &Operands);
154
155   bool searchSymbolAlias(OperandVector &Operands);
156
157   bool parseOperand(OperandVector &, StringRef Mnemonic);
158
159   bool needsExpansion(MCInst &Inst);
160
161   // Expands assembly pseudo instructions.
162   // Returns false on success, true otherwise.
163   bool expandInstruction(MCInst &Inst, SMLoc IDLoc,
164                          SmallVectorImpl<MCInst> &Instructions);
165
166   bool expandLoadImm(MCInst &Inst, SMLoc IDLoc,
167                      SmallVectorImpl<MCInst> &Instructions);
168
169   bool expandLoadAddressImm(MCInst &Inst, SMLoc IDLoc,
170                             SmallVectorImpl<MCInst> &Instructions);
171
172   bool expandLoadAddressReg(MCInst &Inst, SMLoc IDLoc,
173                             SmallVectorImpl<MCInst> &Instructions);
174   bool expandUncondBranchMMPseudo(MCInst &Inst, SMLoc IDLoc,
175                                   SmallVectorImpl<MCInst> &Instructions);
176
177   void expandLoadAddressSym(MCInst &Inst, SMLoc IDLoc,
178                             SmallVectorImpl<MCInst> &Instructions);
179
180   void expandMemInst(MCInst &Inst, SMLoc IDLoc,
181                      SmallVectorImpl<MCInst> &Instructions, bool isLoad,
182                      bool isImmOpnd);
183   bool reportParseError(Twine ErrorMsg);
184   bool reportParseError(SMLoc Loc, Twine ErrorMsg);
185
186   bool parseMemOffset(const MCExpr *&Res, bool isParenExpr);
187   bool parseRelocOperand(const MCExpr *&Res);
188
189   const MCExpr *evaluateRelocExpr(const MCExpr *Expr, StringRef RelocStr);
190
191   bool isEvaluated(const MCExpr *Expr);
192   bool parseSetMips0Directive();
193   bool parseSetArchDirective();
194   bool parseSetFeature(uint64_t Feature);
195   bool parseDirectiveCpLoad(SMLoc Loc);
196   bool parseDirectiveCPSetup();
197   bool parseDirectiveNaN();
198   bool parseDirectiveSet();
199   bool parseDirectiveOption();
200
201   bool parseSetAtDirective();
202   bool parseSetNoAtDirective();
203   bool parseSetMacroDirective();
204   bool parseSetNoMacroDirective();
205   bool parseSetMsaDirective();
206   bool parseSetNoMsaDirective();
207   bool parseSetNoDspDirective();
208   bool parseSetReorderDirective();
209   bool parseSetNoReorderDirective();
210   bool parseSetMips16Directive();
211   bool parseSetNoMips16Directive();
212   bool parseSetFpDirective();
213   bool parseSetPopDirective();
214   bool parseSetPushDirective();
215
216   bool parseSetAssignment();
217
218   bool parseDataDirective(unsigned Size, SMLoc L);
219   bool parseDirectiveGpWord();
220   bool parseDirectiveGpDWord();
221   bool parseDirectiveModule();
222   bool parseDirectiveModuleFP();
223   bool parseFpABIValue(MipsABIFlagsSection::FpABIKind &FpABI,
224                        StringRef Directive);
225
226   MCSymbolRefExpr::VariantKind getVariantKind(StringRef Symbol);
227
228   bool eatComma(StringRef ErrorStr);
229
230   int matchCPURegisterName(StringRef Symbol);
231
232   int matchHWRegsRegisterName(StringRef Symbol);
233
234   int matchRegisterByNumber(unsigned RegNum, unsigned RegClass);
235
236   int matchFPURegisterName(StringRef Name);
237
238   int matchFCCRegisterName(StringRef Name);
239
240   int matchACRegisterName(StringRef Name);
241
242   int matchMSA128RegisterName(StringRef Name);
243
244   int matchMSA128CtrlRegisterName(StringRef Name);
245
246   unsigned getReg(int RC, int RegNo);
247
248   unsigned getGPR(int RegNo);
249
250   int getATReg(SMLoc Loc);
251
252   bool processInstruction(MCInst &Inst, SMLoc IDLoc,
253                           SmallVectorImpl<MCInst> &Instructions);
254
255   // Helper function that checks if the value of a vector index is within the
256   // boundaries of accepted values for each RegisterKind
257   // Example: INSERT.B $w0[n], $1 => 16 > n >= 0
258   bool validateMSAIndex(int Val, int RegKind);
259
260   // Selects a new architecture by updating the FeatureBits with the necessary
261   // info including implied dependencies.
262   // Internally, it clears all the feature bits related to *any* architecture
263   // and selects the new one using the ToggleFeature functionality of the
264   // MCSubtargetInfo object that handles implied dependencies. The reason we
265   // clear all the arch related bits manually is because ToggleFeature only
266   // clears the features that imply the feature being cleared and not the
267   // features implied by the feature being cleared. This is easier to see
268   // with an example:
269   //  --------------------------------------------------
270   // | Feature         | Implies                        |
271   // | -------------------------------------------------|
272   // | FeatureMips1    | None                           |
273   // | FeatureMips2    | FeatureMips1                   |
274   // | FeatureMips3    | FeatureMips2 | FeatureMipsGP64 |
275   // | FeatureMips4    | FeatureMips3                   |
276   // | ...             |                                |
277   //  --------------------------------------------------
278   //
279   // Setting Mips3 is equivalent to set: (FeatureMips3 | FeatureMips2 |
280   // FeatureMipsGP64 | FeatureMips1)
281   // Clearing Mips3 is equivalent to clear (FeatureMips3 | FeatureMips4).
282   void selectArch(StringRef ArchFeature) {
283     uint64_t FeatureBits = STI.getFeatureBits();
284     FeatureBits &= ~MipsAssemblerOptions::AllArchRelatedMask;
285     STI.setFeatureBits(FeatureBits);
286     setAvailableFeatures(
287         ComputeAvailableFeatures(STI.ToggleFeature(ArchFeature)));
288     AssemblerOptions.back()->setFeatures(getAvailableFeatures());
289   }
290
291   void setFeatureBits(uint64_t Feature, StringRef FeatureString) {
292     if (!(STI.getFeatureBits() & Feature)) {
293       setAvailableFeatures(
294           ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
295     }
296     AssemblerOptions.back()->setFeatures(getAvailableFeatures());
297   }
298
299   void clearFeatureBits(uint64_t Feature, StringRef FeatureString) {
300     if (STI.getFeatureBits() & Feature) {
301       setAvailableFeatures(
302           ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
303     }
304     AssemblerOptions.back()->setFeatures(getAvailableFeatures());
305   }
306
307 public:
308   enum MipsMatchResultTy {
309     Match_RequiresDifferentSrcAndDst = FIRST_TARGET_MATCH_RESULT_TY
310 #define GET_OPERAND_DIAGNOSTIC_TYPES
311 #include "MipsGenAsmMatcher.inc"
312 #undef GET_OPERAND_DIAGNOSTIC_TYPES
313
314   };
315
316   MipsAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
317                 const MCInstrInfo &MII, const MCTargetOptions &Options)
318       : MCTargetAsmParser(), STI(sti) {
319     MCAsmParserExtension::Initialize(parser);
320
321     // Initialize the set of available features.
322     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
323     
324     // Remember the initial assembler options. The user can not modify these.
325     AssemblerOptions.push_back(
326                      make_unique<MipsAssemblerOptions>(getAvailableFeatures()));
327     
328     // Create an assembler options environment for the user to modify.
329     AssemblerOptions.push_back(
330                      make_unique<MipsAssemblerOptions>(getAvailableFeatures()));
331
332     getTargetStreamer().updateABIInfo(*this);
333
334     // Assert exactly one ABI was chosen.
335     assert((((STI.getFeatureBits() & Mips::FeatureO32) != 0) +
336             ((STI.getFeatureBits() & Mips::FeatureEABI) != 0) +
337             ((STI.getFeatureBits() & Mips::FeatureN32) != 0) +
338             ((STI.getFeatureBits() & Mips::FeatureN64) != 0)) == 1);
339
340     if (!isABI_O32() && !useOddSPReg() != 0)
341       report_fatal_error("-mno-odd-spreg requires the O32 ABI");
342
343     CurrentFn = nullptr;
344   }
345
346   /// True if all of $fcc0 - $fcc7 exist for the current ISA.
347   bool hasEightFccRegisters() const { return hasMips4() || hasMips32(); }
348
349   bool isGP64bit() const { return STI.getFeatureBits() & Mips::FeatureGP64Bit; }
350   bool isFP64bit() const { return STI.getFeatureBits() & Mips::FeatureFP64Bit; }
351   bool isABI_N32() const { return STI.getFeatureBits() & Mips::FeatureN32; }
352   bool isABI_N64() const { return STI.getFeatureBits() & Mips::FeatureN64; }
353   bool isABI_O32() const { return STI.getFeatureBits() & Mips::FeatureO32; }
354   bool isABI_FPXX() const { return STI.getFeatureBits() & Mips::FeatureFPXX; }
355
356   bool useOddSPReg() const {
357     return !(STI.getFeatureBits() & Mips::FeatureNoOddSPReg);
358   }
359
360   bool inMicroMipsMode() const {
361     return STI.getFeatureBits() & Mips::FeatureMicroMips;
362   }
363   bool hasMips1() const { return STI.getFeatureBits() & Mips::FeatureMips1; }
364   bool hasMips2() const { return STI.getFeatureBits() & Mips::FeatureMips2; }
365   bool hasMips3() const { return STI.getFeatureBits() & Mips::FeatureMips3; }
366   bool hasMips4() const { return STI.getFeatureBits() & Mips::FeatureMips4; }
367   bool hasMips5() const { return STI.getFeatureBits() & Mips::FeatureMips5; }
368   bool hasMips32() const {
369     return (STI.getFeatureBits() & Mips::FeatureMips32);
370   }
371   bool hasMips64() const {
372     return (STI.getFeatureBits() & Mips::FeatureMips64);
373   }
374   bool hasMips32r2() const {
375     return (STI.getFeatureBits() & Mips::FeatureMips32r2);
376   }
377   bool hasMips64r2() const {
378     return (STI.getFeatureBits() & Mips::FeatureMips64r2);
379   }
380   bool hasMips32r6() const {
381     return (STI.getFeatureBits() & Mips::FeatureMips32r6);
382   }
383   bool hasMips64r6() const {
384     return (STI.getFeatureBits() & Mips::FeatureMips64r6);
385   }
386   bool hasDSP() const { return (STI.getFeatureBits() & Mips::FeatureDSP); }
387   bool hasDSPR2() const { return (STI.getFeatureBits() & Mips::FeatureDSPR2); }
388   bool hasMSA() const { return (STI.getFeatureBits() & Mips::FeatureMSA); }
389
390   bool inMips16Mode() const {
391     return STI.getFeatureBits() & Mips::FeatureMips16;
392   }
393   // TODO: see how can we get this info.
394   bool abiUsesSoftFloat() const { return false; }
395
396   /// Warn if RegNo is the current assembler temporary.
397   void warnIfAssemblerTemporary(int RegNo, SMLoc Loc);
398 };
399 }
400
401 namespace {
402
403 /// MipsOperand - Instances of this class represent a parsed Mips machine
404 /// instruction.
405 class MipsOperand : public MCParsedAsmOperand {
406 public:
407   /// Broad categories of register classes
408   /// The exact class is finalized by the render method.
409   enum RegKind {
410     RegKind_GPR = 1,      /// GPR32 and GPR64 (depending on isGP64bit())
411     RegKind_FGR = 2,      /// FGR32, FGR64, AFGR64 (depending on context and
412                           /// isFP64bit())
413     RegKind_FCC = 4,      /// FCC
414     RegKind_MSA128 = 8,   /// MSA128[BHWD] (makes no difference which)
415     RegKind_MSACtrl = 16, /// MSA control registers
416     RegKind_COP2 = 32,    /// COP2
417     RegKind_ACC = 64,     /// HI32DSP, LO32DSP, and ACC64DSP (depending on
418                           /// context).
419     RegKind_CCR = 128,    /// CCR
420     RegKind_HWRegs = 256, /// HWRegs
421     RegKind_COP3 = 512,   /// COP3
422
423     /// Potentially any (e.g. $1)
424     RegKind_Numeric = RegKind_GPR | RegKind_FGR | RegKind_FCC | RegKind_MSA128 |
425                       RegKind_MSACtrl | RegKind_COP2 | RegKind_ACC |
426                       RegKind_CCR | RegKind_HWRegs | RegKind_COP3
427   };
428
429 private:
430   enum KindTy {
431     k_Immediate,     /// An immediate (possibly involving symbol references)
432     k_Memory,        /// Base + Offset Memory Address
433     k_PhysRegister,  /// A physical register from the Mips namespace
434     k_RegisterIndex, /// A register index in one or more RegKind.
435     k_Token,         /// A simple token
436     k_RegList,       /// A physical register list
437     k_RegPair        /// A pair of physical register
438   } Kind;
439
440 public:
441   MipsOperand(KindTy K, MipsAsmParser &Parser)
442       : MCParsedAsmOperand(), Kind(K), AsmParser(Parser) {}
443
444 private:
445   /// For diagnostics, and checking the assembler temporary
446   MipsAsmParser &AsmParser;
447
448   struct Token {
449     const char *Data;
450     unsigned Length;
451   };
452
453   struct PhysRegOp {
454     unsigned Num; /// Register Number
455   };
456
457   struct RegIdxOp {
458     unsigned Index; /// Index into the register class
459     RegKind Kind;   /// Bitfield of the kinds it could possibly be
460     const MCRegisterInfo *RegInfo;
461   };
462
463   struct ImmOp {
464     const MCExpr *Val;
465   };
466
467   struct MemOp {
468     MipsOperand *Base;
469     const MCExpr *Off;
470   };
471
472   struct RegListOp {
473     SmallVector<unsigned, 10> *List;
474   };
475
476   union {
477     struct Token Tok;
478     struct PhysRegOp PhysReg;
479     struct RegIdxOp RegIdx;
480     struct ImmOp Imm;
481     struct MemOp Mem;
482     struct RegListOp RegList;
483   };
484
485   SMLoc StartLoc, EndLoc;
486
487   /// Internal constructor for register kinds
488   static std::unique_ptr<MipsOperand> CreateReg(unsigned Index, RegKind RegKind,
489                                                 const MCRegisterInfo *RegInfo,
490                                                 SMLoc S, SMLoc E,
491                                                 MipsAsmParser &Parser) {
492     auto Op = make_unique<MipsOperand>(k_RegisterIndex, Parser);
493     Op->RegIdx.Index = Index;
494     Op->RegIdx.RegInfo = RegInfo;
495     Op->RegIdx.Kind = RegKind;
496     Op->StartLoc = S;
497     Op->EndLoc = E;
498     return Op;
499   }
500
501 public:
502   /// Coerce the register to GPR32 and return the real register for the current
503   /// target.
504   unsigned getGPR32Reg() const {
505     assert(isRegIdx() && (RegIdx.Kind & RegKind_GPR) && "Invalid access!");
506     AsmParser.warnIfAssemblerTemporary(RegIdx.Index, StartLoc);
507     unsigned ClassID = Mips::GPR32RegClassID;
508     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
509   }
510
511   /// Coerce the register to GPR32 and return the real register for the current
512   /// target.
513   unsigned getGPRMM16Reg() const {
514     assert(isRegIdx() && (RegIdx.Kind & RegKind_GPR) && "Invalid access!");
515     unsigned ClassID = Mips::GPR32RegClassID;
516     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
517   }
518
519   /// Coerce the register to GPR64 and return the real register for the current
520   /// target.
521   unsigned getGPR64Reg() const {
522     assert(isRegIdx() && (RegIdx.Kind & RegKind_GPR) && "Invalid access!");
523     unsigned ClassID = Mips::GPR64RegClassID;
524     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
525   }
526
527 private:
528   /// Coerce the register to AFGR64 and return the real register for the current
529   /// target.
530   unsigned getAFGR64Reg() const {
531     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
532     if (RegIdx.Index % 2 != 0)
533       AsmParser.Warning(StartLoc, "Float register should be even.");
534     return RegIdx.RegInfo->getRegClass(Mips::AFGR64RegClassID)
535         .getRegister(RegIdx.Index / 2);
536   }
537
538   /// Coerce the register to FGR64 and return the real register for the current
539   /// target.
540   unsigned getFGR64Reg() const {
541     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
542     return RegIdx.RegInfo->getRegClass(Mips::FGR64RegClassID)
543         .getRegister(RegIdx.Index);
544   }
545
546   /// Coerce the register to FGR32 and return the real register for the current
547   /// target.
548   unsigned getFGR32Reg() const {
549     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
550     return RegIdx.RegInfo->getRegClass(Mips::FGR32RegClassID)
551         .getRegister(RegIdx.Index);
552   }
553
554   /// Coerce the register to FGRH32 and return the real register for the current
555   /// target.
556   unsigned getFGRH32Reg() const {
557     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
558     return RegIdx.RegInfo->getRegClass(Mips::FGRH32RegClassID)
559         .getRegister(RegIdx.Index);
560   }
561
562   /// Coerce the register to FCC and return the real register for the current
563   /// target.
564   unsigned getFCCReg() const {
565     assert(isRegIdx() && (RegIdx.Kind & RegKind_FCC) && "Invalid access!");
566     return RegIdx.RegInfo->getRegClass(Mips::FCCRegClassID)
567         .getRegister(RegIdx.Index);
568   }
569
570   /// Coerce the register to MSA128 and return the real register for the current
571   /// target.
572   unsigned getMSA128Reg() const {
573     assert(isRegIdx() && (RegIdx.Kind & RegKind_MSA128) && "Invalid access!");
574     // It doesn't matter which of the MSA128[BHWD] classes we use. They are all
575     // identical
576     unsigned ClassID = Mips::MSA128BRegClassID;
577     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
578   }
579
580   /// Coerce the register to MSACtrl and return the real register for the
581   /// current target.
582   unsigned getMSACtrlReg() const {
583     assert(isRegIdx() && (RegIdx.Kind & RegKind_MSACtrl) && "Invalid access!");
584     unsigned ClassID = Mips::MSACtrlRegClassID;
585     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
586   }
587
588   /// Coerce the register to COP2 and return the real register for the
589   /// current target.
590   unsigned getCOP2Reg() const {
591     assert(isRegIdx() && (RegIdx.Kind & RegKind_COP2) && "Invalid access!");
592     unsigned ClassID = Mips::COP2RegClassID;
593     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
594   }
595
596   /// Coerce the register to COP3 and return the real register for the
597   /// current target.
598   unsigned getCOP3Reg() const {
599     assert(isRegIdx() && (RegIdx.Kind & RegKind_COP3) && "Invalid access!");
600     unsigned ClassID = Mips::COP3RegClassID;
601     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
602   }
603
604   /// Coerce the register to ACC64DSP and return the real register for the
605   /// current target.
606   unsigned getACC64DSPReg() const {
607     assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!");
608     unsigned ClassID = Mips::ACC64DSPRegClassID;
609     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
610   }
611
612   /// Coerce the register to HI32DSP and return the real register for the
613   /// current target.
614   unsigned getHI32DSPReg() const {
615     assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!");
616     unsigned ClassID = Mips::HI32DSPRegClassID;
617     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
618   }
619
620   /// Coerce the register to LO32DSP and return the real register for the
621   /// current target.
622   unsigned getLO32DSPReg() const {
623     assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!");
624     unsigned ClassID = Mips::LO32DSPRegClassID;
625     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
626   }
627
628   /// Coerce the register to CCR and return the real register for the
629   /// current target.
630   unsigned getCCRReg() const {
631     assert(isRegIdx() && (RegIdx.Kind & RegKind_CCR) && "Invalid access!");
632     unsigned ClassID = Mips::CCRRegClassID;
633     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
634   }
635
636   /// Coerce the register to HWRegs and return the real register for the
637   /// current target.
638   unsigned getHWRegsReg() const {
639     assert(isRegIdx() && (RegIdx.Kind & RegKind_HWRegs) && "Invalid access!");
640     unsigned ClassID = Mips::HWRegsRegClassID;
641     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
642   }
643
644 public:
645   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
646     // Add as immediate when possible.  Null MCExpr = 0.
647     if (!Expr)
648       Inst.addOperand(MCOperand::CreateImm(0));
649     else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
650       Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
651     else
652       Inst.addOperand(MCOperand::CreateExpr(Expr));
653   }
654
655   void addRegOperands(MCInst &Inst, unsigned N) const {
656     llvm_unreachable("Use a custom parser instead");
657   }
658
659   /// Render the operand to an MCInst as a GPR32
660   /// Asserts if the wrong number of operands are requested, or the operand
661   /// is not a k_RegisterIndex compatible with RegKind_GPR
662   void addGPR32AsmRegOperands(MCInst &Inst, unsigned N) const {
663     assert(N == 1 && "Invalid number of operands!");
664     Inst.addOperand(MCOperand::CreateReg(getGPR32Reg()));
665   }
666
667   void addGPRMM16AsmRegOperands(MCInst &Inst, unsigned N) const {
668     assert(N == 1 && "Invalid number of operands!");
669     Inst.addOperand(MCOperand::CreateReg(getGPRMM16Reg()));
670   }
671
672   void addGPRMM16AsmRegZeroOperands(MCInst &Inst, unsigned N) const {
673     assert(N == 1 && "Invalid number of operands!");
674     Inst.addOperand(MCOperand::CreateReg(getGPRMM16Reg()));
675   }
676
677   /// Render the operand to an MCInst as a GPR64
678   /// Asserts if the wrong number of operands are requested, or the operand
679   /// is not a k_RegisterIndex compatible with RegKind_GPR
680   void addGPR64AsmRegOperands(MCInst &Inst, unsigned N) const {
681     assert(N == 1 && "Invalid number of operands!");
682     Inst.addOperand(MCOperand::CreateReg(getGPR64Reg()));
683   }
684
685   void addAFGR64AsmRegOperands(MCInst &Inst, unsigned N) const {
686     assert(N == 1 && "Invalid number of operands!");
687     Inst.addOperand(MCOperand::CreateReg(getAFGR64Reg()));
688   }
689
690   void addFGR64AsmRegOperands(MCInst &Inst, unsigned N) const {
691     assert(N == 1 && "Invalid number of operands!");
692     Inst.addOperand(MCOperand::CreateReg(getFGR64Reg()));
693   }
694
695   void addFGR32AsmRegOperands(MCInst &Inst, unsigned N) const {
696     assert(N == 1 && "Invalid number of operands!");
697     Inst.addOperand(MCOperand::CreateReg(getFGR32Reg()));
698     // FIXME: We ought to do this for -integrated-as without -via-file-asm too.
699     if (!AsmParser.useOddSPReg() && RegIdx.Index & 1)
700       AsmParser.Error(StartLoc, "-mno-odd-spreg prohibits the use of odd FPU "
701                                 "registers");
702   }
703
704   void addFGRH32AsmRegOperands(MCInst &Inst, unsigned N) const {
705     assert(N == 1 && "Invalid number of operands!");
706     Inst.addOperand(MCOperand::CreateReg(getFGRH32Reg()));
707   }
708
709   void addFCCAsmRegOperands(MCInst &Inst, unsigned N) const {
710     assert(N == 1 && "Invalid number of operands!");
711     Inst.addOperand(MCOperand::CreateReg(getFCCReg()));
712   }
713
714   void addMSA128AsmRegOperands(MCInst &Inst, unsigned N) const {
715     assert(N == 1 && "Invalid number of operands!");
716     Inst.addOperand(MCOperand::CreateReg(getMSA128Reg()));
717   }
718
719   void addMSACtrlAsmRegOperands(MCInst &Inst, unsigned N) const {
720     assert(N == 1 && "Invalid number of operands!");
721     Inst.addOperand(MCOperand::CreateReg(getMSACtrlReg()));
722   }
723
724   void addCOP2AsmRegOperands(MCInst &Inst, unsigned N) const {
725     assert(N == 1 && "Invalid number of operands!");
726     Inst.addOperand(MCOperand::CreateReg(getCOP2Reg()));
727   }
728
729   void addCOP3AsmRegOperands(MCInst &Inst, unsigned N) const {
730     assert(N == 1 && "Invalid number of operands!");
731     Inst.addOperand(MCOperand::CreateReg(getCOP3Reg()));
732   }
733
734   void addACC64DSPAsmRegOperands(MCInst &Inst, unsigned N) const {
735     assert(N == 1 && "Invalid number of operands!");
736     Inst.addOperand(MCOperand::CreateReg(getACC64DSPReg()));
737   }
738
739   void addHI32DSPAsmRegOperands(MCInst &Inst, unsigned N) const {
740     assert(N == 1 && "Invalid number of operands!");
741     Inst.addOperand(MCOperand::CreateReg(getHI32DSPReg()));
742   }
743
744   void addLO32DSPAsmRegOperands(MCInst &Inst, unsigned N) const {
745     assert(N == 1 && "Invalid number of operands!");
746     Inst.addOperand(MCOperand::CreateReg(getLO32DSPReg()));
747   }
748
749   void addCCRAsmRegOperands(MCInst &Inst, unsigned N) const {
750     assert(N == 1 && "Invalid number of operands!");
751     Inst.addOperand(MCOperand::CreateReg(getCCRReg()));
752   }
753
754   void addHWRegsAsmRegOperands(MCInst &Inst, unsigned N) const {
755     assert(N == 1 && "Invalid number of operands!");
756     Inst.addOperand(MCOperand::CreateReg(getHWRegsReg()));
757   }
758
759   void addImmOperands(MCInst &Inst, unsigned N) const {
760     assert(N == 1 && "Invalid number of operands!");
761     const MCExpr *Expr = getImm();
762     addExpr(Inst, Expr);
763   }
764
765   void addMemOperands(MCInst &Inst, unsigned N) const {
766     assert(N == 2 && "Invalid number of operands!");
767
768     Inst.addOperand(MCOperand::CreateReg(getMemBase()->getGPR32Reg()));
769
770     const MCExpr *Expr = getMemOff();
771     addExpr(Inst, Expr);
772   }
773
774   void addMicroMipsMemOperands(MCInst &Inst, unsigned N) const {
775     assert(N == 2 && "Invalid number of operands!");
776
777     Inst.addOperand(MCOperand::CreateReg(getMemBase()->getGPRMM16Reg()));
778
779     const MCExpr *Expr = getMemOff();
780     addExpr(Inst, Expr);
781   }
782
783   void addRegListOperands(MCInst &Inst, unsigned N) const {
784     assert(N == 1 && "Invalid number of operands!");
785
786     for (auto RegNo : getRegList())
787       Inst.addOperand(MCOperand::CreateReg(RegNo));
788   }
789
790   void addRegPairOperands(MCInst &Inst, unsigned N) const {
791     assert(N == 2 && "Invalid number of operands!");
792     unsigned RegNo = getRegPair();
793     Inst.addOperand(MCOperand::CreateReg(RegNo++));
794     Inst.addOperand(MCOperand::CreateReg(RegNo));
795   }
796
797   bool isReg() const override {
798     // As a special case until we sort out the definition of div/divu, pretend
799     // that $0/$zero are k_PhysRegister so that MCK_ZERO works correctly.
800     if (isGPRAsmReg() && RegIdx.Index == 0)
801       return true;
802
803     return Kind == k_PhysRegister;
804   }
805   bool isRegIdx() const { return Kind == k_RegisterIndex; }
806   bool isImm() const override { return Kind == k_Immediate; }
807   bool isConstantImm() const {
808     return isImm() && dyn_cast<MCConstantExpr>(getImm());
809   }
810   bool isToken() const override {
811     // Note: It's not possible to pretend that other operand kinds are tokens.
812     // The matcher emitter checks tokens first.
813     return Kind == k_Token;
814   }
815   bool isMem() const override { return Kind == k_Memory; }
816   bool isConstantMemOff() const {
817     return isMem() && dyn_cast<MCConstantExpr>(getMemOff());
818   }
819   template <unsigned Bits> bool isMemWithSimmOffset() const {
820     return isMem() && isConstantMemOff() && isInt<Bits>(getConstantMemOff());
821   }
822   bool isMemWithGRPMM16Base() const {
823     return isMem() && getMemBase()->isMM16AsmReg();
824   }
825   template <unsigned Bits> bool isMemWithUimmOffsetSP() const {
826     return isMem() && isConstantMemOff() && isUInt<Bits>(getConstantMemOff())
827       && getMemBase()->isRegIdx() && (getMemBase()->getGPR32Reg() == Mips::SP);
828   }
829   template <unsigned Bits> bool isMemWithUimmWordAlignedOffsetSP() const {
830     return isMem() && isConstantMemOff() && isUInt<Bits>(getConstantMemOff())
831       && (getConstantMemOff() % 4 == 0) && getMemBase()->isRegIdx()
832       && (getMemBase()->getGPR32Reg() == Mips::SP);
833   }
834   bool isRegList16() const {
835     if (!isRegList())
836       return false;
837
838     int Size = RegList.List->size();
839     if (Size < 2 || Size > 5 || *RegList.List->begin() != Mips::S0 ||
840         RegList.List->back() != Mips::RA)
841       return false;
842
843     int PrevReg = *RegList.List->begin();
844     for (int i = 1; i < Size - 1; i++) {
845       int Reg = (*(RegList.List))[i];
846       if ( Reg != PrevReg + 1)
847         return false;
848       PrevReg = Reg;
849     }
850
851     return true;
852   }
853   bool isInvNum() const { return Kind == k_Immediate; }
854   bool isLSAImm() const {
855     if (!isConstantImm())
856       return false;
857     int64_t Val = getConstantImm();
858     return 1 <= Val && Val <= 4;
859   }
860   bool isRegList() const { return Kind == k_RegList; }
861
862   StringRef getToken() const {
863     assert(Kind == k_Token && "Invalid access!");
864     return StringRef(Tok.Data, Tok.Length);
865   }
866   bool isRegPair() const { return Kind == k_RegPair; }
867
868   unsigned getReg() const override {
869     // As a special case until we sort out the definition of div/divu, pretend
870     // that $0/$zero are k_PhysRegister so that MCK_ZERO works correctly.
871     if (Kind == k_RegisterIndex && RegIdx.Index == 0 &&
872         RegIdx.Kind & RegKind_GPR)
873       return getGPR32Reg(); // FIXME: GPR64 too
874
875     assert(Kind == k_PhysRegister && "Invalid access!");
876     return PhysReg.Num;
877   }
878
879   const MCExpr *getImm() const {
880     assert((Kind == k_Immediate) && "Invalid access!");
881     return Imm.Val;
882   }
883
884   int64_t getConstantImm() const {
885     const MCExpr *Val = getImm();
886     return static_cast<const MCConstantExpr *>(Val)->getValue();
887   }
888
889   MipsOperand *getMemBase() const {
890     assert((Kind == k_Memory) && "Invalid access!");
891     return Mem.Base;
892   }
893
894   const MCExpr *getMemOff() const {
895     assert((Kind == k_Memory) && "Invalid access!");
896     return Mem.Off;
897   }
898
899   int64_t getConstantMemOff() const {
900     return static_cast<const MCConstantExpr *>(getMemOff())->getValue();
901   }
902
903   const SmallVectorImpl<unsigned> &getRegList() const {
904     assert((Kind == k_RegList) && "Invalid access!");
905     return *(RegList.List);
906   }
907
908   unsigned getRegPair() const {
909     assert((Kind == k_RegPair) && "Invalid access!");
910     return RegIdx.Index;
911   }
912
913   static std::unique_ptr<MipsOperand> CreateToken(StringRef Str, SMLoc S,
914                                                   MipsAsmParser &Parser) {
915     auto Op = make_unique<MipsOperand>(k_Token, Parser);
916     Op->Tok.Data = Str.data();
917     Op->Tok.Length = Str.size();
918     Op->StartLoc = S;
919     Op->EndLoc = S;
920     return Op;
921   }
922
923   /// Create a numeric register (e.g. $1). The exact register remains
924   /// unresolved until an instruction successfully matches
925   static std::unique_ptr<MipsOperand>
926   createNumericReg(unsigned Index, const MCRegisterInfo *RegInfo, SMLoc S,
927                    SMLoc E, MipsAsmParser &Parser) {
928     DEBUG(dbgs() << "createNumericReg(" << Index << ", ...)\n");
929     return CreateReg(Index, RegKind_Numeric, RegInfo, S, E, Parser);
930   }
931
932   /// Create a register that is definitely a GPR.
933   /// This is typically only used for named registers such as $gp.
934   static std::unique_ptr<MipsOperand>
935   createGPRReg(unsigned Index, const MCRegisterInfo *RegInfo, SMLoc S, SMLoc E,
936                MipsAsmParser &Parser) {
937     return CreateReg(Index, RegKind_GPR, RegInfo, S, E, Parser);
938   }
939
940   /// Create a register that is definitely a FGR.
941   /// This is typically only used for named registers such as $f0.
942   static std::unique_ptr<MipsOperand>
943   createFGRReg(unsigned Index, const MCRegisterInfo *RegInfo, SMLoc S, SMLoc E,
944                MipsAsmParser &Parser) {
945     return CreateReg(Index, RegKind_FGR, RegInfo, S, E, Parser);
946   }
947
948   /// Create a register that is definitely a HWReg.
949   /// This is typically only used for named registers such as $hwr_cpunum.
950   static std::unique_ptr<MipsOperand>
951   createHWRegsReg(unsigned Index, const MCRegisterInfo *RegInfo,
952                   SMLoc S, SMLoc E, MipsAsmParser &Parser) {
953     return CreateReg(Index, RegKind_HWRegs, RegInfo, S, E, Parser);
954   }
955
956   /// Create a register that is definitely an FCC.
957   /// This is typically only used for named registers such as $fcc0.
958   static std::unique_ptr<MipsOperand>
959   createFCCReg(unsigned Index, const MCRegisterInfo *RegInfo, SMLoc S, SMLoc E,
960                MipsAsmParser &Parser) {
961     return CreateReg(Index, RegKind_FCC, RegInfo, S, E, Parser);
962   }
963
964   /// Create a register that is definitely an ACC.
965   /// This is typically only used for named registers such as $ac0.
966   static std::unique_ptr<MipsOperand>
967   createACCReg(unsigned Index, const MCRegisterInfo *RegInfo, SMLoc S, SMLoc E,
968                MipsAsmParser &Parser) {
969     return CreateReg(Index, RegKind_ACC, RegInfo, S, E, Parser);
970   }
971
972   /// Create a register that is definitely an MSA128.
973   /// This is typically only used for named registers such as $w0.
974   static std::unique_ptr<MipsOperand>
975   createMSA128Reg(unsigned Index, const MCRegisterInfo *RegInfo, SMLoc S,
976                   SMLoc E, MipsAsmParser &Parser) {
977     return CreateReg(Index, RegKind_MSA128, RegInfo, S, E, Parser);
978   }
979
980   /// Create a register that is definitely an MSACtrl.
981   /// This is typically only used for named registers such as $msaaccess.
982   static std::unique_ptr<MipsOperand>
983   createMSACtrlReg(unsigned Index, const MCRegisterInfo *RegInfo, SMLoc S,
984                    SMLoc E, MipsAsmParser &Parser) {
985     return CreateReg(Index, RegKind_MSACtrl, RegInfo, S, E, Parser);
986   }
987
988   static std::unique_ptr<MipsOperand>
989   CreateImm(const MCExpr *Val, SMLoc S, SMLoc E, MipsAsmParser &Parser) {
990     auto Op = make_unique<MipsOperand>(k_Immediate, Parser);
991     Op->Imm.Val = Val;
992     Op->StartLoc = S;
993     Op->EndLoc = E;
994     return Op;
995   }
996
997   static std::unique_ptr<MipsOperand>
998   CreateMem(std::unique_ptr<MipsOperand> Base, const MCExpr *Off, SMLoc S,
999             SMLoc E, MipsAsmParser &Parser) {
1000     auto Op = make_unique<MipsOperand>(k_Memory, Parser);
1001     Op->Mem.Base = Base.release();
1002     Op->Mem.Off = Off;
1003     Op->StartLoc = S;
1004     Op->EndLoc = E;
1005     return Op;
1006   }
1007
1008   static std::unique_ptr<MipsOperand>
1009   CreateRegList(SmallVectorImpl<unsigned> &Regs, SMLoc StartLoc, SMLoc EndLoc,
1010                 MipsAsmParser &Parser) {
1011     assert (Regs.size() > 0 && "Empty list not allowed");
1012
1013     auto Op = make_unique<MipsOperand>(k_RegList, Parser);
1014     Op->RegList.List = new SmallVector<unsigned, 10>();
1015     for (auto Reg : Regs)
1016       Op->RegList.List->push_back(Reg);
1017     Op->StartLoc = StartLoc;
1018     Op->EndLoc = EndLoc;
1019     return Op;
1020   }
1021
1022   static std::unique_ptr<MipsOperand>
1023   CreateRegPair(unsigned RegNo, SMLoc S, SMLoc E, MipsAsmParser &Parser) {
1024     auto Op = make_unique<MipsOperand>(k_RegPair, Parser);
1025     Op->RegIdx.Index = RegNo;
1026     Op->StartLoc = S;
1027     Op->EndLoc = E;
1028     return Op;
1029   }
1030
1031   bool isGPRAsmReg() const {
1032     return isRegIdx() && RegIdx.Kind & RegKind_GPR && RegIdx.Index <= 31;
1033   }
1034   bool isMM16AsmReg() const {
1035     if (!(isRegIdx() && RegIdx.Kind))
1036       return false;
1037     return ((RegIdx.Index >= 2 && RegIdx.Index <= 7)
1038             || RegIdx.Index == 16 || RegIdx.Index == 17);
1039   }
1040   bool isMM16AsmRegZero() const {
1041     if (!(isRegIdx() && RegIdx.Kind))
1042       return false;
1043     return (RegIdx.Index == 0 ||
1044             (RegIdx.Index >= 2 && RegIdx.Index <= 7) ||
1045             RegIdx.Index == 17);
1046   }
1047   bool isFGRAsmReg() const {
1048     // AFGR64 is $0-$15 but we handle this in getAFGR64()
1049     return isRegIdx() && RegIdx.Kind & RegKind_FGR && RegIdx.Index <= 31;
1050   }
1051   bool isHWRegsAsmReg() const {
1052     return isRegIdx() && RegIdx.Kind & RegKind_HWRegs && RegIdx.Index <= 31;
1053   }
1054   bool isCCRAsmReg() const {
1055     return isRegIdx() && RegIdx.Kind & RegKind_CCR && RegIdx.Index <= 31;
1056   }
1057   bool isFCCAsmReg() const {
1058     if (!(isRegIdx() && RegIdx.Kind & RegKind_FCC))
1059       return false;
1060     if (!AsmParser.hasEightFccRegisters())
1061       return RegIdx.Index == 0;
1062     return RegIdx.Index <= 7;
1063   }
1064   bool isACCAsmReg() const {
1065     return isRegIdx() && RegIdx.Kind & RegKind_ACC && RegIdx.Index <= 3;
1066   }
1067   bool isCOP2AsmReg() const {
1068     return isRegIdx() && RegIdx.Kind & RegKind_COP2 && RegIdx.Index <= 31;
1069   }
1070   bool isCOP3AsmReg() const {
1071     return isRegIdx() && RegIdx.Kind & RegKind_COP3 && RegIdx.Index <= 31;
1072   }
1073   bool isMSA128AsmReg() const {
1074     return isRegIdx() && RegIdx.Kind & RegKind_MSA128 && RegIdx.Index <= 31;
1075   }
1076   bool isMSACtrlAsmReg() const {
1077     return isRegIdx() && RegIdx.Kind & RegKind_MSACtrl && RegIdx.Index <= 7;
1078   }
1079
1080   /// getStartLoc - Get the location of the first token of this operand.
1081   SMLoc getStartLoc() const override { return StartLoc; }
1082   /// getEndLoc - Get the location of the last token of this operand.
1083   SMLoc getEndLoc() const override { return EndLoc; }
1084
1085   virtual ~MipsOperand() {
1086     switch (Kind) {
1087     case k_Immediate:
1088       break;
1089     case k_Memory:
1090       delete Mem.Base;
1091       break;
1092     case k_RegList:
1093       delete RegList.List;
1094     case k_PhysRegister:
1095     case k_RegisterIndex:
1096     case k_Token:
1097     case k_RegPair:
1098       break;
1099     }
1100   }
1101
1102   void print(raw_ostream &OS) const override {
1103     switch (Kind) {
1104     case k_Immediate:
1105       OS << "Imm<";
1106       Imm.Val->print(OS);
1107       OS << ">";
1108       break;
1109     case k_Memory:
1110       OS << "Mem<";
1111       Mem.Base->print(OS);
1112       OS << ", ";
1113       Mem.Off->print(OS);
1114       OS << ">";
1115       break;
1116     case k_PhysRegister:
1117       OS << "PhysReg<" << PhysReg.Num << ">";
1118       break;
1119     case k_RegisterIndex:
1120       OS << "RegIdx<" << RegIdx.Index << ":" << RegIdx.Kind << ">";
1121       break;
1122     case k_Token:
1123       OS << Tok.Data;
1124       break;
1125     case k_RegList:
1126       OS << "RegList< ";
1127       for (auto Reg : (*RegList.List))
1128         OS << Reg << " ";
1129       OS <<  ">";
1130       break;
1131     case k_RegPair:
1132       OS << "RegPair<" << RegIdx.Index << "," << RegIdx.Index + 1 << ">";
1133       break;
1134     }
1135   }
1136 }; // class MipsOperand
1137 } // namespace
1138
1139 namespace llvm {
1140 extern const MCInstrDesc MipsInsts[];
1141 }
1142 static const MCInstrDesc &getInstDesc(unsigned Opcode) {
1143   return MipsInsts[Opcode];
1144 }
1145
1146 static bool hasShortDelaySlot(unsigned Opcode) {
1147   switch (Opcode) {
1148     case Mips::JALS_MM:
1149     case Mips::JALRS_MM:
1150     case Mips::JALRS16_MM:
1151     case Mips::BGEZALS_MM:
1152     case Mips::BLTZALS_MM:
1153       return true;
1154     default:
1155       return false;
1156   }
1157 }
1158
1159 bool MipsAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
1160                                        SmallVectorImpl<MCInst> &Instructions) {
1161   const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
1162
1163   Inst.setLoc(IDLoc);
1164
1165   if (MCID.isBranch() || MCID.isCall()) {
1166     const unsigned Opcode = Inst.getOpcode();
1167     MCOperand Offset;
1168
1169     switch (Opcode) {
1170     default:
1171       break;
1172     case Mips::BEQ:
1173     case Mips::BNE:
1174     case Mips::BEQ_MM:
1175     case Mips::BNE_MM:
1176       assert(MCID.getNumOperands() == 3 && "unexpected number of operands");
1177       Offset = Inst.getOperand(2);
1178       if (!Offset.isImm())
1179         break; // We'll deal with this situation later on when applying fixups.
1180       if (!isIntN(inMicroMipsMode() ? 17 : 18, Offset.getImm()))
1181         return Error(IDLoc, "branch target out of range");
1182       if (OffsetToAlignment(Offset.getImm(),
1183                             1LL << (inMicroMipsMode() ? 1 : 2)))
1184         return Error(IDLoc, "branch to misaligned address");
1185       break;
1186     case Mips::BGEZ:
1187     case Mips::BGTZ:
1188     case Mips::BLEZ:
1189     case Mips::BLTZ:
1190     case Mips::BGEZAL:
1191     case Mips::BLTZAL:
1192     case Mips::BC1F:
1193     case Mips::BC1T:
1194     case Mips::BGEZ_MM:
1195     case Mips::BGTZ_MM:
1196     case Mips::BLEZ_MM:
1197     case Mips::BLTZ_MM:
1198     case Mips::BGEZAL_MM:
1199     case Mips::BLTZAL_MM:
1200     case Mips::BC1F_MM:
1201     case Mips::BC1T_MM:
1202       assert(MCID.getNumOperands() == 2 && "unexpected number of operands");
1203       Offset = Inst.getOperand(1);
1204       if (!Offset.isImm())
1205         break; // We'll deal with this situation later on when applying fixups.
1206       if (!isIntN(inMicroMipsMode() ? 17 : 18, Offset.getImm()))
1207         return Error(IDLoc, "branch target out of range");
1208       if (OffsetToAlignment(Offset.getImm(),
1209                             1LL << (inMicroMipsMode() ? 1 : 2)))
1210         return Error(IDLoc, "branch to misaligned address");
1211       break;
1212     case Mips::BEQZ16_MM:
1213     case Mips::BNEZ16_MM:
1214       assert(MCID.getNumOperands() == 2 && "unexpected number of operands");
1215       Offset = Inst.getOperand(1);
1216       if (!Offset.isImm())
1217         break; // We'll deal with this situation later on when applying fixups.
1218       if (!isIntN(8, Offset.getImm()))
1219         return Error(IDLoc, "branch target out of range");
1220       if (OffsetToAlignment(Offset.getImm(), 2LL))
1221         return Error(IDLoc, "branch to misaligned address");
1222       break;
1223     }
1224   }
1225
1226   // SSNOP is deprecated on MIPS32r6/MIPS64r6
1227   // We still accept it but it is a normal nop.
1228   if (hasMips32r6() && Inst.getOpcode() == Mips::SSNOP) {
1229     std::string ISA = hasMips64r6() ? "MIPS64r6" : "MIPS32r6";
1230     Warning(IDLoc, "ssnop is deprecated for " + ISA + " and is equivalent to a "
1231                                                       "nop instruction");
1232   }
1233
1234   if (MCID.hasDelaySlot() && AssemblerOptions.back()->isReorder()) {
1235     // If this instruction has a delay slot and .set reorder is active,
1236     // emit a NOP after it.
1237     Instructions.push_back(Inst);
1238     MCInst NopInst;
1239     if (hasShortDelaySlot(Inst.getOpcode())) {
1240       NopInst.setOpcode(Mips::MOVE16_MM);
1241       NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1242       NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1243     } else {
1244       NopInst.setOpcode(Mips::SLL);
1245       NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1246       NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1247       NopInst.addOperand(MCOperand::CreateImm(0));
1248     }
1249     Instructions.push_back(NopInst);
1250     return false;
1251   }
1252
1253   if (MCID.mayLoad() || MCID.mayStore()) {
1254     // Check the offset of memory operand, if it is a symbol
1255     // reference or immediate we may have to expand instructions.
1256     for (unsigned i = 0; i < MCID.getNumOperands(); i++) {
1257       const MCOperandInfo &OpInfo = MCID.OpInfo[i];
1258       if ((OpInfo.OperandType == MCOI::OPERAND_MEMORY) ||
1259           (OpInfo.OperandType == MCOI::OPERAND_UNKNOWN)) {
1260         MCOperand &Op = Inst.getOperand(i);
1261         if (Op.isImm()) {
1262           int MemOffset = Op.getImm();
1263           if (MemOffset < -32768 || MemOffset > 32767) {
1264             // Offset can't exceed 16bit value.
1265             expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), true);
1266             return false;
1267           }
1268         } else if (Op.isExpr()) {
1269           const MCExpr *Expr = Op.getExpr();
1270           if (Expr->getKind() == MCExpr::SymbolRef) {
1271             const MCSymbolRefExpr *SR =
1272                 static_cast<const MCSymbolRefExpr *>(Expr);
1273             if (SR->getKind() == MCSymbolRefExpr::VK_None) {
1274               // Expand symbol.
1275               expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), false);
1276               return false;
1277             }
1278           } else if (!isEvaluated(Expr)) {
1279             expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), false);
1280             return false;
1281           }
1282         }
1283       }
1284     } // for
1285   }   // if load/store
1286
1287   // TODO: Handle this with the AsmOperandClass.PredicateMethod.
1288   if (inMicroMipsMode()) {
1289     MCOperand Opnd;
1290     int Imm;
1291
1292     switch (Inst.getOpcode()) {
1293       default:
1294         break;
1295       case Mips::ADDIUS5_MM:
1296         Opnd = Inst.getOperand(2);
1297         if (!Opnd.isImm())
1298           return Error(IDLoc, "expected immediate operand kind");
1299         Imm = Opnd.getImm();
1300         if (Imm < -8 || Imm > 7)
1301           return Error(IDLoc, "immediate operand value out of range");
1302         break;
1303       case Mips::ADDIUSP_MM:
1304         Opnd = Inst.getOperand(0);
1305         if (!Opnd.isImm())
1306           return Error(IDLoc, "expected immediate operand kind");
1307         Imm = Opnd.getImm();
1308         if (Imm < -1032 || Imm > 1028 || (Imm < 8 && Imm > -12) ||
1309             Imm % 4 != 0)
1310           return Error(IDLoc, "immediate operand value out of range");
1311         break;
1312       case Mips::SLL16_MM:
1313       case Mips::SRL16_MM:
1314         Opnd = Inst.getOperand(2);
1315         if (!Opnd.isImm())
1316           return Error(IDLoc, "expected immediate operand kind");
1317         Imm = Opnd.getImm();
1318         if (Imm < 1 || Imm > 8)
1319           return Error(IDLoc, "immediate operand value out of range");
1320         break;
1321       case Mips::LI16_MM:
1322         Opnd = Inst.getOperand(1);
1323         if (!Opnd.isImm())
1324           return Error(IDLoc, "expected immediate operand kind");
1325         Imm = Opnd.getImm();
1326         if (Imm < -1 || Imm > 126)
1327           return Error(IDLoc, "immediate operand value out of range");
1328         break;
1329       case Mips::ADDIUR2_MM:
1330         Opnd = Inst.getOperand(2);
1331         if (!Opnd.isImm())
1332           return Error(IDLoc, "expected immediate operand kind");
1333         Imm = Opnd.getImm();
1334         if (!(Imm == 1 || Imm == -1 ||
1335               ((Imm % 4 == 0) && Imm < 28 && Imm > 0)))
1336           return Error(IDLoc, "immediate operand value out of range");
1337         break;
1338       case Mips::ADDIUR1SP_MM:
1339         Opnd = Inst.getOperand(1);
1340         if (!Opnd.isImm())
1341           return Error(IDLoc, "expected immediate operand kind");
1342         Imm = Opnd.getImm();
1343         if (OffsetToAlignment(Imm, 4LL))
1344           return Error(IDLoc, "misaligned immediate operand value");
1345         if (Imm < 0 || Imm > 255)
1346           return Error(IDLoc, "immediate operand value out of range");
1347         break;
1348       case Mips::ANDI16_MM:
1349         Opnd = Inst.getOperand(2);
1350         if (!Opnd.isImm())
1351           return Error(IDLoc, "expected immediate operand kind");
1352         Imm = Opnd.getImm();
1353         if (!(Imm == 128 || (Imm >= 1 && Imm <= 4) || Imm == 7 || Imm == 8 ||
1354               Imm == 15 || Imm == 16 || Imm == 31 || Imm == 32 || Imm == 63 ||
1355               Imm == 64 || Imm == 255 || Imm == 32768 || Imm == 65535))
1356           return Error(IDLoc, "immediate operand value out of range");
1357         break;
1358       case Mips::LBU16_MM:
1359         Opnd = Inst.getOperand(2);
1360         if (!Opnd.isImm())
1361           return Error(IDLoc, "expected immediate operand kind");
1362         Imm = Opnd.getImm();
1363         if (Imm < -1 || Imm > 14)
1364           return Error(IDLoc, "immediate operand value out of range");
1365         break;
1366       case Mips::SB16_MM:
1367         Opnd = Inst.getOperand(2);
1368         if (!Opnd.isImm())
1369           return Error(IDLoc, "expected immediate operand kind");
1370         Imm = Opnd.getImm();
1371         if (Imm < 0 || Imm > 15)
1372           return Error(IDLoc, "immediate operand value out of range");
1373         break;
1374       case Mips::LHU16_MM:
1375       case Mips::SH16_MM:
1376         Opnd = Inst.getOperand(2);
1377         if (!Opnd.isImm())
1378           return Error(IDLoc, "expected immediate operand kind");
1379         Imm = Opnd.getImm();
1380         if (Imm < 0 || Imm > 30 || (Imm % 2 != 0))
1381           return Error(IDLoc, "immediate operand value out of range");
1382         break;
1383       case Mips::LW16_MM:
1384       case Mips::SW16_MM:
1385         Opnd = Inst.getOperand(2);
1386         if (!Opnd.isImm())
1387           return Error(IDLoc, "expected immediate operand kind");
1388         Imm = Opnd.getImm();
1389         if (Imm < 0 || Imm > 60 || (Imm % 4 != 0))
1390           return Error(IDLoc, "immediate operand value out of range");
1391         break;
1392       case Mips::CACHE:
1393       case Mips::PREF:
1394         Opnd = Inst.getOperand(2);
1395         if (!Opnd.isImm())
1396           return Error(IDLoc, "expected immediate operand kind");
1397         Imm = Opnd.getImm();
1398         if (!isUInt<5>(Imm))
1399           return Error(IDLoc, "immediate operand value out of range");
1400         break;
1401     }
1402   }
1403
1404   if (needsExpansion(Inst))
1405     return expandInstruction(Inst, IDLoc, Instructions);
1406   else
1407     Instructions.push_back(Inst);
1408
1409   return false;
1410 }
1411
1412 bool MipsAsmParser::needsExpansion(MCInst &Inst) {
1413
1414   switch (Inst.getOpcode()) {
1415   case Mips::LoadImm32Reg:
1416   case Mips::LoadAddr32Imm:
1417   case Mips::LoadAddr32Reg:
1418   case Mips::LoadImm64Reg:
1419   case Mips::B_MM_Pseudo:
1420     return true;
1421   default:
1422     return false;
1423   }
1424 }
1425
1426 bool MipsAsmParser::expandInstruction(MCInst &Inst, SMLoc IDLoc,
1427                                       SmallVectorImpl<MCInst> &Instructions) {
1428   switch (Inst.getOpcode()) {
1429   default: llvm_unreachable("unimplemented expansion");
1430   case Mips::LoadImm32Reg:
1431     return expandLoadImm(Inst, IDLoc, Instructions);
1432   case Mips::LoadImm64Reg:
1433     if (!isGP64bit()) {
1434       Error(IDLoc, "instruction requires a 64-bit architecture");
1435       return true;
1436     }
1437     return expandLoadImm(Inst, IDLoc, Instructions);
1438   case Mips::LoadAddr32Imm:
1439     return expandLoadAddressImm(Inst, IDLoc, Instructions);
1440   case Mips::LoadAddr32Reg:
1441     return expandLoadAddressReg(Inst, IDLoc, Instructions);
1442   case Mips::B_MM_Pseudo:
1443     return expandUncondBranchMMPseudo(Inst, IDLoc, Instructions);
1444   }
1445 }
1446
1447 namespace {
1448 template <bool PerformShift>
1449 void createShiftOr(MCOperand Operand, unsigned RegNo, SMLoc IDLoc,
1450                    SmallVectorImpl<MCInst> &Instructions) {
1451   MCInst tmpInst;
1452   if (PerformShift) {
1453     tmpInst.setOpcode(Mips::DSLL);
1454     tmpInst.addOperand(MCOperand::CreateReg(RegNo));
1455     tmpInst.addOperand(MCOperand::CreateReg(RegNo));
1456     tmpInst.addOperand(MCOperand::CreateImm(16));
1457     tmpInst.setLoc(IDLoc);
1458     Instructions.push_back(tmpInst);
1459     tmpInst.clear();
1460   }
1461   tmpInst.setOpcode(Mips::ORi);
1462   tmpInst.addOperand(MCOperand::CreateReg(RegNo));
1463   tmpInst.addOperand(MCOperand::CreateReg(RegNo));
1464   tmpInst.addOperand(Operand);
1465   tmpInst.setLoc(IDLoc);
1466   Instructions.push_back(tmpInst);
1467 }
1468
1469 template <int Shift, bool PerformShift>
1470 void createShiftOr(int64_t Value, unsigned RegNo, SMLoc IDLoc,
1471                    SmallVectorImpl<MCInst> &Instructions) {
1472   createShiftOr<PerformShift>(
1473       MCOperand::CreateImm(((Value & (0xffffLL << Shift)) >> Shift)), RegNo,
1474       IDLoc, Instructions);
1475 }
1476 }
1477
1478 bool MipsAsmParser::expandLoadImm(MCInst &Inst, SMLoc IDLoc,
1479                                   SmallVectorImpl<MCInst> &Instructions) {
1480   MCInst tmpInst;
1481   const MCOperand &ImmOp = Inst.getOperand(1);
1482   assert(ImmOp.isImm() && "expected immediate operand kind");
1483   const MCOperand &RegOp = Inst.getOperand(0);
1484   assert(RegOp.isReg() && "expected register operand kind");
1485
1486   int64_t ImmValue = ImmOp.getImm();
1487   tmpInst.setLoc(IDLoc);
1488   // FIXME: gas has a special case for values that are 000...1111, which
1489   // becomes a li -1 and then a dsrl
1490   if (0 <= ImmValue && ImmValue <= 65535) {
1491     // For 0 <= j <= 65535.
1492     // li d,j => ori d,$zero,j
1493     tmpInst.setOpcode(Mips::ORi);
1494     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1495     tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1496     tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
1497     Instructions.push_back(tmpInst);
1498   } else if (ImmValue < 0 && ImmValue >= -32768) {
1499     // For -32768 <= j < 0.
1500     // li d,j => addiu d,$zero,j
1501     tmpInst.setOpcode(Mips::ADDiu);
1502     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1503     tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1504     tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
1505     Instructions.push_back(tmpInst);
1506   } else if ((ImmValue & 0xffffffff) == ImmValue) {
1507     // For any value of j that is representable as a 32-bit integer, create
1508     // a sequence of:
1509     // li d,j => lui d,hi16(j)
1510     //           ori d,d,lo16(j)
1511     tmpInst.setOpcode(Mips::LUi);
1512     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1513     tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
1514     Instructions.push_back(tmpInst);
1515     createShiftOr<0, false>(ImmValue, RegOp.getReg(), IDLoc, Instructions);
1516   } else if ((ImmValue & (0xffffLL << 48)) == 0) {
1517     if (!isGP64bit()) {
1518       Error(IDLoc, "instruction requires a 64-bit architecture");
1519       return true;
1520     }
1521
1522     //            <-------  lo32 ------>
1523     // <-------  hi32 ------>
1524     // <- hi16 ->             <- lo16 ->
1525     //  _________________________________
1526     // |          |          |          |
1527     // | 16-bytes | 16-bytes | 16-bytes |
1528     // |__________|__________|__________|
1529     //
1530     // For any value of j that is representable as a 48-bit integer, create
1531     // a sequence of:
1532     // li d,j => lui d,hi16(j)
1533     //           ori d,d,hi16(lo32(j))
1534     //           dsll d,d,16
1535     //           ori d,d,lo16(lo32(j))
1536     tmpInst.setOpcode(Mips::LUi);
1537     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1538     tmpInst.addOperand(
1539         MCOperand::CreateImm((ImmValue & (0xffffLL << 32)) >> 32));
1540     Instructions.push_back(tmpInst);
1541     createShiftOr<16, false>(ImmValue, RegOp.getReg(), IDLoc, Instructions);
1542     createShiftOr<0, true>(ImmValue, RegOp.getReg(), IDLoc, Instructions);
1543   } else {
1544     if (!isGP64bit()) {
1545       Error(IDLoc, "instruction requires a 64-bit architecture");
1546       return true;
1547     }
1548
1549     // <-------  hi32 ------> <-------  lo32 ------>
1550     // <- hi16 ->                        <- lo16 ->
1551     //  ___________________________________________
1552     // |          |          |          |          |
1553     // | 16-bytes | 16-bytes | 16-bytes | 16-bytes |
1554     // |__________|__________|__________|__________|
1555     //
1556     // For any value of j that isn't representable as a 48-bit integer.
1557     // li d,j => lui d,hi16(j)
1558     //           ori d,d,lo16(hi32(j))
1559     //           dsll d,d,16
1560     //           ori d,d,hi16(lo32(j))
1561     //           dsll d,d,16
1562     //           ori d,d,lo16(lo32(j))
1563     tmpInst.setOpcode(Mips::LUi);
1564     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1565     tmpInst.addOperand(
1566         MCOperand::CreateImm((ImmValue & (0xffffLL << 48)) >> 48));
1567     Instructions.push_back(tmpInst);
1568     createShiftOr<32, false>(ImmValue, RegOp.getReg(), IDLoc, Instructions);
1569     createShiftOr<16, true>(ImmValue, RegOp.getReg(), IDLoc, Instructions);
1570     createShiftOr<0, true>(ImmValue, RegOp.getReg(), IDLoc, Instructions);
1571   }
1572   return false;
1573 }
1574
1575 bool
1576 MipsAsmParser::expandLoadAddressReg(MCInst &Inst, SMLoc IDLoc,
1577                                     SmallVectorImpl<MCInst> &Instructions) {
1578   MCInst tmpInst;
1579   const MCOperand &ImmOp = Inst.getOperand(2);
1580   assert((ImmOp.isImm() || ImmOp.isExpr()) &&
1581          "expected immediate operand kind");
1582   if (!ImmOp.isImm()) {
1583     expandLoadAddressSym(Inst, IDLoc, Instructions);
1584     return false;
1585   }
1586   const MCOperand &SrcRegOp = Inst.getOperand(1);
1587   assert(SrcRegOp.isReg() && "expected register operand kind");
1588   const MCOperand &DstRegOp = Inst.getOperand(0);
1589   assert(DstRegOp.isReg() && "expected register operand kind");
1590   int ImmValue = ImmOp.getImm();
1591   if (-32768 <= ImmValue && ImmValue <= 65535) {
1592     // For -32768 <= j <= 65535.
1593     // la d,j(s) => addiu d,s,j
1594     tmpInst.setOpcode(Mips::ADDiu);
1595     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1596     tmpInst.addOperand(MCOperand::CreateReg(SrcRegOp.getReg()));
1597     tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
1598     Instructions.push_back(tmpInst);
1599   } else {
1600     // For any other value of j that is representable as a 32-bit integer.
1601     // la d,j(s) => lui d,hi16(j)
1602     //              ori d,d,lo16(j)
1603     //              addu d,d,s
1604     tmpInst.setOpcode(Mips::LUi);
1605     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1606     tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
1607     Instructions.push_back(tmpInst);
1608     tmpInst.clear();
1609     tmpInst.setOpcode(Mips::ORi);
1610     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1611     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1612     tmpInst.addOperand(MCOperand::CreateImm(ImmValue & 0xffff));
1613     Instructions.push_back(tmpInst);
1614     tmpInst.clear();
1615     tmpInst.setOpcode(Mips::ADDu);
1616     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1617     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1618     tmpInst.addOperand(MCOperand::CreateReg(SrcRegOp.getReg()));
1619     Instructions.push_back(tmpInst);
1620   }
1621   return false;
1622 }
1623
1624 bool
1625 MipsAsmParser::expandLoadAddressImm(MCInst &Inst, SMLoc IDLoc,
1626                                     SmallVectorImpl<MCInst> &Instructions) {
1627   MCInst tmpInst;
1628   const MCOperand &ImmOp = Inst.getOperand(1);
1629   assert((ImmOp.isImm() || ImmOp.isExpr()) &&
1630          "expected immediate operand kind");
1631   if (!ImmOp.isImm()) {
1632     expandLoadAddressSym(Inst, IDLoc, Instructions);
1633     return false;
1634   }
1635   const MCOperand &RegOp = Inst.getOperand(0);
1636   assert(RegOp.isReg() && "expected register operand kind");
1637   int ImmValue = ImmOp.getImm();
1638   if (-32768 <= ImmValue && ImmValue <= 65535) {
1639     // For -32768 <= j <= 65535.
1640     // la d,j => addiu d,$zero,j
1641     tmpInst.setOpcode(Mips::ADDiu);
1642     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1643     tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1644     tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
1645     Instructions.push_back(tmpInst);
1646   } else {
1647     // For any other value of j that is representable as a 32-bit integer.
1648     // la d,j => lui d,hi16(j)
1649     //           ori d,d,lo16(j)
1650     tmpInst.setOpcode(Mips::LUi);
1651     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1652     tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
1653     Instructions.push_back(tmpInst);
1654     tmpInst.clear();
1655     tmpInst.setOpcode(Mips::ORi);
1656     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1657     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1658     tmpInst.addOperand(MCOperand::CreateImm(ImmValue & 0xffff));
1659     Instructions.push_back(tmpInst);
1660   }
1661   return false;
1662 }
1663
1664 void
1665 MipsAsmParser::expandLoadAddressSym(MCInst &Inst, SMLoc IDLoc,
1666                                     SmallVectorImpl<MCInst> &Instructions) {
1667   // FIXME: If we do have a valid at register to use, we should generate a
1668   // slightly shorter sequence here.
1669   MCInst tmpInst;
1670   int ExprOperandNo = 1;
1671   // Sometimes the assembly parser will get the immediate expression as
1672   // a $zero + an immediate.
1673   if (Inst.getNumOperands() == 3) {
1674     assert(Inst.getOperand(1).getReg() ==
1675            (isGP64bit() ? Mips::ZERO_64 : Mips::ZERO));
1676     ExprOperandNo = 2;
1677   }
1678   const MCOperand &SymOp = Inst.getOperand(ExprOperandNo);
1679   assert(SymOp.isExpr() && "expected symbol operand kind");
1680   const MCOperand &RegOp = Inst.getOperand(0);
1681   unsigned RegNo = RegOp.getReg();
1682   const MCSymbolRefExpr *Symbol = cast<MCSymbolRefExpr>(SymOp.getExpr());
1683   const MCSymbolRefExpr *HiExpr =
1684       MCSymbolRefExpr::Create(Symbol->getSymbol().getName(),
1685                               MCSymbolRefExpr::VK_Mips_ABS_HI, getContext());
1686   const MCSymbolRefExpr *LoExpr =
1687       MCSymbolRefExpr::Create(Symbol->getSymbol().getName(),
1688                               MCSymbolRefExpr::VK_Mips_ABS_LO, getContext());
1689   if (isGP64bit()) {
1690     // If it's a 64-bit architecture, expand to:
1691     // la d,sym => lui  d,highest(sym)
1692     //             ori  d,d,higher(sym)
1693     //             dsll d,d,16
1694     //             ori  d,d,hi16(sym)
1695     //             dsll d,d,16
1696     //             ori  d,d,lo16(sym)
1697     const MCSymbolRefExpr *HighestExpr =
1698         MCSymbolRefExpr::Create(Symbol->getSymbol().getName(),
1699                                 MCSymbolRefExpr::VK_Mips_HIGHEST, getContext());
1700     const MCSymbolRefExpr *HigherExpr =
1701         MCSymbolRefExpr::Create(Symbol->getSymbol().getName(),
1702                                 MCSymbolRefExpr::VK_Mips_HIGHER, getContext());
1703
1704     tmpInst.setOpcode(Mips::LUi);
1705     tmpInst.addOperand(MCOperand::CreateReg(RegNo));
1706     tmpInst.addOperand(MCOperand::CreateExpr(HighestExpr));
1707     Instructions.push_back(tmpInst);
1708
1709     createShiftOr<false>(MCOperand::CreateExpr(HigherExpr), RegNo, SMLoc(),
1710                          Instructions);
1711     createShiftOr<true>(MCOperand::CreateExpr(HiExpr), RegNo, SMLoc(),
1712                         Instructions);
1713     createShiftOr<true>(MCOperand::CreateExpr(LoExpr), RegNo, SMLoc(),
1714                         Instructions);
1715   } else {
1716     // Otherwise, expand to:
1717     // la d,sym => lui  d,hi16(sym)
1718     //             ori  d,d,lo16(sym)
1719     tmpInst.setOpcode(Mips::LUi);
1720     tmpInst.addOperand(MCOperand::CreateReg(RegNo));
1721     tmpInst.addOperand(MCOperand::CreateExpr(HiExpr));
1722     Instructions.push_back(tmpInst);
1723
1724     createShiftOr<false>(MCOperand::CreateExpr(LoExpr), RegNo, SMLoc(),
1725                          Instructions);
1726   }
1727 }
1728
1729 bool MipsAsmParser::
1730 expandUncondBranchMMPseudo(MCInst &Inst, SMLoc IDLoc,
1731                            SmallVectorImpl<MCInst> &Instructions) {
1732   assert(getInstDesc(Inst.getOpcode()).getNumOperands() == 1 &&
1733          "unexpected number of operands");
1734
1735   MCOperand Offset = Inst.getOperand(0);
1736   if (Offset.isExpr()) {
1737     Inst.clear();
1738     Inst.setOpcode(Mips::BEQ_MM);
1739     Inst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1740     Inst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1741     Inst.addOperand(MCOperand::CreateExpr(Offset.getExpr()));
1742   } else {
1743     assert(Offset.isImm() && "expected immediate operand kind");
1744     if (isIntN(11, Offset.getImm())) {
1745       // If offset fits into 11 bits then this instruction becomes microMIPS
1746       // 16-bit unconditional branch instruction.
1747       Inst.setOpcode(Mips::B16_MM);
1748     } else {
1749       if (!isIntN(17, Offset.getImm()))
1750         Error(IDLoc, "branch target out of range");
1751       if (OffsetToAlignment(Offset.getImm(), 1LL << 1))
1752         Error(IDLoc, "branch to misaligned address");
1753       Inst.clear();
1754       Inst.setOpcode(Mips::BEQ_MM);
1755       Inst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1756       Inst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1757       Inst.addOperand(MCOperand::CreateImm(Offset.getImm()));
1758     }
1759   }
1760   Instructions.push_back(Inst);
1761
1762   if (AssemblerOptions.back()->isReorder()) {
1763     // If .set reorder is active, emit a NOP after the branch instruction.
1764     MCInst NopInst;
1765     NopInst.setOpcode(Mips::MOVE16_MM);
1766     NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1767     NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1768     Instructions.push_back(NopInst);
1769   }
1770   return false;
1771 }
1772
1773 void MipsAsmParser::expandMemInst(MCInst &Inst, SMLoc IDLoc,
1774                                   SmallVectorImpl<MCInst> &Instructions,
1775                                   bool isLoad, bool isImmOpnd) {
1776   const MCSymbolRefExpr *SR;
1777   MCInst TempInst;
1778   unsigned ImmOffset, HiOffset, LoOffset;
1779   const MCExpr *ExprOffset;
1780   unsigned TmpRegNum;
1781   // 1st operand is either the source or destination register.
1782   assert(Inst.getOperand(0).isReg() && "expected register operand kind");
1783   unsigned RegOpNum = Inst.getOperand(0).getReg();
1784   // 2nd operand is the base register.
1785   assert(Inst.getOperand(1).isReg() && "expected register operand kind");
1786   unsigned BaseRegNum = Inst.getOperand(1).getReg();
1787   // 3rd operand is either an immediate or expression.
1788   if (isImmOpnd) {
1789     assert(Inst.getOperand(2).isImm() && "expected immediate operand kind");
1790     ImmOffset = Inst.getOperand(2).getImm();
1791     LoOffset = ImmOffset & 0x0000ffff;
1792     HiOffset = (ImmOffset & 0xffff0000) >> 16;
1793     // If msb of LoOffset is 1(negative number) we must increment HiOffset.
1794     if (LoOffset & 0x8000)
1795       HiOffset++;
1796   } else
1797     ExprOffset = Inst.getOperand(2).getExpr();
1798   // All instructions will have the same location.
1799   TempInst.setLoc(IDLoc);
1800   // These are some of the types of expansions we perform here:
1801   // 1) lw $8, sym        => lui $8, %hi(sym)
1802   //                         lw $8, %lo(sym)($8)
1803   // 2) lw $8, offset($9) => lui $8, %hi(offset)
1804   //                         add $8, $8, $9
1805   //                         lw $8, %lo(offset)($9)
1806   // 3) lw $8, offset($8) => lui $at, %hi(offset)
1807   //                         add $at, $at, $8
1808   //                         lw $8, %lo(offset)($at)
1809   // 4) sw $8, sym        => lui $at, %hi(sym)
1810   //                         sw $8, %lo(sym)($at)
1811   // 5) sw $8, offset($8) => lui $at, %hi(offset)
1812   //                         add $at, $at, $8
1813   //                         sw $8, %lo(offset)($at)
1814   // 6) ldc1 $f0, sym     => lui $at, %hi(sym)
1815   //                         ldc1 $f0, %lo(sym)($at)
1816   //
1817   // For load instructions we can use the destination register as a temporary
1818   // if base and dst are different (examples 1 and 2) and if the base register
1819   // is general purpose otherwise we must use $at (example 6) and error if it's
1820   // not available. For stores we must use $at (examples 4 and 5) because we
1821   // must not clobber the source register setting up the offset.
1822   const MCInstrDesc &Desc = getInstDesc(Inst.getOpcode());
1823   int16_t RegClassOp0 = Desc.OpInfo[0].RegClass;
1824   unsigned RegClassIDOp0 =
1825       getContext().getRegisterInfo()->getRegClass(RegClassOp0).getID();
1826   bool IsGPR = (RegClassIDOp0 == Mips::GPR32RegClassID) ||
1827                (RegClassIDOp0 == Mips::GPR64RegClassID);
1828   if (isLoad && IsGPR && (BaseRegNum != RegOpNum))
1829     TmpRegNum = RegOpNum;
1830   else {
1831     int AT = getATReg(IDLoc);
1832     // At this point we need AT to perform the expansions and we exit if it is
1833     // not available.
1834     if (!AT)
1835       return;
1836     TmpRegNum = getReg(
1837         (isGP64bit()) ? Mips::GPR64RegClassID : Mips::GPR32RegClassID, AT);
1838   }
1839
1840   TempInst.setOpcode(Mips::LUi);
1841   TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
1842   if (isImmOpnd)
1843     TempInst.addOperand(MCOperand::CreateImm(HiOffset));
1844   else {
1845     if (ExprOffset->getKind() == MCExpr::SymbolRef) {
1846       SR = static_cast<const MCSymbolRefExpr *>(ExprOffset);
1847       const MCSymbolRefExpr *HiExpr = MCSymbolRefExpr::Create(
1848           SR->getSymbol().getName(), MCSymbolRefExpr::VK_Mips_ABS_HI,
1849           getContext());
1850       TempInst.addOperand(MCOperand::CreateExpr(HiExpr));
1851     } else {
1852       const MCExpr *HiExpr = evaluateRelocExpr(ExprOffset, "hi");
1853       TempInst.addOperand(MCOperand::CreateExpr(HiExpr));
1854     }
1855   }
1856   // Add the instruction to the list.
1857   Instructions.push_back(TempInst);
1858   // Prepare TempInst for next instruction.
1859   TempInst.clear();
1860   // Add temp register to base.
1861   TempInst.setOpcode(Mips::ADDu);
1862   TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
1863   TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
1864   TempInst.addOperand(MCOperand::CreateReg(BaseRegNum));
1865   Instructions.push_back(TempInst);
1866   TempInst.clear();
1867   // And finally, create original instruction with low part
1868   // of offset and new base.
1869   TempInst.setOpcode(Inst.getOpcode());
1870   TempInst.addOperand(MCOperand::CreateReg(RegOpNum));
1871   TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
1872   if (isImmOpnd)
1873     TempInst.addOperand(MCOperand::CreateImm(LoOffset));
1874   else {
1875     if (ExprOffset->getKind() == MCExpr::SymbolRef) {
1876       const MCSymbolRefExpr *LoExpr = MCSymbolRefExpr::Create(
1877           SR->getSymbol().getName(), MCSymbolRefExpr::VK_Mips_ABS_LO,
1878           getContext());
1879       TempInst.addOperand(MCOperand::CreateExpr(LoExpr));
1880     } else {
1881       const MCExpr *LoExpr = evaluateRelocExpr(ExprOffset, "lo");
1882       TempInst.addOperand(MCOperand::CreateExpr(LoExpr));
1883     }
1884   }
1885   Instructions.push_back(TempInst);
1886   TempInst.clear();
1887 }
1888
1889 unsigned MipsAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
1890   // As described by the Mips32r2 spec, the registers Rd and Rs for
1891   // jalr.hb must be different.
1892   unsigned Opcode = Inst.getOpcode();
1893
1894   if (Opcode == Mips::JALR_HB &&
1895       (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()))
1896     return Match_RequiresDifferentSrcAndDst;
1897
1898   return Match_Success;
1899 }
1900
1901 bool MipsAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
1902                                             OperandVector &Operands,
1903                                             MCStreamer &Out,
1904                                             uint64_t &ErrorInfo,
1905                                             bool MatchingInlineAsm) {
1906
1907   MCInst Inst;
1908   SmallVector<MCInst, 8> Instructions;
1909   unsigned MatchResult =
1910       MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
1911
1912   switch (MatchResult) {
1913   case Match_Success: {
1914     if (processInstruction(Inst, IDLoc, Instructions))
1915       return true;
1916     for (unsigned i = 0; i < Instructions.size(); i++)
1917       Out.EmitInstruction(Instructions[i], STI);
1918     return false;
1919   }
1920   case Match_MissingFeature:
1921     Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1922     return true;
1923   case Match_InvalidOperand: {
1924     SMLoc ErrorLoc = IDLoc;
1925     if (ErrorInfo != ~0ULL) {
1926       if (ErrorInfo >= Operands.size())
1927         return Error(IDLoc, "too few operands for instruction");
1928
1929       ErrorLoc = ((MipsOperand &)*Operands[ErrorInfo]).getStartLoc();
1930       if (ErrorLoc == SMLoc())
1931         ErrorLoc = IDLoc;
1932     }
1933
1934     return Error(ErrorLoc, "invalid operand for instruction");
1935   }
1936   case Match_MnemonicFail:
1937     return Error(IDLoc, "invalid instruction");
1938   case Match_RequiresDifferentSrcAndDst:
1939     return Error(IDLoc, "source and destination must be different");
1940   }
1941
1942   llvm_unreachable("Implement any new match types added!");
1943 }
1944
1945 void MipsAsmParser::warnIfAssemblerTemporary(int RegIndex, SMLoc Loc) {
1946   if ((RegIndex != 0) && 
1947       ((int)AssemblerOptions.back()->getATRegNum() == RegIndex)) {
1948     if (RegIndex == 1)
1949       Warning(Loc, "used $at without \".set noat\"");
1950     else
1951       Warning(Loc, Twine("used $") + Twine(RegIndex) + " with \".set at=$" +
1952                        Twine(RegIndex) + "\"");
1953   }
1954 }
1955
1956 void
1957 MipsAsmParser::printWarningWithFixIt(const Twine &Msg, const Twine &FixMsg,
1958                                      SMRange Range, bool ShowColors) {
1959   getSourceManager().PrintMessage(Range.Start, SourceMgr::DK_Warning, Msg,
1960                                   Range, SMFixIt(Range, FixMsg),
1961                                   ShowColors);
1962 }
1963
1964 int MipsAsmParser::matchCPURegisterName(StringRef Name) {
1965   int CC;
1966
1967   CC = StringSwitch<unsigned>(Name)
1968            .Case("zero", 0)
1969            .Case("at", 1)
1970            .Case("a0", 4)
1971            .Case("a1", 5)
1972            .Case("a2", 6)
1973            .Case("a3", 7)
1974            .Case("v0", 2)
1975            .Case("v1", 3)
1976            .Case("s0", 16)
1977            .Case("s1", 17)
1978            .Case("s2", 18)
1979            .Case("s3", 19)
1980            .Case("s4", 20)
1981            .Case("s5", 21)
1982            .Case("s6", 22)
1983            .Case("s7", 23)
1984            .Case("k0", 26)
1985            .Case("k1", 27)
1986            .Case("gp", 28)
1987            .Case("sp", 29)
1988            .Case("fp", 30)
1989            .Case("s8", 30)
1990            .Case("ra", 31)
1991            .Case("t0", 8)
1992            .Case("t1", 9)
1993            .Case("t2", 10)
1994            .Case("t3", 11)
1995            .Case("t4", 12)
1996            .Case("t5", 13)
1997            .Case("t6", 14)
1998            .Case("t7", 15)
1999            .Case("t8", 24)
2000            .Case("t9", 25)
2001            .Default(-1);
2002
2003   if (!(isABI_N32() || isABI_N64()))
2004     return CC;
2005
2006   if (12 <= CC && CC <= 15) {
2007     // Name is one of t4-t7
2008     AsmToken RegTok = getLexer().peekTok();
2009     SMRange RegRange = RegTok.getLocRange();
2010
2011     StringRef FixedName = StringSwitch<StringRef>(Name)
2012                               .Case("t4", "t0")
2013                               .Case("t5", "t1")
2014                               .Case("t6", "t2")
2015                               .Case("t7", "t3")
2016                               .Default("");
2017     assert(FixedName != "" &&  "Register name is not one of t4-t7.");
2018
2019     printWarningWithFixIt("register names $t4-$t7 are only available in O32.",
2020                           "Did you mean $" + FixedName + "?", RegRange);
2021   }
2022
2023   // Although SGI documentation just cuts out t0-t3 for n32/n64,
2024   // GNU pushes the values of t0-t3 to override the o32/o64 values for t4-t7
2025   // We are supporting both cases, so for t0-t3 we'll just push them to t4-t7.
2026   if (8 <= CC && CC <= 11)
2027     CC += 4;
2028
2029   if (CC == -1)
2030     CC = StringSwitch<unsigned>(Name)
2031              .Case("a4", 8)
2032              .Case("a5", 9)
2033              .Case("a6", 10)
2034              .Case("a7", 11)
2035              .Case("kt0", 26)
2036              .Case("kt1", 27)
2037              .Default(-1);
2038
2039   return CC;
2040 }
2041
2042 int MipsAsmParser::matchHWRegsRegisterName(StringRef Name) {
2043   int CC;
2044
2045   CC = StringSwitch<unsigned>(Name)
2046             .Case("hwr_cpunum", 0)
2047             .Case("hwr_synci_step", 1)
2048             .Case("hwr_cc", 2)
2049             .Case("hwr_ccres", 3)
2050             .Case("hwr_ulr", 29)
2051             .Default(-1);
2052
2053   return CC;
2054 }
2055
2056 int MipsAsmParser::matchFPURegisterName(StringRef Name) {
2057
2058   if (Name[0] == 'f') {
2059     StringRef NumString = Name.substr(1);
2060     unsigned IntVal;
2061     if (NumString.getAsInteger(10, IntVal))
2062       return -1;     // This is not an integer.
2063     if (IntVal > 31) // Maximum index for fpu register.
2064       return -1;
2065     return IntVal;
2066   }
2067   return -1;
2068 }
2069
2070 int MipsAsmParser::matchFCCRegisterName(StringRef Name) {
2071
2072   if (Name.startswith("fcc")) {
2073     StringRef NumString = Name.substr(3);
2074     unsigned IntVal;
2075     if (NumString.getAsInteger(10, IntVal))
2076       return -1;    // This is not an integer.
2077     if (IntVal > 7) // There are only 8 fcc registers.
2078       return -1;
2079     return IntVal;
2080   }
2081   return -1;
2082 }
2083
2084 int MipsAsmParser::matchACRegisterName(StringRef Name) {
2085
2086   if (Name.startswith("ac")) {
2087     StringRef NumString = Name.substr(2);
2088     unsigned IntVal;
2089     if (NumString.getAsInteger(10, IntVal))
2090       return -1;    // This is not an integer.
2091     if (IntVal > 3) // There are only 3 acc registers.
2092       return -1;
2093     return IntVal;
2094   }
2095   return -1;
2096 }
2097
2098 int MipsAsmParser::matchMSA128RegisterName(StringRef Name) {
2099   unsigned IntVal;
2100
2101   if (Name.front() != 'w' || Name.drop_front(1).getAsInteger(10, IntVal))
2102     return -1;
2103
2104   if (IntVal > 31)
2105     return -1;
2106
2107   return IntVal;
2108 }
2109
2110 int MipsAsmParser::matchMSA128CtrlRegisterName(StringRef Name) {
2111   int CC;
2112
2113   CC = StringSwitch<unsigned>(Name)
2114            .Case("msair", 0)
2115            .Case("msacsr", 1)
2116            .Case("msaaccess", 2)
2117            .Case("msasave", 3)
2118            .Case("msamodify", 4)
2119            .Case("msarequest", 5)
2120            .Case("msamap", 6)
2121            .Case("msaunmap", 7)
2122            .Default(-1);
2123
2124   return CC;
2125 }
2126
2127 bool MipsAssemblerOptions::setATReg(unsigned Reg) {
2128   if (Reg > 31)
2129     return false;
2130
2131   ATReg = Reg;
2132   return true;
2133 }
2134
2135 int MipsAsmParser::getATReg(SMLoc Loc) {
2136   int AT = AssemblerOptions.back()->getATRegNum();
2137   if (AT == 0)
2138     reportParseError(Loc,
2139                      "pseudo-instruction requires $at, which is not available");
2140   return AT;
2141 }
2142
2143 unsigned MipsAsmParser::getReg(int RC, int RegNo) {
2144   return *(getContext().getRegisterInfo()->getRegClass(RC).begin() + RegNo);
2145 }
2146
2147 unsigned MipsAsmParser::getGPR(int RegNo) {
2148   return getReg(isGP64bit() ? Mips::GPR64RegClassID : Mips::GPR32RegClassID,
2149                 RegNo);
2150 }
2151
2152 int MipsAsmParser::matchRegisterByNumber(unsigned RegNum, unsigned RegClass) {
2153   if (RegNum >
2154       getContext().getRegisterInfo()->getRegClass(RegClass).getNumRegs() - 1)
2155     return -1;
2156
2157   return getReg(RegClass, RegNum);
2158 }
2159
2160 bool MipsAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
2161   MCAsmParser &Parser = getParser();
2162   DEBUG(dbgs() << "parseOperand\n");
2163
2164   // Check if the current operand has a custom associated parser, if so, try to
2165   // custom parse the operand, or fallback to the general approach.
2166   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
2167   if (ResTy == MatchOperand_Success)
2168     return false;
2169   // If there wasn't a custom match, try the generic matcher below. Otherwise,
2170   // there was a match, but an error occurred, in which case, just return that
2171   // the operand parsing failed.
2172   if (ResTy == MatchOperand_ParseFail)
2173     return true;
2174
2175   DEBUG(dbgs() << ".. Generic Parser\n");
2176
2177   switch (getLexer().getKind()) {
2178   default:
2179     Error(Parser.getTok().getLoc(), "unexpected token in operand");
2180     return true;
2181   case AsmToken::Dollar: {
2182     // Parse the register.
2183     SMLoc S = Parser.getTok().getLoc();
2184
2185     // Almost all registers have been parsed by custom parsers. There is only
2186     // one exception to this. $zero (and it's alias $0) will reach this point
2187     // for div, divu, and similar instructions because it is not an operand
2188     // to the instruction definition but an explicit register. Special case
2189     // this situation for now.
2190     if (parseAnyRegister(Operands) != MatchOperand_NoMatch)
2191       return false;
2192
2193     // Maybe it is a symbol reference.
2194     StringRef Identifier;
2195     if (Parser.parseIdentifier(Identifier))
2196       return true;
2197
2198     SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
2199     MCSymbol *Sym = getContext().GetOrCreateSymbol("$" + Identifier);
2200     // Otherwise create a symbol reference.
2201     const MCExpr *Res =
2202         MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
2203
2204     Operands.push_back(MipsOperand::CreateImm(Res, S, E, *this));
2205     return false;
2206   }
2207   // Else drop to expression parsing.
2208   case AsmToken::LParen:
2209   case AsmToken::Minus:
2210   case AsmToken::Plus:
2211   case AsmToken::Integer:
2212   case AsmToken::Tilde:
2213   case AsmToken::String: {
2214     DEBUG(dbgs() << ".. generic integer\n");
2215     OperandMatchResultTy ResTy = parseImm(Operands);
2216     return ResTy != MatchOperand_Success;
2217   }
2218   case AsmToken::Percent: {
2219     // It is a symbol reference or constant expression.
2220     const MCExpr *IdVal;
2221     SMLoc S = Parser.getTok().getLoc(); // Start location of the operand.
2222     if (parseRelocOperand(IdVal))
2223       return true;
2224
2225     SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
2226
2227     Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
2228     return false;
2229   } // case AsmToken::Percent
2230   } // switch(getLexer().getKind())
2231   return true;
2232 }
2233
2234 const MCExpr *MipsAsmParser::evaluateRelocExpr(const MCExpr *Expr,
2235                                                StringRef RelocStr) {
2236   const MCExpr *Res;
2237   // Check the type of the expression.
2238   if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Expr)) {
2239     // It's a constant, evaluate reloc value.
2240     int16_t Val;
2241     switch (getVariantKind(RelocStr)) {
2242     case MCSymbolRefExpr::VK_Mips_ABS_LO:
2243       // Get the 1st 16-bits.
2244       Val = MCE->getValue() & 0xffff;
2245       break;
2246     case MCSymbolRefExpr::VK_Mips_ABS_HI:
2247       // Get the 2nd 16-bits. Also add 1 if bit 15 is 1, to compensate for low
2248       // 16 bits being negative.
2249       Val = ((MCE->getValue() + 0x8000) >> 16) & 0xffff;
2250       break;
2251     case MCSymbolRefExpr::VK_Mips_HIGHER:
2252       // Get the 3rd 16-bits.
2253       Val = ((MCE->getValue() + 0x80008000LL) >> 32) & 0xffff;
2254       break;
2255     case MCSymbolRefExpr::VK_Mips_HIGHEST:
2256       // Get the 4th 16-bits.
2257       Val = ((MCE->getValue() + 0x800080008000LL) >> 48) & 0xffff;
2258       break;
2259     default:
2260       report_fatal_error("unsupported reloc value");
2261     }
2262     return MCConstantExpr::Create(Val, getContext());
2263   }
2264
2265   if (const MCSymbolRefExpr *MSRE = dyn_cast<MCSymbolRefExpr>(Expr)) {
2266     // It's a symbol, create a symbolic expression from the symbol.
2267     StringRef Symbol = MSRE->getSymbol().getName();
2268     MCSymbolRefExpr::VariantKind VK = getVariantKind(RelocStr);
2269     Res = MCSymbolRefExpr::Create(Symbol, VK, getContext());
2270     return Res;
2271   }
2272
2273   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
2274     MCSymbolRefExpr::VariantKind VK = getVariantKind(RelocStr);
2275
2276     // Try to create target expression.
2277     if (MipsMCExpr::isSupportedBinaryExpr(VK, BE))
2278       return MipsMCExpr::Create(VK, Expr, getContext());
2279
2280     const MCExpr *LExp = evaluateRelocExpr(BE->getLHS(), RelocStr);
2281     const MCExpr *RExp = evaluateRelocExpr(BE->getRHS(), RelocStr);
2282     Res = MCBinaryExpr::Create(BE->getOpcode(), LExp, RExp, getContext());
2283     return Res;
2284   }
2285
2286   if (const MCUnaryExpr *UN = dyn_cast<MCUnaryExpr>(Expr)) {
2287     const MCExpr *UnExp = evaluateRelocExpr(UN->getSubExpr(), RelocStr);
2288     Res = MCUnaryExpr::Create(UN->getOpcode(), UnExp, getContext());
2289     return Res;
2290   }
2291   // Just return the original expression.
2292   return Expr;
2293 }
2294
2295 bool MipsAsmParser::isEvaluated(const MCExpr *Expr) {
2296
2297   switch (Expr->getKind()) {
2298   case MCExpr::Constant:
2299     return true;
2300   case MCExpr::SymbolRef:
2301     return (cast<MCSymbolRefExpr>(Expr)->getKind() != MCSymbolRefExpr::VK_None);
2302   case MCExpr::Binary:
2303     if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
2304       if (!isEvaluated(BE->getLHS()))
2305         return false;
2306       return isEvaluated(BE->getRHS());
2307     }
2308   case MCExpr::Unary:
2309     return isEvaluated(cast<MCUnaryExpr>(Expr)->getSubExpr());
2310   case MCExpr::Target:
2311     return true;
2312   }
2313   return false;
2314 }
2315
2316 bool MipsAsmParser::parseRelocOperand(const MCExpr *&Res) {
2317   MCAsmParser &Parser = getParser();
2318   Parser.Lex();                          // Eat the % token.
2319   const AsmToken &Tok = Parser.getTok(); // Get next token, operation.
2320   if (Tok.isNot(AsmToken::Identifier))
2321     return true;
2322
2323   std::string Str = Tok.getIdentifier().str();
2324
2325   Parser.Lex(); // Eat the identifier.
2326   // Now make an expression from the rest of the operand.
2327   const MCExpr *IdVal;
2328   SMLoc EndLoc;
2329
2330   if (getLexer().getKind() == AsmToken::LParen) {
2331     while (1) {
2332       Parser.Lex(); // Eat the '(' token.
2333       if (getLexer().getKind() == AsmToken::Percent) {
2334         Parser.Lex(); // Eat the % token.
2335         const AsmToken &nextTok = Parser.getTok();
2336         if (nextTok.isNot(AsmToken::Identifier))
2337           return true;
2338         Str += "(%";
2339         Str += nextTok.getIdentifier();
2340         Parser.Lex(); // Eat the identifier.
2341         if (getLexer().getKind() != AsmToken::LParen)
2342           return true;
2343       } else
2344         break;
2345     }
2346     if (getParser().parseParenExpression(IdVal, EndLoc))
2347       return true;
2348
2349     while (getLexer().getKind() == AsmToken::RParen)
2350       Parser.Lex(); // Eat the ')' token.
2351
2352   } else
2353     return true; // Parenthesis must follow the relocation operand.
2354
2355   Res = evaluateRelocExpr(IdVal, Str);
2356   return false;
2357 }
2358
2359 bool MipsAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
2360                                   SMLoc &EndLoc) {
2361   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Operands;
2362   OperandMatchResultTy ResTy = parseAnyRegister(Operands);
2363   if (ResTy == MatchOperand_Success) {
2364     assert(Operands.size() == 1);
2365     MipsOperand &Operand = static_cast<MipsOperand &>(*Operands.front());
2366     StartLoc = Operand.getStartLoc();
2367     EndLoc = Operand.getEndLoc();
2368
2369     // AFAIK, we only support numeric registers and named GPR's in CFI
2370     // directives.
2371     // Don't worry about eating tokens before failing. Using an unrecognised
2372     // register is a parse error.
2373     if (Operand.isGPRAsmReg()) {
2374       // Resolve to GPR32 or GPR64 appropriately.
2375       RegNo = isGP64bit() ? Operand.getGPR64Reg() : Operand.getGPR32Reg();
2376     }
2377
2378     return (RegNo == (unsigned)-1);
2379   }
2380
2381   assert(Operands.size() == 0);
2382   return (RegNo == (unsigned)-1);
2383 }
2384
2385 bool MipsAsmParser::parseMemOffset(const MCExpr *&Res, bool isParenExpr) {
2386   MCAsmParser &Parser = getParser();
2387   SMLoc S;
2388   bool Result = true;
2389
2390   while (getLexer().getKind() == AsmToken::LParen)
2391     Parser.Lex();
2392
2393   switch (getLexer().getKind()) {
2394   default:
2395     return true;
2396   case AsmToken::Identifier:
2397   case AsmToken::LParen:
2398   case AsmToken::Integer:
2399   case AsmToken::Minus:
2400   case AsmToken::Plus:
2401     if (isParenExpr)
2402       Result = getParser().parseParenExpression(Res, S);
2403     else
2404       Result = (getParser().parseExpression(Res));
2405     while (getLexer().getKind() == AsmToken::RParen)
2406       Parser.Lex();
2407     break;
2408   case AsmToken::Percent:
2409     Result = parseRelocOperand(Res);
2410   }
2411   return Result;
2412 }
2413
2414 MipsAsmParser::OperandMatchResultTy
2415 MipsAsmParser::parseMemOperand(OperandVector &Operands) {
2416   MCAsmParser &Parser = getParser();
2417   DEBUG(dbgs() << "parseMemOperand\n");
2418   const MCExpr *IdVal = nullptr;
2419   SMLoc S;
2420   bool isParenExpr = false;
2421   MipsAsmParser::OperandMatchResultTy Res = MatchOperand_NoMatch;
2422   // First operand is the offset.
2423   S = Parser.getTok().getLoc();
2424
2425   if (getLexer().getKind() == AsmToken::LParen) {
2426     Parser.Lex();
2427     isParenExpr = true;
2428   }
2429
2430   if (getLexer().getKind() != AsmToken::Dollar) {
2431     if (parseMemOffset(IdVal, isParenExpr))
2432       return MatchOperand_ParseFail;
2433
2434     const AsmToken &Tok = Parser.getTok(); // Get the next token.
2435     if (Tok.isNot(AsmToken::LParen)) {
2436       MipsOperand &Mnemonic = static_cast<MipsOperand &>(*Operands[0]);
2437       if (Mnemonic.getToken() == "la") {
2438         SMLoc E =
2439             SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
2440         Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
2441         return MatchOperand_Success;
2442       }
2443       if (Tok.is(AsmToken::EndOfStatement)) {
2444         SMLoc E =
2445             SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
2446
2447         // Zero register assumed, add a memory operand with ZERO as its base.
2448         // "Base" will be managed by k_Memory.
2449         auto Base = MipsOperand::createGPRReg(0, getContext().getRegisterInfo(),
2450                                               S, E, *this);
2451         Operands.push_back(
2452             MipsOperand::CreateMem(std::move(Base), IdVal, S, E, *this));
2453         return MatchOperand_Success;
2454       }
2455       Error(Parser.getTok().getLoc(), "'(' expected");
2456       return MatchOperand_ParseFail;
2457     }
2458
2459     Parser.Lex(); // Eat the '(' token.
2460   }
2461
2462   Res = parseAnyRegister(Operands);
2463   if (Res != MatchOperand_Success)
2464     return Res;
2465
2466   if (Parser.getTok().isNot(AsmToken::RParen)) {
2467     Error(Parser.getTok().getLoc(), "')' expected");
2468     return MatchOperand_ParseFail;
2469   }
2470
2471   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
2472
2473   Parser.Lex(); // Eat the ')' token.
2474
2475   if (!IdVal)
2476     IdVal = MCConstantExpr::Create(0, getContext());
2477
2478   // Replace the register operand with the memory operand.
2479   std::unique_ptr<MipsOperand> op(
2480       static_cast<MipsOperand *>(Operands.back().release()));
2481   // Remove the register from the operands.
2482   // "op" will be managed by k_Memory.
2483   Operands.pop_back();
2484   // Add the memory operand.
2485   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(IdVal)) {
2486     int64_t Imm;
2487     if (IdVal->EvaluateAsAbsolute(Imm))
2488       IdVal = MCConstantExpr::Create(Imm, getContext());
2489     else if (BE->getLHS()->getKind() != MCExpr::SymbolRef)
2490       IdVal = MCBinaryExpr::Create(BE->getOpcode(), BE->getRHS(), BE->getLHS(),
2491                                    getContext());
2492   }
2493
2494   Operands.push_back(MipsOperand::CreateMem(std::move(op), IdVal, S, E, *this));
2495   return MatchOperand_Success;
2496 }
2497
2498 bool MipsAsmParser::searchSymbolAlias(OperandVector &Operands) {
2499   MCAsmParser &Parser = getParser();
2500   MCSymbol *Sym = getContext().LookupSymbol(Parser.getTok().getIdentifier());
2501   if (Sym) {
2502     SMLoc S = Parser.getTok().getLoc();
2503     const MCExpr *Expr;
2504     if (Sym->isVariable())
2505       Expr = Sym->getVariableValue();
2506     else
2507       return false;
2508     if (Expr->getKind() == MCExpr::SymbolRef) {
2509       const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
2510       StringRef DefSymbol = Ref->getSymbol().getName();
2511       if (DefSymbol.startswith("$")) {
2512         OperandMatchResultTy ResTy =
2513             matchAnyRegisterNameWithoutDollar(Operands, DefSymbol.substr(1), S);
2514         if (ResTy == MatchOperand_Success) {
2515           Parser.Lex();
2516           return true;
2517         } else if (ResTy == MatchOperand_ParseFail)
2518           llvm_unreachable("Should never ParseFail");
2519         return false;
2520       }
2521     } else if (Expr->getKind() == MCExpr::Constant) {
2522       Parser.Lex();
2523       const MCConstantExpr *Const = static_cast<const MCConstantExpr *>(Expr);
2524       Operands.push_back(
2525           MipsOperand::CreateImm(Const, S, Parser.getTok().getLoc(), *this));
2526       return true;
2527     }
2528   }
2529   return false;
2530 }
2531
2532 MipsAsmParser::OperandMatchResultTy
2533 MipsAsmParser::matchAnyRegisterNameWithoutDollar(OperandVector &Operands,
2534                                                  StringRef Identifier,
2535                                                  SMLoc S) {
2536   int Index = matchCPURegisterName(Identifier);
2537   if (Index != -1) {
2538     Operands.push_back(MipsOperand::createGPRReg(
2539         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
2540     return MatchOperand_Success;
2541   }
2542
2543   Index = matchHWRegsRegisterName(Identifier);
2544   if (Index != -1) {
2545     Operands.push_back(MipsOperand::createHWRegsReg(
2546         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
2547     return MatchOperand_Success;
2548   }
2549
2550   Index = matchFPURegisterName(Identifier);
2551   if (Index != -1) {
2552     Operands.push_back(MipsOperand::createFGRReg(
2553         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
2554     return MatchOperand_Success;
2555   }
2556
2557   Index = matchFCCRegisterName(Identifier);
2558   if (Index != -1) {
2559     Operands.push_back(MipsOperand::createFCCReg(
2560         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
2561     return MatchOperand_Success;
2562   }
2563
2564   Index = matchACRegisterName(Identifier);
2565   if (Index != -1) {
2566     Operands.push_back(MipsOperand::createACCReg(
2567         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
2568     return MatchOperand_Success;
2569   }
2570
2571   Index = matchMSA128RegisterName(Identifier);
2572   if (Index != -1) {
2573     Operands.push_back(MipsOperand::createMSA128Reg(
2574         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
2575     return MatchOperand_Success;
2576   }
2577
2578   Index = matchMSA128CtrlRegisterName(Identifier);
2579   if (Index != -1) {
2580     Operands.push_back(MipsOperand::createMSACtrlReg(
2581         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
2582     return MatchOperand_Success;
2583   }
2584
2585   return MatchOperand_NoMatch;
2586 }
2587
2588 MipsAsmParser::OperandMatchResultTy
2589 MipsAsmParser::matchAnyRegisterWithoutDollar(OperandVector &Operands, SMLoc S) {
2590   MCAsmParser &Parser = getParser();
2591   auto Token = Parser.getLexer().peekTok(false);
2592
2593   if (Token.is(AsmToken::Identifier)) {
2594     DEBUG(dbgs() << ".. identifier\n");
2595     StringRef Identifier = Token.getIdentifier();
2596     OperandMatchResultTy ResTy =
2597         matchAnyRegisterNameWithoutDollar(Operands, Identifier, S);
2598     return ResTy;
2599   } else if (Token.is(AsmToken::Integer)) {
2600     DEBUG(dbgs() << ".. integer\n");
2601     Operands.push_back(MipsOperand::createNumericReg(
2602         Token.getIntVal(), getContext().getRegisterInfo(), S, Token.getLoc(),
2603         *this));
2604     return MatchOperand_Success;
2605   }
2606
2607   DEBUG(dbgs() << Parser.getTok().getKind() << "\n");
2608
2609   return MatchOperand_NoMatch;
2610 }
2611
2612 MipsAsmParser::OperandMatchResultTy
2613 MipsAsmParser::parseAnyRegister(OperandVector &Operands) {
2614   MCAsmParser &Parser = getParser();
2615   DEBUG(dbgs() << "parseAnyRegister\n");
2616
2617   auto Token = Parser.getTok();
2618
2619   SMLoc S = Token.getLoc();
2620
2621   if (Token.isNot(AsmToken::Dollar)) {
2622     DEBUG(dbgs() << ".. !$ -> try sym aliasing\n");
2623     if (Token.is(AsmToken::Identifier)) {
2624       if (searchSymbolAlias(Operands))
2625         return MatchOperand_Success;
2626     }
2627     DEBUG(dbgs() << ".. !symalias -> NoMatch\n");
2628     return MatchOperand_NoMatch;
2629   }
2630   DEBUG(dbgs() << ".. $\n");
2631
2632   OperandMatchResultTy ResTy = matchAnyRegisterWithoutDollar(Operands, S);
2633   if (ResTy == MatchOperand_Success) {
2634     Parser.Lex(); // $
2635     Parser.Lex(); // identifier
2636   }
2637   return ResTy;
2638 }
2639
2640 MipsAsmParser::OperandMatchResultTy
2641 MipsAsmParser::parseImm(OperandVector &Operands) {
2642   MCAsmParser &Parser = getParser();
2643   switch (getLexer().getKind()) {
2644   default:
2645     return MatchOperand_NoMatch;
2646   case AsmToken::LParen:
2647   case AsmToken::Minus:
2648   case AsmToken::Plus:
2649   case AsmToken::Integer:
2650   case AsmToken::Tilde:
2651   case AsmToken::String:
2652     break;
2653   }
2654
2655   const MCExpr *IdVal;
2656   SMLoc S = Parser.getTok().getLoc();
2657   if (getParser().parseExpression(IdVal))
2658     return MatchOperand_ParseFail;
2659
2660   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
2661   Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
2662   return MatchOperand_Success;
2663 }
2664
2665 MipsAsmParser::OperandMatchResultTy
2666 MipsAsmParser::parseJumpTarget(OperandVector &Operands) {
2667   MCAsmParser &Parser = getParser();
2668   DEBUG(dbgs() << "parseJumpTarget\n");
2669
2670   SMLoc S = getLexer().getLoc();
2671
2672   // Integers and expressions are acceptable
2673   OperandMatchResultTy ResTy = parseImm(Operands);
2674   if (ResTy != MatchOperand_NoMatch)
2675     return ResTy;
2676
2677   // Registers are a valid target and have priority over symbols.
2678   ResTy = parseAnyRegister(Operands);
2679   if (ResTy != MatchOperand_NoMatch)
2680     return ResTy;
2681
2682   const MCExpr *Expr = nullptr;
2683   if (Parser.parseExpression(Expr)) {
2684     // We have no way of knowing if a symbol was consumed so we must ParseFail
2685     return MatchOperand_ParseFail;
2686   }
2687   Operands.push_back(
2688       MipsOperand::CreateImm(Expr, S, getLexer().getLoc(), *this));
2689   return MatchOperand_Success;
2690 }
2691
2692 MipsAsmParser::OperandMatchResultTy
2693 MipsAsmParser::parseInvNum(OperandVector &Operands) {
2694   MCAsmParser &Parser = getParser();
2695   const MCExpr *IdVal;
2696   // If the first token is '$' we may have register operand.
2697   if (Parser.getTok().is(AsmToken::Dollar))
2698     return MatchOperand_NoMatch;
2699   SMLoc S = Parser.getTok().getLoc();
2700   if (getParser().parseExpression(IdVal))
2701     return MatchOperand_ParseFail;
2702   const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(IdVal);
2703   assert(MCE && "Unexpected MCExpr type.");
2704   int64_t Val = MCE->getValue();
2705   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
2706   Operands.push_back(MipsOperand::CreateImm(
2707       MCConstantExpr::Create(0 - Val, getContext()), S, E, *this));
2708   return MatchOperand_Success;
2709 }
2710
2711 MipsAsmParser::OperandMatchResultTy
2712 MipsAsmParser::parseLSAImm(OperandVector &Operands) {
2713   MCAsmParser &Parser = getParser();
2714   switch (getLexer().getKind()) {
2715   default:
2716     return MatchOperand_NoMatch;
2717   case AsmToken::LParen:
2718   case AsmToken::Plus:
2719   case AsmToken::Minus:
2720   case AsmToken::Integer:
2721     break;
2722   }
2723
2724   const MCExpr *Expr;
2725   SMLoc S = Parser.getTok().getLoc();
2726
2727   if (getParser().parseExpression(Expr))
2728     return MatchOperand_ParseFail;
2729
2730   int64_t Val;
2731   if (!Expr->EvaluateAsAbsolute(Val)) {
2732     Error(S, "expected immediate value");
2733     return MatchOperand_ParseFail;
2734   }
2735
2736   // The LSA instruction allows a 2-bit unsigned immediate. For this reason
2737   // and because the CPU always adds one to the immediate field, the allowed
2738   // range becomes 1..4. We'll only check the range here and will deal
2739   // with the addition/subtraction when actually decoding/encoding
2740   // the instruction.
2741   if (Val < 1 || Val > 4) {
2742     Error(S, "immediate not in range (1..4)");
2743     return MatchOperand_ParseFail;
2744   }
2745
2746   Operands.push_back(
2747       MipsOperand::CreateImm(Expr, S, Parser.getTok().getLoc(), *this));
2748   return MatchOperand_Success;
2749 }
2750
2751 MipsAsmParser::OperandMatchResultTy
2752 MipsAsmParser::parseRegisterList(OperandVector &Operands) {
2753   MCAsmParser &Parser = getParser();
2754   SmallVector<unsigned, 10> Regs;
2755   unsigned RegNo;
2756   unsigned PrevReg = Mips::NoRegister;
2757   bool RegRange = false;
2758   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> TmpOperands;
2759
2760   if (Parser.getTok().isNot(AsmToken::Dollar))
2761     return MatchOperand_ParseFail;
2762
2763   SMLoc S = Parser.getTok().getLoc();
2764   while (parseAnyRegister(TmpOperands) == MatchOperand_Success) {
2765     SMLoc E = getLexer().getLoc();
2766     MipsOperand &Reg = static_cast<MipsOperand &>(*TmpOperands.back());
2767     RegNo = isGP64bit() ? Reg.getGPR64Reg() : Reg.getGPR32Reg();
2768     if (RegRange) {
2769       // Remove last register operand because registers from register range
2770       // should be inserted first.
2771       if (RegNo == Mips::RA) {
2772         Regs.push_back(RegNo);
2773       } else {
2774         unsigned TmpReg = PrevReg + 1;
2775         while (TmpReg <= RegNo) {
2776           if ((TmpReg < Mips::S0) || (TmpReg > Mips::S7)) {
2777             Error(E, "invalid register operand");
2778             return MatchOperand_ParseFail;
2779           }
2780
2781           PrevReg = TmpReg;
2782           Regs.push_back(TmpReg++);
2783         }
2784       }
2785
2786       RegRange = false;
2787     } else {
2788       if ((PrevReg == Mips::NoRegister) && (RegNo != Mips::S0) &&
2789           (RegNo != Mips::RA)) {
2790         Error(E, "$16 or $31 expected");
2791         return MatchOperand_ParseFail;
2792       } else if (((RegNo < Mips::S0) || (RegNo > Mips::S7)) &&
2793                  (RegNo != Mips::FP) && (RegNo != Mips::RA)) {
2794         Error(E, "invalid register operand");
2795         return MatchOperand_ParseFail;
2796       } else if ((PrevReg != Mips::NoRegister) && (RegNo != PrevReg + 1) &&
2797                  (RegNo != Mips::FP) && (RegNo != Mips::RA)) {
2798         Error(E, "consecutive register numbers expected");
2799         return MatchOperand_ParseFail;
2800       }
2801
2802       Regs.push_back(RegNo);
2803     }
2804
2805     if (Parser.getTok().is(AsmToken::Minus))
2806       RegRange = true;
2807
2808     if (!Parser.getTok().isNot(AsmToken::Minus) &&
2809         !Parser.getTok().isNot(AsmToken::Comma)) {
2810       Error(E, "',' or '-' expected");
2811       return MatchOperand_ParseFail;
2812     }
2813
2814     Lex(); // Consume comma or minus
2815     if (Parser.getTok().isNot(AsmToken::Dollar))
2816       break;
2817
2818     PrevReg = RegNo;
2819   }
2820
2821   SMLoc E = Parser.getTok().getLoc();
2822   Operands.push_back(MipsOperand::CreateRegList(Regs, S, E, *this));
2823   parseMemOperand(Operands);
2824   return MatchOperand_Success;
2825 }
2826
2827 MipsAsmParser::OperandMatchResultTy
2828 MipsAsmParser::parseRegisterPair(OperandVector &Operands) {
2829   MCAsmParser &Parser = getParser();
2830
2831   SMLoc S = Parser.getTok().getLoc();
2832   if (parseAnyRegister(Operands) != MatchOperand_Success)
2833     return MatchOperand_ParseFail;
2834
2835   SMLoc E = Parser.getTok().getLoc();
2836   MipsOperand &Op = static_cast<MipsOperand &>(*Operands.back());
2837   unsigned Reg = Op.getGPR32Reg();
2838   Operands.pop_back();
2839   Operands.push_back(MipsOperand::CreateRegPair(Reg, S, E, *this));
2840   return MatchOperand_Success;
2841 }
2842
2843 MCSymbolRefExpr::VariantKind MipsAsmParser::getVariantKind(StringRef Symbol) {
2844
2845   MCSymbolRefExpr::VariantKind VK =
2846       StringSwitch<MCSymbolRefExpr::VariantKind>(Symbol)
2847           .Case("hi", MCSymbolRefExpr::VK_Mips_ABS_HI)
2848           .Case("lo", MCSymbolRefExpr::VK_Mips_ABS_LO)
2849           .Case("gp_rel", MCSymbolRefExpr::VK_Mips_GPREL)
2850           .Case("call16", MCSymbolRefExpr::VK_Mips_GOT_CALL)
2851           .Case("got", MCSymbolRefExpr::VK_Mips_GOT)
2852           .Case("tlsgd", MCSymbolRefExpr::VK_Mips_TLSGD)
2853           .Case("tlsldm", MCSymbolRefExpr::VK_Mips_TLSLDM)
2854           .Case("dtprel_hi", MCSymbolRefExpr::VK_Mips_DTPREL_HI)
2855           .Case("dtprel_lo", MCSymbolRefExpr::VK_Mips_DTPREL_LO)
2856           .Case("gottprel", MCSymbolRefExpr::VK_Mips_GOTTPREL)
2857           .Case("tprel_hi", MCSymbolRefExpr::VK_Mips_TPREL_HI)
2858           .Case("tprel_lo", MCSymbolRefExpr::VK_Mips_TPREL_LO)
2859           .Case("got_disp", MCSymbolRefExpr::VK_Mips_GOT_DISP)
2860           .Case("got_page", MCSymbolRefExpr::VK_Mips_GOT_PAGE)
2861           .Case("got_ofst", MCSymbolRefExpr::VK_Mips_GOT_OFST)
2862           .Case("hi(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_HI)
2863           .Case("lo(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_LO)
2864           .Case("got_hi", MCSymbolRefExpr::VK_Mips_GOT_HI16)
2865           .Case("got_lo", MCSymbolRefExpr::VK_Mips_GOT_LO16)
2866           .Case("call_hi", MCSymbolRefExpr::VK_Mips_CALL_HI16)
2867           .Case("call_lo", MCSymbolRefExpr::VK_Mips_CALL_LO16)
2868           .Case("higher", MCSymbolRefExpr::VK_Mips_HIGHER)
2869           .Case("highest", MCSymbolRefExpr::VK_Mips_HIGHEST)
2870           .Case("pcrel_hi", MCSymbolRefExpr::VK_Mips_PCREL_HI16)
2871           .Case("pcrel_lo", MCSymbolRefExpr::VK_Mips_PCREL_LO16)
2872           .Default(MCSymbolRefExpr::VK_None);
2873
2874   assert(VK != MCSymbolRefExpr::VK_None);
2875
2876   return VK;
2877 }
2878
2879 /// Sometimes (i.e. load/stores) the operand may be followed immediately by
2880 /// either this.
2881 /// ::= '(', register, ')'
2882 /// handle it before we iterate so we don't get tripped up by the lack of
2883 /// a comma.
2884 bool MipsAsmParser::parseParenSuffix(StringRef Name, OperandVector &Operands) {
2885   MCAsmParser &Parser = getParser();
2886   if (getLexer().is(AsmToken::LParen)) {
2887     Operands.push_back(
2888         MipsOperand::CreateToken("(", getLexer().getLoc(), *this));
2889     Parser.Lex();
2890     if (parseOperand(Operands, Name)) {
2891       SMLoc Loc = getLexer().getLoc();
2892       Parser.eatToEndOfStatement();
2893       return Error(Loc, "unexpected token in argument list");
2894     }
2895     if (Parser.getTok().isNot(AsmToken::RParen)) {
2896       SMLoc Loc = getLexer().getLoc();
2897       Parser.eatToEndOfStatement();
2898       return Error(Loc, "unexpected token, expected ')'");
2899     }
2900     Operands.push_back(
2901         MipsOperand::CreateToken(")", getLexer().getLoc(), *this));
2902     Parser.Lex();
2903   }
2904   return false;
2905 }
2906
2907 /// Sometimes (i.e. in MSA) the operand may be followed immediately by
2908 /// either one of these.
2909 /// ::= '[', register, ']'
2910 /// ::= '[', integer, ']'
2911 /// handle it before we iterate so we don't get tripped up by the lack of
2912 /// a comma.
2913 bool MipsAsmParser::parseBracketSuffix(StringRef Name,
2914                                        OperandVector &Operands) {
2915   MCAsmParser &Parser = getParser();
2916   if (getLexer().is(AsmToken::LBrac)) {
2917     Operands.push_back(
2918         MipsOperand::CreateToken("[", getLexer().getLoc(), *this));
2919     Parser.Lex();
2920     if (parseOperand(Operands, Name)) {
2921       SMLoc Loc = getLexer().getLoc();
2922       Parser.eatToEndOfStatement();
2923       return Error(Loc, "unexpected token in argument list");
2924     }
2925     if (Parser.getTok().isNot(AsmToken::RBrac)) {
2926       SMLoc Loc = getLexer().getLoc();
2927       Parser.eatToEndOfStatement();
2928       return Error(Loc, "unexpected token, expected ']'");
2929     }
2930     Operands.push_back(
2931         MipsOperand::CreateToken("]", getLexer().getLoc(), *this));
2932     Parser.Lex();
2933   }
2934   return false;
2935 }
2936
2937 bool MipsAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
2938                                      SMLoc NameLoc, OperandVector &Operands) {
2939   MCAsmParser &Parser = getParser();
2940   DEBUG(dbgs() << "ParseInstruction\n");
2941
2942   // We have reached first instruction, module directive are now forbidden.
2943   getTargetStreamer().forbidModuleDirective();
2944
2945   // Check if we have valid mnemonic
2946   if (!mnemonicIsValid(Name, 0)) {
2947     Parser.eatToEndOfStatement();
2948     return Error(NameLoc, "unknown instruction");
2949   }
2950   // First operand in MCInst is instruction mnemonic.
2951   Operands.push_back(MipsOperand::CreateToken(Name, NameLoc, *this));
2952
2953   // Read the remaining operands.
2954   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2955     // Read the first operand.
2956     if (parseOperand(Operands, Name)) {
2957       SMLoc Loc = getLexer().getLoc();
2958       Parser.eatToEndOfStatement();
2959       return Error(Loc, "unexpected token in argument list");
2960     }
2961     if (getLexer().is(AsmToken::LBrac) && parseBracketSuffix(Name, Operands))
2962       return true;
2963     // AFAIK, parenthesis suffixes are never on the first operand
2964
2965     while (getLexer().is(AsmToken::Comma)) {
2966       Parser.Lex(); // Eat the comma.
2967       // Parse and remember the operand.
2968       if (parseOperand(Operands, Name)) {
2969         SMLoc Loc = getLexer().getLoc();
2970         Parser.eatToEndOfStatement();
2971         return Error(Loc, "unexpected token in argument list");
2972       }
2973       // Parse bracket and parenthesis suffixes before we iterate
2974       if (getLexer().is(AsmToken::LBrac)) {
2975         if (parseBracketSuffix(Name, Operands))
2976           return true;
2977       } else if (getLexer().is(AsmToken::LParen) &&
2978                  parseParenSuffix(Name, Operands))
2979         return true;
2980     }
2981   }
2982   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2983     SMLoc Loc = getLexer().getLoc();
2984     Parser.eatToEndOfStatement();
2985     return Error(Loc, "unexpected token in argument list");
2986   }
2987   Parser.Lex(); // Consume the EndOfStatement.
2988   return false;
2989 }
2990
2991 bool MipsAsmParser::reportParseError(Twine ErrorMsg) {
2992   MCAsmParser &Parser = getParser();
2993   SMLoc Loc = getLexer().getLoc();
2994   Parser.eatToEndOfStatement();
2995   return Error(Loc, ErrorMsg);
2996 }
2997
2998 bool MipsAsmParser::reportParseError(SMLoc Loc, Twine ErrorMsg) {
2999   return Error(Loc, ErrorMsg);
3000 }
3001
3002 bool MipsAsmParser::parseSetNoAtDirective() {
3003   MCAsmParser &Parser = getParser();
3004   // Line should look like: ".set noat".
3005   // set at reg to 0.
3006   AssemblerOptions.back()->setATReg(0);
3007   // eat noat
3008   Parser.Lex();
3009   // If this is not the end of the statement, report an error.
3010   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3011     reportParseError("unexpected token, expected end of statement");
3012     return false;
3013   }
3014   Parser.Lex(); // Consume the EndOfStatement.
3015   return false;
3016 }
3017
3018 bool MipsAsmParser::parseSetAtDirective() {
3019   MCAsmParser &Parser = getParser();
3020   // Line can be .set at - defaults to $1
3021   // or .set at=$reg
3022   int AtRegNo;
3023   getParser().Lex();
3024   if (getLexer().is(AsmToken::EndOfStatement)) {
3025     AssemblerOptions.back()->setATReg(1);
3026     Parser.Lex(); // Consume the EndOfStatement.
3027     return false;
3028   } else if (getLexer().is(AsmToken::Equal)) {
3029     getParser().Lex(); // Eat the '='.
3030     if (getLexer().isNot(AsmToken::Dollar)) {
3031       reportParseError("unexpected token, expected dollar sign '$'");
3032       return false;
3033     }
3034     Parser.Lex(); // Eat the '$'.
3035     const AsmToken &Reg = Parser.getTok();
3036     if (Reg.is(AsmToken::Identifier)) {
3037       AtRegNo = matchCPURegisterName(Reg.getIdentifier());
3038     } else if (Reg.is(AsmToken::Integer)) {
3039       AtRegNo = Reg.getIntVal();
3040     } else {
3041       reportParseError("unexpected token, expected identifier or integer");
3042       return false;
3043     }
3044
3045     if (AtRegNo < 0 || AtRegNo > 31) {
3046       reportParseError("unexpected token in statement");
3047       return false;
3048     }
3049
3050     if (!AssemblerOptions.back()->setATReg(AtRegNo)) {
3051       reportParseError("invalid register");
3052       return false;
3053     }
3054     getParser().Lex(); // Eat the register.
3055
3056     if (getLexer().isNot(AsmToken::EndOfStatement)) {
3057       reportParseError("unexpected token, expected end of statement");
3058       return false;
3059     }
3060     Parser.Lex(); // Consume the EndOfStatement.
3061     return false;
3062   } else {
3063     reportParseError("unexpected token in statement");
3064     return false;
3065   }
3066 }
3067
3068 bool MipsAsmParser::parseSetReorderDirective() {
3069   MCAsmParser &Parser = getParser();
3070   Parser.Lex();
3071   // If this is not the end of the statement, report an error.
3072   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3073     reportParseError("unexpected token, expected end of statement");
3074     return false;
3075   }
3076   AssemblerOptions.back()->setReorder();
3077   getTargetStreamer().emitDirectiveSetReorder();
3078   Parser.Lex(); // Consume the EndOfStatement.
3079   return false;
3080 }
3081
3082 bool MipsAsmParser::parseSetNoReorderDirective() {
3083   MCAsmParser &Parser = getParser();
3084   Parser.Lex();
3085   // If this is not the end of the statement, report an error.
3086   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3087     reportParseError("unexpected token, expected end of statement");
3088     return false;
3089   }
3090   AssemblerOptions.back()->setNoReorder();
3091   getTargetStreamer().emitDirectiveSetNoReorder();
3092   Parser.Lex(); // Consume the EndOfStatement.
3093   return false;
3094 }
3095
3096 bool MipsAsmParser::parseSetMacroDirective() {
3097   MCAsmParser &Parser = getParser();
3098   Parser.Lex();
3099   // If this is not the end of the statement, report an error.
3100   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3101     reportParseError("unexpected token, expected end of statement");
3102     return false;
3103   }
3104   AssemblerOptions.back()->setMacro();
3105   Parser.Lex(); // Consume the EndOfStatement.
3106   return false;
3107 }
3108
3109 bool MipsAsmParser::parseSetNoMacroDirective() {
3110   MCAsmParser &Parser = getParser();
3111   Parser.Lex();
3112   // If this is not the end of the statement, report an error.
3113   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3114     reportParseError("unexpected token, expected end of statement");
3115     return false;
3116   }
3117   if (AssemblerOptions.back()->isReorder()) {
3118     reportParseError("`noreorder' must be set before `nomacro'");
3119     return false;
3120   }
3121   AssemblerOptions.back()->setNoMacro();
3122   Parser.Lex(); // Consume the EndOfStatement.
3123   return false;
3124 }
3125
3126 bool MipsAsmParser::parseSetMsaDirective() {
3127   MCAsmParser &Parser = getParser();
3128   Parser.Lex();
3129
3130   // If this is not the end of the statement, report an error.
3131   if (getLexer().isNot(AsmToken::EndOfStatement))
3132     return reportParseError("unexpected token, expected end of statement");
3133
3134   setFeatureBits(Mips::FeatureMSA, "msa");
3135   getTargetStreamer().emitDirectiveSetMsa();
3136   return false;
3137 }
3138
3139 bool MipsAsmParser::parseSetNoMsaDirective() {
3140   MCAsmParser &Parser = getParser();
3141   Parser.Lex();
3142
3143   // If this is not the end of the statement, report an error.
3144   if (getLexer().isNot(AsmToken::EndOfStatement))
3145     return reportParseError("unexpected token, expected end of statement");
3146
3147   clearFeatureBits(Mips::FeatureMSA, "msa");
3148   getTargetStreamer().emitDirectiveSetNoMsa();
3149   return false;
3150 }
3151
3152 bool MipsAsmParser::parseSetNoDspDirective() {
3153   MCAsmParser &Parser = getParser();
3154   Parser.Lex(); // Eat "nodsp".
3155
3156   // If this is not the end of the statement, report an error.
3157   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3158     reportParseError("unexpected token, expected end of statement");
3159     return false;
3160   }
3161
3162   clearFeatureBits(Mips::FeatureDSP, "dsp");
3163   getTargetStreamer().emitDirectiveSetNoDsp();
3164   return false;
3165 }
3166
3167 bool MipsAsmParser::parseSetMips16Directive() {
3168   MCAsmParser &Parser = getParser();
3169   Parser.Lex(); // Eat "mips16".
3170
3171   // If this is not the end of the statement, report an error.
3172   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3173     reportParseError("unexpected token, expected end of statement");
3174     return false;
3175   }
3176
3177   setFeatureBits(Mips::FeatureMips16, "mips16");
3178   getTargetStreamer().emitDirectiveSetMips16();
3179   Parser.Lex(); // Consume the EndOfStatement.
3180   return false;
3181 }
3182
3183 bool MipsAsmParser::parseSetNoMips16Directive() {
3184   MCAsmParser &Parser = getParser();
3185   Parser.Lex(); // Eat "nomips16".
3186
3187   // If this is not the end of the statement, report an error.
3188   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3189     reportParseError("unexpected token, expected end of statement");
3190     return false;
3191   }
3192
3193   clearFeatureBits(Mips::FeatureMips16, "mips16");
3194   getTargetStreamer().emitDirectiveSetNoMips16();
3195   Parser.Lex(); // Consume the EndOfStatement.
3196   return false;
3197 }
3198
3199 bool MipsAsmParser::parseSetFpDirective() {
3200   MCAsmParser &Parser = getParser();
3201   MipsABIFlagsSection::FpABIKind FpAbiVal;
3202   // Line can be: .set fp=32
3203   //              .set fp=xx
3204   //              .set fp=64
3205   Parser.Lex(); // Eat fp token
3206   AsmToken Tok = Parser.getTok();
3207   if (Tok.isNot(AsmToken::Equal)) {
3208     reportParseError("unexpected token, expected equals sign '='");
3209     return false;
3210   }
3211   Parser.Lex(); // Eat '=' token.
3212   Tok = Parser.getTok();
3213
3214   if (!parseFpABIValue(FpAbiVal, ".set"))
3215     return false;
3216
3217   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3218     reportParseError("unexpected token, expected end of statement");
3219     return false;
3220   }
3221   getTargetStreamer().emitDirectiveSetFp(FpAbiVal);
3222   Parser.Lex(); // Consume the EndOfStatement.
3223   return false;
3224 }
3225
3226 bool MipsAsmParser::parseSetPopDirective() {
3227   MCAsmParser &Parser = getParser();
3228   SMLoc Loc = getLexer().getLoc();
3229
3230   Parser.Lex();
3231   if (getLexer().isNot(AsmToken::EndOfStatement))
3232     return reportParseError("unexpected token, expected end of statement");
3233
3234   // Always keep an element on the options "stack" to prevent the user
3235   // from changing the initial options. This is how we remember them.
3236   if (AssemblerOptions.size() == 2)
3237     return reportParseError(Loc, ".set pop with no .set push");
3238
3239   AssemblerOptions.pop_back();
3240   setAvailableFeatures(AssemblerOptions.back()->getFeatures());
3241
3242   getTargetStreamer().emitDirectiveSetPop();
3243   return false;
3244 }
3245
3246 bool MipsAsmParser::parseSetPushDirective() {
3247   MCAsmParser &Parser = getParser();
3248   Parser.Lex();
3249   if (getLexer().isNot(AsmToken::EndOfStatement))
3250     return reportParseError("unexpected token, expected end of statement");
3251
3252   // Create a copy of the current assembler options environment and push it.
3253   AssemblerOptions.push_back(
3254               make_unique<MipsAssemblerOptions>(AssemblerOptions.back().get()));
3255
3256   getTargetStreamer().emitDirectiveSetPush();
3257   return false;
3258 }
3259
3260 bool MipsAsmParser::parseSetAssignment() {
3261   StringRef Name;
3262   const MCExpr *Value;
3263   MCAsmParser &Parser = getParser();
3264
3265   if (Parser.parseIdentifier(Name))
3266     reportParseError("expected identifier after .set");
3267
3268   if (getLexer().isNot(AsmToken::Comma))
3269     return reportParseError("unexpected token, expected comma");
3270   Lex(); // Eat comma
3271
3272   if (Parser.parseExpression(Value))
3273     return reportParseError("expected valid expression after comma");
3274
3275   // Check if the Name already exists as a symbol.
3276   MCSymbol *Sym = getContext().LookupSymbol(Name);
3277   if (Sym)
3278     return reportParseError("symbol already defined");
3279   Sym = getContext().GetOrCreateSymbol(Name);
3280   Sym->setVariableValue(Value);
3281
3282   return false;
3283 }
3284
3285 bool MipsAsmParser::parseSetMips0Directive() {
3286   MCAsmParser &Parser = getParser();
3287   Parser.Lex();
3288   if (getLexer().isNot(AsmToken::EndOfStatement))
3289     return reportParseError("unexpected token, expected end of statement");
3290
3291   // Reset assembler options to their initial values.
3292   setAvailableFeatures(AssemblerOptions.front()->getFeatures());
3293   AssemblerOptions.back()->setFeatures(AssemblerOptions.front()->getFeatures());
3294
3295   getTargetStreamer().emitDirectiveSetMips0();
3296   return false;
3297 }
3298
3299 bool MipsAsmParser::parseSetArchDirective() {
3300   MCAsmParser &Parser = getParser();
3301   Parser.Lex();
3302   if (getLexer().isNot(AsmToken::Equal))
3303     return reportParseError("unexpected token, expected equals sign");
3304
3305   Parser.Lex();
3306   StringRef Arch;
3307   if (Parser.parseIdentifier(Arch))
3308     return reportParseError("expected arch identifier");
3309
3310   StringRef ArchFeatureName =
3311       StringSwitch<StringRef>(Arch)
3312           .Case("mips1", "mips1")
3313           .Case("mips2", "mips2")
3314           .Case("mips3", "mips3")
3315           .Case("mips4", "mips4")
3316           .Case("mips5", "mips5")
3317           .Case("mips32", "mips32")
3318           .Case("mips32r2", "mips32r2")
3319           .Case("mips32r6", "mips32r6")
3320           .Case("mips64", "mips64")
3321           .Case("mips64r2", "mips64r2")
3322           .Case("mips64r6", "mips64r6")
3323           .Case("cnmips", "cnmips")
3324           .Case("r4000", "mips3") // This is an implementation of Mips3.
3325           .Default("");
3326
3327   if (ArchFeatureName.empty())
3328     return reportParseError("unsupported architecture");
3329
3330   selectArch(ArchFeatureName);
3331   getTargetStreamer().emitDirectiveSetArch(Arch);
3332   return false;
3333 }
3334
3335 bool MipsAsmParser::parseSetFeature(uint64_t Feature) {
3336   MCAsmParser &Parser = getParser();
3337   Parser.Lex();
3338   if (getLexer().isNot(AsmToken::EndOfStatement))
3339     return reportParseError("unexpected token, expected end of statement");
3340
3341   switch (Feature) {
3342   default:
3343     llvm_unreachable("Unimplemented feature");
3344   case Mips::FeatureDSP:
3345     setFeatureBits(Mips::FeatureDSP, "dsp");
3346     getTargetStreamer().emitDirectiveSetDsp();
3347     break;
3348   case Mips::FeatureMicroMips:
3349     getTargetStreamer().emitDirectiveSetMicroMips();
3350     break;
3351   case Mips::FeatureMips1:
3352     selectArch("mips1");
3353     getTargetStreamer().emitDirectiveSetMips1();
3354     break;
3355   case Mips::FeatureMips2:
3356     selectArch("mips2");
3357     getTargetStreamer().emitDirectiveSetMips2();
3358     break;
3359   case Mips::FeatureMips3:
3360     selectArch("mips3");
3361     getTargetStreamer().emitDirectiveSetMips3();
3362     break;
3363   case Mips::FeatureMips4:
3364     selectArch("mips4");
3365     getTargetStreamer().emitDirectiveSetMips4();
3366     break;
3367   case Mips::FeatureMips5:
3368     selectArch("mips5");
3369     getTargetStreamer().emitDirectiveSetMips5();
3370     break;
3371   case Mips::FeatureMips32:
3372     selectArch("mips32");
3373     getTargetStreamer().emitDirectiveSetMips32();
3374     break;
3375   case Mips::FeatureMips32r2:
3376     selectArch("mips32r2");
3377     getTargetStreamer().emitDirectiveSetMips32R2();
3378     break;
3379   case Mips::FeatureMips32r6:
3380     selectArch("mips32r6");
3381     getTargetStreamer().emitDirectiveSetMips32R6();
3382     break;
3383   case Mips::FeatureMips64:
3384     selectArch("mips64");
3385     getTargetStreamer().emitDirectiveSetMips64();
3386     break;
3387   case Mips::FeatureMips64r2:
3388     selectArch("mips64r2");
3389     getTargetStreamer().emitDirectiveSetMips64R2();
3390     break;
3391   case Mips::FeatureMips64r6:
3392     selectArch("mips64r6");
3393     getTargetStreamer().emitDirectiveSetMips64R6();
3394     break;
3395   }
3396   return false;
3397 }
3398
3399 bool MipsAsmParser::eatComma(StringRef ErrorStr) {
3400   MCAsmParser &Parser = getParser();
3401   if (getLexer().isNot(AsmToken::Comma)) {
3402     SMLoc Loc = getLexer().getLoc();
3403     Parser.eatToEndOfStatement();
3404     return Error(Loc, ErrorStr);
3405   }
3406
3407   Parser.Lex(); // Eat the comma.
3408   return true;
3409 }
3410
3411 bool MipsAsmParser::parseDirectiveCpLoad(SMLoc Loc) {
3412   if (AssemblerOptions.back()->isReorder())
3413     Warning(Loc, ".cpload should be inside a noreorder section");
3414
3415   if (inMips16Mode()) {
3416     reportParseError(".cpload is not supported in Mips16 mode");
3417     return false;
3418   }
3419
3420   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Reg;
3421   OperandMatchResultTy ResTy = parseAnyRegister(Reg);
3422   if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
3423     reportParseError("expected register containing function address");
3424     return false;
3425   }
3426
3427   MipsOperand &RegOpnd = static_cast<MipsOperand &>(*Reg[0]);
3428   if (!RegOpnd.isGPRAsmReg()) {
3429     reportParseError(RegOpnd.getStartLoc(), "invalid register");
3430     return false;
3431   }
3432
3433   // If this is not the end of the statement, report an error.
3434   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3435     reportParseError("unexpected token, expected end of statement");
3436     return false;
3437   }
3438
3439   getTargetStreamer().emitDirectiveCpLoad(RegOpnd.getGPR32Reg());
3440   return false;
3441 }
3442
3443 bool MipsAsmParser::parseDirectiveCPSetup() {
3444   MCAsmParser &Parser = getParser();
3445   unsigned FuncReg;
3446   unsigned Save;
3447   bool SaveIsReg = true;
3448
3449   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> TmpReg;
3450   OperandMatchResultTy ResTy = parseAnyRegister(TmpReg);
3451   if (ResTy == MatchOperand_NoMatch) {
3452     reportParseError("expected register containing function address");
3453     Parser.eatToEndOfStatement();
3454     return false;
3455   }
3456
3457   MipsOperand &FuncRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
3458   if (!FuncRegOpnd.isGPRAsmReg()) {
3459     reportParseError(FuncRegOpnd.getStartLoc(), "invalid register");
3460     Parser.eatToEndOfStatement();
3461     return false;
3462   }
3463
3464   FuncReg = FuncRegOpnd.getGPR32Reg();
3465   TmpReg.clear();
3466
3467   if (!eatComma("unexpected token, expected comma"))
3468     return true;
3469
3470   ResTy = parseAnyRegister(TmpReg);
3471   if (ResTy == MatchOperand_NoMatch) {
3472     const AsmToken &Tok = Parser.getTok();
3473     if (Tok.is(AsmToken::Integer)) {
3474       Save = Tok.getIntVal();
3475       SaveIsReg = false;
3476       Parser.Lex();
3477     } else {
3478       reportParseError("expected save register or stack offset");
3479       Parser.eatToEndOfStatement();
3480       return false;
3481     }
3482   } else {
3483     MipsOperand &SaveOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
3484     if (!SaveOpnd.isGPRAsmReg()) {
3485       reportParseError(SaveOpnd.getStartLoc(), "invalid register");
3486       Parser.eatToEndOfStatement();
3487       return false;
3488     }
3489     Save = SaveOpnd.getGPR32Reg();
3490   }
3491
3492   if (!eatComma("unexpected token, expected comma"))
3493     return true;
3494
3495   StringRef Name;
3496   if (Parser.parseIdentifier(Name))
3497     reportParseError("expected identifier");
3498   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3499
3500   getTargetStreamer().emitDirectiveCpsetup(FuncReg, Save, *Sym, SaveIsReg);
3501   return false;
3502 }
3503
3504 bool MipsAsmParser::parseDirectiveNaN() {
3505   MCAsmParser &Parser = getParser();
3506   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3507     const AsmToken &Tok = Parser.getTok();
3508
3509     if (Tok.getString() == "2008") {
3510       Parser.Lex();
3511       getTargetStreamer().emitDirectiveNaN2008();
3512       return false;
3513     } else if (Tok.getString() == "legacy") {
3514       Parser.Lex();
3515       getTargetStreamer().emitDirectiveNaNLegacy();
3516       return false;
3517     }
3518   }
3519   // If we don't recognize the option passed to the .nan
3520   // directive (e.g. no option or unknown option), emit an error.
3521   reportParseError("invalid option in .nan directive");
3522   return false;
3523 }
3524
3525 bool MipsAsmParser::parseDirectiveSet() {
3526   MCAsmParser &Parser = getParser();
3527   // Get the next token.
3528   const AsmToken &Tok = Parser.getTok();
3529
3530   if (Tok.getString() == "noat") {
3531     return parseSetNoAtDirective();
3532   } else if (Tok.getString() == "at") {
3533     return parseSetAtDirective();
3534   } else if (Tok.getString() == "arch") {
3535     return parseSetArchDirective();
3536   } else if (Tok.getString() == "fp") {
3537     return parseSetFpDirective();
3538   } else if (Tok.getString() == "pop") {
3539     return parseSetPopDirective();
3540   } else if (Tok.getString() == "push") {
3541     return parseSetPushDirective();
3542   } else if (Tok.getString() == "reorder") {
3543     return parseSetReorderDirective();
3544   } else if (Tok.getString() == "noreorder") {
3545     return parseSetNoReorderDirective();
3546   } else if (Tok.getString() == "macro") {
3547     return parseSetMacroDirective();
3548   } else if (Tok.getString() == "nomacro") {
3549     return parseSetNoMacroDirective();
3550   } else if (Tok.getString() == "mips16") {
3551     return parseSetMips16Directive();
3552   } else if (Tok.getString() == "nomips16") {
3553     return parseSetNoMips16Directive();
3554   } else if (Tok.getString() == "nomicromips") {
3555     getTargetStreamer().emitDirectiveSetNoMicroMips();
3556     Parser.eatToEndOfStatement();
3557     return false;
3558   } else if (Tok.getString() == "micromips") {
3559     return parseSetFeature(Mips::FeatureMicroMips);
3560   } else if (Tok.getString() == "mips0") {
3561     return parseSetMips0Directive();
3562   } else if (Tok.getString() == "mips1") {
3563     return parseSetFeature(Mips::FeatureMips1);
3564   } else if (Tok.getString() == "mips2") {
3565     return parseSetFeature(Mips::FeatureMips2);
3566   } else if (Tok.getString() == "mips3") {
3567     return parseSetFeature(Mips::FeatureMips3);
3568   } else if (Tok.getString() == "mips4") {
3569     return parseSetFeature(Mips::FeatureMips4);
3570   } else if (Tok.getString() == "mips5") {
3571     return parseSetFeature(Mips::FeatureMips5);
3572   } else if (Tok.getString() == "mips32") {
3573     return parseSetFeature(Mips::FeatureMips32);
3574   } else if (Tok.getString() == "mips32r2") {
3575     return parseSetFeature(Mips::FeatureMips32r2);
3576   } else if (Tok.getString() == "mips32r6") {
3577     return parseSetFeature(Mips::FeatureMips32r6);
3578   } else if (Tok.getString() == "mips64") {
3579     return parseSetFeature(Mips::FeatureMips64);
3580   } else if (Tok.getString() == "mips64r2") {
3581     return parseSetFeature(Mips::FeatureMips64r2);
3582   } else if (Tok.getString() == "mips64r6") {
3583     return parseSetFeature(Mips::FeatureMips64r6);
3584   } else if (Tok.getString() == "dsp") {
3585     return parseSetFeature(Mips::FeatureDSP);
3586   } else if (Tok.getString() == "nodsp") {
3587     return parseSetNoDspDirective();
3588   } else if (Tok.getString() == "msa") {
3589     return parseSetMsaDirective();
3590   } else if (Tok.getString() == "nomsa") {
3591     return parseSetNoMsaDirective();
3592   } else {
3593     // It is just an identifier, look for an assignment.
3594     parseSetAssignment();
3595     return false;
3596   }
3597
3598   return true;
3599 }
3600
3601 /// parseDataDirective
3602 ///  ::= .word [ expression (, expression)* ]
3603 bool MipsAsmParser::parseDataDirective(unsigned Size, SMLoc L) {
3604   MCAsmParser &Parser = getParser();
3605   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3606     for (;;) {
3607       const MCExpr *Value;
3608       if (getParser().parseExpression(Value))
3609         return true;
3610
3611       getParser().getStreamer().EmitValue(Value, Size);
3612
3613       if (getLexer().is(AsmToken::EndOfStatement))
3614         break;
3615
3616       if (getLexer().isNot(AsmToken::Comma))
3617         return Error(L, "unexpected token, expected comma");
3618       Parser.Lex();
3619     }
3620   }
3621
3622   Parser.Lex();
3623   return false;
3624 }
3625
3626 /// parseDirectiveGpWord
3627 ///  ::= .gpword local_sym
3628 bool MipsAsmParser::parseDirectiveGpWord() {
3629   MCAsmParser &Parser = getParser();
3630   const MCExpr *Value;
3631   // EmitGPRel32Value requires an expression, so we are using base class
3632   // method to evaluate the expression.
3633   if (getParser().parseExpression(Value))
3634     return true;
3635   getParser().getStreamer().EmitGPRel32Value(Value);
3636
3637   if (getLexer().isNot(AsmToken::EndOfStatement))
3638     return Error(getLexer().getLoc(), 
3639                 "unexpected token, expected end of statement");
3640   Parser.Lex(); // Eat EndOfStatement token.
3641   return false;
3642 }
3643
3644 /// parseDirectiveGpDWord
3645 ///  ::= .gpdword local_sym
3646 bool MipsAsmParser::parseDirectiveGpDWord() {
3647   MCAsmParser &Parser = getParser();
3648   const MCExpr *Value;
3649   // EmitGPRel64Value requires an expression, so we are using base class
3650   // method to evaluate the expression.
3651   if (getParser().parseExpression(Value))
3652     return true;
3653   getParser().getStreamer().EmitGPRel64Value(Value);
3654
3655   if (getLexer().isNot(AsmToken::EndOfStatement))
3656     return Error(getLexer().getLoc(), 
3657                 "unexpected token, expected end of statement");
3658   Parser.Lex(); // Eat EndOfStatement token.
3659   return false;
3660 }
3661
3662 bool MipsAsmParser::parseDirectiveOption() {
3663   MCAsmParser &Parser = getParser();
3664   // Get the option token.
3665   AsmToken Tok = Parser.getTok();
3666   // At the moment only identifiers are supported.
3667   if (Tok.isNot(AsmToken::Identifier)) {
3668     Error(Parser.getTok().getLoc(), "unexpected token, expected identifier");
3669     Parser.eatToEndOfStatement();
3670     return false;
3671   }
3672
3673   StringRef Option = Tok.getIdentifier();
3674
3675   if (Option == "pic0") {
3676     getTargetStreamer().emitDirectiveOptionPic0();
3677     Parser.Lex();
3678     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
3679       Error(Parser.getTok().getLoc(),
3680             "unexpected token, expected end of statement");
3681       Parser.eatToEndOfStatement();
3682     }
3683     return false;
3684   }
3685
3686   if (Option == "pic2") {
3687     getTargetStreamer().emitDirectiveOptionPic2();
3688     Parser.Lex();
3689     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
3690       Error(Parser.getTok().getLoc(),
3691             "unexpected token, expected end of statement");
3692       Parser.eatToEndOfStatement();
3693     }
3694     return false;
3695   }
3696
3697   // Unknown option.
3698   Warning(Parser.getTok().getLoc(), 
3699           "unknown option, expected 'pic0' or 'pic2'");
3700   Parser.eatToEndOfStatement();
3701   return false;
3702 }
3703
3704 /// parseDirectiveModule
3705 ///  ::= .module oddspreg
3706 ///  ::= .module nooddspreg
3707 ///  ::= .module fp=value
3708 bool MipsAsmParser::parseDirectiveModule() {
3709   MCAsmParser &Parser = getParser();
3710   MCAsmLexer &Lexer = getLexer();
3711   SMLoc L = Lexer.getLoc();
3712
3713   if (!getTargetStreamer().isModuleDirectiveAllowed()) {
3714     // TODO : get a better message.
3715     reportParseError(".module directive must appear before any code");
3716     return false;
3717   }
3718
3719   if (Lexer.is(AsmToken::Identifier)) {
3720     StringRef Option = Parser.getTok().getString();
3721     Parser.Lex();
3722
3723     if (Option == "oddspreg") {
3724       getTargetStreamer().emitDirectiveModuleOddSPReg(true, isABI_O32());
3725       clearFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
3726
3727       if (getLexer().isNot(AsmToken::EndOfStatement)) {
3728         reportParseError("unexpected token, expected end of statement");
3729         return false;
3730       }
3731
3732       return false;
3733     } else if (Option == "nooddspreg") {
3734       if (!isABI_O32()) {
3735         Error(L, "'.module nooddspreg' requires the O32 ABI");
3736         return false;
3737       }
3738
3739       getTargetStreamer().emitDirectiveModuleOddSPReg(false, isABI_O32());
3740       setFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
3741
3742       if (getLexer().isNot(AsmToken::EndOfStatement)) {
3743         reportParseError("unexpected token, expected end of statement");
3744         return false;
3745       }
3746
3747       return false;
3748     } else if (Option == "fp") {
3749       return parseDirectiveModuleFP();
3750     }
3751
3752     return Error(L, "'" + Twine(Option) + "' is not a valid .module option.");
3753   }
3754
3755   return false;
3756 }
3757
3758 /// parseDirectiveModuleFP
3759 ///  ::= =32
3760 ///  ::= =xx
3761 ///  ::= =64
3762 bool MipsAsmParser::parseDirectiveModuleFP() {
3763   MCAsmParser &Parser = getParser();
3764   MCAsmLexer &Lexer = getLexer();
3765
3766   if (Lexer.isNot(AsmToken::Equal)) {
3767     reportParseError("unexpected token, expected equals sign '='");
3768     return false;
3769   }
3770   Parser.Lex(); // Eat '=' token.
3771
3772   MipsABIFlagsSection::FpABIKind FpABI;
3773   if (!parseFpABIValue(FpABI, ".module"))
3774     return false;
3775
3776   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3777     reportParseError("unexpected token, expected end of statement");
3778     return false;
3779   }
3780
3781   // Emit appropriate flags.
3782   getTargetStreamer().emitDirectiveModuleFP(FpABI, isABI_O32());
3783   Parser.Lex(); // Consume the EndOfStatement.
3784   return false;
3785 }
3786
3787 bool MipsAsmParser::parseFpABIValue(MipsABIFlagsSection::FpABIKind &FpABI,
3788                                     StringRef Directive) {
3789   MCAsmParser &Parser = getParser();
3790   MCAsmLexer &Lexer = getLexer();
3791
3792   if (Lexer.is(AsmToken::Identifier)) {
3793     StringRef Value = Parser.getTok().getString();
3794     Parser.Lex();
3795
3796     if (Value != "xx") {
3797       reportParseError("unsupported value, expected 'xx', '32' or '64'");
3798       return false;
3799     }
3800
3801     if (!isABI_O32()) {
3802       reportParseError("'" + Directive + " fp=xx' requires the O32 ABI");
3803       return false;
3804     }
3805
3806     FpABI = MipsABIFlagsSection::FpABIKind::XX;
3807     return true;
3808   }
3809
3810   if (Lexer.is(AsmToken::Integer)) {
3811     unsigned Value = Parser.getTok().getIntVal();
3812     Parser.Lex();
3813
3814     if (Value != 32 && Value != 64) {
3815       reportParseError("unsupported value, expected 'xx', '32' or '64'");
3816       return false;
3817     }
3818
3819     if (Value == 32) {
3820       if (!isABI_O32()) {
3821         reportParseError("'" + Directive + " fp=32' requires the O32 ABI");
3822         return false;
3823       }
3824
3825       FpABI = MipsABIFlagsSection::FpABIKind::S32;
3826     } else
3827       FpABI = MipsABIFlagsSection::FpABIKind::S64;
3828
3829     return true;
3830   }
3831
3832   return false;
3833 }
3834
3835 bool MipsAsmParser::ParseDirective(AsmToken DirectiveID) {
3836   MCAsmParser &Parser = getParser();
3837   StringRef IDVal = DirectiveID.getString();
3838
3839   if (IDVal == ".cpload")
3840     return parseDirectiveCpLoad(DirectiveID.getLoc());
3841   if (IDVal == ".dword") {
3842     parseDataDirective(8, DirectiveID.getLoc());
3843     return false;
3844   }
3845   if (IDVal == ".ent") {
3846     StringRef SymbolName;
3847
3848     if (Parser.parseIdentifier(SymbolName)) {
3849       reportParseError("expected identifier after .ent");
3850       return false;
3851     }
3852
3853     // There's an undocumented extension that allows an integer to
3854     // follow the name of the procedure which AFAICS is ignored by GAS.
3855     // Example: .ent foo,2
3856     if (getLexer().isNot(AsmToken::EndOfStatement)) {
3857       if (getLexer().isNot(AsmToken::Comma)) {
3858         // Even though we accept this undocumented extension for compatibility
3859         // reasons, the additional integer argument does not actually change
3860         // the behaviour of the '.ent' directive, so we would like to discourage
3861         // its use. We do this by not referring to the extended version in
3862         // error messages which are not directly related to its use.
3863         reportParseError("unexpected token, expected end of statement");
3864         return false;
3865       }
3866       Parser.Lex(); // Eat the comma.
3867       const MCExpr *DummyNumber;
3868       int64_t DummyNumberVal;
3869       // If the user was explicitly trying to use the extended version,
3870       // we still give helpful extension-related error messages.
3871       if (Parser.parseExpression(DummyNumber)) {
3872         reportParseError("expected number after comma");
3873         return false;
3874       }
3875       if (!DummyNumber->EvaluateAsAbsolute(DummyNumberVal)) {
3876         reportParseError("expected an absolute expression after comma");
3877         return false;
3878       }
3879     }
3880
3881     // If this is not the end of the statement, report an error.
3882     if (getLexer().isNot(AsmToken::EndOfStatement)) {
3883       reportParseError("unexpected token, expected end of statement");
3884       return false;
3885     }
3886
3887     MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
3888
3889     getTargetStreamer().emitDirectiveEnt(*Sym);
3890     CurrentFn = Sym;
3891     return false;
3892   }
3893
3894   if (IDVal == ".end") {
3895     StringRef SymbolName;
3896
3897     if (Parser.parseIdentifier(SymbolName)) {
3898       reportParseError("expected identifier after .end");
3899       return false;
3900     }
3901
3902     if (getLexer().isNot(AsmToken::EndOfStatement)) {
3903       reportParseError("unexpected token, expected end of statement");
3904       return false;
3905     }
3906
3907     if (CurrentFn == nullptr) {
3908       reportParseError(".end used without .ent");
3909       return false;
3910     }
3911
3912     if ((SymbolName != CurrentFn->getName())) {
3913       reportParseError(".end symbol does not match .ent symbol");
3914       return false;
3915     }
3916
3917     getTargetStreamer().emitDirectiveEnd(SymbolName);
3918     CurrentFn = nullptr;
3919     return false;
3920   }
3921
3922   if (IDVal == ".frame") {
3923     // .frame $stack_reg, frame_size_in_bytes, $return_reg
3924     SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> TmpReg;
3925     OperandMatchResultTy ResTy = parseAnyRegister(TmpReg);
3926     if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
3927       reportParseError("expected stack register");
3928       return false;
3929     }
3930
3931     MipsOperand &StackRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
3932     if (!StackRegOpnd.isGPRAsmReg()) {
3933       reportParseError(StackRegOpnd.getStartLoc(),
3934                        "expected general purpose register");
3935       return false;
3936     }
3937     unsigned StackReg = StackRegOpnd.getGPR32Reg();
3938
3939     if (Parser.getTok().is(AsmToken::Comma))
3940       Parser.Lex();
3941     else {
3942       reportParseError("unexpected token, expected comma");
3943       return false;
3944     }
3945
3946     // Parse the frame size.
3947     const MCExpr *FrameSize;
3948     int64_t FrameSizeVal;
3949
3950     if (Parser.parseExpression(FrameSize)) {
3951       reportParseError("expected frame size value");
3952       return false;
3953     }
3954
3955     if (!FrameSize->EvaluateAsAbsolute(FrameSizeVal)) {
3956       reportParseError("frame size not an absolute expression");
3957       return false;
3958     }
3959
3960     if (Parser.getTok().is(AsmToken::Comma))
3961       Parser.Lex();
3962     else {
3963       reportParseError("unexpected token, expected comma");
3964       return false;
3965     }
3966
3967     // Parse the return register.
3968     TmpReg.clear();
3969     ResTy = parseAnyRegister(TmpReg);
3970     if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
3971       reportParseError("expected return register");
3972       return false;
3973     }
3974
3975     MipsOperand &ReturnRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
3976     if (!ReturnRegOpnd.isGPRAsmReg()) {
3977       reportParseError(ReturnRegOpnd.getStartLoc(),
3978                        "expected general purpose register");
3979       return false;
3980     }
3981
3982     // If this is not the end of the statement, report an error.
3983     if (getLexer().isNot(AsmToken::EndOfStatement)) {
3984       reportParseError("unexpected token, expected end of statement");
3985       return false;
3986     }
3987
3988     getTargetStreamer().emitFrame(StackReg, FrameSizeVal,
3989                                   ReturnRegOpnd.getGPR32Reg());
3990     return false;
3991   }
3992
3993   if (IDVal == ".set") {
3994     return parseDirectiveSet();
3995   }
3996
3997   if (IDVal == ".mask" || IDVal == ".fmask") {
3998     // .mask bitmask, frame_offset
3999     // bitmask: One bit for each register used.
4000     // frame_offset: Offset from Canonical Frame Address ($sp on entry) where
4001     //               first register is expected to be saved.
4002     // Examples:
4003     //   .mask 0x80000000, -4
4004     //   .fmask 0x80000000, -4
4005     //
4006
4007     // Parse the bitmask
4008     const MCExpr *BitMask;
4009     int64_t BitMaskVal;
4010
4011     if (Parser.parseExpression(BitMask)) {
4012       reportParseError("expected bitmask value");
4013       return false;
4014     }
4015
4016     if (!BitMask->EvaluateAsAbsolute(BitMaskVal)) {
4017       reportParseError("bitmask not an absolute expression");
4018       return false;
4019     }
4020
4021     if (Parser.getTok().is(AsmToken::Comma))
4022       Parser.Lex();
4023     else {
4024       reportParseError("unexpected token, expected comma");
4025       return false;
4026     }
4027
4028     // Parse the frame_offset
4029     const MCExpr *FrameOffset;
4030     int64_t FrameOffsetVal;
4031
4032     if (Parser.parseExpression(FrameOffset)) {
4033       reportParseError("expected frame offset value");
4034       return false;
4035     }
4036
4037     if (!FrameOffset->EvaluateAsAbsolute(FrameOffsetVal)) {
4038       reportParseError("frame offset not an absolute expression");
4039       return false;
4040     }
4041
4042     // If this is not the end of the statement, report an error.
4043     if (getLexer().isNot(AsmToken::EndOfStatement)) {
4044       reportParseError("unexpected token, expected end of statement");
4045       return false;
4046     }
4047
4048     if (IDVal == ".mask")
4049       getTargetStreamer().emitMask(BitMaskVal, FrameOffsetVal);
4050     else
4051       getTargetStreamer().emitFMask(BitMaskVal, FrameOffsetVal);
4052     return false;
4053   }
4054
4055   if (IDVal == ".nan")
4056     return parseDirectiveNaN();
4057
4058   if (IDVal == ".gpword") {
4059     parseDirectiveGpWord();
4060     return false;
4061   }
4062
4063   if (IDVal == ".gpdword") {
4064     parseDirectiveGpDWord();
4065     return false;
4066   }
4067
4068   if (IDVal == ".word") {
4069     parseDataDirective(4, DirectiveID.getLoc());
4070     return false;
4071   }
4072
4073   if (IDVal == ".option")
4074     return parseDirectiveOption();
4075
4076   if (IDVal == ".abicalls") {
4077     getTargetStreamer().emitDirectiveAbiCalls();
4078     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
4079       Error(Parser.getTok().getLoc(), 
4080             "unexpected token, expected end of statement");
4081       // Clear line
4082       Parser.eatToEndOfStatement();
4083     }
4084     return false;
4085   }
4086
4087   if (IDVal == ".cpsetup")
4088     return parseDirectiveCPSetup();
4089
4090   if (IDVal == ".module")
4091     return parseDirectiveModule();
4092
4093   return true;
4094 }
4095
4096 extern "C" void LLVMInitializeMipsAsmParser() {
4097   RegisterMCAsmParser<MipsAsmParser> X(TheMipsTarget);
4098   RegisterMCAsmParser<MipsAsmParser> Y(TheMipselTarget);
4099   RegisterMCAsmParser<MipsAsmParser> A(TheMips64Target);
4100   RegisterMCAsmParser<MipsAsmParser> B(TheMips64elTarget);
4101 }
4102
4103 #define GET_REGISTER_MATCHER
4104 #define GET_MATCHER_IMPLEMENTATION
4105 #include "MipsGenAsmMatcher.inc"