blob: 4159eb19423ad16db18198c0c2d717581704576b [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
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/ADT/SmallString.h"
15#include "llvm/ADT/Twine.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#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"
David Blaikie3f833ed2017-11-08 01:01:31 +000020#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000021#include "llvm/CodeGen/TargetRegisterInfo.h"
22#include "llvm/CodeGen/TargetSubtargetInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Constants.h"
Rafael Espindola58873562014-01-03 19:21:54 +000024#include "llvm/IR/DataLayout.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/InlineAsm.h"
26#include "llvm/IR/LLVMContext.h"
27#include "llvm/IR/Module.h"
Chris Lattner1e158692010-04-04 18:34:07 +000028#include "llvm/MC/MCAsmInfo.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000029#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Chris Lattner1e158692010-04-04 18:34:07 +000030#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000031#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner1e158692010-04-04 18:34:07 +000032#include "llvm/MC/MCSymbol.h"
Chris Lattner1e158692010-04-04 18:34:07 +000033#include "llvm/Support/ErrorHandling.h"
Chris Lattner8900ef12010-04-05 23:11:24 +000034#include "llvm/Support/MemoryBuffer.h"
35#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000036#include "llvm/Support/TargetRegistry.h"
Chris Lattner1e158692010-04-04 18:34:07 +000037#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000038#include "llvm/Target/TargetMachine.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
Chad Rosierb759ede2012-09-07 18:16:38 +000043/// srcMgrDiagHandler - This callback is invoked when the SourceMgr for an
Chris Lattner300fa452010-11-17 08:03:32 +000044/// inline asm has an error in it. diagInfo is a pointer to the SrcMgrDiagInfo
45/// struct above.
Chad Rosierb759ede2012-09-07 18:16:38 +000046static void srcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
Sanne Wouda29338752017-02-08 14:48:05 +000047 AsmPrinter::SrcMgrDiagInfo *DiagInfo =
48 static_cast<AsmPrinter::SrcMgrDiagInfo *>(diagInfo);
Chris Lattner300fa452010-11-17 08:03:32 +000049 assert(DiagInfo && "Diagnostic context not passed down?");
Jim Grosbach098f5a22011-09-21 21:36:53 +000050
Sanne Wouda91eadad2017-02-13 13:58:00 +000051 // Look up a LocInfo for the buffer this diagnostic is coming from.
52 unsigned BufNum = DiagInfo->SrcMgr.FindBufferContainingLoc(Diag.getLoc());
53 const MDNode *LocInfo = nullptr;
54 if (BufNum > 0 && BufNum <= DiagInfo->LocInfos.size())
55 LocInfo = DiagInfo->LocInfos[BufNum-1];
56
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;
Sanne Wouda91eadad2017-02-13 13:58:00 +000060 if (LocInfo) {
Chris Lattner79ffdc72010-11-17 08:20:42 +000061 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.
Akira Hatanaka322ffce2015-03-16 18:02:16 +000075void AsmPrinter::EmitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
Akira Hatanakaff867732015-05-15 00:20:44 +000076 const MCTargetOptions &MCOptions,
Akira Hatanaka322ffce2015-03-16 18:02:16 +000077 const MDNode *LocMDNode,
Chad Rosierf24ae7b2012-09-05 23:57:37 +000078 InlineAsm::AsmDialect Dialect) const {
Chris Lattner1e158692010-04-04 18:34:07 +000079 assert(!Str.empty() && "Can't emit empty inline asm block");
Jim Grosbach00915352010-10-01 23:29:12 +000080
Chris Lattner8900ef12010-04-05 23:11:24 +000081 // Remember if the buffer is nul terminated or not so we can avoid a copy.
82 bool isNullTerminated = Str.back() == 0;
83 if (isNullTerminated)
84 Str = Str.substr(0, Str.size()-1);
Jim Grosbach00915352010-10-01 23:29:12 +000085
Daniel Sanders753e1762014-02-13 14:44:26 +000086 // If the output streamer does not have mature MC support or the integrated
87 // assembler has been disabled, just emit the blob textually.
88 // Otherwise parse the asm and emit it via MC support.
Chris Lattner1e158692010-04-04 18:34:07 +000089 // This is useful in case the asm parser doesn't handle something but the
90 // system assembler does.
Daniel Sanders753e1762014-02-13 14:44:26 +000091 const MCAsmInfo *MCAI = TM.getMCAsmInfo();
92 assert(MCAI && "No MCAsmInfo");
93 if (!MCAI->useIntegratedAssembler() &&
Lang Hames9ff69c82015-04-24 19:11:51 +000094 !OutStreamer->isIntegratedAssemblerRequired()) {
Eric Christopher64d35be2015-02-19 19:52:25 +000095 emitInlineAsmStart();
Lang Hames9ff69c82015-04-24 19:11:51 +000096 OutStreamer->EmitRawText(Str);
Akira Hatanaka322ffce2015-03-16 18:02:16 +000097 emitInlineAsmEnd(STI, nullptr);
Chris Lattner1e158692010-04-04 18:34:07 +000098 return;
99 }
Jim Grosbach00915352010-10-01 23:29:12 +0000100
Ties Stuij083fb1a2018-08-08 17:11:54 +0000101 if (!DiagInfo) {
102 DiagInfo = make_unique<SrcMgrDiagInfo>();
Saleem Abdulrasool6252bd82017-01-05 05:56:39 +0000103
Ties Stuij083fb1a2018-08-08 17:11:54 +0000104 MCContext &Context = MMI->getContext();
105 Context.setInlineSourceManager(&DiagInfo->SrcMgr);
106
107 LLVMContext &LLVMCtx = MMI->getModule()->getContext();
108 if (LLVMCtx.getInlineAsmDiagnosticHandler()) {
109 DiagInfo->DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
110 DiagInfo->DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
111 DiagInfo->SrcMgr.setDiagHandler(srcMgrDiagHandler, DiagInfo.get());
112 }
113 }
114
115 SourceMgr &SrcMgr = DiagInfo->SrcMgr;
116 SrcMgr.setIncludeDirs(MCOptions.IASSearchPaths);
117
118 std::unique_ptr<MemoryBuffer> Buffer;
119 // The inline asm source manager will outlive Str, so make a copy of the
120 // string for SourceMgr to own.
121 Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
122
123 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
124 unsigned BufNum = SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
125
126 // Store LocMDNode in DiagInfo, using BufNum as an identifier.
127 if (LocMDNode) {
128 DiagInfo->LocInfos.resize(BufNum);
129 DiagInfo->LocInfos[BufNum-1] = LocMDNode;
130 }
131
132 std::unique_ptr<MCAsmParser> Parser(
133 createMCAsmParser(SrcMgr, OutContext, *OutStreamer, *MAI, BufNum));
Evan Cheng4d1ca962011-07-08 01:53:10 +0000134
Nirav Dave6c0665e2018-04-30 19:22:40 +0000135 // Do not use assembler-level information for parsing inline assembly.
136 OutStreamer->setUseAssemblerInfoForParsing(false);
137
Eric Christopher3f05e1a2015-02-21 09:09:15 +0000138 // We create a new MCInstrInfo here since we might be at the module level
Eric Christopher2105ae92015-02-19 23:52:35 +0000139 // and not have a MachineFunction to initialize the TargetInstrInfo from and
Eric Christopher3f05e1a2015-02-21 09:09:15 +0000140 // we only need MCInstrInfo for asm parsing. We create one unconditionally
141 // because it's not subtarget dependent.
142 std::unique_ptr<MCInstrInfo> MII(TM.getTarget().createMCInstrInfo());
Eric Christophercbdbf392015-02-19 21:29:51 +0000143 std::unique_ptr<MCTargetAsmParser> TAP(TM.getTarget().createMCAsmParser(
Akira Hatanakab11ef082015-11-14 06:35:56 +0000144 STI, *Parser, *MII, MCOptions));
Chris Lattner8900ef12010-04-05 23:11:24 +0000145 if (!TAP)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000146 report_fatal_error("Inline asm not supported by this streamer because"
Benjamin Kramera6769262010-04-08 10:44:28 +0000147 " we don't have an asm parser for this target\n");
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000148 Parser->setAssemblerDialect(Dialect);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000149 Parser->setTargetParser(*TAP.get());
Andrew V. Tischenko3543f0a2017-11-09 12:45:40 +0000150 Parser->setEnablePrintSchedInfo(EnablePrintSchedInfo);
Andrew V. Tischenkoc3c67232017-04-26 09:56:59 +0000151 if (Dialect == InlineAsm::AD_Intel)
152 // We need this flag to be able to parse numbers like "0bH"
153 Parser->setParsingInlineAsm(true);
Yuri Gorshenin3939dec2014-09-10 09:45:49 +0000154 if (MF) {
155 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
156 TAP->SetFrameRegister(TRI->getFrameRegister(*MF));
157 }
Chris Lattner8900ef12010-04-05 23:11:24 +0000158
Eric Christopher64d35be2015-02-19 19:52:25 +0000159 emitInlineAsmStart();
Chris Lattner8900ef12010-04-05 23:11:24 +0000160 // Don't implicitly switch to the text section before the asm.
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000161 int Res = Parser->Run(/*NoInitialTextSection*/ true,
162 /*NoFinalize*/ true);
Akira Hatanakab11ef082015-11-14 06:35:56 +0000163 emitInlineAsmEnd(STI, &TAP->getSTI());
Sanne Wouda29338752017-02-08 14:48:05 +0000164
Sanne Wouda29338752017-02-08 14:48:05 +0000165 if (Res && !DiagInfo->DiagHandler)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000166 report_fatal_error("Error parsing inline asm\n");
Chris Lattner1e158692010-04-04 18:34:07 +0000167}
168
Chad Rosier17788312012-09-11 19:09:56 +0000169static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
170 MachineModuleInfo *MMI, int InlineAsmVariant,
171 AsmPrinter *AP, unsigned LocCookie,
172 raw_ostream &OS) {
173 // Switch to the inline assembly variant.
174 OS << "\t.intel_syntax\n\t";
Chris Lattner1e158692010-04-04 18:34:07 +0000175
Chad Rosier17788312012-09-11 19:09:56 +0000176 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner1e158692010-04-04 18:34:07 +0000177 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach00915352010-10-01 23:29:12 +0000178
Chad Rosier17788312012-09-11 19:09:56 +0000179 while (*LastEmitted) {
180 switch (*LastEmitted) {
181 default: {
182 // Not a special case, emit the string section literally.
183 const char *LiteralEnd = LastEmitted+1;
184 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
185 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
186 ++LiteralEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000187
Chad Rosier17788312012-09-11 19:09:56 +0000188 OS.write(LastEmitted, LiteralEnd-LastEmitted);
189 LastEmitted = LiteralEnd;
190 break;
191 }
192 case '\n':
193 ++LastEmitted; // Consume newline character.
194 OS << '\n'; // Indent code with newline.
195 break;
196 case '$': {
197 ++LastEmitted; // Consume '$' character.
198 bool Done = true;
Chris Lattner1e158692010-04-04 18:34:07 +0000199
Chad Rosier17788312012-09-11 19:09:56 +0000200 // Handle escapes.
201 switch (*LastEmitted) {
202 default: Done = false; break;
203 case '$':
204 ++LastEmitted; // Consume second '$' character.
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000205 break;
206 }
Chad Rosier17788312012-09-11 19:09:56 +0000207 if (Done) break;
208
Reid Klecknerc68a6c42016-11-29 00:29:27 +0000209 // If we have ${:foo}, then this is not a real operand reference, it is a
210 // "magic" string reference, just like in .td files. Arrange to call
211 // PrintSpecial.
212 if (LastEmitted[0] == '{' && LastEmitted[1] == ':') {
213 LastEmitted += 2;
214 const char *StrStart = LastEmitted;
215 const char *StrEnd = strchr(StrStart, '}');
216 if (!StrEnd)
217 report_fatal_error("Unterminated ${:foo} operand in inline asm"
218 " string: '" + Twine(AsmStr) + "'");
219
220 std::string Val(StrStart, StrEnd);
221 AP->PrintSpecial(MI, OS, Val.c_str());
222 LastEmitted = StrEnd+1;
223 break;
224 }
225
Chad Rosier17788312012-09-11 19:09:56 +0000226 const char *IDStart = LastEmitted;
227 const char *IDEnd = IDStart;
228 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
229
230 unsigned Val;
231 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
232 report_fatal_error("Bad $ operand number in inline asm string: '" +
233 Twine(AsmStr) + "'");
234 LastEmitted = IDEnd;
235
236 if (Val >= NumOperands-1)
237 report_fatal_error("Invalid $ operand number in inline asm string: '" +
238 Twine(AsmStr) + "'");
239
240 // Okay, we finally have a value number. Ask the target to print this
241 // operand!
242 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
243
244 bool Error = false;
245
246 // Scan to find the machine operand number for the operand.
247 for (; Val; --Val) {
248 if (OpNo >= MI->getNumOperands()) break;
249 unsigned OpFlags = MI->getOperand(OpNo).getImm();
250 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
251 }
252
253 // We may have a location metadata attached to the end of the
254 // instruction, and at no point should see metadata at any
255 // other point while processing. It's an error if so.
256 if (OpNo >= MI->getNumOperands() ||
257 MI->getOperand(OpNo).isMetadata()) {
258 Error = true;
259 } else {
260 unsigned OpFlags = MI->getOperand(OpNo).getImm();
261 ++OpNo; // Skip over the ID number.
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000262
Chad Rosier17788312012-09-11 19:09:56 +0000263 if (InlineAsm::isMemKind(OpFlags)) {
264 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000265 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000266 } else {
267 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000268 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000269 }
270 }
271 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000272 std::string msg;
273 raw_string_ostream Msg(msg);
Chad Rosier17788312012-09-11 19:09:56 +0000274 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
275 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
276 }
277 break;
278 }
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000279 }
Chris Lattner51065562010-04-07 05:38:05 +0000280 }
Chad Rosier17788312012-09-11 19:09:56 +0000281 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
282}
Jim Grosbach00915352010-10-01 23:29:12 +0000283
Chad Rosier17788312012-09-11 19:09:56 +0000284static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
285 MachineModuleInfo *MMI, int InlineAsmVariant,
286 int AsmPrinterVariant, AsmPrinter *AP,
287 unsigned LocCookie, raw_ostream &OS) {
Chris Lattner1e158692010-04-04 18:34:07 +0000288 int CurVariant = -1; // The number of the {.|.|.} region we are in.
289 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chad Rosier17788312012-09-11 19:09:56 +0000290 unsigned NumOperands = MI->getNumOperands();
291
292 OS << '\t';
Jim Grosbach00915352010-10-01 23:29:12 +0000293
Chris Lattner1e158692010-04-04 18:34:07 +0000294 while (*LastEmitted) {
295 switch (*LastEmitted) {
296 default: {
297 // Not a special case, emit the string section literally.
298 const char *LiteralEnd = LastEmitted+1;
299 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
300 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
301 ++LiteralEnd;
302 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
303 OS.write(LastEmitted, LiteralEnd-LastEmitted);
304 LastEmitted = LiteralEnd;
305 break;
306 }
307 case '\n':
308 ++LastEmitted; // Consume newline character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000309 OS << '\n'; // Indent code with newline.
Chris Lattner1e158692010-04-04 18:34:07 +0000310 break;
311 case '$': {
312 ++LastEmitted; // Consume '$' character.
313 bool Done = true;
314
315 // Handle escapes.
316 switch (*LastEmitted) {
317 default: Done = false; break;
318 case '$': // $$ -> $
319 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
320 OS << '$';
321 ++LastEmitted; // Consume second '$' character.
322 break;
323 case '(': // $( -> same as GCC's { character.
324 ++LastEmitted; // Consume '(' character.
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000325 if (CurVariant != -1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000326 report_fatal_error("Nested variants found in inline asm string: '" +
327 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000328 CurVariant = 0; // We're in the first variant now.
329 break;
330 case '|':
331 ++LastEmitted; // consume '|' character.
332 if (CurVariant == -1)
333 OS << '|'; // this is gcc's behavior for | outside a variant
334 else
335 ++CurVariant; // We're in the next variant.
336 break;
337 case ')': // $) -> same as GCC's } char.
338 ++LastEmitted; // consume ')' character.
339 if (CurVariant == -1)
340 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach00915352010-10-01 23:29:12 +0000341 else
Chris Lattner1e158692010-04-04 18:34:07 +0000342 CurVariant = -1;
343 break;
344 }
345 if (Done) break;
Jim Grosbach00915352010-10-01 23:29:12 +0000346
Chris Lattner1e158692010-04-04 18:34:07 +0000347 bool HasCurlyBraces = false;
348 if (*LastEmitted == '{') { // ${variable}
349 ++LastEmitted; // Consume '{' character.
350 HasCurlyBraces = true;
351 }
Jim Grosbach00915352010-10-01 23:29:12 +0000352
Chris Lattner1e158692010-04-04 18:34:07 +0000353 // If we have ${:foo}, then this is not a real operand reference, it is a
354 // "magic" string reference, just like in .td files. Arrange to call
355 // PrintSpecial.
356 if (HasCurlyBraces && *LastEmitted == ':') {
357 ++LastEmitted;
358 const char *StrStart = LastEmitted;
359 const char *StrEnd = strchr(StrStart, '}');
Craig Topper353eda42014-04-24 06:44:33 +0000360 if (!StrEnd)
Benjamin Kramera6769262010-04-08 10:44:28 +0000361 report_fatal_error("Unterminated ${:foo} operand in inline asm"
362 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000363
Chris Lattner1e158692010-04-04 18:34:07 +0000364 std::string Val(StrStart, StrEnd);
Chad Rosier17788312012-09-11 19:09:56 +0000365 AP->PrintSpecial(MI, OS, Val.c_str());
Chris Lattner1e158692010-04-04 18:34:07 +0000366 LastEmitted = StrEnd+1;
367 break;
368 }
Jim Grosbach00915352010-10-01 23:29:12 +0000369
Chris Lattner1e158692010-04-04 18:34:07 +0000370 const char *IDStart = LastEmitted;
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000371 const char *IDEnd = IDStart;
Jim Grosbach00915352010-10-01 23:29:12 +0000372 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
373
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000374 unsigned Val;
375 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramera6769262010-04-08 10:44:28 +0000376 report_fatal_error("Bad $ operand number in inline asm string: '" +
377 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000378 LastEmitted = IDEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000379
Chris Lattner1e158692010-04-04 18:34:07 +0000380 char Modifier[2] = { 0, 0 };
Jim Grosbach00915352010-10-01 23:29:12 +0000381
Chris Lattner1e158692010-04-04 18:34:07 +0000382 if (HasCurlyBraces) {
383 // If we have curly braces, check for a modifier character. This
384 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
385 if (*LastEmitted == ':') {
386 ++LastEmitted; // Consume ':' character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000387 if (*LastEmitted == 0)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000388 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramera6769262010-04-08 10:44:28 +0000389 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000390
Chris Lattner1e158692010-04-04 18:34:07 +0000391 Modifier[0] = *LastEmitted;
392 ++LastEmitted; // Consume modifier character.
393 }
Jim Grosbach00915352010-10-01 23:29:12 +0000394
Chris Lattner0e45d242010-04-05 22:42:30 +0000395 if (*LastEmitted != '}')
Benjamin Kramera6769262010-04-08 10:44:28 +0000396 report_fatal_error("Bad ${} expression in inline asm string: '" +
397 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000398 ++LastEmitted; // Consume '}' character.
399 }
Jim Grosbach00915352010-10-01 23:29:12 +0000400
Chris Lattner0e45d242010-04-05 22:42:30 +0000401 if (Val >= NumOperands-1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000402 report_fatal_error("Invalid $ operand number in inline asm string: '" +
403 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000404
Chris Lattner1e158692010-04-04 18:34:07 +0000405 // Okay, we finally have a value number. Ask the target to print this
406 // operand!
407 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Cheng6eb516d2011-01-07 23:50:32 +0000408 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner1e158692010-04-04 18:34:07 +0000409
410 bool Error = false;
411
412 // Scan to find the machine operand number for the operand.
413 for (; Val; --Val) {
414 if (OpNo >= MI->getNumOperands()) break;
415 unsigned OpFlags = MI->getOperand(OpNo).getImm();
416 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
417 }
418
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000419 // We may have a location metadata attached to the end of the
420 // instruction, and at no point should see metadata at any
421 // other point while processing. It's an error if so.
Eric Christopher12da1692012-03-22 01:33:51 +0000422 if (OpNo >= MI->getNumOperands() ||
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000423 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner1e158692010-04-04 18:34:07 +0000424 Error = true;
425 } else {
426 unsigned OpFlags = MI->getOperand(OpNo).getImm();
427 ++OpNo; // Skip over the ID number.
428
Matt Arsenault8b643552015-06-09 00:31:39 +0000429 if (Modifier[0] == 'l') { // Labels are target independent.
Chris Lattner1e158692010-04-04 18:34:07 +0000430 // FIXME: What if the operand isn't an MBB, report error?
Matt Arsenault8b643552015-06-09 00:31:39 +0000431 const MCSymbol *Sym = MI->getOperand(OpNo).getMBB()->getSymbol();
432 Sym->print(OS, AP->MAI);
433 } else {
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000434 if (InlineAsm::isMemKind(OpFlags)) {
Chad Rosierdb20a412012-09-10 21:10:49 +0000435 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000436 Modifier[0] ? Modifier : nullptr,
Chris Lattner1e158692010-04-04 18:34:07 +0000437 OS);
438 } else {
Chad Rosierdb20a412012-09-10 21:10:49 +0000439 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000440 Modifier[0] ? Modifier : nullptr, OS);
Chris Lattner1e158692010-04-04 18:34:07 +0000441 }
442 }
443 }
444 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000445 std::string msg;
446 raw_string_ostream Msg(msg);
Chris Lattner1e457892010-04-07 23:40:44 +0000447 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
448 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner1e158692010-04-04 18:34:07 +0000449 }
450 }
451 break;
452 }
453 }
454 }
Chad Rosier17788312012-09-11 19:09:56 +0000455 OS << '\n' << (char)0; // null terminate string.
456}
457
458/// EmitInlineAsm - This method formats and emits the specified machine
459/// instruction that is an inline asm.
460void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
461 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
462
463 // Count the number of register definitions to find the asm string.
464 unsigned NumDefs = 0;
465 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
466 ++NumDefs)
467 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
468
469 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
470
471 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
472 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
473
474 // If this asmstr is empty, just print the #APP/#NOAPP markers.
475 // These are useful to see where empty asm's wound up.
476 if (AsmStr[0] == 0) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000477 OutStreamer->emitRawComment(MAI->getInlineAsmStart());
478 OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
Chad Rosier17788312012-09-11 19:09:56 +0000479 return;
Chad Rosier7641f582012-09-10 21:36:05 +0000480 }
481
Chad Rosier17788312012-09-11 19:09:56 +0000482 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000483 // enabled, so we use emitRawComment.
Lang Hames9ff69c82015-04-24 19:11:51 +0000484 OutStreamer->emitRawComment(MAI->getInlineAsmStart());
Chad Rosier17788312012-09-11 19:09:56 +0000485
486 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
487 // it.
488 unsigned LocCookie = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000489 const MDNode *LocMD = nullptr;
Chad Rosier17788312012-09-11 19:09:56 +0000490 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
491 if (MI->getOperand(i-1).isMetadata() &&
492 (LocMD = MI->getOperand(i-1).getMetadata()) &&
493 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000494 if (const ConstantInt *CI =
495 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Chad Rosier17788312012-09-11 19:09:56 +0000496 LocCookie = CI->getZExtValue();
497 break;
498 }
499 }
500 }
501
502 // Emit the inline asm to a temporary string so we can emit it through
503 // EmitInlineAsm.
Alp Tokere69170a2014-06-26 22:52:05 +0000504 SmallString<256> StringData;
505 raw_svector_ostream OS(StringData);
Chad Rosier17788312012-09-11 19:09:56 +0000506
507 // The variant of the current asmprinter.
508 int AsmPrinterVariant = MAI->getAssemblerDialect();
509 InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
510 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
511 if (InlineAsmVariant == InlineAsm::AD_ATT)
512 EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
513 AP, LocCookie, OS);
514 else
515 EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
516
Akira Hatanakaff867732015-05-15 00:20:44 +0000517 // Reset SanitizeAddress based on the function's attribute.
518 MCTargetOptions MCOptions = TM.Options.MCOptions;
519 MCOptions.SanitizeAddress =
Matthias Braunf1caa282017-12-15 22:22:58 +0000520 MF->getFunction().hasFnAttribute(Attribute::SanitizeAddress);
Akira Hatanakaff867732015-05-15 00:20:44 +0000521
522 EmitInlineAsm(OS.str(), getSubtargetInfo(), MCOptions, LocMD,
523 MI->getInlineAsmDialect());
Jim Grosbach00915352010-10-01 23:29:12 +0000524
Chris Lattner1e158692010-04-04 18:34:07 +0000525 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000526 // enabled, so we use emitRawComment.
Lang Hames9ff69c82015-04-24 19:11:51 +0000527 OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
Chris Lattner1e158692010-04-04 18:34:07 +0000528}
529
530
531/// PrintSpecial - Print information related to the specified machine instr
532/// that is independent of the operand, and may be independent of the instr
533/// itself. This can be useful for portably encoding the comment character
534/// or other bits of target-specific knowledge into the asmstrings. The
535/// syntax used is ${:comment}. Targets can override this to add support
536/// for their own strange codes.
537void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
538 const char *Code) const {
539 if (!strcmp(Code, "private")) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000540 const DataLayout &DL = MF->getDataLayout();
541 OS << DL.getPrivateGlobalPrefix();
Chris Lattner1e158692010-04-04 18:34:07 +0000542 } else if (!strcmp(Code, "comment")) {
543 OS << MAI->getCommentString();
544 } else if (!strcmp(Code, "uid")) {
545 // Comparing the address of MI isn't sufficient, because machineinstrs may
546 // be allocated to the same address across functions.
Jim Grosbach00915352010-10-01 23:29:12 +0000547
Chris Lattner1e158692010-04-04 18:34:07 +0000548 // If this is a new LastFn instruction, bump the counter.
549 if (LastMI != MI || LastFn != getFunctionNumber()) {
550 ++Counter;
551 LastMI = MI;
552 LastFn = getFunctionNumber();
553 }
554 OS << Counter;
555 } else {
Alp Tokere69170a2014-06-26 22:52:05 +0000556 std::string msg;
557 raw_string_ostream Msg(msg);
Chris Lattner1e158692010-04-04 18:34:07 +0000558 Msg << "Unknown special formatter '" << Code
559 << "' for machine instr: " << *MI;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000560 report_fatal_error(Msg.str());
Jim Grosbach00915352010-10-01 23:29:12 +0000561 }
Chris Lattner1e158692010-04-04 18:34:07 +0000562}
563
564/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
565/// instruction, using the specified assembler variant. Targets should
566/// override this to format as appropriate.
567bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chad Rosier1f57bcb2012-09-07 20:23:29 +0000568 unsigned AsmVariant, const char *ExtraCode,
569 raw_ostream &O) {
Jack Carterb2fd5f62012-06-21 17:14:46 +0000570 // Does this asm operand have a single letter operand modifier?
571 if (ExtraCode && ExtraCode[0]) {
572 if (ExtraCode[1] != 0) return true; // Unknown modifier.
573
574 const MachineOperand &MO = MI->getOperand(OpNo);
575 switch (ExtraCode[0]) {
576 default:
577 return true; // Unknown modifier.
578 case 'c': // Substitute immediate value without immediate syntax
Jack Carterc457f622012-06-21 21:37:54 +0000579 if (MO.getType() != MachineOperand::MO_Immediate)
Jack Carterb2fd5f62012-06-21 17:14:46 +0000580 return true;
581 O << MO.getImm();
582 return false;
Jack Carterc457f622012-06-21 21:37:54 +0000583 case 'n': // Negate the immediate constant.
584 if (MO.getType() != MachineOperand::MO_Immediate)
585 return true;
586 O << -MO.getImm();
587 return false;
Nemanja Ivanovice8cbae32016-02-04 16:18:08 +0000588 case 's': // The GCC deprecated s modifier
589 if (MO.getType() != MachineOperand::MO_Immediate)
590 return true;
591 O << ((32 - MO.getImm()) & 31);
592 return false;
Jack Carterb2fd5f62012-06-21 17:14:46 +0000593 }
594 }
Chris Lattner1e158692010-04-04 18:34:07 +0000595 return true;
596}
597
598bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
599 unsigned AsmVariant,
600 const char *ExtraCode, raw_ostream &O) {
601 // Target doesn't support this yet!
602 return true;
603}
604
Eric Christopher64d35be2015-02-19 19:52:25 +0000605void AsmPrinter::emitInlineAsmStart() const {}
Toma Tabacua23f13c2014-12-17 10:56:16 +0000606
Rafael Espindola65fd0a82014-01-24 15:47:54 +0000607void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
David Peixottoea2bcb92014-02-06 18:19:40 +0000608 const MCSubtargetInfo *EndInfo) const {}