109467b48Spatrick //===-- AsmPrinterInlineAsm.cpp - AsmPrinter Inline Asm Handling ----------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // This file implements the inline assembler pieces of the AsmPrinter class.
1009467b48Spatrick //
1109467b48Spatrick //===----------------------------------------------------------------------===//
1209467b48Spatrick
1309467b48Spatrick #include "llvm/ADT/SmallString.h"
1473471bf0Spatrick #include "llvm/ADT/SmallVector.h"
1509467b48Spatrick #include "llvm/ADT/Twine.h"
1609467b48Spatrick #include "llvm/CodeGen/AsmPrinter.h"
1709467b48Spatrick #include "llvm/CodeGen/MachineBasicBlock.h"
1809467b48Spatrick #include "llvm/CodeGen/MachineFunction.h"
1909467b48Spatrick #include "llvm/CodeGen/MachineModuleInfo.h"
2009467b48Spatrick #include "llvm/CodeGen/TargetRegisterInfo.h"
21*d415bd75Srobert #include "llvm/CodeGen/TargetSubtargetInfo.h"
2209467b48Spatrick #include "llvm/IR/Constants.h"
2309467b48Spatrick #include "llvm/IR/DataLayout.h"
2473471bf0Spatrick #include "llvm/IR/DiagnosticInfo.h"
2509467b48Spatrick #include "llvm/IR/InlineAsm.h"
2609467b48Spatrick #include "llvm/IR/LLVMContext.h"
2709467b48Spatrick #include "llvm/IR/Module.h"
2809467b48Spatrick #include "llvm/MC/MCAsmInfo.h"
29*d415bd75Srobert #include "llvm/MC/MCInstrInfo.h"
30*d415bd75Srobert #include "llvm/MC/MCParser/MCAsmLexer.h"
3109467b48Spatrick #include "llvm/MC/MCParser/MCTargetAsmParser.h"
3209467b48Spatrick #include "llvm/MC/MCStreamer.h"
3309467b48Spatrick #include "llvm/MC/MCSymbol.h"
34*d415bd75Srobert #include "llvm/MC/TargetRegistry.h"
3509467b48Spatrick #include "llvm/Support/ErrorHandling.h"
3609467b48Spatrick #include "llvm/Support/MemoryBuffer.h"
3709467b48Spatrick #include "llvm/Support/SourceMgr.h"
3809467b48Spatrick #include "llvm/Support/raw_ostream.h"
3909467b48Spatrick #include "llvm/Target/TargetMachine.h"
4009467b48Spatrick using namespace llvm;
4109467b48Spatrick
4209467b48Spatrick #define DEBUG_TYPE "asm-printer"
4309467b48Spatrick
addInlineAsmDiagBuffer(StringRef AsmStr,const MDNode * LocMDNode) const4409467b48Spatrick unsigned AsmPrinter::addInlineAsmDiagBuffer(StringRef AsmStr,
4509467b48Spatrick const MDNode *LocMDNode) const {
4609467b48Spatrick MCContext &Context = MMI->getContext();
4773471bf0Spatrick Context.initInlineSourceManager();
4873471bf0Spatrick SourceMgr &SrcMgr = *Context.getInlineSourceManager();
4973471bf0Spatrick std::vector<const MDNode *> &LocInfos = Context.getLocInfos();
5009467b48Spatrick
5109467b48Spatrick std::unique_ptr<MemoryBuffer> Buffer;
5209467b48Spatrick // The inline asm source manager will outlive AsmStr, so make a copy of the
5309467b48Spatrick // string for SourceMgr to own.
5409467b48Spatrick Buffer = MemoryBuffer::getMemBufferCopy(AsmStr, "<inline asm>");
5509467b48Spatrick
5609467b48Spatrick // Tell SrcMgr about this buffer, it takes ownership of the buffer.
5709467b48Spatrick unsigned BufNum = SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
5809467b48Spatrick
5909467b48Spatrick // Store LocMDNode in DiagInfo, using BufNum as an identifier.
6009467b48Spatrick if (LocMDNode) {
6173471bf0Spatrick LocInfos.resize(BufNum);
6273471bf0Spatrick LocInfos[BufNum - 1] = LocMDNode;
6309467b48Spatrick }
6409467b48Spatrick
6509467b48Spatrick return BufNum;
6609467b48Spatrick }
6709467b48Spatrick
6809467b48Spatrick
6909467b48Spatrick /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
emitInlineAsm(StringRef Str,const MCSubtargetInfo & STI,const MCTargetOptions & MCOptions,const MDNode * LocMDNode,InlineAsm::AsmDialect Dialect) const70097a140dSpatrick void AsmPrinter::emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
7109467b48Spatrick const MCTargetOptions &MCOptions,
7209467b48Spatrick const MDNode *LocMDNode,
7309467b48Spatrick InlineAsm::AsmDialect Dialect) const {
7409467b48Spatrick assert(!Str.empty() && "Can't emit empty inline asm block");
7509467b48Spatrick
7609467b48Spatrick // Remember if the buffer is nul terminated or not so we can avoid a copy.
7709467b48Spatrick bool isNullTerminated = Str.back() == 0;
7809467b48Spatrick if (isNullTerminated)
7909467b48Spatrick Str = Str.substr(0, Str.size()-1);
8009467b48Spatrick
8109467b48Spatrick // If the output streamer does not have mature MC support or the integrated
8273471bf0Spatrick // assembler has been disabled or not required, just emit the blob textually.
8309467b48Spatrick // Otherwise parse the asm and emit it via MC support.
8409467b48Spatrick // This is useful in case the asm parser doesn't handle something but the
8509467b48Spatrick // system assembler does.
8609467b48Spatrick const MCAsmInfo *MCAI = TM.getMCAsmInfo();
8709467b48Spatrick assert(MCAI && "No MCAsmInfo");
8809467b48Spatrick if (!MCAI->useIntegratedAssembler() &&
8973471bf0Spatrick !MCAI->parseInlineAsmUsingAsmParser() &&
9009467b48Spatrick !OutStreamer->isIntegratedAssemblerRequired()) {
9109467b48Spatrick emitInlineAsmStart();
92097a140dSpatrick OutStreamer->emitRawText(Str);
9309467b48Spatrick emitInlineAsmEnd(STI, nullptr);
9409467b48Spatrick return;
9509467b48Spatrick }
9609467b48Spatrick
9709467b48Spatrick unsigned BufNum = addInlineAsmDiagBuffer(Str, LocMDNode);
9873471bf0Spatrick SourceMgr &SrcMgr = *MMI->getContext().getInlineSourceManager();
9973471bf0Spatrick SrcMgr.setIncludeDirs(MCOptions.IASSearchPaths);
10009467b48Spatrick
10173471bf0Spatrick std::unique_ptr<MCAsmParser> Parser(
10273471bf0Spatrick createMCAsmParser(SrcMgr, OutContext, *OutStreamer, *MAI, BufNum));
10309467b48Spatrick
10409467b48Spatrick // Do not use assembler-level information for parsing inline assembly.
10509467b48Spatrick OutStreamer->setUseAssemblerInfoForParsing(false);
10609467b48Spatrick
10709467b48Spatrick // We create a new MCInstrInfo here since we might be at the module level
10809467b48Spatrick // and not have a MachineFunction to initialize the TargetInstrInfo from and
10909467b48Spatrick // we only need MCInstrInfo for asm parsing. We create one unconditionally
11009467b48Spatrick // because it's not subtarget dependent.
11109467b48Spatrick std::unique_ptr<MCInstrInfo> MII(TM.getTarget().createMCInstrInfo());
11273471bf0Spatrick assert(MII && "Failed to create instruction info");
11309467b48Spatrick std::unique_ptr<MCTargetAsmParser> TAP(TM.getTarget().createMCAsmParser(
11409467b48Spatrick STI, *Parser, *MII, MCOptions));
11509467b48Spatrick if (!TAP)
11609467b48Spatrick report_fatal_error("Inline asm not supported by this streamer because"
11709467b48Spatrick " we don't have an asm parser for this target\n");
11809467b48Spatrick Parser->setAssemblerDialect(Dialect);
119*d415bd75Srobert Parser->setTargetParser(*TAP);
12009467b48Spatrick // Enable lexing Masm binary and hex integer literals in intel inline
12109467b48Spatrick // assembly.
12209467b48Spatrick if (Dialect == InlineAsm::AD_Intel)
12309467b48Spatrick Parser->getLexer().setLexMasmIntegers(true);
12409467b48Spatrick
12509467b48Spatrick emitInlineAsmStart();
12609467b48Spatrick // Don't implicitly switch to the text section before the asm.
12773471bf0Spatrick (void)Parser->Run(/*NoInitialTextSection*/ true,
12809467b48Spatrick /*NoFinalize*/ true);
12909467b48Spatrick emitInlineAsmEnd(STI, &TAP->getSTI());
13009467b48Spatrick }
13109467b48Spatrick
EmitInlineAsmStr(const char * AsmStr,const MachineInstr * MI,MachineModuleInfo * MMI,const MCAsmInfo * MAI,AsmPrinter * AP,uint64_t LocCookie,raw_ostream & OS)132*d415bd75Srobert static void EmitInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
13373471bf0Spatrick MachineModuleInfo *MMI, const MCAsmInfo *MAI,
13473471bf0Spatrick AsmPrinter *AP, uint64_t LocCookie,
13509467b48Spatrick raw_ostream &OS) {
136*d415bd75Srobert bool InputIsIntelDialect = MI->getInlineAsmDialect() == InlineAsm::AD_Intel;
137*d415bd75Srobert
138*d415bd75Srobert if (InputIsIntelDialect) {
139*d415bd75Srobert // Switch to the inline assembly variant.
140*d415bd75Srobert OS << "\t.intel_syntax\n\t";
141*d415bd75Srobert }
142*d415bd75Srobert
14309467b48Spatrick int CurVariant = -1; // The number of the {.|.|.} region we are in.
14409467b48Spatrick const char *LastEmitted = AsmStr; // One past the last character emitted.
14509467b48Spatrick unsigned NumOperands = MI->getNumOperands();
14609467b48Spatrick
147*d415bd75Srobert int AsmPrinterVariant;
148*d415bd75Srobert if (InputIsIntelDialect)
149*d415bd75Srobert AsmPrinterVariant = 1; // X86MCAsmInfo.cpp's AsmWriterFlavorTy::Intel.
150*d415bd75Srobert else
151*d415bd75Srobert AsmPrinterVariant = MMI->getTarget().unqualifiedInlineAsmVariant();
152*d415bd75Srobert
153*d415bd75Srobert // FIXME: Should this happen for `asm inteldialect` as well?
154*d415bd75Srobert if (!InputIsIntelDialect && MAI->getEmitGNUAsmStartIndentationMarker())
15509467b48Spatrick OS << '\t';
15609467b48Spatrick
15709467b48Spatrick while (*LastEmitted) {
15809467b48Spatrick switch (*LastEmitted) {
15909467b48Spatrick default: {
16009467b48Spatrick // Not a special case, emit the string section literally.
16109467b48Spatrick const char *LiteralEnd = LastEmitted+1;
16209467b48Spatrick while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
16309467b48Spatrick *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
16409467b48Spatrick ++LiteralEnd;
16509467b48Spatrick if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
16609467b48Spatrick OS.write(LastEmitted, LiteralEnd - LastEmitted);
16709467b48Spatrick LastEmitted = LiteralEnd;
16809467b48Spatrick break;
16909467b48Spatrick }
17009467b48Spatrick case '\n':
17109467b48Spatrick ++LastEmitted; // Consume newline character.
17209467b48Spatrick OS << '\n'; // Indent code with newline.
17309467b48Spatrick break;
17409467b48Spatrick case '$': {
17509467b48Spatrick ++LastEmitted; // Consume '$' character.
17609467b48Spatrick bool Done = true;
17709467b48Spatrick
17809467b48Spatrick // Handle escapes.
17909467b48Spatrick switch (*LastEmitted) {
18009467b48Spatrick default: Done = false; break;
18109467b48Spatrick case '$': // $$ -> $
182*d415bd75Srobert if (!InputIsIntelDialect)
18309467b48Spatrick if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
18409467b48Spatrick OS << '$';
18509467b48Spatrick ++LastEmitted; // Consume second '$' character.
18609467b48Spatrick break;
18709467b48Spatrick case '(': // $( -> same as GCC's { character.
18809467b48Spatrick ++LastEmitted; // Consume '(' character.
18909467b48Spatrick if (CurVariant != -1)
19009467b48Spatrick report_fatal_error("Nested variants found in inline asm string: '" +
19109467b48Spatrick Twine(AsmStr) + "'");
19209467b48Spatrick CurVariant = 0; // We're in the first variant now.
19309467b48Spatrick break;
19409467b48Spatrick case '|':
195*d415bd75Srobert ++LastEmitted; // Consume '|' character.
19609467b48Spatrick if (CurVariant == -1)
197*d415bd75Srobert OS << '|'; // This is gcc's behavior for | outside a variant.
19809467b48Spatrick else
19909467b48Spatrick ++CurVariant; // We're in the next variant.
20009467b48Spatrick break;
20109467b48Spatrick case ')': // $) -> same as GCC's } char.
202*d415bd75Srobert ++LastEmitted; // Consume ')' character.
20309467b48Spatrick if (CurVariant == -1)
204*d415bd75Srobert OS << '}'; // This is gcc's behavior for } outside a variant.
20509467b48Spatrick else
20609467b48Spatrick CurVariant = -1;
20709467b48Spatrick break;
20809467b48Spatrick }
20909467b48Spatrick if (Done) break;
21009467b48Spatrick
21109467b48Spatrick bool HasCurlyBraces = false;
21209467b48Spatrick if (*LastEmitted == '{') { // ${variable}
21309467b48Spatrick ++LastEmitted; // Consume '{' character.
21409467b48Spatrick HasCurlyBraces = true;
21509467b48Spatrick }
21609467b48Spatrick
21709467b48Spatrick // If we have ${:foo}, then this is not a real operand reference, it is a
21809467b48Spatrick // "magic" string reference, just like in .td files. Arrange to call
21909467b48Spatrick // PrintSpecial.
22009467b48Spatrick if (HasCurlyBraces && *LastEmitted == ':') {
22109467b48Spatrick ++LastEmitted;
22209467b48Spatrick const char *StrStart = LastEmitted;
22309467b48Spatrick const char *StrEnd = strchr(StrStart, '}');
22409467b48Spatrick if (!StrEnd)
22509467b48Spatrick report_fatal_error("Unterminated ${:foo} operand in inline asm"
22609467b48Spatrick " string: '" + Twine(AsmStr) + "'");
227*d415bd75Srobert if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
228*d415bd75Srobert AP->PrintSpecial(MI, OS, StringRef(StrStart, StrEnd - StrStart));
22909467b48Spatrick LastEmitted = StrEnd+1;
23009467b48Spatrick break;
23109467b48Spatrick }
23209467b48Spatrick
23309467b48Spatrick const char *IDStart = LastEmitted;
23409467b48Spatrick const char *IDEnd = IDStart;
23573471bf0Spatrick while (isDigit(*IDEnd))
23673471bf0Spatrick ++IDEnd;
23709467b48Spatrick
23809467b48Spatrick unsigned Val;
23909467b48Spatrick if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
24009467b48Spatrick report_fatal_error("Bad $ operand number in inline asm string: '" +
24109467b48Spatrick Twine(AsmStr) + "'");
24209467b48Spatrick LastEmitted = IDEnd;
24309467b48Spatrick
244*d415bd75Srobert if (Val >= NumOperands - 1)
245*d415bd75Srobert report_fatal_error("Invalid $ operand number in inline asm string: '" +
246*d415bd75Srobert Twine(AsmStr) + "'");
247*d415bd75Srobert
24809467b48Spatrick char Modifier[2] = { 0, 0 };
24909467b48Spatrick
25009467b48Spatrick if (HasCurlyBraces) {
25109467b48Spatrick // If we have curly braces, check for a modifier character. This
25209467b48Spatrick // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
25309467b48Spatrick if (*LastEmitted == ':') {
25409467b48Spatrick ++LastEmitted; // Consume ':' character.
25509467b48Spatrick if (*LastEmitted == 0)
25609467b48Spatrick report_fatal_error("Bad ${:} expression in inline asm string: '" +
25709467b48Spatrick Twine(AsmStr) + "'");
25809467b48Spatrick
25909467b48Spatrick Modifier[0] = *LastEmitted;
26009467b48Spatrick ++LastEmitted; // Consume modifier character.
26109467b48Spatrick }
26209467b48Spatrick
26309467b48Spatrick if (*LastEmitted != '}')
26409467b48Spatrick report_fatal_error("Bad ${} expression in inline asm string: '" +
26509467b48Spatrick Twine(AsmStr) + "'");
26609467b48Spatrick ++LastEmitted; // Consume '}' character.
26709467b48Spatrick }
26809467b48Spatrick
26909467b48Spatrick // Okay, we finally have a value number. Ask the target to print this
27009467b48Spatrick // operand!
27109467b48Spatrick if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
27209467b48Spatrick unsigned OpNo = InlineAsm::MIOp_FirstOperand;
27309467b48Spatrick
27409467b48Spatrick bool Error = false;
27509467b48Spatrick
27609467b48Spatrick // Scan to find the machine operand number for the operand.
27709467b48Spatrick for (; Val; --Val) {
278*d415bd75Srobert if (OpNo >= MI->getNumOperands())
279*d415bd75Srobert break;
28009467b48Spatrick unsigned OpFlags = MI->getOperand(OpNo).getImm();
28109467b48Spatrick OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
28209467b48Spatrick }
28309467b48Spatrick
28409467b48Spatrick // We may have a location metadata attached to the end of the
28509467b48Spatrick // instruction, and at no point should see metadata at any
28609467b48Spatrick // other point while processing. It's an error if so.
287*d415bd75Srobert if (OpNo >= MI->getNumOperands() || MI->getOperand(OpNo).isMetadata()) {
28809467b48Spatrick Error = true;
28909467b48Spatrick } else {
29009467b48Spatrick unsigned OpFlags = MI->getOperand(OpNo).getImm();
29109467b48Spatrick ++OpNo; // Skip over the ID number.
29209467b48Spatrick
29309467b48Spatrick // FIXME: Shouldn't arch-independent output template handling go into
29409467b48Spatrick // PrintAsmOperand?
29509467b48Spatrick // Labels are target independent.
29609467b48Spatrick if (MI->getOperand(OpNo).isBlockAddress()) {
29709467b48Spatrick const BlockAddress *BA = MI->getOperand(OpNo).getBlockAddress();
29809467b48Spatrick MCSymbol *Sym = AP->GetBlockAddressSymbol(BA);
29909467b48Spatrick Sym->print(OS, AP->MAI);
30009467b48Spatrick MMI->getContext().registerInlineAsmLabel(Sym);
30109467b48Spatrick } else if (MI->getOperand(OpNo).isMBB()) {
30209467b48Spatrick const MCSymbol *Sym = MI->getOperand(OpNo).getMBB()->getSymbol();
30309467b48Spatrick Sym->print(OS, AP->MAI);
30409467b48Spatrick } else if (InlineAsm::isMemKind(OpFlags)) {
30509467b48Spatrick Error = AP->PrintAsmMemoryOperand(
30609467b48Spatrick MI, OpNo, Modifier[0] ? Modifier : nullptr, OS);
30709467b48Spatrick } else {
30809467b48Spatrick Error = AP->PrintAsmOperand(MI, OpNo,
30909467b48Spatrick Modifier[0] ? Modifier : nullptr, OS);
31009467b48Spatrick }
31109467b48Spatrick }
31209467b48Spatrick if (Error) {
31309467b48Spatrick std::string msg;
31409467b48Spatrick raw_string_ostream Msg(msg);
31509467b48Spatrick Msg << "invalid operand in inline asm: '" << AsmStr << "'";
31609467b48Spatrick MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
31709467b48Spatrick }
31809467b48Spatrick }
31909467b48Spatrick break;
32009467b48Spatrick }
32109467b48Spatrick }
32209467b48Spatrick }
323*d415bd75Srobert if (InputIsIntelDialect)
324*d415bd75Srobert OS << "\n\t.att_syntax";
32509467b48Spatrick OS << '\n' << (char)0; // null terminate string.
32609467b48Spatrick }
32709467b48Spatrick
328097a140dSpatrick /// This method formats and emits the specified machine instruction that is an
329097a140dSpatrick /// inline asm.
emitInlineAsm(const MachineInstr * MI) const330097a140dSpatrick void AsmPrinter::emitInlineAsm(const MachineInstr *MI) const {
33109467b48Spatrick assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
33209467b48Spatrick
33309467b48Spatrick // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
334*d415bd75Srobert const char *AsmStr = MI->getOperand(0).getSymbolName();
33509467b48Spatrick
33609467b48Spatrick // If this asmstr is empty, just print the #APP/#NOAPP markers.
33709467b48Spatrick // These are useful to see where empty asm's wound up.
33809467b48Spatrick if (AsmStr[0] == 0) {
33909467b48Spatrick OutStreamer->emitRawComment(MAI->getInlineAsmStart());
34009467b48Spatrick OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
34109467b48Spatrick return;
34209467b48Spatrick }
34309467b48Spatrick
34409467b48Spatrick // Emit the #APP start marker. This has to happen even if verbose-asm isn't
34509467b48Spatrick // enabled, so we use emitRawComment.
34609467b48Spatrick OutStreamer->emitRawComment(MAI->getInlineAsmStart());
34709467b48Spatrick
34809467b48Spatrick // Get the !srcloc metadata node if we have it, and decode the loc cookie from
34909467b48Spatrick // it.
35073471bf0Spatrick uint64_t LocCookie = 0;
35109467b48Spatrick const MDNode *LocMD = nullptr;
352*d415bd75Srobert for (const MachineOperand &MO : llvm::reverse(MI->operands())) {
353*d415bd75Srobert if (MO.isMetadata() && (LocMD = MO.getMetadata()) &&
35409467b48Spatrick LocMD->getNumOperands() != 0) {
35509467b48Spatrick if (const ConstantInt *CI =
35609467b48Spatrick mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
35709467b48Spatrick LocCookie = CI->getZExtValue();
35809467b48Spatrick break;
35909467b48Spatrick }
36009467b48Spatrick }
36109467b48Spatrick }
36209467b48Spatrick
36309467b48Spatrick // Emit the inline asm to a temporary string so we can emit it through
36409467b48Spatrick // EmitInlineAsm.
36509467b48Spatrick SmallString<256> StringData;
36609467b48Spatrick raw_svector_ostream OS(StringData);
36709467b48Spatrick
36809467b48Spatrick AsmPrinter *AP = const_cast<AsmPrinter*>(this);
369*d415bd75Srobert EmitInlineAsmStr(AsmStr, MI, MMI, MAI, AP, LocCookie, OS);
37009467b48Spatrick
37109467b48Spatrick // Emit warnings if we use reserved registers on the clobber list, as
37273471bf0Spatrick // that might lead to undefined behaviour.
37373471bf0Spatrick SmallVector<Register, 8> RestrRegs;
37473471bf0Spatrick const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
37509467b48Spatrick // Start with the first operand descriptor, and iterate over them.
37609467b48Spatrick for (unsigned I = InlineAsm::MIOp_FirstOperand, NumOps = MI->getNumOperands();
37709467b48Spatrick I < NumOps; ++I) {
37809467b48Spatrick const MachineOperand &MO = MI->getOperand(I);
37973471bf0Spatrick if (!MO.isImm())
38073471bf0Spatrick continue;
38109467b48Spatrick unsigned Flags = MO.getImm();
38273471bf0Spatrick if (InlineAsm::getKind(Flags) == InlineAsm::Kind_Clobber) {
38373471bf0Spatrick Register Reg = MI->getOperand(I + 1).getReg();
38473471bf0Spatrick if (!TRI->isAsmClobberable(*MF, Reg))
38573471bf0Spatrick RestrRegs.push_back(Reg);
38609467b48Spatrick }
38709467b48Spatrick // Skip to one before the next operand descriptor, if it exists.
38809467b48Spatrick I += InlineAsm::getNumOperandRegisters(Flags);
38909467b48Spatrick }
39009467b48Spatrick
39109467b48Spatrick if (!RestrRegs.empty()) {
39209467b48Spatrick std::string Msg = "inline asm clobber list contains reserved registers: ";
39373471bf0Spatrick ListSeparator LS;
394*d415bd75Srobert for (const Register RR : RestrRegs) {
39573471bf0Spatrick Msg += LS;
396*d415bd75Srobert Msg += TRI->getRegAsmName(RR);
39709467b48Spatrick }
39873471bf0Spatrick const char *Note =
39973471bf0Spatrick "Reserved registers on the clobber list may not be "
40009467b48Spatrick "preserved across the asm statement, and clobbering them may "
40109467b48Spatrick "lead to undefined behaviour.";
40273471bf0Spatrick MMI->getModule()->getContext().diagnose(DiagnosticInfoInlineAsm(
403*d415bd75Srobert LocCookie, Msg, DiagnosticSeverity::DS_Warning));
40473471bf0Spatrick MMI->getModule()->getContext().diagnose(
40573471bf0Spatrick DiagnosticInfoInlineAsm(LocCookie, Note, DiagnosticSeverity::DS_Note));
406*d415bd75Srobert
407*d415bd75Srobert for (const Register RR : RestrRegs) {
408*d415bd75Srobert if (std::optional<std::string> reason =
409*d415bd75Srobert TRI->explainReservedReg(*MF, RR)) {
410*d415bd75Srobert MMI->getModule()->getContext().diagnose(DiagnosticInfoInlineAsm(
411*d415bd75Srobert LocCookie, *reason, DiagnosticSeverity::DS_Note));
412*d415bd75Srobert }
413*d415bd75Srobert }
41409467b48Spatrick }
41509467b48Spatrick
416097a140dSpatrick emitInlineAsm(OS.str(), getSubtargetInfo(), TM.Options.MCOptions, LocMD,
41709467b48Spatrick MI->getInlineAsmDialect());
41809467b48Spatrick
41909467b48Spatrick // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
42009467b48Spatrick // enabled, so we use emitRawComment.
42109467b48Spatrick OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
42209467b48Spatrick }
42309467b48Spatrick
42409467b48Spatrick /// PrintSpecial - Print information related to the specified machine instr
42509467b48Spatrick /// that is independent of the operand, and may be independent of the instr
42609467b48Spatrick /// itself. This can be useful for portably encoding the comment character
42709467b48Spatrick /// or other bits of target-specific knowledge into the asmstrings. The
42809467b48Spatrick /// syntax used is ${:comment}. Targets can override this to add support
42909467b48Spatrick /// for their own strange codes.
PrintSpecial(const MachineInstr * MI,raw_ostream & OS,StringRef Code) const43009467b48Spatrick void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
431*d415bd75Srobert StringRef Code) const {
432*d415bd75Srobert if (Code == "private") {
43309467b48Spatrick const DataLayout &DL = MF->getDataLayout();
43409467b48Spatrick OS << DL.getPrivateGlobalPrefix();
435*d415bd75Srobert } else if (Code == "comment") {
43609467b48Spatrick OS << MAI->getCommentString();
437*d415bd75Srobert } else if (Code == "uid") {
43809467b48Spatrick // Comparing the address of MI isn't sufficient, because machineinstrs may
43909467b48Spatrick // be allocated to the same address across functions.
44009467b48Spatrick
44109467b48Spatrick // If this is a new LastFn instruction, bump the counter.
44209467b48Spatrick if (LastMI != MI || LastFn != getFunctionNumber()) {
44309467b48Spatrick ++Counter;
44409467b48Spatrick LastMI = MI;
44509467b48Spatrick LastFn = getFunctionNumber();
44609467b48Spatrick }
44709467b48Spatrick OS << Counter;
44809467b48Spatrick } else {
44909467b48Spatrick std::string msg;
45009467b48Spatrick raw_string_ostream Msg(msg);
45109467b48Spatrick Msg << "Unknown special formatter '" << Code
45209467b48Spatrick << "' for machine instr: " << *MI;
453*d415bd75Srobert report_fatal_error(Twine(Msg.str()));
45409467b48Spatrick }
45509467b48Spatrick }
45609467b48Spatrick
PrintSymbolOperand(const MachineOperand & MO,raw_ostream & OS)45709467b48Spatrick void AsmPrinter::PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS) {
45809467b48Spatrick assert(MO.isGlobal() && "caller should check MO.isGlobal");
45973471bf0Spatrick getSymbolPreferLocal(*MO.getGlobal())->print(OS, MAI);
46009467b48Spatrick printOffset(MO.getOffset(), OS);
46109467b48Spatrick }
46209467b48Spatrick
46309467b48Spatrick /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
46409467b48Spatrick /// instruction, using the specified assembler variant. Targets should
46509467b48Spatrick /// override this to format as appropriate for machine specific ExtraCodes
46609467b48Spatrick /// or when the arch-independent handling would be too complex otherwise.
PrintAsmOperand(const MachineInstr * MI,unsigned OpNo,const char * ExtraCode,raw_ostream & O)46709467b48Spatrick bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
46809467b48Spatrick const char *ExtraCode, raw_ostream &O) {
46909467b48Spatrick // Does this asm operand have a single letter operand modifier?
47009467b48Spatrick if (ExtraCode && ExtraCode[0]) {
47109467b48Spatrick if (ExtraCode[1] != 0) return true; // Unknown modifier.
47209467b48Spatrick
47309467b48Spatrick // https://gcc.gnu.org/onlinedocs/gccint/Output-Template.html
47409467b48Spatrick const MachineOperand &MO = MI->getOperand(OpNo);
47509467b48Spatrick switch (ExtraCode[0]) {
47609467b48Spatrick default:
47709467b48Spatrick return true; // Unknown modifier.
47809467b48Spatrick case 'a': // Print as memory address.
47909467b48Spatrick if (MO.isReg()) {
48009467b48Spatrick PrintAsmMemoryOperand(MI, OpNo, nullptr, O);
48109467b48Spatrick return false;
48209467b48Spatrick }
483*d415bd75Srobert [[fallthrough]]; // GCC allows '%a' to behave like '%c' with immediates.
48409467b48Spatrick case 'c': // Substitute immediate value without immediate syntax
48509467b48Spatrick if (MO.isImm()) {
48609467b48Spatrick O << MO.getImm();
48709467b48Spatrick return false;
48809467b48Spatrick }
48909467b48Spatrick if (MO.isGlobal()) {
49009467b48Spatrick PrintSymbolOperand(MO, O);
49109467b48Spatrick return false;
49209467b48Spatrick }
49309467b48Spatrick return true;
49409467b48Spatrick case 'n': // Negate the immediate constant.
49509467b48Spatrick if (!MO.isImm())
49609467b48Spatrick return true;
49709467b48Spatrick O << -MO.getImm();
49809467b48Spatrick return false;
49909467b48Spatrick case 's': // The GCC deprecated s modifier
50009467b48Spatrick if (!MO.isImm())
50109467b48Spatrick return true;
50209467b48Spatrick O << ((32 - MO.getImm()) & 31);
50309467b48Spatrick return false;
50409467b48Spatrick }
50509467b48Spatrick }
50609467b48Spatrick return true;
50709467b48Spatrick }
50809467b48Spatrick
PrintAsmMemoryOperand(const MachineInstr * MI,unsigned OpNo,const char * ExtraCode,raw_ostream & O)50909467b48Spatrick bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
51009467b48Spatrick const char *ExtraCode, raw_ostream &O) {
51109467b48Spatrick // Target doesn't support this yet!
51209467b48Spatrick return true;
51309467b48Spatrick }
51409467b48Spatrick
emitInlineAsmStart() const51509467b48Spatrick void AsmPrinter::emitInlineAsmStart() const {}
51609467b48Spatrick
emitInlineAsmEnd(const MCSubtargetInfo & StartInfo,const MCSubtargetInfo * EndInfo) const51709467b48Spatrick void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
51809467b48Spatrick const MCSubtargetInfo *EndInfo) const {}
519