blob: 04ab2d16b70d1e597d307fe18e3d106afc6b0555 [file] [log] [blame]
Chris Lattner9efd1182010-04-04 19:09:29 +00001//===-- AsmPrinterInlineAsm.cpp - AsmPrinter Inline Asm Handling ----------===//
Chris Lattner1e158692010-04-04 18:34:07 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner9efd1182010-04-04 19:09:29 +000010// This file implements the inline assembler pieces of the AsmPrinter class.
Chris Lattner1e158692010-04-04 18:34:07 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner1e158692010-04-04 18:34:07 +000014#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/Twine.h"
17#include "llvm/CodeGen/MachineBasicBlock.h"
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +000018#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Constants.h"
Rafael Espindola58873562014-01-03 19:21:54 +000021#include "llvm/IR/DataLayout.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/InlineAsm.h"
23#include "llvm/IR/LLVMContext.h"
24#include "llvm/IR/Module.h"
Chris Lattner1e158692010-04-04 18:34:07 +000025#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCStreamer.h"
Evan Cheng91111d22011-07-09 05:47:46 +000027#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner1e158692010-04-04 18:34:07 +000028#include "llvm/MC/MCSymbol.h"
Evan Cheng11424442011-07-26 00:24:13 +000029#include "llvm/MC/MCTargetAsmParser.h"
Chris Lattner1e158692010-04-04 18:34:07 +000030#include "llvm/Support/ErrorHandling.h"
Chris Lattner8900ef12010-04-05 23:11:24 +000031#include "llvm/Support/MemoryBuffer.h"
32#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000033#include "llvm/Support/TargetRegistry.h"
Chris Lattner1e158692010-04-04 18:34:07 +000034#include "llvm/Support/raw_ostream.h"
Eric Christopher97ea7622015-02-20 06:35:21 +000035#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Target/TargetMachine.h"
Yuri Gorshenin3939dec2014-09-10 09:45:49 +000037#include "llvm/Target/TargetRegisterInfo.h"
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +000038#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner1e158692010-04-04 18:34:07 +000039using namespace llvm;
40
Chandler Carruth1b9dde02014-04-22 02:02:50 +000041#define DEBUG_TYPE "asm-printer"
42
Chris Lattner300fa452010-11-17 08:03:32 +000043namespace {
44 struct SrcMgrDiagInfo {
45 const MDNode *LocInfo;
Bob Wilsona594fab2013-02-11 05:37:07 +000046 LLVMContext::InlineAsmDiagHandlerTy DiagHandler;
Chris Lattner300fa452010-11-17 08:03:32 +000047 void *DiagContext;
48 };
49}
50
Chad Rosierb759ede2012-09-07 18:16:38 +000051/// srcMgrDiagHandler - This callback is invoked when the SourceMgr for an
Chris Lattner300fa452010-11-17 08:03:32 +000052/// inline asm has an error in it. diagInfo is a pointer to the SrcMgrDiagInfo
53/// struct above.
Chad Rosierb759ede2012-09-07 18:16:38 +000054static void srcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
Chris Lattner300fa452010-11-17 08:03:32 +000055 SrcMgrDiagInfo *DiagInfo = static_cast<SrcMgrDiagInfo *>(diagInfo);
56 assert(DiagInfo && "Diagnostic context not passed down?");
Jim Grosbach098f5a22011-09-21 21:36:53 +000057
Chris Lattner79ffdc72010-11-17 08:20:42 +000058 // If the inline asm had metadata associated with it, pull out a location
59 // cookie corresponding to which line the error occurred on.
Chris Lattner300fa452010-11-17 08:03:32 +000060 unsigned LocCookie = 0;
Chris Lattner79ffdc72010-11-17 08:20:42 +000061 if (const MDNode *LocInfo = DiagInfo->LocInfo) {
62 unsigned ErrorLine = Diag.getLineNo()-1;
63 if (ErrorLine >= LocInfo->getNumOperands())
64 ErrorLine = 0;
Jim Grosbach098f5a22011-09-21 21:36:53 +000065
Chris Lattner79ffdc72010-11-17 08:20:42 +000066 if (LocInfo->getNumOperands() != 0)
67 if (const ConstantInt *CI =
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000068 mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
Chris Lattner300fa452010-11-17 08:03:32 +000069 LocCookie = CI->getZExtValue();
Chris Lattner79ffdc72010-11-17 08:20:42 +000070 }
Jim Grosbach098f5a22011-09-21 21:36:53 +000071
Chris Lattnerb0e36082010-11-17 08:13:01 +000072 DiagInfo->DiagHandler(Diag, DiagInfo->DiagContext, LocCookie);
Chris Lattner300fa452010-11-17 08:03:32 +000073}
74
Chris Lattner1e158692010-04-04 18:34:07 +000075/// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
Akira Hatanaka322ffce2015-03-16 18:02:16 +000076void AsmPrinter::EmitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
77 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
Chris Lattner8900ef12010-04-05 23:11:24 +0000101 SourceMgr SrcMgr;
Chris Lattner300fa452010-11-17 08:03:32 +0000102 SrcMgrDiagInfo DiagInfo;
Jim Grosbach00915352010-10-01 23:29:12 +0000103
Bob Wilsona594fab2013-02-11 05:37:07 +0000104 // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
Chris Lattner59126b22010-04-06 00:55:39 +0000105 LLVMContext &LLVMCtx = MMI->getModule()->getContext();
106 bool HasDiagHandler = false;
Craig Topper353eda42014-04-24 06:44:33 +0000107 if (LLVMCtx.getInlineAsmDiagnosticHandler() != nullptr) {
Chad Rosierb759ede2012-09-07 18:16:38 +0000108 // If the source manager has an issue, we arrange for srcMgrDiagHandler
Chris Lattner300fa452010-11-17 08:03:32 +0000109 // to be invoked, getting DiagInfo passed into it.
110 DiagInfo.LocInfo = LocMDNode;
Bob Wilsona594fab2013-02-11 05:37:07 +0000111 DiagInfo.DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
112 DiagInfo.DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
Chad Rosierb759ede2012-09-07 18:16:38 +0000113 SrcMgr.setDiagHandler(srcMgrDiagHandler, &DiagInfo);
Chris Lattner59126b22010-04-06 00:55:39 +0000114 HasDiagHandler = true;
115 }
Jim Grosbach00915352010-10-01 23:29:12 +0000116
Rafael Espindola3560ff22014-08-27 20:03:13 +0000117 std::unique_ptr<MemoryBuffer> Buffer;
118 if (isNullTerminated)
119 Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
120 else
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.
David Blaikie1961f142014-08-21 20:44:56 +0000124 SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
Jim Grosbach00915352010-10-01 23:29:12 +0000125
Ahmed Charles56440fd2014-03-06 05:51:42 +0000126 std::unique_ptr<MCAsmParser> Parser(
Lang Hames9ff69c82015-04-24 19:11:51 +0000127 createMCAsmParser(SrcMgr, OutContext, *OutStreamer, *MAI));
Evan Cheng4d1ca962011-07-08 01:53:10 +0000128
Akira Hatanaka322ffce2015-03-16 18:02:16 +0000129 // Create a temporary copy of the original STI because the parser may modify
130 // it. For example, when switching between arm and thumb mode. If the target
131 // needs to emit code to return to the original state it can do so in
Eric Christopherf9761a22014-02-26 02:53:18 +0000132 // emitInlineAsmEnd().
Akira Hatanaka322ffce2015-03-16 18:02:16 +0000133 MCSubtargetInfo TmpSTI = STI;
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +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 Hatanaka322ffce2015-03-16 18:02:16 +0000141 TmpSTI, *Parser, *MII, TM.Options.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());
Yuri Gorshenin3939dec2014-09-10 09:45:49 +0000147 if (MF) {
148 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
149 TAP->SetFrameRegister(TRI->getFrameRegister(*MF));
150 }
Chris Lattner8900ef12010-04-05 23:11:24 +0000151
Eric Christopher64d35be2015-02-19 19:52:25 +0000152 emitInlineAsmStart();
Chris Lattner8900ef12010-04-05 23:11:24 +0000153 // Don't implicitly switch to the text section before the asm.
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000154 int Res = Parser->Run(/*NoInitialTextSection*/ true,
155 /*NoFinalize*/ true);
Akira Hatanaka322ffce2015-03-16 18:02:16 +0000156 emitInlineAsmEnd(STI, &TmpSTI);
Chris Lattner59126b22010-04-06 00:55:39 +0000157 if (Res && !HasDiagHandler)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000158 report_fatal_error("Error parsing inline asm\n");
Chris Lattner1e158692010-04-04 18:34:07 +0000159}
160
Chad Rosier17788312012-09-11 19:09:56 +0000161static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
162 MachineModuleInfo *MMI, int InlineAsmVariant,
163 AsmPrinter *AP, unsigned LocCookie,
164 raw_ostream &OS) {
165 // Switch to the inline assembly variant.
166 OS << "\t.intel_syntax\n\t";
Chris Lattner1e158692010-04-04 18:34:07 +0000167
Chad Rosier17788312012-09-11 19:09:56 +0000168 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner1e158692010-04-04 18:34:07 +0000169 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach00915352010-10-01 23:29:12 +0000170
Chad Rosier17788312012-09-11 19:09:56 +0000171 while (*LastEmitted) {
172 switch (*LastEmitted) {
173 default: {
174 // Not a special case, emit the string section literally.
175 const char *LiteralEnd = LastEmitted+1;
176 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
177 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
178 ++LiteralEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000179
Chad Rosier17788312012-09-11 19:09:56 +0000180 OS.write(LastEmitted, LiteralEnd-LastEmitted);
181 LastEmitted = LiteralEnd;
182 break;
183 }
184 case '\n':
185 ++LastEmitted; // Consume newline character.
186 OS << '\n'; // Indent code with newline.
187 break;
188 case '$': {
189 ++LastEmitted; // Consume '$' character.
190 bool Done = true;
Chris Lattner1e158692010-04-04 18:34:07 +0000191
Chad Rosier17788312012-09-11 19:09:56 +0000192 // Handle escapes.
193 switch (*LastEmitted) {
194 default: Done = false; break;
195 case '$':
196 ++LastEmitted; // Consume second '$' character.
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000197 break;
198 }
Chad Rosier17788312012-09-11 19:09:56 +0000199 if (Done) break;
200
201 const char *IDStart = LastEmitted;
202 const char *IDEnd = IDStart;
203 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
204
205 unsigned Val;
206 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
207 report_fatal_error("Bad $ operand number in inline asm string: '" +
208 Twine(AsmStr) + "'");
209 LastEmitted = IDEnd;
210
211 if (Val >= NumOperands-1)
212 report_fatal_error("Invalid $ operand number in inline asm string: '" +
213 Twine(AsmStr) + "'");
214
215 // Okay, we finally have a value number. Ask the target to print this
216 // operand!
217 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
218
219 bool Error = false;
220
221 // Scan to find the machine operand number for the operand.
222 for (; Val; --Val) {
223 if (OpNo >= MI->getNumOperands()) break;
224 unsigned OpFlags = MI->getOperand(OpNo).getImm();
225 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
226 }
227
228 // We may have a location metadata attached to the end of the
229 // instruction, and at no point should see metadata at any
230 // other point while processing. It's an error if so.
231 if (OpNo >= MI->getNumOperands() ||
232 MI->getOperand(OpNo).isMetadata()) {
233 Error = true;
234 } else {
235 unsigned OpFlags = MI->getOperand(OpNo).getImm();
236 ++OpNo; // Skip over the ID number.
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000237
Chad Rosier17788312012-09-11 19:09:56 +0000238 if (InlineAsm::isMemKind(OpFlags)) {
239 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000240 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000241 } else {
242 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000243 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000244 }
245 }
246 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000247 std::string msg;
248 raw_string_ostream Msg(msg);
Chad Rosier17788312012-09-11 19:09:56 +0000249 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
250 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
251 }
252 break;
253 }
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000254 }
Chris Lattner51065562010-04-07 05:38:05 +0000255 }
Chad Rosier17788312012-09-11 19:09:56 +0000256 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
257}
Jim Grosbach00915352010-10-01 23:29:12 +0000258
Chad Rosier17788312012-09-11 19:09:56 +0000259static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
260 MachineModuleInfo *MMI, int InlineAsmVariant,
261 int AsmPrinterVariant, AsmPrinter *AP,
262 unsigned LocCookie, raw_ostream &OS) {
Chris Lattner1e158692010-04-04 18:34:07 +0000263 int CurVariant = -1; // The number of the {.|.|.} region we are in.
264 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chad Rosier17788312012-09-11 19:09:56 +0000265 unsigned NumOperands = MI->getNumOperands();
266
267 OS << '\t';
Jim Grosbach00915352010-10-01 23:29:12 +0000268
Chris Lattner1e158692010-04-04 18:34:07 +0000269 while (*LastEmitted) {
270 switch (*LastEmitted) {
271 default: {
272 // Not a special case, emit the string section literally.
273 const char *LiteralEnd = LastEmitted+1;
274 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
275 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
276 ++LiteralEnd;
277 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
278 OS.write(LastEmitted, LiteralEnd-LastEmitted);
279 LastEmitted = LiteralEnd;
280 break;
281 }
282 case '\n':
283 ++LastEmitted; // Consume newline character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000284 OS << '\n'; // Indent code with newline.
Chris Lattner1e158692010-04-04 18:34:07 +0000285 break;
286 case '$': {
287 ++LastEmitted; // Consume '$' character.
288 bool Done = true;
289
290 // Handle escapes.
291 switch (*LastEmitted) {
292 default: Done = false; break;
293 case '$': // $$ -> $
294 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
295 OS << '$';
296 ++LastEmitted; // Consume second '$' character.
297 break;
298 case '(': // $( -> same as GCC's { character.
299 ++LastEmitted; // Consume '(' character.
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000300 if (CurVariant != -1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000301 report_fatal_error("Nested variants found in inline asm string: '" +
302 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000303 CurVariant = 0; // We're in the first variant now.
304 break;
305 case '|':
306 ++LastEmitted; // consume '|' character.
307 if (CurVariant == -1)
308 OS << '|'; // this is gcc's behavior for | outside a variant
309 else
310 ++CurVariant; // We're in the next variant.
311 break;
312 case ')': // $) -> same as GCC's } char.
313 ++LastEmitted; // consume ')' character.
314 if (CurVariant == -1)
315 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach00915352010-10-01 23:29:12 +0000316 else
Chris Lattner1e158692010-04-04 18:34:07 +0000317 CurVariant = -1;
318 break;
319 }
320 if (Done) break;
Jim Grosbach00915352010-10-01 23:29:12 +0000321
Chris Lattner1e158692010-04-04 18:34:07 +0000322 bool HasCurlyBraces = false;
323 if (*LastEmitted == '{') { // ${variable}
324 ++LastEmitted; // Consume '{' character.
325 HasCurlyBraces = true;
326 }
Jim Grosbach00915352010-10-01 23:29:12 +0000327
Chris Lattner1e158692010-04-04 18:34:07 +0000328 // If we have ${:foo}, then this is not a real operand reference, it is a
329 // "magic" string reference, just like in .td files. Arrange to call
330 // PrintSpecial.
331 if (HasCurlyBraces && *LastEmitted == ':') {
332 ++LastEmitted;
333 const char *StrStart = LastEmitted;
334 const char *StrEnd = strchr(StrStart, '}');
Craig Topper353eda42014-04-24 06:44:33 +0000335 if (!StrEnd)
Benjamin Kramera6769262010-04-08 10:44:28 +0000336 report_fatal_error("Unterminated ${:foo} operand in inline asm"
337 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000338
Chris Lattner1e158692010-04-04 18:34:07 +0000339 std::string Val(StrStart, StrEnd);
Chad Rosier17788312012-09-11 19:09:56 +0000340 AP->PrintSpecial(MI, OS, Val.c_str());
Chris Lattner1e158692010-04-04 18:34:07 +0000341 LastEmitted = StrEnd+1;
342 break;
343 }
Jim Grosbach00915352010-10-01 23:29:12 +0000344
Chris Lattner1e158692010-04-04 18:34:07 +0000345 const char *IDStart = LastEmitted;
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000346 const char *IDEnd = IDStart;
Jim Grosbach00915352010-10-01 23:29:12 +0000347 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
348
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000349 unsigned Val;
350 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramera6769262010-04-08 10:44:28 +0000351 report_fatal_error("Bad $ operand number in inline asm string: '" +
352 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000353 LastEmitted = IDEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000354
Chris Lattner1e158692010-04-04 18:34:07 +0000355 char Modifier[2] = { 0, 0 };
Jim Grosbach00915352010-10-01 23:29:12 +0000356
Chris Lattner1e158692010-04-04 18:34:07 +0000357 if (HasCurlyBraces) {
358 // If we have curly braces, check for a modifier character. This
359 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
360 if (*LastEmitted == ':') {
361 ++LastEmitted; // Consume ':' character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000362 if (*LastEmitted == 0)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000363 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramera6769262010-04-08 10:44:28 +0000364 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000365
Chris Lattner1e158692010-04-04 18:34:07 +0000366 Modifier[0] = *LastEmitted;
367 ++LastEmitted; // Consume modifier character.
368 }
Jim Grosbach00915352010-10-01 23:29:12 +0000369
Chris Lattner0e45d242010-04-05 22:42:30 +0000370 if (*LastEmitted != '}')
Benjamin Kramera6769262010-04-08 10:44:28 +0000371 report_fatal_error("Bad ${} expression in inline asm string: '" +
372 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000373 ++LastEmitted; // Consume '}' character.
374 }
Jim Grosbach00915352010-10-01 23:29:12 +0000375
Chris Lattner0e45d242010-04-05 22:42:30 +0000376 if (Val >= NumOperands-1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000377 report_fatal_error("Invalid $ operand number in inline asm string: '" +
378 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000379
Chris Lattner1e158692010-04-04 18:34:07 +0000380 // Okay, we finally have a value number. Ask the target to print this
381 // operand!
382 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Cheng6eb516d2011-01-07 23:50:32 +0000383 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner1e158692010-04-04 18:34:07 +0000384
385 bool Error = false;
386
387 // Scan to find the machine operand number for the operand.
388 for (; Val; --Val) {
389 if (OpNo >= MI->getNumOperands()) break;
390 unsigned OpFlags = MI->getOperand(OpNo).getImm();
391 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
392 }
393
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000394 // We may have a location metadata attached to the end of the
395 // instruction, and at no point should see metadata at any
396 // other point while processing. It's an error if so.
Eric Christopher12da1692012-03-22 01:33:51 +0000397 if (OpNo >= MI->getNumOperands() ||
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000398 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner1e158692010-04-04 18:34:07 +0000399 Error = true;
400 } else {
401 unsigned OpFlags = MI->getOperand(OpNo).getImm();
402 ++OpNo; // Skip over the ID number.
403
404 if (Modifier[0] == 'l') // labels are target independent
405 // FIXME: What if the operand isn't an MBB, report error?
406 OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
407 else {
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000408 if (InlineAsm::isMemKind(OpFlags)) {
Chad Rosierdb20a412012-09-10 21:10:49 +0000409 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000410 Modifier[0] ? Modifier : nullptr,
Chris Lattner1e158692010-04-04 18:34:07 +0000411 OS);
412 } else {
Chad Rosierdb20a412012-09-10 21:10:49 +0000413 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000414 Modifier[0] ? Modifier : nullptr, OS);
Chris Lattner1e158692010-04-04 18:34:07 +0000415 }
416 }
417 }
418 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000419 std::string msg;
420 raw_string_ostream Msg(msg);
Chris Lattner1e457892010-04-07 23:40:44 +0000421 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
422 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner1e158692010-04-04 18:34:07 +0000423 }
424 }
425 break;
426 }
427 }
428 }
Chad Rosier17788312012-09-11 19:09:56 +0000429 OS << '\n' << (char)0; // null terminate string.
430}
431
432/// EmitInlineAsm - This method formats and emits the specified machine
433/// instruction that is an inline asm.
434void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
435 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
436
437 // Count the number of register definitions to find the asm string.
438 unsigned NumDefs = 0;
439 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
440 ++NumDefs)
441 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
442
443 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
444
445 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
446 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
447
448 // If this asmstr is empty, just print the #APP/#NOAPP markers.
449 // These are useful to see where empty asm's wound up.
450 if (AsmStr[0] == 0) {
Lang Hames9ff69c82015-04-24 19:11:51 +0000451 OutStreamer->emitRawComment(MAI->getInlineAsmStart());
452 OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
Chad Rosier17788312012-09-11 19:09:56 +0000453 return;
Chad Rosier7641f582012-09-10 21:36:05 +0000454 }
455
Chad Rosier17788312012-09-11 19:09:56 +0000456 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000457 // enabled, so we use emitRawComment.
Lang Hames9ff69c82015-04-24 19:11:51 +0000458 OutStreamer->emitRawComment(MAI->getInlineAsmStart());
Chad Rosier17788312012-09-11 19:09:56 +0000459
460 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
461 // it.
462 unsigned LocCookie = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000463 const MDNode *LocMD = nullptr;
Chad Rosier17788312012-09-11 19:09:56 +0000464 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
465 if (MI->getOperand(i-1).isMetadata() &&
466 (LocMD = MI->getOperand(i-1).getMetadata()) &&
467 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000468 if (const ConstantInt *CI =
469 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Chad Rosier17788312012-09-11 19:09:56 +0000470 LocCookie = CI->getZExtValue();
471 break;
472 }
473 }
474 }
475
476 // Emit the inline asm to a temporary string so we can emit it through
477 // EmitInlineAsm.
Alp Tokere69170a2014-06-26 22:52:05 +0000478 SmallString<256> StringData;
479 raw_svector_ostream OS(StringData);
Chad Rosier17788312012-09-11 19:09:56 +0000480
481 // The variant of the current asmprinter.
482 int AsmPrinterVariant = MAI->getAssemblerDialect();
483 InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
484 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
485 if (InlineAsmVariant == InlineAsm::AD_ATT)
486 EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
487 AP, LocCookie, OS);
488 else
489 EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
490
Akira Hatanaka322ffce2015-03-16 18:02:16 +0000491 EmitInlineAsm(OS.str(), getSubtargetInfo(), LocMD, MI->getInlineAsmDialect());
Jim Grosbach00915352010-10-01 23:29:12 +0000492
Chris Lattner1e158692010-04-04 18:34:07 +0000493 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000494 // enabled, so we use emitRawComment.
Lang Hames9ff69c82015-04-24 19:11:51 +0000495 OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
Chris Lattner1e158692010-04-04 18:34:07 +0000496}
497
498
499/// PrintSpecial - Print information related to the specified machine instr
500/// that is independent of the operand, and may be independent of the instr
501/// itself. This can be useful for portably encoding the comment character
502/// or other bits of target-specific knowledge into the asmstrings. The
503/// syntax used is ${:comment}. Targets can override this to add support
504/// for their own strange codes.
505void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
506 const char *Code) const {
Eric Christopher8b770652015-01-26 19:03:15 +0000507 const DataLayout *DL = TM.getDataLayout();
Chris Lattner1e158692010-04-04 18:34:07 +0000508 if (!strcmp(Code, "private")) {
Rafael Espindola58873562014-01-03 19:21:54 +0000509 OS << DL->getPrivateGlobalPrefix();
Chris Lattner1e158692010-04-04 18:34:07 +0000510 } else if (!strcmp(Code, "comment")) {
511 OS << MAI->getCommentString();
512 } else if (!strcmp(Code, "uid")) {
513 // Comparing the address of MI isn't sufficient, because machineinstrs may
514 // be allocated to the same address across functions.
Jim Grosbach00915352010-10-01 23:29:12 +0000515
Chris Lattner1e158692010-04-04 18:34:07 +0000516 // If this is a new LastFn instruction, bump the counter.
517 if (LastMI != MI || LastFn != getFunctionNumber()) {
518 ++Counter;
519 LastMI = MI;
520 LastFn = getFunctionNumber();
521 }
522 OS << Counter;
523 } else {
Alp Tokere69170a2014-06-26 22:52:05 +0000524 std::string msg;
525 raw_string_ostream Msg(msg);
Chris Lattner1e158692010-04-04 18:34:07 +0000526 Msg << "Unknown special formatter '" << Code
527 << "' for machine instr: " << *MI;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000528 report_fatal_error(Msg.str());
Jim Grosbach00915352010-10-01 23:29:12 +0000529 }
Chris Lattner1e158692010-04-04 18:34:07 +0000530}
531
532/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
533/// instruction, using the specified assembler variant. Targets should
534/// override this to format as appropriate.
535bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chad Rosier1f57bcb2012-09-07 20:23:29 +0000536 unsigned AsmVariant, const char *ExtraCode,
537 raw_ostream &O) {
Jack Carterb2fd5f62012-06-21 17:14:46 +0000538 // Does this asm operand have a single letter operand modifier?
539 if (ExtraCode && ExtraCode[0]) {
540 if (ExtraCode[1] != 0) return true; // Unknown modifier.
541
542 const MachineOperand &MO = MI->getOperand(OpNo);
543 switch (ExtraCode[0]) {
544 default:
545 return true; // Unknown modifier.
546 case 'c': // Substitute immediate value without immediate syntax
Jack Carterc457f622012-06-21 21:37:54 +0000547 if (MO.getType() != MachineOperand::MO_Immediate)
Jack Carterb2fd5f62012-06-21 17:14:46 +0000548 return true;
549 O << MO.getImm();
550 return false;
Jack Carterc457f622012-06-21 21:37:54 +0000551 case 'n': // Negate the immediate constant.
552 if (MO.getType() != MachineOperand::MO_Immediate)
553 return true;
554 O << -MO.getImm();
555 return false;
Jack Carterb2fd5f62012-06-21 17:14:46 +0000556 }
557 }
Chris Lattner1e158692010-04-04 18:34:07 +0000558 return true;
559}
560
561bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
562 unsigned AsmVariant,
563 const char *ExtraCode, raw_ostream &O) {
564 // Target doesn't support this yet!
565 return true;
566}
567
Eric Christopher64d35be2015-02-19 19:52:25 +0000568void AsmPrinter::emitInlineAsmStart() const {}
Toma Tabacua23f13c2014-12-17 10:56:16 +0000569
Rafael Espindola65fd0a82014-01-24 15:47:54 +0000570void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
David Peixottoea2bcb92014-02-06 18:19:40 +0000571 const MCSubtargetInfo *EndInfo) const {}