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