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