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"
27 #include "llvm/Target/TargetAsmInfo.h"
32 class MCAsmStreamer : public MCStreamer {
33 formatted_raw_ostream &OS;
35 OwningPtr<MCInstPrinter> InstPrinter;
36 OwningPtr<MCCodeEmitter> Emitter;
38 SmallString<128> CommentToEmit;
39 raw_svector_ostream CommentStream;
41 unsigned IsVerboseAsm : 1;
42 unsigned ShowInst : 1;
45 bool needsSet(const MCExpr *Value);
48 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
51 MCInstPrinter *printer, MCCodeEmitter *emitter, bool showInst)
52 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
53 InstPrinter(printer), Emitter(emitter), CommentStream(CommentToEmit),
54 IsVerboseAsm(isVerboseAsm),
55 ShowInst(showInst), UseLoc(useLoc) {
56 if (InstPrinter && IsVerboseAsm)
57 InstPrinter->setCommentStream(CommentStream);
61 inline void EmitEOL() {
62 // If we don't have any comments, just emit a \n.
69 void EmitCommentsAndEOL();
71 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
73 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
75 /// hasRawTextSupport - We support EmitRawText.
76 virtual bool hasRawTextSupport() const { return true; }
78 /// AddComment - Add a comment that can be emitted to the generated .s
79 /// file if applicable as a QoI issue to make the output of the compiler
80 /// more readable. This only affects the MCAsmStreamer, and only when
81 /// verbose assembly output is enabled.
82 virtual void AddComment(const Twine &T);
84 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
85 virtual void AddEncodingComment(const MCInst &Inst);
87 /// GetCommentOS - Return a raw_ostream that comments can be written to.
88 /// Unlike AddComment, you are required to terminate comments with \n if you
90 virtual raw_ostream &GetCommentOS() {
92 return nulls(); // Discard comments unless in verbose asm mode.
96 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
97 virtual void AddBlankLine() {
101 /// @name MCStreamer Interface
104 virtual void SwitchSection(const MCSection *Section);
106 virtual void InitSections() {
107 // FIXME, this is MachO specific, but the testsuite
109 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
110 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
111 0, SectionKind::getText()));
114 virtual void EmitLabel(MCSymbol *Symbol);
116 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
117 virtual void EmitThumbFunc(MCSymbol *Func);
119 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
120 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
121 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
122 const MCSymbol *LastLabel,
123 const MCSymbol *Label);
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 EmitValueImpl(const MCExpr *Value, unsigned Size,
151 bool isPCRel, unsigned AddrSpace);
152 virtual void EmitIntValue(uint64_t Value, unsigned Size,
153 unsigned AddrSpace = 0);
155 virtual void EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
157 virtual void EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
159 virtual void EmitGPRel32Value(const MCExpr *Value);
162 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
165 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
166 unsigned ValueSize = 1,
167 unsigned MaxBytesToEmit = 0);
169 virtual void EmitCodeAlignment(unsigned ByteAlignment,
170 unsigned MaxBytesToEmit = 0);
172 virtual void EmitValueToOffset(const MCExpr *Offset,
173 unsigned char Value = 0);
175 virtual void EmitFileDirective(StringRef Filename);
176 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
177 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
178 unsigned Column, unsigned Flags,
179 unsigned Isa, unsigned Discriminator);
181 virtual bool EmitCFIStartProc();
182 virtual bool EmitCFIEndProc();
183 virtual bool EmitCFIDefCfaOffset(int64_t Offset);
184 virtual bool EmitCFIDefCfaRegister(int64_t Register);
185 virtual bool EmitCFIOffset(int64_t Register, int64_t Offset);
186 virtual bool EmitCFIPersonality(const MCSymbol *Sym);
187 virtual bool EmitCFILsda(const MCSymbol *Sym);
189 virtual void EmitInstruction(const MCInst &Inst);
191 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
192 /// the specified string in the output .s file. This capability is
193 /// indicated by the hasRawTextSupport() predicate.
194 virtual void EmitRawText(StringRef String);
196 virtual void Finish();
201 } // end anonymous namespace.
203 /// AddComment - Add a comment that can be emitted to the generated .s
204 /// file if applicable as a QoI issue to make the output of the compiler
205 /// more readable. This only affects the MCAsmStreamer, and only when
206 /// verbose assembly output is enabled.
207 void MCAsmStreamer::AddComment(const Twine &T) {
208 if (!IsVerboseAsm) return;
210 // Make sure that CommentStream is flushed.
211 CommentStream.flush();
213 T.toVector(CommentToEmit);
214 // Each comment goes on its own line.
215 CommentToEmit.push_back('\n');
217 // Tell the comment stream that the vector changed underneath it.
218 CommentStream.resync();
221 void MCAsmStreamer::EmitCommentsAndEOL() {
222 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
227 CommentStream.flush();
228 StringRef Comments = CommentToEmit.str();
230 assert(Comments.back() == '\n' &&
231 "Comment array not newline terminated");
233 // Emit a line of comments.
234 OS.PadToColumn(MAI.getCommentColumn());
235 size_t Position = Comments.find('\n');
236 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
238 Comments = Comments.substr(Position+1);
239 } while (!Comments.empty());
241 CommentToEmit.clear();
242 // Tell the comment stream that the vector changed underneath it.
243 CommentStream.resync();
246 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
247 assert(Bytes && "Invalid size!");
248 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
251 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
252 assert(Section && "Cannot switch to a null section!");
253 if (Section != CurSection) {
254 PrevSection = CurSection;
255 CurSection = Section;
256 Section->PrintSwitchToSection(MAI, OS);
260 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
261 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
262 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
263 assert(CurSection && "Cannot emit before setting section!");
265 OS << *Symbol << MAI.getLabelSuffix();
267 Symbol->setSection(*CurSection);
270 void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
272 default: assert(0 && "Invalid flag!");
273 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
274 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
275 case MCAF_Code16: OS << "\t.code\t16"; break;
276 case MCAF_Code32: OS << "\t.code\t32"; break;
281 void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
282 // This needs to emit to a temporary string to get properly quoted
283 // MCSymbols when they have spaces in them.
284 OS << "\t.thumb_func";
290 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
291 OS << *Symbol << " = " << *Value;
294 // FIXME: Lift context changes into super class.
295 Symbol->setVariableValue(Value);
298 void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
299 OS << ".weakref " << *Alias << ", " << *Symbol;
303 void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
304 const MCSymbol *LastLabel,
305 const MCSymbol *Label) {
306 EmitDwarfSetLineAddr(LineDelta, Label,
307 getContext().getTargetAsmInfo().getPointerSize());
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::EmitIntValue(uint64_t Value, unsigned Size,
508 unsigned AddrSpace) {
509 EmitValue(MCConstantExpr::Create(Value, getContext()), Size, AddrSpace);
512 void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
513 bool isPCRel, unsigned AddrSpace) {
514 assert(CurSection && "Cannot emit contents before setting section!");
515 assert(!isPCRel && "Cannot emit pc relative relocations!");
516 const char *Directive = 0;
519 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
520 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
521 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
523 Directive = MAI.getData64bitsDirective(AddrSpace);
524 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
525 if (Directive) break;
527 if (!Value->EvaluateAsAbsolute(IntValue))
528 report_fatal_error("Don't know how to emit this value.");
529 if (getContext().getTargetAsmInfo().isLittleEndian()) {
530 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
531 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
533 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
534 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
539 assert(Directive && "Invalid size for machine code value!");
540 OS << Directive << *Value;
544 void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace) {
546 if (Value->EvaluateAsAbsolute(IntValue)) {
547 EmitULEB128IntValue(IntValue, AddrSpace);
550 assert(MAI.hasLEB128() && "Cannot print a .uleb");
551 OS << ".uleb128 " << *Value;
555 void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace) {
557 if (Value->EvaluateAsAbsolute(IntValue)) {
558 EmitSLEB128IntValue(IntValue, AddrSpace);
561 assert(MAI.hasLEB128() && "Cannot print a .sleb");
562 OS << ".sleb128 " << *Value;
566 void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
567 assert(MAI.getGPRel32Directive() != 0);
568 OS << MAI.getGPRel32Directive() << *Value;
573 /// EmitFill - Emit NumBytes bytes worth of the value specified by
574 /// FillValue. This implements directives such as '.space'.
575 void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
576 unsigned AddrSpace) {
577 if (NumBytes == 0) return;
580 if (const char *ZeroDirective = MAI.getZeroDirective()) {
581 OS << ZeroDirective << NumBytes;
583 OS << ',' << (int)FillValue;
588 // Emit a byte at a time.
589 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
592 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
594 unsigned MaxBytesToEmit) {
595 // Some assemblers don't support non-power of two alignments, so we always
596 // emit alignments as a power of two if possible.
597 if (isPowerOf2_32(ByteAlignment)) {
599 default: llvm_unreachable("Invalid size for machine code value!");
600 case 1: OS << MAI.getAlignDirective(); break;
601 // FIXME: use MAI for this!
602 case 2: OS << ".p2alignw "; break;
603 case 4: OS << ".p2alignl "; break;
604 case 8: llvm_unreachable("Unsupported alignment size!");
607 if (MAI.getAlignmentIsInBytes())
610 OS << Log2_32(ByteAlignment);
612 if (Value || MaxBytesToEmit) {
614 OS.write_hex(truncateToSize(Value, ValueSize));
617 OS << ", " << MaxBytesToEmit;
623 // Non-power of two alignment. This is not widely supported by assemblers.
624 // FIXME: Parameterize this based on MAI.
626 default: llvm_unreachable("Invalid size for machine code value!");
627 case 1: OS << ".balign"; break;
628 case 2: OS << ".balignw"; break;
629 case 4: OS << ".balignl"; break;
630 case 8: llvm_unreachable("Unsupported alignment size!");
633 OS << ' ' << ByteAlignment;
634 OS << ", " << truncateToSize(Value, ValueSize);
636 OS << ", " << MaxBytesToEmit;
640 void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
641 unsigned MaxBytesToEmit) {
642 // Emit with a text fill value.
643 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
647 void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
648 unsigned char Value) {
649 // FIXME: Verify that Offset is associated with the current section.
650 OS << ".org " << *Offset << ", " << (unsigned) Value;
655 void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
656 assert(MAI.hasSingleParameterDotFile());
658 PrintQuotedString(Filename, OS);
662 bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
664 OS << "\t.file\t" << FileNo << ' ';
665 PrintQuotedString(Filename, OS);
668 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
671 void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
672 unsigned Column, unsigned Flags,
674 unsigned Discriminator) {
675 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
680 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
681 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
682 OS << " basic_block";
683 if (Flags & DWARF2_FLAG_PROLOGUE_END)
684 OS << " prologue_end";
685 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
686 OS << " epilogue_begin";
688 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
689 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
692 if (Flags & DWARF2_FLAG_IS_STMT)
701 OS << "discriminator " << Discriminator;
705 bool MCAsmStreamer::EmitCFIStartProc() {
706 if (this->MCStreamer::EmitCFIStartProc())
709 OS << ".cfi_startproc";
715 bool MCAsmStreamer::EmitCFIEndProc() {
716 if (this->MCStreamer::EmitCFIEndProc())
719 OS << ".cfi_endproc";
725 bool MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
726 if (this->MCStreamer::EmitCFIDefCfaOffset(Offset))
729 OS << ".cfi_def_cfa_offset " << Offset;
735 bool MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
736 if (this->MCStreamer::EmitCFIDefCfaRegister(Register))
739 OS << ".cfi_def_cfa_register " << Register;
745 bool MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
746 if (this->MCStreamer::EmitCFIOffset(Register, Offset))
749 OS << ".cfi_offset " << Register << ", " << Offset;
755 bool MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym) {
756 if (this->MCStreamer::EmitCFIPersonality(Sym))
759 OS << ".cfi_personality 0, " << *Sym;
765 bool MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym) {
766 if (this->MCStreamer::EmitCFILsda(Sym))
769 OS << ".cfi_lsda 0, " << *Sym;
775 void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
776 raw_ostream &OS = GetCommentOS();
777 SmallString<256> Code;
778 SmallVector<MCFixup, 4> Fixups;
779 raw_svector_ostream VecOS(Code);
780 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
783 // If we are showing fixups, create symbolic markers in the encoded
784 // representation. We do this by making a per-bit map to the fixup item index,
785 // then trying to display it as nicely as possible.
786 SmallVector<uint8_t, 64> FixupMap;
787 FixupMap.resize(Code.size() * 8);
788 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
791 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
792 MCFixup &F = Fixups[i];
793 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
794 for (unsigned j = 0; j != Info.TargetSize; ++j) {
795 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
796 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
797 FixupMap[Index] = 1 + i;
802 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
806 // See if all bits are the same map entry.
807 uint8_t MapEntry = FixupMap[i * 8 + 0];
808 for (unsigned j = 1; j != 8; ++j) {
809 if (FixupMap[i * 8 + j] == MapEntry)
812 MapEntry = uint8_t(~0U);
816 if (MapEntry != uint8_t(~0U)) {
818 OS << format("0x%02x", uint8_t(Code[i]));
820 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
821 OS << char('A' + MapEntry - 1);
824 // Otherwise, write out in binary.
826 for (unsigned j = 8; j--;) {
827 unsigned Bit = (Code[i] >> j) & 1;
830 if (getContext().getTargetAsmInfo().isLittleEndian())
831 FixupBit = i * 8 + j;
833 FixupBit = i * 8 + (7-j);
835 if (uint8_t MapEntry = FixupMap[FixupBit]) {
836 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
837 OS << char('A' + MapEntry - 1);
845 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
846 MCFixup &F = Fixups[i];
847 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
848 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
849 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
853 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
854 assert(CurSection && "Cannot emit contents before setting section!");
857 MCLineEntry::Make(this, getCurrentSection());
859 // Show the encoding in a comment if we have a code emitter.
861 AddEncodingComment(Inst);
863 // Show the MCInst if enabled.
865 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
866 GetCommentOS() << "\n";
869 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
871 InstPrinter->printInst(&Inst, OS);
873 Inst.print(OS, &MAI);
877 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
878 /// the specified string in the output .s file. This capability is
879 /// indicated by the hasRawTextSupport() predicate.
880 void MCAsmStreamer::EmitRawText(StringRef String) {
881 if (!String.empty() && String.back() == '\n')
882 String = String.substr(0, String.size()-1);
887 void MCAsmStreamer::Finish() {
888 // Dump out the dwarf file & directory tables and line tables.
889 if (getContext().hasDwarfFiles() && !UseLoc)
890 MCDwarfFileTable::Emit(this);
893 MCStreamer *llvm::createAsmStreamer(MCContext &Context,
894 formatted_raw_ostream &OS,
895 bool isVerboseAsm, bool useLoc,
897 MCCodeEmitter *CE, bool ShowInst) {
898 return new MCAsmStreamer(Context, OS, isVerboseAsm, useLoc,