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 *EmitCFILabel();
173 MCSymbol *EmitCFICommon();
175 std::vector<WinEH::FrameInfo *> WinFrameInfos;
176 WinEH::FrameInfo *CurrentWinFrameInfo;
177 void EnsureValidWinFrameInfo();
179 /// \brief Tracks an index to represent the order a symbol was emitted in.
180 /// Zero means we did not emit that symbol.
181 DenseMap<const MCSymbol *, unsigned> SymbolOrdering;
183 /// \brief This is stack of current and previous section values saved by
185 SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack;
187 /// The next unique ID to use when creating a WinCFI-related section (.pdata
188 /// or .xdata). This ID ensures that we have a one-to-one mapping from
189 /// code section to unwind info section, which MSVC's incremental linker
191 unsigned NextWinCFIID = 0;
194 MCStreamer(MCContext &Ctx);
196 virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
197 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
199 WinEH::FrameInfo *getCurrentWinFrameInfo() {
200 return CurrentWinFrameInfo;
203 virtual void EmitWindowsUnwindTables();
205 virtual void EmitRawTextImpl(StringRef String);
208 virtual ~MCStreamer();
210 void visitUsedExpr(const MCExpr &Expr);
211 virtual void visitUsedSymbol(const MCSymbol &Sym);
213 void setTargetStreamer(MCTargetStreamer *TS) {
214 TargetStreamer.reset(TS);
219 virtual void reset();
221 MCContext &getContext() const { return Context; }
223 MCTargetStreamer *getTargetStreamer() {
224 return TargetStreamer.get();
227 unsigned getNumFrameInfos() { return DwarfFrameInfos.size(); }
228 ArrayRef<MCDwarfFrameInfo> getDwarfFrameInfos() const {
229 return DwarfFrameInfos;
232 bool hasUnfinishedDwarfFrameInfo();
234 unsigned getNumWinFrameInfos() { return WinFrameInfos.size(); }
235 ArrayRef<WinEH::FrameInfo *> getWinFrameInfos() const {
236 return WinFrameInfos;
239 void generateCompactUnwindEncodings(MCAsmBackend *MAB);
241 /// \name Assembly File Formatting.
244 /// \brief Return true if this streamer supports verbose assembly and if it is
246 virtual bool isVerboseAsm() const { return false; }
248 /// \brief Return true if this asm streamer supports emitting unformatted text
249 /// to the .s file with EmitRawText.
250 virtual bool hasRawTextSupport() const { return false; }
252 /// \brief Is the integrated assembler required for this streamer to function
254 virtual bool isIntegratedAssemblerRequired() const { return false; }
256 /// \brief Add a textual comment.
258 /// Typically for comments that can be emitted to the generated .s
259 /// file if applicable as a QoI issue to make the output of the compiler
260 /// more readable. This only affects the MCAsmStreamer, and only when
261 /// verbose assembly output is enabled.
263 /// If the comment includes embedded \n's, they will each get the comment
264 /// prefix as appropriate. The added comment should not end with a \n.
265 /// By default, each comment is terminated with an end of line, i.e. the
266 /// EOL param is set to true by default. If one prefers not to end the
267 /// comment with a new line then the EOL param should be passed
268 /// with a false value.
269 virtual void AddComment(const Twine &T, bool EOL = true) {}
271 /// \brief Return a raw_ostream that comments can be written to. Unlike
272 /// AddComment, you are required to terminate comments with \n if you use this
274 virtual raw_ostream &GetCommentOS();
276 /// \brief Print T and prefix it with the comment string (normally #) and
277 /// optionally a tab. This prints the comment immediately, not at the end of
278 /// the current line. It is basically a safe version of EmitRawText: since it
279 /// only prints comments, the object streamer ignores it instead of asserting.
280 virtual void emitRawComment(const Twine &T, bool TabPrefix = true);
282 /// \brief Add explicit comment T. T is required to be a valid
283 /// comment in the output and does not need to be escaped.
284 virtual void addExplicitComment(const Twine &T);
285 /// \brief Emit added explicit comments.
286 virtual void emitExplicitComments();
288 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
289 virtual void AddBlankLine() {}
293 /// \name Symbol & Section Management
296 /// \brief Return the current section that the streamer is emitting code to.
297 MCSectionSubPair getCurrentSection() const {
298 if (!SectionStack.empty())
299 return SectionStack.back().first;
300 return MCSectionSubPair();
302 MCSection *getCurrentSectionOnly() const { return getCurrentSection().first; }
304 /// \brief Return the previous section that the streamer is emitting code to.
305 MCSectionSubPair getPreviousSection() const {
306 if (!SectionStack.empty())
307 return SectionStack.back().second;
308 return MCSectionSubPair();
311 /// \brief Returns an index to represent the order a symbol was emitted in.
312 /// (zero if we did not emit that symbol)
313 unsigned GetSymbolOrder(const MCSymbol *Sym) const {
314 return SymbolOrdering.lookup(Sym);
317 /// \brief Update streamer for a new active section.
319 /// This is called by PopSection and SwitchSection, if the current
321 virtual void ChangeSection(MCSection *, const MCExpr *);
323 /// \brief Save the current and previous section on the section stack.
325 SectionStack.push_back(
326 std::make_pair(getCurrentSection(), getPreviousSection()));
329 /// \brief Restore the current and previous section from the section stack.
330 /// Calls ChangeSection as needed.
332 /// Returns false if the stack was empty.
334 if (SectionStack.size() <= 1)
336 auto I = SectionStack.end();
338 MCSectionSubPair OldSection = I->first;
340 MCSectionSubPair NewSection = I->first;
342 if (OldSection != NewSection)
343 ChangeSection(NewSection.first, NewSection.second);
344 SectionStack.pop_back();
348 bool SubSection(const MCExpr *Subsection) {
349 if (SectionStack.empty())
352 SwitchSection(SectionStack.back().first.first, Subsection);
356 /// Set the current section where code is being emitted to \p Section. This
357 /// is required to update CurSection.
359 /// This corresponds to assembler directives like .section, .text, etc.
360 virtual void SwitchSection(MCSection *Section,
361 const MCExpr *Subsection = nullptr);
363 /// \brief Set the current section where code is being emitted to \p Section.
364 /// This is required to update CurSection. This version does not call
366 void SwitchSectionNoChange(MCSection *Section,
367 const MCExpr *Subsection = nullptr) {
368 assert(Section && "Cannot switch to a null section!");
369 MCSectionSubPair curSection = SectionStack.back().first;
370 SectionStack.back().second = curSection;
371 if (MCSectionSubPair(Section, Subsection) != curSection)
372 SectionStack.back().first = MCSectionSubPair(Section, Subsection);
375 /// \brief Create the default sections and set the initial one.
376 virtual void InitSections(bool NoExecStack);
378 MCSymbol *endSection(MCSection *Section);
380 /// \brief Sets the symbol's section.
382 /// Each emitted symbol will be tracked in the ordering table,
383 /// so we can sort on them later.
384 void AssignFragment(MCSymbol *Symbol, MCFragment *Fragment);
386 /// \brief Emit a label for \p Symbol into the current section.
388 /// This corresponds to an assembler statement such as:
391 /// \param Symbol - The symbol to emit. A given symbol should only be
392 /// emitted as a label once, and symbols emitted as a label should never be
393 /// used in an assignment.
394 // FIXME: These emission are non-const because we mutate the symbol to
395 // add the section we're emitting it to later.
396 virtual void EmitLabel(MCSymbol *Symbol);
398 virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol);
400 /// \brief Note in the output the specified \p Flag.
401 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
403 /// \brief Emit the given list \p Options of strings as linker
404 /// options into the output.
405 virtual void EmitLinkerOptions(ArrayRef<std::string> Kind) {}
407 /// \brief Note in the output the specified region \p Kind.
408 virtual void EmitDataRegion(MCDataRegionType Kind) {}
410 /// \brief Specify the MachO minimum deployment target version.
411 virtual void EmitVersionMin(MCVersionMinType, unsigned Major, unsigned Minor,
414 /// \brief Note in the output that the specified \p Func is a Thumb mode
415 /// function (ARM target only).
416 virtual void EmitThumbFunc(MCSymbol *Func);
418 /// \brief Emit an assignment of \p Value to \p Symbol.
420 /// This corresponds to an assembler statement such as:
423 /// The assignment generates no code, but has the side effect of binding the
424 /// value in the current context. For the assembly streamer, this prints the
425 /// binding into the .s file.
427 /// \param Symbol - The symbol being assigned to.
428 /// \param Value - The value for the symbol.
429 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
431 /// \brief Emit an weak reference from \p Alias to \p Symbol.
433 /// This corresponds to an assembler statement such as:
434 /// .weakref alias, symbol
436 /// \param Alias - The alias that is being created.
437 /// \param Symbol - The symbol being aliased.
438 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
440 /// \brief Add the given \p Attribute to \p Symbol.
441 virtual bool EmitSymbolAttribute(MCSymbol *Symbol,
442 MCSymbolAttr Attribute) = 0;
444 /// \brief Set the \p DescValue for the \p Symbol.
446 /// \param Symbol - The symbol to have its n_desc field set.
447 /// \param DescValue - The value to set into the n_desc field.
448 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
450 /// \brief Start emitting COFF symbol definition
452 /// \param Symbol - The symbol to have its External & Type fields set.
453 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
455 /// \brief Emit the storage class of the symbol.
457 /// \param StorageClass - The storage class the symbol should have.
458 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
460 /// \brief Emit the type of the symbol.
462 /// \param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
463 virtual void EmitCOFFSymbolType(int Type);
465 /// \brief Marks the end of the symbol definition.
466 virtual void EndCOFFSymbolDef();
468 virtual void EmitCOFFSafeSEH(MCSymbol const *Symbol);
470 /// \brief Emits a COFF section index.
472 /// \param Symbol - Symbol the section number relocation should point to.
473 virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
475 /// \brief Emits a COFF section relative relocation.
477 /// \param Symbol - Symbol the section relative relocation should point to.
478 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset);
480 /// \brief Emit an ELF .size directive.
482 /// This corresponds to an assembler statement such as:
483 /// .size symbol, expression
484 virtual void emitELFSize(MCSymbol *Symbol, const MCExpr *Value);
486 /// \brief Emit a Linker Optimization Hint (LOH) directive.
487 /// \param Args - Arguments of the LOH.
488 virtual void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {}
490 /// \brief Emit a common symbol.
492 /// \param Symbol - The common symbol to emit.
493 /// \param Size - The size of the common symbol.
494 /// \param ByteAlignment - The alignment of the symbol if
495 /// non-zero. This must be a power of 2.
496 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
497 unsigned ByteAlignment) = 0;
499 /// \brief Emit a local common (.lcomm) symbol.
501 /// \param Symbol - The common symbol to emit.
502 /// \param Size - The size of the common symbol.
503 /// \param ByteAlignment - The alignment of the common symbol in bytes.
504 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
505 unsigned ByteAlignment);
507 /// \brief Emit the zerofill section and an optional symbol.
509 /// \param Section - The zerofill section to create and or to put the symbol
510 /// \param Symbol - The zerofill symbol to emit, if non-NULL.
511 /// \param Size - The size of the zerofill symbol.
512 /// \param ByteAlignment - The alignment of the zerofill symbol if
513 /// non-zero. This must be a power of 2 on some targets.
514 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
515 uint64_t Size = 0, unsigned ByteAlignment = 0) = 0;
517 /// \brief Emit a thread local bss (.tbss) symbol.
519 /// \param Section - The thread local common section.
520 /// \param Symbol - The thread local common symbol to emit.
521 /// \param Size - The size of the symbol.
522 /// \param ByteAlignment - The alignment of the thread local common symbol
523 /// if non-zero. This must be a power of 2 on some targets.
524 virtual void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
525 uint64_t Size, unsigned ByteAlignment = 0);
528 /// \name Generating Data
531 /// \brief Emit the bytes in \p Data into the output.
533 /// This is used to implement assembler directives such as .byte, .ascii,
535 virtual void EmitBytes(StringRef Data);
537 /// Functionally identical to EmitBytes. When emitting textual assembly, this
538 /// method uses .byte directives instead of .ascii or .asciz for readability.
539 virtual void EmitBinaryData(StringRef Data);
541 /// \brief Emit the expression \p Value into the output as a native
542 /// integer of the given \p Size bytes.
544 /// This is used to implement assembler directives such as .word, .quad,
547 /// \param Value - The value to emit.
548 /// \param Size - The size of the integer (in bytes) to emit. This must
549 /// match a native machine width.
550 /// \param Loc - The location of the expression for error reporting.
551 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
552 SMLoc Loc = SMLoc());
554 void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc = SMLoc());
556 /// \brief Special case of EmitValue that avoids the client having
557 /// to pass in a MCExpr for constant integers.
558 virtual void EmitIntValue(uint64_t Value, unsigned Size);
560 virtual void EmitULEB128Value(const MCExpr *Value);
562 virtual void EmitSLEB128Value(const MCExpr *Value);
564 /// \brief Special case of EmitULEB128Value that avoids the client having to
565 /// pass in a MCExpr for constant integers.
566 void EmitULEB128IntValue(uint64_t Value, unsigned Padding = 0);
568 /// \brief Special case of EmitSLEB128Value that avoids the client having to
569 /// pass in a MCExpr for constant integers.
570 void EmitSLEB128IntValue(int64_t Value);
572 /// \brief Special case of EmitValue that avoids the client having to pass in
573 /// a MCExpr for MCSymbols.
574 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
575 bool IsSectionRelative = false);
577 /// \brief Emit the expression \p Value into the output as a dtprel
578 /// (64-bit DTP relative) value.
580 /// This is used to implement assembler directives such as .dtpreldword on
581 /// targets that support them.
582 virtual void EmitDTPRel64Value(const MCExpr *Value);
584 /// \brief Emit the expression \p Value into the output as a dtprel
585 /// (32-bit DTP relative) value.
587 /// This is used to implement assembler directives such as .dtprelword on
588 /// targets that support them.
589 virtual void EmitDTPRel32Value(const MCExpr *Value);
591 /// \brief Emit the expression \p Value into the output as a tprel
592 /// (64-bit TP relative) value.
594 /// This is used to implement assembler directives such as .tpreldword on
595 /// targets that support them.
596 virtual void EmitTPRel64Value(const MCExpr *Value);
598 /// \brief Emit the expression \p Value into the output as a tprel
599 /// (32-bit TP relative) value.
601 /// This is used to implement assembler directives such as .tprelword on
602 /// targets that support them.
603 virtual void EmitTPRel32Value(const MCExpr *Value);
605 /// \brief Emit the expression \p Value into the output as a gprel64 (64-bit
606 /// GP relative) value.
608 /// This is used to implement assembler directives such as .gpdword on
609 /// targets that support them.
610 virtual void EmitGPRel64Value(const MCExpr *Value);
612 /// \brief Emit the expression \p Value into the output as a gprel32 (32-bit
613 /// GP relative) value.
615 /// This is used to implement assembler directives such as .gprel32 on
616 /// targets that support them.
617 virtual void EmitGPRel32Value(const MCExpr *Value);
619 /// \brief Emit NumBytes bytes worth of the value specified by FillValue.
620 /// This implements directives such as '.space'.
621 virtual void emitFill(uint64_t NumBytes, uint8_t FillValue);
623 /// \brief Emit \p Size bytes worth of the value specified by \p FillValue.
625 /// This is used to implement assembler directives such as .space or .skip.
627 /// \param NumBytes - The number of bytes to emit.
628 /// \param FillValue - The value to use when filling bytes.
629 /// \param Loc - The location of the expression for error reporting.
630 virtual void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
631 SMLoc Loc = SMLoc());
633 /// \brief Emit \p NumValues copies of \p Size bytes. Each \p Size bytes is
634 /// taken from the lowest order 4 bytes of \p Expr expression.
636 /// This is used to implement assembler directives such as .fill.
638 /// \param NumValues - The number of copies of \p Size bytes to emit.
639 /// \param Size - The size (in bytes) of each repeated value.
640 /// \param Expr - The expression from which \p Size bytes are used.
641 virtual void emitFill(uint64_t NumValues, int64_t Size, int64_t Expr);
642 virtual void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
643 SMLoc Loc = SMLoc());
645 /// \brief Emit NumBytes worth of zeros.
646 /// This function properly handles data in virtual sections.
647 void EmitZeros(uint64_t NumBytes);
649 /// \brief Emit some number of copies of \p Value until the byte alignment \p
650 /// ByteAlignment is reached.
652 /// If the number of bytes need to emit for the alignment is not a multiple
653 /// of \p ValueSize, then the contents of the emitted fill bytes is
656 /// This used to implement the .align assembler directive.
658 /// \param ByteAlignment - The alignment to reach. This must be a power of
659 /// two on some targets.
660 /// \param Value - The value to use when filling bytes.
661 /// \param ValueSize - The size of the integer (in bytes) to emit for
662 /// \p Value. This must match a native machine width.
663 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
664 /// the alignment cannot be reached in this many bytes, no bytes are
666 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
667 unsigned ValueSize = 1,
668 unsigned MaxBytesToEmit = 0);
670 /// \brief Emit nops until the byte alignment \p ByteAlignment is reached.
672 /// This used to align code where the alignment bytes may be executed. This
673 /// can emit different bytes for different sizes to optimize execution.
675 /// \param ByteAlignment - The alignment to reach. This must be a power of
676 /// two on some targets.
677 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
678 /// the alignment cannot be reached in this many bytes, no bytes are
680 virtual void EmitCodeAlignment(unsigned ByteAlignment,
681 unsigned MaxBytesToEmit = 0);
683 /// \brief Emit some number of copies of \p Value until the byte offset \p
684 /// Offset is reached.
686 /// This is used to implement assembler directives such as .org.
688 /// \param Offset - The offset to reach. This may be an expression, but the
689 /// expression must be associated with the current section.
690 /// \param Value - The value to use when filling bytes.
691 virtual void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
696 /// \brief Switch to a new logical file. This is used to implement the '.file
697 /// "foo.c"' assembler directive.
698 virtual void EmitFileDirective(StringRef Filename);
700 /// \brief Emit the "identifiers" directive. This implements the
701 /// '.ident "version foo"' assembler directive.
702 virtual void EmitIdent(StringRef IdentString) {}
704 /// \brief Associate a filename with a specified logical file number. This
705 /// implements the DWARF2 '.file 4 "foo.c"' assembler directive.
706 virtual unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
710 /// \brief This implements the DWARF2 '.loc fileno lineno ...' assembler
712 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
713 unsigned Column, unsigned Flags,
714 unsigned Isa, unsigned Discriminator,
717 /// \brief Associate a filename with a specified logical file number. This
718 /// implements the '.cv_file 4 "foo.c"' assembler directive. Returns true on
720 virtual bool EmitCVFileDirective(unsigned FileNo, StringRef Filename);
722 /// \brief Introduces a function id for use with .cv_loc.
723 virtual bool EmitCVFuncIdDirective(unsigned FunctionId);
725 /// \brief Introduces an inline call site id for use with .cv_loc. Includes
726 /// extra information for inline line table generation.
727 virtual bool EmitCVInlineSiteIdDirective(unsigned FunctionId, unsigned IAFunc,
728 unsigned IAFile, unsigned IALine,
729 unsigned IACol, SMLoc Loc);
731 /// \brief This implements the CodeView '.cv_loc' assembler directive.
732 virtual void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo,
733 unsigned Line, unsigned Column,
734 bool PrologueEnd, bool IsStmt,
735 StringRef FileName, SMLoc Loc);
737 /// \brief This implements the CodeView '.cv_linetable' assembler directive.
738 virtual void EmitCVLinetableDirective(unsigned FunctionId,
739 const MCSymbol *FnStart,
740 const MCSymbol *FnEnd);
742 /// \brief This implements the CodeView '.cv_inline_linetable' assembler
744 virtual void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
745 unsigned SourceFileId,
746 unsigned SourceLineNum,
747 const MCSymbol *FnStartSym,
748 const MCSymbol *FnEndSym);
750 /// \brief This implements the CodeView '.cv_def_range' assembler
752 virtual void EmitCVDefRangeDirective(
753 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
754 StringRef FixedSizePortion);
756 /// \brief This implements the CodeView '.cv_stringtable' assembler directive.
757 virtual void EmitCVStringTableDirective() {}
759 /// \brief This implements the CodeView '.cv_filechecksums' assembler directive.
760 virtual void EmitCVFileChecksumsDirective() {}
762 /// Emit the absolute difference between two symbols.
764 /// \pre Offset of \c Hi is greater than the offset \c Lo.
765 virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
768 virtual MCSymbol *getDwarfLineTableSymbol(unsigned CUID);
769 virtual void EmitCFISections(bool EH, bool Debug);
770 void EmitCFIStartProc(bool IsSimple);
771 void EmitCFIEndProc();
772 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
773 virtual void EmitCFIDefCfaOffset(int64_t Offset);
774 virtual void EmitCFIDefCfaRegister(int64_t Register);
775 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
776 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
777 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
778 virtual void EmitCFIRememberState();
779 virtual void EmitCFIRestoreState();
780 virtual void EmitCFISameValue(int64_t Register);
781 virtual void EmitCFIRestore(int64_t Register);
782 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
783 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
784 virtual void EmitCFIEscape(StringRef Values);
785 virtual void EmitCFIGnuArgsSize(int64_t Size);
786 virtual void EmitCFISignalFrame();
787 virtual void EmitCFIUndefined(int64_t Register);
788 virtual void EmitCFIRegister(int64_t Register1, int64_t Register2);
789 virtual void EmitCFIWindowSave();
791 virtual void EmitWinCFIStartProc(const MCSymbol *Symbol);
792 virtual void EmitWinCFIEndProc();
793 virtual void EmitWinCFIStartChained();
794 virtual void EmitWinCFIEndChained();
795 virtual void EmitWinCFIPushReg(unsigned Register);
796 virtual void EmitWinCFISetFrame(unsigned Register, unsigned Offset);
797 virtual void EmitWinCFIAllocStack(unsigned Size);
798 virtual void EmitWinCFISaveReg(unsigned Register, unsigned Offset);
799 virtual void EmitWinCFISaveXMM(unsigned Register, unsigned Offset);
800 virtual void EmitWinCFIPushFrame(bool Code);
801 virtual void EmitWinCFIEndProlog();
803 virtual void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except);
804 virtual void EmitWinEHHandlerData();
806 /// Get the .pdata section used for the given section. Typically the given
807 /// section is either the main .text section or some other COMDAT .text
808 /// section, but it may be any section containing code.
809 MCSection *getAssociatedPDataSection(const MCSection *TextSec);
811 /// Get the .xdata section used for the given section.
812 MCSection *getAssociatedXDataSection(const MCSection *TextSec);
814 virtual void EmitSyntaxDirective();
816 /// \brief Emit a .reloc directive.
817 /// Returns true if the relocation could not be emitted because Name is not
819 virtual bool EmitRelocDirective(const MCExpr &Offset, StringRef Name,
820 const MCExpr *Expr, SMLoc Loc) {
824 /// \brief Emit the given \p Instruction into the current section.
825 virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI);
827 /// \brief Set the bundle alignment mode from now on in the section.
828 /// The argument is the power of 2 to which the alignment is set. The
829 /// value 0 means turn the bundle alignment off.
830 virtual void EmitBundleAlignMode(unsigned AlignPow2);
832 /// \brief The following instructions are a bundle-locked group.
834 /// \param AlignToEnd - If true, the bundle-locked group will be aligned to
835 /// the end of a bundle.
836 virtual void EmitBundleLock(bool AlignToEnd);
838 /// \brief Ends a bundle-locked group.
839 virtual void EmitBundleUnlock();
841 /// \brief If this file is backed by a assembly streamer, this dumps the
842 /// specified string in the output .s file. This capability is indicated by
843 /// the hasRawTextSupport() predicate. By default this aborts.
844 void EmitRawText(const Twine &String);
846 /// \brief Streamer specific finalization.
847 virtual void FinishImpl();
848 /// \brief Finish emission of machine code.
851 virtual bool mayHaveInstructions(MCSection &Sec) const { return true; }
854 /// Create a dummy machine code streamer, which does nothing. This is useful for
855 /// timing the assembler front end.
856 MCStreamer *createNullStreamer(MCContext &Ctx);
858 /// Create a machine code streamer which will print out assembly for the native
859 /// target, suitable for compiling with a native assembler.
861 /// \param InstPrint - If given, the instruction printer to use. If not given
862 /// the MCInst representation will be printed. This method takes ownership of
865 /// \param CE - If given, a code emitter to use to show the instruction
866 /// encoding inline with the assembly. This method takes ownership of \p CE.
868 /// \param TAB - If given, a target asm backend to use to show the fixup
869 /// information in conjunction with encoding information. This method takes
870 /// ownership of \p TAB.
872 /// \param ShowInst - Whether to show the MCInst representation inline with
874 MCStreamer *createAsmStreamer(MCContext &Ctx,
875 std::unique_ptr<formatted_raw_ostream> OS,
876 bool isVerboseAsm, bool useDwarfDirectory,
877 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
878 MCAsmBackend *TAB, bool ShowInst);
879 } // end namespace llvm