1 //===- MCStreamer.h - High-level Streaming Machine Code Output --*- C++ -*-===//
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 // This file declares the MCStreamer class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_MC_MCSTREAMER_H
15 #define LLVM_MC_MCSTREAMER_H
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/MC/MCDirectives.h"
23 #include "llvm/MC/MCDwarf.h"
24 #include "llvm/MC/MCLinkerOptimizationHint.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/MC/MCWinEH.h"
27 #include "llvm/Support/Error.h"
28 #include "llvm/Support/MD5.h"
29 #include "llvm/Support/SMLoc.h"
30 #include "llvm/Support/TargetParser.h"
31 #include "llvm/Support/VersionTuple.h"
41 class AssemblerConstantPools;
42 class formatted_raw_ostream;
45 struct MCCodePaddingContext;
52 class MCSymbolRefExpr;
53 class MCSubtargetInfo;
57 using MCSectionSubPair = std::pair<MCSection *, const MCExpr *>;
59 /// Target specific streamer interface. This is used so that targets can
60 /// implement support for target specific assembly directives.
62 /// If target foo wants to use this, it should implement 3 classes:
63 /// * FooTargetStreamer : public MCTargetStreamer
64 /// * FooTargetAsmStreamer : public FooTargetStreamer
65 /// * FooTargetELFStreamer : public FooTargetStreamer
67 /// FooTargetStreamer should have a pure virtual method for each directive. For
68 /// example, for a ".bar symbol_name" directive, it should have
69 /// virtual emitBar(const MCSymbol &Symbol) = 0;
71 /// The FooTargetAsmStreamer and FooTargetELFStreamer classes implement the
72 /// method. The assembly streamer just prints ".bar symbol_name". The object
73 /// streamer does whatever is needed to implement .bar in the object file.
75 /// In the assembly printer and parser the target streamer can be used by
76 /// calling getTargetStreamer and casting it to FooTargetStreamer:
78 /// MCTargetStreamer &TS = OutStreamer.getTargetStreamer();
79 /// FooTargetStreamer &ATS = static_cast<FooTargetStreamer &>(TS);
81 /// The base classes FooTargetAsmStreamer and FooTargetELFStreamer should
82 /// *never* be treated differently. Callers should always talk to a
83 /// FooTargetStreamer.
84 class MCTargetStreamer {
89 MCTargetStreamer(MCStreamer &S);
90 virtual ~MCTargetStreamer();
92 MCStreamer &getStreamer() { return Streamer; }
94 // Allow a target to add behavior to the EmitLabel of MCStreamer.
95 virtual void emitLabel(MCSymbol *Symbol);
96 // Allow a target to add behavior to the emitAssignment of MCStreamer.
97 virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value);
99 virtual void prettyPrintAsm(MCInstPrinter &InstPrinter, raw_ostream &OS,
100 const MCInst &Inst, const MCSubtargetInfo &STI);
102 virtual void emitDwarfFileDirective(StringRef Directive);
104 /// Update streamer for a new active section.
106 /// This is called by PopSection and SwitchSection, if the current
108 virtual void changeSection(const MCSection *CurSection, MCSection *Section,
109 const MCExpr *SubSection, raw_ostream &OS);
111 virtual void emitValue(const MCExpr *Value);
113 /// Emit the bytes in \p Data into the output.
115 /// This is used to emit bytes in \p Data as sequence of .byte directives.
116 virtual void emitRawBytes(StringRef Data);
118 virtual void finish();
121 // FIXME: declared here because it is used from
122 // lib/CodeGen/AsmPrinter/ARMException.cpp.
123 class ARMTargetStreamer : public MCTargetStreamer {
125 ARMTargetStreamer(MCStreamer &S);
126 ~ARMTargetStreamer() override;
128 virtual void emitFnStart();
129 virtual void emitFnEnd();
130 virtual void emitCantUnwind();
131 virtual void emitPersonality(const MCSymbol *Personality);
132 virtual void emitPersonalityIndex(unsigned Index);
133 virtual void emitHandlerData();
134 virtual void emitSetFP(unsigned FpReg, unsigned SpReg,
136 virtual void emitMovSP(unsigned Reg, int64_t Offset = 0);
137 virtual void emitPad(int64_t Offset);
138 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
140 virtual void emitUnwindRaw(int64_t StackOffset,
141 const SmallVectorImpl<uint8_t> &Opcodes);
143 virtual void switchVendor(StringRef Vendor);
144 virtual void emitAttribute(unsigned Attribute, unsigned Value);
145 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
146 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
147 StringRef StringValue = "");
148 virtual void emitFPU(unsigned FPU);
149 virtual void emitArch(ARM::ArchKind Arch);
150 virtual void emitArchExtension(unsigned ArchExt);
151 virtual void emitObjectArch(ARM::ArchKind Arch);
152 void emitTargetAttributes(const MCSubtargetInfo &STI);
153 virtual void finishAttributeSection();
154 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
156 virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
158 virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value);
160 void finish() override;
162 /// Reset any state between object emissions, i.e. the equivalent of
163 /// MCStreamer's reset method.
164 virtual void reset();
166 /// Callback used to implement the ldr= pseudo.
167 /// Add a new entry to the constant pool for the current section and return an
168 /// MCExpr that can be used to refer to the constant pool location.
169 const MCExpr *addConstantPoolEntry(const MCExpr *, SMLoc Loc);
171 /// Callback used to implemnt the .ltorg directive.
172 /// Emit contents of constant pool for the current section.
173 void emitCurrentConstantPool();
176 std::unique_ptr<AssemblerConstantPools> ConstantPools;
179 /// Streaming machine code generation interface.
181 /// This interface is intended to provide a programatic interface that is very
182 /// similar to the level that an assembler .s file provides. It has callbacks
183 /// to emit bytes, handle directives, etc. The implementation of this interface
184 /// retains state to know what the current section is etc.
186 /// There are multiple implementations of this interface: one for writing out
187 /// a .s file, and implementations that write out .o files of various formats.
191 std::unique_ptr<MCTargetStreamer> TargetStreamer;
193 std::vector<MCDwarfFrameInfo> DwarfFrameInfos;
194 MCDwarfFrameInfo *getCurrentDwarfFrameInfo();
196 /// Similar to DwarfFrameInfos, but for SEH unwind info. Chained frames may
197 /// refer to each other, so use std::unique_ptr to provide pointer stability.
198 std::vector<std::unique_ptr<WinEH::FrameInfo>> WinFrameInfos;
200 WinEH::FrameInfo *CurrentWinFrameInfo;
202 /// Tracks an index to represent the order a symbol was emitted in.
203 /// Zero means we did not emit that symbol.
204 DenseMap<const MCSymbol *, unsigned> SymbolOrdering;
206 /// This is stack of current and previous section values saved by
208 SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack;
210 /// The next unique ID to use when creating a WinCFI-related section (.pdata
211 /// or .xdata). This ID ensures that we have a one-to-one mapping from
212 /// code section to unwind info section, which MSVC's incremental linker
214 unsigned NextWinCFIID = 0;
216 bool UseAssemblerInfoForParsing;
219 MCStreamer(MCContext &Ctx);
221 virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
222 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
224 WinEH::FrameInfo *getCurrentWinFrameInfo() {
225 return CurrentWinFrameInfo;
228 virtual void EmitWindowsUnwindTables();
230 virtual void EmitRawTextImpl(StringRef String);
232 /// Returns true if the the .cv_loc directive is in the right section.
233 bool checkCVLocSection(unsigned FuncId, unsigned FileNo, SMLoc Loc);
236 MCStreamer(const MCStreamer &) = delete;
237 MCStreamer &operator=(const MCStreamer &) = delete;
238 virtual ~MCStreamer();
240 void visitUsedExpr(const MCExpr &Expr);
241 virtual void visitUsedSymbol(const MCSymbol &Sym);
243 void setTargetStreamer(MCTargetStreamer *TS) {
244 TargetStreamer.reset(TS);
249 virtual void reset();
251 MCContext &getContext() const { return Context; }
253 virtual MCAssembler *getAssemblerPtr() { return nullptr; }
255 void setUseAssemblerInfoForParsing(bool v) { UseAssemblerInfoForParsing = v; }
256 bool getUseAssemblerInfoForParsing() { return UseAssemblerInfoForParsing; }
258 MCTargetStreamer *getTargetStreamer() {
259 return TargetStreamer.get();
262 /// When emitting an object file, create and emit a real label. When emitting
263 /// textual assembly, this should do nothing to avoid polluting our output.
264 virtual MCSymbol *EmitCFILabel();
266 /// Retreive the current frame info if one is available and it is not yet
267 /// closed. Otherwise, issue an error and return null.
268 WinEH::FrameInfo *EnsureValidWinFrameInfo(SMLoc Loc);
270 unsigned getNumFrameInfos() { return DwarfFrameInfos.size(); }
271 ArrayRef<MCDwarfFrameInfo> getDwarfFrameInfos() const {
272 return DwarfFrameInfos;
275 bool hasUnfinishedDwarfFrameInfo();
277 unsigned getNumWinFrameInfos() { return WinFrameInfos.size(); }
278 ArrayRef<std::unique_ptr<WinEH::FrameInfo>> getWinFrameInfos() const {
279 return WinFrameInfos;
282 void generateCompactUnwindEncodings(MCAsmBackend *MAB);
284 /// \name Assembly File Formatting.
287 /// Return true if this streamer supports verbose assembly and if it is
289 virtual bool isVerboseAsm() const { return false; }
291 /// Return true if this asm streamer supports emitting unformatted text
292 /// to the .s file with EmitRawText.
293 virtual bool hasRawTextSupport() const { return false; }
295 /// Is the integrated assembler required for this streamer to function
297 virtual bool isIntegratedAssemblerRequired() const { return false; }
299 /// Add a textual comment.
301 /// Typically for comments that can be emitted to the generated .s
302 /// file if applicable as a QoI issue to make the output of the compiler
303 /// more readable. This only affects the MCAsmStreamer, and only when
304 /// verbose assembly output is enabled.
306 /// If the comment includes embedded \n's, they will each get the comment
307 /// prefix as appropriate. The added comment should not end with a \n.
308 /// By default, each comment is terminated with an end of line, i.e. the
309 /// EOL param is set to true by default. If one prefers not to end the
310 /// comment with a new line then the EOL param should be passed
311 /// with a false value.
312 virtual void AddComment(const Twine &T, bool EOL = true) {}
314 /// Return a raw_ostream that comments can be written to. Unlike
315 /// AddComment, you are required to terminate comments with \n if you use this
317 virtual raw_ostream &GetCommentOS();
319 /// Print T and prefix it with the comment string (normally #) and
320 /// optionally a tab. This prints the comment immediately, not at the end of
321 /// the current line. It is basically a safe version of EmitRawText: since it
322 /// only prints comments, the object streamer ignores it instead of asserting.
323 virtual void emitRawComment(const Twine &T, bool TabPrefix = true);
325 /// Add explicit comment T. T is required to be a valid
326 /// comment in the output and does not need to be escaped.
327 virtual void addExplicitComment(const Twine &T);
329 /// Emit added explicit comments.
330 virtual void emitExplicitComments();
332 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
333 virtual void AddBlankLine() {}
337 /// \name Symbol & Section Management
340 /// Return the current section that the streamer is emitting code to.
341 MCSectionSubPair getCurrentSection() const {
342 if (!SectionStack.empty())
343 return SectionStack.back().first;
344 return MCSectionSubPair();
346 MCSection *getCurrentSectionOnly() const { return getCurrentSection().first; }
348 /// Return the previous section that the streamer is emitting code to.
349 MCSectionSubPair getPreviousSection() const {
350 if (!SectionStack.empty())
351 return SectionStack.back().second;
352 return MCSectionSubPair();
355 /// Returns an index to represent the order a symbol was emitted in.
356 /// (zero if we did not emit that symbol)
357 unsigned GetSymbolOrder(const MCSymbol *Sym) const {
358 return SymbolOrdering.lookup(Sym);
361 /// Update streamer for a new active section.
363 /// This is called by PopSection and SwitchSection, if the current
365 virtual void ChangeSection(MCSection *, const MCExpr *);
367 /// Save the current and previous section on the section stack.
369 SectionStack.push_back(
370 std::make_pair(getCurrentSection(), getPreviousSection()));
373 /// Restore the current and previous section from the section stack.
374 /// Calls ChangeSection as needed.
376 /// Returns false if the stack was empty.
378 if (SectionStack.size() <= 1)
380 auto I = SectionStack.end();
382 MCSectionSubPair OldSection = I->first;
384 MCSectionSubPair NewSection = I->first;
386 if (OldSection != NewSection)
387 ChangeSection(NewSection.first, NewSection.second);
388 SectionStack.pop_back();
392 bool SubSection(const MCExpr *Subsection) {
393 if (SectionStack.empty())
396 SwitchSection(SectionStack.back().first.first, Subsection);
400 /// Set the current section where code is being emitted to \p Section. This
401 /// is required to update CurSection.
403 /// This corresponds to assembler directives like .section, .text, etc.
404 virtual void SwitchSection(MCSection *Section,
405 const MCExpr *Subsection = nullptr);
407 /// Set the current section where code is being emitted to \p Section.
408 /// This is required to update CurSection. This version does not call
410 void SwitchSectionNoChange(MCSection *Section,
411 const MCExpr *Subsection = nullptr) {
412 assert(Section && "Cannot switch to a null section!");
413 MCSectionSubPair curSection = SectionStack.back().first;
414 SectionStack.back().second = curSection;
415 if (MCSectionSubPair(Section, Subsection) != curSection)
416 SectionStack.back().first = MCSectionSubPair(Section, Subsection);
419 /// Create the default sections and set the initial one.
420 virtual void InitSections(bool NoExecStack);
422 MCSymbol *endSection(MCSection *Section);
424 /// Sets the symbol's section.
426 /// Each emitted symbol will be tracked in the ordering table,
427 /// so we can sort on them later.
428 void AssignFragment(MCSymbol *Symbol, MCFragment *Fragment);
430 /// Emit a label for \p Symbol into the current section.
432 /// This corresponds to an assembler statement such as:
435 /// \param Symbol - The symbol to emit. A given symbol should only be
436 /// emitted as a label once, and symbols emitted as a label should never be
437 /// used in an assignment.
438 // FIXME: These emission are non-const because we mutate the symbol to
439 // add the section we're emitting it to later.
440 virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc());
442 virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol);
444 /// Note in the output the specified \p Flag.
445 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
447 /// Emit the given list \p Options of strings as linker
448 /// options into the output.
449 virtual void EmitLinkerOptions(ArrayRef<std::string> Kind) {}
451 /// Note in the output the specified region \p Kind.
452 virtual void EmitDataRegion(MCDataRegionType Kind) {}
454 /// Specify the Mach-O minimum deployment target version.
455 virtual void EmitVersionMin(MCVersionMinType Type, unsigned Major,
456 unsigned Minor, unsigned Update,
457 VersionTuple SDKVersion) {}
459 /// Emit/Specify Mach-O build version command.
460 /// \p Platform should be one of MachO::PlatformType.
461 virtual void EmitBuildVersion(unsigned Platform, unsigned Major,
462 unsigned Minor, unsigned Update,
463 VersionTuple SDKVersion) {}
465 void EmitVersionForTarget(const Triple &Target,
466 const VersionTuple &SDKVersion);
468 /// Note in the output that the specified \p Func is a Thumb mode
469 /// function (ARM target only).
470 virtual void EmitThumbFunc(MCSymbol *Func);
472 /// Emit an assignment of \p Value to \p Symbol.
474 /// This corresponds to an assembler statement such as:
477 /// The assignment generates no code, but has the side effect of binding the
478 /// value in the current context. For the assembly streamer, this prints the
479 /// binding into the .s file.
481 /// \param Symbol - The symbol being assigned to.
482 /// \param Value - The value for the symbol.
483 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
485 /// Emit an weak reference from \p Alias to \p Symbol.
487 /// This corresponds to an assembler statement such as:
488 /// .weakref alias, symbol
490 /// \param Alias - The alias that is being created.
491 /// \param Symbol - The symbol being aliased.
492 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
494 /// Add the given \p Attribute to \p Symbol.
495 virtual bool EmitSymbolAttribute(MCSymbol *Symbol,
496 MCSymbolAttr Attribute) = 0;
498 /// Set the \p DescValue for the \p Symbol.
500 /// \param Symbol - The symbol to have its n_desc field set.
501 /// \param DescValue - The value to set into the n_desc field.
502 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
504 /// Start emitting COFF symbol definition
506 /// \param Symbol - The symbol to have its External & Type fields set.
507 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
509 /// Emit the storage class of the symbol.
511 /// \param StorageClass - The storage class the symbol should have.
512 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
514 /// Emit the type of the symbol.
516 /// \param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
517 virtual void EmitCOFFSymbolType(int Type);
519 /// Marks the end of the symbol definition.
520 virtual void EndCOFFSymbolDef();
522 virtual void EmitCOFFSafeSEH(MCSymbol const *Symbol);
524 /// Emits the symbol table index of a Symbol into the current section.
525 virtual void EmitCOFFSymbolIndex(MCSymbol const *Symbol);
527 /// Emits a COFF section index.
529 /// \param Symbol - Symbol the section number relocation should point to.
530 virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
532 /// Emits a COFF section relative relocation.
534 /// \param Symbol - Symbol the section relative relocation should point to.
535 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset);
537 /// Emits a COFF image relative relocation.
539 /// \param Symbol - Symbol the image relative relocation should point to.
540 virtual void EmitCOFFImgRel32(MCSymbol const *Symbol, int64_t Offset);
542 /// Emit an ELF .size directive.
544 /// This corresponds to an assembler statement such as:
545 /// .size symbol, expression
546 virtual void emitELFSize(MCSymbol *Symbol, const MCExpr *Value);
548 /// Emit an ELF .symver directive.
550 /// This corresponds to an assembler statement such as:
551 /// .symver _start, foo@@SOME_VERSION
552 /// \param AliasName - The versioned alias (i.e. "foo@@SOME_VERSION")
553 /// \param Aliasee - The aliased symbol (i.e. "_start")
554 virtual void emitELFSymverDirective(StringRef AliasName,
555 const MCSymbol *Aliasee);
557 /// Emit a Linker Optimization Hint (LOH) directive.
558 /// \param Args - Arguments of the LOH.
559 virtual void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {}
561 /// Emit a common symbol.
563 /// \param Symbol - The common symbol to emit.
564 /// \param Size - The size of the common symbol.
565 /// \param ByteAlignment - The alignment of the symbol if
566 /// non-zero. This must be a power of 2.
567 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
568 unsigned ByteAlignment) = 0;
570 /// Emit a local common (.lcomm) symbol.
572 /// \param Symbol - The common symbol to emit.
573 /// \param Size - The size of the common symbol.
574 /// \param ByteAlignment - The alignment of the common symbol in bytes.
575 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
576 unsigned ByteAlignment);
578 /// Emit the zerofill section and an optional symbol.
580 /// \param Section - The zerofill section to create and or to put the symbol
581 /// \param Symbol - The zerofill symbol to emit, if non-NULL.
582 /// \param Size - The size of the zerofill symbol.
583 /// \param ByteAlignment - The alignment of the zerofill symbol if
584 /// non-zero. This must be a power of 2 on some targets.
585 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
586 uint64_t Size = 0, unsigned ByteAlignment = 0,
587 SMLoc Loc = SMLoc()) = 0;
589 /// Emit a thread local bss (.tbss) symbol.
591 /// \param Section - The thread local common section.
592 /// \param Symbol - The thread local common symbol to emit.
593 /// \param Size - The size of the symbol.
594 /// \param ByteAlignment - The alignment of the thread local common symbol
595 /// if non-zero. This must be a power of 2 on some targets.
596 virtual void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
597 uint64_t Size, unsigned ByteAlignment = 0);
600 /// \name Generating Data
603 /// Emit the bytes in \p Data into the output.
605 /// This is used to implement assembler directives such as .byte, .ascii,
607 virtual void EmitBytes(StringRef Data);
609 /// Functionally identical to EmitBytes. When emitting textual assembly, this
610 /// method uses .byte directives instead of .ascii or .asciz for readability.
611 virtual void EmitBinaryData(StringRef Data);
613 /// Emit the expression \p Value into the output as a native
614 /// integer of the given \p Size bytes.
616 /// This is used to implement assembler directives such as .word, .quad,
619 /// \param Value - The value to emit.
620 /// \param Size - The size of the integer (in bytes) to emit. This must
621 /// match a native machine width.
622 /// \param Loc - The location of the expression for error reporting.
623 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
624 SMLoc Loc = SMLoc());
626 void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc = SMLoc());
628 /// Special case of EmitValue that avoids the client having
629 /// to pass in a MCExpr for constant integers.
630 virtual void EmitIntValue(uint64_t Value, unsigned Size);
632 virtual void EmitULEB128Value(const MCExpr *Value);
634 virtual void EmitSLEB128Value(const MCExpr *Value);
636 /// Special case of EmitULEB128Value that avoids the client having to
637 /// pass in a MCExpr for constant integers.
638 void EmitULEB128IntValue(uint64_t Value);
640 /// Special case of EmitSLEB128Value that avoids the client having to
641 /// pass in a MCExpr for constant integers.
642 void EmitSLEB128IntValue(int64_t Value);
644 /// Special case of EmitValue that avoids the client having to pass in
645 /// a MCExpr for MCSymbols.
646 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
647 bool IsSectionRelative = false);
649 /// Emit the expression \p Value into the output as a dtprel
650 /// (64-bit DTP relative) value.
652 /// This is used to implement assembler directives such as .dtpreldword on
653 /// targets that support them.
654 virtual void EmitDTPRel64Value(const MCExpr *Value);
656 /// Emit the expression \p Value into the output as a dtprel
657 /// (32-bit DTP relative) value.
659 /// This is used to implement assembler directives such as .dtprelword on
660 /// targets that support them.
661 virtual void EmitDTPRel32Value(const MCExpr *Value);
663 /// Emit the expression \p Value into the output as a tprel
664 /// (64-bit TP relative) value.
666 /// This is used to implement assembler directives such as .tpreldword on
667 /// targets that support them.
668 virtual void EmitTPRel64Value(const MCExpr *Value);
670 /// Emit the expression \p Value into the output as a tprel
671 /// (32-bit TP relative) value.
673 /// This is used to implement assembler directives such as .tprelword on
674 /// targets that support them.
675 virtual void EmitTPRel32Value(const MCExpr *Value);
677 /// Emit the expression \p Value into the output as a gprel64 (64-bit
678 /// GP relative) value.
680 /// This is used to implement assembler directives such as .gpdword on
681 /// targets that support them.
682 virtual void EmitGPRel64Value(const MCExpr *Value);
684 /// Emit the expression \p Value into the output as a gprel32 (32-bit
685 /// GP relative) value.
687 /// This is used to implement assembler directives such as .gprel32 on
688 /// targets that support them.
689 virtual void EmitGPRel32Value(const MCExpr *Value);
691 /// Emit NumBytes bytes worth of the value specified by FillValue.
692 /// This implements directives such as '.space'.
693 void emitFill(uint64_t NumBytes, uint8_t FillValue);
695 /// Emit \p Size bytes worth of the value specified by \p FillValue.
697 /// This is used to implement assembler directives such as .space or .skip.
699 /// \param NumBytes - The number of bytes to emit.
700 /// \param FillValue - The value to use when filling bytes.
701 /// \param Loc - The location of the expression for error reporting.
702 virtual void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
703 SMLoc Loc = SMLoc());
705 /// Emit \p NumValues copies of \p Size bytes. Each \p Size bytes is
706 /// taken from the lowest order 4 bytes of \p Expr expression.
708 /// This is used to implement assembler directives such as .fill.
710 /// \param NumValues - The number of copies of \p Size bytes to emit.
711 /// \param Size - The size (in bytes) of each repeated value.
712 /// \param Expr - The expression from which \p Size bytes are used.
713 virtual void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
714 SMLoc Loc = SMLoc());
716 /// Emit NumBytes worth of zeros.
717 /// This function properly handles data in virtual sections.
718 void EmitZeros(uint64_t NumBytes);
720 /// Emit some number of copies of \p Value until the byte alignment \p
721 /// ByteAlignment is reached.
723 /// If the number of bytes need to emit for the alignment is not a multiple
724 /// of \p ValueSize, then the contents of the emitted fill bytes is
727 /// This used to implement the .align assembler directive.
729 /// \param ByteAlignment - The alignment to reach. This must be a power of
730 /// two on some targets.
731 /// \param Value - The value to use when filling bytes.
732 /// \param ValueSize - The size of the integer (in bytes) to emit for
733 /// \p Value. This must match a native machine width.
734 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
735 /// the alignment cannot be reached in this many bytes, no bytes are
737 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
738 unsigned ValueSize = 1,
739 unsigned MaxBytesToEmit = 0);
741 /// Emit nops until the byte alignment \p ByteAlignment is reached.
743 /// This used to align code where the alignment bytes may be executed. This
744 /// can emit different bytes for different sizes to optimize execution.
746 /// \param ByteAlignment - The alignment to reach. This must be a power of
747 /// two on some targets.
748 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
749 /// the alignment cannot be reached in this many bytes, no bytes are
751 virtual void EmitCodeAlignment(unsigned ByteAlignment,
752 unsigned MaxBytesToEmit = 0);
754 /// Emit some number of copies of \p Value until the byte offset \p
755 /// Offset is reached.
757 /// This is used to implement assembler directives such as .org.
759 /// \param Offset - The offset to reach. This may be an expression, but the
760 /// expression must be associated with the current section.
761 /// \param Value - The value to use when filling bytes.
762 virtual void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
766 EmitCodePaddingBasicBlockStart(const MCCodePaddingContext &Context) {}
769 EmitCodePaddingBasicBlockEnd(const MCCodePaddingContext &Context) {}
773 /// Switch to a new logical file. This is used to implement the '.file
774 /// "foo.c"' assembler directive.
775 virtual void EmitFileDirective(StringRef Filename);
777 /// Emit the "identifiers" directive. This implements the
778 /// '.ident "version foo"' assembler directive.
779 virtual void EmitIdent(StringRef IdentString) {}
781 /// Associate a filename with a specified logical file number. This
782 /// implements the DWARF2 '.file 4 "foo.c"' assembler directive.
783 unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
785 MD5::MD5Result *Checksum = nullptr,
786 Optional<StringRef> Source = None,
789 tryEmitDwarfFileDirective(FileNo, Directory, Filename, Checksum,
793 /// Associate a filename with a specified logical file number.
794 /// Also associate a directory, optional checksum, and optional source
795 /// text with the logical file. This implements the DWARF2
796 /// '.file 4 "dir/foo.c"' assembler directive, and the DWARF5
797 /// '.file 4 "dir/foo.c" md5 "..." source "..."' assembler directive.
798 virtual Expected<unsigned> tryEmitDwarfFileDirective(
799 unsigned FileNo, StringRef Directory, StringRef Filename,
800 MD5::MD5Result *Checksum = nullptr, Optional<StringRef> Source = None,
803 /// Specify the "root" file of the compilation, using the ".file 0" extension.
804 virtual void emitDwarfFile0Directive(StringRef Directory, StringRef Filename,
805 MD5::MD5Result *Checksum,
806 Optional<StringRef> Source,
809 virtual void EmitCFIBKeyFrame();
811 /// This implements the DWARF2 '.loc fileno lineno ...' assembler
813 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
814 unsigned Column, unsigned Flags,
815 unsigned Isa, unsigned Discriminator,
818 /// Associate a filename with a specified logical file number, and also
819 /// specify that file's checksum information. This implements the '.cv_file 4
820 /// "foo.c"' assembler directive. Returns true on success.
821 virtual bool EmitCVFileDirective(unsigned FileNo, StringRef Filename,
822 ArrayRef<uint8_t> Checksum,
823 unsigned ChecksumKind);
825 /// Introduces a function id for use with .cv_loc.
826 virtual bool EmitCVFuncIdDirective(unsigned FunctionId);
828 /// Introduces an inline call site id for use with .cv_loc. Includes
829 /// extra information for inline line table generation.
830 virtual bool EmitCVInlineSiteIdDirective(unsigned FunctionId, unsigned IAFunc,
831 unsigned IAFile, unsigned IALine,
832 unsigned IACol, SMLoc Loc);
834 /// This implements the CodeView '.cv_loc' assembler directive.
835 virtual void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo,
836 unsigned Line, unsigned Column,
837 bool PrologueEnd, bool IsStmt,
838 StringRef FileName, SMLoc Loc);
840 /// This implements the CodeView '.cv_linetable' assembler directive.
841 virtual void EmitCVLinetableDirective(unsigned FunctionId,
842 const MCSymbol *FnStart,
843 const MCSymbol *FnEnd);
845 /// This implements the CodeView '.cv_inline_linetable' assembler
847 virtual void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
848 unsigned SourceFileId,
849 unsigned SourceLineNum,
850 const MCSymbol *FnStartSym,
851 const MCSymbol *FnEndSym);
853 /// This implements the CodeView '.cv_def_range' assembler
855 virtual void EmitCVDefRangeDirective(
856 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
857 StringRef FixedSizePortion);
859 /// This implements the CodeView '.cv_stringtable' assembler directive.
860 virtual void EmitCVStringTableDirective() {}
862 /// This implements the CodeView '.cv_filechecksums' assembler directive.
863 virtual void EmitCVFileChecksumsDirective() {}
865 /// This implements the CodeView '.cv_filechecksumoffset' assembler
867 virtual void EmitCVFileChecksumOffsetDirective(unsigned FileNo) {}
869 /// This implements the CodeView '.cv_fpo_data' assembler directive.
870 virtual void EmitCVFPOData(const MCSymbol *ProcSym, SMLoc Loc = {}) {}
872 /// Emit the absolute difference between two symbols.
874 /// \pre Offset of \c Hi is greater than the offset \c Lo.
875 virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
878 /// Emit the absolute difference between two symbols encoded with ULEB128.
879 virtual void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi,
882 virtual MCSymbol *getDwarfLineTableSymbol(unsigned CUID);
883 virtual void EmitCFISections(bool EH, bool Debug);
884 void EmitCFIStartProc(bool IsSimple, SMLoc Loc = SMLoc());
885 void EmitCFIEndProc();
886 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
887 virtual void EmitCFIDefCfaOffset(int64_t Offset);
888 virtual void EmitCFIDefCfaRegister(int64_t Register);
889 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
890 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
891 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
892 virtual void EmitCFIRememberState();
893 virtual void EmitCFIRestoreState();
894 virtual void EmitCFISameValue(int64_t Register);
895 virtual void EmitCFIRestore(int64_t Register);
896 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
897 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
898 virtual void EmitCFIEscape(StringRef Values);
899 virtual void EmitCFIReturnColumn(int64_t Register);
900 virtual void EmitCFIGnuArgsSize(int64_t Size);
901 virtual void EmitCFISignalFrame();
902 virtual void EmitCFIUndefined(int64_t Register);
903 virtual void EmitCFIRegister(int64_t Register1, int64_t Register2);
904 virtual void EmitCFIWindowSave();
905 virtual void EmitCFINegateRAState();
907 virtual void EmitWinCFIStartProc(const MCSymbol *Symbol, SMLoc Loc = SMLoc());
908 virtual void EmitWinCFIEndProc(SMLoc Loc = SMLoc());
909 /// This is used on platforms, such as Windows on ARM64, that require function
910 /// or funclet sizes to be emitted in .xdata before the End marker is emitted
911 /// for the frame. We cannot use the End marker, as it is not set at the
912 /// point of emitting .xdata, in order to indicate that the frame is active.
913 virtual void EmitWinCFIFuncletOrFuncEnd(SMLoc Loc = SMLoc());
914 virtual void EmitWinCFIStartChained(SMLoc Loc = SMLoc());
915 virtual void EmitWinCFIEndChained(SMLoc Loc = SMLoc());
916 virtual void EmitWinCFIPushReg(unsigned Register, SMLoc Loc = SMLoc());
917 virtual void EmitWinCFISetFrame(unsigned Register, unsigned Offset,
918 SMLoc Loc = SMLoc());
919 virtual void EmitWinCFIAllocStack(unsigned Size, SMLoc Loc = SMLoc());
920 virtual void EmitWinCFISaveReg(unsigned Register, unsigned Offset,
921 SMLoc Loc = SMLoc());
922 virtual void EmitWinCFISaveXMM(unsigned Register, unsigned Offset,
923 SMLoc Loc = SMLoc());
924 virtual void EmitWinCFIPushFrame(bool Code, SMLoc Loc = SMLoc());
925 virtual void EmitWinCFIEndProlog(SMLoc Loc = SMLoc());
926 virtual void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except,
927 SMLoc Loc = SMLoc());
928 virtual void EmitWinEHHandlerData(SMLoc Loc = SMLoc());
930 virtual void emitCGProfileEntry(const MCSymbolRefExpr *From,
931 const MCSymbolRefExpr *To, uint64_t Count);
933 /// Get the .pdata section used for the given section. Typically the given
934 /// section is either the main .text section or some other COMDAT .text
935 /// section, but it may be any section containing code.
936 MCSection *getAssociatedPDataSection(const MCSection *TextSec);
938 /// Get the .xdata section used for the given section.
939 MCSection *getAssociatedXDataSection(const MCSection *TextSec);
941 virtual void EmitSyntaxDirective();
943 /// Emit a .reloc directive.
944 /// Returns true if the relocation could not be emitted because Name is not
946 virtual bool EmitRelocDirective(const MCExpr &Offset, StringRef Name,
947 const MCExpr *Expr, SMLoc Loc,
948 const MCSubtargetInfo &STI) {
952 virtual void EmitAddrsig() {}
953 virtual void EmitAddrsigSym(const MCSymbol *Sym) {}
955 /// Emit the given \p Instruction into the current section.
956 /// PrintSchedInfo == true then schedul comment should be added to output
957 virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
958 bool PrintSchedInfo = false);
960 /// Set the bundle alignment mode from now on in the section.
961 /// The argument is the power of 2 to which the alignment is set. The
962 /// value 0 means turn the bundle alignment off.
963 virtual void EmitBundleAlignMode(unsigned AlignPow2);
965 /// The following instructions are a bundle-locked group.
967 /// \param AlignToEnd - If true, the bundle-locked group will be aligned to
968 /// the end of a bundle.
969 virtual void EmitBundleLock(bool AlignToEnd);
971 /// Ends a bundle-locked group.
972 virtual void EmitBundleUnlock();
974 /// If this file is backed by a assembly streamer, this dumps the
975 /// specified string in the output .s file. This capability is indicated by
976 /// the hasRawTextSupport() predicate. By default this aborts.
977 void EmitRawText(const Twine &String);
979 /// Streamer specific finalization.
980 virtual void FinishImpl();
981 /// Finish emission of machine code.
984 virtual bool mayHaveInstructions(MCSection &Sec) const { return true; }
987 /// Create a dummy machine code streamer, which does nothing. This is useful for
988 /// timing the assembler front end.
989 MCStreamer *createNullStreamer(MCContext &Ctx);
991 /// Create a machine code streamer which will print out assembly for the native
992 /// target, suitable for compiling with a native assembler.
994 /// \param InstPrint - If given, the instruction printer to use. If not given
995 /// the MCInst representation will be printed. This method takes ownership of
998 /// \param CE - If given, a code emitter to use to show the instruction
999 /// encoding inline with the assembly. This method takes ownership of \p CE.
1001 /// \param TAB - If given, a target asm backend to use to show the fixup
1002 /// information in conjunction with encoding information. This method takes
1003 /// ownership of \p TAB.
1005 /// \param ShowInst - Whether to show the MCInst representation inline with
1007 MCStreamer *createAsmStreamer(MCContext &Ctx,
1008 std::unique_ptr<formatted_raw_ostream> OS,
1009 bool isVerboseAsm, bool useDwarfDirectory,
1010 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1011 MCAsmBackend *TAB, bool ShowInst);
1013 } // end namespace llvm
1015 #endif // LLVM_MC_MCSTREAMER_H