blob: 260871d33b0755f5365d4d6852241b1ed45ae83e [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"
Chris Lattnercf9a4152010-04-07 05:38:05 +000016#include "llvm/Constants.h"
Chris Lattner7e1a8f82010-04-04 19:09:29 +000017#include "llvm/InlineAsm.h"
Chris Lattner6eb78062010-04-06 00:55:39 +000018#include "llvm/LLVMContext.h"
19#include "llvm/Module.h"
Chris Lattner736e31d2010-04-04 18:34:07 +000020#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner6eb78062010-04-06 00:55:39 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner736e31d2010-04-04 18:34:07 +000022#include "llvm/MC/MCAsmInfo.h"
23#include "llvm/MC/MCStreamer.h"
Evan Chengffc0e732011-07-09 05:47:46 +000024#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner736e31d2010-04-04 18:34:07 +000025#include "llvm/MC/MCSymbol.h"
Evan Cheng94b95502011-07-26 00:24:13 +000026#include "llvm/MC/MCTargetAsmParser.h"
Chris Lattneraf632c92010-04-05 23:11:24 +000027#include "llvm/Target/TargetMachine.h"
Chris Lattneraf632c92010-04-05 23:11:24 +000028#include "llvm/ADT/OwningPtr.h"
Chris Lattner736e31d2010-04-04 18:34:07 +000029#include "llvm/ADT/SmallString.h"
30#include "llvm/ADT/Twine.h"
31#include "llvm/Support/ErrorHandling.h"
Chris Lattneraf632c92010-04-05 23:11:24 +000032#include "llvm/Support/MemoryBuffer.h"
33#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000034#include "llvm/Support/TargetRegistry.h"
Chris Lattner736e31d2010-04-04 18:34:07 +000035#include "llvm/Support/raw_ostream.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
140
141/// EmitInlineAsm - This method formats and emits the specified machine
142/// instruction that is an inline asm.
143void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
144 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000145
Chris Lattner736e31d2010-04-04 18:34:07 +0000146 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach2b2de242010-10-01 23:29:12 +0000147
Chris Lattner736e31d2010-04-04 18:34:07 +0000148 // Count the number of register definitions to find the asm string.
149 unsigned NumDefs = 0;
150 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
151 ++NumDefs)
Chris Lattnercf9a4152010-04-07 05:38:05 +0000152 assert(NumDefs != NumOperands-2 && "No asm string?");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000153
Chris Lattner736e31d2010-04-04 18:34:07 +0000154 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
155
156 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
157 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
158
159 // If this asmstr is empty, just print the #APP/#NOAPP markers.
160 // These are useful to see where empty asm's wound up.
161 if (AsmStr[0] == 0) {
Chris Lattner4c842dd2010-04-05 22:42:30 +0000162 // Don't emit the comments if writing to a .o file.
Chris Lattner736e31d2010-04-04 18:34:07 +0000163 if (!OutStreamer.hasRawTextSupport()) return;
164
165 OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
166 MAI->getInlineAsmStart());
167 OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
168 MAI->getInlineAsmEnd());
169 return;
170 }
171
172 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
173 // enabled, so we use EmitRawText.
174 if (OutStreamer.hasRawTextSupport())
175 OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
176 MAI->getInlineAsmStart());
177
Chris Lattnercf9a4152010-04-07 05:38:05 +0000178 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
179 // it.
180 unsigned LocCookie = 0;
Chris Lattnera38941d2010-11-17 07:53:40 +0000181 const MDNode *LocMD = 0;
Chris Lattnerd0024fe2010-04-08 18:20:52 +0000182 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
Chris Lattnera38941d2010-11-17 07:53:40 +0000183 if (MI->getOperand(i-1).isMetadata() &&
184 (LocMD = MI->getOperand(i-1).getMetadata()) &&
185 LocMD->getNumOperands() != 0) {
186 if (const ConstantInt *CI = dyn_cast<ConstantInt>(LocMD->getOperand(0))) {
187 LocCookie = CI->getZExtValue();
188 break;
189 }
190 }
Chris Lattnercf9a4152010-04-07 05:38:05 +0000191 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000192
Chris Lattner736e31d2010-04-04 18:34:07 +0000193 // Emit the inline asm to a temporary string so we can emit it through
194 // EmitInlineAsm.
195 SmallString<256> StringData;
196 raw_svector_ostream OS(StringData);
Jim Grosbach2b2de242010-10-01 23:29:12 +0000197
Chris Lattner736e31d2010-04-04 18:34:07 +0000198 OS << '\t';
199
200 // The variant of the current asmprinter.
201 int AsmPrinterVariant = MAI->getAssemblerDialect();
Chad Rosier3b132fa2012-09-10 21:10:49 +0000202 int InlineAsmVariant = MI->getInlineAsmDialect();
Chad Rosier24f5fdd2012-09-10 21:36:05 +0000203
204 // Switch to the inline assembly variant.
205 if (AsmPrinterVariant != InlineAsmVariant) {
206 if (InlineAsmVariant == 0)
207 OS << ".att_syntax\n\t";
208 else
209 OS << ".intel_syntax\n\t";
210 }
211
Chris Lattner736e31d2010-04-04 18:34:07 +0000212 int CurVariant = -1; // The number of the {.|.|.} region we are in.
213 const char *LastEmitted = AsmStr; // One past the last character emitted.
Jim Grosbach2b2de242010-10-01 23:29:12 +0000214
Chris Lattner736e31d2010-04-04 18:34:07 +0000215 while (*LastEmitted) {
216 switch (*LastEmitted) {
217 default: {
218 // Not a special case, emit the string section literally.
219 const char *LiteralEnd = LastEmitted+1;
220 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
221 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
222 ++LiteralEnd;
223 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
224 OS.write(LastEmitted, LiteralEnd-LastEmitted);
225 LastEmitted = LiteralEnd;
226 break;
227 }
228 case '\n':
229 ++LastEmitted; // Consume newline character.
Chris Lattner4c842dd2010-04-05 22:42:30 +0000230 OS << '\n'; // Indent code with newline.
Chris Lattner736e31d2010-04-04 18:34:07 +0000231 break;
232 case '$': {
233 ++LastEmitted; // Consume '$' character.
234 bool Done = true;
235
236 // Handle escapes.
237 switch (*LastEmitted) {
238 default: Done = false; break;
239 case '$': // $$ -> $
240 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
241 OS << '$';
242 ++LastEmitted; // Consume second '$' character.
243 break;
244 case '(': // $( -> same as GCC's { character.
245 ++LastEmitted; // Consume '(' character.
Chris Lattnerfee455e2010-04-07 05:27:36 +0000246 if (CurVariant != -1)
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000247 report_fatal_error("Nested variants found in inline asm string: '" +
248 Twine(AsmStr) + "'");
Chris Lattner736e31d2010-04-04 18:34:07 +0000249 CurVariant = 0; // We're in the first variant now.
250 break;
251 case '|':
252 ++LastEmitted; // consume '|' character.
253 if (CurVariant == -1)
254 OS << '|'; // this is gcc's behavior for | outside a variant
255 else
256 ++CurVariant; // We're in the next variant.
257 break;
258 case ')': // $) -> same as GCC's } char.
259 ++LastEmitted; // consume ')' character.
260 if (CurVariant == -1)
261 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach2b2de242010-10-01 23:29:12 +0000262 else
Chris Lattner736e31d2010-04-04 18:34:07 +0000263 CurVariant = -1;
264 break;
265 }
266 if (Done) break;
Jim Grosbach2b2de242010-10-01 23:29:12 +0000267
Chris Lattner736e31d2010-04-04 18:34:07 +0000268 bool HasCurlyBraces = false;
269 if (*LastEmitted == '{') { // ${variable}
270 ++LastEmitted; // Consume '{' character.
271 HasCurlyBraces = true;
272 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000273
Chris Lattner736e31d2010-04-04 18:34:07 +0000274 // If we have ${:foo}, then this is not a real operand reference, it is a
275 // "magic" string reference, just like in .td files. Arrange to call
276 // PrintSpecial.
277 if (HasCurlyBraces && *LastEmitted == ':') {
278 ++LastEmitted;
279 const char *StrStart = LastEmitted;
280 const char *StrEnd = strchr(StrStart, '}');
281 if (StrEnd == 0)
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000282 report_fatal_error("Unterminated ${:foo} operand in inline asm"
283 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000284
Chris Lattner736e31d2010-04-04 18:34:07 +0000285 std::string Val(StrStart, StrEnd);
286 PrintSpecial(MI, OS, Val.c_str());
287 LastEmitted = StrEnd+1;
288 break;
289 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000290
Chris Lattner736e31d2010-04-04 18:34:07 +0000291 const char *IDStart = LastEmitted;
Chris Lattner65eeaad2010-04-04 18:42:18 +0000292 const char *IDEnd = IDStart;
Jim Grosbach2b2de242010-10-01 23:29:12 +0000293 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
294
Chris Lattner65eeaad2010-04-04 18:42:18 +0000295 unsigned Val;
296 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000297 report_fatal_error("Bad $ operand number in inline asm string: '" +
298 Twine(AsmStr) + "'");
Chris Lattner736e31d2010-04-04 18:34:07 +0000299 LastEmitted = IDEnd;
Jim Grosbach2b2de242010-10-01 23:29:12 +0000300
Chris Lattner736e31d2010-04-04 18:34:07 +0000301 char Modifier[2] = { 0, 0 };
Jim Grosbach2b2de242010-10-01 23:29:12 +0000302
Chris Lattner736e31d2010-04-04 18:34:07 +0000303 if (HasCurlyBraces) {
304 // If we have curly braces, check for a modifier character. This
305 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
306 if (*LastEmitted == ':') {
307 ++LastEmitted; // Consume ':' character.
Chris Lattner4c842dd2010-04-05 22:42:30 +0000308 if (*LastEmitted == 0)
Chris Lattner75361b62010-04-07 22:58:41 +0000309 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000310 Twine(AsmStr) + "'");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000311
Chris Lattner736e31d2010-04-04 18:34:07 +0000312 Modifier[0] = *LastEmitted;
313 ++LastEmitted; // Consume modifier character.
314 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000315
Chris Lattner4c842dd2010-04-05 22:42:30 +0000316 if (*LastEmitted != '}')
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000317 report_fatal_error("Bad ${} expression in inline asm string: '" +
318 Twine(AsmStr) + "'");
Chris Lattner736e31d2010-04-04 18:34:07 +0000319 ++LastEmitted; // Consume '}' character.
320 }
Jim Grosbach2b2de242010-10-01 23:29:12 +0000321
Chris Lattner4c842dd2010-04-05 22:42:30 +0000322 if (Val >= NumOperands-1)
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000323 report_fatal_error("Invalid $ operand number in inline asm string: '" +
324 Twine(AsmStr) + "'");
Jim Grosbach2b2de242010-10-01 23:29:12 +0000325
Chris Lattner736e31d2010-04-04 18:34:07 +0000326 // Okay, we finally have a value number. Ask the target to print this
327 // operand!
328 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Chengc36b7062011-01-07 23:50:32 +0000329 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner736e31d2010-04-04 18:34:07 +0000330
331 bool Error = false;
332
333 // Scan to find the machine operand number for the operand.
334 for (; Val; --Val) {
335 if (OpNo >= MI->getNumOperands()) break;
336 unsigned OpFlags = MI->getOperand(OpNo).getImm();
337 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
338 }
339
Akira Hatanaka9318e482012-05-08 19:14:42 +0000340 // We may have a location metadata attached to the end of the
341 // instruction, and at no point should see metadata at any
342 // other point while processing. It's an error if so.
Eric Christopheraa206ff2012-03-22 01:33:51 +0000343 if (OpNo >= MI->getNumOperands() ||
Akira Hatanaka9318e482012-05-08 19:14:42 +0000344 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner736e31d2010-04-04 18:34:07 +0000345 Error = true;
346 } else {
347 unsigned OpFlags = MI->getOperand(OpNo).getImm();
348 ++OpNo; // Skip over the ID number.
349
350 if (Modifier[0] == 'l') // labels are target independent
351 // FIXME: What if the operand isn't an MBB, report error?
352 OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
353 else {
354 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Chris Lattnerfee455e2010-04-07 05:27:36 +0000355 if (InlineAsm::isMemKind(OpFlags)) {
Chad Rosier3b132fa2012-09-10 21:10:49 +0000356 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Chris Lattner736e31d2010-04-04 18:34:07 +0000357 Modifier[0] ? Modifier : 0,
358 OS);
359 } else {
Chad Rosier3b132fa2012-09-10 21:10:49 +0000360 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Chris Lattner736e31d2010-04-04 18:34:07 +0000361 Modifier[0] ? Modifier : 0, OS);
362 }
363 }
364 }
365 if (Error) {
366 std::string msg;
367 raw_string_ostream Msg(msg);
Chris Lattner38686bd2010-04-07 23:40:44 +0000368 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
369 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner736e31d2010-04-04 18:34:07 +0000370 }
371 }
372 break;
373 }
374 }
375 }
Chad Rosier24f5fdd2012-09-10 21:36:05 +0000376 // Switch to the AsmPrinter variant.
377 if (AsmPrinterVariant != InlineAsmVariant) {
378 if (AsmPrinterVariant == 0)
379 OS << "\n\t.att_syntax";
380 else
381 OS << "\n\t.intel_syntax";
382 }
383
Chris Lattneraf632c92010-04-05 23:11:24 +0000384 OS << '\n' << (char)0; // null terminate string.
Chad Rosier366df792012-09-05 23:57:37 +0000385 EmitInlineAsm(OS.str(), LocMD, MI->getInlineAsmDialect());
Jim Grosbach2b2de242010-10-01 23:29:12 +0000386
Chris Lattner736e31d2010-04-04 18:34:07 +0000387 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
388 // enabled, so we use EmitRawText.
389 if (OutStreamer.hasRawTextSupport())
390 OutStreamer.EmitRawText(Twine("\t")+MAI->getCommentString()+
391 MAI->getInlineAsmEnd());
392}
393
394
395/// PrintSpecial - Print information related to the specified machine instr
396/// that is independent of the operand, and may be independent of the instr
397/// itself. This can be useful for portably encoding the comment character
398/// or other bits of target-specific knowledge into the asmstrings. The
399/// syntax used is ${:comment}. Targets can override this to add support
400/// for their own strange codes.
401void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
402 const char *Code) const {
403 if (!strcmp(Code, "private")) {
404 OS << MAI->getPrivateGlobalPrefix();
405 } else if (!strcmp(Code, "comment")) {
406 OS << MAI->getCommentString();
407 } else if (!strcmp(Code, "uid")) {
408 // Comparing the address of MI isn't sufficient, because machineinstrs may
409 // be allocated to the same address across functions.
Jim Grosbach2b2de242010-10-01 23:29:12 +0000410
Chris Lattner736e31d2010-04-04 18:34:07 +0000411 // If this is a new LastFn instruction, bump the counter.
412 if (LastMI != MI || LastFn != getFunctionNumber()) {
413 ++Counter;
414 LastMI = MI;
415 LastFn = getFunctionNumber();
416 }
417 OS << Counter;
418 } else {
419 std::string msg;
420 raw_string_ostream Msg(msg);
421 Msg << "Unknown special formatter '" << Code
422 << "' for machine instr: " << *MI;
Chris Lattner75361b62010-04-07 22:58:41 +0000423 report_fatal_error(Msg.str());
Jim Grosbach2b2de242010-10-01 23:29:12 +0000424 }
Chris Lattner736e31d2010-04-04 18:34:07 +0000425}
426
427/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
428/// instruction, using the specified assembler variant. Targets should
429/// override this to format as appropriate.
430bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chad Rosier26c5d5f2012-09-07 20:23:29 +0000431 unsigned AsmVariant, const char *ExtraCode,
432 raw_ostream &O) {
Jack Carterd5e11ad2012-06-21 17:14:46 +0000433 // Does this asm operand have a single letter operand modifier?
434 if (ExtraCode && ExtraCode[0]) {
435 if (ExtraCode[1] != 0) return true; // Unknown modifier.
436
437 const MachineOperand &MO = MI->getOperand(OpNo);
438 switch (ExtraCode[0]) {
439 default:
440 return true; // Unknown modifier.
441 case 'c': // Substitute immediate value without immediate syntax
Jack Carter4db98be2012-06-21 21:37:54 +0000442 if (MO.getType() != MachineOperand::MO_Immediate)
Jack Carterd5e11ad2012-06-21 17:14:46 +0000443 return true;
444 O << MO.getImm();
445 return false;
Jack Carter4db98be2012-06-21 21:37:54 +0000446 case 'n': // Negate the immediate constant.
447 if (MO.getType() != MachineOperand::MO_Immediate)
448 return true;
449 O << -MO.getImm();
450 return false;
Jack Carterd5e11ad2012-06-21 17:14:46 +0000451 }
452 }
Chris Lattner736e31d2010-04-04 18:34:07 +0000453 return true;
454}
455
456bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
457 unsigned AsmVariant,
458 const char *ExtraCode, raw_ostream &O) {
459 // Target doesn't support this yet!
460 return true;
461}
462