[mips] Add support for branch-likely pseudo-instructions
[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(sti.getTargetTuple(), sti.getCPU(),
379                                           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     const TargetTuple &TT = sti.getTargetTuple();
406     if ((TT.getArch() == TargetTuple::mips) ||
407         (TT.getArch() == TargetTuple::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::BLTL:
1821   case Mips::BLEL:
1822   case Mips::BGEL:
1823   case Mips::BGTL:
1824   case Mips::BLTUL:
1825   case Mips::BLEUL:
1826   case Mips::BGEUL:
1827   case Mips::BGTUL:
1828   case Mips::SDivMacro:
1829   case Mips::UDivMacro:
1830   case Mips::DSDivMacro:
1831   case Mips::DUDivMacro:
1832   case Mips::Ulhu:
1833   case Mips::Ulw:
1834     return true;
1835   default:
1836     return false;
1837   }
1838 }
1839
1840 bool MipsAsmParser::expandInstruction(MCInst &Inst, SMLoc IDLoc,
1841                                       SmallVectorImpl<MCInst> &Instructions) {
1842   switch (Inst.getOpcode()) {
1843   default: llvm_unreachable("unimplemented expansion");
1844   case Mips::LoadImm32:
1845     return expandLoadImm(Inst, true, IDLoc, Instructions);
1846   case Mips::LoadImm64:
1847     return expandLoadImm(Inst, false, IDLoc, Instructions);
1848   case Mips::LoadAddrImm32:
1849   case Mips::LoadAddrImm64:
1850     assert(Inst.getOperand(0).isReg() && "expected register operand kind");
1851     assert((Inst.getOperand(1).isImm() || Inst.getOperand(1).isExpr()) &&
1852            "expected immediate operand kind");
1853
1854     return expandLoadAddress(
1855         Inst.getOperand(0).getReg(), Mips::NoRegister, Inst.getOperand(1),
1856         Inst.getOpcode() == Mips::LoadAddrImm32, IDLoc, Instructions);
1857   case Mips::LoadAddrReg32:
1858   case Mips::LoadAddrReg64:
1859     assert(Inst.getOperand(0).isReg() && "expected register operand kind");
1860     assert(Inst.getOperand(1).isReg() && "expected register operand kind");
1861     assert((Inst.getOperand(2).isImm() || Inst.getOperand(2).isExpr()) &&
1862            "expected immediate operand kind");
1863
1864     return expandLoadAddress(
1865         Inst.getOperand(0).getReg(), Inst.getOperand(1).getReg(), Inst.getOperand(2),
1866         Inst.getOpcode() == Mips::LoadAddrReg32, IDLoc, Instructions);
1867   case Mips::B_MM_Pseudo:
1868   case Mips::B_MMR6_Pseudo:
1869     return expandUncondBranchMMPseudo(Inst, IDLoc, Instructions);
1870   case Mips::SWM_MM:
1871   case Mips::LWM_MM:
1872     return expandLoadStoreMultiple(Inst, IDLoc, Instructions);
1873   case Mips::JalOneReg:
1874   case Mips::JalTwoReg:
1875     return expandJalWithRegs(Inst, IDLoc, Instructions);
1876   case Mips::BneImm:
1877   case Mips::BeqImm:
1878     return expandBranchImm(Inst, IDLoc, Instructions);
1879   case Mips::BLT:
1880   case Mips::BLE:
1881   case Mips::BGE:
1882   case Mips::BGT:
1883   case Mips::BLTU:
1884   case Mips::BLEU:
1885   case Mips::BGEU:
1886   case Mips::BGTU:
1887   case Mips::BLTL:
1888   case Mips::BLEL:
1889   case Mips::BGEL:
1890   case Mips::BGTL:
1891   case Mips::BLTUL:
1892   case Mips::BLEUL:
1893   case Mips::BGEUL:
1894   case Mips::BGTUL:
1895     return expandCondBranches(Inst, IDLoc, Instructions);
1896   case Mips::SDivMacro:
1897     return expandDiv(Inst, IDLoc, Instructions, false, true);
1898   case Mips::DSDivMacro:
1899     return expandDiv(Inst, IDLoc, Instructions, true, true);
1900   case Mips::UDivMacro:
1901     return expandDiv(Inst, IDLoc, Instructions, false, false);
1902   case Mips::DUDivMacro:
1903     return expandDiv(Inst, IDLoc, Instructions, true, false);
1904   case Mips::Ulhu:
1905     return expandUlhu(Inst, IDLoc, Instructions);
1906   case Mips::Ulw:
1907     return expandUlw(Inst, IDLoc, Instructions);
1908   }
1909 }
1910
1911 namespace {
1912 void emitRX(unsigned Opcode, unsigned Reg0, MCOperand Op1, SMLoc IDLoc,
1913             SmallVectorImpl<MCInst> &Instructions) {
1914   MCInst tmpInst;
1915   tmpInst.setOpcode(Opcode);
1916   tmpInst.addOperand(MCOperand::createReg(Reg0));
1917   tmpInst.addOperand(Op1);
1918   tmpInst.setLoc(IDLoc);
1919   Instructions.push_back(tmpInst);
1920 }
1921
1922 void emitRI(unsigned Opcode, unsigned Reg0, int32_t Imm, SMLoc IDLoc,
1923             SmallVectorImpl<MCInst> &Instructions) {
1924   emitRX(Opcode, Reg0, MCOperand::createImm(Imm), IDLoc, Instructions);
1925 }
1926
1927 void emitRR(unsigned Opcode, unsigned Reg0, unsigned Reg1, SMLoc IDLoc,
1928             SmallVectorImpl<MCInst> &Instructions) {
1929   emitRX(Opcode, Reg0, MCOperand::createReg(Reg1), IDLoc, Instructions);
1930 }
1931
1932 void emitII(unsigned Opcode, int16_t Imm1, int16_t Imm2, SMLoc IDLoc,
1933             SmallVectorImpl<MCInst> &Instructions) {
1934   MCInst tmpInst;
1935   tmpInst.setOpcode(Opcode);
1936   tmpInst.addOperand(MCOperand::createImm(Imm1));
1937   tmpInst.addOperand(MCOperand::createImm(Imm2));
1938   tmpInst.setLoc(IDLoc);
1939   Instructions.push_back(tmpInst);
1940 }
1941
1942 void emitR(unsigned Opcode, unsigned Reg0, SMLoc IDLoc,
1943            SmallVectorImpl<MCInst> &Instructions) {
1944   MCInst tmpInst;
1945   tmpInst.setOpcode(Opcode);
1946   tmpInst.addOperand(MCOperand::createReg(Reg0));
1947   tmpInst.setLoc(IDLoc);
1948   Instructions.push_back(tmpInst);
1949 }
1950
1951 void emitRRX(unsigned Opcode, unsigned Reg0, unsigned Reg1, MCOperand Op2,
1952              SMLoc IDLoc, SmallVectorImpl<MCInst> &Instructions) {
1953   MCInst tmpInst;
1954   tmpInst.setOpcode(Opcode);
1955   tmpInst.addOperand(MCOperand::createReg(Reg0));
1956   tmpInst.addOperand(MCOperand::createReg(Reg1));
1957   tmpInst.addOperand(Op2);
1958   tmpInst.setLoc(IDLoc);
1959   Instructions.push_back(tmpInst);
1960 }
1961
1962 void emitRRR(unsigned Opcode, unsigned Reg0, unsigned Reg1, unsigned Reg2,
1963              SMLoc IDLoc, SmallVectorImpl<MCInst> &Instructions) {
1964   emitRRX(Opcode, Reg0, Reg1, MCOperand::createReg(Reg2), IDLoc,
1965           Instructions);
1966 }
1967
1968 void emitRRI(unsigned Opcode, unsigned Reg0, unsigned Reg1, int16_t Imm,
1969              SMLoc IDLoc, SmallVectorImpl<MCInst> &Instructions) {
1970   emitRRX(Opcode, Reg0, Reg1, MCOperand::createImm(Imm), IDLoc,
1971           Instructions);
1972 }
1973
1974 void emitAppropriateDSLL(unsigned DstReg, unsigned SrcReg, int16_t ShiftAmount,
1975                          SMLoc IDLoc, SmallVectorImpl<MCInst> &Instructions) {
1976   if (ShiftAmount >= 32) {
1977     emitRRI(Mips::DSLL32, DstReg, SrcReg, ShiftAmount - 32, IDLoc,
1978             Instructions);
1979     return;
1980   }
1981
1982   emitRRI(Mips::DSLL, DstReg, SrcReg, ShiftAmount, IDLoc, Instructions);
1983 }
1984 } // end anonymous namespace.
1985
1986 bool MipsAsmParser::expandJalWithRegs(MCInst &Inst, SMLoc IDLoc,
1987                                       SmallVectorImpl<MCInst> &Instructions) {
1988   // Create a JALR instruction which is going to replace the pseudo-JAL.
1989   MCInst JalrInst;
1990   JalrInst.setLoc(IDLoc);
1991   const MCOperand FirstRegOp = Inst.getOperand(0);
1992   const unsigned Opcode = Inst.getOpcode();
1993
1994   if (Opcode == Mips::JalOneReg) {
1995     // jal $rs => jalr $rs
1996     if (inMicroMipsMode()) {
1997       JalrInst.setOpcode(Mips::JALR16_MM);
1998       JalrInst.addOperand(FirstRegOp);
1999     } else {
2000       JalrInst.setOpcode(Mips::JALR);
2001       JalrInst.addOperand(MCOperand::createReg(Mips::RA));
2002       JalrInst.addOperand(FirstRegOp);
2003     }
2004   } else if (Opcode == Mips::JalTwoReg) {
2005     // jal $rd, $rs => jalr $rd, $rs
2006     JalrInst.setOpcode(inMicroMipsMode() ? Mips::JALR_MM : Mips::JALR);
2007     JalrInst.addOperand(FirstRegOp);
2008     const MCOperand SecondRegOp = Inst.getOperand(1);
2009     JalrInst.addOperand(SecondRegOp);
2010   }
2011   Instructions.push_back(JalrInst);
2012
2013   // If .set reorder is active, emit a NOP after it.
2014   if (AssemblerOptions.back()->isReorder()) {
2015     // This is a 32-bit NOP because these 2 pseudo-instructions
2016     // do not have a short delay slot.
2017     MCInst NopInst;
2018     NopInst.setOpcode(Mips::SLL);
2019     NopInst.addOperand(MCOperand::createReg(Mips::ZERO));
2020     NopInst.addOperand(MCOperand::createReg(Mips::ZERO));
2021     NopInst.addOperand(MCOperand::createImm(0));
2022     Instructions.push_back(NopInst);
2023   }
2024
2025   return false;
2026 }
2027
2028 /// Can the value be represented by a unsigned N-bit value and a shift left?
2029 template<unsigned N>
2030 bool isShiftedUIntAtAnyPosition(uint64_t x) {
2031   unsigned BitNum = findFirstSet(x);
2032
2033   return (x == x >> BitNum << BitNum) && isUInt<N>(x >> BitNum);
2034 }
2035
2036 /// Load (or add) an immediate into a register.
2037 ///
2038 /// @param ImmValue     The immediate to load.
2039 /// @param DstReg       The register that will hold the immediate.
2040 /// @param SrcReg       A register to add to the immediate or Mips::NoRegister
2041 ///                     for a simple initialization.
2042 /// @param Is32BitImm   Is ImmValue 32-bit or 64-bit?
2043 /// @param IsAddress    True if the immediate represents an address. False if it
2044 ///                     is an integer.
2045 /// @param IDLoc        Location of the immediate in the source file.
2046 /// @param Instructions The instructions emitted by this expansion.
2047 bool MipsAsmParser::loadImmediate(int64_t ImmValue, unsigned DstReg,
2048                                   unsigned SrcReg, bool Is32BitImm,
2049                                   bool IsAddress, SMLoc IDLoc,
2050                                   SmallVectorImpl<MCInst> &Instructions) {
2051   if (!Is32BitImm && !isGP64bit()) {
2052     Error(IDLoc, "instruction requires a 64-bit architecture");
2053     return true;
2054   }
2055
2056   if (Is32BitImm) {
2057     if (isInt<32>(ImmValue) || isUInt<32>(ImmValue)) {
2058       // Sign extend up to 64-bit so that the predicates match the hardware
2059       // behaviour. In particular, isInt<16>(0xffff8000) and similar should be
2060       // true.
2061       ImmValue = SignExtend64<32>(ImmValue);
2062     } else {
2063       Error(IDLoc, "instruction requires a 32-bit immediate");
2064       return true;
2065     }
2066   }
2067
2068   unsigned ZeroReg = IsAddress ? ABI.GetNullPtr() : ABI.GetZeroReg();
2069   unsigned AdduOp = !Is32BitImm ? Mips::DADDu : Mips::ADDu;
2070
2071   bool UseSrcReg = false;
2072   if (SrcReg != Mips::NoRegister)
2073     UseSrcReg = true;
2074
2075   unsigned TmpReg = DstReg;
2076   if (UseSrcReg && (DstReg == SrcReg)) {
2077     // At this point we need AT to perform the expansions and we exit if it is
2078     // not available.
2079     unsigned ATReg = getATReg(IDLoc);
2080     if (!ATReg)
2081       return true;
2082     TmpReg = ATReg;
2083   }
2084
2085   if (isInt<16>(ImmValue)) {
2086     if (!UseSrcReg)
2087       SrcReg = ZeroReg;
2088
2089     // This doesn't quite follow the usual ABI expectations for N32 but matches
2090     // traditional assembler behaviour. N32 would normally use addiu for both
2091     // integers and addresses.
2092     if (IsAddress && !Is32BitImm) {
2093       emitRRI(Mips::DADDiu, DstReg, SrcReg, ImmValue, IDLoc, Instructions);
2094       return false;
2095     }
2096
2097     emitRRI(Mips::ADDiu, DstReg, SrcReg, ImmValue, IDLoc, Instructions);
2098     return false;
2099   }
2100
2101   if (isUInt<16>(ImmValue)) {
2102     unsigned TmpReg = DstReg;
2103     if (SrcReg == DstReg) {
2104       TmpReg = getATReg(IDLoc);
2105       if (!TmpReg)
2106         return true;
2107     }
2108
2109     emitRRI(Mips::ORi, TmpReg, ZeroReg, ImmValue, IDLoc, Instructions);
2110     if (UseSrcReg)
2111       emitRRR(ABI.GetPtrAdduOp(), DstReg, TmpReg, SrcReg, IDLoc, Instructions);
2112     return false;
2113   }
2114
2115   if (isInt<32>(ImmValue) || isUInt<32>(ImmValue)) {
2116     warnIfNoMacro(IDLoc);
2117
2118     uint16_t Bits31To16 = (ImmValue >> 16) & 0xffff;
2119     uint16_t Bits15To0 = ImmValue & 0xffff;
2120
2121     if (!Is32BitImm && !isInt<32>(ImmValue)) {
2122       // Traditional behaviour seems to special case this particular value. It's
2123       // not clear why other masks are handled differently.
2124       if (ImmValue == 0xffffffff) {
2125         emitRI(Mips::LUi, TmpReg, 0xffff, IDLoc, Instructions);
2126         emitRRI(Mips::DSRL32, TmpReg, TmpReg, 0, IDLoc, Instructions);
2127         if (UseSrcReg)
2128           emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, Instructions);
2129         return false;
2130       }
2131
2132       // Expand to an ORi instead of a LUi to avoid sign-extending into the
2133       // upper 32 bits.
2134       emitRRI(Mips::ORi, TmpReg, ZeroReg, Bits31To16, IDLoc, Instructions);
2135       emitRRI(Mips::DSLL, TmpReg, TmpReg, 16, IDLoc, Instructions);
2136       if (Bits15To0)
2137         emitRRI(Mips::ORi, TmpReg, TmpReg, Bits15To0, IDLoc, Instructions);
2138       if (UseSrcReg)
2139         emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, Instructions);
2140       return false;
2141     }
2142
2143     emitRI(Mips::LUi, TmpReg, Bits31To16, IDLoc, Instructions);
2144     if (Bits15To0)
2145       emitRRI(Mips::ORi, TmpReg, TmpReg, Bits15To0, IDLoc, Instructions);
2146     if (UseSrcReg)
2147       emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, Instructions);
2148     return false;
2149   }
2150
2151   if (isShiftedUIntAtAnyPosition<16>(ImmValue)) {
2152     if (Is32BitImm) {
2153       Error(IDLoc, "instruction requires a 32-bit immediate");
2154       return true;
2155     }
2156
2157     // Traditionally, these immediates are shifted as little as possible and as
2158     // such we align the most significant bit to bit 15 of our temporary.
2159     unsigned FirstSet = findFirstSet((uint64_t)ImmValue);
2160     unsigned LastSet = findLastSet((uint64_t)ImmValue);
2161     unsigned ShiftAmount = FirstSet - (15 - (LastSet - FirstSet));
2162     uint16_t Bits = (ImmValue >> ShiftAmount) & 0xffff;
2163     emitRRI(Mips::ORi, TmpReg, ZeroReg, Bits, IDLoc, Instructions);
2164     emitRRI(Mips::DSLL, TmpReg, TmpReg, ShiftAmount, IDLoc, Instructions);
2165
2166     if (UseSrcReg)
2167       emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, Instructions);
2168
2169     return false;
2170   }
2171
2172   warnIfNoMacro(IDLoc);
2173
2174   // The remaining case is packed with a sequence of dsll and ori with zeros
2175   // being omitted and any neighbouring dsll's being coalesced.
2176   // The highest 32-bit's are equivalent to a 32-bit immediate load.
2177
2178   // Load bits 32-63 of ImmValue into bits 0-31 of the temporary register.
2179   if (loadImmediate(ImmValue >> 32, TmpReg, Mips::NoRegister, true, false,
2180                     IDLoc, Instructions))
2181     return false;
2182
2183   // Shift and accumulate into the register. If a 16-bit chunk is zero, then
2184   // skip it and defer the shift to the next chunk.
2185   unsigned ShiftCarriedForwards = 16;
2186   for (int BitNum = 16; BitNum >= 0; BitNum -= 16) {
2187     uint16_t ImmChunk = (ImmValue >> BitNum) & 0xffff;
2188
2189     if (ImmChunk != 0) {
2190       emitAppropriateDSLL(TmpReg, TmpReg, ShiftCarriedForwards, IDLoc,
2191                           Instructions);
2192       emitRRI(Mips::ORi, TmpReg, TmpReg, ImmChunk, IDLoc, Instructions);
2193       ShiftCarriedForwards = 0;
2194     }
2195
2196     ShiftCarriedForwards += 16;
2197   }
2198   ShiftCarriedForwards -= 16;
2199
2200   // Finish any remaining shifts left by trailing zeros.
2201   if (ShiftCarriedForwards)
2202     emitAppropriateDSLL(TmpReg, TmpReg, ShiftCarriedForwards, IDLoc,
2203                         Instructions);
2204
2205   if (UseSrcReg)
2206     emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, Instructions);
2207
2208   return false;
2209 }
2210
2211 bool MipsAsmParser::expandLoadImm(MCInst &Inst, bool Is32BitImm, SMLoc IDLoc,
2212                                   SmallVectorImpl<MCInst> &Instructions) {
2213   const MCOperand &ImmOp = Inst.getOperand(1);
2214   assert(ImmOp.isImm() && "expected immediate operand kind");
2215   const MCOperand &DstRegOp = Inst.getOperand(0);
2216   assert(DstRegOp.isReg() && "expected register operand kind");
2217
2218   if (loadImmediate(ImmOp.getImm(), DstRegOp.getReg(), Mips::NoRegister,
2219                     Is32BitImm, false, IDLoc, Instructions))
2220     return true;
2221
2222   return false;
2223 }
2224
2225 bool MipsAsmParser::expandLoadAddress(unsigned DstReg, unsigned BaseReg,
2226                                       const MCOperand &Offset,
2227                                       bool Is32BitAddress, SMLoc IDLoc,
2228                                       SmallVectorImpl<MCInst> &Instructions) {
2229   // la can't produce a usable address when addresses are 64-bit.
2230   if (Is32BitAddress && ABI.ArePtrs64bit()) {
2231     // FIXME: Demote this to a warning and continue as if we had 'dla' instead.
2232     //        We currently can't do this because we depend on the equality
2233     //        operator and N64 can end up with a GPR32/GPR64 mismatch.
2234     Error(IDLoc, "la used to load 64-bit address");
2235     // Continue as if we had 'dla' instead.
2236     Is32BitAddress = false;
2237   }
2238
2239   // dla requires 64-bit addresses.
2240   if (!Is32BitAddress && !ABI.ArePtrs64bit()) {
2241     Error(IDLoc, "instruction requires a 64-bit architecture");
2242     return true;
2243   }
2244
2245   if (!Offset.isImm())
2246     return loadAndAddSymbolAddress(Offset.getExpr(), DstReg, BaseReg,
2247                                    Is32BitAddress, IDLoc, Instructions);
2248
2249   return loadImmediate(Offset.getImm(), DstReg, BaseReg, Is32BitAddress, true,
2250                        IDLoc, Instructions);
2251 }
2252
2253 bool MipsAsmParser::loadAndAddSymbolAddress(
2254     const MCExpr *SymExpr, unsigned DstReg, unsigned SrcReg, bool Is32BitSym,
2255     SMLoc IDLoc, SmallVectorImpl<MCInst> &Instructions) {
2256   warnIfNoMacro(IDLoc);
2257
2258   // FIXME: The way we're handling symbols right now prevents simple expressions
2259   //        like foo+8. We'll be able to fix this once our unary operators (%hi
2260   //        and similar) are treated as operators rather than as fixup types.
2261   const MCSymbolRefExpr *Symbol = cast<MCSymbolRefExpr>(SymExpr);
2262   const MCSymbolRefExpr *HiExpr = MCSymbolRefExpr::create(
2263       &Symbol->getSymbol(), MCSymbolRefExpr::VK_Mips_ABS_HI, getContext());
2264   const MCSymbolRefExpr *LoExpr = MCSymbolRefExpr::create(
2265       &Symbol->getSymbol(), MCSymbolRefExpr::VK_Mips_ABS_LO, getContext());
2266
2267   bool UseSrcReg = SrcReg != Mips::NoRegister;
2268
2269   // This is the 64-bit symbol address expansion.
2270   if (ABI.ArePtrs64bit() && isGP64bit()) {
2271     // We always need AT for the 64-bit expansion.
2272     // If it is not available we exit.
2273     unsigned ATReg = getATReg(IDLoc);
2274     if (!ATReg)
2275       return true;
2276
2277     const MCSymbolRefExpr *HighestExpr = MCSymbolRefExpr::create(
2278         &Symbol->getSymbol(), MCSymbolRefExpr::VK_Mips_HIGHEST, getContext());
2279     const MCSymbolRefExpr *HigherExpr = MCSymbolRefExpr::create(
2280         &Symbol->getSymbol(), MCSymbolRefExpr::VK_Mips_HIGHER, getContext());
2281
2282     if (UseSrcReg && (DstReg == SrcReg)) {
2283       // If $rs is the same as $rd:
2284       // (d)la $rd, sym($rd) => lui    $at, %highest(sym)
2285       //                        daddiu $at, $at, %higher(sym)
2286       //                        dsll   $at, $at, 16
2287       //                        daddiu $at, $at, %hi(sym)
2288       //                        dsll   $at, $at, 16
2289       //                        daddiu $at, $at, %lo(sym)
2290       //                        daddu  $rd, $at, $rd
2291       emitRX(Mips::LUi, ATReg, MCOperand::createExpr(HighestExpr), IDLoc,
2292              Instructions);
2293       emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(HigherExpr),
2294               IDLoc, Instructions);
2295       emitRRI(Mips::DSLL, ATReg, ATReg, 16, IDLoc, Instructions);
2296       emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(HiExpr), IDLoc,
2297               Instructions);
2298       emitRRI(Mips::DSLL, ATReg, ATReg, 16, IDLoc, Instructions);
2299       emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(LoExpr), IDLoc,
2300               Instructions);
2301       emitRRR(Mips::DADDu, DstReg, ATReg, SrcReg, IDLoc, Instructions);
2302
2303       return false;
2304     }
2305
2306     // Otherwise, if the $rs is different from $rd or if $rs isn't specified:
2307     // (d)la $rd, sym/sym($rs) => lui    $rd, %highest(sym)
2308     //                            lui    $at, %hi(sym)
2309     //                            daddiu $rd, $rd, %higher(sym)
2310     //                            daddiu $at, $at, %lo(sym)
2311     //                            dsll32 $rd, $rd, 0
2312     //                            daddu  $rd, $rd, $at
2313     //                            (daddu  $rd, $rd, $rs)
2314     emitRX(Mips::LUi, DstReg, MCOperand::createExpr(HighestExpr), IDLoc,
2315            Instructions);
2316     emitRX(Mips::LUi, ATReg, MCOperand::createExpr(HiExpr), IDLoc,
2317            Instructions);
2318     emitRRX(Mips::DADDiu, DstReg, DstReg, MCOperand::createExpr(HigherExpr),
2319             IDLoc, Instructions);
2320     emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(LoExpr), IDLoc,
2321             Instructions);
2322     emitRRI(Mips::DSLL32, DstReg, DstReg, 0, IDLoc, Instructions);
2323     emitRRR(Mips::DADDu, DstReg, DstReg, ATReg, IDLoc, Instructions);
2324     if (UseSrcReg)
2325       emitRRR(Mips::DADDu, DstReg, DstReg, SrcReg, IDLoc, Instructions);
2326
2327     return false;
2328   }
2329
2330   // And now, the 32-bit symbol address expansion:
2331   // If $rs is the same as $rd:
2332   // (d)la $rd, sym($rd)     => lui   $at, %hi(sym)
2333   //                            ori   $at, $at, %lo(sym)
2334   //                            addu  $rd, $at, $rd
2335   // Otherwise, if the $rs is different from $rd or if $rs isn't specified:
2336   // (d)la $rd, sym/sym($rs) => lui   $rd, %hi(sym)
2337   //                            ori   $rd, $rd, %lo(sym)
2338   //                            (addu $rd, $rd, $rs)
2339   unsigned TmpReg = DstReg;
2340   if (UseSrcReg && (DstReg == SrcReg)) {
2341     // If $rs is the same as $rd, we need to use AT.
2342     // If it is not available we exit.
2343     unsigned ATReg = getATReg(IDLoc);
2344     if (!ATReg)
2345       return true;
2346     TmpReg = ATReg;
2347   }
2348
2349   emitRX(Mips::LUi, TmpReg, MCOperand::createExpr(HiExpr), IDLoc, Instructions);
2350   emitRRX(Mips::ADDiu, TmpReg, TmpReg, MCOperand::createExpr(LoExpr), IDLoc,
2351           Instructions);
2352
2353   if (UseSrcReg)
2354     emitRRR(Mips::ADDu, DstReg, TmpReg, SrcReg, IDLoc, Instructions);
2355   else
2356     assert(DstReg == TmpReg);
2357
2358   return false;
2359 }
2360
2361 bool MipsAsmParser::expandUncondBranchMMPseudo(
2362     MCInst &Inst, SMLoc IDLoc, SmallVectorImpl<MCInst> &Instructions) {
2363   assert(getInstDesc(Inst.getOpcode()).getNumOperands() == 1 &&
2364          "unexpected number of operands");
2365
2366   MCOperand Offset = Inst.getOperand(0);
2367   if (Offset.isExpr()) {
2368     Inst.clear();
2369     Inst.setOpcode(Mips::BEQ_MM);
2370     Inst.addOperand(MCOperand::createReg(Mips::ZERO));
2371     Inst.addOperand(MCOperand::createReg(Mips::ZERO));
2372     Inst.addOperand(MCOperand::createExpr(Offset.getExpr()));
2373   } else {
2374     assert(Offset.isImm() && "expected immediate operand kind");
2375     if (isIntN(11, Offset.getImm())) {
2376       // If offset fits into 11 bits then this instruction becomes microMIPS
2377       // 16-bit unconditional branch instruction.
2378       if (inMicroMipsMode())
2379         Inst.setOpcode(hasMips32r6() ? Mips::BC16_MMR6 : Mips::B16_MM);
2380     } else {
2381       if (!isIntN(17, Offset.getImm()))
2382         Error(IDLoc, "branch target out of range");
2383       if (OffsetToAlignment(Offset.getImm(), 1LL << 1))
2384         Error(IDLoc, "branch to misaligned address");
2385       Inst.clear();
2386       Inst.setOpcode(Mips::BEQ_MM);
2387       Inst.addOperand(MCOperand::createReg(Mips::ZERO));
2388       Inst.addOperand(MCOperand::createReg(Mips::ZERO));
2389       Inst.addOperand(MCOperand::createImm(Offset.getImm()));
2390     }
2391   }
2392   Instructions.push_back(Inst);
2393
2394   // If .set reorder is active and branch instruction has a delay slot,
2395   // emit a NOP after it.
2396   const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
2397   if (MCID.hasDelaySlot() && AssemblerOptions.back()->isReorder())
2398     createNop(true, IDLoc, Instructions);
2399
2400   return false;
2401 }
2402
2403 bool MipsAsmParser::expandBranchImm(MCInst &Inst, SMLoc IDLoc,
2404                                     SmallVectorImpl<MCInst> &Instructions) {
2405   const MCOperand &DstRegOp = Inst.getOperand(0);
2406   assert(DstRegOp.isReg() && "expected register operand kind");
2407
2408   const MCOperand &ImmOp = Inst.getOperand(1);
2409   assert(ImmOp.isImm() && "expected immediate operand kind");
2410
2411   const MCOperand &MemOffsetOp = Inst.getOperand(2);
2412   assert(MemOffsetOp.isImm() && "expected immediate operand kind");
2413
2414   unsigned OpCode = 0;
2415   switch(Inst.getOpcode()) {
2416     case Mips::BneImm:
2417       OpCode = Mips::BNE;
2418       break;
2419     case Mips::BeqImm:
2420       OpCode = Mips::BEQ;
2421       break;
2422     default:
2423       llvm_unreachable("Unknown immediate branch pseudo-instruction.");
2424       break;
2425   }
2426
2427   int64_t ImmValue = ImmOp.getImm();
2428   if (ImmValue == 0) {
2429     MCInst BranchInst;
2430     BranchInst.setOpcode(OpCode);
2431     BranchInst.addOperand(DstRegOp);
2432     BranchInst.addOperand(MCOperand::createReg(Mips::ZERO));
2433     BranchInst.addOperand(MemOffsetOp);
2434     Instructions.push_back(BranchInst);
2435   } else {
2436     warnIfNoMacro(IDLoc);
2437
2438     unsigned ATReg = getATReg(IDLoc);
2439     if (!ATReg)
2440       return true;
2441
2442     if (loadImmediate(ImmValue, ATReg, Mips::NoRegister, !isGP64bit(), true,
2443                       IDLoc, Instructions))
2444       return true;
2445
2446     MCInst BranchInst;
2447     BranchInst.setOpcode(OpCode);
2448     BranchInst.addOperand(DstRegOp);
2449     BranchInst.addOperand(MCOperand::createReg(ATReg));
2450     BranchInst.addOperand(MemOffsetOp);
2451     Instructions.push_back(BranchInst);
2452   }
2453   return false;
2454 }
2455
2456 void MipsAsmParser::expandMemInst(MCInst &Inst, SMLoc IDLoc,
2457                                   SmallVectorImpl<MCInst> &Instructions,
2458                                   bool isLoad, bool isImmOpnd) {
2459   MCInst TempInst;
2460   unsigned ImmOffset, HiOffset, LoOffset;
2461   const MCExpr *ExprOffset;
2462   unsigned TmpRegNum;
2463   // 1st operand is either the source or destination register.
2464   assert(Inst.getOperand(0).isReg() && "expected register operand kind");
2465   unsigned RegOpNum = Inst.getOperand(0).getReg();
2466   // 2nd operand is the base register.
2467   assert(Inst.getOperand(1).isReg() && "expected register operand kind");
2468   unsigned BaseRegNum = Inst.getOperand(1).getReg();
2469   // 3rd operand is either an immediate or expression.
2470   if (isImmOpnd) {
2471     assert(Inst.getOperand(2).isImm() && "expected immediate operand kind");
2472     ImmOffset = Inst.getOperand(2).getImm();
2473     LoOffset = ImmOffset & 0x0000ffff;
2474     HiOffset = (ImmOffset & 0xffff0000) >> 16;
2475     // If msb of LoOffset is 1(negative number) we must increment HiOffset.
2476     if (LoOffset & 0x8000)
2477       HiOffset++;
2478   } else
2479     ExprOffset = Inst.getOperand(2).getExpr();
2480   // All instructions will have the same location.
2481   TempInst.setLoc(IDLoc);
2482   // These are some of the types of expansions we perform here:
2483   // 1) lw $8, sym        => lui $8, %hi(sym)
2484   //                         lw $8, %lo(sym)($8)
2485   // 2) lw $8, offset($9) => lui $8, %hi(offset)
2486   //                         add $8, $8, $9
2487   //                         lw $8, %lo(offset)($9)
2488   // 3) lw $8, offset($8) => lui $at, %hi(offset)
2489   //                         add $at, $at, $8
2490   //                         lw $8, %lo(offset)($at)
2491   // 4) sw $8, sym        => lui $at, %hi(sym)
2492   //                         sw $8, %lo(sym)($at)
2493   // 5) sw $8, offset($8) => lui $at, %hi(offset)
2494   //                         add $at, $at, $8
2495   //                         sw $8, %lo(offset)($at)
2496   // 6) ldc1 $f0, sym     => lui $at, %hi(sym)
2497   //                         ldc1 $f0, %lo(sym)($at)
2498   //
2499   // For load instructions we can use the destination register as a temporary
2500   // if base and dst are different (examples 1 and 2) and if the base register
2501   // is general purpose otherwise we must use $at (example 6) and error if it's
2502   // not available. For stores we must use $at (examples 4 and 5) because we
2503   // must not clobber the source register setting up the offset.
2504   const MCInstrDesc &Desc = getInstDesc(Inst.getOpcode());
2505   int16_t RegClassOp0 = Desc.OpInfo[0].RegClass;
2506   unsigned RegClassIDOp0 =
2507       getContext().getRegisterInfo()->getRegClass(RegClassOp0).getID();
2508   bool IsGPR = (RegClassIDOp0 == Mips::GPR32RegClassID) ||
2509                (RegClassIDOp0 == Mips::GPR64RegClassID);
2510   if (isLoad && IsGPR && (BaseRegNum != RegOpNum))
2511     TmpRegNum = RegOpNum;
2512   else {
2513     // At this point we need AT to perform the expansions and we exit if it is
2514     // not available.
2515     TmpRegNum = getATReg(IDLoc);
2516     if (!TmpRegNum)
2517       return;
2518   }
2519
2520   TempInst.setOpcode(Mips::LUi);
2521   TempInst.addOperand(MCOperand::createReg(TmpRegNum));
2522   if (isImmOpnd)
2523     TempInst.addOperand(MCOperand::createImm(HiOffset));
2524   else {
2525     const MCExpr *HiExpr = evaluateRelocExpr(ExprOffset, "hi");
2526     TempInst.addOperand(MCOperand::createExpr(HiExpr));
2527   }
2528   // Add the instruction to the list.
2529   Instructions.push_back(TempInst);
2530   // Prepare TempInst for next instruction.
2531   TempInst.clear();
2532   // Add temp register to base.
2533   if (BaseRegNum != Mips::ZERO) {
2534     TempInst.setOpcode(Mips::ADDu);
2535     TempInst.addOperand(MCOperand::createReg(TmpRegNum));
2536     TempInst.addOperand(MCOperand::createReg(TmpRegNum));
2537     TempInst.addOperand(MCOperand::createReg(BaseRegNum));
2538     Instructions.push_back(TempInst);
2539     TempInst.clear();
2540   }
2541   // And finally, create original instruction with low part
2542   // of offset and new base.
2543   TempInst.setOpcode(Inst.getOpcode());
2544   TempInst.addOperand(MCOperand::createReg(RegOpNum));
2545   TempInst.addOperand(MCOperand::createReg(TmpRegNum));
2546   if (isImmOpnd)
2547     TempInst.addOperand(MCOperand::createImm(LoOffset));
2548   else {
2549     const MCExpr *LoExpr = evaluateRelocExpr(ExprOffset, "lo");
2550     TempInst.addOperand(MCOperand::createExpr(LoExpr));
2551   }
2552   Instructions.push_back(TempInst);
2553   TempInst.clear();
2554 }
2555
2556 bool
2557 MipsAsmParser::expandLoadStoreMultiple(MCInst &Inst, SMLoc IDLoc,
2558                                        SmallVectorImpl<MCInst> &Instructions) {
2559   unsigned OpNum = Inst.getNumOperands();
2560   unsigned Opcode = Inst.getOpcode();
2561   unsigned NewOpcode = Opcode == Mips::SWM_MM ? Mips::SWM32_MM : Mips::LWM32_MM;
2562
2563   assert (Inst.getOperand(OpNum - 1).isImm() &&
2564           Inst.getOperand(OpNum - 2).isReg() &&
2565           Inst.getOperand(OpNum - 3).isReg() && "Invalid instruction operand.");
2566
2567   if (OpNum < 8 && Inst.getOperand(OpNum - 1).getImm() <= 60 &&
2568       Inst.getOperand(OpNum - 1).getImm() >= 0 &&
2569       Inst.getOperand(OpNum - 2).getReg() == Mips::SP &&
2570       Inst.getOperand(OpNum - 3).getReg() == Mips::RA)
2571     // It can be implemented as SWM16 or LWM16 instruction.
2572     NewOpcode = Opcode == Mips::SWM_MM ? Mips::SWM16_MM : Mips::LWM16_MM;
2573
2574   Inst.setOpcode(NewOpcode);
2575   Instructions.push_back(Inst);
2576   return false;
2577 }
2578
2579 bool MipsAsmParser::expandCondBranches(MCInst &Inst, SMLoc IDLoc,
2580                                        SmallVectorImpl<MCInst> &Instructions) {
2581   unsigned PseudoOpcode = Inst.getOpcode();
2582   unsigned SrcReg = Inst.getOperand(0).getReg();
2583   unsigned TrgReg = Inst.getOperand(1).getReg();
2584   const MCExpr *OffsetExpr = Inst.getOperand(2).getExpr();
2585
2586   unsigned ZeroSrcOpcode, ZeroTrgOpcode;
2587   bool ReverseOrderSLT, IsUnsigned, IsLikely, AcceptsEquality;
2588
2589   switch (PseudoOpcode) {
2590   case Mips::BLT:
2591   case Mips::BLTU:
2592   case Mips::BLTL:
2593   case Mips::BLTUL:
2594     AcceptsEquality = false;
2595     ReverseOrderSLT = false;
2596     IsUnsigned = ((PseudoOpcode == Mips::BLTU) || (PseudoOpcode == Mips::BLTUL));
2597     IsLikely = ((PseudoOpcode == Mips::BLTL) || (PseudoOpcode == Mips::BLTUL));
2598     ZeroSrcOpcode = Mips::BGTZ;
2599     ZeroTrgOpcode = Mips::BLTZ;
2600     break;
2601   case Mips::BLE:
2602   case Mips::BLEU:
2603   case Mips::BLEL:
2604   case Mips::BLEUL:
2605     AcceptsEquality = true;
2606     ReverseOrderSLT = true;
2607     IsUnsigned = ((PseudoOpcode == Mips::BLEU) || (PseudoOpcode == Mips::BLEUL));
2608     IsLikely = ((PseudoOpcode == Mips::BLEL) || (PseudoOpcode == Mips::BLEUL));
2609     ZeroSrcOpcode = Mips::BGEZ;
2610     ZeroTrgOpcode = Mips::BLEZ;
2611     break;
2612   case Mips::BGE:
2613   case Mips::BGEU:
2614   case Mips::BGEL:
2615   case Mips::BGEUL:
2616     AcceptsEquality = true;
2617     ReverseOrderSLT = false;
2618     IsUnsigned = ((PseudoOpcode == Mips::BGEU) || (PseudoOpcode == Mips::BGEUL));
2619     IsLikely = ((PseudoOpcode == Mips::BGEL) || (PseudoOpcode == Mips::BGEUL));
2620     ZeroSrcOpcode = Mips::BLEZ;
2621     ZeroTrgOpcode = Mips::BGEZ;
2622     break;
2623   case Mips::BGT:
2624   case Mips::BGTU:
2625   case Mips::BGTL:
2626   case Mips::BGTUL:
2627     AcceptsEquality = false;
2628     ReverseOrderSLT = true;
2629     IsUnsigned = ((PseudoOpcode == Mips::BGTU) || (PseudoOpcode == Mips::BGTUL));
2630     IsLikely = ((PseudoOpcode == Mips::BGTL) || (PseudoOpcode == Mips::BGTUL));
2631     ZeroSrcOpcode = Mips::BLTZ;
2632     ZeroTrgOpcode = Mips::BGTZ;
2633     break;
2634   default:
2635     llvm_unreachable("unknown opcode for branch pseudo-instruction");
2636   }
2637
2638   MCInst BranchInst;
2639   bool IsTrgRegZero = (TrgReg == Mips::ZERO);
2640   bool IsSrcRegZero = (SrcReg == Mips::ZERO);
2641   if (IsSrcRegZero && IsTrgRegZero) {
2642     // FIXME: All of these Opcode-specific if's are needed for compatibility
2643     // with GAS' behaviour. However, they may not generate the most efficient
2644     // code in some circumstances.
2645     if (PseudoOpcode == Mips::BLT) {
2646       BranchInst.setOpcode(Mips::BLTZ);
2647       BranchInst.addOperand(MCOperand::createReg(Mips::ZERO));
2648       BranchInst.addOperand(MCOperand::createExpr(OffsetExpr));
2649       Instructions.push_back(BranchInst);
2650       return false;
2651     }
2652     if (PseudoOpcode == Mips::BLE) {
2653       BranchInst.setOpcode(Mips::BLEZ);
2654       BranchInst.addOperand(MCOperand::createReg(Mips::ZERO));
2655       BranchInst.addOperand(MCOperand::createExpr(OffsetExpr));
2656       Instructions.push_back(BranchInst);
2657       Warning(IDLoc, "branch is always taken");
2658       return false;
2659     }
2660     if (PseudoOpcode == Mips::BGE) {
2661       BranchInst.setOpcode(Mips::BGEZ);
2662       BranchInst.addOperand(MCOperand::createReg(Mips::ZERO));
2663       BranchInst.addOperand(MCOperand::createExpr(OffsetExpr));
2664       Instructions.push_back(BranchInst);
2665       Warning(IDLoc, "branch is always taken");
2666       return false;
2667     }
2668     if (PseudoOpcode == Mips::BGT) {
2669       BranchInst.setOpcode(Mips::BGTZ);
2670       BranchInst.addOperand(MCOperand::createReg(Mips::ZERO));
2671       BranchInst.addOperand(MCOperand::createExpr(OffsetExpr));
2672       Instructions.push_back(BranchInst);
2673       return false;
2674     }
2675     if (PseudoOpcode == Mips::BGTU) {
2676       BranchInst.setOpcode(Mips::BNE);
2677       BranchInst.addOperand(MCOperand::createReg(Mips::ZERO));
2678       BranchInst.addOperand(MCOperand::createReg(Mips::ZERO));
2679       BranchInst.addOperand(MCOperand::createExpr(OffsetExpr));
2680       Instructions.push_back(BranchInst);
2681       return false;
2682     }
2683     if (AcceptsEquality) {
2684       // If both registers are $0 and the pseudo-branch accepts equality, it
2685       // will always be taken, so we emit an unconditional branch.
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 both registers are $0 and the pseudo-branch does not accept
2695     // equality, it will never be taken, so we don't have to emit anything.
2696     return false;
2697   }
2698   if (IsSrcRegZero || IsTrgRegZero) {
2699     if ((IsSrcRegZero && PseudoOpcode == Mips::BGTU) ||
2700         (IsTrgRegZero && PseudoOpcode == Mips::BLTU)) {
2701       // If the $rs is $0 and the pseudo-branch is BGTU (0 > x) or
2702       // if the $rt is $0 and the pseudo-branch is BLTU (x < 0),
2703       // the pseudo-branch will never be taken, so we don't emit anything.
2704       // This only applies to unsigned pseudo-branches.
2705       return false;
2706     }
2707     if ((IsSrcRegZero && PseudoOpcode == Mips::BLEU) ||
2708         (IsTrgRegZero && PseudoOpcode == Mips::BGEU)) {
2709       // If the $rs is $0 and the pseudo-branch is BLEU (0 <= x) or
2710       // if the $rt is $0 and the pseudo-branch is BGEU (x >= 0),
2711       // the pseudo-branch will always be taken, so we emit an unconditional
2712       // branch.
2713       // This only applies to unsigned pseudo-branches.
2714       BranchInst.setOpcode(Mips::BEQ);
2715       BranchInst.addOperand(MCOperand::createReg(Mips::ZERO));
2716       BranchInst.addOperand(MCOperand::createReg(Mips::ZERO));
2717       BranchInst.addOperand(MCOperand::createExpr(OffsetExpr));
2718       Instructions.push_back(BranchInst);
2719       Warning(IDLoc, "branch is always taken");
2720       return false;
2721     }
2722     if (IsUnsigned) {
2723       // If the $rs is $0 and the pseudo-branch is BLTU (0 < x) or
2724       // if the $rt is $0 and the pseudo-branch is BGTU (x > 0),
2725       // the pseudo-branch will be taken only when the non-zero register is
2726       // different from 0, so we emit a BNEZ.
2727       //
2728       // If the $rs is $0 and the pseudo-branch is BGEU (0 >= x) or
2729       // if the $rt is $0 and the pseudo-branch is BLEU (x <= 0),
2730       // the pseudo-branch will be taken only when the non-zero register is
2731       // equal to 0, so we emit a BEQZ.
2732       //
2733       // Because only BLEU and BGEU branch on equality, we can use the
2734       // AcceptsEquality variable to decide when to emit the BEQZ.
2735       BranchInst.setOpcode(AcceptsEquality ? Mips::BEQ : Mips::BNE);
2736       BranchInst.addOperand(
2737           MCOperand::createReg(IsSrcRegZero ? TrgReg : SrcReg));
2738       BranchInst.addOperand(MCOperand::createReg(Mips::ZERO));
2739       BranchInst.addOperand(MCOperand::createExpr(OffsetExpr));
2740       Instructions.push_back(BranchInst);
2741       return false;
2742     }
2743     // If we have a signed pseudo-branch and one of the registers is $0,
2744     // we can use an appropriate compare-to-zero branch. We select which one
2745     // to use in the switch statement above.
2746     BranchInst.setOpcode(IsSrcRegZero ? ZeroSrcOpcode : ZeroTrgOpcode);
2747     BranchInst.addOperand(MCOperand::createReg(IsSrcRegZero ? TrgReg : SrcReg));
2748     BranchInst.addOperand(MCOperand::createExpr(OffsetExpr));
2749     Instructions.push_back(BranchInst);
2750     return false;
2751   }
2752
2753   // If neither the SrcReg nor the TrgReg are $0, we need AT to perform the
2754   // expansions. If it is not available, we return.
2755   unsigned ATRegNum = getATReg(IDLoc);
2756   if (!ATRegNum)
2757     return true;
2758
2759   warnIfNoMacro(IDLoc);
2760
2761   // SLT fits well with 2 of our 4 pseudo-branches:
2762   //   BLT, where $rs < $rt, translates into "slt $at, $rs, $rt" and
2763   //   BGT, where $rs > $rt, translates into "slt $at, $rt, $rs".
2764   // If the result of the SLT is 1, we branch, and if it's 0, we don't.
2765   // This is accomplished by using a BNEZ with the result of the SLT.
2766   //
2767   // The other 2 pseudo-branches are opposites of the above 2 (BGE with BLT
2768   // and BLE with BGT), so we change the BNEZ into a a BEQZ.
2769   // Because only BGE and BLE branch on equality, we can use the
2770   // AcceptsEquality variable to decide when to emit the BEQZ.
2771   // Note that the order of the SLT arguments doesn't change between
2772   // opposites.
2773   //
2774   // The same applies to the unsigned variants, except that SLTu is used
2775   // instead of SLT.
2776   MCInst SetInst;
2777   SetInst.setOpcode(IsUnsigned ? Mips::SLTu : Mips::SLT);
2778   SetInst.addOperand(MCOperand::createReg(ATRegNum));
2779   SetInst.addOperand(MCOperand::createReg(ReverseOrderSLT ? TrgReg : SrcReg));
2780   SetInst.addOperand(MCOperand::createReg(ReverseOrderSLT ? SrcReg : TrgReg));
2781   Instructions.push_back(SetInst);
2782
2783   if (!IsLikely)
2784     BranchInst.setOpcode(AcceptsEquality ? Mips::BEQ : Mips::BNE);
2785   else
2786     BranchInst.setOpcode(AcceptsEquality ? Mips::BEQL : Mips::BNEL);
2787   BranchInst.addOperand(MCOperand::createReg(ATRegNum));
2788   BranchInst.addOperand(MCOperand::createReg(Mips::ZERO));
2789   BranchInst.addOperand(MCOperand::createExpr(OffsetExpr));
2790   Instructions.push_back(BranchInst);
2791   return false;
2792 }
2793
2794 bool MipsAsmParser::expandDiv(MCInst &Inst, SMLoc IDLoc,
2795                               SmallVectorImpl<MCInst> &Instructions,
2796                               const bool IsMips64, const bool Signed) {
2797   if (hasMips32r6()) {
2798     Error(IDLoc, "instruction not supported on mips32r6 or mips64r6");
2799     return false;
2800   }
2801
2802   warnIfNoMacro(IDLoc);
2803
2804   const MCOperand &RsRegOp = Inst.getOperand(0);
2805   assert(RsRegOp.isReg() && "expected register operand kind");
2806   unsigned RsReg = RsRegOp.getReg();
2807
2808   const MCOperand &RtRegOp = Inst.getOperand(1);
2809   assert(RtRegOp.isReg() && "expected register operand kind");
2810   unsigned RtReg = RtRegOp.getReg();
2811   unsigned DivOp;
2812   unsigned ZeroReg;
2813
2814   if (IsMips64) {
2815     DivOp = Signed ? Mips::DSDIV : Mips::DUDIV;
2816     ZeroReg = Mips::ZERO_64;
2817   } else {
2818     DivOp = Signed ? Mips::SDIV : Mips::UDIV;
2819     ZeroReg = Mips::ZERO;
2820   }
2821
2822   bool UseTraps = useTraps();
2823
2824   if (RsReg == Mips::ZERO || RsReg == Mips::ZERO_64) {
2825     if (RtReg == Mips::ZERO || RtReg == Mips::ZERO_64)
2826       Warning(IDLoc, "dividing zero by zero");
2827     if (IsMips64) {
2828       if (Signed && (RtReg == Mips::ZERO || RtReg == Mips::ZERO_64)) {
2829         if (UseTraps) {
2830           emitRRI(Mips::TEQ, RtReg, ZeroReg, 0x7, IDLoc, Instructions);
2831           return false;
2832         }
2833
2834         emitII(Mips::BREAK, 0x7, 0, IDLoc, Instructions);
2835         return false;
2836       }
2837     } else {
2838       emitRR(DivOp, RsReg, RtReg, IDLoc, Instructions);
2839       return false;
2840     }
2841   }
2842
2843   if (RtReg == Mips::ZERO || RtReg == Mips::ZERO_64) {
2844     Warning(IDLoc, "division by zero");
2845     if (Signed) {
2846       if (UseTraps) {
2847         emitRRI(Mips::TEQ, RtReg, ZeroReg, 0x7, IDLoc, Instructions);
2848         return false;
2849       }
2850
2851       emitII(Mips::BREAK, 0x7, 0, IDLoc, Instructions);
2852       return false;
2853     }
2854   }
2855
2856   // FIXME: The values for these two BranchTarget variables may be different in
2857   // micromips. These magic numbers need to be removed.
2858   unsigned BranchTargetNoTraps;
2859   unsigned BranchTarget;
2860
2861   if (UseTraps) {
2862     BranchTarget = IsMips64 ? 12 : 8;
2863     emitRRI(Mips::TEQ, RtReg, ZeroReg, 0x7, IDLoc, Instructions);
2864   } else {
2865     BranchTarget = IsMips64 ? 20 : 16;
2866     BranchTargetNoTraps = 8;
2867     // Branch to the li instruction.
2868     emitRRI(Mips::BNE, RtReg, ZeroReg, BranchTargetNoTraps, IDLoc,
2869             Instructions);
2870   }
2871
2872   emitRR(DivOp, RsReg, RtReg, IDLoc, Instructions);
2873
2874   if (!UseTraps)
2875     emitII(Mips::BREAK, 0x7, 0, IDLoc, Instructions);
2876
2877   if (!Signed) {
2878     emitR(Mips::MFLO, RsReg, IDLoc, Instructions);
2879     return false;
2880   }
2881
2882   unsigned ATReg = getATReg(IDLoc);
2883   if (!ATReg)
2884     return true;
2885
2886   emitRRI(Mips::ADDiu, ATReg, ZeroReg, -1, IDLoc, Instructions);
2887   if (IsMips64) {
2888     // Branch to the mflo instruction.
2889     emitRRI(Mips::BNE, RtReg, ATReg, BranchTarget, IDLoc, Instructions);
2890     emitRRI(Mips::ADDiu, ATReg, ZeroReg, 1, IDLoc, Instructions);
2891     emitRRI(Mips::DSLL32, ATReg, ATReg, 0x1f, IDLoc, Instructions);
2892   } else {
2893     // Branch to the mflo instruction.
2894     emitRRI(Mips::BNE, RtReg, ATReg, BranchTarget, IDLoc, Instructions);
2895     emitRI(Mips::LUi, ATReg, (uint16_t)0x8000, IDLoc, Instructions);
2896   }
2897
2898   if (UseTraps)
2899     emitRRI(Mips::TEQ, RsReg, ATReg, 0x6, IDLoc, Instructions);
2900   else {
2901     // Branch to the mflo instruction.
2902     emitRRI(Mips::BNE, RsReg, ATReg, BranchTargetNoTraps, IDLoc, Instructions);
2903     emitRRI(Mips::SLL, ZeroReg, ZeroReg, 0, IDLoc, Instructions);
2904     emitII(Mips::BREAK, 0x6, 0, IDLoc, Instructions);
2905   }
2906   emitR(Mips::MFLO, RsReg, IDLoc, Instructions);
2907   return false;
2908 }
2909
2910 bool MipsAsmParser::expandUlhu(MCInst &Inst, SMLoc IDLoc,
2911                                SmallVectorImpl<MCInst> &Instructions) {
2912   if (hasMips32r6() || hasMips64r6()) {
2913     Error(IDLoc, "instruction not supported on mips32r6 or mips64r6");
2914     return false;
2915   }
2916
2917   warnIfNoMacro(IDLoc);
2918
2919   const MCOperand &DstRegOp = Inst.getOperand(0);
2920   assert(DstRegOp.isReg() && "expected register operand kind");
2921
2922   const MCOperand &SrcRegOp = Inst.getOperand(1);
2923   assert(SrcRegOp.isReg() && "expected register operand kind");
2924
2925   const MCOperand &OffsetImmOp = Inst.getOperand(2);
2926   assert(OffsetImmOp.isImm() && "expected immediate operand kind");
2927
2928   unsigned DstReg = DstRegOp.getReg();
2929   unsigned SrcReg = SrcRegOp.getReg();
2930   int64_t OffsetValue = OffsetImmOp.getImm();
2931
2932   // NOTE: We always need AT for ULHU, as it is always used as the source
2933   // register for one of the LBu's.
2934   unsigned ATReg = getATReg(IDLoc);
2935   if (!ATReg)
2936     return true;
2937
2938   // When the value of offset+1 does not fit in 16 bits, we have to load the
2939   // offset in AT, (D)ADDu the original source register (if there was one), and
2940   // then use AT as the source register for the 2 generated LBu's.
2941   bool LoadedOffsetInAT = false;
2942   if (!isInt<16>(OffsetValue + 1) || !isInt<16>(OffsetValue)) {
2943     LoadedOffsetInAT = true;
2944
2945     if (loadImmediate(OffsetValue, ATReg, Mips::NoRegister, !ABI.ArePtrs64bit(),
2946                       true, IDLoc, Instructions))
2947       return true;
2948
2949     // NOTE: We do this (D)ADDu here instead of doing it in loadImmediate()
2950     // because it will make our output more similar to GAS'. For example,
2951     // generating an "ori $1, $zero, 32768" followed by an "addu $1, $1, $9",
2952     // instead of just an "ori $1, $9, 32768".
2953     // NOTE: If there is no source register specified in the ULHU, the parser
2954     // will interpret it as $0.
2955     if (SrcReg != Mips::ZERO && SrcReg != Mips::ZERO_64)
2956       createAddu(ATReg, ATReg, SrcReg, ABI.ArePtrs64bit(), Instructions);
2957   }
2958
2959   unsigned FirstLbuDstReg = LoadedOffsetInAT ? DstReg : ATReg;
2960   unsigned SecondLbuDstReg = LoadedOffsetInAT ? ATReg : DstReg;
2961   unsigned LbuSrcReg = LoadedOffsetInAT ? ATReg : SrcReg;
2962
2963   int64_t FirstLbuOffset = 0, SecondLbuOffset = 0;
2964   if (isLittle()) {
2965     FirstLbuOffset = LoadedOffsetInAT ? 1 : (OffsetValue + 1);
2966     SecondLbuOffset = LoadedOffsetInAT ? 0 : OffsetValue;
2967   } else {
2968     FirstLbuOffset = LoadedOffsetInAT ? 0 : OffsetValue;
2969     SecondLbuOffset = LoadedOffsetInAT ? 1 : (OffsetValue + 1);
2970   }
2971
2972   unsigned SllReg = LoadedOffsetInAT ? DstReg : ATReg;
2973
2974   MCInst TmpInst;
2975   TmpInst.setOpcode(Mips::LBu);
2976   TmpInst.addOperand(MCOperand::createReg(FirstLbuDstReg));
2977   TmpInst.addOperand(MCOperand::createReg(LbuSrcReg));
2978   TmpInst.addOperand(MCOperand::createImm(FirstLbuOffset));
2979   Instructions.push_back(TmpInst);
2980
2981   TmpInst.clear();
2982   TmpInst.setOpcode(Mips::LBu);
2983   TmpInst.addOperand(MCOperand::createReg(SecondLbuDstReg));
2984   TmpInst.addOperand(MCOperand::createReg(LbuSrcReg));
2985   TmpInst.addOperand(MCOperand::createImm(SecondLbuOffset));
2986   Instructions.push_back(TmpInst);
2987
2988   TmpInst.clear();
2989   TmpInst.setOpcode(Mips::SLL);
2990   TmpInst.addOperand(MCOperand::createReg(SllReg));
2991   TmpInst.addOperand(MCOperand::createReg(SllReg));
2992   TmpInst.addOperand(MCOperand::createImm(8));
2993   Instructions.push_back(TmpInst);
2994
2995   TmpInst.clear();
2996   TmpInst.setOpcode(Mips::OR);
2997   TmpInst.addOperand(MCOperand::createReg(DstReg));
2998   TmpInst.addOperand(MCOperand::createReg(DstReg));
2999   TmpInst.addOperand(MCOperand::createReg(ATReg));
3000   Instructions.push_back(TmpInst);
3001
3002   return false;
3003 }
3004
3005 bool MipsAsmParser::expandUlw(MCInst &Inst, SMLoc IDLoc,
3006                               SmallVectorImpl<MCInst> &Instructions) {
3007   if (hasMips32r6() || hasMips64r6()) {
3008     Error(IDLoc, "instruction not supported on mips32r6 or mips64r6");
3009     return false;
3010   }
3011
3012   const MCOperand &DstRegOp = Inst.getOperand(0);
3013   assert(DstRegOp.isReg() && "expected register operand kind");
3014
3015   const MCOperand &SrcRegOp = Inst.getOperand(1);
3016   assert(SrcRegOp.isReg() && "expected register operand kind");
3017
3018   const MCOperand &OffsetImmOp = Inst.getOperand(2);
3019   assert(OffsetImmOp.isImm() && "expected immediate operand kind");
3020
3021   unsigned SrcReg = SrcRegOp.getReg();
3022   int64_t OffsetValue = OffsetImmOp.getImm();
3023   unsigned ATReg = 0;
3024
3025   // When the value of offset+3 does not fit in 16 bits, we have to load the
3026   // offset in AT, (D)ADDu the original source register (if there was one), and
3027   // then use AT as the source register for the generated LWL and LWR.
3028   bool LoadedOffsetInAT = false;
3029   if (!isInt<16>(OffsetValue + 3) || !isInt<16>(OffsetValue)) {
3030     ATReg = getATReg(IDLoc);
3031     if (!ATReg)
3032       return true;
3033     LoadedOffsetInAT = true;
3034
3035     warnIfNoMacro(IDLoc);
3036
3037     if (loadImmediate(OffsetValue, ATReg, Mips::NoRegister, !ABI.ArePtrs64bit(),
3038                       true, IDLoc, Instructions))
3039       return true;
3040
3041     // NOTE: We do this (D)ADDu here instead of doing it in loadImmediate()
3042     // because it will make our output more similar to GAS'. For example,
3043     // generating an "ori $1, $zero, 32768" followed by an "addu $1, $1, $9",
3044     // instead of just an "ori $1, $9, 32768".
3045     // NOTE: If there is no source register specified in the ULW, the parser
3046     // will interpret it as $0.
3047     if (SrcReg != Mips::ZERO && SrcReg != Mips::ZERO_64)
3048       createAddu(ATReg, ATReg, SrcReg, ABI.ArePtrs64bit(), Instructions);
3049   }
3050
3051   unsigned FinalSrcReg = LoadedOffsetInAT ? ATReg : SrcReg;
3052   int64_t LeftLoadOffset = 0, RightLoadOffset  = 0;
3053   if (isLittle()) {
3054     LeftLoadOffset = LoadedOffsetInAT ? 3 : (OffsetValue + 3);
3055     RightLoadOffset  = LoadedOffsetInAT ? 0 : OffsetValue;
3056   } else {
3057     LeftLoadOffset = LoadedOffsetInAT ? 0 : OffsetValue;
3058     RightLoadOffset  = LoadedOffsetInAT ? 3 : (OffsetValue + 3);
3059   }
3060
3061   MCInst LeftLoadInst;
3062   LeftLoadInst.setOpcode(Mips::LWL);
3063   LeftLoadInst.addOperand(DstRegOp);
3064   LeftLoadInst.addOperand(MCOperand::createReg(FinalSrcReg));
3065   LeftLoadInst.addOperand(MCOperand::createImm(LeftLoadOffset));
3066   Instructions.push_back(LeftLoadInst);
3067
3068   MCInst RightLoadInst;
3069   RightLoadInst.setOpcode(Mips::LWR);
3070   RightLoadInst.addOperand(DstRegOp);
3071   RightLoadInst.addOperand(MCOperand::createReg(FinalSrcReg));
3072   RightLoadInst.addOperand(MCOperand::createImm(RightLoadOffset ));
3073   Instructions.push_back(RightLoadInst);
3074
3075   return false;
3076 }
3077
3078 void MipsAsmParser::createNop(bool hasShortDelaySlot, SMLoc IDLoc,
3079                               SmallVectorImpl<MCInst> &Instructions) {
3080   MCInst NopInst;
3081   if (hasShortDelaySlot) {
3082     NopInst.setOpcode(Mips::MOVE16_MM);
3083     NopInst.addOperand(MCOperand::createReg(Mips::ZERO));
3084     NopInst.addOperand(MCOperand::createReg(Mips::ZERO));
3085   } else {
3086     NopInst.setOpcode(Mips::SLL);
3087     NopInst.addOperand(MCOperand::createReg(Mips::ZERO));
3088     NopInst.addOperand(MCOperand::createReg(Mips::ZERO));
3089     NopInst.addOperand(MCOperand::createImm(0));
3090   }
3091   Instructions.push_back(NopInst);
3092 }
3093
3094 void MipsAsmParser::createAddu(unsigned DstReg, unsigned SrcReg,
3095                                unsigned TrgReg, bool Is64Bit,
3096                                SmallVectorImpl<MCInst> &Instructions) {
3097   emitRRR(Is64Bit ? Mips::DADDu : Mips::ADDu, DstReg, SrcReg, TrgReg, SMLoc(),
3098           Instructions);
3099 }
3100
3101 unsigned MipsAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
3102   // As described by the Mips32r2 spec, the registers Rd and Rs for
3103   // jalr.hb must be different.
3104   unsigned Opcode = Inst.getOpcode();
3105
3106   if (Opcode == Mips::JALR_HB &&
3107       (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()))
3108     return Match_RequiresDifferentSrcAndDst;
3109
3110   return Match_Success;
3111 }
3112
3113 bool MipsAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
3114                                             OperandVector &Operands,
3115                                             MCStreamer &Out,
3116                                             uint64_t &ErrorInfo,
3117                                             bool MatchingInlineAsm) {
3118
3119   MCInst Inst;
3120   SmallVector<MCInst, 8> Instructions;
3121   unsigned MatchResult =
3122       MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
3123
3124   switch (MatchResult) {
3125   case Match_Success: {
3126     if (processInstruction(Inst, IDLoc, Instructions))
3127       return true;
3128     for (unsigned i = 0; i < Instructions.size(); i++)
3129       Out.EmitInstruction(Instructions[i], STI);
3130     return false;
3131   }
3132   case Match_MissingFeature:
3133     Error(IDLoc, "instruction requires a CPU feature not currently enabled");
3134     return true;
3135   case Match_InvalidOperand: {
3136     SMLoc ErrorLoc = IDLoc;
3137     if (ErrorInfo != ~0ULL) {
3138       if (ErrorInfo >= Operands.size())
3139         return Error(IDLoc, "too few operands for instruction");
3140
3141       ErrorLoc = ((MipsOperand &)*Operands[ErrorInfo]).getStartLoc();
3142       if (ErrorLoc == SMLoc())
3143         ErrorLoc = IDLoc;
3144     }
3145
3146     return Error(ErrorLoc, "invalid operand for instruction");
3147   }
3148   case Match_MnemonicFail:
3149     return Error(IDLoc, "invalid instruction");
3150   case Match_RequiresDifferentSrcAndDst:
3151     return Error(IDLoc, "source and destination must be different");
3152   }
3153
3154   llvm_unreachable("Implement any new match types added!");
3155 }
3156
3157 void MipsAsmParser::warnIfRegIndexIsAT(unsigned RegIndex, SMLoc Loc) {
3158   if (RegIndex != 0 && AssemblerOptions.back()->getATRegIndex() == RegIndex)
3159     Warning(Loc, "used $at (currently $" + Twine(RegIndex) +
3160                      ") without \".set noat\"");
3161 }
3162
3163 void MipsAsmParser::warnIfNoMacro(SMLoc Loc) {
3164   if (!AssemblerOptions.back()->isMacro())
3165     Warning(Loc, "macro instruction expanded into multiple instructions");
3166 }
3167
3168 void
3169 MipsAsmParser::printWarningWithFixIt(const Twine &Msg, const Twine &FixMsg,
3170                                      SMRange Range, bool ShowColors) {
3171   getSourceManager().PrintMessage(Range.Start, SourceMgr::DK_Warning, Msg,
3172                                   Range, SMFixIt(Range, FixMsg),
3173                                   ShowColors);
3174 }
3175
3176 int MipsAsmParser::matchCPURegisterName(StringRef Name) {
3177   int CC;
3178
3179   CC = StringSwitch<unsigned>(Name)
3180            .Case("zero", 0)
3181            .Case("at", 1)
3182            .Case("a0", 4)
3183            .Case("a1", 5)
3184            .Case("a2", 6)
3185            .Case("a3", 7)
3186            .Case("v0", 2)
3187            .Case("v1", 3)
3188            .Case("s0", 16)
3189            .Case("s1", 17)
3190            .Case("s2", 18)
3191            .Case("s3", 19)
3192            .Case("s4", 20)
3193            .Case("s5", 21)
3194            .Case("s6", 22)
3195            .Case("s7", 23)
3196            .Case("k0", 26)
3197            .Case("k1", 27)
3198            .Case("gp", 28)
3199            .Case("sp", 29)
3200            .Case("fp", 30)
3201            .Case("s8", 30)
3202            .Case("ra", 31)
3203            .Case("t0", 8)
3204            .Case("t1", 9)
3205            .Case("t2", 10)
3206            .Case("t3", 11)
3207            .Case("t4", 12)
3208            .Case("t5", 13)
3209            .Case("t6", 14)
3210            .Case("t7", 15)
3211            .Case("t8", 24)
3212            .Case("t9", 25)
3213            .Default(-1);
3214
3215   if (!(isABI_N32() || isABI_N64()))
3216     return CC;
3217
3218   if (12 <= CC && CC <= 15) {
3219     // Name is one of t4-t7
3220     AsmToken RegTok = getLexer().peekTok();
3221     SMRange RegRange = RegTok.getLocRange();
3222
3223     StringRef FixedName = StringSwitch<StringRef>(Name)
3224                               .Case("t4", "t0")
3225                               .Case("t5", "t1")
3226                               .Case("t6", "t2")
3227                               .Case("t7", "t3")
3228                               .Default("");
3229     assert(FixedName != "" &&  "Register name is not one of t4-t7.");
3230
3231     printWarningWithFixIt("register names $t4-$t7 are only available in O32.",
3232                           "Did you mean $" + FixedName + "?", RegRange);
3233   }
3234
3235   // Although SGI documentation just cuts out t0-t3 for n32/n64,
3236   // GNU pushes the values of t0-t3 to override the o32/o64 values for t4-t7
3237   // We are supporting both cases, so for t0-t3 we'll just push them to t4-t7.
3238   if (8 <= CC && CC <= 11)
3239     CC += 4;
3240
3241   if (CC == -1)
3242     CC = StringSwitch<unsigned>(Name)
3243              .Case("a4", 8)
3244              .Case("a5", 9)
3245              .Case("a6", 10)
3246              .Case("a7", 11)
3247              .Case("kt0", 26)
3248              .Case("kt1", 27)
3249              .Default(-1);
3250
3251   return CC;
3252 }
3253
3254 int MipsAsmParser::matchHWRegsRegisterName(StringRef Name) {
3255   int CC;
3256
3257   CC = StringSwitch<unsigned>(Name)
3258             .Case("hwr_cpunum", 0)
3259             .Case("hwr_synci_step", 1)
3260             .Case("hwr_cc", 2)
3261             .Case("hwr_ccres", 3)
3262             .Case("hwr_ulr", 29)
3263             .Default(-1);
3264
3265   return CC;
3266 }
3267
3268 int MipsAsmParser::matchFPURegisterName(StringRef Name) {
3269
3270   if (Name[0] == 'f') {
3271     StringRef NumString = Name.substr(1);
3272     unsigned IntVal;
3273     if (NumString.getAsInteger(10, IntVal))
3274       return -1;     // This is not an integer.
3275     if (IntVal > 31) // Maximum index for fpu register.
3276       return -1;
3277     return IntVal;
3278   }
3279   return -1;
3280 }
3281
3282 int MipsAsmParser::matchFCCRegisterName(StringRef Name) {
3283
3284   if (Name.startswith("fcc")) {
3285     StringRef NumString = Name.substr(3);
3286     unsigned IntVal;
3287     if (NumString.getAsInteger(10, IntVal))
3288       return -1;    // This is not an integer.
3289     if (IntVal > 7) // There are only 8 fcc registers.
3290       return -1;
3291     return IntVal;
3292   }
3293   return -1;
3294 }
3295
3296 int MipsAsmParser::matchACRegisterName(StringRef Name) {
3297
3298   if (Name.startswith("ac")) {
3299     StringRef NumString = Name.substr(2);
3300     unsigned IntVal;
3301     if (NumString.getAsInteger(10, IntVal))
3302       return -1;    // This is not an integer.
3303     if (IntVal > 3) // There are only 3 acc registers.
3304       return -1;
3305     return IntVal;
3306   }
3307   return -1;
3308 }
3309
3310 int MipsAsmParser::matchMSA128RegisterName(StringRef Name) {
3311   unsigned IntVal;
3312
3313   if (Name.front() != 'w' || Name.drop_front(1).getAsInteger(10, IntVal))
3314     return -1;
3315
3316   if (IntVal > 31)
3317     return -1;
3318
3319   return IntVal;
3320 }
3321
3322 int MipsAsmParser::matchMSA128CtrlRegisterName(StringRef Name) {
3323   int CC;
3324
3325   CC = StringSwitch<unsigned>(Name)
3326            .Case("msair", 0)
3327            .Case("msacsr", 1)
3328            .Case("msaaccess", 2)
3329            .Case("msasave", 3)
3330            .Case("msamodify", 4)
3331            .Case("msarequest", 5)
3332            .Case("msamap", 6)
3333            .Case("msaunmap", 7)
3334            .Default(-1);
3335
3336   return CC;
3337 }
3338
3339 unsigned MipsAsmParser::getATReg(SMLoc Loc) {
3340   unsigned ATIndex = AssemblerOptions.back()->getATRegIndex();
3341   if (ATIndex == 0) {
3342     reportParseError(Loc,
3343                      "pseudo-instruction requires $at, which is not available");
3344     return 0;
3345   }
3346   unsigned AT = getReg(
3347       (isGP64bit()) ? Mips::GPR64RegClassID : Mips::GPR32RegClassID, ATIndex);
3348   return AT;
3349 }
3350
3351 unsigned MipsAsmParser::getReg(int RC, int RegNo) {
3352   return *(getContext().getRegisterInfo()->getRegClass(RC).begin() + RegNo);
3353 }
3354
3355 unsigned MipsAsmParser::getGPR(int RegNo) {
3356   return getReg(isGP64bit() ? Mips::GPR64RegClassID : Mips::GPR32RegClassID,
3357                 RegNo);
3358 }
3359
3360 int MipsAsmParser::matchRegisterByNumber(unsigned RegNum, unsigned RegClass) {
3361   if (RegNum >
3362       getContext().getRegisterInfo()->getRegClass(RegClass).getNumRegs() - 1)
3363     return -1;
3364
3365   return getReg(RegClass, RegNum);
3366 }
3367
3368 bool MipsAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
3369   MCAsmParser &Parser = getParser();
3370   DEBUG(dbgs() << "parseOperand\n");
3371
3372   // Check if the current operand has a custom associated parser, if so, try to
3373   // custom parse the operand, or fallback to the general approach.
3374   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
3375   if (ResTy == MatchOperand_Success)
3376     return false;
3377   // If there wasn't a custom match, try the generic matcher below. Otherwise,
3378   // there was a match, but an error occurred, in which case, just return that
3379   // the operand parsing failed.
3380   if (ResTy == MatchOperand_ParseFail)
3381     return true;
3382
3383   DEBUG(dbgs() << ".. Generic Parser\n");
3384
3385   switch (getLexer().getKind()) {
3386   default:
3387     Error(Parser.getTok().getLoc(), "unexpected token in operand");
3388     return true;
3389   case AsmToken::Dollar: {
3390     // Parse the register.
3391     SMLoc S = Parser.getTok().getLoc();
3392
3393     // Almost all registers have been parsed by custom parsers. There is only
3394     // one exception to this. $zero (and it's alias $0) will reach this point
3395     // for div, divu, and similar instructions because it is not an operand
3396     // to the instruction definition but an explicit register. Special case
3397     // this situation for now.
3398     if (parseAnyRegister(Operands) != MatchOperand_NoMatch)
3399       return false;
3400
3401     // Maybe it is a symbol reference.
3402     StringRef Identifier;
3403     if (Parser.parseIdentifier(Identifier))
3404       return true;
3405
3406     SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3407     MCSymbol *Sym = getContext().getOrCreateSymbol("$" + Identifier);
3408     // Otherwise create a symbol reference.
3409     const MCExpr *Res =
3410         MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
3411
3412     Operands.push_back(MipsOperand::CreateImm(Res, S, E, *this));
3413     return false;
3414   }
3415   // Else drop to expression parsing.
3416   case AsmToken::LParen:
3417   case AsmToken::Minus:
3418   case AsmToken::Plus:
3419   case AsmToken::Integer:
3420   case AsmToken::Tilde:
3421   case AsmToken::String: {
3422     DEBUG(dbgs() << ".. generic integer\n");
3423     OperandMatchResultTy ResTy = parseImm(Operands);
3424     return ResTy != MatchOperand_Success;
3425   }
3426   case AsmToken::Percent: {
3427     // It is a symbol reference or constant expression.
3428     const MCExpr *IdVal;
3429     SMLoc S = Parser.getTok().getLoc(); // Start location of the operand.
3430     if (parseRelocOperand(IdVal))
3431       return true;
3432
3433     SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3434
3435     Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
3436     return false;
3437   } // case AsmToken::Percent
3438   } // switch(getLexer().getKind())
3439   return true;
3440 }
3441
3442 const MCExpr *MipsAsmParser::evaluateRelocExpr(const MCExpr *Expr,
3443                                                StringRef RelocStr) {
3444   const MCExpr *Res;
3445   // Check the type of the expression.
3446   if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Expr)) {
3447     // It's a constant, evaluate reloc value.
3448     int16_t Val;
3449     switch (getVariantKind(RelocStr)) {
3450     case MCSymbolRefExpr::VK_Mips_ABS_LO:
3451       // Get the 1st 16-bits.
3452       Val = MCE->getValue() & 0xffff;
3453       break;
3454     case MCSymbolRefExpr::VK_Mips_ABS_HI:
3455       // Get the 2nd 16-bits. Also add 1 if bit 15 is 1, to compensate for low
3456       // 16 bits being negative.
3457       Val = ((MCE->getValue() + 0x8000) >> 16) & 0xffff;
3458       break;
3459     case MCSymbolRefExpr::VK_Mips_HIGHER:
3460       // Get the 3rd 16-bits.
3461       Val = ((MCE->getValue() + 0x80008000LL) >> 32) & 0xffff;
3462       break;
3463     case MCSymbolRefExpr::VK_Mips_HIGHEST:
3464       // Get the 4th 16-bits.
3465       Val = ((MCE->getValue() + 0x800080008000LL) >> 48) & 0xffff;
3466       break;
3467     default:
3468       report_fatal_error("unsupported reloc value");
3469     }
3470     return MCConstantExpr::create(Val, getContext());
3471   }
3472
3473   if (const MCSymbolRefExpr *MSRE = dyn_cast<MCSymbolRefExpr>(Expr)) {
3474     // It's a symbol, create a symbolic expression from the symbol.
3475     const MCSymbol *Symbol = &MSRE->getSymbol();
3476     MCSymbolRefExpr::VariantKind VK = getVariantKind(RelocStr);
3477     Res = MCSymbolRefExpr::create(Symbol, VK, getContext());
3478     return Res;
3479   }
3480
3481   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
3482     MCSymbolRefExpr::VariantKind VK = getVariantKind(RelocStr);
3483
3484     // Try to create target expression.
3485     if (MipsMCExpr::isSupportedBinaryExpr(VK, BE))
3486       return MipsMCExpr::create(VK, Expr, getContext());
3487
3488     const MCExpr *LExp = evaluateRelocExpr(BE->getLHS(), RelocStr);
3489     const MCExpr *RExp = evaluateRelocExpr(BE->getRHS(), RelocStr);
3490     Res = MCBinaryExpr::create(BE->getOpcode(), LExp, RExp, getContext());
3491     return Res;
3492   }
3493
3494   if (const MCUnaryExpr *UN = dyn_cast<MCUnaryExpr>(Expr)) {
3495     const MCExpr *UnExp = evaluateRelocExpr(UN->getSubExpr(), RelocStr);
3496     Res = MCUnaryExpr::create(UN->getOpcode(), UnExp, getContext());
3497     return Res;
3498   }
3499   // Just return the original expression.
3500   return Expr;
3501 }
3502
3503 bool MipsAsmParser::isEvaluated(const MCExpr *Expr) {
3504
3505   switch (Expr->getKind()) {
3506   case MCExpr::Constant:
3507     return true;
3508   case MCExpr::SymbolRef:
3509     return (cast<MCSymbolRefExpr>(Expr)->getKind() != MCSymbolRefExpr::VK_None);
3510   case MCExpr::Binary:
3511     if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
3512       if (!isEvaluated(BE->getLHS()))
3513         return false;
3514       return isEvaluated(BE->getRHS());
3515     }
3516   case MCExpr::Unary:
3517     return isEvaluated(cast<MCUnaryExpr>(Expr)->getSubExpr());
3518   case MCExpr::Target:
3519     return true;
3520   }
3521   return false;
3522 }
3523
3524 bool MipsAsmParser::parseRelocOperand(const MCExpr *&Res) {
3525   MCAsmParser &Parser = getParser();
3526   Parser.Lex();                          // Eat the % token.
3527   const AsmToken &Tok = Parser.getTok(); // Get next token, operation.
3528   if (Tok.isNot(AsmToken::Identifier))
3529     return true;
3530
3531   std::string Str = Tok.getIdentifier();
3532
3533   Parser.Lex(); // Eat the identifier.
3534   // Now make an expression from the rest of the operand.
3535   const MCExpr *IdVal;
3536   SMLoc EndLoc;
3537
3538   if (getLexer().getKind() == AsmToken::LParen) {
3539     while (1) {
3540       Parser.Lex(); // Eat the '(' token.
3541       if (getLexer().getKind() == AsmToken::Percent) {
3542         Parser.Lex(); // Eat the % token.
3543         const AsmToken &nextTok = Parser.getTok();
3544         if (nextTok.isNot(AsmToken::Identifier))
3545           return true;
3546         Str += "(%";
3547         Str += nextTok.getIdentifier();
3548         Parser.Lex(); // Eat the identifier.
3549         if (getLexer().getKind() != AsmToken::LParen)
3550           return true;
3551       } else
3552         break;
3553     }
3554     if (getParser().parseParenExpression(IdVal, EndLoc))
3555       return true;
3556
3557     while (getLexer().getKind() == AsmToken::RParen)
3558       Parser.Lex(); // Eat the ')' token.
3559
3560   } else
3561     return true; // Parenthesis must follow the relocation operand.
3562
3563   Res = evaluateRelocExpr(IdVal, Str);
3564   return false;
3565 }
3566
3567 bool MipsAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
3568                                   SMLoc &EndLoc) {
3569   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Operands;
3570   OperandMatchResultTy ResTy = parseAnyRegister(Operands);
3571   if (ResTy == MatchOperand_Success) {
3572     assert(Operands.size() == 1);
3573     MipsOperand &Operand = static_cast<MipsOperand &>(*Operands.front());
3574     StartLoc = Operand.getStartLoc();
3575     EndLoc = Operand.getEndLoc();
3576
3577     // AFAIK, we only support numeric registers and named GPR's in CFI
3578     // directives.
3579     // Don't worry about eating tokens before failing. Using an unrecognised
3580     // register is a parse error.
3581     if (Operand.isGPRAsmReg()) {
3582       // Resolve to GPR32 or GPR64 appropriately.
3583       RegNo = isGP64bit() ? Operand.getGPR64Reg() : Operand.getGPR32Reg();
3584     }
3585
3586     return (RegNo == (unsigned)-1);
3587   }
3588
3589   assert(Operands.size() == 0);
3590   return (RegNo == (unsigned)-1);
3591 }
3592
3593 bool MipsAsmParser::parseMemOffset(const MCExpr *&Res, bool isParenExpr) {
3594   MCAsmParser &Parser = getParser();
3595   SMLoc S;
3596   bool Result = true;
3597   unsigned NumOfLParen = 0;
3598
3599   while (getLexer().getKind() == AsmToken::LParen) {
3600     Parser.Lex();
3601     ++NumOfLParen;
3602   }
3603
3604   switch (getLexer().getKind()) {
3605   default:
3606     return true;
3607   case AsmToken::Identifier:
3608   case AsmToken::LParen:
3609   case AsmToken::Integer:
3610   case AsmToken::Minus:
3611   case AsmToken::Plus:
3612     if (isParenExpr)
3613       Result = getParser().parseParenExprOfDepth(NumOfLParen, Res, S);
3614     else
3615       Result = (getParser().parseExpression(Res));
3616     while (getLexer().getKind() == AsmToken::RParen)
3617       Parser.Lex();
3618     break;
3619   case AsmToken::Percent:
3620     Result = parseRelocOperand(Res);
3621   }
3622   return Result;
3623 }
3624
3625 MipsAsmParser::OperandMatchResultTy
3626 MipsAsmParser::parseMemOperand(OperandVector &Operands) {
3627   MCAsmParser &Parser = getParser();
3628   DEBUG(dbgs() << "parseMemOperand\n");
3629   const MCExpr *IdVal = nullptr;
3630   SMLoc S;
3631   bool isParenExpr = false;
3632   MipsAsmParser::OperandMatchResultTy Res = MatchOperand_NoMatch;
3633   // First operand is the offset.
3634   S = Parser.getTok().getLoc();
3635
3636   if (getLexer().getKind() == AsmToken::LParen) {
3637     Parser.Lex();
3638     isParenExpr = true;
3639   }
3640
3641   if (getLexer().getKind() != AsmToken::Dollar) {
3642     if (parseMemOffset(IdVal, isParenExpr))
3643       return MatchOperand_ParseFail;
3644
3645     const AsmToken &Tok = Parser.getTok(); // Get the next token.
3646     if (Tok.isNot(AsmToken::LParen)) {
3647       MipsOperand &Mnemonic = static_cast<MipsOperand &>(*Operands[0]);
3648       if (Mnemonic.getToken() == "la" || Mnemonic.getToken() == "dla") {
3649         SMLoc E =
3650             SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3651         Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
3652         return MatchOperand_Success;
3653       }
3654       if (Tok.is(AsmToken::EndOfStatement)) {
3655         SMLoc E =
3656             SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3657
3658         // Zero register assumed, add a memory operand with ZERO as its base.
3659         // "Base" will be managed by k_Memory.
3660         auto Base = MipsOperand::createGPRReg(0, getContext().getRegisterInfo(),
3661                                               S, E, *this);
3662         Operands.push_back(
3663             MipsOperand::CreateMem(std::move(Base), IdVal, S, E, *this));
3664         return MatchOperand_Success;
3665       }
3666       Error(Parser.getTok().getLoc(), "'(' expected");
3667       return MatchOperand_ParseFail;
3668     }
3669
3670     Parser.Lex(); // Eat the '(' token.
3671   }
3672
3673   Res = parseAnyRegister(Operands);
3674   if (Res != MatchOperand_Success)
3675     return Res;
3676
3677   if (Parser.getTok().isNot(AsmToken::RParen)) {
3678     Error(Parser.getTok().getLoc(), "')' expected");
3679     return MatchOperand_ParseFail;
3680   }
3681
3682   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3683
3684   Parser.Lex(); // Eat the ')' token.
3685
3686   if (!IdVal)
3687     IdVal = MCConstantExpr::create(0, getContext());
3688
3689   // Replace the register operand with the memory operand.
3690   std::unique_ptr<MipsOperand> op(
3691       static_cast<MipsOperand *>(Operands.back().release()));
3692   // Remove the register from the operands.
3693   // "op" will be managed by k_Memory.
3694   Operands.pop_back();
3695   // Add the memory operand.
3696   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(IdVal)) {
3697     int64_t Imm;
3698     if (IdVal->evaluateAsAbsolute(Imm))
3699       IdVal = MCConstantExpr::create(Imm, getContext());
3700     else if (BE->getLHS()->getKind() != MCExpr::SymbolRef)
3701       IdVal = MCBinaryExpr::create(BE->getOpcode(), BE->getRHS(), BE->getLHS(),
3702                                    getContext());
3703   }
3704
3705   Operands.push_back(MipsOperand::CreateMem(std::move(op), IdVal, S, E, *this));
3706   return MatchOperand_Success;
3707 }
3708
3709 bool MipsAsmParser::searchSymbolAlias(OperandVector &Operands) {
3710   MCAsmParser &Parser = getParser();
3711   MCSymbol *Sym = getContext().lookupSymbol(Parser.getTok().getIdentifier());
3712   if (Sym) {
3713     SMLoc S = Parser.getTok().getLoc();
3714     const MCExpr *Expr;
3715     if (Sym->isVariable())
3716       Expr = Sym->getVariableValue();
3717     else
3718       return false;
3719     if (Expr->getKind() == MCExpr::SymbolRef) {
3720       const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
3721       StringRef DefSymbol = Ref->getSymbol().getName();
3722       if (DefSymbol.startswith("$")) {
3723         OperandMatchResultTy ResTy =
3724             matchAnyRegisterNameWithoutDollar(Operands, DefSymbol.substr(1), S);
3725         if (ResTy == MatchOperand_Success) {
3726           Parser.Lex();
3727           return true;
3728         } else if (ResTy == MatchOperand_ParseFail)
3729           llvm_unreachable("Should never ParseFail");
3730         return false;
3731       }
3732     } else if (Expr->getKind() == MCExpr::Constant) {
3733       Parser.Lex();
3734       const MCConstantExpr *Const = static_cast<const MCConstantExpr *>(Expr);
3735       Operands.push_back(
3736           MipsOperand::CreateImm(Const, S, Parser.getTok().getLoc(), *this));
3737       return true;
3738     }
3739   }
3740   return false;
3741 }
3742
3743 MipsAsmParser::OperandMatchResultTy
3744 MipsAsmParser::matchAnyRegisterNameWithoutDollar(OperandVector &Operands,
3745                                                  StringRef Identifier,
3746                                                  SMLoc S) {
3747   int Index = matchCPURegisterName(Identifier);
3748   if (Index != -1) {
3749     Operands.push_back(MipsOperand::createGPRReg(
3750         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3751     return MatchOperand_Success;
3752   }
3753
3754   Index = matchHWRegsRegisterName(Identifier);
3755   if (Index != -1) {
3756     Operands.push_back(MipsOperand::createHWRegsReg(
3757         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3758     return MatchOperand_Success;
3759   }
3760
3761   Index = matchFPURegisterName(Identifier);
3762   if (Index != -1) {
3763     Operands.push_back(MipsOperand::createFGRReg(
3764         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3765     return MatchOperand_Success;
3766   }
3767
3768   Index = matchFCCRegisterName(Identifier);
3769   if (Index != -1) {
3770     Operands.push_back(MipsOperand::createFCCReg(
3771         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3772     return MatchOperand_Success;
3773   }
3774
3775   Index = matchACRegisterName(Identifier);
3776   if (Index != -1) {
3777     Operands.push_back(MipsOperand::createACCReg(
3778         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3779     return MatchOperand_Success;
3780   }
3781
3782   Index = matchMSA128RegisterName(Identifier);
3783   if (Index != -1) {
3784     Operands.push_back(MipsOperand::createMSA128Reg(
3785         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3786     return MatchOperand_Success;
3787   }
3788
3789   Index = matchMSA128CtrlRegisterName(Identifier);
3790   if (Index != -1) {
3791     Operands.push_back(MipsOperand::createMSACtrlReg(
3792         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3793     return MatchOperand_Success;
3794   }
3795
3796   return MatchOperand_NoMatch;
3797 }
3798
3799 MipsAsmParser::OperandMatchResultTy
3800 MipsAsmParser::matchAnyRegisterWithoutDollar(OperandVector &Operands, SMLoc S) {
3801   MCAsmParser &Parser = getParser();
3802   auto Token = Parser.getLexer().peekTok(false);
3803
3804   if (Token.is(AsmToken::Identifier)) {
3805     DEBUG(dbgs() << ".. identifier\n");
3806     StringRef Identifier = Token.getIdentifier();
3807     OperandMatchResultTy ResTy =
3808         matchAnyRegisterNameWithoutDollar(Operands, Identifier, S);
3809     return ResTy;
3810   } else if (Token.is(AsmToken::Integer)) {
3811     DEBUG(dbgs() << ".. integer\n");
3812     Operands.push_back(MipsOperand::createNumericReg(
3813         Token.getIntVal(), getContext().getRegisterInfo(), S, Token.getLoc(),
3814         *this));
3815     return MatchOperand_Success;
3816   }
3817
3818   DEBUG(dbgs() << Parser.getTok().getKind() << "\n");
3819
3820   return MatchOperand_NoMatch;
3821 }
3822
3823 MipsAsmParser::OperandMatchResultTy
3824 MipsAsmParser::parseAnyRegister(OperandVector &Operands) {
3825   MCAsmParser &Parser = getParser();
3826   DEBUG(dbgs() << "parseAnyRegister\n");
3827
3828   auto Token = Parser.getTok();
3829
3830   SMLoc S = Token.getLoc();
3831
3832   if (Token.isNot(AsmToken::Dollar)) {
3833     DEBUG(dbgs() << ".. !$ -> try sym aliasing\n");
3834     if (Token.is(AsmToken::Identifier)) {
3835       if (searchSymbolAlias(Operands))
3836         return MatchOperand_Success;
3837     }
3838     DEBUG(dbgs() << ".. !symalias -> NoMatch\n");
3839     return MatchOperand_NoMatch;
3840   }
3841   DEBUG(dbgs() << ".. $\n");
3842
3843   OperandMatchResultTy ResTy = matchAnyRegisterWithoutDollar(Operands, S);
3844   if (ResTy == MatchOperand_Success) {
3845     Parser.Lex(); // $
3846     Parser.Lex(); // identifier
3847   }
3848   return ResTy;
3849 }
3850
3851 MipsAsmParser::OperandMatchResultTy
3852 MipsAsmParser::parseImm(OperandVector &Operands) {
3853   MCAsmParser &Parser = getParser();
3854   switch (getLexer().getKind()) {
3855   default:
3856     return MatchOperand_NoMatch;
3857   case AsmToken::LParen:
3858   case AsmToken::Minus:
3859   case AsmToken::Plus:
3860   case AsmToken::Integer:
3861   case AsmToken::Tilde:
3862   case AsmToken::String:
3863     break;
3864   }
3865
3866   const MCExpr *IdVal;
3867   SMLoc S = Parser.getTok().getLoc();
3868   if (getParser().parseExpression(IdVal))
3869     return MatchOperand_ParseFail;
3870
3871   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3872   Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
3873   return MatchOperand_Success;
3874 }
3875
3876 MipsAsmParser::OperandMatchResultTy
3877 MipsAsmParser::parseJumpTarget(OperandVector &Operands) {
3878   MCAsmParser &Parser = getParser();
3879   DEBUG(dbgs() << "parseJumpTarget\n");
3880
3881   SMLoc S = getLexer().getLoc();
3882
3883   // Integers and expressions are acceptable
3884   OperandMatchResultTy ResTy = parseImm(Operands);
3885   if (ResTy != MatchOperand_NoMatch)
3886     return ResTy;
3887
3888   // Registers are a valid target and have priority over symbols.
3889   ResTy = parseAnyRegister(Operands);
3890   if (ResTy != MatchOperand_NoMatch)
3891     return ResTy;
3892
3893   const MCExpr *Expr = nullptr;
3894   if (Parser.parseExpression(Expr)) {
3895     // We have no way of knowing if a symbol was consumed so we must ParseFail
3896     return MatchOperand_ParseFail;
3897   }
3898   Operands.push_back(
3899       MipsOperand::CreateImm(Expr, S, getLexer().getLoc(), *this));
3900   return MatchOperand_Success;
3901 }
3902
3903 MipsAsmParser::OperandMatchResultTy
3904 MipsAsmParser::parseInvNum(OperandVector &Operands) {
3905   MCAsmParser &Parser = getParser();
3906   const MCExpr *IdVal;
3907   // If the first token is '$' we may have register operand.
3908   if (Parser.getTok().is(AsmToken::Dollar))
3909     return MatchOperand_NoMatch;
3910   SMLoc S = Parser.getTok().getLoc();
3911   if (getParser().parseExpression(IdVal))
3912     return MatchOperand_ParseFail;
3913   const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(IdVal);
3914   assert(MCE && "Unexpected MCExpr type.");
3915   int64_t Val = MCE->getValue();
3916   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3917   Operands.push_back(MipsOperand::CreateImm(
3918       MCConstantExpr::create(0 - Val, getContext()), S, E, *this));
3919   return MatchOperand_Success;
3920 }
3921
3922 MipsAsmParser::OperandMatchResultTy
3923 MipsAsmParser::parseLSAImm(OperandVector &Operands) {
3924   MCAsmParser &Parser = getParser();
3925   switch (getLexer().getKind()) {
3926   default:
3927     return MatchOperand_NoMatch;
3928   case AsmToken::LParen:
3929   case AsmToken::Plus:
3930   case AsmToken::Minus:
3931   case AsmToken::Integer:
3932     break;
3933   }
3934
3935   const MCExpr *Expr;
3936   SMLoc S = Parser.getTok().getLoc();
3937
3938   if (getParser().parseExpression(Expr))
3939     return MatchOperand_ParseFail;
3940
3941   int64_t Val;
3942   if (!Expr->evaluateAsAbsolute(Val)) {
3943     Error(S, "expected immediate value");
3944     return MatchOperand_ParseFail;
3945   }
3946
3947   // The LSA instruction allows a 2-bit unsigned immediate. For this reason
3948   // and because the CPU always adds one to the immediate field, the allowed
3949   // range becomes 1..4. We'll only check the range here and will deal
3950   // with the addition/subtraction when actually decoding/encoding
3951   // the instruction.
3952   if (Val < 1 || Val > 4) {
3953     Error(S, "immediate not in range (1..4)");
3954     return MatchOperand_ParseFail;
3955   }
3956
3957   Operands.push_back(
3958       MipsOperand::CreateImm(Expr, S, Parser.getTok().getLoc(), *this));
3959   return MatchOperand_Success;
3960 }
3961
3962 MipsAsmParser::OperandMatchResultTy
3963 MipsAsmParser::parseRegisterList(OperandVector &Operands) {
3964   MCAsmParser &Parser = getParser();
3965   SmallVector<unsigned, 10> Regs;
3966   unsigned RegNo;
3967   unsigned PrevReg = Mips::NoRegister;
3968   bool RegRange = false;
3969   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> TmpOperands;
3970
3971   if (Parser.getTok().isNot(AsmToken::Dollar))
3972     return MatchOperand_ParseFail;
3973
3974   SMLoc S = Parser.getTok().getLoc();
3975   while (parseAnyRegister(TmpOperands) == MatchOperand_Success) {
3976     SMLoc E = getLexer().getLoc();
3977     MipsOperand &Reg = static_cast<MipsOperand &>(*TmpOperands.back());
3978     RegNo = isGP64bit() ? Reg.getGPR64Reg() : Reg.getGPR32Reg();
3979     if (RegRange) {
3980       // Remove last register operand because registers from register range
3981       // should be inserted first.
3982       if (RegNo == Mips::RA) {
3983         Regs.push_back(RegNo);
3984       } else {
3985         unsigned TmpReg = PrevReg + 1;
3986         while (TmpReg <= RegNo) {
3987           if ((TmpReg < Mips::S0) || (TmpReg > Mips::S7)) {
3988             Error(E, "invalid register operand");
3989             return MatchOperand_ParseFail;
3990           }
3991
3992           PrevReg = TmpReg;
3993           Regs.push_back(TmpReg++);
3994         }
3995       }
3996
3997       RegRange = false;
3998     } else {
3999       if ((PrevReg == Mips::NoRegister) && (RegNo != Mips::S0) &&
4000           (RegNo != Mips::RA)) {
4001         Error(E, "$16 or $31 expected");
4002         return MatchOperand_ParseFail;
4003       } else if (((RegNo < Mips::S0) || (RegNo > Mips::S7)) &&
4004                  (RegNo != Mips::FP) && (RegNo != Mips::RA)) {
4005         Error(E, "invalid register operand");
4006         return MatchOperand_ParseFail;
4007       } else if ((PrevReg != Mips::NoRegister) && (RegNo != PrevReg + 1) &&
4008                  (RegNo != Mips::FP) && (RegNo != Mips::RA)) {
4009         Error(E, "consecutive register numbers expected");
4010         return MatchOperand_ParseFail;
4011       }
4012
4013       Regs.push_back(RegNo);
4014     }
4015
4016     if (Parser.getTok().is(AsmToken::Minus))
4017       RegRange = true;
4018
4019     if (!Parser.getTok().isNot(AsmToken::Minus) &&
4020         !Parser.getTok().isNot(AsmToken::Comma)) {
4021       Error(E, "',' or '-' expected");
4022       return MatchOperand_ParseFail;
4023     }
4024
4025     Lex(); // Consume comma or minus
4026     if (Parser.getTok().isNot(AsmToken::Dollar))
4027       break;
4028
4029     PrevReg = RegNo;
4030   }
4031
4032   SMLoc E = Parser.getTok().getLoc();
4033   Operands.push_back(MipsOperand::CreateRegList(Regs, S, E, *this));
4034   parseMemOperand(Operands);
4035   return MatchOperand_Success;
4036 }
4037
4038 MipsAsmParser::OperandMatchResultTy
4039 MipsAsmParser::parseRegisterPair(OperandVector &Operands) {
4040   MCAsmParser &Parser = getParser();
4041
4042   SMLoc S = Parser.getTok().getLoc();
4043   if (parseAnyRegister(Operands) != MatchOperand_Success)
4044     return MatchOperand_ParseFail;
4045
4046   SMLoc E = Parser.getTok().getLoc();
4047   MipsOperand &Op = static_cast<MipsOperand &>(*Operands.back());
4048   unsigned Reg = Op.getGPR32Reg();
4049   Operands.pop_back();
4050   Operands.push_back(MipsOperand::CreateRegPair(Reg, S, E, *this));
4051   return MatchOperand_Success;
4052 }
4053
4054 MipsAsmParser::OperandMatchResultTy
4055 MipsAsmParser::parseMovePRegPair(OperandVector &Operands) {
4056   MCAsmParser &Parser = getParser();
4057   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> TmpOperands;
4058   SmallVector<unsigned, 10> Regs;
4059
4060   if (Parser.getTok().isNot(AsmToken::Dollar))
4061     return MatchOperand_ParseFail;
4062
4063   SMLoc S = Parser.getTok().getLoc();
4064
4065   if (parseAnyRegister(TmpOperands) != MatchOperand_Success)
4066     return MatchOperand_ParseFail;
4067
4068   MipsOperand *Reg = &static_cast<MipsOperand &>(*TmpOperands.back());
4069   unsigned RegNo = isGP64bit() ? Reg->getGPR64Reg() : Reg->getGPR32Reg();
4070   Regs.push_back(RegNo);
4071
4072   SMLoc E = Parser.getTok().getLoc();
4073   if (Parser.getTok().isNot(AsmToken::Comma)) {
4074     Error(E, "',' expected");
4075     return MatchOperand_ParseFail;
4076   }
4077
4078   // Remove comma.
4079   Parser.Lex();
4080
4081   if (parseAnyRegister(TmpOperands) != MatchOperand_Success)
4082     return MatchOperand_ParseFail;
4083
4084   Reg = &static_cast<MipsOperand &>(*TmpOperands.back());
4085   RegNo = isGP64bit() ? Reg->getGPR64Reg() : Reg->getGPR32Reg();
4086   Regs.push_back(RegNo);
4087
4088   Operands.push_back(MipsOperand::CreateRegList(Regs, S, E, *this));
4089
4090   return MatchOperand_Success;
4091 }
4092
4093 MCSymbolRefExpr::VariantKind MipsAsmParser::getVariantKind(StringRef Symbol) {
4094
4095   MCSymbolRefExpr::VariantKind VK =
4096       StringSwitch<MCSymbolRefExpr::VariantKind>(Symbol)
4097           .Case("hi", MCSymbolRefExpr::VK_Mips_ABS_HI)
4098           .Case("lo", MCSymbolRefExpr::VK_Mips_ABS_LO)
4099           .Case("gp_rel", MCSymbolRefExpr::VK_Mips_GPREL)
4100           .Case("call16", MCSymbolRefExpr::VK_Mips_GOT_CALL)
4101           .Case("got", MCSymbolRefExpr::VK_Mips_GOT)
4102           .Case("tlsgd", MCSymbolRefExpr::VK_Mips_TLSGD)
4103           .Case("tlsldm", MCSymbolRefExpr::VK_Mips_TLSLDM)
4104           .Case("dtprel_hi", MCSymbolRefExpr::VK_Mips_DTPREL_HI)
4105           .Case("dtprel_lo", MCSymbolRefExpr::VK_Mips_DTPREL_LO)
4106           .Case("gottprel", MCSymbolRefExpr::VK_Mips_GOTTPREL)
4107           .Case("tprel_hi", MCSymbolRefExpr::VK_Mips_TPREL_HI)
4108           .Case("tprel_lo", MCSymbolRefExpr::VK_Mips_TPREL_LO)
4109           .Case("got_disp", MCSymbolRefExpr::VK_Mips_GOT_DISP)
4110           .Case("got_page", MCSymbolRefExpr::VK_Mips_GOT_PAGE)
4111           .Case("got_ofst", MCSymbolRefExpr::VK_Mips_GOT_OFST)
4112           .Case("hi(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_HI)
4113           .Case("lo(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_LO)
4114           .Case("got_hi", MCSymbolRefExpr::VK_Mips_GOT_HI16)
4115           .Case("got_lo", MCSymbolRefExpr::VK_Mips_GOT_LO16)
4116           .Case("call_hi", MCSymbolRefExpr::VK_Mips_CALL_HI16)
4117           .Case("call_lo", MCSymbolRefExpr::VK_Mips_CALL_LO16)
4118           .Case("higher", MCSymbolRefExpr::VK_Mips_HIGHER)
4119           .Case("highest", MCSymbolRefExpr::VK_Mips_HIGHEST)
4120           .Case("pcrel_hi", MCSymbolRefExpr::VK_Mips_PCREL_HI16)
4121           .Case("pcrel_lo", MCSymbolRefExpr::VK_Mips_PCREL_LO16)
4122           .Default(MCSymbolRefExpr::VK_None);
4123
4124   assert(VK != MCSymbolRefExpr::VK_None);
4125
4126   return VK;
4127 }
4128
4129 /// Sometimes (i.e. load/stores) the operand may be followed immediately by
4130 /// either this.
4131 /// ::= '(', register, ')'
4132 /// handle it before we iterate so we don't get tripped up by the lack of
4133 /// a comma.
4134 bool MipsAsmParser::parseParenSuffix(StringRef Name, OperandVector &Operands) {
4135   MCAsmParser &Parser = getParser();
4136   if (getLexer().is(AsmToken::LParen)) {
4137     Operands.push_back(
4138         MipsOperand::CreateToken("(", getLexer().getLoc(), *this));
4139     Parser.Lex();
4140     if (parseOperand(Operands, Name)) {
4141       SMLoc Loc = getLexer().getLoc();
4142       Parser.eatToEndOfStatement();
4143       return Error(Loc, "unexpected token in argument list");
4144     }
4145     if (Parser.getTok().isNot(AsmToken::RParen)) {
4146       SMLoc Loc = getLexer().getLoc();
4147       Parser.eatToEndOfStatement();
4148       return Error(Loc, "unexpected token, expected ')'");
4149     }
4150     Operands.push_back(
4151         MipsOperand::CreateToken(")", getLexer().getLoc(), *this));
4152     Parser.Lex();
4153   }
4154   return false;
4155 }
4156
4157 /// Sometimes (i.e. in MSA) the operand may be followed immediately by
4158 /// either one of these.
4159 /// ::= '[', register, ']'
4160 /// ::= '[', integer, ']'
4161 /// handle it before we iterate so we don't get tripped up by the lack of
4162 /// a comma.
4163 bool MipsAsmParser::parseBracketSuffix(StringRef Name,
4164                                        OperandVector &Operands) {
4165   MCAsmParser &Parser = getParser();
4166   if (getLexer().is(AsmToken::LBrac)) {
4167     Operands.push_back(
4168         MipsOperand::CreateToken("[", getLexer().getLoc(), *this));
4169     Parser.Lex();
4170     if (parseOperand(Operands, Name)) {
4171       SMLoc Loc = getLexer().getLoc();
4172       Parser.eatToEndOfStatement();
4173       return Error(Loc, "unexpected token in argument list");
4174     }
4175     if (Parser.getTok().isNot(AsmToken::RBrac)) {
4176       SMLoc Loc = getLexer().getLoc();
4177       Parser.eatToEndOfStatement();
4178       return Error(Loc, "unexpected token, expected ']'");
4179     }
4180     Operands.push_back(
4181         MipsOperand::CreateToken("]", getLexer().getLoc(), *this));
4182     Parser.Lex();
4183   }
4184   return false;
4185 }
4186
4187 bool MipsAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
4188                                      SMLoc NameLoc, OperandVector &Operands) {
4189   MCAsmParser &Parser = getParser();
4190   DEBUG(dbgs() << "ParseInstruction\n");
4191
4192   // We have reached first instruction, module directive are now forbidden.
4193   getTargetStreamer().forbidModuleDirective();
4194
4195   // Check if we have valid mnemonic
4196   if (!mnemonicIsValid(Name, 0)) {
4197     Parser.eatToEndOfStatement();
4198     return Error(NameLoc, "unknown instruction");
4199   }
4200   // First operand in MCInst is instruction mnemonic.
4201   Operands.push_back(MipsOperand::CreateToken(Name, NameLoc, *this));
4202
4203   // Read the remaining operands.
4204   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4205     // Read the first operand.
4206     if (parseOperand(Operands, Name)) {
4207       SMLoc Loc = getLexer().getLoc();
4208       Parser.eatToEndOfStatement();
4209       return Error(Loc, "unexpected token in argument list");
4210     }
4211     if (getLexer().is(AsmToken::LBrac) && parseBracketSuffix(Name, Operands))
4212       return true;
4213     // AFAIK, parenthesis suffixes are never on the first operand
4214
4215     while (getLexer().is(AsmToken::Comma)) {
4216       Parser.Lex(); // Eat the comma.
4217       // Parse and remember the operand.
4218       if (parseOperand(Operands, Name)) {
4219         SMLoc Loc = getLexer().getLoc();
4220         Parser.eatToEndOfStatement();
4221         return Error(Loc, "unexpected token in argument list");
4222       }
4223       // Parse bracket and parenthesis suffixes before we iterate
4224       if (getLexer().is(AsmToken::LBrac)) {
4225         if (parseBracketSuffix(Name, Operands))
4226           return true;
4227       } else if (getLexer().is(AsmToken::LParen) &&
4228                  parseParenSuffix(Name, Operands))
4229         return true;
4230     }
4231   }
4232   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4233     SMLoc Loc = getLexer().getLoc();
4234     Parser.eatToEndOfStatement();
4235     return Error(Loc, "unexpected token in argument list");
4236   }
4237   Parser.Lex(); // Consume the EndOfStatement.
4238   return false;
4239 }
4240
4241 bool MipsAsmParser::reportParseError(Twine ErrorMsg) {
4242   MCAsmParser &Parser = getParser();
4243   SMLoc Loc = getLexer().getLoc();
4244   Parser.eatToEndOfStatement();
4245   return Error(Loc, ErrorMsg);
4246 }
4247
4248 bool MipsAsmParser::reportParseError(SMLoc Loc, Twine ErrorMsg) {
4249   return Error(Loc, ErrorMsg);
4250 }
4251
4252 bool MipsAsmParser::parseSetNoAtDirective() {
4253   MCAsmParser &Parser = getParser();
4254   // Line should look like: ".set noat".
4255
4256   // Set the $at register to $0.
4257   AssemblerOptions.back()->setATRegIndex(0);
4258
4259   Parser.Lex(); // Eat "noat".
4260
4261   // If this is not the end of the statement, report an error.
4262   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4263     reportParseError("unexpected token, expected end of statement");
4264     return false;
4265   }
4266
4267   getTargetStreamer().emitDirectiveSetNoAt();
4268   Parser.Lex(); // Consume the EndOfStatement.
4269   return false;
4270 }
4271
4272 bool MipsAsmParser::parseSetAtDirective() {
4273   // Line can be: ".set at", which sets $at to $1
4274   //          or  ".set at=$reg", which sets $at to $reg.
4275   MCAsmParser &Parser = getParser();
4276   Parser.Lex(); // Eat "at".
4277
4278   if (getLexer().is(AsmToken::EndOfStatement)) {
4279     // No register was specified, so we set $at to $1.
4280     AssemblerOptions.back()->setATRegIndex(1);
4281
4282     getTargetStreamer().emitDirectiveSetAt();
4283     Parser.Lex(); // Consume the EndOfStatement.
4284     return false;
4285   }
4286
4287   if (getLexer().isNot(AsmToken::Equal)) {
4288     reportParseError("unexpected token, expected equals sign");
4289     return false;
4290   }
4291   Parser.Lex(); // Eat "=".
4292
4293   if (getLexer().isNot(AsmToken::Dollar)) {
4294     if (getLexer().is(AsmToken::EndOfStatement)) {
4295       reportParseError("no register specified");
4296       return false;
4297     } else {
4298       reportParseError("unexpected token, expected dollar sign '$'");
4299       return false;
4300     }
4301   }
4302   Parser.Lex(); // Eat "$".
4303
4304   // Find out what "reg" is.
4305   unsigned AtRegNo;
4306   const AsmToken &Reg = Parser.getTok();
4307   if (Reg.is(AsmToken::Identifier)) {
4308     AtRegNo = matchCPURegisterName(Reg.getIdentifier());
4309   } else if (Reg.is(AsmToken::Integer)) {
4310     AtRegNo = Reg.getIntVal();
4311   } else {
4312     reportParseError("unexpected token, expected identifier or integer");
4313     return false;
4314   }
4315
4316   // Check if $reg is a valid register. If it is, set $at to $reg.
4317   if (!AssemblerOptions.back()->setATRegIndex(AtRegNo)) {
4318     reportParseError("invalid register");
4319     return false;
4320   }
4321   Parser.Lex(); // Eat "reg".
4322
4323   // If this is not the end of the statement, report an error.
4324   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4325     reportParseError("unexpected token, expected end of statement");
4326     return false;
4327   }
4328
4329   getTargetStreamer().emitDirectiveSetAtWithArg(AtRegNo);
4330
4331   Parser.Lex(); // Consume the EndOfStatement.
4332   return false;
4333 }
4334
4335 bool MipsAsmParser::parseSetReorderDirective() {
4336   MCAsmParser &Parser = getParser();
4337   Parser.Lex();
4338   // If this is not the end of the statement, report an error.
4339   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4340     reportParseError("unexpected token, expected end of statement");
4341     return false;
4342   }
4343   AssemblerOptions.back()->setReorder();
4344   getTargetStreamer().emitDirectiveSetReorder();
4345   Parser.Lex(); // Consume the EndOfStatement.
4346   return false;
4347 }
4348
4349 bool MipsAsmParser::parseSetNoReorderDirective() {
4350   MCAsmParser &Parser = getParser();
4351   Parser.Lex();
4352   // If this is not the end of the statement, report an error.
4353   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4354     reportParseError("unexpected token, expected end of statement");
4355     return false;
4356   }
4357   AssemblerOptions.back()->setNoReorder();
4358   getTargetStreamer().emitDirectiveSetNoReorder();
4359   Parser.Lex(); // Consume the EndOfStatement.
4360   return false;
4361 }
4362
4363 bool MipsAsmParser::parseSetMacroDirective() {
4364   MCAsmParser &Parser = getParser();
4365   Parser.Lex();
4366   // If this is not the end of the statement, report an error.
4367   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4368     reportParseError("unexpected token, expected end of statement");
4369     return false;
4370   }
4371   AssemblerOptions.back()->setMacro();
4372   getTargetStreamer().emitDirectiveSetMacro();
4373   Parser.Lex(); // Consume the EndOfStatement.
4374   return false;
4375 }
4376
4377 bool MipsAsmParser::parseSetNoMacroDirective() {
4378   MCAsmParser &Parser = getParser();
4379   Parser.Lex();
4380   // If this is not the end of the statement, report an error.
4381   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4382     reportParseError("unexpected token, expected end of statement");
4383     return false;
4384   }
4385   if (AssemblerOptions.back()->isReorder()) {
4386     reportParseError("`noreorder' must be set before `nomacro'");
4387     return false;
4388   }
4389   AssemblerOptions.back()->setNoMacro();
4390   getTargetStreamer().emitDirectiveSetNoMacro();
4391   Parser.Lex(); // Consume the EndOfStatement.
4392   return false;
4393 }
4394
4395 bool MipsAsmParser::parseSetMsaDirective() {
4396   MCAsmParser &Parser = getParser();
4397   Parser.Lex();
4398
4399   // If this is not the end of the statement, report an error.
4400   if (getLexer().isNot(AsmToken::EndOfStatement))
4401     return reportParseError("unexpected token, expected end of statement");
4402
4403   setFeatureBits(Mips::FeatureMSA, "msa");
4404   getTargetStreamer().emitDirectiveSetMsa();
4405   return false;
4406 }
4407
4408 bool MipsAsmParser::parseSetNoMsaDirective() {
4409   MCAsmParser &Parser = getParser();
4410   Parser.Lex();
4411
4412   // If this is not the end of the statement, report an error.
4413   if (getLexer().isNot(AsmToken::EndOfStatement))
4414     return reportParseError("unexpected token, expected end of statement");
4415
4416   clearFeatureBits(Mips::FeatureMSA, "msa");
4417   getTargetStreamer().emitDirectiveSetNoMsa();
4418   return false;
4419 }
4420
4421 bool MipsAsmParser::parseSetNoDspDirective() {
4422   MCAsmParser &Parser = getParser();
4423   Parser.Lex(); // Eat "nodsp".
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::FeatureDSP, "dsp");
4432   getTargetStreamer().emitDirectiveSetNoDsp();
4433   return false;
4434 }
4435
4436 bool MipsAsmParser::parseSetMips16Directive() {
4437   MCAsmParser &Parser = getParser();
4438   Parser.Lex(); // Eat "mips16".
4439
4440   // If this is not the end of the statement, report an error.
4441   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4442     reportParseError("unexpected token, expected end of statement");
4443     return false;
4444   }
4445
4446   setFeatureBits(Mips::FeatureMips16, "mips16");
4447   getTargetStreamer().emitDirectiveSetMips16();
4448   Parser.Lex(); // Consume the EndOfStatement.
4449   return false;
4450 }
4451
4452 bool MipsAsmParser::parseSetNoMips16Directive() {
4453   MCAsmParser &Parser = getParser();
4454   Parser.Lex(); // Eat "nomips16".
4455
4456   // If this is not the end of the statement, report an error.
4457   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4458     reportParseError("unexpected token, expected end of statement");
4459     return false;
4460   }
4461
4462   clearFeatureBits(Mips::FeatureMips16, "mips16");
4463   getTargetStreamer().emitDirectiveSetNoMips16();
4464   Parser.Lex(); // Consume the EndOfStatement.
4465   return false;
4466 }
4467
4468 bool MipsAsmParser::parseSetFpDirective() {
4469   MCAsmParser &Parser = getParser();
4470   MipsABIFlagsSection::FpABIKind FpAbiVal;
4471   // Line can be: .set fp=32
4472   //              .set fp=xx
4473   //              .set fp=64
4474   Parser.Lex(); // Eat fp token
4475   AsmToken Tok = Parser.getTok();
4476   if (Tok.isNot(AsmToken::Equal)) {
4477     reportParseError("unexpected token, expected equals sign '='");
4478     return false;
4479   }
4480   Parser.Lex(); // Eat '=' token.
4481   Tok = Parser.getTok();
4482
4483   if (!parseFpABIValue(FpAbiVal, ".set"))
4484     return false;
4485
4486   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4487     reportParseError("unexpected token, expected end of statement");
4488     return false;
4489   }
4490   getTargetStreamer().emitDirectiveSetFp(FpAbiVal);
4491   Parser.Lex(); // Consume the EndOfStatement.
4492   return false;
4493 }
4494
4495 bool MipsAsmParser::parseSetOddSPRegDirective() {
4496   MCAsmParser &Parser = getParser();
4497
4498   Parser.Lex(); // Eat "oddspreg".
4499   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4500     reportParseError("unexpected token, expected end of statement");
4501     return false;
4502   }
4503
4504   clearFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
4505   getTargetStreamer().emitDirectiveSetOddSPReg();
4506   return false;
4507 }
4508
4509 bool MipsAsmParser::parseSetNoOddSPRegDirective() {
4510   MCAsmParser &Parser = getParser();
4511
4512   Parser.Lex(); // Eat "nooddspreg".
4513   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4514     reportParseError("unexpected token, expected end of statement");
4515     return false;
4516   }
4517
4518   setFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
4519   getTargetStreamer().emitDirectiveSetNoOddSPReg();
4520   return false;
4521 }
4522
4523 bool MipsAsmParser::parseSetPopDirective() {
4524   MCAsmParser &Parser = getParser();
4525   SMLoc Loc = getLexer().getLoc();
4526
4527   Parser.Lex();
4528   if (getLexer().isNot(AsmToken::EndOfStatement))
4529     return reportParseError("unexpected token, expected end of statement");
4530
4531   // Always keep an element on the options "stack" to prevent the user
4532   // from changing the initial options. This is how we remember them.
4533   if (AssemblerOptions.size() == 2)
4534     return reportParseError(Loc, ".set pop with no .set push");
4535
4536   AssemblerOptions.pop_back();
4537   setAvailableFeatures(
4538       ComputeAvailableFeatures(AssemblerOptions.back()->getFeatures()));
4539   STI.setFeatureBits(AssemblerOptions.back()->getFeatures());
4540
4541   getTargetStreamer().emitDirectiveSetPop();
4542   return false;
4543 }
4544
4545 bool MipsAsmParser::parseSetPushDirective() {
4546   MCAsmParser &Parser = getParser();
4547   Parser.Lex();
4548   if (getLexer().isNot(AsmToken::EndOfStatement))
4549     return reportParseError("unexpected token, expected end of statement");
4550
4551   // Create a copy of the current assembler options environment and push it.
4552   AssemblerOptions.push_back(
4553               make_unique<MipsAssemblerOptions>(AssemblerOptions.back().get()));
4554
4555   getTargetStreamer().emitDirectiveSetPush();
4556   return false;
4557 }
4558
4559 bool MipsAsmParser::parseSetSoftFloatDirective() {
4560   MCAsmParser &Parser = getParser();
4561   Parser.Lex();
4562   if (getLexer().isNot(AsmToken::EndOfStatement))
4563     return reportParseError("unexpected token, expected end of statement");
4564
4565   setFeatureBits(Mips::FeatureSoftFloat, "soft-float");
4566   getTargetStreamer().emitDirectiveSetSoftFloat();
4567   return false;
4568 }
4569
4570 bool MipsAsmParser::parseSetHardFloatDirective() {
4571   MCAsmParser &Parser = getParser();
4572   Parser.Lex();
4573   if (getLexer().isNot(AsmToken::EndOfStatement))
4574     return reportParseError("unexpected token, expected end of statement");
4575
4576   clearFeatureBits(Mips::FeatureSoftFloat, "soft-float");
4577   getTargetStreamer().emitDirectiveSetHardFloat();
4578   return false;
4579 }
4580
4581 bool MipsAsmParser::parseSetAssignment() {
4582   StringRef Name;
4583   const MCExpr *Value;
4584   MCAsmParser &Parser = getParser();
4585
4586   if (Parser.parseIdentifier(Name))
4587     reportParseError("expected identifier after .set");
4588
4589   if (getLexer().isNot(AsmToken::Comma))
4590     return reportParseError("unexpected token, expected comma");
4591   Lex(); // Eat comma
4592
4593   if (Parser.parseExpression(Value))
4594     return reportParseError("expected valid expression after comma");
4595
4596   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
4597   Sym->setVariableValue(Value);
4598
4599   return false;
4600 }
4601
4602 bool MipsAsmParser::parseSetMips0Directive() {
4603   MCAsmParser &Parser = getParser();
4604   Parser.Lex();
4605   if (getLexer().isNot(AsmToken::EndOfStatement))
4606     return reportParseError("unexpected token, expected end of statement");
4607
4608   // Reset assembler options to their initial values.
4609   setAvailableFeatures(
4610       ComputeAvailableFeatures(AssemblerOptions.front()->getFeatures()));
4611   STI.setFeatureBits(AssemblerOptions.front()->getFeatures());
4612   AssemblerOptions.back()->setFeatures(AssemblerOptions.front()->getFeatures());
4613
4614   getTargetStreamer().emitDirectiveSetMips0();
4615   return false;
4616 }
4617
4618 bool MipsAsmParser::parseSetArchDirective() {
4619   MCAsmParser &Parser = getParser();
4620   Parser.Lex();
4621   if (getLexer().isNot(AsmToken::Equal))
4622     return reportParseError("unexpected token, expected equals sign");
4623
4624   Parser.Lex();
4625   StringRef Arch;
4626   if (Parser.parseIdentifier(Arch))
4627     return reportParseError("expected arch identifier");
4628
4629   StringRef ArchFeatureName =
4630       StringSwitch<StringRef>(Arch)
4631           .Case("mips1", "mips1")
4632           .Case("mips2", "mips2")
4633           .Case("mips3", "mips3")
4634           .Case("mips4", "mips4")
4635           .Case("mips5", "mips5")
4636           .Case("mips32", "mips32")
4637           .Case("mips32r2", "mips32r2")
4638           .Case("mips32r3", "mips32r3")
4639           .Case("mips32r5", "mips32r5")
4640           .Case("mips32r6", "mips32r6")
4641           .Case("mips64", "mips64")
4642           .Case("mips64r2", "mips64r2")
4643           .Case("mips64r3", "mips64r3")
4644           .Case("mips64r5", "mips64r5")
4645           .Case("mips64r6", "mips64r6")
4646           .Case("cnmips", "cnmips")
4647           .Case("r4000", "mips3") // This is an implementation of Mips3.
4648           .Default("");
4649
4650   if (ArchFeatureName.empty())
4651     return reportParseError("unsupported architecture");
4652
4653   selectArch(ArchFeatureName);
4654   getTargetStreamer().emitDirectiveSetArch(Arch);
4655   return false;
4656 }
4657
4658 bool MipsAsmParser::parseSetFeature(uint64_t Feature) {
4659   MCAsmParser &Parser = getParser();
4660   Parser.Lex();
4661   if (getLexer().isNot(AsmToken::EndOfStatement))
4662     return reportParseError("unexpected token, expected end of statement");
4663
4664   switch (Feature) {
4665   default:
4666     llvm_unreachable("Unimplemented feature");
4667   case Mips::FeatureDSP:
4668     setFeatureBits(Mips::FeatureDSP, "dsp");
4669     getTargetStreamer().emitDirectiveSetDsp();
4670     break;
4671   case Mips::FeatureMicroMips:
4672     getTargetStreamer().emitDirectiveSetMicroMips();
4673     break;
4674   case Mips::FeatureMips1:
4675     selectArch("mips1");
4676     getTargetStreamer().emitDirectiveSetMips1();
4677     break;
4678   case Mips::FeatureMips2:
4679     selectArch("mips2");
4680     getTargetStreamer().emitDirectiveSetMips2();
4681     break;
4682   case Mips::FeatureMips3:
4683     selectArch("mips3");
4684     getTargetStreamer().emitDirectiveSetMips3();
4685     break;
4686   case Mips::FeatureMips4:
4687     selectArch("mips4");
4688     getTargetStreamer().emitDirectiveSetMips4();
4689     break;
4690   case Mips::FeatureMips5:
4691     selectArch("mips5");
4692     getTargetStreamer().emitDirectiveSetMips5();
4693     break;
4694   case Mips::FeatureMips32:
4695     selectArch("mips32");
4696     getTargetStreamer().emitDirectiveSetMips32();
4697     break;
4698   case Mips::FeatureMips32r2:
4699     selectArch("mips32r2");
4700     getTargetStreamer().emitDirectiveSetMips32R2();
4701     break;
4702   case Mips::FeatureMips32r3:
4703     selectArch("mips32r3");
4704     getTargetStreamer().emitDirectiveSetMips32R3();
4705     break;
4706   case Mips::FeatureMips32r5:
4707     selectArch("mips32r5");
4708     getTargetStreamer().emitDirectiveSetMips32R5();
4709     break;
4710   case Mips::FeatureMips32r6:
4711     selectArch("mips32r6");
4712     getTargetStreamer().emitDirectiveSetMips32R6();
4713     break;
4714   case Mips::FeatureMips64:
4715     selectArch("mips64");
4716     getTargetStreamer().emitDirectiveSetMips64();
4717     break;
4718   case Mips::FeatureMips64r2:
4719     selectArch("mips64r2");
4720     getTargetStreamer().emitDirectiveSetMips64R2();
4721     break;
4722   case Mips::FeatureMips64r3:
4723     selectArch("mips64r3");
4724     getTargetStreamer().emitDirectiveSetMips64R3();
4725     break;
4726   case Mips::FeatureMips64r5:
4727     selectArch("mips64r5");
4728     getTargetStreamer().emitDirectiveSetMips64R5();
4729     break;
4730   case Mips::FeatureMips64r6:
4731     selectArch("mips64r6");
4732     getTargetStreamer().emitDirectiveSetMips64R6();
4733     break;
4734   }
4735   return false;
4736 }
4737
4738 bool MipsAsmParser::eatComma(StringRef ErrorStr) {
4739   MCAsmParser &Parser = getParser();
4740   if (getLexer().isNot(AsmToken::Comma)) {
4741     SMLoc Loc = getLexer().getLoc();
4742     Parser.eatToEndOfStatement();
4743     return Error(Loc, ErrorStr);
4744   }
4745
4746   Parser.Lex(); // Eat the comma.
4747   return true;
4748 }
4749
4750 bool MipsAsmParser::parseDirectiveCpLoad(SMLoc Loc) {
4751   if (AssemblerOptions.back()->isReorder())
4752     Warning(Loc, ".cpload should be inside a noreorder section");
4753
4754   if (inMips16Mode()) {
4755     reportParseError(".cpload is not supported in Mips16 mode");
4756     return false;
4757   }
4758
4759   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Reg;
4760   OperandMatchResultTy ResTy = parseAnyRegister(Reg);
4761   if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
4762     reportParseError("expected register containing function address");
4763     return false;
4764   }
4765
4766   MipsOperand &RegOpnd = static_cast<MipsOperand &>(*Reg[0]);
4767   if (!RegOpnd.isGPRAsmReg()) {
4768     reportParseError(RegOpnd.getStartLoc(), "invalid register");
4769     return false;
4770   }
4771
4772   // If this is not the end of the statement, report an error.
4773   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4774     reportParseError("unexpected token, expected end of statement");
4775     return false;
4776   }
4777
4778   getTargetStreamer().emitDirectiveCpLoad(RegOpnd.getGPR32Reg());
4779   return false;
4780 }
4781
4782 bool MipsAsmParser::parseDirectiveCPSetup() {
4783   MCAsmParser &Parser = getParser();
4784   unsigned FuncReg;
4785   unsigned Save;
4786   bool SaveIsReg = true;
4787
4788   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> TmpReg;
4789   OperandMatchResultTy ResTy = parseAnyRegister(TmpReg);
4790   if (ResTy == MatchOperand_NoMatch) {
4791     reportParseError("expected register containing function address");
4792     Parser.eatToEndOfStatement();
4793     return false;
4794   }
4795
4796   MipsOperand &FuncRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
4797   if (!FuncRegOpnd.isGPRAsmReg()) {
4798     reportParseError(FuncRegOpnd.getStartLoc(), "invalid register");
4799     Parser.eatToEndOfStatement();
4800     return false;
4801   }
4802
4803   FuncReg = FuncRegOpnd.getGPR32Reg();
4804   TmpReg.clear();
4805
4806   if (!eatComma("unexpected token, expected comma"))
4807     return true;
4808
4809   ResTy = parseAnyRegister(TmpReg);
4810   if (ResTy == MatchOperand_NoMatch) {
4811     const AsmToken &Tok = Parser.getTok();
4812     if (Tok.is(AsmToken::Integer)) {
4813       Save = Tok.getIntVal();
4814       SaveIsReg = false;
4815       Parser.Lex();
4816     } else {
4817       reportParseError("expected save register or stack offset");
4818       Parser.eatToEndOfStatement();
4819       return false;
4820     }
4821   } else {
4822     MipsOperand &SaveOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
4823     if (!SaveOpnd.isGPRAsmReg()) {
4824       reportParseError(SaveOpnd.getStartLoc(), "invalid register");
4825       Parser.eatToEndOfStatement();
4826       return false;
4827     }
4828     Save = SaveOpnd.getGPR32Reg();
4829   }
4830
4831   if (!eatComma("unexpected token, expected comma"))
4832     return true;
4833
4834   const MCExpr *Expr;
4835   if (Parser.parseExpression(Expr)) {
4836     reportParseError("expected expression");
4837     return false;
4838   }
4839
4840   if (Expr->getKind() != MCExpr::SymbolRef) {
4841     reportParseError("expected symbol");
4842     return false;
4843   }
4844   const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
4845
4846   getTargetStreamer().emitDirectiveCpsetup(FuncReg, Save, Ref->getSymbol(),
4847                                            SaveIsReg);
4848   return false;
4849 }
4850
4851 bool MipsAsmParser::parseDirectiveNaN() {
4852   MCAsmParser &Parser = getParser();
4853   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4854     const AsmToken &Tok = Parser.getTok();
4855
4856     if (Tok.getString() == "2008") {
4857       Parser.Lex();
4858       getTargetStreamer().emitDirectiveNaN2008();
4859       return false;
4860     } else if (Tok.getString() == "legacy") {
4861       Parser.Lex();
4862       getTargetStreamer().emitDirectiveNaNLegacy();
4863       return false;
4864     }
4865   }
4866   // If we don't recognize the option passed to the .nan
4867   // directive (e.g. no option or unknown option), emit an error.
4868   reportParseError("invalid option in .nan directive");
4869   return false;
4870 }
4871
4872 bool MipsAsmParser::parseDirectiveSet() {
4873   MCAsmParser &Parser = getParser();
4874   // Get the next token.
4875   const AsmToken &Tok = Parser.getTok();
4876
4877   if (Tok.getString() == "noat") {
4878     return parseSetNoAtDirective();
4879   } else if (Tok.getString() == "at") {
4880     return parseSetAtDirective();
4881   } else if (Tok.getString() == "arch") {
4882     return parseSetArchDirective();
4883   } else if (Tok.getString() == "fp") {
4884     return parseSetFpDirective();
4885   } else if (Tok.getString() == "oddspreg") {
4886     return parseSetOddSPRegDirective();
4887   } else if (Tok.getString() == "nooddspreg") {
4888     return parseSetNoOddSPRegDirective();
4889   } else if (Tok.getString() == "pop") {
4890     return parseSetPopDirective();
4891   } else if (Tok.getString() == "push") {
4892     return parseSetPushDirective();
4893   } else if (Tok.getString() == "reorder") {
4894     return parseSetReorderDirective();
4895   } else if (Tok.getString() == "noreorder") {
4896     return parseSetNoReorderDirective();
4897   } else if (Tok.getString() == "macro") {
4898     return parseSetMacroDirective();
4899   } else if (Tok.getString() == "nomacro") {
4900     return parseSetNoMacroDirective();
4901   } else if (Tok.getString() == "mips16") {
4902     return parseSetMips16Directive();
4903   } else if (Tok.getString() == "nomips16") {
4904     return parseSetNoMips16Directive();
4905   } else if (Tok.getString() == "nomicromips") {
4906     getTargetStreamer().emitDirectiveSetNoMicroMips();
4907     Parser.eatToEndOfStatement();
4908     return false;
4909   } else if (Tok.getString() == "micromips") {
4910     return parseSetFeature(Mips::FeatureMicroMips);
4911   } else if (Tok.getString() == "mips0") {
4912     return parseSetMips0Directive();
4913   } else if (Tok.getString() == "mips1") {
4914     return parseSetFeature(Mips::FeatureMips1);
4915   } else if (Tok.getString() == "mips2") {
4916     return parseSetFeature(Mips::FeatureMips2);
4917   } else if (Tok.getString() == "mips3") {
4918     return parseSetFeature(Mips::FeatureMips3);
4919   } else if (Tok.getString() == "mips4") {
4920     return parseSetFeature(Mips::FeatureMips4);
4921   } else if (Tok.getString() == "mips5") {
4922     return parseSetFeature(Mips::FeatureMips5);
4923   } else if (Tok.getString() == "mips32") {
4924     return parseSetFeature(Mips::FeatureMips32);
4925   } else if (Tok.getString() == "mips32r2") {
4926     return parseSetFeature(Mips::FeatureMips32r2);
4927   } else if (Tok.getString() == "mips32r3") {
4928     return parseSetFeature(Mips::FeatureMips32r3);
4929   } else if (Tok.getString() == "mips32r5") {
4930     return parseSetFeature(Mips::FeatureMips32r5);
4931   } else if (Tok.getString() == "mips32r6") {
4932     return parseSetFeature(Mips::FeatureMips32r6);
4933   } else if (Tok.getString() == "mips64") {
4934     return parseSetFeature(Mips::FeatureMips64);
4935   } else if (Tok.getString() == "mips64r2") {
4936     return parseSetFeature(Mips::FeatureMips64r2);
4937   } else if (Tok.getString() == "mips64r3") {
4938     return parseSetFeature(Mips::FeatureMips64r3);
4939   } else if (Tok.getString() == "mips64r5") {
4940     return parseSetFeature(Mips::FeatureMips64r5);
4941   } else if (Tok.getString() == "mips64r6") {
4942     return parseSetFeature(Mips::FeatureMips64r6);
4943   } else if (Tok.getString() == "dsp") {
4944     return parseSetFeature(Mips::FeatureDSP);
4945   } else if (Tok.getString() == "nodsp") {
4946     return parseSetNoDspDirective();
4947   } else if (Tok.getString() == "msa") {
4948     return parseSetMsaDirective();
4949   } else if (Tok.getString() == "nomsa") {
4950     return parseSetNoMsaDirective();
4951   } else if (Tok.getString() == "softfloat") {
4952     return parseSetSoftFloatDirective();
4953   } else if (Tok.getString() == "hardfloat") {
4954     return parseSetHardFloatDirective();
4955   } else {
4956     // It is just an identifier, look for an assignment.
4957     parseSetAssignment();
4958     return false;
4959   }
4960
4961   return true;
4962 }
4963
4964 /// parseDataDirective
4965 ///  ::= .word [ expression (, expression)* ]
4966 bool MipsAsmParser::parseDataDirective(unsigned Size, SMLoc L) {
4967   MCAsmParser &Parser = getParser();
4968   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4969     for (;;) {
4970       const MCExpr *Value;
4971       if (getParser().parseExpression(Value))
4972         return true;
4973
4974       getParser().getStreamer().EmitValue(Value, Size);
4975
4976       if (getLexer().is(AsmToken::EndOfStatement))
4977         break;
4978
4979       if (getLexer().isNot(AsmToken::Comma))
4980         return Error(L, "unexpected token, expected comma");
4981       Parser.Lex();
4982     }
4983   }
4984
4985   Parser.Lex();
4986   return false;
4987 }
4988
4989 /// parseDirectiveGpWord
4990 ///  ::= .gpword local_sym
4991 bool MipsAsmParser::parseDirectiveGpWord() {
4992   MCAsmParser &Parser = getParser();
4993   const MCExpr *Value;
4994   // EmitGPRel32Value requires an expression, so we are using base class
4995   // method to evaluate the expression.
4996   if (getParser().parseExpression(Value))
4997     return true;
4998   getParser().getStreamer().EmitGPRel32Value(Value);
4999
5000   if (getLexer().isNot(AsmToken::EndOfStatement))
5001     return Error(getLexer().getLoc(), 
5002                 "unexpected token, expected end of statement");
5003   Parser.Lex(); // Eat EndOfStatement token.
5004   return false;
5005 }
5006
5007 /// parseDirectiveGpDWord
5008 ///  ::= .gpdword local_sym
5009 bool MipsAsmParser::parseDirectiveGpDWord() {
5010   MCAsmParser &Parser = getParser();
5011   const MCExpr *Value;
5012   // EmitGPRel64Value requires an expression, so we are using base class
5013   // method to evaluate the expression.
5014   if (getParser().parseExpression(Value))
5015     return true;
5016   getParser().getStreamer().EmitGPRel64Value(Value);
5017
5018   if (getLexer().isNot(AsmToken::EndOfStatement))
5019     return Error(getLexer().getLoc(), 
5020                 "unexpected token, expected end of statement");
5021   Parser.Lex(); // Eat EndOfStatement token.
5022   return false;
5023 }
5024
5025 bool MipsAsmParser::parseDirectiveOption() {
5026   MCAsmParser &Parser = getParser();
5027   // Get the option token.
5028   AsmToken Tok = Parser.getTok();
5029   // At the moment only identifiers are supported.
5030   if (Tok.isNot(AsmToken::Identifier)) {
5031     Error(Parser.getTok().getLoc(), "unexpected token, expected identifier");
5032     Parser.eatToEndOfStatement();
5033     return false;
5034   }
5035
5036   StringRef Option = Tok.getIdentifier();
5037
5038   if (Option == "pic0") {
5039     // MipsAsmParser needs to know if the current PIC mode changes.
5040     IsPicEnabled = false;
5041
5042     getTargetStreamer().emitDirectiveOptionPic0();
5043     Parser.Lex();
5044     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
5045       Error(Parser.getTok().getLoc(),
5046             "unexpected token, expected end of statement");
5047       Parser.eatToEndOfStatement();
5048     }
5049     return false;
5050   }
5051
5052   if (Option == "pic2") {
5053     // MipsAsmParser needs to know if the current PIC mode changes.
5054     IsPicEnabled = true;
5055
5056     getTargetStreamer().emitDirectiveOptionPic2();
5057     Parser.Lex();
5058     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
5059       Error(Parser.getTok().getLoc(),
5060             "unexpected token, expected end of statement");
5061       Parser.eatToEndOfStatement();
5062     }
5063     return false;
5064   }
5065
5066   // Unknown option.
5067   Warning(Parser.getTok().getLoc(), 
5068           "unknown option, expected 'pic0' or 'pic2'");
5069   Parser.eatToEndOfStatement();
5070   return false;
5071 }
5072
5073 /// parseInsnDirective
5074 ///  ::= .insn
5075 bool MipsAsmParser::parseInsnDirective() {
5076   // If this is not the end of the statement, report an error.
5077   if (getLexer().isNot(AsmToken::EndOfStatement)) {
5078     reportParseError("unexpected token, expected end of statement");
5079     return false;
5080   }
5081
5082   // The actual label marking happens in
5083   // MipsELFStreamer::createPendingLabelRelocs().
5084   getTargetStreamer().emitDirectiveInsn();
5085
5086   getParser().Lex(); // Eat EndOfStatement token.
5087   return false;
5088 }
5089
5090 /// parseDirectiveModule
5091 ///  ::= .module oddspreg
5092 ///  ::= .module nooddspreg
5093 ///  ::= .module fp=value
5094 ///  ::= .module softfloat
5095 ///  ::= .module hardfloat
5096 bool MipsAsmParser::parseDirectiveModule() {
5097   MCAsmParser &Parser = getParser();
5098   MCAsmLexer &Lexer = getLexer();
5099   SMLoc L = Lexer.getLoc();
5100
5101   if (!getTargetStreamer().isModuleDirectiveAllowed()) {
5102     // TODO : get a better message.
5103     reportParseError(".module directive must appear before any code");
5104     return false;
5105   }
5106
5107   StringRef Option;
5108   if (Parser.parseIdentifier(Option)) {
5109     reportParseError("expected .module option identifier");
5110     return false;
5111   }
5112
5113   if (Option == "oddspreg") {
5114     clearModuleFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
5115
5116     // Synchronize the abiflags information with the FeatureBits information we
5117     // changed above.
5118     getTargetStreamer().updateABIInfo(*this);
5119
5120     // If printing assembly, use the recently updated abiflags information.
5121     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
5122     // emitted at the end).
5123     getTargetStreamer().emitDirectiveModuleOddSPReg();
5124
5125     // If this is not the end of the statement, report an error.
5126     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5127       reportParseError("unexpected token, expected end of statement");
5128       return false;
5129     }
5130
5131     return false; // parseDirectiveModule has finished successfully.
5132   } else if (Option == "nooddspreg") {
5133     if (!isABI_O32()) {
5134       Error(L, "'.module nooddspreg' requires the O32 ABI");
5135       return false;
5136     }
5137
5138     setModuleFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
5139
5140     // Synchronize the abiflags information with the FeatureBits information we
5141     // changed above.
5142     getTargetStreamer().updateABIInfo(*this);
5143
5144     // If printing assembly, use the recently updated abiflags information.
5145     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
5146     // emitted at the end).
5147     getTargetStreamer().emitDirectiveModuleOddSPReg();
5148
5149     // If this is not the end of the statement, report an error.
5150     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5151       reportParseError("unexpected token, expected end of statement");
5152       return false;
5153     }
5154
5155     return false; // parseDirectiveModule has finished successfully.
5156   } else if (Option == "fp") {
5157     return parseDirectiveModuleFP();
5158   } else if (Option == "softfloat") {
5159     setModuleFeatureBits(Mips::FeatureSoftFloat, "soft-float");
5160
5161     // Synchronize the ABI Flags information with the FeatureBits information we
5162     // updated above.
5163     getTargetStreamer().updateABIInfo(*this);
5164
5165     // If printing assembly, use the recently updated ABI Flags information.
5166     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
5167     // emitted later).
5168     getTargetStreamer().emitDirectiveModuleSoftFloat();
5169
5170     // If this is not the end of the statement, report an error.
5171     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5172       reportParseError("unexpected token, expected end of statement");
5173       return false;
5174     }
5175
5176     return false; // parseDirectiveModule has finished successfully.
5177   } else if (Option == "hardfloat") {
5178     clearModuleFeatureBits(Mips::FeatureSoftFloat, "soft-float");
5179
5180     // Synchronize the ABI Flags information with the FeatureBits information we
5181     // updated above.
5182     getTargetStreamer().updateABIInfo(*this);
5183
5184     // If printing assembly, use the recently updated ABI Flags information.
5185     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
5186     // emitted later).
5187     getTargetStreamer().emitDirectiveModuleHardFloat();
5188
5189     // If this is not the end of the statement, report an error.
5190     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5191       reportParseError("unexpected token, expected end of statement");
5192       return false;
5193     }
5194
5195     return false; // parseDirectiveModule has finished successfully.
5196   } else {
5197     return Error(L, "'" + Twine(Option) + "' is not a valid .module option.");
5198   }
5199 }
5200
5201 /// parseDirectiveModuleFP
5202 ///  ::= =32
5203 ///  ::= =xx
5204 ///  ::= =64
5205 bool MipsAsmParser::parseDirectiveModuleFP() {
5206   MCAsmParser &Parser = getParser();
5207   MCAsmLexer &Lexer = getLexer();
5208
5209   if (Lexer.isNot(AsmToken::Equal)) {
5210     reportParseError("unexpected token, expected equals sign '='");
5211     return false;
5212   }
5213   Parser.Lex(); // Eat '=' token.
5214
5215   MipsABIFlagsSection::FpABIKind FpABI;
5216   if (!parseFpABIValue(FpABI, ".module"))
5217     return false;
5218
5219   if (getLexer().isNot(AsmToken::EndOfStatement)) {
5220     reportParseError("unexpected token, expected end of statement");
5221     return false;
5222   }
5223
5224   // Synchronize the abiflags information with the FeatureBits information we
5225   // changed above.
5226   getTargetStreamer().updateABIInfo(*this);
5227
5228   // If printing assembly, use the recently updated abiflags information.
5229   // If generating ELF, don't do anything (the .MIPS.abiflags section gets
5230   // emitted at the end).
5231   getTargetStreamer().emitDirectiveModuleFP();
5232
5233   Parser.Lex(); // Consume the EndOfStatement.
5234   return false;
5235 }
5236
5237 bool MipsAsmParser::parseFpABIValue(MipsABIFlagsSection::FpABIKind &FpABI,
5238                                     StringRef Directive) {
5239   MCAsmParser &Parser = getParser();
5240   MCAsmLexer &Lexer = getLexer();
5241   bool ModuleLevelOptions = Directive == ".module";
5242
5243   if (Lexer.is(AsmToken::Identifier)) {
5244     StringRef Value = Parser.getTok().getString();
5245     Parser.Lex();
5246
5247     if (Value != "xx") {
5248       reportParseError("unsupported value, expected 'xx', '32' or '64'");
5249       return false;
5250     }
5251
5252     if (!isABI_O32()) {
5253       reportParseError("'" + Directive + " fp=xx' requires the O32 ABI");
5254       return false;
5255     }
5256
5257     FpABI = MipsABIFlagsSection::FpABIKind::XX;
5258     if (ModuleLevelOptions) {
5259       setModuleFeatureBits(Mips::FeatureFPXX, "fpxx");
5260       clearModuleFeatureBits(Mips::FeatureFP64Bit, "fp64");
5261     } else {
5262       setFeatureBits(Mips::FeatureFPXX, "fpxx");
5263       clearFeatureBits(Mips::FeatureFP64Bit, "fp64");
5264     }
5265     return true;
5266   }
5267
5268   if (Lexer.is(AsmToken::Integer)) {
5269     unsigned Value = Parser.getTok().getIntVal();
5270     Parser.Lex();
5271
5272     if (Value != 32 && Value != 64) {
5273       reportParseError("unsupported value, expected 'xx', '32' or '64'");
5274       return false;
5275     }
5276
5277     if (Value == 32) {
5278       if (!isABI_O32()) {
5279         reportParseError("'" + Directive + " fp=32' requires the O32 ABI");
5280         return false;
5281       }
5282
5283       FpABI = MipsABIFlagsSection::FpABIKind::S32;
5284       if (ModuleLevelOptions) {
5285         clearModuleFeatureBits(Mips::FeatureFPXX, "fpxx");
5286         clearModuleFeatureBits(Mips::FeatureFP64Bit, "fp64");
5287       } else {
5288         clearFeatureBits(Mips::FeatureFPXX, "fpxx");
5289         clearFeatureBits(Mips::FeatureFP64Bit, "fp64");
5290       }
5291     } else {
5292       FpABI = MipsABIFlagsSection::FpABIKind::S64;
5293       if (ModuleLevelOptions) {
5294         clearModuleFeatureBits(Mips::FeatureFPXX, "fpxx");
5295         setModuleFeatureBits(Mips::FeatureFP64Bit, "fp64");
5296       } else {
5297         clearFeatureBits(Mips::FeatureFPXX, "fpxx");
5298         setFeatureBits(Mips::FeatureFP64Bit, "fp64");
5299       }
5300     }
5301
5302     return true;
5303   }
5304
5305   return false;
5306 }
5307
5308 bool MipsAsmParser::ParseDirective(AsmToken DirectiveID) {
5309   MCAsmParser &Parser = getParser();
5310   StringRef IDVal = DirectiveID.getString();
5311
5312   if (IDVal == ".cpload")
5313     return parseDirectiveCpLoad(DirectiveID.getLoc());
5314   if (IDVal == ".dword") {
5315     parseDataDirective(8, DirectiveID.getLoc());
5316     return false;
5317   }
5318   if (IDVal == ".ent") {
5319     StringRef SymbolName;
5320
5321     if (Parser.parseIdentifier(SymbolName)) {
5322       reportParseError("expected identifier after .ent");
5323       return false;
5324     }
5325
5326     // There's an undocumented extension that allows an integer to
5327     // follow the name of the procedure which AFAICS is ignored by GAS.
5328     // Example: .ent foo,2
5329     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5330       if (getLexer().isNot(AsmToken::Comma)) {
5331         // Even though we accept this undocumented extension for compatibility
5332         // reasons, the additional integer argument does not actually change
5333         // the behaviour of the '.ent' directive, so we would like to discourage
5334         // its use. We do this by not referring to the extended version in
5335         // error messages which are not directly related to its use.
5336         reportParseError("unexpected token, expected end of statement");
5337         return false;
5338       }
5339       Parser.Lex(); // Eat the comma.
5340       const MCExpr *DummyNumber;
5341       int64_t DummyNumberVal;
5342       // If the user was explicitly trying to use the extended version,
5343       // we still give helpful extension-related error messages.
5344       if (Parser.parseExpression(DummyNumber)) {
5345         reportParseError("expected number after comma");
5346         return false;
5347       }
5348       if (!DummyNumber->evaluateAsAbsolute(DummyNumberVal)) {
5349         reportParseError("expected an absolute expression after comma");
5350         return false;
5351       }
5352     }
5353
5354     // If this is not the end of the statement, report an error.
5355     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5356       reportParseError("unexpected token, expected end of statement");
5357       return false;
5358     }
5359
5360     MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
5361
5362     getTargetStreamer().emitDirectiveEnt(*Sym);
5363     CurrentFn = Sym;
5364     return false;
5365   }
5366
5367   if (IDVal == ".end") {
5368     StringRef SymbolName;
5369
5370     if (Parser.parseIdentifier(SymbolName)) {
5371       reportParseError("expected identifier after .end");
5372       return false;
5373     }
5374
5375     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5376       reportParseError("unexpected token, expected end of statement");
5377       return false;
5378     }
5379
5380     if (CurrentFn == nullptr) {
5381       reportParseError(".end used without .ent");
5382       return false;
5383     }
5384
5385     if ((SymbolName != CurrentFn->getName())) {
5386       reportParseError(".end symbol does not match .ent symbol");
5387       return false;
5388     }
5389
5390     getTargetStreamer().emitDirectiveEnd(SymbolName);
5391     CurrentFn = nullptr;
5392     return false;
5393   }
5394
5395   if (IDVal == ".frame") {
5396     // .frame $stack_reg, frame_size_in_bytes, $return_reg
5397     SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> TmpReg;
5398     OperandMatchResultTy ResTy = parseAnyRegister(TmpReg);
5399     if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
5400       reportParseError("expected stack register");
5401       return false;
5402     }
5403
5404     MipsOperand &StackRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
5405     if (!StackRegOpnd.isGPRAsmReg()) {
5406       reportParseError(StackRegOpnd.getStartLoc(),
5407                        "expected general purpose register");
5408       return false;
5409     }
5410     unsigned StackReg = StackRegOpnd.getGPR32Reg();
5411
5412     if (Parser.getTok().is(AsmToken::Comma))
5413       Parser.Lex();
5414     else {
5415       reportParseError("unexpected token, expected comma");
5416       return false;
5417     }
5418
5419     // Parse the frame size.
5420     const MCExpr *FrameSize;
5421     int64_t FrameSizeVal;
5422
5423     if (Parser.parseExpression(FrameSize)) {
5424       reportParseError("expected frame size value");
5425       return false;
5426     }
5427
5428     if (!FrameSize->evaluateAsAbsolute(FrameSizeVal)) {
5429       reportParseError("frame size not an absolute expression");
5430       return false;
5431     }
5432
5433     if (Parser.getTok().is(AsmToken::Comma))
5434       Parser.Lex();
5435     else {
5436       reportParseError("unexpected token, expected comma");
5437       return false;
5438     }
5439
5440     // Parse the return register.
5441     TmpReg.clear();
5442     ResTy = parseAnyRegister(TmpReg);
5443     if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
5444       reportParseError("expected return register");
5445       return false;
5446     }
5447
5448     MipsOperand &ReturnRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
5449     if (!ReturnRegOpnd.isGPRAsmReg()) {
5450       reportParseError(ReturnRegOpnd.getStartLoc(),
5451                        "expected general purpose register");
5452       return false;
5453     }
5454
5455     // If this is not the end of the statement, report an error.
5456     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5457       reportParseError("unexpected token, expected end of statement");
5458       return false;
5459     }
5460
5461     getTargetStreamer().emitFrame(StackReg, FrameSizeVal,
5462                                   ReturnRegOpnd.getGPR32Reg());
5463     return false;
5464   }
5465
5466   if (IDVal == ".set") {
5467     return parseDirectiveSet();
5468   }
5469
5470   if (IDVal == ".mask" || IDVal == ".fmask") {
5471     // .mask bitmask, frame_offset
5472     // bitmask: One bit for each register used.
5473     // frame_offset: Offset from Canonical Frame Address ($sp on entry) where
5474     //               first register is expected to be saved.
5475     // Examples:
5476     //   .mask 0x80000000, -4
5477     //   .fmask 0x80000000, -4
5478     //
5479
5480     // Parse the bitmask
5481     const MCExpr *BitMask;
5482     int64_t BitMaskVal;
5483
5484     if (Parser.parseExpression(BitMask)) {
5485       reportParseError("expected bitmask value");
5486       return false;
5487     }
5488
5489     if (!BitMask->evaluateAsAbsolute(BitMaskVal)) {
5490       reportParseError("bitmask not an absolute expression");
5491       return false;
5492     }
5493
5494     if (Parser.getTok().is(AsmToken::Comma))
5495       Parser.Lex();
5496     else {
5497       reportParseError("unexpected token, expected comma");
5498       return false;
5499     }
5500
5501     // Parse the frame_offset
5502     const MCExpr *FrameOffset;
5503     int64_t FrameOffsetVal;
5504
5505     if (Parser.parseExpression(FrameOffset)) {
5506       reportParseError("expected frame offset value");
5507       return false;
5508     }
5509
5510     if (!FrameOffset->evaluateAsAbsolute(FrameOffsetVal)) {
5511       reportParseError("frame offset not an absolute expression");
5512       return false;
5513     }
5514
5515     // If this is not the end of the statement, report an error.
5516     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5517       reportParseError("unexpected token, expected end of statement");
5518       return false;
5519     }
5520
5521     if (IDVal == ".mask")
5522       getTargetStreamer().emitMask(BitMaskVal, FrameOffsetVal);
5523     else
5524       getTargetStreamer().emitFMask(BitMaskVal, FrameOffsetVal);
5525     return false;
5526   }
5527
5528   if (IDVal == ".nan")
5529     return parseDirectiveNaN();
5530
5531   if (IDVal == ".gpword") {
5532     parseDirectiveGpWord();
5533     return false;
5534   }
5535
5536   if (IDVal == ".gpdword") {
5537     parseDirectiveGpDWord();
5538     return false;
5539   }
5540
5541   if (IDVal == ".word") {
5542     parseDataDirective(4, DirectiveID.getLoc());
5543     return false;
5544   }
5545
5546   if (IDVal == ".option")
5547     return parseDirectiveOption();
5548
5549   if (IDVal == ".abicalls") {
5550     getTargetStreamer().emitDirectiveAbiCalls();
5551     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
5552       Error(Parser.getTok().getLoc(), 
5553             "unexpected token, expected end of statement");
5554       // Clear line
5555       Parser.eatToEndOfStatement();
5556     }
5557     return false;
5558   }
5559
5560   if (IDVal == ".cpsetup")
5561     return parseDirectiveCPSetup();
5562
5563   if (IDVal == ".module")
5564     return parseDirectiveModule();
5565
5566   if (IDVal == ".llvm_internal_mips_reallow_module_directive")
5567     return parseInternalDirectiveReallowModule();
5568
5569   if (IDVal == ".insn")
5570     return parseInsnDirective();
5571
5572   return true;
5573 }
5574
5575 bool MipsAsmParser::parseInternalDirectiveReallowModule() {
5576   // If this is not the end of the statement, report an error.
5577   if (getLexer().isNot(AsmToken::EndOfStatement)) {
5578     reportParseError("unexpected token, expected end of statement");
5579     return false;
5580   }
5581
5582   getTargetStreamer().reallowModuleDirective();
5583
5584   getParser().Lex(); // Eat EndOfStatement token.
5585   return false;
5586 }
5587
5588 extern "C" void LLVMInitializeMipsAsmParser() {
5589   RegisterMCAsmParser<MipsAsmParser> X(TheMipsTarget);
5590   RegisterMCAsmParser<MipsAsmParser> Y(TheMipselTarget);
5591   RegisterMCAsmParser<MipsAsmParser> A(TheMips64Target);
5592   RegisterMCAsmParser<MipsAsmParser> B(TheMips64elTarget);
5593 }
5594
5595 #define GET_REGISTER_MATCHER
5596 #define GET_MATCHER_IMPLEMENTATION
5597 #include "MipsGenAsmMatcher.inc"