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