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 finish();
101 // FIXME: declared here because it is used from
102 // lib/CodeGen/AsmPrinter/ARMException.cpp.
103 class ARMTargetStreamer : public MCTargetStreamer {
105 ARMTargetStreamer(MCStreamer &S);
106 ~ARMTargetStreamer() override;
108 virtual void emitFnStart();
109 virtual void emitFnEnd();
110 virtual void emitCantUnwind();
111 virtual void emitPersonality(const MCSymbol *Personality);
112 virtual void emitPersonalityIndex(unsigned Index);
113 virtual void emitHandlerData();
114 virtual void emitSetFP(unsigned FpReg, unsigned SpReg,
116 virtual void emitMovSP(unsigned Reg, int64_t Offset = 0);
117 virtual void emitPad(int64_t Offset);
118 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
120 virtual void emitUnwindRaw(int64_t StackOffset,
121 const SmallVectorImpl<uint8_t> &Opcodes);
123 virtual void switchVendor(StringRef Vendor);
124 virtual void emitAttribute(unsigned Attribute, unsigned Value);
125 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
126 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
127 StringRef StringValue = "");
128 virtual void emitFPU(unsigned FPU);
129 virtual void emitArch(ARM::ArchKind Arch);
130 virtual void emitArchExtension(unsigned ArchExt);
131 virtual void emitObjectArch(ARM::ArchKind Arch);
132 void emitTargetAttributes(const MCSubtargetInfo &STI);
133 virtual void finishAttributeSection();
134 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
136 virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
138 virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value);
140 void finish() override;
142 /// Reset any state between object emissions, i.e. the equivalent of
143 /// MCStreamer's reset method.
144 virtual void reset();
146 /// Callback used to implement the ldr= pseudo.
147 /// Add a new entry to the constant pool for the current section and return an
148 /// MCExpr that can be used to refer to the constant pool location.
149 const MCExpr *addConstantPoolEntry(const MCExpr *, SMLoc Loc);
151 /// Callback used to implemnt the .ltorg directive.
152 /// Emit contents of constant pool for the current section.
153 void emitCurrentConstantPool();
156 std::unique_ptr<AssemblerConstantPools> ConstantPools;
159 /// \brief Streaming machine code generation interface.
161 /// This interface is intended to provide a programatic interface that is very
162 /// similar to the level that an assembler .s file provides. It has callbacks
163 /// to emit bytes, handle directives, etc. The implementation of this interface
164 /// retains state to know what the current section is etc.
166 /// There are multiple implementations of this interface: one for writing out
167 /// a .s file, and implementations that write out .o files of various formats.
171 std::unique_ptr<MCTargetStreamer> TargetStreamer;
173 std::vector<MCDwarfFrameInfo> DwarfFrameInfos;
174 MCDwarfFrameInfo *getCurrentDwarfFrameInfo();
176 /// Similar to DwarfFrameInfos, but for SEH unwind info. Chained frames may
177 /// refer to each other, so use std::unique_ptr to provide pointer stability.
178 std::vector<std::unique_ptr<WinEH::FrameInfo>> WinFrameInfos;
180 WinEH::FrameInfo *CurrentWinFrameInfo;
182 /// Retreive the current frame info if one is available and it is not yet
183 /// closed. Otherwise, issue an error and return null.
184 WinEH::FrameInfo *EnsureValidWinFrameInfo(SMLoc Loc);
186 /// \brief Tracks an index to represent the order a symbol was emitted in.
187 /// Zero means we did not emit that symbol.
188 DenseMap<const MCSymbol *, unsigned> SymbolOrdering;
190 /// \brief This is stack of current and previous section values saved by
192 SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack;
194 /// The next unique ID to use when creating a WinCFI-related section (.pdata
195 /// or .xdata). This ID ensures that we have a one-to-one mapping from
196 /// code section to unwind info section, which MSVC's incremental linker
198 unsigned NextWinCFIID = 0;
201 MCStreamer(MCContext &Ctx);
203 virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
204 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
206 /// When emitting an object file, create and emit a real label. When emitting
207 /// textual assembly, this should do nothing to avoid polluting our output.
208 virtual MCSymbol *EmitCFILabel();
210 WinEH::FrameInfo *getCurrentWinFrameInfo() {
211 return CurrentWinFrameInfo;
214 virtual void EmitWindowsUnwindTables();
216 virtual void EmitRawTextImpl(StringRef String);
219 MCStreamer(const MCStreamer &) = delete;
220 MCStreamer &operator=(const MCStreamer &) = delete;
221 virtual ~MCStreamer();
223 void visitUsedExpr(const MCExpr &Expr);
224 virtual void visitUsedSymbol(const MCSymbol &Sym);
226 void setTargetStreamer(MCTargetStreamer *TS) {
227 TargetStreamer.reset(TS);
232 virtual void reset();
234 MCContext &getContext() const { return Context; }
236 MCTargetStreamer *getTargetStreamer() {
237 return TargetStreamer.get();
240 unsigned getNumFrameInfos() { return DwarfFrameInfos.size(); }
241 ArrayRef<MCDwarfFrameInfo> getDwarfFrameInfos() const {
242 return DwarfFrameInfos;
245 bool hasUnfinishedDwarfFrameInfo();
247 unsigned getNumWinFrameInfos() { return WinFrameInfos.size(); }
248 ArrayRef<std::unique_ptr<WinEH::FrameInfo>> getWinFrameInfos() const {
249 return WinFrameInfos;
252 void generateCompactUnwindEncodings(MCAsmBackend *MAB);
254 /// \name Assembly File Formatting.
257 /// \brief Return true if this streamer supports verbose assembly and if it is
259 virtual bool isVerboseAsm() const { return false; }
261 /// \brief Return true if this asm streamer supports emitting unformatted text
262 /// to the .s file with EmitRawText.
263 virtual bool hasRawTextSupport() const { return false; }
265 /// \brief Is the integrated assembler required for this streamer to function
267 virtual bool isIntegratedAssemblerRequired() const { return false; }
269 /// \brief Add a textual comment.
271 /// Typically for comments that can be emitted to the generated .s
272 /// file if applicable as a QoI issue to make the output of the compiler
273 /// more readable. This only affects the MCAsmStreamer, and only when
274 /// verbose assembly output is enabled.
276 /// If the comment includes embedded \n's, they will each get the comment
277 /// prefix as appropriate. The added comment should not end with a \n.
278 /// By default, each comment is terminated with an end of line, i.e. the
279 /// EOL param is set to true by default. If one prefers not to end the
280 /// comment with a new line then the EOL param should be passed
281 /// with a false value.
282 virtual void AddComment(const Twine &T, bool EOL = true) {}
284 /// \brief Return a raw_ostream that comments can be written to. Unlike
285 /// AddComment, you are required to terminate comments with \n if you use this
287 virtual raw_ostream &GetCommentOS();
289 /// \brief Print T and prefix it with the comment string (normally #) and
290 /// optionally a tab. This prints the comment immediately, not at the end of
291 /// the current line. It is basically a safe version of EmitRawText: since it
292 /// only prints comments, the object streamer ignores it instead of asserting.
293 virtual void emitRawComment(const Twine &T, bool TabPrefix = true);
295 /// \brief Add explicit comment T. T is required to be a valid
296 /// comment in the output and does not need to be escaped.
297 virtual void addExplicitComment(const Twine &T);
299 /// \brief Emit added explicit comments.
300 virtual void emitExplicitComments();
302 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
303 virtual void AddBlankLine() {}
307 /// \name Symbol & Section Management
310 /// \brief Return the current section that the streamer is emitting code to.
311 MCSectionSubPair getCurrentSection() const {
312 if (!SectionStack.empty())
313 return SectionStack.back().first;
314 return MCSectionSubPair();
316 MCSection *getCurrentSectionOnly() const { return getCurrentSection().first; }
318 /// \brief Return the previous section that the streamer is emitting code to.
319 MCSectionSubPair getPreviousSection() const {
320 if (!SectionStack.empty())
321 return SectionStack.back().second;
322 return MCSectionSubPair();
325 /// \brief Returns an index to represent the order a symbol was emitted in.
326 /// (zero if we did not emit that symbol)
327 unsigned GetSymbolOrder(const MCSymbol *Sym) const {
328 return SymbolOrdering.lookup(Sym);
331 /// \brief Update streamer for a new active section.
333 /// This is called by PopSection and SwitchSection, if the current
335 virtual void ChangeSection(MCSection *, const MCExpr *);
337 /// \brief Save the current and previous section on the section stack.
339 SectionStack.push_back(
340 std::make_pair(getCurrentSection(), getPreviousSection()));
343 /// \brief Restore the current and previous section from the section stack.
344 /// Calls ChangeSection as needed.
346 /// Returns false if the stack was empty.
348 if (SectionStack.size() <= 1)
350 auto I = SectionStack.end();
352 MCSectionSubPair OldSection = I->first;
354 MCSectionSubPair NewSection = I->first;
356 if (OldSection != NewSection)
357 ChangeSection(NewSection.first, NewSection.second);
358 SectionStack.pop_back();
362 bool SubSection(const MCExpr *Subsection) {
363 if (SectionStack.empty())
366 SwitchSection(SectionStack.back().first.first, Subsection);
370 /// Set the current section where code is being emitted to \p Section. This
371 /// is required to update CurSection.
373 /// This corresponds to assembler directives like .section, .text, etc.
374 virtual void SwitchSection(MCSection *Section,
375 const MCExpr *Subsection = nullptr);
377 /// \brief Set the current section where code is being emitted to \p Section.
378 /// This is required to update CurSection. This version does not call
380 void SwitchSectionNoChange(MCSection *Section,
381 const MCExpr *Subsection = nullptr) {
382 assert(Section && "Cannot switch to a null section!");
383 MCSectionSubPair curSection = SectionStack.back().first;
384 SectionStack.back().second = curSection;
385 if (MCSectionSubPair(Section, Subsection) != curSection)
386 SectionStack.back().first = MCSectionSubPair(Section, Subsection);
389 /// \brief Create the default sections and set the initial one.
390 virtual void InitSections(bool NoExecStack);
392 MCSymbol *endSection(MCSection *Section);
394 /// \brief Sets the symbol's section.
396 /// Each emitted symbol will be tracked in the ordering table,
397 /// so we can sort on them later.
398 void AssignFragment(MCSymbol *Symbol, MCFragment *Fragment);
400 /// \brief Emit a label for \p Symbol into the current section.
402 /// This corresponds to an assembler statement such as:
405 /// \param Symbol - The symbol to emit. A given symbol should only be
406 /// emitted as a label once, and symbols emitted as a label should never be
407 /// used in an assignment.
408 // FIXME: These emission are non-const because we mutate the symbol to
409 // add the section we're emitting it to later.
410 virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc());
412 virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol);
414 /// \brief Note in the output the specified \p Flag.
415 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
417 /// \brief Emit the given list \p Options of strings as linker
418 /// options into the output.
419 virtual void EmitLinkerOptions(ArrayRef<std::string> Kind) {}
421 /// \brief Note in the output the specified region \p Kind.
422 virtual void EmitDataRegion(MCDataRegionType Kind) {}
424 /// \brief Specify the Mach-O minimum deployment target version.
425 virtual void EmitVersionMin(MCVersionMinType Type, unsigned Major,
426 unsigned Minor, unsigned Update) {}
428 /// Emit/Specify Mach-O build version command.
429 /// \p Platform should be one of MachO::PlatformType.
430 virtual void EmitBuildVersion(unsigned Platform, unsigned Major,
431 unsigned Minor, unsigned Update) {}
433 void EmitVersionForTarget(const Triple &Target);
435 /// \brief Note in the output that the specified \p Func is a Thumb mode
436 /// function (ARM target only).
437 virtual void EmitThumbFunc(MCSymbol *Func);
439 /// \brief Emit an assignment of \p Value to \p Symbol.
441 /// This corresponds to an assembler statement such as:
444 /// The assignment generates no code, but has the side effect of binding the
445 /// value in the current context. For the assembly streamer, this prints the
446 /// binding into the .s file.
448 /// \param Symbol - The symbol being assigned to.
449 /// \param Value - The value for the symbol.
450 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
452 /// \brief Emit an weak reference from \p Alias to \p Symbol.
454 /// This corresponds to an assembler statement such as:
455 /// .weakref alias, symbol
457 /// \param Alias - The alias that is being created.
458 /// \param Symbol - The symbol being aliased.
459 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
461 /// \brief Add the given \p Attribute to \p Symbol.
462 virtual bool EmitSymbolAttribute(MCSymbol *Symbol,
463 MCSymbolAttr Attribute) = 0;
465 /// \brief Set the \p DescValue for the \p Symbol.
467 /// \param Symbol - The symbol to have its n_desc field set.
468 /// \param DescValue - The value to set into the n_desc field.
469 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
471 /// \brief Start emitting COFF symbol definition
473 /// \param Symbol - The symbol to have its External & Type fields set.
474 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
476 /// \brief Emit the storage class of the symbol.
478 /// \param StorageClass - The storage class the symbol should have.
479 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
481 /// \brief Emit the type of the symbol.
483 /// \param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
484 virtual void EmitCOFFSymbolType(int Type);
486 /// \brief Marks the end of the symbol definition.
487 virtual void EndCOFFSymbolDef();
489 virtual void EmitCOFFSafeSEH(MCSymbol const *Symbol);
491 /// \brief Emits a COFF section index.
493 /// \param Symbol - Symbol the section number relocation should point to.
494 virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
496 /// \brief Emits a COFF section relative relocation.
498 /// \param Symbol - Symbol the section relative relocation should point to.
499 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset);
501 /// \brief Emit an ELF .size directive.
503 /// This corresponds to an assembler statement such as:
504 /// .size symbol, expression
505 virtual void emitELFSize(MCSymbol *Symbol, const MCExpr *Value);
507 /// \brief Emit an ELF .symver directive.
509 /// This corresponds to an assembler statement such as:
510 /// .symver _start, foo@@SOME_VERSION
511 /// \param Alias - The versioned alias (i.e. "foo@@SOME_VERSION")
512 /// \param Aliasee - The aliased symbol (i.e. "_start")
513 virtual void emitELFSymverDirective(MCSymbol *Alias, const MCSymbol *Aliasee);
515 /// \brief Emit a Linker Optimization Hint (LOH) directive.
516 /// \param Args - Arguments of the LOH.
517 virtual void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {}
519 /// \brief Emit a common symbol.
521 /// \param Symbol - The common symbol to emit.
522 /// \param Size - The size of the common symbol.
523 /// \param ByteAlignment - The alignment of the symbol if
524 /// non-zero. This must be a power of 2.
525 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
526 unsigned ByteAlignment) = 0;
528 /// \brief Emit a local common (.lcomm) symbol.
530 /// \param Symbol - The common symbol to emit.
531 /// \param Size - The size of the common symbol.
532 /// \param ByteAlignment - The alignment of the common symbol in bytes.
533 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
534 unsigned ByteAlignment);
536 /// \brief Emit the zerofill section and an optional symbol.
538 /// \param Section - The zerofill section to create and or to put the symbol
539 /// \param Symbol - The zerofill symbol to emit, if non-NULL.
540 /// \param Size - The size of the zerofill symbol.
541 /// \param ByteAlignment - The alignment of the zerofill symbol if
542 /// non-zero. This must be a power of 2 on some targets.
543 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
544 uint64_t Size = 0, unsigned ByteAlignment = 0) = 0;
546 /// \brief Emit a thread local bss (.tbss) symbol.
548 /// \param Section - The thread local common section.
549 /// \param Symbol - The thread local common symbol to emit.
550 /// \param Size - The size of the symbol.
551 /// \param ByteAlignment - The alignment of the thread local common symbol
552 /// if non-zero. This must be a power of 2 on some targets.
553 virtual void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
554 uint64_t Size, unsigned ByteAlignment = 0);
557 /// \name Generating Data
560 /// \brief Emit the bytes in \p Data into the output.
562 /// This is used to implement assembler directives such as .byte, .ascii,
564 virtual void EmitBytes(StringRef Data);
566 /// Functionally identical to EmitBytes. When emitting textual assembly, this
567 /// method uses .byte directives instead of .ascii or .asciz for readability.
568 virtual void EmitBinaryData(StringRef Data);
570 /// \brief Emit the expression \p Value into the output as a native
571 /// integer of the given \p Size bytes.
573 /// This is used to implement assembler directives such as .word, .quad,
576 /// \param Value - The value to emit.
577 /// \param Size - The size of the integer (in bytes) to emit. This must
578 /// match a native machine width.
579 /// \param Loc - The location of the expression for error reporting.
580 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
581 SMLoc Loc = SMLoc());
583 void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc = SMLoc());
585 /// \brief Special case of EmitValue that avoids the client having
586 /// to pass in a MCExpr for constant integers.
587 virtual void EmitIntValue(uint64_t Value, unsigned Size);
589 virtual void EmitULEB128Value(const MCExpr *Value);
591 virtual void EmitSLEB128Value(const MCExpr *Value);
593 /// \brief Special case of EmitULEB128Value that avoids the client having to
594 /// pass in a MCExpr for constant integers.
595 void EmitULEB128IntValue(uint64_t Value);
597 /// \brief Like EmitULEB128Value but pads the output to specific number of
599 void EmitPaddedULEB128IntValue(uint64_t Value, unsigned PadTo);
601 /// \brief Special case of EmitSLEB128Value that avoids the client having to
602 /// pass in a MCExpr for constant integers.
603 void EmitSLEB128IntValue(int64_t Value);
605 /// \brief Special case of EmitValue that avoids the client having to pass in
606 /// a MCExpr for MCSymbols.
607 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
608 bool IsSectionRelative = false);
610 /// \brief Emit the expression \p Value into the output as a dtprel
611 /// (64-bit DTP relative) value.
613 /// This is used to implement assembler directives such as .dtpreldword on
614 /// targets that support them.
615 virtual void EmitDTPRel64Value(const MCExpr *Value);
617 /// \brief Emit the expression \p Value into the output as a dtprel
618 /// (32-bit DTP relative) value.
620 /// This is used to implement assembler directives such as .dtprelword on
621 /// targets that support them.
622 virtual void EmitDTPRel32Value(const MCExpr *Value);
624 /// \brief Emit the expression \p Value into the output as a tprel
625 /// (64-bit TP relative) value.
627 /// This is used to implement assembler directives such as .tpreldword on
628 /// targets that support them.
629 virtual void EmitTPRel64Value(const MCExpr *Value);
631 /// \brief Emit the expression \p Value into the output as a tprel
632 /// (32-bit TP relative) value.
634 /// This is used to implement assembler directives such as .tprelword on
635 /// targets that support them.
636 virtual void EmitTPRel32Value(const MCExpr *Value);
638 /// \brief Emit the expression \p Value into the output as a gprel64 (64-bit
639 /// GP relative) value.
641 /// This is used to implement assembler directives such as .gpdword on
642 /// targets that support them.
643 virtual void EmitGPRel64Value(const MCExpr *Value);
645 /// \brief Emit the expression \p Value into the output as a gprel32 (32-bit
646 /// GP relative) value.
648 /// This is used to implement assembler directives such as .gprel32 on
649 /// targets that support them.
650 virtual void EmitGPRel32Value(const MCExpr *Value);
652 /// \brief Emit NumBytes bytes worth of the value specified by FillValue.
653 /// This implements directives such as '.space'.
654 virtual void emitFill(uint64_t NumBytes, uint8_t FillValue);
656 /// \brief Emit \p Size bytes worth of the value specified by \p FillValue.
658 /// This is used to implement assembler directives such as .space or .skip.
660 /// \param NumBytes - The number of bytes to emit.
661 /// \param FillValue - The value to use when filling bytes.
662 /// \param Loc - The location of the expression for error reporting.
663 virtual void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
664 SMLoc Loc = SMLoc());
666 /// \brief Emit \p NumValues copies of \p Size bytes. Each \p Size bytes is
667 /// taken from the lowest order 4 bytes of \p Expr expression.
669 /// This is used to implement assembler directives such as .fill.
671 /// \param NumValues - The number of copies of \p Size bytes to emit.
672 /// \param Size - The size (in bytes) of each repeated value.
673 /// \param Expr - The expression from which \p Size bytes are used.
674 virtual void emitFill(uint64_t NumValues, int64_t Size, int64_t Expr);
675 virtual void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
676 SMLoc Loc = SMLoc());
678 /// \brief Emit NumBytes worth of zeros.
679 /// This function properly handles data in virtual sections.
680 void EmitZeros(uint64_t NumBytes);
682 /// \brief Emit some number of copies of \p Value until the byte alignment \p
683 /// ByteAlignment is reached.
685 /// If the number of bytes need to emit for the alignment is not a multiple
686 /// of \p ValueSize, then the contents of the emitted fill bytes is
689 /// This used to implement the .align assembler directive.
691 /// \param ByteAlignment - The alignment to reach. This must be a power of
692 /// two on some targets.
693 /// \param Value - The value to use when filling bytes.
694 /// \param ValueSize - The size of the integer (in bytes) to emit for
695 /// \p Value. This must match a native machine width.
696 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
697 /// the alignment cannot be reached in this many bytes, no bytes are
699 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
700 unsigned ValueSize = 1,
701 unsigned MaxBytesToEmit = 0);
703 /// \brief Emit nops until the byte alignment \p ByteAlignment is reached.
705 /// This used to align code where the alignment bytes may be executed. This
706 /// can emit different bytes for different sizes to optimize execution.
708 /// \param ByteAlignment - The alignment to reach. This must be a power of
709 /// two on some targets.
710 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
711 /// the alignment cannot be reached in this many bytes, no bytes are
713 virtual void EmitCodeAlignment(unsigned ByteAlignment,
714 unsigned MaxBytesToEmit = 0);
716 /// \brief Emit some number of copies of \p Value until the byte offset \p
717 /// Offset is reached.
719 /// This is used to implement assembler directives such as .org.
721 /// \param Offset - The offset to reach. This may be an expression, but the
722 /// expression must be associated with the current section.
723 /// \param Value - The value to use when filling bytes.
724 virtual void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
728 EmitCodePaddingBasicBlockStart(const MCCodePaddingContext &Context) {}
731 EmitCodePaddingBasicBlockEnd(const MCCodePaddingContext &Context) {}
735 /// \brief Switch to a new logical file. This is used to implement the '.file
736 /// "foo.c"' assembler directive.
737 virtual void EmitFileDirective(StringRef Filename);
739 /// \brief Emit the "identifiers" directive. This implements the
740 /// '.ident "version foo"' assembler directive.
741 virtual void EmitIdent(StringRef IdentString) {}
743 /// \brief Associate a filename with a specified logical file number. This
744 /// implements the DWARF2 '.file 4 "foo.c"' assembler directive.
745 virtual unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
749 /// \brief This implements the DWARF2 '.loc fileno lineno ...' assembler
751 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
752 unsigned Column, unsigned Flags,
753 unsigned Isa, unsigned Discriminator,
756 /// Associate a filename with a specified logical file number, and also
757 /// specify that file's checksum information. This implements the '.cv_file 4
758 /// "foo.c"' assembler directive. Returns true on success.
759 virtual bool EmitCVFileDirective(unsigned FileNo, StringRef Filename,
760 ArrayRef<uint8_t> Checksum,
761 unsigned ChecksumKind);
763 /// \brief Introduces a function id for use with .cv_loc.
764 virtual bool EmitCVFuncIdDirective(unsigned FunctionId);
766 /// \brief Introduces an inline call site id for use with .cv_loc. Includes
767 /// extra information for inline line table generation.
768 virtual bool EmitCVInlineSiteIdDirective(unsigned FunctionId, unsigned IAFunc,
769 unsigned IAFile, unsigned IALine,
770 unsigned IACol, SMLoc Loc);
772 /// \brief This implements the CodeView '.cv_loc' assembler directive.
773 virtual void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo,
774 unsigned Line, unsigned Column,
775 bool PrologueEnd, bool IsStmt,
776 StringRef FileName, SMLoc Loc);
778 /// \brief This implements the CodeView '.cv_linetable' assembler directive.
779 virtual void EmitCVLinetableDirective(unsigned FunctionId,
780 const MCSymbol *FnStart,
781 const MCSymbol *FnEnd);
783 /// \brief This implements the CodeView '.cv_inline_linetable' assembler
785 virtual void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
786 unsigned SourceFileId,
787 unsigned SourceLineNum,
788 const MCSymbol *FnStartSym,
789 const MCSymbol *FnEndSym);
791 /// \brief This implements the CodeView '.cv_def_range' assembler
793 virtual void EmitCVDefRangeDirective(
794 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
795 StringRef FixedSizePortion);
797 /// \brief This implements the CodeView '.cv_stringtable' assembler directive.
798 virtual void EmitCVStringTableDirective() {}
800 /// \brief This implements the CodeView '.cv_filechecksums' assembler directive.
801 virtual void EmitCVFileChecksumsDirective() {}
803 /// This implements the CodeView '.cv_filechecksumoffset' assembler
805 virtual void EmitCVFileChecksumOffsetDirective(unsigned FileNo) {}
807 /// This implements the CodeView '.cv_fpo_data' assembler directive.
808 virtual void EmitCVFPOData(const MCSymbol *ProcSym, SMLoc Loc = {}) {}
810 /// Emit the absolute difference between two symbols.
812 /// \pre Offset of \c Hi is greater than the offset \c Lo.
813 virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
816 virtual MCSymbol *getDwarfLineTableSymbol(unsigned CUID);
817 virtual void EmitCFISections(bool EH, bool Debug);
818 void EmitCFIStartProc(bool IsSimple);
819 void EmitCFIEndProc();
820 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
821 virtual void EmitCFIDefCfaOffset(int64_t Offset);
822 virtual void EmitCFIDefCfaRegister(int64_t Register);
823 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
824 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
825 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
826 virtual void EmitCFIRememberState();
827 virtual void EmitCFIRestoreState();
828 virtual void EmitCFISameValue(int64_t Register);
829 virtual void EmitCFIRestore(int64_t Register);
830 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
831 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
832 virtual void EmitCFIEscape(StringRef Values);
833 virtual void EmitCFIReturnColumn(int64_t Register);
834 virtual void EmitCFIGnuArgsSize(int64_t Size);
835 virtual void EmitCFISignalFrame();
836 virtual void EmitCFIUndefined(int64_t Register);
837 virtual void EmitCFIRegister(int64_t Register1, int64_t Register2);
838 virtual void EmitCFIWindowSave();
840 virtual void EmitWinCFIStartProc(const MCSymbol *Symbol, SMLoc Loc = SMLoc());
841 virtual void EmitWinCFIEndProc(SMLoc Loc = SMLoc());
842 virtual void EmitWinCFIStartChained(SMLoc Loc = SMLoc());
843 virtual void EmitWinCFIEndChained(SMLoc Loc = SMLoc());
844 virtual void EmitWinCFIPushReg(unsigned Register, SMLoc Loc = SMLoc());
845 virtual void EmitWinCFISetFrame(unsigned Register, unsigned Offset,
846 SMLoc Loc = SMLoc());
847 virtual void EmitWinCFIAllocStack(unsigned Size, SMLoc Loc = SMLoc());
848 virtual void EmitWinCFISaveReg(unsigned Register, unsigned Offset,
849 SMLoc Loc = SMLoc());
850 virtual void EmitWinCFISaveXMM(unsigned Register, unsigned Offset,
851 SMLoc Loc = SMLoc());
852 virtual void EmitWinCFIPushFrame(bool Code, SMLoc Loc = SMLoc());
853 virtual void EmitWinCFIEndProlog(SMLoc Loc = SMLoc());
854 virtual void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except,
855 SMLoc Loc = SMLoc());
856 virtual void EmitWinEHHandlerData(SMLoc Loc = SMLoc());
858 /// Get the .pdata section used for the given section. Typically the given
859 /// section is either the main .text section or some other COMDAT .text
860 /// section, but it may be any section containing code.
861 MCSection *getAssociatedPDataSection(const MCSection *TextSec);
863 /// Get the .xdata section used for the given section.
864 MCSection *getAssociatedXDataSection(const MCSection *TextSec);
866 virtual void EmitSyntaxDirective();
868 /// \brief Emit a .reloc directive.
869 /// Returns true if the relocation could not be emitted because Name is not
871 virtual bool EmitRelocDirective(const MCExpr &Offset, StringRef Name,
872 const MCExpr *Expr, SMLoc Loc) {
876 /// \brief Emit the given \p Instruction into the current section.
877 /// PrintSchedInfo == true then schedul comment should be added to output
878 virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
879 bool PrintSchedInfo = false);
881 /// \brief Set the bundle alignment mode from now on in the section.
882 /// The argument is the power of 2 to which the alignment is set. The
883 /// value 0 means turn the bundle alignment off.
884 virtual void EmitBundleAlignMode(unsigned AlignPow2);
886 /// \brief The following instructions are a bundle-locked group.
888 /// \param AlignToEnd - If true, the bundle-locked group will be aligned to
889 /// the end of a bundle.
890 virtual void EmitBundleLock(bool AlignToEnd);
892 /// \brief Ends a bundle-locked group.
893 virtual void EmitBundleUnlock();
895 /// \brief If this file is backed by a assembly streamer, this dumps the
896 /// specified string in the output .s file. This capability is indicated by
897 /// the hasRawTextSupport() predicate. By default this aborts.
898 void EmitRawText(const Twine &String);
900 /// \brief Streamer specific finalization.
901 virtual void FinishImpl();
902 /// \brief Finish emission of machine code.
905 virtual bool mayHaveInstructions(MCSection &Sec) const { return true; }
908 /// Create a dummy machine code streamer, which does nothing. This is useful for
909 /// timing the assembler front end.
910 MCStreamer *createNullStreamer(MCContext &Ctx);
912 /// Create a machine code streamer which will print out assembly for the native
913 /// target, suitable for compiling with a native assembler.
915 /// \param InstPrint - If given, the instruction printer to use. If not given
916 /// the MCInst representation will be printed. This method takes ownership of
919 /// \param CE - If given, a code emitter to use to show the instruction
920 /// encoding inline with the assembly. This method takes ownership of \p CE.
922 /// \param TAB - If given, a target asm backend to use to show the fixup
923 /// information in conjunction with encoding information. This method takes
924 /// ownership of \p TAB.
926 /// \param ShowInst - Whether to show the MCInst representation inline with
928 MCStreamer *createAsmStreamer(MCContext &Ctx,
929 std::unique_ptr<formatted_raw_ostream> OS,
930 bool isVerboseAsm, bool useDwarfDirectory,
931 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
932 MCAsmBackend *TAB, bool ShowInst);
934 } // end namespace llvm
936 #endif // LLVM_MC_MCSTREAMER_H