[mips] Add support for .cpload.
[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/MipsMCExpr.h"
11 #include "MCTargetDesc/MipsMCTargetDesc.h"
12 #include "MipsRegisterInfo.h"
13 #include "MipsTargetStreamer.h"
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCInstBuilder.h"
20 #include "llvm/MC/MCParser/MCAsmLexer.h"
21 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/MC/MCTargetAsmParser.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/TargetRegistry.h"
29
30 using namespace llvm;
31
32 #define DEBUG_TYPE "mips-asm-parser"
33
34 namespace llvm {
35 class MCInstrInfo;
36 }
37
38 namespace {
39 class MipsAssemblerOptions {
40 public:
41   MipsAssemblerOptions() : aTReg(1), reorder(true), macro(true) {}
42
43   unsigned getATRegNum() { return aTReg; }
44   bool setATReg(unsigned Reg);
45
46   bool isReorder() { return reorder; }
47   void setReorder() { reorder = true; }
48   void setNoreorder() { reorder = false; }
49
50   bool isMacro() { return macro; }
51   void setMacro() { macro = true; }
52   void setNomacro() { macro = false; }
53
54 private:
55   unsigned aTReg;
56   bool reorder;
57   bool macro;
58 };
59 }
60
61 namespace {
62 class MipsAsmParser : public MCTargetAsmParser {
63   MipsTargetStreamer &getTargetStreamer() {
64     MCTargetStreamer &TS = *Parser.getStreamer().getTargetStreamer();
65     return static_cast<MipsTargetStreamer &>(TS);
66   }
67
68   MCSubtargetInfo &STI;
69   MCAsmParser &Parser;
70   MipsAssemblerOptions Options;
71
72 #define GET_ASSEMBLER_HEADER
73 #include "MipsGenAsmMatcher.inc"
74
75   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
76                                SmallVectorImpl<MCParsedAsmOperand *> &Operands,
77                                MCStreamer &Out, unsigned &ErrorInfo,
78                                bool MatchingInlineAsm) override;
79
80   /// Parse a register as used in CFI directives
81   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
82
83   bool ParseParenSuffix(StringRef Name,
84                         SmallVectorImpl<MCParsedAsmOperand *> &Operands);
85
86   bool ParseBracketSuffix(StringRef Name,
87                           SmallVectorImpl<MCParsedAsmOperand *> &Operands);
88
89   bool
90   ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
91                    SmallVectorImpl<MCParsedAsmOperand *> &Operands) override;
92
93   bool ParseDirective(AsmToken DirectiveID) override;
94
95   MipsAsmParser::OperandMatchResultTy
96   parseMemOperand(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
97
98   MipsAsmParser::OperandMatchResultTy MatchAnyRegisterNameWithoutDollar(
99       SmallVectorImpl<MCParsedAsmOperand *> &Operands, StringRef Identifier,
100       SMLoc S);
101
102   MipsAsmParser::OperandMatchResultTy
103   MatchAnyRegisterWithoutDollar(SmallVectorImpl<MCParsedAsmOperand *> &Operands,
104                                 SMLoc S);
105
106   MipsAsmParser::OperandMatchResultTy
107   ParseAnyRegister(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
108
109   MipsAsmParser::OperandMatchResultTy
110   ParseImm(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
111
112   MipsAsmParser::OperandMatchResultTy
113   ParseJumpTarget(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
114
115   MipsAsmParser::OperandMatchResultTy
116   parseInvNum(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
117
118   MipsAsmParser::OperandMatchResultTy
119   ParseLSAImm(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
120
121   bool searchSymbolAlias(SmallVectorImpl<MCParsedAsmOperand *> &Operands);
122
123   bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand *> &,
124                     StringRef Mnemonic);
125
126   bool needsExpansion(MCInst &Inst);
127
128   void expandInstruction(MCInst &Inst, SMLoc IDLoc,
129                          SmallVectorImpl<MCInst> &Instructions);
130   void expandLoadImm(MCInst &Inst, SMLoc IDLoc,
131                      SmallVectorImpl<MCInst> &Instructions);
132   void expandLoadAddressImm(MCInst &Inst, SMLoc IDLoc,
133                             SmallVectorImpl<MCInst> &Instructions);
134   void expandLoadAddressReg(MCInst &Inst, SMLoc IDLoc,
135                             SmallVectorImpl<MCInst> &Instructions);
136   void expandMemInst(MCInst &Inst, SMLoc IDLoc,
137                      SmallVectorImpl<MCInst> &Instructions, bool isLoad,
138                      bool isImmOpnd);
139   bool reportParseError(StringRef ErrorMsg);
140   bool reportParseError(SMLoc Loc, StringRef ErrorMsg);
141
142   bool parseMemOffset(const MCExpr *&Res, bool isParenExpr);
143   bool parseRelocOperand(const MCExpr *&Res);
144
145   const MCExpr *evaluateRelocExpr(const MCExpr *Expr, StringRef RelocStr);
146
147   bool isEvaluated(const MCExpr *Expr);
148   bool parseSetFeature(uint64_t Feature);
149   bool parseDirectiveCPLoad(SMLoc Loc);
150   bool parseDirectiveCPSetup();
151   bool parseDirectiveNaN();
152   bool parseDirectiveSet();
153   bool parseDirectiveOption();
154
155   bool parseSetAtDirective();
156   bool parseSetNoAtDirective();
157   bool parseSetMacroDirective();
158   bool parseSetNoMacroDirective();
159   bool parseSetReorderDirective();
160   bool parseSetNoReorderDirective();
161   bool parseSetNoMips16Directive();
162
163   bool parseSetAssignment();
164
165   bool parseDataDirective(unsigned Size, SMLoc L);
166   bool parseDirectiveGpWord();
167   bool parseDirectiveGpDWord();
168
169   MCSymbolRefExpr::VariantKind getVariantKind(StringRef Symbol);
170
171   bool isGP64() const {
172     return (STI.getFeatureBits() & Mips::FeatureGP64Bit) != 0;
173   }
174
175   bool isFP64() const {
176     return (STI.getFeatureBits() & Mips::FeatureFP64Bit) != 0;
177   }
178
179   bool isN32() const { return STI.getFeatureBits() & Mips::FeatureN32; }
180   bool isN64() const { return STI.getFeatureBits() & Mips::FeatureN64; }
181
182   bool isMicroMips() const {
183     return STI.getFeatureBits() & Mips::FeatureMicroMips;
184   }
185
186   bool parseRegister(unsigned &RegNum);
187
188   bool eatComma(StringRef ErrorStr);
189
190   int matchCPURegisterName(StringRef Symbol);
191
192   int matchRegisterByNumber(unsigned RegNum, unsigned RegClass);
193
194   int matchFPURegisterName(StringRef Name);
195
196   int matchFCCRegisterName(StringRef Name);
197
198   int matchACRegisterName(StringRef Name);
199
200   int matchMSA128RegisterName(StringRef Name);
201
202   int matchMSA128CtrlRegisterName(StringRef Name);
203
204   unsigned getReg(int RC, int RegNo);
205
206   unsigned getGPR(int RegNo);
207
208   int getATReg();
209
210   bool processInstruction(MCInst &Inst, SMLoc IDLoc,
211                           SmallVectorImpl<MCInst> &Instructions);
212
213   // Helper function that checks if the value of a vector index is within the
214   // boundaries of accepted values for each RegisterKind
215   // Example: INSERT.B $w0[n], $1 => 16 > n >= 0
216   bool validateMSAIndex(int Val, int RegKind);
217
218   void setFeatureBits(unsigned Feature, StringRef FeatureString) {
219     if (!(STI.getFeatureBits() & Feature)) {
220       setAvailableFeatures(
221           ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
222     }
223   }
224
225   void clearFeatureBits(unsigned Feature, StringRef FeatureString) {
226     if (STI.getFeatureBits() & Feature) {
227       setAvailableFeatures(
228           ComputeAvailableFeatures(STI.ToggleFeature(FeatureString)));
229     }
230   }
231
232 public:
233   MipsAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser,
234                 const MCInstrInfo &MII,
235                 const MCTargetOptions &Options)
236       : MCTargetAsmParser(), STI(sti), Parser(parser) {
237     // Initialize the set of available features.
238     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
239
240     // Assert exactly one ABI was chosen.
241     assert((((STI.getFeatureBits() & Mips::FeatureO32) != 0) +
242             ((STI.getFeatureBits() & Mips::FeatureEABI) != 0) +
243             ((STI.getFeatureBits() & Mips::FeatureN32) != 0) +
244             ((STI.getFeatureBits() & Mips::FeatureN64) != 0)) == 1);
245   }
246
247   MCAsmParser &getParser() const { return Parser; }
248   MCAsmLexer &getLexer() const { return Parser.getLexer(); }
249
250   /// Warn if RegNo is the current assembler temporary.
251   void WarnIfAssemblerTemporary(int RegNo, SMLoc Loc);
252 };
253 }
254
255 namespace {
256
257 /// MipsOperand - Instances of this class represent a parsed Mips machine
258 /// instruction.
259 class MipsOperand : public MCParsedAsmOperand {
260 public:
261   /// Broad categories of register classes
262   /// The exact class is finalized by the render method.
263   enum RegKind {
264     RegKind_GPR = 1,      /// GPR32 and GPR64 (depending on isGP64())
265     RegKind_FGR = 2,      /// FGR32, FGR64, AFGR64 (depending on context and
266                           /// isFP64())
267     RegKind_FCC = 4,      /// FCC
268     RegKind_MSA128 = 8,   /// MSA128[BHWD] (makes no difference which)
269     RegKind_MSACtrl = 16, /// MSA control registers
270     RegKind_COP2 = 32,    /// COP2
271     RegKind_ACC = 64,     /// HI32DSP, LO32DSP, and ACC64DSP (depending on
272                           /// context).
273     RegKind_CCR = 128,    /// CCR
274     RegKind_HWRegs = 256, /// HWRegs
275
276     /// Potentially any (e.g. $1)
277     RegKind_Numeric = RegKind_GPR | RegKind_FGR | RegKind_FCC | RegKind_MSA128 |
278                       RegKind_MSACtrl | RegKind_COP2 | RegKind_ACC |
279                       RegKind_CCR | RegKind_HWRegs
280   };
281
282 private:
283   enum KindTy {
284     k_Immediate,     /// An immediate (possibly involving symbol references)
285     k_Memory,        /// Base + Offset Memory Address
286     k_PhysRegister,  /// A physical register from the Mips namespace
287     k_RegisterIndex, /// A register index in one or more RegKind.
288     k_Token          /// A simple token
289   } Kind;
290
291   MipsOperand(KindTy K, MipsAsmParser &Parser)
292       : MCParsedAsmOperand(), Kind(K), AsmParser(Parser) {}
293
294   /// For diagnostics, and checking the assembler temporary
295   MipsAsmParser &AsmParser;
296
297   struct Token {
298     const char *Data;
299     unsigned Length;
300   };
301
302   struct PhysRegOp {
303     unsigned Num; /// Register Number
304   };
305
306   struct RegIdxOp {
307     unsigned Index; /// Index into the register class
308     RegKind Kind;   /// Bitfield of the kinds it could possibly be
309     const MCRegisterInfo *RegInfo;
310   };
311
312   struct ImmOp {
313     const MCExpr *Val;
314   };
315
316   struct MemOp {
317     MipsOperand *Base;
318     const MCExpr *Off;
319   };
320
321   union {
322     struct Token Tok;
323     struct PhysRegOp PhysReg;
324     struct RegIdxOp RegIdx;
325     struct ImmOp Imm;
326     struct MemOp Mem;
327   };
328
329   SMLoc StartLoc, EndLoc;
330
331   /// Internal constructor for register kinds
332   static MipsOperand *CreateReg(unsigned Index, RegKind RegKind,
333                                 const MCRegisterInfo *RegInfo, SMLoc S, SMLoc E,
334                                 MipsAsmParser &Parser) {
335     MipsOperand *Op = new MipsOperand(k_RegisterIndex, Parser);
336     Op->RegIdx.Index = Index;
337     Op->RegIdx.RegInfo = RegInfo;
338     Op->RegIdx.Kind = RegKind;
339     Op->StartLoc = S;
340     Op->EndLoc = E;
341     return Op;
342   }
343
344 public:
345   /// Coerce the register to GPR32 and return the real register for the current
346   /// target.
347   unsigned getGPR32Reg() const {
348     assert(isRegIdx() && (RegIdx.Kind & RegKind_GPR) && "Invalid access!");
349     AsmParser.WarnIfAssemblerTemporary(RegIdx.Index, StartLoc);
350     unsigned ClassID = Mips::GPR32RegClassID;
351     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
352   }
353
354   /// Coerce the register to GPR64 and return the real register for the current
355   /// target.
356   unsigned getGPR64Reg() const {
357     assert(isRegIdx() && (RegIdx.Kind & RegKind_GPR) && "Invalid access!");
358     unsigned ClassID = Mips::GPR64RegClassID;
359     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
360   }
361
362 private:
363   /// Coerce the register to AFGR64 and return the real register for the current
364   /// target.
365   unsigned getAFGR64Reg() const {
366     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
367     if (RegIdx.Index % 2 != 0)
368       AsmParser.Warning(StartLoc, "Float register should be even.");
369     return RegIdx.RegInfo->getRegClass(Mips::AFGR64RegClassID)
370         .getRegister(RegIdx.Index / 2);
371   }
372
373   /// Coerce the register to FGR64 and return the real register for the current
374   /// target.
375   unsigned getFGR64Reg() const {
376     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
377     return RegIdx.RegInfo->getRegClass(Mips::FGR64RegClassID)
378         .getRegister(RegIdx.Index);
379   }
380
381   /// Coerce the register to FGR32 and return the real register for the current
382   /// target.
383   unsigned getFGR32Reg() const {
384     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
385     return RegIdx.RegInfo->getRegClass(Mips::FGR32RegClassID)
386         .getRegister(RegIdx.Index);
387   }
388
389   /// Coerce the register to FGRH32 and return the real register for the current
390   /// target.
391   unsigned getFGRH32Reg() const {
392     assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!");
393     return RegIdx.RegInfo->getRegClass(Mips::FGRH32RegClassID)
394         .getRegister(RegIdx.Index);
395   }
396
397   /// Coerce the register to FCC and return the real register for the current
398   /// target.
399   unsigned getFCCReg() const {
400     assert(isRegIdx() && (RegIdx.Kind & RegKind_FCC) && "Invalid access!");
401     return RegIdx.RegInfo->getRegClass(Mips::FCCRegClassID)
402         .getRegister(RegIdx.Index);
403   }
404
405   /// Coerce the register to MSA128 and return the real register for the current
406   /// target.
407   unsigned getMSA128Reg() const {
408     assert(isRegIdx() && (RegIdx.Kind & RegKind_MSA128) && "Invalid access!");
409     // It doesn't matter which of the MSA128[BHWD] classes we use. They are all
410     // identical
411     unsigned ClassID = Mips::MSA128BRegClassID;
412     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
413   }
414
415   /// Coerce the register to MSACtrl and return the real register for the
416   /// current target.
417   unsigned getMSACtrlReg() const {
418     assert(isRegIdx() && (RegIdx.Kind & RegKind_MSACtrl) && "Invalid access!");
419     unsigned ClassID = Mips::MSACtrlRegClassID;
420     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
421   }
422
423   /// Coerce the register to COP2 and return the real register for the
424   /// current target.
425   unsigned getCOP2Reg() const {
426     assert(isRegIdx() && (RegIdx.Kind & RegKind_COP2) && "Invalid access!");
427     unsigned ClassID = Mips::COP2RegClassID;
428     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
429   }
430
431   /// Coerce the register to ACC64DSP and return the real register for the
432   /// current target.
433   unsigned getACC64DSPReg() const {
434     assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!");
435     unsigned ClassID = Mips::ACC64DSPRegClassID;
436     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
437   }
438
439   /// Coerce the register to HI32DSP and return the real register for the
440   /// current target.
441   unsigned getHI32DSPReg() const {
442     assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!");
443     unsigned ClassID = Mips::HI32DSPRegClassID;
444     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
445   }
446
447   /// Coerce the register to LO32DSP and return the real register for the
448   /// current target.
449   unsigned getLO32DSPReg() const {
450     assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!");
451     unsigned ClassID = Mips::LO32DSPRegClassID;
452     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
453   }
454
455   /// Coerce the register to CCR and return the real register for the
456   /// current target.
457   unsigned getCCRReg() const {
458     assert(isRegIdx() && (RegIdx.Kind & RegKind_CCR) && "Invalid access!");
459     unsigned ClassID = Mips::CCRRegClassID;
460     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
461   }
462
463   /// Coerce the register to HWRegs and return the real register for the
464   /// current target.
465   unsigned getHWRegsReg() const {
466     assert(isRegIdx() && (RegIdx.Kind & RegKind_HWRegs) && "Invalid access!");
467     unsigned ClassID = Mips::HWRegsRegClassID;
468     return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index);
469   }
470
471 public:
472   void addExpr(MCInst &Inst, const MCExpr *Expr) const {
473     // Add as immediate when possible.  Null MCExpr = 0.
474     if (!Expr)
475       Inst.addOperand(MCOperand::CreateImm(0));
476     else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
477       Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
478     else
479       Inst.addOperand(MCOperand::CreateExpr(Expr));
480   }
481
482   void addRegOperands(MCInst &Inst, unsigned N) const {
483     llvm_unreachable("Use a custom parser instead");
484   }
485
486   /// Render the operand to an MCInst as a GPR32
487   /// Asserts if the wrong number of operands are requested, or the operand
488   /// is not a k_RegisterIndex compatible with RegKind_GPR
489   void addGPR32AsmRegOperands(MCInst &Inst, unsigned N) const {
490     assert(N == 1 && "Invalid number of operands!");
491     Inst.addOperand(MCOperand::CreateReg(getGPR32Reg()));
492   }
493
494   /// Render the operand to an MCInst as a GPR64
495   /// Asserts if the wrong number of operands are requested, or the operand
496   /// is not a k_RegisterIndex compatible with RegKind_GPR
497   void addGPR64AsmRegOperands(MCInst &Inst, unsigned N) const {
498     assert(N == 1 && "Invalid number of operands!");
499     Inst.addOperand(MCOperand::CreateReg(getGPR64Reg()));
500   }
501
502   void addAFGR64AsmRegOperands(MCInst &Inst, unsigned N) const {
503     assert(N == 1 && "Invalid number of operands!");
504     Inst.addOperand(MCOperand::CreateReg(getAFGR64Reg()));
505   }
506
507   void addFGR64AsmRegOperands(MCInst &Inst, unsigned N) const {
508     assert(N == 1 && "Invalid number of operands!");
509     Inst.addOperand(MCOperand::CreateReg(getFGR64Reg()));
510   }
511
512   void addFGR32AsmRegOperands(MCInst &Inst, unsigned N) const {
513     assert(N == 1 && "Invalid number of operands!");
514     Inst.addOperand(MCOperand::CreateReg(getFGR32Reg()));
515   }
516
517   void addFGRH32AsmRegOperands(MCInst &Inst, unsigned N) const {
518     assert(N == 1 && "Invalid number of operands!");
519     Inst.addOperand(MCOperand::CreateReg(getFGRH32Reg()));
520   }
521
522   void addFCCAsmRegOperands(MCInst &Inst, unsigned N) const {
523     assert(N == 1 && "Invalid number of operands!");
524     Inst.addOperand(MCOperand::CreateReg(getFCCReg()));
525   }
526
527   void addMSA128AsmRegOperands(MCInst &Inst, unsigned N) const {
528     assert(N == 1 && "Invalid number of operands!");
529     Inst.addOperand(MCOperand::CreateReg(getMSA128Reg()));
530   }
531
532   void addMSACtrlAsmRegOperands(MCInst &Inst, unsigned N) const {
533     assert(N == 1 && "Invalid number of operands!");
534     Inst.addOperand(MCOperand::CreateReg(getMSACtrlReg()));
535   }
536
537   void addCOP2AsmRegOperands(MCInst &Inst, unsigned N) const {
538     assert(N == 1 && "Invalid number of operands!");
539     Inst.addOperand(MCOperand::CreateReg(getCOP2Reg()));
540   }
541
542   void addACC64DSPAsmRegOperands(MCInst &Inst, unsigned N) const {
543     assert(N == 1 && "Invalid number of operands!");
544     Inst.addOperand(MCOperand::CreateReg(getACC64DSPReg()));
545   }
546
547   void addHI32DSPAsmRegOperands(MCInst &Inst, unsigned N) const {
548     assert(N == 1 && "Invalid number of operands!");
549     Inst.addOperand(MCOperand::CreateReg(getHI32DSPReg()));
550   }
551
552   void addLO32DSPAsmRegOperands(MCInst &Inst, unsigned N) const {
553     assert(N == 1 && "Invalid number of operands!");
554     Inst.addOperand(MCOperand::CreateReg(getLO32DSPReg()));
555   }
556
557   void addCCRAsmRegOperands(MCInst &Inst, unsigned N) const {
558     assert(N == 1 && "Invalid number of operands!");
559     Inst.addOperand(MCOperand::CreateReg(getCCRReg()));
560   }
561
562   void addHWRegsAsmRegOperands(MCInst &Inst, unsigned N) const {
563     assert(N == 1 && "Invalid number of operands!");
564     Inst.addOperand(MCOperand::CreateReg(getHWRegsReg()));
565   }
566
567   void addImmOperands(MCInst &Inst, unsigned N) const {
568     assert(N == 1 && "Invalid number of operands!");
569     const MCExpr *Expr = getImm();
570     addExpr(Inst, Expr);
571   }
572
573   void addMemOperands(MCInst &Inst, unsigned N) const {
574     assert(N == 2 && "Invalid number of operands!");
575
576     Inst.addOperand(MCOperand::CreateReg(getMemBase()->getGPR32Reg()));
577
578     const MCExpr *Expr = getMemOff();
579     addExpr(Inst, Expr);
580   }
581
582   bool isReg() const override {
583     // As a special case until we sort out the definition of div/divu, pretend
584     // that $0/$zero are k_PhysRegister so that MCK_ZERO works correctly.
585     if (isGPRAsmReg() && RegIdx.Index == 0)
586       return true;
587
588     return Kind == k_PhysRegister;
589   }
590   bool isRegIdx() const { return Kind == k_RegisterIndex; }
591   bool isImm() const override { return Kind == k_Immediate; }
592   bool isConstantImm() const {
593     return isImm() && dyn_cast<MCConstantExpr>(getImm());
594   }
595   bool isToken() const override {
596     // Note: It's not possible to pretend that other operand kinds are tokens.
597     // The matcher emitter checks tokens first.
598     return Kind == k_Token;
599   }
600   bool isMem() const override { return Kind == k_Memory; }
601   bool isInvNum() const { return Kind == k_Immediate; }
602   bool isLSAImm() const {
603     if (!isConstantImm())
604       return false;
605     int64_t Val = getConstantImm();
606     return 1 <= Val && Val <= 4;
607   }
608
609   StringRef getToken() const {
610     assert(Kind == k_Token && "Invalid access!");
611     return StringRef(Tok.Data, Tok.Length);
612   }
613
614   unsigned getReg() const override {
615     // As a special case until we sort out the definition of div/divu, pretend
616     // that $0/$zero are k_PhysRegister so that MCK_ZERO works correctly.
617     if (Kind == k_RegisterIndex && RegIdx.Index == 0 &&
618         RegIdx.Kind & RegKind_GPR)
619       return getGPR32Reg(); // FIXME: GPR64 too
620
621     assert(Kind == k_PhysRegister && "Invalid access!");
622     return PhysReg.Num;
623   }
624
625   const MCExpr *getImm() const {
626     assert((Kind == k_Immediate) && "Invalid access!");
627     return Imm.Val;
628   }
629
630   int64_t getConstantImm() const {
631     const MCExpr *Val = getImm();
632     return static_cast<const MCConstantExpr *>(Val)->getValue();
633   }
634
635   MipsOperand *getMemBase() const {
636     assert((Kind == k_Memory) && "Invalid access!");
637     return Mem.Base;
638   }
639
640   const MCExpr *getMemOff() const {
641     assert((Kind == k_Memory) && "Invalid access!");
642     return Mem.Off;
643   }
644
645   static MipsOperand *CreateToken(StringRef Str, SMLoc S,
646                                   MipsAsmParser &Parser) {
647     MipsOperand *Op = new MipsOperand(k_Token, Parser);
648     Op->Tok.Data = Str.data();
649     Op->Tok.Length = Str.size();
650     Op->StartLoc = S;
651     Op->EndLoc = S;
652     return Op;
653   }
654
655   /// Create a numeric register (e.g. $1). The exact register remains
656   /// unresolved until an instruction successfully matches
657   static MipsOperand *CreateNumericReg(unsigned Index,
658                                        const MCRegisterInfo *RegInfo, SMLoc S,
659                                        SMLoc E, MipsAsmParser &Parser) {
660     DEBUG(dbgs() << "CreateNumericReg(" << Index << ", ...)\n");
661     return CreateReg(Index, RegKind_Numeric, RegInfo, S, E, Parser);
662   }
663
664   /// Create a register that is definitely a GPR.
665   /// This is typically only used for named registers such as $gp.
666   static MipsOperand *CreateGPRReg(unsigned Index,
667                                    const MCRegisterInfo *RegInfo, SMLoc S,
668                                    SMLoc E, MipsAsmParser &Parser) {
669     return CreateReg(Index, RegKind_GPR, RegInfo, S, E, Parser);
670   }
671
672   /// Create a register that is definitely a FGR.
673   /// This is typically only used for named registers such as $f0.
674   static MipsOperand *CreateFGRReg(unsigned Index,
675                                    const MCRegisterInfo *RegInfo, SMLoc S,
676                                    SMLoc E, MipsAsmParser &Parser) {
677     return CreateReg(Index, RegKind_FGR, RegInfo, S, E, Parser);
678   }
679
680   /// Create a register that is definitely an FCC.
681   /// This is typically only used for named registers such as $fcc0.
682   static MipsOperand *CreateFCCReg(unsigned Index,
683                                    const MCRegisterInfo *RegInfo, SMLoc S,
684                                    SMLoc E, MipsAsmParser &Parser) {
685     return CreateReg(Index, RegKind_FCC, RegInfo, S, E, Parser);
686   }
687
688   /// Create a register that is definitely an ACC.
689   /// This is typically only used for named registers such as $ac0.
690   static MipsOperand *CreateACCReg(unsigned Index,
691                                    const MCRegisterInfo *RegInfo, SMLoc S,
692                                    SMLoc E, MipsAsmParser &Parser) {
693     return CreateReg(Index, RegKind_ACC, RegInfo, S, E, Parser);
694   }
695
696   /// Create a register that is definitely an MSA128.
697   /// This is typically only used for named registers such as $w0.
698   static MipsOperand *CreateMSA128Reg(unsigned Index,
699                                       const MCRegisterInfo *RegInfo, SMLoc S,
700                                       SMLoc E, MipsAsmParser &Parser) {
701     return CreateReg(Index, RegKind_MSA128, RegInfo, S, E, Parser);
702   }
703
704   /// Create a register that is definitely an MSACtrl.
705   /// This is typically only used for named registers such as $msaaccess.
706   static MipsOperand *CreateMSACtrlReg(unsigned Index,
707                                        const MCRegisterInfo *RegInfo, SMLoc S,
708                                        SMLoc E, MipsAsmParser &Parser) {
709     return CreateReg(Index, RegKind_MSACtrl, RegInfo, S, E, Parser);
710   }
711
712   static MipsOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E,
713                                 MipsAsmParser &Parser) {
714     MipsOperand *Op = new MipsOperand(k_Immediate, Parser);
715     Op->Imm.Val = Val;
716     Op->StartLoc = S;
717     Op->EndLoc = E;
718     return Op;
719   }
720
721   static MipsOperand *CreateMem(MipsOperand *Base, const MCExpr *Off, SMLoc S,
722                                 SMLoc E, MipsAsmParser &Parser) {
723     MipsOperand *Op = new MipsOperand(k_Memory, Parser);
724     Op->Mem.Base = Base;
725     Op->Mem.Off = Off;
726     Op->StartLoc = S;
727     Op->EndLoc = E;
728     return Op;
729   }
730
731   bool isGPRAsmReg() const {
732     return isRegIdx() && RegIdx.Kind & RegKind_GPR && RegIdx.Index <= 31;
733   }
734   bool isFGRAsmReg() const {
735     // AFGR64 is $0-$15 but we handle this in getAFGR64()
736     return isRegIdx() && RegIdx.Kind & RegKind_FGR && RegIdx.Index <= 31;
737   }
738   bool isHWRegsAsmReg() const {
739     return isRegIdx() && RegIdx.Kind & RegKind_HWRegs && RegIdx.Index <= 31;
740   }
741   bool isCCRAsmReg() const {
742     return isRegIdx() && RegIdx.Kind & RegKind_CCR && RegIdx.Index <= 31;
743   }
744   bool isFCCAsmReg() const {
745     return isRegIdx() && RegIdx.Kind & RegKind_FCC && RegIdx.Index <= 7;
746   }
747   bool isACCAsmReg() const {
748     return isRegIdx() && RegIdx.Kind & RegKind_ACC && RegIdx.Index <= 3;
749   }
750   bool isCOP2AsmReg() const {
751     return isRegIdx() && RegIdx.Kind & RegKind_COP2 && RegIdx.Index <= 31;
752   }
753   bool isMSA128AsmReg() const {
754     return isRegIdx() && RegIdx.Kind & RegKind_MSA128 && RegIdx.Index <= 31;
755   }
756   bool isMSACtrlAsmReg() const {
757     return isRegIdx() && RegIdx.Kind & RegKind_MSACtrl && RegIdx.Index <= 7;
758   }
759
760   /// getStartLoc - Get the location of the first token of this operand.
761   SMLoc getStartLoc() const override { return StartLoc; }
762   /// getEndLoc - Get the location of the last token of this operand.
763   SMLoc getEndLoc() const override { return EndLoc; }
764
765   virtual ~MipsOperand() {
766     switch (Kind) {
767     case k_Immediate:
768       break;
769     case k_Memory:
770       delete Mem.Base;
771       break;
772     case k_PhysRegister:
773     case k_RegisterIndex:
774     case k_Token:
775       break;
776     }
777   }
778
779   void print(raw_ostream &OS) const override {
780     switch (Kind) {
781     case k_Immediate:
782       OS << "Imm<";
783       Imm.Val->print(OS);
784       OS << ">";
785       break;
786     case k_Memory:
787       OS << "Mem<";
788       Mem.Base->print(OS);
789       OS << ", ";
790       Mem.Off->print(OS);
791       OS << ">";
792       break;
793     case k_PhysRegister:
794       OS << "PhysReg<" << PhysReg.Num << ">";
795       break;
796     case k_RegisterIndex:
797       OS << "RegIdx<" << RegIdx.Index << ":" << RegIdx.Kind << ">";
798       break;
799     case k_Token:
800       OS << Tok.Data;
801       break;
802     }
803   }
804 }; // class MipsOperand
805 } // namespace
806
807 namespace llvm {
808 extern const MCInstrDesc MipsInsts[];
809 }
810 static const MCInstrDesc &getInstDesc(unsigned Opcode) {
811   return MipsInsts[Opcode];
812 }
813
814 bool MipsAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
815                                        SmallVectorImpl<MCInst> &Instructions) {
816   const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
817
818   Inst.setLoc(IDLoc);
819
820   if (MCID.isBranch() || MCID.isCall()) {
821     const unsigned Opcode = Inst.getOpcode();
822     MCOperand Offset;
823
824     switch (Opcode) {
825     default:
826       break;
827     case Mips::BEQ:
828     case Mips::BNE:
829     case Mips::BEQ_MM:
830     case Mips::BNE_MM:
831       assert(MCID.getNumOperands() == 3 && "unexpected number of operands");
832       Offset = Inst.getOperand(2);
833       if (!Offset.isImm())
834         break; // We'll deal with this situation later on when applying fixups.
835       if (!isIntN(isMicroMips() ? 17 : 18, Offset.getImm()))
836         return Error(IDLoc, "branch target out of range");
837       if (OffsetToAlignment(Offset.getImm(), 1LL << (isMicroMips() ? 1 : 2)))
838         return Error(IDLoc, "branch to misaligned address");
839       break;
840     case Mips::BGEZ:
841     case Mips::BGTZ:
842     case Mips::BLEZ:
843     case Mips::BLTZ:
844     case Mips::BGEZAL:
845     case Mips::BLTZAL:
846     case Mips::BC1F:
847     case Mips::BC1T:
848     case Mips::BGEZ_MM:
849     case Mips::BGTZ_MM:
850     case Mips::BLEZ_MM:
851     case Mips::BLTZ_MM:
852     case Mips::BGEZAL_MM:
853     case Mips::BLTZAL_MM:
854     case Mips::BC1F_MM:
855     case Mips::BC1T_MM:
856       assert(MCID.getNumOperands() == 2 && "unexpected number of operands");
857       Offset = Inst.getOperand(1);
858       if (!Offset.isImm())
859         break; // We'll deal with this situation later on when applying fixups.
860       if (!isIntN(isMicroMips() ? 17 : 18, Offset.getImm()))
861         return Error(IDLoc, "branch target out of range");
862       if (OffsetToAlignment(Offset.getImm(), 1LL << (isMicroMips() ? 1 : 2)))
863         return Error(IDLoc, "branch to misaligned address");
864       break;
865     }
866   }
867
868   if (MCID.hasDelaySlot() && Options.isReorder()) {
869     // If this instruction has a delay slot and .set reorder is active,
870     // emit a NOP after it.
871     Instructions.push_back(Inst);
872     MCInst NopInst;
873     NopInst.setOpcode(Mips::SLL);
874     NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
875     NopInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
876     NopInst.addOperand(MCOperand::CreateImm(0));
877     Instructions.push_back(NopInst);
878     return false;
879   }
880
881   if (MCID.mayLoad() || MCID.mayStore()) {
882     // Check the offset of memory operand, if it is a symbol
883     // reference or immediate we may have to expand instructions.
884     for (unsigned i = 0; i < MCID.getNumOperands(); i++) {
885       const MCOperandInfo &OpInfo = MCID.OpInfo[i];
886       if ((OpInfo.OperandType == MCOI::OPERAND_MEMORY) ||
887           (OpInfo.OperandType == MCOI::OPERAND_UNKNOWN)) {
888         MCOperand &Op = Inst.getOperand(i);
889         if (Op.isImm()) {
890           int MemOffset = Op.getImm();
891           if (MemOffset < -32768 || MemOffset > 32767) {
892             // Offset can't exceed 16bit value.
893             expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), true);
894             return false;
895           }
896         } else if (Op.isExpr()) {
897           const MCExpr *Expr = Op.getExpr();
898           if (Expr->getKind() == MCExpr::SymbolRef) {
899             const MCSymbolRefExpr *SR =
900                 static_cast<const MCSymbolRefExpr *>(Expr);
901             if (SR->getKind() == MCSymbolRefExpr::VK_None) {
902               // Expand symbol.
903               expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), false);
904               return false;
905             }
906           } else if (!isEvaluated(Expr)) {
907             expandMemInst(Inst, IDLoc, Instructions, MCID.mayLoad(), false);
908             return false;
909           }
910         }
911       }
912     } // for
913   }   // if load/store
914
915   if (needsExpansion(Inst))
916     expandInstruction(Inst, IDLoc, Instructions);
917   else
918     Instructions.push_back(Inst);
919
920   return false;
921 }
922
923 bool MipsAsmParser::needsExpansion(MCInst &Inst) {
924
925   switch (Inst.getOpcode()) {
926   case Mips::LoadImm32Reg:
927   case Mips::LoadAddr32Imm:
928   case Mips::LoadAddr32Reg:
929     return true;
930   default:
931     return false;
932   }
933 }
934
935 void MipsAsmParser::expandInstruction(MCInst &Inst, SMLoc IDLoc,
936                                       SmallVectorImpl<MCInst> &Instructions) {
937   switch (Inst.getOpcode()) {
938   case Mips::LoadImm32Reg:
939     return expandLoadImm(Inst, IDLoc, Instructions);
940   case Mips::LoadAddr32Imm:
941     return expandLoadAddressImm(Inst, IDLoc, Instructions);
942   case Mips::LoadAddr32Reg:
943     return expandLoadAddressReg(Inst, IDLoc, Instructions);
944   }
945 }
946
947 void MipsAsmParser::expandLoadImm(MCInst &Inst, SMLoc IDLoc,
948                                   SmallVectorImpl<MCInst> &Instructions) {
949   MCInst tmpInst;
950   const MCOperand &ImmOp = Inst.getOperand(1);
951   assert(ImmOp.isImm() && "expected immediate operand kind");
952   const MCOperand &RegOp = Inst.getOperand(0);
953   assert(RegOp.isReg() && "expected register operand kind");
954
955   int ImmValue = ImmOp.getImm();
956   tmpInst.setLoc(IDLoc);
957   if (0 <= ImmValue && ImmValue <= 65535) {
958     // For 0 <= j <= 65535.
959     // li d,j => ori d,$zero,j
960     tmpInst.setOpcode(Mips::ORi);
961     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
962     tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
963     tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
964     Instructions.push_back(tmpInst);
965   } else if (ImmValue < 0 && ImmValue >= -32768) {
966     // For -32768 <= j < 0.
967     // li d,j => addiu d,$zero,j
968     tmpInst.setOpcode(Mips::ADDiu);
969     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
970     tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
971     tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
972     Instructions.push_back(tmpInst);
973   } else {
974     // For any other value of j that is representable as a 32-bit integer.
975     // li d,j => lui d,hi16(j)
976     //           ori d,d,lo16(j)
977     tmpInst.setOpcode(Mips::LUi);
978     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
979     tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
980     Instructions.push_back(tmpInst);
981     tmpInst.clear();
982     tmpInst.setOpcode(Mips::ORi);
983     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
984     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
985     tmpInst.addOperand(MCOperand::CreateImm(ImmValue & 0xffff));
986     tmpInst.setLoc(IDLoc);
987     Instructions.push_back(tmpInst);
988   }
989 }
990
991 void
992 MipsAsmParser::expandLoadAddressReg(MCInst &Inst, SMLoc IDLoc,
993                                     SmallVectorImpl<MCInst> &Instructions) {
994   MCInst tmpInst;
995   const MCOperand &ImmOp = Inst.getOperand(2);
996   assert(ImmOp.isImm() && "expected immediate operand kind");
997   const MCOperand &SrcRegOp = Inst.getOperand(1);
998   assert(SrcRegOp.isReg() && "expected register operand kind");
999   const MCOperand &DstRegOp = Inst.getOperand(0);
1000   assert(DstRegOp.isReg() && "expected register operand kind");
1001   int ImmValue = ImmOp.getImm();
1002   if (-32768 <= ImmValue && ImmValue <= 65535) {
1003     // For -32768 <= j <= 65535.
1004     // la d,j(s) => addiu d,s,j
1005     tmpInst.setOpcode(Mips::ADDiu);
1006     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1007     tmpInst.addOperand(MCOperand::CreateReg(SrcRegOp.getReg()));
1008     tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
1009     Instructions.push_back(tmpInst);
1010   } else {
1011     // For any other value of j that is representable as a 32-bit integer.
1012     // la d,j(s) => lui d,hi16(j)
1013     //              ori d,d,lo16(j)
1014     //              addu d,d,s
1015     tmpInst.setOpcode(Mips::LUi);
1016     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1017     tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
1018     Instructions.push_back(tmpInst);
1019     tmpInst.clear();
1020     tmpInst.setOpcode(Mips::ORi);
1021     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1022     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1023     tmpInst.addOperand(MCOperand::CreateImm(ImmValue & 0xffff));
1024     Instructions.push_back(tmpInst);
1025     tmpInst.clear();
1026     tmpInst.setOpcode(Mips::ADDu);
1027     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1028     tmpInst.addOperand(MCOperand::CreateReg(DstRegOp.getReg()));
1029     tmpInst.addOperand(MCOperand::CreateReg(SrcRegOp.getReg()));
1030     Instructions.push_back(tmpInst);
1031   }
1032 }
1033
1034 void
1035 MipsAsmParser::expandLoadAddressImm(MCInst &Inst, SMLoc IDLoc,
1036                                     SmallVectorImpl<MCInst> &Instructions) {
1037   MCInst tmpInst;
1038   const MCOperand &ImmOp = Inst.getOperand(1);
1039   assert(ImmOp.isImm() && "expected immediate operand kind");
1040   const MCOperand &RegOp = Inst.getOperand(0);
1041   assert(RegOp.isReg() && "expected register operand kind");
1042   int ImmValue = ImmOp.getImm();
1043   if (-32768 <= ImmValue && ImmValue <= 65535) {
1044     // For -32768 <= j <= 65535.
1045     // la d,j => addiu d,$zero,j
1046     tmpInst.setOpcode(Mips::ADDiu);
1047     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1048     tmpInst.addOperand(MCOperand::CreateReg(Mips::ZERO));
1049     tmpInst.addOperand(MCOperand::CreateImm(ImmValue));
1050     Instructions.push_back(tmpInst);
1051   } else {
1052     // For any other value of j that is representable as a 32-bit integer.
1053     // la d,j => lui d,hi16(j)
1054     //           ori d,d,lo16(j)
1055     tmpInst.setOpcode(Mips::LUi);
1056     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1057     tmpInst.addOperand(MCOperand::CreateImm((ImmValue & 0xffff0000) >> 16));
1058     Instructions.push_back(tmpInst);
1059     tmpInst.clear();
1060     tmpInst.setOpcode(Mips::ORi);
1061     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1062     tmpInst.addOperand(MCOperand::CreateReg(RegOp.getReg()));
1063     tmpInst.addOperand(MCOperand::CreateImm(ImmValue & 0xffff));
1064     Instructions.push_back(tmpInst);
1065   }
1066 }
1067
1068 void MipsAsmParser::expandMemInst(MCInst &Inst, SMLoc IDLoc,
1069                                   SmallVectorImpl<MCInst> &Instructions,
1070                                   bool isLoad, bool isImmOpnd) {
1071   const MCSymbolRefExpr *SR;
1072   MCInst TempInst;
1073   unsigned ImmOffset, HiOffset, LoOffset;
1074   const MCExpr *ExprOffset;
1075   unsigned TmpRegNum;
1076   unsigned AtRegNum = getReg(
1077       (isGP64()) ? Mips::GPR64RegClassID : Mips::GPR32RegClassID, getATReg());
1078   // 1st operand is either the source or destination register.
1079   assert(Inst.getOperand(0).isReg() && "expected register operand kind");
1080   unsigned RegOpNum = Inst.getOperand(0).getReg();
1081   // 2nd operand is the base register.
1082   assert(Inst.getOperand(1).isReg() && "expected register operand kind");
1083   unsigned BaseRegNum = Inst.getOperand(1).getReg();
1084   // 3rd operand is either an immediate or expression.
1085   if (isImmOpnd) {
1086     assert(Inst.getOperand(2).isImm() && "expected immediate operand kind");
1087     ImmOffset = Inst.getOperand(2).getImm();
1088     LoOffset = ImmOffset & 0x0000ffff;
1089     HiOffset = (ImmOffset & 0xffff0000) >> 16;
1090     // If msb of LoOffset is 1(negative number) we must increment HiOffset.
1091     if (LoOffset & 0x8000)
1092       HiOffset++;
1093   } else
1094     ExprOffset = Inst.getOperand(2).getExpr();
1095   // All instructions will have the same location.
1096   TempInst.setLoc(IDLoc);
1097   // 1st instruction in expansion is LUi. For load instruction we can use
1098   // the dst register as a temporary if base and dst are different,
1099   // but for stores we must use $at.
1100   TmpRegNum = (isLoad && (BaseRegNum != RegOpNum)) ? RegOpNum : AtRegNum;
1101   TempInst.setOpcode(Mips::LUi);
1102   TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
1103   if (isImmOpnd)
1104     TempInst.addOperand(MCOperand::CreateImm(HiOffset));
1105   else {
1106     if (ExprOffset->getKind() == MCExpr::SymbolRef) {
1107       SR = static_cast<const MCSymbolRefExpr *>(ExprOffset);
1108       const MCSymbolRefExpr *HiExpr = MCSymbolRefExpr::Create(
1109           SR->getSymbol().getName(), MCSymbolRefExpr::VK_Mips_ABS_HI,
1110           getContext());
1111       TempInst.addOperand(MCOperand::CreateExpr(HiExpr));
1112     } else {
1113       const MCExpr *HiExpr = evaluateRelocExpr(ExprOffset, "hi");
1114       TempInst.addOperand(MCOperand::CreateExpr(HiExpr));
1115     }
1116   }
1117   // Add the instruction to the list.
1118   Instructions.push_back(TempInst);
1119   // Prepare TempInst for next instruction.
1120   TempInst.clear();
1121   // Add temp register to base.
1122   TempInst.setOpcode(Mips::ADDu);
1123   TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
1124   TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
1125   TempInst.addOperand(MCOperand::CreateReg(BaseRegNum));
1126   Instructions.push_back(TempInst);
1127   TempInst.clear();
1128   // And finally, create original instruction with low part
1129   // of offset and new base.
1130   TempInst.setOpcode(Inst.getOpcode());
1131   TempInst.addOperand(MCOperand::CreateReg(RegOpNum));
1132   TempInst.addOperand(MCOperand::CreateReg(TmpRegNum));
1133   if (isImmOpnd)
1134     TempInst.addOperand(MCOperand::CreateImm(LoOffset));
1135   else {
1136     if (ExprOffset->getKind() == MCExpr::SymbolRef) {
1137       const MCSymbolRefExpr *LoExpr = MCSymbolRefExpr::Create(
1138           SR->getSymbol().getName(), MCSymbolRefExpr::VK_Mips_ABS_LO,
1139           getContext());
1140       TempInst.addOperand(MCOperand::CreateExpr(LoExpr));
1141     } else {
1142       const MCExpr *LoExpr = evaluateRelocExpr(ExprOffset, "lo");
1143       TempInst.addOperand(MCOperand::CreateExpr(LoExpr));
1144     }
1145   }
1146   Instructions.push_back(TempInst);
1147   TempInst.clear();
1148 }
1149
1150 bool MipsAsmParser::MatchAndEmitInstruction(
1151     SMLoc IDLoc, unsigned &Opcode,
1152     SmallVectorImpl<MCParsedAsmOperand *> &Operands, MCStreamer &Out,
1153     unsigned &ErrorInfo, bool MatchingInlineAsm) {
1154   MCInst Inst;
1155   SmallVector<MCInst, 8> Instructions;
1156   unsigned MatchResult =
1157       MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
1158
1159   switch (MatchResult) {
1160   default:
1161     break;
1162   case Match_Success: {
1163     if (processInstruction(Inst, IDLoc, Instructions))
1164       return true;
1165     for (unsigned i = 0; i < Instructions.size(); i++)
1166       Out.EmitInstruction(Instructions[i], STI);
1167     return false;
1168   }
1169   case Match_MissingFeature:
1170     Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1171     return true;
1172   case Match_InvalidOperand: {
1173     SMLoc ErrorLoc = IDLoc;
1174     if (ErrorInfo != ~0U) {
1175       if (ErrorInfo >= Operands.size())
1176         return Error(IDLoc, "too few operands for instruction");
1177
1178       ErrorLoc = ((MipsOperand *)Operands[ErrorInfo])->getStartLoc();
1179       if (ErrorLoc == SMLoc())
1180         ErrorLoc = IDLoc;
1181     }
1182
1183     return Error(ErrorLoc, "invalid operand for instruction");
1184   }
1185   case Match_MnemonicFail:
1186     return Error(IDLoc, "invalid instruction");
1187   }
1188   return true;
1189 }
1190
1191 void MipsAsmParser::WarnIfAssemblerTemporary(int RegIndex, SMLoc Loc) {
1192   if ((RegIndex != 0) && ((int)Options.getATRegNum() == RegIndex)) {
1193     if (RegIndex == 1)
1194       Warning(Loc, "Used $at without \".set noat\"");
1195     else
1196       Warning(Loc, Twine("Used $") + Twine(RegIndex) + " with \".set at=$" +
1197                        Twine(RegIndex) + "\"");
1198   }
1199 }
1200
1201 int MipsAsmParser::matchCPURegisterName(StringRef Name) {
1202   int CC;
1203
1204   CC = StringSwitch<unsigned>(Name)
1205            .Case("zero", 0)
1206            .Case("at", 1)
1207            .Case("a0", 4)
1208            .Case("a1", 5)
1209            .Case("a2", 6)
1210            .Case("a3", 7)
1211            .Case("v0", 2)
1212            .Case("v1", 3)
1213            .Case("s0", 16)
1214            .Case("s1", 17)
1215            .Case("s2", 18)
1216            .Case("s3", 19)
1217            .Case("s4", 20)
1218            .Case("s5", 21)
1219            .Case("s6", 22)
1220            .Case("s7", 23)
1221            .Case("k0", 26)
1222            .Case("k1", 27)
1223            .Case("gp", 28)
1224            .Case("sp", 29)
1225            .Case("fp", 30)
1226            .Case("s8", 30)
1227            .Case("ra", 31)
1228            .Case("t0", 8)
1229            .Case("t1", 9)
1230            .Case("t2", 10)
1231            .Case("t3", 11)
1232            .Case("t4", 12)
1233            .Case("t5", 13)
1234            .Case("t6", 14)
1235            .Case("t7", 15)
1236            .Case("t8", 24)
1237            .Case("t9", 25)
1238            .Default(-1);
1239
1240   if (isN32() || isN64()) {
1241     // Although SGI documentation just cuts out t0-t3 for n32/n64,
1242     // GNU pushes the values of t0-t3 to override the o32/o64 values for t4-t7
1243     // We are supporting both cases, so for t0-t3 we'll just push them to t4-t7.
1244     if (8 <= CC && CC <= 11)
1245       CC += 4;
1246
1247     if (CC == -1)
1248       CC = StringSwitch<unsigned>(Name)
1249                .Case("a4", 8)
1250                .Case("a5", 9)
1251                .Case("a6", 10)
1252                .Case("a7", 11)
1253                .Case("kt0", 26)
1254                .Case("kt1", 27)
1255                .Default(-1);
1256   }
1257
1258   return CC;
1259 }
1260
1261 int MipsAsmParser::matchFPURegisterName(StringRef Name) {
1262
1263   if (Name[0] == 'f') {
1264     StringRef NumString = Name.substr(1);
1265     unsigned IntVal;
1266     if (NumString.getAsInteger(10, IntVal))
1267       return -1;     // This is not an integer.
1268     if (IntVal > 31) // Maximum index for fpu register.
1269       return -1;
1270     return IntVal;
1271   }
1272   return -1;
1273 }
1274
1275 int MipsAsmParser::matchFCCRegisterName(StringRef Name) {
1276
1277   if (Name.startswith("fcc")) {
1278     StringRef NumString = Name.substr(3);
1279     unsigned IntVal;
1280     if (NumString.getAsInteger(10, IntVal))
1281       return -1;    // This is not an integer.
1282     if (IntVal > 7) // There are only 8 fcc registers.
1283       return -1;
1284     return IntVal;
1285   }
1286   return -1;
1287 }
1288
1289 int MipsAsmParser::matchACRegisterName(StringRef Name) {
1290
1291   if (Name.startswith("ac")) {
1292     StringRef NumString = Name.substr(2);
1293     unsigned IntVal;
1294     if (NumString.getAsInteger(10, IntVal))
1295       return -1;    // This is not an integer.
1296     if (IntVal > 3) // There are only 3 acc registers.
1297       return -1;
1298     return IntVal;
1299   }
1300   return -1;
1301 }
1302
1303 int MipsAsmParser::matchMSA128RegisterName(StringRef Name) {
1304   unsigned IntVal;
1305
1306   if (Name.front() != 'w' || Name.drop_front(1).getAsInteger(10, IntVal))
1307     return -1;
1308
1309   if (IntVal > 31)
1310     return -1;
1311
1312   return IntVal;
1313 }
1314
1315 int MipsAsmParser::matchMSA128CtrlRegisterName(StringRef Name) {
1316   int CC;
1317
1318   CC = StringSwitch<unsigned>(Name)
1319            .Case("msair", 0)
1320            .Case("msacsr", 1)
1321            .Case("msaaccess", 2)
1322            .Case("msasave", 3)
1323            .Case("msamodify", 4)
1324            .Case("msarequest", 5)
1325            .Case("msamap", 6)
1326            .Case("msaunmap", 7)
1327            .Default(-1);
1328
1329   return CC;
1330 }
1331
1332 bool MipsAssemblerOptions::setATReg(unsigned Reg) {
1333   if (Reg > 31)
1334     return false;
1335
1336   aTReg = Reg;
1337   return true;
1338 }
1339
1340 int MipsAsmParser::getATReg() {
1341   int AT = Options.getATRegNum();
1342   if (AT == 0)
1343     TokError("Pseudo instruction requires $at, which is not available");
1344   return AT;
1345 }
1346
1347 unsigned MipsAsmParser::getReg(int RC, int RegNo) {
1348   return *(getContext().getRegisterInfo()->getRegClass(RC).begin() + RegNo);
1349 }
1350
1351 unsigned MipsAsmParser::getGPR(int RegNo) {
1352   return getReg(isGP64() ? Mips::GPR64RegClassID : Mips::GPR32RegClassID,
1353                 RegNo);
1354 }
1355
1356 int MipsAsmParser::matchRegisterByNumber(unsigned RegNum, unsigned RegClass) {
1357   if (RegNum >
1358       getContext().getRegisterInfo()->getRegClass(RegClass).getNumRegs() - 1)
1359     return -1;
1360
1361   return getReg(RegClass, RegNum);
1362 }
1363
1364 bool
1365 MipsAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand *> &Operands,
1366                             StringRef Mnemonic) {
1367   DEBUG(dbgs() << "ParseOperand\n");
1368
1369   // Check if the current operand has a custom associated parser, if so, try to
1370   // custom parse the operand, or fallback to the general approach.
1371   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1372   if (ResTy == MatchOperand_Success)
1373     return false;
1374   // If there wasn't a custom match, try the generic matcher below. Otherwise,
1375   // there was a match, but an error occurred, in which case, just return that
1376   // the operand parsing failed.
1377   if (ResTy == MatchOperand_ParseFail)
1378     return true;
1379
1380   DEBUG(dbgs() << ".. Generic Parser\n");
1381
1382   switch (getLexer().getKind()) {
1383   default:
1384     Error(Parser.getTok().getLoc(), "unexpected token in operand");
1385     return true;
1386   case AsmToken::Dollar: {
1387     // Parse the register.
1388     SMLoc S = Parser.getTok().getLoc();
1389
1390     // Almost all registers have been parsed by custom parsers. There is only
1391     // one exception to this. $zero (and it's alias $0) will reach this point
1392     // for div, divu, and similar instructions because it is not an operand
1393     // to the instruction definition but an explicit register. Special case
1394     // this situation for now.
1395     if (ParseAnyRegister(Operands) != MatchOperand_NoMatch)
1396       return false;
1397
1398     // Maybe it is a symbol reference.
1399     StringRef Identifier;
1400     if (Parser.parseIdentifier(Identifier))
1401       return true;
1402
1403     SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1404     MCSymbol *Sym = getContext().GetOrCreateSymbol("$" + Identifier);
1405     // Otherwise create a symbol reference.
1406     const MCExpr *Res =
1407         MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
1408
1409     Operands.push_back(MipsOperand::CreateImm(Res, S, E, *this));
1410     return false;
1411   }
1412   // Else drop to expression parsing.
1413   case AsmToken::LParen:
1414   case AsmToken::Minus:
1415   case AsmToken::Plus:
1416   case AsmToken::Integer:
1417   case AsmToken::String: {
1418     DEBUG(dbgs() << ".. generic integer\n");
1419     OperandMatchResultTy ResTy = ParseImm(Operands);
1420     return ResTy != MatchOperand_Success;
1421   }
1422   case AsmToken::Percent: {
1423     // It is a symbol reference or constant expression.
1424     const MCExpr *IdVal;
1425     SMLoc S = Parser.getTok().getLoc(); // Start location of the operand.
1426     if (parseRelocOperand(IdVal))
1427       return true;
1428
1429     SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1430
1431     Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
1432     return false;
1433   } // case AsmToken::Percent
1434   } // switch(getLexer().getKind())
1435   return true;
1436 }
1437
1438 const MCExpr *MipsAsmParser::evaluateRelocExpr(const MCExpr *Expr,
1439                                                StringRef RelocStr) {
1440   const MCExpr *Res;
1441   // Check the type of the expression.
1442   if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Expr)) {
1443     // It's a constant, evaluate reloc value.
1444     int16_t Val;
1445     switch (getVariantKind(RelocStr)) {
1446     case MCSymbolRefExpr::VK_Mips_ABS_LO:
1447       // Get the 1st 16-bits.
1448       Val = MCE->getValue() & 0xffff;
1449       break;
1450     case MCSymbolRefExpr::VK_Mips_ABS_HI:
1451       // Get the 2nd 16-bits. Also add 1 if bit 15 is 1, to compensate for low
1452       // 16 bits being negative.
1453       Val = ((MCE->getValue() + 0x8000) >> 16) & 0xffff;
1454       break;
1455     case MCSymbolRefExpr::VK_Mips_HIGHER:
1456       // Get the 3rd 16-bits.
1457       Val = ((MCE->getValue() + 0x80008000LL) >> 32) & 0xffff;
1458       break;
1459     case MCSymbolRefExpr::VK_Mips_HIGHEST:
1460       // Get the 4th 16-bits.
1461       Val = ((MCE->getValue() + 0x800080008000LL) >> 48) & 0xffff;
1462       break;
1463     default:
1464       report_fatal_error("Unsupported reloc value!");
1465     }
1466     return MCConstantExpr::Create(Val, getContext());
1467   }
1468
1469   if (const MCSymbolRefExpr *MSRE = dyn_cast<MCSymbolRefExpr>(Expr)) {
1470     // It's a symbol, create a symbolic expression from the symbol.
1471     StringRef Symbol = MSRE->getSymbol().getName();
1472     MCSymbolRefExpr::VariantKind VK = getVariantKind(RelocStr);
1473     Res = MCSymbolRefExpr::Create(Symbol, VK, getContext());
1474     return Res;
1475   }
1476
1477   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
1478     MCSymbolRefExpr::VariantKind VK = getVariantKind(RelocStr);
1479
1480     // Try to create target expression.
1481     if (MipsMCExpr::isSupportedBinaryExpr(VK, BE))
1482       return MipsMCExpr::Create(VK, Expr, getContext());
1483
1484     const MCExpr *LExp = evaluateRelocExpr(BE->getLHS(), RelocStr);
1485     const MCExpr *RExp = evaluateRelocExpr(BE->getRHS(), RelocStr);
1486     Res = MCBinaryExpr::Create(BE->getOpcode(), LExp, RExp, getContext());
1487     return Res;
1488   }
1489
1490   if (const MCUnaryExpr *UN = dyn_cast<MCUnaryExpr>(Expr)) {
1491     const MCExpr *UnExp = evaluateRelocExpr(UN->getSubExpr(), RelocStr);
1492     Res = MCUnaryExpr::Create(UN->getOpcode(), UnExp, getContext());
1493     return Res;
1494   }
1495   // Just return the original expression.
1496   return Expr;
1497 }
1498
1499 bool MipsAsmParser::isEvaluated(const MCExpr *Expr) {
1500
1501   switch (Expr->getKind()) {
1502   case MCExpr::Constant:
1503     return true;
1504   case MCExpr::SymbolRef:
1505     return (cast<MCSymbolRefExpr>(Expr)->getKind() != MCSymbolRefExpr::VK_None);
1506   case MCExpr::Binary:
1507     if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
1508       if (!isEvaluated(BE->getLHS()))
1509         return false;
1510       return isEvaluated(BE->getRHS());
1511     }
1512   case MCExpr::Unary:
1513     return isEvaluated(cast<MCUnaryExpr>(Expr)->getSubExpr());
1514   case MCExpr::Target:
1515     return true;
1516   }
1517   return false;
1518 }
1519
1520 bool MipsAsmParser::parseRelocOperand(const MCExpr *&Res) {
1521   Parser.Lex();                          // Eat the % token.
1522   const AsmToken &Tok = Parser.getTok(); // Get next token, operation.
1523   if (Tok.isNot(AsmToken::Identifier))
1524     return true;
1525
1526   std::string Str = Tok.getIdentifier().str();
1527
1528   Parser.Lex(); // Eat the identifier.
1529   // Now make an expression from the rest of the operand.
1530   const MCExpr *IdVal;
1531   SMLoc EndLoc;
1532
1533   if (getLexer().getKind() == AsmToken::LParen) {
1534     while (1) {
1535       Parser.Lex(); // Eat the '(' token.
1536       if (getLexer().getKind() == AsmToken::Percent) {
1537         Parser.Lex(); // Eat the % token.
1538         const AsmToken &nextTok = Parser.getTok();
1539         if (nextTok.isNot(AsmToken::Identifier))
1540           return true;
1541         Str += "(%";
1542         Str += nextTok.getIdentifier();
1543         Parser.Lex(); // Eat the identifier.
1544         if (getLexer().getKind() != AsmToken::LParen)
1545           return true;
1546       } else
1547         break;
1548     }
1549     if (getParser().parseParenExpression(IdVal, EndLoc))
1550       return true;
1551
1552     while (getLexer().getKind() == AsmToken::RParen)
1553       Parser.Lex(); // Eat the ')' token.
1554
1555   } else
1556     return true; // Parenthesis must follow the relocation operand.
1557
1558   Res = evaluateRelocExpr(IdVal, Str);
1559   return false;
1560 }
1561
1562 bool MipsAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc,
1563                                   SMLoc &EndLoc) {
1564   SmallVector<MCParsedAsmOperand *, 1> Operands;
1565   OperandMatchResultTy ResTy = ParseAnyRegister(Operands);
1566   if (ResTy == MatchOperand_Success) {
1567     assert(Operands.size() == 1);
1568     MipsOperand &Operand = *static_cast<MipsOperand *>(Operands.front());
1569     StartLoc = Operand.getStartLoc();
1570     EndLoc = Operand.getEndLoc();
1571
1572     // AFAIK, we only support numeric registers and named GPR's in CFI
1573     // directives.
1574     // Don't worry about eating tokens before failing. Using an unrecognised
1575     // register is a parse error.
1576     if (Operand.isGPRAsmReg()) {
1577       // Resolve to GPR32 or GPR64 appropriately.
1578       RegNo = isGP64() ? Operand.getGPR64Reg() : Operand.getGPR32Reg();
1579     }
1580
1581     delete &Operand;
1582
1583     return (RegNo == (unsigned)-1);
1584   }
1585
1586   assert(Operands.size() == 0);
1587   return (RegNo == (unsigned)-1);
1588 }
1589
1590 bool MipsAsmParser::parseMemOffset(const MCExpr *&Res, bool isParenExpr) {
1591   SMLoc S;
1592   bool Result = true;
1593
1594   while (getLexer().getKind() == AsmToken::LParen)
1595     Parser.Lex();
1596
1597   switch (getLexer().getKind()) {
1598   default:
1599     return true;
1600   case AsmToken::Identifier:
1601   case AsmToken::LParen:
1602   case AsmToken::Integer:
1603   case AsmToken::Minus:
1604   case AsmToken::Plus:
1605     if (isParenExpr)
1606       Result = getParser().parseParenExpression(Res, S);
1607     else
1608       Result = (getParser().parseExpression(Res));
1609     while (getLexer().getKind() == AsmToken::RParen)
1610       Parser.Lex();
1611     break;
1612   case AsmToken::Percent:
1613     Result = parseRelocOperand(Res);
1614   }
1615   return Result;
1616 }
1617
1618 MipsAsmParser::OperandMatchResultTy MipsAsmParser::parseMemOperand(
1619     SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1620   DEBUG(dbgs() << "parseMemOperand\n");
1621   const MCExpr *IdVal = nullptr;
1622   SMLoc S;
1623   bool isParenExpr = false;
1624   MipsAsmParser::OperandMatchResultTy Res = MatchOperand_NoMatch;
1625   // First operand is the offset.
1626   S = Parser.getTok().getLoc();
1627
1628   if (getLexer().getKind() == AsmToken::LParen) {
1629     Parser.Lex();
1630     isParenExpr = true;
1631   }
1632
1633   if (getLexer().getKind() != AsmToken::Dollar) {
1634     if (parseMemOffset(IdVal, isParenExpr))
1635       return MatchOperand_ParseFail;
1636
1637     const AsmToken &Tok = Parser.getTok(); // Get the next token.
1638     if (Tok.isNot(AsmToken::LParen)) {
1639       MipsOperand *Mnemonic = static_cast<MipsOperand *>(Operands[0]);
1640       if (Mnemonic->getToken() == "la") {
1641         SMLoc E =
1642             SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1643         Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
1644         return MatchOperand_Success;
1645       }
1646       if (Tok.is(AsmToken::EndOfStatement)) {
1647         SMLoc E =
1648             SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1649
1650         // Zero register assumed, add a memory operand with ZERO as its base.
1651         // "Base" will be managed by k_Memory.
1652         MipsOperand *Base = MipsOperand::CreateGPRReg(
1653             0, getContext().getRegisterInfo(), S, E, *this);
1654         Operands.push_back(MipsOperand::CreateMem(Base, IdVal, S, E, *this));
1655         return MatchOperand_Success;
1656       }
1657       Error(Parser.getTok().getLoc(), "'(' expected");
1658       return MatchOperand_ParseFail;
1659     }
1660
1661     Parser.Lex(); // Eat the '(' token.
1662   }
1663
1664   Res = ParseAnyRegister(Operands);
1665   if (Res != MatchOperand_Success)
1666     return Res;
1667
1668   if (Parser.getTok().isNot(AsmToken::RParen)) {
1669     Error(Parser.getTok().getLoc(), "')' expected");
1670     return MatchOperand_ParseFail;
1671   }
1672
1673   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1674
1675   Parser.Lex(); // Eat the ')' token.
1676
1677   if (!IdVal)
1678     IdVal = MCConstantExpr::Create(0, getContext());
1679
1680   // Replace the register operand with the memory operand.
1681   MipsOperand *op = static_cast<MipsOperand *>(Operands.back());
1682   // Remove the register from the operands.
1683   // "op" will be managed by k_Memory.
1684   Operands.pop_back();
1685   // Add the memory operand.
1686   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(IdVal)) {
1687     int64_t Imm;
1688     if (IdVal->EvaluateAsAbsolute(Imm))
1689       IdVal = MCConstantExpr::Create(Imm, getContext());
1690     else if (BE->getLHS()->getKind() != MCExpr::SymbolRef)
1691       IdVal = MCBinaryExpr::Create(BE->getOpcode(), BE->getRHS(), BE->getLHS(),
1692                                    getContext());
1693   }
1694
1695   Operands.push_back(MipsOperand::CreateMem(op, IdVal, S, E, *this));
1696   return MatchOperand_Success;
1697 }
1698
1699 bool MipsAsmParser::searchSymbolAlias(
1700     SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1701
1702   MCSymbol *Sym = getContext().LookupSymbol(Parser.getTok().getIdentifier());
1703   if (Sym) {
1704     SMLoc S = Parser.getTok().getLoc();
1705     const MCExpr *Expr;
1706     if (Sym->isVariable())
1707       Expr = Sym->getVariableValue();
1708     else
1709       return false;
1710     if (Expr->getKind() == MCExpr::SymbolRef) {
1711       const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr);
1712       const StringRef DefSymbol = Ref->getSymbol().getName();
1713       if (DefSymbol.startswith("$")) {
1714         OperandMatchResultTy ResTy =
1715             MatchAnyRegisterNameWithoutDollar(Operands, DefSymbol.substr(1), S);
1716         if (ResTy == MatchOperand_Success) {
1717           Parser.Lex();
1718           return true;
1719         } else if (ResTy == MatchOperand_ParseFail)
1720           llvm_unreachable("Should never ParseFail");
1721         return false;
1722       }
1723     } else if (Expr->getKind() == MCExpr::Constant) {
1724       Parser.Lex();
1725       const MCConstantExpr *Const = static_cast<const MCConstantExpr *>(Expr);
1726       MipsOperand *op =
1727           MipsOperand::CreateImm(Const, S, Parser.getTok().getLoc(), *this);
1728       Operands.push_back(op);
1729       return true;
1730     }
1731   }
1732   return false;
1733 }
1734
1735 MipsAsmParser::OperandMatchResultTy
1736 MipsAsmParser::MatchAnyRegisterNameWithoutDollar(
1737     SmallVectorImpl<MCParsedAsmOperand *> &Operands, StringRef Identifier,
1738     SMLoc S) {
1739   int Index = matchCPURegisterName(Identifier);
1740   if (Index != -1) {
1741     Operands.push_back(MipsOperand::CreateGPRReg(
1742         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
1743     return MatchOperand_Success;
1744   }
1745
1746   Index = matchFPURegisterName(Identifier);
1747   if (Index != -1) {
1748     Operands.push_back(MipsOperand::CreateFGRReg(
1749         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
1750     return MatchOperand_Success;
1751   }
1752
1753   Index = matchFCCRegisterName(Identifier);
1754   if (Index != -1) {
1755     Operands.push_back(MipsOperand::CreateFCCReg(
1756         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
1757     return MatchOperand_Success;
1758   }
1759
1760   Index = matchACRegisterName(Identifier);
1761   if (Index != -1) {
1762     Operands.push_back(MipsOperand::CreateACCReg(
1763         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
1764     return MatchOperand_Success;
1765   }
1766
1767   Index = matchMSA128RegisterName(Identifier);
1768   if (Index != -1) {
1769     Operands.push_back(MipsOperand::CreateMSA128Reg(
1770         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
1771     return MatchOperand_Success;
1772   }
1773
1774   Index = matchMSA128CtrlRegisterName(Identifier);
1775   if (Index != -1) {
1776     Operands.push_back(MipsOperand::CreateMSACtrlReg(
1777         Index, getContext().getRegisterInfo(), S, getLexer().getLoc(), *this));
1778     return MatchOperand_Success;
1779   }
1780
1781   return MatchOperand_NoMatch;
1782 }
1783
1784 MipsAsmParser::OperandMatchResultTy
1785 MipsAsmParser::MatchAnyRegisterWithoutDollar(
1786     SmallVectorImpl<MCParsedAsmOperand *> &Operands, SMLoc S) {
1787   auto Token = Parser.getLexer().peekTok(false);
1788
1789   if (Token.is(AsmToken::Identifier)) {
1790     DEBUG(dbgs() << ".. identifier\n");
1791     StringRef Identifier = Token.getIdentifier();
1792     OperandMatchResultTy ResTy =
1793         MatchAnyRegisterNameWithoutDollar(Operands, Identifier, S);
1794     return ResTy;
1795   } else if (Token.is(AsmToken::Integer)) {
1796     DEBUG(dbgs() << ".. integer\n");
1797     Operands.push_back(MipsOperand::CreateNumericReg(
1798         Token.getIntVal(), getContext().getRegisterInfo(), S, Token.getLoc(),
1799         *this));
1800     return MatchOperand_Success;
1801   }
1802
1803   DEBUG(dbgs() << Parser.getTok().getKind() << "\n");
1804
1805   return MatchOperand_NoMatch;
1806 }
1807
1808 MipsAsmParser::OperandMatchResultTy MipsAsmParser::ParseAnyRegister(
1809     SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1810   DEBUG(dbgs() << "ParseAnyRegister\n");
1811
1812   auto Token = Parser.getTok();
1813
1814   SMLoc S = Token.getLoc();
1815
1816   if (Token.isNot(AsmToken::Dollar)) {
1817     DEBUG(dbgs() << ".. !$ -> try sym aliasing\n");
1818     if (Token.is(AsmToken::Identifier)) {
1819       if (searchSymbolAlias(Operands))
1820         return MatchOperand_Success;
1821     }
1822     DEBUG(dbgs() << ".. !symalias -> NoMatch\n");
1823     return MatchOperand_NoMatch;
1824   }
1825   DEBUG(dbgs() << ".. $\n");
1826
1827   OperandMatchResultTy ResTy = MatchAnyRegisterWithoutDollar(Operands, S);
1828   if (ResTy == MatchOperand_Success) {
1829     Parser.Lex(); // $
1830     Parser.Lex(); // identifier
1831   }
1832   return ResTy;
1833 }
1834
1835 MipsAsmParser::OperandMatchResultTy
1836 MipsAsmParser::ParseImm(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1837   switch (getLexer().getKind()) {
1838   default:
1839     return MatchOperand_NoMatch;
1840   case AsmToken::LParen:
1841   case AsmToken::Minus:
1842   case AsmToken::Plus:
1843   case AsmToken::Integer:
1844   case AsmToken::String:
1845     break;
1846   }
1847
1848   const MCExpr *IdVal;
1849   SMLoc S = Parser.getTok().getLoc();
1850   if (getParser().parseExpression(IdVal))
1851     return MatchOperand_ParseFail;
1852
1853   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1854   Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this));
1855   return MatchOperand_Success;
1856 }
1857
1858 MipsAsmParser::OperandMatchResultTy MipsAsmParser::ParseJumpTarget(
1859     SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1860   DEBUG(dbgs() << "ParseJumpTarget\n");
1861
1862   SMLoc S = getLexer().getLoc();
1863
1864   // Integers and expressions are acceptable
1865   OperandMatchResultTy ResTy = ParseImm(Operands);
1866   if (ResTy != MatchOperand_NoMatch)
1867     return ResTy;
1868
1869   // Registers are a valid target and have priority over symbols.
1870   ResTy = ParseAnyRegister(Operands);
1871   if (ResTy != MatchOperand_NoMatch)
1872     return ResTy;
1873
1874   const MCExpr *Expr = nullptr;
1875   if (Parser.parseExpression(Expr)) {
1876     // We have no way of knowing if a symbol was consumed so we must ParseFail
1877     return MatchOperand_ParseFail;
1878   }
1879   Operands.push_back(
1880       MipsOperand::CreateImm(Expr, S, getLexer().getLoc(), *this));
1881   return MatchOperand_Success;
1882 }
1883
1884 MipsAsmParser::OperandMatchResultTy
1885 MipsAsmParser::parseInvNum(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1886   const MCExpr *IdVal;
1887   // If the first token is '$' we may have register operand.
1888   if (Parser.getTok().is(AsmToken::Dollar))
1889     return MatchOperand_NoMatch;
1890   SMLoc S = Parser.getTok().getLoc();
1891   if (getParser().parseExpression(IdVal))
1892     return MatchOperand_ParseFail;
1893   const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(IdVal);
1894   assert(MCE && "Unexpected MCExpr type.");
1895   int64_t Val = MCE->getValue();
1896   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1897   Operands.push_back(MipsOperand::CreateImm(
1898       MCConstantExpr::Create(0 - Val, getContext()), S, E, *this));
1899   return MatchOperand_Success;
1900 }
1901
1902 MipsAsmParser::OperandMatchResultTy
1903 MipsAsmParser::ParseLSAImm(SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1904   switch (getLexer().getKind()) {
1905   default:
1906     return MatchOperand_NoMatch;
1907   case AsmToken::LParen:
1908   case AsmToken::Plus:
1909   case AsmToken::Minus:
1910   case AsmToken::Integer:
1911     break;
1912   }
1913
1914   const MCExpr *Expr;
1915   SMLoc S = Parser.getTok().getLoc();
1916
1917   if (getParser().parseExpression(Expr))
1918     return MatchOperand_ParseFail;
1919
1920   int64_t Val;
1921   if (!Expr->EvaluateAsAbsolute(Val)) {
1922     Error(S, "expected immediate value");
1923     return MatchOperand_ParseFail;
1924   }
1925
1926   // The LSA instruction allows a 2-bit unsigned immediate. For this reason
1927   // and because the CPU always adds one to the immediate field, the allowed
1928   // range becomes 1..4. We'll only check the range here and will deal
1929   // with the addition/subtraction when actually decoding/encoding
1930   // the instruction.
1931   if (Val < 1 || Val > 4) {
1932     Error(S, "immediate not in range (1..4)");
1933     return MatchOperand_ParseFail;
1934   }
1935
1936   Operands.push_back(
1937       MipsOperand::CreateImm(Expr, S, Parser.getTok().getLoc(), *this));
1938   return MatchOperand_Success;
1939 }
1940
1941 MCSymbolRefExpr::VariantKind MipsAsmParser::getVariantKind(StringRef Symbol) {
1942
1943   MCSymbolRefExpr::VariantKind VK =
1944       StringSwitch<MCSymbolRefExpr::VariantKind>(Symbol)
1945           .Case("hi", MCSymbolRefExpr::VK_Mips_ABS_HI)
1946           .Case("lo", MCSymbolRefExpr::VK_Mips_ABS_LO)
1947           .Case("gp_rel", MCSymbolRefExpr::VK_Mips_GPREL)
1948           .Case("call16", MCSymbolRefExpr::VK_Mips_GOT_CALL)
1949           .Case("got", MCSymbolRefExpr::VK_Mips_GOT)
1950           .Case("tlsgd", MCSymbolRefExpr::VK_Mips_TLSGD)
1951           .Case("tlsldm", MCSymbolRefExpr::VK_Mips_TLSLDM)
1952           .Case("dtprel_hi", MCSymbolRefExpr::VK_Mips_DTPREL_HI)
1953           .Case("dtprel_lo", MCSymbolRefExpr::VK_Mips_DTPREL_LO)
1954           .Case("gottprel", MCSymbolRefExpr::VK_Mips_GOTTPREL)
1955           .Case("tprel_hi", MCSymbolRefExpr::VK_Mips_TPREL_HI)
1956           .Case("tprel_lo", MCSymbolRefExpr::VK_Mips_TPREL_LO)
1957           .Case("got_disp", MCSymbolRefExpr::VK_Mips_GOT_DISP)
1958           .Case("got_page", MCSymbolRefExpr::VK_Mips_GOT_PAGE)
1959           .Case("got_ofst", MCSymbolRefExpr::VK_Mips_GOT_OFST)
1960           .Case("hi(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_HI)
1961           .Case("lo(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_LO)
1962           .Case("got_hi", MCSymbolRefExpr::VK_Mips_GOT_HI16)
1963           .Case("got_lo", MCSymbolRefExpr::VK_Mips_GOT_LO16)
1964           .Case("call_hi", MCSymbolRefExpr::VK_Mips_CALL_HI16)
1965           .Case("call_lo", MCSymbolRefExpr::VK_Mips_CALL_LO16)
1966           .Case("higher", MCSymbolRefExpr::VK_Mips_HIGHER)
1967           .Case("highest", MCSymbolRefExpr::VK_Mips_HIGHEST)
1968           .Default(MCSymbolRefExpr::VK_None);
1969
1970   assert(VK != MCSymbolRefExpr::VK_None);
1971
1972   return VK;
1973 }
1974
1975 /// Sometimes (i.e. load/stores) the operand may be followed immediately by
1976 /// either this.
1977 /// ::= '(', register, ')'
1978 /// handle it before we iterate so we don't get tripped up by the lack of
1979 /// a comma.
1980 bool MipsAsmParser::ParseParenSuffix(
1981     StringRef Name, SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
1982   if (getLexer().is(AsmToken::LParen)) {
1983     Operands.push_back(
1984         MipsOperand::CreateToken("(", getLexer().getLoc(), *this));
1985     Parser.Lex();
1986     if (ParseOperand(Operands, Name)) {
1987       SMLoc Loc = getLexer().getLoc();
1988       Parser.eatToEndOfStatement();
1989       return Error(Loc, "unexpected token in argument list");
1990     }
1991     if (Parser.getTok().isNot(AsmToken::RParen)) {
1992       SMLoc Loc = getLexer().getLoc();
1993       Parser.eatToEndOfStatement();
1994       return Error(Loc, "unexpected token, expected ')'");
1995     }
1996     Operands.push_back(
1997         MipsOperand::CreateToken(")", getLexer().getLoc(), *this));
1998     Parser.Lex();
1999   }
2000   return false;
2001 }
2002
2003 /// Sometimes (i.e. in MSA) the operand may be followed immediately by
2004 /// either one of these.
2005 /// ::= '[', register, ']'
2006 /// ::= '[', integer, ']'
2007 /// handle it before we iterate so we don't get tripped up by the lack of
2008 /// a comma.
2009 bool MipsAsmParser::ParseBracketSuffix(
2010     StringRef Name, SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
2011   if (getLexer().is(AsmToken::LBrac)) {
2012     Operands.push_back(
2013         MipsOperand::CreateToken("[", getLexer().getLoc(), *this));
2014     Parser.Lex();
2015     if (ParseOperand(Operands, Name)) {
2016       SMLoc Loc = getLexer().getLoc();
2017       Parser.eatToEndOfStatement();
2018       return Error(Loc, "unexpected token in argument list");
2019     }
2020     if (Parser.getTok().isNot(AsmToken::RBrac)) {
2021       SMLoc Loc = getLexer().getLoc();
2022       Parser.eatToEndOfStatement();
2023       return Error(Loc, "unexpected token, expected ']'");
2024     }
2025     Operands.push_back(
2026         MipsOperand::CreateToken("]", getLexer().getLoc(), *this));
2027     Parser.Lex();
2028   }
2029   return false;
2030 }
2031
2032 bool MipsAsmParser::ParseInstruction(
2033     ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
2034     SmallVectorImpl<MCParsedAsmOperand *> &Operands) {
2035   DEBUG(dbgs() << "ParseInstruction\n");
2036   // Check if we have valid mnemonic
2037   if (!mnemonicIsValid(Name, 0)) {
2038     Parser.eatToEndOfStatement();
2039     return Error(NameLoc, "Unknown instruction");
2040   }
2041   // First operand in MCInst is instruction mnemonic.
2042   Operands.push_back(MipsOperand::CreateToken(Name, NameLoc, *this));
2043
2044   // Read the remaining operands.
2045   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2046     // Read the first operand.
2047     if (ParseOperand(Operands, Name)) {
2048       SMLoc Loc = getLexer().getLoc();
2049       Parser.eatToEndOfStatement();
2050       return Error(Loc, "unexpected token in argument list");
2051     }
2052     if (getLexer().is(AsmToken::LBrac) && ParseBracketSuffix(Name, Operands))
2053       return true;
2054     // AFAIK, parenthesis suffixes are never on the first operand
2055
2056     while (getLexer().is(AsmToken::Comma)) {
2057       Parser.Lex(); // Eat the comma.
2058       // Parse and remember the operand.
2059       if (ParseOperand(Operands, Name)) {
2060         SMLoc Loc = getLexer().getLoc();
2061         Parser.eatToEndOfStatement();
2062         return Error(Loc, "unexpected token in argument list");
2063       }
2064       // Parse bracket and parenthesis suffixes before we iterate
2065       if (getLexer().is(AsmToken::LBrac)) {
2066         if (ParseBracketSuffix(Name, Operands))
2067           return true;
2068       } else if (getLexer().is(AsmToken::LParen) &&
2069                  ParseParenSuffix(Name, Operands))
2070         return true;
2071     }
2072   }
2073   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2074     SMLoc Loc = getLexer().getLoc();
2075     Parser.eatToEndOfStatement();
2076     return Error(Loc, "unexpected token in argument list");
2077   }
2078   Parser.Lex(); // Consume the EndOfStatement.
2079   return false;
2080 }
2081
2082 bool MipsAsmParser::reportParseError(StringRef ErrorMsg) {
2083   SMLoc Loc = getLexer().getLoc();
2084   Parser.eatToEndOfStatement();
2085   return Error(Loc, ErrorMsg);
2086 }
2087
2088 bool MipsAsmParser::reportParseError(SMLoc Loc, StringRef ErrorMsg) {
2089   return Error(Loc, ErrorMsg);
2090 }
2091
2092 bool MipsAsmParser::parseSetNoAtDirective() {
2093   // Line should look like: ".set noat".
2094   // set at reg to 0.
2095   Options.setATReg(0);
2096   // eat noat
2097   Parser.Lex();
2098   // If this is not the end of the statement, report an error.
2099   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2100     reportParseError("unexpected token in statement");
2101     return false;
2102   }
2103   Parser.Lex(); // Consume the EndOfStatement.
2104   return false;
2105 }
2106
2107 bool MipsAsmParser::parseSetAtDirective() {
2108   // Line can be .set at - defaults to $1
2109   // or .set at=$reg
2110   int AtRegNo;
2111   getParser().Lex();
2112   if (getLexer().is(AsmToken::EndOfStatement)) {
2113     Options.setATReg(1);
2114     Parser.Lex(); // Consume the EndOfStatement.
2115     return false;
2116   } else if (getLexer().is(AsmToken::Equal)) {
2117     getParser().Lex(); // Eat the '='.
2118     if (getLexer().isNot(AsmToken::Dollar)) {
2119       reportParseError("unexpected token in statement");
2120       return false;
2121     }
2122     Parser.Lex(); // Eat the '$'.
2123     const AsmToken &Reg = Parser.getTok();
2124     if (Reg.is(AsmToken::Identifier)) {
2125       AtRegNo = matchCPURegisterName(Reg.getIdentifier());
2126     } else if (Reg.is(AsmToken::Integer)) {
2127       AtRegNo = Reg.getIntVal();
2128     } else {
2129       reportParseError("unexpected token in statement");
2130       return false;
2131     }
2132
2133     if (AtRegNo < 0 || AtRegNo > 31) {
2134       reportParseError("unexpected token in statement");
2135       return false;
2136     }
2137
2138     if (!Options.setATReg(AtRegNo)) {
2139       reportParseError("unexpected token in statement");
2140       return false;
2141     }
2142     getParser().Lex(); // Eat the register.
2143
2144     if (getLexer().isNot(AsmToken::EndOfStatement)) {
2145       reportParseError("unexpected token in statement");
2146       return false;
2147     }
2148     Parser.Lex(); // Consume the EndOfStatement.
2149     return false;
2150   } else {
2151     reportParseError("unexpected token in statement");
2152     return false;
2153   }
2154 }
2155
2156 bool MipsAsmParser::parseSetReorderDirective() {
2157   Parser.Lex();
2158   // If this is not the end of the statement, report an error.
2159   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2160     reportParseError("unexpected token in statement");
2161     return false;
2162   }
2163   Options.setReorder();
2164   getTargetStreamer().emitDirectiveSetReorder();
2165   Parser.Lex(); // Consume the EndOfStatement.
2166   return false;
2167 }
2168
2169 bool MipsAsmParser::parseSetNoReorderDirective() {
2170   Parser.Lex();
2171   // If this is not the end of the statement, report an error.
2172   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2173     reportParseError("unexpected token in statement");
2174     return false;
2175   }
2176   Options.setNoreorder();
2177   getTargetStreamer().emitDirectiveSetNoReorder();
2178   Parser.Lex(); // Consume the EndOfStatement.
2179   return false;
2180 }
2181
2182 bool MipsAsmParser::parseSetMacroDirective() {
2183   Parser.Lex();
2184   // If this is not the end of the statement, report an error.
2185   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2186     reportParseError("unexpected token in statement");
2187     return false;
2188   }
2189   Options.setMacro();
2190   Parser.Lex(); // Consume the EndOfStatement.
2191   return false;
2192 }
2193
2194 bool MipsAsmParser::parseSetNoMacroDirective() {
2195   Parser.Lex();
2196   // If this is not the end of the statement, report an error.
2197   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2198     reportParseError("`noreorder' must be set before `nomacro'");
2199     return false;
2200   }
2201   if (Options.isReorder()) {
2202     reportParseError("`noreorder' must be set before `nomacro'");
2203     return false;
2204   }
2205   Options.setNomacro();
2206   Parser.Lex(); // Consume the EndOfStatement.
2207   return false;
2208 }
2209
2210 bool MipsAsmParser::parseSetNoMips16Directive() {
2211   Parser.Lex();
2212   // If this is not the end of the statement, report an error.
2213   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2214     reportParseError("unexpected token in statement");
2215     return false;
2216   }
2217   // For now do nothing.
2218   Parser.Lex(); // Consume the EndOfStatement.
2219   return false;
2220 }
2221
2222 bool MipsAsmParser::parseSetAssignment() {
2223   StringRef Name;
2224   const MCExpr *Value;
2225
2226   if (Parser.parseIdentifier(Name))
2227     reportParseError("expected identifier after .set");
2228
2229   if (getLexer().isNot(AsmToken::Comma))
2230     return reportParseError("unexpected token in .set directive");
2231   Lex(); // Eat comma
2232
2233   if (Parser.parseExpression(Value))
2234     return reportParseError("expected valid expression after comma");
2235
2236   // Check if the Name already exists as a symbol.
2237   MCSymbol *Sym = getContext().LookupSymbol(Name);
2238   if (Sym)
2239     return reportParseError("symbol already defined");
2240   Sym = getContext().GetOrCreateSymbol(Name);
2241   Sym->setVariableValue(Value);
2242
2243   return false;
2244 }
2245
2246 bool MipsAsmParser::parseSetFeature(uint64_t Feature) {
2247   Parser.Lex();
2248   if (getLexer().isNot(AsmToken::EndOfStatement))
2249     return reportParseError("unexpected token in .set directive");
2250
2251   switch (Feature) {
2252   default:
2253     llvm_unreachable("Unimplemented feature");
2254   case Mips::FeatureDSP:
2255     setFeatureBits(Mips::FeatureDSP, "dsp");
2256     getTargetStreamer().emitDirectiveSetDsp();
2257     break;
2258   case Mips::FeatureMicroMips:
2259     getTargetStreamer().emitDirectiveSetMicroMips();
2260     break;
2261   case Mips::FeatureMips16:
2262     getTargetStreamer().emitDirectiveSetMips16();
2263     break;
2264   case Mips::FeatureMips32r2:
2265     setFeatureBits(Mips::FeatureMips32r2, "mips32r2");
2266     getTargetStreamer().emitDirectiveSetMips32R2();
2267     break;
2268   case Mips::FeatureMips64:
2269     setFeatureBits(Mips::FeatureMips64, "mips64");
2270     getTargetStreamer().emitDirectiveSetMips64();
2271     break;
2272   case Mips::FeatureMips64r2:
2273     setFeatureBits(Mips::FeatureMips64r2, "mips64r2");
2274     getTargetStreamer().emitDirectiveSetMips64R2();
2275     break;
2276   }
2277   return false;
2278 }
2279
2280 bool MipsAsmParser::parseRegister(unsigned &RegNum) {
2281   if (!getLexer().is(AsmToken::Dollar))
2282     return false;
2283
2284   Parser.Lex();
2285
2286   const AsmToken &Reg = Parser.getTok();
2287   if (Reg.is(AsmToken::Identifier)) {
2288     RegNum = matchCPURegisterName(Reg.getIdentifier());
2289   } else if (Reg.is(AsmToken::Integer)) {
2290     RegNum = Reg.getIntVal();
2291   } else {
2292     return false;
2293   }
2294
2295   Parser.Lex();
2296   return true;
2297 }
2298
2299 bool MipsAsmParser::eatComma(StringRef ErrorStr) {
2300   if (getLexer().isNot(AsmToken::Comma)) {
2301     SMLoc Loc = getLexer().getLoc();
2302     Parser.eatToEndOfStatement();
2303     return Error(Loc, ErrorStr);
2304   }
2305
2306   Parser.Lex(); // Eat the comma.
2307   return true;
2308 }
2309
2310 bool MipsAsmParser::parseDirectiveCPLoad(SMLoc Loc) {
2311   if (Options.isReorder())
2312     Warning(Loc, ".cpload in reorder section");
2313
2314   // FIXME: Warn if cpload is used in Mips16 mode.
2315
2316   SmallVector<MCParsedAsmOperand *, 1> Reg;
2317   OperandMatchResultTy ResTy = ParseAnyRegister(Reg);
2318   if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) {
2319     reportParseError("expected register containing function address");
2320     return false;
2321   }
2322
2323   MipsOperand *RegOpnd = static_cast<MipsOperand *>(Reg[0]);
2324   if (!RegOpnd->isGPRAsmReg()) {
2325     reportParseError(RegOpnd->getStartLoc(), "invalid register");
2326     return false;
2327   }
2328
2329   getTargetStreamer().emitDirectiveCpload(RegOpnd->getGPR32Reg());
2330   delete RegOpnd;
2331   return false;
2332 }
2333
2334 bool MipsAsmParser::parseDirectiveCPSetup() {
2335   unsigned FuncReg;
2336   unsigned Save;
2337   bool SaveIsReg = true;
2338
2339   if (!parseRegister(FuncReg))
2340     return reportParseError("expected register containing function address");
2341   FuncReg = getGPR(FuncReg);
2342
2343   if (!eatComma("expected comma parsing directive"))
2344     return true;
2345
2346   if (!parseRegister(Save)) {
2347     const AsmToken &Tok = Parser.getTok();
2348     if (Tok.is(AsmToken::Integer)) {
2349       Save = Tok.getIntVal();
2350       SaveIsReg = false;
2351       Parser.Lex();
2352     } else
2353       return reportParseError("expected save register or stack offset");
2354   } else
2355     Save = getGPR(Save);
2356
2357   if (!eatComma("expected comma parsing directive"))
2358     return true;
2359
2360   StringRef Name;
2361   if (Parser.parseIdentifier(Name))
2362     reportParseError("expected identifier");
2363   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
2364   unsigned GPReg = getGPR(matchCPURegisterName("gp"));
2365
2366   // FIXME: The code below this point should be in the TargetStreamers.
2367   // Only N32 and N64 emit anything for .cpsetup
2368   // FIXME: We should only emit something for PIC mode too.
2369   if (!isN32() && !isN64())
2370     return false;
2371
2372   MCStreamer &TS = getStreamer();
2373   MCInst Inst;
2374   // Either store the old $gp in a register or on the stack
2375   if (SaveIsReg) {
2376     // move $save, $gpreg
2377     Inst.setOpcode(Mips::DADDu);
2378     Inst.addOperand(MCOperand::CreateReg(Save));
2379     Inst.addOperand(MCOperand::CreateReg(GPReg));
2380     Inst.addOperand(MCOperand::CreateReg(getGPR(0)));
2381   } else {
2382     // sd $gpreg, offset($sp)
2383     Inst.setOpcode(Mips::SD);
2384     Inst.addOperand(MCOperand::CreateReg(GPReg));
2385     Inst.addOperand(MCOperand::CreateReg(getGPR(matchCPURegisterName("sp"))));
2386     Inst.addOperand(MCOperand::CreateImm(Save));
2387   }
2388   TS.EmitInstruction(Inst, STI);
2389   Inst.clear();
2390
2391   const MCSymbolRefExpr *HiExpr = MCSymbolRefExpr::Create(
2392       Sym->getName(), MCSymbolRefExpr::VK_Mips_GPOFF_HI, getContext());
2393   const MCSymbolRefExpr *LoExpr = MCSymbolRefExpr::Create(
2394       Sym->getName(), MCSymbolRefExpr::VK_Mips_GPOFF_LO, getContext());
2395   // lui $gp, %hi(%neg(%gp_rel(funcSym)))
2396   Inst.setOpcode(Mips::LUi);
2397   Inst.addOperand(MCOperand::CreateReg(GPReg));
2398   Inst.addOperand(MCOperand::CreateExpr(HiExpr));
2399   TS.EmitInstruction(Inst, STI);
2400   Inst.clear();
2401
2402   // addiu  $gp, $gp, %lo(%neg(%gp_rel(funcSym)))
2403   Inst.setOpcode(Mips::ADDiu);
2404   Inst.addOperand(MCOperand::CreateReg(GPReg));
2405   Inst.addOperand(MCOperand::CreateReg(GPReg));
2406   Inst.addOperand(MCOperand::CreateExpr(LoExpr));
2407   TS.EmitInstruction(Inst, STI);
2408   Inst.clear();
2409
2410   // daddu  $gp, $gp, $funcreg
2411   Inst.setOpcode(Mips::DADDu);
2412   Inst.addOperand(MCOperand::CreateReg(GPReg));
2413   Inst.addOperand(MCOperand::CreateReg(GPReg));
2414   Inst.addOperand(MCOperand::CreateReg(FuncReg));
2415   TS.EmitInstruction(Inst, STI);
2416   return false;
2417 }
2418
2419 bool MipsAsmParser::parseDirectiveNaN() {
2420   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2421     const AsmToken &Tok = Parser.getTok();
2422
2423     if (Tok.getString() == "2008") {
2424       Parser.Lex();
2425       getTargetStreamer().emitDirectiveNaN2008();
2426       return false;
2427     } else if (Tok.getString() == "legacy") {
2428       Parser.Lex();
2429       getTargetStreamer().emitDirectiveNaNLegacy();
2430       return false;
2431     }
2432   }
2433   // If we don't recognize the option passed to the .nan
2434   // directive (e.g. no option or unknown option), emit an error.
2435   reportParseError("invalid option in .nan directive");
2436   return false;
2437 }
2438
2439 bool MipsAsmParser::parseDirectiveSet() {
2440
2441   // Get the next token.
2442   const AsmToken &Tok = Parser.getTok();
2443
2444   if (Tok.getString() == "noat") {
2445     return parseSetNoAtDirective();
2446   } else if (Tok.getString() == "at") {
2447     return parseSetAtDirective();
2448   } else if (Tok.getString() == "reorder") {
2449     return parseSetReorderDirective();
2450   } else if (Tok.getString() == "noreorder") {
2451     return parseSetNoReorderDirective();
2452   } else if (Tok.getString() == "macro") {
2453     return parseSetMacroDirective();
2454   } else if (Tok.getString() == "nomacro") {
2455     return parseSetNoMacroDirective();
2456   } else if (Tok.getString() == "mips16") {
2457     return parseSetFeature(Mips::FeatureMips16);
2458   } else if (Tok.getString() == "nomips16") {
2459     return parseSetNoMips16Directive();
2460   } else if (Tok.getString() == "nomicromips") {
2461     getTargetStreamer().emitDirectiveSetNoMicroMips();
2462     Parser.eatToEndOfStatement();
2463     return false;
2464   } else if (Tok.getString() == "micromips") {
2465     return parseSetFeature(Mips::FeatureMicroMips);
2466   } else if (Tok.getString() == "mips32r2") {
2467     return parseSetFeature(Mips::FeatureMips32r2);
2468   } else if (Tok.getString() == "mips64") {
2469     return parseSetFeature(Mips::FeatureMips64);
2470   } else if (Tok.getString() == "mips64r2") {
2471     return parseSetFeature(Mips::FeatureMips64r2);
2472   } else if (Tok.getString() == "dsp") {
2473     return parseSetFeature(Mips::FeatureDSP);
2474   } else {
2475     // It is just an identifier, look for an assignment.
2476     parseSetAssignment();
2477     return false;
2478   }
2479
2480   return true;
2481 }
2482
2483 /// parseDataDirective
2484 ///  ::= .word [ expression (, expression)* ]
2485 bool MipsAsmParser::parseDataDirective(unsigned Size, SMLoc L) {
2486   if (getLexer().isNot(AsmToken::EndOfStatement)) {
2487     for (;;) {
2488       const MCExpr *Value;
2489       if (getParser().parseExpression(Value))
2490         return true;
2491
2492       getParser().getStreamer().EmitValue(Value, Size);
2493
2494       if (getLexer().is(AsmToken::EndOfStatement))
2495         break;
2496
2497       // FIXME: Improve diagnostic.
2498       if (getLexer().isNot(AsmToken::Comma))
2499         return Error(L, "unexpected token in directive");
2500       Parser.Lex();
2501     }
2502   }
2503
2504   Parser.Lex();
2505   return false;
2506 }
2507
2508 /// parseDirectiveGpWord
2509 ///  ::= .gpword local_sym
2510 bool MipsAsmParser::parseDirectiveGpWord() {
2511   const MCExpr *Value;
2512   // EmitGPRel32Value requires an expression, so we are using base class
2513   // method to evaluate the expression.
2514   if (getParser().parseExpression(Value))
2515     return true;
2516   getParser().getStreamer().EmitGPRel32Value(Value);
2517
2518   if (getLexer().isNot(AsmToken::EndOfStatement))
2519     return Error(getLexer().getLoc(), "unexpected token in directive");
2520   Parser.Lex(); // Eat EndOfStatement token.
2521   return false;
2522 }
2523
2524 /// parseDirectiveGpDWord
2525 ///  ::= .gpdword local_sym
2526 bool MipsAsmParser::parseDirectiveGpDWord() {
2527   const MCExpr *Value;
2528   // EmitGPRel64Value requires an expression, so we are using base class
2529   // method to evaluate the expression.
2530   if (getParser().parseExpression(Value))
2531     return true;
2532   getParser().getStreamer().EmitGPRel64Value(Value);
2533
2534   if (getLexer().isNot(AsmToken::EndOfStatement))
2535     return Error(getLexer().getLoc(), "unexpected token in directive");
2536   Parser.Lex(); // Eat EndOfStatement token.
2537   return false;
2538 }
2539
2540 bool MipsAsmParser::parseDirectiveOption() {
2541   // Get the option token.
2542   AsmToken Tok = Parser.getTok();
2543   // At the moment only identifiers are supported.
2544   if (Tok.isNot(AsmToken::Identifier)) {
2545     Error(Parser.getTok().getLoc(), "unexpected token in .option directive");
2546     Parser.eatToEndOfStatement();
2547     return false;
2548   }
2549
2550   StringRef Option = Tok.getIdentifier();
2551
2552   if (Option == "pic0") {
2553     getTargetStreamer().emitDirectiveOptionPic0();
2554     Parser.Lex();
2555     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
2556       Error(Parser.getTok().getLoc(),
2557             "unexpected token in .option pic0 directive");
2558       Parser.eatToEndOfStatement();
2559     }
2560     return false;
2561   }
2562
2563   if (Option == "pic2") {
2564     getTargetStreamer().emitDirectiveOptionPic2();
2565     Parser.Lex();
2566     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
2567       Error(Parser.getTok().getLoc(),
2568             "unexpected token in .option pic2 directive");
2569       Parser.eatToEndOfStatement();
2570     }
2571     return false;
2572   }
2573
2574   // Unknown option.
2575   Warning(Parser.getTok().getLoc(), "unknown option in .option directive");
2576   Parser.eatToEndOfStatement();
2577   return false;
2578 }
2579
2580 bool MipsAsmParser::ParseDirective(AsmToken DirectiveID) {
2581   StringRef IDVal = DirectiveID.getString();
2582
2583   if (IDVal == ".cpload")
2584     return parseDirectiveCPLoad(DirectiveID.getLoc());
2585   if (IDVal == ".dword") {
2586     parseDataDirective(8, DirectiveID.getLoc());
2587     return false;
2588   }
2589
2590   if (IDVal == ".ent") {
2591     // Ignore this directive for now.
2592     Parser.Lex();
2593     return false;
2594   }
2595
2596   if (IDVal == ".end") {
2597     // Ignore this directive for now.
2598     Parser.Lex();
2599     return false;
2600   }
2601
2602   if (IDVal == ".frame") {
2603     // Ignore this directive for now.
2604     Parser.eatToEndOfStatement();
2605     return false;
2606   }
2607
2608   if (IDVal == ".set") {
2609     return parseDirectiveSet();
2610   }
2611
2612   if (IDVal == ".fmask") {
2613     // Ignore this directive for now.
2614     Parser.eatToEndOfStatement();
2615     return false;
2616   }
2617
2618   if (IDVal == ".mask") {
2619     // Ignore this directive for now.
2620     Parser.eatToEndOfStatement();
2621     return false;
2622   }
2623
2624   if (IDVal == ".nan")
2625     return parseDirectiveNaN();
2626
2627   if (IDVal == ".gpword") {
2628     parseDirectiveGpWord();
2629     return false;
2630   }
2631
2632   if (IDVal == ".gpdword") {
2633     parseDirectiveGpDWord();
2634     return false;
2635   }
2636
2637   if (IDVal == ".word") {
2638     parseDataDirective(4, DirectiveID.getLoc());
2639     return false;
2640   }
2641
2642   if (IDVal == ".option")
2643     return parseDirectiveOption();
2644
2645   if (IDVal == ".abicalls") {
2646     getTargetStreamer().emitDirectiveAbiCalls();
2647     if (Parser.getTok().isNot(AsmToken::EndOfStatement)) {
2648       Error(Parser.getTok().getLoc(), "unexpected token in directive");
2649       // Clear line
2650       Parser.eatToEndOfStatement();
2651     }
2652     return false;
2653   }
2654
2655   if (IDVal == ".cpsetup")
2656     return parseDirectiveCPSetup();
2657
2658   return true;
2659 }
2660
2661 extern "C" void LLVMInitializeMipsAsmParser() {
2662   RegisterMCAsmParser<MipsAsmParser> X(TheMipsTarget);
2663   RegisterMCAsmParser<MipsAsmParser> Y(TheMipselTarget);
2664   RegisterMCAsmParser<MipsAsmParser> A(TheMips64Target);
2665   RegisterMCAsmParser<MipsAsmParser> B(TheMips64elTarget);
2666 }
2667
2668 #define GET_REGISTER_MATCHER
2669 #define GET_MATCHER_IMPLEMENTATION
2670 #include "MipsGenAsmMatcher.inc"