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