blob: 57f05be35c982b8f9680471a6ec41ef53e3969d8 [file] [log] [blame]
Chris Lattner9efd1182010-04-04 19:09:29 +00001//===-- AsmPrinterInlineAsm.cpp - AsmPrinter Inline Asm Handling ----------===//
Chris Lattner1e158692010-04-04 18:34:07 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner9efd1182010-04-04 19:09:29 +000010// This file implements the inline assembler pieces of the AsmPrinter class.
Chris Lattner1e158692010-04-04 18:34:07 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner1e158692010-04-04 18:34:07 +000014#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/Twine.h"
17#include "llvm/CodeGen/MachineBasicBlock.h"
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +000018#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Constants.h"
Rafael Espindola58873562014-01-03 19:21:54 +000021#include "llvm/IR/DataLayout.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/InlineAsm.h"
23#include "llvm/IR/LLVMContext.h"
24#include "llvm/IR/Module.h"
Chris Lattner1e158692010-04-04 18:34:07 +000025#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000027#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner1e158692010-04-04 18:34:07 +000028#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000029#include "llvm/MC/MCTargetAsmParser.h"
Chris Lattner1e158692010-04-04 18:34:07 +000030#include "llvm/Support/ErrorHandling.h"
Chris Lattner8900ef12010-04-05 23:11:24 +000031#include "llvm/Support/MemoryBuffer.h"
32#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000033#include "llvm/Support/TargetRegistry.h"
Chris Lattner1e158692010-04-04 18:34:07 +000034#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/Target/TargetMachine.h"
Yuri Gorshenin3939dec2014-09-10 09:45:49 +000036#include "llvm/Target/TargetRegisterInfo.h"
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +000037#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner1e158692010-04-04 18:34:07 +000038using namespace llvm;
39
Chandler Carruth1b9dde02014-04-22 02:02:50 +000040#define DEBUG_TYPE "asm-printer"
41
Chris Lattner300fa452010-11-17 08:03:32 +000042namespace {
43 struct SrcMgrDiagInfo {
44 const MDNode *LocInfo;
Bob Wilsona594fab2013-02-11 05:37:07 +000045 LLVMContext::InlineAsmDiagHandlerTy DiagHandler;
Chris Lattner300fa452010-11-17 08:03:32 +000046 void *DiagContext;
47 };
48}
49
Chad Rosierb759ede2012-09-07 18:16:38 +000050/// srcMgrDiagHandler - This callback is invoked when the SourceMgr for an
Chris Lattner300fa452010-11-17 08:03:32 +000051/// inline asm has an error in it. diagInfo is a pointer to the SrcMgrDiagInfo
52/// struct above.
Chad Rosierb759ede2012-09-07 18:16:38 +000053static void srcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
Chris Lattner300fa452010-11-17 08:03:32 +000054 SrcMgrDiagInfo *DiagInfo = static_cast<SrcMgrDiagInfo *>(diagInfo);
55 assert(DiagInfo && "Diagnostic context not passed down?");
Jim Grosbach098f5a22011-09-21 21:36:53 +000056
Chris Lattner79ffdc72010-11-17 08:20:42 +000057 // If the inline asm had metadata associated with it, pull out a location
58 // cookie corresponding to which line the error occurred on.
Chris Lattner300fa452010-11-17 08:03:32 +000059 unsigned LocCookie = 0;
Chris Lattner79ffdc72010-11-17 08:20:42 +000060 if (const MDNode *LocInfo = DiagInfo->LocInfo) {
61 unsigned ErrorLine = Diag.getLineNo()-1;
62 if (ErrorLine >= LocInfo->getNumOperands())
63 ErrorLine = 0;
Jim Grosbach098f5a22011-09-21 21:36:53 +000064
Chris Lattner79ffdc72010-11-17 08:20:42 +000065 if (LocInfo->getNumOperands() != 0)
66 if (const ConstantInt *CI =
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000067 mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
Chris Lattner300fa452010-11-17 08:03:32 +000068 LocCookie = CI->getZExtValue();
Chris Lattner79ffdc72010-11-17 08:20:42 +000069 }
Jim Grosbach098f5a22011-09-21 21:36:53 +000070
Chris Lattnerb0e36082010-11-17 08:13:01 +000071 DiagInfo->DiagHandler(Diag, DiagInfo->DiagContext, LocCookie);
Chris Lattner300fa452010-11-17 08:03:32 +000072}
73
Chris Lattner1e158692010-04-04 18:34:07 +000074/// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
Chad Rosierf24ae7b2012-09-05 23:57:37 +000075void AsmPrinter::EmitInlineAsm(StringRef Str, const MDNode *LocMDNode,
76 InlineAsm::AsmDialect Dialect) const {
Chris Lattner1e158692010-04-04 18:34:07 +000077 assert(!Str.empty() && "Can't emit empty inline asm block");
Jim Grosbach00915352010-10-01 23:29:12 +000078
Chris Lattner8900ef12010-04-05 23:11:24 +000079 // Remember if the buffer is nul terminated or not so we can avoid a copy.
80 bool isNullTerminated = Str.back() == 0;
81 if (isNullTerminated)
82 Str = Str.substr(0, Str.size()-1);
Jim Grosbach00915352010-10-01 23:29:12 +000083
Daniel Sanders753e1762014-02-13 14:44:26 +000084 // If the output streamer does not have mature MC support or the integrated
85 // assembler has been disabled, just emit the blob textually.
86 // Otherwise parse the asm and emit it via MC support.
Chris Lattner1e158692010-04-04 18:34:07 +000087 // This is useful in case the asm parser doesn't handle something but the
88 // system assembler does.
Daniel Sanders753e1762014-02-13 14:44:26 +000089 const MCAsmInfo *MCAI = TM.getMCAsmInfo();
90 assert(MCAI && "No MCAsmInfo");
91 if (!MCAI->useIntegratedAssembler() &&
92 !OutStreamer.isIntegratedAssemblerRequired()) {
Eric Christopher64d35be2015-02-19 19:52:25 +000093 emitInlineAsmStart();
Chris Lattner1e158692010-04-04 18:34:07 +000094 OutStreamer.EmitRawText(Str);
Eric Christopher45786412015-02-19 21:24:23 +000095 // If we have a machine function then grab the MCSubtarget off of that,
96 // otherwise we're at the module level and want to construct one from
97 // the default CPU and target triple.
98 if (MF) {
99 emitInlineAsmEnd(MF->getSubtarget<MCSubtargetInfo>(), nullptr);
100 } else {
101 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
102 TM.getTargetTriple(), TM.getTargetCPU(),
103 TM.getTargetFeatureString()));
104 emitInlineAsmEnd(*STI, nullptr);
105 }
Chris Lattner1e158692010-04-04 18:34:07 +0000106 return;
107 }
Jim Grosbach00915352010-10-01 23:29:12 +0000108
Chris Lattner8900ef12010-04-05 23:11:24 +0000109 SourceMgr SrcMgr;
Chris Lattner300fa452010-11-17 08:03:32 +0000110 SrcMgrDiagInfo DiagInfo;
Jim Grosbach00915352010-10-01 23:29:12 +0000111
Bob Wilsona594fab2013-02-11 05:37:07 +0000112 // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
Chris Lattner59126b22010-04-06 00:55:39 +0000113 LLVMContext &LLVMCtx = MMI->getModule()->getContext();
114 bool HasDiagHandler = false;
Craig Topper353eda42014-04-24 06:44:33 +0000115 if (LLVMCtx.getInlineAsmDiagnosticHandler() != nullptr) {
Chad Rosierb759ede2012-09-07 18:16:38 +0000116 // If the source manager has an issue, we arrange for srcMgrDiagHandler
Chris Lattner300fa452010-11-17 08:03:32 +0000117 // to be invoked, getting DiagInfo passed into it.
118 DiagInfo.LocInfo = LocMDNode;
Bob Wilsona594fab2013-02-11 05:37:07 +0000119 DiagInfo.DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
120 DiagInfo.DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
Chad Rosierb759ede2012-09-07 18:16:38 +0000121 SrcMgr.setDiagHandler(srcMgrDiagHandler, &DiagInfo);
Chris Lattner59126b22010-04-06 00:55:39 +0000122 HasDiagHandler = true;
123 }
Jim Grosbach00915352010-10-01 23:29:12 +0000124
Rafael Espindola3560ff22014-08-27 20:03:13 +0000125 std::unique_ptr<MemoryBuffer> Buffer;
126 if (isNullTerminated)
127 Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
128 else
129 Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
Chris Lattner8900ef12010-04-05 23:11:24 +0000130
131 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
David Blaikie1961f142014-08-21 20:44:56 +0000132 SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
Jim Grosbach00915352010-10-01 23:29:12 +0000133
Ahmed Charles56440fd2014-03-06 05:51:42 +0000134 std::unique_ptr<MCAsmParser> Parser(
135 createMCAsmParser(SrcMgr, OutContext, OutStreamer, *MAI));
Evan Cheng4d1ca962011-07-08 01:53:10 +0000136
David Peixottoea2bcb92014-02-06 18:19:40 +0000137 // Initialize the parser with a fresh subtarget info. It is better to use a
138 // new STI here because the parser may modify it and we do not want those
139 // modifications to persist after parsing the inlineasm. The modifications
140 // made by the parser will be seen by the code emitters because it passes
141 // the current STI down to the EncodeInstruction() method.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000142 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
David Peixottoea2bcb92014-02-06 18:19:40 +0000143 TM.getTargetTriple(), TM.getTargetCPU(), TM.getTargetFeatureString()));
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000144
David Peixottoea2bcb92014-02-06 18:19:40 +0000145 // Preserve a copy of the original STI because the parser may modify it. For
146 // example, when switching between arm and thumb mode. If the target needs to
Eric Christopherf9761a22014-02-26 02:53:18 +0000147 // emit code to return to the original state it can do so in
148 // emitInlineAsmEnd().
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000149 MCSubtargetInfo STIOrig = *STI;
150
Eric Christopher2105ae92015-02-19 23:52:35 +0000151 // We may create a new MCInstrInfo here since we might be at the module level
152 // and not have a MachineFunction to initialize the TargetInstrInfo from and
153 // we only need MCInstrInfo for asm parsing.
Eric Christophercbdbf392015-02-19 21:29:51 +0000154 std::unique_ptr<MCTargetAsmParser> TAP(TM.getTarget().createMCAsmParser(
Eric Christopher2105ae92015-02-19 23:52:35 +0000155 *STI, *Parser, MII ? *MII : *TM.getTarget().createMCInstrInfo(),
156 TM.Options.MCOptions));
Chris Lattner8900ef12010-04-05 23:11:24 +0000157 if (!TAP)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000158 report_fatal_error("Inline asm not supported by this streamer because"
Benjamin Kramera6769262010-04-08 10:44:28 +0000159 " we don't have an asm parser for this target\n");
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000160 Parser->setAssemblerDialect(Dialect);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000161 Parser->setTargetParser(*TAP.get());
Yuri Gorshenin3939dec2014-09-10 09:45:49 +0000162 if (MF) {
163 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
164 TAP->SetFrameRegister(TRI->getFrameRegister(*MF));
165 }
Chris Lattner8900ef12010-04-05 23:11:24 +0000166
Eric Christopher64d35be2015-02-19 19:52:25 +0000167 emitInlineAsmStart();
Chris Lattner8900ef12010-04-05 23:11:24 +0000168 // Don't implicitly switch to the text section before the asm.
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000169 int Res = Parser->Run(/*NoInitialTextSection*/ true,
170 /*NoFinalize*/ true);
David Peixottoea2bcb92014-02-06 18:19:40 +0000171 emitInlineAsmEnd(STIOrig, STI.get());
Chris Lattner59126b22010-04-06 00:55:39 +0000172 if (Res && !HasDiagHandler)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000173 report_fatal_error("Error parsing inline asm\n");
Chris Lattner1e158692010-04-04 18:34:07 +0000174}
175
Chad Rosier17788312012-09-11 19:09:56 +0000176static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
177 MachineModuleInfo *MMI, int InlineAsmVariant,
178 AsmPrinter *AP, unsigned LocCookie,
179 raw_ostream &OS) {
180 // Switch to the inline assembly variant.
181 OS << "\t.intel_syntax\n\t";
Chris Lattner1e158692010-04-04 18:34:07 +0000182
Chad Rosier17788312012-09-11 19:09:56 +0000183 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner1e158692010-04-04 18:34:07 +0000184 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach00915352010-10-01 23:29:12 +0000185
Chad Rosier17788312012-09-11 19:09:56 +0000186 while (*LastEmitted) {
187 switch (*LastEmitted) {
188 default: {
189 // Not a special case, emit the string section literally.
190 const char *LiteralEnd = LastEmitted+1;
191 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
192 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
193 ++LiteralEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000194
Chad Rosier17788312012-09-11 19:09:56 +0000195 OS.write(LastEmitted, LiteralEnd-LastEmitted);
196 LastEmitted = LiteralEnd;
197 break;
198 }
199 case '\n':
200 ++LastEmitted; // Consume newline character.
201 OS << '\n'; // Indent code with newline.
202 break;
203 case '$': {
204 ++LastEmitted; // Consume '$' character.
205 bool Done = true;
Chris Lattner1e158692010-04-04 18:34:07 +0000206
Chad Rosier17788312012-09-11 19:09:56 +0000207 // Handle escapes.
208 switch (*LastEmitted) {
209 default: Done = false; break;
210 case '$':
211 ++LastEmitted; // Consume second '$' character.
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000212 break;
213 }
Chad Rosier17788312012-09-11 19:09:56 +0000214 if (Done) break;
215
216 const char *IDStart = LastEmitted;
217 const char *IDEnd = IDStart;
218 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
219
220 unsigned Val;
221 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
222 report_fatal_error("Bad $ operand number in inline asm string: '" +
223 Twine(AsmStr) + "'");
224 LastEmitted = IDEnd;
225
226 if (Val >= NumOperands-1)
227 report_fatal_error("Invalid $ operand number in inline asm string: '" +
228 Twine(AsmStr) + "'");
229
230 // Okay, we finally have a value number. Ask the target to print this
231 // operand!
232 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
233
234 bool Error = false;
235
236 // Scan to find the machine operand number for the operand.
237 for (; Val; --Val) {
238 if (OpNo >= MI->getNumOperands()) break;
239 unsigned OpFlags = MI->getOperand(OpNo).getImm();
240 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
241 }
242
243 // We may have a location metadata attached to the end of the
244 // instruction, and at no point should see metadata at any
245 // other point while processing. It's an error if so.
246 if (OpNo >= MI->getNumOperands() ||
247 MI->getOperand(OpNo).isMetadata()) {
248 Error = true;
249 } else {
250 unsigned OpFlags = MI->getOperand(OpNo).getImm();
251 ++OpNo; // Skip over the ID number.
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000252
Chad Rosier17788312012-09-11 19:09:56 +0000253 if (InlineAsm::isMemKind(OpFlags)) {
254 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000255 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000256 } else {
257 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000258 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000259 }
260 }
261 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000262 std::string msg;
263 raw_string_ostream Msg(msg);
Chad Rosier17788312012-09-11 19:09:56 +0000264 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
265 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
266 }
267 break;
268 }
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000269 }
Chris Lattner51065562010-04-07 05:38:05 +0000270 }
Chad Rosier17788312012-09-11 19:09:56 +0000271 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
272}
Jim Grosbach00915352010-10-01 23:29:12 +0000273
Chad Rosier17788312012-09-11 19:09:56 +0000274static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
275 MachineModuleInfo *MMI, int InlineAsmVariant,
276 int AsmPrinterVariant, AsmPrinter *AP,
277 unsigned LocCookie, raw_ostream &OS) {
Chris Lattner1e158692010-04-04 18:34:07 +0000278 int CurVariant = -1; // The number of the {.|.|.} region we are in.
279 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chad Rosier17788312012-09-11 19:09:56 +0000280 unsigned NumOperands = MI->getNumOperands();
281
282 OS << '\t';
Jim Grosbach00915352010-10-01 23:29:12 +0000283
Chris Lattner1e158692010-04-04 18:34:07 +0000284 while (*LastEmitted) {
285 switch (*LastEmitted) {
286 default: {
287 // Not a special case, emit the string section literally.
288 const char *LiteralEnd = LastEmitted+1;
289 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
290 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
291 ++LiteralEnd;
292 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
293 OS.write(LastEmitted, LiteralEnd-LastEmitted);
294 LastEmitted = LiteralEnd;
295 break;
296 }
297 case '\n':
298 ++LastEmitted; // Consume newline character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000299 OS << '\n'; // Indent code with newline.
Chris Lattner1e158692010-04-04 18:34:07 +0000300 break;
301 case '$': {
302 ++LastEmitted; // Consume '$' character.
303 bool Done = true;
304
305 // Handle escapes.
306 switch (*LastEmitted) {
307 default: Done = false; break;
308 case '$': // $$ -> $
309 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
310 OS << '$';
311 ++LastEmitted; // Consume second '$' character.
312 break;
313 case '(': // $( -> same as GCC's { character.
314 ++LastEmitted; // Consume '(' character.
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000315 if (CurVariant != -1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000316 report_fatal_error("Nested variants found in inline asm string: '" +
317 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000318 CurVariant = 0; // We're in the first variant now.
319 break;
320 case '|':
321 ++LastEmitted; // consume '|' character.
322 if (CurVariant == -1)
323 OS << '|'; // this is gcc's behavior for | outside a variant
324 else
325 ++CurVariant; // We're in the next variant.
326 break;
327 case ')': // $) -> same as GCC's } char.
328 ++LastEmitted; // consume ')' character.
329 if (CurVariant == -1)
330 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach00915352010-10-01 23:29:12 +0000331 else
Chris Lattner1e158692010-04-04 18:34:07 +0000332 CurVariant = -1;
333 break;
334 }
335 if (Done) break;
Jim Grosbach00915352010-10-01 23:29:12 +0000336
Chris Lattner1e158692010-04-04 18:34:07 +0000337 bool HasCurlyBraces = false;
338 if (*LastEmitted == '{') { // ${variable}
339 ++LastEmitted; // Consume '{' character.
340 HasCurlyBraces = true;
341 }
Jim Grosbach00915352010-10-01 23:29:12 +0000342
Chris Lattner1e158692010-04-04 18:34:07 +0000343 // If we have ${:foo}, then this is not a real operand reference, it is a
344 // "magic" string reference, just like in .td files. Arrange to call
345 // PrintSpecial.
346 if (HasCurlyBraces && *LastEmitted == ':') {
347 ++LastEmitted;
348 const char *StrStart = LastEmitted;
349 const char *StrEnd = strchr(StrStart, '}');
Craig Topper353eda42014-04-24 06:44:33 +0000350 if (!StrEnd)
Benjamin Kramera6769262010-04-08 10:44:28 +0000351 report_fatal_error("Unterminated ${:foo} operand in inline asm"
352 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000353
Chris Lattner1e158692010-04-04 18:34:07 +0000354 std::string Val(StrStart, StrEnd);
Chad Rosier17788312012-09-11 19:09:56 +0000355 AP->PrintSpecial(MI, OS, Val.c_str());
Chris Lattner1e158692010-04-04 18:34:07 +0000356 LastEmitted = StrEnd+1;
357 break;
358 }
Jim Grosbach00915352010-10-01 23:29:12 +0000359
Chris Lattner1e158692010-04-04 18:34:07 +0000360 const char *IDStart = LastEmitted;
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000361 const char *IDEnd = IDStart;
Jim Grosbach00915352010-10-01 23:29:12 +0000362 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
363
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000364 unsigned Val;
365 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramera6769262010-04-08 10:44:28 +0000366 report_fatal_error("Bad $ operand number in inline asm string: '" +
367 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000368 LastEmitted = IDEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000369
Chris Lattner1e158692010-04-04 18:34:07 +0000370 char Modifier[2] = { 0, 0 };
Jim Grosbach00915352010-10-01 23:29:12 +0000371
Chris Lattner1e158692010-04-04 18:34:07 +0000372 if (HasCurlyBraces) {
373 // If we have curly braces, check for a modifier character. This
374 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
375 if (*LastEmitted == ':') {
376 ++LastEmitted; // Consume ':' character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000377 if (*LastEmitted == 0)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000378 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramera6769262010-04-08 10:44:28 +0000379 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000380
Chris Lattner1e158692010-04-04 18:34:07 +0000381 Modifier[0] = *LastEmitted;
382 ++LastEmitted; // Consume modifier character.
383 }
Jim Grosbach00915352010-10-01 23:29:12 +0000384
Chris Lattner0e45d242010-04-05 22:42:30 +0000385 if (*LastEmitted != '}')
Benjamin Kramera6769262010-04-08 10:44:28 +0000386 report_fatal_error("Bad ${} expression in inline asm string: '" +
387 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000388 ++LastEmitted; // Consume '}' character.
389 }
Jim Grosbach00915352010-10-01 23:29:12 +0000390
Chris Lattner0e45d242010-04-05 22:42:30 +0000391 if (Val >= NumOperands-1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000392 report_fatal_error("Invalid $ operand number in inline asm string: '" +
393 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000394
Chris Lattner1e158692010-04-04 18:34:07 +0000395 // Okay, we finally have a value number. Ask the target to print this
396 // operand!
397 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Cheng6eb516d2011-01-07 23:50:32 +0000398 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner1e158692010-04-04 18:34:07 +0000399
400 bool Error = false;
401
402 // Scan to find the machine operand number for the operand.
403 for (; Val; --Val) {
404 if (OpNo >= MI->getNumOperands()) break;
405 unsigned OpFlags = MI->getOperand(OpNo).getImm();
406 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
407 }
408
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000409 // We may have a location metadata attached to the end of the
410 // instruction, and at no point should see metadata at any
411 // other point while processing. It's an error if so.
Eric Christopher12da1692012-03-22 01:33:51 +0000412 if (OpNo >= MI->getNumOperands() ||
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000413 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner1e158692010-04-04 18:34:07 +0000414 Error = true;
415 } else {
416 unsigned OpFlags = MI->getOperand(OpNo).getImm();
417 ++OpNo; // Skip over the ID number.
418
419 if (Modifier[0] == 'l') // labels are target independent
420 // FIXME: What if the operand isn't an MBB, report error?
421 OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
422 else {
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000423 if (InlineAsm::isMemKind(OpFlags)) {
Chad Rosierdb20a412012-09-10 21:10:49 +0000424 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000425 Modifier[0] ? Modifier : nullptr,
Chris Lattner1e158692010-04-04 18:34:07 +0000426 OS);
427 } else {
Chad Rosierdb20a412012-09-10 21:10:49 +0000428 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000429 Modifier[0] ? Modifier : nullptr, OS);
Chris Lattner1e158692010-04-04 18:34:07 +0000430 }
431 }
432 }
433 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000434 std::string msg;
435 raw_string_ostream Msg(msg);
Chris Lattner1e457892010-04-07 23:40:44 +0000436 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
437 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner1e158692010-04-04 18:34:07 +0000438 }
439 }
440 break;
441 }
442 }
443 }
Chad Rosier17788312012-09-11 19:09:56 +0000444 OS << '\n' << (char)0; // null terminate string.
445}
446
447/// EmitInlineAsm - This method formats and emits the specified machine
448/// instruction that is an inline asm.
449void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
450 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
451
452 // Count the number of register definitions to find the asm string.
453 unsigned NumDefs = 0;
454 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
455 ++NumDefs)
456 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
457
458 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
459
460 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
461 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
462
463 // If this asmstr is empty, just print the #APP/#NOAPP markers.
464 // These are useful to see where empty asm's wound up.
465 if (AsmStr[0] == 0) {
Rafael Espindola0b694812014-01-16 16:28:37 +0000466 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
467 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chad Rosier17788312012-09-11 19:09:56 +0000468 return;
Chad Rosier7641f582012-09-10 21:36:05 +0000469 }
470
Chad Rosier17788312012-09-11 19:09:56 +0000471 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000472 // enabled, so we use emitRawComment.
473 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
Chad Rosier17788312012-09-11 19:09:56 +0000474
475 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
476 // it.
477 unsigned LocCookie = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000478 const MDNode *LocMD = nullptr;
Chad Rosier17788312012-09-11 19:09:56 +0000479 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
480 if (MI->getOperand(i-1).isMetadata() &&
481 (LocMD = MI->getOperand(i-1).getMetadata()) &&
482 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000483 if (const ConstantInt *CI =
484 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Chad Rosier17788312012-09-11 19:09:56 +0000485 LocCookie = CI->getZExtValue();
486 break;
487 }
488 }
489 }
490
491 // Emit the inline asm to a temporary string so we can emit it through
492 // EmitInlineAsm.
Alp Tokere69170a2014-06-26 22:52:05 +0000493 SmallString<256> StringData;
494 raw_svector_ostream OS(StringData);
Chad Rosier17788312012-09-11 19:09:56 +0000495
496 // The variant of the current asmprinter.
497 int AsmPrinterVariant = MAI->getAssemblerDialect();
498 InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
499 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
500 if (InlineAsmVariant == InlineAsm::AD_ATT)
501 EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
502 AP, LocCookie, OS);
503 else
504 EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
505
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000506 EmitInlineAsm(OS.str(), LocMD, MI->getInlineAsmDialect());
Jim Grosbach00915352010-10-01 23:29:12 +0000507
Chris Lattner1e158692010-04-04 18:34:07 +0000508 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000509 // enabled, so we use emitRawComment.
510 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chris Lattner1e158692010-04-04 18:34:07 +0000511}
512
513
514/// PrintSpecial - Print information related to the specified machine instr
515/// that is independent of the operand, and may be independent of the instr
516/// itself. This can be useful for portably encoding the comment character
517/// or other bits of target-specific knowledge into the asmstrings. The
518/// syntax used is ${:comment}. Targets can override this to add support
519/// for their own strange codes.
520void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
521 const char *Code) const {
Eric Christopher8b770652015-01-26 19:03:15 +0000522 const DataLayout *DL = TM.getDataLayout();
Chris Lattner1e158692010-04-04 18:34:07 +0000523 if (!strcmp(Code, "private")) {
Rafael Espindola58873562014-01-03 19:21:54 +0000524 OS << DL->getPrivateGlobalPrefix();
Chris Lattner1e158692010-04-04 18:34:07 +0000525 } else if (!strcmp(Code, "comment")) {
526 OS << MAI->getCommentString();
527 } else if (!strcmp(Code, "uid")) {
528 // Comparing the address of MI isn't sufficient, because machineinstrs may
529 // be allocated to the same address across functions.
Jim Grosbach00915352010-10-01 23:29:12 +0000530
Chris Lattner1e158692010-04-04 18:34:07 +0000531 // If this is a new LastFn instruction, bump the counter.
532 if (LastMI != MI || LastFn != getFunctionNumber()) {
533 ++Counter;
534 LastMI = MI;
535 LastFn = getFunctionNumber();
536 }
537 OS << Counter;
538 } else {
Alp Tokere69170a2014-06-26 22:52:05 +0000539 std::string msg;
540 raw_string_ostream Msg(msg);
Chris Lattner1e158692010-04-04 18:34:07 +0000541 Msg << "Unknown special formatter '" << Code
542 << "' for machine instr: " << *MI;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000543 report_fatal_error(Msg.str());
Jim Grosbach00915352010-10-01 23:29:12 +0000544 }
Chris Lattner1e158692010-04-04 18:34:07 +0000545}
546
547/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
548/// instruction, using the specified assembler variant. Targets should
549/// override this to format as appropriate.
550bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chad Rosier1f57bcb2012-09-07 20:23:29 +0000551 unsigned AsmVariant, const char *ExtraCode,
552 raw_ostream &O) {
Jack Carterb2fd5f62012-06-21 17:14:46 +0000553 // Does this asm operand have a single letter operand modifier?
554 if (ExtraCode && ExtraCode[0]) {
555 if (ExtraCode[1] != 0) return true; // Unknown modifier.
556
557 const MachineOperand &MO = MI->getOperand(OpNo);
558 switch (ExtraCode[0]) {
559 default:
560 return true; // Unknown modifier.
561 case 'c': // Substitute immediate value without immediate syntax
Jack Carterc457f622012-06-21 21:37:54 +0000562 if (MO.getType() != MachineOperand::MO_Immediate)
Jack Carterb2fd5f62012-06-21 17:14:46 +0000563 return true;
564 O << MO.getImm();
565 return false;
Jack Carterc457f622012-06-21 21:37:54 +0000566 case 'n': // Negate the immediate constant.
567 if (MO.getType() != MachineOperand::MO_Immediate)
568 return true;
569 O << -MO.getImm();
570 return false;
Jack Carterb2fd5f62012-06-21 17:14:46 +0000571 }
572 }
Chris Lattner1e158692010-04-04 18:34:07 +0000573 return true;
574}
575
576bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
577 unsigned AsmVariant,
578 const char *ExtraCode, raw_ostream &O) {
579 // Target doesn't support this yet!
580 return true;
581}
582
Eric Christopher64d35be2015-02-19 19:52:25 +0000583void AsmPrinter::emitInlineAsmStart() const {}
Toma Tabacua23f13c2014-12-17 10:56:16 +0000584
Rafael Espindola65fd0a82014-01-24 15:47:54 +0000585void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
David Peixottoea2bcb92014-02-06 18:19:40 +0000586 const MCSubtargetInfo *EndInfo) const {}