blob: 567b6e3b18e32c8efc9770e439a5b4d01d865d3e [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/SmallString.h"
17#include "llvm/ADT/Twine.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
19#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
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
Daniel Sanders753e1762014-02-13 14:44:26 +000081 // If the output streamer does not have mature MC support or the integrated
82 // assembler has been disabled, just emit the blob textually.
83 // Otherwise parse the asm and emit it via MC support.
Chris Lattner1e158692010-04-04 18:34:07 +000084 // This is useful in case the asm parser doesn't handle something but the
85 // system assembler does.
Daniel Sanders753e1762014-02-13 14:44:26 +000086 const MCAsmInfo *MCAI = TM.getMCAsmInfo();
87 assert(MCAI && "No MCAsmInfo");
88 if (!MCAI->useIntegratedAssembler() &&
89 !OutStreamer.isIntegratedAssemblerRequired()) {
Chris Lattner1e158692010-04-04 18:34:07 +000090 OutStreamer.EmitRawText(Str);
Rafael Espindola65fd0a82014-01-24 15:47:54 +000091 emitInlineAsmEnd(TM.getSubtarget<MCSubtargetInfo>(), 0);
Chris Lattner1e158692010-04-04 18:34:07 +000092 return;
93 }
Jim Grosbach00915352010-10-01 23:29:12 +000094
Chris Lattner8900ef12010-04-05 23:11:24 +000095 SourceMgr SrcMgr;
Chris Lattner300fa452010-11-17 08:03:32 +000096 SrcMgrDiagInfo DiagInfo;
Jim Grosbach00915352010-10-01 23:29:12 +000097
Bob Wilsona594fab2013-02-11 05:37:07 +000098 // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
Chris Lattner59126b22010-04-06 00:55:39 +000099 LLVMContext &LLVMCtx = MMI->getModule()->getContext();
100 bool HasDiagHandler = false;
Bob Wilsona594fab2013-02-11 05:37:07 +0000101 if (LLVMCtx.getInlineAsmDiagnosticHandler() != 0) {
Chad Rosierb759ede2012-09-07 18:16:38 +0000102 // If the source manager has an issue, we arrange for srcMgrDiagHandler
Chris Lattner300fa452010-11-17 08:03:32 +0000103 // to be invoked, getting DiagInfo passed into it.
104 DiagInfo.LocInfo = LocMDNode;
Bob Wilsona594fab2013-02-11 05:37:07 +0000105 DiagInfo.DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
106 DiagInfo.DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
Chad Rosierb759ede2012-09-07 18:16:38 +0000107 SrcMgr.setDiagHandler(srcMgrDiagHandler, &DiagInfo);
Chris Lattner59126b22010-04-06 00:55:39 +0000108 HasDiagHandler = true;
109 }
Jim Grosbach00915352010-10-01 23:29:12 +0000110
Chris Lattner8900ef12010-04-05 23:11:24 +0000111 MemoryBuffer *Buffer;
112 if (isNullTerminated)
113 Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
114 else
115 Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
116
117 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
118 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach00915352010-10-01 23:29:12 +0000119
Ahmed Charles56440fd2014-03-06 05:51:42 +0000120 std::unique_ptr<MCAsmParser> Parser(
121 createMCAsmParser(SrcMgr, OutContext, OutStreamer, *MAI));
Evan Cheng4d1ca962011-07-08 01:53:10 +0000122
David Peixottoea2bcb92014-02-06 18:19:40 +0000123 // Initialize the parser with a fresh subtarget info. It is better to use a
124 // new STI here because the parser may modify it and we do not want those
125 // modifications to persist after parsing the inlineasm. The modifications
126 // made by the parser will be seen by the code emitters because it passes
127 // the current STI down to the EncodeInstruction() method.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000128 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
David Peixottoea2bcb92014-02-06 18:19:40 +0000129 TM.getTargetTriple(), TM.getTargetCPU(), TM.getTargetFeatureString()));
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000130
David Peixottoea2bcb92014-02-06 18:19:40 +0000131 // Preserve a copy of the original STI because the parser may modify it. For
132 // example, when switching between arm and thumb mode. If the target needs to
Eric Christopherf9761a22014-02-26 02:53:18 +0000133 // emit code to return to the original state it can do so in
134 // emitInlineAsmEnd().
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000135 MCSubtargetInfo STIOrig = *STI;
136
Ahmed Charles56440fd2014-03-06 05:51:42 +0000137 std::unique_ptr<MCTargetAsmParser> TAP(
138 TM.getTarget().createMCAsmParser(*STI, *Parser, *MII));
Chris Lattner8900ef12010-04-05 23:11:24 +0000139 if (!TAP)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000140 report_fatal_error("Inline asm not supported by this streamer because"
Benjamin Kramera6769262010-04-08 10:44:28 +0000141 " we don't have an asm parser for this target\n");
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000142 Parser->setAssemblerDialect(Dialect);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000143 Parser->setTargetParser(*TAP.get());
Chris Lattner8900ef12010-04-05 23:11:24 +0000144
145 // Don't implicitly switch to the text section before the asm.
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000146 int Res = Parser->Run(/*NoInitialTextSection*/ true,
147 /*NoFinalize*/ true);
David Peixottoea2bcb92014-02-06 18:19:40 +0000148 emitInlineAsmEnd(STIOrig, STI.get());
Chris Lattner59126b22010-04-06 00:55:39 +0000149 if (Res && !HasDiagHandler)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000150 report_fatal_error("Error parsing inline asm\n");
Chris Lattner1e158692010-04-04 18:34:07 +0000151}
152
Chad Rosier17788312012-09-11 19:09:56 +0000153static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
154 MachineModuleInfo *MMI, int InlineAsmVariant,
155 AsmPrinter *AP, unsigned LocCookie,
156 raw_ostream &OS) {
157 // Switch to the inline assembly variant.
158 OS << "\t.intel_syntax\n\t";
Chris Lattner1e158692010-04-04 18:34:07 +0000159
Chad Rosier17788312012-09-11 19:09:56 +0000160 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner1e158692010-04-04 18:34:07 +0000161 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach00915352010-10-01 23:29:12 +0000162
Chad Rosier17788312012-09-11 19:09:56 +0000163 while (*LastEmitted) {
164 switch (*LastEmitted) {
165 default: {
166 // Not a special case, emit the string section literally.
167 const char *LiteralEnd = LastEmitted+1;
168 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
169 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
170 ++LiteralEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000171
Chad Rosier17788312012-09-11 19:09:56 +0000172 OS.write(LastEmitted, LiteralEnd-LastEmitted);
173 LastEmitted = LiteralEnd;
174 break;
175 }
176 case '\n':
177 ++LastEmitted; // Consume newline character.
178 OS << '\n'; // Indent code with newline.
179 break;
180 case '$': {
181 ++LastEmitted; // Consume '$' character.
182 bool Done = true;
Chris Lattner1e158692010-04-04 18:34:07 +0000183
Chad Rosier17788312012-09-11 19:09:56 +0000184 // Handle escapes.
185 switch (*LastEmitted) {
186 default: Done = false; break;
187 case '$':
188 ++LastEmitted; // Consume second '$' character.
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000189 break;
190 }
Chad Rosier17788312012-09-11 19:09:56 +0000191 if (Done) break;
192
193 const char *IDStart = LastEmitted;
194 const char *IDEnd = IDStart;
195 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
196
197 unsigned Val;
198 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
199 report_fatal_error("Bad $ operand number in inline asm string: '" +
200 Twine(AsmStr) + "'");
201 LastEmitted = IDEnd;
202
203 if (Val >= NumOperands-1)
204 report_fatal_error("Invalid $ operand number in inline asm string: '" +
205 Twine(AsmStr) + "'");
206
207 // Okay, we finally have a value number. Ask the target to print this
208 // operand!
209 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
210
211 bool Error = false;
212
213 // Scan to find the machine operand number for the operand.
214 for (; Val; --Val) {
215 if (OpNo >= MI->getNumOperands()) break;
216 unsigned OpFlags = MI->getOperand(OpNo).getImm();
217 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
218 }
219
220 // We may have a location metadata attached to the end of the
221 // instruction, and at no point should see metadata at any
222 // other point while processing. It's an error if so.
223 if (OpNo >= MI->getNumOperands() ||
224 MI->getOperand(OpNo).isMetadata()) {
225 Error = true;
226 } else {
227 unsigned OpFlags = MI->getOperand(OpNo).getImm();
228 ++OpNo; // Skip over the ID number.
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000229
Chad Rosier17788312012-09-11 19:09:56 +0000230 if (InlineAsm::isMemKind(OpFlags)) {
231 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
232 /*Modifier*/ 0, OS);
233 } else {
234 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
235 /*Modifier*/ 0, OS);
236 }
237 }
238 if (Error) {
239 std::string msg;
240 raw_string_ostream Msg(msg);
241 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
242 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
243 }
244 break;
245 }
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000246 }
Chris Lattner51065562010-04-07 05:38:05 +0000247 }
Chad Rosier17788312012-09-11 19:09:56 +0000248 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
249}
Jim Grosbach00915352010-10-01 23:29:12 +0000250
Chad Rosier17788312012-09-11 19:09:56 +0000251static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
252 MachineModuleInfo *MMI, int InlineAsmVariant,
253 int AsmPrinterVariant, AsmPrinter *AP,
254 unsigned LocCookie, raw_ostream &OS) {
Chris Lattner1e158692010-04-04 18:34:07 +0000255 int CurVariant = -1; // The number of the {.|.|.} region we are in.
256 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chad Rosier17788312012-09-11 19:09:56 +0000257 unsigned NumOperands = MI->getNumOperands();
258
259 OS << '\t';
Jim Grosbach00915352010-10-01 23:29:12 +0000260
Chris Lattner1e158692010-04-04 18:34:07 +0000261 while (*LastEmitted) {
262 switch (*LastEmitted) {
263 default: {
264 // Not a special case, emit the string section literally.
265 const char *LiteralEnd = LastEmitted+1;
266 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
267 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
268 ++LiteralEnd;
269 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
270 OS.write(LastEmitted, LiteralEnd-LastEmitted);
271 LastEmitted = LiteralEnd;
272 break;
273 }
274 case '\n':
275 ++LastEmitted; // Consume newline character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000276 OS << '\n'; // Indent code with newline.
Chris Lattner1e158692010-04-04 18:34:07 +0000277 break;
278 case '$': {
279 ++LastEmitted; // Consume '$' character.
280 bool Done = true;
281
282 // Handle escapes.
283 switch (*LastEmitted) {
284 default: Done = false; break;
285 case '$': // $$ -> $
286 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
287 OS << '$';
288 ++LastEmitted; // Consume second '$' character.
289 break;
290 case '(': // $( -> same as GCC's { character.
291 ++LastEmitted; // Consume '(' character.
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000292 if (CurVariant != -1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000293 report_fatal_error("Nested variants found in inline asm string: '" +
294 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000295 CurVariant = 0; // We're in the first variant now.
296 break;
297 case '|':
298 ++LastEmitted; // consume '|' character.
299 if (CurVariant == -1)
300 OS << '|'; // this is gcc's behavior for | outside a variant
301 else
302 ++CurVariant; // We're in the next variant.
303 break;
304 case ')': // $) -> same as GCC's } char.
305 ++LastEmitted; // consume ')' character.
306 if (CurVariant == -1)
307 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach00915352010-10-01 23:29:12 +0000308 else
Chris Lattner1e158692010-04-04 18:34:07 +0000309 CurVariant = -1;
310 break;
311 }
312 if (Done) break;
Jim Grosbach00915352010-10-01 23:29:12 +0000313
Chris Lattner1e158692010-04-04 18:34:07 +0000314 bool HasCurlyBraces = false;
315 if (*LastEmitted == '{') { // ${variable}
316 ++LastEmitted; // Consume '{' character.
317 HasCurlyBraces = true;
318 }
Jim Grosbach00915352010-10-01 23:29:12 +0000319
Chris Lattner1e158692010-04-04 18:34:07 +0000320 // If we have ${:foo}, then this is not a real operand reference, it is a
321 // "magic" string reference, just like in .td files. Arrange to call
322 // PrintSpecial.
323 if (HasCurlyBraces && *LastEmitted == ':') {
324 ++LastEmitted;
325 const char *StrStart = LastEmitted;
326 const char *StrEnd = strchr(StrStart, '}');
327 if (StrEnd == 0)
Benjamin Kramera6769262010-04-08 10:44:28 +0000328 report_fatal_error("Unterminated ${:foo} operand in inline asm"
329 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000330
Chris Lattner1e158692010-04-04 18:34:07 +0000331 std::string Val(StrStart, StrEnd);
Chad Rosier17788312012-09-11 19:09:56 +0000332 AP->PrintSpecial(MI, OS, Val.c_str());
Chris Lattner1e158692010-04-04 18:34:07 +0000333 LastEmitted = StrEnd+1;
334 break;
335 }
Jim Grosbach00915352010-10-01 23:29:12 +0000336
Chris Lattner1e158692010-04-04 18:34:07 +0000337 const char *IDStart = LastEmitted;
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000338 const char *IDEnd = IDStart;
Jim Grosbach00915352010-10-01 23:29:12 +0000339 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
340
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000341 unsigned Val;
342 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramera6769262010-04-08 10:44:28 +0000343 report_fatal_error("Bad $ operand number in inline asm string: '" +
344 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000345 LastEmitted = IDEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000346
Chris Lattner1e158692010-04-04 18:34:07 +0000347 char Modifier[2] = { 0, 0 };
Jim Grosbach00915352010-10-01 23:29:12 +0000348
Chris Lattner1e158692010-04-04 18:34:07 +0000349 if (HasCurlyBraces) {
350 // If we have curly braces, check for a modifier character. This
351 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
352 if (*LastEmitted == ':') {
353 ++LastEmitted; // Consume ':' character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000354 if (*LastEmitted == 0)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000355 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramera6769262010-04-08 10:44:28 +0000356 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000357
Chris Lattner1e158692010-04-04 18:34:07 +0000358 Modifier[0] = *LastEmitted;
359 ++LastEmitted; // Consume modifier character.
360 }
Jim Grosbach00915352010-10-01 23:29:12 +0000361
Chris Lattner0e45d242010-04-05 22:42:30 +0000362 if (*LastEmitted != '}')
Benjamin Kramera6769262010-04-08 10:44:28 +0000363 report_fatal_error("Bad ${} expression in inline asm string: '" +
364 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000365 ++LastEmitted; // Consume '}' character.
366 }
Jim Grosbach00915352010-10-01 23:29:12 +0000367
Chris Lattner0e45d242010-04-05 22:42:30 +0000368 if (Val >= NumOperands-1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000369 report_fatal_error("Invalid $ operand number in inline asm string: '" +
370 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000371
Chris Lattner1e158692010-04-04 18:34:07 +0000372 // Okay, we finally have a value number. Ask the target to print this
373 // operand!
374 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Cheng6eb516d2011-01-07 23:50:32 +0000375 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner1e158692010-04-04 18:34:07 +0000376
377 bool Error = false;
378
379 // Scan to find the machine operand number for the operand.
380 for (; Val; --Val) {
381 if (OpNo >= MI->getNumOperands()) break;
382 unsigned OpFlags = MI->getOperand(OpNo).getImm();
383 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
384 }
385
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000386 // We may have a location metadata attached to the end of the
387 // instruction, and at no point should see metadata at any
388 // other point while processing. It's an error if so.
Eric Christopher12da1692012-03-22 01:33:51 +0000389 if (OpNo >= MI->getNumOperands() ||
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000390 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner1e158692010-04-04 18:34:07 +0000391 Error = true;
392 } else {
393 unsigned OpFlags = MI->getOperand(OpNo).getImm();
394 ++OpNo; // Skip over the ID number.
395
396 if (Modifier[0] == 'l') // labels are target independent
397 // FIXME: What if the operand isn't an MBB, report error?
398 OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
399 else {
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000400 if (InlineAsm::isMemKind(OpFlags)) {
Chad Rosierdb20a412012-09-10 21:10:49 +0000401 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Chris Lattner1e158692010-04-04 18:34:07 +0000402 Modifier[0] ? Modifier : 0,
403 OS);
404 } else {
Chad Rosierdb20a412012-09-10 21:10:49 +0000405 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Chris Lattner1e158692010-04-04 18:34:07 +0000406 Modifier[0] ? Modifier : 0, OS);
407 }
408 }
409 }
410 if (Error) {
411 std::string msg;
412 raw_string_ostream Msg(msg);
Chris Lattner1e457892010-04-07 23:40:44 +0000413 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
414 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner1e158692010-04-04 18:34:07 +0000415 }
416 }
417 break;
418 }
419 }
420 }
Chad Rosier17788312012-09-11 19:09:56 +0000421 OS << '\n' << (char)0; // null terminate string.
422}
423
424/// EmitInlineAsm - This method formats and emits the specified machine
425/// instruction that is an inline asm.
426void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
427 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
428
429 // Count the number of register definitions to find the asm string.
430 unsigned NumDefs = 0;
431 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
432 ++NumDefs)
433 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
434
435 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
436
437 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
438 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
439
440 // If this asmstr is empty, just print the #APP/#NOAPP markers.
441 // These are useful to see where empty asm's wound up.
442 if (AsmStr[0] == 0) {
Rafael Espindola0b694812014-01-16 16:28:37 +0000443 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
444 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chad Rosier17788312012-09-11 19:09:56 +0000445 return;
Chad Rosier7641f582012-09-10 21:36:05 +0000446 }
447
Chad Rosier17788312012-09-11 19:09:56 +0000448 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000449 // enabled, so we use emitRawComment.
450 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
Chad Rosier17788312012-09-11 19:09:56 +0000451
452 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
453 // it.
454 unsigned LocCookie = 0;
455 const MDNode *LocMD = 0;
456 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
457 if (MI->getOperand(i-1).isMetadata() &&
458 (LocMD = MI->getOperand(i-1).getMetadata()) &&
459 LocMD->getNumOperands() != 0) {
460 if (const ConstantInt *CI = dyn_cast<ConstantInt>(LocMD->getOperand(0))) {
461 LocCookie = CI->getZExtValue();
462 break;
463 }
464 }
465 }
466
467 // Emit the inline asm to a temporary string so we can emit it through
468 // EmitInlineAsm.
469 SmallString<256> StringData;
470 raw_svector_ostream OS(StringData);
471
472 // The variant of the current asmprinter.
473 int AsmPrinterVariant = MAI->getAssemblerDialect();
474 InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
475 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
476 if (InlineAsmVariant == InlineAsm::AD_ATT)
477 EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
478 AP, LocCookie, OS);
479 else
480 EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
481
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000482 EmitInlineAsm(OS.str(), LocMD, MI->getInlineAsmDialect());
Jim Grosbach00915352010-10-01 23:29:12 +0000483
Chris Lattner1e158692010-04-04 18:34:07 +0000484 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000485 // enabled, so we use emitRawComment.
486 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chris Lattner1e158692010-04-04 18:34:07 +0000487}
488
489
490/// PrintSpecial - Print information related to the specified machine instr
491/// that is independent of the operand, and may be independent of the instr
492/// itself. This can be useful for portably encoding the comment character
493/// or other bits of target-specific knowledge into the asmstrings. The
494/// syntax used is ${:comment}. Targets can override this to add support
495/// for their own strange codes.
496void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
497 const char *Code) const {
Rafael Espindola58873562014-01-03 19:21:54 +0000498 const DataLayout *DL = TM.getDataLayout();
Chris Lattner1e158692010-04-04 18:34:07 +0000499 if (!strcmp(Code, "private")) {
Rafael Espindola58873562014-01-03 19:21:54 +0000500 OS << DL->getPrivateGlobalPrefix();
Chris Lattner1e158692010-04-04 18:34:07 +0000501 } else if (!strcmp(Code, "comment")) {
502 OS << MAI->getCommentString();
503 } else if (!strcmp(Code, "uid")) {
504 // Comparing the address of MI isn't sufficient, because machineinstrs may
505 // be allocated to the same address across functions.
Jim Grosbach00915352010-10-01 23:29:12 +0000506
Chris Lattner1e158692010-04-04 18:34:07 +0000507 // If this is a new LastFn instruction, bump the counter.
508 if (LastMI != MI || LastFn != getFunctionNumber()) {
509 ++Counter;
510 LastMI = MI;
511 LastFn = getFunctionNumber();
512 }
513 OS << Counter;
514 } else {
515 std::string msg;
516 raw_string_ostream Msg(msg);
517 Msg << "Unknown special formatter '" << Code
518 << "' for machine instr: " << *MI;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000519 report_fatal_error(Msg.str());
Jim Grosbach00915352010-10-01 23:29:12 +0000520 }
Chris Lattner1e158692010-04-04 18:34:07 +0000521}
522
523/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
524/// instruction, using the specified assembler variant. Targets should
525/// override this to format as appropriate.
526bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chad Rosier1f57bcb2012-09-07 20:23:29 +0000527 unsigned AsmVariant, const char *ExtraCode,
528 raw_ostream &O) {
Jack Carterb2fd5f62012-06-21 17:14:46 +0000529 // Does this asm operand have a single letter operand modifier?
530 if (ExtraCode && ExtraCode[0]) {
531 if (ExtraCode[1] != 0) return true; // Unknown modifier.
532
533 const MachineOperand &MO = MI->getOperand(OpNo);
534 switch (ExtraCode[0]) {
535 default:
536 return true; // Unknown modifier.
537 case 'c': // Substitute immediate value without immediate syntax
Jack Carterc457f622012-06-21 21:37:54 +0000538 if (MO.getType() != MachineOperand::MO_Immediate)
Jack Carterb2fd5f62012-06-21 17:14:46 +0000539 return true;
540 O << MO.getImm();
541 return false;
Jack Carterc457f622012-06-21 21:37:54 +0000542 case 'n': // Negate the immediate constant.
543 if (MO.getType() != MachineOperand::MO_Immediate)
544 return true;
545 O << -MO.getImm();
546 return false;
Jack Carterb2fd5f62012-06-21 17:14:46 +0000547 }
548 }
Chris Lattner1e158692010-04-04 18:34:07 +0000549 return true;
550}
551
552bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
553 unsigned AsmVariant,
554 const char *ExtraCode, raw_ostream &O) {
555 // Target doesn't support this yet!
556 return true;
557}
558
Rafael Espindola65fd0a82014-01-24 15:47:54 +0000559void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
David Peixottoea2bcb92014-02-06 18:19:40 +0000560 const MCSubtargetInfo *EndInfo) const {}