1 //===-- AsmPrinterInlineAsm.cpp - AsmPrinter Inline Asm Handling ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the inline assembler pieces of the AsmPrinter class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineModuleInfo.h"
20 #include "llvm/CodeGen/TargetInstrInfo.h"
21 #include "llvm/CodeGen/TargetRegisterInfo.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/InlineAsm.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/MC/MCAsmInfo.h"
28 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSubtargetInfo.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/Support/SourceMgr.h"
35 #include "llvm/Support/TargetRegistry.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Target/TargetMachine.h"
38 using namespace llvm;
39 
40 #define DEBUG_TYPE "asm-printer"
41 
42 /// srcMgrDiagHandler - This callback is invoked when the SourceMgr for an
43 /// inline asm has an error in it.  diagInfo is a pointer to the SrcMgrDiagInfo
44 /// struct above.
45 static void srcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
46   AsmPrinter::SrcMgrDiagInfo *DiagInfo =
47       static_cast<AsmPrinter::SrcMgrDiagInfo *>(diagInfo);
48   assert(DiagInfo && "Diagnostic context not passed down?");
49 
50   // Look up a LocInfo for the buffer this diagnostic is coming from.
51   unsigned BufNum = DiagInfo->SrcMgr.FindBufferContainingLoc(Diag.getLoc());
52   const MDNode *LocInfo = nullptr;
53   if (BufNum > 0 && BufNum <= DiagInfo->LocInfos.size())
54     LocInfo = DiagInfo->LocInfos[BufNum-1];
55 
56   // If the inline asm had metadata associated with it, pull out a location
57   // cookie corresponding to which line the error occurred on.
58   unsigned LocCookie = 0;
59   if (LocInfo) {
60     unsigned ErrorLine = Diag.getLineNo()-1;
61     if (ErrorLine >= LocInfo->getNumOperands())
62       ErrorLine = 0;
63 
64     if (LocInfo->getNumOperands() != 0)
65       if (const ConstantInt *CI =
66               mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
67         LocCookie = CI->getZExtValue();
68   }
69 
70   DiagInfo->DiagHandler(Diag, DiagInfo->DiagContext, LocCookie);
71 }
72 
73 unsigned AsmPrinter::addInlineAsmDiagBuffer(StringRef AsmStr,
74                                             const MDNode *LocMDNode) const {
75   if (!DiagInfo) {
76     DiagInfo = std::make_unique<SrcMgrDiagInfo>();
77 
78     MCContext &Context = MMI->getContext();
79     Context.setInlineSourceManager(&DiagInfo->SrcMgr);
80 
81     LLVMContext &LLVMCtx = MMI->getModule()->getContext();
82     if (LLVMCtx.getInlineAsmDiagnosticHandler()) {
83       DiagInfo->DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
84       DiagInfo->DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
85       DiagInfo->SrcMgr.setDiagHandler(srcMgrDiagHandler, DiagInfo.get());
86     }
87   }
88 
89   SourceMgr &SrcMgr = DiagInfo->SrcMgr;
90 
91   std::unique_ptr<MemoryBuffer> Buffer;
92   // The inline asm source manager will outlive AsmStr, so make a copy of the
93   // string for SourceMgr to own.
94   Buffer = MemoryBuffer::getMemBufferCopy(AsmStr, "<inline asm>");
95 
96   // Tell SrcMgr about this buffer, it takes ownership of the buffer.
97   unsigned BufNum = SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
98 
99   // Store LocMDNode in DiagInfo, using BufNum as an identifier.
100   if (LocMDNode) {
101     DiagInfo->LocInfos.resize(BufNum);
102     DiagInfo->LocInfos[BufNum - 1] = LocMDNode;
103   }
104 
105   return BufNum;
106 }
107 
108 
109 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
110 void AsmPrinter::emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
111                                const MCTargetOptions &MCOptions,
112                                const MDNode *LocMDNode,
113                                InlineAsm::AsmDialect Dialect) const {
114   assert(!Str.empty() && "Can't emit empty inline asm block");
115 
116   // Remember if the buffer is nul terminated or not so we can avoid a copy.
117   bool isNullTerminated = Str.back() == 0;
118   if (isNullTerminated)
119     Str = Str.substr(0, Str.size()-1);
120 
121   // If the output streamer does not have mature MC support or the integrated
122   // assembler has been disabled, just emit the blob textually.
123   // Otherwise parse the asm and emit it via MC support.
124   // This is useful in case the asm parser doesn't handle something but the
125   // system assembler does.
126   const MCAsmInfo *MCAI = TM.getMCAsmInfo();
127   assert(MCAI && "No MCAsmInfo");
128   if (!MCAI->useIntegratedAssembler() &&
129       !OutStreamer->isIntegratedAssemblerRequired()) {
130     emitInlineAsmStart();
131     OutStreamer->emitRawText(Str);
132     emitInlineAsmEnd(STI, nullptr);
133     return;
134   }
135 
136   unsigned BufNum = addInlineAsmDiagBuffer(Str, LocMDNode);
137   DiagInfo->SrcMgr.setIncludeDirs(MCOptions.IASSearchPaths);
138 
139   std::unique_ptr<MCAsmParser> Parser(createMCAsmParser(
140           DiagInfo->SrcMgr, OutContext, *OutStreamer, *MAI, BufNum));
141 
142   // Do not use assembler-level information for parsing inline assembly.
143   OutStreamer->setUseAssemblerInfoForParsing(false);
144 
145   // We create a new MCInstrInfo here since we might be at the module level
146   // and not have a MachineFunction to initialize the TargetInstrInfo from and
147   // we only need MCInstrInfo for asm parsing. We create one unconditionally
148   // because it's not subtarget dependent.
149   std::unique_ptr<MCInstrInfo> MII(TM.getTarget().createMCInstrInfo());
150   std::unique_ptr<MCTargetAsmParser> TAP(TM.getTarget().createMCAsmParser(
151       STI, *Parser, *MII, MCOptions));
152   if (!TAP)
153     report_fatal_error("Inline asm not supported by this streamer because"
154                        " we don't have an asm parser for this target\n");
155   Parser->setAssemblerDialect(Dialect);
156   Parser->setTargetParser(*TAP.get());
157   // Enable lexing Masm binary and hex integer literals in intel inline
158   // assembly.
159   if (Dialect == InlineAsm::AD_Intel)
160     Parser->getLexer().setLexMasmIntegers(true);
161 
162   emitInlineAsmStart();
163   // Don't implicitly switch to the text section before the asm.
164   int Res = Parser->Run(/*NoInitialTextSection*/ true,
165                         /*NoFinalize*/ true);
166   emitInlineAsmEnd(STI, &TAP->getSTI());
167 
168   if (Res && !DiagInfo->DiagHandler)
169     report_fatal_error("Error parsing inline asm\n");
170 }
171 
172 static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
173                                MachineModuleInfo *MMI, AsmPrinter *AP,
174                                unsigned LocCookie, raw_ostream &OS) {
175   // Switch to the inline assembly variant.
176   OS << "\t.intel_syntax\n\t";
177 
178   const char *LastEmitted = AsmStr; // One past the last character emitted.
179   unsigned NumOperands = MI->getNumOperands();
180 
181   while (*LastEmitted) {
182     switch (*LastEmitted) {
183     default: {
184       // Not a special case, emit the string section literally.
185       const char *LiteralEnd = LastEmitted+1;
186       while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
187              *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
188         ++LiteralEnd;
189 
190       OS.write(LastEmitted, LiteralEnd-LastEmitted);
191       LastEmitted = LiteralEnd;
192       break;
193     }
194     case '\n':
195       ++LastEmitted;   // Consume newline character.
196       OS << '\n';      // Indent code with newline.
197       break;
198     case '$': {
199       ++LastEmitted;   // Consume '$' character.
200       bool Done = true;
201 
202       // Handle escapes.
203       switch (*LastEmitted) {
204       default: Done = false; break;
205       case '$':
206         ++LastEmitted;  // Consume second '$' character.
207         break;
208       }
209       if (Done) break;
210 
211       bool HasCurlyBraces = false;
212       if (*LastEmitted == '{') {     // ${variable}
213         ++LastEmitted;               // Consume '{' character.
214         HasCurlyBraces = true;
215       }
216 
217       // If we have ${:foo}, then this is not a real operand reference, it is a
218       // "magic" string reference, just like in .td files.  Arrange to call
219       // PrintSpecial.
220       if (HasCurlyBraces && LastEmitted[0] == ':') {
221         ++LastEmitted;
222         const char *StrStart = LastEmitted;
223         const char *StrEnd = strchr(StrStart, '}');
224         if (!StrEnd)
225           report_fatal_error("Unterminated ${:foo} operand in inline asm"
226                              " string: '" + Twine(AsmStr) + "'");
227 
228         std::string Val(StrStart, StrEnd);
229         AP->PrintSpecial(MI, OS, Val.c_str());
230         LastEmitted = StrEnd+1;
231         break;
232       }
233 
234       const char *IDStart = LastEmitted;
235       const char *IDEnd = IDStart;
236       while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
237 
238       unsigned Val;
239       if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
240         report_fatal_error("Bad $ operand number in inline asm string: '" +
241                            Twine(AsmStr) + "'");
242       LastEmitted = IDEnd;
243 
244       if (Val >= NumOperands-1)
245         report_fatal_error("Invalid $ operand number in inline asm string: '" +
246                            Twine(AsmStr) + "'");
247 
248       char Modifier[2] = { 0, 0 };
249 
250       if (HasCurlyBraces) {
251         // If we have curly braces, check for a modifier character.  This
252         // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
253         if (*LastEmitted == ':') {
254           ++LastEmitted;    // Consume ':' character.
255           if (*LastEmitted == 0)
256             report_fatal_error("Bad ${:} expression in inline asm string: '" +
257                                Twine(AsmStr) + "'");
258 
259           Modifier[0] = *LastEmitted;
260           ++LastEmitted;    // Consume modifier character.
261         }
262 
263         if (*LastEmitted != '}')
264           report_fatal_error("Bad ${} expression in inline asm string: '" +
265                              Twine(AsmStr) + "'");
266         ++LastEmitted;    // Consume '}' character.
267       }
268 
269       // Okay, we finally have a value number.  Ask the target to print this
270       // operand!
271       unsigned OpNo = InlineAsm::MIOp_FirstOperand;
272 
273       bool Error = false;
274 
275       // Scan to find the machine operand number for the operand.
276       for (; Val; --Val) {
277         if (OpNo >= MI->getNumOperands()) break;
278         unsigned OpFlags = MI->getOperand(OpNo).getImm();
279         OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
280       }
281 
282       // We may have a location metadata attached to the end of the
283       // instruction, and at no point should see metadata at any
284       // other point while processing. It's an error if so.
285       if (OpNo >= MI->getNumOperands() ||
286           MI->getOperand(OpNo).isMetadata()) {
287         Error = true;
288       } else {
289         unsigned OpFlags = MI->getOperand(OpNo).getImm();
290         ++OpNo;  // Skip over the ID number.
291 
292         if (InlineAsm::isMemKind(OpFlags)) {
293           Error = AP->PrintAsmMemoryOperand(
294               MI, OpNo, Modifier[0] ? Modifier : nullptr, OS);
295         } else {
296           Error = AP->PrintAsmOperand(MI, OpNo,
297                                       Modifier[0] ? Modifier : nullptr, OS);
298         }
299       }
300       if (Error) {
301         std::string msg;
302         raw_string_ostream Msg(msg);
303         Msg << "invalid operand in inline asm: '" << AsmStr << "'";
304         MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
305       }
306       break;
307     }
308     }
309   }
310   OS << "\n\t.att_syntax\n" << (char)0;  // null terminate string.
311 }
312 
313 static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
314                                 MachineModuleInfo *MMI, int AsmPrinterVariant,
315                                 AsmPrinter *AP, unsigned LocCookie,
316                                 raw_ostream &OS) {
317   int CurVariant = -1;            // The number of the {.|.|.} region we are in.
318   const char *LastEmitted = AsmStr; // One past the last character emitted.
319   unsigned NumOperands = MI->getNumOperands();
320 
321   OS << '\t';
322 
323   while (*LastEmitted) {
324     switch (*LastEmitted) {
325     default: {
326       // Not a special case, emit the string section literally.
327       const char *LiteralEnd = LastEmitted+1;
328       while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
329              *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
330         ++LiteralEnd;
331       if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
332         OS.write(LastEmitted, LiteralEnd-LastEmitted);
333       LastEmitted = LiteralEnd;
334       break;
335     }
336     case '\n':
337       ++LastEmitted;   // Consume newline character.
338       OS << '\n';      // Indent code with newline.
339       break;
340     case '$': {
341       ++LastEmitted;   // Consume '$' character.
342       bool Done = true;
343 
344       // Handle escapes.
345       switch (*LastEmitted) {
346       default: Done = false; break;
347       case '$':     // $$ -> $
348         if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
349           OS << '$';
350         ++LastEmitted;  // Consume second '$' character.
351         break;
352       case '(':             // $( -> same as GCC's { character.
353         ++LastEmitted;      // Consume '(' character.
354         if (CurVariant != -1)
355           report_fatal_error("Nested variants found in inline asm string: '" +
356                              Twine(AsmStr) + "'");
357         CurVariant = 0;     // We're in the first variant now.
358         break;
359       case '|':
360         ++LastEmitted;  // consume '|' character.
361         if (CurVariant == -1)
362           OS << '|';       // this is gcc's behavior for | outside a variant
363         else
364           ++CurVariant;   // We're in the next variant.
365         break;
366       case ')':         // $) -> same as GCC's } char.
367         ++LastEmitted;  // consume ')' character.
368         if (CurVariant == -1)
369           OS << '}';     // this is gcc's behavior for } outside a variant
370         else
371           CurVariant = -1;
372         break;
373       }
374       if (Done) break;
375 
376       bool HasCurlyBraces = false;
377       if (*LastEmitted == '{') {     // ${variable}
378         ++LastEmitted;               // Consume '{' character.
379         HasCurlyBraces = true;
380       }
381 
382       // If we have ${:foo}, then this is not a real operand reference, it is a
383       // "magic" string reference, just like in .td files.  Arrange to call
384       // PrintSpecial.
385       if (HasCurlyBraces && *LastEmitted == ':') {
386         ++LastEmitted;
387         const char *StrStart = LastEmitted;
388         const char *StrEnd = strchr(StrStart, '}');
389         if (!StrEnd)
390           report_fatal_error("Unterminated ${:foo} operand in inline asm"
391                              " string: '" + Twine(AsmStr) + "'");
392 
393         std::string Val(StrStart, StrEnd);
394         AP->PrintSpecial(MI, OS, Val.c_str());
395         LastEmitted = StrEnd+1;
396         break;
397       }
398 
399       const char *IDStart = LastEmitted;
400       const char *IDEnd = IDStart;
401       while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
402 
403       unsigned Val;
404       if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
405         report_fatal_error("Bad $ operand number in inline asm string: '" +
406                            Twine(AsmStr) + "'");
407       LastEmitted = IDEnd;
408 
409       char Modifier[2] = { 0, 0 };
410 
411       if (HasCurlyBraces) {
412         // If we have curly braces, check for a modifier character.  This
413         // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
414         if (*LastEmitted == ':') {
415           ++LastEmitted;    // Consume ':' character.
416           if (*LastEmitted == 0)
417             report_fatal_error("Bad ${:} expression in inline asm string: '" +
418                                Twine(AsmStr) + "'");
419 
420           Modifier[0] = *LastEmitted;
421           ++LastEmitted;    // Consume modifier character.
422         }
423 
424         if (*LastEmitted != '}')
425           report_fatal_error("Bad ${} expression in inline asm string: '" +
426                              Twine(AsmStr) + "'");
427         ++LastEmitted;    // Consume '}' character.
428       }
429 
430       if (Val >= NumOperands-1)
431         report_fatal_error("Invalid $ operand number in inline asm string: '" +
432                            Twine(AsmStr) + "'");
433 
434       // Okay, we finally have a value number.  Ask the target to print this
435       // operand!
436       if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
437         unsigned OpNo = InlineAsm::MIOp_FirstOperand;
438 
439         bool Error = false;
440 
441         // Scan to find the machine operand number for the operand.
442         for (; Val; --Val) {
443           if (OpNo >= MI->getNumOperands()) break;
444           unsigned OpFlags = MI->getOperand(OpNo).getImm();
445           OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
446         }
447 
448         // We may have a location metadata attached to the end of the
449         // instruction, and at no point should see metadata at any
450         // other point while processing. It's an error if so.
451         if (OpNo >= MI->getNumOperands() ||
452             MI->getOperand(OpNo).isMetadata()) {
453           Error = true;
454         } else {
455           unsigned OpFlags = MI->getOperand(OpNo).getImm();
456           ++OpNo;  // Skip over the ID number.
457 
458           // FIXME: Shouldn't arch-independent output template handling go into
459           // PrintAsmOperand?
460           // Labels are target independent.
461           if (MI->getOperand(OpNo).isBlockAddress()) {
462             const BlockAddress *BA = MI->getOperand(OpNo).getBlockAddress();
463             MCSymbol *Sym = AP->GetBlockAddressSymbol(BA);
464             Sym->print(OS, AP->MAI);
465             MMI->getContext().registerInlineAsmLabel(Sym);
466           } else if (MI->getOperand(OpNo).isMBB()) {
467             const MCSymbol *Sym = MI->getOperand(OpNo).getMBB()->getSymbol();
468             Sym->print(OS, AP->MAI);
469           } else if (Modifier[0] == 'l') {
470             Error = true;
471           } else if (InlineAsm::isMemKind(OpFlags)) {
472             Error = AP->PrintAsmMemoryOperand(
473                 MI, OpNo, Modifier[0] ? Modifier : nullptr, OS);
474           } else {
475             Error = AP->PrintAsmOperand(MI, OpNo,
476                                         Modifier[0] ? Modifier : nullptr, OS);
477           }
478         }
479         if (Error) {
480           std::string msg;
481           raw_string_ostream Msg(msg);
482           Msg << "invalid operand in inline asm: '" << AsmStr << "'";
483           MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
484         }
485       }
486       break;
487     }
488     }
489   }
490   OS << '\n' << (char)0;  // null terminate string.
491 }
492 
493 /// This method formats and emits the specified machine instruction that is an
494 /// inline asm.
495 void AsmPrinter::emitInlineAsm(const MachineInstr *MI) const {
496   assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
497 
498   // Count the number of register definitions to find the asm string.
499   unsigned NumDefs = 0;
500   for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
501        ++NumDefs)
502     assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
503 
504   assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
505 
506   // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
507   const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
508 
509   // If this asmstr is empty, just print the #APP/#NOAPP markers.
510   // These are useful to see where empty asm's wound up.
511   if (AsmStr[0] == 0) {
512     OutStreamer->emitRawComment(MAI->getInlineAsmStart());
513     OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
514     return;
515   }
516 
517   // Emit the #APP start marker.  This has to happen even if verbose-asm isn't
518   // enabled, so we use emitRawComment.
519   OutStreamer->emitRawComment(MAI->getInlineAsmStart());
520 
521   // Get the !srcloc metadata node if we have it, and decode the loc cookie from
522   // it.
523   unsigned LocCookie = 0;
524   const MDNode *LocMD = nullptr;
525   for (unsigned i = MI->getNumOperands(); i != 0; --i) {
526     if (MI->getOperand(i-1).isMetadata() &&
527         (LocMD = MI->getOperand(i-1).getMetadata()) &&
528         LocMD->getNumOperands() != 0) {
529       if (const ConstantInt *CI =
530               mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
531         LocCookie = CI->getZExtValue();
532         break;
533       }
534     }
535   }
536 
537   // Emit the inline asm to a temporary string so we can emit it through
538   // EmitInlineAsm.
539   SmallString<256> StringData;
540   raw_svector_ostream OS(StringData);
541 
542   // The variant of the current asmprinter.
543   int AsmPrinterVariant = MAI->getAssemblerDialect();
544   AsmPrinter *AP = const_cast<AsmPrinter*>(this);
545   if (MI->getInlineAsmDialect() == InlineAsm::AD_ATT)
546     EmitGCCInlineAsmStr(AsmStr, MI, MMI, AsmPrinterVariant, AP, LocCookie, OS);
547   else
548     EmitMSInlineAsmStr(AsmStr, MI, MMI, AP, LocCookie, OS);
549 
550   // Emit warnings if we use reserved registers on the clobber list, as
551   // that might lead to undefined behaviour.
552   SmallVector<Register, 8> RestrRegs;
553   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
554   // Start with the first operand descriptor, and iterate over them.
555   for (unsigned I = InlineAsm::MIOp_FirstOperand, NumOps = MI->getNumOperands();
556        I < NumOps; ++I) {
557     const MachineOperand &MO = MI->getOperand(I);
558     if (!MO.isImm())
559       continue;
560     unsigned Flags = MO.getImm();
561     if (InlineAsm::getKind(Flags) == InlineAsm::Kind_Clobber) {
562       Register Reg = MI->getOperand(I + 1).getReg();
563       if (!TRI->isAsmClobberable(*MF, Reg))
564         RestrRegs.push_back(Reg);
565     }
566     // Skip to one before the next operand descriptor, if it exists.
567     I += InlineAsm::getNumOperandRegisters(Flags);
568   }
569 
570   if (!RestrRegs.empty()) {
571     unsigned BufNum = addInlineAsmDiagBuffer(OS.str(), LocMD);
572     auto &SrcMgr = DiagInfo->SrcMgr;
573     SMLoc Loc = SMLoc::getFromPointer(
574         SrcMgr.getMemoryBuffer(BufNum)->getBuffer().begin());
575 
576     std::string Msg = "inline asm clobber list contains reserved registers: ";
577     for (auto I = RestrRegs.begin(), E = RestrRegs.end(); I != E; ++I) {
578       if(I != RestrRegs.begin())
579         Msg += ", ";
580       Msg += TRI->getName(*I);
581     }
582     const char *Note =
583         "Reserved registers on the clobber list may not be "
584         "preserved across the asm statement, and clobbering them may "
585         "lead to undefined behaviour.";
586     SrcMgr.PrintMessage(Loc, SourceMgr::DK_Warning, Msg);
587     SrcMgr.PrintMessage(Loc, SourceMgr::DK_Note, Note);
588   }
589 
590   emitInlineAsm(OS.str(), getSubtargetInfo(), TM.Options.MCOptions, LocMD,
591                 MI->getInlineAsmDialect());
592 
593   // Emit the #NOAPP end marker.  This has to happen even if verbose-asm isn't
594   // enabled, so we use emitRawComment.
595   OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
596 }
597 
598 /// PrintSpecial - Print information related to the specified machine instr
599 /// that is independent of the operand, and may be independent of the instr
600 /// itself.  This can be useful for portably encoding the comment character
601 /// or other bits of target-specific knowledge into the asmstrings.  The
602 /// syntax used is ${:comment}.  Targets can override this to add support
603 /// for their own strange codes.
604 void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
605                               const char *Code) const {
606   if (!strcmp(Code, "private")) {
607     const DataLayout &DL = MF->getDataLayout();
608     OS << DL.getPrivateGlobalPrefix();
609   } else if (!strcmp(Code, "comment")) {
610     OS << MAI->getCommentString();
611   } else if (!strcmp(Code, "uid")) {
612     // Comparing the address of MI isn't sufficient, because machineinstrs may
613     // be allocated to the same address across functions.
614 
615     // If this is a new LastFn instruction, bump the counter.
616     if (LastMI != MI || LastFn != getFunctionNumber()) {
617       ++Counter;
618       LastMI = MI;
619       LastFn = getFunctionNumber();
620     }
621     OS << Counter;
622   } else {
623     std::string msg;
624     raw_string_ostream Msg(msg);
625     Msg << "Unknown special formatter '" << Code
626          << "' for machine instr: " << *MI;
627     report_fatal_error(Msg.str());
628   }
629 }
630 
631 void AsmPrinter::PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS) {
632   assert(MO.isGlobal() && "caller should check MO.isGlobal");
633   getSymbol(MO.getGlobal())->print(OS, MAI);
634   printOffset(MO.getOffset(), OS);
635 }
636 
637 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
638 /// instruction, using the specified assembler variant.  Targets should
639 /// override this to format as appropriate for machine specific ExtraCodes
640 /// or when the arch-independent handling would be too complex otherwise.
641 bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
642                                  const char *ExtraCode, raw_ostream &O) {
643   // Does this asm operand have a single letter operand modifier?
644   if (ExtraCode && ExtraCode[0]) {
645     if (ExtraCode[1] != 0) return true; // Unknown modifier.
646 
647     // https://gcc.gnu.org/onlinedocs/gccint/Output-Template.html
648     const MachineOperand &MO = MI->getOperand(OpNo);
649     switch (ExtraCode[0]) {
650     default:
651       return true;  // Unknown modifier.
652     case 'a': // Print as memory address.
653       if (MO.isReg()) {
654         PrintAsmMemoryOperand(MI, OpNo, nullptr, O);
655         return false;
656       }
657       LLVM_FALLTHROUGH; // GCC allows '%a' to behave like '%c' with immediates.
658     case 'c': // Substitute immediate value without immediate syntax
659       if (MO.isImm()) {
660         O << MO.getImm();
661         return false;
662       }
663       if (MO.isGlobal()) {
664         PrintSymbolOperand(MO, O);
665         return false;
666       }
667       return true;
668     case 'n':  // Negate the immediate constant.
669       if (!MO.isImm())
670         return true;
671       O << -MO.getImm();
672       return false;
673     case 's':  // The GCC deprecated s modifier
674       if (!MO.isImm())
675         return true;
676       O << ((32 - MO.getImm()) & 31);
677       return false;
678     }
679   }
680   return true;
681 }
682 
683 bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
684                                        const char *ExtraCode, raw_ostream &O) {
685   // Target doesn't support this yet!
686   return true;
687 }
688 
689 void AsmPrinter::emitInlineAsmStart() const {}
690 
691 void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
692                                   const MCSubtargetInfo *EndInfo) const {}
693