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/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/MC/MCAsmBackend.h"
16 #include "llvm/MC/MCAsmInfo.h"
17 #include "llvm/MC/MCCodeEmitter.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCFixupKindInfo.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCInstPrinter.h"
23 #include "llvm/MC/MCObjectFileInfo.h"
24 #include "llvm/MC/MCRegisterInfo.h"
25 #include "llvm/MC/MCSectionCOFF.h"
26 #include "llvm/MC/MCSectionMachO.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/Format.h"
31 #include "llvm/Support/FormattedStream.h"
32 #include "llvm/Support/MathExtras.h"
33 #include "llvm/Support/Path.h"
39 class MCAsmStreamer : public MCStreamer {
41 formatted_raw_ostream &OS;
44 std::unique_ptr<MCInstPrinter> InstPrinter;
45 std::unique_ptr<MCCodeEmitter> Emitter;
46 std::unique_ptr<MCAsmBackend> AsmBackend;
48 SmallString<128> CommentToEmit;
49 raw_svector_ostream CommentStream;
51 unsigned IsVerboseAsm : 1;
52 unsigned ShowInst : 1;
53 unsigned UseDwarfDirectory : 1;
55 void EmitRegisterName(int64_t Register);
56 void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
57 void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
60 MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
61 bool isVerboseAsm, bool useDwarfDirectory,
62 MCInstPrinter *printer, MCCodeEmitter *emitter,
63 MCAsmBackend *asmbackend, bool showInst)
64 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
65 InstPrinter(printer), Emitter(emitter), AsmBackend(asmbackend),
66 CommentStream(CommentToEmit), IsVerboseAsm(isVerboseAsm),
67 ShowInst(showInst), UseDwarfDirectory(useDwarfDirectory) {
68 if (InstPrinter && IsVerboseAsm)
69 InstPrinter->setCommentStream(CommentStream);
72 inline void EmitEOL() {
73 // If we don't have any comments, just emit a \n.
80 void EmitCommentsAndEOL();
82 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
84 bool isVerboseAsm() const override { return IsVerboseAsm; }
86 /// hasRawTextSupport - We support EmitRawText.
87 bool hasRawTextSupport() const override { return true; }
89 /// AddComment - Add a comment that can be emitted to the generated .s
90 /// file if applicable as a QoI issue to make the output of the compiler
91 /// more readable. This only affects the MCAsmStreamer, and only when
92 /// verbose assembly output is enabled.
93 void AddComment(const Twine &T) override;
95 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
96 void AddEncodingComment(const MCInst &Inst, const MCSubtargetInfo &);
98 /// GetCommentOS - Return a raw_ostream that comments can be written to.
99 /// Unlike AddComment, you are required to terminate comments with \n if you
101 raw_ostream &GetCommentOS() override {
103 return nulls(); // Discard comments unless in verbose asm mode.
104 return CommentStream;
107 void emitRawComment(const Twine &T, bool TabPrefix = true) override;
109 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
110 void AddBlankLine() override {
114 /// @name MCStreamer Interface
117 void ChangeSection(const MCSection *Section,
118 const MCExpr *Subsection) override;
120 void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override;
121 void EmitLabel(MCSymbol *Symbol) override;
123 void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
124 void EmitLinkerOptions(ArrayRef<std::string> Options) override;
125 void EmitDataRegion(MCDataRegionType Kind) override;
126 void EmitVersionMin(MCVersionMinType Kind, unsigned Major, unsigned Minor,
127 unsigned Update) override;
128 void EmitThumbFunc(MCSymbol *Func) override;
130 void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
131 void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
132 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
134 void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
135 void BeginCOFFSymbolDef(const MCSymbol *Symbol) override;
136 void EmitCOFFSymbolStorageClass(int StorageClass) override;
137 void EmitCOFFSymbolType(int Type) override;
138 void EndCOFFSymbolDef() override;
139 void EmitCOFFSectionIndex(MCSymbol const *Symbol) override;
140 void EmitCOFFSecRel32(MCSymbol const *Symbol) override;
141 void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override;
142 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
143 unsigned ByteAlignment) override;
145 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
147 /// @param Symbol - The common symbol to emit.
148 /// @param Size - The size of the common symbol.
149 /// @param ByteAlignment - The alignment of the common symbol in bytes.
150 void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
151 unsigned ByteAlignment) override;
153 void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = nullptr,
154 uint64_t Size = 0, unsigned ByteAlignment = 0) override;
156 void EmitTBSSSymbol (const MCSection *Section, MCSymbol *Symbol,
157 uint64_t Size, unsigned ByteAlignment = 0) override;
159 void EmitBytes(StringRef Data) override;
161 void EmitValueImpl(const MCExpr *Value, unsigned Size,
162 const SMLoc &Loc = SMLoc()) override;
163 void EmitIntValue(uint64_t Value, unsigned Size) override;
165 void EmitULEB128Value(const MCExpr *Value) override;
167 void EmitSLEB128Value(const MCExpr *Value) override;
169 void EmitGPRel64Value(const MCExpr *Value) override;
171 void EmitGPRel32Value(const MCExpr *Value) override;
174 void EmitFill(uint64_t NumBytes, uint8_t FillValue) override;
176 void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
177 unsigned ValueSize = 1,
178 unsigned MaxBytesToEmit = 0) override;
180 void EmitCodeAlignment(unsigned ByteAlignment,
181 unsigned MaxBytesToEmit = 0) override;
183 bool EmitValueToOffset(const MCExpr *Offset,
184 unsigned char Value = 0) override;
186 void EmitFileDirective(StringRef Filename) override;
187 unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
189 unsigned CUID = 0) override;
190 void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
191 unsigned Column, unsigned Flags,
192 unsigned Isa, unsigned Discriminator,
193 StringRef FileName) override;
194 MCSymbol *getDwarfLineTableSymbol(unsigned CUID) override;
196 void EmitIdent(StringRef IdentString) override;
197 void EmitCFISections(bool EH, bool Debug) override;
198 void EmitCFIDefCfa(int64_t Register, int64_t Offset) override;
199 void EmitCFIDefCfaOffset(int64_t Offset) override;
200 void EmitCFIDefCfaRegister(int64_t Register) override;
201 void EmitCFIOffset(int64_t Register, int64_t Offset) override;
202 void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding) override;
203 void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) override;
204 void EmitCFIRememberState() override;
205 void EmitCFIRestoreState() override;
206 void EmitCFISameValue(int64_t Register) override;
207 void EmitCFIRelOffset(int64_t Register, int64_t Offset) override;
208 void EmitCFIAdjustCfaOffset(int64_t Adjustment) override;
209 void EmitCFISignalFrame() override;
210 void EmitCFIUndefined(int64_t Register) override;
211 void EmitCFIRegister(int64_t Register1, int64_t Register2) override;
212 void EmitCFIWindowSave() override;
214 void EmitWinCFIStartProc(const MCSymbol *Symbol) override;
215 void EmitWinCFIEndProc() override;
216 void EmitWinCFIStartChained() override;
217 void EmitWinCFIEndChained() override;
218 void EmitWinCFIPushReg(unsigned Register) override;
219 void EmitWinCFISetFrame(unsigned Register, unsigned Offset) override;
220 void EmitWinCFIAllocStack(unsigned Size) override;
221 void EmitWinCFISaveReg(unsigned Register, unsigned Offset) override;
222 void EmitWinCFISaveXMM(unsigned Register, unsigned Offset) override;
223 void EmitWinCFIPushFrame(bool Code) override;
224 void EmitWinCFIEndProlog() override;
226 void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except) override;
227 void EmitWinEHHandlerData() override;
229 void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
231 void EmitBundleAlignMode(unsigned AlignPow2) override;
232 void EmitBundleLock(bool AlignToEnd) override;
233 void EmitBundleUnlock() override;
235 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
236 /// the specified string in the output .s file. This capability is
237 /// indicated by the hasRawTextSupport() predicate.
238 void EmitRawTextImpl(StringRef String) override;
240 void FinishImpl() override;
243 } // end anonymous namespace.
245 /// AddComment - Add a comment that can be emitted to the generated .s
246 /// file if applicable as a QoI issue to make the output of the compiler
247 /// more readable. This only affects the MCAsmStreamer, and only when
248 /// verbose assembly output is enabled.
249 void MCAsmStreamer::AddComment(const Twine &T) {
250 if (!IsVerboseAsm) return;
252 // Make sure that CommentStream is flushed.
253 CommentStream.flush();
255 T.toVector(CommentToEmit);
256 // Each comment goes on its own line.
257 CommentToEmit.push_back('\n');
259 // Tell the comment stream that the vector changed underneath it.
260 CommentStream.resync();
263 void MCAsmStreamer::EmitCommentsAndEOL() {
264 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
269 CommentStream.flush();
270 StringRef Comments = CommentToEmit.str();
272 assert(Comments.back() == '\n' &&
273 "Comment array not newline terminated");
275 // Emit a line of comments.
276 OS.PadToColumn(MAI->getCommentColumn());
277 size_t Position = Comments.find('\n');
278 OS << MAI->getCommentString() << ' ' << Comments.substr(0, Position) <<'\n';
280 Comments = Comments.substr(Position+1);
281 } while (!Comments.empty());
283 CommentToEmit.clear();
284 // Tell the comment stream that the vector changed underneath it.
285 CommentStream.resync();
288 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
289 assert(Bytes && "Invalid size!");
290 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
293 void MCAsmStreamer::emitRawComment(const Twine &T, bool TabPrefix) {
296 OS << MAI->getCommentString() << T;
300 void MCAsmStreamer::ChangeSection(const MCSection *Section,
301 const MCExpr *Subsection) {
302 assert(Section && "Cannot switch to a null section!");
303 Section->PrintSwitchToSection(*MAI, OS, Subsection);
306 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
307 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
308 MCStreamer::EmitLabel(Symbol);
310 OS << *Symbol << MAI->getLabelSuffix();
314 void MCAsmStreamer::EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {
315 StringRef str = MCLOHIdToName(Kind);
318 int NbArgs = MCLOHIdToNbArgs(Kind);
319 assert(NbArgs != -1 && ((size_t)NbArgs) == Args.size() && "Malformed LOH!");
320 assert(str != "" && "Invalid LOH name");
323 OS << "\t" << MCLOHDirectiveName() << " " << str << "\t";
325 for (MCLOHArgs::const_iterator It = Args.begin(), EndIt = Args.end();
335 void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
337 case MCAF_SyntaxUnified: OS << "\t.syntax unified"; break;
338 case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
339 case MCAF_Code16: OS << '\t'<< MAI->getCode16Directive();break;
340 case MCAF_Code32: OS << '\t'<< MAI->getCode32Directive();break;
341 case MCAF_Code64: OS << '\t'<< MAI->getCode64Directive();break;
346 void MCAsmStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
347 assert(!Options.empty() && "At least one option is required!");
348 OS << "\t.linker_option \"" << Options[0] << '"';
349 for (ArrayRef<std::string>::iterator it = Options.begin() + 1,
350 ie = Options.end(); it != ie; ++it) {
351 OS << ", " << '"' << *it << '"';
356 void MCAsmStreamer::EmitDataRegion(MCDataRegionType Kind) {
357 if (!MAI->doesSupportDataRegionDirectives())
360 case MCDR_DataRegion: OS << "\t.data_region"; break;
361 case MCDR_DataRegionJT8: OS << "\t.data_region jt8"; break;
362 case MCDR_DataRegionJT16: OS << "\t.data_region jt16"; break;
363 case MCDR_DataRegionJT32: OS << "\t.data_region jt32"; break;
364 case MCDR_DataRegionEnd: OS << "\t.end_data_region"; break;
369 void MCAsmStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major,
370 unsigned Minor, unsigned Update) {
372 case MCVM_IOSVersionMin: OS << "\t.ios_version_min"; break;
373 case MCVM_OSXVersionMin: OS << "\t.macosx_version_min"; break;
375 OS << " " << Major << ", " << Minor;
377 OS << ", " << Update;
381 void MCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {
382 // This needs to emit to a temporary string to get properly quoted
383 // MCSymbols when they have spaces in them.
384 OS << "\t.thumb_func";
385 // Only Mach-O hasSubsectionsViaSymbols()
386 if (MAI->hasSubsectionsViaSymbols())
391 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
392 OS << *Symbol << " = " << *Value;
395 MCStreamer::EmitAssignment(Symbol, Value);
398 void MCAsmStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
399 OS << ".weakref " << *Alias << ", " << *Symbol;
403 bool MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
404 MCSymbolAttr Attribute) {
406 case MCSA_Invalid: llvm_unreachable("Invalid symbol attribute");
407 case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
408 case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
409 case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
410 case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
411 case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
412 case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
413 case MCSA_ELF_TypeGnuUniqueObject: /// .type _foo, @gnu_unique_object
414 if (!MAI->hasDotTypeDotSizeDirective())
415 return false; // Symbol attribute not supported
416 OS << "\t.type\t" << *Symbol << ','
417 << ((MAI->getCommentString()[0] != '@') ? '@' : '%');
419 default: return false;
420 case MCSA_ELF_TypeFunction: OS << "function"; break;
421 case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
422 case MCSA_ELF_TypeObject: OS << "object"; break;
423 case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
424 case MCSA_ELF_TypeCommon: OS << "common"; break;
425 case MCSA_ELF_TypeNoType: OS << "no_type"; break;
426 case MCSA_ELF_TypeGnuUniqueObject: OS << "gnu_unique_object"; break;
430 case MCSA_Global: // .globl/.global
431 OS << MAI->getGlobalDirective();
433 case MCSA_Hidden: OS << "\t.hidden\t"; break;
434 case MCSA_IndirectSymbol: OS << "\t.indirect_symbol\t"; break;
435 case MCSA_Internal: OS << "\t.internal\t"; break;
436 case MCSA_LazyReference: OS << "\t.lazy_reference\t"; break;
437 case MCSA_Local: OS << "\t.local\t"; break;
438 case MCSA_NoDeadStrip:
439 if (!MAI->hasNoDeadStrip())
441 OS << "\t.no_dead_strip\t";
443 case MCSA_SymbolResolver: OS << "\t.symbol_resolver\t"; break;
444 case MCSA_PrivateExtern:
445 OS << "\t.private_extern\t";
447 case MCSA_Protected: OS << "\t.protected\t"; break;
448 case MCSA_Reference: OS << "\t.reference\t"; break;
449 case MCSA_Weak: OS << MAI->getWeakDirective(); break;
450 case MCSA_WeakDefinition:
451 OS << "\t.weak_definition\t";
454 case MCSA_WeakReference: OS << MAI->getWeakRefDirective(); break;
455 case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
464 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
465 OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
469 void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
470 OS << "\t.def\t " << *Symbol << ';';
474 void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {
475 OS << "\t.scl\t" << StorageClass << ';';
479 void MCAsmStreamer::EmitCOFFSymbolType (int Type) {
480 OS << "\t.type\t" << Type << ';';
484 void MCAsmStreamer::EndCOFFSymbolDef() {
489 void MCAsmStreamer::EmitCOFFSectionIndex(MCSymbol const *Symbol) {
490 OS << "\t.secidx\t" << *Symbol;
494 void MCAsmStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
495 OS << "\t.secrel32\t" << *Symbol;
499 void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
500 assert(MAI->hasDotTypeDotSizeDirective());
501 OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
504 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
505 unsigned ByteAlignment) {
506 // Common symbols do not belong to any actual section.
507 AssignSection(Symbol, nullptr);
509 OS << "\t.comm\t" << *Symbol << ',' << Size;
510 if (ByteAlignment != 0) {
511 if (MAI->getCOMMDirectiveAlignmentIsInBytes())
512 OS << ',' << ByteAlignment;
514 OS << ',' << Log2_32(ByteAlignment);
519 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
521 /// @param Symbol - The common symbol to emit.
522 /// @param Size - The size of the common symbol.
523 void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
524 unsigned ByteAlign) {
525 // Common symbols do not belong to any actual section.
526 AssignSection(Symbol, nullptr);
528 OS << "\t.lcomm\t" << *Symbol << ',' << Size;
530 switch (MAI->getLCOMMDirectiveAlignmentType()) {
531 case LCOMM::NoAlignment:
532 llvm_unreachable("alignment not supported on .lcomm!");
533 case LCOMM::ByteAlignment:
534 OS << ',' << ByteAlign;
536 case LCOMM::Log2Alignment:
537 assert(isPowerOf2_32(ByteAlign) && "alignment must be a power of 2");
538 OS << ',' << Log2_32(ByteAlign);
545 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
546 uint64_t Size, unsigned ByteAlignment) {
548 AssignSection(Symbol, Section);
550 // Note: a .zerofill directive does not switch sections.
553 // This is a mach-o specific directive.
554 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
555 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
558 OS << ',' << *Symbol << ',' << Size;
559 if (ByteAlignment != 0)
560 OS << ',' << Log2_32(ByteAlignment);
565 // .tbss sym, size, align
566 // This depends that the symbol has already been mangled from the original,
568 void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
569 uint64_t Size, unsigned ByteAlignment) {
570 AssignSection(Symbol, Section);
572 assert(Symbol && "Symbol shouldn't be NULL!");
573 // Instead of using the Section we'll just use the shortcut.
574 // This is a mach-o specific directive and section.
575 OS << ".tbss " << *Symbol << ", " << Size;
577 // Output align if we have it. We default to 1 so don't bother printing
579 if (ByteAlignment > 1) OS << ", " << Log2_32(ByteAlignment);
584 static inline char toOctal(int X) { return (X&7)+'0'; }
586 static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
589 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
590 unsigned char C = Data[i];
591 if (C == '"' || C == '\\') {
592 OS << '\\' << (char)C;
596 if (isprint((unsigned char)C)) {
602 case '\b': OS << "\\b"; break;
603 case '\f': OS << "\\f"; break;
604 case '\n': OS << "\\n"; break;
605 case '\r': OS << "\\r"; break;
606 case '\t': OS << "\\t"; break;
609 OS << toOctal(C >> 6);
610 OS << toOctal(C >> 3);
611 OS << toOctal(C >> 0);
620 void MCAsmStreamer::EmitBytes(StringRef Data) {
621 assert(getCurrentSection().first &&
622 "Cannot emit contents before setting section!");
623 if (Data.empty()) return;
625 if (Data.size() == 1) {
626 OS << MAI->getData8bitsDirective();
627 OS << (unsigned)(unsigned char)Data[0];
632 // If the data ends with 0 and the target supports .asciz, use it, otherwise
634 if (MAI->getAscizDirective() && Data.back() == 0) {
635 OS << MAI->getAscizDirective();
636 Data = Data.substr(0, Data.size()-1);
638 OS << MAI->getAsciiDirective();
641 PrintQuotedString(Data, OS);
645 void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size) {
646 EmitValue(MCConstantExpr::Create(Value, getContext()), Size);
649 void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
651 assert(Size <= 8 && "Invalid size");
652 assert(getCurrentSection().first &&
653 "Cannot emit contents before setting section!");
654 const char *Directive = nullptr;
657 case 1: Directive = MAI->getData8bitsDirective(); break;
658 case 2: Directive = MAI->getData16bitsDirective(); break;
659 case 4: Directive = MAI->getData32bitsDirective(); break;
660 case 8: Directive = MAI->getData64bitsDirective(); break;
665 if (!Value->EvaluateAsAbsolute(IntValue))
666 report_fatal_error("Don't know how to emit this value.");
668 // We couldn't handle the requested integer size so we fallback by breaking
669 // the request down into several, smaller, integers. Since sizes greater
670 // than eight are invalid and size equivalent to eight should have been
671 // handled earlier, we use four bytes as our largest piece of granularity.
672 bool IsLittleEndian = MAI->isLittleEndian();
673 for (unsigned Emitted = 0; Emitted != Size;) {
674 unsigned Remaining = Size - Emitted;
675 // The size of our partial emission must be a power of two less than
677 unsigned EmissionSize = PowerOf2Floor(Remaining);
678 if (EmissionSize > 4)
680 // Calculate the byte offset of our partial emission taking into account
681 // the endianness of the target.
682 unsigned ByteOffset =
683 IsLittleEndian ? Emitted : (Remaining - EmissionSize);
684 uint64_t ValueToEmit = IntValue >> (ByteOffset * 8);
685 // We truncate our partial emission to fit within the bounds of the
686 // emission domain. This produces nicer output and silences potential
687 // truncation warnings when round tripping through another assembler.
688 uint64_t Shift = 64 - EmissionSize * 8;
689 assert(Shift < static_cast<uint64_t>(
690 std::numeric_limits<unsigned long long>::digits) &&
691 "undefined behavior");
692 ValueToEmit &= ~0ULL >> Shift;
693 EmitIntValue(ValueToEmit, EmissionSize);
694 Emitted += EmissionSize;
699 assert(Directive && "Invalid size for machine code value!");
700 OS << Directive << *Value;
704 void MCAsmStreamer::EmitULEB128Value(const MCExpr *Value) {
706 if (Value->EvaluateAsAbsolute(IntValue)) {
707 EmitULEB128IntValue(IntValue);
710 OS << ".uleb128 " << *Value;
714 void MCAsmStreamer::EmitSLEB128Value(const MCExpr *Value) {
716 if (Value->EvaluateAsAbsolute(IntValue)) {
717 EmitSLEB128IntValue(IntValue);
720 OS << ".sleb128 " << *Value;
724 void MCAsmStreamer::EmitGPRel64Value(const MCExpr *Value) {
725 assert(MAI->getGPRel64Directive() != nullptr);
726 OS << MAI->getGPRel64Directive() << *Value;
730 void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
731 assert(MAI->getGPRel32Directive() != nullptr);
732 OS << MAI->getGPRel32Directive() << *Value;
737 /// EmitFill - Emit NumBytes bytes worth of the value specified by
738 /// FillValue. This implements directives such as '.space'.
739 void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) {
740 if (NumBytes == 0) return;
742 if (const char *ZeroDirective = MAI->getZeroDirective()) {
743 OS << ZeroDirective << NumBytes;
745 OS << ',' << (int)FillValue;
750 // Emit a byte at a time.
751 MCStreamer::EmitFill(NumBytes, FillValue);
754 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
756 unsigned MaxBytesToEmit) {
757 // Some assemblers don't support non-power of two alignments, so we always
758 // emit alignments as a power of two if possible.
759 if (isPowerOf2_32(ByteAlignment)) {
762 llvm_unreachable("Invalid size for machine code value!");
773 llvm_unreachable("Unsupported alignment size!");
776 if (MAI->getAlignmentIsInBytes())
779 OS << Log2_32(ByteAlignment);
781 if (Value || MaxBytesToEmit) {
783 OS.write_hex(truncateToSize(Value, ValueSize));
786 OS << ", " << MaxBytesToEmit;
792 // Non-power of two alignment. This is not widely supported by assemblers.
793 // FIXME: Parameterize this based on MAI.
795 default: llvm_unreachable("Invalid size for machine code value!");
796 case 1: OS << ".balign"; break;
797 case 2: OS << ".balignw"; break;
798 case 4: OS << ".balignl"; break;
799 case 8: llvm_unreachable("Unsupported alignment size!");
802 OS << ' ' << ByteAlignment;
803 OS << ", " << truncateToSize(Value, ValueSize);
805 OS << ", " << MaxBytesToEmit;
809 void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
810 unsigned MaxBytesToEmit) {
811 // Emit with a text fill value.
812 EmitValueToAlignment(ByteAlignment, MAI->getTextAlignFillValue(),
816 bool MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
817 unsigned char Value) {
818 // FIXME: Verify that Offset is associated with the current section.
819 OS << ".org " << *Offset << ", " << (unsigned) Value;
825 void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
826 assert(MAI->hasSingleParameterDotFile());
828 PrintQuotedString(Filename, OS);
832 unsigned MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo,
838 MCDwarfLineTable &Table = getContext().getMCDwarfLineTable(CUID);
839 unsigned NumFiles = Table.getMCDwarfFiles().size();
840 FileNo = Table.getFile(Directory, Filename, FileNo);
843 if (NumFiles == Table.getMCDwarfFiles().size())
846 SmallString<128> FullPathName;
848 if (!UseDwarfDirectory && !Directory.empty()) {
849 if (sys::path::is_absolute(Filename))
852 FullPathName = Directory;
853 sys::path::append(FullPathName, Filename);
855 Filename = FullPathName;
859 OS << "\t.file\t" << FileNo << ' ';
860 if (!Directory.empty()) {
861 PrintQuotedString(Directory, OS);
864 PrintQuotedString(Filename, OS);
870 void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
871 unsigned Column, unsigned Flags,
873 unsigned Discriminator,
874 StringRef FileName) {
875 OS << "\t.loc\t" << FileNo << " " << Line << " " << Column;
876 if (Flags & DWARF2_FLAG_BASIC_BLOCK)
877 OS << " basic_block";
878 if (Flags & DWARF2_FLAG_PROLOGUE_END)
879 OS << " prologue_end";
880 if (Flags & DWARF2_FLAG_EPILOGUE_BEGIN)
881 OS << " epilogue_begin";
883 unsigned OldFlags = getContext().getCurrentDwarfLoc().getFlags();
884 if ((Flags & DWARF2_FLAG_IS_STMT) != (OldFlags & DWARF2_FLAG_IS_STMT)) {
887 if (Flags & DWARF2_FLAG_IS_STMT)
894 OS << " isa " << Isa;
896 OS << " discriminator " << Discriminator;
899 OS.PadToColumn(MAI->getCommentColumn());
900 OS << MAI->getCommentString() << ' ' << FileName << ':'
901 << Line << ':' << Column;
904 this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
905 Isa, Discriminator, FileName);
908 MCSymbol *MCAsmStreamer::getDwarfLineTableSymbol(unsigned CUID) {
909 // Always use the zeroth line table, since asm syntax only supports one line
911 return MCStreamer::getDwarfLineTableSymbol(0);
914 void MCAsmStreamer::EmitIdent(StringRef IdentString) {
915 assert(MAI->hasIdentDirective() && ".ident directive not supported");
917 PrintQuotedString(IdentString, OS);
921 void MCAsmStreamer::EmitCFISections(bool EH, bool Debug) {
922 MCStreamer::EmitCFISections(EH, Debug);
923 OS << "\t.cfi_sections ";
927 OS << ", .debug_frame";
929 OS << ".debug_frame";
935 void MCAsmStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
936 OS << "\t.cfi_startproc";
942 void MCAsmStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
943 MCStreamer::EmitCFIEndProcImpl(Frame);
944 OS << "\t.cfi_endproc";
948 void MCAsmStreamer::EmitRegisterName(int64_t Register) {
949 if (InstPrinter && !MAI->useDwarfRegNumForCFI()) {
950 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
951 unsigned LLVMRegister = MRI->getLLVMRegNum(Register, true);
952 InstPrinter->printRegName(OS, LLVMRegister);
958 void MCAsmStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) {
959 MCStreamer::EmitCFIDefCfa(Register, Offset);
960 OS << "\t.cfi_def_cfa ";
961 EmitRegisterName(Register);
962 OS << ", " << Offset;
966 void MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
967 MCStreamer::EmitCFIDefCfaOffset(Offset);
968 OS << "\t.cfi_def_cfa_offset " << Offset;
972 void MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) {
973 MCStreamer::EmitCFIDefCfaRegister(Register);
974 OS << "\t.cfi_def_cfa_register ";
975 EmitRegisterName(Register);
979 void MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
980 this->MCStreamer::EmitCFIOffset(Register, Offset);
981 OS << "\t.cfi_offset ";
982 EmitRegisterName(Register);
983 OS << ", " << Offset;
987 void MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym,
989 MCStreamer::EmitCFIPersonality(Sym, Encoding);
990 OS << "\t.cfi_personality " << Encoding << ", " << *Sym;
994 void MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
995 MCStreamer::EmitCFILsda(Sym, Encoding);
996 OS << "\t.cfi_lsda " << Encoding << ", " << *Sym;
1000 void MCAsmStreamer::EmitCFIRememberState() {
1001 MCStreamer::EmitCFIRememberState();
1002 OS << "\t.cfi_remember_state";
1006 void MCAsmStreamer::EmitCFIRestoreState() {
1007 MCStreamer::EmitCFIRestoreState();
1008 OS << "\t.cfi_restore_state";
1012 void MCAsmStreamer::EmitCFISameValue(int64_t Register) {
1013 MCStreamer::EmitCFISameValue(Register);
1014 OS << "\t.cfi_same_value ";
1015 EmitRegisterName(Register);
1019 void MCAsmStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) {
1020 MCStreamer::EmitCFIRelOffset(Register, Offset);
1021 OS << "\t.cfi_rel_offset ";
1022 EmitRegisterName(Register);
1023 OS << ", " << Offset;
1027 void MCAsmStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) {
1028 MCStreamer::EmitCFIAdjustCfaOffset(Adjustment);
1029 OS << "\t.cfi_adjust_cfa_offset " << Adjustment;
1033 void MCAsmStreamer::EmitCFISignalFrame() {
1034 MCStreamer::EmitCFISignalFrame();
1035 OS << "\t.cfi_signal_frame";
1039 void MCAsmStreamer::EmitCFIUndefined(int64_t Register) {
1040 MCStreamer::EmitCFIUndefined(Register);
1041 OS << "\t.cfi_undefined " << Register;
1045 void MCAsmStreamer::EmitCFIRegister(int64_t Register1, int64_t Register2) {
1046 MCStreamer::EmitCFIRegister(Register1, Register2);
1047 OS << "\t.cfi_register " << Register1 << ", " << Register2;
1051 void MCAsmStreamer::EmitCFIWindowSave() {
1052 MCStreamer::EmitCFIWindowSave();
1053 OS << "\t.cfi_window_save";
1057 void MCAsmStreamer::EmitWinCFIStartProc(const MCSymbol *Symbol) {
1058 MCStreamer::EmitWinCFIStartProc(Symbol);
1060 OS << ".seh_proc " << *Symbol;
1064 void MCAsmStreamer::EmitWinCFIEndProc() {
1065 MCStreamer::EmitWinCFIEndProc();
1067 OS << "\t.seh_endproc";
1071 void MCAsmStreamer::EmitWinCFIStartChained() {
1072 MCStreamer::EmitWinCFIStartChained();
1074 OS << "\t.seh_startchained";
1078 void MCAsmStreamer::EmitWinCFIEndChained() {
1079 MCStreamer::EmitWinCFIEndChained();
1081 OS << "\t.seh_endchained";
1085 void MCAsmStreamer::EmitWinEHHandler(const MCSymbol *Sym, bool Unwind,
1087 MCStreamer::EmitWinEHHandler(Sym, Unwind, Except);
1089 OS << "\t.seh_handler " << *Sym;
1097 void MCAsmStreamer::EmitWinEHHandlerData() {
1098 MCStreamer::EmitWinEHHandlerData();
1100 // Switch sections. Don't call SwitchSection directly, because that will
1101 // cause the section switch to be visible in the emitted assembly.
1102 // We only do this so the section switch that terminates the handler
1103 // data block is visible.
1104 WinEH::FrameInfo *CurFrame = getCurrentWinFrameInfo();
1105 if (const MCSection *XData = WinEH::UnwindEmitter::getXDataSection(
1106 CurFrame->Function, getContext()))
1107 SwitchSectionNoChange(XData);
1109 OS << "\t.seh_handlerdata";
1113 void MCAsmStreamer::EmitWinCFIPushReg(unsigned Register) {
1114 MCStreamer::EmitWinCFIPushReg(Register);
1116 OS << "\t.seh_pushreg " << Register;
1120 void MCAsmStreamer::EmitWinCFISetFrame(unsigned Register, unsigned Offset) {
1121 MCStreamer::EmitWinCFISetFrame(Register, Offset);
1123 OS << "\t.seh_setframe " << Register << ", " << Offset;
1127 void MCAsmStreamer::EmitWinCFIAllocStack(unsigned Size) {
1128 MCStreamer::EmitWinCFIAllocStack(Size);
1130 OS << "\t.seh_stackalloc " << Size;
1134 void MCAsmStreamer::EmitWinCFISaveReg(unsigned Register, unsigned Offset) {
1135 MCStreamer::EmitWinCFISaveReg(Register, Offset);
1137 OS << "\t.seh_savereg " << Register << ", " << Offset;
1141 void MCAsmStreamer::EmitWinCFISaveXMM(unsigned Register, unsigned Offset) {
1142 MCStreamer::EmitWinCFISaveXMM(Register, Offset);
1144 OS << "\t.seh_savexmm " << Register << ", " << Offset;
1148 void MCAsmStreamer::EmitWinCFIPushFrame(bool Code) {
1149 MCStreamer::EmitWinCFIPushFrame(Code);
1151 OS << "\t.seh_pushframe";
1157 void MCAsmStreamer::EmitWinCFIEndProlog(void) {
1158 MCStreamer::EmitWinCFIEndProlog();
1160 OS << "\t.seh_endprologue";
1164 void MCAsmStreamer::AddEncodingComment(const MCInst &Inst,
1165 const MCSubtargetInfo &STI) {
1166 raw_ostream &OS = GetCommentOS();
1167 SmallString<256> Code;
1168 SmallVector<MCFixup, 4> Fixups;
1169 raw_svector_ostream VecOS(Code);
1170 Emitter->EncodeInstruction(Inst, VecOS, Fixups, STI);
1173 // If we are showing fixups, create symbolic markers in the encoded
1174 // representation. We do this by making a per-bit map to the fixup item index,
1175 // then trying to display it as nicely as possible.
1176 SmallVector<uint8_t, 64> FixupMap;
1177 FixupMap.resize(Code.size() * 8);
1178 for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
1181 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1182 MCFixup &F = Fixups[i];
1183 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
1184 for (unsigned j = 0; j != Info.TargetSize; ++j) {
1185 unsigned Index = F.getOffset() * 8 + Info.TargetOffset + j;
1186 assert(Index < Code.size() * 8 && "Invalid offset in fixup!");
1187 FixupMap[Index] = 1 + i;
1191 // FIXME: Note the fixup comments for Thumb2 are completely bogus since the
1192 // high order halfword of a 32-bit Thumb2 instruction is emitted first.
1193 OS << "encoding: [";
1194 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
1198 // See if all bits are the same map entry.
1199 uint8_t MapEntry = FixupMap[i * 8 + 0];
1200 for (unsigned j = 1; j != 8; ++j) {
1201 if (FixupMap[i * 8 + j] == MapEntry)
1204 MapEntry = uint8_t(~0U);
1208 if (MapEntry != uint8_t(~0U)) {
1209 if (MapEntry == 0) {
1210 OS << format("0x%02x", uint8_t(Code[i]));
1213 // FIXME: Some of the 8 bits require fix up.
1214 OS << format("0x%02x", uint8_t(Code[i])) << '\''
1215 << char('A' + MapEntry - 1) << '\'';
1217 OS << char('A' + MapEntry - 1);
1220 // Otherwise, write out in binary.
1222 for (unsigned j = 8; j--;) {
1223 unsigned Bit = (Code[i] >> j) & 1;
1226 if (MAI->isLittleEndian())
1227 FixupBit = i * 8 + j;
1229 FixupBit = i * 8 + (7-j);
1231 if (uint8_t MapEntry = FixupMap[FixupBit]) {
1232 assert(Bit == 0 && "Encoder wrote into fixed up bit!");
1233 OS << char('A' + MapEntry - 1);
1241 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
1242 MCFixup &F = Fixups[i];
1243 const MCFixupKindInfo &Info = AsmBackend->getFixupKindInfo(F.getKind());
1244 OS << " fixup " << char('A' + i) << " - " << "offset: " << F.getOffset()
1245 << ", value: " << *F.getValue() << ", kind: " << Info.Name << "\n";
1249 void MCAsmStreamer::EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) {
1250 assert(getCurrentSection().first &&
1251 "Cannot emit contents before setting section!");
1253 // Show the encoding in a comment if we have a code emitter.
1255 AddEncodingComment(Inst, STI);
1257 // Show the MCInst if enabled.
1259 Inst.dump_pretty(GetCommentOS(), InstPrinter.get(), "\n ");
1260 GetCommentOS() << "\n";
1263 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
1265 InstPrinter->printInst(&Inst, OS, "");
1271 void MCAsmStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
1272 OS << "\t.bundle_align_mode " << AlignPow2;
1276 void MCAsmStreamer::EmitBundleLock(bool AlignToEnd) {
1277 OS << "\t.bundle_lock";
1279 OS << " align_to_end";
1283 void MCAsmStreamer::EmitBundleUnlock() {
1284 OS << "\t.bundle_unlock";
1288 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
1289 /// the specified string in the output .s file. This capability is
1290 /// indicated by the hasRawTextSupport() predicate.
1291 void MCAsmStreamer::EmitRawTextImpl(StringRef String) {
1292 if (!String.empty() && String.back() == '\n')
1293 String = String.substr(0, String.size()-1);
1298 void MCAsmStreamer::FinishImpl() {
1299 // If we are generating dwarf for assembly source files dump out the sections.
1300 if (getContext().getGenDwarfForAssembly())
1301 MCGenDwarfInfo::Emit(this);
1303 // Emit the label for the line table, if requested - since the rest of the
1304 // line table will be defined by .loc/.file directives, and not emitted
1305 // directly, the label is the only work required here.
1306 auto &Tables = getContext().getMCDwarfLineTables();
1307 if (!Tables.empty()) {
1308 assert(Tables.size() == 1 && "asm output only supports one line table");
1309 if (auto *Label = Tables.begin()->second.getLabel()) {
1310 SwitchSection(getContext().getObjectFileInfo()->getDwarfLineSection());
1316 MCStreamer *llvm::createAsmStreamer(MCContext &Context,
1317 formatted_raw_ostream &OS,
1318 bool isVerboseAsm, bool useDwarfDirectory,
1319 MCInstPrinter *IP, MCCodeEmitter *CE,
1320 MCAsmBackend *MAB, bool ShowInst) {
1321 return new MCAsmStreamer(Context, OS, isVerboseAsm, useDwarfDirectory, IP, CE,