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