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/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/MC/MCDirectives.h"
22 #include "llvm/MC/MCDwarf.h"
23 #include "llvm/MC/MCLinkerOptimizationHint.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/MC/MCWinEH.h"
26 #include "llvm/Support/SMLoc.h"
27 #include "llvm/Support/TargetParser.h"
37 class AssemblerConstantPools;
38 class formatted_raw_ostream;
41 struct MCCodePaddingContext;
48 class MCSymbolRefExpr;
49 class MCSubtargetInfo;
53 using MCSectionSubPair = std::pair<MCSection *, const MCExpr *>;
55 /// Target specific streamer interface. This is used so that targets can
56 /// implement support for target specific assembly directives.
58 /// If target foo wants to use this, it should implement 3 classes:
59 /// * FooTargetStreamer : public MCTargetStreamer
60 /// * FooTargetAsmStreamer : public FooTargetStreamer
61 /// * FooTargetELFStreamer : public FooTargetStreamer
63 /// FooTargetStreamer should have a pure virtual method for each directive. For
64 /// example, for a ".bar symbol_name" directive, it should have
65 /// virtual emitBar(const MCSymbol &Symbol) = 0;
67 /// The FooTargetAsmStreamer and FooTargetELFStreamer classes implement the
68 /// method. The assembly streamer just prints ".bar symbol_name". The object
69 /// streamer does whatever is needed to implement .bar in the object file.
71 /// In the assembly printer and parser the target streamer can be used by
72 /// calling getTargetStreamer and casting it to FooTargetStreamer:
74 /// MCTargetStreamer &TS = OutStreamer.getTargetStreamer();
75 /// FooTargetStreamer &ATS = static_cast<FooTargetStreamer &>(TS);
77 /// The base classes FooTargetAsmStreamer and FooTargetELFStreamer should
78 /// *never* be treated differently. Callers should always talk to a
79 /// FooTargetStreamer.
80 class MCTargetStreamer {
85 MCTargetStreamer(MCStreamer &S);
86 virtual ~MCTargetStreamer();
88 MCStreamer &getStreamer() { return Streamer; }
90 // Allow a target to add behavior to the EmitLabel of MCStreamer.
91 virtual void emitLabel(MCSymbol *Symbol);
92 // Allow a target to add behavior to the emitAssignment of MCStreamer.
93 virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value);
95 virtual void prettyPrintAsm(MCInstPrinter &InstPrinter, raw_ostream &OS,
96 const MCInst &Inst, const MCSubtargetInfo &STI);
98 virtual void emitDwarfFileDirective(StringRef Directive);
100 /// Update streamer for a new active section.
102 /// This is called by PopSection and SwitchSection, if the current
104 virtual void changeSection(const MCSection *CurSection, MCSection *Section,
105 const MCExpr *SubSection, raw_ostream &OS);
107 virtual void emitValue(const MCExpr *Value);
109 virtual void finish();
112 // FIXME: declared here because it is used from
113 // lib/CodeGen/AsmPrinter/ARMException.cpp.
114 class ARMTargetStreamer : public MCTargetStreamer {
116 ARMTargetStreamer(MCStreamer &S);
117 ~ARMTargetStreamer() override;
119 virtual void emitFnStart();
120 virtual void emitFnEnd();
121 virtual void emitCantUnwind();
122 virtual void emitPersonality(const MCSymbol *Personality);
123 virtual void emitPersonalityIndex(unsigned Index);
124 virtual void emitHandlerData();
125 virtual void emitSetFP(unsigned FpReg, unsigned SpReg,
127 virtual void emitMovSP(unsigned Reg, int64_t Offset = 0);
128 virtual void emitPad(int64_t Offset);
129 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
131 virtual void emitUnwindRaw(int64_t StackOffset,
132 const SmallVectorImpl<uint8_t> &Opcodes);
134 virtual void switchVendor(StringRef Vendor);
135 virtual void emitAttribute(unsigned Attribute, unsigned Value);
136 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
137 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
138 StringRef StringValue = "");
139 virtual void emitFPU(unsigned FPU);
140 virtual void emitArch(ARM::ArchKind Arch);
141 virtual void emitArchExtension(unsigned ArchExt);
142 virtual void emitObjectArch(ARM::ArchKind Arch);
143 void emitTargetAttributes(const MCSubtargetInfo &STI);
144 virtual void finishAttributeSection();
145 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
147 virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
149 virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value);
151 void finish() override;
153 /// Reset any state between object emissions, i.e. the equivalent of
154 /// MCStreamer's reset method.
155 virtual void reset();
157 /// Callback used to implement the ldr= pseudo.
158 /// Add a new entry to the constant pool for the current section and return an
159 /// MCExpr that can be used to refer to the constant pool location.
160 const MCExpr *addConstantPoolEntry(const MCExpr *, SMLoc Loc);
162 /// Callback used to implemnt the .ltorg directive.
163 /// Emit contents of constant pool for the current section.
164 void emitCurrentConstantPool();
167 std::unique_ptr<AssemblerConstantPools> ConstantPools;
170 /// \brief Streaming machine code generation interface.
172 /// This interface is intended to provide a programatic interface that is very
173 /// similar to the level that an assembler .s file provides. It has callbacks
174 /// to emit bytes, handle directives, etc. The implementation of this interface
175 /// retains state to know what the current section is etc.
177 /// There are multiple implementations of this interface: one for writing out
178 /// a .s file, and implementations that write out .o files of various formats.
182 std::unique_ptr<MCTargetStreamer> TargetStreamer;
184 std::vector<MCDwarfFrameInfo> DwarfFrameInfos;
185 MCDwarfFrameInfo *getCurrentDwarfFrameInfo();
187 /// Similar to DwarfFrameInfos, but for SEH unwind info. Chained frames may
188 /// refer to each other, so use std::unique_ptr to provide pointer stability.
189 std::vector<std::unique_ptr<WinEH::FrameInfo>> WinFrameInfos;
191 WinEH::FrameInfo *CurrentWinFrameInfo;
193 /// Retreive the current frame info if one is available and it is not yet
194 /// closed. Otherwise, issue an error and return null.
195 WinEH::FrameInfo *EnsureValidWinFrameInfo(SMLoc Loc);
197 /// \brief Tracks an index to represent the order a symbol was emitted in.
198 /// Zero means we did not emit that symbol.
199 DenseMap<const MCSymbol *, unsigned> SymbolOrdering;
201 /// \brief This is stack of current and previous section values saved by
203 SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack;
205 /// The next unique ID to use when creating a WinCFI-related section (.pdata
206 /// or .xdata). This ID ensures that we have a one-to-one mapping from
207 /// code section to unwind info section, which MSVC's incremental linker
209 unsigned NextWinCFIID = 0;
212 MCStreamer(MCContext &Ctx);
214 virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
215 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
217 /// When emitting an object file, create and emit a real label. When emitting
218 /// textual assembly, this should do nothing to avoid polluting our output.
219 virtual MCSymbol *EmitCFILabel();
221 WinEH::FrameInfo *getCurrentWinFrameInfo() {
222 return CurrentWinFrameInfo;
225 virtual void EmitWindowsUnwindTables();
227 virtual void EmitRawTextImpl(StringRef String);
230 MCStreamer(const MCStreamer &) = delete;
231 MCStreamer &operator=(const MCStreamer &) = delete;
232 virtual ~MCStreamer();
234 void visitUsedExpr(const MCExpr &Expr);
235 virtual void visitUsedSymbol(const MCSymbol &Sym);
237 void setTargetStreamer(MCTargetStreamer *TS) {
238 TargetStreamer.reset(TS);
243 virtual void reset();
245 MCContext &getContext() const { return Context; }
247 MCTargetStreamer *getTargetStreamer() {
248 return TargetStreamer.get();
251 unsigned getNumFrameInfos() { return DwarfFrameInfos.size(); }
252 ArrayRef<MCDwarfFrameInfo> getDwarfFrameInfos() const {
253 return DwarfFrameInfos;
256 bool hasUnfinishedDwarfFrameInfo();
258 unsigned getNumWinFrameInfos() { return WinFrameInfos.size(); }
259 ArrayRef<std::unique_ptr<WinEH::FrameInfo>> getWinFrameInfos() const {
260 return WinFrameInfos;
263 void generateCompactUnwindEncodings(MCAsmBackend *MAB);
265 /// \name Assembly File Formatting.
268 /// \brief Return true if this streamer supports verbose assembly and if it is
270 virtual bool isVerboseAsm() const { return false; }
272 /// \brief Return true if this asm streamer supports emitting unformatted text
273 /// to the .s file with EmitRawText.
274 virtual bool hasRawTextSupport() const { return false; }
276 /// \brief Is the integrated assembler required for this streamer to function
278 virtual bool isIntegratedAssemblerRequired() const { return false; }
280 /// \brief Add a textual comment.
282 /// Typically for comments that can be emitted to the generated .s
283 /// file if applicable as a QoI issue to make the output of the compiler
284 /// more readable. This only affects the MCAsmStreamer, and only when
285 /// verbose assembly output is enabled.
287 /// If the comment includes embedded \n's, they will each get the comment
288 /// prefix as appropriate. The added comment should not end with a \n.
289 /// By default, each comment is terminated with an end of line, i.e. the
290 /// EOL param is set to true by default. If one prefers not to end the
291 /// comment with a new line then the EOL param should be passed
292 /// with a false value.
293 virtual void AddComment(const Twine &T, bool EOL = true) {}
295 /// \brief Return a raw_ostream that comments can be written to. Unlike
296 /// AddComment, you are required to terminate comments with \n if you use this
298 virtual raw_ostream &GetCommentOS();
300 /// \brief Print T and prefix it with the comment string (normally #) and
301 /// optionally a tab. This prints the comment immediately, not at the end of
302 /// the current line. It is basically a safe version of EmitRawText: since it
303 /// only prints comments, the object streamer ignores it instead of asserting.
304 virtual void emitRawComment(const Twine &T, bool TabPrefix = true);
306 /// \brief Add explicit comment T. T is required to be a valid
307 /// comment in the output and does not need to be escaped.
308 virtual void addExplicitComment(const Twine &T);
310 /// \brief Emit added explicit comments.
311 virtual void emitExplicitComments();
313 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
314 virtual void AddBlankLine() {}
318 /// \name Symbol & Section Management
321 /// \brief Return the current section that the streamer is emitting code to.
322 MCSectionSubPair getCurrentSection() const {
323 if (!SectionStack.empty())
324 return SectionStack.back().first;
325 return MCSectionSubPair();
327 MCSection *getCurrentSectionOnly() const { return getCurrentSection().first; }
329 /// \brief Return the previous section that the streamer is emitting code to.
330 MCSectionSubPair getPreviousSection() const {
331 if (!SectionStack.empty())
332 return SectionStack.back().second;
333 return MCSectionSubPair();
336 /// \brief Returns an index to represent the order a symbol was emitted in.
337 /// (zero if we did not emit that symbol)
338 unsigned GetSymbolOrder(const MCSymbol *Sym) const {
339 return SymbolOrdering.lookup(Sym);
342 /// \brief Update streamer for a new active section.
344 /// This is called by PopSection and SwitchSection, if the current
346 virtual void ChangeSection(MCSection *, const MCExpr *);
348 /// \brief Save the current and previous section on the section stack.
350 SectionStack.push_back(
351 std::make_pair(getCurrentSection(), getPreviousSection()));
354 /// \brief Restore the current and previous section from the section stack.
355 /// Calls ChangeSection as needed.
357 /// Returns false if the stack was empty.
359 if (SectionStack.size() <= 1)
361 auto I = SectionStack.end();
363 MCSectionSubPair OldSection = I->first;
365 MCSectionSubPair NewSection = I->first;
367 if (OldSection != NewSection)
368 ChangeSection(NewSection.first, NewSection.second);
369 SectionStack.pop_back();
373 bool SubSection(const MCExpr *Subsection) {
374 if (SectionStack.empty())
377 SwitchSection(SectionStack.back().first.first, Subsection);
381 /// Set the current section where code is being emitted to \p Section. This
382 /// is required to update CurSection.
384 /// This corresponds to assembler directives like .section, .text, etc.
385 virtual void SwitchSection(MCSection *Section,
386 const MCExpr *Subsection = nullptr);
388 /// \brief Set the current section where code is being emitted to \p Section.
389 /// This is required to update CurSection. This version does not call
391 void SwitchSectionNoChange(MCSection *Section,
392 const MCExpr *Subsection = nullptr) {
393 assert(Section && "Cannot switch to a null section!");
394 MCSectionSubPair curSection = SectionStack.back().first;
395 SectionStack.back().second = curSection;
396 if (MCSectionSubPair(Section, Subsection) != curSection)
397 SectionStack.back().first = MCSectionSubPair(Section, Subsection);
400 /// \brief Create the default sections and set the initial one.
401 virtual void InitSections(bool NoExecStack);
403 MCSymbol *endSection(MCSection *Section);
405 /// \brief Sets the symbol's section.
407 /// Each emitted symbol will be tracked in the ordering table,
408 /// so we can sort on them later.
409 void AssignFragment(MCSymbol *Symbol, MCFragment *Fragment);
411 /// \brief Emit a label for \p Symbol into the current section.
413 /// This corresponds to an assembler statement such as:
416 /// \param Symbol - The symbol to emit. A given symbol should only be
417 /// emitted as a label once, and symbols emitted as a label should never be
418 /// used in an assignment.
419 // FIXME: These emission are non-const because we mutate the symbol to
420 // add the section we're emitting it to later.
421 virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc());
423 virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol);
425 /// \brief Note in the output the specified \p Flag.
426 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
428 /// \brief Emit the given list \p Options of strings as linker
429 /// options into the output.
430 virtual void EmitLinkerOptions(ArrayRef<std::string> Kind) {}
432 /// \brief Note in the output the specified region \p Kind.
433 virtual void EmitDataRegion(MCDataRegionType Kind) {}
435 /// \brief Specify the Mach-O minimum deployment target version.
436 virtual void EmitVersionMin(MCVersionMinType Type, unsigned Major,
437 unsigned Minor, unsigned Update) {}
439 /// Emit/Specify Mach-O build version command.
440 /// \p Platform should be one of MachO::PlatformType.
441 virtual void EmitBuildVersion(unsigned Platform, unsigned Major,
442 unsigned Minor, unsigned Update) {}
444 void EmitVersionForTarget(const Triple &Target);
446 /// \brief Note in the output that the specified \p Func is a Thumb mode
447 /// function (ARM target only).
448 virtual void EmitThumbFunc(MCSymbol *Func);
450 /// \brief Emit an assignment of \p Value to \p Symbol.
452 /// This corresponds to an assembler statement such as:
455 /// The assignment generates no code, but has the side effect of binding the
456 /// value in the current context. For the assembly streamer, this prints the
457 /// binding into the .s file.
459 /// \param Symbol - The symbol being assigned to.
460 /// \param Value - The value for the symbol.
461 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
463 /// \brief Emit an weak reference from \p Alias to \p Symbol.
465 /// This corresponds to an assembler statement such as:
466 /// .weakref alias, symbol
468 /// \param Alias - The alias that is being created.
469 /// \param Symbol - The symbol being aliased.
470 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
472 /// \brief Add the given \p Attribute to \p Symbol.
473 virtual bool EmitSymbolAttribute(MCSymbol *Symbol,
474 MCSymbolAttr Attribute) = 0;
476 /// \brief Set the \p DescValue for the \p Symbol.
478 /// \param Symbol - The symbol to have its n_desc field set.
479 /// \param DescValue - The value to set into the n_desc field.
480 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
482 /// \brief Start emitting COFF symbol definition
484 /// \param Symbol - The symbol to have its External & Type fields set.
485 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
487 /// \brief Emit the storage class of the symbol.
489 /// \param StorageClass - The storage class the symbol should have.
490 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
492 /// \brief Emit the type of the symbol.
494 /// \param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
495 virtual void EmitCOFFSymbolType(int Type);
497 /// \brief Marks the end of the symbol definition.
498 virtual void EndCOFFSymbolDef();
500 virtual void EmitCOFFSafeSEH(MCSymbol const *Symbol);
502 /// \brief Emits a COFF section index.
504 /// \param Symbol - Symbol the section number relocation should point to.
505 virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
507 /// \brief Emits a COFF section relative relocation.
509 /// \param Symbol - Symbol the section relative relocation should point to.
510 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset);
512 /// \brief Emit an ELF .size directive.
514 /// This corresponds to an assembler statement such as:
515 /// .size symbol, expression
516 virtual void emitELFSize(MCSymbol *Symbol, const MCExpr *Value);
518 /// \brief Emit an ELF .symver directive.
520 /// This corresponds to an assembler statement such as:
521 /// .symver _start, foo@@SOME_VERSION
522 /// \param Alias - The versioned alias (i.e. "foo@@SOME_VERSION")
523 /// \param Aliasee - The aliased symbol (i.e. "_start")
524 virtual void emitELFSymverDirective(MCSymbol *Alias, const MCSymbol *Aliasee);
526 /// \brief Emit a Linker Optimization Hint (LOH) directive.
527 /// \param Args - Arguments of the LOH.
528 virtual void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {}
530 /// \brief Emit a common symbol.
532 /// \param Symbol - The common symbol to emit.
533 /// \param Size - The size of the common symbol.
534 /// \param ByteAlignment - The alignment of the symbol if
535 /// non-zero. This must be a power of 2.
536 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
537 unsigned ByteAlignment) = 0;
539 /// \brief Emit a local common (.lcomm) symbol.
541 /// \param Symbol - The common symbol to emit.
542 /// \param Size - The size of the common symbol.
543 /// \param ByteAlignment - The alignment of the common symbol in bytes.
544 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
545 unsigned ByteAlignment);
547 /// \brief Emit the zerofill section and an optional symbol.
549 /// \param Section - The zerofill section to create and or to put the symbol
550 /// \param Symbol - The zerofill symbol to emit, if non-NULL.
551 /// \param Size - The size of the zerofill symbol.
552 /// \param ByteAlignment - The alignment of the zerofill symbol if
553 /// non-zero. This must be a power of 2 on some targets.
554 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
555 uint64_t Size = 0, unsigned ByteAlignment = 0) = 0;
557 /// \brief Emit a thread local bss (.tbss) symbol.
559 /// \param Section - The thread local common section.
560 /// \param Symbol - The thread local common symbol to emit.
561 /// \param Size - The size of the symbol.
562 /// \param ByteAlignment - The alignment of the thread local common symbol
563 /// if non-zero. This must be a power of 2 on some targets.
564 virtual void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
565 uint64_t Size, unsigned ByteAlignment = 0);
568 /// \name Generating Data
571 /// \brief Emit the bytes in \p Data into the output.
573 /// This is used to implement assembler directives such as .byte, .ascii,
575 virtual void EmitBytes(StringRef Data);
577 /// Functionally identical to EmitBytes. When emitting textual assembly, this
578 /// method uses .byte directives instead of .ascii or .asciz for readability.
579 virtual void EmitBinaryData(StringRef Data);
581 /// \brief Emit the expression \p Value into the output as a native
582 /// integer of the given \p Size bytes.
584 /// This is used to implement assembler directives such as .word, .quad,
587 /// \param Value - The value to emit.
588 /// \param Size - The size of the integer (in bytes) to emit. This must
589 /// match a native machine width.
590 /// \param Loc - The location of the expression for error reporting.
591 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
592 SMLoc Loc = SMLoc());
594 void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc = SMLoc());
596 /// \brief Special case of EmitValue that avoids the client having
597 /// to pass in a MCExpr for constant integers.
598 virtual void EmitIntValue(uint64_t Value, unsigned Size);
600 virtual void EmitULEB128Value(const MCExpr *Value);
602 virtual void EmitSLEB128Value(const MCExpr *Value);
604 /// \brief Special case of EmitULEB128Value that avoids the client having to
605 /// pass in a MCExpr for constant integers.
606 void EmitULEB128IntValue(uint64_t Value);
608 /// \brief Like EmitULEB128Value but pads the output to specific number of
610 void EmitPaddedULEB128IntValue(uint64_t Value, unsigned PadTo);
612 /// \brief Special case of EmitSLEB128Value that avoids the client having to
613 /// pass in a MCExpr for constant integers.
614 void EmitSLEB128IntValue(int64_t Value);
616 /// \brief Special case of EmitValue that avoids the client having to pass in
617 /// a MCExpr for MCSymbols.
618 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
619 bool IsSectionRelative = false);
621 /// \brief Emit the expression \p Value into the output as a dtprel
622 /// (64-bit DTP relative) value.
624 /// This is used to implement assembler directives such as .dtpreldword on
625 /// targets that support them.
626 virtual void EmitDTPRel64Value(const MCExpr *Value);
628 /// \brief Emit the expression \p Value into the output as a dtprel
629 /// (32-bit DTP relative) value.
631 /// This is used to implement assembler directives such as .dtprelword on
632 /// targets that support them.
633 virtual void EmitDTPRel32Value(const MCExpr *Value);
635 /// \brief Emit the expression \p Value into the output as a tprel
636 /// (64-bit TP relative) value.
638 /// This is used to implement assembler directives such as .tpreldword on
639 /// targets that support them.
640 virtual void EmitTPRel64Value(const MCExpr *Value);
642 /// \brief Emit the expression \p Value into the output as a tprel
643 /// (32-bit TP relative) value.
645 /// This is used to implement assembler directives such as .tprelword on
646 /// targets that support them.
647 virtual void EmitTPRel32Value(const MCExpr *Value);
649 /// \brief Emit the expression \p Value into the output as a gprel64 (64-bit
650 /// GP relative) value.
652 /// This is used to implement assembler directives such as .gpdword on
653 /// targets that support them.
654 virtual void EmitGPRel64Value(const MCExpr *Value);
656 /// \brief Emit the expression \p Value into the output as a gprel32 (32-bit
657 /// GP relative) value.
659 /// This is used to implement assembler directives such as .gprel32 on
660 /// targets that support them.
661 virtual void EmitGPRel32Value(const MCExpr *Value);
663 /// \brief Emit NumBytes bytes worth of the value specified by FillValue.
664 /// This implements directives such as '.space'.
665 virtual void emitFill(uint64_t NumBytes, uint8_t FillValue);
667 /// \brief Emit \p Size bytes worth of the value specified by \p FillValue.
669 /// This is used to implement assembler directives such as .space or .skip.
671 /// \param NumBytes - The number of bytes to emit.
672 /// \param FillValue - The value to use when filling bytes.
673 /// \param Loc - The location of the expression for error reporting.
674 virtual void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
675 SMLoc Loc = SMLoc());
677 /// \brief Emit \p NumValues copies of \p Size bytes. Each \p Size bytes is
678 /// taken from the lowest order 4 bytes of \p Expr expression.
680 /// This is used to implement assembler directives such as .fill.
682 /// \param NumValues - The number of copies of \p Size bytes to emit.
683 /// \param Size - The size (in bytes) of each repeated value.
684 /// \param Expr - The expression from which \p Size bytes are used.
685 virtual void emitFill(uint64_t NumValues, int64_t Size, int64_t Expr);
686 virtual void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
687 SMLoc Loc = SMLoc());
689 /// \brief Emit NumBytes worth of zeros.
690 /// This function properly handles data in virtual sections.
691 void EmitZeros(uint64_t NumBytes);
693 /// \brief Emit some number of copies of \p Value until the byte alignment \p
694 /// ByteAlignment is reached.
696 /// If the number of bytes need to emit for the alignment is not a multiple
697 /// of \p ValueSize, then the contents of the emitted fill bytes is
700 /// This used to implement the .align assembler directive.
702 /// \param ByteAlignment - The alignment to reach. This must be a power of
703 /// two on some targets.
704 /// \param Value - The value to use when filling bytes.
705 /// \param ValueSize - The size of the integer (in bytes) to emit for
706 /// \p Value. This must match a native machine width.
707 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
708 /// the alignment cannot be reached in this many bytes, no bytes are
710 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
711 unsigned ValueSize = 1,
712 unsigned MaxBytesToEmit = 0);
714 /// \brief Emit nops until the byte alignment \p ByteAlignment is reached.
716 /// This used to align code where the alignment bytes may be executed. This
717 /// can emit different bytes for different sizes to optimize execution.
719 /// \param ByteAlignment - The alignment to reach. This must be a power of
720 /// two on some targets.
721 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
722 /// the alignment cannot be reached in this many bytes, no bytes are
724 virtual void EmitCodeAlignment(unsigned ByteAlignment,
725 unsigned MaxBytesToEmit = 0);
727 /// \brief Emit some number of copies of \p Value until the byte offset \p
728 /// Offset is reached.
730 /// This is used to implement assembler directives such as .org.
732 /// \param Offset - The offset to reach. This may be an expression, but the
733 /// expression must be associated with the current section.
734 /// \param Value - The value to use when filling bytes.
735 virtual void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
739 EmitCodePaddingBasicBlockStart(const MCCodePaddingContext &Context) {}
742 EmitCodePaddingBasicBlockEnd(const MCCodePaddingContext &Context) {}
746 /// \brief Switch to a new logical file. This is used to implement the '.file
747 /// "foo.c"' assembler directive.
748 virtual void EmitFileDirective(StringRef Filename);
750 /// \brief Emit the "identifiers" directive. This implements the
751 /// '.ident "version foo"' assembler directive.
752 virtual void EmitIdent(StringRef IdentString) {}
754 /// \brief Associate a filename with a specified logical file number. This
755 /// implements the DWARF2 '.file 4 "foo.c"' assembler directive.
756 virtual unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
760 /// \brief This implements the DWARF2 '.loc fileno lineno ...' assembler
762 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
763 unsigned Column, unsigned Flags,
764 unsigned Isa, unsigned Discriminator,
767 /// Associate a filename with a specified logical file number, and also
768 /// specify that file's checksum information. This implements the '.cv_file 4
769 /// "foo.c"' assembler directive. Returns true on success.
770 virtual bool EmitCVFileDirective(unsigned FileNo, StringRef Filename,
771 ArrayRef<uint8_t> Checksum,
772 unsigned ChecksumKind);
774 /// \brief Introduces a function id for use with .cv_loc.
775 virtual bool EmitCVFuncIdDirective(unsigned FunctionId);
777 /// \brief Introduces an inline call site id for use with .cv_loc. Includes
778 /// extra information for inline line table generation.
779 virtual bool EmitCVInlineSiteIdDirective(unsigned FunctionId, unsigned IAFunc,
780 unsigned IAFile, unsigned IALine,
781 unsigned IACol, SMLoc Loc);
783 /// \brief This implements the CodeView '.cv_loc' assembler directive.
784 virtual void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo,
785 unsigned Line, unsigned Column,
786 bool PrologueEnd, bool IsStmt,
787 StringRef FileName, SMLoc Loc);
789 /// \brief This implements the CodeView '.cv_linetable' assembler directive.
790 virtual void EmitCVLinetableDirective(unsigned FunctionId,
791 const MCSymbol *FnStart,
792 const MCSymbol *FnEnd);
794 /// \brief This implements the CodeView '.cv_inline_linetable' assembler
796 virtual void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
797 unsigned SourceFileId,
798 unsigned SourceLineNum,
799 const MCSymbol *FnStartSym,
800 const MCSymbol *FnEndSym);
802 /// \brief This implements the CodeView '.cv_def_range' assembler
804 virtual void EmitCVDefRangeDirective(
805 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
806 StringRef FixedSizePortion);
808 /// \brief This implements the CodeView '.cv_stringtable' assembler directive.
809 virtual void EmitCVStringTableDirective() {}
811 /// \brief This implements the CodeView '.cv_filechecksums' assembler directive.
812 virtual void EmitCVFileChecksumsDirective() {}
814 /// This implements the CodeView '.cv_filechecksumoffset' assembler
816 virtual void EmitCVFileChecksumOffsetDirective(unsigned FileNo) {}
818 /// This implements the CodeView '.cv_fpo_data' assembler directive.
819 virtual void EmitCVFPOData(const MCSymbol *ProcSym, SMLoc Loc = {}) {}
821 /// Emit the absolute difference between two symbols.
823 /// \pre Offset of \c Hi is greater than the offset \c Lo.
824 virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
827 virtual MCSymbol *getDwarfLineTableSymbol(unsigned CUID);
828 virtual void EmitCFISections(bool EH, bool Debug);
829 void EmitCFIStartProc(bool IsSimple);
830 void EmitCFIEndProc();
831 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
832 virtual void EmitCFIDefCfaOffset(int64_t Offset);
833 virtual void EmitCFIDefCfaRegister(int64_t Register);
834 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
835 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
836 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
837 virtual void EmitCFIRememberState();
838 virtual void EmitCFIRestoreState();
839 virtual void EmitCFISameValue(int64_t Register);
840 virtual void EmitCFIRestore(int64_t Register);
841 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
842 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
843 virtual void EmitCFIEscape(StringRef Values);
844 virtual void EmitCFIReturnColumn(int64_t Register);
845 virtual void EmitCFIGnuArgsSize(int64_t Size);
846 virtual void EmitCFISignalFrame();
847 virtual void EmitCFIUndefined(int64_t Register);
848 virtual void EmitCFIRegister(int64_t Register1, int64_t Register2);
849 virtual void EmitCFIWindowSave();
851 virtual void EmitWinCFIStartProc(const MCSymbol *Symbol, SMLoc Loc = SMLoc());
852 virtual void EmitWinCFIEndProc(SMLoc Loc = SMLoc());
853 virtual void EmitWinCFIStartChained(SMLoc Loc = SMLoc());
854 virtual void EmitWinCFIEndChained(SMLoc Loc = SMLoc());
855 virtual void EmitWinCFIPushReg(unsigned Register, SMLoc Loc = SMLoc());
856 virtual void EmitWinCFISetFrame(unsigned Register, unsigned Offset,
857 SMLoc Loc = SMLoc());
858 virtual void EmitWinCFIAllocStack(unsigned Size, SMLoc Loc = SMLoc());
859 virtual void EmitWinCFISaveReg(unsigned Register, unsigned Offset,
860 SMLoc Loc = SMLoc());
861 virtual void EmitWinCFISaveXMM(unsigned Register, unsigned Offset,
862 SMLoc Loc = SMLoc());
863 virtual void EmitWinCFIPushFrame(bool Code, SMLoc Loc = SMLoc());
864 virtual void EmitWinCFIEndProlog(SMLoc Loc = SMLoc());
865 virtual void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except,
866 SMLoc Loc = SMLoc());
867 virtual void EmitWinEHHandlerData(SMLoc Loc = SMLoc());
869 /// Get the .pdata section used for the given section. Typically the given
870 /// section is either the main .text section or some other COMDAT .text
871 /// section, but it may be any section containing code.
872 MCSection *getAssociatedPDataSection(const MCSection *TextSec);
874 /// Get the .xdata section used for the given section.
875 MCSection *getAssociatedXDataSection(const MCSection *TextSec);
877 virtual void EmitSyntaxDirective();
879 /// \brief Emit a .reloc directive.
880 /// Returns true if the relocation could not be emitted because Name is not
882 virtual bool EmitRelocDirective(const MCExpr &Offset, StringRef Name,
883 const MCExpr *Expr, SMLoc Loc) {
887 /// \brief Emit the given \p Instruction into the current section.
888 /// PrintSchedInfo == true then schedul comment should be added to output
889 virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
890 bool PrintSchedInfo = false);
892 /// \brief Set the bundle alignment mode from now on in the section.
893 /// The argument is the power of 2 to which the alignment is set. The
894 /// value 0 means turn the bundle alignment off.
895 virtual void EmitBundleAlignMode(unsigned AlignPow2);
897 /// \brief The following instructions are a bundle-locked group.
899 /// \param AlignToEnd - If true, the bundle-locked group will be aligned to
900 /// the end of a bundle.
901 virtual void EmitBundleLock(bool AlignToEnd);
903 /// \brief Ends a bundle-locked group.
904 virtual void EmitBundleUnlock();
906 /// \brief If this file is backed by a assembly streamer, this dumps the
907 /// specified string in the output .s file. This capability is indicated by
908 /// the hasRawTextSupport() predicate. By default this aborts.
909 void EmitRawText(const Twine &String);
911 /// \brief Streamer specific finalization.
912 virtual void FinishImpl();
913 /// \brief Finish emission of machine code.
916 virtual bool mayHaveInstructions(MCSection &Sec) const { return true; }
919 /// Create a dummy machine code streamer, which does nothing. This is useful for
920 /// timing the assembler front end.
921 MCStreamer *createNullStreamer(MCContext &Ctx);
923 /// Create a machine code streamer which will print out assembly for the native
924 /// target, suitable for compiling with a native assembler.
926 /// \param InstPrint - If given, the instruction printer to use. If not given
927 /// the MCInst representation will be printed. This method takes ownership of
930 /// \param CE - If given, a code emitter to use to show the instruction
931 /// encoding inline with the assembly. This method takes ownership of \p CE.
933 /// \param TAB - If given, a target asm backend to use to show the fixup
934 /// information in conjunction with encoding information. This method takes
935 /// ownership of \p TAB.
937 /// \param ShowInst - Whether to show the MCInst representation inline with
939 MCStreamer *createAsmStreamer(MCContext &Ctx,
940 std::unique_ptr<formatted_raw_ostream> OS,
941 bool isVerboseAsm, bool useDwarfDirectory,
942 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
943 MCAsmBackend *TAB, bool ShowInst);
945 } // end namespace llvm
947 #endif // LLVM_MC_MCSTREAMER_H