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