blob: d5f43cacc37ff70314108064967f2a75bc1cbdbc [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"
18#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
Rafael Espindola58873562014-01-03 19:21:54 +000020#include "llvm/IR/DataLayout.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/InlineAsm.h"
22#include "llvm/IR/LLVMContext.h"
23#include "llvm/IR/Module.h"
Chris Lattner1e158692010-04-04 18:34:07 +000024#include "llvm/MC/MCAsmInfo.h"
25#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000026#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner1e158692010-04-04 18:34:07 +000027#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000028#include "llvm/MC/MCTargetAsmParser.h"
Chris Lattner1e158692010-04-04 18:34:07 +000029#include "llvm/Support/ErrorHandling.h"
Chris Lattner8900ef12010-04-05 23:11:24 +000030#include "llvm/Support/MemoryBuffer.h"
31#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000032#include "llvm/Support/TargetRegistry.h"
Chris Lattner1e158692010-04-04 18:34:07 +000033#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Target/TargetMachine.h"
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +000035#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner1e158692010-04-04 18:34:07 +000036using namespace llvm;
37
Chandler Carruth1b9dde02014-04-22 02:02:50 +000038#define DEBUG_TYPE "asm-printer"
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
Daniel Sanders753e1762014-02-13 14:44:26 +000082 // If the output streamer does not have mature MC support or the integrated
83 // assembler has been disabled, just emit the blob textually.
84 // Otherwise parse the asm and emit it via MC support.
Chris Lattner1e158692010-04-04 18:34:07 +000085 // This is useful in case the asm parser doesn't handle something but the
86 // system assembler does.
Daniel Sanders753e1762014-02-13 14:44:26 +000087 const MCAsmInfo *MCAI = TM.getMCAsmInfo();
88 assert(MCAI && "No MCAsmInfo");
89 if (!MCAI->useIntegratedAssembler() &&
90 !OutStreamer.isIntegratedAssemblerRequired()) {
Chris Lattner1e158692010-04-04 18:34:07 +000091 OutStreamer.EmitRawText(Str);
Rafael Espindola65fd0a82014-01-24 15:47:54 +000092 emitInlineAsmEnd(TM.getSubtarget<MCSubtargetInfo>(), 0);
Chris Lattner1e158692010-04-04 18:34:07 +000093 return;
94 }
Jim Grosbach00915352010-10-01 23:29:12 +000095
Chris Lattner8900ef12010-04-05 23:11:24 +000096 SourceMgr SrcMgr;
Chris Lattner300fa452010-11-17 08:03:32 +000097 SrcMgrDiagInfo DiagInfo;
Jim Grosbach00915352010-10-01 23:29:12 +000098
Bob Wilsona594fab2013-02-11 05:37:07 +000099 // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
Chris Lattner59126b22010-04-06 00:55:39 +0000100 LLVMContext &LLVMCtx = MMI->getModule()->getContext();
101 bool HasDiagHandler = false;
Bob Wilsona594fab2013-02-11 05:37:07 +0000102 if (LLVMCtx.getInlineAsmDiagnosticHandler() != 0) {
Chad Rosierb759ede2012-09-07 18:16:38 +0000103 // If the source manager has an issue, we arrange for srcMgrDiagHandler
Chris Lattner300fa452010-11-17 08:03:32 +0000104 // to be invoked, getting DiagInfo passed into it.
105 DiagInfo.LocInfo = LocMDNode;
Bob Wilsona594fab2013-02-11 05:37:07 +0000106 DiagInfo.DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
107 DiagInfo.DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
Chad Rosierb759ede2012-09-07 18:16:38 +0000108 SrcMgr.setDiagHandler(srcMgrDiagHandler, &DiagInfo);
Chris Lattner59126b22010-04-06 00:55:39 +0000109 HasDiagHandler = true;
110 }
Jim Grosbach00915352010-10-01 23:29:12 +0000111
Chris Lattner8900ef12010-04-05 23:11:24 +0000112 MemoryBuffer *Buffer;
113 if (isNullTerminated)
114 Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
115 else
116 Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
117
118 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
119 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach00915352010-10-01 23:29:12 +0000120
Ahmed Charles56440fd2014-03-06 05:51:42 +0000121 std::unique_ptr<MCAsmParser> Parser(
122 createMCAsmParser(SrcMgr, OutContext, OutStreamer, *MAI));
Evan Cheng4d1ca962011-07-08 01:53:10 +0000123
David Peixottoea2bcb92014-02-06 18:19:40 +0000124 // Initialize the parser with a fresh subtarget info. It is better to use a
125 // new STI here because the parser may modify it and we do not want those
126 // modifications to persist after parsing the inlineasm. The modifications
127 // made by the parser will be seen by the code emitters because it passes
128 // the current STI down to the EncodeInstruction() method.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000129 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
David Peixottoea2bcb92014-02-06 18:19:40 +0000130 TM.getTargetTriple(), TM.getTargetCPU(), TM.getTargetFeatureString()));
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000131
David Peixottoea2bcb92014-02-06 18:19:40 +0000132 // Preserve a copy of the original STI because the parser may modify it. For
133 // example, when switching between arm and thumb mode. If the target needs to
Eric Christopherf9761a22014-02-26 02:53:18 +0000134 // emit code to return to the original state it can do so in
135 // emitInlineAsmEnd().
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000136 MCSubtargetInfo STIOrig = *STI;
137
Ahmed Charles56440fd2014-03-06 05:51:42 +0000138 std::unique_ptr<MCTargetAsmParser> TAP(
139 TM.getTarget().createMCAsmParser(*STI, *Parser, *MII));
Chris Lattner8900ef12010-04-05 23:11:24 +0000140 if (!TAP)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000141 report_fatal_error("Inline asm not supported by this streamer because"
Benjamin Kramera6769262010-04-08 10:44:28 +0000142 " we don't have an asm parser for this target\n");
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000143 Parser->setAssemblerDialect(Dialect);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000144 Parser->setTargetParser(*TAP.get());
Chris Lattner8900ef12010-04-05 23:11:24 +0000145
146 // Don't implicitly switch to the text section before the asm.
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000147 int Res = Parser->Run(/*NoInitialTextSection*/ true,
148 /*NoFinalize*/ true);
David Peixottoea2bcb92014-02-06 18:19:40 +0000149 emitInlineAsmEnd(STIOrig, STI.get());
Chris Lattner59126b22010-04-06 00:55:39 +0000150 if (Res && !HasDiagHandler)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000151 report_fatal_error("Error parsing inline asm\n");
Chris Lattner1e158692010-04-04 18:34:07 +0000152}
153
Chad Rosier17788312012-09-11 19:09:56 +0000154static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
155 MachineModuleInfo *MMI, int InlineAsmVariant,
156 AsmPrinter *AP, unsigned LocCookie,
157 raw_ostream &OS) {
158 // Switch to the inline assembly variant.
159 OS << "\t.intel_syntax\n\t";
Chris Lattner1e158692010-04-04 18:34:07 +0000160
Chad Rosier17788312012-09-11 19:09:56 +0000161 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner1e158692010-04-04 18:34:07 +0000162 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach00915352010-10-01 23:29:12 +0000163
Chad Rosier17788312012-09-11 19:09:56 +0000164 while (*LastEmitted) {
165 switch (*LastEmitted) {
166 default: {
167 // Not a special case, emit the string section literally.
168 const char *LiteralEnd = LastEmitted+1;
169 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
170 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
171 ++LiteralEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000172
Chad Rosier17788312012-09-11 19:09:56 +0000173 OS.write(LastEmitted, LiteralEnd-LastEmitted);
174 LastEmitted = LiteralEnd;
175 break;
176 }
177 case '\n':
178 ++LastEmitted; // Consume newline character.
179 OS << '\n'; // Indent code with newline.
180 break;
181 case '$': {
182 ++LastEmitted; // Consume '$' character.
183 bool Done = true;
Chris Lattner1e158692010-04-04 18:34:07 +0000184
Chad Rosier17788312012-09-11 19:09:56 +0000185 // Handle escapes.
186 switch (*LastEmitted) {
187 default: Done = false; break;
188 case '$':
189 ++LastEmitted; // Consume second '$' character.
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000190 break;
191 }
Chad Rosier17788312012-09-11 19:09:56 +0000192 if (Done) break;
193
194 const char *IDStart = LastEmitted;
195 const char *IDEnd = IDStart;
196 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
197
198 unsigned Val;
199 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
200 report_fatal_error("Bad $ operand number in inline asm string: '" +
201 Twine(AsmStr) + "'");
202 LastEmitted = IDEnd;
203
204 if (Val >= NumOperands-1)
205 report_fatal_error("Invalid $ operand number in inline asm string: '" +
206 Twine(AsmStr) + "'");
207
208 // Okay, we finally have a value number. Ask the target to print this
209 // operand!
210 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
211
212 bool Error = false;
213
214 // Scan to find the machine operand number for the operand.
215 for (; Val; --Val) {
216 if (OpNo >= MI->getNumOperands()) break;
217 unsigned OpFlags = MI->getOperand(OpNo).getImm();
218 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
219 }
220
221 // We may have a location metadata attached to the end of the
222 // instruction, and at no point should see metadata at any
223 // other point while processing. It's an error if so.
224 if (OpNo >= MI->getNumOperands() ||
225 MI->getOperand(OpNo).isMetadata()) {
226 Error = true;
227 } else {
228 unsigned OpFlags = MI->getOperand(OpNo).getImm();
229 ++OpNo; // Skip over the ID number.
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000230
Chad Rosier17788312012-09-11 19:09:56 +0000231 if (InlineAsm::isMemKind(OpFlags)) {
232 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
233 /*Modifier*/ 0, OS);
234 } else {
235 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
236 /*Modifier*/ 0, OS);
237 }
238 }
239 if (Error) {
240 std::string msg;
241 raw_string_ostream Msg(msg);
242 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
243 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
244 }
245 break;
246 }
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000247 }
Chris Lattner51065562010-04-07 05:38:05 +0000248 }
Chad Rosier17788312012-09-11 19:09:56 +0000249 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
250}
Jim Grosbach00915352010-10-01 23:29:12 +0000251
Chad Rosier17788312012-09-11 19:09:56 +0000252static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
253 MachineModuleInfo *MMI, int InlineAsmVariant,
254 int AsmPrinterVariant, AsmPrinter *AP,
255 unsigned LocCookie, raw_ostream &OS) {
Chris Lattner1e158692010-04-04 18:34:07 +0000256 int CurVariant = -1; // The number of the {.|.|.} region we are in.
257 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chad Rosier17788312012-09-11 19:09:56 +0000258 unsigned NumOperands = MI->getNumOperands();
259
260 OS << '\t';
Jim Grosbach00915352010-10-01 23:29:12 +0000261
Chris Lattner1e158692010-04-04 18:34:07 +0000262 while (*LastEmitted) {
263 switch (*LastEmitted) {
264 default: {
265 // Not a special case, emit the string section literally.
266 const char *LiteralEnd = LastEmitted+1;
267 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
268 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
269 ++LiteralEnd;
270 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
271 OS.write(LastEmitted, LiteralEnd-LastEmitted);
272 LastEmitted = LiteralEnd;
273 break;
274 }
275 case '\n':
276 ++LastEmitted; // Consume newline character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000277 OS << '\n'; // Indent code with newline.
Chris Lattner1e158692010-04-04 18:34:07 +0000278 break;
279 case '$': {
280 ++LastEmitted; // Consume '$' character.
281 bool Done = true;
282
283 // Handle escapes.
284 switch (*LastEmitted) {
285 default: Done = false; break;
286 case '$': // $$ -> $
287 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
288 OS << '$';
289 ++LastEmitted; // Consume second '$' character.
290 break;
291 case '(': // $( -> same as GCC's { character.
292 ++LastEmitted; // Consume '(' character.
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000293 if (CurVariant != -1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000294 report_fatal_error("Nested variants found in inline asm string: '" +
295 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000296 CurVariant = 0; // We're in the first variant now.
297 break;
298 case '|':
299 ++LastEmitted; // consume '|' character.
300 if (CurVariant == -1)
301 OS << '|'; // this is gcc's behavior for | outside a variant
302 else
303 ++CurVariant; // We're in the next variant.
304 break;
305 case ')': // $) -> same as GCC's } char.
306 ++LastEmitted; // consume ')' character.
307 if (CurVariant == -1)
308 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach00915352010-10-01 23:29:12 +0000309 else
Chris Lattner1e158692010-04-04 18:34:07 +0000310 CurVariant = -1;
311 break;
312 }
313 if (Done) break;
Jim Grosbach00915352010-10-01 23:29:12 +0000314
Chris Lattner1e158692010-04-04 18:34:07 +0000315 bool HasCurlyBraces = false;
316 if (*LastEmitted == '{') { // ${variable}
317 ++LastEmitted; // Consume '{' character.
318 HasCurlyBraces = true;
319 }
Jim Grosbach00915352010-10-01 23:29:12 +0000320
Chris Lattner1e158692010-04-04 18:34:07 +0000321 // If we have ${:foo}, then this is not a real operand reference, it is a
322 // "magic" string reference, just like in .td files. Arrange to call
323 // PrintSpecial.
324 if (HasCurlyBraces && *LastEmitted == ':') {
325 ++LastEmitted;
326 const char *StrStart = LastEmitted;
327 const char *StrEnd = strchr(StrStart, '}');
328 if (StrEnd == 0)
Benjamin Kramera6769262010-04-08 10:44:28 +0000329 report_fatal_error("Unterminated ${:foo} operand in inline asm"
330 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000331
Chris Lattner1e158692010-04-04 18:34:07 +0000332 std::string Val(StrStart, StrEnd);
Chad Rosier17788312012-09-11 19:09:56 +0000333 AP->PrintSpecial(MI, OS, Val.c_str());
Chris Lattner1e158692010-04-04 18:34:07 +0000334 LastEmitted = StrEnd+1;
335 break;
336 }
Jim Grosbach00915352010-10-01 23:29:12 +0000337
Chris Lattner1e158692010-04-04 18:34:07 +0000338 const char *IDStart = LastEmitted;
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000339 const char *IDEnd = IDStart;
Jim Grosbach00915352010-10-01 23:29:12 +0000340 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
341
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000342 unsigned Val;
343 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramera6769262010-04-08 10:44:28 +0000344 report_fatal_error("Bad $ operand number in inline asm string: '" +
345 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000346 LastEmitted = IDEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000347
Chris Lattner1e158692010-04-04 18:34:07 +0000348 char Modifier[2] = { 0, 0 };
Jim Grosbach00915352010-10-01 23:29:12 +0000349
Chris Lattner1e158692010-04-04 18:34:07 +0000350 if (HasCurlyBraces) {
351 // If we have curly braces, check for a modifier character. This
352 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
353 if (*LastEmitted == ':') {
354 ++LastEmitted; // Consume ':' character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000355 if (*LastEmitted == 0)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000356 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramera6769262010-04-08 10:44:28 +0000357 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000358
Chris Lattner1e158692010-04-04 18:34:07 +0000359 Modifier[0] = *LastEmitted;
360 ++LastEmitted; // Consume modifier character.
361 }
Jim Grosbach00915352010-10-01 23:29:12 +0000362
Chris Lattner0e45d242010-04-05 22:42:30 +0000363 if (*LastEmitted != '}')
Benjamin Kramera6769262010-04-08 10:44:28 +0000364 report_fatal_error("Bad ${} expression in inline asm string: '" +
365 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000366 ++LastEmitted; // Consume '}' character.
367 }
Jim Grosbach00915352010-10-01 23:29:12 +0000368
Chris Lattner0e45d242010-04-05 22:42:30 +0000369 if (Val >= NumOperands-1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000370 report_fatal_error("Invalid $ operand number in inline asm string: '" +
371 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000372
Chris Lattner1e158692010-04-04 18:34:07 +0000373 // Okay, we finally have a value number. Ask the target to print this
374 // operand!
375 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Cheng6eb516d2011-01-07 23:50:32 +0000376 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner1e158692010-04-04 18:34:07 +0000377
378 bool Error = false;
379
380 // Scan to find the machine operand number for the operand.
381 for (; Val; --Val) {
382 if (OpNo >= MI->getNumOperands()) break;
383 unsigned OpFlags = MI->getOperand(OpNo).getImm();
384 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
385 }
386
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000387 // We may have a location metadata attached to the end of the
388 // instruction, and at no point should see metadata at any
389 // other point while processing. It's an error if so.
Eric Christopher12da1692012-03-22 01:33:51 +0000390 if (OpNo >= MI->getNumOperands() ||
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000391 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner1e158692010-04-04 18:34:07 +0000392 Error = true;
393 } else {
394 unsigned OpFlags = MI->getOperand(OpNo).getImm();
395 ++OpNo; // Skip over the ID number.
396
397 if (Modifier[0] == 'l') // labels are target independent
398 // FIXME: What if the operand isn't an MBB, report error?
399 OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
400 else {
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000401 if (InlineAsm::isMemKind(OpFlags)) {
Chad Rosierdb20a412012-09-10 21:10:49 +0000402 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Chris Lattner1e158692010-04-04 18:34:07 +0000403 Modifier[0] ? Modifier : 0,
404 OS);
405 } else {
Chad Rosierdb20a412012-09-10 21:10:49 +0000406 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Chris Lattner1e158692010-04-04 18:34:07 +0000407 Modifier[0] ? Modifier : 0, OS);
408 }
409 }
410 }
411 if (Error) {
412 std::string msg;
413 raw_string_ostream Msg(msg);
Chris Lattner1e457892010-04-07 23:40:44 +0000414 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
415 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner1e158692010-04-04 18:34:07 +0000416 }
417 }
418 break;
419 }
420 }
421 }
Chad Rosier17788312012-09-11 19:09:56 +0000422 OS << '\n' << (char)0; // null terminate string.
423}
424
425/// EmitInlineAsm - This method formats and emits the specified machine
426/// instruction that is an inline asm.
427void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
428 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
429
430 // Count the number of register definitions to find the asm string.
431 unsigned NumDefs = 0;
432 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
433 ++NumDefs)
434 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
435
436 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
437
438 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
439 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
440
441 // If this asmstr is empty, just print the #APP/#NOAPP markers.
442 // These are useful to see where empty asm's wound up.
443 if (AsmStr[0] == 0) {
Rafael Espindola0b694812014-01-16 16:28:37 +0000444 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
445 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chad Rosier17788312012-09-11 19:09:56 +0000446 return;
Chad Rosier7641f582012-09-10 21:36:05 +0000447 }
448
Chad Rosier17788312012-09-11 19:09:56 +0000449 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000450 // enabled, so we use emitRawComment.
451 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
Chad Rosier17788312012-09-11 19:09:56 +0000452
453 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
454 // it.
455 unsigned LocCookie = 0;
456 const MDNode *LocMD = 0;
457 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
458 if (MI->getOperand(i-1).isMetadata() &&
459 (LocMD = MI->getOperand(i-1).getMetadata()) &&
460 LocMD->getNumOperands() != 0) {
461 if (const ConstantInt *CI = dyn_cast<ConstantInt>(LocMD->getOperand(0))) {
462 LocCookie = CI->getZExtValue();
463 break;
464 }
465 }
466 }
467
468 // Emit the inline asm to a temporary string so we can emit it through
469 // EmitInlineAsm.
470 SmallString<256> StringData;
471 raw_svector_ostream OS(StringData);
472
473 // The variant of the current asmprinter.
474 int AsmPrinterVariant = MAI->getAssemblerDialect();
475 InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
476 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
477 if (InlineAsmVariant == InlineAsm::AD_ATT)
478 EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
479 AP, LocCookie, OS);
480 else
481 EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
482
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000483 EmitInlineAsm(OS.str(), LocMD, MI->getInlineAsmDialect());
Jim Grosbach00915352010-10-01 23:29:12 +0000484
Chris Lattner1e158692010-04-04 18:34:07 +0000485 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000486 // enabled, so we use emitRawComment.
487 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chris Lattner1e158692010-04-04 18:34:07 +0000488}
489
490
491/// PrintSpecial - Print information related to the specified machine instr
492/// that is independent of the operand, and may be independent of the instr
493/// itself. This can be useful for portably encoding the comment character
494/// or other bits of target-specific knowledge into the asmstrings. The
495/// syntax used is ${:comment}. Targets can override this to add support
496/// for their own strange codes.
497void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
498 const char *Code) const {
Rafael Espindola58873562014-01-03 19:21:54 +0000499 const DataLayout *DL = TM.getDataLayout();
Chris Lattner1e158692010-04-04 18:34:07 +0000500 if (!strcmp(Code, "private")) {
Rafael Espindola58873562014-01-03 19:21:54 +0000501 OS << DL->getPrivateGlobalPrefix();
Chris Lattner1e158692010-04-04 18:34:07 +0000502 } else if (!strcmp(Code, "comment")) {
503 OS << MAI->getCommentString();
504 } else if (!strcmp(Code, "uid")) {
505 // Comparing the address of MI isn't sufficient, because machineinstrs may
506 // be allocated to the same address across functions.
Jim Grosbach00915352010-10-01 23:29:12 +0000507
Chris Lattner1e158692010-04-04 18:34:07 +0000508 // If this is a new LastFn instruction, bump the counter.
509 if (LastMI != MI || LastFn != getFunctionNumber()) {
510 ++Counter;
511 LastMI = MI;
512 LastFn = getFunctionNumber();
513 }
514 OS << Counter;
515 } else {
516 std::string msg;
517 raw_string_ostream Msg(msg);
518 Msg << "Unknown special formatter '" << Code
519 << "' for machine instr: " << *MI;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000520 report_fatal_error(Msg.str());
Jim Grosbach00915352010-10-01 23:29:12 +0000521 }
Chris Lattner1e158692010-04-04 18:34:07 +0000522}
523
524/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
525/// instruction, using the specified assembler variant. Targets should
526/// override this to format as appropriate.
527bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chad Rosier1f57bcb2012-09-07 20:23:29 +0000528 unsigned AsmVariant, const char *ExtraCode,
529 raw_ostream &O) {
Jack Carterb2fd5f62012-06-21 17:14:46 +0000530 // Does this asm operand have a single letter operand modifier?
531 if (ExtraCode && ExtraCode[0]) {
532 if (ExtraCode[1] != 0) return true; // Unknown modifier.
533
534 const MachineOperand &MO = MI->getOperand(OpNo);
535 switch (ExtraCode[0]) {
536 default:
537 return true; // Unknown modifier.
538 case 'c': // Substitute immediate value without immediate syntax
Jack Carterc457f622012-06-21 21:37:54 +0000539 if (MO.getType() != MachineOperand::MO_Immediate)
Jack Carterb2fd5f62012-06-21 17:14:46 +0000540 return true;
541 O << MO.getImm();
542 return false;
Jack Carterc457f622012-06-21 21:37:54 +0000543 case 'n': // Negate the immediate constant.
544 if (MO.getType() != MachineOperand::MO_Immediate)
545 return true;
546 O << -MO.getImm();
547 return false;
Jack Carterb2fd5f62012-06-21 17:14:46 +0000548 }
549 }
Chris Lattner1e158692010-04-04 18:34:07 +0000550 return true;
551}
552
553bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
554 unsigned AsmVariant,
555 const char *ExtraCode, raw_ostream &O) {
556 // Target doesn't support this yet!
557 return true;
558}
559
Rafael Espindola65fd0a82014-01-24 15:47:54 +0000560void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
David Peixottoea2bcb92014-02-06 18:19:40 +0000561 const MCSubtargetInfo *EndInfo) const {}