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