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