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