blob: 10a56af5d32e5545757b4f2a21a104d17a2c39b4 [file] [log] [blame]
Chris Lattner7e1a8f82010-04-04 19:09:29 +00001//===-- AsmPrinterInlineAsm.cpp - AsmPrinter Inline Asm Handling ----------===//
Chris Lattner736e31d2010-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 Lattner7e1a8f82010-04-04 19:09:29 +000010// This file implements the inline assembler pieces of the AsmPrinter class.
Chris Lattner736e31d2010-04-04 18:34:07 +000011//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "asm-printer"
Chris Lattner736e31d2010-04-04 18:34:07 +000015#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruthd04a8d42012-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"
Chris Lattnercf9a4152010-04-07 05:38:05 +000021#include "llvm/Constants.h"
Chris Lattner7e1a8f82010-04-04 19:09:29 +000022#include "llvm/InlineAsm.h"
Chris Lattner6eb78062010-04-06 00:55:39 +000023#include "llvm/LLVMContext.h"
Chris Lattner736e31d2010-04-04 18:34:07 +000024#include "llvm/MC/MCAsmInfo.h"
25#include "llvm/MC/MCStreamer.h"
Evan Chengffc0e732011-07-09 05:47:46 +000026#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner736e31d2010-04-04 18:34:07 +000027#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000028#include "llvm/MC/MCTargetAsmParser.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/Module.h"
Chris Lattner736e31d2010-04-04 18:34:07 +000030#include "llvm/Support/ErrorHandling.h"
Chris Lattneraf632c92010-04-05 23:11:24 +000031#include "llvm/Support/MemoryBuffer.h"
32#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000033#include "llvm/Support/TargetRegistry.h"
Chris Lattner736e31d2010-04-04 18:34:07 +000034#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000035#include "llvm/Target/TargetMachine.h"
Chris Lattner736e31d2010-04-04 18:34:07 +000036using namespace llvm;
37
Chris Lattner6e30c6a2010-11-17 08:03:32 +000038namespace {
39 struct SrcMgrDiagInfo {
40 const MDNode *LocInfo;
Chris Lattner4afa1282010-11-17 08:13:01 +000041 LLVMContext::InlineAsmDiagHandlerTy DiagHandler;
Chris Lattner6e30c6a2010-11-17 08:03:32 +000042 void *DiagContext;
43 };
44}
45
Chad Rosierc53ade22012-09-07 18:16:38 +000046/// srcMgrDiagHandler - This callback is invoked when the SourceMgr for an
Chris Lattner6e30c6a2010-11-17 08:03:32 +000047/// inline asm has an error in it. diagInfo is a pointer to the SrcMgrDiagInfo
48/// struct above.
Chad Rosierc53ade22012-09-07 18:16:38 +000049static void srcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
Chris Lattner6e30c6a2010-11-17 08:03:32 +000050 SrcMgrDiagInfo *DiagInfo = static_cast<SrcMgrDiagInfo *>(diagInfo);
51 assert(DiagInfo && "Diagnostic context not passed down?");
Jim Grosbachfe59d852011-09-21 21:36:53 +000052
Chris Lattnerce1b9ad2010-11-17 08:20:42 +000053 // If the inline asm had metadata associated with it, pull out a location
54 // cookie corresponding to which line the error occurred on.
Chris Lattner6e30c6a2010-11-17 08:03:32 +000055 unsigned LocCookie = 0;
Chris Lattnerce1b9ad2010-11-17 08:20:42 +000056 if (const MDNode *LocInfo = DiagInfo->LocInfo) {
57 unsigned ErrorLine = Diag.getLineNo()-1;
58 if (ErrorLine >= LocInfo->getNumOperands())
59 ErrorLine = 0;
Jim Grosbachfe59d852011-09-21 21:36:53 +000060
Chris Lattnerce1b9ad2010-11-17 08:20:42 +000061 if (LocInfo->getNumOperands() != 0)
62 if (const ConstantInt *CI =
63 dyn_cast<ConstantInt>(LocInfo->getOperand(ErrorLine)))
Chris Lattner6e30c6a2010-11-17 08:03:32 +000064 LocCookie = CI->getZExtValue();
Chris Lattnerce1b9ad2010-11-17 08:20:42 +000065 }
Jim Grosbachfe59d852011-09-21 21:36:53 +000066
Chris Lattner4afa1282010-11-17 08:13:01 +000067 DiagInfo->DiagHandler(Diag, DiagInfo->DiagContext, LocCookie);
Chris Lattner6e30c6a2010-11-17 08:03:32 +000068}
69
Chris Lattner736e31d2010-04-04 18:34:07 +000070/// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
Chad Rosier366df792012-09-05 23:57:37 +000071void AsmPrinter::EmitInlineAsm(StringRef Str, const MDNode *LocMDNode,
72 InlineAsm::AsmDialect Dialect) const {
Chris Lattner736e31d2010-04-04 18:34:07 +000073 assert(!Str.empty() && "Can't emit empty inline asm block");
Jim Grosbach2b2de242010-10-01 23:29:12 +000074
Chris Lattneraf632c92010-04-05 23:11:24 +000075 // Remember if the buffer is nul terminated or not so we can avoid a copy.
76 bool isNullTerminated = Str.back() == 0;
77 if (isNullTerminated)
78 Str = Str.substr(0, Str.size()-1);
Jim Grosbach2b2de242010-10-01 23:29:12 +000079
Chris Lattner736e31d2010-04-04 18:34:07 +000080 // If the output streamer is actually a .s file, just emit the blob textually.
81 // This is useful in case the asm parser doesn't handle something but the
82 // system assembler does.
83 if (OutStreamer.hasRawTextSupport()) {
84 OutStreamer.EmitRawText(Str);
85 return;
86 }
Jim Grosbach2b2de242010-10-01 23:29:12 +000087
Chris Lattneraf632c92010-04-05 23:11:24 +000088 SourceMgr SrcMgr;
Chris Lattner6e30c6a2010-11-17 08:03:32 +000089 SrcMgrDiagInfo DiagInfo;
Jim Grosbach2b2de242010-10-01 23:29:12 +000090
Chris Lattner6eb78062010-04-06 00:55:39 +000091 // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
92 LLVMContext &LLVMCtx = MMI->getModule()->getContext();
93 bool HasDiagHandler = false;
Chris Lattner4afa1282010-11-17 08:13:01 +000094 if (LLVMCtx.getInlineAsmDiagnosticHandler() != 0) {
Chad Rosierc53ade22012-09-07 18:16:38 +000095 // If the source manager has an issue, we arrange for srcMgrDiagHandler
Chris Lattner6e30c6a2010-11-17 08:03:32 +000096 // to be invoked, getting DiagInfo passed into it.
97 DiagInfo.LocInfo = LocMDNode;
Chris Lattner4afa1282010-11-17 08:13:01 +000098 DiagInfo.DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
Chris Lattner6e30c6a2010-11-17 08:03:32 +000099 DiagInfo.DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
Chad Rosierc53ade22012-09-07 18:16:38 +0000100 SrcMgr.setDiagHandler(srcMgrDiagHandler, &DiagInfo);
Chris Lattner6eb78062010-04-06 00:55:39 +0000101 HasDiagHandler = true;
102 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000103
Chris Lattneraf632c92010-04-05 23:11:24 +0000104 MemoryBuffer *Buffer;
105 if (isNullTerminated)
106 Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
107 else
108 Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
109
110 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
111 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
Jim Grosbach2b2de242010-10-01 23:29:12 +0000112
Jim Grosbach1b84cce2011-08-16 18:33:49 +0000113 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
Daniel Dunbar9fbb37e2010-07-18 18:31:33 +0000114 OutContext, OutStreamer,
115 *MAI));
Evan Chengebdeeab2011-07-08 01:53:10 +0000116
Evan Chengffc0e732011-07-09 05:47:46 +0000117 // FIXME: It would be nice if we can avoid createing a new instance of
118 // MCSubtargetInfo here given TargetSubtargetInfo is available. However,
119 // we have to watch out for asm directives which can change subtarget
120 // state. e.g. .code 16, .code 32.
121 OwningPtr<MCSubtargetInfo>
122 STI(TM.getTarget().createMCSubtargetInfo(TM.getTargetTriple(),
123 TM.getTargetCPU(),
124 TM.getTargetFeatureString()));
Evan Cheng94b95502011-07-26 00:24:13 +0000125 OwningPtr<MCTargetAsmParser>
126 TAP(TM.getTarget().createMCAsmParser(*STI, *Parser));
Chris Lattneraf632c92010-04-05 23:11:24 +0000127 if (!TAP)
Chris Lattner75361b62010-04-07 22:58:41 +0000128 report_fatal_error("Inline asm not supported by this streamer because"
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000129 " we don't have an asm parser for this target\n");
Chad Rosier366df792012-09-05 23:57:37 +0000130 Parser->setAssemblerDialect(Dialect);
Daniel Dunbar9fbb37e2010-07-18 18:31:33 +0000131 Parser->setTargetParser(*TAP.get());
Chris Lattneraf632c92010-04-05 23:11:24 +0000132
133 // Don't implicitly switch to the text section before the asm.
Daniel Dunbar9fbb37e2010-07-18 18:31:33 +0000134 int Res = Parser->Run(/*NoInitialTextSection*/ true,
135 /*NoFinalize*/ true);
Chris Lattner6eb78062010-04-06 00:55:39 +0000136 if (Res && !HasDiagHandler)
Chris Lattner75361b62010-04-07 22:58:41 +0000137 report_fatal_error("Error parsing inline asm\n");
Chris Lattner736e31d2010-04-04 18:34:07 +0000138}
139
Chad Rosiera7b159c2012-09-11 19:09:56 +0000140static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
141 MachineModuleInfo *MMI, int InlineAsmVariant,
142 AsmPrinter *AP, unsigned LocCookie,
143 raw_ostream &OS) {
144 // Switch to the inline assembly variant.
145 OS << "\t.intel_syntax\n\t";
Chris Lattner736e31d2010-04-04 18:34:07 +0000146
Chad Rosiera7b159c2012-09-11 19:09:56 +0000147 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner736e31d2010-04-04 18:34:07 +0000148 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach2b2de242010-10-01 23:29:12 +0000149
Chad Rosiera7b159c2012-09-11 19:09:56 +0000150 while (*LastEmitted) {
151 switch (*LastEmitted) {
152 default: {
153 // Not a special case, emit the string section literally.
154 const char *LiteralEnd = LastEmitted+1;
155 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
156 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
157 ++LiteralEnd;
Jim Grosbach2b2de242010-10-01 23:29:12 +0000158
Chad Rosiera7b159c2012-09-11 19:09:56 +0000159 OS.write(LastEmitted, LiteralEnd-LastEmitted);
160 LastEmitted = LiteralEnd;
161 break;
162 }
163 case '\n':
164 ++LastEmitted; // Consume newline character.
165 OS << '\n'; // Indent code with newline.
166 break;
167 case '$': {
168 ++LastEmitted; // Consume '$' character.
169 bool Done = true;
Chris Lattner736e31d2010-04-04 18:34:07 +0000170
Chad Rosiera7b159c2012-09-11 19:09:56 +0000171 // Handle escapes.
172 switch (*LastEmitted) {
173 default: Done = false; break;
174 case '$':
175 ++LastEmitted; // Consume second '$' character.
Chris Lattnera38941d2010-11-17 07:53:40 +0000176 break;
177 }
Chad Rosiera7b159c2012-09-11 19:09:56 +0000178 if (Done) break;
179
180 const char *IDStart = LastEmitted;
181 const char *IDEnd = IDStart;
182 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
183
184 unsigned Val;
185 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
186 report_fatal_error("Bad $ operand number in inline asm string: '" +
187 Twine(AsmStr) + "'");
188 LastEmitted = IDEnd;
189
190 if (Val >= NumOperands-1)
191 report_fatal_error("Invalid $ operand number in inline asm string: '" +
192 Twine(AsmStr) + "'");
193
194 // Okay, we finally have a value number. Ask the target to print this
195 // operand!
196 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
197
198 bool Error = false;
199
200 // Scan to find the machine operand number for the operand.
201 for (; Val; --Val) {
202 if (OpNo >= MI->getNumOperands()) break;
203 unsigned OpFlags = MI->getOperand(OpNo).getImm();
204 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
205 }
206
207 // We may have a location metadata attached to the end of the
208 // instruction, and at no point should see metadata at any
209 // other point while processing. It's an error if so.
210 if (OpNo >= MI->getNumOperands() ||
211 MI->getOperand(OpNo).isMetadata()) {
212 Error = true;
213 } else {
214 unsigned OpFlags = MI->getOperand(OpNo).getImm();
215 ++OpNo; // Skip over the ID number.
216
217 if (InlineAsm::isMemKind(OpFlags)) {
218 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
219 /*Modifier*/ 0, OS);
220 } else {
221 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
222 /*Modifier*/ 0, OS);
223 }
224 }
225 if (Error) {
226 std::string msg;
227 raw_string_ostream Msg(msg);
228 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
229 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
230 }
231 break;
232 }
Chris Lattnera38941d2010-11-17 07:53:40 +0000233 }
Chris Lattnercf9a4152010-04-07 05:38:05 +0000234 }
Chad Rosiera7b159c2012-09-11 19:09:56 +0000235 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
236}
Jim Grosbach2b2de242010-10-01 23:29:12 +0000237
Chad Rosiera7b159c2012-09-11 19:09:56 +0000238static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
239 MachineModuleInfo *MMI, int InlineAsmVariant,
240 int AsmPrinterVariant, AsmPrinter *AP,
241 unsigned LocCookie, raw_ostream &OS) {
Chris Lattner736e31d2010-04-04 18:34:07 +0000242 int CurVariant = -1; // The number of the {.|.|.} region we are in.
243 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chad Rosiera7b159c2012-09-11 19:09:56 +0000244 unsigned NumOperands = MI->getNumOperands();
245
246 OS << '\t';
Jim Grosbach2b2de242010-10-01 23:29:12 +0000247
Chris Lattner736e31d2010-04-04 18:34:07 +0000248 while (*LastEmitted) {
249 switch (*LastEmitted) {
250 default: {
251 // Not a special case, emit the string section literally.
252 const char *LiteralEnd = LastEmitted+1;
253 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
254 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
255 ++LiteralEnd;
256 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
257 OS.write(LastEmitted, LiteralEnd-LastEmitted);
258 LastEmitted = LiteralEnd;
259 break;
260 }
261 case '\n':
262 ++LastEmitted; // Consume newline character.
Chris Lattner4c842dd2010-04-05 22:42:30 +0000263 OS << '\n'; // Indent code with newline.
Chris Lattner736e31d2010-04-04 18:34:07 +0000264 break;
265 case '$': {
266 ++LastEmitted; // Consume '$' character.
267 bool Done = true;
268
269 // Handle escapes.
270 switch (*LastEmitted) {
271 default: Done = false; break;
272 case '$': // $$ -> $
273 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
274 OS << '$';
275 ++LastEmitted; // Consume second '$' character.
276 break;
277 case '(': // $( -> same as GCC's { character.
278 ++LastEmitted; // Consume '(' character.
Chris Lattnerfee455e2010-04-07 05:27:36 +0000279 if (CurVariant != -1)
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000280 report_fatal_error("Nested variants found in inline asm string: '" +
281 Twine(AsmStr) + "'");
Chris Lattner736e31d2010-04-04 18:34:07 +0000282 CurVariant = 0; // We're in the first variant now.
283 break;
284 case '|':
285 ++LastEmitted; // consume '|' character.
286 if (CurVariant == -1)
287 OS << '|'; // this is gcc's behavior for | outside a variant
288 else
289 ++CurVariant; // We're in the next variant.
290 break;
291 case ')': // $) -> same as GCC's } char.
292 ++LastEmitted; // consume ')' character.
293 if (CurVariant == -1)
294 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach2b2de242010-10-01 23:29:12 +0000295 else
Chris Lattner736e31d2010-04-04 18:34:07 +0000296 CurVariant = -1;
297 break;
298 }
299 if (Done) break;
Jim Grosbach2b2de242010-10-01 23:29:12 +0000300
Chris Lattner736e31d2010-04-04 18:34:07 +0000301 bool HasCurlyBraces = false;
302 if (*LastEmitted == '{') { // ${variable}
303 ++LastEmitted; // Consume '{' character.
304 HasCurlyBraces = true;
305 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000306
Chris Lattner736e31d2010-04-04 18:34:07 +0000307 // If we have ${:foo}, then this is not a real operand reference, it is a
308 // "magic" string reference, just like in .td files. Arrange to call
309 // PrintSpecial.
310 if (HasCurlyBraces && *LastEmitted == ':') {
311 ++LastEmitted;
312 const char *StrStart = LastEmitted;
313 const char *StrEnd = strchr(StrStart, '}');
314 if (StrEnd == 0)
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000315 report_fatal_error("Unterminated ${:foo} operand in inline asm"
316 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000317
Chris Lattner736e31d2010-04-04 18:34:07 +0000318 std::string Val(StrStart, StrEnd);
Chad Rosiera7b159c2012-09-11 19:09:56 +0000319 AP->PrintSpecial(MI, OS, Val.c_str());
Chris Lattner736e31d2010-04-04 18:34:07 +0000320 LastEmitted = StrEnd+1;
321 break;
322 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000323
Chris Lattner736e31d2010-04-04 18:34:07 +0000324 const char *IDStart = LastEmitted;
Chris Lattner65eeaad2010-04-04 18:42:18 +0000325 const char *IDEnd = IDStart;
Jim Grosbach2b2de242010-10-01 23:29:12 +0000326 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
327
Chris Lattner65eeaad2010-04-04 18:42:18 +0000328 unsigned Val;
329 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000330 report_fatal_error("Bad $ operand number in inline asm string: '" +
331 Twine(AsmStr) + "'");
Chris Lattner736e31d2010-04-04 18:34:07 +0000332 LastEmitted = IDEnd;
Jim Grosbach2b2de242010-10-01 23:29:12 +0000333
Chris Lattner736e31d2010-04-04 18:34:07 +0000334 char Modifier[2] = { 0, 0 };
Jim Grosbach2b2de242010-10-01 23:29:12 +0000335
Chris Lattner736e31d2010-04-04 18:34:07 +0000336 if (HasCurlyBraces) {
337 // If we have curly braces, check for a modifier character. This
338 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
339 if (*LastEmitted == ':') {
340 ++LastEmitted; // Consume ':' character.
Chris Lattner4c842dd2010-04-05 22:42:30 +0000341 if (*LastEmitted == 0)
Chris Lattner75361b62010-04-07 22:58:41 +0000342 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000343 Twine(AsmStr) + "'");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000344
Chris Lattner736e31d2010-04-04 18:34:07 +0000345 Modifier[0] = *LastEmitted;
346 ++LastEmitted; // Consume modifier character.
347 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000348
Chris Lattner4c842dd2010-04-05 22:42:30 +0000349 if (*LastEmitted != '}')
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000350 report_fatal_error("Bad ${} expression in inline asm string: '" +
351 Twine(AsmStr) + "'");
Chris Lattner736e31d2010-04-04 18:34:07 +0000352 ++LastEmitted; // Consume '}' character.
353 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000354
Chris Lattner4c842dd2010-04-05 22:42:30 +0000355 if (Val >= NumOperands-1)
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000356 report_fatal_error("Invalid $ operand number in inline asm string: '" +
357 Twine(AsmStr) + "'");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000358
Chris Lattner736e31d2010-04-04 18:34:07 +0000359 // Okay, we finally have a value number. Ask the target to print this
360 // operand!
361 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Chengc36b7062011-01-07 23:50:32 +0000362 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner736e31d2010-04-04 18:34:07 +0000363
364 bool Error = false;
365
366 // Scan to find the machine operand number for the operand.
367 for (; Val; --Val) {
368 if (OpNo >= MI->getNumOperands()) break;
369 unsigned OpFlags = MI->getOperand(OpNo).getImm();
370 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
371 }
372
Akira Hatanaka9318e482012-05-08 19:14:42 +0000373 // We may have a location metadata attached to the end of the
374 // instruction, and at no point should see metadata at any
375 // other point while processing. It's an error if so.
Eric Christopheraa206ff2012-03-22 01:33:51 +0000376 if (OpNo >= MI->getNumOperands() ||
Akira Hatanaka9318e482012-05-08 19:14:42 +0000377 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner736e31d2010-04-04 18:34:07 +0000378 Error = true;
379 } else {
380 unsigned OpFlags = MI->getOperand(OpNo).getImm();
381 ++OpNo; // Skip over the ID number.
382
383 if (Modifier[0] == 'l') // labels are target independent
384 // FIXME: What if the operand isn't an MBB, report error?
385 OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
386 else {
Chris Lattnerfee455e2010-04-07 05:27:36 +0000387 if (InlineAsm::isMemKind(OpFlags)) {
Chad Rosier3b132fa2012-09-10 21:10:49 +0000388 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Chris Lattner736e31d2010-04-04 18:34:07 +0000389 Modifier[0] ? Modifier : 0,
390 OS);
391 } else {
Chad Rosier3b132fa2012-09-10 21:10:49 +0000392 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Chris Lattner736e31d2010-04-04 18:34:07 +0000393 Modifier[0] ? Modifier : 0, OS);
394 }
395 }
396 }
397 if (Error) {
398 std::string msg;
399 raw_string_ostream Msg(msg);
Chris Lattner38686bd2010-04-07 23:40:44 +0000400 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
401 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner736e31d2010-04-04 18:34:07 +0000402 }
403 }
404 break;
405 }
406 }
407 }
Chad Rosiera7b159c2012-09-11 19:09:56 +0000408 OS << '\n' << (char)0; // null terminate string.
409}
410
411/// EmitInlineAsm - This method formats and emits the specified machine
412/// instruction that is an inline asm.
413void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
414 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
415
416 // Count the number of register definitions to find the asm string.
417 unsigned NumDefs = 0;
418 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
419 ++NumDefs)
420 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
421
422 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
423
424 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
425 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
426
427 // If this asmstr is empty, just print the #APP/#NOAPP markers.
428 // These are useful to see where empty asm's wound up.
429 if (AsmStr[0] == 0) {
430 // Don't emit the comments if writing to a .o file.
431 if (!OutStreamer.hasRawTextSupport()) return;
432
433 OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
434 MAI->getInlineAsmStart());
435 OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
436 MAI->getInlineAsmEnd());
437 return;
Chad Rosier24f5fdd2012-09-10 21:36:05 +0000438 }
439
Chad Rosiera7b159c2012-09-11 19:09:56 +0000440 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
441 // enabled, so we use EmitRawText.
442 if (OutStreamer.hasRawTextSupport())
443 OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
444 MAI->getInlineAsmStart());
445
446 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
447 // it.
448 unsigned LocCookie = 0;
449 const MDNode *LocMD = 0;
450 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
451 if (MI->getOperand(i-1).isMetadata() &&
452 (LocMD = MI->getOperand(i-1).getMetadata()) &&
453 LocMD->getNumOperands() != 0) {
454 if (const ConstantInt *CI = dyn_cast<ConstantInt>(LocMD->getOperand(0))) {
455 LocCookie = CI->getZExtValue();
456 break;
457 }
458 }
459 }
460
461 // Emit the inline asm to a temporary string so we can emit it through
462 // EmitInlineAsm.
463 SmallString<256> StringData;
464 raw_svector_ostream OS(StringData);
465
466 // The variant of the current asmprinter.
467 int AsmPrinterVariant = MAI->getAssemblerDialect();
468 InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
469 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
470 if (InlineAsmVariant == InlineAsm::AD_ATT)
471 EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
472 AP, LocCookie, OS);
473 else
474 EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
475
Chad Rosier366df792012-09-05 23:57:37 +0000476 EmitInlineAsm(OS.str(), LocMD, MI->getInlineAsmDialect());
Jim Grosbach2b2de242010-10-01 23:29:12 +0000477
Chris Lattner736e31d2010-04-04 18:34:07 +0000478 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
479 // enabled, so we use EmitRawText.
480 if (OutStreamer.hasRawTextSupport())
481 OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
482 MAI->getInlineAsmEnd());
483}
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 {
494 if (!strcmp(Code, "private")) {
495 OS << MAI->getPrivateGlobalPrefix();
496 } else if (!strcmp(Code, "comment")) {
497 OS << MAI->getCommentString();
498 } else if (!strcmp(Code, "uid")) {
499 // Comparing the address of MI isn't sufficient, because machineinstrs may
500 // be allocated to the same address across functions.
Jim Grosbach2b2de242010-10-01 23:29:12 +0000501
Chris Lattner736e31d2010-04-04 18:34:07 +0000502 // If this is a new LastFn instruction, bump the counter.
503 if (LastMI != MI || LastFn != getFunctionNumber()) {
504 ++Counter;
505 LastMI = MI;
506 LastFn = getFunctionNumber();
507 }
508 OS << Counter;
509 } else {
510 std::string msg;
511 raw_string_ostream Msg(msg);
512 Msg << "Unknown special formatter '" << Code
513 << "' for machine instr: " << *MI;
Chris Lattner75361b62010-04-07 22:58:41 +0000514 report_fatal_error(Msg.str());
Jim Grosbach2b2de242010-10-01 23:29:12 +0000515 }
Chris Lattner736e31d2010-04-04 18:34:07 +0000516}
517
518/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
519/// instruction, using the specified assembler variant. Targets should
520/// override this to format as appropriate.
521bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chad Rosier26c5d5f2012-09-07 20:23:29 +0000522 unsigned AsmVariant, const char *ExtraCode,
523 raw_ostream &O) {
Jack Carterd5e11ad2012-06-21 17:14:46 +0000524 // Does this asm operand have a single letter operand modifier?
525 if (ExtraCode && ExtraCode[0]) {
526 if (ExtraCode[1] != 0) return true; // Unknown modifier.
527
528 const MachineOperand &MO = MI->getOperand(OpNo);
529 switch (ExtraCode[0]) {
530 default:
531 return true; // Unknown modifier.
532 case 'c': // Substitute immediate value without immediate syntax
Jack Carter4db98be2012-06-21 21:37:54 +0000533 if (MO.getType() != MachineOperand::MO_Immediate)
Jack Carterd5e11ad2012-06-21 17:14:46 +0000534 return true;
535 O << MO.getImm();
536 return false;
Jack Carter4db98be2012-06-21 21:37:54 +0000537 case 'n': // Negate the immediate constant.
538 if (MO.getType() != MachineOperand::MO_Immediate)
539 return true;
540 O << -MO.getImm();
541 return false;
Jack Carterd5e11ad2012-06-21 17:14:46 +0000542 }
543 }
Chris Lattner736e31d2010-04-04 18:34:07 +0000544 return true;
545}
546
547bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
548 unsigned AsmVariant,
549 const char *ExtraCode, raw_ostream &O) {
550 // Target doesn't support this yet!
551 return true;
552}
553