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/TargetAsmBackend.h"
27 #include "llvm/Target/TargetAsmInfo.h"
28 #include "llvm/Target/TargetLoweringObjectFile.h"
33 class MCAsmStreamer : public MCStreamer {
34 formatted_raw_ostream &OS;
36 OwningPtr<MCInstPrinter> InstPrinter;
37 OwningPtr<MCCodeEmitter> Emitter;
38 OwningPtr<TargetAsmBackend> AsmBackend;
40 SmallString<128> CommentToEmit;
41 raw_svector_ostream CommentStream;
43 unsigned IsVerboseAsm : 1;
44 unsigned ShowInst : 1;
47 bool needsSet(const MCExpr *Value);
50 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
53 MCInstPrinter *printer, MCCodeEmitter *emitter,
54 TargetAsmBackend *asmbackend,
56 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
57 InstPrinter(printer), Emitter(emitter), AsmBackend(asmbackend),
58 CommentStream(CommentToEmit), IsVerboseAsm(isVerboseAsm),
59 ShowInst(showInst), UseLoc(useLoc) {
60 if (InstPrinter && IsVerboseAsm)
61 InstPrinter->setCommentStream(CommentStream);
65 inline void EmitEOL() {
66 // If we don't have any comments, just emit a \n.
73 void EmitCommentsAndEOL();
75 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
77 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
79 /// hasRawTextSupport - We support EmitRawText.
80 virtual bool hasRawTextSupport() const { return true; }
82 /// AddComment - Add a comment that can be emitted to the generated .s
83 /// file if applicable as a QoI issue to make the output of the compiler
84 /// more readable. This only affects the MCAsmStreamer, and only when
85 /// verbose assembly output is enabled.
86 virtual void AddComment(const Twine &T);
88 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
89 virtual void AddEncodingComment(const MCInst &Inst);
91 /// GetCommentOS - Return a raw_ostream that comments can be written to.
92 /// Unlike AddComment, you are required to terminate comments with \n if you
94 virtual raw_ostream &GetCommentOS() {
96 return nulls(); // Discard comments unless in verbose asm mode.
100 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
101 virtual void AddBlankLine() {
105 /// @name MCStreamer Interface
108 virtual void SwitchSection(const MCSection *Section);
110 virtual void InitSections() {
111 // FIXME, this is MachO specific, but the testsuite
113 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
114 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
115 0, SectionKind::getText()));
118 virtual void EmitLabel(MCSymbol *Symbol);
120 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
121 virtual void EmitThumbFunc(MCSymbol *Func);
123 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
124 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
125 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
126 const MCSymbol *LastLabel,
127 const MCSymbol *Label);
129 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
131 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
132 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
133 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
134 virtual void EmitCOFFSymbolType(int Type);
135 virtual void EndCOFFSymbolDef();
136 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
137 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
138 unsigned ByteAlignment);
140 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
142 /// @param Symbol - The common symbol to emit.
143 /// @param Size - The size of the common symbol.
144 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
146 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
147 unsigned Size = 0, unsigned ByteAlignment = 0);
149 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
150 uint64_t Size, unsigned ByteAlignment = 0);
152 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
154 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
155 bool isPCRel, unsigned AddrSpace);
156 virtual void EmitIntValue(uint64_t Value, unsigned Size,
157 unsigned AddrSpace = 0);
159 virtual void EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
161 virtual void EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
163 virtual void EmitGPRel32Value(const MCExpr *Value);
166 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
169 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
170 unsigned ValueSize = 1,
171 unsigned MaxBytesToEmit = 0);
173 virtual void EmitCodeAlignment(unsigned ByteAlignment,
174 unsigned MaxBytesToEmit = 0);
176 virtual void EmitValueToOffset(const MCExpr *Offset,
177 unsigned char Value = 0);
179 virtual void EmitFileDirective(StringRef Filename);
180 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
181 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
182 unsigned Column, unsigned Flags,
183 unsigned Isa, unsigned Discriminator);
185 virtual bool EmitCFIStartProc();
186 virtual bool EmitCFIEndProc();
187 virtual bool EmitCFIDefCfaOffset(int64_t Offset);
188 virtual bool EmitCFIDefCfaRegister(int64_t Register);
189 virtual bool EmitCFIOffset(int64_t Register, int64_t Offset);
190 virtual bool EmitCFIPersonality(const MCSymbol *Sym);
191 virtual bool EmitCFILsda(const MCSymbol *Sym);
193 virtual void EmitInstruction(const MCInst &Inst);
195 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
196 /// the specified string in the output .s file. This capability is
197 /// indicated by the hasRawTextSupport() predicate.
198 virtual void EmitRawText(StringRef String);
200 virtual void Finish();
205 } // end anonymous namespace.
207 /// AddComment - Add a comment that can be emitted to the generated .s
208 /// file if applicable as a QoI issue to make the output of the compiler
209 /// more readable. This only affects the MCAsmStreamer, and only when
210 /// verbose assembly output is enabled.
211 void MCAsmStreamer::AddComment(const Twine &T) {
212 if (!IsVerboseAsm) return;
214 // Make sure that CommentStream is flushed.
215 CommentStream.flush();
217 T.toVector(CommentToEmit);
218 // Each comment goes on its own line.
219 CommentToEmit.push_back('\n');
221 // Tell the comment stream that the vector changed underneath it.
222 CommentStream.resync();
225 void MCAsmStreamer::EmitCommentsAndEOL() {
226 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
231 CommentStream.flush();
232 StringRef Comments = CommentToEmit.str();
234 assert(Comments.back() == '\n' &&
235 "Comment array not newline terminated");
237 // Emit a line of comments.
238 OS.PadToColumn(MAI.getCommentColumn());
239 size_t Position = Comments.find('\n');
240 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
242 Comments = Comments.substr(Position+1);
243 } while (!Comments.empty());
245 CommentToEmit.clear();
246 // Tell the comment stream that the vector changed underneath it.
247 CommentStream.resync();
250 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
251 assert(Bytes && "Invalid size!");
252 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
255 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
256 assert(Section && "Cannot switch to a null section!");
257 if (Section != CurSection) {
258 PrevSection = CurSection;
259 CurSection = Section;
260 Section->PrintSwitchToSection(MAI, OS);
264 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
265 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
266 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
267 assert(CurSection && "Cannot emit before setting section!");
269 OS << *Symbol << MAI.getLabelSuffix();
271 Symbol->setSection(*CurSection);
274 void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
276 default: assert(0 && "Invalid flag!");
277 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
278 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
279 case MCAF_Code16: OS << "\t.code\t16"; break;
280 case MCAF_Code32: OS << "\t.code\t32"; break;
285 void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
286 // This needs to emit to a temporary string to get properly quoted
287 // MCSymbols when they have spaces in them.
288 OS << "\t.thumb_func";
294 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
295 OS << *Symbol << " = " << *Value;
298 // FIXME: Lift context changes into super class.
299 Symbol->setVariableValue(Value);
302 void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
303 OS << ".weakref " << *Alias << ", " << *Symbol;
307 void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
308 const MCSymbol *LastLabel,
309 const MCSymbol *Label) {
310 EmitDwarfSetLineAddr(LineDelta, Label,
311 getContext().getTargetAsmInfo().getPointerSize());
314 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
315 MCSymbolAttr Attribute) {
317 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
318 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
319 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
320 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
321 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
322 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
323 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
324 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
325 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
326 OS << "\t.type\t" << *Symbol << ','
327 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
329 default: assert(0 && "Unknown ELF .type");
330 case MCSA_ELF_TypeFunction: OS << "function"; break;
331 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
332 case MCSA_ELF_TypeObject: OS << "object"; break;
333 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
334 case MCSA_ELF_TypeCommon: OS << "common"; break;
335 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
336 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
340 case MCSA_Global: // .globl/.global
341 OS << MAI.getGlobalDirective();
343 case MCSA_Hidden: OS << "\t.hidden\t"; break;
344 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
345 case MCSA_Internal: OS << "\t.internal\t"; break;
346 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
347 case MCSA_Local: OS << "\t.local\t"; break;
348 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
349 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
350 case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break;
351 case MCSA_Protected: OS << "\t.protected\t"; break;
352 case MCSA_Reference: OS << "\t.reference\t"; break;
353 case MCSA_Weak: OS << "\t.weak\t"; break;
354 case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break;
356 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
357 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
364 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
365 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
369 void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
370 OS << "\t.def\t " << *Symbol << ';';
374 void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
375 OS << "\t.scl\t" << StorageClass << ';';
379 void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
380 OS << "\t.type\t" << Type << ';';
384 void MCAsmStreamer::EndCOFFSymbolDef() {
389 void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
390 assert(MAI.hasDotTypeDotSizeDirective());
391 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
394 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
395 unsigned ByteAlignment) {
396 OS << "\t.comm\t" << *Symbol << ',' << Size;
397 if (ByteAlignment != 0) {
398 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
399 OS << ',' << ByteAlignment;
401 OS << ',' << Log2_32(ByteAlignment);
406 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
408 /// @param Symbol - The common symbol to emit.
409 /// @param Size - The size of the common symbol.
410 void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
411 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
412 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
416 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
417 unsigned Size, unsigned ByteAlignment) {
418 // Note: a .zerofill directive does not switch sections.
421 // This is a mach-o specific directive.
422 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
423 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
425 if (Symbol != NULL) {
426 OS << ',' << *Symbol << ',' << Size;
427 if (ByteAlignment != 0)
428 OS << ',' << Log2_32(ByteAlignment);
433 // .tbss sym, size, align
434 // This depends that the symbol has already been mangled from the original,
436 void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
437 uint64_t Size, unsigned ByteAlignment) {
438 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
439 // Instead of using the Section we'll just use the shortcut.
440 // This is a mach-o specific directive and section.
441 OS << ".tbss " << *Symbol << ", " << Size;
443 // Output align if we have it. We default to 1 so don't bother printing
445 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
450 static inline char toOctal(int X) { return (X&7)+'0'; }
452 static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
455 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
456 unsigned char C = Data[i];
457 if (C == '"' || C == '\\') {
458 OS << '\\' << (char)C;
462 if (isprint((unsigned char)C)) {
468 case '\b': OS << "\\b"; break;
469 case '\f': OS << "\\f"; break;
470 case '\n': OS << "\\n"; break;
471 case '\r': OS << "\\r"; break;
472 case '\t': OS << "\\t"; break;
475 OS << toOctal(C >> 6);
476 OS << toOctal(C >> 3);
477 OS << toOctal(C >> 0);
486 void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
487 assert(CurSection && "Cannot emit contents before setting section!");
488 if (Data.empty()) return;
490 if (Data.size() == 1) {
491 OS << MAI.getData8bitsDirective(AddrSpace);
492 OS << (unsigned)(unsigned char)Data[0];
497 // If the data ends with 0 and the target supports .asciz, use it, otherwise
499 if (MAI.getAscizDirective() && Data.back() == 0) {
500 OS << MAI.getAscizDirective();
501 Data = Data.substr(0, Data.size()-1);
503 OS << MAI.getAsciiDirective();
507 PrintQuotedString(Data, OS);
511 void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
512 unsigned AddrSpace) {
513 EmitValue(MCConstantExpr::Create(Value, getContext()), Size, AddrSpace);
516 void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
517 bool isPCRel, unsigned AddrSpace) {
518 assert(CurSection && "Cannot emit contents before setting section!");
519 assert(!isPCRel && "Cannot emit pc relative relocations!");
520 const char *Directive = 0;
523 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
524 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
525 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
527 Directive = MAI.getData64bitsDirective(AddrSpace);
528 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
529 if (Directive) break;
531 if (!Value->EvaluateAsAbsolute(IntValue))
532 report_fatal_error("Don't know how to emit this value.");
533 if (getContext().getTargetAsmInfo().isLittleEndian()) {
534 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
535 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
537 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
538 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
543 assert(Directive && "Invalid size for machine code value!");
544 OS << Directive << *Value;
548 void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace) {
550 if (Value->EvaluateAsAbsolute(IntValue)) {
551 EmitULEB128IntValue(IntValue, AddrSpace);
554 assert(MAI.hasLEB128() && "Cannot print a .uleb");
555 OS << ".uleb128 " << *Value;
559 void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace) {
561 if (Value->EvaluateAsAbsolute(IntValue)) {
562 EmitSLEB128IntValue(IntValue, AddrSpace);
565 assert(MAI.hasLEB128() && "Cannot print a .sleb");
566 OS << ".sleb128 " << *Value;
570 void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
571 assert(MAI.getGPRel32Directive() != 0);
572 OS << MAI.getGPRel32Directive() << *Value;
577 /// EmitFill - Emit NumBytes bytes worth of the value specified by
578 /// FillValue. This implements directives such as '.space'.
579 void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
580 unsigned AddrSpace) {
581 if (NumBytes == 0) return;
584 if (const char *ZeroDirective = MAI.getZeroDirective()) {
585 OS << ZeroDirective << NumBytes;
587 OS << ',' << (int)FillValue;
592 // Emit a byte at a time.
593 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
596 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
598 unsigned MaxBytesToEmit) {
599 // Some assemblers don't support non-power of two alignments, so we always
600 // emit alignments as a power of two if possible.
601 if (isPowerOf2_32(ByteAlignment)) {
603 default: llvm_unreachable("Invalid size for machine code value!");
604 case 1: OS << MAI.getAlignDirective(); break;
605 // FIXME: use MAI for this!
606 case 2: OS << ".p2alignw "; break;
607 case 4: OS << ".p2alignl "; break;
608 case 8: llvm_unreachable("Unsupported alignment size!");
611 if (MAI.getAlignmentIsInBytes())
614 OS << Log2_32(ByteAlignment);
616 if (Value || MaxBytesToEmit) {
618 OS.write_hex(truncateToSize(Value, ValueSize));
621 OS << ", " << MaxBytesToEmit;
627 // Non-power of two alignment. This is not widely supported by assemblers.
628 // FIXME: Parameterize this based on MAI.
630 default: llvm_unreachable("Invalid size for machine code value!");
631 case 1: OS << ".balign"; break;
632 case 2: OS << ".balignw"; break;
633 case 4: OS << ".balignl"; break;
634 case 8: llvm_unreachable("Unsupported alignment size!");
637 OS << ' ' << ByteAlignment;
638 OS << ", " << truncateToSize(Value, ValueSize);
640 OS << ", " << MaxBytesToEmit;
644 void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
645 unsigned MaxBytesToEmit) {
646 // Emit with a text fill value.
647 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
651 void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
652 unsigned char Value) {
653 // FIXME: Verify that Offset is associated with the current section.
654 OS << ".org " << *Offset << ", " << (unsigned) Value;
659 void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
660 assert(MAI.hasSingleParameterDotFile());
662 PrintQuotedString(Filename, OS);
666 bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
668 OS << "\t.file\t" << FileNo << ' ';
669 PrintQuotedString(Filename, OS);
672 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
675 void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
676 unsigned Column, unsigned Flags,
678 unsigned Discriminator) {
679 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
684 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
685 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
686 OS << " basic_block";
687 if (Flags & DWARF2_FLAG_PROLOGUE_END)
688 OS << " prologue_end";
689 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
690 OS << " epilogue_begin";
692 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
693 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
696 if (Flags & DWARF2_FLAG_IS_STMT)
705 OS << "discriminator " << Discriminator;
709 bool MCAsmStreamer::EmitCFIStartProc() {
710 if (this->MCStreamer::EmitCFIStartProc())
713 OS << ".cfi_startproc";
719 bool MCAsmStreamer::EmitCFIEndProc() {
720 if (this->MCStreamer::EmitCFIEndProc())
723 OS << ".cfi_endproc";
729 bool MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
730 if (this->MCStreamer::EmitCFIDefCfaOffset(Offset))
733 OS << ".cfi_def_cfa_offset " << Offset;
739 bool MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
740 if (this->MCStreamer::EmitCFIDefCfaRegister(Register))
743 OS << ".cfi_def_cfa_register " << Register;
749 bool MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
750 if (this->MCStreamer::EmitCFIOffset(Register, Offset))
753 OS << ".cfi_offset " << Register << ", " << Offset;
759 bool MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym) {
760 if (this->MCStreamer::EmitCFIPersonality(Sym))
763 OS << ".cfi_personality 0, " << *Sym;
769 bool MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym) {
770 if (this->MCStreamer::EmitCFILsda(Sym))
773 OS << ".cfi_lsda 0, " << *Sym;
779 void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
780 raw_ostream &OS = GetCommentOS();
781 SmallString<256> Code;
782 SmallVector<MCFixup, 4> Fixups;
783 raw_svector_ostream VecOS(Code);
784 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
787 // If we are showing fixups, create symbolic markers in the encoded
788 // representation. We do this by making a per-bit map to the fixup item index,
789 // then trying to display it as nicely as possible.
790 SmallVector<uint8_t, 64> FixupMap;
791 FixupMap.resize(Code.size() * 8);
792 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
795 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
796 MCFixup &F = Fixups[i];
797 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
798 for (unsigned j = 0; j != Info.TargetSize; ++j) {
799 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
800 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
801 FixupMap[Index] = 1 + i;
806 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
810 // See if all bits are the same map entry.
811 uint8_t MapEntry = FixupMap[i * 8 + 0];
812 for (unsigned j = 1; j != 8; ++j) {
813 if (FixupMap[i * 8 + j] == MapEntry)
816 MapEntry = uint8_t(~0U);
820 if (MapEntry != uint8_t(~0U)) {
822 OS << format("0x%02x", uint8_t(Code[i]));
824 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
825 OS << char('A' + MapEntry - 1);
828 // Otherwise, write out in binary.
830 for (unsigned j = 8; j--;) {
831 unsigned Bit = (Code[i] >> j) & 1;
834 if (getContext().getTargetAsmInfo().isLittleEndian())
835 FixupBit = i * 8 + j;
837 FixupBit = i * 8 + (7-j);
839 if (uint8_t MapEntry = FixupMap[FixupBit]) {
840 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
841 OS << char('A' + MapEntry - 1);
849 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
850 MCFixup &F = Fixups[i];
851 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
852 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
853 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
857 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
858 assert(CurSection && "Cannot emit contents before setting section!");
861 MCLineEntry::Make(this, getCurrentSection());
863 // Show the encoding in a comment if we have a code emitter.
865 AddEncodingComment(Inst);
867 // Show the MCInst if enabled.
869 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
870 GetCommentOS() << "\n";
873 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
875 InstPrinter->printInst(&Inst, OS);
877 Inst.print(OS, &MAI);
881 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
882 /// the specified string in the output .s file. This capability is
883 /// indicated by the hasRawTextSupport() predicate.
884 void MCAsmStreamer::EmitRawText(StringRef String) {
885 if (!String.empty() && String.back() == '\n')
886 String = String.substr(0, String.size()-1);
891 void MCAsmStreamer::Finish() {
892 // Dump out the dwarf file & directory tables and line tables.
893 if (getContext().hasDwarfFiles() && !UseLoc)
894 MCDwarfFileTable::Emit(this);
897 MCStreamer *llvm::createAsmStreamer(MCContext &Context,
898 formatted_raw_ostream &OS,
899 bool isVerboseAsm, bool useLoc,
900 MCInstPrinter *IP, MCCodeEmitter *CE,
901 TargetAsmBackend *TAB, bool ShowInst) {
902 return new MCAsmStreamer(Context, OS, isVerboseAsm, useLoc,
903 IP, CE, TAB, ShowInst);