1 //===-- MipsAsmParser.cpp - Parse Mips assembly to MCInst instructions ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "MCTargetDesc/MipsABIFlagsSection.h"
10 #include "MCTargetDesc/MipsABIInfo.h"
11 #include "MCTargetDesc/MipsBaseInfo.h"
12 #include "MCTargetDesc/MipsMCExpr.h"
13 #include "MCTargetDesc/MipsMCTargetDesc.h"
14 #include "MipsTargetStreamer.h"
15 #include "TargetInfo/MipsTargetInfo.h"
16 #include "llvm/ADT/APFloat.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/BinaryFormat/ELF.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCExpr.h"
26 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCInstrDesc.h"
28 #include "llvm/MC/MCObjectFileInfo.h"
29 #include "llvm/MC/MCParser/MCAsmLexer.h"
30 #include "llvm/MC/MCParser/MCAsmParser.h"
31 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
32 #include "llvm/MC/MCParser/MCAsmParserUtils.h"
33 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
34 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
35 #include "llvm/MC/MCSectionELF.h"
36 #include "llvm/MC/MCStreamer.h"
37 #include "llvm/MC/MCSubtargetInfo.h"
38 #include "llvm/MC/MCSymbol.h"
39 #include "llvm/MC/MCSymbolELF.h"
40 #include "llvm/MC/MCValue.h"
41 #include "llvm/MC/SubtargetFeature.h"
42 #include "llvm/Support/Alignment.h"
43 #include "llvm/Support/Casting.h"
44 #include "llvm/Support/CommandLine.h"
45 #include "llvm/Support/Compiler.h"
46 #include "llvm/Support/Debug.h"
47 #include "llvm/Support/ErrorHandling.h"
48 #include "llvm/Support/MathExtras.h"
49 #include "llvm/Support/SMLoc.h"
50 #include "llvm/Support/SourceMgr.h"
51 #include "llvm/Support/TargetRegistry.h"
52 #include "llvm/Support/raw_ostream.h"
53 #include <algorithm>
54 #include <cassert>
55 #include <cstdint>
56 #include <memory>
57 #include <string>
58 #include <utility>
59 
60 using namespace llvm;
61 
62 #define DEBUG_TYPE "mips-asm-parser"
63 
64 namespace llvm {
65 
66 class MCInstrInfo;
67 
68 } // end namespace llvm
69 
70 extern cl::opt<bool> EmitJalrReloc;
71 
72 namespace {
73 
74 class MipsAssemblerOptions {
75 public:
76   MipsAssemblerOptions(const FeatureBitset &Features_) : Features(Features_) {}
77 
78   MipsAssemblerOptions(const MipsAssemblerOptions *Opts) {
79     ATReg = Opts->getATRegIndex();
80     Reorder = Opts->isReorder();
81     Macro = Opts->isMacro();
82     Features = Opts->getFeatures();
83   }
84 
85   unsigned getATRegIndex() const { return ATReg; }
86   bool setATRegIndex(unsigned Reg) {
87     if (Reg > 31)
88       return false;
89 
90     ATReg = Reg;
91     return true;
92   }
93 
94   bool isReorder() const { return Reorder; }
95   void setReorder() { Reorder = true; }
96   void setNoReorder() { Reorder = false; }
97 
98   bool isMacro() const { return Macro; }
99   void setMacro() { Macro = true; }
100   void setNoMacro() { Macro = false; }
101 
102   const FeatureBitset &getFeatures() const { return Features; }
103   void setFeatures(const FeatureBitset &Features_) { Features = Features_; }
104 
105   // Set of features that are either architecture features or referenced
106   // by them (e.g.: FeatureNaN2008 implied by FeatureMips32r6).
107   // The full table can be found in MipsGenSubtargetInfo.inc (MipsFeatureKV[]).
108   // The reason we need this mask is explained in the selectArch function.
109   // FIXME: Ideally we would like TableGen to generate this information.
110   static const FeatureBitset AllArchRelatedMask;
111 
112 private:
113   unsigned ATReg = 1;
114   bool Reorder = true;
115   bool Macro = true;
116   FeatureBitset Features;
117 };
118 
119 } // end anonymous namespace
120 
121 const FeatureBitset MipsAssemblerOptions::AllArchRelatedMask = {
122     Mips::FeatureMips1, Mips::FeatureMips2, Mips::FeatureMips3,
123     Mips::FeatureMips3_32, Mips::FeatureMips3_32r2, Mips::FeatureMips4,
124     Mips::FeatureMips4_32, Mips::FeatureMips4_32r2, Mips::FeatureMips5,
125     Mips::FeatureMips5_32r2, Mips::FeatureMips32, Mips::FeatureMips32r2,
126     Mips::FeatureMips32r3, Mips::FeatureMips32r5, Mips::FeatureMips32r6,
127     Mips::FeatureMips64, Mips::FeatureMips64r2, Mips::FeatureMips64r3,
128     Mips::FeatureMips64r5, Mips::FeatureMips64r6, Mips::FeatureCnMips,
129     Mips::FeatureCnMipsP, Mips::FeatureFP64Bit, Mips::FeatureGP64Bit,
130     Mips::FeatureNaN2008
131 };
132 
133 namespace {
134 
135 class MipsAsmParser : public MCTargetAsmParser {
136   MipsTargetStreamer &getTargetStreamer() {
137     assert(getParser().getStreamer().getTargetStreamer() &&
138            "do not have a target streamer");
139     MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
140     return static_cast<MipsTargetStreamer &>(TS);
141   }
142 
143   MipsABIInfo ABI;
144   SmallVector<std::unique_ptr<MipsAssemblerOptions>, 2> AssemblerOptions;
145   MCSymbol *CurrentFn; // Pointer to the function being parsed. It may be a
146                        // nullptr, which indicates that no function is currently
147                        // selected. This usually happens after an '.end func'
148                        // directive.
149   bool IsLittleEndian;
150   bool IsPicEnabled;
151   bool IsCpRestoreSet;
152   int CpRestoreOffset;
153   unsigned GPReg;
154   unsigned CpSaveLocation;
155   /// If true, then CpSaveLocation is a register, otherwise it's an offset.
156   bool     CpSaveLocationIsRegister;
157 
158   // Map of register aliases created via the .set directive.
159   StringMap<AsmToken> RegisterSets;
160 
161   // Print a warning along with its fix-it message at the given range.
162   void printWarningWithFixIt(const Twine &Msg, const Twine &FixMsg,
163                              SMRange Range, bool ShowColors = true);
164 
165   void ConvertXWPOperands(MCInst &Inst, const OperandVector &Operands);
166 
167 #define GET_ASSEMBLER_HEADER
168 #include "MipsGenAsmMatcher.inc"
169 
170   unsigned
171   checkEarlyTargetMatchPredicate(MCInst &Inst,
172                                  const OperandVector &Operands) override;
173   unsigned checkTargetMatchPredicate(MCInst &Inst) override;
174 
175   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
176                                OperandVector &Operands, MCStreamer &Out,
177                                uint64_t &ErrorInfo,
178                                bool MatchingInlineAsm) override;
179 
180   /// Parse a register as used in CFI directives
181   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
182   OperandMatchResultTy tryParseRegister(unsigned &RegNo, SMLoc &StartLoc,
183                                         SMLoc &EndLoc) override;
184 
185   bool parseParenSuffix(StringRef Name, OperandVector &Operands);
186 
187   bool parseBracketSuffix(StringRef Name, OperandVector &Operands);
188 
189   bool mnemonicIsValid(StringRef Mnemonic, unsigned VariantID);
190 
191   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
192                         SMLoc NameLoc, OperandVector &Operands) override;
193 
194   bool ParseDirective(AsmToken DirectiveID) override;
195 
196   OperandMatchResultTy parseMemOperand(OperandVector &Operands);
197   OperandMatchResultTy
198   matchAnyRegisterNameWithoutDollar(OperandVector &Operands,
199                                     StringRef Identifier, SMLoc S);
200   OperandMatchResultTy matchAnyRegisterWithoutDollar(OperandVector &Operands,
201                                                      const AsmToken &Token,
202                                                      SMLoc S);
203   OperandMatchResultTy matchAnyRegisterWithoutDollar(OperandVector &Operands,
204                                                      SMLoc S);
205   OperandMatchResultTy parseAnyRegister(OperandVector &Operands);
206   OperandMatchResultTy parseImm(OperandVector &Operands);
207   OperandMatchResultTy parseJumpTarget(OperandVector &Operands);
208   OperandMatchResultTy parseInvNum(OperandVector &Operands);
209   OperandMatchResultTy parseRegisterList(OperandVector &Operands);
210 
211   bool searchSymbolAlias(OperandVector &Operands);
212 
213   bool parseOperand(OperandVector &, StringRef Mnemonic);
214 
215   enum MacroExpanderResultTy {
216     MER_NotAMacro,
217     MER_Success,
218     MER_Fail,
219   };
220 
221   // Expands assembly pseudo instructions.
222   MacroExpanderResultTy tryExpandInstruction(MCInst &Inst, SMLoc IDLoc,
223                                              MCStreamer &Out,
224                                              const MCSubtargetInfo *STI);
225 
226   bool expandJalWithRegs(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
227                          const MCSubtargetInfo *STI);
228 
229   bool loadImmediate(int64_t ImmValue, unsigned DstReg, unsigned SrcReg,
230                      bool Is32BitImm, bool IsAddress, SMLoc IDLoc,
231                      MCStreamer &Out, const MCSubtargetInfo *STI);
232 
233   bool loadAndAddSymbolAddress(const MCExpr *SymExpr, unsigned DstReg,
234                                unsigned SrcReg, bool Is32BitSym, SMLoc IDLoc,
235                                MCStreamer &Out, const MCSubtargetInfo *STI);
236 
237   bool emitPartialAddress(MipsTargetStreamer &TOut, SMLoc IDLoc, MCSymbol *Sym);
238 
239   bool expandLoadImm(MCInst &Inst, bool Is32BitImm, SMLoc IDLoc,
240                      MCStreamer &Out, const MCSubtargetInfo *STI);
241 
242   bool expandLoadSingleImmToGPR(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
243                                 const MCSubtargetInfo *STI);
244   bool expandLoadSingleImmToFPR(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
245                                 const MCSubtargetInfo *STI);
246   bool expandLoadDoubleImmToGPR(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
247                                 const MCSubtargetInfo *STI);
248   bool expandLoadDoubleImmToFPR(MCInst &Inst, bool Is64FPU, SMLoc IDLoc,
249                                 MCStreamer &Out, const MCSubtargetInfo *STI);
250 
251   bool expandLoadAddress(unsigned DstReg, unsigned BaseReg,
252                          const MCOperand &Offset, bool Is32BitAddress,
253                          SMLoc IDLoc, MCStreamer &Out,
254                          const MCSubtargetInfo *STI);
255 
256   bool expandUncondBranchMMPseudo(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
257                                   const MCSubtargetInfo *STI);
258 
259   void expandMem16Inst(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
260                        const MCSubtargetInfo *STI, bool IsLoad);
261   void expandMem9Inst(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
262                       const MCSubtargetInfo *STI, bool IsLoad);
263 
264   bool expandLoadStoreMultiple(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
265                                const MCSubtargetInfo *STI);
266 
267   bool expandAliasImmediate(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
268                             const MCSubtargetInfo *STI);
269 
270   bool expandBranchImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
271                        const MCSubtargetInfo *STI);
272 
273   bool expandCondBranches(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
274                           const MCSubtargetInfo *STI);
275 
276   bool expandDivRem(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
277                     const MCSubtargetInfo *STI, const bool IsMips64,
278                     const bool Signed);
279 
280   bool expandTrunc(MCInst &Inst, bool IsDouble, bool Is64FPU, SMLoc IDLoc,
281                    MCStreamer &Out, const MCSubtargetInfo *STI);
282 
283   bool expandUlh(MCInst &Inst, bool Signed, SMLoc IDLoc, MCStreamer &Out,
284                  const MCSubtargetInfo *STI);
285 
286   bool expandUsh(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
287                  const MCSubtargetInfo *STI);
288 
289   bool expandUxw(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
290                  const MCSubtargetInfo *STI);
291 
292   bool expandSge(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
293                  const MCSubtargetInfo *STI);
294 
295   bool expandSgeImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
296                     const MCSubtargetInfo *STI);
297 
298   bool expandSgtImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
299                     const MCSubtargetInfo *STI);
300 
301   bool expandRotation(MCInst &Inst, SMLoc IDLoc,
302                       MCStreamer &Out, const MCSubtargetInfo *STI);
303   bool expandRotationImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
304                          const MCSubtargetInfo *STI);
305   bool expandDRotation(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
306                        const MCSubtargetInfo *STI);
307   bool expandDRotationImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
308                           const MCSubtargetInfo *STI);
309 
310   bool expandAbs(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
311                  const MCSubtargetInfo *STI);
312 
313   bool expandMulImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
314                     const MCSubtargetInfo *STI);
315 
316   bool expandMulO(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
317                   const MCSubtargetInfo *STI);
318 
319   bool expandMulOU(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
320                    const MCSubtargetInfo *STI);
321 
322   bool expandDMULMacro(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
323                        const MCSubtargetInfo *STI);
324 
325   bool expandLoadStoreDMacro(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
326                              const MCSubtargetInfo *STI, bool IsLoad);
327 
328   bool expandStoreDM1Macro(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
329                            const MCSubtargetInfo *STI);
330 
331   bool expandSeq(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
332                  const MCSubtargetInfo *STI);
333 
334   bool expandSeqI(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
335                   const MCSubtargetInfo *STI);
336 
337   bool expandMXTRAlias(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
338                        const MCSubtargetInfo *STI);
339 
340   bool expandSaaAddr(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
341                      const MCSubtargetInfo *STI);
342 
343   bool reportParseError(Twine ErrorMsg);
344   bool reportParseError(SMLoc Loc, Twine ErrorMsg);
345 
346   bool parseMemOffset(const MCExpr *&Res, bool isParenExpr);
347 
348   bool parseSetMips0Directive();
349   bool parseSetArchDirective();
350   bool parseSetFeature(uint64_t Feature);
351   bool isPicAndNotNxxAbi(); // Used by .cpload, .cprestore, and .cpsetup.
352   bool parseDirectiveCpLoad(SMLoc Loc);
353   bool parseDirectiveCpLocal(SMLoc Loc);
354   bool parseDirectiveCpRestore(SMLoc Loc);
355   bool parseDirectiveCPSetup();
356   bool parseDirectiveCPReturn();
357   bool parseDirectiveNaN();
358   bool parseDirectiveSet();
359   bool parseDirectiveOption();
360   bool parseInsnDirective();
361   bool parseRSectionDirective(StringRef Section);
362   bool parseSSectionDirective(StringRef Section, unsigned Type);
363 
364   bool parseSetAtDirective();
365   bool parseSetNoAtDirective();
366   bool parseSetMacroDirective();
367   bool parseSetNoMacroDirective();
368   bool parseSetMsaDirective();
369   bool parseSetNoMsaDirective();
370   bool parseSetNoDspDirective();
371   bool parseSetReorderDirective();
372   bool parseSetNoReorderDirective();
373   bool parseSetMips16Directive();
374   bool parseSetNoMips16Directive();
375   bool parseSetFpDirective();
376   bool parseSetOddSPRegDirective();
377   bool parseSetNoOddSPRegDirective();
378   bool parseSetPopDirective();
379   bool parseSetPushDirective();
380   bool parseSetSoftFloatDirective();
381   bool parseSetHardFloatDirective();
382   bool parseSetMtDirective();
383   bool parseSetNoMtDirective();
384   bool parseSetNoCRCDirective();
385   bool parseSetNoVirtDirective();
386   bool parseSetNoGINVDirective();
387 
388   bool parseSetAssignment();
389 
390   bool parseDirectiveGpWord();
391   bool parseDirectiveGpDWord();
392   bool parseDirectiveDtpRelWord();
393   bool parseDirectiveDtpRelDWord();
394   bool parseDirectiveTpRelWord();
395   bool parseDirectiveTpRelDWord();
396   bool parseDirectiveModule();
397   bool parseDirectiveModuleFP();
398   bool parseFpABIValue(MipsABIFlagsSection::FpABIKind &FpABI,
399                        StringRef Directive);
400 
401   bool parseInternalDirectiveReallowModule();
402 
403   bool eatComma(StringRef ErrorStr);
404 
405   int matchCPURegisterName(StringRef Symbol);
406 
407   int matchHWRegsRegisterName(StringRef Symbol);
408 
409   int matchFPURegisterName(StringRef Name);
410 
411   int matchFCCRegisterName(StringRef Name);
412 
413   int matchACRegisterName(StringRef Name);
414 
415   int matchMSA128RegisterName(StringRef Name);
416 
417   int matchMSA128CtrlRegisterName(StringRef Name);
418 
419   unsigned getReg(int RC, int RegNo);
420 
421   /// Returns the internal register number for the current AT. Also checks if
422   /// the current AT is unavailable (set to $0) and gives an error if it is.
423   /// This should be used in pseudo-instruction expansions which need AT.
424   unsigned getATReg(SMLoc Loc);
425 
426   bool canUseATReg();
427 
428   bool processInstruction(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
429                           const MCSubtargetInfo *STI);
430 
431   // Helper function that checks if the value of a vector index is within the
432   // boundaries of accepted values for each RegisterKind
433   // Example: INSERT.B $w0[n], $1 => 16 > n >= 0
434   bool validateMSAIndex(int Val, int RegKind);
435 
436   // Selects a new architecture by updating the FeatureBits with the necessary
437   // info including implied dependencies.
438   // Internally, it clears all the feature bits related to *any* architecture
439   // and selects the new one using the ToggleFeature functionality of the
440   // MCSubtargetInfo object that handles implied dependencies. The reason we
441   // clear all the arch related bits manually is because ToggleFeature only
442   // clears the features that imply the feature being cleared and not the
443   // features implied by the feature being cleared. This is easier to see
444   // with an example:
445   //  --------------------------------------------------
446   // | Feature         | Implies                        |
447   // | -------------------------------------------------|
448   // | FeatureMips1    | None                           |
449   // | FeatureMips2    | FeatureMips1                   |
450   // | FeatureMips3    | FeatureMips2 | FeatureMipsGP64 |
451   // | FeatureMips4    | FeatureMips3                   |
452   // | ...             |                                |
453   //  --------------------------------------------------
454   //
455   // Setting Mips3 is equivalent to set: (FeatureMips3 | FeatureMips2 |
456   // FeatureMipsGP64 | FeatureMips1)
457   // Clearing Mips3 is equivalent to clear (FeatureMips3 | FeatureMips4).
458   void selectArch(StringRef ArchFeature) {
459     MCSubtargetInfo &STI = copySTI();
460     FeatureBitset FeatureBits = STI.getFeatureBits();
461     FeatureBits &= ~MipsAssemblerOptions::AllArchRelatedMask;
462     STI.setFeatureBits(FeatureBits);
463     setAvailableFeatures(
464         ComputeAvailableFeatures(STI.ToggleFeature(ArchFeature)));
465     AssemblerOptions.back()->setFeatures(STI.getFeatureBits());
466   }
467 
468   void setFeatureBits(uint64_t Feature, StringRef FeatureString) {
469     if (!(getSTI().getFeatureBits()[Feature])) {
470       MCSubtargetInfo &STI = copySTI();
471       setAvailableFeatures(
472           ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
473       AssemblerOptions.back()->setFeatures(STI.getFeatureBits());
474     }
475   }
476 
477   void clearFeatureBits(uint64_t Feature, StringRef FeatureString) {
478     if (getSTI().getFeatureBits()[Feature]) {
479       MCSubtargetInfo &STI = copySTI();
480       setAvailableFeatures(
481           ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
482       AssemblerOptions.back()->setFeatures(STI.getFeatureBits());
483     }
484   }
485 
486   void setModuleFeatureBits(uint64_t Feature, StringRef FeatureString) {
487     setFeatureBits(Feature, FeatureString);
488     AssemblerOptions.front()->setFeatures(getSTI().getFeatureBits());
489   }
490 
491   void clearModuleFeatureBits(uint64_t Feature, StringRef FeatureString) {
492     clearFeatureBits(Feature, FeatureString);
493     AssemblerOptions.front()->setFeatures(getSTI().getFeatureBits());
494   }
495 
496 public:
497   enum MipsMatchResultTy {
498     Match_RequiresDifferentSrcAndDst = FIRST_TARGET_MATCH_RESULT_TY,
499     Match_RequiresDifferentOperands,
500     Match_RequiresNoZeroRegister,
501     Match_RequiresSameSrcAndDst,
502     Match_NoFCCRegisterForCurrentISA,
503     Match_NonZeroOperandForSync,
504     Match_NonZeroOperandForMTCX,
505     Match_RequiresPosSizeRange0_32,
506     Match_RequiresPosSizeRange33_64,
507     Match_RequiresPosSizeUImm6,
508 #define GET_OPERAND_DIAGNOSTIC_TYPES
509 #include "MipsGenAsmMatcher.inc"
510 #undef GET_OPERAND_DIAGNOSTIC_TYPES
511   };
512 
513   MipsAsmParser(const MCSubtargetInfo &sti, MCAsmParser &parser,
514                 const MCInstrInfo &MII, const MCTargetOptions &Options)
515     : MCTargetAsmParser(Options, sti, MII),
516         ABI(MipsABIInfo::computeTargetABI(Triple(sti.getTargetTriple()),
517                                           sti.getCPU(), Options)) {
518     MCAsmParserExtension::Initialize(parser);
519 
520     parser.addAliasForDirective(".asciiz", ".asciz");
521     parser.addAliasForDirective(".hword", ".2byte");
522     parser.addAliasForDirective(".word", ".4byte");
523     parser.addAliasForDirective(".dword", ".8byte");
524 
525     // Initialize the set of available features.
526     setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
527 
528     // Remember the initial assembler options. The user can not modify these.
529     AssemblerOptions.push_back(
530         std::make_unique<MipsAssemblerOptions>(getSTI().getFeatureBits()));
531 
532     // Create an assembler options environment for the user to modify.
533     AssemblerOptions.push_back(
534         std::make_unique<MipsAssemblerOptions>(getSTI().getFeatureBits()));
535 
536     getTargetStreamer().updateABIInfo(*this);
537 
538     if (!isABI_O32() && !useOddSPReg() != 0)
539       report_fatal_error("-mno-odd-spreg requires the O32 ABI");
540 
541     CurrentFn = nullptr;
542 
543     IsPicEnabled = getContext().getObjectFileInfo()->isPositionIndependent();
544 
545     IsCpRestoreSet = false;
546     CpRestoreOffset = -1;
547     GPReg = ABI.GetGlobalPtr();
548 
549     const Triple &TheTriple = sti.getTargetTriple();
550     IsLittleEndian = TheTriple.isLittleEndian();
551 
552     if (getSTI().getCPU() == "mips64r6" && inMicroMipsMode())
553       report_fatal_error("microMIPS64R6 is not supported", false);
554 
555     if (!isABI_O32() && inMicroMipsMode())
556       report_fatal_error("microMIPS64 is not supported", false);
557   }
558 
559   /// True if all of $fcc0 - $fcc7 exist for the current ISA.
560   bool hasEightFccRegisters() const { return hasMips4() || hasMips32(); }
561 
562   bool isGP64bit() const {
563     return getSTI().getFeatureBits()[Mips::FeatureGP64Bit];
564   }
565 
566   bool isFP64bit() const {
567     return getSTI().getFeatureBits()[Mips::FeatureFP64Bit];
568   }
569 
570   bool isJalrRelocAvailable(const MCExpr *JalExpr) {
571     if (!EmitJalrReloc)
572       return false;
573     MCValue Res;
574     if (!JalExpr->evaluateAsRelocatable(Res, nullptr, nullptr))
575       return false;
576     if (Res.getSymB() != nullptr)
577       return false;
578     if (Res.getConstant() != 0)
579       return ABI.IsN32() || ABI.IsN64();
580     return true;
581   }
582 
583   const MipsABIInfo &getABI() const { return ABI; }
584   bool isABI_N32() const { return ABI.IsN32(); }
585   bool isABI_N64() const { return ABI.IsN64(); }
586   bool isABI_O32() const { return ABI.IsO32(); }
587   bool isABI_FPXX() const {
588     return getSTI().getFeatureBits()[Mips::FeatureFPXX];
589   }
590 
591   bool useOddSPReg() const {
592     return !(getSTI().getFeatureBits()[Mips::FeatureNoOddSPReg]);
593   }
594 
595   bool inMicroMipsMode() const {
596     return getSTI().getFeatureBits()[Mips::FeatureMicroMips];
597   }
598 
599   bool hasMips1() const {
600     return getSTI().getFeatureBits()[Mips::FeatureMips1];
601   }
602 
603   bool hasMips2() const {
604     return getSTI().getFeatureBits()[Mips::FeatureMips2];
605   }
606 
607   bool hasMips3() const {
608     return getSTI().getFeatureBits()[Mips::FeatureMips3];
609   }
610 
611   bool hasMips4() const {
612     return getSTI().getFeatureBits()[Mips::FeatureMips4];
613   }
614 
615   bool hasMips5() const {
616     return getSTI().getFeatureBits()[Mips::FeatureMips5];
617   }
618 
619   bool hasMips32() const {
620     return getSTI().getFeatureBits()[Mips::FeatureMips32];
621   }
622 
623   bool hasMips64() const {
624     return getSTI().getFeatureBits()[Mips::FeatureMips64];
625   }
626 
627   bool hasMips32r2() const {
628     return getSTI().getFeatureBits()[Mips::FeatureMips32r2];
629   }
630 
631   bool hasMips64r2() const {
632     return getSTI().getFeatureBits()[Mips::FeatureMips64r2];
633   }
634 
635   bool hasMips32r3() const {
636     return (getSTI().getFeatureBits()[Mips::FeatureMips32r3]);
637   }
638 
639   bool hasMips64r3() const {
640     return (getSTI().getFeatureBits()[Mips::FeatureMips64r3]);
641   }
642 
643   bool hasMips32r5() const {
644     return (getSTI().getFeatureBits()[Mips::FeatureMips32r5]);
645   }
646 
647   bool hasMips64r5() const {
648     return (getSTI().getFeatureBits()[Mips::FeatureMips64r5]);
649   }
650 
651   bool hasMips32r6() const {
652     return getSTI().getFeatureBits()[Mips::FeatureMips32r6];
653   }
654 
655   bool hasMips64r6() const {
656     return getSTI().getFeatureBits()[Mips::FeatureMips64r6];
657   }
658 
659   bool hasDSP() const {
660     return getSTI().getFeatureBits()[Mips::FeatureDSP];
661   }
662 
663   bool hasDSPR2() const {
664     return getSTI().getFeatureBits()[Mips::FeatureDSPR2];
665   }
666 
667   bool hasDSPR3() const {
668     return getSTI().getFeatureBits()[Mips::FeatureDSPR3];
669   }
670 
671   bool hasMSA() const {
672     return getSTI().getFeatureBits()[Mips::FeatureMSA];
673   }
674 
675   bool hasCnMips() const {
676     return (getSTI().getFeatureBits()[Mips::FeatureCnMips]);
677   }
678 
679   bool hasCnMipsP() const {
680     return (getSTI().getFeatureBits()[Mips::FeatureCnMipsP]);
681   }
682 
683   bool inPicMode() {
684     return IsPicEnabled;
685   }
686 
687   bool inMips16Mode() const {
688     return getSTI().getFeatureBits()[Mips::FeatureMips16];
689   }
690 
691   bool useTraps() const {
692     return getSTI().getFeatureBits()[Mips::FeatureUseTCCInDIV];
693   }
694 
695   bool useSoftFloat() const {
696     return getSTI().getFeatureBits()[Mips::FeatureSoftFloat];
697   }
698   bool hasMT() const {
699     return getSTI().getFeatureBits()[Mips::FeatureMT];
700   }
701 
702   bool hasCRC() const {
703     return getSTI().getFeatureBits()[Mips::FeatureCRC];
704   }
705 
706   bool hasVirt() const {
707     return getSTI().getFeatureBits()[Mips::FeatureVirt];
708   }
709 
710   bool hasGINV() const {
711     return getSTI().getFeatureBits()[Mips::FeatureGINV];
712   }
713 
714   /// Warn if RegIndex is the same as the current AT.
715   void warnIfRegIndexIsAT(unsigned RegIndex, SMLoc Loc);
716 
717   void warnIfNoMacro(SMLoc Loc);
718 
719   bool isLittle() const { return IsLittleEndian; }
720 
721   const MCExpr *createTargetUnaryExpr(const MCExpr *E,
722                                       AsmToken::TokenKind OperatorToken,
723                                       MCContext &Ctx) override {
724     switch(OperatorToken) {
725     default:
726       llvm_unreachable("Unknown token");
727       return nullptr;
728     case AsmToken::PercentCall16:
729       return MipsMCExpr::create(MipsMCExpr::MEK_GOT_CALL, E, Ctx);
730     case AsmToken::PercentCall_Hi:
731       return MipsMCExpr::create(MipsMCExpr::MEK_CALL_HI16, E, Ctx);
732     case AsmToken::PercentCall_Lo:
733       return MipsMCExpr::create(MipsMCExpr::MEK_CALL_LO16, E, Ctx);
734     case AsmToken::PercentDtprel_Hi:
735       return MipsMCExpr::create(MipsMCExpr::MEK_DTPREL_HI, E, Ctx);
736     case AsmToken::PercentDtprel_Lo:
737       return MipsMCExpr::create(MipsMCExpr::MEK_DTPREL_LO, E, Ctx);
738     case AsmToken::PercentGot:
739       return MipsMCExpr::create(MipsMCExpr::MEK_GOT, E, Ctx);
740     case AsmToken::PercentGot_Disp:
741       return MipsMCExpr::create(MipsMCExpr::MEK_GOT_DISP, E, Ctx);
742     case AsmToken::PercentGot_Hi:
743       return MipsMCExpr::create(MipsMCExpr::MEK_GOT_HI16, E, Ctx);
744     case AsmToken::PercentGot_Lo:
745       return MipsMCExpr::create(MipsMCExpr::MEK_GOT_LO16, E, Ctx);
746     case AsmToken::PercentGot_Ofst:
747       return MipsMCExpr::create(MipsMCExpr::MEK_GOT_OFST, E, Ctx);
748     case AsmToken::PercentGot_Page:
749       return MipsMCExpr::create(MipsMCExpr::MEK_GOT_PAGE, E, Ctx);
750     case AsmToken::PercentGottprel:
751       return MipsMCExpr::create(MipsMCExpr::MEK_GOTTPREL, E, Ctx);
752     case AsmToken::PercentGp_Rel:
753       return MipsMCExpr::create(MipsMCExpr::MEK_GPREL, E, Ctx);
754     case AsmToken::PercentHi:
755       return MipsMCExpr::create(MipsMCExpr::MEK_HI, E, Ctx);
756     case AsmToken::PercentHigher:
757       return MipsMCExpr::create(MipsMCExpr::MEK_HIGHER, E, Ctx);
758     case AsmToken::PercentHighest:
759       return MipsMCExpr::create(MipsMCExpr::MEK_HIGHEST, E, Ctx);
760     case AsmToken::PercentLo:
761       return MipsMCExpr::create(MipsMCExpr::MEK_LO, E, Ctx);
762     case AsmToken::PercentNeg:
763       return MipsMCExpr::create(MipsMCExpr::MEK_NEG, E, Ctx);
764     case AsmToken::PercentPcrel_Hi:
765       return MipsMCExpr::create(MipsMCExpr::MEK_PCREL_HI16, E, Ctx);
766     case AsmToken::PercentPcrel_Lo:
767       return MipsMCExpr::create(MipsMCExpr::MEK_PCREL_LO16, E, Ctx);
768     case AsmToken::PercentTlsgd:
769       return MipsMCExpr::create(MipsMCExpr::MEK_TLSGD, E, Ctx);
770     case AsmToken::PercentTlsldm:
771       return MipsMCExpr::create(MipsMCExpr::MEK_TLSLDM, E, Ctx);
772     case AsmToken::PercentTprel_Hi:
773       return MipsMCExpr::create(MipsMCExpr::MEK_TPREL_HI, E, Ctx);
774     case AsmToken::PercentTprel_Lo:
775       return MipsMCExpr::create(MipsMCExpr::MEK_TPREL_LO, E, Ctx);
776     }
777   }
778 };
779 
780 /// MipsOperand - Instances of this class represent a parsed Mips machine
781 /// instruction.
782 class MipsOperand : public MCParsedAsmOperand {
783 public:
784   /// Broad categories of register classes
785   /// The exact class is finalized by the render method.
786   enum RegKind {
787     RegKind_GPR = 1,      /// GPR32 and GPR64 (depending on isGP64bit())
788     RegKind_FGR = 2,      /// FGR32, FGR64, AFGR64 (depending on context and
789                           /// isFP64bit())
790     RegKind_FCC = 4,      /// FCC
791     RegKind_MSA128 = 8,   /// MSA128[BHWD] (makes no difference which)
792     RegKind_MSACtrl = 16, /// MSA control registers
793     RegKind_COP2 = 32,    /// COP2
794     RegKind_ACC = 64,     /// HI32DSP, LO32DSP, and ACC64DSP (depending on
795                           /// context).
796     RegKind_CCR = 128,    /// CCR
797     RegKind_HWRegs = 256, /// HWRegs
798     RegKind_COP3 = 512,   /// COP3
799     RegKind_COP0 = 1024,  /// COP0
800     /// Potentially any (e.g. $1)
801     RegKind_Numeric = RegKind_GPR | RegKind_FGR | RegKind_FCC | RegKind_MSA128 |
802                       RegKind_MSACtrl | RegKind_COP2 | RegKind_ACC |
803                       RegKind_CCR | RegKind_HWRegs | RegKind_COP3 | RegKind_COP0
804   };
805 
806 private:
807   enum KindTy {
808     k_Immediate,     /// An immediate (possibly involving symbol references)
809     k_Memory,        /// Base + Offset Memory Address
810     k_RegisterIndex, /// A register index in one or more RegKind.
811     k_Token,         /// A simple token
812     k_RegList,       /// A physical register list
813   } Kind;
814 
815 public:
816   MipsOperand(KindTy K, MipsAsmParser &Parser)
817       : MCParsedAsmOperand(), Kind(K), AsmParser(Parser) {}
818 
819   ~MipsOperand() override {
820     switch (Kind) {
821     case k_Memory:
822       delete Mem.Base;
823       break;
824     case k_RegList:
825       delete RegList.List;
826       break;
827     case k_Immediate:
828     case k_RegisterIndex:
829     case k_Token:
830       break;
831     }
832   }
833 
834 private:
835   /// For diagnostics, and checking the assembler temporary
836   MipsAsmParser &AsmParser;
837 
838   struct Token {
839     const char *Data;
840     unsigned Length;
841   };
842 
843   struct RegIdxOp {
844     unsigned Index; /// Index into the register class
845     RegKind Kind;   /// Bitfield of the kinds it could possibly be
846     struct Token Tok; /// The input token this operand originated from.
847     const MCRegisterInfo *RegInfo;
848   };
849 
850   struct ImmOp {
851     const MCExpr *Val;
852   };
853 
854   struct MemOp {
855     MipsOperand *Base;
856     const MCExpr *Off;
857   };
858 
859   struct RegListOp {
860     SmallVector<unsigned, 10> *List;
861   };
862 
863   union {
864     struct Token Tok;
865     struct RegIdxOp RegIdx;
866     struct ImmOp Imm;
867     struct MemOp Mem;
868     struct RegListOp RegList;
869   };
870 
871   SMLoc StartLoc, EndLoc;
872 
873   /// Internal constructor for register kinds
874   static std::unique_ptr<MipsOperand> CreateReg(unsigned Index, StringRef Str,
875                                                 RegKind RegKind,
876                                                 const MCRegisterInfo *RegInfo,
877                                                 SMLoc S, SMLoc E,
878                                                 MipsAsmParser &Parser) {
879     auto Op = std::make_unique<MipsOperand>(k_RegisterIndex, Parser);
880     Op->RegIdx.Index = Index;
881     Op->RegIdx.RegInfo = RegInfo;
882     Op->RegIdx.Kind = RegKind;
883     Op->RegIdx.Tok.Data = Str.data();
884     Op->RegIdx.Tok.Length = Str.size();
885     Op->StartLoc = S;
886     Op->EndLoc = E;
887     return Op;
888   }
889 
890 public:
891   /// Coerce the register to GPR32 and return the real register for the current
892   /// target.
893   unsigned getGPR32Reg() const {
894     assert(isRegIdx() && (RegIdx.Kind & RegKind_GPR) && "Invalid access!");
895     AsmParser.warnIfRegIndexIsAT(RegIdx.Index, StartLoc);
896     unsigned ClassID = Mips::GPR32RegClassID;
897     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
898   }
899 
900   /// Coerce the register to GPR32 and return the real register for the current
901   /// target.
902   unsigned getGPRMM16Reg() const {
903     assert(isRegIdx() && (RegIdx.Kind & RegKind_GPR) && "Invalid access!");
904     unsigned ClassID = Mips::GPR32RegClassID;
905     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
906   }
907 
908   /// Coerce the register to GPR64 and return the real register for the current
909   /// target.
910   unsigned getGPR64Reg() const {
911     assert(isRegIdx() && (RegIdx.Kind & RegKind_GPR) && "Invalid access!");
912     unsigned ClassID = Mips::GPR64RegClassID;
913     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
914   }
915 
916 private:
917   /// Coerce the register to AFGR64 and return the real register for the current
918   /// target.
919   unsigned getAFGR64Reg() const {
920     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
921     if (RegIdx.Index % 2 != 0)
922       AsmParser.Warning(StartLoc, "Float register should be even.");
923     return RegIdx.RegInfo->getRegClass(Mips::AFGR64RegClassID)
924         .getRegister(RegIdx.Index / 2);
925   }
926 
927   /// Coerce the register to FGR64 and return the real register for the current
928   /// target.
929   unsigned getFGR64Reg() const {
930     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
931     return RegIdx.RegInfo->getRegClass(Mips::FGR64RegClassID)
932         .getRegister(RegIdx.Index);
933   }
934 
935   /// Coerce the register to FGR32 and return the real register for the current
936   /// target.
937   unsigned getFGR32Reg() const {
938     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
939     return RegIdx.RegInfo->getRegClass(Mips::FGR32RegClassID)
940         .getRegister(RegIdx.Index);
941   }
942 
943   /// Coerce the register to FCC and return the real register for the current
944   /// target.
945   unsigned getFCCReg() const {
946     assert(isRegIdx() && (RegIdx.Kind & RegKind_FCC) && "Invalid access!");
947     return RegIdx.RegInfo->getRegClass(Mips::FCCRegClassID)
948         .getRegister(RegIdx.Index);
949   }
950 
951   /// Coerce the register to MSA128 and return the real register for the current
952   /// target.
953   unsigned getMSA128Reg() const {
954     assert(isRegIdx() && (RegIdx.Kind & RegKind_MSA128) && "Invalid access!");
955     // It doesn't matter which of the MSA128[BHWD] classes we use. They are all
956     // identical
957     unsigned ClassID = Mips::MSA128BRegClassID;
958     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
959   }
960 
961   /// Coerce the register to MSACtrl and return the real register for the
962   /// current target.
963   unsigned getMSACtrlReg() const {
964     assert(isRegIdx() && (RegIdx.Kind & RegKind_MSACtrl) && "Invalid access!");
965     unsigned ClassID = Mips::MSACtrlRegClassID;
966     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
967   }
968 
969   /// Coerce the register to COP0 and return the real register for the
970   /// current target.
971   unsigned getCOP0Reg() const {
972     assert(isRegIdx() && (RegIdx.Kind & RegKind_COP0) && "Invalid access!");
973     unsigned ClassID = Mips::COP0RegClassID;
974     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
975   }
976 
977   /// Coerce the register to COP2 and return the real register for the
978   /// current target.
979   unsigned getCOP2Reg() const {
980     assert(isRegIdx() && (RegIdx.Kind & RegKind_COP2) && "Invalid access!");
981     unsigned ClassID = Mips::COP2RegClassID;
982     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
983   }
984 
985   /// Coerce the register to COP3 and return the real register for the
986   /// current target.
987   unsigned getCOP3Reg() const {
988     assert(isRegIdx() && (RegIdx.Kind & RegKind_COP3) && "Invalid access!");
989     unsigned ClassID = Mips::COP3RegClassID;
990     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
991   }
992 
993   /// Coerce the register to ACC64DSP and return the real register for the
994   /// current target.
995   unsigned getACC64DSPReg() const {
996     assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!");
997     unsigned ClassID = Mips::ACC64DSPRegClassID;
998     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
999   }
1000 
1001   /// Coerce the register to HI32DSP and return the real register for the
1002   /// current target.
1003   unsigned getHI32DSPReg() const {
1004     assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!");
1005     unsigned ClassID = Mips::HI32DSPRegClassID;
1006     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
1007   }
1008 
1009   /// Coerce the register to LO32DSP and return the real register for the
1010   /// current target.
1011   unsigned getLO32DSPReg() const {
1012     assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!");
1013     unsigned ClassID = Mips::LO32DSPRegClassID;
1014     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
1015   }
1016 
1017   /// Coerce the register to CCR and return the real register for the
1018   /// current target.
1019   unsigned getCCRReg() const {
1020     assert(isRegIdx() && (RegIdx.Kind & RegKind_CCR) && "Invalid access!");
1021     unsigned ClassID = Mips::CCRRegClassID;
1022     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
1023   }
1024 
1025   /// Coerce the register to HWRegs and return the real register for the
1026   /// current target.
1027   unsigned getHWRegsReg() const {
1028     assert(isRegIdx() && (RegIdx.Kind & RegKind_HWRegs) && "Invalid access!");
1029     unsigned ClassID = Mips::HWRegsRegClassID;
1030     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
1031   }
1032 
1033 public:
1034   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
1035     // Add as immediate when possible.  Null MCExpr = 0.
1036     if (!Expr)
1037       Inst.addOperand(MCOperand::createImm(0));
1038     else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
1039       Inst.addOperand(MCOperand::createImm(CE->getValue()));
1040     else
1041       Inst.addOperand(MCOperand::createExpr(Expr));
1042   }
1043 
1044   void addRegOperands(MCInst &Inst, unsigned N) const {
1045     llvm_unreachable("Use a custom parser instead");
1046   }
1047 
1048   /// Render the operand to an MCInst as a GPR32
1049   /// Asserts if the wrong number of operands are requested, or the operand
1050   /// is not a k_RegisterIndex compatible with RegKind_GPR
1051   void addGPR32ZeroAsmRegOperands(MCInst &Inst, unsigned N) const {
1052     assert(N == 1 && "Invalid number of operands!");
1053     Inst.addOperand(MCOperand::createReg(getGPR32Reg()));
1054   }
1055 
1056   void addGPR32NonZeroAsmRegOperands(MCInst &Inst, unsigned N) const {
1057     assert(N == 1 && "Invalid number of operands!");
1058     Inst.addOperand(MCOperand::createReg(getGPR32Reg()));
1059   }
1060 
1061   void addGPR32AsmRegOperands(MCInst &Inst, unsigned N) const {
1062     assert(N == 1 && "Invalid number of operands!");
1063     Inst.addOperand(MCOperand::createReg(getGPR32Reg()));
1064   }
1065 
1066   void addGPRMM16AsmRegOperands(MCInst &Inst, unsigned N) const {
1067     assert(N == 1 && "Invalid number of operands!");
1068     Inst.addOperand(MCOperand::createReg(getGPRMM16Reg()));
1069   }
1070 
1071   void addGPRMM16AsmRegZeroOperands(MCInst &Inst, unsigned N) const {
1072     assert(N == 1 && "Invalid number of operands!");
1073     Inst.addOperand(MCOperand::createReg(getGPRMM16Reg()));
1074   }
1075 
1076   void addGPRMM16AsmRegMovePOperands(MCInst &Inst, unsigned N) const {
1077     assert(N == 1 && "Invalid number of operands!");
1078     Inst.addOperand(MCOperand::createReg(getGPRMM16Reg()));
1079   }
1080 
1081   void addGPRMM16AsmRegMovePPairFirstOperands(MCInst &Inst, unsigned N) const {
1082     assert(N == 1 && "Invalid number of operands!");
1083     Inst.addOperand(MCOperand::createReg(getGPRMM16Reg()));
1084   }
1085 
1086   void addGPRMM16AsmRegMovePPairSecondOperands(MCInst &Inst,
1087                                                unsigned N) const {
1088     assert(N == 1 && "Invalid number of operands!");
1089     Inst.addOperand(MCOperand::createReg(getGPRMM16Reg()));
1090   }
1091 
1092   /// Render the operand to an MCInst as a GPR64
1093   /// Asserts if the wrong number of operands are requested, or the operand
1094   /// is not a k_RegisterIndex compatible with RegKind_GPR
1095   void addGPR64AsmRegOperands(MCInst &Inst, unsigned N) const {
1096     assert(N == 1 && "Invalid number of operands!");
1097     Inst.addOperand(MCOperand::createReg(getGPR64Reg()));
1098   }
1099 
1100   void addAFGR64AsmRegOperands(MCInst &Inst, unsigned N) const {
1101     assert(N == 1 && "Invalid number of operands!");
1102     Inst.addOperand(MCOperand::createReg(getAFGR64Reg()));
1103   }
1104 
1105   void addStrictlyAFGR64AsmRegOperands(MCInst &Inst, unsigned N) const {
1106     assert(N == 1 && "Invalid number of operands!");
1107     Inst.addOperand(MCOperand::createReg(getAFGR64Reg()));
1108   }
1109 
1110   void addStrictlyFGR64AsmRegOperands(MCInst &Inst, unsigned N) const {
1111     assert(N == 1 && "Invalid number of operands!");
1112     Inst.addOperand(MCOperand::createReg(getFGR64Reg()));
1113   }
1114 
1115   void addFGR64AsmRegOperands(MCInst &Inst, unsigned N) const {
1116     assert(N == 1 && "Invalid number of operands!");
1117     Inst.addOperand(MCOperand::createReg(getFGR64Reg()));
1118   }
1119 
1120   void addFGR32AsmRegOperands(MCInst &Inst, unsigned N) const {
1121     assert(N == 1 && "Invalid number of operands!");
1122     Inst.addOperand(MCOperand::createReg(getFGR32Reg()));
1123     // FIXME: We ought to do this for -integrated-as without -via-file-asm too.
1124     // FIXME: This should propagate failure up to parseStatement.
1125     if (!AsmParser.useOddSPReg() && RegIdx.Index & 1)
1126       AsmParser.getParser().printError(
1127           StartLoc, "-mno-odd-spreg prohibits the use of odd FPU "
1128                     "registers");
1129   }
1130 
1131   void addStrictlyFGR32AsmRegOperands(MCInst &Inst, unsigned N) const {
1132     assert(N == 1 && "Invalid number of operands!");
1133     Inst.addOperand(MCOperand::createReg(getFGR32Reg()));
1134     // FIXME: We ought to do this for -integrated-as without -via-file-asm too.
1135     if (!AsmParser.useOddSPReg() && RegIdx.Index & 1)
1136       AsmParser.Error(StartLoc, "-mno-odd-spreg prohibits the use of odd FPU "
1137                                 "registers");
1138   }
1139 
1140   void addFCCAsmRegOperands(MCInst &Inst, unsigned N) const {
1141     assert(N == 1 && "Invalid number of operands!");
1142     Inst.addOperand(MCOperand::createReg(getFCCReg()));
1143   }
1144 
1145   void addMSA128AsmRegOperands(MCInst &Inst, unsigned N) const {
1146     assert(N == 1 && "Invalid number of operands!");
1147     Inst.addOperand(MCOperand::createReg(getMSA128Reg()));
1148   }
1149 
1150   void addMSACtrlAsmRegOperands(MCInst &Inst, unsigned N) const {
1151     assert(N == 1 && "Invalid number of operands!");
1152     Inst.addOperand(MCOperand::createReg(getMSACtrlReg()));
1153   }
1154 
1155   void addCOP0AsmRegOperands(MCInst &Inst, unsigned N) const {
1156     assert(N == 1 && "Invalid number of operands!");
1157     Inst.addOperand(MCOperand::createReg(getCOP0Reg()));
1158   }
1159 
1160   void addCOP2AsmRegOperands(MCInst &Inst, unsigned N) const {
1161     assert(N == 1 && "Invalid number of operands!");
1162     Inst.addOperand(MCOperand::createReg(getCOP2Reg()));
1163   }
1164 
1165   void addCOP3AsmRegOperands(MCInst &Inst, unsigned N) const {
1166     assert(N == 1 && "Invalid number of operands!");
1167     Inst.addOperand(MCOperand::createReg(getCOP3Reg()));
1168   }
1169 
1170   void addACC64DSPAsmRegOperands(MCInst &Inst, unsigned N) const {
1171     assert(N == 1 && "Invalid number of operands!");
1172     Inst.addOperand(MCOperand::createReg(getACC64DSPReg()));
1173   }
1174 
1175   void addHI32DSPAsmRegOperands(MCInst &Inst, unsigned N) const {
1176     assert(N == 1 && "Invalid number of operands!");
1177     Inst.addOperand(MCOperand::createReg(getHI32DSPReg()));
1178   }
1179 
1180   void addLO32DSPAsmRegOperands(MCInst &Inst, unsigned N) const {
1181     assert(N == 1 && "Invalid number of operands!");
1182     Inst.addOperand(MCOperand::createReg(getLO32DSPReg()));
1183   }
1184 
1185   void addCCRAsmRegOperands(MCInst &Inst, unsigned N) const {
1186     assert(N == 1 && "Invalid number of operands!");
1187     Inst.addOperand(MCOperand::createReg(getCCRReg()));
1188   }
1189 
1190   void addHWRegsAsmRegOperands(MCInst &Inst, unsigned N) const {
1191     assert(N == 1 && "Invalid number of operands!");
1192     Inst.addOperand(MCOperand::createReg(getHWRegsReg()));
1193   }
1194 
1195   template <unsigned Bits, int Offset = 0, int AdjustOffset = 0>
1196   void addConstantUImmOperands(MCInst &Inst, unsigned N) const {
1197     assert(N == 1 && "Invalid number of operands!");
1198     uint64_t Imm = getConstantImm() - Offset;
1199     Imm &= (1ULL << Bits) - 1;
1200     Imm += Offset;
1201     Imm += AdjustOffset;
1202     Inst.addOperand(MCOperand::createImm(Imm));
1203   }
1204 
1205   template <unsigned Bits>
1206   void addSImmOperands(MCInst &Inst, unsigned N) const {
1207     if (isImm() && !isConstantImm()) {
1208       addExpr(Inst, getImm());
1209       return;
1210     }
1211     addConstantSImmOperands<Bits, 0, 0>(Inst, N);
1212   }
1213 
1214   template <unsigned Bits>
1215   void addUImmOperands(MCInst &Inst, unsigned N) const {
1216     if (isImm() && !isConstantImm()) {
1217       addExpr(Inst, getImm());
1218       return;
1219     }
1220     addConstantUImmOperands<Bits, 0, 0>(Inst, N);
1221   }
1222 
1223   template <unsigned Bits, int Offset = 0, int AdjustOffset = 0>
1224   void addConstantSImmOperands(MCInst &Inst, unsigned N) const {
1225     assert(N == 1 && "Invalid number of operands!");
1226     int64_t Imm = getConstantImm() - Offset;
1227     Imm = SignExtend64<Bits>(Imm);
1228     Imm += Offset;
1229     Imm += AdjustOffset;
1230     Inst.addOperand(MCOperand::createImm(Imm));
1231   }
1232 
1233   void addImmOperands(MCInst &Inst, unsigned N) const {
1234     assert(N == 1 && "Invalid number of operands!");
1235     const MCExpr *Expr = getImm();
1236     addExpr(Inst, Expr);
1237   }
1238 
1239   void addMemOperands(MCInst &Inst, unsigned N) const {
1240     assert(N == 2 && "Invalid number of operands!");
1241 
1242     Inst.addOperand(MCOperand::createReg(AsmParser.getABI().ArePtrs64bit()
1243                                              ? getMemBase()->getGPR64Reg()
1244                                              : getMemBase()->getGPR32Reg()));
1245 
1246     const MCExpr *Expr = getMemOff();
1247     addExpr(Inst, Expr);
1248   }
1249 
1250   void addMicroMipsMemOperands(MCInst &Inst, unsigned N) const {
1251     assert(N == 2 && "Invalid number of operands!");
1252 
1253     Inst.addOperand(MCOperand::createReg(getMemBase()->getGPRMM16Reg()));
1254 
1255     const MCExpr *Expr = getMemOff();
1256     addExpr(Inst, Expr);
1257   }
1258 
1259   void addRegListOperands(MCInst &Inst, unsigned N) const {
1260     assert(N == 1 && "Invalid number of operands!");
1261 
1262     for (auto RegNo : getRegList())
1263       Inst.addOperand(MCOperand::createReg(RegNo));
1264   }
1265 
1266   bool isReg() const override {
1267     // As a special case until we sort out the definition of div/divu, accept
1268     // $0/$zero here so that MCK_ZERO works correctly.
1269     return isGPRAsmReg() && RegIdx.Index == 0;
1270   }
1271 
1272   bool isRegIdx() const { return Kind == k_RegisterIndex; }
1273   bool isImm() const override { return Kind == k_Immediate; }
1274 
1275   bool isConstantImm() const {
1276     int64_t Res;
1277     return isImm() && getImm()->evaluateAsAbsolute(Res);
1278   }
1279 
1280   bool isConstantImmz() const {
1281     return isConstantImm() && getConstantImm() == 0;
1282   }
1283 
1284   template <unsigned Bits, int Offset = 0> bool isConstantUImm() const {
1285     return isConstantImm() && isUInt<Bits>(getConstantImm() - Offset);
1286   }
1287 
1288   template <unsigned Bits> bool isSImm() const {
1289     return isConstantImm() ? isInt<Bits>(getConstantImm()) : isImm();
1290   }
1291 
1292   template <unsigned Bits> bool isUImm() const {
1293     return isConstantImm() ? isUInt<Bits>(getConstantImm()) : isImm();
1294   }
1295 
1296   template <unsigned Bits> bool isAnyImm() const {
1297     return isConstantImm() ? (isInt<Bits>(getConstantImm()) ||
1298                               isUInt<Bits>(getConstantImm()))
1299                            : isImm();
1300   }
1301 
1302   template <unsigned Bits, int Offset = 0> bool isConstantSImm() const {
1303     return isConstantImm() && isInt<Bits>(getConstantImm() - Offset);
1304   }
1305 
1306   template <unsigned Bottom, unsigned Top> bool isConstantUImmRange() const {
1307     return isConstantImm() && getConstantImm() >= Bottom &&
1308            getConstantImm() <= Top;
1309   }
1310 
1311   bool isToken() const override {
1312     // Note: It's not possible to pretend that other operand kinds are tokens.
1313     // The matcher emitter checks tokens first.
1314     return Kind == k_Token;
1315   }
1316 
1317   bool isMem() const override { return Kind == k_Memory; }
1318 
1319   bool isConstantMemOff() const {
1320     return isMem() && isa<MCConstantExpr>(getMemOff());
1321   }
1322 
1323   // Allow relocation operators.
1324   template <unsigned Bits, unsigned ShiftAmount = 0>
1325   bool isMemWithSimmOffset() const {
1326     if (!isMem())
1327       return false;
1328     if (!getMemBase()->isGPRAsmReg())
1329       return false;
1330     if (isa<MCTargetExpr>(getMemOff()) ||
1331         (isConstantMemOff() &&
1332          isShiftedInt<Bits, ShiftAmount>(getConstantMemOff())))
1333       return true;
1334     MCValue Res;
1335     bool IsReloc = getMemOff()->evaluateAsRelocatable(Res, nullptr, nullptr);
1336     return IsReloc && isShiftedInt<Bits, ShiftAmount>(Res.getConstant());
1337   }
1338 
1339   bool isMemWithPtrSizeOffset() const {
1340     if (!isMem())
1341       return false;
1342     if (!getMemBase()->isGPRAsmReg())
1343       return false;
1344     const unsigned PtrBits = AsmParser.getABI().ArePtrs64bit() ? 64 : 32;
1345     if (isa<MCTargetExpr>(getMemOff()) ||
1346         (isConstantMemOff() && isIntN(PtrBits, getConstantMemOff())))
1347       return true;
1348     MCValue Res;
1349     bool IsReloc = getMemOff()->evaluateAsRelocatable(Res, nullptr, nullptr);
1350     return IsReloc && isIntN(PtrBits, Res.getConstant());
1351   }
1352 
1353   bool isMemWithGRPMM16Base() const {
1354     return isMem() && getMemBase()->isMM16AsmReg();
1355   }
1356 
1357   template <unsigned Bits> bool isMemWithUimmOffsetSP() const {
1358     return isMem() && isConstantMemOff() && isUInt<Bits>(getConstantMemOff())
1359       && getMemBase()->isRegIdx() && (getMemBase()->getGPR32Reg() == Mips::SP);
1360   }
1361 
1362   template <unsigned Bits> bool isMemWithUimmWordAlignedOffsetSP() const {
1363     return isMem() && isConstantMemOff() && isUInt<Bits>(getConstantMemOff())
1364       && (getConstantMemOff() % 4 == 0) && getMemBase()->isRegIdx()
1365       && (getMemBase()->getGPR32Reg() == Mips::SP);
1366   }
1367 
1368   template <unsigned Bits> bool isMemWithSimmWordAlignedOffsetGP() const {
1369     return isMem() && isConstantMemOff() && isInt<Bits>(getConstantMemOff())
1370       && (getConstantMemOff() % 4 == 0) && getMemBase()->isRegIdx()
1371       && (getMemBase()->getGPR32Reg() == Mips::GP);
1372   }
1373 
1374   template <unsigned Bits, unsigned ShiftLeftAmount>
1375   bool isScaledUImm() const {
1376     return isConstantImm() &&
1377            isShiftedUInt<Bits, ShiftLeftAmount>(getConstantImm());
1378   }
1379 
1380   template <unsigned Bits, unsigned ShiftLeftAmount>
1381   bool isScaledSImm() const {
1382     if (isConstantImm() &&
1383         isShiftedInt<Bits, ShiftLeftAmount>(getConstantImm()))
1384       return true;
1385     // Operand can also be a symbol or symbol plus
1386     // offset in case of relocations.
1387     if (Kind != k_Immediate)
1388       return false;
1389     MCValue Res;
1390     bool Success = getImm()->evaluateAsRelocatable(Res, nullptr, nullptr);
1391     return Success && isShiftedInt<Bits, ShiftLeftAmount>(Res.getConstant());
1392   }
1393 
1394   bool isRegList16() const {
1395     if (!isRegList())
1396       return false;
1397 
1398     int Size = RegList.List->size();
1399     if (Size < 2 || Size > 5)
1400       return false;
1401 
1402     unsigned R0 = RegList.List->front();
1403     unsigned R1 = RegList.List->back();
1404     if (!((R0 == Mips::S0 && R1 == Mips::RA) ||
1405           (R0 == Mips::S0_64 && R1 == Mips::RA_64)))
1406       return false;
1407 
1408     int PrevReg = *RegList.List->begin();
1409     for (int i = 1; i < Size - 1; i++) {
1410       int Reg = (*(RegList.List))[i];
1411       if ( Reg != PrevReg + 1)
1412         return false;
1413       PrevReg = Reg;
1414     }
1415 
1416     return true;
1417   }
1418 
1419   bool isInvNum() const { return Kind == k_Immediate; }
1420 
1421   bool isLSAImm() const {
1422     if (!isConstantImm())
1423       return false;
1424     int64_t Val = getConstantImm();
1425     return 1 <= Val && Val <= 4;
1426   }
1427 
1428   bool isRegList() const { return Kind == k_RegList; }
1429 
1430   StringRef getToken() const {
1431     assert(Kind == k_Token && "Invalid access!");
1432     return StringRef(Tok.Data, Tok.Length);
1433   }
1434 
1435   unsigned getReg() const override {
1436     // As a special case until we sort out the definition of div/divu, accept
1437     // $0/$zero here so that MCK_ZERO works correctly.
1438     if (Kind == k_RegisterIndex && RegIdx.Index == 0 &&
1439         RegIdx.Kind & RegKind_GPR)
1440       return getGPR32Reg(); // FIXME: GPR64 too
1441 
1442     llvm_unreachable("Invalid access!");
1443     return 0;
1444   }
1445 
1446   const MCExpr *getImm() const {
1447     assert((Kind == k_Immediate) && "Invalid access!");
1448     return Imm.Val;
1449   }
1450 
1451   int64_t getConstantImm() const {
1452     const MCExpr *Val = getImm();
1453     int64_t Value = 0;
1454     (void)Val->evaluateAsAbsolute(Value);
1455     return Value;
1456   }
1457 
1458   MipsOperand *getMemBase() const {
1459     assert((Kind == k_Memory) && "Invalid access!");
1460     return Mem.Base;
1461   }
1462 
1463   const MCExpr *getMemOff() const {
1464     assert((Kind == k_Memory) && "Invalid access!");
1465     return Mem.Off;
1466   }
1467 
1468   int64_t getConstantMemOff() const {
1469     return static_cast<const MCConstantExpr *>(getMemOff())->getValue();
1470   }
1471 
1472   const SmallVectorImpl<unsigned> &getRegList() const {
1473     assert((Kind == k_RegList) && "Invalid access!");
1474     return *(RegList.List);
1475   }
1476 
1477   static std::unique_ptr<MipsOperand> CreateToken(StringRef Str, SMLoc S,
1478                                                   MipsAsmParser &Parser) {
1479     auto Op = std::make_unique<MipsOperand>(k_Token, Parser);
1480     Op->Tok.Data = Str.data();
1481     Op->Tok.Length = Str.size();
1482     Op->StartLoc = S;
1483     Op->EndLoc = S;
1484     return Op;
1485   }
1486 
1487   /// Create a numeric register (e.g. $1). The exact register remains
1488   /// unresolved until an instruction successfully matches
1489   static std::unique_ptr<MipsOperand>
1490   createNumericReg(unsigned Index, StringRef Str, const MCRegisterInfo *RegInfo,
1491                    SMLoc S, SMLoc E, MipsAsmParser &Parser) {
1492     LLVM_DEBUG(dbgs() << "createNumericReg(" << Index << ", ...)\n");
1493     return CreateReg(Index, Str, RegKind_Numeric, RegInfo, S, E, Parser);
1494   }
1495 
1496   /// Create a register that is definitely a GPR.
1497   /// This is typically only used for named registers such as $gp.
1498   static std::unique_ptr<MipsOperand>
1499   createGPRReg(unsigned Index, StringRef Str, const MCRegisterInfo *RegInfo,
1500                SMLoc S, SMLoc E, MipsAsmParser &Parser) {
1501     return CreateReg(Index, Str, RegKind_GPR, RegInfo, S, E, Parser);
1502   }
1503 
1504   /// Create a register that is definitely a FGR.
1505   /// This is typically only used for named registers such as $f0.
1506   static std::unique_ptr<MipsOperand>
1507   createFGRReg(unsigned Index, StringRef Str, const MCRegisterInfo *RegInfo,
1508                SMLoc S, SMLoc E, MipsAsmParser &Parser) {
1509     return CreateReg(Index, Str, RegKind_FGR, RegInfo, S, E, Parser);
1510   }
1511 
1512   /// Create a register that is definitely a HWReg.
1513   /// This is typically only used for named registers such as $hwr_cpunum.
1514   static std::unique_ptr<MipsOperand>
1515   createHWRegsReg(unsigned Index, StringRef Str, const MCRegisterInfo *RegInfo,
1516                   SMLoc S, SMLoc E, MipsAsmParser &Parser) {
1517     return CreateReg(Index, Str, RegKind_HWRegs, RegInfo, S, E, Parser);
1518   }
1519 
1520   /// Create a register that is definitely an FCC.
1521   /// This is typically only used for named registers such as $fcc0.
1522   static std::unique_ptr<MipsOperand>
1523   createFCCReg(unsigned Index, StringRef Str, const MCRegisterInfo *RegInfo,
1524                SMLoc S, SMLoc E, MipsAsmParser &Parser) {
1525     return CreateReg(Index, Str, RegKind_FCC, RegInfo, S, E, Parser);
1526   }
1527 
1528   /// Create a register that is definitely an ACC.
1529   /// This is typically only used for named registers such as $ac0.
1530   static std::unique_ptr<MipsOperand>
1531   createACCReg(unsigned Index, StringRef Str, const MCRegisterInfo *RegInfo,
1532                SMLoc S, SMLoc E, MipsAsmParser &Parser) {
1533     return CreateReg(Index, Str, RegKind_ACC, RegInfo, S, E, Parser);
1534   }
1535 
1536   /// Create a register that is definitely an MSA128.
1537   /// This is typically only used for named registers such as $w0.
1538   static std::unique_ptr<MipsOperand>
1539   createMSA128Reg(unsigned Index, StringRef Str, const MCRegisterInfo *RegInfo,
1540                   SMLoc S, SMLoc E, MipsAsmParser &Parser) {
1541     return CreateReg(Index, Str, RegKind_MSA128, RegInfo, S, E, Parser);
1542   }
1543 
1544   /// Create a register that is definitely an MSACtrl.
1545   /// This is typically only used for named registers such as $msaaccess.
1546   static std::unique_ptr<MipsOperand>
1547   createMSACtrlReg(unsigned Index, StringRef Str, const MCRegisterInfo *RegInfo,
1548                    SMLoc S, SMLoc E, MipsAsmParser &Parser) {
1549     return CreateReg(Index, Str, RegKind_MSACtrl, RegInfo, S, E, Parser);
1550   }
1551 
1552   static std::unique_ptr<MipsOperand>
1553   CreateImm(const MCExpr *Val, SMLoc S, SMLoc E, MipsAsmParser &Parser) {
1554     auto Op = std::make_unique<MipsOperand>(k_Immediate, Parser);
1555     Op->Imm.Val = Val;
1556     Op->StartLoc = S;
1557     Op->EndLoc = E;
1558     return Op;
1559   }
1560 
1561   static std::unique_ptr<MipsOperand>
1562   CreateMem(std::unique_ptr<MipsOperand> Base, const MCExpr *Off, SMLoc S,
1563             SMLoc E, MipsAsmParser &Parser) {
1564     auto Op = std::make_unique<MipsOperand>(k_Memory, Parser);
1565     Op->Mem.Base = Base.release();
1566     Op->Mem.Off = Off;
1567     Op->StartLoc = S;
1568     Op->EndLoc = E;
1569     return Op;
1570   }
1571 
1572   static std::unique_ptr<MipsOperand>
1573   CreateRegList(SmallVectorImpl<unsigned> &Regs, SMLoc StartLoc, SMLoc EndLoc,
1574                 MipsAsmParser &Parser) {
1575     assert(Regs.size() > 0 && "Empty list not allowed");
1576 
1577     auto Op = std::make_unique<MipsOperand>(k_RegList, Parser);
1578     Op->RegList.List = new SmallVector<unsigned, 10>(Regs.begin(), Regs.end());
1579     Op->StartLoc = StartLoc;
1580     Op->EndLoc = EndLoc;
1581     return Op;
1582   }
1583 
1584  bool isGPRZeroAsmReg() const {
1585     return isRegIdx() && RegIdx.Kind & RegKind_GPR && RegIdx.Index == 0;
1586   }
1587 
1588  bool isGPRNonZeroAsmReg() const {
1589    return isRegIdx() && RegIdx.Kind & RegKind_GPR && RegIdx.Index > 0 &&
1590           RegIdx.Index <= 31;
1591   }
1592 
1593   bool isGPRAsmReg() const {
1594     return isRegIdx() && RegIdx.Kind & RegKind_GPR && RegIdx.Index <= 31;
1595   }
1596 
1597   bool isMM16AsmReg() const {
1598     if (!(isRegIdx() && RegIdx.Kind))
1599       return false;
1600     return ((RegIdx.Index >= 2 && RegIdx.Index <= 7)
1601             || RegIdx.Index == 16 || RegIdx.Index == 17);
1602 
1603   }
1604   bool isMM16AsmRegZero() const {
1605     if (!(isRegIdx() && RegIdx.Kind))
1606       return false;
1607     return (RegIdx.Index == 0 ||
1608             (RegIdx.Index >= 2 && RegIdx.Index <= 7) ||
1609             RegIdx.Index == 17);
1610   }
1611 
1612   bool isMM16AsmRegMoveP() const {
1613     if (!(isRegIdx() && RegIdx.Kind))
1614       return false;
1615     return (RegIdx.Index == 0 || (RegIdx.Index >= 2 && RegIdx.Index <= 3) ||
1616       (RegIdx.Index >= 16 && RegIdx.Index <= 20));
1617   }
1618 
1619   bool isMM16AsmRegMovePPairFirst() const {
1620     if (!(isRegIdx() && RegIdx.Kind))
1621       return false;
1622     return RegIdx.Index >= 4 && RegIdx.Index <= 6;
1623   }
1624 
1625   bool isMM16AsmRegMovePPairSecond() const {
1626     if (!(isRegIdx() && RegIdx.Kind))
1627       return false;
1628     return (RegIdx.Index == 21 || RegIdx.Index == 22 ||
1629       (RegIdx.Index >= 5 && RegIdx.Index <= 7));
1630   }
1631 
1632   bool isFGRAsmReg() const {
1633     // AFGR64 is $0-$15 but we handle this in getAFGR64()
1634     return isRegIdx() && RegIdx.Kind & RegKind_FGR && RegIdx.Index <= 31;
1635   }
1636 
1637   bool isStrictlyFGRAsmReg() const {
1638     // AFGR64 is $0-$15 but we handle this in getAFGR64()
1639     return isRegIdx() && RegIdx.Kind == RegKind_FGR && RegIdx.Index <= 31;
1640   }
1641 
1642   bool isHWRegsAsmReg() const {
1643     return isRegIdx() && RegIdx.Kind & RegKind_HWRegs && RegIdx.Index <= 31;
1644   }
1645 
1646   bool isCCRAsmReg() const {
1647     return isRegIdx() && RegIdx.Kind & RegKind_CCR && RegIdx.Index <= 31;
1648   }
1649 
1650   bool isFCCAsmReg() const {
1651     if (!(isRegIdx() && RegIdx.Kind & RegKind_FCC))
1652       return false;
1653     return RegIdx.Index <= 7;
1654   }
1655 
1656   bool isACCAsmReg() const {
1657     return isRegIdx() && RegIdx.Kind & RegKind_ACC && RegIdx.Index <= 3;
1658   }
1659 
1660   bool isCOP0AsmReg() const {
1661     return isRegIdx() && RegIdx.Kind & RegKind_COP0 && RegIdx.Index <= 31;
1662   }
1663 
1664   bool isCOP2AsmReg() const {
1665     return isRegIdx() && RegIdx.Kind & RegKind_COP2 && RegIdx.Index <= 31;
1666   }
1667 
1668   bool isCOP3AsmReg() const {
1669     return isRegIdx() && RegIdx.Kind & RegKind_COP3 && RegIdx.Index <= 31;
1670   }
1671 
1672   bool isMSA128AsmReg() const {
1673     return isRegIdx() && RegIdx.Kind & RegKind_MSA128 && RegIdx.Index <= 31;
1674   }
1675 
1676   bool isMSACtrlAsmReg() const {
1677     return isRegIdx() && RegIdx.Kind & RegKind_MSACtrl && RegIdx.Index <= 7;
1678   }
1679 
1680   /// getStartLoc - Get the location of the first token of this operand.
1681   SMLoc getStartLoc() const override { return StartLoc; }
1682   /// getEndLoc - Get the location of the last token of this operand.
1683   SMLoc getEndLoc() const override { return EndLoc; }
1684 
1685   void print(raw_ostream &OS) const override {
1686     switch (Kind) {
1687     case k_Immediate:
1688       OS << "Imm<";
1689       OS << *Imm.Val;
1690       OS << ">";
1691       break;
1692     case k_Memory:
1693       OS << "Mem<";
1694       Mem.Base->print(OS);
1695       OS << ", ";
1696       OS << *Mem.Off;
1697       OS << ">";
1698       break;
1699     case k_RegisterIndex:
1700       OS << "RegIdx<" << RegIdx.Index << ":" << RegIdx.Kind << ", "
1701          << StringRef(RegIdx.Tok.Data, RegIdx.Tok.Length) << ">";
1702       break;
1703     case k_Token:
1704       OS << getToken();
1705       break;
1706     case k_RegList:
1707       OS << "RegList< ";
1708       for (auto Reg : (*RegList.List))
1709         OS << Reg << " ";
1710       OS <<  ">";
1711       break;
1712     }
1713   }
1714 
1715   bool isValidForTie(const MipsOperand &Other) const {
1716     if (Kind != Other.Kind)
1717       return false;
1718 
1719     switch (Kind) {
1720     default:
1721       llvm_unreachable("Unexpected kind");
1722       return false;
1723     case k_RegisterIndex: {
1724       StringRef Token(RegIdx.Tok.Data, RegIdx.Tok.Length);
1725       StringRef OtherToken(Other.RegIdx.Tok.Data, Other.RegIdx.Tok.Length);
1726       return Token == OtherToken;
1727     }
1728     }
1729   }
1730 }; // class MipsOperand
1731 
1732 } // end anonymous namespace
1733 
1734 namespace llvm {
1735 
1736 extern const MCInstrDesc MipsInsts[];
1737 
1738 } // end namespace llvm
1739 
1740 static const MCInstrDesc &getInstDesc(unsigned Opcode) {
1741   return MipsInsts[Opcode];
1742 }
1743 
1744 static bool hasShortDelaySlot(MCInst &Inst) {
1745   switch (Inst.getOpcode()) {
1746     case Mips::BEQ_MM:
1747     case Mips::BNE_MM:
1748     case Mips::BLTZ_MM:
1749     case Mips::BGEZ_MM:
1750     case Mips::BLEZ_MM:
1751     case Mips::BGTZ_MM:
1752     case Mips::JRC16_MM:
1753     case Mips::JALS_MM:
1754     case Mips::JALRS_MM:
1755     case Mips::JALRS16_MM:
1756     case Mips::BGEZALS_MM:
1757     case Mips::BLTZALS_MM:
1758       return true;
1759     case Mips::J_MM:
1760       return !Inst.getOperand(0).isReg();
1761     default:
1762       return false;
1763   }
1764 }
1765 
1766 static const MCSymbol *getSingleMCSymbol(const MCExpr *Expr) {
1767   if (const MCSymbolRefExpr *SRExpr = dyn_cast<MCSymbolRefExpr>(Expr)) {
1768     return &SRExpr->getSymbol();
1769   }
1770 
1771   if (const MCBinaryExpr *BExpr = dyn_cast<MCBinaryExpr>(Expr)) {
1772     const MCSymbol *LHSSym = getSingleMCSymbol(BExpr->getLHS());
1773     const MCSymbol *RHSSym = getSingleMCSymbol(BExpr->getRHS());
1774 
1775     if (LHSSym)
1776       return LHSSym;
1777 
1778     if (RHSSym)
1779       return RHSSym;
1780 
1781     return nullptr;
1782   }
1783 
1784   if (const MCUnaryExpr *UExpr = dyn_cast<MCUnaryExpr>(Expr))
1785     return getSingleMCSymbol(UExpr->getSubExpr());
1786 
1787   return nullptr;
1788 }
1789 
1790 static unsigned countMCSymbolRefExpr(const MCExpr *Expr) {
1791   if (isa<MCSymbolRefExpr>(Expr))
1792     return 1;
1793 
1794   if (const MCBinaryExpr *BExpr = dyn_cast<MCBinaryExpr>(Expr))
1795     return countMCSymbolRefExpr(BExpr->getLHS()) +
1796            countMCSymbolRefExpr(BExpr->getRHS());
1797 
1798   if (const MCUnaryExpr *UExpr = dyn_cast<MCUnaryExpr>(Expr))
1799     return countMCSymbolRefExpr(UExpr->getSubExpr());
1800 
1801   return 0;
1802 }
1803 
1804 static bool isEvaluated(const MCExpr *Expr) {
1805   switch (Expr->getKind()) {
1806   case MCExpr::Constant:
1807     return true;
1808   case MCExpr::SymbolRef:
1809     return (cast<MCSymbolRefExpr>(Expr)->getKind() != MCSymbolRefExpr::VK_None);
1810   case MCExpr::Binary: {
1811     const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
1812     if (!isEvaluated(BE->getLHS()))
1813       return false;
1814     return isEvaluated(BE->getRHS());
1815   }
1816   case MCExpr::Unary:
1817     return isEvaluated(cast<MCUnaryExpr>(Expr)->getSubExpr());
1818   case MCExpr::Target:
1819     return true;
1820   }
1821   return false;
1822 }
1823 
1824 static bool needsExpandMemInst(MCInst &Inst) {
1825   const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
1826 
1827   unsigned NumOp = MCID.getNumOperands();
1828   if (NumOp != 3 && NumOp != 4)
1829     return false;
1830 
1831   const MCOperandInfo &OpInfo = MCID.OpInfo[NumOp - 1];
1832   if (OpInfo.OperandType != MCOI::OPERAND_MEMORY &&
1833       OpInfo.OperandType != MCOI::OPERAND_UNKNOWN &&
1834       OpInfo.OperandType != MipsII::OPERAND_MEM_SIMM9)
1835     return false;
1836 
1837   MCOperand &Op = Inst.getOperand(NumOp - 1);
1838   if (Op.isImm()) {
1839     if (OpInfo.OperandType == MipsII::OPERAND_MEM_SIMM9)
1840       return !isInt<9>(Op.getImm());
1841     // Offset can't exceed 16bit value.
1842     return !isInt<16>(Op.getImm());
1843   }
1844 
1845   if (Op.isExpr()) {
1846     const MCExpr *Expr = Op.getExpr();
1847     if (Expr->getKind() != MCExpr::SymbolRef)
1848       return !isEvaluated(Expr);
1849 
1850     // Expand symbol.
1851     const MCSymbolRefExpr *SR = static_cast<const MCSymbolRefExpr *>(Expr);
1852     return SR->getKind() == MCSymbolRefExpr::VK_None;
1853   }
1854 
1855   return false;
1856 }
1857 
1858 bool MipsAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
1859                                        MCStreamer &Out,
1860                                        const MCSubtargetInfo *STI) {
1861   MipsTargetStreamer &TOut = getTargetStreamer();
1862   const unsigned Opcode = Inst.getOpcode();
1863   const MCInstrDesc &MCID = getInstDesc(Opcode);
1864   bool ExpandedJalSym = false;
1865 
1866   Inst.setLoc(IDLoc);
1867 
1868   if (MCID.isBranch() || MCID.isCall()) {
1869     MCOperand Offset;
1870 
1871     switch (Opcode) {
1872     default:
1873       break;
1874     case Mips::BBIT0:
1875     case Mips::BBIT032:
1876     case Mips::BBIT1:
1877     case Mips::BBIT132:
1878       assert(hasCnMips() && "instruction only valid for octeon cpus");
1879       LLVM_FALLTHROUGH;
1880 
1881     case Mips::BEQ:
1882     case Mips::BNE:
1883     case Mips::BEQ_MM:
1884     case Mips::BNE_MM:
1885       assert(MCID.getNumOperands() == 3 && "unexpected number of operands");
1886       Offset = Inst.getOperand(2);
1887       if (!Offset.isImm())
1888         break; // We'll deal with this situation later on when applying fixups.
1889       if (!isIntN(inMicroMipsMode() ? 17 : 18, Offset.getImm()))
1890         return Error(IDLoc, "branch target out of range");
1891       if (offsetToAlignment(Offset.getImm(),
1892                             (inMicroMipsMode() ? Align(2) : Align(4))))
1893         return Error(IDLoc, "branch to misaligned address");
1894       break;
1895     case Mips::BGEZ:
1896     case Mips::BGTZ:
1897     case Mips::BLEZ:
1898     case Mips::BLTZ:
1899     case Mips::BGEZAL:
1900     case Mips::BLTZAL:
1901     case Mips::BC1F:
1902     case Mips::BC1T:
1903     case Mips::BGEZ_MM:
1904     case Mips::BGTZ_MM:
1905     case Mips::BLEZ_MM:
1906     case Mips::BLTZ_MM:
1907     case Mips::BGEZAL_MM:
1908     case Mips::BLTZAL_MM:
1909     case Mips::BC1F_MM:
1910     case Mips::BC1T_MM:
1911     case Mips::BC1EQZC_MMR6:
1912     case Mips::BC1NEZC_MMR6:
1913     case Mips::BC2EQZC_MMR6:
1914     case Mips::BC2NEZC_MMR6:
1915       assert(MCID.getNumOperands() == 2 && "unexpected number of operands");
1916       Offset = Inst.getOperand(1);
1917       if (!Offset.isImm())
1918         break; // We'll deal with this situation later on when applying fixups.
1919       if (!isIntN(inMicroMipsMode() ? 17 : 18, Offset.getImm()))
1920         return Error(IDLoc, "branch target out of range");
1921       if (offsetToAlignment(Offset.getImm(),
1922                             (inMicroMipsMode() ? Align(2) : Align(4))))
1923         return Error(IDLoc, "branch to misaligned address");
1924       break;
1925     case Mips::BGEC:    case Mips::BGEC_MMR6:
1926     case Mips::BLTC:    case Mips::BLTC_MMR6:
1927     case Mips::BGEUC:   case Mips::BGEUC_MMR6:
1928     case Mips::BLTUC:   case Mips::BLTUC_MMR6:
1929     case Mips::BEQC:    case Mips::BEQC_MMR6:
1930     case Mips::BNEC:    case Mips::BNEC_MMR6:
1931       assert(MCID.getNumOperands() == 3 && "unexpected number of operands");
1932       Offset = Inst.getOperand(2);
1933       if (!Offset.isImm())
1934         break; // We'll deal with this situation later on when applying fixups.
1935       if (!isIntN(18, Offset.getImm()))
1936         return Error(IDLoc, "branch target out of range");
1937       if (offsetToAlignment(Offset.getImm(), Align(4)))
1938         return Error(IDLoc, "branch to misaligned address");
1939       break;
1940     case Mips::BLEZC:   case Mips::BLEZC_MMR6:
1941     case Mips::BGEZC:   case Mips::BGEZC_MMR6:
1942     case Mips::BGTZC:   case Mips::BGTZC_MMR6:
1943     case Mips::BLTZC:   case Mips::BLTZC_MMR6:
1944       assert(MCID.getNumOperands() == 2 && "unexpected number of operands");
1945       Offset = Inst.getOperand(1);
1946       if (!Offset.isImm())
1947         break; // We'll deal with this situation later on when applying fixups.
1948       if (!isIntN(18, Offset.getImm()))
1949         return Error(IDLoc, "branch target out of range");
1950       if (offsetToAlignment(Offset.getImm(), Align(4)))
1951         return Error(IDLoc, "branch to misaligned address");
1952       break;
1953     case Mips::BEQZC:   case Mips::BEQZC_MMR6:
1954     case Mips::BNEZC:   case Mips::BNEZC_MMR6:
1955       assert(MCID.getNumOperands() == 2 && "unexpected number of operands");
1956       Offset = Inst.getOperand(1);
1957       if (!Offset.isImm())
1958         break; // We'll deal with this situation later on when applying fixups.
1959       if (!isIntN(23, Offset.getImm()))
1960         return Error(IDLoc, "branch target out of range");
1961       if (offsetToAlignment(Offset.getImm(), Align(4)))
1962         return Error(IDLoc, "branch to misaligned address");
1963       break;
1964     case Mips::BEQZ16_MM:
1965     case Mips::BEQZC16_MMR6:
1966     case Mips::BNEZ16_MM:
1967     case Mips::BNEZC16_MMR6:
1968       assert(MCID.getNumOperands() == 2 && "unexpected number of operands");
1969       Offset = Inst.getOperand(1);
1970       if (!Offset.isImm())
1971         break; // We'll deal with this situation later on when applying fixups.
1972       if (!isInt<8>(Offset.getImm()))
1973         return Error(IDLoc, "branch target out of range");
1974       if (offsetToAlignment(Offset.getImm(), Align(2)))
1975         return Error(IDLoc, "branch to misaligned address");
1976       break;
1977     }
1978   }
1979 
1980   // SSNOP is deprecated on MIPS32r6/MIPS64r6
1981   // We still accept it but it is a normal nop.
1982   if (hasMips32r6() && Opcode == Mips::SSNOP) {
1983     std::string ISA = hasMips64r6() ? "MIPS64r6" : "MIPS32r6";
1984     Warning(IDLoc, "ssnop is deprecated for " + ISA + " and is equivalent to a "
1985                                                       "nop instruction");
1986   }
1987 
1988   if (hasCnMips()) {
1989     MCOperand Opnd;
1990     int Imm;
1991 
1992     switch (Opcode) {
1993       default:
1994         break;
1995 
1996       case Mips::BBIT0:
1997       case Mips::BBIT032:
1998       case Mips::BBIT1:
1999       case Mips::BBIT132:
2000         assert(MCID.getNumOperands() == 3 && "unexpected number of operands");
2001         // The offset is handled above
2002         Opnd = Inst.getOperand(1);
2003         if (!Opnd.isImm())
2004           return Error(IDLoc, "expected immediate operand kind");
2005         Imm = Opnd.getImm();
2006         if (Imm < 0 || Imm > (Opcode == Mips::BBIT0 ||
2007                               Opcode == Mips::BBIT1 ? 63 : 31))
2008           return Error(IDLoc, "immediate operand value out of range");
2009         if (Imm > 31) {
2010           Inst.setOpcode(Opcode == Mips::BBIT0 ? Mips::BBIT032
2011                                                : Mips::BBIT132);
2012           Inst.getOperand(1).setImm(Imm - 32);
2013         }
2014         break;
2015 
2016       case Mips::SEQi:
2017       case Mips::SNEi:
2018         assert(MCID.getNumOperands() == 3 && "unexpected number of operands");
2019         Opnd = Inst.getOperand(2);
2020         if (!Opnd.isImm())
2021           return Error(IDLoc, "expected immediate operand kind");
2022         Imm = Opnd.getImm();
2023         if (!isInt<10>(Imm))
2024           return Error(IDLoc, "immediate operand value out of range");
2025         break;
2026     }
2027   }
2028 
2029   // Warn on division by zero. We're checking here as all instructions get
2030   // processed here, not just the macros that need expansion.
2031   //
2032   // The MIPS backend models most of the divison instructions and macros as
2033   // three operand instructions. The pre-R6 divide instructions however have
2034   // two operands and explicitly define HI/LO as part of the instruction,
2035   // not in the operands.
2036   unsigned FirstOp = 1;
2037   unsigned SecondOp = 2;
2038   switch (Opcode) {
2039   default:
2040     break;
2041   case Mips::SDivIMacro:
2042   case Mips::UDivIMacro:
2043   case Mips::DSDivIMacro:
2044   case Mips::DUDivIMacro:
2045     if (Inst.getOperand(2).getImm() == 0) {
2046       if (Inst.getOperand(1).getReg() == Mips::ZERO ||
2047           Inst.getOperand(1).getReg() == Mips::ZERO_64)
2048         Warning(IDLoc, "dividing zero by zero");
2049       else
2050         Warning(IDLoc, "division by zero");
2051     }
2052     break;
2053   case Mips::DSDIV:
2054   case Mips::SDIV:
2055   case Mips::UDIV:
2056   case Mips::DUDIV:
2057   case Mips::UDIV_MM:
2058   case Mips::SDIV_MM:
2059     FirstOp = 0;
2060     SecondOp = 1;
2061     LLVM_FALLTHROUGH;
2062   case Mips::SDivMacro:
2063   case Mips::DSDivMacro:
2064   case Mips::UDivMacro:
2065   case Mips::DUDivMacro:
2066   case Mips::DIV:
2067   case Mips::DIVU:
2068   case Mips::DDIV:
2069   case Mips::DDIVU:
2070   case Mips::DIVU_MMR6:
2071   case Mips::DIV_MMR6:
2072     if (Inst.getOperand(SecondOp).getReg() == Mips::ZERO ||
2073         Inst.getOperand(SecondOp).getReg() == Mips::ZERO_64) {
2074       if (Inst.getOperand(FirstOp).getReg() == Mips::ZERO ||
2075           Inst.getOperand(FirstOp).getReg() == Mips::ZERO_64)
2076         Warning(IDLoc, "dividing zero by zero");
2077       else
2078         Warning(IDLoc, "division by zero");
2079     }
2080     break;
2081   }
2082 
2083   // For PIC code convert unconditional jump to unconditional branch.
2084   if ((Opcode == Mips::J || Opcode == Mips::J_MM) && inPicMode()) {
2085     MCInst BInst;
2086     BInst.setOpcode(inMicroMipsMode() ? Mips::BEQ_MM : Mips::BEQ);
2087     BInst.addOperand(MCOperand::createReg(Mips::ZERO));
2088     BInst.addOperand(MCOperand::createReg(Mips::ZERO));
2089     BInst.addOperand(Inst.getOperand(0));
2090     Inst = BInst;
2091   }
2092 
2093   // This expansion is not in a function called by tryExpandInstruction()
2094   // because the pseudo-instruction doesn't have a distinct opcode.
2095   if ((Opcode == Mips::JAL || Opcode == Mips::JAL_MM) && inPicMode()) {
2096     warnIfNoMacro(IDLoc);
2097 
2098     const MCExpr *JalExpr = Inst.getOperand(0).getExpr();
2099 
2100     // We can do this expansion if there's only 1 symbol in the argument
2101     // expression.
2102     if (countMCSymbolRefExpr(JalExpr) > 1)
2103       return Error(IDLoc, "jal doesn't support multiple symbols in PIC mode");
2104 
2105     // FIXME: This is checking the expression can be handled by the later stages
2106     //        of the assembler. We ought to leave it to those later stages.
2107     const MCSymbol *JalSym = getSingleMCSymbol(JalExpr);
2108 
2109     if (expandLoadAddress(Mips::T9, Mips::NoRegister, Inst.getOperand(0),
2110                           !isGP64bit(), IDLoc, Out, STI))
2111       return true;
2112 
2113     MCInst JalrInst;
2114     if (inMicroMipsMode())
2115       JalrInst.setOpcode(IsCpRestoreSet ? Mips::JALRS_MM : Mips::JALR_MM);
2116     else
2117       JalrInst.setOpcode(Mips::JALR);
2118     JalrInst.addOperand(MCOperand::createReg(Mips::RA));
2119     JalrInst.addOperand(MCOperand::createReg(Mips::T9));
2120 
2121     if (isJalrRelocAvailable(JalExpr)) {
2122       // As an optimization hint for the linker, before the JALR we add:
2123       // .reloc tmplabel, R_{MICRO}MIPS_JALR, symbol
2124       // tmplabel:
2125       MCSymbol *TmpLabel = getContext().createTempSymbol();
2126       const MCExpr *TmpExpr = MCSymbolRefExpr::create(TmpLabel, getContext());
2127       const MCExpr *RelocJalrExpr =
2128           MCSymbolRefExpr::create(JalSym, MCSymbolRefExpr::VK_None,
2129                                   getContext(), IDLoc);
2130 
2131       TOut.getStreamer().emitRelocDirective(
2132           *TmpExpr, inMicroMipsMode() ? "R_MICROMIPS_JALR" : "R_MIPS_JALR",
2133           RelocJalrExpr, IDLoc, *STI);
2134       TOut.getStreamer().emitLabel(TmpLabel);
2135     }
2136 
2137     Inst = JalrInst;
2138     ExpandedJalSym = true;
2139   }
2140 
2141   if (MCID.mayLoad() || MCID.mayStore()) {
2142     // Check the offset of memory operand, if it is a symbol
2143     // reference or immediate we may have to expand instructions.
2144     if (needsExpandMemInst(Inst)) {
2145       const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
2146       switch (MCID.OpInfo[MCID.getNumOperands() - 1].OperandType) {
2147       case MipsII::OPERAND_MEM_SIMM9:
2148         expandMem9Inst(Inst, IDLoc, Out, STI, MCID.mayLoad());
2149         break;
2150       default:
2151         expandMem16Inst(Inst, IDLoc, Out, STI, MCID.mayLoad());
2152         break;
2153       }
2154       return getParser().hasPendingError();
2155     }
2156   }
2157 
2158   if (inMicroMipsMode()) {
2159     if (MCID.mayLoad() && Opcode != Mips::LWP_MM) {
2160       // Try to create 16-bit GP relative load instruction.
2161       for (unsigned i = 0; i < MCID.getNumOperands(); i++) {
2162         const MCOperandInfo &OpInfo = MCID.OpInfo[i];
2163         if ((OpInfo.OperandType == MCOI::OPERAND_MEMORY) ||
2164             (OpInfo.OperandType == MCOI::OPERAND_UNKNOWN)) {
2165           MCOperand &Op = Inst.getOperand(i);
2166           if (Op.isImm()) {
2167             int MemOffset = Op.getImm();
2168             MCOperand &DstReg = Inst.getOperand(0);
2169             MCOperand &BaseReg = Inst.getOperand(1);
2170             if (isInt<9>(MemOffset) && (MemOffset % 4 == 0) &&
2171                 getContext().getRegisterInfo()->getRegClass(
2172                   Mips::GPRMM16RegClassID).contains(DstReg.getReg()) &&
2173                 (BaseReg.getReg() == Mips::GP ||
2174                 BaseReg.getReg() == Mips::GP_64)) {
2175 
2176               TOut.emitRRI(Mips::LWGP_MM, DstReg.getReg(), Mips::GP, MemOffset,
2177                            IDLoc, STI);
2178               return false;
2179             }
2180           }
2181         }
2182       } // for
2183     }   // if load
2184 
2185     // TODO: Handle this with the AsmOperandClass.PredicateMethod.
2186 
2187     MCOperand Opnd;
2188     int Imm;
2189 
2190     switch (Opcode) {
2191       default:
2192         break;
2193       case Mips::ADDIUSP_MM:
2194         Opnd = Inst.getOperand(0);
2195         if (!Opnd.isImm())
2196           return Error(IDLoc, "expected immediate operand kind");
2197         Imm = Opnd.getImm();
2198         if (Imm < -1032 || Imm > 1028 || (Imm < 8 && Imm > -12) ||
2199             Imm % 4 != 0)
2200           return Error(IDLoc, "immediate operand value out of range");
2201         break;
2202       case Mips::SLL16_MM:
2203       case Mips::SRL16_MM:
2204         Opnd = Inst.getOperand(2);
2205         if (!Opnd.isImm())
2206           return Error(IDLoc, "expected immediate operand kind");
2207         Imm = Opnd.getImm();
2208         if (Imm < 1 || Imm > 8)
2209           return Error(IDLoc, "immediate operand value out of range");
2210         break;
2211       case Mips::LI16_MM:
2212         Opnd = Inst.getOperand(1);
2213         if (!Opnd.isImm())
2214           return Error(IDLoc, "expected immediate operand kind");
2215         Imm = Opnd.getImm();
2216         if (Imm < -1 || Imm > 126)
2217           return Error(IDLoc, "immediate operand value out of range");
2218         break;
2219       case Mips::ADDIUR2_MM:
2220         Opnd = Inst.getOperand(2);
2221         if (!Opnd.isImm())
2222           return Error(IDLoc, "expected immediate operand kind");
2223         Imm = Opnd.getImm();
2224         if (!(Imm == 1 || Imm == -1 ||
2225               ((Imm % 4 == 0) && Imm < 28 && Imm > 0)))
2226           return Error(IDLoc, "immediate operand value out of range");
2227         break;
2228       case Mips::ANDI16_MM:
2229         Opnd = Inst.getOperand(2);
2230         if (!Opnd.isImm())
2231           return Error(IDLoc, "expected immediate operand kind");
2232         Imm = Opnd.getImm();
2233         if (!(Imm == 128 || (Imm >= 1 && Imm <= 4) || Imm == 7 || Imm == 8 ||
2234               Imm == 15 || Imm == 16 || Imm == 31 || Imm == 32 || Imm == 63 ||
2235               Imm == 64 || Imm == 255 || Imm == 32768 || Imm == 65535))
2236           return Error(IDLoc, "immediate operand value out of range");
2237         break;
2238       case Mips::LBU16_MM:
2239         Opnd = Inst.getOperand(2);
2240         if (!Opnd.isImm())
2241           return Error(IDLoc, "expected immediate operand kind");
2242         Imm = Opnd.getImm();
2243         if (Imm < -1 || Imm > 14)
2244           return Error(IDLoc, "immediate operand value out of range");
2245         break;
2246       case Mips::SB16_MM:
2247       case Mips::SB16_MMR6:
2248         Opnd = Inst.getOperand(2);
2249         if (!Opnd.isImm())
2250           return Error(IDLoc, "expected immediate operand kind");
2251         Imm = Opnd.getImm();
2252         if (Imm < 0 || Imm > 15)
2253           return Error(IDLoc, "immediate operand value out of range");
2254         break;
2255       case Mips::LHU16_MM:
2256       case Mips::SH16_MM:
2257       case Mips::SH16_MMR6:
2258         Opnd = Inst.getOperand(2);
2259         if (!Opnd.isImm())
2260           return Error(IDLoc, "expected immediate operand kind");
2261         Imm = Opnd.getImm();
2262         if (Imm < 0 || Imm > 30 || (Imm % 2 != 0))
2263           return Error(IDLoc, "immediate operand value out of range");
2264         break;
2265       case Mips::LW16_MM:
2266       case Mips::SW16_MM:
2267       case Mips::SW16_MMR6:
2268         Opnd = Inst.getOperand(2);
2269         if (!Opnd.isImm())
2270           return Error(IDLoc, "expected immediate operand kind");
2271         Imm = Opnd.getImm();
2272         if (Imm < 0 || Imm > 60 || (Imm % 4 != 0))
2273           return Error(IDLoc, "immediate operand value out of range");
2274         break;
2275       case Mips::ADDIUPC_MM:
2276         Opnd = Inst.getOperand(1);
2277         if (!Opnd.isImm())
2278           return Error(IDLoc, "expected immediate operand kind");
2279         Imm = Opnd.getImm();
2280         if ((Imm % 4 != 0) || !isInt<25>(Imm))
2281           return Error(IDLoc, "immediate operand value out of range");
2282         break;
2283       case Mips::LWP_MM:
2284       case Mips::SWP_MM:
2285         if (Inst.getOperand(0).getReg() == Mips::RA)
2286           return Error(IDLoc, "invalid operand for instruction");
2287         break;
2288       case Mips::MOVEP_MM:
2289       case Mips::MOVEP_MMR6: {
2290         unsigned R0 = Inst.getOperand(0).getReg();
2291         unsigned R1 = Inst.getOperand(1).getReg();
2292         bool RegPair = ((R0 == Mips::A1 && R1 == Mips::A2) ||
2293                         (R0 == Mips::A1 && R1 == Mips::A3) ||
2294                         (R0 == Mips::A2 && R1 == Mips::A3) ||
2295                         (R0 == Mips::A0 && R1 == Mips::S5) ||
2296                         (R0 == Mips::A0 && R1 == Mips::S6) ||
2297                         (R0 == Mips::A0 && R1 == Mips::A1) ||
2298                         (R0 == Mips::A0 && R1 == Mips::A2) ||
2299                         (R0 == Mips::A0 && R1 == Mips::A3));
2300         if (!RegPair)
2301           return Error(IDLoc, "invalid operand for instruction");
2302         break;
2303       }
2304     }
2305   }
2306 
2307   bool FillDelaySlot =
2308       MCID.hasDelaySlot() && AssemblerOptions.back()->isReorder();
2309   if (FillDelaySlot)
2310     TOut.emitDirectiveSetNoReorder();
2311 
2312   MacroExpanderResultTy ExpandResult =
2313       tryExpandInstruction(Inst, IDLoc, Out, STI);
2314   switch (ExpandResult) {
2315   case MER_NotAMacro:
2316     Out.emitInstruction(Inst, *STI);
2317     break;
2318   case MER_Success:
2319     break;
2320   case MER_Fail:
2321     return true;
2322   }
2323 
2324   // We know we emitted an instruction on the MER_NotAMacro or MER_Success path.
2325   // If we're in microMIPS mode then we must also set EF_MIPS_MICROMIPS.
2326   if (inMicroMipsMode()) {
2327     TOut.setUsesMicroMips();
2328     TOut.updateABIInfo(*this);
2329   }
2330 
2331   // If this instruction has a delay slot and .set reorder is active,
2332   // emit a NOP after it.
2333   if (FillDelaySlot) {
2334     TOut.emitEmptyDelaySlot(hasShortDelaySlot(Inst), IDLoc, STI);
2335     TOut.emitDirectiveSetReorder();
2336   }
2337 
2338   if ((Opcode == Mips::JalOneReg || Opcode == Mips::JalTwoReg ||
2339        ExpandedJalSym) &&
2340       isPicAndNotNxxAbi()) {
2341     if (IsCpRestoreSet) {
2342       // We need a NOP between the JALR and the LW:
2343       // If .set reorder has been used, we've already emitted a NOP.
2344       // If .set noreorder has been used, we need to emit a NOP at this point.
2345       if (!AssemblerOptions.back()->isReorder())
2346         TOut.emitEmptyDelaySlot(hasShortDelaySlot(Inst), IDLoc,
2347                                 STI);
2348 
2349       // Load the $gp from the stack.
2350       TOut.emitGPRestore(CpRestoreOffset, IDLoc, STI);
2351     } else
2352       Warning(IDLoc, "no .cprestore used in PIC mode");
2353   }
2354 
2355   return false;
2356 }
2357 
2358 MipsAsmParser::MacroExpanderResultTy
2359 MipsAsmParser::tryExpandInstruction(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
2360                                     const MCSubtargetInfo *STI) {
2361   switch (Inst.getOpcode()) {
2362   default:
2363     return MER_NotAMacro;
2364   case Mips::LoadImm32:
2365     return expandLoadImm(Inst, true, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2366   case Mips::LoadImm64:
2367     return expandLoadImm(Inst, false, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2368   case Mips::LoadAddrImm32:
2369   case Mips::LoadAddrImm64:
2370     assert(Inst.getOperand(0).isReg() && "expected register operand kind");
2371     assert((Inst.getOperand(1).isImm() || Inst.getOperand(1).isExpr()) &&
2372            "expected immediate operand kind");
2373 
2374     return expandLoadAddress(Inst.getOperand(0).getReg(), Mips::NoRegister,
2375                              Inst.getOperand(1),
2376                              Inst.getOpcode() == Mips::LoadAddrImm32, IDLoc,
2377                              Out, STI)
2378                ? MER_Fail
2379                : MER_Success;
2380   case Mips::LoadAddrReg32:
2381   case Mips::LoadAddrReg64:
2382     assert(Inst.getOperand(0).isReg() && "expected register operand kind");
2383     assert(Inst.getOperand(1).isReg() && "expected register operand kind");
2384     assert((Inst.getOperand(2).isImm() || Inst.getOperand(2).isExpr()) &&
2385            "expected immediate operand kind");
2386 
2387     return expandLoadAddress(Inst.getOperand(0).getReg(),
2388                              Inst.getOperand(1).getReg(), Inst.getOperand(2),
2389                              Inst.getOpcode() == Mips::LoadAddrReg32, IDLoc,
2390                              Out, STI)
2391                ? MER_Fail
2392                : MER_Success;
2393   case Mips::B_MM_Pseudo:
2394   case Mips::B_MMR6_Pseudo:
2395     return expandUncondBranchMMPseudo(Inst, IDLoc, Out, STI) ? MER_Fail
2396                                                              : MER_Success;
2397   case Mips::SWM_MM:
2398   case Mips::LWM_MM:
2399     return expandLoadStoreMultiple(Inst, IDLoc, Out, STI) ? MER_Fail
2400                                                           : MER_Success;
2401   case Mips::JalOneReg:
2402   case Mips::JalTwoReg:
2403     return expandJalWithRegs(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2404   case Mips::BneImm:
2405   case Mips::BeqImm:
2406   case Mips::BEQLImmMacro:
2407   case Mips::BNELImmMacro:
2408     return expandBranchImm(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2409   case Mips::BLT:
2410   case Mips::BLE:
2411   case Mips::BGE:
2412   case Mips::BGT:
2413   case Mips::BLTU:
2414   case Mips::BLEU:
2415   case Mips::BGEU:
2416   case Mips::BGTU:
2417   case Mips::BLTL:
2418   case Mips::BLEL:
2419   case Mips::BGEL:
2420   case Mips::BGTL:
2421   case Mips::BLTUL:
2422   case Mips::BLEUL:
2423   case Mips::BGEUL:
2424   case Mips::BGTUL:
2425   case Mips::BLTImmMacro:
2426   case Mips::BLEImmMacro:
2427   case Mips::BGEImmMacro:
2428   case Mips::BGTImmMacro:
2429   case Mips::BLTUImmMacro:
2430   case Mips::BLEUImmMacro:
2431   case Mips::BGEUImmMacro:
2432   case Mips::BGTUImmMacro:
2433   case Mips::BLTLImmMacro:
2434   case Mips::BLELImmMacro:
2435   case Mips::BGELImmMacro:
2436   case Mips::BGTLImmMacro:
2437   case Mips::BLTULImmMacro:
2438   case Mips::BLEULImmMacro:
2439   case Mips::BGEULImmMacro:
2440   case Mips::BGTULImmMacro:
2441     return expandCondBranches(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2442   case Mips::SDivMacro:
2443   case Mips::SDivIMacro:
2444   case Mips::SRemMacro:
2445   case Mips::SRemIMacro:
2446     return expandDivRem(Inst, IDLoc, Out, STI, false, true) ? MER_Fail
2447                                                             : MER_Success;
2448   case Mips::DSDivMacro:
2449   case Mips::DSDivIMacro:
2450   case Mips::DSRemMacro:
2451   case Mips::DSRemIMacro:
2452     return expandDivRem(Inst, IDLoc, Out, STI, true, true) ? MER_Fail
2453                                                            : MER_Success;
2454   case Mips::UDivMacro:
2455   case Mips::UDivIMacro:
2456   case Mips::URemMacro:
2457   case Mips::URemIMacro:
2458     return expandDivRem(Inst, IDLoc, Out, STI, false, false) ? MER_Fail
2459                                                              : MER_Success;
2460   case Mips::DUDivMacro:
2461   case Mips::DUDivIMacro:
2462   case Mips::DURemMacro:
2463   case Mips::DURemIMacro:
2464     return expandDivRem(Inst, IDLoc, Out, STI, true, false) ? MER_Fail
2465                                                             : MER_Success;
2466   case Mips::PseudoTRUNC_W_S:
2467     return expandTrunc(Inst, false, false, IDLoc, Out, STI) ? MER_Fail
2468                                                             : MER_Success;
2469   case Mips::PseudoTRUNC_W_D32:
2470     return expandTrunc(Inst, true, false, IDLoc, Out, STI) ? MER_Fail
2471                                                            : MER_Success;
2472   case Mips::PseudoTRUNC_W_D:
2473     return expandTrunc(Inst, true, true, IDLoc, Out, STI) ? MER_Fail
2474                                                           : MER_Success;
2475 
2476   case Mips::LoadImmSingleGPR:
2477     return expandLoadSingleImmToGPR(Inst, IDLoc, Out, STI) ? MER_Fail
2478                                                            : MER_Success;
2479   case Mips::LoadImmSingleFGR:
2480     return expandLoadSingleImmToFPR(Inst, IDLoc, Out, STI) ? MER_Fail
2481                                                            : MER_Success;
2482   case Mips::LoadImmDoubleGPR:
2483     return expandLoadDoubleImmToGPR(Inst, IDLoc, Out, STI) ? MER_Fail
2484                                                            : MER_Success;
2485   case Mips::LoadImmDoubleFGR:
2486     return expandLoadDoubleImmToFPR(Inst, true, IDLoc, Out, STI) ? MER_Fail
2487                                                                  : MER_Success;
2488   case Mips::LoadImmDoubleFGR_32:
2489     return expandLoadDoubleImmToFPR(Inst, false, IDLoc, Out, STI) ? MER_Fail
2490                                                                   : MER_Success;
2491 
2492   case Mips::Ulh:
2493     return expandUlh(Inst, true, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2494   case Mips::Ulhu:
2495     return expandUlh(Inst, false, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2496   case Mips::Ush:
2497     return expandUsh(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2498   case Mips::Ulw:
2499   case Mips::Usw:
2500     return expandUxw(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2501   case Mips::NORImm:
2502   case Mips::NORImm64:
2503     return expandAliasImmediate(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2504   case Mips::SGE:
2505   case Mips::SGEU:
2506     return expandSge(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2507   case Mips::SGEImm:
2508   case Mips::SGEUImm:
2509   case Mips::SGEImm64:
2510   case Mips::SGEUImm64:
2511     return expandSgeImm(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2512   case Mips::SGTImm:
2513   case Mips::SGTUImm:
2514   case Mips::SGTImm64:
2515   case Mips::SGTUImm64:
2516     return expandSgtImm(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2517   case Mips::SLTImm64:
2518     if (isInt<16>(Inst.getOperand(2).getImm())) {
2519       Inst.setOpcode(Mips::SLTi64);
2520       return MER_NotAMacro;
2521     }
2522     return expandAliasImmediate(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2523   case Mips::SLTUImm64:
2524     if (isInt<16>(Inst.getOperand(2).getImm())) {
2525       Inst.setOpcode(Mips::SLTiu64);
2526       return MER_NotAMacro;
2527     }
2528     return expandAliasImmediate(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2529   case Mips::ADDi:   case Mips::ADDi_MM:
2530   case Mips::ADDiu:  case Mips::ADDiu_MM:
2531   case Mips::SLTi:   case Mips::SLTi_MM:
2532   case Mips::SLTiu:  case Mips::SLTiu_MM:
2533     if ((Inst.getNumOperands() == 3) && Inst.getOperand(0).isReg() &&
2534         Inst.getOperand(1).isReg() && Inst.getOperand(2).isImm()) {
2535       int64_t ImmValue = Inst.getOperand(2).getImm();
2536       if (isInt<16>(ImmValue))
2537         return MER_NotAMacro;
2538       return expandAliasImmediate(Inst, IDLoc, Out, STI) ? MER_Fail
2539                                                          : MER_Success;
2540     }
2541     return MER_NotAMacro;
2542   case Mips::ANDi:  case Mips::ANDi_MM:  case Mips::ANDi64:
2543   case Mips::ORi:   case Mips::ORi_MM:   case Mips::ORi64:
2544   case Mips::XORi:  case Mips::XORi_MM:  case Mips::XORi64:
2545     if ((Inst.getNumOperands() == 3) && Inst.getOperand(0).isReg() &&
2546         Inst.getOperand(1).isReg() && Inst.getOperand(2).isImm()) {
2547       int64_t ImmValue = Inst.getOperand(2).getImm();
2548       if (isUInt<16>(ImmValue))
2549         return MER_NotAMacro;
2550       return expandAliasImmediate(Inst, IDLoc, Out, STI) ? MER_Fail
2551                                                          : MER_Success;
2552     }
2553     return MER_NotAMacro;
2554   case Mips::ROL:
2555   case Mips::ROR:
2556     return expandRotation(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2557   case Mips::ROLImm:
2558   case Mips::RORImm:
2559     return expandRotationImm(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2560   case Mips::DROL:
2561   case Mips::DROR:
2562     return expandDRotation(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2563   case Mips::DROLImm:
2564   case Mips::DRORImm:
2565     return expandDRotationImm(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2566   case Mips::ABSMacro:
2567     return expandAbs(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2568   case Mips::MULImmMacro:
2569   case Mips::DMULImmMacro:
2570     return expandMulImm(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2571   case Mips::MULOMacro:
2572   case Mips::DMULOMacro:
2573     return expandMulO(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2574   case Mips::MULOUMacro:
2575   case Mips::DMULOUMacro:
2576     return expandMulOU(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2577   case Mips::DMULMacro:
2578     return expandDMULMacro(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2579   case Mips::LDMacro:
2580   case Mips::SDMacro:
2581     return expandLoadStoreDMacro(Inst, IDLoc, Out, STI,
2582                                  Inst.getOpcode() == Mips::LDMacro)
2583                ? MER_Fail
2584                : MER_Success;
2585   case Mips::SDC1_M1:
2586     return expandStoreDM1Macro(Inst, IDLoc, Out, STI)
2587                ? MER_Fail
2588                : MER_Success;
2589   case Mips::SEQMacro:
2590     return expandSeq(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2591   case Mips::SEQIMacro:
2592     return expandSeqI(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2593   case Mips::MFTC0:   case Mips::MTTC0:
2594   case Mips::MFTGPR:  case Mips::MTTGPR:
2595   case Mips::MFTLO:   case Mips::MTTLO:
2596   case Mips::MFTHI:   case Mips::MTTHI:
2597   case Mips::MFTACX:  case Mips::MTTACX:
2598   case Mips::MFTDSP:  case Mips::MTTDSP:
2599   case Mips::MFTC1:   case Mips::MTTC1:
2600   case Mips::MFTHC1:  case Mips::MTTHC1:
2601   case Mips::CFTC1:   case Mips::CTTC1:
2602     return expandMXTRAlias(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2603   case Mips::SaaAddr:
2604   case Mips::SaadAddr:
2605     return expandSaaAddr(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success;
2606   }
2607 }
2608 
2609 bool MipsAsmParser::expandJalWithRegs(MCInst &Inst, SMLoc IDLoc,
2610                                       MCStreamer &Out,
2611                                       const MCSubtargetInfo *STI) {
2612   MipsTargetStreamer &TOut = getTargetStreamer();
2613 
2614   // Create a JALR instruction which is going to replace the pseudo-JAL.
2615   MCInst JalrInst;
2616   JalrInst.setLoc(IDLoc);
2617   const MCOperand FirstRegOp = Inst.getOperand(0);
2618   const unsigned Opcode = Inst.getOpcode();
2619 
2620   if (Opcode == Mips::JalOneReg) {
2621     // jal $rs => jalr $rs
2622     if (IsCpRestoreSet && inMicroMipsMode()) {
2623       JalrInst.setOpcode(Mips::JALRS16_MM);
2624       JalrInst.addOperand(FirstRegOp);
2625     } else if (inMicroMipsMode()) {
2626       JalrInst.setOpcode(hasMips32r6() ? Mips::JALRC16_MMR6 : Mips::JALR16_MM);
2627       JalrInst.addOperand(FirstRegOp);
2628     } else {
2629       JalrInst.setOpcode(Mips::JALR);
2630       JalrInst.addOperand(MCOperand::createReg(Mips::RA));
2631       JalrInst.addOperand(FirstRegOp);
2632     }
2633   } else if (Opcode == Mips::JalTwoReg) {
2634     // jal $rd, $rs => jalr $rd, $rs
2635     if (IsCpRestoreSet && inMicroMipsMode())
2636       JalrInst.setOpcode(Mips::JALRS_MM);
2637     else
2638       JalrInst.setOpcode(inMicroMipsMode() ? Mips::JALR_MM : Mips::JALR);
2639     JalrInst.addOperand(FirstRegOp);
2640     const MCOperand SecondRegOp = Inst.getOperand(1);
2641     JalrInst.addOperand(SecondRegOp);
2642   }
2643   Out.emitInstruction(JalrInst, *STI);
2644 
2645   // If .set reorder is active and branch instruction has a delay slot,
2646   // emit a NOP after it.
2647   const MCInstrDesc &MCID = getInstDesc(JalrInst.getOpcode());
2648   if (MCID.hasDelaySlot() && AssemblerOptions.back()->isReorder())
2649     TOut.emitEmptyDelaySlot(hasShortDelaySlot(JalrInst), IDLoc,
2650                             STI);
2651 
2652   return false;
2653 }
2654 
2655 /// Can the value be represented by a unsigned N-bit value and a shift left?
2656 template <unsigned N> static bool isShiftedUIntAtAnyPosition(uint64_t x) {
2657   unsigned BitNum = findFirstSet(x);
2658 
2659   return (x == x >> BitNum << BitNum) && isUInt<N>(x >> BitNum);
2660 }
2661 
2662 /// Load (or add) an immediate into a register.
2663 ///
2664 /// @param ImmValue     The immediate to load.
2665 /// @param DstReg       The register that will hold the immediate.
2666 /// @param SrcReg       A register to add to the immediate or Mips::NoRegister
2667 ///                     for a simple initialization.
2668 /// @param Is32BitImm   Is ImmValue 32-bit or 64-bit?
2669 /// @param IsAddress    True if the immediate represents an address. False if it
2670 ///                     is an integer.
2671 /// @param IDLoc        Location of the immediate in the source file.
2672 bool MipsAsmParser::loadImmediate(int64_t ImmValue, unsigned DstReg,
2673                                   unsigned SrcReg, bool Is32BitImm,
2674                                   bool IsAddress, SMLoc IDLoc, MCStreamer &Out,
2675                                   const MCSubtargetInfo *STI) {
2676   MipsTargetStreamer &TOut = getTargetStreamer();
2677 
2678   if (!Is32BitImm && !isGP64bit()) {
2679     Error(IDLoc, "instruction requires a 64-bit architecture");
2680     return true;
2681   }
2682 
2683   if (Is32BitImm) {
2684     if (isInt<32>(ImmValue) || isUInt<32>(ImmValue)) {
2685       // Sign extend up to 64-bit so that the predicates match the hardware
2686       // behaviour. In particular, isInt<16>(0xffff8000) and similar should be
2687       // true.
2688       ImmValue = SignExtend64<32>(ImmValue);
2689     } else {
2690       Error(IDLoc, "instruction requires a 32-bit immediate");
2691       return true;
2692     }
2693   }
2694 
2695   unsigned ZeroReg = IsAddress ? ABI.GetNullPtr() : ABI.GetZeroReg();
2696   unsigned AdduOp = !Is32BitImm ? Mips::DADDu : Mips::ADDu;
2697 
2698   bool UseSrcReg = false;
2699   if (SrcReg != Mips::NoRegister)
2700     UseSrcReg = true;
2701 
2702   unsigned TmpReg = DstReg;
2703   if (UseSrcReg &&
2704       getContext().getRegisterInfo()->isSuperOrSubRegisterEq(DstReg, SrcReg)) {
2705     // At this point we need AT to perform the expansions and we exit if it is
2706     // not available.
2707     unsigned ATReg = getATReg(IDLoc);
2708     if (!ATReg)
2709       return true;
2710     TmpReg = ATReg;
2711   }
2712 
2713   if (isInt<16>(ImmValue)) {
2714     if (!UseSrcReg)
2715       SrcReg = ZeroReg;
2716 
2717     // This doesn't quite follow the usual ABI expectations for N32 but matches
2718     // traditional assembler behaviour. N32 would normally use addiu for both
2719     // integers and addresses.
2720     if (IsAddress && !Is32BitImm) {
2721       TOut.emitRRI(Mips::DADDiu, DstReg, SrcReg, ImmValue, IDLoc, STI);
2722       return false;
2723     }
2724 
2725     TOut.emitRRI(Mips::ADDiu, DstReg, SrcReg, ImmValue, IDLoc, STI);
2726     return false;
2727   }
2728 
2729   if (isUInt<16>(ImmValue)) {
2730     unsigned TmpReg = DstReg;
2731     if (SrcReg == DstReg) {
2732       TmpReg = getATReg(IDLoc);
2733       if (!TmpReg)
2734         return true;
2735     }
2736 
2737     TOut.emitRRI(Mips::ORi, TmpReg, ZeroReg, ImmValue, IDLoc, STI);
2738     if (UseSrcReg)
2739       TOut.emitRRR(ABI.GetPtrAdduOp(), DstReg, TmpReg, SrcReg, IDLoc, STI);
2740     return false;
2741   }
2742 
2743   if (isInt<32>(ImmValue) || isUInt<32>(ImmValue)) {
2744     warnIfNoMacro(IDLoc);
2745 
2746     uint16_t Bits31To16 = (ImmValue >> 16) & 0xffff;
2747     uint16_t Bits15To0 = ImmValue & 0xffff;
2748     if (!Is32BitImm && !isInt<32>(ImmValue)) {
2749       // Traditional behaviour seems to special case this particular value. It's
2750       // not clear why other masks are handled differently.
2751       if (ImmValue == 0xffffffff) {
2752         TOut.emitRI(Mips::LUi, TmpReg, 0xffff, IDLoc, STI);
2753         TOut.emitRRI(Mips::DSRL32, TmpReg, TmpReg, 0, IDLoc, STI);
2754         if (UseSrcReg)
2755           TOut.emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, STI);
2756         return false;
2757       }
2758 
2759       // Expand to an ORi instead of a LUi to avoid sign-extending into the
2760       // upper 32 bits.
2761       TOut.emitRRI(Mips::ORi, TmpReg, ZeroReg, Bits31To16, IDLoc, STI);
2762       TOut.emitRRI(Mips::DSLL, TmpReg, TmpReg, 16, IDLoc, STI);
2763       if (Bits15To0)
2764         TOut.emitRRI(Mips::ORi, TmpReg, TmpReg, Bits15To0, IDLoc, STI);
2765       if (UseSrcReg)
2766         TOut.emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, STI);
2767       return false;
2768     }
2769 
2770     TOut.emitRI(Mips::LUi, TmpReg, Bits31To16, IDLoc, STI);
2771     if (Bits15To0)
2772       TOut.emitRRI(Mips::ORi, TmpReg, TmpReg, Bits15To0, IDLoc, STI);
2773     if (UseSrcReg)
2774       TOut.emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, STI);
2775     return false;
2776   }
2777 
2778   if (isShiftedUIntAtAnyPosition<16>(ImmValue)) {
2779     if (Is32BitImm) {
2780       Error(IDLoc, "instruction requires a 32-bit immediate");
2781       return true;
2782     }
2783 
2784     // Traditionally, these immediates are shifted as little as possible and as
2785     // such we align the most significant bit to bit 15 of our temporary.
2786     unsigned FirstSet = findFirstSet((uint64_t)ImmValue);
2787     unsigned LastSet = findLastSet((uint64_t)ImmValue);
2788     unsigned ShiftAmount = FirstSet - (15 - (LastSet - FirstSet));
2789     uint16_t Bits = (ImmValue >> ShiftAmount) & 0xffff;
2790     TOut.emitRRI(Mips::ORi, TmpReg, ZeroReg, Bits, IDLoc, STI);
2791     TOut.emitRRI(Mips::DSLL, TmpReg, TmpReg, ShiftAmount, IDLoc, STI);
2792 
2793     if (UseSrcReg)
2794       TOut.emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, STI);
2795 
2796     return false;
2797   }
2798 
2799   warnIfNoMacro(IDLoc);
2800 
2801   // The remaining case is packed with a sequence of dsll and ori with zeros
2802   // being omitted and any neighbouring dsll's being coalesced.
2803   // The highest 32-bit's are equivalent to a 32-bit immediate load.
2804 
2805   // Load bits 32-63 of ImmValue into bits 0-31 of the temporary register.
2806   if (loadImmediate(ImmValue >> 32, TmpReg, Mips::NoRegister, true, false,
2807                     IDLoc, Out, STI))
2808     return false;
2809 
2810   // Shift and accumulate into the register. If a 16-bit chunk is zero, then
2811   // skip it and defer the shift to the next chunk.
2812   unsigned ShiftCarriedForwards = 16;
2813   for (int BitNum = 16; BitNum >= 0; BitNum -= 16) {
2814     uint16_t ImmChunk = (ImmValue >> BitNum) & 0xffff;
2815 
2816     if (ImmChunk != 0) {
2817       TOut.emitDSLL(TmpReg, TmpReg, ShiftCarriedForwards, IDLoc, STI);
2818       TOut.emitRRI(Mips::ORi, TmpReg, TmpReg, ImmChunk, IDLoc, STI);
2819       ShiftCarriedForwards = 0;
2820     }
2821 
2822     ShiftCarriedForwards += 16;
2823   }
2824   ShiftCarriedForwards -= 16;
2825 
2826   // Finish any remaining shifts left by trailing zeros.
2827   if (ShiftCarriedForwards)
2828     TOut.emitDSLL(TmpReg, TmpReg, ShiftCarriedForwards, IDLoc, STI);
2829 
2830   if (UseSrcReg)
2831     TOut.emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, STI);
2832 
2833   return false;
2834 }
2835 
2836 bool MipsAsmParser::expandLoadImm(MCInst &Inst, bool Is32BitImm, SMLoc IDLoc,
2837                                   MCStreamer &Out, const MCSubtargetInfo *STI) {
2838   const MCOperand &ImmOp = Inst.getOperand(1);
2839   assert(ImmOp.isImm() && "expected immediate operand kind");
2840   const MCOperand &DstRegOp = Inst.getOperand(0);
2841   assert(DstRegOp.isReg() && "expected register operand kind");
2842 
2843   if (loadImmediate(ImmOp.getImm(), DstRegOp.getReg(), Mips::NoRegister,
2844                     Is32BitImm, false, IDLoc, Out, STI))
2845     return true;
2846 
2847   return false;
2848 }
2849 
2850 bool MipsAsmParser::expandLoadAddress(unsigned DstReg, unsigned BaseReg,
2851                                       const MCOperand &Offset,
2852                                       bool Is32BitAddress, SMLoc IDLoc,
2853                                       MCStreamer &Out,
2854                                       const MCSubtargetInfo *STI) {
2855   // la can't produce a usable address when addresses are 64-bit.
2856   if (Is32BitAddress && ABI.ArePtrs64bit()) {
2857     Warning(IDLoc, "la used to load 64-bit address");
2858     // Continue as if we had 'dla' instead.
2859     Is32BitAddress = false;
2860   }
2861 
2862   // dla requires 64-bit addresses.
2863   if (!Is32BitAddress && !hasMips3()) {
2864     Error(IDLoc, "instruction requires a 64-bit architecture");
2865     return true;
2866   }
2867 
2868   if (!Offset.isImm())
2869     return loadAndAddSymbolAddress(Offset.getExpr(), DstReg, BaseReg,
2870                                    Is32BitAddress, IDLoc, Out, STI);
2871 
2872   if (!ABI.ArePtrs64bit()) {
2873     // Continue as if we had 'la' whether we had 'la' or 'dla'.
2874     Is32BitAddress = true;
2875   }
2876 
2877   return loadImmediate(Offset.getImm(), DstReg, BaseReg, Is32BitAddress, true,
2878                        IDLoc, Out, STI);
2879 }
2880 
2881 bool MipsAsmParser::loadAndAddSymbolAddress(const MCExpr *SymExpr,
2882                                             unsigned DstReg, unsigned SrcReg,
2883                                             bool Is32BitSym, SMLoc IDLoc,
2884                                             MCStreamer &Out,
2885                                             const MCSubtargetInfo *STI) {
2886   MipsTargetStreamer &TOut = getTargetStreamer();
2887   bool UseSrcReg = SrcReg != Mips::NoRegister && SrcReg != Mips::ZERO &&
2888                    SrcReg != Mips::ZERO_64;
2889   warnIfNoMacro(IDLoc);
2890 
2891   if (inPicMode()) {
2892     MCValue Res;
2893     if (!SymExpr->evaluateAsRelocatable(Res, nullptr, nullptr)) {
2894       Error(IDLoc, "expected relocatable expression");
2895       return true;
2896     }
2897     if (Res.getSymB() != nullptr) {
2898       Error(IDLoc, "expected relocatable expression with only one symbol");
2899       return true;
2900     }
2901 
2902     bool IsPtr64 = ABI.ArePtrs64bit();
2903     bool IsLocalSym =
2904         Res.getSymA()->getSymbol().isInSection() ||
2905         Res.getSymA()->getSymbol().isTemporary() ||
2906         (Res.getSymA()->getSymbol().isELF() &&
2907          cast<MCSymbolELF>(Res.getSymA()->getSymbol()).getBinding() ==
2908              ELF::STB_LOCAL);
2909     bool UseXGOT = STI->getFeatureBits()[Mips::FeatureXGOT] && !IsLocalSym;
2910 
2911     // The case where the result register is $25 is somewhat special. If the
2912     // symbol in the final relocation is external and not modified with a
2913     // constant then we must use R_MIPS_CALL16 instead of R_MIPS_GOT16
2914     // or R_MIPS_CALL16 instead of R_MIPS_GOT_DISP in 64-bit case.
2915     if ((DstReg == Mips::T9 || DstReg == Mips::T9_64) && !UseSrcReg &&
2916         Res.getConstant() == 0 && !IsLocalSym) {
2917       if (UseXGOT) {
2918         const MCExpr *CallHiExpr = MipsMCExpr::create(MipsMCExpr::MEK_CALL_HI16,
2919                                                       SymExpr, getContext());
2920         const MCExpr *CallLoExpr = MipsMCExpr::create(MipsMCExpr::MEK_CALL_LO16,
2921                                                       SymExpr, getContext());
2922         TOut.emitRX(Mips::LUi, DstReg, MCOperand::createExpr(CallHiExpr), IDLoc,
2923                     STI);
2924         TOut.emitRRR(IsPtr64 ? Mips::DADDu : Mips::ADDu, DstReg, DstReg, GPReg,
2925                      IDLoc, STI);
2926         TOut.emitRRX(IsPtr64 ? Mips::LD : Mips::LW, DstReg, DstReg,
2927                      MCOperand::createExpr(CallLoExpr), IDLoc, STI);
2928       } else {
2929         const MCExpr *CallExpr =
2930             MipsMCExpr::create(MipsMCExpr::MEK_GOT_CALL, SymExpr, getContext());
2931         TOut.emitRRX(IsPtr64 ? Mips::LD : Mips::LW, DstReg, GPReg,
2932                      MCOperand::createExpr(CallExpr), IDLoc, STI);
2933       }
2934       return false;
2935     }
2936 
2937     unsigned TmpReg = DstReg;
2938     if (UseSrcReg &&
2939         getContext().getRegisterInfo()->isSuperOrSubRegisterEq(DstReg,
2940                                                                SrcReg)) {
2941       // If $rs is the same as $rd, we need to use AT.
2942       // If it is not available we exit.
2943       unsigned ATReg = getATReg(IDLoc);
2944       if (!ATReg)
2945         return true;
2946       TmpReg = ATReg;
2947     }
2948 
2949     // FIXME: In case of N32 / N64 ABI and emabled XGOT, local addresses
2950     // loaded using R_MIPS_GOT_PAGE / R_MIPS_GOT_OFST pair of relocations.
2951     // FIXME: Implement XGOT for microMIPS.
2952     if (UseXGOT) {
2953       // Loading address from XGOT
2954       //   External GOT: lui $tmp, %got_hi(symbol)($gp)
2955       //                 addu $tmp, $tmp, $gp
2956       //                 lw $tmp, %got_lo(symbol)($tmp)
2957       //                >addiu $tmp, $tmp, offset
2958       //                >addiu $rd, $tmp, $rs
2959       // The addiu's marked with a '>' may be omitted if they are redundant. If
2960       // this happens then the last instruction must use $rd as the result
2961       // register.
2962       const MCExpr *CallHiExpr =
2963           MipsMCExpr::create(MipsMCExpr::MEK_GOT_HI16, SymExpr, getContext());
2964       const MCExpr *CallLoExpr = MipsMCExpr::create(
2965           MipsMCExpr::MEK_GOT_LO16, Res.getSymA(), getContext());
2966 
2967       TOut.emitRX(Mips::LUi, TmpReg, MCOperand::createExpr(CallHiExpr), IDLoc,
2968                   STI);
2969       TOut.emitRRR(IsPtr64 ? Mips::DADDu : Mips::ADDu, TmpReg, TmpReg, GPReg,
2970                    IDLoc, STI);
2971       TOut.emitRRX(IsPtr64 ? Mips::LD : Mips::LW, TmpReg, TmpReg,
2972                    MCOperand::createExpr(CallLoExpr), IDLoc, STI);
2973 
2974       if (Res.getConstant() != 0)
2975         TOut.emitRRX(IsPtr64 ? Mips::DADDiu : Mips::ADDiu, TmpReg, TmpReg,
2976                      MCOperand::createExpr(MCConstantExpr::create(
2977                          Res.getConstant(), getContext())),
2978                      IDLoc, STI);
2979 
2980       if (UseSrcReg)
2981         TOut.emitRRR(IsPtr64 ? Mips::DADDu : Mips::ADDu, DstReg, TmpReg, SrcReg,
2982                      IDLoc, STI);
2983       return false;
2984     }
2985 
2986     const MipsMCExpr *GotExpr = nullptr;
2987     const MCExpr *LoExpr = nullptr;
2988     if (ABI.IsN32() || ABI.IsN64()) {
2989       // The remaining cases are:
2990       //   Small offset: ld $tmp, %got_disp(symbol)($gp)
2991       //                >daddiu $tmp, $tmp, offset
2992       //                >daddu $rd, $tmp, $rs
2993       // The daddiu's marked with a '>' may be omitted if they are redundant. If
2994       // this happens then the last instruction must use $rd as the result
2995       // register.
2996       GotExpr = MipsMCExpr::create(MipsMCExpr::MEK_GOT_DISP, Res.getSymA(),
2997                                    getContext());
2998       if (Res.getConstant() != 0) {
2999         // Symbols fully resolve with just the %got_disp(symbol) but we
3000         // must still account for any offset to the symbol for
3001         // expressions like symbol+8.
3002         LoExpr = MCConstantExpr::create(Res.getConstant(), getContext());
3003 
3004         // FIXME: Offsets greater than 16 bits are not yet implemented.
3005         // FIXME: The correct range is a 32-bit sign-extended number.
3006         if (Res.getConstant() < -0x8000 || Res.getConstant() > 0x7fff) {
3007           Error(IDLoc, "macro instruction uses large offset, which is not "
3008                        "currently supported");
3009           return true;
3010         }
3011       }
3012     } else {
3013       // The remaining cases are:
3014       //   External GOT: lw $tmp, %got(symbol)($gp)
3015       //                >addiu $tmp, $tmp, offset
3016       //                >addiu $rd, $tmp, $rs
3017       //   Local GOT:    lw $tmp, %got(symbol+offset)($gp)
3018       //                 addiu $tmp, $tmp, %lo(symbol+offset)($gp)
3019       //                >addiu $rd, $tmp, $rs
3020       // The addiu's marked with a '>' may be omitted if they are redundant. If
3021       // this happens then the last instruction must use $rd as the result
3022       // register.
3023       if (IsLocalSym) {
3024         GotExpr =
3025             MipsMCExpr::create(MipsMCExpr::MEK_GOT, SymExpr, getContext());
3026         LoExpr = MipsMCExpr::create(MipsMCExpr::MEK_LO, SymExpr, getContext());
3027       } else {
3028         // External symbols fully resolve the symbol with just the %got(symbol)
3029         // but we must still account for any offset to the symbol for
3030         // expressions like symbol+8.
3031         GotExpr = MipsMCExpr::create(MipsMCExpr::MEK_GOT, Res.getSymA(),
3032                                      getContext());
3033         if (Res.getConstant() != 0)
3034           LoExpr = MCConstantExpr::create(Res.getConstant(), getContext());
3035       }
3036     }
3037 
3038     TOut.emitRRX(IsPtr64 ? Mips::LD : Mips::LW, TmpReg, GPReg,
3039                  MCOperand::createExpr(GotExpr), IDLoc, STI);
3040 
3041     if (LoExpr)
3042       TOut.emitRRX(IsPtr64 ? Mips::DADDiu : Mips::ADDiu, TmpReg, TmpReg,
3043                    MCOperand::createExpr(LoExpr), IDLoc, STI);
3044 
3045     if (UseSrcReg)
3046       TOut.emitRRR(IsPtr64 ? Mips::DADDu : Mips::ADDu, DstReg, TmpReg, SrcReg,
3047                    IDLoc, STI);
3048 
3049     return false;
3050   }
3051 
3052   const MipsMCExpr *HiExpr =
3053       MipsMCExpr::create(MipsMCExpr::MEK_HI, SymExpr, getContext());
3054   const MipsMCExpr *LoExpr =
3055       MipsMCExpr::create(MipsMCExpr::MEK_LO, SymExpr, getContext());
3056 
3057   // This is the 64-bit symbol address expansion.
3058   if (ABI.ArePtrs64bit() && isGP64bit()) {
3059     // We need AT for the 64-bit expansion in the cases where the optional
3060     // source register is the destination register and for the superscalar
3061     // scheduled form.
3062     //
3063     // If it is not available we exit if the destination is the same as the
3064     // source register.
3065 
3066     const MipsMCExpr *HighestExpr =
3067         MipsMCExpr::create(MipsMCExpr::MEK_HIGHEST, SymExpr, getContext());
3068     const MipsMCExpr *HigherExpr =
3069         MipsMCExpr::create(MipsMCExpr::MEK_HIGHER, SymExpr, getContext());
3070 
3071     bool RdRegIsRsReg =
3072         getContext().getRegisterInfo()->isSuperOrSubRegisterEq(DstReg, SrcReg);
3073 
3074     if (canUseATReg() && UseSrcReg && RdRegIsRsReg) {
3075       unsigned ATReg = getATReg(IDLoc);
3076 
3077       // If $rs is the same as $rd:
3078       // (d)la $rd, sym($rd) => lui    $at, %highest(sym)
3079       //                        daddiu $at, $at, %higher(sym)
3080       //                        dsll   $at, $at, 16
3081       //                        daddiu $at, $at, %hi(sym)
3082       //                        dsll   $at, $at, 16
3083       //                        daddiu $at, $at, %lo(sym)
3084       //                        daddu  $rd, $at, $rd
3085       TOut.emitRX(Mips::LUi, ATReg, MCOperand::createExpr(HighestExpr), IDLoc,
3086                   STI);
3087       TOut.emitRRX(Mips::DADDiu, ATReg, ATReg,
3088                    MCOperand::createExpr(HigherExpr), IDLoc, STI);
3089       TOut.emitRRI(Mips::DSLL, ATReg, ATReg, 16, IDLoc, STI);
3090       TOut.emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(HiExpr),
3091                    IDLoc, STI);
3092       TOut.emitRRI(Mips::DSLL, ATReg, ATReg, 16, IDLoc, STI);
3093       TOut.emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(LoExpr),
3094                    IDLoc, STI);
3095       TOut.emitRRR(Mips::DADDu, DstReg, ATReg, SrcReg, IDLoc, STI);
3096 
3097       return false;
3098     } else if (canUseATReg() && !RdRegIsRsReg && DstReg != getATReg(IDLoc)) {
3099       unsigned ATReg = getATReg(IDLoc);
3100 
3101       // If the $rs is different from $rd or if $rs isn't specified and we
3102       // have $at available:
3103       // (d)la $rd, sym/sym($rs) => lui    $rd, %highest(sym)
3104       //                            lui    $at, %hi(sym)
3105       //                            daddiu $rd, $rd, %higher(sym)
3106       //                            daddiu $at, $at, %lo(sym)
3107       //                            dsll32 $rd, $rd, 0
3108       //                            daddu  $rd, $rd, $at
3109       //                            (daddu  $rd, $rd, $rs)
3110       //
3111       // Which is preferred for superscalar issue.
3112       TOut.emitRX(Mips::LUi, DstReg, MCOperand::createExpr(HighestExpr), IDLoc,
3113                   STI);
3114       TOut.emitRX(Mips::LUi, ATReg, MCOperand::createExpr(HiExpr), IDLoc, STI);
3115       TOut.emitRRX(Mips::DADDiu, DstReg, DstReg,
3116                    MCOperand::createExpr(HigherExpr), IDLoc, STI);
3117       TOut.emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(LoExpr),
3118                    IDLoc, STI);
3119       TOut.emitRRI(Mips::DSLL32, DstReg, DstReg, 0, IDLoc, STI);
3120       TOut.emitRRR(Mips::DADDu, DstReg, DstReg, ATReg, IDLoc, STI);
3121       if (UseSrcReg)
3122         TOut.emitRRR(Mips::DADDu, DstReg, DstReg, SrcReg, IDLoc, STI);
3123 
3124       return false;
3125     } else if ((!canUseATReg() && !RdRegIsRsReg) ||
3126                (canUseATReg() && DstReg == getATReg(IDLoc))) {
3127       // Otherwise, synthesize the address in the destination register
3128       // serially:
3129       // (d)la $rd, sym/sym($rs) => lui    $rd, %highest(sym)
3130       //                            daddiu $rd, $rd, %higher(sym)
3131       //                            dsll   $rd, $rd, 16
3132       //                            daddiu $rd, $rd, %hi(sym)
3133       //                            dsll   $rd, $rd, 16
3134       //                            daddiu $rd, $rd, %lo(sym)
3135       TOut.emitRX(Mips::LUi, DstReg, MCOperand::createExpr(HighestExpr), IDLoc,
3136                   STI);
3137       TOut.emitRRX(Mips::DADDiu, DstReg, DstReg,
3138                    MCOperand::createExpr(HigherExpr), IDLoc, STI);
3139       TOut.emitRRI(Mips::DSLL, DstReg, DstReg, 16, IDLoc, STI);
3140       TOut.emitRRX(Mips::DADDiu, DstReg, DstReg,
3141                    MCOperand::createExpr(HiExpr), IDLoc, STI);
3142       TOut.emitRRI(Mips::DSLL, DstReg, DstReg, 16, IDLoc, STI);
3143       TOut.emitRRX(Mips::DADDiu, DstReg, DstReg,
3144                    MCOperand::createExpr(LoExpr), IDLoc, STI);
3145       if (UseSrcReg)
3146         TOut.emitRRR(Mips::DADDu, DstReg, DstReg, SrcReg, IDLoc, STI);
3147 
3148       return false;
3149     } else {
3150       // We have a case where SrcReg == DstReg and we don't have $at
3151       // available. We can't expand this case, so error out appropriately.
3152       assert(SrcReg == DstReg && !canUseATReg() &&
3153              "Could have expanded dla but didn't?");
3154       reportParseError(IDLoc,
3155                      "pseudo-instruction requires $at, which is not available");
3156       return true;
3157     }
3158   }
3159 
3160   // And now, the 32-bit symbol address expansion:
3161   // If $rs is the same as $rd:
3162   // (d)la $rd, sym($rd)     => lui   $at, %hi(sym)
3163   //                            ori   $at, $at, %lo(sym)
3164   //                            addu  $rd, $at, $rd
3165   // Otherwise, if the $rs is different from $rd or if $rs isn't specified:
3166   // (d)la $rd, sym/sym($rs) => lui   $rd, %hi(sym)
3167   //                            ori   $rd, $rd, %lo(sym)
3168   //                            (addu $rd, $rd, $rs)
3169   unsigned TmpReg = DstReg;
3170   if (UseSrcReg &&
3171       getContext().getRegisterInfo()->isSuperOrSubRegisterEq(DstReg, SrcReg)) {
3172     // If $rs is the same as $rd, we need to use AT.
3173     // If it is not available we exit.
3174     unsigned ATReg = getATReg(IDLoc);
3175     if (!ATReg)
3176       return true;
3177     TmpReg = ATReg;
3178   }
3179 
3180   TOut.emitRX(Mips::LUi, TmpReg, MCOperand::createExpr(HiExpr), IDLoc, STI);
3181   TOut.emitRRX(Mips::ADDiu, TmpReg, TmpReg, MCOperand::createExpr(LoExpr),
3182                IDLoc, STI);
3183 
3184   if (UseSrcReg)
3185     TOut.emitRRR(Mips::ADDu, DstReg, TmpReg, SrcReg, IDLoc, STI);
3186   else
3187     assert(
3188         getContext().getRegisterInfo()->isSuperOrSubRegisterEq(DstReg, TmpReg));
3189 
3190   return false;
3191 }
3192 
3193 // Each double-precision register DO-D15 overlaps with two of the single
3194 // precision registers F0-F31. As an example, all of the following hold true:
3195 // D0 + 1 == F1, F1 + 1 == D1, F1 + 1 == F2, depending on the context.
3196 static unsigned nextReg(unsigned Reg) {
3197   if (MipsMCRegisterClasses[Mips::FGR32RegClassID].contains(Reg))
3198     return Reg == (unsigned)Mips::F31 ? (unsigned)Mips::F0 : Reg + 1;
3199   switch (Reg) {
3200   default: llvm_unreachable("Unknown register in assembly macro expansion!");
3201   case Mips::ZERO: return Mips::AT;
3202   case Mips::AT:   return Mips::V0;
3203   case Mips::V0:   return Mips::V1;
3204   case Mips::V1:   return Mips::A0;
3205   case Mips::A0:   return Mips::A1;
3206   case Mips::A1:   return Mips::A2;
3207   case Mips::A2:   return Mips::A3;
3208   case Mips::A3:   return Mips::T0;
3209   case Mips::T0:   return Mips::T1;
3210   case Mips::T1:   return Mips::T2;
3211   case Mips::T2:   return Mips::T3;
3212   case Mips::T3:   return Mips::T4;
3213   case Mips::T4:   return Mips::T5;
3214   case Mips::T5:   return Mips::T6;
3215   case Mips::T6:   return Mips::T7;
3216   case Mips::T7:   return Mips::S0;
3217   case Mips::S0:   return Mips::S1;
3218   case Mips::S1:   return Mips::S2;
3219   case Mips::S2:   return Mips::S3;
3220   case Mips::S3:   return Mips::S4;
3221   case Mips::S4:   return Mips::S5;
3222   case Mips::S5:   return Mips::S6;
3223   case Mips::S6:   return Mips::S7;
3224   case Mips::S7:   return Mips::T8;
3225   case Mips::T8:   return Mips::T9;
3226   case Mips::T9:   return Mips::K0;
3227   case Mips::K0:   return Mips::K1;
3228   case Mips::K1:   return Mips::GP;
3229   case Mips::GP:   return Mips::SP;
3230   case Mips::SP:   return Mips::FP;
3231   case Mips::FP:   return Mips::RA;
3232   case Mips::RA:   return Mips::ZERO;
3233   case Mips::D0:   return Mips::F1;
3234   case Mips::D1:   return Mips::F3;
3235   case Mips::D2:   return Mips::F5;
3236   case Mips::D3:   return Mips::F7;
3237   case Mips::D4:   return Mips::F9;
3238   case Mips::D5:   return Mips::F11;
3239   case Mips::D6:   return Mips::F13;
3240   case Mips::D7:   return Mips::F15;
3241   case Mips::D8:   return Mips::F17;
3242   case Mips::D9:   return Mips::F19;
3243   case Mips::D10:   return Mips::F21;
3244   case Mips::D11:   return Mips::F23;
3245   case Mips::D12:   return Mips::F25;
3246   case Mips::D13:   return Mips::F27;
3247   case Mips::D14:   return Mips::F29;
3248   case Mips::D15:   return Mips::F31;
3249   }
3250 }
3251 
3252 // FIXME: This method is too general. In principle we should compute the number
3253 // of instructions required to synthesize the immediate inline compared to
3254 // synthesizing the address inline and relying on non .text sections.
3255 // For static O32 and N32 this may yield a small benefit, for static N64 this is
3256 // likely to yield a much larger benefit as we have to synthesize a 64bit
3257 // address to load a 64 bit value.
3258 bool MipsAsmParser::emitPartialAddress(MipsTargetStreamer &TOut, SMLoc IDLoc,
3259                                        MCSymbol *Sym) {
3260   unsigned ATReg = getATReg(IDLoc);
3261   if (!ATReg)
3262     return true;
3263 
3264   if(IsPicEnabled) {
3265     const MCExpr *GotSym =
3266         MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
3267     const MipsMCExpr *GotExpr =
3268         MipsMCExpr::create(MipsMCExpr::MEK_GOT, GotSym, getContext());
3269 
3270     if(isABI_O32() || isABI_N32()) {
3271       TOut.emitRRX(Mips::LW, ATReg, GPReg, MCOperand::createExpr(GotExpr),
3272                    IDLoc, STI);
3273     } else { //isABI_N64()
3274       TOut.emitRRX(Mips::LD, ATReg, GPReg, MCOperand::createExpr(GotExpr),
3275                    IDLoc, STI);
3276     }
3277   } else { //!IsPicEnabled
3278     const MCExpr *HiSym =
3279         MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
3280     const MipsMCExpr *HiExpr =
3281         MipsMCExpr::create(MipsMCExpr::MEK_HI, HiSym, getContext());
3282 
3283     // FIXME: This is technically correct but gives a different result to gas,
3284     // but gas is incomplete there (it has a fixme noting it doesn't work with
3285     // 64-bit addresses).
3286     // FIXME: With -msym32 option, the address expansion for N64 should probably
3287     // use the O32 / N32 case. It's safe to use the 64 address expansion as the
3288     // symbol's value is considered sign extended.
3289     if(isABI_O32() || isABI_N32()) {
3290       TOut.emitRX(Mips::LUi, ATReg, MCOperand::createExpr(HiExpr), IDLoc, STI);
3291     } else { //isABI_N64()
3292       const MCExpr *HighestSym =
3293           MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
3294       const MipsMCExpr *HighestExpr =
3295           MipsMCExpr::create(MipsMCExpr::MEK_HIGHEST, HighestSym, getContext());
3296       const MCExpr *HigherSym =
3297           MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
3298       const MipsMCExpr *HigherExpr =
3299           MipsMCExpr::create(MipsMCExpr::MEK_HIGHER, HigherSym, getContext());
3300 
3301       TOut.emitRX(Mips::LUi, ATReg, MCOperand::createExpr(HighestExpr), IDLoc,
3302                   STI);
3303       TOut.emitRRX(Mips::DADDiu, ATReg, ATReg,
3304                    MCOperand::createExpr(HigherExpr), IDLoc, STI);
3305       TOut.emitRRI(Mips::DSLL, ATReg, ATReg, 16, IDLoc, STI);
3306       TOut.emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(HiExpr),
3307                    IDLoc, STI);
3308       TOut.emitRRI(Mips::DSLL, ATReg, ATReg, 16, IDLoc, STI);
3309     }
3310   }
3311   return false;
3312 }
3313 
3314 static uint64_t convertIntToDoubleImm(uint64_t ImmOp64) {
3315   // If ImmOp64 is AsmToken::Integer type (all bits set to zero in the
3316   // exponent field), convert it to double (e.g. 1 to 1.0)
3317   if ((Hi_32(ImmOp64) & 0x7ff00000) == 0) {
3318     APFloat RealVal(APFloat::IEEEdouble(), ImmOp64);
3319     ImmOp64 = RealVal.bitcastToAPInt().getZExtValue();
3320   }
3321   return ImmOp64;
3322 }
3323 
3324 static uint32_t covertDoubleImmToSingleImm(uint64_t ImmOp64) {
3325   // Conversion of a double in an uint64_t to a float in a uint32_t,
3326   // retaining the bit pattern of a float.
3327   double DoubleImm = BitsToDouble(ImmOp64);
3328   float TmpFloat = static_cast<float>(DoubleImm);
3329   return FloatToBits(TmpFloat);
3330 }
3331 
3332 bool MipsAsmParser::expandLoadSingleImmToGPR(MCInst &Inst, SMLoc IDLoc,
3333                                              MCStreamer &Out,
3334                                              const MCSubtargetInfo *STI) {
3335   assert(Inst.getNumOperands() == 2 && "Invalid operand count");
3336   assert(Inst.getOperand(0).isReg() && Inst.getOperand(1).isImm() &&
3337          "Invalid instruction operand.");
3338 
3339   unsigned FirstReg = Inst.getOperand(0).getReg();
3340   uint64_t ImmOp64 = Inst.getOperand(1).getImm();
3341 
3342   uint32_t ImmOp32 = covertDoubleImmToSingleImm(convertIntToDoubleImm(ImmOp64));
3343 
3344   return loadImmediate(ImmOp32, FirstReg, Mips::NoRegister, true, false, IDLoc,
3345                        Out, STI);
3346 }
3347 
3348 bool MipsAsmParser::expandLoadSingleImmToFPR(MCInst &Inst, SMLoc IDLoc,
3349                                              MCStreamer &Out,
3350                                              const MCSubtargetInfo *STI) {
3351   MipsTargetStreamer &TOut = getTargetStreamer();
3352   assert(Inst.getNumOperands() == 2 && "Invalid operand count");
3353   assert(Inst.getOperand(0).isReg() && Inst.getOperand(1).isImm() &&
3354          "Invalid instruction operand.");
3355 
3356   unsigned FirstReg = Inst.getOperand(0).getReg();
3357   uint64_t ImmOp64 = Inst.getOperand(1).getImm();
3358 
3359   ImmOp64 = convertIntToDoubleImm(ImmOp64);
3360 
3361   uint32_t ImmOp32 = covertDoubleImmToSingleImm(ImmOp64);
3362 
3363   unsigned TmpReg = Mips::ZERO;
3364   if (ImmOp32 != 0) {
3365     TmpReg = getATReg(IDLoc);
3366     if (!TmpReg)
3367       return true;
3368   }
3369 
3370   if (Lo_32(ImmOp64) == 0) {
3371     if (TmpReg != Mips::ZERO && loadImmediate(ImmOp32, TmpReg, Mips::NoRegister,
3372                                               true, false, IDLoc, Out, STI))
3373       return true;
3374     TOut.emitRR(Mips::MTC1, FirstReg, TmpReg, IDLoc, STI);
3375     return false;
3376   }
3377 
3378   MCSection *CS = getStreamer().getCurrentSectionOnly();
3379   // FIXME: Enhance this expansion to use the .lit4 & .lit8 sections
3380   // where appropriate.
3381   MCSection *ReadOnlySection =
3382       getContext().getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
3383 
3384   MCSymbol *Sym = getContext().createTempSymbol();
3385   const MCExpr *LoSym =
3386       MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
3387   const MipsMCExpr *LoExpr =
3388       MipsMCExpr::create(MipsMCExpr::MEK_LO, LoSym, getContext());
3389 
3390   getStreamer().SwitchSection(ReadOnlySection);
3391   getStreamer().emitLabel(Sym, IDLoc);
3392   getStreamer().emitIntValue(ImmOp32, 4);
3393   getStreamer().SwitchSection(CS);
3394 
3395   if (emitPartialAddress(TOut, IDLoc, Sym))
3396     return true;
3397   TOut.emitRRX(Mips::LWC1, FirstReg, TmpReg, MCOperand::createExpr(LoExpr),
3398                IDLoc, STI);
3399   return false;
3400 }
3401 
3402 bool MipsAsmParser::expandLoadDoubleImmToGPR(MCInst &Inst, SMLoc IDLoc,
3403                                              MCStreamer &Out,
3404                                              const MCSubtargetInfo *STI) {
3405   MipsTargetStreamer &TOut = getTargetStreamer();
3406   assert(Inst.getNumOperands() == 2 && "Invalid operand count");
3407   assert(Inst.getOperand(0).isReg() && Inst.getOperand(1).isImm() &&
3408          "Invalid instruction operand.");
3409 
3410   unsigned FirstReg = Inst.getOperand(0).getReg();
3411   uint64_t ImmOp64 = Inst.getOperand(1).getImm();
3412 
3413   ImmOp64 = convertIntToDoubleImm(ImmOp64);
3414 
3415   if (Lo_32(ImmOp64) == 0) {
3416     if (isGP64bit()) {
3417       if (loadImmediate(ImmOp64, FirstReg, Mips::NoRegister, false, false,
3418                         IDLoc, Out, STI))
3419         return true;
3420     } else {
3421       if (loadImmediate(Hi_32(ImmOp64), FirstReg, Mips::NoRegister, true, false,
3422                         IDLoc, Out, STI))
3423         return true;
3424 
3425       if (loadImmediate(0, nextReg(FirstReg), Mips::NoRegister, true, false,
3426                         IDLoc, Out, STI))
3427         return true;
3428     }
3429     return false;
3430   }
3431 
3432   MCSection *CS = getStreamer().getCurrentSectionOnly();
3433   MCSection *ReadOnlySection =
3434       getContext().getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
3435 
3436   MCSymbol *Sym = getContext().createTempSymbol();
3437   const MCExpr *LoSym =
3438       MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
3439   const MipsMCExpr *LoExpr =
3440       MipsMCExpr::create(MipsMCExpr::MEK_LO, LoSym, getContext());
3441 
3442   getStreamer().SwitchSection(ReadOnlySection);
3443   getStreamer().emitLabel(Sym, IDLoc);
3444   getStreamer().emitValueToAlignment(8);
3445   getStreamer().emitIntValue(ImmOp64, 8);
3446   getStreamer().SwitchSection(CS);
3447 
3448   unsigned TmpReg = getATReg(IDLoc);
3449   if (!TmpReg)
3450     return true;
3451 
3452   if (emitPartialAddress(TOut, IDLoc, Sym))
3453     return true;
3454 
3455   TOut.emitRRX(isABI_N64() ? Mips::DADDiu : Mips::ADDiu, TmpReg, TmpReg,
3456                MCOperand::createExpr(LoExpr), IDLoc, STI);
3457 
3458   if (isGP64bit())
3459     TOut.emitRRI(Mips::LD, FirstReg, TmpReg, 0, IDLoc, STI);
3460   else {
3461     TOut.emitRRI(Mips::LW, FirstReg, TmpReg, 0, IDLoc, STI);
3462     TOut.emitRRI(Mips::LW, nextReg(FirstReg), TmpReg, 4, IDLoc, STI);
3463   }
3464   return false;
3465 }
3466 
3467 bool MipsAsmParser::expandLoadDoubleImmToFPR(MCInst &Inst, bool Is64FPU,
3468                                              SMLoc IDLoc, MCStreamer &Out,
3469                                              const MCSubtargetInfo *STI) {
3470   MipsTargetStreamer &TOut = getTargetStreamer();
3471   assert(Inst.getNumOperands() == 2 && "Invalid operand count");
3472   assert(Inst.getOperand(0).isReg() && Inst.getOperand(1).isImm() &&
3473          "Invalid instruction operand.");
3474 
3475   unsigned FirstReg = Inst.getOperand(0).getReg();
3476   uint64_t ImmOp64 = Inst.getOperand(1).getImm();
3477 
3478   ImmOp64 = convertIntToDoubleImm(ImmOp64);
3479 
3480   unsigned TmpReg = Mips::ZERO;
3481   if (ImmOp64 != 0) {
3482     TmpReg = getATReg(IDLoc);
3483     if (!TmpReg)
3484       return true;
3485   }
3486 
3487   if ((Lo_32(ImmOp64) == 0) &&
3488       !((Hi_32(ImmOp64) & 0xffff0000) && (Hi_32(ImmOp64) & 0x0000ffff))) {
3489     if (isGP64bit()) {
3490       if (TmpReg != Mips::ZERO &&
3491           loadImmediate(ImmOp64, TmpReg, Mips::NoRegister, false, false, IDLoc,
3492                         Out, STI))
3493         return true;
3494       TOut.emitRR(Mips::DMTC1, FirstReg, TmpReg, IDLoc, STI);
3495       return false;
3496     }
3497 
3498     if (TmpReg != Mips::ZERO &&
3499         loadImmediate(Hi_32(ImmOp64), TmpReg, Mips::NoRegister, true, false,
3500                       IDLoc, Out, STI))
3501       return true;
3502 
3503     if (hasMips32r2()) {
3504       TOut.emitRR(Mips::MTC1, FirstReg, Mips::ZERO, IDLoc, STI);
3505       TOut.emitRRR(Mips::MTHC1_D32, FirstReg, FirstReg, TmpReg, IDLoc, STI);
3506     } else {
3507       TOut.emitRR(Mips::MTC1, nextReg(FirstReg), TmpReg, IDLoc, STI);
3508       TOut.emitRR(Mips::MTC1, FirstReg, Mips::ZERO, IDLoc, STI);
3509     }
3510     return false;
3511   }
3512 
3513   MCSection *CS = getStreamer().getCurrentSectionOnly();
3514   // FIXME: Enhance this expansion to use the .lit4 & .lit8 sections
3515   // where appropriate.
3516   MCSection *ReadOnlySection =
3517       getContext().getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
3518 
3519   MCSymbol *Sym = getContext().createTempSymbol();
3520   const MCExpr *LoSym =
3521       MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
3522   const MipsMCExpr *LoExpr =
3523       MipsMCExpr::create(MipsMCExpr::MEK_LO, LoSym, getContext());
3524 
3525   getStreamer().SwitchSection(ReadOnlySection);
3526   getStreamer().emitLabel(Sym, IDLoc);
3527   getStreamer().emitValueToAlignment(8);
3528   getStreamer().emitIntValue(ImmOp64, 8);
3529   getStreamer().SwitchSection(CS);
3530 
3531   if (emitPartialAddress(TOut, IDLoc, Sym))
3532     return true;
3533 
3534   TOut.emitRRX(Is64FPU ? Mips::LDC164 : Mips::LDC1, FirstReg, TmpReg,
3535                MCOperand::createExpr(LoExpr), IDLoc, STI);
3536 
3537   return false;
3538 }
3539 
3540 bool MipsAsmParser::expandUncondBranchMMPseudo(MCInst &Inst, SMLoc IDLoc,
3541                                                MCStreamer &Out,
3542                                                const MCSubtargetInfo *STI) {
3543   MipsTargetStreamer &TOut = getTargetStreamer();
3544 
3545   assert(getInstDesc(Inst.getOpcode()).getNumOperands() == 1 &&
3546          "unexpected number of operands");
3547 
3548   MCOperand Offset = Inst.getOperand(0);
3549   if (Offset.isExpr()) {
3550     Inst.clear();
3551     Inst.setOpcode(Mips::BEQ_MM);
3552     Inst.addOperand(MCOperand::createReg(Mips::ZERO));
3553     Inst.addOperand(MCOperand::createReg(Mips::ZERO));
3554     Inst.addOperand(MCOperand::createExpr(Offset.getExpr()));
3555   } else {
3556     assert(Offset.isImm() && "expected immediate operand kind");
3557     if (isInt<11>(Offset.getImm())) {
3558       // If offset fits into 11 bits then this instruction becomes microMIPS
3559       // 16-bit unconditional branch instruction.
3560       if (inMicroMipsMode())
3561         Inst.setOpcode(hasMips32r6() ? Mips::BC16_MMR6 : Mips::B16_MM);
3562     } else {
3563       if (!isInt<17>(Offset.getImm()))
3564         return Error(IDLoc, "branch target out of range");
3565       if (offsetToAlignment(Offset.getImm(), Align(2)))
3566         return Error(IDLoc, "branch to misaligned address");
3567       Inst.clear();
3568       Inst.setOpcode(Mips::BEQ_MM);
3569       Inst.addOperand(MCOperand::createReg(Mips::ZERO));
3570       Inst.addOperand(MCOperand::createReg(Mips::ZERO));
3571       Inst.addOperand(MCOperand::createImm(Offset.getImm()));
3572     }
3573   }
3574   Out.emitInstruction(Inst, *STI);
3575 
3576   // If .set reorder is active and branch instruction has a delay slot,
3577   // emit a NOP after it.
3578   const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
3579   if (MCID.hasDelaySlot() && AssemblerOptions.back()->isReorder())
3580     TOut.emitEmptyDelaySlot(true, IDLoc, STI);
3581 
3582   return false;
3583 }
3584 
3585 bool MipsAsmParser::expandBranchImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
3586                                     const MCSubtargetInfo *STI) {
3587   MipsTargetStreamer &TOut = getTargetStreamer();
3588   const MCOperand &DstRegOp = Inst.getOperand(0);
3589   assert(DstRegOp.isReg() && "expected register operand kind");
3590 
3591   const MCOperand &ImmOp = Inst.getOperand(1);
3592   assert(ImmOp.isImm() && "expected immediate operand kind");
3593 
3594   const MCOperand &MemOffsetOp = Inst.getOperand(2);
3595   assert((MemOffsetOp.isImm() || MemOffsetOp.isExpr()) &&
3596          "expected immediate or expression operand");
3597 
3598   bool IsLikely = false;
3599 
3600   unsigned OpCode = 0;
3601   switch(Inst.getOpcode()) {
3602     case Mips::BneImm:
3603       OpCode = Mips::BNE;
3604       break;
3605     case Mips::BeqImm:
3606       OpCode = Mips::BEQ;
3607       break;
3608     case Mips::BEQLImmMacro:
3609       OpCode = Mips::BEQL;
3610       IsLikely = true;
3611       break;
3612     case Mips::BNELImmMacro:
3613       OpCode = Mips::BNEL;
3614       IsLikely = true;
3615       break;
3616     default:
3617       llvm_unreachable("Unknown immediate branch pseudo-instruction.");
3618       break;
3619   }
3620 
3621   int64_t ImmValue = ImmOp.getImm();
3622   if (ImmValue == 0) {
3623     if (IsLikely) {
3624       TOut.emitRRX(OpCode, DstRegOp.getReg(), Mips::ZERO,
3625                    MCOperand::createExpr(MemOffsetOp.getExpr()), IDLoc, STI);
3626       TOut.emitRRI(Mips::SLL, Mips::ZERO, Mips::ZERO, 0, IDLoc, STI);
3627     } else
3628       TOut.emitRRX(OpCode, DstRegOp.getReg(), Mips::ZERO, MemOffsetOp, IDLoc,
3629               STI);
3630   } else {
3631     warnIfNoMacro(IDLoc);
3632 
3633     unsigned ATReg = getATReg(IDLoc);
3634     if (!ATReg)
3635       return true;
3636 
3637     if (loadImmediate(ImmValue, ATReg, Mips::NoRegister, !isGP64bit(), true,
3638                       IDLoc, Out, STI))
3639       return true;
3640 
3641     if (IsLikely) {
3642       TOut.emitRRX(OpCode, DstRegOp.getReg(), ATReg,
3643               MCOperand::createExpr(MemOffsetOp.getExpr()), IDLoc, STI);
3644       TOut.emitRRI(Mips::SLL, Mips::ZERO, Mips::ZERO, 0, IDLoc, STI);
3645     } else
3646       TOut.emitRRX(OpCode, DstRegOp.getReg(), ATReg, MemOffsetOp, IDLoc, STI);
3647   }
3648   return false;
3649 }
3650 
3651 void MipsAsmParser::expandMem16Inst(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
3652                                     const MCSubtargetInfo *STI, bool IsLoad) {
3653   unsigned NumOp = Inst.getNumOperands();
3654   assert((NumOp == 3 || NumOp == 4) && "unexpected operands number");
3655   unsigned StartOp = NumOp == 3 ? 0 : 1;
3656 
3657   const MCOperand &DstRegOp = Inst.getOperand(StartOp);
3658   assert(DstRegOp.isReg() && "expected register operand kind");
3659   const MCOperand &BaseRegOp = Inst.getOperand(StartOp + 1);
3660   assert(BaseRegOp.isReg() && "expected register operand kind");
3661   const MCOperand &OffsetOp = Inst.getOperand(StartOp + 2);
3662 
3663   MipsTargetStreamer &TOut = getTargetStreamer();
3664   unsigned OpCode = Inst.getOpcode();
3665   unsigned DstReg = DstRegOp.getReg();
3666   unsigned BaseReg = BaseRegOp.getReg();
3667   unsigned TmpReg = DstReg;
3668 
3669   const MCInstrDesc &Desc = getInstDesc(OpCode);
3670   int16_t DstRegClass = Desc.OpInfo[StartOp].RegClass;
3671   unsigned DstRegClassID =
3672       getContext().getRegisterInfo()->getRegClass(DstRegClass).getID();
3673   bool IsGPR = (DstRegClassID == Mips::GPR32RegClassID) ||
3674                (DstRegClassID == Mips::GPR64RegClassID);
3675 
3676   if (!IsLoad || !IsGPR || (BaseReg == DstReg)) {
3677     // At this point we need AT to perform the expansions
3678     // and we exit if it is not available.
3679     TmpReg = getATReg(IDLoc);
3680     if (!TmpReg)
3681       return;
3682   }
3683 
3684   auto emitInstWithOffset = [&](const MCOperand &Off) {
3685     if (NumOp == 3)
3686       TOut.emitRRX(OpCode, DstReg, TmpReg, Off, IDLoc, STI);
3687     else
3688       TOut.emitRRRX(OpCode, DstReg, DstReg, TmpReg, Off, IDLoc, STI);
3689   };
3690 
3691   if (OffsetOp.isImm()) {
3692     int64_t LoOffset = OffsetOp.getImm() & 0xffff;
3693     int64_t HiOffset = OffsetOp.getImm() & ~0xffff;
3694 
3695     // If msb of LoOffset is 1(negative number) we must increment
3696     // HiOffset to account for the sign-extension of the low part.
3697     if (LoOffset & 0x8000)
3698       HiOffset += 0x10000;
3699 
3700     bool IsLargeOffset = HiOffset != 0;
3701 
3702     if (IsLargeOffset) {
3703       bool Is32BitImm = isInt<32>(OffsetOp.getImm());
3704       if (loadImmediate(HiOffset, TmpReg, Mips::NoRegister, Is32BitImm, true,
3705                         IDLoc, Out, STI))
3706         return;
3707     }
3708 
3709     if (BaseReg != Mips::ZERO && BaseReg != Mips::ZERO_64)
3710       TOut.emitRRR(ABI.ArePtrs64bit() ? Mips::DADDu : Mips::ADDu, TmpReg,
3711                    TmpReg, BaseReg, IDLoc, STI);
3712     emitInstWithOffset(MCOperand::createImm(int16_t(LoOffset)));
3713     return;
3714   }
3715 
3716   if (OffsetOp.isExpr()) {
3717     if (inPicMode()) {
3718       // FIXME:
3719       // c) Check that immediates of R_MIPS_GOT16/R_MIPS_LO16 relocations
3720       //    do not exceed 16-bit.
3721       // d) Use R_MIPS_GOT_PAGE/R_MIPS_GOT_OFST relocations instead
3722       //    of R_MIPS_GOT_DISP in appropriate cases to reduce number
3723       //    of GOT entries.
3724       MCValue Res;
3725       if (!OffsetOp.getExpr()->evaluateAsRelocatable(Res, nullptr, nullptr)) {
3726         Error(IDLoc, "expected relocatable expression");
3727         return;
3728       }
3729       if (Res.getSymB() != nullptr) {
3730         Error(IDLoc, "expected relocatable expression with only one symbol");
3731         return;
3732       }
3733 
3734       loadAndAddSymbolAddress(Res.getSymA(), TmpReg, BaseReg,
3735                               !ABI.ArePtrs64bit(), IDLoc, Out, STI);
3736       emitInstWithOffset(MCOperand::createImm(int16_t(Res.getConstant())));
3737     } else {
3738       // FIXME: Implement 64-bit case.
3739       // 1) lw $8, sym => lui $8,  %hi(sym)
3740       //                  lw  $8,  %lo(sym)($8)
3741       // 2) sw $8, sym => lui $at, %hi(sym)
3742       //                  sw  $8,  %lo(sym)($at)
3743       const MCExpr *OffExpr = OffsetOp.getExpr();
3744       MCOperand LoOperand = MCOperand::createExpr(
3745           MipsMCExpr::create(MipsMCExpr::MEK_LO, OffExpr, getContext()));
3746       MCOperand HiOperand = MCOperand::createExpr(
3747           MipsMCExpr::create(MipsMCExpr::MEK_HI, OffExpr, getContext()));
3748 
3749       if (ABI.IsN64()) {
3750         MCOperand HighestOperand = MCOperand::createExpr(
3751             MipsMCExpr::create(MipsMCExpr::MEK_HIGHEST, OffExpr, getContext()));
3752         MCOperand HigherOperand = MCOperand::createExpr(
3753             MipsMCExpr::create(MipsMCExpr::MEK_HIGHER, OffExpr, getContext()));
3754 
3755         TOut.emitRX(Mips::LUi, TmpReg, HighestOperand, IDLoc, STI);
3756         TOut.emitRRX(Mips::DADDiu, TmpReg, TmpReg, HigherOperand, IDLoc, STI);
3757         TOut.emitRRI(Mips::DSLL, TmpReg, TmpReg, 16, IDLoc, STI);
3758         TOut.emitRRX(Mips::DADDiu, TmpReg, TmpReg, HiOperand, IDLoc, STI);
3759         TOut.emitRRI(Mips::DSLL, TmpReg, TmpReg, 16, IDLoc, STI);
3760         if (BaseReg != Mips::ZERO && BaseReg != Mips::ZERO_64)
3761           TOut.emitRRR(Mips::DADDu, TmpReg, TmpReg, BaseReg, IDLoc, STI);
3762         emitInstWithOffset(LoOperand);
3763       } else {
3764         // Generate the base address in TmpReg.
3765         TOut.emitRX(Mips::LUi, TmpReg, HiOperand, IDLoc, STI);
3766         if (BaseReg != Mips::ZERO)
3767           TOut.emitRRR(Mips::ADDu, TmpReg, TmpReg, BaseReg, IDLoc, STI);
3768         // Emit the load or store with the adjusted base and offset.
3769         emitInstWithOffset(LoOperand);
3770       }
3771     }
3772     return;
3773   }
3774 
3775   llvm_unreachable("unexpected operand type");
3776 }
3777 
3778 void MipsAsmParser::expandMem9Inst(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
3779                                    const MCSubtargetInfo *STI, bool IsLoad) {
3780   unsigned NumOp = Inst.getNumOperands();
3781   assert((NumOp == 3 || NumOp == 4) && "unexpected operands number");
3782   unsigned StartOp = NumOp == 3 ? 0 : 1;
3783 
3784   const MCOperand &DstRegOp = Inst.getOperand(StartOp);
3785   assert(DstRegOp.isReg() && "expected register operand kind");
3786   const MCOperand &BaseRegOp = Inst.getOperand(StartOp + 1);
3787   assert(BaseRegOp.isReg() && "expected register operand kind");
3788   const MCOperand &OffsetOp = Inst.getOperand(StartOp + 2);
3789 
3790   MipsTargetStreamer &TOut = getTargetStreamer();
3791   unsigned OpCode = Inst.getOpcode();
3792   unsigned DstReg = DstRegOp.getReg();
3793   unsigned BaseReg = BaseRegOp.getReg();
3794   unsigned TmpReg = DstReg;
3795 
3796   const MCInstrDesc &Desc = getInstDesc(OpCode);
3797   int16_t DstRegClass = Desc.OpInfo[StartOp].RegClass;
3798   unsigned DstRegClassID =
3799       getContext().getRegisterInfo()->getRegClass(DstRegClass).getID();
3800   bool IsGPR = (DstRegClassID == Mips::GPR32RegClassID) ||
3801                (DstRegClassID == Mips::GPR64RegClassID);
3802 
3803   if (!IsLoad || !IsGPR || (BaseReg == DstReg)) {
3804     // At this point we need AT to perform the expansions
3805     // and we exit if it is not available.
3806     TmpReg = getATReg(IDLoc);
3807     if (!TmpReg)
3808       return;
3809   }
3810 
3811   auto emitInst = [&]() {
3812     if (NumOp == 3)
3813       TOut.emitRRX(OpCode, DstReg, TmpReg, MCOperand::createImm(0), IDLoc, STI);
3814     else
3815       TOut.emitRRRX(OpCode, DstReg, DstReg, TmpReg, MCOperand::createImm(0),
3816                     IDLoc, STI);
3817   };
3818 
3819   if (OffsetOp.isImm()) {
3820     loadImmediate(OffsetOp.getImm(), TmpReg, BaseReg, !ABI.ArePtrs64bit(), true,
3821                   IDLoc, Out, STI);
3822     emitInst();
3823     return;
3824   }
3825 
3826   if (OffsetOp.isExpr()) {
3827     loadAndAddSymbolAddress(OffsetOp.getExpr(), TmpReg, BaseReg,
3828                             !ABI.ArePtrs64bit(), IDLoc, Out, STI);
3829     emitInst();
3830     return;
3831   }
3832 
3833   llvm_unreachable("unexpected operand type");
3834 }
3835 
3836 bool MipsAsmParser::expandLoadStoreMultiple(MCInst &Inst, SMLoc IDLoc,
3837                                             MCStreamer &Out,
3838                                             const MCSubtargetInfo *STI) {
3839   unsigned OpNum = Inst.getNumOperands();
3840   unsigned Opcode = Inst.getOpcode();
3841   unsigned NewOpcode = Opcode == Mips::SWM_MM ? Mips::SWM32_MM : Mips::LWM32_MM;
3842 
3843   assert(Inst.getOperand(OpNum - 1).isImm() &&
3844          Inst.getOperand(OpNum - 2).isReg() &&
3845          Inst.getOperand(OpNum - 3).isReg() && "Invalid instruction operand.");
3846 
3847   if (OpNum < 8 && Inst.getOperand(OpNum - 1).getImm() <= 60 &&
3848       Inst.getOperand(OpNum - 1).getImm() >= 0 &&
3849       (Inst.getOperand(OpNum - 2).getReg() == Mips::SP ||
3850        Inst.getOperand(OpNum - 2).getReg() == Mips::SP_64) &&
3851       (Inst.getOperand(OpNum - 3).getReg() == Mips::RA ||
3852        Inst.getOperand(OpNum - 3).getReg() == Mips::RA_64)) {
3853     // It can be implemented as SWM16 or LWM16 instruction.
3854     if (inMicroMipsMode() && hasMips32r6())
3855       NewOpcode = Opcode == Mips::SWM_MM ? Mips::SWM16_MMR6 : Mips::LWM16_MMR6;
3856     else
3857       NewOpcode = Opcode == Mips::SWM_MM ? Mips::SWM16_MM : Mips::LWM16_MM;
3858   }
3859 
3860   Inst.setOpcode(NewOpcode);
3861   Out.emitInstruction(Inst, *STI);
3862   return false;
3863 }
3864 
3865 bool MipsAsmParser::expandCondBranches(MCInst &Inst, SMLoc IDLoc,
3866                                        MCStreamer &Out,
3867                                        const MCSubtargetInfo *STI) {
3868   MipsTargetStreamer &TOut = getTargetStreamer();
3869   bool EmittedNoMacroWarning = false;
3870   unsigned PseudoOpcode = Inst.getOpcode();
3871   unsigned SrcReg = Inst.getOperand(0).getReg();
3872   const MCOperand &TrgOp = Inst.getOperand(1);
3873   const MCExpr *OffsetExpr = Inst.getOperand(2).getExpr();
3874 
3875   unsigned ZeroSrcOpcode, ZeroTrgOpcode;
3876   bool ReverseOrderSLT, IsUnsigned, IsLikely, AcceptsEquality;
3877 
3878   unsigned TrgReg;
3879   if (TrgOp.isReg())
3880     TrgReg = TrgOp.getReg();
3881   else if (TrgOp.isImm()) {
3882     warnIfNoMacro(IDLoc);
3883     EmittedNoMacroWarning = true;
3884 
3885     TrgReg = getATReg(IDLoc);
3886     if (!TrgReg)
3887       return true;
3888 
3889     switch(PseudoOpcode) {
3890     default:
3891       llvm_unreachable("unknown opcode for branch pseudo-instruction");
3892     case Mips::BLTImmMacro:
3893       PseudoOpcode = Mips::BLT;
3894       break;
3895     case Mips::BLEImmMacro:
3896       PseudoOpcode = Mips::BLE;
3897       break;
3898     case Mips::BGEImmMacro:
3899       PseudoOpcode = Mips::BGE;
3900       break;
3901     case Mips::BGTImmMacro:
3902       PseudoOpcode = Mips::BGT;
3903       break;
3904     case Mips::BLTUImmMacro:
3905       PseudoOpcode = Mips::BLTU;
3906       break;
3907     case Mips::BLEUImmMacro:
3908       PseudoOpcode = Mips::BLEU;
3909       break;
3910     case Mips::BGEUImmMacro:
3911       PseudoOpcode = Mips::BGEU;
3912       break;
3913     case Mips::BGTUImmMacro:
3914       PseudoOpcode = Mips::BGTU;
3915       break;
3916     case Mips::BLTLImmMacro:
3917       PseudoOpcode = Mips::BLTL;
3918       break;
3919     case Mips::BLELImmMacro:
3920       PseudoOpcode = Mips::BLEL;
3921       break;
3922     case Mips::BGELImmMacro:
3923       PseudoOpcode = Mips::BGEL;
3924       break;
3925     case Mips::BGTLImmMacro:
3926       PseudoOpcode = Mips::BGTL;
3927       break;
3928     case Mips::BLTULImmMacro:
3929       PseudoOpcode = Mips::BLTUL;
3930       break;
3931     case Mips::BLEULImmMacro:
3932       PseudoOpcode = Mips::BLEUL;
3933       break;
3934     case Mips::BGEULImmMacro:
3935       PseudoOpcode = Mips::BGEUL;
3936       break;
3937     case Mips::BGTULImmMacro:
3938       PseudoOpcode = Mips::BGTUL;
3939       break;
3940     }
3941 
3942     if (loadImmediate(TrgOp.getImm(), TrgReg, Mips::NoRegister, !isGP64bit(),
3943                       false, IDLoc, Out, STI))
3944       return true;
3945   }
3946 
3947   switch (PseudoOpcode) {
3948   case Mips::BLT:
3949   case Mips::BLTU:
3950   case Mips::BLTL:
3951   case Mips::BLTUL:
3952     AcceptsEquality = false;
3953     ReverseOrderSLT = false;
3954     IsUnsigned =
3955         ((PseudoOpcode == Mips::BLTU) || (PseudoOpcode == Mips::BLTUL));
3956     IsLikely = ((PseudoOpcode == Mips::BLTL) || (PseudoOpcode == Mips::BLTUL));
3957     ZeroSrcOpcode = Mips::BGTZ;
3958     ZeroTrgOpcode = Mips::BLTZ;
3959     break;
3960   case Mips::BLE:
3961   case Mips::BLEU:
3962   case Mips::BLEL:
3963   case Mips::BLEUL:
3964     AcceptsEquality = true;
3965     ReverseOrderSLT = true;
3966     IsUnsigned =
3967         ((PseudoOpcode == Mips::BLEU) || (PseudoOpcode == Mips::BLEUL));
3968     IsLikely = ((PseudoOpcode == Mips::BLEL) || (PseudoOpcode == Mips::BLEUL));
3969     ZeroSrcOpcode = Mips::BGEZ;
3970     ZeroTrgOpcode = Mips::BLEZ;
3971     break;
3972   case Mips::BGE:
3973   case Mips::BGEU:
3974   case Mips::BGEL:
3975   case Mips::BGEUL:
3976     AcceptsEquality = true;
3977     ReverseOrderSLT = false;
3978     IsUnsigned =
3979         ((PseudoOpcode == Mips::BGEU) || (PseudoOpcode == Mips::BGEUL));
3980     IsLikely = ((PseudoOpcode == Mips::BGEL) || (PseudoOpcode == Mips::BGEUL));
3981     ZeroSrcOpcode = Mips::BLEZ;
3982     ZeroTrgOpcode = Mips::BGEZ;
3983     break;
3984   case Mips::BGT:
3985   case Mips::BGTU:
3986   case Mips::BGTL:
3987   case Mips::BGTUL:
3988     AcceptsEquality = false;
3989     ReverseOrderSLT = true;
3990     IsUnsigned =
3991         ((PseudoOpcode == Mips::BGTU) || (PseudoOpcode == Mips::BGTUL));
3992     IsLikely = ((PseudoOpcode == Mips::BGTL) || (PseudoOpcode == Mips::BGTUL));
3993     ZeroSrcOpcode = Mips::BLTZ;
3994     ZeroTrgOpcode = Mips::BGTZ;
3995     break;
3996   default:
3997     llvm_unreachable("unknown opcode for branch pseudo-instruction");
3998   }
3999 
4000   bool IsTrgRegZero = (TrgReg == Mips::ZERO);
4001   bool IsSrcRegZero = (SrcReg == Mips::ZERO);
4002   if (IsSrcRegZero && IsTrgRegZero) {
4003     // FIXME: All of these Opcode-specific if's are needed for compatibility
4004     // with GAS' behaviour. However, they may not generate the most efficient
4005     // code in some circumstances.
4006     if (PseudoOpcode == Mips::BLT) {
4007       TOut.emitRX(Mips::BLTZ, Mips::ZERO, MCOperand::createExpr(OffsetExpr),
4008                   IDLoc, STI);
4009       return false;
4010     }
4011     if (PseudoOpcode == Mips::BLE) {
4012       TOut.emitRX(Mips::BLEZ, Mips::ZERO, MCOperand::createExpr(OffsetExpr),
4013                   IDLoc, STI);
4014       Warning(IDLoc, "branch is always taken");
4015       return false;
4016     }
4017     if (PseudoOpcode == Mips::BGE) {
4018       TOut.emitRX(Mips::BGEZ, Mips::ZERO, MCOperand::createExpr(OffsetExpr),
4019                   IDLoc, STI);
4020       Warning(IDLoc, "branch is always taken");
4021       return false;
4022     }
4023     if (PseudoOpcode == Mips::BGT) {
4024       TOut.emitRX(Mips::BGTZ, Mips::ZERO, MCOperand::createExpr(OffsetExpr),
4025                   IDLoc, STI);
4026       return false;
4027     }
4028     if (PseudoOpcode == Mips::BGTU) {
4029       TOut.emitRRX(Mips::BNE, Mips::ZERO, Mips::ZERO,
4030                    MCOperand::createExpr(OffsetExpr), IDLoc, STI);
4031       return false;
4032     }
4033     if (AcceptsEquality) {
4034       // If both registers are $0 and the pseudo-branch accepts equality, it
4035       // will always be taken, so we emit an unconditional branch.
4036       TOut.emitRRX(Mips::BEQ, Mips::ZERO, Mips::ZERO,
4037                    MCOperand::createExpr(OffsetExpr), IDLoc, STI);
4038       Warning(IDLoc, "branch is always taken");
4039       return false;
4040     }
4041     // If both registers are $0 and the pseudo-branch does not accept
4042     // equality, it will never be taken, so we don't have to emit anything.
4043     return false;
4044   }
4045   if (IsSrcRegZero || IsTrgRegZero) {
4046     if ((IsSrcRegZero && PseudoOpcode == Mips::BGTU) ||
4047         (IsTrgRegZero && PseudoOpcode == Mips::BLTU)) {
4048       // If the $rs is $0 and the pseudo-branch is BGTU (0 > x) or
4049       // if the $rt is $0 and the pseudo-branch is BLTU (x < 0),
4050       // the pseudo-branch will never be taken, so we don't emit anything.
4051       // This only applies to unsigned pseudo-branches.
4052       return false;
4053     }
4054     if ((IsSrcRegZero && PseudoOpcode == Mips::BLEU) ||
4055         (IsTrgRegZero && PseudoOpcode == Mips::BGEU)) {
4056       // If the $rs is $0 and the pseudo-branch is BLEU (0 <= x) or
4057       // if the $rt is $0 and the pseudo-branch is BGEU (x >= 0),
4058       // the pseudo-branch will always be taken, so we emit an unconditional
4059       // branch.
4060       // This only applies to unsigned pseudo-branches.
4061       TOut.emitRRX(Mips::BEQ, Mips::ZERO, Mips::ZERO,
4062                    MCOperand::createExpr(OffsetExpr), IDLoc, STI);
4063       Warning(IDLoc, "branch is always taken");
4064       return false;
4065     }
4066     if (IsUnsigned) {
4067       // If the $rs is $0 and the pseudo-branch is BLTU (0 < x) or
4068       // if the $rt is $0 and the pseudo-branch is BGTU (x > 0),
4069       // the pseudo-branch will be taken only when the non-zero register is
4070       // different from 0, so we emit a BNEZ.
4071       //
4072       // If the $rs is $0 and the pseudo-branch is BGEU (0 >= x) or
4073       // if the $rt is $0 and the pseudo-branch is BLEU (x <= 0),
4074       // the pseudo-branch will be taken only when the non-zero register is
4075       // equal to 0, so we emit a BEQZ.
4076       //
4077       // Because only BLEU and BGEU branch on equality, we can use the
4078       // AcceptsEquality variable to decide when to emit the BEQZ.
4079       TOut.emitRRX(AcceptsEquality ? Mips::BEQ : Mips::BNE,
4080                    IsSrcRegZero ? TrgReg : SrcReg, Mips::ZERO,
4081                    MCOperand::createExpr(OffsetExpr), IDLoc, STI);
4082       return false;
4083     }
4084     // If we have a signed pseudo-branch and one of the registers is $0,
4085     // we can use an appropriate compare-to-zero branch. We select which one
4086     // to use in the switch statement above.
4087     TOut.emitRX(IsSrcRegZero ? ZeroSrcOpcode : ZeroTrgOpcode,
4088                 IsSrcRegZero ? TrgReg : SrcReg,
4089                 MCOperand::createExpr(OffsetExpr), IDLoc, STI);
4090     return false;
4091   }
4092 
4093   // If neither the SrcReg nor the TrgReg are $0, we need AT to perform the
4094   // expansions. If it is not available, we return.
4095   unsigned ATRegNum = getATReg(IDLoc);
4096   if (!ATRegNum)
4097     return true;
4098 
4099   if (!EmittedNoMacroWarning)
4100     warnIfNoMacro(IDLoc);
4101 
4102   // SLT fits well with 2 of our 4 pseudo-branches:
4103   //   BLT, where $rs < $rt, translates into "slt $at, $rs, $rt" and
4104   //   BGT, where $rs > $rt, translates into "slt $at, $rt, $rs".
4105   // If the result of the SLT is 1, we branch, and if it's 0, we don't.
4106   // This is accomplished by using a BNEZ with the result of the SLT.
4107   //
4108   // The other 2 pseudo-branches are opposites of the above 2 (BGE with BLT
4109   // and BLE with BGT), so we change the BNEZ into a BEQZ.
4110   // Because only BGE and BLE branch on equality, we can use the
4111   // AcceptsEquality variable to decide when to emit the BEQZ.
4112   // Note that the order of the SLT arguments doesn't change between
4113   // opposites.
4114   //
4115   // The same applies to the unsigned variants, except that SLTu is used
4116   // instead of SLT.
4117   TOut.emitRRR(IsUnsigned ? Mips::SLTu : Mips::SLT, ATRegNum,
4118                ReverseOrderSLT ? TrgReg : SrcReg,
4119                ReverseOrderSLT ? SrcReg : TrgReg, IDLoc, STI);
4120 
4121   TOut.emitRRX(IsLikely ? (AcceptsEquality ? Mips::BEQL : Mips::BNEL)
4122                         : (AcceptsEquality ? Mips::BEQ : Mips::BNE),
4123                ATRegNum, Mips::ZERO, MCOperand::createExpr(OffsetExpr), IDLoc,
4124                STI);
4125   return false;
4126 }
4127 
4128 // Expand a integer division macro.
4129 //
4130 // Notably we don't have to emit a warning when encountering $rt as the $zero
4131 // register, or 0 as an immediate. processInstruction() has already done that.
4132 //
4133 // The destination register can only be $zero when expanding (S)DivIMacro or
4134 // D(S)DivMacro.
4135 
4136 bool MipsAsmParser::expandDivRem(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
4137                                  const MCSubtargetInfo *STI,
4138                                  const bool IsMips64, const bool Signed) {
4139   MipsTargetStreamer &TOut = getTargetStreamer();
4140 
4141   warnIfNoMacro(IDLoc);
4142 
4143   const MCOperand &RdRegOp = Inst.getOperand(0);
4144   assert(RdRegOp.isReg() && "expected register operand kind");
4145   unsigned RdReg = RdRegOp.getReg();
4146 
4147   const MCOperand &RsRegOp = Inst.getOperand(1);
4148   assert(RsRegOp.isReg() && "expected register operand kind");
4149   unsigned RsReg = RsRegOp.getReg();
4150 
4151   unsigned RtReg;
4152   int64_t ImmValue;
4153 
4154   const MCOperand &RtOp = Inst.getOperand(2);
4155   assert((RtOp.isReg() || RtOp.isImm()) &&
4156          "expected register or immediate operand kind");
4157   if (RtOp.isReg())
4158     RtReg = RtOp.getReg();
4159   else
4160     ImmValue = RtOp.getImm();
4161 
4162   unsigned DivOp;
4163   unsigned ZeroReg;
4164   unsigned SubOp;
4165 
4166   if (IsMips64) {
4167     DivOp = Signed ? Mips::DSDIV : Mips::DUDIV;
4168     ZeroReg = Mips::ZERO_64;
4169     SubOp = Mips::DSUB;
4170   } else {
4171     DivOp = Signed ? Mips::SDIV : Mips::UDIV;
4172     ZeroReg = Mips::ZERO;
4173     SubOp = Mips::SUB;
4174   }
4175 
4176   bool UseTraps = useTraps();
4177 
4178   unsigned Opcode = Inst.getOpcode();
4179   bool isDiv = Opcode == Mips::SDivMacro || Opcode == Mips::SDivIMacro ||
4180                Opcode == Mips::UDivMacro || Opcode == Mips::UDivIMacro ||
4181                Opcode == Mips::DSDivMacro || Opcode == Mips::DSDivIMacro ||
4182                Opcode == Mips::DUDivMacro || Opcode == Mips::DUDivIMacro;
4183 
4184   bool isRem = Opcode == Mips::SRemMacro || Opcode == Mips::SRemIMacro ||
4185                Opcode == Mips::URemMacro || Opcode == Mips::URemIMacro ||
4186                Opcode == Mips::DSRemMacro || Opcode == Mips::DSRemIMacro ||
4187                Opcode == Mips::DURemMacro || Opcode == Mips::DURemIMacro;
4188 
4189   if (RtOp.isImm()) {
4190     unsigned ATReg = getATReg(IDLoc);
4191     if (!ATReg)
4192       return true;
4193 
4194     if (ImmValue == 0) {
4195       if (UseTraps)
4196         TOut.emitRRI(Mips::TEQ, ZeroReg, ZeroReg, 0x7, IDLoc, STI);
4197       else
4198         TOut.emitII(Mips::BREAK, 0x7, 0, IDLoc, STI);
4199       return false;
4200     }
4201 
4202     if (isRem && (ImmValue == 1 || (Signed && (ImmValue == -1)))) {
4203       TOut.emitRRR(Mips::OR, RdReg, ZeroReg, ZeroReg, IDLoc, STI);
4204       return false;
4205     } else if (isDiv && ImmValue == 1) {
4206       TOut.emitRRR(Mips::OR, RdReg, RsReg, Mips::ZERO, IDLoc, STI);
4207       return false;
4208     } else if (isDiv && Signed && ImmValue == -1) {
4209       TOut.emitRRR(SubOp, RdReg, ZeroReg, RsReg, IDLoc, STI);
4210       return false;
4211     } else {
4212       if (loadImmediate(ImmValue, ATReg, Mips::NoRegister, isInt<32>(ImmValue),
4213                         false, Inst.getLoc(), Out, STI))
4214         return true;
4215       TOut.emitRR(DivOp, RsReg, ATReg, IDLoc, STI);
4216       TOut.emitR(isDiv ? Mips::MFLO : Mips::MFHI, RdReg, IDLoc, STI);
4217       return false;
4218     }
4219     return true;
4220   }
4221 
4222   // If the macro expansion of (d)div(u) or (d)rem(u) would always trap or
4223   // break, insert the trap/break and exit. This gives a different result to
4224   // GAS. GAS has an inconsistency/missed optimization in that not all cases
4225   // are handled equivalently. As the observed behaviour is the same, we're ok.
4226   if (RtReg == Mips::ZERO || RtReg == Mips::ZERO_64) {
4227     if (UseTraps) {
4228       TOut.emitRRI(Mips::TEQ, ZeroReg, ZeroReg, 0x7, IDLoc, STI);
4229       return false;
4230     }
4231     TOut.emitII(Mips::BREAK, 0x7, 0, IDLoc, STI);
4232     return false;
4233   }
4234 
4235   // (d)rem(u) $0, $X, $Y is a special case. Like div $zero, $X, $Y, it does
4236   // not expand to macro sequence.
4237   if (isRem && (RdReg == Mips::ZERO || RdReg == Mips::ZERO_64)) {
4238     TOut.emitRR(DivOp, RsReg, RtReg, IDLoc, STI);
4239     return false;
4240   }
4241 
4242   // Temporary label for first branch traget
4243   MCContext &Context = TOut.getStreamer().getContext();
4244   MCSymbol *BrTarget;
4245   MCOperand LabelOp;
4246 
4247   if (UseTraps) {
4248     TOut.emitRRI(Mips::TEQ, RtReg, ZeroReg, 0x7, IDLoc, STI);
4249   } else {
4250     // Branch to the li instruction.
4251     BrTarget = Context.createTempSymbol();
4252     LabelOp = MCOperand::createExpr(MCSymbolRefExpr::create(BrTarget, Context));
4253     TOut.emitRRX(Mips::BNE, RtReg, ZeroReg, LabelOp, IDLoc, STI);
4254   }
4255 
4256   TOut.emitRR(DivOp, RsReg, RtReg, IDLoc, STI);
4257 
4258   if (!UseTraps)
4259     TOut.emitII(Mips::BREAK, 0x7, 0, IDLoc, STI);
4260 
4261   if (!Signed) {
4262     if (!UseTraps)
4263       TOut.getStreamer().emitLabel(BrTarget);
4264 
4265     TOut.emitR(isDiv ? Mips::MFLO : Mips::MFHI, RdReg, IDLoc, STI);
4266     return false;
4267   }
4268 
4269   unsigned ATReg = getATReg(IDLoc);
4270   if (!ATReg)
4271     return true;
4272 
4273   if (!UseTraps)
4274     TOut.getStreamer().emitLabel(BrTarget);
4275 
4276   TOut.emitRRI(Mips::ADDiu, ATReg, ZeroReg, -1, IDLoc, STI);
4277 
4278   // Temporary label for the second branch target.
4279   MCSymbol *BrTargetEnd = Context.createTempSymbol();
4280   MCOperand LabelOpEnd =
4281       MCOperand::createExpr(MCSymbolRefExpr::create(BrTargetEnd, Context));
4282 
4283   // Branch to the mflo instruction.
4284   TOut.emitRRX(Mips::BNE, RtReg, ATReg, LabelOpEnd, IDLoc, STI);
4285 
4286   if (IsMips64) {
4287     TOut.emitRRI(Mips::ADDiu, ATReg, ZeroReg, 1, IDLoc, STI);
4288     TOut.emitDSLL(ATReg, ATReg, 63, IDLoc, STI);
4289   } else {
4290     TOut.emitRI(Mips::LUi, ATReg, (uint16_t)0x8000, IDLoc, STI);
4291   }
4292 
4293   if (UseTraps)
4294     TOut.emitRRI(Mips::TEQ, RsReg, ATReg, 0x6, IDLoc, STI);
4295   else {
4296     // Branch to the mflo instruction.
4297     TOut.emitRRX(Mips::BNE, RsReg, ATReg, LabelOpEnd, IDLoc, STI);
4298     TOut.emitNop(IDLoc, STI);
4299     TOut.emitII(Mips::BREAK, 0x6, 0, IDLoc, STI);
4300   }
4301 
4302   TOut.getStreamer().emitLabel(BrTargetEnd);
4303   TOut.emitR(isDiv ? Mips::MFLO : Mips::MFHI, RdReg, IDLoc, STI);
4304   return false;
4305 }
4306 
4307 bool MipsAsmParser::expandTrunc(MCInst &Inst, bool IsDouble, bool Is64FPU,
4308                                 SMLoc IDLoc, MCStreamer &Out,
4309                                 const MCSubtargetInfo *STI) {
4310   MipsTargetStreamer &TOut = getTargetStreamer();
4311 
4312   assert(Inst.getNumOperands() == 3 && "Invalid operand count");
4313   assert(Inst.getOperand(0).isReg() && Inst.getOperand(1).isReg() &&
4314          Inst.getOperand(2).isReg() && "Invalid instruction operand.");
4315 
4316   unsigned FirstReg = Inst.getOperand(0).getReg();
4317   unsigned SecondReg = Inst.getOperand(1).getReg();
4318   unsigned ThirdReg = Inst.getOperand(2).getReg();
4319 
4320   if (hasMips1() && !hasMips2()) {
4321     unsigned ATReg = getATReg(IDLoc);
4322     if (!ATReg)
4323       return true;
4324     TOut.emitRR(Mips::CFC1, ThirdReg, Mips::RA, IDLoc, STI);
4325     TOut.emitRR(Mips::CFC1, ThirdReg, Mips::RA, IDLoc, STI);
4326     TOut.emitNop(IDLoc, STI);
4327     TOut.emitRRI(Mips::ORi, ATReg, ThirdReg, 0x3, IDLoc, STI);
4328     TOut.emitRRI(Mips::XORi, ATReg, ATReg, 0x2, IDLoc, STI);
4329     TOut.emitRR(Mips::CTC1, Mips::RA, ATReg, IDLoc, STI);
4330     TOut.emitNop(IDLoc, STI);
4331     TOut.emitRR(IsDouble ? (Is64FPU ? Mips::CVT_W_D64 : Mips::CVT_W_D32)
4332                          : Mips::CVT_W_S,
4333                 FirstReg, SecondReg, IDLoc, STI);
4334     TOut.emitRR(Mips::CTC1, Mips::RA, ThirdReg, IDLoc, STI);
4335     TOut.emitNop(IDLoc, STI);
4336     return false;
4337   }
4338 
4339   TOut.emitRR(IsDouble ? (Is64FPU ? Mips::TRUNC_W_D64 : Mips::TRUNC_W_D32)
4340                        : Mips::TRUNC_W_S,
4341               FirstReg, SecondReg, IDLoc, STI);
4342 
4343   return false;
4344 }
4345 
4346 bool MipsAsmParser::expandUlh(MCInst &Inst, bool Signed, SMLoc IDLoc,
4347                               MCStreamer &Out, const MCSubtargetInfo *STI) {
4348   if (hasMips32r6() || hasMips64r6()) {
4349     return Error(IDLoc, "instruction not supported on mips32r6 or mips64r6");
4350   }
4351 
4352   const MCOperand &DstRegOp = Inst.getOperand(0);
4353   assert(DstRegOp.isReg() && "expected register operand kind");
4354   const MCOperand &SrcRegOp = Inst.getOperand(1);
4355   assert(SrcRegOp.isReg() && "expected register operand kind");
4356   const MCOperand &OffsetImmOp = Inst.getOperand(2);
4357   assert(OffsetImmOp.isImm() && "expected immediate operand kind");
4358 
4359   MipsTargetStreamer &TOut = getTargetStreamer();
4360   unsigned DstReg = DstRegOp.getReg();
4361   unsigned SrcReg = SrcRegOp.getReg();
4362   int64_t OffsetValue = OffsetImmOp.getImm();
4363 
4364   // NOTE: We always need AT for ULHU, as it is always used as the source
4365   // register for one of the LBu's.
4366   warnIfNoMacro(IDLoc);
4367   unsigned ATReg = getATReg(IDLoc);
4368   if (!ATReg)
4369     return true;
4370 
4371   bool IsLargeOffset = !(isInt<16>(OffsetValue + 1) && isInt<16>(OffsetValue));
4372   if (IsLargeOffset) {
4373     if (loadImmediate(OffsetValue, ATReg, SrcReg, !ABI.ArePtrs64bit(), true,
4374                       IDLoc, Out, STI))
4375       return true;
4376   }
4377 
4378   int64_t FirstOffset = IsLargeOffset ? 0 : OffsetValue;
4379   int64_t SecondOffset = IsLargeOffset ? 1 : (OffsetValue + 1);
4380   if (isLittle())
4381     std::swap(FirstOffset, SecondOffset);
4382 
4383   unsigned FirstLbuDstReg = IsLargeOffset ? DstReg : ATReg;
4384   unsigned SecondLbuDstReg = IsLargeOffset ? ATReg : DstReg;
4385 
4386   unsigned LbuSrcReg = IsLargeOffset ? ATReg : SrcReg;
4387   unsigned SllReg = IsLargeOffset ? DstReg : ATReg;
4388 
4389   TOut.emitRRI(Signed ? Mips::LB : Mips::LBu, FirstLbuDstReg, LbuSrcReg,
4390                FirstOffset, IDLoc, STI);
4391   TOut.emitRRI(Mips::LBu, SecondLbuDstReg, LbuSrcReg, SecondOffset, IDLoc, STI);
4392   TOut.emitRRI(Mips::SLL, SllReg, SllReg, 8, IDLoc, STI);
4393   TOut.emitRRR(Mips::OR, DstReg, DstReg, ATReg, IDLoc, STI);
4394 
4395   return false;
4396 }
4397 
4398 bool MipsAsmParser::expandUsh(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
4399                               const MCSubtargetInfo *STI) {
4400   if (hasMips32r6() || hasMips64r6()) {
4401     return Error(IDLoc, "instruction not supported on mips32r6 or mips64r6");
4402   }
4403 
4404   const MCOperand &DstRegOp = Inst.getOperand(0);
4405   assert(DstRegOp.isReg() && "expected register operand kind");
4406   const MCOperand &SrcRegOp = Inst.getOperand(1);
4407   assert(SrcRegOp.isReg() && "expected register operand kind");
4408   const MCOperand &OffsetImmOp = Inst.getOperand(2);
4409   assert(OffsetImmOp.isImm() && "expected immediate operand kind");
4410 
4411   MipsTargetStreamer &TOut = getTargetStreamer();
4412   unsigned DstReg = DstRegOp.getReg();
4413   unsigned SrcReg = SrcRegOp.getReg();
4414   int64_t OffsetValue = OffsetImmOp.getImm();
4415 
4416   warnIfNoMacro(IDLoc);
4417   unsigned ATReg = getATReg(IDLoc);
4418   if (!ATReg)
4419     return true;
4420 
4421   bool IsLargeOffset = !(isInt<16>(OffsetValue + 1) && isInt<16>(OffsetValue));
4422   if (IsLargeOffset) {
4423     if (loadImmediate(OffsetValue, ATReg, SrcReg, !ABI.ArePtrs64bit(), true,
4424                       IDLoc, Out, STI))
4425       return true;
4426   }
4427 
4428   int64_t FirstOffset = IsLargeOffset ? 1 : (OffsetValue + 1);
4429   int64_t SecondOffset = IsLargeOffset ? 0 : OffsetValue;
4430   if (isLittle())
4431     std::swap(FirstOffset, SecondOffset);
4432 
4433   if (IsLargeOffset) {
4434     TOut.emitRRI(Mips::SB, DstReg, ATReg, FirstOffset, IDLoc, STI);
4435     TOut.emitRRI(Mips::SRL, DstReg, DstReg, 8, IDLoc, STI);
4436     TOut.emitRRI(Mips::SB, DstReg, ATReg, SecondOffset, IDLoc, STI);
4437     TOut.emitRRI(Mips::LBu, ATReg, ATReg, 0, IDLoc, STI);
4438     TOut.emitRRI(Mips::SLL, DstReg, DstReg, 8, IDLoc, STI);
4439     TOut.emitRRR(Mips::OR, DstReg, DstReg, ATReg, IDLoc, STI);
4440   } else {
4441     TOut.emitRRI(Mips::SB, DstReg, SrcReg, FirstOffset, IDLoc, STI);
4442     TOut.emitRRI(Mips::SRL, ATReg, DstReg, 8, IDLoc, STI);
4443     TOut.emitRRI(Mips::SB, ATReg, SrcReg, SecondOffset, IDLoc, STI);
4444   }
4445 
4446   return false;
4447 }
4448 
4449 bool MipsAsmParser::expandUxw(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
4450                               const MCSubtargetInfo *STI) {
4451   if (hasMips32r6() || hasMips64r6()) {
4452     return Error(IDLoc, "instruction not supported on mips32r6 or mips64r6");
4453   }
4454 
4455   const MCOperand &DstRegOp = Inst.getOperand(0);
4456   assert(DstRegOp.isReg() && "expected register operand kind");
4457   const MCOperand &SrcRegOp = Inst.getOperand(1);
4458   assert(SrcRegOp.isReg() && "expected register operand kind");
4459   const MCOperand &OffsetImmOp = Inst.getOperand(2);
4460   assert(OffsetImmOp.isImm() && "expected immediate operand kind");
4461 
4462   MipsTargetStreamer &TOut = getTargetStreamer();
4463   unsigned DstReg = DstRegOp.getReg();
4464   unsigned SrcReg = SrcRegOp.getReg();
4465   int64_t OffsetValue = OffsetImmOp.getImm();
4466 
4467   // Compute left/right load/store offsets.
4468   bool IsLargeOffset = !(isInt<16>(OffsetValue + 3) && isInt<16>(OffsetValue));
4469   int64_t LxlOffset = IsLargeOffset ? 0 : OffsetValue;
4470   int64_t LxrOffset = IsLargeOffset ? 3 : (OffsetValue + 3);
4471   if (isLittle())
4472     std::swap(LxlOffset, LxrOffset);
4473 
4474   bool IsLoadInst = (Inst.getOpcode() == Mips::Ulw);
4475   bool DoMove = IsLoadInst && (SrcReg == DstReg) && !IsLargeOffset;
4476   unsigned TmpReg = SrcReg;
4477   if (IsLargeOffset || DoMove) {
4478     warnIfNoMacro(IDLoc);
4479     TmpReg = getATReg(IDLoc);
4480     if (!TmpReg)
4481       return true;
4482   }
4483 
4484   if (IsLargeOffset) {
4485     if (loadImmediate(OffsetValue, TmpReg, SrcReg, !ABI.ArePtrs64bit(), true,
4486                       IDLoc, Out, STI))
4487       return true;
4488   }
4489 
4490   if (DoMove)
4491     std::swap(DstReg, TmpReg);
4492 
4493   unsigned XWL = IsLoadInst ? Mips::LWL : Mips::SWL;
4494   unsigned XWR = IsLoadInst ? Mips::LWR : Mips::SWR;
4495   TOut.emitRRI(XWL, DstReg, TmpReg, LxlOffset, IDLoc, STI);
4496   TOut.emitRRI(XWR, DstReg, TmpReg, LxrOffset, IDLoc, STI);
4497 
4498   if (DoMove)
4499     TOut.emitRRR(Mips::OR, TmpReg, DstReg, Mips::ZERO, IDLoc, STI);
4500 
4501   return false;
4502 }
4503 
4504 bool MipsAsmParser::expandSge(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
4505                               const MCSubtargetInfo *STI) {
4506   MipsTargetStreamer &TOut = getTargetStreamer();
4507 
4508   assert(Inst.getNumOperands() == 3 && "Invalid operand count");
4509   assert(Inst.getOperand(0).isReg() &&
4510          Inst.getOperand(1).isReg() &&
4511          Inst.getOperand(2).isReg() && "Invalid instruction operand.");
4512 
4513   unsigned DstReg = Inst.getOperand(0).getReg();
4514   unsigned SrcReg = Inst.getOperand(1).getReg();
4515   unsigned OpReg = Inst.getOperand(2).getReg();
4516   unsigned OpCode;
4517 
4518   warnIfNoMacro(IDLoc);
4519 
4520   switch (Inst.getOpcode()) {
4521   case Mips::SGE:
4522     OpCode = Mips::SLT;
4523     break;
4524   case Mips::SGEU:
4525     OpCode = Mips::SLTu;
4526     break;
4527   default:
4528     llvm_unreachable("unexpected 'sge' opcode");
4529   }
4530 
4531   // $SrcReg >= $OpReg is equal to (not ($SrcReg < $OpReg))
4532   TOut.emitRRR(OpCode, DstReg, SrcReg, OpReg, IDLoc, STI);
4533   TOut.emitRRI(Mips::XORi, DstReg, DstReg, 1, IDLoc, STI);
4534 
4535   return false;
4536 }
4537 
4538 bool MipsAsmParser::expandSgeImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
4539                                  const MCSubtargetInfo *STI) {
4540   MipsTargetStreamer &TOut = getTargetStreamer();
4541 
4542   assert(Inst.getNumOperands() == 3 && "Invalid operand count");
4543   assert(Inst.getOperand(0).isReg() &&
4544          Inst.getOperand(1).isReg() &&
4545          Inst.getOperand(2).isImm() && "Invalid instruction operand.");
4546 
4547   unsigned DstReg = Inst.getOperand(0).getReg();
4548   unsigned SrcReg = Inst.getOperand(1).getReg();
4549   int64_t ImmValue = Inst.getOperand(2).getImm();
4550   unsigned OpRegCode, OpImmCode;
4551 
4552   warnIfNoMacro(IDLoc);
4553 
4554   switch (Inst.getOpcode()) {
4555   case Mips::SGEImm:
4556   case Mips::SGEImm64:
4557     OpRegCode = Mips::SLT;
4558     OpImmCode = Mips::SLTi;
4559     break;
4560   case Mips::SGEUImm:
4561   case Mips::SGEUImm64:
4562     OpRegCode = Mips::SLTu;
4563     OpImmCode = Mips::SLTiu;
4564     break;
4565   default:
4566     llvm_unreachable("unexpected 'sge' opcode with immediate");
4567   }
4568 
4569   // $SrcReg >= Imm is equal to (not ($SrcReg < Imm))
4570   if (isInt<16>(ImmValue)) {
4571     // Use immediate version of STL.
4572     TOut.emitRRI(OpImmCode, DstReg, SrcReg, ImmValue, IDLoc, STI);
4573     TOut.emitRRI(Mips::XORi, DstReg, DstReg, 1, IDLoc, STI);
4574   } else {
4575     unsigned ImmReg = DstReg;
4576     if (DstReg == SrcReg) {
4577       unsigned ATReg = getATReg(Inst.getLoc());
4578       if (!ATReg)
4579         return true;
4580       ImmReg = ATReg;
4581     }
4582 
4583     if (loadImmediate(ImmValue, ImmReg, Mips::NoRegister, isInt<32>(ImmValue),
4584                       false, IDLoc, Out, STI))
4585       return true;
4586 
4587     TOut.emitRRR(OpRegCode, DstReg, SrcReg, ImmReg, IDLoc, STI);
4588     TOut.emitRRI(Mips::XORi, DstReg, DstReg, 1, IDLoc, STI);
4589   }
4590 
4591   return false;
4592 }
4593 
4594 bool MipsAsmParser::expandSgtImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
4595                                  const MCSubtargetInfo *STI) {
4596   MipsTargetStreamer &TOut = getTargetStreamer();
4597 
4598   assert(Inst.getNumOperands() == 3 && "Invalid operand count");
4599   assert(Inst.getOperand(0).isReg() &&
4600          Inst.getOperand(1).isReg() &&
4601          Inst.getOperand(2).isImm() && "Invalid instruction operand.");
4602 
4603   unsigned DstReg = Inst.getOperand(0).getReg();
4604   unsigned SrcReg = Inst.getOperand(1).getReg();
4605   unsigned ImmReg = DstReg;
4606   int64_t ImmValue = Inst.getOperand(2).getImm();
4607   unsigned OpCode;
4608 
4609   warnIfNoMacro(IDLoc);
4610 
4611   switch (Inst.getOpcode()) {
4612   case Mips::SGTImm:
4613   case Mips::SGTImm64:
4614     OpCode = Mips::SLT;
4615     break;
4616   case Mips::SGTUImm:
4617   case Mips::SGTUImm64:
4618     OpCode = Mips::SLTu;
4619     break;
4620   default:
4621     llvm_unreachable("unexpected 'sgt' opcode with immediate");
4622   }
4623 
4624   if (DstReg == SrcReg) {
4625     unsigned ATReg = getATReg(Inst.getLoc());
4626     if (!ATReg)
4627       return true;
4628     ImmReg = ATReg;
4629   }
4630 
4631   if (loadImmediate(ImmValue, ImmReg, Mips::NoRegister, isInt<32>(ImmValue),
4632                     false, IDLoc, Out, STI))
4633     return true;
4634 
4635   // $SrcReg > $ImmReg is equal to $ImmReg < $SrcReg
4636   TOut.emitRRR(OpCode, DstReg, ImmReg, SrcReg, IDLoc, STI);
4637 
4638   return false;
4639 }
4640 
4641 bool MipsAsmParser::expandAliasImmediate(MCInst &Inst, SMLoc IDLoc,
4642                                          MCStreamer &Out,
4643                                          const MCSubtargetInfo *STI) {
4644   MipsTargetStreamer &TOut = getTargetStreamer();
4645 
4646   assert(Inst.getNumOperands() == 3 && "Invalid operand count");
4647   assert(Inst.getOperand(0).isReg() &&
4648          Inst.getOperand(1).isReg() &&
4649          Inst.getOperand(2).isImm() && "Invalid instruction operand.");
4650 
4651   unsigned ATReg = Mips::NoRegister;
4652   unsigned FinalDstReg = Mips::NoRegister;
4653   unsigned DstReg = Inst.getOperand(0).getReg();
4654   unsigned SrcReg = Inst.getOperand(1).getReg();
4655   int64_t ImmValue = Inst.getOperand(2).getImm();
4656 
4657   bool Is32Bit = isInt<32>(ImmValue) || (!isGP64bit() && isUInt<32>(ImmValue));
4658 
4659   unsigned FinalOpcode = Inst.getOpcode();
4660 
4661   if (DstReg == SrcReg) {
4662     ATReg = getATReg(Inst.getLoc());
4663     if (!ATReg)
4664       return true;
4665     FinalDstReg = DstReg;
4666     DstReg = ATReg;
4667   }
4668 
4669   if (!loadImmediate(ImmValue, DstReg, Mips::NoRegister, Is32Bit, false,
4670                      Inst.getLoc(), Out, STI)) {
4671     switch (FinalOpcode) {
4672     default:
4673       llvm_unreachable("unimplemented expansion");
4674     case Mips::ADDi:
4675       FinalOpcode = Mips::ADD;
4676       break;
4677     case Mips::ADDiu:
4678       FinalOpcode = Mips::ADDu;
4679       break;
4680     case Mips::ANDi:
4681       FinalOpcode = Mips::AND;
4682       break;
4683     case Mips::NORImm:
4684       FinalOpcode = Mips::NOR;
4685       break;
4686     case Mips::ORi:
4687       FinalOpcode = Mips::OR;
4688       break;
4689     case Mips::SLTi:
4690       FinalOpcode = Mips::SLT;
4691       break;
4692     case Mips::SLTiu:
4693       FinalOpcode = Mips::SLTu;
4694       break;
4695     case Mips::XORi:
4696       FinalOpcode = Mips::XOR;
4697       break;
4698     case Mips::ADDi_MM:
4699       FinalOpcode = Mips::ADD_MM;
4700       break;
4701     case Mips::ADDiu_MM:
4702       FinalOpcode = Mips::ADDu_MM;
4703       break;
4704     case Mips::ANDi_MM:
4705       FinalOpcode = Mips::AND_MM;
4706       break;
4707     case Mips::ORi_MM:
4708       FinalOpcode = Mips::OR_MM;
4709       break;
4710     case Mips::SLTi_MM:
4711       FinalOpcode = Mips::SLT_MM;
4712       break;
4713     case Mips::SLTiu_MM:
4714       FinalOpcode = Mips::SLTu_MM;
4715       break;
4716     case Mips::XORi_MM:
4717       FinalOpcode = Mips::XOR_MM;
4718       break;
4719     case Mips::ANDi64:
4720       FinalOpcode = Mips::AND64;
4721       break;
4722     case Mips::NORImm64:
4723       FinalOpcode = Mips::NOR64;
4724       break;
4725     case Mips::ORi64:
4726       FinalOpcode = Mips::OR64;
4727       break;
4728     case Mips::SLTImm64:
4729       FinalOpcode = Mips::SLT64;
4730       break;
4731     case Mips::SLTUImm64:
4732       FinalOpcode = Mips::SLTu64;
4733       break;
4734     case Mips::XORi64:
4735       FinalOpcode = Mips::XOR64;
4736       break;
4737     }
4738 
4739     if (FinalDstReg == Mips::NoRegister)
4740       TOut.emitRRR(FinalOpcode, DstReg, DstReg, SrcReg, IDLoc, STI);
4741     else
4742       TOut.emitRRR(FinalOpcode, FinalDstReg, FinalDstReg, DstReg, IDLoc, STI);
4743     return false;
4744   }
4745   return true;
4746 }
4747 
4748 bool MipsAsmParser::expandRotation(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
4749                                    const MCSubtargetInfo *STI) {
4750   MipsTargetStreamer &TOut = getTargetStreamer();
4751   unsigned ATReg = Mips::NoRegister;
4752   unsigned DReg = Inst.getOperand(0).getReg();
4753   unsigned SReg = Inst.getOperand(1).getReg();
4754   unsigned TReg = Inst.getOperand(2).getReg();
4755   unsigned TmpReg = DReg;
4756 
4757   unsigned FirstShift = Mips::NOP;
4758   unsigned SecondShift = Mips::NOP;
4759 
4760   if (hasMips32r2()) {
4761     if (DReg == SReg) {
4762       TmpReg = getATReg(Inst.getLoc());
4763       if (!TmpReg)
4764         return true;
4765     }
4766 
4767     if (Inst.getOpcode() == Mips::ROL) {
4768       TOut.emitRRR(Mips::SUBu, TmpReg, Mips::ZERO, TReg, Inst.getLoc(), STI);
4769       TOut.emitRRR(Mips::ROTRV, DReg, SReg, TmpReg, Inst.getLoc(), STI);
4770       return false;
4771     }
4772 
4773     if (Inst.getOpcode() == Mips::ROR) {
4774       TOut.emitRRR(Mips::ROTRV, DReg, SReg, TReg, Inst.getLoc(), STI);
4775       return false;
4776     }
4777 
4778     return true;
4779   }
4780 
4781   if (hasMips32()) {
4782     switch (Inst.getOpcode()) {
4783     default:
4784       llvm_unreachable("unexpected instruction opcode");
4785     case Mips::ROL:
4786       FirstShift = Mips::SRLV;
4787       SecondShift = Mips::SLLV;
4788       break;
4789     case Mips::ROR:
4790       FirstShift = Mips::SLLV;
4791       SecondShift = Mips::SRLV;
4792       break;
4793     }
4794 
4795     ATReg = getATReg(Inst.getLoc());
4796     if (!ATReg)
4797       return true;
4798 
4799     TOut.emitRRR(Mips::SUBu, ATReg, Mips::ZERO, TReg, Inst.getLoc(), STI);
4800     TOut.emitRRR(FirstShift, ATReg, SReg, ATReg, Inst.getLoc(), STI);
4801     TOut.emitRRR(SecondShift, DReg, SReg, TReg, Inst.getLoc(), STI);
4802     TOut.emitRRR(Mips::OR, DReg, DReg, ATReg, Inst.getLoc(), STI);
4803 
4804     return false;
4805   }
4806 
4807   return true;
4808 }
4809 
4810 bool MipsAsmParser::expandRotationImm(MCInst &Inst, SMLoc IDLoc,
4811                                       MCStreamer &Out,
4812                                       const MCSubtargetInfo *STI) {
4813   MipsTargetStreamer &TOut = getTargetStreamer();
4814   unsigned ATReg = Mips::NoRegister;
4815   unsigned DReg = Inst.getOperand(0).getReg();
4816   unsigned SReg = Inst.getOperand(1).getReg();
4817   int64_t ImmValue = Inst.getOperand(2).getImm();
4818 
4819   unsigned FirstShift = Mips::NOP;
4820   unsigned SecondShift = Mips::NOP;
4821 
4822   if (hasMips32r2()) {
4823     if (Inst.getOpcode() == Mips::ROLImm) {
4824       uint64_t MaxShift = 32;
4825       uint64_t ShiftValue = ImmValue;
4826       if (ImmValue != 0)
4827         ShiftValue = MaxShift - ImmValue;
4828       TOut.emitRRI(Mips::ROTR, DReg, SReg, ShiftValue, Inst.getLoc(), STI);
4829       return false;
4830     }
4831 
4832     if (Inst.getOpcode() == Mips::RORImm) {
4833       TOut.emitRRI(Mips::ROTR, DReg, SReg, ImmValue, Inst.getLoc(), STI);
4834       return false;
4835     }
4836 
4837     return true;
4838   }
4839 
4840   if (hasMips32()) {
4841     if (ImmValue == 0) {
4842       TOut.emitRRI(Mips::SRL, DReg, SReg, 0, Inst.getLoc(), STI);
4843       return false;
4844     }
4845 
4846     switch (Inst.getOpcode()) {
4847     default:
4848       llvm_unreachable("unexpected instruction opcode");
4849     case Mips::ROLImm:
4850       FirstShift = Mips::SLL;
4851       SecondShift = Mips::SRL;
4852       break;
4853     case Mips::RORImm:
4854       FirstShift = Mips::SRL;
4855       SecondShift = Mips::SLL;
4856       break;
4857     }
4858 
4859     ATReg = getATReg(Inst.getLoc());
4860     if (!ATReg)
4861       return true;
4862 
4863     TOut.emitRRI(FirstShift, ATReg, SReg, ImmValue, Inst.getLoc(), STI);
4864     TOut.emitRRI(SecondShift, DReg, SReg, 32 - ImmValue, Inst.getLoc(), STI);
4865     TOut.emitRRR(Mips::OR, DReg, DReg, ATReg, Inst.getLoc(), STI);
4866 
4867     return false;
4868   }
4869 
4870   return true;
4871 }
4872 
4873 bool MipsAsmParser::expandDRotation(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
4874                                     const MCSubtargetInfo *STI) {
4875   MipsTargetStreamer &TOut = getTargetStreamer();
4876   unsigned ATReg = Mips::NoRegister;
4877   unsigned DReg = Inst.getOperand(0).getReg();
4878   unsigned SReg = Inst.getOperand(1).getReg();
4879   unsigned TReg = Inst.getOperand(2).getReg();
4880   unsigned TmpReg = DReg;
4881 
4882   unsigned FirstShift = Mips::NOP;
4883   unsigned SecondShift = Mips::NOP;
4884 
4885   if (hasMips64r2()) {
4886     if (TmpReg == SReg) {
4887       TmpReg = getATReg(Inst.getLoc());
4888       if (!TmpReg)
4889         return true;
4890     }
4891 
4892     if (Inst.getOpcode() == Mips::DROL) {
4893       TOut.emitRRR(Mips::DSUBu, TmpReg, Mips::ZERO, TReg, Inst.getLoc(), STI);
4894       TOut.emitRRR(Mips::DROTRV, DReg, SReg, TmpReg, Inst.getLoc(), STI);
4895       return false;
4896     }
4897 
4898     if (Inst.getOpcode() == Mips::DROR) {
4899       TOut.emitRRR(Mips::DROTRV, DReg, SReg, TReg, Inst.getLoc(), STI);
4900       return false;
4901     }
4902 
4903     return true;
4904   }
4905 
4906   if (hasMips64()) {
4907     switch (Inst.getOpcode()) {
4908     default:
4909       llvm_unreachable("unexpected instruction opcode");
4910     case Mips::DROL:
4911       FirstShift = Mips::DSRLV;
4912       SecondShift = Mips::DSLLV;
4913       break;
4914     case Mips::DROR:
4915       FirstShift = Mips::DSLLV;
4916       SecondShift = Mips::DSRLV;
4917       break;
4918     }
4919 
4920     ATReg = getATReg(Inst.getLoc());
4921     if (!ATReg)
4922       return true;
4923 
4924     TOut.emitRRR(Mips::DSUBu, ATReg, Mips::ZERO, TReg, Inst.getLoc(), STI);
4925     TOut.emitRRR(FirstShift, ATReg, SReg, ATReg, Inst.getLoc(), STI);
4926     TOut.emitRRR(SecondShift, DReg, SReg, TReg, Inst.getLoc(), STI);
4927     TOut.emitRRR(Mips::OR, DReg, DReg, ATReg, Inst.getLoc(), STI);
4928 
4929     return false;
4930   }
4931 
4932   return true;
4933 }
4934 
4935 bool MipsAsmParser::expandDRotationImm(MCInst &Inst, SMLoc IDLoc,
4936                                        MCStreamer &Out,
4937                                        const MCSubtargetInfo *STI) {
4938   MipsTargetStreamer &TOut = getTargetStreamer();
4939   unsigned ATReg = Mips::NoRegister;
4940   unsigned DReg = Inst.getOperand(0).getReg();
4941   unsigned SReg = Inst.getOperand(1).getReg();
4942   int64_t ImmValue = Inst.getOperand(2).getImm() % 64;
4943 
4944   unsigned FirstShift = Mips::NOP;
4945   unsigned SecondShift = Mips::NOP;
4946 
4947   MCInst TmpInst;
4948 
4949   if (hasMips64r2()) {
4950     unsigned FinalOpcode = Mips::NOP;
4951     if (ImmValue == 0)
4952       FinalOpcode = Mips::DROTR;
4953     else if (ImmValue % 32 == 0)
4954       FinalOpcode = Mips::DROTR32;
4955     else if ((ImmValue >= 1) && (ImmValue <= 32)) {
4956       if (Inst.getOpcode() == Mips::DROLImm)
4957         FinalOpcode = Mips::DROTR32;
4958       else
4959         FinalOpcode = Mips::DROTR;
4960     } else if (ImmValue >= 33) {
4961       if (Inst.getOpcode() == Mips::DROLImm)
4962         FinalOpcode = Mips::DROTR;
4963       else
4964         FinalOpcode = Mips::DROTR32;
4965     }
4966 
4967     uint64_t ShiftValue = ImmValue % 32;
4968     if (Inst.getOpcode() == Mips::DROLImm)
4969       ShiftValue = (32 - ImmValue % 32) % 32;
4970 
4971     TOut.emitRRI(FinalOpcode, DReg, SReg, ShiftValue, Inst.getLoc(), STI);
4972 
4973     return false;
4974   }
4975 
4976   if (hasMips64()) {
4977     if (ImmValue == 0) {
4978       TOut.emitRRI(Mips::DSRL, DReg, SReg, 0, Inst.getLoc(), STI);
4979       return false;
4980     }
4981 
4982     switch (Inst.getOpcode()) {
4983     default:
4984       llvm_unreachable("unexpected instruction opcode");
4985     case Mips::DROLImm:
4986       if ((ImmValue >= 1) && (ImmValue <= 31)) {
4987         FirstShift = Mips::DSLL;
4988         SecondShift = Mips::DSRL32;
4989       }
4990       if (ImmValue == 32) {
4991         FirstShift = Mips::DSLL32;
4992         SecondShift = Mips::DSRL32;
4993       }
4994       if ((ImmValue >= 33) && (ImmValue <= 63)) {
4995         FirstShift = Mips::DSLL32;
4996         SecondShift = Mips::DSRL;
4997       }
4998       break;
4999     case Mips::DRORImm:
5000       if ((ImmValue >= 1) && (ImmValue <= 31)) {
5001         FirstShift = Mips::DSRL;
5002         SecondShift = Mips::DSLL32;
5003       }
5004       if (ImmValue == 32) {
5005         FirstShift = Mips::DSRL32;
5006         SecondShift = Mips::DSLL32;
5007       }
5008       if ((ImmValue >= 33) && (ImmValue <= 63)) {
5009         FirstShift = Mips::DSRL32;
5010         SecondShift = Mips::DSLL;
5011       }
5012       break;
5013     }
5014 
5015     ATReg = getATReg(Inst.getLoc());
5016     if (!ATReg)
5017       return true;
5018 
5019     TOut.emitRRI(FirstShift, ATReg, SReg, ImmValue % 32, Inst.getLoc(), STI);
5020     TOut.emitRRI(SecondShift, DReg, SReg, (32 - ImmValue % 32) % 32,
5021                  Inst.getLoc(), STI);
5022     TOut.emitRRR(Mips::OR, DReg, DReg, ATReg, Inst.getLoc(), STI);
5023 
5024     return false;
5025   }
5026 
5027   return true;
5028 }
5029 
5030 bool MipsAsmParser::expandAbs(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
5031                               const MCSubtargetInfo *STI) {
5032   MipsTargetStreamer &TOut = getTargetStreamer();
5033   unsigned FirstRegOp = Inst.getOperand(0).getReg();
5034   unsigned SecondRegOp = Inst.getOperand(1).getReg();
5035 
5036   TOut.emitRI(Mips::BGEZ, SecondRegOp, 8, IDLoc, STI);
5037   if (FirstRegOp != SecondRegOp)
5038     TOut.emitRRR(Mips::ADDu, FirstRegOp, SecondRegOp, Mips::ZERO, IDLoc, STI);
5039   else
5040     TOut.emitEmptyDelaySlot(false, IDLoc, STI);
5041   TOut.emitRRR(Mips::SUB, FirstRegOp, Mips::ZERO, SecondRegOp, IDLoc, STI);
5042 
5043   return false;
5044 }
5045 
5046 bool MipsAsmParser::expandMulImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
5047                                  const MCSubtargetInfo *STI) {
5048   MipsTargetStreamer &TOut = getTargetStreamer();
5049   unsigned ATReg = Mips::NoRegister;
5050   unsigned DstReg = Inst.getOperand(0).getReg();
5051   unsigned SrcReg = Inst.getOperand(1).getReg();
5052   int32_t ImmValue = Inst.getOperand(2).getImm();
5053 
5054   ATReg = getATReg(IDLoc);
5055   if (!ATReg)
5056     return true;
5057 
5058   loadImmediate(ImmValue, ATReg, Mips::NoRegister, true, false, IDLoc, Out,
5059                 STI);
5060 
5061   TOut.emitRR(Inst.getOpcode() == Mips::MULImmMacro ? Mips::MULT : Mips::DMULT,
5062               SrcReg, ATReg, IDLoc, STI);
5063 
5064   TOut.emitR(Mips::MFLO, DstReg, IDLoc, STI);
5065 
5066   return false;
5067 }
5068 
5069 bool MipsAsmParser::expandMulO(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
5070                                const MCSubtargetInfo *STI) {
5071   MipsTargetStreamer &TOut = getTargetStreamer();
5072   unsigned ATReg = Mips::NoRegister;
5073   unsigned DstReg = Inst.getOperand(0).getReg();
5074   unsigned SrcReg = Inst.getOperand(1).getReg();
5075   unsigned TmpReg = Inst.getOperand(2).getReg();
5076 
5077   ATReg = getATReg(Inst.getLoc());
5078   if (!ATReg)
5079     return true;
5080 
5081   TOut.emitRR(Inst.getOpcode() == Mips::MULOMacro ? Mips::MULT : Mips::DMULT,
5082               SrcReg, TmpReg, IDLoc, STI);
5083 
5084   TOut.emitR(Mips::MFLO, DstReg, IDLoc, STI);
5085 
5086   TOut.emitRRI(Inst.getOpcode() == Mips::MULOMacro ? Mips::SRA : Mips::DSRA32,
5087                DstReg, DstReg, 0x1F, IDLoc, STI);
5088 
5089   TOut.emitR(Mips::MFHI, ATReg, IDLoc, STI);
5090 
5091   if (useTraps()) {
5092     TOut.emitRRI(Mips::TNE, DstReg, ATReg, 6, IDLoc, STI);
5093   } else {
5094     MCContext & Context = TOut.getStreamer().getContext();
5095     MCSymbol * BrTarget = Context.createTempSymbol();
5096     MCOperand LabelOp =
5097         MCOperand::createExpr(MCSymbolRefExpr::create(BrTarget, Context));
5098 
5099     TOut.emitRRX(Mips::BEQ, DstReg, ATReg, LabelOp, IDLoc, STI);
5100     if (AssemblerOptions.back()->isReorder())
5101       TOut.emitNop(IDLoc, STI);
5102     TOut.emitII(Mips::BREAK, 6, 0, IDLoc, STI);
5103 
5104     TOut.getStreamer().emitLabel(BrTarget);
5105   }
5106   TOut.emitR(Mips::MFLO, DstReg, IDLoc, STI);
5107 
5108   return false;
5109 }
5110 
5111 bool MipsAsmParser::expandMulOU(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
5112                                 const MCSubtargetInfo *STI) {
5113   MipsTargetStreamer &TOut = getTargetStreamer();
5114   unsigned ATReg = Mips::NoRegister;
5115   unsigned DstReg = Inst.getOperand(0).getReg();
5116   unsigned SrcReg = Inst.getOperand(1).getReg();
5117   unsigned TmpReg = Inst.getOperand(2).getReg();
5118 
5119   ATReg = getATReg(IDLoc);
5120   if (!ATReg)
5121     return true;
5122 
5123   TOut.emitRR(Inst.getOpcode() == Mips::MULOUMacro ? Mips::MULTu : Mips::DMULTu,
5124               SrcReg, TmpReg, IDLoc, STI);
5125 
5126   TOut.emitR(Mips::MFHI, ATReg, IDLoc, STI);
5127   TOut.emitR(Mips::MFLO, DstReg, IDLoc, STI);
5128   if (useTraps()) {
5129     TOut.emitRRI(Mips::TNE, ATReg, Mips::ZERO, 6, IDLoc, STI);
5130   } else {
5131     MCContext & Context = TOut.getStreamer().getContext();
5132     MCSymbol * BrTarget = Context.createTempSymbol();
5133     MCOperand LabelOp =
5134         MCOperand::createExpr(MCSymbolRefExpr::create(BrTarget, Context));
5135 
5136     TOut.emitRRX(Mips::BEQ, ATReg, Mips::ZERO, LabelOp, IDLoc, STI);
5137     if (AssemblerOptions.back()->isReorder())
5138       TOut.emitNop(IDLoc, STI);
5139     TOut.emitII(Mips::BREAK, 6, 0, IDLoc, STI);
5140 
5141     TOut.getStreamer().emitLabel(BrTarget);
5142   }
5143 
5144   return false;
5145 }
5146 
5147 bool MipsAsmParser::expandDMULMacro(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
5148                                     const MCSubtargetInfo *STI) {
5149   MipsTargetStreamer &TOut = getTargetStreamer();
5150   unsigned DstReg = Inst.getOperand(0).getReg();
5151   unsigned SrcReg = Inst.getOperand(1).getReg();
5152   unsigned TmpReg = Inst.getOperand(2).getReg();
5153 
5154   TOut.emitRR(Mips::DMULTu, SrcReg, TmpReg, IDLoc, STI);
5155   TOut.emitR(Mips::MFLO, DstReg, IDLoc, STI);
5156 
5157   return false;
5158 }
5159 
5160 // Expand 'ld $<reg> offset($reg2)' to 'lw $<reg>, offset($reg2);
5161 //                                      lw $<reg+1>>, offset+4($reg2)'
5162 // or expand 'sd $<reg> offset($reg2)' to 'sw $<reg>, offset($reg2);
5163 //                                         sw $<reg+1>>, offset+4($reg2)'
5164 // for O32.
5165 bool MipsAsmParser::expandLoadStoreDMacro(MCInst &Inst, SMLoc IDLoc,
5166                                           MCStreamer &Out,
5167                                           const MCSubtargetInfo *STI,
5168                                           bool IsLoad) {
5169   if (!isABI_O32())
5170     return true;
5171 
5172   warnIfNoMacro(IDLoc);
5173 
5174   MipsTargetStreamer &TOut = getTargetStreamer();
5175   unsigned Opcode = IsLoad ? Mips::LW : Mips::SW;
5176   unsigned FirstReg = Inst.getOperand(0).getReg();
5177   unsigned SecondReg = nextReg(FirstReg);
5178   unsigned BaseReg = Inst.getOperand(1).getReg();
5179   if (!SecondReg)
5180     return true;
5181 
5182   warnIfRegIndexIsAT(FirstReg, IDLoc);
5183 
5184   assert(Inst.getOperand(2).isImm() &&
5185          "Offset for load macro is not immediate!");
5186 
5187   MCOperand &FirstOffset = Inst.getOperand(2);
5188   signed NextOffset = FirstOffset.getImm() + 4;
5189   MCOperand SecondOffset = MCOperand::createImm(NextOffset);
5190 
5191   if (!isInt<16>(FirstOffset.getImm()) || !isInt<16>(NextOffset))
5192     return true;
5193 
5194   // For loads, clobber the base register with the second load instead of the
5195   // first if the BaseReg == FirstReg.
5196   if (FirstReg != BaseReg || !IsLoad) {
5197     TOut.emitRRX(Opcode, FirstReg, BaseReg, FirstOffset, IDLoc, STI);
5198     TOut.emitRRX(Opcode, SecondReg, BaseReg, SecondOffset, IDLoc, STI);
5199   } else {
5200     TOut.emitRRX(Opcode, SecondReg, BaseReg, SecondOffset, IDLoc, STI);
5201     TOut.emitRRX(Opcode, FirstReg, BaseReg, FirstOffset, IDLoc, STI);
5202   }
5203 
5204   return false;
5205 }
5206 
5207 
5208 // Expand 's.d $<reg> offset($reg2)' to 'swc1 $<reg+1>, offset($reg2);
5209 //                                       swc1 $<reg>, offset+4($reg2)'
5210 // or if little endian to 'swc1 $<reg>, offset($reg2);
5211 //                         swc1 $<reg+1>, offset+4($reg2)'
5212 // for Mips1.
5213 bool MipsAsmParser::expandStoreDM1Macro(MCInst &Inst, SMLoc IDLoc,
5214                                         MCStreamer &Out,
5215                                         const MCSubtargetInfo *STI) {
5216   if (!isABI_O32())
5217     return true;
5218 
5219   warnIfNoMacro(IDLoc);
5220 
5221   MipsTargetStreamer &TOut = getTargetStreamer();
5222   unsigned Opcode = Mips::SWC1;
5223   unsigned FirstReg = Inst.getOperand(0).getReg();
5224   unsigned SecondReg = nextReg(FirstReg);
5225   unsigned BaseReg = Inst.getOperand(1).getReg();
5226   if (!SecondReg)
5227     return true;
5228 
5229   warnIfRegIndexIsAT(FirstReg, IDLoc);
5230 
5231   assert(Inst.getOperand(2).isImm() &&
5232          "Offset for macro is not immediate!");
5233 
5234   MCOperand &FirstOffset = Inst.getOperand(2);
5235   signed NextOffset = FirstOffset.getImm() + 4;
5236   MCOperand SecondOffset = MCOperand::createImm(NextOffset);
5237 
5238   if (!isInt<16>(FirstOffset.getImm()) || !isInt<16>(NextOffset))
5239     return true;
5240 
5241   if (!IsLittleEndian)
5242     std::swap(FirstReg, SecondReg);
5243 
5244   TOut.emitRRX(Opcode, FirstReg, BaseReg, FirstOffset, IDLoc, STI);
5245   TOut.emitRRX(Opcode, SecondReg, BaseReg, SecondOffset, IDLoc, STI);
5246 
5247   return false;
5248 }
5249 
5250 bool MipsAsmParser::expandSeq(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
5251                               const MCSubtargetInfo *STI) {
5252   MipsTargetStreamer &TOut = getTargetStreamer();
5253 
5254   assert(Inst.getNumOperands() == 3 && "Invalid operand count");
5255   assert(Inst.getOperand(0).isReg() &&
5256          Inst.getOperand(1).isReg() &&
5257          Inst.getOperand(2).isReg() && "Invalid instruction operand.");
5258 
5259   unsigned DstReg = Inst.getOperand(0).getReg();
5260   unsigned SrcReg = Inst.getOperand(1).getReg();
5261   unsigned OpReg = Inst.getOperand(2).getReg();
5262 
5263   warnIfNoMacro(IDLoc);
5264 
5265   if (SrcReg != Mips::ZERO && OpReg != Mips::ZERO) {
5266     TOut.emitRRR(Mips::XOR, DstReg, SrcReg, OpReg, IDLoc, STI);
5267     TOut.emitRRI(Mips::SLTiu, DstReg, DstReg, 1, IDLoc, STI);
5268     return false;
5269   }
5270 
5271   unsigned Reg = SrcReg == Mips::ZERO ? OpReg : SrcReg;
5272   TOut.emitRRI(Mips::SLTiu, DstReg, Reg, 1, IDLoc, STI);
5273   return false;
5274 }
5275 
5276 bool MipsAsmParser::expandSeqI(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
5277                                const MCSubtargetInfo *STI) {
5278   MipsTargetStreamer &TOut = getTargetStreamer();
5279 
5280   assert(Inst.getNumOperands() == 3 && "Invalid operand count");
5281   assert(Inst.getOperand(0).isReg() &&
5282          Inst.getOperand(1).isReg() &&
5283          Inst.getOperand(2).isImm() && "Invalid instruction operand.");
5284 
5285   unsigned DstReg = Inst.getOperand(0).getReg();
5286   unsigned SrcReg = Inst.getOperand(1).getReg();
5287   int64_t Imm = Inst.getOperand(2).getImm();
5288 
5289   warnIfNoMacro(IDLoc);
5290 
5291   if (Imm == 0) {
5292     TOut.emitRRI(Mips::SLTiu, DstReg, SrcReg, 1, IDLoc, STI);
5293     return false;
5294   }
5295 
5296   if (SrcReg == Mips::ZERO) {
5297     Warning(IDLoc, "comparison is always false");
5298     TOut.emitRRR(isGP64bit() ? Mips::DADDu : Mips::ADDu,
5299                  DstReg, SrcReg, SrcReg, IDLoc, STI);
5300     return false;
5301   }
5302 
5303   unsigned Opc;
5304   if (Imm > -0x8000 && Imm < 0) {
5305     Imm = -Imm;
5306     Opc = isGP64bit() ? Mips::DADDiu : Mips::ADDiu;
5307   } else {
5308     Opc = Mips::XORi;
5309   }
5310 
5311   if (!isUInt<16>(Imm)) {
5312     unsigned ATReg = getATReg(IDLoc);
5313     if (!ATReg)
5314       return true;
5315 
5316     if (loadImmediate(Imm, ATReg, Mips::NoRegister, true, isGP64bit(), IDLoc,
5317                       Out, STI))
5318       return true;
5319 
5320     TOut.emitRRR(Mips::XOR, DstReg, SrcReg, ATReg, IDLoc, STI);
5321     TOut.emitRRI(Mips::SLTiu, DstReg, DstReg, 1, IDLoc, STI);
5322     return false;
5323   }
5324 
5325   TOut.emitRRI(Opc, DstReg, SrcReg, Imm, IDLoc, STI);
5326   TOut.emitRRI(Mips::SLTiu, DstReg, DstReg, 1, IDLoc, STI);
5327   return false;
5328 }
5329 
5330 // Map the DSP accumulator and control register to the corresponding gpr
5331 // operand. Unlike the other alias, the m(f|t)t(lo|hi|acx) instructions
5332 // do not map the DSP registers contigously to gpr registers.
5333 static unsigned getRegisterForMxtrDSP(MCInst &Inst, bool IsMFDSP) {
5334   switch (Inst.getOpcode()) {
5335     case Mips::MFTLO:
5336     case Mips::MTTLO:
5337       switch (Inst.getOperand(IsMFDSP ? 1 : 0).getReg()) {
5338         case Mips::AC0:
5339           return Mips::ZERO;
5340         case Mips::AC1:
5341           return Mips::A0;
5342         case Mips::AC2:
5343           return Mips::T0;
5344         case Mips::AC3:
5345           return Mips::T4;
5346         default:
5347           llvm_unreachable("Unknown register for 'mttr' alias!");
5348     }
5349     case Mips::MFTHI:
5350     case Mips::MTTHI:
5351       switch (Inst.getOperand(IsMFDSP ? 1 : 0).getReg()) {
5352         case Mips::AC0:
5353           return Mips::AT;
5354         case Mips::AC1:
5355           return Mips::A1;
5356         case Mips::AC2:
5357           return Mips::T1;
5358         case Mips::AC3:
5359           return Mips::T5;
5360         default:
5361           llvm_unreachable("Unknown register for 'mttr' alias!");
5362     }
5363     case Mips::MFTACX:
5364     case Mips::MTTACX:
5365       switch (Inst.getOperand(IsMFDSP ? 1 : 0).getReg()) {
5366         case Mips::AC0:
5367           return Mips::V0;
5368         case Mips::AC1:
5369           return Mips::A2;
5370         case Mips::AC2:
5371           return Mips::T2;
5372         case Mips::AC3:
5373           return Mips::T6;
5374         default:
5375           llvm_unreachable("Unknown register for 'mttr' alias!");
5376     }
5377     case Mips::MFTDSP:
5378     case Mips::MTTDSP:
5379       return Mips::S0;
5380     default:
5381       llvm_unreachable("Unknown instruction for 'mttr' dsp alias!");
5382   }
5383 }
5384 
5385 // Map the floating point register operand to the corresponding register
5386 // operand.
5387 static unsigned getRegisterForMxtrFP(MCInst &Inst, bool IsMFTC1) {
5388   switch (Inst.getOperand(IsMFTC1 ? 1 : 0).getReg()) {
5389     case Mips::F0:  return Mips::ZERO;
5390     case Mips::F1:  return Mips::AT;
5391     case Mips::F2:  return Mips::V0;
5392     case Mips::F3:  return Mips::V1;
5393     case Mips::F4:  return Mips::A0;
5394     case Mips::F5:  return Mips::A1;
5395     case Mips::F6:  return Mips::A2;
5396     case Mips::F7:  return Mips::A3;
5397     case Mips::F8:  return Mips::T0;
5398     case Mips::F9:  return Mips::T1;
5399     case Mips::F10: return Mips::T2;
5400     case Mips::F11: return Mips::T3;
5401     case Mips::F12: return Mips::T4;
5402     case Mips::F13: return Mips::T5;
5403     case Mips::F14: return Mips::T6;
5404     case Mips::F15: return Mips::T7;
5405     case Mips::F16: return Mips::S0;
5406     case Mips::F17: return Mips::S1;
5407     case Mips::F18: return Mips::S2;
5408     case Mips::F19: return Mips::S3;
5409     case Mips::F20: return Mips::S4;
5410     case Mips::F21: return Mips::S5;
5411     case Mips::F22: return Mips::S6;
5412     case Mips::F23: return Mips::S7;
5413     case Mips::F24: return Mips::T8;
5414     case Mips::F25: return Mips::T9;
5415     case Mips::F26: return Mips::K0;
5416     case Mips::F27: return Mips::K1;
5417     case Mips::F28: return Mips::GP;
5418     case Mips::F29: return Mips::SP;
5419     case Mips::F30: return Mips::FP;
5420     case Mips::F31: return Mips::RA;
5421     default: llvm_unreachable("Unknown register for mttc1 alias!");
5422   }
5423 }
5424 
5425 // Map the coprocessor operand the corresponding gpr register operand.
5426 static unsigned getRegisterForMxtrC0(MCInst &Inst, bool IsMFTC0) {
5427   switch (Inst.getOperand(IsMFTC0 ? 1 : 0).getReg()) {
5428     case Mips::COP00:  return Mips::ZERO;
5429     case Mips::COP01:  return Mips::AT;
5430     case Mips::COP02:  return Mips::V0;
5431     case Mips::COP03:  return Mips::V1;
5432     case Mips::COP04:  return Mips::A0;
5433     case Mips::COP05:  return Mips::A1;
5434     case Mips::COP06:  return Mips::A2;
5435     case Mips::COP07:  return Mips::A3;
5436     case Mips::COP08:  return Mips::T0;
5437     case Mips::COP09:  return Mips::T1;
5438     case Mips::COP010: return Mips::T2;
5439     case Mips::COP011: return Mips::T3;
5440     case Mips::COP012: return Mips::T4;
5441     case Mips::COP013: return Mips::T5;
5442     case Mips::COP014: return Mips::T6;
5443     case Mips::COP015: return Mips::T7;
5444     case Mips::COP016: return Mips::S0;
5445     case Mips::COP017: return Mips::S1;
5446     case Mips::COP018: return Mips::S2;
5447     case Mips::COP019: return Mips::S3;
5448     case Mips::COP020: return Mips::S4;
5449     case Mips::COP021: return Mips::S5;
5450     case Mips::COP022: return Mips::S6;
5451     case Mips::COP023: return Mips::S7;
5452     case Mips::COP024: return Mips::T8;
5453     case Mips::COP025: return Mips::T9;
5454     case Mips::COP026: return Mips::K0;
5455     case Mips::COP027: return Mips::K1;
5456     case Mips::COP028: return Mips::GP;
5457     case Mips::COP029: return Mips::SP;
5458     case Mips::COP030: return Mips::FP;
5459     case Mips::COP031: return Mips::RA;
5460     default: llvm_unreachable("Unknown register for mttc0 alias!");
5461   }
5462 }
5463 
5464 /// Expand an alias of 'mftr' or 'mttr' into the full instruction, by producing
5465 /// an mftr or mttr with the correctly mapped gpr register, u, sel and h bits.
5466 bool MipsAsmParser::expandMXTRAlias(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
5467                                     const MCSubtargetInfo *STI) {
5468   MipsTargetStreamer &TOut = getTargetStreamer();
5469   unsigned rd = 0;
5470   unsigned u = 1;
5471   unsigned sel = 0;
5472   unsigned h = 0;
5473   bool IsMFTR = false;
5474   switch (Inst.getOpcode()) {
5475     case Mips::MFTC0:
5476       IsMFTR = true;
5477       LLVM_FALLTHROUGH;
5478     case Mips::MTTC0:
5479       u = 0;
5480       rd = getRegisterForMxtrC0(Inst, IsMFTR);
5481       sel = Inst.getOperand(2).getImm();
5482       break;
5483     case Mips::MFTGPR:
5484       IsMFTR = true;
5485       LLVM_FALLTHROUGH;
5486     case Mips::MTTGPR:
5487       rd = Inst.getOperand(IsMFTR ? 1 : 0).getReg();
5488       break;
5489     case Mips::MFTLO:
5490     case Mips::MFTHI:
5491     case Mips::MFTACX:
5492     case Mips::MFTDSP:
5493       IsMFTR = true;
5494       LLVM_FALLTHROUGH;
5495     case Mips::MTTLO:
5496     case Mips::MTTHI:
5497     case Mips::MTTACX:
5498     case Mips::MTTDSP:
5499       rd = getRegisterForMxtrDSP(Inst, IsMFTR);
5500       sel = 1;
5501       break;
5502     case Mips::MFTHC1:
5503       h = 1;
5504       LLVM_FALLTHROUGH;
5505     case Mips::MFTC1:
5506       IsMFTR = true;
5507       rd = getRegisterForMxtrFP(Inst, IsMFTR);
5508       sel = 2;
5509       break;
5510     case Mips::MTTHC1:
5511       h = 1;
5512       LLVM_FALLTHROUGH;
5513     case Mips::MTTC1:
5514       rd = getRegisterForMxtrFP(Inst, IsMFTR);
5515       sel = 2;
5516       break;
5517     case Mips::CFTC1:
5518       IsMFTR = true;
5519       LLVM_FALLTHROUGH;
5520     case Mips::CTTC1:
5521       rd = getRegisterForMxtrFP(Inst, IsMFTR);
5522       sel = 3;
5523       break;
5524   }
5525   unsigned Op0 = IsMFTR ? Inst.getOperand(0).getReg() : rd;
5526   unsigned Op1 =
5527       IsMFTR ? rd
5528              : (Inst.getOpcode() != Mips::MTTDSP ? Inst.getOperand(1).getReg()
5529                                                  : Inst.getOperand(0).getReg());
5530 
5531   TOut.emitRRIII(IsMFTR ? Mips::MFTR : Mips::MTTR, Op0, Op1, u, sel, h, IDLoc,
5532                  STI);
5533   return false;
5534 }
5535 
5536 bool MipsAsmParser::expandSaaAddr(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
5537                                   const MCSubtargetInfo *STI) {
5538   assert(Inst.getNumOperands() == 3 && "expected three operands");
5539   assert(Inst.getOperand(0).isReg() && "expected register operand kind");
5540   assert(Inst.getOperand(1).isReg() && "expected register operand kind");
5541 
5542   warnIfNoMacro(IDLoc);
5543 
5544   MipsTargetStreamer &TOut = getTargetStreamer();
5545   unsigned Opcode = Inst.getOpcode() == Mips::SaaAddr ? Mips::SAA : Mips::SAAD;
5546   unsigned RtReg = Inst.getOperand(0).getReg();
5547   unsigned BaseReg = Inst.getOperand(1).getReg();
5548   const MCOperand &BaseOp = Inst.getOperand(2);
5549 
5550   if (BaseOp.isImm()) {
5551     int64_t ImmValue = BaseOp.getImm();
5552     if (ImmValue == 0) {
5553       TOut.emitRR(Opcode, RtReg, BaseReg, IDLoc, STI);
5554       return false;
5555     }
5556   }
5557 
5558   unsigned ATReg = getATReg(IDLoc);
5559   if (!ATReg)
5560     return true;
5561 
5562   if (expandLoadAddress(ATReg, BaseReg, BaseOp, !isGP64bit(), IDLoc, Out, STI))
5563     return true;
5564 
5565   TOut.emitRR(Opcode, RtReg, ATReg, IDLoc, STI);
5566   return false;
5567 }
5568 
5569 unsigned
5570 MipsAsmParser::checkEarlyTargetMatchPredicate(MCInst &Inst,
5571                                               const OperandVector &Operands) {
5572   switch (Inst.getOpcode()) {
5573   default:
5574     return Match_Success;
5575   case Mips::DATI:
5576   case Mips::DAHI:
5577     if (static_cast<MipsOperand &>(*Operands[1])
5578             .isValidForTie(static_cast<MipsOperand &>(*Operands[2])))
5579       return Match_Success;
5580     return Match_RequiresSameSrcAndDst;
5581   }
5582 }
5583 
5584 unsigned MipsAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
5585   switch (Inst.getOpcode()) {
5586   // As described by the MIPSR6 spec, daui must not use the zero operand for
5587   // its source operand.
5588   case Mips::DAUI:
5589     if (Inst.getOperand(1).getReg() == Mips::ZERO ||
5590         Inst.getOperand(1).getReg() == Mips::ZERO_64)
5591       return Match_RequiresNoZeroRegister;
5592     return Match_Success;
5593   // As described by the Mips32r2 spec, the registers Rd and Rs for
5594   // jalr.hb must be different.
5595   // It also applies for registers Rt and Rs of microMIPSr6 jalrc.hb instruction
5596   // and registers Rd and Base for microMIPS lwp instruction
5597   case Mips::JALR_HB:
5598   case Mips::JALR_HB64:
5599   case Mips::JALRC_HB_MMR6:
5600   case Mips::JALRC_MMR6:
5601     if (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg())
5602       return Match_RequiresDifferentSrcAndDst;
5603     return Match_Success;
5604   case Mips::LWP_MM:
5605     if (Inst.getOperand(0).getReg() == Inst.getOperand(2).getReg())
5606       return Match_RequiresDifferentSrcAndDst;
5607     return Match_Success;
5608   case Mips::SYNC:
5609     if (Inst.getOperand(0).getImm() != 0 && !hasMips32())
5610       return Match_NonZeroOperandForSync;
5611     return Match_Success;
5612   case Mips::MFC0:
5613   case Mips::MTC0:
5614   case Mips::MTC2:
5615   case Mips::MFC2:
5616     if (Inst.getOperand(2).getImm() != 0 && !hasMips32())
5617       return Match_NonZeroOperandForMTCX;
5618     return Match_Success;
5619   // As described the MIPSR6 spec, the compact branches that compare registers
5620   // must:
5621   // a) Not use the zero register.
5622   // b) Not use the same register twice.
5623   // c) rs < rt for bnec, beqc.
5624   //    NB: For this case, the encoding will swap the operands as their
5625   //    ordering doesn't matter. GAS performs this transformation  too.
5626   //    Hence, that constraint does not have to be enforced.
5627   //
5628   // The compact branches that branch iff the signed addition of two registers
5629   // would overflow must have rs >= rt. That can be handled like beqc/bnec with
5630   // operand swapping. They do not have restriction of using the zero register.
5631   case Mips::BLEZC:   case Mips::BLEZC_MMR6:
5632   case Mips::BGEZC:   case Mips::BGEZC_MMR6:
5633   case Mips::BGTZC:   case Mips::BGTZC_MMR6:
5634   case Mips::BLTZC:   case Mips::BLTZC_MMR6:
5635   case Mips::BEQZC:   case Mips::BEQZC_MMR6:
5636   case Mips::BNEZC:   case Mips::BNEZC_MMR6:
5637   case Mips::BLEZC64:
5638   case Mips::BGEZC64:
5639   case Mips::BGTZC64:
5640   case Mips::BLTZC64:
5641   case Mips::BEQZC64:
5642   case Mips::BNEZC64:
5643     if (Inst.getOperand(0).getReg() == Mips::ZERO ||
5644         Inst.getOperand(0).getReg() == Mips::ZERO_64)
5645       return Match_RequiresNoZeroRegister;
5646     return Match_Success;
5647   case Mips::BGEC:    case Mips::BGEC_MMR6:
5648   case Mips::BLTC:    case Mips::BLTC_MMR6:
5649   case Mips::BGEUC:   case Mips::BGEUC_MMR6:
5650   case Mips::BLTUC:   case Mips::BLTUC_MMR6:
5651   case Mips::BEQC:    case Mips::BEQC_MMR6:
5652   case Mips::BNEC:    case Mips::BNEC_MMR6:
5653   case Mips::BGEC64:
5654   case Mips::BLTC64:
5655   case Mips::BGEUC64:
5656   case Mips::BLTUC64:
5657   case Mips::BEQC64:
5658   case Mips::BNEC64:
5659     if (Inst.getOperand(0).getReg() == Mips::ZERO ||
5660         Inst.getOperand(0).getReg() == Mips::ZERO_64)
5661       return Match_RequiresNoZeroRegister;
5662     if (Inst.getOperand(1).getReg() == Mips::ZERO ||
5663         Inst.getOperand(1).getReg() == Mips::ZERO_64)
5664       return Match_RequiresNoZeroRegister;
5665     if (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg())
5666       return Match_RequiresDifferentOperands;
5667     return Match_Success;
5668   case Mips::DINS: {
5669     assert(Inst.getOperand(2).isImm() && Inst.getOperand(3).isImm() &&
5670            "Operands must be immediates for dins!");
5671     const signed Pos = Inst.getOperand(2).getImm();
5672     const signed Size = Inst.getOperand(3).getImm();
5673     if ((0 > (Pos + Size)) || ((Pos + Size) > 32))
5674       return Match_RequiresPosSizeRange0_32;
5675     return Match_Success;
5676   }
5677   case Mips::DINSM:
5678   case Mips::DINSU: {
5679     assert(Inst.getOperand(2).isImm() && Inst.getOperand(3).isImm() &&
5680            "Operands must be immediates for dinsm/dinsu!");
5681     const signed Pos = Inst.getOperand(2).getImm();
5682     const signed Size = Inst.getOperand(3).getImm();
5683     if ((32 >= (Pos + Size)) || ((Pos + Size) > 64))
5684       return Match_RequiresPosSizeRange33_64;
5685     return Match_Success;
5686   }
5687   case Mips::DEXT: {
5688     assert(Inst.getOperand(2).isImm() && Inst.getOperand(3).isImm() &&
5689            "Operands must be immediates for DEXTM!");
5690     const signed Pos = Inst.getOperand(2).getImm();
5691     const signed Size = Inst.getOperand(3).getImm();
5692     if ((1 > (Pos + Size)) || ((Pos + Size) > 63))
5693       return Match_RequiresPosSizeUImm6;
5694     return Match_Success;
5695   }
5696   case Mips::DEXTM:
5697   case Mips::DEXTU: {
5698     assert(Inst.getOperand(2).isImm() && Inst.getOperand(3).isImm() &&
5699            "Operands must be immediates for dextm/dextu!");
5700     const signed Pos = Inst.getOperand(2).getImm();
5701     const signed Size = Inst.getOperand(3).getImm();
5702     if ((32 > (Pos + Size)) || ((Pos + Size) > 64))
5703       return Match_RequiresPosSizeRange33_64;
5704     return Match_Success;
5705   }
5706   case Mips::CRC32B: case Mips::CRC32CB:
5707   case Mips::CRC32H: case Mips::CRC32CH:
5708   case Mips::CRC32W: case Mips::CRC32CW:
5709   case Mips::CRC32D: case Mips::CRC32CD:
5710     if (Inst.getOperand(0).getReg() != Inst.getOperand(2).getReg())
5711       return Match_RequiresSameSrcAndDst;
5712     return Match_Success;
5713   }
5714 
5715   uint64_t TSFlags = getInstDesc(Inst.getOpcode()).TSFlags;
5716   if ((TSFlags & MipsII::HasFCCRegOperand) &&
5717       (Inst.getOperand(0).getReg() != Mips::FCC0) && !hasEightFccRegisters())
5718     return Match_NoFCCRegisterForCurrentISA;
5719 
5720   return Match_Success;
5721 
5722 }
5723 
5724 static SMLoc RefineErrorLoc(const SMLoc Loc, const OperandVector &Operands,
5725                             uint64_t ErrorInfo) {
5726   if (ErrorInfo != ~0ULL && ErrorInfo < Operands.size()) {
5727     SMLoc ErrorLoc = Operands[ErrorInfo]->getStartLoc();
5728     if (ErrorLoc == SMLoc())
5729       return Loc;
5730     return ErrorLoc;
5731   }
5732   return Loc;
5733 }
5734 
5735 bool MipsAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
5736                                             OperandVector &Operands,
5737                                             MCStreamer &Out,
5738                                             uint64_t &ErrorInfo,
5739                                             bool MatchingInlineAsm) {
5740   MCInst Inst;
5741   unsigned MatchResult =
5742       MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
5743 
5744   switch (MatchResult) {
5745   case Match_Success:
5746     if (processInstruction(Inst, IDLoc, Out, STI))
5747       return true;
5748     return false;
5749   case Match_MissingFeature:
5750     Error(IDLoc, "instruction requires a CPU feature not currently enabled");
5751     return true;
5752   case Match_InvalidOperand: {
5753     SMLoc ErrorLoc = IDLoc;
5754     if (ErrorInfo != ~0ULL) {
5755       if (ErrorInfo >= Operands.size())
5756         return Error(IDLoc, "too few operands for instruction");
5757 
5758       ErrorLoc = Operands[ErrorInfo]->getStartLoc();
5759       if (ErrorLoc == SMLoc())
5760         ErrorLoc = IDLoc;
5761     }
5762 
5763     return Error(ErrorLoc, "invalid operand for instruction");
5764   }
5765   case Match_NonZeroOperandForSync:
5766     return Error(IDLoc,
5767                  "s-type must be zero or unspecified for pre-MIPS32 ISAs");
5768   case Match_NonZeroOperandForMTCX:
5769     return Error(IDLoc, "selector must be zero for pre-MIPS32 ISAs");
5770   case Match_MnemonicFail:
5771     return Error(IDLoc, "invalid instruction");
5772   case Match_RequiresDifferentSrcAndDst:
5773     return Error(IDLoc, "source and destination must be different");
5774   case Match_RequiresDifferentOperands:
5775     return Error(IDLoc, "registers must be different");
5776   case Match_RequiresNoZeroRegister:
5777     return Error(IDLoc, "invalid operand ($zero) for instruction");
5778   case Match_RequiresSameSrcAndDst:
5779     return Error(IDLoc, "source and destination must match");
5780   case Match_NoFCCRegisterForCurrentISA:
5781     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5782                  "non-zero fcc register doesn't exist in current ISA level");
5783   case Match_Immz:
5784     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), "expected '0'");
5785   case Match_UImm1_0:
5786     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5787                  "expected 1-bit unsigned immediate");
5788   case Match_UImm2_0:
5789     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5790                  "expected 2-bit unsigned immediate");
5791   case Match_UImm2_1:
5792     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5793                  "expected immediate in range 1 .. 4");
5794   case Match_UImm3_0:
5795     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5796                  "expected 3-bit unsigned immediate");
5797   case Match_UImm4_0:
5798     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5799                  "expected 4-bit unsigned immediate");
5800   case Match_SImm4_0:
5801     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5802                  "expected 4-bit signed immediate");
5803   case Match_UImm5_0:
5804     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5805                  "expected 5-bit unsigned immediate");
5806   case Match_SImm5_0:
5807     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5808                  "expected 5-bit signed immediate");
5809   case Match_UImm5_1:
5810     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5811                  "expected immediate in range 1 .. 32");
5812   case Match_UImm5_32:
5813     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5814                  "expected immediate in range 32 .. 63");
5815   case Match_UImm5_33:
5816     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5817                  "expected immediate in range 33 .. 64");
5818   case Match_UImm5_0_Report_UImm6:
5819     // This is used on UImm5 operands that have a corresponding UImm5_32
5820     // operand to avoid confusing the user.
5821     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5822                  "expected 6-bit unsigned immediate");
5823   case Match_UImm5_Lsl2:
5824     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5825                  "expected both 7-bit unsigned immediate and multiple of 4");
5826   case Match_UImmRange2_64:
5827     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5828                  "expected immediate in range 2 .. 64");
5829   case Match_UImm6_0:
5830     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5831                  "expected 6-bit unsigned immediate");
5832   case Match_UImm6_Lsl2:
5833     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5834                  "expected both 8-bit unsigned immediate and multiple of 4");
5835   case Match_SImm6_0:
5836     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5837                  "expected 6-bit signed immediate");
5838   case Match_UImm7_0:
5839     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5840                  "expected 7-bit unsigned immediate");
5841   case Match_UImm7_N1:
5842     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5843                  "expected immediate in range -1 .. 126");
5844   case Match_SImm7_Lsl2:
5845     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5846                  "expected both 9-bit signed immediate and multiple of 4");
5847   case Match_UImm8_0:
5848     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5849                  "expected 8-bit unsigned immediate");
5850   case Match_UImm10_0:
5851     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5852                  "expected 10-bit unsigned immediate");
5853   case Match_SImm10_0:
5854     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5855                  "expected 10-bit signed immediate");
5856   case Match_SImm11_0:
5857     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5858                  "expected 11-bit signed immediate");
5859   case Match_UImm16:
5860   case Match_UImm16_Relaxed:
5861   case Match_UImm16_AltRelaxed:
5862     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5863                  "expected 16-bit unsigned immediate");
5864   case Match_SImm16:
5865   case Match_SImm16_Relaxed:
5866     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5867                  "expected 16-bit signed immediate");
5868   case Match_SImm19_Lsl2:
5869     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5870                  "expected both 19-bit signed immediate and multiple of 4");
5871   case Match_UImm20_0:
5872     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5873                  "expected 20-bit unsigned immediate");
5874   case Match_UImm26_0:
5875     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5876                  "expected 26-bit unsigned immediate");
5877   case Match_SImm32:
5878   case Match_SImm32_Relaxed:
5879     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5880                  "expected 32-bit signed immediate");
5881   case Match_UImm32_Coerced:
5882     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5883                  "expected 32-bit immediate");
5884   case Match_MemSImm9:
5885     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5886                  "expected memory with 9-bit signed offset");
5887   case Match_MemSImm10:
5888     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5889                  "expected memory with 10-bit signed offset");
5890   case Match_MemSImm10Lsl1:
5891     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5892                  "expected memory with 11-bit signed offset and multiple of 2");
5893   case Match_MemSImm10Lsl2:
5894     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5895                  "expected memory with 12-bit signed offset and multiple of 4");
5896   case Match_MemSImm10Lsl3:
5897     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5898                  "expected memory with 13-bit signed offset and multiple of 8");
5899   case Match_MemSImm11:
5900     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5901                  "expected memory with 11-bit signed offset");
5902   case Match_MemSImm12:
5903     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5904                  "expected memory with 12-bit signed offset");
5905   case Match_MemSImm16:
5906     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5907                  "expected memory with 16-bit signed offset");
5908   case Match_MemSImmPtr:
5909     return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
5910                  "expected memory with 32-bit signed offset");
5911   case Match_RequiresPosSizeRange0_32: {
5912     SMLoc ErrorStart = Operands[3]->getStartLoc();
5913     SMLoc ErrorEnd = Operands[4]->getEndLoc();
5914     return Error(ErrorStart, "size plus position are not in the range 0 .. 32",
5915                  SMRange(ErrorStart, ErrorEnd));
5916     }
5917   case Match_RequiresPosSizeUImm6: {
5918     SMLoc ErrorStart = Operands[3]->getStartLoc();
5919     SMLoc ErrorEnd = Operands[4]->getEndLoc();
5920     return Error(ErrorStart, "size plus position are not in the range 1 .. 63",
5921                  SMRange(ErrorStart, ErrorEnd));
5922     }
5923   case Match_RequiresPosSizeRange33_64: {
5924     SMLoc ErrorStart = Operands[3]->getStartLoc();
5925     SMLoc ErrorEnd = Operands[4]->getEndLoc();
5926     return Error(ErrorStart, "size plus position are not in the range 33 .. 64",
5927                  SMRange(ErrorStart, ErrorEnd));
5928     }
5929   }
5930 
5931   llvm_unreachable("Implement any new match types added!");
5932 }
5933 
5934 void MipsAsmParser::warnIfRegIndexIsAT(unsigned RegIndex, SMLoc Loc) {
5935   if (RegIndex != 0 && AssemblerOptions.back()->getATRegIndex() == RegIndex)
5936     Warning(Loc, "used $at (currently $" + Twine(RegIndex) +
5937                      ") without \".set noat\"");
5938 }
5939 
5940 void MipsAsmParser::warnIfNoMacro(SMLoc Loc) {
5941   if (!AssemblerOptions.back()->isMacro())
5942     Warning(Loc, "macro instruction expanded into multiple instructions");
5943 }
5944 
5945 void MipsAsmParser::ConvertXWPOperands(MCInst &Inst,
5946                                        const OperandVector &Operands) {
5947   assert(
5948       (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM) &&
5949       "Unexpected instruction!");
5950   ((MipsOperand &)*Operands[1]).addGPR32ZeroAsmRegOperands(Inst, 1);
5951   int NextReg = nextReg(((MipsOperand &)*Operands[1]).getGPR32Reg());
5952   Inst.addOperand(MCOperand::createReg(NextReg));
5953   ((MipsOperand &)*Operands[2]).addMemOperands(Inst, 2);
5954 }
5955 
5956 void
5957 MipsAsmParser::printWarningWithFixIt(const Twine &Msg, const Twine &FixMsg,
5958                                      SMRange Range, bool ShowColors) {
5959   getSourceManager().PrintMessage(Range.Start, SourceMgr::DK_Warning, Msg,
5960                                   Range, SMFixIt(Range, FixMsg),
5961                                   ShowColors);
5962 }
5963 
5964 int MipsAsmParser::matchCPURegisterName(StringRef Name) {
5965   int CC;
5966 
5967   CC = StringSwitch<unsigned>(Name)
5968            .Case("zero", 0)
5969            .Cases("at", "AT", 1)
5970            .Case("a0", 4)
5971            .Case("a1", 5)
5972            .Case("a2", 6)
5973            .Case("a3", 7)
5974            .Case("v0", 2)
5975            .Case("v1", 3)
5976            .Case("s0", 16)
5977            .Case("s1", 17)
5978            .Case("s2", 18)
5979            .Case("s3", 19)
5980            .Case("s4", 20)
5981            .Case("s5", 21)
5982            .Case("s6", 22)
5983            .Case("s7", 23)
5984            .Case("k0", 26)
5985            .Case("k1", 27)
5986            .Case("gp", 28)
5987            .Case("sp", 29)
5988            .Case("fp", 30)
5989            .Case("s8", 30)
5990            .Case("ra", 31)
5991            .Case("t0", 8)
5992            .Case("t1", 9)
5993            .Case("t2", 10)
5994            .Case("t3", 11)
5995            .Case("t4", 12)
5996            .Case("t5", 13)
5997            .Case("t6", 14)
5998            .Case("t7", 15)
5999            .Case("t8", 24)
6000            .Case("t9", 25)
6001            .Default(-1);
6002 
6003   if (!(isABI_N32() || isABI_N64()))
6004     return CC;
6005 
6006   if (12 <= CC && CC <= 15) {
6007     // Name is one of t4-t7
6008     AsmToken RegTok = getLexer().peekTok();
6009     SMRange RegRange = RegTok.getLocRange();
6010 
6011     StringRef FixedName = StringSwitch<StringRef>(Name)
6012                               .Case("t4", "t0")
6013                               .Case("t5", "t1")
6014                               .Case("t6", "t2")
6015                               .Case("t7", "t3")
6016                               .Default("");
6017     assert(FixedName != "" &&  "Register name is not one of t4-t7.");
6018 
6019     printWarningWithFixIt("register names $t4-$t7 are only available in O32.",
6020                           "Did you mean $" + FixedName + "?", RegRange);
6021   }
6022 
6023   // Although SGI documentation just cuts out t0-t3 for n32/n64,
6024   // GNU pushes the values of t0-t3 to override the o32/o64 values for t4-t7
6025   // We are supporting both cases, so for t0-t3 we'll just push them to t4-t7.
6026   if (8 <= CC && CC <= 11)
6027     CC += 4;
6028 
6029   if (CC == -1)
6030     CC = StringSwitch<unsigned>(Name)
6031              .Case("a4", 8)
6032              .Case("a5", 9)
6033              .Case("a6", 10)
6034              .Case("a7", 11)
6035              .Case("kt0", 26)
6036              .Case("kt1", 27)
6037              .Default(-1);
6038 
6039   return CC;
6040 }
6041 
6042 int MipsAsmParser::matchHWRegsRegisterName(StringRef Name) {
6043   int CC;
6044 
6045   CC = StringSwitch<unsigned>(Name)
6046             .Case("hwr_cpunum", 0)
6047             .Case("hwr_synci_step", 1)
6048             .Case("hwr_cc", 2)
6049             .Case("hwr_ccres", 3)
6050             .Case("hwr_ulr", 29)
6051             .Default(-1);
6052 
6053   return CC;
6054 }
6055 
6056 int MipsAsmParser::matchFPURegisterName(StringRef Name) {
6057   if (Name[0] == 'f') {
6058     StringRef NumString = Name.substr(1);
6059     unsigned IntVal;
6060     if (NumString.getAsInteger(10, IntVal))
6061       return -1;     // This is not an integer.
6062     if (IntVal > 31) // Maximum index for fpu register.
6063       return -1;
6064     return IntVal;
6065   }
6066   return -1;
6067 }
6068 
6069 int MipsAsmParser::matchFCCRegisterName(StringRef Name) {
6070   if (Name.startswith("fcc")) {
6071     StringRef NumString = Name.substr(3);
6072     unsigned IntVal;
6073     if (NumString.getAsInteger(10, IntVal))
6074       return -1;    // This is not an integer.
6075     if (IntVal > 7) // There are only 8 fcc registers.
6076       return -1;
6077     return IntVal;
6078   }
6079   return -1;
6080 }
6081 
6082 int MipsAsmParser::matchACRegisterName(StringRef Name) {
6083   if (Name.startswith("ac")) {
6084     StringRef NumString = Name.substr(2);
6085     unsigned IntVal;
6086     if (NumString.getAsInteger(10, IntVal))
6087       return -1;    // This is not an integer.
6088     if (IntVal > 3) // There are only 3 acc registers.
6089       return -1;
6090     return IntVal;
6091   }
6092   return -1;
6093 }
6094 
6095 int MipsAsmParser::matchMSA128RegisterName(StringRef Name) {
6096   unsigned IntVal;
6097 
6098   if (Name.front() != 'w' || Name.drop_front(1).getAsInteger(10, IntVal))
6099     return -1;
6100 
6101   if (IntVal > 31)
6102     return -1;
6103 
6104   return IntVal;
6105 }
6106 
6107 int MipsAsmParser::matchMSA128CtrlRegisterName(StringRef Name) {
6108   int CC;
6109 
6110   CC = StringSwitch<unsigned>(Name)
6111            .Case("msair", 0)
6112            .Case("msacsr", 1)
6113            .Case("msaaccess", 2)
6114            .Case("msasave", 3)
6115            .Case("msamodify", 4)
6116            .Case("msarequest", 5)
6117            .Case("msamap", 6)
6118            .Case("msaunmap", 7)
6119            .Default(-1);
6120 
6121   return CC;
6122 }
6123 
6124 bool MipsAsmParser::canUseATReg() {
6125   return AssemblerOptions.back()->getATRegIndex() != 0;
6126 }
6127 
6128 unsigned MipsAsmParser::getATReg(SMLoc Loc) {
6129   unsigned ATIndex = AssemblerOptions.back()->getATRegIndex();
6130   if (ATIndex == 0) {
6131     reportParseError(Loc,
6132                      "pseudo-instruction requires $at, which is not available");
6133     return 0;
6134   }
6135   unsigned AT = getReg(
6136       (isGP64bit()) ? Mips::GPR64RegClassID : Mips::GPR32RegClassID, ATIndex);
6137   return AT;
6138 }
6139 
6140 unsigned MipsAsmParser::getReg(int RC, int RegNo) {
6141   return *(getContext().getRegisterInfo()->getRegClass(RC).begin() + RegNo);
6142 }
6143 
6144 bool MipsAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
6145   MCAsmParser &Parser = getParser();
6146   LLVM_DEBUG(dbgs() << "parseOperand\n");
6147 
6148   // Check if the current operand has a custom associated parser, if so, try to
6149   // custom parse the operand, or fallback to the general approach.
6150   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
6151   if (ResTy == MatchOperand_Success)
6152     return false;
6153   // If there wasn't a custom match, try the generic matcher below. Otherwise,
6154   // there was a match, but an error occurred, in which case, just return that
6155   // the operand parsing failed.
6156   if (ResTy == MatchOperand_ParseFail)
6157     return true;
6158 
6159   LLVM_DEBUG(dbgs() << ".. Generic Parser\n");
6160 
6161   switch (getLexer().getKind()) {
6162   case AsmToken::Dollar: {
6163     // Parse the register.
6164     SMLoc S = Parser.getTok().getLoc();
6165 
6166     // Almost all registers have been parsed by custom parsers. There is only
6167     // one exception to this. $zero (and it's alias $0) will reach this point
6168     // for div, divu, and similar instructions because it is not an operand
6169     // to the instruction definition but an explicit register. Special case
6170     // this situation for now.
6171     if (parseAnyRegister(Operands) != MatchOperand_NoMatch)
6172       return false;
6173 
6174     // Maybe it is a symbol reference.
6175     StringRef Identifier;
6176     if (Parser.parseIdentifier(Identifier))
6177       return true;
6178 
6179     SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
6180     MCSymbol *Sym = getContext().getOrCreateSymbol("$" + Identifier);
6181     // Otherwise create a symbol reference.
6182     const MCExpr *Res =
6183         MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
6184 
6185     Operands.push_back(MipsOperand::CreateImm(Res, S, E, *this));
6186     return false;
6187   }
6188   default: {
6189     LLVM_DEBUG(dbgs() << ".. generic integer expression\n");
6190 
6191     const MCExpr *Expr;
6192     SMLoc S = Parser.getTok().getLoc(); // Start location of the operand.
6193     if (getParser().parseExpression(Expr))
6194       return true;
6195 
6196     SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
6197 
6198     Operands.push_back(MipsOperand::CreateImm(Expr, S, E, *this));
6199     return false;
6200   }
6201   } // switch(getLexer().getKind())
6202   return true;
6203 }
6204 
6205 bool MipsAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
6206                                   SMLoc &EndLoc) {
6207   return tryParseRegister(RegNo, StartLoc, EndLoc) != MatchOperand_Success;
6208 }
6209 
6210 OperandMatchResultTy MipsAsmParser::tryParseRegister(unsigned &RegNo,
6211                                                      SMLoc &StartLoc,
6212                                                      SMLoc &EndLoc) {
6213   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Operands;
6214   OperandMatchResultTy ResTy = parseAnyRegister(Operands);
6215   if (ResTy == MatchOperand_Success) {
6216     assert(Operands.size() == 1);
6217     MipsOperand &Operand = static_cast<MipsOperand &>(*Operands.front());
6218     StartLoc = Operand.getStartLoc();
6219     EndLoc = Operand.getEndLoc();
6220 
6221     // AFAIK, we only support numeric registers and named GPR's in CFI
6222     // directives.
6223     // Don't worry about eating tokens before failing. Using an unrecognised
6224     // register is a parse error.
6225     if (Operand.isGPRAsmReg()) {
6226       // Resolve to GPR32 or GPR64 appropriately.
6227       RegNo = isGP64bit() ? Operand.getGPR64Reg() : Operand.getGPR32Reg();
6228     }
6229 
6230     return (RegNo == (unsigned)-1) ? MatchOperand_NoMatch
6231                                    : MatchOperand_Success;
6232   }
6233 
6234   assert(Operands.size() == 0);
6235   return (RegNo == (unsigned)-1) ? MatchOperand_NoMatch : MatchOperand_Success;
6236 }
6237 
6238 bool MipsAsmParser::parseMemOffset(const MCExpr *&Res, bool isParenExpr) {
6239   SMLoc S;
6240 
6241   if (isParenExpr)
6242     return getParser().parseParenExprOfDepth(0, Res, S);
6243   return getParser().parseExpression(Res);
6244 }
6245 
6246 OperandMatchResultTy
6247 MipsAsmParser::parseMemOperand(OperandVector &Operands) {
6248   MCAsmParser &Parser = getParser();
6249   LLVM_DEBUG(dbgs() << "parseMemOperand\n");
6250   const MCExpr *IdVal = nullptr;
6251   SMLoc S;
6252   bool isParenExpr = false;
6253   OperandMatchResultTy Res = MatchOperand_NoMatch;
6254   // First operand is the offset.
6255   S = Parser.getTok().getLoc();
6256 
6257   if (getLexer().getKind() == AsmToken::LParen) {
6258     Parser.Lex();
6259     isParenExpr = true;
6260   }
6261 
6262   if (getLexer().getKind() != AsmToken::Dollar) {
6263     if (parseMemOffset(IdVal, isParenExpr))
6264       return MatchOperand_ParseFail;
6265 
6266     const AsmToken &Tok = Parser.getTok(); // Get the next token.
6267     if (Tok.isNot(AsmToken::LParen)) {
6268       MipsOperand &Mnemonic = static_cast<MipsOperand &>(*Operands[0]);
6269       if (Mnemonic.getToken() == "la" || Mnemonic.getToken() == "dla") {
6270         SMLoc E =
6271             SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
6272         Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
6273         return MatchOperand_Success;
6274       }
6275       if (Tok.is(AsmToken::EndOfStatement)) {
6276         SMLoc E =
6277             SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
6278 
6279         // Zero register assumed, add a memory operand with ZERO as its base.
6280         // "Base" will be managed by k_Memory.
6281         auto Base = MipsOperand::createGPRReg(
6282             0, "0", getContext().getRegisterInfo(), S, E, *this);
6283         Operands.push_back(
6284             MipsOperand::CreateMem(std::move(Base), IdVal, S, E, *this));
6285         return MatchOperand_Success;
6286       }
6287       MCBinaryExpr::Opcode Opcode;
6288       // GAS and LLVM treat comparison operators different. GAS will generate -1
6289       // or 0, while LLVM will generate 0 or 1. Since a comparsion operator is
6290       // highly unlikely to be found in a memory offset expression, we don't
6291       // handle them.
6292       switch (Tok.getKind()) {
6293       case AsmToken::Plus:
6294         Opcode = MCBinaryExpr::Add;
6295         Parser.Lex();
6296         break;
6297       case AsmToken::Minus:
6298         Opcode = MCBinaryExpr::Sub;
6299         Parser.Lex();
6300         break;
6301       case AsmToken::Star:
6302         Opcode = MCBinaryExpr::Mul;
6303         Parser.Lex();
6304         break;
6305       case AsmToken::Pipe:
6306         Opcode = MCBinaryExpr::Or;
6307         Parser.Lex();
6308         break;
6309       case AsmToken::Amp:
6310         Opcode = MCBinaryExpr::And;
6311         Parser.Lex();
6312         break;
6313       case AsmToken::LessLess:
6314         Opcode = MCBinaryExpr::Shl;
6315         Parser.Lex();
6316         break;
6317       case AsmToken::GreaterGreater:
6318         Opcode = MCBinaryExpr::LShr;
6319         Parser.Lex();
6320         break;
6321       case AsmToken::Caret:
6322         Opcode = MCBinaryExpr::Xor;
6323         Parser.Lex();
6324         break;
6325       case AsmToken::Slash:
6326         Opcode = MCBinaryExpr::Div;
6327         Parser.Lex();
6328         break;
6329       case AsmToken::Percent:
6330         Opcode = MCBinaryExpr::Mod;
6331         Parser.Lex();
6332         break;
6333       default:
6334         Error(Parser.getTok().getLoc(), "'(' or expression expected");
6335         return MatchOperand_ParseFail;
6336       }
6337       const MCExpr * NextExpr;
6338       if (getParser().parseExpression(NextExpr))
6339         return MatchOperand_ParseFail;
6340       IdVal = MCBinaryExpr::create(Opcode, IdVal, NextExpr, getContext());
6341     }
6342 
6343     Parser.Lex(); // Eat the '(' token.
6344   }
6345 
6346   Res = parseAnyRegister(Operands);
6347   if (Res != MatchOperand_Success)
6348     return Res;
6349 
6350   if (Parser.getTok().isNot(AsmToken::RParen)) {
6351     Error(Parser.getTok().getLoc(), "')' expected");
6352     return MatchOperand_ParseFail;
6353   }
6354 
6355   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
6356 
6357   Parser.Lex(); // Eat the ')' token.
6358 
6359   if (!IdVal)
6360     IdVal = MCConstantExpr::create(0, getContext());
6361 
6362   // Replace the register operand with the memory operand.
6363   std::unique_ptr<MipsOperand> op(
6364       static_cast<MipsOperand *>(Operands.back().release()));
6365   // Remove the register from the operands.
6366   // "op" will be managed by k_Memory.
6367   Operands.pop_back();
6368   // Add the memory operand.
6369   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(IdVal)) {
6370     int64_t Imm;
6371     if (IdVal->evaluateAsAbsolute(Imm))
6372       IdVal = MCConstantExpr::create(Imm, getContext());
6373     else if (BE->getLHS()->getKind() != MCExpr::SymbolRef)
6374       IdVal = MCBinaryExpr::create(BE->getOpcode(), BE->getRHS(), BE->getLHS(),
6375                                    getContext());
6376   }
6377 
6378   Operands.push_back(MipsOperand::CreateMem(std::move(op), IdVal, S, E, *this));
6379   return MatchOperand_Success;
6380 }
6381 
6382 bool MipsAsmParser::searchSymbolAlias(OperandVector &Operands) {
6383   MCAsmParser &Parser = getParser();
6384   MCSymbol *Sym = getContext().lookupSymbol(Parser.getTok().getIdentifier());
6385   if (!Sym)
6386     return false;
6387 
6388   SMLoc S = Parser.getTok().getLoc();
6389   if (Sym->isVariable()) {
6390     const MCExpr *Expr = Sym->getVariableValue();
6391     if (Expr->getKind() == MCExpr::SymbolRef) {
6392       const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
6393       StringRef DefSymbol = Ref->getSymbol().getName();
6394       if (DefSymbol.startswith("$")) {
6395         OperandMatchResultTy ResTy =
6396             matchAnyRegisterNameWithoutDollar(Operands, DefSymbol.substr(1), S);
6397         if (ResTy == MatchOperand_Success) {
6398           Parser.Lex();
6399           return true;
6400         }
6401         if (ResTy == MatchOperand_ParseFail)
6402           llvm_unreachable("Should never ParseFail");
6403       }
6404     }
6405   } else if (Sym->isUnset()) {
6406     // If symbol is unset, it might be created in the `parseSetAssignment`
6407     // routine as an alias for a numeric register name.
6408     // Lookup in the aliases list.
6409     auto Entry = RegisterSets.find(Sym->getName());
6410     if (Entry != RegisterSets.end()) {
6411       OperandMatchResultTy ResTy =
6412           matchAnyRegisterWithoutDollar(Operands, Entry->getValue(), S);
6413       if (ResTy == MatchOperand_Success) {
6414         Parser.Lex();
6415         return true;
6416       }
6417     }
6418   }
6419 
6420   return false;
6421 }
6422 
6423 OperandMatchResultTy
6424 MipsAsmParser::matchAnyRegisterNameWithoutDollar(OperandVector &Operands,
6425                                                  StringRef Identifier,
6426                                                  SMLoc S) {
6427   int Index = matchCPURegisterName(Identifier);
6428   if (Index != -1) {
6429     Operands.push_back(MipsOperand::createGPRReg(
6430         Index, Identifier, getContext().getRegisterInfo(), S,
6431         getLexer().getLoc(), *this));
6432     return MatchOperand_Success;
6433   }
6434 
6435   Index = matchHWRegsRegisterName(Identifier);
6436   if (Index != -1) {
6437     Operands.push_back(MipsOperand::createHWRegsReg(
6438         Index, Identifier, getContext().getRegisterInfo(), S,
6439         getLexer().getLoc(), *this));
6440     return MatchOperand_Success;
6441   }
6442 
6443   Index = matchFPURegisterName(Identifier);
6444   if (Index != -1) {
6445     Operands.push_back(MipsOperand::createFGRReg(
6446         Index, Identifier, getContext().getRegisterInfo(), S,
6447         getLexer().getLoc(), *this));
6448     return MatchOperand_Success;
6449   }
6450 
6451   Index = matchFCCRegisterName(Identifier);
6452   if (Index != -1) {
6453     Operands.push_back(MipsOperand::createFCCReg(
6454         Index, Identifier, getContext().getRegisterInfo(), S,
6455         getLexer().getLoc(), *this));
6456     return MatchOperand_Success;
6457   }
6458 
6459   Index = matchACRegisterName(Identifier);
6460   if (Index != -1) {
6461     Operands.push_back(MipsOperand::createACCReg(
6462         Index, Identifier, getContext().getRegisterInfo(), S,
6463         getLexer().getLoc(), *this));
6464     return MatchOperand_Success;
6465   }
6466 
6467   Index = matchMSA128RegisterName(Identifier);
6468   if (Index != -1) {
6469     Operands.push_back(MipsOperand::createMSA128Reg(
6470         Index, Identifier, getContext().getRegisterInfo(), S,
6471         getLexer().getLoc(), *this));
6472     return MatchOperand_Success;
6473   }
6474 
6475   Index = matchMSA128CtrlRegisterName(Identifier);
6476   if (Index != -1) {
6477     Operands.push_back(MipsOperand::createMSACtrlReg(
6478         Index, Identifier, getContext().getRegisterInfo(), S,
6479         getLexer().getLoc(), *this));
6480     return MatchOperand_Success;
6481   }
6482 
6483   return MatchOperand_NoMatch;
6484 }
6485 
6486 OperandMatchResultTy
6487 MipsAsmParser::matchAnyRegisterWithoutDollar(OperandVector &Operands,
6488                                              const AsmToken &Token, SMLoc S) {
6489   if (Token.is(AsmToken::Identifier)) {
6490     LLVM_DEBUG(dbgs() << ".. identifier\n");
6491     StringRef Identifier = Token.getIdentifier();
6492     OperandMatchResultTy ResTy =
6493         matchAnyRegisterNameWithoutDollar(Operands, Identifier, S);
6494     return ResTy;
6495   } else if (Token.is(AsmToken::Integer)) {
6496     LLVM_DEBUG(dbgs() << ".. integer\n");
6497     int64_t RegNum = Token.getIntVal();
6498     if (RegNum < 0 || RegNum > 31) {
6499       // Show the error, but treat invalid register
6500       // number as a normal one to continue parsing
6501       // and catch other possible errors.
6502       Error(getLexer().getLoc(), "invalid register number");
6503     }
6504     Operands.push_back(MipsOperand::createNumericReg(
6505         RegNum, Token.getString(), getContext().getRegisterInfo(), S,
6506         Token.getLoc(), *this));
6507     return MatchOperand_Success;
6508   }
6509 
6510   LLVM_DEBUG(dbgs() << Token.getKind() << "\n");
6511 
6512   return MatchOperand_NoMatch;
6513 }
6514 
6515 OperandMatchResultTy
6516 MipsAsmParser::matchAnyRegisterWithoutDollar(OperandVector &Operands, SMLoc S) {
6517   auto Token = getLexer().peekTok(false);
6518   return matchAnyRegisterWithoutDollar(Operands, Token, S);
6519 }
6520 
6521 OperandMatchResultTy
6522 MipsAsmParser::parseAnyRegister(OperandVector &Operands) {
6523   MCAsmParser &Parser = getParser();
6524   LLVM_DEBUG(dbgs() << "parseAnyRegister\n");
6525 
6526   auto Token = Parser.getTok();
6527 
6528   SMLoc S = Token.getLoc();
6529 
6530   if (Token.isNot(AsmToken::Dollar)) {
6531     LLVM_DEBUG(dbgs() << ".. !$ -> try sym aliasing\n");
6532     if (Token.is(AsmToken::Identifier)) {
6533       if (searchSymbolAlias(Operands))
6534         return MatchOperand_Success;
6535     }
6536     LLVM_DEBUG(dbgs() << ".. !symalias -> NoMatch\n");
6537     return MatchOperand_NoMatch;
6538   }
6539   LLVM_DEBUG(dbgs() << ".. $\n");
6540 
6541   OperandMatchResultTy ResTy = matchAnyRegisterWithoutDollar(Operands, S);
6542   if (ResTy == MatchOperand_Success) {
6543     Parser.Lex(); // $
6544     Parser.Lex(); // identifier
6545   }
6546   return ResTy;
6547 }
6548 
6549 OperandMatchResultTy
6550 MipsAsmParser::parseJumpTarget(OperandVector &Operands) {
6551   MCAsmParser &Parser = getParser();
6552   LLVM_DEBUG(dbgs() << "parseJumpTarget\n");
6553 
6554   SMLoc S = getLexer().getLoc();
6555 
6556   // Registers are a valid target and have priority over symbols.
6557   OperandMatchResultTy ResTy = parseAnyRegister(Operands);
6558   if (ResTy != MatchOperand_NoMatch)
6559     return ResTy;
6560 
6561   // Integers and expressions are acceptable
6562   const MCExpr *Expr = nullptr;
6563   if (Parser.parseExpression(Expr)) {
6564     // We have no way of knowing if a symbol was consumed so we must ParseFail
6565     return MatchOperand_ParseFail;
6566   }
6567   Operands.push_back(
6568       MipsOperand::CreateImm(Expr, S, getLexer().getLoc(), *this));
6569   return MatchOperand_Success;
6570 }
6571 
6572 OperandMatchResultTy
6573 MipsAsmParser::parseInvNum(OperandVector &Operands) {
6574   MCAsmParser &Parser = getParser();
6575   const MCExpr *IdVal;
6576   // If the first token is '$' we may have register operand. We have to reject
6577   // cases where it is not a register. Complicating the matter is that
6578   // register names are not reserved across all ABIs.
6579   // Peek past the dollar to see if it's a register name for this ABI.
6580   SMLoc S = Parser.getTok().getLoc();
6581   if (Parser.getTok().is(AsmToken::Dollar)) {
6582     return matchCPURegisterName(Parser.getLexer().peekTok().getString()) == -1
6583                ? MatchOperand_ParseFail
6584                : MatchOperand_NoMatch;
6585   }
6586   if (getParser().parseExpression(IdVal))
6587     return MatchOperand_ParseFail;
6588   const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(IdVal);
6589   if (!MCE)
6590     return MatchOperand_NoMatch;
6591   int64_t Val = MCE->getValue();
6592   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
6593   Operands.push_back(MipsOperand::CreateImm(
6594       MCConstantExpr::create(0 - Val, getContext()), S, E, *this));
6595   return MatchOperand_Success;
6596 }
6597 
6598 OperandMatchResultTy
6599 MipsAsmParser::parseRegisterList(OperandVector &Operands) {
6600   MCAsmParser &Parser = getParser();
6601   SmallVector<unsigned, 10> Regs;
6602   unsigned RegNo;
6603   unsigned PrevReg = Mips::NoRegister;
6604   bool RegRange = false;
6605   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> TmpOperands;
6606 
6607   if (Parser.getTok().isNot(AsmToken::Dollar))
6608     return MatchOperand_ParseFail;
6609 
6610   SMLoc S = Parser.getTok().getLoc();
6611   while (parseAnyRegister(TmpOperands) == MatchOperand_Success) {
6612     SMLoc E = getLexer().getLoc();
6613     MipsOperand &Reg = static_cast<MipsOperand &>(*TmpOperands.back());
6614     RegNo = isGP64bit() ? Reg.getGPR64Reg() : Reg.getGPR32Reg();
6615     if (RegRange) {
6616       // Remove last register operand because registers from register range
6617       // should be inserted first.
6618       if ((isGP64bit() && RegNo == Mips::RA_64) ||
6619           (!isGP64bit() && RegNo == Mips::RA)) {
6620         Regs.push_back(RegNo);
6621       } else {
6622         unsigned TmpReg = PrevReg + 1;
6623         while (TmpReg <= RegNo) {
6624           if ((((TmpReg < Mips::S0) || (TmpReg > Mips::S7)) && !isGP64bit()) ||
6625               (((TmpReg < Mips::S0_64) || (TmpReg > Mips::S7_64)) &&
6626                isGP64bit())) {
6627             Error(E, "invalid register operand");
6628             return MatchOperand_ParseFail;
6629           }
6630 
6631           PrevReg = TmpReg;
6632           Regs.push_back(TmpReg++);
6633         }
6634       }
6635 
6636       RegRange = false;
6637     } else {
6638       if ((PrevReg == Mips::NoRegister) &&
6639           ((isGP64bit() && (RegNo != Mips::S0_64) && (RegNo != Mips::RA_64)) ||
6640           (!isGP64bit() && (RegNo != Mips::S0) && (RegNo != Mips::RA)))) {
6641         Error(E, "$16 or $31 expected");
6642         return MatchOperand_ParseFail;
6643       } else if (!(((RegNo == Mips::FP || RegNo == Mips::RA ||
6644                     (RegNo >= Mips::S0 && RegNo <= Mips::S7)) &&
6645                     !isGP64bit()) ||
6646                    ((RegNo == Mips::FP_64 || RegNo == Mips::RA_64 ||
6647                     (RegNo >= Mips::S0_64 && RegNo <= Mips::S7_64)) &&
6648                     isGP64bit()))) {
6649         Error(E, "invalid register operand");
6650         return MatchOperand_ParseFail;
6651       } else if ((PrevReg != Mips::NoRegister) && (RegNo != PrevReg + 1) &&
6652                  ((RegNo != Mips::FP && RegNo != Mips::RA && !isGP64bit()) ||
6653                   (RegNo != Mips::FP_64 && RegNo != Mips::RA_64 &&
6654                    isGP64bit()))) {
6655         Error(E, "consecutive register numbers expected");
6656         return MatchOperand_ParseFail;
6657       }
6658 
6659       Regs.push_back(RegNo);
6660     }
6661 
6662     if (Parser.getTok().is(AsmToken::Minus))
6663       RegRange = true;
6664 
6665     if (!Parser.getTok().isNot(AsmToken::Minus) &&
6666         !Parser.getTok().isNot(AsmToken::Comma)) {
6667       Error(E, "',' or '-' expected");
6668       return MatchOperand_ParseFail;
6669     }
6670 
6671     Lex(); // Consume comma or minus
6672     if (Parser.getTok().isNot(AsmToken::Dollar))
6673       break;
6674 
6675     PrevReg = RegNo;
6676   }
6677 
6678   SMLoc E = Parser.getTok().getLoc();
6679   Operands.push_back(MipsOperand::CreateRegList(Regs, S, E, *this));
6680   parseMemOperand(Operands);
6681   return MatchOperand_Success;
6682 }
6683 
6684 /// Sometimes (i.e. load/stores) the operand may be followed immediately by
6685 /// either this.
6686 /// ::= '(', register, ')'
6687 /// handle it before we iterate so we don't get tripped up by the lack of
6688 /// a comma.
6689 bool MipsAsmParser::parseParenSuffix(StringRef Name, OperandVector &Operands) {
6690   MCAsmParser &Parser = getParser();
6691   if (getLexer().is(AsmToken::LParen)) {
6692     Operands.push_back(
6693         MipsOperand::CreateToken("(", getLexer().getLoc(), *this));
6694     Parser.Lex();
6695     if (parseOperand(Operands, Name)) {
6696       SMLoc Loc = getLexer().getLoc();
6697       return Error(Loc, "unexpected token in argument list");
6698     }
6699     if (Parser.getTok().isNot(AsmToken::RParen)) {
6700       SMLoc Loc = getLexer().getLoc();
6701       return Error(Loc, "unexpected token, expected ')'");
6702     }
6703     Operands.push_back(
6704         MipsOperand::CreateToken(")", getLexer().getLoc(), *this));
6705     Parser.Lex();
6706   }
6707   return false;
6708 }
6709 
6710 /// Sometimes (i.e. in MSA) the operand may be followed immediately by
6711 /// either one of these.
6712 /// ::= '[', register, ']'
6713 /// ::= '[', integer, ']'
6714 /// handle it before we iterate so we don't get tripped up by the lack of
6715 /// a comma.
6716 bool MipsAsmParser::parseBracketSuffix(StringRef Name,
6717                                        OperandVector &Operands) {
6718   MCAsmParser &Parser = getParser();
6719   if (getLexer().is(AsmToken::LBrac)) {
6720     Operands.push_back(
6721         MipsOperand::CreateToken("[", getLexer().getLoc(), *this));
6722     Parser.Lex();
6723     if (parseOperand(Operands, Name)) {
6724       SMLoc Loc = getLexer().getLoc();
6725       return Error(Loc, "unexpected token in argument list");
6726     }
6727     if (Parser.getTok().isNot(AsmToken::RBrac)) {
6728       SMLoc Loc = getLexer().getLoc();
6729       return Error(Loc, "unexpected token, expected ']'");
6730     }
6731     Operands.push_back(
6732         MipsOperand::CreateToken("]", getLexer().getLoc(), *this));
6733     Parser.Lex();
6734   }
6735   return false;
6736 }
6737 
6738 static std::string MipsMnemonicSpellCheck(StringRef S, const FeatureBitset &FBS,
6739                                           unsigned VariantID = 0);
6740 
6741 bool MipsAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
6742                                      SMLoc NameLoc, OperandVector &Operands) {
6743   MCAsmParser &Parser = getParser();
6744   LLVM_DEBUG(dbgs() << "ParseInstruction\n");
6745 
6746   // We have reached first instruction, module directive are now forbidden.
6747   getTargetStreamer().forbidModuleDirective();
6748 
6749   // Check if we have valid mnemonic
6750   if (!mnemonicIsValid(Name, 0)) {
6751     FeatureBitset FBS = ComputeAvailableFeatures(getSTI().getFeatureBits());
6752     std::string Suggestion = MipsMnemonicSpellCheck(Name, FBS);
6753     return Error(NameLoc, "unknown instruction" + Suggestion);
6754   }
6755   // First operand in MCInst is instruction mnemonic.
6756   Operands.push_back(MipsOperand::CreateToken(Name, NameLoc, *this));
6757 
6758   // Read the remaining operands.
6759   if (getLexer().isNot(AsmToken::EndOfStatement)) {
6760     // Read the first operand.
6761     if (parseOperand(Operands, Name)) {
6762       SMLoc Loc = getLexer().getLoc();
6763       return Error(Loc, "unexpected token in argument list");
6764     }
6765     if (getLexer().is(AsmToken::LBrac) && parseBracketSuffix(Name, Operands))
6766       return true;
6767     // AFAIK, parenthesis suffixes are never on the first operand
6768 
6769     while (getLexer().is(AsmToken::Comma)) {
6770       Parser.Lex(); // Eat the comma.
6771       // Parse and remember the operand.
6772       if (parseOperand(Operands, Name)) {
6773         SMLoc Loc = getLexer().getLoc();
6774         return Error(Loc, "unexpected token in argument list");
6775       }
6776       // Parse bracket and parenthesis suffixes before we iterate
6777       if (getLexer().is(AsmToken::LBrac)) {
6778         if (parseBracketSuffix(Name, Operands))
6779           return true;
6780       } else if (getLexer().is(AsmToken::LParen) &&
6781                  parseParenSuffix(Name, Operands))
6782         return true;
6783     }
6784   }
6785   if (getLexer().isNot(AsmToken::EndOfStatement)) {
6786     SMLoc Loc = getLexer().getLoc();
6787     return Error(Loc, "unexpected token in argument list");
6788   }
6789   Parser.Lex(); // Consume the EndOfStatement.
6790   return false;
6791 }
6792 
6793 // FIXME: Given that these have the same name, these should both be
6794 // consistent on affecting the Parser.
6795 bool MipsAsmParser::reportParseError(Twine ErrorMsg) {
6796   SMLoc Loc = getLexer().getLoc();
6797   return Error(Loc, ErrorMsg);
6798 }
6799 
6800 bool MipsAsmParser::reportParseError(SMLoc Loc, Twine ErrorMsg) {
6801   return Error(Loc, ErrorMsg);
6802 }
6803 
6804 bool MipsAsmParser::parseSetNoAtDirective() {
6805   MCAsmParser &Parser = getParser();
6806   // Line should look like: ".set noat".
6807 
6808   // Set the $at register to $0.
6809   AssemblerOptions.back()->setATRegIndex(0);
6810 
6811   Parser.Lex(); // Eat "noat".
6812 
6813   // If this is not the end of the statement, report an error.
6814   if (getLexer().isNot(AsmToken::EndOfStatement)) {
6815     reportParseError("unexpected token, expected end of statement");
6816     return false;
6817   }
6818 
6819   getTargetStreamer().emitDirectiveSetNoAt();
6820   Parser.Lex(); // Consume the EndOfStatement.
6821   return false;
6822 }
6823 
6824 bool MipsAsmParser::parseSetAtDirective() {
6825   // Line can be: ".set at", which sets $at to $1
6826   //          or  ".set at=$reg", which sets $at to $reg.
6827   MCAsmParser &Parser = getParser();
6828   Parser.Lex(); // Eat "at".
6829 
6830   if (getLexer().is(AsmToken::EndOfStatement)) {
6831     // No register was specified, so we set $at to $1.
6832     AssemblerOptions.back()->setATRegIndex(1);
6833 
6834     getTargetStreamer().emitDirectiveSetAt();
6835     Parser.Lex(); // Consume the EndOfStatement.
6836     return false;
6837   }
6838 
6839   if (getLexer().isNot(AsmToken::Equal)) {
6840     reportParseError("unexpected token, expected equals sign");
6841     return false;
6842   }
6843   Parser.Lex(); // Eat "=".
6844 
6845   if (getLexer().isNot(AsmToken::Dollar)) {
6846     if (getLexer().is(AsmToken::EndOfStatement)) {
6847       reportParseError("no register specified");
6848       return false;
6849     } else {
6850       reportParseError("unexpected token, expected dollar sign '$'");
6851       return false;
6852     }
6853   }
6854   Parser.Lex(); // Eat "$".
6855 
6856   // Find out what "reg" is.
6857   unsigned AtRegNo;
6858   const AsmToken &Reg = Parser.getTok();
6859   if (Reg.is(AsmToken::Identifier)) {
6860     AtRegNo = matchCPURegisterName(Reg.getIdentifier());
6861   } else if (Reg.is(AsmToken::Integer)) {
6862     AtRegNo = Reg.getIntVal();
6863   } else {
6864     reportParseError("unexpected token, expected identifier or integer");
6865     return false;
6866   }
6867 
6868   // Check if $reg is a valid register. If it is, set $at to $reg.
6869   if (!AssemblerOptions.back()->setATRegIndex(AtRegNo)) {
6870     reportParseError("invalid register");
6871     return false;
6872   }
6873   Parser.Lex(); // Eat "reg".
6874 
6875   // If this is not the end of the statement, report an error.
6876   if (getLexer().isNot(AsmToken::EndOfStatement)) {
6877     reportParseError("unexpected token, expected end of statement");
6878     return false;
6879   }
6880 
6881   getTargetStreamer().emitDirectiveSetAtWithArg(AtRegNo);
6882 
6883   Parser.Lex(); // Consume the EndOfStatement.
6884   return false;
6885 }
6886 
6887 bool MipsAsmParser::parseSetReorderDirective() {
6888   MCAsmParser &Parser = getParser();
6889   Parser.Lex();
6890   // If this is not the end of the statement, report an error.
6891   if (getLexer().isNot(AsmToken::EndOfStatement)) {
6892     reportParseError("unexpected token, expected end of statement");
6893     return false;
6894   }
6895   AssemblerOptions.back()->setReorder();
6896   getTargetStreamer().emitDirectiveSetReorder();
6897   Parser.Lex(); // Consume the EndOfStatement.
6898   return false;
6899 }
6900 
6901 bool MipsAsmParser::parseSetNoReorderDirective() {
6902   MCAsmParser &Parser = getParser();
6903   Parser.Lex();
6904   // If this is not the end of the statement, report an error.
6905   if (getLexer().isNot(AsmToken::EndOfStatement)) {
6906     reportParseError("unexpected token, expected end of statement");
6907     return false;
6908   }
6909   AssemblerOptions.back()->setNoReorder();
6910   getTargetStreamer().emitDirectiveSetNoReorder();
6911   Parser.Lex(); // Consume the EndOfStatement.
6912   return false;
6913 }
6914 
6915 bool MipsAsmParser::parseSetMacroDirective() {
6916   MCAsmParser &Parser = getParser();
6917   Parser.Lex();
6918   // If this is not the end of the statement, report an error.
6919   if (getLexer().isNot(AsmToken::EndOfStatement)) {
6920     reportParseError("unexpected token, expected end of statement");
6921     return false;
6922   }
6923   AssemblerOptions.back()->setMacro();
6924   getTargetStreamer().emitDirectiveSetMacro();
6925   Parser.Lex(); // Consume the EndOfStatement.
6926   return false;
6927 }
6928 
6929 bool MipsAsmParser::parseSetNoMacroDirective() {
6930   MCAsmParser &Parser = getParser();
6931   Parser.Lex();
6932   // If this is not the end of the statement, report an error.
6933   if (getLexer().isNot(AsmToken::EndOfStatement)) {
6934     reportParseError("unexpected token, expected end of statement");
6935     return false;
6936   }
6937   if (AssemblerOptions.back()->isReorder()) {
6938     reportParseError("`noreorder' must be set before `nomacro'");
6939     return false;
6940   }
6941   AssemblerOptions.back()->setNoMacro();
6942   getTargetStreamer().emitDirectiveSetNoMacro();
6943   Parser.Lex(); // Consume the EndOfStatement.
6944   return false;
6945 }
6946 
6947 bool MipsAsmParser::parseSetMsaDirective() {
6948   MCAsmParser &Parser = getParser();
6949   Parser.Lex();
6950 
6951   // If this is not the end of the statement, report an error.
6952   if (getLexer().isNot(AsmToken::EndOfStatement))
6953     return reportParseError("unexpected token, expected end of statement");
6954 
6955   setFeatureBits(Mips::FeatureMSA, "msa");
6956   getTargetStreamer().emitDirectiveSetMsa();
6957   return false;
6958 }
6959 
6960 bool MipsAsmParser::parseSetNoMsaDirective() {
6961   MCAsmParser &Parser = getParser();
6962   Parser.Lex();
6963 
6964   // If this is not the end of the statement, report an error.
6965   if (getLexer().isNot(AsmToken::EndOfStatement))
6966     return reportParseError("unexpected token, expected end of statement");
6967 
6968   clearFeatureBits(Mips::FeatureMSA, "msa");
6969   getTargetStreamer().emitDirectiveSetNoMsa();
6970   return false;
6971 }
6972 
6973 bool MipsAsmParser::parseSetNoDspDirective() {
6974   MCAsmParser &Parser = getParser();
6975   Parser.Lex(); // Eat "nodsp".
6976 
6977   // If this is not the end of the statement, report an error.
6978   if (getLexer().isNot(AsmToken::EndOfStatement)) {
6979     reportParseError("unexpected token, expected end of statement");
6980     return false;
6981   }
6982 
6983   clearFeatureBits(Mips::FeatureDSP, "dsp");
6984   getTargetStreamer().emitDirectiveSetNoDsp();
6985   return false;
6986 }
6987 
6988 bool MipsAsmParser::parseSetMips16Directive() {
6989   MCAsmParser &Parser = getParser();
6990   Parser.Lex(); // Eat "mips16".
6991 
6992   // If this is not the end of the statement, report an error.
6993   if (getLexer().isNot(AsmToken::EndOfStatement)) {
6994     reportParseError("unexpected token, expected end of statement");
6995     return false;
6996   }
6997 
6998   setFeatureBits(Mips::FeatureMips16, "mips16");
6999   getTargetStreamer().emitDirectiveSetMips16();
7000   Parser.Lex(); // Consume the EndOfStatement.
7001   return false;
7002 }
7003 
7004 bool MipsAsmParser::parseSetNoMips16Directive() {
7005   MCAsmParser &Parser = getParser();
7006   Parser.Lex(); // Eat "nomips16".
7007 
7008   // If this is not the end of the statement, report an error.
7009   if (getLexer().isNot(AsmToken::EndOfStatement)) {
7010     reportParseError("unexpected token, expected end of statement");
7011     return false;
7012   }
7013 
7014   clearFeatureBits(Mips::FeatureMips16, "mips16");
7015   getTargetStreamer().emitDirectiveSetNoMips16();
7016   Parser.Lex(); // Consume the EndOfStatement.
7017   return false;
7018 }
7019 
7020 bool MipsAsmParser::parseSetFpDirective() {
7021   MCAsmParser &Parser = getParser();
7022   MipsABIFlagsSection::FpABIKind FpAbiVal;
7023   // Line can be: .set fp=32
7024   //              .set fp=xx
7025   //              .set fp=64
7026   Parser.Lex(); // Eat fp token
7027   AsmToken Tok = Parser.getTok();
7028   if (Tok.isNot(AsmToken::Equal)) {
7029     reportParseError("unexpected token, expected equals sign '='");
7030     return false;
7031   }
7032   Parser.Lex(); // Eat '=' token.
7033   Tok = Parser.getTok();
7034 
7035   if (!parseFpABIValue(FpAbiVal, ".set"))
7036     return false;
7037 
7038   if (getLexer().isNot(AsmToken::EndOfStatement)) {
7039     reportParseError("unexpected token, expected end of statement");
7040     return false;
7041   }
7042   getTargetStreamer().emitDirectiveSetFp(FpAbiVal);
7043   Parser.Lex(); // Consume the EndOfStatement.
7044   return false;
7045 }
7046 
7047 bool MipsAsmParser::parseSetOddSPRegDirective() {
7048   MCAsmParser &Parser = getParser();
7049 
7050   Parser.Lex(); // Eat "oddspreg".
7051   if (getLexer().isNot(AsmToken::EndOfStatement)) {
7052     reportParseError("unexpected token, expected end of statement");
7053     return false;
7054   }
7055 
7056   clearFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
7057   getTargetStreamer().emitDirectiveSetOddSPReg();
7058   return false;
7059 }
7060 
7061 bool MipsAsmParser::parseSetNoOddSPRegDirective() {
7062   MCAsmParser &Parser = getParser();
7063 
7064   Parser.Lex(); // Eat "nooddspreg".
7065   if (getLexer().isNot(AsmToken::EndOfStatement)) {
7066     reportParseError("unexpected token, expected end of statement");
7067     return false;
7068   }
7069 
7070   setFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
7071   getTargetStreamer().emitDirectiveSetNoOddSPReg();
7072   return false;
7073 }
7074 
7075 bool MipsAsmParser::parseSetMtDirective() {
7076   MCAsmParser &Parser = getParser();
7077   Parser.Lex(); // Eat "mt".
7078 
7079   // If this is not the end of the statement, report an error.
7080   if (getLexer().isNot(AsmToken::EndOfStatement)) {
7081     reportParseError("unexpected token, expected end of statement");
7082     return false;
7083   }
7084 
7085   setFeatureBits(Mips::FeatureMT, "mt");
7086   getTargetStreamer().emitDirectiveSetMt();
7087   Parser.Lex(); // Consume the EndOfStatement.
7088   return false;
7089 }
7090 
7091 bool MipsAsmParser::parseSetNoMtDirective() {
7092   MCAsmParser &Parser = getParser();
7093   Parser.Lex(); // Eat "nomt".
7094 
7095   // If this is not the end of the statement, report an error.
7096   if (getLexer().isNot(AsmToken::EndOfStatement)) {
7097     reportParseError("unexpected token, expected end of statement");
7098     return false;
7099   }
7100 
7101   clearFeatureBits(Mips::FeatureMT, "mt");
7102 
7103   getTargetStreamer().emitDirectiveSetNoMt();
7104   Parser.Lex(); // Consume the EndOfStatement.
7105   return false;
7106 }
7107 
7108 bool MipsAsmParser::parseSetNoCRCDirective() {
7109   MCAsmParser &Parser = getParser();
7110   Parser.Lex(); // Eat "nocrc".
7111 
7112   // If this is not the end of the statement, report an error.
7113   if (getLexer().isNot(AsmToken::EndOfStatement)) {
7114     reportParseError("unexpected token, expected end of statement");
7115     return false;
7116   }
7117 
7118   clearFeatureBits(Mips::FeatureCRC, "crc");
7119 
7120   getTargetStreamer().emitDirectiveSetNoCRC();
7121   Parser.Lex(); // Consume the EndOfStatement.
7122   return false;
7123 }
7124 
7125 bool MipsAsmParser::parseSetNoVirtDirective() {
7126   MCAsmParser &Parser = getParser();
7127   Parser.Lex(); // Eat "novirt".
7128 
7129   // If this is not the end of the statement, report an error.
7130   if (getLexer().isNot(AsmToken::EndOfStatement)) {
7131     reportParseError("unexpected token, expected end of statement");
7132     return false;
7133   }
7134 
7135   clearFeatureBits(Mips::FeatureVirt, "virt");
7136 
7137   getTargetStreamer().emitDirectiveSetNoVirt();
7138   Parser.Lex(); // Consume the EndOfStatement.
7139   return false;
7140 }
7141 
7142 bool MipsAsmParser::parseSetNoGINVDirective() {
7143   MCAsmParser &Parser = getParser();
7144   Parser.Lex(); // Eat "noginv".
7145 
7146   // If this is not the end of the statement, report an error.
7147   if (getLexer().isNot(AsmToken::EndOfStatement)) {
7148     reportParseError("unexpected token, expected end of statement");
7149     return false;
7150   }
7151 
7152   clearFeatureBits(Mips::FeatureGINV, "ginv");
7153 
7154   getTargetStreamer().emitDirectiveSetNoGINV();
7155   Parser.Lex(); // Consume the EndOfStatement.
7156   return false;
7157 }
7158 
7159 bool MipsAsmParser::parseSetPopDirective() {
7160   MCAsmParser &Parser = getParser();
7161   SMLoc Loc = getLexer().getLoc();
7162 
7163   Parser.Lex();
7164   if (getLexer().isNot(AsmToken::EndOfStatement))
7165     return reportParseError("unexpected token, expected end of statement");
7166 
7167   // Always keep an element on the options "stack" to prevent the user
7168   // from changing the initial options. This is how we remember them.
7169   if (AssemblerOptions.size() == 2)
7170     return reportParseError(Loc, ".set pop with no .set push");
7171 
7172   MCSubtargetInfo &STI = copySTI();
7173   AssemblerOptions.pop_back();
7174   setAvailableFeatures(
7175       ComputeAvailableFeatures(AssemblerOptions.back()->getFeatures()));
7176   STI.setFeatureBits(AssemblerOptions.back()->getFeatures());
7177 
7178   getTargetStreamer().emitDirectiveSetPop();
7179   return false;
7180 }
7181 
7182 bool MipsAsmParser::parseSetPushDirective() {
7183   MCAsmParser &Parser = getParser();
7184   Parser.Lex();
7185   if (getLexer().isNot(AsmToken::EndOfStatement))
7186     return reportParseError("unexpected token, expected end of statement");
7187 
7188   // Create a copy of the current assembler options environment and push it.
7189   AssemblerOptions.push_back(
7190         std::make_unique<MipsAssemblerOptions>(AssemblerOptions.back().get()));
7191 
7192   getTargetStreamer().emitDirectiveSetPush();
7193   return false;
7194 }
7195 
7196 bool MipsAsmParser::parseSetSoftFloatDirective() {
7197   MCAsmParser &Parser = getParser();
7198   Parser.Lex();
7199   if (getLexer().isNot(AsmToken::EndOfStatement))
7200     return reportParseError("unexpected token, expected end of statement");
7201 
7202   setFeatureBits(Mips::FeatureSoftFloat, "soft-float");
7203   getTargetStreamer().emitDirectiveSetSoftFloat();
7204   return false;
7205 }
7206 
7207 bool MipsAsmParser::parseSetHardFloatDirective() {
7208   MCAsmParser &Parser = getParser();
7209   Parser.Lex();
7210   if (getLexer().isNot(AsmToken::EndOfStatement))
7211     return reportParseError("unexpected token, expected end of statement");
7212 
7213   clearFeatureBits(Mips::FeatureSoftFloat, "soft-float");
7214   getTargetStreamer().emitDirectiveSetHardFloat();
7215   return false;
7216 }
7217 
7218 bool MipsAsmParser::parseSetAssignment() {
7219   StringRef Name;
7220   MCAsmParser &Parser = getParser();
7221 
7222   if (Parser.parseIdentifier(Name))
7223     return reportParseError("expected identifier after .set");
7224 
7225   if (getLexer().isNot(AsmToken::Comma))
7226     return reportParseError("unexpected token, expected comma");
7227   Lex(); // Eat comma
7228 
7229   if (getLexer().is(AsmToken::Dollar) &&
7230       getLexer().peekTok().is(AsmToken::Integer)) {
7231     // Parse assignment of a numeric register:
7232     //   .set r1,$1
7233     Parser.Lex(); // Eat $.
7234     RegisterSets[Name] = Parser.getTok();
7235     Parser.Lex(); // Eat identifier.
7236     getContext().getOrCreateSymbol(Name);
7237     return false;
7238   }
7239 
7240   MCSymbol *Sym;
7241   const MCExpr *Value;
7242   if (MCParserUtils::parseAssignmentExpression(Name, /* allow_redef */ true,
7243                                                Parser, Sym, Value))
7244     return true;
7245   Sym->setVariableValue(Value);
7246 
7247   return false;
7248 }
7249 
7250 bool MipsAsmParser::parseSetMips0Directive() {
7251   MCAsmParser &Parser = getParser();
7252   Parser.Lex();
7253   if (getLexer().isNot(AsmToken::EndOfStatement))
7254     return reportParseError("unexpected token, expected end of statement");
7255 
7256   // Reset assembler options to their initial values.
7257   MCSubtargetInfo &STI = copySTI();
7258   setAvailableFeatures(
7259       ComputeAvailableFeatures(AssemblerOptions.front()->getFeatures()));
7260   STI.setFeatureBits(AssemblerOptions.front()->getFeatures());
7261   AssemblerOptions.back()->setFeatures(AssemblerOptions.front()->getFeatures());
7262 
7263   getTargetStreamer().emitDirectiveSetMips0();
7264   return false;
7265 }
7266 
7267 bool MipsAsmParser::parseSetArchDirective() {
7268   MCAsmParser &Parser = getParser();
7269   Parser.Lex();
7270   if (getLexer().isNot(AsmToken::Equal))
7271     return reportParseError("unexpected token, expected equals sign");
7272 
7273   Parser.Lex();
7274   StringRef Arch = getParser().parseStringToEndOfStatement().trim();
7275   if (Arch.empty())
7276     return reportParseError("expected arch identifier");
7277 
7278   StringRef ArchFeatureName =
7279       StringSwitch<StringRef>(Arch)
7280           .Case("mips1", "mips1")
7281           .Case("mips2", "mips2")
7282           .Case("mips3", "mips3")
7283           .Case("mips4", "mips4")
7284           .Case("mips5", "mips5")
7285           .Case("mips32", "mips32")
7286           .Case("mips32r2", "mips32r2")
7287           .Case("mips32r3", "mips32r3")
7288           .Case("mips32r5", "mips32r5")
7289           .Case("mips32r6", "mips32r6")
7290           .Case("mips64", "mips64")
7291           .Case("mips64r2", "mips64r2")
7292           .Case("mips64r3", "mips64r3")
7293           .Case("mips64r5", "mips64r5")
7294           .Case("mips64r6", "mips64r6")
7295           .Case("octeon", "cnmips")
7296           .Case("octeon+", "cnmipsp")
7297           .Case("r4000", "mips3") // This is an implementation of Mips3.
7298           .Default("");
7299 
7300   if (ArchFeatureName.empty())
7301     return reportParseError("unsupported architecture");
7302 
7303   if (ArchFeatureName == "mips64r6" && inMicroMipsMode())
7304     return reportParseError("mips64r6 does not support microMIPS");
7305 
7306   selectArch(ArchFeatureName);
7307   getTargetStreamer().emitDirectiveSetArch(Arch);
7308   return false;
7309 }
7310 
7311 bool MipsAsmParser::parseSetFeature(uint64_t Feature) {
7312   MCAsmParser &Parser = getParser();
7313   Parser.Lex();
7314   if (getLexer().isNot(AsmToken::EndOfStatement))
7315     return reportParseError("unexpected token, expected end of statement");
7316 
7317   switch (Feature) {
7318   default:
7319     llvm_unreachable("Unimplemented feature");
7320   case Mips::FeatureDSP:
7321     setFeatureBits(Mips::FeatureDSP, "dsp");
7322     getTargetStreamer().emitDirectiveSetDsp();
7323     break;
7324   case Mips::FeatureDSPR2:
7325     setFeatureBits(Mips::FeatureDSPR2, "dspr2");
7326     getTargetStreamer().emitDirectiveSetDspr2();
7327     break;
7328   case Mips::FeatureMicroMips:
7329     setFeatureBits(Mips::FeatureMicroMips, "micromips");
7330     getTargetStreamer().emitDirectiveSetMicroMips();
7331     break;
7332   case Mips::FeatureMips1:
7333     selectArch("mips1");
7334     getTargetStreamer().emitDirectiveSetMips1();
7335     break;
7336   case Mips::FeatureMips2:
7337     selectArch("mips2");
7338     getTargetStreamer().emitDirectiveSetMips2();
7339     break;
7340   case Mips::FeatureMips3:
7341     selectArch("mips3");
7342     getTargetStreamer().emitDirectiveSetMips3();
7343     break;
7344   case Mips::FeatureMips4:
7345     selectArch("mips4");
7346     getTargetStreamer().emitDirectiveSetMips4();
7347     break;
7348   case Mips::FeatureMips5:
7349     selectArch("mips5");
7350     getTargetStreamer().emitDirectiveSetMips5();
7351     break;
7352   case Mips::FeatureMips32:
7353     selectArch("mips32");
7354     getTargetStreamer().emitDirectiveSetMips32();
7355     break;
7356   case Mips::FeatureMips32r2:
7357     selectArch("mips32r2");
7358     getTargetStreamer().emitDirectiveSetMips32R2();
7359     break;
7360   case Mips::FeatureMips32r3:
7361     selectArch("mips32r3");
7362     getTargetStreamer().emitDirectiveSetMips32R3();
7363     break;
7364   case Mips::FeatureMips32r5:
7365     selectArch("mips32r5");
7366     getTargetStreamer().emitDirectiveSetMips32R5();
7367     break;
7368   case Mips::FeatureMips32r6:
7369     selectArch("mips32r6");
7370     getTargetStreamer().emitDirectiveSetMips32R6();
7371     break;
7372   case Mips::FeatureMips64:
7373     selectArch("mips64");
7374     getTargetStreamer().emitDirectiveSetMips64();
7375     break;
7376   case Mips::FeatureMips64r2:
7377     selectArch("mips64r2");
7378     getTargetStreamer().emitDirectiveSetMips64R2();
7379     break;
7380   case Mips::FeatureMips64r3:
7381     selectArch("mips64r3");
7382     getTargetStreamer().emitDirectiveSetMips64R3();
7383     break;
7384   case Mips::FeatureMips64r5:
7385     selectArch("mips64r5");
7386     getTargetStreamer().emitDirectiveSetMips64R5();
7387     break;
7388   case Mips::FeatureMips64r6:
7389     selectArch("mips64r6");
7390     getTargetStreamer().emitDirectiveSetMips64R6();
7391     break;
7392   case Mips::FeatureCRC:
7393     setFeatureBits(Mips::FeatureCRC, "crc");
7394     getTargetStreamer().emitDirectiveSetCRC();
7395     break;
7396   case Mips::FeatureVirt:
7397     setFeatureBits(Mips::FeatureVirt, "virt");
7398     getTargetStreamer().emitDirectiveSetVirt();
7399     break;
7400   case Mips::FeatureGINV:
7401     setFeatureBits(Mips::FeatureGINV, "ginv");
7402     getTargetStreamer().emitDirectiveSetGINV();
7403     break;
7404   }
7405   return false;
7406 }
7407 
7408 bool MipsAsmParser::eatComma(StringRef ErrorStr) {
7409   MCAsmParser &Parser = getParser();
7410   if (getLexer().isNot(AsmToken::Comma)) {
7411     SMLoc Loc = getLexer().getLoc();
7412     return Error(Loc, ErrorStr);
7413   }
7414 
7415   Parser.Lex(); // Eat the comma.
7416   return true;
7417 }
7418 
7419 // Used to determine if .cpload, .cprestore, and .cpsetup have any effect.
7420 // In this class, it is only used for .cprestore.
7421 // FIXME: Only keep track of IsPicEnabled in one place, instead of in both
7422 // MipsTargetELFStreamer and MipsAsmParser.
7423 bool MipsAsmParser::isPicAndNotNxxAbi() {
7424   return inPicMode() && !(isABI_N32() || isABI_N64());
7425 }
7426 
7427 bool MipsAsmParser::parseDirectiveCpLoad(SMLoc Loc) {
7428   if (AssemblerOptions.back()->isReorder())
7429     Warning(Loc, ".cpload should be inside a noreorder section");
7430 
7431   if (inMips16Mode()) {
7432     reportParseError(".cpload is not supported in Mips16 mode");
7433     return false;
7434   }
7435 
7436   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Reg;
7437   OperandMatchResultTy ResTy = parseAnyRegister(Reg);
7438   if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
7439     reportParseError("expected register containing function address");
7440     return false;
7441   }
7442 
7443   MipsOperand &RegOpnd = static_cast<MipsOperand &>(*Reg[0]);
7444   if (!RegOpnd.isGPRAsmReg()) {
7445     reportParseError(RegOpnd.getStartLoc(), "invalid register");
7446     return false;
7447   }
7448 
7449   // If this is not the end of the statement, report an error.
7450   if (getLexer().isNot(AsmToken::EndOfStatement)) {
7451     reportParseError("unexpected token, expected end of statement");
7452     return false;
7453   }
7454 
7455   getTargetStreamer().emitDirectiveCpLoad(RegOpnd.getGPR32Reg());
7456   return false;
7457 }
7458 
7459 bool MipsAsmParser::parseDirectiveCpLocal(SMLoc Loc) {
7460   if (!isABI_N32() && !isABI_N64()) {
7461     reportParseError(".cplocal is allowed only in N32 or N64 mode");
7462     return false;
7463   }
7464 
7465   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Reg;
7466   OperandMatchResultTy ResTy = parseAnyRegister(Reg);
7467   if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
7468     reportParseError("expected register containing global pointer");
7469     return false;
7470   }
7471 
7472   MipsOperand &RegOpnd = static_cast<MipsOperand &>(*Reg[0]);
7473   if (!RegOpnd.isGPRAsmReg()) {
7474     reportParseError(RegOpnd.getStartLoc(), "invalid register");
7475     return false;
7476   }
7477 
7478   // If this is not the end of the statement, report an error.
7479   if (getLexer().isNot(AsmToken::EndOfStatement)) {
7480     reportParseError("unexpected token, expected end of statement");
7481     return false;
7482   }
7483   getParser().Lex(); // Consume the EndOfStatement.
7484 
7485   unsigned NewReg = RegOpnd.getGPR32Reg();
7486   if (IsPicEnabled)
7487     GPReg = NewReg;
7488 
7489   getTargetStreamer().emitDirectiveCpLocal(NewReg);
7490   return false;
7491 }
7492 
7493 bool MipsAsmParser::parseDirectiveCpRestore(SMLoc Loc) {
7494   MCAsmParser &Parser = getParser();
7495 
7496   // Note that .cprestore is ignored if used with the N32 and N64 ABIs or if it
7497   // is used in non-PIC mode.
7498 
7499   if (inMips16Mode()) {
7500     reportParseError(".cprestore is not supported in Mips16 mode");
7501     return false;
7502   }
7503 
7504   // Get the stack offset value.
7505   const MCExpr *StackOffset;
7506   int64_t StackOffsetVal;
7507   if (Parser.parseExpression(StackOffset)) {
7508     reportParseError("expected stack offset value");
7509     return false;
7510   }
7511 
7512   if (!StackOffset->evaluateAsAbsolute(StackOffsetVal)) {
7513     reportParseError("stack offset is not an absolute expression");
7514     return false;
7515   }
7516 
7517   if (StackOffsetVal < 0) {
7518     Warning(Loc, ".cprestore with negative stack offset has no effect");
7519     IsCpRestoreSet = false;
7520   } else {
7521     IsCpRestoreSet = true;
7522     CpRestoreOffset = StackOffsetVal;
7523   }
7524 
7525   // If this is not the end of the statement, report an error.
7526   if (getLexer().isNot(AsmToken::EndOfStatement)) {
7527     reportParseError("unexpected token, expected end of statement");
7528     return false;
7529   }
7530 
7531   if (!getTargetStreamer().emitDirectiveCpRestore(
7532           CpRestoreOffset, [&]() { return getATReg(Loc); }, Loc, STI))
7533     return true;
7534   Parser.Lex(); // Consume the EndOfStatement.
7535   return false;
7536 }
7537 
7538 bool MipsAsmParser::parseDirectiveCPSetup() {
7539   MCAsmParser &Parser = getParser();
7540   unsigned FuncReg;
7541   unsigned Save;
7542   bool SaveIsReg = true;
7543 
7544   SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> TmpReg;
7545   OperandMatchResultTy ResTy = parseAnyRegister(TmpReg);
7546   if (ResTy == MatchOperand_NoMatch) {
7547     reportParseError("expected register containing function address");
7548     return false;
7549   }
7550 
7551   MipsOperand &FuncRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
7552   if (!FuncRegOpnd.isGPRAsmReg()) {
7553     reportParseError(FuncRegOpnd.getStartLoc(), "invalid register");
7554     return false;
7555   }
7556 
7557   FuncReg = FuncRegOpnd.getGPR32Reg();
7558   TmpReg.clear();
7559 
7560   if (!eatComma("unexpected token, expected comma"))
7561     return true;
7562 
7563   ResTy = parseAnyRegister(TmpReg);
7564   if (ResTy == MatchOperand_NoMatch) {
7565     const MCExpr *OffsetExpr;
7566     int64_t OffsetVal;
7567     SMLoc ExprLoc = getLexer().getLoc();
7568 
7569     if (Parser.parseExpression(OffsetExpr) ||
7570         !OffsetExpr->evaluateAsAbsolute(OffsetVal)) {
7571       reportParseError(ExprLoc, "expected save register or stack offset");
7572       return false;
7573     }
7574 
7575     Save = OffsetVal;
7576     SaveIsReg = false;
7577   } else {
7578     MipsOperand &SaveOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
7579     if (!SaveOpnd.isGPRAsmReg()) {
7580       reportParseError(SaveOpnd.getStartLoc(), "invalid register");
7581       return false;
7582     }
7583     Save = SaveOpnd.getGPR32Reg();
7584   }
7585 
7586   if (!eatComma("unexpected token, expected comma"))
7587     return true;
7588 
7589   const MCExpr *Expr;
7590   if (Parser.parseExpression(Expr)) {
7591     reportParseError("expected expression");
7592     return false;
7593   }
7594 
7595   if (Expr->getKind() != MCExpr::SymbolRef) {
7596     reportParseError("expected symbol");
7597     return false;
7598   }
7599   const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
7600 
7601   CpSaveLocation = Save;
7602   CpSaveLocationIsRegister = SaveIsReg;
7603 
7604   getTargetStreamer().emitDirectiveCpsetup(FuncReg, Save, Ref->getSymbol(),
7605                                            SaveIsReg);
7606   return false;
7607 }
7608 
7609 bool MipsAsmParser::parseDirectiveCPReturn() {
7610   getTargetStreamer().emitDirectiveCpreturn(CpSaveLocation,
7611                                             CpSaveLocationIsRegister);
7612   return false;
7613 }
7614 
7615 bool MipsAsmParser::parseDirectiveNaN() {
7616   MCAsmParser &Parser = getParser();
7617   if (getLexer().isNot(AsmToken::EndOfStatement)) {
7618     const AsmToken &Tok = Parser.getTok();
7619 
7620     if (Tok.getString() == "2008") {
7621       Parser.Lex();
7622       getTargetStreamer().emitDirectiveNaN2008();
7623       return false;
7624     } else if (Tok.getString() == "legacy") {
7625       Parser.Lex();
7626       getTargetStreamer().emitDirectiveNaNLegacy();
7627       return false;
7628     }
7629   }
7630   // If we don't recognize the option passed to the .nan
7631   // directive (e.g. no option or unknown option), emit an error.
7632   reportParseError("invalid option in .nan directive");
7633   return false;
7634 }
7635 
7636 bool MipsAsmParser::parseDirectiveSet() {
7637   const AsmToken &Tok = getParser().getTok();
7638   StringRef IdVal = Tok.getString();
7639   SMLoc Loc = Tok.getLoc();
7640 
7641   if (IdVal == "noat")
7642     return parseSetNoAtDirective();
7643   if (IdVal == "at")
7644     return parseSetAtDirective();
7645   if (IdVal == "arch")
7646     return parseSetArchDirective();
7647   if (IdVal == "bopt") {
7648     Warning(Loc, "'bopt' feature is unsupported");
7649     getParser().Lex();
7650     return false;
7651   }
7652   if (IdVal == "nobopt") {
7653     // We're already running in nobopt mode, so nothing to do.
7654     getParser().Lex();
7655     return false;
7656   }
7657   if (IdVal == "fp")
7658     return parseSetFpDirective();
7659   if (IdVal == "oddspreg")
7660     return parseSetOddSPRegDirective();
7661   if (IdVal == "nooddspreg")
7662     return parseSetNoOddSPRegDirective();
7663   if (IdVal == "pop")
7664     return parseSetPopDirective();
7665   if (IdVal == "push")
7666     return parseSetPushDirective();
7667   if (IdVal == "reorder")
7668     return parseSetReorderDirective();
7669   if (IdVal == "noreorder")
7670     return parseSetNoReorderDirective();
7671   if (IdVal == "macro")
7672     return parseSetMacroDirective();
7673   if (IdVal == "nomacro")
7674     return parseSetNoMacroDirective();
7675   if (IdVal == "mips16")
7676     return parseSetMips16Directive();
7677   if (IdVal == "nomips16")
7678     return parseSetNoMips16Directive();
7679   if (IdVal == "nomicromips") {
7680     clearFeatureBits(Mips::FeatureMicroMips, "micromips");
7681     getTargetStreamer().emitDirectiveSetNoMicroMips();
7682     getParser().eatToEndOfStatement();
7683     return false;
7684   }
7685   if (IdVal == "micromips") {
7686     if (hasMips64r6()) {
7687       Error(Loc, ".set micromips directive is not supported with MIPS64R6");
7688       return false;
7689     }
7690     return parseSetFeature(Mips::FeatureMicroMips);
7691   }
7692   if (IdVal == "mips0")
7693     return parseSetMips0Directive();
7694   if (IdVal == "mips1")
7695     return parseSetFeature(Mips::FeatureMips1);
7696   if (IdVal == "mips2")
7697     return parseSetFeature(Mips::FeatureMips2);
7698   if (IdVal == "mips3")
7699     return parseSetFeature(Mips::FeatureMips3);
7700   if (IdVal == "mips4")
7701     return parseSetFeature(Mips::FeatureMips4);
7702   if (IdVal == "mips5")
7703     return parseSetFeature(Mips::FeatureMips5);
7704   if (IdVal == "mips32")
7705     return parseSetFeature(Mips::FeatureMips32);
7706   if (IdVal == "mips32r2")
7707     return parseSetFeature(Mips::FeatureMips32r2);
7708   if (IdVal == "mips32r3")
7709     return parseSetFeature(Mips::FeatureMips32r3);
7710   if (IdVal == "mips32r5")
7711     return parseSetFeature(Mips::FeatureMips32r5);
7712   if (IdVal == "mips32r6")
7713     return parseSetFeature(Mips::FeatureMips32r6);
7714   if (IdVal == "mips64")
7715     return parseSetFeature(Mips::FeatureMips64);
7716   if (IdVal == "mips64r2")
7717     return parseSetFeature(Mips::FeatureMips64r2);
7718   if (IdVal == "mips64r3")
7719     return parseSetFeature(Mips::FeatureMips64r3);
7720   if (IdVal == "mips64r5")
7721     return parseSetFeature(Mips::FeatureMips64r5);
7722   if (IdVal == "mips64r6") {
7723     if (inMicroMipsMode()) {
7724       Error(Loc, "MIPS64R6 is not supported with microMIPS");
7725       return false;
7726     }
7727     return parseSetFeature(Mips::FeatureMips64r6);
7728   }
7729   if (IdVal == "dsp")
7730     return parseSetFeature(Mips::FeatureDSP);
7731   if (IdVal == "dspr2")
7732     return parseSetFeature(Mips::FeatureDSPR2);
7733   if (IdVal == "nodsp")
7734     return parseSetNoDspDirective();
7735   if (IdVal == "msa")
7736     return parseSetMsaDirective();
7737   if (IdVal == "nomsa")
7738     return parseSetNoMsaDirective();
7739   if (IdVal == "mt")
7740     return parseSetMtDirective();
7741   if (IdVal == "nomt")
7742     return parseSetNoMtDirective();
7743   if (IdVal == "softfloat")
7744     return parseSetSoftFloatDirective();
7745   if (IdVal == "hardfloat")
7746     return parseSetHardFloatDirective();
7747   if (IdVal == "crc")
7748     return parseSetFeature(Mips::FeatureCRC);
7749   if (IdVal == "nocrc")
7750     return parseSetNoCRCDirective();
7751   if (IdVal == "virt")
7752     return parseSetFeature(Mips::FeatureVirt);
7753   if (IdVal == "novirt")
7754     return parseSetNoVirtDirective();
7755   if (IdVal == "ginv")
7756     return parseSetFeature(Mips::FeatureGINV);
7757   if (IdVal == "noginv")
7758     return parseSetNoGINVDirective();
7759 
7760   // It is just an identifier, look for an assignment.
7761   return parseSetAssignment();
7762 }
7763 
7764 /// parseDirectiveGpWord
7765 ///  ::= .gpword local_sym
7766 bool MipsAsmParser::parseDirectiveGpWord() {
7767   MCAsmParser &Parser = getParser();
7768   const MCExpr *Value;
7769   // EmitGPRel32Value requires an expression, so we are using base class
7770   // method to evaluate the expression.
7771   if (getParser().parseExpression(Value))
7772     return true;
7773   getParser().getStreamer().emitGPRel32Value(Value);
7774 
7775   if (getLexer().isNot(AsmToken::EndOfStatement))
7776     return Error(getLexer().getLoc(),
7777                 "unexpected token, expected end of statement");
7778   Parser.Lex(); // Eat EndOfStatement token.
7779   return false;
7780 }
7781 
7782 /// parseDirectiveGpDWord
7783 ///  ::= .gpdword local_sym
7784 bool MipsAsmParser::parseDirectiveGpDWord() {
7785   MCAsmParser &Parser = getParser();
7786   const MCExpr *Value;
7787   // EmitGPRel64Value requires an expression, so we are using base class
7788   // method to evaluate the expression.
7789   if (getParser().parseExpression(Value))
7790     return true;
7791   getParser().getStreamer().emitGPRel64Value(Value);
7792 
7793   if (getLexer().isNot(AsmToken::EndOfStatement))
7794     return Error(getLexer().getLoc(),
7795                 "unexpected token, expected end of statement");
7796   Parser.Lex(); // Eat EndOfStatement token.
7797   return false;
7798 }
7799 
7800 /// parseDirectiveDtpRelWord
7801 ///  ::= .dtprelword tls_sym
7802 bool MipsAsmParser::parseDirectiveDtpRelWord() {
7803   MCAsmParser &Parser = getParser();
7804   const MCExpr *Value;
7805   // EmitDTPRel32Value requires an expression, so we are using base class
7806   // method to evaluate the expression.
7807   if (getParser().parseExpression(Value))
7808     return true;
7809   getParser().getStreamer().emitDTPRel32Value(Value);
7810 
7811   if (getLexer().isNot(AsmToken::EndOfStatement))
7812     return Error(getLexer().getLoc(),
7813                 "unexpected token, expected end of statement");
7814   Parser.Lex(); // Eat EndOfStatement token.
7815   return false;
7816 }
7817 
7818 /// parseDirectiveDtpRelDWord
7819 ///  ::= .dtpreldword tls_sym
7820 bool MipsAsmParser::parseDirectiveDtpRelDWord() {
7821   MCAsmParser &Parser = getParser();
7822   const MCExpr *Value;
7823   // EmitDTPRel64Value requires an expression, so we are using base class
7824   // method to evaluate the expression.
7825   if (getParser().parseExpression(Value))
7826     return true;
7827   getParser().getStreamer().emitDTPRel64Value(Value);
7828 
7829   if (getLexer().isNot(AsmToken::EndOfStatement))
7830     return Error(getLexer().getLoc(),
7831                 "unexpected token, expected end of statement");
7832   Parser.Lex(); // Eat EndOfStatement token.
7833   return false;
7834 }
7835 
7836 /// parseDirectiveTpRelWord
7837 ///  ::= .tprelword tls_sym
7838 bool MipsAsmParser::parseDirectiveTpRelWord() {
7839   MCAsmParser &Parser = getParser();
7840   const MCExpr *Value;
7841   // EmitTPRel32Value requires an expression, so we are using base class
7842   // method to evaluate the expression.
7843   if (getParser().parseExpression(Value))
7844     return true;
7845   getParser().getStreamer().emitTPRel32Value(Value);
7846 
7847   if (getLexer().isNot(AsmToken::EndOfStatement))
7848     return Error(getLexer().getLoc(),
7849                 "unexpected token, expected end of statement");
7850   Parser.Lex(); // Eat EndOfStatement token.
7851   return false;
7852 }
7853 
7854 /// parseDirectiveTpRelDWord
7855 ///  ::= .tpreldword tls_sym
7856 bool MipsAsmParser::parseDirectiveTpRelDWord() {
7857   MCAsmParser &Parser = getParser();
7858   const MCExpr *Value;
7859   // EmitTPRel64Value requires an expression, so we are using base class
7860   // method to evaluate the expression.
7861   if (getParser().parseExpression(Value))
7862     return true;
7863   getParser().getStreamer().emitTPRel64Value(Value);
7864 
7865   if (getLexer().isNot(AsmToken::EndOfStatement))
7866     return Error(getLexer().getLoc(),
7867                 "unexpected token, expected end of statement");
7868   Parser.Lex(); // Eat EndOfStatement token.
7869   return false;
7870 }
7871 
7872 bool MipsAsmParser::parseDirectiveOption() {
7873   MCAsmParser &Parser = getParser();
7874   // Get the option token.
7875   AsmToken Tok = Parser.getTok();
7876   // At the moment only identifiers are supported.
7877   if (Tok.isNot(AsmToken::Identifier)) {
7878     return Error(Parser.getTok().getLoc(),
7879                  "unexpected token, expected identifier");
7880   }
7881 
7882   StringRef Option = Tok.getIdentifier();
7883 
7884   if (Option == "pic0") {
7885     // MipsAsmParser needs to know if the current PIC mode changes.
7886     IsPicEnabled = false;
7887 
7888     getTargetStreamer().emitDirectiveOptionPic0();
7889     Parser.Lex();
7890     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
7891       return Error(Parser.getTok().getLoc(),
7892                    "unexpected token, expected end of statement");
7893     }
7894     return false;
7895   }
7896 
7897   if (Option == "pic2") {
7898     // MipsAsmParser needs to know if the current PIC mode changes.
7899     IsPicEnabled = true;
7900 
7901     getTargetStreamer().emitDirectiveOptionPic2();
7902     Parser.Lex();
7903     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
7904       return Error(Parser.getTok().getLoc(),
7905                    "unexpected token, expected end of statement");
7906     }
7907     return false;
7908   }
7909 
7910   // Unknown option.
7911   Warning(Parser.getTok().getLoc(),
7912           "unknown option, expected 'pic0' or 'pic2'");
7913   Parser.eatToEndOfStatement();
7914   return false;
7915 }
7916 
7917 /// parseInsnDirective
7918 ///  ::= .insn
7919 bool MipsAsmParser::parseInsnDirective() {
7920   // If this is not the end of the statement, report an error.
7921   if (getLexer().isNot(AsmToken::EndOfStatement)) {
7922     reportParseError("unexpected token, expected end of statement");
7923     return false;
7924   }
7925 
7926   // The actual label marking happens in
7927   // MipsELFStreamer::createPendingLabelRelocs().
7928   getTargetStreamer().emitDirectiveInsn();
7929 
7930   getParser().Lex(); // Eat EndOfStatement token.
7931   return false;
7932 }
7933 
7934 /// parseRSectionDirective
7935 ///  ::= .rdata
7936 bool MipsAsmParser::parseRSectionDirective(StringRef Section) {
7937   // If this is not the end of the statement, report an error.
7938   if (getLexer().isNot(AsmToken::EndOfStatement)) {
7939     reportParseError("unexpected token, expected end of statement");
7940     return false;
7941   }
7942 
7943   MCSection *ELFSection = getContext().getELFSection(
7944       Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
7945   getParser().getStreamer().SwitchSection(ELFSection);
7946 
7947   getParser().Lex(); // Eat EndOfStatement token.
7948   return false;
7949 }
7950 
7951 /// parseSSectionDirective
7952 ///  ::= .sbss
7953 ///  ::= .sdata
7954 bool MipsAsmParser::parseSSectionDirective(StringRef Section, unsigned Type) {
7955   // If this is not the end of the statement, report an error.
7956   if (getLexer().isNot(AsmToken::EndOfStatement)) {
7957     reportParseError("unexpected token, expected end of statement");
7958     return false;
7959   }
7960 
7961   MCSection *ELFSection = getContext().getELFSection(
7962       Section, Type, ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_MIPS_GPREL);
7963   getParser().getStreamer().SwitchSection(ELFSection);
7964 
7965   getParser().Lex(); // Eat EndOfStatement token.
7966   return false;
7967 }
7968 
7969 /// parseDirectiveModule
7970 ///  ::= .module oddspreg
7971 ///  ::= .module nooddspreg
7972 ///  ::= .module fp=value
7973 ///  ::= .module softfloat
7974 ///  ::= .module hardfloat
7975 ///  ::= .module mt
7976 ///  ::= .module crc
7977 ///  ::= .module nocrc
7978 ///  ::= .module virt
7979 ///  ::= .module novirt
7980 ///  ::= .module ginv
7981 ///  ::= .module noginv
7982 bool MipsAsmParser::parseDirectiveModule() {
7983   MCAsmParser &Parser = getParser();
7984   MCAsmLexer &Lexer = getLexer();
7985   SMLoc L = Lexer.getLoc();
7986 
7987   if (!getTargetStreamer().isModuleDirectiveAllowed()) {
7988     // TODO : get a better message.
7989     reportParseError(".module directive must appear before any code");
7990     return false;
7991   }
7992 
7993   StringRef Option;
7994   if (Parser.parseIdentifier(Option)) {
7995     reportParseError("expected .module option identifier");
7996     return false;
7997   }
7998 
7999   if (Option == "oddspreg") {
8000     clearModuleFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
8001 
8002     // Synchronize the abiflags information with the FeatureBits information we
8003     // changed above.
8004     getTargetStreamer().updateABIInfo(*this);
8005 
8006     // If printing assembly, use the recently updated abiflags information.
8007     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
8008     // emitted at the end).
8009     getTargetStreamer().emitDirectiveModuleOddSPReg();
8010 
8011     // If this is not the end of the statement, report an error.
8012     if (getLexer().isNot(AsmToken::EndOfStatement)) {
8013       reportParseError("unexpected token, expected end of statement");
8014       return false;
8015     }
8016 
8017     return false; // parseDirectiveModule has finished successfully.
8018   } else if (Option == "nooddspreg") {
8019     if (!isABI_O32()) {
8020       return Error(L, "'.module nooddspreg' requires the O32 ABI");
8021     }
8022 
8023     setModuleFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg");
8024 
8025     // Synchronize the abiflags information with the FeatureBits information we
8026     // changed above.
8027     getTargetStreamer().updateABIInfo(*this);
8028 
8029     // If printing assembly, use the recently updated abiflags information.
8030     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
8031     // emitted at the end).
8032     getTargetStreamer().emitDirectiveModuleOddSPReg();
8033 
8034     // If this is not the end of the statement, report an error.
8035     if (getLexer().isNot(AsmToken::EndOfStatement)) {
8036       reportParseError("unexpected token, expected end of statement");
8037       return false;
8038     }
8039 
8040     return false; // parseDirectiveModule has finished successfully.
8041   } else if (Option == "fp") {
8042     return parseDirectiveModuleFP();
8043   } else if (Option == "softfloat") {
8044     setModuleFeatureBits(Mips::FeatureSoftFloat, "soft-float");
8045 
8046     // Synchronize the ABI Flags information with the FeatureBits information we
8047     // updated above.
8048     getTargetStreamer().updateABIInfo(*this);
8049 
8050     // If printing assembly, use the recently updated ABI Flags information.
8051     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
8052     // emitted later).
8053     getTargetStreamer().emitDirectiveModuleSoftFloat();
8054 
8055     // If this is not the end of the statement, report an error.
8056     if (getLexer().isNot(AsmToken::EndOfStatement)) {
8057       reportParseError("unexpected token, expected end of statement");
8058       return false;
8059     }
8060 
8061     return false; // parseDirectiveModule has finished successfully.
8062   } else if (Option == "hardfloat") {
8063     clearModuleFeatureBits(Mips::FeatureSoftFloat, "soft-float");
8064 
8065     // Synchronize the ABI Flags information with the FeatureBits information we
8066     // updated above.
8067     getTargetStreamer().updateABIInfo(*this);
8068 
8069     // If printing assembly, use the recently updated ABI Flags information.
8070     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
8071     // emitted later).
8072     getTargetStreamer().emitDirectiveModuleHardFloat();
8073 
8074     // If this is not the end of the statement, report an error.
8075     if (getLexer().isNot(AsmToken::EndOfStatement)) {
8076       reportParseError("unexpected token, expected end of statement");
8077       return false;
8078     }
8079 
8080     return false; // parseDirectiveModule has finished successfully.
8081   } else if (Option == "mt") {
8082     setModuleFeatureBits(Mips::FeatureMT, "mt");
8083 
8084     // Synchronize the ABI Flags information with the FeatureBits information we
8085     // updated above.
8086     getTargetStreamer().updateABIInfo(*this);
8087 
8088     // If printing assembly, use the recently updated ABI Flags information.
8089     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
8090     // emitted later).
8091     getTargetStreamer().emitDirectiveModuleMT();
8092 
8093     // If this is not the end of the statement, report an error.
8094     if (getLexer().isNot(AsmToken::EndOfStatement)) {
8095       reportParseError("unexpected token, expected end of statement");
8096       return false;
8097     }
8098 
8099     return false; // parseDirectiveModule has finished successfully.
8100   } else if (Option == "crc") {
8101     setModuleFeatureBits(Mips::FeatureCRC, "crc");
8102 
8103     // Synchronize the ABI Flags information with the FeatureBits information we
8104     // updated above.
8105     getTargetStreamer().updateABIInfo(*this);
8106 
8107     // If printing assembly, use the recently updated ABI Flags information.
8108     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
8109     // emitted later).
8110     getTargetStreamer().emitDirectiveModuleCRC();
8111 
8112     // If this is not the end of the statement, report an error.
8113     if (getLexer().isNot(AsmToken::EndOfStatement)) {
8114       reportParseError("unexpected token, expected end of statement");
8115       return false;
8116     }
8117 
8118     return false; // parseDirectiveModule has finished successfully.
8119   } else if (Option == "nocrc") {
8120     clearModuleFeatureBits(Mips::FeatureCRC, "crc");
8121 
8122     // Synchronize the ABI Flags information with the FeatureBits information we
8123     // updated above.
8124     getTargetStreamer().updateABIInfo(*this);
8125 
8126     // If printing assembly, use the recently updated ABI Flags information.
8127     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
8128     // emitted later).
8129     getTargetStreamer().emitDirectiveModuleNoCRC();
8130 
8131     // If this is not the end of the statement, report an error.
8132     if (getLexer().isNot(AsmToken::EndOfStatement)) {
8133       reportParseError("unexpected token, expected end of statement");
8134       return false;
8135     }
8136 
8137     return false; // parseDirectiveModule has finished successfully.
8138   } else if (Option == "virt") {
8139     setModuleFeatureBits(Mips::FeatureVirt, "virt");
8140 
8141     // Synchronize the ABI Flags information with the FeatureBits information we
8142     // updated above.
8143     getTargetStreamer().updateABIInfo(*this);
8144 
8145     // If printing assembly, use the recently updated ABI Flags information.
8146     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
8147     // emitted later).
8148     getTargetStreamer().emitDirectiveModuleVirt();
8149 
8150     // If this is not the end of the statement, report an error.
8151     if (getLexer().isNot(AsmToken::EndOfStatement)) {
8152       reportParseError("unexpected token, expected end of statement");
8153       return false;
8154     }
8155 
8156     return false; // parseDirectiveModule has finished successfully.
8157   } else if (Option == "novirt") {
8158     clearModuleFeatureBits(Mips::FeatureVirt, "virt");
8159 
8160     // Synchronize the ABI Flags information with the FeatureBits information we
8161     // updated above.
8162     getTargetStreamer().updateABIInfo(*this);
8163 
8164     // If printing assembly, use the recently updated ABI Flags information.
8165     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
8166     // emitted later).
8167     getTargetStreamer().emitDirectiveModuleNoVirt();
8168 
8169     // If this is not the end of the statement, report an error.
8170     if (getLexer().isNot(AsmToken::EndOfStatement)) {
8171       reportParseError("unexpected token, expected end of statement");
8172       return false;
8173     }
8174 
8175     return false; // parseDirectiveModule has finished successfully.
8176   } else if (Option == "ginv") {
8177     setModuleFeatureBits(Mips::FeatureGINV, "ginv");
8178 
8179     // Synchronize the ABI Flags information with the FeatureBits information we
8180     // updated above.
8181     getTargetStreamer().updateABIInfo(*this);
8182 
8183     // If printing assembly, use the recently updated ABI Flags information.
8184     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
8185     // emitted later).
8186     getTargetStreamer().emitDirectiveModuleGINV();
8187 
8188     // If this is not the end of the statement, report an error.
8189     if (getLexer().isNot(AsmToken::EndOfStatement)) {
8190       reportParseError("unexpected token, expected end of statement");
8191       return false;
8192     }
8193 
8194     return false; // parseDirectiveModule has finished successfully.
8195   } else if (Option == "noginv") {
8196     clearModuleFeatureBits(Mips::FeatureGINV, "ginv");
8197 
8198     // Synchronize the ABI Flags information with the FeatureBits information we
8199     // updated above.
8200     getTargetStreamer().updateABIInfo(*this);
8201 
8202     // If printing assembly, use the recently updated ABI Flags information.
8203     // If generating ELF, don't do anything (the .MIPS.abiflags section gets
8204     // emitted later).
8205     getTargetStreamer().emitDirectiveModuleNoGINV();
8206 
8207     // If this is not the end of the statement, report an error.
8208     if (getLexer().isNot(AsmToken::EndOfStatement)) {
8209       reportParseError("unexpected token, expected end of statement");
8210       return false;
8211     }
8212 
8213     return false; // parseDirectiveModule has finished successfully.
8214   } else {
8215     return Error(L, "'" + Twine(Option) + "' is not a valid .module option.");
8216   }
8217 }
8218 
8219 /// parseDirectiveModuleFP
8220 ///  ::= =32
8221 ///  ::= =xx
8222 ///  ::= =64
8223 bool MipsAsmParser::parseDirectiveModuleFP() {
8224   MCAsmParser &Parser = getParser();
8225   MCAsmLexer &Lexer = getLexer();
8226 
8227   if (Lexer.isNot(AsmToken::Equal)) {
8228     reportParseError("unexpected token, expected equals sign '='");
8229     return false;
8230   }
8231   Parser.Lex(); // Eat '=' token.
8232 
8233   MipsABIFlagsSection::FpABIKind FpABI;
8234   if (!parseFpABIValue(FpABI, ".module"))
8235     return false;
8236 
8237   if (getLexer().isNot(AsmToken::EndOfStatement)) {
8238     reportParseError("unexpected token, expected end of statement");
8239     return false;
8240   }
8241 
8242   // Synchronize the abiflags information with the FeatureBits information we
8243   // changed above.
8244   getTargetStreamer().updateABIInfo(*this);
8245 
8246   // If printing assembly, use the recently updated abiflags information.
8247   // If generating ELF, don't do anything (the .MIPS.abiflags section gets
8248   // emitted at the end).
8249   getTargetStreamer().emitDirectiveModuleFP();
8250 
8251   Parser.Lex(); // Consume the EndOfStatement.
8252   return false;
8253 }
8254 
8255 bool MipsAsmParser::parseFpABIValue(MipsABIFlagsSection::FpABIKind &FpABI,
8256                                     StringRef Directive) {
8257   MCAsmParser &Parser = getParser();
8258   MCAsmLexer &Lexer = getLexer();
8259   bool ModuleLevelOptions = Directive == ".module";
8260 
8261   if (Lexer.is(AsmToken::Identifier)) {
8262     StringRef Value = Parser.getTok().getString();
8263     Parser.Lex();
8264 
8265     if (Value != "xx") {
8266       reportParseError("unsupported value, expected 'xx', '32' or '64'");
8267       return false;
8268     }
8269 
8270     if (!isABI_O32()) {
8271       reportParseError("'" + Directive + " fp=xx' requires the O32 ABI");
8272       return false;
8273     }
8274 
8275     FpABI = MipsABIFlagsSection::FpABIKind::XX;
8276     if (ModuleLevelOptions) {
8277       setModuleFeatureBits(Mips::FeatureFPXX, "fpxx");
8278       clearModuleFeatureBits(Mips::FeatureFP64Bit, "fp64");
8279     } else {
8280       setFeatureBits(Mips::FeatureFPXX, "fpxx");
8281       clearFeatureBits(Mips::FeatureFP64Bit, "fp64");
8282     }
8283     return true;
8284   }
8285 
8286   if (Lexer.is(AsmToken::Integer)) {
8287     unsigned Value = Parser.getTok().getIntVal();
8288     Parser.Lex();
8289 
8290     if (Value != 32 && Value != 64) {
8291       reportParseError("unsupported value, expected 'xx', '32' or '64'");
8292       return false;
8293     }
8294 
8295     if (Value == 32) {
8296       if (!isABI_O32()) {
8297         reportParseError("'" + Directive + " fp=32' requires the O32 ABI");
8298         return false;
8299       }
8300 
8301       FpABI = MipsABIFlagsSection::FpABIKind::S32;
8302       if (ModuleLevelOptions) {
8303         clearModuleFeatureBits(Mips::FeatureFPXX, "fpxx");
8304         clearModuleFeatureBits(Mips::FeatureFP64Bit, "fp64");
8305       } else {
8306         clearFeatureBits(Mips::FeatureFPXX, "fpxx");
8307         clearFeatureBits(Mips::FeatureFP64Bit, "fp64");
8308       }
8309     } else {
8310       FpABI = MipsABIFlagsSection::FpABIKind::S64;
8311       if (ModuleLevelOptions) {
8312         clearModuleFeatureBits(Mips::FeatureFPXX, "fpxx");
8313         setModuleFeatureBits(Mips::FeatureFP64Bit, "fp64");
8314       } else {
8315         clearFeatureBits(Mips::FeatureFPXX, "fpxx");
8316         setFeatureBits(Mips::FeatureFP64Bit, "fp64");
8317       }
8318     }
8319 
8320     return true;
8321   }
8322 
8323   return false;
8324 }
8325 
8326 bool MipsAsmParser::ParseDirective(AsmToken DirectiveID) {
8327   // This returns false if this function recognizes the directive
8328   // regardless of whether it is successfully handles or reports an
8329   // error. Otherwise it returns true to give the generic parser a
8330   // chance at recognizing it.
8331 
8332   MCAsmParser &Parser = getParser();
8333   StringRef IDVal = DirectiveID.getString();
8334 
8335   if (IDVal == ".cpload") {
8336     parseDirectiveCpLoad(DirectiveID.getLoc());
8337     return false;
8338   }
8339   if (IDVal == ".cprestore") {
8340     parseDirectiveCpRestore(DirectiveID.getLoc());
8341     return false;
8342   }
8343   if (IDVal == ".cplocal") {
8344     parseDirectiveCpLocal(DirectiveID.getLoc());
8345     return false;
8346   }
8347   if (IDVal == ".ent") {
8348     StringRef SymbolName;
8349 
8350     if (Parser.parseIdentifier(SymbolName)) {
8351       reportParseError("expected identifier after .ent");
8352       return false;
8353     }
8354 
8355     // There's an undocumented extension that allows an integer to
8356     // follow the name of the procedure which AFAICS is ignored by GAS.
8357     // Example: .ent foo,2
8358     if (getLexer().isNot(AsmToken::EndOfStatement)) {
8359       if (getLexer().isNot(AsmToken::Comma)) {
8360         // Even though we accept this undocumented extension for compatibility
8361         // reasons, the additional integer argument does not actually change
8362         // the behaviour of the '.ent' directive, so we would like to discourage
8363         // its use. We do this by not referring to the extended version in
8364         // error messages which are not directly related to its use.
8365         reportParseError("unexpected token, expected end of statement");
8366         return false;
8367       }
8368       Parser.Lex(); // Eat the comma.
8369       const MCExpr *DummyNumber;
8370       int64_t DummyNumberVal;
8371       // If the user was explicitly trying to use the extended version,
8372       // we still give helpful extension-related error messages.
8373       if (Parser.parseExpression(DummyNumber)) {
8374         reportParseError("expected number after comma");
8375         return false;
8376       }
8377       if (!DummyNumber->evaluateAsAbsolute(DummyNumberVal)) {
8378         reportParseError("expected an absolute expression after comma");
8379         return false;
8380       }
8381     }
8382 
8383     // If this is not the end of the statement, report an error.
8384     if (getLexer().isNot(AsmToken::EndOfStatement)) {
8385       reportParseError("unexpected token, expected end of statement");
8386       return false;
8387     }
8388 
8389     MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
8390 
8391     getTargetStreamer().emitDirectiveEnt(*Sym);
8392     CurrentFn = Sym;
8393     IsCpRestoreSet = false;
8394     return false;
8395   }
8396 
8397   if (IDVal == ".end") {
8398     StringRef SymbolName;
8399 
8400     if (Parser.parseIdentifier(SymbolName)) {
8401       reportParseError("expected identifier after .end");
8402       return false;
8403     }
8404 
8405     if (getLexer().isNot(AsmToken::EndOfStatement)) {
8406       reportParseError("unexpected token, expected end of statement");
8407       return false;
8408     }
8409 
8410     if (CurrentFn == nullptr) {
8411       reportParseError(".end used without .ent");
8412       return false;
8413     }
8414 
8415     if ((SymbolName != CurrentFn->getName())) {
8416       reportParseError(".end symbol does not match .ent symbol");
8417       return false;
8418     }
8419 
8420     getTargetStreamer().emitDirectiveEnd(SymbolName);
8421     CurrentFn = nullptr;
8422     IsCpRestoreSet = false;
8423     return false;
8424   }
8425 
8426   if (IDVal == ".frame") {
8427     // .frame $stack_reg, frame_size_in_bytes, $return_reg
8428     SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> TmpReg;
8429     OperandMatchResultTy ResTy = parseAnyRegister(TmpReg);
8430     if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
8431       reportParseError("expected stack register");
8432       return false;
8433     }
8434 
8435     MipsOperand &StackRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
8436     if (!StackRegOpnd.isGPRAsmReg()) {
8437       reportParseError(StackRegOpnd.getStartLoc(),
8438                        "expected general purpose register");
8439       return false;
8440     }
8441     unsigned StackReg = StackRegOpnd.getGPR32Reg();
8442 
8443     if (Parser.getTok().is(AsmToken::Comma))
8444       Parser.Lex();
8445     else {
8446       reportParseError("unexpected token, expected comma");
8447       return false;
8448     }
8449 
8450     // Parse the frame size.
8451     const MCExpr *FrameSize;
8452     int64_t FrameSizeVal;
8453 
8454     if (Parser.parseExpression(FrameSize)) {
8455       reportParseError("expected frame size value");
8456       return false;
8457     }
8458 
8459     if (!FrameSize->evaluateAsAbsolute(FrameSizeVal)) {
8460       reportParseError("frame size not an absolute expression");
8461       return false;
8462     }
8463 
8464     if (Parser.getTok().is(AsmToken::Comma))
8465       Parser.Lex();
8466     else {
8467       reportParseError("unexpected token, expected comma");
8468       return false;
8469     }
8470 
8471     // Parse the return register.
8472     TmpReg.clear();
8473     ResTy = parseAnyRegister(TmpReg);
8474     if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
8475       reportParseError("expected return register");
8476       return false;
8477     }
8478 
8479     MipsOperand &ReturnRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]);
8480     if (!ReturnRegOpnd.isGPRAsmReg()) {
8481       reportParseError(ReturnRegOpnd.getStartLoc(),
8482                        "expected general purpose register");
8483       return false;
8484     }
8485 
8486     // If this is not the end of the statement, report an error.
8487     if (getLexer().isNot(AsmToken::EndOfStatement)) {
8488       reportParseError("unexpected token, expected end of statement");
8489       return false;
8490     }
8491 
8492     getTargetStreamer().emitFrame(StackReg, FrameSizeVal,
8493                                   ReturnRegOpnd.getGPR32Reg());
8494     IsCpRestoreSet = false;
8495     return false;
8496   }
8497 
8498   if (IDVal == ".set") {
8499     parseDirectiveSet();
8500     return false;
8501   }
8502 
8503   if (IDVal == ".mask" || IDVal == ".fmask") {
8504     // .mask bitmask, frame_offset
8505     // bitmask: One bit for each register used.
8506     // frame_offset: Offset from Canonical Frame Address ($sp on entry) where
8507     //               first register is expected to be saved.
8508     // Examples:
8509     //   .mask 0x80000000, -4
8510     //   .fmask 0x80000000, -4
8511     //
8512 
8513     // Parse the bitmask
8514     const MCExpr *BitMask;
8515     int64_t BitMaskVal;
8516 
8517     if (Parser.parseExpression(BitMask)) {
8518       reportParseError("expected bitmask value");
8519       return false;
8520     }
8521 
8522     if (!BitMask->evaluateAsAbsolute(BitMaskVal)) {
8523       reportParseError("bitmask not an absolute expression");
8524       return false;
8525     }
8526 
8527     if (Parser.getTok().is(AsmToken::Comma))
8528       Parser.Lex();
8529     else {
8530       reportParseError("unexpected token, expected comma");
8531       return false;
8532     }
8533 
8534     // Parse the frame_offset
8535     const MCExpr *FrameOffset;
8536     int64_t FrameOffsetVal;
8537 
8538     if (Parser.parseExpression(FrameOffset)) {
8539       reportParseError("expected frame offset value");
8540       return false;
8541     }
8542 
8543     if (!FrameOffset->evaluateAsAbsolute(FrameOffsetVal)) {
8544       reportParseError("frame offset not an absolute expression");
8545       return false;
8546     }
8547 
8548     // If this is not the end of the statement, report an error.
8549     if (getLexer().isNot(AsmToken::EndOfStatement)) {
8550       reportParseError("unexpected token, expected end of statement");
8551       return false;
8552     }
8553 
8554     if (IDVal == ".mask")
8555       getTargetStreamer().emitMask(BitMaskVal, FrameOffsetVal);
8556     else
8557       getTargetStreamer().emitFMask(BitMaskVal, FrameOffsetVal);
8558     return false;
8559   }
8560 
8561   if (IDVal == ".nan")
8562     return parseDirectiveNaN();
8563 
8564   if (IDVal == ".gpword") {
8565     parseDirectiveGpWord();
8566     return false;
8567   }
8568 
8569   if (IDVal == ".gpdword") {
8570     parseDirectiveGpDWord();
8571     return false;
8572   }
8573 
8574   if (IDVal == ".dtprelword") {
8575     parseDirectiveDtpRelWord();
8576     return false;
8577   }
8578 
8579   if (IDVal == ".dtpreldword") {
8580     parseDirectiveDtpRelDWord();
8581     return false;
8582   }
8583 
8584   if (IDVal == ".tprelword") {
8585     parseDirectiveTpRelWord();
8586     return false;
8587   }
8588 
8589   if (IDVal == ".tpreldword") {
8590     parseDirectiveTpRelDWord();
8591     return false;
8592   }
8593 
8594   if (IDVal == ".option") {
8595     parseDirectiveOption();
8596     return false;
8597   }
8598 
8599   if (IDVal == ".abicalls") {
8600     getTargetStreamer().emitDirectiveAbiCalls();
8601     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
8602       Error(Parser.getTok().getLoc(),
8603             "unexpected token, expected end of statement");
8604     }
8605     return false;
8606   }
8607 
8608   if (IDVal == ".cpsetup") {
8609     parseDirectiveCPSetup();
8610     return false;
8611   }
8612   if (IDVal == ".cpreturn") {
8613     parseDirectiveCPReturn();
8614     return false;
8615   }
8616   if (IDVal == ".module") {
8617     parseDirectiveModule();
8618     return false;
8619   }
8620   if (IDVal == ".llvm_internal_mips_reallow_module_directive") {
8621     parseInternalDirectiveReallowModule();
8622     return false;
8623   }
8624   if (IDVal == ".insn") {
8625     parseInsnDirective();
8626     return false;
8627   }
8628   if (IDVal == ".rdata") {
8629     parseRSectionDirective(".rodata");
8630     return false;
8631   }
8632   if (IDVal == ".sbss") {
8633     parseSSectionDirective(IDVal, ELF::SHT_NOBITS);
8634     return false;
8635   }
8636   if (IDVal == ".sdata") {
8637     parseSSectionDirective(IDVal, ELF::SHT_PROGBITS);
8638     return false;
8639   }
8640 
8641   return true;
8642 }
8643 
8644 bool MipsAsmParser::parseInternalDirectiveReallowModule() {
8645   // If this is not the end of the statement, report an error.
8646   if (getLexer().isNot(AsmToken::EndOfStatement)) {
8647     reportParseError("unexpected token, expected end of statement");
8648     return false;
8649   }
8650 
8651   getTargetStreamer().reallowModuleDirective();
8652 
8653   getParser().Lex(); // Eat EndOfStatement token.
8654   return false;
8655 }
8656 
8657 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeMipsAsmParser() {
8658   RegisterMCAsmParser<MipsAsmParser> X(getTheMipsTarget());
8659   RegisterMCAsmParser<MipsAsmParser> Y(getTheMipselTarget());
8660   RegisterMCAsmParser<MipsAsmParser> A(getTheMips64Target());
8661   RegisterMCAsmParser<MipsAsmParser> B(getTheMips64elTarget());
8662 }
8663 
8664 #define GET_REGISTER_MATCHER
8665 #define GET_MATCHER_IMPLEMENTATION
8666 #define GET_MNEMONIC_SPELL_CHECKER
8667 #include "MipsGenAsmMatcher.inc"
8668 
8669 bool MipsAsmParser::mnemonicIsValid(StringRef Mnemonic, unsigned VariantID) {
8670   // Find the appropriate table for this asm variant.
8671   const MatchEntry *Start, *End;
8672   switch (VariantID) {
8673   default: llvm_unreachable("invalid variant!");
8674   case 0: Start = std::begin(MatchTable0); End = std::end(MatchTable0); break;
8675   }
8676   // Search the table.
8677   auto MnemonicRange = std::equal_range(Start, End, Mnemonic, LessOpcode());
8678   return MnemonicRange.first != MnemonicRange.second;
8679 }
8680