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"
30 class MCAsmStreamer : public MCStreamer {
31 formatted_raw_ostream &OS;
33 OwningPtr<MCInstPrinter> InstPrinter;
34 MCCodeEmitter *Emitter;
36 SmallString<128> CommentToEmit;
37 raw_svector_ostream CommentStream;
39 unsigned IsLittleEndian : 1;
40 unsigned IsVerboseAsm : 1;
41 unsigned ShowInst : 1;
44 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
45 bool isLittleEndian, bool isVerboseAsm, MCInstPrinter *printer,
46 MCCodeEmitter *emitter, bool showInst)
47 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
48 InstPrinter(printer), Emitter(emitter), CommentStream(CommentToEmit),
49 IsLittleEndian(isLittleEndian), IsVerboseAsm(isVerboseAsm),
51 if (InstPrinter && IsVerboseAsm)
52 InstPrinter->setCommentStream(CommentStream);
56 bool isLittleEndian() const { return IsLittleEndian; }
58 inline void EmitEOL() {
59 // If we don't have any comments, just emit a \n.
66 void EmitCommentsAndEOL();
68 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
70 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
72 /// hasRawTextSupport - We support EmitRawText.
73 virtual bool hasRawTextSupport() const { return true; }
75 /// AddComment - Add a comment that can be emitted to the generated .s
76 /// file if applicable as a QoI issue to make the output of the compiler
77 /// more readable. This only affects the MCAsmStreamer, and only when
78 /// verbose assembly output is enabled.
79 virtual void AddComment(const Twine &T);
81 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
82 virtual void AddEncodingComment(const MCInst &Inst);
84 /// GetCommentOS - Return a raw_ostream that comments can be written to.
85 /// Unlike AddComment, you are required to terminate comments with \n if you
87 virtual raw_ostream &GetCommentOS() {
89 return nulls(); // Discard comments unless in verbose asm mode.
93 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
94 virtual void AddBlankLine() {
98 /// @name MCStreamer Interface
101 virtual void SwitchSection(const MCSection *Section);
103 virtual void EmitLabel(MCSymbol *Symbol);
105 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
107 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
109 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
111 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
112 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
113 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
114 virtual void EmitCOFFSymbolType(int Type);
115 virtual void EndCOFFSymbolDef();
116 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
117 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
118 unsigned ByteAlignment);
120 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
122 /// @param Symbol - The common symbol to emit.
123 /// @param Size - The size of the common symbol.
124 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
126 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
127 unsigned Size = 0, unsigned ByteAlignment = 0);
129 virtual void EmitTBSSSymbol (MCSymbol *Symbol, uint64_t Size,
130 unsigned ByteAlignment = 0);
132 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
134 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
135 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
136 virtual void EmitGPRel32Value(const MCExpr *Value);
139 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
142 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
143 unsigned ValueSize = 1,
144 unsigned MaxBytesToEmit = 0);
146 virtual void EmitCodeAlignment(unsigned ByteAlignment,
147 unsigned MaxBytesToEmit = 0);
149 virtual void EmitValueToOffset(const MCExpr *Offset,
150 unsigned char Value = 0);
152 virtual void EmitFileDirective(StringRef Filename);
153 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
155 virtual void EmitInstruction(const MCInst &Inst);
157 /// EmitRawText - If this file is backed by a assembly streamer, this dumps
158 /// the specified string in the output .s file. This capability is
159 /// indicated by the hasRawTextSupport() predicate.
160 virtual void EmitRawText(StringRef String);
162 virtual void Finish();
167 } // end anonymous namespace.
169 /// AddComment - Add a comment that can be emitted to the generated .s
170 /// file if applicable as a QoI issue to make the output of the compiler
171 /// more readable. This only affects the MCAsmStreamer, and only when
172 /// verbose assembly output is enabled.
173 void MCAsmStreamer::AddComment(const Twine &T) {
174 if (!IsVerboseAsm) return;
176 // Make sure that CommentStream is flushed.
177 CommentStream.flush();
179 T.toVector(CommentToEmit);
180 // Each comment goes on its own line.
181 CommentToEmit.push_back('\n');
183 // Tell the comment stream that the vector changed underneath it.
184 CommentStream.resync();
187 void MCAsmStreamer::EmitCommentsAndEOL() {
188 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
193 CommentStream.flush();
194 StringRef Comments = CommentToEmit.str();
196 assert(Comments.back() == '\n' &&
197 "Comment array not newline terminated");
199 // Emit a line of comments.
200 OS.PadToColumn(MAI.getCommentColumn());
201 size_t Position = Comments.find('\n');
202 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
204 Comments = Comments.substr(Position+1);
205 } while (!Comments.empty());
207 CommentToEmit.clear();
208 // Tell the comment stream that the vector changed underneath it.
209 CommentStream.resync();
212 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
213 assert(Bytes && "Invalid size!");
214 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
217 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
218 assert(Section && "Cannot switch to a null section!");
219 if (Section != CurSection) {
220 CurSection = Section;
221 Section->PrintSwitchToSection(MAI, OS);
225 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
226 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
227 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
228 assert(CurSection && "Cannot emit before setting section!");
230 OS << *Symbol << ":";
232 Symbol->setSection(*CurSection);
235 void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
237 default: assert(0 && "Invalid flag!");
238 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
243 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
244 OS << *Symbol << " = " << *Value;
247 // FIXME: Lift context changes into super class.
248 Symbol->setVariableValue(Value);
251 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
252 MCSymbolAttr Attribute) {
254 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
255 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
256 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
257 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
258 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
259 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
260 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
261 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
262 OS << "\t.type\t" << *Symbol << ','
263 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
265 default: assert(0 && "Unknown ELF .type");
266 case MCSA_ELF_TypeFunction: OS << "function"; break;
267 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
268 case MCSA_ELF_TypeObject: OS << "object"; break;
269 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
270 case MCSA_ELF_TypeCommon: OS << "common"; break;
271 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
275 case MCSA_Global: // .globl/.global
276 OS << MAI.getGlobalDirective();
278 case MCSA_Hidden: OS << ".hidden "; break;
279 case MCSA_IndirectSymbol: OS << ".indirect_symbol "; break;
280 case MCSA_Internal: OS << ".internal "; break;
281 case MCSA_LazyReference: OS << ".lazy_reference "; break;
282 case MCSA_Local: OS << ".local "; break;
283 case MCSA_NoDeadStrip: OS << ".no_dead_strip "; break;
284 case MCSA_PrivateExtern: OS << ".private_extern "; break;
285 case MCSA_Protected: OS << ".protected "; break;
286 case MCSA_Reference: OS << ".reference "; break;
287 case MCSA_Weak: OS << ".weak "; break;
288 case MCSA_WeakDefinition: OS << ".weak_definition "; break;
290 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
297 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
298 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
302 void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
303 OS << "\t.def\t " << *Symbol << ';';
307 void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
308 OS << "\t.scl\t" << StorageClass << ';';
312 void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
313 OS << "\t.type\t" << Type << ';';
317 void MCAsmStreamer::EndCOFFSymbolDef() {
322 void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
323 assert(MAI.hasDotTypeDotSizeDirective());
324 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
327 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
328 unsigned ByteAlignment) {
329 OS << "\t.comm\t" << *Symbol << ',' << Size;
330 if (ByteAlignment != 0) {
331 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
332 OS << ',' << ByteAlignment;
334 OS << ',' << Log2_32(ByteAlignment);
339 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
341 /// @param Symbol - The common symbol to emit.
342 /// @param Size - The size of the common symbol.
343 void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
344 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
345 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
349 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
350 unsigned Size, unsigned ByteAlignment) {
351 // Note: a .zerofill directive does not switch sections.
354 // This is a mach-o specific directive.
355 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
356 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
358 if (Symbol != NULL) {
359 OS << ',' << *Symbol << ',' << Size;
360 if (ByteAlignment != 0)
361 OS << ',' << Log2_32(ByteAlignment);
366 // .tbss sym$tlv$init, size, align
367 void MCAsmStreamer::EmitTBSSSymbol(MCSymbol *Symbol, uint64_t Size,
368 unsigned ByteAlignment) {
369 assert(Symbol != NULL && "Symbol shouldn't be NULL!");
372 // This is a mach-o specific directive and the name requires some mangling.
373 OS << *Symbol << "$tlv$init, " << Size;
375 // Output align if we have it.
376 if (ByteAlignment != 0) OS << ", " << Log2_32(ByteAlignment);
381 static inline char toOctal(int X) { return (X&7)+'0'; }
383 static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
386 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
387 unsigned char C = Data[i];
388 if (C == '"' || C == '\\') {
389 OS << '\\' << (char)C;
393 if (isprint((unsigned char)C)) {
399 case '\b': OS << "\\b"; break;
400 case '\f': OS << "\\f"; break;
401 case '\n': OS << "\\n"; break;
402 case '\r': OS << "\\r"; break;
403 case '\t': OS << "\\t"; break;
406 OS << toOctal(C >> 6);
407 OS << toOctal(C >> 3);
408 OS << toOctal(C >> 0);
417 void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
418 assert(CurSection && "Cannot emit contents before setting section!");
419 if (Data.empty()) return;
421 if (Data.size() == 1) {
422 OS << MAI.getData8bitsDirective(AddrSpace);
423 OS << (unsigned)(unsigned char)Data[0];
428 // If the data ends with 0 and the target supports .asciz, use it, otherwise
430 if (MAI.getAscizDirective() && Data.back() == 0) {
431 OS << MAI.getAscizDirective();
432 Data = Data.substr(0, Data.size()-1);
434 OS << MAI.getAsciiDirective();
438 PrintQuotedString(Data, OS);
442 /// EmitIntValue - Special case of EmitValue that avoids the client having
443 /// to pass in a MCExpr for constant integers.
444 void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
445 unsigned AddrSpace) {
446 assert(CurSection && "Cannot emit contents before setting section!");
447 const char *Directive = 0;
450 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
451 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
452 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
454 Directive = MAI.getData64bitsDirective(AddrSpace);
455 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
456 if (Directive) break;
457 if (isLittleEndian()) {
458 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
459 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
461 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
462 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
467 assert(Directive && "Invalid size for machine code value!");
468 OS << Directive << truncateToSize(Value, Size);
472 void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
473 unsigned AddrSpace) {
474 assert(CurSection && "Cannot emit contents before setting section!");
475 const char *Directive = 0;
478 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
479 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
480 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
481 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
484 assert(Directive && "Invalid size for machine code value!");
485 OS << Directive << *Value;
489 void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
490 assert(MAI.getGPRel32Directive() != 0);
491 OS << MAI.getGPRel32Directive() << *Value;
496 /// EmitFill - Emit NumBytes bytes worth of the value specified by
497 /// FillValue. This implements directives such as '.space'.
498 void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
499 unsigned AddrSpace) {
500 if (NumBytes == 0) return;
503 if (const char *ZeroDirective = MAI.getZeroDirective()) {
504 OS << ZeroDirective << NumBytes;
506 OS << ',' << (int)FillValue;
511 // Emit a byte at a time.
512 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
515 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
517 unsigned MaxBytesToEmit) {
518 // Some assemblers don't support non-power of two alignments, so we always
519 // emit alignments as a power of two if possible.
520 if (isPowerOf2_32(ByteAlignment)) {
522 default: llvm_unreachable("Invalid size for machine code value!");
523 case 1: OS << MAI.getAlignDirective(); break;
524 // FIXME: use MAI for this!
525 case 2: OS << ".p2alignw "; break;
526 case 4: OS << ".p2alignl "; break;
527 case 8: llvm_unreachable("Unsupported alignment size!");
530 if (MAI.getAlignmentIsInBytes())
533 OS << Log2_32(ByteAlignment);
535 if (Value || MaxBytesToEmit) {
537 OS.write_hex(truncateToSize(Value, ValueSize));
540 OS << ", " << MaxBytesToEmit;
546 // Non-power of two alignment. This is not widely supported by assemblers.
547 // FIXME: Parameterize this based on MAI.
549 default: llvm_unreachable("Invalid size for machine code value!");
550 case 1: OS << ".balign"; break;
551 case 2: OS << ".balignw"; break;
552 case 4: OS << ".balignl"; break;
553 case 8: llvm_unreachable("Unsupported alignment size!");
556 OS << ' ' << ByteAlignment;
557 OS << ", " << truncateToSize(Value, ValueSize);
559 OS << ", " << MaxBytesToEmit;
563 void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
564 unsigned MaxBytesToEmit) {
565 // Emit with a text fill value.
566 EmitValueToAlignment(ByteAlignment, MAI.getTextAlignFillValue(),
570 void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
571 unsigned char Value) {
572 // FIXME: Verify that Offset is associated with the current section.
573 OS << ".org " << *Offset << ", " << (unsigned) Value;
578 void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
579 assert(MAI.hasSingleParameterDotFile());
581 PrintQuotedString(Filename, OS);
585 void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
586 OS << "\t.file\t" << FileNo << ' ';
587 PrintQuotedString(Filename, OS);
591 void MCAsmStreamer::AddEncodingComment(const MCInst &Inst) {
592 raw_ostream &OS = GetCommentOS();
593 SmallString<256> Code;
594 SmallVector<MCFixup, 4> Fixups;
595 raw_svector_ostream VecOS(Code);
596 Emitter->EncodeInstruction(Inst, VecOS, Fixups);
599 // If we are showing fixups, create symbolic markers in the encoded
600 // representation. We do this by making a per-bit map to the fixup item index,
601 // then trying to display it as nicely as possible.
602 SmallVector<uint8_t, 64> FixupMap;
603 FixupMap.resize(Code.size() * 8);
604 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
607 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
608 MCFixup &F = Fixups[i];
609 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
610 for (unsigned j = 0; j != Info.TargetSize; ++j) {
611 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
612 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
613 FixupMap[Index] = 1 + i;
618 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
622 // See if all bits are the same map entry.
623 uint8_t MapEntry = FixupMap[i * 8 + 0];
624 for (unsigned j = 1; j != 8; ++j) {
625 if (FixupMap[i * 8 + j] == MapEntry)
628 MapEntry = uint8_t(~0U);
632 if (MapEntry != uint8_t(~0U)) {
634 OS << format("0x%02x", uint8_t(Code[i]));
636 assert(Code[i] == 0 && "Encoder wrote into fixed up bit!");
637 OS << char('A' + MapEntry - 1);
640 // Otherwise, write out in binary.
642 for (unsigned j = 8; j--;) {
643 unsigned Bit = (Code[i] >> j) & 1;
644 if (uint8_t MapEntry = FixupMap[i * 8 + j]) {
645 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
646 OS << char('A' + MapEntry - 1);
654 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
655 MCFixup &F = Fixups[i];
656 const MCFixupKindInfo &Info = Emitter->getFixupKindInfo(F.getKind());
657 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
658 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
662 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
663 assert(CurSection && "Cannot emit contents before setting section!");
665 // Show the encoding in a comment if we have a code emitter.
667 AddEncodingComment(Inst);
669 // Show the MCInst if enabled.
671 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
673 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
675 InstPrinter->printInst(&Inst, OS);
677 Inst.print(OS, &MAI);
681 /// EmitRawText - If this file is backed by a assembly streamer, this dumps
682 /// the specified string in the output .s file. This capability is
683 /// indicated by the hasRawTextSupport() predicate.
684 void MCAsmStreamer::EmitRawText(StringRef String) {
685 if (!String.empty() && String.back() == '\n')
686 String = String.substr(0, String.size()-1);
691 void MCAsmStreamer::Finish() {
695 MCStreamer *llvm::createAsmStreamer(MCContext &Context,
696 formatted_raw_ostream &OS,
698 bool isVerboseAsm, MCInstPrinter *IP,
699 MCCodeEmitter *CE, bool ShowInst) {
700 return new MCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,