blob: 8badf429d2dd3d5fd67a7e4ebae2c738d41ad621 [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);
Rafael Espindola65fd0a82014-01-24 15:47:54 +000087 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
David Peixottoea2bcb92014-02-06 18:19:40 +0000120 // Initialize the parser with a fresh subtarget info. It is better to use a
121 // new STI here because the parser may modify it and we do not want those
122 // modifications to persist after parsing the inlineasm. The modifications
123 // made by the parser will be seen by the code emitters because it passes
124 // the current STI down to the EncodeInstruction() method.
125 OwningPtr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
126 TM.getTargetTriple(), TM.getTargetCPU(), TM.getTargetFeatureString()));
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000127
David Peixottoea2bcb92014-02-06 18:19:40 +0000128 // Preserve a copy of the original STI because the parser may modify it. For
129 // example, when switching between arm and thumb mode. If the target needs to
130 // emit code to return to the original state it can do so in emitInlineAsmEnd().
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000131 MCSubtargetInfo STIOrig = *STI;
132
Evan Cheng11424442011-07-26 00:24:13 +0000133 OwningPtr<MCTargetAsmParser>
Joey Gouly0e76fa72013-09-12 10:28:05 +0000134 TAP(TM.getTarget().createMCAsmParser(*STI, *Parser, *MII));
Chris Lattner8900ef12010-04-05 23:11:24 +0000135 if (!TAP)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000136 report_fatal_error("Inline asm not supported by this streamer because"
Benjamin Kramera6769262010-04-08 10:44:28 +0000137 " we don't have an asm parser for this target\n");
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000138 Parser->setAssemblerDialect(Dialect);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000139 Parser->setTargetParser(*TAP.get());
Chris Lattner8900ef12010-04-05 23:11:24 +0000140
141 // Don't implicitly switch to the text section before the asm.
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000142 int Res = Parser->Run(/*NoInitialTextSection*/ true,
143 /*NoFinalize*/ true);
David Peixottoea2bcb92014-02-06 18:19:40 +0000144 emitInlineAsmEnd(STIOrig, STI.get());
Chris Lattner59126b22010-04-06 00:55:39 +0000145 if (Res && !HasDiagHandler)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000146 report_fatal_error("Error parsing inline asm\n");
Chris Lattner1e158692010-04-04 18:34:07 +0000147}
148
Chad Rosier17788312012-09-11 19:09:56 +0000149static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
150 MachineModuleInfo *MMI, int InlineAsmVariant,
151 AsmPrinter *AP, unsigned LocCookie,
152 raw_ostream &OS) {
153 // Switch to the inline assembly variant.
154 OS << "\t.intel_syntax\n\t";
Chris Lattner1e158692010-04-04 18:34:07 +0000155
Chad Rosier17788312012-09-11 19:09:56 +0000156 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner1e158692010-04-04 18:34:07 +0000157 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach00915352010-10-01 23:29:12 +0000158
Chad Rosier17788312012-09-11 19:09:56 +0000159 while (*LastEmitted) {
160 switch (*LastEmitted) {
161 default: {
162 // Not a special case, emit the string section literally.
163 const char *LiteralEnd = LastEmitted+1;
164 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
165 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
166 ++LiteralEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000167
Chad Rosier17788312012-09-11 19:09:56 +0000168 OS.write(LastEmitted, LiteralEnd-LastEmitted);
169 LastEmitted = LiteralEnd;
170 break;
171 }
172 case '\n':
173 ++LastEmitted; // Consume newline character.
174 OS << '\n'; // Indent code with newline.
175 break;
176 case '$': {
177 ++LastEmitted; // Consume '$' character.
178 bool Done = true;
Chris Lattner1e158692010-04-04 18:34:07 +0000179
Chad Rosier17788312012-09-11 19:09:56 +0000180 // Handle escapes.
181 switch (*LastEmitted) {
182 default: Done = false; break;
183 case '$':
184 ++LastEmitted; // Consume second '$' character.
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000185 break;
186 }
Chad Rosier17788312012-09-11 19:09:56 +0000187 if (Done) break;
188
189 const char *IDStart = LastEmitted;
190 const char *IDEnd = IDStart;
191 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
192
193 unsigned Val;
194 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
195 report_fatal_error("Bad $ operand number in inline asm string: '" +
196 Twine(AsmStr) + "'");
197 LastEmitted = IDEnd;
198
199 if (Val >= NumOperands-1)
200 report_fatal_error("Invalid $ operand number in inline asm string: '" +
201 Twine(AsmStr) + "'");
202
203 // Okay, we finally have a value number. Ask the target to print this
204 // operand!
205 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
206
207 bool Error = false;
208
209 // Scan to find the machine operand number for the operand.
210 for (; Val; --Val) {
211 if (OpNo >= MI->getNumOperands()) break;
212 unsigned OpFlags = MI->getOperand(OpNo).getImm();
213 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
214 }
215
216 // We may have a location metadata attached to the end of the
217 // instruction, and at no point should see metadata at any
218 // other point while processing. It's an error if so.
219 if (OpNo >= MI->getNumOperands() ||
220 MI->getOperand(OpNo).isMetadata()) {
221 Error = true;
222 } else {
223 unsigned OpFlags = MI->getOperand(OpNo).getImm();
224 ++OpNo; // Skip over the ID number.
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000225
Chad Rosier17788312012-09-11 19:09:56 +0000226 if (InlineAsm::isMemKind(OpFlags)) {
227 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
228 /*Modifier*/ 0, OS);
229 } else {
230 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
231 /*Modifier*/ 0, OS);
232 }
233 }
234 if (Error) {
235 std::string msg;
236 raw_string_ostream Msg(msg);
237 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
238 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
239 }
240 break;
241 }
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000242 }
Chris Lattner51065562010-04-07 05:38:05 +0000243 }
Chad Rosier17788312012-09-11 19:09:56 +0000244 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
245}
Jim Grosbach00915352010-10-01 23:29:12 +0000246
Chad Rosier17788312012-09-11 19:09:56 +0000247static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
248 MachineModuleInfo *MMI, int InlineAsmVariant,
249 int AsmPrinterVariant, AsmPrinter *AP,
250 unsigned LocCookie, raw_ostream &OS) {
Chris Lattner1e158692010-04-04 18:34:07 +0000251 int CurVariant = -1; // The number of the {.|.|.} region we are in.
252 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chad Rosier17788312012-09-11 19:09:56 +0000253 unsigned NumOperands = MI->getNumOperands();
254
255 OS << '\t';
Jim Grosbach00915352010-10-01 23:29:12 +0000256
Chris Lattner1e158692010-04-04 18:34:07 +0000257 while (*LastEmitted) {
258 switch (*LastEmitted) {
259 default: {
260 // Not a special case, emit the string section literally.
261 const char *LiteralEnd = LastEmitted+1;
262 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
263 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
264 ++LiteralEnd;
265 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
266 OS.write(LastEmitted, LiteralEnd-LastEmitted);
267 LastEmitted = LiteralEnd;
268 break;
269 }
270 case '\n':
271 ++LastEmitted; // Consume newline character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000272 OS << '\n'; // Indent code with newline.
Chris Lattner1e158692010-04-04 18:34:07 +0000273 break;
274 case '$': {
275 ++LastEmitted; // Consume '$' character.
276 bool Done = true;
277
278 // Handle escapes.
279 switch (*LastEmitted) {
280 default: Done = false; break;
281 case '$': // $$ -> $
282 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
283 OS << '$';
284 ++LastEmitted; // Consume second '$' character.
285 break;
286 case '(': // $( -> same as GCC's { character.
287 ++LastEmitted; // Consume '(' character.
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000288 if (CurVariant != -1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000289 report_fatal_error("Nested variants found in inline asm string: '" +
290 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000291 CurVariant = 0; // We're in the first variant now.
292 break;
293 case '|':
294 ++LastEmitted; // consume '|' character.
295 if (CurVariant == -1)
296 OS << '|'; // this is gcc's behavior for | outside a variant
297 else
298 ++CurVariant; // We're in the next variant.
299 break;
300 case ')': // $) -> same as GCC's } char.
301 ++LastEmitted; // consume ')' character.
302 if (CurVariant == -1)
303 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach00915352010-10-01 23:29:12 +0000304 else
Chris Lattner1e158692010-04-04 18:34:07 +0000305 CurVariant = -1;
306 break;
307 }
308 if (Done) break;
Jim Grosbach00915352010-10-01 23:29:12 +0000309
Chris Lattner1e158692010-04-04 18:34:07 +0000310 bool HasCurlyBraces = false;
311 if (*LastEmitted == '{') { // ${variable}
312 ++LastEmitted; // Consume '{' character.
313 HasCurlyBraces = true;
314 }
Jim Grosbach00915352010-10-01 23:29:12 +0000315
Chris Lattner1e158692010-04-04 18:34:07 +0000316 // If we have ${:foo}, then this is not a real operand reference, it is a
317 // "magic" string reference, just like in .td files. Arrange to call
318 // PrintSpecial.
319 if (HasCurlyBraces && *LastEmitted == ':') {
320 ++LastEmitted;
321 const char *StrStart = LastEmitted;
322 const char *StrEnd = strchr(StrStart, '}');
323 if (StrEnd == 0)
Benjamin Kramera6769262010-04-08 10:44:28 +0000324 report_fatal_error("Unterminated ${:foo} operand in inline asm"
325 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000326
Chris Lattner1e158692010-04-04 18:34:07 +0000327 std::string Val(StrStart, StrEnd);
Chad Rosier17788312012-09-11 19:09:56 +0000328 AP->PrintSpecial(MI, OS, Val.c_str());
Chris Lattner1e158692010-04-04 18:34:07 +0000329 LastEmitted = StrEnd+1;
330 break;
331 }
Jim Grosbach00915352010-10-01 23:29:12 +0000332
Chris Lattner1e158692010-04-04 18:34:07 +0000333 const char *IDStart = LastEmitted;
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000334 const char *IDEnd = IDStart;
Jim Grosbach00915352010-10-01 23:29:12 +0000335 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
336
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000337 unsigned Val;
338 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramera6769262010-04-08 10:44:28 +0000339 report_fatal_error("Bad $ operand number in inline asm string: '" +
340 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000341 LastEmitted = IDEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000342
Chris Lattner1e158692010-04-04 18:34:07 +0000343 char Modifier[2] = { 0, 0 };
Jim Grosbach00915352010-10-01 23:29:12 +0000344
Chris Lattner1e158692010-04-04 18:34:07 +0000345 if (HasCurlyBraces) {
346 // If we have curly braces, check for a modifier character. This
347 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
348 if (*LastEmitted == ':') {
349 ++LastEmitted; // Consume ':' character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000350 if (*LastEmitted == 0)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000351 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramera6769262010-04-08 10:44:28 +0000352 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000353
Chris Lattner1e158692010-04-04 18:34:07 +0000354 Modifier[0] = *LastEmitted;
355 ++LastEmitted; // Consume modifier character.
356 }
Jim Grosbach00915352010-10-01 23:29:12 +0000357
Chris Lattner0e45d242010-04-05 22:42:30 +0000358 if (*LastEmitted != '}')
Benjamin Kramera6769262010-04-08 10:44:28 +0000359 report_fatal_error("Bad ${} expression in inline asm string: '" +
360 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000361 ++LastEmitted; // Consume '}' character.
362 }
Jim Grosbach00915352010-10-01 23:29:12 +0000363
Chris Lattner0e45d242010-04-05 22:42:30 +0000364 if (Val >= NumOperands-1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000365 report_fatal_error("Invalid $ operand number in inline asm string: '" +
366 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000367
Chris Lattner1e158692010-04-04 18:34:07 +0000368 // Okay, we finally have a value number. Ask the target to print this
369 // operand!
370 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Cheng6eb516d2011-01-07 23:50:32 +0000371 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner1e158692010-04-04 18:34:07 +0000372
373 bool Error = false;
374
375 // Scan to find the machine operand number for the operand.
376 for (; Val; --Val) {
377 if (OpNo >= MI->getNumOperands()) break;
378 unsigned OpFlags = MI->getOperand(OpNo).getImm();
379 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
380 }
381
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000382 // We may have a location metadata attached to the end of the
383 // instruction, and at no point should see metadata at any
384 // other point while processing. It's an error if so.
Eric Christopher12da1692012-03-22 01:33:51 +0000385 if (OpNo >= MI->getNumOperands() ||
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000386 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner1e158692010-04-04 18:34:07 +0000387 Error = true;
388 } else {
389 unsigned OpFlags = MI->getOperand(OpNo).getImm();
390 ++OpNo; // Skip over the ID number.
391
392 if (Modifier[0] == 'l') // labels are target independent
393 // FIXME: What if the operand isn't an MBB, report error?
394 OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
395 else {
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000396 if (InlineAsm::isMemKind(OpFlags)) {
Chad Rosierdb20a412012-09-10 21:10:49 +0000397 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Chris Lattner1e158692010-04-04 18:34:07 +0000398 Modifier[0] ? Modifier : 0,
399 OS);
400 } else {
Chad Rosierdb20a412012-09-10 21:10:49 +0000401 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Chris Lattner1e158692010-04-04 18:34:07 +0000402 Modifier[0] ? Modifier : 0, OS);
403 }
404 }
405 }
406 if (Error) {
407 std::string msg;
408 raw_string_ostream Msg(msg);
Chris Lattner1e457892010-04-07 23:40:44 +0000409 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
410 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner1e158692010-04-04 18:34:07 +0000411 }
412 }
413 break;
414 }
415 }
416 }
Chad Rosier17788312012-09-11 19:09:56 +0000417 OS << '\n' << (char)0; // null terminate string.
418}
419
420/// EmitInlineAsm - This method formats and emits the specified machine
421/// instruction that is an inline asm.
422void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
423 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
424
425 // Count the number of register definitions to find the asm string.
426 unsigned NumDefs = 0;
427 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
428 ++NumDefs)
429 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
430
431 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
432
433 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
434 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
435
436 // If this asmstr is empty, just print the #APP/#NOAPP markers.
437 // These are useful to see where empty asm's wound up.
438 if (AsmStr[0] == 0) {
Rafael Espindola0b694812014-01-16 16:28:37 +0000439 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
440 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chad Rosier17788312012-09-11 19:09:56 +0000441 return;
Chad Rosier7641f582012-09-10 21:36:05 +0000442 }
443
Chad Rosier17788312012-09-11 19:09:56 +0000444 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000445 // enabled, so we use emitRawComment.
446 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
Chad Rosier17788312012-09-11 19:09:56 +0000447
448 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
449 // it.
450 unsigned LocCookie = 0;
451 const MDNode *LocMD = 0;
452 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
453 if (MI->getOperand(i-1).isMetadata() &&
454 (LocMD = MI->getOperand(i-1).getMetadata()) &&
455 LocMD->getNumOperands() != 0) {
456 if (const ConstantInt *CI = dyn_cast<ConstantInt>(LocMD->getOperand(0))) {
457 LocCookie = CI->getZExtValue();
458 break;
459 }
460 }
461 }
462
463 // Emit the inline asm to a temporary string so we can emit it through
464 // EmitInlineAsm.
465 SmallString<256> StringData;
466 raw_svector_ostream OS(StringData);
467
468 // The variant of the current asmprinter.
469 int AsmPrinterVariant = MAI->getAssemblerDialect();
470 InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
471 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
472 if (InlineAsmVariant == InlineAsm::AD_ATT)
473 EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
474 AP, LocCookie, OS);
475 else
476 EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
477
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000478 EmitInlineAsm(OS.str(), LocMD, MI->getInlineAsmDialect());
Jim Grosbach00915352010-10-01 23:29:12 +0000479
Chris Lattner1e158692010-04-04 18:34:07 +0000480 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000481 // enabled, so we use emitRawComment.
482 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chris Lattner1e158692010-04-04 18:34:07 +0000483}
484
485
486/// PrintSpecial - Print information related to the specified machine instr
487/// that is independent of the operand, and may be independent of the instr
488/// itself. This can be useful for portably encoding the comment character
489/// or other bits of target-specific knowledge into the asmstrings. The
490/// syntax used is ${:comment}. Targets can override this to add support
491/// for their own strange codes.
492void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
493 const char *Code) const {
Rafael Espindola58873562014-01-03 19:21:54 +0000494 const DataLayout *DL = TM.getDataLayout();
Chris Lattner1e158692010-04-04 18:34:07 +0000495 if (!strcmp(Code, "private")) {
Rafael Espindola58873562014-01-03 19:21:54 +0000496 OS << DL->getPrivateGlobalPrefix();
Chris Lattner1e158692010-04-04 18:34:07 +0000497 } else if (!strcmp(Code, "comment")) {
498 OS << MAI->getCommentString();
499 } else if (!strcmp(Code, "uid")) {
500 // Comparing the address of MI isn't sufficient, because machineinstrs may
501 // be allocated to the same address across functions.
Jim Grosbach00915352010-10-01 23:29:12 +0000502
Chris Lattner1e158692010-04-04 18:34:07 +0000503 // If this is a new LastFn instruction, bump the counter.
504 if (LastMI != MI || LastFn != getFunctionNumber()) {
505 ++Counter;
506 LastMI = MI;
507 LastFn = getFunctionNumber();
508 }
509 OS << Counter;
510 } else {
511 std::string msg;
512 raw_string_ostream Msg(msg);
513 Msg << "Unknown special formatter '" << Code
514 << "' for machine instr: " << *MI;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000515 report_fatal_error(Msg.str());
Jim Grosbach00915352010-10-01 23:29:12 +0000516 }
Chris Lattner1e158692010-04-04 18:34:07 +0000517}
518
519/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
520/// instruction, using the specified assembler variant. Targets should
521/// override this to format as appropriate.
522bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chad Rosier1f57bcb2012-09-07 20:23:29 +0000523 unsigned AsmVariant, const char *ExtraCode,
524 raw_ostream &O) {
Jack Carterb2fd5f62012-06-21 17:14:46 +0000525 // Does this asm operand have a single letter operand modifier?
526 if (ExtraCode && ExtraCode[0]) {
527 if (ExtraCode[1] != 0) return true; // Unknown modifier.
528
529 const MachineOperand &MO = MI->getOperand(OpNo);
530 switch (ExtraCode[0]) {
531 default:
532 return true; // Unknown modifier.
533 case 'c': // Substitute immediate value without immediate syntax
Jack Carterc457f622012-06-21 21:37:54 +0000534 if (MO.getType() != MachineOperand::MO_Immediate)
Jack Carterb2fd5f62012-06-21 17:14:46 +0000535 return true;
536 O << MO.getImm();
537 return false;
Jack Carterc457f622012-06-21 21:37:54 +0000538 case 'n': // Negate the immediate constant.
539 if (MO.getType() != MachineOperand::MO_Immediate)
540 return true;
541 O << -MO.getImm();
542 return false;
Jack Carterb2fd5f62012-06-21 17:14:46 +0000543 }
544 }
Chris Lattner1e158692010-04-04 18:34:07 +0000545 return true;
546}
547
548bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
549 unsigned AsmVariant,
550 const char *ExtraCode, raw_ostream &O) {
551 // Target doesn't support this yet!
552 return true;
553}
554
Rafael Espindola65fd0a82014-01-24 15:47:54 +0000555void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
David Peixottoea2bcb92014-02-06 18:19:40 +0000556 const MCSubtargetInfo *EndInfo) const {}