xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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"
140b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
150b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
210b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
220b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
230b57cec5SDimitry Andric #include "llvm/IR/InlineAsm.h"
240b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
250b57cec5SDimitry Andric #include "llvm/IR/Module.h"
260b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
270b57cec5SDimitry Andric #include "llvm/MC/MCParser/MCTargetAsmParser.h"
280b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
290b57cec5SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
300b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
310b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
320b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
330b57cec5SDimitry Andric #include "llvm/Support/SourceMgr.h"
340b57cec5SDimitry Andric #include "llvm/Support/TargetRegistry.h"
350b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
360b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
370b57cec5SDimitry Andric using namespace llvm;
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric #define DEBUG_TYPE "asm-printer"
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric /// srcMgrDiagHandler - This callback is invoked when the SourceMgr for an
420b57cec5SDimitry Andric /// inline asm has an error in it.  diagInfo is a pointer to the SrcMgrDiagInfo
430b57cec5SDimitry Andric /// struct above.
440b57cec5SDimitry Andric static void srcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
450b57cec5SDimitry Andric   AsmPrinter::SrcMgrDiagInfo *DiagInfo =
460b57cec5SDimitry Andric       static_cast<AsmPrinter::SrcMgrDiagInfo *>(diagInfo);
470b57cec5SDimitry Andric   assert(DiagInfo && "Diagnostic context not passed down?");
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric   // Look up a LocInfo for the buffer this diagnostic is coming from.
500b57cec5SDimitry Andric   unsigned BufNum = DiagInfo->SrcMgr.FindBufferContainingLoc(Diag.getLoc());
510b57cec5SDimitry Andric   const MDNode *LocInfo = nullptr;
520b57cec5SDimitry Andric   if (BufNum > 0 && BufNum <= DiagInfo->LocInfos.size())
530b57cec5SDimitry Andric     LocInfo = DiagInfo->LocInfos[BufNum-1];
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   // If the inline asm had metadata associated with it, pull out a location
560b57cec5SDimitry Andric   // cookie corresponding to which line the error occurred on.
570b57cec5SDimitry Andric   unsigned LocCookie = 0;
580b57cec5SDimitry Andric   if (LocInfo) {
590b57cec5SDimitry Andric     unsigned ErrorLine = Diag.getLineNo()-1;
600b57cec5SDimitry Andric     if (ErrorLine >= LocInfo->getNumOperands())
610b57cec5SDimitry Andric       ErrorLine = 0;
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric     if (LocInfo->getNumOperands() != 0)
640b57cec5SDimitry Andric       if (const ConstantInt *CI =
650b57cec5SDimitry Andric               mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
660b57cec5SDimitry Andric         LocCookie = CI->getZExtValue();
670b57cec5SDimitry Andric   }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   DiagInfo->DiagHandler(Diag, DiagInfo->DiagContext, LocCookie);
700b57cec5SDimitry Andric }
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric unsigned AsmPrinter::addInlineAsmDiagBuffer(StringRef AsmStr,
730b57cec5SDimitry Andric                                             const MDNode *LocMDNode) const {
740b57cec5SDimitry Andric   if (!DiagInfo) {
758bcb0991SDimitry Andric     DiagInfo = std::make_unique<SrcMgrDiagInfo>();
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric     MCContext &Context = MMI->getContext();
780b57cec5SDimitry Andric     Context.setInlineSourceManager(&DiagInfo->SrcMgr);
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric     LLVMContext &LLVMCtx = MMI->getModule()->getContext();
810b57cec5SDimitry Andric     if (LLVMCtx.getInlineAsmDiagnosticHandler()) {
820b57cec5SDimitry Andric       DiagInfo->DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
830b57cec5SDimitry Andric       DiagInfo->DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
840b57cec5SDimitry Andric       DiagInfo->SrcMgr.setDiagHandler(srcMgrDiagHandler, DiagInfo.get());
850b57cec5SDimitry Andric     }
860b57cec5SDimitry Andric   }
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric   SourceMgr &SrcMgr = DiagInfo->SrcMgr;
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric   std::unique_ptr<MemoryBuffer> Buffer;
910b57cec5SDimitry Andric   // The inline asm source manager will outlive AsmStr, so make a copy of the
920b57cec5SDimitry Andric   // string for SourceMgr to own.
930b57cec5SDimitry Andric   Buffer = MemoryBuffer::getMemBufferCopy(AsmStr, "<inline asm>");
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric   // Tell SrcMgr about this buffer, it takes ownership of the buffer.
960b57cec5SDimitry Andric   unsigned BufNum = SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   // Store LocMDNode in DiagInfo, using BufNum as an identifier.
990b57cec5SDimitry Andric   if (LocMDNode) {
1000b57cec5SDimitry Andric     DiagInfo->LocInfos.resize(BufNum);
1010b57cec5SDimitry Andric     DiagInfo->LocInfos[BufNum - 1] = LocMDNode;
1020b57cec5SDimitry Andric   }
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric   return BufNum;
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
109*5ffd83dbSDimitry Andric void AsmPrinter::emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
1100b57cec5SDimitry Andric                                const MCTargetOptions &MCOptions,
1110b57cec5SDimitry Andric                                const MDNode *LocMDNode,
1120b57cec5SDimitry Andric                                InlineAsm::AsmDialect Dialect) const {
1130b57cec5SDimitry Andric   assert(!Str.empty() && "Can't emit empty inline asm block");
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   // Remember if the buffer is nul terminated or not so we can avoid a copy.
1160b57cec5SDimitry Andric   bool isNullTerminated = Str.back() == 0;
1170b57cec5SDimitry Andric   if (isNullTerminated)
1180b57cec5SDimitry Andric     Str = Str.substr(0, Str.size()-1);
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric   // If the output streamer does not have mature MC support or the integrated
1210b57cec5SDimitry Andric   // assembler has been disabled, just emit the blob textually.
1220b57cec5SDimitry Andric   // Otherwise parse the asm and emit it via MC support.
1230b57cec5SDimitry Andric   // This is useful in case the asm parser doesn't handle something but the
1240b57cec5SDimitry Andric   // system assembler does.
1250b57cec5SDimitry Andric   const MCAsmInfo *MCAI = TM.getMCAsmInfo();
1260b57cec5SDimitry Andric   assert(MCAI && "No MCAsmInfo");
1270b57cec5SDimitry Andric   if (!MCAI->useIntegratedAssembler() &&
1280b57cec5SDimitry Andric       !OutStreamer->isIntegratedAssemblerRequired()) {
1290b57cec5SDimitry Andric     emitInlineAsmStart();
130*5ffd83dbSDimitry Andric     OutStreamer->emitRawText(Str);
1310b57cec5SDimitry Andric     emitInlineAsmEnd(STI, nullptr);
1320b57cec5SDimitry Andric     return;
1330b57cec5SDimitry Andric   }
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric   unsigned BufNum = addInlineAsmDiagBuffer(Str, LocMDNode);
1360b57cec5SDimitry Andric   DiagInfo->SrcMgr.setIncludeDirs(MCOptions.IASSearchPaths);
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric   std::unique_ptr<MCAsmParser> Parser(createMCAsmParser(
1390b57cec5SDimitry Andric           DiagInfo->SrcMgr, OutContext, *OutStreamer, *MAI, BufNum));
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   // Do not use assembler-level information for parsing inline assembly.
1420b57cec5SDimitry Andric   OutStreamer->setUseAssemblerInfoForParsing(false);
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric   // We create a new MCInstrInfo here since we might be at the module level
1450b57cec5SDimitry Andric   // and not have a MachineFunction to initialize the TargetInstrInfo from and
1460b57cec5SDimitry Andric   // we only need MCInstrInfo for asm parsing. We create one unconditionally
1470b57cec5SDimitry Andric   // because it's not subtarget dependent.
1480b57cec5SDimitry Andric   std::unique_ptr<MCInstrInfo> MII(TM.getTarget().createMCInstrInfo());
1490b57cec5SDimitry Andric   std::unique_ptr<MCTargetAsmParser> TAP(TM.getTarget().createMCAsmParser(
1500b57cec5SDimitry Andric       STI, *Parser, *MII, MCOptions));
1510b57cec5SDimitry Andric   if (!TAP)
1520b57cec5SDimitry Andric     report_fatal_error("Inline asm not supported by this streamer because"
1530b57cec5SDimitry Andric                        " we don't have an asm parser for this target\n");
1540b57cec5SDimitry Andric   Parser->setAssemblerDialect(Dialect);
1550b57cec5SDimitry Andric   Parser->setTargetParser(*TAP.get());
1560b57cec5SDimitry Andric   // Enable lexing Masm binary and hex integer literals in intel inline
1570b57cec5SDimitry Andric   // assembly.
1580b57cec5SDimitry Andric   if (Dialect == InlineAsm::AD_Intel)
1590b57cec5SDimitry Andric     Parser->getLexer().setLexMasmIntegers(true);
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric   emitInlineAsmStart();
1620b57cec5SDimitry Andric   // Don't implicitly switch to the text section before the asm.
1630b57cec5SDimitry Andric   int Res = Parser->Run(/*NoInitialTextSection*/ true,
1640b57cec5SDimitry Andric                         /*NoFinalize*/ true);
1650b57cec5SDimitry Andric   emitInlineAsmEnd(STI, &TAP->getSTI());
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric   if (Res && !DiagInfo->DiagHandler)
1680b57cec5SDimitry Andric     report_fatal_error("Error parsing inline asm\n");
1690b57cec5SDimitry Andric }
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
1720b57cec5SDimitry Andric                                MachineModuleInfo *MMI, AsmPrinter *AP,
1730b57cec5SDimitry Andric                                unsigned LocCookie, raw_ostream &OS) {
1740b57cec5SDimitry Andric   // Switch to the inline assembly variant.
1750b57cec5SDimitry Andric   OS << "\t.intel_syntax\n\t";
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric   const char *LastEmitted = AsmStr; // One past the last character emitted.
1780b57cec5SDimitry Andric   unsigned NumOperands = MI->getNumOperands();
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric   while (*LastEmitted) {
1810b57cec5SDimitry Andric     switch (*LastEmitted) {
1820b57cec5SDimitry Andric     default: {
1830b57cec5SDimitry Andric       // Not a special case, emit the string section literally.
1840b57cec5SDimitry Andric       const char *LiteralEnd = LastEmitted+1;
1850b57cec5SDimitry Andric       while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1860b57cec5SDimitry Andric              *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1870b57cec5SDimitry Andric         ++LiteralEnd;
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric       OS.write(LastEmitted, LiteralEnd-LastEmitted);
1900b57cec5SDimitry Andric       LastEmitted = LiteralEnd;
1910b57cec5SDimitry Andric       break;
1920b57cec5SDimitry Andric     }
1930b57cec5SDimitry Andric     case '\n':
1940b57cec5SDimitry Andric       ++LastEmitted;   // Consume newline character.
1950b57cec5SDimitry Andric       OS << '\n';      // Indent code with newline.
1960b57cec5SDimitry Andric       break;
1970b57cec5SDimitry Andric     case '$': {
1980b57cec5SDimitry Andric       ++LastEmitted;   // Consume '$' character.
1990b57cec5SDimitry Andric       bool Done = true;
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric       // Handle escapes.
2020b57cec5SDimitry Andric       switch (*LastEmitted) {
2030b57cec5SDimitry Andric       default: Done = false; break;
2040b57cec5SDimitry Andric       case '$':
2050b57cec5SDimitry Andric         ++LastEmitted;  // Consume second '$' character.
2060b57cec5SDimitry Andric         break;
2070b57cec5SDimitry Andric       }
2080b57cec5SDimitry Andric       if (Done) break;
2090b57cec5SDimitry Andric 
210480093f4SDimitry Andric       bool HasCurlyBraces = false;
211480093f4SDimitry Andric       if (*LastEmitted == '{') {     // ${variable}
212480093f4SDimitry Andric         ++LastEmitted;               // Consume '{' character.
213480093f4SDimitry Andric         HasCurlyBraces = true;
214480093f4SDimitry Andric       }
215480093f4SDimitry Andric 
2160b57cec5SDimitry Andric       // If we have ${:foo}, then this is not a real operand reference, it is a
2170b57cec5SDimitry Andric       // "magic" string reference, just like in .td files.  Arrange to call
2180b57cec5SDimitry Andric       // PrintSpecial.
219480093f4SDimitry Andric       if (HasCurlyBraces && LastEmitted[0] == ':') {
220480093f4SDimitry Andric         ++LastEmitted;
2210b57cec5SDimitry Andric         const char *StrStart = LastEmitted;
2220b57cec5SDimitry Andric         const char *StrEnd = strchr(StrStart, '}');
2230b57cec5SDimitry Andric         if (!StrEnd)
2240b57cec5SDimitry Andric           report_fatal_error("Unterminated ${:foo} operand in inline asm"
2250b57cec5SDimitry Andric                              " string: '" + Twine(AsmStr) + "'");
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric         std::string Val(StrStart, StrEnd);
2280b57cec5SDimitry Andric         AP->PrintSpecial(MI, OS, Val.c_str());
2290b57cec5SDimitry Andric         LastEmitted = StrEnd+1;
2300b57cec5SDimitry Andric         break;
2310b57cec5SDimitry Andric       }
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric       const char *IDStart = LastEmitted;
2340b57cec5SDimitry Andric       const char *IDEnd = IDStart;
2350b57cec5SDimitry Andric       while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric       unsigned Val;
2380b57cec5SDimitry Andric       if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
2390b57cec5SDimitry Andric         report_fatal_error("Bad $ operand number in inline asm string: '" +
2400b57cec5SDimitry Andric                            Twine(AsmStr) + "'");
2410b57cec5SDimitry Andric       LastEmitted = IDEnd;
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric       if (Val >= NumOperands-1)
2440b57cec5SDimitry Andric         report_fatal_error("Invalid $ operand number in inline asm string: '" +
2450b57cec5SDimitry Andric                            Twine(AsmStr) + "'");
2460b57cec5SDimitry Andric 
247480093f4SDimitry Andric       char Modifier[2] = { 0, 0 };
248480093f4SDimitry Andric 
249480093f4SDimitry Andric       if (HasCurlyBraces) {
250480093f4SDimitry Andric         // If we have curly braces, check for a modifier character.  This
251480093f4SDimitry Andric         // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
252480093f4SDimitry Andric         if (*LastEmitted == ':') {
253480093f4SDimitry Andric           ++LastEmitted;    // Consume ':' character.
254480093f4SDimitry Andric           if (*LastEmitted == 0)
255480093f4SDimitry Andric             report_fatal_error("Bad ${:} expression in inline asm string: '" +
256480093f4SDimitry Andric                                Twine(AsmStr) + "'");
257480093f4SDimitry Andric 
258480093f4SDimitry Andric           Modifier[0] = *LastEmitted;
259480093f4SDimitry Andric           ++LastEmitted;    // Consume modifier character.
260480093f4SDimitry Andric         }
261480093f4SDimitry Andric 
262480093f4SDimitry Andric         if (*LastEmitted != '}')
263480093f4SDimitry Andric           report_fatal_error("Bad ${} expression in inline asm string: '" +
264480093f4SDimitry Andric                              Twine(AsmStr) + "'");
265480093f4SDimitry Andric         ++LastEmitted;    // Consume '}' character.
266480093f4SDimitry Andric       }
267480093f4SDimitry Andric 
2680b57cec5SDimitry Andric       // Okay, we finally have a value number.  Ask the target to print this
2690b57cec5SDimitry Andric       // operand!
2700b57cec5SDimitry Andric       unsigned OpNo = InlineAsm::MIOp_FirstOperand;
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric       bool Error = false;
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric       // Scan to find the machine operand number for the operand.
2750b57cec5SDimitry Andric       for (; Val; --Val) {
2760b57cec5SDimitry Andric         if (OpNo >= MI->getNumOperands()) break;
2770b57cec5SDimitry Andric         unsigned OpFlags = MI->getOperand(OpNo).getImm();
2780b57cec5SDimitry Andric         OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
2790b57cec5SDimitry Andric       }
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric       // We may have a location metadata attached to the end of the
2820b57cec5SDimitry Andric       // instruction, and at no point should see metadata at any
2830b57cec5SDimitry Andric       // other point while processing. It's an error if so.
2840b57cec5SDimitry Andric       if (OpNo >= MI->getNumOperands() ||
2850b57cec5SDimitry Andric           MI->getOperand(OpNo).isMetadata()) {
2860b57cec5SDimitry Andric         Error = true;
2870b57cec5SDimitry Andric       } else {
2880b57cec5SDimitry Andric         unsigned OpFlags = MI->getOperand(OpNo).getImm();
2890b57cec5SDimitry Andric         ++OpNo;  // Skip over the ID number.
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric         if (InlineAsm::isMemKind(OpFlags)) {
292480093f4SDimitry Andric           Error = AP->PrintAsmMemoryOperand(
293480093f4SDimitry Andric               MI, OpNo, Modifier[0] ? Modifier : nullptr, OS);
2940b57cec5SDimitry Andric         } else {
295480093f4SDimitry Andric           Error = AP->PrintAsmOperand(MI, OpNo,
296480093f4SDimitry Andric                                       Modifier[0] ? Modifier : nullptr, OS);
2970b57cec5SDimitry Andric         }
2980b57cec5SDimitry Andric       }
2990b57cec5SDimitry Andric       if (Error) {
3000b57cec5SDimitry Andric         std::string msg;
3010b57cec5SDimitry Andric         raw_string_ostream Msg(msg);
3020b57cec5SDimitry Andric         Msg << "invalid operand in inline asm: '" << AsmStr << "'";
3030b57cec5SDimitry Andric         MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
3040b57cec5SDimitry Andric       }
3050b57cec5SDimitry Andric       break;
3060b57cec5SDimitry Andric     }
3070b57cec5SDimitry Andric     }
3080b57cec5SDimitry Andric   }
3090b57cec5SDimitry Andric   OS << "\n\t.att_syntax\n" << (char)0;  // null terminate string.
3100b57cec5SDimitry Andric }
3110b57cec5SDimitry Andric 
3120b57cec5SDimitry Andric static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
3130b57cec5SDimitry Andric                                 MachineModuleInfo *MMI, int AsmPrinterVariant,
3140b57cec5SDimitry Andric                                 AsmPrinter *AP, unsigned LocCookie,
3150b57cec5SDimitry Andric                                 raw_ostream &OS) {
3160b57cec5SDimitry Andric   int CurVariant = -1;            // The number of the {.|.|.} region we are in.
3170b57cec5SDimitry Andric   const char *LastEmitted = AsmStr; // One past the last character emitted.
3180b57cec5SDimitry Andric   unsigned NumOperands = MI->getNumOperands();
3190b57cec5SDimitry Andric 
3200b57cec5SDimitry Andric   OS << '\t';
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric   while (*LastEmitted) {
3230b57cec5SDimitry Andric     switch (*LastEmitted) {
3240b57cec5SDimitry Andric     default: {
3250b57cec5SDimitry Andric       // Not a special case, emit the string section literally.
3260b57cec5SDimitry Andric       const char *LiteralEnd = LastEmitted+1;
3270b57cec5SDimitry Andric       while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
3280b57cec5SDimitry Andric              *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
3290b57cec5SDimitry Andric         ++LiteralEnd;
3300b57cec5SDimitry Andric       if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
3310b57cec5SDimitry Andric         OS.write(LastEmitted, LiteralEnd-LastEmitted);
3320b57cec5SDimitry Andric       LastEmitted = LiteralEnd;
3330b57cec5SDimitry Andric       break;
3340b57cec5SDimitry Andric     }
3350b57cec5SDimitry Andric     case '\n':
3360b57cec5SDimitry Andric       ++LastEmitted;   // Consume newline character.
3370b57cec5SDimitry Andric       OS << '\n';      // Indent code with newline.
3380b57cec5SDimitry Andric       break;
3390b57cec5SDimitry Andric     case '$': {
3400b57cec5SDimitry Andric       ++LastEmitted;   // Consume '$' character.
3410b57cec5SDimitry Andric       bool Done = true;
3420b57cec5SDimitry Andric 
3430b57cec5SDimitry Andric       // Handle escapes.
3440b57cec5SDimitry Andric       switch (*LastEmitted) {
3450b57cec5SDimitry Andric       default: Done = false; break;
3460b57cec5SDimitry Andric       case '$':     // $$ -> $
3470b57cec5SDimitry Andric         if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
3480b57cec5SDimitry Andric           OS << '$';
3490b57cec5SDimitry Andric         ++LastEmitted;  // Consume second '$' character.
3500b57cec5SDimitry Andric         break;
3510b57cec5SDimitry Andric       case '(':             // $( -> same as GCC's { character.
3520b57cec5SDimitry Andric         ++LastEmitted;      // Consume '(' character.
3530b57cec5SDimitry Andric         if (CurVariant != -1)
3540b57cec5SDimitry Andric           report_fatal_error("Nested variants found in inline asm string: '" +
3550b57cec5SDimitry Andric                              Twine(AsmStr) + "'");
3560b57cec5SDimitry Andric         CurVariant = 0;     // We're in the first variant now.
3570b57cec5SDimitry Andric         break;
3580b57cec5SDimitry Andric       case '|':
3590b57cec5SDimitry Andric         ++LastEmitted;  // consume '|' character.
3600b57cec5SDimitry Andric         if (CurVariant == -1)
3610b57cec5SDimitry Andric           OS << '|';       // this is gcc's behavior for | outside a variant
3620b57cec5SDimitry Andric         else
3630b57cec5SDimitry Andric           ++CurVariant;   // We're in the next variant.
3640b57cec5SDimitry Andric         break;
3650b57cec5SDimitry Andric       case ')':         // $) -> same as GCC's } char.
3660b57cec5SDimitry Andric         ++LastEmitted;  // consume ')' character.
3670b57cec5SDimitry Andric         if (CurVariant == -1)
3680b57cec5SDimitry Andric           OS << '}';     // this is gcc's behavior for } outside a variant
3690b57cec5SDimitry Andric         else
3700b57cec5SDimitry Andric           CurVariant = -1;
3710b57cec5SDimitry Andric         break;
3720b57cec5SDimitry Andric       }
3730b57cec5SDimitry Andric       if (Done) break;
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric       bool HasCurlyBraces = false;
3760b57cec5SDimitry Andric       if (*LastEmitted == '{') {     // ${variable}
3770b57cec5SDimitry Andric         ++LastEmitted;               // Consume '{' character.
3780b57cec5SDimitry Andric         HasCurlyBraces = true;
3790b57cec5SDimitry Andric       }
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric       // If we have ${:foo}, then this is not a real operand reference, it is a
3820b57cec5SDimitry Andric       // "magic" string reference, just like in .td files.  Arrange to call
3830b57cec5SDimitry Andric       // PrintSpecial.
3840b57cec5SDimitry Andric       if (HasCurlyBraces && *LastEmitted == ':') {
3850b57cec5SDimitry Andric         ++LastEmitted;
3860b57cec5SDimitry Andric         const char *StrStart = LastEmitted;
3870b57cec5SDimitry Andric         const char *StrEnd = strchr(StrStart, '}');
3880b57cec5SDimitry Andric         if (!StrEnd)
3890b57cec5SDimitry Andric           report_fatal_error("Unterminated ${:foo} operand in inline asm"
3900b57cec5SDimitry Andric                              " string: '" + Twine(AsmStr) + "'");
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric         std::string Val(StrStart, StrEnd);
3930b57cec5SDimitry Andric         AP->PrintSpecial(MI, OS, Val.c_str());
3940b57cec5SDimitry Andric         LastEmitted = StrEnd+1;
3950b57cec5SDimitry Andric         break;
3960b57cec5SDimitry Andric       }
3970b57cec5SDimitry Andric 
3980b57cec5SDimitry Andric       const char *IDStart = LastEmitted;
3990b57cec5SDimitry Andric       const char *IDEnd = IDStart;
4000b57cec5SDimitry Andric       while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
4010b57cec5SDimitry Andric 
4020b57cec5SDimitry Andric       unsigned Val;
4030b57cec5SDimitry Andric       if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
4040b57cec5SDimitry Andric         report_fatal_error("Bad $ operand number in inline asm string: '" +
4050b57cec5SDimitry Andric                            Twine(AsmStr) + "'");
4060b57cec5SDimitry Andric       LastEmitted = IDEnd;
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric       char Modifier[2] = { 0, 0 };
4090b57cec5SDimitry Andric 
4100b57cec5SDimitry Andric       if (HasCurlyBraces) {
4110b57cec5SDimitry Andric         // If we have curly braces, check for a modifier character.  This
4120b57cec5SDimitry Andric         // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
4130b57cec5SDimitry Andric         if (*LastEmitted == ':') {
4140b57cec5SDimitry Andric           ++LastEmitted;    // Consume ':' character.
4150b57cec5SDimitry Andric           if (*LastEmitted == 0)
4160b57cec5SDimitry Andric             report_fatal_error("Bad ${:} expression in inline asm string: '" +
4170b57cec5SDimitry Andric                                Twine(AsmStr) + "'");
4180b57cec5SDimitry Andric 
4190b57cec5SDimitry Andric           Modifier[0] = *LastEmitted;
4200b57cec5SDimitry Andric           ++LastEmitted;    // Consume modifier character.
4210b57cec5SDimitry Andric         }
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric         if (*LastEmitted != '}')
4240b57cec5SDimitry Andric           report_fatal_error("Bad ${} expression in inline asm string: '" +
4250b57cec5SDimitry Andric                              Twine(AsmStr) + "'");
4260b57cec5SDimitry Andric         ++LastEmitted;    // Consume '}' character.
4270b57cec5SDimitry Andric       }
4280b57cec5SDimitry Andric 
4290b57cec5SDimitry Andric       if (Val >= NumOperands-1)
4300b57cec5SDimitry Andric         report_fatal_error("Invalid $ operand number in inline asm string: '" +
4310b57cec5SDimitry Andric                            Twine(AsmStr) + "'");
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric       // Okay, we finally have a value number.  Ask the target to print this
4340b57cec5SDimitry Andric       // operand!
4350b57cec5SDimitry Andric       if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
4360b57cec5SDimitry Andric         unsigned OpNo = InlineAsm::MIOp_FirstOperand;
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric         bool Error = false;
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric         // Scan to find the machine operand number for the operand.
4410b57cec5SDimitry Andric         for (; Val; --Val) {
4420b57cec5SDimitry Andric           if (OpNo >= MI->getNumOperands()) break;
4430b57cec5SDimitry Andric           unsigned OpFlags = MI->getOperand(OpNo).getImm();
4440b57cec5SDimitry Andric           OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
4450b57cec5SDimitry Andric         }
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric         // We may have a location metadata attached to the end of the
4480b57cec5SDimitry Andric         // instruction, and at no point should see metadata at any
4490b57cec5SDimitry Andric         // other point while processing. It's an error if so.
4500b57cec5SDimitry Andric         if (OpNo >= MI->getNumOperands() ||
4510b57cec5SDimitry Andric             MI->getOperand(OpNo).isMetadata()) {
4520b57cec5SDimitry Andric           Error = true;
4530b57cec5SDimitry Andric         } else {
4540b57cec5SDimitry Andric           unsigned OpFlags = MI->getOperand(OpNo).getImm();
4550b57cec5SDimitry Andric           ++OpNo;  // Skip over the ID number.
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric           // FIXME: Shouldn't arch-independent output template handling go into
4580b57cec5SDimitry Andric           // PrintAsmOperand?
459480093f4SDimitry Andric           // Labels are target independent.
4600b57cec5SDimitry Andric           if (MI->getOperand(OpNo).isBlockAddress()) {
4610b57cec5SDimitry Andric             const BlockAddress *BA = MI->getOperand(OpNo).getBlockAddress();
4620b57cec5SDimitry Andric             MCSymbol *Sym = AP->GetBlockAddressSymbol(BA);
4630b57cec5SDimitry Andric             Sym->print(OS, AP->MAI);
4640b57cec5SDimitry Andric             MMI->getContext().registerInlineAsmLabel(Sym);
4650b57cec5SDimitry Andric           } else if (MI->getOperand(OpNo).isMBB()) {
4660b57cec5SDimitry Andric             const MCSymbol *Sym = MI->getOperand(OpNo).getMBB()->getSymbol();
4670b57cec5SDimitry Andric             Sym->print(OS, AP->MAI);
468480093f4SDimitry Andric           } else if (Modifier[0] == 'l') {
4690b57cec5SDimitry Andric             Error = true;
470480093f4SDimitry Andric           } else if (InlineAsm::isMemKind(OpFlags)) {
4710b57cec5SDimitry Andric             Error = AP->PrintAsmMemoryOperand(
4720b57cec5SDimitry Andric                 MI, OpNo, Modifier[0] ? Modifier : nullptr, OS);
4730b57cec5SDimitry Andric           } else {
4740b57cec5SDimitry Andric             Error = AP->PrintAsmOperand(MI, OpNo,
4750b57cec5SDimitry Andric                                         Modifier[0] ? Modifier : nullptr, OS);
4760b57cec5SDimitry Andric           }
4770b57cec5SDimitry Andric         }
4780b57cec5SDimitry Andric         if (Error) {
4790b57cec5SDimitry Andric           std::string msg;
4800b57cec5SDimitry Andric           raw_string_ostream Msg(msg);
4810b57cec5SDimitry Andric           Msg << "invalid operand in inline asm: '" << AsmStr << "'";
4820b57cec5SDimitry Andric           MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
4830b57cec5SDimitry Andric         }
4840b57cec5SDimitry Andric       }
4850b57cec5SDimitry Andric       break;
4860b57cec5SDimitry Andric     }
4870b57cec5SDimitry Andric     }
4880b57cec5SDimitry Andric   }
4890b57cec5SDimitry Andric   OS << '\n' << (char)0;  // null terminate string.
4900b57cec5SDimitry Andric }
4910b57cec5SDimitry Andric 
492*5ffd83dbSDimitry Andric /// This method formats and emits the specified machine instruction that is an
493*5ffd83dbSDimitry Andric /// inline asm.
494*5ffd83dbSDimitry Andric void AsmPrinter::emitInlineAsm(const MachineInstr *MI) const {
4950b57cec5SDimitry Andric   assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
4960b57cec5SDimitry Andric 
4970b57cec5SDimitry Andric   // Count the number of register definitions to find the asm string.
4980b57cec5SDimitry Andric   unsigned NumDefs = 0;
4990b57cec5SDimitry Andric   for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
5000b57cec5SDimitry Andric        ++NumDefs)
5010b57cec5SDimitry Andric     assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric   assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
5040b57cec5SDimitry Andric 
5050b57cec5SDimitry Andric   // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
5060b57cec5SDimitry Andric   const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
5070b57cec5SDimitry Andric 
5080b57cec5SDimitry Andric   // If this asmstr is empty, just print the #APP/#NOAPP markers.
5090b57cec5SDimitry Andric   // These are useful to see where empty asm's wound up.
5100b57cec5SDimitry Andric   if (AsmStr[0] == 0) {
5110b57cec5SDimitry Andric     OutStreamer->emitRawComment(MAI->getInlineAsmStart());
5120b57cec5SDimitry Andric     OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
5130b57cec5SDimitry Andric     return;
5140b57cec5SDimitry Andric   }
5150b57cec5SDimitry Andric 
5160b57cec5SDimitry Andric   // Emit the #APP start marker.  This has to happen even if verbose-asm isn't
5170b57cec5SDimitry Andric   // enabled, so we use emitRawComment.
5180b57cec5SDimitry Andric   OutStreamer->emitRawComment(MAI->getInlineAsmStart());
5190b57cec5SDimitry Andric 
5200b57cec5SDimitry Andric   // Get the !srcloc metadata node if we have it, and decode the loc cookie from
5210b57cec5SDimitry Andric   // it.
5220b57cec5SDimitry Andric   unsigned LocCookie = 0;
5230b57cec5SDimitry Andric   const MDNode *LocMD = nullptr;
5240b57cec5SDimitry Andric   for (unsigned i = MI->getNumOperands(); i != 0; --i) {
5250b57cec5SDimitry Andric     if (MI->getOperand(i-1).isMetadata() &&
5260b57cec5SDimitry Andric         (LocMD = MI->getOperand(i-1).getMetadata()) &&
5270b57cec5SDimitry Andric         LocMD->getNumOperands() != 0) {
5280b57cec5SDimitry Andric       if (const ConstantInt *CI =
5290b57cec5SDimitry Andric               mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
5300b57cec5SDimitry Andric         LocCookie = CI->getZExtValue();
5310b57cec5SDimitry Andric         break;
5320b57cec5SDimitry Andric       }
5330b57cec5SDimitry Andric     }
5340b57cec5SDimitry Andric   }
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric   // Emit the inline asm to a temporary string so we can emit it through
5370b57cec5SDimitry Andric   // EmitInlineAsm.
5380b57cec5SDimitry Andric   SmallString<256> StringData;
5390b57cec5SDimitry Andric   raw_svector_ostream OS(StringData);
5400b57cec5SDimitry Andric 
5410b57cec5SDimitry Andric   // The variant of the current asmprinter.
5420b57cec5SDimitry Andric   int AsmPrinterVariant = MAI->getAssemblerDialect();
5430b57cec5SDimitry Andric   AsmPrinter *AP = const_cast<AsmPrinter*>(this);
5440b57cec5SDimitry Andric   if (MI->getInlineAsmDialect() == InlineAsm::AD_ATT)
5450b57cec5SDimitry Andric     EmitGCCInlineAsmStr(AsmStr, MI, MMI, AsmPrinterVariant, AP, LocCookie, OS);
5460b57cec5SDimitry Andric   else
5470b57cec5SDimitry Andric     EmitMSInlineAsmStr(AsmStr, MI, MMI, AP, LocCookie, OS);
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric   // Emit warnings if we use reserved registers on the clobber list, as
5500b57cec5SDimitry Andric   // that might give surprising results.
5510b57cec5SDimitry Andric   std::vector<std::string> RestrRegs;
5520b57cec5SDimitry Andric   // Start with the first operand descriptor, and iterate over them.
5530b57cec5SDimitry Andric   for (unsigned I = InlineAsm::MIOp_FirstOperand, NumOps = MI->getNumOperands();
5540b57cec5SDimitry Andric        I < NumOps; ++I) {
5550b57cec5SDimitry Andric     const MachineOperand &MO = MI->getOperand(I);
5560b57cec5SDimitry Andric     if (MO.isImm()) {
5570b57cec5SDimitry Andric       unsigned Flags = MO.getImm();
5580b57cec5SDimitry Andric       const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
5590b57cec5SDimitry Andric       if (InlineAsm::getKind(Flags) == InlineAsm::Kind_Clobber &&
5600b57cec5SDimitry Andric           !TRI->isAsmClobberable(*MF, MI->getOperand(I + 1).getReg())) {
5610b57cec5SDimitry Andric         RestrRegs.push_back(TRI->getName(MI->getOperand(I + 1).getReg()));
5620b57cec5SDimitry Andric       }
5630b57cec5SDimitry Andric       // Skip to one before the next operand descriptor, if it exists.
5640b57cec5SDimitry Andric       I += InlineAsm::getNumOperandRegisters(Flags);
5650b57cec5SDimitry Andric     }
5660b57cec5SDimitry Andric   }
5670b57cec5SDimitry Andric 
5680b57cec5SDimitry Andric   if (!RestrRegs.empty()) {
5690b57cec5SDimitry Andric     unsigned BufNum = addInlineAsmDiagBuffer(OS.str(), LocMD);
5700b57cec5SDimitry Andric     auto &SrcMgr = DiagInfo->SrcMgr;
5710b57cec5SDimitry Andric     SMLoc Loc = SMLoc::getFromPointer(
5720b57cec5SDimitry Andric         SrcMgr.getMemoryBuffer(BufNum)->getBuffer().begin());
5730b57cec5SDimitry Andric 
5740b57cec5SDimitry Andric     std::string Msg = "inline asm clobber list contains reserved registers: ";
5750b57cec5SDimitry Andric     for (auto I = RestrRegs.begin(), E = RestrRegs.end(); I != E; I++) {
5760b57cec5SDimitry Andric       if(I != RestrRegs.begin())
5770b57cec5SDimitry Andric         Msg += ", ";
5780b57cec5SDimitry Andric       Msg += *I;
5790b57cec5SDimitry Andric     }
5800b57cec5SDimitry Andric     std::string Note = "Reserved registers on the clobber list may not be "
5810b57cec5SDimitry Andric                 "preserved across the asm statement, and clobbering them may "
5820b57cec5SDimitry Andric                 "lead to undefined behaviour.";
5830b57cec5SDimitry Andric     SrcMgr.PrintMessage(Loc, SourceMgr::DK_Warning, Msg);
5840b57cec5SDimitry Andric     SrcMgr.PrintMessage(Loc, SourceMgr::DK_Note, Note);
5850b57cec5SDimitry Andric   }
5860b57cec5SDimitry Andric 
587*5ffd83dbSDimitry Andric   emitInlineAsm(OS.str(), getSubtargetInfo(), TM.Options.MCOptions, LocMD,
5880b57cec5SDimitry Andric                 MI->getInlineAsmDialect());
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric   // Emit the #NOAPP end marker.  This has to happen even if verbose-asm isn't
5910b57cec5SDimitry Andric   // enabled, so we use emitRawComment.
5920b57cec5SDimitry Andric   OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
5930b57cec5SDimitry Andric }
5940b57cec5SDimitry Andric 
5950b57cec5SDimitry Andric /// PrintSpecial - Print information related to the specified machine instr
5960b57cec5SDimitry Andric /// that is independent of the operand, and may be independent of the instr
5970b57cec5SDimitry Andric /// itself.  This can be useful for portably encoding the comment character
5980b57cec5SDimitry Andric /// or other bits of target-specific knowledge into the asmstrings.  The
5990b57cec5SDimitry Andric /// syntax used is ${:comment}.  Targets can override this to add support
6000b57cec5SDimitry Andric /// for their own strange codes.
6010b57cec5SDimitry Andric void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
6020b57cec5SDimitry Andric                               const char *Code) const {
6030b57cec5SDimitry Andric   if (!strcmp(Code, "private")) {
6040b57cec5SDimitry Andric     const DataLayout &DL = MF->getDataLayout();
6050b57cec5SDimitry Andric     OS << DL.getPrivateGlobalPrefix();
6060b57cec5SDimitry Andric   } else if (!strcmp(Code, "comment")) {
6070b57cec5SDimitry Andric     OS << MAI->getCommentString();
6080b57cec5SDimitry Andric   } else if (!strcmp(Code, "uid")) {
6090b57cec5SDimitry Andric     // Comparing the address of MI isn't sufficient, because machineinstrs may
6100b57cec5SDimitry Andric     // be allocated to the same address across functions.
6110b57cec5SDimitry Andric 
6120b57cec5SDimitry Andric     // If this is a new LastFn instruction, bump the counter.
6130b57cec5SDimitry Andric     if (LastMI != MI || LastFn != getFunctionNumber()) {
6140b57cec5SDimitry Andric       ++Counter;
6150b57cec5SDimitry Andric       LastMI = MI;
6160b57cec5SDimitry Andric       LastFn = getFunctionNumber();
6170b57cec5SDimitry Andric     }
6180b57cec5SDimitry Andric     OS << Counter;
6190b57cec5SDimitry Andric   } else {
6200b57cec5SDimitry Andric     std::string msg;
6210b57cec5SDimitry Andric     raw_string_ostream Msg(msg);
6220b57cec5SDimitry Andric     Msg << "Unknown special formatter '" << Code
6230b57cec5SDimitry Andric          << "' for machine instr: " << *MI;
6240b57cec5SDimitry Andric     report_fatal_error(Msg.str());
6250b57cec5SDimitry Andric   }
6260b57cec5SDimitry Andric }
6270b57cec5SDimitry Andric 
6280b57cec5SDimitry Andric void AsmPrinter::PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS) {
6290b57cec5SDimitry Andric   assert(MO.isGlobal() && "caller should check MO.isGlobal");
6300b57cec5SDimitry Andric   getSymbol(MO.getGlobal())->print(OS, MAI);
6310b57cec5SDimitry Andric   printOffset(MO.getOffset(), OS);
6320b57cec5SDimitry Andric }
6330b57cec5SDimitry Andric 
6340b57cec5SDimitry Andric /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
6350b57cec5SDimitry Andric /// instruction, using the specified assembler variant.  Targets should
6360b57cec5SDimitry Andric /// override this to format as appropriate for machine specific ExtraCodes
6370b57cec5SDimitry Andric /// or when the arch-independent handling would be too complex otherwise.
6380b57cec5SDimitry Andric bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
6390b57cec5SDimitry Andric                                  const char *ExtraCode, raw_ostream &O) {
6400b57cec5SDimitry Andric   // Does this asm operand have a single letter operand modifier?
6410b57cec5SDimitry Andric   if (ExtraCode && ExtraCode[0]) {
6420b57cec5SDimitry Andric     if (ExtraCode[1] != 0) return true; // Unknown modifier.
6430b57cec5SDimitry Andric 
6440b57cec5SDimitry Andric     // https://gcc.gnu.org/onlinedocs/gccint/Output-Template.html
6450b57cec5SDimitry Andric     const MachineOperand &MO = MI->getOperand(OpNo);
6460b57cec5SDimitry Andric     switch (ExtraCode[0]) {
6470b57cec5SDimitry Andric     default:
6480b57cec5SDimitry Andric       return true;  // Unknown modifier.
6490b57cec5SDimitry Andric     case 'a': // Print as memory address.
6500b57cec5SDimitry Andric       if (MO.isReg()) {
6510b57cec5SDimitry Andric         PrintAsmMemoryOperand(MI, OpNo, nullptr, O);
6520b57cec5SDimitry Andric         return false;
6530b57cec5SDimitry Andric       }
6540b57cec5SDimitry Andric       LLVM_FALLTHROUGH; // GCC allows '%a' to behave like '%c' with immediates.
6550b57cec5SDimitry Andric     case 'c': // Substitute immediate value without immediate syntax
6560b57cec5SDimitry Andric       if (MO.isImm()) {
6570b57cec5SDimitry Andric         O << MO.getImm();
6580b57cec5SDimitry Andric         return false;
6590b57cec5SDimitry Andric       }
6600b57cec5SDimitry Andric       if (MO.isGlobal()) {
6610b57cec5SDimitry Andric         PrintSymbolOperand(MO, O);
6620b57cec5SDimitry Andric         return false;
6630b57cec5SDimitry Andric       }
6640b57cec5SDimitry Andric       return true;
6650b57cec5SDimitry Andric     case 'n':  // Negate the immediate constant.
6660b57cec5SDimitry Andric       if (!MO.isImm())
6670b57cec5SDimitry Andric         return true;
6680b57cec5SDimitry Andric       O << -MO.getImm();
6690b57cec5SDimitry Andric       return false;
6700b57cec5SDimitry Andric     case 's':  // The GCC deprecated s modifier
6710b57cec5SDimitry Andric       if (!MO.isImm())
6720b57cec5SDimitry Andric         return true;
6730b57cec5SDimitry Andric       O << ((32 - MO.getImm()) & 31);
6740b57cec5SDimitry Andric       return false;
6750b57cec5SDimitry Andric     }
6760b57cec5SDimitry Andric   }
6770b57cec5SDimitry Andric   return true;
6780b57cec5SDimitry Andric }
6790b57cec5SDimitry Andric 
6800b57cec5SDimitry Andric bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
6810b57cec5SDimitry Andric                                        const char *ExtraCode, raw_ostream &O) {
6820b57cec5SDimitry Andric   // Target doesn't support this yet!
6830b57cec5SDimitry Andric   return true;
6840b57cec5SDimitry Andric }
6850b57cec5SDimitry Andric 
6860b57cec5SDimitry Andric void AsmPrinter::emitInlineAsmStart() const {}
6870b57cec5SDimitry Andric 
6880b57cec5SDimitry Andric void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
6890b57cec5SDimitry Andric                                   const MCSubtargetInfo *EndInfo) const {}
690