xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===-- AsmPrinterInlineAsm.cpp - AsmPrinter Inline Asm Handling ----------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the inline assembler pieces of the AsmPrinter class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
14e8d8bef9SDimitry Andric #include "llvm/ADT/SmallVector.h"
1506c3fb27SDimitry Andric #include "llvm/ADT/StringExtras.h"
160b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
2281ad6265SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
230b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
240b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
25fe6060f1SDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
260b57cec5SDimitry Andric #include "llvm/IR/InlineAsm.h"
270b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
280b57cec5SDimitry Andric #include "llvm/IR/Module.h"
290b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
3081ad6265SDimitry Andric #include "llvm/MC/MCInstrInfo.h"
3181ad6265SDimitry Andric #include "llvm/MC/MCParser/MCAsmLexer.h"
320b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCTargetAsmParser.h"
330b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
340b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
35349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
360b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
370b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
380b57cec5SDimitry Andric #include "llvm/Support/SourceMgr.h"
390b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
400b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
410b57cec5SDimitry Andric using namespace llvm;
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric #define DEBUG_TYPE "asm-printer"
440b57cec5SDimitry Andric 
addInlineAsmDiagBuffer(StringRef AsmStr,const MDNode * LocMDNode) const450b57cec5SDimitry Andric unsigned AsmPrinter::addInlineAsmDiagBuffer(StringRef AsmStr,
460b57cec5SDimitry Andric                                             const MDNode *LocMDNode) const {
470b57cec5SDimitry Andric   MCContext &Context = MMI->getContext();
48fe6060f1SDimitry Andric   Context.initInlineSourceManager();
49fe6060f1SDimitry Andric   SourceMgr &SrcMgr = *Context.getInlineSourceManager();
50fe6060f1SDimitry Andric   std::vector<const MDNode *> &LocInfos = Context.getLocInfos();
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   std::unique_ptr<MemoryBuffer> Buffer;
530b57cec5SDimitry Andric   // The inline asm source manager will outlive AsmStr, so make a copy of the
540b57cec5SDimitry Andric   // string for SourceMgr to own.
550b57cec5SDimitry Andric   Buffer = MemoryBuffer::getMemBufferCopy(AsmStr, "<inline asm>");
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   // Tell SrcMgr about this buffer, it takes ownership of the buffer.
580b57cec5SDimitry Andric   unsigned BufNum = SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   // Store LocMDNode in DiagInfo, using BufNum as an identifier.
610b57cec5SDimitry Andric   if (LocMDNode) {
62fe6060f1SDimitry Andric     LocInfos.resize(BufNum);
63fe6060f1SDimitry Andric     LocInfos[BufNum - 1] = LocMDNode;
640b57cec5SDimitry Andric   }
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   return BufNum;
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric /// 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) const715ffd83dbSDimitry Andric void AsmPrinter::emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
720b57cec5SDimitry Andric                                const MCTargetOptions &MCOptions,
730b57cec5SDimitry Andric                                const MDNode *LocMDNode,
740b57cec5SDimitry Andric                                InlineAsm::AsmDialect Dialect) const {
750b57cec5SDimitry Andric   assert(!Str.empty() && "Can't emit empty inline asm block");
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   // Remember if the buffer is nul terminated or not so we can avoid a copy.
780b57cec5SDimitry Andric   bool isNullTerminated = Str.back() == 0;
790b57cec5SDimitry Andric   if (isNullTerminated)
800b57cec5SDimitry Andric     Str = Str.substr(0, Str.size()-1);
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   // If the output streamer does not have mature MC support or the integrated
83fe6060f1SDimitry Andric   // assembler has been disabled or not required, just emit the blob textually.
840b57cec5SDimitry Andric   // Otherwise parse the asm and emit it via MC support.
850b57cec5SDimitry Andric   // This is useful in case the asm parser doesn't handle something but the
860b57cec5SDimitry Andric   // system assembler does.
870b57cec5SDimitry Andric   const MCAsmInfo *MCAI = TM.getMCAsmInfo();
880b57cec5SDimitry Andric   assert(MCAI && "No MCAsmInfo");
890b57cec5SDimitry Andric   if (!MCAI->useIntegratedAssembler() &&
90fe6060f1SDimitry Andric       !MCAI->parseInlineAsmUsingAsmParser() &&
910b57cec5SDimitry Andric       !OutStreamer->isIntegratedAssemblerRequired()) {
920b57cec5SDimitry Andric     emitInlineAsmStart();
935ffd83dbSDimitry Andric     OutStreamer->emitRawText(Str);
940b57cec5SDimitry Andric     emitInlineAsmEnd(STI, nullptr);
950b57cec5SDimitry Andric     return;
960b57cec5SDimitry Andric   }
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   unsigned BufNum = addInlineAsmDiagBuffer(Str, LocMDNode);
99fe6060f1SDimitry Andric   SourceMgr &SrcMgr = *MMI->getContext().getInlineSourceManager();
100fe6060f1SDimitry Andric   SrcMgr.setIncludeDirs(MCOptions.IASSearchPaths);
1010b57cec5SDimitry Andric 
102fe6060f1SDimitry Andric   std::unique_ptr<MCAsmParser> Parser(
103fe6060f1SDimitry Andric       createMCAsmParser(SrcMgr, OutContext, *OutStreamer, *MAI, BufNum));
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric   // We create a new MCInstrInfo here since we might be at the module level
1060b57cec5SDimitry Andric   // and not have a MachineFunction to initialize the TargetInstrInfo from and
1070b57cec5SDimitry Andric   // we only need MCInstrInfo for asm parsing. We create one unconditionally
1080b57cec5SDimitry Andric   // because it's not subtarget dependent.
1090b57cec5SDimitry Andric   std::unique_ptr<MCInstrInfo> MII(TM.getTarget().createMCInstrInfo());
110e8d8bef9SDimitry Andric   assert(MII && "Failed to create instruction info");
1110b57cec5SDimitry Andric   std::unique_ptr<MCTargetAsmParser> TAP(TM.getTarget().createMCAsmParser(
1120b57cec5SDimitry Andric       STI, *Parser, *MII, MCOptions));
1130b57cec5SDimitry Andric   if (!TAP)
1140b57cec5SDimitry Andric     report_fatal_error("Inline asm not supported by this streamer because"
1150b57cec5SDimitry Andric                        " we don't have an asm parser for this target\n");
116*0fca6ea1SDimitry Andric 
117*0fca6ea1SDimitry Andric   // Respect inlineasm dialect on X86 targets only
118*0fca6ea1SDimitry Andric   if (TM.getTargetTriple().isX86()) {
1190b57cec5SDimitry Andric     Parser->setAssemblerDialect(Dialect);
1200b57cec5SDimitry Andric     // Enable lexing Masm binary and hex integer literals in intel inline
1210b57cec5SDimitry Andric     // assembly.
1220b57cec5SDimitry Andric     if (Dialect == InlineAsm::AD_Intel)
1230b57cec5SDimitry Andric       Parser->getLexer().setLexMasmIntegers(true);
124*0fca6ea1SDimitry Andric   }
125*0fca6ea1SDimitry Andric   Parser->setTargetParser(*TAP);
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric   emitInlineAsmStart();
1280b57cec5SDimitry Andric   // Don't implicitly switch to the text section before the asm.
129fe6060f1SDimitry Andric   (void)Parser->Run(/*NoInitialTextSection*/ true,
1300b57cec5SDimitry Andric                     /*NoFinalize*/ true);
1310b57cec5SDimitry Andric   emitInlineAsmEnd(STI, &TAP->getSTI());
1320b57cec5SDimitry Andric }
1330b57cec5SDimitry Andric 
EmitInlineAsmStr(const char * AsmStr,const MachineInstr * MI,MachineModuleInfo * MMI,const MCAsmInfo * MAI,AsmPrinter * AP,uint64_t LocCookie,raw_ostream & OS)1344824e7fdSDimitry Andric static void EmitInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
135349cc55cSDimitry Andric                              MachineModuleInfo *MMI, const MCAsmInfo *MAI,
136349cc55cSDimitry Andric                              AsmPrinter *AP, uint64_t LocCookie,
137349cc55cSDimitry Andric                              raw_ostream &OS) {
1384824e7fdSDimitry Andric   bool InputIsIntelDialect = MI->getInlineAsmDialect() == InlineAsm::AD_Intel;
1394824e7fdSDimitry Andric 
1404824e7fdSDimitry Andric   if (InputIsIntelDialect) {
1410b57cec5SDimitry Andric     // Switch to the inline assembly variant.
1420b57cec5SDimitry Andric     OS << "\t.intel_syntax\n\t";
1434824e7fdSDimitry Andric   }
1440b57cec5SDimitry Andric 
145349cc55cSDimitry Andric   int CurVariant = -1; // The number of the {.|.|.} region we are in.
1460b57cec5SDimitry Andric   const char *LastEmitted = AsmStr; // One past the last character emitted.
1470b57cec5SDimitry Andric   unsigned NumOperands = MI->getNumOperands();
1480b57cec5SDimitry Andric 
1494824e7fdSDimitry Andric   int AsmPrinterVariant;
1504824e7fdSDimitry Andric   if (InputIsIntelDialect)
1514824e7fdSDimitry Andric     AsmPrinterVariant = 1; // X86MCAsmInfo.cpp's AsmWriterFlavorTy::Intel.
152349cc55cSDimitry Andric   else
1534824e7fdSDimitry Andric     AsmPrinterVariant = MMI->getTarget().unqualifiedInlineAsmVariant();
1540b57cec5SDimitry Andric 
1554824e7fdSDimitry Andric   // FIXME: Should this happen for `asm inteldialect` as well?
1564824e7fdSDimitry Andric   if (!InputIsIntelDialect && MAI->getEmitGNUAsmStartIndentationMarker())
1570b57cec5SDimitry Andric     OS << '\t';
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric   while (*LastEmitted) {
1600b57cec5SDimitry Andric     switch (*LastEmitted) {
1610b57cec5SDimitry Andric     default: {
1620b57cec5SDimitry Andric       // Not a special case, emit the string section literally.
1630b57cec5SDimitry Andric       const char *LiteralEnd = LastEmitted+1;
1640b57cec5SDimitry Andric       while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1650b57cec5SDimitry Andric              *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1660b57cec5SDimitry Andric         ++LiteralEnd;
1670b57cec5SDimitry Andric       if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1680b57cec5SDimitry Andric         OS.write(LastEmitted, LiteralEnd - LastEmitted);
1690b57cec5SDimitry Andric       LastEmitted = LiteralEnd;
1700b57cec5SDimitry Andric       break;
1710b57cec5SDimitry Andric     }
1720b57cec5SDimitry Andric     case '\n':
1730b57cec5SDimitry Andric       ++LastEmitted;   // Consume newline character.
1740b57cec5SDimitry Andric       OS << '\n';      // Indent code with newline.
1750b57cec5SDimitry Andric       break;
1760b57cec5SDimitry Andric     case '$': {
1770b57cec5SDimitry Andric       ++LastEmitted;   // Consume '$' character.
1780b57cec5SDimitry Andric       bool Done = true;
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric       // Handle escapes.
1810b57cec5SDimitry Andric       switch (*LastEmitted) {
1820b57cec5SDimitry Andric       default: Done = false; break;
1830b57cec5SDimitry Andric       case '$':     // $$ -> $
1844824e7fdSDimitry Andric         if (!InputIsIntelDialect)
1850b57cec5SDimitry Andric           if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1860b57cec5SDimitry Andric             OS << '$';
1870b57cec5SDimitry Andric         ++LastEmitted;  // Consume second '$' character.
1880b57cec5SDimitry Andric         break;
1890b57cec5SDimitry Andric       case '(':        // $( -> same as GCC's { character.
1900b57cec5SDimitry Andric         ++LastEmitted; // Consume '(' character.
1910b57cec5SDimitry Andric         if (CurVariant != -1)
1920b57cec5SDimitry Andric           report_fatal_error("Nested variants found in inline asm string: '" +
1930b57cec5SDimitry Andric                              Twine(AsmStr) + "'");
1940b57cec5SDimitry Andric         CurVariant = 0; // We're in the first variant now.
1950b57cec5SDimitry Andric         break;
1960b57cec5SDimitry Andric       case '|':
197349cc55cSDimitry Andric         ++LastEmitted; // Consume '|' character.
1980b57cec5SDimitry Andric         if (CurVariant == -1)
199349cc55cSDimitry Andric           OS << '|'; // This is gcc's behavior for | outside a variant.
2000b57cec5SDimitry Andric         else
2010b57cec5SDimitry Andric           ++CurVariant; // We're in the next variant.
2020b57cec5SDimitry Andric         break;
2030b57cec5SDimitry Andric       case ')':        // $) -> same as GCC's } char.
204349cc55cSDimitry Andric         ++LastEmitted; // Consume ')' character.
2050b57cec5SDimitry Andric         if (CurVariant == -1)
206349cc55cSDimitry Andric           OS << '}'; // This is gcc's behavior for } outside a variant.
2070b57cec5SDimitry Andric         else
2080b57cec5SDimitry Andric           CurVariant = -1;
2090b57cec5SDimitry Andric         break;
2100b57cec5SDimitry Andric       }
2110b57cec5SDimitry Andric       if (Done) break;
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric       bool HasCurlyBraces = false;
2140b57cec5SDimitry Andric       if (*LastEmitted == '{') {     // ${variable}
2150b57cec5SDimitry Andric         ++LastEmitted;               // Consume '{' character.
2160b57cec5SDimitry Andric         HasCurlyBraces = true;
2170b57cec5SDimitry Andric       }
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric       // If we have ${:foo}, then this is not a real operand reference, it is a
2200b57cec5SDimitry Andric       // "magic" string reference, just like in .td files.  Arrange to call
2210b57cec5SDimitry Andric       // PrintSpecial.
2220b57cec5SDimitry Andric       if (HasCurlyBraces && *LastEmitted == ':') {
2230b57cec5SDimitry Andric         ++LastEmitted;
2240b57cec5SDimitry Andric         const char *StrStart = LastEmitted;
2250b57cec5SDimitry Andric         const char *StrEnd = strchr(StrStart, '}');
2260b57cec5SDimitry Andric         if (!StrEnd)
2270b57cec5SDimitry Andric           report_fatal_error("Unterminated ${:foo} operand in inline asm"
2280b57cec5SDimitry Andric                              " string: '" + Twine(AsmStr) + "'");
229349cc55cSDimitry Andric         if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
230349cc55cSDimitry Andric           AP->PrintSpecial(MI, OS, StringRef(StrStart, StrEnd - StrStart));
2310b57cec5SDimitry Andric         LastEmitted = StrEnd+1;
2320b57cec5SDimitry Andric         break;
2330b57cec5SDimitry Andric       }
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric       const char *IDStart = LastEmitted;
2360b57cec5SDimitry Andric       const char *IDEnd = IDStart;
237e8d8bef9SDimitry Andric       while (isDigit(*IDEnd))
238e8d8bef9SDimitry Andric         ++IDEnd;
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric       unsigned Val;
2410b57cec5SDimitry Andric       if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
2420b57cec5SDimitry Andric         report_fatal_error("Bad $ operand number in inline asm string: '" +
2430b57cec5SDimitry Andric                            Twine(AsmStr) + "'");
2440b57cec5SDimitry Andric       LastEmitted = IDEnd;
2450b57cec5SDimitry Andric 
246349cc55cSDimitry Andric       if (Val >= NumOperands - 1)
247349cc55cSDimitry Andric         report_fatal_error("Invalid $ operand number in inline asm string: '" +
248349cc55cSDimitry Andric                            Twine(AsmStr) + "'");
249349cc55cSDimitry Andric 
2500b57cec5SDimitry Andric       char Modifier[2] = { 0, 0 };
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric       if (HasCurlyBraces) {
2530b57cec5SDimitry Andric         // If we have curly braces, check for a modifier character.  This
2540b57cec5SDimitry Andric         // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
2550b57cec5SDimitry Andric         if (*LastEmitted == ':') {
2560b57cec5SDimitry Andric           ++LastEmitted;    // Consume ':' character.
2570b57cec5SDimitry Andric           if (*LastEmitted == 0)
2580b57cec5SDimitry Andric             report_fatal_error("Bad ${:} expression in inline asm string: '" +
2590b57cec5SDimitry Andric                                Twine(AsmStr) + "'");
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric           Modifier[0] = *LastEmitted;
2620b57cec5SDimitry Andric           ++LastEmitted;    // Consume modifier character.
2630b57cec5SDimitry Andric         }
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric         if (*LastEmitted != '}')
2660b57cec5SDimitry Andric           report_fatal_error("Bad ${} expression in inline asm string: '" +
2670b57cec5SDimitry Andric                              Twine(AsmStr) + "'");
2680b57cec5SDimitry Andric         ++LastEmitted;    // Consume '}' character.
2690b57cec5SDimitry Andric       }
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric       // Okay, we finally have a value number.  Ask the target to print this
2720b57cec5SDimitry Andric       // operand!
2730b57cec5SDimitry Andric       if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
2740b57cec5SDimitry Andric         unsigned OpNo = InlineAsm::MIOp_FirstOperand;
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric         bool Error = false;
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric         // Scan to find the machine operand number for the operand.
2790b57cec5SDimitry Andric         for (; Val; --Val) {
280349cc55cSDimitry Andric           if (OpNo >= MI->getNumOperands())
281349cc55cSDimitry Andric             break;
2825f757f3fSDimitry Andric           const InlineAsm::Flag F(MI->getOperand(OpNo).getImm());
2835f757f3fSDimitry Andric           OpNo += F.getNumOperandRegisters() + 1;
2840b57cec5SDimitry Andric         }
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric         // We may have a location metadata attached to the end of the
2870b57cec5SDimitry Andric         // instruction, and at no point should see metadata at any
2880b57cec5SDimitry Andric         // other point while processing. It's an error if so.
289349cc55cSDimitry Andric         if (OpNo >= MI->getNumOperands() || MI->getOperand(OpNo).isMetadata()) {
2900b57cec5SDimitry Andric           Error = true;
2910b57cec5SDimitry Andric         } else {
2925f757f3fSDimitry Andric           const InlineAsm::Flag F(MI->getOperand(OpNo).getImm());
2930b57cec5SDimitry Andric           ++OpNo; // Skip over the ID number.
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric           // FIXME: Shouldn't arch-independent output template handling go into
2960b57cec5SDimitry Andric           // PrintAsmOperand?
297480093f4SDimitry Andric           // Labels are target independent.
2980b57cec5SDimitry Andric           if (MI->getOperand(OpNo).isBlockAddress()) {
2990b57cec5SDimitry Andric             const BlockAddress *BA = MI->getOperand(OpNo).getBlockAddress();
3000b57cec5SDimitry Andric             MCSymbol *Sym = AP->GetBlockAddressSymbol(BA);
3010b57cec5SDimitry Andric             Sym->print(OS, AP->MAI);
3020b57cec5SDimitry Andric             MMI->getContext().registerInlineAsmLabel(Sym);
3030b57cec5SDimitry Andric           } else if (MI->getOperand(OpNo).isMBB()) {
3040b57cec5SDimitry Andric             const MCSymbol *Sym = MI->getOperand(OpNo).getMBB()->getSymbol();
3050b57cec5SDimitry Andric             Sym->print(OS, AP->MAI);
3065f757f3fSDimitry Andric           } else if (F.isMemKind()) {
3070b57cec5SDimitry Andric             Error = AP->PrintAsmMemoryOperand(
3080b57cec5SDimitry Andric                 MI, OpNo, Modifier[0] ? Modifier : nullptr, OS);
3090b57cec5SDimitry Andric           } else {
3100b57cec5SDimitry Andric             Error = AP->PrintAsmOperand(MI, OpNo,
3110b57cec5SDimitry Andric                                         Modifier[0] ? Modifier : nullptr, OS);
3120b57cec5SDimitry Andric           }
3130b57cec5SDimitry Andric         }
3140b57cec5SDimitry Andric         if (Error) {
3150b57cec5SDimitry Andric           std::string msg;
3160b57cec5SDimitry Andric           raw_string_ostream Msg(msg);
3170b57cec5SDimitry Andric           Msg << "invalid operand in inline asm: '" << AsmStr << "'";
318*0fca6ea1SDimitry Andric           MMI->getModule()->getContext().emitError(LocCookie, msg);
3190b57cec5SDimitry Andric         }
3200b57cec5SDimitry Andric       }
3210b57cec5SDimitry Andric       break;
3220b57cec5SDimitry Andric     }
3230b57cec5SDimitry Andric     }
3240b57cec5SDimitry Andric   }
3254824e7fdSDimitry Andric   if (InputIsIntelDialect)
3264824e7fdSDimitry Andric     OS << "\n\t.att_syntax";
3270b57cec5SDimitry Andric   OS << '\n' << (char)0;  // null terminate string.
3280b57cec5SDimitry Andric }
3290b57cec5SDimitry Andric 
3305ffd83dbSDimitry Andric /// This method formats and emits the specified machine instruction that is an
3315ffd83dbSDimitry Andric /// inline asm.
emitInlineAsm(const MachineInstr * MI) const3325ffd83dbSDimitry Andric void AsmPrinter::emitInlineAsm(const MachineInstr *MI) const {
3330b57cec5SDimitry Andric   assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric   // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
336bdd1243dSDimitry Andric   const char *AsmStr = MI->getOperand(0).getSymbolName();
3370b57cec5SDimitry Andric 
3380b57cec5SDimitry Andric   // If this asmstr is empty, just print the #APP/#NOAPP markers.
3390b57cec5SDimitry Andric   // These are useful to see where empty asm's wound up.
3400b57cec5SDimitry Andric   if (AsmStr[0] == 0) {
3410b57cec5SDimitry Andric     OutStreamer->emitRawComment(MAI->getInlineAsmStart());
3420b57cec5SDimitry Andric     OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
3430b57cec5SDimitry Andric     return;
3440b57cec5SDimitry Andric   }
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric   // Emit the #APP start marker.  This has to happen even if verbose-asm isn't
3470b57cec5SDimitry Andric   // enabled, so we use emitRawComment.
3480b57cec5SDimitry Andric   OutStreamer->emitRawComment(MAI->getInlineAsmStart());
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric   // Get the !srcloc metadata node if we have it, and decode the loc cookie from
3510b57cec5SDimitry Andric   // it.
352fe6060f1SDimitry Andric   uint64_t LocCookie = 0;
3530b57cec5SDimitry Andric   const MDNode *LocMD = nullptr;
3544824e7fdSDimitry Andric   for (const MachineOperand &MO : llvm::reverse(MI->operands())) {
3554824e7fdSDimitry Andric     if (MO.isMetadata() && (LocMD = MO.getMetadata()) &&
3560b57cec5SDimitry Andric         LocMD->getNumOperands() != 0) {
3570b57cec5SDimitry Andric       if (const ConstantInt *CI =
3580b57cec5SDimitry Andric               mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
3590b57cec5SDimitry Andric         LocCookie = CI->getZExtValue();
3600b57cec5SDimitry Andric         break;
3610b57cec5SDimitry Andric       }
3620b57cec5SDimitry Andric     }
3630b57cec5SDimitry Andric   }
3640b57cec5SDimitry Andric 
3650b57cec5SDimitry Andric   // Emit the inline asm to a temporary string so we can emit it through
3660b57cec5SDimitry Andric   // EmitInlineAsm.
3670b57cec5SDimitry Andric   SmallString<256> StringData;
3680b57cec5SDimitry Andric   raw_svector_ostream OS(StringData);
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric   AsmPrinter *AP = const_cast<AsmPrinter*>(this);
3714824e7fdSDimitry Andric   EmitInlineAsmStr(AsmStr, MI, MMI, MAI, AP, LocCookie, OS);
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric   // Emit warnings if we use reserved registers on the clobber list, as
374e8d8bef9SDimitry Andric   // that might lead to undefined behaviour.
375e8d8bef9SDimitry Andric   SmallVector<Register, 8> RestrRegs;
376e8d8bef9SDimitry Andric   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
3770b57cec5SDimitry Andric   // Start with the first operand descriptor, and iterate over them.
3780b57cec5SDimitry Andric   for (unsigned I = InlineAsm::MIOp_FirstOperand, NumOps = MI->getNumOperands();
3790b57cec5SDimitry Andric        I < NumOps; ++I) {
3800b57cec5SDimitry Andric     const MachineOperand &MO = MI->getOperand(I);
381e8d8bef9SDimitry Andric     if (!MO.isImm())
382e8d8bef9SDimitry Andric       continue;
3835f757f3fSDimitry Andric     const InlineAsm::Flag F(MO.getImm());
3845f757f3fSDimitry Andric     if (F.isClobberKind()) {
385e8d8bef9SDimitry Andric       Register Reg = MI->getOperand(I + 1).getReg();
386e8d8bef9SDimitry Andric       if (!TRI->isAsmClobberable(*MF, Reg))
387e8d8bef9SDimitry Andric         RestrRegs.push_back(Reg);
3880b57cec5SDimitry Andric     }
3890b57cec5SDimitry Andric     // Skip to one before the next operand descriptor, if it exists.
3905f757f3fSDimitry Andric     I += F.getNumOperandRegisters();
3910b57cec5SDimitry Andric   }
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric   if (!RestrRegs.empty()) {
3940b57cec5SDimitry Andric     std::string Msg = "inline asm clobber list contains reserved registers: ";
395fe6060f1SDimitry Andric     ListSeparator LS;
39681ad6265SDimitry Andric     for (const Register RR : RestrRegs) {
397fe6060f1SDimitry Andric       Msg += LS;
39881ad6265SDimitry Andric       Msg += TRI->getRegAsmName(RR);
3990b57cec5SDimitry Andric     }
400e8d8bef9SDimitry Andric     const char *Note =
401e8d8bef9SDimitry Andric         "Reserved registers on the clobber list may not be "
4020b57cec5SDimitry Andric         "preserved across the asm statement, and clobbering them may "
4030b57cec5SDimitry Andric         "lead to undefined behaviour.";
404fe6060f1SDimitry Andric     MMI->getModule()->getContext().diagnose(DiagnosticInfoInlineAsm(
405349cc55cSDimitry Andric         LocCookie, Msg, DiagnosticSeverity::DS_Warning));
406fe6060f1SDimitry Andric     MMI->getModule()->getContext().diagnose(
407fe6060f1SDimitry Andric         DiagnosticInfoInlineAsm(LocCookie, Note, DiagnosticSeverity::DS_Note));
408bdd1243dSDimitry Andric 
409bdd1243dSDimitry Andric     for (const Register RR : RestrRegs) {
410bdd1243dSDimitry Andric       if (std::optional<std::string> reason =
411bdd1243dSDimitry Andric               TRI->explainReservedReg(*MF, RR)) {
412bdd1243dSDimitry Andric         MMI->getModule()->getContext().diagnose(DiagnosticInfoInlineAsm(
413bdd1243dSDimitry Andric             LocCookie, *reason, DiagnosticSeverity::DS_Note));
414bdd1243dSDimitry Andric       }
415bdd1243dSDimitry Andric     }
4160b57cec5SDimitry Andric   }
4170b57cec5SDimitry Andric 
418*0fca6ea1SDimitry Andric   emitInlineAsm(StringData, getSubtargetInfo(), TM.Options.MCOptions, LocMD,
4190b57cec5SDimitry Andric                 MI->getInlineAsmDialect());
4200b57cec5SDimitry Andric 
4210b57cec5SDimitry Andric   // Emit the #NOAPP end marker.  This has to happen even if verbose-asm isn't
4220b57cec5SDimitry Andric   // enabled, so we use emitRawComment.
4230b57cec5SDimitry Andric   OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
4240b57cec5SDimitry Andric }
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric /// PrintSpecial - Print information related to the specified machine instr
4270b57cec5SDimitry Andric /// that is independent of the operand, and may be independent of the instr
4280b57cec5SDimitry Andric /// itself.  This can be useful for portably encoding the comment character
4290b57cec5SDimitry Andric /// or other bits of target-specific knowledge into the asmstrings.  The
4300b57cec5SDimitry Andric /// syntax used is ${:comment}.  Targets can override this to add support
4310b57cec5SDimitry Andric /// for their own strange codes.
PrintSpecial(const MachineInstr * MI,raw_ostream & OS,StringRef Code) const4320b57cec5SDimitry Andric void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
433349cc55cSDimitry Andric                               StringRef Code) const {
434349cc55cSDimitry Andric   if (Code == "private") {
4350b57cec5SDimitry Andric     const DataLayout &DL = MF->getDataLayout();
4360b57cec5SDimitry Andric     OS << DL.getPrivateGlobalPrefix();
437349cc55cSDimitry Andric   } else if (Code == "comment") {
4380b57cec5SDimitry Andric     OS << MAI->getCommentString();
439349cc55cSDimitry Andric   } else if (Code == "uid") {
4400b57cec5SDimitry Andric     // Comparing the address of MI isn't sufficient, because machineinstrs may
4410b57cec5SDimitry Andric     // be allocated to the same address across functions.
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric     // If this is a new LastFn instruction, bump the counter.
4440b57cec5SDimitry Andric     if (LastMI != MI || LastFn != getFunctionNumber()) {
4450b57cec5SDimitry Andric       ++Counter;
4460b57cec5SDimitry Andric       LastMI = MI;
4470b57cec5SDimitry Andric       LastFn = getFunctionNumber();
4480b57cec5SDimitry Andric     }
4490b57cec5SDimitry Andric     OS << Counter;
4500b57cec5SDimitry Andric   } else {
4510b57cec5SDimitry Andric     std::string msg;
4520b57cec5SDimitry Andric     raw_string_ostream Msg(msg);
4530b57cec5SDimitry Andric     Msg << "Unknown special formatter '" << Code
4540b57cec5SDimitry Andric          << "' for machine instr: " << *MI;
455349cc55cSDimitry Andric     report_fatal_error(Twine(Msg.str()));
4560b57cec5SDimitry Andric   }
4570b57cec5SDimitry Andric }
4580b57cec5SDimitry Andric 
PrintSymbolOperand(const MachineOperand & MO,raw_ostream & OS)4590b57cec5SDimitry Andric void AsmPrinter::PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS) {
4600b57cec5SDimitry Andric   assert(MO.isGlobal() && "caller should check MO.isGlobal");
461fe6060f1SDimitry Andric   getSymbolPreferLocal(*MO.getGlobal())->print(OS, MAI);
4620b57cec5SDimitry Andric   printOffset(MO.getOffset(), OS);
4630b57cec5SDimitry Andric }
4640b57cec5SDimitry Andric 
4650b57cec5SDimitry Andric /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
4660b57cec5SDimitry Andric /// instruction, using the specified assembler variant.  Targets should
4670b57cec5SDimitry Andric /// override this to format as appropriate for machine specific ExtraCodes
4680b57cec5SDimitry Andric /// or when the arch-independent handling would be too complex otherwise.
PrintAsmOperand(const MachineInstr * MI,unsigned OpNo,const char * ExtraCode,raw_ostream & O)4690b57cec5SDimitry Andric bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
4700b57cec5SDimitry Andric                                  const char *ExtraCode, raw_ostream &O) {
4710b57cec5SDimitry Andric   // Does this asm operand have a single letter operand modifier?
4720b57cec5SDimitry Andric   if (ExtraCode && ExtraCode[0]) {
4730b57cec5SDimitry Andric     if (ExtraCode[1] != 0) return true; // Unknown modifier.
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric     // https://gcc.gnu.org/onlinedocs/gccint/Output-Template.html
4760b57cec5SDimitry Andric     const MachineOperand &MO = MI->getOperand(OpNo);
4770b57cec5SDimitry Andric     switch (ExtraCode[0]) {
4780b57cec5SDimitry Andric     default:
4790b57cec5SDimitry Andric       return true;  // Unknown modifier.
4800b57cec5SDimitry Andric     case 'a': // Print as memory address.
4810b57cec5SDimitry Andric       if (MO.isReg()) {
4820b57cec5SDimitry Andric         PrintAsmMemoryOperand(MI, OpNo, nullptr, O);
4830b57cec5SDimitry Andric         return false;
4840b57cec5SDimitry Andric       }
485bdd1243dSDimitry Andric       [[fallthrough]]; // GCC allows '%a' to behave like '%c' with immediates.
4860b57cec5SDimitry Andric     case 'c': // Substitute immediate value without immediate syntax
4870b57cec5SDimitry Andric       if (MO.isImm()) {
4880b57cec5SDimitry Andric         O << MO.getImm();
4890b57cec5SDimitry Andric         return false;
4900b57cec5SDimitry Andric       }
4910b57cec5SDimitry Andric       if (MO.isGlobal()) {
4920b57cec5SDimitry Andric         PrintSymbolOperand(MO, O);
4930b57cec5SDimitry Andric         return false;
4940b57cec5SDimitry Andric       }
4950b57cec5SDimitry Andric       return true;
4960b57cec5SDimitry Andric     case 'n':  // Negate the immediate constant.
4970b57cec5SDimitry Andric       if (!MO.isImm())
4980b57cec5SDimitry Andric         return true;
4990b57cec5SDimitry Andric       O << -MO.getImm();
5000b57cec5SDimitry Andric       return false;
5010b57cec5SDimitry Andric     case 's':  // The GCC deprecated s modifier
5020b57cec5SDimitry Andric       if (!MO.isImm())
5030b57cec5SDimitry Andric         return true;
5040b57cec5SDimitry Andric       O << ((32 - MO.getImm()) & 31);
5050b57cec5SDimitry Andric       return false;
5060b57cec5SDimitry Andric     }
5070b57cec5SDimitry Andric   }
5080b57cec5SDimitry Andric   return true;
5090b57cec5SDimitry Andric }
5100b57cec5SDimitry Andric 
PrintAsmMemoryOperand(const MachineInstr * MI,unsigned OpNo,const char * ExtraCode,raw_ostream & O)5110b57cec5SDimitry Andric bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
5120b57cec5SDimitry Andric                                        const char *ExtraCode, raw_ostream &O) {
5130b57cec5SDimitry Andric   // Target doesn't support this yet!
5140b57cec5SDimitry Andric   return true;
5150b57cec5SDimitry Andric }
5160b57cec5SDimitry Andric 
emitInlineAsmStart() const5170b57cec5SDimitry Andric void AsmPrinter::emitInlineAsmStart() const {}
5180b57cec5SDimitry Andric 
emitInlineAsmEnd(const MCSubtargetInfo & StartInfo,const MCSubtargetInfo * EndInfo) const5190b57cec5SDimitry Andric void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
5200b57cec5SDimitry Andric                                   const MCSubtargetInfo *EndInfo) const {}
521