AsmPrinterInlineAsm.cpp revision 94b9550a32d189704a8eae55505edf62662c0534
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#define DEBUG_TYPE "asm-printer"
15#include "llvm/CodeGen/AsmPrinter.h"
16#include "llvm/Constants.h"
17#include "llvm/InlineAsm.h"
18#include "llvm/LLVMContext.h"
19#include "llvm/Module.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
21#include "llvm/CodeGen/MachineModuleInfo.h"
22#include "llvm/MC/MCAsmInfo.h"
23#include "llvm/MC/MCStreamer.h"
24#include "llvm/MC/MCSubtargetInfo.h"
25#include "llvm/MC/MCSymbol.h"
26#include "llvm/MC/MCTargetAsmParser.h"
27#include "llvm/Target/TargetMachine.h"
28#include "llvm/Target/TargetRegistry.h"
29#include "llvm/ADT/OwningPtr.h"
30#include "llvm/ADT/SmallString.h"
31#include "llvm/ADT/Twine.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/MemoryBuffer.h"
34#include "llvm/Support/SourceMgr.h"
35#include "llvm/Support/raw_ostream.h"
36using namespace llvm;
37
38namespace {
39  struct SrcMgrDiagInfo {
40    const MDNode *LocInfo;
41    LLVMContext::InlineAsmDiagHandlerTy DiagHandler;
42    void *DiagContext;
43  };
44}
45
46/// SrcMgrDiagHandler - This callback is invoked when the SourceMgr for an
47/// inline asm has an error in it.  diagInfo is a pointer to the SrcMgrDiagInfo
48/// struct above.
49static void SrcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
50  SrcMgrDiagInfo *DiagInfo = static_cast<SrcMgrDiagInfo *>(diagInfo);
51  assert(DiagInfo && "Diagnostic context not passed down?");
52
53  // If the inline asm had metadata associated with it, pull out a location
54  // cookie corresponding to which line the error occurred on.
55  unsigned LocCookie = 0;
56  if (const MDNode *LocInfo = DiagInfo->LocInfo) {
57    unsigned ErrorLine = Diag.getLineNo()-1;
58    if (ErrorLine >= LocInfo->getNumOperands())
59      ErrorLine = 0;
60
61    if (LocInfo->getNumOperands() != 0)
62      if (const ConstantInt *CI =
63          dyn_cast<ConstantInt>(LocInfo->getOperand(ErrorLine)))
64        LocCookie = CI->getZExtValue();
65  }
66
67  DiagInfo->DiagHandler(Diag, DiagInfo->DiagContext, LocCookie);
68}
69
70/// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
71void AsmPrinter::EmitInlineAsm(StringRef Str, const MDNode *LocMDNode) const {
72  assert(!Str.empty() && "Can't emit empty inline asm block");
73
74  // Remember if the buffer is nul terminated or not so we can avoid a copy.
75  bool isNullTerminated = Str.back() == 0;
76  if (isNullTerminated)
77    Str = Str.substr(0, Str.size()-1);
78
79  // If the output streamer is actually a .s file, just emit the blob textually.
80  // This is useful in case the asm parser doesn't handle something but the
81  // system assembler does.
82  if (OutStreamer.hasRawTextSupport()) {
83    OutStreamer.EmitRawText(Str);
84    return;
85  }
86
87  SourceMgr SrcMgr;
88  SrcMgrDiagInfo DiagInfo;
89
90  // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
91  LLVMContext &LLVMCtx = MMI->getModule()->getContext();
92  bool HasDiagHandler = false;
93  if (LLVMCtx.getInlineAsmDiagnosticHandler() != 0) {
94    // If the source manager has an issue, we arrange for SrcMgrDiagHandler
95    // to be invoked, getting DiagInfo passed into it.
96    DiagInfo.LocInfo = LocMDNode;
97    DiagInfo.DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
98    DiagInfo.DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
99    SrcMgr.setDiagHandler(SrcMgrDiagHandler, &DiagInfo);
100    HasDiagHandler = true;
101  }
102
103  MemoryBuffer *Buffer;
104  if (isNullTerminated)
105    Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
106  else
107    Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
108
109  // Tell SrcMgr about this buffer, it takes ownership of the buffer.
110  SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
111
112  OwningPtr<MCAsmParser> Parser(createMCAsmParser(TM.getTarget(), SrcMgr,
113                                                  OutContext, OutStreamer,
114                                                  *MAI));
115
116  // FIXME: It would be nice if we can avoid createing a new instance of
117  // MCSubtargetInfo here given TargetSubtargetInfo is available. However,
118  // we have to watch out for asm directives which can change subtarget
119  // state. e.g. .code 16, .code 32.
120  OwningPtr<MCSubtargetInfo>
121    STI(TM.getTarget().createMCSubtargetInfo(TM.getTargetTriple(),
122                                             TM.getTargetCPU(),
123                                             TM.getTargetFeatureString()));
124  OwningPtr<MCTargetAsmParser>
125    TAP(TM.getTarget().createMCAsmParser(*STI, *Parser));
126  if (!TAP)
127    report_fatal_error("Inline asm not supported by this streamer because"
128                       " we don't have an asm parser for this target\n");
129  Parser->setTargetParser(*TAP.get());
130
131  // Don't implicitly switch to the text section before the asm.
132  int Res = Parser->Run(/*NoInitialTextSection*/ true,
133                        /*NoFinalize*/ true);
134  if (Res && !HasDiagHandler)
135    report_fatal_error("Error parsing inline asm\n");
136}
137
138
139/// EmitInlineAsm - This method formats and emits the specified machine
140/// instruction that is an inline asm.
141void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
142  assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
143
144  unsigned NumOperands = MI->getNumOperands();
145
146  // Count the number of register definitions to find the asm string.
147  unsigned NumDefs = 0;
148  for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
149       ++NumDefs)
150    assert(NumDefs != NumOperands-2 && "No asm string?");
151
152  assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
153
154  // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
155  const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
156
157  // If this asmstr is empty, just print the #APP/#NOAPP markers.
158  // These are useful to see where empty asm's wound up.
159  if (AsmStr[0] == 0) {
160    // Don't emit the comments if writing to a .o file.
161    if (!OutStreamer.hasRawTextSupport()) return;
162
163    OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
164                            MAI->getInlineAsmStart());
165    OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
166                            MAI->getInlineAsmEnd());
167    return;
168  }
169
170  // Emit the #APP start marker.  This has to happen even if verbose-asm isn't
171  // enabled, so we use EmitRawText.
172  if (OutStreamer.hasRawTextSupport())
173    OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
174                            MAI->getInlineAsmStart());
175
176  // Get the !srcloc metadata node if we have it, and decode the loc cookie from
177  // it.
178  unsigned LocCookie = 0;
179  const MDNode *LocMD = 0;
180  for (unsigned i = MI->getNumOperands(); i != 0; --i) {
181    if (MI->getOperand(i-1).isMetadata() &&
182        (LocMD = MI->getOperand(i-1).getMetadata()) &&
183        LocMD->getNumOperands() != 0) {
184      if (const ConstantInt *CI = dyn_cast<ConstantInt>(LocMD->getOperand(0))) {
185        LocCookie = CI->getZExtValue();
186        break;
187      }
188    }
189  }
190
191  // Emit the inline asm to a temporary string so we can emit it through
192  // EmitInlineAsm.
193  SmallString<256> StringData;
194  raw_svector_ostream OS(StringData);
195
196  OS << '\t';
197
198  // The variant of the current asmprinter.
199  int AsmPrinterVariant = MAI->getAssemblerDialect();
200
201  int CurVariant = -1;            // The number of the {.|.|.} region we are in.
202  const char *LastEmitted = AsmStr; // One past the last character emitted.
203
204  while (*LastEmitted) {
205    switch (*LastEmitted) {
206    default: {
207      // Not a special case, emit the string section literally.
208      const char *LiteralEnd = LastEmitted+1;
209      while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
210             *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
211        ++LiteralEnd;
212      if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
213        OS.write(LastEmitted, LiteralEnd-LastEmitted);
214      LastEmitted = LiteralEnd;
215      break;
216    }
217    case '\n':
218      ++LastEmitted;   // Consume newline character.
219      OS << '\n';      // Indent code with newline.
220      break;
221    case '$': {
222      ++LastEmitted;   // Consume '$' character.
223      bool Done = true;
224
225      // Handle escapes.
226      switch (*LastEmitted) {
227      default: Done = false; break;
228      case '$':     // $$ -> $
229        if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
230          OS << '$';
231        ++LastEmitted;  // Consume second '$' character.
232        break;
233      case '(':             // $( -> same as GCC's { character.
234        ++LastEmitted;      // Consume '(' character.
235        if (CurVariant != -1)
236          report_fatal_error("Nested variants found in inline asm string: '" +
237                             Twine(AsmStr) + "'");
238        CurVariant = 0;     // We're in the first variant now.
239        break;
240      case '|':
241        ++LastEmitted;  // consume '|' character.
242        if (CurVariant == -1)
243          OS << '|';       // this is gcc's behavior for | outside a variant
244        else
245          ++CurVariant;   // We're in the next variant.
246        break;
247      case ')':         // $) -> same as GCC's } char.
248        ++LastEmitted;  // consume ')' character.
249        if (CurVariant == -1)
250          OS << '}';     // this is gcc's behavior for } outside a variant
251        else
252          CurVariant = -1;
253        break;
254      }
255      if (Done) break;
256
257      bool HasCurlyBraces = false;
258      if (*LastEmitted == '{') {     // ${variable}
259        ++LastEmitted;               // Consume '{' character.
260        HasCurlyBraces = true;
261      }
262
263      // If we have ${:foo}, then this is not a real operand reference, it is a
264      // "magic" string reference, just like in .td files.  Arrange to call
265      // PrintSpecial.
266      if (HasCurlyBraces && *LastEmitted == ':') {
267        ++LastEmitted;
268        const char *StrStart = LastEmitted;
269        const char *StrEnd = strchr(StrStart, '}');
270        if (StrEnd == 0)
271          report_fatal_error("Unterminated ${:foo} operand in inline asm"
272                             " string: '" + Twine(AsmStr) + "'");
273
274        std::string Val(StrStart, StrEnd);
275        PrintSpecial(MI, OS, Val.c_str());
276        LastEmitted = StrEnd+1;
277        break;
278      }
279
280      const char *IDStart = LastEmitted;
281      const char *IDEnd = IDStart;
282      while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
283
284      unsigned Val;
285      if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
286        report_fatal_error("Bad $ operand number in inline asm string: '" +
287                           Twine(AsmStr) + "'");
288      LastEmitted = IDEnd;
289
290      char Modifier[2] = { 0, 0 };
291
292      if (HasCurlyBraces) {
293        // If we have curly braces, check for a modifier character.  This
294        // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
295        if (*LastEmitted == ':') {
296          ++LastEmitted;    // Consume ':' character.
297          if (*LastEmitted == 0)
298            report_fatal_error("Bad ${:} expression in inline asm string: '" +
299                               Twine(AsmStr) + "'");
300
301          Modifier[0] = *LastEmitted;
302          ++LastEmitted;    // Consume modifier character.
303        }
304
305        if (*LastEmitted != '}')
306          report_fatal_error("Bad ${} expression in inline asm string: '" +
307                             Twine(AsmStr) + "'");
308        ++LastEmitted;    // Consume '}' character.
309      }
310
311      if (Val >= NumOperands-1)
312        report_fatal_error("Invalid $ operand number in inline asm string: '" +
313                           Twine(AsmStr) + "'");
314
315      // Okay, we finally have a value number.  Ask the target to print this
316      // operand!
317      if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
318        unsigned OpNo = InlineAsm::MIOp_FirstOperand;
319
320        bool Error = false;
321
322        // Scan to find the machine operand number for the operand.
323        for (; Val; --Val) {
324          if (OpNo >= MI->getNumOperands()) break;
325          unsigned OpFlags = MI->getOperand(OpNo).getImm();
326          OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
327        }
328
329        if (OpNo >= MI->getNumOperands()) {
330          Error = true;
331        } else {
332          unsigned OpFlags = MI->getOperand(OpNo).getImm();
333          ++OpNo;  // Skip over the ID number.
334
335          if (Modifier[0] == 'l')  // labels are target independent
336            // FIXME: What if the operand isn't an MBB, report error?
337            OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
338          else {
339            AsmPrinter *AP = const_cast<AsmPrinter*>(this);
340            if (InlineAsm::isMemKind(OpFlags)) {
341              Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
342                                                Modifier[0] ? Modifier : 0,
343                                                OS);
344            } else {
345              Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
346                                          Modifier[0] ? Modifier : 0, OS);
347            }
348          }
349        }
350        if (Error) {
351          std::string msg;
352          raw_string_ostream Msg(msg);
353          Msg << "invalid operand in inline asm: '" << AsmStr << "'";
354          MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
355        }
356      }
357      break;
358    }
359    }
360  }
361  OS << '\n' << (char)0;  // null terminate string.
362  EmitInlineAsm(OS.str(), LocMD);
363
364  // Emit the #NOAPP end marker.  This has to happen even if verbose-asm isn't
365  // enabled, so we use EmitRawText.
366  if (OutStreamer.hasRawTextSupport())
367    OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
368                            MAI->getInlineAsmEnd());
369}
370
371
372/// PrintSpecial - Print information related to the specified machine instr
373/// that is independent of the operand, and may be independent of the instr
374/// itself.  This can be useful for portably encoding the comment character
375/// or other bits of target-specific knowledge into the asmstrings.  The
376/// syntax used is ${:comment}.  Targets can override this to add support
377/// for their own strange codes.
378void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
379                              const char *Code) const {
380  if (!strcmp(Code, "private")) {
381    OS << MAI->getPrivateGlobalPrefix();
382  } else if (!strcmp(Code, "comment")) {
383    OS << MAI->getCommentString();
384  } else if (!strcmp(Code, "uid")) {
385    // Comparing the address of MI isn't sufficient, because machineinstrs may
386    // be allocated to the same address across functions.
387
388    // If this is a new LastFn instruction, bump the counter.
389    if (LastMI != MI || LastFn != getFunctionNumber()) {
390      ++Counter;
391      LastMI = MI;
392      LastFn = getFunctionNumber();
393    }
394    OS << Counter;
395  } else {
396    std::string msg;
397    raw_string_ostream Msg(msg);
398    Msg << "Unknown special formatter '" << Code
399         << "' for machine instr: " << *MI;
400    report_fatal_error(Msg.str());
401  }
402}
403
404/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
405/// instruction, using the specified assembler variant.  Targets should
406/// override this to format as appropriate.
407bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
408                                 unsigned AsmVariant, const char *ExtraCode,
409                                 raw_ostream &O) {
410  // Target doesn't support this yet!
411  return true;
412}
413
414bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
415                                       unsigned AsmVariant,
416                                       const char *ExtraCode, raw_ostream &O) {
417  // Target doesn't support this yet!
418  return true;
419}
420
421