blob: b476ac057e81b611b640a94ee23e826e0b5c1465 [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"
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"
Yuri Gorshenin3939dec2014-09-10 09:45:49 +000037#include "llvm/Target/TargetRegisterInfo.h"
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +000038#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner1e158692010-04-04 18:34:07 +000039using namespace llvm;
40
Chandler Carruth1b9dde02014-04-22 02:02:50 +000041#define DEBUG_TYPE "asm-printer"
42
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
Sanne Wouda29338752017-02-08 14:48:05 +0000101 if (!DiagInfo) {
102 DiagInfo = make_unique<SrcMgrDiagInfo>();
Saleem Abdulrasool6252bd82017-01-05 05:56:39 +0000103
Sanne Wouda29338752017-02-08 14:48:05 +0000104 MCContext &Context = MMI->getContext();
105 Context.setInlineSourceManager(&DiagInfo->SrcMgr);
Jim Grosbach00915352010-10-01 23:29:12 +0000106
Sanne Wouda29338752017-02-08 14:48:05 +0000107 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 }
Chris Lattner59126b22010-04-06 00:55:39 +0000113 }
Jim Grosbach00915352010-10-01 23:29:12 +0000114
Sanne Wouda29338752017-02-08 14:48:05 +0000115 SourceMgr &SrcMgr = DiagInfo->SrcMgr;
116 SrcMgr.setIncludeDirs(MCOptions.IASSearchPaths);
Sanne Wouda29338752017-02-08 14:48:05 +0000117
Rafael Espindola3560ff22014-08-27 20:03:13 +0000118 std::unique_ptr<MemoryBuffer> Buffer;
Sanne Wouda29338752017-02-08 14:48:05 +0000119 // 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>");
Chris Lattner8900ef12010-04-05 23:11:24 +0000122
123 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
Sanne Wouda29338752017-02-08 14:48:05 +0000124 unsigned BufNum = SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
Jim Grosbach00915352010-10-01 23:29:12 +0000125
Sanne Wouda91eadad2017-02-13 13:58:00 +0000126 // 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
Ahmed Charles56440fd2014-03-06 05:51:42 +0000132 std::unique_ptr<MCAsmParser> Parser(
Sanne Wouda29338752017-02-08 14:48:05 +0000133 createMCAsmParser(SrcMgr, OutContext, *OutStreamer, *MAI, BufNum));
Evan Cheng4d1ca962011-07-08 01:53:10 +0000134
Eric Christopher3f05e1a2015-02-21 09:09:15 +0000135 // We create a new MCInstrInfo here since we might be at the module level
Eric Christopher2105ae92015-02-19 23:52:35 +0000136 // and not have a MachineFunction to initialize the TargetInstrInfo from and
Eric Christopher3f05e1a2015-02-21 09:09:15 +0000137 // we only need MCInstrInfo for asm parsing. We create one unconditionally
138 // because it's not subtarget dependent.
139 std::unique_ptr<MCInstrInfo> MII(TM.getTarget().createMCInstrInfo());
Eric Christophercbdbf392015-02-19 21:29:51 +0000140 std::unique_ptr<MCTargetAsmParser> TAP(TM.getTarget().createMCAsmParser(
Akira Hatanakab11ef082015-11-14 06:35:56 +0000141 STI, *Parser, *MII, MCOptions));
Chris Lattner8900ef12010-04-05 23:11:24 +0000142 if (!TAP)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000143 report_fatal_error("Inline asm not supported by this streamer because"
Benjamin Kramera6769262010-04-08 10:44:28 +0000144 " we don't have an asm parser for this target\n");
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000145 Parser->setAssemblerDialect(Dialect);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000146 Parser->setTargetParser(*TAP.get());
Andrew V. Tischenko3543f0a2017-11-09 12:45:40 +0000147 Parser->setEnablePrintSchedInfo(EnablePrintSchedInfo);
Andrew V. Tischenkoc3c67232017-04-26 09:56:59 +0000148 if (Dialect == InlineAsm::AD_Intel)
149 // We need this flag to be able to parse numbers like "0bH"
150 Parser->setParsingInlineAsm(true);
Yuri Gorshenin3939dec2014-09-10 09:45:49 +0000151 if (MF) {
152 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
153 TAP->SetFrameRegister(TRI->getFrameRegister(*MF));
154 }
Chris Lattner8900ef12010-04-05 23:11:24 +0000155
Eric Christopher64d35be2015-02-19 19:52:25 +0000156 emitInlineAsmStart();
Chris Lattner8900ef12010-04-05 23:11:24 +0000157 // Don't implicitly switch to the text section before the asm.
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000158 int Res = Parser->Run(/*NoInitialTextSection*/ true,
159 /*NoFinalize*/ true);
Akira Hatanakab11ef082015-11-14 06:35:56 +0000160 emitInlineAsmEnd(STI, &TAP->getSTI());
Sanne Wouda29338752017-02-08 14:48:05 +0000161
Sanne Wouda29338752017-02-08 14:48:05 +0000162 if (Res && !DiagInfo->DiagHandler)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000163 report_fatal_error("Error parsing inline asm\n");
Chris Lattner1e158692010-04-04 18:34:07 +0000164}
165
Chad Rosier17788312012-09-11 19:09:56 +0000166static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
167 MachineModuleInfo *MMI, int InlineAsmVariant,
168 AsmPrinter *AP, unsigned LocCookie,
169 raw_ostream &OS) {
170 // Switch to the inline assembly variant.
171 OS << "\t.intel_syntax\n\t";
Chris Lattner1e158692010-04-04 18:34:07 +0000172
Chad Rosier17788312012-09-11 19:09:56 +0000173 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner1e158692010-04-04 18:34:07 +0000174 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach00915352010-10-01 23:29:12 +0000175
Chad Rosier17788312012-09-11 19:09:56 +0000176 while (*LastEmitted) {
177 switch (*LastEmitted) {
178 default: {
179 // Not a special case, emit the string section literally.
180 const char *LiteralEnd = LastEmitted+1;
181 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
182 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
183 ++LiteralEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000184
Chad Rosier17788312012-09-11 19:09:56 +0000185 OS.write(LastEmitted, LiteralEnd-LastEmitted);
186 LastEmitted = LiteralEnd;
187 break;
188 }
189 case '\n':
190 ++LastEmitted; // Consume newline character.
191 OS << '\n'; // Indent code with newline.
192 break;
193 case '$': {
194 ++LastEmitted; // Consume '$' character.
195 bool Done = true;
Chris Lattner1e158692010-04-04 18:34:07 +0000196
Chad Rosier17788312012-09-11 19:09:56 +0000197 // Handle escapes.
198 switch (*LastEmitted) {
199 default: Done = false; break;
200 case '$':
201 ++LastEmitted; // Consume second '$' character.
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000202 break;
203 }
Chad Rosier17788312012-09-11 19:09:56 +0000204 if (Done) break;
205
Reid Klecknerc68a6c42016-11-29 00:29:27 +0000206 // If we have ${:foo}, then this is not a real operand reference, it is a
207 // "magic" string reference, just like in .td files. Arrange to call
208 // PrintSpecial.
209 if (LastEmitted[0] == '{' && LastEmitted[1] == ':') {
210 LastEmitted += 2;
211 const char *StrStart = LastEmitted;
212 const char *StrEnd = strchr(StrStart, '}');
213 if (!StrEnd)
214 report_fatal_error("Unterminated ${:foo} operand in inline asm"
215 " string: '" + Twine(AsmStr) + "'");
216
217 std::string Val(StrStart, StrEnd);
218 AP->PrintSpecial(MI, OS, Val.c_str());
219 LastEmitted = StrEnd+1;
220 break;
221 }
222
Chad Rosier17788312012-09-11 19:09:56 +0000223 const char *IDStart = LastEmitted;
224 const char *IDEnd = IDStart;
225 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
226
227 unsigned Val;
228 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
229 report_fatal_error("Bad $ operand number in inline asm string: '" +
230 Twine(AsmStr) + "'");
231 LastEmitted = IDEnd;
232
233 if (Val >= NumOperands-1)
234 report_fatal_error("Invalid $ operand number in inline asm string: '" +
235 Twine(AsmStr) + "'");
236
237 // Okay, we finally have a value number. Ask the target to print this
238 // operand!
239 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
240
241 bool Error = false;
242
243 // Scan to find the machine operand number for the operand.
244 for (; Val; --Val) {
245 if (OpNo >= MI->getNumOperands()) break;
246 unsigned OpFlags = MI->getOperand(OpNo).getImm();
247 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
248 }
249
250 // We may have a location metadata attached to the end of the
251 // instruction, and at no point should see metadata at any
252 // other point while processing. It's an error if so.
253 if (OpNo >= MI->getNumOperands() ||
254 MI->getOperand(OpNo).isMetadata()) {
255 Error = true;
256 } else {
257 unsigned OpFlags = MI->getOperand(OpNo).getImm();
258 ++OpNo; // Skip over the ID number.
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000259
Chad Rosier17788312012-09-11 19:09:56 +0000260 if (InlineAsm::isMemKind(OpFlags)) {
261 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000262 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000263 } else {
264 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000265 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000266 }
267 }
268 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000269 std::string msg;
270 raw_string_ostream Msg(msg);
Chad Rosier17788312012-09-11 19:09:56 +0000271 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
272 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
273 }
274 break;
275 }
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000276 }
Chris Lattner51065562010-04-07 05:38:05 +0000277 }
Chad Rosier17788312012-09-11 19:09:56 +0000278 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
279}
Jim Grosbach00915352010-10-01 23:29:12 +0000280
Chad Rosier17788312012-09-11 19:09:56 +0000281static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
282 MachineModuleInfo *MMI, int InlineAsmVariant,
283 int AsmPrinterVariant, AsmPrinter *AP,
284 unsigned LocCookie, raw_ostream &OS) {
Chris Lattner1e158692010-04-04 18:34:07 +0000285 int CurVariant = -1; // The number of the {.|.|.} region we are in.
286 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chad Rosier17788312012-09-11 19:09:56 +0000287 unsigned NumOperands = MI->getNumOperands();
288
289 OS << '\t';
Jim Grosbach00915352010-10-01 23:29:12 +0000290
Chris Lattner1e158692010-04-04 18:34:07 +0000291 while (*LastEmitted) {
292 switch (*LastEmitted) {
293 default: {
294 // Not a special case, emit the string section literally.
295 const char *LiteralEnd = LastEmitted+1;
296 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
297 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
298 ++LiteralEnd;
299 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
300 OS.write(LastEmitted, LiteralEnd-LastEmitted);
301 LastEmitted = LiteralEnd;
302 break;
303 }
304 case '\n':
305 ++LastEmitted; // Consume newline character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000306 OS << '\n'; // Indent code with newline.
Chris Lattner1e158692010-04-04 18:34:07 +0000307 break;
308 case '$': {
309 ++LastEmitted; // Consume '$' character.
310 bool Done = true;
311
312 // Handle escapes.
313 switch (*LastEmitted) {
314 default: Done = false; break;
315 case '$': // $$ -> $
316 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
317 OS << '$';
318 ++LastEmitted; // Consume second '$' character.
319 break;
320 case '(': // $( -> same as GCC's { character.
321 ++LastEmitted; // Consume '(' character.
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000322 if (CurVariant != -1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000323 report_fatal_error("Nested variants found in inline asm string: '" +
324 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000325 CurVariant = 0; // We're in the first variant now.
326 break;
327 case '|':
328 ++LastEmitted; // consume '|' character.
329 if (CurVariant == -1)
330 OS << '|'; // this is gcc's behavior for | outside a variant
331 else
332 ++CurVariant; // We're in the next variant.
333 break;
334 case ')': // $) -> same as GCC's } char.
335 ++LastEmitted; // consume ')' character.
336 if (CurVariant == -1)
337 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach00915352010-10-01 23:29:12 +0000338 else
Chris Lattner1e158692010-04-04 18:34:07 +0000339 CurVariant = -1;
340 break;
341 }
342 if (Done) break;
Jim Grosbach00915352010-10-01 23:29:12 +0000343
Chris Lattner1e158692010-04-04 18:34:07 +0000344 bool HasCurlyBraces = false;
345 if (*LastEmitted == '{') { // ${variable}
346 ++LastEmitted; // Consume '{' character.
347 HasCurlyBraces = true;
348 }
Jim Grosbach00915352010-10-01 23:29:12 +0000349
Chris Lattner1e158692010-04-04 18:34:07 +0000350 // If we have ${:foo}, then this is not a real operand reference, it is a
351 // "magic" string reference, just like in .td files. Arrange to call
352 // PrintSpecial.
353 if (HasCurlyBraces && *LastEmitted == ':') {
354 ++LastEmitted;
355 const char *StrStart = LastEmitted;
356 const char *StrEnd = strchr(StrStart, '}');
Craig Topper353eda42014-04-24 06:44:33 +0000357 if (!StrEnd)
Benjamin Kramera6769262010-04-08 10:44:28 +0000358 report_fatal_error("Unterminated ${:foo} operand in inline asm"
359 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000360
Chris Lattner1e158692010-04-04 18:34:07 +0000361 std::string Val(StrStart, StrEnd);
Chad Rosier17788312012-09-11 19:09:56 +0000362 AP->PrintSpecial(MI, OS, Val.c_str());
Chris Lattner1e158692010-04-04 18:34:07 +0000363 LastEmitted = StrEnd+1;
364 break;
365 }
Jim Grosbach00915352010-10-01 23:29:12 +0000366
Chris Lattner1e158692010-04-04 18:34:07 +0000367 const char *IDStart = LastEmitted;
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000368 const char *IDEnd = IDStart;
Jim Grosbach00915352010-10-01 23:29:12 +0000369 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
370
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000371 unsigned Val;
372 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramera6769262010-04-08 10:44:28 +0000373 report_fatal_error("Bad $ operand number in inline asm string: '" +
374 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000375 LastEmitted = IDEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000376
Chris Lattner1e158692010-04-04 18:34:07 +0000377 char Modifier[2] = { 0, 0 };
Jim Grosbach00915352010-10-01 23:29:12 +0000378
Chris Lattner1e158692010-04-04 18:34:07 +0000379 if (HasCurlyBraces) {
380 // If we have curly braces, check for a modifier character. This
381 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
382 if (*LastEmitted == ':') {
383 ++LastEmitted; // Consume ':' character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000384 if (*LastEmitted == 0)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000385 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramera6769262010-04-08 10:44:28 +0000386 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000387
Chris Lattner1e158692010-04-04 18:34:07 +0000388 Modifier[0] = *LastEmitted;
389 ++LastEmitted; // Consume modifier character.
390 }
Jim Grosbach00915352010-10-01 23:29:12 +0000391
Chris Lattner0e45d242010-04-05 22:42:30 +0000392 if (*LastEmitted != '}')
Benjamin Kramera6769262010-04-08 10:44:28 +0000393 report_fatal_error("Bad ${} expression in inline asm string: '" +
394 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000395 ++LastEmitted; // Consume '}' character.
396 }
Jim Grosbach00915352010-10-01 23:29:12 +0000397
Chris Lattner0e45d242010-04-05 22:42:30 +0000398 if (Val >= NumOperands-1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000399 report_fatal_error("Invalid $ operand number in inline asm string: '" +
400 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000401
Chris Lattner1e158692010-04-04 18:34:07 +0000402 // Okay, we finally have a value number. Ask the target to print this
403 // operand!
404 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Cheng6eb516d2011-01-07 23:50:32 +0000405 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner1e158692010-04-04 18:34:07 +0000406
407 bool Error = false;
408
409 // Scan to find the machine operand number for the operand.
410 for (; Val; --Val) {
411 if (OpNo >= MI->getNumOperands()) break;
412 unsigned OpFlags = MI->getOperand(OpNo).getImm();
413 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
414 }
415
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000416 // We may have a location metadata attached to the end of the
417 // instruction, and at no point should see metadata at any
418 // other point while processing. It's an error if so.
Eric Christopher12da1692012-03-22 01:33:51 +0000419 if (OpNo >= MI->getNumOperands() ||
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000420 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner1e158692010-04-04 18:34:07 +0000421 Error = true;
422 } else {
423 unsigned OpFlags = MI->getOperand(OpNo).getImm();
424 ++OpNo; // Skip over the ID number.
425
Matt Arsenault8b643552015-06-09 00:31:39 +0000426 if (Modifier[0] == 'l') { // Labels are target independent.
Chris Lattner1e158692010-04-04 18:34:07 +0000427 // FIXME: What if the operand isn't an MBB, report error?
Matt Arsenault8b643552015-06-09 00:31:39 +0000428 const MCSymbol *Sym = MI->getOperand(OpNo).getMBB()->getSymbol();
429 Sym->print(OS, AP->MAI);
430 } else {
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000431 if (InlineAsm::isMemKind(OpFlags)) {
Chad Rosierdb20a412012-09-10 21:10:49 +0000432 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000433 Modifier[0] ? Modifier : nullptr,
Chris Lattner1e158692010-04-04 18:34:07 +0000434 OS);
435 } else {
Chad Rosierdb20a412012-09-10 21:10:49 +0000436 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000437 Modifier[0] ? Modifier : nullptr, OS);
Chris Lattner1e158692010-04-04 18:34:07 +0000438 }
439 }
440 }
441 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000442 std::string msg;
443 raw_string_ostream Msg(msg);
Chris Lattner1e457892010-04-07 23:40:44 +0000444 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
445 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner1e158692010-04-04 18:34:07 +0000446 }
447 }
448 break;
449 }
450 }
451 }
Chad Rosier17788312012-09-11 19:09:56 +0000452 OS << '\n' << (char)0; // null terminate string.
453}
454
455/// EmitInlineAsm - This method formats and emits the specified machine
456/// instruction that is an inline asm.
457void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
458 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
459
460 // Count the number of register definitions to find the asm string.
461 unsigned NumDefs = 0;
462 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
463 ++NumDefs)
464 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
465
466 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
467
468 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
469 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
470
471 // If this asmstr is empty, just print the #APP/#NOAPP markers.
472 // These are useful to see where empty asm's wound up.
473 if (AsmStr[0] == 0) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000474 OutStreamer->emitRawComment(MAI->getInlineAsmStart());
475 OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
Chad Rosier17788312012-09-11 19:09:56 +0000476 return;
Chad Rosier7641f582012-09-10 21:36:05 +0000477 }
478
Chad Rosier17788312012-09-11 19:09:56 +0000479 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000480 // enabled, so we use emitRawComment.
Lang Hames9ff69c82015-04-24 19:11:51 +0000481 OutStreamer->emitRawComment(MAI->getInlineAsmStart());
Chad Rosier17788312012-09-11 19:09:56 +0000482
483 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
484 // it.
485 unsigned LocCookie = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000486 const MDNode *LocMD = nullptr;
Chad Rosier17788312012-09-11 19:09:56 +0000487 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
488 if (MI->getOperand(i-1).isMetadata() &&
489 (LocMD = MI->getOperand(i-1).getMetadata()) &&
490 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000491 if (const ConstantInt *CI =
492 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Chad Rosier17788312012-09-11 19:09:56 +0000493 LocCookie = CI->getZExtValue();
494 break;
495 }
496 }
497 }
498
499 // Emit the inline asm to a temporary string so we can emit it through
500 // EmitInlineAsm.
Alp Tokere69170a2014-06-26 22:52:05 +0000501 SmallString<256> StringData;
502 raw_svector_ostream OS(StringData);
Chad Rosier17788312012-09-11 19:09:56 +0000503
504 // The variant of the current asmprinter.
505 int AsmPrinterVariant = MAI->getAssemblerDialect();
506 InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
507 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
508 if (InlineAsmVariant == InlineAsm::AD_ATT)
509 EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
510 AP, LocCookie, OS);
511 else
512 EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
513
Akira Hatanakaff867732015-05-15 00:20:44 +0000514 // Reset SanitizeAddress based on the function's attribute.
515 MCTargetOptions MCOptions = TM.Options.MCOptions;
516 MCOptions.SanitizeAddress =
517 MF->getFunction()->hasFnAttribute(Attribute::SanitizeAddress);
518
519 EmitInlineAsm(OS.str(), getSubtargetInfo(), MCOptions, LocMD,
520 MI->getInlineAsmDialect());
Jim Grosbach00915352010-10-01 23:29:12 +0000521
Chris Lattner1e158692010-04-04 18:34:07 +0000522 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000523 // enabled, so we use emitRawComment.
Lang Hames9ff69c82015-04-24 19:11:51 +0000524 OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
Chris Lattner1e158692010-04-04 18:34:07 +0000525}
526
527
528/// PrintSpecial - Print information related to the specified machine instr
529/// that is independent of the operand, and may be independent of the instr
530/// itself. This can be useful for portably encoding the comment character
531/// or other bits of target-specific knowledge into the asmstrings. The
532/// syntax used is ${:comment}. Targets can override this to add support
533/// for their own strange codes.
534void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
535 const char *Code) const {
536 if (!strcmp(Code, "private")) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000537 const DataLayout &DL = MF->getDataLayout();
538 OS << DL.getPrivateGlobalPrefix();
Chris Lattner1e158692010-04-04 18:34:07 +0000539 } else if (!strcmp(Code, "comment")) {
540 OS << MAI->getCommentString();
541 } else if (!strcmp(Code, "uid")) {
542 // Comparing the address of MI isn't sufficient, because machineinstrs may
543 // be allocated to the same address across functions.
Jim Grosbach00915352010-10-01 23:29:12 +0000544
Chris Lattner1e158692010-04-04 18:34:07 +0000545 // If this is a new LastFn instruction, bump the counter.
546 if (LastMI != MI || LastFn != getFunctionNumber()) {
547 ++Counter;
548 LastMI = MI;
549 LastFn = getFunctionNumber();
550 }
551 OS << Counter;
552 } else {
Alp Tokere69170a2014-06-26 22:52:05 +0000553 std::string msg;
554 raw_string_ostream Msg(msg);
Chris Lattner1e158692010-04-04 18:34:07 +0000555 Msg << "Unknown special formatter '" << Code
556 << "' for machine instr: " << *MI;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000557 report_fatal_error(Msg.str());
Jim Grosbach00915352010-10-01 23:29:12 +0000558 }
Chris Lattner1e158692010-04-04 18:34:07 +0000559}
560
561/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
562/// instruction, using the specified assembler variant. Targets should
563/// override this to format as appropriate.
564bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chad Rosier1f57bcb2012-09-07 20:23:29 +0000565 unsigned AsmVariant, const char *ExtraCode,
566 raw_ostream &O) {
Jack Carterb2fd5f62012-06-21 17:14:46 +0000567 // Does this asm operand have a single letter operand modifier?
568 if (ExtraCode && ExtraCode[0]) {
569 if (ExtraCode[1] != 0) return true; // Unknown modifier.
570
571 const MachineOperand &MO = MI->getOperand(OpNo);
572 switch (ExtraCode[0]) {
573 default:
574 return true; // Unknown modifier.
575 case 'c': // Substitute immediate value without immediate syntax
Jack Carterc457f622012-06-21 21:37:54 +0000576 if (MO.getType() != MachineOperand::MO_Immediate)
Jack Carterb2fd5f62012-06-21 17:14:46 +0000577 return true;
578 O << MO.getImm();
579 return false;
Jack Carterc457f622012-06-21 21:37:54 +0000580 case 'n': // Negate the immediate constant.
581 if (MO.getType() != MachineOperand::MO_Immediate)
582 return true;
583 O << -MO.getImm();
584 return false;
Nemanja Ivanovice8cbae32016-02-04 16:18:08 +0000585 case 's': // The GCC deprecated s modifier
586 if (MO.getType() != MachineOperand::MO_Immediate)
587 return true;
588 O << ((32 - MO.getImm()) & 31);
589 return false;
Jack Carterb2fd5f62012-06-21 17:14:46 +0000590 }
591 }
Chris Lattner1e158692010-04-04 18:34:07 +0000592 return true;
593}
594
595bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
596 unsigned AsmVariant,
597 const char *ExtraCode, raw_ostream &O) {
598 // Target doesn't support this yet!
599 return true;
600}
601
Eric Christopher64d35be2015-02-19 19:52:25 +0000602void AsmPrinter::emitInlineAsmStart() const {}
Toma Tabacua23f13c2014-12-17 10:56:16 +0000603
Rafael Espindola65fd0a82014-01-24 15:47:54 +0000604void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
David Peixottoea2bcb92014-02-06 18:19:40 +0000605 const MCSubtargetInfo *EndInfo) const {}