blob: b092704f3cb750d2e6eedab9f10d2fea349ac7d8 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner1e158692010-04-04 18:34:07 +00006//
7//===----------------------------------------------------------------------===//
8//
Chris Lattner9efd1182010-04-04 19:09:29 +00009// This file implements the inline assembler pieces of the AsmPrinter class.
Chris Lattner1e158692010-04-04 18:34:07 +000010//
11//===----------------------------------------------------------------------===//
12
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "llvm/ADT/SmallString.h"
14#include "llvm/ADT/Twine.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/CodeGen/MachineBasicBlock.h"
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +000017#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000019#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000020#include "llvm/CodeGen/TargetRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
Rafael Espindola58873562014-01-03 19:21:54 +000022#include "llvm/IR/DataLayout.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/InlineAsm.h"
24#include "llvm/IR/LLVMContext.h"
25#include "llvm/IR/Module.h"
Chris Lattner1e158692010-04-04 18:34:07 +000026#include "llvm/MC/MCAsmInfo.h"
Benjamin Kramerb3e8a6d2016-01-27 10:01:28 +000027#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Chris Lattner1e158692010-04-04 18:34:07 +000028#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000029#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner1e158692010-04-04 18:34:07 +000030#include "llvm/MC/MCSymbol.h"
Chris Lattner1e158692010-04-04 18:34:07 +000031#include "llvm/Support/ErrorHandling.h"
Chris Lattner8900ef12010-04-05 23:11:24 +000032#include "llvm/Support/MemoryBuffer.h"
33#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000034#include "llvm/Support/TargetRegistry.h"
Chris Lattner1e158692010-04-04 18:34:07 +000035#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Target/TargetMachine.h"
Chris Lattner1e158692010-04-04 18:34:07 +000037using namespace llvm;
38
Chandler Carruth1b9dde02014-04-22 02:02:50 +000039#define DEBUG_TYPE "asm-printer"
40
Chad Rosierb759ede2012-09-07 18:16:38 +000041/// srcMgrDiagHandler - This callback is invoked when the SourceMgr for an
Chris Lattner300fa452010-11-17 08:03:32 +000042/// inline asm has an error in it. diagInfo is a pointer to the SrcMgrDiagInfo
43/// struct above.
Chad Rosierb759ede2012-09-07 18:16:38 +000044static void srcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
Sanne Wouda29338752017-02-08 14:48:05 +000045 AsmPrinter::SrcMgrDiagInfo *DiagInfo =
46 static_cast<AsmPrinter::SrcMgrDiagInfo *>(diagInfo);
Chris Lattner300fa452010-11-17 08:03:32 +000047 assert(DiagInfo && "Diagnostic context not passed down?");
Jim Grosbach098f5a22011-09-21 21:36:53 +000048
Sanne Wouda91eadad2017-02-13 13:58:00 +000049 // Look up a LocInfo for the buffer this diagnostic is coming from.
50 unsigned BufNum = DiagInfo->SrcMgr.FindBufferContainingLoc(Diag.getLoc());
51 const MDNode *LocInfo = nullptr;
52 if (BufNum > 0 && BufNum <= DiagInfo->LocInfos.size())
53 LocInfo = DiagInfo->LocInfos[BufNum-1];
54
Chris Lattner79ffdc72010-11-17 08:20:42 +000055 // If the inline asm had metadata associated with it, pull out a location
56 // cookie corresponding to which line the error occurred on.
Chris Lattner300fa452010-11-17 08:03:32 +000057 unsigned LocCookie = 0;
Sanne Wouda91eadad2017-02-13 13:58:00 +000058 if (LocInfo) {
Chris Lattner79ffdc72010-11-17 08:20:42 +000059 unsigned ErrorLine = Diag.getLineNo()-1;
60 if (ErrorLine >= LocInfo->getNumOperands())
61 ErrorLine = 0;
Jim Grosbach098f5a22011-09-21 21:36:53 +000062
Chris Lattner79ffdc72010-11-17 08:20:42 +000063 if (LocInfo->getNumOperands() != 0)
64 if (const ConstantInt *CI =
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000065 mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
Chris Lattner300fa452010-11-17 08:03:32 +000066 LocCookie = CI->getZExtValue();
Chris Lattner79ffdc72010-11-17 08:20:42 +000067 }
Jim Grosbach098f5a22011-09-21 21:36:53 +000068
Chris Lattnerb0e36082010-11-17 08:13:01 +000069 DiagInfo->DiagHandler(Diag, DiagInfo->DiagContext, LocCookie);
Chris Lattner300fa452010-11-17 08:03:32 +000070}
71
Ties Stuij9c16d802018-08-30 12:52:35 +000072unsigned AsmPrinter::addInlineAsmDiagBuffer(StringRef AsmStr,
73 const MDNode *LocMDNode) const {
74 if (!DiagInfo) {
75 DiagInfo = make_unique<SrcMgrDiagInfo>();
76
77 MCContext &Context = MMI->getContext();
78 Context.setInlineSourceManager(&DiagInfo->SrcMgr);
79
80 LLVMContext &LLVMCtx = MMI->getModule()->getContext();
81 if (LLVMCtx.getInlineAsmDiagnosticHandler()) {
82 DiagInfo->DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
83 DiagInfo->DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
84 DiagInfo->SrcMgr.setDiagHandler(srcMgrDiagHandler, DiagInfo.get());
85 }
86 }
87
88 SourceMgr &SrcMgr = DiagInfo->SrcMgr;
89
90 std::unique_ptr<MemoryBuffer> Buffer;
91 // The inline asm source manager will outlive AsmStr, so make a copy of the
92 // string for SourceMgr to own.
93 Buffer = MemoryBuffer::getMemBufferCopy(AsmStr, "<inline asm>");
94
95 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
96 unsigned BufNum = SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
97
98 // Store LocMDNode in DiagInfo, using BufNum as an identifier.
99 if (LocMDNode) {
100 DiagInfo->LocInfos.resize(BufNum);
101 DiagInfo->LocInfos[BufNum - 1] = LocMDNode;
102 }
103
104 return BufNum;
105}
106
107
Chris Lattner1e158692010-04-04 18:34:07 +0000108/// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
Akira Hatanaka322ffce2015-03-16 18:02:16 +0000109void AsmPrinter::EmitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
Akira Hatanakaff867732015-05-15 00:20:44 +0000110 const MCTargetOptions &MCOptions,
Akira Hatanaka322ffce2015-03-16 18:02:16 +0000111 const MDNode *LocMDNode,
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000112 InlineAsm::AsmDialect Dialect) const {
Chris Lattner1e158692010-04-04 18:34:07 +0000113 assert(!Str.empty() && "Can't emit empty inline asm block");
Jim Grosbach00915352010-10-01 23:29:12 +0000114
Chris Lattner8900ef12010-04-05 23:11:24 +0000115 // Remember if the buffer is nul terminated or not so we can avoid a copy.
116 bool isNullTerminated = Str.back() == 0;
117 if (isNullTerminated)
118 Str = Str.substr(0, Str.size()-1);
Jim Grosbach00915352010-10-01 23:29:12 +0000119
Daniel Sanders753e1762014-02-13 14:44:26 +0000120 // If the output streamer does not have mature MC support or the integrated
121 // assembler has been disabled, just emit the blob textually.
122 // Otherwise parse the asm and emit it via MC support.
Chris Lattner1e158692010-04-04 18:34:07 +0000123 // This is useful in case the asm parser doesn't handle something but the
124 // system assembler does.
Daniel Sanders753e1762014-02-13 14:44:26 +0000125 const MCAsmInfo *MCAI = TM.getMCAsmInfo();
126 assert(MCAI && "No MCAsmInfo");
127 if (!MCAI->useIntegratedAssembler() &&
Lang Hames9ff69c82015-04-24 19:11:51 +0000128 !OutStreamer->isIntegratedAssemblerRequired()) {
Eric Christopher64d35be2015-02-19 19:52:25 +0000129 emitInlineAsmStart();
Lang Hames9ff69c82015-04-24 19:11:51 +0000130 OutStreamer->EmitRawText(Str);
Akira Hatanaka322ffce2015-03-16 18:02:16 +0000131 emitInlineAsmEnd(STI, nullptr);
Chris Lattner1e158692010-04-04 18:34:07 +0000132 return;
133 }
Jim Grosbach00915352010-10-01 23:29:12 +0000134
Ties Stuij9c16d802018-08-30 12:52:35 +0000135 unsigned BufNum = addInlineAsmDiagBuffer(Str, LocMDNode);
136 DiagInfo->SrcMgr.setIncludeDirs(MCOptions.IASSearchPaths);
Saleem Abdulrasool6252bd82017-01-05 05:56:39 +0000137
Ties Stuij9c16d802018-08-30 12:52:35 +0000138 std::unique_ptr<MCAsmParser> Parser(createMCAsmParser(
139 DiagInfo->SrcMgr, OutContext, *OutStreamer, *MAI, BufNum));
Evan Cheng4d1ca962011-07-08 01:53:10 +0000140
Nirav Dave6c0665e2018-04-30 19:22:40 +0000141 // Do not use assembler-level information for parsing inline assembly.
142 OutStreamer->setUseAssemblerInfoForParsing(false);
143
Eric Christopher3f05e1a2015-02-21 09:09:15 +0000144 // We create a new MCInstrInfo here since we might be at the module level
Eric Christopher2105ae92015-02-19 23:52:35 +0000145 // and not have a MachineFunction to initialize the TargetInstrInfo from and
Eric Christopher3f05e1a2015-02-21 09:09:15 +0000146 // we only need MCInstrInfo for asm parsing. We create one unconditionally
147 // because it's not subtarget dependent.
148 std::unique_ptr<MCInstrInfo> MII(TM.getTarget().createMCInstrInfo());
Eric Christophercbdbf392015-02-19 21:29:51 +0000149 std::unique_ptr<MCTargetAsmParser> TAP(TM.getTarget().createMCAsmParser(
Akira Hatanakab11ef082015-11-14 06:35:56 +0000150 STI, *Parser, *MII, MCOptions));
Chris Lattner8900ef12010-04-05 23:11:24 +0000151 if (!TAP)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000152 report_fatal_error("Inline asm not supported by this streamer because"
Benjamin Kramera6769262010-04-08 10:44:28 +0000153 " we don't have an asm parser for this target\n");
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000154 Parser->setAssemblerDialect(Dialect);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000155 Parser->setTargetParser(*TAP.get());
Reid Kleckner953bdce2018-10-24 20:23:57 +0000156 // Enable lexing Masm binary and hex integer literals in intel inline
157 // assembly.
Andrew V. Tischenkoc3c67232017-04-26 09:56:59 +0000158 if (Dialect == InlineAsm::AD_Intel)
Reid Kleckner953bdce2018-10-24 20:23:57 +0000159 Parser->getLexer().setLexMasmIntegers(true);
Chris Lattner8900ef12010-04-05 23:11:24 +0000160
Eric Christopher64d35be2015-02-19 19:52:25 +0000161 emitInlineAsmStart();
Chris Lattner8900ef12010-04-05 23:11:24 +0000162 // Don't implicitly switch to the text section before the asm.
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000163 int Res = Parser->Run(/*NoInitialTextSection*/ true,
164 /*NoFinalize*/ true);
Akira Hatanakab11ef082015-11-14 06:35:56 +0000165 emitInlineAsmEnd(STI, &TAP->getSTI());
Sanne Wouda29338752017-02-08 14:48:05 +0000166
Sanne Wouda29338752017-02-08 14:48:05 +0000167 if (Res && !DiagInfo->DiagHandler)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000168 report_fatal_error("Error parsing inline asm\n");
Chris Lattner1e158692010-04-04 18:34:07 +0000169}
170
Chad Rosier17788312012-09-11 19:09:56 +0000171static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
Nick Desaulniers5277b3f2019-04-10 16:38:43 +0000172 MachineModuleInfo *MMI, AsmPrinter *AP,
173 unsigned LocCookie, raw_ostream &OS) {
Chad Rosier17788312012-09-11 19:09:56 +0000174 // Switch to the inline assembly variant.
175 OS << "\t.intel_syntax\n\t";
Chris Lattner1e158692010-04-04 18:34:07 +0000176
Chad Rosier17788312012-09-11 19:09:56 +0000177 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner1e158692010-04-04 18:34:07 +0000178 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach00915352010-10-01 23:29:12 +0000179
Chad Rosier17788312012-09-11 19:09:56 +0000180 while (*LastEmitted) {
181 switch (*LastEmitted) {
182 default: {
183 // Not a special case, emit the string section literally.
184 const char *LiteralEnd = LastEmitted+1;
185 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
186 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
187 ++LiteralEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000188
Chad Rosier17788312012-09-11 19:09:56 +0000189 OS.write(LastEmitted, LiteralEnd-LastEmitted);
190 LastEmitted = LiteralEnd;
191 break;
192 }
193 case '\n':
194 ++LastEmitted; // Consume newline character.
195 OS << '\n'; // Indent code with newline.
196 break;
197 case '$': {
198 ++LastEmitted; // Consume '$' character.
199 bool Done = true;
Chris Lattner1e158692010-04-04 18:34:07 +0000200
Chad Rosier17788312012-09-11 19:09:56 +0000201 // Handle escapes.
202 switch (*LastEmitted) {
203 default: Done = false; break;
204 case '$':
205 ++LastEmitted; // Consume second '$' character.
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000206 break;
207 }
Chad Rosier17788312012-09-11 19:09:56 +0000208 if (Done) break;
209
Reid Klecknerc68a6c42016-11-29 00:29:27 +0000210 // If we have ${:foo}, then this is not a real operand reference, it is a
211 // "magic" string reference, just like in .td files. Arrange to call
212 // PrintSpecial.
213 if (LastEmitted[0] == '{' && LastEmitted[1] == ':') {
214 LastEmitted += 2;
215 const char *StrStart = LastEmitted;
216 const char *StrEnd = strchr(StrStart, '}');
217 if (!StrEnd)
218 report_fatal_error("Unterminated ${:foo} operand in inline asm"
219 " string: '" + Twine(AsmStr) + "'");
220
221 std::string Val(StrStart, StrEnd);
222 AP->PrintSpecial(MI, OS, Val.c_str());
223 LastEmitted = StrEnd+1;
224 break;
225 }
226
Chad Rosier17788312012-09-11 19:09:56 +0000227 const char *IDStart = LastEmitted;
228 const char *IDEnd = IDStart;
229 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
230
231 unsigned Val;
232 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
233 report_fatal_error("Bad $ operand number in inline asm string: '" +
234 Twine(AsmStr) + "'");
235 LastEmitted = IDEnd;
236
237 if (Val >= NumOperands-1)
238 report_fatal_error("Invalid $ operand number in inline asm string: '" +
239 Twine(AsmStr) + "'");
240
241 // Okay, we finally have a value number. Ask the target to print this
242 // operand!
243 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
244
245 bool Error = false;
246
247 // Scan to find the machine operand number for the operand.
248 for (; Val; --Val) {
249 if (OpNo >= MI->getNumOperands()) break;
250 unsigned OpFlags = MI->getOperand(OpNo).getImm();
251 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
252 }
253
254 // We may have a location metadata attached to the end of the
255 // instruction, and at no point should see metadata at any
256 // other point while processing. It's an error if so.
257 if (OpNo >= MI->getNumOperands() ||
258 MI->getOperand(OpNo).isMetadata()) {
259 Error = true;
260 } else {
261 unsigned OpFlags = MI->getOperand(OpNo).getImm();
262 ++OpNo; // Skip over the ID number.
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000263
Chad Rosier17788312012-09-11 19:09:56 +0000264 if (InlineAsm::isMemKind(OpFlags)) {
Nick Desaulniers5277b3f2019-04-10 16:38:43 +0000265 Error = AP->PrintAsmMemoryOperand(MI, OpNo, /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000266 } else {
Nick Desaulniers5277b3f2019-04-10 16:38:43 +0000267 Error = AP->PrintAsmOperand(MI, OpNo, /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000268 }
269 }
270 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000271 std::string msg;
272 raw_string_ostream Msg(msg);
Chad Rosier17788312012-09-11 19:09:56 +0000273 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
274 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
275 }
276 break;
277 }
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000278 }
Chris Lattner51065562010-04-07 05:38:05 +0000279 }
Chad Rosier17788312012-09-11 19:09:56 +0000280 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
281}
Jim Grosbach00915352010-10-01 23:29:12 +0000282
Chad Rosier17788312012-09-11 19:09:56 +0000283static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
Nick Desaulniers5277b3f2019-04-10 16:38:43 +0000284 MachineModuleInfo *MMI, int AsmPrinterVariant,
285 AsmPrinter *AP, unsigned LocCookie,
286 raw_ostream &OS) {
Chris Lattner1e158692010-04-04 18:34:07 +0000287 int CurVariant = -1; // The number of the {.|.|.} region we are in.
288 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chad Rosier17788312012-09-11 19:09:56 +0000289 unsigned NumOperands = MI->getNumOperands();
290
291 OS << '\t';
Jim Grosbach00915352010-10-01 23:29:12 +0000292
Chris Lattner1e158692010-04-04 18:34:07 +0000293 while (*LastEmitted) {
294 switch (*LastEmitted) {
295 default: {
296 // Not a special case, emit the string section literally.
297 const char *LiteralEnd = LastEmitted+1;
298 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
299 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
300 ++LiteralEnd;
301 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
302 OS.write(LastEmitted, LiteralEnd-LastEmitted);
303 LastEmitted = LiteralEnd;
304 break;
305 }
306 case '\n':
307 ++LastEmitted; // Consume newline character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000308 OS << '\n'; // Indent code with newline.
Chris Lattner1e158692010-04-04 18:34:07 +0000309 break;
310 case '$': {
311 ++LastEmitted; // Consume '$' character.
312 bool Done = true;
313
314 // Handle escapes.
315 switch (*LastEmitted) {
316 default: Done = false; break;
317 case '$': // $$ -> $
318 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
319 OS << '$';
320 ++LastEmitted; // Consume second '$' character.
321 break;
322 case '(': // $( -> same as GCC's { character.
323 ++LastEmitted; // Consume '(' character.
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000324 if (CurVariant != -1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000325 report_fatal_error("Nested variants found in inline asm string: '" +
326 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000327 CurVariant = 0; // We're in the first variant now.
328 break;
329 case '|':
330 ++LastEmitted; // consume '|' character.
331 if (CurVariant == -1)
332 OS << '|'; // this is gcc's behavior for | outside a variant
333 else
334 ++CurVariant; // We're in the next variant.
335 break;
336 case ')': // $) -> same as GCC's } char.
337 ++LastEmitted; // consume ')' character.
338 if (CurVariant == -1)
339 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach00915352010-10-01 23:29:12 +0000340 else
Chris Lattner1e158692010-04-04 18:34:07 +0000341 CurVariant = -1;
342 break;
343 }
344 if (Done) break;
Jim Grosbach00915352010-10-01 23:29:12 +0000345
Chris Lattner1e158692010-04-04 18:34:07 +0000346 bool HasCurlyBraces = false;
347 if (*LastEmitted == '{') { // ${variable}
348 ++LastEmitted; // Consume '{' character.
349 HasCurlyBraces = true;
350 }
Jim Grosbach00915352010-10-01 23:29:12 +0000351
Chris Lattner1e158692010-04-04 18:34:07 +0000352 // If we have ${:foo}, then this is not a real operand reference, it is a
353 // "magic" string reference, just like in .td files. Arrange to call
354 // PrintSpecial.
355 if (HasCurlyBraces && *LastEmitted == ':') {
356 ++LastEmitted;
357 const char *StrStart = LastEmitted;
358 const char *StrEnd = strchr(StrStart, '}');
Craig Topper353eda42014-04-24 06:44:33 +0000359 if (!StrEnd)
Benjamin Kramera6769262010-04-08 10:44:28 +0000360 report_fatal_error("Unterminated ${:foo} operand in inline asm"
361 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000362
Chris Lattner1e158692010-04-04 18:34:07 +0000363 std::string Val(StrStart, StrEnd);
Chad Rosier17788312012-09-11 19:09:56 +0000364 AP->PrintSpecial(MI, OS, Val.c_str());
Chris Lattner1e158692010-04-04 18:34:07 +0000365 LastEmitted = StrEnd+1;
366 break;
367 }
Jim Grosbach00915352010-10-01 23:29:12 +0000368
Chris Lattner1e158692010-04-04 18:34:07 +0000369 const char *IDStart = LastEmitted;
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000370 const char *IDEnd = IDStart;
Jim Grosbach00915352010-10-01 23:29:12 +0000371 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
372
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000373 unsigned Val;
374 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramera6769262010-04-08 10:44:28 +0000375 report_fatal_error("Bad $ operand number in inline asm string: '" +
376 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000377 LastEmitted = IDEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000378
Chris Lattner1e158692010-04-04 18:34:07 +0000379 char Modifier[2] = { 0, 0 };
Jim Grosbach00915352010-10-01 23:29:12 +0000380
Chris Lattner1e158692010-04-04 18:34:07 +0000381 if (HasCurlyBraces) {
382 // If we have curly braces, check for a modifier character. This
383 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
384 if (*LastEmitted == ':') {
385 ++LastEmitted; // Consume ':' character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000386 if (*LastEmitted == 0)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000387 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramera6769262010-04-08 10:44:28 +0000388 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000389
Chris Lattner1e158692010-04-04 18:34:07 +0000390 Modifier[0] = *LastEmitted;
391 ++LastEmitted; // Consume modifier character.
392 }
Jim Grosbach00915352010-10-01 23:29:12 +0000393
Chris Lattner0e45d242010-04-05 22:42:30 +0000394 if (*LastEmitted != '}')
Benjamin Kramera6769262010-04-08 10:44:28 +0000395 report_fatal_error("Bad ${} expression in inline asm string: '" +
396 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000397 ++LastEmitted; // Consume '}' character.
398 }
Jim Grosbach00915352010-10-01 23:29:12 +0000399
Chris Lattner0e45d242010-04-05 22:42:30 +0000400 if (Val >= NumOperands-1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000401 report_fatal_error("Invalid $ operand number in inline asm string: '" +
402 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000403
Chris Lattner1e158692010-04-04 18:34:07 +0000404 // Okay, we finally have a value number. Ask the target to print this
405 // operand!
406 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Cheng6eb516d2011-01-07 23:50:32 +0000407 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner1e158692010-04-04 18:34:07 +0000408
409 bool Error = false;
410
411 // Scan to find the machine operand number for the operand.
412 for (; Val; --Val) {
413 if (OpNo >= MI->getNumOperands()) break;
414 unsigned OpFlags = MI->getOperand(OpNo).getImm();
415 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
416 }
417
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000418 // We may have a location metadata attached to the end of the
419 // instruction, and at no point should see metadata at any
420 // other point while processing. It's an error if so.
Eric Christopher12da1692012-03-22 01:33:51 +0000421 if (OpNo >= MI->getNumOperands() ||
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000422 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner1e158692010-04-04 18:34:07 +0000423 Error = true;
424 } else {
425 unsigned OpFlags = MI->getOperand(OpNo).getImm();
426 ++OpNo; // Skip over the ID number.
427
Ali Tamur62633652019-04-18 02:39:37 +0000428 // FIXME: Shouldn't arch-independent output template handling go into
Nick Desaulniersa2077ba2019-04-17 18:22:48 +0000429 // PrintAsmOperand?
Matt Arsenault8b643552015-06-09 00:31:39 +0000430 if (Modifier[0] == 'l') { // Labels are target independent.
Craig Topper784929d2019-02-08 20:48:56 +0000431 if (MI->getOperand(OpNo).isBlockAddress()) {
432 const BlockAddress *BA = MI->getOperand(OpNo).getBlockAddress();
433 MCSymbol *Sym = AP->GetBlockAddressSymbol(BA);
434 Sym->print(OS, AP->MAI);
435 } else if (MI->getOperand(OpNo).isMBB()) {
436 const MCSymbol *Sym = MI->getOperand(OpNo).getMBB()->getSymbol();
437 Sym->print(OS, AP->MAI);
438 } else {
439 Error = true;
440 }
Matt Arsenault8b643552015-06-09 00:31:39 +0000441 } else {
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000442 if (InlineAsm::isMemKind(OpFlags)) {
Nick Desaulniers5277b3f2019-04-10 16:38:43 +0000443 Error = AP->PrintAsmMemoryOperand(
444 MI, OpNo, Modifier[0] ? Modifier : nullptr, OS);
Chris Lattner1e158692010-04-04 18:34:07 +0000445 } else {
Nick Desaulniers5277b3f2019-04-10 16:38:43 +0000446 Error = AP->PrintAsmOperand(MI, OpNo,
Craig Topper353eda42014-04-24 06:44:33 +0000447 Modifier[0] ? Modifier : nullptr, OS);
Chris Lattner1e158692010-04-04 18:34:07 +0000448 }
449 }
450 }
451 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000452 std::string msg;
453 raw_string_ostream Msg(msg);
Chris Lattner1e457892010-04-07 23:40:44 +0000454 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
455 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner1e158692010-04-04 18:34:07 +0000456 }
457 }
458 break;
459 }
460 }
461 }
Chad Rosier17788312012-09-11 19:09:56 +0000462 OS << '\n' << (char)0; // null terminate string.
463}
464
465/// EmitInlineAsm - This method formats and emits the specified machine
466/// instruction that is an inline asm.
467void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
468 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
469
470 // Count the number of register definitions to find the asm string.
471 unsigned NumDefs = 0;
472 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
473 ++NumDefs)
474 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
475
476 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
477
478 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
479 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
480
481 // If this asmstr is empty, just print the #APP/#NOAPP markers.
482 // These are useful to see where empty asm's wound up.
483 if (AsmStr[0] == 0) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000484 OutStreamer->emitRawComment(MAI->getInlineAsmStart());
485 OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
Chad Rosier17788312012-09-11 19:09:56 +0000486 return;
Chad Rosier7641f582012-09-10 21:36:05 +0000487 }
488
Chad Rosier17788312012-09-11 19:09:56 +0000489 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000490 // enabled, so we use emitRawComment.
Lang Hames9ff69c82015-04-24 19:11:51 +0000491 OutStreamer->emitRawComment(MAI->getInlineAsmStart());
Chad Rosier17788312012-09-11 19:09:56 +0000492
493 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
494 // it.
495 unsigned LocCookie = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000496 const MDNode *LocMD = nullptr;
Chad Rosier17788312012-09-11 19:09:56 +0000497 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
498 if (MI->getOperand(i-1).isMetadata() &&
499 (LocMD = MI->getOperand(i-1).getMetadata()) &&
500 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000501 if (const ConstantInt *CI =
502 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Chad Rosier17788312012-09-11 19:09:56 +0000503 LocCookie = CI->getZExtValue();
504 break;
505 }
506 }
507 }
508
509 // Emit the inline asm to a temporary string so we can emit it through
510 // EmitInlineAsm.
Alp Tokere69170a2014-06-26 22:52:05 +0000511 SmallString<256> StringData;
512 raw_svector_ostream OS(StringData);
Chad Rosier17788312012-09-11 19:09:56 +0000513
514 // The variant of the current asmprinter.
515 int AsmPrinterVariant = MAI->getAssemblerDialect();
Chad Rosier17788312012-09-11 19:09:56 +0000516 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Nick Desaulniers5277b3f2019-04-10 16:38:43 +0000517 if (MI->getInlineAsmDialect() == InlineAsm::AD_ATT)
518 EmitGCCInlineAsmStr(AsmStr, MI, MMI, AsmPrinterVariant, AP, LocCookie, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000519 else
Nick Desaulniers5277b3f2019-04-10 16:38:43 +0000520 EmitMSInlineAsmStr(AsmStr, MI, MMI, AP, LocCookie, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000521
Ties Stuij9c16d802018-08-30 12:52:35 +0000522 // Emit warnings if we use reserved registers on the clobber list, as
523 // that might give surprising results.
524 std::vector<std::string> RestrRegs;
525 // Start with the first operand descriptor, and iterate over them.
526 for (unsigned I = InlineAsm::MIOp_FirstOperand, NumOps = MI->getNumOperands();
527 I < NumOps; ++I) {
528 const MachineOperand &MO = MI->getOperand(I);
529 if (MO.isImm()) {
530 unsigned Flags = MO.getImm();
531 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
532 if (InlineAsm::getKind(Flags) == InlineAsm::Kind_Clobber &&
533 !TRI->isAsmClobberable(*MF, MI->getOperand(I + 1).getReg())) {
534 RestrRegs.push_back(TRI->getName(MI->getOperand(I + 1).getReg()));
535 }
536 // Skip to one before the next operand descriptor, if it exists.
537 I += InlineAsm::getNumOperandRegisters(Flags);
538 }
539 }
540
541 if (!RestrRegs.empty()) {
542 unsigned BufNum = addInlineAsmDiagBuffer(OS.str(), LocMD);
543 auto &SrcMgr = DiagInfo->SrcMgr;
544 SMLoc Loc = SMLoc::getFromPointer(
545 SrcMgr.getMemoryBuffer(BufNum)->getBuffer().begin());
546
547 std::string Msg = "inline asm clobber list contains reserved registers: ";
548 for (auto I = RestrRegs.begin(), E = RestrRegs.end(); I != E; I++) {
549 if(I != RestrRegs.begin())
550 Msg += ", ";
551 Msg += *I;
552 }
553 std::string Note = "Reserved registers on the clobber list may not be "
554 "preserved across the asm statement, and clobbering them may "
555 "lead to undefined behaviour.";
556 SrcMgr.PrintMessage(Loc, SourceMgr::DK_Warning, Msg);
557 SrcMgr.PrintMessage(Loc, SourceMgr::DK_Note, Note);
558 }
559
Evgeniy Stepanovaedec3f2019-03-11 21:50:10 +0000560 EmitInlineAsm(OS.str(), getSubtargetInfo(), TM.Options.MCOptions, LocMD,
Akira Hatanakaff867732015-05-15 00:20:44 +0000561 MI->getInlineAsmDialect());
Jim Grosbach00915352010-10-01 23:29:12 +0000562
Chris Lattner1e158692010-04-04 18:34:07 +0000563 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000564 // enabled, so we use emitRawComment.
Lang Hames9ff69c82015-04-24 19:11:51 +0000565 OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
Chris Lattner1e158692010-04-04 18:34:07 +0000566}
567
568
569/// PrintSpecial - Print information related to the specified machine instr
570/// that is independent of the operand, and may be independent of the instr
571/// itself. This can be useful for portably encoding the comment character
572/// or other bits of target-specific knowledge into the asmstrings. The
573/// syntax used is ${:comment}. Targets can override this to add support
574/// for their own strange codes.
575void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
576 const char *Code) const {
577 if (!strcmp(Code, "private")) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000578 const DataLayout &DL = MF->getDataLayout();
579 OS << DL.getPrivateGlobalPrefix();
Chris Lattner1e158692010-04-04 18:34:07 +0000580 } else if (!strcmp(Code, "comment")) {
581 OS << MAI->getCommentString();
582 } else if (!strcmp(Code, "uid")) {
583 // Comparing the address of MI isn't sufficient, because machineinstrs may
584 // be allocated to the same address across functions.
Jim Grosbach00915352010-10-01 23:29:12 +0000585
Chris Lattner1e158692010-04-04 18:34:07 +0000586 // If this is a new LastFn instruction, bump the counter.
587 if (LastMI != MI || LastFn != getFunctionNumber()) {
588 ++Counter;
589 LastMI = MI;
590 LastFn = getFunctionNumber();
591 }
592 OS << Counter;
593 } else {
Alp Tokere69170a2014-06-26 22:52:05 +0000594 std::string msg;
595 raw_string_ostream Msg(msg);
Chris Lattner1e158692010-04-04 18:34:07 +0000596 Msg << "Unknown special formatter '" << Code
597 << "' for machine instr: " << *MI;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000598 report_fatal_error(Msg.str());
Jim Grosbach00915352010-10-01 23:29:12 +0000599 }
Chris Lattner1e158692010-04-04 18:34:07 +0000600}
601
602/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
603/// instruction, using the specified assembler variant. Targets should
Nick Desaulniers9609ce22019-04-17 22:21:10 +0000604/// override this to format as appropriate for machine specific ExtraCodes
605/// or when the arch-independent handling would be too complex otherwise.
Chris Lattner1e158692010-04-04 18:34:07 +0000606bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Nick Desaulniers5277b3f2019-04-10 16:38:43 +0000607 const char *ExtraCode, raw_ostream &O) {
Jack Carterb2fd5f62012-06-21 17:14:46 +0000608 // Does this asm operand have a single letter operand modifier?
609 if (ExtraCode && ExtraCode[0]) {
610 if (ExtraCode[1] != 0) return true; // Unknown modifier.
611
Nick Desaulniersa2077ba2019-04-17 18:22:48 +0000612 // https://gcc.gnu.org/onlinedocs/gccint/Output-Template.html
Jack Carterb2fd5f62012-06-21 17:14:46 +0000613 const MachineOperand &MO = MI->getOperand(OpNo);
614 switch (ExtraCode[0]) {
615 default:
616 return true; // Unknown modifier.
Nick Desaulniers9609ce22019-04-17 22:21:10 +0000617 case 'a': // Print as memory address.
618 if (MO.isReg()) {
619 PrintAsmMemoryOperand(MI, OpNo, nullptr, O);
620 return false;
621 }
622 LLVM_FALLTHROUGH; // GCC allows '%a' to behave like '%c' with immediates.
Jack Carterb2fd5f62012-06-21 17:14:46 +0000623 case 'c': // Substitute immediate value without immediate syntax
Nick Desaulniers9609ce22019-04-17 22:21:10 +0000624 if (!MO.isImm())
Jack Carterb2fd5f62012-06-21 17:14:46 +0000625 return true;
626 O << MO.getImm();
627 return false;
Jack Carterc457f622012-06-21 21:37:54 +0000628 case 'n': // Negate the immediate constant.
Nick Desaulniers9609ce22019-04-17 22:21:10 +0000629 if (!MO.isImm())
Jack Carterc457f622012-06-21 21:37:54 +0000630 return true;
631 O << -MO.getImm();
632 return false;
Nemanja Ivanovice8cbae32016-02-04 16:18:08 +0000633 case 's': // The GCC deprecated s modifier
Nick Desaulniers9609ce22019-04-17 22:21:10 +0000634 if (!MO.isImm())
Nemanja Ivanovice8cbae32016-02-04 16:18:08 +0000635 return true;
636 O << ((32 - MO.getImm()) & 31);
637 return false;
Jack Carterb2fd5f62012-06-21 17:14:46 +0000638 }
639 }
Chris Lattner1e158692010-04-04 18:34:07 +0000640 return true;
641}
642
643bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner1e158692010-04-04 18:34:07 +0000644 const char *ExtraCode, raw_ostream &O) {
645 // Target doesn't support this yet!
646 return true;
647}
648
Eric Christopher64d35be2015-02-19 19:52:25 +0000649void AsmPrinter::emitInlineAsmStart() const {}
Toma Tabacua23f13c2014-12-17 10:56:16 +0000650
Rafael Espindola65fd0a82014-01-24 15:47:54 +0000651void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
David Peixottoea2bcb92014-02-06 18:19:40 +0000652 const MCSubtargetInfo *EndInfo) const {}