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/DiagnosticInfo.h"
25 #include "llvm/IR/InlineAsm.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/MC/MCAsmInfo.h"
29 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
30 #include "llvm/MC/MCStreamer.h"
31 #include "llvm/MC/MCSubtargetInfo.h"
32 #include "llvm/MC/MCSymbol.h"
33 #include "llvm/MC/TargetRegistry.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/MemoryBuffer.h"
36 #include "llvm/Support/SourceMgr.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/Target/TargetMachine.h"
39 using namespace llvm;
40 
41 #define DEBUG_TYPE "asm-printer"
42 
43 unsigned AsmPrinter::addInlineAsmDiagBuffer(StringRef AsmStr,
44                                             const MDNode *LocMDNode) const {
45   MCContext &Context = MMI->getContext();
46   Context.initInlineSourceManager();
47   SourceMgr &SrcMgr = *Context.getInlineSourceManager();
48   std::vector<const MDNode *> &LocInfos = Context.getLocInfos();
49 
50   std::unique_ptr<MemoryBuffer> Buffer;
51   // The inline asm source manager will outlive AsmStr, so make a copy of the
52   // string for SourceMgr to own.
53   Buffer = MemoryBuffer::getMemBufferCopy(AsmStr, "<inline asm>");
54 
55   // Tell SrcMgr about this buffer, it takes ownership of the buffer.
56   unsigned BufNum = SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
57 
58   // Store LocMDNode in DiagInfo, using BufNum as an identifier.
59   if (LocMDNode) {
60     LocInfos.resize(BufNum);
61     LocInfos[BufNum - 1] = LocMDNode;
62   }
63 
64   return BufNum;
65 }
66 
67 
68 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
69 void AsmPrinter::emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
70                                const MCTargetOptions &MCOptions,
71                                const MDNode *LocMDNode,
72                                InlineAsm::AsmDialect Dialect) const {
73   assert(!Str.empty() && "Can't emit empty inline asm block");
74 
75   // Remember if the buffer is nul terminated or not so we can avoid a copy.
76   bool isNullTerminated = Str.back() == 0;
77   if (isNullTerminated)
78     Str = Str.substr(0, Str.size()-1);
79 
80   // If the output streamer does not have mature MC support or the integrated
81   // assembler has been disabled or not required, just emit the blob textually.
82   // Otherwise parse the asm and emit it via MC support.
83   // This is useful in case the asm parser doesn't handle something but the
84   // system assembler does.
85   const MCAsmInfo *MCAI = TM.getMCAsmInfo();
86   assert(MCAI && "No MCAsmInfo");
87   if (!MCAI->useIntegratedAssembler() &&
88       !MCAI->parseInlineAsmUsingAsmParser() &&
89       !OutStreamer->isIntegratedAssemblerRequired()) {
90     emitInlineAsmStart();
91     OutStreamer->emitRawText(Str);
92     emitInlineAsmEnd(STI, nullptr);
93     return;
94   }
95 
96   unsigned BufNum = addInlineAsmDiagBuffer(Str, LocMDNode);
97   SourceMgr &SrcMgr = *MMI->getContext().getInlineSourceManager();
98   SrcMgr.setIncludeDirs(MCOptions.IASSearchPaths);
99 
100   std::unique_ptr<MCAsmParser> Parser(
101       createMCAsmParser(SrcMgr, OutContext, *OutStreamer, *MAI, BufNum));
102 
103   // Do not use assembler-level information for parsing inline assembly.
104   OutStreamer->setUseAssemblerInfoForParsing(false);
105 
106   // We create a new MCInstrInfo here since we might be at the module level
107   // and not have a MachineFunction to initialize the TargetInstrInfo from and
108   // we only need MCInstrInfo for asm parsing. We create one unconditionally
109   // because it's not subtarget dependent.
110   std::unique_ptr<MCInstrInfo> MII(TM.getTarget().createMCInstrInfo());
111   assert(MII && "Failed to create instruction info");
112   std::unique_ptr<MCTargetAsmParser> TAP(TM.getTarget().createMCAsmParser(
113       STI, *Parser, *MII, MCOptions));
114   if (!TAP)
115     report_fatal_error("Inline asm not supported by this streamer because"
116                        " we don't have an asm parser for this target\n");
117   Parser->setAssemblerDialect(Dialect);
118   Parser->setTargetParser(*TAP.get());
119   // Enable lexing Masm binary and hex integer literals in intel inline
120   // assembly.
121   if (Dialect == InlineAsm::AD_Intel)
122     Parser->getLexer().setLexMasmIntegers(true);
123 
124   emitInlineAsmStart();
125   // Don't implicitly switch to the text section before the asm.
126   (void)Parser->Run(/*NoInitialTextSection*/ true,
127                     /*NoFinalize*/ true);
128   emitInlineAsmEnd(STI, &TAP->getSTI());
129 }
130 
131 static void EmitInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
132                              MachineModuleInfo *MMI, const MCAsmInfo *MAI,
133                              AsmPrinter *AP, uint64_t LocCookie,
134                              raw_ostream &OS) {
135   bool InputIsIntelDialect = MI->getInlineAsmDialect() == InlineAsm::AD_Intel;
136 
137   if (InputIsIntelDialect) {
138     // Switch to the inline assembly variant.
139     OS << "\t.intel_syntax\n\t";
140   }
141 
142   int CurVariant = -1; // The number of the {.|.|.} region we are in.
143   const char *LastEmitted = AsmStr; // One past the last character emitted.
144   unsigned NumOperands = MI->getNumOperands();
145 
146   int AsmPrinterVariant;
147   if (InputIsIntelDialect)
148     AsmPrinterVariant = 1; // X86MCAsmInfo.cpp's AsmWriterFlavorTy::Intel.
149   else
150     AsmPrinterVariant = MMI->getTarget().unqualifiedInlineAsmVariant();
151 
152   // FIXME: Should this happen for `asm inteldialect` as well?
153   if (!InputIsIntelDialect && MAI->getEmitGNUAsmStartIndentationMarker())
154     OS << '\t';
155 
156   while (*LastEmitted) {
157     switch (*LastEmitted) {
158     default: {
159       // Not a special case, emit the string section literally.
160       const char *LiteralEnd = LastEmitted+1;
161       while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
162              *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
163         ++LiteralEnd;
164       if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
165         OS.write(LastEmitted, LiteralEnd - LastEmitted);
166       LastEmitted = LiteralEnd;
167       break;
168     }
169     case '\n':
170       ++LastEmitted;   // Consume newline character.
171       OS << '\n';      // Indent code with newline.
172       break;
173     case '$': {
174       ++LastEmitted;   // Consume '$' character.
175       bool Done = true;
176 
177       // Handle escapes.
178       switch (*LastEmitted) {
179       default: Done = false; break;
180       case '$':     // $$ -> $
181         if (!InputIsIntelDialect)
182           if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
183             OS << '$';
184         ++LastEmitted;  // Consume second '$' character.
185         break;
186       case '(':        // $( -> same as GCC's { character.
187         ++LastEmitted; // Consume '(' character.
188         if (CurVariant != -1)
189           report_fatal_error("Nested variants found in inline asm string: '" +
190                              Twine(AsmStr) + "'");
191         CurVariant = 0; // We're in the first variant now.
192         break;
193       case '|':
194         ++LastEmitted; // Consume '|' character.
195         if (CurVariant == -1)
196           OS << '|'; // This is gcc's behavior for | outside a variant.
197         else
198           ++CurVariant; // We're in the next variant.
199         break;
200       case ')':        // $) -> same as GCC's } char.
201         ++LastEmitted; // Consume ')' character.
202         if (CurVariant == -1)
203           OS << '}'; // This is gcc's behavior for } outside a variant.
204         else
205           CurVariant = -1;
206         break;
207       }
208       if (Done) break;
209 
210       bool HasCurlyBraces = false;
211       if (*LastEmitted == '{') {     // ${variable}
212         ++LastEmitted;               // Consume '{' character.
213         HasCurlyBraces = true;
214       }
215 
216       // If we have ${:foo}, then this is not a real operand reference, it is a
217       // "magic" string reference, just like in .td files.  Arrange to call
218       // PrintSpecial.
219       if (HasCurlyBraces && *LastEmitted == ':') {
220         ++LastEmitted;
221         const char *StrStart = LastEmitted;
222         const char *StrEnd = strchr(StrStart, '}');
223         if (!StrEnd)
224           report_fatal_error("Unterminated ${:foo} operand in inline asm"
225                              " string: '" + Twine(AsmStr) + "'");
226         if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
227           AP->PrintSpecial(MI, OS, StringRef(StrStart, StrEnd - StrStart));
228         LastEmitted = StrEnd+1;
229         break;
230       }
231 
232       const char *IDStart = LastEmitted;
233       const char *IDEnd = IDStart;
234       while (isDigit(*IDEnd))
235         ++IDEnd;
236 
237       unsigned Val;
238       if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
239         report_fatal_error("Bad $ operand number in inline asm string: '" +
240                            Twine(AsmStr) + "'");
241       LastEmitted = IDEnd;
242 
243       if (Val >= NumOperands - 1)
244         report_fatal_error("Invalid $ operand number in inline asm string: '" +
245                            Twine(AsmStr) + "'");
246 
247       char Modifier[2] = { 0, 0 };
248 
249       if (HasCurlyBraces) {
250         // If we have curly braces, check for a modifier character.  This
251         // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
252         if (*LastEmitted == ':') {
253           ++LastEmitted;    // Consume ':' character.
254           if (*LastEmitted == 0)
255             report_fatal_error("Bad ${:} expression in inline asm string: '" +
256                                Twine(AsmStr) + "'");
257 
258           Modifier[0] = *LastEmitted;
259           ++LastEmitted;    // Consume modifier character.
260         }
261 
262         if (*LastEmitted != '}')
263           report_fatal_error("Bad ${} expression in inline asm string: '" +
264                              Twine(AsmStr) + "'");
265         ++LastEmitted;    // Consume '}' character.
266       }
267 
268       // Okay, we finally have a value number.  Ask the target to print this
269       // operand!
270       if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
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())
278             break;
279           unsigned OpFlags = MI->getOperand(OpNo).getImm();
280           OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
281         }
282 
283         // We may have a location metadata attached to the end of the
284         // instruction, and at no point should see metadata at any
285         // other point while processing. It's an error if so.
286         if (OpNo >= MI->getNumOperands() || 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           // FIXME: Shouldn't arch-independent output template handling go into
293           // PrintAsmOperand?
294           // Labels are target independent.
295           if (MI->getOperand(OpNo).isBlockAddress()) {
296             const BlockAddress *BA = MI->getOperand(OpNo).getBlockAddress();
297             MCSymbol *Sym = AP->GetBlockAddressSymbol(BA);
298             Sym->print(OS, AP->MAI);
299             MMI->getContext().registerInlineAsmLabel(Sym);
300           } else if (MI->getOperand(OpNo).isMBB()) {
301             const MCSymbol *Sym = MI->getOperand(OpNo).getMBB()->getSymbol();
302             Sym->print(OS, AP->MAI);
303           } else if (InlineAsm::isMemKind(OpFlags)) {
304             Error = AP->PrintAsmMemoryOperand(
305                 MI, OpNo, Modifier[0] ? Modifier : nullptr, OS);
306           } else {
307             Error = AP->PrintAsmOperand(MI, OpNo,
308                                         Modifier[0] ? Modifier : nullptr, OS);
309           }
310         }
311         if (Error) {
312           std::string msg;
313           raw_string_ostream Msg(msg);
314           Msg << "invalid operand in inline asm: '" << AsmStr << "'";
315           MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
316         }
317       }
318       break;
319     }
320     }
321   }
322   if (InputIsIntelDialect)
323     OS << "\n\t.att_syntax";
324   OS << '\n' << (char)0;  // null terminate string.
325 }
326 
327 /// This method formats and emits the specified machine instruction that is an
328 /// inline asm.
329 void AsmPrinter::emitInlineAsm(const MachineInstr *MI) const {
330   assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
331 
332   // Count the number of register definitions to find the asm string.
333   unsigned NumDefs = 0;
334   for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
335        ++NumDefs)
336     assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
337 
338   assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
339 
340   // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
341   const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
342 
343   // If this asmstr is empty, just print the #APP/#NOAPP markers.
344   // These are useful to see where empty asm's wound up.
345   if (AsmStr[0] == 0) {
346     OutStreamer->emitRawComment(MAI->getInlineAsmStart());
347     OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
348     return;
349   }
350 
351   // Emit the #APP start marker.  This has to happen even if verbose-asm isn't
352   // enabled, so we use emitRawComment.
353   OutStreamer->emitRawComment(MAI->getInlineAsmStart());
354 
355   // Get the !srcloc metadata node if we have it, and decode the loc cookie from
356   // it.
357   uint64_t LocCookie = 0;
358   const MDNode *LocMD = nullptr;
359   for (unsigned i = MI->getNumOperands(); i != 0; --i) {
360     if (MI->getOperand(i-1).isMetadata() &&
361         (LocMD = MI->getOperand(i-1).getMetadata()) &&
362         LocMD->getNumOperands() != 0) {
363       if (const ConstantInt *CI =
364               mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
365         LocCookie = CI->getZExtValue();
366         break;
367       }
368     }
369   }
370 
371   // Emit the inline asm to a temporary string so we can emit it through
372   // EmitInlineAsm.
373   SmallString<256> StringData;
374   raw_svector_ostream OS(StringData);
375 
376   AsmPrinter *AP = const_cast<AsmPrinter*>(this);
377   EmitInlineAsmStr(AsmStr, MI, MMI, MAI, AP, LocCookie, OS);
378 
379   // Emit warnings if we use reserved registers on the clobber list, as
380   // that might lead to undefined behaviour.
381   SmallVector<Register, 8> RestrRegs;
382   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
383   // Start with the first operand descriptor, and iterate over them.
384   for (unsigned I = InlineAsm::MIOp_FirstOperand, NumOps = MI->getNumOperands();
385        I < NumOps; ++I) {
386     const MachineOperand &MO = MI->getOperand(I);
387     if (!MO.isImm())
388       continue;
389     unsigned Flags = MO.getImm();
390     if (InlineAsm::getKind(Flags) == InlineAsm::Kind_Clobber) {
391       Register Reg = MI->getOperand(I + 1).getReg();
392       if (!TRI->isAsmClobberable(*MF, Reg))
393         RestrRegs.push_back(Reg);
394     }
395     // Skip to one before the next operand descriptor, if it exists.
396     I += InlineAsm::getNumOperandRegisters(Flags);
397   }
398 
399   if (!RestrRegs.empty()) {
400     std::string Msg = "inline asm clobber list contains reserved registers: ";
401     ListSeparator LS;
402     for (const Register &RR : RestrRegs) {
403       Msg += LS;
404       Msg += TRI->getName(RR);
405     }
406     const char *Note =
407         "Reserved registers on the clobber list may not be "
408         "preserved across the asm statement, and clobbering them may "
409         "lead to undefined behaviour.";
410     MMI->getModule()->getContext().diagnose(DiagnosticInfoInlineAsm(
411         LocCookie, Msg, DiagnosticSeverity::DS_Warning));
412     MMI->getModule()->getContext().diagnose(
413         DiagnosticInfoInlineAsm(LocCookie, Note, DiagnosticSeverity::DS_Note));
414   }
415 
416   emitInlineAsm(OS.str(), getSubtargetInfo(), TM.Options.MCOptions, LocMD,
417                 MI->getInlineAsmDialect());
418 
419   // Emit the #NOAPP end marker.  This has to happen even if verbose-asm isn't
420   // enabled, so we use emitRawComment.
421   OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
422 }
423 
424 /// PrintSpecial - Print information related to the specified machine instr
425 /// that is independent of the operand, and may be independent of the instr
426 /// itself.  This can be useful for portably encoding the comment character
427 /// or other bits of target-specific knowledge into the asmstrings.  The
428 /// syntax used is ${:comment}.  Targets can override this to add support
429 /// for their own strange codes.
430 void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
431                               StringRef Code) const {
432   if (Code == "private") {
433     const DataLayout &DL = MF->getDataLayout();
434     OS << DL.getPrivateGlobalPrefix();
435   } else if (Code == "comment") {
436     OS << MAI->getCommentString();
437   } else if (Code == "uid") {
438     // Comparing the address of MI isn't sufficient, because machineinstrs may
439     // be allocated to the same address across functions.
440 
441     // If this is a new LastFn instruction, bump the counter.
442     if (LastMI != MI || LastFn != getFunctionNumber()) {
443       ++Counter;
444       LastMI = MI;
445       LastFn = getFunctionNumber();
446     }
447     OS << Counter;
448   } else {
449     std::string msg;
450     raw_string_ostream Msg(msg);
451     Msg << "Unknown special formatter '" << Code
452          << "' for machine instr: " << *MI;
453     report_fatal_error(Twine(Msg.str()));
454   }
455 }
456 
457 void AsmPrinter::PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS) {
458   assert(MO.isGlobal() && "caller should check MO.isGlobal");
459   getSymbolPreferLocal(*MO.getGlobal())->print(OS, MAI);
460   printOffset(MO.getOffset(), OS);
461 }
462 
463 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
464 /// instruction, using the specified assembler variant.  Targets should
465 /// override this to format as appropriate for machine specific ExtraCodes
466 /// or when the arch-independent handling would be too complex otherwise.
467 bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
468                                  const char *ExtraCode, raw_ostream &O) {
469   // Does this asm operand have a single letter operand modifier?
470   if (ExtraCode && ExtraCode[0]) {
471     if (ExtraCode[1] != 0) return true; // Unknown modifier.
472 
473     // https://gcc.gnu.org/onlinedocs/gccint/Output-Template.html
474     const MachineOperand &MO = MI->getOperand(OpNo);
475     switch (ExtraCode[0]) {
476     default:
477       return true;  // Unknown modifier.
478     case 'a': // Print as memory address.
479       if (MO.isReg()) {
480         PrintAsmMemoryOperand(MI, OpNo, nullptr, O);
481         return false;
482       }
483       LLVM_FALLTHROUGH; // GCC allows '%a' to behave like '%c' with immediates.
484     case 'c': // Substitute immediate value without immediate syntax
485       if (MO.isImm()) {
486         O << MO.getImm();
487         return false;
488       }
489       if (MO.isGlobal()) {
490         PrintSymbolOperand(MO, O);
491         return false;
492       }
493       return true;
494     case 'n':  // Negate the immediate constant.
495       if (!MO.isImm())
496         return true;
497       O << -MO.getImm();
498       return false;
499     case 's':  // The GCC deprecated s modifier
500       if (!MO.isImm())
501         return true;
502       O << ((32 - MO.getImm()) & 31);
503       return false;
504     }
505   }
506   return true;
507 }
508 
509 bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
510                                        const char *ExtraCode, raw_ostream &O) {
511   // Target doesn't support this yet!
512   return true;
513 }
514 
515 void AsmPrinter::emitInlineAsmStart() const {}
516 
517 void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
518                                   const MCSubtargetInfo *EndInfo) const {}
519