]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/MC/MCStreamer.h
Merge ^/head r316992 through r317215.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / MC / MCStreamer.h
1 //===- MCStreamer.h - High-level Streaming Machine Code Output --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the MCStreamer class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_MC_MCSTREAMER_H
15 #define LLVM_MC_MCSTREAMER_H
16
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/MC/MCDirectives.h"
22 #include "llvm/MC/MCDwarf.h"
23 #include "llvm/MC/MCLinkerOptimizationHint.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/MC/MCWinEH.h"
26 #include "llvm/Support/SMLoc.h"
27 #include <cassert>
28 #include <cstdint>
29 #include <memory>
30 #include <string>
31 #include <utility>
32 #include <vector>
33
34 namespace llvm {
35
36 class AssemblerConstantPools;
37 class formatted_raw_ostream;
38 class MCAsmBackend;
39 class MCCodeEmitter;
40 class MCContext;
41 class MCExpr;
42 class MCInst;
43 class MCInstPrinter;
44 class MCSection;
45 class MCStreamer;
46 class MCSymbolRefExpr;
47 class MCSymbolWasm;
48 class MCSubtargetInfo;
49 class raw_ostream;
50 class Twine;
51
52 typedef std::pair<MCSection *, const MCExpr *> MCSectionSubPair;
53
54 /// Target specific streamer interface. This is used so that targets can
55 /// implement support for target specific assembly directives.
56 ///
57 /// If target foo wants to use this, it should implement 3 classes:
58 /// * FooTargetStreamer : public MCTargetStreamer
59 /// * FooTargetAsmStreamer : public FooTargetStreamer
60 /// * FooTargetELFStreamer : public FooTargetStreamer
61 ///
62 /// FooTargetStreamer should have a pure virtual method for each directive. For
63 /// example, for a ".bar symbol_name" directive, it should have
64 /// virtual emitBar(const MCSymbol &Symbol) = 0;
65 ///
66 /// The FooTargetAsmStreamer and FooTargetELFStreamer classes implement the
67 /// method. The assembly streamer just prints ".bar symbol_name". The object
68 /// streamer does whatever is needed to implement .bar in the object file.
69 ///
70 /// In the assembly printer and parser the target streamer can be used by
71 /// calling getTargetStreamer and casting it to FooTargetStreamer:
72 ///
73 /// MCTargetStreamer &TS = OutStreamer.getTargetStreamer();
74 /// FooTargetStreamer &ATS = static_cast<FooTargetStreamer &>(TS);
75 ///
76 /// The base classes FooTargetAsmStreamer and FooTargetELFStreamer should
77 /// *never* be treated differently. Callers should always talk to a
78 /// FooTargetStreamer.
79 class MCTargetStreamer {
80 protected:
81   MCStreamer &Streamer;
82
83 public:
84   MCTargetStreamer(MCStreamer &S);
85   virtual ~MCTargetStreamer();
86
87   MCStreamer &getStreamer() { return Streamer; }
88
89   // Allow a target to add behavior to the EmitLabel of MCStreamer.
90   virtual void emitLabel(MCSymbol *Symbol);
91   // Allow a target to add behavior to the emitAssignment of MCStreamer.
92   virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value);
93
94   virtual void prettyPrintAsm(MCInstPrinter &InstPrinter, raw_ostream &OS,
95                               const MCInst &Inst, const MCSubtargetInfo &STI);
96
97   virtual void finish();
98 };
99
100 // FIXME: declared here because it is used from
101 // lib/CodeGen/AsmPrinter/ARMException.cpp.
102 class ARMTargetStreamer : public MCTargetStreamer {
103 public:
104   ARMTargetStreamer(MCStreamer &S);
105   ~ARMTargetStreamer() override;
106
107   virtual void emitFnStart();
108   virtual void emitFnEnd();
109   virtual void emitCantUnwind();
110   virtual void emitPersonality(const MCSymbol *Personality);
111   virtual void emitPersonalityIndex(unsigned Index);
112   virtual void emitHandlerData();
113   virtual void emitSetFP(unsigned FpReg, unsigned SpReg,
114                          int64_t Offset = 0);
115   virtual void emitMovSP(unsigned Reg, int64_t Offset = 0);
116   virtual void emitPad(int64_t Offset);
117   virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
118                            bool isVector);
119   virtual void emitUnwindRaw(int64_t StackOffset,
120                              const SmallVectorImpl<uint8_t> &Opcodes);
121
122   virtual void switchVendor(StringRef Vendor);
123   virtual void emitAttribute(unsigned Attribute, unsigned Value);
124   virtual void emitTextAttribute(unsigned Attribute, StringRef String);
125   virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
126                                     StringRef StringValue = "");
127   virtual void emitFPU(unsigned FPU);
128   virtual void emitArch(unsigned Arch);
129   virtual void emitArchExtension(unsigned ArchExt);
130   virtual void emitObjectArch(unsigned Arch);
131   virtual void finishAttributeSection();
132   virtual void emitInst(uint32_t Inst, char Suffix = '\0');
133
134   virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
135
136   virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value);
137
138   void finish() override;
139
140   /// Reset any state between object emissions, i.e. the equivalent of
141   /// MCStreamer's reset method.
142   virtual void reset();
143
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);
148
149   /// Callback used to implemnt the .ltorg directive.
150   /// Emit contents of constant pool for the current section.
151   void emitCurrentConstantPool();
152
153 private:
154   std::unique_ptr<AssemblerConstantPools> ConstantPools;
155 };
156
157 /// \brief Streaming machine code generation interface.
158 ///
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.
163 ///
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.
166 ///
167 class MCStreamer {
168   MCContext &Context;
169   std::unique_ptr<MCTargetStreamer> TargetStreamer;
170
171   std::vector<MCDwarfFrameInfo> DwarfFrameInfos;
172   MCDwarfFrameInfo *getCurrentDwarfFrameInfo();
173   void EnsureValidDwarfFrame();
174
175   MCSymbol *EmitCFILabel();
176   MCSymbol *EmitCFICommon();
177
178   std::vector<WinEH::FrameInfo *> WinFrameInfos;
179   WinEH::FrameInfo *CurrentWinFrameInfo;
180   void EnsureValidWinFrameInfo();
181
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;
185
186   /// \brief This is stack of current and previous section values saved by
187   /// PushSection.
188   SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack;
189
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
193   /// requires.
194   unsigned NextWinCFIID = 0;
195
196 protected:
197   MCStreamer(MCContext &Ctx);
198
199   virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
200   virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
201
202   WinEH::FrameInfo *getCurrentWinFrameInfo() {
203     return CurrentWinFrameInfo;
204   }
205
206   virtual void EmitWindowsUnwindTables();
207
208   virtual void EmitRawTextImpl(StringRef String);
209
210 public:
211   MCStreamer(const MCStreamer &) = delete;
212   MCStreamer &operator=(const MCStreamer &) = delete;
213   virtual ~MCStreamer();
214
215   void visitUsedExpr(const MCExpr &Expr);
216   virtual void visitUsedSymbol(const MCSymbol &Sym);
217
218   void setTargetStreamer(MCTargetStreamer *TS) {
219     TargetStreamer.reset(TS);
220   }
221
222   /// State management
223   ///
224   virtual void reset();
225
226   MCContext &getContext() const { return Context; }
227
228   MCTargetStreamer *getTargetStreamer() {
229     return TargetStreamer.get();
230   }
231
232   unsigned getNumFrameInfos() { return DwarfFrameInfos.size(); }
233   ArrayRef<MCDwarfFrameInfo> getDwarfFrameInfos() const {
234     return DwarfFrameInfos;
235   }
236
237   bool hasUnfinishedDwarfFrameInfo();
238
239   unsigned getNumWinFrameInfos() { return WinFrameInfos.size(); }
240   ArrayRef<WinEH::FrameInfo *> getWinFrameInfos() const {
241     return WinFrameInfos;
242   }
243
244   void generateCompactUnwindEncodings(MCAsmBackend *MAB);
245
246   /// \name Assembly File Formatting.
247   /// @{
248
249   /// \brief Return true if this streamer supports verbose assembly and if it is
250   /// enabled.
251   virtual bool isVerboseAsm() const { return false; }
252
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; }
256
257   /// \brief Is the integrated assembler required for this streamer to function
258   /// correctly?
259   virtual bool isIntegratedAssemblerRequired() const { return false; }
260
261   /// \brief Add a textual comment.
262   ///
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.
267   ///
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) {}
275
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
278   /// method.
279   virtual raw_ostream &GetCommentOS();
280
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);
286
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);
290
291   /// \brief Emit added explicit comments.
292   virtual void emitExplicitComments();
293
294   /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
295   virtual void AddBlankLine() {}
296
297   /// @}
298
299   /// \name Symbol & Section Management
300   /// @{
301
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();
307   }
308   MCSection *getCurrentSectionOnly() const { return getCurrentSection().first; }
309
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();
315   }
316
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);
321   }
322
323   /// \brief Update streamer for a new active section.
324   ///
325   /// This is called by PopSection and SwitchSection, if the current
326   /// section changes.
327   virtual void ChangeSection(MCSection *, const MCExpr *);
328
329   /// \brief Save the current and previous section on the section stack.
330   void PushSection() {
331     SectionStack.push_back(
332         std::make_pair(getCurrentSection(), getPreviousSection()));
333   }
334
335   /// \brief Restore the current and previous section from the section stack.
336   /// Calls ChangeSection as needed.
337   ///
338   /// Returns false if the stack was empty.
339   bool PopSection() {
340     if (SectionStack.size() <= 1)
341       return false;
342     auto I = SectionStack.end();
343     --I;
344     MCSectionSubPair OldSection = I->first;
345     --I;
346     MCSectionSubPair NewSection = I->first;
347
348     if (OldSection != NewSection)
349       ChangeSection(NewSection.first, NewSection.second);
350     SectionStack.pop_back();
351     return true;
352   }
353
354   bool SubSection(const MCExpr *Subsection) {
355     if (SectionStack.empty())
356       return false;
357
358     SwitchSection(SectionStack.back().first.first, Subsection);
359     return true;
360   }
361
362   /// Set the current section where code is being emitted to \p Section.  This
363   /// is required to update CurSection.
364   ///
365   /// This corresponds to assembler directives like .section, .text, etc.
366   virtual void SwitchSection(MCSection *Section,
367                              const MCExpr *Subsection = nullptr);
368
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
371   /// ChangeSection.
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);
379   }
380
381   /// \brief Create the default sections and set the initial one.
382   virtual void InitSections(bool NoExecStack);
383
384   MCSymbol *endSection(MCSection *Section);
385
386   /// \brief Sets the symbol's section.
387   ///
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);
391
392   /// \brief Emit a label for \p Symbol into the current section.
393   ///
394   /// This corresponds to an assembler statement such as:
395   ///   foo:
396   ///
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());
403
404   virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol);
405
406   /// \brief Note in the output the specified \p Flag.
407   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
408
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) {}
412
413   /// \brief Note in the output the specified region \p Kind.
414   virtual void EmitDataRegion(MCDataRegionType Kind) {}
415
416   /// \brief Specify the MachO minimum deployment target version.
417   virtual void EmitVersionMin(MCVersionMinType, unsigned Major, unsigned Minor,
418                               unsigned Update) {}
419
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);
423
424   /// \brief Emit an assignment of \p Value to \p Symbol.
425   ///
426   /// This corresponds to an assembler statement such as:
427   ///  symbol = value
428   ///
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.
432   ///
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);
436
437   /// \brief Emit an weak reference from \p Alias to \p Symbol.
438   ///
439   /// This corresponds to an assembler statement such as:
440   ///  .weakref alias, symbol
441   ///
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);
445
446   /// \brief Add the given \p Attribute to \p Symbol.
447   virtual bool EmitSymbolAttribute(MCSymbol *Symbol,
448                                    MCSymbolAttr Attribute) = 0;
449
450   /// \brief Set the \p DescValue for the \p Symbol.
451   ///
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);
455
456   /// \brief Start emitting COFF symbol definition
457   ///
458   /// \param Symbol - The symbol to have its External & Type fields set.
459   virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
460
461   /// \brief Emit the storage class of the symbol.
462   ///
463   /// \param StorageClass - The storage class the symbol should have.
464   virtual void EmitCOFFSymbolStorageClass(int StorageClass);
465
466   /// \brief Emit the type of the symbol.
467   ///
468   /// \param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
469   virtual void EmitCOFFSymbolType(int Type);
470
471   /// \brief Marks the end of the symbol definition.
472   virtual void EndCOFFSymbolDef();
473
474   virtual void EmitCOFFSafeSEH(MCSymbol const *Symbol);
475
476   /// \brief Emits a COFF section index.
477   ///
478   /// \param Symbol - Symbol the section number relocation should point to.
479   virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
480
481   /// \brief Emits a COFF section relative relocation.
482   ///
483   /// \param Symbol - Symbol the section relative relocation should point to.
484   virtual void EmitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset);
485
486   /// \brief Emit an ELF .size directive.
487   ///
488   /// This corresponds to an assembler statement such as:
489   ///  .size symbol, expression
490   virtual void emitELFSize(MCSymbol *Symbol, const MCExpr *Value);
491
492   /// \brief Emit an ELF .symver directive.
493   ///
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);
499
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) {}
503
504   /// \brief Emit a common symbol.
505   ///
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;
512
513   /// \brief Emit a local common (.lcomm) symbol.
514   ///
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);
520
521   /// \brief Emit the zerofill section and an optional symbol.
522   ///
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;
530
531   /// \brief Emit a thread local bss (.tbss) symbol.
532   ///
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);
540
541   /// @}
542   /// \name Generating Data
543   /// @{
544
545   /// \brief Emit the bytes in \p Data into the output.
546   ///
547   /// This is used to implement assembler directives such as .byte, .ascii,
548   /// etc.
549   virtual void EmitBytes(StringRef Data);
550
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);
554
555   /// \brief Emit the expression \p Value into the output as a native
556   /// integer of the given \p Size bytes.
557   ///
558   /// This is used to implement assembler directives such as .word, .quad,
559   /// etc.
560   ///
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());
567
568   void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc = SMLoc());
569
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);
573
574   virtual void EmitULEB128Value(const MCExpr *Value);
575
576   virtual void EmitSLEB128Value(const MCExpr *Value);
577
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);
581
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);
585
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);
590
591   /// \brief Emit the expression \p Value into the output as a dtprel
592   /// (64-bit DTP relative) value.
593   ///
594   /// This is used to implement assembler directives such as .dtpreldword on
595   /// targets that support them.
596   virtual void EmitDTPRel64Value(const MCExpr *Value);
597
598   /// \brief Emit the expression \p Value into the output as a dtprel
599   /// (32-bit DTP relative) value.
600   ///
601   /// This is used to implement assembler directives such as .dtprelword on
602   /// targets that support them.
603   virtual void EmitDTPRel32Value(const MCExpr *Value);
604
605   /// \brief Emit the expression \p Value into the output as a tprel
606   /// (64-bit TP relative) value.
607   ///
608   /// This is used to implement assembler directives such as .tpreldword on
609   /// targets that support them.
610   virtual void EmitTPRel64Value(const MCExpr *Value);
611
612   /// \brief Emit the expression \p Value into the output as a tprel
613   /// (32-bit TP relative) value.
614   ///
615   /// This is used to implement assembler directives such as .tprelword on
616   /// targets that support them.
617   virtual void EmitTPRel32Value(const MCExpr *Value);
618
619   /// \brief Emit the expression \p Value into the output as a gprel64 (64-bit
620   /// GP relative) value.
621   ///
622   /// This is used to implement assembler directives such as .gpdword on
623   /// targets that support them.
624   virtual void EmitGPRel64Value(const MCExpr *Value);
625
626   /// \brief Emit the expression \p Value into the output as a gprel32 (32-bit
627   /// GP relative) value.
628   ///
629   /// This is used to implement assembler directives such as .gprel32 on
630   /// targets that support them.
631   virtual void EmitGPRel32Value(const MCExpr *Value);
632
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);
636
637   /// \brief Emit \p Size bytes worth of the value specified by \p FillValue.
638   ///
639   /// This is used to implement assembler directives such as .space or .skip.
640   ///
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());
646
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.
649   ///
650   /// This is used to implement assembler directives such as .fill.
651   ///
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());
658
659   /// \brief Emit NumBytes worth of zeros.
660   /// This function properly handles data in virtual sections.
661   void EmitZeros(uint64_t NumBytes);
662
663   /// \brief Emit some number of copies of \p Value until the byte alignment \p
664   /// ByteAlignment is reached.
665   ///
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
668   /// undefined.
669   ///
670   /// This used to implement the .align assembler directive.
671   ///
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
679   /// emitted.
680   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
681                                     unsigned ValueSize = 1,
682                                     unsigned MaxBytesToEmit = 0);
683
684   /// \brief Emit nops until the byte alignment \p ByteAlignment is reached.
685   ///
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.
688   ///
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
693   /// emitted.
694   virtual void EmitCodeAlignment(unsigned ByteAlignment,
695                                  unsigned MaxBytesToEmit = 0);
696
697   /// \brief Emit some number of copies of \p Value until the byte offset \p
698   /// Offset is reached.
699   ///
700   /// This is used to implement assembler directives such as .org.
701   ///
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,
706                                  SMLoc Loc);
707
708   /// @}
709
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);
713
714   /// \brief Emit the "identifiers" directive.  This implements the
715   /// '.ident "version foo"' assembler directive.
716   virtual void EmitIdent(StringRef IdentString) {}
717
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,
721                                           StringRef Filename,
722                                           unsigned CUID = 0);
723
724   /// \brief This implements the DWARF2 '.loc fileno lineno ...' assembler
725   /// directive.
726   virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
727                                      unsigned Column, unsigned Flags,
728                                      unsigned Isa, unsigned Discriminator,
729                                      StringRef FileName);
730
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
733   /// success.
734   virtual bool EmitCVFileDirective(unsigned FileNo, StringRef Filename);
735
736   /// \brief Introduces a function id for use with .cv_loc.
737   virtual bool EmitCVFuncIdDirective(unsigned FunctionId);
738
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);
744
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);
750
751   /// \brief This implements the CodeView '.cv_linetable' assembler directive.
752   virtual void EmitCVLinetableDirective(unsigned FunctionId,
753                                         const MCSymbol *FnStart,
754                                         const MCSymbol *FnEnd);
755
756   /// \brief This implements the CodeView '.cv_inline_linetable' assembler
757   /// directive.
758   virtual void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
759                                               unsigned SourceFileId,
760                                               unsigned SourceLineNum,
761                                               const MCSymbol *FnStartSym,
762                                               const MCSymbol *FnEndSym);
763
764   /// \brief This implements the CodeView '.cv_def_range' assembler
765   /// directive.
766   virtual void EmitCVDefRangeDirective(
767       ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
768       StringRef FixedSizePortion);
769
770   /// \brief This implements the CodeView '.cv_stringtable' assembler directive.
771   virtual void EmitCVStringTableDirective() {}
772
773   /// \brief This implements the CodeView '.cv_filechecksums' assembler directive.
774   virtual void EmitCVFileChecksumsDirective() {}
775
776   /// Emit the absolute difference between two symbols.
777   ///
778   /// \pre Offset of \c Hi is greater than the offset \c Lo.
779   virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
780                                       unsigned Size);
781
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();
804
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();
816
817   virtual void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except);
818   virtual void EmitWinEHHandlerData();
819
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);
824
825   /// Get the .xdata section used for the given section.
826   MCSection *getAssociatedXDataSection(const MCSection *TextSec);
827
828   virtual void EmitSyntaxDirective();
829
830   /// \brief Emit a .reloc directive.
831   /// Returns true if the relocation could not be emitted because Name is not
832   /// known.
833   virtual bool EmitRelocDirective(const MCExpr &Offset, StringRef Name,
834                                   const MCExpr *Expr, SMLoc Loc) {
835     return true;
836   }
837
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);
842
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);
847
848   /// \brief The following instructions are a bundle-locked group.
849   ///
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);
853
854   /// \brief Ends a bundle-locked group.
855   virtual void EmitBundleUnlock();
856
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);
861
862   /// \brief Streamer specific finalization.
863   virtual void FinishImpl();
864   /// \brief Finish emission of machine code.
865   void Finish();
866
867   virtual bool mayHaveInstructions(MCSection &Sec) const { return true; }
868 };
869
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);
873
874 /// Create a machine code streamer which will print out assembly for the native
875 /// target, suitable for compiling with a native assembler.
876 ///
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
879 /// InstPrint.
880 ///
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.
883 ///
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.
887 ///
888 /// \param ShowInst - Whether to show the MCInst representation inline with
889 /// the assembly.
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);
895
896 } // end namespace llvm
897
898 #endif // LLVM_MC_MCSTREAMER_H