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