blob: cd1e4102761ea5cf62af0ed7ef1997bf778a453e [file] [log] [blame]
Chris Lattner9efd1182010-04-04 19:09:29 +00001//===-- AsmPrinterInlineAsm.cpp - AsmPrinter Inline Asm Handling ----------===//
Chris Lattner1e158692010-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 Lattner9efd1182010-04-04 19:09:29 +000010// This file implements the inline assembler pieces of the AsmPrinter class.
Chris Lattner1e158692010-04-04 18:34:07 +000011//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "asm-printer"
Chris Lattner1e158692010-04-04 18:34:07 +000015#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/OwningPtr.h"
17#include "llvm/ADT/SmallString.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/CodeGen/MachineBasicBlock.h"
20#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
Rafael Espindola58873562014-01-03 19:21:54 +000022#include "llvm/IR/DataLayout.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/InlineAsm.h"
24#include "llvm/IR/LLVMContext.h"
25#include "llvm/IR/Module.h"
Chris Lattner1e158692010-04-04 18:34:07 +000026#include "llvm/MC/MCAsmInfo.h"
27#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000028#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner1e158692010-04-04 18:34:07 +000029#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000030#include "llvm/MC/MCTargetAsmParser.h"
Chris Lattner1e158692010-04-04 18:34:07 +000031#include "llvm/Support/ErrorHandling.h"
Chris Lattner8900ef12010-04-05 23:11:24 +000032#include "llvm/Support/MemoryBuffer.h"
33#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000034#include "llvm/Support/TargetRegistry.h"
Chris Lattner1e158692010-04-04 18:34:07 +000035#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Target/TargetMachine.h"
Chris Lattner1e158692010-04-04 18:34:07 +000037using namespace llvm;
38
Chris Lattner300fa452010-11-17 08:03:32 +000039namespace {
40 struct SrcMgrDiagInfo {
41 const MDNode *LocInfo;
Bob Wilsona594fab2013-02-11 05:37:07 +000042 LLVMContext::InlineAsmDiagHandlerTy DiagHandler;
Chris Lattner300fa452010-11-17 08:03:32 +000043 void *DiagContext;
44 };
45}
46
Chad Rosierb759ede2012-09-07 18:16:38 +000047/// srcMgrDiagHandler - This callback is invoked when the SourceMgr for an
Chris Lattner300fa452010-11-17 08:03:32 +000048/// inline asm has an error in it. diagInfo is a pointer to the SrcMgrDiagInfo
49/// struct above.
Chad Rosierb759ede2012-09-07 18:16:38 +000050static void srcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
Chris Lattner300fa452010-11-17 08:03:32 +000051 SrcMgrDiagInfo *DiagInfo = static_cast<SrcMgrDiagInfo *>(diagInfo);
52 assert(DiagInfo && "Diagnostic context not passed down?");
Jim Grosbach098f5a22011-09-21 21:36:53 +000053
Chris Lattner79ffdc72010-11-17 08:20:42 +000054 // If the inline asm had metadata associated with it, pull out a location
55 // cookie corresponding to which line the error occurred on.
Chris Lattner300fa452010-11-17 08:03:32 +000056 unsigned LocCookie = 0;
Chris Lattner79ffdc72010-11-17 08:20:42 +000057 if (const MDNode *LocInfo = DiagInfo->LocInfo) {
58 unsigned ErrorLine = Diag.getLineNo()-1;
59 if (ErrorLine >= LocInfo->getNumOperands())
60 ErrorLine = 0;
Jim Grosbach098f5a22011-09-21 21:36:53 +000061
Chris Lattner79ffdc72010-11-17 08:20:42 +000062 if (LocInfo->getNumOperands() != 0)
63 if (const ConstantInt *CI =
64 dyn_cast<ConstantInt>(LocInfo->getOperand(ErrorLine)))
Chris Lattner300fa452010-11-17 08:03:32 +000065 LocCookie = CI->getZExtValue();
Chris Lattner79ffdc72010-11-17 08:20:42 +000066 }
Jim Grosbach098f5a22011-09-21 21:36:53 +000067
Chris Lattnerb0e36082010-11-17 08:13:01 +000068 DiagInfo->DiagHandler(Diag, DiagInfo->DiagContext, LocCookie);
Chris Lattner300fa452010-11-17 08:03:32 +000069}
70
Chris Lattner1e158692010-04-04 18:34:07 +000071/// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
Chad Rosierf24ae7b2012-09-05 23:57:37 +000072void AsmPrinter::EmitInlineAsm(StringRef Str, const MDNode *LocMDNode,
73 InlineAsm::AsmDialect Dialect) const {
Chris Lattner1e158692010-04-04 18:34:07 +000074 assert(!Str.empty() && "Can't emit empty inline asm block");
Jim Grosbach00915352010-10-01 23:29:12 +000075
Chris Lattner8900ef12010-04-05 23:11:24 +000076 // Remember if the buffer is nul terminated or not so we can avoid a copy.
77 bool isNullTerminated = Str.back() == 0;
78 if (isNullTerminated)
79 Str = Str.substr(0, Str.size()-1);
Jim Grosbach00915352010-10-01 23:29:12 +000080
Chris Lattner1e158692010-04-04 18:34:07 +000081 // If the output streamer is actually a .s file, just emit the blob textually.
82 // This is useful in case the asm parser doesn't handle something but the
83 // system assembler does.
84 if (OutStreamer.hasRawTextSupport()) {
85 OutStreamer.EmitRawText(Str);
86 return;
87 }
Jim Grosbach00915352010-10-01 23:29:12 +000088
Chris Lattner8900ef12010-04-05 23:11:24 +000089 SourceMgr SrcMgr;
Chris Lattner300fa452010-11-17 08:03:32 +000090 SrcMgrDiagInfo DiagInfo;
Jim Grosbach00915352010-10-01 23:29:12 +000091
Bob Wilsona594fab2013-02-11 05:37:07 +000092 // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
Chris Lattner59126b22010-04-06 00:55:39 +000093 LLVMContext &LLVMCtx = MMI->getModule()->getContext();
94 bool HasDiagHandler = false;
Bob Wilsona594fab2013-02-11 05:37:07 +000095 if (LLVMCtx.getInlineAsmDiagnosticHandler() != 0) {
Chad Rosierb759ede2012-09-07 18:16:38 +000096 // If the source manager has an issue, we arrange for srcMgrDiagHandler
Chris Lattner300fa452010-11-17 08:03:32 +000097 // to be invoked, getting DiagInfo passed into it.
98 DiagInfo.LocInfo = LocMDNode;
Bob Wilsona594fab2013-02-11 05:37:07 +000099 DiagInfo.DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
100 DiagInfo.DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
Chad Rosierb759ede2012-09-07 18:16:38 +0000101 SrcMgr.setDiagHandler(srcMgrDiagHandler, &DiagInfo);
Chris Lattner59126b22010-04-06 00:55:39 +0000102 HasDiagHandler = true;
103 }
Jim Grosbach00915352010-10-01 23:29:12 +0000104
Chris Lattner8900ef12010-04-05 23:11:24 +0000105 MemoryBuffer *Buffer;
106 if (isNullTerminated)
107 Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
108 else
109 Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
110
111 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
112 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach00915352010-10-01 23:29:12 +0000113
Jim Grosbach345768c2011-08-16 18:33:49 +0000114 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000115 OutContext, OutStreamer,
116 *MAI));
Evan Cheng4d1ca962011-07-08 01:53:10 +0000117
Evan Cheng91111d22011-07-09 05:47:46 +0000118 // FIXME: It would be nice if we can avoid createing a new instance of
119 // MCSubtargetInfo here given TargetSubtargetInfo is available. However,
120 // we have to watch out for asm directives which can change subtarget
121 // state. e.g. .code 16, .code 32.
122 OwningPtr<MCSubtargetInfo>
123 STI(TM.getTarget().createMCSubtargetInfo(TM.getTargetTriple(),
124 TM.getTargetCPU(),
125 TM.getTargetFeatureString()));
Evan Cheng11424442011-07-26 00:24:13 +0000126 OwningPtr<MCTargetAsmParser>
Joey Gouly0e76fa72013-09-12 10:28:05 +0000127 TAP(TM.getTarget().createMCAsmParser(*STI, *Parser, *MII));
Chris Lattner8900ef12010-04-05 23:11:24 +0000128 if (!TAP)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000129 report_fatal_error("Inline asm not supported by this streamer because"
Benjamin Kramera6769262010-04-08 10:44:28 +0000130 " we don't have an asm parser for this target\n");
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000131 Parser->setAssemblerDialect(Dialect);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000132 Parser->setTargetParser(*TAP.get());
Chris Lattner8900ef12010-04-05 23:11:24 +0000133
134 // Don't implicitly switch to the text section before the asm.
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000135 int Res = Parser->Run(/*NoInitialTextSection*/ true,
136 /*NoFinalize*/ true);
Chris Lattner59126b22010-04-06 00:55:39 +0000137 if (Res && !HasDiagHandler)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000138 report_fatal_error("Error parsing inline asm\n");
Chris Lattner1e158692010-04-04 18:34:07 +0000139}
140
Chad Rosier17788312012-09-11 19:09:56 +0000141static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
142 MachineModuleInfo *MMI, int InlineAsmVariant,
143 AsmPrinter *AP, unsigned LocCookie,
144 raw_ostream &OS) {
145 // Switch to the inline assembly variant.
146 OS << "\t.intel_syntax\n\t";
Chris Lattner1e158692010-04-04 18:34:07 +0000147
Chad Rosier17788312012-09-11 19:09:56 +0000148 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner1e158692010-04-04 18:34:07 +0000149 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach00915352010-10-01 23:29:12 +0000150
Chad Rosier17788312012-09-11 19:09:56 +0000151 while (*LastEmitted) {
152 switch (*LastEmitted) {
153 default: {
154 // Not a special case, emit the string section literally.
155 const char *LiteralEnd = LastEmitted+1;
156 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
157 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
158 ++LiteralEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000159
Chad Rosier17788312012-09-11 19:09:56 +0000160 OS.write(LastEmitted, LiteralEnd-LastEmitted);
161 LastEmitted = LiteralEnd;
162 break;
163 }
164 case '\n':
165 ++LastEmitted; // Consume newline character.
166 OS << '\n'; // Indent code with newline.
167 break;
168 case '$': {
169 ++LastEmitted; // Consume '$' character.
170 bool Done = true;
Chris Lattner1e158692010-04-04 18:34:07 +0000171
Chad Rosier17788312012-09-11 19:09:56 +0000172 // Handle escapes.
173 switch (*LastEmitted) {
174 default: Done = false; break;
175 case '$':
176 ++LastEmitted; // Consume second '$' character.
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000177 break;
178 }
Chad Rosier17788312012-09-11 19:09:56 +0000179 if (Done) break;
180
181 const char *IDStart = LastEmitted;
182 const char *IDEnd = IDStart;
183 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
184
185 unsigned Val;
186 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
187 report_fatal_error("Bad $ operand number in inline asm string: '" +
188 Twine(AsmStr) + "'");
189 LastEmitted = IDEnd;
190
191 if (Val >= NumOperands-1)
192 report_fatal_error("Invalid $ operand number in inline asm string: '" +
193 Twine(AsmStr) + "'");
194
195 // Okay, we finally have a value number. Ask the target to print this
196 // operand!
197 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
198
199 bool Error = false;
200
201 // Scan to find the machine operand number for the operand.
202 for (; Val; --Val) {
203 if (OpNo >= MI->getNumOperands()) break;
204 unsigned OpFlags = MI->getOperand(OpNo).getImm();
205 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
206 }
207
208 // We may have a location metadata attached to the end of the
209 // instruction, and at no point should see metadata at any
210 // other point while processing. It's an error if so.
211 if (OpNo >= MI->getNumOperands() ||
212 MI->getOperand(OpNo).isMetadata()) {
213 Error = true;
214 } else {
215 unsigned OpFlags = MI->getOperand(OpNo).getImm();
216 ++OpNo; // Skip over the ID number.
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000217
Chad Rosier17788312012-09-11 19:09:56 +0000218 if (InlineAsm::isMemKind(OpFlags)) {
219 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
220 /*Modifier*/ 0, OS);
221 } else {
222 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
223 /*Modifier*/ 0, OS);
224 }
225 }
226 if (Error) {
227 std::string msg;
228 raw_string_ostream Msg(msg);
229 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
230 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
231 }
232 break;
233 }
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000234 }
Chris Lattner51065562010-04-07 05:38:05 +0000235 }
Chad Rosier17788312012-09-11 19:09:56 +0000236 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
237}
Jim Grosbach00915352010-10-01 23:29:12 +0000238
Chad Rosier17788312012-09-11 19:09:56 +0000239static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
240 MachineModuleInfo *MMI, int InlineAsmVariant,
241 int AsmPrinterVariant, AsmPrinter *AP,
242 unsigned LocCookie, raw_ostream &OS) {
Chris Lattner1e158692010-04-04 18:34:07 +0000243 int CurVariant = -1; // The number of the {.|.|.} region we are in.
244 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chad Rosier17788312012-09-11 19:09:56 +0000245 unsigned NumOperands = MI->getNumOperands();
246
247 OS << '\t';
Jim Grosbach00915352010-10-01 23:29:12 +0000248
Chris Lattner1e158692010-04-04 18:34:07 +0000249 while (*LastEmitted) {
250 switch (*LastEmitted) {
251 default: {
252 // Not a special case, emit the string section literally.
253 const char *LiteralEnd = LastEmitted+1;
254 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
255 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
256 ++LiteralEnd;
257 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
258 OS.write(LastEmitted, LiteralEnd-LastEmitted);
259 LastEmitted = LiteralEnd;
260 break;
261 }
262 case '\n':
263 ++LastEmitted; // Consume newline character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000264 OS << '\n'; // Indent code with newline.
Chris Lattner1e158692010-04-04 18:34:07 +0000265 break;
266 case '$': {
267 ++LastEmitted; // Consume '$' character.
268 bool Done = true;
269
270 // Handle escapes.
271 switch (*LastEmitted) {
272 default: Done = false; break;
273 case '$': // $$ -> $
274 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
275 OS << '$';
276 ++LastEmitted; // Consume second '$' character.
277 break;
278 case '(': // $( -> same as GCC's { character.
279 ++LastEmitted; // Consume '(' character.
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000280 if (CurVariant != -1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000281 report_fatal_error("Nested variants found in inline asm string: '" +
282 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000283 CurVariant = 0; // We're in the first variant now.
284 break;
285 case '|':
286 ++LastEmitted; // consume '|' character.
287 if (CurVariant == -1)
288 OS << '|'; // this is gcc's behavior for | outside a variant
289 else
290 ++CurVariant; // We're in the next variant.
291 break;
292 case ')': // $) -> same as GCC's } char.
293 ++LastEmitted; // consume ')' character.
294 if (CurVariant == -1)
295 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach00915352010-10-01 23:29:12 +0000296 else
Chris Lattner1e158692010-04-04 18:34:07 +0000297 CurVariant = -1;
298 break;
299 }
300 if (Done) break;
Jim Grosbach00915352010-10-01 23:29:12 +0000301
Chris Lattner1e158692010-04-04 18:34:07 +0000302 bool HasCurlyBraces = false;
303 if (*LastEmitted == '{') { // ${variable}
304 ++LastEmitted; // Consume '{' character.
305 HasCurlyBraces = true;
306 }
Jim Grosbach00915352010-10-01 23:29:12 +0000307
Chris Lattner1e158692010-04-04 18:34:07 +0000308 // If we have ${:foo}, then this is not a real operand reference, it is a
309 // "magic" string reference, just like in .td files. Arrange to call
310 // PrintSpecial.
311 if (HasCurlyBraces && *LastEmitted == ':') {
312 ++LastEmitted;
313 const char *StrStart = LastEmitted;
314 const char *StrEnd = strchr(StrStart, '}');
315 if (StrEnd == 0)
Benjamin Kramera6769262010-04-08 10:44:28 +0000316 report_fatal_error("Unterminated ${:foo} operand in inline asm"
317 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000318
Chris Lattner1e158692010-04-04 18:34:07 +0000319 std::string Val(StrStart, StrEnd);
Chad Rosier17788312012-09-11 19:09:56 +0000320 AP->PrintSpecial(MI, OS, Val.c_str());
Chris Lattner1e158692010-04-04 18:34:07 +0000321 LastEmitted = StrEnd+1;
322 break;
323 }
Jim Grosbach00915352010-10-01 23:29:12 +0000324
Chris Lattner1e158692010-04-04 18:34:07 +0000325 const char *IDStart = LastEmitted;
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000326 const char *IDEnd = IDStart;
Jim Grosbach00915352010-10-01 23:29:12 +0000327 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
328
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000329 unsigned Val;
330 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramera6769262010-04-08 10:44:28 +0000331 report_fatal_error("Bad $ operand number in inline asm string: '" +
332 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000333 LastEmitted = IDEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000334
Chris Lattner1e158692010-04-04 18:34:07 +0000335 char Modifier[2] = { 0, 0 };
Jim Grosbach00915352010-10-01 23:29:12 +0000336
Chris Lattner1e158692010-04-04 18:34:07 +0000337 if (HasCurlyBraces) {
338 // If we have curly braces, check for a modifier character. This
339 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
340 if (*LastEmitted == ':') {
341 ++LastEmitted; // Consume ':' character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000342 if (*LastEmitted == 0)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000343 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramera6769262010-04-08 10:44:28 +0000344 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000345
Chris Lattner1e158692010-04-04 18:34:07 +0000346 Modifier[0] = *LastEmitted;
347 ++LastEmitted; // Consume modifier character.
348 }
Jim Grosbach00915352010-10-01 23:29:12 +0000349
Chris Lattner0e45d242010-04-05 22:42:30 +0000350 if (*LastEmitted != '}')
Benjamin Kramera6769262010-04-08 10:44:28 +0000351 report_fatal_error("Bad ${} expression in inline asm string: '" +
352 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000353 ++LastEmitted; // Consume '}' character.
354 }
Jim Grosbach00915352010-10-01 23:29:12 +0000355
Chris Lattner0e45d242010-04-05 22:42:30 +0000356 if (Val >= NumOperands-1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000357 report_fatal_error("Invalid $ operand number in inline asm string: '" +
358 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000359
Chris Lattner1e158692010-04-04 18:34:07 +0000360 // Okay, we finally have a value number. Ask the target to print this
361 // operand!
362 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Cheng6eb516d2011-01-07 23:50:32 +0000363 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner1e158692010-04-04 18:34:07 +0000364
365 bool Error = false;
366
367 // Scan to find the machine operand number for the operand.
368 for (; Val; --Val) {
369 if (OpNo >= MI->getNumOperands()) break;
370 unsigned OpFlags = MI->getOperand(OpNo).getImm();
371 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
372 }
373
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000374 // We may have a location metadata attached to the end of the
375 // instruction, and at no point should see metadata at any
376 // other point while processing. It's an error if so.
Eric Christopher12da1692012-03-22 01:33:51 +0000377 if (OpNo >= MI->getNumOperands() ||
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000378 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner1e158692010-04-04 18:34:07 +0000379 Error = true;
380 } else {
381 unsigned OpFlags = MI->getOperand(OpNo).getImm();
382 ++OpNo; // Skip over the ID number.
383
384 if (Modifier[0] == 'l') // labels are target independent
385 // FIXME: What if the operand isn't an MBB, report error?
386 OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
387 else {
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000388 if (InlineAsm::isMemKind(OpFlags)) {
Chad Rosierdb20a412012-09-10 21:10:49 +0000389 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Chris Lattner1e158692010-04-04 18:34:07 +0000390 Modifier[0] ? Modifier : 0,
391 OS);
392 } else {
Chad Rosierdb20a412012-09-10 21:10:49 +0000393 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Chris Lattner1e158692010-04-04 18:34:07 +0000394 Modifier[0] ? Modifier : 0, OS);
395 }
396 }
397 }
398 if (Error) {
399 std::string msg;
400 raw_string_ostream Msg(msg);
Chris Lattner1e457892010-04-07 23:40:44 +0000401 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
402 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner1e158692010-04-04 18:34:07 +0000403 }
404 }
405 break;
406 }
407 }
408 }
Chad Rosier17788312012-09-11 19:09:56 +0000409 OS << '\n' << (char)0; // null terminate string.
410}
411
412/// EmitInlineAsm - This method formats and emits the specified machine
413/// instruction that is an inline asm.
414void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
415 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
416
417 // Count the number of register definitions to find the asm string.
418 unsigned NumDefs = 0;
419 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
420 ++NumDefs)
421 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
422
423 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
424
425 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
426 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
427
428 // If this asmstr is empty, just print the #APP/#NOAPP markers.
429 // These are useful to see where empty asm's wound up.
430 if (AsmStr[0] == 0) {
Rafael Espindola0b694812014-01-16 16:28:37 +0000431 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
432 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chad Rosier17788312012-09-11 19:09:56 +0000433 return;
Chad Rosier7641f582012-09-10 21:36:05 +0000434 }
435
Chad Rosier17788312012-09-11 19:09:56 +0000436 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000437 // enabled, so we use emitRawComment.
438 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
Chad Rosier17788312012-09-11 19:09:56 +0000439
440 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
441 // it.
442 unsigned LocCookie = 0;
443 const MDNode *LocMD = 0;
444 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
445 if (MI->getOperand(i-1).isMetadata() &&
446 (LocMD = MI->getOperand(i-1).getMetadata()) &&
447 LocMD->getNumOperands() != 0) {
448 if (const ConstantInt *CI = dyn_cast<ConstantInt>(LocMD->getOperand(0))) {
449 LocCookie = CI->getZExtValue();
450 break;
451 }
452 }
453 }
454
455 // Emit the inline asm to a temporary string so we can emit it through
456 // EmitInlineAsm.
457 SmallString<256> StringData;
458 raw_svector_ostream OS(StringData);
459
460 // The variant of the current asmprinter.
461 int AsmPrinterVariant = MAI->getAssemblerDialect();
462 InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
463 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
464 if (InlineAsmVariant == InlineAsm::AD_ATT)
465 EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
466 AP, LocCookie, OS);
467 else
468 EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
469
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000470 EmitInlineAsm(OS.str(), LocMD, MI->getInlineAsmDialect());
Jim Grosbach00915352010-10-01 23:29:12 +0000471
Chris Lattner1e158692010-04-04 18:34:07 +0000472 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000473 // enabled, so we use emitRawComment.
474 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chris Lattner1e158692010-04-04 18:34:07 +0000475}
476
477
478/// PrintSpecial - Print information related to the specified machine instr
479/// that is independent of the operand, and may be independent of the instr
480/// itself. This can be useful for portably encoding the comment character
481/// or other bits of target-specific knowledge into the asmstrings. The
482/// syntax used is ${:comment}. Targets can override this to add support
483/// for their own strange codes.
484void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
485 const char *Code) const {
Rafael Espindola58873562014-01-03 19:21:54 +0000486 const DataLayout *DL = TM.getDataLayout();
Chris Lattner1e158692010-04-04 18:34:07 +0000487 if (!strcmp(Code, "private")) {
Rafael Espindola58873562014-01-03 19:21:54 +0000488 OS << DL->getPrivateGlobalPrefix();
Chris Lattner1e158692010-04-04 18:34:07 +0000489 } else if (!strcmp(Code, "comment")) {
490 OS << MAI->getCommentString();
491 } else if (!strcmp(Code, "uid")) {
492 // Comparing the address of MI isn't sufficient, because machineinstrs may
493 // be allocated to the same address across functions.
Jim Grosbach00915352010-10-01 23:29:12 +0000494
Chris Lattner1e158692010-04-04 18:34:07 +0000495 // If this is a new LastFn instruction, bump the counter.
496 if (LastMI != MI || LastFn != getFunctionNumber()) {
497 ++Counter;
498 LastMI = MI;
499 LastFn = getFunctionNumber();
500 }
501 OS << Counter;
502 } else {
503 std::string msg;
504 raw_string_ostream Msg(msg);
505 Msg << "Unknown special formatter '" << Code
506 << "' for machine instr: " << *MI;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000507 report_fatal_error(Msg.str());
Jim Grosbach00915352010-10-01 23:29:12 +0000508 }
Chris Lattner1e158692010-04-04 18:34:07 +0000509}
510
511/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
512/// instruction, using the specified assembler variant. Targets should
513/// override this to format as appropriate.
514bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chad Rosier1f57bcb2012-09-07 20:23:29 +0000515 unsigned AsmVariant, const char *ExtraCode,
516 raw_ostream &O) {
Jack Carterb2fd5f62012-06-21 17:14:46 +0000517 // Does this asm operand have a single letter operand modifier?
518 if (ExtraCode && ExtraCode[0]) {
519 if (ExtraCode[1] != 0) return true; // Unknown modifier.
520
521 const MachineOperand &MO = MI->getOperand(OpNo);
522 switch (ExtraCode[0]) {
523 default:
524 return true; // Unknown modifier.
525 case 'c': // Substitute immediate value without immediate syntax
Jack Carterc457f622012-06-21 21:37:54 +0000526 if (MO.getType() != MachineOperand::MO_Immediate)
Jack Carterb2fd5f62012-06-21 17:14:46 +0000527 return true;
528 O << MO.getImm();
529 return false;
Jack Carterc457f622012-06-21 21:37:54 +0000530 case 'n': // Negate the immediate constant.
531 if (MO.getType() != MachineOperand::MO_Immediate)
532 return true;
533 O << -MO.getImm();
534 return false;
Jack Carterb2fd5f62012-06-21 17:14:46 +0000535 }
536 }
Chris Lattner1e158692010-04-04 18:34:07 +0000537 return true;
538}
539
540bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
541 unsigned AsmVariant,
542 const char *ExtraCode, raw_ostream &O) {
543 // Target doesn't support this yet!
544 return true;
545}
546