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;
47 class MCSubtargetInfo;
51 using MCSectionSubPair = std::pair<MCSection *, const MCExpr *>;
53 /// Target specific streamer interface. This is used so that targets can
54 /// implement support for target specific assembly directives.
56 /// If target foo wants to use this, it should implement 3 classes:
57 /// * FooTargetStreamer : public MCTargetStreamer
58 /// * FooTargetAsmStreamer : public FooTargetStreamer
59 /// * FooTargetELFStreamer : public FooTargetStreamer
61 /// FooTargetStreamer should have a pure virtual method for each directive. For
62 /// example, for a ".bar symbol_name" directive, it should have
63 /// virtual emitBar(const MCSymbol &Symbol) = 0;
65 /// The FooTargetAsmStreamer and FooTargetELFStreamer classes implement the
66 /// method. The assembly streamer just prints ".bar symbol_name". The object
67 /// streamer does whatever is needed to implement .bar in the object file.
69 /// In the assembly printer and parser the target streamer can be used by
70 /// calling getTargetStreamer and casting it to FooTargetStreamer:
72 /// MCTargetStreamer &TS = OutStreamer.getTargetStreamer();
73 /// FooTargetStreamer &ATS = static_cast<FooTargetStreamer &>(TS);
75 /// The base classes FooTargetAsmStreamer and FooTargetELFStreamer should
76 /// *never* be treated differently. Callers should always talk to a
77 /// FooTargetStreamer.
78 class MCTargetStreamer {
83 MCTargetStreamer(MCStreamer &S);
84 virtual ~MCTargetStreamer();
86 MCStreamer &getStreamer() { return Streamer; }
88 // Allow a target to add behavior to the EmitLabel of MCStreamer.
89 virtual void emitLabel(MCSymbol *Symbol);
90 // Allow a target to add behavior to the emitAssignment of MCStreamer.
91 virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value);
93 virtual void prettyPrintAsm(MCInstPrinter &InstPrinter, raw_ostream &OS,
94 const MCInst &Inst, const MCSubtargetInfo &STI);
96 virtual void finish();
99 // FIXME: declared here because it is used from
100 // lib/CodeGen/AsmPrinter/ARMException.cpp.
101 class ARMTargetStreamer : public MCTargetStreamer {
103 ARMTargetStreamer(MCStreamer &S);
104 ~ARMTargetStreamer() override;
106 virtual void emitFnStart();
107 virtual void emitFnEnd();
108 virtual void emitCantUnwind();
109 virtual void emitPersonality(const MCSymbol *Personality);
110 virtual void emitPersonalityIndex(unsigned Index);
111 virtual void emitHandlerData();
112 virtual void emitSetFP(unsigned FpReg, unsigned SpReg,
114 virtual void emitMovSP(unsigned Reg, int64_t Offset = 0);
115 virtual void emitPad(int64_t Offset);
116 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
118 virtual void emitUnwindRaw(int64_t StackOffset,
119 const SmallVectorImpl<uint8_t> &Opcodes);
121 virtual void switchVendor(StringRef Vendor);
122 virtual void emitAttribute(unsigned Attribute, unsigned Value);
123 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
124 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
125 StringRef StringValue = "");
126 virtual void emitFPU(unsigned FPU);
127 virtual void emitArch(unsigned Arch);
128 virtual void emitArchExtension(unsigned ArchExt);
129 virtual void emitObjectArch(unsigned Arch);
130 void emitTargetAttributes(const MCSubtargetInfo &STI);
131 virtual void finishAttributeSection();
132 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
134 virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
136 virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value);
138 void finish() override;
140 /// Reset any state between object emissions, i.e. the equivalent of
141 /// MCStreamer's reset method.
142 virtual void reset();
144 /// Callback used to implement the ldr= pseudo.
145 /// Add a new entry to the constant pool for the current section and return an
146 /// MCExpr that can be used to refer to the constant pool location.
147 const MCExpr *addConstantPoolEntry(const MCExpr *, SMLoc Loc);
149 /// Callback used to implemnt the .ltorg directive.
150 /// Emit contents of constant pool for the current section.
151 void emitCurrentConstantPool();
154 std::unique_ptr<AssemblerConstantPools> ConstantPools;
157 /// \brief Streaming machine code generation interface.
159 /// This interface is intended to provide a programatic interface that is very
160 /// similar to the level that an assembler .s file provides. It has callbacks
161 /// to emit bytes, handle directives, etc. The implementation of this interface
162 /// retains state to know what the current section is etc.
164 /// There are multiple implementations of this interface: one for writing out
165 /// a .s file, and implementations that write out .o files of various formats.
169 std::unique_ptr<MCTargetStreamer> TargetStreamer;
171 std::vector<MCDwarfFrameInfo> DwarfFrameInfos;
172 MCDwarfFrameInfo *getCurrentDwarfFrameInfo();
173 void EnsureValidDwarfFrame();
175 MCSymbol *EmitCFILabel();
176 MCSymbol *EmitCFICommon();
178 std::vector<WinEH::FrameInfo *> WinFrameInfos;
179 WinEH::FrameInfo *CurrentWinFrameInfo;
180 void EnsureValidWinFrameInfo();
182 /// \brief Tracks an index to represent the order a symbol was emitted in.
183 /// Zero means we did not emit that symbol.
184 DenseMap<const MCSymbol *, unsigned> SymbolOrdering;
186 /// \brief This is stack of current and previous section values saved by
188 SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack;
190 /// The next unique ID to use when creating a WinCFI-related section (.pdata
191 /// or .xdata). This ID ensures that we have a one-to-one mapping from
192 /// code section to unwind info section, which MSVC's incremental linker
194 unsigned NextWinCFIID = 0;
197 MCStreamer(MCContext &Ctx);
199 virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
200 virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
202 WinEH::FrameInfo *getCurrentWinFrameInfo() {
203 return CurrentWinFrameInfo;
206 virtual void EmitWindowsUnwindTables();
208 virtual void EmitRawTextImpl(StringRef String);
211 MCStreamer(const MCStreamer &) = delete;
212 MCStreamer &operator=(const MCStreamer &) = delete;
213 virtual ~MCStreamer();
215 void visitUsedExpr(const MCExpr &Expr);
216 virtual void visitUsedSymbol(const MCSymbol &Sym);
218 void setTargetStreamer(MCTargetStreamer *TS) {
219 TargetStreamer.reset(TS);
224 virtual void reset();
226 MCContext &getContext() const { return Context; }
228 MCTargetStreamer *getTargetStreamer() {
229 return TargetStreamer.get();
232 unsigned getNumFrameInfos() { return DwarfFrameInfos.size(); }
233 ArrayRef<MCDwarfFrameInfo> getDwarfFrameInfos() const {
234 return DwarfFrameInfos;
237 bool hasUnfinishedDwarfFrameInfo();
239 unsigned getNumWinFrameInfos() { return WinFrameInfos.size(); }
240 ArrayRef<WinEH::FrameInfo *> getWinFrameInfos() const {
241 return WinFrameInfos;
244 void generateCompactUnwindEncodings(MCAsmBackend *MAB);
246 /// \name Assembly File Formatting.
249 /// \brief Return true if this streamer supports verbose assembly and if it is
251 virtual bool isVerboseAsm() const { return false; }
253 /// \brief Return true if this asm streamer supports emitting unformatted text
254 /// to the .s file with EmitRawText.
255 virtual bool hasRawTextSupport() const { return false; }
257 /// \brief Is the integrated assembler required for this streamer to function
259 virtual bool isIntegratedAssemblerRequired() const { return false; }
261 /// \brief Add a textual comment.
263 /// Typically for comments that can be emitted to the generated .s
264 /// file if applicable as a QoI issue to make the output of the compiler
265 /// more readable. This only affects the MCAsmStreamer, and only when
266 /// verbose assembly output is enabled.
268 /// If the comment includes embedded \n's, they will each get the comment
269 /// prefix as appropriate. The added comment should not end with a \n.
270 /// By default, each comment is terminated with an end of line, i.e. the
271 /// EOL param is set to true by default. If one prefers not to end the
272 /// comment with a new line then the EOL param should be passed
273 /// with a false value.
274 virtual void AddComment(const Twine &T, bool EOL = true) {}
276 /// \brief Return a raw_ostream that comments can be written to. Unlike
277 /// AddComment, you are required to terminate comments with \n if you use this
279 virtual raw_ostream &GetCommentOS();
281 /// \brief Print T and prefix it with the comment string (normally #) and
282 /// optionally a tab. This prints the comment immediately, not at the end of
283 /// the current line. It is basically a safe version of EmitRawText: since it
284 /// only prints comments, the object streamer ignores it instead of asserting.
285 virtual void emitRawComment(const Twine &T, bool TabPrefix = true);
287 /// \brief Add explicit comment T. T is required to be a valid
288 /// comment in the output and does not need to be escaped.
289 virtual void addExplicitComment(const Twine &T);
291 /// \brief Emit added explicit comments.
292 virtual void emitExplicitComments();
294 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
295 virtual void AddBlankLine() {}
299 /// \name Symbol & Section Management
302 /// \brief Return the current section that the streamer is emitting code to.
303 MCSectionSubPair getCurrentSection() const {
304 if (!SectionStack.empty())
305 return SectionStack.back().first;
306 return MCSectionSubPair();
308 MCSection *getCurrentSectionOnly() const { return getCurrentSection().first; }
310 /// \brief Return the previous section that the streamer is emitting code to.
311 MCSectionSubPair getPreviousSection() const {
312 if (!SectionStack.empty())
313 return SectionStack.back().second;
314 return MCSectionSubPair();
317 /// \brief Returns an index to represent the order a symbol was emitted in.
318 /// (zero if we did not emit that symbol)
319 unsigned GetSymbolOrder(const MCSymbol *Sym) const {
320 return SymbolOrdering.lookup(Sym);
323 /// \brief Update streamer for a new active section.
325 /// This is called by PopSection and SwitchSection, if the current
327 virtual void ChangeSection(MCSection *, const MCExpr *);
329 /// \brief Save the current and previous section on the section stack.
331 SectionStack.push_back(
332 std::make_pair(getCurrentSection(), getPreviousSection()));
335 /// \brief Restore the current and previous section from the section stack.
336 /// Calls ChangeSection as needed.
338 /// Returns false if the stack was empty.
340 if (SectionStack.size() <= 1)
342 auto I = SectionStack.end();
344 MCSectionSubPair OldSection = I->first;
346 MCSectionSubPair NewSection = I->first;
348 if (OldSection != NewSection)
349 ChangeSection(NewSection.first, NewSection.second);
350 SectionStack.pop_back();
354 bool SubSection(const MCExpr *Subsection) {
355 if (SectionStack.empty())
358 SwitchSection(SectionStack.back().first.first, Subsection);
362 /// Set the current section where code is being emitted to \p Section. This
363 /// is required to update CurSection.
365 /// This corresponds to assembler directives like .section, .text, etc.
366 virtual void SwitchSection(MCSection *Section,
367 const MCExpr *Subsection = nullptr);
369 /// \brief Set the current section where code is being emitted to \p Section.
370 /// This is required to update CurSection. This version does not call
372 void SwitchSectionNoChange(MCSection *Section,
373 const MCExpr *Subsection = nullptr) {
374 assert(Section && "Cannot switch to a null section!");
375 MCSectionSubPair curSection = SectionStack.back().first;
376 SectionStack.back().second = curSection;
377 if (MCSectionSubPair(Section, Subsection) != curSection)
378 SectionStack.back().first = MCSectionSubPair(Section, Subsection);
381 /// \brief Create the default sections and set the initial one.
382 virtual void InitSections(bool NoExecStack);
384 MCSymbol *endSection(MCSection *Section);
386 /// \brief Sets the symbol's section.
388 /// Each emitted symbol will be tracked in the ordering table,
389 /// so we can sort on them later.
390 void AssignFragment(MCSymbol *Symbol, MCFragment *Fragment);
392 /// \brief Emit a label for \p Symbol into the current section.
394 /// This corresponds to an assembler statement such as:
397 /// \param Symbol - The symbol to emit. A given symbol should only be
398 /// emitted as a label once, and symbols emitted as a label should never be
399 /// used in an assignment.
400 // FIXME: These emission are non-const because we mutate the symbol to
401 // add the section we're emitting it to later.
402 virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc());
404 virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol);
406 /// \brief Note in the output the specified \p Flag.
407 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
409 /// \brief Emit the given list \p Options of strings as linker
410 /// options into the output.
411 virtual void EmitLinkerOptions(ArrayRef<std::string> Kind) {}
413 /// \brief Note in the output the specified region \p Kind.
414 virtual void EmitDataRegion(MCDataRegionType Kind) {}
416 /// \brief Specify the MachO minimum deployment target version.
417 virtual void EmitVersionMin(MCVersionMinType, unsigned Major, unsigned Minor,
420 /// \brief Note in the output that the specified \p Func is a Thumb mode
421 /// function (ARM target only).
422 virtual void EmitThumbFunc(MCSymbol *Func);
424 /// \brief Emit an assignment of \p Value to \p Symbol.
426 /// This corresponds to an assembler statement such as:
429 /// The assignment generates no code, but has the side effect of binding the
430 /// value in the current context. For the assembly streamer, this prints the
431 /// binding into the .s file.
433 /// \param Symbol - The symbol being assigned to.
434 /// \param Value - The value for the symbol.
435 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
437 /// \brief Emit an weak reference from \p Alias to \p Symbol.
439 /// This corresponds to an assembler statement such as:
440 /// .weakref alias, symbol
442 /// \param Alias - The alias that is being created.
443 /// \param Symbol - The symbol being aliased.
444 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
446 /// \brief Add the given \p Attribute to \p Symbol.
447 virtual bool EmitSymbolAttribute(MCSymbol *Symbol,
448 MCSymbolAttr Attribute) = 0;
450 /// \brief Set the \p DescValue for the \p Symbol.
452 /// \param Symbol - The symbol to have its n_desc field set.
453 /// \param DescValue - The value to set into the n_desc field.
454 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
456 /// \brief Start emitting COFF symbol definition
458 /// \param Symbol - The symbol to have its External & Type fields set.
459 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
461 /// \brief Emit the storage class of the symbol.
463 /// \param StorageClass - The storage class the symbol should have.
464 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
466 /// \brief Emit the type of the symbol.
468 /// \param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
469 virtual void EmitCOFFSymbolType(int Type);
471 /// \brief Marks the end of the symbol definition.
472 virtual void EndCOFFSymbolDef();
474 virtual void EmitCOFFSafeSEH(MCSymbol const *Symbol);
476 /// \brief Emits a COFF section index.
478 /// \param Symbol - Symbol the section number relocation should point to.
479 virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
481 /// \brief Emits a COFF section relative relocation.
483 /// \param Symbol - Symbol the section relative relocation should point to.
484 virtual void EmitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset);
486 /// \brief Emit an ELF .size directive.
488 /// This corresponds to an assembler statement such as:
489 /// .size symbol, expression
490 virtual void emitELFSize(MCSymbol *Symbol, const MCExpr *Value);
492 /// \brief Emit an ELF .symver directive.
494 /// This corresponds to an assembler statement such as:
495 /// .symver _start, foo@@SOME_VERSION
496 /// \param Alias - The versioned alias (i.e. "foo@@SOME_VERSION")
497 /// \param Aliasee - The aliased symbol (i.e. "_start")
498 virtual void emitELFSymverDirective(MCSymbol *Alias, const MCSymbol *Aliasee);
500 /// \brief Emit a Linker Optimization Hint (LOH) directive.
501 /// \param Args - Arguments of the LOH.
502 virtual void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {}
504 /// \brief Emit a common symbol.
506 /// \param Symbol - The common symbol to emit.
507 /// \param Size - The size of the common symbol.
508 /// \param ByteAlignment - The alignment of the symbol if
509 /// non-zero. This must be a power of 2.
510 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
511 unsigned ByteAlignment) = 0;
513 /// \brief Emit a local common (.lcomm) symbol.
515 /// \param Symbol - The common symbol to emit.
516 /// \param Size - The size of the common symbol.
517 /// \param ByteAlignment - The alignment of the common symbol in bytes.
518 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
519 unsigned ByteAlignment);
521 /// \brief Emit the zerofill section and an optional symbol.
523 /// \param Section - The zerofill section to create and or to put the symbol
524 /// \param Symbol - The zerofill symbol to emit, if non-NULL.
525 /// \param Size - The size of the zerofill symbol.
526 /// \param ByteAlignment - The alignment of the zerofill symbol if
527 /// non-zero. This must be a power of 2 on some targets.
528 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
529 uint64_t Size = 0, unsigned ByteAlignment = 0) = 0;
531 /// \brief Emit a thread local bss (.tbss) symbol.
533 /// \param Section - The thread local common section.
534 /// \param Symbol - The thread local common symbol to emit.
535 /// \param Size - The size of the symbol.
536 /// \param ByteAlignment - The alignment of the thread local common symbol
537 /// if non-zero. This must be a power of 2 on some targets.
538 virtual void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
539 uint64_t Size, unsigned ByteAlignment = 0);
542 /// \name Generating Data
545 /// \brief Emit the bytes in \p Data into the output.
547 /// This is used to implement assembler directives such as .byte, .ascii,
549 virtual void EmitBytes(StringRef Data);
551 /// Functionally identical to EmitBytes. When emitting textual assembly, this
552 /// method uses .byte directives instead of .ascii or .asciz for readability.
553 virtual void EmitBinaryData(StringRef Data);
555 /// \brief Emit the expression \p Value into the output as a native
556 /// integer of the given \p Size bytes.
558 /// This is used to implement assembler directives such as .word, .quad,
561 /// \param Value - The value to emit.
562 /// \param Size - The size of the integer (in bytes) to emit. This must
563 /// match a native machine width.
564 /// \param Loc - The location of the expression for error reporting.
565 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
566 SMLoc Loc = SMLoc());
568 void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc = SMLoc());
570 /// \brief Special case of EmitValue that avoids the client having
571 /// to pass in a MCExpr for constant integers.
572 virtual void EmitIntValue(uint64_t Value, unsigned Size);
574 virtual void EmitULEB128Value(const MCExpr *Value);
576 virtual void EmitSLEB128Value(const MCExpr *Value);
578 /// \brief Special case of EmitULEB128Value that avoids the client having to
579 /// pass in a MCExpr for constant integers.
580 void EmitULEB128IntValue(uint64_t Value, unsigned Padding = 0);
582 /// \brief Special case of EmitSLEB128Value that avoids the client having to
583 /// pass in a MCExpr for constant integers.
584 void EmitSLEB128IntValue(int64_t Value);
586 /// \brief Special case of EmitValue that avoids the client having to pass in
587 /// a MCExpr for MCSymbols.
588 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
589 bool IsSectionRelative = false);
591 /// \brief Emit the expression \p Value into the output as a dtprel
592 /// (64-bit DTP relative) value.
594 /// This is used to implement assembler directives such as .dtpreldword on
595 /// targets that support them.
596 virtual void EmitDTPRel64Value(const MCExpr *Value);
598 /// \brief Emit the expression \p Value into the output as a dtprel
599 /// (32-bit DTP relative) value.
601 /// This is used to implement assembler directives such as .dtprelword on
602 /// targets that support them.
603 virtual void EmitDTPRel32Value(const MCExpr *Value);
605 /// \brief Emit the expression \p Value into the output as a tprel
606 /// (64-bit TP relative) value.
608 /// This is used to implement assembler directives such as .tpreldword on
609 /// targets that support them.
610 virtual void EmitTPRel64Value(const MCExpr *Value);
612 /// \brief Emit the expression \p Value into the output as a tprel
613 /// (32-bit TP relative) value.
615 /// This is used to implement assembler directives such as .tprelword on
616 /// targets that support them.
617 virtual void EmitTPRel32Value(const MCExpr *Value);
619 /// \brief Emit the expression \p Value into the output as a gprel64 (64-bit
620 /// GP relative) value.
622 /// This is used to implement assembler directives such as .gpdword on
623 /// targets that support them.
624 virtual void EmitGPRel64Value(const MCExpr *Value);
626 /// \brief Emit the expression \p Value into the output as a gprel32 (32-bit
627 /// GP relative) value.
629 /// This is used to implement assembler directives such as .gprel32 on
630 /// targets that support them.
631 virtual void EmitGPRel32Value(const MCExpr *Value);
633 /// \brief Emit NumBytes bytes worth of the value specified by FillValue.
634 /// This implements directives such as '.space'.
635 virtual void emitFill(uint64_t NumBytes, uint8_t FillValue);
637 /// \brief Emit \p Size bytes worth of the value specified by \p FillValue.
639 /// This is used to implement assembler directives such as .space or .skip.
641 /// \param NumBytes - The number of bytes to emit.
642 /// \param FillValue - The value to use when filling bytes.
643 /// \param Loc - The location of the expression for error reporting.
644 virtual void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
645 SMLoc Loc = SMLoc());
647 /// \brief Emit \p NumValues copies of \p Size bytes. Each \p Size bytes is
648 /// taken from the lowest order 4 bytes of \p Expr expression.
650 /// This is used to implement assembler directives such as .fill.
652 /// \param NumValues - The number of copies of \p Size bytes to emit.
653 /// \param Size - The size (in bytes) of each repeated value.
654 /// \param Expr - The expression from which \p Size bytes are used.
655 virtual void emitFill(uint64_t NumValues, int64_t Size, int64_t Expr);
656 virtual void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
657 SMLoc Loc = SMLoc());
659 /// \brief Emit NumBytes worth of zeros.
660 /// This function properly handles data in virtual sections.
661 void EmitZeros(uint64_t NumBytes);
663 /// \brief Emit some number of copies of \p Value until the byte alignment \p
664 /// ByteAlignment is reached.
666 /// If the number of bytes need to emit for the alignment is not a multiple
667 /// of \p ValueSize, then the contents of the emitted fill bytes is
670 /// This used to implement the .align assembler directive.
672 /// \param ByteAlignment - The alignment to reach. This must be a power of
673 /// two on some targets.
674 /// \param Value - The value to use when filling bytes.
675 /// \param ValueSize - The size of the integer (in bytes) to emit for
676 /// \p Value. This must match a native machine width.
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 EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
681 unsigned ValueSize = 1,
682 unsigned MaxBytesToEmit = 0);
684 /// \brief Emit nops until the byte alignment \p ByteAlignment is reached.
686 /// This used to align code where the alignment bytes may be executed. This
687 /// can emit different bytes for different sizes to optimize execution.
689 /// \param ByteAlignment - The alignment to reach. This must be a power of
690 /// two on some targets.
691 /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
692 /// the alignment cannot be reached in this many bytes, no bytes are
694 virtual void EmitCodeAlignment(unsigned ByteAlignment,
695 unsigned MaxBytesToEmit = 0);
697 /// \brief Emit some number of copies of \p Value until the byte offset \p
698 /// Offset is reached.
700 /// This is used to implement assembler directives such as .org.
702 /// \param Offset - The offset to reach. This may be an expression, but the
703 /// expression must be associated with the current section.
704 /// \param Value - The value to use when filling bytes.
705 virtual void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
710 /// \brief Switch to a new logical file. This is used to implement the '.file
711 /// "foo.c"' assembler directive.
712 virtual void EmitFileDirective(StringRef Filename);
714 /// \brief Emit the "identifiers" directive. This implements the
715 /// '.ident "version foo"' assembler directive.
716 virtual void EmitIdent(StringRef IdentString) {}
718 /// \brief Associate a filename with a specified logical file number. This
719 /// implements the DWARF2 '.file 4 "foo.c"' assembler directive.
720 virtual unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
724 /// \brief This implements the DWARF2 '.loc fileno lineno ...' assembler
726 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
727 unsigned Column, unsigned Flags,
728 unsigned Isa, unsigned Discriminator,
731 /// \brief Associate a filename with a specified logical file number. This
732 /// implements the '.cv_file 4 "foo.c"' assembler directive. Returns true on
734 virtual bool EmitCVFileDirective(unsigned FileNo, StringRef Filename);
736 /// \brief Introduces a function id for use with .cv_loc.
737 virtual bool EmitCVFuncIdDirective(unsigned FunctionId);
739 /// \brief Introduces an inline call site id for use with .cv_loc. Includes
740 /// extra information for inline line table generation.
741 virtual bool EmitCVInlineSiteIdDirective(unsigned FunctionId, unsigned IAFunc,
742 unsigned IAFile, unsigned IALine,
743 unsigned IACol, SMLoc Loc);
745 /// \brief This implements the CodeView '.cv_loc' assembler directive.
746 virtual void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo,
747 unsigned Line, unsigned Column,
748 bool PrologueEnd, bool IsStmt,
749 StringRef FileName, SMLoc Loc);
751 /// \brief This implements the CodeView '.cv_linetable' assembler directive.
752 virtual void EmitCVLinetableDirective(unsigned FunctionId,
753 const MCSymbol *FnStart,
754 const MCSymbol *FnEnd);
756 /// \brief This implements the CodeView '.cv_inline_linetable' assembler
758 virtual void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
759 unsigned SourceFileId,
760 unsigned SourceLineNum,
761 const MCSymbol *FnStartSym,
762 const MCSymbol *FnEndSym);
764 /// \brief This implements the CodeView '.cv_def_range' assembler
766 virtual void EmitCVDefRangeDirective(
767 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
768 StringRef FixedSizePortion);
770 /// \brief This implements the CodeView '.cv_stringtable' assembler directive.
771 virtual void EmitCVStringTableDirective() {}
773 /// \brief This implements the CodeView '.cv_filechecksums' assembler directive.
774 virtual void EmitCVFileChecksumsDirective() {}
776 /// Emit the absolute difference between two symbols.
778 /// \pre Offset of \c Hi is greater than the offset \c Lo.
779 virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
782 virtual MCSymbol *getDwarfLineTableSymbol(unsigned CUID);
783 virtual void EmitCFISections(bool EH, bool Debug);
784 void EmitCFIStartProc(bool IsSimple);
785 void EmitCFIEndProc();
786 virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
787 virtual void EmitCFIDefCfaOffset(int64_t Offset);
788 virtual void EmitCFIDefCfaRegister(int64_t Register);
789 virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
790 virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
791 virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
792 virtual void EmitCFIRememberState();
793 virtual void EmitCFIRestoreState();
794 virtual void EmitCFISameValue(int64_t Register);
795 virtual void EmitCFIRestore(int64_t Register);
796 virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
797 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
798 virtual void EmitCFIEscape(StringRef Values);
799 virtual void EmitCFIGnuArgsSize(int64_t Size);
800 virtual void EmitCFISignalFrame();
801 virtual void EmitCFIUndefined(int64_t Register);
802 virtual void EmitCFIRegister(int64_t Register1, int64_t Register2);
803 virtual void EmitCFIWindowSave();
805 virtual void EmitWinCFIStartProc(const MCSymbol *Symbol);
806 virtual void EmitWinCFIEndProc();
807 virtual void EmitWinCFIStartChained();
808 virtual void EmitWinCFIEndChained();
809 virtual void EmitWinCFIPushReg(unsigned Register);
810 virtual void EmitWinCFISetFrame(unsigned Register, unsigned Offset);
811 virtual void EmitWinCFIAllocStack(unsigned Size);
812 virtual void EmitWinCFISaveReg(unsigned Register, unsigned Offset);
813 virtual void EmitWinCFISaveXMM(unsigned Register, unsigned Offset);
814 virtual void EmitWinCFIPushFrame(bool Code);
815 virtual void EmitWinCFIEndProlog();
817 virtual void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except);
818 virtual void EmitWinEHHandlerData();
820 /// Get the .pdata section used for the given section. Typically the given
821 /// section is either the main .text section or some other COMDAT .text
822 /// section, but it may be any section containing code.
823 MCSection *getAssociatedPDataSection(const MCSection *TextSec);
825 /// Get the .xdata section used for the given section.
826 MCSection *getAssociatedXDataSection(const MCSection *TextSec);
828 virtual void EmitSyntaxDirective();
830 /// \brief Emit a .reloc directive.
831 /// Returns true if the relocation could not be emitted because Name is not
833 virtual bool EmitRelocDirective(const MCExpr &Offset, StringRef Name,
834 const MCExpr *Expr, SMLoc Loc) {
838 /// \brief Emit the given \p Instruction into the current section.
839 /// PrintSchedInfo == true then schedul comment should be added to output
840 virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
841 bool PrintSchedInfo = false);
843 /// \brief Set the bundle alignment mode from now on in the section.
844 /// The argument is the power of 2 to which the alignment is set. The
845 /// value 0 means turn the bundle alignment off.
846 virtual void EmitBundleAlignMode(unsigned AlignPow2);
848 /// \brief The following instructions are a bundle-locked group.
850 /// \param AlignToEnd - If true, the bundle-locked group will be aligned to
851 /// the end of a bundle.
852 virtual void EmitBundleLock(bool AlignToEnd);
854 /// \brief Ends a bundle-locked group.
855 virtual void EmitBundleUnlock();
857 /// \brief If this file is backed by a assembly streamer, this dumps the
858 /// specified string in the output .s file. This capability is indicated by
859 /// the hasRawTextSupport() predicate. By default this aborts.
860 void EmitRawText(const Twine &String);
862 /// \brief Streamer specific finalization.
863 virtual void FinishImpl();
864 /// \brief Finish emission of machine code.
867 virtual bool mayHaveInstructions(MCSection &Sec) const { return true; }
870 /// Create a dummy machine code streamer, which does nothing. This is useful for
871 /// timing the assembler front end.
872 MCStreamer *createNullStreamer(MCContext &Ctx);
874 /// Create a machine code streamer which will print out assembly for the native
875 /// target, suitable for compiling with a native assembler.
877 /// \param InstPrint - If given, the instruction printer to use. If not given
878 /// the MCInst representation will be printed. This method takes ownership of
881 /// \param CE - If given, a code emitter to use to show the instruction
882 /// encoding inline with the assembly. This method takes ownership of \p CE.
884 /// \param TAB - If given, a target asm backend to use to show the fixup
885 /// information in conjunction with encoding information. This method takes
886 /// ownership of \p TAB.
888 /// \param ShowInst - Whether to show the MCInst representation inline with
890 MCStreamer *createAsmStreamer(MCContext &Ctx,
891 std::unique_ptr<formatted_raw_ostream> OS,
892 bool isVerboseAsm, bool useDwarfDirectory,
893 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
894 MCAsmBackend *TAB, bool ShowInst);
896 } // end namespace llvm
898 #endif // LLVM_MC_MCSTREAMER_H