1 //===-- AsmPrinterInlineAsm.cpp - AsmPrinter Inline Asm Handling ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the inline assembler pieces of the AsmPrinter class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/AsmPrinter.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineModuleInfo.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/InlineAsm.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/MC/MCSubtargetInfo.h"
28 #include "llvm/MC/MCSymbol.h"
29 #include "llvm/MC/MCTargetAsmParser.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/SourceMgr.h"
33 #include "llvm/Support/TargetRegistry.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Target/TargetInstrInfo.h"
36 #include "llvm/Target/TargetMachine.h"
37 #include "llvm/Target/TargetRegisterInfo.h"
38 #include "llvm/Target/TargetSubtargetInfo.h"
41 #define DEBUG_TYPE "asm-printer"
44 struct SrcMgrDiagInfo {
45 const MDNode *LocInfo;
46 LLVMContext::InlineAsmDiagHandlerTy DiagHandler;
51 /// srcMgrDiagHandler - This callback is invoked when the SourceMgr for an
52 /// inline asm has an error in it. diagInfo is a pointer to the SrcMgrDiagInfo
54 static void srcMgrDiagHandler(const SMDiagnostic &Diag, void *diagInfo) {
55 SrcMgrDiagInfo *DiagInfo = static_cast<SrcMgrDiagInfo *>(diagInfo);
56 assert(DiagInfo && "Diagnostic context not passed down?");
58 // If the inline asm had metadata associated with it, pull out a location
59 // cookie corresponding to which line the error occurred on.
60 unsigned LocCookie = 0;
61 if (const MDNode *LocInfo = DiagInfo->LocInfo) {
62 unsigned ErrorLine = Diag.getLineNo()-1;
63 if (ErrorLine >= LocInfo->getNumOperands())
66 if (LocInfo->getNumOperands() != 0)
67 if (const ConstantInt *CI =
68 mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
69 LocCookie = CI->getZExtValue();
72 DiagInfo->DiagHandler(Diag, DiagInfo->DiagContext, LocCookie);
75 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
76 void AsmPrinter::EmitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
77 const MDNode *LocMDNode,
78 InlineAsm::AsmDialect Dialect) const {
79 assert(!Str.empty() && "Can't emit empty inline asm block");
81 // Remember if the buffer is nul terminated or not so we can avoid a copy.
82 bool isNullTerminated = Str.back() == 0;
84 Str = Str.substr(0, Str.size()-1);
86 // If the output streamer does not have mature MC support or the integrated
87 // assembler has been disabled, just emit the blob textually.
88 // Otherwise parse the asm and emit it via MC support.
89 // This is useful in case the asm parser doesn't handle something but the
90 // system assembler does.
91 const MCAsmInfo *MCAI = TM.getMCAsmInfo();
92 assert(MCAI && "No MCAsmInfo");
93 if (!MCAI->useIntegratedAssembler() &&
94 !OutStreamer->isIntegratedAssemblerRequired()) {
96 OutStreamer->EmitRawText(Str);
97 emitInlineAsmEnd(STI, nullptr);
102 SrcMgrDiagInfo DiagInfo;
104 // If the current LLVMContext has an inline asm handler, set it in SourceMgr.
105 LLVMContext &LLVMCtx = MMI->getModule()->getContext();
106 bool HasDiagHandler = false;
107 if (LLVMCtx.getInlineAsmDiagnosticHandler() != nullptr) {
108 // If the source manager has an issue, we arrange for srcMgrDiagHandler
109 // to be invoked, getting DiagInfo passed into it.
110 DiagInfo.LocInfo = LocMDNode;
111 DiagInfo.DiagHandler = LLVMCtx.getInlineAsmDiagnosticHandler();
112 DiagInfo.DiagContext = LLVMCtx.getInlineAsmDiagnosticContext();
113 SrcMgr.setDiagHandler(srcMgrDiagHandler, &DiagInfo);
114 HasDiagHandler = true;
117 std::unique_ptr<MemoryBuffer> Buffer;
118 if (isNullTerminated)
119 Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
121 Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
123 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
124 SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
126 std::unique_ptr<MCAsmParser> Parser(
127 createMCAsmParser(SrcMgr, OutContext, *OutStreamer, *MAI));
129 // Create a temporary copy of the original STI because the parser may modify
130 // it. For example, when switching between arm and thumb mode. If the target
131 // needs to emit code to return to the original state it can do so in
132 // emitInlineAsmEnd().
133 MCSubtargetInfo TmpSTI = STI;
135 // We create a new MCInstrInfo here since we might be at the module level
136 // and not have a MachineFunction to initialize the TargetInstrInfo from and
137 // we only need MCInstrInfo for asm parsing. We create one unconditionally
138 // because it's not subtarget dependent.
139 std::unique_ptr<MCInstrInfo> MII(TM.getTarget().createMCInstrInfo());
140 std::unique_ptr<MCTargetAsmParser> TAP(TM.getTarget().createMCAsmParser(
141 TmpSTI, *Parser, *MII, TM.Options.MCOptions));
143 report_fatal_error("Inline asm not supported by this streamer because"
144 " we don't have an asm parser for this target\n");
145 Parser->setAssemblerDialect(Dialect);
146 Parser->setTargetParser(*TAP.get());
148 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
149 TAP->SetFrameRegister(TRI->getFrameRegister(*MF));
152 emitInlineAsmStart();
153 // Don't implicitly switch to the text section before the asm.
154 int Res = Parser->Run(/*NoInitialTextSection*/ true,
155 /*NoFinalize*/ true);
156 emitInlineAsmEnd(STI, &TmpSTI);
157 if (Res && !HasDiagHandler)
158 report_fatal_error("Error parsing inline asm\n");
161 static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
162 MachineModuleInfo *MMI, int InlineAsmVariant,
163 AsmPrinter *AP, unsigned LocCookie,
165 // Switch to the inline assembly variant.
166 OS << "\t.intel_syntax\n\t";
168 const char *LastEmitted = AsmStr; // One past the last character emitted.
169 unsigned NumOperands = MI->getNumOperands();
171 while (*LastEmitted) {
172 switch (*LastEmitted) {
174 // Not a special case, emit the string section literally.
175 const char *LiteralEnd = LastEmitted+1;
176 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
177 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
180 OS.write(LastEmitted, LiteralEnd-LastEmitted);
181 LastEmitted = LiteralEnd;
185 ++LastEmitted; // Consume newline character.
186 OS << '\n'; // Indent code with newline.
189 ++LastEmitted; // Consume '$' character.
193 switch (*LastEmitted) {
194 default: Done = false; break;
196 ++LastEmitted; // Consume second '$' character.
201 const char *IDStart = LastEmitted;
202 const char *IDEnd = IDStart;
203 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
206 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
207 report_fatal_error("Bad $ operand number in inline asm string: '" +
208 Twine(AsmStr) + "'");
211 if (Val >= NumOperands-1)
212 report_fatal_error("Invalid $ operand number in inline asm string: '" +
213 Twine(AsmStr) + "'");
215 // Okay, we finally have a value number. Ask the target to print this
217 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
221 // Scan to find the machine operand number for the operand.
223 if (OpNo >= MI->getNumOperands()) break;
224 unsigned OpFlags = MI->getOperand(OpNo).getImm();
225 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
228 // We may have a location metadata attached to the end of the
229 // instruction, and at no point should see metadata at any
230 // other point while processing. It's an error if so.
231 if (OpNo >= MI->getNumOperands() ||
232 MI->getOperand(OpNo).isMetadata()) {
235 unsigned OpFlags = MI->getOperand(OpNo).getImm();
236 ++OpNo; // Skip over the ID number.
238 if (InlineAsm::isMemKind(OpFlags)) {
239 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
240 /*Modifier*/ nullptr, OS);
242 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
243 /*Modifier*/ nullptr, OS);
248 raw_string_ostream Msg(msg);
249 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
250 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
256 OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
259 static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
260 MachineModuleInfo *MMI, int InlineAsmVariant,
261 int AsmPrinterVariant, AsmPrinter *AP,
262 unsigned LocCookie, raw_ostream &OS) {
263 int CurVariant = -1; // The number of the {.|.|.} region we are in.
264 const char *LastEmitted = AsmStr; // One past the last character emitted.
265 unsigned NumOperands = MI->getNumOperands();
269 while (*LastEmitted) {
270 switch (*LastEmitted) {
272 // Not a special case, emit the string section literally.
273 const char *LiteralEnd = LastEmitted+1;
274 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
275 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
277 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
278 OS.write(LastEmitted, LiteralEnd-LastEmitted);
279 LastEmitted = LiteralEnd;
283 ++LastEmitted; // Consume newline character.
284 OS << '\n'; // Indent code with newline.
287 ++LastEmitted; // Consume '$' character.
291 switch (*LastEmitted) {
292 default: Done = false; break;
294 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
296 ++LastEmitted; // Consume second '$' character.
298 case '(': // $( -> same as GCC's { character.
299 ++LastEmitted; // Consume '(' character.
300 if (CurVariant != -1)
301 report_fatal_error("Nested variants found in inline asm string: '" +
302 Twine(AsmStr) + "'");
303 CurVariant = 0; // We're in the first variant now.
306 ++LastEmitted; // consume '|' character.
307 if (CurVariant == -1)
308 OS << '|'; // this is gcc's behavior for | outside a variant
310 ++CurVariant; // We're in the next variant.
312 case ')': // $) -> same as GCC's } char.
313 ++LastEmitted; // consume ')' character.
314 if (CurVariant == -1)
315 OS << '}'; // this is gcc's behavior for } outside a variant
322 bool HasCurlyBraces = false;
323 if (*LastEmitted == '{') { // ${variable}
324 ++LastEmitted; // Consume '{' character.
325 HasCurlyBraces = true;
328 // If we have ${:foo}, then this is not a real operand reference, it is a
329 // "magic" string reference, just like in .td files. Arrange to call
331 if (HasCurlyBraces && *LastEmitted == ':') {
333 const char *StrStart = LastEmitted;
334 const char *StrEnd = strchr(StrStart, '}');
336 report_fatal_error("Unterminated ${:foo} operand in inline asm"
337 " string: '" + Twine(AsmStr) + "'");
339 std::string Val(StrStart, StrEnd);
340 AP->PrintSpecial(MI, OS, Val.c_str());
341 LastEmitted = StrEnd+1;
345 const char *IDStart = LastEmitted;
346 const char *IDEnd = IDStart;
347 while (*IDEnd >= '0' && *IDEnd <= '9') ++IDEnd;
350 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
351 report_fatal_error("Bad $ operand number in inline asm string: '" +
352 Twine(AsmStr) + "'");
355 char Modifier[2] = { 0, 0 };
357 if (HasCurlyBraces) {
358 // If we have curly braces, check for a modifier character. This
359 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
360 if (*LastEmitted == ':') {
361 ++LastEmitted; // Consume ':' character.
362 if (*LastEmitted == 0)
363 report_fatal_error("Bad ${:} expression in inline asm string: '" +
364 Twine(AsmStr) + "'");
366 Modifier[0] = *LastEmitted;
367 ++LastEmitted; // Consume modifier character.
370 if (*LastEmitted != '}')
371 report_fatal_error("Bad ${} expression in inline asm string: '" +
372 Twine(AsmStr) + "'");
373 ++LastEmitted; // Consume '}' character.
376 if (Val >= NumOperands-1)
377 report_fatal_error("Invalid $ operand number in inline asm string: '" +
378 Twine(AsmStr) + "'");
380 // Okay, we finally have a value number. Ask the target to print this
382 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
383 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
387 // Scan to find the machine operand number for the operand.
389 if (OpNo >= MI->getNumOperands()) break;
390 unsigned OpFlags = MI->getOperand(OpNo).getImm();
391 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
394 // We may have a location metadata attached to the end of the
395 // instruction, and at no point should see metadata at any
396 // other point while processing. It's an error if so.
397 if (OpNo >= MI->getNumOperands() ||
398 MI->getOperand(OpNo).isMetadata()) {
401 unsigned OpFlags = MI->getOperand(OpNo).getImm();
402 ++OpNo; // Skip over the ID number.
404 if (Modifier[0] == 'l') // labels are target independent
405 // FIXME: What if the operand isn't an MBB, report error?
406 OS << *MI->getOperand(OpNo).getMBB()->getSymbol();
408 if (InlineAsm::isMemKind(OpFlags)) {
409 Error = AP->PrintAsmMemoryOperand(MI, OpNo, InlineAsmVariant,
410 Modifier[0] ? Modifier : nullptr,
413 Error = AP->PrintAsmOperand(MI, OpNo, InlineAsmVariant,
414 Modifier[0] ? Modifier : nullptr, OS);
420 raw_string_ostream Msg(msg);
421 Msg << "invalid operand in inline asm: '" << AsmStr << "'";
422 MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
429 OS << '\n' << (char)0; // null terminate string.
432 /// EmitInlineAsm - This method formats and emits the specified machine
433 /// instruction that is an inline asm.
434 void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const {
435 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
437 // Count the number of register definitions to find the asm string.
438 unsigned NumDefs = 0;
439 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
441 assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
443 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
445 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
446 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
448 // If this asmstr is empty, just print the #APP/#NOAPP markers.
449 // These are useful to see where empty asm's wound up.
450 if (AsmStr[0] == 0) {
451 OutStreamer->emitRawComment(MAI->getInlineAsmStart());
452 OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
456 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
457 // enabled, so we use emitRawComment.
458 OutStreamer->emitRawComment(MAI->getInlineAsmStart());
460 // Get the !srcloc metadata node if we have it, and decode the loc cookie from
462 unsigned LocCookie = 0;
463 const MDNode *LocMD = nullptr;
464 for (unsigned i = MI->getNumOperands(); i != 0; --i) {
465 if (MI->getOperand(i-1).isMetadata() &&
466 (LocMD = MI->getOperand(i-1).getMetadata()) &&
467 LocMD->getNumOperands() != 0) {
468 if (const ConstantInt *CI =
469 mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
470 LocCookie = CI->getZExtValue();
476 // Emit the inline asm to a temporary string so we can emit it through
478 SmallString<256> StringData;
479 raw_svector_ostream OS(StringData);
481 // The variant of the current asmprinter.
482 int AsmPrinterVariant = MAI->getAssemblerDialect();
483 InlineAsm::AsmDialect InlineAsmVariant = MI->getInlineAsmDialect();
484 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
485 if (InlineAsmVariant == InlineAsm::AD_ATT)
486 EmitGCCInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AsmPrinterVariant,
489 EmitMSInlineAsmStr(AsmStr, MI, MMI, InlineAsmVariant, AP, LocCookie, OS);
491 EmitInlineAsm(OS.str(), getSubtargetInfo(), LocMD, MI->getInlineAsmDialect());
493 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
494 // enabled, so we use emitRawComment.
495 OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
499 /// PrintSpecial - Print information related to the specified machine instr
500 /// that is independent of the operand, and may be independent of the instr
501 /// itself. This can be useful for portably encoding the comment character
502 /// or other bits of target-specific knowledge into the asmstrings. The
503 /// syntax used is ${:comment}. Targets can override this to add support
504 /// for their own strange codes.
505 void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
506 const char *Code) const {
507 const DataLayout *DL = TM.getDataLayout();
508 if (!strcmp(Code, "private")) {
509 OS << DL->getPrivateGlobalPrefix();
510 } else if (!strcmp(Code, "comment")) {
511 OS << MAI->getCommentString();
512 } else if (!strcmp(Code, "uid")) {
513 // Comparing the address of MI isn't sufficient, because machineinstrs may
514 // be allocated to the same address across functions.
516 // If this is a new LastFn instruction, bump the counter.
517 if (LastMI != MI || LastFn != getFunctionNumber()) {
520 LastFn = getFunctionNumber();
525 raw_string_ostream Msg(msg);
526 Msg << "Unknown special formatter '" << Code
527 << "' for machine instr: " << *MI;
528 report_fatal_error(Msg.str());
532 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
533 /// instruction, using the specified assembler variant. Targets should
534 /// override this to format as appropriate.
535 bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
536 unsigned AsmVariant, const char *ExtraCode,
538 // Does this asm operand have a single letter operand modifier?
539 if (ExtraCode && ExtraCode[0]) {
540 if (ExtraCode[1] != 0) return true; // Unknown modifier.
542 const MachineOperand &MO = MI->getOperand(OpNo);
543 switch (ExtraCode[0]) {
545 return true; // Unknown modifier.
546 case 'c': // Substitute immediate value without immediate syntax
547 if (MO.getType() != MachineOperand::MO_Immediate)
551 case 'n': // Negate the immediate constant.
552 if (MO.getType() != MachineOperand::MO_Immediate)
561 bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
563 const char *ExtraCode, raw_ostream &O) {
564 // Target doesn't support this yet!
568 void AsmPrinter::emitInlineAsmStart() const {}
570 void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
571 const MCSubtargetInfo *EndInfo) const {}