LLVM  13.0.0git
AsmPrinterInlineAsm.cpp
Go to the documentation of this file.
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/Twine.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/InlineAsm.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/MC/MCAsmInfo.h"
29 #include "llvm/MC/MCStreamer.h"
31 #include "llvm/MC/MCSymbol.h"
34 #include "llvm/Support/SourceMgr.h"
38 using namespace llvm;
39 
40 #define DEBUG_TYPE "asm-printer"
41 
42 unsigned AsmPrinter::addInlineAsmDiagBuffer(StringRef AsmStr,
43  const MDNode *LocMDNode) const {
45  Context.initInlineSourceManager();
46  SourceMgr &SrcMgr = *Context.getInlineSourceManager();
47  std::vector<const MDNode *> &LocInfos = Context.getLocInfos();
48 
49  std::unique_ptr<MemoryBuffer> Buffer;
50  // The inline asm source manager will outlive AsmStr, so make a copy of the
51  // string for SourceMgr to own.
52  Buffer = MemoryBuffer::getMemBufferCopy(AsmStr, "<inline asm>");
53 
54  // Tell SrcMgr about this buffer, it takes ownership of the buffer.
55  unsigned BufNum = SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
56 
57  // Store LocMDNode in DiagInfo, using BufNum as an identifier.
58  if (LocMDNode) {
59  LocInfos.resize(BufNum);
60  LocInfos[BufNum - 1] = LocMDNode;
61  }
62 
63  return BufNum;
64 }
65 
66 
67 /// EmitInlineAsm - Emit a blob of inline asm to the output streamer.
68 void AsmPrinter::emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
69  const MCTargetOptions &MCOptions,
70  const MDNode *LocMDNode,
71  InlineAsm::AsmDialect Dialect) const {
72  assert(!Str.empty() && "Can't emit empty inline asm block");
73 
74  // Remember if the buffer is nul terminated or not so we can avoid a copy.
75  bool isNullTerminated = Str.back() == 0;
76  if (isNullTerminated)
77  Str = Str.substr(0, Str.size()-1);
78 
79  // If the output streamer does not have mature MC support or the integrated
80  // assembler has been disabled, just emit the blob textually.
81  // Otherwise parse the asm and emit it via MC support.
82  // This is useful in case the asm parser doesn't handle something but the
83  // system assembler does.
84  const MCAsmInfo *MCAI = TM.getMCAsmInfo();
85  assert(MCAI && "No MCAsmInfo");
86  if (!MCAI->useIntegratedAssembler() &&
87  !OutStreamer->isIntegratedAssemblerRequired()) {
89  OutStreamer->emitRawText(Str);
90  emitInlineAsmEnd(STI, nullptr);
91  return;
92  }
93 
94  unsigned BufNum = addInlineAsmDiagBuffer(Str, LocMDNode);
97 
98  std::unique_ptr<MCAsmParser> Parser(
100 
101  // Do not use assembler-level information for parsing inline assembly.
102  OutStreamer->setUseAssemblerInfoForParsing(false);
103 
104  // We create a new MCInstrInfo here since we might be at the module level
105  // and not have a MachineFunction to initialize the TargetInstrInfo from and
106  // we only need MCInstrInfo for asm parsing. We create one unconditionally
107  // because it's not subtarget dependent.
108  std::unique_ptr<MCInstrInfo> MII(TM.getTarget().createMCInstrInfo());
109  assert(MII && "Failed to create instruction info");
110  std::unique_ptr<MCTargetAsmParser> TAP(TM.getTarget().createMCAsmParser(
111  STI, *Parser, *MII, MCOptions));
112  if (!TAP)
113  report_fatal_error("Inline asm not supported by this streamer because"
114  " we don't have an asm parser for this target\n");
115  Parser->setAssemblerDialect(Dialect);
116  Parser->setTargetParser(*TAP.get());
117  // Enable lexing Masm binary and hex integer literals in intel inline
118  // assembly.
119  if (Dialect == InlineAsm::AD_Intel)
120  Parser->getLexer().setLexMasmIntegers(true);
121 
123  // Don't implicitly switch to the text section before the asm.
124  (void)Parser->Run(/*NoInitialTextSection*/ true,
125  /*NoFinalize*/ true);
126  emitInlineAsmEnd(STI, &TAP->getSTI());
127 }
128 
129 static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
130  MachineModuleInfo *MMI, AsmPrinter *AP,
131  unsigned LocCookie, raw_ostream &OS) {
132  // Switch to the inline assembly variant.
133  OS << "\t.intel_syntax\n\t";
134 
135  const char *LastEmitted = AsmStr; // One past the last character emitted.
136  unsigned NumOperands = MI->getNumOperands();
137 
138  while (*LastEmitted) {
139  switch (*LastEmitted) {
140  default: {
141  // Not a special case, emit the string section literally.
142  const char *LiteralEnd = LastEmitted+1;
143  while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
144  *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
145  ++LiteralEnd;
146 
147  OS.write(LastEmitted, LiteralEnd-LastEmitted);
148  LastEmitted = LiteralEnd;
149  break;
150  }
151  case '\n':
152  ++LastEmitted; // Consume newline character.
153  OS << '\n'; // Indent code with newline.
154  break;
155  case '$': {
156  ++LastEmitted; // Consume '$' character.
157  bool Done = true;
158 
159  // Handle escapes.
160  switch (*LastEmitted) {
161  default: Done = false; break;
162  case '$':
163  ++LastEmitted; // Consume second '$' character.
164  break;
165  }
166  if (Done) break;
167 
168  bool HasCurlyBraces = false;
169  if (*LastEmitted == '{') { // ${variable}
170  ++LastEmitted; // Consume '{' character.
171  HasCurlyBraces = true;
172  }
173 
174  // If we have ${:foo}, then this is not a real operand reference, it is a
175  // "magic" string reference, just like in .td files. Arrange to call
176  // PrintSpecial.
177  if (HasCurlyBraces && LastEmitted[0] == ':') {
178  ++LastEmitted;
179  const char *StrStart = LastEmitted;
180  const char *StrEnd = strchr(StrStart, '}');
181  if (!StrEnd)
182  report_fatal_error("Unterminated ${:foo} operand in inline asm"
183  " string: '" + Twine(AsmStr) + "'");
184 
185  std::string Val(StrStart, StrEnd);
186  AP->PrintSpecial(MI, OS, Val.c_str());
187  LastEmitted = StrEnd+1;
188  break;
189  }
190 
191  const char *IDStart = LastEmitted;
192  const char *IDEnd = IDStart;
193  while (isDigit(*IDEnd))
194  ++IDEnd;
195 
196  unsigned Val;
197  if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
198  report_fatal_error("Bad $ operand number in inline asm string: '" +
199  Twine(AsmStr) + "'");
200  LastEmitted = IDEnd;
201 
202  if (Val >= NumOperands-1)
203  report_fatal_error("Invalid $ operand number in inline asm string: '" +
204  Twine(AsmStr) + "'");
205 
206  char Modifier[2] = { 0, 0 };
207 
208  if (HasCurlyBraces) {
209  // If we have curly braces, check for a modifier character. This
210  // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
211  if (*LastEmitted == ':') {
212  ++LastEmitted; // Consume ':' character.
213  if (*LastEmitted == 0)
214  report_fatal_error("Bad ${:} expression in inline asm string: '" +
215  Twine(AsmStr) + "'");
216 
217  Modifier[0] = *LastEmitted;
218  ++LastEmitted; // Consume modifier character.
219  }
220 
221  if (*LastEmitted != '}')
222  report_fatal_error("Bad ${} expression in inline asm string: '" +
223  Twine(AsmStr) + "'");
224  ++LastEmitted; // Consume '}' character.
225  }
226 
227  // Okay, we finally have a value number. Ask the target to print this
228  // operand!
229  unsigned OpNo = InlineAsm::MIOp_FirstOperand;
230 
231  bool Error = false;
232 
233  // Scan to find the machine operand number for the operand.
234  for (; Val; --Val) {
235  if (OpNo >= MI->getNumOperands()) break;
236  unsigned OpFlags = MI->getOperand(OpNo).getImm();
237  OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
238  }
239 
240  // We may have a location metadata attached to the end of the
241  // instruction, and at no point should see metadata at any
242  // other point while processing. It's an error if so.
243  if (OpNo >= MI->getNumOperands() ||
244  MI->getOperand(OpNo).isMetadata()) {
245  Error = true;
246  } else {
247  unsigned OpFlags = MI->getOperand(OpNo).getImm();
248  ++OpNo; // Skip over the ID number.
249 
250  if (InlineAsm::isMemKind(OpFlags)) {
252  MI, OpNo, Modifier[0] ? Modifier : nullptr, OS);
253  } else {
254  Error = AP->PrintAsmOperand(MI, OpNo,
255  Modifier[0] ? Modifier : nullptr, OS);
256  }
257  }
258  if (Error) {
259  std::string msg;
260  raw_string_ostream Msg(msg);
261  Msg << "invalid operand in inline asm: '" << AsmStr << "'";
262  MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
263  }
264  break;
265  }
266  }
267  }
268  OS << "\n\t.att_syntax\n" << (char)0; // null terminate string.
269 }
270 
271 static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI,
272  MachineModuleInfo *MMI, int AsmPrinterVariant,
273  AsmPrinter *AP, unsigned LocCookie,
274  raw_ostream &OS) {
275  int CurVariant = -1; // The number of the {.|.|.} region we are in.
276  const char *LastEmitted = AsmStr; // One past the last character emitted.
277  unsigned NumOperands = MI->getNumOperands();
278 
279  OS << '\t';
280 
281  while (*LastEmitted) {
282  switch (*LastEmitted) {
283  default: {
284  // Not a special case, emit the string section literally.
285  const char *LiteralEnd = LastEmitted+1;
286  while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
287  *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
288  ++LiteralEnd;
289  if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
290  OS.write(LastEmitted, LiteralEnd-LastEmitted);
291  LastEmitted = LiteralEnd;
292  break;
293  }
294  case '\n':
295  ++LastEmitted; // Consume newline character.
296  OS << '\n'; // Indent code with newline.
297  break;
298  case '$': {
299  ++LastEmitted; // Consume '$' character.
300  bool Done = true;
301 
302  // Handle escapes.
303  switch (*LastEmitted) {
304  default: Done = false; break;
305  case '$': // $$ -> $
306  if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
307  OS << '$';
308  ++LastEmitted; // Consume second '$' character.
309  break;
310  case '(': // $( -> same as GCC's { character.
311  ++LastEmitted; // Consume '(' character.
312  if (CurVariant != -1)
313  report_fatal_error("Nested variants found in inline asm string: '" +
314  Twine(AsmStr) + "'");
315  CurVariant = 0; // We're in the first variant now.
316  break;
317  case '|':
318  ++LastEmitted; // consume '|' character.
319  if (CurVariant == -1)
320  OS << '|'; // this is gcc's behavior for | outside a variant
321  else
322  ++CurVariant; // We're in the next variant.
323  break;
324  case ')': // $) -> same as GCC's } char.
325  ++LastEmitted; // consume ')' character.
326  if (CurVariant == -1)
327  OS << '}'; // this is gcc's behavior for } outside a variant
328  else
329  CurVariant = -1;
330  break;
331  }
332  if (Done) break;
333 
334  bool HasCurlyBraces = false;
335  if (*LastEmitted == '{') { // ${variable}
336  ++LastEmitted; // Consume '{' character.
337  HasCurlyBraces = true;
338  }
339 
340  // If we have ${:foo}, then this is not a real operand reference, it is a
341  // "magic" string reference, just like in .td files. Arrange to call
342  // PrintSpecial.
343  if (HasCurlyBraces && *LastEmitted == ':') {
344  ++LastEmitted;
345  const char *StrStart = LastEmitted;
346  const char *StrEnd = strchr(StrStart, '}');
347  if (!StrEnd)
348  report_fatal_error("Unterminated ${:foo} operand in inline asm"
349  " string: '" + Twine(AsmStr) + "'");
350 
351  std::string Val(StrStart, StrEnd);
352  AP->PrintSpecial(MI, OS, Val.c_str());
353  LastEmitted = StrEnd+1;
354  break;
355  }
356 
357  const char *IDStart = LastEmitted;
358  const char *IDEnd = IDStart;
359  while (isDigit(*IDEnd))
360  ++IDEnd;
361 
362  unsigned Val;
363  if (StringRef(IDStart, IDEnd-IDStart).getAsInteger(10, Val))
364  report_fatal_error("Bad $ operand number in inline asm string: '" +
365  Twine(AsmStr) + "'");
366  LastEmitted = IDEnd;
367 
368  char Modifier[2] = { 0, 0 };
369 
370  if (HasCurlyBraces) {
371  // If we have curly braces, check for a modifier character. This
372  // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
373  if (*LastEmitted == ':') {
374  ++LastEmitted; // Consume ':' character.
375  if (*LastEmitted == 0)
376  report_fatal_error("Bad ${:} expression in inline asm string: '" +
377  Twine(AsmStr) + "'");
378 
379  Modifier[0] = *LastEmitted;
380  ++LastEmitted; // Consume modifier character.
381  }
382 
383  if (*LastEmitted != '}')
384  report_fatal_error("Bad ${} expression in inline asm string: '" +
385  Twine(AsmStr) + "'");
386  ++LastEmitted; // Consume '}' character.
387  }
388 
389  if (Val >= NumOperands-1)
390  report_fatal_error("Invalid $ operand number in inline asm string: '" +
391  Twine(AsmStr) + "'");
392 
393  // Okay, we finally have a value number. Ask the target to print this
394  // operand!
395  if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
396  unsigned OpNo = InlineAsm::MIOp_FirstOperand;
397 
398  bool Error = false;
399 
400  // Scan to find the machine operand number for the operand.
401  for (; Val; --Val) {
402  if (OpNo >= MI->getNumOperands()) break;
403  unsigned OpFlags = MI->getOperand(OpNo).getImm();
404  OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
405  }
406 
407  // We may have a location metadata attached to the end of the
408  // instruction, and at no point should see metadata at any
409  // other point while processing. It's an error if so.
410  if (OpNo >= MI->getNumOperands() ||
411  MI->getOperand(OpNo).isMetadata()) {
412  Error = true;
413  } else {
414  unsigned OpFlags = MI->getOperand(OpNo).getImm();
415  ++OpNo; // Skip over the ID number.
416 
417  // FIXME: Shouldn't arch-independent output template handling go into
418  // PrintAsmOperand?
419  // Labels are target independent.
420  if (MI->getOperand(OpNo).isBlockAddress()) {
421  const BlockAddress *BA = MI->getOperand(OpNo).getBlockAddress();
422  MCSymbol *Sym = AP->GetBlockAddressSymbol(BA);
423  Sym->print(OS, AP->MAI);
425  } else if (MI->getOperand(OpNo).isMBB()) {
426  const MCSymbol *Sym = MI->getOperand(OpNo).getMBB()->getSymbol();
427  Sym->print(OS, AP->MAI);
428  } else if (Modifier[0] == 'l') {
429  Error = true;
430  } else if (InlineAsm::isMemKind(OpFlags)) {
432  MI, OpNo, Modifier[0] ? Modifier : nullptr, OS);
433  } else {
434  Error = AP->PrintAsmOperand(MI, OpNo,
435  Modifier[0] ? Modifier : nullptr, OS);
436  }
437  }
438  if (Error) {
439  std::string msg;
440  raw_string_ostream Msg(msg);
441  Msg << "invalid operand in inline asm: '" << AsmStr << "'";
442  MMI->getModule()->getContext().emitError(LocCookie, Msg.str());
443  }
444  }
445  break;
446  }
447  }
448  }
449  OS << '\n' << (char)0; // null terminate string.
450 }
451 
452 /// This method formats and emits the specified machine instruction that is an
453 /// inline asm.
454 void AsmPrinter::emitInlineAsm(const MachineInstr *MI) const {
455  assert(MI->isInlineAsm() && "printInlineAsm only works on inline asms");
456 
457  // Count the number of register definitions to find the asm string.
458  unsigned NumDefs = 0;
459  for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
460  ++NumDefs)
461  assert(NumDefs != MI->getNumOperands()-2 && "No asm string?");
462 
463  assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
464 
465  // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
466  const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
467 
468  // If this asmstr is empty, just print the #APP/#NOAPP markers.
469  // These are useful to see where empty asm's wound up.
470  if (AsmStr[0] == 0) {
471  OutStreamer->emitRawComment(MAI->getInlineAsmStart());
472  OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
473  return;
474  }
475 
476  // Emit the #APP start marker. This has to happen even if verbose-asm isn't
477  // enabled, so we use emitRawComment.
478  OutStreamer->emitRawComment(MAI->getInlineAsmStart());
479 
480  // Get the !srcloc metadata node if we have it, and decode the loc cookie from
481  // it.
482  unsigned LocCookie = 0;
483  const MDNode *LocMD = nullptr;
484  for (unsigned i = MI->getNumOperands(); i != 0; --i) {
485  if (MI->getOperand(i-1).isMetadata() &&
486  (LocMD = MI->getOperand(i-1).getMetadata()) &&
487  LocMD->getNumOperands() != 0) {
488  if (const ConstantInt *CI =
489  mdconst::dyn_extract<ConstantInt>(LocMD->getOperand(0))) {
490  LocCookie = CI->getZExtValue();
491  break;
492  }
493  }
494  }
495 
496  // Emit the inline asm to a temporary string so we can emit it through
497  // EmitInlineAsm.
498  SmallString<256> StringData;
499  raw_svector_ostream OS(StringData);
500 
501  // The variant of the current asmprinter.
502  int AsmPrinterVariant = MAI->getAssemblerDialect();
503  AsmPrinter *AP = const_cast<AsmPrinter*>(this);
504  if (MI->getInlineAsmDialect() == InlineAsm::AD_ATT)
505  EmitGCCInlineAsmStr(AsmStr, MI, MMI, AsmPrinterVariant, AP, LocCookie, OS);
506  else
507  EmitMSInlineAsmStr(AsmStr, MI, MMI, AP, LocCookie, OS);
508 
509  // Emit warnings if we use reserved registers on the clobber list, as
510  // that might lead to undefined behaviour.
511  SmallVector<Register, 8> RestrRegs;
513  // Start with the first operand descriptor, and iterate over them.
514  for (unsigned I = InlineAsm::MIOp_FirstOperand, NumOps = MI->getNumOperands();
515  I < NumOps; ++I) {
516  const MachineOperand &MO = MI->getOperand(I);
517  if (!MO.isImm())
518  continue;
519  unsigned Flags = MO.getImm();
521  Register Reg = MI->getOperand(I + 1).getReg();
522  if (!TRI->isAsmClobberable(*MF, Reg))
523  RestrRegs.push_back(Reg);
524  }
525  // Skip to one before the next operand descriptor, if it exists.
527  }
528 
529  if (!RestrRegs.empty()) {
530  unsigned BufNum = addInlineAsmDiagBuffer(OS.str(), LocMD);
533  SrcMgr.getMemoryBuffer(BufNum)->getBuffer().begin());
534 
535  std::string Msg = "inline asm clobber list contains reserved registers: ";
536  ListSeparator LS;
537  for (const Register &RR : RestrRegs) {
538  Msg += LS;
539  Msg += TRI->getName(RR);
540  }
541  const char *Note =
542  "Reserved registers on the clobber list may not be "
543  "preserved across the asm statement, and clobbering them may "
544  "lead to undefined behaviour.";
547  }
548 
549  emitInlineAsm(OS.str(), getSubtargetInfo(), TM.Options.MCOptions, LocMD,
550  MI->getInlineAsmDialect());
551 
552  // Emit the #NOAPP end marker. This has to happen even if verbose-asm isn't
553  // enabled, so we use emitRawComment.
554  OutStreamer->emitRawComment(MAI->getInlineAsmEnd());
555 }
556 
557 /// PrintSpecial - Print information related to the specified machine instr
558 /// that is independent of the operand, and may be independent of the instr
559 /// itself. This can be useful for portably encoding the comment character
560 /// or other bits of target-specific knowledge into the asmstrings. The
561 /// syntax used is ${:comment}. Targets can override this to add support
562 /// for their own strange codes.
564  const char *Code) const {
565  if (!strcmp(Code, "private")) {
566  const DataLayout &DL = MF->getDataLayout();
567  OS << DL.getPrivateGlobalPrefix();
568  } else if (!strcmp(Code, "comment")) {
569  OS << MAI->getCommentString();
570  } else if (!strcmp(Code, "uid")) {
571  // Comparing the address of MI isn't sufficient, because machineinstrs may
572  // be allocated to the same address across functions.
573 
574  // If this is a new LastFn instruction, bump the counter.
575  if (LastMI != MI || LastFn != getFunctionNumber()) {
576  ++Counter;
577  LastMI = MI;
578  LastFn = getFunctionNumber();
579  }
580  OS << Counter;
581  } else {
582  std::string msg;
583  raw_string_ostream Msg(msg);
584  Msg << "Unknown special formatter '" << Code
585  << "' for machine instr: " << *MI;
586  report_fatal_error(Msg.str());
587  }
588 }
589 
591  assert(MO.isGlobal() && "caller should check MO.isGlobal");
593  printOffset(MO.getOffset(), OS);
594 }
595 
596 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
597 /// instruction, using the specified assembler variant. Targets should
598 /// override this to format as appropriate for machine specific ExtraCodes
599 /// or when the arch-independent handling would be too complex otherwise.
600 bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
601  const char *ExtraCode, raw_ostream &O) {
602  // Does this asm operand have a single letter operand modifier?
603  if (ExtraCode && ExtraCode[0]) {
604  if (ExtraCode[1] != 0) return true; // Unknown modifier.
605 
606  // https://gcc.gnu.org/onlinedocs/gccint/Output-Template.html
607  const MachineOperand &MO = MI->getOperand(OpNo);
608  switch (ExtraCode[0]) {
609  default:
610  return true; // Unknown modifier.
611  case 'a': // Print as memory address.
612  if (MO.isReg()) {
613  PrintAsmMemoryOperand(MI, OpNo, nullptr, O);
614  return false;
615  }
616  LLVM_FALLTHROUGH; // GCC allows '%a' to behave like '%c' with immediates.
617  case 'c': // Substitute immediate value without immediate syntax
618  if (MO.isImm()) {
619  O << MO.getImm();
620  return false;
621  }
622  if (MO.isGlobal()) {
623  PrintSymbolOperand(MO, O);
624  return false;
625  }
626  return true;
627  case 'n': // Negate the immediate constant.
628  if (!MO.isImm())
629  return true;
630  O << -MO.getImm();
631  return false;
632  case 's': // The GCC deprecated s modifier
633  if (!MO.isImm())
634  return true;
635  O << ((32 - MO.getImm()) & 31);
636  return false;
637  }
638  }
639  return true;
640 }
641 
643  const char *ExtraCode, raw_ostream &O) {
644  // Target doesn't support this yet!
645  return true;
646 }
647 
649 
651  const MCSubtargetInfo *EndInfo) const {}
i
i
Definition: README.txt:29
MemoryBuffer.h
AsmPrinter.h
llvm::StringRef::back
LLVM_NODISCARD char back() const
back - Get the last character in the string.
Definition: StringRef.h:171
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:100
llvm
Definition: AllocatorList.h:23
llvm::MCAsmInfo::getCommentString
StringRef getCommentString() const
Definition: MCAsmInfo.h:622
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
llvm::MCRegisterInfo::getName
const char * getName(MCRegister RegNo) const
Return the human-readable symbolic target-specific name for the specified physical register.
Definition: MCRegisterInfo.h:485
llvm::InlineAsm::AsmDialect
AsmDialect
Definition: InlineAsm.h:33
llvm::AsmPrinter::printOffset
void printOffset(int64_t Offset, raw_ostream &OS) const
This is just convenient handler for printing offsets.
Definition: AsmPrinter.cpp:3032
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::StringRef::empty
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:156
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:112
llvm::InlineAsm::Kind_Clobber
@ Kind_Clobber
Definition: InlineAsm.h:234
llvm::MachineModuleInfo::getContext
const MCContext & getContext() const
Definition: MachineModuleInfo.h:167
llvm::MCAsmInfo::useIntegratedAssembler
bool useIntegratedAssembler() const
Return true if assembly (inline or otherwise) should be parsed.
Definition: MCAsmInfo.h:789
llvm::MachineOperand::getGlobal
const GlobalValue * getGlobal() const
Definition: MachineOperand.h:563
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:71
Note
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles Note
Definition: README.txt:239
llvm::AsmPrinter::emitInlineAsmEnd
virtual void emitInlineAsmEnd(const MCSubtargetInfo &StartInfo, const MCSubtargetInfo *EndInfo) const
Let the target do anything it needs to do after emitting inlineasm.
Definition: AsmPrinterInlineAsm.cpp:650
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:614
llvm::InlineAsm::AD_ATT
@ AD_ATT
Definition: InlineAsm.h:34
llvm::AsmPrinter::MAI
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:88
llvm::MCAsmInfo::getInlineAsmStart
const char * getInlineAsmStart() const
Definition: MCAsmInfo.h:644
llvm::MCAsmInfo
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
InlineAsm.h
ErrorHandling.h
llvm::TargetRegisterInfo::isAsmClobberable
virtual bool isAsmClobberable(const MachineFunction &MF, MCRegister PhysReg) const
Returns false if we can't guarantee that Physreg, specified as an IR asm clobber constraint,...
Definition: TargetRegisterInfo.h:533
llvm::InlineAsm::MIOp_FirstOperand
@ MIOp_FirstOperand
Definition: InlineAsm.h:218
MachineBasicBlock.h
llvm::TargetSubtargetInfo::getRegisterInfo
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
Definition: TargetSubtargetInfo.h:124
llvm::MCTargetOptions::IASSearchPaths
std::vector< std::string > IASSearchPaths
Additional paths to search for .include directives when using the integrated assembler.
Definition: MCTargetOptions.h:70
llvm::TargetRegisterInfo
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Definition: TargetRegisterInfo.h:231
llvm::AsmPrinter::PrintSymbolOperand
virtual void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS)
Print the MachineOperand as a symbol.
Definition: AsmPrinterInlineAsm.cpp:590
Module.h
TargetInstrInfo.h
llvm::SourceMgr::DK_Note
@ DK_Note
Definition: SourceMgr.h:37
llvm::MCAsmInfo::getAssemblerDialect
unsigned getAssemblerDialect() const
Definition: MCAsmInfo.h:649
llvm::MachineOperand::getOffset
int64_t getOffset() const
Return the offset from the symbol in this operand.
Definition: MachineOperand.h:600
TRI
unsigned const TargetRegisterInfo * TRI
Definition: MachineSink.cpp:1567
MCTargetAsmParser.h
llvm::Target::createMCInstrInfo
MCInstrInfo * createMCInstrInfo() const
createMCInstrInfo - Create a MCInstrInfo implementation.
Definition: TargetRegistry.h:355
llvm::MDNode::getNumOperands
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1108
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::StringRef::substr
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:612
llvm::MachineBasicBlock::getSymbol
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Definition: MachineBasicBlock.cpp:59
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:77
TargetMachine.h
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
Constants.h
SmallString.h
llvm::MachineOperand::getImm
int64_t getImm() const
Definition: MachineOperand.h:537
llvm::AsmPrinter::OutStreamer
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:97
llvm::AsmPrinter::PrintAsmMemoryOperand
virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant as...
Definition: AsmPrinterInlineAsm.cpp:642
Twine.h
llvm::SrcMgr
SourceMgr SrcMgr
Definition: Error.cpp:24
MCSymbol.h
llvm::TargetOptions::MCOptions
MCTargetOptions MCOptions
Machine level options.
Definition: TargetOptions.h:407
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:49
MCSubtargetInfo.h
llvm::MachineModuleInfo
This class contains meta information specific to a module.
Definition: MachineModuleInfo.h:78
llvm::raw_ostream::write
raw_ostream & write(unsigned char C)
Definition: raw_ostream.cpp:220
llvm::report_fatal_error
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
llvm::MCContext::registerInlineAsmLabel
void registerInlineAsmLabel(MCSymbol *Sym)
registerInlineAsmLabel - Records that the name is a label referenced in inline assembly.
Definition: MCContext.cpp:351
llvm::MemoryBuffer::getBuffer
StringRef getBuffer() const
Definition: MemoryBuffer.h:69
llvm::MCAsmInfo::getInlineAsmEnd
const char * getInlineAsmEnd() const
Definition: MCAsmInfo.h:645
llvm::AsmPrinter::PrintSpecial
virtual void PrintSpecial(const MachineInstr *MI, raw_ostream &OS, const char *Code) const
Print information related to the specified machine instr that is independent of the operand,...
Definition: AsmPrinterInlineAsm.cpp:563
llvm::MCSymbol::print
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:59
llvm::InlineAsm::getKind
static unsigned getKind(unsigned Flags)
Definition: InlineAsm.h:326
SourceMgr.h
llvm::InlineAsm::isMemKind
static bool isMemKind(unsigned Flag)
Definition: InlineAsm.h:278
llvm::SmallString< 256 >
llvm::SourceMgr::getMemoryBuffer
const MemoryBuffer * getMemoryBuffer(unsigned i) const
Definition: SourceMgr.h:122
llvm::Target::createMCAsmParser
MCTargetAsmParser * createMCAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser, const MCInstrInfo &MII, const MCTargetOptions &Options) const
createMCAsmParser - Create a target specific assembly parser.
Definition: TargetRegistry.h:426
llvm::MDNode::getOperand
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1102
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:558
llvm::LLVMContext::emitError
void emitError(unsigned LocCookie, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
Definition: LLVMContext.cpp:251
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:132
llvm::MachineOperand::isReg
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Definition: MachineOperand.h:321
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
llvm::SourceMgr::PrintMessage
void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges={}, ArrayRef< SMFixIt > FixIts={}, bool ShowColors=true) const
Emit a message about the specified location with the specified string.
Definition: SourceMgr.cpp:341
llvm::SourceMgr::DK_Warning
@ DK_Warning
Definition: SourceMgr.h:35
llvm::AsmPrinter::GetBlockAddressSymbol
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
Definition: AsmPrinter.cpp:3053
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::AsmPrinter::getSymbolPreferLocal
MCSymbol * getSymbolPreferLocal(const GlobalValue &GV) const
Similar to getSymbol() but preferred for references.
Definition: AsmPrinter.cpp:482
llvm::MCContext::getInlineSourceManager
SourceMgr * getInlineSourceManager()
Definition: MCContext.h:411
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::TargetMachine::Options
TargetOptions Options
Definition: TargetMachine.h:115
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::AsmPrinter::MF
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:100
llvm::AsmPrinter::OutContext
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:92
MachineModuleInfo.h
llvm::MDNode
Metadata node.
Definition: Metadata.h:897
llvm::MCTargetOptions
Definition: MCTargetOptions.h:36
isReg
static bool isReg(const MCInst &MI, unsigned OpNo)
Definition: MipsInstPrinter.cpp:31
llvm::BlockAddress
The address of a basic block.
Definition: Constants.h:847
llvm::TargetMachine::getMCAsmInfo
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
Definition: TargetMachine.h:202
isDigit
static bool isDigit(const char C)
Definition: RustDemangle.cpp:81
MCAsmInfo.h
llvm::InlineAsm::AD_Intel
@ AD_Intel
Definition: InlineAsm.h:35
DataLayout.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::SourceMgr
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
LLVM_FALLTHROUGH
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:281
llvm::SourceMgr::setIncludeDirs
void setIncludeDirs(const std::vector< std::string > &Dirs)
Definition: SourceMgr.h:103
llvm::Register
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
llvm::AsmPrinter::MMI
MachineModuleInfo * MMI
This is a pointer to the current MachineModuleInfo.
Definition: AsmPrinter.h:103
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
EmitGCCInlineAsmStr
static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI, MachineModuleInfo *MMI, int AsmPrinterVariant, AsmPrinter *AP, unsigned LocCookie, raw_ostream &OS)
Definition: AsmPrinterInlineAsm.cpp:271
llvm::AsmPrinter::getSubtargetInfo
const MCSubtargetInfo & getSubtargetInfo() const
Return information about subtarget.
Definition: AsmPrinter.cpp:237
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::AsmPrinter::emitInlineAsmStart
virtual void emitInlineAsmStart() const
Let the target do anything it needs to do before emitting inlineasm.
Definition: AsmPrinterInlineAsm.cpp:648
llvm::AArch64CC::LS
@ LS
Definition: AArch64BaseInfo.h:245
llvm::Module::getContext
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:261
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:82
llvm::SourceMgr::AddNewSourceBuffer
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:141
llvm::MachineOperand::isImm
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
Definition: MachineOperand.h:323
EmitMSInlineAsmStr
static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI, MachineModuleInfo *MMI, AsmPrinter *AP, unsigned LocCookie, raw_ostream &OS)
Definition: AsmPrinterInlineAsm.cpp:129
llvm::AsmPrinter::TM
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:85
SmallVector.h
llvm::InlineAsm::getNumOperandRegisters
static unsigned getNumOperandRegisters(unsigned Flag)
getNumOperandRegisters - Extract the number of registers field from the inline asm operand flag.
Definition: InlineAsm.h:337
MCStreamer.h
llvm::MachineFunction::getDataLayout
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
Definition: MachineFunction.cpp:260
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:647
llvm::createMCAsmParser
MCAsmParser * createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &, const MCAsmInfo &, unsigned CB=0)
Create an MCAsmParser instance for parsing assembly similar to gas syntax.
Definition: AsmParser.cpp:6211
llvm::SMLoc::getFromPointer
static SMLoc getFromPointer(const char *Ptr)
Definition: SMLoc.h:36
LLVMContext.h
llvm::TargetMachine::getTarget
const Target & getTarget() const
Definition: TargetMachine.h:121
raw_ostream.h
llvm::StringRef::size
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:160
MachineFunction.h
llvm::MemoryBuffer::getMemBufferCopy
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
Definition: MemoryBuffer.cpp:136
llvm::StringRef::begin
iterator begin() const
Definition: StringRef.h:131
TargetRegistry.h
llvm::raw_string_ostream::str
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
Definition: raw_ostream.h:632
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:75
llvm::AsmPrinter::getFunctionNumber
unsigned getFunctionNumber() const
Return a unique ID for the current function.
Definition: AsmPrinter.cpp:219
llvm::MachineModuleInfo::getModule
const Module * getModule() const
Definition: MachineModuleInfo.h:174
TargetRegisterInfo.h
llvm::AsmPrinter::PrintAsmOperand
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
Definition: AsmPrinterInlineAsm.cpp:600
llvm::MachineOperand::isGlobal
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
Definition: MachineOperand.h:339