xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
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 
450b57cec5SDimitry 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.
715ffd83dbSDimitry 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   // Do not use assembler-level information for parsing inline assembly.
1060b57cec5SDimitry Andric   OutStreamer->setUseAssemblerInfoForParsing(false);
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   // We create a new MCInstrInfo here since we might be at the module level
1090b57cec5SDimitry Andric   // and not have a MachineFunction to initialize the TargetInstrInfo from and
1100b57cec5SDimitry Andric   // we only need MCInstrInfo for asm parsing. We create one unconditionally
1110b57cec5SDimitry Andric   // because it's not subtarget dependent.
1120b57cec5SDimitry Andric   std::unique_ptr<MCInstrInfo> MII(TM.getTarget().createMCInstrInfo());
113e8d8bef9SDimitry Andric   assert(MII && "Failed to create instruction info");
1140b57cec5SDimitry Andric   std::unique_ptr<MCTargetAsmParser> TAP(TM.getTarget().createMCAsmParser(
1150b57cec5SDimitry Andric       STI, *Parser, *MII, MCOptions));
1160b57cec5SDimitry Andric   if (!TAP)
1170b57cec5SDimitry Andric     report_fatal_error("Inline asm not supported by this streamer because"
1180b57cec5SDimitry Andric                        " we don't have an asm parser for this target\n");
1190b57cec5SDimitry Andric   Parser->setAssemblerDialect(Dialect);
12081ad6265SDimitry Andric   Parser->setTargetParser(*TAP);
1210b57cec5SDimitry Andric   // Enable lexing Masm binary and hex integer literals in intel inline
1220b57cec5SDimitry Andric   // assembly.
1230b57cec5SDimitry Andric   if (Dialect == InlineAsm::AD_Intel)
1240b57cec5SDimitry Andric     Parser->getLexer().setLexMasmIntegers(true);
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric   emitInlineAsmStart();
1270b57cec5SDimitry Andric   // Don't implicitly switch to the text section before the asm.
128fe6060f1SDimitry Andric   (void)Parser->Run(/*NoInitialTextSection*/ true,
1290b57cec5SDimitry Andric                     /*NoFinalize*/ true);
1300b57cec5SDimitry Andric   emitInlineAsmEnd(STI, &TAP->getSTI());
1310b57cec5SDimitry Andric }
1320b57cec5SDimitry Andric 
1334824e7fdSDimitry Andric static void EmitInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
134349cc55cSDimitry Andric                              MachineModuleInfo *MMI, const MCAsmInfo *MAI,
135349cc55cSDimitry Andric                              AsmPrinter *AP, uint64_t LocCookie,
136349cc55cSDimitry Andric                              raw_ostream &OS) {
1374824e7fdSDimitry Andric   bool InputIsIntelDialect = MI->getInlineAsmDialect() == InlineAsm::AD_Intel;
1384824e7fdSDimitry Andric 
1394824e7fdSDimitry Andric   if (InputIsIntelDialect) {
1400b57cec5SDimitry Andric     // Switch to the inline assembly variant.
1410b57cec5SDimitry Andric     OS << "\t.intel_syntax\n\t";
1424824e7fdSDimitry Andric   }
1430b57cec5SDimitry Andric 
144349cc55cSDimitry Andric   int CurVariant = -1; // The number of the {.|.|.} region we are in.
1450b57cec5SDimitry Andric   const char *LastEmitted = AsmStr; // One past the last character emitted.
1460b57cec5SDimitry Andric   unsigned NumOperands = MI->getNumOperands();
1470b57cec5SDimitry Andric 
1484824e7fdSDimitry Andric   int AsmPrinterVariant;
1494824e7fdSDimitry Andric   if (InputIsIntelDialect)
1504824e7fdSDimitry Andric     AsmPrinterVariant = 1; // X86MCAsmInfo.cpp's AsmWriterFlavorTy::Intel.
151349cc55cSDimitry Andric   else
1524824e7fdSDimitry Andric     AsmPrinterVariant = MMI->getTarget().unqualifiedInlineAsmVariant();
1530b57cec5SDimitry Andric 
1544824e7fdSDimitry Andric   // FIXME: Should this happen for `asm inteldialect` as well?
1554824e7fdSDimitry Andric   if (!InputIsIntelDialect && MAI->getEmitGNUAsmStartIndentationMarker())
1560b57cec5SDimitry Andric     OS << '\t';
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric   while (*LastEmitted) {
1590b57cec5SDimitry Andric     switch (*LastEmitted) {
1600b57cec5SDimitry Andric     default: {
1610b57cec5SDimitry Andric       // Not a special case, emit the string section literally.
1620b57cec5SDimitry Andric       const char *LiteralEnd = LastEmitted+1;
1630b57cec5SDimitry Andric       while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1640b57cec5SDimitry Andric              *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1650b57cec5SDimitry Andric         ++LiteralEnd;
1660b57cec5SDimitry Andric       if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1670b57cec5SDimitry Andric         OS.write(LastEmitted, LiteralEnd - LastEmitted);
1680b57cec5SDimitry Andric       LastEmitted = LiteralEnd;
1690b57cec5SDimitry Andric       break;
1700b57cec5SDimitry Andric     }
1710b57cec5SDimitry Andric     case '\n':
1720b57cec5SDimitry Andric       ++LastEmitted;   // Consume newline character.
1730b57cec5SDimitry Andric       OS << '\n';      // Indent code with newline.
1740b57cec5SDimitry Andric       break;
1750b57cec5SDimitry Andric     case '$': {
1760b57cec5SDimitry Andric       ++LastEmitted;   // Consume '$' character.
1770b57cec5SDimitry Andric       bool Done = true;
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric       // Handle escapes.
1800b57cec5SDimitry Andric       switch (*LastEmitted) {
1810b57cec5SDimitry Andric       default: Done = false; break;
1820b57cec5SDimitry Andric       case '$':     // $$ -> $
1834824e7fdSDimitry Andric         if (!InputIsIntelDialect)
1840b57cec5SDimitry Andric           if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1850b57cec5SDimitry Andric             OS << '$';
1860b57cec5SDimitry Andric         ++LastEmitted;  // Consume second '$' character.
1870b57cec5SDimitry Andric         break;
1880b57cec5SDimitry Andric       case '(':        // $( -> same as GCC's { character.
1890b57cec5SDimitry Andric         ++LastEmitted; // Consume '(' character.
1900b57cec5SDimitry Andric         if (CurVariant != -1)
1910b57cec5SDimitry Andric           report_fatal_error("Nested variants found in inline asm string: '" +
1920b57cec5SDimitry Andric                              Twine(AsmStr) + "'");
1930b57cec5SDimitry Andric         CurVariant = 0; // We're in the first variant now.
1940b57cec5SDimitry Andric         break;
1950b57cec5SDimitry Andric       case '|':
196349cc55cSDimitry Andric         ++LastEmitted; // Consume '|' character.
1970b57cec5SDimitry Andric         if (CurVariant == -1)
198349cc55cSDimitry Andric           OS << '|'; // This is gcc's behavior for | outside a variant.
1990b57cec5SDimitry Andric         else
2000b57cec5SDimitry Andric           ++CurVariant; // We're in the next variant.
2010b57cec5SDimitry Andric         break;
2020b57cec5SDimitry Andric       case ')':        // $) -> same as GCC's } char.
203349cc55cSDimitry Andric         ++LastEmitted; // Consume ')' character.
2040b57cec5SDimitry Andric         if (CurVariant == -1)
205349cc55cSDimitry Andric           OS << '}'; // This is gcc's behavior for } outside a variant.
2060b57cec5SDimitry Andric         else
2070b57cec5SDimitry Andric           CurVariant = -1;
2080b57cec5SDimitry Andric         break;
2090b57cec5SDimitry Andric       }
2100b57cec5SDimitry Andric       if (Done) break;
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric       bool HasCurlyBraces = false;
2130b57cec5SDimitry Andric       if (*LastEmitted == '{') {     // ${variable}
2140b57cec5SDimitry Andric         ++LastEmitted;               // Consume '{' character.
2150b57cec5SDimitry Andric         HasCurlyBraces = true;
2160b57cec5SDimitry Andric       }
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric       // If we have ${:foo}, then this is not a real operand reference, it is a
2190b57cec5SDimitry Andric       // "magic" string reference, just like in .td files.  Arrange to call
2200b57cec5SDimitry Andric       // PrintSpecial.
2210b57cec5SDimitry Andric       if (HasCurlyBraces && *LastEmitted == ':') {
2220b57cec5SDimitry Andric         ++LastEmitted;
2230b57cec5SDimitry Andric         const char *StrStart = LastEmitted;
2240b57cec5SDimitry Andric         const char *StrEnd = strchr(StrStart, '}');
2250b57cec5SDimitry Andric         if (!StrEnd)
2260b57cec5SDimitry Andric           report_fatal_error("Unterminated ${:foo} operand in inline asm"
2270b57cec5SDimitry Andric                              " string: '" + Twine(AsmStr) + "'");
228349cc55cSDimitry Andric         if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
229349cc55cSDimitry Andric           AP->PrintSpecial(MI, OS, StringRef(StrStart, StrEnd - StrStart));
2300b57cec5SDimitry Andric         LastEmitted = StrEnd+1;
2310b57cec5SDimitry Andric         break;
2320b57cec5SDimitry Andric       }
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric       const char *IDStart = LastEmitted;
2350b57cec5SDimitry Andric       const char *IDEnd = IDStart;
236e8d8bef9SDimitry Andric       while (isDigit(*IDEnd))
237e8d8bef9SDimitry Andric         ++IDEnd;
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric       unsigned Val;
2400b57cec5SDimitry Andric       if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
2410b57cec5SDimitry Andric         report_fatal_error("Bad $ operand number in inline asm string: '" +
2420b57cec5SDimitry Andric                            Twine(AsmStr) + "'");
2430b57cec5SDimitry Andric       LastEmitted = IDEnd;
2440b57cec5SDimitry Andric 
245349cc55cSDimitry Andric       if (Val >= NumOperands - 1)
246349cc55cSDimitry Andric         report_fatal_error("Invalid $ operand number in inline asm string: '" +
247349cc55cSDimitry Andric                            Twine(AsmStr) + "'");
248349cc55cSDimitry Andric 
2490b57cec5SDimitry Andric       char Modifier[2] = { 0, 0 };
2500b57cec5SDimitry Andric 
2510b57cec5SDimitry Andric       if (HasCurlyBraces) {
2520b57cec5SDimitry Andric         // If we have curly braces, check for a modifier character.  This
2530b57cec5SDimitry Andric         // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
2540b57cec5SDimitry Andric         if (*LastEmitted == ':') {
2550b57cec5SDimitry Andric           ++LastEmitted;    // Consume ':' character.
2560b57cec5SDimitry Andric           if (*LastEmitted == 0)
2570b57cec5SDimitry Andric             report_fatal_error("Bad ${:} expression in inline asm string: '" +
2580b57cec5SDimitry Andric                                Twine(AsmStr) + "'");
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric           Modifier[0] = *LastEmitted;
2610b57cec5SDimitry Andric           ++LastEmitted;    // Consume modifier character.
2620b57cec5SDimitry Andric         }
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric         if (*LastEmitted != '}')
2650b57cec5SDimitry Andric           report_fatal_error("Bad ${} expression in inline asm string: '" +
2660b57cec5SDimitry Andric                              Twine(AsmStr) + "'");
2670b57cec5SDimitry Andric         ++LastEmitted;    // Consume '}' character.
2680b57cec5SDimitry Andric       }
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric       // Okay, we finally have a value number.  Ask the target to print this
2710b57cec5SDimitry Andric       // operand!
2720b57cec5SDimitry Andric       if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
2730b57cec5SDimitry Andric         unsigned OpNo = InlineAsm::MIOp_FirstOperand;
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric         bool Error = false;
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric         // Scan to find the machine operand number for the operand.
2780b57cec5SDimitry Andric         for (; Val; --Val) {
279349cc55cSDimitry Andric           if (OpNo >= MI->getNumOperands())
280349cc55cSDimitry Andric             break;
281*5f757f3fSDimitry Andric           const InlineAsm::Flag F(MI->getOperand(OpNo).getImm());
282*5f757f3fSDimitry Andric           OpNo += F.getNumOperandRegisters() + 1;
2830b57cec5SDimitry Andric         }
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric         // We may have a location metadata attached to the end of the
2860b57cec5SDimitry Andric         // instruction, and at no point should see metadata at any
2870b57cec5SDimitry Andric         // other point while processing. It's an error if so.
288349cc55cSDimitry Andric         if (OpNo >= MI->getNumOperands() || MI->getOperand(OpNo).isMetadata()) {
2890b57cec5SDimitry Andric           Error = true;
2900b57cec5SDimitry Andric         } else {
291*5f757f3fSDimitry Andric           const InlineAsm::Flag F(MI->getOperand(OpNo).getImm());
2920b57cec5SDimitry Andric           ++OpNo; // Skip over the ID number.
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric           // FIXME: Shouldn't arch-independent output template handling go into
2950b57cec5SDimitry Andric           // PrintAsmOperand?
296480093f4SDimitry Andric           // Labels are target independent.
2970b57cec5SDimitry Andric           if (MI->getOperand(OpNo).isBlockAddress()) {
2980b57cec5SDimitry Andric             const BlockAddress *BA = MI->getOperand(OpNo).getBlockAddress();
2990b57cec5SDimitry Andric             MCSymbol *Sym = AP->GetBlockAddressSymbol(BA);
3000b57cec5SDimitry Andric             Sym->print(OS, AP->MAI);
3010b57cec5SDimitry Andric             MMI->getContext().registerInlineAsmLabel(Sym);
3020b57cec5SDimitry Andric           } else if (MI->getOperand(OpNo).isMBB()) {
3030b57cec5SDimitry Andric             const MCSymbol *Sym = MI->getOperand(OpNo).getMBB()->getSymbol();
3040b57cec5SDimitry Andric             Sym->print(OS, AP->MAI);
305*5f757f3fSDimitry Andric           } else if (F.isMemKind()) {
3060b57cec5SDimitry Andric             Error = AP->PrintAsmMemoryOperand(
3070b57cec5SDimitry Andric                 MI, OpNo, Modifier[0] ? Modifier : nullptr, OS);
3080b57cec5SDimitry Andric           } else {
3090b57cec5SDimitry Andric             Error = AP->PrintAsmOperand(MI, OpNo,
3100b57cec5SDimitry Andric                                         Modifier[0] ? Modifier : nullptr, OS);
3110b57cec5SDimitry Andric           }
3120b57cec5SDimitry Andric         }
3130b57cec5SDimitry Andric         if (Error) {
3140b57cec5SDimitry Andric           std::string msg;
3150b57cec5SDimitry Andric           raw_string_ostream Msg(msg);
3160b57cec5SDimitry Andric           Msg << "invalid operand in inline asm: '" << AsmStr << "'";
3170b57cec5SDimitry Andric           MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
3180b57cec5SDimitry Andric         }
3190b57cec5SDimitry Andric       }
3200b57cec5SDimitry Andric       break;
3210b57cec5SDimitry Andric     }
3220b57cec5SDimitry Andric     }
3230b57cec5SDimitry Andric   }
3244824e7fdSDimitry Andric   if (InputIsIntelDialect)
3254824e7fdSDimitry Andric     OS << "\n\t.att_syntax";
3260b57cec5SDimitry Andric   OS << '\n' << (char)0;  // null terminate string.
3270b57cec5SDimitry Andric }
3280b57cec5SDimitry Andric 
3295ffd83dbSDimitry Andric /// This method formats and emits the specified machine instruction that is an
3305ffd83dbSDimitry Andric /// inline asm.
3315ffd83dbSDimitry Andric void AsmPrinter::emitInlineAsm(const MachineInstr *MI) const {
3320b57cec5SDimitry Andric   assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric   // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
335bdd1243dSDimitry Andric   const char *AsmStr = MI->getOperand(0).getSymbolName();
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric   // If this asmstr is empty, just print the #APP/#NOAPP markers.
3380b57cec5SDimitry Andric   // These are useful to see where empty asm's wound up.
3390b57cec5SDimitry Andric   if (AsmStr[0] == 0) {
3400b57cec5SDimitry Andric     OutStreamer->emitRawComment(MAI->getInlineAsmStart());
3410b57cec5SDimitry Andric     OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
3420b57cec5SDimitry Andric     return;
3430b57cec5SDimitry Andric   }
3440b57cec5SDimitry Andric 
3450b57cec5SDimitry Andric   // Emit the #APP start marker.  This has to happen even if verbose-asm isn't
3460b57cec5SDimitry Andric   // enabled, so we use emitRawComment.
3470b57cec5SDimitry Andric   OutStreamer->emitRawComment(MAI->getInlineAsmStart());
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric   // Get the !srcloc metadata node if we have it, and decode the loc cookie from
3500b57cec5SDimitry Andric   // it.
351fe6060f1SDimitry Andric   uint64_t LocCookie = 0;
3520b57cec5SDimitry Andric   const MDNode *LocMD = nullptr;
3534824e7fdSDimitry Andric   for (const MachineOperand &MO : llvm::reverse(MI->operands())) {
3544824e7fdSDimitry Andric     if (MO.isMetadata() && (LocMD = MO.getMetadata()) &&
3550b57cec5SDimitry Andric         LocMD->getNumOperands() != 0) {
3560b57cec5SDimitry Andric       if (const ConstantInt *CI =
3570b57cec5SDimitry Andric               mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
3580b57cec5SDimitry Andric         LocCookie = CI->getZExtValue();
3590b57cec5SDimitry Andric         break;
3600b57cec5SDimitry Andric       }
3610b57cec5SDimitry Andric     }
3620b57cec5SDimitry Andric   }
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric   // Emit the inline asm to a temporary string so we can emit it through
3650b57cec5SDimitry Andric   // EmitInlineAsm.
3660b57cec5SDimitry Andric   SmallString<256> StringData;
3670b57cec5SDimitry Andric   raw_svector_ostream OS(StringData);
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric   AsmPrinter *AP = const_cast<AsmPrinter*>(this);
3704824e7fdSDimitry Andric   EmitInlineAsmStr(AsmStr, MI, MMI, MAI, AP, LocCookie, OS);
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric   // Emit warnings if we use reserved registers on the clobber list, as
373e8d8bef9SDimitry Andric   // that might lead to undefined behaviour.
374e8d8bef9SDimitry Andric   SmallVector<Register, 8> RestrRegs;
375e8d8bef9SDimitry Andric   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
3760b57cec5SDimitry Andric   // Start with the first operand descriptor, and iterate over them.
3770b57cec5SDimitry Andric   for (unsigned I = InlineAsm::MIOp_FirstOperand, NumOps = MI->getNumOperands();
3780b57cec5SDimitry Andric        I < NumOps; ++I) {
3790b57cec5SDimitry Andric     const MachineOperand &MO = MI->getOperand(I);
380e8d8bef9SDimitry Andric     if (!MO.isImm())
381e8d8bef9SDimitry Andric       continue;
382*5f757f3fSDimitry Andric     const InlineAsm::Flag F(MO.getImm());
383*5f757f3fSDimitry Andric     if (F.isClobberKind()) {
384e8d8bef9SDimitry Andric       Register Reg = MI->getOperand(I + 1).getReg();
385e8d8bef9SDimitry Andric       if (!TRI->isAsmClobberable(*MF, Reg))
386e8d8bef9SDimitry Andric         RestrRegs.push_back(Reg);
3870b57cec5SDimitry Andric     }
3880b57cec5SDimitry Andric     // Skip to one before the next operand descriptor, if it exists.
389*5f757f3fSDimitry Andric     I += F.getNumOperandRegisters();
3900b57cec5SDimitry Andric   }
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric   if (!RestrRegs.empty()) {
3930b57cec5SDimitry Andric     std::string Msg = "inline asm clobber list contains reserved registers: ";
394fe6060f1SDimitry Andric     ListSeparator LS;
39581ad6265SDimitry Andric     for (const Register RR : RestrRegs) {
396fe6060f1SDimitry Andric       Msg += LS;
39781ad6265SDimitry Andric       Msg += TRI->getRegAsmName(RR);
3980b57cec5SDimitry Andric     }
399e8d8bef9SDimitry Andric     const char *Note =
400e8d8bef9SDimitry Andric         "Reserved registers on the clobber list may not be "
4010b57cec5SDimitry Andric         "preserved across the asm statement, and clobbering them may "
4020b57cec5SDimitry Andric         "lead to undefined behaviour.";
403fe6060f1SDimitry Andric     MMI->getModule()->getContext().diagnose(DiagnosticInfoInlineAsm(
404349cc55cSDimitry Andric         LocCookie, Msg, DiagnosticSeverity::DS_Warning));
405fe6060f1SDimitry Andric     MMI->getModule()->getContext().diagnose(
406fe6060f1SDimitry Andric         DiagnosticInfoInlineAsm(LocCookie, Note, DiagnosticSeverity::DS_Note));
407bdd1243dSDimitry Andric 
408bdd1243dSDimitry Andric     for (const Register RR : RestrRegs) {
409bdd1243dSDimitry Andric       if (std::optional<std::string> reason =
410bdd1243dSDimitry Andric               TRI->explainReservedReg(*MF, RR)) {
411bdd1243dSDimitry Andric         MMI->getModule()->getContext().diagnose(DiagnosticInfoInlineAsm(
412bdd1243dSDimitry Andric             LocCookie, *reason, DiagnosticSeverity::DS_Note));
413bdd1243dSDimitry Andric       }
414bdd1243dSDimitry Andric     }
4150b57cec5SDimitry Andric   }
4160b57cec5SDimitry Andric 
4175ffd83dbSDimitry Andric   emitInlineAsm(OS.str(), getSubtargetInfo(), TM.Options.MCOptions, LocMD,
4180b57cec5SDimitry Andric                 MI->getInlineAsmDialect());
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric   // Emit the #NOAPP end marker.  This has to happen even if verbose-asm isn't
4210b57cec5SDimitry Andric   // enabled, so we use emitRawComment.
4220b57cec5SDimitry Andric   OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
4230b57cec5SDimitry Andric }
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric /// PrintSpecial - Print information related to the specified machine instr
4260b57cec5SDimitry Andric /// that is independent of the operand, and may be independent of the instr
4270b57cec5SDimitry Andric /// itself.  This can be useful for portably encoding the comment character
4280b57cec5SDimitry Andric /// or other bits of target-specific knowledge into the asmstrings.  The
4290b57cec5SDimitry Andric /// syntax used is ${:comment}.  Targets can override this to add support
4300b57cec5SDimitry Andric /// for their own strange codes.
4310b57cec5SDimitry Andric void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
432349cc55cSDimitry Andric                               StringRef Code) const {
433349cc55cSDimitry Andric   if (Code == "private") {
4340b57cec5SDimitry Andric     const DataLayout &DL = MF->getDataLayout();
4350b57cec5SDimitry Andric     OS << DL.getPrivateGlobalPrefix();
436349cc55cSDimitry Andric   } else if (Code == "comment") {
4370b57cec5SDimitry Andric     OS << MAI->getCommentString();
438349cc55cSDimitry Andric   } else if (Code == "uid") {
4390b57cec5SDimitry Andric     // Comparing the address of MI isn't sufficient, because machineinstrs may
4400b57cec5SDimitry Andric     // be allocated to the same address across functions.
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric     // If this is a new LastFn instruction, bump the counter.
4430b57cec5SDimitry Andric     if (LastMI != MI || LastFn != getFunctionNumber()) {
4440b57cec5SDimitry Andric       ++Counter;
4450b57cec5SDimitry Andric       LastMI = MI;
4460b57cec5SDimitry Andric       LastFn = getFunctionNumber();
4470b57cec5SDimitry Andric     }
4480b57cec5SDimitry Andric     OS << Counter;
4490b57cec5SDimitry Andric   } else {
4500b57cec5SDimitry Andric     std::string msg;
4510b57cec5SDimitry Andric     raw_string_ostream Msg(msg);
4520b57cec5SDimitry Andric     Msg << "Unknown special formatter '" << Code
4530b57cec5SDimitry Andric          << "' for machine instr: " << *MI;
454349cc55cSDimitry Andric     report_fatal_error(Twine(Msg.str()));
4550b57cec5SDimitry Andric   }
4560b57cec5SDimitry Andric }
4570b57cec5SDimitry Andric 
4580b57cec5SDimitry Andric void AsmPrinter::PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS) {
4590b57cec5SDimitry Andric   assert(MO.isGlobal() && "caller should check MO.isGlobal");
460fe6060f1SDimitry Andric   getSymbolPreferLocal(*MO.getGlobal())->print(OS, MAI);
4610b57cec5SDimitry Andric   printOffset(MO.getOffset(), OS);
4620b57cec5SDimitry Andric }
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
4650b57cec5SDimitry Andric /// instruction, using the specified assembler variant.  Targets should
4660b57cec5SDimitry Andric /// override this to format as appropriate for machine specific ExtraCodes
4670b57cec5SDimitry Andric /// or when the arch-independent handling would be too complex otherwise.
4680b57cec5SDimitry Andric bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
4690b57cec5SDimitry Andric                                  const char *ExtraCode, raw_ostream &O) {
4700b57cec5SDimitry Andric   // Does this asm operand have a single letter operand modifier?
4710b57cec5SDimitry Andric   if (ExtraCode && ExtraCode[0]) {
4720b57cec5SDimitry Andric     if (ExtraCode[1] != 0) return true; // Unknown modifier.
4730b57cec5SDimitry Andric 
4740b57cec5SDimitry Andric     // https://gcc.gnu.org/onlinedocs/gccint/Output-Template.html
4750b57cec5SDimitry Andric     const MachineOperand &MO = MI->getOperand(OpNo);
4760b57cec5SDimitry Andric     switch (ExtraCode[0]) {
4770b57cec5SDimitry Andric     default:
4780b57cec5SDimitry Andric       return true;  // Unknown modifier.
4790b57cec5SDimitry Andric     case 'a': // Print as memory address.
4800b57cec5SDimitry Andric       if (MO.isReg()) {
4810b57cec5SDimitry Andric         PrintAsmMemoryOperand(MI, OpNo, nullptr, O);
4820b57cec5SDimitry Andric         return false;
4830b57cec5SDimitry Andric       }
484bdd1243dSDimitry Andric       [[fallthrough]]; // GCC allows '%a' to behave like '%c' with immediates.
4850b57cec5SDimitry Andric     case 'c': // Substitute immediate value without immediate syntax
4860b57cec5SDimitry Andric       if (MO.isImm()) {
4870b57cec5SDimitry Andric         O << MO.getImm();
4880b57cec5SDimitry Andric         return false;
4890b57cec5SDimitry Andric       }
4900b57cec5SDimitry Andric       if (MO.isGlobal()) {
4910b57cec5SDimitry Andric         PrintSymbolOperand(MO, O);
4920b57cec5SDimitry Andric         return false;
4930b57cec5SDimitry Andric       }
4940b57cec5SDimitry Andric       return true;
4950b57cec5SDimitry Andric     case 'n':  // Negate the immediate constant.
4960b57cec5SDimitry Andric       if (!MO.isImm())
4970b57cec5SDimitry Andric         return true;
4980b57cec5SDimitry Andric       O << -MO.getImm();
4990b57cec5SDimitry Andric       return false;
5000b57cec5SDimitry Andric     case 's':  // The GCC deprecated s modifier
5010b57cec5SDimitry Andric       if (!MO.isImm())
5020b57cec5SDimitry Andric         return true;
5030b57cec5SDimitry Andric       O << ((32 - MO.getImm()) & 31);
5040b57cec5SDimitry Andric       return false;
5050b57cec5SDimitry Andric     }
5060b57cec5SDimitry Andric   }
5070b57cec5SDimitry Andric   return true;
5080b57cec5SDimitry Andric }
5090b57cec5SDimitry Andric 
5100b57cec5SDimitry Andric bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
5110b57cec5SDimitry Andric                                        const char *ExtraCode, raw_ostream &O) {
5120b57cec5SDimitry Andric   // Target doesn't support this yet!
5130b57cec5SDimitry Andric   return true;
5140b57cec5SDimitry Andric }
5150b57cec5SDimitry Andric 
5160b57cec5SDimitry Andric void AsmPrinter::emitInlineAsmStart() const {}
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
5190b57cec5SDimitry Andric                                   const MCSubtargetInfo *EndInfo) const {}
520