blob: e6e7c973c18f7c6a04581b7b7270d0db3cd15e46 [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.
Chad Rosierf24ae7b2012-09-05 23:57:37 +000076void AsmPrinter::EmitInlineAsm(StringRef Str, const MDNode *LocMDNode,
77 InlineAsm::AsmDialect Dialect) const {
Chris Lattner1e158692010-04-04 18:34:07 +000078 assert(!Str.empty() && "Can't emit empty inline asm block");
Jim Grosbach00915352010-10-01 23:29:12 +000079
Chris Lattner8900ef12010-04-05 23:11:24 +000080 // Remember if the buffer is nul terminated or not so we can avoid a copy.
81 bool isNullTerminated = Str.back() == 0;
82 if (isNullTerminated)
83 Str = Str.substr(0, Str.size()-1);
Jim Grosbach00915352010-10-01 23:29:12 +000084
Daniel Sanders753e1762014-02-13 14:44:26 +000085 // If the output streamer does not have mature MC support or the integrated
86 // assembler has been disabled, just emit the blob textually.
87 // Otherwise parse the asm and emit it via MC support.
Chris Lattner1e158692010-04-04 18:34:07 +000088 // This is useful in case the asm parser doesn't handle something but the
89 // system assembler does.
Daniel Sanders753e1762014-02-13 14:44:26 +000090 const MCAsmInfo *MCAI = TM.getMCAsmInfo();
91 assert(MCAI && "No MCAsmInfo");
92 if (!MCAI->useIntegratedAssembler() &&
93 !OutStreamer.isIntegratedAssemblerRequired()) {
Eric Christopher64d35be2015-02-19 19:52:25 +000094 emitInlineAsmStart();
Chris Lattner1e158692010-04-04 18:34:07 +000095 OutStreamer.EmitRawText(Str);
Eric Christopher45786412015-02-19 21:24:23 +000096 // If we have a machine function then grab the MCSubtarget off of that,
97 // otherwise we're at the module level and want to construct one from
98 // the default CPU and target triple.
99 if (MF) {
100 emitInlineAsmEnd(MF->getSubtarget<MCSubtargetInfo>(), nullptr);
101 } else {
102 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
103 TM.getTargetTriple(), TM.getTargetCPU(),
104 TM.getTargetFeatureString()));
105 emitInlineAsmEnd(*STI, nullptr);
106 }
Chris Lattner1e158692010-04-04 18:34:07 +0000107 return;
108 }
Jim Grosbach00915352010-10-01 23:29:12 +0000109
Chris Lattner8900ef12010-04-05 23:11:24 +0000110 SourceMgr SrcMgr;
Chris Lattner300fa452010-11-17 08:03:32 +0000111 SrcMgrDiagInfo DiagInfo;
Jim Grosbach00915352010-10-01 23:29:12 +0000112
Bob Wilsona594fab2013-02-11 05:37:07 +0000113 // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
Chris Lattner59126b22010-04-06 00:55:39 +0000114 LLVMContext &LLVMCtx = MMI->getModule()->getContext();
115 bool HasDiagHandler = false;
Craig Topper353eda42014-04-24 06:44:33 +0000116 if (LLVMCtx.getInlineAsmDiagnosticHandler() != nullptr) {
Chad Rosierb759ede2012-09-07 18:16:38 +0000117 // If the source manager has an issue, we arrange for srcMgrDiagHandler
Chris Lattner300fa452010-11-17 08:03:32 +0000118 // to be invoked, getting DiagInfo passed into it.
119 DiagInfo.LocInfo = LocMDNode;
Bob Wilsona594fab2013-02-11 05:37:07 +0000120 DiagInfo.DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
121 DiagInfo.DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
Chad Rosierb759ede2012-09-07 18:16:38 +0000122 SrcMgr.setDiagHandler(srcMgrDiagHandler, &DiagInfo);
Chris Lattner59126b22010-04-06 00:55:39 +0000123 HasDiagHandler = true;
124 }
Jim Grosbach00915352010-10-01 23:29:12 +0000125
Rafael Espindola3560ff22014-08-27 20:03:13 +0000126 std::unique_ptr<MemoryBuffer> Buffer;
127 if (isNullTerminated)
128 Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
129 else
130 Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
Chris Lattner8900ef12010-04-05 23:11:24 +0000131
132 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
David Blaikie1961f142014-08-21 20:44:56 +0000133 SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
Jim Grosbach00915352010-10-01 23:29:12 +0000134
Ahmed Charles56440fd2014-03-06 05:51:42 +0000135 std::unique_ptr<MCAsmParser> Parser(
136 createMCAsmParser(SrcMgr, OutContext, OutStreamer, *MAI));
Evan Cheng4d1ca962011-07-08 01:53:10 +0000137
David Peixottoea2bcb92014-02-06 18:19:40 +0000138 // Initialize the parser with a fresh subtarget info. It is better to use a
139 // new STI here because the parser may modify it and we do not want those
140 // modifications to persist after parsing the inlineasm. The modifications
141 // made by the parser will be seen by the code emitters because it passes
142 // the current STI down to the EncodeInstruction() method.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000143 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
David Peixottoea2bcb92014-02-06 18:19:40 +0000144 TM.getTargetTriple(), TM.getTargetCPU(), TM.getTargetFeatureString()));
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000145
David Peixottoea2bcb92014-02-06 18:19:40 +0000146 // Preserve a copy of the original STI because the parser may modify it. For
147 // example, when switching between arm and thumb mode. If the target needs to
Eric Christopherf9761a22014-02-26 02:53:18 +0000148 // emit code to return to the original state it can do so in
149 // emitInlineAsmEnd().
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000150 MCSubtargetInfo STIOrig = *STI;
151
Eric Christopher3f05e1a2015-02-21 09:09:15 +0000152 // We create a new MCInstrInfo here since we might be at the module level
Eric Christopher2105ae92015-02-19 23:52:35 +0000153 // and not have a MachineFunction to initialize the TargetInstrInfo from and
Eric Christopher3f05e1a2015-02-21 09:09:15 +0000154 // we only need MCInstrInfo for asm parsing. We create one unconditionally
155 // because it's not subtarget dependent.
156 std::unique_ptr<MCInstrInfo> MII(TM.getTarget().createMCInstrInfo());
Eric Christophercbdbf392015-02-19 21:29:51 +0000157 std::unique_ptr<MCTargetAsmParser> TAP(TM.getTarget().createMCAsmParser(
Eric Christopher97ea7622015-02-20 06:35:21 +0000158 *STI, *Parser, *MII, TM.Options.MCOptions));
Chris Lattner8900ef12010-04-05 23:11:24 +0000159 if (!TAP)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000160 report_fatal_error("Inline asm not supported by this streamer because"
Benjamin Kramera6769262010-04-08 10:44:28 +0000161 " we don't have an asm parser for this target\n");
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000162 Parser->setAssemblerDialect(Dialect);
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000163 Parser->setTargetParser(*TAP.get());
Yuri Gorshenin3939dec2014-09-10 09:45:49 +0000164 if (MF) {
165 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
166 TAP->SetFrameRegister(TRI->getFrameRegister(*MF));
167 }
Chris Lattner8900ef12010-04-05 23:11:24 +0000168
Eric Christopher64d35be2015-02-19 19:52:25 +0000169 emitInlineAsmStart();
Chris Lattner8900ef12010-04-05 23:11:24 +0000170 // Don't implicitly switch to the text section before the asm.
Daniel Dunbar7f5bf5a2010-07-18 18:31:33 +0000171 int Res = Parser->Run(/*NoInitialTextSection*/ true,
172 /*NoFinalize*/ true);
David Peixottoea2bcb92014-02-06 18:19:40 +0000173 emitInlineAsmEnd(STIOrig, STI.get());
Chris Lattner59126b22010-04-06 00:55:39 +0000174 if (Res && !HasDiagHandler)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000175 report_fatal_error("Error parsing inline asm\n");
Chris Lattner1e158692010-04-04 18:34:07 +0000176}
177
Chad Rosier17788312012-09-11 19:09:56 +0000178static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
179 MachineModuleInfo *MMI, int InlineAsmVariant,
180 AsmPrinter *AP, unsigned LocCookie,
181 raw_ostream &OS) {
182 // Switch to the inline assembly variant.
183 OS << "\t.intel_syntax\n\t";
Chris Lattner1e158692010-04-04 18:34:07 +0000184
Chad Rosier17788312012-09-11 19:09:56 +0000185 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner1e158692010-04-04 18:34:07 +0000186 unsigned NumOperands = MI->getNumOperands();
Jim Grosbach00915352010-10-01 23:29:12 +0000187
Chad Rosier17788312012-09-11 19:09:56 +0000188 while (*LastEmitted) {
189 switch (*LastEmitted) {
190 default: {
191 // Not a special case, emit the string section literally.
192 const char *LiteralEnd = LastEmitted+1;
193 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
194 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
195 ++LiteralEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000196
Chad Rosier17788312012-09-11 19:09:56 +0000197 OS.write(LastEmitted, LiteralEnd-LastEmitted);
198 LastEmitted = LiteralEnd;
199 break;
200 }
201 case '\n':
202 ++LastEmitted; // Consume newline character.
203 OS << '\n'; // Indent code with newline.
204 break;
205 case '$': {
206 ++LastEmitted; // Consume '$' character.
207 bool Done = true;
Chris Lattner1e158692010-04-04 18:34:07 +0000208
Chad Rosier17788312012-09-11 19:09:56 +0000209 // Handle escapes.
210 switch (*LastEmitted) {
211 default: Done = false; break;
212 case '$':
213 ++LastEmitted; // Consume second '$' character.
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000214 break;
215 }
Chad Rosier17788312012-09-11 19:09:56 +0000216 if (Done) break;
217
218 const char *IDStart = LastEmitted;
219 const char *IDEnd = IDStart;
220 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
221
222 unsigned Val;
223 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
224 report_fatal_error("Bad $ operand number in inline asm string: '" +
225 Twine(AsmStr) + "'");
226 LastEmitted = IDEnd;
227
228 if (Val >= NumOperands-1)
229 report_fatal_error("Invalid $ operand number in inline asm string: '" +
230 Twine(AsmStr) + "'");
231
232 // Okay, we finally have a value number. Ask the target to print this
233 // operand!
234 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
235
236 bool Error = false;
237
238 // Scan to find the machine operand number for the operand.
239 for (; Val; --Val) {
240 if (OpNo >= MI->getNumOperands()) break;
241 unsigned OpFlags = MI->getOperand(OpNo).getImm();
242 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
243 }
244
245 // We may have a location metadata attached to the end of the
246 // instruction, and at no point should see metadata at any
247 // other point while processing. It's an error if so.
248 if (OpNo >= MI->getNumOperands() ||
249 MI->getOperand(OpNo).isMetadata()) {
250 Error = true;
251 } else {
252 unsigned OpFlags = MI->getOperand(OpNo).getImm();
253 ++OpNo; // Skip over the ID number.
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000254
Chad Rosier17788312012-09-11 19:09:56 +0000255 if (InlineAsm::isMemKind(OpFlags)) {
256 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000257 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000258 } else {
259 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000260 /*Modifier*/ nullptr, OS);
Chad Rosier17788312012-09-11 19:09:56 +0000261 }
262 }
263 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000264 std::string msg;
265 raw_string_ostream Msg(msg);
Chad Rosier17788312012-09-11 19:09:56 +0000266 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
267 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
268 }
269 break;
270 }
Chris Lattner2a7f6fd2010-11-17 07:53:40 +0000271 }
Chris Lattner51065562010-04-07 05:38:05 +0000272 }
Chad Rosier17788312012-09-11 19:09:56 +0000273 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
274}
Jim Grosbach00915352010-10-01 23:29:12 +0000275
Chad Rosier17788312012-09-11 19:09:56 +0000276static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
277 MachineModuleInfo *MMI, int InlineAsmVariant,
278 int AsmPrinterVariant, AsmPrinter *AP,
279 unsigned LocCookie, raw_ostream &OS) {
Chris Lattner1e158692010-04-04 18:34:07 +0000280 int CurVariant = -1; // The number of the {.|.|.} region we are in.
281 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chad Rosier17788312012-09-11 19:09:56 +0000282 unsigned NumOperands = MI->getNumOperands();
283
284 OS << '\t';
Jim Grosbach00915352010-10-01 23:29:12 +0000285
Chris Lattner1e158692010-04-04 18:34:07 +0000286 while (*LastEmitted) {
287 switch (*LastEmitted) {
288 default: {
289 // Not a special case, emit the string section literally.
290 const char *LiteralEnd = LastEmitted+1;
291 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
292 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
293 ++LiteralEnd;
294 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
295 OS.write(LastEmitted, LiteralEnd-LastEmitted);
296 LastEmitted = LiteralEnd;
297 break;
298 }
299 case '\n':
300 ++LastEmitted; // Consume newline character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000301 OS << '\n'; // Indent code with newline.
Chris Lattner1e158692010-04-04 18:34:07 +0000302 break;
303 case '$': {
304 ++LastEmitted; // Consume '$' character.
305 bool Done = true;
306
307 // Handle escapes.
308 switch (*LastEmitted) {
309 default: Done = false; break;
310 case '$': // $$ -> $
311 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
312 OS << '$';
313 ++LastEmitted; // Consume second '$' character.
314 break;
315 case '(': // $( -> same as GCC's { character.
316 ++LastEmitted; // Consume '(' character.
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000317 if (CurVariant != -1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000318 report_fatal_error("Nested variants found in inline asm string: '" +
319 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000320 CurVariant = 0; // We're in the first variant now.
321 break;
322 case '|':
323 ++LastEmitted; // consume '|' character.
324 if (CurVariant == -1)
325 OS << '|'; // this is gcc's behavior for | outside a variant
326 else
327 ++CurVariant; // We're in the next variant.
328 break;
329 case ')': // $) -> same as GCC's } char.
330 ++LastEmitted; // consume ')' character.
331 if (CurVariant == -1)
332 OS << '}'; // this is gcc's behavior for } outside a variant
Jim Grosbach00915352010-10-01 23:29:12 +0000333 else
Chris Lattner1e158692010-04-04 18:34:07 +0000334 CurVariant = -1;
335 break;
336 }
337 if (Done) break;
Jim Grosbach00915352010-10-01 23:29:12 +0000338
Chris Lattner1e158692010-04-04 18:34:07 +0000339 bool HasCurlyBraces = false;
340 if (*LastEmitted == '{') { // ${variable}
341 ++LastEmitted; // Consume '{' character.
342 HasCurlyBraces = true;
343 }
Jim Grosbach00915352010-10-01 23:29:12 +0000344
Chris Lattner1e158692010-04-04 18:34:07 +0000345 // If we have ${:foo}, then this is not a real operand reference, it is a
346 // "magic" string reference, just like in .td files. Arrange to call
347 // PrintSpecial.
348 if (HasCurlyBraces && *LastEmitted == ':') {
349 ++LastEmitted;
350 const char *StrStart = LastEmitted;
351 const char *StrEnd = strchr(StrStart, '}');
Craig Topper353eda42014-04-24 06:44:33 +0000352 if (!StrEnd)
Benjamin Kramera6769262010-04-08 10:44:28 +0000353 report_fatal_error("Unterminated ${:foo} operand in inline asm"
354 " string: '" + Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000355
Chris Lattner1e158692010-04-04 18:34:07 +0000356 std::string Val(StrStart, StrEnd);
Chad Rosier17788312012-09-11 19:09:56 +0000357 AP->PrintSpecial(MI, OS, Val.c_str());
Chris Lattner1e158692010-04-04 18:34:07 +0000358 LastEmitted = StrEnd+1;
359 break;
360 }
Jim Grosbach00915352010-10-01 23:29:12 +0000361
Chris Lattner1e158692010-04-04 18:34:07 +0000362 const char *IDStart = LastEmitted;
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000363 const char *IDEnd = IDStart;
Jim Grosbach00915352010-10-01 23:29:12 +0000364 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
365
Chris Lattnerbaa2c972010-04-04 18:42:18 +0000366 unsigned Val;
367 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
Benjamin Kramera6769262010-04-08 10:44:28 +0000368 report_fatal_error("Bad $ operand number in inline asm string: '" +
369 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000370 LastEmitted = IDEnd;
Jim Grosbach00915352010-10-01 23:29:12 +0000371
Chris Lattner1e158692010-04-04 18:34:07 +0000372 char Modifier[2] = { 0, 0 };
Jim Grosbach00915352010-10-01 23:29:12 +0000373
Chris Lattner1e158692010-04-04 18:34:07 +0000374 if (HasCurlyBraces) {
375 // If we have curly braces, check for a modifier character. This
376 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
377 if (*LastEmitted == ':') {
378 ++LastEmitted; // Consume ':' character.
Chris Lattner0e45d242010-04-05 22:42:30 +0000379 if (*LastEmitted == 0)
Chris Lattner2104b8d2010-04-07 22:58:41 +0000380 report_fatal_error("Bad ${:} expression in inline asm string: '" +
Benjamin Kramera6769262010-04-08 10:44:28 +0000381 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000382
Chris Lattner1e158692010-04-04 18:34:07 +0000383 Modifier[0] = *LastEmitted;
384 ++LastEmitted; // Consume modifier character.
385 }
Jim Grosbach00915352010-10-01 23:29:12 +0000386
Chris Lattner0e45d242010-04-05 22:42:30 +0000387 if (*LastEmitted != '}')
Benjamin Kramera6769262010-04-08 10:44:28 +0000388 report_fatal_error("Bad ${} expression in inline asm string: '" +
389 Twine(AsmStr) + "'");
Chris Lattner1e158692010-04-04 18:34:07 +0000390 ++LastEmitted; // Consume '}' character.
391 }
Jim Grosbach00915352010-10-01 23:29:12 +0000392
Chris Lattner0e45d242010-04-05 22:42:30 +0000393 if (Val >= NumOperands-1)
Benjamin Kramera6769262010-04-08 10:44:28 +0000394 report_fatal_error("Invalid $ operand number in inline asm string: '" +
395 Twine(AsmStr) + "'");
Jim Grosbach00915352010-10-01 23:29:12 +0000396
Chris Lattner1e158692010-04-04 18:34:07 +0000397 // Okay, we finally have a value number. Ask the target to print this
398 // operand!
399 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
Evan Cheng6eb516d2011-01-07 23:50:32 +0000400 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
Chris Lattner1e158692010-04-04 18:34:07 +0000401
402 bool Error = false;
403
404 // Scan to find the machine operand number for the operand.
405 for (; Val; --Val) {
406 if (OpNo >= MI->getNumOperands()) break;
407 unsigned OpFlags = MI->getOperand(OpNo).getImm();
408 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
409 }
410
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000411 // We may have a location metadata attached to the end of the
412 // instruction, and at no point should see metadata at any
413 // other point while processing. It's an error if so.
Eric Christopher12da1692012-03-22 01:33:51 +0000414 if (OpNo >= MI->getNumOperands() ||
Akira Hatanakafd82286e2012-05-08 19:14:42 +0000415 MI->getOperand(OpNo).isMetadata()) {
Chris Lattner1e158692010-04-04 18:34:07 +0000416 Error = true;
417 } else {
418 unsigned OpFlags = MI->getOperand(OpNo).getImm();
419 ++OpNo; // Skip over the ID number.
420
421 if (Modifier[0] == 'l') // labels are target independent
422 // FIXME: What if the operand isn't an MBB, report error?
423 OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
424 else {
Chris Lattnerd62adaa2010-04-07 05:27:36 +0000425 if (InlineAsm::isMemKind(OpFlags)) {
Chad Rosierdb20a412012-09-10 21:10:49 +0000426 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000427 Modifier[0] ? Modifier : nullptr,
Chris Lattner1e158692010-04-04 18:34:07 +0000428 OS);
429 } else {
Chad Rosierdb20a412012-09-10 21:10:49 +0000430 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
Craig Topper353eda42014-04-24 06:44:33 +0000431 Modifier[0] ? Modifier : nullptr, OS);
Chris Lattner1e158692010-04-04 18:34:07 +0000432 }
433 }
434 }
435 if (Error) {
Alp Tokere69170a2014-06-26 22:52:05 +0000436 std::string msg;
437 raw_string_ostream Msg(msg);
Chris Lattner1e457892010-04-07 23:40:44 +0000438 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
439 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
Chris Lattner1e158692010-04-04 18:34:07 +0000440 }
441 }
442 break;
443 }
444 }
445 }
Chad Rosier17788312012-09-11 19:09:56 +0000446 OS << '\n' << (char)0; // null terminate string.
447}
448
449/// EmitInlineAsm - This method formats and emits the specified machine
450/// instruction that is an inline asm.
451void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
452 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
453
454 // Count the number of register definitions to find the asm string.
455 unsigned NumDefs = 0;
456 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
457 ++NumDefs)
458 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
459
460 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
461
462 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
463 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
464
465 // If this asmstr is empty, just print the #APP/#NOAPP markers.
466 // These are useful to see where empty asm's wound up.
467 if (AsmStr[0] == 0) {
Rafael Espindola0b694812014-01-16 16:28:37 +0000468 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
469 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chad Rosier17788312012-09-11 19:09:56 +0000470 return;
Chad Rosier7641f582012-09-10 21:36:05 +0000471 }
472
Chad Rosier17788312012-09-11 19:09:56 +0000473 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000474 // enabled, so we use emitRawComment.
475 OutStreamer.emitRawComment(MAI->getInlineAsmStart());
Chad Rosier17788312012-09-11 19:09:56 +0000476
477 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
478 // it.
479 unsigned LocCookie = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000480 const MDNode *LocMD = nullptr;
Chad Rosier17788312012-09-11 19:09:56 +0000481 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
482 if (MI->getOperand(i-1).isMetadata() &&
483 (LocMD = MI->getOperand(i-1).getMetadata()) &&
484 LocMD->getNumOperands() != 0) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000485 if (const ConstantInt *CI =
486 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
Chad Rosier17788312012-09-11 19:09:56 +0000487 LocCookie = CI->getZExtValue();
488 break;
489 }
490 }
491 }
492
493 // Emit the inline asm to a temporary string so we can emit it through
494 // EmitInlineAsm.
Alp Tokere69170a2014-06-26 22:52:05 +0000495 SmallString<256> StringData;
496 raw_svector_ostream OS(StringData);
Chad Rosier17788312012-09-11 19:09:56 +0000497
498 // The variant of the current asmprinter.
499 int AsmPrinterVariant = MAI->getAssemblerDialect();
500 InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
501 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
502 if (InlineAsmVariant == InlineAsm::AD_ATT)
503 EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
504 AP, LocCookie, OS);
505 else
506 EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
507
Chad Rosierf24ae7b2012-09-05 23:57:37 +0000508 EmitInlineAsm(OS.str(), LocMD, MI->getInlineAsmDialect());
Jim Grosbach00915352010-10-01 23:29:12 +0000509
Chris Lattner1e158692010-04-04 18:34:07 +0000510 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
Rafael Espindola0b694812014-01-16 16:28:37 +0000511 // enabled, so we use emitRawComment.
512 OutStreamer.emitRawComment(MAI->getInlineAsmEnd());
Chris Lattner1e158692010-04-04 18:34:07 +0000513}
514
515
516/// PrintSpecial - Print information related to the specified machine instr
517/// that is independent of the operand, and may be independent of the instr
518/// itself. This can be useful for portably encoding the comment character
519/// or other bits of target-specific knowledge into the asmstrings. The
520/// syntax used is ${:comment}. Targets can override this to add support
521/// for their own strange codes.
522void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
523 const char *Code) const {
Eric Christopher8b770652015-01-26 19:03:15 +0000524 const DataLayout *DL = TM.getDataLayout();
Chris Lattner1e158692010-04-04 18:34:07 +0000525 if (!strcmp(Code, "private")) {
Rafael Espindola58873562014-01-03 19:21:54 +0000526 OS << DL->getPrivateGlobalPrefix();
Chris Lattner1e158692010-04-04 18:34:07 +0000527 } else if (!strcmp(Code, "comment")) {
528 OS << MAI->getCommentString();
529 } else if (!strcmp(Code, "uid")) {
530 // Comparing the address of MI isn't sufficient, because machineinstrs may
531 // be allocated to the same address across functions.
Jim Grosbach00915352010-10-01 23:29:12 +0000532
Chris Lattner1e158692010-04-04 18:34:07 +0000533 // If this is a new LastFn instruction, bump the counter.
534 if (LastMI != MI || LastFn != getFunctionNumber()) {
535 ++Counter;
536 LastMI = MI;
537 LastFn = getFunctionNumber();
538 }
539 OS << Counter;
540 } else {
Alp Tokere69170a2014-06-26 22:52:05 +0000541 std::string msg;
542 raw_string_ostream Msg(msg);
Chris Lattner1e158692010-04-04 18:34:07 +0000543 Msg << "Unknown special formatter '" << Code
544 << "' for machine instr: " << *MI;
Chris Lattner2104b8d2010-04-07 22:58:41 +0000545 report_fatal_error(Msg.str());
Jim Grosbach00915352010-10-01 23:29:12 +0000546 }
Chris Lattner1e158692010-04-04 18:34:07 +0000547}
548
549/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
550/// instruction, using the specified assembler variant. Targets should
551/// override this to format as appropriate.
552bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chad Rosier1f57bcb2012-09-07 20:23:29 +0000553 unsigned AsmVariant, const char *ExtraCode,
554 raw_ostream &O) {
Jack Carterb2fd5f62012-06-21 17:14:46 +0000555 // Does this asm operand have a single letter operand modifier?
556 if (ExtraCode && ExtraCode[0]) {
557 if (ExtraCode[1] != 0) return true; // Unknown modifier.
558
559 const MachineOperand &MO = MI->getOperand(OpNo);
560 switch (ExtraCode[0]) {
561 default:
562 return true; // Unknown modifier.
563 case 'c': // Substitute immediate value without immediate syntax
Jack Carterc457f622012-06-21 21:37:54 +0000564 if (MO.getType() != MachineOperand::MO_Immediate)
Jack Carterb2fd5f62012-06-21 17:14:46 +0000565 return true;
566 O << MO.getImm();
567 return false;
Jack Carterc457f622012-06-21 21:37:54 +0000568 case 'n': // Negate the immediate constant.
569 if (MO.getType() != MachineOperand::MO_Immediate)
570 return true;
571 O << -MO.getImm();
572 return false;
Jack Carterb2fd5f62012-06-21 17:14:46 +0000573 }
574 }
Chris Lattner1e158692010-04-04 18:34:07 +0000575 return true;
576}
577
578bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
579 unsigned AsmVariant,
580 const char *ExtraCode, raw_ostream &O) {
581 // Target doesn't support this yet!
582 return true;
583}
584
Eric Christopher64d35be2015-02-19 19:52:25 +0000585void AsmPrinter::emitInlineAsmStart() const {}
Toma Tabacua23f13c2014-12-17 10:56:16 +0000586
Rafael Espindola65fd0a82014-01-24 15:47:54 +0000587void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
David Peixottoea2bcb92014-02-06 18:19:40 +0000588 const MCSubtargetInfo *EndInfo) const {}