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