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 "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"
43 struct OptionalOperand;
45 class AMDGPUOperand : public MCParsedAsmOperand {
53 SMLoc StartLoc, EndLoc;
56 AMDGPUOperand(enum KindTy K) : MCParsedAsmOperand(), Kind(K) {}
87 const MCRegisterInfo *TRI;
98 void addImmOperands(MCInst &Inst, unsigned N) const {
99 Inst.addOperand(MCOperand::createImm(getImm()));
102 StringRef getToken() const {
103 return StringRef(Tok.Data, Tok.Length);
106 void addRegOperands(MCInst &Inst, unsigned N) const {
107 Inst.addOperand(MCOperand::createReg(getReg()));
110 void addRegOrImmOperands(MCInst &Inst, unsigned N) const {
112 addRegOperands(Inst, N);
114 addImmOperands(Inst, N);
117 void addRegWithInputModsOperands(MCInst &Inst, unsigned N) const {
118 Inst.addOperand(MCOperand::createImm(
119 Reg.Modifiers == -1 ? 0 : Reg.Modifiers));
120 addRegOperands(Inst, N);
123 void addSoppBrTargetOperands(MCInst &Inst, unsigned N) const {
125 addImmOperands(Inst, N);
128 Inst.addOperand(MCOperand::createExpr(Expr));
132 bool defaultTokenHasSuffix() const {
133 StringRef Token(Tok.Data, Tok.Length);
135 return Token.endswith("_e32") || Token.endswith("_e64");
138 bool isToken() const override {
139 return Kind == Token;
142 bool isImm() const override {
143 return Kind == Immediate;
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));
154 bool isDSOffset0() const {
156 return Imm.Type == ImmTyDSOffset0;
159 bool isDSOffset1() const {
161 return Imm.Type == ImmTyDSOffset1;
164 int64_t getImm() const {
168 enum ImmTy getImmTy() const {
173 bool isRegKind() const {
174 return Kind == Register;
177 bool isReg() const override {
178 return Kind == Register && Reg.Modifiers == -1;
181 bool isRegWithInputMods() const {
182 return Kind == Register && (Reg.IsForcedVOP3 || Reg.Modifiers != -1);
185 void setModifiers(unsigned Mods) {
187 Reg.Modifiers = Mods;
190 bool hasModifiers() const {
192 return Reg.Modifiers != -1;
195 unsigned getReg() const override {
199 bool isRegOrImm() const {
200 return isReg() || isImm();
203 bool isRegClass(unsigned RCID) const {
204 return Reg.TRI->getRegClass(RCID).contains(getReg());
207 bool isSCSrc32() const {
208 return isInlineImm() || (isReg() && isRegClass(AMDGPU::SReg_32RegClassID));
211 bool isSSrc32() const {
212 return isImm() || (isReg() && isRegClass(AMDGPU::SReg_32RegClassID));
215 bool isSSrc64() const {
216 return isImm() || isInlineImm() ||
217 (isReg() && isRegClass(AMDGPU::SReg_64RegClassID));
220 bool isSCSrc64() const {
221 return (isReg() && isRegClass(AMDGPU::SReg_64RegClassID)) || isInlineImm();
224 bool isVCSrc32() const {
225 return isInlineImm() || (isReg() && isRegClass(AMDGPU::VS_32RegClassID));
228 bool isVCSrc64() const {
229 return isInlineImm() || (isReg() && isRegClass(AMDGPU::VS_64RegClassID));
232 bool isVSrc32() const {
233 return isImm() || (isReg() && isRegClass(AMDGPU::VS_32RegClassID));
236 bool isVSrc64() const {
237 return isImm() || (isReg() && isRegClass(AMDGPU::VS_64RegClassID));
240 bool isMem() const override {
244 bool isExpr() const {
245 return Kind == Expression;
248 bool isSoppBrTarget() const {
249 return isExpr() || isImm();
252 SMLoc getStartLoc() const override {
256 SMLoc getEndLoc() const override {
260 void print(raw_ostream &OS) const override {
263 OS << "<register " << getReg() << " mods: " << Reg.Modifiers << '>';
269 OS << '\'' << getToken() << '\'';
272 OS << "<expr " << *Expr << '>';
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);
282 Op->Imm.IsFPImm = IsFPImm;
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();
299 static std::unique_ptr<AMDGPUOperand> CreateReg(unsigned RegNo, SMLoc S,
301 const MCRegisterInfo *TRI,
303 auto Op = llvm::make_unique<AMDGPUOperand>(Register);
304 Op->Reg.RegNo = RegNo;
306 Op->Reg.Modifiers = -1;
307 Op->Reg.IsForcedVOP3 = ForceVOP3;
313 static std::unique_ptr<AMDGPUOperand> CreateExpr(const class MCExpr *Expr, SMLoc S) {
314 auto Op = llvm::make_unique<AMDGPUOperand>(Expression);
321 bool isDSOffset() const;
322 bool isDSOffset01() const;
323 bool isSWaitCnt() const;
324 bool isMubufOffset() const;
325 bool isSMRDOffset() const;
326 bool isSMRDLiteralOffset() const;
329 class AMDGPUAsmParser : public MCTargetAsmParser {
330 const MCInstrInfo &MII;
333 unsigned ForcedEncodingSize;
336 return STI->getFeatureBits()[AMDGPU::FeatureSouthernIslands];
340 return STI->getFeatureBits()[AMDGPU::FeatureSeaIslands];
344 return getSTI().getFeatureBits()[AMDGPU::FeatureVolcanicIslands];
347 bool hasSGPR102_SGPR103() const {
351 /// @name Auto-generated Match Functions
354 #define GET_ASSEMBLER_HEADER
355 #include "AMDGPUGenAsmMatcher.inc"
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();
371 enum AMDGPUMatchResultTy {
372 Match_PreferE32 = FIRST_TARGET_MATCH_RESULT_TY
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);
382 if (getSTI().getFeatureBits().none()) {
383 // Set default features.
384 copySTI().ToggleFeature("SOUTHERN_ISLANDS");
387 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
390 AMDGPUTargetStreamer &getTargetStreamer() {
391 MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
392 return static_cast<AMDGPUTargetStreamer &>(TS);
395 unsigned getForcedEncodingSize() const {
396 return ForcedEncodingSize;
399 void setForcedEncodingSize(unsigned Size) {
400 ForcedEncodingSize = Size;
403 bool isForcedVOP3() const {
404 return ForcedEncodingSize == 64;
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,
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;
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);
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);
438 bool parseCnt(int64_t &IntVal);
439 OperandMatchResultTy parseSWaitCntOps(OperandVector &Operands);
440 OperandMatchResultTy parseSOppBrTarget(OperandVector &Operands);
442 OperandMatchResultTy parseFlatOptionalOps(OperandVector &Operands);
443 OperandMatchResultTy parseFlatAtomicOptionalOps(OperandVector &Operands);
444 void cvtFlat(MCInst &Inst, const OperandVector &Operands);
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);
453 OperandMatchResultTy parseDMask(OperandVector &Operands);
454 OperandMatchResultTy parseUNorm(OperandVector &Operands);
455 OperandMatchResultTy parseR128(OperandVector &Operands);
457 void cvtVOP3(MCInst &Inst, const OperandVector &Operands);
458 OperandMatchResultTy parseVOP3OptionalOps(OperandVector &Operands);
461 struct OptionalOperand {
463 AMDGPUOperand::ImmTy Type;
466 bool (*ConvertResult)(int64_t&);
471 static int getRegClass(bool IsVgpr, unsigned RegWidth) {
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;
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;
494 static unsigned getRegForName(StringRef RegName) {
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)
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();
517 StringRef RegName = Tok.getString();
518 RegNo = getRegForName(RegName);
522 return !subtargetHasRegister(*TRI, RegNo);
525 // Match vgprs and sgprs
526 if (RegName[0] != 's' && RegName[0] != 'v')
529 bool IsVgpr = RegName[0] == 'v';
531 unsigned RegIndexInClass;
532 if (RegName.size() > 1) {
533 // We have a 32-bit register
535 if (RegName.substr(1).getAsInteger(10, RegIndexInClass))
539 // We have a register greater than 32-bits.
541 int64_t RegLo, RegHi;
543 if (getLexer().isNot(AsmToken::LBrac))
547 if (getParser().parseAbsoluteExpression(RegLo))
550 if (getLexer().isNot(AsmToken::Colon))
554 if (getParser().parseAbsoluteExpression(RegHi))
557 if (getLexer().isNot(AsmToken::RBrac))
561 RegWidth = (RegHi - RegLo) + 1;
563 // VGPR registers aren't aligned.
564 RegIndexInClass = RegLo;
566 // SGPR registers are aligned. Max alignment is 4 dwords.
567 unsigned Size = std::min(RegWidth, 4u);
568 if (RegLo % Size != 0)
571 RegIndexInClass = RegLo / Size;
575 int RCID = getRegClass(IsVgpr, RegWidth);
579 const MCRegisterClass RC = TRI->getRegClass(RCID);
580 if (RegIndexInClass >= RC.getNumRegs())
583 RegNo = RC.getRegister(RegIndexInClass);
584 return !subtargetHasRegister(*TRI, RegNo);
587 unsigned AMDGPUAsmParser::checkTargetMatchPredicate(MCInst &Inst) {
589 uint64_t TSFlags = MII.get(Inst.getOpcode()).TSFlags;
591 if ((getForcedEncodingSize() == 32 && (TSFlags & SIInstrFlags::VOP3)) ||
592 (getForcedEncodingSize() == 64 && !(TSFlags & SIInstrFlags::VOP3)))
593 return Match_InvalidOperand;
595 if ((TSFlags & SIInstrFlags::VOP3) &&
596 (TSFlags & SIInstrFlags::VOPAsmPrefer32Bit) &&
597 getForcedEncodingSize() != 64)
598 return Match_PreferE32;
600 return Match_Success;
604 bool AMDGPUAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
605 OperandVector &Operands,
608 bool MatchingInlineAsm) {
611 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm)) {
615 Out.EmitInstruction(Inst, getSTI());
617 case Match_MissingFeature:
618 return Error(IDLoc, "instruction not supported on this GPU");
620 case Match_MnemonicFail:
621 return Error(IDLoc, "unrecognized instruction mnemonic");
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() ||
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,
649 return Error(IDLoc, "too few operands for instruction");
652 ErrorLoc = ((AMDGPUOperand &)*Operands[ErrorInfo]).getStartLoc();
653 if (ErrorLoc == SMLoc())
656 return Error(ErrorLoc, "invalid operand for instruction");
658 case Match_PreferE32:
659 return Error(IDLoc, "internal error: instruction without _e64 suffix "
660 "should be encoded as e32");
662 llvm_unreachable("Implement any new match types added!");
665 bool AMDGPUAsmParser::ParseDirectiveMajorMinor(uint32_t &Major,
667 if (getLexer().isNot(AsmToken::Integer))
668 return TokError("invalid major version");
670 Major = getLexer().getTok().getIntVal();
673 if (getLexer().isNot(AsmToken::Comma))
674 return TokError("minor version number required, comma expected");
677 if (getLexer().isNot(AsmToken::Integer))
678 return TokError("invalid minor version");
680 Minor = getLexer().getTok().getIntVal();
686 bool AMDGPUAsmParser::ParseDirectiveHSACodeObjectVersion() {
691 if (ParseDirectiveMajorMinor(Major, Minor))
694 getTargetStreamer().EmitDirectiveHSACodeObjectVersion(Major, Minor);
698 bool AMDGPUAsmParser::ParseDirectiveHSACodeObjectISA() {
703 StringRef VendorName;
706 // If this directive has no arguments, then use the ISA version for the
708 if (getLexer().is(AsmToken::EndOfStatement)) {
709 AMDGPU::IsaVersion Isa = AMDGPU::getIsaVersion(getSTI().getFeatureBits());
710 getTargetStreamer().EmitDirectiveHSACodeObjectISA(Isa.Major, Isa.Minor,
717 if (ParseDirectiveMajorMinor(Major, Minor))
720 if (getLexer().isNot(AsmToken::Comma))
721 return TokError("stepping version number required, comma expected");
724 if (getLexer().isNot(AsmToken::Integer))
725 return TokError("invalid stepping version");
727 Stepping = getLexer().getTok().getIntVal();
730 if (getLexer().isNot(AsmToken::Comma))
731 return TokError("vendor name required, comma expected");
734 if (getLexer().isNot(AsmToken::String))
735 return TokError("invalid vendor name");
737 VendorName = getLexer().getTok().getStringContents();
740 if (getLexer().isNot(AsmToken::Comma))
741 return TokError("arch name required, comma expected");
744 if (getLexer().isNot(AsmToken::String))
745 return TokError("invalid arch name");
747 ArchName = getLexer().getTok().getStringContents();
750 getTargetStreamer().EmitDirectiveHSACodeObjectISA(Major, Minor, Stepping,
751 VendorName, ArchName);
755 bool AMDGPUAsmParser::ParseAMDKernelCodeTValue(StringRef ID,
756 amd_kernel_code_t &Header) {
758 if (getLexer().isNot(AsmToken::Equal))
759 return TokError("expected '='");
762 if (getLexer().isNot(AsmToken::Integer))
763 return TokError("amd_kernel_code_t values must be integers");
765 uint64_t Value = getLexer().getTok().getIntVal();
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;
913 return TokError("amd_kernel_code_t value not recognized.");
918 bool AMDGPUAsmParser::ParseDirectiveAMDKernelCodeT() {
920 amd_kernel_code_t Header;
921 AMDGPU::initDefaultAMDKernelCodeT(Header, getSTI().getFeatureBits());
925 if (getLexer().isNot(AsmToken::EndOfStatement))
926 return TokError("amd_kernel_code_t values must begin on a new line");
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))
933 if (getLexer().isNot(AsmToken::Identifier))
934 return TokError("expected value identifier or .end_amd_kernel_code_t");
936 StringRef ID = getLexer().getTok().getIdentifier();
939 if (ID == ".end_amd_kernel_code_t")
942 if (ParseAMDKernelCodeTValue(ID, Header))
946 getTargetStreamer().EmitAMDKernelCodeT(Header);
951 bool AMDGPUAsmParser::ParseSectionDirectiveHSAText() {
952 getParser().getStreamer().SwitchSection(
953 AMDGPU::getHSATextSection(getContext()));
957 bool AMDGPUAsmParser::ParseDirectiveAMDGPUHsaKernel() {
958 if (getLexer().isNot(AsmToken::Identifier))
959 return TokError("expected symbol name");
961 StringRef KernelName = Parser.getTok().getString();
963 getTargetStreamer().EmitAMDGPUSymbolType(KernelName,
964 ELF::STT_AMDGPU_HSA_KERNEL);
969 bool AMDGPUAsmParser::ParseDirective(AsmToken DirectiveID) {
970 StringRef IDVal = DirectiveID.getString();
972 if (IDVal == ".hsa_code_object_version")
973 return ParseDirectiveHSACodeObjectVersion();
975 if (IDVal == ".hsa_code_object_isa")
976 return ParseDirectiveHSACodeObjectISA();
978 if (IDVal == ".amd_kernel_code_t")
979 return ParseDirectiveAMDKernelCodeT();
981 if (IDVal == ".hsatext" || IDVal == ".text")
982 return ParseSectionDirectiveHSAText();
984 if (IDVal == ".amdgpu_hsa_kernel")
985 return ParseDirectiveAMDGPUHsaKernel();
990 bool AMDGPUAsmParser::subtargetHasRegister(const MCRegisterInfo &MRI,
991 unsigned RegNo) const {
998 case AMDGPU::FLAT_SCR:
999 case AMDGPU::FLAT_SCR_LO:
1000 case AMDGPU::FLAT_SCR_HI:
1007 // VI only has 102 SGPRs, so make sure we aren't trying to use the 2 more that
1009 for (MCRegAliasIterator R(AMDGPU::SGPR102_SGPR103, &MRI, true);
1018 static bool operandsHaveModifiers(const OperandVector &Operands) {
1020 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
1021 const AMDGPUOperand &Op = ((AMDGPUOperand&)*Operands[i]);
1022 if (Op.isRegKind() && Op.hasModifiers())
1024 if (Op.isImm() && (Op.getImmTy() == AMDGPUOperand::ImmTyOMod ||
1025 Op.getImmTy() == AMDGPUOperand::ImmTyClamp))
1031 AMDGPUAsmParser::OperandMatchResultTy
1032 AMDGPUAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) {
1034 // Try to parse with a custom parser
1035 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1037 // If we successfully parsed the operand or if there as an error parsing,
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))
1047 bool Negate = false, Abs = false;
1048 if (getLexer().getKind()== AsmToken::Minus) {
1053 if (getLexer().getKind() == AsmToken::Pipe) {
1058 switch(getLexer().getKind()) {
1059 case AsmToken::Integer: {
1060 SMLoc S = Parser.getTok().getLoc();
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;
1071 Operands.push_back(AMDGPUOperand::CreateImm(IntVal, S));
1072 return MatchOperand_Success;
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();
1079 if (getParser().parseAbsoluteExpression(IntVal))
1080 return MatchOperand_ParseFail;
1082 APFloat F((float)BitsToDouble(IntVal));
1086 AMDGPUOperand::CreateImm(F.bitcastToAPInt().getZExtValue(), S));
1087 return MatchOperand_Success;
1089 case AsmToken::Identifier: {
1092 if (!ParseRegister(RegNo, S, E)) {
1094 bool HasModifiers = operandsHaveModifiers(Operands);
1095 unsigned Modifiers = 0;
1101 if (getLexer().getKind() != AsmToken::Pipe)
1102 return MatchOperand_ParseFail;
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;
1114 AMDGPUOperand &RegOp = ((AMDGPUOperand&)*Operands[PrevRegIdx]);
1115 RegOp.setModifiers(0);
1120 Operands.push_back(AMDGPUOperand::CreateReg(
1121 RegNo, S, E, getContext().getRegisterInfo(),
1124 if (HasModifiers || Modifiers) {
1125 AMDGPUOperand &RegOp = ((AMDGPUOperand&)*Operands[Operands.size() - 1]);
1126 RegOp.setModifiers(Modifiers);
1130 Operands.push_back(AMDGPUOperand::CreateToken(Parser.getTok().getString(),
1134 return MatchOperand_Success;
1137 return MatchOperand_NoMatch;
1141 bool AMDGPUAsmParser::ParseInstruction(ParseInstructionInfo &Info,
1143 SMLoc NameLoc, OperandVector &Operands) {
1145 // Clear any forced encodings from the previous instruction.
1146 setForcedEncodingSize(0);
1148 if (Name.endswith("_e64"))
1149 setForcedEncodingSize(64);
1150 else if (Name.endswith("_e32"))
1151 setForcedEncodingSize(32);
1153 // Add the instruction mnemonic
1154 Operands.push_back(AMDGPUOperand::CreateToken(Name, NameLoc));
1156 while (!getLexer().is(AsmToken::EndOfStatement)) {
1157 AMDGPUAsmParser::OperandMatchResultTy Res = parseOperand(Operands, Name);
1159 // Eat the comma or space if there is one.
1160 if (getLexer().is(AsmToken::Comma))
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.");
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.");
1182 //===----------------------------------------------------------------------===//
1183 // Utility functions
1184 //===----------------------------------------------------------------------===//
1186 AMDGPUAsmParser::OperandMatchResultTy
1187 AMDGPUAsmParser::parseIntWithPrefix(const char *Prefix, int64_t &Int,
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)) {
1194 return MatchOperand_Success;
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;
1205 if (getLexer().isNot(AsmToken::Colon))
1206 return MatchOperand_ParseFail;
1209 if (getLexer().isNot(AsmToken::Integer))
1210 return MatchOperand_ParseFail;
1212 if (getParser().parseAbsoluteExpression(Int))
1213 return MatchOperand_ParseFail;
1217 return MatchOperand_Success;
1220 AMDGPUAsmParser::OperandMatchResultTy
1221 AMDGPUAsmParser::parseIntWithPrefix(const char *Prefix, OperandVector &Operands,
1222 enum AMDGPUOperand::ImmTy ImmTy) {
1224 SMLoc S = Parser.getTok().getLoc();
1227 AMDGPUAsmParser::OperandMatchResultTy Res = parseIntWithPrefix(Prefix, Offset);
1228 if (Res != MatchOperand_Success)
1231 Operands.push_back(AMDGPUOperand::CreateImm(Offset, S, ImmTy));
1232 return MatchOperand_Success;
1235 AMDGPUAsmParser::OperandMatchResultTy
1236 AMDGPUAsmParser::parseNamedBit(const char *Name, OperandVector &Operands,
1237 enum AMDGPUOperand::ImmTy ImmTy) {
1239 SMLoc S = Parser.getTok().getLoc();
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();
1250 } else if (Tok.startswith("no") && Tok.endswith(Name)) {
1254 return MatchOperand_NoMatch;
1259 return MatchOperand_NoMatch;
1263 Operands.push_back(AMDGPUOperand::CreateImm(Bit, S, ImmTy));
1264 return MatchOperand_Success;
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))
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))
1286 AMDGPUAsmParser::OperandMatchResultTy Res;
1289 Res = parseNamedBit(Op.Name, Operands, Op.Type);
1290 if (Res == MatchOperand_NoMatch)
1295 Res = parseIntWithPrefix(Op.Name, Value, Op.Default);
1297 if (Res == MatchOperand_NoMatch)
1300 if (Res != MatchOperand_Success)
1303 if (Op.ConvertResult && !Op.ConvertResult(Value)) {
1304 return MatchOperand_ParseFail;
1307 Operands.push_back(AMDGPUOperand::CreateImm(Value, S, Op.Type));
1308 return MatchOperand_Success;
1310 return MatchOperand_NoMatch;
1313 //===----------------------------------------------------------------------===//
1315 //===----------------------------------------------------------------------===//
1317 static const OptionalOperand DSOptionalOps [] = {
1318 {"offset", AMDGPUOperand::ImmTyOffset, false, 0, nullptr},
1319 {"gds", AMDGPUOperand::ImmTyGDS, true, 0, nullptr}
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}
1328 AMDGPUAsmParser::OperandMatchResultTy
1329 AMDGPUAsmParser::parseDSOptionalOps(OperandVector &Operands) {
1330 return parseOptionalOps(DSOptionalOps, Operands);
1332 AMDGPUAsmParser::OperandMatchResultTy
1333 AMDGPUAsmParser::parseDSOff01OptionalOps(OperandVector &Operands) {
1334 return parseOptionalOps(DSOptionalOpsOff01, Operands);
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;
1350 bool AMDGPUOperand::isDSOffset() const {
1351 return isImm() && isUInt<16>(getImm());
1354 bool AMDGPUOperand::isDSOffset01() const {
1355 return isImm() && isUInt<8>(getImm());
1358 void AMDGPUAsmParser::cvtDSOffset01(MCInst &Inst,
1359 const OperandVector &Operands) {
1361 std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1363 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1364 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1366 // Add the register arguments
1368 Op.addRegOperands(Inst, 1);
1372 // Handle optional arguments
1373 OptionalIdx[Op.getImmTy()] = i;
1376 unsigned Offset0Idx = OptionalIdx[AMDGPUOperand::ImmTyDSOffset0];
1377 unsigned Offset1Idx = OptionalIdx[AMDGPUOperand::ImmTyDSOffset1];
1378 unsigned GDSIdx = OptionalIdx[AMDGPUOperand::ImmTyGDS];
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
1386 void AMDGPUAsmParser::cvtDS(MCInst &Inst, const OperandVector &Operands) {
1388 std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1389 bool GDSOnly = false;
1391 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1392 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1394 // Add the register arguments
1396 Op.addRegOperands(Inst, 1);
1400 if (Op.isToken() && Op.getToken() == "gds") {
1405 // Handle optional arguments
1406 OptionalIdx[Op.getImmTy()] = i;
1409 unsigned OffsetIdx = OptionalIdx[AMDGPUOperand::ImmTyOffset];
1410 ((AMDGPUOperand &)*Operands[OffsetIdx]).addImmOperands(Inst, 1); // offset
1413 unsigned GDSIdx = OptionalIdx[AMDGPUOperand::ImmTyGDS];
1414 ((AMDGPUOperand &)*Operands[GDSIdx]).addImmOperands(Inst, 1); // gds
1416 Inst.addOperand(MCOperand::createReg(AMDGPU::M0)); // m0
1420 //===----------------------------------------------------------------------===//
1422 //===----------------------------------------------------------------------===//
1424 bool AMDGPUAsmParser::parseCnt(int64_t &IntVal) {
1425 StringRef CntName = Parser.getTok().getString();
1429 if (getLexer().isNot(AsmToken::LParen))
1433 if (getLexer().isNot(AsmToken::Integer))
1436 if (getParser().parseAbsoluteExpression(CntVal))
1439 if (getLexer().isNot(AsmToken::RParen))
1443 if (getLexer().is(AsmToken::Amp) || getLexer().is(AsmToken::Comma))
1449 if (CntName == "vmcnt") {
1452 } else if (CntName == "expcnt") {
1455 } else if (CntName == "lgkmcnt") {
1462 IntVal &= ~(CntMask << CntShift);
1463 IntVal |= (CntVal << CntShift);
1467 AMDGPUAsmParser::OperandMatchResultTy
1468 AMDGPUAsmParser::parseSWaitCntOps(OperandVector &Operands) {
1469 // Disable all counters by default.
1473 int64_t CntVal = 0x77f;
1474 SMLoc S = Parser.getTok().getLoc();
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;
1484 case AsmToken::Identifier:
1486 if (parseCnt(CntVal))
1487 return MatchOperand_ParseFail;
1488 } while(getLexer().isNot(AsmToken::EndOfStatement));
1491 Operands.push_back(AMDGPUOperand::CreateImm(CntVal, S));
1492 return MatchOperand_Success;
1495 bool AMDGPUOperand::isSWaitCnt() const {
1499 //===----------------------------------------------------------------------===//
1500 // sopp branch targets
1501 //===----------------------------------------------------------------------===//
1503 AMDGPUAsmParser::OperandMatchResultTy
1504 AMDGPUAsmParser::parseSOppBrTarget(OperandVector &Operands) {
1505 SMLoc S = Parser.getTok().getLoc();
1507 switch (getLexer().getKind()) {
1508 default: return MatchOperand_ParseFail;
1509 case AsmToken::Integer: {
1511 if (getParser().parseAbsoluteExpression(Imm))
1512 return MatchOperand_ParseFail;
1513 Operands.push_back(AMDGPUOperand::CreateImm(Imm, S));
1514 return MatchOperand_Success;
1517 case AsmToken::Identifier:
1518 Operands.push_back(AMDGPUOperand::CreateExpr(
1519 MCSymbolRefExpr::create(getContext().getOrCreateSymbol(
1520 Parser.getTok().getString()), getContext()), S));
1522 return MatchOperand_Success;
1526 //===----------------------------------------------------------------------===//
1528 //===----------------------------------------------------------------------===//
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}
1536 static const OptionalOperand FlatAtomicOptionalOps [] = {
1537 {"slc", AMDGPUOperand::ImmTySLC, true, 0, nullptr},
1538 {"tfe", AMDGPUOperand::ImmTyTFE, true, 0, nullptr}
1541 AMDGPUAsmParser::OperandMatchResultTy
1542 AMDGPUAsmParser::parseFlatOptionalOps(OperandVector &Operands) {
1543 return parseOptionalOps(FlatOptionalOps, Operands);
1546 AMDGPUAsmParser::OperandMatchResultTy
1547 AMDGPUAsmParser::parseFlatAtomicOptionalOps(OperandVector &Operands) {
1548 return parseOptionalOps(FlatAtomicOptionalOps, Operands);
1551 void AMDGPUAsmParser::cvtFlat(MCInst &Inst,
1552 const OperandVector &Operands) {
1553 std::map<AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1555 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1556 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1558 // Add the register arguments
1560 Op.addRegOperands(Inst, 1);
1564 // Handle 'glc' token which is sometimes hard-coded into the
1565 // asm string. There are no MCInst operands for these.
1569 // Handle optional arguments
1570 OptionalIdx[Op.getImmTy()] = i;
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);
1580 unsigned SLCIdx = OptionalIdx[AMDGPUOperand::ImmTySLC];
1581 unsigned TFEIdx = OptionalIdx[AMDGPUOperand::ImmTyTFE];
1583 ((AMDGPUOperand &)*Operands[SLCIdx]).addImmOperands(Inst, 1);
1584 ((AMDGPUOperand &)*Operands[TFEIdx]).addImmOperands(Inst, 1);
1587 //===----------------------------------------------------------------------===//
1589 //===----------------------------------------------------------------------===//
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}
1598 AMDGPUAsmParser::OperandMatchResultTy
1599 AMDGPUAsmParser::parseMubufOptionalOps(OperandVector &Operands) {
1600 return parseOptionalOps(MubufOptionalOps, Operands);
1603 AMDGPUAsmParser::OperandMatchResultTy
1604 AMDGPUAsmParser::parseOffset(OperandVector &Operands) {
1605 return parseIntWithPrefix("offset", Operands);
1608 AMDGPUAsmParser::OperandMatchResultTy
1609 AMDGPUAsmParser::parseGLC(OperandVector &Operands) {
1610 return parseNamedBit("glc", Operands);
1613 AMDGPUAsmParser::OperandMatchResultTy
1614 AMDGPUAsmParser::parseSLC(OperandVector &Operands) {
1615 return parseNamedBit("slc", Operands);
1618 AMDGPUAsmParser::OperandMatchResultTy
1619 AMDGPUAsmParser::parseTFE(OperandVector &Operands) {
1620 return parseNamedBit("tfe", Operands);
1623 bool AMDGPUOperand::isMubufOffset() const {
1624 return isImm() && isUInt<12>(getImm());
1627 void AMDGPUAsmParser::cvtMubuf(MCInst &Inst,
1628 const OperandVector &Operands) {
1629 std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1631 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1632 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1634 // Add the register arguments
1636 Op.addRegOperands(Inst, 1);
1640 // Handle the case where soffset is an immediate
1641 if (Op.isImm() && Op.getImmTy() == AMDGPUOperand::ImmTyNone) {
1642 Op.addImmOperands(Inst, 1);
1646 // Handle tokens like 'offen' which are sometimes hard-coded into the
1647 // asm string. There are no MCInst operands for these.
1653 // Handle optional arguments
1654 OptionalIdx[Op.getImmTy()] = i;
1657 assert(OptionalIdx.size() == 4);
1659 unsigned OffsetIdx = OptionalIdx[AMDGPUOperand::ImmTyOffset];
1660 unsigned GLCIdx = OptionalIdx[AMDGPUOperand::ImmTyGLC];
1661 unsigned SLCIdx = OptionalIdx[AMDGPUOperand::ImmTySLC];
1662 unsigned TFEIdx = OptionalIdx[AMDGPUOperand::ImmTyTFE];
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);
1670 //===----------------------------------------------------------------------===//
1672 //===----------------------------------------------------------------------===//
1674 AMDGPUAsmParser::OperandMatchResultTy
1675 AMDGPUAsmParser::parseDMask(OperandVector &Operands) {
1676 return parseIntWithPrefix("dmask", Operands);
1679 AMDGPUAsmParser::OperandMatchResultTy
1680 AMDGPUAsmParser::parseUNorm(OperandVector &Operands) {
1681 return parseNamedBit("unorm", Operands);
1684 AMDGPUAsmParser::OperandMatchResultTy
1685 AMDGPUAsmParser::parseR128(OperandVector &Operands) {
1686 return parseNamedBit("r128", Operands);
1689 //===----------------------------------------------------------------------===//
1691 //===----------------------------------------------------------------------===//
1693 bool AMDGPUOperand::isSMRDOffset() const {
1695 // FIXME: Support 20-bit offsets on VI. We need to to pass subtarget
1696 // information here.
1697 return isImm() && isUInt<8>(getImm());
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());
1706 //===----------------------------------------------------------------------===//
1708 //===----------------------------------------------------------------------===//
1710 static bool ConvertOmodMul(int64_t &Mul) {
1711 if (Mul != 1 && Mul != 2 && Mul != 4)
1718 static bool ConvertOmodDiv(int64_t &Div) {
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},
1738 static bool isVOP3(OperandVector &Operands) {
1739 if (operandsHaveModifiers(Operands))
1742 AMDGPUOperand &DstOp = ((AMDGPUOperand&)*Operands[1]);
1744 if (DstOp.isReg() && DstOp.isRegClass(AMDGPU::SGPR_64RegClassID))
1747 if (Operands.size() >= 5)
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)))
1759 AMDGPUAsmParser::OperandMatchResultTy
1760 AMDGPUAsmParser::parseVOP3OptionalOps(OperandVector &Operands) {
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);
1766 bool IsVOP3 = isVOP3(Operands);
1767 if (HasModifiers || IsVOP3 ||
1768 getLexer().isNot(AsmToken::EndOfStatement) ||
1769 getForcedEncodingSize() == 64) {
1771 AMDGPUAsmParser::OperandMatchResultTy Res =
1772 parseOptionalOps(VOP3OptionalOps, Operands);
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]);
1785 return MatchOperand_NoMatch;
1788 void AMDGPUAsmParser::cvtVOP3(MCInst &Inst, const OperandVector &Operands) {
1791 const MCInstrDesc &Desc = MII.get(Inst.getOpcode());
1792 if (Desc.getNumDefs() > 0) {
1793 ((AMDGPUOperand &)*Operands[i++]).addRegOperands(Inst, 1);
1796 std::map<enum AMDGPUOperand::ImmTy, unsigned> OptionalIdx;
1798 if (operandsHaveModifiers(Operands)) {
1799 for (unsigned e = Operands.size(); i != e; ++i) {
1800 AMDGPUOperand &Op = ((AMDGPUOperand &)*Operands[i]);
1802 if (Op.isRegWithInputMods()) {
1803 ((AMDGPUOperand &)*Operands[i]).addRegWithInputModsOperands(Inst, 2);
1806 OptionalIdx[Op.getImmTy()] = i;
1809 unsigned ClampIdx = OptionalIdx[AMDGPUOperand::ImmTyClamp];
1810 unsigned OModIdx = OptionalIdx[AMDGPUOperand::ImmTyOMod];
1812 ((AMDGPUOperand &)*Operands[ClampIdx]).addImmOperands(Inst, 1);
1813 ((AMDGPUOperand &)*Operands[OModIdx]).addImmOperands(Inst, 1);
1815 for (unsigned e = Operands.size(); i != e; ++i)
1816 ((AMDGPUOperand &)*Operands[i]).addRegOrImmOperands(Inst, 1);
1820 /// Force static initialization.
1821 extern "C" void LLVMInitializeAMDGPUAsmParser() {
1822 RegisterMCAsmParser<AMDGPUAsmParser> A(TheAMDGPUTarget);
1823 RegisterMCAsmParser<AMDGPUAsmParser> B(TheGCNTarget);
1826 #define GET_REGISTER_MATCHER
1827 #define GET_MATCHER_IMPLEMENTATION
1828 #include "AMDGPUGenAsmMatcher.inc"