1 //===-- AMDGPUAsmParser.cpp - Parse SI asm to MCInst instructions ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
11 #include "MCTargetDesc/AMDGPUTargetStreamer.h"
12 #include "Utils/AMDGPUBaseInfo.h"
13 #include "SIDefines.h"
14 #include "llvm/ADT/APFloat.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCInstrInfo.h"
24 #include "llvm/MC/MCParser/MCAsmLexer.h"
25 #include "llvm/MC/MCParser/MCAsmParser.h"
26 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
27 #include "llvm/MC/MCRegisterInfo.h"
28 #include "llvm/MC/MCStreamer.h"
29 #include "llvm/MC/MCSubtargetInfo.h"
30 #include "llvm/MC/MCTargetAsmParser.h"
31 #include "llvm/Support/SourceMgr.h"
32 #include "llvm/Support/TargetRegistry.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Support/Debug.h"
40 struct OptionalOperand;
42 class AMDGPUOperand : public MCParsedAsmOperand {
50 SMLoc StartLoc, EndLoc;
53 AMDGPUOperand(enum KindTy K) : MCParsedAsmOperand(), Kind(K) {}
84 const MCRegisterInfo *TRI;
95 void addImmOperands(MCInst &Inst, unsigned N) const {
96 Inst.addOperand(MCOperand::createImm(getImm()));
99 StringRef getToken() const {
100 return StringRef(Tok.Data, Tok.Length);
103 void addRegOperands(MCInst &Inst, unsigned N) const {
104 Inst.addOperand(MCOperand::createReg(getReg()));
107 void addRegOrImmOperands(MCInst &Inst, unsigned N) const {
109 addRegOperands(Inst, N);
111 addImmOperands(Inst, N);
114 void addRegWithInputModsOperands(MCInst &Inst, unsigned N) const {
115 Inst.addOperand(MCOperand::createImm(
116 Reg.Modifiers == -1 ? 0 : Reg.Modifiers));
117 addRegOperands(Inst, N);
120 void addSoppBrTargetOperands(MCInst &Inst, unsigned N) const {
122 addImmOperands(Inst, N);
125 Inst.addOperand(MCOperand::createExpr(Expr));
129 bool defaultTokenHasSuffix() const {
130 StringRef Token(Tok.Data, Tok.Length);
132 return Token.endswith("_e32") || Token.endswith("_e64");
135 bool isToken() const override {
136 return Kind == Token;
139 bool isImm() const override {
140 return Kind == Immediate;
143 bool isInlineImm() const {
144 float F = BitsToFloat(Imm.Val);
145 // TODO: Add 0.5pi for VI
146 return isImm() && ((Imm.Val <= 64 && Imm.Val >= -16) ||
147 (F == 0.0 || F == 0.5 || F == -0.5 || F == 1.0 || F == -1.0 ||
148 F == 2.0 || F == -2.0 || F == 4.0 || F == -4.0));
151 bool isDSOffset0() const {
153 return Imm.Type == ImmTyDSOffset0;
156 bool isDSOffset1() const {
158 return Imm.Type == ImmTyDSOffset1;
161 int64_t getImm() const {
165 enum ImmTy getImmTy() const {
170 bool isRegKind() const {
171 return Kind == Register;
174 bool isReg() const override {
175 return Kind == Register && Reg.Modifiers == -1;
178 bool isRegWithInputMods() const {
179 return Kind == Register && (Reg.IsForcedVOP3 || Reg.Modifiers != -1);
182 void setModifiers(unsigned Mods) {
184 Reg.Modifiers = Mods;
187 bool hasModifiers() const {
189 return Reg.Modifiers != -1;
192 unsigned getReg() const override {
196 bool isRegOrImm() const {
197 return isReg() || isImm();
200 bool isRegClass(unsigned RCID) const {
201 return Reg.TRI->getRegClass(RCID).contains(getReg());
204 bool isSCSrc32() const {
205 return isInlineImm() || (isReg() && isRegClass(AMDGPU::SReg_32RegClassID));
208 bool isSSrc32() const {
209 return isImm() || (isReg() && isRegClass(AMDGPU::SReg_32RegClassID));
212 bool isSSrc64() const {
213 return isImm() || isInlineImm() ||
214 (isReg() && isRegClass(AMDGPU::SReg_64RegClassID));
217 bool isVCSrc32() const {
218 return isInlineImm() || (isReg() && isRegClass(AMDGPU::VS_32RegClassID));
221 bool isVCSrc64() const {
222 return isInlineImm() || (isReg() && isRegClass(AMDGPU::VS_64RegClassID));
225 bool isVSrc32() const {
226 return isImm() || (isReg() && isRegClass(AMDGPU::VS_32RegClassID));
229 bool isVSrc64() const {
230 return isImm() || (isReg() && isRegClass(AMDGPU::VS_64RegClassID));
233 bool isMem() const override {
237 bool isExpr() const {
238 return Kind == Expression;
241 bool isSoppBrTarget() const {
242 return isExpr() || isImm();
245 SMLoc getStartLoc() const override {
249 SMLoc getEndLoc() const override {
253 void print(raw_ostream &OS) const override { }
255 static std::unique_ptr<AMDGPUOperand> CreateImm(int64_t Val, SMLoc Loc,
256 enum ImmTy Type = ImmTyNone,
257 bool IsFPImm = false) {
258 auto Op = llvm::make_unique<AMDGPUOperand>(Immediate);
260 Op->Imm.IsFPImm = IsFPImm;
267 static std::unique_ptr<AMDGPUOperand> CreateToken(StringRef Str, SMLoc Loc,
268 bool HasExplicitEncodingSize = true) {
269 auto Res = llvm::make_unique<AMDGPUOperand>(Token);
270 Res->Tok.Data = Str.data();
271 Res->Tok.Length = Str.size();
277 static std::unique_ptr<AMDGPUOperand> CreateReg(unsigned RegNo, SMLoc S,
279 const MCRegisterInfo *TRI,
281 auto Op = llvm::make_unique<AMDGPUOperand>(Register);
282 Op->Reg.RegNo = RegNo;
284 Op->Reg.Modifiers = -1;
285 Op->Reg.IsForcedVOP3 = ForceVOP3;
291 static std::unique_ptr<AMDGPUOperand> CreateExpr(const class MCExpr *Expr, SMLoc S) {
292 auto Op = llvm::make_unique<AMDGPUOperand>(Expression);
299 bool isDSOffset() const;
300 bool isDSOffset01() const;
301 bool isSWaitCnt() const;
302 bool isMubufOffset() const;
305 class AMDGPUAsmParser : public MCTargetAsmParser {
306 MCSubtargetInfo &STI;
307 const MCInstrInfo &MII;
310 unsigned ForcedEncodingSize;
311 /// @name Auto-generated Match Functions
314 #define GET_ASSEMBLER_HEADER
315 #include "AMDGPUGenAsmMatcher.inc"
320 bool ParseDirectiveMajorMinor(uint32_t &Major, uint32_t &Minor);
321 bool ParseDirectiveHSACodeObjectVersion();
322 bool ParseDirectiveHSACodeObjectISA();
325 AMDGPUAsmParser(MCSubtargetInfo &STI, MCAsmParser &_Parser,
326 const MCInstrInfo &MII,
327 const MCTargetOptions &Options)
328 : MCTargetAsmParser(), STI(STI), MII(MII), Parser(_Parser),
329 ForcedEncodingSize(0){
331 if (STI.getFeatureBits().none()) {
332 // Set default features.
333 STI.ToggleFeature("SOUTHERN_ISLANDS");
336 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
339 AMDGPUTargetStreamer &getTargetStreamer() {
340 MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
341 return static_cast<AMDGPUTargetStreamer &>(TS);
344 unsigned getForcedEncodingSize() const {
345 return ForcedEncodingSize;
348 void setForcedEncodingSize(unsigned Size) {
349 ForcedEncodingSize = Size;
352 bool isForcedVOP3() const {
353 return ForcedEncodingSize == 64;
356 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
357 unsigned checkTargetMatchPredicate(MCInst &Inst) override;
358 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
359 OperandVector &Operands, MCStreamer &Out,
361 bool MatchingInlineAsm) override;
362 bool ParseDirective(AsmToken DirectiveID) override;
363 OperandMatchResultTy parseOperand(OperandVector &Operands, StringRef Mnemonic);
364 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
365 SMLoc NameLoc, OperandVector &Operands) override;
367 OperandMatchResultTy parseIntWithPrefix(const char *Prefix, int64_t &Int,
368 int64_t Default = 0);
369 OperandMatchResultTy parseIntWithPrefix(const char *Prefix,
370 OperandVector &Operands,
371 enum AMDGPUOperand::ImmTy ImmTy =
372 AMDGPUOperand::ImmTyNone);
373 OperandMatchResultTy parseNamedBit(const char *Name, OperandVector &Operands,
374 enum AMDGPUOperand::ImmTy ImmTy =
375 AMDGPUOperand::ImmTyNone);
376 OperandMatchResultTy parseOptionalOps(
377 const ArrayRef<OptionalOperand> &OptionalOps,
378 OperandVector &Operands);
381 void cvtDSOffset01(MCInst &Inst, const OperandVector &Operands);
382 void cvtDS(MCInst &Inst, const OperandVector &Operands);
383 OperandMatchResultTy parseDSOptionalOps(OperandVector &Operands);
384 OperandMatchResultTy parseDSOff01OptionalOps(OperandVector &Operands);
385 OperandMatchResultTy parseDSOffsetOptional(OperandVector &Operands);
387 bool parseCnt(int64_t &IntVal);
388 OperandMatchResultTy parseSWaitCntOps(OperandVector &Operands);
389 OperandMatchResultTy parseSOppBrTarget(OperandVector &Operands);
391 OperandMatchResultTy parseFlatOptionalOps(OperandVector &Operands);
392 OperandMatchResultTy parseFlatAtomicOptionalOps(OperandVector &Operands);
393 void cvtFlat(MCInst &Inst, const OperandVector &Operands);
395 void cvtMubuf(MCInst &Inst, const OperandVector &Operands);
396 OperandMatchResultTy parseOffset(OperandVector &Operands);
397 OperandMatchResultTy parseMubufOptionalOps(OperandVector &Operands);
398 OperandMatchResultTy parseGLC(OperandVector &Operands);
399 OperandMatchResultTy parseSLC(OperandVector &Operands);
400 OperandMatchResultTy parseTFE(OperandVector &Operands);
402 OperandMatchResultTy parseDMask(OperandVector &Operands);
403 OperandMatchResultTy parseUNorm(OperandVector &Operands);
404 OperandMatchResultTy parseR128(OperandVector &Operands);
406 void cvtVOP3(MCInst &Inst, const OperandVector &Operands);
407 OperandMatchResultTy parseVOP3OptionalOps(OperandVector &Operands);
410 struct OptionalOperand {
412 AMDGPUOperand::ImmTy Type;
415 bool (*ConvertResult)(int64_t&);
420 static unsigned getRegClass(bool IsVgpr, unsigned RegWidth) {
423 default: llvm_unreachable("Unknown register width");
424 case 1: return AMDGPU::VGPR_32RegClassID;
425 case 2: return AMDGPU::VReg_64RegClassID;
426 case 3: return AMDGPU::VReg_96RegClassID;
427 case 4: return AMDGPU::VReg_128RegClassID;
428 case 8: return AMDGPU::VReg_256RegClassID;
429 case 16: return AMDGPU::VReg_512RegClassID;
434 default: llvm_unreachable("Unknown register width");
435 case 1: return AMDGPU::SGPR_32RegClassID;
436 case 2: return AMDGPU::SGPR_64RegClassID;
437 case 4: return AMDGPU::SReg_128RegClassID;
438 case 8: return AMDGPU::SReg_256RegClassID;
439 case 16: return AMDGPU::SReg_512RegClassID;
443 static unsigned getRegForName(const StringRef &RegName) {
445 return StringSwitch<unsigned>(RegName)
446 .Case("exec", AMDGPU::EXEC)
447 .Case("vcc", AMDGPU::VCC)
448 .Case("flat_scr", AMDGPU::FLAT_SCR)
449 .Case("m0", AMDGPU::M0)
450 .Case("scc", AMDGPU::SCC)
451 .Case("flat_scr_lo", AMDGPU::FLAT_SCR_LO)
452 .Case("flat_scr_hi", AMDGPU::FLAT_SCR_HI)
453 .Case("vcc_lo", AMDGPU::VCC_LO)
454 .Case("vcc_hi", AMDGPU::VCC_HI)
455 .Case("exec_lo", AMDGPU::EXEC_LO)
456 .Case("exec_hi", AMDGPU::EXEC_HI)
460 bool AMDGPUAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) {
461 const AsmToken Tok = Parser.getTok();
462 StartLoc = Tok.getLoc();
463 EndLoc = Tok.getEndLoc();
464 const StringRef &RegName = Tok.getString();
465 RegNo = getRegForName(RegName);
472 // Match vgprs and sgprs
473 if (RegName[0] != 's' && RegName[0] != 'v')
476 bool IsVgpr = RegName[0] == 'v';
478 unsigned RegIndexInClass;
479 if (RegName.size() > 1) {
480 // We have a 32-bit register
482 if (RegName.substr(1).getAsInteger(10, RegIndexInClass))
486 // We have a register greater than 32-bits.
488 int64_t RegLo, RegHi;
490 if (getLexer().isNot(AsmToken::LBrac))
494 if (getParser().parseAbsoluteExpression(RegLo))
497 if (getLexer().isNot(AsmToken::Colon))
501 if (getParser().parseAbsoluteExpression(RegHi))
504 if (getLexer().isNot(AsmToken::RBrac))
508 RegWidth = (RegHi - RegLo) + 1;
510 // VGPR registers aren't aligned.
511 RegIndexInClass = RegLo;
513 // SGPR registers are aligned. Max alignment is 4 dwords.
514 RegIndexInClass = RegLo / std::min(RegWidth, 4u);
518 const MCRegisterInfo *TRC = getContext().getRegisterInfo();
519 unsigned RC = getRegClass(IsVgpr, RegWidth);
520 if (RegIndexInClass > TRC->getRegClass(RC).getNumRegs())
522 RegNo = TRC->getRegClass(RC).getRegister(RegIndexInClass);
526 unsigned AMDGPUAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
528 uint64_t TSFlags = MII.get(Inst.getOpcode()).TSFlags;
530 if ((getForcedEncodingSize() == 32 && (TSFlags & SIInstrFlags::VOP3)) ||
531 (getForcedEncodingSize() == 64 && !(TSFlags & SIInstrFlags::VOP3)))
532 return Match_InvalidOperand;
534 return Match_Success;
538 bool AMDGPUAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
539 OperandVector &Operands,
542 bool MatchingInlineAsm) {
545 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm)) {
549 Out.EmitInstruction(Inst, STI);
551 case Match_MissingFeature:
552 return Error(IDLoc, "instruction not supported on this GPU");
554 case Match_MnemonicFail:
555 return Error(IDLoc, "unrecognized instruction mnemonic");
557 case Match_InvalidOperand: {
558 SMLoc ErrorLoc = IDLoc;
559 if (ErrorInfo != ~0ULL) {
560 if (ErrorInfo >= Operands.size()) {
561 if (isForcedVOP3()) {
562 // If 64-bit encoding has been forced we can end up with no
563 // clamp or omod operands if none of the registers have modifiers,
564 // so we need to add these to the operand list.
565 AMDGPUOperand &LastOp =
566 ((AMDGPUOperand &)*Operands[Operands.size() - 1]);
567 if (LastOp.isRegKind() ||
569 LastOp.getImmTy() != AMDGPUOperand::ImmTyNone)) {
570 SMLoc S = Parser.getTok().getLoc();
571 Operands.push_back(AMDGPUOperand::CreateImm(0, S,
572 AMDGPUOperand::ImmTyClamp));
573 Operands.push_back(AMDGPUOperand::CreateImm(0, S,
574 AMDGPUOperand::ImmTyOMod));
575 bool Res = MatchAndEmitInstruction(IDLoc, Opcode, Operands,
583 return Error(IDLoc, "too few operands for instruction");
586 ErrorLoc = ((AMDGPUOperand &)*Operands[ErrorInfo]).getStartLoc();
587 if (ErrorLoc == SMLoc())
590 return Error(ErrorLoc, "invalid operand for instruction");
593 llvm_unreachable("Implement any new match types added!");
596 bool AMDGPUAsmParser::ParseDirectiveMajorMinor(uint32_t &Major,
598 if (getLexer().isNot(AsmToken::Integer))
599 return TokError("invalid major version");
601 Major = getLexer().getTok().getIntVal();
604 if (getLexer().isNot(AsmToken::Comma))
605 return TokError("minor version number required, comma expected");
608 if (getLexer().isNot(AsmToken::Integer))
609 return TokError("invalid minor version");
611 Minor = getLexer().getTok().getIntVal();
617 bool AMDGPUAsmParser::ParseDirectiveHSACodeObjectVersion() {
622 if (ParseDirectiveMajorMinor(Major, Minor))
625 getTargetStreamer().EmitDirectiveHSACodeObjectVersion(Major, Minor);
629 bool AMDGPUAsmParser::ParseDirectiveHSACodeObjectISA() {
634 StringRef VendorName;
637 // If this directive has no arguments, then use the ISA version for the
639 if (getLexer().is(AsmToken::EndOfStatement)) {
640 AMDGPU::IsaVersion Isa = AMDGPU::getIsaVersion(STI.getFeatureBits());
641 getTargetStreamer().EmitDirectiveHSACodeObjectISA(Isa.Major, Isa.Minor,
648 if (ParseDirectiveMajorMinor(Major, Minor))
651 if (getLexer().isNot(AsmToken::Comma))
652 return TokError("stepping version number required, comma expected");
655 if (getLexer().isNot(AsmToken::Integer))
656 return TokError("invalid stepping version");
658 Stepping = getLexer().getTok().getIntVal();
661 if (getLexer().isNot(AsmToken::Comma))
662 return TokError("vendor name required, comma expected");
665 if (getLexer().isNot(AsmToken::String))
666 return TokError("invalid vendor name");
668 VendorName = getLexer().getTok().getStringContents();
671 if (getLexer().isNot(AsmToken::Comma))
672 return TokError("arch name required, comma expected");
675 if (getLexer().isNot(AsmToken::String))
676 return TokError("invalid arch name");
678 ArchName = getLexer().getTok().getStringContents();
681 getTargetStreamer().EmitDirectiveHSACodeObjectISA(Major, Minor, Stepping,
682 VendorName, ArchName);
686 bool AMDGPUAsmParser::ParseDirective(AsmToken DirectiveID) {
687 MCAsmParser &Parser = getParser();
688 StringRef IDVal = DirectiveID.getString();
690 if (IDVal == ".hsa_code_object_version")
691 return ParseDirectiveHSACodeObjectVersion();
693 if (IDVal == ".hsa_code_object_isa")
694 return ParseDirectiveHSACodeObjectISA();
699 static bool operandsHaveModifiers(const OperandVector &Operands) {
701 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
702 const AMDGPUOperand &Op = ((AMDGPUOperand&)*Operands[i]);
703 if (Op.isRegKind() && Op.hasModifiers())
705 if (Op.isImm() && (Op.getImmTy() == AMDGPUOperand::ImmTyOMod ||
706 Op.getImmTy() == AMDGPUOperand::ImmTyClamp))
712 AMDGPUAsmParser::OperandMatchResultTy
713 AMDGPUAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
715 // Try to parse with a custom parser
716 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
718 // If we successfully parsed the operand or if there as an error parsing,
721 // If we are parsing after we reach EndOfStatement then this means we
722 // are appending default values to the Operands list. This is only done
723 // by custom parser, so we shouldn't continue on to the generic parsing.
724 if (ResTy == MatchOperand_Success || ResTy == MatchOperand_ParseFail ||
725 getLexer().is(AsmToken::EndOfStatement))
728 bool Negate = false, Abs = false;
729 if (getLexer().getKind()== AsmToken::Minus) {
734 if (getLexer().getKind() == AsmToken::Pipe) {
739 switch(getLexer().getKind()) {
740 case AsmToken::Integer: {
741 SMLoc S = Parser.getTok().getLoc();
743 if (getParser().parseAbsoluteExpression(IntVal))
744 return MatchOperand_ParseFail;
745 APInt IntVal32(32, IntVal);
746 if (IntVal32.getSExtValue() != IntVal) {
747 Error(S, "invalid immediate: only 32-bit values are legal");
748 return MatchOperand_ParseFail;
751 IntVal = IntVal32.getSExtValue();
754 Operands.push_back(AMDGPUOperand::CreateImm(IntVal, S));
755 return MatchOperand_Success;
757 case AsmToken::Real: {
758 // FIXME: We should emit an error if a double precisions floating-point
759 // value is used. I'm not sure the best way to detect this.
760 SMLoc S = Parser.getTok().getLoc();
762 if (getParser().parseAbsoluteExpression(IntVal))
763 return MatchOperand_ParseFail;
765 APFloat F((float)BitsToDouble(IntVal));
769 AMDGPUOperand::CreateImm(F.bitcastToAPInt().getZExtValue(), S));
770 return MatchOperand_Success;
772 case AsmToken::Identifier: {
775 if (!ParseRegister(RegNo, S, E)) {
777 bool HasModifiers = operandsHaveModifiers(Operands);
778 unsigned Modifiers = 0;
784 if (getLexer().getKind() != AsmToken::Pipe)
785 return MatchOperand_ParseFail;
790 if (Modifiers && !HasModifiers) {
791 // We are adding a modifier to src1 or src2 and previous sources
792 // don't have modifiers, so we need to go back and empty modifers
793 // for each previous source.
794 for (unsigned PrevRegIdx = Operands.size() - 1; PrevRegIdx > 1;
797 AMDGPUOperand &RegOp = ((AMDGPUOperand&)*Operands[PrevRegIdx]);
798 RegOp.setModifiers(0);
803 Operands.push_back(AMDGPUOperand::CreateReg(
804 RegNo, S, E, getContext().getRegisterInfo(),
807 if (HasModifiers || Modifiers) {
808 AMDGPUOperand &RegOp = ((AMDGPUOperand&)*Operands[Operands.size() - 1]);
809 RegOp.setModifiers(Modifiers);
813 Operands.push_back(AMDGPUOperand::CreateToken(Parser.getTok().getString(),
817 return MatchOperand_Success;
820 return MatchOperand_NoMatch;
824 bool AMDGPUAsmParser::ParseInstruction(ParseInstructionInfo &Info,
826 SMLoc NameLoc, OperandVector &Operands) {
828 // Clear any forced encodings from the previous instruction.
829 setForcedEncodingSize(0);
831 if (Name.endswith("_e64"))
832 setForcedEncodingSize(64);
833 else if (Name.endswith("_e32"))
834 setForcedEncodingSize(32);
836 // Add the instruction mnemonic
837 Operands.push_back(AMDGPUOperand::CreateToken(Name, NameLoc));
839 while (!getLexer().is(AsmToken::EndOfStatement)) {
840 AMDGPUAsmParser::OperandMatchResultTy Res = parseOperand(Operands, Name);
842 // Eat the comma or space if there is one.
843 if (getLexer().is(AsmToken::Comma))
847 case MatchOperand_Success: break;
848 case MatchOperand_ParseFail: return Error(getLexer().getLoc(),
849 "failed parsing operand.");
850 case MatchOperand_NoMatch: return Error(getLexer().getLoc(),
851 "not a valid operand.");
855 // Once we reach end of statement, continue parsing so we can add default
856 // values for optional arguments.
857 AMDGPUAsmParser::OperandMatchResultTy Res;
858 while ((Res = parseOperand(Operands, Name)) != MatchOperand_NoMatch) {
859 if (Res != MatchOperand_Success)
860 return Error(getLexer().getLoc(), "failed parsing operand.");
865 //===----------------------------------------------------------------------===//
867 //===----------------------------------------------------------------------===//
869 AMDGPUAsmParser::OperandMatchResultTy
870 AMDGPUAsmParser::parseIntWithPrefix(const char *Prefix, int64_t &Int,
873 // We are at the end of the statement, and this is a default argument, so
874 // use a default value.
875 if (getLexer().is(AsmToken::EndOfStatement)) {
877 return MatchOperand_Success;
880 switch(getLexer().getKind()) {
881 default: return MatchOperand_NoMatch;
882 case AsmToken::Identifier: {
883 StringRef OffsetName = Parser.getTok().getString();
884 if (!OffsetName.equals(Prefix))
885 return MatchOperand_NoMatch;
888 if (getLexer().isNot(AsmToken::Colon))
889 return MatchOperand_ParseFail;
892 if (getLexer().isNot(AsmToken::Integer))
893 return MatchOperand_ParseFail;
895 if (getParser().parseAbsoluteExpression(Int))
896 return MatchOperand_ParseFail;
900 return MatchOperand_Success;
903 AMDGPUAsmParser::OperandMatchResultTy
904 AMDGPUAsmParser::parseIntWithPrefix(const char *Prefix, OperandVector &Operands,
905 enum AMDGPUOperand::ImmTy ImmTy) {
907 SMLoc S = Parser.getTok().getLoc();
910 AMDGPUAsmParser::OperandMatchResultTy Res = parseIntWithPrefix(Prefix, Offset);
911 if (Res != MatchOperand_Success)
914 Operands.push_back(AMDGPUOperand::CreateImm(Offset, S, ImmTy));
915 return MatchOperand_Success;
918 AMDGPUAsmParser::OperandMatchResultTy
919 AMDGPUAsmParser::parseNamedBit(const char *Name, OperandVector &Operands,
920 enum AMDGPUOperand::ImmTy ImmTy) {
922 SMLoc S = Parser.getTok().getLoc();
924 // We are at the end of the statement, and this is a default argument, so
925 // use a default value.
926 if (getLexer().isNot(AsmToken::EndOfStatement)) {
927 switch(getLexer().getKind()) {
928 case AsmToken::Identifier: {
929 StringRef Tok = Parser.getTok().getString();
933 } else if (Tok.startswith("no") && Tok.endswith(Name)) {
937 return MatchOperand_NoMatch;
942 return MatchOperand_NoMatch;
946 Operands.push_back(AMDGPUOperand::CreateImm(Bit, S, ImmTy));
947 return MatchOperand_Success;
950 static bool operandsHasOptionalOp(const OperandVector &Operands,
951 const OptionalOperand &OOp) {
952 for (unsigned i = 0; i < Operands.size(); i++) {
953 const AMDGPUOperand &ParsedOp = ((const AMDGPUOperand &)*Operands[i]);
954 if ((ParsedOp.isImm() && ParsedOp.getImmTy() == OOp.Type) ||
955 (ParsedOp.isToken() && ParsedOp.getToken() == OOp.Name))
962 AMDGPUAsmParser::OperandMatchResultTy
963 AMDGPUAsmParser::parseOptionalOps(const ArrayRef<OptionalOperand> &OptionalOps,
964 OperandVector &Operands) {
965 SMLoc S = Parser.getTok().getLoc();
966 for (const OptionalOperand &Op : OptionalOps) {
967 if (operandsHasOptionalOp(Operands, Op))
969 AMDGPUAsmParser::OperandMatchResultTy Res;
972 Res = parseNamedBit(Op.Name, Operands, Op.Type);
973 if (Res == MatchOperand_NoMatch)
978 Res = parseIntWithPrefix(Op.Name, Value, Op.Default);
980 if (Res == MatchOperand_NoMatch)
983 if (Res != MatchOperand_Success)
986 if (Op.ConvertResult && !Op.ConvertResult(Value)) {
987 return MatchOperand_ParseFail;
990 Operands.push_back(AMDGPUOperand::CreateImm(Value, S, Op.Type));
991 return MatchOperand_Success;
993 return MatchOperand_NoMatch;
996 //===----------------------------------------------------------------------===//
998 //===----------------------------------------------------------------------===//
1000 static const OptionalOperand DSOptionalOps [] = {
1001 {"offset", AMDGPUOperand::ImmTyOffset, false, 0, nullptr},
1002 {"gds", AMDGPUOperand::ImmTyGDS, true, 0, nullptr}
1005 static const OptionalOperand DSOptionalOpsOff01 [] = {
1006 {"offset0", AMDGPUOperand::ImmTyDSOffset0, false, 0, nullptr},
1007 {"offset1", AMDGPUOperand::ImmTyDSOffset1, false, 0, nullptr},
1008 {"gds", AMDGPUOperand::ImmTyGDS, true, 0, nullptr}
1011 AMDGPUAsmParser::OperandMatchResultTy
1012 AMDGPUAsmParser::parseDSOptionalOps(OperandVector &Operands) {
1013 return parseOptionalOps(DSOptionalOps, Operands);
1015 AMDGPUAsmParser::OperandMatchResultTy
1016 AMDGPUAsmParser::parseDSOff01OptionalOps(OperandVector &Operands) {
1017 return parseOptionalOps(DSOptionalOpsOff01, Operands);
1020 AMDGPUAsmParser::OperandMatchResultTy
1021 AMDGPUAsmParser::parseDSOffsetOptional(OperandVector &Operands) {
1022 SMLoc S = Parser.getTok().getLoc();
1023 AMDGPUAsmParser::OperandMatchResultTy Res =
1024 parseIntWithPrefix("offset", Operands, AMDGPUOperand::ImmTyOffset);
1025 if (Res == MatchOperand_NoMatch) {
1026 Operands.push_back(AMDGPUOperand::CreateImm(0, S,
1027 AMDGPUOperand::ImmTyOffset));
1028 Res = MatchOperand_Success;
1033 bool AMDGPUOperand::isDSOffset() const {
1034 return isImm() && isUInt<16>(getImm());
1037 bool AMDGPUOperand::isDSOffset01() const {
1038 return isImm() && isUInt<8>(getImm());
1041 void AMDGPUAsmParser::cvtDSOffset01(MCInst &Inst,
1042 const OperandVector &Operands) {
1044 std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1046 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1047 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1049 // Add the register arguments
1051 Op.addRegOperands(Inst, 1);
1055 // Handle optional arguments
1056 OptionalIdx[Op.getImmTy()] = i;
1059 unsigned Offset0Idx = OptionalIdx[AMDGPUOperand::ImmTyDSOffset0];
1060 unsigned Offset1Idx = OptionalIdx[AMDGPUOperand::ImmTyDSOffset1];
1061 unsigned GDSIdx = OptionalIdx[AMDGPUOperand::ImmTyGDS];
1063 ((AMDGPUOperand &)*Operands[Offset0Idx]).addImmOperands(Inst, 1); // offset0
1064 ((AMDGPUOperand &)*Operands[Offset1Idx]).addImmOperands(Inst, 1); // offset1
1065 ((AMDGPUOperand &)*Operands[GDSIdx]).addImmOperands(Inst, 1); // gds
1066 Inst.addOperand(MCOperand::createReg(AMDGPU::M0)); // m0
1069 void AMDGPUAsmParser::cvtDS(MCInst &Inst, const OperandVector &Operands) {
1071 std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1072 bool GDSOnly = false;
1074 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1075 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1077 // Add the register arguments
1079 Op.addRegOperands(Inst, 1);
1083 if (Op.isToken() && Op.getToken() == "gds") {
1088 // Handle optional arguments
1089 OptionalIdx[Op.getImmTy()] = i;
1092 unsigned OffsetIdx = OptionalIdx[AMDGPUOperand::ImmTyOffset];
1093 ((AMDGPUOperand &)*Operands[OffsetIdx]).addImmOperands(Inst, 1); // offset
1096 unsigned GDSIdx = OptionalIdx[AMDGPUOperand::ImmTyGDS];
1097 ((AMDGPUOperand &)*Operands[GDSIdx]).addImmOperands(Inst, 1); // gds
1099 Inst.addOperand(MCOperand::createReg(AMDGPU::M0)); // m0
1103 //===----------------------------------------------------------------------===//
1105 //===----------------------------------------------------------------------===//
1107 bool AMDGPUAsmParser::parseCnt(int64_t &IntVal) {
1108 StringRef CntName = Parser.getTok().getString();
1112 if (getLexer().isNot(AsmToken::LParen))
1116 if (getLexer().isNot(AsmToken::Integer))
1119 if (getParser().parseAbsoluteExpression(CntVal))
1122 if (getLexer().isNot(AsmToken::RParen))
1126 if (getLexer().is(AsmToken::Amp) || getLexer().is(AsmToken::Comma))
1132 if (CntName == "vmcnt") {
1135 } else if (CntName == "expcnt") {
1138 } else if (CntName == "lgkmcnt") {
1145 IntVal &= ~(CntMask << CntShift);
1146 IntVal |= (CntVal << CntShift);
1150 AMDGPUAsmParser::OperandMatchResultTy
1151 AMDGPUAsmParser::parseSWaitCntOps(OperandVector &Operands) {
1152 // Disable all counters by default.
1156 int64_t CntVal = 0x77f;
1157 SMLoc S = Parser.getTok().getLoc();
1159 switch(getLexer().getKind()) {
1160 default: return MatchOperand_ParseFail;
1161 case AsmToken::Integer:
1162 // The operand can be an integer value.
1163 if (getParser().parseAbsoluteExpression(CntVal))
1164 return MatchOperand_ParseFail;
1167 case AsmToken::Identifier:
1169 if (parseCnt(CntVal))
1170 return MatchOperand_ParseFail;
1171 } while(getLexer().isNot(AsmToken::EndOfStatement));
1174 Operands.push_back(AMDGPUOperand::CreateImm(CntVal, S));
1175 return MatchOperand_Success;
1178 bool AMDGPUOperand::isSWaitCnt() const {
1182 //===----------------------------------------------------------------------===//
1183 // sopp branch targets
1184 //===----------------------------------------------------------------------===//
1186 AMDGPUAsmParser::OperandMatchResultTy
1187 AMDGPUAsmParser::parseSOppBrTarget(OperandVector &Operands) {
1188 SMLoc S = Parser.getTok().getLoc();
1190 switch (getLexer().getKind()) {
1191 default: return MatchOperand_ParseFail;
1192 case AsmToken::Integer: {
1194 if (getParser().parseAbsoluteExpression(Imm))
1195 return MatchOperand_ParseFail;
1196 Operands.push_back(AMDGPUOperand::CreateImm(Imm, S));
1197 return MatchOperand_Success;
1200 case AsmToken::Identifier:
1201 Operands.push_back(AMDGPUOperand::CreateExpr(
1202 MCSymbolRefExpr::create(getContext().getOrCreateSymbol(
1203 Parser.getTok().getString()), getContext()), S));
1205 return MatchOperand_Success;
1209 //===----------------------------------------------------------------------===//
1211 //===----------------------------------------------------------------------===//
1213 static const OptionalOperand FlatOptionalOps [] = {
1214 {"glc", AMDGPUOperand::ImmTyGLC, true, 0, nullptr},
1215 {"slc", AMDGPUOperand::ImmTySLC, true, 0, nullptr},
1216 {"tfe", AMDGPUOperand::ImmTyTFE, true, 0, nullptr}
1219 static const OptionalOperand FlatAtomicOptionalOps [] = {
1220 {"slc", AMDGPUOperand::ImmTySLC, true, 0, nullptr},
1221 {"tfe", AMDGPUOperand::ImmTyTFE, true, 0, nullptr}
1224 AMDGPUAsmParser::OperandMatchResultTy
1225 AMDGPUAsmParser::parseFlatOptionalOps(OperandVector &Operands) {
1226 return parseOptionalOps(FlatOptionalOps, Operands);
1229 AMDGPUAsmParser::OperandMatchResultTy
1230 AMDGPUAsmParser::parseFlatAtomicOptionalOps(OperandVector &Operands) {
1231 return parseOptionalOps(FlatAtomicOptionalOps, Operands);
1234 void AMDGPUAsmParser::cvtFlat(MCInst &Inst,
1235 const OperandVector &Operands) {
1236 std::map<AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1238 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1239 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1241 // Add the register arguments
1243 Op.addRegOperands(Inst, 1);
1247 // Handle 'glc' token which is sometimes hard-coded into the
1248 // asm string. There are no MCInst operands for these.
1252 // Handle optional arguments
1253 OptionalIdx[Op.getImmTy()] = i;
1257 // flat atomic instructions don't have a glc argument.
1258 if (OptionalIdx.count(AMDGPUOperand::ImmTyGLC)) {
1259 unsigned GLCIdx = OptionalIdx[AMDGPUOperand::ImmTyGLC];
1260 ((AMDGPUOperand &)*Operands[GLCIdx]).addImmOperands(Inst, 1);
1263 unsigned SLCIdx = OptionalIdx[AMDGPUOperand::ImmTySLC];
1264 unsigned TFEIdx = OptionalIdx[AMDGPUOperand::ImmTyTFE];
1266 ((AMDGPUOperand &)*Operands[SLCIdx]).addImmOperands(Inst, 1);
1267 ((AMDGPUOperand &)*Operands[TFEIdx]).addImmOperands(Inst, 1);
1270 //===----------------------------------------------------------------------===//
1272 //===----------------------------------------------------------------------===//
1274 static const OptionalOperand MubufOptionalOps [] = {
1275 {"offset", AMDGPUOperand::ImmTyOffset, false, 0, nullptr},
1276 {"glc", AMDGPUOperand::ImmTyGLC, true, 0, nullptr},
1277 {"slc", AMDGPUOperand::ImmTySLC, true, 0, nullptr},
1278 {"tfe", AMDGPUOperand::ImmTyTFE, true, 0, nullptr}
1281 AMDGPUAsmParser::OperandMatchResultTy
1282 AMDGPUAsmParser::parseMubufOptionalOps(OperandVector &Operands) {
1283 return parseOptionalOps(MubufOptionalOps, Operands);
1286 AMDGPUAsmParser::OperandMatchResultTy
1287 AMDGPUAsmParser::parseOffset(OperandVector &Operands) {
1288 return parseIntWithPrefix("offset", Operands);
1291 AMDGPUAsmParser::OperandMatchResultTy
1292 AMDGPUAsmParser::parseGLC(OperandVector &Operands) {
1293 return parseNamedBit("glc", Operands);
1296 AMDGPUAsmParser::OperandMatchResultTy
1297 AMDGPUAsmParser::parseSLC(OperandVector &Operands) {
1298 return parseNamedBit("slc", Operands);
1301 AMDGPUAsmParser::OperandMatchResultTy
1302 AMDGPUAsmParser::parseTFE(OperandVector &Operands) {
1303 return parseNamedBit("tfe", Operands);
1306 bool AMDGPUOperand::isMubufOffset() const {
1307 return isImm() && isUInt<12>(getImm());
1310 void AMDGPUAsmParser::cvtMubuf(MCInst &Inst,
1311 const OperandVector &Operands) {
1312 std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1314 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1315 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1317 // Add the register arguments
1319 Op.addRegOperands(Inst, 1);
1323 // Handle the case where soffset is an immediate
1324 if (Op.isImm() && Op.getImmTy() == AMDGPUOperand::ImmTyNone) {
1325 Op.addImmOperands(Inst, 1);
1329 // Handle tokens like 'offen' which are sometimes hard-coded into the
1330 // asm string. There are no MCInst operands for these.
1336 // Handle optional arguments
1337 OptionalIdx[Op.getImmTy()] = i;
1340 assert(OptionalIdx.size() == 4);
1342 unsigned OffsetIdx = OptionalIdx[AMDGPUOperand::ImmTyOffset];
1343 unsigned GLCIdx = OptionalIdx[AMDGPUOperand::ImmTyGLC];
1344 unsigned SLCIdx = OptionalIdx[AMDGPUOperand::ImmTySLC];
1345 unsigned TFEIdx = OptionalIdx[AMDGPUOperand::ImmTyTFE];
1347 ((AMDGPUOperand &)*Operands[OffsetIdx]).addImmOperands(Inst, 1);
1348 ((AMDGPUOperand &)*Operands[GLCIdx]).addImmOperands(Inst, 1);
1349 ((AMDGPUOperand &)*Operands[SLCIdx]).addImmOperands(Inst, 1);
1350 ((AMDGPUOperand &)*Operands[TFEIdx]).addImmOperands(Inst, 1);
1353 //===----------------------------------------------------------------------===//
1355 //===----------------------------------------------------------------------===//
1357 AMDGPUAsmParser::OperandMatchResultTy
1358 AMDGPUAsmParser::parseDMask(OperandVector &Operands) {
1359 return parseIntWithPrefix("dmask", Operands);
1362 AMDGPUAsmParser::OperandMatchResultTy
1363 AMDGPUAsmParser::parseUNorm(OperandVector &Operands) {
1364 return parseNamedBit("unorm", Operands);
1367 AMDGPUAsmParser::OperandMatchResultTy
1368 AMDGPUAsmParser::parseR128(OperandVector &Operands) {
1369 return parseNamedBit("r128", Operands);
1372 //===----------------------------------------------------------------------===//
1374 //===----------------------------------------------------------------------===//
1376 static bool ConvertOmodMul(int64_t &Mul) {
1377 if (Mul != 1 && Mul != 2 && Mul != 4)
1384 static bool ConvertOmodDiv(int64_t &Div) {
1398 static const OptionalOperand VOP3OptionalOps [] = {
1399 {"clamp", AMDGPUOperand::ImmTyClamp, true, 0, nullptr},
1400 {"mul", AMDGPUOperand::ImmTyOMod, false, 1, ConvertOmodMul},
1401 {"div", AMDGPUOperand::ImmTyOMod, false, 1, ConvertOmodDiv},
1404 static bool isVOP3(OperandVector &Operands) {
1405 if (operandsHaveModifiers(Operands))
1408 AMDGPUOperand &DstOp = ((AMDGPUOperand&)*Operands[1]);
1410 if (DstOp.isReg() && DstOp.isRegClass(AMDGPU::SGPR_64RegClassID))
1413 if (Operands.size() >= 5)
1416 if (Operands.size() > 3) {
1417 AMDGPUOperand &Src1Op = ((AMDGPUOperand&)*Operands[3]);
1418 if (Src1Op.getReg() && (Src1Op.isRegClass(AMDGPU::SReg_32RegClassID) ||
1419 Src1Op.isRegClass(AMDGPU::SReg_64RegClassID)))
1425 AMDGPUAsmParser::OperandMatchResultTy
1426 AMDGPUAsmParser::parseVOP3OptionalOps(OperandVector &Operands) {
1428 // The value returned by this function may change after parsing
1429 // an operand so store the original value here.
1430 bool HasModifiers = operandsHaveModifiers(Operands);
1432 bool IsVOP3 = isVOP3(Operands);
1433 if (HasModifiers || IsVOP3 ||
1434 getLexer().isNot(AsmToken::EndOfStatement) ||
1435 getForcedEncodingSize() == 64) {
1437 AMDGPUAsmParser::OperandMatchResultTy Res =
1438 parseOptionalOps(VOP3OptionalOps, Operands);
1440 if (!HasModifiers && Res == MatchOperand_Success) {
1441 // We have added a modifier operation, so we need to make sure all
1442 // previous register operands have modifiers
1443 for (unsigned i = 2, e = Operands.size(); i != e; ++i) {
1444 AMDGPUOperand &Op = ((AMDGPUOperand&)*Operands[i]);
1451 return MatchOperand_NoMatch;
1454 void AMDGPUAsmParser::cvtVOP3(MCInst &Inst, const OperandVector &Operands) {
1455 ((AMDGPUOperand &)*Operands[1]).addRegOperands(Inst, 1);
1458 std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1460 if (operandsHaveModifiers(Operands)) {
1461 for (unsigned e = Operands.size(); i != e; ++i) {
1462 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1464 if (Op.isRegWithInputMods()) {
1465 ((AMDGPUOperand &)*Operands[i]).addRegWithInputModsOperands(Inst, 2);
1468 OptionalIdx[Op.getImmTy()] = i;
1471 unsigned ClampIdx = OptionalIdx[AMDGPUOperand::ImmTyClamp];
1472 unsigned OModIdx = OptionalIdx[AMDGPUOperand::ImmTyOMod];
1474 ((AMDGPUOperand &)*Operands[ClampIdx]).addImmOperands(Inst, 1);
1475 ((AMDGPUOperand &)*Operands[OModIdx]).addImmOperands(Inst, 1);
1477 for (unsigned e = Operands.size(); i != e; ++i)
1478 ((AMDGPUOperand &)*Operands[i]).addRegOrImmOperands(Inst, 1);
1482 /// Force static initialization.
1483 extern "C" void LLVMInitializeAMDGPUAsmParser() {
1484 RegisterMCAsmParser<AMDGPUAsmParser> A(TheAMDGPUTarget);
1485 RegisterMCAsmParser<AMDGPUAsmParser> B(TheGCNTarget);
1488 #define GET_REGISTER_MATCHER
1489 #define GET_MATCHER_IMPLEMENTATION
1490 #include "AMDGPUGenAsmMatcher.inc"