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