]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/MC/MCStreamer.h
Merge lld trunk r321017 to contrib/llvm/tools/lld.
[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 "llvm/Support/TargetParser.h"
28 #include <cassert>
29 #include <cstdint>
30 #include <memory>
31 #include <string>
32 #include <utility>
33 #include <vector>
34
35 namespace llvm {
36
37 class AssemblerConstantPools;
38 class formatted_raw_ostream;
39 class MCAsmBackend;
40 class MCCodeEmitter;
41 struct MCCodePaddingContext;
42 class MCContext;
43 class MCExpr;
44 class MCInst;
45 class MCInstPrinter;
46 class MCSection;
47 class MCStreamer;
48 class MCSymbolRefExpr;
49 class MCSubtargetInfo;
50 class raw_ostream;
51 class Twine;
52
53 using MCSectionSubPair = std::pair<MCSection *, const MCExpr *>;
54
55 /// Target specific streamer interface. This is used so that targets can
56 /// implement support for target specific assembly directives.
57 ///
58 /// If target foo wants to use this, it should implement 3 classes:
59 /// * FooTargetStreamer : public MCTargetStreamer
60 /// * FooTargetAsmStreamer : public FooTargetStreamer
61 /// * FooTargetELFStreamer : public FooTargetStreamer
62 ///
63 /// FooTargetStreamer should have a pure virtual method for each directive. For
64 /// example, for a ".bar symbol_name" directive, it should have
65 /// virtual emitBar(const MCSymbol &Symbol) = 0;
66 ///
67 /// The FooTargetAsmStreamer and FooTargetELFStreamer classes implement the
68 /// method. The assembly streamer just prints ".bar symbol_name". The object
69 /// streamer does whatever is needed to implement .bar in the object file.
70 ///
71 /// In the assembly printer and parser the target streamer can be used by
72 /// calling getTargetStreamer and casting it to FooTargetStreamer:
73 ///
74 /// MCTargetStreamer &TS = OutStreamer.getTargetStreamer();
75 /// FooTargetStreamer &ATS = static_cast<FooTargetStreamer &>(TS);
76 ///
77 /// The base classes FooTargetAsmStreamer and FooTargetELFStreamer should
78 /// *never* be treated differently. Callers should always talk to a
79 /// FooTargetStreamer.
80 class MCTargetStreamer {
81 protected:
82   MCStreamer &Streamer;
83
84 public:
85   MCTargetStreamer(MCStreamer &S);
86   virtual ~MCTargetStreamer();
87
88   MCStreamer &getStreamer() { return Streamer; }
89
90   // Allow a target to add behavior to the EmitLabel of MCStreamer.
91   virtual void emitLabel(MCSymbol *Symbol);
92   // Allow a target to add behavior to the emitAssignment of MCStreamer.
93   virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value);
94
95   virtual void prettyPrintAsm(MCInstPrinter &InstPrinter, raw_ostream &OS,
96                               const MCInst &Inst, const MCSubtargetInfo &STI);
97
98   virtual void finish();
99 };
100
101 // FIXME: declared here because it is used from
102 // lib/CodeGen/AsmPrinter/ARMException.cpp.
103 class ARMTargetStreamer : public MCTargetStreamer {
104 public:
105   ARMTargetStreamer(MCStreamer &S);
106   ~ARMTargetStreamer() override;
107
108   virtual void emitFnStart();
109   virtual void emitFnEnd();
110   virtual void emitCantUnwind();
111   virtual void emitPersonality(const MCSymbol *Personality);
112   virtual void emitPersonalityIndex(unsigned Index);
113   virtual void emitHandlerData();
114   virtual void emitSetFP(unsigned FpReg, unsigned SpReg,
115                          int64_t Offset = 0);
116   virtual void emitMovSP(unsigned Reg, int64_t Offset = 0);
117   virtual void emitPad(int64_t Offset);
118   virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
119                            bool isVector);
120   virtual void emitUnwindRaw(int64_t StackOffset,
121                              const SmallVectorImpl<uint8_t> &Opcodes);
122
123   virtual void switchVendor(StringRef Vendor);
124   virtual void emitAttribute(unsigned Attribute, unsigned Value);
125   virtual void emitTextAttribute(unsigned Attribute, StringRef String);
126   virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
127                                     StringRef StringValue = "");
128   virtual void emitFPU(unsigned FPU);
129   virtual void emitArch(ARM::ArchKind Arch);
130   virtual void emitArchExtension(unsigned ArchExt);
131   virtual void emitObjectArch(ARM::ArchKind Arch);
132   void emitTargetAttributes(const MCSubtargetInfo &STI);
133   virtual void finishAttributeSection();
134   virtual void emitInst(uint32_t Inst, char Suffix = '\0');
135
136   virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
137
138   virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value);
139
140   void finish() override;
141
142   /// Reset any state between object emissions, i.e. the equivalent of
143   /// MCStreamer's reset method.
144   virtual void reset();
145
146   /// Callback used to implement the ldr= pseudo.
147   /// Add a new entry to the constant pool for the current section and return an
148   /// MCExpr that can be used to refer to the constant pool location.
149   const MCExpr *addConstantPoolEntry(const MCExpr *, SMLoc Loc);
150
151   /// Callback used to implemnt the .ltorg directive.
152   /// Emit contents of constant pool for the current section.
153   void emitCurrentConstantPool();
154
155 private:
156   std::unique_ptr<AssemblerConstantPools> ConstantPools;
157 };
158
159 /// \brief Streaming machine code generation interface.
160 ///
161 /// This interface is intended to provide a programatic interface that is very
162 /// similar to the level that an assembler .s file provides.  It has callbacks
163 /// to emit bytes, handle directives, etc.  The implementation of this interface
164 /// retains state to know what the current section is etc.
165 ///
166 /// There are multiple implementations of this interface: one for writing out
167 /// a .s file, and implementations that write out .o files of various formats.
168 ///
169 class MCStreamer {
170   MCContext &Context;
171   std::unique_ptr<MCTargetStreamer> TargetStreamer;
172
173   std::vector<MCDwarfFrameInfo> DwarfFrameInfos;
174   MCDwarfFrameInfo *getCurrentDwarfFrameInfo();
175
176   /// Similar to DwarfFrameInfos, but for SEH unwind info. Chained frames may
177   /// refer to each other, so use std::unique_ptr to provide pointer stability.
178   std::vector<std::unique_ptr<WinEH::FrameInfo>> WinFrameInfos;
179
180   WinEH::FrameInfo *CurrentWinFrameInfo;
181
182   /// Retreive the current frame info if one is available and it is not yet
183   /// closed. Otherwise, issue an error and return null.
184   WinEH::FrameInfo *EnsureValidWinFrameInfo(SMLoc Loc);
185
186   /// \brief Tracks an index to represent the order a symbol was emitted in.
187   /// Zero means we did not emit that symbol.
188   DenseMap<const MCSymbol *, unsigned> SymbolOrdering;
189
190   /// \brief This is stack of current and previous section values saved by
191   /// PushSection.
192   SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack;
193
194   /// The next unique ID to use when creating a WinCFI-related section (.pdata
195   /// or .xdata). This ID ensures that we have a one-to-one mapping from
196   /// code section to unwind info section, which MSVC's incremental linker
197   /// requires.
198   unsigned NextWinCFIID = 0;
199
200 protected:
201   MCStreamer(MCContext &Ctx);
202
203   virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
204   virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
205
206   /// When emitting an object file, create and emit a real label. When emitting
207   /// textual assembly, this should do nothing to avoid polluting our output.
208   virtual MCSymbol *EmitCFILabel();
209
210   WinEH::FrameInfo *getCurrentWinFrameInfo() {
211     return CurrentWinFrameInfo;
212   }
213
214   virtual void EmitWindowsUnwindTables();
215
216   virtual void EmitRawTextImpl(StringRef String);
217
218 public:
219   MCStreamer(const MCStreamer &) = delete;
220   MCStreamer &operator=(const MCStreamer &) = delete;
221   virtual ~MCStreamer();
222
223   void visitUsedExpr(const MCExpr &Expr);
224   virtual void visitUsedSymbol(const MCSymbol &Sym);
225
226   void setTargetStreamer(MCTargetStreamer *TS) {
227     TargetStreamer.reset(TS);
228   }
229
230   /// State management
231   ///
232   virtual void reset();
233
234   MCContext &getContext() const { return Context; }
235
236   MCTargetStreamer *getTargetStreamer() {
237     return TargetStreamer.get();
238   }
239
240   unsigned getNumFrameInfos() { return DwarfFrameInfos.size(); }
241   ArrayRef<MCDwarfFrameInfo> getDwarfFrameInfos() const {
242     return DwarfFrameInfos;
243   }
244
245   bool hasUnfinishedDwarfFrameInfo();
246
247   unsigned getNumWinFrameInfos() { return WinFrameInfos.size(); }
248   ArrayRef<std::unique_ptr<WinEH::FrameInfo>> getWinFrameInfos() const {
249     return WinFrameInfos;
250   }
251
252   void generateCompactUnwindEncodings(MCAsmBackend *MAB);
253
254   /// \name Assembly File Formatting.
255   /// @{
256
257   /// \brief Return true if this streamer supports verbose assembly and if it is
258   /// enabled.
259   virtual bool isVerboseAsm() const { return false; }
260
261   /// \brief Return true if this asm streamer supports emitting unformatted text
262   /// to the .s file with EmitRawText.
263   virtual bool hasRawTextSupport() const { return false; }
264
265   /// \brief Is the integrated assembler required for this streamer to function
266   /// correctly?
267   virtual bool isIntegratedAssemblerRequired() const { return false; }
268
269   /// \brief Add a textual comment.
270   ///
271   /// Typically for comments that can be emitted to the generated .s
272   /// file if applicable as a QoI issue to make the output of the compiler
273   /// more readable.  This only affects the MCAsmStreamer, and only when
274   /// verbose assembly output is enabled.
275   ///
276   /// If the comment includes embedded \n's, they will each get the comment
277   /// prefix as appropriate.  The added comment should not end with a \n.
278   /// By default, each comment is terminated with an end of line, i.e. the
279   /// EOL param is set to true by default. If one prefers not to end the 
280   /// comment with a new line then the EOL param should be passed 
281   /// with a false value.
282   virtual void AddComment(const Twine &T, bool EOL = true) {}
283
284   /// \brief Return a raw_ostream that comments can be written to. Unlike
285   /// AddComment, you are required to terminate comments with \n if you use this
286   /// method.
287   virtual raw_ostream &GetCommentOS();
288
289   /// \brief Print T and prefix it with the comment string (normally #) and
290   /// optionally a tab. This prints the comment immediately, not at the end of
291   /// the current line. It is basically a safe version of EmitRawText: since it
292   /// only prints comments, the object streamer ignores it instead of asserting.
293   virtual void emitRawComment(const Twine &T, bool TabPrefix = true);
294
295   /// \brief Add explicit comment T. T is required to be a valid
296   /// comment in the output and does not need to be escaped.
297   virtual void addExplicitComment(const Twine &T);
298
299   /// \brief Emit added explicit comments.
300   virtual void emitExplicitComments();
301
302   /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
303   virtual void AddBlankLine() {}
304
305   /// @}
306
307   /// \name Symbol & Section Management
308   /// @{
309
310   /// \brief Return the current section that the streamer is emitting code to.
311   MCSectionSubPair getCurrentSection() const {
312     if (!SectionStack.empty())
313       return SectionStack.back().first;
314     return MCSectionSubPair();
315   }
316   MCSection *getCurrentSectionOnly() const { return getCurrentSection().first; }
317
318   /// \brief Return the previous section that the streamer is emitting code to.
319   MCSectionSubPair getPreviousSection() const {
320     if (!SectionStack.empty())
321       return SectionStack.back().second;
322     return MCSectionSubPair();
323   }
324
325   /// \brief Returns an index to represent the order a symbol was emitted in.
326   /// (zero if we did not emit that symbol)
327   unsigned GetSymbolOrder(const MCSymbol *Sym) const {
328     return SymbolOrdering.lookup(Sym);
329   }
330
331   /// \brief Update streamer for a new active section.
332   ///
333   /// This is called by PopSection and SwitchSection, if the current
334   /// section changes.
335   virtual void ChangeSection(MCSection *, const MCExpr *);
336
337   /// \brief Save the current and previous section on the section stack.
338   void PushSection() {
339     SectionStack.push_back(
340         std::make_pair(getCurrentSection(), getPreviousSection()));
341   }
342
343   /// \brief Restore the current and previous section from the section stack.
344   /// Calls ChangeSection as needed.
345   ///
346   /// Returns false if the stack was empty.
347   bool PopSection() {
348     if (SectionStack.size() <= 1)
349       return false;
350     auto I = SectionStack.end();
351     --I;
352     MCSectionSubPair OldSection = I->first;
353     --I;
354     MCSectionSubPair NewSection = I->first;
355
356     if (OldSection != NewSection)
357       ChangeSection(NewSection.first, NewSection.second);
358     SectionStack.pop_back();
359     return true;
360   }
361
362   bool SubSection(const MCExpr *Subsection) {
363     if (SectionStack.empty())
364       return false;
365
366     SwitchSection(SectionStack.back().first.first, Subsection);
367     return true;
368   }
369
370   /// Set the current section where code is being emitted to \p Section.  This
371   /// is required to update CurSection.
372   ///
373   /// This corresponds to assembler directives like .section, .text, etc.
374   virtual void SwitchSection(MCSection *Section,
375                              const MCExpr *Subsection = nullptr);
376
377   /// \brief Set the current section where code is being emitted to \p Section.
378   /// This is required to update CurSection. This version does not call
379   /// ChangeSection.
380   void SwitchSectionNoChange(MCSection *Section,
381                              const MCExpr *Subsection = nullptr) {
382     assert(Section && "Cannot switch to a null section!");
383     MCSectionSubPair curSection = SectionStack.back().first;
384     SectionStack.back().second = curSection;
385     if (MCSectionSubPair(Section, Subsection) != curSection)
386       SectionStack.back().first = MCSectionSubPair(Section, Subsection);
387   }
388
389   /// \brief Create the default sections and set the initial one.
390   virtual void InitSections(bool NoExecStack);
391
392   MCSymbol *endSection(MCSection *Section);
393
394   /// \brief Sets the symbol's section.
395   ///
396   /// Each emitted symbol will be tracked in the ordering table,
397   /// so we can sort on them later.
398   void AssignFragment(MCSymbol *Symbol, MCFragment *Fragment);
399
400   /// \brief Emit a label for \p Symbol into the current section.
401   ///
402   /// This corresponds to an assembler statement such as:
403   ///   foo:
404   ///
405   /// \param Symbol - The symbol to emit. A given symbol should only be
406   /// emitted as a label once, and symbols emitted as a label should never be
407   /// used in an assignment.
408   // FIXME: These emission are non-const because we mutate the symbol to
409   // add the section we're emitting it to later.
410   virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc());
411
412   virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol);
413
414   /// \brief Note in the output the specified \p Flag.
415   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
416
417   /// \brief Emit the given list \p Options of strings as linker
418   /// options into the output.
419   virtual void EmitLinkerOptions(ArrayRef<std::string> Kind) {}
420
421   /// \brief Note in the output the specified region \p Kind.
422   virtual void EmitDataRegion(MCDataRegionType Kind) {}
423
424   /// \brief Specify the Mach-O minimum deployment target version.
425   virtual void EmitVersionMin(MCVersionMinType Type, unsigned Major,
426                               unsigned Minor, unsigned Update) {}
427
428   /// Emit/Specify Mach-O build version command.
429   /// \p Platform should be one of MachO::PlatformType.
430   virtual void EmitBuildVersion(unsigned Platform, unsigned Major,
431                                 unsigned Minor, unsigned Update) {}
432
433   void EmitVersionForTarget(const Triple &Target);
434
435   /// \brief Note in the output that the specified \p Func is a Thumb mode
436   /// function (ARM target only).
437   virtual void EmitThumbFunc(MCSymbol *Func);
438
439   /// \brief Emit an assignment of \p Value to \p Symbol.
440   ///
441   /// This corresponds to an assembler statement such as:
442   ///  symbol = value
443   ///
444   /// The assignment generates no code, but has the side effect of binding the
445   /// value in the current context. For the assembly streamer, this prints the
446   /// binding into the .s file.
447   ///
448   /// \param Symbol - The symbol being assigned to.
449   /// \param Value - The value for the symbol.
450   virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
451
452   /// \brief Emit an weak reference from \p Alias to \p Symbol.
453   ///
454   /// This corresponds to an assembler statement such as:
455   ///  .weakref alias, symbol
456   ///
457   /// \param Alias - The alias that is being created.
458   /// \param Symbol - The symbol being aliased.
459   virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
460
461   /// \brief Add the given \p Attribute to \p Symbol.
462   virtual bool EmitSymbolAttribute(MCSymbol *Symbol,
463                                    MCSymbolAttr Attribute) = 0;
464
465   /// \brief Set the \p DescValue for the \p Symbol.
466   ///
467   /// \param Symbol - The symbol to have its n_desc field set.
468   /// \param DescValue - The value to set into the n_desc field.
469   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
470
471   /// \brief Start emitting COFF symbol definition
472   ///
473   /// \param Symbol - The symbol to have its External & Type fields set.
474   virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
475
476   /// \brief Emit the storage class of the symbol.
477   ///
478   /// \param StorageClass - The storage class the symbol should have.
479   virtual void EmitCOFFSymbolStorageClass(int StorageClass);
480
481   /// \brief Emit the type of the symbol.
482   ///
483   /// \param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
484   virtual void EmitCOFFSymbolType(int Type);
485
486   /// \brief Marks the end of the symbol definition.
487   virtual void EndCOFFSymbolDef();
488
489   virtual void EmitCOFFSafeSEH(MCSymbol const *Symbol);
490
491   /// \brief Emits a COFF section index.
492   ///
493   /// \param Symbol - Symbol the section number relocation should point to.
494   virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
495
496   /// \brief Emits a COFF section relative relocation.
497   ///
498   /// \param Symbol - Symbol the section relative relocation should point to.
499   virtual void EmitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset);
500
501   /// \brief Emit an ELF .size directive.
502   ///
503   /// This corresponds to an assembler statement such as:
504   ///  .size symbol, expression
505   virtual void emitELFSize(MCSymbol *Symbol, const MCExpr *Value);
506
507   /// \brief Emit an ELF .symver directive.
508   ///
509   /// This corresponds to an assembler statement such as:
510   ///  .symver _start, foo@@SOME_VERSION
511   /// \param Alias - The versioned alias (i.e. "foo@@SOME_VERSION")
512   /// \param Aliasee - The aliased symbol (i.e. "_start")
513   virtual void emitELFSymverDirective(MCSymbol *Alias, const MCSymbol *Aliasee);
514
515   /// \brief Emit a Linker Optimization Hint (LOH) directive.
516   /// \param Args - Arguments of the LOH.
517   virtual void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {}
518
519   /// \brief Emit a common symbol.
520   ///
521   /// \param Symbol - The common symbol to emit.
522   /// \param Size - The size of the common symbol.
523   /// \param ByteAlignment - The alignment of the symbol if
524   /// non-zero. This must be a power of 2.
525   virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
526                                 unsigned ByteAlignment) = 0;
527
528   /// \brief Emit a local common (.lcomm) symbol.
529   ///
530   /// \param Symbol - The common symbol to emit.
531   /// \param Size - The size of the common symbol.
532   /// \param ByteAlignment - The alignment of the common symbol in bytes.
533   virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
534                                      unsigned ByteAlignment);
535
536   /// \brief Emit the zerofill section and an optional symbol.
537   ///
538   /// \param Section - The zerofill section to create and or to put the symbol
539   /// \param Symbol - The zerofill symbol to emit, if non-NULL.
540   /// \param Size - The size of the zerofill symbol.
541   /// \param ByteAlignment - The alignment of the zerofill symbol if
542   /// non-zero. This must be a power of 2 on some targets.
543   virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
544                             uint64_t Size = 0, unsigned ByteAlignment = 0) = 0;
545
546   /// \brief Emit a thread local bss (.tbss) symbol.
547   ///
548   /// \param Section - The thread local common section.
549   /// \param Symbol - The thread local common symbol to emit.
550   /// \param Size - The size of the symbol.
551   /// \param ByteAlignment - The alignment of the thread local common symbol
552   /// if non-zero.  This must be a power of 2 on some targets.
553   virtual void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
554                               uint64_t Size, unsigned ByteAlignment = 0);
555
556   /// @}
557   /// \name Generating Data
558   /// @{
559
560   /// \brief Emit the bytes in \p Data into the output.
561   ///
562   /// This is used to implement assembler directives such as .byte, .ascii,
563   /// etc.
564   virtual void EmitBytes(StringRef Data);
565
566   /// Functionally identical to EmitBytes. When emitting textual assembly, this
567   /// method uses .byte directives instead of .ascii or .asciz for readability.
568   virtual void EmitBinaryData(StringRef Data);
569
570   /// \brief Emit the expression \p Value into the output as a native
571   /// integer of the given \p Size bytes.
572   ///
573   /// This is used to implement assembler directives such as .word, .quad,
574   /// etc.
575   ///
576   /// \param Value - The value to emit.
577   /// \param Size - The size of the integer (in bytes) to emit. This must
578   /// match a native machine width.
579   /// \param Loc - The location of the expression for error reporting.
580   virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
581                              SMLoc Loc = SMLoc());
582
583   void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc = SMLoc());
584
585   /// \brief Special case of EmitValue that avoids the client having
586   /// to pass in a MCExpr for constant integers.
587   virtual void EmitIntValue(uint64_t Value, unsigned Size);
588
589   virtual void EmitULEB128Value(const MCExpr *Value);
590
591   virtual void EmitSLEB128Value(const MCExpr *Value);
592
593   /// \brief Special case of EmitULEB128Value that avoids the client having to
594   /// pass in a MCExpr for constant integers.
595   void EmitULEB128IntValue(uint64_t Value);
596
597   /// \brief Like EmitULEB128Value but pads the output to specific number of
598   /// bytes.
599   void EmitPaddedULEB128IntValue(uint64_t Value, unsigned PadTo);
600
601   /// \brief Special case of EmitSLEB128Value that avoids the client having to
602   /// pass in a MCExpr for constant integers.
603   void EmitSLEB128IntValue(int64_t Value);
604
605   /// \brief Special case of EmitValue that avoids the client having to pass in
606   /// a MCExpr for MCSymbols.
607   void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
608                        bool IsSectionRelative = false);
609
610   /// \brief Emit the expression \p Value into the output as a dtprel
611   /// (64-bit DTP relative) value.
612   ///
613   /// This is used to implement assembler directives such as .dtpreldword on
614   /// targets that support them.
615   virtual void EmitDTPRel64Value(const MCExpr *Value);
616
617   /// \brief Emit the expression \p Value into the output as a dtprel
618   /// (32-bit DTP relative) value.
619   ///
620   /// This is used to implement assembler directives such as .dtprelword on
621   /// targets that support them.
622   virtual void EmitDTPRel32Value(const MCExpr *Value);
623
624   /// \brief Emit the expression \p Value into the output as a tprel
625   /// (64-bit TP relative) value.
626   ///
627   /// This is used to implement assembler directives such as .tpreldword on
628   /// targets that support them.
629   virtual void EmitTPRel64Value(const MCExpr *Value);
630
631   /// \brief Emit the expression \p Value into the output as a tprel
632   /// (32-bit TP relative) value.
633   ///
634   /// This is used to implement assembler directives such as .tprelword on
635   /// targets that support them.
636   virtual void EmitTPRel32Value(const MCExpr *Value);
637
638   /// \brief Emit the expression \p Value into the output as a gprel64 (64-bit
639   /// GP relative) value.
640   ///
641   /// This is used to implement assembler directives such as .gpdword on
642   /// targets that support them.
643   virtual void EmitGPRel64Value(const MCExpr *Value);
644
645   /// \brief Emit the expression \p Value into the output as a gprel32 (32-bit
646   /// GP relative) value.
647   ///
648   /// This is used to implement assembler directives such as .gprel32 on
649   /// targets that support them.
650   virtual void EmitGPRel32Value(const MCExpr *Value);
651
652   /// \brief Emit NumBytes bytes worth of the value specified by FillValue.
653   /// This implements directives such as '.space'.
654   virtual void emitFill(uint64_t NumBytes, uint8_t FillValue);
655
656   /// \brief Emit \p Size bytes worth of the value specified by \p FillValue.
657   ///
658   /// This is used to implement assembler directives such as .space or .skip.
659   ///
660   /// \param NumBytes - The number of bytes to emit.
661   /// \param FillValue - The value to use when filling bytes.
662   /// \param Loc - The location of the expression for error reporting.
663   virtual void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
664                         SMLoc Loc = SMLoc());
665
666   /// \brief Emit \p NumValues copies of \p Size bytes. Each \p Size bytes is
667   /// taken from the lowest order 4 bytes of \p Expr expression.
668   ///
669   /// This is used to implement assembler directives such as .fill.
670   ///
671   /// \param NumValues - The number of copies of \p Size bytes to emit.
672   /// \param Size - The size (in bytes) of each repeated value.
673   /// \param Expr - The expression from which \p Size bytes are used.
674   virtual void emitFill(uint64_t NumValues, int64_t Size, int64_t Expr);
675   virtual void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
676                         SMLoc Loc = SMLoc());
677
678   /// \brief Emit NumBytes worth of zeros.
679   /// This function properly handles data in virtual sections.
680   void EmitZeros(uint64_t NumBytes);
681
682   /// \brief Emit some number of copies of \p Value until the byte alignment \p
683   /// ByteAlignment is reached.
684   ///
685   /// If the number of bytes need to emit for the alignment is not a multiple
686   /// of \p ValueSize, then the contents of the emitted fill bytes is
687   /// undefined.
688   ///
689   /// This used to implement the .align assembler directive.
690   ///
691   /// \param ByteAlignment - The alignment to reach. This must be a power of
692   /// two on some targets.
693   /// \param Value - The value to use when filling bytes.
694   /// \param ValueSize - The size of the integer (in bytes) to emit for
695   /// \p Value. This must match a native machine width.
696   /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
697   /// the alignment cannot be reached in this many bytes, no bytes are
698   /// emitted.
699   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
700                                     unsigned ValueSize = 1,
701                                     unsigned MaxBytesToEmit = 0);
702
703   /// \brief Emit nops until the byte alignment \p ByteAlignment is reached.
704   ///
705   /// This used to align code where the alignment bytes may be executed.  This
706   /// can emit different bytes for different sizes to optimize execution.
707   ///
708   /// \param ByteAlignment - The alignment to reach. This must be a power of
709   /// two on some targets.
710   /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
711   /// the alignment cannot be reached in this many bytes, no bytes are
712   /// emitted.
713   virtual void EmitCodeAlignment(unsigned ByteAlignment,
714                                  unsigned MaxBytesToEmit = 0);
715
716   /// \brief Emit some number of copies of \p Value until the byte offset \p
717   /// Offset is reached.
718   ///
719   /// This is used to implement assembler directives such as .org.
720   ///
721   /// \param Offset - The offset to reach. This may be an expression, but the
722   /// expression must be associated with the current section.
723   /// \param Value - The value to use when filling bytes.
724   virtual void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
725                                  SMLoc Loc);
726
727   virtual void
728   EmitCodePaddingBasicBlockStart(const MCCodePaddingContext &Context) {}
729
730   virtual void
731   EmitCodePaddingBasicBlockEnd(const MCCodePaddingContext &Context) {}
732
733   /// @}
734
735   /// \brief Switch to a new logical file.  This is used to implement the '.file
736   /// "foo.c"' assembler directive.
737   virtual void EmitFileDirective(StringRef Filename);
738
739   /// \brief Emit the "identifiers" directive.  This implements the
740   /// '.ident "version foo"' assembler directive.
741   virtual void EmitIdent(StringRef IdentString) {}
742
743   /// \brief Associate a filename with a specified logical file number.  This
744   /// implements the DWARF2 '.file 4 "foo.c"' assembler directive.
745   virtual unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
746                                           StringRef Filename,
747                                           unsigned CUID = 0);
748
749   /// \brief This implements the DWARF2 '.loc fileno lineno ...' assembler
750   /// directive.
751   virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
752                                      unsigned Column, unsigned Flags,
753                                      unsigned Isa, unsigned Discriminator,
754                                      StringRef FileName);
755
756   /// Associate a filename with a specified logical file number, and also
757   /// specify that file's checksum information.  This implements the '.cv_file 4
758   /// "foo.c"' assembler directive. Returns true on success.
759   virtual bool EmitCVFileDirective(unsigned FileNo, StringRef Filename,
760                                    ArrayRef<uint8_t> Checksum,
761                                    unsigned ChecksumKind);
762
763   /// \brief Introduces a function id for use with .cv_loc.
764   virtual bool EmitCVFuncIdDirective(unsigned FunctionId);
765
766   /// \brief Introduces an inline call site id for use with .cv_loc. Includes
767   /// extra information for inline line table generation.
768   virtual bool EmitCVInlineSiteIdDirective(unsigned FunctionId, unsigned IAFunc,
769                                            unsigned IAFile, unsigned IALine,
770                                            unsigned IACol, SMLoc Loc);
771
772   /// \brief This implements the CodeView '.cv_loc' assembler directive.
773   virtual void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo,
774                                   unsigned Line, unsigned Column,
775                                   bool PrologueEnd, bool IsStmt,
776                                   StringRef FileName, SMLoc Loc);
777
778   /// \brief This implements the CodeView '.cv_linetable' assembler directive.
779   virtual void EmitCVLinetableDirective(unsigned FunctionId,
780                                         const MCSymbol *FnStart,
781                                         const MCSymbol *FnEnd);
782
783   /// \brief This implements the CodeView '.cv_inline_linetable' assembler
784   /// directive.
785   virtual void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
786                                               unsigned SourceFileId,
787                                               unsigned SourceLineNum,
788                                               const MCSymbol *FnStartSym,
789                                               const MCSymbol *FnEndSym);
790
791   /// \brief This implements the CodeView '.cv_def_range' assembler
792   /// directive.
793   virtual void EmitCVDefRangeDirective(
794       ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
795       StringRef FixedSizePortion);
796
797   /// \brief This implements the CodeView '.cv_stringtable' assembler directive.
798   virtual void EmitCVStringTableDirective() {}
799
800   /// \brief This implements the CodeView '.cv_filechecksums' assembler directive.
801   virtual void EmitCVFileChecksumsDirective() {}
802
803   /// This implements the CodeView '.cv_filechecksumoffset' assembler
804   /// directive.
805   virtual void EmitCVFileChecksumOffsetDirective(unsigned FileNo) {}
806
807   /// This implements the CodeView '.cv_fpo_data' assembler directive.
808   virtual void EmitCVFPOData(const MCSymbol *ProcSym, SMLoc Loc = {}) {}
809
810   /// Emit the absolute difference between two symbols.
811   ///
812   /// \pre Offset of \c Hi is greater than the offset \c Lo.
813   virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
814                                       unsigned Size);
815
816   virtual MCSymbol *getDwarfLineTableSymbol(unsigned CUID);
817   virtual void EmitCFISections(bool EH, bool Debug);
818   void EmitCFIStartProc(bool IsSimple);
819   void EmitCFIEndProc();
820   virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
821   virtual void EmitCFIDefCfaOffset(int64_t Offset);
822   virtual void EmitCFIDefCfaRegister(int64_t Register);
823   virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
824   virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
825   virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
826   virtual void EmitCFIRememberState();
827   virtual void EmitCFIRestoreState();
828   virtual void EmitCFISameValue(int64_t Register);
829   virtual void EmitCFIRestore(int64_t Register);
830   virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
831   virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
832   virtual void EmitCFIEscape(StringRef Values);
833   virtual void EmitCFIReturnColumn(int64_t Register);
834   virtual void EmitCFIGnuArgsSize(int64_t Size);
835   virtual void EmitCFISignalFrame();
836   virtual void EmitCFIUndefined(int64_t Register);
837   virtual void EmitCFIRegister(int64_t Register1, int64_t Register2);
838   virtual void EmitCFIWindowSave();
839
840   virtual void EmitWinCFIStartProc(const MCSymbol *Symbol, SMLoc Loc = SMLoc());
841   virtual void EmitWinCFIEndProc(SMLoc Loc = SMLoc());
842   virtual void EmitWinCFIStartChained(SMLoc Loc = SMLoc());
843   virtual void EmitWinCFIEndChained(SMLoc Loc = SMLoc());
844   virtual void EmitWinCFIPushReg(unsigned Register, SMLoc Loc = SMLoc());
845   virtual void EmitWinCFISetFrame(unsigned Register, unsigned Offset,
846                                   SMLoc Loc = SMLoc());
847   virtual void EmitWinCFIAllocStack(unsigned Size, SMLoc Loc = SMLoc());
848   virtual void EmitWinCFISaveReg(unsigned Register, unsigned Offset,
849                                  SMLoc Loc = SMLoc());
850   virtual void EmitWinCFISaveXMM(unsigned Register, unsigned Offset,
851                                  SMLoc Loc = SMLoc());
852   virtual void EmitWinCFIPushFrame(bool Code, SMLoc Loc = SMLoc());
853   virtual void EmitWinCFIEndProlog(SMLoc Loc = SMLoc());
854   virtual void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except,
855                                 SMLoc Loc = SMLoc());
856   virtual void EmitWinEHHandlerData(SMLoc Loc = SMLoc());
857
858   /// Get the .pdata section used for the given section. Typically the given
859   /// section is either the main .text section or some other COMDAT .text
860   /// section, but it may be any section containing code.
861   MCSection *getAssociatedPDataSection(const MCSection *TextSec);
862
863   /// Get the .xdata section used for the given section.
864   MCSection *getAssociatedXDataSection(const MCSection *TextSec);
865
866   virtual void EmitSyntaxDirective();
867
868   /// \brief Emit a .reloc directive.
869   /// Returns true if the relocation could not be emitted because Name is not
870   /// known.
871   virtual bool EmitRelocDirective(const MCExpr &Offset, StringRef Name,
872                                   const MCExpr *Expr, SMLoc Loc) {
873     return true;
874   }
875
876   /// \brief Emit the given \p Instruction into the current section.
877   /// PrintSchedInfo == true then schedul comment should be added to output
878   virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
879                                bool PrintSchedInfo = false);
880
881   /// \brief Set the bundle alignment mode from now on in the section.
882   /// The argument is the power of 2 to which the alignment is set. The
883   /// value 0 means turn the bundle alignment off.
884   virtual void EmitBundleAlignMode(unsigned AlignPow2);
885
886   /// \brief The following instructions are a bundle-locked group.
887   ///
888   /// \param AlignToEnd - If true, the bundle-locked group will be aligned to
889   ///                     the end of a bundle.
890   virtual void EmitBundleLock(bool AlignToEnd);
891
892   /// \brief Ends a bundle-locked group.
893   virtual void EmitBundleUnlock();
894
895   /// \brief If this file is backed by a assembly streamer, this dumps the
896   /// specified string in the output .s file.  This capability is indicated by
897   /// the hasRawTextSupport() predicate.  By default this aborts.
898   void EmitRawText(const Twine &String);
899
900   /// \brief Streamer specific finalization.
901   virtual void FinishImpl();
902   /// \brief Finish emission of machine code.
903   void Finish();
904
905   virtual bool mayHaveInstructions(MCSection &Sec) const { return true; }
906 };
907
908 /// Create a dummy machine code streamer, which does nothing. This is useful for
909 /// timing the assembler front end.
910 MCStreamer *createNullStreamer(MCContext &Ctx);
911
912 /// Create a machine code streamer which will print out assembly for the native
913 /// target, suitable for compiling with a native assembler.
914 ///
915 /// \param InstPrint - If given, the instruction printer to use. If not given
916 /// the MCInst representation will be printed.  This method takes ownership of
917 /// InstPrint.
918 ///
919 /// \param CE - If given, a code emitter to use to show the instruction
920 /// encoding inline with the assembly. This method takes ownership of \p CE.
921 ///
922 /// \param TAB - If given, a target asm backend to use to show the fixup
923 /// information in conjunction with encoding information. This method takes
924 /// ownership of \p TAB.
925 ///
926 /// \param ShowInst - Whether to show the MCInst representation inline with
927 /// the assembly.
928 MCStreamer *createAsmStreamer(MCContext &Ctx,
929                               std::unique_ptr<formatted_raw_ostream> OS,
930                               bool isVerboseAsm, bool useDwarfDirectory,
931                               MCInstPrinter *InstPrint, MCCodeEmitter *CE,
932                               MCAsmBackend *TAB, bool ShowInst);
933
934 } // end namespace llvm
935
936 #endif // LLVM_MC_MCSTREAMER_H