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