blob: 1844e44a47a163c43fd359f4449b69878fb02389 [file] [log] [blame]
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001//===-- AsmPrinterInlineAsm.cpp - AsmPrinter Inline Asm Handling ----------===//
Chris Lattner736e31d2010-04-04 18:34:07 +00002//
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//
Chris Lattner7e1a8f82010-04-04 19:09:29 +000010// This file implements the inline assembler pieces of the AsmPrinter class.
Chris Lattner736e31d2010-04-04 18:34:07 +000011//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "asm-printer"
Chris Lattner736e31d2010-04-04 18:34:07 +000015#include "llvm/CodeGen/AsmPrinter.h"
Chris Lattnercf9a4152010-04-07 05:38:05 +000016#include "llvm/Constants.h"
Chris Lattner7e1a8f82010-04-04 19:09:29 +000017#include "llvm/InlineAsm.h"
Chris Lattner6eb78062010-04-06 00:55:39 +000018#include "llvm/LLVMContext.h"
19#include "llvm/Module.h"
Chris Lattner736e31d2010-04-04 18:34:07 +000020#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner6eb78062010-04-06 00:55:39 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner736e31d2010-04-04 18:34:07 +000022#include "llvm/MC/MCAsmInfo.h"
23#include "llvm/MC/MCStreamer.h"
Evan Chengffc0e732011-07-09 05:47:46 +000024#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner736e31d2010-04-04 18:34:07 +000025#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000026#include "llvm/MC/MCTargetAsmParser.h"
Chris Lattneraf632c92010-04-05 23:11:24 +000027#include "llvm/Target/TargetMachine.h"
Chris Lattneraf632c92010-04-05 23:11:24 +000028#include "llvm/ADT/OwningPtr.h"
Chris Lattner736e31d2010-04-04 18:34:07 +000029#include "llvm/ADT/SmallString.h"
30#include "llvm/ADT/Twine.h"
31#include "llvm/Support/ErrorHandling.h"
Chris Lattneraf632c92010-04-05 23:11:24 +000032#include "llvm/Support/MemoryBuffer.h"
33#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000034#include "llvm/Support/TargetRegistry.h"
Chris Lattner736e31d2010-04-04 18:34:07 +000035#include "llvm/Support/raw_ostream.h"
Chris Lattner736e31d2010-04-04 18:34:07 +000036using namespace llvm;
37
Chris Lattner6e30c6a2010-11-17 08:03:32 +000038namespace {
39 struct SrcMgrDiagInfo {
40 const MDNode *LocInfo;
Chris Lattner4afa1282010-11-17 08:13:01 +000041 LLVMContext::InlineAsmDiagHandlerTy DiagHandler;
Chris Lattner6e30c6a2010-11-17 08:03:32 +000042 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.
Chris Lattner4afa1282010-11-17 08:13:01 +000049static void SrcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
Chris Lattner6e30c6a2010-11-17 08:03:32 +000050 SrcMgrDiagInfo *DiagInfo = static_cast<SrcMgrDiagInfo *>(diagInfo);
51 assert(DiagInfo && "Diagnostic context not passed down?");
Jim Grosbachfe59d852011-09-21 21:36:53 +000052
Chris Lattnerce1b9ad2010-11-17 08:20:42 +000053 // If the inline asm had metadata associated with it, pull out a location
54 // cookie corresponding to which line the error occurred on.
Chris Lattner6e30c6a2010-11-17 08:03:32 +000055 unsigned LocCookie = 0;
Chris Lattnerce1b9ad2010-11-17 08:20:42 +000056 if (const MDNode *LocInfo = DiagInfo->LocInfo) {
57 unsigned ErrorLine = Diag.getLineNo()-1;
58 if (ErrorLine >= LocInfo->getNumOperands())
59 ErrorLine = 0;
Jim Grosbachfe59d852011-09-21 21:36:53 +000060
Chris Lattnerce1b9ad2010-11-17 08:20:42 +000061 if (LocInfo->getNumOperands() != 0)
62 if (const ConstantInt *CI =
63 dyn_cast<ConstantInt>(LocInfo->getOperand(ErrorLine)))
Chris Lattner6e30c6a2010-11-17 08:03:32 +000064 LocCookie = CI->getZExtValue();
Chris Lattnerce1b9ad2010-11-17 08:20:42 +000065 }
Jim Grosbachfe59d852011-09-21 21:36:53 +000066
Chris Lattner4afa1282010-11-17 08:13:01 +000067 DiagInfo->DiagHandler(Diag, DiagInfo->DiagContext, LocCookie);
Chris Lattner6e30c6a2010-11-17 08:03:32 +000068}
69
Chris Lattner736e31d2010-04-04 18:34:07 +000070/// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
Chris Lattnera38941d2010-11-17 07:53:40 +000071void AsmPrinter::EmitInlineAsm(StringRef Str, const MDNode *LocMDNode) const {
Chris Lattner736e31d2010-04-04 18:34:07 +000072 assert(!Str.empty() && "Can't emit empty inline asm block");
Jim Grosbach2b2de242010-10-01 23:29:12 +000073
Chris Lattneraf632c92010-04-05 23:11:24 +000074 // 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);
Jim Grosbach2b2de242010-10-01 23:29:12 +000078
Chris Lattner736e31d2010-04-04 18:34:07 +000079 // 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 }
Jim Grosbach2b2de242010-10-01 23:29:12 +000086
Chris Lattneraf632c92010-04-05 23:11:24 +000087 SourceMgr SrcMgr;
Chris Lattner6e30c6a2010-11-17 08:03:32 +000088 SrcMgrDiagInfo DiagInfo;
Jim Grosbach2b2de242010-10-01 23:29:12 +000089
Chris Lattner6eb78062010-04-06 00:55:39 +000090 // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
91 LLVMContext &LLVMCtx = MMI->getModule()->getContext();
92 bool HasDiagHandler = false;
Chris Lattner4afa1282010-11-17 08:13:01 +000093 if (LLVMCtx.getInlineAsmDiagnosticHandler() != 0) {
Chris Lattner6e30c6a2010-11-17 08:03:32 +000094 // If the source manager has an issue, we arrange for SrcMgrDiagHandler
95 // to be invoked, getting DiagInfo passed into it.
96 DiagInfo.LocInfo = LocMDNode;
Chris Lattner4afa1282010-11-17 08:13:01 +000097 DiagInfo.DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
Chris Lattner6e30c6a2010-11-17 08:03:32 +000098 DiagInfo.DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
99 SrcMgr.setDiagHandler(SrcMgrDiagHandler, &DiagInfo);
Chris Lattner6eb78062010-04-06 00:55:39 +0000100 HasDiagHandler = true;
101 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000102
Chris Lattneraf632c92010-04-05 23:11:24 +0000103 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());
Jim Grosbach2b2de242010-10-01 23:29:12 +0000111
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000112 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Daniel Dunbar9fbb37e2010-07-18 18:31:33 +0000113 OutContext, OutStreamer,
114 *MAI));
Evan Chengebdeeab2011-07-08 01:53:10 +0000115
Evan Chengffc0e732011-07-09 05:47:46 +0000116 // 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()));
Evan Cheng94b95502011-07-26 00:24:13 +0000124 OwningPtr<MCTargetAsmParser>
125 TAP(TM.getTarget().createMCAsmParser(*STI, *Parser));
Chris Lattneraf632c92010-04-05 23:11:24 +0000126 if (!TAP)
Chris Lattner75361b62010-04-07 22:58:41 +0000127 report_fatal_error("Inline asm not supported by this streamer because"
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000128 " we don't have an asm parser for this target\n");
Daniel Dunbar9fbb37e2010-07-18 18:31:33 +0000129 Parser->setTargetParser(*TAP.get());
Chris Lattneraf632c92010-04-05 23:11:24 +0000130
131 // Don't implicitly switch to the text section before the asm.
Daniel Dunbar9fbb37e2010-07-18 18:31:33 +0000132 int Res = Parser->Run(/*NoInitialTextSection*/ true,
133 /*NoFinalize*/ true);
Chris Lattner6eb78062010-04-06 00:55:39 +0000134 if (Res && !HasDiagHandler)
Chris Lattner75361b62010-04-07 22:58:41 +0000135 report_fatal_error("Error parsing inline asm\n");
Chris Lattner736e31d2010-04-04 18:34:07 +0000136}
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");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000143
Chris Lattner736e31d2010-04-04 18:34:07 +0000144 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach2b2de242010-10-01 23:29:12 +0000145
Chris Lattner736e31d2010-04-04 18:34:07 +0000146 // 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)
Chris Lattnercf9a4152010-04-07 05:38:05 +0000150 assert(NumDefs != NumOperands-2 && "No asm string?");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000151
Chris Lattner736e31d2010-04-04 18:34:07 +0000152 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) {
Chris Lattner4c842dd2010-04-05 22:42:30 +0000160 // Don't emit the comments if writing to a .o file.
Chris Lattner736e31d2010-04-04 18:34:07 +0000161 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
Chris Lattnercf9a4152010-04-07 05:38:05 +0000176 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
177 // it.
178 unsigned LocCookie = 0;
Chris Lattnera38941d2010-11-17 07:53:40 +0000179 const MDNode *LocMD = 0;
Chris Lattnerd0024fe2010-04-08 18:20:52 +0000180 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
Chris Lattnera38941d2010-11-17 07:53:40 +0000181 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 }
Chris Lattnercf9a4152010-04-07 05:38:05 +0000189 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000190
Chris Lattner736e31d2010-04-04 18:34:07 +0000191 // 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);
Jim Grosbach2b2de242010-10-01 23:29:12 +0000195
Chris Lattner736e31d2010-04-04 18:34:07 +0000196 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.
Jim Grosbach2b2de242010-10-01 23:29:12 +0000203
Chris Lattner736e31d2010-04-04 18:34:07 +0000204 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.
Chris Lattner4c842dd2010-04-05 22:42:30 +0000219 OS << '\n'; // Indent code with newline.
Chris Lattner736e31d2010-04-04 18:34:07 +0000220 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.
Chris Lattnerfee455e2010-04-07 05:27:36 +0000235 if (CurVariant != -1)
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000236 report_fatal_error("Nested variants found in inline asm string: '" +
237 Twine(AsmStr) + "'");
Chris Lattner736e31d2010-04-04 18:34:07 +0000238 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
Jim Grosbach2b2de242010-10-01 23:29:12 +0000251 else
Chris Lattner736e31d2010-04-04 18:34:07 +0000252 CurVariant = -1;
253 break;
254 }
255 if (Done) break;
Jim Grosbach2b2de242010-10-01 23:29:12 +0000256
Chris Lattner736e31d2010-04-04 18:34:07 +0000257 bool HasCurlyBraces = false;
258 if (*LastEmitted == '{') { // ${variable}
259 ++LastEmitted; // Consume '{' character.
260 HasCurlyBraces = true;
261 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000262
Chris Lattner736e31d2010-04-04 18:34:07 +0000263 // 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)
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000271 report_fatal_error("Unterminated ${:foo} operand in inline asm"
272 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000273
Chris Lattner736e31d2010-04-04 18:34:07 +0000274 std::string Val(StrStart, StrEnd);
275 PrintSpecial(MI, OS, Val.c_str());
276 LastEmitted = StrEnd+1;
277 break;
278 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000279
Chris Lattner736e31d2010-04-04 18:34:07 +0000280 const char *IDStart = LastEmitted;
Chris Lattner65eeaad2010-04-04 18:42:18 +0000281 const char *IDEnd = IDStart;
Jim Grosbach2b2de242010-10-01 23:29:12 +0000282 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
283
Chris Lattner65eeaad2010-04-04 18:42:18 +0000284 unsigned Val;
285 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000286 report_fatal_error("Bad $ operand number in inline asm string: '" +
287 Twine(AsmStr) + "'");
Chris Lattner736e31d2010-04-04 18:34:07 +0000288 LastEmitted = IDEnd;
Jim Grosbach2b2de242010-10-01 23:29:12 +0000289
Chris Lattner736e31d2010-04-04 18:34:07 +0000290 char Modifier[2] = { 0, 0 };
Jim Grosbach2b2de242010-10-01 23:29:12 +0000291
Chris Lattner736e31d2010-04-04 18:34:07 +0000292 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.
Chris Lattner4c842dd2010-04-05 22:42:30 +0000297 if (*LastEmitted == 0)
Chris Lattner75361b62010-04-07 22:58:41 +0000298 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000299 Twine(AsmStr) + "'");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000300
Chris Lattner736e31d2010-04-04 18:34:07 +0000301 Modifier[0] = *LastEmitted;
302 ++LastEmitted; // Consume modifier character.
303 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000304
Chris Lattner4c842dd2010-04-05 22:42:30 +0000305 if (*LastEmitted != '}')
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000306 report_fatal_error("Bad ${} expression in inline asm string: '" +
307 Twine(AsmStr) + "'");
Chris Lattner736e31d2010-04-04 18:34:07 +0000308 ++LastEmitted; // Consume '}' character.
309 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000310
Chris Lattner4c842dd2010-04-05 22:42:30 +0000311 if (Val >= NumOperands-1)
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000312 report_fatal_error("Invalid $ operand number in inline asm string: '" +
313 Twine(AsmStr) + "'");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000314
Chris Lattner736e31d2010-04-04 18:34:07 +0000315 // Okay, we finally have a value number. Ask the target to print this
316 // operand!
317 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Chengc36b7062011-01-07 23:50:32 +0000318 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner736e31d2010-04-04 18:34:07 +0000319
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
Akira Hatanaka9318e482012-05-08 19:14:42 +0000329 // We may have a location metadata attached to the end of the
330 // instruction, and at no point should see metadata at any
331 // other point while processing. It's an error if so.
Eric Christopheraa206ff2012-03-22 01:33:51 +0000332 if (OpNo >= MI->getNumOperands() ||
Akira Hatanaka9318e482012-05-08 19:14:42 +0000333 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner736e31d2010-04-04 18:34:07 +0000334 Error = true;
335 } else {
336 unsigned OpFlags = MI->getOperand(OpNo).getImm();
337 ++OpNo; // Skip over the ID number.
338
339 if (Modifier[0] == 'l') // labels are target independent
340 // FIXME: What if the operand isn't an MBB, report error?
341 OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
342 else {
343 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Chris Lattnerfee455e2010-04-07 05:27:36 +0000344 if (InlineAsm::isMemKind(OpFlags)) {
Chris Lattner736e31d2010-04-04 18:34:07 +0000345 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
346 Modifier[0] ? Modifier : 0,
347 OS);
348 } else {
349 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
350 Modifier[0] ? Modifier : 0, OS);
351 }
352 }
353 }
354 if (Error) {
355 std::string msg;
356 raw_string_ostream Msg(msg);
Chris Lattner38686bd2010-04-07 23:40:44 +0000357 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
358 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner736e31d2010-04-04 18:34:07 +0000359 }
360 }
361 break;
362 }
363 }
364 }
Chris Lattneraf632c92010-04-05 23:11:24 +0000365 OS << '\n' << (char)0; // null terminate string.
Chris Lattnera38941d2010-11-17 07:53:40 +0000366 EmitInlineAsm(OS.str(), LocMD);
Jim Grosbach2b2de242010-10-01 23:29:12 +0000367
Chris Lattner736e31d2010-04-04 18:34:07 +0000368 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
369 // enabled, so we use EmitRawText.
370 if (OutStreamer.hasRawTextSupport())
371 OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
372 MAI->getInlineAsmEnd());
373}
374
375
376/// PrintSpecial - Print information related to the specified machine instr
377/// that is independent of the operand, and may be independent of the instr
378/// itself. This can be useful for portably encoding the comment character
379/// or other bits of target-specific knowledge into the asmstrings. The
380/// syntax used is ${:comment}. Targets can override this to add support
381/// for their own strange codes.
382void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
383 const char *Code) const {
384 if (!strcmp(Code, "private")) {
385 OS << MAI->getPrivateGlobalPrefix();
386 } else if (!strcmp(Code, "comment")) {
387 OS << MAI->getCommentString();
388 } else if (!strcmp(Code, "uid")) {
389 // Comparing the address of MI isn't sufficient, because machineinstrs may
390 // be allocated to the same address across functions.
Jim Grosbach2b2de242010-10-01 23:29:12 +0000391
Chris Lattner736e31d2010-04-04 18:34:07 +0000392 // If this is a new LastFn instruction, bump the counter.
393 if (LastMI != MI || LastFn != getFunctionNumber()) {
394 ++Counter;
395 LastMI = MI;
396 LastFn = getFunctionNumber();
397 }
398 OS << Counter;
399 } else {
400 std::string msg;
401 raw_string_ostream Msg(msg);
402 Msg << "Unknown special formatter '" << Code
403 << "' for machine instr: " << *MI;
Chris Lattner75361b62010-04-07 22:58:41 +0000404 report_fatal_error(Msg.str());
Jim Grosbach2b2de242010-10-01 23:29:12 +0000405 }
Chris Lattner736e31d2010-04-04 18:34:07 +0000406}
407
408/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
409/// instruction, using the specified assembler variant. Targets should
410/// override this to format as appropriate.
411bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
412 unsigned AsmVariant, const char *ExtraCode,
413 raw_ostream &O) {
414 // Target doesn't support this yet!
415 return true;
416}
417
418bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
419 unsigned AsmVariant,
420 const char *ExtraCode, raw_ostream &O) {
421 // Target doesn't support this yet!
422 return true;
423}
424