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