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