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"
26 #include "llvm/Target/TargetLoweringObjectFile.h"
31 class MCAsmStreamer : public MCStreamer {
32 formatted_raw_ostream &OS;
34 OwningPtr<MCInstPrinter> InstPrinter;
35 OwningPtr<MCCodeEmitter> Emitter;
37 SmallString<128> CommentToEmit;
38 raw_svector_ostream CommentStream;
40 const TargetLoweringObjectFile *TLOF;
43 unsigned IsLittleEndian : 1;
44 unsigned IsVerboseAsm : 1;
45 unsigned ShowInst : 1;
48 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
49 bool isLittleEndian, bool isVerboseAsm,
50 const TargetLoweringObjectFile *tlof, int pointerSize,
51 MCInstPrinter *printer, MCCodeEmitter *emitter, bool showInst)
52 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
53 InstPrinter(printer), Emitter(emitter), CommentStream(CommentToEmit),
54 TLOF(tlof), PointerSize(pointerSize),
55 IsLittleEndian(isLittleEndian), IsVerboseAsm(isVerboseAsm),
57 if (InstPrinter && IsVerboseAsm)
58 InstPrinter->setCommentStream(CommentStream);
62 bool isLittleEndian() const { return IsLittleEndian; }
64 inline void EmitEOL() {
65 // If we don't have any comments, just emit a \n.
72 void EmitCommentsAndEOL();
74 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
76 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
78 /// hasRawTextSupport - We support EmitRawText.
79 virtual bool hasRawTextSupport() const { return true; }
81 /// AddComment - Add a comment that can be emitted to the generated .s
82 /// file if applicable as a QoI issue to make the output of the compiler
83 /// more readable. This only affects the MCAsmStreamer, and only when
84 /// verbose assembly output is enabled.
85 virtual void AddComment(const Twine &T);
87 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
88 virtual void AddEncodingComment(const MCInst &Inst);
90 /// GetCommentOS - Return a raw_ostream that comments can be written to.
91 /// Unlike AddComment, you are required to terminate comments with \n if you
93 virtual raw_ostream &GetCommentOS() {
95 return nulls(); // Discard comments unless in verbose asm mode.
99 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
100 virtual void AddBlankLine() {
104 /// @name MCStreamer Interface
107 virtual void SwitchSection(const MCSection *Section);
109 virtual void InitSections() {
110 // FIXME, this is MachO specific, but the testsuite
112 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
113 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
114 0, SectionKind::getText()));
117 virtual void EmitLabel(MCSymbol *Symbol);
119 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
120 virtual void EmitThumbFunc(MCSymbol *Func);
122 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
123 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
125 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
127 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
128 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
129 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
130 virtual void EmitCOFFSymbolType(int Type);
131 virtual void EndCOFFSymbolDef();
132 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
133 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
134 unsigned ByteAlignment);
136 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
138 /// @param Symbol - The common symbol to emit.
139 /// @param Size - The size of the common symbol.
140 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
142 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
143 unsigned Size = 0, unsigned ByteAlignment = 0);
145 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
146 uint64_t Size, unsigned ByteAlignment = 0);
148 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
150 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace,
151 bool UseSet = false);
153 virtual void EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
155 virtual void EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
157 virtual void EmitGPRel32Value(const MCExpr *Value);
160 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
163 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
164 unsigned ValueSize = 1,
165 unsigned MaxBytesToEmit = 0);
167 virtual void EmitCodeAlignment(unsigned ByteAlignment,
168 unsigned MaxBytesToEmit = 0);
170 virtual void EmitValueToOffset(const MCExpr *Offset,
171 unsigned char Value = 0);
173 virtual void EmitFileDirective(StringRef Filename);
174 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
175 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
176 unsigned Column, unsigned Flags,
177 unsigned Isa, unsigned Discriminator);
179 virtual bool EmitCFIStartProc();
180 virtual bool EmitCFIEndProc();
181 virtual bool EmitCFIDefCfaOffset(int64_t Offset);
182 virtual bool EmitCFIDefCfaRegister(int64_t Register);
183 virtual bool EmitCFIOffset(int64_t Register, int64_t Offset);
184 virtual bool EmitCFIPersonality(const MCSymbol *Sym);
185 virtual bool EmitCFILsda(const MCSymbol *Sym);
187 virtual void EmitInstruction(const MCInst &Inst);
189 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
190 /// the specified string in the output .s file. This capability is
191 /// indicated by the hasRawTextSupport() predicate.
192 virtual void EmitRawText(StringRef String);
194 virtual void Finish();
199 } // end anonymous namespace.
201 /// AddComment - Add a comment that can be emitted to the generated .s
202 /// file if applicable as a QoI issue to make the output of the compiler
203 /// more readable. This only affects the MCAsmStreamer, and only when
204 /// verbose assembly output is enabled.
205 void MCAsmStreamer::AddComment(const Twine &T) {
206 if (!IsVerboseAsm) return;
208 // Make sure that CommentStream is flushed.
209 CommentStream.flush();
211 T.toVector(CommentToEmit);
212 // Each comment goes on its own line.
213 CommentToEmit.push_back('\n');
215 // Tell the comment stream that the vector changed underneath it.
216 CommentStream.resync();
219 void MCAsmStreamer::EmitCommentsAndEOL() {
220 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
225 CommentStream.flush();
226 StringRef Comments = CommentToEmit.str();
228 assert(Comments.back() == '\n' &&
229 "Comment array not newline terminated");
231 // Emit a line of comments.
232 OS.PadToColumn(MAI.getCommentColumn());
233 size_t Position = Comments.find('\n');
234 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
236 Comments = Comments.substr(Position+1);
237 } while (!Comments.empty());
239 CommentToEmit.clear();
240 // Tell the comment stream that the vector changed underneath it.
241 CommentStream.resync();
244 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
245 assert(Bytes && "Invalid size!");
246 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
249 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
250 assert(Section && "Cannot switch to a null section!");
251 if (Section != CurSection) {
252 PrevSection = CurSection;
253 CurSection = Section;
254 Section->PrintSwitchToSection(MAI, OS);
258 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
259 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
260 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
261 assert(CurSection && "Cannot emit before setting section!");
263 OS << *Symbol << MAI.getLabelSuffix();
265 Symbol->setSection(*CurSection);
268 void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
270 default: assert(0 && "Invalid flag!");
271 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
272 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
273 case MCAF_Code16: OS << "\t.code\t16"; break;
274 case MCAF_Code32: OS << "\t.code\t32"; break;
279 void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
280 // This needs to emit to a temporary string to get properly quoted
281 // MCSymbols when they have spaces in them.
282 OS << "\t.thumb_func";
288 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
289 OS << *Symbol << " = " << *Value;
292 // FIXME: Lift context changes into super class.
293 Symbol->setVariableValue(Value);
296 void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
297 OS << ".weakref " << *Alias << ", " << *Symbol;
301 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
302 MCSymbolAttr Attribute) {
304 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
305 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
306 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
307 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
308 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
309 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
310 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
311 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
312 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
313 OS << "\t.type\t" << *Symbol << ','
314 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
316 default: assert(0 && "Unknown ELF .type");
317 case MCSA_ELF_TypeFunction: OS << "function"; break;
318 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
319 case MCSA_ELF_TypeObject: OS << "object"; break;
320 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
321 case MCSA_ELF_TypeCommon: OS << "common"; break;
322 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
323 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
327 case MCSA_Global: // .globl/.global
328 OS << MAI.getGlobalDirective();
330 case MCSA_Hidden: OS << "\t.hidden\t"; break;
331 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
332 case MCSA_Internal: OS << "\t.internal\t"; break;
333 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
334 case MCSA_Local: OS << "\t.local\t"; break;
335 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
336 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
337 case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break;
338 case MCSA_Protected: OS << "\t.protected\t"; break;
339 case MCSA_Reference: OS << "\t.reference\t"; break;
340 case MCSA_Weak: OS << "\t.weak\t"; break;
341 case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break;
343 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
344 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
351 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
352 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
356 void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
357 OS << "\t.def\t " << *Symbol << ';';
361 void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
362 OS << "\t.scl\t" << StorageClass << ';';
366 void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
367 OS << "\t.type\t" << Type << ';';
371 void MCAsmStreamer::EndCOFFSymbolDef() {
376 void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
377 assert(MAI.hasDotTypeDotSizeDirective());
378 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
381 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
382 unsigned ByteAlignment) {
383 OS << "\t.comm\t" << *Symbol << ',' << Size;
384 if (ByteAlignment != 0) {
385 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
386 OS << ',' << ByteAlignment;
388 OS << ',' << Log2_32(ByteAlignment);
393 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
395 /// @param Symbol - The common symbol to emit.
396 /// @param Size - The size of the common symbol.
397 void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
398 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
399 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
403 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
404 unsigned Size, unsigned ByteAlignment) {
405 // Note: a .zerofill directive does not switch sections.
408 // This is a mach-o specific directive.
409 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
410 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
412 if (Symbol != NULL) {
413 OS << ',' << *Symbol << ',' << Size;
414 if (ByteAlignment != 0)
415 OS << ',' << Log2_32(ByteAlignment);
420 // .tbss sym, size, align
421 // This depends that the symbol has already been mangled from the original,
423 void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
424 uint64_t Size, unsigned ByteAlignment) {
425 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
426 // Instead of using the Section we'll just use the shortcut.
427 // This is a mach-o specific directive and section.
428 OS << ".tbss " << *Symbol << ", " << Size;
430 // Output align if we have it. We default to 1 so don't bother printing
432 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
437 static inline char toOctal(int X) { return (X&7)+'0'; }
439 static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
442 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
443 unsigned char C = Data[i];
444 if (C == '"' || C == '\\') {
445 OS << '\\' << (char)C;
449 if (isprint((unsigned char)C)) {
455 case '\b': OS << "\\b"; break;
456 case '\f': OS << "\\f"; break;
457 case '\n': OS << "\\n"; break;
458 case '\r': OS << "\\r"; break;
459 case '\t': OS << "\\t"; break;
462 OS << toOctal(C >> 6);
463 OS << toOctal(C >> 3);
464 OS << toOctal(C >> 0);
473 void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
474 assert(CurSection && "Cannot emit contents before setting section!");
475 if (Data.empty()) return;
477 if (Data.size() == 1) {
478 OS << MAI.getData8bitsDirective(AddrSpace);
479 OS << (unsigned)(unsigned char)Data[0];
484 // If the data ends with 0 and the target supports .asciz, use it, otherwise
486 if (MAI.getAscizDirective() && Data.back() == 0) {
487 OS << MAI.getAscizDirective();
488 Data = Data.substr(0, Data.size()-1);
490 OS << MAI.getAsciiDirective();
494 PrintQuotedString(Data, OS);
498 void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
499 unsigned AddrSpace, bool UseSet) {
500 assert(CurSection && "Cannot emit contents before setting section!");
501 const char *Directive = 0;
504 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
505 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
506 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
508 Directive = MAI.getData64bitsDirective(AddrSpace);
509 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
510 if (Directive) break;
512 if (!Value->EvaluateAsAbsolute(IntValue))
513 report_fatal_error("Don't know how to emit this value.");
514 if (isLittleEndian()) {
515 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
516 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
518 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
519 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
524 assert(Directive && "Invalid size for machine code value!");
525 if (UseSet && MAI.hasSetDirective()) {
526 MCSymbol *SetLabel = getContext().CreateTempSymbol();
527 EmitAssignment(SetLabel, Value);
528 OS << Directive << *SetLabel;
533 OS << Directive << *Value;
537 void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace) {
539 if (Value->EvaluateAsAbsolute(IntValue)) {
541 raw_svector_ostream OSE(Tmp);
542 MCObjectWriter::EncodeULEB128(IntValue, OSE);
543 EmitBytes(OSE.str(), AddrSpace);
546 assert(MAI.hasLEB128() && "Cannot print a .uleb");
547 OS << ".uleb128 " << *Value;
551 void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace) {
553 if (Value->EvaluateAsAbsolute(IntValue)) {
555 raw_svector_ostream OSE(Tmp);
556 MCObjectWriter::EncodeSLEB128(IntValue, OSE);
557 EmitBytes(OSE.str(), AddrSpace);
560 assert(MAI.hasLEB128() && "Cannot print a .sleb");
561 OS << ".sleb128 " << *Value;
565 void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
566 assert(MAI.getGPRel32Directive() != 0);
567 OS << MAI.getGPRel32Directive() << *Value;
572 /// EmitFill - Emit NumBytes bytes worth of the value specified by
573 /// FillValue. This implements directives such as '.space'.
574 void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
575 unsigned AddrSpace) {
576 if (NumBytes == 0) return;
579 if (const char *ZeroDirective = MAI.getZeroDirective()) {
580 OS << ZeroDirective << NumBytes;
582 OS << ',' << (int)FillValue;
587 // Emit a byte at a time.
588 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
591 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
593 unsigned MaxBytesToEmit) {
594 // Some assemblers don't support non-power of two alignments, so we always
595 // emit alignments as a power of two if possible.
596 if (isPowerOf2_32(ByteAlignment)) {
598 default: llvm_unreachable("Invalid size for machine code value!");
599 case 1: OS << MAI.getAlignDirective(); break;
600 // FIXME: use MAI for this!
601 case 2: OS << ".p2alignw "; break;
602 case 4: OS << ".p2alignl "; break;
603 case 8: llvm_unreachable("Unsupported alignment size!");
606 if (MAI.getAlignmentIsInBytes())
609 OS << Log2_32(ByteAlignment);
611 if (Value || MaxBytesToEmit) {
613 OS.write_hex(truncateToSize(Value, ValueSize));
616 OS << ", " << MaxBytesToEmit;
622 // Non-power of two alignment. This is not widely supported by assemblers.
623 // FIXME: Parameterize this based on MAI.
625 default: llvm_unreachable("Invalid size for machine code value!");
626 case 1: OS << ".balign"; break;
627 case 2: OS << ".balignw"; break;
628 case 4: OS << ".balignl"; break;
629 case 8: llvm_unreachable("Unsupported alignment size!");
632 OS << ' ' << ByteAlignment;
633 OS << ", " << truncateToSize(Value, ValueSize);
635 OS << ", " << MaxBytesToEmit;
639 void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
640 unsigned MaxBytesToEmit) {
641 // Emit with a text fill value.
642 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
646 void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
647 unsigned char Value) {
648 // FIXME: Verify that Offset is associated with the current section.
649 OS << ".org " << *Offset << ", " << (unsigned) Value;
654 void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
655 assert(MAI.hasSingleParameterDotFile());
657 PrintQuotedString(Filename, OS);
661 bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
663 OS << "\t.file\t" << FileNo << ' ';
664 PrintQuotedString(Filename, OS);
667 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
670 void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
671 unsigned Column, unsigned Flags,
673 unsigned Discriminator) {
674 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
679 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
680 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
681 OS << " basic_block";
682 if (Flags & DWARF2_FLAG_PROLOGUE_END)
683 OS << " prologue_end";
684 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
685 OS << " epilogue_begin";
687 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
688 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
691 if (Flags & DWARF2_FLAG_IS_STMT)
700 OS << "discriminator " << Discriminator;
704 bool MCAsmStreamer::EmitCFIStartProc() {
705 if (this->MCStreamer::EmitCFIStartProc())
708 OS << ".cfi_startproc";
714 bool MCAsmStreamer::EmitCFIEndProc() {
715 if (this->MCStreamer::EmitCFIEndProc())
718 OS << ".cfi_endproc";
724 bool MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
725 if (this->MCStreamer::EmitCFIDefCfaOffset(Offset))
728 OS << ".cfi_def_cfa_offset " << Offset;
734 bool MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
735 if (this->MCStreamer::EmitCFIDefCfaRegister(Register))
738 OS << ".cfi_def_cfa_register " << Register;
744 bool MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
745 if (this->MCStreamer::EmitCFIOffset(Register, Offset))
748 OS << ".cfi_offset " << Register << ", " << Offset;
754 bool MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym) {
755 if (this->MCStreamer::EmitCFIPersonality(Sym))
758 OS << ".cfi_personality 0, " << *Sym;
764 bool MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym) {
765 if (this->MCStreamer::EmitCFILsda(Sym))
768 OS << ".cfi_lsda 0, " << *Sym;
774 void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
775 raw_ostream &OS = GetCommentOS();
776 SmallString<256> Code;
777 SmallVector<MCFixup, 4> Fixups;
778 raw_svector_ostream VecOS(Code);
779 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
782 // If we are showing fixups, create symbolic markers in the encoded
783 // representation. We do this by making a per-bit map to the fixup item index,
784 // then trying to display it as nicely as possible.
785 SmallVector<uint8_t, 64> FixupMap;
786 FixupMap.resize(Code.size() * 8);
787 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
790 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
791 MCFixup &F = Fixups[i];
792 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
793 for (unsigned j = 0; j != Info.TargetSize; ++j) {
794 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
795 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
796 FixupMap[Index] = 1 + i;
801 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
805 // See if all bits are the same map entry.
806 uint8_t MapEntry = FixupMap[i * 8 + 0];
807 for (unsigned j = 1; j != 8; ++j) {
808 if (FixupMap[i * 8 + j] == MapEntry)
811 MapEntry = uint8_t(~0U);
815 if (MapEntry != uint8_t(~0U)) {
817 OS << format("0x%02x", uint8_t(Code[i]));
819 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
820 OS << char('A' + MapEntry - 1);
823 // Otherwise, write out in binary.
825 for (unsigned j = 8; j--;) {
826 unsigned Bit = (Code[i] >> j) & 1;
830 FixupBit = i * 8 + j;
832 FixupBit = i * 8 + (7-j);
834 if (uint8_t MapEntry = FixupMap[FixupBit]) {
835 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
836 OS << char('A' + MapEntry - 1);
844 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
845 MCFixup &F = Fixups[i];
846 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
847 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
848 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
852 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
853 assert(CurSection && "Cannot emit contents before setting section!");
856 MCLineEntry::Make(this, getCurrentSection());
858 // Show the encoding in a comment if we have a code emitter.
860 AddEncodingComment(Inst);
862 // Show the MCInst if enabled.
864 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
865 GetCommentOS() << "\n";
868 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
870 InstPrinter->printInst(&Inst, OS);
872 Inst.print(OS, &MAI);
876 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
877 /// the specified string in the output .s file. This capability is
878 /// indicated by the hasRawTextSupport() predicate.
879 void MCAsmStreamer::EmitRawText(StringRef String) {
880 if (!String.empty() && String.back() == '\n')
881 String = String.substr(0, String.size()-1);
886 void MCAsmStreamer::Finish() {
887 // Dump out the dwarf file & directory tables and line tables.
888 if (getContext().hasDwarfFiles() && getContext().hasDwarfLines() && TLOF) {
889 MCDwarfFileTable::Emit(this, TLOF->getDwarfLineSection(), NULL,
894 MCStreamer *llvm::createAsmStreamer(MCContext &Context,
895 formatted_raw_ostream &OS,
897 bool isVerboseAsm, MCInstPrinter *IP,
898 MCCodeEmitter *CE, bool ShowInst) {
899 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
900 NULL, 0, IP, CE, ShowInst);
904 MCStreamer *llvm::createAsmStreamerNoLoc(MCContext &Context,
905 formatted_raw_ostream &OS,
908 const TargetLoweringObjectFile *TLOF,
911 MCCodeEmitter *CE, bool ShowInst) {
912 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
913 TLOF, PointerSize, IP, CE, ShowInst);