1 //===- MCStreamer.h - High-level Streaming Machine Code Output --*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file declares the MCStreamer class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_MC_MCSTREAMER_H
14 #define LLVM_MC_MCSTREAMER_H
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/MC/MCDirectives.h"
22 #include "llvm/MC/MCLinkerOptimizationHint.h"
23 #include "llvm/MC/MCSymbol.h"
24 #include "llvm/MC/MCWinEH.h"
25 #include "llvm/Support/Error.h"
26 #include "llvm/Support/MD5.h"
27 #include "llvm/Support/SMLoc.h"
28 #include "llvm/Support/TargetParser.h"
29 #include "llvm/Support/VersionTuple.h"
39 class AssemblerConstantPools
;
40 class formatted_raw_ostream
;
43 struct MCCodePaddingContext
;
45 struct MCDwarfFrameInfo
;
52 class MCSymbolRefExpr
;
53 class MCSubtargetInfo
;
58 struct DefRangeRegisterRelHeader
;
59 struct DefRangeSubfieldRegisterHeader
;
60 struct DefRangeRegisterHeader
;
61 struct DefRangeFramePointerRelHeader
;
64 using MCSectionSubPair
= std::pair
<MCSection
*, const MCExpr
*>;
66 /// Target specific streamer interface. This is used so that targets can
67 /// implement support for target specific assembly directives.
69 /// If target foo wants to use this, it should implement 3 classes:
70 /// * FooTargetStreamer : public MCTargetStreamer
71 /// * FooTargetAsmStreamer : public FooTargetStreamer
72 /// * FooTargetELFStreamer : public FooTargetStreamer
74 /// FooTargetStreamer should have a pure virtual method for each directive. For
75 /// example, for a ".bar symbol_name" directive, it should have
76 /// virtual emitBar(const MCSymbol &Symbol) = 0;
78 /// The FooTargetAsmStreamer and FooTargetELFStreamer classes implement the
79 /// method. The assembly streamer just prints ".bar symbol_name". The object
80 /// streamer does whatever is needed to implement .bar in the object file.
82 /// In the assembly printer and parser the target streamer can be used by
83 /// calling getTargetStreamer and casting it to FooTargetStreamer:
85 /// MCTargetStreamer &TS = OutStreamer.getTargetStreamer();
86 /// FooTargetStreamer &ATS = static_cast<FooTargetStreamer &>(TS);
88 /// The base classes FooTargetAsmStreamer and FooTargetELFStreamer should
89 /// *never* be treated differently. Callers should always talk to a
90 /// FooTargetStreamer.
91 class MCTargetStreamer
{
96 MCTargetStreamer(MCStreamer
&S
);
97 virtual ~MCTargetStreamer();
99 MCStreamer
&getStreamer() { return Streamer
; }
101 // Allow a target to add behavior to the EmitLabel of MCStreamer.
102 virtual void emitLabel(MCSymbol
*Symbol
);
103 // Allow a target to add behavior to the emitAssignment of MCStreamer.
104 virtual void emitAssignment(MCSymbol
*Symbol
, const MCExpr
*Value
);
106 virtual void prettyPrintAsm(MCInstPrinter
&InstPrinter
, raw_ostream
&OS
,
107 const MCInst
&Inst
, const MCSubtargetInfo
&STI
);
109 virtual void emitDwarfFileDirective(StringRef Directive
);
111 /// Update streamer for a new active section.
113 /// This is called by PopSection and SwitchSection, if the current
115 virtual void changeSection(const MCSection
*CurSection
, MCSection
*Section
,
116 const MCExpr
*SubSection
, raw_ostream
&OS
);
118 virtual void emitValue(const MCExpr
*Value
);
120 /// Emit the bytes in \p Data into the output.
122 /// This is used to emit bytes in \p Data as sequence of .byte directives.
123 virtual void emitRawBytes(StringRef Data
);
125 virtual void finish();
128 // FIXME: declared here because it is used from
129 // lib/CodeGen/AsmPrinter/ARMException.cpp.
130 class ARMTargetStreamer
: public MCTargetStreamer
{
132 ARMTargetStreamer(MCStreamer
&S
);
133 ~ARMTargetStreamer() override
;
135 virtual void emitFnStart();
136 virtual void emitFnEnd();
137 virtual void emitCantUnwind();
138 virtual void emitPersonality(const MCSymbol
*Personality
);
139 virtual void emitPersonalityIndex(unsigned Index
);
140 virtual void emitHandlerData();
141 virtual void emitSetFP(unsigned FpReg
, unsigned SpReg
,
143 virtual void emitMovSP(unsigned Reg
, int64_t Offset
= 0);
144 virtual void emitPad(int64_t Offset
);
145 virtual void emitRegSave(const SmallVectorImpl
<unsigned> &RegList
,
147 virtual void emitUnwindRaw(int64_t StackOffset
,
148 const SmallVectorImpl
<uint8_t> &Opcodes
);
150 virtual void switchVendor(StringRef Vendor
);
151 virtual void emitAttribute(unsigned Attribute
, unsigned Value
);
152 virtual void emitTextAttribute(unsigned Attribute
, StringRef String
);
153 virtual void emitIntTextAttribute(unsigned Attribute
, unsigned IntValue
,
154 StringRef StringValue
= "");
155 virtual void emitFPU(unsigned FPU
);
156 virtual void emitArch(ARM::ArchKind Arch
);
157 virtual void emitArchExtension(unsigned ArchExt
);
158 virtual void emitObjectArch(ARM::ArchKind Arch
);
159 void emitTargetAttributes(const MCSubtargetInfo
&STI
);
160 virtual void finishAttributeSection();
161 virtual void emitInst(uint32_t Inst
, char Suffix
= '\0');
163 virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr
*SRE
);
165 virtual void emitThumbSet(MCSymbol
*Symbol
, const MCExpr
*Value
);
167 void finish() override
;
169 /// Reset any state between object emissions, i.e. the equivalent of
170 /// MCStreamer's reset method.
171 virtual void reset();
173 /// Callback used to implement the ldr= pseudo.
174 /// Add a new entry to the constant pool for the current section and return an
175 /// MCExpr that can be used to refer to the constant pool location.
176 const MCExpr
*addConstantPoolEntry(const MCExpr
*, SMLoc Loc
);
178 /// Callback used to implemnt the .ltorg directive.
179 /// Emit contents of constant pool for the current section.
180 void emitCurrentConstantPool();
183 std::unique_ptr
<AssemblerConstantPools
> ConstantPools
;
186 /// Streaming machine code generation interface.
188 /// This interface is intended to provide a programatic interface that is very
189 /// similar to the level that an assembler .s file provides. It has callbacks
190 /// to emit bytes, handle directives, etc. The implementation of this interface
191 /// retains state to know what the current section is etc.
193 /// There are multiple implementations of this interface: one for writing out
194 /// a .s file, and implementations that write out .o files of various formats.
198 std::unique_ptr
<MCTargetStreamer
> TargetStreamer
;
200 std::vector
<MCDwarfFrameInfo
> DwarfFrameInfos
;
201 MCDwarfFrameInfo
*getCurrentDwarfFrameInfo();
203 /// Similar to DwarfFrameInfos, but for SEH unwind info. Chained frames may
204 /// refer to each other, so use std::unique_ptr to provide pointer stability.
205 std::vector
<std::unique_ptr
<WinEH::FrameInfo
>> WinFrameInfos
;
207 WinEH::FrameInfo
*CurrentWinFrameInfo
;
209 /// Tracks an index to represent the order a symbol was emitted in.
210 /// Zero means we did not emit that symbol.
211 DenseMap
<const MCSymbol
*, unsigned> SymbolOrdering
;
213 /// This is stack of current and previous section values saved by
215 SmallVector
<std::pair
<MCSectionSubPair
, MCSectionSubPair
>, 4> SectionStack
;
217 /// The next unique ID to use when creating a WinCFI-related section (.pdata
218 /// or .xdata). This ID ensures that we have a one-to-one mapping from
219 /// code section to unwind info section, which MSVC's incremental linker
221 unsigned NextWinCFIID
= 0;
223 bool UseAssemblerInfoForParsing
;
226 MCStreamer(MCContext
&Ctx
);
228 virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo
&Frame
);
229 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo
&CurFrame
);
231 WinEH::FrameInfo
*getCurrentWinFrameInfo() {
232 return CurrentWinFrameInfo
;
235 virtual void EmitWindowsUnwindTables();
237 virtual void EmitRawTextImpl(StringRef String
);
239 /// Returns true if the the .cv_loc directive is in the right section.
240 bool checkCVLocSection(unsigned FuncId
, unsigned FileNo
, SMLoc Loc
);
243 MCStreamer(const MCStreamer
&) = delete;
244 MCStreamer
&operator=(const MCStreamer
&) = delete;
245 virtual ~MCStreamer();
247 void visitUsedExpr(const MCExpr
&Expr
);
248 virtual void visitUsedSymbol(const MCSymbol
&Sym
);
250 void setTargetStreamer(MCTargetStreamer
*TS
) {
251 TargetStreamer
.reset(TS
);
256 virtual void reset();
258 MCContext
&getContext() const { return Context
; }
260 virtual MCAssembler
*getAssemblerPtr() { return nullptr; }
262 void setUseAssemblerInfoForParsing(bool v
) { UseAssemblerInfoForParsing
= v
; }
263 bool getUseAssemblerInfoForParsing() { return UseAssemblerInfoForParsing
; }
265 MCTargetStreamer
*getTargetStreamer() {
266 return TargetStreamer
.get();
269 /// When emitting an object file, create and emit a real label. When emitting
270 /// textual assembly, this should do nothing to avoid polluting our output.
271 virtual MCSymbol
*EmitCFILabel();
273 /// Retreive the current frame info if one is available and it is not yet
274 /// closed. Otherwise, issue an error and return null.
275 WinEH::FrameInfo
*EnsureValidWinFrameInfo(SMLoc Loc
);
277 unsigned getNumFrameInfos();
278 ArrayRef
<MCDwarfFrameInfo
> getDwarfFrameInfos() const;
280 bool hasUnfinishedDwarfFrameInfo();
282 unsigned getNumWinFrameInfos() { return WinFrameInfos
.size(); }
283 ArrayRef
<std::unique_ptr
<WinEH::FrameInfo
>> getWinFrameInfos() const {
284 return WinFrameInfos
;
287 void generateCompactUnwindEncodings(MCAsmBackend
*MAB
);
289 /// \name Assembly File Formatting.
292 /// Return true if this streamer supports verbose assembly and if it is
294 virtual bool isVerboseAsm() const { return false; }
296 /// Return true if this asm streamer supports emitting unformatted text
297 /// to the .s file with EmitRawText.
298 virtual bool hasRawTextSupport() const { return false; }
300 /// Is the integrated assembler required for this streamer to function
302 virtual bool isIntegratedAssemblerRequired() const { return false; }
304 /// Add a textual comment.
306 /// Typically for comments that can be emitted to the generated .s
307 /// file if applicable as a QoI issue to make the output of the compiler
308 /// more readable. This only affects the MCAsmStreamer, and only when
309 /// verbose assembly output is enabled.
311 /// If the comment includes embedded \n's, they will each get the comment
312 /// prefix as appropriate. The added comment should not end with a \n.
313 /// By default, each comment is terminated with an end of line, i.e. the
314 /// EOL param is set to true by default. If one prefers not to end the
315 /// comment with a new line then the EOL param should be passed
316 /// with a false value.
317 virtual void AddComment(const Twine
&T
, bool EOL
= true) {}
319 /// Return a raw_ostream that comments can be written to. Unlike
320 /// AddComment, you are required to terminate comments with \n if you use this
322 virtual raw_ostream
&GetCommentOS();
324 /// Print T and prefix it with the comment string (normally #) and
325 /// optionally a tab. This prints the comment immediately, not at the end of
326 /// the current line. It is basically a safe version of EmitRawText: since it
327 /// only prints comments, the object streamer ignores it instead of asserting.
328 virtual void emitRawComment(const Twine
&T
, bool TabPrefix
= true);
330 /// Add explicit comment T. T is required to be a valid
331 /// comment in the output and does not need to be escaped.
332 virtual void addExplicitComment(const Twine
&T
);
334 /// Emit added explicit comments.
335 virtual void emitExplicitComments();
337 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
338 virtual void AddBlankLine() {}
342 /// \name Symbol & Section Management
345 /// Return the current section that the streamer is emitting code to.
346 MCSectionSubPair
getCurrentSection() const {
347 if (!SectionStack
.empty())
348 return SectionStack
.back().first
;
349 return MCSectionSubPair();
351 MCSection
*getCurrentSectionOnly() const { return getCurrentSection().first
; }
353 /// Return the previous section that the streamer is emitting code to.
354 MCSectionSubPair
getPreviousSection() const {
355 if (!SectionStack
.empty())
356 return SectionStack
.back().second
;
357 return MCSectionSubPair();
360 /// Returns an index to represent the order a symbol was emitted in.
361 /// (zero if we did not emit that symbol)
362 unsigned GetSymbolOrder(const MCSymbol
*Sym
) const {
363 return SymbolOrdering
.lookup(Sym
);
366 /// Update streamer for a new active section.
368 /// This is called by PopSection and SwitchSection, if the current
370 virtual void ChangeSection(MCSection
*, const MCExpr
*);
372 /// Save the current and previous section on the section stack.
374 SectionStack
.push_back(
375 std::make_pair(getCurrentSection(), getPreviousSection()));
378 /// Restore the current and previous section from the section stack.
379 /// Calls ChangeSection as needed.
381 /// Returns false if the stack was empty.
383 if (SectionStack
.size() <= 1)
385 auto I
= SectionStack
.end();
387 MCSectionSubPair OldSection
= I
->first
;
389 MCSectionSubPair NewSection
= I
->first
;
391 if (OldSection
!= NewSection
)
392 ChangeSection(NewSection
.first
, NewSection
.second
);
393 SectionStack
.pop_back();
397 bool SubSection(const MCExpr
*Subsection
) {
398 if (SectionStack
.empty())
401 SwitchSection(SectionStack
.back().first
.first
, Subsection
);
405 /// Set the current section where code is being emitted to \p Section. This
406 /// is required to update CurSection.
408 /// This corresponds to assembler directives like .section, .text, etc.
409 virtual void SwitchSection(MCSection
*Section
,
410 const MCExpr
*Subsection
= nullptr);
412 /// Set the current section where code is being emitted to \p Section.
413 /// This is required to update CurSection. This version does not call
415 void SwitchSectionNoChange(MCSection
*Section
,
416 const MCExpr
*Subsection
= nullptr) {
417 assert(Section
&& "Cannot switch to a null section!");
418 MCSectionSubPair curSection
= SectionStack
.back().first
;
419 SectionStack
.back().second
= curSection
;
420 if (MCSectionSubPair(Section
, Subsection
) != curSection
)
421 SectionStack
.back().first
= MCSectionSubPair(Section
, Subsection
);
424 /// Create the default sections and set the initial one.
425 virtual void InitSections(bool NoExecStack
);
427 MCSymbol
*endSection(MCSection
*Section
);
429 /// Sets the symbol's section.
431 /// Each emitted symbol will be tracked in the ordering table,
432 /// so we can sort on them later.
433 void AssignFragment(MCSymbol
*Symbol
, MCFragment
*Fragment
);
435 /// Emit a label for \p Symbol into the current section.
437 /// This corresponds to an assembler statement such as:
440 /// \param Symbol - The symbol to emit. A given symbol should only be
441 /// emitted as a label once, and symbols emitted as a label should never be
442 /// used in an assignment.
443 // FIXME: These emission are non-const because we mutate the symbol to
444 // add the section we're emitting it to later.
445 virtual void EmitLabel(MCSymbol
*Symbol
, SMLoc Loc
= SMLoc());
447 virtual void EmitEHSymAttributes(const MCSymbol
*Symbol
, MCSymbol
*EHSymbol
);
449 /// Note in the output the specified \p Flag.
450 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag
);
452 /// Emit the given list \p Options of strings as linker
453 /// options into the output.
454 virtual void EmitLinkerOptions(ArrayRef
<std::string
> Kind
) {}
456 /// Note in the output the specified region \p Kind.
457 virtual void EmitDataRegion(MCDataRegionType Kind
) {}
459 /// Specify the Mach-O minimum deployment target version.
460 virtual void EmitVersionMin(MCVersionMinType Type
, unsigned Major
,
461 unsigned Minor
, unsigned Update
,
462 VersionTuple SDKVersion
) {}
464 /// Emit/Specify Mach-O build version command.
465 /// \p Platform should be one of MachO::PlatformType.
466 virtual void EmitBuildVersion(unsigned Platform
, unsigned Major
,
467 unsigned Minor
, unsigned Update
,
468 VersionTuple SDKVersion
) {}
470 void EmitVersionForTarget(const Triple
&Target
,
471 const VersionTuple
&SDKVersion
);
473 /// Note in the output that the specified \p Func is a Thumb mode
474 /// function (ARM target only).
475 virtual void EmitThumbFunc(MCSymbol
*Func
);
477 /// Emit an assignment of \p Value to \p Symbol.
479 /// This corresponds to an assembler statement such as:
482 /// The assignment generates no code, but has the side effect of binding the
483 /// value in the current context. For the assembly streamer, this prints the
484 /// binding into the .s file.
486 /// \param Symbol - The symbol being assigned to.
487 /// \param Value - The value for the symbol.
488 virtual void EmitAssignment(MCSymbol
*Symbol
, const MCExpr
*Value
);
490 /// Emit an weak reference from \p Alias to \p Symbol.
492 /// This corresponds to an assembler statement such as:
493 /// .weakref alias, symbol
495 /// \param Alias - The alias that is being created.
496 /// \param Symbol - The symbol being aliased.
497 virtual void EmitWeakReference(MCSymbol
*Alias
, const MCSymbol
*Symbol
);
499 /// Add the given \p Attribute to \p Symbol.
500 virtual bool EmitSymbolAttribute(MCSymbol
*Symbol
,
501 MCSymbolAttr Attribute
) = 0;
503 /// Set the \p DescValue for the \p Symbol.
505 /// \param Symbol - The symbol to have its n_desc field set.
506 /// \param DescValue - The value to set into the n_desc field.
507 virtual void EmitSymbolDesc(MCSymbol
*Symbol
, unsigned DescValue
);
509 /// Start emitting COFF symbol definition
511 /// \param Symbol - The symbol to have its External & Type fields set.
512 virtual void BeginCOFFSymbolDef(const MCSymbol
*Symbol
);
514 /// Emit the storage class of the symbol.
516 /// \param StorageClass - The storage class the symbol should have.
517 virtual void EmitCOFFSymbolStorageClass(int StorageClass
);
519 /// Emit the type of the symbol.
521 /// \param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
522 virtual void EmitCOFFSymbolType(int Type
);
524 /// Marks the end of the symbol definition.
525 virtual void EndCOFFSymbolDef();
527 virtual void EmitCOFFSafeSEH(MCSymbol
const *Symbol
);
529 /// Emits the symbol table index of a Symbol into the current section.
530 virtual void EmitCOFFSymbolIndex(MCSymbol
const *Symbol
);
532 /// Emits a COFF section index.
534 /// \param Symbol - Symbol the section number relocation should point to.
535 virtual void EmitCOFFSectionIndex(MCSymbol
const *Symbol
);
537 /// Emits a COFF section relative relocation.
539 /// \param Symbol - Symbol the section relative relocation should point to.
540 virtual void EmitCOFFSecRel32(MCSymbol
const *Symbol
, uint64_t Offset
);
542 /// Emits a COFF image relative relocation.
544 /// \param Symbol - Symbol the image relative relocation should point to.
545 virtual void EmitCOFFImgRel32(MCSymbol
const *Symbol
, int64_t Offset
);
547 /// Emits an lcomm directive with XCOFF csect information.
549 /// \param Symbol - The symbol we are emiting.
550 /// \param Size - The size of the block of storage.
551 /// \param ByteAlignment - The alignment of the symbol in bytes. Must be a power
553 virtual void EmitXCOFFLocalCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
554 unsigned ByteAlignment
);
556 /// Emit an ELF .size directive.
558 /// This corresponds to an assembler statement such as:
559 /// .size symbol, expression
560 virtual void emitELFSize(MCSymbol
*Symbol
, const MCExpr
*Value
);
562 /// Emit an ELF .symver directive.
564 /// This corresponds to an assembler statement such as:
565 /// .symver _start, foo@@SOME_VERSION
566 /// \param AliasName - The versioned alias (i.e. "foo@@SOME_VERSION")
567 /// \param Aliasee - The aliased symbol (i.e. "_start")
568 virtual void emitELFSymverDirective(StringRef AliasName
,
569 const MCSymbol
*Aliasee
);
571 /// Emit a Linker Optimization Hint (LOH) directive.
572 /// \param Args - Arguments of the LOH.
573 virtual void EmitLOHDirective(MCLOHType Kind
, const MCLOHArgs
&Args
) {}
575 /// Emit a common symbol.
577 /// \param Symbol - The common symbol to emit.
578 /// \param Size - The size of the common symbol.
579 /// \param ByteAlignment - The alignment of the symbol if
580 /// non-zero. This must be a power of 2.
581 virtual void EmitCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
582 unsigned ByteAlignment
) = 0;
584 /// Emit a local common (.lcomm) symbol.
586 /// \param Symbol - The common symbol to emit.
587 /// \param Size - The size of the common symbol.
588 /// \param ByteAlignment - The alignment of the common symbol in bytes.
589 virtual void EmitLocalCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
590 unsigned ByteAlignment
);
592 /// Emit the zerofill section and an optional symbol.
594 /// \param Section - The zerofill section to create and or to put the symbol
595 /// \param Symbol - The zerofill symbol to emit, if non-NULL.
596 /// \param Size - The size of the zerofill symbol.
597 /// \param ByteAlignment - The alignment of the zerofill symbol if
598 /// non-zero. This must be a power of 2 on some targets.
599 virtual void EmitZerofill(MCSection
*Section
, MCSymbol
*Symbol
= nullptr,
600 uint64_t Size
= 0, unsigned ByteAlignment
= 0,
601 SMLoc Loc
= SMLoc()) = 0;
603 /// Emit a thread local bss (.tbss) symbol.
605 /// \param Section - The thread local common section.
606 /// \param Symbol - The thread local common symbol to emit.
607 /// \param Size - The size of the symbol.
608 /// \param ByteAlignment - The alignment of the thread local common symbol
609 /// if non-zero. This must be a power of 2 on some targets.
610 virtual void EmitTBSSSymbol(MCSection
*Section
, MCSymbol
*Symbol
,
611 uint64_t Size
, unsigned ByteAlignment
= 0);
614 /// \name Generating Data
617 /// Emit the bytes in \p Data into the output.
619 /// This is used to implement assembler directives such as .byte, .ascii,
621 virtual void EmitBytes(StringRef Data
);
623 /// Functionally identical to EmitBytes. When emitting textual assembly, this
624 /// method uses .byte directives instead of .ascii or .asciz for readability.
625 virtual void EmitBinaryData(StringRef Data
);
627 /// Emit the expression \p Value into the output as a native
628 /// integer of the given \p Size bytes.
630 /// This is used to implement assembler directives such as .word, .quad,
633 /// \param Value - The value to emit.
634 /// \param Size - The size of the integer (in bytes) to emit. This must
635 /// match a native machine width.
636 /// \param Loc - The location of the expression for error reporting.
637 virtual void EmitValueImpl(const MCExpr
*Value
, unsigned Size
,
638 SMLoc Loc
= SMLoc());
640 void EmitValue(const MCExpr
*Value
, unsigned Size
, SMLoc Loc
= SMLoc());
642 /// Special case of EmitValue that avoids the client having
643 /// to pass in a MCExpr for constant integers.
644 virtual void EmitIntValue(uint64_t Value
, unsigned Size
);
646 /// Special case of EmitValue that avoids the client having to pass
647 /// in a MCExpr for constant integers & prints in Hex format for certain
649 virtual void EmitIntValueInHex(uint64_t Value
, unsigned Size
) {
650 EmitIntValue(Value
, Size
);
653 virtual void EmitULEB128Value(const MCExpr
*Value
);
655 virtual void EmitSLEB128Value(const MCExpr
*Value
);
657 /// Special case of EmitULEB128Value that avoids the client having to
658 /// pass in a MCExpr for constant integers.
659 void EmitULEB128IntValue(uint64_t Value
, unsigned PadTo
= 0);
661 /// Special case of EmitSLEB128Value that avoids the client having to
662 /// pass in a MCExpr for constant integers.
663 void EmitSLEB128IntValue(int64_t Value
);
665 /// Special case of EmitValue that avoids the client having to pass in
666 /// a MCExpr for MCSymbols.
667 void EmitSymbolValue(const MCSymbol
*Sym
, unsigned Size
,
668 bool IsSectionRelative
= false);
670 /// Emit the expression \p Value into the output as a dtprel
671 /// (64-bit DTP relative) value.
673 /// This is used to implement assembler directives such as .dtpreldword on
674 /// targets that support them.
675 virtual void EmitDTPRel64Value(const MCExpr
*Value
);
677 /// Emit the expression \p Value into the output as a dtprel
678 /// (32-bit DTP relative) value.
680 /// This is used to implement assembler directives such as .dtprelword on
681 /// targets that support them.
682 virtual void EmitDTPRel32Value(const MCExpr
*Value
);
684 /// Emit the expression \p Value into the output as a tprel
685 /// (64-bit TP relative) value.
687 /// This is used to implement assembler directives such as .tpreldword on
688 /// targets that support them.
689 virtual void EmitTPRel64Value(const MCExpr
*Value
);
691 /// Emit the expression \p Value into the output as a tprel
692 /// (32-bit TP relative) value.
694 /// This is used to implement assembler directives such as .tprelword on
695 /// targets that support them.
696 virtual void EmitTPRel32Value(const MCExpr
*Value
);
698 /// Emit the expression \p Value into the output as a gprel64 (64-bit
699 /// GP relative) value.
701 /// This is used to implement assembler directives such as .gpdword on
702 /// targets that support them.
703 virtual void EmitGPRel64Value(const MCExpr
*Value
);
705 /// Emit the expression \p Value into the output as a gprel32 (32-bit
706 /// GP relative) value.
708 /// This is used to implement assembler directives such as .gprel32 on
709 /// targets that support them.
710 virtual void EmitGPRel32Value(const MCExpr
*Value
);
712 /// Emit NumBytes bytes worth of the value specified by FillValue.
713 /// This implements directives such as '.space'.
714 void emitFill(uint64_t NumBytes
, uint8_t FillValue
);
716 /// Emit \p Size bytes worth of the value specified by \p FillValue.
718 /// This is used to implement assembler directives such as .space or .skip.
720 /// \param NumBytes - The number of bytes to emit.
721 /// \param FillValue - The value to use when filling bytes.
722 /// \param Loc - The location of the expression for error reporting.
723 virtual void emitFill(const MCExpr
&NumBytes
, uint64_t FillValue
,
724 SMLoc Loc
= SMLoc());
726 /// Emit \p NumValues copies of \p Size bytes. Each \p Size bytes is
727 /// taken from the lowest order 4 bytes of \p Expr expression.
729 /// This is used to implement assembler directives such as .fill.
731 /// \param NumValues - The number of copies of \p Size bytes to emit.
732 /// \param Size - The size (in bytes) of each repeated value.
733 /// \param Expr - The expression from which \p Size bytes are used.
734 virtual void emitFill(const MCExpr
&NumValues
, int64_t Size
, int64_t Expr
,
735 SMLoc Loc
= SMLoc());
737 /// Emit NumBytes worth of zeros.
738 /// This function properly handles data in virtual sections.
739 void EmitZeros(uint64_t NumBytes
);
741 /// Emit some number of copies of \p Value until the byte alignment \p
742 /// ByteAlignment is reached.
744 /// If the number of bytes need to emit for the alignment is not a multiple
745 /// of \p ValueSize, then the contents of the emitted fill bytes is
748 /// This used to implement the .align assembler directive.
750 /// \param ByteAlignment - The alignment to reach. This must be a power of
751 /// two on some targets.
752 /// \param Value - The value to use when filling bytes.
753 /// \param ValueSize - The size of the integer (in bytes) to emit for
754 /// \p Value. This must match a native machine width.
755 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
756 /// the alignment cannot be reached in this many bytes, no bytes are
758 virtual void EmitValueToAlignment(unsigned ByteAlignment
, int64_t Value
= 0,
759 unsigned ValueSize
= 1,
760 unsigned MaxBytesToEmit
= 0);
762 /// Emit nops until the byte alignment \p ByteAlignment is reached.
764 /// This used to align code where the alignment bytes may be executed. This
765 /// can emit different bytes for different sizes to optimize execution.
767 /// \param ByteAlignment - The alignment to reach. This must be a power of
768 /// two on some targets.
769 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
770 /// the alignment cannot be reached in this many bytes, no bytes are
772 virtual void EmitCodeAlignment(unsigned ByteAlignment
,
773 unsigned MaxBytesToEmit
= 0);
775 /// Emit some number of copies of \p Value until the byte offset \p
776 /// Offset is reached.
778 /// This is used to implement assembler directives such as .org.
780 /// \param Offset - The offset to reach. This may be an expression, but the
781 /// expression must be associated with the current section.
782 /// \param Value - The value to use when filling bytes.
783 virtual void emitValueToOffset(const MCExpr
*Offset
, unsigned char Value
,
787 EmitCodePaddingBasicBlockStart(const MCCodePaddingContext
&Context
) {}
790 EmitCodePaddingBasicBlockEnd(const MCCodePaddingContext
&Context
) {}
794 /// Switch to a new logical file. This is used to implement the '.file
795 /// "foo.c"' assembler directive.
796 virtual void EmitFileDirective(StringRef Filename
);
798 /// Emit the "identifiers" directive. This implements the
799 /// '.ident "version foo"' assembler directive.
800 virtual void EmitIdent(StringRef IdentString
) {}
802 /// Associate a filename with a specified logical file number. This
803 /// implements the DWARF2 '.file 4 "foo.c"' assembler directive.
804 unsigned EmitDwarfFileDirective(unsigned FileNo
, StringRef Directory
,
806 Optional
<MD5::MD5Result
> Checksum
= None
,
807 Optional
<StringRef
> Source
= None
,
810 tryEmitDwarfFileDirective(FileNo
, Directory
, Filename
, Checksum
,
814 /// Associate a filename with a specified logical file number.
815 /// Also associate a directory, optional checksum, and optional source
816 /// text with the logical file. This implements the DWARF2
817 /// '.file 4 "dir/foo.c"' assembler directive, and the DWARF5
818 /// '.file 4 "dir/foo.c" md5 "..." source "..."' assembler directive.
819 virtual Expected
<unsigned> tryEmitDwarfFileDirective(
820 unsigned FileNo
, StringRef Directory
, StringRef Filename
,
821 Optional
<MD5::MD5Result
> Checksum
= None
, Optional
<StringRef
> Source
= None
,
824 /// Specify the "root" file of the compilation, using the ".file 0" extension.
825 virtual void emitDwarfFile0Directive(StringRef Directory
, StringRef Filename
,
826 Optional
<MD5::MD5Result
> Checksum
,
827 Optional
<StringRef
> Source
,
830 virtual void EmitCFIBKeyFrame();
832 /// This implements the DWARF2 '.loc fileno lineno ...' assembler
834 virtual void EmitDwarfLocDirective(unsigned FileNo
, unsigned Line
,
835 unsigned Column
, unsigned Flags
,
836 unsigned Isa
, unsigned Discriminator
,
839 /// Associate a filename with a specified logical file number, and also
840 /// specify that file's checksum information. This implements the '.cv_file 4
841 /// "foo.c"' assembler directive. Returns true on success.
842 virtual bool EmitCVFileDirective(unsigned FileNo
, StringRef Filename
,
843 ArrayRef
<uint8_t> Checksum
,
844 unsigned ChecksumKind
);
846 /// Introduces a function id for use with .cv_loc.
847 virtual bool EmitCVFuncIdDirective(unsigned FunctionId
);
849 /// Introduces an inline call site id for use with .cv_loc. Includes
850 /// extra information for inline line table generation.
851 virtual bool EmitCVInlineSiteIdDirective(unsigned FunctionId
, unsigned IAFunc
,
852 unsigned IAFile
, unsigned IALine
,
853 unsigned IACol
, SMLoc Loc
);
855 /// This implements the CodeView '.cv_loc' assembler directive.
856 virtual void EmitCVLocDirective(unsigned FunctionId
, unsigned FileNo
,
857 unsigned Line
, unsigned Column
,
858 bool PrologueEnd
, bool IsStmt
,
859 StringRef FileName
, SMLoc Loc
);
861 /// This implements the CodeView '.cv_linetable' assembler directive.
862 virtual void EmitCVLinetableDirective(unsigned FunctionId
,
863 const MCSymbol
*FnStart
,
864 const MCSymbol
*FnEnd
);
866 /// This implements the CodeView '.cv_inline_linetable' assembler
868 virtual void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId
,
869 unsigned SourceFileId
,
870 unsigned SourceLineNum
,
871 const MCSymbol
*FnStartSym
,
872 const MCSymbol
*FnEndSym
);
874 /// This implements the CodeView '.cv_def_range' assembler
876 virtual void EmitCVDefRangeDirective(
877 ArrayRef
<std::pair
<const MCSymbol
*, const MCSymbol
*>> Ranges
,
878 StringRef FixedSizePortion
);
880 virtual void EmitCVDefRangeDirective(
881 ArrayRef
<std::pair
<const MCSymbol
*, const MCSymbol
*>> Ranges
,
882 codeview::DefRangeRegisterRelHeader DRHdr
);
884 virtual void EmitCVDefRangeDirective(
885 ArrayRef
<std::pair
<const MCSymbol
*, const MCSymbol
*>> Ranges
,
886 codeview::DefRangeSubfieldRegisterHeader DRHdr
);
888 virtual void EmitCVDefRangeDirective(
889 ArrayRef
<std::pair
<const MCSymbol
*, const MCSymbol
*>> Ranges
,
890 codeview::DefRangeRegisterHeader DRHdr
);
892 virtual void EmitCVDefRangeDirective(
893 ArrayRef
<std::pair
<const MCSymbol
*, const MCSymbol
*>> Ranges
,
894 codeview::DefRangeFramePointerRelHeader DRHdr
);
896 /// This implements the CodeView '.cv_stringtable' assembler directive.
897 virtual void EmitCVStringTableDirective() {}
899 /// This implements the CodeView '.cv_filechecksums' assembler directive.
900 virtual void EmitCVFileChecksumsDirective() {}
902 /// This implements the CodeView '.cv_filechecksumoffset' assembler
904 virtual void EmitCVFileChecksumOffsetDirective(unsigned FileNo
) {}
906 /// This implements the CodeView '.cv_fpo_data' assembler directive.
907 virtual void EmitCVFPOData(const MCSymbol
*ProcSym
, SMLoc Loc
= {}) {}
909 /// Emit the absolute difference between two symbols.
911 /// \pre Offset of \c Hi is greater than the offset \c Lo.
912 virtual void emitAbsoluteSymbolDiff(const MCSymbol
*Hi
, const MCSymbol
*Lo
,
915 /// Emit the absolute difference between two symbols encoded with ULEB128.
916 virtual void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol
*Hi
,
919 virtual MCSymbol
*getDwarfLineTableSymbol(unsigned CUID
);
920 virtual void EmitCFISections(bool EH
, bool Debug
);
921 void EmitCFIStartProc(bool IsSimple
, SMLoc Loc
= SMLoc());
922 void EmitCFIEndProc();
923 virtual void EmitCFIDefCfa(int64_t Register
, int64_t Offset
);
924 virtual void EmitCFIDefCfaOffset(int64_t Offset
);
925 virtual void EmitCFIDefCfaRegister(int64_t Register
);
926 virtual void EmitCFIOffset(int64_t Register
, int64_t Offset
);
927 virtual void EmitCFIPersonality(const MCSymbol
*Sym
, unsigned Encoding
);
928 virtual void EmitCFILsda(const MCSymbol
*Sym
, unsigned Encoding
);
929 virtual void EmitCFIRememberState();
930 virtual void EmitCFIRestoreState();
931 virtual void EmitCFISameValue(int64_t Register
);
932 virtual void EmitCFIRestore(int64_t Register
);
933 virtual void EmitCFIRelOffset(int64_t Register
, int64_t Offset
);
934 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment
);
935 virtual void EmitCFIEscape(StringRef Values
);
936 virtual void EmitCFIReturnColumn(int64_t Register
);
937 virtual void EmitCFIGnuArgsSize(int64_t Size
);
938 virtual void EmitCFISignalFrame();
939 virtual void EmitCFIUndefined(int64_t Register
);
940 virtual void EmitCFIRegister(int64_t Register1
, int64_t Register2
);
941 virtual void EmitCFIWindowSave();
942 virtual void EmitCFINegateRAState();
944 virtual void EmitWinCFIStartProc(const MCSymbol
*Symbol
, SMLoc Loc
= SMLoc());
945 virtual void EmitWinCFIEndProc(SMLoc Loc
= SMLoc());
946 /// This is used on platforms, such as Windows on ARM64, that require function
947 /// or funclet sizes to be emitted in .xdata before the End marker is emitted
948 /// for the frame. We cannot use the End marker, as it is not set at the
949 /// point of emitting .xdata, in order to indicate that the frame is active.
950 virtual void EmitWinCFIFuncletOrFuncEnd(SMLoc Loc
= SMLoc());
951 virtual void EmitWinCFIStartChained(SMLoc Loc
= SMLoc());
952 virtual void EmitWinCFIEndChained(SMLoc Loc
= SMLoc());
953 virtual void EmitWinCFIPushReg(MCRegister Register
, SMLoc Loc
= SMLoc());
954 virtual void EmitWinCFISetFrame(MCRegister Register
, unsigned Offset
,
955 SMLoc Loc
= SMLoc());
956 virtual void EmitWinCFIAllocStack(unsigned Size
, SMLoc Loc
= SMLoc());
957 virtual void EmitWinCFISaveReg(MCRegister Register
, unsigned Offset
,
958 SMLoc Loc
= SMLoc());
959 virtual void EmitWinCFISaveXMM(MCRegister Register
, unsigned Offset
,
960 SMLoc Loc
= SMLoc());
961 virtual void EmitWinCFIPushFrame(bool Code
, SMLoc Loc
= SMLoc());
962 virtual void EmitWinCFIEndProlog(SMLoc Loc
= SMLoc());
963 virtual void EmitWinEHHandler(const MCSymbol
*Sym
, bool Unwind
, bool Except
,
964 SMLoc Loc
= SMLoc());
965 virtual void EmitWinEHHandlerData(SMLoc Loc
= SMLoc());
967 virtual void emitCGProfileEntry(const MCSymbolRefExpr
*From
,
968 const MCSymbolRefExpr
*To
, uint64_t Count
);
970 /// Get the .pdata section used for the given section. Typically the given
971 /// section is either the main .text section or some other COMDAT .text
972 /// section, but it may be any section containing code.
973 MCSection
*getAssociatedPDataSection(const MCSection
*TextSec
);
975 /// Get the .xdata section used for the given section.
976 MCSection
*getAssociatedXDataSection(const MCSection
*TextSec
);
978 virtual void EmitSyntaxDirective();
980 /// Emit a .reloc directive.
981 /// Returns true if the relocation could not be emitted because Name is not
983 virtual bool EmitRelocDirective(const MCExpr
&Offset
, StringRef Name
,
984 const MCExpr
*Expr
, SMLoc Loc
,
985 const MCSubtargetInfo
&STI
) {
989 virtual void EmitAddrsig() {}
990 virtual void EmitAddrsigSym(const MCSymbol
*Sym
) {}
992 /// Emit the given \p Instruction into the current section.
993 virtual void EmitInstruction(const MCInst
&Inst
, const MCSubtargetInfo
&STI
);
995 /// Set the bundle alignment mode from now on in the section.
996 /// The argument is the power of 2 to which the alignment is set. The
997 /// value 0 means turn the bundle alignment off.
998 virtual void EmitBundleAlignMode(unsigned AlignPow2
);
1000 /// The following instructions are a bundle-locked group.
1002 /// \param AlignToEnd - If true, the bundle-locked group will be aligned to
1003 /// the end of a bundle.
1004 virtual void EmitBundleLock(bool AlignToEnd
);
1006 /// Ends a bundle-locked group.
1007 virtual void EmitBundleUnlock();
1009 /// If this file is backed by a assembly streamer, this dumps the
1010 /// specified string in the output .s file. This capability is indicated by
1011 /// the hasRawTextSupport() predicate. By default this aborts.
1012 void EmitRawText(const Twine
&String
);
1014 /// Streamer specific finalization.
1015 virtual void FinishImpl();
1016 /// Finish emission of machine code.
1019 virtual bool mayHaveInstructions(MCSection
&Sec
) const { return true; }
1022 /// Create a dummy machine code streamer, which does nothing. This is useful for
1023 /// timing the assembler front end.
1024 MCStreamer
*createNullStreamer(MCContext
&Ctx
);
1026 /// Create a machine code streamer which will print out assembly for the native
1027 /// target, suitable for compiling with a native assembler.
1029 /// \param InstPrint - If given, the instruction printer to use. If not given
1030 /// the MCInst representation will be printed. This method takes ownership of
1033 /// \param CE - If given, a code emitter to use to show the instruction
1034 /// encoding inline with the assembly. This method takes ownership of \p CE.
1036 /// \param TAB - If given, a target asm backend to use to show the fixup
1037 /// information in conjunction with encoding information. This method takes
1038 /// ownership of \p TAB.
1040 /// \param ShowInst - Whether to show the MCInst representation inline with
1042 MCStreamer
*createAsmStreamer(MCContext
&Ctx
,
1043 std::unique_ptr
<formatted_raw_ostream
> OS
,
1044 bool isVerboseAsm
, bool useDwarfDirectory
,
1045 MCInstPrinter
*InstPrint
, MCCodeEmitter
*CE
,
1046 MCAsmBackend
*TAB
, bool ShowInst
);
1048 } // end namespace llvm
1050 #endif // LLVM_MC_MCSTREAMER_H