1 //===- lib/MC/MCAsmStreamer.cpp - Text Assembly Output --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
30 class MCAsmStreamer : public MCStreamer {
31 formatted_raw_ostream &OS;
33 OwningPtr<MCInstPrinter> InstPrinter;
34 OwningPtr<MCCodeEmitter> Emitter;
36 SmallString<128> CommentToEmit;
37 raw_svector_ostream CommentStream;
39 unsigned IsLittleEndian : 1;
40 unsigned IsVerboseAsm : 1;
41 unsigned ShowInst : 1;
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),
51 if (InstPrinter && IsVerboseAsm)
52 InstPrinter->setCommentStream(CommentStream);
56 bool isLittleEndian() const { return IsLittleEndian; }
58 inline void EmitEOL() {
59 // If we don't have any comments, just emit a \n.
66 void EmitCommentsAndEOL();
68 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
70 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
72 /// hasRawTextSupport - We support EmitRawText.
73 virtual bool hasRawTextSupport() const { return true; }
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);
81 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
82 virtual void AddEncodingComment(const MCInst &Inst);
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
87 virtual raw_ostream &GetCommentOS() {
89 return nulls(); // Discard comments unless in verbose asm mode.
93 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
94 virtual void AddBlankLine() {
98 /// @name MCStreamer Interface
101 virtual void SwitchSection(const MCSection *Section);
103 virtual void InitSections() {
104 // FIXME, this is MachO specific, but the testsuite
106 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
107 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
108 0, SectionKind::getText()));
111 virtual void EmitLabel(MCSymbol *Symbol);
113 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
114 virtual void EmitThumbFunc(MCSymbol *Func);
116 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
117 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
119 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
121 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
122 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
123 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
124 virtual void EmitCOFFSymbolType(int Type);
125 virtual void EndCOFFSymbolDef();
126 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
127 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
128 unsigned ByteAlignment);
130 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
132 /// @param Symbol - The common symbol to emit.
133 /// @param Size - The size of the common symbol.
134 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
136 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
137 unsigned Size = 0, unsigned ByteAlignment = 0);
139 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
140 uint64_t Size, unsigned ByteAlignment = 0);
142 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
144 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
146 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
148 virtual void EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
150 virtual void EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
152 virtual void EmitGPRel32Value(const MCExpr *Value);
155 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
158 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
159 unsigned ValueSize = 1,
160 unsigned MaxBytesToEmit = 0);
162 virtual void EmitCodeAlignment(unsigned ByteAlignment,
163 unsigned MaxBytesToEmit = 0);
165 virtual void EmitValueToOffset(const MCExpr *Offset,
166 unsigned char Value = 0);
168 virtual void EmitFileDirective(StringRef Filename);
169 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
171 virtual void EmitInstruction(const MCInst &Inst);
173 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
174 /// the specified string in the output .s file. This capability is
175 /// indicated by the hasRawTextSupport() predicate.
176 virtual void EmitRawText(StringRef String);
178 virtual void Finish();
183 } // end anonymous namespace.
185 /// AddComment - Add a comment that can be emitted to the generated .s
186 /// file if applicable as a QoI issue to make the output of the compiler
187 /// more readable. This only affects the MCAsmStreamer, and only when
188 /// verbose assembly output is enabled.
189 void MCAsmStreamer::AddComment(const Twine &T) {
190 if (!IsVerboseAsm) return;
192 // Make sure that CommentStream is flushed.
193 CommentStream.flush();
195 T.toVector(CommentToEmit);
196 // Each comment goes on its own line.
197 CommentToEmit.push_back('\n');
199 // Tell the comment stream that the vector changed underneath it.
200 CommentStream.resync();
203 void MCAsmStreamer::EmitCommentsAndEOL() {
204 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
209 CommentStream.flush();
210 StringRef Comments = CommentToEmit.str();
212 assert(Comments.back() == '\n' &&
213 "Comment array not newline terminated");
215 // Emit a line of comments.
216 OS.PadToColumn(MAI.getCommentColumn());
217 size_t Position = Comments.find('\n');
218 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
220 Comments = Comments.substr(Position+1);
221 } while (!Comments.empty());
223 CommentToEmit.clear();
224 // Tell the comment stream that the vector changed underneath it.
225 CommentStream.resync();
228 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
229 assert(Bytes && "Invalid size!");
230 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
233 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
234 assert(Section && "Cannot switch to a null section!");
235 if (Section != CurSection) {
236 PrevSection = CurSection;
237 CurSection = Section;
238 Section->PrintSwitchToSection(MAI, OS);
242 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
243 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
244 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
245 assert(CurSection && "Cannot emit before setting section!");
247 OS << *Symbol << MAI.getLabelSuffix();
249 Symbol->setSection(*CurSection);
252 void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
254 default: assert(0 && "Invalid flag!");
255 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
256 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
257 case MCAF_Code16: OS << "\t.code\t16"; break;
258 case MCAF_Code32: OS << "\t.code\t32"; break;
263 void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
264 // This needs to emit to a temporary string to get properly quoted
265 // MCSymbols when they have spaces in them.
266 OS << "\t.thumb_func";
272 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
273 OS << *Symbol << " = " << *Value;
276 // FIXME: Lift context changes into super class.
277 Symbol->setVariableValue(Value);
280 void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
281 OS << ".weakref " << *Alias << ", " << *Symbol;
285 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
286 MCSymbolAttr Attribute) {
288 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
289 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
290 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
291 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
292 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
293 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
294 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
295 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
296 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
297 OS << "\t.type\t" << *Symbol << ','
298 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
300 default: assert(0 && "Unknown ELF .type");
301 case MCSA_ELF_TypeFunction: OS << "function"; break;
302 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
303 case MCSA_ELF_TypeObject: OS << "object"; break;
304 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
305 case MCSA_ELF_TypeCommon: OS << "common"; break;
306 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
307 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
311 case MCSA_Global: // .globl/.global
312 OS << MAI.getGlobalDirective();
314 case MCSA_Hidden: OS << "\t.hidden\t"; break;
315 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
316 case MCSA_Internal: OS << "\t.internal\t"; break;
317 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
318 case MCSA_Local: OS << "\t.local\t"; break;
319 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
320 case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break;
321 case MCSA_Protected: OS << "\t.protected\t"; break;
322 case MCSA_Reference: OS << "\t.reference\t"; break;
323 case MCSA_Weak: OS << "\t.weak\t"; break;
324 case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break;
326 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
327 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
334 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
335 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
339 void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
340 OS << "\t.def\t " << *Symbol << ';';
344 void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
345 OS << "\t.scl\t" << StorageClass << ';';
349 void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
350 OS << "\t.type\t" << Type << ';';
354 void MCAsmStreamer::EndCOFFSymbolDef() {
359 void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
360 assert(MAI.hasDotTypeDotSizeDirective());
361 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
364 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
365 unsigned ByteAlignment) {
366 OS << "\t.comm\t" << *Symbol << ',' << Size;
367 if (ByteAlignment != 0) {
368 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
369 OS << ',' << ByteAlignment;
371 OS << ',' << Log2_32(ByteAlignment);
376 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
378 /// @param Symbol - The common symbol to emit.
379 /// @param Size - The size of the common symbol.
380 void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
381 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
382 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
386 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
387 unsigned Size, unsigned ByteAlignment) {
388 // Note: a .zerofill directive does not switch sections.
391 // This is a mach-o specific directive.
392 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
393 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
395 if (Symbol != NULL) {
396 OS << ',' << *Symbol << ',' << Size;
397 if (ByteAlignment != 0)
398 OS << ',' << Log2_32(ByteAlignment);
403 // .tbss sym, size, align
404 // This depends that the symbol has already been mangled from the original,
406 void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
407 uint64_t Size, unsigned ByteAlignment) {
408 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
409 // Instead of using the Section we'll just use the shortcut.
410 // This is a mach-o specific directive and section.
411 OS << ".tbss " << *Symbol << ", " << Size;
413 // Output align if we have it. We default to 1 so don't bother printing
415 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
420 static inline char toOctal(int X) { return (X&7)+'0'; }
422 static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
425 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
426 unsigned char C = Data[i];
427 if (C == '"' || C == '\\') {
428 OS << '\\' << (char)C;
432 if (isprint((unsigned char)C)) {
438 case '\b': OS << "\\b"; break;
439 case '\f': OS << "\\f"; break;
440 case '\n': OS << "\\n"; break;
441 case '\r': OS << "\\r"; break;
442 case '\t': OS << "\\t"; break;
445 OS << toOctal(C >> 6);
446 OS << toOctal(C >> 3);
447 OS << toOctal(C >> 0);
456 void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
457 assert(CurSection && "Cannot emit contents before setting section!");
458 if (Data.empty()) return;
460 if (Data.size() == 1) {
461 OS << MAI.getData8bitsDirective(AddrSpace);
462 OS << (unsigned)(unsigned char)Data[0];
467 // If the data ends with 0 and the target supports .asciz, use it, otherwise
469 if (MAI.getAscizDirective() && Data.back() == 0) {
470 OS << MAI.getAscizDirective();
471 Data = Data.substr(0, Data.size()-1);
473 OS << MAI.getAsciiDirective();
477 PrintQuotedString(Data, OS);
481 /// EmitIntValue - Special case of EmitValue that avoids the client having
482 /// to pass in a MCExpr for constant integers.
483 void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
484 unsigned AddrSpace) {
485 assert(CurSection && "Cannot emit contents before setting section!");
486 const char *Directive = 0;
489 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
490 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
491 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
493 Directive = MAI.getData64bitsDirective(AddrSpace);
494 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
495 if (Directive) break;
496 if (isLittleEndian()) {
497 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
498 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
500 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
501 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
506 assert(Directive && "Invalid size for machine code value!");
507 OS << Directive << truncateToSize(Value, Size);
511 void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
512 unsigned AddrSpace) {
513 assert(CurSection && "Cannot emit contents before setting section!");
514 const char *Directive = 0;
517 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
518 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
519 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
520 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
523 assert(Directive && "Invalid size for machine code value!");
524 OS << Directive << *Value;
528 void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace) {
529 assert(MAI.hasLEB128() && "Cannot print a .uleb");
530 OS << ".uleb128 " << *Value;
534 void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace) {
535 assert(MAI.hasLEB128() && "Cannot print a .sleb");
536 OS << ".sleb128 " << *Value;
540 void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
541 assert(MAI.getGPRel32Directive() != 0);
542 OS << MAI.getGPRel32Directive() << *Value;
547 /// EmitFill - Emit NumBytes bytes worth of the value specified by
548 /// FillValue. This implements directives such as '.space'.
549 void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
550 unsigned AddrSpace) {
551 if (NumBytes == 0) return;
554 if (const char *ZeroDirective = MAI.getZeroDirective()) {
555 OS << ZeroDirective << NumBytes;
557 OS << ',' << (int)FillValue;
562 // Emit a byte at a time.
563 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
566 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
568 unsigned MaxBytesToEmit) {
569 // Some assemblers don't support non-power of two alignments, so we always
570 // emit alignments as a power of two if possible.
571 if (isPowerOf2_32(ByteAlignment)) {
573 default: llvm_unreachable("Invalid size for machine code value!");
574 case 1: OS << MAI.getAlignDirective(); break;
575 // FIXME: use MAI for this!
576 case 2: OS << ".p2alignw "; break;
577 case 4: OS << ".p2alignl "; break;
578 case 8: llvm_unreachable("Unsupported alignment size!");
581 if (MAI.getAlignmentIsInBytes())
584 OS << Log2_32(ByteAlignment);
586 if (Value || MaxBytesToEmit) {
588 OS.write_hex(truncateToSize(Value, ValueSize));
591 OS << ", " << MaxBytesToEmit;
597 // Non-power of two alignment. This is not widely supported by assemblers.
598 // FIXME: Parameterize this based on MAI.
600 default: llvm_unreachable("Invalid size for machine code value!");
601 case 1: OS << ".balign"; break;
602 case 2: OS << ".balignw"; break;
603 case 4: OS << ".balignl"; break;
604 case 8: llvm_unreachable("Unsupported alignment size!");
607 OS << ' ' << ByteAlignment;
608 OS << ", " << truncateToSize(Value, ValueSize);
610 OS << ", " << MaxBytesToEmit;
614 void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
615 unsigned MaxBytesToEmit) {
616 // Emit with a text fill value.
617 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
621 void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
622 unsigned char Value) {
623 // FIXME: Verify that Offset is associated with the current section.
624 OS << ".org " << *Offset << ", " << (unsigned) Value;
629 void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
630 assert(MAI.hasSingleParameterDotFile());
632 PrintQuotedString(Filename, OS);
636 void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
637 OS << "\t.file\t" << FileNo << ' ';
638 PrintQuotedString(Filename, OS);
642 void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
643 raw_ostream &OS = GetCommentOS();
644 SmallString<256> Code;
645 SmallVector<MCFixup, 4> Fixups;
646 raw_svector_ostream VecOS(Code);
647 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
650 // If we are showing fixups, create symbolic markers in the encoded
651 // representation. We do this by making a per-bit map to the fixup item index,
652 // then trying to display it as nicely as possible.
653 SmallVector<uint8_t, 64> FixupMap;
654 FixupMap.resize(Code.size() * 8);
655 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
658 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
659 MCFixup &F = Fixups[i];
660 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
661 for (unsigned j = 0; j != Info.TargetSize; ++j) {
662 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
663 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
664 FixupMap[Index] = 1 + i;
669 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
673 // See if all bits are the same map entry.
674 uint8_t MapEntry = FixupMap[i * 8 + 0];
675 for (unsigned j = 1; j != 8; ++j) {
676 if (FixupMap[i * 8 + j] == MapEntry)
679 MapEntry = uint8_t(~0U);
683 if (MapEntry != uint8_t(~0U)) {
685 OS << format("0x%02x", uint8_t(Code[i]));
687 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
688 OS << char('A' + MapEntry - 1);
691 // Otherwise, write out in binary.
693 for (unsigned j = 8; j--;) {
694 unsigned Bit = (Code[i] >> j) & 1;
698 FixupBit = i * 8 + j;
700 FixupBit = i * 8 + (7-j);
702 if (uint8_t MapEntry = FixupMap[FixupBit]) {
703 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
704 OS << char('A' + MapEntry - 1);
712 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
713 MCFixup &F = Fixups[i];
714 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
715 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
716 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
720 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
721 assert(CurSection && "Cannot emit contents before setting section!");
723 // Show the encoding in a comment if we have a code emitter.
725 AddEncodingComment(Inst);
727 // Show the MCInst if enabled.
729 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
730 GetCommentOS() << "\n";
733 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
735 InstPrinter->printInst(&Inst, OS);
737 Inst.print(OS, &MAI);
741 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
742 /// the specified string in the output .s file. This capability is
743 /// indicated by the hasRawTextSupport() predicate.
744 void MCAsmStreamer::EmitRawText(StringRef String) {
745 if (!String.empty() && String.back() == '\n')
746 String = String.substr(0, String.size()-1);
751 void MCAsmStreamer::Finish() {
754 MCStreamer *llvm::createAsmStreamer(MCContext &Context,
755 formatted_raw_ostream &OS,
757 bool isVerboseAsm, MCInstPrinter *IP,
758 MCCodeEmitter *CE, bool ShowInst) {
759 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,