1//===-- AsmPrinterInlineAsm.cpp - AsmPrinter Inline Asm Handling ----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the inline assembler pieces of the AsmPrinter class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/ADT/SmallString.h"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/ADT/StringExtras.h"
16#include "llvm/ADT/Twine.h"
17#include "llvm/CodeGen/AsmPrinter.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineModuleInfo.h"
21#include "llvm/CodeGen/TargetRegisterInfo.h"
22#include "llvm/CodeGen/TargetSubtargetInfo.h"
23#include "llvm/IR/Constants.h"
24#include "llvm/IR/DataLayout.h"
25#include "llvm/IR/DiagnosticInfo.h"
26#include "llvm/IR/InlineAsm.h"
27#include "llvm/IR/LLVMContext.h"
28#include "llvm/IR/Module.h"
29#include "llvm/MC/MCAsmInfo.h"
30#include "llvm/MC/MCInstrInfo.h"
31#include "llvm/MC/MCParser/AsmLexer.h"
32#include "llvm/MC/MCParser/MCTargetAsmParser.h"
33#include "llvm/MC/MCStreamer.h"
34#include "llvm/MC/MCSymbol.h"
35#include "llvm/MC/TargetRegistry.h"
36#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/IOSandbox.h"
38#include "llvm/Support/MemoryBuffer.h"
39#include "llvm/Support/SourceMgr.h"
40#include "llvm/Support/VirtualFileSystem.h"
41#include "llvm/Support/raw_ostream.h"
42#include "llvm/Target/TargetMachine.h"
43using namespace llvm;
44
45#define DEBUG_TYPE "asm-printer"
46
47unsigned AsmPrinter::addInlineAsmDiagBuffer(StringRef AsmStr,
48 const MDNode *LocMDNode) const {
49 MCContext &Context = MMI->getContext();
50 Context.initInlineSourceManager();
51 SourceMgr &SrcMgr = *Context.getInlineSourceManager();
52 std::vector<const MDNode *> &LocInfos = Context.getLocInfos();
53
54 std::unique_ptr<MemoryBuffer> Buffer;
55 // The inline asm source manager will outlive AsmStr, so make a copy of the
56 // string for SourceMgr to own.
57 Buffer = MemoryBuffer::getMemBufferCopy(InputData: AsmStr, BufferName: "<inline asm>");
58
59 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
60 unsigned BufNum = SrcMgr.AddNewSourceBuffer(F: std::move(Buffer), IncludeLoc: SMLoc());
61
62 // Store LocMDNode in DiagInfo, using BufNum as an identifier.
63 if (LocMDNode) {
64 LocInfos.resize(new_size: BufNum);
65 LocInfos[BufNum - 1] = LocMDNode;
66 }
67
68 return BufNum;
69}
70
71
72/// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
73void AsmPrinter::emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
74 const MCTargetOptions &MCOptions,
75 const MDNode *LocMDNode,
76 InlineAsm::AsmDialect Dialect,
77 const MachineInstr *MI) {
78 assert(!Str.empty() && "Can't emit empty inline asm block");
79
80 // 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(Start: 0, N: Str.size()-1);
84
85 // If the output streamer does not have mature MC support or the integrated
86 // assembler has been disabled or not required, just emit the blob textually.
87 // Otherwise parse the asm and emit it via MC support.
88 // This is useful in case the asm parser doesn't handle something but the
89 // system assembler does.
90 const MCAsmInfo &MCAI = TM.getMCAsmInfo();
91 if (!MCAI.useIntegratedAssembler() && !MCAI.parseInlineAsmUsingAsmParser() &&
92 !OutStreamer->isIntegratedAssemblerRequired()) {
93 emitInlineAsmStart();
94 OutStreamer->emitRawText(String: Str);
95 emitInlineAsmEnd(StartInfo: STI, EndInfo: nullptr, MI);
96 return;
97 }
98
99 unsigned BufNum = addInlineAsmDiagBuffer(AsmStr: Str, LocMDNode);
100 SourceMgr &SrcMgr = *MMI->getContext().getInlineSourceManager();
101 SrcMgr.setIncludeDirs(MCOptions.IASSearchPaths);
102 SrcMgr.setVirtualFileSystem([] {
103 // FIXME(sandboxing): Propagating vfs::FileSystem here is lots of work.
104 auto BypassSandbox = sys::sandbox::scopedDisable();
105 return vfs::getRealFileSystem();
106 }());
107
108 std::unique_ptr<MCAsmParser> Parser(
109 createMCAsmParser(SrcMgr, OutContext, *OutStreamer, MAI, CB: BufNum));
110
111 // We create a new MCInstrInfo here since we might be at the module level
112 // and not have a MachineFunction to initialize the TargetInstrInfo from and
113 // we only need MCInstrInfo for asm parsing. We create one unconditionally
114 // because it's not subtarget dependent.
115 std::unique_ptr<MCInstrInfo> MII(TM.getTarget().createMCInstrInfo());
116 assert(MII && "Failed to create instruction info");
117 std::unique_ptr<MCTargetAsmParser> TAP(
118 TM.getTarget().createMCAsmParser(STI, Parser&: *Parser, MII: *MII));
119 if (!TAP)
120 report_fatal_error(reason: "Inline asm not supported by this streamer because"
121 " we don't have an asm parser for this target\n");
122
123 // Respect inlineasm dialect on X86 targets only
124 if (TM.getTargetTriple().isX86()) {
125 Parser->setAssemblerDialect(Dialect);
126 // Enable lexing Masm binary and hex integer literals in intel inline
127 // assembly.
128 if (Dialect == InlineAsm::AD_Intel)
129 Parser->getLexer().setLexMasmIntegers(true);
130 }
131 Parser->setTargetParser(*TAP);
132
133 emitInlineAsmStart();
134 // Don't implicitly switch to the text section before the asm.
135 (void)Parser->Run(/*NoInitialTextSection*/ true,
136 /*NoFinalize*/ true);
137 emitInlineAsmEnd(StartInfo: STI, EndInfo: &TAP->getSTI(), MI);
138}
139
140static void EmitInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
141 MachineModuleInfo *MMI, const MCAsmInfo &MAI,
142 AsmPrinter *AP, uint64_t LocCookie,
143 raw_ostream &OS) {
144 bool InputIsIntelDialect = MI->getInlineAsmDialect() == InlineAsm::AD_Intel;
145
146 if (InputIsIntelDialect) {
147 // Switch to the inline assembly variant.
148 OS << "\t.intel_syntax\n\t";
149 }
150
151 int CurVariant = -1; // The number of the {.|.|.} region we are in.
152 const char *LastEmitted = AsmStr; // One past the last character emitted.
153 unsigned NumOperands = MI->getNumOperands();
154
155 int AsmPrinterVariant;
156 if (InputIsIntelDialect)
157 AsmPrinterVariant = 1; // X86MCAsmInfo.cpp's AsmWriterFlavorTy::Intel.
158 else
159 AsmPrinterVariant = MMI->getTarget().unqualifiedInlineAsmVariant();
160
161 // FIXME: Should this happen for `asm inteldialect` as well?
162 if (!InputIsIntelDialect && !MAI.isHLASM())
163 OS << '\t';
164
165 while (*LastEmitted) {
166 switch (*LastEmitted) {
167 default: {
168 // Not a special case, emit the string section literally.
169 const char *LiteralEnd = LastEmitted+1;
170 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
171 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
172 ++LiteralEnd;
173 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
174 OS.write(Ptr: LastEmitted, Size: LiteralEnd - LastEmitted);
175 LastEmitted = LiteralEnd;
176 break;
177 }
178 case '\n':
179 ++LastEmitted; // Consume newline character.
180 OS << '\n'; // Indent code with newline.
181 break;
182 case '$': {
183 ++LastEmitted; // Consume '$' character.
184 bool Done = true;
185
186 // Handle escapes.
187 switch (*LastEmitted) {
188 default: Done = false; break;
189 case '$': // $$ -> $
190 if (!InputIsIntelDialect)
191 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
192 OS << '$';
193 ++LastEmitted; // Consume second '$' character.
194 break;
195 case '(': // $( -> same as GCC's { character.
196 ++LastEmitted; // Consume '(' character.
197 if (CurVariant != -1)
198 report_fatal_error(reason: "Nested variants found in inline asm string: '" +
199 Twine(AsmStr) + "'");
200 CurVariant = 0; // We're in the first variant now.
201 break;
202 case '|':
203 ++LastEmitted; // Consume '|' character.
204 if (CurVariant == -1)
205 OS << '|'; // This is gcc's behavior for | outside a variant.
206 else
207 ++CurVariant; // We're in the next variant.
208 break;
209 case ')': // $) -> same as GCC's } char.
210 ++LastEmitted; // Consume ')' character.
211 if (CurVariant == -1)
212 OS << '}'; // This is gcc's behavior for } outside a variant.
213 else
214 CurVariant = -1;
215 break;
216 }
217 if (Done) break;
218
219 bool HasCurlyBraces = false;
220 if (*LastEmitted == '{') { // ${variable}
221 ++LastEmitted; // Consume '{' character.
222 HasCurlyBraces = true;
223 }
224
225 // If we have ${:foo}, then this is not a real operand reference, it is a
226 // "magic" string reference, just like in .td files. Arrange to call
227 // PrintSpecial.
228 if (HasCurlyBraces && *LastEmitted == ':') {
229 ++LastEmitted;
230 const char *StrStart = LastEmitted;
231 const char *StrEnd = strchr(s: StrStart, c: '}');
232 if (!StrEnd)
233 report_fatal_error(reason: "Unterminated ${:foo} operand in inline asm"
234 " string: '" + Twine(AsmStr) + "'");
235 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
236 AP->PrintSpecial(MI, OS, Code: StringRef(StrStart, StrEnd - StrStart));
237 LastEmitted = StrEnd+1;
238 break;
239 }
240
241 const char *IDStart = LastEmitted;
242 const char *IDEnd = IDStart;
243 while (isDigit(C: *IDEnd))
244 ++IDEnd;
245
246 unsigned Val;
247 if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(Radix: 10, Result&: Val))
248 report_fatal_error(reason: "Bad $ operand number in inline asm string: '" +
249 Twine(AsmStr) + "'");
250 LastEmitted = IDEnd;
251
252 if (Val >= NumOperands - 1)
253 report_fatal_error(reason: "Invalid $ operand number in inline asm string: '" +
254 Twine(AsmStr) + "'");
255
256 char Modifier[2] = { 0, 0 };
257
258 if (HasCurlyBraces) {
259 // If we have curly braces, check for a modifier character. This
260 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
261 if (*LastEmitted == ':') {
262 ++LastEmitted; // Consume ':' character.
263 if (*LastEmitted == 0)
264 report_fatal_error(reason: "Bad ${:} expression in inline asm string: '" +
265 Twine(AsmStr) + "'");
266
267 Modifier[0] = *LastEmitted;
268 ++LastEmitted; // Consume modifier character.
269 }
270
271 if (*LastEmitted != '}')
272 report_fatal_error(reason: "Bad ${} expression in inline asm string: '" +
273 Twine(AsmStr) + "'");
274 ++LastEmitted; // Consume '}' character.
275 }
276
277 // Okay, we finally have a value number. Ask the target to print this
278 // operand!
279 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
280 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
281
282 bool Error = false;
283
284 // Scan to find the machine operand number for the operand.
285 for (; Val; --Val) {
286 if (OpNo >= MI->getNumOperands())
287 break;
288 const InlineAsm::Flag F(MI->getOperand(i: OpNo).getImm());
289 OpNo += F.getNumOperandRegisters() + 1;
290 }
291
292 // We may have a location metadata attached to the end of the
293 // instruction, and at no point should see metadata at any
294 // other point while processing. It's an error if so.
295 if (OpNo >= MI->getNumOperands() || MI->getOperand(i: OpNo).isMetadata()) {
296 Error = true;
297 } else {
298 const InlineAsm::Flag F(MI->getOperand(i: OpNo).getImm());
299 ++OpNo; // Skip over the ID number.
300
301 // FIXME: Shouldn't arch-independent output template handling go into
302 // PrintAsmOperand?
303 // Labels are target independent.
304 if (MI->getOperand(i: OpNo).isBlockAddress()) {
305 const BlockAddress *BA = MI->getOperand(i: OpNo).getBlockAddress();
306 MCSymbol *Sym = AP->GetBlockAddressSymbol(BA);
307 Sym->print(OS, MAI: AP->MAI);
308 MMI->getContext().registerInlineAsmLabel(Sym);
309 } else if (MI->getOperand(i: OpNo).isMBB()) {
310 const MCSymbol *Sym = MI->getOperand(i: OpNo).getMBB()->getSymbol();
311 Sym->print(OS, MAI: AP->MAI);
312 } else if (F.isMemKind()) {
313 Error = AP->PrintAsmMemoryOperand(
314 MI, OpNo, ExtraCode: Modifier[0] ? Modifier : nullptr, OS);
315 } else {
316 Error = AP->PrintAsmOperand(MI, OpNo,
317 ExtraCode: Modifier[0] ? Modifier : nullptr, OS);
318 }
319 }
320 if (Error) {
321 const Function &Fn = MI->getMF()->getFunction();
322 Fn.getContext().diagnose(DI: DiagnosticInfoInlineAsm(
323 LocCookie,
324 "invalid operand in inline asm: '" + Twine(AsmStr) + "'"));
325 }
326 }
327 break;
328 }
329 }
330 }
331 if (InputIsIntelDialect)
332 OS << "\n\t.att_syntax";
333 OS << '\n' << (char)0; // null terminate string.
334}
335
336/// This method formats and emits the specified machine instruction that is an
337/// inline asm.
338void AsmPrinter::emitInlineAsm(const MachineInstr *MI) {
339 assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
340
341 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
342 const char *AsmStr = MI->getOperand(i: 0).getSymbolName();
343
344 // If this asmstr is empty, just print the #APP/#NOAPP markers.
345 // These are useful to see where empty asm's wound up.
346 if (AsmStr[0] == 0) {
347 OutStreamer->emitRawComment(T: MAI.getInlineAsmStart());
348 OutStreamer->emitRawComment(T: MAI.getInlineAsmEnd());
349 return;
350 }
351
352 // Emit the #APP start marker. This has to happen even if verbose-asm isn't
353 // enabled, so we use emitRawComment.
354 OutStreamer->emitRawComment(T: MAI.getInlineAsmStart());
355
356 const MDNode *LocMD = MI->getLocCookieMD();
357 uint64_t LocCookie =
358 LocMD
359 ? mdconst::extract<ConstantInt>(MD: LocMD->getOperand(I: 0))->getZExtValue()
360 : 0;
361
362 // Emit the inline asm to a temporary string so we can emit it through
363 // EmitInlineAsm.
364 SmallString<256> StringData;
365 raw_svector_ostream OS(StringData);
366
367 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
368 EmitInlineAsmStr(AsmStr, MI, MMI, MAI, AP, LocCookie, OS);
369
370 // Emit warnings if we use reserved registers on the clobber list, as
371 // that might lead to undefined behaviour.
372 SmallVector<Register, 8> RestrRegs;
373 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
374 // Start with the first operand descriptor, and iterate over them.
375 for (unsigned I = InlineAsm::MIOp_FirstOperand, NumOps = MI->getNumOperands();
376 I < NumOps; ++I) {
377 const MachineOperand &MO = MI->getOperand(i: I);
378 if (!MO.isImm())
379 continue;
380 const InlineAsm::Flag F(MO.getImm());
381 if (F.isClobberKind()) {
382 Register Reg = MI->getOperand(i: I + 1).getReg();
383 if (!TRI->isAsmClobberable(MF: *MF, PhysReg: Reg))
384 RestrRegs.push_back(Elt: Reg);
385 }
386 // Skip to one before the next operand descriptor, if it exists.
387 I += F.getNumOperandRegisters();
388 }
389
390 if (!RestrRegs.empty()) {
391 std::string Msg = "inline asm clobber list contains reserved registers: ";
392 ListSeparator LS;
393 for (const Register RR : RestrRegs) {
394 Msg += LS;
395 Msg += TRI->getRegAsmName(Reg: RR);
396 }
397
398 const Function &Fn = MF->getFunction();
399 const char *Note =
400 "Reserved registers on the clobber list may not be "
401 "preserved across the asm statement, and clobbering them may "
402 "lead to undefined behaviour.";
403 LLVMContext &Ctx = Fn.getContext();
404 Ctx.diagnose(DI: DiagnosticInfoInlineAsm(LocCookie, Msg,
405 DiagnosticSeverity::DS_Warning));
406 Ctx.diagnose(
407 DI: DiagnosticInfoInlineAsm(LocCookie, Note, DiagnosticSeverity::DS_Note));
408
409 for (const Register RR : RestrRegs) {
410 if (std::optional<std::string> reason =
411 TRI->explainReservedReg(MF: *MF, PhysReg: RR)) {
412 Ctx.diagnose(DI: DiagnosticInfoInlineAsm(LocCookie, *reason,
413 DiagnosticSeverity::DS_Note));
414 }
415 }
416 }
417
418 emitInlineAsm(Str: StringData, STI: getSubtargetInfo(), MCOptions: TM.Options.MCOptions, LocMDNode: LocMD,
419 Dialect: MI->getInlineAsmDialect(), MI);
420
421 // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
422 // enabled, so we use emitRawComment.
423 OutStreamer->emitRawComment(T: MAI.getInlineAsmEnd());
424}
425
426/// PrintSpecial - Print information related to the specified machine instr
427/// that is independent of the operand, and may be independent of the instr
428/// itself. This can be useful for portably encoding the comment character
429/// or other bits of target-specific knowledge into the asmstrings. The
430/// syntax used is ${:comment}. Targets can override this to add support
431/// for their own strange codes.
432void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
433 StringRef Code) const {
434 if (Code == "private") {
435 const DataLayout &DL = MF->getDataLayout();
436 OS << DL.getInternalSymbolPrefix();
437 } else if (Code == "comment") {
438 OS << MAI.getCommentString();
439 } else if (Code == "uid") {
440 // Comparing the address of MI isn't sufficient, because machineinstrs may
441 // be allocated to the same address across functions.
442
443 // If this is a new LastFn instruction, bump the counter.
444 if (LastMI != MI || LastFn != getFunctionNumber()) {
445 ++Counter;
446 LastMI = MI;
447 LastFn = getFunctionNumber();
448 }
449 OS << Counter;
450 } else {
451 std::string msg;
452 raw_string_ostream Msg(msg);
453 Msg << "Unknown special formatter '" << Code
454 << "' for machine instr: " << *MI;
455 report_fatal_error(reason: Twine(Msg.str()));
456 }
457}
458
459void AsmPrinter::PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS) {
460 assert(MO.isGlobal() && "caller should check MO.isGlobal");
461 getSymbolPreferLocal(GV: *MO.getGlobal())->print(OS, MAI);
462 printOffset(Offset: MO.getOffset(), OS);
463}
464
465/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
466/// instruction, using the specified assembler variant. Targets should
467/// override this to format as appropriate for machine specific ExtraCodes
468/// or when the arch-independent handling would be too complex otherwise.
469bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
470 const char *ExtraCode, raw_ostream &O) {
471 // Does this asm operand have a single letter operand modifier?
472 if (ExtraCode && ExtraCode[0]) {
473 if (ExtraCode[1] != 0) return true; // Unknown modifier.
474
475 // https://gcc.gnu.org/onlinedocs/gccint/Output-Template.html
476 const MachineOperand &MO = MI->getOperand(i: OpNo);
477 switch (ExtraCode[0]) {
478 default:
479 return true; // Unknown modifier.
480 case 'a': // Print as memory address.
481 if (MO.isReg()) {
482 PrintAsmMemoryOperand(MI, OpNo, ExtraCode: nullptr, OS&: O);
483 return false;
484 }
485 [[fallthrough]]; // GCC allows '%a' to behave like '%c' with immediates.
486 case 'c': // Substitute immediate value without immediate syntax
487 if (MO.isImm()) {
488 O << MO.getImm();
489 return false;
490 }
491 if (MO.isGlobal()) {
492 PrintSymbolOperand(MO, OS&: O);
493 return false;
494 }
495 return true;
496 case 'n': // Negate the immediate constant.
497 if (!MO.isImm())
498 return true;
499 O << -MO.getImm();
500 return false;
501 case 's': // The GCC deprecated s modifier
502 if (!MO.isImm())
503 return true;
504 O << ((32 - MO.getImm()) & 31);
505 return false;
506 }
507 }
508 return true;
509}
510
511bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
512 const char *ExtraCode, raw_ostream &O) {
513 // Target doesn't support this yet!
514 return true;
515}
516
517void AsmPrinter::emitInlineAsmStart() const {}
518
519void AsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
520 const MCSubtargetInfo *EndInfo,
521 const MachineInstr *MI) {}
522