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 EmitFnStart();
196 virtual void EmitFnEnd();
197 virtual void EmitCantUnwind();
198 virtual void EmitPersonality(const MCSymbol *Personality);
199 virtual void EmitHandlerData();
200 virtual void EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
201 virtual void EmitPad(int64_t Offset);
202 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList, bool);
205 virtual void EmitInstruction(const MCInst &Inst);
207 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
208 /// the specified string in the output .s file. This capability is
209 /// indicated by the hasRawTextSupport() predicate.
210 virtual void EmitRawText(StringRef String);
212 virtual void Finish();
217 } // end anonymous namespace.
219 /// AddComment - Add a comment that can be emitted to the generated .s
220 /// file if applicable as a QoI issue to make the output of the compiler
221 /// more readable. This only affects the MCAsmStreamer, and only when
222 /// verbose assembly output is enabled.
223 void MCAsmStreamer::AddComment(const Twine &T) {
224 if (!IsVerboseAsm) return;
226 // Make sure that CommentStream is flushed.
227 CommentStream.flush();
229 T.toVector(CommentToEmit);
230 // Each comment goes on its own line.
231 CommentToEmit.push_back('\n');
233 // Tell the comment stream that the vector changed underneath it.
234 CommentStream.resync();
237 void MCAsmStreamer::EmitCommentsAndEOL() {
238 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
243 CommentStream.flush();
244 StringRef Comments = CommentToEmit.str();
246 assert(Comments.back() == '\n' &&
247 "Comment array not newline terminated");
249 // Emit a line of comments.
250 OS.PadToColumn(MAI.getCommentColumn());
251 size_t Position = Comments.find('\n');
252 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
254 Comments = Comments.substr(Position+1);
255 } while (!Comments.empty());
257 CommentToEmit.clear();
258 // Tell the comment stream that the vector changed underneath it.
259 CommentStream.resync();
262 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
263 assert(Bytes && "Invalid size!");
264 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
267 void MCAsmStreamer::ChangeSection(const MCSection *Section) {
268 assert(Section && "Cannot switch to a null section!");
269 Section->PrintSwitchToSection(MAI, OS);
272 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
273 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
274 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
275 assert(getCurrentSection() && "Cannot emit before setting section!");
277 OS << *Symbol << MAI.getLabelSuffix();
279 Symbol->setSection(*getCurrentSection());
282 void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
284 default: assert(0 && "Invalid flag!");
285 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
286 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
287 case MCAF_Code16: OS << "\t.code\t16"; break;
288 case MCAF_Code32: OS << "\t.code\t32"; break;
293 void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
294 // This needs to emit to a temporary string to get properly quoted
295 // MCSymbols when they have spaces in them.
296 OS << "\t.thumb_func";
302 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
303 OS << *Symbol << " = " << *Value;
306 // FIXME: Lift context changes into super class.
307 Symbol->setVariableValue(Value);
310 void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
311 OS << ".weakref " << *Alias << ", " << *Symbol;
315 void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
316 const MCSymbol *LastLabel,
317 const MCSymbol *Label) {
318 EmitDwarfSetLineAddr(LineDelta, Label,
319 getContext().getTargetAsmInfo().getPointerSize());
322 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
323 MCSymbolAttr Attribute) {
325 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
326 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
327 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
328 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
329 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
330 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
331 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
332 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
333 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
334 OS << "\t.type\t" << *Symbol << ','
335 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
337 default: assert(0 && "Unknown ELF .type");
338 case MCSA_ELF_TypeFunction: OS << "function"; break;
339 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
340 case MCSA_ELF_TypeObject: OS << "object"; break;
341 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
342 case MCSA_ELF_TypeCommon: OS << "common"; break;
343 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
344 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
348 case MCSA_Global: // .globl/.global
349 OS << MAI.getGlobalDirective();
351 case MCSA_Hidden: OS << "\t.hidden\t"; break;
352 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
353 case MCSA_Internal: OS << "\t.internal\t"; break;
354 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
355 case MCSA_Local: OS << "\t.local\t"; break;
356 case MCSA_NoDeadStrip: OS << "\t.no_dead_strip\t"; break;
357 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
358 case MCSA_PrivateExtern: OS << "\t.private_extern\t"; break;
359 case MCSA_Protected: OS << "\t.protected\t"; break;
360 case MCSA_Reference: OS << "\t.reference\t"; break;
361 case MCSA_Weak: OS << "\t.weak\t"; break;
362 case MCSA_WeakDefinition: OS << "\t.weak_definition\t"; break;
364 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
365 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
372 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
373 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
377 void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
378 OS << "\t.def\t " << *Symbol << ';';
382 void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
383 OS << "\t.scl\t" << StorageClass << ';';
387 void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
388 OS << "\t.type\t" << Type << ';';
392 void MCAsmStreamer::EndCOFFSymbolDef() {
397 void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
398 assert(MAI.hasDotTypeDotSizeDirective());
399 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
402 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
403 unsigned ByteAlignment) {
404 OS << "\t.comm\t" << *Symbol << ',' << Size;
405 if (ByteAlignment != 0) {
406 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
407 OS << ',' << ByteAlignment;
409 OS << ',' << Log2_32(ByteAlignment);
414 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
416 /// @param Symbol - The common symbol to emit.
417 /// @param Size - The size of the common symbol.
418 void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
419 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
420 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
424 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
425 unsigned Size, unsigned ByteAlignment) {
426 // Note: a .zerofill directive does not switch sections.
429 // This is a mach-o specific directive.
430 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
431 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
433 if (Symbol != NULL) {
434 OS << ',' << *Symbol << ',' << Size;
435 if (ByteAlignment != 0)
436 OS << ',' << Log2_32(ByteAlignment);
441 // .tbss sym, size, align
442 // This depends that the symbol has already been mangled from the original,
444 void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
445 uint64_t Size, unsigned ByteAlignment) {
446 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
447 // Instead of using the Section we'll just use the shortcut.
448 // This is a mach-o specific directive and section.
449 OS << ".tbss " << *Symbol << ", " << Size;
451 // Output align if we have it. We default to 1 so don't bother printing
453 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
458 static inline char toOctal(int X) { return (X&7)+'0'; }
460 static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
463 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
464 unsigned char C = Data[i];
465 if (C == '"' || C == '\\') {
466 OS << '\\' << (char)C;
470 if (isprint((unsigned char)C)) {
476 case '\b': OS << "\\b"; break;
477 case '\f': OS << "\\f"; break;
478 case '\n': OS << "\\n"; break;
479 case '\r': OS << "\\r"; break;
480 case '\t': OS << "\\t"; break;
483 OS << toOctal(C >> 6);
484 OS << toOctal(C >> 3);
485 OS << toOctal(C >> 0);
494 void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
495 assert(getCurrentSection() && "Cannot emit contents before setting section!");
496 if (Data.empty()) return;
498 if (Data.size() == 1) {
499 OS << MAI.getData8bitsDirective(AddrSpace);
500 OS << (unsigned)(unsigned char)Data[0];
505 // If the data ends with 0 and the target supports .asciz, use it, otherwise
507 if (MAI.getAscizDirective() && Data.back() == 0) {
508 OS << MAI.getAscizDirective();
509 Data = Data.substr(0, Data.size()-1);
511 OS << MAI.getAsciiDirective();
515 PrintQuotedString(Data, OS);
519 void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
520 unsigned AddrSpace) {
521 EmitValue(MCConstantExpr::Create(Value, getContext()), Size, AddrSpace);
524 void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
525 bool isPCRel, unsigned AddrSpace) {
526 assert(getCurrentSection() && "Cannot emit contents before setting section!");
527 assert(!isPCRel && "Cannot emit pc relative relocations!");
528 const char *Directive = 0;
531 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
532 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
533 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
535 Directive = MAI.getData64bitsDirective(AddrSpace);
536 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
537 if (Directive) break;
539 if (!Value->EvaluateAsAbsolute(IntValue))
540 report_fatal_error("Don't know how to emit this value.");
541 if (getContext().getTargetAsmInfo().isLittleEndian()) {
542 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
543 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
545 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
546 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
551 assert(Directive && "Invalid size for machine code value!");
552 OS << Directive << *Value;
556 void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace) {
558 if (Value->EvaluateAsAbsolute(IntValue)) {
559 EmitULEB128IntValue(IntValue, AddrSpace);
562 assert(MAI.hasLEB128() && "Cannot print a .uleb");
563 OS << ".uleb128 " << *Value;
567 void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace) {
569 if (Value->EvaluateAsAbsolute(IntValue)) {
570 EmitSLEB128IntValue(IntValue, AddrSpace);
573 assert(MAI.hasLEB128() && "Cannot print a .sleb");
574 OS << ".sleb128 " << *Value;
578 void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
579 assert(MAI.getGPRel32Directive() != 0);
580 OS << MAI.getGPRel32Directive() << *Value;
585 /// EmitFill - Emit NumBytes bytes worth of the value specified by
586 /// FillValue. This implements directives such as '.space'.
587 void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
588 unsigned AddrSpace) {
589 if (NumBytes == 0) return;
592 if (const char *ZeroDirective = MAI.getZeroDirective()) {
593 OS << ZeroDirective << NumBytes;
595 OS << ',' << (int)FillValue;
600 // Emit a byte at a time.
601 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
604 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
606 unsigned MaxBytesToEmit) {
607 // Some assemblers don't support non-power of two alignments, so we always
608 // emit alignments as a power of two if possible.
609 if (isPowerOf2_32(ByteAlignment)) {
611 default: llvm_unreachable("Invalid size for machine code value!");
612 case 1: OS << MAI.getAlignDirective(); break;
613 // FIXME: use MAI for this!
614 case 2: OS << ".p2alignw "; break;
615 case 4: OS << ".p2alignl "; break;
616 case 8: llvm_unreachable("Unsupported alignment size!");
619 if (MAI.getAlignmentIsInBytes())
622 OS << Log2_32(ByteAlignment);
624 if (Value || MaxBytesToEmit) {
626 OS.write_hex(truncateToSize(Value, ValueSize));
629 OS << ", " << MaxBytesToEmit;
635 // Non-power of two alignment. This is not widely supported by assemblers.
636 // FIXME: Parameterize this based on MAI.
638 default: llvm_unreachable("Invalid size for machine code value!");
639 case 1: OS << ".balign"; break;
640 case 2: OS << ".balignw"; break;
641 case 4: OS << ".balignl"; break;
642 case 8: llvm_unreachable("Unsupported alignment size!");
645 OS << ' ' << ByteAlignment;
646 OS << ", " << truncateToSize(Value, ValueSize);
648 OS << ", " << MaxBytesToEmit;
652 void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
653 unsigned MaxBytesToEmit) {
654 // Emit with a text fill value.
655 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
659 void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
660 unsigned char Value) {
661 // FIXME: Verify that Offset is associated with the current section.
662 OS << ".org " << *Offset << ", " << (unsigned) Value;
667 void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
668 assert(MAI.hasSingleParameterDotFile());
670 PrintQuotedString(Filename, OS);
674 bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
676 OS << "\t.file\t" << FileNo << ' ';
677 PrintQuotedString(Filename, OS);
680 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
683 void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
684 unsigned Column, unsigned Flags,
686 unsigned Discriminator) {
687 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
692 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
693 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
694 OS << " basic_block";
695 if (Flags & DWARF2_FLAG_PROLOGUE_END)
696 OS << " prologue_end";
697 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
698 OS << " epilogue_begin";
700 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
701 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
704 if (Flags & DWARF2_FLAG_IS_STMT)
713 OS << "discriminator " << Discriminator;
717 bool MCAsmStreamer::EmitCFIStartProc() {
718 if (this->MCStreamer::EmitCFIStartProc())
721 OS << "\t.cfi_startproc";
727 bool MCAsmStreamer::EmitCFIEndProc() {
728 if (this->MCStreamer::EmitCFIEndProc())
731 OS << "\t.cfi_endproc";
737 bool MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
738 if (this->MCStreamer::EmitCFIDefCfaOffset(Offset))
741 OS << "\t.cfi_def_cfa_offset " << Offset;
747 bool MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
748 if (this->MCStreamer::EmitCFIDefCfaRegister(Register))
751 OS << "\t.cfi_def_cfa_register " << Register;
757 bool MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
758 if (this->MCStreamer::EmitCFIOffset(Register, Offset))
761 OS << "\t.cfi_offset " << Register << ", " << Offset;
767 bool MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym,
769 if (this->MCStreamer::EmitCFIPersonality(Sym, Encoding))
772 OS << "\t.cfi_personality " << Encoding << ", " << *Sym;
778 bool MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
779 if (this->MCStreamer::EmitCFILsda(Sym, Encoding))
782 OS << "\t.cfi_lsda " << Encoding << ", " << *Sym;
788 void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
789 raw_ostream &OS = GetCommentOS();
790 SmallString<256> Code;
791 SmallVector<MCFixup, 4> Fixups;
792 raw_svector_ostream VecOS(Code);
793 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
796 // If we are showing fixups, create symbolic markers in the encoded
797 // representation. We do this by making a per-bit map to the fixup item index,
798 // then trying to display it as nicely as possible.
799 SmallVector<uint8_t, 64> FixupMap;
800 FixupMap.resize(Code.size() * 8);
801 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
804 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
805 MCFixup &F = Fixups[i];
806 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
807 for (unsigned j = 0; j != Info.TargetSize; ++j) {
808 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
809 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
810 FixupMap[Index] = 1 + i;
814 // FIXME: Node the fixup comments for Thumb2 are completely bogus since the
815 // high order halfword of a 32-bit Thumb2 instruction is emitted first.
817 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
821 // See if all bits are the same map entry.
822 uint8_t MapEntry = FixupMap[i * 8 + 0];
823 for (unsigned j = 1; j != 8; ++j) {
824 if (FixupMap[i * 8 + j] == MapEntry)
827 MapEntry = uint8_t(~0U);
831 if (MapEntry != uint8_t(~0U)) {
833 OS << format("0x%02x", uint8_t(Code[i]));
836 // FIXME: Some of the 8 bits require fix up.
837 OS << format("0x%02x", uint8_t(Code[i])) << '\''
838 << char('A' + MapEntry - 1) << '\'';
840 OS << char('A' + MapEntry - 1);
843 // Otherwise, write out in binary.
845 for (unsigned j = 8; j--;) {
846 unsigned Bit = (Code[i] >> j) & 1;
849 if (getContext().getTargetAsmInfo().isLittleEndian())
850 FixupBit = i * 8 + j;
852 FixupBit = i * 8 + (7-j);
854 if (uint8_t MapEntry = FixupMap[FixupBit]) {
855 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
856 OS << char('A' + MapEntry - 1);
864 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
865 MCFixup &F = Fixups[i];
866 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
867 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
868 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
872 void MCAsmStreamer::EmitFnStart() {
877 void MCAsmStreamer::EmitFnEnd() {
882 void MCAsmStreamer::EmitCantUnwind() {
883 OS << "\t.cantunwind";
887 void MCAsmStreamer::EmitHandlerData() {
888 OS << "\t.handlerdata";
892 void MCAsmStreamer::EmitPersonality(const MCSymbol *Personality) {
893 OS << "\t.personality " << Personality->getName();
897 void MCAsmStreamer::EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset) {
898 OS << "\t.setfp\t" << InstPrinter->getRegName(FpReg)
899 << ", " << InstPrinter->getRegName(SpReg);
901 OS << ", #" << Offset;
905 void MCAsmStreamer::EmitPad(int64_t Offset) {
906 OS << "\t.pad\t#" << Offset;
910 void MCAsmStreamer::EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
912 assert(RegList.size() && "RegList should not be empty");
918 OS << InstPrinter->getRegName(RegList[0]);
920 for (unsigned i = 1, e = RegList.size(); i != e; ++i)
921 OS << ", " << InstPrinter->getRegName(RegList[i]);
927 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
928 assert(getCurrentSection() && "Cannot emit contents before setting section!");
931 MCLineEntry::Make(this, getCurrentSection());
933 // Show the encoding in a comment if we have a code emitter.
935 AddEncodingComment(Inst);
937 // Show the MCInst if enabled.
939 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
940 GetCommentOS() << "\n";
943 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
945 InstPrinter->printInst(&Inst, OS);
947 Inst.print(OS, &MAI);
951 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
952 /// the specified string in the output .s file. This capability is
953 /// indicated by the hasRawTextSupport() predicate.
954 void MCAsmStreamer::EmitRawText(StringRef String) {
955 if (!String.empty() && String.back() == '\n')
956 String = String.substr(0, String.size()-1);
961 void MCAsmStreamer::Finish() {
962 // Dump out the dwarf file & directory tables and line tables.
963 if (getContext().hasDwarfFiles() && !UseLoc)
964 MCDwarfFileTable::Emit(this);
967 MCStreamer *llvm::createAsmStreamer(MCContext &Context,
968 formatted_raw_ostream &OS,
969 bool isVerboseAsm, bool useLoc,
970 MCInstPrinter *IP, MCCodeEmitter *CE,
971 TargetAsmBackend *TAB, bool ShowInst) {
972 return new MCAsmStreamer(Context, OS, isVerboseAsm, useLoc,
973 IP, CE, TAB, ShowInst);