Home | History | Annotate | Download | only in MC
      1 //===- lib/MC/MCAsmStreamer.cpp - Text Assembly Output --------------------===//
      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/MC/MCStreamer.h"
     11 #include "llvm/ADT/SmallString.h"
     12 #include "llvm/ADT/STLExtras.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/MCContext.h"
     19 #include "llvm/MC/MCExpr.h"
     20 #include "llvm/MC/MCFixupKindInfo.h"
     21 #include "llvm/MC/MCInst.h"
     22 #include "llvm/MC/MCInstPrinter.h"
     23 #include "llvm/MC/MCObjectFileInfo.h"
     24 #include "llvm/MC/MCRegisterInfo.h"
     25 #include "llvm/MC/MCSectionCOFF.h"
     26 #include "llvm/MC/MCSectionMachO.h"
     27 #include "llvm/MC/MCSymbol.h"
     28 #include "llvm/Support/CommandLine.h"
     29 #include "llvm/Support/ErrorHandling.h"
     30 #include "llvm/Support/Format.h"
     31 #include "llvm/Support/FormattedStream.h"
     32 #include "llvm/Support/MathExtras.h"
     33 #include "llvm/Support/Path.h"
     34 #include <cctype>
     35 #include <unordered_map>
     36 using namespace llvm;
     37 
     38 namespace {
     39 
     40 class MCAsmStreamer : public MCStreamer {
     41 protected:
     42   formatted_raw_ostream &OS;
     43   const MCAsmInfo *MAI;
     44 private:
     45   std::unique_ptr<MCInstPrinter> InstPrinter;
     46   std::unique_ptr<MCCodeEmitter> Emitter;
     47   std::unique_ptr<MCAsmBackend> AsmBackend;
     48 
     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, formatted_raw_ostream &os,
     62                 bool isVerboseAsm, bool useDwarfDirectory,
     63                 MCInstPrinter *printer, MCCodeEmitter *emitter,
     64                 MCAsmBackend *asmbackend, bool showInst)
     65       : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
     66         InstPrinter(printer), Emitter(emitter), AsmBackend(asmbackend),
     67         CommentStream(CommentToEmit), IsVerboseAsm(isVerboseAsm),
     68         ShowInst(showInst), UseDwarfDirectory(useDwarfDirectory) {
     69     if (InstPrinter && IsVerboseAsm)
     70       InstPrinter->setCommentStream(CommentStream);
     71   }
     72 
     73   inline void EmitEOL() {
     74     // If we don't have any comments, just emit a \n.
     75     if (!IsVerboseAsm) {
     76       OS << '\n';
     77       return;
     78     }
     79     EmitCommentsAndEOL();
     80   }
     81   void EmitCommentsAndEOL();
     82 
     83   /// isVerboseAsm - Return true if this streamer supports verbose assembly at
     84   /// all.
     85   bool isVerboseAsm() const override { return IsVerboseAsm; }
     86 
     87   /// hasRawTextSupport - We support EmitRawText.
     88   bool hasRawTextSupport() const override { return true; }
     89 
     90   /// AddComment - Add a comment that can be emitted to the generated .s
     91   /// file if applicable as a QoI issue to make the output of the compiler
     92   /// more readable.  This only affects the MCAsmStreamer, and only when
     93   /// verbose assembly output is enabled.
     94   void AddComment(const Twine &T) override;
     95 
     96   /// AddEncodingComment - Add a comment showing the encoding of an instruction.
     97   void AddEncodingComment(const MCInst &Inst, const MCSubtargetInfo &);
     98 
     99   /// GetCommentOS - Return a raw_ostream that comments can be written to.
    100   /// Unlike AddComment, you are required to terminate comments with \n if you
    101   /// use this method.
    102   raw_ostream &GetCommentOS() override {
    103     if (!IsVerboseAsm)
    104       return nulls();  // Discard comments unless in verbose asm mode.
    105     return CommentStream;
    106   }
    107 
    108   void emitRawComment(const Twine &T, bool TabPrefix = true) override;
    109 
    110   /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
    111   void AddBlankLine() override {
    112     EmitEOL();
    113   }
    114 
    115   /// @name MCStreamer Interface
    116   /// @{
    117 
    118   void ChangeSection(const MCSection *Section,
    119                      const MCExpr *Subsection) override;
    120 
    121   void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override;
    122   void EmitLabel(MCSymbol *Symbol) override;
    123 
    124   void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
    125   void EmitLinkerOptions(ArrayRef<std::string> Options) override;
    126   void EmitDataRegion(MCDataRegionType Kind) override;
    127   void EmitVersionMin(MCVersionMinType Kind, unsigned Major, unsigned Minor,
    128                       unsigned Update) override;
    129   void EmitThumbFunc(MCSymbol *Func) override;
    130 
    131   void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
    132   void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
    133   bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
    134 
    135   void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
    136   void BeginCOFFSymbolDef(const MCSymbol *Symbol) override;
    137   void EmitCOFFSymbolStorageClass(int StorageClass) override;
    138   void EmitCOFFSymbolType(int Type) override;
    139   void EndCOFFSymbolDef() override;
    140   void EmitCOFFSectionIndex(MCSymbol const *Symbol) override;
    141   void EmitCOFFSecRel32(MCSymbol const *Symbol) override;
    142   void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override;
    143   void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
    144                         unsigned ByteAlignment) override;
    145 
    146   /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
    147   ///
    148   /// @param Symbol - The common symbol to emit.
    149   /// @param Size - The size of the common symbol.
    150   /// @param ByteAlignment - The alignment of the common symbol in bytes.
    151   void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
    152                              unsigned ByteAlignment) override;
    153 
    154   void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = nullptr,
    155                     uint64_t Size = 0, unsigned ByteAlignment = 0) override;
    156 
    157   void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
    158                        uint64_t Size, unsigned ByteAlignment = 0) override;
    159 
    160   void EmitBytes(StringRef Data) override;
    161 
    162   void EmitValueImpl(const MCExpr *Value, unsigned Size,
    163                      const SMLoc &Loc = SMLoc()) override;
    164   void EmitIntValue(uint64_t Value, unsigned Size) override;
    165 
    166   void EmitULEB128Value(const MCExpr *Value) override;
    167 
    168   void EmitSLEB128Value(const MCExpr *Value) override;
    169 
    170   void EmitGPRel64Value(const MCExpr *Value) override;
    171 
    172   void EmitGPRel32Value(const MCExpr *Value) override;
    173 
    174 
    175   void EmitFill(uint64_t NumBytes, uint8_t FillValue) override;
    176 
    177   void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
    178                             unsigned ValueSize = 1,
    179                             unsigned MaxBytesToEmit = 0) override;
    180 
    181   void EmitCodeAlignment(unsigned ByteAlignment,
    182                          unsigned MaxBytesToEmit = 0) override;
    183 
    184   bool EmitValueToOffset(const MCExpr *Offset,
    185                          unsigned char Value = 0) override;
    186 
    187   void EmitFileDirective(StringRef Filename) override;
    188   unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
    189                                   StringRef Filename,
    190                                   unsigned CUID = 0) override;
    191   void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
    192                              unsigned Column, unsigned Flags,
    193                              unsigned Isa, unsigned Discriminator,
    194                              StringRef FileName) override;
    195   MCSymbol *getDwarfLineTableSymbol(unsigned CUID) override;
    196 
    197   void EmitIdent(StringRef IdentString) override;
    198   void EmitCFISections(bool EH, bool Debug) override;
    199   void EmitCFIDefCfa(int64_t Register, int64_t Offset) override;
    200   void EmitCFIDefCfaOffset(int64_t Offset) override;
    201   void EmitCFIDefCfaRegister(int64_t Register) override;
    202   void EmitCFIOffset(int64_t Register, int64_t Offset) override;
    203   void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding) override;
    204   void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) override;
    205   void EmitCFIRememberState() override;
    206   void EmitCFIRestoreState() override;
    207   void EmitCFISameValue(int64_t Register) override;
    208   void EmitCFIRelOffset(int64_t Register, int64_t Offset) override;
    209   void EmitCFIAdjustCfaOffset(int64_t Adjustment) override;
    210   void EmitCFISignalFrame() override;
    211   void EmitCFIUndefined(int64_t Register) override;
    212   void EmitCFIRegister(int64_t Register1, int64_t Register2) override;
    213   void EmitCFIWindowSave() override;
    214 
    215   void EmitWinCFIStartProc(const MCSymbol *Symbol) override;
    216   void EmitWinCFIEndProc() override;
    217   void EmitWinCFIStartChained() override;
    218   void EmitWinCFIEndChained() override;
    219   void EmitWinCFIPushReg(unsigned Register) override;
    220   void EmitWinCFISetFrame(unsigned Register, unsigned Offset) override;
    221   void EmitWinCFIAllocStack(unsigned Size) override;
    222   void EmitWinCFISaveReg(unsigned Register, unsigned Offset) override;
    223   void EmitWinCFISaveXMM(unsigned Register, unsigned Offset) override;
    224   void EmitWinCFIPushFrame(bool Code) override;
    225   void EmitWinCFIEndProlog() override;
    226 
    227   void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except) override;
    228   void EmitWinEHHandlerData() override;
    229 
    230   void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
    231 
    232   void EmitBundleAlignMode(unsigned AlignPow2) override;
    233   void EmitBundleLock(bool AlignToEnd) override;
    234   void EmitBundleUnlock() override;
    235 
    236   /// EmitRawText - If this file is backed by an assembly streamer, this dumps
    237   /// the specified string in the output .s file.  This capability is
    238   /// indicated by the hasRawTextSupport() predicate.
    239   void EmitRawTextImpl(StringRef String) override;
    240 
    241   void FinishImpl() override;
    242 };
    243 
    244 } // end anonymous namespace.
    245 
    246 /// AddComment - Add a comment that can be emitted to the generated .s
    247 /// file if applicable as a QoI issue to make the output of the compiler
    248 /// more readable.  This only affects the MCAsmStreamer, and only when
    249 /// verbose assembly output is enabled.
    250 void MCAsmStreamer::AddComment(const Twine &T) {
    251   if (!IsVerboseAsm) return;
    252 
    253   // Make sure that CommentStream is flushed.
    254   CommentStream.flush();
    255 
    256   T.toVector(CommentToEmit);
    257   // Each comment goes on its own line.
    258   CommentToEmit.push_back('\n');
    259 
    260   // Tell the comment stream that the vector changed underneath it.
    261   CommentStream.resync();
    262 }
    263 
    264 void MCAsmStreamer::EmitCommentsAndEOL() {
    265   if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
    266     OS << '\n';
    267     return;
    268   }
    269 
    270   CommentStream.flush();
    271   StringRef Comments = CommentToEmit.str();
    272 
    273   assert(Comments.back() == '\n' &&
    274          "Comment array not newline terminated");
    275   do {
    276     // Emit a line of comments.
    277     OS.PadToColumn(MAI->getCommentColumn());
    278     size_t Position = Comments.find('\n');
    279     OS << MAI->getCommentString() << ' ' << Comments.substr(0, Position) <<'\n';
    280 
    281     Comments = Comments.substr(Position+1);
    282   } while (!Comments.empty());
    283 
    284   CommentToEmit.clear();
    285   // Tell the comment stream that the vector changed underneath it.
    286   CommentStream.resync();
    287 }
    288 
    289 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
    290   assert(Bytes && "Invalid size!");
    291   return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
    292 }
    293 
    294 void MCAsmStreamer::emitRawComment(const Twine &T, bool TabPrefix) {
    295   if (TabPrefix)
    296     OS << '\t';
    297   OS << MAI->getCommentString() << T;
    298   EmitEOL();
    299 }
    300 
    301 void MCAsmStreamer::ChangeSection(const MCSection *Section,
    302                                   const MCExpr *Subsection) {
    303   assert(Section && "Cannot switch to a null section!");
    304   Section->PrintSwitchToSection(*MAI, OS, Subsection);
    305 }
    306 
    307 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
    308   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
    309   MCStreamer::EmitLabel(Symbol);
    310 
    311   OS << *Symbol << MAI->getLabelSuffix();
    312   EmitEOL();
    313 }
    314 
    315 void MCAsmStreamer::EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {
    316   StringRef str = MCLOHIdToName(Kind);
    317 
    318 #ifndef NDEBUG
    319   int NbArgs = MCLOHIdToNbArgs(Kind);
    320   assert(NbArgs != -1 && ((size_t)NbArgs) == Args.size() && "Malformed LOH!");
    321   assert(str != "" && "Invalid LOH name");
    322 #endif
    323 
    324   OS << "\t" << MCLOHDirectiveName() << " " << str << "\t";
    325   bool IsFirst = true;
    326   for (MCLOHArgs::const_iterator It = Args.begin(), EndIt = Args.end();
    327        It != EndIt; ++It) {
    328     if (!IsFirst)
    329       OS << ", ";
    330     IsFirst = false;
    331     OS << **It;
    332   }
    333   EmitEOL();
    334 }
    335 
    336 void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
    337   switch (Flag) {
    338   case MCAF_SyntaxUnified:         OS << "\t.syntax unified"; break;
    339   case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
    340   case MCAF_Code16:                OS << '\t'<< MAI->getCode16Directive();break;
    341   case MCAF_Code32:                OS << '\t'<< MAI->getCode32Directive();break;
    342   case MCAF_Code64:                OS << '\t'<< MAI->getCode64Directive();break;
    343   }
    344   EmitEOL();
    345 }
    346 
    347 void MCAsmStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
    348   assert(!Options.empty() && "At least one option is required!");
    349   OS << "\t.linker_option \"" << Options[0] << '"';
    350   for (ArrayRef<std::string>::iterator it = Options.begin() + 1,
    351          ie = Options.end(); it != ie; ++it) {
    352     OS << ", " << '"' << *it << '"';
    353   }
    354   OS << "\n";
    355 }
    356 
    357 void MCAsmStreamer::EmitDataRegion(MCDataRegionType Kind) {
    358   if (!MAI->doesSupportDataRegionDirectives())
    359     return;
    360   switch (Kind) {
    361   case MCDR_DataRegion:            OS << "\t.data_region"; break;
    362   case MCDR_DataRegionJT8:         OS << "\t.data_region jt8"; break;
    363   case MCDR_DataRegionJT16:        OS << "\t.data_region jt16"; break;
    364   case MCDR_DataRegionJT32:        OS << "\t.data_region jt32"; break;
    365   case MCDR_DataRegionEnd:         OS << "\t.end_data_region"; break;
    366   }
    367   EmitEOL();
    368 }
    369 
    370 void MCAsmStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major,
    371                                    unsigned Minor, unsigned Update) {
    372   switch (Kind) {
    373   case MCVM_IOSVersionMin:        OS << "\t.ios_version_min"; break;
    374   case MCVM_OSXVersionMin:        OS << "\t.macosx_version_min"; break;
    375   }
    376   OS << " " << Major << ", " << Minor;
    377   if (Update)
    378     OS << ", " << Update;
    379   EmitEOL();
    380 }
    381 
    382 void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
    383   // This needs to emit to a temporary string to get properly quoted
    384   // MCSymbols when they have spaces in them.
    385   OS << "\t.thumb_func";
    386   // Only Mach-O hasSubsectionsViaSymbols()
    387   if (MAI->hasSubsectionsViaSymbols())
    388     OS << '\t' << *Func;
    389   EmitEOL();
    390 }
    391 
    392 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
    393   OS << *Symbol << " = " << *Value;
    394   EmitEOL();
    395 
    396   MCStreamer::EmitAssignment(Symbol, Value);
    397 }
    398 
    399 void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
    400   OS << ".weakref " << *Alias << ", " << *Symbol;
    401   EmitEOL();
    402 }
    403 
    404 bool MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
    405                                         MCSymbolAttr Attribute) {
    406   switch (Attribute) {
    407   case MCSA_Invalid: llvm_unreachable("Invalid symbol attribute");
    408   case MCSA_ELF_TypeFunction:    /// .type _foo, STT_FUNC  # aka @function
    409   case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
    410   case MCSA_ELF_TypeObject:      /// .type _foo, STT_OBJECT  # aka @object
    411   case MCSA_ELF_TypeTLS:         /// .type _foo, STT_TLS     # aka @tls_object
    412   case MCSA_ELF_TypeCommon:      /// .type _foo, STT_COMMON  # aka @common
    413   case MCSA_ELF_TypeNoType:      /// .type _foo, STT_NOTYPE  # aka @notype
    414   case MCSA_ELF_TypeGnuUniqueObject:  /// .type _foo, @gnu_unique_object
    415     if (!MAI->hasDotTypeDotSizeDirective())
    416       return false; // Symbol attribute not supported
    417     OS << "\t.type\t" << *Symbol << ','
    418        << ((MAI->getCommentString()[0] != '@') ? '@' : '%');
    419     switch (Attribute) {
    420     default: return false;
    421     case MCSA_ELF_TypeFunction:    OS << "function"; break;
    422     case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
    423     case MCSA_ELF_TypeObject:      OS << "object"; break;
    424     case MCSA_ELF_TypeTLS:         OS << "tls_object"; break;
    425     case MCSA_ELF_TypeCommon:      OS << "common"; break;
    426     case MCSA_ELF_TypeNoType:      OS << "no_type"; break;
    427     case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
    428     }
    429     EmitEOL();
    430     return true;
    431   case MCSA_Global: // .globl/.global
    432     OS << MAI->getGlobalDirective();
    433     break;
    434   case MCSA_Hidden:         OS << "\t.hidden\t";          break;
    435   case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
    436   case MCSA_Internal:       OS << "\t.internal\t";        break;
    437   case MCSA_LazyReference:  OS << "\t.lazy_reference\t";  break;
    438   case MCSA_Local:          OS << "\t.local\t";           break;
    439   case MCSA_NoDeadStrip:    OS << "\t.no_dead_strip\t";   break;
    440   case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
    441   case MCSA_PrivateExtern:
    442     OS << "\t.private_extern\t";
    443     break;
    444   case MCSA_Protected:      OS << "\t.protected\t";       break;
    445   case MCSA_Reference:      OS << "\t.reference\t";       break;
    446   case MCSA_Weak:           OS << "\t.weak\t";            break;
    447   case MCSA_WeakDefinition:
    448     OS << "\t.weak_definition\t";
    449     break;
    450       // .weak_reference
    451   case MCSA_WeakReference:  OS << MAI->getWeakRefDirective(); break;
    452   case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
    453   }
    454 
    455   OS << *Symbol;
    456   EmitEOL();
    457 
    458   return true;
    459 }
    460 
    461 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
    462   OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
    463   EmitEOL();
    464 }
    465 
    466 void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
    467   OS << "\t.def\t " << *Symbol << ';';
    468   EmitEOL();
    469 }
    470 
    471 void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
    472   OS << "\t.scl\t" << StorageClass << ';';
    473   EmitEOL();
    474 }
    475 
    476 void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
    477   OS << "\t.type\t" << Type << ';';
    478   EmitEOL();
    479 }
    480 
    481 void MCAsmStreamer::EndCOFFSymbolDef() {
    482   OS << "\t.endef";
    483   EmitEOL();
    484 }
    485 
    486 void MCAsmStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
    487   OS << "\t.secidx\t" << *Symbol;
    488   EmitEOL();
    489 }
    490 
    491 void MCAsmStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
    492   OS << "\t.secrel32\t" << *Symbol;
    493   EmitEOL();
    494 }
    495 
    496 void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
    497   assert(MAI->hasDotTypeDotSizeDirective());
    498   OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
    499 }
    500 
    501 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
    502                                      unsigned ByteAlignment) {
    503   // Common symbols do not belong to any actual section.
    504   AssignSection(Symbol, nullptr);
    505 
    506   OS << "\t.comm\t" << *Symbol << ',' << Size;
    507   if (ByteAlignment != 0) {
    508     if (MAI->getCOMMDirectiveAlignmentIsInBytes())
    509       OS << ',' << ByteAlignment;
    510     else
    511       OS << ',' << Log2_32(ByteAlignment);
    512   }
    513   EmitEOL();
    514 }
    515 
    516 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
    517 ///
    518 /// @param Symbol - The common symbol to emit.
    519 /// @param Size - The size of the common symbol.
    520 void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
    521                                           unsigned ByteAlign) {
    522   // Common symbols do not belong to any actual section.
    523   AssignSection(Symbol, nullptr);
    524 
    525   OS << "\t.lcomm\t" << *Symbol << ',' << Size;
    526   if (ByteAlign > 1) {
    527     switch (MAI->getLCOMMDirectiveAlignmentType()) {
    528     case LCOMM::NoAlignment:
    529       llvm_unreachable("alignment not supported on .lcomm!");
    530     case LCOMM::ByteAlignment:
    531       OS << ',' << ByteAlign;
    532       break;
    533     case LCOMM::Log2Alignment:
    534       assert(isPowerOf2_32(ByteAlign) && "alignment must be a power of 2");
    535       OS << ',' << Log2_32(ByteAlign);
    536       break;
    537     }
    538   }
    539   EmitEOL();
    540 }
    541 
    542 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
    543                                  uint64_t Size, unsigned ByteAlignment) {
    544   if (Symbol)
    545     AssignSection(Symbol, Section);
    546 
    547   // Note: a .zerofill directive does not switch sections.
    548   OS << ".zerofill ";
    549 
    550   // This is a mach-o specific directive.
    551   const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
    552   OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
    553 
    554   if (Symbol) {
    555     OS << ',' << *Symbol << ',' << Size;
    556     if (ByteAlignment != 0)
    557       OS << ',' << Log2_32(ByteAlignment);
    558   }
    559   EmitEOL();
    560 }
    561 
    562 // .tbss sym, size, align
    563 // This depends that the symbol has already been mangled from the original,
    564 // e.g. _a.
    565 void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
    566                                    uint64_t Size, unsigned ByteAlignment) {
    567   AssignSection(Symbol, Section);
    568 
    569   assert(Symbol && "Symbol shouldn't be NULL!");
    570   // Instead of using the Section we'll just use the shortcut.
    571   // This is a mach-o specific directive and section.
    572   OS << ".tbss " << *Symbol << ", " << Size;
    573 
    574   // Output align if we have it.  We default to 1 so don't bother printing
    575   // that.
    576   if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
    577 
    578   EmitEOL();
    579 }
    580 
    581 static inline char toOctal(int X) { return (X&7)+'0'; }
    582 
    583 static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
    584   OS << '"';
    585 
    586   for (unsigned i = 0, e = Data.size(); i != e; ++i) {
    587     unsigned char C = Data[i];
    588     if (C == '"' || C == '\\') {
    589       OS << '\\' << (char)C;
    590       continue;
    591     }
    592 
    593     if (isprint((unsigned char)C)) {
    594       OS << (char)C;
    595       continue;
    596     }
    597 
    598     switch (C) {
    599       case '\b': OS << "\\b"; break;
    600       case '\f': OS << "\\f"; break;
    601       case '\n': OS << "\\n"; break;
    602       case '\r': OS << "\\r"; break;
    603       case '\t': OS << "\\t"; break;
    604       default:
    605         OS << '\\';
    606         OS << toOctal(C >> 6);
    607         OS << toOctal(C >> 3);
    608         OS << toOctal(C >> 0);
    609         break;
    610     }
    611   }
    612 
    613   OS << '"';
    614 }
    615 
    616 
    617 void MCAsmStreamer::EmitBytes(StringRef Data) {
    618   assert(getCurrentSection().first &&
    619          "Cannot emit contents before setting section!");
    620   if (Data.empty()) return;
    621 
    622   if (Data.size() == 1) {
    623     OS << MAI->getData8bitsDirective();
    624     OS << (unsigned)(unsigned char)Data[0];
    625     EmitEOL();
    626     return;
    627   }
    628 
    629   // If the data ends with 0 and the target supports .asciz, use it, otherwise
    630   // use .ascii
    631   if (MAI->getAscizDirective() && Data.back() == 0) {
    632     OS << MAI->getAscizDirective();
    633     Data = Data.substr(0, Data.size()-1);
    634   } else {
    635     OS << MAI->getAsciiDirective();
    636   }
    637 
    638   PrintQuotedString(Data, OS);
    639   EmitEOL();
    640 }
    641 
    642 void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size) {
    643   EmitValue(MCConstantExpr::Create(Value, getContext()), Size);
    644 }
    645 
    646 void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
    647                                   const SMLoc &Loc) {
    648   assert(Size <= 8 && "Invalid size");
    649   assert(getCurrentSection().first &&
    650          "Cannot emit contents before setting section!");
    651   const char *Directive = nullptr;
    652   switch (Size) {
    653   default: break;
    654   case 1: Directive = MAI->getData8bitsDirective();  break;
    655   case 2: Directive = MAI->getData16bitsDirective(); break;
    656   case 4: Directive = MAI->getData32bitsDirective(); break;
    657   case 8: Directive = MAI->getData64bitsDirective(); break;
    658   }
    659 
    660   if (!Directive) {
    661     int64_t IntValue;
    662     if (!Value->EvaluateAsAbsolute(IntValue))
    663       report_fatal_error("Don't know how to emit this value.");
    664 
    665     // We couldn't handle the requested integer size so we fallback by breaking
    666     // the request down into several, smaller, integers.  Since sizes greater
    667     // than eight are invalid and size equivalent to eight should have been
    668     // handled earlier, we use four bytes as our largest piece of granularity.
    669     bool IsLittleEndian = MAI->isLittleEndian();
    670     for (unsigned Emitted = 0; Emitted != Size;) {
    671       unsigned Remaining = Size - Emitted;
    672       // The size of our partial emission must be a power of two less than
    673       // eight.
    674       unsigned EmissionSize = PowerOf2Floor(Remaining);
    675       if (EmissionSize > 4)
    676         EmissionSize = 4;
    677       // Calculate the byte offset of our partial emission taking into account
    678       // the endianness of the target.
    679       unsigned ByteOffset =
    680           IsLittleEndian ? Emitted : (Remaining - EmissionSize);
    681       uint64_t ValueToEmit = IntValue >> (ByteOffset * 8);
    682       // We truncate our partial emission to fit within the bounds of the
    683       // emission domain.  This produces nicer output and silences potential
    684       // truncation warnings when round tripping through another assembler.
    685       ValueToEmit &= ~0ULL >> (64 - EmissionSize * 8);
    686       EmitIntValue(ValueToEmit, EmissionSize);
    687       Emitted += EmissionSize;
    688     }
    689     return;
    690   }
    691 
    692   assert(Directive && "Invalid size for machine code value!");
    693   OS << Directive << *Value;
    694   EmitEOL();
    695 }
    696 
    697 void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value) {
    698   int64_t IntValue;
    699   if (Value->EvaluateAsAbsolute(IntValue)) {
    700     EmitULEB128IntValue(IntValue);
    701     return;
    702   }
    703   assert(MAI->hasLEB128() && "Cannot print a .uleb");
    704   OS << ".uleb128 " << *Value;
    705   EmitEOL();
    706 }
    707 
    708 void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value) {
    709   int64_t IntValue;
    710   if (Value->EvaluateAsAbsolute(IntValue)) {
    711     EmitSLEB128IntValue(IntValue);
    712     return;
    713   }
    714   assert(MAI->hasLEB128() && "Cannot print a .sleb");
    715   OS << ".sleb128 " << *Value;
    716   EmitEOL();
    717 }
    718 
    719 void MCAsmStreamer::EmitGPRel64Value(const MCExpr *Value) {
    720   assert(MAI->getGPRel64Directive() != nullptr);
    721   OS << MAI->getGPRel64Directive() << *Value;
    722   EmitEOL();
    723 }
    724 
    725 void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
    726   assert(MAI->getGPRel32Directive() != nullptr);
    727   OS << MAI->getGPRel32Directive() << *Value;
    728   EmitEOL();
    729 }
    730 
    731 
    732 /// EmitFill - Emit NumBytes bytes worth of the value specified by
    733 /// FillValue.  This implements directives such as '.space'.
    734 void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) {
    735   if (NumBytes == 0) return;
    736 
    737   if (const char *ZeroDirective = MAI->getZeroDirective()) {
    738     OS << ZeroDirective << NumBytes;
    739     if (FillValue != 0)
    740       OS << ',' << (int)FillValue;
    741     EmitEOL();
    742     return;
    743   }
    744 
    745   // Emit a byte at a time.
    746   MCStreamer::EmitFill(NumBytes, FillValue);
    747 }
    748 
    749 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
    750                                          unsigned ValueSize,
    751                                          unsigned MaxBytesToEmit) {
    752   // Some assemblers don't support non-power of two alignments, so we always
    753   // emit alignments as a power of two if possible.
    754   if (isPowerOf2_32(ByteAlignment)) {
    755     switch (ValueSize) {
    756     default:
    757       llvm_unreachable("Invalid size for machine code value!");
    758     case 1:
    759       OS << "\t.align\t";
    760       break;
    761     case 2:
    762       OS << ".p2alignw ";
    763       break;
    764     case 4:
    765       OS << ".p2alignl ";
    766       break;
    767     case 8:
    768       llvm_unreachable("Unsupported alignment size!");
    769     }
    770 
    771     if (MAI->getAlignmentIsInBytes())
    772       OS << ByteAlignment;
    773     else
    774       OS << Log2_32(ByteAlignment);
    775 
    776     if (Value || MaxBytesToEmit) {
    777       OS << ", 0x";
    778       OS.write_hex(truncateToSize(Value, ValueSize));
    779 
    780       if (MaxBytesToEmit)
    781         OS << ", " << MaxBytesToEmit;
    782     }
    783     EmitEOL();
    784     return;
    785   }
    786 
    787   // Non-power of two alignment.  This is not widely supported by assemblers.
    788   // FIXME: Parameterize this based on MAI.
    789   switch (ValueSize) {
    790   default: llvm_unreachable("Invalid size for machine code value!");
    791   case 1: OS << ".balign";  break;
    792   case 2: OS << ".balignw"; break;
    793   case 4: OS << ".balignl"; break;
    794   case 8: llvm_unreachable("Unsupported alignment size!");
    795   }
    796 
    797   OS << ' ' << ByteAlignment;
    798   OS << ", " << truncateToSize(Value, ValueSize);
    799   if (MaxBytesToEmit)
    800     OS << ", " << MaxBytesToEmit;
    801   EmitEOL();
    802 }
    803 
    804 void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
    805                                       unsigned MaxBytesToEmit) {
    806   // Emit with a text fill value.
    807   EmitValueToAlignment(ByteAlignment, MAI->getTextAlignFillValue(),
    808                        1, MaxBytesToEmit);
    809 }
    810 
    811 bool MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
    812                                       unsigned char Value) {
    813   // FIXME: Verify that Offset is associated with the current section.
    814   OS << ".org " << *Offset << ", " << (unsigned) Value;
    815   EmitEOL();
    816   return false;
    817 }
    818 
    819 
    820 void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
    821   assert(MAI->hasSingleParameterDotFile());
    822   OS << "\t.file\t";
    823   PrintQuotedString(Filename, OS);
    824   EmitEOL();
    825 }
    826 
    827 unsigned MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo,
    828                                                StringRef Directory,
    829                                                StringRef Filename,
    830                                                unsigned CUID) {
    831   assert(CUID == 0);
    832 
    833   MCDwarfLineTable &Table = getContext().getMCDwarfLineTable(CUID);
    834   unsigned NumFiles = Table.getMCDwarfFiles().size();
    835   FileNo = Table.getFile(Directory, Filename, FileNo);
    836   if (FileNo == 0)
    837     return 0;
    838   if (NumFiles == Table.getMCDwarfFiles().size())
    839     return FileNo;
    840 
    841   SmallString<128> FullPathName;
    842 
    843   if (!UseDwarfDirectory && !Directory.empty()) {
    844     if (sys::path::is_absolute(Filename))
    845       Directory = "";
    846     else {
    847       FullPathName = Directory;
    848       sys::path::append(FullPathName, Filename);
    849       Directory = "";
    850       Filename = FullPathName;
    851     }
    852   }
    853 
    854   OS << "\t.file\t" << FileNo << ' ';
    855   if (!Directory.empty()) {
    856     PrintQuotedString(Directory, OS);
    857     OS << ' ';
    858   }
    859   PrintQuotedString(Filename, OS);
    860   EmitEOL();
    861 
    862   return FileNo;
    863 }
    864 
    865 void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
    866                                           unsigned Column, unsigned Flags,
    867                                           unsigned Isa,
    868                                           unsigned Discriminator,
    869                                           StringRef FileName) {
    870   this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
    871                                           Isa, Discriminator, FileName);
    872   OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
    873   if (Flags & DWARF2_FLAG_BASIC_BLOCK)
    874     OS << " basic_block";
    875   if (Flags & DWARF2_FLAG_PROLOGUE_END)
    876     OS << " prologue_end";
    877   if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
    878     OS << " epilogue_begin";
    879 
    880   unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
    881   if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
    882     OS << " is_stmt ";
    883 
    884     if (Flags & DWARF2_FLAG_IS_STMT)
    885       OS << "1";
    886     else
    887       OS << "0";
    888   }
    889 
    890   if (Isa)
    891     OS << " isa " << Isa;
    892   if (Discriminator)
    893     OS << " discriminator " << Discriminator;
    894 
    895   if (IsVerboseAsm) {
    896     OS.PadToColumn(MAI->getCommentColumn());
    897     OS << MAI->getCommentString() << ' ' << FileName << ':'
    898        << Line << ':' << Column;
    899   }
    900   EmitEOL();
    901 }
    902 
    903 MCSymbol *MCAsmStreamer::getDwarfLineTableSymbol(unsigned CUID) {
    904   // Always use the zeroth line table, since asm syntax only supports one line
    905   // table for now.
    906   return MCStreamer::getDwarfLineTableSymbol(0);
    907 }
    908 
    909 void MCAsmStreamer::EmitIdent(StringRef IdentString) {
    910   assert(MAI->hasIdentDirective() && ".ident directive not supported");
    911   OS << "\t.ident\t";
    912   PrintQuotedString(IdentString, OS);
    913   EmitEOL();
    914 }
    915 
    916 void MCAsmStreamer::EmitCFISections(bool EH, bool Debug) {
    917   MCStreamer::EmitCFISections(EH, Debug);
    918   OS << "\t.cfi_sections ";
    919   if (EH) {
    920     OS << ".eh_frame";
    921     if (Debug)
    922       OS << ", .debug_frame";
    923   } else if (Debug) {
    924     OS << ".debug_frame";
    925   }
    926 
    927   EmitEOL();
    928 }
    929 
    930 void MCAsmStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
    931   OS << "\t.cfi_startproc";
    932   if (Frame.IsSimple)
    933     OS << " simple";
    934   EmitEOL();
    935 }
    936 
    937 void MCAsmStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
    938   MCStreamer::EmitCFIEndProcImpl(Frame);
    939   OS << "\t.cfi_endproc";
    940   EmitEOL();
    941 }
    942 
    943 void MCAsmStreamer::EmitRegisterName(int64_t Register) {
    944   if (InstPrinter && !MAI->useDwarfRegNumForCFI()) {
    945     const MCRegisterInfo *MRI = getContext().getRegisterInfo();
    946     unsigned LLVMRegister = MRI->getLLVMRegNum(Register, true);
    947     InstPrinter->printRegName(OS, LLVMRegister);
    948   } else {
    949     OS << Register;
    950   }
    951 }
    952 
    953 void MCAsmStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) {
    954   MCStreamer::EmitCFIDefCfa(Register, Offset);
    955   OS << "\t.cfi_def_cfa ";
    956   EmitRegisterName(Register);
    957   OS << ", " << Offset;
    958   EmitEOL();
    959 }
    960 
    961 void MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
    962   MCStreamer::EmitCFIDefCfaOffset(Offset);
    963   OS << "\t.cfi_def_cfa_offset " << Offset;
    964   EmitEOL();
    965 }
    966 
    967 void MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
    968   MCStreamer::EmitCFIDefCfaRegister(Register);
    969   OS << "\t.cfi_def_cfa_register ";
    970   EmitRegisterName(Register);
    971   EmitEOL();
    972 }
    973 
    974 void MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
    975   this->MCStreamer::EmitCFIOffset(Register, Offset);
    976   OS << "\t.cfi_offset ";
    977   EmitRegisterName(Register);
    978   OS << ", " << Offset;
    979   EmitEOL();
    980 }
    981 
    982 void MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym,
    983                                        unsigned Encoding) {
    984   MCStreamer::EmitCFIPersonality(Sym, Encoding);
    985   OS << "\t.cfi_personality " << Encoding << ", " << *Sym;
    986   EmitEOL();
    987 }
    988 
    989 void MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
    990   MCStreamer::EmitCFILsda(Sym, Encoding);
    991   OS << "\t.cfi_lsda " << Encoding << ", " << *Sym;
    992   EmitEOL();
    993 }
    994 
    995 void MCAsmStreamer::EmitCFIRememberState() {
    996   MCStreamer::EmitCFIRememberState();
    997   OS << "\t.cfi_remember_state";
    998   EmitEOL();
    999 }
   1000 
   1001 void MCAsmStreamer::EmitCFIRestoreState() {
   1002   MCStreamer::EmitCFIRestoreState();
   1003   OS << "\t.cfi_restore_state";
   1004   EmitEOL();
   1005 }
   1006 
   1007 void MCAsmStreamer::EmitCFISameValue(int64_t Register) {
   1008   MCStreamer::EmitCFISameValue(Register);
   1009   OS << "\t.cfi_same_value ";
   1010   EmitRegisterName(Register);
   1011   EmitEOL();
   1012 }
   1013 
   1014 void MCAsmStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) {
   1015   MCStreamer::EmitCFIRelOffset(Register, Offset);
   1016   OS << "\t.cfi_rel_offset ";
   1017   EmitRegisterName(Register);
   1018   OS << ", " << Offset;
   1019   EmitEOL();
   1020 }
   1021 
   1022 void MCAsmStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) {
   1023   MCStreamer::EmitCFIAdjustCfaOffset(Adjustment);
   1024   OS << "\t.cfi_adjust_cfa_offset " << Adjustment;
   1025   EmitEOL();
   1026 }
   1027 
   1028 void MCAsmStreamer::EmitCFISignalFrame() {
   1029   MCStreamer::EmitCFISignalFrame();
   1030   OS << "\t.cfi_signal_frame";
   1031   EmitEOL();
   1032 }
   1033 
   1034 void MCAsmStreamer::EmitCFIUndefined(int64_t Register) {
   1035   MCStreamer::EmitCFIUndefined(Register);
   1036   OS << "\t.cfi_undefined " << Register;
   1037   EmitEOL();
   1038 }
   1039 
   1040 void MCAsmStreamer::EmitCFIRegister(int64_t Register1, int64_t Register2) {
   1041   MCStreamer::EmitCFIRegister(Register1, Register2);
   1042   OS << "\t.cfi_register " << Register1 << ", " << Register2;
   1043   EmitEOL();
   1044 }
   1045 
   1046 void MCAsmStreamer::EmitCFIWindowSave() {
   1047   MCStreamer::EmitCFIWindowSave();
   1048   OS << "\t.cfi_window_save";
   1049   EmitEOL();
   1050 }
   1051 
   1052 void MCAsmStreamer::EmitWinCFIStartProc(const MCSymbol *Symbol) {
   1053   MCStreamer::EmitWinCFIStartProc(Symbol);
   1054 
   1055   OS << ".seh_proc " << *Symbol;
   1056   EmitEOL();
   1057 }
   1058 
   1059 void MCAsmStreamer::EmitWinCFIEndProc() {
   1060   MCStreamer::EmitWinCFIEndProc();
   1061 
   1062   OS << "\t.seh_endproc";
   1063   EmitEOL();
   1064 }
   1065 
   1066 void MCAsmStreamer::EmitWinCFIStartChained() {
   1067   MCStreamer::EmitWinCFIStartChained();
   1068 
   1069   OS << "\t.seh_startchained";
   1070   EmitEOL();
   1071 }
   1072 
   1073 void MCAsmStreamer::EmitWinCFIEndChained() {
   1074   MCStreamer::EmitWinCFIEndChained();
   1075 
   1076   OS << "\t.seh_endchained";
   1077   EmitEOL();
   1078 }
   1079 
   1080 void MCAsmStreamer::EmitWinEHHandler(const MCSymbol *Sym, bool Unwind,
   1081                                       bool Except) {
   1082   MCStreamer::EmitWinEHHandler(Sym, Unwind, Except);
   1083 
   1084   OS << "\t.seh_handler " << *Sym;
   1085   if (Unwind)
   1086     OS << ", @unwind";
   1087   if (Except)
   1088     OS << ", @except";
   1089   EmitEOL();
   1090 }
   1091 
   1092 static const MCSection *getWin64EHTableSection(StringRef suffix,
   1093                                                MCContext &context) {
   1094   // FIXME: This doesn't belong in MCObjectFileInfo. However,
   1095   /// this duplicate code in MCWin64EH.cpp.
   1096   if (suffix == "")
   1097     return context.getObjectFileInfo()->getXDataSection();
   1098   return context.getCOFFSection((".xdata"+suffix).str(),
   1099                                 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
   1100                                 COFF::IMAGE_SCN_MEM_READ |
   1101                                 COFF::IMAGE_SCN_MEM_WRITE,
   1102                                 SectionKind::getDataRel());
   1103 }
   1104 
   1105 void MCAsmStreamer::EmitWinEHHandlerData() {
   1106   MCStreamer::EmitWinEHHandlerData();
   1107 
   1108   // Switch sections. Don't call SwitchSection directly, because that will
   1109   // cause the section switch to be visible in the emitted assembly.
   1110   // We only do this so the section switch that terminates the handler
   1111   // data block is visible.
   1112   MCWin64EHUnwindInfo *CurFrame = getCurrentW64UnwindInfo();
   1113   StringRef suffix=MCWin64EHUnwindEmitter::GetSectionSuffix(CurFrame->Function);
   1114   const MCSection *xdataSect = getWin64EHTableSection(suffix, getContext());
   1115   if (xdataSect)
   1116     SwitchSectionNoChange(xdataSect);
   1117 
   1118   OS << "\t.seh_handlerdata";
   1119   EmitEOL();
   1120 }
   1121 
   1122 void MCAsmStreamer::EmitWinCFIPushReg(unsigned Register) {
   1123   MCStreamer::EmitWinCFIPushReg(Register);
   1124 
   1125   OS << "\t.seh_pushreg " << Register;
   1126   EmitEOL();
   1127 }
   1128 
   1129 void MCAsmStreamer::EmitWinCFISetFrame(unsigned Register, unsigned Offset) {
   1130   MCStreamer::EmitWinCFISetFrame(Register, Offset);
   1131 
   1132   OS << "\t.seh_setframe " << Register << ", " << Offset;
   1133   EmitEOL();
   1134 }
   1135 
   1136 void MCAsmStreamer::EmitWinCFIAllocStack(unsigned Size) {
   1137   MCStreamer::EmitWinCFIAllocStack(Size);
   1138 
   1139   OS << "\t.seh_stackalloc " << Size;
   1140   EmitEOL();
   1141 }
   1142 
   1143 void MCAsmStreamer::EmitWinCFISaveReg(unsigned Register, unsigned Offset) {
   1144   MCStreamer::EmitWinCFISaveReg(Register, Offset);
   1145 
   1146   OS << "\t.seh_savereg " << Register << ", " << Offset;
   1147   EmitEOL();
   1148 }
   1149 
   1150 void MCAsmStreamer::EmitWinCFISaveXMM(unsigned Register, unsigned Offset) {
   1151   MCStreamer::EmitWinCFISaveXMM(Register, Offset);
   1152 
   1153   OS << "\t.seh_savexmm " << Register << ", " << Offset;
   1154   EmitEOL();
   1155 }
   1156 
   1157 void MCAsmStreamer::EmitWinCFIPushFrame(bool Code) {
   1158   MCStreamer::EmitWinCFIPushFrame(Code);
   1159 
   1160   OS << "\t.seh_pushframe";
   1161   if (Code)
   1162     OS << " @code";
   1163   EmitEOL();
   1164 }
   1165 
   1166 void MCAsmStreamer::EmitWinCFIEndProlog(void) {
   1167   MCStreamer::EmitWinCFIEndProlog();
   1168 
   1169   OS << "\t.seh_endprologue";
   1170   EmitEOL();
   1171 }
   1172 
   1173 void MCAsmStreamer::AddEncodingComment(const MCInst &Inst,
   1174                                        const MCSubtargetInfo &STI) {
   1175   raw_ostream &OS = GetCommentOS();
   1176   SmallString<256> Code;
   1177   SmallVector<MCFixup, 4> Fixups;
   1178   raw_svector_ostream VecOS(Code);
   1179   Emitter->EncodeInstruction(Inst, VecOS, Fixups, STI);
   1180   VecOS.flush();
   1181 
   1182   // If we are showing fixups, create symbolic markers in the encoded
   1183   // representation. We do this by making a per-bit map to the fixup item index,
   1184   // then trying to display it as nicely as possible.
   1185   SmallVector<uint8_t, 64> FixupMap;
   1186   FixupMap.resize(Code.size() * 8);
   1187   for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
   1188     FixupMap[i] = 0;
   1189 
   1190   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
   1191     MCFixup &F = Fixups[i];
   1192     const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
   1193     for (unsigned j = 0; j != Info.TargetSize; ++j) {
   1194       unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
   1195       assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
   1196       FixupMap[Index] = 1 + i;
   1197     }
   1198   }
   1199 
   1200   // FIXME: Note the fixup comments for Thumb2 are completely bogus since the
   1201   // high order halfword of a 32-bit Thumb2 instruction is emitted first.
   1202   OS << "encoding: [";
   1203   for (unsigned i = 0, e = Code.size(); i != e; ++i) {
   1204     if (i)
   1205       OS << ',';
   1206 
   1207     // See if all bits are the same map entry.
   1208     uint8_t MapEntry = FixupMap[i * 8 + 0];
   1209     for (unsigned j = 1; j != 8; ++j) {
   1210       if (FixupMap[i * 8 + j] == MapEntry)
   1211         continue;
   1212 
   1213       MapEntry = uint8_t(~0U);
   1214       break;
   1215     }
   1216 
   1217     if (MapEntry != uint8_t(~0U)) {
   1218       if (MapEntry == 0) {
   1219         OS << format("0x%02x", uint8_t(Code[i]));
   1220       } else {
   1221         if (Code[i]) {
   1222           // FIXME: Some of the 8 bits require fix up.
   1223           OS << format("0x%02x", uint8_t(Code[i])) << '\''
   1224              << char('A' + MapEntry - 1) << '\'';
   1225         } else
   1226           OS << char('A' + MapEntry - 1);
   1227       }
   1228     } else {
   1229       // Otherwise, write out in binary.
   1230       OS << "0b";
   1231       for (unsigned j = 8; j--;) {
   1232         unsigned Bit = (Code[i] >> j) & 1;
   1233 
   1234         unsigned FixupBit;
   1235         if (MAI->isLittleEndian())
   1236           FixupBit = i * 8 + j;
   1237         else
   1238           FixupBit = i * 8 + (7-j);
   1239 
   1240         if (uint8_t MapEntry = FixupMap[FixupBit]) {
   1241           assert(Bit == 0 && "Encoder wrote into fixed up bit!");
   1242           OS << char('A' + MapEntry - 1);
   1243         } else
   1244           OS << Bit;
   1245       }
   1246     }
   1247   }
   1248   OS << "]\n";
   1249 
   1250   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
   1251     MCFixup &F = Fixups[i];
   1252     const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
   1253     OS << "  fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
   1254        << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
   1255   }
   1256 }
   1257 
   1258 void MCAsmStreamer::EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) {
   1259   assert(getCurrentSection().first &&
   1260          "Cannot emit contents before setting section!");
   1261 
   1262   // Show the encoding in a comment if we have a code emitter.
   1263   if (Emitter)
   1264     AddEncodingComment(Inst, STI);
   1265 
   1266   // Show the MCInst if enabled.
   1267   if (ShowInst) {
   1268     Inst.dump_pretty(GetCommentOS(), MAI, InstPrinter.get(), "\n ");
   1269     GetCommentOS() << "\n";
   1270   }
   1271 
   1272   // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
   1273   if (InstPrinter)
   1274     InstPrinter->printInst(&Inst, OS, "");
   1275   else
   1276     Inst.print(OS, MAI);
   1277   EmitEOL();
   1278 }
   1279 
   1280 void MCAsmStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
   1281   OS << "\t.bundle_align_mode " << AlignPow2;
   1282   EmitEOL();
   1283 }
   1284 
   1285 void MCAsmStreamer::EmitBundleLock(bool AlignToEnd) {
   1286   OS << "\t.bundle_lock";
   1287   if (AlignToEnd)
   1288     OS << " align_to_end";
   1289   EmitEOL();
   1290 }
   1291 
   1292 void MCAsmStreamer::EmitBundleUnlock() {
   1293   OS << "\t.bundle_unlock";
   1294   EmitEOL();
   1295 }
   1296 
   1297 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
   1298 /// the specified string in the output .s file.  This capability is
   1299 /// indicated by the hasRawTextSupport() predicate.
   1300 void MCAsmStreamer::EmitRawTextImpl(StringRef String) {
   1301   if (!String.empty() && String.back() == '\n')
   1302     String = String.substr(0, String.size()-1);
   1303   OS << String;
   1304   EmitEOL();
   1305 }
   1306 
   1307 void MCAsmStreamer::FinishImpl() {
   1308   // If we are generating dwarf for assembly source files dump out the sections.
   1309   if (getContext().getGenDwarfForAssembly())
   1310     MCGenDwarfInfo::Emit(this);
   1311 
   1312   // Emit the label for the line table, if requested - since the rest of the
   1313   // line table will be defined by .loc/.file directives, and not emitted
   1314   // directly, the label is the only work required here.
   1315   auto &Tables = getContext().getMCDwarfLineTables();
   1316   if (!Tables.empty()) {
   1317     assert(Tables.size() == 1 && "asm output only supports one line table");
   1318     if (auto *Label = Tables.begin()->second.getLabel()) {
   1319       SwitchSection(getContext().getObjectFileInfo()->getDwarfLineSection());
   1320       EmitLabel(Label);
   1321     }
   1322   }
   1323 }
   1324 
   1325 MCStreamer *llvm::createAsmStreamer(MCContext &Context,
   1326                                     formatted_raw_ostream &OS,
   1327                                     bool isVerboseAsm, bool useDwarfDirectory,
   1328                                     MCInstPrinter *IP, MCCodeEmitter *CE,
   1329                                     MCAsmBackend *MAB, bool ShowInst) {
   1330   return new MCAsmStreamer(Context, OS, isVerboseAsm, useDwarfDirectory, IP, CE,
   1331                            MAB, ShowInst);
   1332 }
   1333