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