4dd53df25ec27b87702dd24a1e56e3c7bd16167b
[oota-llvm.git] / 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/MCWinEH.h"
23 #include "llvm/Support/DataTypes.h"
24 #include "llvm/Support/SMLoc.h"
25 #include <string>
26
27 namespace llvm {
28 class MCAsmBackend;
29 class MCCodeEmitter;
30 class MCContext;
31 class MCExpr;
32 class MCInst;
33 class MCInstPrinter;
34 class MCSection;
35 class MCStreamer;
36 class MCSymbol;
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   const 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 finish();
89 };
90
91 // FIXME: declared here because it is used from
92 // lib/CodeGen/AsmPrinter/ARMException.cpp.
93 class ARMTargetStreamer : public MCTargetStreamer {
94 public:
95   ARMTargetStreamer(MCStreamer &S);
96   ~ARMTargetStreamer() override;
97
98   virtual void emitFnStart();
99   virtual void emitFnEnd();
100   virtual void emitCantUnwind();
101   virtual void emitPersonality(const MCSymbol *Personality);
102   virtual void emitPersonalityIndex(unsigned Index);
103   virtual void emitHandlerData();
104   virtual void emitSetFP(unsigned FpReg, unsigned SpReg,
105                          int64_t Offset = 0);
106   virtual void emitMovSP(unsigned Reg, int64_t Offset = 0);
107   virtual void emitPad(int64_t Offset);
108   virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
109                            bool isVector);
110   virtual void emitUnwindRaw(int64_t StackOffset,
111                              const SmallVectorImpl<uint8_t> &Opcodes);
112
113   virtual void switchVendor(StringRef Vendor);
114   virtual void emitAttribute(unsigned Attribute, unsigned Value);
115   virtual void emitTextAttribute(unsigned Attribute, StringRef String);
116   virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
117                                     StringRef StringValue = "");
118   virtual void emitFPU(unsigned FPU);
119   virtual void emitArch(unsigned Arch);
120   virtual void emitArchExtension(unsigned ArchExt);
121   virtual void emitObjectArch(unsigned Arch);
122   virtual void finishAttributeSection();
123   virtual void emitInst(uint32_t Inst, char Suffix = '\0');
124
125   virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
126
127   virtual void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value);
128
129   void finish() override;
130
131   /// Callback used to implement the ldr= pseudo.
132   /// Add a new entry to the constant pool for the current section and return an
133   /// MCExpr that can be used to refer to the constant pool location.
134   const MCExpr *addConstantPoolEntry(const MCExpr *);
135
136   /// Callback used to implemnt the .ltorg directive.
137   /// Emit contents of constant pool for the current section.
138   void emitCurrentConstantPool();
139
140 private:
141   std::unique_ptr<AssemblerConstantPools> ConstantPools;
142 };
143
144 /// \brief Streaming machine code generation interface.
145 ///
146 /// This interface is intended to provide a programatic interface that is very
147 /// similar to the level that an assembler .s file provides.  It has callbacks
148 /// to emit bytes, handle directives, etc.  The implementation of this interface
149 /// retains state to know what the current section is etc.
150 ///
151 /// There are multiple implementations of this interface: one for writing out
152 /// a .s file, and implementations that write out .o files of various formats.
153 ///
154 class MCStreamer {
155   MCContext &Context;
156   std::unique_ptr<MCTargetStreamer> TargetStreamer;
157
158   MCStreamer(const MCStreamer &) = delete;
159   MCStreamer &operator=(const MCStreamer &) = delete;
160
161   std::vector<MCDwarfFrameInfo> DwarfFrameInfos;
162   MCDwarfFrameInfo *getCurrentDwarfFrameInfo();
163   void EnsureValidDwarfFrame();
164
165   MCSymbol *EmitCFICommon();
166
167   std::vector<WinEH::FrameInfo *> WinFrameInfos;
168   WinEH::FrameInfo *CurrentWinFrameInfo;
169   void EnsureValidWinFrameInfo();
170
171   /// \brief Tracks an index to represent the order a symbol was emitted in.
172   /// Zero means we did not emit that symbol.
173   DenseMap<const MCSymbol *, unsigned> SymbolOrdering;
174
175   /// \brief This is stack of current and previous section values saved by
176   /// PushSection.
177   SmallVector<std::pair<MCSectionSubPair, MCSectionSubPair>, 4> SectionStack;
178
179 protected:
180   MCStreamer(MCContext &Ctx);
181
182   virtual void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame);
183   virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame);
184
185   WinEH::FrameInfo *getCurrentWinFrameInfo() {
186     return CurrentWinFrameInfo;
187   }
188
189   virtual void EmitWindowsUnwindTables();
190
191   virtual void EmitRawTextImpl(StringRef String);
192
193 public:
194   virtual ~MCStreamer();
195
196   void visitUsedExpr(const MCExpr &Expr);
197   virtual void visitUsedSymbol(const MCSymbol &Sym);
198
199   void setTargetStreamer(MCTargetStreamer *TS) {
200     TargetStreamer.reset(TS);
201   }
202
203   /// State management
204   ///
205   virtual void reset();
206
207   MCContext &getContext() const { return Context; }
208
209   MCTargetStreamer *getTargetStreamer() {
210     return TargetStreamer.get();
211   }
212
213   unsigned getNumFrameInfos() { return DwarfFrameInfos.size(); }
214   ArrayRef<MCDwarfFrameInfo> getDwarfFrameInfos() const {
215     return DwarfFrameInfos;
216   }
217
218   unsigned getNumWinFrameInfos() { return WinFrameInfos.size(); }
219   ArrayRef<WinEH::FrameInfo *> getWinFrameInfos() const {
220     return WinFrameInfos;
221   }
222
223   void generateCompactUnwindEncodings(MCAsmBackend *MAB);
224
225   /// \name Assembly File Formatting.
226   /// @{
227
228   /// \brief Return true if this streamer supports verbose assembly and if it is
229   /// enabled.
230   virtual bool isVerboseAsm() const { return false; }
231
232   /// \brief Return true if this asm streamer supports emitting unformatted text
233   /// to the .s file with EmitRawText.
234   virtual bool hasRawTextSupport() const { return false; }
235
236   /// \brief Is the integrated assembler required for this streamer to function
237   /// correctly?
238   virtual bool isIntegratedAssemblerRequired() const { return false; }
239
240   /// \brief Add a textual command.
241   ///
242   /// Typically for comments that can be emitted to the generated .s
243   /// file if applicable as a QoI issue to make the output of the compiler
244   /// more readable.  This only affects the MCAsmStreamer, and only when
245   /// verbose assembly output is enabled.
246   ///
247   /// If the comment includes embedded \n's, they will each get the comment
248   /// prefix as appropriate.  The added comment should not end with a \n.
249   virtual void AddComment(const Twine &T) {}
250
251   /// \brief Return a raw_ostream that comments can be written to. Unlike
252   /// AddComment, you are required to terminate comments with \n if you use this
253   /// method.
254   virtual raw_ostream &GetCommentOS();
255
256   /// \brief Print T and prefix it with the comment string (normally #) and
257   /// optionally a tab. This prints the comment immediately, not at the end of
258   /// the current line. It is basically a safe version of EmitRawText: since it
259   /// only prints comments, the object streamer ignores it instead of asserting.
260   virtual void emitRawComment(const Twine &T, bool TabPrefix = true);
261
262   /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
263   virtual void AddBlankLine() {}
264
265   /// @}
266
267   /// \name Symbol & Section Management
268   /// @{
269
270   /// \brief Return the current section that the streamer is emitting code to.
271   MCSectionSubPair getCurrentSection() const {
272     if (!SectionStack.empty())
273       return SectionStack.back().first;
274     return MCSectionSubPair();
275   }
276   MCSection *getCurrentSectionOnly() const { return getCurrentSection().first; }
277
278   /// \brief Return the previous section that the streamer is emitting code to.
279   MCSectionSubPair getPreviousSection() const {
280     if (!SectionStack.empty())
281       return SectionStack.back().second;
282     return MCSectionSubPair();
283   }
284
285   /// \brief Returns an index to represent the order a symbol was emitted in.
286   /// (zero if we did not emit that symbol)
287   unsigned GetSymbolOrder(const MCSymbol *Sym) const {
288     return SymbolOrdering.lookup(Sym);
289   }
290
291   /// \brief Update streamer for a new active section.
292   ///
293   /// This is called by PopSection and SwitchSection, if the current
294   /// section changes.
295   virtual void ChangeSection(MCSection *, const MCExpr *);
296
297   /// \brief Save the current and previous section on the section stack.
298   void PushSection() {
299     SectionStack.push_back(
300         std::make_pair(getCurrentSection(), getPreviousSection()));
301   }
302
303   /// \brief Restore the current and previous section from the section stack.
304   /// Calls ChangeSection as needed.
305   ///
306   /// Returns false if the stack was empty.
307   bool PopSection() {
308     if (SectionStack.size() <= 1)
309       return false;
310     auto I = SectionStack.end();
311     --I;
312     MCSectionSubPair OldSection = I->first;
313     --I;
314     MCSectionSubPair NewSection = I->first;
315
316     if (OldSection != NewSection)
317       ChangeSection(NewSection.first, NewSection.second);
318     SectionStack.pop_back();
319     return true;
320   }
321
322   bool SubSection(const MCExpr *Subsection) {
323     if (SectionStack.empty())
324       return false;
325
326     SwitchSection(SectionStack.back().first.first, Subsection);
327     return true;
328   }
329
330   /// Set the current section where code is being emitted to \p Section.  This
331   /// is required to update CurSection.
332   ///
333   /// This corresponds to assembler directives like .section, .text, etc.
334   virtual void SwitchSection(MCSection *Section,
335                              const MCExpr *Subsection = nullptr);
336
337   /// \brief Set the current section where code is being emitted to \p Section.
338   /// This is required to update CurSection. This version does not call
339   /// ChangeSection.
340   void SwitchSectionNoChange(MCSection *Section,
341                              const MCExpr *Subsection = nullptr) {
342     assert(Section && "Cannot switch to a null section!");
343     MCSectionSubPair curSection = SectionStack.back().first;
344     SectionStack.back().second = curSection;
345     if (MCSectionSubPair(Section, Subsection) != curSection)
346       SectionStack.back().first = MCSectionSubPair(Section, Subsection);
347   }
348
349   /// \brief Create the default sections and set the initial one.
350   virtual void InitSections(bool NoExecStack);
351
352   MCSymbol *endSection(MCSection *Section);
353
354   /// \brief Sets the symbol's section.
355   ///
356   /// Each emitted symbol will be tracked in the ordering table,
357   /// so we can sort on them later.
358   void AssignSection(MCSymbol *Symbol, MCSection *Section);
359
360   /// \brief Emit a label for \p Symbol into the current section.
361   ///
362   /// This corresponds to an assembler statement such as:
363   ///   foo:
364   ///
365   /// \param Symbol - The symbol to emit. A given symbol should only be
366   /// emitted as a label once, and symbols emitted as a label should never be
367   /// used in an assignment.
368   // FIXME: These emission are non-const because we mutate the symbol to
369   // add the section we're emitting it to later.
370   virtual void EmitLabel(MCSymbol *Symbol);
371
372   virtual void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol);
373
374   /// \brief Note in the output the specified \p Flag.
375   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
376
377   /// \brief Emit the given list \p Options of strings as linker
378   /// options into the output.
379   virtual void EmitLinkerOptions(ArrayRef<std::string> Kind) {}
380
381   /// \brief Note in the output the specified region \p Kind.
382   virtual void EmitDataRegion(MCDataRegionType Kind) {}
383
384   /// \brief Specify the MachO minimum deployment target version.
385   virtual void EmitVersionMin(MCVersionMinType, unsigned Major, unsigned Minor,
386                               unsigned Update) {}
387
388   /// \brief Note in the output that the specified \p Func is a Thumb mode
389   /// function (ARM target only).
390   virtual void EmitThumbFunc(MCSymbol *Func);
391
392   /// \brief Emit an assignment of \p Value to \p Symbol.
393   ///
394   /// This corresponds to an assembler statement such as:
395   ///  symbol = value
396   ///
397   /// The assignment generates no code, but has the side effect of binding the
398   /// value in the current context. For the assembly streamer, this prints the
399   /// binding into the .s file.
400   ///
401   /// \param Symbol - The symbol being assigned to.
402   /// \param Value - The value for the symbol.
403   virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
404
405   /// \brief Emit an weak reference from \p Alias to \p Symbol.
406   ///
407   /// This corresponds to an assembler statement such as:
408   ///  .weakref alias, symbol
409   ///
410   /// \param Alias - The alias that is being created.
411   /// \param Symbol - The symbol being aliased.
412   virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
413
414   /// \brief Add the given \p Attribute to \p Symbol.
415   virtual bool EmitSymbolAttribute(MCSymbol *Symbol,
416                                    MCSymbolAttr Attribute) = 0;
417
418   /// \brief Set the \p DescValue for the \p Symbol.
419   ///
420   /// \param Symbol - The symbol to have its n_desc field set.
421   /// \param DescValue - The value to set into the n_desc field.
422   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
423
424   /// \brief Start emitting COFF symbol definition
425   ///
426   /// \param Symbol - The symbol to have its External & Type fields set.
427   virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
428
429   /// \brief Emit the storage class of the symbol.
430   ///
431   /// \param StorageClass - The storage class the symbol should have.
432   virtual void EmitCOFFSymbolStorageClass(int StorageClass);
433
434   /// \brief Emit the type of the symbol.
435   ///
436   /// \param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
437   virtual void EmitCOFFSymbolType(int Type);
438
439   /// \brief Marks the end of the symbol definition.
440   virtual void EndCOFFSymbolDef();
441
442   virtual void EmitCOFFSafeSEH(MCSymbol const *Symbol);
443
444   /// \brief Emits a COFF section index.
445   ///
446   /// \param Symbol - Symbol the section number relocation should point to.
447   virtual void EmitCOFFSectionIndex(MCSymbol const *Symbol);
448
449   /// \brief Emits a COFF section relative relocation.
450   ///
451   /// \param Symbol - Symbol the section relative relocation should point to.
452   virtual void EmitCOFFSecRel32(MCSymbol const *Symbol);
453
454   /// \brief Emit an ELF .size directive.
455   ///
456   /// This corresponds to an assembler statement such as:
457   ///  .size symbol, expression
458   virtual void emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value);
459
460   /// \brief Emit a Linker Optimization Hint (LOH) directive.
461   /// \param Args - Arguments of the LOH.
462   virtual void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) {}
463
464   /// \brief Emit a common symbol.
465   ///
466   /// \param Symbol - The common symbol to emit.
467   /// \param Size - The size of the common symbol.
468   /// \param ByteAlignment - The alignment of the symbol if
469   /// non-zero. This must be a power of 2.
470   virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
471                                 unsigned ByteAlignment) = 0;
472
473   /// \brief Emit a local common (.lcomm) symbol.
474   ///
475   /// \param Symbol - The common symbol to emit.
476   /// \param Size - The size of the common symbol.
477   /// \param ByteAlignment - The alignment of the common symbol in bytes.
478   virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
479                                      unsigned ByteAlignment);
480
481   /// \brief Emit the zerofill section and an optional symbol.
482   ///
483   /// \param Section - The zerofill section to create and or to put the symbol
484   /// \param Symbol - The zerofill symbol to emit, if non-NULL.
485   /// \param Size - The size of the zerofill symbol.
486   /// \param ByteAlignment - The alignment of the zerofill symbol if
487   /// non-zero. This must be a power of 2 on some targets.
488   virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
489                             uint64_t Size = 0, unsigned ByteAlignment = 0) = 0;
490
491   /// \brief Emit a thread local bss (.tbss) symbol.
492   ///
493   /// \param Section - The thread local common section.
494   /// \param Symbol - The thread local common symbol to emit.
495   /// \param Size - The size of the symbol.
496   /// \param ByteAlignment - The alignment of the thread local common symbol
497   /// if non-zero.  This must be a power of 2 on some targets.
498   virtual void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
499                               uint64_t Size, unsigned ByteAlignment = 0);
500
501   /// @}
502   /// \name Generating Data
503   /// @{
504
505   /// \brief Emit the bytes in \p Data into the output.
506   ///
507   /// This is used to implement assembler directives such as .byte, .ascii,
508   /// etc.
509   virtual void EmitBytes(StringRef Data);
510
511   /// \brief Emit the expression \p Value into the output as a native
512   /// integer of the given \p Size bytes.
513   ///
514   /// This is used to implement assembler directives such as .word, .quad,
515   /// etc.
516   ///
517   /// \param Value - The value to emit.
518   /// \param Size - The size of the integer (in bytes) to emit. This must
519   /// match a native machine width.
520   /// \param Loc - The location of the expression for error reporting.
521   virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
522                              const SMLoc &Loc = SMLoc());
523
524   void EmitValue(const MCExpr *Value, unsigned Size,
525                  const SMLoc &Loc = SMLoc());
526
527   /// \brief Special case of EmitValue that avoids the client having
528   /// to pass in a MCExpr for constant integers.
529   virtual void EmitIntValue(uint64_t Value, unsigned Size);
530
531   virtual void EmitULEB128Value(const MCExpr *Value);
532
533   virtual void EmitSLEB128Value(const MCExpr *Value);
534
535   /// \brief Special case of EmitULEB128Value that avoids the client having to
536   /// pass in a MCExpr for constant integers.
537   void EmitULEB128IntValue(uint64_t Value, unsigned Padding = 0);
538
539   /// \brief Special case of EmitSLEB128Value that avoids the client having to
540   /// pass in a MCExpr for constant integers.
541   void EmitSLEB128IntValue(int64_t Value);
542
543   /// \brief Special case of EmitValue that avoids the client having to pass in
544   /// a MCExpr for MCSymbols.
545   void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
546                        bool IsSectionRelative = false);
547
548   /// \brief Emit the expression \p Value into the output as a gprel64 (64-bit
549   /// GP relative) value.
550   ///
551   /// This is used to implement assembler directives such as .gpdword on
552   /// targets that support them.
553   virtual void EmitGPRel64Value(const MCExpr *Value);
554
555   /// \brief Emit the expression \p Value into the output as a gprel32 (32-bit
556   /// GP relative) value.
557   ///
558   /// This is used to implement assembler directives such as .gprel32 on
559   /// targets that support them.
560   virtual void EmitGPRel32Value(const MCExpr *Value);
561
562   /// \brief Emit NumBytes bytes worth of the value specified by FillValue.
563   /// This implements directives such as '.space'.
564   virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue);
565
566   /// \brief Emit NumBytes worth of zeros.
567   /// This function properly handles data in virtual sections.
568   virtual void EmitZeros(uint64_t NumBytes);
569
570   /// \brief Emit some number of copies of \p Value until the byte alignment \p
571   /// ByteAlignment is reached.
572   ///
573   /// If the number of bytes need to emit for the alignment is not a multiple
574   /// of \p ValueSize, then the contents of the emitted fill bytes is
575   /// undefined.
576   ///
577   /// This used to implement the .align assembler directive.
578   ///
579   /// \param ByteAlignment - The alignment to reach. This must be a power of
580   /// two on some targets.
581   /// \param Value - The value to use when filling bytes.
582   /// \param ValueSize - The size of the integer (in bytes) to emit for
583   /// \p Value. This must match a native machine width.
584   /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
585   /// the alignment cannot be reached in this many bytes, no bytes are
586   /// emitted.
587   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
588                                     unsigned ValueSize = 1,
589                                     unsigned MaxBytesToEmit = 0);
590
591   /// \brief Emit nops until the byte alignment \p ByteAlignment is reached.
592   ///
593   /// This used to align code where the alignment bytes may be executed.  This
594   /// can emit different bytes for different sizes to optimize execution.
595   ///
596   /// \param ByteAlignment - The alignment to reach. This must be a power of
597   /// two on some targets.
598   /// \param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
599   /// the alignment cannot be reached in this many bytes, no bytes are
600   /// emitted.
601   virtual void EmitCodeAlignment(unsigned ByteAlignment,
602                                  unsigned MaxBytesToEmit = 0);
603
604   /// \brief Emit some number of copies of \p Value until the byte offset \p
605   /// Offset is reached.
606   ///
607   /// This is used to implement assembler directives such as .org.
608   ///
609   /// \param Offset - The offset to reach. This may be an expression, but the
610   /// expression must be associated with the current section.
611   /// \param Value - The value to use when filling bytes.
612   /// \return false on success, true if the offset was invalid.
613   virtual bool EmitValueToOffset(const MCExpr *Offset,
614                                  unsigned char Value = 0);
615
616   /// @}
617
618   /// \brief Switch to a new logical file.  This is used to implement the '.file
619   /// "foo.c"' assembler directive.
620   virtual void EmitFileDirective(StringRef Filename);
621
622   /// \brief Emit the "identifiers" directive.  This implements the
623   /// '.ident "version foo"' assembler directive.
624   virtual void EmitIdent(StringRef IdentString) {}
625
626   /// \brief Associate a filename with a specified logical file number.  This
627   /// implements the DWARF2 '.file 4 "foo.c"' assembler directive.
628   virtual unsigned EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
629                                           StringRef Filename,
630                                           unsigned CUID = 0);
631
632   /// \brief This implements the DWARF2 '.loc fileno lineno ...' assembler
633   /// directive.
634   virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
635                                      unsigned Column, unsigned Flags,
636                                      unsigned Isa, unsigned Discriminator,
637                                      StringRef FileName);
638
639   /// Emit the absolute difference between two symbols.
640   ///
641   /// \pre Offset of \c Hi is greater than the offset \c Lo.
642   /// \return true on success.
643   virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
644                                       unsigned Size);
645
646   virtual MCSymbol *getDwarfLineTableSymbol(unsigned CUID);
647   virtual void EmitCFISections(bool EH, bool Debug);
648   void EmitCFIStartProc(bool IsSimple);
649   void EmitCFIEndProc();
650   virtual void EmitCFIDefCfa(int64_t Register, int64_t Offset);
651   virtual void EmitCFIDefCfaOffset(int64_t Offset);
652   virtual void EmitCFIDefCfaRegister(int64_t Register);
653   virtual void EmitCFIOffset(int64_t Register, int64_t Offset);
654   virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding);
655   virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
656   virtual void EmitCFIRememberState();
657   virtual void EmitCFIRestoreState();
658   virtual void EmitCFISameValue(int64_t Register);
659   virtual void EmitCFIRestore(int64_t Register);
660   virtual void EmitCFIRelOffset(int64_t Register, int64_t Offset);
661   virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment);
662   virtual void EmitCFIEscape(StringRef Values);
663   virtual void EmitCFISignalFrame();
664   virtual void EmitCFIUndefined(int64_t Register);
665   virtual void EmitCFIRegister(int64_t Register1, int64_t Register2);
666   virtual void EmitCFIWindowSave();
667
668   virtual void EmitWinCFIStartProc(const MCSymbol *Symbol);
669   virtual void EmitWinCFIEndProc();
670   virtual void EmitWinCFIStartChained();
671   virtual void EmitWinCFIEndChained();
672   virtual void EmitWinCFIPushReg(unsigned Register);
673   virtual void EmitWinCFISetFrame(unsigned Register, unsigned Offset);
674   virtual void EmitWinCFIAllocStack(unsigned Size);
675   virtual void EmitWinCFISaveReg(unsigned Register, unsigned Offset);
676   virtual void EmitWinCFISaveXMM(unsigned Register, unsigned Offset);
677   virtual void EmitWinCFIPushFrame(bool Code);
678   virtual void EmitWinCFIEndProlog();
679
680   virtual void EmitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except);
681   virtual void EmitWinEHHandlerData();
682
683   /// \brief Emit the given \p Instruction into the current section.
684   virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI);
685
686   /// \brief Set the bundle alignment mode from now on in the section.
687   /// The argument is the power of 2 to which the alignment is set. The
688   /// value 0 means turn the bundle alignment off.
689   virtual void EmitBundleAlignMode(unsigned AlignPow2);
690
691   /// \brief The following instructions are a bundle-locked group.
692   ///
693   /// \param AlignToEnd - If true, the bundle-locked group will be aligned to
694   ///                     the end of a bundle.
695   virtual void EmitBundleLock(bool AlignToEnd);
696
697   /// \brief Ends a bundle-locked group.
698   virtual void EmitBundleUnlock();
699
700   /// \brief If this file is backed by a assembly streamer, this dumps the
701   /// specified string in the output .s file.  This capability is indicated by
702   /// the hasRawTextSupport() predicate.  By default this aborts.
703   void EmitRawText(const Twine &String);
704
705   /// \brief Causes any cached state to be written out.
706   virtual void Flush() {}
707
708   /// \brief Streamer specific finalization.
709   virtual void FinishImpl();
710   /// \brief Finish emission of machine code.
711   void Finish();
712
713   virtual bool mayHaveInstructions(MCSection &Sec) const { return true; }
714 };
715
716 /// Create a dummy machine code streamer, which does nothing. This is useful for
717 /// timing the assembler front end.
718 MCStreamer *createNullStreamer(MCContext &Ctx);
719
720 /// Create a machine code streamer which will print out assembly for the native
721 /// target, suitable for compiling with a native assembler.
722 ///
723 /// \param InstPrint - If given, the instruction printer to use. If not given
724 /// the MCInst representation will be printed.  This method takes ownership of
725 /// InstPrint.
726 ///
727 /// \param CE - If given, a code emitter to use to show the instruction
728 /// encoding inline with the assembly. This method takes ownership of \p CE.
729 ///
730 /// \param TAB - If given, a target asm backend to use to show the fixup
731 /// information in conjunction with encoding information. This method takes
732 /// ownership of \p TAB.
733 ///
734 /// \param ShowInst - Whether to show the MCInst representation inline with
735 /// the assembly.
736 MCStreamer *createAsmStreamer(MCContext &Ctx,
737                               std::unique_ptr<formatted_raw_ostream> OS,
738                               bool isVerboseAsm, bool useDwarfDirectory,
739                               MCInstPrinter *InstPrint, MCCodeEmitter *CE,
740                               MCAsmBackend *TAB, bool ShowInst);
741 } // end namespace llvm
742
743 #endif