LLVM  7.0.0svn
MCAsmStreamer.cpp
Go to the documentation of this file.
1 //===- lib/MC/MCAsmStreamer.cpp - Text Assembly Output ----------*- C++ -*-===//
2 //
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 
10 #include "llvm/ADT/Optional.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/MC/MCAsmBackend.h"
16 #include "llvm/MC/MCAsmInfo.h"
17 #include "llvm/MC/MCCodeEmitter.h"
18 #include "llvm/MC/MCCodeView.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCInstPrinter.h"
25 #include "llvm/MC/MCRegisterInfo.h"
26 #include "llvm/MC/MCSectionMachO.h"
27 #include "llvm/MC/MCStreamer.h"
29 #include "llvm/Support/Format.h"
31 #include "llvm/Support/LEB128.h"
33 #include "llvm/Support/Path.h"
34 #include <cctype>
35 
36 using namespace llvm;
37 
38 namespace {
39 
40 class MCAsmStreamer final : public MCStreamer {
41  std::unique_ptr<formatted_raw_ostream> OSOwner;
43  const MCAsmInfo *MAI;
44  std::unique_ptr<MCInstPrinter> InstPrinter;
45  std::unique_ptr<MCCodeEmitter> Emitter;
46  std::unique_ptr<MCAsmBackend> AsmBackend;
47 
48  SmallString<128> ExplicitCommentToEmit;
49  SmallString<128> CommentToEmit;
50  raw_svector_ostream CommentStream;
51 
52  unsigned IsVerboseAsm : 1;
53  unsigned ShowInst : 1;
54  unsigned UseDwarfDirectory : 1;
55 
56  void EmitRegisterName(int64_t Register);
57  void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
58  void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
59 
60 public:
61  MCAsmStreamer(MCContext &Context, std::unique_ptr<formatted_raw_ostream> os,
62  bool isVerboseAsm, bool useDwarfDirectory,
63  MCInstPrinter *printer, MCCodeEmitter *emitter,
64  MCAsmBackend *asmbackend, bool showInst)
65  : MCStreamer(Context), OSOwner(std::move(os)), OS(*OSOwner),
66  MAI(Context.getAsmInfo()), InstPrinter(printer), Emitter(emitter),
67  AsmBackend(asmbackend), CommentStream(CommentToEmit),
68  IsVerboseAsm(isVerboseAsm), ShowInst(showInst),
69  UseDwarfDirectory(useDwarfDirectory) {
70  assert(InstPrinter);
71  if (IsVerboseAsm)
72  InstPrinter->setCommentStream(CommentStream);
73  }
74 
75  inline void EmitEOL() {
76  // Dump Explicit Comments here.
77  emitExplicitComments();
78  // If we don't have any comments, just emit a \n.
79  if (!IsVerboseAsm) {
80  OS << '\n';
81  return;
82  }
83  EmitCommentsAndEOL();
84  }
85 
86  void EmitSyntaxDirective() override;
87 
88  void EmitCommentsAndEOL();
89 
90  /// isVerboseAsm - Return true if this streamer supports verbose assembly at
91  /// all.
92  bool isVerboseAsm() const override { return IsVerboseAsm; }
93 
94  /// hasRawTextSupport - We support EmitRawText.
95  bool hasRawTextSupport() const override { return true; }
96 
97  /// AddComment - Add a comment that can be emitted to the generated .s
98  /// file if applicable as a QoI issue to make the output of the compiler
99  /// more readable. This only affects the MCAsmStreamer, and only when
100  /// verbose assembly output is enabled.
101  void AddComment(const Twine &T, bool EOL = true) override;
102 
103  /// AddEncodingComment - Add a comment showing the encoding of an instruction.
104  /// If PrintSchedInfo - is true then the comment sched:[x:y] should
105  // be added to output if it's being supported by target
106  void AddEncodingComment(const MCInst &Inst, const MCSubtargetInfo &,
107  bool PrintSchedInfo);
108 
109  /// GetCommentOS - Return a raw_ostream that comments can be written to.
110  /// Unlike AddComment, you are required to terminate comments with \n if you
111  /// use this method.
112  raw_ostream &GetCommentOS() override {
113  if (!IsVerboseAsm)
114  return nulls(); // Discard comments unless in verbose asm mode.
115  return CommentStream;
116  }
117 
118  void emitRawComment(const Twine &T, bool TabPrefix = true) override;
119 
120  void addExplicitComment(const Twine &T) override;
121  void emitExplicitComments() override;
122 
123  /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
124  void AddBlankLine() override {
125  EmitEOL();
126  }
127 
128  /// @name MCStreamer Interface
129  /// @{
130 
131  void ChangeSection(MCSection *Section, const MCExpr *Subsection) override;
132 
133  void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override;
134  void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
135 
136  void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
137  void EmitLinkerOptions(ArrayRef<std::string> Options) override;
138  void EmitDataRegion(MCDataRegionType Kind) override;
139  void EmitVersionMin(MCVersionMinType Kind, unsigned Major, unsigned Minor,
140  unsigned Update) override;
141  void EmitBuildVersion(unsigned Platform, unsigned Major, unsigned Minor,
142  unsigned Update) override;
143  void EmitThumbFunc(MCSymbol *Func) override;
144 
145  void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
146  void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
147  bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
148 
149  void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
150  void BeginCOFFSymbolDef(const MCSymbol *Symbol) override;
151  void EmitCOFFSymbolStorageClass(int StorageClass) override;
152  void EmitCOFFSymbolType(int Type) override;
153  void EndCOFFSymbolDef() override;
154  void EmitCOFFSafeSEH(MCSymbol const *Symbol) override;
155  void EmitCOFFSymbolIndex(MCSymbol const *Symbol) override;
156  void EmitCOFFSectionIndex(MCSymbol const *Symbol) override;
157  void EmitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset) override;
158  void emitELFSize(MCSymbol *Symbol, const MCExpr *Value) override;
159  void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
160  unsigned ByteAlignment) override;
161 
162  /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
163  ///
164  /// @param Symbol - The common symbol to emit.
165  /// @param Size - The size of the common symbol.
166  /// @param ByteAlignment - The alignment of the common symbol in bytes.
167  void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
168  unsigned ByteAlignment) override;
169 
170  void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
171  uint64_t Size = 0, unsigned ByteAlignment = 0) override;
172 
173  void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
174  unsigned ByteAlignment = 0) override;
175 
176  void EmitBinaryData(StringRef Data) override;
177 
178  void EmitBytes(StringRef Data) override;
179 
180  void EmitValueImpl(const MCExpr *Value, unsigned Size,
181  SMLoc Loc = SMLoc()) override;
182  void EmitIntValue(uint64_t Value, unsigned Size) override;
183 
184  void EmitULEB128Value(const MCExpr *Value) override;
185 
186  void EmitSLEB128Value(const MCExpr *Value) override;
187 
188  void EmitDTPRel32Value(const MCExpr *Value) override;
189  void EmitDTPRel64Value(const MCExpr *Value) override;
190  void EmitTPRel32Value(const MCExpr *Value) override;
191  void EmitTPRel64Value(const MCExpr *Value) override;
192 
193  void EmitGPRel64Value(const MCExpr *Value) override;
194 
195  void EmitGPRel32Value(const MCExpr *Value) override;
196 
197  void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
198  SMLoc Loc = SMLoc()) override;
199 
200  void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
201  SMLoc Loc = SMLoc()) override;
202 
203  void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
204  unsigned ValueSize = 1,
205  unsigned MaxBytesToEmit = 0) override;
206 
207  void EmitCodeAlignment(unsigned ByteAlignment,
208  unsigned MaxBytesToEmit = 0) override;
209 
210  void emitValueToOffset(const MCExpr *Offset,
211  unsigned char Value,
212  SMLoc Loc) override;
213 
214  void EmitFileDirective(StringRef Filename) override;
215  Expected<unsigned> tryEmitDwarfFileDirective(unsigned FileNo,
216  StringRef Directory,
217  StringRef Filename,
218  MD5::MD5Result *Checksum = 0,
220  unsigned CUID = 0) override;
221  void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
222  unsigned Column, unsigned Flags,
223  unsigned Isa, unsigned Discriminator,
224  StringRef FileName) override;
225  MCSymbol *getDwarfLineTableSymbol(unsigned CUID) override;
226 
227  bool EmitCVFileDirective(unsigned FileNo, StringRef Filename,
228  ArrayRef<uint8_t> Checksum,
229  unsigned ChecksumKind) override;
230  bool EmitCVFuncIdDirective(unsigned FuncId) override;
231  bool EmitCVInlineSiteIdDirective(unsigned FunctionId, unsigned IAFunc,
232  unsigned IAFile, unsigned IALine,
233  unsigned IACol, SMLoc Loc) override;
234  void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line,
235  unsigned Column, bool PrologueEnd, bool IsStmt,
236  StringRef FileName, SMLoc Loc) override;
237  void EmitCVLinetableDirective(unsigned FunctionId, const MCSymbol *FnStart,
238  const MCSymbol *FnEnd) override;
239  void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
240  unsigned SourceFileId,
241  unsigned SourceLineNum,
242  const MCSymbol *FnStartSym,
243  const MCSymbol *FnEndSym) override;
244  void EmitCVDefRangeDirective(
245  ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
246  StringRef FixedSizePortion) override;
247  void EmitCVStringTableDirective() override;
248  void EmitCVFileChecksumsDirective() override;
249  void EmitCVFileChecksumOffsetDirective(unsigned FileNo) override;
250  void EmitCVFPOData(const MCSymbol *ProcSym, SMLoc L) override;
251 
252  void EmitIdent(StringRef IdentString) override;
253  void EmitCFISections(bool EH, bool Debug) override;
254  void EmitCFIDefCfa(int64_t Register, int64_t Offset) override;
255  void EmitCFIDefCfaOffset(int64_t Offset) override;
256  void EmitCFIDefCfaRegister(int64_t Register) override;
257  void EmitCFIOffset(int64_t Register, int64_t Offset) override;
258  void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding) override;
259  void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) override;
260  void EmitCFIRememberState() override;
261  void EmitCFIRestoreState() override;
262  void EmitCFIRestore(int64_t Register) override;
263  void EmitCFISameValue(int64_t Register) override;
264  void EmitCFIRelOffset(int64_t Register, int64_t Offset) override;
265  void EmitCFIAdjustCfaOffset(int64_t Adjustment) override;
266  void EmitCFIEscape(StringRef Values) override;
267  void EmitCFIGnuArgsSize(int64_t Size) override;
268  void EmitCFISignalFrame() override;
269  void EmitCFIUndefined(int64_t Register) override;
270  void EmitCFIRegister(int64_t Register1, int64_t Register2) override;
271  void EmitCFIWindowSave() override;
272  void EmitCFIReturnColumn(int64_t Register) override;
273 
274  void EmitWinCFIStartProc(const MCSymbol *Symbol, SMLoc Loc) override;
275  void EmitWinCFIEndProc(SMLoc Loc) override;
276  void EmitWinCFIStartChained(SMLoc Loc) override;
277  void EmitWinCFIEndChained(SMLoc Loc) override;
278  void EmitWinCFIPushReg(unsigned Register, SMLoc Loc) override;
279  void EmitWinCFISetFrame(unsigned Register, unsigned Offset,
280  SMLoc Loc) override;
281  void EmitWinCFIAllocStack(unsigned Size, SMLoc Loc) override;
282  void EmitWinCFISaveReg(unsigned Register, unsigned Offset,
283  SMLoc Loc) override;
284  void EmitWinCFISaveXMM(unsigned Register, unsigned Offset,
285  SMLoc Loc) override;
286  void EmitWinCFIPushFrame(bool Code, SMLoc Loc) override;
287  void EmitWinCFIEndProlog(SMLoc Loc) override;
288 
289  void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except,
290  SMLoc Loc) override;
291  void EmitWinEHHandlerData(SMLoc Loc) override;
292 
293  void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
294  bool PrintSchedInfo) override;
295 
296  void EmitBundleAlignMode(unsigned AlignPow2) override;
297  void EmitBundleLock(bool AlignToEnd) override;
298  void EmitBundleUnlock() override;
299 
300  bool EmitRelocDirective(const MCExpr &Offset, StringRef Name,
301  const MCExpr *Expr, SMLoc Loc) override;
302 
303  /// EmitRawText - If this file is backed by an assembly streamer, this dumps
304  /// the specified string in the output .s file. This capability is
305  /// indicated by the hasRawTextSupport() predicate.
306  void EmitRawTextImpl(StringRef String) override;
307 
308  void FinishImpl() override;
309 };
310 
311 } // end anonymous namespace.
312 
313 /// AddComment - Add a comment that can be emitted to the generated .s
314 /// file if applicable as a QoI issue to make the output of the compiler
315 /// more readable. This only affects the MCAsmStreamer, and only when
316 /// verbose assembly output is enabled.
317 /// By deafult EOL is set to true so that each comment goes on its own line.
318 void MCAsmStreamer::AddComment(const Twine &T, bool EOL) {
319  if (!IsVerboseAsm) return;
320 
321  T.toVector(CommentToEmit);
322 
323  if (EOL)
324  CommentToEmit.push_back('\n'); // Place comment in a new line.
325 }
326 
327 void MCAsmStreamer::EmitCommentsAndEOL() {
328  if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
329  OS << '\n';
330  return;
331  }
332 
333  StringRef Comments = CommentToEmit;
334 
335  assert(Comments.back() == '\n' &&
336  "Comment array not newline terminated");
337  do {
338  // Emit a line of comments.
339  OS.PadToColumn(MAI->getCommentColumn());
340  size_t Position = Comments.find('\n');
341  OS << MAI->getCommentString() << ' ' << Comments.substr(0, Position) <<'\n';
342 
343  Comments = Comments.substr(Position+1);
344  } while (!Comments.empty());
345 
346  CommentToEmit.clear();
347 }
348 
349 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
350  assert(Bytes > 0 && Bytes <= 8 && "Invalid size!");
351  return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
352 }
353 
354 void MCAsmStreamer::emitRawComment(const Twine &T, bool TabPrefix) {
355  if (TabPrefix)
356  OS << '\t';
357  OS << MAI->getCommentString() << T;
358  EmitEOL();
359 }
360 
361 void MCAsmStreamer::addExplicitComment(const Twine &T) {
363  if (c.equals(StringRef(MAI->getSeparatorString())))
364  return;
365  if (c.startswith(StringRef("//"))) {
366  ExplicitCommentToEmit.append("\t");
367  ExplicitCommentToEmit.append(MAI->getCommentString());
368  // drop //
369  ExplicitCommentToEmit.append(c.slice(2, c.size()).str());
370  } else if (c.startswith(StringRef("/*"))) {
371  size_t p = 2, len = c.size() - 2;
372  // emit each line in comment as separate newline.
373  do {
374  size_t newp = std::min(len, c.find_first_of("\r\n", p));
375  ExplicitCommentToEmit.append("\t");
376  ExplicitCommentToEmit.append(MAI->getCommentString());
377  ExplicitCommentToEmit.append(c.slice(p, newp).str());
378  // If we have another line in this comment add line
379  if (newp < len)
380  ExplicitCommentToEmit.append("\n");
381  p = newp + 1;
382  } while (p < len);
383  } else if (c.startswith(StringRef(MAI->getCommentString()))) {
384  ExplicitCommentToEmit.append("\t");
385  ExplicitCommentToEmit.append(c.str());
386  } else if (c.front() == '#') {
387 
388  ExplicitCommentToEmit.append("\t");
389  ExplicitCommentToEmit.append(MAI->getCommentString());
390  ExplicitCommentToEmit.append(c.slice(1, c.size()).str());
391  } else
392  assert(false && "Unexpected Assembly Comment");
393  // full line comments immediately output
394  if (c.back() == '\n')
395  emitExplicitComments();
396 }
397 
398 void MCAsmStreamer::emitExplicitComments() {
399  StringRef Comments = ExplicitCommentToEmit;
400  if (!Comments.empty())
401  OS << Comments;
402  ExplicitCommentToEmit.clear();
403 }
404 
405 void MCAsmStreamer::ChangeSection(MCSection *Section,
406  const MCExpr *Subsection) {
407  assert(Section && "Cannot switch to a null section!");
408  if (MCTargetStreamer *TS = getTargetStreamer()) {
409  TS->changeSection(getCurrentSectionOnly(), Section, Subsection, OS);
410  } else {
411  Section->PrintSwitchToSection(
412  *MAI, getContext().getObjectFileInfo()->getTargetTriple(), OS,
413  Subsection);
414  }
415 }
416 
417 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol, SMLoc Loc) {
418  MCStreamer::EmitLabel(Symbol, Loc);
419 
420  Symbol->print(OS, MAI);
421  OS << MAI->getLabelSuffix();
422 
423  EmitEOL();
424 }
425 
426 void MCAsmStreamer::EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {
427  StringRef str = MCLOHIdToName(Kind);
428 
429 #ifndef NDEBUG
430  int NbArgs = MCLOHIdToNbArgs(Kind);
431  assert(NbArgs != -1 && ((size_t)NbArgs) == Args.size() && "Malformed LOH!");
432  assert(str != "" && "Invalid LOH name");
433 #endif
434 
435  OS << "\t" << MCLOHDirectiveName() << " " << str << "\t";
436  bool IsFirst = true;
437  for (const MCSymbol *Arg : Args) {
438  if (!IsFirst)
439  OS << ", ";
440  IsFirst = false;
441  Arg->print(OS, MAI);
442  }
443  EmitEOL();
444 }
445 
446 void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
447  switch (Flag) {
448  case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
449  case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
450  case MCAF_Code16: OS << '\t'<< MAI->getCode16Directive();break;
451  case MCAF_Code32: OS << '\t'<< MAI->getCode32Directive();break;
452  case MCAF_Code64: OS << '\t'<< MAI->getCode64Directive();break;
453  }
454  EmitEOL();
455 }
456 
457 void MCAsmStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
458  assert(!Options.empty() && "At least one option is required!");
459  OS << "\t.linker_option \"" << Options[0] << '"';
460  for (ArrayRef<std::string>::iterator it = Options.begin() + 1,
461  ie = Options.end(); it != ie; ++it) {
462  OS << ", " << '"' << *it << '"';
463  }
464  EmitEOL();
465 }
466 
467 void MCAsmStreamer::EmitDataRegion(MCDataRegionType Kind) {
469  return;
470  switch (Kind) {
471  case MCDR_DataRegion: OS << "\t.data_region"; break;
472  case MCDR_DataRegionJT8: OS << "\t.data_region jt8"; break;
473  case MCDR_DataRegionJT16: OS << "\t.data_region jt16"; break;
474  case MCDR_DataRegionJT32: OS << "\t.data_region jt32"; break;
475  case MCDR_DataRegionEnd: OS << "\t.end_data_region"; break;
476  }
477  EmitEOL();
478 }
479 
481  switch (Type) {
482  case MCVM_WatchOSVersionMin: return ".watchos_version_min";
483  case MCVM_TvOSVersionMin: return ".tvos_version_min";
484  case MCVM_IOSVersionMin: return ".ios_version_min";
485  case MCVM_OSXVersionMin: return ".macosx_version_min";
486  }
487  llvm_unreachable("Invalid MC version min type");
488 }
489 
490 void MCAsmStreamer::EmitVersionMin(MCVersionMinType Type, unsigned Major,
491  unsigned Minor, unsigned Update) {
492  OS << '\t' << getVersionMinDirective(Type) << ' ' << Major << ", " << Minor;
493  if (Update)
494  OS << ", " << Update;
495  EmitEOL();
496 }
497 
498 static const char *getPlatformName(MachO::PlatformType Type) {
499  switch (Type) {
500  case MachO::PLATFORM_MACOS: return "macos";
501  case MachO::PLATFORM_IOS: return "ios";
502  case MachO::PLATFORM_TVOS: return "tvos";
503  case MachO::PLATFORM_WATCHOS: return "watchos";
504  case MachO::PLATFORM_BRIDGEOS: return "bridgeos";
505  }
506  llvm_unreachable("Invalid Mach-O platform type");
507 }
508 
509 void MCAsmStreamer::EmitBuildVersion(unsigned Platform, unsigned Major,
510  unsigned Minor, unsigned Update) {
511  const char *PlatformName = getPlatformName((MachO::PlatformType)Platform);
512  OS << "\t.build_version " << PlatformName << ", " << Major << ", " << Minor;
513  if (Update)
514  OS << ", " << Update;
515  EmitEOL();
516 }
517 
518 void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
519  // This needs to emit to a temporary string to get properly quoted
520  // MCSymbols when they have spaces in them.
521  OS << "\t.thumb_func";
522  // Only Mach-O hasSubsectionsViaSymbols()
523  if (MAI->hasSubsectionsViaSymbols()) {
524  OS << '\t';
525  Func->print(OS, MAI);
526  }
527  EmitEOL();
528 }
529 
530 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
531  Symbol->print(OS, MAI);
532  OS << " = ";
533  Value->print(OS, MAI);
534 
535  EmitEOL();
536 
537  MCStreamer::EmitAssignment(Symbol, Value);
538 }
539 
540 void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
541  OS << ".weakref ";
542  Alias->print(OS, MAI);
543  OS << ", ";
544  Symbol->print(OS, MAI);
545  EmitEOL();
546 }
547 
548 bool MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
550  switch (Attribute) {
551  case MCSA_Invalid: llvm_unreachable("Invalid symbol attribute");
552  case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
553  case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
554  case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
555  case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
556  case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
557  case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
558  case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
559  if (!MAI->hasDotTypeDotSizeDirective())
560  return false; // Symbol attribute not supported
561  OS << "\t.type\t";
562  Symbol->print(OS, MAI);
563  OS << ',' << ((MAI->getCommentString()[0] != '@') ? '@' : '%');
564  switch (Attribute) {
565  default: return false;
566  case MCSA_ELF_TypeFunction: OS << "function"; break;
567  case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
568  case MCSA_ELF_TypeObject: OS << "object"; break;
569  case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
570  case MCSA_ELF_TypeCommon: OS << "common"; break;
571  case MCSA_ELF_TypeNoType: OS << "notype"; break;
572  case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
573  }
574  EmitEOL();
575  return true;
576  case MCSA_Global: // .globl/.global
577  OS << MAI->getGlobalDirective();
578  break;
579  case MCSA_Hidden: OS << "\t.hidden\t"; break;
580  case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
581  case MCSA_Internal: OS << "\t.internal\t"; break;
582  case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
583  case MCSA_Local: OS << "\t.local\t"; break;
584  case MCSA_NoDeadStrip:
585  if (!MAI->hasNoDeadStrip())
586  return false;
587  OS << "\t.no_dead_strip\t";
588  break;
589  case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
590  case MCSA_AltEntry: OS << "\t.alt_entry\t"; break;
591  case MCSA_PrivateExtern:
592  OS << "\t.private_extern\t";
593  break;
594  case MCSA_Protected: OS << "\t.protected\t"; break;
595  case MCSA_Reference: OS << "\t.reference\t"; break;
596  case MCSA_Weak: OS << MAI->getWeakDirective(); break;
597  case MCSA_WeakDefinition:
598  OS << "\t.weak_definition\t";
599  break;
600  // .weak_reference
601  case MCSA_WeakReference: OS << MAI->getWeakRefDirective(); break;
602  case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
603  }
604 
605  Symbol->print(OS, MAI);
606  EmitEOL();
607 
608  return true;
609 }
610 
611 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
612  OS << ".desc" << ' ';
613  Symbol->print(OS, MAI);
614  OS << ',' << DescValue;
615  EmitEOL();
616 }
617 
618 void MCAsmStreamer::EmitSyntaxDirective() {
619  if (MAI->getAssemblerDialect() == 1) {
620  OS << "\t.intel_syntax noprefix";
621  EmitEOL();
622  }
623  // FIXME: Currently emit unprefix'ed registers.
624  // The intel_syntax directive has one optional argument
625  // with may have a value of prefix or noprefix.
626 }
627 
628 void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
629  OS << "\t.def\t ";
630  Symbol->print(OS, MAI);
631  OS << ';';
632  EmitEOL();
633 }
634 
635 void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
636  OS << "\t.scl\t" << StorageClass << ';';
637  EmitEOL();
638 }
639 
640 void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
641  OS << "\t.type\t" << Type << ';';
642  EmitEOL();
643 }
644 
645 void MCAsmStreamer::EndCOFFSymbolDef() {
646  OS << "\t.endef";
647  EmitEOL();
648 }
649 
650 void MCAsmStreamer::EmitCOFFSafeSEH(MCSymbol const *Symbol) {
651  OS << "\t.safeseh\t";
652  Symbol->print(OS, MAI);
653  EmitEOL();
654 }
655 
656 void MCAsmStreamer::EmitCOFFSymbolIndex(MCSymbol const *Symbol) {
657  OS << "\t.symidx\t";
658  Symbol->print(OS, MAI);
659  EmitEOL();
660 }
661 
662 void MCAsmStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
663  OS << "\t.secidx\t";
664  Symbol->print(OS, MAI);
665  EmitEOL();
666 }
667 
668 void MCAsmStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset) {
669  OS << "\t.secrel32\t";
670  Symbol->print(OS, MAI);
671  if (Offset != 0)
672  OS << '+' << Offset;
673  EmitEOL();
674 }
675 
676 void MCAsmStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
678  OS << "\t.size\t";
679  Symbol->print(OS, MAI);
680  OS << ", ";
681  Value->print(OS, MAI);
682  EmitEOL();
683 }
684 
685 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
686  unsigned ByteAlignment) {
687  OS << "\t.comm\t";
688  Symbol->print(OS, MAI);
689  OS << ',' << Size;
690 
691  if (ByteAlignment != 0) {
693  OS << ',' << ByteAlignment;
694  else
695  OS << ',' << Log2_32(ByteAlignment);
696  }
697  EmitEOL();
698 }
699 
700 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
701 ///
702 /// @param Symbol - The common symbol to emit.
703 /// @param Size - The size of the common symbol.
704 void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
705  unsigned ByteAlign) {
706  OS << "\t.lcomm\t";
707  Symbol->print(OS, MAI);
708  OS << ',' << Size;
709 
710  if (ByteAlign > 1) {
711  switch (MAI->getLCOMMDirectiveAlignmentType()) {
712  case LCOMM::NoAlignment:
713  llvm_unreachable("alignment not supported on .lcomm!");
715  OS << ',' << ByteAlign;
716  break;
718  assert(isPowerOf2_32(ByteAlign) && "alignment must be a power of 2");
719  OS << ',' << Log2_32(ByteAlign);
720  break;
721  }
722  }
723  EmitEOL();
724 }
725 
726 void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
727  uint64_t Size, unsigned ByteAlignment) {
728  if (Symbol)
729  AssignFragment(Symbol, &Section->getDummyFragment());
730 
731  // Note: a .zerofill directive does not switch sections.
732  OS << ".zerofill ";
733 
734  // This is a mach-o specific directive.
735  const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
736  OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
737 
738  if (Symbol) {
739  OS << ',';
740  Symbol->print(OS, MAI);
741  OS << ',' << Size;
742  if (ByteAlignment != 0)
743  OS << ',' << Log2_32(ByteAlignment);
744  }
745  EmitEOL();
746 }
747 
748 // .tbss sym, size, align
749 // This depends that the symbol has already been mangled from the original,
750 // e.g. _a.
751 void MCAsmStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
752  uint64_t Size, unsigned ByteAlignment) {
753  AssignFragment(Symbol, &Section->getDummyFragment());
754 
755  assert(Symbol && "Symbol shouldn't be NULL!");
756  // Instead of using the Section we'll just use the shortcut.
757  // This is a mach-o specific directive and section.
758  OS << ".tbss ";
759  Symbol->print(OS, MAI);
760  OS << ", " << Size;
761 
762  // Output align if we have it. We default to 1 so don't bother printing
763  // that.
764  if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
765 
766  EmitEOL();
767 }
768 
769 static inline char toOctal(int X) { return (X&7)+'0'; }
770 
771 static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
772  OS << '"';
773 
774  for (unsigned i = 0, e = Data.size(); i != e; ++i) {
775  unsigned char C = Data[i];
776  if (C == '"' || C == '\\') {
777  OS << '\\' << (char)C;
778  continue;
779  }
780 
781  if (isprint((unsigned char)C)) {
782  OS << (char)C;
783  continue;
784  }
785 
786  switch (C) {
787  case '\b': OS << "\\b"; break;
788  case '\f': OS << "\\f"; break;
789  case '\n': OS << "\\n"; break;
790  case '\r': OS << "\\r"; break;
791  case '\t': OS << "\\t"; break;
792  default:
793  OS << '\\';
794  OS << toOctal(C >> 6);
795  OS << toOctal(C >> 3);
796  OS << toOctal(C >> 0);
797  break;
798  }
799  }
800 
801  OS << '"';
802 }
803 
804 void MCAsmStreamer::EmitBytes(StringRef Data) {
805  assert(getCurrentSectionOnly() &&
806  "Cannot emit contents before setting section!");
807  if (Data.empty()) return;
808 
809  // If only single byte is provided or no ascii or asciz directives is
810  // supported, emit as vector of 8bits data.
811  if (Data.size() == 1 ||
812  !(MAI->getAscizDirective() || MAI->getAsciiDirective())) {
813  const char *Directive = MAI->getData8bitsDirective();
814  for (const unsigned char C : Data.bytes()) {
815  OS << Directive << (unsigned)C;
816  EmitEOL();
817  }
818  return;
819  }
820 
821  // If the data ends with 0 and the target supports .asciz, use it, otherwise
822  // use .ascii
823  if (MAI->getAscizDirective() && Data.back() == 0) {
824  OS << MAI->getAscizDirective();
825  Data = Data.substr(0, Data.size()-1);
826  } else {
827  OS << MAI->getAsciiDirective();
828  }
829 
830  PrintQuotedString(Data, OS);
831  EmitEOL();
832 }
833 
834 void MCAsmStreamer::EmitBinaryData(StringRef Data) {
835  // This is binary data. Print it in a grid of hex bytes for readability.
836  const size_t Cols = 4;
837  for (size_t I = 0, EI = alignTo(Data.size(), Cols); I < EI; I += Cols) {
838  size_t J = I, EJ = std::min(I + Cols, Data.size());
839  assert(EJ > 0);
840  OS << MAI->getData8bitsDirective();
841  for (; J < EJ - 1; ++J)
842  OS << format("0x%02x", uint8_t(Data[J])) << ", ";
843  OS << format("0x%02x", uint8_t(Data[J]));
844  EmitEOL();
845  }
846 }
847 
848 void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size) {
849  EmitValue(MCConstantExpr::create(Value, getContext()), Size);
850 }
851 
852 void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
853  SMLoc Loc) {
854  assert(Size <= 8 && "Invalid size");
855  assert(getCurrentSectionOnly() &&
856  "Cannot emit contents before setting section!");
857  const char *Directive = nullptr;
858  switch (Size) {
859  default: break;
860  case 1: Directive = MAI->getData8bitsDirective(); break;
861  case 2: Directive = MAI->getData16bitsDirective(); break;
862  case 4: Directive = MAI->getData32bitsDirective(); break;
863  case 8: Directive = MAI->getData64bitsDirective(); break;
864  }
865 
866  if (!Directive) {
867  int64_t IntValue;
868  if (!Value->evaluateAsAbsolute(IntValue))
869  report_fatal_error("Don't know how to emit this value.");
870 
871  // We couldn't handle the requested integer size so we fallback by breaking
872  // the request down into several, smaller, integers.
873  // Since sizes greater or equal to "Size" are invalid, we use the greatest
874  // power of 2 that is less than "Size" as our largest piece of granularity.
875  bool IsLittleEndian = MAI->isLittleEndian();
876  for (unsigned Emitted = 0; Emitted != Size;) {
877  unsigned Remaining = Size - Emitted;
878  // The size of our partial emission must be a power of two less than
879  // Size.
880  unsigned EmissionSize = PowerOf2Floor(std::min(Remaining, Size - 1));
881  // Calculate the byte offset of our partial emission taking into account
882  // the endianness of the target.
883  unsigned ByteOffset =
884  IsLittleEndian ? Emitted : (Remaining - EmissionSize);
885  uint64_t ValueToEmit = IntValue >> (ByteOffset * 8);
886  // We truncate our partial emission to fit within the bounds of the
887  // emission domain. This produces nicer output and silences potential
888  // truncation warnings when round tripping through another assembler.
889  uint64_t Shift = 64 - EmissionSize * 8;
890  assert(Shift < static_cast<uint64_t>(
891  std::numeric_limits<unsigned long long>::digits) &&
892  "undefined behavior");
893  ValueToEmit &= ~0ULL >> Shift;
894  EmitIntValue(ValueToEmit, EmissionSize);
895  Emitted += EmissionSize;
896  }
897  return;
898  }
899 
900  assert(Directive && "Invalid size for machine code value!");
901  OS << Directive;
902  if (MCTargetStreamer *TS = getTargetStreamer()) {
903  TS->emitValue(Value);
904  } else {
905  Value->print(OS, MAI);
906  EmitEOL();
907  }
908 }
909 
910 void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value) {
911  int64_t IntValue;
912  if (Value->evaluateAsAbsolute(IntValue)) {
913  EmitULEB128IntValue(IntValue);
914  return;
915  }
916  OS << "\t.uleb128 ";
917  Value->print(OS, MAI);
918  EmitEOL();
919 }
920 
921 void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value) {
922  int64_t IntValue;
923  if (Value->evaluateAsAbsolute(IntValue)) {
924  EmitSLEB128IntValue(IntValue);
925  return;
926  }
927  OS << "\t.sleb128 ";
928  Value->print(OS, MAI);
929  EmitEOL();
930 }
931 
932 void MCAsmStreamer::EmitDTPRel64Value(const MCExpr *Value) {
933  assert(MAI->getDTPRel64Directive() != nullptr);
934  OS << MAI->getDTPRel64Directive();
935  Value->print(OS, MAI);
936  EmitEOL();
937 }
938 
939 void MCAsmStreamer::EmitDTPRel32Value(const MCExpr *Value) {
940  assert(MAI->getDTPRel32Directive() != nullptr);
941  OS << MAI->getDTPRel32Directive();
942  Value->print(OS, MAI);
943  EmitEOL();
944 }
945 
946 void MCAsmStreamer::EmitTPRel64Value(const MCExpr *Value) {
947  assert(MAI->getTPRel64Directive() != nullptr);
948  OS << MAI->getTPRel64Directive();
949  Value->print(OS, MAI);
950  EmitEOL();
951 }
952 
953 void MCAsmStreamer::EmitTPRel32Value(const MCExpr *Value) {
954  assert(MAI->getTPRel32Directive() != nullptr);
955  OS << MAI->getTPRel32Directive();
956  Value->print(OS, MAI);
957  EmitEOL();
958 }
959 
960 void MCAsmStreamer::EmitGPRel64Value(const MCExpr *Value) {
961  assert(MAI->getGPRel64Directive() != nullptr);
962  OS << MAI->getGPRel64Directive();
963  Value->print(OS, MAI);
964  EmitEOL();
965 }
966 
967 void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
968  assert(MAI->getGPRel32Directive() != nullptr);
969  OS << MAI->getGPRel32Directive();
970  Value->print(OS, MAI);
971  EmitEOL();
972 }
973 
974 void MCAsmStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
975  SMLoc Loc) {
976  int64_t IntNumBytes;
977  if (NumBytes.evaluateAsAbsolute(IntNumBytes) && IntNumBytes == 0)
978  return;
979 
980  if (const char *ZeroDirective = MAI->getZeroDirective()) {
981  // FIXME: Emit location directives
982  OS << ZeroDirective;
983  NumBytes.print(OS, MAI);
984  if (FillValue != 0)
985  OS << ',' << (int)FillValue;
986  EmitEOL();
987  return;
988  }
989 
990  MCStreamer::emitFill(NumBytes, FillValue);
991 }
992 
993 void MCAsmStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
994  int64_t Expr, SMLoc Loc) {
995  // FIXME: Emit location directives
996  OS << "\t.fill\t";
997  NumValues.print(OS, MAI);
998  OS << ", " << Size << ", 0x";
999  OS.write_hex(truncateToSize(Expr, 4));
1000  EmitEOL();
1001 }
1002 
1003 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
1004  unsigned ValueSize,
1005  unsigned MaxBytesToEmit) {
1006  // Some assemblers don't support non-power of two alignments, so we always
1007  // emit alignments as a power of two if possible.
1008  if (isPowerOf2_32(ByteAlignment)) {
1009  switch (ValueSize) {
1010  default:
1011  llvm_unreachable("Invalid size for machine code value!");
1012  case 1:
1013  OS << "\t.p2align\t";
1014  break;
1015  case 2:
1016  OS << ".p2alignw ";
1017  break;
1018  case 4:
1019  OS << ".p2alignl ";
1020  break;
1021  case 8:
1022  llvm_unreachable("Unsupported alignment size!");
1023  }
1024 
1025  OS << Log2_32(ByteAlignment);
1026 
1027  if (Value || MaxBytesToEmit) {
1028  OS << ", 0x";
1029  OS.write_hex(truncateToSize(Value, ValueSize));
1030 
1031  if (MaxBytesToEmit)
1032  OS << ", " << MaxBytesToEmit;
1033  }
1034  EmitEOL();
1035  return;
1036  }
1037 
1038  // Non-power of two alignment. This is not widely supported by assemblers.
1039  // FIXME: Parameterize this based on MAI.
1040  switch (ValueSize) {
1041  default: llvm_unreachable("Invalid size for machine code value!");
1042  case 1: OS << ".balign"; break;
1043  case 2: OS << ".balignw"; break;
1044  case 4: OS << ".balignl"; break;
1045  case 8: llvm_unreachable("Unsupported alignment size!");
1046  }
1047 
1048  OS << ' ' << ByteAlignment;
1049  OS << ", " << truncateToSize(Value, ValueSize);
1050  if (MaxBytesToEmit)
1051  OS << ", " << MaxBytesToEmit;
1052  EmitEOL();
1053 }
1054 
1055 void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
1056  unsigned MaxBytesToEmit) {
1057  // Emit with a text fill value.
1058  EmitValueToAlignment(ByteAlignment, MAI->getTextAlignFillValue(),
1059  1, MaxBytesToEmit);
1060 }
1061 
1062 void MCAsmStreamer::emitValueToOffset(const MCExpr *Offset,
1063  unsigned char Value,
1064  SMLoc Loc) {
1065  // FIXME: Verify that Offset is associated with the current section.
1066  OS << ".org ";
1067  Offset->print(OS, MAI);
1068  OS << ", " << (unsigned)Value;
1069  EmitEOL();
1070 }
1071 
1072 void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
1074  OS << "\t.file\t";
1075  PrintQuotedString(Filename, OS);
1076  EmitEOL();
1077 }
1078 
1079 Expected<unsigned> MCAsmStreamer::tryEmitDwarfFileDirective(
1080  unsigned FileNo, StringRef Directory, StringRef Filename,
1081  MD5::MD5Result *Checksum, Optional<StringRef> Source, unsigned CUID) {
1082  assert(CUID == 0);
1083 
1084  MCDwarfLineTable &Table = getContext().getMCDwarfLineTable(CUID);
1085  unsigned NumFiles = Table.getMCDwarfFiles().size();
1086  Expected<unsigned> FileNoOrErr =
1087  Table.tryGetFile(Directory, Filename, Checksum, Source, FileNo);
1088  if (!FileNoOrErr)
1089  return FileNoOrErr.takeError();
1090  FileNo = FileNoOrErr.get();
1091  if (NumFiles == Table.getMCDwarfFiles().size())
1092  return FileNo;
1093 
1094  SmallString<128> FullPathName;
1095 
1096  if (!UseDwarfDirectory && !Directory.empty()) {
1097  if (sys::path::is_absolute(Filename))
1098  Directory = "";
1099  else {
1100  FullPathName = Directory;
1101  sys::path::append(FullPathName, Filename);
1102  Directory = "";
1103  Filename = FullPathName;
1104  }
1105  }
1106 
1107  SmallString<128> Str;
1108  raw_svector_ostream OS1(Str);
1109  OS1 << "\t.file\t" << FileNo << ' ';
1110  if (!Directory.empty()) {
1111  PrintQuotedString(Directory, OS1);
1112  OS1 << ' ';
1113  }
1114  PrintQuotedString(Filename, OS1);
1115  if (Checksum) {
1116  OS1 << " md5 ";
1117  PrintQuotedString(Checksum->digest(), OS1);
1118  }
1119  if (Source) {
1120  OS1 << " source ";
1121  PrintQuotedString(*Source, OS1);
1122  }
1123  if (MCTargetStreamer *TS = getTargetStreamer()) {
1124  TS->emitDwarfFileDirective(OS1.str());
1125  } else {
1126  EmitRawText(OS1.str());
1127  }
1128 
1129  return FileNo;
1130 }
1131 
1132 void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
1133  unsigned Column, unsigned Flags,
1134  unsigned Isa,
1135  unsigned Discriminator,
1136  StringRef FileName) {
1137  OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
1138  if (Flags & DWARF2_FLAG_BASIC_BLOCK)
1139  OS << " basic_block";
1140  if (Flags & DWARF2_FLAG_PROLOGUE_END)
1141  OS << " prologue_end";
1142  if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
1143  OS << " epilogue_begin";
1144 
1145  unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
1146  if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
1147  OS << " is_stmt ";
1148 
1149  if (Flags & DWARF2_FLAG_IS_STMT)
1150  OS << "1";
1151  else
1152  OS << "0";
1153  }
1154 
1155  if (Isa)
1156  OS << " isa " << Isa;
1157  if (Discriminator)
1158  OS << " discriminator " << Discriminator;
1159 
1160  if (IsVerboseAsm) {
1161  OS.PadToColumn(MAI->getCommentColumn());
1162  OS << MAI->getCommentString() << ' ' << FileName << ':'
1163  << Line << ':' << Column;
1164  }
1165  EmitEOL();
1166  this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
1167  Isa, Discriminator, FileName);
1168 }
1169 
1170 MCSymbol *MCAsmStreamer::getDwarfLineTableSymbol(unsigned CUID) {
1171  // Always use the zeroth line table, since asm syntax only supports one line
1172  // table for now.
1174 }
1175 
1176 bool MCAsmStreamer::EmitCVFileDirective(unsigned FileNo, StringRef Filename,
1177  ArrayRef<uint8_t> Checksum,
1178  unsigned ChecksumKind) {
1179  if (!getContext().getCVContext().addFile(*this, FileNo, Filename, Checksum,
1180  ChecksumKind))
1181  return false;
1182 
1183  OS << "\t.cv_file\t" << FileNo << ' ';
1184  PrintQuotedString(Filename, OS);
1185 
1186  if (!ChecksumKind) {
1187  EmitEOL();
1188  return true;
1189  }
1190 
1191  OS << ' ';
1192  PrintQuotedString(toHex(Checksum), OS);
1193  OS << ' ' << ChecksumKind;
1194 
1195  EmitEOL();
1196  return true;
1197 }
1198 
1199 bool MCAsmStreamer::EmitCVFuncIdDirective(unsigned FuncId) {
1200  OS << "\t.cv_func_id " << FuncId << '\n';
1201  return MCStreamer::EmitCVFuncIdDirective(FuncId);
1202 }
1203 
1204 bool MCAsmStreamer::EmitCVInlineSiteIdDirective(unsigned FunctionId,
1205  unsigned IAFunc,
1206  unsigned IAFile,
1207  unsigned IALine, unsigned IACol,
1208  SMLoc Loc) {
1209  OS << "\t.cv_inline_site_id " << FunctionId << " within " << IAFunc
1210  << " inlined_at " << IAFile << ' ' << IALine << ' ' << IACol << '\n';
1211  return MCStreamer::EmitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile,
1212  IALine, IACol, Loc);
1213 }
1214 
1215 void MCAsmStreamer::EmitCVLocDirective(unsigned FunctionId, unsigned FileNo,
1216  unsigned Line, unsigned Column,
1217  bool PrologueEnd, bool IsStmt,
1218  StringRef FileName, SMLoc Loc) {
1219  OS << "\t.cv_loc\t" << FunctionId << " " << FileNo << " " << Line << " "
1220  << Column;
1221  if (PrologueEnd)
1222  OS << " prologue_end";
1223 
1224  unsigned OldIsStmt = getContext().getCVContext().getCurrentCVLoc().isStmt();
1225  if (IsStmt != OldIsStmt) {
1226  OS << " is_stmt ";
1227 
1228  if (IsStmt)
1229  OS << "1";
1230  else
1231  OS << "0";
1232  }
1233 
1234  if (IsVerboseAsm) {
1235  OS.PadToColumn(MAI->getCommentColumn());
1236  OS << MAI->getCommentString() << ' ' << FileName << ':' << Line << ':'
1237  << Column;
1238  }
1239  EmitEOL();
1240  this->MCStreamer::EmitCVLocDirective(FunctionId, FileNo, Line, Column,
1241  PrologueEnd, IsStmt, FileName, Loc);
1242 }
1243 
1244 void MCAsmStreamer::EmitCVLinetableDirective(unsigned FunctionId,
1245  const MCSymbol *FnStart,
1246  const MCSymbol *FnEnd) {
1247  OS << "\t.cv_linetable\t" << FunctionId << ", ";
1248  FnStart->print(OS, MAI);
1249  OS << ", ";
1250  FnEnd->print(OS, MAI);
1251  EmitEOL();
1252  this->MCStreamer::EmitCVLinetableDirective(FunctionId, FnStart, FnEnd);
1253 }
1254 
1255 void MCAsmStreamer::EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
1256  unsigned SourceFileId,
1257  unsigned SourceLineNum,
1258  const MCSymbol *FnStartSym,
1259  const MCSymbol *FnEndSym) {
1260  OS << "\t.cv_inline_linetable\t" << PrimaryFunctionId << ' ' << SourceFileId
1261  << ' ' << SourceLineNum << ' ';
1262  FnStartSym->print(OS, MAI);
1263  OS << ' ';
1264  FnEndSym->print(OS, MAI);
1265  EmitEOL();
1267  PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym);
1268 }
1269 
1270 void MCAsmStreamer::EmitCVDefRangeDirective(
1271  ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
1272  StringRef FixedSizePortion) {
1273  OS << "\t.cv_def_range\t";
1274  for (std::pair<const MCSymbol *, const MCSymbol *> Range : Ranges) {
1275  OS << ' ';
1276  Range.first->print(OS, MAI);
1277  OS << ' ';
1278  Range.second->print(OS, MAI);
1279  }
1280  OS << ", ";
1281  PrintQuotedString(FixedSizePortion, OS);
1282  EmitEOL();
1283  this->MCStreamer::EmitCVDefRangeDirective(Ranges, FixedSizePortion);
1284 }
1285 
1286 void MCAsmStreamer::EmitCVStringTableDirective() {
1287  OS << "\t.cv_stringtable";
1288  EmitEOL();
1289 }
1290 
1291 void MCAsmStreamer::EmitCVFileChecksumsDirective() {
1292  OS << "\t.cv_filechecksums";
1293  EmitEOL();
1294 }
1295 
1296 void MCAsmStreamer::EmitCVFileChecksumOffsetDirective(unsigned FileNo) {
1297  OS << "\t.cv_filechecksumoffset\t" << FileNo;
1298  EmitEOL();
1299 }
1300 
1301 void MCAsmStreamer::EmitCVFPOData(const MCSymbol *ProcSym, SMLoc L) {
1302  OS << "\t.cv_fpo_data\t";
1303  ProcSym->print(OS, MAI);
1304  EmitEOL();
1305 }
1306 
1307 void MCAsmStreamer::EmitIdent(StringRef IdentString) {
1308  assert(MAI->hasIdentDirective() && ".ident directive not supported");
1309  OS << "\t.ident\t";
1310  PrintQuotedString(IdentString, OS);
1311  EmitEOL();
1312 }
1313 
1314 void MCAsmStreamer::EmitCFISections(bool EH, bool Debug) {
1315  MCStreamer::EmitCFISections(EH, Debug);
1316  OS << "\t.cfi_sections ";
1317  if (EH) {
1318  OS << ".eh_frame";
1319  if (Debug)
1320  OS << ", .debug_frame";
1321  } else if (Debug) {
1322  OS << ".debug_frame";
1323  }
1324 
1325  EmitEOL();
1326 }
1327 
1328 void MCAsmStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
1329  OS << "\t.cfi_startproc";
1330  if (Frame.IsSimple)
1331  OS << " simple";
1332  EmitEOL();
1333 }
1334 
1335 void MCAsmStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
1337  OS << "\t.cfi_endproc";
1338  EmitEOL();
1339 }
1340 
1341 void MCAsmStreamer::EmitRegisterName(int64_t Register) {
1342  if (!MAI->useDwarfRegNumForCFI()) {
1343  // User .cfi_* directives can use arbitrary DWARF register numbers, not
1344  // just ones that map to LLVM register numbers and have known names.
1345  // Fall back to using the original number directly if no name is known.
1346  const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1347  int LLVMRegister = MRI->getLLVMRegNumFromEH(Register);
1348  if (LLVMRegister != -1) {
1349  InstPrinter->printRegName(OS, LLVMRegister);
1350  return;
1351  }
1352  }
1353  OS << Register;
1354 }
1355 
1356 void MCAsmStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) {
1357  MCStreamer::EmitCFIDefCfa(Register, Offset);
1358  OS << "\t.cfi_def_cfa ";
1359  EmitRegisterName(Register);
1360  OS << ", " << Offset;
1361  EmitEOL();
1362 }
1363 
1364 void MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
1366  OS << "\t.cfi_def_cfa_offset " << Offset;
1367  EmitEOL();
1368 }
1369 
1371  OS << "\t.cfi_escape ";
1372  if (!Values.empty()) {
1373  size_t e = Values.size() - 1;
1374  for (size_t i = 0; i < e; ++i)
1375  OS << format("0x%02x", uint8_t(Values[i])) << ", ";
1376  OS << format("0x%02x", uint8_t(Values[e]));
1377  }
1378 }
1379 
1380 void MCAsmStreamer::EmitCFIEscape(StringRef Values) {
1381  MCStreamer::EmitCFIEscape(Values);
1382  PrintCFIEscape(OS, Values);
1383  EmitEOL();
1384 }
1385 
1386 void MCAsmStreamer::EmitCFIGnuArgsSize(int64_t Size) {
1388 
1389  uint8_t Buffer[16] = { dwarf::DW_CFA_GNU_args_size };
1390  unsigned Len = encodeULEB128(Size, Buffer + 1) + 1;
1391 
1392  PrintCFIEscape(OS, StringRef((const char *)&Buffer[0], Len));
1393  EmitEOL();
1394 }
1395 
1396 void MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
1398  OS << "\t.cfi_def_cfa_register ";
1399  EmitRegisterName(Register);
1400  EmitEOL();
1401 }
1402 
1403 void MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
1404  this->MCStreamer::EmitCFIOffset(Register, Offset);
1405  OS << "\t.cfi_offset ";
1406  EmitRegisterName(Register);
1407  OS << ", " << Offset;
1408  EmitEOL();
1409 }
1410 
1411 void MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym,
1412  unsigned Encoding) {
1413  MCStreamer::EmitCFIPersonality(Sym, Encoding);
1414  OS << "\t.cfi_personality " << Encoding << ", ";
1415  Sym->print(OS, MAI);
1416  EmitEOL();
1417 }
1418 
1419 void MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
1420  MCStreamer::EmitCFILsda(Sym, Encoding);
1421  OS << "\t.cfi_lsda " << Encoding << ", ";
1422  Sym->print(OS, MAI);
1423  EmitEOL();
1424 }
1425 
1426 void MCAsmStreamer::EmitCFIRememberState() {
1428  OS << "\t.cfi_remember_state";
1429  EmitEOL();
1430 }
1431 
1432 void MCAsmStreamer::EmitCFIRestoreState() {
1434  OS << "\t.cfi_restore_state";
1435  EmitEOL();
1436 }
1437 
1438 void MCAsmStreamer::EmitCFIRestore(int64_t Register) {
1439  MCStreamer::EmitCFIRestore(Register);
1440  OS << "\t.cfi_restore ";
1441  EmitRegisterName(Register);
1442  EmitEOL();
1443 }
1444 
1445 void MCAsmStreamer::EmitCFISameValue(int64_t Register) {
1446  MCStreamer::EmitCFISameValue(Register);
1447  OS << "\t.cfi_same_value ";
1448  EmitRegisterName(Register);
1449  EmitEOL();
1450 }
1451 
1452 void MCAsmStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) {
1453  MCStreamer::EmitCFIRelOffset(Register, Offset);
1454  OS << "\t.cfi_rel_offset ";
1455  EmitRegisterName(Register);
1456  OS << ", " << Offset;
1457  EmitEOL();
1458 }
1459 
1460 void MCAsmStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) {
1462  OS << "\t.cfi_adjust_cfa_offset " << Adjustment;
1463  EmitEOL();
1464 }
1465 
1466 void MCAsmStreamer::EmitCFISignalFrame() {
1468  OS << "\t.cfi_signal_frame";
1469  EmitEOL();
1470 }
1471 
1472 void MCAsmStreamer::EmitCFIUndefined(int64_t Register) {
1473  MCStreamer::EmitCFIUndefined(Register);
1474  OS << "\t.cfi_undefined " << Register;
1475  EmitEOL();
1476 }
1477 
1478 void MCAsmStreamer::EmitCFIRegister(int64_t Register1, int64_t Register2) {
1479  MCStreamer::EmitCFIRegister(Register1, Register2);
1480  OS << "\t.cfi_register " << Register1 << ", " << Register2;
1481  EmitEOL();
1482 }
1483 
1484 void MCAsmStreamer::EmitCFIWindowSave() {
1486  OS << "\t.cfi_window_save";
1487  EmitEOL();
1488 }
1489 
1490 void MCAsmStreamer::EmitCFIReturnColumn(int64_t Register) {
1492  OS << "\t.cfi_return_column " << Register;
1493  EmitEOL();
1494 }
1495 
1496 void MCAsmStreamer::EmitWinCFIStartProc(const MCSymbol *Symbol, SMLoc Loc) {
1497  MCStreamer::EmitWinCFIStartProc(Symbol, Loc);
1498 
1499  OS << ".seh_proc ";
1500  Symbol->print(OS, MAI);
1501  EmitEOL();
1502 }
1503 
1504 void MCAsmStreamer::EmitWinCFIEndProc(SMLoc Loc) {
1506 
1507  OS << "\t.seh_endproc";
1508  EmitEOL();
1509 }
1510 
1511 void MCAsmStreamer::EmitWinCFIStartChained(SMLoc Loc) {
1513 
1514  OS << "\t.seh_startchained";
1515  EmitEOL();
1516 }
1517 
1518 void MCAsmStreamer::EmitWinCFIEndChained(SMLoc Loc) {
1520 
1521  OS << "\t.seh_endchained";
1522  EmitEOL();
1523 }
1524 
1525 void MCAsmStreamer::EmitWinEHHandler(const MCSymbol *Sym, bool Unwind,
1526  bool Except, SMLoc Loc) {
1527  MCStreamer::EmitWinEHHandler(Sym, Unwind, Except, Loc);
1528 
1529  OS << "\t.seh_handler ";
1530  Sym->print(OS, MAI);
1531  if (Unwind)
1532  OS << ", @unwind";
1533  if (Except)
1534  OS << ", @except";
1535  EmitEOL();
1536 }
1537 
1538 void MCAsmStreamer::EmitWinEHHandlerData(SMLoc Loc) {
1540 
1541  // Switch sections. Don't call SwitchSection directly, because that will
1542  // cause the section switch to be visible in the emitted assembly.
1543  // We only do this so the section switch that terminates the handler
1544  // data block is visible.
1545  WinEH::FrameInfo *CurFrame = getCurrentWinFrameInfo();
1546  MCSection *TextSec = &CurFrame->Function->getSection();
1547  MCSection *XData = getAssociatedXDataSection(TextSec);
1548  SwitchSectionNoChange(XData);
1549 
1550  OS << "\t.seh_handlerdata";
1551  EmitEOL();
1552 }
1553 
1554 void MCAsmStreamer::EmitWinCFIPushReg(unsigned Register, SMLoc Loc) {
1555  MCStreamer::EmitWinCFIPushReg(Register, Loc);
1556 
1557  OS << "\t.seh_pushreg " << Register;
1558  EmitEOL();
1559 }
1560 
1561 void MCAsmStreamer::EmitWinCFISetFrame(unsigned Register, unsigned Offset,
1562  SMLoc Loc) {
1563  MCStreamer::EmitWinCFISetFrame(Register, Offset, Loc);
1564 
1565  OS << "\t.seh_setframe " << Register << ", " << Offset;
1566  EmitEOL();
1567 }
1568 
1569 void MCAsmStreamer::EmitWinCFIAllocStack(unsigned Size, SMLoc Loc) {
1571 
1572  OS << "\t.seh_stackalloc " << Size;
1573  EmitEOL();
1574 }
1575 
1576 void MCAsmStreamer::EmitWinCFISaveReg(unsigned Register, unsigned Offset,
1577  SMLoc Loc) {
1578  MCStreamer::EmitWinCFISaveReg(Register, Offset, Loc);
1579 
1580  OS << "\t.seh_savereg " << Register << ", " << Offset;
1581  EmitEOL();
1582 }
1583 
1584 void MCAsmStreamer::EmitWinCFISaveXMM(unsigned Register, unsigned Offset,
1585  SMLoc Loc) {
1586  MCStreamer::EmitWinCFISaveXMM(Register, Offset, Loc);
1587 
1588  OS << "\t.seh_savexmm " << Register << ", " << Offset;
1589  EmitEOL();
1590 }
1591 
1592 void MCAsmStreamer::EmitWinCFIPushFrame(bool Code, SMLoc Loc) {
1594 
1595  OS << "\t.seh_pushframe";
1596  if (Code)
1597  OS << " @code";
1598  EmitEOL();
1599 }
1600 
1601 void MCAsmStreamer::EmitWinCFIEndProlog(SMLoc Loc) {
1603 
1604  OS << "\t.seh_endprologue";
1605  EmitEOL();
1606 }
1607 
1608 void MCAsmStreamer::AddEncodingComment(const MCInst &Inst,
1609  const MCSubtargetInfo &STI,
1610  bool PrintSchedInfo) {
1611  raw_ostream &OS = GetCommentOS();
1612  SmallString<256> Code;
1614  raw_svector_ostream VecOS(Code);
1615  Emitter->encodeInstruction(Inst, VecOS, Fixups, STI);
1616 
1617  // If we are showing fixups, create symbolic markers in the encoded
1618  // representation. We do this by making a per-bit map to the fixup item index,
1619  // then trying to display it as nicely as possible.
1620  SmallVector<uint8_t, 64> FixupMap;
1621  FixupMap.resize(Code.size() * 8);
1622  for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
1623  FixupMap[i] = 0;
1624 
1625  for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1626  MCFixup &F = Fixups[i];
1627  const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
1628  for (unsigned j = 0; j != Info.TargetSize; ++j) {
1629  unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
1630  assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
1631  FixupMap[Index] = 1 + i;
1632  }
1633  }
1634 
1635  // FIXME: Note the fixup comments for Thumb2 are completely bogus since the
1636  // high order halfword of a 32-bit Thumb2 instruction is emitted first.
1637  OS << "encoding: [";
1638  for (unsigned i = 0, e = Code.size(); i != e; ++i) {
1639  if (i)
1640  OS << ',';
1641 
1642  // See if all bits are the same map entry.
1643  uint8_t MapEntry = FixupMap[i * 8 + 0];
1644  for (unsigned j = 1; j != 8; ++j) {
1645  if (FixupMap[i * 8 + j] == MapEntry)
1646  continue;
1647 
1648  MapEntry = uint8_t(~0U);
1649  break;
1650  }
1651 
1652  if (MapEntry != uint8_t(~0U)) {
1653  if (MapEntry == 0) {
1654  OS << format("0x%02x", uint8_t(Code[i]));
1655  } else {
1656  if (Code[i]) {
1657  // FIXME: Some of the 8 bits require fix up.
1658  OS << format("0x%02x", uint8_t(Code[i])) << '\''
1659  << char('A' + MapEntry - 1) << '\'';
1660  } else
1661  OS << char('A' + MapEntry - 1);
1662  }
1663  } else {
1664  // Otherwise, write out in binary.
1665  OS << "0b";
1666  for (unsigned j = 8; j--;) {
1667  unsigned Bit = (Code[i] >> j) & 1;
1668 
1669  unsigned FixupBit;
1670  if (MAI->isLittleEndian())
1671  FixupBit = i * 8 + j;
1672  else
1673  FixupBit = i * 8 + (7-j);
1674 
1675  if (uint8_t MapEntry = FixupMap[FixupBit]) {
1676  assert(Bit == 0 && "Encoder wrote into fixed up bit!");
1677  OS << char('A' + MapEntry - 1);
1678  } else
1679  OS << Bit;
1680  }
1681  }
1682  }
1683  OS << "]";
1684  // If we are not going to add fixup or schedule comments after this point
1685  // then we have to end the current comment line with "\n".
1686  if (Fixups.size() || !PrintSchedInfo)
1687  OS << "\n";
1688 
1689  for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1690  MCFixup &F = Fixups[i];
1691  const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
1692  OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
1693  << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
1694  }
1695 }
1696 
1697 void MCAsmStreamer::EmitInstruction(const MCInst &Inst,
1698  const MCSubtargetInfo &STI,
1699  bool PrintSchedInfo) {
1700  assert(getCurrentSectionOnly() &&
1701  "Cannot emit contents before setting section!");
1702 
1703  // Show the encoding in a comment if we have a code emitter.
1704  if (Emitter)
1705  AddEncodingComment(Inst, STI, PrintSchedInfo);
1706 
1707  // Show the MCInst if enabled.
1708  if (ShowInst) {
1709  if (PrintSchedInfo)
1710  GetCommentOS() << "\n";
1711  Inst.dump_pretty(GetCommentOS(), InstPrinter.get(), "\n ");
1712  GetCommentOS() << "\n";
1713  }
1714 
1715  if(getTargetStreamer())
1716  getTargetStreamer()->prettyPrintAsm(*InstPrinter, OS, Inst, STI);
1717  else
1718  InstPrinter->printInst(&Inst, OS, "", STI);
1719 
1720  if (PrintSchedInfo) {
1721  std::string SI = STI.getSchedInfoStr(Inst);
1722  if (!SI.empty())
1723  GetCommentOS() << SI;
1724  }
1725 
1726  StringRef Comments = CommentToEmit;
1727  if (Comments.size() && Comments.back() != '\n')
1728  GetCommentOS() << "\n";
1729 
1730  EmitEOL();
1731 }
1732 
1733 void MCAsmStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
1734  OS << "\t.bundle_align_mode " << AlignPow2;
1735  EmitEOL();
1736 }
1737 
1738 void MCAsmStreamer::EmitBundleLock(bool AlignToEnd) {
1739  OS << "\t.bundle_lock";
1740  if (AlignToEnd)
1741  OS << " align_to_end";
1742  EmitEOL();
1743 }
1744 
1745 void MCAsmStreamer::EmitBundleUnlock() {
1746  OS << "\t.bundle_unlock";
1747  EmitEOL();
1748 }
1749 
1750 bool MCAsmStreamer::EmitRelocDirective(const MCExpr &Offset, StringRef Name,
1751  const MCExpr *Expr, SMLoc) {
1752  OS << "\t.reloc ";
1753  Offset.print(OS, MAI);
1754  OS << ", " << Name;
1755  if (Expr) {
1756  OS << ", ";
1757  Expr->print(OS, MAI);
1758  }
1759  EmitEOL();
1760  return false;
1761 }
1762 
1763 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
1764 /// the specified string in the output .s file. This capability is
1765 /// indicated by the hasRawTextSupport() predicate.
1766 void MCAsmStreamer::EmitRawTextImpl(StringRef String) {
1767  if (!String.empty() && String.back() == '\n')
1768  String = String.substr(0, String.size()-1);
1769  OS << String;
1770  EmitEOL();
1771 }
1772 
1773 void MCAsmStreamer::FinishImpl() {
1774  // If we are generating dwarf for assembly source files dump out the sections.
1775  if (getContext().getGenDwarfForAssembly())
1776  MCGenDwarfInfo::Emit(this);
1777 
1778  // Emit the label for the line table, if requested - since the rest of the
1779  // line table will be defined by .loc/.file directives, and not emitted
1780  // directly, the label is the only work required here.
1781  auto &Tables = getContext().getMCDwarfLineTables();
1782  if (!Tables.empty()) {
1783  assert(Tables.size() == 1 && "asm output only supports one line table");
1784  if (auto *Label = Tables.begin()->second.getLabel()) {
1785  SwitchSection(getContext().getObjectFileInfo()->getDwarfLineSection());
1786  EmitLabel(Label);
1787  }
1788  }
1789 }
1790 
1792  std::unique_ptr<formatted_raw_ostream> OS,
1793  bool isVerboseAsm, bool useDwarfDirectory,
1794  MCInstPrinter *IP, MCCodeEmitter *CE,
1795  MCAsmBackend *MAB, bool ShowInst) {
1796  return new MCAsmStreamer(Context, std::move(OS), isVerboseAsm,
1797  useDwarfDirectory, IP, CE, MAB, ShowInst);
1798 }
bool doesSupportDataRegionDirectives() const
Definition: MCAsmInfo.h:498
bool getCOMMDirectiveAlignmentIsInBytes() const
Definition: MCAsmInfo.h:515
virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column, unsigned Flags, unsigned Isa, unsigned Discriminator, StringRef FileName)
This implements the DWARF2 &#39;.loc fileno lineno ...&#39; assembler directive.
Definition: MCStreamer.cpp:202
uint64_t CallInst * C
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
void push_back(const T &Elt)
Definition: SmallVector.h:212
virtual void EmitCFISameValue(int64_t Register)
Definition: MCStreamer.cpp:476
const char * getLabelSuffix() const
Definition: MCAsmInfo.h:472
virtual void EmitCVDefRangeDirective(ArrayRef< std::pair< const MCSymbol *, const MCSymbol *>> Ranges, StringRef FixedSizePortion)
This implements the CodeView &#39;.cv_def_range&#39; assembler directive.
Definition: MCStreamer.cpp:291
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding)
Definition: MCStreamer.cpp:440
virtual void EmitWinCFIPushReg(unsigned Register, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:688
This represents a section on a Mach-O system (used by Mac OS X).
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
virtual void EmitCFIGnuArgsSize(int64_t Size)
Definition: MCStreamer.cpp:505
LLVMContext & Context
#define DWARF2_FLAG_PROLOGUE_END
Definition: MCDwarf.h:82
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:115
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
const char * getGlobalDirective() const
Definition: MCAsmInfo.h:507
.type _foo, STT_OBJECT # aka
Definition: MCDirectives.h:25
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
Not a valid directive.
Definition: MCDirectives.h:20
virtual void EmitWinCFIStartProc(const MCSymbol *Symbol, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:574
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:136
bool hasSingleParameterDotFile() const
Definition: MCAsmInfo.h:525
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
.watchos_version_min
Definition: MCDirectives.h:68
virtual void EmitWinCFISaveXMM(unsigned Register, unsigned Offset, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:755
SmallString< 32 > digest() const
Definition: MD5.cpp:264
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
void dump_pretty(raw_ostream &OS, const MCInstPrinter *Printer=nullptr, StringRef Separator=" ") const
Dump the MCInst as prettily as possible using the additional MC structures, if given.
Definition: MCInst.cpp:54
Target specific streamer interface.
Definition: MCStreamer.h:83
.ios_version_min
Definition: MCDirectives.h:65
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
const char * getCode16Directive() const
Definition: MCAsmInfo.h:491
bool hasDotTypeDotSizeDirective() const
Definition: MCAsmInfo.h:524
int getLLVMRegNumFromEH(unsigned RegNum) const
Map a DWARF EH register back to a target register (same as getLLVMRegNum(RegNum, true)) but return -1...
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:489
const char * getData64bitsDirective() const
Definition: MCAsmInfo.h:404
virtual void EmitCFIRegister(int64_t Register1, int64_t Register2)
Definition: MCStreamer.cpp:532
F(f)
const char * getTPRel64Directive() const
Definition: MCAsmInfo.h:409
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:677
.macosx_version_min
Definition: MCDirectives.h:66
MCDataRegionType
Definition: MCDirectives.h:56
Error takeError()
Take ownership of the stored error.
Definition: Error.h:537
virtual void EmitCFIDefCfaOffset(int64_t Offset)
Definition: MCStreamer.cpp:389
.type _foo, STT_NOTYPE # aka
Definition: MCDirectives.h:28
virtual MCSymbol * getDwarfLineTableSymbol(unsigned CUID)
Definition: MCStreamer.cpp:211
COFF::SymbolStorageClass StorageClass
Definition: COFFYAML.cpp:354
bool hasIdentDirective() const
Definition: MCAsmInfo.h:526
unsigned TargetOffset
The bit offset to write the relocation into.
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
virtual void EmitCFISections(bool EH, bool Debug)
Definition: MCStreamer.cpp:330
const char * getDTPRel64Directive() const
Definition: MCAsmInfo.h:407
static int64_t truncateToSize(int64_t Value, unsigned Bytes)
virtual void EmitCFIRememberState()
Definition: MCStreamer.cpp:457
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:466
virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding)
Definition: MCStreamer.cpp:449
#define DWARF2_FLAG_IS_STMT
Definition: MCDwarf.h:80
Definition: BitVector.h:921
const char * getAscizDirective() const
Definition: MCAsmInfo.h:504
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
virtual void EmitWinCFIEndProc(SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:591
virtual void EmitCFIEscape(StringRef Values)
Definition: MCStreamer.cpp:496
const char * getZeroDirective() const
Definition: MCAsmInfo.h:502
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
virtual void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:629
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:674
const char * Name
A target specific name for the fixup kind.
.data_region jt16
Definition: MCDirectives.h:59
iterator_range< const unsigned char * > bytes() const
Definition: StringRef.h:116
.local (ELF)
Definition: MCDirectives.h:35
const char * getGPRel64Directive() const
Definition: MCAsmInfo.h:405
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
StringRef getSectionName() const
StringRef getSingleStringRef() const
This returns the twine as a single StringRef.
Definition: Twine.h:437
const char * getWeakRefDirective() const
Definition: MCAsmInfo.h:530
.no_dead_strip (MachO)
Definition: MCDirectives.h:36
virtual void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line, unsigned Column, bool PrologueEnd, bool IsStmt, StringRef FileName, SMLoc Loc)
This implements the CodeView &#39;.cv_loc&#39; assembler directive.
Definition: MCStreamer.cpp:260
PlatformType
Definition: MachO.h:484
virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value)
Emit an assignment of Value to Symbol.
Definition: MCStreamer.cpp:835
virtual void EmitWinCFIStartChained(SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:602
Context object for machine code objects.
Definition: MCContext.h:63
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
const char * getWeakDirective() const
Definition: MCAsmInfo.h:529
formatted_raw_ostream & PadToColumn(unsigned NewCol)
PadToColumn - Align the output to some column number.
.code16 (X86) / .code 16 (ARM)
Definition: MCDirectives.h:51
#define T
virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame)
Definition: MCStreamer.cpp:366
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
.type _foo, STT_GNU_IFUNC
Definition: MCDirectives.h:24
.alt_entry (MachO)
Definition: MCDirectives.h:38
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
.protected (ELF)
Definition: MCDirectives.h:40
.lazy_reference (MachO)
Definition: MCDirectives.h:34
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
virtual void EmitCFIRestoreState()
Definition: MCStreamer.cpp:466
const char * getTPRel32Directive() const
Definition: MCAsmInfo.h:410
bool hasNoDeadStrip() const
Definition: MCAsmInfo.h:527
.reference (MachO)
Definition: MCDirectives.h:41
StringRef getSegmentName() const
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
virtual void EmitCFIRestore(int64_t Register)
Definition: MCStreamer.cpp:486
const char * getData8bitsDirective() const
Definition: MCAsmInfo.h:401
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:121
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
void append(in_iter S, in_iter E)
Append from an iterator pair.
Definition: SmallString.h:75
.hidden (ELF)
Definition: MCDirectives.h:31
.data_region jt32
Definition: MCDirectives.h:60
const SmallVectorImpl< MCDwarfFile > & getMCDwarfFiles() const
Definition: MCDwarf.h:299
Streaming machine code generation interface.
Definition: MCStreamer.h:183
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:40
unsigned const MachineRegisterInfo * MRI
.weak_def_can_be_hidden (MachO)
Definition: MCDirectives.h:45
bool useDwarfRegNumForCFI() const
Definition: MCAsmInfo.h:580
const char * getData16bitsDirective() const
Definition: MCAsmInfo.h:402
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:421
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
virtual void EmitCVLinetableDirective(unsigned FunctionId, const MCSymbol *FnStart, const MCSymbol *FnEnd)
This implements the CodeView &#39;.cv_linetable&#39; assembler directive.
Definition: MCStreamer.cpp:281
.tvos_version_min
Definition: MCDirectives.h:67
static char toOctal(int X)
unsigned getAssemblerDialect() const
Definition: MCAsmInfo.h:494
#define DWARF2_FLAG_EPILOGUE_BEGIN
Definition: MCDwarf.h:83
MCStreamer * createAsmStreamer(MCContext &Ctx, std::unique_ptr< formatted_raw_ostream > OS, bool isVerboseAsm, bool useDwarfDirectory, MCInstPrinter *InstPrint, MCCodeEmitter *CE, MCAsmBackend *TAB, bool ShowInst)
Create a machine code streamer which will print out assembly for the native target, suitable for compiling with a native assembler.
const char * getSeparatorString() const
Definition: MCAsmInfo.h:465
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:22
virtual void EmitCFIDefCfaRegister(int64_t Register)
Definition: MCStreamer.cpp:409
MCLOHType
Linker Optimization Hint Type.
StringRef getCommentString() const
Definition: MCAsmInfo.h:471
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
Definition: AsmWriter.cpp:3503
.subsections_via_symbols (MachO)
Definition: MCDirectives.h:50
LCOMM::LCOMMType getLCOMMDirectiveAlignmentType() const
Definition: MCAsmInfo.h:519
#define DWARF2_FLAG_BASIC_BLOCK
Definition: MCDwarf.h:81
virtual void EmitWinEHHandlerData(SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:646
static const char * getVersionMinDirective(MCVersionMinType Type)
uint32_t getOffset() const
Definition: MCFixup.h:95
virtual void EmitCFIUndefined(int64_t Register)
Definition: MCStreamer.cpp:522
bool hasSubsectionsViaSymbols() const
Definition: MCAsmInfo.h:397
.weak_reference (MachO)
Definition: MCDirectives.h:44
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition: Twine.cpp:32
const char * getData32bitsDirective() const
Definition: MCAsmInfo.h:403
virtual void EmitWinCFIEndChained(SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:615
LLVM_NODISCARD char back() const
back - Get the last character in the string.
Definition: StringRef.h:149
virtual void PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T, raw_ostream &OS, const MCExpr *Subsection) const =0
virtual void EmitWinCFISetFrame(unsigned Register, unsigned Offset, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:699
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
virtual void EmitWinCFIPushFrame(bool Code, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:770
static void PrintCFIEscape(llvm::formatted_raw_ostream &OS, StringRef Values)
const MCDummyFragment & getDummyFragment() const
Definition: MCSection.h:153
virtual void EmitCFIReturnColumn(int64_t Register)
Definition: MCStreamer.cpp:552
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:81
virtual void EmitCFIOffset(int64_t Register, int64_t Offset)
Definition: MCStreamer.cpp:420
Expected< unsigned > tryGetFile(StringRef &Directory, StringRef &FileName, MD5::MD5Result *Checksum, Optional< StringRef > Source, unsigned FileNumber=0)
Definition: MCDwarf.cpp:519
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:862
virtual void EmitWinCFIEndProlog(SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:784
.indirect_symbol (MachO)
Definition: MCDirectives.h:32
virtual void EmitWinCFIAllocStack(unsigned Size, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:721
.type _foo, STT_TLS # aka
Definition: MCDirectives.h:26
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:710
Promote Memory to Register
Definition: Mem2Reg.cpp:110
virtual void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, const MCSymbol *FnStartSym, const MCSymbol *FnEndSym)
This implements the CodeView &#39;.cv_inline_linetable&#39; assembler directive.
Definition: MCStreamer.cpp:285
unsigned TargetSize
The number of bits written by this fixup.
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:514
reference get()
Returns a reference to the stored T value.
Definition: Error.h:517
MCSymbolAttr
Definition: MCDirectives.h:19
static StringRef MCLOHDirectiveName()
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:531
const MCSymbol * Function
Definition: MCWinEH.h:35
.syntax (ARM/ELF)
Definition: MCDirectives.h:49
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:267
std::string toHex(StringRef Input)
Convert buffer Input to its hexadecimal representation.
Definition: StringExtras.h:112
size_t GetNumBytesInBuffer() const
Definition: raw_ostream.h:132
.internal (ELF)
Definition: MCDirectives.h:33
static void Emit(MCStreamer *MCOS)
Definition: MCDwarf.cpp:1059
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:169
.code32 (X86) / .code 32 (ARM)
Definition: MCDirectives.h:52
amdgpu Simplify well known AMD library false Value Value * Arg
.type _foo, STT_COMMON # aka
Definition: MCDirectives.h:27
.code64 (X86)
Definition: MCDirectives.h:53
static const char * getPlatformName(MachO::PlatformType Type)
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:41
.symbol_resolver (MachO)
Definition: MCDirectives.h:37
virtual void EmitWinCFISaveReg(unsigned Register, unsigned Offset, SMLoc Loc=SMLoc())
Definition: MCStreamer.cpp:738
.type _foo,
Definition: MCDirectives.h:30
virtual void EmitCFISignalFrame()
Definition: MCStreamer.cpp:515
virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset)
Definition: MCStreamer.cpp:430
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:61
MCAssemblerFlag
Definition: MCDirectives.h:48
.type _foo, STT_FUNC # aka
Definition: MCDirectives.h:23
LLVM_NODISCARD size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:395
#define I(x, y, z)
Definition: MD5.cpp:58
const char * getDTPRel32Directive() const
Definition: MCAsmInfo.h:408
MCSubtargetInfo - Generic base class for all target subtargets.
bool isLittleEndian() const
True if the target is little endian.
Definition: MCAsmInfo.h:392
.weak_definition (MachO)
Definition: MCDirectives.h:43
void emitFill(uint64_t NumBytes, uint8_t FillValue)
Emit NumBytes bytes worth of the value specified by FillValue.
Definition: MCStreamer.cpp:183
virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset)
Definition: MCStreamer.cpp:378
Target independent information on a fixup kind.
const char * getAsciiDirective() const
Definition: MCAsmInfo.h:503
const unsigned Kind
const char * getGPRel32Directive() const
Definition: MCAsmInfo.h:406
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
.private_extern (MachO)
Definition: MCDirectives.h:39
unsigned getTextAlignFillValue() const
Definition: MCAsmInfo.h:506
.data_region jt8
Definition: MCDirectives.h:58
LLVM_NODISCARD char front() const
front - Get the first character in the string.
Definition: StringRef.h:142
unsigned getCommentColumn() const
This indicates the column (zero-based) at which asm comments should be printed.
Definition: MCAsmInfo.h:469
static int MCLOHIdToNbArgs(MCLOHType Kind)
virtual bool EmitCVInlineSiteIdDirective(unsigned FunctionId, unsigned IAFunc, unsigned IAFile, unsigned IALine, unsigned IACol, SMLoc Loc)
Introduces an inline call site id for use with .cv_loc.
Definition: MCStreamer.cpp:246
uint64_t PowerOf2Floor(uint64_t A)
Returns the power of two which is less than or equal to the given value.
Definition: MathExtras.h:644
MCVersionMinType
Definition: MCDirectives.h:64
LLVM Value Representation.
Definition: Value.h:73
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:40
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
static cl::opt< bool, true > Debug("debug", cl::desc("Enable debug output"), cl::Hidden, cl::location(DebugFlag))
virtual bool EmitCVFuncIdDirective(unsigned FunctionId)
Introduces a function id for use with .cv_loc.
Definition: MCStreamer.cpp:242
raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
static StringRef MCLOHIdToName(MCLOHType Kind)
virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:312
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
const MCExpr * getValue() const
Definition: MCFixup.h:98
const char * getCode64Directive() const
Definition: MCAsmInfo.h:493
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
virtual std::string getSchedInfoStr(const MachineInstr &MI) const
Returns string representation of scheduler comment.
static void PrintQuotedString(StringRef Data, raw_ostream &OS)
Represents a location in source code.
Definition: SMLoc.h:24
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:298
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:159
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment)
Definition: MCStreamer.cpp:399
.end_data_region
Definition: MCDirectives.h:61
MCFixupKind getKind() const
Definition: MCFixup.h:93
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144
const char * getCode32Directive() const
Definition: MCAsmInfo.h:492
virtual void EmitCFIWindowSave()
Definition: MCStreamer.cpp:542
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:59
void resize(size_type N)
Definition: SmallVector.h:353