blob: 861f738e5d3863b3012cccbc4d430c893fd54f77 [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"
24#include "llvm/MC/MCSymbol.h"
Chris Lattneraf632c92010-04-05 23:11:24 +000025#include "llvm/Target/TargetAsmParser.h"
26#include "llvm/Target/TargetMachine.h"
27#include "llvm/Target/TargetRegistry.h"
28#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"
Chris Lattner736e31d2010-04-04 18:34:07 +000034#include "llvm/Support/raw_ostream.h"
Chris Lattner736e31d2010-04-04 18:34:07 +000035using namespace llvm;
36
37/// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
Chris Lattnera38941d2010-11-17 07:53:40 +000038void AsmPrinter::EmitInlineAsm(StringRef Str, const MDNode *LocMDNode) const {
Chris Lattner736e31d2010-04-04 18:34:07 +000039 assert(!Str.empty() && "Can't emit empty inline asm block");
Jim Grosbach2b2de242010-10-01 23:29:12 +000040
Chris Lattneraf632c92010-04-05 23:11:24 +000041 // Remember if the buffer is nul terminated or not so we can avoid a copy.
42 bool isNullTerminated = Str.back() == 0;
43 if (isNullTerminated)
44 Str = Str.substr(0, Str.size()-1);
Jim Grosbach2b2de242010-10-01 23:29:12 +000045
Chris Lattner736e31d2010-04-04 18:34:07 +000046 // If the output streamer is actually a .s file, just emit the blob textually.
47 // This is useful in case the asm parser doesn't handle something but the
48 // system assembler does.
49 if (OutStreamer.hasRawTextSupport()) {
50 OutStreamer.EmitRawText(Str);
51 return;
52 }
Jim Grosbach2b2de242010-10-01 23:29:12 +000053
Chris Lattneraf632c92010-04-05 23:11:24 +000054 SourceMgr SrcMgr;
Jim Grosbach2b2de242010-10-01 23:29:12 +000055
Chris Lattner6eb78062010-04-06 00:55:39 +000056 // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
57 LLVMContext &LLVMCtx = MMI->getModule()->getContext();
58 bool HasDiagHandler = false;
59 if (void *DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler()) {
Chris Lattnera38941d2010-11-17 07:53:40 +000060 unsigned LocCookie = 0;
61 if (LocMDNode && LocMDNode->getNumOperands() > 0)
62 if (const ConstantInt *CI =
63 dyn_cast<ConstantInt>(LocMDNode->getOperand(0)))
64 LocCookie = CI->getZExtValue();
65
Chris Lattner6eb78062010-04-06 00:55:39 +000066 SrcMgr.setDiagHandler((SourceMgr::DiagHandlerTy)(intptr_t)DiagHandler,
Chris Lattner885d9412010-04-06 00:58:50 +000067 LLVMCtx.getInlineAsmDiagnosticContext(), LocCookie);
Chris Lattner6eb78062010-04-06 00:55:39 +000068 HasDiagHandler = true;
69 }
Jim Grosbach2b2de242010-10-01 23:29:12 +000070
Chris Lattneraf632c92010-04-05 23:11:24 +000071 MemoryBuffer *Buffer;
72 if (isNullTerminated)
73 Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
74 else
75 Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
76
77 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
78 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach2b2de242010-10-01 23:29:12 +000079
Daniel Dunbar9fbb37e2010-07-18 18:31:33 +000080 OwningPtr<MCAsmParser> Parser(createMCAsmParser(TM.getTarget(), SrcMgr,
81 OutContext, OutStreamer,
82 *MAI));
Daniel Dunbard73ada72010-07-19 00:33:49 +000083 OwningPtr<TargetAsmParser> TAP(TM.getTarget().createAsmParser(*Parser, TM));
Chris Lattneraf632c92010-04-05 23:11:24 +000084 if (!TAP)
Chris Lattner75361b62010-04-07 22:58:41 +000085 report_fatal_error("Inline asm not supported by this streamer because"
Benjamin Kramer1bd73352010-04-08 10:44:28 +000086 " we don't have an asm parser for this target\n");
Daniel Dunbar9fbb37e2010-07-18 18:31:33 +000087 Parser->setTargetParser(*TAP.get());
Chris Lattneraf632c92010-04-05 23:11:24 +000088
89 // Don't implicitly switch to the text section before the asm.
Daniel Dunbar9fbb37e2010-07-18 18:31:33 +000090 int Res = Parser->Run(/*NoInitialTextSection*/ true,
91 /*NoFinalize*/ true);
Chris Lattner6eb78062010-04-06 00:55:39 +000092 if (Res && !HasDiagHandler)
Chris Lattner75361b62010-04-07 22:58:41 +000093 report_fatal_error("Error parsing inline asm\n");
Chris Lattner736e31d2010-04-04 18:34:07 +000094}
95
96
97/// EmitInlineAsm - This method formats and emits the specified machine
98/// instruction that is an inline asm.
99void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
100 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000101
Chris Lattner736e31d2010-04-04 18:34:07 +0000102 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach2b2de242010-10-01 23:29:12 +0000103
Chris Lattner736e31d2010-04-04 18:34:07 +0000104 // Count the number of register definitions to find the asm string.
105 unsigned NumDefs = 0;
106 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
107 ++NumDefs)
Chris Lattnercf9a4152010-04-07 05:38:05 +0000108 assert(NumDefs != NumOperands-2 && "No asm string?");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000109
Chris Lattner736e31d2010-04-04 18:34:07 +0000110 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
111
112 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
113 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
114
115 // If this asmstr is empty, just print the #APP/#NOAPP markers.
116 // These are useful to see where empty asm's wound up.
117 if (AsmStr[0] == 0) {
Chris Lattner4c842dd2010-04-05 22:42:30 +0000118 // Don't emit the comments if writing to a .o file.
Chris Lattner736e31d2010-04-04 18:34:07 +0000119 if (!OutStreamer.hasRawTextSupport()) return;
120
121 OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
122 MAI->getInlineAsmStart());
123 OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
124 MAI->getInlineAsmEnd());
125 return;
126 }
127
128 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
129 // enabled, so we use EmitRawText.
130 if (OutStreamer.hasRawTextSupport())
131 OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
132 MAI->getInlineAsmStart());
133
Chris Lattnercf9a4152010-04-07 05:38:05 +0000134 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
135 // it.
136 unsigned LocCookie = 0;
Chris Lattnera38941d2010-11-17 07:53:40 +0000137 const MDNode *LocMD = 0;
Chris Lattnerd0024fe2010-04-08 18:20:52 +0000138 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
Chris Lattnera38941d2010-11-17 07:53:40 +0000139 if (MI->getOperand(i-1).isMetadata() &&
140 (LocMD = MI->getOperand(i-1).getMetadata()) &&
141 LocMD->getNumOperands() != 0) {
142 if (const ConstantInt *CI = dyn_cast<ConstantInt>(LocMD->getOperand(0))) {
143 LocCookie = CI->getZExtValue();
144 break;
145 }
146 }
Chris Lattnercf9a4152010-04-07 05:38:05 +0000147 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000148
Chris Lattner736e31d2010-04-04 18:34:07 +0000149 // Emit the inline asm to a temporary string so we can emit it through
150 // EmitInlineAsm.
151 SmallString<256> StringData;
152 raw_svector_ostream OS(StringData);
Jim Grosbach2b2de242010-10-01 23:29:12 +0000153
Chris Lattner736e31d2010-04-04 18:34:07 +0000154 OS << '\t';
155
156 // The variant of the current asmprinter.
157 int AsmPrinterVariant = MAI->getAssemblerDialect();
158
159 int CurVariant = -1; // The number of the {.|.|.} region we are in.
160 const char *LastEmitted = AsmStr; // One past the last character emitted.
Jim Grosbach2b2de242010-10-01 23:29:12 +0000161
Chris Lattner736e31d2010-04-04 18:34:07 +0000162 while (*LastEmitted) {
163 switch (*LastEmitted) {
164 default: {
165 // Not a special case, emit the string section literally.
166 const char *LiteralEnd = LastEmitted+1;
167 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
168 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
169 ++LiteralEnd;
170 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
171 OS.write(LastEmitted, LiteralEnd-LastEmitted);
172 LastEmitted = LiteralEnd;
173 break;
174 }
175 case '\n':
176 ++LastEmitted; // Consume newline character.
Chris Lattner4c842dd2010-04-05 22:42:30 +0000177 OS << '\n'; // Indent code with newline.
Chris Lattner736e31d2010-04-04 18:34:07 +0000178 break;
179 case '$': {
180 ++LastEmitted; // Consume '$' character.
181 bool Done = true;
182
183 // Handle escapes.
184 switch (*LastEmitted) {
185 default: Done = false; break;
186 case '$': // $$ -> $
187 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
188 OS << '$';
189 ++LastEmitted; // Consume second '$' character.
190 break;
191 case '(': // $( -> same as GCC's { character.
192 ++LastEmitted; // Consume '(' character.
Chris Lattnerfee455e2010-04-07 05:27:36 +0000193 if (CurVariant != -1)
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000194 report_fatal_error("Nested variants found in inline asm string: '" +
195 Twine(AsmStr) + "'");
Chris Lattner736e31d2010-04-04 18:34:07 +0000196 CurVariant = 0; // We're in the first variant now.
197 break;
198 case '|':
199 ++LastEmitted; // consume '|' character.
200 if (CurVariant == -1)
201 OS << '|'; // this is gcc's behavior for | outside a variant
202 else
203 ++CurVariant; // We're in the next variant.
204 break;
205 case ')': // $) -> same as GCC's } char.
206 ++LastEmitted; // consume ')' character.
207 if (CurVariant == -1)
208 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach2b2de242010-10-01 23:29:12 +0000209 else
Chris Lattner736e31d2010-04-04 18:34:07 +0000210 CurVariant = -1;
211 break;
212 }
213 if (Done) break;
Jim Grosbach2b2de242010-10-01 23:29:12 +0000214
Chris Lattner736e31d2010-04-04 18:34:07 +0000215 bool HasCurlyBraces = false;
216 if (*LastEmitted == '{') { // ${variable}
217 ++LastEmitted; // Consume '{' character.
218 HasCurlyBraces = true;
219 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000220
Chris Lattner736e31d2010-04-04 18:34:07 +0000221 // If we have ${:foo}, then this is not a real operand reference, it is a
222 // "magic" string reference, just like in .td files. Arrange to call
223 // PrintSpecial.
224 if (HasCurlyBraces && *LastEmitted == ':') {
225 ++LastEmitted;
226 const char *StrStart = LastEmitted;
227 const char *StrEnd = strchr(StrStart, '}');
228 if (StrEnd == 0)
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000229 report_fatal_error("Unterminated ${:foo} operand in inline asm"
230 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000231
Chris Lattner736e31d2010-04-04 18:34:07 +0000232 std::string Val(StrStart, StrEnd);
233 PrintSpecial(MI, OS, Val.c_str());
234 LastEmitted = StrEnd+1;
235 break;
236 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000237
Chris Lattner736e31d2010-04-04 18:34:07 +0000238 const char *IDStart = LastEmitted;
Chris Lattner65eeaad2010-04-04 18:42:18 +0000239 const char *IDEnd = IDStart;
Jim Grosbach2b2de242010-10-01 23:29:12 +0000240 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
241
Chris Lattner65eeaad2010-04-04 18:42:18 +0000242 unsigned Val;
243 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000244 report_fatal_error("Bad $ operand number in inline asm string: '" +
245 Twine(AsmStr) + "'");
Chris Lattner736e31d2010-04-04 18:34:07 +0000246 LastEmitted = IDEnd;
Jim Grosbach2b2de242010-10-01 23:29:12 +0000247
Chris Lattner736e31d2010-04-04 18:34:07 +0000248 char Modifier[2] = { 0, 0 };
Jim Grosbach2b2de242010-10-01 23:29:12 +0000249
Chris Lattner736e31d2010-04-04 18:34:07 +0000250 if (HasCurlyBraces) {
251 // If we have curly braces, check for a modifier character. This
252 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
253 if (*LastEmitted == ':') {
254 ++LastEmitted; // Consume ':' character.
Chris Lattner4c842dd2010-04-05 22:42:30 +0000255 if (*LastEmitted == 0)
Chris Lattner75361b62010-04-07 22:58:41 +0000256 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000257 Twine(AsmStr) + "'");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000258
Chris Lattner736e31d2010-04-04 18:34:07 +0000259 Modifier[0] = *LastEmitted;
260 ++LastEmitted; // Consume modifier character.
261 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000262
Chris Lattner4c842dd2010-04-05 22:42:30 +0000263 if (*LastEmitted != '}')
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000264 report_fatal_error("Bad ${} expression in inline asm string: '" +
265 Twine(AsmStr) + "'");
Chris Lattner736e31d2010-04-04 18:34:07 +0000266 ++LastEmitted; // Consume '}' character.
267 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000268
Chris Lattner4c842dd2010-04-05 22:42:30 +0000269 if (Val >= NumOperands-1)
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000270 report_fatal_error("Invalid $ operand number in inline asm string: '" +
271 Twine(AsmStr) + "'");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000272
Chris Lattner736e31d2010-04-04 18:34:07 +0000273 // Okay, we finally have a value number. Ask the target to print this
274 // operand!
275 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Dale Johannesenf1e309e2010-07-02 20:16:09 +0000276 unsigned OpNo = 2;
Chris Lattner736e31d2010-04-04 18:34:07 +0000277
278 bool Error = false;
279
280 // Scan to find the machine operand number for the operand.
281 for (; Val; --Val) {
282 if (OpNo >= MI->getNumOperands()) break;
283 unsigned OpFlags = MI->getOperand(OpNo).getImm();
284 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
285 }
286
287 if (OpNo >= MI->getNumOperands()) {
288 Error = true;
289 } else {
290 unsigned OpFlags = MI->getOperand(OpNo).getImm();
291 ++OpNo; // Skip over the ID number.
292
293 if (Modifier[0] == 'l') // labels are target independent
294 // FIXME: What if the operand isn't an MBB, report error?
295 OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
296 else {
297 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Chris Lattnerfee455e2010-04-07 05:27:36 +0000298 if (InlineAsm::isMemKind(OpFlags)) {
Chris Lattner736e31d2010-04-04 18:34:07 +0000299 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
300 Modifier[0] ? Modifier : 0,
301 OS);
302 } else {
303 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
304 Modifier[0] ? Modifier : 0, OS);
305 }
306 }
307 }
308 if (Error) {
309 std::string msg;
310 raw_string_ostream Msg(msg);
Chris Lattner38686bd2010-04-07 23:40:44 +0000311 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
312 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner736e31d2010-04-04 18:34:07 +0000313 }
314 }
315 break;
316 }
317 }
318 }
Chris Lattneraf632c92010-04-05 23:11:24 +0000319 OS << '\n' << (char)0; // null terminate string.
Chris Lattnera38941d2010-11-17 07:53:40 +0000320 EmitInlineAsm(OS.str(), LocMD);
Jim Grosbach2b2de242010-10-01 23:29:12 +0000321
Chris Lattner736e31d2010-04-04 18:34:07 +0000322 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
323 // enabled, so we use EmitRawText.
324 if (OutStreamer.hasRawTextSupport())
325 OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
326 MAI->getInlineAsmEnd());
327}
328
329
330/// PrintSpecial - Print information related to the specified machine instr
331/// that is independent of the operand, and may be independent of the instr
332/// itself. This can be useful for portably encoding the comment character
333/// or other bits of target-specific knowledge into the asmstrings. The
334/// syntax used is ${:comment}. Targets can override this to add support
335/// for their own strange codes.
336void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
337 const char *Code) const {
338 if (!strcmp(Code, "private")) {
339 OS << MAI->getPrivateGlobalPrefix();
340 } else if (!strcmp(Code, "comment")) {
341 OS << MAI->getCommentString();
342 } else if (!strcmp(Code, "uid")) {
343 // Comparing the address of MI isn't sufficient, because machineinstrs may
344 // be allocated to the same address across functions.
Jim Grosbach2b2de242010-10-01 23:29:12 +0000345
Chris Lattner736e31d2010-04-04 18:34:07 +0000346 // If this is a new LastFn instruction, bump the counter.
347 if (LastMI != MI || LastFn != getFunctionNumber()) {
348 ++Counter;
349 LastMI = MI;
350 LastFn = getFunctionNumber();
351 }
352 OS << Counter;
353 } else {
354 std::string msg;
355 raw_string_ostream Msg(msg);
356 Msg << "Unknown special formatter '" << Code
357 << "' for machine instr: " << *MI;
Chris Lattner75361b62010-04-07 22:58:41 +0000358 report_fatal_error(Msg.str());
Jim Grosbach2b2de242010-10-01 23:29:12 +0000359 }
Chris Lattner736e31d2010-04-04 18:34:07 +0000360}
361
362/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
363/// instruction, using the specified assembler variant. Targets should
364/// override this to format as appropriate.
365bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
366 unsigned AsmVariant, const char *ExtraCode,
367 raw_ostream &O) {
368 // Target doesn't support this yet!
369 return true;
370}
371
372bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
373 unsigned AsmVariant,
374 const char *ExtraCode, raw_ostream &O) {
375 // Target doesn't support this yet!
376 return true;
377}
378