blob: 022de00a37ed9e5fc3491b5b5781fe684201d2c9 [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
Chris Lattner1e158692010-04-04 18:34:07 +000014#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/Twine.h"
17#include "llvm/CodeGen/MachineBasicBlock.h"
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +000018#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Constants.h"
Rafael Espindola58873562014-01-03 19:21:54 +000021#include "llvm/IR/DataLayout.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/InlineAsm.h"
23#include "llvm/IR/LLVMContext.h"
24#include "llvm/IR/Module.h"
Chris Lattner1e158692010-04-04 18:34:07 +000025#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000027#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner1e158692010-04-04 18:34:07 +000028#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000029#include "llvm/MC/MCTargetAsmParser.h"
Chris Lattner1e158692010-04-04 18:34:07 +000030#include "llvm/Support/ErrorHandling.h"
Chris Lattner8900ef12010-04-05 23:11:24 +000031#include "llvm/Support/MemoryBuffer.h"
32#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000033#include "llvm/Support/TargetRegistry.h"
Chris Lattner1e158692010-04-04 18:34:07 +000034#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/Target/TargetMachine.h"
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +000036#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner1e158692010-04-04 18:34:07 +000037using namespace llvm;
38
Chandler Carruth1b9dde02014-04-22 02:02:50 +000039#define DEBUG_TYPE "asm-printer"
40
Chris Lattner300fa452010-11-17 08:03:32 +000041namespace {
42 struct SrcMgrDiagInfo {
43 const MDNode *LocInfo;
Bob Wilsona594fab2013-02-11 05:37:07 +000044 LLVMContext::InlineAsmDiagHandlerTy DiagHandler;
Chris Lattner300fa452010-11-17 08:03:32 +000045 void *DiagContext;
46 };
47}
48
Chad Rosierb759ede2012-09-07 18:16:38 +000049/// srcMgrDiagHandler - This callback is invoked when the SourceMgr for an
Chris Lattner300fa452010-11-17 08:03:32 +000050/// inline asm has an error in it. diagInfo is a pointer to the SrcMgrDiagInfo
51/// struct above.
Chad Rosierb759ede2012-09-07 18:16:38 +000052static void srcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
Chris Lattner300fa452010-11-17 08:03:32 +000053 SrcMgrDiagInfo *DiagInfo = static_cast<SrcMgrDiagInfo *>(diagInfo);
54 assert(DiagInfo && "Diagnostic context not passed down?");
Jim Grosbach098f5a22011-09-21 21:36:53 +000055
Chris Lattner79ffdc72010-11-17 08:20:42 +000056 // If the inline asm had metadata associated with it, pull out a location
57 // cookie corresponding to which line the error occurred on.
Chris Lattner300fa452010-11-17 08:03:32 +000058 unsigned LocCookie = 0;
Chris Lattner79ffdc72010-11-17 08:20:42 +000059 if (const MDNode *LocInfo = DiagInfo->LocInfo) {
60 unsigned ErrorLine = Diag.getLineNo()-1;
61 if (ErrorLine >= LocInfo->getNumOperands())
62 ErrorLine = 0;
Jim Grosbach098f5a22011-09-21 21:36:53 +000063
Chris Lattner79ffdc72010-11-17 08:20:42 +000064 if (LocInfo->getNumOperands() != 0)
65 if (const ConstantInt *CI =
66 dyn_cast<ConstantInt>(LocInfo->getOperand(ErrorLine)))
Chris Lattner300fa452010-11-17 08:03:32 +000067 LocCookie = CI->getZExtValue();
Chris Lattner79ffdc72010-11-17 08:20:42 +000068 }
Jim Grosbach098f5a22011-09-21 21:36:53 +000069
Chris Lattnerb0e36082010-11-17 08:13:01 +000070 DiagInfo->DiagHandler(Diag, DiagInfo->DiagContext, LocCookie);
Chris Lattner300fa452010-11-17 08:03:32 +000071}
72
Chris Lattner1e158692010-04-04 18:34:07 +000073/// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
Chad Rosierf24ae7b2012-09-05 23:57:37 +000074void AsmPrinter::EmitInlineAsm(StringRef Str, const MDNode *LocMDNode,
75 InlineAsm::AsmDialect Dialect) const {
Chris Lattner1e158692010-04-04 18:34:07 +000076 assert(!Str.empty() && "Can't emit empty inline asm block");
Jim Grosbach00915352010-10-01 23:29:12 +000077
Chris Lattner8900ef12010-04-05 23:11:24 +000078 // Remember if the buffer is nul terminated or not so we can avoid a copy.
79 bool isNullTerminated = Str.back() == 0;
80 if (isNullTerminated)
81 Str = Str.substr(0, Str.size()-1);
Jim Grosbach00915352010-10-01 23:29:12 +000082
Daniel Sanders753e1762014-02-13 14:44:26 +000083 // If the output streamer does not have mature MC support or the integrated
84 // assembler has been disabled, just emit the blob textually.
85 // Otherwise parse the asm and emit it via MC support.
Chris Lattner1e158692010-04-04 18:34:07 +000086 // This is useful in case the asm parser doesn't handle something but the
87 // system assembler does.
Daniel Sanders753e1762014-02-13 14:44:26 +000088 const MCAsmInfo *MCAI = TM.getMCAsmInfo();
89 assert(MCAI && "No MCAsmInfo");
90 if (!MCAI->useIntegratedAssembler() &&
91 !OutStreamer.isIntegratedAssemblerRequired()) {
Chris Lattner1e158692010-04-04 18:34:07 +000092 OutStreamer.EmitRawText(Str);
Rafael Espindola65fd0a82014-01-24 15:47:54 +000093 emitInlineAsmEnd(TM.getSubtarget<MCSubtargetInfo>(), 0);
Chris Lattner1e158692010-04-04 18:34:07 +000094 return;
95 }
Jim Grosbach00915352010-10-01 23:29:12 +000096
Chris Lattner8900ef12010-04-05 23:11:24 +000097 SourceMgr SrcMgr;
Chris Lattner300fa452010-11-17 08:03:32 +000098 SrcMgrDiagInfo DiagInfo;
Jim Grosbach00915352010-10-01 23:29:12 +000099
Bob Wilsona594fab2013-02-11 05:37:07 +0000100 // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
Chris Lattner59126b22010-04-06 00:55:39 +0000101 LLVMContext &LLVMCtx = MMI->getModule()->getContext();
102 bool HasDiagHandler = false;
Bob Wilsona594fab2013-02-11 05:37:07 +0000103 if (LLVMCtx.getInlineAsmDiagnosticHandler() != 0) {
Chad Rosierb759ede2012-09-07 18:16:38 +0000104 // If the source manager has an issue, we arrange for srcMgrDiagHandler
Chris Lattner300fa452010-11-17 08:03:32 +0000105 // to be invoked, getting DiagInfo passed into it.
106 DiagInfo.LocInfo = LocMDNode;
Bob Wilsona594fab2013-02-11 05:37:07 +0000107 DiagInfo.DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
108 DiagInfo.DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
Chad Rosierb759ede2012-09-07 18:16:38 +0000109 SrcMgr.setDiagHandler(srcMgrDiagHandler, &DiagInfo);
Chris Lattner59126b22010-04-06 00:55:39 +0000110 HasDiagHandler = true;
111 }
Jim Grosbach00915352010-10-01 23:29:12 +0000112
Chris Lattner8900ef12010-04-05 23:11:24 +0000113 MemoryBuffer *Buffer;
114 if (isNullTerminated)
115 Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
116 else
117 Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
118
119 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
120 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach00915352010-10-01 23:29:12 +0000121
Ahmed Charles56440fd2014-03-06 05:51:42 +0000122 std::unique_ptr<MCAsmParser> Parser(
123 createMCAsmParser(SrcMgr, OutContext, OutStreamer, *MAI));
Evan Cheng4d1ca962011-07-08 01:53:10 +0000124
David Peixottoea2bcb92014-02-06 18:19:40 +0000125 // Initialize the parser with a fresh subtarget info. It is better to use a
126 // new STI here because the parser may modify it and we do not want those
127 // modifications to persist after parsing the inlineasm. The modifications
128 // made by the parser will be seen by the code emitters because it passes
129 // the current STI down to the EncodeInstruction() method.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000130 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
David Peixottoea2bcb92014-02-06 18:19:40 +0000131 TM.getTargetTriple(), TM.getTargetCPU(), TM.getTargetFeatureString()));
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000132
David Peixottoea2bcb92014-02-06 18:19:40 +0000133 // Preserve a copy of the original STI because the parser may modify it. For
134 // example, when switching between arm and thumb mode. If the target needs to
Eric Christopherf9761a22014-02-26 02:53:18 +0000135 // emit code to return to the original state it can do so in
136 // emitInlineAsmEnd().
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000137 MCSubtargetInfo STIOrig = *STI;
138
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000139 MCTargetOptions MCOptions;
140 if (MF)
141 MCOptions = MF->getTarget().Options.MCOptions;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000142 std::unique_ptr<MCTargetAsmParser> TAP(
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000143 TM.getTarget().createMCAsmParser(*STI, *Parser, *MII, MCOptions));
Chris Lattner8900ef12010-04-05 23:11:24 +0000144 if (!TAP)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000145 report_fatal_error("Inline asm not supported by this streamer because"
Benjamin Kramera6769262010-04-08 10:44:28 +0000146 " we don't have an asm parser for this target\n");
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000147 Parser->setAssemblerDialect(Dialect);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000148 Parser->setTargetParser(*TAP.get());
Chris Lattner8900ef12010-04-05 23:11:24 +0000149
150 // Don't implicitly switch to the text section before the asm.
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000151 int Res = Parser->Run(/*NoInitialTextSection*/ true,
152 /*NoFinalize*/ true);
David Peixottoea2bcb92014-02-06 18:19:40 +0000153 emitInlineAsmEnd(STIOrig, STI.get());
Chris Lattner59126b22010-04-06 00:55:39 +0000154 if (Res && !HasDiagHandler)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000155 report_fatal_error("Error parsing inline asm\n");
Chris Lattner1e158692010-04-04 18:34:07 +0000156}
157
Chad Rosier17788312012-09-11 19:09:56 +0000158static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
159 MachineModuleInfo *MMI, int InlineAsmVariant,
160 AsmPrinter *AP, unsigned LocCookie,
161 raw_ostream &OS) {
162 // Switch to the inline assembly variant.
163 OS << "\t.intel_syntax\n\t";
Chris Lattner1e158692010-04-04 18:34:07 +0000164
Chad Rosier17788312012-09-11 19:09:56 +0000165 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner1e158692010-04-04 18:34:07 +0000166 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach00915352010-10-01 23:29:12 +0000167
Chad Rosier17788312012-09-11 19:09:56 +0000168 while (*LastEmitted) {
169 switch (*LastEmitted) {
170 default: {
171 // Not a special case, emit the string section literally.
172 const char *LiteralEnd = LastEmitted+1;
173 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
174 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
175 ++LiteralEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000176
Chad Rosier17788312012-09-11 19:09:56 +0000177 OS.write(LastEmitted, LiteralEnd-LastEmitted);
178 LastEmitted = LiteralEnd;
179 break;
180 }
181 case '\n':
182 ++LastEmitted; // Consume newline character.
183 OS << '\n'; // Indent code with newline.
184 break;
185 case '$': {
186 ++LastEmitted; // Consume '$' character.
187 bool Done = true;
Chris Lattner1e158692010-04-04 18:34:07 +0000188
Chad Rosier17788312012-09-11 19:09:56 +0000189 // Handle escapes.
190 switch (*LastEmitted) {
191 default: Done = false; break;
192 case '$':
193 ++LastEmitted; // Consume second '$' character.
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000194 break;
195 }
Chad Rosier17788312012-09-11 19:09:56 +0000196 if (Done) break;
197
198 const char *IDStart = LastEmitted;
199 const char *IDEnd = IDStart;
200 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
201
202 unsigned Val;
203 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
204 report_fatal_error("Bad $ operand number in inline asm string: '" +
205 Twine(AsmStr) + "'");
206 LastEmitted = IDEnd;
207
208 if (Val >= NumOperands-1)
209 report_fatal_error("Invalid $ operand number in inline asm string: '" +
210 Twine(AsmStr) + "'");
211
212 // Okay, we finally have a value number. Ask the target to print this
213 // operand!
214 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
215
216 bool Error = false;
217
218 // Scan to find the machine operand number for the operand.
219 for (; Val; --Val) {
220 if (OpNo >= MI->getNumOperands()) break;
221 unsigned OpFlags = MI->getOperand(OpNo).getImm();
222 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
223 }
224
225 // We may have a location metadata attached to the end of the
226 // instruction, and at no point should see metadata at any
227 // other point while processing. It's an error if so.
228 if (OpNo >= MI->getNumOperands() ||
229 MI->getOperand(OpNo).isMetadata()) {
230 Error = true;
231 } else {
232 unsigned OpFlags = MI->getOperand(OpNo).getImm();
233 ++OpNo; // Skip over the ID number.
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000234
Chad Rosier17788312012-09-11 19:09:56 +0000235 if (InlineAsm::isMemKind(OpFlags)) {
236 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
237 /*Modifier*/ 0, OS);
238 } else {
239 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
240 /*Modifier*/ 0, OS);
241 }
242 }
243 if (Error) {
244 std::string msg;
245 raw_string_ostream Msg(msg);
246 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
247 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
248 }
249 break;
250 }
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000251 }
Chris Lattner51065562010-04-07 05:38:05 +0000252 }
Chad Rosier17788312012-09-11 19:09:56 +0000253 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
254}
Jim Grosbach00915352010-10-01 23:29:12 +0000255
Chad Rosier17788312012-09-11 19:09:56 +0000256static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
257 MachineModuleInfo *MMI, int InlineAsmVariant,
258 int AsmPrinterVariant, AsmPrinter *AP,
259 unsigned LocCookie, raw_ostream &OS) {
Chris Lattner1e158692010-04-04 18:34:07 +0000260 int CurVariant = -1; // The number of the {.|.|.} region we are in.
261 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chad Rosier17788312012-09-11 19:09:56 +0000262 unsigned NumOperands = MI->getNumOperands();
263
264 OS << '\t';
Jim Grosbach00915352010-10-01 23:29:12 +0000265
Chris Lattner1e158692010-04-04 18:34:07 +0000266 while (*LastEmitted) {
267 switch (*LastEmitted) {
268 default: {
269 // Not a special case, emit the string section literally.
270 const char *LiteralEnd = LastEmitted+1;
271 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
272 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
273 ++LiteralEnd;
274 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
275 OS.write(LastEmitted, LiteralEnd-LastEmitted);
276 LastEmitted = LiteralEnd;
277 break;
278 }
279 case '\n':
280 ++LastEmitted; // Consume newline character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000281 OS << '\n'; // Indent code with newline.
Chris Lattner1e158692010-04-04 18:34:07 +0000282 break;
283 case '$': {
284 ++LastEmitted; // Consume '$' character.
285 bool Done = true;
286
287 // Handle escapes.
288 switch (*LastEmitted) {
289 default: Done = false; break;
290 case '$': // $$ -> $
291 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
292 OS << '$';
293 ++LastEmitted; // Consume second '$' character.
294 break;
295 case '(': // $( -> same as GCC's { character.
296 ++LastEmitted; // Consume '(' character.
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000297 if (CurVariant != -1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000298 report_fatal_error("Nested variants found in inline asm string: '" +
299 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000300 CurVariant = 0; // We're in the first variant now.
301 break;
302 case '|':
303 ++LastEmitted; // consume '|' character.
304 if (CurVariant == -1)
305 OS << '|'; // this is gcc's behavior for | outside a variant
306 else
307 ++CurVariant; // We're in the next variant.
308 break;
309 case ')': // $) -> same as GCC's } char.
310 ++LastEmitted; // consume ')' character.
311 if (CurVariant == -1)
312 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach00915352010-10-01 23:29:12 +0000313 else
Chris Lattner1e158692010-04-04 18:34:07 +0000314 CurVariant = -1;
315 break;
316 }
317 if (Done) break;
Jim Grosbach00915352010-10-01 23:29:12 +0000318
Chris Lattner1e158692010-04-04 18:34:07 +0000319 bool HasCurlyBraces = false;
320 if (*LastEmitted == '{') { // ${variable}
321 ++LastEmitted; // Consume '{' character.
322 HasCurlyBraces = true;
323 }
Jim Grosbach00915352010-10-01 23:29:12 +0000324
Chris Lattner1e158692010-04-04 18:34:07 +0000325 // If we have ${:foo}, then this is not a real operand reference, it is a
326 // "magic" string reference, just like in .td files. Arrange to call
327 // PrintSpecial.
328 if (HasCurlyBraces && *LastEmitted == ':') {
329 ++LastEmitted;
330 const char *StrStart = LastEmitted;
331 const char *StrEnd = strchr(StrStart, '}');
332 if (StrEnd == 0)
Benjamin Kramera6769262010-04-08 10:44:28 +0000333 report_fatal_error("Unterminated ${:foo} operand in inline asm"
334 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000335
Chris Lattner1e158692010-04-04 18:34:07 +0000336 std::string Val(StrStart, StrEnd);
Chad Rosier17788312012-09-11 19:09:56 +0000337 AP->PrintSpecial(MI, OS, Val.c_str());
Chris Lattner1e158692010-04-04 18:34:07 +0000338 LastEmitted = StrEnd+1;
339 break;
340 }
Jim Grosbach00915352010-10-01 23:29:12 +0000341
Chris Lattner1e158692010-04-04 18:34:07 +0000342 const char *IDStart = LastEmitted;
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000343 const char *IDEnd = IDStart;
Jim Grosbach00915352010-10-01 23:29:12 +0000344 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
345
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000346 unsigned Val;
347 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramera6769262010-04-08 10:44:28 +0000348 report_fatal_error("Bad $ operand number in inline asm string: '" +
349 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000350 LastEmitted = IDEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000351
Chris Lattner1e158692010-04-04 18:34:07 +0000352 char Modifier[2] = { 0, 0 };
Jim Grosbach00915352010-10-01 23:29:12 +0000353
Chris Lattner1e158692010-04-04 18:34:07 +0000354 if (HasCurlyBraces) {
355 // If we have curly braces, check for a modifier character. This
356 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
357 if (*LastEmitted == ':') {
358 ++LastEmitted; // Consume ':' character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000359 if (*LastEmitted == 0)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000360 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramera6769262010-04-08 10:44:28 +0000361 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000362
Chris Lattner1e158692010-04-04 18:34:07 +0000363 Modifier[0] = *LastEmitted;
364 ++LastEmitted; // Consume modifier character.
365 }
Jim Grosbach00915352010-10-01 23:29:12 +0000366
Chris Lattner0e45d242010-04-05 22:42:30 +0000367 if (*LastEmitted != '}')
Benjamin Kramera6769262010-04-08 10:44:28 +0000368 report_fatal_error("Bad ${} expression in inline asm string: '" +
369 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000370 ++LastEmitted; // Consume '}' character.
371 }
Jim Grosbach00915352010-10-01 23:29:12 +0000372
Chris Lattner0e45d242010-04-05 22:42:30 +0000373 if (Val >= NumOperands-1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000374 report_fatal_error("Invalid $ operand number in inline asm string: '" +
375 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000376
Chris Lattner1e158692010-04-04 18:34:07 +0000377 // Okay, we finally have a value number. Ask the target to print this
378 // operand!
379 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Cheng6eb516d2011-01-07 23:50:32 +0000380 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner1e158692010-04-04 18:34:07 +0000381
382 bool Error = false;
383
384 // Scan to find the machine operand number for the operand.
385 for (; Val; --Val) {
386 if (OpNo >= MI->getNumOperands()) break;
387 unsigned OpFlags = MI->getOperand(OpNo).getImm();
388 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
389 }
390
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000391 // We may have a location metadata attached to the end of the
392 // instruction, and at no point should see metadata at any
393 // other point while processing. It's an error if so.
Eric Christopher12da1692012-03-22 01:33:51 +0000394 if (OpNo >= MI->getNumOperands() ||
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000395 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner1e158692010-04-04 18:34:07 +0000396 Error = true;
397 } else {
398 unsigned OpFlags = MI->getOperand(OpNo).getImm();
399 ++OpNo; // Skip over the ID number.
400
401 if (Modifier[0] == 'l') // labels are target independent
402 // FIXME: What if the operand isn't an MBB, report error?
403 OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
404 else {
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000405 if (InlineAsm::isMemKind(OpFlags)) {
Chad Rosierdb20a412012-09-10 21:10:49 +0000406 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Chris Lattner1e158692010-04-04 18:34:07 +0000407 Modifier[0] ? Modifier : 0,
408 OS);
409 } else {
Chad Rosierdb20a412012-09-10 21:10:49 +0000410 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Chris Lattner1e158692010-04-04 18:34:07 +0000411 Modifier[0] ? Modifier : 0, OS);
412 }
413 }
414 }
415 if (Error) {
416 std::string msg;
417 raw_string_ostream Msg(msg);
Chris Lattner1e457892010-04-07 23:40:44 +0000418 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
419 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner1e158692010-04-04 18:34:07 +0000420 }
421 }
422 break;
423 }
424 }
425 }
Chad Rosier17788312012-09-11 19:09:56 +0000426 OS << '\n' << (char)0; // null terminate string.
427}
428
429/// EmitInlineAsm - This method formats and emits the specified machine
430/// instruction that is an inline asm.
431void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
432 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
433
434 // Count the number of register definitions to find the asm string.
435 unsigned NumDefs = 0;
436 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
437 ++NumDefs)
438 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
439
440 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
441
442 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
443 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
444
445 // If this asmstr is empty, just print the #APP/#NOAPP markers.
446 // These are useful to see where empty asm's wound up.
447 if (AsmStr[0] == 0) {
Rafael Espindola0b694812014-01-16 16:28:37 +0000448 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
449 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chad Rosier17788312012-09-11 19:09:56 +0000450 return;
Chad Rosier7641f582012-09-10 21:36:05 +0000451 }
452
Chad Rosier17788312012-09-11 19:09:56 +0000453 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000454 // enabled, so we use emitRawComment.
455 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
Chad Rosier17788312012-09-11 19:09:56 +0000456
457 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
458 // it.
459 unsigned LocCookie = 0;
460 const MDNode *LocMD = 0;
461 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
462 if (MI->getOperand(i-1).isMetadata() &&
463 (LocMD = MI->getOperand(i-1).getMetadata()) &&
464 LocMD->getNumOperands() != 0) {
465 if (const ConstantInt *CI = dyn_cast<ConstantInt>(LocMD->getOperand(0))) {
466 LocCookie = CI->getZExtValue();
467 break;
468 }
469 }
470 }
471
472 // Emit the inline asm to a temporary string so we can emit it through
473 // EmitInlineAsm.
474 SmallString<256> StringData;
475 raw_svector_ostream OS(StringData);
476
477 // The variant of the current asmprinter.
478 int AsmPrinterVariant = MAI->getAssemblerDialect();
479 InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
480 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
481 if (InlineAsmVariant == InlineAsm::AD_ATT)
482 EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
483 AP, LocCookie, OS);
484 else
485 EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
486
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000487 EmitInlineAsm(OS.str(), LocMD, MI->getInlineAsmDialect());
Jim Grosbach00915352010-10-01 23:29:12 +0000488
Chris Lattner1e158692010-04-04 18:34:07 +0000489 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000490 // enabled, so we use emitRawComment.
491 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chris Lattner1e158692010-04-04 18:34:07 +0000492}
493
494
495/// PrintSpecial - Print information related to the specified machine instr
496/// that is independent of the operand, and may be independent of the instr
497/// itself. This can be useful for portably encoding the comment character
498/// or other bits of target-specific knowledge into the asmstrings. The
499/// syntax used is ${:comment}. Targets can override this to add support
500/// for their own strange codes.
501void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
502 const char *Code) const {
Rafael Espindola58873562014-01-03 19:21:54 +0000503 const DataLayout *DL = TM.getDataLayout();
Chris Lattner1e158692010-04-04 18:34:07 +0000504 if (!strcmp(Code, "private")) {
Rafael Espindola58873562014-01-03 19:21:54 +0000505 OS << DL->getPrivateGlobalPrefix();
Chris Lattner1e158692010-04-04 18:34:07 +0000506 } else if (!strcmp(Code, "comment")) {
507 OS << MAI->getCommentString();
508 } else if (!strcmp(Code, "uid")) {
509 // Comparing the address of MI isn't sufficient, because machineinstrs may
510 // be allocated to the same address across functions.
Jim Grosbach00915352010-10-01 23:29:12 +0000511
Chris Lattner1e158692010-04-04 18:34:07 +0000512 // If this is a new LastFn instruction, bump the counter.
513 if (LastMI != MI || LastFn != getFunctionNumber()) {
514 ++Counter;
515 LastMI = MI;
516 LastFn = getFunctionNumber();
517 }
518 OS << Counter;
519 } else {
520 std::string msg;
521 raw_string_ostream Msg(msg);
522 Msg << "Unknown special formatter '" << Code
523 << "' for machine instr: " << *MI;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000524 report_fatal_error(Msg.str());
Jim Grosbach00915352010-10-01 23:29:12 +0000525 }
Chris Lattner1e158692010-04-04 18:34:07 +0000526}
527
528/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
529/// instruction, using the specified assembler variant. Targets should
530/// override this to format as appropriate.
531bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chad Rosier1f57bcb2012-09-07 20:23:29 +0000532 unsigned AsmVariant, const char *ExtraCode,
533 raw_ostream &O) {
Jack Carterb2fd5f62012-06-21 17:14:46 +0000534 // Does this asm operand have a single letter operand modifier?
535 if (ExtraCode && ExtraCode[0]) {
536 if (ExtraCode[1] != 0) return true; // Unknown modifier.
537
538 const MachineOperand &MO = MI->getOperand(OpNo);
539 switch (ExtraCode[0]) {
540 default:
541 return true; // Unknown modifier.
542 case 'c': // Substitute immediate value without immediate syntax
Jack Carterc457f622012-06-21 21:37:54 +0000543 if (MO.getType() != MachineOperand::MO_Immediate)
Jack Carterb2fd5f62012-06-21 17:14:46 +0000544 return true;
545 O << MO.getImm();
546 return false;
Jack Carterc457f622012-06-21 21:37:54 +0000547 case 'n': // Negate the immediate constant.
548 if (MO.getType() != MachineOperand::MO_Immediate)
549 return true;
550 O << -MO.getImm();
551 return false;
Jack Carterb2fd5f62012-06-21 17:14:46 +0000552 }
553 }
Chris Lattner1e158692010-04-04 18:34:07 +0000554 return true;
555}
556
557bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
558 unsigned AsmVariant,
559 const char *ExtraCode, raw_ostream &O) {
560 // Target doesn't support this yet!
561 return true;
562}
563
Rafael Espindola65fd0a82014-01-24 15:47:54 +0000564void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
David Peixottoea2bcb92014-02-06 18:19:40 +0000565 const MCSubtargetInfo *EndInfo) const {}