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);
124 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
125 const MCSymbol *LastLabel,
126 const MCSymbol *Label);
128 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
130 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
131 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
132 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
133 virtual void EmitCOFFSymbolType(int Type);
134 virtual void EndCOFFSymbolDef();
135 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
136 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
137 unsigned ByteAlignment);
139 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
141 /// @param Symbol - The common symbol to emit.
142 /// @param Size - The size of the common symbol.
143 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
145 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
146 unsigned Size = 0, unsigned ByteAlignment = 0);
148 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
149 uint64_t Size, unsigned ByteAlignment = 0);
151 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
153 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace,
154 bool UseSet = false);
156 virtual void EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
158 virtual void EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
160 virtual void EmitGPRel32Value(const MCExpr *Value);
163 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
166 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
167 unsigned ValueSize = 1,
168 unsigned MaxBytesToEmit = 0);
170 virtual void EmitCodeAlignment(unsigned ByteAlignment,
171 unsigned MaxBytesToEmit = 0);
173 virtual void EmitValueToOffset(const MCExpr *Offset,
174 unsigned char Value = 0);
176 virtual void EmitFileDirective(StringRef Filename);
177 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
178 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
179 unsigned Column, unsigned Flags,
180 unsigned Isa, unsigned Discriminator);
182 virtual bool EmitCFIStartProc();
183 virtual bool EmitCFIEndProc();
184 virtual bool EmitCFIDefCfaOffset(int64_t Offset);
185 virtual bool EmitCFIDefCfaRegister(int64_t Register);
186 virtual bool EmitCFIOffset(int64_t Register, int64_t Offset);
187 virtual bool EmitCFIPersonality(const MCSymbol *Sym);
188 virtual bool EmitCFILsda(const MCSymbol *Sym);
190 virtual void EmitInstruction(const MCInst &Inst);
192 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
193 /// the specified string in the output .s file. This capability is
194 /// indicated by the hasRawTextSupport() predicate.
195 virtual void EmitRawText(StringRef String);
197 virtual void Finish();
202 } // end anonymous namespace.
204 /// AddComment - Add a comment that can be emitted to the generated .s
205 /// file if applicable as a QoI issue to make the output of the compiler
206 /// more readable. This only affects the MCAsmStreamer, and only when
207 /// verbose assembly output is enabled.
208 void MCAsmStreamer::AddComment(const Twine &T) {
209 if (!IsVerboseAsm) return;
211 // Make sure that CommentStream is flushed.
212 CommentStream.flush();
214 T.toVector(CommentToEmit);
215 // Each comment goes on its own line.
216 CommentToEmit.push_back('\n');
218 // Tell the comment stream that the vector changed underneath it.
219 CommentStream.resync();
222 void MCAsmStreamer::EmitCommentsAndEOL() {
223 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
228 CommentStream.flush();
229 StringRef Comments = CommentToEmit.str();
231 assert(Comments.back() == '\n' &&
232 "Comment array not newline terminated");
234 // Emit a line of comments.
235 OS.PadToColumn(MAI.getCommentColumn());
236 size_t Position = Comments.find('\n');
237 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
239 Comments = Comments.substr(Position+1);
240 } while (!Comments.empty());
242 CommentToEmit.clear();
243 // Tell the comment stream that the vector changed underneath it.
244 CommentStream.resync();
247 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
248 assert(Bytes && "Invalid size!");
249 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
252 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
253 assert(Section && "Cannot switch to a null section!");
254 if (Section != CurSection) {
255 PrevSection = CurSection;
256 CurSection = Section;
257 Section->PrintSwitchToSection(MAI, OS);
261 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
262 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
263 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
264 assert(CurSection && "Cannot emit before setting section!");
266 OS << *Symbol << MAI.getLabelSuffix();
268 Symbol->setSection(*CurSection);
271 void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
273 default: assert(0 && "Invalid flag!");
274 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
275 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
276 case MCAF_Code16: OS << "\t.code\t16"; break;
277 case MCAF_Code32: OS << "\t.code\t32"; break;
282 void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
283 // This needs to emit to a temporary string to get properly quoted
284 // MCSymbols when they have spaces in them.
285 OS << "\t.thumb_func";
291 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
292 OS << *Symbol << " = " << *Value;
295 // FIXME: Lift context changes into super class.
296 Symbol->setVariableValue(Value);
299 void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
300 OS << ".weakref " << *Alias << ", " << *Symbol;
304 void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
305 const MCSymbol *LastLabel,
306 const MCSymbol *Label) {
307 EmitDwarfSetLineAddr(LineDelta, Label, PointerSize);
310 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
311 MCSymbolAttr Attribute) {
313 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
314 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
315 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
316 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
317 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
318 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
319 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
320 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
321 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
322 OS << "\t.type\t" << *Symbol << ','
323 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
325 default: assert(0 && "Unknown ELF .type");
326 case MCSA_ELF_TypeFunction: OS << "function"; break;
327 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
328 case MCSA_ELF_TypeObject: OS << "object"; break;
329 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
330 case MCSA_ELF_TypeCommon: OS << "common"; break;
331 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
332 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
336 case MCSA_Global: // .globl/.global
337 OS << MAI.getGlobalDirective();
339 case MCSA_Hidden: OS << "\t.hidden\t"; break;
340 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
341 case MCSA_Internal: OS << "\t.internal\t"; break;
342 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
343 case MCSA_Local: OS << "\t.local\t"; break;
344 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
345 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
346 case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break;
347 case MCSA_Protected: OS << "\t.protected\t"; break;
348 case MCSA_Reference: OS << "\t.reference\t"; break;
349 case MCSA_Weak: OS << "\t.weak\t"; break;
350 case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break;
352 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
353 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
360 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
361 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
365 void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
366 OS << "\t.def\t " << *Symbol << ';';
370 void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
371 OS << "\t.scl\t" << StorageClass << ';';
375 void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
376 OS << "\t.type\t" << Type << ';';
380 void MCAsmStreamer::EndCOFFSymbolDef() {
385 void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
386 assert(MAI.hasDotTypeDotSizeDirective());
387 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
390 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
391 unsigned ByteAlignment) {
392 OS << "\t.comm\t" << *Symbol << ',' << Size;
393 if (ByteAlignment != 0) {
394 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
395 OS << ',' << ByteAlignment;
397 OS << ',' << Log2_32(ByteAlignment);
402 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
404 /// @param Symbol - The common symbol to emit.
405 /// @param Size - The size of the common symbol.
406 void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
407 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
408 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
412 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
413 unsigned Size, unsigned ByteAlignment) {
414 // Note: a .zerofill directive does not switch sections.
417 // This is a mach-o specific directive.
418 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
419 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
421 if (Symbol != NULL) {
422 OS << ',' << *Symbol << ',' << Size;
423 if (ByteAlignment != 0)
424 OS << ',' << Log2_32(ByteAlignment);
429 // .tbss sym, size, align
430 // This depends that the symbol has already been mangled from the original,
432 void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
433 uint64_t Size, unsigned ByteAlignment) {
434 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
435 // Instead of using the Section we'll just use the shortcut.
436 // This is a mach-o specific directive and section.
437 OS << ".tbss " << *Symbol << ", " << Size;
439 // Output align if we have it. We default to 1 so don't bother printing
441 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
446 static inline char toOctal(int X) { return (X&7)+'0'; }
448 static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
451 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
452 unsigned char C = Data[i];
453 if (C == '"' || C == '\\') {
454 OS << '\\' << (char)C;
458 if (isprint((unsigned char)C)) {
464 case '\b': OS << "\\b"; break;
465 case '\f': OS << "\\f"; break;
466 case '\n': OS << "\\n"; break;
467 case '\r': OS << "\\r"; break;
468 case '\t': OS << "\\t"; break;
471 OS << toOctal(C >> 6);
472 OS << toOctal(C >> 3);
473 OS << toOctal(C >> 0);
482 void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
483 assert(CurSection && "Cannot emit contents before setting section!");
484 if (Data.empty()) return;
486 if (Data.size() == 1) {
487 OS << MAI.getData8bitsDirective(AddrSpace);
488 OS << (unsigned)(unsigned char)Data[0];
493 // If the data ends with 0 and the target supports .asciz, use it, otherwise
495 if (MAI.getAscizDirective() && Data.back() == 0) {
496 OS << MAI.getAscizDirective();
497 Data = Data.substr(0, Data.size()-1);
499 OS << MAI.getAsciiDirective();
503 PrintQuotedString(Data, OS);
507 void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
508 unsigned AddrSpace, bool UseSet) {
509 assert(CurSection && "Cannot emit contents before setting section!");
510 const char *Directive = 0;
513 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
514 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
515 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
517 Directive = MAI.getData64bitsDirective(AddrSpace);
518 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
519 if (Directive) break;
521 if (!Value->EvaluateAsAbsolute(IntValue))
522 report_fatal_error("Don't know how to emit this value.");
523 if (isLittleEndian()) {
524 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
525 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
527 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
528 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
533 assert(Directive && "Invalid size for machine code value!");
534 if (UseSet && MAI.hasSetDirective()) {
535 MCSymbol *SetLabel = getContext().CreateTempSymbol();
536 EmitAssignment(SetLabel, Value);
537 OS << Directive << *SetLabel;
542 OS << Directive << *Value;
546 void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace) {
548 if (Value->EvaluateAsAbsolute(IntValue)) {
550 raw_svector_ostream OSE(Tmp);
551 MCObjectWriter::EncodeULEB128(IntValue, OSE);
552 EmitBytes(OSE.str(), AddrSpace);
555 assert(MAI.hasLEB128() && "Cannot print a .uleb");
556 OS << ".uleb128 " << *Value;
560 void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace) {
562 if (Value->EvaluateAsAbsolute(IntValue)) {
564 raw_svector_ostream OSE(Tmp);
565 MCObjectWriter::EncodeSLEB128(IntValue, OSE);
566 EmitBytes(OSE.str(), AddrSpace);
569 assert(MAI.hasLEB128() && "Cannot print a .sleb");
570 OS << ".sleb128 " << *Value;
574 void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
575 assert(MAI.getGPRel32Directive() != 0);
576 OS << MAI.getGPRel32Directive() << *Value;
581 /// EmitFill - Emit NumBytes bytes worth of the value specified by
582 /// FillValue. This implements directives such as '.space'.
583 void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
584 unsigned AddrSpace) {
585 if (NumBytes == 0) return;
588 if (const char *ZeroDirective = MAI.getZeroDirective()) {
589 OS << ZeroDirective << NumBytes;
591 OS << ',' << (int)FillValue;
596 // Emit a byte at a time.
597 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
600 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
602 unsigned MaxBytesToEmit) {
603 // Some assemblers don't support non-power of two alignments, so we always
604 // emit alignments as a power of two if possible.
605 if (isPowerOf2_32(ByteAlignment)) {
607 default: llvm_unreachable("Invalid size for machine code value!");
608 case 1: OS << MAI.getAlignDirective(); break;
609 // FIXME: use MAI for this!
610 case 2: OS << ".p2alignw "; break;
611 case 4: OS << ".p2alignl "; break;
612 case 8: llvm_unreachable("Unsupported alignment size!");
615 if (MAI.getAlignmentIsInBytes())
618 OS << Log2_32(ByteAlignment);
620 if (Value || MaxBytesToEmit) {
622 OS.write_hex(truncateToSize(Value, ValueSize));
625 OS << ", " << MaxBytesToEmit;
631 // Non-power of two alignment. This is not widely supported by assemblers.
632 // FIXME: Parameterize this based on MAI.
634 default: llvm_unreachable("Invalid size for machine code value!");
635 case 1: OS << ".balign"; break;
636 case 2: OS << ".balignw"; break;
637 case 4: OS << ".balignl"; break;
638 case 8: llvm_unreachable("Unsupported alignment size!");
641 OS << ' ' << ByteAlignment;
642 OS << ", " << truncateToSize(Value, ValueSize);
644 OS << ", " << MaxBytesToEmit;
648 void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
649 unsigned MaxBytesToEmit) {
650 // Emit with a text fill value.
651 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
655 void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
656 unsigned char Value) {
657 // FIXME: Verify that Offset is associated with the current section.
658 OS << ".org " << *Offset << ", " << (unsigned) Value;
663 void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
664 assert(MAI.hasSingleParameterDotFile());
666 PrintQuotedString(Filename, OS);
670 bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
672 OS << "\t.file\t" << FileNo << ' ';
673 PrintQuotedString(Filename, OS);
676 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
679 void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
680 unsigned Column, unsigned Flags,
682 unsigned Discriminator) {
683 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
688 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
689 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
690 OS << " basic_block";
691 if (Flags & DWARF2_FLAG_PROLOGUE_END)
692 OS << " prologue_end";
693 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
694 OS << " epilogue_begin";
696 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
697 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
700 if (Flags & DWARF2_FLAG_IS_STMT)
709 OS << "discriminator " << Discriminator;
713 bool MCAsmStreamer::EmitCFIStartProc() {
714 if (this->MCStreamer::EmitCFIStartProc())
717 OS << ".cfi_startproc";
723 bool MCAsmStreamer::EmitCFIEndProc() {
724 if (this->MCStreamer::EmitCFIEndProc())
727 OS << ".cfi_endproc";
733 bool MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
734 if (this->MCStreamer::EmitCFIDefCfaOffset(Offset))
737 OS << ".cfi_def_cfa_offset " << Offset;
743 bool MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
744 if (this->MCStreamer::EmitCFIDefCfaRegister(Register))
747 OS << ".cfi_def_cfa_register " << Register;
753 bool MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
754 if (this->MCStreamer::EmitCFIOffset(Register, Offset))
757 OS << ".cfi_offset " << Register << ", " << Offset;
763 bool MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym) {
764 if (this->MCStreamer::EmitCFIPersonality(Sym))
767 OS << ".cfi_personality 0, " << *Sym;
773 bool MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym) {
774 if (this->MCStreamer::EmitCFILsda(Sym))
777 OS << ".cfi_lsda 0, " << *Sym;
783 void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
784 raw_ostream &OS = GetCommentOS();
785 SmallString<256> Code;
786 SmallVector<MCFixup, 4> Fixups;
787 raw_svector_ostream VecOS(Code);
788 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
791 // If we are showing fixups, create symbolic markers in the encoded
792 // representation. We do this by making a per-bit map to the fixup item index,
793 // then trying to display it as nicely as possible.
794 SmallVector<uint8_t, 64> FixupMap;
795 FixupMap.resize(Code.size() * 8);
796 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
799 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
800 MCFixup &F = Fixups[i];
801 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
802 for (unsigned j = 0; j != Info.TargetSize; ++j) {
803 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
804 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
805 FixupMap[Index] = 1 + i;
810 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
814 // See if all bits are the same map entry.
815 uint8_t MapEntry = FixupMap[i * 8 + 0];
816 for (unsigned j = 1; j != 8; ++j) {
817 if (FixupMap[i * 8 + j] == MapEntry)
820 MapEntry = uint8_t(~0U);
824 if (MapEntry != uint8_t(~0U)) {
826 OS << format("0x%02x", uint8_t(Code[i]));
828 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
829 OS << char('A' + MapEntry - 1);
832 // Otherwise, write out in binary.
834 for (unsigned j = 8; j--;) {
835 unsigned Bit = (Code[i] >> j) & 1;
839 FixupBit = i * 8 + j;
841 FixupBit = i * 8 + (7-j);
843 if (uint8_t MapEntry = FixupMap[FixupBit]) {
844 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
845 OS << char('A' + MapEntry - 1);
853 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
854 MCFixup &F = Fixups[i];
855 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
856 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
857 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
861 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
862 assert(CurSection && "Cannot emit contents before setting section!");
865 MCLineEntry::Make(this, getCurrentSection());
867 // Show the encoding in a comment if we have a code emitter.
869 AddEncodingComment(Inst);
871 // Show the MCInst if enabled.
873 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
874 GetCommentOS() << "\n";
877 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
879 InstPrinter->printInst(&Inst, OS);
881 Inst.print(OS, &MAI);
885 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
886 /// the specified string in the output .s file. This capability is
887 /// indicated by the hasRawTextSupport() predicate.
888 void MCAsmStreamer::EmitRawText(StringRef String) {
889 if (!String.empty() && String.back() == '\n')
890 String = String.substr(0, String.size()-1);
895 void MCAsmStreamer::Finish() {
896 // Dump out the dwarf file & directory tables and line tables.
897 if (getContext().hasDwarfFiles() && TLOF)
898 MCDwarfFileTable::Emit(this, TLOF->getDwarfLineSection(),
899 TLOF->getTextSection());
902 MCStreamer *llvm::createAsmStreamer(MCContext &Context,
903 formatted_raw_ostream &OS,
905 bool isVerboseAsm, MCInstPrinter *IP,
906 MCCodeEmitter *CE, bool ShowInst) {
907 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
908 NULL, 0, IP, CE, ShowInst);
912 MCStreamer *llvm::createAsmStreamerNoLoc(MCContext &Context,
913 formatted_raw_ostream &OS,
916 const TargetLoweringObjectFile *TLOF,
919 MCCodeEmitter *CE, bool ShowInst) {
920 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
921 TLOF, PointerSize, IP, CE, ShowInst);