blob: d679bff4cb59ec5c9b3952659519cbf261226d2f [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"
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +000037#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner1e158692010-04-04 18:34:07 +000038using namespace llvm;
39
Chris Lattner300fa452010-11-17 08:03:32 +000040namespace {
41 struct SrcMgrDiagInfo {
42 const MDNode *LocInfo;
Bob Wilsona594fab2013-02-11 05:37:07 +000043 LLVMContext::InlineAsmDiagHandlerTy DiagHandler;
Chris Lattner300fa452010-11-17 08:03:32 +000044 void *DiagContext;
45 };
46}
47
Chad Rosierb759ede2012-09-07 18:16:38 +000048/// srcMgrDiagHandler - This callback is invoked when the SourceMgr for an
Chris Lattner300fa452010-11-17 08:03:32 +000049/// inline asm has an error in it. diagInfo is a pointer to the SrcMgrDiagInfo
50/// struct above.
Chad Rosierb759ede2012-09-07 18:16:38 +000051static void srcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
Chris Lattner300fa452010-11-17 08:03:32 +000052 SrcMgrDiagInfo *DiagInfo = static_cast<SrcMgrDiagInfo *>(diagInfo);
53 assert(DiagInfo && "Diagnostic context not passed down?");
Jim Grosbach098f5a22011-09-21 21:36:53 +000054
Chris Lattner79ffdc72010-11-17 08:20:42 +000055 // If the inline asm had metadata associated with it, pull out a location
56 // cookie corresponding to which line the error occurred on.
Chris Lattner300fa452010-11-17 08:03:32 +000057 unsigned LocCookie = 0;
Chris Lattner79ffdc72010-11-17 08:20:42 +000058 if (const MDNode *LocInfo = DiagInfo->LocInfo) {
59 unsigned ErrorLine = Diag.getLineNo()-1;
60 if (ErrorLine >= LocInfo->getNumOperands())
61 ErrorLine = 0;
Jim Grosbach098f5a22011-09-21 21:36:53 +000062
Chris Lattner79ffdc72010-11-17 08:20:42 +000063 if (LocInfo->getNumOperands() != 0)
64 if (const ConstantInt *CI =
65 dyn_cast<ConstantInt>(LocInfo->getOperand(ErrorLine)))
Chris Lattner300fa452010-11-17 08:03:32 +000066 LocCookie = CI->getZExtValue();
Chris Lattner79ffdc72010-11-17 08:20:42 +000067 }
Jim Grosbach098f5a22011-09-21 21:36:53 +000068
Chris Lattnerb0e36082010-11-17 08:13:01 +000069 DiagInfo->DiagHandler(Diag, DiagInfo->DiagContext, LocCookie);
Chris Lattner300fa452010-11-17 08:03:32 +000070}
71
Chris Lattner1e158692010-04-04 18:34:07 +000072/// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
Chad Rosierf24ae7b2012-09-05 23:57:37 +000073void AsmPrinter::EmitInlineAsm(StringRef Str, const MDNode *LocMDNode,
74 InlineAsm::AsmDialect Dialect) const {
Chris Lattner1e158692010-04-04 18:34:07 +000075 assert(!Str.empty() && "Can't emit empty inline asm block");
Jim Grosbach00915352010-10-01 23:29:12 +000076
Chris Lattner8900ef12010-04-05 23:11:24 +000077 // Remember if the buffer is nul terminated or not so we can avoid a copy.
78 bool isNullTerminated = Str.back() == 0;
79 if (isNullTerminated)
80 Str = Str.substr(0, Str.size()-1);
Jim Grosbach00915352010-10-01 23:29:12 +000081
Chris Lattner1e158692010-04-04 18:34:07 +000082 // If the output streamer is actually a .s file, just emit the blob textually.
83 // This is useful in case the asm parser doesn't handle something but the
84 // system assembler does.
85 if (OutStreamer.hasRawTextSupport()) {
86 OutStreamer.EmitRawText(Str);
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +000087 OutStreamer.EmitInlineAsmEnd(TM.getSubtarget<MCSubtargetInfo>(), 0);
Chris Lattner1e158692010-04-04 18:34:07 +000088 return;
89 }
Jim Grosbach00915352010-10-01 23:29:12 +000090
Chris Lattner8900ef12010-04-05 23:11:24 +000091 SourceMgr SrcMgr;
Chris Lattner300fa452010-11-17 08:03:32 +000092 SrcMgrDiagInfo DiagInfo;
Jim Grosbach00915352010-10-01 23:29:12 +000093
Bob Wilsona594fab2013-02-11 05:37:07 +000094 // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
Chris Lattner59126b22010-04-06 00:55:39 +000095 LLVMContext &LLVMCtx = MMI->getModule()->getContext();
96 bool HasDiagHandler = false;
Bob Wilsona594fab2013-02-11 05:37:07 +000097 if (LLVMCtx.getInlineAsmDiagnosticHandler() != 0) {
Chad Rosierb759ede2012-09-07 18:16:38 +000098 // If the source manager has an issue, we arrange for srcMgrDiagHandler
Chris Lattner300fa452010-11-17 08:03:32 +000099 // to be invoked, getting DiagInfo passed into it.
100 DiagInfo.LocInfo = LocMDNode;
Bob Wilsona594fab2013-02-11 05:37:07 +0000101 DiagInfo.DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
102 DiagInfo.DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
Chad Rosierb759ede2012-09-07 18:16:38 +0000103 SrcMgr.setDiagHandler(srcMgrDiagHandler, &DiagInfo);
Chris Lattner59126b22010-04-06 00:55:39 +0000104 HasDiagHandler = true;
105 }
Jim Grosbach00915352010-10-01 23:29:12 +0000106
Chris Lattner8900ef12010-04-05 23:11:24 +0000107 MemoryBuffer *Buffer;
108 if (isNullTerminated)
109 Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
110 else
111 Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
112
113 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
114 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach00915352010-10-01 23:29:12 +0000115
Jim Grosbach345768c2011-08-16 18:33:49 +0000116 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000117 OutContext, OutStreamer,
118 *MAI));
Evan Cheng4d1ca962011-07-08 01:53:10 +0000119
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000120 // Reuse the existing Subtarget, because the AsmParser may need to
121 // modify it. For example, when switching between ARM and
122 // Thumb mode.
123 MCSubtargetInfo* STI =
124 const_cast<MCSubtargetInfo*>(&TM.getSubtarget<MCSubtargetInfo>());
125
126 // Preserve a copy of the original STI because the parser may modify it.
127 // The target can restore the original state in EmitInlineAsmEnd().
128 MCSubtargetInfo STIOrig = *STI;
129
Evan Cheng11424442011-07-26 00:24:13 +0000130 OwningPtr<MCTargetAsmParser>
Joey Gouly0e76fa72013-09-12 10:28:05 +0000131 TAP(TM.getTarget().createMCAsmParser(*STI, *Parser, *MII));
Chris Lattner8900ef12010-04-05 23:11:24 +0000132 if (!TAP)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000133 report_fatal_error("Inline asm not supported by this streamer because"
Benjamin Kramera6769262010-04-08 10:44:28 +0000134 " we don't have an asm parser for this target\n");
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000135 Parser->setAssemblerDialect(Dialect);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000136 Parser->setTargetParser(*TAP.get());
Chris Lattner8900ef12010-04-05 23:11:24 +0000137
138 // Don't implicitly switch to the text section before the asm.
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000139 int Res = Parser->Run(/*NoInitialTextSection*/ true,
140 /*NoFinalize*/ true);
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000141 OutStreamer.EmitInlineAsmEnd(STIOrig, STI);
Chris Lattner59126b22010-04-06 00:55:39 +0000142 if (Res && !HasDiagHandler)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000143 report_fatal_error("Error parsing inline asm\n");
Chris Lattner1e158692010-04-04 18:34:07 +0000144}
145
Chad Rosier17788312012-09-11 19:09:56 +0000146static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
147 MachineModuleInfo *MMI, int InlineAsmVariant,
148 AsmPrinter *AP, unsigned LocCookie,
149 raw_ostream &OS) {
150 // Switch to the inline assembly variant.
151 OS << "\t.intel_syntax\n\t";
Chris Lattner1e158692010-04-04 18:34:07 +0000152
Chad Rosier17788312012-09-11 19:09:56 +0000153 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner1e158692010-04-04 18:34:07 +0000154 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach00915352010-10-01 23:29:12 +0000155
Chad Rosier17788312012-09-11 19:09:56 +0000156 while (*LastEmitted) {
157 switch (*LastEmitted) {
158 default: {
159 // Not a special case, emit the string section literally.
160 const char *LiteralEnd = LastEmitted+1;
161 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
162 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
163 ++LiteralEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000164
Chad Rosier17788312012-09-11 19:09:56 +0000165 OS.write(LastEmitted, LiteralEnd-LastEmitted);
166 LastEmitted = LiteralEnd;
167 break;
168 }
169 case '\n':
170 ++LastEmitted; // Consume newline character.
171 OS << '\n'; // Indent code with newline.
172 break;
173 case '$': {
174 ++LastEmitted; // Consume '$' character.
175 bool Done = true;
Chris Lattner1e158692010-04-04 18:34:07 +0000176
Chad Rosier17788312012-09-11 19:09:56 +0000177 // Handle escapes.
178 switch (*LastEmitted) {
179 default: Done = false; break;
180 case '$':
181 ++LastEmitted; // Consume second '$' character.
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000182 break;
183 }
Chad Rosier17788312012-09-11 19:09:56 +0000184 if (Done) break;
185
186 const char *IDStart = LastEmitted;
187 const char *IDEnd = IDStart;
188 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
189
190 unsigned Val;
191 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
192 report_fatal_error("Bad $ operand number in inline asm string: '" +
193 Twine(AsmStr) + "'");
194 LastEmitted = IDEnd;
195
196 if (Val >= NumOperands-1)
197 report_fatal_error("Invalid $ operand number in inline asm string: '" +
198 Twine(AsmStr) + "'");
199
200 // Okay, we finally have a value number. Ask the target to print this
201 // operand!
202 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
203
204 bool Error = false;
205
206 // Scan to find the machine operand number for the operand.
207 for (; Val; --Val) {
208 if (OpNo >= MI->getNumOperands()) break;
209 unsigned OpFlags = MI->getOperand(OpNo).getImm();
210 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
211 }
212
213 // We may have a location metadata attached to the end of the
214 // instruction, and at no point should see metadata at any
215 // other point while processing. It's an error if so.
216 if (OpNo >= MI->getNumOperands() ||
217 MI->getOperand(OpNo).isMetadata()) {
218 Error = true;
219 } else {
220 unsigned OpFlags = MI->getOperand(OpNo).getImm();
221 ++OpNo; // Skip over the ID number.
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000222
Chad Rosier17788312012-09-11 19:09:56 +0000223 if (InlineAsm::isMemKind(OpFlags)) {
224 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
225 /*Modifier*/ 0, OS);
226 } else {
227 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
228 /*Modifier*/ 0, OS);
229 }
230 }
231 if (Error) {
232 std::string msg;
233 raw_string_ostream Msg(msg);
234 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
235 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
236 }
237 break;
238 }
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000239 }
Chris Lattner51065562010-04-07 05:38:05 +0000240 }
Chad Rosier17788312012-09-11 19:09:56 +0000241 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
242}
Jim Grosbach00915352010-10-01 23:29:12 +0000243
Chad Rosier17788312012-09-11 19:09:56 +0000244static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
245 MachineModuleInfo *MMI, int InlineAsmVariant,
246 int AsmPrinterVariant, AsmPrinter *AP,
247 unsigned LocCookie, raw_ostream &OS) {
Chris Lattner1e158692010-04-04 18:34:07 +0000248 int CurVariant = -1; // The number of the {.|.|.} region we are in.
249 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chad Rosier17788312012-09-11 19:09:56 +0000250 unsigned NumOperands = MI->getNumOperands();
251
252 OS << '\t';
Jim Grosbach00915352010-10-01 23:29:12 +0000253
Chris Lattner1e158692010-04-04 18:34:07 +0000254 while (*LastEmitted) {
255 switch (*LastEmitted) {
256 default: {
257 // Not a special case, emit the string section literally.
258 const char *LiteralEnd = LastEmitted+1;
259 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
260 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
261 ++LiteralEnd;
262 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
263 OS.write(LastEmitted, LiteralEnd-LastEmitted);
264 LastEmitted = LiteralEnd;
265 break;
266 }
267 case '\n':
268 ++LastEmitted; // Consume newline character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000269 OS << '\n'; // Indent code with newline.
Chris Lattner1e158692010-04-04 18:34:07 +0000270 break;
271 case '$': {
272 ++LastEmitted; // Consume '$' character.
273 bool Done = true;
274
275 // Handle escapes.
276 switch (*LastEmitted) {
277 default: Done = false; break;
278 case '$': // $$ -> $
279 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
280 OS << '$';
281 ++LastEmitted; // Consume second '$' character.
282 break;
283 case '(': // $( -> same as GCC's { character.
284 ++LastEmitted; // Consume '(' character.
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000285 if (CurVariant != -1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000286 report_fatal_error("Nested variants found in inline asm string: '" +
287 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000288 CurVariant = 0; // We're in the first variant now.
289 break;
290 case '|':
291 ++LastEmitted; // consume '|' character.
292 if (CurVariant == -1)
293 OS << '|'; // this is gcc's behavior for | outside a variant
294 else
295 ++CurVariant; // We're in the next variant.
296 break;
297 case ')': // $) -> same as GCC's } char.
298 ++LastEmitted; // consume ')' character.
299 if (CurVariant == -1)
300 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach00915352010-10-01 23:29:12 +0000301 else
Chris Lattner1e158692010-04-04 18:34:07 +0000302 CurVariant = -1;
303 break;
304 }
305 if (Done) break;
Jim Grosbach00915352010-10-01 23:29:12 +0000306
Chris Lattner1e158692010-04-04 18:34:07 +0000307 bool HasCurlyBraces = false;
308 if (*LastEmitted == '{') { // ${variable}
309 ++LastEmitted; // Consume '{' character.
310 HasCurlyBraces = true;
311 }
Jim Grosbach00915352010-10-01 23:29:12 +0000312
Chris Lattner1e158692010-04-04 18:34:07 +0000313 // If we have ${:foo}, then this is not a real operand reference, it is a
314 // "magic" string reference, just like in .td files. Arrange to call
315 // PrintSpecial.
316 if (HasCurlyBraces && *LastEmitted == ':') {
317 ++LastEmitted;
318 const char *StrStart = LastEmitted;
319 const char *StrEnd = strchr(StrStart, '}');
320 if (StrEnd == 0)
Benjamin Kramera6769262010-04-08 10:44:28 +0000321 report_fatal_error("Unterminated ${:foo} operand in inline asm"
322 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000323
Chris Lattner1e158692010-04-04 18:34:07 +0000324 std::string Val(StrStart, StrEnd);
Chad Rosier17788312012-09-11 19:09:56 +0000325 AP->PrintSpecial(MI, OS, Val.c_str());
Chris Lattner1e158692010-04-04 18:34:07 +0000326 LastEmitted = StrEnd+1;
327 break;
328 }
Jim Grosbach00915352010-10-01 23:29:12 +0000329
Chris Lattner1e158692010-04-04 18:34:07 +0000330 const char *IDStart = LastEmitted;
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000331 const char *IDEnd = IDStart;
Jim Grosbach00915352010-10-01 23:29:12 +0000332 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
333
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000334 unsigned Val;
335 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramera6769262010-04-08 10:44:28 +0000336 report_fatal_error("Bad $ operand number in inline asm string: '" +
337 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000338 LastEmitted = IDEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000339
Chris Lattner1e158692010-04-04 18:34:07 +0000340 char Modifier[2] = { 0, 0 };
Jim Grosbach00915352010-10-01 23:29:12 +0000341
Chris Lattner1e158692010-04-04 18:34:07 +0000342 if (HasCurlyBraces) {
343 // If we have curly braces, check for a modifier character. This
344 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
345 if (*LastEmitted == ':') {
346 ++LastEmitted; // Consume ':' character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000347 if (*LastEmitted == 0)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000348 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramera6769262010-04-08 10:44:28 +0000349 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000350
Chris Lattner1e158692010-04-04 18:34:07 +0000351 Modifier[0] = *LastEmitted;
352 ++LastEmitted; // Consume modifier character.
353 }
Jim Grosbach00915352010-10-01 23:29:12 +0000354
Chris Lattner0e45d242010-04-05 22:42:30 +0000355 if (*LastEmitted != '}')
Benjamin Kramera6769262010-04-08 10:44:28 +0000356 report_fatal_error("Bad ${} expression in inline asm string: '" +
357 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000358 ++LastEmitted; // Consume '}' character.
359 }
Jim Grosbach00915352010-10-01 23:29:12 +0000360
Chris Lattner0e45d242010-04-05 22:42:30 +0000361 if (Val >= NumOperands-1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000362 report_fatal_error("Invalid $ operand number in inline asm string: '" +
363 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000364
Chris Lattner1e158692010-04-04 18:34:07 +0000365 // Okay, we finally have a value number. Ask the target to print this
366 // operand!
367 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Cheng6eb516d2011-01-07 23:50:32 +0000368 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner1e158692010-04-04 18:34:07 +0000369
370 bool Error = false;
371
372 // Scan to find the machine operand number for the operand.
373 for (; Val; --Val) {
374 if (OpNo >= MI->getNumOperands()) break;
375 unsigned OpFlags = MI->getOperand(OpNo).getImm();
376 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
377 }
378
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000379 // We may have a location metadata attached to the end of the
380 // instruction, and at no point should see metadata at any
381 // other point while processing. It's an error if so.
Eric Christopher12da1692012-03-22 01:33:51 +0000382 if (OpNo >= MI->getNumOperands() ||
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000383 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner1e158692010-04-04 18:34:07 +0000384 Error = true;
385 } else {
386 unsigned OpFlags = MI->getOperand(OpNo).getImm();
387 ++OpNo; // Skip over the ID number.
388
389 if (Modifier[0] == 'l') // labels are target independent
390 // FIXME: What if the operand isn't an MBB, report error?
391 OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
392 else {
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000393 if (InlineAsm::isMemKind(OpFlags)) {
Chad Rosierdb20a412012-09-10 21:10:49 +0000394 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Chris Lattner1e158692010-04-04 18:34:07 +0000395 Modifier[0] ? Modifier : 0,
396 OS);
397 } else {
Chad Rosierdb20a412012-09-10 21:10:49 +0000398 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Chris Lattner1e158692010-04-04 18:34:07 +0000399 Modifier[0] ? Modifier : 0, OS);
400 }
401 }
402 }
403 if (Error) {
404 std::string msg;
405 raw_string_ostream Msg(msg);
Chris Lattner1e457892010-04-07 23:40:44 +0000406 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
407 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner1e158692010-04-04 18:34:07 +0000408 }
409 }
410 break;
411 }
412 }
413 }
Chad Rosier17788312012-09-11 19:09:56 +0000414 OS << '\n' << (char)0; // null terminate string.
415}
416
417/// EmitInlineAsm - This method formats and emits the specified machine
418/// instruction that is an inline asm.
419void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
420 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
421
422 // Count the number of register definitions to find the asm string.
423 unsigned NumDefs = 0;
424 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
425 ++NumDefs)
426 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
427
428 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
429
430 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
431 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
432
433 // If this asmstr is empty, just print the #APP/#NOAPP markers.
434 // These are useful to see where empty asm's wound up.
435 if (AsmStr[0] == 0) {
Rafael Espindola0b694812014-01-16 16:28:37 +0000436 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
437 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chad Rosier17788312012-09-11 19:09:56 +0000438 return;
Chad Rosier7641f582012-09-10 21:36:05 +0000439 }
440
Chad Rosier17788312012-09-11 19:09:56 +0000441 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000442 // enabled, so we use emitRawComment.
443 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
Chad Rosier17788312012-09-11 19:09:56 +0000444
445 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
446 // it.
447 unsigned LocCookie = 0;
448 const MDNode *LocMD = 0;
449 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
450 if (MI->getOperand(i-1).isMetadata() &&
451 (LocMD = MI->getOperand(i-1).getMetadata()) &&
452 LocMD->getNumOperands() != 0) {
453 if (const ConstantInt *CI = dyn_cast<ConstantInt>(LocMD->getOperand(0))) {
454 LocCookie = CI->getZExtValue();
455 break;
456 }
457 }
458 }
459
460 // Emit the inline asm to a temporary string so we can emit it through
461 // EmitInlineAsm.
462 SmallString<256> StringData;
463 raw_svector_ostream OS(StringData);
464
465 // The variant of the current asmprinter.
466 int AsmPrinterVariant = MAI->getAssemblerDialect();
467 InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
468 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
469 if (InlineAsmVariant == InlineAsm::AD_ATT)
470 EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
471 AP, LocCookie, OS);
472 else
473 EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
474
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000475 EmitInlineAsm(OS.str(), LocMD, MI->getInlineAsmDialect());
Jim Grosbach00915352010-10-01 23:29:12 +0000476
Chris Lattner1e158692010-04-04 18:34:07 +0000477 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000478 // enabled, so we use emitRawComment.
479 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chris Lattner1e158692010-04-04 18:34:07 +0000480}
481
482
483/// PrintSpecial - Print information related to the specified machine instr
484/// that is independent of the operand, and may be independent of the instr
485/// itself. This can be useful for portably encoding the comment character
486/// or other bits of target-specific knowledge into the asmstrings. The
487/// syntax used is ${:comment}. Targets can override this to add support
488/// for their own strange codes.
489void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
490 const char *Code) const {
Rafael Espindola58873562014-01-03 19:21:54 +0000491 const DataLayout *DL = TM.getDataLayout();
Chris Lattner1e158692010-04-04 18:34:07 +0000492 if (!strcmp(Code, "private")) {
Rafael Espindola58873562014-01-03 19:21:54 +0000493 OS << DL->getPrivateGlobalPrefix();
Chris Lattner1e158692010-04-04 18:34:07 +0000494 } else if (!strcmp(Code, "comment")) {
495 OS << MAI->getCommentString();
496 } else if (!strcmp(Code, "uid")) {
497 // Comparing the address of MI isn't sufficient, because machineinstrs may
498 // be allocated to the same address across functions.
Jim Grosbach00915352010-10-01 23:29:12 +0000499
Chris Lattner1e158692010-04-04 18:34:07 +0000500 // If this is a new LastFn instruction, bump the counter.
501 if (LastMI != MI || LastFn != getFunctionNumber()) {
502 ++Counter;
503 LastMI = MI;
504 LastFn = getFunctionNumber();
505 }
506 OS << Counter;
507 } else {
508 std::string msg;
509 raw_string_ostream Msg(msg);
510 Msg << "Unknown special formatter '" << Code
511 << "' for machine instr: " << *MI;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000512 report_fatal_error(Msg.str());
Jim Grosbach00915352010-10-01 23:29:12 +0000513 }
Chris Lattner1e158692010-04-04 18:34:07 +0000514}
515
516/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
517/// instruction, using the specified assembler variant. Targets should
518/// override this to format as appropriate.
519bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chad Rosier1f57bcb2012-09-07 20:23:29 +0000520 unsigned AsmVariant, const char *ExtraCode,
521 raw_ostream &O) {
Jack Carterb2fd5f62012-06-21 17:14:46 +0000522 // Does this asm operand have a single letter operand modifier?
523 if (ExtraCode && ExtraCode[0]) {
524 if (ExtraCode[1] != 0) return true; // Unknown modifier.
525
526 const MachineOperand &MO = MI->getOperand(OpNo);
527 switch (ExtraCode[0]) {
528 default:
529 return true; // Unknown modifier.
530 case 'c': // Substitute immediate value without immediate syntax
Jack Carterc457f622012-06-21 21:37:54 +0000531 if (MO.getType() != MachineOperand::MO_Immediate)
Jack Carterb2fd5f62012-06-21 17:14:46 +0000532 return true;
533 O << MO.getImm();
534 return false;
Jack Carterc457f622012-06-21 21:37:54 +0000535 case 'n': // Negate the immediate constant.
536 if (MO.getType() != MachineOperand::MO_Immediate)
537 return true;
538 O << -MO.getImm();
539 return false;
Jack Carterb2fd5f62012-06-21 17:14:46 +0000540 }
541 }
Chris Lattner1e158692010-04-04 18:34:07 +0000542 return true;
543}
544
545bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
546 unsigned AsmVariant,
547 const char *ExtraCode, raw_ostream &O) {
548 // Target doesn't support this yet!
549 return true;
550}
551