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