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