[mips] [IAS] Fix parsing of memory offset expressions with parenthesis depth >1.
[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   unsigned NumOfLParen = 0;
3109
3110   while (getLexer().getKind() == AsmToken::LParen) {
3111     Parser.Lex();
3112     ++NumOfLParen;
3113   }
3114
3115   switch (getLexer().getKind()) {
3116   default:
3117     return true;
3118   case AsmToken::Identifier:
3119   case AsmToken::LParen:
3120   case AsmToken::Integer:
3121   case AsmToken::Minus:
3122   case AsmToken::Plus:
3123     if (isParenExpr)
3124       Result = getParser().parseParenExprOfDepth(NumOfLParen, Res, S);
3125     else
3126       Result = (getParser().parseExpression(Res));
3127     while (getLexer().getKind() == AsmToken::RParen)
3128       Parser.Lex();
3129     break;
3130   case AsmToken::Percent:
3131     Result = parseRelocOperand(Res);
3132   }
3133   return Result;
3134 }
3135
3136 MipsAsmParser::OperandMatchResultTy
3137 MipsAsmParser::parseMemOperand(OperandVector &Operands) {
3138   MCAsmParser &Parser = getParser();
3139   DEBUG(dbgs() << "parseMemOperand\n");
3140   const MCExpr *IdVal = nullptr;
3141   SMLoc S;
3142   bool isParenExpr = false;
3143   MipsAsmParser::OperandMatchResultTy Res = MatchOperand_NoMatch;
3144   // First operand is the offset.
3145   S = Parser.getTok().getLoc();
3146
3147   if (getLexer().getKind() == AsmToken::LParen) {
3148     Parser.Lex();
3149     isParenExpr = true;
3150   }
3151
3152   if (getLexer().getKind() != AsmToken::Dollar) {
3153     if (parseMemOffset(IdVal, isParenExpr))
3154       return MatchOperand_ParseFail;
3155
3156     const AsmToken &Tok = Parser.getTok(); // Get the next token.
3157     if (Tok.isNot(AsmToken::LParen)) {
3158       MipsOperand &Mnemonic = static_cast<MipsOperand &>(*Operands[0]);
3159       if (Mnemonic.getToken() == "la") {
3160         SMLoc E =
3161             SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3162         Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
3163         return MatchOperand_Success;
3164       }
3165       if (Tok.is(AsmToken::EndOfStatement)) {
3166         SMLoc E =
3167             SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3168
3169         // Zero register assumed, add a memory operand with ZERO as its base.
3170         // "Base" will be managed by k_Memory.
3171         auto Base = MipsOperand::createGPRReg(0, getContext().getRegisterInfo(),
3172                                               S, E, *this);
3173         Operands.push_back(
3174             MipsOperand::CreateMem(std::move(Base), IdVal, S, E, *this));
3175         return MatchOperand_Success;
3176       }
3177       Error(Parser.getTok().getLoc(), "'(' expected");
3178       return MatchOperand_ParseFail;
3179     }
3180
3181     Parser.Lex(); // Eat the '(' token.
3182   }
3183
3184   Res = parseAnyRegister(Operands);
3185   if (Res != MatchOperand_Success)
3186     return Res;
3187
3188   if (Parser.getTok().isNot(AsmToken::RParen)) {
3189     Error(Parser.getTok().getLoc(), "')' expected");
3190     return MatchOperand_ParseFail;
3191   }
3192
3193   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3194
3195   Parser.Lex(); // Eat the ')' token.
3196
3197   if (!IdVal)
3198     IdVal = MCConstantExpr::create(0, getContext());
3199
3200   // Replace the register operand with the memory operand.
3201   std::unique_ptr<MipsOperand> op(
3202       static_cast<MipsOperand *>(Operands.back().release()));
3203   // Remove the register from the operands.
3204   // "op" will be managed by k_Memory.
3205   Operands.pop_back();
3206   // Add the memory operand.
3207   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(IdVal)) {
3208     int64_t Imm;
3209     if (IdVal->evaluateAsAbsolute(Imm))
3210       IdVal = MCConstantExpr::create(Imm, getContext());
3211     else if (BE->getLHS()->getKind() != MCExpr::SymbolRef)
3212       IdVal = MCBinaryExpr::create(BE->getOpcode(), BE->getRHS(), BE->getLHS(),
3213                                    getContext());
3214   }
3215
3216   Operands.push_back(MipsOperand::CreateMem(std::move(op), IdVal, S, E, *this));
3217   return MatchOperand_Success;
3218 }
3219
3220 bool MipsAsmParser::searchSymbolAlias(OperandVector &Operands) {
3221   MCAsmParser &Parser = getParser();
3222   MCSymbol *Sym = getContext().lookupSymbol(Parser.getTok().getIdentifier());
3223   if (Sym) {
3224     SMLoc S = Parser.getTok().getLoc();
3225     const MCExpr *Expr;
3226     if (Sym->isVariable())
3227       Expr = Sym->getVariableValue();
3228     else
3229       return false;
3230     if (Expr->getKind() == MCExpr::SymbolRef) {
3231       const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
3232       StringRef DefSymbol = Ref->getSymbol().getName();
3233       if (DefSymbol.startswith("$")) {
3234         OperandMatchResultTy ResTy =
3235             matchAnyRegisterNameWithoutDollar(Operands, DefSymbol.substr(1), S);
3236         if (ResTy == MatchOperand_Success) {
3237           Parser.Lex();
3238           return true;
3239         } else if (ResTy == MatchOperand_ParseFail)
3240           llvm_unreachable("Should never ParseFail");
3241         return false;
3242       }
3243     } else if (Expr->getKind() == MCExpr::Constant) {
3244       Parser.Lex();
3245       const MCConstantExpr *Const = static_cast<const MCConstantExpr *>(Expr);
3246       Operands.push_back(
3247           MipsOperand::CreateImm(Const, S, Parser.getTok().getLoc(), *this));
3248       return true;
3249     }
3250   }
3251   return false;
3252 }
3253
3254 MipsAsmParser::OperandMatchResultTy
3255 MipsAsmParser::matchAnyRegisterNameWithoutDollar(OperandVector &Operands,
3256                                                  StringRef Identifier,
3257                                                  SMLoc S) {
3258   int Index = matchCPURegisterName(Identifier);
3259   if (Index != -1) {
3260     Operands.push_back(MipsOperand::createGPRReg(
3261         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3262     return MatchOperand_Success;
3263   }
3264
3265   Index = matchHWRegsRegisterName(Identifier);
3266   if (Index != -1) {
3267     Operands.push_back(MipsOperand::createHWRegsReg(
3268         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3269     return MatchOperand_Success;
3270   }
3271
3272   Index = matchFPURegisterName(Identifier);
3273   if (Index != -1) {
3274     Operands.push_back(MipsOperand::createFGRReg(
3275         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3276     return MatchOperand_Success;
3277   }
3278
3279   Index = matchFCCRegisterName(Identifier);
3280   if (Index != -1) {
3281     Operands.push_back(MipsOperand::createFCCReg(
3282         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3283     return MatchOperand_Success;
3284   }
3285
3286   Index = matchACRegisterName(Identifier);
3287   if (Index != -1) {
3288     Operands.push_back(MipsOperand::createACCReg(
3289         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3290     return MatchOperand_Success;
3291   }
3292
3293   Index = matchMSA128RegisterName(Identifier);
3294   if (Index != -1) {
3295     Operands.push_back(MipsOperand::createMSA128Reg(
3296         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3297     return MatchOperand_Success;
3298   }
3299
3300   Index = matchMSA128CtrlRegisterName(Identifier);
3301   if (Index != -1) {
3302     Operands.push_back(MipsOperand::createMSACtrlReg(
3303         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3304     return MatchOperand_Success;
3305   }
3306
3307   return MatchOperand_NoMatch;
3308 }
3309
3310 MipsAsmParser::OperandMatchResultTy
3311 MipsAsmParser::matchAnyRegisterWithoutDollar(OperandVector &Operands, SMLoc S) {
3312   MCAsmParser &Parser = getParser();
3313   auto Token = Parser.getLexer().peekTok(false);
3314
3315   if (Token.is(AsmToken::Identifier)) {
3316     DEBUG(dbgs() << ".. identifier\n");
3317     StringRef Identifier = Token.getIdentifier();
3318     OperandMatchResultTy ResTy =
3319         matchAnyRegisterNameWithoutDollar(Operands, Identifier, S);
3320     return ResTy;
3321   } else if (Token.is(AsmToken::Integer)) {
3322     DEBUG(dbgs() << ".. integer\n");
3323     Operands.push_back(MipsOperand::createNumericReg(
3324         Token.getIntVal(), getContext().getRegisterInfo(), S, Token.getLoc(),
3325         *this));
3326     return MatchOperand_Success;
3327   }
3328
3329   DEBUG(dbgs() << Parser.getTok().getKind() << "\n");
3330
3331   return MatchOperand_NoMatch;
3332 }
3333
3334 MipsAsmParser::OperandMatchResultTy
3335 MipsAsmParser::parseAnyRegister(OperandVector &Operands) {
3336   MCAsmParser &Parser = getParser();
3337   DEBUG(dbgs() << "parseAnyRegister\n");
3338
3339   auto Token = Parser.getTok();
3340
3341   SMLoc S = Token.getLoc();
3342
3343   if (Token.isNot(AsmToken::Dollar)) {
3344     DEBUG(dbgs() << ".. !$ -> try sym aliasing\n");
3345     if (Token.is(AsmToken::Identifier)) {
3346       if (searchSymbolAlias(Operands))
3347         return MatchOperand_Success;
3348     }
3349     DEBUG(dbgs() << ".. !symalias -> NoMatch\n");
3350     return MatchOperand_NoMatch;
3351   }
3352   DEBUG(dbgs() << ".. $\n");
3353
3354   OperandMatchResultTy ResTy = matchAnyRegisterWithoutDollar(Operands, S);
3355   if (ResTy == MatchOperand_Success) {
3356     Parser.Lex(); // $
3357     Parser.Lex(); // identifier
3358   }
3359   return ResTy;
3360 }
3361
3362 MipsAsmParser::OperandMatchResultTy
3363 MipsAsmParser::parseImm(OperandVector &Operands) {
3364   MCAsmParser &Parser = getParser();
3365   switch (getLexer().getKind()) {
3366   default:
3367     return MatchOperand_NoMatch;
3368   case AsmToken::LParen:
3369   case AsmToken::Minus:
3370   case AsmToken::Plus:
3371   case AsmToken::Integer:
3372   case AsmToken::Tilde:
3373   case AsmToken::String:
3374     break;
3375   }
3376
3377   const MCExpr *IdVal;
3378   SMLoc S = Parser.getTok().getLoc();
3379   if (getParser().parseExpression(IdVal))
3380     return MatchOperand_ParseFail;
3381
3382   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3383   Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
3384   return MatchOperand_Success;
3385 }
3386
3387 MipsAsmParser::OperandMatchResultTy
3388 MipsAsmParser::parseJumpTarget(OperandVector &Operands) {
3389   MCAsmParser &Parser = getParser();
3390   DEBUG(dbgs() << "parseJumpTarget\n");
3391
3392   SMLoc S = getLexer().getLoc();
3393
3394   // Integers and expressions are acceptable
3395   OperandMatchResultTy ResTy = parseImm(Operands);
3396   if (ResTy != MatchOperand_NoMatch)
3397     return ResTy;
3398
3399   // Registers are a valid target and have priority over symbols.
3400   ResTy = parseAnyRegister(Operands);
3401   if (ResTy != MatchOperand_NoMatch)
3402     return ResTy;
3403
3404   const MCExpr *Expr = nullptr;
3405   if (Parser.parseExpression(Expr)) {
3406     // We have no way of knowing if a symbol was consumed so we must ParseFail
3407     return MatchOperand_ParseFail;
3408   }
3409   Operands.push_back(
3410       MipsOperand::CreateImm(Expr, S, getLexer().getLoc(), *this));
3411   return MatchOperand_Success;
3412 }
3413
3414 MipsAsmParser::OperandMatchResultTy
3415 MipsAsmParser::parseInvNum(OperandVector &Operands) {
3416   MCAsmParser &Parser = getParser();
3417   const MCExpr *IdVal;
3418   // If the first token is '$' we may have register operand.
3419   if (Parser.getTok().is(AsmToken::Dollar))
3420     return MatchOperand_NoMatch;
3421   SMLoc S = Parser.getTok().getLoc();
3422   if (getParser().parseExpression(IdVal))
3423     return MatchOperand_ParseFail;
3424   const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(IdVal);
3425   assert(MCE && "Unexpected MCExpr type.");
3426   int64_t Val = MCE->getValue();
3427   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3428   Operands.push_back(MipsOperand::CreateImm(
3429       MCConstantExpr::create(0 - Val, getContext()), S, E, *this));
3430   return MatchOperand_Success;
3431 }
3432
3433 MipsAsmParser::OperandMatchResultTy
3434 MipsAsmParser::parseLSAImm(OperandVector &Operands) {
3435   MCAsmParser &Parser = getParser();
3436   switch (getLexer().getKind()) {
3437   default:
3438     return MatchOperand_NoMatch;
3439   case AsmToken::LParen:
3440   case AsmToken::Plus:
3441   case AsmToken::Minus:
3442   case AsmToken::Integer:
3443     break;
3444   }
3445
3446   const MCExpr *Expr;
3447   SMLoc S = Parser.getTok().getLoc();
3448
3449   if (getParser().parseExpression(Expr))
3450     return MatchOperand_ParseFail;
3451
3452   int64_t Val;
3453   if (!Expr->evaluateAsAbsolute(Val)) {
3454     Error(S, "expected immediate value");
3455     return MatchOperand_ParseFail;
3456   }
3457
3458   // The LSA instruction allows a 2-bit unsigned immediate. For this reason
3459   // and because the CPU always adds one to the immediate field, the allowed
3460   // range becomes 1..4. We'll only check the range here and will deal
3461   // with the addition/subtraction when actually decoding/encoding
3462   // the instruction.
3463   if (Val < 1 || Val > 4) {
3464     Error(S, "immediate not in range (1..4)");
3465     return MatchOperand_ParseFail;
3466   }
3467
3468   Operands.push_back(
3469       MipsOperand::CreateImm(Expr, S, Parser.getTok().getLoc(), *this));
3470   return MatchOperand_Success;
3471 }
3472
3473 MipsAsmParser::OperandMatchResultTy
3474 MipsAsmParser::parseRegisterList(OperandVector &Operands) {
3475   MCAsmParser &Parser = getParser();
3476   SmallVector<unsigned, 10> Regs;
3477   unsigned RegNo;
3478   unsigned PrevReg = Mips::NoRegister;
3479   bool RegRange = false;
3480   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> TmpOperands;
3481
3482   if (Parser.getTok().isNot(AsmToken::Dollar))
3483     return MatchOperand_ParseFail;
3484
3485   SMLoc S = Parser.getTok().getLoc();
3486   while (parseAnyRegister(TmpOperands) == MatchOperand_Success) {
3487     SMLoc E = getLexer().getLoc();
3488     MipsOperand &Reg = static_cast<MipsOperand &>(*TmpOperands.back());
3489     RegNo = isGP64bit() ? Reg.getGPR64Reg() : Reg.getGPR32Reg();
3490     if (RegRange) {
3491       // Remove last register operand because registers from register range
3492       // should be inserted first.
3493       if (RegNo == Mips::RA) {
3494         Regs.push_back(RegNo);
3495       } else {
3496         unsigned TmpReg = PrevReg + 1;
3497         while (TmpReg <= RegNo) {
3498           if ((TmpReg < Mips::S0) || (TmpReg > Mips::S7)) {
3499             Error(E, "invalid register operand");
3500             return MatchOperand_ParseFail;
3501           }
3502
3503           PrevReg = TmpReg;
3504           Regs.push_back(TmpReg++);
3505         }
3506       }
3507
3508       RegRange = false;
3509     } else {
3510       if ((PrevReg == Mips::NoRegister) && (RegNo != Mips::S0) &&
3511           (RegNo != Mips::RA)) {
3512         Error(E, "$16 or $31 expected");
3513         return MatchOperand_ParseFail;
3514       } else if (((RegNo < Mips::S0) || (RegNo > Mips::S7)) &&
3515                  (RegNo != Mips::FP) && (RegNo != Mips::RA)) {
3516         Error(E, "invalid register operand");
3517         return MatchOperand_ParseFail;
3518       } else if ((PrevReg != Mips::NoRegister) && (RegNo != PrevReg + 1) &&
3519                  (RegNo != Mips::FP) && (RegNo != Mips::RA)) {
3520         Error(E, "consecutive register numbers expected");
3521         return MatchOperand_ParseFail;
3522       }
3523
3524       Regs.push_back(RegNo);
3525     }
3526
3527     if (Parser.getTok().is(AsmToken::Minus))
3528       RegRange = true;
3529
3530     if (!Parser.getTok().isNot(AsmToken::Minus) &&
3531         !Parser.getTok().isNot(AsmToken::Comma)) {
3532       Error(E, "',' or '-' expected");
3533       return MatchOperand_ParseFail;
3534     }
3535
3536     Lex(); // Consume comma or minus
3537     if (Parser.getTok().isNot(AsmToken::Dollar))
3538       break;
3539
3540     PrevReg = RegNo;
3541   }
3542
3543   SMLoc E = Parser.getTok().getLoc();
3544   Operands.push_back(MipsOperand::CreateRegList(Regs, S, E, *this));
3545   parseMemOperand(Operands);
3546   return MatchOperand_Success;
3547 }
3548
3549 MipsAsmParser::OperandMatchResultTy
3550 MipsAsmParser::parseRegisterPair(OperandVector &Operands) {
3551   MCAsmParser &Parser = getParser();
3552
3553   SMLoc S = Parser.getTok().getLoc();
3554   if (parseAnyRegister(Operands) != MatchOperand_Success)
3555     return MatchOperand_ParseFail;
3556
3557   SMLoc E = Parser.getTok().getLoc();
3558   MipsOperand &Op = static_cast<MipsOperand &>(*Operands.back());
3559   unsigned Reg = Op.getGPR32Reg();
3560   Operands.pop_back();
3561   Operands.push_back(MipsOperand::CreateRegPair(Reg, S, E, *this));
3562   return MatchOperand_Success;
3563 }
3564
3565 MipsAsmParser::OperandMatchResultTy
3566 MipsAsmParser::parseMovePRegPair(OperandVector &Operands) {
3567   MCAsmParser &Parser = getParser();
3568   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> TmpOperands;
3569   SmallVector<unsigned, 10> Regs;
3570
3571   if (Parser.getTok().isNot(AsmToken::Dollar))
3572     return MatchOperand_ParseFail;
3573
3574   SMLoc S = Parser.getTok().getLoc();
3575
3576   if (parseAnyRegister(TmpOperands) != MatchOperand_Success)
3577     return MatchOperand_ParseFail;
3578
3579   MipsOperand *Reg = &static_cast<MipsOperand &>(*TmpOperands.back());
3580   unsigned RegNo = isGP64bit() ? Reg->getGPR64Reg() : Reg->getGPR32Reg();
3581   Regs.push_back(RegNo);
3582
3583   SMLoc E = Parser.getTok().getLoc();
3584   if (Parser.getTok().isNot(AsmToken::Comma)) {
3585     Error(E, "',' expected");
3586     return MatchOperand_ParseFail;
3587   }
3588
3589   // Remove comma.
3590   Parser.Lex();
3591
3592   if (parseAnyRegister(TmpOperands) != MatchOperand_Success)
3593     return MatchOperand_ParseFail;
3594
3595   Reg = &static_cast<MipsOperand &>(*TmpOperands.back());
3596   RegNo = isGP64bit() ? Reg->getGPR64Reg() : Reg->getGPR32Reg();
3597   Regs.push_back(RegNo);
3598
3599   Operands.push_back(MipsOperand::CreateRegList(Regs, S, E, *this));
3600
3601   return MatchOperand_Success;
3602 }
3603
3604 MCSymbolRefExpr::VariantKind MipsAsmParser::getVariantKind(StringRef Symbol) {
3605
3606   MCSymbolRefExpr::VariantKind VK =
3607       StringSwitch<MCSymbolRefExpr::VariantKind>(Symbol)
3608           .Case("hi", MCSymbolRefExpr::VK_Mips_ABS_HI)
3609           .Case("lo", MCSymbolRefExpr::VK_Mips_ABS_LO)
3610           .Case("gp_rel", MCSymbolRefExpr::VK_Mips_GPREL)
3611           .Case("call16", MCSymbolRefExpr::VK_Mips_GOT_CALL)
3612           .Case("got", MCSymbolRefExpr::VK_Mips_GOT)
3613           .Case("tlsgd", MCSymbolRefExpr::VK_Mips_TLSGD)
3614           .Case("tlsldm", MCSymbolRefExpr::VK_Mips_TLSLDM)
3615           .Case("dtprel_hi", MCSymbolRefExpr::VK_Mips_DTPREL_HI)
3616           .Case("dtprel_lo", MCSymbolRefExpr::VK_Mips_DTPREL_LO)
3617           .Case("gottprel", MCSymbolRefExpr::VK_Mips_GOTTPREL)
3618           .Case("tprel_hi", MCSymbolRefExpr::VK_Mips_TPREL_HI)
3619           .Case("tprel_lo", MCSymbolRefExpr::VK_Mips_TPREL_LO)
3620           .Case("got_disp", MCSymbolRefExpr::VK_Mips_GOT_DISP)
3621           .Case("got_page", MCSymbolRefExpr::VK_Mips_GOT_PAGE)
3622           .Case("got_ofst", MCSymbolRefExpr::VK_Mips_GOT_OFST)
3623           .Case("hi(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_HI)
3624           .Case("lo(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_LO)
3625           .Case("got_hi", MCSymbolRefExpr::VK_Mips_GOT_HI16)
3626           .Case("got_lo", MCSymbolRefExpr::VK_Mips_GOT_LO16)
3627           .Case("call_hi", MCSymbolRefExpr::VK_Mips_CALL_HI16)
3628           .Case("call_lo", MCSymbolRefExpr::VK_Mips_CALL_LO16)
3629           .Case("higher", MCSymbolRefExpr::VK_Mips_HIGHER)
3630           .Case("highest", MCSymbolRefExpr::VK_Mips_HIGHEST)
3631           .Case("pcrel_hi", MCSymbolRefExpr::VK_Mips_PCREL_HI16)
3632           .Case("pcrel_lo", MCSymbolRefExpr::VK_Mips_PCREL_LO16)
3633           .Default(MCSymbolRefExpr::VK_None);
3634
3635   assert(VK != MCSymbolRefExpr::VK_None);
3636
3637   return VK;
3638 }
3639
3640 /// Sometimes (i.e. load/stores) the operand may be followed immediately by
3641 /// either this.
3642 /// ::= '(', register, ')'
3643 /// handle it before we iterate so we don't get tripped up by the lack of
3644 /// a comma.
3645 bool MipsAsmParser::parseParenSuffix(StringRef Name, OperandVector &Operands) {
3646   MCAsmParser &Parser = getParser();
3647   if (getLexer().is(AsmToken::LParen)) {
3648     Operands.push_back(
3649         MipsOperand::CreateToken("(", getLexer().getLoc(), *this));
3650     Parser.Lex();
3651     if (parseOperand(Operands, Name)) {
3652       SMLoc Loc = getLexer().getLoc();
3653       Parser.eatToEndOfStatement();
3654       return Error(Loc, "unexpected token in argument list");
3655     }
3656     if (Parser.getTok().isNot(AsmToken::RParen)) {
3657       SMLoc Loc = getLexer().getLoc();
3658       Parser.eatToEndOfStatement();
3659       return Error(Loc, "unexpected token, expected ')'");
3660     }
3661     Operands.push_back(
3662         MipsOperand::CreateToken(")", getLexer().getLoc(), *this));
3663     Parser.Lex();
3664   }
3665   return false;
3666 }
3667
3668 /// Sometimes (i.e. in MSA) the operand may be followed immediately by
3669 /// either one of these.
3670 /// ::= '[', register, ']'
3671 /// ::= '[', integer, ']'
3672 /// handle it before we iterate so we don't get tripped up by the lack of
3673 /// a comma.
3674 bool MipsAsmParser::parseBracketSuffix(StringRef Name,
3675                                        OperandVector &Operands) {
3676   MCAsmParser &Parser = getParser();
3677   if (getLexer().is(AsmToken::LBrac)) {
3678     Operands.push_back(
3679         MipsOperand::CreateToken("[", getLexer().getLoc(), *this));
3680     Parser.Lex();
3681     if (parseOperand(Operands, Name)) {
3682       SMLoc Loc = getLexer().getLoc();
3683       Parser.eatToEndOfStatement();
3684       return Error(Loc, "unexpected token in argument list");
3685     }
3686     if (Parser.getTok().isNot(AsmToken::RBrac)) {
3687       SMLoc Loc = getLexer().getLoc();
3688       Parser.eatToEndOfStatement();
3689       return Error(Loc, "unexpected token, expected ']'");
3690     }
3691     Operands.push_back(
3692         MipsOperand::CreateToken("]", getLexer().getLoc(), *this));
3693     Parser.Lex();
3694   }
3695   return false;
3696 }
3697
3698 bool MipsAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
3699                                      SMLoc NameLoc, OperandVector &Operands) {
3700   MCAsmParser &Parser = getParser();
3701   DEBUG(dbgs() << "ParseInstruction\n");
3702
3703   // We have reached first instruction, module directive are now forbidden.
3704   getTargetStreamer().forbidModuleDirective();
3705
3706   // Check if we have valid mnemonic
3707   if (!mnemonicIsValid(Name, 0)) {
3708     Parser.eatToEndOfStatement();
3709     return Error(NameLoc, "unknown instruction");
3710   }
3711   // First operand in MCInst is instruction mnemonic.
3712   Operands.push_back(MipsOperand::CreateToken(Name, NameLoc, *this));
3713
3714   // Read the remaining operands.
3715   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3716     // Read the first operand.
3717     if (parseOperand(Operands, Name)) {
3718       SMLoc Loc = getLexer().getLoc();
3719       Parser.eatToEndOfStatement();
3720       return Error(Loc, "unexpected token in argument list");
3721     }
3722     if (getLexer().is(AsmToken::LBrac) && parseBracketSuffix(Name, Operands))
3723       return true;
3724     // AFAIK, parenthesis suffixes are never on the first operand
3725
3726     while (getLexer().is(AsmToken::Comma)) {
3727       Parser.Lex(); // Eat the comma.
3728       // Parse and remember the operand.
3729       if (parseOperand(Operands, Name)) {
3730         SMLoc Loc = getLexer().getLoc();
3731         Parser.eatToEndOfStatement();
3732         return Error(Loc, "unexpected token in argument list");
3733       }
3734       // Parse bracket and parenthesis suffixes before we iterate
3735       if (getLexer().is(AsmToken::LBrac)) {
3736         if (parseBracketSuffix(Name, Operands))
3737           return true;
3738       } else if (getLexer().is(AsmToken::LParen) &&
3739                  parseParenSuffix(Name, Operands))
3740         return true;
3741     }
3742   }
3743   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3744     SMLoc Loc = getLexer().getLoc();
3745     Parser.eatToEndOfStatement();
3746     return Error(Loc, "unexpected token in argument list");
3747   }
3748   Parser.Lex(); // Consume the EndOfStatement.
3749   return false;
3750 }
3751
3752 bool MipsAsmParser::reportParseError(Twine ErrorMsg) {
3753   MCAsmParser &Parser = getParser();
3754   SMLoc Loc = getLexer().getLoc();
3755   Parser.eatToEndOfStatement();
3756   return Error(Loc, ErrorMsg);
3757 }
3758
3759 bool MipsAsmParser::reportParseError(SMLoc Loc, Twine ErrorMsg) {
3760   return Error(Loc, ErrorMsg);
3761 }
3762
3763 bool MipsAsmParser::parseSetNoAtDirective() {
3764   MCAsmParser &Parser = getParser();
3765   // Line should look like: ".set noat".
3766
3767   // Set the $at register to $0.
3768   AssemblerOptions.back()->setATRegIndex(0);
3769
3770   Parser.Lex(); // Eat "noat".
3771
3772   // If this is not the end of the statement, report an error.
3773   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3774     reportParseError("unexpected token, expected end of statement");
3775     return false;
3776   }
3777
3778   getTargetStreamer().emitDirectiveSetNoAt();
3779   Parser.Lex(); // Consume the EndOfStatement.
3780   return false;
3781 }
3782
3783 bool MipsAsmParser::parseSetAtDirective() {
3784   // Line can be: ".set at", which sets $at to $1
3785   //          or  ".set at=$reg", which sets $at to $reg.
3786   MCAsmParser &Parser = getParser();
3787   Parser.Lex(); // Eat "at".
3788
3789   if (getLexer().is(AsmToken::EndOfStatement)) {
3790     // No register was specified, so we set $at to $1.
3791     AssemblerOptions.back()->setATRegIndex(1);
3792
3793     getTargetStreamer().emitDirectiveSetAt();
3794     Parser.Lex(); // Consume the EndOfStatement.
3795     return false;
3796   }
3797
3798   if (getLexer().isNot(AsmToken::Equal)) {
3799     reportParseError("unexpected token, expected equals sign");
3800     return false;
3801   }
3802   Parser.Lex(); // Eat "=".
3803
3804   if (getLexer().isNot(AsmToken::Dollar)) {
3805     if (getLexer().is(AsmToken::EndOfStatement)) {
3806       reportParseError("no register specified");
3807       return false;
3808     } else {
3809       reportParseError("unexpected token, expected dollar sign '$'");
3810       return false;
3811     }
3812   }
3813   Parser.Lex(); // Eat "$".
3814
3815   // Find out what "reg" is.
3816   unsigned AtRegNo;
3817   const AsmToken &Reg = Parser.getTok();
3818   if (Reg.is(AsmToken::Identifier)) {
3819     AtRegNo = matchCPURegisterName(Reg.getIdentifier());
3820   } else if (Reg.is(AsmToken::Integer)) {
3821     AtRegNo = Reg.getIntVal();
3822   } else {
3823     reportParseError("unexpected token, expected identifier or integer");
3824     return false;
3825   }
3826
3827   // Check if $reg is a valid register. If it is, set $at to $reg.
3828   if (!AssemblerOptions.back()->setATRegIndex(AtRegNo)) {
3829     reportParseError("invalid register");
3830     return false;
3831   }
3832   Parser.Lex(); // Eat "reg".
3833
3834   // If this is not the end of the statement, report an error.
3835   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3836     reportParseError("unexpected token, expected end of statement");
3837     return false;
3838   }
3839
3840   getTargetStreamer().emitDirectiveSetAtWithArg(AtRegNo);
3841
3842   Parser.Lex(); // Consume the EndOfStatement.
3843   return false;
3844 }
3845
3846 bool MipsAsmParser::parseSetReorderDirective() {
3847   MCAsmParser &Parser = getParser();
3848   Parser.Lex();
3849   // If this is not the end of the statement, report an error.
3850   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3851     reportParseError("unexpected token, expected end of statement");
3852     return false;
3853   }
3854   AssemblerOptions.back()->setReorder();
3855   getTargetStreamer().emitDirectiveSetReorder();
3856   Parser.Lex(); // Consume the EndOfStatement.
3857   return false;
3858 }
3859
3860 bool MipsAsmParser::parseSetNoReorderDirective() {
3861   MCAsmParser &Parser = getParser();
3862   Parser.Lex();
3863   // If this is not the end of the statement, report an error.
3864   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3865     reportParseError("unexpected token, expected end of statement");
3866     return false;
3867   }
3868   AssemblerOptions.back()->setNoReorder();
3869   getTargetStreamer().emitDirectiveSetNoReorder();
3870   Parser.Lex(); // Consume the EndOfStatement.
3871   return false;
3872 }
3873
3874 bool MipsAsmParser::parseSetMacroDirective() {
3875   MCAsmParser &Parser = getParser();
3876   Parser.Lex();
3877   // If this is not the end of the statement, report an error.
3878   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3879     reportParseError("unexpected token, expected end of statement");
3880     return false;
3881   }
3882   AssemblerOptions.back()->setMacro();
3883   getTargetStreamer().emitDirectiveSetMacro();
3884   Parser.Lex(); // Consume the EndOfStatement.
3885   return false;
3886 }
3887
3888 bool MipsAsmParser::parseSetNoMacroDirective() {
3889   MCAsmParser &Parser = getParser();
3890   Parser.Lex();
3891   // If this is not the end of the statement, report an error.
3892   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3893     reportParseError("unexpected token, expected end of statement");
3894     return false;
3895   }
3896   if (AssemblerOptions.back()->isReorder()) {
3897     reportParseError("`noreorder' must be set before `nomacro'");
3898     return false;
3899   }
3900   AssemblerOptions.back()->setNoMacro();
3901   getTargetStreamer().emitDirectiveSetNoMacro();
3902   Parser.Lex(); // Consume the EndOfStatement.
3903   return false;
3904 }
3905
3906 bool MipsAsmParser::parseSetMsaDirective() {
3907   MCAsmParser &Parser = getParser();
3908   Parser.Lex();
3909
3910   // If this is not the end of the statement, report an error.
3911   if (getLexer().isNot(AsmToken::EndOfStatement))
3912     return reportParseError("unexpected token, expected end of statement");
3913
3914   setFeatureBits(Mips::FeatureMSA, "msa");
3915   getTargetStreamer().emitDirectiveSetMsa();
3916   return false;
3917 }
3918
3919 bool MipsAsmParser::parseSetNoMsaDirective() {
3920   MCAsmParser &Parser = getParser();
3921   Parser.Lex();
3922
3923   // If this is not the end of the statement, report an error.
3924   if (getLexer().isNot(AsmToken::EndOfStatement))
3925     return reportParseError("unexpected token, expected end of statement");
3926
3927   clearFeatureBits(Mips::FeatureMSA, "msa");
3928   getTargetStreamer().emitDirectiveSetNoMsa();
3929   return false;
3930 }
3931
3932 bool MipsAsmParser::parseSetNoDspDirective() {
3933   MCAsmParser &Parser = getParser();
3934   Parser.Lex(); // Eat "nodsp".
3935
3936   // If this is not the end of the statement, report an error.
3937   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3938     reportParseError("unexpected token, expected end of statement");
3939     return false;
3940   }
3941
3942   clearFeatureBits(Mips::FeatureDSP, "dsp");
3943   getTargetStreamer().emitDirectiveSetNoDsp();
3944   return false;
3945 }
3946
3947 bool MipsAsmParser::parseSetMips16Directive() {
3948   MCAsmParser &Parser = getParser();
3949   Parser.Lex(); // Eat "mips16".
3950
3951   // If this is not the end of the statement, report an error.
3952   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3953     reportParseError("unexpected token, expected end of statement");
3954     return false;
3955   }
3956
3957   setFeatureBits(Mips::FeatureMips16, "mips16");
3958   getTargetStreamer().emitDirectiveSetMips16();
3959   Parser.Lex(); // Consume the EndOfStatement.
3960   return false;
3961 }
3962
3963 bool MipsAsmParser::parseSetNoMips16Directive() {
3964   MCAsmParser &Parser = getParser();
3965   Parser.Lex(); // Eat "nomips16".
3966
3967   // If this is not the end of the statement, report an error.
3968   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3969     reportParseError("unexpected token, expected end of statement");
3970     return false;
3971   }
3972
3973   clearFeatureBits(Mips::FeatureMips16, "mips16");
3974   getTargetStreamer().emitDirectiveSetNoMips16();
3975   Parser.Lex(); // Consume the EndOfStatement.
3976   return false;
3977 }
3978
3979 bool MipsAsmParser::parseSetFpDirective() {
3980   MCAsmParser &Parser = getParser();
3981   MipsABIFlagsSection::FpABIKind FpAbiVal;
3982   // Line can be: .set fp=32
3983   //              .set fp=xx
3984   //              .set fp=64
3985   Parser.Lex(); // Eat fp token
3986   AsmToken Tok = Parser.getTok();
3987   if (Tok.isNot(AsmToken::Equal)) {
3988     reportParseError("unexpected token, expected equals sign '='");
3989     return false;
3990   }
3991   Parser.Lex(); // Eat '=' token.
3992   Tok = Parser.getTok();
3993
3994   if (!parseFpABIValue(FpAbiVal, ".set"))
3995     return false;
3996
3997   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3998     reportParseError("unexpected token, expected end of statement");
3999     return false;
4000   }
4001   getTargetStreamer().emitDirectiveSetFp(FpAbiVal);
4002   Parser.Lex(); // Consume the EndOfStatement.
4003   return false;
4004 }
4005
4006 bool MipsAsmParser::parseSetPopDirective() {
4007   MCAsmParser &Parser = getParser();
4008   SMLoc Loc = getLexer().getLoc();
4009
4010   Parser.Lex();
4011   if (getLexer().isNot(AsmToken::EndOfStatement))
4012     return reportParseError("unexpected token, expected end of statement");
4013
4014   // Always keep an element on the options "stack" to prevent the user
4015   // from changing the initial options. This is how we remember them.
4016   if (AssemblerOptions.size() == 2)
4017     return reportParseError(Loc, ".set pop with no .set push");
4018
4019   AssemblerOptions.pop_back();
4020   setAvailableFeatures(
4021       ComputeAvailableFeatures(AssemblerOptions.back()->getFeatures()));
4022   STI.setFeatureBits(AssemblerOptions.back()->getFeatures());
4023
4024   getTargetStreamer().emitDirectiveSetPop();
4025   return false;
4026 }
4027
4028 bool MipsAsmParser::parseSetPushDirective() {
4029   MCAsmParser &Parser = getParser();
4030   Parser.Lex();
4031   if (getLexer().isNot(AsmToken::EndOfStatement))
4032     return reportParseError("unexpected token, expected end of statement");
4033
4034   // Create a copy of the current assembler options environment and push it.
4035   AssemblerOptions.push_back(
4036               make_unique<MipsAssemblerOptions>(AssemblerOptions.back().get()));
4037
4038   getTargetStreamer().emitDirectiveSetPush();
4039   return false;
4040 }
4041
4042 bool MipsAsmParser::parseSetSoftFloatDirective() {
4043   MCAsmParser &Parser = getParser();
4044   Parser.Lex();
4045   if (getLexer().isNot(AsmToken::EndOfStatement))
4046     return reportParseError("unexpected token, expected end of statement");
4047
4048   setFeatureBits(Mips::FeatureSoftFloat, "soft-float");
4049   getTargetStreamer().emitDirectiveSetSoftFloat();
4050   return false;
4051 }
4052
4053 bool MipsAsmParser::parseSetHardFloatDirective() {
4054   MCAsmParser &Parser = getParser();
4055   Parser.Lex();
4056   if (getLexer().isNot(AsmToken::EndOfStatement))
4057     return reportParseError("unexpected token, expected end of statement");
4058
4059   clearFeatureBits(Mips::FeatureSoftFloat, "soft-float");
4060   getTargetStreamer().emitDirectiveSetHardFloat();
4061   return false;
4062 }
4063
4064 bool MipsAsmParser::parseSetAssignment() {
4065   StringRef Name;
4066   const MCExpr *Value;
4067   MCAsmParser &Parser = getParser();
4068
4069   if (Parser.parseIdentifier(Name))
4070     reportParseError("expected identifier after .set");
4071
4072   if (getLexer().isNot(AsmToken::Comma))
4073     return reportParseError("unexpected token, expected comma");
4074   Lex(); // Eat comma
4075
4076   if (Parser.parseExpression(Value))
4077     return reportParseError("expected valid expression after comma");
4078
4079   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
4080   Sym->setVariableValue(Value);
4081
4082   return false;
4083 }
4084
4085 bool MipsAsmParser::parseSetMips0Directive() {
4086   MCAsmParser &Parser = getParser();
4087   Parser.Lex();
4088   if (getLexer().isNot(AsmToken::EndOfStatement))
4089     return reportParseError("unexpected token, expected end of statement");
4090
4091   // Reset assembler options to their initial values.
4092   setAvailableFeatures(
4093       ComputeAvailableFeatures(AssemblerOptions.front()->getFeatures()));
4094   STI.setFeatureBits(AssemblerOptions.front()->getFeatures());
4095   AssemblerOptions.back()->setFeatures(AssemblerOptions.front()->getFeatures());
4096
4097   getTargetStreamer().emitDirectiveSetMips0();
4098   return false;
4099 }
4100
4101 bool MipsAsmParser::parseSetArchDirective() {
4102   MCAsmParser &Parser = getParser();
4103   Parser.Lex();
4104   if (getLexer().isNot(AsmToken::Equal))
4105     return reportParseError("unexpected token, expected equals sign");
4106
4107   Parser.Lex();
4108   StringRef Arch;
4109   if (Parser.parseIdentifier(Arch))
4110     return reportParseError("expected arch identifier");
4111
4112   StringRef ArchFeatureName =
4113       StringSwitch<StringRef>(Arch)
4114           .Case("mips1", "mips1")
4115           .Case("mips2", "mips2")
4116           .Case("mips3", "mips3")
4117           .Case("mips4", "mips4")
4118           .Case("mips5", "mips5")
4119           .Case("mips32", "mips32")
4120           .Case("mips32r2", "mips32r2")
4121           .Case("mips32r3", "mips32r3")
4122           .Case("mips32r5", "mips32r5")
4123           .Case("mips32r6", "mips32r6")
4124           .Case("mips64", "mips64")
4125           .Case("mips64r2", "mips64r2")
4126           .Case("mips64r3", "mips64r3")
4127           .Case("mips64r5", "mips64r5")
4128           .Case("mips64r6", "mips64r6")
4129           .Case("cnmips", "cnmips")
4130           .Case("r4000", "mips3") // This is an implementation of Mips3.
4131           .Default("");
4132
4133   if (ArchFeatureName.empty())
4134     return reportParseError("unsupported architecture");
4135
4136   selectArch(ArchFeatureName);
4137   getTargetStreamer().emitDirectiveSetArch(Arch);
4138   return false;
4139 }
4140
4141 bool MipsAsmParser::parseSetFeature(uint64_t Feature) {
4142   MCAsmParser &Parser = getParser();
4143   Parser.Lex();
4144   if (getLexer().isNot(AsmToken::EndOfStatement))
4145     return reportParseError("unexpected token, expected end of statement");
4146
4147   switch (Feature) {
4148   default:
4149     llvm_unreachable("Unimplemented feature");
4150   case Mips::FeatureDSP:
4151     setFeatureBits(Mips::FeatureDSP, "dsp");
4152     getTargetStreamer().emitDirectiveSetDsp();
4153     break;
4154   case Mips::FeatureMicroMips:
4155     getTargetStreamer().emitDirectiveSetMicroMips();
4156     break;
4157   case Mips::FeatureMips1:
4158     selectArch("mips1");
4159     getTargetStreamer().emitDirectiveSetMips1();
4160     break;
4161   case Mips::FeatureMips2:
4162     selectArch("mips2");
4163     getTargetStreamer().emitDirectiveSetMips2();
4164     break;
4165   case Mips::FeatureMips3:
4166     selectArch("mips3");
4167     getTargetStreamer().emitDirectiveSetMips3();
4168     break;
4169   case Mips::FeatureMips4:
4170     selectArch("mips4");
4171     getTargetStreamer().emitDirectiveSetMips4();
4172     break;
4173   case Mips::FeatureMips5:
4174     selectArch("mips5");
4175     getTargetStreamer().emitDirectiveSetMips5();
4176     break;
4177   case Mips::FeatureMips32:
4178     selectArch("mips32");
4179     getTargetStreamer().emitDirectiveSetMips32();
4180     break;
4181   case Mips::FeatureMips32r2:
4182     selectArch("mips32r2");
4183     getTargetStreamer().emitDirectiveSetMips32R2();
4184     break;
4185   case Mips::FeatureMips32r3:
4186     selectArch("mips32r3");
4187     getTargetStreamer().emitDirectiveSetMips32R3();
4188     break;
4189   case Mips::FeatureMips32r5:
4190     selectArch("mips32r5");
4191     getTargetStreamer().emitDirectiveSetMips32R5();
4192     break;
4193   case Mips::FeatureMips32r6:
4194     selectArch("mips32r6");
4195     getTargetStreamer().emitDirectiveSetMips32R6();
4196     break;
4197   case Mips::FeatureMips64:
4198     selectArch("mips64");
4199     getTargetStreamer().emitDirectiveSetMips64();
4200     break;
4201   case Mips::FeatureMips64r2:
4202     selectArch("mips64r2");
4203     getTargetStreamer().emitDirectiveSetMips64R2();
4204     break;
4205   case Mips::FeatureMips64r3:
4206     selectArch("mips64r3");
4207     getTargetStreamer().emitDirectiveSetMips64R3();
4208     break;
4209   case Mips::FeatureMips64r5:
4210     selectArch("mips64r5");
4211     getTargetStreamer().emitDirectiveSetMips64R5();
4212     break;
4213   case Mips::FeatureMips64r6:
4214     selectArch("mips64r6");
4215     getTargetStreamer().emitDirectiveSetMips64R6();
4216     break;
4217   }
4218   return false;
4219 }
4220
4221 bool MipsAsmParser::eatComma(StringRef ErrorStr) {
4222   MCAsmParser &Parser = getParser();
4223   if (getLexer().isNot(AsmToken::Comma)) {
4224     SMLoc Loc = getLexer().getLoc();
4225     Parser.eatToEndOfStatement();
4226     return Error(Loc, ErrorStr);
4227   }
4228
4229   Parser.Lex(); // Eat the comma.
4230   return true;
4231 }
4232
4233 bool MipsAsmParser::parseDirectiveCpLoad(SMLoc Loc) {
4234   if (AssemblerOptions.back()->isReorder())
4235     Warning(Loc, ".cpload should be inside a noreorder section");
4236
4237   if (inMips16Mode()) {
4238     reportParseError(".cpload is not supported in Mips16 mode");
4239     return false;
4240   }
4241
4242   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Reg;
4243   OperandMatchResultTy ResTy = parseAnyRegister(Reg);
4244   if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
4245     reportParseError("expected register containing function address");
4246     return false;
4247   }
4248
4249   MipsOperand &RegOpnd = static_cast<MipsOperand &>(*Reg[0]);
4250   if (!RegOpnd.isGPRAsmReg()) {
4251     reportParseError(RegOpnd.getStartLoc(), "invalid register");
4252     return false;
4253   }
4254
4255   // If this is not the end of the statement, report an error.
4256   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4257     reportParseError("unexpected token, expected end of statement");
4258     return false;
4259   }
4260
4261   getTargetStreamer().emitDirectiveCpLoad(RegOpnd.getGPR32Reg());
4262   return false;
4263 }
4264
4265 bool MipsAsmParser::parseDirectiveCPSetup() {
4266   MCAsmParser &Parser = getParser();
4267   unsigned FuncReg;
4268   unsigned Save;
4269   bool SaveIsReg = true;
4270
4271   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> TmpReg;
4272   OperandMatchResultTy ResTy = parseAnyRegister(TmpReg);
4273   if (ResTy == MatchOperand_NoMatch) {
4274     reportParseError("expected register containing function address");
4275     Parser.eatToEndOfStatement();
4276     return false;
4277   }
4278
4279   MipsOperand &FuncRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
4280   if (!FuncRegOpnd.isGPRAsmReg()) {
4281     reportParseError(FuncRegOpnd.getStartLoc(), "invalid register");
4282     Parser.eatToEndOfStatement();
4283     return false;
4284   }
4285
4286   FuncReg = FuncRegOpnd.getGPR32Reg();
4287   TmpReg.clear();
4288
4289   if (!eatComma("unexpected token, expected comma"))
4290     return true;
4291
4292   ResTy = parseAnyRegister(TmpReg);
4293   if (ResTy == MatchOperand_NoMatch) {
4294     const AsmToken &Tok = Parser.getTok();
4295     if (Tok.is(AsmToken::Integer)) {
4296       Save = Tok.getIntVal();
4297       SaveIsReg = false;
4298       Parser.Lex();
4299     } else {
4300       reportParseError("expected save register or stack offset");
4301       Parser.eatToEndOfStatement();
4302       return false;
4303     }
4304   } else {
4305     MipsOperand &SaveOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
4306     if (!SaveOpnd.isGPRAsmReg()) {
4307       reportParseError(SaveOpnd.getStartLoc(), "invalid register");
4308       Parser.eatToEndOfStatement();
4309       return false;
4310     }
4311     Save = SaveOpnd.getGPR32Reg();
4312   }
4313
4314   if (!eatComma("unexpected token, expected comma"))
4315     return true;
4316
4317   const MCExpr *Expr;
4318   if (Parser.parseExpression(Expr)) {
4319     reportParseError("expected expression");
4320     return false;
4321   }
4322
4323   if (Expr->getKind() != MCExpr::SymbolRef) {
4324     reportParseError("expected symbol");
4325     return false;
4326   }
4327   const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
4328
4329   getTargetStreamer().emitDirectiveCpsetup(FuncReg, Save, Ref->getSymbol(),
4330                                            SaveIsReg);
4331   return false;
4332 }
4333
4334 bool MipsAsmParser::parseDirectiveNaN() {
4335   MCAsmParser &Parser = getParser();
4336   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4337     const AsmToken &Tok = Parser.getTok();
4338
4339     if (Tok.getString() == "2008") {
4340       Parser.Lex();
4341       getTargetStreamer().emitDirectiveNaN2008();
4342       return false;
4343     } else if (Tok.getString() == "legacy") {
4344       Parser.Lex();
4345       getTargetStreamer().emitDirectiveNaNLegacy();
4346       return false;
4347     }
4348   }
4349   // If we don't recognize the option passed to the .nan
4350   // directive (e.g. no option or unknown option), emit an error.
4351   reportParseError("invalid option in .nan directive");
4352   return false;
4353 }
4354
4355 bool MipsAsmParser::parseDirectiveSet() {
4356   MCAsmParser &Parser = getParser();
4357   // Get the next token.
4358   const AsmToken &Tok = Parser.getTok();
4359
4360   if (Tok.getString() == "noat") {
4361     return parseSetNoAtDirective();
4362   } else if (Tok.getString() == "at") {
4363     return parseSetAtDirective();
4364   } else if (Tok.getString() == "arch") {
4365     return parseSetArchDirective();
4366   } else if (Tok.getString() == "fp") {
4367     return parseSetFpDirective();
4368   } else if (Tok.getString() == "pop") {
4369     return parseSetPopDirective();
4370   } else if (Tok.getString() == "push") {
4371     return parseSetPushDirective();
4372   } else if (Tok.getString() == "reorder") {
4373     return parseSetReorderDirective();
4374   } else if (Tok.getString() == "noreorder") {
4375     return parseSetNoReorderDirective();
4376   } else if (Tok.getString() == "macro") {
4377     return parseSetMacroDirective();
4378   } else if (Tok.getString() == "nomacro") {
4379     return parseSetNoMacroDirective();
4380   } else if (Tok.getString() == "mips16") {
4381     return parseSetMips16Directive();
4382   } else if (Tok.getString() == "nomips16") {
4383     return parseSetNoMips16Directive();
4384   } else if (Tok.getString() == "nomicromips") {
4385     getTargetStreamer().emitDirectiveSetNoMicroMips();
4386     Parser.eatToEndOfStatement();
4387     return false;
4388   } else if (Tok.getString() == "micromips") {
4389     return parseSetFeature(Mips::FeatureMicroMips);
4390   } else if (Tok.getString() == "mips0") {
4391     return parseSetMips0Directive();
4392   } else if (Tok.getString() == "mips1") {
4393     return parseSetFeature(Mips::FeatureMips1);
4394   } else if (Tok.getString() == "mips2") {
4395     return parseSetFeature(Mips::FeatureMips2);
4396   } else if (Tok.getString() == "mips3") {
4397     return parseSetFeature(Mips::FeatureMips3);
4398   } else if (Tok.getString() == "mips4") {
4399     return parseSetFeature(Mips::FeatureMips4);
4400   } else if (Tok.getString() == "mips5") {
4401     return parseSetFeature(Mips::FeatureMips5);
4402   } else if (Tok.getString() == "mips32") {
4403     return parseSetFeature(Mips::FeatureMips32);
4404   } else if (Tok.getString() == "mips32r2") {
4405     return parseSetFeature(Mips::FeatureMips32r2);
4406   } else if (Tok.getString() == "mips32r3") {
4407     return parseSetFeature(Mips::FeatureMips32r3);
4408   } else if (Tok.getString() == "mips32r5") {
4409     return parseSetFeature(Mips::FeatureMips32r5);
4410   } else if (Tok.getString() == "mips32r6") {
4411     return parseSetFeature(Mips::FeatureMips32r6);
4412   } else if (Tok.getString() == "mips64") {
4413     return parseSetFeature(Mips::FeatureMips64);
4414   } else if (Tok.getString() == "mips64r2") {
4415     return parseSetFeature(Mips::FeatureMips64r2);
4416   } else if (Tok.getString() == "mips64r3") {
4417     return parseSetFeature(Mips::FeatureMips64r3);
4418   } else if (Tok.getString() == "mips64r5") {
4419     return parseSetFeature(Mips::FeatureMips64r5);
4420   } else if (Tok.getString() == "mips64r6") {
4421     return parseSetFeature(Mips::FeatureMips64r6);
4422   } else if (Tok.getString() == "dsp") {
4423     return parseSetFeature(Mips::FeatureDSP);
4424   } else if (Tok.getString() == "nodsp") {
4425     return parseSetNoDspDirective();
4426   } else if (Tok.getString() == "msa") {
4427     return parseSetMsaDirective();
4428   } else if (Tok.getString() == "nomsa") {
4429     return parseSetNoMsaDirective();
4430   } else if (Tok.getString() == "softfloat") {
4431     return parseSetSoftFloatDirective();
4432   } else if (Tok.getString() == "hardfloat") {
4433     return parseSetHardFloatDirective();
4434   } else {
4435     // It is just an identifier, look for an assignment.
4436     parseSetAssignment();
4437     return false;
4438   }
4439
4440   return true;
4441 }
4442
4443 /// parseDataDirective
4444 ///  ::= .word [ expression (, expression)* ]
4445 bool MipsAsmParser::parseDataDirective(unsigned Size, SMLoc L) {
4446   MCAsmParser &Parser = getParser();
4447   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4448     for (;;) {
4449       const MCExpr *Value;
4450       if (getParser().parseExpression(Value))
4451         return true;
4452
4453       getParser().getStreamer().EmitValue(Value, Size);
4454
4455       if (getLexer().is(AsmToken::EndOfStatement))
4456         break;
4457
4458       if (getLexer().isNot(AsmToken::Comma))
4459         return Error(L, "unexpected token, expected comma");
4460       Parser.Lex();
4461     }
4462   }
4463
4464   Parser.Lex();
4465   return false;
4466 }
4467
4468 /// parseDirectiveGpWord
4469 ///  ::= .gpword local_sym
4470 bool MipsAsmParser::parseDirectiveGpWord() {
4471   MCAsmParser &Parser = getParser();
4472   const MCExpr *Value;
4473   // EmitGPRel32Value requires an expression, so we are using base class
4474   // method to evaluate the expression.
4475   if (getParser().parseExpression(Value))
4476     return true;
4477   getParser().getStreamer().EmitGPRel32Value(Value);
4478
4479   if (getLexer().isNot(AsmToken::EndOfStatement))
4480     return Error(getLexer().getLoc(), 
4481                 "unexpected token, expected end of statement");
4482   Parser.Lex(); // Eat EndOfStatement token.
4483   return false;
4484 }
4485
4486 /// parseDirectiveGpDWord
4487 ///  ::= .gpdword local_sym
4488 bool MipsAsmParser::parseDirectiveGpDWord() {
4489   MCAsmParser &Parser = getParser();
4490   const MCExpr *Value;
4491   // EmitGPRel64Value requires an expression, so we are using base class
4492   // method to evaluate the expression.
4493   if (getParser().parseExpression(Value))
4494     return true;
4495   getParser().getStreamer().EmitGPRel64Value(Value);
4496
4497   if (getLexer().isNot(AsmToken::EndOfStatement))
4498     return Error(getLexer().getLoc(), 
4499                 "unexpected token, expected end of statement");
4500   Parser.Lex(); // Eat EndOfStatement token.
4501   return false;
4502 }
4503
4504 bool MipsAsmParser::parseDirectiveOption() {
4505   MCAsmParser &Parser = getParser();
4506   // Get the option token.
4507   AsmToken Tok = Parser.getTok();
4508   // At the moment only identifiers are supported.
4509   if (Tok.isNot(AsmToken::Identifier)) {
4510     Error(Parser.getTok().getLoc(), "unexpected token, expected identifier");
4511     Parser.eatToEndOfStatement();
4512     return false;
4513   }
4514
4515   StringRef Option = Tok.getIdentifier();
4516
4517   if (Option == "pic0") {
4518     getTargetStreamer().emitDirectiveOptionPic0();
4519     Parser.Lex();
4520     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
4521       Error(Parser.getTok().getLoc(),
4522             "unexpected token, expected end of statement");
4523       Parser.eatToEndOfStatement();
4524     }
4525     return false;
4526   }
4527
4528   if (Option == "pic2") {
4529     getTargetStreamer().emitDirectiveOptionPic2();
4530     Parser.Lex();
4531     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
4532       Error(Parser.getTok().getLoc(),
4533             "unexpected token, expected end of statement");
4534       Parser.eatToEndOfStatement();
4535     }
4536     return false;
4537   }
4538
4539   // Unknown option.
4540   Warning(Parser.getTok().getLoc(), 
4541           "unknown option, expected 'pic0' or 'pic2'");
4542   Parser.eatToEndOfStatement();
4543   return false;
4544 }
4545
4546 /// parseInsnDirective
4547 ///  ::= .insn
4548 bool MipsAsmParser::parseInsnDirective() {
4549   // If this is not the end of the statement, report an error.
4550   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4551     reportParseError("unexpected token, expected end of statement");
4552     return false;
4553   }
4554
4555   // The actual label marking happens in
4556   // MipsELFStreamer::createPendingLabelRelocs().
4557   getTargetStreamer().emitDirectiveInsn();
4558
4559   getParser().Lex(); // Eat EndOfStatement token.
4560   return false;
4561 }
4562
4563 /// parseDirectiveModule
4564 ///  ::= .module oddspreg
4565 ///  ::= .module nooddspreg
4566 ///  ::= .module fp=value
4567 bool MipsAsmParser::parseDirectiveModule() {
4568   MCAsmParser &Parser = getParser();
4569   MCAsmLexer &Lexer = getLexer();
4570   SMLoc L = Lexer.getLoc();
4571
4572   if (!getTargetStreamer().isModuleDirectiveAllowed()) {
4573     // TODO : get a better message.
4574     reportParseError(".module directive must appear before any code");
4575     return false;
4576   }
4577
4578   StringRef Option;
4579   if (Parser.parseIdentifier(Option)) {
4580     reportParseError("expected .module option identifier");
4581     return false;
4582   }
4583
4584   if (Option == "oddspreg") {
4585     getTargetStreamer().emitDirectiveModuleOddSPReg(true, isABI_O32());
4586     clearFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
4587
4588     // If this is not the end of the statement, report an error.
4589     if (getLexer().isNot(AsmToken::EndOfStatement)) {
4590       reportParseError("unexpected token, expected end of statement");
4591       return false;
4592     }
4593
4594     return false; // parseDirectiveModule has finished successfully.
4595   } else if (Option == "nooddspreg") {
4596     if (!isABI_O32()) {
4597       Error(L, "'.module nooddspreg' requires the O32 ABI");
4598       return false;
4599     }
4600
4601     getTargetStreamer().emitDirectiveModuleOddSPReg(false, isABI_O32());
4602     setFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
4603
4604     // If this is not the end of the statement, report an error.
4605     if (getLexer().isNot(AsmToken::EndOfStatement)) {
4606       reportParseError("unexpected token, expected end of statement");
4607       return false;
4608     }
4609
4610     return false; // parseDirectiveModule has finished successfully.
4611   } else if (Option == "fp") {
4612     return parseDirectiveModuleFP();
4613   } else {
4614     return Error(L, "'" + Twine(Option) + "' is not a valid .module option.");
4615   }
4616 }
4617
4618 /// parseDirectiveModuleFP
4619 ///  ::= =32
4620 ///  ::= =xx
4621 ///  ::= =64
4622 bool MipsAsmParser::parseDirectiveModuleFP() {
4623   MCAsmParser &Parser = getParser();
4624   MCAsmLexer &Lexer = getLexer();
4625
4626   if (Lexer.isNot(AsmToken::Equal)) {
4627     reportParseError("unexpected token, expected equals sign '='");
4628     return false;
4629   }
4630   Parser.Lex(); // Eat '=' token.
4631
4632   MipsABIFlagsSection::FpABIKind FpABI;
4633   if (!parseFpABIValue(FpABI, ".module"))
4634     return false;
4635
4636   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4637     reportParseError("unexpected token, expected end of statement");
4638     return false;
4639   }
4640
4641   // Emit appropriate flags.
4642   getTargetStreamer().emitDirectiveModuleFP(FpABI, isABI_O32());
4643   Parser.Lex(); // Consume the EndOfStatement.
4644   return false;
4645 }
4646
4647 bool MipsAsmParser::parseFpABIValue(MipsABIFlagsSection::FpABIKind &FpABI,
4648                                     StringRef Directive) {
4649   MCAsmParser &Parser = getParser();
4650   MCAsmLexer &Lexer = getLexer();
4651
4652   if (Lexer.is(AsmToken::Identifier)) {
4653     StringRef Value = Parser.getTok().getString();
4654     Parser.Lex();
4655
4656     if (Value != "xx") {
4657       reportParseError("unsupported value, expected 'xx', '32' or '64'");
4658       return false;
4659     }
4660
4661     if (!isABI_O32()) {
4662       reportParseError("'" + Directive + " fp=xx' requires the O32 ABI");
4663       return false;
4664     }
4665
4666     FpABI = MipsABIFlagsSection::FpABIKind::XX;
4667     return true;
4668   }
4669
4670   if (Lexer.is(AsmToken::Integer)) {
4671     unsigned Value = Parser.getTok().getIntVal();
4672     Parser.Lex();
4673
4674     if (Value != 32 && Value != 64) {
4675       reportParseError("unsupported value, expected 'xx', '32' or '64'");
4676       return false;
4677     }
4678
4679     if (Value == 32) {
4680       if (!isABI_O32()) {
4681         reportParseError("'" + Directive + " fp=32' requires the O32 ABI");
4682         return false;
4683       }
4684
4685       FpABI = MipsABIFlagsSection::FpABIKind::S32;
4686     } else
4687       FpABI = MipsABIFlagsSection::FpABIKind::S64;
4688
4689     return true;
4690   }
4691
4692   return false;
4693 }
4694
4695 bool MipsAsmParser::ParseDirective(AsmToken DirectiveID) {
4696   MCAsmParser &Parser = getParser();
4697   StringRef IDVal = DirectiveID.getString();
4698
4699   if (IDVal == ".cpload")
4700     return parseDirectiveCpLoad(DirectiveID.getLoc());
4701   if (IDVal == ".dword") {
4702     parseDataDirective(8, DirectiveID.getLoc());
4703     return false;
4704   }
4705   if (IDVal == ".ent") {
4706     StringRef SymbolName;
4707
4708     if (Parser.parseIdentifier(SymbolName)) {
4709       reportParseError("expected identifier after .ent");
4710       return false;
4711     }
4712
4713     // There's an undocumented extension that allows an integer to
4714     // follow the name of the procedure which AFAICS is ignored by GAS.
4715     // Example: .ent foo,2
4716     if (getLexer().isNot(AsmToken::EndOfStatement)) {
4717       if (getLexer().isNot(AsmToken::Comma)) {
4718         // Even though we accept this undocumented extension for compatibility
4719         // reasons, the additional integer argument does not actually change
4720         // the behaviour of the '.ent' directive, so we would like to discourage
4721         // its use. We do this by not referring to the extended version in
4722         // error messages which are not directly related to its use.
4723         reportParseError("unexpected token, expected end of statement");
4724         return false;
4725       }
4726       Parser.Lex(); // Eat the comma.
4727       const MCExpr *DummyNumber;
4728       int64_t DummyNumberVal;
4729       // If the user was explicitly trying to use the extended version,
4730       // we still give helpful extension-related error messages.
4731       if (Parser.parseExpression(DummyNumber)) {
4732         reportParseError("expected number after comma");
4733         return false;
4734       }
4735       if (!DummyNumber->evaluateAsAbsolute(DummyNumberVal)) {
4736         reportParseError("expected an absolute expression after comma");
4737         return false;
4738       }
4739     }
4740
4741     // If this is not the end of the statement, report an error.
4742     if (getLexer().isNot(AsmToken::EndOfStatement)) {
4743       reportParseError("unexpected token, expected end of statement");
4744       return false;
4745     }
4746
4747     MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
4748
4749     getTargetStreamer().emitDirectiveEnt(*Sym);
4750     CurrentFn = Sym;
4751     return false;
4752   }
4753
4754   if (IDVal == ".end") {
4755     StringRef SymbolName;
4756
4757     if (Parser.parseIdentifier(SymbolName)) {
4758       reportParseError("expected identifier after .end");
4759       return false;
4760     }
4761
4762     if (getLexer().isNot(AsmToken::EndOfStatement)) {
4763       reportParseError("unexpected token, expected end of statement");
4764       return false;
4765     }
4766
4767     if (CurrentFn == nullptr) {
4768       reportParseError(".end used without .ent");
4769       return false;
4770     }
4771
4772     if ((SymbolName != CurrentFn->getName())) {
4773       reportParseError(".end symbol does not match .ent symbol");
4774       return false;
4775     }
4776
4777     getTargetStreamer().emitDirectiveEnd(SymbolName);
4778     CurrentFn = nullptr;
4779     return false;
4780   }
4781
4782   if (IDVal == ".frame") {
4783     // .frame $stack_reg, frame_size_in_bytes, $return_reg
4784     SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> TmpReg;
4785     OperandMatchResultTy ResTy = parseAnyRegister(TmpReg);
4786     if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
4787       reportParseError("expected stack register");
4788       return false;
4789     }
4790
4791     MipsOperand &StackRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
4792     if (!StackRegOpnd.isGPRAsmReg()) {
4793       reportParseError(StackRegOpnd.getStartLoc(),
4794                        "expected general purpose register");
4795       return false;
4796     }
4797     unsigned StackReg = StackRegOpnd.getGPR32Reg();
4798
4799     if (Parser.getTok().is(AsmToken::Comma))
4800       Parser.Lex();
4801     else {
4802       reportParseError("unexpected token, expected comma");
4803       return false;
4804     }
4805
4806     // Parse the frame size.
4807     const MCExpr *FrameSize;
4808     int64_t FrameSizeVal;
4809
4810     if (Parser.parseExpression(FrameSize)) {
4811       reportParseError("expected frame size value");
4812       return false;
4813     }
4814
4815     if (!FrameSize->evaluateAsAbsolute(FrameSizeVal)) {
4816       reportParseError("frame size not an absolute expression");
4817       return false;
4818     }
4819
4820     if (Parser.getTok().is(AsmToken::Comma))
4821       Parser.Lex();
4822     else {
4823       reportParseError("unexpected token, expected comma");
4824       return false;
4825     }
4826
4827     // Parse the return register.
4828     TmpReg.clear();
4829     ResTy = parseAnyRegister(TmpReg);
4830     if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
4831       reportParseError("expected return register");
4832       return false;
4833     }
4834
4835     MipsOperand &ReturnRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
4836     if (!ReturnRegOpnd.isGPRAsmReg()) {
4837       reportParseError(ReturnRegOpnd.getStartLoc(),
4838                        "expected general purpose register");
4839       return false;
4840     }
4841
4842     // If this is not the end of the statement, report an error.
4843     if (getLexer().isNot(AsmToken::EndOfStatement)) {
4844       reportParseError("unexpected token, expected end of statement");
4845       return false;
4846     }
4847
4848     getTargetStreamer().emitFrame(StackReg, FrameSizeVal,
4849                                   ReturnRegOpnd.getGPR32Reg());
4850     return false;
4851   }
4852
4853   if (IDVal == ".set") {
4854     return parseDirectiveSet();
4855   }
4856
4857   if (IDVal == ".mask" || IDVal == ".fmask") {
4858     // .mask bitmask, frame_offset
4859     // bitmask: One bit for each register used.
4860     // frame_offset: Offset from Canonical Frame Address ($sp on entry) where
4861     //               first register is expected to be saved.
4862     // Examples:
4863     //   .mask 0x80000000, -4
4864     //   .fmask 0x80000000, -4
4865     //
4866
4867     // Parse the bitmask
4868     const MCExpr *BitMask;
4869     int64_t BitMaskVal;
4870
4871     if (Parser.parseExpression(BitMask)) {
4872       reportParseError("expected bitmask value");
4873       return false;
4874     }
4875
4876     if (!BitMask->evaluateAsAbsolute(BitMaskVal)) {
4877       reportParseError("bitmask not an absolute expression");
4878       return false;
4879     }
4880
4881     if (Parser.getTok().is(AsmToken::Comma))
4882       Parser.Lex();
4883     else {
4884       reportParseError("unexpected token, expected comma");
4885       return false;
4886     }
4887
4888     // Parse the frame_offset
4889     const MCExpr *FrameOffset;
4890     int64_t FrameOffsetVal;
4891
4892     if (Parser.parseExpression(FrameOffset)) {
4893       reportParseError("expected frame offset value");
4894       return false;
4895     }
4896
4897     if (!FrameOffset->evaluateAsAbsolute(FrameOffsetVal)) {
4898       reportParseError("frame offset not an absolute expression");
4899       return false;
4900     }
4901
4902     // If this is not the end of the statement, report an error.
4903     if (getLexer().isNot(AsmToken::EndOfStatement)) {
4904       reportParseError("unexpected token, expected end of statement");
4905       return false;
4906     }
4907
4908     if (IDVal == ".mask")
4909       getTargetStreamer().emitMask(BitMaskVal, FrameOffsetVal);
4910     else
4911       getTargetStreamer().emitFMask(BitMaskVal, FrameOffsetVal);
4912     return false;
4913   }
4914
4915   if (IDVal == ".nan")
4916     return parseDirectiveNaN();
4917
4918   if (IDVal == ".gpword") {
4919     parseDirectiveGpWord();
4920     return false;
4921   }
4922
4923   if (IDVal == ".gpdword") {
4924     parseDirectiveGpDWord();
4925     return false;
4926   }
4927
4928   if (IDVal == ".word") {
4929     parseDataDirective(4, DirectiveID.getLoc());
4930     return false;
4931   }
4932
4933   if (IDVal == ".option")
4934     return parseDirectiveOption();
4935
4936   if (IDVal == ".abicalls") {
4937     getTargetStreamer().emitDirectiveAbiCalls();
4938     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
4939       Error(Parser.getTok().getLoc(), 
4940             "unexpected token, expected end of statement");
4941       // Clear line
4942       Parser.eatToEndOfStatement();
4943     }
4944     return false;
4945   }
4946
4947   if (IDVal == ".cpsetup")
4948     return parseDirectiveCPSetup();
4949
4950   if (IDVal == ".module")
4951     return parseDirectiveModule();
4952
4953   if (IDVal == ".llvm_internal_mips_reallow_module_directive")
4954     return parseInternalDirectiveReallowModule();
4955
4956   if (IDVal == ".insn")
4957     return parseInsnDirective();
4958
4959   return true;
4960 }
4961
4962 bool MipsAsmParser::parseInternalDirectiveReallowModule() {
4963   // If this is not the end of the statement, report an error.
4964   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4965     reportParseError("unexpected token, expected end of statement");
4966     return false;
4967   }
4968
4969   getTargetStreamer().reallowModuleDirective();
4970
4971   getParser().Lex(); // Eat EndOfStatement token.
4972   return false;
4973 }
4974
4975 extern "C" void LLVMInitializeMipsAsmParser() {
4976   RegisterMCAsmParser<MipsAsmParser> X(TheMipsTarget);
4977   RegisterMCAsmParser<MipsAsmParser> Y(TheMipselTarget);
4978   RegisterMCAsmParser<MipsAsmParser> A(TheMips64Target);
4979   RegisterMCAsmParser<MipsAsmParser> B(TheMips64elTarget);
4980 }
4981
4982 #define GET_REGISTER_MATCHER
4983 #define GET_MATCHER_IMPLEMENTATION
4984 #include "MipsGenAsmMatcher.inc"