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