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