f47104597704ef0abc709ba233e3e9ee23f082c9
[oota-llvm.git] / lib / Target / Mips / AsmParser / MipsAsmParser.cpp
1 //===-- MipsAsmParser.cpp - Parse Mips assembly to MCInst instructions ----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "MCTargetDesc/MipsABIInfo.h"
11 #include "MCTargetDesc/MipsMCExpr.h"
12 #include "MCTargetDesc/MipsMCTargetDesc.h"
13 #include "MipsRegisterInfo.h"
14 #include "MipsTargetObjectFile.h"
15 #include "MipsTargetStreamer.h"
16 #include "llvm/ADT/APInt.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCInstBuilder.h"
23 #include "llvm/MC/MCParser/MCAsmLexer.h"
24 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/MC/MCSubtargetInfo.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/MC/MCTargetAsmParser.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/MathExtras.h"
31 #include "llvm/Support/SourceMgr.h"
32 #include "llvm/Support/TargetRegistry.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include <memory>
35
36 using namespace llvm;
37
38 #define DEBUG_TYPE "mips-asm-parser"
39
40 namespace llvm {
41 class MCInstrInfo;
42 }
43
44 namespace {
45 class MipsAssemblerOptions {
46 public:
47   MipsAssemblerOptions(const FeatureBitset &Features_) :
48     ATReg(1), Reorder(true), Macro(true), Features(Features_) {}
49
50   MipsAssemblerOptions(const MipsAssemblerOptions *Opts) {
51     ATReg = Opts->getATRegIndex();
52     Reorder = Opts->isReorder();
53     Macro = Opts->isMacro();
54     Features = Opts->getFeatures();
55   }
56
57   unsigned getATRegIndex() const { return ATReg; }
58   bool setATRegIndex(unsigned Reg) {
59     if (Reg > 31)
60       return false;
61
62     ATReg = Reg;
63     return true;
64   }
65
66   bool isReorder() const { return Reorder; }
67   void setReorder() { Reorder = true; }
68   void setNoReorder() { Reorder = false; }
69
70   bool isMacro() const { return Macro; }
71   void setMacro() { Macro = true; }
72   void setNoMacro() { Macro = false; }
73
74   const FeatureBitset &getFeatures() const { return Features; }
75   void setFeatures(const FeatureBitset &Features_) { Features = Features_; }
76
77   // Set of features that are either architecture features or referenced
78   // by them (e.g.: FeatureNaN2008 implied by FeatureMips32r6).
79   // The full table can be found in MipsGenSubtargetInfo.inc (MipsFeatureKV[]).
80   // The reason we need this mask is explained in the selectArch function.
81   // FIXME: Ideally we would like TableGen to generate this information.
82   static const FeatureBitset AllArchRelatedMask;
83
84 private:
85   unsigned ATReg;
86   bool Reorder;
87   bool Macro;
88   FeatureBitset Features;
89 };
90 }
91
92 const FeatureBitset MipsAssemblerOptions::AllArchRelatedMask = {
93     Mips::FeatureMips1, Mips::FeatureMips2, Mips::FeatureMips3,
94     Mips::FeatureMips3_32, Mips::FeatureMips3_32r2, Mips::FeatureMips4,
95     Mips::FeatureMips4_32, Mips::FeatureMips4_32r2, Mips::FeatureMips5,
96     Mips::FeatureMips5_32r2, Mips::FeatureMips32, Mips::FeatureMips32r2,
97     Mips::FeatureMips32r3, Mips::FeatureMips32r5, Mips::FeatureMips32r6,
98     Mips::FeatureMips64, Mips::FeatureMips64r2, Mips::FeatureMips64r3,
99     Mips::FeatureMips64r5, Mips::FeatureMips64r6, Mips::FeatureCnMips,
100     Mips::FeatureFP64Bit, Mips::FeatureGP64Bit, Mips::FeatureNaN2008
101 };
102
103 namespace {
104 class MipsAsmParser : public MCTargetAsmParser {
105   MipsTargetStreamer &getTargetStreamer() {
106     MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
107     return static_cast<MipsTargetStreamer &>(TS);
108   }
109
110   MCSubtargetInfo &STI;
111   MipsABIInfo ABI;
112   SmallVector<std::unique_ptr<MipsAssemblerOptions>, 2> AssemblerOptions;
113   MCSymbol *CurrentFn; // Pointer to the function being parsed. It may be a
114                        // nullptr, which indicates that no function is currently
115                        // selected. This usually happens after an '.end func'
116                        // directive.
117   bool IsLittleEndian;
118   bool IsPicEnabled;
119   bool IsCpRestoreSet;
120   int CpRestoreOffset;
121   unsigned CpSaveLocation;
122   /// If true, then CpSaveLocation is a register, otherwise it's an offset.
123   bool     CpSaveLocationIsRegister;
124
125   // Print a warning along with its fix-it message at the given range.
126   void printWarningWithFixIt(const Twine &Msg, const Twine &FixMsg,
127                              SMRange Range, bool ShowColors = true);
128
129 #define GET_ASSEMBLER_HEADER
130 #include "MipsGenAsmMatcher.inc"
131
132   unsigned checkTargetMatchPredicate(MCInst &Inst) override;
133
134   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
135                                OperandVector &Operands, MCStreamer &Out,
136                                uint64_t &ErrorInfo,
137                                bool MatchingInlineAsm) override;
138
139   /// Parse a register as used in CFI directives
140   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
141
142   bool parseParenSuffix(StringRef Name, OperandVector &Operands);
143
144   bool parseBracketSuffix(StringRef Name, OperandVector &Operands);
145
146   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
147                         SMLoc NameLoc, OperandVector &Operands) override;
148
149   bool ParseDirective(AsmToken DirectiveID) override;
150
151   OperandMatchResultTy parseMemOperand(OperandVector &Operands);
152   OperandMatchResultTy
153   matchAnyRegisterNameWithoutDollar(OperandVector &Operands,
154                                     StringRef Identifier, SMLoc S);
155   OperandMatchResultTy matchAnyRegisterWithoutDollar(OperandVector &Operands,
156                                                      SMLoc S);
157   OperandMatchResultTy parseAnyRegister(OperandVector &Operands);
158   OperandMatchResultTy parseImm(OperandVector &Operands);
159   OperandMatchResultTy parseJumpTarget(OperandVector &Operands);
160   OperandMatchResultTy parseInvNum(OperandVector &Operands);
161   OperandMatchResultTy parseLSAImm(OperandVector &Operands);
162   OperandMatchResultTy parseRegisterPair(OperandVector &Operands);
163   OperandMatchResultTy parseMovePRegPair(OperandVector &Operands);
164   OperandMatchResultTy parseRegisterList(OperandVector &Operands);
165
166   bool searchSymbolAlias(OperandVector &Operands);
167
168   bool parseOperand(OperandVector &, StringRef Mnemonic);
169
170   enum MacroExpanderResultTy {
171     MER_NotAMacro,
172     MER_Success,
173     MER_Fail,
174   };
175
176   // Expands assembly pseudo instructions.
177   MacroExpanderResultTy
178   tryExpandInstruction(MCInst &Inst, SMLoc IDLoc,
179                        SmallVectorImpl<MCInst> &Instructions);
180
181   bool expandJalWithRegs(MCInst &Inst, SMLoc IDLoc,
182                          SmallVectorImpl<MCInst> &Instructions);
183
184   bool loadImmediate(int64_t ImmValue, unsigned DstReg, unsigned SrcReg,
185                      bool Is32BitImm, bool IsAddress, SMLoc IDLoc,
186                      SmallVectorImpl<MCInst> &Instructions);
187
188   bool loadAndAddSymbolAddress(const MCExpr *SymExpr, unsigned DstReg,
189                                unsigned SrcReg, bool Is32BitSym, SMLoc IDLoc,
190                                SmallVectorImpl<MCInst> &Instructions);
191
192   bool expandLoadImm(MCInst &Inst, bool Is32BitImm, SMLoc IDLoc,
193                      SmallVectorImpl<MCInst> &Instructions);
194
195   bool expandLoadAddress(unsigned DstReg, unsigned BaseReg,
196                          const MCOperand &Offset, bool Is32BitAddress,
197                          SMLoc IDLoc, SmallVectorImpl<MCInst> &Instructions);
198
199   bool expandUncondBranchMMPseudo(MCInst &Inst, SMLoc IDLoc,
200                                   SmallVectorImpl<MCInst> &Instructions);
201
202   void expandMemInst(MCInst &Inst, SMLoc IDLoc,
203                      SmallVectorImpl<MCInst> &Instructions, bool isLoad,
204                      bool isImmOpnd);
205
206   bool expandLoadStoreMultiple(MCInst &Inst, SMLoc IDLoc,
207                                SmallVectorImpl<MCInst> &Instructions);
208
209   bool expandAliasImmediate(MCInst &Inst, SMLoc IDLoc,
210                           SmallVectorImpl<MCInst> &Instructions);
211
212   bool expandBranchImm(MCInst &Inst, SMLoc IDLoc,
213                        SmallVectorImpl<MCInst> &Instructions);
214
215   bool expandCondBranches(MCInst &Inst, SMLoc IDLoc,
216                           SmallVectorImpl<MCInst> &Instructions);
217
218   bool expandDiv(MCInst &Inst, SMLoc IDLoc,
219                  SmallVectorImpl<MCInst> &Instructions, const bool IsMips64,
220                  const bool Signed);
221
222   bool expandUlh(MCInst &Inst, bool Signed, SMLoc IDLoc,
223                  SmallVectorImpl<MCInst> &Instructions);
224
225   bool expandUlw(MCInst &Inst, SMLoc IDLoc,
226                  SmallVectorImpl<MCInst> &Instructions);
227
228   void createNop(bool hasShortDelaySlot, SMLoc IDLoc,
229                  SmallVectorImpl<MCInst> &Instructions);
230
231   void createAddu(unsigned DstReg, unsigned SrcReg, unsigned TrgReg,
232                   bool Is64Bit, SmallVectorImpl<MCInst> &Instructions);
233
234   void createCpRestoreMemOp(bool IsLoad, int StackOffset, SMLoc IDLoc,
235                             SmallVectorImpl<MCInst> &Instructions);
236
237   bool reportParseError(Twine ErrorMsg);
238   bool reportParseError(SMLoc Loc, Twine ErrorMsg);
239
240   bool parseMemOffset(const MCExpr *&Res, bool isParenExpr);
241   bool parseRelocOperand(const MCExpr *&Res);
242
243   const MCExpr *evaluateRelocExpr(const MCExpr *Expr, StringRef RelocStr);
244
245   bool isEvaluated(const MCExpr *Expr);
246   bool parseSetMips0Directive();
247   bool parseSetArchDirective();
248   bool parseSetFeature(uint64_t Feature);
249   bool isPicAndNotNxxAbi(); // Used by .cpload, .cprestore, and .cpsetup.
250   bool parseDirectiveCpLoad(SMLoc Loc);
251   bool parseDirectiveCpRestore(SMLoc Loc);
252   bool parseDirectiveCPSetup();
253   bool parseDirectiveCPReturn();
254   bool parseDirectiveNaN();
255   bool parseDirectiveSet();
256   bool parseDirectiveOption();
257   bool parseInsnDirective();
258
259   bool parseSetAtDirective();
260   bool parseSetNoAtDirective();
261   bool parseSetMacroDirective();
262   bool parseSetNoMacroDirective();
263   bool parseSetMsaDirective();
264   bool parseSetNoMsaDirective();
265   bool parseSetNoDspDirective();
266   bool parseSetReorderDirective();
267   bool parseSetNoReorderDirective();
268   bool parseSetMips16Directive();
269   bool parseSetNoMips16Directive();
270   bool parseSetFpDirective();
271   bool parseSetOddSPRegDirective();
272   bool parseSetNoOddSPRegDirective();
273   bool parseSetPopDirective();
274   bool parseSetPushDirective();
275   bool parseSetSoftFloatDirective();
276   bool parseSetHardFloatDirective();
277
278   bool parseSetAssignment();
279
280   bool parseDataDirective(unsigned Size, SMLoc L);
281   bool parseDirectiveGpWord();
282   bool parseDirectiveGpDWord();
283   bool parseDirectiveModule();
284   bool parseDirectiveModuleFP();
285   bool parseFpABIValue(MipsABIFlagsSection::FpABIKind &FpABI,
286                        StringRef Directive);
287
288   bool parseInternalDirectiveReallowModule();
289
290   MCSymbolRefExpr::VariantKind getVariantKind(StringRef Symbol);
291
292   bool eatComma(StringRef ErrorStr);
293
294   int matchCPURegisterName(StringRef Symbol);
295
296   int matchHWRegsRegisterName(StringRef Symbol);
297
298   int matchRegisterByNumber(unsigned RegNum, unsigned RegClass);
299
300   int matchFPURegisterName(StringRef Name);
301
302   int matchFCCRegisterName(StringRef Name);
303
304   int matchACRegisterName(StringRef Name);
305
306   int matchMSA128RegisterName(StringRef Name);
307
308   int matchMSA128CtrlRegisterName(StringRef Name);
309
310   unsigned getReg(int RC, int RegNo);
311
312   unsigned getGPR(int RegNo);
313
314   /// Returns the internal register number for the current AT. Also checks if
315   /// the current AT is unavailable (set to $0) and gives an error if it is.
316   /// This should be used in pseudo-instruction expansions which need AT.
317   unsigned getATReg(SMLoc Loc);
318
319   bool processInstruction(MCInst &Inst, SMLoc IDLoc,
320                           SmallVectorImpl<MCInst> &Instructions);
321
322   // Helper function that checks if the value of a vector index is within the
323   // boundaries of accepted values for each RegisterKind
324   // Example: INSERT.B $w0[n], $1 => 16 > n >= 0
325   bool validateMSAIndex(int Val, int RegKind);
326
327   // Selects a new architecture by updating the FeatureBits with the necessary
328   // info including implied dependencies.
329   // Internally, it clears all the feature bits related to *any* architecture
330   // and selects the new one using the ToggleFeature functionality of the
331   // MCSubtargetInfo object that handles implied dependencies. The reason we
332   // clear all the arch related bits manually is because ToggleFeature only
333   // clears the features that imply the feature being cleared and not the
334   // features implied by the feature being cleared. This is easier to see
335   // with an example:
336   //  --------------------------------------------------
337   // | Feature         | Implies                        |
338   // | -------------------------------------------------|
339   // | FeatureMips1    | None                           |
340   // | FeatureMips2    | FeatureMips1                   |
341   // | FeatureMips3    | FeatureMips2 | FeatureMipsGP64 |
342   // | FeatureMips4    | FeatureMips3                   |
343   // | ...             |                                |
344   //  --------------------------------------------------
345   //
346   // Setting Mips3 is equivalent to set: (FeatureMips3 | FeatureMips2 |
347   // FeatureMipsGP64 | FeatureMips1)
348   // Clearing Mips3 is equivalent to clear (FeatureMips3 | FeatureMips4).
349   void selectArch(StringRef ArchFeature) {
350     FeatureBitset FeatureBits = STI.getFeatureBits();
351     FeatureBits &= ~MipsAssemblerOptions::AllArchRelatedMask;
352     STI.setFeatureBits(FeatureBits);
353     setAvailableFeatures(
354         ComputeAvailableFeatures(STI.ToggleFeature(ArchFeature)));
355     AssemblerOptions.back()->setFeatures(STI.getFeatureBits());
356   }
357
358   void setFeatureBits(uint64_t Feature, StringRef FeatureString) {
359     if (!(STI.getFeatureBits()[Feature])) {
360       setAvailableFeatures(
361           ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
362       AssemblerOptions.back()->setFeatures(STI.getFeatureBits());
363     }
364   }
365
366   void clearFeatureBits(uint64_t Feature, StringRef FeatureString) {
367     if (STI.getFeatureBits()[Feature]) {
368       setAvailableFeatures(
369           ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
370       AssemblerOptions.back()->setFeatures(STI.getFeatureBits());
371     }
372   }
373
374   void setModuleFeatureBits(uint64_t Feature, StringRef FeatureString) {
375     setFeatureBits(Feature, FeatureString);
376     AssemblerOptions.front()->setFeatures(STI.getFeatureBits());
377   }
378
379   void clearModuleFeatureBits(uint64_t Feature, StringRef FeatureString) {
380     clearFeatureBits(Feature, FeatureString);
381     AssemblerOptions.front()->setFeatures(STI.getFeatureBits());
382   }
383
384 public:
385   enum MipsMatchResultTy {
386     Match_RequiresDifferentSrcAndDst = FIRST_TARGET_MATCH_RESULT_TY,
387 #define GET_OPERAND_DIAGNOSTIC_TYPES
388 #include "MipsGenAsmMatcher.inc"
389 #undef GET_OPERAND_DIAGNOSTIC_TYPES
390   };
391
392   MipsAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
393                 const MCInstrInfo &MII, const MCTargetOptions &Options)
394       : MCTargetAsmParser(Options), STI(sti),
395         ABI(MipsABIInfo::computeTargetABI(Triple(sti.getTargetTriple()),
396                                           sti.getCPU(), Options)) {
397     MCAsmParserExtension::Initialize(parser);
398
399     parser.addAliasForDirective(".asciiz", ".asciz");
400
401     // Initialize the set of available features.
402     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
403
404     // Remember the initial assembler options. The user can not modify these.
405     AssemblerOptions.push_back(
406         llvm::make_unique<MipsAssemblerOptions>(STI.getFeatureBits()));
407
408     // Create an assembler options environment for the user to modify.
409     AssemblerOptions.push_back(
410         llvm::make_unique<MipsAssemblerOptions>(STI.getFeatureBits()));
411
412     getTargetStreamer().updateABIInfo(*this);
413
414     if (!isABI_O32() && !useOddSPReg() != 0)
415       report_fatal_error("-mno-odd-spreg requires the O32 ABI");
416
417     CurrentFn = nullptr;
418
419     IsPicEnabled =
420         (getContext().getObjectFileInfo()->getRelocM() == Reloc::PIC_);
421
422     IsCpRestoreSet = false;
423     CpRestoreOffset = -1;
424
425     Triple TheTriple(sti.getTargetTriple());
426     if ((TheTriple.getArch() == Triple::mips) ||
427         (TheTriple.getArch() == Triple::mips64))
428       IsLittleEndian = false;
429     else
430       IsLittleEndian = true;
431   }
432
433   /// True if all of $fcc0 - $fcc7 exist for the current ISA.
434   bool hasEightFccRegisters() const { return hasMips4() || hasMips32(); }
435
436   bool isGP64bit() const { return STI.getFeatureBits()[Mips::FeatureGP64Bit]; }
437   bool isFP64bit() const { return STI.getFeatureBits()[Mips::FeatureFP64Bit]; }
438   const MipsABIInfo &getABI() const { return ABI; }
439   bool isABI_N32() const { return ABI.IsN32(); }
440   bool isABI_N64() const { return ABI.IsN64(); }
441   bool isABI_O32() const { return ABI.IsO32(); }
442   bool isABI_FPXX() const { return STI.getFeatureBits()[Mips::FeatureFPXX]; }
443
444   bool useOddSPReg() const {
445     return !(STI.getFeatureBits()[Mips::FeatureNoOddSPReg]);
446   }
447
448   bool inMicroMipsMode() const {
449     return STI.getFeatureBits()[Mips::FeatureMicroMips];
450   }
451   bool hasMips1() const { return STI.getFeatureBits()[Mips::FeatureMips1]; }
452   bool hasMips2() const { return STI.getFeatureBits()[Mips::FeatureMips2]; }
453   bool hasMips3() const { return STI.getFeatureBits()[Mips::FeatureMips3]; }
454   bool hasMips4() const { return STI.getFeatureBits()[Mips::FeatureMips4]; }
455   bool hasMips5() const { return STI.getFeatureBits()[Mips::FeatureMips5]; }
456   bool hasMips32() const {
457     return STI.getFeatureBits()[Mips::FeatureMips32];
458   }
459   bool hasMips64() const {
460     return STI.getFeatureBits()[Mips::FeatureMips64];
461   }
462   bool hasMips32r2() const {
463     return STI.getFeatureBits()[Mips::FeatureMips32r2];
464   }
465   bool hasMips64r2() const {
466     return STI.getFeatureBits()[Mips::FeatureMips64r2];
467   }
468   bool hasMips32r3() const {
469     return (STI.getFeatureBits()[Mips::FeatureMips32r3]);
470   }
471   bool hasMips64r3() const {
472     return (STI.getFeatureBits()[Mips::FeatureMips64r3]);
473   }
474   bool hasMips32r5() const {
475     return (STI.getFeatureBits()[Mips::FeatureMips32r5]);
476   }
477   bool hasMips64r5() const {
478     return (STI.getFeatureBits()[Mips::FeatureMips64r5]);
479   }
480   bool hasMips32r6() const {
481     return STI.getFeatureBits()[Mips::FeatureMips32r6];
482   }
483   bool hasMips64r6() const {
484     return STI.getFeatureBits()[Mips::FeatureMips64r6];
485   }
486
487   bool hasDSP() const { return STI.getFeatureBits()[Mips::FeatureDSP]; }
488   bool hasDSPR2() const { return STI.getFeatureBits()[Mips::FeatureDSPR2]; }
489   bool hasDSPR3() const { return STI.getFeatureBits()[Mips::FeatureDSPR3]; }
490   bool hasMSA() const { return STI.getFeatureBits()[Mips::FeatureMSA]; }
491   bool hasCnMips() const {
492     return (STI.getFeatureBits()[Mips::FeatureCnMips]);
493   }
494
495   bool inPicMode() {
496     return IsPicEnabled;
497   }
498
499   bool inMips16Mode() const {
500     return STI.getFeatureBits()[Mips::FeatureMips16];
501   }
502
503   bool useTraps() const {
504     return STI.getFeatureBits()[Mips::FeatureUseTCCInDIV];
505   }
506
507   bool useSoftFloat() const {
508     return STI.getFeatureBits()[Mips::FeatureSoftFloat];
509   }
510
511   /// Warn if RegIndex is the same as the current AT.
512   void warnIfRegIndexIsAT(unsigned RegIndex, SMLoc Loc);
513
514   void warnIfNoMacro(SMLoc Loc);
515
516   bool isLittle() const { return IsLittleEndian; }
517 };
518 }
519
520 namespace {
521
522 /// MipsOperand - Instances of this class represent a parsed Mips machine
523 /// instruction.
524 class MipsOperand : public MCParsedAsmOperand {
525 public:
526   /// Broad categories of register classes
527   /// The exact class is finalized by the render method.
528   enum RegKind {
529     RegKind_GPR = 1,      /// GPR32 and GPR64 (depending on isGP64bit())
530     RegKind_FGR = 2,      /// FGR32, FGR64, AFGR64 (depending on context and
531                           /// isFP64bit())
532     RegKind_FCC = 4,      /// FCC
533     RegKind_MSA128 = 8,   /// MSA128[BHWD] (makes no difference which)
534     RegKind_MSACtrl = 16, /// MSA control registers
535     RegKind_COP2 = 32,    /// COP2
536     RegKind_ACC = 64,     /// HI32DSP, LO32DSP, and ACC64DSP (depending on
537                           /// context).
538     RegKind_CCR = 128,    /// CCR
539     RegKind_HWRegs = 256, /// HWRegs
540     RegKind_COP3 = 512,   /// COP3
541     RegKind_COP0 = 1024,  /// COP0
542     /// Potentially any (e.g. $1)
543     RegKind_Numeric = RegKind_GPR | RegKind_FGR | RegKind_FCC | RegKind_MSA128 |
544                       RegKind_MSACtrl | RegKind_COP2 | RegKind_ACC |
545                       RegKind_CCR | RegKind_HWRegs | RegKind_COP3 | RegKind_COP0
546   };
547
548 private:
549   enum KindTy {
550     k_Immediate,     /// An immediate (possibly involving symbol references)
551     k_Memory,        /// Base + Offset Memory Address
552     k_PhysRegister,  /// A physical register from the Mips namespace
553     k_RegisterIndex, /// A register index in one or more RegKind.
554     k_Token,         /// A simple token
555     k_RegList,       /// A physical register list
556     k_RegPair        /// A pair of physical register
557   } Kind;
558
559 public:
560   MipsOperand(KindTy K, MipsAsmParser &Parser)
561       : MCParsedAsmOperand(), Kind(K), AsmParser(Parser) {}
562
563 private:
564   /// For diagnostics, and checking the assembler temporary
565   MipsAsmParser &AsmParser;
566
567   struct Token {
568     const char *Data;
569     unsigned Length;
570   };
571
572   struct PhysRegOp {
573     unsigned Num; /// Register Number
574   };
575
576   struct RegIdxOp {
577     unsigned Index; /// Index into the register class
578     RegKind Kind;   /// Bitfield of the kinds it could possibly be
579     const MCRegisterInfo *RegInfo;
580   };
581
582   struct ImmOp {
583     const MCExpr *Val;
584   };
585
586   struct MemOp {
587     MipsOperand *Base;
588     const MCExpr *Off;
589   };
590
591   struct RegListOp {
592     SmallVector<unsigned, 10> *List;
593   };
594
595   union {
596     struct Token Tok;
597     struct PhysRegOp PhysReg;
598     struct RegIdxOp RegIdx;
599     struct ImmOp Imm;
600     struct MemOp Mem;
601     struct RegListOp RegList;
602   };
603
604   SMLoc StartLoc, EndLoc;
605
606   /// Internal constructor for register kinds
607   static std::unique_ptr<MipsOperand> CreateReg(unsigned Index, RegKind RegKind,
608                                                 const MCRegisterInfo *RegInfo,
609                                                 SMLoc S, SMLoc E,
610                                                 MipsAsmParser &Parser) {
611     auto Op = make_unique<MipsOperand>(k_RegisterIndex, Parser);
612     Op->RegIdx.Index = Index;
613     Op->RegIdx.RegInfo = RegInfo;
614     Op->RegIdx.Kind = RegKind;
615     Op->StartLoc = S;
616     Op->EndLoc = E;
617     return Op;
618   }
619
620 public:
621   /// Coerce the register to GPR32 and return the real register for the current
622   /// target.
623   unsigned getGPR32Reg() const {
624     assert(isRegIdx() && (RegIdx.Kind & RegKind_GPR) && "Invalid access!");
625     AsmParser.warnIfRegIndexIsAT(RegIdx.Index, StartLoc);
626     unsigned ClassID = Mips::GPR32RegClassID;
627     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
628   }
629
630   /// Coerce the register to GPR32 and return the real register for the current
631   /// target.
632   unsigned getGPRMM16Reg() const {
633     assert(isRegIdx() && (RegIdx.Kind & RegKind_GPR) && "Invalid access!");
634     unsigned ClassID = Mips::GPR32RegClassID;
635     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
636   }
637
638   /// Coerce the register to GPR64 and return the real register for the current
639   /// target.
640   unsigned getGPR64Reg() const {
641     assert(isRegIdx() && (RegIdx.Kind & RegKind_GPR) && "Invalid access!");
642     unsigned ClassID = Mips::GPR64RegClassID;
643     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
644   }
645
646 private:
647   /// Coerce the register to AFGR64 and return the real register for the current
648   /// target.
649   unsigned getAFGR64Reg() const {
650     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
651     if (RegIdx.Index % 2 != 0)
652       AsmParser.Warning(StartLoc, "Float register should be even.");
653     return RegIdx.RegInfo->getRegClass(Mips::AFGR64RegClassID)
654         .getRegister(RegIdx.Index / 2);
655   }
656
657   /// Coerce the register to FGR64 and return the real register for the current
658   /// target.
659   unsigned getFGR64Reg() const {
660     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
661     return RegIdx.RegInfo->getRegClass(Mips::FGR64RegClassID)
662         .getRegister(RegIdx.Index);
663   }
664
665   /// Coerce the register to FGR32 and return the real register for the current
666   /// target.
667   unsigned getFGR32Reg() const {
668     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
669     return RegIdx.RegInfo->getRegClass(Mips::FGR32RegClassID)
670         .getRegister(RegIdx.Index);
671   }
672
673   /// Coerce the register to FGRH32 and return the real register for the current
674   /// target.
675   unsigned getFGRH32Reg() const {
676     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
677     return RegIdx.RegInfo->getRegClass(Mips::FGRH32RegClassID)
678         .getRegister(RegIdx.Index);
679   }
680
681   /// Coerce the register to FCC and return the real register for the current
682   /// target.
683   unsigned getFCCReg() const {
684     assert(isRegIdx() && (RegIdx.Kind & RegKind_FCC) && "Invalid access!");
685     return RegIdx.RegInfo->getRegClass(Mips::FCCRegClassID)
686         .getRegister(RegIdx.Index);
687   }
688
689   /// Coerce the register to MSA128 and return the real register for the current
690   /// target.
691   unsigned getMSA128Reg() const {
692     assert(isRegIdx() && (RegIdx.Kind & RegKind_MSA128) && "Invalid access!");
693     // It doesn't matter which of the MSA128[BHWD] classes we use. They are all
694     // identical
695     unsigned ClassID = Mips::MSA128BRegClassID;
696     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
697   }
698
699   /// Coerce the register to MSACtrl and return the real register for the
700   /// current target.
701   unsigned getMSACtrlReg() const {
702     assert(isRegIdx() && (RegIdx.Kind & RegKind_MSACtrl) && "Invalid access!");
703     unsigned ClassID = Mips::MSACtrlRegClassID;
704     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
705   }
706
707   /// Coerce the register to COP0 and return the real register for the
708   /// current target.
709   unsigned getCOP0Reg() const {
710     assert(isRegIdx() && (RegIdx.Kind & RegKind_COP0) && "Invalid access!");
711     unsigned ClassID = Mips::COP0RegClassID;
712     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
713   }
714
715   /// Coerce the register to COP2 and return the real register for the
716   /// current target.
717   unsigned getCOP2Reg() const {
718     assert(isRegIdx() && (RegIdx.Kind & RegKind_COP2) && "Invalid access!");
719     unsigned ClassID = Mips::COP2RegClassID;
720     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
721   }
722
723   /// Coerce the register to COP3 and return the real register for the
724   /// current target.
725   unsigned getCOP3Reg() const {
726     assert(isRegIdx() && (RegIdx.Kind & RegKind_COP3) && "Invalid access!");
727     unsigned ClassID = Mips::COP3RegClassID;
728     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
729   }
730
731   /// Coerce the register to ACC64DSP and return the real register for the
732   /// current target.
733   unsigned getACC64DSPReg() const {
734     assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!");
735     unsigned ClassID = Mips::ACC64DSPRegClassID;
736     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
737   }
738
739   /// Coerce the register to HI32DSP and return the real register for the
740   /// current target.
741   unsigned getHI32DSPReg() const {
742     assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!");
743     unsigned ClassID = Mips::HI32DSPRegClassID;
744     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
745   }
746
747   /// Coerce the register to LO32DSP and return the real register for the
748   /// current target.
749   unsigned getLO32DSPReg() const {
750     assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!");
751     unsigned ClassID = Mips::LO32DSPRegClassID;
752     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
753   }
754
755   /// Coerce the register to CCR and return the real register for the
756   /// current target.
757   unsigned getCCRReg() const {
758     assert(isRegIdx() && (RegIdx.Kind & RegKind_CCR) && "Invalid access!");
759     unsigned ClassID = Mips::CCRRegClassID;
760     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
761   }
762
763   /// Coerce the register to HWRegs and return the real register for the
764   /// current target.
765   unsigned getHWRegsReg() const {
766     assert(isRegIdx() && (RegIdx.Kind & RegKind_HWRegs) && "Invalid access!");
767     unsigned ClassID = Mips::HWRegsRegClassID;
768     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
769   }
770
771 public:
772   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
773     // Add as immediate when possible.  Null MCExpr = 0.
774     if (!Expr)
775       Inst.addOperand(MCOperand::createImm(0));
776     else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
777       Inst.addOperand(MCOperand::createImm(CE->getValue()));
778     else
779       Inst.addOperand(MCOperand::createExpr(Expr));
780   }
781
782   void addRegOperands(MCInst &Inst, unsigned N) const {
783     llvm_unreachable("Use a custom parser instead");
784   }
785
786   /// Render the operand to an MCInst as a GPR32
787   /// Asserts if the wrong number of operands are requested, or the operand
788   /// is not a k_RegisterIndex compatible with RegKind_GPR
789   void addGPR32AsmRegOperands(MCInst &Inst, unsigned N) const {
790     assert(N == 1 && "Invalid number of operands!");
791     Inst.addOperand(MCOperand::createReg(getGPR32Reg()));
792   }
793
794   void addGPRMM16AsmRegOperands(MCInst &Inst, unsigned N) const {
795     assert(N == 1 && "Invalid number of operands!");
796     Inst.addOperand(MCOperand::createReg(getGPRMM16Reg()));
797   }
798
799   void addGPRMM16AsmRegZeroOperands(MCInst &Inst, unsigned N) const {
800     assert(N == 1 && "Invalid number of operands!");
801     Inst.addOperand(MCOperand::createReg(getGPRMM16Reg()));
802   }
803
804   void addGPRMM16AsmRegMovePOperands(MCInst &Inst, unsigned N) const {
805     assert(N == 1 && "Invalid number of operands!");
806     Inst.addOperand(MCOperand::createReg(getGPRMM16Reg()));
807   }
808
809   /// Render the operand to an MCInst as a GPR64
810   /// Asserts if the wrong number of operands are requested, or the operand
811   /// is not a k_RegisterIndex compatible with RegKind_GPR
812   void addGPR64AsmRegOperands(MCInst &Inst, unsigned N) const {
813     assert(N == 1 && "Invalid number of operands!");
814     Inst.addOperand(MCOperand::createReg(getGPR64Reg()));
815   }
816
817   void addAFGR64AsmRegOperands(MCInst &Inst, unsigned N) const {
818     assert(N == 1 && "Invalid number of operands!");
819     Inst.addOperand(MCOperand::createReg(getAFGR64Reg()));
820   }
821
822   void addFGR64AsmRegOperands(MCInst &Inst, unsigned N) const {
823     assert(N == 1 && "Invalid number of operands!");
824     Inst.addOperand(MCOperand::createReg(getFGR64Reg()));
825   }
826
827   void addFGR32AsmRegOperands(MCInst &Inst, unsigned N) const {
828     assert(N == 1 && "Invalid number of operands!");
829     Inst.addOperand(MCOperand::createReg(getFGR32Reg()));
830     // FIXME: We ought to do this for -integrated-as without -via-file-asm too.
831     if (!AsmParser.useOddSPReg() && RegIdx.Index & 1)
832       AsmParser.Error(StartLoc, "-mno-odd-spreg prohibits the use of odd FPU "
833                                 "registers");
834   }
835
836   void addFGRH32AsmRegOperands(MCInst &Inst, unsigned N) const {
837     assert(N == 1 && "Invalid number of operands!");
838     Inst.addOperand(MCOperand::createReg(getFGRH32Reg()));
839   }
840
841   void addFCCAsmRegOperands(MCInst &Inst, unsigned N) const {
842     assert(N == 1 && "Invalid number of operands!");
843     Inst.addOperand(MCOperand::createReg(getFCCReg()));
844   }
845
846   void addMSA128AsmRegOperands(MCInst &Inst, unsigned N) const {
847     assert(N == 1 && "Invalid number of operands!");
848     Inst.addOperand(MCOperand::createReg(getMSA128Reg()));
849   }
850
851   void addMSACtrlAsmRegOperands(MCInst &Inst, unsigned N) const {
852     assert(N == 1 && "Invalid number of operands!");
853     Inst.addOperand(MCOperand::createReg(getMSACtrlReg()));
854   }
855
856   void addCOP0AsmRegOperands(MCInst &Inst, unsigned N) const {
857     assert(N == 1 && "Invalid number of operands!");
858     Inst.addOperand(MCOperand::createReg(getCOP0Reg()));
859   }
860
861   void addCOP2AsmRegOperands(MCInst &Inst, unsigned N) const {
862     assert(N == 1 && "Invalid number of operands!");
863     Inst.addOperand(MCOperand::createReg(getCOP2Reg()));
864   }
865
866   void addCOP3AsmRegOperands(MCInst &Inst, unsigned N) const {
867     assert(N == 1 && "Invalid number of operands!");
868     Inst.addOperand(MCOperand::createReg(getCOP3Reg()));
869   }
870
871   void addACC64DSPAsmRegOperands(MCInst &Inst, unsigned N) const {
872     assert(N == 1 && "Invalid number of operands!");
873     Inst.addOperand(MCOperand::createReg(getACC64DSPReg()));
874   }
875
876   void addHI32DSPAsmRegOperands(MCInst &Inst, unsigned N) const {
877     assert(N == 1 && "Invalid number of operands!");
878     Inst.addOperand(MCOperand::createReg(getHI32DSPReg()));
879   }
880
881   void addLO32DSPAsmRegOperands(MCInst &Inst, unsigned N) const {
882     assert(N == 1 && "Invalid number of operands!");
883     Inst.addOperand(MCOperand::createReg(getLO32DSPReg()));
884   }
885
886   void addCCRAsmRegOperands(MCInst &Inst, unsigned N) const {
887     assert(N == 1 && "Invalid number of operands!");
888     Inst.addOperand(MCOperand::createReg(getCCRReg()));
889   }
890
891   void addHWRegsAsmRegOperands(MCInst &Inst, unsigned N) const {
892     assert(N == 1 && "Invalid number of operands!");
893     Inst.addOperand(MCOperand::createReg(getHWRegsReg()));
894   }
895
896   template <unsigned Bits, int Offset = 0>
897   void addConstantUImmOperands(MCInst &Inst, unsigned N) const {
898     assert(N == 1 && "Invalid number of operands!");
899     uint64_t Imm = getConstantImm() - Offset;
900     Imm &= (1 << Bits) - 1;
901     Imm += Offset;
902     Inst.addOperand(MCOperand::createImm(Imm));
903   }
904
905   void addImmOperands(MCInst &Inst, unsigned N) const {
906     assert(N == 1 && "Invalid number of operands!");
907     const MCExpr *Expr = getImm();
908     addExpr(Inst, Expr);
909   }
910
911   void addMemOperands(MCInst &Inst, unsigned N) const {
912     assert(N == 2 && "Invalid number of operands!");
913
914     Inst.addOperand(MCOperand::createReg(AsmParser.getABI().ArePtrs64bit()
915                                              ? getMemBase()->getGPR64Reg()
916                                              : getMemBase()->getGPR32Reg()));
917
918     const MCExpr *Expr = getMemOff();
919     addExpr(Inst, Expr);
920   }
921
922   void addMicroMipsMemOperands(MCInst &Inst, unsigned N) const {
923     assert(N == 2 && "Invalid number of operands!");
924
925     Inst.addOperand(MCOperand::createReg(getMemBase()->getGPRMM16Reg()));
926
927     const MCExpr *Expr = getMemOff();
928     addExpr(Inst, Expr);
929   }
930
931   void addRegListOperands(MCInst &Inst, unsigned N) const {
932     assert(N == 1 && "Invalid number of operands!");
933
934     for (auto RegNo : getRegList())
935       Inst.addOperand(MCOperand::createReg(RegNo));
936   }
937
938   void addRegPairOperands(MCInst &Inst, unsigned N) const {
939     assert(N == 2 && "Invalid number of operands!");
940     unsigned RegNo = getRegPair();
941     Inst.addOperand(MCOperand::createReg(RegNo++));
942     Inst.addOperand(MCOperand::createReg(RegNo));
943   }
944
945   void addMovePRegPairOperands(MCInst &Inst, unsigned N) const {
946     assert(N == 2 && "Invalid number of operands!");
947     for (auto RegNo : getRegList())
948       Inst.addOperand(MCOperand::createReg(RegNo));
949   }
950
951   bool isReg() const override {
952     // As a special case until we sort out the definition of div/divu, pretend
953     // that $0/$zero are k_PhysRegister so that MCK_ZERO works correctly.
954     if (isGPRAsmReg() && RegIdx.Index == 0)
955       return true;
956
957     return Kind == k_PhysRegister;
958   }
959   bool isRegIdx() const { return Kind == k_RegisterIndex; }
960   bool isImm() const override { return Kind == k_Immediate; }
961   bool isConstantImm() const {
962     return isImm() && dyn_cast<MCConstantExpr>(getImm());
963   }
964   bool isConstantImmz() const {
965     return isConstantImm() && getConstantImm() == 0;
966   }
967   template <unsigned Bits, int Offset = 0> bool isConstantUImm() const {
968     return isConstantImm() && isUInt<Bits>(getConstantImm() - Offset);
969   }
970   template <unsigned Bits> bool isUImm() const {
971     return isImm() && isConstantImm() && isUInt<Bits>(getConstantImm());
972   }
973   bool isToken() const override {
974     // Note: It's not possible to pretend that other operand kinds are tokens.
975     // The matcher emitter checks tokens first.
976     return Kind == k_Token;
977   }
978   bool isMem() const override { return Kind == k_Memory; }
979   bool isConstantMemOff() const {
980     return isMem() && dyn_cast<MCConstantExpr>(getMemOff());
981   }
982   template <unsigned Bits> bool isMemWithSimmOffset() const {
983     return isMem() && isConstantMemOff() && isInt<Bits>(getConstantMemOff())
984       && getMemBase()->isGPRAsmReg();
985   }
986   template <unsigned Bits> bool isMemWithSimmOffsetGPR() const {
987     return isMem() && isConstantMemOff() && isInt<Bits>(getConstantMemOff()) &&
988            getMemBase()->isGPRAsmReg();
989   }
990   bool isMemWithGRPMM16Base() const {
991     return isMem() && getMemBase()->isMM16AsmReg();
992   }
993   template <unsigned Bits> bool isMemWithUimmOffsetSP() const {
994     return isMem() && isConstantMemOff() && isUInt<Bits>(getConstantMemOff())
995       && getMemBase()->isRegIdx() && (getMemBase()->getGPR32Reg() == Mips::SP);
996   }
997   template <unsigned Bits> bool isMemWithUimmWordAlignedOffsetSP() const {
998     return isMem() && isConstantMemOff() && isUInt<Bits>(getConstantMemOff())
999       && (getConstantMemOff() % 4 == 0) && getMemBase()->isRegIdx()
1000       && (getMemBase()->getGPR32Reg() == Mips::SP);
1001   }
1002   bool isUImm5Lsl2() const {
1003     return (isImm() && isConstantImm() && isShiftedUInt<5, 2>(getConstantImm()));
1004   }
1005   bool isRegList16() const {
1006     if (!isRegList())
1007       return false;
1008
1009     int Size = RegList.List->size();
1010     if (Size < 2 || Size > 5)
1011       return false;
1012
1013     unsigned R0 = RegList.List->front();
1014     unsigned R1 = RegList.List->back();
1015     if (!((R0 == Mips::S0 && R1 == Mips::RA) ||
1016           (R0 == Mips::S0_64 && R1 == Mips::RA_64)))
1017       return false;
1018
1019     int PrevReg = *RegList.List->begin();
1020     for (int i = 1; i < Size - 1; i++) {
1021       int Reg = (*(RegList.List))[i];
1022       if ( Reg != PrevReg + 1)
1023         return false;
1024       PrevReg = Reg;
1025     }
1026
1027     return true;
1028   }
1029   bool isInvNum() const { return Kind == k_Immediate; }
1030   bool isLSAImm() const {
1031     if (!isConstantImm())
1032       return false;
1033     int64_t Val = getConstantImm();
1034     return 1 <= Val && Val <= 4;
1035   }
1036   bool isRegList() const { return Kind == k_RegList; }
1037   bool isMovePRegPair() const {
1038     if (Kind != k_RegList || RegList.List->size() != 2)
1039       return false;
1040
1041     unsigned R0 = RegList.List->front();
1042     unsigned R1 = RegList.List->back();
1043
1044     if ((R0 == Mips::A1 && R1 == Mips::A2) ||
1045         (R0 == Mips::A1 && R1 == Mips::A3) ||
1046         (R0 == Mips::A2 && R1 == Mips::A3) ||
1047         (R0 == Mips::A0 && R1 == Mips::S5) ||
1048         (R0 == Mips::A0 && R1 == Mips::S6) ||
1049         (R0 == Mips::A0 && R1 == Mips::A1) ||
1050         (R0 == Mips::A0 && R1 == Mips::A2) ||
1051         (R0 == Mips::A0 && R1 == Mips::A3))
1052       return true;
1053
1054     return false;
1055   }
1056
1057   StringRef getToken() const {
1058     assert(Kind == k_Token && "Invalid access!");
1059     return StringRef(Tok.Data, Tok.Length);
1060   }
1061   bool isRegPair() const { return Kind == k_RegPair; }
1062
1063   unsigned getReg() const override {
1064     // As a special case until we sort out the definition of div/divu, pretend
1065     // that $0/$zero are k_PhysRegister so that MCK_ZERO works correctly.
1066     if (Kind == k_RegisterIndex && RegIdx.Index == 0 &&
1067         RegIdx.Kind & RegKind_GPR)
1068       return getGPR32Reg(); // FIXME: GPR64 too
1069
1070     assert(Kind == k_PhysRegister && "Invalid access!");
1071     return PhysReg.Num;
1072   }
1073
1074   const MCExpr *getImm() const {
1075     assert((Kind == k_Immediate) && "Invalid access!");
1076     return Imm.Val;
1077   }
1078
1079   int64_t getConstantImm() const {
1080     const MCExpr *Val = getImm();
1081     return static_cast<const MCConstantExpr *>(Val)->getValue();
1082   }
1083
1084   MipsOperand *getMemBase() const {
1085     assert((Kind == k_Memory) && "Invalid access!");
1086     return Mem.Base;
1087   }
1088
1089   const MCExpr *getMemOff() const {
1090     assert((Kind == k_Memory) && "Invalid access!");
1091     return Mem.Off;
1092   }
1093
1094   int64_t getConstantMemOff() const {
1095     return static_cast<const MCConstantExpr *>(getMemOff())->getValue();
1096   }
1097
1098   const SmallVectorImpl<unsigned> &getRegList() const {
1099     assert((Kind == k_RegList) && "Invalid access!");
1100     return *(RegList.List);
1101   }
1102
1103   unsigned getRegPair() const {
1104     assert((Kind == k_RegPair) && "Invalid access!");
1105     return RegIdx.Index;
1106   }
1107
1108   static std::unique_ptr<MipsOperand> CreateToken(StringRef Str, SMLoc S,
1109                                                   MipsAsmParser &Parser) {
1110     auto Op = make_unique<MipsOperand>(k_Token, Parser);
1111     Op->Tok.Data = Str.data();
1112     Op->Tok.Length = Str.size();
1113     Op->StartLoc = S;
1114     Op->EndLoc = S;
1115     return Op;
1116   }
1117
1118   /// Create a numeric register (e.g. $1). The exact register remains
1119   /// unresolved until an instruction successfully matches
1120   static std::unique_ptr<MipsOperand>
1121   createNumericReg(unsigned Index, const MCRegisterInfo *RegInfo, SMLoc S,
1122                    SMLoc E, MipsAsmParser &Parser) {
1123     DEBUG(dbgs() << "createNumericReg(" << Index << ", ...)\n");
1124     return CreateReg(Index, RegKind_Numeric, RegInfo, S, E, Parser);
1125   }
1126
1127   /// Create a register that is definitely a GPR.
1128   /// This is typically only used for named registers such as $gp.
1129   static std::unique_ptr<MipsOperand>
1130   createGPRReg(unsigned Index, const MCRegisterInfo *RegInfo, SMLoc S, SMLoc E,
1131                MipsAsmParser &Parser) {
1132     return CreateReg(Index, RegKind_GPR, RegInfo, S, E, Parser);
1133   }
1134
1135   /// Create a register that is definitely a FGR.
1136   /// This is typically only used for named registers such as $f0.
1137   static std::unique_ptr<MipsOperand>
1138   createFGRReg(unsigned Index, const MCRegisterInfo *RegInfo, SMLoc S, SMLoc E,
1139                MipsAsmParser &Parser) {
1140     return CreateReg(Index, RegKind_FGR, RegInfo, S, E, Parser);
1141   }
1142
1143   /// Create a register that is definitely a HWReg.
1144   /// This is typically only used for named registers such as $hwr_cpunum.
1145   static std::unique_ptr<MipsOperand>
1146   createHWRegsReg(unsigned Index, const MCRegisterInfo *RegInfo,
1147                   SMLoc S, SMLoc E, MipsAsmParser &Parser) {
1148     return CreateReg(Index, RegKind_HWRegs, RegInfo, S, E, Parser);
1149   }
1150
1151   /// Create a register that is definitely an FCC.
1152   /// This is typically only used for named registers such as $fcc0.
1153   static std::unique_ptr<MipsOperand>
1154   createFCCReg(unsigned Index, const MCRegisterInfo *RegInfo, SMLoc S, SMLoc E,
1155                MipsAsmParser &Parser) {
1156     return CreateReg(Index, RegKind_FCC, RegInfo, S, E, Parser);
1157   }
1158
1159   /// Create a register that is definitely an ACC.
1160   /// This is typically only used for named registers such as $ac0.
1161   static std::unique_ptr<MipsOperand>
1162   createACCReg(unsigned Index, const MCRegisterInfo *RegInfo, SMLoc S, SMLoc E,
1163                MipsAsmParser &Parser) {
1164     return CreateReg(Index, RegKind_ACC, RegInfo, S, E, Parser);
1165   }
1166
1167   /// Create a register that is definitely an MSA128.
1168   /// This is typically only used for named registers such as $w0.
1169   static std::unique_ptr<MipsOperand>
1170   createMSA128Reg(unsigned Index, const MCRegisterInfo *RegInfo, SMLoc S,
1171                   SMLoc E, MipsAsmParser &Parser) {
1172     return CreateReg(Index, RegKind_MSA128, RegInfo, S, E, Parser);
1173   }
1174
1175   /// Create a register that is definitely an MSACtrl.
1176   /// This is typically only used for named registers such as $msaaccess.
1177   static std::unique_ptr<MipsOperand>
1178   createMSACtrlReg(unsigned Index, const MCRegisterInfo *RegInfo, SMLoc S,
1179                    SMLoc E, MipsAsmParser &Parser) {
1180     return CreateReg(Index, RegKind_MSACtrl, RegInfo, S, E, Parser);
1181   }
1182
1183   static std::unique_ptr<MipsOperand>
1184   CreateImm(const MCExpr *Val, SMLoc S, SMLoc E, MipsAsmParser &Parser) {
1185     auto Op = make_unique<MipsOperand>(k_Immediate, Parser);
1186     Op->Imm.Val = Val;
1187     Op->StartLoc = S;
1188     Op->EndLoc = E;
1189     return Op;
1190   }
1191
1192   static std::unique_ptr<MipsOperand>
1193   CreateMem(std::unique_ptr<MipsOperand> Base, const MCExpr *Off, SMLoc S,
1194             SMLoc E, MipsAsmParser &Parser) {
1195     auto Op = make_unique<MipsOperand>(k_Memory, Parser);
1196     Op->Mem.Base = Base.release();
1197     Op->Mem.Off = Off;
1198     Op->StartLoc = S;
1199     Op->EndLoc = E;
1200     return Op;
1201   }
1202
1203   static std::unique_ptr<MipsOperand>
1204   CreateRegList(SmallVectorImpl<unsigned> &Regs, SMLoc StartLoc, SMLoc EndLoc,
1205                 MipsAsmParser &Parser) {
1206     assert (Regs.size() > 0 && "Empty list not allowed");
1207
1208     auto Op = make_unique<MipsOperand>(k_RegList, Parser);
1209     Op->RegList.List = new SmallVector<unsigned, 10>(Regs.begin(), Regs.end());
1210     Op->StartLoc = StartLoc;
1211     Op->EndLoc = EndLoc;
1212     return Op;
1213   }
1214
1215   static std::unique_ptr<MipsOperand>
1216   CreateRegPair(unsigned RegNo, SMLoc S, SMLoc E, MipsAsmParser &Parser) {
1217     auto Op = make_unique<MipsOperand>(k_RegPair, Parser);
1218     Op->RegIdx.Index = RegNo;
1219     Op->StartLoc = S;
1220     Op->EndLoc = E;
1221     return Op;
1222   }
1223
1224   bool isGPRAsmReg() const {
1225     return isRegIdx() && RegIdx.Kind & RegKind_GPR && RegIdx.Index <= 31;
1226   }
1227   bool isMM16AsmReg() const {
1228     if (!(isRegIdx() && RegIdx.Kind))
1229       return false;
1230     return ((RegIdx.Index >= 2 && RegIdx.Index <= 7)
1231             || RegIdx.Index == 16 || RegIdx.Index == 17);
1232   }
1233   bool isMM16AsmRegZero() const {
1234     if (!(isRegIdx() && RegIdx.Kind))
1235       return false;
1236     return (RegIdx.Index == 0 ||
1237             (RegIdx.Index >= 2 && RegIdx.Index <= 7) ||
1238             RegIdx.Index == 17);
1239   }
1240   bool isMM16AsmRegMoveP() const {
1241     if (!(isRegIdx() && RegIdx.Kind))
1242       return false;
1243     return (RegIdx.Index == 0 || (RegIdx.Index >= 2 && RegIdx.Index <= 3) ||
1244       (RegIdx.Index >= 16 && RegIdx.Index <= 20));
1245   }
1246   bool isFGRAsmReg() const {
1247     // AFGR64 is $0-$15 but we handle this in getAFGR64()
1248     return isRegIdx() && RegIdx.Kind & RegKind_FGR && RegIdx.Index <= 31;
1249   }
1250   bool isHWRegsAsmReg() const {
1251     return isRegIdx() && RegIdx.Kind & RegKind_HWRegs && RegIdx.Index <= 31;
1252   }
1253   bool isCCRAsmReg() const {
1254     return isRegIdx() && RegIdx.Kind & RegKind_CCR && RegIdx.Index <= 31;
1255   }
1256   bool isFCCAsmReg() const {
1257     if (!(isRegIdx() && RegIdx.Kind & RegKind_FCC))
1258       return false;
1259     if (!AsmParser.hasEightFccRegisters())
1260       return RegIdx.Index == 0;
1261     return RegIdx.Index <= 7;
1262   }
1263   bool isACCAsmReg() const {
1264     return isRegIdx() && RegIdx.Kind & RegKind_ACC && RegIdx.Index <= 3;
1265   }
1266   bool isCOP0AsmReg() const {
1267     return isRegIdx() && RegIdx.Kind & RegKind_COP0 && RegIdx.Index <= 31;
1268   }
1269   bool isCOP2AsmReg() const {
1270     return isRegIdx() && RegIdx.Kind & RegKind_COP2 && RegIdx.Index <= 31;
1271   }
1272   bool isCOP3AsmReg() const {
1273     return isRegIdx() && RegIdx.Kind & RegKind_COP3 && RegIdx.Index <= 31;
1274   }
1275   bool isMSA128AsmReg() const {
1276     return isRegIdx() && RegIdx.Kind & RegKind_MSA128 && RegIdx.Index <= 31;
1277   }
1278   bool isMSACtrlAsmReg() const {
1279     return isRegIdx() && RegIdx.Kind & RegKind_MSACtrl && RegIdx.Index <= 7;
1280   }
1281
1282   /// getStartLoc - Get the location of the first token of this operand.
1283   SMLoc getStartLoc() const override { return StartLoc; }
1284   /// getEndLoc - Get the location of the last token of this operand.
1285   SMLoc getEndLoc() const override { return EndLoc; }
1286
1287   virtual ~MipsOperand() {
1288     switch (Kind) {
1289     case k_Immediate:
1290       break;
1291     case k_Memory:
1292       delete Mem.Base;
1293       break;
1294     case k_RegList:
1295       delete RegList.List;
1296     case k_PhysRegister:
1297     case k_RegisterIndex:
1298     case k_Token:
1299     case k_RegPair:
1300       break;
1301     }
1302   }
1303
1304   void print(raw_ostream &OS) const override {
1305     switch (Kind) {
1306     case k_Immediate:
1307       OS << "Imm<";
1308       OS << *Imm.Val;
1309       OS << ">";
1310       break;
1311     case k_Memory:
1312       OS << "Mem<";
1313       Mem.Base->print(OS);
1314       OS << ", ";
1315       OS << *Mem.Off;
1316       OS << ">";
1317       break;
1318     case k_PhysRegister:
1319       OS << "PhysReg<" << PhysReg.Num << ">";
1320       break;
1321     case k_RegisterIndex:
1322       OS << "RegIdx<" << RegIdx.Index << ":" << RegIdx.Kind << ">";
1323       break;
1324     case k_Token:
1325       OS << Tok.Data;
1326       break;
1327     case k_RegList:
1328       OS << "RegList< ";
1329       for (auto Reg : (*RegList.List))
1330         OS << Reg << " ";
1331       OS <<  ">";
1332       break;
1333     case k_RegPair:
1334       OS << "RegPair<" << RegIdx.Index << "," << RegIdx.Index + 1 << ">";
1335       break;
1336     }
1337   }
1338 }; // class MipsOperand
1339 } // namespace
1340
1341 namespace llvm {
1342 extern const MCInstrDesc MipsInsts[];
1343 }
1344 static const MCInstrDesc &getInstDesc(unsigned Opcode) {
1345   return MipsInsts[Opcode];
1346 }
1347
1348 static bool hasShortDelaySlot(unsigned Opcode) {
1349   switch (Opcode) {
1350     case Mips::JALS_MM:
1351     case Mips::JALRS_MM:
1352     case Mips::JALRS16_MM:
1353     case Mips::BGEZALS_MM:
1354     case Mips::BLTZALS_MM:
1355       return true;
1356     default:
1357       return false;
1358   }
1359 }
1360
1361 static const MCSymbol *getSingleMCSymbol(const MCExpr *Expr) {
1362   if (const MCSymbolRefExpr *SRExpr = dyn_cast<MCSymbolRefExpr>(Expr)) {
1363     return &SRExpr->getSymbol();
1364   }
1365
1366   if (const MCBinaryExpr *BExpr = dyn_cast<MCBinaryExpr>(Expr)) {
1367     const MCSymbol *LHSSym = getSingleMCSymbol(BExpr->getLHS());
1368     const MCSymbol *RHSSym = getSingleMCSymbol(BExpr->getRHS());
1369
1370     if (LHSSym)
1371       return LHSSym;
1372
1373     if (RHSSym)
1374       return RHSSym;
1375
1376     return nullptr;
1377   }
1378
1379   if (const MCUnaryExpr *UExpr = dyn_cast<MCUnaryExpr>(Expr))
1380     return getSingleMCSymbol(UExpr->getSubExpr());
1381
1382   return nullptr;
1383 }
1384
1385 static unsigned countMCSymbolRefExpr(const MCExpr *Expr) {
1386   if (isa<MCSymbolRefExpr>(Expr))
1387     return 1;
1388
1389   if (const MCBinaryExpr *BExpr = dyn_cast<MCBinaryExpr>(Expr))
1390     return countMCSymbolRefExpr(BExpr->getLHS()) +
1391            countMCSymbolRefExpr(BExpr->getRHS());
1392
1393   if (const MCUnaryExpr *UExpr = dyn_cast<MCUnaryExpr>(Expr))
1394     return countMCSymbolRefExpr(UExpr->getSubExpr());
1395
1396   return 0;
1397 }
1398
1399 namespace {
1400 void emitRX(unsigned Opcode, unsigned Reg0, MCOperand Op1, SMLoc IDLoc,
1401             SmallVectorImpl<MCInst> &Instructions) {
1402   MCInst tmpInst;
1403   tmpInst.setOpcode(Opcode);
1404   tmpInst.addOperand(MCOperand::createReg(Reg0));
1405   tmpInst.addOperand(Op1);
1406   tmpInst.setLoc(IDLoc);
1407   Instructions.push_back(tmpInst);
1408 }
1409
1410 void emitRI(unsigned Opcode, unsigned Reg0, int32_t Imm, SMLoc IDLoc,
1411             SmallVectorImpl<MCInst> &Instructions) {
1412   emitRX(Opcode, Reg0, MCOperand::createImm(Imm), IDLoc, Instructions);
1413 }
1414
1415 void emitRR(unsigned Opcode, unsigned Reg0, unsigned Reg1, SMLoc IDLoc,
1416             SmallVectorImpl<MCInst> &Instructions) {
1417   emitRX(Opcode, Reg0, MCOperand::createReg(Reg1), IDLoc, Instructions);
1418 }
1419
1420 void emitII(unsigned Opcode, int16_t Imm1, int16_t Imm2, SMLoc IDLoc,
1421             SmallVectorImpl<MCInst> &Instructions) {
1422   MCInst tmpInst;
1423   tmpInst.setOpcode(Opcode);
1424   tmpInst.addOperand(MCOperand::createImm(Imm1));
1425   tmpInst.addOperand(MCOperand::createImm(Imm2));
1426   tmpInst.setLoc(IDLoc);
1427   Instructions.push_back(tmpInst);
1428 }
1429
1430 void emitR(unsigned Opcode, unsigned Reg0, SMLoc IDLoc,
1431            SmallVectorImpl<MCInst> &Instructions) {
1432   MCInst tmpInst;
1433   tmpInst.setOpcode(Opcode);
1434   tmpInst.addOperand(MCOperand::createReg(Reg0));
1435   tmpInst.setLoc(IDLoc);
1436   Instructions.push_back(tmpInst);
1437 }
1438
1439 void emitRRX(unsigned Opcode, unsigned Reg0, unsigned Reg1, MCOperand Op2,
1440              SMLoc IDLoc, SmallVectorImpl<MCInst> &Instructions) {
1441   MCInst tmpInst;
1442   tmpInst.setOpcode(Opcode);
1443   tmpInst.addOperand(MCOperand::createReg(Reg0));
1444   tmpInst.addOperand(MCOperand::createReg(Reg1));
1445   tmpInst.addOperand(Op2);
1446   tmpInst.setLoc(IDLoc);
1447   Instructions.push_back(tmpInst);
1448 }
1449
1450 void emitRRR(unsigned Opcode, unsigned Reg0, unsigned Reg1, unsigned Reg2,
1451              SMLoc IDLoc, SmallVectorImpl<MCInst> &Instructions) {
1452   emitRRX(Opcode, Reg0, Reg1, MCOperand::createReg(Reg2), IDLoc,
1453           Instructions);
1454 }
1455
1456 void emitRRI(unsigned Opcode, unsigned Reg0, unsigned Reg1, int16_t Imm,
1457              SMLoc IDLoc, SmallVectorImpl<MCInst> &Instructions) {
1458   emitRRX(Opcode, Reg0, Reg1, MCOperand::createImm(Imm), IDLoc,
1459           Instructions);
1460 }
1461
1462 void emitAppropriateDSLL(unsigned DstReg, unsigned SrcReg, int16_t ShiftAmount,
1463                          SMLoc IDLoc, SmallVectorImpl<MCInst> &Instructions) {
1464   if (ShiftAmount >= 32) {
1465     emitRRI(Mips::DSLL32, DstReg, SrcReg, ShiftAmount - 32, IDLoc,
1466             Instructions);
1467     return;
1468   }
1469
1470   emitRRI(Mips::DSLL, DstReg, SrcReg, ShiftAmount, IDLoc, Instructions);
1471 }
1472 } // end anonymous namespace.
1473
1474 bool MipsAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
1475                                        SmallVectorImpl<MCInst> &Instructions) {
1476   const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
1477   bool ExpandedJalSym = false;
1478
1479   Inst.setLoc(IDLoc);
1480
1481   if (MCID.isBranch() || MCID.isCall()) {
1482     const unsigned Opcode = Inst.getOpcode();
1483     MCOperand Offset;
1484
1485     switch (Opcode) {
1486     default:
1487       break;
1488     case Mips::BBIT0:
1489     case Mips::BBIT032:
1490     case Mips::BBIT1:
1491     case Mips::BBIT132:
1492       assert(hasCnMips() && "instruction only valid for octeon cpus");
1493       // Fall through
1494
1495     case Mips::BEQ:
1496     case Mips::BNE:
1497     case Mips::BEQ_MM:
1498     case Mips::BNE_MM:
1499       assert(MCID.getNumOperands() == 3 && "unexpected number of operands");
1500       Offset = Inst.getOperand(2);
1501       if (!Offset.isImm())
1502         break; // We'll deal with this situation later on when applying fixups.
1503       if (!isIntN(inMicroMipsMode() ? 17 : 18, Offset.getImm()))
1504         return Error(IDLoc, "branch target out of range");
1505       if (OffsetToAlignment(Offset.getImm(),
1506                             1LL << (inMicroMipsMode() ? 1 : 2)))
1507         return Error(IDLoc, "branch to misaligned address");
1508       break;
1509     case Mips::BGEZ:
1510     case Mips::BGTZ:
1511     case Mips::BLEZ:
1512     case Mips::BLTZ:
1513     case Mips::BGEZAL:
1514     case Mips::BLTZAL:
1515     case Mips::BC1F:
1516     case Mips::BC1T:
1517     case Mips::BGEZ_MM:
1518     case Mips::BGTZ_MM:
1519     case Mips::BLEZ_MM:
1520     case Mips::BLTZ_MM:
1521     case Mips::BGEZAL_MM:
1522     case Mips::BLTZAL_MM:
1523     case Mips::BC1F_MM:
1524     case Mips::BC1T_MM:
1525       assert(MCID.getNumOperands() == 2 && "unexpected number of operands");
1526       Offset = Inst.getOperand(1);
1527       if (!Offset.isImm())
1528         break; // We'll deal with this situation later on when applying fixups.
1529       if (!isIntN(inMicroMipsMode() ? 17 : 18, Offset.getImm()))
1530         return Error(IDLoc, "branch target out of range");
1531       if (OffsetToAlignment(Offset.getImm(),
1532                             1LL << (inMicroMipsMode() ? 1 : 2)))
1533         return Error(IDLoc, "branch to misaligned address");
1534       break;
1535     case Mips::BEQZ16_MM:
1536     case Mips::BEQZC16_MMR6:
1537     case Mips::BNEZ16_MM:
1538     case Mips::BNEZC16_MMR6:
1539       assert(MCID.getNumOperands() == 2 && "unexpected number of operands");
1540       Offset = Inst.getOperand(1);
1541       if (!Offset.isImm())
1542         break; // We'll deal with this situation later on when applying fixups.
1543       if (!isInt<8>(Offset.getImm()))
1544         return Error(IDLoc, "branch target out of range");
1545       if (OffsetToAlignment(Offset.getImm(), 2LL))
1546         return Error(IDLoc, "branch to misaligned address");
1547       break;
1548     }
1549   }
1550
1551   // SSNOP is deprecated on MIPS32r6/MIPS64r6
1552   // We still accept it but it is a normal nop.
1553   if (hasMips32r6() && Inst.getOpcode() == Mips::SSNOP) {
1554     std::string ISA = hasMips64r6() ? "MIPS64r6" : "MIPS32r6";
1555     Warning(IDLoc, "ssnop is deprecated for " + ISA + " and is equivalent to a "
1556                                                       "nop instruction");
1557   }
1558
1559   if (hasCnMips()) {
1560     const unsigned Opcode = Inst.getOpcode();
1561     MCOperand Opnd;
1562     int Imm;
1563
1564     switch (Opcode) {
1565       default:
1566         break;
1567
1568       case Mips::BBIT0:
1569       case Mips::BBIT032:
1570       case Mips::BBIT1:
1571       case Mips::BBIT132:
1572         assert(MCID.getNumOperands() == 3 && "unexpected number of operands");
1573         // The offset is handled above
1574         Opnd = Inst.getOperand(1);
1575         if (!Opnd.isImm())
1576           return Error(IDLoc, "expected immediate operand kind");
1577         Imm = Opnd.getImm();
1578         if (Imm < 0 || Imm > (Opcode == Mips::BBIT0 ||
1579                               Opcode == Mips::BBIT1 ? 63 : 31))
1580           return Error(IDLoc, "immediate operand value out of range");
1581         if (Imm > 31) {
1582           Inst.setOpcode(Opcode == Mips::BBIT0 ? Mips::BBIT032
1583                                                : Mips::BBIT132);
1584           Inst.getOperand(1).setImm(Imm - 32);
1585         }
1586         break;
1587
1588       case Mips::CINS:
1589       case Mips::CINS32:
1590       case Mips::EXTS:
1591       case Mips::EXTS32:
1592         assert(MCID.getNumOperands() == 4 && "unexpected number of operands");
1593         // Check length
1594         Opnd = Inst.getOperand(3);
1595         if (!Opnd.isImm())
1596           return Error(IDLoc, "expected immediate operand kind");
1597         Imm = Opnd.getImm();
1598         if (Imm < 0 || Imm > 31)
1599           return Error(IDLoc, "immediate operand value out of range");
1600         // Check position
1601         Opnd = Inst.getOperand(2);
1602         if (!Opnd.isImm())
1603           return Error(IDLoc, "expected immediate operand kind");
1604         Imm = Opnd.getImm();
1605         if (Imm < 0 || Imm > (Opcode == Mips::CINS ||
1606                               Opcode == Mips::EXTS ? 63 : 31))
1607           return Error(IDLoc, "immediate operand value out of range");
1608         if (Imm > 31) {
1609           Inst.setOpcode(Opcode == Mips::CINS ? Mips::CINS32 : Mips::EXTS32);
1610           Inst.getOperand(2).setImm(Imm - 32);
1611         }
1612         break;
1613
1614       case Mips::SEQi:
1615       case Mips::SNEi:
1616         assert(MCID.getNumOperands() == 3 && "unexpected number of operands");
1617         Opnd = Inst.getOperand(2);
1618         if (!Opnd.isImm())
1619           return Error(IDLoc, "expected immediate operand kind");
1620         Imm = Opnd.getImm();
1621         if (!isInt<10>(Imm))
1622           return Error(IDLoc, "immediate operand value out of range");
1623         break;
1624     }
1625   }
1626
1627   // This expansion is not in a function called by tryExpandInstruction()
1628   // because the pseudo-instruction doesn't have a distinct opcode.
1629   if ((Inst.getOpcode() == Mips::JAL || Inst.getOpcode() == Mips::JAL_MM) &&
1630       inPicMode()) {
1631     warnIfNoMacro(IDLoc);
1632
1633     const MCExpr *JalExpr = Inst.getOperand(0).getExpr();
1634
1635     // We can do this expansion if there's only 1 symbol in the argument
1636     // expression.
1637     if (countMCSymbolRefExpr(JalExpr) > 1)
1638       return Error(IDLoc, "jal doesn't support multiple symbols in PIC mode");
1639
1640     // FIXME: This is checking the expression can be handled by the later stages
1641     //        of the assembler. We ought to leave it to those later stages but
1642     //        we can't do that until we stop evaluateRelocExpr() rewriting the
1643     //        expressions into non-equivalent forms.
1644     const MCSymbol *JalSym = getSingleMCSymbol(JalExpr);
1645
1646     // FIXME: Add support for label+offset operands (currently causes an error).
1647     // FIXME: Add support for forward-declared local symbols.
1648     // FIXME: Add expansion for when the LargeGOT option is enabled.
1649     if (JalSym->isInSection() || JalSym->isTemporary()) {
1650       if (isABI_O32()) {
1651         // If it's a local symbol and the O32 ABI is being used, we expand to:
1652         //  lw $25, 0($gp)
1653         //    R_(MICRO)MIPS_GOT16  label
1654         //  addiu $25, $25, 0
1655         //    R_(MICRO)MIPS_LO16   label
1656         //  jalr  $25
1657         const MCExpr *Got16RelocExpr = evaluateRelocExpr(JalExpr, "got");
1658         const MCExpr *Lo16RelocExpr = evaluateRelocExpr(JalExpr, "lo");
1659
1660         emitRRX(Mips::LW, Mips::T9, Mips::GP,
1661                 MCOperand::createExpr(Got16RelocExpr), IDLoc, Instructions);
1662         emitRRX(Mips::ADDiu, Mips::T9, Mips::T9,
1663                 MCOperand::createExpr(Lo16RelocExpr), IDLoc, Instructions);
1664       } else if (isABI_N32() || isABI_N64()) {
1665         // If it's a local symbol and the N32/N64 ABIs are being used,
1666         // we expand to:
1667         //  lw/ld $25, 0($gp)
1668         //    R_(MICRO)MIPS_GOT_DISP  label
1669         //  jalr  $25
1670         const MCExpr *GotDispRelocExpr = evaluateRelocExpr(JalExpr, "got_disp");
1671
1672         emitRRX(ABI.ArePtrs64bit() ? Mips::LD : Mips::LW, Mips::T9, Mips::GP,
1673                 MCOperand::createExpr(GotDispRelocExpr), IDLoc, Instructions);
1674       }
1675     } else {
1676       // If it's an external/weak symbol, we expand to:
1677       //  lw/ld    $25, 0($gp)
1678       //    R_(MICRO)MIPS_CALL16  label
1679       //  jalr  $25
1680       const MCExpr *Call16RelocExpr = evaluateRelocExpr(JalExpr, "call16");
1681
1682       emitRRX(ABI.ArePtrs64bit() ? Mips::LD : Mips::LW, Mips::T9, Mips::GP,
1683               MCOperand::createExpr(Call16RelocExpr), IDLoc, Instructions);
1684     }
1685
1686     MCInst JalrInst;
1687     if (IsCpRestoreSet && inMicroMipsMode())
1688       JalrInst.setOpcode(Mips::JALRS_MM);
1689     else
1690       JalrInst.setOpcode(inMicroMipsMode() ? Mips::JALR_MM : Mips::JALR);
1691     JalrInst.addOperand(MCOperand::createReg(Mips::RA));
1692     JalrInst.addOperand(MCOperand::createReg(Mips::T9));
1693
1694     // FIXME: Add an R_(MICRO)MIPS_JALR relocation after the JALR.
1695     // This relocation is supposed to be an optimization hint for the linker
1696     // and is not necessary for correctness.
1697
1698     Inst = JalrInst;
1699     ExpandedJalSym = true;
1700   }
1701
1702   if (MCID.mayLoad() || MCID.mayStore()) {
1703     // Check the offset of memory operand, if it is a symbol
1704     // reference or immediate we may have to expand instructions.
1705     for (unsigned i = 0; i < MCID.getNumOperands(); i++) {
1706       const MCOperandInfo &OpInfo = MCID.OpInfo[i];
1707       if ((OpInfo.OperandType == MCOI::OPERAND_MEMORY) ||
1708           (OpInfo.OperandType == MCOI::OPERAND_UNKNOWN)) {
1709         MCOperand &Op = Inst.getOperand(i);
1710         if (Op.isImm()) {
1711           int MemOffset = Op.getImm();
1712           if (MemOffset < -32768 || MemOffset > 32767) {
1713             // Offset can't exceed 16bit value.
1714             expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), true);
1715             return false;
1716           }
1717         } else if (Op.isExpr()) {
1718           const MCExpr *Expr = Op.getExpr();
1719           if (Expr->getKind() == MCExpr::SymbolRef) {
1720             const MCSymbolRefExpr *SR =
1721                 static_cast<const MCSymbolRefExpr *>(Expr);
1722             if (SR->getKind() == MCSymbolRefExpr::VK_None) {
1723               // Expand symbol.
1724               expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), false);
1725               return false;
1726             }
1727           } else if (!isEvaluated(Expr)) {
1728             expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), false);
1729             return false;
1730           }
1731         }
1732       }
1733     } // for
1734   }   // if load/store
1735
1736   if (inMicroMipsMode()) {
1737     if (MCID.mayLoad()) {
1738       // Try to create 16-bit GP relative load instruction.
1739       for (unsigned i = 0; i < MCID.getNumOperands(); i++) {
1740         const MCOperandInfo &OpInfo = MCID.OpInfo[i];
1741         if ((OpInfo.OperandType == MCOI::OPERAND_MEMORY) ||
1742             (OpInfo.OperandType == MCOI::OPERAND_UNKNOWN)) {
1743           MCOperand &Op = Inst.getOperand(i);
1744           if (Op.isImm()) {
1745             int MemOffset = Op.getImm();
1746             MCOperand &DstReg = Inst.getOperand(0);
1747             MCOperand &BaseReg = Inst.getOperand(1);
1748             if (isInt<9>(MemOffset) && (MemOffset % 4 == 0) &&
1749                 getContext().getRegisterInfo()->getRegClass(
1750                   Mips::GPRMM16RegClassID).contains(DstReg.getReg()) &&
1751                 (BaseReg.getReg() == Mips::GP ||
1752                 BaseReg.getReg() == Mips::GP_64)) {
1753
1754               emitRRI(Mips::LWGP_MM, DstReg.getReg(), Mips::GP, MemOffset,
1755                       IDLoc, Instructions);
1756               return false;
1757             }
1758           }
1759         }
1760       } // for
1761     }   // if load
1762
1763     // TODO: Handle this with the AsmOperandClass.PredicateMethod.
1764
1765     MCOperand Opnd;
1766     int Imm;
1767
1768     switch (Inst.getOpcode()) {
1769       default:
1770         break;
1771       case Mips::ADDIUS5_MM:
1772         Opnd = Inst.getOperand(2);
1773         if (!Opnd.isImm())
1774           return Error(IDLoc, "expected immediate operand kind");
1775         Imm = Opnd.getImm();
1776         if (Imm < -8 || Imm > 7)
1777           return Error(IDLoc, "immediate operand value out of range");
1778         break;
1779       case Mips::ADDIUSP_MM:
1780         Opnd = Inst.getOperand(0);
1781         if (!Opnd.isImm())
1782           return Error(IDLoc, "expected immediate operand kind");
1783         Imm = Opnd.getImm();
1784         if (Imm < -1032 || Imm > 1028 || (Imm < 8 && Imm > -12) ||
1785             Imm % 4 != 0)
1786           return Error(IDLoc, "immediate operand value out of range");
1787         break;
1788       case Mips::SLL16_MM:
1789       case Mips::SRL16_MM:
1790         Opnd = Inst.getOperand(2);
1791         if (!Opnd.isImm())
1792           return Error(IDLoc, "expected immediate operand kind");
1793         Imm = Opnd.getImm();
1794         if (Imm < 1 || Imm > 8)
1795           return Error(IDLoc, "immediate operand value out of range");
1796         break;
1797       case Mips::LI16_MM:
1798         Opnd = Inst.getOperand(1);
1799         if (!Opnd.isImm())
1800           return Error(IDLoc, "expected immediate operand kind");
1801         Imm = Opnd.getImm();
1802         if (Imm < -1 || Imm > 126)
1803           return Error(IDLoc, "immediate operand value out of range");
1804         break;
1805       case Mips::ADDIUR2_MM:
1806         Opnd = Inst.getOperand(2);
1807         if (!Opnd.isImm())
1808           return Error(IDLoc, "expected immediate operand kind");
1809         Imm = Opnd.getImm();
1810         if (!(Imm == 1 || Imm == -1 ||
1811               ((Imm % 4 == 0) && Imm < 28 && Imm > 0)))
1812           return Error(IDLoc, "immediate operand value out of range");
1813         break;
1814       case Mips::ADDIUR1SP_MM:
1815         Opnd = Inst.getOperand(1);
1816         if (!Opnd.isImm())
1817           return Error(IDLoc, "expected immediate operand kind");
1818         Imm = Opnd.getImm();
1819         if (OffsetToAlignment(Imm, 4LL))
1820           return Error(IDLoc, "misaligned immediate operand value");
1821         if (Imm < 0 || Imm > 255)
1822           return Error(IDLoc, "immediate operand value out of range");
1823         break;
1824       case Mips::ANDI16_MM:
1825         Opnd = Inst.getOperand(2);
1826         if (!Opnd.isImm())
1827           return Error(IDLoc, "expected immediate operand kind");
1828         Imm = Opnd.getImm();
1829         if (!(Imm == 128 || (Imm >= 1 && Imm <= 4) || Imm == 7 || Imm == 8 ||
1830               Imm == 15 || Imm == 16 || Imm == 31 || Imm == 32 || Imm == 63 ||
1831               Imm == 64 || Imm == 255 || Imm == 32768 || Imm == 65535))
1832           return Error(IDLoc, "immediate operand value out of range");
1833         break;
1834       case Mips::LBU16_MM:
1835         Opnd = Inst.getOperand(2);
1836         if (!Opnd.isImm())
1837           return Error(IDLoc, "expected immediate operand kind");
1838         Imm = Opnd.getImm();
1839         if (Imm < -1 || Imm > 14)
1840           return Error(IDLoc, "immediate operand value out of range");
1841         break;
1842       case Mips::TEQ_MM:
1843       case Mips::TGE_MM:
1844       case Mips::TGEU_MM:
1845       case Mips::TLT_MM:
1846       case Mips::TLTU_MM:
1847       case Mips::TNE_MM:
1848       case Mips::SB16_MM:
1849       case Mips::SB16_MMR6:
1850         Opnd = Inst.getOperand(2);
1851         if (!Opnd.isImm())
1852           return Error(IDLoc, "expected immediate operand kind");
1853         Imm = Opnd.getImm();
1854         if (Imm < 0 || Imm > 15)
1855           return Error(IDLoc, "immediate operand value out of range");
1856         break;
1857       case Mips::LHU16_MM:
1858       case Mips::SH16_MM:
1859       case Mips::SH16_MMR6:
1860         Opnd = Inst.getOperand(2);
1861         if (!Opnd.isImm())
1862           return Error(IDLoc, "expected immediate operand kind");
1863         Imm = Opnd.getImm();
1864         if (Imm < 0 || Imm > 30 || (Imm % 2 != 0))
1865           return Error(IDLoc, "immediate operand value out of range");
1866         break;
1867       case Mips::LW16_MM:
1868       case Mips::SW16_MM:
1869       case Mips::SW16_MMR6:
1870         Opnd = Inst.getOperand(2);
1871         if (!Opnd.isImm())
1872           return Error(IDLoc, "expected immediate operand kind");
1873         Imm = Opnd.getImm();
1874         if (Imm < 0 || Imm > 60 || (Imm % 4 != 0))
1875           return Error(IDLoc, "immediate operand value out of range");
1876         break;
1877       case Mips::PREFX_MM:
1878       case Mips::CACHE:
1879       case Mips::PREF:
1880         Opnd = Inst.getOperand(2);
1881         if (!Opnd.isImm())
1882           return Error(IDLoc, "expected immediate operand kind");
1883         Imm = Opnd.getImm();
1884         if (!isUInt<5>(Imm))
1885           return Error(IDLoc, "immediate operand value out of range");
1886         break;
1887       case Mips::ADDIUPC_MM:
1888         MCOperand Opnd = Inst.getOperand(1);
1889         if (!Opnd.isImm())
1890           return Error(IDLoc, "expected immediate operand kind");
1891         int Imm = Opnd.getImm();
1892         if ((Imm % 4 != 0) || !isInt<25>(Imm))
1893           return Error(IDLoc, "immediate operand value out of range");
1894         break;
1895     }
1896   }
1897
1898   MacroExpanderResultTy ExpandResult =
1899       tryExpandInstruction(Inst, IDLoc, Instructions);
1900   switch (ExpandResult) {
1901   case MER_NotAMacro:
1902     Instructions.push_back(Inst);
1903     break;
1904   case MER_Success:
1905     break;
1906   case MER_Fail:
1907     return true;
1908   }
1909
1910   // If this instruction has a delay slot and .set reorder is active,
1911   // emit a NOP after it.
1912   if (MCID.hasDelaySlot() && AssemblerOptions.back()->isReorder())
1913     createNop(hasShortDelaySlot(Inst.getOpcode()), IDLoc, Instructions);
1914
1915   if ((Inst.getOpcode() == Mips::JalOneReg ||
1916        Inst.getOpcode() == Mips::JalTwoReg || ExpandedJalSym) &&
1917       isPicAndNotNxxAbi()) {
1918     if (IsCpRestoreSet) {
1919       // We need a NOP between the JALR and the LW:
1920       // If .set reorder has been used, we've already emitted a NOP.
1921       // If .set noreorder has been used, we need to emit a NOP at this point.
1922       if (!AssemblerOptions.back()->isReorder())
1923         createNop(hasShortDelaySlot(Inst.getOpcode()), IDLoc, Instructions);
1924
1925       // Load the $gp from the stack.
1926       SmallVector<MCInst, 3> LoadInsts;
1927       createCpRestoreMemOp(true /*IsLoad*/, CpRestoreOffset /*StackOffset*/,
1928                            IDLoc, LoadInsts);
1929
1930       for (const MCInst &Inst : LoadInsts)
1931         Instructions.push_back(Inst);
1932
1933     } else
1934       Warning(IDLoc, "no .cprestore used in PIC mode");
1935   }
1936
1937   return false;
1938 }
1939
1940 MipsAsmParser::MacroExpanderResultTy
1941 MipsAsmParser::tryExpandInstruction(MCInst &Inst, SMLoc IDLoc,
1942                                     SmallVectorImpl<MCInst> &Instructions) {
1943   switch (Inst.getOpcode()) {
1944   default:
1945     return MER_NotAMacro;
1946   case Mips::LoadImm32:
1947     return expandLoadImm(Inst, true, IDLoc, Instructions) ? MER_Fail
1948                                                           : MER_Success;
1949   case Mips::LoadImm64:
1950     return expandLoadImm(Inst, false, IDLoc, Instructions) ? MER_Fail
1951                                                            : MER_Success;
1952   case Mips::LoadAddrImm32:
1953   case Mips::LoadAddrImm64:
1954     assert(Inst.getOperand(0).isReg() && "expected register operand kind");
1955     assert((Inst.getOperand(1).isImm() || Inst.getOperand(1).isExpr()) &&
1956            "expected immediate operand kind");
1957
1958     return expandLoadAddress(Inst.getOperand(0).getReg(), Mips::NoRegister,
1959                              Inst.getOperand(1),
1960                              Inst.getOpcode() == Mips::LoadAddrImm32, IDLoc,
1961                              Instructions)
1962                ? MER_Fail
1963                : MER_Success;
1964   case Mips::LoadAddrReg32:
1965   case Mips::LoadAddrReg64:
1966     assert(Inst.getOperand(0).isReg() && "expected register operand kind");
1967     assert(Inst.getOperand(1).isReg() && "expected register operand kind");
1968     assert((Inst.getOperand(2).isImm() || Inst.getOperand(2).isExpr()) &&
1969            "expected immediate operand kind");
1970
1971     return expandLoadAddress(Inst.getOperand(0).getReg(),
1972                              Inst.getOperand(1).getReg(), Inst.getOperand(2),
1973                              Inst.getOpcode() == Mips::LoadAddrReg32, IDLoc,
1974                              Instructions)
1975                ? MER_Fail
1976                : MER_Success;
1977   case Mips::B_MM_Pseudo:
1978   case Mips::B_MMR6_Pseudo:
1979     return expandUncondBranchMMPseudo(Inst, IDLoc, Instructions) ? MER_Fail
1980                                                                  : MER_Success;
1981   case Mips::SWM_MM:
1982   case Mips::LWM_MM:
1983     return expandLoadStoreMultiple(Inst, IDLoc, Instructions) ? MER_Fail
1984                                                               : MER_Success;
1985   case Mips::JalOneReg:
1986   case Mips::JalTwoReg:
1987     return expandJalWithRegs(Inst, IDLoc, Instructions) ? MER_Fail
1988                                                         : MER_Success;
1989   case Mips::BneImm:
1990   case Mips::BeqImm:
1991     return expandBranchImm(Inst, IDLoc, Instructions) ? MER_Fail : MER_Success;
1992   case Mips::BLT:
1993   case Mips::BLE:
1994   case Mips::BGE:
1995   case Mips::BGT:
1996   case Mips::BLTU:
1997   case Mips::BLEU:
1998   case Mips::BGEU:
1999   case Mips::BGTU:
2000   case Mips::BLTL:
2001   case Mips::BLEL:
2002   case Mips::BGEL:
2003   case Mips::BGTL:
2004   case Mips::BLTUL:
2005   case Mips::BLEUL:
2006   case Mips::BGEUL:
2007   case Mips::BGTUL:
2008   case Mips::BLTImmMacro:
2009   case Mips::BLEImmMacro:
2010   case Mips::BGEImmMacro:
2011   case Mips::BGTImmMacro:
2012   case Mips::BLTUImmMacro:
2013   case Mips::BLEUImmMacro:
2014   case Mips::BGEUImmMacro:
2015   case Mips::BGTUImmMacro:
2016   case Mips::BLTLImmMacro:
2017   case Mips::BLELImmMacro:
2018   case Mips::BGELImmMacro:
2019   case Mips::BGTLImmMacro:
2020   case Mips::BLTULImmMacro:
2021   case Mips::BLEULImmMacro:
2022   case Mips::BGEULImmMacro:
2023   case Mips::BGTULImmMacro:
2024     return expandCondBranches(Inst, IDLoc, Instructions) ? MER_Fail
2025                                                          : MER_Success;
2026   case Mips::SDivMacro:
2027     return expandDiv(Inst, IDLoc, Instructions, false, true) ? MER_Fail
2028                                                              : MER_Success;
2029   case Mips::DSDivMacro:
2030     return expandDiv(Inst, IDLoc, Instructions, true, true) ? MER_Fail
2031                                                             : MER_Success;
2032   case Mips::UDivMacro:
2033     return expandDiv(Inst, IDLoc, Instructions, false, false) ? MER_Fail
2034                                                               : MER_Success;
2035   case Mips::DUDivMacro:
2036     return expandDiv(Inst, IDLoc, Instructions, true, false) ? MER_Fail
2037                                                              : MER_Success;
2038   case Mips::Ulh:
2039     return expandUlh(Inst, true, IDLoc, Instructions) ? MER_Fail : MER_Success;
2040   case Mips::Ulhu:
2041     return expandUlh(Inst, false, IDLoc, Instructions) ? MER_Fail : MER_Success;
2042   case Mips::Ulw:
2043     return expandUlw(Inst, IDLoc, Instructions) ? MER_Fail : MER_Success;
2044   case Mips::NORImm:
2045     return expandAliasImmediate(Inst, IDLoc, Instructions) ? MER_Fail
2046                                                            : MER_Success;
2047   case Mips::ADDi:
2048   case Mips::ADDiu:
2049   case Mips::SLTi:
2050   case Mips::SLTiu:
2051     if ((Inst.getNumOperands() == 3) && Inst.getOperand(0).isReg() &&
2052         Inst.getOperand(1).isReg() && Inst.getOperand(2).isImm()) {
2053       int64_t ImmValue = Inst.getOperand(2).getImm();
2054       if (isInt<16>(ImmValue))
2055         return MER_NotAMacro;
2056       return expandAliasImmediate(Inst, IDLoc, Instructions) ? MER_Fail
2057                                                              : MER_Success;
2058     }
2059     return MER_NotAMacro;
2060   case Mips::ANDi:
2061   case Mips::ORi:
2062   case Mips::XORi:
2063     if ((Inst.getNumOperands() == 3) && Inst.getOperand(0).isReg() &&
2064         Inst.getOperand(1).isReg() && Inst.getOperand(2).isImm()) {
2065       int64_t ImmValue = Inst.getOperand(2).getImm();
2066       if (isUInt<16>(ImmValue))
2067         return MER_NotAMacro;
2068       return expandAliasImmediate(Inst, IDLoc, Instructions) ? MER_Fail
2069                                                              : MER_Success;
2070     }
2071     return MER_NotAMacro;
2072   }
2073 }
2074
2075 bool MipsAsmParser::expandJalWithRegs(MCInst &Inst, SMLoc IDLoc,
2076                                       SmallVectorImpl<MCInst> &Instructions) {
2077   // Create a JALR instruction which is going to replace the pseudo-JAL.
2078   MCInst JalrInst;
2079   JalrInst.setLoc(IDLoc);
2080   const MCOperand FirstRegOp = Inst.getOperand(0);
2081   const unsigned Opcode = Inst.getOpcode();
2082
2083   if (Opcode == Mips::JalOneReg) {
2084     // jal $rs => jalr $rs
2085     if (IsCpRestoreSet && inMicroMipsMode()) {
2086       JalrInst.setOpcode(Mips::JALRS16_MM);
2087       JalrInst.addOperand(FirstRegOp);
2088     } else if (inMicroMipsMode()) {
2089       JalrInst.setOpcode(hasMips32r6() ? Mips::JALRC16_MMR6 : Mips::JALR16_MM);
2090       JalrInst.addOperand(FirstRegOp);
2091     } else {
2092       JalrInst.setOpcode(Mips::JALR);
2093       JalrInst.addOperand(MCOperand::createReg(Mips::RA));
2094       JalrInst.addOperand(FirstRegOp);
2095     }
2096   } else if (Opcode == Mips::JalTwoReg) {
2097     // jal $rd, $rs => jalr $rd, $rs
2098     if (IsCpRestoreSet && inMicroMipsMode())
2099       JalrInst.setOpcode(Mips::JALRS_MM);
2100     else
2101       JalrInst.setOpcode(inMicroMipsMode() ? Mips::JALR_MM : Mips::JALR);
2102     JalrInst.addOperand(FirstRegOp);
2103     const MCOperand SecondRegOp = Inst.getOperand(1);
2104     JalrInst.addOperand(SecondRegOp);
2105   }
2106   Instructions.push_back(JalrInst);
2107
2108   // If .set reorder is active and branch instruction has a delay slot,
2109   // emit a NOP after it.
2110   const MCInstrDesc &MCID = getInstDesc(JalrInst.getOpcode());
2111   if (MCID.hasDelaySlot() && AssemblerOptions.back()->isReorder()) {
2112     createNop(hasShortDelaySlot(JalrInst.getOpcode()), IDLoc, Instructions);
2113   }
2114
2115   return false;
2116 }
2117
2118 /// Can the value be represented by a unsigned N-bit value and a shift left?
2119 template <unsigned N> static bool isShiftedUIntAtAnyPosition(uint64_t x) {
2120   unsigned BitNum = findFirstSet(x);
2121
2122   return (x == x >> BitNum << BitNum) && isUInt<N>(x >> BitNum);
2123 }
2124
2125 /// Load (or add) an immediate into a register.
2126 ///
2127 /// @param ImmValue     The immediate to load.
2128 /// @param DstReg       The register that will hold the immediate.
2129 /// @param SrcReg       A register to add to the immediate or Mips::NoRegister
2130 ///                     for a simple initialization.
2131 /// @param Is32BitImm   Is ImmValue 32-bit or 64-bit?
2132 /// @param IsAddress    True if the immediate represents an address. False if it
2133 ///                     is an integer.
2134 /// @param IDLoc        Location of the immediate in the source file.
2135 /// @param Instructions The instructions emitted by this expansion.
2136 bool MipsAsmParser::loadImmediate(int64_t ImmValue, unsigned DstReg,
2137                                   unsigned SrcReg, bool Is32BitImm,
2138                                   bool IsAddress, SMLoc IDLoc,
2139                                   SmallVectorImpl<MCInst> &Instructions) {
2140   if (!Is32BitImm && !isGP64bit()) {
2141     Error(IDLoc, "instruction requires a 64-bit architecture");
2142     return true;
2143   }
2144
2145   if (Is32BitImm) {
2146     if (isInt<32>(ImmValue) || isUInt<32>(ImmValue)) {
2147       // Sign extend up to 64-bit so that the predicates match the hardware
2148       // behaviour. In particular, isInt<16>(0xffff8000) and similar should be
2149       // true.
2150       ImmValue = SignExtend64<32>(ImmValue);
2151     } else {
2152       Error(IDLoc, "instruction requires a 32-bit immediate");
2153       return true;
2154     }
2155   }
2156
2157   unsigned ZeroReg = IsAddress ? ABI.GetNullPtr() : ABI.GetZeroReg();
2158   unsigned AdduOp = !Is32BitImm ? Mips::DADDu : Mips::ADDu;
2159
2160   bool UseSrcReg = false;
2161   if (SrcReg != Mips::NoRegister)
2162     UseSrcReg = true;
2163
2164   unsigned TmpReg = DstReg;
2165   if (UseSrcReg && (DstReg == SrcReg)) {
2166     // At this point we need AT to perform the expansions and we exit if it is
2167     // not available.
2168     unsigned ATReg = getATReg(IDLoc);
2169     if (!ATReg)
2170       return true;
2171     TmpReg = ATReg;
2172   }
2173
2174   if (isInt<16>(ImmValue)) {
2175     if (!UseSrcReg)
2176       SrcReg = ZeroReg;
2177
2178     // This doesn't quite follow the usual ABI expectations for N32 but matches
2179     // traditional assembler behaviour. N32 would normally use addiu for both
2180     // integers and addresses.
2181     if (IsAddress && !Is32BitImm) {
2182       emitRRI(Mips::DADDiu, DstReg, SrcReg, ImmValue, IDLoc, Instructions);
2183       return false;
2184     }
2185
2186     emitRRI(Mips::ADDiu, DstReg, SrcReg, ImmValue, IDLoc, Instructions);
2187     return false;
2188   }
2189
2190   if (isUInt<16>(ImmValue)) {
2191     unsigned TmpReg = DstReg;
2192     if (SrcReg == DstReg) {
2193       TmpReg = getATReg(IDLoc);
2194       if (!TmpReg)
2195         return true;
2196     }
2197
2198     emitRRI(Mips::ORi, TmpReg, ZeroReg, ImmValue, IDLoc, Instructions);
2199     if (UseSrcReg)
2200       emitRRR(ABI.GetPtrAdduOp(), DstReg, TmpReg, SrcReg, IDLoc, Instructions);
2201     return false;
2202   }
2203
2204   if (isInt<32>(ImmValue) || isUInt<32>(ImmValue)) {
2205     warnIfNoMacro(IDLoc);
2206
2207     uint16_t Bits31To16 = (ImmValue >> 16) & 0xffff;
2208     uint16_t Bits15To0 = ImmValue & 0xffff;
2209
2210     if (!Is32BitImm && !isInt<32>(ImmValue)) {
2211       // Traditional behaviour seems to special case this particular value. It's
2212       // not clear why other masks are handled differently.
2213       if (ImmValue == 0xffffffff) {
2214         emitRI(Mips::LUi, TmpReg, 0xffff, IDLoc, Instructions);
2215         emitRRI(Mips::DSRL32, TmpReg, TmpReg, 0, IDLoc, Instructions);
2216         if (UseSrcReg)
2217           emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, Instructions);
2218         return false;
2219       }
2220
2221       // Expand to an ORi instead of a LUi to avoid sign-extending into the
2222       // upper 32 bits.
2223       emitRRI(Mips::ORi, TmpReg, ZeroReg, Bits31To16, IDLoc, Instructions);
2224       emitRRI(Mips::DSLL, TmpReg, TmpReg, 16, IDLoc, Instructions);
2225       if (Bits15To0)
2226         emitRRI(Mips::ORi, TmpReg, TmpReg, Bits15To0, IDLoc, Instructions);
2227       if (UseSrcReg)
2228         emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, Instructions);
2229       return false;
2230     }
2231
2232     emitRI(Mips::LUi, TmpReg, Bits31To16, IDLoc, Instructions);
2233     if (Bits15To0)
2234       emitRRI(Mips::ORi, TmpReg, TmpReg, Bits15To0, IDLoc, Instructions);
2235     if (UseSrcReg)
2236       emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, Instructions);
2237     return false;
2238   }
2239
2240   if (isShiftedUIntAtAnyPosition<16>(ImmValue)) {
2241     if (Is32BitImm) {
2242       Error(IDLoc, "instruction requires a 32-bit immediate");
2243       return true;
2244     }
2245
2246     // Traditionally, these immediates are shifted as little as possible and as
2247     // such we align the most significant bit to bit 15 of our temporary.
2248     unsigned FirstSet = findFirstSet((uint64_t)ImmValue);
2249     unsigned LastSet = findLastSet((uint64_t)ImmValue);
2250     unsigned ShiftAmount = FirstSet - (15 - (LastSet - FirstSet));
2251     uint16_t Bits = (ImmValue >> ShiftAmount) & 0xffff;
2252     emitRRI(Mips::ORi, TmpReg, ZeroReg, Bits, IDLoc, Instructions);
2253     emitRRI(Mips::DSLL, TmpReg, TmpReg, ShiftAmount, IDLoc, Instructions);
2254
2255     if (UseSrcReg)
2256       emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, Instructions);
2257
2258     return false;
2259   }
2260
2261   warnIfNoMacro(IDLoc);
2262
2263   // The remaining case is packed with a sequence of dsll and ori with zeros
2264   // being omitted and any neighbouring dsll's being coalesced.
2265   // The highest 32-bit's are equivalent to a 32-bit immediate load.
2266
2267   // Load bits 32-63 of ImmValue into bits 0-31 of the temporary register.
2268   if (loadImmediate(ImmValue >> 32, TmpReg, Mips::NoRegister, true, false,
2269                     IDLoc, Instructions))
2270     return false;
2271
2272   // Shift and accumulate into the register. If a 16-bit chunk is zero, then
2273   // skip it and defer the shift to the next chunk.
2274   unsigned ShiftCarriedForwards = 16;
2275   for (int BitNum = 16; BitNum >= 0; BitNum -= 16) {
2276     uint16_t ImmChunk = (ImmValue >> BitNum) & 0xffff;
2277
2278     if (ImmChunk != 0) {
2279       emitAppropriateDSLL(TmpReg, TmpReg, ShiftCarriedForwards, IDLoc,
2280                           Instructions);
2281       emitRRI(Mips::ORi, TmpReg, TmpReg, ImmChunk, IDLoc, Instructions);
2282       ShiftCarriedForwards = 0;
2283     }
2284
2285     ShiftCarriedForwards += 16;
2286   }
2287   ShiftCarriedForwards -= 16;
2288
2289   // Finish any remaining shifts left by trailing zeros.
2290   if (ShiftCarriedForwards)
2291     emitAppropriateDSLL(TmpReg, TmpReg, ShiftCarriedForwards, IDLoc,
2292                         Instructions);
2293
2294   if (UseSrcReg)
2295     emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, Instructions);
2296
2297   return false;
2298 }
2299
2300 bool MipsAsmParser::expandLoadImm(MCInst &Inst, bool Is32BitImm, SMLoc IDLoc,
2301                                   SmallVectorImpl<MCInst> &Instructions) {
2302   const MCOperand &ImmOp = Inst.getOperand(1);
2303   assert(ImmOp.isImm() && "expected immediate operand kind");
2304   const MCOperand &DstRegOp = Inst.getOperand(0);
2305   assert(DstRegOp.isReg() && "expected register operand kind");
2306
2307   if (loadImmediate(ImmOp.getImm(), DstRegOp.getReg(), Mips::NoRegister,
2308                     Is32BitImm, false, IDLoc, Instructions))
2309     return true;
2310
2311   return false;
2312 }
2313
2314 bool MipsAsmParser::expandLoadAddress(unsigned DstReg, unsigned BaseReg,
2315                                       const MCOperand &Offset,
2316                                       bool Is32BitAddress, SMLoc IDLoc,
2317                                       SmallVectorImpl<MCInst> &Instructions) {
2318   // la can't produce a usable address when addresses are 64-bit.
2319   if (Is32BitAddress && ABI.ArePtrs64bit()) {
2320     // FIXME: Demote this to a warning and continue as if we had 'dla' instead.
2321     //        We currently can't do this because we depend on the equality
2322     //        operator and N64 can end up with a GPR32/GPR64 mismatch.
2323     Error(IDLoc, "la used to load 64-bit address");
2324     // Continue as if we had 'dla' instead.
2325     Is32BitAddress = false;
2326   }
2327
2328   // dla requires 64-bit addresses.
2329   if (!Is32BitAddress && !ABI.ArePtrs64bit()) {
2330     Error(IDLoc, "instruction requires a 64-bit architecture");
2331     return true;
2332   }
2333
2334   if (!Offset.isImm())
2335     return loadAndAddSymbolAddress(Offset.getExpr(), DstReg, BaseReg,
2336                                    Is32BitAddress, IDLoc, Instructions);
2337
2338   return loadImmediate(Offset.getImm(), DstReg, BaseReg, Is32BitAddress, true,
2339                        IDLoc, Instructions);
2340 }
2341
2342 bool MipsAsmParser::loadAndAddSymbolAddress(
2343     const MCExpr *SymExpr, unsigned DstReg, unsigned SrcReg, bool Is32BitSym,
2344     SMLoc IDLoc, SmallVectorImpl<MCInst> &Instructions) {
2345   warnIfNoMacro(IDLoc);
2346
2347   const MCExpr *Symbol = cast<MCExpr>(SymExpr);
2348   const MipsMCExpr *HiExpr = MipsMCExpr::create(
2349       MCSymbolRefExpr::VK_Mips_ABS_HI, Symbol, getContext());
2350   const MipsMCExpr *LoExpr = MipsMCExpr::create(
2351       MCSymbolRefExpr::VK_Mips_ABS_LO, Symbol, getContext());
2352
2353   bool UseSrcReg = SrcReg != Mips::NoRegister;
2354
2355   // This is the 64-bit symbol address expansion.
2356   if (ABI.ArePtrs64bit() && isGP64bit()) {
2357     // We always need AT for the 64-bit expansion.
2358     // If it is not available we exit.
2359     unsigned ATReg = getATReg(IDLoc);
2360     if (!ATReg)
2361       return true;
2362
2363     const MipsMCExpr *HighestExpr = MipsMCExpr::create(
2364         MCSymbolRefExpr::VK_Mips_HIGHEST, Symbol, getContext());
2365     const MipsMCExpr *HigherExpr = MipsMCExpr::create(
2366         MCSymbolRefExpr::VK_Mips_HIGHER, Symbol, getContext());
2367
2368     if (UseSrcReg && (DstReg == SrcReg)) {
2369       // If $rs is the same as $rd:
2370       // (d)la $rd, sym($rd) => lui    $at, %highest(sym)
2371       //                        daddiu $at, $at, %higher(sym)
2372       //                        dsll   $at, $at, 16
2373       //                        daddiu $at, $at, %hi(sym)
2374       //                        dsll   $at, $at, 16
2375       //                        daddiu $at, $at, %lo(sym)
2376       //                        daddu  $rd, $at, $rd
2377       emitRX(Mips::LUi, ATReg, MCOperand::createExpr(HighestExpr), IDLoc,
2378              Instructions);
2379       emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(HigherExpr),
2380               IDLoc, Instructions);
2381       emitRRI(Mips::DSLL, ATReg, ATReg, 16, IDLoc, Instructions);
2382       emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(HiExpr), IDLoc,
2383               Instructions);
2384       emitRRI(Mips::DSLL, ATReg, ATReg, 16, IDLoc, Instructions);
2385       emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(LoExpr), IDLoc,
2386               Instructions);
2387       emitRRR(Mips::DADDu, DstReg, ATReg, SrcReg, IDLoc, Instructions);
2388
2389       return false;
2390     }
2391
2392     // Otherwise, if the $rs is different from $rd or if $rs isn't specified:
2393     // (d)la $rd, sym/sym($rs) => lui    $rd, %highest(sym)
2394     //                            lui    $at, %hi(sym)
2395     //                            daddiu $rd, $rd, %higher(sym)
2396     //                            daddiu $at, $at, %lo(sym)
2397     //                            dsll32 $rd, $rd, 0
2398     //                            daddu  $rd, $rd, $at
2399     //                            (daddu  $rd, $rd, $rs)
2400     emitRX(Mips::LUi, DstReg, MCOperand::createExpr(HighestExpr), IDLoc,
2401            Instructions);
2402     emitRX(Mips::LUi, ATReg, MCOperand::createExpr(HiExpr), IDLoc,
2403            Instructions);
2404     emitRRX(Mips::DADDiu, DstReg, DstReg, MCOperand::createExpr(HigherExpr),
2405             IDLoc, Instructions);
2406     emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(LoExpr), IDLoc,
2407             Instructions);
2408     emitRRI(Mips::DSLL32, DstReg, DstReg, 0, IDLoc, Instructions);
2409     emitRRR(Mips::DADDu, DstReg, DstReg, ATReg, IDLoc, Instructions);
2410     if (UseSrcReg)
2411       emitRRR(Mips::DADDu, DstReg, DstReg, SrcReg, IDLoc, Instructions);
2412
2413     return false;
2414   }
2415
2416   // And now, the 32-bit symbol address expansion:
2417   // If $rs is the same as $rd:
2418   // (d)la $rd, sym($rd)     => lui   $at, %hi(sym)
2419   //                            ori   $at, $at, %lo(sym)
2420   //                            addu  $rd, $at, $rd
2421   // Otherwise, if the $rs is different from $rd or if $rs isn't specified:
2422   // (d)la $rd, sym/sym($rs) => lui   $rd, %hi(sym)
2423   //                            ori   $rd, $rd, %lo(sym)
2424   //                            (addu $rd, $rd, $rs)
2425   unsigned TmpReg = DstReg;
2426   if (UseSrcReg && (DstReg == SrcReg)) {
2427     // If $rs is the same as $rd, we need to use AT.
2428     // If it is not available we exit.
2429     unsigned ATReg = getATReg(IDLoc);
2430     if (!ATReg)
2431       return true;
2432     TmpReg = ATReg;
2433   }
2434
2435   emitRX(Mips::LUi, TmpReg, MCOperand::createExpr(HiExpr), IDLoc, Instructions);
2436   emitRRX(Mips::ADDiu, TmpReg, TmpReg, MCOperand::createExpr(LoExpr), IDLoc,
2437           Instructions);
2438
2439   if (UseSrcReg)
2440     emitRRR(Mips::ADDu, DstReg, TmpReg, SrcReg, IDLoc, Instructions);
2441   else
2442     assert(DstReg == TmpReg);
2443
2444   return false;
2445 }
2446
2447 bool MipsAsmParser::expandUncondBranchMMPseudo(
2448     MCInst &Inst, SMLoc IDLoc, SmallVectorImpl<MCInst> &Instructions) {
2449   assert(getInstDesc(Inst.getOpcode()).getNumOperands() == 1 &&
2450          "unexpected number of operands");
2451
2452   MCOperand Offset = Inst.getOperand(0);
2453   if (Offset.isExpr()) {
2454     Inst.clear();
2455     Inst.setOpcode(Mips::BEQ_MM);
2456     Inst.addOperand(MCOperand::createReg(Mips::ZERO));
2457     Inst.addOperand(MCOperand::createReg(Mips::ZERO));
2458     Inst.addOperand(MCOperand::createExpr(Offset.getExpr()));
2459   } else {
2460     assert(Offset.isImm() && "expected immediate operand kind");
2461     if (isInt<11>(Offset.getImm())) {
2462       // If offset fits into 11 bits then this instruction becomes microMIPS
2463       // 16-bit unconditional branch instruction.
2464       if (inMicroMipsMode())
2465         Inst.setOpcode(hasMips32r6() ? Mips::BC16_MMR6 : Mips::B16_MM);
2466     } else {
2467       if (!isInt<17>(Offset.getImm()))
2468         Error(IDLoc, "branch target out of range");
2469       if (OffsetToAlignment(Offset.getImm(), 1LL << 1))
2470         Error(IDLoc, "branch to misaligned address");
2471       Inst.clear();
2472       Inst.setOpcode(Mips::BEQ_MM);
2473       Inst.addOperand(MCOperand::createReg(Mips::ZERO));
2474       Inst.addOperand(MCOperand::createReg(Mips::ZERO));
2475       Inst.addOperand(MCOperand::createImm(Offset.getImm()));
2476     }
2477   }
2478   Instructions.push_back(Inst);
2479
2480   // If .set reorder is active and branch instruction has a delay slot,
2481   // emit a NOP after it.
2482   const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
2483   if (MCID.hasDelaySlot() && AssemblerOptions.back()->isReorder())
2484     createNop(true, IDLoc, Instructions);
2485
2486   return false;
2487 }
2488
2489 bool MipsAsmParser::expandBranchImm(MCInst &Inst, SMLoc IDLoc,
2490                                     SmallVectorImpl<MCInst> &Instructions) {
2491   const MCOperand &DstRegOp = Inst.getOperand(0);
2492   assert(DstRegOp.isReg() && "expected register operand kind");
2493
2494   const MCOperand &ImmOp = Inst.getOperand(1);
2495   assert(ImmOp.isImm() && "expected immediate operand kind");
2496
2497   const MCOperand &MemOffsetOp = Inst.getOperand(2);
2498   assert(MemOffsetOp.isImm() && "expected immediate operand kind");
2499
2500   unsigned OpCode = 0;
2501   switch(Inst.getOpcode()) {
2502     case Mips::BneImm:
2503       OpCode = Mips::BNE;
2504       break;
2505     case Mips::BeqImm:
2506       OpCode = Mips::BEQ;
2507       break;
2508     default:
2509       llvm_unreachable("Unknown immediate branch pseudo-instruction.");
2510       break;
2511   }
2512
2513   int64_t ImmValue = ImmOp.getImm();
2514   if (ImmValue == 0)
2515     emitRRX(OpCode, DstRegOp.getReg(), Mips::ZERO, MemOffsetOp, IDLoc,
2516             Instructions);
2517   else {
2518     warnIfNoMacro(IDLoc);
2519
2520     unsigned ATReg = getATReg(IDLoc);
2521     if (!ATReg)
2522       return true;
2523
2524     if (loadImmediate(ImmValue, ATReg, Mips::NoRegister, !isGP64bit(), true,
2525                       IDLoc, Instructions))
2526       return true;
2527
2528     emitRRX(OpCode, DstRegOp.getReg(), ATReg, MemOffsetOp, IDLoc, Instructions);
2529   }
2530   return false;
2531 }
2532
2533 void MipsAsmParser::expandMemInst(MCInst &Inst, SMLoc IDLoc,
2534                                   SmallVectorImpl<MCInst> &Instructions,
2535                                   bool isLoad, bool isImmOpnd) {
2536   unsigned ImmOffset, HiOffset, LoOffset;
2537   const MCExpr *ExprOffset;
2538   unsigned TmpRegNum;
2539   // 1st operand is either the source or destination register.
2540   assert(Inst.getOperand(0).isReg() && "expected register operand kind");
2541   unsigned RegOpNum = Inst.getOperand(0).getReg();
2542   // 2nd operand is the base register.
2543   assert(Inst.getOperand(1).isReg() && "expected register operand kind");
2544   unsigned BaseRegNum = Inst.getOperand(1).getReg();
2545   // 3rd operand is either an immediate or expression.
2546   if (isImmOpnd) {
2547     assert(Inst.getOperand(2).isImm() && "expected immediate operand kind");
2548     ImmOffset = Inst.getOperand(2).getImm();
2549     LoOffset = ImmOffset & 0x0000ffff;
2550     HiOffset = (ImmOffset & 0xffff0000) >> 16;
2551     // If msb of LoOffset is 1(negative number) we must increment HiOffset.
2552     if (LoOffset & 0x8000)
2553       HiOffset++;
2554   } else
2555     ExprOffset = Inst.getOperand(2).getExpr();
2556   // These are some of the types of expansions we perform here:
2557   // 1) lw $8, sym        => lui $8, %hi(sym)
2558   //                         lw $8, %lo(sym)($8)
2559   // 2) lw $8, offset($9) => lui $8, %hi(offset)
2560   //                         add $8, $8, $9
2561   //                         lw $8, %lo(offset)($9)
2562   // 3) lw $8, offset($8) => lui $at, %hi(offset)
2563   //                         add $at, $at, $8
2564   //                         lw $8, %lo(offset)($at)
2565   // 4) sw $8, sym        => lui $at, %hi(sym)
2566   //                         sw $8, %lo(sym)($at)
2567   // 5) sw $8, offset($8) => lui $at, %hi(offset)
2568   //                         add $at, $at, $8
2569   //                         sw $8, %lo(offset)($at)
2570   // 6) ldc1 $f0, sym     => lui $at, %hi(sym)
2571   //                         ldc1 $f0, %lo(sym)($at)
2572   //
2573   // For load instructions we can use the destination register as a temporary
2574   // if base and dst are different (examples 1 and 2) and if the base register
2575   // is general purpose otherwise we must use $at (example 6) and error if it's
2576   // not available. For stores we must use $at (examples 4 and 5) because we
2577   // must not clobber the source register setting up the offset.
2578   const MCInstrDesc &Desc = getInstDesc(Inst.getOpcode());
2579   int16_t RegClassOp0 = Desc.OpInfo[0].RegClass;
2580   unsigned RegClassIDOp0 =
2581       getContext().getRegisterInfo()->getRegClass(RegClassOp0).getID();
2582   bool IsGPR = (RegClassIDOp0 == Mips::GPR32RegClassID) ||
2583                (RegClassIDOp0 == Mips::GPR64RegClassID);
2584   if (isLoad && IsGPR && (BaseRegNum != RegOpNum))
2585     TmpRegNum = RegOpNum;
2586   else {
2587     // At this point we need AT to perform the expansions and we exit if it is
2588     // not available.
2589     TmpRegNum = getATReg(IDLoc);
2590     if (!TmpRegNum)
2591       return;
2592   }
2593
2594   emitRX(Mips::LUi, TmpRegNum,
2595          isImmOpnd ? MCOperand::createImm(HiOffset)
2596                    : MCOperand::createExpr(evaluateRelocExpr(ExprOffset, "hi")),
2597          IDLoc, Instructions);
2598   // Add temp register to base.
2599   if (BaseRegNum != Mips::ZERO)
2600     emitRRR(Mips::ADDu, TmpRegNum, TmpRegNum, BaseRegNum, IDLoc, Instructions);
2601   // And finally, create original instruction with low part
2602   // of offset and new base.
2603   emitRRX(Inst.getOpcode(), RegOpNum, TmpRegNum,
2604           isImmOpnd
2605               ? MCOperand::createImm(LoOffset)
2606               : MCOperand::createExpr(evaluateRelocExpr(ExprOffset, "lo")),
2607           IDLoc, Instructions);
2608 }
2609
2610 bool
2611 MipsAsmParser::expandLoadStoreMultiple(MCInst &Inst, SMLoc IDLoc,
2612                                        SmallVectorImpl<MCInst> &Instructions) {
2613   unsigned OpNum = Inst.getNumOperands();
2614   unsigned Opcode = Inst.getOpcode();
2615   unsigned NewOpcode = Opcode == Mips::SWM_MM ? Mips::SWM32_MM : Mips::LWM32_MM;
2616
2617   assert (Inst.getOperand(OpNum - 1).isImm() &&
2618           Inst.getOperand(OpNum - 2).isReg() &&
2619           Inst.getOperand(OpNum - 3).isReg() && "Invalid instruction operand.");
2620
2621   if (OpNum < 8 && Inst.getOperand(OpNum - 1).getImm() <= 60 &&
2622       Inst.getOperand(OpNum - 1).getImm() >= 0 &&
2623       (Inst.getOperand(OpNum - 2).getReg() == Mips::SP ||
2624        Inst.getOperand(OpNum - 2).getReg() == Mips::SP_64) &&
2625       (Inst.getOperand(OpNum - 3).getReg() == Mips::RA ||
2626        Inst.getOperand(OpNum - 3).getReg() == Mips::RA_64)) {
2627     // It can be implemented as SWM16 or LWM16 instruction.
2628     if (inMicroMipsMode() && hasMips32r6())
2629       NewOpcode = Opcode == Mips::SWM_MM ? Mips::SWM16_MMR6 : Mips::LWM16_MMR6;
2630     else
2631       NewOpcode = Opcode == Mips::SWM_MM ? Mips::SWM16_MM : Mips::LWM16_MM;
2632   }
2633
2634   Inst.setOpcode(NewOpcode);
2635   Instructions.push_back(Inst);
2636   return false;
2637 }
2638
2639 bool MipsAsmParser::expandCondBranches(MCInst &Inst, SMLoc IDLoc,
2640                                        SmallVectorImpl<MCInst> &Instructions) {
2641   bool EmittedNoMacroWarning = false;
2642   unsigned PseudoOpcode = Inst.getOpcode();
2643   unsigned SrcReg = Inst.getOperand(0).getReg();
2644   const MCOperand &TrgOp = Inst.getOperand(1);
2645   const MCExpr *OffsetExpr = Inst.getOperand(2).getExpr();
2646
2647   unsigned ZeroSrcOpcode, ZeroTrgOpcode;
2648   bool ReverseOrderSLT, IsUnsigned, IsLikely, AcceptsEquality;
2649
2650   unsigned TrgReg;
2651   if (TrgOp.isReg())
2652     TrgReg = TrgOp.getReg();
2653   else if (TrgOp.isImm()) {
2654     warnIfNoMacro(IDLoc);
2655     EmittedNoMacroWarning = true;
2656
2657     TrgReg = getATReg(IDLoc);
2658     if (!TrgReg)
2659       return true;
2660
2661     switch(PseudoOpcode) {
2662     default:
2663       llvm_unreachable("unknown opcode for branch pseudo-instruction");
2664     case Mips::BLTImmMacro:
2665       PseudoOpcode = Mips::BLT;
2666       break;
2667     case Mips::BLEImmMacro:
2668       PseudoOpcode = Mips::BLE;
2669       break;
2670     case Mips::BGEImmMacro:
2671       PseudoOpcode = Mips::BGE;
2672       break;
2673     case Mips::BGTImmMacro:
2674       PseudoOpcode = Mips::BGT;
2675       break;
2676     case Mips::BLTUImmMacro:
2677       PseudoOpcode = Mips::BLTU;
2678       break;
2679     case Mips::BLEUImmMacro:
2680       PseudoOpcode = Mips::BLEU;
2681       break;
2682     case Mips::BGEUImmMacro:
2683       PseudoOpcode = Mips::BGEU;
2684       break;
2685     case Mips::BGTUImmMacro:
2686       PseudoOpcode = Mips::BGTU;
2687       break;
2688     case Mips::BLTLImmMacro:
2689       PseudoOpcode = Mips::BLTL;
2690       break;
2691     case Mips::BLELImmMacro:
2692       PseudoOpcode = Mips::BLEL;
2693       break;
2694     case Mips::BGELImmMacro:
2695       PseudoOpcode = Mips::BGEL;
2696       break;
2697     case Mips::BGTLImmMacro:
2698       PseudoOpcode = Mips::BGTL;
2699       break;
2700     case Mips::BLTULImmMacro:
2701       PseudoOpcode = Mips::BLTUL;
2702       break;
2703     case Mips::BLEULImmMacro:
2704       PseudoOpcode = Mips::BLEUL;
2705       break;
2706     case Mips::BGEULImmMacro:
2707       PseudoOpcode = Mips::BGEUL;
2708       break;
2709     case Mips::BGTULImmMacro:
2710       PseudoOpcode = Mips::BGTUL;
2711       break;
2712     }
2713
2714     if (loadImmediate(TrgOp.getImm(), TrgReg, Mips::NoRegister, !isGP64bit(),
2715                       false, IDLoc, Instructions))
2716       return true;
2717   }
2718
2719   switch (PseudoOpcode) {
2720   case Mips::BLT:
2721   case Mips::BLTU:
2722   case Mips::BLTL:
2723   case Mips::BLTUL:
2724     AcceptsEquality = false;
2725     ReverseOrderSLT = false;
2726     IsUnsigned = ((PseudoOpcode == Mips::BLTU) || (PseudoOpcode == Mips::BLTUL));
2727     IsLikely = ((PseudoOpcode == Mips::BLTL) || (PseudoOpcode == Mips::BLTUL));
2728     ZeroSrcOpcode = Mips::BGTZ;
2729     ZeroTrgOpcode = Mips::BLTZ;
2730     break;
2731   case Mips::BLE:
2732   case Mips::BLEU:
2733   case Mips::BLEL:
2734   case Mips::BLEUL:
2735     AcceptsEquality = true;
2736     ReverseOrderSLT = true;
2737     IsUnsigned = ((PseudoOpcode == Mips::BLEU) || (PseudoOpcode == Mips::BLEUL));
2738     IsLikely = ((PseudoOpcode == Mips::BLEL) || (PseudoOpcode == Mips::BLEUL));
2739     ZeroSrcOpcode = Mips::BGEZ;
2740     ZeroTrgOpcode = Mips::BLEZ;
2741     break;
2742   case Mips::BGE:
2743   case Mips::BGEU:
2744   case Mips::BGEL:
2745   case Mips::BGEUL:
2746     AcceptsEquality = true;
2747     ReverseOrderSLT = false;
2748     IsUnsigned = ((PseudoOpcode == Mips::BGEU) || (PseudoOpcode == Mips::BGEUL));
2749     IsLikely = ((PseudoOpcode == Mips::BGEL) || (PseudoOpcode == Mips::BGEUL));
2750     ZeroSrcOpcode = Mips::BLEZ;
2751     ZeroTrgOpcode = Mips::BGEZ;
2752     break;
2753   case Mips::BGT:
2754   case Mips::BGTU:
2755   case Mips::BGTL:
2756   case Mips::BGTUL:
2757     AcceptsEquality = false;
2758     ReverseOrderSLT = true;
2759     IsUnsigned = ((PseudoOpcode == Mips::BGTU) || (PseudoOpcode == Mips::BGTUL));
2760     IsLikely = ((PseudoOpcode == Mips::BGTL) || (PseudoOpcode == Mips::BGTUL));
2761     ZeroSrcOpcode = Mips::BLTZ;
2762     ZeroTrgOpcode = Mips::BGTZ;
2763     break;
2764   default:
2765     llvm_unreachable("unknown opcode for branch pseudo-instruction");
2766   }
2767
2768   bool IsTrgRegZero = (TrgReg == Mips::ZERO);
2769   bool IsSrcRegZero = (SrcReg == Mips::ZERO);
2770   if (IsSrcRegZero && IsTrgRegZero) {
2771     // FIXME: All of these Opcode-specific if's are needed for compatibility
2772     // with GAS' behaviour. However, they may not generate the most efficient
2773     // code in some circumstances.
2774     if (PseudoOpcode == Mips::BLT) {
2775       emitRX(Mips::BLTZ, Mips::ZERO, MCOperand::createExpr(OffsetExpr), IDLoc,
2776              Instructions);
2777       return false;
2778     }
2779     if (PseudoOpcode == Mips::BLE) {
2780       emitRX(Mips::BLEZ, Mips::ZERO, MCOperand::createExpr(OffsetExpr), IDLoc,
2781              Instructions);
2782       Warning(IDLoc, "branch is always taken");
2783       return false;
2784     }
2785     if (PseudoOpcode == Mips::BGE) {
2786       emitRX(Mips::BGEZ, Mips::ZERO, MCOperand::createExpr(OffsetExpr), IDLoc,
2787              Instructions);
2788       Warning(IDLoc, "branch is always taken");
2789       return false;
2790     }
2791     if (PseudoOpcode == Mips::BGT) {
2792       emitRX(Mips::BGTZ, Mips::ZERO, MCOperand::createExpr(OffsetExpr), IDLoc,
2793              Instructions);
2794       return false;
2795     }
2796     if (PseudoOpcode == Mips::BGTU) {
2797       emitRRX(Mips::BNE, Mips::ZERO, Mips::ZERO,
2798               MCOperand::createExpr(OffsetExpr), IDLoc, Instructions);
2799       return false;
2800     }
2801     if (AcceptsEquality) {
2802       // If both registers are $0 and the pseudo-branch accepts equality, it
2803       // will always be taken, so we emit an unconditional branch.
2804       emitRRX(Mips::BEQ, Mips::ZERO, Mips::ZERO,
2805               MCOperand::createExpr(OffsetExpr), IDLoc, Instructions);
2806       Warning(IDLoc, "branch is always taken");
2807       return false;
2808     }
2809     // If both registers are $0 and the pseudo-branch does not accept
2810     // equality, it will never be taken, so we don't have to emit anything.
2811     return false;
2812   }
2813   if (IsSrcRegZero || IsTrgRegZero) {
2814     if ((IsSrcRegZero && PseudoOpcode == Mips::BGTU) ||
2815         (IsTrgRegZero && PseudoOpcode == Mips::BLTU)) {
2816       // If the $rs is $0 and the pseudo-branch is BGTU (0 > x) or
2817       // if the $rt is $0 and the pseudo-branch is BLTU (x < 0),
2818       // the pseudo-branch will never be taken, so we don't emit anything.
2819       // This only applies to unsigned pseudo-branches.
2820       return false;
2821     }
2822     if ((IsSrcRegZero && PseudoOpcode == Mips::BLEU) ||
2823         (IsTrgRegZero && PseudoOpcode == Mips::BGEU)) {
2824       // If the $rs is $0 and the pseudo-branch is BLEU (0 <= x) or
2825       // if the $rt is $0 and the pseudo-branch is BGEU (x >= 0),
2826       // the pseudo-branch will always be taken, so we emit an unconditional
2827       // branch.
2828       // This only applies to unsigned pseudo-branches.
2829       emitRRX(Mips::BEQ, Mips::ZERO, Mips::ZERO,
2830               MCOperand::createExpr(OffsetExpr), IDLoc, Instructions);
2831       Warning(IDLoc, "branch is always taken");
2832       return false;
2833     }
2834     if (IsUnsigned) {
2835       // If the $rs is $0 and the pseudo-branch is BLTU (0 < x) or
2836       // if the $rt is $0 and the pseudo-branch is BGTU (x > 0),
2837       // the pseudo-branch will be taken only when the non-zero register is
2838       // different from 0, so we emit a BNEZ.
2839       //
2840       // If the $rs is $0 and the pseudo-branch is BGEU (0 >= x) or
2841       // if the $rt is $0 and the pseudo-branch is BLEU (x <= 0),
2842       // the pseudo-branch will be taken only when the non-zero register is
2843       // equal to 0, so we emit a BEQZ.
2844       //
2845       // Because only BLEU and BGEU branch on equality, we can use the
2846       // AcceptsEquality variable to decide when to emit the BEQZ.
2847       emitRRX(AcceptsEquality ? Mips::BEQ : Mips::BNE,
2848               IsSrcRegZero ? TrgReg : SrcReg, Mips::ZERO,
2849               MCOperand::createExpr(OffsetExpr), IDLoc, Instructions);
2850       return false;
2851     }
2852     // If we have a signed pseudo-branch and one of the registers is $0,
2853     // we can use an appropriate compare-to-zero branch. We select which one
2854     // to use in the switch statement above.
2855     emitRX(IsSrcRegZero ? ZeroSrcOpcode : ZeroTrgOpcode,
2856            IsSrcRegZero ? TrgReg : SrcReg, MCOperand::createExpr(OffsetExpr),
2857            IDLoc, Instructions);
2858     return false;
2859   }
2860
2861   // If neither the SrcReg nor the TrgReg are $0, we need AT to perform the
2862   // expansions. If it is not available, we return.
2863   unsigned ATRegNum = getATReg(IDLoc);
2864   if (!ATRegNum)
2865     return true;
2866
2867   if (!EmittedNoMacroWarning)
2868     warnIfNoMacro(IDLoc);
2869
2870   // SLT fits well with 2 of our 4 pseudo-branches:
2871   //   BLT, where $rs < $rt, translates into "slt $at, $rs, $rt" and
2872   //   BGT, where $rs > $rt, translates into "slt $at, $rt, $rs".
2873   // If the result of the SLT is 1, we branch, and if it's 0, we don't.
2874   // This is accomplished by using a BNEZ with the result of the SLT.
2875   //
2876   // The other 2 pseudo-branches are opposites of the above 2 (BGE with BLT
2877   // and BLE with BGT), so we change the BNEZ into a a BEQZ.
2878   // Because only BGE and BLE branch on equality, we can use the
2879   // AcceptsEquality variable to decide when to emit the BEQZ.
2880   // Note that the order of the SLT arguments doesn't change between
2881   // opposites.
2882   //
2883   // The same applies to the unsigned variants, except that SLTu is used
2884   // instead of SLT.
2885   emitRRR(IsUnsigned ? Mips::SLTu : Mips::SLT, ATRegNum,
2886           ReverseOrderSLT ? TrgReg : SrcReg, ReverseOrderSLT ? SrcReg : TrgReg,
2887           IDLoc, Instructions);
2888
2889   emitRRX(IsLikely ? (AcceptsEquality ? Mips::BEQL : Mips::BNEL)
2890                    : (AcceptsEquality ? Mips::BEQ : Mips::BNE),
2891           ATRegNum, Mips::ZERO, MCOperand::createExpr(OffsetExpr), IDLoc,
2892           Instructions);
2893   return false;
2894 }
2895
2896 bool MipsAsmParser::expandDiv(MCInst &Inst, SMLoc IDLoc,
2897                               SmallVectorImpl<MCInst> &Instructions,
2898                               const bool IsMips64, const bool Signed) {
2899   if (hasMips32r6()) {
2900     Error(IDLoc, "instruction not supported on mips32r6 or mips64r6");
2901     return false;
2902   }
2903
2904   warnIfNoMacro(IDLoc);
2905
2906   const MCOperand &RsRegOp = Inst.getOperand(0);
2907   assert(RsRegOp.isReg() && "expected register operand kind");
2908   unsigned RsReg = RsRegOp.getReg();
2909
2910   const MCOperand &RtRegOp = Inst.getOperand(1);
2911   assert(RtRegOp.isReg() && "expected register operand kind");
2912   unsigned RtReg = RtRegOp.getReg();
2913   unsigned DivOp;
2914   unsigned ZeroReg;
2915
2916   if (IsMips64) {
2917     DivOp = Signed ? Mips::DSDIV : Mips::DUDIV;
2918     ZeroReg = Mips::ZERO_64;
2919   } else {
2920     DivOp = Signed ? Mips::SDIV : Mips::UDIV;
2921     ZeroReg = Mips::ZERO;
2922   }
2923
2924   bool UseTraps = useTraps();
2925
2926   if (RsReg == Mips::ZERO || RsReg == Mips::ZERO_64) {
2927     if (RtReg == Mips::ZERO || RtReg == Mips::ZERO_64)
2928       Warning(IDLoc, "dividing zero by zero");
2929     if (IsMips64) {
2930       if (Signed && (RtReg == Mips::ZERO || RtReg == Mips::ZERO_64)) {
2931         if (UseTraps) {
2932           emitRRI(Mips::TEQ, RtReg, ZeroReg, 0x7, IDLoc, Instructions);
2933           return false;
2934         }
2935
2936         emitII(Mips::BREAK, 0x7, 0, IDLoc, Instructions);
2937         return false;
2938       }
2939     } else {
2940       emitRR(DivOp, RsReg, RtReg, IDLoc, Instructions);
2941       return false;
2942     }
2943   }
2944
2945   if (RtReg == Mips::ZERO || RtReg == Mips::ZERO_64) {
2946     Warning(IDLoc, "division by zero");
2947     if (Signed) {
2948       if (UseTraps) {
2949         emitRRI(Mips::TEQ, RtReg, ZeroReg, 0x7, IDLoc, Instructions);
2950         return false;
2951       }
2952
2953       emitII(Mips::BREAK, 0x7, 0, IDLoc, Instructions);
2954       return false;
2955     }
2956   }
2957
2958   // FIXME: The values for these two BranchTarget variables may be different in
2959   // micromips. These magic numbers need to be removed.
2960   unsigned BranchTargetNoTraps;
2961   unsigned BranchTarget;
2962
2963   if (UseTraps) {
2964     BranchTarget = IsMips64 ? 12 : 8;
2965     emitRRI(Mips::TEQ, RtReg, ZeroReg, 0x7, IDLoc, Instructions);
2966   } else {
2967     BranchTarget = IsMips64 ? 20 : 16;
2968     BranchTargetNoTraps = 8;
2969     // Branch to the li instruction.
2970     emitRRI(Mips::BNE, RtReg, ZeroReg, BranchTargetNoTraps, IDLoc,
2971             Instructions);
2972   }
2973
2974   emitRR(DivOp, RsReg, RtReg, IDLoc, Instructions);
2975
2976   if (!UseTraps)
2977     emitII(Mips::BREAK, 0x7, 0, IDLoc, Instructions);
2978
2979   if (!Signed) {
2980     emitR(Mips::MFLO, RsReg, IDLoc, Instructions);
2981     return false;
2982   }
2983
2984   unsigned ATReg = getATReg(IDLoc);
2985   if (!ATReg)
2986     return true;
2987
2988   emitRRI(Mips::ADDiu, ATReg, ZeroReg, -1, IDLoc, Instructions);
2989   if (IsMips64) {
2990     // Branch to the mflo instruction.
2991     emitRRI(Mips::BNE, RtReg, ATReg, BranchTarget, IDLoc, Instructions);
2992     emitRRI(Mips::ADDiu, ATReg, ZeroReg, 1, IDLoc, Instructions);
2993     emitRRI(Mips::DSLL32, ATReg, ATReg, 0x1f, IDLoc, Instructions);
2994   } else {
2995     // Branch to the mflo instruction.
2996     emitRRI(Mips::BNE, RtReg, ATReg, BranchTarget, IDLoc, Instructions);
2997     emitRI(Mips::LUi, ATReg, (uint16_t)0x8000, IDLoc, Instructions);
2998   }
2999
3000   if (UseTraps)
3001     emitRRI(Mips::TEQ, RsReg, ATReg, 0x6, IDLoc, Instructions);
3002   else {
3003     // Branch to the mflo instruction.
3004     emitRRI(Mips::BNE, RsReg, ATReg, BranchTargetNoTraps, IDLoc, Instructions);
3005     emitRRI(Mips::SLL, ZeroReg, ZeroReg, 0, IDLoc, Instructions);
3006     emitII(Mips::BREAK, 0x6, 0, IDLoc, Instructions);
3007   }
3008   emitR(Mips::MFLO, RsReg, IDLoc, Instructions);
3009   return false;
3010 }
3011
3012 bool MipsAsmParser::expandUlh(MCInst &Inst, bool Signed, SMLoc IDLoc,
3013                               SmallVectorImpl<MCInst> &Instructions) {
3014   if (hasMips32r6() || hasMips64r6()) {
3015     Error(IDLoc, "instruction not supported on mips32r6 or mips64r6");
3016     return false;
3017   }
3018
3019   warnIfNoMacro(IDLoc);
3020
3021   const MCOperand &DstRegOp = Inst.getOperand(0);
3022   assert(DstRegOp.isReg() && "expected register operand kind");
3023
3024   const MCOperand &SrcRegOp = Inst.getOperand(1);
3025   assert(SrcRegOp.isReg() && "expected register operand kind");
3026
3027   const MCOperand &OffsetImmOp = Inst.getOperand(2);
3028   assert(OffsetImmOp.isImm() && "expected immediate operand kind");
3029
3030   unsigned DstReg = DstRegOp.getReg();
3031   unsigned SrcReg = SrcRegOp.getReg();
3032   int64_t OffsetValue = OffsetImmOp.getImm();
3033
3034   // NOTE: We always need AT for ULHU, as it is always used as the source
3035   // register for one of the LBu's.
3036   unsigned ATReg = getATReg(IDLoc);
3037   if (!ATReg)
3038     return true;
3039
3040   // When the value of offset+1 does not fit in 16 bits, we have to load the
3041   // offset in AT, (D)ADDu the original source register (if there was one), and
3042   // then use AT as the source register for the 2 generated LBu's.
3043   bool LoadedOffsetInAT = false;
3044   if (!isInt<16>(OffsetValue + 1) || !isInt<16>(OffsetValue)) {
3045     LoadedOffsetInAT = true;
3046
3047     if (loadImmediate(OffsetValue, ATReg, Mips::NoRegister, !ABI.ArePtrs64bit(),
3048                       true, IDLoc, Instructions))
3049       return true;
3050
3051     // NOTE: We do this (D)ADDu here instead of doing it in loadImmediate()
3052     // because it will make our output more similar to GAS'. For example,
3053     // generating an "ori $1, $zero, 32768" followed by an "addu $1, $1, $9",
3054     // instead of just an "ori $1, $9, 32768".
3055     // NOTE: If there is no source register specified in the ULHU, the parser
3056     // will interpret it as $0.
3057     if (SrcReg != Mips::ZERO && SrcReg != Mips::ZERO_64)
3058       createAddu(ATReg, ATReg, SrcReg, ABI.ArePtrs64bit(), Instructions);
3059   }
3060
3061   unsigned FirstLbuDstReg = LoadedOffsetInAT ? DstReg : ATReg;
3062   unsigned SecondLbuDstReg = LoadedOffsetInAT ? ATReg : DstReg;
3063   unsigned LbuSrcReg = LoadedOffsetInAT ? ATReg : SrcReg;
3064
3065   int64_t FirstLbuOffset = 0, SecondLbuOffset = 0;
3066   if (isLittle()) {
3067     FirstLbuOffset = LoadedOffsetInAT ? 1 : (OffsetValue + 1);
3068     SecondLbuOffset = LoadedOffsetInAT ? 0 : OffsetValue;
3069   } else {
3070     FirstLbuOffset = LoadedOffsetInAT ? 0 : OffsetValue;
3071     SecondLbuOffset = LoadedOffsetInAT ? 1 : (OffsetValue + 1);
3072   }
3073
3074   unsigned SllReg = LoadedOffsetInAT ? DstReg : ATReg;
3075
3076   emitRRI(Signed ? Mips::LB : Mips::LBu, FirstLbuDstReg, LbuSrcReg,
3077           FirstLbuOffset, IDLoc, Instructions);
3078
3079   emitRRI(Mips::LBu, SecondLbuDstReg, LbuSrcReg, SecondLbuOffset, IDLoc,
3080           Instructions);
3081
3082   emitRRI(Mips::SLL, SllReg, SllReg, 8, IDLoc, Instructions);
3083
3084   emitRRR(Mips::OR, DstReg, DstReg, ATReg, IDLoc, Instructions);
3085
3086   return false;
3087 }
3088
3089 bool MipsAsmParser::expandUlw(MCInst &Inst, SMLoc IDLoc,
3090                               SmallVectorImpl<MCInst> &Instructions) {
3091   if (hasMips32r6() || hasMips64r6()) {
3092     Error(IDLoc, "instruction not supported on mips32r6 or mips64r6");
3093     return false;
3094   }
3095
3096   const MCOperand &DstRegOp = Inst.getOperand(0);
3097   assert(DstRegOp.isReg() && "expected register operand kind");
3098
3099   const MCOperand &SrcRegOp = Inst.getOperand(1);
3100   assert(SrcRegOp.isReg() && "expected register operand kind");
3101
3102   const MCOperand &OffsetImmOp = Inst.getOperand(2);
3103   assert(OffsetImmOp.isImm() && "expected immediate operand kind");
3104
3105   unsigned SrcReg = SrcRegOp.getReg();
3106   int64_t OffsetValue = OffsetImmOp.getImm();
3107   unsigned ATReg = 0;
3108
3109   // When the value of offset+3 does not fit in 16 bits, we have to load the
3110   // offset in AT, (D)ADDu the original source register (if there was one), and
3111   // then use AT as the source register for the generated LWL and LWR.
3112   bool LoadedOffsetInAT = false;
3113   if (!isInt<16>(OffsetValue + 3) || !isInt<16>(OffsetValue)) {
3114     ATReg = getATReg(IDLoc);
3115     if (!ATReg)
3116       return true;
3117     LoadedOffsetInAT = true;
3118
3119     warnIfNoMacro(IDLoc);
3120
3121     if (loadImmediate(OffsetValue, ATReg, Mips::NoRegister, !ABI.ArePtrs64bit(),
3122                       true, IDLoc, Instructions))
3123       return true;
3124
3125     // NOTE: We do this (D)ADDu here instead of doing it in loadImmediate()
3126     // because it will make our output more similar to GAS'. For example,
3127     // generating an "ori $1, $zero, 32768" followed by an "addu $1, $1, $9",
3128     // instead of just an "ori $1, $9, 32768".
3129     // NOTE: If there is no source register specified in the ULW, the parser
3130     // will interpret it as $0.
3131     if (SrcReg != Mips::ZERO && SrcReg != Mips::ZERO_64)
3132       createAddu(ATReg, ATReg, SrcReg, ABI.ArePtrs64bit(), Instructions);
3133   }
3134
3135   unsigned FinalSrcReg = LoadedOffsetInAT ? ATReg : SrcReg;
3136   int64_t LeftLoadOffset = 0, RightLoadOffset  = 0;
3137   if (isLittle()) {
3138     LeftLoadOffset = LoadedOffsetInAT ? 3 : (OffsetValue + 3);
3139     RightLoadOffset  = LoadedOffsetInAT ? 0 : OffsetValue;
3140   } else {
3141     LeftLoadOffset = LoadedOffsetInAT ? 0 : OffsetValue;
3142     RightLoadOffset  = LoadedOffsetInAT ? 3 : (OffsetValue + 3);
3143   }
3144
3145   emitRRI(Mips::LWL, DstRegOp.getReg(), FinalSrcReg, LeftLoadOffset, IDLoc,
3146           Instructions);
3147
3148   emitRRI(Mips::LWR, DstRegOp.getReg(), FinalSrcReg, RightLoadOffset, IDLoc,
3149           Instructions);
3150
3151   return false;
3152 }
3153
3154 bool MipsAsmParser::expandAliasImmediate(MCInst &Inst, SMLoc IDLoc,
3155                                          SmallVectorImpl<MCInst> &Instructions) {
3156
3157   assert (Inst.getNumOperands() == 3 && "Invalid operand count");
3158   assert (Inst.getOperand(0).isReg() &&
3159           Inst.getOperand(1).isReg() &&
3160           Inst.getOperand(2).isImm() && "Invalid instruction operand.");
3161
3162   unsigned ATReg = Mips::NoRegister;
3163   unsigned FinalDstReg = Mips::NoRegister;
3164   unsigned DstReg = Inst.getOperand(0).getReg();
3165   unsigned SrcReg = Inst.getOperand(1).getReg();
3166   int64_t ImmValue = Inst.getOperand(2).getImm();
3167
3168   bool Is32Bit = isInt<32>(ImmValue) || isUInt<32>(ImmValue);
3169
3170   unsigned FinalOpcode = Inst.getOpcode();
3171
3172   if (DstReg == SrcReg) {
3173     ATReg = getATReg(Inst.getLoc());
3174     if (!ATReg)
3175       return true;
3176     FinalDstReg = DstReg;
3177     DstReg = ATReg;
3178   }
3179
3180   if (!loadImmediate(ImmValue, DstReg, Mips::NoRegister, Is32Bit, false, Inst.getLoc(), Instructions)) {
3181     switch (FinalOpcode) {
3182     default:
3183       llvm_unreachable("unimplemented expansion");
3184     case (Mips::ADDi):
3185       FinalOpcode = Mips::ADD;
3186       break;
3187     case (Mips::ADDiu):
3188       FinalOpcode = Mips::ADDu;
3189       break;
3190     case (Mips::ANDi):
3191       FinalOpcode = Mips::AND;
3192       break;
3193     case (Mips::NORImm):
3194       FinalOpcode = Mips::NOR;
3195       break;
3196     case (Mips::ORi):
3197       FinalOpcode = Mips::OR;
3198       break;
3199     case (Mips::SLTi):
3200       FinalOpcode = Mips::SLT;
3201       break;
3202     case (Mips::SLTiu):
3203       FinalOpcode = Mips::SLTu;
3204       break;
3205     case (Mips::XORi):
3206       FinalOpcode = Mips::XOR;
3207       break;
3208     }
3209
3210     if (FinalDstReg == Mips::NoRegister)
3211       emitRRR(FinalOpcode, DstReg, DstReg, SrcReg, IDLoc, Instructions);
3212     else
3213       emitRRR(FinalOpcode, FinalDstReg, FinalDstReg, DstReg, IDLoc,
3214               Instructions);
3215     return false;
3216   }
3217   return true;
3218 }
3219
3220 void MipsAsmParser::createNop(bool hasShortDelaySlot, SMLoc IDLoc,
3221                               SmallVectorImpl<MCInst> &Instructions) {
3222   if (hasShortDelaySlot)
3223     emitRR(Mips::MOVE16_MM, Mips::ZERO, Mips::ZERO, IDLoc, Instructions);
3224   else
3225     emitRRI(Mips::SLL, Mips::ZERO, Mips::ZERO, 0, IDLoc, Instructions);
3226 }
3227
3228 void MipsAsmParser::createAddu(unsigned DstReg, unsigned SrcReg,
3229                                unsigned TrgReg, bool Is64Bit,
3230                                SmallVectorImpl<MCInst> &Instructions) {
3231   emitRRR(Is64Bit ? Mips::DADDu : Mips::ADDu, DstReg, SrcReg, TrgReg, SMLoc(),
3232           Instructions);
3233 }
3234
3235 void MipsAsmParser::createCpRestoreMemOp(
3236     bool IsLoad, int StackOffset, SMLoc IDLoc,
3237     SmallVectorImpl<MCInst> &Instructions) {
3238   // If the offset can not fit into 16 bits, we need to expand.
3239   if (!isInt<16>(StackOffset)) {
3240     MCInst MemInst;
3241     MemInst.setOpcode(IsLoad ? Mips::LW : Mips::SW);
3242     MemInst.addOperand(MCOperand::createReg(Mips::GP));
3243     MemInst.addOperand(MCOperand::createReg(Mips::SP));
3244     MemInst.addOperand(MCOperand::createImm(StackOffset));
3245     expandMemInst(MemInst, IDLoc, Instructions, IsLoad, true /*HasImmOpnd*/);
3246     return;
3247   }
3248
3249   emitRRI(IsLoad ? Mips::LW : Mips::SW, Mips::GP, Mips::SP, StackOffset, IDLoc,
3250           Instructions);
3251 }
3252
3253 unsigned MipsAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
3254   // As described by the Mips32r2 spec, the registers Rd and Rs for
3255   // jalr.hb must be different.
3256   unsigned Opcode = Inst.getOpcode();
3257
3258   if (Opcode == Mips::JALR_HB &&
3259       (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()))
3260     return Match_RequiresDifferentSrcAndDst;
3261
3262   return Match_Success;
3263 }
3264
3265 static SMLoc RefineErrorLoc(const SMLoc Loc, const OperandVector &Operands,
3266                             uint64_t ErrorInfo) {
3267   if (ErrorInfo != ~0ULL && ErrorInfo < Operands.size()) {
3268     SMLoc ErrorLoc = Operands[ErrorInfo]->getStartLoc();
3269     if (ErrorLoc == SMLoc())
3270       return Loc;
3271     return ErrorLoc;
3272   }
3273   return Loc;
3274 }
3275
3276 bool MipsAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
3277                                             OperandVector &Operands,
3278                                             MCStreamer &Out,
3279                                             uint64_t &ErrorInfo,
3280                                             bool MatchingInlineAsm) {
3281
3282   MCInst Inst;
3283   SmallVector<MCInst, 8> Instructions;
3284   unsigned MatchResult =
3285       MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
3286
3287   switch (MatchResult) {
3288   case Match_Success: {
3289     if (processInstruction(Inst, IDLoc, Instructions))
3290       return true;
3291     for (unsigned i = 0; i < Instructions.size(); i++)
3292       Out.EmitInstruction(Instructions[i], STI);
3293     return false;
3294   }
3295   case Match_MissingFeature:
3296     Error(IDLoc, "instruction requires a CPU feature not currently enabled");
3297     return true;
3298   case Match_InvalidOperand: {
3299     SMLoc ErrorLoc = IDLoc;
3300     if (ErrorInfo != ~0ULL) {
3301       if (ErrorInfo >= Operands.size())
3302         return Error(IDLoc, "too few operands for instruction");
3303
3304       ErrorLoc = Operands[ErrorInfo]->getStartLoc();
3305       if (ErrorLoc == SMLoc())
3306         ErrorLoc = IDLoc;
3307     }
3308
3309     return Error(ErrorLoc, "invalid operand for instruction");
3310   }
3311   case Match_MnemonicFail:
3312     return Error(IDLoc, "invalid instruction");
3313   case Match_RequiresDifferentSrcAndDst:
3314     return Error(IDLoc, "source and destination must be different");
3315   case Match_Immz:
3316     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), "expected '0'");
3317   case Match_UImm1_0:
3318     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
3319                  "expected 1-bit unsigned immediate");
3320   case Match_UImm2_0:
3321     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
3322                  "expected 2-bit unsigned immediate");
3323   case Match_UImm2_1:
3324     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
3325                  "expected immediate in range 1 .. 4");
3326   case Match_UImm3_0:
3327     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
3328                  "expected 3-bit unsigned immediate");
3329   case Match_UImm4_0:
3330     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
3331                  "expected 4-bit unsigned immediate");
3332   }
3333
3334   llvm_unreachable("Implement any new match types added!");
3335 }
3336
3337 void MipsAsmParser::warnIfRegIndexIsAT(unsigned RegIndex, SMLoc Loc) {
3338   if (RegIndex != 0 && AssemblerOptions.back()->getATRegIndex() == RegIndex)
3339     Warning(Loc, "used $at (currently $" + Twine(RegIndex) +
3340                      ") without \".set noat\"");
3341 }
3342
3343 void MipsAsmParser::warnIfNoMacro(SMLoc Loc) {
3344   if (!AssemblerOptions.back()->isMacro())
3345     Warning(Loc, "macro instruction expanded into multiple instructions");
3346 }
3347
3348 void
3349 MipsAsmParser::printWarningWithFixIt(const Twine &Msg, const Twine &FixMsg,
3350                                      SMRange Range, bool ShowColors) {
3351   getSourceManager().PrintMessage(Range.Start, SourceMgr::DK_Warning, Msg,
3352                                   Range, SMFixIt(Range, FixMsg),
3353                                   ShowColors);
3354 }
3355
3356 int MipsAsmParser::matchCPURegisterName(StringRef Name) {
3357   int CC;
3358
3359   CC = StringSwitch<unsigned>(Name)
3360            .Case("zero", 0)
3361            .Case("at", 1)
3362            .Case("a0", 4)
3363            .Case("a1", 5)
3364            .Case("a2", 6)
3365            .Case("a3", 7)
3366            .Case("v0", 2)
3367            .Case("v1", 3)
3368            .Case("s0", 16)
3369            .Case("s1", 17)
3370            .Case("s2", 18)
3371            .Case("s3", 19)
3372            .Case("s4", 20)
3373            .Case("s5", 21)
3374            .Case("s6", 22)
3375            .Case("s7", 23)
3376            .Case("k0", 26)
3377            .Case("k1", 27)
3378            .Case("gp", 28)
3379            .Case("sp", 29)
3380            .Case("fp", 30)
3381            .Case("s8", 30)
3382            .Case("ra", 31)
3383            .Case("t0", 8)
3384            .Case("t1", 9)
3385            .Case("t2", 10)
3386            .Case("t3", 11)
3387            .Case("t4", 12)
3388            .Case("t5", 13)
3389            .Case("t6", 14)
3390            .Case("t7", 15)
3391            .Case("t8", 24)
3392            .Case("t9", 25)
3393            .Default(-1);
3394
3395   if (!(isABI_N32() || isABI_N64()))
3396     return CC;
3397
3398   if (12 <= CC && CC <= 15) {
3399     // Name is one of t4-t7
3400     AsmToken RegTok = getLexer().peekTok();
3401     SMRange RegRange = RegTok.getLocRange();
3402
3403     StringRef FixedName = StringSwitch<StringRef>(Name)
3404                               .Case("t4", "t0")
3405                               .Case("t5", "t1")
3406                               .Case("t6", "t2")
3407                               .Case("t7", "t3")
3408                               .Default("");
3409     assert(FixedName != "" &&  "Register name is not one of t4-t7.");
3410
3411     printWarningWithFixIt("register names $t4-$t7 are only available in O32.",
3412                           "Did you mean $" + FixedName + "?", RegRange);
3413   }
3414
3415   // Although SGI documentation just cuts out t0-t3 for n32/n64,
3416   // GNU pushes the values of t0-t3 to override the o32/o64 values for t4-t7
3417   // We are supporting both cases, so for t0-t3 we'll just push them to t4-t7.
3418   if (8 <= CC && CC <= 11)
3419     CC += 4;
3420
3421   if (CC == -1)
3422     CC = StringSwitch<unsigned>(Name)
3423              .Case("a4", 8)
3424              .Case("a5", 9)
3425              .Case("a6", 10)
3426              .Case("a7", 11)
3427              .Case("kt0", 26)
3428              .Case("kt1", 27)
3429              .Default(-1);
3430
3431   return CC;
3432 }
3433
3434 int MipsAsmParser::matchHWRegsRegisterName(StringRef Name) {
3435   int CC;
3436
3437   CC = StringSwitch<unsigned>(Name)
3438             .Case("hwr_cpunum", 0)
3439             .Case("hwr_synci_step", 1)
3440             .Case("hwr_cc", 2)
3441             .Case("hwr_ccres", 3)
3442             .Case("hwr_ulr", 29)
3443             .Default(-1);
3444
3445   return CC;
3446 }
3447
3448 int MipsAsmParser::matchFPURegisterName(StringRef Name) {
3449
3450   if (Name[0] == 'f') {
3451     StringRef NumString = Name.substr(1);
3452     unsigned IntVal;
3453     if (NumString.getAsInteger(10, IntVal))
3454       return -1;     // This is not an integer.
3455     if (IntVal > 31) // Maximum index for fpu register.
3456       return -1;
3457     return IntVal;
3458   }
3459   return -1;
3460 }
3461
3462 int MipsAsmParser::matchFCCRegisterName(StringRef Name) {
3463
3464   if (Name.startswith("fcc")) {
3465     StringRef NumString = Name.substr(3);
3466     unsigned IntVal;
3467     if (NumString.getAsInteger(10, IntVal))
3468       return -1;    // This is not an integer.
3469     if (IntVal > 7) // There are only 8 fcc registers.
3470       return -1;
3471     return IntVal;
3472   }
3473   return -1;
3474 }
3475
3476 int MipsAsmParser::matchACRegisterName(StringRef Name) {
3477
3478   if (Name.startswith("ac")) {
3479     StringRef NumString = Name.substr(2);
3480     unsigned IntVal;
3481     if (NumString.getAsInteger(10, IntVal))
3482       return -1;    // This is not an integer.
3483     if (IntVal > 3) // There are only 3 acc registers.
3484       return -1;
3485     return IntVal;
3486   }
3487   return -1;
3488 }
3489
3490 int MipsAsmParser::matchMSA128RegisterName(StringRef Name) {
3491   unsigned IntVal;
3492
3493   if (Name.front() != 'w' || Name.drop_front(1).getAsInteger(10, IntVal))
3494     return -1;
3495
3496   if (IntVal > 31)
3497     return -1;
3498
3499   return IntVal;
3500 }
3501
3502 int MipsAsmParser::matchMSA128CtrlRegisterName(StringRef Name) {
3503   int CC;
3504
3505   CC = StringSwitch<unsigned>(Name)
3506            .Case("msair", 0)
3507            .Case("msacsr", 1)
3508            .Case("msaaccess", 2)
3509            .Case("msasave", 3)
3510            .Case("msamodify", 4)
3511            .Case("msarequest", 5)
3512            .Case("msamap", 6)
3513            .Case("msaunmap", 7)
3514            .Default(-1);
3515
3516   return CC;
3517 }
3518
3519 unsigned MipsAsmParser::getATReg(SMLoc Loc) {
3520   unsigned ATIndex = AssemblerOptions.back()->getATRegIndex();
3521   if (ATIndex == 0) {
3522     reportParseError(Loc,
3523                      "pseudo-instruction requires $at, which is not available");
3524     return 0;
3525   }
3526   unsigned AT = getReg(
3527       (isGP64bit()) ? Mips::GPR64RegClassID : Mips::GPR32RegClassID, ATIndex);
3528   return AT;
3529 }
3530
3531 unsigned MipsAsmParser::getReg(int RC, int RegNo) {
3532   return *(getContext().getRegisterInfo()->getRegClass(RC).begin() + RegNo);
3533 }
3534
3535 unsigned MipsAsmParser::getGPR(int RegNo) {
3536   return getReg(isGP64bit() ? Mips::GPR64RegClassID : Mips::GPR32RegClassID,
3537                 RegNo);
3538 }
3539
3540 int MipsAsmParser::matchRegisterByNumber(unsigned RegNum, unsigned RegClass) {
3541   if (RegNum >
3542       getContext().getRegisterInfo()->getRegClass(RegClass).getNumRegs() - 1)
3543     return -1;
3544
3545   return getReg(RegClass, RegNum);
3546 }
3547
3548 bool MipsAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
3549   MCAsmParser &Parser = getParser();
3550   DEBUG(dbgs() << "parseOperand\n");
3551
3552   // Check if the current operand has a custom associated parser, if so, try to
3553   // custom parse the operand, or fallback to the general approach.
3554   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
3555   if (ResTy == MatchOperand_Success)
3556     return false;
3557   // If there wasn't a custom match, try the generic matcher below. Otherwise,
3558   // there was a match, but an error occurred, in which case, just return that
3559   // the operand parsing failed.
3560   if (ResTy == MatchOperand_ParseFail)
3561     return true;
3562
3563   DEBUG(dbgs() << ".. Generic Parser\n");
3564
3565   switch (getLexer().getKind()) {
3566   default:
3567     Error(Parser.getTok().getLoc(), "unexpected token in operand");
3568     return true;
3569   case AsmToken::Dollar: {
3570     // Parse the register.
3571     SMLoc S = Parser.getTok().getLoc();
3572
3573     // Almost all registers have been parsed by custom parsers. There is only
3574     // one exception to this. $zero (and it's alias $0) will reach this point
3575     // for div, divu, and similar instructions because it is not an operand
3576     // to the instruction definition but an explicit register. Special case
3577     // this situation for now.
3578     if (parseAnyRegister(Operands) != MatchOperand_NoMatch)
3579       return false;
3580
3581     // Maybe it is a symbol reference.
3582     StringRef Identifier;
3583     if (Parser.parseIdentifier(Identifier))
3584       return true;
3585
3586     SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3587     MCSymbol *Sym = getContext().getOrCreateSymbol("$" + Identifier);
3588     // Otherwise create a symbol reference.
3589     const MCExpr *Res =
3590         MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
3591
3592     Operands.push_back(MipsOperand::CreateImm(Res, S, E, *this));
3593     return false;
3594   }
3595   // Else drop to expression parsing.
3596   case AsmToken::LParen:
3597   case AsmToken::Minus:
3598   case AsmToken::Plus:
3599   case AsmToken::Integer:
3600   case AsmToken::Tilde:
3601   case AsmToken::String: {
3602     DEBUG(dbgs() << ".. generic integer\n");
3603     OperandMatchResultTy ResTy = parseImm(Operands);
3604     return ResTy != MatchOperand_Success;
3605   }
3606   case AsmToken::Percent: {
3607     // It is a symbol reference or constant expression.
3608     const MCExpr *IdVal;
3609     SMLoc S = Parser.getTok().getLoc(); // Start location of the operand.
3610     if (parseRelocOperand(IdVal))
3611       return true;
3612
3613     SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3614
3615     Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
3616     return false;
3617   } // case AsmToken::Percent
3618   } // switch(getLexer().getKind())
3619   return true;
3620 }
3621
3622 const MCExpr *MipsAsmParser::evaluateRelocExpr(const MCExpr *Expr,
3623                                                StringRef RelocStr) {
3624   const MCExpr *Res;
3625   // Check the type of the expression.
3626   if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Expr)) {
3627     // It's a constant, evaluate reloc value.
3628     int16_t Val;
3629     switch (getVariantKind(RelocStr)) {
3630     case MCSymbolRefExpr::VK_Mips_ABS_LO:
3631       // Get the 1st 16-bits.
3632       Val = MCE->getValue() & 0xffff;
3633       break;
3634     case MCSymbolRefExpr::VK_Mips_ABS_HI:
3635       // Get the 2nd 16-bits. Also add 1 if bit 15 is 1, to compensate for low
3636       // 16 bits being negative.
3637       Val = ((MCE->getValue() + 0x8000) >> 16) & 0xffff;
3638       break;
3639     case MCSymbolRefExpr::VK_Mips_HIGHER:
3640       // Get the 3rd 16-bits.
3641       Val = ((MCE->getValue() + 0x80008000LL) >> 32) & 0xffff;
3642       break;
3643     case MCSymbolRefExpr::VK_Mips_HIGHEST:
3644       // Get the 4th 16-bits.
3645       Val = ((MCE->getValue() + 0x800080008000LL) >> 48) & 0xffff;
3646       break;
3647     default:
3648       report_fatal_error("unsupported reloc value");
3649     }
3650     return MCConstantExpr::create(Val, getContext());
3651   }
3652
3653   if (const MCSymbolRefExpr *MSRE = dyn_cast<MCSymbolRefExpr>(Expr)) {
3654     // It's a symbol, create a symbolic expression from the symbol.
3655     const MCSymbol *Symbol = &MSRE->getSymbol();
3656     MCSymbolRefExpr::VariantKind VK = getVariantKind(RelocStr);
3657     Res = MCSymbolRefExpr::create(Symbol, VK, getContext());
3658     return Res;
3659   }
3660
3661   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
3662     MCSymbolRefExpr::VariantKind VK = getVariantKind(RelocStr);
3663
3664     // Try to create target expression.
3665     if (MipsMCExpr::isSupportedBinaryExpr(VK, BE))
3666       return MipsMCExpr::create(VK, Expr, getContext());
3667
3668     const MCExpr *LExp = evaluateRelocExpr(BE->getLHS(), RelocStr);
3669     const MCExpr *RExp = evaluateRelocExpr(BE->getRHS(), RelocStr);
3670     Res = MCBinaryExpr::create(BE->getOpcode(), LExp, RExp, getContext());
3671     return Res;
3672   }
3673
3674   if (const MCUnaryExpr *UN = dyn_cast<MCUnaryExpr>(Expr)) {
3675     const MCExpr *UnExp = evaluateRelocExpr(UN->getSubExpr(), RelocStr);
3676     Res = MCUnaryExpr::create(UN->getOpcode(), UnExp, getContext());
3677     return Res;
3678   }
3679   // Just return the original expression.
3680   return Expr;
3681 }
3682
3683 bool MipsAsmParser::isEvaluated(const MCExpr *Expr) {
3684
3685   switch (Expr->getKind()) {
3686   case MCExpr::Constant:
3687     return true;
3688   case MCExpr::SymbolRef:
3689     return (cast<MCSymbolRefExpr>(Expr)->getKind() != MCSymbolRefExpr::VK_None);
3690   case MCExpr::Binary:
3691     if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
3692       if (!isEvaluated(BE->getLHS()))
3693         return false;
3694       return isEvaluated(BE->getRHS());
3695     }
3696   case MCExpr::Unary:
3697     return isEvaluated(cast<MCUnaryExpr>(Expr)->getSubExpr());
3698   case MCExpr::Target:
3699     return true;
3700   }
3701   return false;
3702 }
3703
3704 bool MipsAsmParser::parseRelocOperand(const MCExpr *&Res) {
3705   MCAsmParser &Parser = getParser();
3706   Parser.Lex();                          // Eat the % token.
3707   const AsmToken &Tok = Parser.getTok(); // Get next token, operation.
3708   if (Tok.isNot(AsmToken::Identifier))
3709     return true;
3710
3711   std::string Str = Tok.getIdentifier();
3712
3713   Parser.Lex(); // Eat the identifier.
3714   // Now make an expression from the rest of the operand.
3715   const MCExpr *IdVal;
3716   SMLoc EndLoc;
3717
3718   if (getLexer().getKind() == AsmToken::LParen) {
3719     while (1) {
3720       Parser.Lex(); // Eat the '(' token.
3721       if (getLexer().getKind() == AsmToken::Percent) {
3722         Parser.Lex(); // Eat the % token.
3723         const AsmToken &nextTok = Parser.getTok();
3724         if (nextTok.isNot(AsmToken::Identifier))
3725           return true;
3726         Str += "(%";
3727         Str += nextTok.getIdentifier();
3728         Parser.Lex(); // Eat the identifier.
3729         if (getLexer().getKind() != AsmToken::LParen)
3730           return true;
3731       } else
3732         break;
3733     }
3734     if (getParser().parseParenExpression(IdVal, EndLoc))
3735       return true;
3736
3737     while (getLexer().getKind() == AsmToken::RParen)
3738       Parser.Lex(); // Eat the ')' token.
3739
3740   } else
3741     return true; // Parenthesis must follow the relocation operand.
3742
3743   Res = evaluateRelocExpr(IdVal, Str);
3744   return false;
3745 }
3746
3747 bool MipsAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
3748                                   SMLoc &EndLoc) {
3749   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Operands;
3750   OperandMatchResultTy ResTy = parseAnyRegister(Operands);
3751   if (ResTy == MatchOperand_Success) {
3752     assert(Operands.size() == 1);
3753     MipsOperand &Operand = static_cast<MipsOperand &>(*Operands.front());
3754     StartLoc = Operand.getStartLoc();
3755     EndLoc = Operand.getEndLoc();
3756
3757     // AFAIK, we only support numeric registers and named GPR's in CFI
3758     // directives.
3759     // Don't worry about eating tokens before failing. Using an unrecognised
3760     // register is a parse error.
3761     if (Operand.isGPRAsmReg()) {
3762       // Resolve to GPR32 or GPR64 appropriately.
3763       RegNo = isGP64bit() ? Operand.getGPR64Reg() : Operand.getGPR32Reg();
3764     }
3765
3766     return (RegNo == (unsigned)-1);
3767   }
3768
3769   assert(Operands.size() == 0);
3770   return (RegNo == (unsigned)-1);
3771 }
3772
3773 bool MipsAsmParser::parseMemOffset(const MCExpr *&Res, bool isParenExpr) {
3774   MCAsmParser &Parser = getParser();
3775   SMLoc S;
3776   bool Result = true;
3777   unsigned NumOfLParen = 0;
3778
3779   while (getLexer().getKind() == AsmToken::LParen) {
3780     Parser.Lex();
3781     ++NumOfLParen;
3782   }
3783
3784   switch (getLexer().getKind()) {
3785   default:
3786     return true;
3787   case AsmToken::Identifier:
3788   case AsmToken::LParen:
3789   case AsmToken::Integer:
3790   case AsmToken::Minus:
3791   case AsmToken::Plus:
3792     if (isParenExpr)
3793       Result = getParser().parseParenExprOfDepth(NumOfLParen, Res, S);
3794     else
3795       Result = (getParser().parseExpression(Res));
3796     while (getLexer().getKind() == AsmToken::RParen)
3797       Parser.Lex();
3798     break;
3799   case AsmToken::Percent:
3800     Result = parseRelocOperand(Res);
3801   }
3802   return Result;
3803 }
3804
3805 MipsAsmParser::OperandMatchResultTy
3806 MipsAsmParser::parseMemOperand(OperandVector &Operands) {
3807   MCAsmParser &Parser = getParser();
3808   DEBUG(dbgs() << "parseMemOperand\n");
3809   const MCExpr *IdVal = nullptr;
3810   SMLoc S;
3811   bool isParenExpr = false;
3812   MipsAsmParser::OperandMatchResultTy Res = MatchOperand_NoMatch;
3813   // First operand is the offset.
3814   S = Parser.getTok().getLoc();
3815
3816   if (getLexer().getKind() == AsmToken::LParen) {
3817     Parser.Lex();
3818     isParenExpr = true;
3819   }
3820
3821   if (getLexer().getKind() != AsmToken::Dollar) {
3822     if (parseMemOffset(IdVal, isParenExpr))
3823       return MatchOperand_ParseFail;
3824
3825     const AsmToken &Tok = Parser.getTok(); // Get the next token.
3826     if (Tok.isNot(AsmToken::LParen)) {
3827       MipsOperand &Mnemonic = static_cast<MipsOperand &>(*Operands[0]);
3828       if (Mnemonic.getToken() == "la" || Mnemonic.getToken() == "dla") {
3829         SMLoc E =
3830             SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3831         Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
3832         return MatchOperand_Success;
3833       }
3834       if (Tok.is(AsmToken::EndOfStatement)) {
3835         SMLoc E =
3836             SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3837
3838         // Zero register assumed, add a memory operand with ZERO as its base.
3839         // "Base" will be managed by k_Memory.
3840         auto Base = MipsOperand::createGPRReg(0, getContext().getRegisterInfo(),
3841                                               S, E, *this);
3842         Operands.push_back(
3843             MipsOperand::CreateMem(std::move(Base), IdVal, S, E, *this));
3844         return MatchOperand_Success;
3845       }
3846       Error(Parser.getTok().getLoc(), "'(' expected");
3847       return MatchOperand_ParseFail;
3848     }
3849
3850     Parser.Lex(); // Eat the '(' token.
3851   }
3852
3853   Res = parseAnyRegister(Operands);
3854   if (Res != MatchOperand_Success)
3855     return Res;
3856
3857   if (Parser.getTok().isNot(AsmToken::RParen)) {
3858     Error(Parser.getTok().getLoc(), "')' expected");
3859     return MatchOperand_ParseFail;
3860   }
3861
3862   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
3863
3864   Parser.Lex(); // Eat the ')' token.
3865
3866   if (!IdVal)
3867     IdVal = MCConstantExpr::create(0, getContext());
3868
3869   // Replace the register operand with the memory operand.
3870   std::unique_ptr<MipsOperand> op(
3871       static_cast<MipsOperand *>(Operands.back().release()));
3872   // Remove the register from the operands.
3873   // "op" will be managed by k_Memory.
3874   Operands.pop_back();
3875   // Add the memory operand.
3876   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(IdVal)) {
3877     int64_t Imm;
3878     if (IdVal->evaluateAsAbsolute(Imm))
3879       IdVal = MCConstantExpr::create(Imm, getContext());
3880     else if (BE->getLHS()->getKind() != MCExpr::SymbolRef)
3881       IdVal = MCBinaryExpr::create(BE->getOpcode(), BE->getRHS(), BE->getLHS(),
3882                                    getContext());
3883   }
3884
3885   Operands.push_back(MipsOperand::CreateMem(std::move(op), IdVal, S, E, *this));
3886   return MatchOperand_Success;
3887 }
3888
3889 bool MipsAsmParser::searchSymbolAlias(OperandVector &Operands) {
3890   MCAsmParser &Parser = getParser();
3891   MCSymbol *Sym = getContext().lookupSymbol(Parser.getTok().getIdentifier());
3892   if (Sym) {
3893     SMLoc S = Parser.getTok().getLoc();
3894     const MCExpr *Expr;
3895     if (Sym->isVariable())
3896       Expr = Sym->getVariableValue();
3897     else
3898       return false;
3899     if (Expr->getKind() == MCExpr::SymbolRef) {
3900       const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
3901       StringRef DefSymbol = Ref->getSymbol().getName();
3902       if (DefSymbol.startswith("$")) {
3903         OperandMatchResultTy ResTy =
3904             matchAnyRegisterNameWithoutDollar(Operands, DefSymbol.substr(1), S);
3905         if (ResTy == MatchOperand_Success) {
3906           Parser.Lex();
3907           return true;
3908         } else if (ResTy == MatchOperand_ParseFail)
3909           llvm_unreachable("Should never ParseFail");
3910         return false;
3911       }
3912     } else if (Expr->getKind() == MCExpr::Constant) {
3913       Parser.Lex();
3914       const MCConstantExpr *Const = static_cast<const MCConstantExpr *>(Expr);
3915       Operands.push_back(
3916           MipsOperand::CreateImm(Const, S, Parser.getTok().getLoc(), *this));
3917       return true;
3918     }
3919   }
3920   return false;
3921 }
3922
3923 MipsAsmParser::OperandMatchResultTy
3924 MipsAsmParser::matchAnyRegisterNameWithoutDollar(OperandVector &Operands,
3925                                                  StringRef Identifier,
3926                                                  SMLoc S) {
3927   int Index = matchCPURegisterName(Identifier);
3928   if (Index != -1) {
3929     Operands.push_back(MipsOperand::createGPRReg(
3930         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3931     return MatchOperand_Success;
3932   }
3933
3934   Index = matchHWRegsRegisterName(Identifier);
3935   if (Index != -1) {
3936     Operands.push_back(MipsOperand::createHWRegsReg(
3937         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3938     return MatchOperand_Success;
3939   }
3940
3941   Index = matchFPURegisterName(Identifier);
3942   if (Index != -1) {
3943     Operands.push_back(MipsOperand::createFGRReg(
3944         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3945     return MatchOperand_Success;
3946   }
3947
3948   Index = matchFCCRegisterName(Identifier);
3949   if (Index != -1) {
3950     Operands.push_back(MipsOperand::createFCCReg(
3951         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3952     return MatchOperand_Success;
3953   }
3954
3955   Index = matchACRegisterName(Identifier);
3956   if (Index != -1) {
3957     Operands.push_back(MipsOperand::createACCReg(
3958         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3959     return MatchOperand_Success;
3960   }
3961
3962   Index = matchMSA128RegisterName(Identifier);
3963   if (Index != -1) {
3964     Operands.push_back(MipsOperand::createMSA128Reg(
3965         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3966     return MatchOperand_Success;
3967   }
3968
3969   Index = matchMSA128CtrlRegisterName(Identifier);
3970   if (Index != -1) {
3971     Operands.push_back(MipsOperand::createMSACtrlReg(
3972         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
3973     return MatchOperand_Success;
3974   }
3975
3976   return MatchOperand_NoMatch;
3977 }
3978
3979 MipsAsmParser::OperandMatchResultTy
3980 MipsAsmParser::matchAnyRegisterWithoutDollar(OperandVector &Operands, SMLoc S) {
3981   MCAsmParser &Parser = getParser();
3982   auto Token = Parser.getLexer().peekTok(false);
3983
3984   if (Token.is(AsmToken::Identifier)) {
3985     DEBUG(dbgs() << ".. identifier\n");
3986     StringRef Identifier = Token.getIdentifier();
3987     OperandMatchResultTy ResTy =
3988         matchAnyRegisterNameWithoutDollar(Operands, Identifier, S);
3989     return ResTy;
3990   } else if (Token.is(AsmToken::Integer)) {
3991     DEBUG(dbgs() << ".. integer\n");
3992     Operands.push_back(MipsOperand::createNumericReg(
3993         Token.getIntVal(), getContext().getRegisterInfo(), S, Token.getLoc(),
3994         *this));
3995     return MatchOperand_Success;
3996   }
3997
3998   DEBUG(dbgs() << Parser.getTok().getKind() << "\n");
3999
4000   return MatchOperand_NoMatch;
4001 }
4002
4003 MipsAsmParser::OperandMatchResultTy
4004 MipsAsmParser::parseAnyRegister(OperandVector &Operands) {
4005   MCAsmParser &Parser = getParser();
4006   DEBUG(dbgs() << "parseAnyRegister\n");
4007
4008   auto Token = Parser.getTok();
4009
4010   SMLoc S = Token.getLoc();
4011
4012   if (Token.isNot(AsmToken::Dollar)) {
4013     DEBUG(dbgs() << ".. !$ -> try sym aliasing\n");
4014     if (Token.is(AsmToken::Identifier)) {
4015       if (searchSymbolAlias(Operands))
4016         return MatchOperand_Success;
4017     }
4018     DEBUG(dbgs() << ".. !symalias -> NoMatch\n");
4019     return MatchOperand_NoMatch;
4020   }
4021   DEBUG(dbgs() << ".. $\n");
4022
4023   OperandMatchResultTy ResTy = matchAnyRegisterWithoutDollar(Operands, S);
4024   if (ResTy == MatchOperand_Success) {
4025     Parser.Lex(); // $
4026     Parser.Lex(); // identifier
4027   }
4028   return ResTy;
4029 }
4030
4031 MipsAsmParser::OperandMatchResultTy
4032 MipsAsmParser::parseImm(OperandVector &Operands) {
4033   MCAsmParser &Parser = getParser();
4034   switch (getLexer().getKind()) {
4035   default:
4036     return MatchOperand_NoMatch;
4037   case AsmToken::LParen:
4038   case AsmToken::Minus:
4039   case AsmToken::Plus:
4040   case AsmToken::Integer:
4041   case AsmToken::Tilde:
4042   case AsmToken::String:
4043     break;
4044   }
4045
4046   const MCExpr *IdVal;
4047   SMLoc S = Parser.getTok().getLoc();
4048   if (getParser().parseExpression(IdVal))
4049     return MatchOperand_ParseFail;
4050
4051   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
4052   Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
4053   return MatchOperand_Success;
4054 }
4055
4056 MipsAsmParser::OperandMatchResultTy
4057 MipsAsmParser::parseJumpTarget(OperandVector &Operands) {
4058   MCAsmParser &Parser = getParser();
4059   DEBUG(dbgs() << "parseJumpTarget\n");
4060
4061   SMLoc S = getLexer().getLoc();
4062
4063   // Integers and expressions are acceptable
4064   OperandMatchResultTy ResTy = parseImm(Operands);
4065   if (ResTy != MatchOperand_NoMatch)
4066     return ResTy;
4067
4068   // Registers are a valid target and have priority over symbols.
4069   ResTy = parseAnyRegister(Operands);
4070   if (ResTy != MatchOperand_NoMatch)
4071     return ResTy;
4072
4073   const MCExpr *Expr = nullptr;
4074   if (Parser.parseExpression(Expr)) {
4075     // We have no way of knowing if a symbol was consumed so we must ParseFail
4076     return MatchOperand_ParseFail;
4077   }
4078   Operands.push_back(
4079       MipsOperand::CreateImm(Expr, S, getLexer().getLoc(), *this));
4080   return MatchOperand_Success;
4081 }
4082
4083 MipsAsmParser::OperandMatchResultTy
4084 MipsAsmParser::parseInvNum(OperandVector &Operands) {
4085   MCAsmParser &Parser = getParser();
4086   const MCExpr *IdVal;
4087   // If the first token is '$' we may have register operand.
4088   if (Parser.getTok().is(AsmToken::Dollar))
4089     return MatchOperand_NoMatch;
4090   SMLoc S = Parser.getTok().getLoc();
4091   if (getParser().parseExpression(IdVal))
4092     return MatchOperand_ParseFail;
4093   const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(IdVal);
4094   assert(MCE && "Unexpected MCExpr type.");
4095   int64_t Val = MCE->getValue();
4096   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
4097   Operands.push_back(MipsOperand::CreateImm(
4098       MCConstantExpr::create(0 - Val, getContext()), S, E, *this));
4099   return MatchOperand_Success;
4100 }
4101
4102 MipsAsmParser::OperandMatchResultTy
4103 MipsAsmParser::parseLSAImm(OperandVector &Operands) {
4104   MCAsmParser &Parser = getParser();
4105   switch (getLexer().getKind()) {
4106   default:
4107     return MatchOperand_NoMatch;
4108   case AsmToken::LParen:
4109   case AsmToken::Plus:
4110   case AsmToken::Minus:
4111   case AsmToken::Integer:
4112     break;
4113   }
4114
4115   const MCExpr *Expr;
4116   SMLoc S = Parser.getTok().getLoc();
4117
4118   if (getParser().parseExpression(Expr))
4119     return MatchOperand_ParseFail;
4120
4121   int64_t Val;
4122   if (!Expr->evaluateAsAbsolute(Val)) {
4123     Error(S, "expected immediate value");
4124     return MatchOperand_ParseFail;
4125   }
4126
4127   // The LSA instruction allows a 2-bit unsigned immediate. For this reason
4128   // and because the CPU always adds one to the immediate field, the allowed
4129   // range becomes 1..4. We'll only check the range here and will deal
4130   // with the addition/subtraction when actually decoding/encoding
4131   // the instruction.
4132   if (Val < 1 || Val > 4) {
4133     Error(S, "immediate not in range (1..4)");
4134     return MatchOperand_ParseFail;
4135   }
4136
4137   Operands.push_back(
4138       MipsOperand::CreateImm(Expr, S, Parser.getTok().getLoc(), *this));
4139   return MatchOperand_Success;
4140 }
4141
4142 MipsAsmParser::OperandMatchResultTy
4143 MipsAsmParser::parseRegisterList(OperandVector &Operands) {
4144   MCAsmParser &Parser = getParser();
4145   SmallVector<unsigned, 10> Regs;
4146   unsigned RegNo;
4147   unsigned PrevReg = Mips::NoRegister;
4148   bool RegRange = false;
4149   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> TmpOperands;
4150
4151   if (Parser.getTok().isNot(AsmToken::Dollar))
4152     return MatchOperand_ParseFail;
4153
4154   SMLoc S = Parser.getTok().getLoc();
4155   while (parseAnyRegister(TmpOperands) == MatchOperand_Success) {
4156     SMLoc E = getLexer().getLoc();
4157     MipsOperand &Reg = static_cast<MipsOperand &>(*TmpOperands.back());
4158     RegNo = isGP64bit() ? Reg.getGPR64Reg() : Reg.getGPR32Reg();
4159     if (RegRange) {
4160       // Remove last register operand because registers from register range
4161       // should be inserted first.
4162       if ((isGP64bit() && RegNo == Mips::RA_64) ||
4163           (!isGP64bit() && RegNo == Mips::RA)) {
4164         Regs.push_back(RegNo);
4165       } else {
4166         unsigned TmpReg = PrevReg + 1;
4167         while (TmpReg <= RegNo) {
4168           if ((((TmpReg < Mips::S0) || (TmpReg > Mips::S7)) && !isGP64bit()) ||
4169               (((TmpReg < Mips::S0_64) || (TmpReg > Mips::S7_64)) &&
4170                isGP64bit())) {
4171             Error(E, "invalid register operand");
4172             return MatchOperand_ParseFail;
4173           }
4174
4175           PrevReg = TmpReg;
4176           Regs.push_back(TmpReg++);
4177         }
4178       }
4179
4180       RegRange = false;
4181     } else {
4182       if ((PrevReg == Mips::NoRegister) &&
4183           ((isGP64bit() && (RegNo != Mips::S0_64) && (RegNo != Mips::RA_64)) ||
4184           (!isGP64bit() && (RegNo != Mips::S0) && (RegNo != Mips::RA)))) {
4185         Error(E, "$16 or $31 expected");
4186         return MatchOperand_ParseFail;
4187       } else if (!(((RegNo == Mips::FP || RegNo == Mips::RA ||
4188                     (RegNo >= Mips::S0 && RegNo <= Mips::S7)) &&
4189                     !isGP64bit()) ||
4190                    ((RegNo == Mips::FP_64 || RegNo == Mips::RA_64 ||
4191                     (RegNo >= Mips::S0_64 && RegNo <= Mips::S7_64)) &&
4192                     isGP64bit()))) {
4193         Error(E, "invalid register operand");
4194         return MatchOperand_ParseFail;
4195       } else if ((PrevReg != Mips::NoRegister) && (RegNo != PrevReg + 1) &&
4196                  ((RegNo != Mips::FP && RegNo != Mips::RA && !isGP64bit()) ||
4197                   (RegNo != Mips::FP_64 && RegNo != Mips::RA_64 &&
4198                    isGP64bit()))) {
4199         Error(E, "consecutive register numbers expected");
4200         return MatchOperand_ParseFail;
4201       }
4202
4203       Regs.push_back(RegNo);
4204     }
4205
4206     if (Parser.getTok().is(AsmToken::Minus))
4207       RegRange = true;
4208
4209     if (!Parser.getTok().isNot(AsmToken::Minus) &&
4210         !Parser.getTok().isNot(AsmToken::Comma)) {
4211       Error(E, "',' or '-' expected");
4212       return MatchOperand_ParseFail;
4213     }
4214
4215     Lex(); // Consume comma or minus
4216     if (Parser.getTok().isNot(AsmToken::Dollar))
4217       break;
4218
4219     PrevReg = RegNo;
4220   }
4221
4222   SMLoc E = Parser.getTok().getLoc();
4223   Operands.push_back(MipsOperand::CreateRegList(Regs, S, E, *this));
4224   parseMemOperand(Operands);
4225   return MatchOperand_Success;
4226 }
4227
4228 MipsAsmParser::OperandMatchResultTy
4229 MipsAsmParser::parseRegisterPair(OperandVector &Operands) {
4230   MCAsmParser &Parser = getParser();
4231
4232   SMLoc S = Parser.getTok().getLoc();
4233   if (parseAnyRegister(Operands) != MatchOperand_Success)
4234     return MatchOperand_ParseFail;
4235
4236   SMLoc E = Parser.getTok().getLoc();
4237   MipsOperand &Op = static_cast<MipsOperand &>(*Operands.back());
4238   unsigned Reg = Op.getGPR32Reg();
4239   Operands.pop_back();
4240   Operands.push_back(MipsOperand::CreateRegPair(Reg, S, E, *this));
4241   return MatchOperand_Success;
4242 }
4243
4244 MipsAsmParser::OperandMatchResultTy
4245 MipsAsmParser::parseMovePRegPair(OperandVector &Operands) {
4246   MCAsmParser &Parser = getParser();
4247   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> TmpOperands;
4248   SmallVector<unsigned, 10> Regs;
4249
4250   if (Parser.getTok().isNot(AsmToken::Dollar))
4251     return MatchOperand_ParseFail;
4252
4253   SMLoc S = Parser.getTok().getLoc();
4254
4255   if (parseAnyRegister(TmpOperands) != MatchOperand_Success)
4256     return MatchOperand_ParseFail;
4257
4258   MipsOperand *Reg = &static_cast<MipsOperand &>(*TmpOperands.back());
4259   unsigned RegNo = isGP64bit() ? Reg->getGPR64Reg() : Reg->getGPR32Reg();
4260   Regs.push_back(RegNo);
4261
4262   SMLoc E = Parser.getTok().getLoc();
4263   if (Parser.getTok().isNot(AsmToken::Comma)) {
4264     Error(E, "',' expected");
4265     return MatchOperand_ParseFail;
4266   }
4267
4268   // Remove comma.
4269   Parser.Lex();
4270
4271   if (parseAnyRegister(TmpOperands) != MatchOperand_Success)
4272     return MatchOperand_ParseFail;
4273
4274   Reg = &static_cast<MipsOperand &>(*TmpOperands.back());
4275   RegNo = isGP64bit() ? Reg->getGPR64Reg() : Reg->getGPR32Reg();
4276   Regs.push_back(RegNo);
4277
4278   Operands.push_back(MipsOperand::CreateRegList(Regs, S, E, *this));
4279
4280   return MatchOperand_Success;
4281 }
4282
4283 MCSymbolRefExpr::VariantKind MipsAsmParser::getVariantKind(StringRef Symbol) {
4284
4285   MCSymbolRefExpr::VariantKind VK =
4286       StringSwitch<MCSymbolRefExpr::VariantKind>(Symbol)
4287           .Case("hi", MCSymbolRefExpr::VK_Mips_ABS_HI)
4288           .Case("lo", MCSymbolRefExpr::VK_Mips_ABS_LO)
4289           .Case("gp_rel", MCSymbolRefExpr::VK_Mips_GPREL)
4290           .Case("call16", MCSymbolRefExpr::VK_Mips_GOT_CALL)
4291           .Case("got", MCSymbolRefExpr::VK_Mips_GOT)
4292           .Case("tlsgd", MCSymbolRefExpr::VK_Mips_TLSGD)
4293           .Case("tlsldm", MCSymbolRefExpr::VK_Mips_TLSLDM)
4294           .Case("dtprel_hi", MCSymbolRefExpr::VK_Mips_DTPREL_HI)
4295           .Case("dtprel_lo", MCSymbolRefExpr::VK_Mips_DTPREL_LO)
4296           .Case("gottprel", MCSymbolRefExpr::VK_Mips_GOTTPREL)
4297           .Case("tprel_hi", MCSymbolRefExpr::VK_Mips_TPREL_HI)
4298           .Case("tprel_lo", MCSymbolRefExpr::VK_Mips_TPREL_LO)
4299           .Case("got_disp", MCSymbolRefExpr::VK_Mips_GOT_DISP)
4300           .Case("got_page", MCSymbolRefExpr::VK_Mips_GOT_PAGE)
4301           .Case("got_ofst", MCSymbolRefExpr::VK_Mips_GOT_OFST)
4302           .Case("hi(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_HI)
4303           .Case("lo(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_LO)
4304           .Case("got_hi", MCSymbolRefExpr::VK_Mips_GOT_HI16)
4305           .Case("got_lo", MCSymbolRefExpr::VK_Mips_GOT_LO16)
4306           .Case("call_hi", MCSymbolRefExpr::VK_Mips_CALL_HI16)
4307           .Case("call_lo", MCSymbolRefExpr::VK_Mips_CALL_LO16)
4308           .Case("higher", MCSymbolRefExpr::VK_Mips_HIGHER)
4309           .Case("highest", MCSymbolRefExpr::VK_Mips_HIGHEST)
4310           .Case("pcrel_hi", MCSymbolRefExpr::VK_Mips_PCREL_HI16)
4311           .Case("pcrel_lo", MCSymbolRefExpr::VK_Mips_PCREL_LO16)
4312           .Default(MCSymbolRefExpr::VK_None);
4313
4314   assert(VK != MCSymbolRefExpr::VK_None);
4315
4316   return VK;
4317 }
4318
4319 /// Sometimes (i.e. load/stores) the operand may be followed immediately by
4320 /// either this.
4321 /// ::= '(', register, ')'
4322 /// handle it before we iterate so we don't get tripped up by the lack of
4323 /// a comma.
4324 bool MipsAsmParser::parseParenSuffix(StringRef Name, OperandVector &Operands) {
4325   MCAsmParser &Parser = getParser();
4326   if (getLexer().is(AsmToken::LParen)) {
4327     Operands.push_back(
4328         MipsOperand::CreateToken("(", getLexer().getLoc(), *this));
4329     Parser.Lex();
4330     if (parseOperand(Operands, Name)) {
4331       SMLoc Loc = getLexer().getLoc();
4332       Parser.eatToEndOfStatement();
4333       return Error(Loc, "unexpected token in argument list");
4334     }
4335     if (Parser.getTok().isNot(AsmToken::RParen)) {
4336       SMLoc Loc = getLexer().getLoc();
4337       Parser.eatToEndOfStatement();
4338       return Error(Loc, "unexpected token, expected ')'");
4339     }
4340     Operands.push_back(
4341         MipsOperand::CreateToken(")", getLexer().getLoc(), *this));
4342     Parser.Lex();
4343   }
4344   return false;
4345 }
4346
4347 /// Sometimes (i.e. in MSA) the operand may be followed immediately by
4348 /// either one of these.
4349 /// ::= '[', register, ']'
4350 /// ::= '[', integer, ']'
4351 /// handle it before we iterate so we don't get tripped up by the lack of
4352 /// a comma.
4353 bool MipsAsmParser::parseBracketSuffix(StringRef Name,
4354                                        OperandVector &Operands) {
4355   MCAsmParser &Parser = getParser();
4356   if (getLexer().is(AsmToken::LBrac)) {
4357     Operands.push_back(
4358         MipsOperand::CreateToken("[", getLexer().getLoc(), *this));
4359     Parser.Lex();
4360     if (parseOperand(Operands, Name)) {
4361       SMLoc Loc = getLexer().getLoc();
4362       Parser.eatToEndOfStatement();
4363       return Error(Loc, "unexpected token in argument list");
4364     }
4365     if (Parser.getTok().isNot(AsmToken::RBrac)) {
4366       SMLoc Loc = getLexer().getLoc();
4367       Parser.eatToEndOfStatement();
4368       return Error(Loc, "unexpected token, expected ']'");
4369     }
4370     Operands.push_back(
4371         MipsOperand::CreateToken("]", getLexer().getLoc(), *this));
4372     Parser.Lex();
4373   }
4374   return false;
4375 }
4376
4377 bool MipsAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
4378                                      SMLoc NameLoc, OperandVector &Operands) {
4379   MCAsmParser &Parser = getParser();
4380   DEBUG(dbgs() << "ParseInstruction\n");
4381
4382   // We have reached first instruction, module directive are now forbidden.
4383   getTargetStreamer().forbidModuleDirective();
4384
4385   // Check if we have valid mnemonic
4386   if (!mnemonicIsValid(Name, 0)) {
4387     Parser.eatToEndOfStatement();
4388     return Error(NameLoc, "unknown instruction");
4389   }
4390   // First operand in MCInst is instruction mnemonic.
4391   Operands.push_back(MipsOperand::CreateToken(Name, NameLoc, *this));
4392
4393   // Read the remaining operands.
4394   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4395     // Read the first operand.
4396     if (parseOperand(Operands, Name)) {
4397       SMLoc Loc = getLexer().getLoc();
4398       Parser.eatToEndOfStatement();
4399       return Error(Loc, "unexpected token in argument list");
4400     }
4401     if (getLexer().is(AsmToken::LBrac) && parseBracketSuffix(Name, Operands))
4402       return true;
4403     // AFAIK, parenthesis suffixes are never on the first operand
4404
4405     while (getLexer().is(AsmToken::Comma)) {
4406       Parser.Lex(); // Eat the comma.
4407       // Parse and remember the operand.
4408       if (parseOperand(Operands, Name)) {
4409         SMLoc Loc = getLexer().getLoc();
4410         Parser.eatToEndOfStatement();
4411         return Error(Loc, "unexpected token in argument list");
4412       }
4413       // Parse bracket and parenthesis suffixes before we iterate
4414       if (getLexer().is(AsmToken::LBrac)) {
4415         if (parseBracketSuffix(Name, Operands))
4416           return true;
4417       } else if (getLexer().is(AsmToken::LParen) &&
4418                  parseParenSuffix(Name, Operands))
4419         return true;
4420     }
4421   }
4422   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4423     SMLoc Loc = getLexer().getLoc();
4424     Parser.eatToEndOfStatement();
4425     return Error(Loc, "unexpected token in argument list");
4426   }
4427   Parser.Lex(); // Consume the EndOfStatement.
4428   return false;
4429 }
4430
4431 bool MipsAsmParser::reportParseError(Twine ErrorMsg) {
4432   MCAsmParser &Parser = getParser();
4433   SMLoc Loc = getLexer().getLoc();
4434   Parser.eatToEndOfStatement();
4435   return Error(Loc, ErrorMsg);
4436 }
4437
4438 bool MipsAsmParser::reportParseError(SMLoc Loc, Twine ErrorMsg) {
4439   return Error(Loc, ErrorMsg);
4440 }
4441
4442 bool MipsAsmParser::parseSetNoAtDirective() {
4443   MCAsmParser &Parser = getParser();
4444   // Line should look like: ".set noat".
4445
4446   // Set the $at register to $0.
4447   AssemblerOptions.back()->setATRegIndex(0);
4448
4449   Parser.Lex(); // Eat "noat".
4450
4451   // If this is not the end of the statement, report an error.
4452   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4453     reportParseError("unexpected token, expected end of statement");
4454     return false;
4455   }
4456
4457   getTargetStreamer().emitDirectiveSetNoAt();
4458   Parser.Lex(); // Consume the EndOfStatement.
4459   return false;
4460 }
4461
4462 bool MipsAsmParser::parseSetAtDirective() {
4463   // Line can be: ".set at", which sets $at to $1
4464   //          or  ".set at=$reg", which sets $at to $reg.
4465   MCAsmParser &Parser = getParser();
4466   Parser.Lex(); // Eat "at".
4467
4468   if (getLexer().is(AsmToken::EndOfStatement)) {
4469     // No register was specified, so we set $at to $1.
4470     AssemblerOptions.back()->setATRegIndex(1);
4471
4472     getTargetStreamer().emitDirectiveSetAt();
4473     Parser.Lex(); // Consume the EndOfStatement.
4474     return false;
4475   }
4476
4477   if (getLexer().isNot(AsmToken::Equal)) {
4478     reportParseError("unexpected token, expected equals sign");
4479     return false;
4480   }
4481   Parser.Lex(); // Eat "=".
4482
4483   if (getLexer().isNot(AsmToken::Dollar)) {
4484     if (getLexer().is(AsmToken::EndOfStatement)) {
4485       reportParseError("no register specified");
4486       return false;
4487     } else {
4488       reportParseError("unexpected token, expected dollar sign '$'");
4489       return false;
4490     }
4491   }
4492   Parser.Lex(); // Eat "$".
4493
4494   // Find out what "reg" is.
4495   unsigned AtRegNo;
4496   const AsmToken &Reg = Parser.getTok();
4497   if (Reg.is(AsmToken::Identifier)) {
4498     AtRegNo = matchCPURegisterName(Reg.getIdentifier());
4499   } else if (Reg.is(AsmToken::Integer)) {
4500     AtRegNo = Reg.getIntVal();
4501   } else {
4502     reportParseError("unexpected token, expected identifier or integer");
4503     return false;
4504   }
4505
4506   // Check if $reg is a valid register. If it is, set $at to $reg.
4507   if (!AssemblerOptions.back()->setATRegIndex(AtRegNo)) {
4508     reportParseError("invalid register");
4509     return false;
4510   }
4511   Parser.Lex(); // Eat "reg".
4512
4513   // If this is not the end of the statement, report an error.
4514   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4515     reportParseError("unexpected token, expected end of statement");
4516     return false;
4517   }
4518
4519   getTargetStreamer().emitDirectiveSetAtWithArg(AtRegNo);
4520
4521   Parser.Lex(); // Consume the EndOfStatement.
4522   return false;
4523 }
4524
4525 bool MipsAsmParser::parseSetReorderDirective() {
4526   MCAsmParser &Parser = getParser();
4527   Parser.Lex();
4528   // If this is not the end of the statement, report an error.
4529   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4530     reportParseError("unexpected token, expected end of statement");
4531     return false;
4532   }
4533   AssemblerOptions.back()->setReorder();
4534   getTargetStreamer().emitDirectiveSetReorder();
4535   Parser.Lex(); // Consume the EndOfStatement.
4536   return false;
4537 }
4538
4539 bool MipsAsmParser::parseSetNoReorderDirective() {
4540   MCAsmParser &Parser = getParser();
4541   Parser.Lex();
4542   // If this is not the end of the statement, report an error.
4543   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4544     reportParseError("unexpected token, expected end of statement");
4545     return false;
4546   }
4547   AssemblerOptions.back()->setNoReorder();
4548   getTargetStreamer().emitDirectiveSetNoReorder();
4549   Parser.Lex(); // Consume the EndOfStatement.
4550   return false;
4551 }
4552
4553 bool MipsAsmParser::parseSetMacroDirective() {
4554   MCAsmParser &Parser = getParser();
4555   Parser.Lex();
4556   // If this is not the end of the statement, report an error.
4557   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4558     reportParseError("unexpected token, expected end of statement");
4559     return false;
4560   }
4561   AssemblerOptions.back()->setMacro();
4562   getTargetStreamer().emitDirectiveSetMacro();
4563   Parser.Lex(); // Consume the EndOfStatement.
4564   return false;
4565 }
4566
4567 bool MipsAsmParser::parseSetNoMacroDirective() {
4568   MCAsmParser &Parser = getParser();
4569   Parser.Lex();
4570   // If this is not the end of the statement, report an error.
4571   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4572     reportParseError("unexpected token, expected end of statement");
4573     return false;
4574   }
4575   if (AssemblerOptions.back()->isReorder()) {
4576     reportParseError("`noreorder' must be set before `nomacro'");
4577     return false;
4578   }
4579   AssemblerOptions.back()->setNoMacro();
4580   getTargetStreamer().emitDirectiveSetNoMacro();
4581   Parser.Lex(); // Consume the EndOfStatement.
4582   return false;
4583 }
4584
4585 bool MipsAsmParser::parseSetMsaDirective() {
4586   MCAsmParser &Parser = getParser();
4587   Parser.Lex();
4588
4589   // If this is not the end of the statement, report an error.
4590   if (getLexer().isNot(AsmToken::EndOfStatement))
4591     return reportParseError("unexpected token, expected end of statement");
4592
4593   setFeatureBits(Mips::FeatureMSA, "msa");
4594   getTargetStreamer().emitDirectiveSetMsa();
4595   return false;
4596 }
4597
4598 bool MipsAsmParser::parseSetNoMsaDirective() {
4599   MCAsmParser &Parser = getParser();
4600   Parser.Lex();
4601
4602   // If this is not the end of the statement, report an error.
4603   if (getLexer().isNot(AsmToken::EndOfStatement))
4604     return reportParseError("unexpected token, expected end of statement");
4605
4606   clearFeatureBits(Mips::FeatureMSA, "msa");
4607   getTargetStreamer().emitDirectiveSetNoMsa();
4608   return false;
4609 }
4610
4611 bool MipsAsmParser::parseSetNoDspDirective() {
4612   MCAsmParser &Parser = getParser();
4613   Parser.Lex(); // Eat "nodsp".
4614
4615   // If this is not the end of the statement, report an error.
4616   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4617     reportParseError("unexpected token, expected end of statement");
4618     return false;
4619   }
4620
4621   clearFeatureBits(Mips::FeatureDSP, "dsp");
4622   getTargetStreamer().emitDirectiveSetNoDsp();
4623   return false;
4624 }
4625
4626 bool MipsAsmParser::parseSetMips16Directive() {
4627   MCAsmParser &Parser = getParser();
4628   Parser.Lex(); // Eat "mips16".
4629
4630   // If this is not the end of the statement, report an error.
4631   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4632     reportParseError("unexpected token, expected end of statement");
4633     return false;
4634   }
4635
4636   setFeatureBits(Mips::FeatureMips16, "mips16");
4637   getTargetStreamer().emitDirectiveSetMips16();
4638   Parser.Lex(); // Consume the EndOfStatement.
4639   return false;
4640 }
4641
4642 bool MipsAsmParser::parseSetNoMips16Directive() {
4643   MCAsmParser &Parser = getParser();
4644   Parser.Lex(); // Eat "nomips16".
4645
4646   // If this is not the end of the statement, report an error.
4647   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4648     reportParseError("unexpected token, expected end of statement");
4649     return false;
4650   }
4651
4652   clearFeatureBits(Mips::FeatureMips16, "mips16");
4653   getTargetStreamer().emitDirectiveSetNoMips16();
4654   Parser.Lex(); // Consume the EndOfStatement.
4655   return false;
4656 }
4657
4658 bool MipsAsmParser::parseSetFpDirective() {
4659   MCAsmParser &Parser = getParser();
4660   MipsABIFlagsSection::FpABIKind FpAbiVal;
4661   // Line can be: .set fp=32
4662   //              .set fp=xx
4663   //              .set fp=64
4664   Parser.Lex(); // Eat fp token
4665   AsmToken Tok = Parser.getTok();
4666   if (Tok.isNot(AsmToken::Equal)) {
4667     reportParseError("unexpected token, expected equals sign '='");
4668     return false;
4669   }
4670   Parser.Lex(); // Eat '=' token.
4671   Tok = Parser.getTok();
4672
4673   if (!parseFpABIValue(FpAbiVal, ".set"))
4674     return false;
4675
4676   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4677     reportParseError("unexpected token, expected end of statement");
4678     return false;
4679   }
4680   getTargetStreamer().emitDirectiveSetFp(FpAbiVal);
4681   Parser.Lex(); // Consume the EndOfStatement.
4682   return false;
4683 }
4684
4685 bool MipsAsmParser::parseSetOddSPRegDirective() {
4686   MCAsmParser &Parser = getParser();
4687
4688   Parser.Lex(); // Eat "oddspreg".
4689   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4690     reportParseError("unexpected token, expected end of statement");
4691     return false;
4692   }
4693
4694   clearFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
4695   getTargetStreamer().emitDirectiveSetOddSPReg();
4696   return false;
4697 }
4698
4699 bool MipsAsmParser::parseSetNoOddSPRegDirective() {
4700   MCAsmParser &Parser = getParser();
4701
4702   Parser.Lex(); // Eat "nooddspreg".
4703   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4704     reportParseError("unexpected token, expected end of statement");
4705     return false;
4706   }
4707
4708   setFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
4709   getTargetStreamer().emitDirectiveSetNoOddSPReg();
4710   return false;
4711 }
4712
4713 bool MipsAsmParser::parseSetPopDirective() {
4714   MCAsmParser &Parser = getParser();
4715   SMLoc Loc = getLexer().getLoc();
4716
4717   Parser.Lex();
4718   if (getLexer().isNot(AsmToken::EndOfStatement))
4719     return reportParseError("unexpected token, expected end of statement");
4720
4721   // Always keep an element on the options "stack" to prevent the user
4722   // from changing the initial options. This is how we remember them.
4723   if (AssemblerOptions.size() == 2)
4724     return reportParseError(Loc, ".set pop with no .set push");
4725
4726   AssemblerOptions.pop_back();
4727   setAvailableFeatures(
4728       ComputeAvailableFeatures(AssemblerOptions.back()->getFeatures()));
4729   STI.setFeatureBits(AssemblerOptions.back()->getFeatures());
4730
4731   getTargetStreamer().emitDirectiveSetPop();
4732   return false;
4733 }
4734
4735 bool MipsAsmParser::parseSetPushDirective() {
4736   MCAsmParser &Parser = getParser();
4737   Parser.Lex();
4738   if (getLexer().isNot(AsmToken::EndOfStatement))
4739     return reportParseError("unexpected token, expected end of statement");
4740
4741   // Create a copy of the current assembler options environment and push it.
4742   AssemblerOptions.push_back(
4743               make_unique<MipsAssemblerOptions>(AssemblerOptions.back().get()));
4744
4745   getTargetStreamer().emitDirectiveSetPush();
4746   return false;
4747 }
4748
4749 bool MipsAsmParser::parseSetSoftFloatDirective() {
4750   MCAsmParser &Parser = getParser();
4751   Parser.Lex();
4752   if (getLexer().isNot(AsmToken::EndOfStatement))
4753     return reportParseError("unexpected token, expected end of statement");
4754
4755   setFeatureBits(Mips::FeatureSoftFloat, "soft-float");
4756   getTargetStreamer().emitDirectiveSetSoftFloat();
4757   return false;
4758 }
4759
4760 bool MipsAsmParser::parseSetHardFloatDirective() {
4761   MCAsmParser &Parser = getParser();
4762   Parser.Lex();
4763   if (getLexer().isNot(AsmToken::EndOfStatement))
4764     return reportParseError("unexpected token, expected end of statement");
4765
4766   clearFeatureBits(Mips::FeatureSoftFloat, "soft-float");
4767   getTargetStreamer().emitDirectiveSetHardFloat();
4768   return false;
4769 }
4770
4771 bool MipsAsmParser::parseSetAssignment() {
4772   StringRef Name;
4773   const MCExpr *Value;
4774   MCAsmParser &Parser = getParser();
4775
4776   if (Parser.parseIdentifier(Name))
4777     reportParseError("expected identifier after .set");
4778
4779   if (getLexer().isNot(AsmToken::Comma))
4780     return reportParseError("unexpected token, expected comma");
4781   Lex(); // Eat comma
4782
4783   if (Parser.parseExpression(Value))
4784     return reportParseError("expected valid expression after comma");
4785
4786   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
4787   Sym->setVariableValue(Value);
4788
4789   return false;
4790 }
4791
4792 bool MipsAsmParser::parseSetMips0Directive() {
4793   MCAsmParser &Parser = getParser();
4794   Parser.Lex();
4795   if (getLexer().isNot(AsmToken::EndOfStatement))
4796     return reportParseError("unexpected token, expected end of statement");
4797
4798   // Reset assembler options to their initial values.
4799   setAvailableFeatures(
4800       ComputeAvailableFeatures(AssemblerOptions.front()->getFeatures()));
4801   STI.setFeatureBits(AssemblerOptions.front()->getFeatures());
4802   AssemblerOptions.back()->setFeatures(AssemblerOptions.front()->getFeatures());
4803
4804   getTargetStreamer().emitDirectiveSetMips0();
4805   return false;
4806 }
4807
4808 bool MipsAsmParser::parseSetArchDirective() {
4809   MCAsmParser &Parser = getParser();
4810   Parser.Lex();
4811   if (getLexer().isNot(AsmToken::Equal))
4812     return reportParseError("unexpected token, expected equals sign");
4813
4814   Parser.Lex();
4815   StringRef Arch;
4816   if (Parser.parseIdentifier(Arch))
4817     return reportParseError("expected arch identifier");
4818
4819   StringRef ArchFeatureName =
4820       StringSwitch<StringRef>(Arch)
4821           .Case("mips1", "mips1")
4822           .Case("mips2", "mips2")
4823           .Case("mips3", "mips3")
4824           .Case("mips4", "mips4")
4825           .Case("mips5", "mips5")
4826           .Case("mips32", "mips32")
4827           .Case("mips32r2", "mips32r2")
4828           .Case("mips32r3", "mips32r3")
4829           .Case("mips32r5", "mips32r5")
4830           .Case("mips32r6", "mips32r6")
4831           .Case("mips64", "mips64")
4832           .Case("mips64r2", "mips64r2")
4833           .Case("mips64r3", "mips64r3")
4834           .Case("mips64r5", "mips64r5")
4835           .Case("mips64r6", "mips64r6")
4836           .Case("cnmips", "cnmips")
4837           .Case("r4000", "mips3") // This is an implementation of Mips3.
4838           .Default("");
4839
4840   if (ArchFeatureName.empty())
4841     return reportParseError("unsupported architecture");
4842
4843   selectArch(ArchFeatureName);
4844   getTargetStreamer().emitDirectiveSetArch(Arch);
4845   return false;
4846 }
4847
4848 bool MipsAsmParser::parseSetFeature(uint64_t Feature) {
4849   MCAsmParser &Parser = getParser();
4850   Parser.Lex();
4851   if (getLexer().isNot(AsmToken::EndOfStatement))
4852     return reportParseError("unexpected token, expected end of statement");
4853
4854   switch (Feature) {
4855   default:
4856     llvm_unreachable("Unimplemented feature");
4857   case Mips::FeatureDSP:
4858     setFeatureBits(Mips::FeatureDSP, "dsp");
4859     getTargetStreamer().emitDirectiveSetDsp();
4860     break;
4861   case Mips::FeatureMicroMips:
4862     getTargetStreamer().emitDirectiveSetMicroMips();
4863     break;
4864   case Mips::FeatureMips1:
4865     selectArch("mips1");
4866     getTargetStreamer().emitDirectiveSetMips1();
4867     break;
4868   case Mips::FeatureMips2:
4869     selectArch("mips2");
4870     getTargetStreamer().emitDirectiveSetMips2();
4871     break;
4872   case Mips::FeatureMips3:
4873     selectArch("mips3");
4874     getTargetStreamer().emitDirectiveSetMips3();
4875     break;
4876   case Mips::FeatureMips4:
4877     selectArch("mips4");
4878     getTargetStreamer().emitDirectiveSetMips4();
4879     break;
4880   case Mips::FeatureMips5:
4881     selectArch("mips5");
4882     getTargetStreamer().emitDirectiveSetMips5();
4883     break;
4884   case Mips::FeatureMips32:
4885     selectArch("mips32");
4886     getTargetStreamer().emitDirectiveSetMips32();
4887     break;
4888   case Mips::FeatureMips32r2:
4889     selectArch("mips32r2");
4890     getTargetStreamer().emitDirectiveSetMips32R2();
4891     break;
4892   case Mips::FeatureMips32r3:
4893     selectArch("mips32r3");
4894     getTargetStreamer().emitDirectiveSetMips32R3();
4895     break;
4896   case Mips::FeatureMips32r5:
4897     selectArch("mips32r5");
4898     getTargetStreamer().emitDirectiveSetMips32R5();
4899     break;
4900   case Mips::FeatureMips32r6:
4901     selectArch("mips32r6");
4902     getTargetStreamer().emitDirectiveSetMips32R6();
4903     break;
4904   case Mips::FeatureMips64:
4905     selectArch("mips64");
4906     getTargetStreamer().emitDirectiveSetMips64();
4907     break;
4908   case Mips::FeatureMips64r2:
4909     selectArch("mips64r2");
4910     getTargetStreamer().emitDirectiveSetMips64R2();
4911     break;
4912   case Mips::FeatureMips64r3:
4913     selectArch("mips64r3");
4914     getTargetStreamer().emitDirectiveSetMips64R3();
4915     break;
4916   case Mips::FeatureMips64r5:
4917     selectArch("mips64r5");
4918     getTargetStreamer().emitDirectiveSetMips64R5();
4919     break;
4920   case Mips::FeatureMips64r6:
4921     selectArch("mips64r6");
4922     getTargetStreamer().emitDirectiveSetMips64R6();
4923     break;
4924   }
4925   return false;
4926 }
4927
4928 bool MipsAsmParser::eatComma(StringRef ErrorStr) {
4929   MCAsmParser &Parser = getParser();
4930   if (getLexer().isNot(AsmToken::Comma)) {
4931     SMLoc Loc = getLexer().getLoc();
4932     Parser.eatToEndOfStatement();
4933     return Error(Loc, ErrorStr);
4934   }
4935
4936   Parser.Lex(); // Eat the comma.
4937   return true;
4938 }
4939
4940 // Used to determine if .cpload, .cprestore, and .cpsetup have any effect.
4941 // In this class, it is only used for .cprestore.
4942 // FIXME: Only keep track of IsPicEnabled in one place, instead of in both
4943 // MipsTargetELFStreamer and MipsAsmParser.
4944 bool MipsAsmParser::isPicAndNotNxxAbi() {
4945   return inPicMode() && !(isABI_N32() || isABI_N64());
4946 }
4947
4948 bool MipsAsmParser::parseDirectiveCpLoad(SMLoc Loc) {
4949   if (AssemblerOptions.back()->isReorder())
4950     Warning(Loc, ".cpload should be inside a noreorder section");
4951
4952   if (inMips16Mode()) {
4953     reportParseError(".cpload is not supported in Mips16 mode");
4954     return false;
4955   }
4956
4957   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Reg;
4958   OperandMatchResultTy ResTy = parseAnyRegister(Reg);
4959   if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
4960     reportParseError("expected register containing function address");
4961     return false;
4962   }
4963
4964   MipsOperand &RegOpnd = static_cast<MipsOperand &>(*Reg[0]);
4965   if (!RegOpnd.isGPRAsmReg()) {
4966     reportParseError(RegOpnd.getStartLoc(), "invalid register");
4967     return false;
4968   }
4969
4970   // If this is not the end of the statement, report an error.
4971   if (getLexer().isNot(AsmToken::EndOfStatement)) {
4972     reportParseError("unexpected token, expected end of statement");
4973     return false;
4974   }
4975
4976   getTargetStreamer().emitDirectiveCpLoad(RegOpnd.getGPR32Reg());
4977   return false;
4978 }
4979
4980 bool MipsAsmParser::parseDirectiveCpRestore(SMLoc Loc) {
4981   MCAsmParser &Parser = getParser();
4982
4983   // Note that .cprestore is ignored if used with the N32 and N64 ABIs or if it
4984   // is used in non-PIC mode.
4985
4986   if (inMips16Mode()) {
4987     reportParseError(".cprestore is not supported in Mips16 mode");
4988     return false;
4989   }
4990
4991   // Get the stack offset value.
4992   const MCExpr *StackOffset;
4993   int64_t StackOffsetVal;
4994   if (Parser.parseExpression(StackOffset)) {
4995     reportParseError("expected stack offset value");
4996     return false;
4997   }
4998
4999   if (!StackOffset->evaluateAsAbsolute(StackOffsetVal)) {
5000     reportParseError("stack offset is not an absolute expression");
5001     return false;
5002   }
5003
5004   if (StackOffsetVal < 0) {
5005     Warning(Loc, ".cprestore with negative stack offset has no effect");
5006     IsCpRestoreSet = false;
5007   } else {
5008     IsCpRestoreSet = true;
5009     CpRestoreOffset = StackOffsetVal;
5010   }
5011
5012   // If this is not the end of the statement, report an error.
5013   if (getLexer().isNot(AsmToken::EndOfStatement)) {
5014     reportParseError("unexpected token, expected end of statement");
5015     return false;
5016   }
5017
5018   // Store the $gp on the stack.
5019   SmallVector<MCInst, 3> StoreInsts;
5020   createCpRestoreMemOp(false /*IsLoad*/, CpRestoreOffset /*StackOffset*/, Loc,
5021                        StoreInsts);
5022
5023   getTargetStreamer().emitDirectiveCpRestore(StoreInsts, CpRestoreOffset);
5024   Parser.Lex(); // Consume the EndOfStatement.
5025   return false;
5026 }
5027
5028 bool MipsAsmParser::parseDirectiveCPSetup() {
5029   MCAsmParser &Parser = getParser();
5030   unsigned FuncReg;
5031   unsigned Save;
5032   bool SaveIsReg = true;
5033
5034   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> TmpReg;
5035   OperandMatchResultTy ResTy = parseAnyRegister(TmpReg);
5036   if (ResTy == MatchOperand_NoMatch) {
5037     reportParseError("expected register containing function address");
5038     Parser.eatToEndOfStatement();
5039     return false;
5040   }
5041
5042   MipsOperand &FuncRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
5043   if (!FuncRegOpnd.isGPRAsmReg()) {
5044     reportParseError(FuncRegOpnd.getStartLoc(), "invalid register");
5045     Parser.eatToEndOfStatement();
5046     return false;
5047   }
5048
5049   FuncReg = FuncRegOpnd.getGPR32Reg();
5050   TmpReg.clear();
5051
5052   if (!eatComma("unexpected token, expected comma"))
5053     return true;
5054
5055   ResTy = parseAnyRegister(TmpReg);
5056   if (ResTy == MatchOperand_NoMatch) {
5057     const MCExpr *OffsetExpr;
5058     int64_t OffsetVal;
5059     SMLoc ExprLoc = getLexer().getLoc();
5060
5061     if (Parser.parseExpression(OffsetExpr) ||
5062         !OffsetExpr->evaluateAsAbsolute(OffsetVal)) {
5063       reportParseError(ExprLoc, "expected save register or stack offset");
5064       Parser.eatToEndOfStatement();
5065       return false;
5066     }
5067
5068     Save = OffsetVal;
5069     SaveIsReg = false;
5070   } else {
5071     MipsOperand &SaveOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
5072     if (!SaveOpnd.isGPRAsmReg()) {
5073       reportParseError(SaveOpnd.getStartLoc(), "invalid register");
5074       Parser.eatToEndOfStatement();
5075       return false;
5076     }
5077     Save = SaveOpnd.getGPR32Reg();
5078   }
5079
5080   if (!eatComma("unexpected token, expected comma"))
5081     return true;
5082
5083   const MCExpr *Expr;
5084   if (Parser.parseExpression(Expr)) {
5085     reportParseError("expected expression");
5086     return false;
5087   }
5088
5089   if (Expr->getKind() != MCExpr::SymbolRef) {
5090     reportParseError("expected symbol");
5091     return false;
5092   }
5093   const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
5094
5095   CpSaveLocation = Save;
5096   CpSaveLocationIsRegister = SaveIsReg;
5097
5098   getTargetStreamer().emitDirectiveCpsetup(FuncReg, Save, Ref->getSymbol(),
5099                                            SaveIsReg);
5100   return false;
5101 }
5102
5103 bool MipsAsmParser::parseDirectiveCPReturn() {
5104   getTargetStreamer().emitDirectiveCpreturn(CpSaveLocation,
5105                                             CpSaveLocationIsRegister);
5106   return false;
5107 }
5108
5109 bool MipsAsmParser::parseDirectiveNaN() {
5110   MCAsmParser &Parser = getParser();
5111   if (getLexer().isNot(AsmToken::EndOfStatement)) {
5112     const AsmToken &Tok = Parser.getTok();
5113
5114     if (Tok.getString() == "2008") {
5115       Parser.Lex();
5116       getTargetStreamer().emitDirectiveNaN2008();
5117       return false;
5118     } else if (Tok.getString() == "legacy") {
5119       Parser.Lex();
5120       getTargetStreamer().emitDirectiveNaNLegacy();
5121       return false;
5122     }
5123   }
5124   // If we don't recognize the option passed to the .nan
5125   // directive (e.g. no option or unknown option), emit an error.
5126   reportParseError("invalid option in .nan directive");
5127   return false;
5128 }
5129
5130 bool MipsAsmParser::parseDirectiveSet() {
5131   MCAsmParser &Parser = getParser();
5132   // Get the next token.
5133   const AsmToken &Tok = Parser.getTok();
5134
5135   if (Tok.getString() == "noat") {
5136     return parseSetNoAtDirective();
5137   } else if (Tok.getString() == "at") {
5138     return parseSetAtDirective();
5139   } else if (Tok.getString() == "arch") {
5140     return parseSetArchDirective();
5141   } else if (Tok.getString() == "fp") {
5142     return parseSetFpDirective();
5143   } else if (Tok.getString() == "oddspreg") {
5144     return parseSetOddSPRegDirective();
5145   } else if (Tok.getString() == "nooddspreg") {
5146     return parseSetNoOddSPRegDirective();
5147   } else if (Tok.getString() == "pop") {
5148     return parseSetPopDirective();
5149   } else if (Tok.getString() == "push") {
5150     return parseSetPushDirective();
5151   } else if (Tok.getString() == "reorder") {
5152     return parseSetReorderDirective();
5153   } else if (Tok.getString() == "noreorder") {
5154     return parseSetNoReorderDirective();
5155   } else if (Tok.getString() == "macro") {
5156     return parseSetMacroDirective();
5157   } else if (Tok.getString() == "nomacro") {
5158     return parseSetNoMacroDirective();
5159   } else if (Tok.getString() == "mips16") {
5160     return parseSetMips16Directive();
5161   } else if (Tok.getString() == "nomips16") {
5162     return parseSetNoMips16Directive();
5163   } else if (Tok.getString() == "nomicromips") {
5164     getTargetStreamer().emitDirectiveSetNoMicroMips();
5165     Parser.eatToEndOfStatement();
5166     return false;
5167   } else if (Tok.getString() == "micromips") {
5168     return parseSetFeature(Mips::FeatureMicroMips);
5169   } else if (Tok.getString() == "mips0") {
5170     return parseSetMips0Directive();
5171   } else if (Tok.getString() == "mips1") {
5172     return parseSetFeature(Mips::FeatureMips1);
5173   } else if (Tok.getString() == "mips2") {
5174     return parseSetFeature(Mips::FeatureMips2);
5175   } else if (Tok.getString() == "mips3") {
5176     return parseSetFeature(Mips::FeatureMips3);
5177   } else if (Tok.getString() == "mips4") {
5178     return parseSetFeature(Mips::FeatureMips4);
5179   } else if (Tok.getString() == "mips5") {
5180     return parseSetFeature(Mips::FeatureMips5);
5181   } else if (Tok.getString() == "mips32") {
5182     return parseSetFeature(Mips::FeatureMips32);
5183   } else if (Tok.getString() == "mips32r2") {
5184     return parseSetFeature(Mips::FeatureMips32r2);
5185   } else if (Tok.getString() == "mips32r3") {
5186     return parseSetFeature(Mips::FeatureMips32r3);
5187   } else if (Tok.getString() == "mips32r5") {
5188     return parseSetFeature(Mips::FeatureMips32r5);
5189   } else if (Tok.getString() == "mips32r6") {
5190     return parseSetFeature(Mips::FeatureMips32r6);
5191   } else if (Tok.getString() == "mips64") {
5192     return parseSetFeature(Mips::FeatureMips64);
5193   } else if (Tok.getString() == "mips64r2") {
5194     return parseSetFeature(Mips::FeatureMips64r2);
5195   } else if (Tok.getString() == "mips64r3") {
5196     return parseSetFeature(Mips::FeatureMips64r3);
5197   } else if (Tok.getString() == "mips64r5") {
5198     return parseSetFeature(Mips::FeatureMips64r5);
5199   } else if (Tok.getString() == "mips64r6") {
5200     return parseSetFeature(Mips::FeatureMips64r6);
5201   } else if (Tok.getString() == "dsp") {
5202     return parseSetFeature(Mips::FeatureDSP);
5203   } else if (Tok.getString() == "nodsp") {
5204     return parseSetNoDspDirective();
5205   } else if (Tok.getString() == "msa") {
5206     return parseSetMsaDirective();
5207   } else if (Tok.getString() == "nomsa") {
5208     return parseSetNoMsaDirective();
5209   } else if (Tok.getString() == "softfloat") {
5210     return parseSetSoftFloatDirective();
5211   } else if (Tok.getString() == "hardfloat") {
5212     return parseSetHardFloatDirective();
5213   } else {
5214     // It is just an identifier, look for an assignment.
5215     parseSetAssignment();
5216     return false;
5217   }
5218
5219   return true;
5220 }
5221
5222 /// parseDataDirective
5223 ///  ::= .word [ expression (, expression)* ]
5224 bool MipsAsmParser::parseDataDirective(unsigned Size, SMLoc L) {
5225   MCAsmParser &Parser = getParser();
5226   if (getLexer().isNot(AsmToken::EndOfStatement)) {
5227     for (;;) {
5228       const MCExpr *Value;
5229       if (getParser().parseExpression(Value))
5230         return true;
5231
5232       getParser().getStreamer().EmitValue(Value, Size);
5233
5234       if (getLexer().is(AsmToken::EndOfStatement))
5235         break;
5236
5237       if (getLexer().isNot(AsmToken::Comma))
5238         return Error(L, "unexpected token, expected comma");
5239       Parser.Lex();
5240     }
5241   }
5242
5243   Parser.Lex();
5244   return false;
5245 }
5246
5247 /// parseDirectiveGpWord
5248 ///  ::= .gpword local_sym
5249 bool MipsAsmParser::parseDirectiveGpWord() {
5250   MCAsmParser &Parser = getParser();
5251   const MCExpr *Value;
5252   // EmitGPRel32Value requires an expression, so we are using base class
5253   // method to evaluate the expression.
5254   if (getParser().parseExpression(Value))
5255     return true;
5256   getParser().getStreamer().EmitGPRel32Value(Value);
5257
5258   if (getLexer().isNot(AsmToken::EndOfStatement))
5259     return Error(getLexer().getLoc(), 
5260                 "unexpected token, expected end of statement");
5261   Parser.Lex(); // Eat EndOfStatement token.
5262   return false;
5263 }
5264
5265 /// parseDirectiveGpDWord
5266 ///  ::= .gpdword local_sym
5267 bool MipsAsmParser::parseDirectiveGpDWord() {
5268   MCAsmParser &Parser = getParser();
5269   const MCExpr *Value;
5270   // EmitGPRel64Value requires an expression, so we are using base class
5271   // method to evaluate the expression.
5272   if (getParser().parseExpression(Value))
5273     return true;
5274   getParser().getStreamer().EmitGPRel64Value(Value);
5275
5276   if (getLexer().isNot(AsmToken::EndOfStatement))
5277     return Error(getLexer().getLoc(), 
5278                 "unexpected token, expected end of statement");
5279   Parser.Lex(); // Eat EndOfStatement token.
5280   return false;
5281 }
5282
5283 bool MipsAsmParser::parseDirectiveOption() {
5284   MCAsmParser &Parser = getParser();
5285   // Get the option token.
5286   AsmToken Tok = Parser.getTok();
5287   // At the moment only identifiers are supported.
5288   if (Tok.isNot(AsmToken::Identifier)) {
5289     Error(Parser.getTok().getLoc(), "unexpected token, expected identifier");
5290     Parser.eatToEndOfStatement();
5291     return false;
5292   }
5293
5294   StringRef Option = Tok.getIdentifier();
5295
5296   if (Option == "pic0") {
5297     // MipsAsmParser needs to know if the current PIC mode changes.
5298     IsPicEnabled = false;
5299
5300     getTargetStreamer().emitDirectiveOptionPic0();
5301     Parser.Lex();
5302     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
5303       Error(Parser.getTok().getLoc(),
5304             "unexpected token, expected end of statement");
5305       Parser.eatToEndOfStatement();
5306     }
5307     return false;
5308   }
5309
5310   if (Option == "pic2") {
5311     // MipsAsmParser needs to know if the current PIC mode changes.
5312     IsPicEnabled = true;
5313
5314     getTargetStreamer().emitDirectiveOptionPic2();
5315     Parser.Lex();
5316     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
5317       Error(Parser.getTok().getLoc(),
5318             "unexpected token, expected end of statement");
5319       Parser.eatToEndOfStatement();
5320     }
5321     return false;
5322   }
5323
5324   // Unknown option.
5325   Warning(Parser.getTok().getLoc(), 
5326           "unknown option, expected 'pic0' or 'pic2'");
5327   Parser.eatToEndOfStatement();
5328   return false;
5329 }
5330
5331 /// parseInsnDirective
5332 ///  ::= .insn
5333 bool MipsAsmParser::parseInsnDirective() {
5334   // If this is not the end of the statement, report an error.
5335   if (getLexer().isNot(AsmToken::EndOfStatement)) {
5336     reportParseError("unexpected token, expected end of statement");
5337     return false;
5338   }
5339
5340   // The actual label marking happens in
5341   // MipsELFStreamer::createPendingLabelRelocs().
5342   getTargetStreamer().emitDirectiveInsn();
5343
5344   getParser().Lex(); // Eat EndOfStatement token.
5345   return false;
5346 }
5347
5348 /// parseDirectiveModule
5349 ///  ::= .module oddspreg
5350 ///  ::= .module nooddspreg
5351 ///  ::= .module fp=value
5352 ///  ::= .module softfloat
5353 ///  ::= .module hardfloat
5354 bool MipsAsmParser::parseDirectiveModule() {
5355   MCAsmParser &Parser = getParser();
5356   MCAsmLexer &Lexer = getLexer();
5357   SMLoc L = Lexer.getLoc();
5358
5359   if (!getTargetStreamer().isModuleDirectiveAllowed()) {
5360     // TODO : get a better message.
5361     reportParseError(".module directive must appear before any code");
5362     return false;
5363   }
5364
5365   StringRef Option;
5366   if (Parser.parseIdentifier(Option)) {
5367     reportParseError("expected .module option identifier");
5368     return false;
5369   }
5370
5371   if (Option == "oddspreg") {
5372     clearModuleFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
5373
5374     // Synchronize the abiflags information with the FeatureBits information we
5375     // changed above.
5376     getTargetStreamer().updateABIInfo(*this);
5377
5378     // If printing assembly, use the recently updated abiflags information.
5379     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
5380     // emitted at the end).
5381     getTargetStreamer().emitDirectiveModuleOddSPReg();
5382
5383     // If this is not the end of the statement, report an error.
5384     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5385       reportParseError("unexpected token, expected end of statement");
5386       return false;
5387     }
5388
5389     return false; // parseDirectiveModule has finished successfully.
5390   } else if (Option == "nooddspreg") {
5391     if (!isABI_O32()) {
5392       Error(L, "'.module nooddspreg' requires the O32 ABI");
5393       return false;
5394     }
5395
5396     setModuleFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
5397
5398     // Synchronize the abiflags information with the FeatureBits information we
5399     // changed above.
5400     getTargetStreamer().updateABIInfo(*this);
5401
5402     // If printing assembly, use the recently updated abiflags information.
5403     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
5404     // emitted at the end).
5405     getTargetStreamer().emitDirectiveModuleOddSPReg();
5406
5407     // If this is not the end of the statement, report an error.
5408     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5409       reportParseError("unexpected token, expected end of statement");
5410       return false;
5411     }
5412
5413     return false; // parseDirectiveModule has finished successfully.
5414   } else if (Option == "fp") {
5415     return parseDirectiveModuleFP();
5416   } else if (Option == "softfloat") {
5417     setModuleFeatureBits(Mips::FeatureSoftFloat, "soft-float");
5418
5419     // Synchronize the ABI Flags information with the FeatureBits information we
5420     // updated above.
5421     getTargetStreamer().updateABIInfo(*this);
5422
5423     // If printing assembly, use the recently updated ABI Flags information.
5424     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
5425     // emitted later).
5426     getTargetStreamer().emitDirectiveModuleSoftFloat();
5427
5428     // If this is not the end of the statement, report an error.
5429     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5430       reportParseError("unexpected token, expected end of statement");
5431       return false;
5432     }
5433
5434     return false; // parseDirectiveModule has finished successfully.
5435   } else if (Option == "hardfloat") {
5436     clearModuleFeatureBits(Mips::FeatureSoftFloat, "soft-float");
5437
5438     // Synchronize the ABI Flags information with the FeatureBits information we
5439     // updated above.
5440     getTargetStreamer().updateABIInfo(*this);
5441
5442     // If printing assembly, use the recently updated ABI Flags information.
5443     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
5444     // emitted later).
5445     getTargetStreamer().emitDirectiveModuleHardFloat();
5446
5447     // If this is not the end of the statement, report an error.
5448     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5449       reportParseError("unexpected token, expected end of statement");
5450       return false;
5451     }
5452
5453     return false; // parseDirectiveModule has finished successfully.
5454   } else {
5455     return Error(L, "'" + Twine(Option) + "' is not a valid .module option.");
5456   }
5457 }
5458
5459 /// parseDirectiveModuleFP
5460 ///  ::= =32
5461 ///  ::= =xx
5462 ///  ::= =64
5463 bool MipsAsmParser::parseDirectiveModuleFP() {
5464   MCAsmParser &Parser = getParser();
5465   MCAsmLexer &Lexer = getLexer();
5466
5467   if (Lexer.isNot(AsmToken::Equal)) {
5468     reportParseError("unexpected token, expected equals sign '='");
5469     return false;
5470   }
5471   Parser.Lex(); // Eat '=' token.
5472
5473   MipsABIFlagsSection::FpABIKind FpABI;
5474   if (!parseFpABIValue(FpABI, ".module"))
5475     return false;
5476
5477   if (getLexer().isNot(AsmToken::EndOfStatement)) {
5478     reportParseError("unexpected token, expected end of statement");
5479     return false;
5480   }
5481
5482   // Synchronize the abiflags information with the FeatureBits information we
5483   // changed above.
5484   getTargetStreamer().updateABIInfo(*this);
5485
5486   // If printing assembly, use the recently updated abiflags information.
5487   // If generating ELF, don't do anything (the .MIPS.abiflags section gets
5488   // emitted at the end).
5489   getTargetStreamer().emitDirectiveModuleFP();
5490
5491   Parser.Lex(); // Consume the EndOfStatement.
5492   return false;
5493 }
5494
5495 bool MipsAsmParser::parseFpABIValue(MipsABIFlagsSection::FpABIKind &FpABI,
5496                                     StringRef Directive) {
5497   MCAsmParser &Parser = getParser();
5498   MCAsmLexer &Lexer = getLexer();
5499   bool ModuleLevelOptions = Directive == ".module";
5500
5501   if (Lexer.is(AsmToken::Identifier)) {
5502     StringRef Value = Parser.getTok().getString();
5503     Parser.Lex();
5504
5505     if (Value != "xx") {
5506       reportParseError("unsupported value, expected 'xx', '32' or '64'");
5507       return false;
5508     }
5509
5510     if (!isABI_O32()) {
5511       reportParseError("'" + Directive + " fp=xx' requires the O32 ABI");
5512       return false;
5513     }
5514
5515     FpABI = MipsABIFlagsSection::FpABIKind::XX;
5516     if (ModuleLevelOptions) {
5517       setModuleFeatureBits(Mips::FeatureFPXX, "fpxx");
5518       clearModuleFeatureBits(Mips::FeatureFP64Bit, "fp64");
5519     } else {
5520       setFeatureBits(Mips::FeatureFPXX, "fpxx");
5521       clearFeatureBits(Mips::FeatureFP64Bit, "fp64");
5522     }
5523     return true;
5524   }
5525
5526   if (Lexer.is(AsmToken::Integer)) {
5527     unsigned Value = Parser.getTok().getIntVal();
5528     Parser.Lex();
5529
5530     if (Value != 32 && Value != 64) {
5531       reportParseError("unsupported value, expected 'xx', '32' or '64'");
5532       return false;
5533     }
5534
5535     if (Value == 32) {
5536       if (!isABI_O32()) {
5537         reportParseError("'" + Directive + " fp=32' requires the O32 ABI");
5538         return false;
5539       }
5540
5541       FpABI = MipsABIFlagsSection::FpABIKind::S32;
5542       if (ModuleLevelOptions) {
5543         clearModuleFeatureBits(Mips::FeatureFPXX, "fpxx");
5544         clearModuleFeatureBits(Mips::FeatureFP64Bit, "fp64");
5545       } else {
5546         clearFeatureBits(Mips::FeatureFPXX, "fpxx");
5547         clearFeatureBits(Mips::FeatureFP64Bit, "fp64");
5548       }
5549     } else {
5550       FpABI = MipsABIFlagsSection::FpABIKind::S64;
5551       if (ModuleLevelOptions) {
5552         clearModuleFeatureBits(Mips::FeatureFPXX, "fpxx");
5553         setModuleFeatureBits(Mips::FeatureFP64Bit, "fp64");
5554       } else {
5555         clearFeatureBits(Mips::FeatureFPXX, "fpxx");
5556         setFeatureBits(Mips::FeatureFP64Bit, "fp64");
5557       }
5558     }
5559
5560     return true;
5561   }
5562
5563   return false;
5564 }
5565
5566 bool MipsAsmParser::ParseDirective(AsmToken DirectiveID) {
5567   MCAsmParser &Parser = getParser();
5568   StringRef IDVal = DirectiveID.getString();
5569
5570   if (IDVal == ".cpload")
5571     return parseDirectiveCpLoad(DirectiveID.getLoc());
5572   if (IDVal == ".cprestore")
5573     return parseDirectiveCpRestore(DirectiveID.getLoc());
5574   if (IDVal == ".dword") {
5575     parseDataDirective(8, DirectiveID.getLoc());
5576     return false;
5577   }
5578   if (IDVal == ".ent") {
5579     StringRef SymbolName;
5580
5581     if (Parser.parseIdentifier(SymbolName)) {
5582       reportParseError("expected identifier after .ent");
5583       return false;
5584     }
5585
5586     // There's an undocumented extension that allows an integer to
5587     // follow the name of the procedure which AFAICS is ignored by GAS.
5588     // Example: .ent foo,2
5589     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5590       if (getLexer().isNot(AsmToken::Comma)) {
5591         // Even though we accept this undocumented extension for compatibility
5592         // reasons, the additional integer argument does not actually change
5593         // the behaviour of the '.ent' directive, so we would like to discourage
5594         // its use. We do this by not referring to the extended version in
5595         // error messages which are not directly related to its use.
5596         reportParseError("unexpected token, expected end of statement");
5597         return false;
5598       }
5599       Parser.Lex(); // Eat the comma.
5600       const MCExpr *DummyNumber;
5601       int64_t DummyNumberVal;
5602       // If the user was explicitly trying to use the extended version,
5603       // we still give helpful extension-related error messages.
5604       if (Parser.parseExpression(DummyNumber)) {
5605         reportParseError("expected number after comma");
5606         return false;
5607       }
5608       if (!DummyNumber->evaluateAsAbsolute(DummyNumberVal)) {
5609         reportParseError("expected an absolute expression after comma");
5610         return false;
5611       }
5612     }
5613
5614     // If this is not the end of the statement, report an error.
5615     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5616       reportParseError("unexpected token, expected end of statement");
5617       return false;
5618     }
5619
5620     MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
5621
5622     getTargetStreamer().emitDirectiveEnt(*Sym);
5623     CurrentFn = Sym;
5624     IsCpRestoreSet = false;
5625     return false;
5626   }
5627
5628   if (IDVal == ".end") {
5629     StringRef SymbolName;
5630
5631     if (Parser.parseIdentifier(SymbolName)) {
5632       reportParseError("expected identifier after .end");
5633       return false;
5634     }
5635
5636     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5637       reportParseError("unexpected token, expected end of statement");
5638       return false;
5639     }
5640
5641     if (CurrentFn == nullptr) {
5642       reportParseError(".end used without .ent");
5643       return false;
5644     }
5645
5646     if ((SymbolName != CurrentFn->getName())) {
5647       reportParseError(".end symbol does not match .ent symbol");
5648       return false;
5649     }
5650
5651     getTargetStreamer().emitDirectiveEnd(SymbolName);
5652     CurrentFn = nullptr;
5653     IsCpRestoreSet = false;
5654     return false;
5655   }
5656
5657   if (IDVal == ".frame") {
5658     // .frame $stack_reg, frame_size_in_bytes, $return_reg
5659     SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> TmpReg;
5660     OperandMatchResultTy ResTy = parseAnyRegister(TmpReg);
5661     if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
5662       reportParseError("expected stack register");
5663       return false;
5664     }
5665
5666     MipsOperand &StackRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
5667     if (!StackRegOpnd.isGPRAsmReg()) {
5668       reportParseError(StackRegOpnd.getStartLoc(),
5669                        "expected general purpose register");
5670       return false;
5671     }
5672     unsigned StackReg = StackRegOpnd.getGPR32Reg();
5673
5674     if (Parser.getTok().is(AsmToken::Comma))
5675       Parser.Lex();
5676     else {
5677       reportParseError("unexpected token, expected comma");
5678       return false;
5679     }
5680
5681     // Parse the frame size.
5682     const MCExpr *FrameSize;
5683     int64_t FrameSizeVal;
5684
5685     if (Parser.parseExpression(FrameSize)) {
5686       reportParseError("expected frame size value");
5687       return false;
5688     }
5689
5690     if (!FrameSize->evaluateAsAbsolute(FrameSizeVal)) {
5691       reportParseError("frame size not an absolute expression");
5692       return false;
5693     }
5694
5695     if (Parser.getTok().is(AsmToken::Comma))
5696       Parser.Lex();
5697     else {
5698       reportParseError("unexpected token, expected comma");
5699       return false;
5700     }
5701
5702     // Parse the return register.
5703     TmpReg.clear();
5704     ResTy = parseAnyRegister(TmpReg);
5705     if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
5706       reportParseError("expected return register");
5707       return false;
5708     }
5709
5710     MipsOperand &ReturnRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
5711     if (!ReturnRegOpnd.isGPRAsmReg()) {
5712       reportParseError(ReturnRegOpnd.getStartLoc(),
5713                        "expected general purpose register");
5714       return false;
5715     }
5716
5717     // If this is not the end of the statement, report an error.
5718     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5719       reportParseError("unexpected token, expected end of statement");
5720       return false;
5721     }
5722
5723     getTargetStreamer().emitFrame(StackReg, FrameSizeVal,
5724                                   ReturnRegOpnd.getGPR32Reg());
5725     IsCpRestoreSet = false;
5726     return false;
5727   }
5728
5729   if (IDVal == ".set") {
5730     return parseDirectiveSet();
5731   }
5732
5733   if (IDVal == ".mask" || IDVal == ".fmask") {
5734     // .mask bitmask, frame_offset
5735     // bitmask: One bit for each register used.
5736     // frame_offset: Offset from Canonical Frame Address ($sp on entry) where
5737     //               first register is expected to be saved.
5738     // Examples:
5739     //   .mask 0x80000000, -4
5740     //   .fmask 0x80000000, -4
5741     //
5742
5743     // Parse the bitmask
5744     const MCExpr *BitMask;
5745     int64_t BitMaskVal;
5746
5747     if (Parser.parseExpression(BitMask)) {
5748       reportParseError("expected bitmask value");
5749       return false;
5750     }
5751
5752     if (!BitMask->evaluateAsAbsolute(BitMaskVal)) {
5753       reportParseError("bitmask not an absolute expression");
5754       return false;
5755     }
5756
5757     if (Parser.getTok().is(AsmToken::Comma))
5758       Parser.Lex();
5759     else {
5760       reportParseError("unexpected token, expected comma");
5761       return false;
5762     }
5763
5764     // Parse the frame_offset
5765     const MCExpr *FrameOffset;
5766     int64_t FrameOffsetVal;
5767
5768     if (Parser.parseExpression(FrameOffset)) {
5769       reportParseError("expected frame offset value");
5770       return false;
5771     }
5772
5773     if (!FrameOffset->evaluateAsAbsolute(FrameOffsetVal)) {
5774       reportParseError("frame offset not an absolute expression");
5775       return false;
5776     }
5777
5778     // If this is not the end of the statement, report an error.
5779     if (getLexer().isNot(AsmToken::EndOfStatement)) {
5780       reportParseError("unexpected token, expected end of statement");
5781       return false;
5782     }
5783
5784     if (IDVal == ".mask")
5785       getTargetStreamer().emitMask(BitMaskVal, FrameOffsetVal);
5786     else
5787       getTargetStreamer().emitFMask(BitMaskVal, FrameOffsetVal);
5788     return false;
5789   }
5790
5791   if (IDVal == ".nan")
5792     return parseDirectiveNaN();
5793
5794   if (IDVal == ".gpword") {
5795     parseDirectiveGpWord();
5796     return false;
5797   }
5798
5799   if (IDVal == ".gpdword") {
5800     parseDirectiveGpDWord();
5801     return false;
5802   }
5803
5804   if (IDVal == ".word") {
5805     parseDataDirective(4, DirectiveID.getLoc());
5806     return false;
5807   }
5808
5809   if (IDVal == ".option")
5810     return parseDirectiveOption();
5811
5812   if (IDVal == ".abicalls") {
5813     getTargetStreamer().emitDirectiveAbiCalls();
5814     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
5815       Error(Parser.getTok().getLoc(), 
5816             "unexpected token, expected end of statement");
5817       // Clear line
5818       Parser.eatToEndOfStatement();
5819     }
5820     return false;
5821   }
5822
5823   if (IDVal == ".cpsetup")
5824     return parseDirectiveCPSetup();
5825
5826   if (IDVal == ".cpreturn")
5827     return parseDirectiveCPReturn();
5828
5829   if (IDVal == ".module")
5830     return parseDirectiveModule();
5831
5832   if (IDVal == ".llvm_internal_mips_reallow_module_directive")
5833     return parseInternalDirectiveReallowModule();
5834
5835   if (IDVal == ".insn")
5836     return parseInsnDirective();
5837
5838   return true;
5839 }
5840
5841 bool MipsAsmParser::parseInternalDirectiveReallowModule() {
5842   // If this is not the end of the statement, report an error.
5843   if (getLexer().isNot(AsmToken::EndOfStatement)) {
5844     reportParseError("unexpected token, expected end of statement");
5845     return false;
5846   }
5847
5848   getTargetStreamer().reallowModuleDirective();
5849
5850   getParser().Lex(); // Eat EndOfStatement token.
5851   return false;
5852 }
5853
5854 extern "C" void LLVMInitializeMipsAsmParser() {
5855   RegisterMCAsmParser<MipsAsmParser> X(TheMipsTarget);
5856   RegisterMCAsmParser<MipsAsmParser> Y(TheMipselTarget);
5857   RegisterMCAsmParser<MipsAsmParser> A(TheMips64Target);
5858   RegisterMCAsmParser<MipsAsmParser> B(TheMips64elTarget);
5859 }
5860
5861 #define GET_REGISTER_MATCHER
5862 #define GET_MATCHER_IMPLEMENTATION
5863 #include "MipsGenAsmMatcher.inc"