]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/MC/MCAsmStreamer.cpp
Update LLVM to r98631.
[FreeBSD/FreeBSD.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/MC/MCAsmInfo.h"
12 #include "llvm/MC/MCCodeEmitter.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCInst.h"
16 #include "llvm/MC/MCInstPrinter.h"
17 #include "llvm/MC/MCSectionMachO.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Support/FormattedStream.h"
25 using namespace llvm;
26
27 namespace {
28
29 class MCAsmStreamer : public MCStreamer {
30   formatted_raw_ostream &OS;
31   const MCAsmInfo &MAI;
32   MCInstPrinter *InstPrinter;
33   MCCodeEmitter *Emitter;
34   
35   SmallString<128> CommentToEmit;
36   raw_svector_ostream CommentStream;
37
38   unsigned IsLittleEndian : 1;
39   unsigned IsVerboseAsm : 1;
40   unsigned ShowInst : 1;
41
42 public:
43   MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
44                 bool isLittleEndian, bool isVerboseAsm, MCInstPrinter *printer,
45                 MCCodeEmitter *emitter, bool showInst)
46     : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
47       InstPrinter(printer), Emitter(emitter), CommentStream(CommentToEmit),
48       IsLittleEndian(isLittleEndian), IsVerboseAsm(isVerboseAsm),
49       ShowInst(showInst) {
50     if (InstPrinter && IsVerboseAsm)
51       InstPrinter->setCommentStream(CommentStream);
52   }
53   ~MCAsmStreamer() {}
54
55   bool isLittleEndian() const { return IsLittleEndian; }
56
57   inline void EmitEOL() {
58     // If we don't have any comments, just emit a \n.
59     if (!IsVerboseAsm) {
60       OS << '\n';
61       return;
62     }
63     EmitCommentsAndEOL();
64   }
65   void EmitCommentsAndEOL();
66
67   /// isVerboseAsm - Return true if this streamer supports verbose assembly at
68   /// all.
69   virtual bool isVerboseAsm() const { return IsVerboseAsm; }
70
71   /// AddComment - Add a comment that can be emitted to the generated .s
72   /// file if applicable as a QoI issue to make the output of the compiler
73   /// more readable.  This only affects the MCAsmStreamer, and only when
74   /// verbose assembly output is enabled.
75   virtual void AddComment(const Twine &T);
76
77   /// AddEncodingComment - Add a comment showing the encoding of an instruction.
78   virtual void AddEncodingComment(const MCInst &Inst);
79
80   /// GetCommentOS - Return a raw_ostream that comments can be written to.
81   /// Unlike AddComment, you are required to terminate comments with \n if you
82   /// use this method.
83   virtual raw_ostream &GetCommentOS() {
84     if (!IsVerboseAsm)
85       return nulls();  // Discard comments unless in verbose asm mode.
86     return CommentStream;
87   }
88
89   /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
90   virtual void AddBlankLine() {
91     EmitEOL();
92   }
93
94   /// @name MCStreamer Interface
95   /// @{
96
97   virtual void SwitchSection(const MCSection *Section);
98
99   virtual void EmitLabel(MCSymbol *Symbol);
100
101   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
102
103   virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
104
105   virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
106
107   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
108
109   virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
110   virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
111                                 unsigned ByteAlignment);
112
113   /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
114   ///
115   /// @param Symbol - The common symbol to emit.
116   /// @param Size - The size of the common symbol.
117   virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
118   
119   virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
120                             unsigned Size = 0, unsigned ByteAlignment = 0);
121
122   virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
123
124   virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
125   virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
126   virtual void EmitGPRel32Value(const MCExpr *Value);
127   
128
129   virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
130                         unsigned AddrSpace);
131
132   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
133                                     unsigned ValueSize = 1,
134                                     unsigned MaxBytesToEmit = 0);
135
136   virtual void EmitCodeAlignment(unsigned ByteAlignment,
137                                  unsigned MaxBytesToEmit = 0);
138
139   virtual void EmitValueToOffset(const MCExpr *Offset,
140                                  unsigned char Value = 0);
141
142   virtual void EmitFileDirective(StringRef Filename);
143   virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
144
145   virtual void EmitInstruction(const MCInst &Inst);
146   
147   virtual void Finish();
148   
149   /// @}
150 };
151
152 } // end anonymous namespace.
153
154 /// AddComment - Add a comment that can be emitted to the generated .s
155 /// file if applicable as a QoI issue to make the output of the compiler
156 /// more readable.  This only affects the MCAsmStreamer, and only when
157 /// verbose assembly output is enabled.
158 void MCAsmStreamer::AddComment(const Twine &T) {
159   if (!IsVerboseAsm) return;
160   
161   // Make sure that CommentStream is flushed.
162   CommentStream.flush();
163   
164   T.toVector(CommentToEmit);
165   // Each comment goes on its own line.
166   CommentToEmit.push_back('\n');
167   
168   // Tell the comment stream that the vector changed underneath it.
169   CommentStream.resync();
170 }
171
172 void MCAsmStreamer::EmitCommentsAndEOL() {
173   if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
174     OS << '\n';
175     return;
176   }
177   
178   CommentStream.flush();
179   StringRef Comments = CommentToEmit.str();
180   
181   assert(Comments.back() == '\n' &&
182          "Comment array not newline terminated");
183   do {
184     // Emit a line of comments.
185     OS.PadToColumn(MAI.getCommentColumn());
186     size_t Position = Comments.find('\n');
187     OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
188     
189     Comments = Comments.substr(Position+1);
190   } while (!Comments.empty());
191   
192   CommentToEmit.clear();
193   // Tell the comment stream that the vector changed underneath it.
194   CommentStream.resync();
195 }
196
197
198 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
199   assert(Bytes && "Invalid size!");
200   return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
201 }
202
203 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
204   assert(Section && "Cannot switch to a null section!");
205   if (Section != CurSection) {
206     CurSection = Section;
207     Section->PrintSwitchToSection(MAI, OS);
208   }
209 }
210
211 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
212   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
213   assert(CurSection && "Cannot emit before setting section!");
214
215   OS << *Symbol << ":";
216   EmitEOL();
217   Symbol->setSection(*CurSection);
218 }
219
220 void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
221   switch (Flag) {
222   default: assert(0 && "Invalid flag!");
223   case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
224   }
225   EmitEOL();
226 }
227
228 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
229   // Only absolute symbols can be redefined.
230   assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
231          "Cannot define a symbol twice!");
232
233   OS << *Symbol << " = " << *Value;
234   EmitEOL();
235
236   // FIXME: Lift context changes into super class.
237   // FIXME: Set associated section.
238   Symbol->setValue(Value);
239 }
240
241 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
242                                         MCSymbolAttr Attribute) {
243   switch (Attribute) {
244   case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
245   case MCSA_ELF_TypeFunction:    /// .type _foo, STT_FUNC  # aka @function
246   case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
247   case MCSA_ELF_TypeObject:      /// .type _foo, STT_OBJECT  # aka @object
248   case MCSA_ELF_TypeTLS:         /// .type _foo, STT_TLS     # aka @tls_object
249   case MCSA_ELF_TypeCommon:      /// .type _foo, STT_COMMON  # aka @common
250   case MCSA_ELF_TypeNoType:      /// .type _foo, STT_NOTYPE  # aka @notype
251     assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
252     OS << "\t.type\t" << *Symbol << ','
253        << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
254     switch (Attribute) {
255     default: assert(0 && "Unknown ELF .type");
256     case MCSA_ELF_TypeFunction:    OS << "function"; break;
257     case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
258     case MCSA_ELF_TypeObject:      OS << "object"; break;
259     case MCSA_ELF_TypeTLS:         OS << "tls_object"; break;
260     case MCSA_ELF_TypeCommon:      OS << "common"; break;
261     case MCSA_ELF_TypeNoType:      OS << "no_type"; break;
262     }
263     EmitEOL();
264     return;
265   case MCSA_Global: // .globl/.global
266     OS << MAI.getGlobalDirective();
267     break;
268   case MCSA_Hidden:         OS << ".hidden ";          break;
269   case MCSA_IndirectSymbol: OS << ".indirect_symbol "; break;
270   case MCSA_Internal:       OS << ".internal ";        break;
271   case MCSA_LazyReference:  OS << ".lazy_reference ";  break;
272   case MCSA_Local:          OS << ".local ";           break;
273   case MCSA_NoDeadStrip:    OS << ".no_dead_strip ";   break;
274   case MCSA_PrivateExtern:  OS << ".private_extern ";  break;
275   case MCSA_Protected:      OS << ".protected ";       break;
276   case MCSA_Reference:      OS << ".reference ";       break;
277   case MCSA_Weak:           OS << ".weak ";            break;
278   case MCSA_WeakDefinition: OS << ".weak_definition "; break;
279       // .weak_reference
280   case MCSA_WeakReference:  OS << MAI.getWeakRefDirective(); break;
281   }
282
283   OS << *Symbol;
284   EmitEOL();
285 }
286
287 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
288   OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
289   EmitEOL();
290 }
291
292 void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
293   assert(MAI.hasDotTypeDotSizeDirective());
294   OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
295 }
296
297 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
298                                      unsigned ByteAlignment) {
299   OS << "\t.comm\t" << *Symbol << ',' << Size;
300   if (ByteAlignment != 0) {
301     if (MAI.getCOMMDirectiveAlignmentIsInBytes())
302       OS << ',' << ByteAlignment;
303     else
304       OS << ',' << Log2_32(ByteAlignment);
305   }
306   EmitEOL();
307 }
308
309 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
310 ///
311 /// @param Symbol - The common symbol to emit.
312 /// @param Size - The size of the common symbol.
313 void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
314   assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
315   OS << "\t.lcomm\t" << *Symbol << ',' << Size;
316   EmitEOL();
317 }
318
319 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
320                                  unsigned Size, unsigned ByteAlignment) {
321   // Note: a .zerofill directive does not switch sections.
322   OS << ".zerofill ";
323   
324   // This is a mach-o specific directive.
325   const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
326   OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
327   
328   if (Symbol != NULL) {
329     OS << ',' << *Symbol << ',' << Size;
330     if (ByteAlignment != 0)
331       OS << ',' << Log2_32(ByteAlignment);
332   }
333   EmitEOL();
334 }
335
336 static inline char toOctal(int X) { return (X&7)+'0'; }
337
338 static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
339   OS << '"';
340   
341   for (unsigned i = 0, e = Data.size(); i != e; ++i) {
342     unsigned char C = Data[i];
343     if (C == '"' || C == '\\') {
344       OS << '\\' << (char)C;
345       continue;
346     }
347     
348     if (isprint((unsigned char)C)) {
349       OS << (char)C;
350       continue;
351     }
352     
353     switch (C) {
354       case '\b': OS << "\\b"; break;
355       case '\f': OS << "\\f"; break;
356       case '\n': OS << "\\n"; break;
357       case '\r': OS << "\\r"; break;
358       case '\t': OS << "\\t"; break;
359       default:
360         OS << '\\';
361         OS << toOctal(C >> 6);
362         OS << toOctal(C >> 3);
363         OS << toOctal(C >> 0);
364         break;
365     }
366   }
367   
368   OS << '"';
369 }
370
371
372 void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
373   assert(CurSection && "Cannot emit contents before setting section!");
374   if (Data.empty()) return;
375   
376   if (Data.size() == 1) {
377     OS << MAI.getData8bitsDirective(AddrSpace);
378     OS << (unsigned)(unsigned char)Data[0];
379     EmitEOL();
380     return;
381   }
382
383   // If the data ends with 0 and the target supports .asciz, use it, otherwise
384   // use .ascii
385   if (MAI.getAscizDirective() && Data.back() == 0) {
386     OS << MAI.getAscizDirective();
387     Data = Data.substr(0, Data.size()-1);
388   } else {
389     OS << MAI.getAsciiDirective();
390   }
391
392   OS << ' ';
393   PrintQuotedString(Data, OS);
394   EmitEOL();
395 }
396
397 /// EmitIntValue - Special case of EmitValue that avoids the client having
398 /// to pass in a MCExpr for constant integers.
399 void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
400                                  unsigned AddrSpace) {
401   assert(CurSection && "Cannot emit contents before setting section!");
402   const char *Directive = 0;
403   switch (Size) {
404   default: break;
405   case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
406   case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
407   case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
408   case 8:
409     Directive = MAI.getData64bitsDirective(AddrSpace);
410     // If the target doesn't support 64-bit data, emit as two 32-bit halves.
411     if (Directive) break;
412     if (isLittleEndian()) {
413       EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
414       EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
415     } else {
416       EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
417       EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
418     }
419     return;
420   }
421   
422   assert(Directive && "Invalid size for machine code value!");
423   OS << Directive << truncateToSize(Value, Size);
424   EmitEOL();
425 }
426
427 void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
428                               unsigned AddrSpace) {
429   assert(CurSection && "Cannot emit contents before setting section!");
430   const char *Directive = 0;
431   switch (Size) {
432   default: break;
433   case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
434   case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
435   case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
436   case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
437   }
438   
439   assert(Directive && "Invalid size for machine code value!");
440   OS << Directive << *Value;
441   EmitEOL();
442 }
443
444 void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
445   assert(MAI.getGPRel32Directive() != 0);
446   OS << MAI.getGPRel32Directive() << *Value;
447   EmitEOL();
448 }
449
450
451 /// EmitFill - Emit NumBytes bytes worth of the value specified by
452 /// FillValue.  This implements directives such as '.space'.
453 void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
454                              unsigned AddrSpace) {
455   if (NumBytes == 0) return;
456   
457   if (AddrSpace == 0)
458     if (const char *ZeroDirective = MAI.getZeroDirective()) {
459       OS << ZeroDirective << NumBytes;
460       if (FillValue != 0)
461         OS << ',' << (int)FillValue;
462       EmitEOL();
463       return;
464     }
465
466   // Emit a byte at a time.
467   MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
468 }
469
470 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
471                                          unsigned ValueSize,
472                                          unsigned MaxBytesToEmit) {
473   // Some assemblers don't support non-power of two alignments, so we always
474   // emit alignments as a power of two if possible.
475   if (isPowerOf2_32(ByteAlignment)) {
476     switch (ValueSize) {
477     default: llvm_unreachable("Invalid size for machine code value!");
478     case 1: OS << MAI.getAlignDirective(); break;
479     // FIXME: use MAI for this!
480     case 2: OS << ".p2alignw "; break;
481     case 4: OS << ".p2alignl "; break;
482     case 8: llvm_unreachable("Unsupported alignment size!");
483     }
484     
485     if (MAI.getAlignmentIsInBytes())
486       OS << ByteAlignment;
487     else
488       OS << Log2_32(ByteAlignment);
489
490     if (Value || MaxBytesToEmit) {
491       OS << ", 0x";
492       OS.write_hex(truncateToSize(Value, ValueSize));
493
494       if (MaxBytesToEmit) 
495         OS << ", " << MaxBytesToEmit;
496     }
497     EmitEOL();
498     return;
499   }
500   
501   // Non-power of two alignment.  This is not widely supported by assemblers.
502   // FIXME: Parameterize this based on MAI.
503   switch (ValueSize) {
504   default: llvm_unreachable("Invalid size for machine code value!");
505   case 1: OS << ".balign";  break;
506   case 2: OS << ".balignw"; break;
507   case 4: OS << ".balignl"; break;
508   case 8: llvm_unreachable("Unsupported alignment size!");
509   }
510
511   OS << ' ' << ByteAlignment;
512   OS << ", " << truncateToSize(Value, ValueSize);
513   if (MaxBytesToEmit) 
514     OS << ", " << MaxBytesToEmit;
515   EmitEOL();
516 }
517
518 void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
519                                       unsigned MaxBytesToEmit) {
520   // Emit with a text fill value.
521   EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
522                        1, MaxBytesToEmit);
523 }
524
525 void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
526                                       unsigned char Value) {
527   // FIXME: Verify that Offset is associated with the current section.
528   OS << ".org " << *Offset << ", " << (unsigned) Value;
529   EmitEOL();
530 }
531
532
533 void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
534   assert(MAI.hasSingleParameterDotFile());
535   OS << "\t.file\t";
536   PrintQuotedString(Filename, OS);
537   EmitEOL();
538 }
539
540 void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
541   OS << "\t.file\t" << FileNo << ' ';
542   PrintQuotedString(Filename, OS);
543   EmitEOL();
544 }
545
546 void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
547   raw_ostream &OS = GetCommentOS();
548   SmallString<256> Code;
549   SmallVector<MCFixup, 4> Fixups;
550   raw_svector_ostream VecOS(Code);
551   Emitter->EncodeInstruction(Inst, VecOS, Fixups);
552   VecOS.flush();
553
554   // If we are showing fixups, create symbolic markers in the encoded
555   // representation. We do this by making a per-bit map to the fixup item index,
556   // then trying to display it as nicely as possible.
557   SmallVector<uint8_t, 64> FixupMap;
558   FixupMap.resize(Code.size() * 8);
559   for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
560     FixupMap[i] = 0;
561
562   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
563     MCFixup &F = Fixups[i];
564     const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
565     for (unsigned j = 0; j != Info.TargetSize; ++j) {
566       unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
567       assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
568       FixupMap[Index] = 1 + i;
569     }
570   }
571
572   OS << "encoding: [";
573   for (unsigned i = 0, e = Code.size(); i != e; ++i) {
574     if (i)
575       OS << ',';
576
577     // See if all bits are the same map entry.
578     uint8_t MapEntry = FixupMap[i * 8 + 0];
579     for (unsigned j = 1; j != 8; ++j) {
580       if (FixupMap[i * 8 + j] == MapEntry)
581         continue;
582
583       MapEntry = uint8_t(~0U);
584       break;
585     }
586
587     if (MapEntry != uint8_t(~0U)) {
588       if (MapEntry == 0) {
589         OS << format("0x%02x", uint8_t(Code[i]));
590       } else {
591         assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
592         OS << char('A' + MapEntry - 1);
593       }
594     } else {
595       // Otherwise, write out in binary.
596       OS << "0b";
597       for (unsigned j = 8; j--;) {
598         unsigned Bit = (Code[i] >> j) & 1;
599         if (uint8_t MapEntry = FixupMap[i * 8 + j]) {
600           assert(Bit == 0 && "Encoder wrote into fixed up bit!");
601           OS << char('A' + MapEntry - 1);
602         } else
603           OS << Bit;
604       }
605     }
606   }
607   OS << "]\n";
608
609   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
610     MCFixup &F = Fixups[i];
611     const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
612     OS << "  fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
613        << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
614   }
615 }
616
617 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
618   assert(CurSection && "Cannot emit contents before setting section!");
619
620   // Show the encoding in a comment if we have a code emitter.
621   if (Emitter)
622     AddEncodingComment(Inst);
623
624   // Show the MCInst if enabled.
625   if (ShowInst) {
626     raw_ostream &OS = GetCommentOS();
627     OS << "<MCInst #" << Inst.getOpcode();
628     
629     StringRef InstName;
630     if (InstPrinter)
631       InstName = InstPrinter->getOpcodeName(Inst.getOpcode());
632     if (!InstName.empty())
633       OS << ' ' << InstName;
634     
635     for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
636       OS << "\n  ";
637       Inst.getOperand(i).print(OS, &MAI);
638     }
639     OS << ">\n";
640   }
641   
642   // If we have an AsmPrinter, use that to print, otherwise dump the MCInst.
643   if (InstPrinter)
644     InstPrinter->printInst(&Inst);
645   else
646     Inst.print(OS, &MAI);
647   EmitEOL();
648 }
649
650 void MCAsmStreamer::Finish() {
651   OS.flush();
652 }
653
654 MCStreamer *llvm::createAsmStreamer(MCContext &Context,
655                                     formatted_raw_ostream &OS,
656                                     bool isLittleEndian,
657                                     bool isVerboseAsm, MCInstPrinter *IP,
658                                     MCCodeEmitter *CE, bool ShowInst) {
659   return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
660                            IP, CE, ShowInst);
661 }