1 //===-- AsmPrinterInlineAsm.cpp - AsmPrinter Inline Asm Handling ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the inline assembler pieces of the AsmPrinter class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/AsmPrinter.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineModuleInfo.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/InlineAsm.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/MC/MCSubtargetInfo.h"
28 #include "llvm/MC/MCSymbol.h"
29 #include "llvm/MC/MCTargetAsmParser.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/SourceMgr.h"
33 #include "llvm/Support/TargetRegistry.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Target/TargetMachine.h"
36 #include "llvm/Target/TargetSubtargetInfo.h"
37 using namespace llvm;
38 
39 #define DEBUG_TYPE "asm-printer"
40 
41 namespace {
42   struct SrcMgrDiagInfo {
43     const MDNode *LocInfo;
44     LLVMContext::InlineAsmDiagHandlerTy DiagHandler;
45     void *DiagContext;
46   };
47 }
48 
49 /// srcMgrDiagHandler - This callback is invoked when the SourceMgr for an
50 /// inline asm has an error in it.  diagInfo is a pointer to the SrcMgrDiagInfo
51 /// struct above.
52 static void srcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
53   SrcMgrDiagInfo *DiagInfo = static_cast<SrcMgrDiagInfo *>(diagInfo);
54   assert(DiagInfo && "Diagnostic context not passed down?");
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 (const MDNode *LocInfo = DiagInfo->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           dyn_cast<ConstantInt>(LocInfo->getOperand(ErrorLine)))
67         LocCookie = CI->getZExtValue();
68   }
69 
70   DiagInfo->DiagHandler(Diag, DiagInfo->DiagContext, LocCookie);
71 }
72 
73 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
74 void AsmPrinter::EmitInlineAsm(StringRef Str, const MDNode *LocMDNode,
75                                InlineAsm::AsmDialect Dialect) const {
76   assert(!Str.empty() && "Can't emit empty inline asm block");
77 
78   // Remember if the buffer is nul terminated or not so we can avoid a copy.
79   bool isNullTerminated = Str.back() == 0;
80   if (isNullTerminated)
81     Str = Str.substr(0, Str.size()-1);
82 
83   // If the output streamer does not have mature MC support or the integrated
84   // assembler has been disabled, just emit the blob textually.
85   // Otherwise parse the asm and emit it via MC support.
86   // This is useful in case the asm parser doesn't handle something but the
87   // system assembler does.
88   const MCAsmInfo *MCAI = TM.getMCAsmInfo();
89   assert(MCAI && "No MCAsmInfo");
90   if (!MCAI->useIntegratedAssembler() &&
91       !OutStreamer.isIntegratedAssemblerRequired()) {
92     OutStreamer.EmitRawText(Str);
93     emitInlineAsmEnd(TM.getSubtarget<MCSubtargetInfo>(), nullptr);
94     return;
95   }
96 
97   SourceMgr SrcMgr;
98   SrcMgrDiagInfo DiagInfo;
99 
100   // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
101   LLVMContext &LLVMCtx = MMI->getModule()->getContext();
102   bool HasDiagHandler = false;
103   if (LLVMCtx.getInlineAsmDiagnosticHandler() != nullptr) {
104     // If the source manager has an issue, we arrange for srcMgrDiagHandler
105     // to be invoked, getting DiagInfo passed into it.
106     DiagInfo.LocInfo = LocMDNode;
107     DiagInfo.DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
108     DiagInfo.DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
109     SrcMgr.setDiagHandler(srcMgrDiagHandler, &DiagInfo);
110     HasDiagHandler = true;
111   }
112 
113   MemoryBuffer *Buffer;
114   if (isNullTerminated)
115     Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
116   else
117     Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
118 
119   // Tell SrcMgr about this buffer, it takes ownership of the buffer.
120   SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
121 
122   std::unique_ptr<MCAsmParser> Parser(
123       createMCAsmParser(SrcMgr, OutContext, OutStreamer, *MAI));
124 
125   // Initialize the parser with a fresh subtarget info. It is better to use a
126   // new STI here because the parser may modify it and we do not want those
127   // modifications to persist after parsing the inlineasm. The modifications
128   // made by the parser will be seen by the code emitters because it passes
129   // the current STI down to the EncodeInstruction() method.
130   std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
131       TM.getTargetTriple(), TM.getTargetCPU(), TM.getTargetFeatureString()));
132 
133   // Preserve a copy of the original STI because the parser may modify it.  For
134   // example, when switching between arm and thumb mode. If the target needs to
135   // emit code to return to the original state it can do so in
136   // emitInlineAsmEnd().
137   MCSubtargetInfo STIOrig = *STI;
138 
139   MCTargetOptions MCOptions;
140   if (MF)
141     MCOptions = MF->getTarget().Options.MCOptions;
142   std::unique_ptr<MCTargetAsmParser> TAP(
143       TM.getTarget().createMCAsmParser(*STI, *Parser, *MII, MCOptions));
144   if (!TAP)
145     report_fatal_error("Inline asm not supported by this streamer because"
146                        " we don't have an asm parser for this target\n");
147   Parser->setAssemblerDialect(Dialect);
148   Parser->setTargetParser(*TAP.get());
149 
150   // Don't implicitly switch to the text section before the asm.
151   int Res = Parser->Run(/*NoInitialTextSection*/ true,
152                         /*NoFinalize*/ true);
153   emitInlineAsmEnd(STIOrig, STI.get());
154   if (Res && !HasDiagHandler)
155     report_fatal_error("Error parsing inline asm\n");
156 }
157 
158 static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
159                                MachineModuleInfo *MMI, int InlineAsmVariant,
160                                AsmPrinter *AP, unsigned LocCookie,
161                                raw_ostream &OS) {
162   // Switch to the inline assembly variant.
163   OS << "\t.intel_syntax\n\t";
164 
165   const char *LastEmitted = AsmStr; // One past the last character emitted.
166   unsigned NumOperands = MI->getNumOperands();
167 
168   while (*LastEmitted) {
169     switch (*LastEmitted) {
170     default: {
171       // Not a special case, emit the string section literally.
172       const char *LiteralEnd = LastEmitted+1;
173       while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
174              *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
175         ++LiteralEnd;
176 
177       OS.write(LastEmitted, LiteralEnd-LastEmitted);
178       LastEmitted = LiteralEnd;
179       break;
180     }
181     case '\n':
182       ++LastEmitted;   // Consume newline character.
183       OS << '\n';      // Indent code with newline.
184       break;
185     case '$': {
186       ++LastEmitted;   // Consume '$' character.
187       bool Done = true;
188 
189       // Handle escapes.
190       switch (*LastEmitted) {
191       default: Done = false; break;
192       case '$':
193         ++LastEmitted;  // Consume second '$' character.
194         break;
195       }
196       if (Done) break;
197 
198       const char *IDStart = LastEmitted;
199       const char *IDEnd = IDStart;
200       while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
201 
202       unsigned Val;
203       if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
204         report_fatal_error("Bad $ operand number in inline asm string: '" +
205                            Twine(AsmStr) + "'");
206       LastEmitted = IDEnd;
207 
208       if (Val >= NumOperands-1)
209         report_fatal_error("Invalid $ operand number in inline asm string: '" +
210                            Twine(AsmStr) + "'");
211 
212       // Okay, we finally have a value number.  Ask the target to print this
213       // operand!
214       unsigned OpNo = InlineAsm::MIOp_FirstOperand;
215 
216       bool Error = false;
217 
218       // Scan to find the machine operand number for the operand.
219       for (; Val; --Val) {
220         if (OpNo >= MI->getNumOperands()) break;
221         unsigned OpFlags = MI->getOperand(OpNo).getImm();
222         OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
223       }
224 
225       // We may have a location metadata attached to the end of the
226       // instruction, and at no point should see metadata at any
227       // other point while processing. It's an error if so.
228       if (OpNo >= MI->getNumOperands() ||
229           MI->getOperand(OpNo).isMetadata()) {
230         Error = true;
231       } else {
232         unsigned OpFlags = MI->getOperand(OpNo).getImm();
233         ++OpNo;  // Skip over the ID number.
234 
235         if (InlineAsm::isMemKind(OpFlags)) {
236           Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
237                                             /*Modifier*/ nullptr, OS);
238         } else {
239           Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
240                                       /*Modifier*/ nullptr, OS);
241         }
242       }
243       if (Error) {
244         string_ostream Msg;
245         Msg << "invalid operand in inline asm: '" << AsmStr << "'";
246         MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
247       }
248       break;
249     }
250     }
251   }
252   OS << "\n\t.att_syntax\n" << (char)0;  // null terminate string.
253 }
254 
255 static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
256                                 MachineModuleInfo *MMI, int InlineAsmVariant,
257                                 int AsmPrinterVariant, AsmPrinter *AP,
258                                 unsigned LocCookie, raw_ostream &OS) {
259   int CurVariant = -1;            // The number of the {.|.|.} region we are in.
260   const char *LastEmitted = AsmStr; // One past the last character emitted.
261   unsigned NumOperands = MI->getNumOperands();
262 
263   OS << '\t';
264 
265   while (*LastEmitted) {
266     switch (*LastEmitted) {
267     default: {
268       // Not a special case, emit the string section literally.
269       const char *LiteralEnd = LastEmitted+1;
270       while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
271              *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
272         ++LiteralEnd;
273       if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
274         OS.write(LastEmitted, LiteralEnd-LastEmitted);
275       LastEmitted = LiteralEnd;
276       break;
277     }
278     case '\n':
279       ++LastEmitted;   // Consume newline character.
280       OS << '\n';      // Indent code with newline.
281       break;
282     case '$': {
283       ++LastEmitted;   // Consume '$' character.
284       bool Done = true;
285 
286       // Handle escapes.
287       switch (*LastEmitted) {
288       default: Done = false; break;
289       case '$':     // $$ -> $
290         if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
291           OS << '$';
292         ++LastEmitted;  // Consume second '$' character.
293         break;
294       case '(':             // $( -> same as GCC's { character.
295         ++LastEmitted;      // Consume '(' character.
296         if (CurVariant != -1)
297           report_fatal_error("Nested variants found in inline asm string: '" +
298                              Twine(AsmStr) + "'");
299         CurVariant = 0;     // We're in the first variant now.
300         break;
301       case '|':
302         ++LastEmitted;  // consume '|' character.
303         if (CurVariant == -1)
304           OS << '|';       // this is gcc's behavior for | outside a variant
305         else
306           ++CurVariant;   // We're in the next variant.
307         break;
308       case ')':         // $) -> same as GCC's } char.
309         ++LastEmitted;  // consume ')' character.
310         if (CurVariant == -1)
311           OS << '}';     // this is gcc's behavior for } outside a variant
312         else
313           CurVariant = -1;
314         break;
315       }
316       if (Done) break;
317 
318       bool HasCurlyBraces = false;
319       if (*LastEmitted == '{') {     // ${variable}
320         ++LastEmitted;               // Consume '{' character.
321         HasCurlyBraces = true;
322       }
323 
324       // If we have ${:foo}, then this is not a real operand reference, it is a
325       // "magic" string reference, just like in .td files.  Arrange to call
326       // PrintSpecial.
327       if (HasCurlyBraces && *LastEmitted == ':') {
328         ++LastEmitted;
329         const char *StrStart = LastEmitted;
330         const char *StrEnd = strchr(StrStart, '}');
331         if (!StrEnd)
332           report_fatal_error("Unterminated ${:foo} operand in inline asm"
333                              " string: '" + Twine(AsmStr) + "'");
334 
335         std::string Val(StrStart, StrEnd);
336         AP->PrintSpecial(MI, OS, Val.c_str());
337         LastEmitted = StrEnd+1;
338         break;
339       }
340 
341       const char *IDStart = LastEmitted;
342       const char *IDEnd = IDStart;
343       while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
344 
345       unsigned Val;
346       if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
347         report_fatal_error("Bad $ operand number in inline asm string: '" +
348                            Twine(AsmStr) + "'");
349       LastEmitted = IDEnd;
350 
351       char Modifier[2] = { 0, 0 };
352 
353       if (HasCurlyBraces) {
354         // If we have curly braces, check for a modifier character.  This
355         // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
356         if (*LastEmitted == ':') {
357           ++LastEmitted;    // Consume ':' character.
358           if (*LastEmitted == 0)
359             report_fatal_error("Bad ${:} expression in inline asm string: '" +
360                                Twine(AsmStr) + "'");
361 
362           Modifier[0] = *LastEmitted;
363           ++LastEmitted;    // Consume modifier character.
364         }
365 
366         if (*LastEmitted != '}')
367           report_fatal_error("Bad ${} expression in inline asm string: '" +
368                              Twine(AsmStr) + "'");
369         ++LastEmitted;    // Consume '}' character.
370       }
371 
372       if (Val >= NumOperands-1)
373         report_fatal_error("Invalid $ operand number in inline asm string: '" +
374                            Twine(AsmStr) + "'");
375 
376       // Okay, we finally have a value number.  Ask the target to print this
377       // operand!
378       if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
379         unsigned OpNo = InlineAsm::MIOp_FirstOperand;
380 
381         bool Error = false;
382 
383         // Scan to find the machine operand number for the operand.
384         for (; Val; --Val) {
385           if (OpNo >= MI->getNumOperands()) break;
386           unsigned OpFlags = MI->getOperand(OpNo).getImm();
387           OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
388         }
389 
390         // We may have a location metadata attached to the end of the
391         // instruction, and at no point should see metadata at any
392         // other point while processing. It's an error if so.
393         if (OpNo >= MI->getNumOperands() ||
394             MI->getOperand(OpNo).isMetadata()) {
395           Error = true;
396         } else {
397           unsigned OpFlags = MI->getOperand(OpNo).getImm();
398           ++OpNo;  // Skip over the ID number.
399 
400           if (Modifier[0] == 'l')  // labels are target independent
401             // FIXME: What if the operand isn't an MBB, report error?
402             OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
403           else {
404             if (InlineAsm::isMemKind(OpFlags)) {
405               Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
406                                                 Modifier[0] ? Modifier : nullptr,
407                                                 OS);
408             } else {
409               Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
410                                           Modifier[0] ? Modifier : nullptr, OS);
411             }
412           }
413         }
414         if (Error) {
415           string_ostream Msg;
416           Msg << "invalid operand in inline asm: '" << AsmStr << "'";
417           MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
418         }
419       }
420       break;
421     }
422     }
423   }
424   OS << '\n' << (char)0;  // null terminate string.
425 }
426 
427 /// EmitInlineAsm - This method formats and emits the specified machine
428 /// instruction that is an inline asm.
429 void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
430   assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
431 
432   // Count the number of register definitions to find the asm string.
433   unsigned NumDefs = 0;
434   for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
435        ++NumDefs)
436     assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
437 
438   assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
439 
440   // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
441   const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
442 
443   // If this asmstr is empty, just print the #APP/#NOAPP markers.
444   // These are useful to see where empty asm's wound up.
445   if (AsmStr[0] == 0) {
446     OutStreamer.emitRawComment(MAI->getInlineAsmStart());
447     OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
448     return;
449   }
450 
451   // Emit the #APP start marker.  This has to happen even if verbose-asm isn't
452   // enabled, so we use emitRawComment.
453   OutStreamer.emitRawComment(MAI->getInlineAsmStart());
454 
455   // Get the !srcloc metadata node if we have it, and decode the loc cookie from
456   // it.
457   unsigned LocCookie = 0;
458   const MDNode *LocMD = nullptr;
459   for (unsigned i = MI->getNumOperands(); i != 0; --i) {
460     if (MI->getOperand(i-1).isMetadata() &&
461         (LocMD = MI->getOperand(i-1).getMetadata()) &&
462         LocMD->getNumOperands() != 0) {
463       if (const ConstantInt *CI = dyn_cast<ConstantInt>(LocMD->getOperand(0))) {
464         LocCookie = CI->getZExtValue();
465         break;
466       }
467     }
468   }
469 
470   // Emit the inline asm to a temporary string so we can emit it through
471   // EmitInlineAsm.
472   small_string_ostream<256> OS;
473 
474   // The variant of the current asmprinter.
475   int AsmPrinterVariant = MAI->getAssemblerDialect();
476   InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
477   AsmPrinter *AP = const_cast<AsmPrinter*>(this);
478   if (InlineAsmVariant == InlineAsm::AD_ATT)
479     EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
480                         AP, LocCookie, OS);
481   else
482     EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
483 
484   EmitInlineAsm(OS.str(), LocMD, MI->getInlineAsmDialect());
485 
486   // Emit the #NOAPP end marker.  This has to happen even if verbose-asm isn't
487   // enabled, so we use emitRawComment.
488   OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
489 }
490 
491 
492 /// PrintSpecial - Print information related to the specified machine instr
493 /// that is independent of the operand, and may be independent of the instr
494 /// itself.  This can be useful for portably encoding the comment character
495 /// or other bits of target-specific knowledge into the asmstrings.  The
496 /// syntax used is ${:comment}.  Targets can override this to add support
497 /// for their own strange codes.
498 void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
499                               const char *Code) const {
500   const DataLayout *DL = TM.getDataLayout();
501   if (!strcmp(Code, "private")) {
502     OS << DL->getPrivateGlobalPrefix();
503   } else if (!strcmp(Code, "comment")) {
504     OS << MAI->getCommentString();
505   } else if (!strcmp(Code, "uid")) {
506     // Comparing the address of MI isn't sufficient, because machineinstrs may
507     // be allocated to the same address across functions.
508 
509     // If this is a new LastFn instruction, bump the counter.
510     if (LastMI != MI || LastFn != getFunctionNumber()) {
511       ++Counter;
512       LastMI = MI;
513       LastFn = getFunctionNumber();
514     }
515     OS << Counter;
516   } else {
517     string_ostream Msg;
518     Msg << "Unknown special formatter '" << Code
519          << "' for machine instr: " << *MI;
520     report_fatal_error(Msg.str());
521   }
522 }
523 
524 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
525 /// instruction, using the specified assembler variant.  Targets should
526 /// override this to format as appropriate.
527 bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
528                                  unsigned AsmVariant, const char *ExtraCode,
529                                  raw_ostream &O) {
530   // Does this asm operand have a single letter operand modifier?
531   if (ExtraCode && ExtraCode[0]) {
532     if (ExtraCode[1] != 0) return true; // Unknown modifier.
533 
534     const MachineOperand &MO = MI->getOperand(OpNo);
535     switch (ExtraCode[0]) {
536     default:
537       return true;  // Unknown modifier.
538     case 'c': // Substitute immediate value without immediate syntax
539       if (MO.getType() != MachineOperand::MO_Immediate)
540         return true;
541       O << MO.getImm();
542       return false;
543     case 'n':  // Negate the immediate constant.
544       if (MO.getType() != MachineOperand::MO_Immediate)
545         return true;
546       O << -MO.getImm();
547       return false;
548     }
549   }
550   return true;
551 }
552 
553 bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
554                                        unsigned AsmVariant,
555                                        const char *ExtraCode, raw_ostream &O) {
556   // Target doesn't support this yet!
557   return true;
558 }
559 
560 void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
561                                   const MCSubtargetInfo *EndInfo) const {}
562