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