[mips] [IAS] Implement the .asciiz directive.
[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->getATRegNum();
51     Reorder = Opts->isReorder();
52     Macro = Opts->isMacro();
53     Features = Opts->getFeatures();
54   }
55
56   unsigned getATRegNum() const { return ATReg; }
57   bool setATReg(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 RegNo is the current assembler temporary.
440   void warnIfAssemblerTemporary(int RegNo, 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.warnIfAssemblerTemporary(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 this instruction has a delay slot and .set reorder is active,
1387   // emit a NOP after it.
1388   if (MCID.hasDelaySlot() && AssemblerOptions.back()->isReorder()) {
1389     Instructions.push_back(Inst);
1390     createNop(hasShortDelaySlot(Inst.getOpcode()), IDLoc, Instructions);
1391     return false;
1392   }
1393
1394   if (MCID.mayLoad() || MCID.mayStore()) {
1395     // Check the offset of memory operand, if it is a symbol
1396     // reference or immediate we may have to expand instructions.
1397     for (unsigned i = 0; i < MCID.getNumOperands(); i++) {
1398       const MCOperandInfo &OpInfo = MCID.OpInfo[i];
1399       if ((OpInfo.OperandType == MCOI::OPERAND_MEMORY) ||
1400           (OpInfo.OperandType == MCOI::OPERAND_UNKNOWN)) {
1401         MCOperand &Op = Inst.getOperand(i);
1402         if (Op.isImm()) {
1403           int MemOffset = Op.getImm();
1404           if (MemOffset < -32768 || MemOffset > 32767) {
1405             // Offset can't exceed 16bit value.
1406             expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), true);
1407             return false;
1408           }
1409         } else if (Op.isExpr()) {
1410           const MCExpr *Expr = Op.getExpr();
1411           if (Expr->getKind() == MCExpr::SymbolRef) {
1412             const MCSymbolRefExpr *SR =
1413                 static_cast<const MCSymbolRefExpr *>(Expr);
1414             if (SR->getKind() == MCSymbolRefExpr::VK_None) {
1415               // Expand symbol.
1416               expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), false);
1417               return false;
1418             }
1419           } else if (!isEvaluated(Expr)) {
1420             expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), false);
1421             return false;
1422           }
1423         }
1424       }
1425     } // for
1426   }   // if load/store
1427
1428   if (inMicroMipsMode()) {
1429     if (MCID.mayLoad()) {
1430       // Try to create 16-bit GP relative load instruction.
1431       for (unsigned i = 0; i < MCID.getNumOperands(); i++) {
1432         const MCOperandInfo &OpInfo = MCID.OpInfo[i];
1433         if ((OpInfo.OperandType == MCOI::OPERAND_MEMORY) ||
1434             (OpInfo.OperandType == MCOI::OPERAND_UNKNOWN)) {
1435           MCOperand &Op = Inst.getOperand(i);
1436           if (Op.isImm()) {
1437             int MemOffset = Op.getImm();
1438             MCOperand &DstReg = Inst.getOperand(0);
1439             MCOperand &BaseReg = Inst.getOperand(1);
1440             if (isIntN(9, MemOffset) && (MemOffset % 4 == 0) &&
1441                 getContext().getRegisterInfo()->getRegClass(
1442                   Mips::GPRMM16RegClassID).contains(DstReg.getReg()) &&
1443                 BaseReg.getReg() == Mips::GP) {
1444               MCInst TmpInst;
1445               TmpInst.setLoc(IDLoc);
1446               TmpInst.setOpcode(Mips::LWGP_MM);
1447               TmpInst.addOperand(MCOperand::CreateReg(DstReg.getReg()));
1448               TmpInst.addOperand(MCOperand::CreateReg(Mips::GP));
1449               TmpInst.addOperand(MCOperand::CreateImm(MemOffset));
1450               Instructions.push_back(TmpInst);
1451               return false;
1452             }
1453           }
1454         }
1455       } // for
1456     }   // if load
1457
1458     // TODO: Handle this with the AsmOperandClass.PredicateMethod.
1459
1460     MCOperand Opnd;
1461     int Imm;
1462
1463     switch (Inst.getOpcode()) {
1464       default:
1465         break;
1466       case Mips::ADDIUS5_MM:
1467         Opnd = Inst.getOperand(2);
1468         if (!Opnd.isImm())
1469           return Error(IDLoc, "expected immediate operand kind");
1470         Imm = Opnd.getImm();
1471         if (Imm < -8 || Imm > 7)
1472           return Error(IDLoc, "immediate operand value out of range");
1473         break;
1474       case Mips::ADDIUSP_MM:
1475         Opnd = Inst.getOperand(0);
1476         if (!Opnd.isImm())
1477           return Error(IDLoc, "expected immediate operand kind");
1478         Imm = Opnd.getImm();
1479         if (Imm < -1032 || Imm > 1028 || (Imm < 8 && Imm > -12) ||
1480             Imm % 4 != 0)
1481           return Error(IDLoc, "immediate operand value out of range");
1482         break;
1483       case Mips::SLL16_MM:
1484       case Mips::SRL16_MM:
1485         Opnd = Inst.getOperand(2);
1486         if (!Opnd.isImm())
1487           return Error(IDLoc, "expected immediate operand kind");
1488         Imm = Opnd.getImm();
1489         if (Imm < 1 || Imm > 8)
1490           return Error(IDLoc, "immediate operand value out of range");
1491         break;
1492       case Mips::LI16_MM:
1493         Opnd = Inst.getOperand(1);
1494         if (!Opnd.isImm())
1495           return Error(IDLoc, "expected immediate operand kind");
1496         Imm = Opnd.getImm();
1497         if (Imm < -1 || Imm > 126)
1498           return Error(IDLoc, "immediate operand value out of range");
1499         break;
1500       case Mips::ADDIUR2_MM:
1501         Opnd = Inst.getOperand(2);
1502         if (!Opnd.isImm())
1503           return Error(IDLoc, "expected immediate operand kind");
1504         Imm = Opnd.getImm();
1505         if (!(Imm == 1 || Imm == -1 ||
1506               ((Imm % 4 == 0) && Imm < 28 && Imm > 0)))
1507           return Error(IDLoc, "immediate operand value out of range");
1508         break;
1509       case Mips::ADDIUR1SP_MM:
1510         Opnd = Inst.getOperand(1);
1511         if (!Opnd.isImm())
1512           return Error(IDLoc, "expected immediate operand kind");
1513         Imm = Opnd.getImm();
1514         if (OffsetToAlignment(Imm, 4LL))
1515           return Error(IDLoc, "misaligned immediate operand value");
1516         if (Imm < 0 || Imm > 255)
1517           return Error(IDLoc, "immediate operand value out of range");
1518         break;
1519       case Mips::ANDI16_MM:
1520         Opnd = Inst.getOperand(2);
1521         if (!Opnd.isImm())
1522           return Error(IDLoc, "expected immediate operand kind");
1523         Imm = Opnd.getImm();
1524         if (!(Imm == 128 || (Imm >= 1 && Imm <= 4) || Imm == 7 || Imm == 8 ||
1525               Imm == 15 || Imm == 16 || Imm == 31 || Imm == 32 || Imm == 63 ||
1526               Imm == 64 || Imm == 255 || Imm == 32768 || Imm == 65535))
1527           return Error(IDLoc, "immediate operand value out of range");
1528         break;
1529       case Mips::LBU16_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 < -1 || Imm > 14)
1535           return Error(IDLoc, "immediate operand value out of range");
1536         break;
1537       case Mips::SB16_MM:
1538         Opnd = Inst.getOperand(2);
1539         if (!Opnd.isImm())
1540           return Error(IDLoc, "expected immediate operand kind");
1541         Imm = Opnd.getImm();
1542         if (Imm < 0 || Imm > 15)
1543           return Error(IDLoc, "immediate operand value out of range");
1544         break;
1545       case Mips::LHU16_MM:
1546       case Mips::SH16_MM:
1547         Opnd = Inst.getOperand(2);
1548         if (!Opnd.isImm())
1549           return Error(IDLoc, "expected immediate operand kind");
1550         Imm = Opnd.getImm();
1551         if (Imm < 0 || Imm > 30 || (Imm % 2 != 0))
1552           return Error(IDLoc, "immediate operand value out of range");
1553         break;
1554       case Mips::LW16_MM:
1555       case Mips::SW16_MM:
1556         Opnd = Inst.getOperand(2);
1557         if (!Opnd.isImm())
1558           return Error(IDLoc, "expected immediate operand kind");
1559         Imm = Opnd.getImm();
1560         if (Imm < 0 || Imm > 60 || (Imm % 4 != 0))
1561           return Error(IDLoc, "immediate operand value out of range");
1562         break;
1563       case Mips::CACHE:
1564       case Mips::PREF:
1565         Opnd = Inst.getOperand(2);
1566         if (!Opnd.isImm())
1567           return Error(IDLoc, "expected immediate operand kind");
1568         Imm = Opnd.getImm();
1569         if (!isUInt<5>(Imm))
1570           return Error(IDLoc, "immediate operand value out of range");
1571         break;
1572       case Mips::ADDIUPC_MM:
1573         MCOperand Opnd = Inst.getOperand(1);
1574         if (!Opnd.isImm())
1575           return Error(IDLoc, "expected immediate operand kind");
1576         int Imm = Opnd.getImm();
1577         if ((Imm % 4 != 0) || !isIntN(25, Imm))
1578           return Error(IDLoc, "immediate operand value out of range");
1579         break;
1580     }
1581   }
1582
1583   if (needsExpansion(Inst))
1584     return expandInstruction(Inst, IDLoc, Instructions);
1585   else
1586     Instructions.push_back(Inst);
1587
1588   return false;
1589 }
1590
1591 bool MipsAsmParser::needsExpansion(MCInst &Inst) {
1592
1593   switch (Inst.getOpcode()) {
1594   case Mips::LoadImm32:
1595   case Mips::LoadImm64:
1596   case Mips::LoadAddrImm32:
1597   case Mips::LoadAddrReg32:
1598   case Mips::B_MM_Pseudo:
1599   case Mips::LWM_MM:
1600   case Mips::SWM_MM:
1601   case Mips::JalOneReg:
1602   case Mips::JalTwoReg:
1603     return true;
1604   default:
1605     return false;
1606   }
1607 }
1608
1609 bool MipsAsmParser::expandInstruction(MCInst &Inst, SMLoc IDLoc,
1610                                       SmallVectorImpl<MCInst> &Instructions) {
1611   switch (Inst.getOpcode()) {
1612   default: llvm_unreachable("unimplemented expansion");
1613   case Mips::LoadImm32:
1614     return expandLoadImm(Inst, IDLoc, Instructions);
1615   case Mips::LoadImm64:
1616     if (!isGP64bit()) {
1617       Error(IDLoc, "instruction requires a 64-bit architecture");
1618       return true;
1619     }
1620     return expandLoadImm(Inst, IDLoc, Instructions);
1621   case Mips::LoadAddrImm32:
1622     return expandLoadAddressImm(Inst, IDLoc, Instructions);
1623   case Mips::LoadAddrReg32:
1624     return expandLoadAddressReg(Inst, IDLoc, Instructions);
1625   case Mips::B_MM_Pseudo:
1626     return expandUncondBranchMMPseudo(Inst, IDLoc, Instructions);
1627   case Mips::SWM_MM:
1628   case Mips::LWM_MM:
1629     return expandLoadStoreMultiple(Inst, IDLoc, Instructions);
1630   case Mips::JalOneReg:
1631   case Mips::JalTwoReg:
1632     return expandJalWithRegs(Inst, IDLoc, Instructions);
1633   }
1634 }
1635
1636 namespace {
1637 template <bool PerformShift>
1638 void createShiftOr(MCOperand Operand, unsigned RegNo, SMLoc IDLoc,
1639                    SmallVectorImpl<MCInst> &Instructions) {
1640   MCInst tmpInst;
1641   if (PerformShift) {
1642     tmpInst.setOpcode(Mips::DSLL);
1643     tmpInst.addOperand(MCOperand::CreateReg(RegNo));
1644     tmpInst.addOperand(MCOperand::CreateReg(RegNo));
1645     tmpInst.addOperand(MCOperand::CreateImm(16));
1646     tmpInst.setLoc(IDLoc);
1647     Instructions.push_back(tmpInst);
1648     tmpInst.clear();
1649   }
1650   tmpInst.setOpcode(Mips::ORi);
1651   tmpInst.addOperand(MCOperand::CreateReg(RegNo));
1652   tmpInst.addOperand(MCOperand::CreateReg(RegNo));
1653   tmpInst.addOperand(Operand);
1654   tmpInst.setLoc(IDLoc);
1655   Instructions.push_back(tmpInst);
1656 }
1657
1658 template <int Shift, bool PerformShift>
1659 void createShiftOr(int64_t Value, unsigned RegNo, SMLoc IDLoc,
1660                    SmallVectorImpl<MCInst> &Instructions) {
1661   createShiftOr<PerformShift>(
1662       MCOperand::CreateImm(((Value & (0xffffLL << Shift)) >> Shift)), RegNo,
1663       IDLoc, Instructions);
1664 }
1665 }
1666
1667 bool MipsAsmParser::expandJalWithRegs(MCInst &Inst, SMLoc IDLoc,
1668                                       SmallVectorImpl<MCInst> &Instructions) {
1669   // Create a JALR instruction which is going to replace the pseudo-JAL.
1670   MCInst JalrInst;
1671   JalrInst.setLoc(IDLoc);
1672   const MCOperand FirstRegOp = Inst.getOperand(0);
1673   const unsigned Opcode = Inst.getOpcode();
1674
1675   if (Opcode == Mips::JalOneReg) {
1676     // jal $rs => jalr $rs
1677     if (inMicroMipsMode()) {
1678       JalrInst.setOpcode(Mips::JALR16_MM);
1679       JalrInst.addOperand(FirstRegOp);
1680     } else {
1681       JalrInst.setOpcode(Mips::JALR);
1682       JalrInst.addOperand(MCOperand::CreateReg(Mips::RA));
1683       JalrInst.addOperand(FirstRegOp);
1684     }
1685   } else if (Opcode == Mips::JalTwoReg) {
1686     // jal $rd, $rs => jalr $rd, $rs
1687     JalrInst.setOpcode(inMicroMipsMode() ? Mips::JALR_MM : Mips::JALR);
1688     JalrInst.addOperand(FirstRegOp);
1689     const MCOperand SecondRegOp = Inst.getOperand(1);
1690     JalrInst.addOperand(SecondRegOp);
1691   }
1692   Instructions.push_back(JalrInst);
1693
1694   // If .set reorder is active, emit a NOP after it.
1695   if (AssemblerOptions.back()->isReorder()) {
1696     // This is a 32-bit NOP because these 2 pseudo-instructions
1697     // do not have a short delay slot.
1698     MCInst NopInst;
1699     NopInst.setOpcode(Mips::SLL);
1700     NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1701     NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1702     NopInst.addOperand(MCOperand::CreateImm(0));
1703     Instructions.push_back(NopInst);
1704   }
1705
1706   return false;
1707 }
1708
1709 bool MipsAsmParser::expandLoadImm(MCInst &Inst, SMLoc IDLoc,
1710                                   SmallVectorImpl<MCInst> &Instructions) {
1711   MCInst tmpInst;
1712   const MCOperand &ImmOp = Inst.getOperand(1);
1713   assert(ImmOp.isImm() && "expected immediate operand kind");
1714   const MCOperand &RegOp = Inst.getOperand(0);
1715   assert(RegOp.isReg() && "expected register operand kind");
1716
1717   int64_t ImmValue = ImmOp.getImm();
1718   tmpInst.setLoc(IDLoc);
1719   // FIXME: gas has a special case for values that are 000...1111, which
1720   // becomes a li -1 and then a dsrl
1721   if (0 <= ImmValue && ImmValue <= 65535) {
1722     // For unsigned and positive signed 16-bit values (0 <= j <= 65535):
1723     // li d,j => ori d,$zero,j
1724     tmpInst.setOpcode(Mips::ORi);
1725     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1726     tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1727     tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
1728     Instructions.push_back(tmpInst);
1729   } else if (ImmValue < 0 && ImmValue >= -32768) {
1730     // For negative signed 16-bit values (-32768 <= j < 0):
1731     // li d,j => addiu d,$zero,j
1732     tmpInst.setOpcode(Mips::ADDiu);
1733     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1734     tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1735     tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
1736     Instructions.push_back(tmpInst);
1737   } else if ((ImmValue & 0xffffffff) == ImmValue) {
1738     // For all other values which are representable as a 32-bit integer:
1739     // li d,j => lui d,hi16(j)
1740     //           ori d,d,lo16(j)
1741     tmpInst.setOpcode(Mips::LUi);
1742     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1743     tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
1744     Instructions.push_back(tmpInst);
1745     createShiftOr<0, false>(ImmValue, RegOp.getReg(), IDLoc, Instructions);
1746   } else if ((ImmValue & (0xffffLL << 48)) == 0) {
1747     if (!isGP64bit()) {
1748       Error(IDLoc, "instruction requires a 64-bit architecture");
1749       return true;
1750     }
1751
1752     //            <-------  lo32 ------>
1753     // <-------  hi32 ------>
1754     // <- hi16 ->             <- lo16 ->
1755     //  _________________________________
1756     // |          |          |          |
1757     // | 16-bytes | 16-bytes | 16-bytes |
1758     // |__________|__________|__________|
1759     //
1760     // For any 64-bit value that is representable as a 48-bit integer:
1761     // li d,j => lui d,hi16(j)
1762     //           ori d,d,hi16(lo32(j))
1763     //           dsll d,d,16
1764     //           ori d,d,lo16(lo32(j))
1765     tmpInst.setOpcode(Mips::LUi);
1766     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1767     tmpInst.addOperand(
1768         MCOperand::CreateImm((ImmValue & (0xffffLL << 32)) >> 32));
1769     Instructions.push_back(tmpInst);
1770     createShiftOr<16, false>(ImmValue, RegOp.getReg(), IDLoc, Instructions);
1771     createShiftOr<0, true>(ImmValue, RegOp.getReg(), IDLoc, Instructions);
1772   } else {
1773     if (!isGP64bit()) {
1774       Error(IDLoc, "instruction requires a 64-bit architecture");
1775       return true;
1776     }
1777
1778     // <-------  hi32 ------> <-------  lo32 ------>
1779     // <- hi16 ->                        <- lo16 ->
1780     //  ___________________________________________
1781     // |          |          |          |          |
1782     // | 16-bytes | 16-bytes | 16-bytes | 16-bytes |
1783     // |__________|__________|__________|__________|
1784     //
1785     // For all other values which are representable as a 64-bit integer:
1786     // li d,j => lui d,hi16(j)
1787     //           ori d,d,lo16(hi32(j))
1788     //           dsll d,d,16
1789     //           ori d,d,hi16(lo32(j))
1790     //           dsll d,d,16
1791     //           ori d,d,lo16(lo32(j))
1792     tmpInst.setOpcode(Mips::LUi);
1793     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1794     tmpInst.addOperand(
1795         MCOperand::CreateImm((ImmValue & (0xffffLL << 48)) >> 48));
1796     Instructions.push_back(tmpInst);
1797     createShiftOr<32, false>(ImmValue, RegOp.getReg(), IDLoc, Instructions);
1798     createShiftOr<16, true>(ImmValue, RegOp.getReg(), IDLoc, Instructions);
1799     createShiftOr<0, true>(ImmValue, RegOp.getReg(), IDLoc, Instructions);
1800   }
1801   return false;
1802 }
1803
1804 bool
1805 MipsAsmParser::expandLoadAddressReg(MCInst &Inst, SMLoc IDLoc,
1806                                     SmallVectorImpl<MCInst> &Instructions) {
1807   MCInst tmpInst;
1808   const MCOperand &ImmOp = Inst.getOperand(2);
1809   assert((ImmOp.isImm() || ImmOp.isExpr()) &&
1810          "expected immediate operand kind");
1811   if (!ImmOp.isImm()) {
1812     expandLoadAddressSym(Inst, IDLoc, Instructions);
1813     return false;
1814   }
1815   const MCOperand &SrcRegOp = Inst.getOperand(1);
1816   assert(SrcRegOp.isReg() && "expected register operand kind");
1817   const MCOperand &DstRegOp = Inst.getOperand(0);
1818   assert(DstRegOp.isReg() && "expected register operand kind");
1819   int ImmValue = ImmOp.getImm();
1820   if (-32768 <= ImmValue && ImmValue <= 65535) {
1821     // For -32768 <= j <= 65535.
1822     // la d,j(s) => addiu d,s,j
1823     tmpInst.setOpcode(Mips::ADDiu);
1824     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1825     tmpInst.addOperand(MCOperand::CreateReg(SrcRegOp.getReg()));
1826     tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
1827     Instructions.push_back(tmpInst);
1828   } else {
1829     // For any other value of j that is representable as a 32-bit integer.
1830     // la d,j(s) => lui d,hi16(j)
1831     //              ori d,d,lo16(j)
1832     //              addu d,d,s
1833     tmpInst.setOpcode(Mips::LUi);
1834     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1835     tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
1836     Instructions.push_back(tmpInst);
1837     tmpInst.clear();
1838     tmpInst.setOpcode(Mips::ORi);
1839     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1840     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1841     tmpInst.addOperand(MCOperand::CreateImm(ImmValue & 0xffff));
1842     Instructions.push_back(tmpInst);
1843     tmpInst.clear();
1844     tmpInst.setOpcode(Mips::ADDu);
1845     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1846     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1847     tmpInst.addOperand(MCOperand::CreateReg(SrcRegOp.getReg()));
1848     Instructions.push_back(tmpInst);
1849   }
1850   return false;
1851 }
1852
1853 bool
1854 MipsAsmParser::expandLoadAddressImm(MCInst &Inst, SMLoc IDLoc,
1855                                     SmallVectorImpl<MCInst> &Instructions) {
1856   MCInst tmpInst;
1857   const MCOperand &ImmOp = Inst.getOperand(1);
1858   assert((ImmOp.isImm() || ImmOp.isExpr()) &&
1859          "expected immediate operand kind");
1860   if (!ImmOp.isImm()) {
1861     expandLoadAddressSym(Inst, IDLoc, Instructions);
1862     return false;
1863   }
1864   const MCOperand &RegOp = Inst.getOperand(0);
1865   assert(RegOp.isReg() && "expected register operand kind");
1866   int ImmValue = ImmOp.getImm();
1867   if (-32768 <= ImmValue && ImmValue <= 65535) {
1868     // For -32768 <= j <= 65535.
1869     // la d,j => addiu d,$zero,j
1870     tmpInst.setOpcode(Mips::ADDiu);
1871     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1872     tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1873     tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
1874     Instructions.push_back(tmpInst);
1875   } else {
1876     // For any other value of j that is representable as a 32-bit integer.
1877     // la d,j => lui d,hi16(j)
1878     //           ori d,d,lo16(j)
1879     tmpInst.setOpcode(Mips::LUi);
1880     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1881     tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
1882     Instructions.push_back(tmpInst);
1883     tmpInst.clear();
1884     tmpInst.setOpcode(Mips::ORi);
1885     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1886     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1887     tmpInst.addOperand(MCOperand::CreateImm(ImmValue & 0xffff));
1888     Instructions.push_back(tmpInst);
1889   }
1890   return false;
1891 }
1892
1893 void
1894 MipsAsmParser::expandLoadAddressSym(MCInst &Inst, SMLoc IDLoc,
1895                                     SmallVectorImpl<MCInst> &Instructions) {
1896   // FIXME: If we do have a valid at register to use, we should generate a
1897   // slightly shorter sequence here.
1898   MCInst tmpInst;
1899   int ExprOperandNo = 1;
1900   // Sometimes the assembly parser will get the immediate expression as
1901   // a $zero + an immediate.
1902   if (Inst.getNumOperands() == 3) {
1903     assert(Inst.getOperand(1).getReg() ==
1904            (isGP64bit() ? Mips::ZERO_64 : Mips::ZERO));
1905     ExprOperandNo = 2;
1906   }
1907   const MCOperand &SymOp = Inst.getOperand(ExprOperandNo);
1908   assert(SymOp.isExpr() && "expected symbol operand kind");
1909   const MCOperand &RegOp = Inst.getOperand(0);
1910   unsigned RegNo = RegOp.getReg();
1911   const MCSymbolRefExpr *Symbol = cast<MCSymbolRefExpr>(SymOp.getExpr());
1912   const MCSymbolRefExpr *HiExpr =
1913       MCSymbolRefExpr::Create(Symbol->getSymbol().getName(),
1914                               MCSymbolRefExpr::VK_Mips_ABS_HI, getContext());
1915   const MCSymbolRefExpr *LoExpr =
1916       MCSymbolRefExpr::Create(Symbol->getSymbol().getName(),
1917                               MCSymbolRefExpr::VK_Mips_ABS_LO, getContext());
1918   if (isGP64bit()) {
1919     // If it's a 64-bit architecture, expand to:
1920     // la d,sym => lui  d,highest(sym)
1921     //             ori  d,d,higher(sym)
1922     //             dsll d,d,16
1923     //             ori  d,d,hi16(sym)
1924     //             dsll d,d,16
1925     //             ori  d,d,lo16(sym)
1926     const MCSymbolRefExpr *HighestExpr =
1927         MCSymbolRefExpr::Create(Symbol->getSymbol().getName(),
1928                                 MCSymbolRefExpr::VK_Mips_HIGHEST, getContext());
1929     const MCSymbolRefExpr *HigherExpr =
1930         MCSymbolRefExpr::Create(Symbol->getSymbol().getName(),
1931                                 MCSymbolRefExpr::VK_Mips_HIGHER, getContext());
1932
1933     tmpInst.setOpcode(Mips::LUi);
1934     tmpInst.addOperand(MCOperand::CreateReg(RegNo));
1935     tmpInst.addOperand(MCOperand::CreateExpr(HighestExpr));
1936     Instructions.push_back(tmpInst);
1937
1938     createShiftOr<false>(MCOperand::CreateExpr(HigherExpr), RegNo, SMLoc(),
1939                          Instructions);
1940     createShiftOr<true>(MCOperand::CreateExpr(HiExpr), RegNo, SMLoc(),
1941                         Instructions);
1942     createShiftOr<true>(MCOperand::CreateExpr(LoExpr), RegNo, SMLoc(),
1943                         Instructions);
1944   } else {
1945     // Otherwise, expand to:
1946     // la d,sym => lui  d,hi16(sym)
1947     //             ori  d,d,lo16(sym)
1948     tmpInst.setOpcode(Mips::LUi);
1949     tmpInst.addOperand(MCOperand::CreateReg(RegNo));
1950     tmpInst.addOperand(MCOperand::CreateExpr(HiExpr));
1951     Instructions.push_back(tmpInst);
1952
1953     createShiftOr<false>(MCOperand::CreateExpr(LoExpr), RegNo, SMLoc(),
1954                          Instructions);
1955   }
1956 }
1957
1958 bool MipsAsmParser::expandUncondBranchMMPseudo(
1959     MCInst &Inst, SMLoc IDLoc, SmallVectorImpl<MCInst> &Instructions) {
1960   assert(getInstDesc(Inst.getOpcode()).getNumOperands() == 1 &&
1961          "unexpected number of operands");
1962
1963   MCOperand Offset = Inst.getOperand(0);
1964   if (Offset.isExpr()) {
1965     Inst.clear();
1966     Inst.setOpcode(Mips::BEQ_MM);
1967     Inst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1968     Inst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1969     Inst.addOperand(MCOperand::CreateExpr(Offset.getExpr()));
1970   } else {
1971     assert(Offset.isImm() && "expected immediate operand kind");
1972     if (isIntN(11, Offset.getImm())) {
1973       // If offset fits into 11 bits then this instruction becomes microMIPS
1974       // 16-bit unconditional branch instruction.
1975       Inst.setOpcode(Mips::B16_MM);
1976     } else {
1977       if (!isIntN(17, Offset.getImm()))
1978         Error(IDLoc, "branch target out of range");
1979       if (OffsetToAlignment(Offset.getImm(), 1LL << 1))
1980         Error(IDLoc, "branch to misaligned address");
1981       Inst.clear();
1982       Inst.setOpcode(Mips::BEQ_MM);
1983       Inst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1984       Inst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1985       Inst.addOperand(MCOperand::CreateImm(Offset.getImm()));
1986     }
1987   }
1988   Instructions.push_back(Inst);
1989
1990   // If .set reorder is active, emit a NOP after the branch instruction.
1991   if (AssemblerOptions.back()->isReorder())
1992     createNop(true, IDLoc, Instructions);
1993
1994   return false;
1995 }
1996
1997 void MipsAsmParser::expandMemInst(MCInst &Inst, SMLoc IDLoc,
1998                                   SmallVectorImpl<MCInst> &Instructions,
1999                                   bool isLoad, bool isImmOpnd) {
2000   const MCSymbolRefExpr *SR;
2001   MCInst TempInst;
2002   unsigned ImmOffset, HiOffset, LoOffset;
2003   const MCExpr *ExprOffset;
2004   unsigned TmpRegNum;
2005   // 1st operand is either the source or destination register.
2006   assert(Inst.getOperand(0).isReg() && "expected register operand kind");
2007   unsigned RegOpNum = Inst.getOperand(0).getReg();
2008   // 2nd operand is the base register.
2009   assert(Inst.getOperand(1).isReg() && "expected register operand kind");
2010   unsigned BaseRegNum = Inst.getOperand(1).getReg();
2011   // 3rd operand is either an immediate or expression.
2012   if (isImmOpnd) {
2013     assert(Inst.getOperand(2).isImm() && "expected immediate operand kind");
2014     ImmOffset = Inst.getOperand(2).getImm();
2015     LoOffset = ImmOffset & 0x0000ffff;
2016     HiOffset = (ImmOffset & 0xffff0000) >> 16;
2017     // If msb of LoOffset is 1(negative number) we must increment HiOffset.
2018     if (LoOffset & 0x8000)
2019       HiOffset++;
2020   } else
2021     ExprOffset = Inst.getOperand(2).getExpr();
2022   // All instructions will have the same location.
2023   TempInst.setLoc(IDLoc);
2024   // These are some of the types of expansions we perform here:
2025   // 1) lw $8, sym        => lui $8, %hi(sym)
2026   //                         lw $8, %lo(sym)($8)
2027   // 2) lw $8, offset($9) => lui $8, %hi(offset)
2028   //                         add $8, $8, $9
2029   //                         lw $8, %lo(offset)($9)
2030   // 3) lw $8, offset($8) => lui $at, %hi(offset)
2031   //                         add $at, $at, $8
2032   //                         lw $8, %lo(offset)($at)
2033   // 4) sw $8, sym        => lui $at, %hi(sym)
2034   //                         sw $8, %lo(sym)($at)
2035   // 5) sw $8, offset($8) => lui $at, %hi(offset)
2036   //                         add $at, $at, $8
2037   //                         sw $8, %lo(offset)($at)
2038   // 6) ldc1 $f0, sym     => lui $at, %hi(sym)
2039   //                         ldc1 $f0, %lo(sym)($at)
2040   //
2041   // For load instructions we can use the destination register as a temporary
2042   // if base and dst are different (examples 1 and 2) and if the base register
2043   // is general purpose otherwise we must use $at (example 6) and error if it's
2044   // not available. For stores we must use $at (examples 4 and 5) because we
2045   // must not clobber the source register setting up the offset.
2046   const MCInstrDesc &Desc = getInstDesc(Inst.getOpcode());
2047   int16_t RegClassOp0 = Desc.OpInfo[0].RegClass;
2048   unsigned RegClassIDOp0 =
2049       getContext().getRegisterInfo()->getRegClass(RegClassOp0).getID();
2050   bool IsGPR = (RegClassIDOp0 == Mips::GPR32RegClassID) ||
2051                (RegClassIDOp0 == Mips::GPR64RegClassID);
2052   if (isLoad && IsGPR && (BaseRegNum != RegOpNum))
2053     TmpRegNum = RegOpNum;
2054   else {
2055     // At this point we need AT to perform the expansions and we exit if it is
2056     // not available.
2057     TmpRegNum = getATReg(IDLoc);
2058     if (!TmpRegNum)
2059       return;
2060   }
2061
2062   TempInst.setOpcode(Mips::LUi);
2063   TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
2064   if (isImmOpnd)
2065     TempInst.addOperand(MCOperand::CreateImm(HiOffset));
2066   else {
2067     if (ExprOffset->getKind() == MCExpr::SymbolRef) {
2068       SR = static_cast<const MCSymbolRefExpr *>(ExprOffset);
2069       const MCSymbolRefExpr *HiExpr = MCSymbolRefExpr::Create(
2070           SR->getSymbol().getName(), MCSymbolRefExpr::VK_Mips_ABS_HI,
2071           getContext());
2072       TempInst.addOperand(MCOperand::CreateExpr(HiExpr));
2073     } else {
2074       const MCExpr *HiExpr = evaluateRelocExpr(ExprOffset, "hi");
2075       TempInst.addOperand(MCOperand::CreateExpr(HiExpr));
2076     }
2077   }
2078   // Add the instruction to the list.
2079   Instructions.push_back(TempInst);
2080   // Prepare TempInst for next instruction.
2081   TempInst.clear();
2082   // Add temp register to base.
2083   if (BaseRegNum != Mips::ZERO) {
2084     TempInst.setOpcode(Mips::ADDu);
2085     TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
2086     TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
2087     TempInst.addOperand(MCOperand::CreateReg(BaseRegNum));
2088     Instructions.push_back(TempInst);
2089     TempInst.clear();
2090   }
2091   // And finally, create original instruction with low part
2092   // of offset and new base.
2093   TempInst.setOpcode(Inst.getOpcode());
2094   TempInst.addOperand(MCOperand::CreateReg(RegOpNum));
2095   TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
2096   if (isImmOpnd)
2097     TempInst.addOperand(MCOperand::CreateImm(LoOffset));
2098   else {
2099     if (ExprOffset->getKind() == MCExpr::SymbolRef) {
2100       const MCSymbolRefExpr *LoExpr = MCSymbolRefExpr::Create(
2101           SR->getSymbol().getName(), MCSymbolRefExpr::VK_Mips_ABS_LO,
2102           getContext());
2103       TempInst.addOperand(MCOperand::CreateExpr(LoExpr));
2104     } else {
2105       const MCExpr *LoExpr = evaluateRelocExpr(ExprOffset, "lo");
2106       TempInst.addOperand(MCOperand::CreateExpr(LoExpr));
2107     }
2108   }
2109   Instructions.push_back(TempInst);
2110   TempInst.clear();
2111 }
2112
2113 bool
2114 MipsAsmParser::expandLoadStoreMultiple(MCInst &Inst, SMLoc IDLoc,
2115                                        SmallVectorImpl<MCInst> &Instructions) {
2116   unsigned OpNum = Inst.getNumOperands();
2117   unsigned Opcode = Inst.getOpcode();
2118   unsigned NewOpcode = Opcode == Mips::SWM_MM ? Mips::SWM32_MM : Mips::LWM32_MM;
2119
2120   assert (Inst.getOperand(OpNum - 1).isImm() &&
2121           Inst.getOperand(OpNum - 2).isReg() &&
2122           Inst.getOperand(OpNum - 3).isReg() && "Invalid instruction operand.");
2123
2124   if (OpNum < 8 && Inst.getOperand(OpNum - 1).getImm() <= 60 &&
2125       Inst.getOperand(OpNum - 1).getImm() >= 0 &&
2126       Inst.getOperand(OpNum - 2).getReg() == Mips::SP &&
2127       Inst.getOperand(OpNum - 3).getReg() == Mips::RA)
2128     // It can be implemented as SWM16 or LWM16 instruction.
2129     NewOpcode = Opcode == Mips::SWM_MM ? Mips::SWM16_MM : Mips::LWM16_MM;
2130
2131   Inst.setOpcode(NewOpcode);
2132   Instructions.push_back(Inst);
2133   return false;
2134 }
2135
2136 void MipsAsmParser::createNop(bool hasShortDelaySlot, SMLoc IDLoc,
2137                               SmallVectorImpl<MCInst> &Instructions) {
2138   MCInst NopInst;
2139   if (hasShortDelaySlot) {
2140     NopInst.setOpcode(Mips::MOVE16_MM);
2141     NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
2142     NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
2143   } else {
2144     NopInst.setOpcode(Mips::SLL);
2145     NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
2146     NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
2147     NopInst.addOperand(MCOperand::CreateImm(0));
2148   }
2149   Instructions.push_back(NopInst);
2150 }
2151
2152 unsigned MipsAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
2153   // As described by the Mips32r2 spec, the registers Rd and Rs for
2154   // jalr.hb must be different.
2155   unsigned Opcode = Inst.getOpcode();
2156
2157   if (Opcode == Mips::JALR_HB &&
2158       (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()))
2159     return Match_RequiresDifferentSrcAndDst;
2160
2161   return Match_Success;
2162 }
2163
2164 bool MipsAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
2165                                             OperandVector &Operands,
2166                                             MCStreamer &Out,
2167                                             uint64_t &ErrorInfo,
2168                                             bool MatchingInlineAsm) {
2169
2170   MCInst Inst;
2171   SmallVector<MCInst, 8> Instructions;
2172   unsigned MatchResult =
2173       MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
2174
2175   switch (MatchResult) {
2176   case Match_Success: {
2177     if (processInstruction(Inst, IDLoc, Instructions))
2178       return true;
2179     for (unsigned i = 0; i < Instructions.size(); i++)
2180       Out.EmitInstruction(Instructions[i], STI);
2181     return false;
2182   }
2183   case Match_MissingFeature:
2184     Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2185     return true;
2186   case Match_InvalidOperand: {
2187     SMLoc ErrorLoc = IDLoc;
2188     if (ErrorInfo != ~0ULL) {
2189       if (ErrorInfo >= Operands.size())
2190         return Error(IDLoc, "too few operands for instruction");
2191
2192       ErrorLoc = ((MipsOperand &)*Operands[ErrorInfo]).getStartLoc();
2193       if (ErrorLoc == SMLoc())
2194         ErrorLoc = IDLoc;
2195     }
2196
2197     return Error(ErrorLoc, "invalid operand for instruction");
2198   }
2199   case Match_MnemonicFail:
2200     return Error(IDLoc, "invalid instruction");
2201   case Match_RequiresDifferentSrcAndDst:
2202     return Error(IDLoc, "source and destination must be different");
2203   }
2204
2205   llvm_unreachable("Implement any new match types added!");
2206 }
2207
2208 void MipsAsmParser::warnIfAssemblerTemporary(int RegIndex, SMLoc Loc) {
2209   if ((RegIndex != 0) && 
2210       ((int)AssemblerOptions.back()->getATRegNum() == RegIndex)) {
2211     if (RegIndex == 1)
2212       Warning(Loc, "used $at without \".set noat\"");
2213     else
2214       Warning(Loc, Twine("used $") + Twine(RegIndex) + " with \".set at=$" +
2215                        Twine(RegIndex) + "\"");
2216   }
2217 }
2218
2219 void
2220 MipsAsmParser::printWarningWithFixIt(const Twine &Msg, const Twine &FixMsg,
2221                                      SMRange Range, bool ShowColors) {
2222   getSourceManager().PrintMessage(Range.Start, SourceMgr::DK_Warning, Msg,
2223                                   Range, SMFixIt(Range, FixMsg),
2224                                   ShowColors);
2225 }
2226
2227 int MipsAsmParser::matchCPURegisterName(StringRef Name) {
2228   int CC;
2229
2230   CC = StringSwitch<unsigned>(Name)
2231            .Case("zero", 0)
2232            .Case("at", 1)
2233            .Case("a0", 4)
2234            .Case("a1", 5)
2235            .Case("a2", 6)
2236            .Case("a3", 7)
2237            .Case("v0", 2)
2238            .Case("v1", 3)
2239            .Case("s0", 16)
2240            .Case("s1", 17)
2241            .Case("s2", 18)
2242            .Case("s3", 19)
2243            .Case("s4", 20)
2244            .Case("s5", 21)
2245            .Case("s6", 22)
2246            .Case("s7", 23)
2247            .Case("k0", 26)
2248            .Case("k1", 27)
2249            .Case("gp", 28)
2250            .Case("sp", 29)
2251            .Case("fp", 30)
2252            .Case("s8", 30)
2253            .Case("ra", 31)
2254            .Case("t0", 8)
2255            .Case("t1", 9)
2256            .Case("t2", 10)
2257            .Case("t3", 11)
2258            .Case("t4", 12)
2259            .Case("t5", 13)
2260            .Case("t6", 14)
2261            .Case("t7", 15)
2262            .Case("t8", 24)
2263            .Case("t9", 25)
2264            .Default(-1);
2265
2266   if (!(isABI_N32() || isABI_N64()))
2267     return CC;
2268
2269   if (12 <= CC && CC <= 15) {
2270     // Name is one of t4-t7
2271     AsmToken RegTok = getLexer().peekTok();
2272     SMRange RegRange = RegTok.getLocRange();
2273
2274     StringRef FixedName = StringSwitch<StringRef>(Name)
2275                               .Case("t4", "t0")
2276                               .Case("t5", "t1")
2277                               .Case("t6", "t2")
2278                               .Case("t7", "t3")
2279                               .Default("");
2280     assert(FixedName != "" &&  "Register name is not one of t4-t7.");
2281
2282     printWarningWithFixIt("register names $t4-$t7 are only available in O32.",
2283                           "Did you mean $" + FixedName + "?", RegRange);
2284   }
2285
2286   // Although SGI documentation just cuts out t0-t3 for n32/n64,
2287   // GNU pushes the values of t0-t3 to override the o32/o64 values for t4-t7
2288   // We are supporting both cases, so for t0-t3 we'll just push them to t4-t7.
2289   if (8 <= CC && CC <= 11)
2290     CC += 4;
2291
2292   if (CC == -1)
2293     CC = StringSwitch<unsigned>(Name)
2294              .Case("a4", 8)
2295              .Case("a5", 9)
2296              .Case("a6", 10)
2297              .Case("a7", 11)
2298              .Case("kt0", 26)
2299              .Case("kt1", 27)
2300              .Default(-1);
2301
2302   return CC;
2303 }
2304
2305 int MipsAsmParser::matchHWRegsRegisterName(StringRef Name) {
2306   int CC;
2307
2308   CC = StringSwitch<unsigned>(Name)
2309             .Case("hwr_cpunum", 0)
2310             .Case("hwr_synci_step", 1)
2311             .Case("hwr_cc", 2)
2312             .Case("hwr_ccres", 3)
2313             .Case("hwr_ulr", 29)
2314             .Default(-1);
2315
2316   return CC;
2317 }
2318
2319 int MipsAsmParser::matchFPURegisterName(StringRef Name) {
2320
2321   if (Name[0] == 'f') {
2322     StringRef NumString = Name.substr(1);
2323     unsigned IntVal;
2324     if (NumString.getAsInteger(10, IntVal))
2325       return -1;     // This is not an integer.
2326     if (IntVal > 31) // Maximum index for fpu register.
2327       return -1;
2328     return IntVal;
2329   }
2330   return -1;
2331 }
2332
2333 int MipsAsmParser::matchFCCRegisterName(StringRef Name) {
2334
2335   if (Name.startswith("fcc")) {
2336     StringRef NumString = Name.substr(3);
2337     unsigned IntVal;
2338     if (NumString.getAsInteger(10, IntVal))
2339       return -1;    // This is not an integer.
2340     if (IntVal > 7) // There are only 8 fcc registers.
2341       return -1;
2342     return IntVal;
2343   }
2344   return -1;
2345 }
2346
2347 int MipsAsmParser::matchACRegisterName(StringRef Name) {
2348
2349   if (Name.startswith("ac")) {
2350     StringRef NumString = Name.substr(2);
2351     unsigned IntVal;
2352     if (NumString.getAsInteger(10, IntVal))
2353       return -1;    // This is not an integer.
2354     if (IntVal > 3) // There are only 3 acc registers.
2355       return -1;
2356     return IntVal;
2357   }
2358   return -1;
2359 }
2360
2361 int MipsAsmParser::matchMSA128RegisterName(StringRef Name) {
2362   unsigned IntVal;
2363
2364   if (Name.front() != 'w' || Name.drop_front(1).getAsInteger(10, IntVal))
2365     return -1;
2366
2367   if (IntVal > 31)
2368     return -1;
2369
2370   return IntVal;
2371 }
2372
2373 int MipsAsmParser::matchMSA128CtrlRegisterName(StringRef Name) {
2374   int CC;
2375
2376   CC = StringSwitch<unsigned>(Name)
2377            .Case("msair", 0)
2378            .Case("msacsr", 1)
2379            .Case("msaaccess", 2)
2380            .Case("msasave", 3)
2381            .Case("msamodify", 4)
2382            .Case("msarequest", 5)
2383            .Case("msamap", 6)
2384            .Case("msaunmap", 7)
2385            .Default(-1);
2386
2387   return CC;
2388 }
2389
2390 unsigned MipsAsmParser::getATReg(SMLoc Loc) {
2391   unsigned ATIndex = AssemblerOptions.back()->getATRegNum();
2392   if (ATIndex == 0) {
2393     reportParseError(Loc,
2394                      "pseudo-instruction requires $at, which is not available");
2395     return 0;
2396   }
2397   unsigned AT = getReg(
2398       (isGP64bit()) ? Mips::GPR64RegClassID : Mips::GPR32RegClassID, ATIndex);
2399   return AT;
2400 }
2401
2402 unsigned MipsAsmParser::getReg(int RC, int RegNo) {
2403   return *(getContext().getRegisterInfo()->getRegClass(RC).begin() + RegNo);
2404 }
2405
2406 unsigned MipsAsmParser::getGPR(int RegNo) {
2407   return getReg(isGP64bit() ? Mips::GPR64RegClassID : Mips::GPR32RegClassID,
2408                 RegNo);
2409 }
2410
2411 int MipsAsmParser::matchRegisterByNumber(unsigned RegNum, unsigned RegClass) {
2412   if (RegNum >
2413       getContext().getRegisterInfo()->getRegClass(RegClass).getNumRegs() - 1)
2414     return -1;
2415
2416   return getReg(RegClass, RegNum);
2417 }
2418
2419 bool MipsAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
2420   MCAsmParser &Parser = getParser();
2421   DEBUG(dbgs() << "parseOperand\n");
2422
2423   // Check if the current operand has a custom associated parser, if so, try to
2424   // custom parse the operand, or fallback to the general approach.
2425   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
2426   if (ResTy == MatchOperand_Success)
2427     return false;
2428   // If there wasn't a custom match, try the generic matcher below. Otherwise,
2429   // there was a match, but an error occurred, in which case, just return that
2430   // the operand parsing failed.
2431   if (ResTy == MatchOperand_ParseFail)
2432     return true;
2433
2434   DEBUG(dbgs() << ".. Generic Parser\n");
2435
2436   switch (getLexer().getKind()) {
2437   default:
2438     Error(Parser.getTok().getLoc(), "unexpected token in operand");
2439     return true;
2440   case AsmToken::Dollar: {
2441     // Parse the register.
2442     SMLoc S = Parser.getTok().getLoc();
2443
2444     // Almost all registers have been parsed by custom parsers. There is only
2445     // one exception to this. $zero (and it's alias $0) will reach this point
2446     // for div, divu, and similar instructions because it is not an operand
2447     // to the instruction definition but an explicit register. Special case
2448     // this situation for now.
2449     if (parseAnyRegister(Operands) != MatchOperand_NoMatch)
2450       return false;
2451
2452     // Maybe it is a symbol reference.
2453     StringRef Identifier;
2454     if (Parser.parseIdentifier(Identifier))
2455       return true;
2456
2457     SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
2458     MCSymbol *Sym = getContext().GetOrCreateSymbol("$" + Identifier);
2459     // Otherwise create a symbol reference.
2460     const MCExpr *Res =
2461         MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
2462
2463     Operands.push_back(MipsOperand::CreateImm(Res, S, E, *this));
2464     return false;
2465   }
2466   // Else drop to expression parsing.
2467   case AsmToken::LParen:
2468   case AsmToken::Minus:
2469   case AsmToken::Plus:
2470   case AsmToken::Integer:
2471   case AsmToken::Tilde:
2472   case AsmToken::String: {
2473     DEBUG(dbgs() << ".. generic integer\n");
2474     OperandMatchResultTy ResTy = parseImm(Operands);
2475     return ResTy != MatchOperand_Success;
2476   }
2477   case AsmToken::Percent: {
2478     // It is a symbol reference or constant expression.
2479     const MCExpr *IdVal;
2480     SMLoc S = Parser.getTok().getLoc(); // Start location of the operand.
2481     if (parseRelocOperand(IdVal))
2482       return true;
2483
2484     SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
2485
2486     Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
2487     return false;
2488   } // case AsmToken::Percent
2489   } // switch(getLexer().getKind())
2490   return true;
2491 }
2492
2493 const MCExpr *MipsAsmParser::evaluateRelocExpr(const MCExpr *Expr,
2494                                                StringRef RelocStr) {
2495   const MCExpr *Res;
2496   // Check the type of the expression.
2497   if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Expr)) {
2498     // It's a constant, evaluate reloc value.
2499     int16_t Val;
2500     switch (getVariantKind(RelocStr)) {
2501     case MCSymbolRefExpr::VK_Mips_ABS_LO:
2502       // Get the 1st 16-bits.
2503       Val = MCE->getValue() & 0xffff;
2504       break;
2505     case MCSymbolRefExpr::VK_Mips_ABS_HI:
2506       // Get the 2nd 16-bits. Also add 1 if bit 15 is 1, to compensate for low
2507       // 16 bits being negative.
2508       Val = ((MCE->getValue() + 0x8000) >> 16) & 0xffff;
2509       break;
2510     case MCSymbolRefExpr::VK_Mips_HIGHER:
2511       // Get the 3rd 16-bits.
2512       Val = ((MCE->getValue() + 0x80008000LL) >> 32) & 0xffff;
2513       break;
2514     case MCSymbolRefExpr::VK_Mips_HIGHEST:
2515       // Get the 4th 16-bits.
2516       Val = ((MCE->getValue() + 0x800080008000LL) >> 48) & 0xffff;
2517       break;
2518     default:
2519       report_fatal_error("unsupported reloc value");
2520     }
2521     return MCConstantExpr::Create(Val, getContext());
2522   }
2523
2524   if (const MCSymbolRefExpr *MSRE = dyn_cast<MCSymbolRefExpr>(Expr)) {
2525     // It's a symbol, create a symbolic expression from the symbol.
2526     StringRef Symbol = MSRE->getSymbol().getName();
2527     MCSymbolRefExpr::VariantKind VK = getVariantKind(RelocStr);
2528     Res = MCSymbolRefExpr::Create(Symbol, VK, getContext());
2529     return Res;
2530   }
2531
2532   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
2533     MCSymbolRefExpr::VariantKind VK = getVariantKind(RelocStr);
2534
2535     // Try to create target expression.
2536     if (MipsMCExpr::isSupportedBinaryExpr(VK, BE))
2537       return MipsMCExpr::Create(VK, Expr, getContext());
2538
2539     const MCExpr *LExp = evaluateRelocExpr(BE->getLHS(), RelocStr);
2540     const MCExpr *RExp = evaluateRelocExpr(BE->getRHS(), RelocStr);
2541     Res = MCBinaryExpr::Create(BE->getOpcode(), LExp, RExp, getContext());
2542     return Res;
2543   }
2544
2545   if (const MCUnaryExpr *UN = dyn_cast<MCUnaryExpr>(Expr)) {
2546     const MCExpr *UnExp = evaluateRelocExpr(UN->getSubExpr(), RelocStr);
2547     Res = MCUnaryExpr::Create(UN->getOpcode(), UnExp, getContext());
2548     return Res;
2549   }
2550   // Just return the original expression.
2551   return Expr;
2552 }
2553
2554 bool MipsAsmParser::isEvaluated(const MCExpr *Expr) {
2555
2556   switch (Expr->getKind()) {
2557   case MCExpr::Constant:
2558     return true;
2559   case MCExpr::SymbolRef:
2560     return (cast<MCSymbolRefExpr>(Expr)->getKind() != MCSymbolRefExpr::VK_None);
2561   case MCExpr::Binary:
2562     if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
2563       if (!isEvaluated(BE->getLHS()))
2564         return false;
2565       return isEvaluated(BE->getRHS());
2566     }
2567   case MCExpr::Unary:
2568     return isEvaluated(cast<MCUnaryExpr>(Expr)->getSubExpr());
2569   case MCExpr::Target:
2570     return true;
2571   }
2572   return false;
2573 }
2574
2575 bool MipsAsmParser::parseRelocOperand(const MCExpr *&Res) {
2576   MCAsmParser &Parser = getParser();
2577   Parser.Lex();                          // Eat the % token.
2578   const AsmToken &Tok = Parser.getTok(); // Get next token, operation.
2579   if (Tok.isNot(AsmToken::Identifier))
2580     return true;
2581
2582   std::string Str = Tok.getIdentifier();
2583
2584   Parser.Lex(); // Eat the identifier.
2585   // Now make an expression from the rest of the operand.
2586   const MCExpr *IdVal;
2587   SMLoc EndLoc;
2588
2589   if (getLexer().getKind() == AsmToken::LParen) {
2590     while (1) {
2591       Parser.Lex(); // Eat the '(' token.
2592       if (getLexer().getKind() == AsmToken::Percent) {
2593         Parser.Lex(); // Eat the % token.
2594         const AsmToken &nextTok = Parser.getTok();
2595         if (nextTok.isNot(AsmToken::Identifier))
2596           return true;
2597         Str += "(%";
2598         Str += nextTok.getIdentifier();
2599         Parser.Lex(); // Eat the identifier.
2600         if (getLexer().getKind() != AsmToken::LParen)
2601           return true;
2602       } else
2603         break;
2604     }
2605     if (getParser().parseParenExpression(IdVal, EndLoc))
2606       return true;
2607
2608     while (getLexer().getKind() == AsmToken::RParen)
2609       Parser.Lex(); // Eat the ')' token.
2610
2611   } else
2612     return true; // Parenthesis must follow the relocation operand.
2613
2614   Res = evaluateRelocExpr(IdVal, Str);
2615   return false;
2616 }
2617
2618 bool MipsAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
2619                                   SMLoc &EndLoc) {
2620   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Operands;
2621   OperandMatchResultTy ResTy = parseAnyRegister(Operands);
2622   if (ResTy == MatchOperand_Success) {
2623     assert(Operands.size() == 1);
2624     MipsOperand &Operand = static_cast<MipsOperand &>(*Operands.front());
2625     StartLoc = Operand.getStartLoc();
2626     EndLoc = Operand.getEndLoc();
2627
2628     // AFAIK, we only support numeric registers and named GPR's in CFI
2629     // directives.
2630     // Don't worry about eating tokens before failing. Using an unrecognised
2631     // register is a parse error.
2632     if (Operand.isGPRAsmReg()) {
2633       // Resolve to GPR32 or GPR64 appropriately.
2634       RegNo = isGP64bit() ? Operand.getGPR64Reg() : Operand.getGPR32Reg();
2635     }
2636
2637     return (RegNo == (unsigned)-1);
2638   }
2639
2640   assert(Operands.size() == 0);
2641   return (RegNo == (unsigned)-1);
2642 }
2643
2644 bool MipsAsmParser::parseMemOffset(const MCExpr *&Res, bool isParenExpr) {
2645   MCAsmParser &Parser = getParser();
2646   SMLoc S;
2647   bool Result = true;
2648
2649   while (getLexer().getKind() == AsmToken::LParen)
2650     Parser.Lex();
2651
2652   switch (getLexer().getKind()) {
2653   default:
2654     return true;
2655   case AsmToken::Identifier:
2656   case AsmToken::LParen:
2657   case AsmToken::Integer:
2658   case AsmToken::Minus:
2659   case AsmToken::Plus:
2660     if (isParenExpr)
2661       Result = getParser().parseParenExpression(Res, S);
2662     else
2663       Result = (getParser().parseExpression(Res));
2664     while (getLexer().getKind() == AsmToken::RParen)
2665       Parser.Lex();
2666     break;
2667   case AsmToken::Percent:
2668     Result = parseRelocOperand(Res);
2669   }
2670   return Result;
2671 }
2672
2673 MipsAsmParser::OperandMatchResultTy
2674 MipsAsmParser::parseMemOperand(OperandVector &Operands) {
2675   MCAsmParser &Parser = getParser();
2676   DEBUG(dbgs() << "parseMemOperand\n");
2677   const MCExpr *IdVal = nullptr;
2678   SMLoc S;
2679   bool isParenExpr = false;
2680   MipsAsmParser::OperandMatchResultTy Res = MatchOperand_NoMatch;
2681   // First operand is the offset.
2682   S = Parser.getTok().getLoc();
2683
2684   if (getLexer().getKind() == AsmToken::LParen) {
2685     Parser.Lex();
2686     isParenExpr = true;
2687   }
2688
2689   if (getLexer().getKind() != AsmToken::Dollar) {
2690     if (parseMemOffset(IdVal, isParenExpr))
2691       return MatchOperand_ParseFail;
2692
2693     const AsmToken &Tok = Parser.getTok(); // Get the next token.
2694     if (Tok.isNot(AsmToken::LParen)) {
2695       MipsOperand &Mnemonic = static_cast<MipsOperand &>(*Operands[0]);
2696       if (Mnemonic.getToken() == "la") {
2697         SMLoc E =
2698             SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
2699         Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
2700         return MatchOperand_Success;
2701       }
2702       if (Tok.is(AsmToken::EndOfStatement)) {
2703         SMLoc E =
2704             SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
2705
2706         // Zero register assumed, add a memory operand with ZERO as its base.
2707         // "Base" will be managed by k_Memory.
2708         auto Base = MipsOperand::createGPRReg(0, getContext().getRegisterInfo(),
2709                                               S, E, *this);
2710         Operands.push_back(
2711             MipsOperand::CreateMem(std::move(Base), IdVal, S, E, *this));
2712         return MatchOperand_Success;
2713       }
2714       Error(Parser.getTok().getLoc(), "'(' expected");
2715       return MatchOperand_ParseFail;
2716     }
2717
2718     Parser.Lex(); // Eat the '(' token.
2719   }
2720
2721   Res = parseAnyRegister(Operands);
2722   if (Res != MatchOperand_Success)
2723     return Res;
2724
2725   if (Parser.getTok().isNot(AsmToken::RParen)) {
2726     Error(Parser.getTok().getLoc(), "')' expected");
2727     return MatchOperand_ParseFail;
2728   }
2729
2730   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
2731
2732   Parser.Lex(); // Eat the ')' token.
2733
2734   if (!IdVal)
2735     IdVal = MCConstantExpr::Create(0, getContext());
2736
2737   // Replace the register operand with the memory operand.
2738   std::unique_ptr<MipsOperand> op(
2739       static_cast<MipsOperand *>(Operands.back().release()));
2740   // Remove the register from the operands.
2741   // "op" will be managed by k_Memory.
2742   Operands.pop_back();
2743   // Add the memory operand.
2744   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(IdVal)) {
2745     int64_t Imm;
2746     if (IdVal->EvaluateAsAbsolute(Imm))
2747       IdVal = MCConstantExpr::Create(Imm, getContext());
2748     else if (BE->getLHS()->getKind() != MCExpr::SymbolRef)
2749       IdVal = MCBinaryExpr::Create(BE->getOpcode(), BE->getRHS(), BE->getLHS(),
2750                                    getContext());
2751   }
2752
2753   Operands.push_back(MipsOperand::CreateMem(std::move(op), IdVal, S, E, *this));
2754   return MatchOperand_Success;
2755 }
2756
2757 bool MipsAsmParser::searchSymbolAlias(OperandVector &Operands) {
2758   MCAsmParser &Parser = getParser();
2759   MCSymbol *Sym = getContext().LookupSymbol(Parser.getTok().getIdentifier());
2760   if (Sym) {
2761     SMLoc S = Parser.getTok().getLoc();
2762     const MCExpr *Expr;
2763     if (Sym->isVariable())
2764       Expr = Sym->getVariableValue();
2765     else
2766       return false;
2767     if (Expr->getKind() == MCExpr::SymbolRef) {
2768       const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
2769       StringRef DefSymbol = Ref->getSymbol().getName();
2770       if (DefSymbol.startswith("$")) {
2771         OperandMatchResultTy ResTy =
2772             matchAnyRegisterNameWithoutDollar(Operands, DefSymbol.substr(1), S);
2773         if (ResTy == MatchOperand_Success) {
2774           Parser.Lex();
2775           return true;
2776         } else if (ResTy == MatchOperand_ParseFail)
2777           llvm_unreachable("Should never ParseFail");
2778         return false;
2779       }
2780     } else if (Expr->getKind() == MCExpr::Constant) {
2781       Parser.Lex();
2782       const MCConstantExpr *Const = static_cast<const MCConstantExpr *>(Expr);
2783       Operands.push_back(
2784           MipsOperand::CreateImm(Const, S, Parser.getTok().getLoc(), *this));
2785       return true;
2786     }
2787   }
2788   return false;
2789 }
2790
2791 MipsAsmParser::OperandMatchResultTy
2792 MipsAsmParser::matchAnyRegisterNameWithoutDollar(OperandVector &Operands,
2793                                                  StringRef Identifier,
2794                                                  SMLoc S) {
2795   int Index = matchCPURegisterName(Identifier);
2796   if (Index != -1) {
2797     Operands.push_back(MipsOperand::createGPRReg(
2798         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
2799     return MatchOperand_Success;
2800   }
2801
2802   Index = matchHWRegsRegisterName(Identifier);
2803   if (Index != -1) {
2804     Operands.push_back(MipsOperand::createHWRegsReg(
2805         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
2806     return MatchOperand_Success;
2807   }
2808
2809   Index = matchFPURegisterName(Identifier);
2810   if (Index != -1) {
2811     Operands.push_back(MipsOperand::createFGRReg(
2812         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
2813     return MatchOperand_Success;
2814   }
2815
2816   Index = matchFCCRegisterName(Identifier);
2817   if (Index != -1) {
2818     Operands.push_back(MipsOperand::createFCCReg(
2819         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
2820     return MatchOperand_Success;
2821   }
2822
2823   Index = matchACRegisterName(Identifier);
2824   if (Index != -1) {
2825     Operands.push_back(MipsOperand::createACCReg(
2826         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
2827     return MatchOperand_Success;
2828   }
2829
2830   Index = matchMSA128RegisterName(Identifier);
2831   if (Index != -1) {
2832     Operands.push_back(MipsOperand::createMSA128Reg(
2833         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
2834     return MatchOperand_Success;
2835   }
2836
2837   Index = matchMSA128CtrlRegisterName(Identifier);
2838   if (Index != -1) {
2839     Operands.push_back(MipsOperand::createMSACtrlReg(
2840         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
2841     return MatchOperand_Success;
2842   }
2843
2844   return MatchOperand_NoMatch;
2845 }
2846
2847 MipsAsmParser::OperandMatchResultTy
2848 MipsAsmParser::matchAnyRegisterWithoutDollar(OperandVector &Operands, SMLoc S) {
2849   MCAsmParser &Parser = getParser();
2850   auto Token = Parser.getLexer().peekTok(false);
2851
2852   if (Token.is(AsmToken::Identifier)) {
2853     DEBUG(dbgs() << ".. identifier\n");
2854     StringRef Identifier = Token.getIdentifier();
2855     OperandMatchResultTy ResTy =
2856         matchAnyRegisterNameWithoutDollar(Operands, Identifier, S);
2857     return ResTy;
2858   } else if (Token.is(AsmToken::Integer)) {
2859     DEBUG(dbgs() << ".. integer\n");
2860     Operands.push_back(MipsOperand::createNumericReg(
2861         Token.getIntVal(), getContext().getRegisterInfo(), S, Token.getLoc(),
2862         *this));
2863     return MatchOperand_Success;
2864   }
2865
2866   DEBUG(dbgs() << Parser.getTok().getKind() << "\n");
2867
2868   return MatchOperand_NoMatch;
2869 }
2870
2871 MipsAsmParser::OperandMatchResultTy
2872 MipsAsmParser::parseAnyRegister(OperandVector &Operands) {
2873   MCAsmParser &Parser = getParser();
2874   DEBUG(dbgs() << "parseAnyRegister\n");
2875
2876   auto Token = Parser.getTok();
2877
2878   SMLoc S = Token.getLoc();
2879
2880   if (Token.isNot(AsmToken::Dollar)) {
2881     DEBUG(dbgs() << ".. !$ -> try sym aliasing\n");
2882     if (Token.is(AsmToken::Identifier)) {
2883       if (searchSymbolAlias(Operands))
2884         return MatchOperand_Success;
2885     }
2886     DEBUG(dbgs() << ".. !symalias -> NoMatch\n");
2887     return MatchOperand_NoMatch;
2888   }
2889   DEBUG(dbgs() << ".. $\n");
2890
2891   OperandMatchResultTy ResTy = matchAnyRegisterWithoutDollar(Operands, S);
2892   if (ResTy == MatchOperand_Success) {
2893     Parser.Lex(); // $
2894     Parser.Lex(); // identifier
2895   }
2896   return ResTy;
2897 }
2898
2899 MipsAsmParser::OperandMatchResultTy
2900 MipsAsmParser::parseImm(OperandVector &Operands) {
2901   MCAsmParser &Parser = getParser();
2902   switch (getLexer().getKind()) {
2903   default:
2904     return MatchOperand_NoMatch;
2905   case AsmToken::LParen:
2906   case AsmToken::Minus:
2907   case AsmToken::Plus:
2908   case AsmToken::Integer:
2909   case AsmToken::Tilde:
2910   case AsmToken::String:
2911     break;
2912   }
2913
2914   const MCExpr *IdVal;
2915   SMLoc S = Parser.getTok().getLoc();
2916   if (getParser().parseExpression(IdVal))
2917     return MatchOperand_ParseFail;
2918
2919   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
2920   Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
2921   return MatchOperand_Success;
2922 }
2923
2924 MipsAsmParser::OperandMatchResultTy
2925 MipsAsmParser::parseJumpTarget(OperandVector &Operands) {
2926   MCAsmParser &Parser = getParser();
2927   DEBUG(dbgs() << "parseJumpTarget\n");
2928
2929   SMLoc S = getLexer().getLoc();
2930
2931   // Integers and expressions are acceptable
2932   OperandMatchResultTy ResTy = parseImm(Operands);
2933   if (ResTy != MatchOperand_NoMatch)
2934     return ResTy;
2935
2936   // Registers are a valid target and have priority over symbols.
2937   ResTy = parseAnyRegister(Operands);
2938   if (ResTy != MatchOperand_NoMatch)
2939     return ResTy;
2940
2941   const MCExpr *Expr = nullptr;
2942   if (Parser.parseExpression(Expr)) {
2943     // We have no way of knowing if a symbol was consumed so we must ParseFail
2944     return MatchOperand_ParseFail;
2945   }
2946   Operands.push_back(
2947       MipsOperand::CreateImm(Expr, S, getLexer().getLoc(), *this));
2948   return MatchOperand_Success;
2949 }
2950
2951 MipsAsmParser::OperandMatchResultTy
2952 MipsAsmParser::parseInvNum(OperandVector &Operands) {
2953   MCAsmParser &Parser = getParser();
2954   const MCExpr *IdVal;
2955   // If the first token is '$' we may have register operand.
2956   if (Parser.getTok().is(AsmToken::Dollar))
2957     return MatchOperand_NoMatch;
2958   SMLoc S = Parser.getTok().getLoc();
2959   if (getParser().parseExpression(IdVal))
2960     return MatchOperand_ParseFail;
2961   const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(IdVal);
2962   assert(MCE && "Unexpected MCExpr type.");
2963   int64_t Val = MCE->getValue();
2964   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
2965   Operands.push_back(MipsOperand::CreateImm(
2966       MCConstantExpr::Create(0 - Val, getContext()), S, E, *this));
2967   return MatchOperand_Success;
2968 }
2969
2970 MipsAsmParser::OperandMatchResultTy
2971 MipsAsmParser::parseLSAImm(OperandVector &Operands) {
2972   MCAsmParser &Parser = getParser();
2973   switch (getLexer().getKind()) {
2974   default:
2975     return MatchOperand_NoMatch;
2976   case AsmToken::LParen:
2977   case AsmToken::Plus:
2978   case AsmToken::Minus:
2979   case AsmToken::Integer:
2980     break;
2981   }
2982
2983   const MCExpr *Expr;
2984   SMLoc S = Parser.getTok().getLoc();
2985
2986   if (getParser().parseExpression(Expr))
2987     return MatchOperand_ParseFail;
2988
2989   int64_t Val;
2990   if (!Expr->EvaluateAsAbsolute(Val)) {
2991     Error(S, "expected immediate value");
2992     return MatchOperand_ParseFail;
2993   }
2994
2995   // The LSA instruction allows a 2-bit unsigned immediate. For this reason
2996   // and because the CPU always adds one to the immediate field, the allowed
2997   // range becomes 1..4. We'll only check the range here and will deal
2998   // with the addition/subtraction when actually decoding/encoding
2999   // the instruction.
3000   if (Val < 1 || Val > 4) {
3001     Error(S, "immediate not in range (1..4)");
3002     return MatchOperand_ParseFail;
3003   }
3004
3005   Operands.push_back(
3006       MipsOperand::CreateImm(Expr, S, Parser.getTok().getLoc(), *this));
3007   return MatchOperand_Success;
3008 }
3009
3010 MipsAsmParser::OperandMatchResultTy
3011 MipsAsmParser::parseRegisterList(OperandVector &Operands) {
3012   MCAsmParser &Parser = getParser();
3013   SmallVector<unsigned, 10> Regs;
3014   unsigned RegNo;
3015   unsigned PrevReg = Mips::NoRegister;
3016   bool RegRange = false;
3017   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> TmpOperands;
3018
3019   if (Parser.getTok().isNot(AsmToken::Dollar))
3020     return MatchOperand_ParseFail;
3021
3022   SMLoc S = Parser.getTok().getLoc();
3023   while (parseAnyRegister(TmpOperands) == MatchOperand_Success) {
3024     SMLoc E = getLexer().getLoc();
3025     MipsOperand &Reg = static_cast<MipsOperand &>(*TmpOperands.back());
3026     RegNo = isGP64bit() ? Reg.getGPR64Reg() : Reg.getGPR32Reg();
3027     if (RegRange) {
3028       // Remove last register operand because registers from register range
3029       // should be inserted first.
3030       if (RegNo == Mips::RA) {
3031         Regs.push_back(RegNo);
3032       } else {
3033         unsigned TmpReg = PrevReg + 1;
3034         while (TmpReg <= RegNo) {
3035           if ((TmpReg < Mips::S0) || (TmpReg > Mips::S7)) {
3036             Error(E, "invalid register operand");
3037             return MatchOperand_ParseFail;
3038           }
3039
3040           PrevReg = TmpReg;
3041           Regs.push_back(TmpReg++);
3042         }
3043       }
3044
3045       RegRange = false;
3046     } else {
3047       if ((PrevReg == Mips::NoRegister) && (RegNo != Mips::S0) &&
3048           (RegNo != Mips::RA)) {
3049         Error(E, "$16 or $31 expected");
3050         return MatchOperand_ParseFail;
3051       } else if (((RegNo < Mips::S0) || (RegNo > Mips::S7)) &&
3052                  (RegNo != Mips::FP) && (RegNo != Mips::RA)) {
3053         Error(E, "invalid register operand");
3054         return MatchOperand_ParseFail;
3055       } else if ((PrevReg != Mips::NoRegister) && (RegNo != PrevReg + 1) &&
3056                  (RegNo != Mips::FP) && (RegNo != Mips::RA)) {
3057         Error(E, "consecutive register numbers expected");
3058         return MatchOperand_ParseFail;
3059       }
3060
3061       Regs.push_back(RegNo);
3062     }
3063
3064     if (Parser.getTok().is(AsmToken::Minus))
3065       RegRange = true;
3066
3067     if (!Parser.getTok().isNot(AsmToken::Minus) &&
3068         !Parser.getTok().isNot(AsmToken::Comma)) {
3069       Error(E, "',' or '-' expected");
3070       return MatchOperand_ParseFail;
3071     }
3072
3073     Lex(); // Consume comma or minus
3074     if (Parser.getTok().isNot(AsmToken::Dollar))
3075       break;
3076
3077     PrevReg = RegNo;
3078   }
3079
3080   SMLoc E = Parser.getTok().getLoc();
3081   Operands.push_back(MipsOperand::CreateRegList(Regs, S, E, *this));
3082   parseMemOperand(Operands);
3083   return MatchOperand_Success;
3084 }
3085
3086 MipsAsmParser::OperandMatchResultTy
3087 MipsAsmParser::parseRegisterPair(OperandVector &Operands) {
3088   MCAsmParser &Parser = getParser();
3089
3090   SMLoc S = Parser.getTok().getLoc();
3091   if (parseAnyRegister(Operands) != MatchOperand_Success)
3092     return MatchOperand_ParseFail;
3093
3094   SMLoc E = Parser.getTok().getLoc();
3095   MipsOperand &Op = static_cast<MipsOperand &>(*Operands.back());
3096   unsigned Reg = Op.getGPR32Reg();
3097   Operands.pop_back();
3098   Operands.push_back(MipsOperand::CreateRegPair(Reg, S, E, *this));
3099   return MatchOperand_Success;
3100 }
3101
3102 MipsAsmParser::OperandMatchResultTy
3103 MipsAsmParser::parseMovePRegPair(OperandVector &Operands) {
3104   MCAsmParser &Parser = getParser();
3105   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> TmpOperands;
3106   SmallVector<unsigned, 10> Regs;
3107
3108   if (Parser.getTok().isNot(AsmToken::Dollar))
3109     return MatchOperand_ParseFail;
3110
3111   SMLoc S = Parser.getTok().getLoc();
3112
3113   if (parseAnyRegister(TmpOperands) != MatchOperand_Success)
3114     return MatchOperand_ParseFail;
3115
3116   MipsOperand *Reg = &static_cast<MipsOperand &>(*TmpOperands.back());
3117   unsigned RegNo = isGP64bit() ? Reg->getGPR64Reg() : Reg->getGPR32Reg();
3118   Regs.push_back(RegNo);
3119
3120   SMLoc E = Parser.getTok().getLoc();
3121   if (Parser.getTok().isNot(AsmToken::Comma)) {
3122     Error(E, "',' expected");
3123     return MatchOperand_ParseFail;
3124   }
3125
3126   // Remove comma.
3127   Parser.Lex();
3128
3129   if (parseAnyRegister(TmpOperands) != MatchOperand_Success)
3130     return MatchOperand_ParseFail;
3131
3132   Reg = &static_cast<MipsOperand &>(*TmpOperands.back());
3133   RegNo = isGP64bit() ? Reg->getGPR64Reg() : Reg->getGPR32Reg();
3134   Regs.push_back(RegNo);
3135
3136   Operands.push_back(MipsOperand::CreateRegList(Regs, S, E, *this));
3137
3138   return MatchOperand_Success;
3139 }
3140
3141 MCSymbolRefExpr::VariantKind MipsAsmParser::getVariantKind(StringRef Symbol) {
3142
3143   MCSymbolRefExpr::VariantKind VK =
3144       StringSwitch<MCSymbolRefExpr::VariantKind>(Symbol)
3145           .Case("hi", MCSymbolRefExpr::VK_Mips_ABS_HI)
3146           .Case("lo", MCSymbolRefExpr::VK_Mips_ABS_LO)
3147           .Case("gp_rel", MCSymbolRefExpr::VK_Mips_GPREL)
3148           .Case("call16", MCSymbolRefExpr::VK_Mips_GOT_CALL)
3149           .Case("got", MCSymbolRefExpr::VK_Mips_GOT)
3150           .Case("tlsgd", MCSymbolRefExpr::VK_Mips_TLSGD)
3151           .Case("tlsldm", MCSymbolRefExpr::VK_Mips_TLSLDM)
3152           .Case("dtprel_hi", MCSymbolRefExpr::VK_Mips_DTPREL_HI)
3153           .Case("dtprel_lo", MCSymbolRefExpr::VK_Mips_DTPREL_LO)
3154           .Case("gottprel", MCSymbolRefExpr::VK_Mips_GOTTPREL)
3155           .Case("tprel_hi", MCSymbolRefExpr::VK_Mips_TPREL_HI)
3156           .Case("tprel_lo", MCSymbolRefExpr::VK_Mips_TPREL_LO)
3157           .Case("got_disp", MCSymbolRefExpr::VK_Mips_GOT_DISP)
3158           .Case("got_page", MCSymbolRefExpr::VK_Mips_GOT_PAGE)
3159           .Case("got_ofst", MCSymbolRefExpr::VK_Mips_GOT_OFST)
3160           .Case("hi(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_HI)
3161           .Case("lo(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_LO)
3162           .Case("got_hi", MCSymbolRefExpr::VK_Mips_GOT_HI16)
3163           .Case("got_lo", MCSymbolRefExpr::VK_Mips_GOT_LO16)
3164           .Case("call_hi", MCSymbolRefExpr::VK_Mips_CALL_HI16)
3165           .Case("call_lo", MCSymbolRefExpr::VK_Mips_CALL_LO16)
3166           .Case("higher", MCSymbolRefExpr::VK_Mips_HIGHER)
3167           .Case("highest", MCSymbolRefExpr::VK_Mips_HIGHEST)
3168           .Case("pcrel_hi", MCSymbolRefExpr::VK_Mips_PCREL_HI16)
3169           .Case("pcrel_lo", MCSymbolRefExpr::VK_Mips_PCREL_LO16)
3170           .Default(MCSymbolRefExpr::VK_None);
3171
3172   assert(VK != MCSymbolRefExpr::VK_None);
3173
3174   return VK;
3175 }
3176
3177 /// Sometimes (i.e. load/stores) the operand may be followed immediately by
3178 /// either this.
3179 /// ::= '(', register, ')'
3180 /// handle it before we iterate so we don't get tripped up by the lack of
3181 /// a comma.
3182 bool MipsAsmParser::parseParenSuffix(StringRef Name, OperandVector &Operands) {
3183   MCAsmParser &Parser = getParser();
3184   if (getLexer().is(AsmToken::LParen)) {
3185     Operands.push_back(
3186         MipsOperand::CreateToken("(", getLexer().getLoc(), *this));
3187     Parser.Lex();
3188     if (parseOperand(Operands, Name)) {
3189       SMLoc Loc = getLexer().getLoc();
3190       Parser.eatToEndOfStatement();
3191       return Error(Loc, "unexpected token in argument list");
3192     }
3193     if (Parser.getTok().isNot(AsmToken::RParen)) {
3194       SMLoc Loc = getLexer().getLoc();
3195       Parser.eatToEndOfStatement();
3196       return Error(Loc, "unexpected token, expected ')'");
3197     }
3198     Operands.push_back(
3199         MipsOperand::CreateToken(")", getLexer().getLoc(), *this));
3200     Parser.Lex();
3201   }
3202   return false;
3203 }
3204
3205 /// Sometimes (i.e. in MSA) the operand may be followed immediately by
3206 /// either one of these.
3207 /// ::= '[', register, ']'
3208 /// ::= '[', integer, ']'
3209 /// handle it before we iterate so we don't get tripped up by the lack of
3210 /// a comma.
3211 bool MipsAsmParser::parseBracketSuffix(StringRef Name,
3212                                        OperandVector &Operands) {
3213   MCAsmParser &Parser = getParser();
3214   if (getLexer().is(AsmToken::LBrac)) {
3215     Operands.push_back(
3216         MipsOperand::CreateToken("[", getLexer().getLoc(), *this));
3217     Parser.Lex();
3218     if (parseOperand(Operands, Name)) {
3219       SMLoc Loc = getLexer().getLoc();
3220       Parser.eatToEndOfStatement();
3221       return Error(Loc, "unexpected token in argument list");
3222     }
3223     if (Parser.getTok().isNot(AsmToken::RBrac)) {
3224       SMLoc Loc = getLexer().getLoc();
3225       Parser.eatToEndOfStatement();
3226       return Error(Loc, "unexpected token, expected ']'");
3227     }
3228     Operands.push_back(
3229         MipsOperand::CreateToken("]", getLexer().getLoc(), *this));
3230     Parser.Lex();
3231   }
3232   return false;
3233 }
3234
3235 bool MipsAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
3236                                      SMLoc NameLoc, OperandVector &Operands) {
3237   MCAsmParser &Parser = getParser();
3238   DEBUG(dbgs() << "ParseInstruction\n");
3239
3240   // We have reached first instruction, module directive are now forbidden.
3241   getTargetStreamer().forbidModuleDirective();
3242
3243   // Check if we have valid mnemonic
3244   if (!mnemonicIsValid(Name, 0)) {
3245     Parser.eatToEndOfStatement();
3246     return Error(NameLoc, "unknown instruction");
3247   }
3248   // First operand in MCInst is instruction mnemonic.
3249   Operands.push_back(MipsOperand::CreateToken(Name, NameLoc, *this));
3250
3251   // Read the remaining operands.
3252   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3253     // Read the first operand.
3254     if (parseOperand(Operands, Name)) {
3255       SMLoc Loc = getLexer().getLoc();
3256       Parser.eatToEndOfStatement();
3257       return Error(Loc, "unexpected token in argument list");
3258     }
3259     if (getLexer().is(AsmToken::LBrac) && parseBracketSuffix(Name, Operands))
3260       return true;
3261     // AFAIK, parenthesis suffixes are never on the first operand
3262
3263     while (getLexer().is(AsmToken::Comma)) {
3264       Parser.Lex(); // Eat the comma.
3265       // Parse and remember the operand.
3266       if (parseOperand(Operands, Name)) {
3267         SMLoc Loc = getLexer().getLoc();
3268         Parser.eatToEndOfStatement();
3269         return Error(Loc, "unexpected token in argument list");
3270       }
3271       // Parse bracket and parenthesis suffixes before we iterate
3272       if (getLexer().is(AsmToken::LBrac)) {
3273         if (parseBracketSuffix(Name, Operands))
3274           return true;
3275       } else if (getLexer().is(AsmToken::LParen) &&
3276                  parseParenSuffix(Name, Operands))
3277         return true;
3278     }
3279   }
3280   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3281     SMLoc Loc = getLexer().getLoc();
3282     Parser.eatToEndOfStatement();
3283     return Error(Loc, "unexpected token in argument list");
3284   }
3285   Parser.Lex(); // Consume the EndOfStatement.
3286   return false;
3287 }
3288
3289 bool MipsAsmParser::reportParseError(Twine ErrorMsg) {
3290   MCAsmParser &Parser = getParser();
3291   SMLoc Loc = getLexer().getLoc();
3292   Parser.eatToEndOfStatement();
3293   return Error(Loc, ErrorMsg);
3294 }
3295
3296 bool MipsAsmParser::reportParseError(SMLoc Loc, Twine ErrorMsg) {
3297   return Error(Loc, ErrorMsg);
3298 }
3299
3300 bool MipsAsmParser::parseSetNoAtDirective() {
3301   MCAsmParser &Parser = getParser();
3302   // Line should look like: ".set noat".
3303
3304   // Set the $at register to $0.
3305   AssemblerOptions.back()->setATReg(0);
3306
3307   Parser.Lex(); // Eat "noat".
3308
3309   // If this is not the end of the statement, report an error.
3310   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3311     reportParseError("unexpected token, expected end of statement");
3312     return false;
3313   }
3314
3315   getTargetStreamer().emitDirectiveSetNoAt();
3316   Parser.Lex(); // Consume the EndOfStatement.
3317   return false;
3318 }
3319
3320 bool MipsAsmParser::parseSetAtDirective() {
3321   // Line can be: ".set at", which sets $at to $1
3322   //          or  ".set at=$reg", which sets $at to $reg.
3323   MCAsmParser &Parser = getParser();
3324   Parser.Lex(); // Eat "at".
3325
3326   if (getLexer().is(AsmToken::EndOfStatement)) {
3327     // No register was specified, so we set $at to $1.
3328     AssemblerOptions.back()->setATReg(1);
3329
3330     getTargetStreamer().emitDirectiveSetAt();
3331     Parser.Lex(); // Consume the EndOfStatement.
3332     return false;
3333   }
3334
3335   if (getLexer().isNot(AsmToken::Equal)) {
3336     reportParseError("unexpected token, expected equals sign");
3337     return false;
3338   }
3339   Parser.Lex(); // Eat "=".
3340
3341   if (getLexer().isNot(AsmToken::Dollar)) {
3342     if (getLexer().is(AsmToken::EndOfStatement)) {
3343       reportParseError("no register specified");
3344       return false;
3345     } else {
3346       reportParseError("unexpected token, expected dollar sign '$'");
3347       return false;
3348     }
3349   }
3350   Parser.Lex(); // Eat "$".
3351
3352   // Find out what "reg" is.
3353   unsigned AtRegNo;
3354   const AsmToken &Reg = Parser.getTok();
3355   if (Reg.is(AsmToken::Identifier)) {
3356     AtRegNo = matchCPURegisterName(Reg.getIdentifier());
3357   } else if (Reg.is(AsmToken::Integer)) {
3358     AtRegNo = Reg.getIntVal();
3359   } else {
3360     reportParseError("unexpected token, expected identifier or integer");
3361     return false;
3362   }
3363
3364   // Check if $reg is a valid register. If it is, set $at to $reg.
3365   if (!AssemblerOptions.back()->setATReg(AtRegNo)) {
3366     reportParseError("invalid register");
3367     return false;
3368   }
3369   Parser.Lex(); // Eat "reg".
3370
3371   // If this is not the end of the statement, report an error.
3372   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3373     reportParseError("unexpected token, expected end of statement");
3374     return false;
3375   }
3376
3377   getTargetStreamer().emitDirectiveSetAtWithArg(AtRegNo);
3378
3379   Parser.Lex(); // Consume the EndOfStatement.
3380   return false;
3381 }
3382
3383 bool MipsAsmParser::parseSetReorderDirective() {
3384   MCAsmParser &Parser = getParser();
3385   Parser.Lex();
3386   // If this is not the end of the statement, report an error.
3387   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3388     reportParseError("unexpected token, expected end of statement");
3389     return false;
3390   }
3391   AssemblerOptions.back()->setReorder();
3392   getTargetStreamer().emitDirectiveSetReorder();
3393   Parser.Lex(); // Consume the EndOfStatement.
3394   return false;
3395 }
3396
3397 bool MipsAsmParser::parseSetNoReorderDirective() {
3398   MCAsmParser &Parser = getParser();
3399   Parser.Lex();
3400   // If this is not the end of the statement, report an error.
3401   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3402     reportParseError("unexpected token, expected end of statement");
3403     return false;
3404   }
3405   AssemblerOptions.back()->setNoReorder();
3406   getTargetStreamer().emitDirectiveSetNoReorder();
3407   Parser.Lex(); // Consume the EndOfStatement.
3408   return false;
3409 }
3410
3411 bool MipsAsmParser::parseSetMacroDirective() {
3412   MCAsmParser &Parser = getParser();
3413   Parser.Lex();
3414   // If this is not the end of the statement, report an error.
3415   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3416     reportParseError("unexpected token, expected end of statement");
3417     return false;
3418   }
3419   AssemblerOptions.back()->setMacro();
3420   Parser.Lex(); // Consume the EndOfStatement.
3421   return false;
3422 }
3423
3424 bool MipsAsmParser::parseSetNoMacroDirective() {
3425   MCAsmParser &Parser = getParser();
3426   Parser.Lex();
3427   // If this is not the end of the statement, report an error.
3428   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3429     reportParseError("unexpected token, expected end of statement");
3430     return false;
3431   }
3432   if (AssemblerOptions.back()->isReorder()) {
3433     reportParseError("`noreorder' must be set before `nomacro'");
3434     return false;
3435   }
3436   AssemblerOptions.back()->setNoMacro();
3437   Parser.Lex(); // Consume the EndOfStatement.
3438   return false;
3439 }
3440
3441 bool MipsAsmParser::parseSetMsaDirective() {
3442   MCAsmParser &Parser = getParser();
3443   Parser.Lex();
3444
3445   // If this is not the end of the statement, report an error.
3446   if (getLexer().isNot(AsmToken::EndOfStatement))
3447     return reportParseError("unexpected token, expected end of statement");
3448
3449   setFeatureBits(Mips::FeatureMSA, "msa");
3450   getTargetStreamer().emitDirectiveSetMsa();
3451   return false;
3452 }
3453
3454 bool MipsAsmParser::parseSetNoMsaDirective() {
3455   MCAsmParser &Parser = getParser();
3456   Parser.Lex();
3457
3458   // If this is not the end of the statement, report an error.
3459   if (getLexer().isNot(AsmToken::EndOfStatement))
3460     return reportParseError("unexpected token, expected end of statement");
3461
3462   clearFeatureBits(Mips::FeatureMSA, "msa");
3463   getTargetStreamer().emitDirectiveSetNoMsa();
3464   return false;
3465 }
3466
3467 bool MipsAsmParser::parseSetNoDspDirective() {
3468   MCAsmParser &Parser = getParser();
3469   Parser.Lex(); // Eat "nodsp".
3470
3471   // If this is not the end of the statement, report an error.
3472   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3473     reportParseError("unexpected token, expected end of statement");
3474     return false;
3475   }
3476
3477   clearFeatureBits(Mips::FeatureDSP, "dsp");
3478   getTargetStreamer().emitDirectiveSetNoDsp();
3479   return false;
3480 }
3481
3482 bool MipsAsmParser::parseSetMips16Directive() {
3483   MCAsmParser &Parser = getParser();
3484   Parser.Lex(); // Eat "mips16".
3485
3486   // If this is not the end of the statement, report an error.
3487   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3488     reportParseError("unexpected token, expected end of statement");
3489     return false;
3490   }
3491
3492   setFeatureBits(Mips::FeatureMips16, "mips16");
3493   getTargetStreamer().emitDirectiveSetMips16();
3494   Parser.Lex(); // Consume the EndOfStatement.
3495   return false;
3496 }
3497
3498 bool MipsAsmParser::parseSetNoMips16Directive() {
3499   MCAsmParser &Parser = getParser();
3500   Parser.Lex(); // Eat "nomips16".
3501
3502   // If this is not the end of the statement, report an error.
3503   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3504     reportParseError("unexpected token, expected end of statement");
3505     return false;
3506   }
3507
3508   clearFeatureBits(Mips::FeatureMips16, "mips16");
3509   getTargetStreamer().emitDirectiveSetNoMips16();
3510   Parser.Lex(); // Consume the EndOfStatement.
3511   return false;
3512 }
3513
3514 bool MipsAsmParser::parseSetFpDirective() {
3515   MCAsmParser &Parser = getParser();
3516   MipsABIFlagsSection::FpABIKind FpAbiVal;
3517   // Line can be: .set fp=32
3518   //              .set fp=xx
3519   //              .set fp=64
3520   Parser.Lex(); // Eat fp token
3521   AsmToken Tok = Parser.getTok();
3522   if (Tok.isNot(AsmToken::Equal)) {
3523     reportParseError("unexpected token, expected equals sign '='");
3524     return false;
3525   }
3526   Parser.Lex(); // Eat '=' token.
3527   Tok = Parser.getTok();
3528
3529   if (!parseFpABIValue(FpAbiVal, ".set"))
3530     return false;
3531
3532   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3533     reportParseError("unexpected token, expected end of statement");
3534     return false;
3535   }
3536   getTargetStreamer().emitDirectiveSetFp(FpAbiVal);
3537   Parser.Lex(); // Consume the EndOfStatement.
3538   return false;
3539 }
3540
3541 bool MipsAsmParser::parseSetPopDirective() {
3542   MCAsmParser &Parser = getParser();
3543   SMLoc Loc = getLexer().getLoc();
3544
3545   Parser.Lex();
3546   if (getLexer().isNot(AsmToken::EndOfStatement))
3547     return reportParseError("unexpected token, expected end of statement");
3548
3549   // Always keep an element on the options "stack" to prevent the user
3550   // from changing the initial options. This is how we remember them.
3551   if (AssemblerOptions.size() == 2)
3552     return reportParseError(Loc, ".set pop with no .set push");
3553
3554   AssemblerOptions.pop_back();
3555   setAvailableFeatures(AssemblerOptions.back()->getFeatures());
3556
3557   getTargetStreamer().emitDirectiveSetPop();
3558   return false;
3559 }
3560
3561 bool MipsAsmParser::parseSetPushDirective() {
3562   MCAsmParser &Parser = getParser();
3563   Parser.Lex();
3564   if (getLexer().isNot(AsmToken::EndOfStatement))
3565     return reportParseError("unexpected token, expected end of statement");
3566
3567   // Create a copy of the current assembler options environment and push it.
3568   AssemblerOptions.push_back(
3569               make_unique<MipsAssemblerOptions>(AssemblerOptions.back().get()));
3570
3571   getTargetStreamer().emitDirectiveSetPush();
3572   return false;
3573 }
3574
3575 bool MipsAsmParser::parseSetAssignment() {
3576   StringRef Name;
3577   const MCExpr *Value;
3578   MCAsmParser &Parser = getParser();
3579
3580   if (Parser.parseIdentifier(Name))
3581     reportParseError("expected identifier after .set");
3582
3583   if (getLexer().isNot(AsmToken::Comma))
3584     return reportParseError("unexpected token, expected comma");
3585   Lex(); // Eat comma
3586
3587   if (Parser.parseExpression(Value))
3588     return reportParseError("expected valid expression after comma");
3589
3590   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
3591   Sym->setVariableValue(Value);
3592
3593   return false;
3594 }
3595
3596 bool MipsAsmParser::parseSetMips0Directive() {
3597   MCAsmParser &Parser = getParser();
3598   Parser.Lex();
3599   if (getLexer().isNot(AsmToken::EndOfStatement))
3600     return reportParseError("unexpected token, expected end of statement");
3601
3602   // Reset assembler options to their initial values.
3603   setAvailableFeatures(AssemblerOptions.front()->getFeatures());
3604   AssemblerOptions.back()->setFeatures(AssemblerOptions.front()->getFeatures());
3605
3606   getTargetStreamer().emitDirectiveSetMips0();
3607   return false;
3608 }
3609
3610 bool MipsAsmParser::parseSetArchDirective() {
3611   MCAsmParser &Parser = getParser();
3612   Parser.Lex();
3613   if (getLexer().isNot(AsmToken::Equal))
3614     return reportParseError("unexpected token, expected equals sign");
3615
3616   Parser.Lex();
3617   StringRef Arch;
3618   if (Parser.parseIdentifier(Arch))
3619     return reportParseError("expected arch identifier");
3620
3621   StringRef ArchFeatureName =
3622       StringSwitch<StringRef>(Arch)
3623           .Case("mips1", "mips1")
3624           .Case("mips2", "mips2")
3625           .Case("mips3", "mips3")
3626           .Case("mips4", "mips4")
3627           .Case("mips5", "mips5")
3628           .Case("mips32", "mips32")
3629           .Case("mips32r2", "mips32r2")
3630           .Case("mips32r3", "mips32r3")
3631           .Case("mips32r5", "mips32r5")
3632           .Case("mips32r6", "mips32r6")
3633           .Case("mips64", "mips64")
3634           .Case("mips64r2", "mips64r2")
3635           .Case("mips64r3", "mips64r3")
3636           .Case("mips64r5", "mips64r5")
3637           .Case("mips64r6", "mips64r6")
3638           .Case("cnmips", "cnmips")
3639           .Case("r4000", "mips3") // This is an implementation of Mips3.
3640           .Default("");
3641
3642   if (ArchFeatureName.empty())
3643     return reportParseError("unsupported architecture");
3644
3645   selectArch(ArchFeatureName);
3646   getTargetStreamer().emitDirectiveSetArch(Arch);
3647   return false;
3648 }
3649
3650 bool MipsAsmParser::parseSetFeature(uint64_t Feature) {
3651   MCAsmParser &Parser = getParser();
3652   Parser.Lex();
3653   if (getLexer().isNot(AsmToken::EndOfStatement))
3654     return reportParseError("unexpected token, expected end of statement");
3655
3656   switch (Feature) {
3657   default:
3658     llvm_unreachable("Unimplemented feature");
3659   case Mips::FeatureDSP:
3660     setFeatureBits(Mips::FeatureDSP, "dsp");
3661     getTargetStreamer().emitDirectiveSetDsp();
3662     break;
3663   case Mips::FeatureMicroMips:
3664     getTargetStreamer().emitDirectiveSetMicroMips();
3665     break;
3666   case Mips::FeatureMips1:
3667     selectArch("mips1");
3668     getTargetStreamer().emitDirectiveSetMips1();
3669     break;
3670   case Mips::FeatureMips2:
3671     selectArch("mips2");
3672     getTargetStreamer().emitDirectiveSetMips2();
3673     break;
3674   case Mips::FeatureMips3:
3675     selectArch("mips3");
3676     getTargetStreamer().emitDirectiveSetMips3();
3677     break;
3678   case Mips::FeatureMips4:
3679     selectArch("mips4");
3680     getTargetStreamer().emitDirectiveSetMips4();
3681     break;
3682   case Mips::FeatureMips5:
3683     selectArch("mips5");
3684     getTargetStreamer().emitDirectiveSetMips5();
3685     break;
3686   case Mips::FeatureMips32:
3687     selectArch("mips32");
3688     getTargetStreamer().emitDirectiveSetMips32();
3689     break;
3690   case Mips::FeatureMips32r2:
3691     selectArch("mips32r2");
3692     getTargetStreamer().emitDirectiveSetMips32R2();
3693     break;
3694   case Mips::FeatureMips32r3:
3695     selectArch("mips32r3");
3696     getTargetStreamer().emitDirectiveSetMips32R3();
3697     break;
3698   case Mips::FeatureMips32r5:
3699     selectArch("mips32r5");
3700     getTargetStreamer().emitDirectiveSetMips32R5();
3701     break;
3702   case Mips::FeatureMips32r6:
3703     selectArch("mips32r6");
3704     getTargetStreamer().emitDirectiveSetMips32R6();
3705     break;
3706   case Mips::FeatureMips64:
3707     selectArch("mips64");
3708     getTargetStreamer().emitDirectiveSetMips64();
3709     break;
3710   case Mips::FeatureMips64r2:
3711     selectArch("mips64r2");
3712     getTargetStreamer().emitDirectiveSetMips64R2();
3713     break;
3714   case Mips::FeatureMips64r3:
3715     selectArch("mips64r3");
3716     getTargetStreamer().emitDirectiveSetMips64R3();
3717     break;
3718   case Mips::FeatureMips64r5:
3719     selectArch("mips64r5");
3720     getTargetStreamer().emitDirectiveSetMips64R5();
3721     break;
3722   case Mips::FeatureMips64r6:
3723     selectArch("mips64r6");
3724     getTargetStreamer().emitDirectiveSetMips64R6();
3725     break;
3726   }
3727   return false;
3728 }
3729
3730 bool MipsAsmParser::eatComma(StringRef ErrorStr) {
3731   MCAsmParser &Parser = getParser();
3732   if (getLexer().isNot(AsmToken::Comma)) {
3733     SMLoc Loc = getLexer().getLoc();
3734     Parser.eatToEndOfStatement();
3735     return Error(Loc, ErrorStr);
3736   }
3737
3738   Parser.Lex(); // Eat the comma.
3739   return true;
3740 }
3741
3742 bool MipsAsmParser::parseDirectiveCpLoad(SMLoc Loc) {
3743   if (AssemblerOptions.back()->isReorder())
3744     Warning(Loc, ".cpload should be inside a noreorder section");
3745
3746   if (inMips16Mode()) {
3747     reportParseError(".cpload is not supported in Mips16 mode");
3748     return false;
3749   }
3750
3751   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Reg;
3752   OperandMatchResultTy ResTy = parseAnyRegister(Reg);
3753   if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
3754     reportParseError("expected register containing function address");
3755     return false;
3756   }
3757
3758   MipsOperand &RegOpnd = static_cast<MipsOperand &>(*Reg[0]);
3759   if (!RegOpnd.isGPRAsmReg()) {
3760     reportParseError(RegOpnd.getStartLoc(), "invalid register");
3761     return false;
3762   }
3763
3764   // If this is not the end of the statement, report an error.
3765   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3766     reportParseError("unexpected token, expected end of statement");
3767     return false;
3768   }
3769
3770   getTargetStreamer().emitDirectiveCpLoad(RegOpnd.getGPR32Reg());
3771   return false;
3772 }
3773
3774 bool MipsAsmParser::parseDirectiveCPSetup() {
3775   MCAsmParser &Parser = getParser();
3776   unsigned FuncReg;
3777   unsigned Save;
3778   bool SaveIsReg = true;
3779
3780   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> TmpReg;
3781   OperandMatchResultTy ResTy = parseAnyRegister(TmpReg);
3782   if (ResTy == MatchOperand_NoMatch) {
3783     reportParseError("expected register containing function address");
3784     Parser.eatToEndOfStatement();
3785     return false;
3786   }
3787
3788   MipsOperand &FuncRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
3789   if (!FuncRegOpnd.isGPRAsmReg()) {
3790     reportParseError(FuncRegOpnd.getStartLoc(), "invalid register");
3791     Parser.eatToEndOfStatement();
3792     return false;
3793   }
3794
3795   FuncReg = FuncRegOpnd.getGPR32Reg();
3796   TmpReg.clear();
3797
3798   if (!eatComma("unexpected token, expected comma"))
3799     return true;
3800
3801   ResTy = parseAnyRegister(TmpReg);
3802   if (ResTy == MatchOperand_NoMatch) {
3803     const AsmToken &Tok = Parser.getTok();
3804     if (Tok.is(AsmToken::Integer)) {
3805       Save = Tok.getIntVal();
3806       SaveIsReg = false;
3807       Parser.Lex();
3808     } else {
3809       reportParseError("expected save register or stack offset");
3810       Parser.eatToEndOfStatement();
3811       return false;
3812     }
3813   } else {
3814     MipsOperand &SaveOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
3815     if (!SaveOpnd.isGPRAsmReg()) {
3816       reportParseError(SaveOpnd.getStartLoc(), "invalid register");
3817       Parser.eatToEndOfStatement();
3818       return false;
3819     }
3820     Save = SaveOpnd.getGPR32Reg();
3821   }
3822
3823   if (!eatComma("unexpected token, expected comma"))
3824     return true;
3825
3826   const MCExpr *Expr;
3827   if (Parser.parseExpression(Expr)) {
3828     reportParseError("expected expression");
3829     return false;
3830   }
3831
3832   if (Expr->getKind() != MCExpr::SymbolRef) {
3833     reportParseError("expected symbol");
3834     return false;
3835   }
3836   const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
3837
3838   getTargetStreamer().emitDirectiveCpsetup(FuncReg, Save, Ref->getSymbol(),
3839                                            SaveIsReg);
3840   return false;
3841 }
3842
3843 bool MipsAsmParser::parseDirectiveNaN() {
3844   MCAsmParser &Parser = getParser();
3845   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3846     const AsmToken &Tok = Parser.getTok();
3847
3848     if (Tok.getString() == "2008") {
3849       Parser.Lex();
3850       getTargetStreamer().emitDirectiveNaN2008();
3851       return false;
3852     } else if (Tok.getString() == "legacy") {
3853       Parser.Lex();
3854       getTargetStreamer().emitDirectiveNaNLegacy();
3855       return false;
3856     }
3857   }
3858   // If we don't recognize the option passed to the .nan
3859   // directive (e.g. no option or unknown option), emit an error.
3860   reportParseError("invalid option in .nan directive");
3861   return false;
3862 }
3863
3864 bool MipsAsmParser::parseDirectiveSet() {
3865   MCAsmParser &Parser = getParser();
3866   // Get the next token.
3867   const AsmToken &Tok = Parser.getTok();
3868
3869   if (Tok.getString() == "noat") {
3870     return parseSetNoAtDirective();
3871   } else if (Tok.getString() == "at") {
3872     return parseSetAtDirective();
3873   } else if (Tok.getString() == "arch") {
3874     return parseSetArchDirective();
3875   } else if (Tok.getString() == "fp") {
3876     return parseSetFpDirective();
3877   } else if (Tok.getString() == "pop") {
3878     return parseSetPopDirective();
3879   } else if (Tok.getString() == "push") {
3880     return parseSetPushDirective();
3881   } else if (Tok.getString() == "reorder") {
3882     return parseSetReorderDirective();
3883   } else if (Tok.getString() == "noreorder") {
3884     return parseSetNoReorderDirective();
3885   } else if (Tok.getString() == "macro") {
3886     return parseSetMacroDirective();
3887   } else if (Tok.getString() == "nomacro") {
3888     return parseSetNoMacroDirective();
3889   } else if (Tok.getString() == "mips16") {
3890     return parseSetMips16Directive();
3891   } else if (Tok.getString() == "nomips16") {
3892     return parseSetNoMips16Directive();
3893   } else if (Tok.getString() == "nomicromips") {
3894     getTargetStreamer().emitDirectiveSetNoMicroMips();
3895     Parser.eatToEndOfStatement();
3896     return false;
3897   } else if (Tok.getString() == "micromips") {
3898     return parseSetFeature(Mips::FeatureMicroMips);
3899   } else if (Tok.getString() == "mips0") {
3900     return parseSetMips0Directive();
3901   } else if (Tok.getString() == "mips1") {
3902     return parseSetFeature(Mips::FeatureMips1);
3903   } else if (Tok.getString() == "mips2") {
3904     return parseSetFeature(Mips::FeatureMips2);
3905   } else if (Tok.getString() == "mips3") {
3906     return parseSetFeature(Mips::FeatureMips3);
3907   } else if (Tok.getString() == "mips4") {
3908     return parseSetFeature(Mips::FeatureMips4);
3909   } else if (Tok.getString() == "mips5") {
3910     return parseSetFeature(Mips::FeatureMips5);
3911   } else if (Tok.getString() == "mips32") {
3912     return parseSetFeature(Mips::FeatureMips32);
3913   } else if (Tok.getString() == "mips32r2") {
3914     return parseSetFeature(Mips::FeatureMips32r2);
3915   } else if (Tok.getString() == "mips32r3") {
3916     return parseSetFeature(Mips::FeatureMips32r3);
3917   } else if (Tok.getString() == "mips32r5") {
3918     return parseSetFeature(Mips::FeatureMips32r5);
3919   } else if (Tok.getString() == "mips32r6") {
3920     return parseSetFeature(Mips::FeatureMips32r6);
3921   } else if (Tok.getString() == "mips64") {
3922     return parseSetFeature(Mips::FeatureMips64);
3923   } else if (Tok.getString() == "mips64r2") {
3924     return parseSetFeature(Mips::FeatureMips64r2);
3925   } else if (Tok.getString() == "mips64r3") {
3926     return parseSetFeature(Mips::FeatureMips64r3);
3927   } else if (Tok.getString() == "mips64r5") {
3928     return parseSetFeature(Mips::FeatureMips64r5);
3929   } else if (Tok.getString() == "mips64r6") {
3930     return parseSetFeature(Mips::FeatureMips64r6);
3931   } else if (Tok.getString() == "dsp") {
3932     return parseSetFeature(Mips::FeatureDSP);
3933   } else if (Tok.getString() == "nodsp") {
3934     return parseSetNoDspDirective();
3935   } else if (Tok.getString() == "msa") {
3936     return parseSetMsaDirective();
3937   } else if (Tok.getString() == "nomsa") {
3938     return parseSetNoMsaDirective();
3939   } else {
3940     // It is just an identifier, look for an assignment.
3941     parseSetAssignment();
3942     return false;
3943   }
3944
3945   return true;
3946 }
3947
3948 /// parseDataDirective
3949 ///  ::= .word [ expression (, expression)* ]
3950 bool MipsAsmParser::parseDataDirective(unsigned Size, SMLoc L) {
3951   MCAsmParser &Parser = getParser();
3952   if (getLexer().isNot(AsmToken::EndOfStatement)) {
3953     for (;;) {
3954       const MCExpr *Value;
3955       if (getParser().parseExpression(Value))
3956         return true;
3957
3958       getParser().getStreamer().EmitValue(Value, Size);
3959
3960       if (getLexer().is(AsmToken::EndOfStatement))
3961         break;
3962
3963       if (getLexer().isNot(AsmToken::Comma))
3964         return Error(L, "unexpected token, expected comma");
3965       Parser.Lex();
3966     }
3967   }
3968
3969   Parser.Lex();
3970   return false;
3971 }
3972
3973 /// parseDirectiveGpWord
3974 ///  ::= .gpword local_sym
3975 bool MipsAsmParser::parseDirectiveGpWord() {
3976   MCAsmParser &Parser = getParser();
3977   const MCExpr *Value;
3978   // EmitGPRel32Value requires an expression, so we are using base class
3979   // method to evaluate the expression.
3980   if (getParser().parseExpression(Value))
3981     return true;
3982   getParser().getStreamer().EmitGPRel32Value(Value);
3983
3984   if (getLexer().isNot(AsmToken::EndOfStatement))
3985     return Error(getLexer().getLoc(), 
3986                 "unexpected token, expected end of statement");
3987   Parser.Lex(); // Eat EndOfStatement token.
3988   return false;
3989 }
3990
3991 /// parseDirectiveGpDWord
3992 ///  ::= .gpdword local_sym
3993 bool MipsAsmParser::parseDirectiveGpDWord() {
3994   MCAsmParser &Parser = getParser();
3995   const MCExpr *Value;
3996   // EmitGPRel64Value requires an expression, so we are using base class
3997   // method to evaluate the expression.
3998   if (getParser().parseExpression(Value))
3999     return true;
4000   getParser().getStreamer().EmitGPRel64Value(Value);
4001
4002   if (getLexer().isNot(AsmToken::EndOfStatement))
4003     return Error(getLexer().getLoc(), 
4004                 "unexpected token, expected end of statement");
4005   Parser.Lex(); // Eat EndOfStatement token.
4006   return false;
4007 }
4008
4009 bool MipsAsmParser::parseDirectiveOption() {
4010   MCAsmParser &Parser = getParser();
4011   // Get the option token.
4012   AsmToken Tok = Parser.getTok();
4013   // At the moment only identifiers are supported.
4014   if (Tok.isNot(AsmToken::Identifier)) {
4015     Error(Parser.getTok().getLoc(), "unexpected token, expected identifier");
4016     Parser.eatToEndOfStatement();
4017     return false;
4018   }
4019
4020   StringRef Option = Tok.getIdentifier();
4021
4022   if (Option == "pic0") {
4023     getTargetStreamer().emitDirectiveOptionPic0();
4024     Parser.Lex();
4025     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
4026       Error(Parser.getTok().getLoc(),
4027             "unexpected token, expected end of statement");
4028       Parser.eatToEndOfStatement();
4029     }
4030     return false;
4031   }
4032
4033   if (Option == "pic2") {
4034     getTargetStreamer().emitDirectiveOptionPic2();
4035     Parser.Lex();
4036     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
4037       Error(Parser.getTok().getLoc(),
4038             "unexpected token, expected end of statement");
4039       Parser.eatToEndOfStatement();
4040     }
4041     return false;
4042   }
4043
4044   // Unknown option.
4045   Warning(Parser.getTok().getLoc(), 
4046           "unknown option, expected 'pic0' or 'pic2'");
4047   Parser.eatToEndOfStatement();
4048   return false;
4049 }
4050
4051 /// parseInsnDirective
4052 ///  ::= .insn
4053 bool MipsAsmParser::parseInsnDirective() {
4054   // If this is not the end of the statement, report an error.
4055   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4056     reportParseError("unexpected token, expected end of statement");
4057     return false;
4058   }
4059
4060   // The actual label marking happens in
4061   // MipsELFStreamer::createPendingLabelRelocs().
4062   getTargetStreamer().emitDirectiveInsn();
4063
4064   getParser().Lex(); // Eat EndOfStatement token.
4065   return false;
4066 }
4067
4068 /// parseDirectiveModule
4069 ///  ::= .module oddspreg
4070 ///  ::= .module nooddspreg
4071 ///  ::= .module fp=value
4072 bool MipsAsmParser::parseDirectiveModule() {
4073   MCAsmParser &Parser = getParser();
4074   MCAsmLexer &Lexer = getLexer();
4075   SMLoc L = Lexer.getLoc();
4076
4077   if (!getTargetStreamer().isModuleDirectiveAllowed()) {
4078     // TODO : get a better message.
4079     reportParseError(".module directive must appear before any code");
4080     return false;
4081   }
4082
4083   StringRef Option;
4084   if (Parser.parseIdentifier(Option)) {
4085     reportParseError("expected .module option identifier");
4086     return false;
4087   }
4088
4089   if (Option == "oddspreg") {
4090     getTargetStreamer().emitDirectiveModuleOddSPReg(true, isABI_O32());
4091     clearFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
4092
4093     // If this is not the end of the statement, report an error.
4094     if (getLexer().isNot(AsmToken::EndOfStatement)) {
4095       reportParseError("unexpected token, expected end of statement");
4096       return false;
4097     }
4098
4099     return false; // parseDirectiveModule has finished successfully.
4100   } else if (Option == "nooddspreg") {
4101     if (!isABI_O32()) {
4102       Error(L, "'.module nooddspreg' requires the O32 ABI");
4103       return false;
4104     }
4105
4106     getTargetStreamer().emitDirectiveModuleOddSPReg(false, isABI_O32());
4107     setFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
4108
4109     // If this is not the end of the statement, report an error.
4110     if (getLexer().isNot(AsmToken::EndOfStatement)) {
4111       reportParseError("unexpected token, expected end of statement");
4112       return false;
4113     }
4114
4115     return false; // parseDirectiveModule has finished successfully.
4116   } else if (Option == "fp") {
4117     return parseDirectiveModuleFP();
4118   } else {
4119     return Error(L, "'" + Twine(Option) + "' is not a valid .module option.");
4120   }
4121 }
4122
4123 /// parseDirectiveModuleFP
4124 ///  ::= =32
4125 ///  ::= =xx
4126 ///  ::= =64
4127 bool MipsAsmParser::parseDirectiveModuleFP() {
4128   MCAsmParser &Parser = getParser();
4129   MCAsmLexer &Lexer = getLexer();
4130
4131   if (Lexer.isNot(AsmToken::Equal)) {
4132     reportParseError("unexpected token, expected equals sign '='");
4133     return false;
4134   }
4135   Parser.Lex(); // Eat '=' token.
4136
4137   MipsABIFlagsSection::FpABIKind FpABI;
4138   if (!parseFpABIValue(FpABI, ".module"))
4139     return false;
4140
4141   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4142     reportParseError("unexpected token, expected end of statement");
4143     return false;
4144   }
4145
4146   // Emit appropriate flags.
4147   getTargetStreamer().emitDirectiveModuleFP(FpABI, isABI_O32());
4148   Parser.Lex(); // Consume the EndOfStatement.
4149   return false;
4150 }
4151
4152 bool MipsAsmParser::parseFpABIValue(MipsABIFlagsSection::FpABIKind &FpABI,
4153                                     StringRef Directive) {
4154   MCAsmParser &Parser = getParser();
4155   MCAsmLexer &Lexer = getLexer();
4156
4157   if (Lexer.is(AsmToken::Identifier)) {
4158     StringRef Value = Parser.getTok().getString();
4159     Parser.Lex();
4160
4161     if (Value != "xx") {
4162       reportParseError("unsupported value, expected 'xx', '32' or '64'");
4163       return false;
4164     }
4165
4166     if (!isABI_O32()) {
4167       reportParseError("'" + Directive + " fp=xx' requires the O32 ABI");
4168       return false;
4169     }
4170
4171     FpABI = MipsABIFlagsSection::FpABIKind::XX;
4172     return true;
4173   }
4174
4175   if (Lexer.is(AsmToken::Integer)) {
4176     unsigned Value = Parser.getTok().getIntVal();
4177     Parser.Lex();
4178
4179     if (Value != 32 && Value != 64) {
4180       reportParseError("unsupported value, expected 'xx', '32' or '64'");
4181       return false;
4182     }
4183
4184     if (Value == 32) {
4185       if (!isABI_O32()) {
4186         reportParseError("'" + Directive + " fp=32' requires the O32 ABI");
4187         return false;
4188       }
4189
4190       FpABI = MipsABIFlagsSection::FpABIKind::S32;
4191     } else
4192       FpABI = MipsABIFlagsSection::FpABIKind::S64;
4193
4194     return true;
4195   }
4196
4197   return false;
4198 }
4199
4200 bool MipsAsmParser::ParseDirective(AsmToken DirectiveID) {
4201   MCAsmParser &Parser = getParser();
4202   StringRef IDVal = DirectiveID.getString();
4203
4204   if (IDVal == ".cpload")
4205     return parseDirectiveCpLoad(DirectiveID.getLoc());
4206   if (IDVal == ".dword") {
4207     parseDataDirective(8, DirectiveID.getLoc());
4208     return false;
4209   }
4210   if (IDVal == ".ent") {
4211     StringRef SymbolName;
4212
4213     if (Parser.parseIdentifier(SymbolName)) {
4214       reportParseError("expected identifier after .ent");
4215       return false;
4216     }
4217
4218     // There's an undocumented extension that allows an integer to
4219     // follow the name of the procedure which AFAICS is ignored by GAS.
4220     // Example: .ent foo,2
4221     if (getLexer().isNot(AsmToken::EndOfStatement)) {
4222       if (getLexer().isNot(AsmToken::Comma)) {
4223         // Even though we accept this undocumented extension for compatibility
4224         // reasons, the additional integer argument does not actually change
4225         // the behaviour of the '.ent' directive, so we would like to discourage
4226         // its use. We do this by not referring to the extended version in
4227         // error messages which are not directly related to its use.
4228         reportParseError("unexpected token, expected end of statement");
4229         return false;
4230       }
4231       Parser.Lex(); // Eat the comma.
4232       const MCExpr *DummyNumber;
4233       int64_t DummyNumberVal;
4234       // If the user was explicitly trying to use the extended version,
4235       // we still give helpful extension-related error messages.
4236       if (Parser.parseExpression(DummyNumber)) {
4237         reportParseError("expected number after comma");
4238         return false;
4239       }
4240       if (!DummyNumber->EvaluateAsAbsolute(DummyNumberVal)) {
4241         reportParseError("expected an absolute expression after comma");
4242         return false;
4243       }
4244     }
4245
4246     // If this is not the end of the statement, report an error.
4247     if (getLexer().isNot(AsmToken::EndOfStatement)) {
4248       reportParseError("unexpected token, expected end of statement");
4249       return false;
4250     }
4251
4252     MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName);
4253
4254     getTargetStreamer().emitDirectiveEnt(*Sym);
4255     CurrentFn = Sym;
4256     return false;
4257   }
4258
4259   if (IDVal == ".end") {
4260     StringRef SymbolName;
4261
4262     if (Parser.parseIdentifier(SymbolName)) {
4263       reportParseError("expected identifier after .end");
4264       return false;
4265     }
4266
4267     if (getLexer().isNot(AsmToken::EndOfStatement)) {
4268       reportParseError("unexpected token, expected end of statement");
4269       return false;
4270     }
4271
4272     if (CurrentFn == nullptr) {
4273       reportParseError(".end used without .ent");
4274       return false;
4275     }
4276
4277     if ((SymbolName != CurrentFn->getName())) {
4278       reportParseError(".end symbol does not match .ent symbol");
4279       return false;
4280     }
4281
4282     getTargetStreamer().emitDirectiveEnd(SymbolName);
4283     CurrentFn = nullptr;
4284     return false;
4285   }
4286
4287   if (IDVal == ".frame") {
4288     // .frame $stack_reg, frame_size_in_bytes, $return_reg
4289     SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> TmpReg;
4290     OperandMatchResultTy ResTy = parseAnyRegister(TmpReg);
4291     if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
4292       reportParseError("expected stack register");
4293       return false;
4294     }
4295
4296     MipsOperand &StackRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
4297     if (!StackRegOpnd.isGPRAsmReg()) {
4298       reportParseError(StackRegOpnd.getStartLoc(),
4299                        "expected general purpose register");
4300       return false;
4301     }
4302     unsigned StackReg = StackRegOpnd.getGPR32Reg();
4303
4304     if (Parser.getTok().is(AsmToken::Comma))
4305       Parser.Lex();
4306     else {
4307       reportParseError("unexpected token, expected comma");
4308       return false;
4309     }
4310
4311     // Parse the frame size.
4312     const MCExpr *FrameSize;
4313     int64_t FrameSizeVal;
4314
4315     if (Parser.parseExpression(FrameSize)) {
4316       reportParseError("expected frame size value");
4317       return false;
4318     }
4319
4320     if (!FrameSize->EvaluateAsAbsolute(FrameSizeVal)) {
4321       reportParseError("frame size not an absolute expression");
4322       return false;
4323     }
4324
4325     if (Parser.getTok().is(AsmToken::Comma))
4326       Parser.Lex();
4327     else {
4328       reportParseError("unexpected token, expected comma");
4329       return false;
4330     }
4331
4332     // Parse the return register.
4333     TmpReg.clear();
4334     ResTy = parseAnyRegister(TmpReg);
4335     if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
4336       reportParseError("expected return register");
4337       return false;
4338     }
4339
4340     MipsOperand &ReturnRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
4341     if (!ReturnRegOpnd.isGPRAsmReg()) {
4342       reportParseError(ReturnRegOpnd.getStartLoc(),
4343                        "expected general purpose register");
4344       return false;
4345     }
4346
4347     // If this is not the end of the statement, report an error.
4348     if (getLexer().isNot(AsmToken::EndOfStatement)) {
4349       reportParseError("unexpected token, expected end of statement");
4350       return false;
4351     }
4352
4353     getTargetStreamer().emitFrame(StackReg, FrameSizeVal,
4354                                   ReturnRegOpnd.getGPR32Reg());
4355     return false;
4356   }
4357
4358   if (IDVal == ".set") {
4359     return parseDirectiveSet();
4360   }
4361
4362   if (IDVal == ".mask" || IDVal == ".fmask") {
4363     // .mask bitmask, frame_offset
4364     // bitmask: One bit for each register used.
4365     // frame_offset: Offset from Canonical Frame Address ($sp on entry) where
4366     //               first register is expected to be saved.
4367     // Examples:
4368     //   .mask 0x80000000, -4
4369     //   .fmask 0x80000000, -4
4370     //
4371
4372     // Parse the bitmask
4373     const MCExpr *BitMask;
4374     int64_t BitMaskVal;
4375
4376     if (Parser.parseExpression(BitMask)) {
4377       reportParseError("expected bitmask value");
4378       return false;
4379     }
4380
4381     if (!BitMask->EvaluateAsAbsolute(BitMaskVal)) {
4382       reportParseError("bitmask not an absolute expression");
4383       return false;
4384     }
4385
4386     if (Parser.getTok().is(AsmToken::Comma))
4387       Parser.Lex();
4388     else {
4389       reportParseError("unexpected token, expected comma");
4390       return false;
4391     }
4392
4393     // Parse the frame_offset
4394     const MCExpr *FrameOffset;
4395     int64_t FrameOffsetVal;
4396
4397     if (Parser.parseExpression(FrameOffset)) {
4398       reportParseError("expected frame offset value");
4399       return false;
4400     }
4401
4402     if (!FrameOffset->EvaluateAsAbsolute(FrameOffsetVal)) {
4403       reportParseError("frame offset not an absolute expression");
4404       return false;
4405     }
4406
4407     // If this is not the end of the statement, report an error.
4408     if (getLexer().isNot(AsmToken::EndOfStatement)) {
4409       reportParseError("unexpected token, expected end of statement");
4410       return false;
4411     }
4412
4413     if (IDVal == ".mask")
4414       getTargetStreamer().emitMask(BitMaskVal, FrameOffsetVal);
4415     else
4416       getTargetStreamer().emitFMask(BitMaskVal, FrameOffsetVal);
4417     return false;
4418   }
4419
4420   if (IDVal == ".nan")
4421     return parseDirectiveNaN();
4422
4423   if (IDVal == ".gpword") {
4424     parseDirectiveGpWord();
4425     return false;
4426   }
4427
4428   if (IDVal == ".gpdword") {
4429     parseDirectiveGpDWord();
4430     return false;
4431   }
4432
4433   if (IDVal == ".word") {
4434     parseDataDirective(4, DirectiveID.getLoc());
4435     return false;
4436   }
4437
4438   if (IDVal == ".option")
4439     return parseDirectiveOption();
4440
4441   if (IDVal == ".abicalls") {
4442     getTargetStreamer().emitDirectiveAbiCalls();
4443     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
4444       Error(Parser.getTok().getLoc(), 
4445             "unexpected token, expected end of statement");
4446       // Clear line
4447       Parser.eatToEndOfStatement();
4448     }
4449     return false;
4450   }
4451
4452   if (IDVal == ".cpsetup")
4453     return parseDirectiveCPSetup();
4454
4455   if (IDVal == ".module")
4456     return parseDirectiveModule();
4457
4458   if (IDVal == ".llvm_internal_mips_reallow_module_directive")
4459     return parseInternalDirectiveReallowModule();
4460
4461   if (IDVal == ".insn")
4462     return parseInsnDirective();
4463
4464   return true;
4465 }
4466
4467 bool MipsAsmParser::parseInternalDirectiveReallowModule() {
4468   // If this is not the end of the statement, report an error.
4469   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4470     reportParseError("unexpected token, expected end of statement");
4471     return false;
4472   }
4473
4474   getTargetStreamer().reallowModuleDirective();
4475
4476   getParser().Lex(); // Eat EndOfStatement token.
4477   return false;
4478 }
4479
4480 extern "C" void LLVMInitializeMipsAsmParser() {
4481   RegisterMCAsmParser<MipsAsmParser> X(TheMipsTarget);
4482   RegisterMCAsmParser<MipsAsmParser> Y(TheMipselTarget);
4483   RegisterMCAsmParser<MipsAsmParser> A(TheMips64Target);
4484   RegisterMCAsmParser<MipsAsmParser> B(TheMips64elTarget);
4485 }
4486
4487 #define GET_REGISTER_MATCHER
4488 #define GET_MATCHER_IMPLEMENTATION
4489 #include "MipsGenAsmMatcher.inc"