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