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/SmallString.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Support/FormattedStream.h"
29 class MCAsmStreamer : public MCStreamer {
30 formatted_raw_ostream &OS;
32 MCInstPrinter *InstPrinter;
33 MCCodeEmitter *Emitter;
35 SmallString<128> CommentToEmit;
36 raw_svector_ostream CommentStream;
38 unsigned IsLittleEndian : 1;
39 unsigned IsVerboseAsm : 1;
40 unsigned ShowInst : 1;
43 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(mai), InstPrinter(printer),
48 Emitter(emitter), CommentStream(CommentToEmit),
49 IsLittleEndian(isLittleEndian), IsVerboseAsm(isVerboseAsm),
53 bool isLittleEndian() const { return IsLittleEndian; }
55 inline void EmitEOL() {
56 // If we don't have any comments, just emit a \n.
63 void EmitCommentsAndEOL();
65 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
67 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
69 /// AddComment - Add a comment that can be emitted to the generated .s
70 /// file if applicable as a QoI issue to make the output of the compiler
71 /// more readable. This only affects the MCAsmStreamer, and only when
72 /// verbose assembly output is enabled.
73 virtual void AddComment(const Twine &T);
75 /// GetCommentOS - Return a raw_ostream that comments can be written to.
76 /// Unlike AddComment, you are required to terminate comments with \n if you
78 virtual raw_ostream &GetCommentOS() {
80 return nulls(); // Discard comments unless in verbose asm mode.
84 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
85 virtual void AddBlankLine() {
89 /// @name MCStreamer Interface
92 virtual void SwitchSection(const MCSection *Section);
94 virtual void EmitLabel(MCSymbol *Symbol);
96 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
98 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
100 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
102 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
104 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
105 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
106 unsigned ByteAlignment);
108 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
110 /// @param Symbol - The common symbol to emit.
111 /// @param Size - The size of the common symbol.
112 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
114 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
115 unsigned Size = 0, unsigned ByteAlignment = 0);
117 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
119 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
120 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
121 virtual void EmitGPRel32Value(const MCExpr *Value);
124 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
127 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
128 unsigned ValueSize = 1,
129 unsigned MaxBytesToEmit = 0);
131 virtual void EmitValueToOffset(const MCExpr *Offset,
132 unsigned char Value = 0);
134 virtual void EmitFileDirective(StringRef Filename);
135 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
137 virtual void EmitInstruction(const MCInst &Inst);
139 virtual void Finish();
144 } // end anonymous namespace.
146 /// AddComment - Add a comment that can be emitted to the generated .s
147 /// file if applicable as a QoI issue to make the output of the compiler
148 /// more readable. This only affects the MCAsmStreamer, and only when
149 /// verbose assembly output is enabled.
150 void MCAsmStreamer::AddComment(const Twine &T) {
151 if (!IsVerboseAsm) return;
153 // Make sure that CommentStream is flushed.
154 CommentStream.flush();
156 T.toVector(CommentToEmit);
157 // Each comment goes on its own line.
158 CommentToEmit.push_back('\n');
160 // Tell the comment stream that the vector changed underneath it.
161 CommentStream.resync();
164 void MCAsmStreamer::EmitCommentsAndEOL() {
165 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
170 CommentStream.flush();
171 StringRef Comments = CommentToEmit.str();
173 assert(Comments.back() == '\n' &&
174 "Comment array not newline terminated");
176 // Emit a line of comments.
177 OS.PadToColumn(MAI.getCommentColumn());
178 size_t Position = Comments.find('\n');
179 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
181 Comments = Comments.substr(Position+1);
182 } while (!Comments.empty());
184 CommentToEmit.clear();
185 // Tell the comment stream that the vector changed underneath it.
186 CommentStream.resync();
190 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
191 assert(Bytes && "Invalid size!");
192 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
195 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
196 assert(Section && "Cannot switch to a null section!");
197 if (Section != CurSection) {
198 CurSection = Section;
199 Section->PrintSwitchToSection(MAI, OS);
203 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
204 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
205 assert(CurSection && "Cannot emit before setting section!");
207 OS << *Symbol << ":";
209 Symbol->setSection(*CurSection);
212 void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
214 default: assert(0 && "Invalid flag!");
215 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
220 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
221 // Only absolute symbols can be redefined.
222 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
223 "Cannot define a symbol twice!");
225 OS << *Symbol << " = " << *Value;
228 // FIXME: Lift context changes into super class.
229 // FIXME: Set associated section.
230 Symbol->setValue(Value);
233 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
234 MCSymbolAttr Attribute) {
236 case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
237 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
238 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
239 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
240 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
241 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
242 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
243 assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
244 OS << "\t.type\t" << *Symbol << ','
245 << ((MAI.getCommentString()[0] != '@') ? '@' : '%');
247 default: assert(0 && "Unknown ELF .type");
248 case MCSA_ELF_TypeFunction: OS << "function"; break;
249 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
250 case MCSA_ELF_TypeObject: OS << "object"; break;
251 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
252 case MCSA_ELF_TypeCommon: OS << "common"; break;
253 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
257 case MCSA_Global: // .globl/.global
258 OS << MAI.getGlobalDirective();
260 case MCSA_Hidden: OS << ".hidden "; break;
261 case MCSA_IndirectSymbol: OS << ".indirect_symbol "; break;
262 case MCSA_Internal: OS << ".internal "; break;
263 case MCSA_LazyReference: OS << ".lazy_reference "; break;
264 case MCSA_Local: OS << ".local "; break;
265 case MCSA_NoDeadStrip: OS << ".no_dead_strip "; break;
266 case MCSA_PrivateExtern: OS << ".private_extern "; break;
267 case MCSA_Protected: OS << ".protected "; break;
268 case MCSA_Reference: OS << ".reference "; break;
269 case MCSA_Weak: OS << ".weak "; break;
270 case MCSA_WeakDefinition: OS << ".weak_definition "; break;
272 case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
279 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
280 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
284 void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
285 assert(MAI.hasDotTypeDotSizeDirective());
286 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
289 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
290 unsigned ByteAlignment) {
291 OS << "\t.comm\t" << *Symbol << ',' << Size;
292 if (ByteAlignment != 0) {
293 if (MAI.getCOMMDirectiveAlignmentIsInBytes())
294 OS << ',' << ByteAlignment;
296 OS << ',' << Log2_32(ByteAlignment);
301 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
303 /// @param Symbol - The common symbol to emit.
304 /// @param Size - The size of the common symbol.
305 void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
306 assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
307 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
311 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
312 unsigned Size, unsigned ByteAlignment) {
313 // Note: a .zerofill directive does not switch sections.
316 // This is a mach-o specific directive.
317 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
318 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
320 if (Symbol != NULL) {
321 OS << ',' << *Symbol << ',' << Size;
322 if (ByteAlignment != 0)
323 OS << ',' << Log2_32(ByteAlignment);
328 static inline char toOctal(int X) { return (X&7)+'0'; }
330 static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
333 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
334 unsigned char C = Data[i];
335 if (C == '"' || C == '\\') {
336 OS << '\\' << (char)C;
340 if (isprint((unsigned char)C)) {
346 case '\b': OS << "\\b"; break;
347 case '\f': OS << "\\f"; break;
348 case '\n': OS << "\\n"; break;
349 case '\r': OS << "\\r"; break;
350 case '\t': OS << "\\t"; break;
353 OS << toOctal(C >> 6);
354 OS << toOctal(C >> 3);
355 OS << toOctal(C >> 0);
364 void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
365 assert(CurSection && "Cannot emit contents before setting section!");
366 if (Data.empty()) return;
368 if (Data.size() == 1) {
369 OS << MAI.getData8bitsDirective(AddrSpace);
370 OS << (unsigned)(unsigned char)Data[0];
375 // If the data ends with 0 and the target supports .asciz, use it, otherwise
377 if (MAI.getAscizDirective() && Data.back() == 0) {
378 OS << MAI.getAscizDirective();
379 Data = Data.substr(0, Data.size()-1);
381 OS << MAI.getAsciiDirective();
385 PrintQuotedString(Data, OS);
389 /// EmitIntValue - Special case of EmitValue that avoids the client having
390 /// to pass in a MCExpr for constant integers.
391 void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
392 unsigned AddrSpace) {
393 assert(CurSection && "Cannot emit contents before setting section!");
394 const char *Directive = 0;
397 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
398 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
399 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
401 Directive = MAI.getData64bitsDirective(AddrSpace);
402 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
403 if (Directive) break;
404 if (isLittleEndian()) {
405 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
406 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
408 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
409 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
414 assert(Directive && "Invalid size for machine code value!");
415 OS << Directive << truncateToSize(Value, Size);
419 void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
420 unsigned AddrSpace) {
421 assert(CurSection && "Cannot emit contents before setting section!");
422 const char *Directive = 0;
425 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
426 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
427 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
428 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
431 assert(Directive && "Invalid size for machine code value!");
432 OS << Directive << *Value;
436 void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
437 assert(MAI.getGPRel32Directive() != 0);
438 OS << MAI.getGPRel32Directive() << *Value;
443 /// EmitFill - Emit NumBytes bytes worth of the value specified by
444 /// FillValue. This implements directives such as '.space'.
445 void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
446 unsigned AddrSpace) {
447 if (NumBytes == 0) return;
450 if (const char *ZeroDirective = MAI.getZeroDirective()) {
451 OS << ZeroDirective << NumBytes;
453 OS << ',' << (int)FillValue;
458 // Emit a byte at a time.
459 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
462 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
464 unsigned MaxBytesToEmit) {
465 // Some assemblers don't support non-power of two alignments, so we always
466 // emit alignments as a power of two if possible.
467 if (isPowerOf2_32(ByteAlignment)) {
469 default: llvm_unreachable("Invalid size for machine code value!");
470 case 1: OS << MAI.getAlignDirective(); break;
471 // FIXME: use MAI for this!
472 case 2: OS << ".p2alignw "; break;
473 case 4: OS << ".p2alignl "; break;
474 case 8: llvm_unreachable("Unsupported alignment size!");
477 if (MAI.getAlignmentIsInBytes())
480 OS << Log2_32(ByteAlignment);
482 if (Value || MaxBytesToEmit) {
484 OS.write_hex(truncateToSize(Value, ValueSize));
487 OS << ", " << MaxBytesToEmit;
493 // Non-power of two alignment. This is not widely supported by assemblers.
494 // FIXME: Parameterize this based on MAI.
496 default: llvm_unreachable("Invalid size for machine code value!");
497 case 1: OS << ".balign"; break;
498 case 2: OS << ".balignw"; break;
499 case 4: OS << ".balignl"; break;
500 case 8: llvm_unreachable("Unsupported alignment size!");
503 OS << ' ' << ByteAlignment;
504 OS << ", " << truncateToSize(Value, ValueSize);
506 OS << ", " << MaxBytesToEmit;
510 void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
511 unsigned char Value) {
512 // FIXME: Verify that Offset is associated with the current section.
513 OS << ".org " << *Offset << ", " << (unsigned) Value;
518 void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
519 assert(MAI.hasSingleParameterDotFile());
521 PrintQuotedString(Filename, OS);
525 void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
526 OS << "\t.file\t" << FileNo << ' ';
527 PrintQuotedString(Filename, OS);
532 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
533 assert(CurSection && "Cannot emit contents before setting section!");
535 // Show the MCInst if enabled.
537 raw_ostream &OS = GetCommentOS();
539 Inst.print(OS, &MAI);
543 // Show the encoding in a comment if we have a code emitter.
545 SmallString<256> Code;
546 raw_svector_ostream VecOS(Code);
547 Emitter->EncodeInstruction(Inst, VecOS);
550 raw_ostream &OS = GetCommentOS();
552 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
555 OS << format("0x%02x", uint8_t(Code[i]));
560 // If we have an AsmPrinter, use that to print, otherwise dump the MCInst.
562 InstPrinter->printInst(&Inst);
564 Inst.print(OS, &MAI);
568 void MCAsmStreamer::Finish() {
572 MCStreamer *llvm::createAsmStreamer(MCContext &Context,
573 formatted_raw_ostream &OS,
574 const MCAsmInfo &MAI, bool isLittleEndian,
575 bool isVerboseAsm, MCInstPrinter *IP,
576 MCCodeEmitter *CE, bool ShowInst) {
577 return new MCAsmStreamer(Context, OS, MAI, isLittleEndian, isVerboseAsm,