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