Clean up static analyzer warnings.
[oota-llvm.git] / lib / MC / MCAsmStreamer.cpp
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 << MAI->getWeakDirective(); 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       uint64_t Shift = 64 - EmissionSize * 8;
686       assert(Shift < std::numeric_limits<unsigned long long>::digits &&
687              "undefined behavior");
688       ValueToEmit &= ~0ULL >> Shift;
689       EmitIntValue(ValueToEmit, EmissionSize);
690       Emitted += EmissionSize;
691     }
692     return;
693   }
694
695   assert(Directive && "Invalid size for machine code value!");
696   OS << Directive << *Value;
697   EmitEOL();
698 }
699
700 void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value) {
701   int64_t IntValue;
702   if (Value->EvaluateAsAbsolute(IntValue)) {
703     EmitULEB128IntValue(IntValue);
704     return;
705   }
706   OS << ".uleb128 " << *Value;
707   EmitEOL();
708 }
709
710 void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value) {
711   int64_t IntValue;
712   if (Value->EvaluateAsAbsolute(IntValue)) {
713     EmitSLEB128IntValue(IntValue);
714     return;
715   }
716   OS << ".sleb128 " << *Value;
717   EmitEOL();
718 }
719
720 void MCAsmStreamer::EmitGPRel64Value(const MCExpr *Value) {
721   assert(MAI->getGPRel64Directive() != nullptr);
722   OS << MAI->getGPRel64Directive() << *Value;
723   EmitEOL();
724 }
725
726 void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
727   assert(MAI->getGPRel32Directive() != nullptr);
728   OS << MAI->getGPRel32Directive() << *Value;
729   EmitEOL();
730 }
731
732
733 /// EmitFill - Emit NumBytes bytes worth of the value specified by
734 /// FillValue.  This implements directives such as '.space'.
735 void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) {
736   if (NumBytes == 0) return;
737
738   if (const char *ZeroDirective = MAI->getZeroDirective()) {
739     OS << ZeroDirective << NumBytes;
740     if (FillValue != 0)
741       OS << ',' << (int)FillValue;
742     EmitEOL();
743     return;
744   }
745
746   // Emit a byte at a time.
747   MCStreamer::EmitFill(NumBytes, FillValue);
748 }
749
750 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
751                                          unsigned ValueSize,
752                                          unsigned MaxBytesToEmit) {
753   // Some assemblers don't support non-power of two alignments, so we always
754   // emit alignments as a power of two if possible.
755   if (isPowerOf2_32(ByteAlignment)) {
756     switch (ValueSize) {
757     default:
758       llvm_unreachable("Invalid size for machine code value!");
759     case 1:
760       OS << "\t.align\t";
761       break;
762     case 2:
763       OS << ".p2alignw ";
764       break;
765     case 4:
766       OS << ".p2alignl ";
767       break;
768     case 8:
769       llvm_unreachable("Unsupported alignment size!");
770     }
771
772     if (MAI->getAlignmentIsInBytes())
773       OS << ByteAlignment;
774     else
775       OS << Log2_32(ByteAlignment);
776
777     if (Value || MaxBytesToEmit) {
778       OS << ", 0x";
779       OS.write_hex(truncateToSize(Value, ValueSize));
780
781       if (MaxBytesToEmit)
782         OS << ", " << MaxBytesToEmit;
783     }
784     EmitEOL();
785     return;
786   }
787
788   // Non-power of two alignment.  This is not widely supported by assemblers.
789   // FIXME: Parameterize this based on MAI.
790   switch (ValueSize) {
791   default: llvm_unreachable("Invalid size for machine code value!");
792   case 1: OS << ".balign";  break;
793   case 2: OS << ".balignw"; break;
794   case 4: OS << ".balignl"; break;
795   case 8: llvm_unreachable("Unsupported alignment size!");
796   }
797
798   OS << ' ' << ByteAlignment;
799   OS << ", " << truncateToSize(Value, ValueSize);
800   if (MaxBytesToEmit)
801     OS << ", " << MaxBytesToEmit;
802   EmitEOL();
803 }
804
805 void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
806                                       unsigned MaxBytesToEmit) {
807   // Emit with a text fill value.
808   EmitValueToAlignment(ByteAlignment, MAI->getTextAlignFillValue(),
809                        1, MaxBytesToEmit);
810 }
811
812 bool MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
813                                       unsigned char Value) {
814   // FIXME: Verify that Offset is associated with the current section.
815   OS << ".org " << *Offset << ", " << (unsigned) Value;
816   EmitEOL();
817   return false;
818 }
819
820
821 void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
822   assert(MAI->hasSingleParameterDotFile());
823   OS << "\t.file\t";
824   PrintQuotedString(Filename, OS);
825   EmitEOL();
826 }
827
828 unsigned MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo,
829                                                StringRef Directory,
830                                                StringRef Filename,
831                                                unsigned CUID) {
832   assert(CUID == 0);
833
834   MCDwarfLineTable &Table = getContext().getMCDwarfLineTable(CUID);
835   unsigned NumFiles = Table.getMCDwarfFiles().size();
836   FileNo = Table.getFile(Directory, Filename, FileNo);
837   if (FileNo == 0)
838     return 0;
839   if (NumFiles == Table.getMCDwarfFiles().size())
840     return FileNo;
841
842   SmallString<128> FullPathName;
843
844   if (!UseDwarfDirectory && !Directory.empty()) {
845     if (sys::path::is_absolute(Filename))
846       Directory = "";
847     else {
848       FullPathName = Directory;
849       sys::path::append(FullPathName, Filename);
850       Directory = "";
851       Filename = FullPathName;
852     }
853   }
854
855   OS << "\t.file\t" << FileNo << ' ';
856   if (!Directory.empty()) {
857     PrintQuotedString(Directory, OS);
858     OS << ' ';
859   }
860   PrintQuotedString(Filename, OS);
861   EmitEOL();
862
863   return FileNo;
864 }
865
866 void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
867                                           unsigned Column, unsigned Flags,
868                                           unsigned Isa,
869                                           unsigned Discriminator,
870                                           StringRef FileName) {
871   this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
872                                           Isa, Discriminator, FileName);
873   OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
874   if (Flags & DWARF2_FLAG_BASIC_BLOCK)
875     OS << " basic_block";
876   if (Flags & DWARF2_FLAG_PROLOGUE_END)
877     OS << " prologue_end";
878   if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
879     OS << " epilogue_begin";
880
881   unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
882   if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
883     OS << " is_stmt ";
884
885     if (Flags & DWARF2_FLAG_IS_STMT)
886       OS << "1";
887     else
888       OS << "0";
889   }
890
891   if (Isa)
892     OS << " isa " << Isa;
893   if (Discriminator)
894     OS << " discriminator " << Discriminator;
895
896   if (IsVerboseAsm) {
897     OS.PadToColumn(MAI->getCommentColumn());
898     OS << MAI->getCommentString() << ' ' << FileName << ':'
899        << Line << ':' << Column;
900   }
901   EmitEOL();
902 }
903
904 MCSymbol *MCAsmStreamer::getDwarfLineTableSymbol(unsigned CUID) {
905   // Always use the zeroth line table, since asm syntax only supports one line
906   // table for now.
907   return MCStreamer::getDwarfLineTableSymbol(0);
908 }
909
910 void MCAsmStreamer::EmitIdent(StringRef IdentString) {
911   assert(MAI->hasIdentDirective() && ".ident directive not supported");
912   OS << "\t.ident\t";
913   PrintQuotedString(IdentString, OS);
914   EmitEOL();
915 }
916
917 void MCAsmStreamer::EmitCFISections(bool EH, bool Debug) {
918   MCStreamer::EmitCFISections(EH, Debug);
919   OS << "\t.cfi_sections ";
920   if (EH) {
921     OS << ".eh_frame";
922     if (Debug)
923       OS << ", .debug_frame";
924   } else if (Debug) {
925     OS << ".debug_frame";
926   }
927
928   EmitEOL();
929 }
930
931 void MCAsmStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
932   OS << "\t.cfi_startproc";
933   if (Frame.IsSimple)
934     OS << " simple";
935   EmitEOL();
936 }
937
938 void MCAsmStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
939   MCStreamer::EmitCFIEndProcImpl(Frame);
940   OS << "\t.cfi_endproc";
941   EmitEOL();
942 }
943
944 void MCAsmStreamer::EmitRegisterName(int64_t Register) {
945   if (InstPrinter && !MAI->useDwarfRegNumForCFI()) {
946     const MCRegisterInfo *MRI = getContext().getRegisterInfo();
947     unsigned LLVMRegister = MRI->getLLVMRegNum(Register, true);
948     InstPrinter->printRegName(OS, LLVMRegister);
949   } else {
950     OS << Register;
951   }
952 }
953
954 void MCAsmStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) {
955   MCStreamer::EmitCFIDefCfa(Register, Offset);
956   OS << "\t.cfi_def_cfa ";
957   EmitRegisterName(Register);
958   OS << ", " << Offset;
959   EmitEOL();
960 }
961
962 void MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
963   MCStreamer::EmitCFIDefCfaOffset(Offset);
964   OS << "\t.cfi_def_cfa_offset " << Offset;
965   EmitEOL();
966 }
967
968 void MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
969   MCStreamer::EmitCFIDefCfaRegister(Register);
970   OS << "\t.cfi_def_cfa_register ";
971   EmitRegisterName(Register);
972   EmitEOL();
973 }
974
975 void MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
976   this->MCStreamer::EmitCFIOffset(Register, Offset);
977   OS << "\t.cfi_offset ";
978   EmitRegisterName(Register);
979   OS << ", " << Offset;
980   EmitEOL();
981 }
982
983 void MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym,
984                                        unsigned Encoding) {
985   MCStreamer::EmitCFIPersonality(Sym, Encoding);
986   OS << "\t.cfi_personality " << Encoding << ", " << *Sym;
987   EmitEOL();
988 }
989
990 void MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
991   MCStreamer::EmitCFILsda(Sym, Encoding);
992   OS << "\t.cfi_lsda " << Encoding << ", " << *Sym;
993   EmitEOL();
994 }
995
996 void MCAsmStreamer::EmitCFIRememberState() {
997   MCStreamer::EmitCFIRememberState();
998   OS << "\t.cfi_remember_state";
999   EmitEOL();
1000 }
1001
1002 void MCAsmStreamer::EmitCFIRestoreState() {
1003   MCStreamer::EmitCFIRestoreState();
1004   OS << "\t.cfi_restore_state";
1005   EmitEOL();
1006 }
1007
1008 void MCAsmStreamer::EmitCFISameValue(int64_t Register) {
1009   MCStreamer::EmitCFISameValue(Register);
1010   OS << "\t.cfi_same_value ";
1011   EmitRegisterName(Register);
1012   EmitEOL();
1013 }
1014
1015 void MCAsmStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) {
1016   MCStreamer::EmitCFIRelOffset(Register, Offset);
1017   OS << "\t.cfi_rel_offset ";
1018   EmitRegisterName(Register);
1019   OS << ", " << Offset;
1020   EmitEOL();
1021 }
1022
1023 void MCAsmStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) {
1024   MCStreamer::EmitCFIAdjustCfaOffset(Adjustment);
1025   OS << "\t.cfi_adjust_cfa_offset " << Adjustment;
1026   EmitEOL();
1027 }
1028
1029 void MCAsmStreamer::EmitCFISignalFrame() {
1030   MCStreamer::EmitCFISignalFrame();
1031   OS << "\t.cfi_signal_frame";
1032   EmitEOL();
1033 }
1034
1035 void MCAsmStreamer::EmitCFIUndefined(int64_t Register) {
1036   MCStreamer::EmitCFIUndefined(Register);
1037   OS << "\t.cfi_undefined " << Register;
1038   EmitEOL();
1039 }
1040
1041 void MCAsmStreamer::EmitCFIRegister(int64_t Register1, int64_t Register2) {
1042   MCStreamer::EmitCFIRegister(Register1, Register2);
1043   OS << "\t.cfi_register " << Register1 << ", " << Register2;
1044   EmitEOL();
1045 }
1046
1047 void MCAsmStreamer::EmitCFIWindowSave() {
1048   MCStreamer::EmitCFIWindowSave();
1049   OS << "\t.cfi_window_save";
1050   EmitEOL();
1051 }
1052
1053 void MCAsmStreamer::EmitWinCFIStartProc(const MCSymbol *Symbol) {
1054   MCStreamer::EmitWinCFIStartProc(Symbol);
1055
1056   OS << ".seh_proc " << *Symbol;
1057   EmitEOL();
1058 }
1059
1060 void MCAsmStreamer::EmitWinCFIEndProc() {
1061   MCStreamer::EmitWinCFIEndProc();
1062
1063   OS << "\t.seh_endproc";
1064   EmitEOL();
1065 }
1066
1067 void MCAsmStreamer::EmitWinCFIStartChained() {
1068   MCStreamer::EmitWinCFIStartChained();
1069
1070   OS << "\t.seh_startchained";
1071   EmitEOL();
1072 }
1073
1074 void MCAsmStreamer::EmitWinCFIEndChained() {
1075   MCStreamer::EmitWinCFIEndChained();
1076
1077   OS << "\t.seh_endchained";
1078   EmitEOL();
1079 }
1080
1081 void MCAsmStreamer::EmitWinEHHandler(const MCSymbol *Sym, bool Unwind,
1082                                       bool Except) {
1083   MCStreamer::EmitWinEHHandler(Sym, Unwind, Except);
1084
1085   OS << "\t.seh_handler " << *Sym;
1086   if (Unwind)
1087     OS << ", @unwind";
1088   if (Except)
1089     OS << ", @except";
1090   EmitEOL();
1091 }
1092
1093 void MCAsmStreamer::EmitWinEHHandlerData() {
1094   MCStreamer::EmitWinEHHandlerData();
1095
1096   // Switch sections. Don't call SwitchSection directly, because that will
1097   // cause the section switch to be visible in the emitted assembly.
1098   // We only do this so the section switch that terminates the handler
1099   // data block is visible.
1100   WinEH::FrameInfo *CurFrame = getCurrentWinFrameInfo();
1101   if (const MCSection *XData = WinEH::UnwindEmitter::getXDataSection(
1102           CurFrame->Function, getContext()))
1103     SwitchSectionNoChange(XData);
1104
1105   OS << "\t.seh_handlerdata";
1106   EmitEOL();
1107 }
1108
1109 void MCAsmStreamer::EmitWinCFIPushReg(unsigned Register) {
1110   MCStreamer::EmitWinCFIPushReg(Register);
1111
1112   OS << "\t.seh_pushreg " << Register;
1113   EmitEOL();
1114 }
1115
1116 void MCAsmStreamer::EmitWinCFISetFrame(unsigned Register, unsigned Offset) {
1117   MCStreamer::EmitWinCFISetFrame(Register, Offset);
1118
1119   OS << "\t.seh_setframe " << Register << ", " << Offset;
1120   EmitEOL();
1121 }
1122
1123 void MCAsmStreamer::EmitWinCFIAllocStack(unsigned Size) {
1124   MCStreamer::EmitWinCFIAllocStack(Size);
1125
1126   OS << "\t.seh_stackalloc " << Size;
1127   EmitEOL();
1128 }
1129
1130 void MCAsmStreamer::EmitWinCFISaveReg(unsigned Register, unsigned Offset) {
1131   MCStreamer::EmitWinCFISaveReg(Register, Offset);
1132
1133   OS << "\t.seh_savereg " << Register << ", " << Offset;
1134   EmitEOL();
1135 }
1136
1137 void MCAsmStreamer::EmitWinCFISaveXMM(unsigned Register, unsigned Offset) {
1138   MCStreamer::EmitWinCFISaveXMM(Register, Offset);
1139
1140   OS << "\t.seh_savexmm " << Register << ", " << Offset;
1141   EmitEOL();
1142 }
1143
1144 void MCAsmStreamer::EmitWinCFIPushFrame(bool Code) {
1145   MCStreamer::EmitWinCFIPushFrame(Code);
1146
1147   OS << "\t.seh_pushframe";
1148   if (Code)
1149     OS << " @code";
1150   EmitEOL();
1151 }
1152
1153 void MCAsmStreamer::EmitWinCFIEndProlog(void) {
1154   MCStreamer::EmitWinCFIEndProlog();
1155
1156   OS << "\t.seh_endprologue";
1157   EmitEOL();
1158 }
1159
1160 void MCAsmStreamer::AddEncodingComment(const MCInst &Inst,
1161                                        const MCSubtargetInfo &STI) {
1162   raw_ostream &OS = GetCommentOS();
1163   SmallString<256> Code;
1164   SmallVector<MCFixup, 4> Fixups;
1165   raw_svector_ostream VecOS(Code);
1166   Emitter->EncodeInstruction(Inst, VecOS, Fixups, STI);
1167   VecOS.flush();
1168
1169   // If we are showing fixups, create symbolic markers in the encoded
1170   // representation. We do this by making a per-bit map to the fixup item index,
1171   // then trying to display it as nicely as possible.
1172   SmallVector<uint8_t, 64> FixupMap;
1173   FixupMap.resize(Code.size() * 8);
1174   for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
1175     FixupMap[i] = 0;
1176
1177   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1178     MCFixup &F = Fixups[i];
1179     const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
1180     for (unsigned j = 0; j != Info.TargetSize; ++j) {
1181       unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
1182       assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
1183       FixupMap[Index] = 1 + i;
1184     }
1185   }
1186
1187   // FIXME: Note the fixup comments for Thumb2 are completely bogus since the
1188   // high order halfword of a 32-bit Thumb2 instruction is emitted first.
1189   OS << "encoding: [";
1190   for (unsigned i = 0, e = Code.size(); i != e; ++i) {
1191     if (i)
1192       OS << ',';
1193
1194     // See if all bits are the same map entry.
1195     uint8_t MapEntry = FixupMap[i * 8 + 0];
1196     for (unsigned j = 1; j != 8; ++j) {
1197       if (FixupMap[i * 8 + j] == MapEntry)
1198         continue;
1199
1200       MapEntry = uint8_t(~0U);
1201       break;
1202     }
1203
1204     if (MapEntry != uint8_t(~0U)) {
1205       if (MapEntry == 0) {
1206         OS << format("0x%02x", uint8_t(Code[i]));
1207       } else {
1208         if (Code[i]) {
1209           // FIXME: Some of the 8 bits require fix up.
1210           OS << format("0x%02x", uint8_t(Code[i])) << '\''
1211              << char('A' + MapEntry - 1) << '\'';
1212         } else
1213           OS << char('A' + MapEntry - 1);
1214       }
1215     } else {
1216       // Otherwise, write out in binary.
1217       OS << "0b";
1218       for (unsigned j = 8; j--;) {
1219         unsigned Bit = (Code[i] >> j) & 1;
1220
1221         unsigned FixupBit;
1222         if (MAI->isLittleEndian())
1223           FixupBit = i * 8 + j;
1224         else
1225           FixupBit = i * 8 + (7-j);
1226
1227         if (uint8_t MapEntry = FixupMap[FixupBit]) {
1228           assert(Bit == 0 && "Encoder wrote into fixed up bit!");
1229           OS << char('A' + MapEntry - 1);
1230         } else
1231           OS << Bit;
1232       }
1233     }
1234   }
1235   OS << "]\n";
1236
1237   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1238     MCFixup &F = Fixups[i];
1239     const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
1240     OS << "  fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
1241        << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
1242   }
1243 }
1244
1245 void MCAsmStreamer::EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) {
1246   assert(getCurrentSection().first &&
1247          "Cannot emit contents before setting section!");
1248
1249   // Show the encoding in a comment if we have a code emitter.
1250   if (Emitter)
1251     AddEncodingComment(Inst, STI);
1252
1253   // Show the MCInst if enabled.
1254   if (ShowInst) {
1255     Inst.dump_pretty(GetCommentOS(), MAI, InstPrinter.get(), "\n ");
1256     GetCommentOS() << "\n";
1257   }
1258
1259   // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
1260   if (InstPrinter)
1261     InstPrinter->printInst(&Inst, OS, "");
1262   else
1263     Inst.print(OS, MAI);
1264   EmitEOL();
1265 }
1266
1267 void MCAsmStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
1268   OS << "\t.bundle_align_mode " << AlignPow2;
1269   EmitEOL();
1270 }
1271
1272 void MCAsmStreamer::EmitBundleLock(bool AlignToEnd) {
1273   OS << "\t.bundle_lock";
1274   if (AlignToEnd)
1275     OS << " align_to_end";
1276   EmitEOL();
1277 }
1278
1279 void MCAsmStreamer::EmitBundleUnlock() {
1280   OS << "\t.bundle_unlock";
1281   EmitEOL();
1282 }
1283
1284 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
1285 /// the specified string in the output .s file.  This capability is
1286 /// indicated by the hasRawTextSupport() predicate.
1287 void MCAsmStreamer::EmitRawTextImpl(StringRef String) {
1288   if (!String.empty() && String.back() == '\n')
1289     String = String.substr(0, String.size()-1);
1290   OS << String;
1291   EmitEOL();
1292 }
1293
1294 void MCAsmStreamer::FinishImpl() {
1295   // If we are generating dwarf for assembly source files dump out the sections.
1296   if (getContext().getGenDwarfForAssembly())
1297     MCGenDwarfInfo::Emit(this);
1298
1299   // Emit the label for the line table, if requested - since the rest of the
1300   // line table will be defined by .loc/.file directives, and not emitted
1301   // directly, the label is the only work required here.
1302   auto &Tables = getContext().getMCDwarfLineTables();
1303   if (!Tables.empty()) {
1304     assert(Tables.size() == 1 && "asm output only supports one line table");
1305     if (auto *Label = Tables.begin()->second.getLabel()) {
1306       SwitchSection(getContext().getObjectFileInfo()->getDwarfLineSection());
1307       EmitLabel(Label);
1308     }
1309   }
1310 }
1311
1312 MCStreamer *llvm::createAsmStreamer(MCContext &Context,
1313                                     formatted_raw_ostream &OS,
1314                                     bool isVerboseAsm, bool useDwarfDirectory,
1315                                     MCInstPrinter *IP, MCCodeEmitter *CE,
1316                                     MCAsmBackend *MAB, bool ShowInst) {
1317   return new MCAsmStreamer(Context, OS, isVerboseAsm, useDwarfDirectory, IP, CE,
1318                            MAB, ShowInst);
1319 }