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