]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/MC/MCStreamer.h
Update lld to trunk r290819 and resolve conflicts.
[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/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"
26 #include <string>
27
28 namespace llvm {
29 class MCAsmBackend;
30 class MCCodeEmitter;
31 class MCContext;
32 class MCExpr;
33 class MCInst;
34 class MCInstPrinter;
35 class MCSection;
36 class MCStreamer;
37 class MCSymbolELF;
38 class MCSymbolRefExpr;
39 class MCSubtargetInfo;
40 class StringRef;
41 class Twine;
42 class raw_ostream;
43 class formatted_raw_ostream;
44 class AssemblerConstantPools;
45
46 typedef std::pair<MCSection *, const MCExpr *> MCSectionSubPair;
47
48 /// Target specific streamer interface. This is used so that targets can
49 /// implement support for target specific assembly directives.
50 ///
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
55 ///
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;
59 ///
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.
63 ///
64 /// In the assembly printer and parser the target streamer can be used by
65 /// calling getTargetStreamer and casting it to FooTargetStreamer:
66 ///
67 /// MCTargetStreamer &TS = OutStreamer.getTargetStreamer();
68 /// FooTargetStreamer &ATS = static_cast<FooTargetStreamer &>(TS);
69 ///
70 /// The base classes FooTargetAsmStreamer and FooTargetELFStreamer should
71 /// *never* be treated differently. Callers should always talk to a
72 /// FooTargetStreamer.
73 class MCTargetStreamer {
74 protected:
75   MCStreamer &Streamer;
76
77 public:
78   MCTargetStreamer(MCStreamer &S);
79   virtual ~MCTargetStreamer();
80
81   MCStreamer &getStreamer() { return Streamer; }
82
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);
87
88   virtual void prettyPrintAsm(MCInstPrinter &InstPrinter, raw_ostream &OS,
89                               const MCInst &Inst, const MCSubtargetInfo &STI);
90
91   virtual void finish();
92 };
93
94 // FIXME: declared here because it is used from
95 // lib/CodeGen/AsmPrinter/ARMException.cpp.
96 class ARMTargetStreamer : public MCTargetStreamer {
97 public:
98   ARMTargetStreamer(MCStreamer &S);
99   ~ARMTargetStreamer() override;
100
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,
108                          int64_t Offset = 0);
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,
112                            bool isVector);
113   virtual void emitUnwindRaw(int64_t StackOffset,
114                              const SmallVectorImpl<uint8_t> &Opcodes);
115
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');
127
128   virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
129
130   virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value);
131
132   void finish() override;
133
134   /// Reset any state between object emissions, i.e. the equivalent of
135   /// MCStreamer's reset method.
136   virtual void reset();
137
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);
142
143   /// Callback used to implemnt the .ltorg directive.
144   /// Emit contents of constant pool for the current section.
145   void emitCurrentConstantPool();
146
147 private:
148   std::unique_ptr<AssemblerConstantPools> ConstantPools;
149 };
150
151 /// \brief Streaming machine code generation interface.
152 ///
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.
157 ///
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.
160 ///
161 class MCStreamer {
162   MCContext &Context;
163   std::unique_ptr<MCTargetStreamer> TargetStreamer;
164
165   MCStreamer(const MCStreamer &) = delete;
166   MCStreamer &operator=(const MCStreamer &) = delete;
167
168   std::vector<MCDwarfFrameInfo> DwarfFrameInfos;
169   MCDwarfFrameInfo *getCurrentDwarfFrameInfo();
170   void EnsureValidDwarfFrame();
171
172   MCSymbol *EmitCFILabel();
173   MCSymbol *EmitCFICommon();
174
175   std::vector<WinEH::FrameInfo *> WinFrameInfos;
176   WinEH::FrameInfo *CurrentWinFrameInfo;
177   void EnsureValidWinFrameInfo();
178
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;
182
183   /// \brief This is stack of current and previous section values saved by
184   /// PushSection.
185   SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack;
186
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
190   /// requires.
191   unsigned NextWinCFIID = 0;
192
193 protected:
194   MCStreamer(MCContext &Ctx);
195
196   virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
197   virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
198
199   WinEH::FrameInfo *getCurrentWinFrameInfo() {
200     return CurrentWinFrameInfo;
201   }
202
203   virtual void EmitWindowsUnwindTables();
204
205   virtual void EmitRawTextImpl(StringRef String);
206
207 public:
208   virtual ~MCStreamer();
209
210   void visitUsedExpr(const MCExpr &Expr);
211   virtual void visitUsedSymbol(const MCSymbol &Sym);
212
213   void setTargetStreamer(MCTargetStreamer *TS) {
214     TargetStreamer.reset(TS);
215   }
216
217   /// State management
218   ///
219   virtual void reset();
220
221   MCContext &getContext() const { return Context; }
222
223   MCTargetStreamer *getTargetStreamer() {
224     return TargetStreamer.get();
225   }
226
227   unsigned getNumFrameInfos() { return DwarfFrameInfos.size(); }
228   ArrayRef<MCDwarfFrameInfo> getDwarfFrameInfos() const {
229     return DwarfFrameInfos;
230   }
231
232   bool hasUnfinishedDwarfFrameInfo();
233
234   unsigned getNumWinFrameInfos() { return WinFrameInfos.size(); }
235   ArrayRef<WinEH::FrameInfo *> getWinFrameInfos() const {
236     return WinFrameInfos;
237   }
238
239   void generateCompactUnwindEncodings(MCAsmBackend *MAB);
240
241   /// \name Assembly File Formatting.
242   /// @{
243
244   /// \brief Return true if this streamer supports verbose assembly and if it is
245   /// enabled.
246   virtual bool isVerboseAsm() const { return false; }
247
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; }
251
252   /// \brief Is the integrated assembler required for this streamer to function
253   /// correctly?
254   virtual bool isIntegratedAssemblerRequired() const { return false; }
255
256   /// \brief Add a textual comment.
257   ///
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.
262   ///
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) {}
270
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
273   /// method.
274   virtual raw_ostream &GetCommentOS();
275
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);
281
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();
287
288   /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
289   virtual void AddBlankLine() {}
290
291   /// @}
292
293   /// \name Symbol & Section Management
294   /// @{
295
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();
301   }
302   MCSection *getCurrentSectionOnly() const { return getCurrentSection().first; }
303
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();
309   }
310
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);
315   }
316
317   /// \brief Update streamer for a new active section.
318   ///
319   /// This is called by PopSection and SwitchSection, if the current
320   /// section changes.
321   virtual void ChangeSection(MCSection *, const MCExpr *);
322
323   /// \brief Save the current and previous section on the section stack.
324   void PushSection() {
325     SectionStack.push_back(
326         std::make_pair(getCurrentSection(), getPreviousSection()));
327   }
328
329   /// \brief Restore the current and previous section from the section stack.
330   /// Calls ChangeSection as needed.
331   ///
332   /// Returns false if the stack was empty.
333   bool PopSection() {
334     if (SectionStack.size() <= 1)
335       return false;
336     auto I = SectionStack.end();
337     --I;
338     MCSectionSubPair OldSection = I->first;
339     --I;
340     MCSectionSubPair NewSection = I->first;
341
342     if (OldSection != NewSection)
343       ChangeSection(NewSection.first, NewSection.second);
344     SectionStack.pop_back();
345     return true;
346   }
347
348   bool SubSection(const MCExpr *Subsection) {
349     if (SectionStack.empty())
350       return false;
351
352     SwitchSection(SectionStack.back().first.first, Subsection);
353     return true;
354   }
355
356   /// Set the current section where code is being emitted to \p Section.  This
357   /// is required to update CurSection.
358   ///
359   /// This corresponds to assembler directives like .section, .text, etc.
360   virtual void SwitchSection(MCSection *Section,
361                              const MCExpr *Subsection = nullptr);
362
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
365   /// ChangeSection.
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);
373   }
374
375   /// \brief Create the default sections and set the initial one.
376   virtual void InitSections(bool NoExecStack);
377
378   MCSymbol *endSection(MCSection *Section);
379
380   /// \brief Sets the symbol's section.
381   ///
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);
385
386   /// \brief Emit a label for \p Symbol into the current section.
387   ///
388   /// This corresponds to an assembler statement such as:
389   ///   foo:
390   ///
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);
397
398   virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol);
399
400   /// \brief Note in the output the specified \p Flag.
401   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
402
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) {}
406
407   /// \brief Note in the output the specified region \p Kind.
408   virtual void EmitDataRegion(MCDataRegionType Kind) {}
409
410   /// \brief Specify the MachO minimum deployment target version.
411   virtual void EmitVersionMin(MCVersionMinType, unsigned Major, unsigned Minor,
412                               unsigned Update) {}
413
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);
417
418   /// \brief Emit an assignment of \p Value to \p Symbol.
419   ///
420   /// This corresponds to an assembler statement such as:
421   ///  symbol = value
422   ///
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.
426   ///
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);
430
431   /// \brief Emit an weak reference from \p Alias to \p Symbol.
432   ///
433   /// This corresponds to an assembler statement such as:
434   ///  .weakref alias, symbol
435   ///
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);
439
440   /// \brief Add the given \p Attribute to \p Symbol.
441   virtual bool EmitSymbolAttribute(MCSymbol *Symbol,
442                                    MCSymbolAttr Attribute) = 0;
443
444   /// \brief Set the \p DescValue for the \p Symbol.
445   ///
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);
449
450   /// \brief Start emitting COFF symbol definition
451   ///
452   /// \param Symbol - The symbol to have its External & Type fields set.
453   virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
454
455   /// \brief Emit the storage class of the symbol.
456   ///
457   /// \param StorageClass - The storage class the symbol should have.
458   virtual void EmitCOFFSymbolStorageClass(int StorageClass);
459
460   /// \brief Emit the type of the symbol.
461   ///
462   /// \param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
463   virtual void EmitCOFFSymbolType(int Type);
464
465   /// \brief Marks the end of the symbol definition.
466   virtual void EndCOFFSymbolDef();
467
468   virtual void EmitCOFFSafeSEH(MCSymbol const *Symbol);
469
470   /// \brief Emits a COFF section index.
471   ///
472   /// \param Symbol - Symbol the section number relocation should point to.
473   virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
474
475   /// \brief Emits a COFF section relative relocation.
476   ///
477   /// \param Symbol - Symbol the section relative relocation should point to.
478   virtual void EmitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset);
479
480   /// \brief Emit an ELF .size directive.
481   ///
482   /// This corresponds to an assembler statement such as:
483   ///  .size symbol, expression
484   virtual void emitELFSize(MCSymbol *Symbol, const MCExpr *Value);
485
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) {}
489
490   /// \brief Emit a common symbol.
491   ///
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;
498
499   /// \brief Emit a local common (.lcomm) symbol.
500   ///
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);
506
507   /// \brief Emit the zerofill section and an optional symbol.
508   ///
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;
516
517   /// \brief Emit a thread local bss (.tbss) symbol.
518   ///
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);
526
527   /// @}
528   /// \name Generating Data
529   /// @{
530
531   /// \brief Emit the bytes in \p Data into the output.
532   ///
533   /// This is used to implement assembler directives such as .byte, .ascii,
534   /// etc.
535   virtual void EmitBytes(StringRef Data);
536
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);
540
541   /// \brief Emit the expression \p Value into the output as a native
542   /// integer of the given \p Size bytes.
543   ///
544   /// This is used to implement assembler directives such as .word, .quad,
545   /// etc.
546   ///
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());
553
554   void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc = SMLoc());
555
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);
559
560   virtual void EmitULEB128Value(const MCExpr *Value);
561
562   virtual void EmitSLEB128Value(const MCExpr *Value);
563
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);
567
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);
571
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);
576
577   /// \brief Emit the expression \p Value into the output as a dtprel
578   /// (64-bit DTP relative) value.
579   ///
580   /// This is used to implement assembler directives such as .dtpreldword on
581   /// targets that support them.
582   virtual void EmitDTPRel64Value(const MCExpr *Value);
583
584   /// \brief Emit the expression \p Value into the output as a dtprel
585   /// (32-bit DTP relative) value.
586   ///
587   /// This is used to implement assembler directives such as .dtprelword on
588   /// targets that support them.
589   virtual void EmitDTPRel32Value(const MCExpr *Value);
590
591   /// \brief Emit the expression \p Value into the output as a tprel
592   /// (64-bit TP relative) value.
593   ///
594   /// This is used to implement assembler directives such as .tpreldword on
595   /// targets that support them.
596   virtual void EmitTPRel64Value(const MCExpr *Value);
597
598   /// \brief Emit the expression \p Value into the output as a tprel
599   /// (32-bit TP relative) value.
600   ///
601   /// This is used to implement assembler directives such as .tprelword on
602   /// targets that support them.
603   virtual void EmitTPRel32Value(const MCExpr *Value);
604
605   /// \brief Emit the expression \p Value into the output as a gprel64 (64-bit
606   /// GP relative) value.
607   ///
608   /// This is used to implement assembler directives such as .gpdword on
609   /// targets that support them.
610   virtual void EmitGPRel64Value(const MCExpr *Value);
611
612   /// \brief Emit the expression \p Value into the output as a gprel32 (32-bit
613   /// GP relative) value.
614   ///
615   /// This is used to implement assembler directives such as .gprel32 on
616   /// targets that support them.
617   virtual void EmitGPRel32Value(const MCExpr *Value);
618
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);
622
623   /// \brief Emit \p Size bytes worth of the value specified by \p FillValue.
624   ///
625   /// This is used to implement assembler directives such as .space or .skip.
626   ///
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());
632
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.
635   ///
636   /// This is used to implement assembler directives such as .fill.
637   ///
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());
644
645   /// \brief Emit NumBytes worth of zeros.
646   /// This function properly handles data in virtual sections.
647   void EmitZeros(uint64_t NumBytes);
648
649   /// \brief Emit some number of copies of \p Value until the byte alignment \p
650   /// ByteAlignment is reached.
651   ///
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
654   /// undefined.
655   ///
656   /// This used to implement the .align assembler directive.
657   ///
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
665   /// emitted.
666   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
667                                     unsigned ValueSize = 1,
668                                     unsigned MaxBytesToEmit = 0);
669
670   /// \brief Emit nops until the byte alignment \p ByteAlignment is reached.
671   ///
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.
674   ///
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
679   /// emitted.
680   virtual void EmitCodeAlignment(unsigned ByteAlignment,
681                                  unsigned MaxBytesToEmit = 0);
682
683   /// \brief Emit some number of copies of \p Value until the byte offset \p
684   /// Offset is reached.
685   ///
686   /// This is used to implement assembler directives such as .org.
687   ///
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,
692                                  SMLoc Loc);
693
694   /// @}
695
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);
699
700   /// \brief Emit the "identifiers" directive.  This implements the
701   /// '.ident "version foo"' assembler directive.
702   virtual void EmitIdent(StringRef IdentString) {}
703
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,
707                                           StringRef Filename,
708                                           unsigned CUID = 0);
709
710   /// \brief This implements the DWARF2 '.loc fileno lineno ...' assembler
711   /// directive.
712   virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
713                                      unsigned Column, unsigned Flags,
714                                      unsigned Isa, unsigned Discriminator,
715                                      StringRef FileName);
716
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
719   /// success.
720   virtual bool EmitCVFileDirective(unsigned FileNo, StringRef Filename);
721
722   /// \brief Introduces a function id for use with .cv_loc.
723   virtual bool EmitCVFuncIdDirective(unsigned FunctionId);
724
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);
730
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);
736
737   /// \brief This implements the CodeView '.cv_linetable' assembler directive.
738   virtual void EmitCVLinetableDirective(unsigned FunctionId,
739                                         const MCSymbol *FnStart,
740                                         const MCSymbol *FnEnd);
741
742   /// \brief This implements the CodeView '.cv_inline_linetable' assembler
743   /// directive.
744   virtual void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
745                                               unsigned SourceFileId,
746                                               unsigned SourceLineNum,
747                                               const MCSymbol *FnStartSym,
748                                               const MCSymbol *FnEndSym);
749
750   /// \brief This implements the CodeView '.cv_def_range' assembler
751   /// directive.
752   virtual void EmitCVDefRangeDirective(
753       ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
754       StringRef FixedSizePortion);
755
756   /// \brief This implements the CodeView '.cv_stringtable' assembler directive.
757   virtual void EmitCVStringTableDirective() {}
758
759   /// \brief This implements the CodeView '.cv_filechecksums' assembler directive.
760   virtual void EmitCVFileChecksumsDirective() {}
761
762   /// Emit the absolute difference between two symbols.
763   ///
764   /// \pre Offset of \c Hi is greater than the offset \c Lo.
765   virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
766                                       unsigned Size);
767
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();
790
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();
802
803   virtual void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except);
804   virtual void EmitWinEHHandlerData();
805
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);
810
811   /// Get the .xdata section used for the given section.
812   MCSection *getAssociatedXDataSection(const MCSection *TextSec);
813
814   virtual void EmitSyntaxDirective();
815
816   /// \brief Emit a .reloc directive.
817   /// Returns true if the relocation could not be emitted because Name is not
818   /// known.
819   virtual bool EmitRelocDirective(const MCExpr &Offset, StringRef Name,
820                                   const MCExpr *Expr, SMLoc Loc) {
821     return true;
822   }
823
824   /// \brief Emit the given \p Instruction into the current section.
825   virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI);
826
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);
831
832   /// \brief The following instructions are a bundle-locked group.
833   ///
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);
837
838   /// \brief Ends a bundle-locked group.
839   virtual void EmitBundleUnlock();
840
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);
845
846   /// \brief Streamer specific finalization.
847   virtual void FinishImpl();
848   /// \brief Finish emission of machine code.
849   void Finish();
850
851   virtual bool mayHaveInstructions(MCSection &Sec) const { return true; }
852 };
853
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);
857
858 /// Create a machine code streamer which will print out assembly for the native
859 /// target, suitable for compiling with a native assembler.
860 ///
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
863 /// InstPrint.
864 ///
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.
867 ///
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.
871 ///
872 /// \param ShowInst - Whether to show the MCInst representation inline with
873 /// the assembly.
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
880
881 #endif