blob: cca5f229c2fc5f1cc8e9ea51574fc0aa03b2897d [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 =
67 dyn_cast<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()) {
Chris Lattner1e158692010-04-04 18:34:07 +000093 OutStreamer.EmitRawText(Str);
Craig Topper353eda42014-04-24 06:44:33 +000094 emitInlineAsmEnd(TM.getSubtarget<MCSubtargetInfo>(), nullptr);
Chris Lattner1e158692010-04-04 18:34:07 +000095 return;
96 }
Jim Grosbach00915352010-10-01 23:29:12 +000097
Chris Lattner8900ef12010-04-05 23:11:24 +000098 SourceMgr SrcMgr;
Chris Lattner300fa452010-11-17 08:03:32 +000099 SrcMgrDiagInfo DiagInfo;
Jim Grosbach00915352010-10-01 23:29:12 +0000100
Bob Wilsona594fab2013-02-11 05:37:07 +0000101 // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
Chris Lattner59126b22010-04-06 00:55:39 +0000102 LLVMContext &LLVMCtx = MMI->getModule()->getContext();
103 bool HasDiagHandler = false;
Craig Topper353eda42014-04-24 06:44:33 +0000104 if (LLVMCtx.getInlineAsmDiagnosticHandler() != nullptr) {
Chad Rosierb759ede2012-09-07 18:16:38 +0000105 // If the source manager has an issue, we arrange for srcMgrDiagHandler
Chris Lattner300fa452010-11-17 08:03:32 +0000106 // to be invoked, getting DiagInfo passed into it.
107 DiagInfo.LocInfo = LocMDNode;
Bob Wilsona594fab2013-02-11 05:37:07 +0000108 DiagInfo.DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
109 DiagInfo.DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
Chad Rosierb759ede2012-09-07 18:16:38 +0000110 SrcMgr.setDiagHandler(srcMgrDiagHandler, &DiagInfo);
Chris Lattner59126b22010-04-06 00:55:39 +0000111 HasDiagHandler = true;
112 }
Jim Grosbach00915352010-10-01 23:29:12 +0000113
Rafael Espindola3560ff22014-08-27 20:03:13 +0000114 std::unique_ptr<MemoryBuffer> Buffer;
115 if (isNullTerminated)
116 Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
117 else
118 Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
Chris Lattner8900ef12010-04-05 23:11:24 +0000119
120 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
David Blaikie1961f142014-08-21 20:44:56 +0000121 SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
Jim Grosbach00915352010-10-01 23:29:12 +0000122
Ahmed Charles56440fd2014-03-06 05:51:42 +0000123 std::unique_ptr<MCAsmParser> Parser(
124 createMCAsmParser(SrcMgr, OutContext, OutStreamer, *MAI));
Evan Cheng4d1ca962011-07-08 01:53:10 +0000125
David Peixottoea2bcb92014-02-06 18:19:40 +0000126 // Initialize the parser with a fresh subtarget info. It is better to use a
127 // new STI here because the parser may modify it and we do not want those
128 // modifications to persist after parsing the inlineasm. The modifications
129 // made by the parser will be seen by the code emitters because it passes
130 // the current STI down to the EncodeInstruction() method.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000131 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
David Peixottoea2bcb92014-02-06 18:19:40 +0000132 TM.getTargetTriple(), TM.getTargetCPU(), TM.getTargetFeatureString()));
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000133
David Peixottoea2bcb92014-02-06 18:19:40 +0000134 // Preserve a copy of the original STI because the parser may modify it. For
135 // example, when switching between arm and thumb mode. If the target needs to
Eric Christopherf9761a22014-02-26 02:53:18 +0000136 // emit code to return to the original state it can do so in
137 // emitInlineAsmEnd().
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000138 MCSubtargetInfo STIOrig = *STI;
139
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000140 MCTargetOptions MCOptions;
141 if (MF)
142 MCOptions = MF->getTarget().Options.MCOptions;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000143 std::unique_ptr<MCTargetAsmParser> TAP(
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +0000144 TM.getTarget().createMCAsmParser(*STI, *Parser, *MII, MCOptions));
Chris Lattner8900ef12010-04-05 23:11:24 +0000145 if (!TAP)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000146 report_fatal_error("Inline asm not supported by this streamer because"
Benjamin Kramera6769262010-04-08 10:44:28 +0000147 " we don't have an asm parser for this target\n");
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000148 Parser->setAssemblerDialect(Dialect);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000149 Parser->setTargetParser(*TAP.get());
Yuri Gorshenin3939dec2014-09-10 09:45:49 +0000150 if (MF) {
151 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
152 TAP->SetFrameRegister(TRI->getFrameRegister(*MF));
153 }
Chris Lattner8900ef12010-04-05 23:11:24 +0000154
155 // Don't implicitly switch to the text section before the asm.
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000156 int Res = Parser->Run(/*NoInitialTextSection*/ true,
157 /*NoFinalize*/ true);
David Peixottoea2bcb92014-02-06 18:19:40 +0000158 emitInlineAsmEnd(STIOrig, STI.get());
Chris Lattner59126b22010-04-06 00:55:39 +0000159 if (Res && !HasDiagHandler)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000160 report_fatal_error("Error parsing inline asm\n");
Chris Lattner1e158692010-04-04 18:34:07 +0000161}
162
Chad Rosier17788312012-09-11 19:09:56 +0000163static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
164 MachineModuleInfo *MMI, int InlineAsmVariant,
165 AsmPrinter *AP, unsigned LocCookie,
166 raw_ostream &OS) {
167 // Switch to the inline assembly variant.
168 OS << "\t.intel_syntax\n\t";
Chris Lattner1e158692010-04-04 18:34:07 +0000169
Chad Rosier17788312012-09-11 19:09:56 +0000170 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner1e158692010-04-04 18:34:07 +0000171 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach00915352010-10-01 23:29:12 +0000172
Chad Rosier17788312012-09-11 19:09:56 +0000173 while (*LastEmitted) {
174 switch (*LastEmitted) {
175 default: {
176 // Not a special case, emit the string section literally.
177 const char *LiteralEnd = LastEmitted+1;
178 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
179 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
180 ++LiteralEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000181
Chad Rosier17788312012-09-11 19:09:56 +0000182 OS.write(LastEmitted, LiteralEnd-LastEmitted);
183 LastEmitted = LiteralEnd;
184 break;
185 }
186 case '\n':
187 ++LastEmitted; // Consume newline character.
188 OS << '\n'; // Indent code with newline.
189 break;
190 case '$': {
191 ++LastEmitted; // Consume '$' character.
192 bool Done = true;
Chris Lattner1e158692010-04-04 18:34:07 +0000193
Chad Rosier17788312012-09-11 19:09:56 +0000194 // Handle escapes.
195 switch (*LastEmitted) {
196 default: Done = false; break;
197 case '$':
198 ++LastEmitted; // Consume second '$' character.
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000199 break;
200 }
Chad Rosier17788312012-09-11 19:09:56 +0000201 if (Done) break;
202
203 const char *IDStart = LastEmitted;
204 const char *IDEnd = IDStart;
205 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
206
207 unsigned Val;
208 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
209 report_fatal_error("Bad $ operand number in inline asm string: '" +
210 Twine(AsmStr) + "'");
211 LastEmitted = IDEnd;
212
213 if (Val >= NumOperands-1)
214 report_fatal_error("Invalid $ operand number in inline asm string: '" +
215 Twine(AsmStr) + "'");
216
217 // Okay, we finally have a value number. Ask the target to print this
218 // operand!
219 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
220
221 bool Error = false;
222
223 // Scan to find the machine operand number for the operand.
224 for (; Val; --Val) {
225 if (OpNo >= MI->getNumOperands()) break;
226 unsigned OpFlags = MI->getOperand(OpNo).getImm();
227 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
228 }
229
230 // We may have a location metadata attached to the end of the
231 // instruction, and at no point should see metadata at any
232 // other point while processing. It's an error if so.
233 if (OpNo >= MI->getNumOperands() ||
234 MI->getOperand(OpNo).isMetadata()) {
235 Error = true;
236 } else {
237 unsigned OpFlags = MI->getOperand(OpNo).getImm();
238 ++OpNo; // Skip over the ID number.
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000239
Chad Rosier17788312012-09-11 19:09:56 +0000240 if (InlineAsm::isMemKind(OpFlags)) {
241 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000242 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000243 } else {
244 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000245 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000246 }
247 }
248 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000249 std::string msg;
250 raw_string_ostream Msg(msg);
Chad Rosier17788312012-09-11 19:09:56 +0000251 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
252 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
253 }
254 break;
255 }
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000256 }
Chris Lattner51065562010-04-07 05:38:05 +0000257 }
Chad Rosier17788312012-09-11 19:09:56 +0000258 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
259}
Jim Grosbach00915352010-10-01 23:29:12 +0000260
Chad Rosier17788312012-09-11 19:09:56 +0000261static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
262 MachineModuleInfo *MMI, int InlineAsmVariant,
263 int AsmPrinterVariant, AsmPrinter *AP,
264 unsigned LocCookie, raw_ostream &OS) {
Chris Lattner1e158692010-04-04 18:34:07 +0000265 int CurVariant = -1; // The number of the {.|.|.} region we are in.
266 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chad Rosier17788312012-09-11 19:09:56 +0000267 unsigned NumOperands = MI->getNumOperands();
268
269 OS << '\t';
Jim Grosbach00915352010-10-01 23:29:12 +0000270
Chris Lattner1e158692010-04-04 18:34:07 +0000271 while (*LastEmitted) {
272 switch (*LastEmitted) {
273 default: {
274 // Not a special case, emit the string section literally.
275 const char *LiteralEnd = LastEmitted+1;
276 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
277 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
278 ++LiteralEnd;
279 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
280 OS.write(LastEmitted, LiteralEnd-LastEmitted);
281 LastEmitted = LiteralEnd;
282 break;
283 }
284 case '\n':
285 ++LastEmitted; // Consume newline character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000286 OS << '\n'; // Indent code with newline.
Chris Lattner1e158692010-04-04 18:34:07 +0000287 break;
288 case '$': {
289 ++LastEmitted; // Consume '$' character.
290 bool Done = true;
291
292 // Handle escapes.
293 switch (*LastEmitted) {
294 default: Done = false; break;
295 case '$': // $$ -> $
296 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
297 OS << '$';
298 ++LastEmitted; // Consume second '$' character.
299 break;
300 case '(': // $( -> same as GCC's { character.
301 ++LastEmitted; // Consume '(' character.
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000302 if (CurVariant != -1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000303 report_fatal_error("Nested variants found in inline asm string: '" +
304 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000305 CurVariant = 0; // We're in the first variant now.
306 break;
307 case '|':
308 ++LastEmitted; // consume '|' character.
309 if (CurVariant == -1)
310 OS << '|'; // this is gcc's behavior for | outside a variant
311 else
312 ++CurVariant; // We're in the next variant.
313 break;
314 case ')': // $) -> same as GCC's } char.
315 ++LastEmitted; // consume ')' character.
316 if (CurVariant == -1)
317 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach00915352010-10-01 23:29:12 +0000318 else
Chris Lattner1e158692010-04-04 18:34:07 +0000319 CurVariant = -1;
320 break;
321 }
322 if (Done) break;
Jim Grosbach00915352010-10-01 23:29:12 +0000323
Chris Lattner1e158692010-04-04 18:34:07 +0000324 bool HasCurlyBraces = false;
325 if (*LastEmitted == '{') { // ${variable}
326 ++LastEmitted; // Consume '{' character.
327 HasCurlyBraces = true;
328 }
Jim Grosbach00915352010-10-01 23:29:12 +0000329
Chris Lattner1e158692010-04-04 18:34:07 +0000330 // If we have ${:foo}, then this is not a real operand reference, it is a
331 // "magic" string reference, just like in .td files. Arrange to call
332 // PrintSpecial.
333 if (HasCurlyBraces && *LastEmitted == ':') {
334 ++LastEmitted;
335 const char *StrStart = LastEmitted;
336 const char *StrEnd = strchr(StrStart, '}');
Craig Topper353eda42014-04-24 06:44:33 +0000337 if (!StrEnd)
Benjamin Kramera6769262010-04-08 10:44:28 +0000338 report_fatal_error("Unterminated ${:foo} operand in inline asm"
339 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000340
Chris Lattner1e158692010-04-04 18:34:07 +0000341 std::string Val(StrStart, StrEnd);
Chad Rosier17788312012-09-11 19:09:56 +0000342 AP->PrintSpecial(MI, OS, Val.c_str());
Chris Lattner1e158692010-04-04 18:34:07 +0000343 LastEmitted = StrEnd+1;
344 break;
345 }
Jim Grosbach00915352010-10-01 23:29:12 +0000346
Chris Lattner1e158692010-04-04 18:34:07 +0000347 const char *IDStart = LastEmitted;
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000348 const char *IDEnd = IDStart;
Jim Grosbach00915352010-10-01 23:29:12 +0000349 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
350
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000351 unsigned Val;
352 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramera6769262010-04-08 10:44:28 +0000353 report_fatal_error("Bad $ operand number in inline asm string: '" +
354 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000355 LastEmitted = IDEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000356
Chris Lattner1e158692010-04-04 18:34:07 +0000357 char Modifier[2] = { 0, 0 };
Jim Grosbach00915352010-10-01 23:29:12 +0000358
Chris Lattner1e158692010-04-04 18:34:07 +0000359 if (HasCurlyBraces) {
360 // If we have curly braces, check for a modifier character. This
361 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
362 if (*LastEmitted == ':') {
363 ++LastEmitted; // Consume ':' character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000364 if (*LastEmitted == 0)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000365 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramera6769262010-04-08 10:44:28 +0000366 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000367
Chris Lattner1e158692010-04-04 18:34:07 +0000368 Modifier[0] = *LastEmitted;
369 ++LastEmitted; // Consume modifier character.
370 }
Jim Grosbach00915352010-10-01 23:29:12 +0000371
Chris Lattner0e45d242010-04-05 22:42:30 +0000372 if (*LastEmitted != '}')
Benjamin Kramera6769262010-04-08 10:44:28 +0000373 report_fatal_error("Bad ${} expression in inline asm string: '" +
374 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000375 ++LastEmitted; // Consume '}' character.
376 }
Jim Grosbach00915352010-10-01 23:29:12 +0000377
Chris Lattner0e45d242010-04-05 22:42:30 +0000378 if (Val >= NumOperands-1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000379 report_fatal_error("Invalid $ operand number in inline asm string: '" +
380 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000381
Chris Lattner1e158692010-04-04 18:34:07 +0000382 // Okay, we finally have a value number. Ask the target to print this
383 // operand!
384 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Cheng6eb516d2011-01-07 23:50:32 +0000385 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner1e158692010-04-04 18:34:07 +0000386
387 bool Error = false;
388
389 // Scan to find the machine operand number for the operand.
390 for (; Val; --Val) {
391 if (OpNo >= MI->getNumOperands()) break;
392 unsigned OpFlags = MI->getOperand(OpNo).getImm();
393 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
394 }
395
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000396 // We may have a location metadata attached to the end of the
397 // instruction, and at no point should see metadata at any
398 // other point while processing. It's an error if so.
Eric Christopher12da1692012-03-22 01:33:51 +0000399 if (OpNo >= MI->getNumOperands() ||
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000400 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner1e158692010-04-04 18:34:07 +0000401 Error = true;
402 } else {
403 unsigned OpFlags = MI->getOperand(OpNo).getImm();
404 ++OpNo; // Skip over the ID number.
405
406 if (Modifier[0] == 'l') // labels are target independent
407 // FIXME: What if the operand isn't an MBB, report error?
408 OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
409 else {
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000410 if (InlineAsm::isMemKind(OpFlags)) {
Chad Rosierdb20a412012-09-10 21:10:49 +0000411 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000412 Modifier[0] ? Modifier : nullptr,
Chris Lattner1e158692010-04-04 18:34:07 +0000413 OS);
414 } else {
Chad Rosierdb20a412012-09-10 21:10:49 +0000415 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000416 Modifier[0] ? Modifier : nullptr, OS);
Chris Lattner1e158692010-04-04 18:34:07 +0000417 }
418 }
419 }
420 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000421 std::string msg;
422 raw_string_ostream Msg(msg);
Chris Lattner1e457892010-04-07 23:40:44 +0000423 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
424 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner1e158692010-04-04 18:34:07 +0000425 }
426 }
427 break;
428 }
429 }
430 }
Chad Rosier17788312012-09-11 19:09:56 +0000431 OS << '\n' << (char)0; // null terminate string.
432}
433
434/// EmitInlineAsm - This method formats and emits the specified machine
435/// instruction that is an inline asm.
436void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
437 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
438
439 // Count the number of register definitions to find the asm string.
440 unsigned NumDefs = 0;
441 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
442 ++NumDefs)
443 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
444
445 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
446
447 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
448 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
449
450 // If this asmstr is empty, just print the #APP/#NOAPP markers.
451 // These are useful to see where empty asm's wound up.
452 if (AsmStr[0] == 0) {
Rafael Espindola0b694812014-01-16 16:28:37 +0000453 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
454 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chad Rosier17788312012-09-11 19:09:56 +0000455 return;
Chad Rosier7641f582012-09-10 21:36:05 +0000456 }
457
Chad Rosier17788312012-09-11 19:09:56 +0000458 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000459 // enabled, so we use emitRawComment.
460 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
Chad Rosier17788312012-09-11 19:09:56 +0000461
462 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
463 // it.
464 unsigned LocCookie = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000465 const MDNode *LocMD = nullptr;
Chad Rosier17788312012-09-11 19:09:56 +0000466 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
467 if (MI->getOperand(i-1).isMetadata() &&
468 (LocMD = MI->getOperand(i-1).getMetadata()) &&
469 LocMD->getNumOperands() != 0) {
470 if (const ConstantInt *CI = dyn_cast<ConstantInt>(LocMD->getOperand(0))) {
471 LocCookie = CI->getZExtValue();
472 break;
473 }
474 }
475 }
476
477 // Emit the inline asm to a temporary string so we can emit it through
478 // EmitInlineAsm.
Alp Tokere69170a2014-06-26 22:52:05 +0000479 SmallString<256> StringData;
480 raw_svector_ostream OS(StringData);
Chad Rosier17788312012-09-11 19:09:56 +0000481
482 // The variant of the current asmprinter.
483 int AsmPrinterVariant = MAI->getAssemblerDialect();
484 InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
485 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
486 if (InlineAsmVariant == InlineAsm::AD_ATT)
487 EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
488 AP, LocCookie, OS);
489 else
490 EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
491
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000492 EmitInlineAsm(OS.str(), LocMD, MI->getInlineAsmDialect());
Jim Grosbach00915352010-10-01 23:29:12 +0000493
Chris Lattner1e158692010-04-04 18:34:07 +0000494 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000495 // enabled, so we use emitRawComment.
496 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chris Lattner1e158692010-04-04 18:34:07 +0000497}
498
499
500/// PrintSpecial - Print information related to the specified machine instr
501/// that is independent of the operand, and may be independent of the instr
502/// itself. This can be useful for portably encoding the comment character
503/// or other bits of target-specific knowledge into the asmstrings. The
504/// syntax used is ${:comment}. Targets can override this to add support
505/// for their own strange codes.
506void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
507 const char *Code) const {
Eric Christopherd9134482014-08-04 21:25:23 +0000508 const DataLayout *DL = TM.getSubtargetImpl()->getDataLayout();
Chris Lattner1e158692010-04-04 18:34:07 +0000509 if (!strcmp(Code, "private")) {
Rafael Espindola58873562014-01-03 19:21:54 +0000510 OS << DL->getPrivateGlobalPrefix();
Chris Lattner1e158692010-04-04 18:34:07 +0000511 } else if (!strcmp(Code, "comment")) {
512 OS << MAI->getCommentString();
513 } else if (!strcmp(Code, "uid")) {
514 // Comparing the address of MI isn't sufficient, because machineinstrs may
515 // be allocated to the same address across functions.
Jim Grosbach00915352010-10-01 23:29:12 +0000516
Chris Lattner1e158692010-04-04 18:34:07 +0000517 // If this is a new LastFn instruction, bump the counter.
518 if (LastMI != MI || LastFn != getFunctionNumber()) {
519 ++Counter;
520 LastMI = MI;
521 LastFn = getFunctionNumber();
522 }
523 OS << Counter;
524 } else {
Alp Tokere69170a2014-06-26 22:52:05 +0000525 std::string msg;
526 raw_string_ostream Msg(msg);
Chris Lattner1e158692010-04-04 18:34:07 +0000527 Msg << "Unknown special formatter '" << Code
528 << "' for machine instr: " << *MI;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000529 report_fatal_error(Msg.str());
Jim Grosbach00915352010-10-01 23:29:12 +0000530 }
Chris Lattner1e158692010-04-04 18:34:07 +0000531}
532
533/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
534/// instruction, using the specified assembler variant. Targets should
535/// override this to format as appropriate.
536bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chad Rosier1f57bcb2012-09-07 20:23:29 +0000537 unsigned AsmVariant, const char *ExtraCode,
538 raw_ostream &O) {
Jack Carterb2fd5f62012-06-21 17:14:46 +0000539 // Does this asm operand have a single letter operand modifier?
540 if (ExtraCode && ExtraCode[0]) {
541 if (ExtraCode[1] != 0) return true; // Unknown modifier.
542
543 const MachineOperand &MO = MI->getOperand(OpNo);
544 switch (ExtraCode[0]) {
545 default:
546 return true; // Unknown modifier.
547 case 'c': // Substitute immediate value without immediate syntax
Jack Carterc457f622012-06-21 21:37:54 +0000548 if (MO.getType() != MachineOperand::MO_Immediate)
Jack Carterb2fd5f62012-06-21 17:14:46 +0000549 return true;
550 O << MO.getImm();
551 return false;
Jack Carterc457f622012-06-21 21:37:54 +0000552 case 'n': // Negate the immediate constant.
553 if (MO.getType() != MachineOperand::MO_Immediate)
554 return true;
555 O << -MO.getImm();
556 return false;
Jack Carterb2fd5f62012-06-21 17:14:46 +0000557 }
558 }
Chris Lattner1e158692010-04-04 18:34:07 +0000559 return true;
560}
561
562bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
563 unsigned AsmVariant,
564 const char *ExtraCode, raw_ostream &O) {
565 // Target doesn't support this yet!
566 return true;
567}
568
Rafael Espindola65fd0a82014-01-24 15:47:54 +0000569void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
David Peixottoea2bcb92014-02-06 18:19:40 +0000570 const MCSubtargetInfo *EndInfo) const {}