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/SmallVector.h"
19 #include "llvm/MC/MCDirectives.h"
20 #include "llvm/MC/MCDwarf.h"
21 #include "llvm/MC/MCLinkerOptimizationHint.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/MC/MCWinEH.h"
24 #include "llvm/Support/DataTypes.h"
25 #include "llvm/Support/SMLoc.h"
38 class MCSymbolRefExpr;
39 class MCSubtargetInfo;
43 class formatted_raw_ostream;
44 class AssemblerConstantPools;
46 typedef std::pair<MCSection *, const MCExpr *> MCSectionSubPair;
48 /// Target specific streamer interface. This is used so that targets can
49 /// implement support for target specific assembly directives.
51 /// If target foo wants to use this, it should implement 3 classes:
52 /// * FooTargetStreamer : public MCTargetStreamer
53 /// * FooTargetAsmStreamer : public FooTargetStreamer
54 /// * FooTargetELFStreamer : public FooTargetStreamer
56 /// FooTargetStreamer should have a pure virtual method for each directive. For
57 /// example, for a ".bar symbol_name" directive, it should have
58 /// virtual emitBar(const MCSymbol &Symbol) = 0;
60 /// The FooTargetAsmStreamer and FooTargetELFStreamer classes implement the
61 /// method. The assembly streamer just prints ".bar symbol_name". The object
62 /// streamer does whatever is needed to implement .bar in the object file.
64 /// In the assembly printer and parser the target streamer can be used by
65 /// calling getTargetStreamer and casting it to FooTargetStreamer:
67 /// MCTargetStreamer &TS = OutStreamer.getTargetStreamer();
68 /// FooTargetStreamer &ATS = static_cast<FooTargetStreamer &>(TS);
70 /// The base classes FooTargetAsmStreamer and FooTargetELFStreamer should
71 /// *never* be treated differently. Callers should always talk to a
72 /// FooTargetStreamer.
73 class MCTargetStreamer {
78 MCTargetStreamer(MCStreamer &S);
79 virtual ~MCTargetStreamer();
81 MCStreamer &getStreamer() { return Streamer; }
83 // Allow a target to add behavior to the EmitLabel of MCStreamer.
84 virtual void emitLabel(MCSymbol *Symbol);
85 // Allow a target to add behavior to the emitAssignment of MCStreamer.
86 virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value);
88 virtual void prettyPrintAsm(MCInstPrinter &InstPrinter, raw_ostream &OS,
89 const MCInst &Inst, const MCSubtargetInfo &STI);
91 virtual void finish();
94 // FIXME: declared here because it is used from
95 // lib/CodeGen/AsmPrinter/ARMException.cpp.
96 class ARMTargetStreamer : public MCTargetStreamer {
98 ARMTargetStreamer(MCStreamer &S);
99 ~ARMTargetStreamer() override;
101 virtual void emitFnStart();
102 virtual void emitFnEnd();
103 virtual void emitCantUnwind();
104 virtual void emitPersonality(const MCSymbol *Personality);
105 virtual void emitPersonalityIndex(unsigned Index);
106 virtual void emitHandlerData();
107 virtual void emitSetFP(unsigned FpReg, unsigned SpReg,
109 virtual void emitMovSP(unsigned Reg, int64_t Offset = 0);
110 virtual void emitPad(int64_t Offset);
111 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
113 virtual void emitUnwindRaw(int64_t StackOffset,
114 const SmallVectorImpl<uint8_t> &Opcodes);
116 virtual void switchVendor(StringRef Vendor);
117 virtual void emitAttribute(unsigned Attribute, unsigned Value);
118 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
119 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
120 StringRef StringValue = "");
121 virtual void emitFPU(unsigned FPU);
122 virtual void emitArch(unsigned Arch);
123 virtual void emitArchExtension(unsigned ArchExt);
124 virtual void emitObjectArch(unsigned Arch);
125 virtual void finishAttributeSection();
126 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
128 virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
130 virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value);
132 void finish() override;
134 /// Reset any state between object emissions, i.e. the equivalent of
135 /// MCStreamer's reset method.
136 virtual void reset();
138 /// Callback used to implement the ldr= pseudo.
139 /// Add a new entry to the constant pool for the current section and return an
140 /// MCExpr that can be used to refer to the constant pool location.
141 const MCExpr *addConstantPoolEntry(const MCExpr *, SMLoc Loc);
143 /// Callback used to implemnt the .ltorg directive.
144 /// Emit contents of constant pool for the current section.
145 void emitCurrentConstantPool();
148 std::unique_ptr<AssemblerConstantPools> ConstantPools;
151 /// \brief Streaming machine code generation interface.
153 /// This interface is intended to provide a programatic interface that is very
154 /// similar to the level that an assembler .s file provides. It has callbacks
155 /// to emit bytes, handle directives, etc. The implementation of this interface
156 /// retains state to know what the current section is etc.
158 /// There are multiple implementations of this interface: one for writing out
159 /// a .s file, and implementations that write out .o files of various formats.
163 std::unique_ptr<MCTargetStreamer> TargetStreamer;
165 MCStreamer(const MCStreamer &) = delete;
166 MCStreamer &operator=(const MCStreamer &) = delete;
168 std::vector<MCDwarfFrameInfo> DwarfFrameInfos;
169 MCDwarfFrameInfo *getCurrentDwarfFrameInfo();
170 void EnsureValidDwarfFrame();
172 MCSymbol *EmitCFICommon();
174 std::vector<WinEH::FrameInfo *> WinFrameInfos;
175 WinEH::FrameInfo *CurrentWinFrameInfo;
176 void EnsureValidWinFrameInfo();
178 /// \brief Tracks an index to represent the order a symbol was emitted in.
179 /// Zero means we did not emit that symbol.
180 DenseMap<const MCSymbol *, unsigned> SymbolOrdering;
182 /// \brief This is stack of current and previous section values saved by
184 SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack;
186 /// The next unique ID to use when creating a WinCFI-related section (.pdata
187 /// or .xdata). This ID ensures that we have a one-to-one mapping from
188 /// code section to unwind info section, which MSVC's incremental linker
190 unsigned NextWinCFIID = 0;
193 MCStreamer(MCContext &Ctx);
195 virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
196 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
198 WinEH::FrameInfo *getCurrentWinFrameInfo() {
199 return CurrentWinFrameInfo;
202 virtual void EmitWindowsUnwindTables();
204 virtual void EmitRawTextImpl(StringRef String);
207 virtual ~MCStreamer();
209 void visitUsedExpr(const MCExpr &Expr);
210 virtual void visitUsedSymbol(const MCSymbol &Sym);
212 void setTargetStreamer(MCTargetStreamer *TS) {
213 TargetStreamer.reset(TS);
218 virtual void reset();
220 MCContext &getContext() const { return Context; }
222 MCTargetStreamer *getTargetStreamer() {
223 return TargetStreamer.get();
226 unsigned getNumFrameInfos() { return DwarfFrameInfos.size(); }
227 ArrayRef<MCDwarfFrameInfo> getDwarfFrameInfos() const {
228 return DwarfFrameInfos;
231 bool hasUnfinishedDwarfFrameInfo();
233 unsigned getNumWinFrameInfos() { return WinFrameInfos.size(); }
234 ArrayRef<WinEH::FrameInfo *> getWinFrameInfos() const {
235 return WinFrameInfos;
238 void generateCompactUnwindEncodings(MCAsmBackend *MAB);
240 /// \name Assembly File Formatting.
243 /// \brief Return true if this streamer supports verbose assembly and if it is
245 virtual bool isVerboseAsm() const { return false; }
247 /// \brief Return true if this asm streamer supports emitting unformatted text
248 /// to the .s file with EmitRawText.
249 virtual bool hasRawTextSupport() const { return false; }
251 /// \brief Is the integrated assembler required for this streamer to function
253 virtual bool isIntegratedAssemblerRequired() const { return false; }
255 /// \brief Add a textual comment.
257 /// Typically for comments that can be emitted to the generated .s
258 /// file if applicable as a QoI issue to make the output of the compiler
259 /// more readable. This only affects the MCAsmStreamer, and only when
260 /// verbose assembly output is enabled.
262 /// If the comment includes embedded \n's, they will each get the comment
263 /// prefix as appropriate. The added comment should not end with a \n.
264 virtual void AddComment(const Twine &T) {}
266 /// \brief Return a raw_ostream that comments can be written to. Unlike
267 /// AddComment, you are required to terminate comments with \n if you use this
269 virtual raw_ostream &GetCommentOS();
271 /// \brief Print T and prefix it with the comment string (normally #) and
272 /// optionally a tab. This prints the comment immediately, not at the end of
273 /// the current line. It is basically a safe version of EmitRawText: since it
274 /// only prints comments, the object streamer ignores it instead of asserting.
275 virtual void emitRawComment(const Twine &T, bool TabPrefix = true);
277 /// \brief Add explicit comment T. T is required to be a valid
278 /// comment in the output and does not need to be escaped.
279 virtual void addExplicitComment(const Twine &T);
280 /// \brief Emit added explicit comments.
281 virtual void emitExplicitComments();
283 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
284 virtual void AddBlankLine() {}
288 /// \name Symbol & Section Management
291 /// \brief Return the current section that the streamer is emitting code to.
292 MCSectionSubPair getCurrentSection() const {
293 if (!SectionStack.empty())
294 return SectionStack.back().first;
295 return MCSectionSubPair();
297 MCSection *getCurrentSectionOnly() const { return getCurrentSection().first; }
299 /// \brief Return the previous section that the streamer is emitting code to.
300 MCSectionSubPair getPreviousSection() const {
301 if (!SectionStack.empty())
302 return SectionStack.back().second;
303 return MCSectionSubPair();
306 /// \brief Returns an index to represent the order a symbol was emitted in.
307 /// (zero if we did not emit that symbol)
308 unsigned GetSymbolOrder(const MCSymbol *Sym) const {
309 return SymbolOrdering.lookup(Sym);
312 /// \brief Update streamer for a new active section.
314 /// This is called by PopSection and SwitchSection, if the current
316 virtual void ChangeSection(MCSection *, const MCExpr *);
318 /// \brief Save the current and previous section on the section stack.
320 SectionStack.push_back(
321 std::make_pair(getCurrentSection(), getPreviousSection()));
324 /// \brief Restore the current and previous section from the section stack.
325 /// Calls ChangeSection as needed.
327 /// Returns false if the stack was empty.
329 if (SectionStack.size() <= 1)
331 auto I = SectionStack.end();
333 MCSectionSubPair OldSection = I->first;
335 MCSectionSubPair NewSection = I->first;
337 if (OldSection != NewSection)
338 ChangeSection(NewSection.first, NewSection.second);
339 SectionStack.pop_back();
343 bool SubSection(const MCExpr *Subsection) {
344 if (SectionStack.empty())
347 SwitchSection(SectionStack.back().first.first, Subsection);
351 /// Set the current section where code is being emitted to \p Section. This
352 /// is required to update CurSection.
354 /// This corresponds to assembler directives like .section, .text, etc.
355 virtual void SwitchSection(MCSection *Section,
356 const MCExpr *Subsection = nullptr);
358 /// \brief Set the current section where code is being emitted to \p Section.
359 /// This is required to update CurSection. This version does not call
361 void SwitchSectionNoChange(MCSection *Section,
362 const MCExpr *Subsection = nullptr) {
363 assert(Section && "Cannot switch to a null section!");
364 MCSectionSubPair curSection = SectionStack.back().first;
365 SectionStack.back().second = curSection;
366 if (MCSectionSubPair(Section, Subsection) != curSection)
367 SectionStack.back().first = MCSectionSubPair(Section, Subsection);
370 /// \brief Create the default sections and set the initial one.
371 virtual void InitSections(bool NoExecStack);
373 MCSymbol *endSection(MCSection *Section);
375 /// \brief Sets the symbol's section.
377 /// Each emitted symbol will be tracked in the ordering table,
378 /// so we can sort on them later.
379 void AssignFragment(MCSymbol *Symbol, MCFragment *Fragment);
381 /// \brief Emit a label for \p Symbol into the current section.
383 /// This corresponds to an assembler statement such as:
386 /// \param Symbol - The symbol to emit. A given symbol should only be
387 /// emitted as a label once, and symbols emitted as a label should never be
388 /// used in an assignment.
389 // FIXME: These emission are non-const because we mutate the symbol to
390 // add the section we're emitting it to later.
391 virtual void EmitLabel(MCSymbol *Symbol);
393 virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol);
395 /// \brief Note in the output the specified \p Flag.
396 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
398 /// \brief Emit the given list \p Options of strings as linker
399 /// options into the output.
400 virtual void EmitLinkerOptions(ArrayRef<std::string> Kind) {}
402 /// \brief Note in the output the specified region \p Kind.
403 virtual void EmitDataRegion(MCDataRegionType Kind) {}
405 /// \brief Specify the MachO minimum deployment target version.
406 virtual void EmitVersionMin(MCVersionMinType, unsigned Major, unsigned Minor,
409 /// \brief Note in the output that the specified \p Func is a Thumb mode
410 /// function (ARM target only).
411 virtual void EmitThumbFunc(MCSymbol *Func);
413 /// \brief Emit an assignment of \p Value to \p Symbol.
415 /// This corresponds to an assembler statement such as:
418 /// The assignment generates no code, but has the side effect of binding the
419 /// value in the current context. For the assembly streamer, this prints the
420 /// binding into the .s file.
422 /// \param Symbol - The symbol being assigned to.
423 /// \param Value - The value for the symbol.
424 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
426 /// \brief Emit an weak reference from \p Alias to \p Symbol.
428 /// This corresponds to an assembler statement such as:
429 /// .weakref alias, symbol
431 /// \param Alias - The alias that is being created.
432 /// \param Symbol - The symbol being aliased.
433 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
435 /// \brief Add the given \p Attribute to \p Symbol.
436 virtual bool EmitSymbolAttribute(MCSymbol *Symbol,
437 MCSymbolAttr Attribute) = 0;
439 /// \brief Set the \p DescValue for the \p Symbol.
441 /// \param Symbol - The symbol to have its n_desc field set.
442 /// \param DescValue - The value to set into the n_desc field.
443 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
445 /// \brief Start emitting COFF symbol definition
447 /// \param Symbol - The symbol to have its External & Type fields set.
448 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
450 /// \brief Emit the storage class of the symbol.
452 /// \param StorageClass - The storage class the symbol should have.
453 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
455 /// \brief Emit the type of the symbol.
457 /// \param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
458 virtual void EmitCOFFSymbolType(int Type);
460 /// \brief Marks the end of the symbol definition.
461 virtual void EndCOFFSymbolDef();
463 virtual void EmitCOFFSafeSEH(MCSymbol const *Symbol);
465 /// \brief Emits a COFF section index.
467 /// \param Symbol - Symbol the section number relocation should point to.
468 virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
470 /// \brief Emits a COFF section relative relocation.
472 /// \param Symbol - Symbol the section relative relocation should point to.
473 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
475 /// \brief Emit an ELF .size directive.
477 /// This corresponds to an assembler statement such as:
478 /// .size symbol, expression
479 virtual void emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value);
481 /// \brief Emit a Linker Optimization Hint (LOH) directive.
482 /// \param Args - Arguments of the LOH.
483 virtual void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {}
485 /// \brief Emit a common symbol.
487 /// \param Symbol - The common symbol to emit.
488 /// \param Size - The size of the common symbol.
489 /// \param ByteAlignment - The alignment of the symbol if
490 /// non-zero. This must be a power of 2.
491 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
492 unsigned ByteAlignment) = 0;
494 /// \brief Emit a local common (.lcomm) symbol.
496 /// \param Symbol - The common symbol to emit.
497 /// \param Size - The size of the common symbol.
498 /// \param ByteAlignment - The alignment of the common symbol in bytes.
499 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
500 unsigned ByteAlignment);
502 /// \brief Emit the zerofill section and an optional symbol.
504 /// \param Section - The zerofill section to create and or to put the symbol
505 /// \param Symbol - The zerofill symbol to emit, if non-NULL.
506 /// \param Size - The size of the zerofill symbol.
507 /// \param ByteAlignment - The alignment of the zerofill symbol if
508 /// non-zero. This must be a power of 2 on some targets.
509 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
510 uint64_t Size = 0, unsigned ByteAlignment = 0) = 0;
512 /// \brief Emit a thread local bss (.tbss) symbol.
514 /// \param Section - The thread local common section.
515 /// \param Symbol - The thread local common symbol to emit.
516 /// \param Size - The size of the symbol.
517 /// \param ByteAlignment - The alignment of the thread local common symbol
518 /// if non-zero. This must be a power of 2 on some targets.
519 virtual void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
520 uint64_t Size, unsigned ByteAlignment = 0);
523 /// \name Generating Data
526 /// \brief Emit the bytes in \p Data into the output.
528 /// This is used to implement assembler directives such as .byte, .ascii,
530 virtual void EmitBytes(StringRef Data);
532 /// Functionally identical to EmitBytes. When emitting textual assembly, this
533 /// method uses .byte directives instead of .ascii or .asciz for readability.
534 virtual void EmitBinaryData(StringRef Data);
536 /// \brief Emit the expression \p Value into the output as a native
537 /// integer of the given \p Size bytes.
539 /// This is used to implement assembler directives such as .word, .quad,
542 /// \param Value - The value to emit.
543 /// \param Size - The size of the integer (in bytes) to emit. This must
544 /// match a native machine width.
545 /// \param Loc - The location of the expression for error reporting.
546 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
547 SMLoc Loc = SMLoc());
549 void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc = SMLoc());
551 /// \brief Special case of EmitValue that avoids the client having
552 /// to pass in a MCExpr for constant integers.
553 virtual void EmitIntValue(uint64_t Value, unsigned Size);
555 virtual void EmitULEB128Value(const MCExpr *Value);
557 virtual void EmitSLEB128Value(const MCExpr *Value);
559 /// \brief Special case of EmitULEB128Value that avoids the client having to
560 /// pass in a MCExpr for constant integers.
561 void EmitULEB128IntValue(uint64_t Value, unsigned Padding = 0);
563 /// \brief Special case of EmitSLEB128Value that avoids the client having to
564 /// pass in a MCExpr for constant integers.
565 void EmitSLEB128IntValue(int64_t Value);
567 /// \brief Special case of EmitValue that avoids the client having to pass in
568 /// a MCExpr for MCSymbols.
569 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
570 bool IsSectionRelative = false);
572 /// \brief Emit the expression \p Value into the output as a gprel64 (64-bit
573 /// GP relative) value.
575 /// This is used to implement assembler directives such as .gpdword on
576 /// targets that support them.
577 virtual void EmitGPRel64Value(const MCExpr *Value);
579 /// \brief Emit the expression \p Value into the output as a gprel32 (32-bit
580 /// GP relative) value.
582 /// This is used to implement assembler directives such as .gprel32 on
583 /// targets that support them.
584 virtual void EmitGPRel32Value(const MCExpr *Value);
586 /// \brief Emit NumBytes bytes worth of the value specified by FillValue.
587 /// This implements directives such as '.space'.
588 virtual void emitFill(uint64_t NumBytes, uint8_t FillValue);
590 /// \brief Emit \p Size bytes worth of the value specified by \p FillValue.
592 /// This is used to implement assembler directives such as .space or .skip.
594 /// \param NumBytes - The number of bytes to emit.
595 /// \param FillValue - The value to use when filling bytes.
596 /// \param Loc - The location of the expression for error reporting.
597 virtual void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
598 SMLoc Loc = SMLoc());
600 /// \brief Emit \p NumValues copies of \p Size bytes. Each \p Size bytes is
601 /// taken from the lowest order 4 bytes of \p Expr expression.
603 /// This is used to implement assembler directives such as .fill.
605 /// \param NumValues - The number of copies of \p Size bytes to emit.
606 /// \param Size - The size (in bytes) of each repeated value.
607 /// \param Expr - The expression from which \p Size bytes are used.
608 virtual void emitFill(uint64_t NumValues, int64_t Size, int64_t Expr);
609 virtual void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
610 SMLoc Loc = SMLoc());
612 /// \brief Emit NumBytes worth of zeros.
613 /// This function properly handles data in virtual sections.
614 void EmitZeros(uint64_t NumBytes);
616 /// \brief Emit some number of copies of \p Value until the byte alignment \p
617 /// ByteAlignment is reached.
619 /// If the number of bytes need to emit for the alignment is not a multiple
620 /// of \p ValueSize, then the contents of the emitted fill bytes is
623 /// This used to implement the .align assembler directive.
625 /// \param ByteAlignment - The alignment to reach. This must be a power of
626 /// two on some targets.
627 /// \param Value - The value to use when filling bytes.
628 /// \param ValueSize - The size of the integer (in bytes) to emit for
629 /// \p Value. This must match a native machine width.
630 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
631 /// the alignment cannot be reached in this many bytes, no bytes are
633 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
634 unsigned ValueSize = 1,
635 unsigned MaxBytesToEmit = 0);
637 /// \brief Emit nops until the byte alignment \p ByteAlignment is reached.
639 /// This used to align code where the alignment bytes may be executed. This
640 /// can emit different bytes for different sizes to optimize execution.
642 /// \param ByteAlignment - The alignment to reach. This must be a power of
643 /// two on some targets.
644 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
645 /// the alignment cannot be reached in this many bytes, no bytes are
647 virtual void EmitCodeAlignment(unsigned ByteAlignment,
648 unsigned MaxBytesToEmit = 0);
650 /// \brief Emit some number of copies of \p Value until the byte offset \p
651 /// Offset is reached.
653 /// This is used to implement assembler directives such as .org.
655 /// \param Offset - The offset to reach. This may be an expression, but the
656 /// expression must be associated with the current section.
657 /// \param Value - The value to use when filling bytes.
658 virtual void emitValueToOffset(const MCExpr *Offset, unsigned char Value = 0);
662 /// \brief Switch to a new logical file. This is used to implement the '.file
663 /// "foo.c"' assembler directive.
664 virtual void EmitFileDirective(StringRef Filename);
666 /// \brief Emit the "identifiers" directive. This implements the
667 /// '.ident "version foo"' assembler directive.
668 virtual void EmitIdent(StringRef IdentString) {}
670 /// \brief Associate a filename with a specified logical file number. This
671 /// implements the DWARF2 '.file 4 "foo.c"' assembler directive.
672 virtual unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
676 /// \brief This implements the DWARF2 '.loc fileno lineno ...' assembler
678 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
679 unsigned Column, unsigned Flags,
680 unsigned Isa, unsigned Discriminator,
683 /// \brief Associate a filename with a specified logical file number. This
684 /// implements the '.cv_file 4 "foo.c"' assembler directive.
685 virtual unsigned EmitCVFileDirective(unsigned FileNo, StringRef Filename);
687 /// \brief This implements the CodeView '.cv_loc' assembler directive.
688 virtual void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo,
689 unsigned Line, unsigned Column,
690 bool PrologueEnd, bool IsStmt,
693 /// \brief This implements the CodeView '.cv_linetable' assembler directive.
694 virtual void EmitCVLinetableDirective(unsigned FunctionId,
695 const MCSymbol *FnStart,
696 const MCSymbol *FnEnd);
698 /// \brief This implements the CodeView '.cv_inline_linetable' assembler
700 virtual void EmitCVInlineLinetableDirective(
701 unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum,
702 const MCSymbol *FnStartSym, const MCSymbol *FnEndSym,
703 ArrayRef<unsigned> SecondaryFunctionIds);
705 /// \brief This implements the CodeView '.cv_def_range' assembler
707 virtual void EmitCVDefRangeDirective(
708 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
709 StringRef FixedSizePortion);
711 /// \brief This implements the CodeView '.cv_stringtable' assembler directive.
712 virtual void EmitCVStringTableDirective() {}
714 /// \brief This implements the CodeView '.cv_filechecksums' assembler directive.
715 virtual void EmitCVFileChecksumsDirective() {}
717 /// Emit the absolute difference between two symbols.
719 /// \pre Offset of \c Hi is greater than the offset \c Lo.
720 virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
723 virtual MCSymbol *getDwarfLineTableSymbol(unsigned CUID);
724 virtual void EmitCFISections(bool EH, bool Debug);
725 void EmitCFIStartProc(bool IsSimple);
726 void EmitCFIEndProc();
727 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
728 virtual void EmitCFIDefCfaOffset(int64_t Offset);
729 virtual void EmitCFIDefCfaRegister(int64_t Register);
730 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
731 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
732 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
733 virtual void EmitCFIRememberState();
734 virtual void EmitCFIRestoreState();
735 virtual void EmitCFISameValue(int64_t Register);
736 virtual void EmitCFIRestore(int64_t Register);
737 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
738 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
739 virtual void EmitCFIEscape(StringRef Values);
740 virtual void EmitCFIGnuArgsSize(int64_t Size);
741 virtual void EmitCFISignalFrame();
742 virtual void EmitCFIUndefined(int64_t Register);
743 virtual void EmitCFIRegister(int64_t Register1, int64_t Register2);
744 virtual void EmitCFIWindowSave();
746 virtual void EmitWinCFIStartProc(const MCSymbol *Symbol);
747 virtual void EmitWinCFIEndProc();
748 virtual void EmitWinCFIStartChained();
749 virtual void EmitWinCFIEndChained();
750 virtual void EmitWinCFIPushReg(unsigned Register);
751 virtual void EmitWinCFISetFrame(unsigned Register, unsigned Offset);
752 virtual void EmitWinCFIAllocStack(unsigned Size);
753 virtual void EmitWinCFISaveReg(unsigned Register, unsigned Offset);
754 virtual void EmitWinCFISaveXMM(unsigned Register, unsigned Offset);
755 virtual void EmitWinCFIPushFrame(bool Code);
756 virtual void EmitWinCFIEndProlog();
758 virtual void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except);
759 virtual void EmitWinEHHandlerData();
761 /// Get the .pdata section used for the given section. Typically the given
762 /// section is either the main .text section or some other COMDAT .text
763 /// section, but it may be any section containing code.
764 MCSection *getAssociatedPDataSection(const MCSection *TextSec);
766 /// Get the .xdata section used for the given section.
767 MCSection *getAssociatedXDataSection(const MCSection *TextSec);
769 virtual void EmitSyntaxDirective();
771 /// \brief Emit a .reloc directive.
772 /// Returns true if the relocation could not be emitted because Name is not
774 virtual bool EmitRelocDirective(const MCExpr &Offset, StringRef Name,
775 const MCExpr *Expr, SMLoc Loc) {
779 /// \brief Emit the given \p Instruction into the current section.
780 virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI);
782 /// \brief Set the bundle alignment mode from now on in the section.
783 /// The argument is the power of 2 to which the alignment is set. The
784 /// value 0 means turn the bundle alignment off.
785 virtual void EmitBundleAlignMode(unsigned AlignPow2);
787 /// \brief The following instructions are a bundle-locked group.
789 /// \param AlignToEnd - If true, the bundle-locked group will be aligned to
790 /// the end of a bundle.
791 virtual void EmitBundleLock(bool AlignToEnd);
793 /// \brief Ends a bundle-locked group.
794 virtual void EmitBundleUnlock();
796 /// \brief If this file is backed by a assembly streamer, this dumps the
797 /// specified string in the output .s file. This capability is indicated by
798 /// the hasRawTextSupport() predicate. By default this aborts.
799 void EmitRawText(const Twine &String);
801 /// \brief Streamer specific finalization.
802 virtual void FinishImpl();
803 /// \brief Finish emission of machine code.
806 virtual bool mayHaveInstructions(MCSection &Sec) const { return true; }
809 /// Create a dummy machine code streamer, which does nothing. This is useful for
810 /// timing the assembler front end.
811 MCStreamer *createNullStreamer(MCContext &Ctx);
813 /// Create a machine code streamer which will print out assembly for the native
814 /// target, suitable for compiling with a native assembler.
816 /// \param InstPrint - If given, the instruction printer to use. If not given
817 /// the MCInst representation will be printed. This method takes ownership of
820 /// \param CE - If given, a code emitter to use to show the instruction
821 /// encoding inline with the assembly. This method takes ownership of \p CE.
823 /// \param TAB - If given, a target asm backend to use to show the fixup
824 /// information in conjunction with encoding information. This method takes
825 /// ownership of \p TAB.
827 /// \param ShowInst - Whether to show the MCInst representation inline with
829 MCStreamer *createAsmStreamer(MCContext &Ctx,
830 std::unique_ptr<formatted_raw_ostream> OS,
831 bool isVerboseAsm, bool useDwarfDirectory,
832 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
833 MCAsmBackend *TAB, bool ShowInst);
834 } // end namespace llvm