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