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