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