blob: 20075e41977fb7028a0d9b1c80e4f90a7ff5bb3e [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"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000026#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Chris Lattner1e158692010-04-04 18:34:07 +000027#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000028#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner1e158692010-04-04 18:34:07 +000029#include "llvm/MC/MCSymbol.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"
Eric Christopher97ea7622015-02-20 06:35:21 +000035#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Target/TargetMachine.h"
Yuri Gorshenin3939dec2014-09-10 09:45:49 +000037#include "llvm/Target/TargetRegisterInfo.h"
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +000038#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner1e158692010-04-04 18:34:07 +000039using namespace llvm;
40
Chandler Carruth1b9dde02014-04-22 02:02:50 +000041#define DEBUG_TYPE "asm-printer"
42
Chris Lattner300fa452010-11-17 08:03:32 +000043namespace {
44 struct SrcMgrDiagInfo {
45 const MDNode *LocInfo;
Bob Wilsona594fab2013-02-11 05:37:07 +000046 LLVMContext::InlineAsmDiagHandlerTy DiagHandler;
Chris Lattner300fa452010-11-17 08:03:32 +000047 void *DiagContext;
48 };
49}
50
Chad Rosierb759ede2012-09-07 18:16:38 +000051/// srcMgrDiagHandler - This callback is invoked when the SourceMgr for an
Chris Lattner300fa452010-11-17 08:03:32 +000052/// inline asm has an error in it. diagInfo is a pointer to the SrcMgrDiagInfo
53/// struct above.
Chad Rosierb759ede2012-09-07 18:16:38 +000054static void srcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
Chris Lattner300fa452010-11-17 08:03:32 +000055 SrcMgrDiagInfo *DiagInfo = static_cast<SrcMgrDiagInfo *>(diagInfo);
56 assert(DiagInfo && "Diagnostic context not passed down?");
Jim Grosbach098f5a22011-09-21 21:36:53 +000057
Chris Lattner79ffdc72010-11-17 08:20:42 +000058 // If the inline asm had metadata associated with it, pull out a location
59 // cookie corresponding to which line the error occurred on.
Chris Lattner300fa452010-11-17 08:03:32 +000060 unsigned LocCookie = 0;
Chris Lattner79ffdc72010-11-17 08:20:42 +000061 if (const MDNode *LocInfo = DiagInfo->LocInfo) {
62 unsigned ErrorLine = Diag.getLineNo()-1;
63 if (ErrorLine >= LocInfo->getNumOperands())
64 ErrorLine = 0;
Jim Grosbach098f5a22011-09-21 21:36:53 +000065
Chris Lattner79ffdc72010-11-17 08:20:42 +000066 if (LocInfo->getNumOperands() != 0)
67 if (const ConstantInt *CI =
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000068 mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
Chris Lattner300fa452010-11-17 08:03:32 +000069 LocCookie = CI->getZExtValue();
Chris Lattner79ffdc72010-11-17 08:20:42 +000070 }
Jim Grosbach098f5a22011-09-21 21:36:53 +000071
Chris Lattnerb0e36082010-11-17 08:13:01 +000072 DiagInfo->DiagHandler(Diag, DiagInfo->DiagContext, LocCookie);
Chris Lattner300fa452010-11-17 08:03:32 +000073}
74
Chris Lattner1e158692010-04-04 18:34:07 +000075/// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
Akira Hatanaka322ffce2015-03-16 18:02:16 +000076void AsmPrinter::EmitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
Akira Hatanakaff867732015-05-15 00:20:44 +000077 const MCTargetOptions &MCOptions,
Akira Hatanaka322ffce2015-03-16 18:02:16 +000078 const MDNode *LocMDNode,
Chad Rosierf24ae7b2012-09-05 23:57:37 +000079 InlineAsm::AsmDialect Dialect) const {
Chris Lattner1e158692010-04-04 18:34:07 +000080 assert(!Str.empty() && "Can't emit empty inline asm block");
Jim Grosbach00915352010-10-01 23:29:12 +000081
Chris Lattner8900ef12010-04-05 23:11:24 +000082 // Remember if the buffer is nul terminated or not so we can avoid a copy.
83 bool isNullTerminated = Str.back() == 0;
84 if (isNullTerminated)
85 Str = Str.substr(0, Str.size()-1);
Jim Grosbach00915352010-10-01 23:29:12 +000086
Daniel Sanders753e1762014-02-13 14:44:26 +000087 // If the output streamer does not have mature MC support or the integrated
88 // assembler has been disabled, just emit the blob textually.
89 // Otherwise parse the asm and emit it via MC support.
Chris Lattner1e158692010-04-04 18:34:07 +000090 // This is useful in case the asm parser doesn't handle something but the
91 // system assembler does.
Daniel Sanders753e1762014-02-13 14:44:26 +000092 const MCAsmInfo *MCAI = TM.getMCAsmInfo();
93 assert(MCAI && "No MCAsmInfo");
94 if (!MCAI->useIntegratedAssembler() &&
Lang Hames9ff69c82015-04-24 19:11:51 +000095 !OutStreamer->isIntegratedAssemblerRequired()) {
Eric Christopher64d35be2015-02-19 19:52:25 +000096 emitInlineAsmStart();
Lang Hames9ff69c82015-04-24 19:11:51 +000097 OutStreamer->EmitRawText(Str);
Akira Hatanaka322ffce2015-03-16 18:02:16 +000098 emitInlineAsmEnd(STI, nullptr);
Chris Lattner1e158692010-04-04 18:34:07 +000099 return;
100 }
Jim Grosbach00915352010-10-01 23:29:12 +0000101
Chris Lattner8900ef12010-04-05 23:11:24 +0000102 SourceMgr SrcMgr;
Chris Lattner300fa452010-11-17 08:03:32 +0000103 SrcMgrDiagInfo DiagInfo;
Jim Grosbach00915352010-10-01 23:29:12 +0000104
Bob Wilsona594fab2013-02-11 05:37:07 +0000105 // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
Chris Lattner59126b22010-04-06 00:55:39 +0000106 LLVMContext &LLVMCtx = MMI->getModule()->getContext();
107 bool HasDiagHandler = false;
Craig Topper353eda42014-04-24 06:44:33 +0000108 if (LLVMCtx.getInlineAsmDiagnosticHandler() != nullptr) {
Chad Rosierb759ede2012-09-07 18:16:38 +0000109 // If the source manager has an issue, we arrange for srcMgrDiagHandler
Chris Lattner300fa452010-11-17 08:03:32 +0000110 // to be invoked, getting DiagInfo passed into it.
111 DiagInfo.LocInfo = LocMDNode;
Bob Wilsona594fab2013-02-11 05:37:07 +0000112 DiagInfo.DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
113 DiagInfo.DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
Chad Rosierb759ede2012-09-07 18:16:38 +0000114 SrcMgr.setDiagHandler(srcMgrDiagHandler, &DiagInfo);
Chris Lattner59126b22010-04-06 00:55:39 +0000115 HasDiagHandler = true;
116 }
Jim Grosbach00915352010-10-01 23:29:12 +0000117
Rafael Espindola3560ff22014-08-27 20:03:13 +0000118 std::unique_ptr<MemoryBuffer> Buffer;
119 if (isNullTerminated)
120 Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
121 else
122 Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
Chris Lattner8900ef12010-04-05 23:11:24 +0000123
124 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
David Blaikie1961f142014-08-21 20:44:56 +0000125 SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
Jim Grosbach00915352010-10-01 23:29:12 +0000126
Ahmed Charles56440fd2014-03-06 05:51:42 +0000127 std::unique_ptr<MCAsmParser> Parser(
Lang Hames9ff69c82015-04-24 19:11:51 +0000128 createMCAsmParser(SrcMgr, OutContext, *OutStreamer, *MAI));
Evan Cheng4d1ca962011-07-08 01:53:10 +0000129
Eric Christopher3f05e1a2015-02-21 09:09:15 +0000130 // We create a new MCInstrInfo here since we might be at the module level
Eric Christopher2105ae92015-02-19 23:52:35 +0000131 // and not have a MachineFunction to initialize the TargetInstrInfo from and
Eric Christopher3f05e1a2015-02-21 09:09:15 +0000132 // we only need MCInstrInfo for asm parsing. We create one unconditionally
133 // because it's not subtarget dependent.
134 std::unique_ptr<MCInstrInfo> MII(TM.getTarget().createMCInstrInfo());
Eric Christophercbdbf392015-02-19 21:29:51 +0000135 std::unique_ptr<MCTargetAsmParser> TAP(TM.getTarget().createMCAsmParser(
Akira Hatanakab11ef082015-11-14 06:35:56 +0000136 STI, *Parser, *MII, MCOptions));
Chris Lattner8900ef12010-04-05 23:11:24 +0000137 if (!TAP)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000138 report_fatal_error("Inline asm not supported by this streamer because"
Benjamin Kramera6769262010-04-08 10:44:28 +0000139 " we don't have an asm parser for this target\n");
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000140 Parser->setAssemblerDialect(Dialect);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000141 Parser->setTargetParser(*TAP.get());
Yuri Gorshenin3939dec2014-09-10 09:45:49 +0000142 if (MF) {
143 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
144 TAP->SetFrameRegister(TRI->getFrameRegister(*MF));
145 }
Chris Lattner8900ef12010-04-05 23:11:24 +0000146
Eric Christopher64d35be2015-02-19 19:52:25 +0000147 emitInlineAsmStart();
Chris Lattner8900ef12010-04-05 23:11:24 +0000148 // Don't implicitly switch to the text section before the asm.
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000149 int Res = Parser->Run(/*NoInitialTextSection*/ true,
150 /*NoFinalize*/ true);
Akira Hatanakab11ef082015-11-14 06:35:56 +0000151 emitInlineAsmEnd(STI, &TAP->getSTI());
Chris Lattner59126b22010-04-06 00:55:39 +0000152 if (Res && !HasDiagHandler)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000153 report_fatal_error("Error parsing inline asm\n");
Chris Lattner1e158692010-04-04 18:34:07 +0000154}
155
Chad Rosier17788312012-09-11 19:09:56 +0000156static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
157 MachineModuleInfo *MMI, int InlineAsmVariant,
158 AsmPrinter *AP, unsigned LocCookie,
159 raw_ostream &OS) {
160 // Switch to the inline assembly variant.
161 OS << "\t.intel_syntax\n\t";
Chris Lattner1e158692010-04-04 18:34:07 +0000162
Chad Rosier17788312012-09-11 19:09:56 +0000163 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner1e158692010-04-04 18:34:07 +0000164 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach00915352010-10-01 23:29:12 +0000165
Chad Rosier17788312012-09-11 19:09:56 +0000166 while (*LastEmitted) {
167 switch (*LastEmitted) {
168 default: {
169 // Not a special case, emit the string section literally.
170 const char *LiteralEnd = LastEmitted+1;
171 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
172 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
173 ++LiteralEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000174
Chad Rosier17788312012-09-11 19:09:56 +0000175 OS.write(LastEmitted, LiteralEnd-LastEmitted);
176 LastEmitted = LiteralEnd;
177 break;
178 }
179 case '\n':
180 ++LastEmitted; // Consume newline character.
181 OS << '\n'; // Indent code with newline.
182 break;
183 case '$': {
184 ++LastEmitted; // Consume '$' character.
185 bool Done = true;
Chris Lattner1e158692010-04-04 18:34:07 +0000186
Chad Rosier17788312012-09-11 19:09:56 +0000187 // Handle escapes.
188 switch (*LastEmitted) {
189 default: Done = false; break;
190 case '$':
191 ++LastEmitted; // Consume second '$' character.
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000192 break;
193 }
Chad Rosier17788312012-09-11 19:09:56 +0000194 if (Done) break;
195
Reid Klecknerc68a6c42016-11-29 00:29:27 +0000196 // If we have ${:foo}, then this is not a real operand reference, it is a
197 // "magic" string reference, just like in .td files. Arrange to call
198 // PrintSpecial.
199 if (LastEmitted[0] == '{' && LastEmitted[1] == ':') {
200 LastEmitted += 2;
201 const char *StrStart = LastEmitted;
202 const char *StrEnd = strchr(StrStart, '}');
203 if (!StrEnd)
204 report_fatal_error("Unterminated ${:foo} operand in inline asm"
205 " string: '" + Twine(AsmStr) + "'");
206
207 std::string Val(StrStart, StrEnd);
208 AP->PrintSpecial(MI, OS, Val.c_str());
209 LastEmitted = StrEnd+1;
210 break;
211 }
212
Chad Rosier17788312012-09-11 19:09:56 +0000213 const char *IDStart = LastEmitted;
214 const char *IDEnd = IDStart;
215 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
216
217 unsigned Val;
218 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
219 report_fatal_error("Bad $ operand number in inline asm string: '" +
220 Twine(AsmStr) + "'");
221 LastEmitted = IDEnd;
222
223 if (Val >= NumOperands-1)
224 report_fatal_error("Invalid $ operand number in inline asm string: '" +
225 Twine(AsmStr) + "'");
226
227 // Okay, we finally have a value number. Ask the target to print this
228 // operand!
229 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
230
231 bool Error = false;
232
233 // Scan to find the machine operand number for the operand.
234 for (; Val; --Val) {
235 if (OpNo >= MI->getNumOperands()) break;
236 unsigned OpFlags = MI->getOperand(OpNo).getImm();
237 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
238 }
239
240 // We may have a location metadata attached to the end of the
241 // instruction, and at no point should see metadata at any
242 // other point while processing. It's an error if so.
243 if (OpNo >= MI->getNumOperands() ||
244 MI->getOperand(OpNo).isMetadata()) {
245 Error = true;
246 } else {
247 unsigned OpFlags = MI->getOperand(OpNo).getImm();
248 ++OpNo; // Skip over the ID number.
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000249
Chad Rosier17788312012-09-11 19:09:56 +0000250 if (InlineAsm::isMemKind(OpFlags)) {
251 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000252 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000253 } else {
254 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000255 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000256 }
257 }
258 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000259 std::string msg;
260 raw_string_ostream Msg(msg);
Chad Rosier17788312012-09-11 19:09:56 +0000261 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
262 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
263 }
264 break;
265 }
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000266 }
Chris Lattner51065562010-04-07 05:38:05 +0000267 }
Chad Rosier17788312012-09-11 19:09:56 +0000268 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
269}
Jim Grosbach00915352010-10-01 23:29:12 +0000270
Chad Rosier17788312012-09-11 19:09:56 +0000271static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
272 MachineModuleInfo *MMI, int InlineAsmVariant,
273 int AsmPrinterVariant, AsmPrinter *AP,
274 unsigned LocCookie, raw_ostream &OS) {
Chris Lattner1e158692010-04-04 18:34:07 +0000275 int CurVariant = -1; // The number of the {.|.|.} region we are in.
276 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chad Rosier17788312012-09-11 19:09:56 +0000277 unsigned NumOperands = MI->getNumOperands();
278
279 OS << '\t';
Jim Grosbach00915352010-10-01 23:29:12 +0000280
Chris Lattner1e158692010-04-04 18:34:07 +0000281 while (*LastEmitted) {
282 switch (*LastEmitted) {
283 default: {
284 // Not a special case, emit the string section literally.
285 const char *LiteralEnd = LastEmitted+1;
286 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
287 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
288 ++LiteralEnd;
289 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
290 OS.write(LastEmitted, LiteralEnd-LastEmitted);
291 LastEmitted = LiteralEnd;
292 break;
293 }
294 case '\n':
295 ++LastEmitted; // Consume newline character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000296 OS << '\n'; // Indent code with newline.
Chris Lattner1e158692010-04-04 18:34:07 +0000297 break;
298 case '$': {
299 ++LastEmitted; // Consume '$' character.
300 bool Done = true;
301
302 // Handle escapes.
303 switch (*LastEmitted) {
304 default: Done = false; break;
305 case '$': // $$ -> $
306 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
307 OS << '$';
308 ++LastEmitted; // Consume second '$' character.
309 break;
310 case '(': // $( -> same as GCC's { character.
311 ++LastEmitted; // Consume '(' character.
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000312 if (CurVariant != -1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000313 report_fatal_error("Nested variants found in inline asm string: '" +
314 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000315 CurVariant = 0; // We're in the first variant now.
316 break;
317 case '|':
318 ++LastEmitted; // consume '|' character.
319 if (CurVariant == -1)
320 OS << '|'; // this is gcc's behavior for | outside a variant
321 else
322 ++CurVariant; // We're in the next variant.
323 break;
324 case ')': // $) -> same as GCC's } char.
325 ++LastEmitted; // consume ')' character.
326 if (CurVariant == -1)
327 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach00915352010-10-01 23:29:12 +0000328 else
Chris Lattner1e158692010-04-04 18:34:07 +0000329 CurVariant = -1;
330 break;
331 }
332 if (Done) break;
Jim Grosbach00915352010-10-01 23:29:12 +0000333
Chris Lattner1e158692010-04-04 18:34:07 +0000334 bool HasCurlyBraces = false;
335 if (*LastEmitted == '{') { // ${variable}
336 ++LastEmitted; // Consume '{' character.
337 HasCurlyBraces = true;
338 }
Jim Grosbach00915352010-10-01 23:29:12 +0000339
Chris Lattner1e158692010-04-04 18:34:07 +0000340 // If we have ${:foo}, then this is not a real operand reference, it is a
341 // "magic" string reference, just like in .td files. Arrange to call
342 // PrintSpecial.
343 if (HasCurlyBraces && *LastEmitted == ':') {
344 ++LastEmitted;
345 const char *StrStart = LastEmitted;
346 const char *StrEnd = strchr(StrStart, '}');
Craig Topper353eda42014-04-24 06:44:33 +0000347 if (!StrEnd)
Benjamin Kramera6769262010-04-08 10:44:28 +0000348 report_fatal_error("Unterminated ${:foo} operand in inline asm"
349 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000350
Chris Lattner1e158692010-04-04 18:34:07 +0000351 std::string Val(StrStart, StrEnd);
Chad Rosier17788312012-09-11 19:09:56 +0000352 AP->PrintSpecial(MI, OS, Val.c_str());
Chris Lattner1e158692010-04-04 18:34:07 +0000353 LastEmitted = StrEnd+1;
354 break;
355 }
Jim Grosbach00915352010-10-01 23:29:12 +0000356
Chris Lattner1e158692010-04-04 18:34:07 +0000357 const char *IDStart = LastEmitted;
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000358 const char *IDEnd = IDStart;
Jim Grosbach00915352010-10-01 23:29:12 +0000359 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
360
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000361 unsigned Val;
362 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramera6769262010-04-08 10:44:28 +0000363 report_fatal_error("Bad $ operand number in inline asm string: '" +
364 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000365 LastEmitted = IDEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000366
Chris Lattner1e158692010-04-04 18:34:07 +0000367 char Modifier[2] = { 0, 0 };
Jim Grosbach00915352010-10-01 23:29:12 +0000368
Chris Lattner1e158692010-04-04 18:34:07 +0000369 if (HasCurlyBraces) {
370 // If we have curly braces, check for a modifier character. This
371 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
372 if (*LastEmitted == ':') {
373 ++LastEmitted; // Consume ':' character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000374 if (*LastEmitted == 0)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000375 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramera6769262010-04-08 10:44:28 +0000376 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000377
Chris Lattner1e158692010-04-04 18:34:07 +0000378 Modifier[0] = *LastEmitted;
379 ++LastEmitted; // Consume modifier character.
380 }
Jim Grosbach00915352010-10-01 23:29:12 +0000381
Chris Lattner0e45d242010-04-05 22:42:30 +0000382 if (*LastEmitted != '}')
Benjamin Kramera6769262010-04-08 10:44:28 +0000383 report_fatal_error("Bad ${} expression in inline asm string: '" +
384 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000385 ++LastEmitted; // Consume '}' character.
386 }
Jim Grosbach00915352010-10-01 23:29:12 +0000387
Chris Lattner0e45d242010-04-05 22:42:30 +0000388 if (Val >= NumOperands-1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000389 report_fatal_error("Invalid $ operand number in inline asm string: '" +
390 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000391
Chris Lattner1e158692010-04-04 18:34:07 +0000392 // Okay, we finally have a value number. Ask the target to print this
393 // operand!
394 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Cheng6eb516d2011-01-07 23:50:32 +0000395 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner1e158692010-04-04 18:34:07 +0000396
397 bool Error = false;
398
399 // Scan to find the machine operand number for the operand.
400 for (; Val; --Val) {
401 if (OpNo >= MI->getNumOperands()) break;
402 unsigned OpFlags = MI->getOperand(OpNo).getImm();
403 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
404 }
405
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000406 // We may have a location metadata attached to the end of the
407 // instruction, and at no point should see metadata at any
408 // other point while processing. It's an error if so.
Eric Christopher12da1692012-03-22 01:33:51 +0000409 if (OpNo >= MI->getNumOperands() ||
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000410 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner1e158692010-04-04 18:34:07 +0000411 Error = true;
412 } else {
413 unsigned OpFlags = MI->getOperand(OpNo).getImm();
414 ++OpNo; // Skip over the ID number.
415
Matt Arsenault8b643552015-06-09 00:31:39 +0000416 if (Modifier[0] == 'l') { // Labels are target independent.
Chris Lattner1e158692010-04-04 18:34:07 +0000417 // FIXME: What if the operand isn't an MBB, report error?
Matt Arsenault8b643552015-06-09 00:31:39 +0000418 const MCSymbol *Sym = MI->getOperand(OpNo).getMBB()->getSymbol();
419 Sym->print(OS, AP->MAI);
420 } else {
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000421 if (InlineAsm::isMemKind(OpFlags)) {
Chad Rosierdb20a412012-09-10 21:10:49 +0000422 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000423 Modifier[0] ? Modifier : nullptr,
Chris Lattner1e158692010-04-04 18:34:07 +0000424 OS);
425 } else {
Chad Rosierdb20a412012-09-10 21:10:49 +0000426 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000427 Modifier[0] ? Modifier : nullptr, OS);
Chris Lattner1e158692010-04-04 18:34:07 +0000428 }
429 }
430 }
431 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000432 std::string msg;
433 raw_string_ostream Msg(msg);
Chris Lattner1e457892010-04-07 23:40:44 +0000434 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
435 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner1e158692010-04-04 18:34:07 +0000436 }
437 }
438 break;
439 }
440 }
441 }
Chad Rosier17788312012-09-11 19:09:56 +0000442 OS << '\n' << (char)0; // null terminate string.
443}
444
445/// EmitInlineAsm - This method formats and emits the specified machine
446/// instruction that is an inline asm.
447void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
448 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
449
450 // Count the number of register definitions to find the asm string.
451 unsigned NumDefs = 0;
452 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
453 ++NumDefs)
454 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
455
456 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
457
458 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
459 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
460
461 // If this asmstr is empty, just print the #APP/#NOAPP markers.
462 // These are useful to see where empty asm's wound up.
463 if (AsmStr[0] == 0) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000464 OutStreamer->emitRawComment(MAI->getInlineAsmStart());
465 OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
Chad Rosier17788312012-09-11 19:09:56 +0000466 return;
Chad Rosier7641f582012-09-10 21:36:05 +0000467 }
468
Chad Rosier17788312012-09-11 19:09:56 +0000469 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000470 // enabled, so we use emitRawComment.
Lang Hames9ff69c82015-04-24 19:11:51 +0000471 OutStreamer->emitRawComment(MAI->getInlineAsmStart());
Chad Rosier17788312012-09-11 19:09:56 +0000472
473 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
474 // it.
475 unsigned LocCookie = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000476 const MDNode *LocMD = nullptr;
Chad Rosier17788312012-09-11 19:09:56 +0000477 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
478 if (MI->getOperand(i-1).isMetadata() &&
479 (LocMD = MI->getOperand(i-1).getMetadata()) &&
480 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000481 if (const ConstantInt *CI =
482 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Chad Rosier17788312012-09-11 19:09:56 +0000483 LocCookie = CI->getZExtValue();
484 break;
485 }
486 }
487 }
488
489 // Emit the inline asm to a temporary string so we can emit it through
490 // EmitInlineAsm.
Alp Tokere69170a2014-06-26 22:52:05 +0000491 SmallString<256> StringData;
492 raw_svector_ostream OS(StringData);
Chad Rosier17788312012-09-11 19:09:56 +0000493
494 // The variant of the current asmprinter.
495 int AsmPrinterVariant = MAI->getAssemblerDialect();
496 InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
497 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
498 if (InlineAsmVariant == InlineAsm::AD_ATT)
499 EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
500 AP, LocCookie, OS);
501 else
502 EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
503
Akira Hatanakaff867732015-05-15 00:20:44 +0000504 // Reset SanitizeAddress based on the function's attribute.
505 MCTargetOptions MCOptions = TM.Options.MCOptions;
506 MCOptions.SanitizeAddress =
507 MF->getFunction()->hasFnAttribute(Attribute::SanitizeAddress);
508
509 EmitInlineAsm(OS.str(), getSubtargetInfo(), MCOptions, LocMD,
510 MI->getInlineAsmDialect());
Jim Grosbach00915352010-10-01 23:29:12 +0000511
Chris Lattner1e158692010-04-04 18:34:07 +0000512 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000513 // enabled, so we use emitRawComment.
Lang Hames9ff69c82015-04-24 19:11:51 +0000514 OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
Chris Lattner1e158692010-04-04 18:34:07 +0000515}
516
517
518/// PrintSpecial - Print information related to the specified machine instr
519/// that is independent of the operand, and may be independent of the instr
520/// itself. This can be useful for portably encoding the comment character
521/// or other bits of target-specific knowledge into the asmstrings. The
522/// syntax used is ${:comment}. Targets can override this to add support
523/// for their own strange codes.
524void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
525 const char *Code) const {
526 if (!strcmp(Code, "private")) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000527 const DataLayout &DL = MF->getDataLayout();
528 OS << DL.getPrivateGlobalPrefix();
Chris Lattner1e158692010-04-04 18:34:07 +0000529 } else if (!strcmp(Code, "comment")) {
530 OS << MAI->getCommentString();
531 } else if (!strcmp(Code, "uid")) {
532 // Comparing the address of MI isn't sufficient, because machineinstrs may
533 // be allocated to the same address across functions.
Jim Grosbach00915352010-10-01 23:29:12 +0000534
Chris Lattner1e158692010-04-04 18:34:07 +0000535 // If this is a new LastFn instruction, bump the counter.
536 if (LastMI != MI || LastFn != getFunctionNumber()) {
537 ++Counter;
538 LastMI = MI;
539 LastFn = getFunctionNumber();
540 }
541 OS << Counter;
542 } else {
Alp Tokere69170a2014-06-26 22:52:05 +0000543 std::string msg;
544 raw_string_ostream Msg(msg);
Chris Lattner1e158692010-04-04 18:34:07 +0000545 Msg << "Unknown special formatter '" << Code
546 << "' for machine instr: " << *MI;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000547 report_fatal_error(Msg.str());
Jim Grosbach00915352010-10-01 23:29:12 +0000548 }
Chris Lattner1e158692010-04-04 18:34:07 +0000549}
550
551/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
552/// instruction, using the specified assembler variant. Targets should
553/// override this to format as appropriate.
554bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chad Rosier1f57bcb2012-09-07 20:23:29 +0000555 unsigned AsmVariant, const char *ExtraCode,
556 raw_ostream &O) {
Jack Carterb2fd5f62012-06-21 17:14:46 +0000557 // Does this asm operand have a single letter operand modifier?
558 if (ExtraCode && ExtraCode[0]) {
559 if (ExtraCode[1] != 0) return true; // Unknown modifier.
560
561 const MachineOperand &MO = MI->getOperand(OpNo);
562 switch (ExtraCode[0]) {
563 default:
564 return true; // Unknown modifier.
565 case 'c': // Substitute immediate value without immediate syntax
Jack Carterc457f622012-06-21 21:37:54 +0000566 if (MO.getType() != MachineOperand::MO_Immediate)
Jack Carterb2fd5f62012-06-21 17:14:46 +0000567 return true;
568 O << MO.getImm();
569 return false;
Jack Carterc457f622012-06-21 21:37:54 +0000570 case 'n': // Negate the immediate constant.
571 if (MO.getType() != MachineOperand::MO_Immediate)
572 return true;
573 O << -MO.getImm();
574 return false;
Nemanja Ivanovice8cbae32016-02-04 16:18:08 +0000575 case 's': // The GCC deprecated s modifier
576 if (MO.getType() != MachineOperand::MO_Immediate)
577 return true;
578 O << ((32 - MO.getImm()) & 31);
579 return false;
Jack Carterb2fd5f62012-06-21 17:14:46 +0000580 }
581 }
Chris Lattner1e158692010-04-04 18:34:07 +0000582 return true;
583}
584
585bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
586 unsigned AsmVariant,
587 const char *ExtraCode, raw_ostream &O) {
588 // Target doesn't support this yet!
589 return true;
590}
591
Eric Christopher64d35be2015-02-19 19:52:25 +0000592void AsmPrinter::emitInlineAsmStart() const {}
Toma Tabacua23f13c2014-12-17 10:56:16 +0000593
Rafael Espindola65fd0a82014-01-24 15:47:54 +0000594void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
David Peixottoea2bcb92014-02-06 18:19:40 +0000595 const MCSubtargetInfo *EndInfo) const {}