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/MCFixupKindInfo.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCInstPrinter.h"
18 #include "llvm/MC/MCSectionMachO.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/ADT/OwningPtr.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/MathExtras.h"
25 #include "llvm/Support/Format.h"
26 #include "llvm/Support/FormattedStream.h"
27 #include "llvm/Target/TargetAsmBackend.h"
28 #include "llvm/Target/TargetAsmInfo.h"
29 #include "llvm/Target/TargetLoweringObjectFile.h"
35 class MCAsmStreamer : public MCStreamer {
36 formatted_raw_ostream &OS;
38 OwningPtr<MCInstPrinter> InstPrinter;
39 OwningPtr<MCCodeEmitter> Emitter;
40 OwningPtr<TargetAsmBackend> AsmBackend;
42 SmallString<128> CommentToEmit;
43 raw_svector_ostream CommentStream;
45 unsigned IsVerboseAsm : 1;
46 unsigned ShowInst : 1;
49 bool needsSet(const MCExpr *Value);
52 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
55 MCInstPrinter *printer, MCCodeEmitter *emitter,
56 TargetAsmBackend *asmbackend,
58 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
59 InstPrinter(printer), Emitter(emitter), AsmBackend(asmbackend),
60 CommentStream(CommentToEmit), IsVerboseAsm(isVerboseAsm),
61 ShowInst(showInst), UseLoc(useLoc) {
62 if (InstPrinter && IsVerboseAsm)
63 InstPrinter->setCommentStream(CommentStream);
67 inline void EmitEOL() {
68 // If we don't have any comments, just emit a \n.
75 void EmitCommentsAndEOL();
77 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
79 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
81 /// hasRawTextSupport - We support EmitRawText.
82 virtual bool hasRawTextSupport() const { return true; }
84 /// AddComment - Add a comment that can be emitted to the generated .s
85 /// file if applicable as a QoI issue to make the output of the compiler
86 /// more readable. This only affects the MCAsmStreamer, and only when
87 /// verbose assembly output is enabled.
88 virtual void AddComment(const Twine &T);
90 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
91 virtual void AddEncodingComment(const MCInst &Inst);
93 /// GetCommentOS - Return a raw_ostream that comments can be written to.
94 /// Unlike AddComment, you are required to terminate comments with \n if you
96 virtual raw_ostream &GetCommentOS() {
98 return nulls(); // Discard comments unless in verbose asm mode.
102 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
103 virtual void AddBlankLine() {
107 /// @name MCStreamer Interface
110 virtual void ChangeSection(const MCSection *Section);
112 virtual void InitSections() {
113 // FIXME, this is MachO specific, but the testsuite
115 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
116 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
117 0, SectionKind::getText()));
120 virtual void EmitLabel(MCSymbol *Symbol);
122 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
123 virtual void EmitThumbFunc(MCSymbol *Func);
125 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
126 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
127 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
128 const MCSymbol *LastLabel,
129 const MCSymbol *Label);
131 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
133 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
134 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
135 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
136 virtual void EmitCOFFSymbolType(int Type);
137 virtual void EndCOFFSymbolDef();
138 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
139 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
140 unsigned ByteAlignment);
142 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
144 /// @param Symbol - The common symbol to emit.
145 /// @param Size - The size of the common symbol.
146 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
148 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
149 unsigned Size = 0, unsigned ByteAlignment = 0);
151 virtual void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
152 uint64_t Size, unsigned ByteAlignment = 0);
154 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
156 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
157 bool isPCRel, unsigned AddrSpace);
158 virtual void EmitIntValue(uint64_t Value, unsigned Size,
159 unsigned AddrSpace = 0);
161 virtual void EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
163 virtual void EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
165 virtual void EmitGPRel32Value(const MCExpr *Value);
168 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
171 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
172 unsigned ValueSize = 1,
173 unsigned MaxBytesToEmit = 0);
175 virtual void EmitCodeAlignment(unsigned ByteAlignment,
176 unsigned MaxBytesToEmit = 0);
178 virtual void EmitValueToOffset(const MCExpr *Offset,
179 unsigned char Value = 0);
181 virtual void EmitFileDirective(StringRef Filename);
182 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
183 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
184 unsigned Column, unsigned Flags,
185 unsigned Isa, unsigned Discriminator);
187 virtual bool EmitCFIStartProc();
188 virtual bool EmitCFIEndProc();
189 virtual bool EmitCFIDefCfaOffset(int64_t Offset);
190 virtual bool EmitCFIDefCfaRegister(int64_t Register);
191 virtual bool EmitCFIOffset(int64_t Register, int64_t Offset);
192 virtual bool EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
193 virtual bool EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
195 virtual void EmitInstruction(const MCInst &Inst);
197 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
198 /// the specified string in the output .s file. This capability is
199 /// indicated by the hasRawTextSupport() predicate.
200 virtual void EmitRawText(StringRef String);
202 virtual void Finish();
207 } // end anonymous namespace.
209 /// AddComment - Add a comment that can be emitted to the generated .s
210 /// file if applicable as a QoI issue to make the output of the compiler
211 /// more readable. This only affects the MCAsmStreamer, and only when
212 /// verbose assembly output is enabled.
213 void MCAsmStreamer::AddComment(const Twine &T) {
214 if (!IsVerboseAsm) return;
216 // Make sure that CommentStream is flushed.
217 CommentStream.flush();
219 T.toVector(CommentToEmit);
220 // Each comment goes on its own line.
221 CommentToEmit.push_back('\n');
223 // Tell the comment stream that the vector changed underneath it.
224 CommentStream.resync();
227 void MCAsmStreamer::EmitCommentsAndEOL() {
228 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
233 CommentStream.flush();
234 StringRef Comments = CommentToEmit.str();
236 assert(Comments.back() == '\n' &&
237 "Comment array not newline terminated");
239 // Emit a line of comments.
240 OS.PadToColumn(MAI.getCommentColumn());
241 size_t Position = Comments.find('\n');
242 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
244 Comments = Comments.substr(Position+1);
245 } while (!Comments.empty());
247 CommentToEmit.clear();
248 // Tell the comment stream that the vector changed underneath it.
249 CommentStream.resync();
252 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
253 assert(Bytes && "Invalid size!");
254 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
257 void MCAsmStreamer::ChangeSection(const MCSection *Section) {
258 assert(Section && "Cannot switch to a null section!");
259 Section->PrintSwitchToSection(MAI, OS);
262 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
263 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
264 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
265 assert(getCurrentSection() && "Cannot emit before setting section!");
267 OS << *Symbol << MAI.getLabelSuffix();
269 Symbol->setSection(*getCurrentSection());
272 void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
274 default: assert(0 && "Invalid flag!");
275 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
276 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
277 case MCAF_Code16: OS << "\t.code\t16"; break;
278 case MCAF_Code32: OS << "\t.code\t32"; break;
283 void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
284 // This needs to emit to a temporary string to get properly quoted
285 // MCSymbols when they have spaces in them.
286 OS << "\t.thumb_func";
292 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
293 OS << *Symbol << " = " << *Value;
296 // FIXME: Lift context changes into super class.
297 Symbol->setVariableValue(Value);
300 void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
301 OS << ".weakref " << *Alias << ", " << *Symbol;
305 void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
306 const MCSymbol *LastLabel,
307 const MCSymbol *Label) {
308 EmitDwarfSetLineAddr(LineDelta, Label,
309 getContext().getTargetAsmInfo().getPointerSize());
312 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
313 MCSymbolAttr Attribute) {
315 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
316 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
317 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
318 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
319 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
320 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
321 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
322 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
323 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
324 OS << "\t.type\t" << *Symbol << ','
325 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
327 default: assert(0 && "Unknown ELF .type");
328 case MCSA_ELF_TypeFunction: OS << "function"; break;
329 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
330 case MCSA_ELF_TypeObject: OS << "object"; break;
331 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
332 case MCSA_ELF_TypeCommon: OS << "common"; break;
333 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
334 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
338 case MCSA_Global: // .globl/.global
339 OS << MAI.getGlobalDirective();
341 case MCSA_Hidden: OS << "\t.hidden\t"; break;
342 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
343 case MCSA_Internal: OS << "\t.internal\t"; break;
344 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
345 case MCSA_Local: OS << "\t.local\t"; break;
346 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
347 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
348 case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break;
349 case MCSA_Protected: OS << "\t.protected\t"; break;
350 case MCSA_Reference: OS << "\t.reference\t"; break;
351 case MCSA_Weak: OS << "\t.weak\t"; break;
352 case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break;
354 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
355 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
362 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
363 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
367 void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
368 OS << "\t.def\t " << *Symbol << ';';
372 void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
373 OS << "\t.scl\t" << StorageClass << ';';
377 void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
378 OS << "\t.type\t" << Type << ';';
382 void MCAsmStreamer::EndCOFFSymbolDef() {
387 void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
388 assert(MAI.hasDotTypeDotSizeDirective());
389 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
392 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
393 unsigned ByteAlignment) {
394 OS << "\t.comm\t" << *Symbol << ',' << Size;
395 if (ByteAlignment != 0) {
396 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
397 OS << ',' << ByteAlignment;
399 OS << ',' << Log2_32(ByteAlignment);
404 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
406 /// @param Symbol - The common symbol to emit.
407 /// @param Size - The size of the common symbol.
408 void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
409 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
410 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
414 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
415 unsigned Size, unsigned ByteAlignment) {
416 // Note: a .zerofill directive does not switch sections.
419 // This is a mach-o specific directive.
420 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
421 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
423 if (Symbol != NULL) {
424 OS << ',' << *Symbol << ',' << Size;
425 if (ByteAlignment != 0)
426 OS << ',' << Log2_32(ByteAlignment);
431 // .tbss sym, size, align
432 // This depends that the symbol has already been mangled from the original,
434 void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
435 uint64_t Size, unsigned ByteAlignment) {
436 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
437 // Instead of using the Section we'll just use the shortcut.
438 // This is a mach-o specific directive and section.
439 OS << ".tbss " << *Symbol << ", " << Size;
441 // Output align if we have it. We default to 1 so don't bother printing
443 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
448 static inline char toOctal(int X) { return (X&7)+'0'; }
450 static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
453 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
454 unsigned char C = Data[i];
455 if (C == '"' || C == '\\') {
456 OS << '\\' << (char)C;
460 if (isprint((unsigned char)C)) {
466 case '\b': OS << "\\b"; break;
467 case '\f': OS << "\\f"; break;
468 case '\n': OS << "\\n"; break;
469 case '\r': OS << "\\r"; break;
470 case '\t': OS << "\\t"; break;
473 OS << toOctal(C >> 6);
474 OS << toOctal(C >> 3);
475 OS << toOctal(C >> 0);
484 void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
485 assert(getCurrentSection() && "Cannot emit contents before setting section!");
486 if (Data.empty()) return;
488 if (Data.size() == 1) {
489 OS << MAI.getData8bitsDirective(AddrSpace);
490 OS << (unsigned)(unsigned char)Data[0];
495 // If the data ends with 0 and the target supports .asciz, use it, otherwise
497 if (MAI.getAscizDirective() && Data.back() == 0) {
498 OS << MAI.getAscizDirective();
499 Data = Data.substr(0, Data.size()-1);
501 OS << MAI.getAsciiDirective();
505 PrintQuotedString(Data, OS);
509 void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
510 unsigned AddrSpace) {
511 EmitValue(MCConstantExpr::Create(Value, getContext()), Size, AddrSpace);
514 void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
515 bool isPCRel, unsigned AddrSpace) {
516 assert(getCurrentSection() && "Cannot emit contents before setting section!");
517 assert(!isPCRel && "Cannot emit pc relative relocations!");
518 const char *Directive = 0;
521 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
522 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
523 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
525 Directive = MAI.getData64bitsDirective(AddrSpace);
526 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
527 if (Directive) break;
529 if (!Value->EvaluateAsAbsolute(IntValue))
530 report_fatal_error("Don't know how to emit this value.");
531 if (getContext().getTargetAsmInfo().isLittleEndian()) {
532 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
533 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
535 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
536 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
541 assert(Directive && "Invalid size for machine code value!");
542 OS << Directive << *Value;
546 void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace) {
548 if (Value->EvaluateAsAbsolute(IntValue)) {
549 EmitULEB128IntValue(IntValue, AddrSpace);
552 assert(MAI.hasLEB128() && "Cannot print a .uleb");
553 OS << ".uleb128 " << *Value;
557 void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace) {
559 if (Value->EvaluateAsAbsolute(IntValue)) {
560 EmitSLEB128IntValue(IntValue, AddrSpace);
563 assert(MAI.hasLEB128() && "Cannot print a .sleb");
564 OS << ".sleb128 " << *Value;
568 void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
569 assert(MAI.getGPRel32Directive() != 0);
570 OS << MAI.getGPRel32Directive() << *Value;
575 /// EmitFill - Emit NumBytes bytes worth of the value specified by
576 /// FillValue. This implements directives such as '.space'.
577 void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
578 unsigned AddrSpace) {
579 if (NumBytes == 0) return;
582 if (const char *ZeroDirective = MAI.getZeroDirective()) {
583 OS << ZeroDirective << NumBytes;
585 OS << ',' << (int)FillValue;
590 // Emit a byte at a time.
591 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
594 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
596 unsigned MaxBytesToEmit) {
597 // Some assemblers don't support non-power of two alignments, so we always
598 // emit alignments as a power of two if possible.
599 if (isPowerOf2_32(ByteAlignment)) {
601 default: llvm_unreachable("Invalid size for machine code value!");
602 case 1: OS << MAI.getAlignDirective(); break;
603 // FIXME: use MAI for this!
604 case 2: OS << ".p2alignw "; break;
605 case 4: OS << ".p2alignl "; break;
606 case 8: llvm_unreachable("Unsupported alignment size!");
609 if (MAI.getAlignmentIsInBytes())
612 OS << Log2_32(ByteAlignment);
614 if (Value || MaxBytesToEmit) {
616 OS.write_hex(truncateToSize(Value, ValueSize));
619 OS << ", " << MaxBytesToEmit;
625 // Non-power of two alignment. This is not widely supported by assemblers.
626 // FIXME: Parameterize this based on MAI.
628 default: llvm_unreachable("Invalid size for machine code value!");
629 case 1: OS << ".balign"; break;
630 case 2: OS << ".balignw"; break;
631 case 4: OS << ".balignl"; break;
632 case 8: llvm_unreachable("Unsupported alignment size!");
635 OS << ' ' << ByteAlignment;
636 OS << ", " << truncateToSize(Value, ValueSize);
638 OS << ", " << MaxBytesToEmit;
642 void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
643 unsigned MaxBytesToEmit) {
644 // Emit with a text fill value.
645 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
649 void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
650 unsigned char Value) {
651 // FIXME: Verify that Offset is associated with the current section.
652 OS << ".org " << *Offset << ", " << (unsigned) Value;
657 void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
658 assert(MAI.hasSingleParameterDotFile());
660 PrintQuotedString(Filename, OS);
664 bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
666 OS << "\t.file\t" << FileNo << ' ';
667 PrintQuotedString(Filename, OS);
670 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
673 void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
674 unsigned Column, unsigned Flags,
676 unsigned Discriminator) {
677 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
682 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
683 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
684 OS << " basic_block";
685 if (Flags & DWARF2_FLAG_PROLOGUE_END)
686 OS << " prologue_end";
687 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
688 OS << " epilogue_begin";
690 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
691 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
694 if (Flags & DWARF2_FLAG_IS_STMT)
703 OS << "discriminator " << Discriminator;
707 bool MCAsmStreamer::EmitCFIStartProc() {
708 if (this->MCStreamer::EmitCFIStartProc())
711 OS << "\t.cfi_startproc";
717 bool MCAsmStreamer::EmitCFIEndProc() {
718 if (this->MCStreamer::EmitCFIEndProc())
721 OS << "\t.cfi_endproc";
727 bool MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
728 if (this->MCStreamer::EmitCFIDefCfaOffset(Offset))
731 OS << "\t.cfi_def_cfa_offset " << Offset;
737 bool MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
738 if (this->MCStreamer::EmitCFIDefCfaRegister(Register))
741 OS << "\t.cfi_def_cfa_register " << Register;
747 bool MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
748 if (this->MCStreamer::EmitCFIOffset(Register, Offset))
751 OS << "\t.cfi_offset " << Register << ", " << Offset;
757 bool MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym,
759 if (this->MCStreamer::EmitCFIPersonality(Sym, Encoding))
762 OS << "\t.cfi_personality " << Encoding << ", " << *Sym;
768 bool MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
769 if (this->MCStreamer::EmitCFILsda(Sym, Encoding))
772 OS << "\t.cfi_lsda " << Encoding << ", " << *Sym;
778 void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
779 raw_ostream &OS = GetCommentOS();
780 SmallString<256> Code;
781 SmallVector<MCFixup, 4> Fixups;
782 raw_svector_ostream VecOS(Code);
783 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
786 // If we are showing fixups, create symbolic markers in the encoded
787 // representation. We do this by making a per-bit map to the fixup item index,
788 // then trying to display it as nicely as possible.
789 SmallVector<uint8_t, 64> FixupMap;
790 FixupMap.resize(Code.size() * 8);
791 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
794 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
795 MCFixup &F = Fixups[i];
796 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
797 for (unsigned j = 0; j != Info.TargetSize; ++j) {
798 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
799 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
800 FixupMap[Index] = 1 + i;
804 // FIXME: Node the fixup comments for Thumb2 are completely bogus since the
805 // high order halfword of a 32-bit Thumb2 instruction is emitted first.
807 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
811 // See if all bits are the same map entry.
812 uint8_t MapEntry = FixupMap[i * 8 + 0];
813 for (unsigned j = 1; j != 8; ++j) {
814 if (FixupMap[i * 8 + j] == MapEntry)
817 MapEntry = uint8_t(~0U);
821 if (MapEntry != uint8_t(~0U)) {
823 OS << format("0x%02x", uint8_t(Code[i]));
826 // FIXME: Some of the 8 bits require fix up.
827 OS << format("0x%02x", uint8_t(Code[i])) << '\''
828 << char('A' + MapEntry - 1) << '\'';
830 OS << char('A' + MapEntry - 1);
833 // Otherwise, write out in binary.
835 for (unsigned j = 8; j--;) {
836 unsigned Bit = (Code[i] >> j) & 1;
839 if (getContext().getTargetAsmInfo().isLittleEndian())
840 FixupBit = i * 8 + j;
842 FixupBit = i * 8 + (7-j);
844 if (uint8_t MapEntry = FixupMap[FixupBit]) {
845 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
846 OS << char('A' + MapEntry - 1);
854 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
855 MCFixup &F = Fixups[i];
856 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
857 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
858 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
862 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
863 assert(getCurrentSection() && "Cannot emit contents before setting section!");
866 MCLineEntry::Make(this, getCurrentSection());
868 // Show the encoding in a comment if we have a code emitter.
870 AddEncodingComment(Inst);
872 // Show the MCInst if enabled.
874 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
875 GetCommentOS() << "\n";
878 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
880 InstPrinter->printInst(&Inst, OS);
882 Inst.print(OS, &MAI);
886 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
887 /// the specified string in the output .s file. This capability is
888 /// indicated by the hasRawTextSupport() predicate.
889 void MCAsmStreamer::EmitRawText(StringRef String) {
890 if (!String.empty() && String.back() == '\n')
891 String = String.substr(0, String.size()-1);
896 void MCAsmStreamer::Finish() {
897 // Dump out the dwarf file & directory tables and line tables.
898 if (getContext().hasDwarfFiles() && !UseLoc)
899 MCDwarfFileTable::Emit(this);
902 MCStreamer *llvm::createAsmStreamer(MCContext &Context,
903 formatted_raw_ostream &OS,
904 bool isVerboseAsm, bool useLoc,
905 MCInstPrinter *IP, MCCodeEmitter *CE,
906 TargetAsmBackend *TAB, bool ShowInst) {
907 return new MCAsmStreamer(Context, OS, isVerboseAsm, useLoc,
908 IP, CE, TAB, ShowInst);