blob: f6ce4a08db5b432c819dd36c1e85a566a735d799 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/Target/TargetMachine.h"
Yuri Gorshenin3939dec2014-09-10 09:45:49 +000036#include "llvm/Target/TargetRegisterInfo.h"
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +000037#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner1e158692010-04-04 18:34:07 +000038using namespace llvm;
39
Chandler Carruth1b9dde02014-04-22 02:02:50 +000040#define DEBUG_TYPE "asm-printer"
41
Chris Lattner300fa452010-11-17 08:03:32 +000042namespace {
43 struct SrcMgrDiagInfo {
44 const MDNode *LocInfo;
Bob Wilsona594fab2013-02-11 05:37:07 +000045 LLVMContext::InlineAsmDiagHandlerTy DiagHandler;
Chris Lattner300fa452010-11-17 08:03:32 +000046 void *DiagContext;
47 };
48}
49
Chad Rosierb759ede2012-09-07 18:16:38 +000050/// srcMgrDiagHandler - This callback is invoked when the SourceMgr for an
Chris Lattner300fa452010-11-17 08:03:32 +000051/// inline asm has an error in it. diagInfo is a pointer to the SrcMgrDiagInfo
52/// struct above.
Chad Rosierb759ede2012-09-07 18:16:38 +000053static void srcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
Chris Lattner300fa452010-11-17 08:03:32 +000054 SrcMgrDiagInfo *DiagInfo = static_cast<SrcMgrDiagInfo *>(diagInfo);
55 assert(DiagInfo && "Diagnostic context not passed down?");
Jim Grosbach098f5a22011-09-21 21:36:53 +000056
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;
Chris Lattner79ffdc72010-11-17 08:20:42 +000060 if (const MDNode *LocInfo = DiagInfo->LocInfo) {
61 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.
Chad Rosierf24ae7b2012-09-05 23:57:37 +000075void AsmPrinter::EmitInlineAsm(StringRef Str, const MDNode *LocMDNode,
76 InlineAsm::AsmDialect Dialect) const {
Chris Lattner1e158692010-04-04 18:34:07 +000077 assert(!Str.empty() && "Can't emit empty inline asm block");
Jim Grosbach00915352010-10-01 23:29:12 +000078
Chris Lattner8900ef12010-04-05 23:11:24 +000079 // Remember if the buffer is nul terminated or not so we can avoid a copy.
80 bool isNullTerminated = Str.back() == 0;
81 if (isNullTerminated)
82 Str = Str.substr(0, Str.size()-1);
Jim Grosbach00915352010-10-01 23:29:12 +000083
Daniel Sanders753e1762014-02-13 14:44:26 +000084 // If the output streamer does not have mature MC support or the integrated
85 // assembler has been disabled, just emit the blob textually.
86 // Otherwise parse the asm and emit it via MC support.
Chris Lattner1e158692010-04-04 18:34:07 +000087 // This is useful in case the asm parser doesn't handle something but the
88 // system assembler does.
Daniel Sanders753e1762014-02-13 14:44:26 +000089 const MCAsmInfo *MCAI = TM.getMCAsmInfo();
90 assert(MCAI && "No MCAsmInfo");
91 if (!MCAI->useIntegratedAssembler() &&
92 !OutStreamer.isIntegratedAssemblerRequired()) {
Toma Tabacua23f13c2014-12-17 10:56:16 +000093 emitInlineAsmStart(TM.getSubtarget<MCSubtargetInfo>());
Chris Lattner1e158692010-04-04 18:34:07 +000094 OutStreamer.EmitRawText(Str);
Craig Topper353eda42014-04-24 06:44:33 +000095 emitInlineAsmEnd(TM.getSubtarget<MCSubtargetInfo>(), nullptr);
Chris Lattner1e158692010-04-04 18:34:07 +000096 return;
97 }
Jim Grosbach00915352010-10-01 23:29:12 +000098
Chris Lattner8900ef12010-04-05 23:11:24 +000099 SourceMgr SrcMgr;
Chris Lattner300fa452010-11-17 08:03:32 +0000100 SrcMgrDiagInfo DiagInfo;
Jim Grosbach00915352010-10-01 23:29:12 +0000101
Bob Wilsona594fab2013-02-11 05:37:07 +0000102 // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
Chris Lattner59126b22010-04-06 00:55:39 +0000103 LLVMContext &LLVMCtx = MMI->getModule()->getContext();
104 bool HasDiagHandler = false;
Craig Topper353eda42014-04-24 06:44:33 +0000105 if (LLVMCtx.getInlineAsmDiagnosticHandler() != nullptr) {
Chad Rosierb759ede2012-09-07 18:16:38 +0000106 // If the source manager has an issue, we arrange for srcMgrDiagHandler
Chris Lattner300fa452010-11-17 08:03:32 +0000107 // to be invoked, getting DiagInfo passed into it.
108 DiagInfo.LocInfo = LocMDNode;
Bob Wilsona594fab2013-02-11 05:37:07 +0000109 DiagInfo.DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
110 DiagInfo.DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
Chad Rosierb759ede2012-09-07 18:16:38 +0000111 SrcMgr.setDiagHandler(srcMgrDiagHandler, &DiagInfo);
Chris Lattner59126b22010-04-06 00:55:39 +0000112 HasDiagHandler = true;
113 }
Jim Grosbach00915352010-10-01 23:29:12 +0000114
Rafael Espindola3560ff22014-08-27 20:03:13 +0000115 std::unique_ptr<MemoryBuffer> Buffer;
116 if (isNullTerminated)
117 Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
118 else
119 Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
Chris Lattner8900ef12010-04-05 23:11:24 +0000120
121 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
David Blaikie1961f142014-08-21 20:44:56 +0000122 SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
Jim Grosbach00915352010-10-01 23:29:12 +0000123
Ahmed Charles56440fd2014-03-06 05:51:42 +0000124 std::unique_ptr<MCAsmParser> Parser(
125 createMCAsmParser(SrcMgr, OutContext, OutStreamer, *MAI));
Evan Cheng4d1ca962011-07-08 01:53:10 +0000126
David Peixottoea2bcb92014-02-06 18:19:40 +0000127 // Initialize the parser with a fresh subtarget info. It is better to use a
128 // new STI here because the parser may modify it and we do not want those
129 // modifications to persist after parsing the inlineasm. The modifications
130 // made by the parser will be seen by the code emitters because it passes
131 // the current STI down to the EncodeInstruction() method.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000132 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
David Peixottoea2bcb92014-02-06 18:19:40 +0000133 TM.getTargetTriple(), TM.getTargetCPU(), TM.getTargetFeatureString()));
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000134
David Peixottoea2bcb92014-02-06 18:19:40 +0000135 // Preserve a copy of the original STI because the parser may modify it. For
136 // example, when switching between arm and thumb mode. If the target needs to
Eric Christopherf9761a22014-02-26 02:53:18 +0000137 // emit code to return to the original state it can do so in
138 // emitInlineAsmEnd().
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000139 MCSubtargetInfo STIOrig = *STI;
140
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000141 MCTargetOptions MCOptions;
142 if (MF)
143 MCOptions = MF->getTarget().Options.MCOptions;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000144 std::unique_ptr<MCTargetAsmParser> TAP(
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000145 TM.getTarget().createMCAsmParser(*STI, *Parser, *MII, MCOptions));
Chris Lattner8900ef12010-04-05 23:11:24 +0000146 if (!TAP)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000147 report_fatal_error("Inline asm not supported by this streamer because"
Benjamin Kramera6769262010-04-08 10:44:28 +0000148 " we don't have an asm parser for this target\n");
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000149 Parser->setAssemblerDialect(Dialect);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000150 Parser->setTargetParser(*TAP.get());
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
Toma Tabacua23f13c2014-12-17 10:56:16 +0000156 emitInlineAsmStart(STIOrig);
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);
David Peixottoea2bcb92014-02-06 18:19:40 +0000160 emitInlineAsmEnd(STIOrig, STI.get());
Chris Lattner59126b22010-04-06 00:55:39 +0000161 if (Res && !HasDiagHandler)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000162 report_fatal_error("Error parsing inline asm\n");
Chris Lattner1e158692010-04-04 18:34:07 +0000163}
164
Chad Rosier17788312012-09-11 19:09:56 +0000165static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
166 MachineModuleInfo *MMI, int InlineAsmVariant,
167 AsmPrinter *AP, unsigned LocCookie,
168 raw_ostream &OS) {
169 // Switch to the inline assembly variant.
170 OS << "\t.intel_syntax\n\t";
Chris Lattner1e158692010-04-04 18:34:07 +0000171
Chad Rosier17788312012-09-11 19:09:56 +0000172 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner1e158692010-04-04 18:34:07 +0000173 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach00915352010-10-01 23:29:12 +0000174
Chad Rosier17788312012-09-11 19:09:56 +0000175 while (*LastEmitted) {
176 switch (*LastEmitted) {
177 default: {
178 // Not a special case, emit the string section literally.
179 const char *LiteralEnd = LastEmitted+1;
180 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
181 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
182 ++LiteralEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000183
Chad Rosier17788312012-09-11 19:09:56 +0000184 OS.write(LastEmitted, LiteralEnd-LastEmitted);
185 LastEmitted = LiteralEnd;
186 break;
187 }
188 case '\n':
189 ++LastEmitted; // Consume newline character.
190 OS << '\n'; // Indent code with newline.
191 break;
192 case '$': {
193 ++LastEmitted; // Consume '$' character.
194 bool Done = true;
Chris Lattner1e158692010-04-04 18:34:07 +0000195
Chad Rosier17788312012-09-11 19:09:56 +0000196 // Handle escapes.
197 switch (*LastEmitted) {
198 default: Done = false; break;
199 case '$':
200 ++LastEmitted; // Consume second '$' character.
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000201 break;
202 }
Chad Rosier17788312012-09-11 19:09:56 +0000203 if (Done) break;
204
205 const char *IDStart = LastEmitted;
206 const char *IDEnd = IDStart;
207 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
208
209 unsigned Val;
210 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
211 report_fatal_error("Bad $ operand number in inline asm string: '" +
212 Twine(AsmStr) + "'");
213 LastEmitted = IDEnd;
214
215 if (Val >= NumOperands-1)
216 report_fatal_error("Invalid $ operand number in inline asm string: '" +
217 Twine(AsmStr) + "'");
218
219 // Okay, we finally have a value number. Ask the target to print this
220 // operand!
221 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
222
223 bool Error = false;
224
225 // Scan to find the machine operand number for the operand.
226 for (; Val; --Val) {
227 if (OpNo >= MI->getNumOperands()) break;
228 unsigned OpFlags = MI->getOperand(OpNo).getImm();
229 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
230 }
231
232 // We may have a location metadata attached to the end of the
233 // instruction, and at no point should see metadata at any
234 // other point while processing. It's an error if so.
235 if (OpNo >= MI->getNumOperands() ||
236 MI->getOperand(OpNo).isMetadata()) {
237 Error = true;
238 } else {
239 unsigned OpFlags = MI->getOperand(OpNo).getImm();
240 ++OpNo; // Skip over the ID number.
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000241
Chad Rosier17788312012-09-11 19:09:56 +0000242 if (InlineAsm::isMemKind(OpFlags)) {
243 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000244 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000245 } else {
246 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000247 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000248 }
249 }
250 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000251 std::string msg;
252 raw_string_ostream Msg(msg);
Chad Rosier17788312012-09-11 19:09:56 +0000253 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
254 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
255 }
256 break;
257 }
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000258 }
Chris Lattner51065562010-04-07 05:38:05 +0000259 }
Chad Rosier17788312012-09-11 19:09:56 +0000260 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
261}
Jim Grosbach00915352010-10-01 23:29:12 +0000262
Chad Rosier17788312012-09-11 19:09:56 +0000263static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
264 MachineModuleInfo *MMI, int InlineAsmVariant,
265 int AsmPrinterVariant, AsmPrinter *AP,
266 unsigned LocCookie, raw_ostream &OS) {
Chris Lattner1e158692010-04-04 18:34:07 +0000267 int CurVariant = -1; // The number of the {.|.|.} region we are in.
268 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chad Rosier17788312012-09-11 19:09:56 +0000269 unsigned NumOperands = MI->getNumOperands();
270
271 OS << '\t';
Jim Grosbach00915352010-10-01 23:29:12 +0000272
Chris Lattner1e158692010-04-04 18:34:07 +0000273 while (*LastEmitted) {
274 switch (*LastEmitted) {
275 default: {
276 // Not a special case, emit the string section literally.
277 const char *LiteralEnd = LastEmitted+1;
278 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
279 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
280 ++LiteralEnd;
281 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
282 OS.write(LastEmitted, LiteralEnd-LastEmitted);
283 LastEmitted = LiteralEnd;
284 break;
285 }
286 case '\n':
287 ++LastEmitted; // Consume newline character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000288 OS << '\n'; // Indent code with newline.
Chris Lattner1e158692010-04-04 18:34:07 +0000289 break;
290 case '$': {
291 ++LastEmitted; // Consume '$' character.
292 bool Done = true;
293
294 // Handle escapes.
295 switch (*LastEmitted) {
296 default: Done = false; break;
297 case '$': // $$ -> $
298 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
299 OS << '$';
300 ++LastEmitted; // Consume second '$' character.
301 break;
302 case '(': // $( -> same as GCC's { character.
303 ++LastEmitted; // Consume '(' character.
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000304 if (CurVariant != -1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000305 report_fatal_error("Nested variants found in inline asm string: '" +
306 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000307 CurVariant = 0; // We're in the first variant now.
308 break;
309 case '|':
310 ++LastEmitted; // consume '|' character.
311 if (CurVariant == -1)
312 OS << '|'; // this is gcc's behavior for | outside a variant
313 else
314 ++CurVariant; // We're in the next variant.
315 break;
316 case ')': // $) -> same as GCC's } char.
317 ++LastEmitted; // consume ')' character.
318 if (CurVariant == -1)
319 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach00915352010-10-01 23:29:12 +0000320 else
Chris Lattner1e158692010-04-04 18:34:07 +0000321 CurVariant = -1;
322 break;
323 }
324 if (Done) break;
Jim Grosbach00915352010-10-01 23:29:12 +0000325
Chris Lattner1e158692010-04-04 18:34:07 +0000326 bool HasCurlyBraces = false;
327 if (*LastEmitted == '{') { // ${variable}
328 ++LastEmitted; // Consume '{' character.
329 HasCurlyBraces = true;
330 }
Jim Grosbach00915352010-10-01 23:29:12 +0000331
Chris Lattner1e158692010-04-04 18:34:07 +0000332 // If we have ${:foo}, then this is not a real operand reference, it is a
333 // "magic" string reference, just like in .td files. Arrange to call
334 // PrintSpecial.
335 if (HasCurlyBraces && *LastEmitted == ':') {
336 ++LastEmitted;
337 const char *StrStart = LastEmitted;
338 const char *StrEnd = strchr(StrStart, '}');
Craig Topper353eda42014-04-24 06:44:33 +0000339 if (!StrEnd)
Benjamin Kramera6769262010-04-08 10:44:28 +0000340 report_fatal_error("Unterminated ${:foo} operand in inline asm"
341 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000342
Chris Lattner1e158692010-04-04 18:34:07 +0000343 std::string Val(StrStart, StrEnd);
Chad Rosier17788312012-09-11 19:09:56 +0000344 AP->PrintSpecial(MI, OS, Val.c_str());
Chris Lattner1e158692010-04-04 18:34:07 +0000345 LastEmitted = StrEnd+1;
346 break;
347 }
Jim Grosbach00915352010-10-01 23:29:12 +0000348
Chris Lattner1e158692010-04-04 18:34:07 +0000349 const char *IDStart = LastEmitted;
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000350 const char *IDEnd = IDStart;
Jim Grosbach00915352010-10-01 23:29:12 +0000351 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
352
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000353 unsigned Val;
354 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramera6769262010-04-08 10:44:28 +0000355 report_fatal_error("Bad $ operand number in inline asm string: '" +
356 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000357 LastEmitted = IDEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000358
Chris Lattner1e158692010-04-04 18:34:07 +0000359 char Modifier[2] = { 0, 0 };
Jim Grosbach00915352010-10-01 23:29:12 +0000360
Chris Lattner1e158692010-04-04 18:34:07 +0000361 if (HasCurlyBraces) {
362 // If we have curly braces, check for a modifier character. This
363 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
364 if (*LastEmitted == ':') {
365 ++LastEmitted; // Consume ':' character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000366 if (*LastEmitted == 0)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000367 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramera6769262010-04-08 10:44:28 +0000368 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000369
Chris Lattner1e158692010-04-04 18:34:07 +0000370 Modifier[0] = *LastEmitted;
371 ++LastEmitted; // Consume modifier character.
372 }
Jim Grosbach00915352010-10-01 23:29:12 +0000373
Chris Lattner0e45d242010-04-05 22:42:30 +0000374 if (*LastEmitted != '}')
Benjamin Kramera6769262010-04-08 10:44:28 +0000375 report_fatal_error("Bad ${} expression in inline asm string: '" +
376 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000377 ++LastEmitted; // Consume '}' character.
378 }
Jim Grosbach00915352010-10-01 23:29:12 +0000379
Chris Lattner0e45d242010-04-05 22:42:30 +0000380 if (Val >= NumOperands-1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000381 report_fatal_error("Invalid $ operand number in inline asm string: '" +
382 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000383
Chris Lattner1e158692010-04-04 18:34:07 +0000384 // Okay, we finally have a value number. Ask the target to print this
385 // operand!
386 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Cheng6eb516d2011-01-07 23:50:32 +0000387 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner1e158692010-04-04 18:34:07 +0000388
389 bool Error = false;
390
391 // Scan to find the machine operand number for the operand.
392 for (; Val; --Val) {
393 if (OpNo >= MI->getNumOperands()) break;
394 unsigned OpFlags = MI->getOperand(OpNo).getImm();
395 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
396 }
397
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000398 // We may have a location metadata attached to the end of the
399 // instruction, and at no point should see metadata at any
400 // other point while processing. It's an error if so.
Eric Christopher12da1692012-03-22 01:33:51 +0000401 if (OpNo >= MI->getNumOperands() ||
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000402 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner1e158692010-04-04 18:34:07 +0000403 Error = true;
404 } else {
405 unsigned OpFlags = MI->getOperand(OpNo).getImm();
406 ++OpNo; // Skip over the ID number.
407
408 if (Modifier[0] == 'l') // labels are target independent
409 // FIXME: What if the operand isn't an MBB, report error?
410 OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
411 else {
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000412 if (InlineAsm::isMemKind(OpFlags)) {
Chad Rosierdb20a412012-09-10 21:10:49 +0000413 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000414 Modifier[0] ? Modifier : nullptr,
Chris Lattner1e158692010-04-04 18:34:07 +0000415 OS);
416 } else {
Chad Rosierdb20a412012-09-10 21:10:49 +0000417 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000418 Modifier[0] ? Modifier : nullptr, OS);
Chris Lattner1e158692010-04-04 18:34:07 +0000419 }
420 }
421 }
422 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000423 std::string msg;
424 raw_string_ostream Msg(msg);
Chris Lattner1e457892010-04-07 23:40:44 +0000425 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
426 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner1e158692010-04-04 18:34:07 +0000427 }
428 }
429 break;
430 }
431 }
432 }
Chad Rosier17788312012-09-11 19:09:56 +0000433 OS << '\n' << (char)0; // null terminate string.
434}
435
436/// EmitInlineAsm - This method formats and emits the specified machine
437/// instruction that is an inline asm.
438void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
439 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
440
441 // Count the number of register definitions to find the asm string.
442 unsigned NumDefs = 0;
443 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
444 ++NumDefs)
445 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
446
447 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
448
449 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
450 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
451
452 // If this asmstr is empty, just print the #APP/#NOAPP markers.
453 // These are useful to see where empty asm's wound up.
454 if (AsmStr[0] == 0) {
Rafael Espindola0b694812014-01-16 16:28:37 +0000455 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
456 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chad Rosier17788312012-09-11 19:09:56 +0000457 return;
Chad Rosier7641f582012-09-10 21:36:05 +0000458 }
459
Chad Rosier17788312012-09-11 19:09:56 +0000460 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000461 // enabled, so we use emitRawComment.
462 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
Chad Rosier17788312012-09-11 19:09:56 +0000463
464 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
465 // it.
466 unsigned LocCookie = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000467 const MDNode *LocMD = nullptr;
Chad Rosier17788312012-09-11 19:09:56 +0000468 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
469 if (MI->getOperand(i-1).isMetadata() &&
470 (LocMD = MI->getOperand(i-1).getMetadata()) &&
471 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000472 if (const ConstantInt *CI =
473 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Chad Rosier17788312012-09-11 19:09:56 +0000474 LocCookie = CI->getZExtValue();
475 break;
476 }
477 }
478 }
479
480 // Emit the inline asm to a temporary string so we can emit it through
481 // EmitInlineAsm.
Alp Tokere69170a2014-06-26 22:52:05 +0000482 SmallString<256> StringData;
483 raw_svector_ostream OS(StringData);
Chad Rosier17788312012-09-11 19:09:56 +0000484
485 // The variant of the current asmprinter.
486 int AsmPrinterVariant = MAI->getAssemblerDialect();
487 InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
488 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
489 if (InlineAsmVariant == InlineAsm::AD_ATT)
490 EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
491 AP, LocCookie, OS);
492 else
493 EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
494
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000495 EmitInlineAsm(OS.str(), LocMD, MI->getInlineAsmDialect());
Jim Grosbach00915352010-10-01 23:29:12 +0000496
Chris Lattner1e158692010-04-04 18:34:07 +0000497 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000498 // enabled, so we use emitRawComment.
499 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chris Lattner1e158692010-04-04 18:34:07 +0000500}
501
502
503/// PrintSpecial - Print information related to the specified machine instr
504/// that is independent of the operand, and may be independent of the instr
505/// itself. This can be useful for portably encoding the comment character
506/// or other bits of target-specific knowledge into the asmstrings. The
507/// syntax used is ${:comment}. Targets can override this to add support
508/// for their own strange codes.
509void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
510 const char *Code) const {
Eric Christopherd9134482014-08-04 21:25:23 +0000511 const DataLayout *DL = TM.getSubtargetImpl()->getDataLayout();
Chris Lattner1e158692010-04-04 18:34:07 +0000512 if (!strcmp(Code, "private")) {
Rafael Espindola58873562014-01-03 19:21:54 +0000513 OS << DL->getPrivateGlobalPrefix();
Chris Lattner1e158692010-04-04 18:34:07 +0000514 } else if (!strcmp(Code, "comment")) {
515 OS << MAI->getCommentString();
516 } else if (!strcmp(Code, "uid")) {
517 // Comparing the address of MI isn't sufficient, because machineinstrs may
518 // be allocated to the same address across functions.
Jim Grosbach00915352010-10-01 23:29:12 +0000519
Chris Lattner1e158692010-04-04 18:34:07 +0000520 // If this is a new LastFn instruction, bump the counter.
521 if (LastMI != MI || LastFn != getFunctionNumber()) {
522 ++Counter;
523 LastMI = MI;
524 LastFn = getFunctionNumber();
525 }
526 OS << Counter;
527 } else {
Alp Tokere69170a2014-06-26 22:52:05 +0000528 std::string msg;
529 raw_string_ostream Msg(msg);
Chris Lattner1e158692010-04-04 18:34:07 +0000530 Msg << "Unknown special formatter '" << Code
531 << "' for machine instr: " << *MI;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000532 report_fatal_error(Msg.str());
Jim Grosbach00915352010-10-01 23:29:12 +0000533 }
Chris Lattner1e158692010-04-04 18:34:07 +0000534}
535
536/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
537/// instruction, using the specified assembler variant. Targets should
538/// override this to format as appropriate.
539bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chad Rosier1f57bcb2012-09-07 20:23:29 +0000540 unsigned AsmVariant, const char *ExtraCode,
541 raw_ostream &O) {
Jack Carterb2fd5f62012-06-21 17:14:46 +0000542 // Does this asm operand have a single letter operand modifier?
543 if (ExtraCode && ExtraCode[0]) {
544 if (ExtraCode[1] != 0) return true; // Unknown modifier.
545
546 const MachineOperand &MO = MI->getOperand(OpNo);
547 switch (ExtraCode[0]) {
548 default:
549 return true; // Unknown modifier.
550 case 'c': // Substitute immediate value without immediate syntax
Jack Carterc457f622012-06-21 21:37:54 +0000551 if (MO.getType() != MachineOperand::MO_Immediate)
Jack Carterb2fd5f62012-06-21 17:14:46 +0000552 return true;
553 O << MO.getImm();
554 return false;
Jack Carterc457f622012-06-21 21:37:54 +0000555 case 'n': // Negate the immediate constant.
556 if (MO.getType() != MachineOperand::MO_Immediate)
557 return true;
558 O << -MO.getImm();
559 return false;
Jack Carterb2fd5f62012-06-21 17:14:46 +0000560 }
561 }
Chris Lattner1e158692010-04-04 18:34:07 +0000562 return true;
563}
564
565bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
566 unsigned AsmVariant,
567 const char *ExtraCode, raw_ostream &O) {
568 // Target doesn't support this yet!
569 return true;
570}
571
Toma Tabacua23f13c2014-12-17 10:56:16 +0000572void AsmPrinter::emitInlineAsmStart(const MCSubtargetInfo &StartInfo) const {}
573
Rafael Espindola65fd0a82014-01-24 15:47:54 +0000574void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
David Peixottoea2bcb92014-02-06 18:19:40 +0000575 const MCSubtargetInfo *EndInfo) const {}