blob: e47eb86bcfe84398b19b691071c60e941ff97aa5 [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,
172 MachineModuleInfo *MMI, int InlineAsmVariant,
173 AsmPrinter *AP, unsigned LocCookie,
174 raw_ostream &OS) {
175 // Switch to the inline assembly variant.
176 OS << "\t.intel_syntax\n\t";
Chris Lattner1e158692010-04-04 18:34:07 +0000177
Chad Rosier17788312012-09-11 19:09:56 +0000178 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner1e158692010-04-04 18:34:07 +0000179 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach00915352010-10-01 23:29:12 +0000180
Chad Rosier17788312012-09-11 19:09:56 +0000181 while (*LastEmitted) {
182 switch (*LastEmitted) {
183 default: {
184 // Not a special case, emit the string section literally.
185 const char *LiteralEnd = LastEmitted+1;
186 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
187 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
188 ++LiteralEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000189
Chad Rosier17788312012-09-11 19:09:56 +0000190 OS.write(LastEmitted, LiteralEnd-LastEmitted);
191 LastEmitted = LiteralEnd;
192 break;
193 }
194 case '\n':
195 ++LastEmitted; // Consume newline character.
196 OS << '\n'; // Indent code with newline.
197 break;
198 case '$': {
199 ++LastEmitted; // Consume '$' character.
200 bool Done = true;
Chris Lattner1e158692010-04-04 18:34:07 +0000201
Chad Rosier17788312012-09-11 19:09:56 +0000202 // Handle escapes.
203 switch (*LastEmitted) {
204 default: Done = false; break;
205 case '$':
206 ++LastEmitted; // Consume second '$' character.
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000207 break;
208 }
Chad Rosier17788312012-09-11 19:09:56 +0000209 if (Done) break;
210
Reid Klecknerc68a6c42016-11-29 00:29:27 +0000211 // If we have ${:foo}, then this is not a real operand reference, it is a
212 // "magic" string reference, just like in .td files. Arrange to call
213 // PrintSpecial.
214 if (LastEmitted[0] == '{' && LastEmitted[1] == ':') {
215 LastEmitted += 2;
216 const char *StrStart = LastEmitted;
217 const char *StrEnd = strchr(StrStart, '}');
218 if (!StrEnd)
219 report_fatal_error("Unterminated ${:foo} operand in inline asm"
220 " string: '" + Twine(AsmStr) + "'");
221
222 std::string Val(StrStart, StrEnd);
223 AP->PrintSpecial(MI, OS, Val.c_str());
224 LastEmitted = StrEnd+1;
225 break;
226 }
227
Chad Rosier17788312012-09-11 19:09:56 +0000228 const char *IDStart = LastEmitted;
229 const char *IDEnd = IDStart;
230 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
231
232 unsigned Val;
233 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
234 report_fatal_error("Bad $ operand number in inline asm string: '" +
235 Twine(AsmStr) + "'");
236 LastEmitted = IDEnd;
237
238 if (Val >= NumOperands-1)
239 report_fatal_error("Invalid $ operand number in inline asm string: '" +
240 Twine(AsmStr) + "'");
241
242 // Okay, we finally have a value number. Ask the target to print this
243 // operand!
244 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
245
246 bool Error = false;
247
248 // Scan to find the machine operand number for the operand.
249 for (; Val; --Val) {
250 if (OpNo >= MI->getNumOperands()) break;
251 unsigned OpFlags = MI->getOperand(OpNo).getImm();
252 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
253 }
254
255 // We may have a location metadata attached to the end of the
256 // instruction, and at no point should see metadata at any
257 // other point while processing. It's an error if so.
258 if (OpNo >= MI->getNumOperands() ||
259 MI->getOperand(OpNo).isMetadata()) {
260 Error = true;
261 } else {
262 unsigned OpFlags = MI->getOperand(OpNo).getImm();
263 ++OpNo; // Skip over the ID number.
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000264
Chad Rosier17788312012-09-11 19:09:56 +0000265 if (InlineAsm::isMemKind(OpFlags)) {
266 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000267 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000268 } else {
269 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000270 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000271 }
272 }
273 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000274 std::string msg;
275 raw_string_ostream Msg(msg);
Chad Rosier17788312012-09-11 19:09:56 +0000276 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
277 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
278 }
279 break;
280 }
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000281 }
Chris Lattner51065562010-04-07 05:38:05 +0000282 }
Chad Rosier17788312012-09-11 19:09:56 +0000283 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
284}
Jim Grosbach00915352010-10-01 23:29:12 +0000285
Chad Rosier17788312012-09-11 19:09:56 +0000286static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
287 MachineModuleInfo *MMI, int InlineAsmVariant,
288 int AsmPrinterVariant, AsmPrinter *AP,
289 unsigned LocCookie, raw_ostream &OS) {
Chris Lattner1e158692010-04-04 18:34:07 +0000290 int CurVariant = -1; // The number of the {.|.|.} region we are in.
291 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chad Rosier17788312012-09-11 19:09:56 +0000292 unsigned NumOperands = MI->getNumOperands();
293
294 OS << '\t';
Jim Grosbach00915352010-10-01 23:29:12 +0000295
Chris Lattner1e158692010-04-04 18:34:07 +0000296 while (*LastEmitted) {
297 switch (*LastEmitted) {
298 default: {
299 // Not a special case, emit the string section literally.
300 const char *LiteralEnd = LastEmitted+1;
301 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
302 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
303 ++LiteralEnd;
304 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
305 OS.write(LastEmitted, LiteralEnd-LastEmitted);
306 LastEmitted = LiteralEnd;
307 break;
308 }
309 case '\n':
310 ++LastEmitted; // Consume newline character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000311 OS << '\n'; // Indent code with newline.
Chris Lattner1e158692010-04-04 18:34:07 +0000312 break;
313 case '$': {
314 ++LastEmitted; // Consume '$' character.
315 bool Done = true;
316
317 // Handle escapes.
318 switch (*LastEmitted) {
319 default: Done = false; break;
320 case '$': // $$ -> $
321 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
322 OS << '$';
323 ++LastEmitted; // Consume second '$' character.
324 break;
325 case '(': // $( -> same as GCC's { character.
326 ++LastEmitted; // Consume '(' character.
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000327 if (CurVariant != -1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000328 report_fatal_error("Nested variants found in inline asm string: '" +
329 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000330 CurVariant = 0; // We're in the first variant now.
331 break;
332 case '|':
333 ++LastEmitted; // consume '|' character.
334 if (CurVariant == -1)
335 OS << '|'; // this is gcc's behavior for | outside a variant
336 else
337 ++CurVariant; // We're in the next variant.
338 break;
339 case ')': // $) -> same as GCC's } char.
340 ++LastEmitted; // consume ')' character.
341 if (CurVariant == -1)
342 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach00915352010-10-01 23:29:12 +0000343 else
Chris Lattner1e158692010-04-04 18:34:07 +0000344 CurVariant = -1;
345 break;
346 }
347 if (Done) break;
Jim Grosbach00915352010-10-01 23:29:12 +0000348
Chris Lattner1e158692010-04-04 18:34:07 +0000349 bool HasCurlyBraces = false;
350 if (*LastEmitted == '{') { // ${variable}
351 ++LastEmitted; // Consume '{' character.
352 HasCurlyBraces = true;
353 }
Jim Grosbach00915352010-10-01 23:29:12 +0000354
Chris Lattner1e158692010-04-04 18:34:07 +0000355 // If we have ${:foo}, then this is not a real operand reference, it is a
356 // "magic" string reference, just like in .td files. Arrange to call
357 // PrintSpecial.
358 if (HasCurlyBraces && *LastEmitted == ':') {
359 ++LastEmitted;
360 const char *StrStart = LastEmitted;
361 const char *StrEnd = strchr(StrStart, '}');
Craig Topper353eda42014-04-24 06:44:33 +0000362 if (!StrEnd)
Benjamin Kramera6769262010-04-08 10:44:28 +0000363 report_fatal_error("Unterminated ${:foo} operand in inline asm"
364 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000365
Chris Lattner1e158692010-04-04 18:34:07 +0000366 std::string Val(StrStart, StrEnd);
Chad Rosier17788312012-09-11 19:09:56 +0000367 AP->PrintSpecial(MI, OS, Val.c_str());
Chris Lattner1e158692010-04-04 18:34:07 +0000368 LastEmitted = StrEnd+1;
369 break;
370 }
Jim Grosbach00915352010-10-01 23:29:12 +0000371
Chris Lattner1e158692010-04-04 18:34:07 +0000372 const char *IDStart = LastEmitted;
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000373 const char *IDEnd = IDStart;
Jim Grosbach00915352010-10-01 23:29:12 +0000374 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
375
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000376 unsigned Val;
377 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramera6769262010-04-08 10:44:28 +0000378 report_fatal_error("Bad $ operand number in inline asm string: '" +
379 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000380 LastEmitted = IDEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000381
Chris Lattner1e158692010-04-04 18:34:07 +0000382 char Modifier[2] = { 0, 0 };
Jim Grosbach00915352010-10-01 23:29:12 +0000383
Chris Lattner1e158692010-04-04 18:34:07 +0000384 if (HasCurlyBraces) {
385 // If we have curly braces, check for a modifier character. This
386 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
387 if (*LastEmitted == ':') {
388 ++LastEmitted; // Consume ':' character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000389 if (*LastEmitted == 0)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000390 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramera6769262010-04-08 10:44:28 +0000391 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000392
Chris Lattner1e158692010-04-04 18:34:07 +0000393 Modifier[0] = *LastEmitted;
394 ++LastEmitted; // Consume modifier character.
395 }
Jim Grosbach00915352010-10-01 23:29:12 +0000396
Chris Lattner0e45d242010-04-05 22:42:30 +0000397 if (*LastEmitted != '}')
Benjamin Kramera6769262010-04-08 10:44:28 +0000398 report_fatal_error("Bad ${} expression in inline asm string: '" +
399 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000400 ++LastEmitted; // Consume '}' character.
401 }
Jim Grosbach00915352010-10-01 23:29:12 +0000402
Chris Lattner0e45d242010-04-05 22:42:30 +0000403 if (Val >= NumOperands-1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000404 report_fatal_error("Invalid $ operand number in inline asm string: '" +
405 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000406
Chris Lattner1e158692010-04-04 18:34:07 +0000407 // Okay, we finally have a value number. Ask the target to print this
408 // operand!
409 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Cheng6eb516d2011-01-07 23:50:32 +0000410 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner1e158692010-04-04 18:34:07 +0000411
412 bool Error = false;
413
414 // Scan to find the machine operand number for the operand.
415 for (; Val; --Val) {
416 if (OpNo >= MI->getNumOperands()) break;
417 unsigned OpFlags = MI->getOperand(OpNo).getImm();
418 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
419 }
420
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000421 // We may have a location metadata attached to the end of the
422 // instruction, and at no point should see metadata at any
423 // other point while processing. It's an error if so.
Eric Christopher12da1692012-03-22 01:33:51 +0000424 if (OpNo >= MI->getNumOperands() ||
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000425 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner1e158692010-04-04 18:34:07 +0000426 Error = true;
427 } else {
428 unsigned OpFlags = MI->getOperand(OpNo).getImm();
429 ++OpNo; // Skip over the ID number.
430
Matt Arsenault8b643552015-06-09 00:31:39 +0000431 if (Modifier[0] == 'l') { // Labels are target independent.
Craig Topper784929d2019-02-08 20:48:56 +0000432 if (MI->getOperand(OpNo).isBlockAddress()) {
433 const BlockAddress *BA = MI->getOperand(OpNo).getBlockAddress();
434 MCSymbol *Sym = AP->GetBlockAddressSymbol(BA);
435 Sym->print(OS, AP->MAI);
436 } else if (MI->getOperand(OpNo).isMBB()) {
437 const MCSymbol *Sym = MI->getOperand(OpNo).getMBB()->getSymbol();
438 Sym->print(OS, AP->MAI);
439 } else {
440 Error = true;
441 }
Matt Arsenault8b643552015-06-09 00:31:39 +0000442 } else {
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000443 if (InlineAsm::isMemKind(OpFlags)) {
Chad Rosierdb20a412012-09-10 21:10:49 +0000444 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000445 Modifier[0] ? Modifier : nullptr,
Chris Lattner1e158692010-04-04 18:34:07 +0000446 OS);
447 } else {
Chad Rosierdb20a412012-09-10 21:10:49 +0000448 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000449 Modifier[0] ? Modifier : nullptr, OS);
Chris Lattner1e158692010-04-04 18:34:07 +0000450 }
451 }
452 }
453 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000454 std::string msg;
455 raw_string_ostream Msg(msg);
Chris Lattner1e457892010-04-07 23:40:44 +0000456 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
457 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner1e158692010-04-04 18:34:07 +0000458 }
459 }
460 break;
461 }
462 }
463 }
Chad Rosier17788312012-09-11 19:09:56 +0000464 OS << '\n' << (char)0; // null terminate string.
465}
466
467/// EmitInlineAsm - This method formats and emits the specified machine
468/// instruction that is an inline asm.
469void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
470 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
471
472 // Count the number of register definitions to find the asm string.
473 unsigned NumDefs = 0;
474 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
475 ++NumDefs)
476 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
477
478 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
479
480 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
481 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
482
483 // If this asmstr is empty, just print the #APP/#NOAPP markers.
484 // These are useful to see where empty asm's wound up.
485 if (AsmStr[0] == 0) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000486 OutStreamer->emitRawComment(MAI->getInlineAsmStart());
487 OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
Chad Rosier17788312012-09-11 19:09:56 +0000488 return;
Chad Rosier7641f582012-09-10 21:36:05 +0000489 }
490
Chad Rosier17788312012-09-11 19:09:56 +0000491 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000492 // enabled, so we use emitRawComment.
Lang Hames9ff69c82015-04-24 19:11:51 +0000493 OutStreamer->emitRawComment(MAI->getInlineAsmStart());
Chad Rosier17788312012-09-11 19:09:56 +0000494
495 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
496 // it.
497 unsigned LocCookie = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000498 const MDNode *LocMD = nullptr;
Chad Rosier17788312012-09-11 19:09:56 +0000499 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
500 if (MI->getOperand(i-1).isMetadata() &&
501 (LocMD = MI->getOperand(i-1).getMetadata()) &&
502 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000503 if (const ConstantInt *CI =
504 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Chad Rosier17788312012-09-11 19:09:56 +0000505 LocCookie = CI->getZExtValue();
506 break;
507 }
508 }
509 }
510
511 // Emit the inline asm to a temporary string so we can emit it through
512 // EmitInlineAsm.
Alp Tokere69170a2014-06-26 22:52:05 +0000513 SmallString<256> StringData;
514 raw_svector_ostream OS(StringData);
Chad Rosier17788312012-09-11 19:09:56 +0000515
516 // The variant of the current asmprinter.
517 int AsmPrinterVariant = MAI->getAssemblerDialect();
518 InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
519 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
520 if (InlineAsmVariant == InlineAsm::AD_ATT)
521 EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
522 AP, LocCookie, OS);
523 else
524 EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
525
Ties Stuij9c16d802018-08-30 12:52:35 +0000526 // Emit warnings if we use reserved registers on the clobber list, as
527 // that might give surprising results.
528 std::vector<std::string> RestrRegs;
529 // Start with the first operand descriptor, and iterate over them.
530 for (unsigned I = InlineAsm::MIOp_FirstOperand, NumOps = MI->getNumOperands();
531 I < NumOps; ++I) {
532 const MachineOperand &MO = MI->getOperand(I);
533 if (MO.isImm()) {
534 unsigned Flags = MO.getImm();
535 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
536 if (InlineAsm::getKind(Flags) == InlineAsm::Kind_Clobber &&
537 !TRI->isAsmClobberable(*MF, MI->getOperand(I + 1).getReg())) {
538 RestrRegs.push_back(TRI->getName(MI->getOperand(I + 1).getReg()));
539 }
540 // Skip to one before the next operand descriptor, if it exists.
541 I += InlineAsm::getNumOperandRegisters(Flags);
542 }
543 }
544
545 if (!RestrRegs.empty()) {
546 unsigned BufNum = addInlineAsmDiagBuffer(OS.str(), LocMD);
547 auto &SrcMgr = DiagInfo->SrcMgr;
548 SMLoc Loc = SMLoc::getFromPointer(
549 SrcMgr.getMemoryBuffer(BufNum)->getBuffer().begin());
550
551 std::string Msg = "inline asm clobber list contains reserved registers: ";
552 for (auto I = RestrRegs.begin(), E = RestrRegs.end(); I != E; I++) {
553 if(I != RestrRegs.begin())
554 Msg += ", ";
555 Msg += *I;
556 }
557 std::string Note = "Reserved registers on the clobber list may not be "
558 "preserved across the asm statement, and clobbering them may "
559 "lead to undefined behaviour.";
560 SrcMgr.PrintMessage(Loc, SourceMgr::DK_Warning, Msg);
561 SrcMgr.PrintMessage(Loc, SourceMgr::DK_Note, Note);
562 }
563
Evgeniy Stepanovaedec3f2019-03-11 21:50:10 +0000564 EmitInlineAsm(OS.str(), getSubtargetInfo(), TM.Options.MCOptions, LocMD,
Akira Hatanakaff867732015-05-15 00:20:44 +0000565 MI->getInlineAsmDialect());
Jim Grosbach00915352010-10-01 23:29:12 +0000566
Chris Lattner1e158692010-04-04 18:34:07 +0000567 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000568 // enabled, so we use emitRawComment.
Lang Hames9ff69c82015-04-24 19:11:51 +0000569 OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
Chris Lattner1e158692010-04-04 18:34:07 +0000570}
571
572
573/// PrintSpecial - Print information related to the specified machine instr
574/// that is independent of the operand, and may be independent of the instr
575/// itself. This can be useful for portably encoding the comment character
576/// or other bits of target-specific knowledge into the asmstrings. The
577/// syntax used is ${:comment}. Targets can override this to add support
578/// for their own strange codes.
579void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
580 const char *Code) const {
581 if (!strcmp(Code, "private")) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000582 const DataLayout &DL = MF->getDataLayout();
583 OS << DL.getPrivateGlobalPrefix();
Chris Lattner1e158692010-04-04 18:34:07 +0000584 } else if (!strcmp(Code, "comment")) {
585 OS << MAI->getCommentString();
586 } else if (!strcmp(Code, "uid")) {
587 // Comparing the address of MI isn't sufficient, because machineinstrs may
588 // be allocated to the same address across functions.
Jim Grosbach00915352010-10-01 23:29:12 +0000589
Chris Lattner1e158692010-04-04 18:34:07 +0000590 // If this is a new LastFn instruction, bump the counter.
591 if (LastMI != MI || LastFn != getFunctionNumber()) {
592 ++Counter;
593 LastMI = MI;
594 LastFn = getFunctionNumber();
595 }
596 OS << Counter;
597 } else {
Alp Tokere69170a2014-06-26 22:52:05 +0000598 std::string msg;
599 raw_string_ostream Msg(msg);
Chris Lattner1e158692010-04-04 18:34:07 +0000600 Msg << "Unknown special formatter '" << Code
601 << "' for machine instr: " << *MI;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000602 report_fatal_error(Msg.str());
Jim Grosbach00915352010-10-01 23:29:12 +0000603 }
Chris Lattner1e158692010-04-04 18:34:07 +0000604}
605
606/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
607/// instruction, using the specified assembler variant. Targets should
608/// override this to format as appropriate.
609bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chad Rosier1f57bcb2012-09-07 20:23:29 +0000610 unsigned AsmVariant, const char *ExtraCode,
611 raw_ostream &O) {
Jack Carterb2fd5f62012-06-21 17:14:46 +0000612 // Does this asm operand have a single letter operand modifier?
613 if (ExtraCode && ExtraCode[0]) {
614 if (ExtraCode[1] != 0) return true; // Unknown modifier.
615
616 const MachineOperand &MO = MI->getOperand(OpNo);
617 switch (ExtraCode[0]) {
618 default:
619 return true; // Unknown modifier.
620 case 'c': // Substitute immediate value without immediate syntax
Jack Carterc457f622012-06-21 21:37:54 +0000621 if (MO.getType() != MachineOperand::MO_Immediate)
Jack Carterb2fd5f62012-06-21 17:14:46 +0000622 return true;
623 O << MO.getImm();
624 return false;
Jack Carterc457f622012-06-21 21:37:54 +0000625 case 'n': // Negate the immediate constant.
626 if (MO.getType() != MachineOperand::MO_Immediate)
627 return true;
628 O << -MO.getImm();
629 return false;
Nemanja Ivanovice8cbae32016-02-04 16:18:08 +0000630 case 's': // The GCC deprecated s modifier
631 if (MO.getType() != MachineOperand::MO_Immediate)
632 return true;
633 O << ((32 - MO.getImm()) & 31);
634 return false;
Jack Carterb2fd5f62012-06-21 17:14:46 +0000635 }
636 }
Chris Lattner1e158692010-04-04 18:34:07 +0000637 return true;
638}
639
640bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
641 unsigned AsmVariant,
642 const char *ExtraCode, raw_ostream &O) {
643 // Target doesn't support this yet!
644 return true;
645}
646
Eric Christopher64d35be2015-02-19 19:52:25 +0000647void AsmPrinter::emitInlineAsmStart() const {}
Toma Tabacua23f13c2014-12-17 10:56:16 +0000648
Rafael Espindola65fd0a82014-01-24 15:47:54 +0000649void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
David Peixottoea2bcb92014-02-06 18:19:40 +0000650 const MCSubtargetInfo *EndInfo) const {}