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"
36 class AssemblerConstantPools;
37 class formatted_raw_ostream;
46 class MCSymbolRefExpr;
48 class MCSubtargetInfo;
52 typedef std::pair<MCSection *, const MCExpr *> MCSectionSubPair;
54 /// Target specific streamer interface. This is used so that targets can
55 /// implement support for target specific assembly directives.
57 /// If target foo wants to use this, it should implement 3 classes:
58 /// * FooTargetStreamer : public MCTargetStreamer
59 /// * FooTargetAsmStreamer : public FooTargetStreamer
60 /// * FooTargetELFStreamer : public FooTargetStreamer
62 /// FooTargetStreamer should have a pure virtual method for each directive. For
63 /// example, for a ".bar symbol_name" directive, it should have
64 /// virtual emitBar(const MCSymbol &Symbol) = 0;
66 /// The FooTargetAsmStreamer and FooTargetELFStreamer classes implement the
67 /// method. The assembly streamer just prints ".bar symbol_name". The object
68 /// streamer does whatever is needed to implement .bar in the object file.
70 /// In the assembly printer and parser the target streamer can be used by
71 /// calling getTargetStreamer and casting it to FooTargetStreamer:
73 /// MCTargetStreamer &TS = OutStreamer.getTargetStreamer();
74 /// FooTargetStreamer &ATS = static_cast<FooTargetStreamer &>(TS);
76 /// The base classes FooTargetAsmStreamer and FooTargetELFStreamer should
77 /// *never* be treated differently. Callers should always talk to a
78 /// FooTargetStreamer.
79 class MCTargetStreamer {
84 MCTargetStreamer(MCStreamer &S);
85 virtual ~MCTargetStreamer();
87 MCStreamer &getStreamer() { return Streamer; }
89 // Allow a target to add behavior to the EmitLabel of MCStreamer.
90 virtual void emitLabel(MCSymbol *Symbol);
91 // Allow a target to add behavior to the emitAssignment of MCStreamer.
92 virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value);
94 virtual void prettyPrintAsm(MCInstPrinter &InstPrinter, raw_ostream &OS,
95 const MCInst &Inst, const MCSubtargetInfo &STI);
97 virtual void finish();
100 // FIXME: declared here because it is used from
101 // lib/CodeGen/AsmPrinter/ARMException.cpp.
102 class ARMTargetStreamer : public MCTargetStreamer {
104 ARMTargetStreamer(MCStreamer &S);
105 ~ARMTargetStreamer() override;
107 virtual void emitFnStart();
108 virtual void emitFnEnd();
109 virtual void emitCantUnwind();
110 virtual void emitPersonality(const MCSymbol *Personality);
111 virtual void emitPersonalityIndex(unsigned Index);
112 virtual void emitHandlerData();
113 virtual void emitSetFP(unsigned FpReg, unsigned SpReg,
115 virtual void emitMovSP(unsigned Reg, int64_t Offset = 0);
116 virtual void emitPad(int64_t Offset);
117 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
119 virtual void emitUnwindRaw(int64_t StackOffset,
120 const SmallVectorImpl<uint8_t> &Opcodes);
122 virtual void switchVendor(StringRef Vendor);
123 virtual void emitAttribute(unsigned Attribute, unsigned Value);
124 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
125 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
126 StringRef StringValue = "");
127 virtual void emitFPU(unsigned FPU);
128 virtual void emitArch(unsigned Arch);
129 virtual void emitArchExtension(unsigned ArchExt);
130 virtual void emitObjectArch(unsigned Arch);
131 void emitTargetAttributes(const MCSubtargetInfo &STI);
132 virtual void finishAttributeSection();
133 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
135 virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
137 virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value);
139 void finish() override;
141 /// Reset any state between object emissions, i.e. the equivalent of
142 /// MCStreamer's reset method.
143 virtual void reset();
145 /// Callback used to implement the ldr= pseudo.
146 /// Add a new entry to the constant pool for the current section and return an
147 /// MCExpr that can be used to refer to the constant pool location.
148 const MCExpr *addConstantPoolEntry(const MCExpr *, SMLoc Loc);
150 /// Callback used to implemnt the .ltorg directive.
151 /// Emit contents of constant pool for the current section.
152 void emitCurrentConstantPool();
155 std::unique_ptr<AssemblerConstantPools> ConstantPools;
158 /// \brief Streaming machine code generation interface.
160 /// This interface is intended to provide a programatic interface that is very
161 /// similar to the level that an assembler .s file provides. It has callbacks
162 /// to emit bytes, handle directives, etc. The implementation of this interface
163 /// retains state to know what the current section is etc.
165 /// There are multiple implementations of this interface: one for writing out
166 /// a .s file, and implementations that write out .o files of various formats.
170 std::unique_ptr<MCTargetStreamer> TargetStreamer;
172 std::vector<MCDwarfFrameInfo> DwarfFrameInfos;
173 MCDwarfFrameInfo *getCurrentDwarfFrameInfo();
174 void EnsureValidDwarfFrame();
176 MCSymbol *EmitCFILabel();
177 MCSymbol *EmitCFICommon();
179 std::vector<WinEH::FrameInfo *> WinFrameInfos;
180 WinEH::FrameInfo *CurrentWinFrameInfo;
181 void EnsureValidWinFrameInfo();
183 /// \brief Tracks an index to represent the order a symbol was emitted in.
184 /// Zero means we did not emit that symbol.
185 DenseMap<const MCSymbol *, unsigned> SymbolOrdering;
187 /// \brief This is stack of current and previous section values saved by
189 SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack;
191 /// The next unique ID to use when creating a WinCFI-related section (.pdata
192 /// or .xdata). This ID ensures that we have a one-to-one mapping from
193 /// code section to unwind info section, which MSVC's incremental linker
195 unsigned NextWinCFIID = 0;
198 MCStreamer(MCContext &Ctx);
200 virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
201 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
203 WinEH::FrameInfo *getCurrentWinFrameInfo() {
204 return CurrentWinFrameInfo;
207 virtual void EmitWindowsUnwindTables();
209 virtual void EmitRawTextImpl(StringRef String);
212 MCStreamer(const MCStreamer &) = delete;
213 MCStreamer &operator=(const MCStreamer &) = delete;
214 virtual ~MCStreamer();
216 void visitUsedExpr(const MCExpr &Expr);
217 virtual void visitUsedSymbol(const MCSymbol &Sym);
219 void setTargetStreamer(MCTargetStreamer *TS) {
220 TargetStreamer.reset(TS);
225 virtual void reset();
227 MCContext &getContext() const { return Context; }
229 MCTargetStreamer *getTargetStreamer() {
230 return TargetStreamer.get();
233 unsigned getNumFrameInfos() { return DwarfFrameInfos.size(); }
234 ArrayRef<MCDwarfFrameInfo> getDwarfFrameInfos() const {
235 return DwarfFrameInfos;
238 bool hasUnfinishedDwarfFrameInfo();
240 unsigned getNumWinFrameInfos() { return WinFrameInfos.size(); }
241 ArrayRef<WinEH::FrameInfo *> getWinFrameInfos() const {
242 return WinFrameInfos;
245 void generateCompactUnwindEncodings(MCAsmBackend *MAB);
247 /// \name Assembly File Formatting.
250 /// \brief Return true if this streamer supports verbose assembly and if it is
252 virtual bool isVerboseAsm() const { return false; }
254 /// \brief Return true if this asm streamer supports emitting unformatted text
255 /// to the .s file with EmitRawText.
256 virtual bool hasRawTextSupport() const { return false; }
258 /// \brief Is the integrated assembler required for this streamer to function
260 virtual bool isIntegratedAssemblerRequired() const { return false; }
262 /// \brief Add a textual comment.
264 /// Typically for comments that can be emitted to the generated .s
265 /// file if applicable as a QoI issue to make the output of the compiler
266 /// more readable. This only affects the MCAsmStreamer, and only when
267 /// verbose assembly output is enabled.
269 /// If the comment includes embedded \n's, they will each get the comment
270 /// prefix as appropriate. The added comment should not end with a \n.
271 /// By default, each comment is terminated with an end of line, i.e. the
272 /// EOL param is set to true by default. If one prefers not to end the
273 /// comment with a new line then the EOL param should be passed
274 /// with a false value.
275 virtual void AddComment(const Twine &T, bool EOL = true) {}
277 /// \brief Return a raw_ostream that comments can be written to. Unlike
278 /// AddComment, you are required to terminate comments with \n if you use this
280 virtual raw_ostream &GetCommentOS();
282 /// \brief Print T and prefix it with the comment string (normally #) and
283 /// optionally a tab. This prints the comment immediately, not at the end of
284 /// the current line. It is basically a safe version of EmitRawText: since it
285 /// only prints comments, the object streamer ignores it instead of asserting.
286 virtual void emitRawComment(const Twine &T, bool TabPrefix = true);
288 /// \brief Add explicit comment T. T is required to be a valid
289 /// comment in the output and does not need to be escaped.
290 virtual void addExplicitComment(const Twine &T);
292 /// \brief Emit added explicit comments.
293 virtual void emitExplicitComments();
295 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
296 virtual void AddBlankLine() {}
300 /// \name Symbol & Section Management
303 /// \brief Return the current section that the streamer is emitting code to.
304 MCSectionSubPair getCurrentSection() const {
305 if (!SectionStack.empty())
306 return SectionStack.back().first;
307 return MCSectionSubPair();
309 MCSection *getCurrentSectionOnly() const { return getCurrentSection().first; }
311 /// \brief Return the previous section that the streamer is emitting code to.
312 MCSectionSubPair getPreviousSection() const {
313 if (!SectionStack.empty())
314 return SectionStack.back().second;
315 return MCSectionSubPair();
318 /// \brief Returns an index to represent the order a symbol was emitted in.
319 /// (zero if we did not emit that symbol)
320 unsigned GetSymbolOrder(const MCSymbol *Sym) const {
321 return SymbolOrdering.lookup(Sym);
324 /// \brief Update streamer for a new active section.
326 /// This is called by PopSection and SwitchSection, if the current
328 virtual void ChangeSection(MCSection *, const MCExpr *);
330 /// \brief Save the current and previous section on the section stack.
332 SectionStack.push_back(
333 std::make_pair(getCurrentSection(), getPreviousSection()));
336 /// \brief Restore the current and previous section from the section stack.
337 /// Calls ChangeSection as needed.
339 /// Returns false if the stack was empty.
341 if (SectionStack.size() <= 1)
343 auto I = SectionStack.end();
345 MCSectionSubPair OldSection = I->first;
347 MCSectionSubPair NewSection = I->first;
349 if (OldSection != NewSection)
350 ChangeSection(NewSection.first, NewSection.second);
351 SectionStack.pop_back();
355 bool SubSection(const MCExpr *Subsection) {
356 if (SectionStack.empty())
359 SwitchSection(SectionStack.back().first.first, Subsection);
363 /// Set the current section where code is being emitted to \p Section. This
364 /// is required to update CurSection.
366 /// This corresponds to assembler directives like .section, .text, etc.
367 virtual void SwitchSection(MCSection *Section,
368 const MCExpr *Subsection = nullptr);
370 /// \brief Set the current section where code is being emitted to \p Section.
371 /// This is required to update CurSection. This version does not call
373 void SwitchSectionNoChange(MCSection *Section,
374 const MCExpr *Subsection = nullptr) {
375 assert(Section && "Cannot switch to a null section!");
376 MCSectionSubPair curSection = SectionStack.back().first;
377 SectionStack.back().second = curSection;
378 if (MCSectionSubPair(Section, Subsection) != curSection)
379 SectionStack.back().first = MCSectionSubPair(Section, Subsection);
382 /// \brief Create the default sections and set the initial one.
383 virtual void InitSections(bool NoExecStack);
385 MCSymbol *endSection(MCSection *Section);
387 /// \brief Sets the symbol's section.
389 /// Each emitted symbol will be tracked in the ordering table,
390 /// so we can sort on them later.
391 void AssignFragment(MCSymbol *Symbol, MCFragment *Fragment);
393 /// \brief Emit a label for \p Symbol into the current section.
395 /// This corresponds to an assembler statement such as:
398 /// \param Symbol - The symbol to emit. A given symbol should only be
399 /// emitted as a label once, and symbols emitted as a label should never be
400 /// used in an assignment.
401 // FIXME: These emission are non-const because we mutate the symbol to
402 // add the section we're emitting it to later.
403 virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc());
405 virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol);
407 /// \brief Note in the output the specified \p Flag.
408 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
410 /// \brief Emit the given list \p Options of strings as linker
411 /// options into the output.
412 virtual void EmitLinkerOptions(ArrayRef<std::string> Kind) {}
414 /// \brief Note in the output the specified region \p Kind.
415 virtual void EmitDataRegion(MCDataRegionType Kind) {}
417 /// \brief Specify the MachO minimum deployment target version.
418 virtual void EmitVersionMin(MCVersionMinType, unsigned Major, unsigned Minor,
421 /// \brief Note in the output that the specified \p Func is a Thumb mode
422 /// function (ARM target only).
423 virtual void EmitThumbFunc(MCSymbol *Func);
425 /// \brief Emit an assignment of \p Value to \p Symbol.
427 /// This corresponds to an assembler statement such as:
430 /// The assignment generates no code, but has the side effect of binding the
431 /// value in the current context. For the assembly streamer, this prints the
432 /// binding into the .s file.
434 /// \param Symbol - The symbol being assigned to.
435 /// \param Value - The value for the symbol.
436 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
438 /// \brief Emit an weak reference from \p Alias to \p Symbol.
440 /// This corresponds to an assembler statement such as:
441 /// .weakref alias, symbol
443 /// \param Alias - The alias that is being created.
444 /// \param Symbol - The symbol being aliased.
445 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
447 /// \brief Add the given \p Attribute to \p Symbol.
448 virtual bool EmitSymbolAttribute(MCSymbol *Symbol,
449 MCSymbolAttr Attribute) = 0;
451 /// \brief Set the \p DescValue for the \p Symbol.
453 /// \param Symbol - The symbol to have its n_desc field set.
454 /// \param DescValue - The value to set into the n_desc field.
455 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
457 /// \brief Start emitting COFF symbol definition
459 /// \param Symbol - The symbol to have its External & Type fields set.
460 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
462 /// \brief Emit the storage class of the symbol.
464 /// \param StorageClass - The storage class the symbol should have.
465 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
467 /// \brief Emit the type of the symbol.
469 /// \param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
470 virtual void EmitCOFFSymbolType(int Type);
472 /// \brief Marks the end of the symbol definition.
473 virtual void EndCOFFSymbolDef();
475 virtual void EmitCOFFSafeSEH(MCSymbol const *Symbol);
477 /// \brief Emits a COFF section index.
479 /// \param Symbol - Symbol the section number relocation should point to.
480 virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
482 /// \brief Emits a COFF section relative relocation.
484 /// \param Symbol - Symbol the section relative relocation should point to.
485 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset);
487 /// \brief Emit an ELF .size directive.
489 /// This corresponds to an assembler statement such as:
490 /// .size symbol, expression
491 virtual void emitELFSize(MCSymbol *Symbol, const MCExpr *Value);
493 /// \brief Emit an ELF .symver directive.
495 /// This corresponds to an assembler statement such as:
496 /// .symver _start, foo@@SOME_VERSION
497 /// \param Alias - The versioned alias (i.e. "foo@@SOME_VERSION")
498 /// \param Aliasee - The aliased symbol (i.e. "_start")
499 virtual void emitELFSymverDirective(MCSymbol *Alias, const MCSymbol *Aliasee);
501 /// \brief Emit a Linker Optimization Hint (LOH) directive.
502 /// \param Args - Arguments of the LOH.
503 virtual void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {}
505 /// \brief Emit a common symbol.
507 /// \param Symbol - The common symbol to emit.
508 /// \param Size - The size of the common symbol.
509 /// \param ByteAlignment - The alignment of the symbol if
510 /// non-zero. This must be a power of 2.
511 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
512 unsigned ByteAlignment) = 0;
514 /// \brief Emit a local common (.lcomm) symbol.
516 /// \param Symbol - The common symbol to emit.
517 /// \param Size - The size of the common symbol.
518 /// \param ByteAlignment - The alignment of the common symbol in bytes.
519 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
520 unsigned ByteAlignment);
522 /// \brief Emit the zerofill section and an optional symbol.
524 /// \param Section - The zerofill section to create and or to put the symbol
525 /// \param Symbol - The zerofill symbol to emit, if non-NULL.
526 /// \param Size - The size of the zerofill symbol.
527 /// \param ByteAlignment - The alignment of the zerofill symbol if
528 /// non-zero. This must be a power of 2 on some targets.
529 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
530 uint64_t Size = 0, unsigned ByteAlignment = 0) = 0;
532 /// \brief Emit a thread local bss (.tbss) symbol.
534 /// \param Section - The thread local common section.
535 /// \param Symbol - The thread local common symbol to emit.
536 /// \param Size - The size of the symbol.
537 /// \param ByteAlignment - The alignment of the thread local common symbol
538 /// if non-zero. This must be a power of 2 on some targets.
539 virtual void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
540 uint64_t Size, unsigned ByteAlignment = 0);
543 /// \name Generating Data
546 /// \brief Emit the bytes in \p Data into the output.
548 /// This is used to implement assembler directives such as .byte, .ascii,
550 virtual void EmitBytes(StringRef Data);
552 /// Functionally identical to EmitBytes. When emitting textual assembly, this
553 /// method uses .byte directives instead of .ascii or .asciz for readability.
554 virtual void EmitBinaryData(StringRef Data);
556 /// \brief Emit the expression \p Value into the output as a native
557 /// integer of the given \p Size bytes.
559 /// This is used to implement assembler directives such as .word, .quad,
562 /// \param Value - The value to emit.
563 /// \param Size - The size of the integer (in bytes) to emit. This must
564 /// match a native machine width.
565 /// \param Loc - The location of the expression for error reporting.
566 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
567 SMLoc Loc = SMLoc());
569 void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc = SMLoc());
571 /// \brief Special case of EmitValue that avoids the client having
572 /// to pass in a MCExpr for constant integers.
573 virtual void EmitIntValue(uint64_t Value, unsigned Size);
575 virtual void EmitULEB128Value(const MCExpr *Value);
577 virtual void EmitSLEB128Value(const MCExpr *Value);
579 /// \brief Special case of EmitULEB128Value that avoids the client having to
580 /// pass in a MCExpr for constant integers.
581 void EmitULEB128IntValue(uint64_t Value, unsigned Padding = 0);
583 /// \brief Special case of EmitSLEB128Value that avoids the client having to
584 /// pass in a MCExpr for constant integers.
585 void EmitSLEB128IntValue(int64_t Value);
587 /// \brief Special case of EmitValue that avoids the client having to pass in
588 /// a MCExpr for MCSymbols.
589 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
590 bool IsSectionRelative = false);
592 /// \brief Emit the expression \p Value into the output as a dtprel
593 /// (64-bit DTP relative) value.
595 /// This is used to implement assembler directives such as .dtpreldword on
596 /// targets that support them.
597 virtual void EmitDTPRel64Value(const MCExpr *Value);
599 /// \brief Emit the expression \p Value into the output as a dtprel
600 /// (32-bit DTP relative) value.
602 /// This is used to implement assembler directives such as .dtprelword on
603 /// targets that support them.
604 virtual void EmitDTPRel32Value(const MCExpr *Value);
606 /// \brief Emit the expression \p Value into the output as a tprel
607 /// (64-bit TP relative) value.
609 /// This is used to implement assembler directives such as .tpreldword on
610 /// targets that support them.
611 virtual void EmitTPRel64Value(const MCExpr *Value);
613 /// \brief Emit the expression \p Value into the output as a tprel
614 /// (32-bit TP relative) value.
616 /// This is used to implement assembler directives such as .tprelword on
617 /// targets that support them.
618 virtual void EmitTPRel32Value(const MCExpr *Value);
620 /// \brief Emit the expression \p Value into the output as a gprel64 (64-bit
621 /// GP relative) value.
623 /// This is used to implement assembler directives such as .gpdword on
624 /// targets that support them.
625 virtual void EmitGPRel64Value(const MCExpr *Value);
627 /// \brief Emit the expression \p Value into the output as a gprel32 (32-bit
628 /// GP relative) value.
630 /// This is used to implement assembler directives such as .gprel32 on
631 /// targets that support them.
632 virtual void EmitGPRel32Value(const MCExpr *Value);
634 /// \brief Emit NumBytes bytes worth of the value specified by FillValue.
635 /// This implements directives such as '.space'.
636 virtual void emitFill(uint64_t NumBytes, uint8_t FillValue);
638 /// \brief Emit \p Size bytes worth of the value specified by \p FillValue.
640 /// This is used to implement assembler directives such as .space or .skip.
642 /// \param NumBytes - The number of bytes to emit.
643 /// \param FillValue - The value to use when filling bytes.
644 /// \param Loc - The location of the expression for error reporting.
645 virtual void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
646 SMLoc Loc = SMLoc());
648 /// \brief Emit \p NumValues copies of \p Size bytes. Each \p Size bytes is
649 /// taken from the lowest order 4 bytes of \p Expr expression.
651 /// This is used to implement assembler directives such as .fill.
653 /// \param NumValues - The number of copies of \p Size bytes to emit.
654 /// \param Size - The size (in bytes) of each repeated value.
655 /// \param Expr - The expression from which \p Size bytes are used.
656 virtual void emitFill(uint64_t NumValues, int64_t Size, int64_t Expr);
657 virtual void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
658 SMLoc Loc = SMLoc());
660 /// \brief Emit NumBytes worth of zeros.
661 /// This function properly handles data in virtual sections.
662 void EmitZeros(uint64_t NumBytes);
664 /// \brief Emit some number of copies of \p Value until the byte alignment \p
665 /// ByteAlignment is reached.
667 /// If the number of bytes need to emit for the alignment is not a multiple
668 /// of \p ValueSize, then the contents of the emitted fill bytes is
671 /// This used to implement the .align assembler directive.
673 /// \param ByteAlignment - The alignment to reach. This must be a power of
674 /// two on some targets.
675 /// \param Value - The value to use when filling bytes.
676 /// \param ValueSize - The size of the integer (in bytes) to emit for
677 /// \p Value. This must match a native machine width.
678 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
679 /// the alignment cannot be reached in this many bytes, no bytes are
681 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
682 unsigned ValueSize = 1,
683 unsigned MaxBytesToEmit = 0);
685 /// \brief Emit nops until the byte alignment \p ByteAlignment is reached.
687 /// This used to align code where the alignment bytes may be executed. This
688 /// can emit different bytes for different sizes to optimize execution.
690 /// \param ByteAlignment - The alignment to reach. This must be a power of
691 /// two on some targets.
692 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
693 /// the alignment cannot be reached in this many bytes, no bytes are
695 virtual void EmitCodeAlignment(unsigned ByteAlignment,
696 unsigned MaxBytesToEmit = 0);
698 /// \brief Emit some number of copies of \p Value until the byte offset \p
699 /// Offset is reached.
701 /// This is used to implement assembler directives such as .org.
703 /// \param Offset - The offset to reach. This may be an expression, but the
704 /// expression must be associated with the current section.
705 /// \param Value - The value to use when filling bytes.
706 virtual void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
711 /// \brief Switch to a new logical file. This is used to implement the '.file
712 /// "foo.c"' assembler directive.
713 virtual void EmitFileDirective(StringRef Filename);
715 /// \brief Emit the "identifiers" directive. This implements the
716 /// '.ident "version foo"' assembler directive.
717 virtual void EmitIdent(StringRef IdentString) {}
719 /// \brief Associate a filename with a specified logical file number. This
720 /// implements the DWARF2 '.file 4 "foo.c"' assembler directive.
721 virtual unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
725 /// \brief This implements the DWARF2 '.loc fileno lineno ...' assembler
727 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
728 unsigned Column, unsigned Flags,
729 unsigned Isa, unsigned Discriminator,
732 /// \brief Associate a filename with a specified logical file number. This
733 /// implements the '.cv_file 4 "foo.c"' assembler directive. Returns true on
735 virtual bool EmitCVFileDirective(unsigned FileNo, StringRef Filename);
737 /// \brief Introduces a function id for use with .cv_loc.
738 virtual bool EmitCVFuncIdDirective(unsigned FunctionId);
740 /// \brief Introduces an inline call site id for use with .cv_loc. Includes
741 /// extra information for inline line table generation.
742 virtual bool EmitCVInlineSiteIdDirective(unsigned FunctionId, unsigned IAFunc,
743 unsigned IAFile, unsigned IALine,
744 unsigned IACol, SMLoc Loc);
746 /// \brief This implements the CodeView '.cv_loc' assembler directive.
747 virtual void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo,
748 unsigned Line, unsigned Column,
749 bool PrologueEnd, bool IsStmt,
750 StringRef FileName, SMLoc Loc);
752 /// \brief This implements the CodeView '.cv_linetable' assembler directive.
753 virtual void EmitCVLinetableDirective(unsigned FunctionId,
754 const MCSymbol *FnStart,
755 const MCSymbol *FnEnd);
757 /// \brief This implements the CodeView '.cv_inline_linetable' assembler
759 virtual void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
760 unsigned SourceFileId,
761 unsigned SourceLineNum,
762 const MCSymbol *FnStartSym,
763 const MCSymbol *FnEndSym);
765 /// \brief This implements the CodeView '.cv_def_range' assembler
767 virtual void EmitCVDefRangeDirective(
768 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
769 StringRef FixedSizePortion);
771 /// \brief This implements the CodeView '.cv_stringtable' assembler directive.
772 virtual void EmitCVStringTableDirective() {}
774 /// \brief This implements the CodeView '.cv_filechecksums' assembler directive.
775 virtual void EmitCVFileChecksumsDirective() {}
777 /// Emit the absolute difference between two symbols.
779 /// \pre Offset of \c Hi is greater than the offset \c Lo.
780 virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
783 virtual MCSymbol *getDwarfLineTableSymbol(unsigned CUID);
784 virtual void EmitCFISections(bool EH, bool Debug);
785 void EmitCFIStartProc(bool IsSimple);
786 void EmitCFIEndProc();
787 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
788 virtual void EmitCFIDefCfaOffset(int64_t Offset);
789 virtual void EmitCFIDefCfaRegister(int64_t Register);
790 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
791 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
792 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
793 virtual void EmitCFIRememberState();
794 virtual void EmitCFIRestoreState();
795 virtual void EmitCFISameValue(int64_t Register);
796 virtual void EmitCFIRestore(int64_t Register);
797 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
798 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
799 virtual void EmitCFIEscape(StringRef Values);
800 virtual void EmitCFIGnuArgsSize(int64_t Size);
801 virtual void EmitCFISignalFrame();
802 virtual void EmitCFIUndefined(int64_t Register);
803 virtual void EmitCFIRegister(int64_t Register1, int64_t Register2);
804 virtual void EmitCFIWindowSave();
806 virtual void EmitWinCFIStartProc(const MCSymbol *Symbol);
807 virtual void EmitWinCFIEndProc();
808 virtual void EmitWinCFIStartChained();
809 virtual void EmitWinCFIEndChained();
810 virtual void EmitWinCFIPushReg(unsigned Register);
811 virtual void EmitWinCFISetFrame(unsigned Register, unsigned Offset);
812 virtual void EmitWinCFIAllocStack(unsigned Size);
813 virtual void EmitWinCFISaveReg(unsigned Register, unsigned Offset);
814 virtual void EmitWinCFISaveXMM(unsigned Register, unsigned Offset);
815 virtual void EmitWinCFIPushFrame(bool Code);
816 virtual void EmitWinCFIEndProlog();
818 virtual void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except);
819 virtual void EmitWinEHHandlerData();
821 /// Get the .pdata section used for the given section. Typically the given
822 /// section is either the main .text section or some other COMDAT .text
823 /// section, but it may be any section containing code.
824 MCSection *getAssociatedPDataSection(const MCSection *TextSec);
826 /// Get the .xdata section used for the given section.
827 MCSection *getAssociatedXDataSection(const MCSection *TextSec);
829 virtual void EmitSyntaxDirective();
831 /// \brief Emit a .reloc directive.
832 /// Returns true if the relocation could not be emitted because Name is not
834 virtual bool EmitRelocDirective(const MCExpr &Offset, StringRef Name,
835 const MCExpr *Expr, SMLoc Loc) {
839 /// \brief Emit the given \p Instruction into the current section.
840 /// PrintSchedInfo == true then schedul comment should be added to output
841 virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
842 bool PrintSchedInfo = false);
844 /// \brief Set the bundle alignment mode from now on in the section.
845 /// The argument is the power of 2 to which the alignment is set. The
846 /// value 0 means turn the bundle alignment off.
847 virtual void EmitBundleAlignMode(unsigned AlignPow2);
849 /// \brief The following instructions are a bundle-locked group.
851 /// \param AlignToEnd - If true, the bundle-locked group will be aligned to
852 /// the end of a bundle.
853 virtual void EmitBundleLock(bool AlignToEnd);
855 /// \brief Ends a bundle-locked group.
856 virtual void EmitBundleUnlock();
858 /// \brief If this file is backed by a assembly streamer, this dumps the
859 /// specified string in the output .s file. This capability is indicated by
860 /// the hasRawTextSupport() predicate. By default this aborts.
861 void EmitRawText(const Twine &String);
863 /// \brief Streamer specific finalization.
864 virtual void FinishImpl();
865 /// \brief Finish emission of machine code.
868 virtual bool mayHaveInstructions(MCSection &Sec) const { return true; }
871 /// Create a dummy machine code streamer, which does nothing. This is useful for
872 /// timing the assembler front end.
873 MCStreamer *createNullStreamer(MCContext &Ctx);
875 /// Create a machine code streamer which will print out assembly for the native
876 /// target, suitable for compiling with a native assembler.
878 /// \param InstPrint - If given, the instruction printer to use. If not given
879 /// the MCInst representation will be printed. This method takes ownership of
882 /// \param CE - If given, a code emitter to use to show the instruction
883 /// encoding inline with the assembly. This method takes ownership of \p CE.
885 /// \param TAB - If given, a target asm backend to use to show the fixup
886 /// information in conjunction with encoding information. This method takes
887 /// ownership of \p TAB.
889 /// \param ShowInst - Whether to show the MCInst representation inline with
891 MCStreamer *createAsmStreamer(MCContext &Ctx,
892 std::unique_ptr<formatted_raw_ostream> OS,
893 bool isVerboseAsm, bool useDwarfDirectory,
894 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
895 MCAsmBackend *TAB, bool ShowInst);
897 } // end namespace llvm
899 #endif // LLVM_MC_MCSTREAMER_H