184cf0f658196a81d691f29f5bb9ba0459202ebf
[oota-llvm.git] / lib / Target / AMDGPU / AsmParser / AMDGPUAsmParser.cpp
1 //===-- AMDGPUAsmParser.cpp - Parse SI asm 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/AMDGPUMCTargetDesc.h"
11 #include "MCTargetDesc/AMDGPUTargetStreamer.h"
12 #include "Utils/AMDGPUBaseInfo.h"
13 #include "AMDKernelCodeT.h"
14 #include "SIDefines.h"
15 #include "llvm/ADT/APFloat.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCInstrInfo.h"
25 #include "llvm/MC/MCParser/MCAsmLexer.h"
26 #include "llvm/MC/MCParser/MCAsmParser.h"
27 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
28 #include "llvm/MC/MCRegisterInfo.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSubtargetInfo.h"
31 #include "llvm/MC/MCSymbolELF.h"
32 #include "llvm/MC/MCTargetAsmParser.h"
33 #include "llvm/Support/ELF.h"
34 #include "llvm/Support/SourceMgr.h"
35 #include "llvm/Support/TargetRegistry.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Support/Debug.h"
38
39 using namespace llvm;
40
41 namespace {
42
43 struct OptionalOperand;
44
45 class AMDGPUOperand : public MCParsedAsmOperand {
46   enum KindTy {
47     Token,
48     Immediate,
49     Register,
50     Expression
51   } Kind;
52
53   SMLoc StartLoc, EndLoc;
54
55 public:
56   AMDGPUOperand(enum KindTy K) : MCParsedAsmOperand(), Kind(K) {}
57
58   MCContext *Ctx;
59
60   enum ImmTy {
61     ImmTyNone,
62     ImmTyDSOffset0,
63     ImmTyDSOffset1,
64     ImmTyGDS,
65     ImmTyOffset,
66     ImmTyGLC,
67     ImmTySLC,
68     ImmTyTFE,
69     ImmTyClamp,
70     ImmTyOMod
71   };
72
73   struct TokOp {
74     const char *Data;
75     unsigned Length;
76   };
77
78   struct ImmOp {
79     bool IsFPImm;
80     ImmTy Type;
81     int64_t Val;
82   };
83
84   struct RegOp {
85     unsigned RegNo;
86     int Modifiers;
87     const MCRegisterInfo *TRI;
88     bool IsForcedVOP3;
89   };
90
91   union {
92     TokOp Tok;
93     ImmOp Imm;
94     RegOp Reg;
95     const MCExpr *Expr;
96   };
97
98   void addImmOperands(MCInst &Inst, unsigned N) const {
99     Inst.addOperand(MCOperand::createImm(getImm()));
100   }
101
102   StringRef getToken() const {
103     return StringRef(Tok.Data, Tok.Length);
104   }
105
106   void addRegOperands(MCInst &Inst, unsigned N) const {
107     Inst.addOperand(MCOperand::createReg(getReg()));
108   }
109
110   void addRegOrImmOperands(MCInst &Inst, unsigned N) const {
111     if (isReg())
112       addRegOperands(Inst, N);
113     else
114       addImmOperands(Inst, N);
115   }
116
117   void addRegWithInputModsOperands(MCInst &Inst, unsigned N) const {
118     Inst.addOperand(MCOperand::createImm(
119         Reg.Modifiers == -1 ? 0 : Reg.Modifiers));
120     addRegOperands(Inst, N);
121   }
122
123   void addSoppBrTargetOperands(MCInst &Inst, unsigned N) const {
124     if (isImm())
125       addImmOperands(Inst, N);
126     else {
127       assert(isExpr());
128       Inst.addOperand(MCOperand::createExpr(Expr));
129     }
130   }
131
132   bool defaultTokenHasSuffix() const {
133     StringRef Token(Tok.Data, Tok.Length);
134
135     return Token.endswith("_e32") || Token.endswith("_e64");
136   }
137
138   bool isToken() const override {
139     return Kind == Token;
140   }
141
142   bool isImm() const override {
143     return Kind == Immediate;
144   }
145
146   bool isInlineImm() const {
147     float F = BitsToFloat(Imm.Val);
148     // TODO: Add 0.5pi for VI
149     return isImm() && ((Imm.Val <= 64 && Imm.Val >= -16) ||
150            (F == 0.0 || F == 0.5 || F == -0.5 || F == 1.0 || F == -1.0 ||
151            F == 2.0 || F == -2.0 || F == 4.0 || F == -4.0));
152   }
153
154   bool isDSOffset0() const {
155     assert(isImm());
156     return Imm.Type == ImmTyDSOffset0;
157   }
158
159   bool isDSOffset1() const {
160     assert(isImm());
161     return Imm.Type == ImmTyDSOffset1;
162   }
163
164   int64_t getImm() const {
165     return Imm.Val;
166   }
167
168   enum ImmTy getImmTy() const {
169     assert(isImm());
170     return Imm.Type;
171   }
172
173   bool isRegKind() const {
174     return Kind == Register;
175   }
176
177   bool isReg() const override {
178     return Kind == Register && Reg.Modifiers == -1;
179   }
180
181   bool isRegWithInputMods() const {
182     return Kind == Register && (Reg.IsForcedVOP3 || Reg.Modifiers != -1);
183   }
184
185   void setModifiers(unsigned Mods) {
186     assert(isReg());
187     Reg.Modifiers = Mods;
188   }
189
190   bool hasModifiers() const {
191     assert(isRegKind());
192     return Reg.Modifiers != -1;
193   }
194
195   unsigned getReg() const override {
196     return Reg.RegNo;
197   }
198
199   bool isRegOrImm() const {
200     return isReg() || isImm();
201   }
202
203   bool isRegClass(unsigned RCID) const {
204     return Reg.TRI->getRegClass(RCID).contains(getReg());
205   }
206
207   bool isSCSrc32() const {
208     return isInlineImm() || (isReg() && isRegClass(AMDGPU::SReg_32RegClassID));
209   }
210
211   bool isSSrc32() const {
212     return isImm() || (isReg() && isRegClass(AMDGPU::SReg_32RegClassID));
213   }
214
215   bool isSSrc64() const {
216     return isImm() || isInlineImm() ||
217            (isReg() && isRegClass(AMDGPU::SReg_64RegClassID));
218   }
219
220   bool isSCSrc64() const {
221     return (isReg() && isRegClass(AMDGPU::SReg_64RegClassID)) || isInlineImm();
222   }
223
224   bool isVCSrc32() const {
225     return isInlineImm() || (isReg() && isRegClass(AMDGPU::VS_32RegClassID));
226   }
227
228   bool isVCSrc64() const {
229     return isInlineImm() || (isReg() && isRegClass(AMDGPU::VS_64RegClassID));
230   }
231
232   bool isVSrc32() const {
233     return isImm() || (isReg() && isRegClass(AMDGPU::VS_32RegClassID));
234   }
235
236   bool isVSrc64() const {
237     return isImm() || (isReg() && isRegClass(AMDGPU::VS_64RegClassID));
238   }
239
240   bool isMem() const override {
241     return false;
242   }
243
244   bool isExpr() const {
245     return Kind == Expression;
246   }
247
248   bool isSoppBrTarget() const {
249     return isExpr() || isImm();
250   }
251
252   SMLoc getStartLoc() const override {
253     return StartLoc;
254   }
255
256   SMLoc getEndLoc() const override {
257     return EndLoc;
258   }
259
260   void print(raw_ostream &OS) const override {
261     switch (Kind) {
262     case Register:
263       OS << "<register " << getReg() << " mods: " << Reg.Modifiers << '>';
264       break;
265     case Immediate:
266       OS << getImm();
267       break;
268     case Token:
269       OS << '\'' << getToken() << '\'';
270       break;
271     case Expression:
272       OS << "<expr " << *Expr << '>';
273       break;
274     }
275   }
276
277   static std::unique_ptr<AMDGPUOperand> CreateImm(int64_t Val, SMLoc Loc,
278                                                   enum ImmTy Type = ImmTyNone,
279                                                   bool IsFPImm = false) {
280     auto Op = llvm::make_unique<AMDGPUOperand>(Immediate);
281     Op->Imm.Val = Val;
282     Op->Imm.IsFPImm = IsFPImm;
283     Op->Imm.Type = Type;
284     Op->StartLoc = Loc;
285     Op->EndLoc = Loc;
286     return Op;
287   }
288
289   static std::unique_ptr<AMDGPUOperand> CreateToken(StringRef Str, SMLoc Loc,
290                                            bool HasExplicitEncodingSize = true) {
291     auto Res = llvm::make_unique<AMDGPUOperand>(Token);
292     Res->Tok.Data = Str.data();
293     Res->Tok.Length = Str.size();
294     Res->StartLoc = Loc;
295     Res->EndLoc = Loc;
296     return Res;
297   }
298
299   static std::unique_ptr<AMDGPUOperand> CreateReg(unsigned RegNo, SMLoc S,
300                                                   SMLoc E,
301                                                   const MCRegisterInfo *TRI,
302                                                   bool ForceVOP3) {
303     auto Op = llvm::make_unique<AMDGPUOperand>(Register);
304     Op->Reg.RegNo = RegNo;
305     Op->Reg.TRI = TRI;
306     Op->Reg.Modifiers = -1;
307     Op->Reg.IsForcedVOP3 = ForceVOP3;
308     Op->StartLoc = S;
309     Op->EndLoc = E;
310     return Op;
311   }
312
313   static std::unique_ptr<AMDGPUOperand> CreateExpr(const class MCExpr *Expr, SMLoc S) {
314     auto Op = llvm::make_unique<AMDGPUOperand>(Expression);
315     Op->Expr = Expr;
316     Op->StartLoc = S;
317     Op->EndLoc = S;
318     return Op;
319   }
320
321   bool isDSOffset() const;
322   bool isDSOffset01() const;
323   bool isSWaitCnt() const;
324   bool isMubufOffset() const;
325   bool isSMRDOffset() const;
326   bool isSMRDLiteralOffset() const;
327 };
328
329 class AMDGPUAsmParser : public MCTargetAsmParser {
330   const MCInstrInfo &MII;
331   MCAsmParser &Parser;
332
333   unsigned ForcedEncodingSize;
334
335   bool isSI() const {
336     return STI->getFeatureBits()[AMDGPU::FeatureSouthernIslands];
337   }
338
339   bool isCI() const {
340     return STI->getFeatureBits()[AMDGPU::FeatureSeaIslands];
341   }
342
343   bool isVI() const {
344     return getSTI().getFeatureBits()[AMDGPU::FeatureVolcanicIslands];
345   }
346
347   bool hasSGPR102_SGPR103() const {
348     return !isVI();
349   }
350
351   /// @name Auto-generated Match Functions
352   /// {
353
354 #define GET_ASSEMBLER_HEADER
355 #include "AMDGPUGenAsmMatcher.inc"
356
357   /// }
358
359 private:
360   bool ParseDirectiveMajorMinor(uint32_t &Major, uint32_t &Minor);
361   bool ParseDirectiveHSACodeObjectVersion();
362   bool ParseDirectiveHSACodeObjectISA();
363   bool ParseAMDKernelCodeTValue(StringRef ID, amd_kernel_code_t &Header);
364   bool ParseDirectiveAMDKernelCodeT();
365   bool ParseSectionDirectiveHSAText();
366   bool subtargetHasRegister(const MCRegisterInfo &MRI, unsigned RegNo) const;
367   bool ParseDirectiveAMDGPUHsaKernel();
368
369 public:
370 public:
371   enum AMDGPUMatchResultTy {
372     Match_PreferE32 = FIRST_TARGET_MATCH_RESULT_TY
373   };
374
375   AMDGPUAsmParser(const MCSubtargetInfo &STI, MCAsmParser &_Parser,
376                const MCInstrInfo &MII,
377                const MCTargetOptions &Options)
378       : MCTargetAsmParser(Options, STI), MII(MII), Parser(_Parser),
379         ForcedEncodingSize(0) {
380     MCAsmParserExtension::Initialize(Parser);
381
382     if (getSTI().getFeatureBits().none()) {
383       // Set default features.
384       copySTI().ToggleFeature("SOUTHERN_ISLANDS");
385     }
386
387     setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
388   }
389
390   AMDGPUTargetStreamer &getTargetStreamer() {
391     MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
392     return static_cast<AMDGPUTargetStreamer &>(TS);
393   }
394
395   unsigned getForcedEncodingSize() const {
396     return ForcedEncodingSize;
397   }
398
399   void setForcedEncodingSize(unsigned Size) {
400     ForcedEncodingSize = Size;
401   }
402
403   bool isForcedVOP3() const {
404     return ForcedEncodingSize == 64;
405   }
406
407   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
408   unsigned checkTargetMatchPredicate(MCInst &Inst) override;
409   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
410                                OperandVector &Operands, MCStreamer &Out,
411                                uint64_t &ErrorInfo,
412                                bool MatchingInlineAsm) override;
413   bool ParseDirective(AsmToken DirectiveID) override;
414   OperandMatchResultTy parseOperand(OperandVector &Operands, StringRef Mnemonic);
415   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
416                         SMLoc NameLoc, OperandVector &Operands) override;
417
418   OperandMatchResultTy parseIntWithPrefix(const char *Prefix, int64_t &Int,
419                                           int64_t Default = 0);
420   OperandMatchResultTy parseIntWithPrefix(const char *Prefix,
421                                           OperandVector &Operands,
422                                           enum AMDGPUOperand::ImmTy ImmTy =
423                                                       AMDGPUOperand::ImmTyNone);
424   OperandMatchResultTy parseNamedBit(const char *Name, OperandVector &Operands,
425                                      enum AMDGPUOperand::ImmTy ImmTy =
426                                                       AMDGPUOperand::ImmTyNone);
427   OperandMatchResultTy parseOptionalOps(
428                                    const ArrayRef<OptionalOperand> &OptionalOps,
429                                    OperandVector &Operands);
430
431
432   void cvtDSOffset01(MCInst &Inst, const OperandVector &Operands);
433   void cvtDS(MCInst &Inst, const OperandVector &Operands);
434   OperandMatchResultTy parseDSOptionalOps(OperandVector &Operands);
435   OperandMatchResultTy parseDSOff01OptionalOps(OperandVector &Operands);
436   OperandMatchResultTy parseDSOffsetOptional(OperandVector &Operands);
437
438   bool parseCnt(int64_t &IntVal);
439   OperandMatchResultTy parseSWaitCntOps(OperandVector &Operands);
440   OperandMatchResultTy parseSOppBrTarget(OperandVector &Operands);
441
442   OperandMatchResultTy parseFlatOptionalOps(OperandVector &Operands);
443   OperandMatchResultTy parseFlatAtomicOptionalOps(OperandVector &Operands);
444   void cvtFlat(MCInst &Inst, const OperandVector &Operands);
445
446   void cvtMubuf(MCInst &Inst, const OperandVector &Operands);
447   OperandMatchResultTy parseOffset(OperandVector &Operands);
448   OperandMatchResultTy parseMubufOptionalOps(OperandVector &Operands);
449   OperandMatchResultTy parseGLC(OperandVector &Operands);
450   OperandMatchResultTy parseSLC(OperandVector &Operands);
451   OperandMatchResultTy parseTFE(OperandVector &Operands);
452
453   OperandMatchResultTy parseDMask(OperandVector &Operands);
454   OperandMatchResultTy parseUNorm(OperandVector &Operands);
455   OperandMatchResultTy parseR128(OperandVector &Operands);
456
457   void cvtVOP3(MCInst &Inst, const OperandVector &Operands);
458   OperandMatchResultTy parseVOP3OptionalOps(OperandVector &Operands);
459 };
460
461 struct OptionalOperand {
462   const char *Name;
463   AMDGPUOperand::ImmTy Type;
464   bool IsBit;
465   int64_t Default;
466   bool (*ConvertResult)(int64_t&);
467 };
468
469 }
470
471 static int getRegClass(bool IsVgpr, unsigned RegWidth) {
472   if (IsVgpr) {
473     switch (RegWidth) {
474       default: return -1;
475       case 1: return AMDGPU::VGPR_32RegClassID;
476       case 2: return AMDGPU::VReg_64RegClassID;
477       case 3: return AMDGPU::VReg_96RegClassID;
478       case 4: return AMDGPU::VReg_128RegClassID;
479       case 8: return AMDGPU::VReg_256RegClassID;
480       case 16: return AMDGPU::VReg_512RegClassID;
481     }
482   }
483
484   switch (RegWidth) {
485     default: return -1;
486     case 1: return AMDGPU::SGPR_32RegClassID;
487     case 2: return AMDGPU::SGPR_64RegClassID;
488     case 4: return AMDGPU::SReg_128RegClassID;
489     case 8: return AMDGPU::SReg_256RegClassID;
490     case 16: return AMDGPU::SReg_512RegClassID;
491   }
492 }
493
494 static unsigned getRegForName(StringRef RegName) {
495
496   return StringSwitch<unsigned>(RegName)
497     .Case("exec", AMDGPU::EXEC)
498     .Case("vcc", AMDGPU::VCC)
499     .Case("flat_scratch", AMDGPU::FLAT_SCR)
500     .Case("m0", AMDGPU::M0)
501     .Case("scc", AMDGPU::SCC)
502     .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
503     .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
504     .Case("vcc_lo", AMDGPU::VCC_LO)
505     .Case("vcc_hi", AMDGPU::VCC_HI)
506     .Case("exec_lo", AMDGPU::EXEC_LO)
507     .Case("exec_hi", AMDGPU::EXEC_HI)
508     .Default(0);
509 }
510
511 bool AMDGPUAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) {
512   const AsmToken Tok = Parser.getTok();
513   StartLoc = Tok.getLoc();
514   EndLoc = Tok.getEndLoc();
515   const MCRegisterInfo *TRI = getContext().getRegisterInfo();
516
517   StringRef RegName = Tok.getString();
518   RegNo = getRegForName(RegName);
519
520   if (RegNo) {
521     Parser.Lex();
522     return !subtargetHasRegister(*TRI, RegNo);
523   }
524
525   // Match vgprs and sgprs
526   if (RegName[0] != 's' && RegName[0] != 'v')
527     return true;
528
529   bool IsVgpr = RegName[0] == 'v';
530   unsigned RegWidth;
531   unsigned RegIndexInClass;
532   if (RegName.size() > 1) {
533     // We have a 32-bit register
534     RegWidth = 1;
535     if (RegName.substr(1).getAsInteger(10, RegIndexInClass))
536       return true;
537     Parser.Lex();
538   } else {
539     // We have a register greater than 32-bits.
540
541     int64_t RegLo, RegHi;
542     Parser.Lex();
543     if (getLexer().isNot(AsmToken::LBrac))
544       return true;
545
546     Parser.Lex();
547     if (getParser().parseAbsoluteExpression(RegLo))
548       return true;
549
550     if (getLexer().isNot(AsmToken::Colon))
551       return true;
552
553     Parser.Lex();
554     if (getParser().parseAbsoluteExpression(RegHi))
555       return true;
556
557     if (getLexer().isNot(AsmToken::RBrac))
558       return true;
559
560     Parser.Lex();
561     RegWidth = (RegHi - RegLo) + 1;
562     if (IsVgpr) {
563       // VGPR registers aren't aligned.
564       RegIndexInClass = RegLo;
565     } else {
566       // SGPR registers are aligned.  Max alignment is 4 dwords.
567       unsigned Size = std::min(RegWidth, 4u);
568       if (RegLo % Size != 0)
569         return true;
570
571       RegIndexInClass = RegLo / Size;
572     }
573   }
574
575   int RCID = getRegClass(IsVgpr, RegWidth);
576   if (RCID == -1)
577     return true;
578
579   const MCRegisterClass RC = TRI->getRegClass(RCID);
580   if (RegIndexInClass >= RC.getNumRegs())
581     return true;
582
583   RegNo = RC.getRegister(RegIndexInClass);
584   return !subtargetHasRegister(*TRI, RegNo);
585 }
586
587 unsigned AMDGPUAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
588
589   uint64_t TSFlags = MII.get(Inst.getOpcode()).TSFlags;
590
591   if ((getForcedEncodingSize() == 32 && (TSFlags & SIInstrFlags::VOP3)) ||
592       (getForcedEncodingSize() == 64 && !(TSFlags & SIInstrFlags::VOP3)))
593     return Match_InvalidOperand;
594
595   if ((TSFlags & SIInstrFlags::VOP3) &&
596       (TSFlags & SIInstrFlags::VOPAsmPrefer32Bit) &&
597       getForcedEncodingSize() != 64)
598     return Match_PreferE32;
599
600   return Match_Success;
601 }
602
603
604 bool AMDGPUAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
605                                               OperandVector &Operands,
606                                               MCStreamer &Out,
607                                               uint64_t &ErrorInfo,
608                                               bool MatchingInlineAsm) {
609   MCInst Inst;
610
611   switch (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm)) {
612     default: break;
613     case Match_Success:
614       Inst.setLoc(IDLoc);
615       Out.EmitInstruction(Inst, getSTI());
616       return false;
617     case Match_MissingFeature:
618       return Error(IDLoc, "instruction not supported on this GPU");
619
620     case Match_MnemonicFail:
621       return Error(IDLoc, "unrecognized instruction mnemonic");
622
623     case Match_InvalidOperand: {
624       SMLoc ErrorLoc = IDLoc;
625       if (ErrorInfo != ~0ULL) {
626         if (ErrorInfo >= Operands.size()) {
627           if (isForcedVOP3()) {
628             // If 64-bit encoding has been forced we can end up with no
629             // clamp or omod operands if none of the registers have modifiers,
630             // so we need to add these to the operand list.
631             AMDGPUOperand &LastOp =
632                 ((AMDGPUOperand &)*Operands[Operands.size() - 1]);
633             if (LastOp.isRegKind() ||
634                (LastOp.isImm() &&
635                 LastOp.getImmTy() != AMDGPUOperand::ImmTyNone)) {
636               SMLoc S = Parser.getTok().getLoc();
637               Operands.push_back(AMDGPUOperand::CreateImm(0, S,
638                                  AMDGPUOperand::ImmTyClamp));
639               Operands.push_back(AMDGPUOperand::CreateImm(0, S,
640                                  AMDGPUOperand::ImmTyOMod));
641               bool Res = MatchAndEmitInstruction(IDLoc, Opcode, Operands,
642                                                  Out, ErrorInfo,
643                                                  MatchingInlineAsm);
644               if (!Res)
645                 return Res;
646             }
647
648           }
649           return Error(IDLoc, "too few operands for instruction");
650         }
651
652         ErrorLoc = ((AMDGPUOperand &)*Operands[ErrorInfo]).getStartLoc();
653         if (ErrorLoc == SMLoc())
654           ErrorLoc = IDLoc;
655       }
656       return Error(ErrorLoc, "invalid operand for instruction");
657     }
658     case Match_PreferE32:
659       return Error(IDLoc, "internal error: instruction without _e64 suffix "
660                           "should be encoded as e32");
661   }
662   llvm_unreachable("Implement any new match types added!");
663 }
664
665 bool AMDGPUAsmParser::ParseDirectiveMajorMinor(uint32_t &Major,
666                                                uint32_t &Minor) {
667   if (getLexer().isNot(AsmToken::Integer))
668     return TokError("invalid major version");
669
670   Major = getLexer().getTok().getIntVal();
671   Lex();
672
673   if (getLexer().isNot(AsmToken::Comma))
674     return TokError("minor version number required, comma expected");
675   Lex();
676
677   if (getLexer().isNot(AsmToken::Integer))
678     return TokError("invalid minor version");
679
680   Minor = getLexer().getTok().getIntVal();
681   Lex();
682
683   return false;
684 }
685
686 bool AMDGPUAsmParser::ParseDirectiveHSACodeObjectVersion() {
687
688   uint32_t Major;
689   uint32_t Minor;
690
691   if (ParseDirectiveMajorMinor(Major, Minor))
692     return true;
693
694   getTargetStreamer().EmitDirectiveHSACodeObjectVersion(Major, Minor);
695   return false;
696 }
697
698 bool AMDGPUAsmParser::ParseDirectiveHSACodeObjectISA() {
699
700   uint32_t Major;
701   uint32_t Minor;
702   uint32_t Stepping;
703   StringRef VendorName;
704   StringRef ArchName;
705
706   // If this directive has no arguments, then use the ISA version for the
707   // targeted GPU.
708   if (getLexer().is(AsmToken::EndOfStatement)) {
709     AMDGPU::IsaVersion Isa = AMDGPU::getIsaVersion(getSTI().getFeatureBits());
710     getTargetStreamer().EmitDirectiveHSACodeObjectISA(Isa.Major, Isa.Minor,
711                                                       Isa.Stepping,
712                                                       "AMD", "AMDGPU");
713     return false;
714   }
715
716
717   if (ParseDirectiveMajorMinor(Major, Minor))
718     return true;
719
720   if (getLexer().isNot(AsmToken::Comma))
721     return TokError("stepping version number required, comma expected");
722   Lex();
723
724   if (getLexer().isNot(AsmToken::Integer))
725     return TokError("invalid stepping version");
726
727   Stepping = getLexer().getTok().getIntVal();
728   Lex();
729
730   if (getLexer().isNot(AsmToken::Comma))
731     return TokError("vendor name required, comma expected");
732   Lex();
733
734   if (getLexer().isNot(AsmToken::String))
735     return TokError("invalid vendor name");
736
737   VendorName = getLexer().getTok().getStringContents();
738   Lex();
739
740   if (getLexer().isNot(AsmToken::Comma))
741     return TokError("arch name required, comma expected");
742   Lex();
743
744   if (getLexer().isNot(AsmToken::String))
745     return TokError("invalid arch name");
746
747   ArchName = getLexer().getTok().getStringContents();
748   Lex();
749
750   getTargetStreamer().EmitDirectiveHSACodeObjectISA(Major, Minor, Stepping,
751                                                     VendorName, ArchName);
752   return false;
753 }
754
755 bool AMDGPUAsmParser::ParseAMDKernelCodeTValue(StringRef ID,
756                                                amd_kernel_code_t &Header) {
757
758   if (getLexer().isNot(AsmToken::Equal))
759     return TokError("expected '='");
760   Lex();
761
762   if (getLexer().isNot(AsmToken::Integer))
763     return TokError("amd_kernel_code_t values must be integers");
764
765   uint64_t Value = getLexer().getTok().getIntVal();
766   Lex();
767
768   if (ID == "kernel_code_version_major")
769     Header.amd_kernel_code_version_major = Value;
770   else if (ID == "kernel_code_version_minor")
771     Header.amd_kernel_code_version_minor = Value;
772   else if (ID == "machine_kind")
773     Header.amd_machine_kind = Value;
774   else if (ID == "machine_version_major")
775     Header.amd_machine_version_major = Value;
776   else if (ID == "machine_version_minor")
777     Header.amd_machine_version_minor = Value;
778   else if (ID == "machine_version_stepping")
779     Header.amd_machine_version_stepping = Value;
780   else if (ID == "kernel_code_entry_byte_offset")
781     Header.kernel_code_entry_byte_offset = Value;
782   else if (ID == "kernel_code_prefetch_byte_size")
783     Header.kernel_code_prefetch_byte_size = Value;
784   else if (ID == "max_scratch_backing_memory_byte_size")
785     Header.max_scratch_backing_memory_byte_size = Value;
786   else if (ID == "compute_pgm_rsrc1_vgprs")
787     Header.compute_pgm_resource_registers |= S_00B848_VGPRS(Value);
788   else if (ID == "compute_pgm_rsrc1_sgprs")
789     Header.compute_pgm_resource_registers |= S_00B848_SGPRS(Value);
790   else if (ID == "compute_pgm_rsrc1_priority")
791     Header.compute_pgm_resource_registers |= S_00B848_PRIORITY(Value);
792   else if (ID == "compute_pgm_rsrc1_float_mode")
793     Header.compute_pgm_resource_registers |= S_00B848_FLOAT_MODE(Value);
794   else if (ID == "compute_pgm_rsrc1_priv")
795     Header.compute_pgm_resource_registers |= S_00B848_PRIV(Value);
796   else if (ID == "compute_pgm_rsrc1_dx10_clamp")
797     Header.compute_pgm_resource_registers |= S_00B848_DX10_CLAMP(Value);
798   else if (ID == "compute_pgm_rsrc1_debug_mode")
799     Header.compute_pgm_resource_registers |= S_00B848_DEBUG_MODE(Value);
800   else if (ID == "compute_pgm_rsrc1_ieee_mode")
801     Header.compute_pgm_resource_registers |= S_00B848_IEEE_MODE(Value);
802   else if (ID == "compute_pgm_rsrc2_scratch_en")
803     Header.compute_pgm_resource_registers |= (S_00B84C_SCRATCH_EN(Value) << 32);
804   else if (ID == "compute_pgm_rsrc2_user_sgpr")
805     Header.compute_pgm_resource_registers |= (S_00B84C_USER_SGPR(Value) << 32);
806   else if (ID == "compute_pgm_rsrc2_tgid_x_en")
807     Header.compute_pgm_resource_registers |= (S_00B84C_TGID_X_EN(Value) << 32);
808   else if (ID == "compute_pgm_rsrc2_tgid_y_en")
809     Header.compute_pgm_resource_registers |= (S_00B84C_TGID_Y_EN(Value) << 32);
810   else if (ID == "compute_pgm_rsrc2_tgid_z_en")
811     Header.compute_pgm_resource_registers |= (S_00B84C_TGID_Z_EN(Value) << 32);
812   else if (ID == "compute_pgm_rsrc2_tg_size_en")
813     Header.compute_pgm_resource_registers |= (S_00B84C_TG_SIZE_EN(Value) << 32);
814   else if (ID == "compute_pgm_rsrc2_tidig_comp_cnt")
815     Header.compute_pgm_resource_registers |=
816         (S_00B84C_TIDIG_COMP_CNT(Value) << 32);
817   else if (ID == "compute_pgm_rsrc2_excp_en_msb")
818     Header.compute_pgm_resource_registers |=
819         (S_00B84C_EXCP_EN_MSB(Value) << 32);
820   else if (ID == "compute_pgm_rsrc2_lds_size")
821     Header.compute_pgm_resource_registers |= (S_00B84C_LDS_SIZE(Value) << 32);
822   else if (ID == "compute_pgm_rsrc2_excp_en")
823     Header.compute_pgm_resource_registers |= (S_00B84C_EXCP_EN(Value) << 32);
824   else if (ID == "compute_pgm_resource_registers")
825     Header.compute_pgm_resource_registers = Value;
826   else if (ID == "enable_sgpr_private_segment_buffer")
827     Header.code_properties |=
828         (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER_SHIFT);
829   else if (ID == "enable_sgpr_dispatch_ptr")
830     Header.code_properties |=
831         (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR_SHIFT);
832   else if (ID == "enable_sgpr_queue_ptr")
833     Header.code_properties |=
834         (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR_SHIFT);
835   else if (ID == "enable_sgpr_kernarg_segment_ptr")
836     Header.code_properties |=
837         (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR_SHIFT);
838   else if (ID == "enable_sgpr_dispatch_id")
839     Header.code_properties |=
840         (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID_SHIFT);
841   else if (ID == "enable_sgpr_flat_scratch_init")
842     Header.code_properties |=
843         (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT_SHIFT);
844   else if (ID == "enable_sgpr_private_segment_size")
845     Header.code_properties |=
846         (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_SIZE_SHIFT);
847   else if (ID == "enable_sgpr_grid_workgroup_count_x")
848     Header.code_properties |=
849         (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X_SHIFT);
850   else if (ID == "enable_sgpr_grid_workgroup_count_y")
851     Header.code_properties |=
852         (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y_SHIFT);
853   else if (ID == "enable_sgpr_grid_workgroup_count_z")
854     Header.code_properties |=
855         (Value << AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z_SHIFT);
856   else if (ID == "enable_ordered_append_gds")
857     Header.code_properties |=
858         (Value << AMD_CODE_PROPERTY_ENABLE_ORDERED_APPEND_GDS_SHIFT);
859   else if (ID == "private_element_size")
860     Header.code_properties |=
861         (Value << AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE_SHIFT);
862   else if (ID == "is_ptr64")
863     Header.code_properties |=
864         (Value << AMD_CODE_PROPERTY_IS_PTR64_SHIFT);
865   else if (ID == "is_dynamic_callstack")
866     Header.code_properties |=
867         (Value << AMD_CODE_PROPERTY_IS_DYNAMIC_CALLSTACK_SHIFT);
868   else if (ID == "is_debug_enabled")
869     Header.code_properties |=
870         (Value << AMD_CODE_PROPERTY_IS_DEBUG_SUPPORTED_SHIFT);
871   else if (ID == "is_xnack_enabled")
872     Header.code_properties |=
873         (Value << AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED_SHIFT);
874   else if (ID == "workitem_private_segment_byte_size")
875     Header.workitem_private_segment_byte_size = Value;
876   else if (ID == "workgroup_group_segment_byte_size")
877     Header.workgroup_group_segment_byte_size = Value;
878   else if (ID == "gds_segment_byte_size")
879     Header.gds_segment_byte_size = Value;
880   else if (ID == "kernarg_segment_byte_size")
881     Header.kernarg_segment_byte_size = Value;
882   else if (ID == "workgroup_fbarrier_count")
883     Header.workgroup_fbarrier_count = Value;
884   else if (ID == "wavefront_sgpr_count")
885     Header.wavefront_sgpr_count = Value;
886   else if (ID == "workitem_vgpr_count")
887     Header.workitem_vgpr_count = Value;
888   else if (ID == "reserved_vgpr_first")
889     Header.reserved_vgpr_first = Value;
890   else if (ID == "reserved_vgpr_count")
891     Header.reserved_vgpr_count = Value;
892   else if (ID == "reserved_sgpr_first")
893     Header.reserved_sgpr_first = Value;
894   else if (ID == "reserved_sgpr_count")
895     Header.reserved_sgpr_count = Value;
896   else if (ID == "debug_wavefront_private_segment_offset_sgpr")
897     Header.debug_wavefront_private_segment_offset_sgpr = Value;
898   else if (ID == "debug_private_segment_buffer_sgpr")
899     Header.debug_private_segment_buffer_sgpr = Value;
900   else if (ID == "kernarg_segment_alignment")
901     Header.kernarg_segment_alignment = Value;
902   else if (ID == "group_segment_alignment")
903     Header.group_segment_alignment = Value;
904   else if (ID == "private_segment_alignment")
905     Header.private_segment_alignment = Value;
906   else if (ID == "wavefront_size")
907     Header.wavefront_size = Value;
908   else if (ID == "call_convention")
909     Header.call_convention = Value;
910   else if (ID == "runtime_loader_kernel_symbol")
911     Header.runtime_loader_kernel_symbol = Value;
912   else
913     return TokError("amd_kernel_code_t value not recognized.");
914
915   return false;
916 }
917
918 bool AMDGPUAsmParser::ParseDirectiveAMDKernelCodeT() {
919
920   amd_kernel_code_t Header;
921   AMDGPU::initDefaultAMDKernelCodeT(Header, getSTI().getFeatureBits());
922
923   while (true) {
924
925     if (getLexer().isNot(AsmToken::EndOfStatement))
926       return TokError("amd_kernel_code_t values must begin on a new line");
927
928     // Lex EndOfStatement.  This is in a while loop, because lexing a comment
929     // will set the current token to EndOfStatement.
930     while(getLexer().is(AsmToken::EndOfStatement))
931       Lex();
932
933     if (getLexer().isNot(AsmToken::Identifier))
934       return TokError("expected value identifier or .end_amd_kernel_code_t");
935
936     StringRef ID = getLexer().getTok().getIdentifier();
937     Lex();
938
939     if (ID == ".end_amd_kernel_code_t")
940       break;
941
942     if (ParseAMDKernelCodeTValue(ID, Header))
943       return true;
944   }
945
946   getTargetStreamer().EmitAMDKernelCodeT(Header);
947
948   return false;
949 }
950
951 bool AMDGPUAsmParser::ParseSectionDirectiveHSAText() {
952   getParser().getStreamer().SwitchSection(
953       AMDGPU::getHSATextSection(getContext()));
954   return false;
955 }
956
957 bool AMDGPUAsmParser::ParseDirectiveAMDGPUHsaKernel() {
958   if (getLexer().isNot(AsmToken::Identifier))
959     return TokError("expected symbol name");
960
961   StringRef KernelName = Parser.getTok().getString();
962
963   getTargetStreamer().EmitAMDGPUSymbolType(KernelName,
964                                            ELF::STT_AMDGPU_HSA_KERNEL);
965   Lex();
966   return false;
967 }
968
969 bool AMDGPUAsmParser::ParseDirective(AsmToken DirectiveID) {
970   StringRef IDVal = DirectiveID.getString();
971
972   if (IDVal == ".hsa_code_object_version")
973     return ParseDirectiveHSACodeObjectVersion();
974
975   if (IDVal == ".hsa_code_object_isa")
976     return ParseDirectiveHSACodeObjectISA();
977
978   if (IDVal == ".amd_kernel_code_t")
979     return ParseDirectiveAMDKernelCodeT();
980
981   if (IDVal == ".hsatext" || IDVal == ".text")
982     return ParseSectionDirectiveHSAText();
983
984   if (IDVal == ".amdgpu_hsa_kernel")
985     return ParseDirectiveAMDGPUHsaKernel();
986
987   return true;
988 }
989
990 bool AMDGPUAsmParser::subtargetHasRegister(const MCRegisterInfo &MRI,
991                                            unsigned RegNo) const {
992   if (isCI())
993     return true;
994
995   if (isSI()) {
996     // No flat_scr
997     switch (RegNo) {
998     case AMDGPU::FLAT_SCR:
999     case AMDGPU::FLAT_SCR_LO:
1000     case AMDGPU::FLAT_SCR_HI:
1001       return false;
1002     default:
1003       return true;
1004     }
1005   }
1006
1007   // VI only has 102 SGPRs, so make sure we aren't trying to use the 2 more that
1008   // SI/CI have.
1009   for (MCRegAliasIterator R(AMDGPU::SGPR102_SGPR103, &MRI, true);
1010        R.isValid(); ++R) {
1011     if (*R == RegNo)
1012       return false;
1013   }
1014
1015   return true;
1016 }
1017
1018 static bool operandsHaveModifiers(const OperandVector &Operands) {
1019
1020   for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
1021     const AMDGPUOperand &Op = ((AMDGPUOperand&)*Operands[i]);
1022     if (Op.isRegKind() && Op.hasModifiers())
1023       return true;
1024     if (Op.isImm() && (Op.getImmTy() == AMDGPUOperand::ImmTyOMod ||
1025                        Op.getImmTy() == AMDGPUOperand::ImmTyClamp))
1026       return true;
1027   }
1028   return false;
1029 }
1030
1031 AMDGPUAsmParser::OperandMatchResultTy
1032 AMDGPUAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
1033
1034   // Try to parse with a custom parser
1035   OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1036
1037   // If we successfully parsed the operand or if there as an error parsing,
1038   // we are done.
1039   //
1040   // If we are parsing after we reach EndOfStatement then this means we
1041   // are appending default values to the Operands list.  This is only done
1042   // by custom parser, so we shouldn't continue on to the generic parsing.
1043   if (ResTy == MatchOperand_Success || ResTy == MatchOperand_ParseFail ||
1044       getLexer().is(AsmToken::EndOfStatement))
1045     return ResTy;
1046
1047   bool Negate = false, Abs = false;
1048   if (getLexer().getKind()== AsmToken::Minus) {
1049     Parser.Lex();
1050     Negate = true;
1051   }
1052
1053   if (getLexer().getKind() == AsmToken::Pipe) {
1054     Parser.Lex();
1055     Abs = true;
1056   }
1057
1058   switch(getLexer().getKind()) {
1059     case AsmToken::Integer: {
1060       SMLoc S = Parser.getTok().getLoc();
1061       int64_t IntVal;
1062       if (getParser().parseAbsoluteExpression(IntVal))
1063         return MatchOperand_ParseFail;
1064       if (!isInt<32>(IntVal) && !isUInt<32>(IntVal)) {
1065         Error(S, "invalid immediate: only 32-bit values are legal");
1066         return MatchOperand_ParseFail;
1067       }
1068
1069       if (Negate)
1070         IntVal *= -1;
1071       Operands.push_back(AMDGPUOperand::CreateImm(IntVal, S));
1072       return MatchOperand_Success;
1073     }
1074     case AsmToken::Real: {
1075       // FIXME: We should emit an error if a double precisions floating-point
1076       // value is used.  I'm not sure the best way to detect this.
1077       SMLoc S = Parser.getTok().getLoc();
1078       int64_t IntVal;
1079       if (getParser().parseAbsoluteExpression(IntVal))
1080         return MatchOperand_ParseFail;
1081
1082       APFloat F((float)BitsToDouble(IntVal));
1083       if (Negate)
1084         F.changeSign();
1085       Operands.push_back(
1086           AMDGPUOperand::CreateImm(F.bitcastToAPInt().getZExtValue(), S));
1087       return MatchOperand_Success;
1088     }
1089     case AsmToken::Identifier: {
1090       SMLoc S, E;
1091       unsigned RegNo;
1092       if (!ParseRegister(RegNo, S, E)) {
1093
1094         bool HasModifiers = operandsHaveModifiers(Operands);
1095         unsigned Modifiers = 0;
1096
1097         if (Negate)
1098           Modifiers |= 0x1;
1099
1100         if (Abs) {
1101           if (getLexer().getKind() != AsmToken::Pipe)
1102             return MatchOperand_ParseFail;
1103           Parser.Lex();
1104           Modifiers |= 0x2;
1105         }
1106
1107         if (Modifiers && !HasModifiers) {
1108           // We are adding a modifier to src1 or src2 and previous sources
1109           // don't have modifiers, so we need to go back and empty modifers
1110           // for each previous source.
1111           for (unsigned PrevRegIdx = Operands.size() - 1; PrevRegIdx > 1;
1112                --PrevRegIdx) {
1113
1114             AMDGPUOperand &RegOp = ((AMDGPUOperand&)*Operands[PrevRegIdx]);
1115             RegOp.setModifiers(0);
1116           }
1117         }
1118
1119
1120         Operands.push_back(AMDGPUOperand::CreateReg(
1121             RegNo, S, E, getContext().getRegisterInfo(),
1122             isForcedVOP3()));
1123
1124         if (HasModifiers || Modifiers) {
1125           AMDGPUOperand &RegOp = ((AMDGPUOperand&)*Operands[Operands.size() - 1]);
1126           RegOp.setModifiers(Modifiers);
1127
1128         }
1129      }  else {
1130       Operands.push_back(AMDGPUOperand::CreateToken(Parser.getTok().getString(),
1131                                                     S));
1132       Parser.Lex();
1133      }
1134      return MatchOperand_Success;
1135     }
1136     default:
1137       return MatchOperand_NoMatch;
1138   }
1139 }
1140
1141 bool AMDGPUAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1142                                        StringRef Name,
1143                                        SMLoc NameLoc, OperandVector &Operands) {
1144
1145   // Clear any forced encodings from the previous instruction.
1146   setForcedEncodingSize(0);
1147
1148   if (Name.endswith("_e64"))
1149     setForcedEncodingSize(64);
1150   else if (Name.endswith("_e32"))
1151     setForcedEncodingSize(32);
1152
1153   // Add the instruction mnemonic
1154   Operands.push_back(AMDGPUOperand::CreateToken(Name, NameLoc));
1155
1156   while (!getLexer().is(AsmToken::EndOfStatement)) {
1157     AMDGPUAsmParser::OperandMatchResultTy Res = parseOperand(Operands, Name);
1158
1159     // Eat the comma or space if there is one.
1160     if (getLexer().is(AsmToken::Comma))
1161       Parser.Lex();
1162
1163     switch (Res) {
1164       case MatchOperand_Success: break;
1165       case MatchOperand_ParseFail: return Error(getLexer().getLoc(),
1166                                                 "failed parsing operand.");
1167       case MatchOperand_NoMatch: return Error(getLexer().getLoc(),
1168                                               "not a valid operand.");
1169     }
1170   }
1171
1172   // Once we reach end of statement, continue parsing so we can add default
1173   // values for optional arguments.
1174   AMDGPUAsmParser::OperandMatchResultTy Res;
1175   while ((Res = parseOperand(Operands, Name)) != MatchOperand_NoMatch) {
1176     if (Res != MatchOperand_Success)
1177       return Error(getLexer().getLoc(), "failed parsing operand.");
1178   }
1179   return false;
1180 }
1181
1182 //===----------------------------------------------------------------------===//
1183 // Utility functions
1184 //===----------------------------------------------------------------------===//
1185
1186 AMDGPUAsmParser::OperandMatchResultTy
1187 AMDGPUAsmParser::parseIntWithPrefix(const char *Prefix, int64_t &Int,
1188                                     int64_t Default) {
1189
1190   // We are at the end of the statement, and this is a default argument, so
1191   // use a default value.
1192   if (getLexer().is(AsmToken::EndOfStatement)) {
1193     Int = Default;
1194     return MatchOperand_Success;
1195   }
1196
1197   switch(getLexer().getKind()) {
1198     default: return MatchOperand_NoMatch;
1199     case AsmToken::Identifier: {
1200       StringRef OffsetName = Parser.getTok().getString();
1201       if (!OffsetName.equals(Prefix))
1202         return MatchOperand_NoMatch;
1203
1204       Parser.Lex();
1205       if (getLexer().isNot(AsmToken::Colon))
1206         return MatchOperand_ParseFail;
1207
1208       Parser.Lex();
1209       if (getLexer().isNot(AsmToken::Integer))
1210         return MatchOperand_ParseFail;
1211
1212       if (getParser().parseAbsoluteExpression(Int))
1213         return MatchOperand_ParseFail;
1214       break;
1215     }
1216   }
1217   return MatchOperand_Success;
1218 }
1219
1220 AMDGPUAsmParser::OperandMatchResultTy
1221 AMDGPUAsmParser::parseIntWithPrefix(const char *Prefix, OperandVector &Operands,
1222                                     enum AMDGPUOperand::ImmTy ImmTy) {
1223
1224   SMLoc S = Parser.getTok().getLoc();
1225   int64_t Offset = 0;
1226
1227   AMDGPUAsmParser::OperandMatchResultTy Res = parseIntWithPrefix(Prefix, Offset);
1228   if (Res != MatchOperand_Success)
1229     return Res;
1230
1231   Operands.push_back(AMDGPUOperand::CreateImm(Offset, S, ImmTy));
1232   return MatchOperand_Success;
1233 }
1234
1235 AMDGPUAsmParser::OperandMatchResultTy
1236 AMDGPUAsmParser::parseNamedBit(const char *Name, OperandVector &Operands,
1237                                enum AMDGPUOperand::ImmTy ImmTy) {
1238   int64_t Bit = 0;
1239   SMLoc S = Parser.getTok().getLoc();
1240
1241   // We are at the end of the statement, and this is a default argument, so
1242   // use a default value.
1243   if (getLexer().isNot(AsmToken::EndOfStatement)) {
1244     switch(getLexer().getKind()) {
1245       case AsmToken::Identifier: {
1246         StringRef Tok = Parser.getTok().getString();
1247         if (Tok == Name) {
1248           Bit = 1;
1249           Parser.Lex();
1250         } else if (Tok.startswith("no") && Tok.endswith(Name)) {
1251           Bit = 0;
1252           Parser.Lex();
1253         } else {
1254           return MatchOperand_NoMatch;
1255         }
1256         break;
1257       }
1258       default:
1259         return MatchOperand_NoMatch;
1260     }
1261   }
1262
1263   Operands.push_back(AMDGPUOperand::CreateImm(Bit, S, ImmTy));
1264   return MatchOperand_Success;
1265 }
1266
1267 static bool operandsHasOptionalOp(const OperandVector &Operands,
1268                                   const OptionalOperand &OOp) {
1269   for (unsigned i = 0; i < Operands.size(); i++) {
1270     const AMDGPUOperand &ParsedOp = ((const AMDGPUOperand &)*Operands[i]);
1271     if ((ParsedOp.isImm() && ParsedOp.getImmTy() == OOp.Type) ||
1272         (ParsedOp.isToken() && ParsedOp.getToken() == OOp.Name))
1273       return true;
1274
1275   }
1276   return false;
1277 }
1278
1279 AMDGPUAsmParser::OperandMatchResultTy
1280 AMDGPUAsmParser::parseOptionalOps(const ArrayRef<OptionalOperand> &OptionalOps,
1281                                    OperandVector &Operands) {
1282   SMLoc S = Parser.getTok().getLoc();
1283   for (const OptionalOperand &Op : OptionalOps) {
1284     if (operandsHasOptionalOp(Operands, Op))
1285       continue;
1286     AMDGPUAsmParser::OperandMatchResultTy Res;
1287     int64_t Value;
1288     if (Op.IsBit) {
1289       Res = parseNamedBit(Op.Name, Operands, Op.Type);
1290       if (Res == MatchOperand_NoMatch)
1291         continue;
1292       return Res;
1293     }
1294
1295     Res = parseIntWithPrefix(Op.Name, Value, Op.Default);
1296
1297     if (Res == MatchOperand_NoMatch)
1298       continue;
1299
1300     if (Res != MatchOperand_Success)
1301       return Res;
1302
1303     if (Op.ConvertResult && !Op.ConvertResult(Value)) {
1304       return MatchOperand_ParseFail;
1305     }
1306
1307     Operands.push_back(AMDGPUOperand::CreateImm(Value, S, Op.Type));
1308     return MatchOperand_Success;
1309   }
1310   return MatchOperand_NoMatch;
1311 }
1312
1313 //===----------------------------------------------------------------------===//
1314 // ds
1315 //===----------------------------------------------------------------------===//
1316
1317 static const OptionalOperand DSOptionalOps [] = {
1318   {"offset",  AMDGPUOperand::ImmTyOffset, false, 0, nullptr},
1319   {"gds",     AMDGPUOperand::ImmTyGDS, true, 0, nullptr}
1320 };
1321
1322 static const OptionalOperand DSOptionalOpsOff01 [] = {
1323   {"offset0", AMDGPUOperand::ImmTyDSOffset0, false, 0, nullptr},
1324   {"offset1", AMDGPUOperand::ImmTyDSOffset1, false, 0, nullptr},
1325   {"gds",     AMDGPUOperand::ImmTyGDS, true, 0, nullptr}
1326 };
1327
1328 AMDGPUAsmParser::OperandMatchResultTy
1329 AMDGPUAsmParser::parseDSOptionalOps(OperandVector &Operands) {
1330   return parseOptionalOps(DSOptionalOps, Operands);
1331 }
1332 AMDGPUAsmParser::OperandMatchResultTy
1333 AMDGPUAsmParser::parseDSOff01OptionalOps(OperandVector &Operands) {
1334   return parseOptionalOps(DSOptionalOpsOff01, Operands);
1335 }
1336
1337 AMDGPUAsmParser::OperandMatchResultTy
1338 AMDGPUAsmParser::parseDSOffsetOptional(OperandVector &Operands) {
1339   SMLoc S = Parser.getTok().getLoc();
1340   AMDGPUAsmParser::OperandMatchResultTy Res =
1341     parseIntWithPrefix("offset", Operands, AMDGPUOperand::ImmTyOffset);
1342   if (Res == MatchOperand_NoMatch) {
1343     Operands.push_back(AMDGPUOperand::CreateImm(0, S,
1344                        AMDGPUOperand::ImmTyOffset));
1345     Res = MatchOperand_Success;
1346   }
1347   return Res;
1348 }
1349
1350 bool AMDGPUOperand::isDSOffset() const {
1351   return isImm() && isUInt<16>(getImm());
1352 }
1353
1354 bool AMDGPUOperand::isDSOffset01() const {
1355   return isImm() && isUInt<8>(getImm());
1356 }
1357
1358 void AMDGPUAsmParser::cvtDSOffset01(MCInst &Inst,
1359                                     const OperandVector &Operands) {
1360
1361   std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1362
1363   for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1364     AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1365
1366     // Add the register arguments
1367     if (Op.isReg()) {
1368       Op.addRegOperands(Inst, 1);
1369       continue;
1370     }
1371
1372     // Handle optional arguments
1373     OptionalIdx[Op.getImmTy()] = i;
1374   }
1375
1376   unsigned Offset0Idx = OptionalIdx[AMDGPUOperand::ImmTyDSOffset0];
1377   unsigned Offset1Idx = OptionalIdx[AMDGPUOperand::ImmTyDSOffset1];
1378   unsigned GDSIdx = OptionalIdx[AMDGPUOperand::ImmTyGDS];
1379
1380   ((AMDGPUOperand &)*Operands[Offset0Idx]).addImmOperands(Inst, 1); // offset0
1381   ((AMDGPUOperand &)*Operands[Offset1Idx]).addImmOperands(Inst, 1); // offset1
1382   ((AMDGPUOperand &)*Operands[GDSIdx]).addImmOperands(Inst, 1); // gds
1383   Inst.addOperand(MCOperand::createReg(AMDGPU::M0)); // m0
1384 }
1385
1386 void AMDGPUAsmParser::cvtDS(MCInst &Inst, const OperandVector &Operands) {
1387
1388   std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1389   bool GDSOnly = false;
1390
1391   for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1392     AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1393
1394     // Add the register arguments
1395     if (Op.isReg()) {
1396       Op.addRegOperands(Inst, 1);
1397       continue;
1398     }
1399
1400     if (Op.isToken() && Op.getToken() == "gds") {
1401       GDSOnly = true;
1402       continue;
1403     }
1404
1405     // Handle optional arguments
1406     OptionalIdx[Op.getImmTy()] = i;
1407   }
1408
1409   unsigned OffsetIdx = OptionalIdx[AMDGPUOperand::ImmTyOffset];
1410   ((AMDGPUOperand &)*Operands[OffsetIdx]).addImmOperands(Inst, 1); // offset
1411
1412   if (!GDSOnly) {
1413     unsigned GDSIdx = OptionalIdx[AMDGPUOperand::ImmTyGDS];
1414     ((AMDGPUOperand &)*Operands[GDSIdx]).addImmOperands(Inst, 1); // gds
1415   }
1416   Inst.addOperand(MCOperand::createReg(AMDGPU::M0)); // m0
1417 }
1418
1419
1420 //===----------------------------------------------------------------------===//
1421 // s_waitcnt
1422 //===----------------------------------------------------------------------===//
1423
1424 bool AMDGPUAsmParser::parseCnt(int64_t &IntVal) {
1425   StringRef CntName = Parser.getTok().getString();
1426   int64_t CntVal;
1427
1428   Parser.Lex();
1429   if (getLexer().isNot(AsmToken::LParen))
1430     return true;
1431
1432   Parser.Lex();
1433   if (getLexer().isNot(AsmToken::Integer))
1434     return true;
1435
1436   if (getParser().parseAbsoluteExpression(CntVal))
1437     return true;
1438
1439   if (getLexer().isNot(AsmToken::RParen))
1440     return true;
1441
1442   Parser.Lex();
1443   if (getLexer().is(AsmToken::Amp) || getLexer().is(AsmToken::Comma))
1444     Parser.Lex();
1445
1446   int CntShift;
1447   int CntMask;
1448
1449   if (CntName == "vmcnt") {
1450     CntMask = 0xf;
1451     CntShift = 0;
1452   } else if (CntName == "expcnt") {
1453     CntMask = 0x7;
1454     CntShift = 4;
1455   } else if (CntName == "lgkmcnt") {
1456     CntMask = 0x7;
1457     CntShift = 8;
1458   } else {
1459     return true;
1460   }
1461
1462   IntVal &= ~(CntMask << CntShift);
1463   IntVal |= (CntVal << CntShift);
1464   return false;
1465 }
1466
1467 AMDGPUAsmParser::OperandMatchResultTy
1468 AMDGPUAsmParser::parseSWaitCntOps(OperandVector &Operands) {
1469   // Disable all counters by default.
1470   // vmcnt   [3:0]
1471   // expcnt  [6:4]
1472   // lgkmcnt [10:8]
1473   int64_t CntVal = 0x77f;
1474   SMLoc S = Parser.getTok().getLoc();
1475
1476   switch(getLexer().getKind()) {
1477     default: return MatchOperand_ParseFail;
1478     case AsmToken::Integer:
1479       // The operand can be an integer value.
1480       if (getParser().parseAbsoluteExpression(CntVal))
1481         return MatchOperand_ParseFail;
1482       break;
1483
1484     case AsmToken::Identifier:
1485       do {
1486         if (parseCnt(CntVal))
1487           return MatchOperand_ParseFail;
1488       } while(getLexer().isNot(AsmToken::EndOfStatement));
1489       break;
1490   }
1491   Operands.push_back(AMDGPUOperand::CreateImm(CntVal, S));
1492   return MatchOperand_Success;
1493 }
1494
1495 bool AMDGPUOperand::isSWaitCnt() const {
1496   return isImm();
1497 }
1498
1499 //===----------------------------------------------------------------------===//
1500 // sopp branch targets
1501 //===----------------------------------------------------------------------===//
1502
1503 AMDGPUAsmParser::OperandMatchResultTy
1504 AMDGPUAsmParser::parseSOppBrTarget(OperandVector &Operands) {
1505   SMLoc S = Parser.getTok().getLoc();
1506
1507   switch (getLexer().getKind()) {
1508     default: return MatchOperand_ParseFail;
1509     case AsmToken::Integer: {
1510       int64_t Imm;
1511       if (getParser().parseAbsoluteExpression(Imm))
1512         return MatchOperand_ParseFail;
1513       Operands.push_back(AMDGPUOperand::CreateImm(Imm, S));
1514       return MatchOperand_Success;
1515     }
1516
1517     case AsmToken::Identifier:
1518       Operands.push_back(AMDGPUOperand::CreateExpr(
1519           MCSymbolRefExpr::create(getContext().getOrCreateSymbol(
1520                                   Parser.getTok().getString()), getContext()), S));
1521       Parser.Lex();
1522       return MatchOperand_Success;
1523   }
1524 }
1525
1526 //===----------------------------------------------------------------------===//
1527 // flat
1528 //===----------------------------------------------------------------------===//
1529
1530 static const OptionalOperand FlatOptionalOps [] = {
1531   {"glc",    AMDGPUOperand::ImmTyGLC, true, 0, nullptr},
1532   {"slc",    AMDGPUOperand::ImmTySLC, true, 0, nullptr},
1533   {"tfe",    AMDGPUOperand::ImmTyTFE, true, 0, nullptr}
1534 };
1535
1536 static const OptionalOperand FlatAtomicOptionalOps [] = {
1537   {"slc",    AMDGPUOperand::ImmTySLC, true, 0, nullptr},
1538   {"tfe",    AMDGPUOperand::ImmTyTFE, true, 0, nullptr}
1539 };
1540
1541 AMDGPUAsmParser::OperandMatchResultTy
1542 AMDGPUAsmParser::parseFlatOptionalOps(OperandVector &Operands) {
1543   return parseOptionalOps(FlatOptionalOps, Operands);
1544 }
1545
1546 AMDGPUAsmParser::OperandMatchResultTy
1547 AMDGPUAsmParser::parseFlatAtomicOptionalOps(OperandVector &Operands) {
1548   return parseOptionalOps(FlatAtomicOptionalOps, Operands);
1549 }
1550
1551 void AMDGPUAsmParser::cvtFlat(MCInst &Inst,
1552                                const OperandVector &Operands) {
1553   std::map<AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1554
1555   for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1556     AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1557
1558     // Add the register arguments
1559     if (Op.isReg()) {
1560       Op.addRegOperands(Inst, 1);
1561       continue;
1562     }
1563
1564     // Handle 'glc' token which is sometimes hard-coded into the
1565     // asm string.  There are no MCInst operands for these.
1566     if (Op.isToken())
1567       continue;
1568
1569     // Handle optional arguments
1570     OptionalIdx[Op.getImmTy()] = i;
1571
1572   }
1573
1574   // flat atomic instructions don't have a glc argument.
1575   if (OptionalIdx.count(AMDGPUOperand::ImmTyGLC)) {
1576     unsigned GLCIdx = OptionalIdx[AMDGPUOperand::ImmTyGLC];
1577     ((AMDGPUOperand &)*Operands[GLCIdx]).addImmOperands(Inst, 1);
1578   }
1579
1580   unsigned SLCIdx = OptionalIdx[AMDGPUOperand::ImmTySLC];
1581   unsigned TFEIdx = OptionalIdx[AMDGPUOperand::ImmTyTFE];
1582
1583   ((AMDGPUOperand &)*Operands[SLCIdx]).addImmOperands(Inst, 1);
1584   ((AMDGPUOperand &)*Operands[TFEIdx]).addImmOperands(Inst, 1);
1585 }
1586
1587 //===----------------------------------------------------------------------===//
1588 // mubuf
1589 //===----------------------------------------------------------------------===//
1590
1591 static const OptionalOperand MubufOptionalOps [] = {
1592   {"offset", AMDGPUOperand::ImmTyOffset, false, 0, nullptr},
1593   {"glc",    AMDGPUOperand::ImmTyGLC, true, 0, nullptr},
1594   {"slc",    AMDGPUOperand::ImmTySLC, true, 0, nullptr},
1595   {"tfe",    AMDGPUOperand::ImmTyTFE, true, 0, nullptr}
1596 };
1597
1598 AMDGPUAsmParser::OperandMatchResultTy
1599 AMDGPUAsmParser::parseMubufOptionalOps(OperandVector &Operands) {
1600   return parseOptionalOps(MubufOptionalOps, Operands);
1601 }
1602
1603 AMDGPUAsmParser::OperandMatchResultTy
1604 AMDGPUAsmParser::parseOffset(OperandVector &Operands) {
1605   return parseIntWithPrefix("offset", Operands);
1606 }
1607
1608 AMDGPUAsmParser::OperandMatchResultTy
1609 AMDGPUAsmParser::parseGLC(OperandVector &Operands) {
1610   return parseNamedBit("glc", Operands);
1611 }
1612
1613 AMDGPUAsmParser::OperandMatchResultTy
1614 AMDGPUAsmParser::parseSLC(OperandVector &Operands) {
1615   return parseNamedBit("slc", Operands);
1616 }
1617
1618 AMDGPUAsmParser::OperandMatchResultTy
1619 AMDGPUAsmParser::parseTFE(OperandVector &Operands) {
1620   return parseNamedBit("tfe", Operands);
1621 }
1622
1623 bool AMDGPUOperand::isMubufOffset() const {
1624   return isImm() && isUInt<12>(getImm());
1625 }
1626
1627 void AMDGPUAsmParser::cvtMubuf(MCInst &Inst,
1628                                const OperandVector &Operands) {
1629   std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1630
1631   for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1632     AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1633
1634     // Add the register arguments
1635     if (Op.isReg()) {
1636       Op.addRegOperands(Inst, 1);
1637       continue;
1638     }
1639
1640     // Handle the case where soffset is an immediate
1641     if (Op.isImm() && Op.getImmTy() == AMDGPUOperand::ImmTyNone) {
1642       Op.addImmOperands(Inst, 1);
1643       continue;
1644     }
1645
1646     // Handle tokens like 'offen' which are sometimes hard-coded into the
1647     // asm string.  There are no MCInst operands for these.
1648     if (Op.isToken()) {
1649       continue;
1650     }
1651     assert(Op.isImm());
1652
1653     // Handle optional arguments
1654     OptionalIdx[Op.getImmTy()] = i;
1655   }
1656
1657   assert(OptionalIdx.size() == 4);
1658
1659   unsigned OffsetIdx = OptionalIdx[AMDGPUOperand::ImmTyOffset];
1660   unsigned GLCIdx = OptionalIdx[AMDGPUOperand::ImmTyGLC];
1661   unsigned SLCIdx = OptionalIdx[AMDGPUOperand::ImmTySLC];
1662   unsigned TFEIdx = OptionalIdx[AMDGPUOperand::ImmTyTFE];
1663
1664   ((AMDGPUOperand &)*Operands[OffsetIdx]).addImmOperands(Inst, 1);
1665   ((AMDGPUOperand &)*Operands[GLCIdx]).addImmOperands(Inst, 1);
1666   ((AMDGPUOperand &)*Operands[SLCIdx]).addImmOperands(Inst, 1);
1667   ((AMDGPUOperand &)*Operands[TFEIdx]).addImmOperands(Inst, 1);
1668 }
1669
1670 //===----------------------------------------------------------------------===//
1671 // mimg
1672 //===----------------------------------------------------------------------===//
1673
1674 AMDGPUAsmParser::OperandMatchResultTy
1675 AMDGPUAsmParser::parseDMask(OperandVector &Operands) {
1676   return parseIntWithPrefix("dmask", Operands);
1677 }
1678
1679 AMDGPUAsmParser::OperandMatchResultTy
1680 AMDGPUAsmParser::parseUNorm(OperandVector &Operands) {
1681   return parseNamedBit("unorm", Operands);
1682 }
1683
1684 AMDGPUAsmParser::OperandMatchResultTy
1685 AMDGPUAsmParser::parseR128(OperandVector &Operands) {
1686   return parseNamedBit("r128", Operands);
1687 }
1688
1689 //===----------------------------------------------------------------------===//
1690 // smrd
1691 //===----------------------------------------------------------------------===//
1692
1693 bool AMDGPUOperand::isSMRDOffset() const {
1694
1695   // FIXME: Support 20-bit offsets on VI.  We need to to pass subtarget
1696   // information here.
1697   return isImm() && isUInt<8>(getImm());
1698 }
1699
1700 bool AMDGPUOperand::isSMRDLiteralOffset() const {
1701   // 32-bit literals are only supported on CI and we only want to use them
1702   // when the offset is > 8-bits.
1703   return isImm() && !isUInt<8>(getImm()) && isUInt<32>(getImm());
1704 }
1705
1706 //===----------------------------------------------------------------------===//
1707 // vop3
1708 //===----------------------------------------------------------------------===//
1709
1710 static bool ConvertOmodMul(int64_t &Mul) {
1711   if (Mul != 1 && Mul != 2 && Mul != 4)
1712     return false;
1713
1714   Mul >>= 1;
1715   return true;
1716 }
1717
1718 static bool ConvertOmodDiv(int64_t &Div) {
1719   if (Div == 1) {
1720     Div = 0;
1721     return true;
1722   }
1723
1724   if (Div == 2) {
1725     Div = 3;
1726     return true;
1727   }
1728
1729   return false;
1730 }
1731
1732 static const OptionalOperand VOP3OptionalOps [] = {
1733   {"clamp", AMDGPUOperand::ImmTyClamp, true, 0, nullptr},
1734   {"mul",   AMDGPUOperand::ImmTyOMod, false, 1, ConvertOmodMul},
1735   {"div",   AMDGPUOperand::ImmTyOMod, false, 1, ConvertOmodDiv},
1736 };
1737
1738 static bool isVOP3(OperandVector &Operands) {
1739   if (operandsHaveModifiers(Operands))
1740     return true;
1741
1742   AMDGPUOperand &DstOp = ((AMDGPUOperand&)*Operands[1]);
1743
1744   if (DstOp.isReg() && DstOp.isRegClass(AMDGPU::SGPR_64RegClassID))
1745     return true;
1746
1747   if (Operands.size() >= 5)
1748     return true;
1749
1750   if (Operands.size() > 3) {
1751     AMDGPUOperand &Src1Op = ((AMDGPUOperand&)*Operands[3]);
1752     if (Src1Op.getReg() && (Src1Op.isRegClass(AMDGPU::SReg_32RegClassID) ||
1753                             Src1Op.isRegClass(AMDGPU::SReg_64RegClassID)))
1754       return true;
1755   }
1756   return false;
1757 }
1758
1759 AMDGPUAsmParser::OperandMatchResultTy
1760 AMDGPUAsmParser::parseVOP3OptionalOps(OperandVector &Operands) {
1761
1762   // The value returned by this function may change after parsing
1763   // an operand so store the original value here.
1764   bool HasModifiers = operandsHaveModifiers(Operands);
1765
1766   bool IsVOP3 = isVOP3(Operands);
1767   if (HasModifiers || IsVOP3 ||
1768       getLexer().isNot(AsmToken::EndOfStatement) ||
1769       getForcedEncodingSize() == 64) {
1770
1771     AMDGPUAsmParser::OperandMatchResultTy Res =
1772         parseOptionalOps(VOP3OptionalOps, Operands);
1773
1774     if (!HasModifiers && Res == MatchOperand_Success) {
1775       // We have added a modifier operation, so we need to make sure all
1776       // previous register operands have modifiers
1777       for (unsigned i = 2, e = Operands.size(); i != e; ++i) {
1778         AMDGPUOperand &Op = ((AMDGPUOperand&)*Operands[i]);
1779         if (Op.isReg())
1780           Op.setModifiers(0);
1781       }
1782     }
1783     return Res;
1784   }
1785   return MatchOperand_NoMatch;
1786 }
1787
1788 void AMDGPUAsmParser::cvtVOP3(MCInst &Inst, const OperandVector &Operands) {
1789
1790   unsigned i = 1;
1791   const MCInstrDesc &Desc = MII.get(Inst.getOpcode());
1792   if (Desc.getNumDefs() > 0) {
1793     ((AMDGPUOperand &)*Operands[i++]).addRegOperands(Inst, 1);
1794   }
1795
1796   std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1797
1798   if (operandsHaveModifiers(Operands)) {
1799     for (unsigned e = Operands.size(); i != e; ++i) {
1800       AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1801
1802       if (Op.isRegWithInputMods()) {
1803         ((AMDGPUOperand &)*Operands[i]).addRegWithInputModsOperands(Inst, 2);
1804         continue;
1805       }
1806       OptionalIdx[Op.getImmTy()] = i;
1807     }
1808
1809     unsigned ClampIdx = OptionalIdx[AMDGPUOperand::ImmTyClamp];
1810     unsigned OModIdx = OptionalIdx[AMDGPUOperand::ImmTyOMod];
1811
1812     ((AMDGPUOperand &)*Operands[ClampIdx]).addImmOperands(Inst, 1);
1813     ((AMDGPUOperand &)*Operands[OModIdx]).addImmOperands(Inst, 1);
1814   } else {
1815     for (unsigned e = Operands.size(); i != e; ++i)
1816       ((AMDGPUOperand &)*Operands[i]).addRegOrImmOperands(Inst, 1);
1817   }
1818 }
1819
1820 /// Force static initialization.
1821 extern "C" void LLVMInitializeAMDGPUAsmParser() {
1822   RegisterMCAsmParser<AMDGPUAsmParser> A(TheAMDGPUTarget);
1823   RegisterMCAsmParser<AMDGPUAsmParser> B(TheGCNTarget);
1824 }
1825
1826 #define GET_REGISTER_MATCHER
1827 #define GET_MATCHER_IMPLEMENTATION
1828 #include "AMDGPUGenAsmMatcher.inc"
1829