248e6b0a4481b0d52bed5232971efb6b9c660d5c
[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/Support/DataTypes.h"
18
19 namespace llvm {
20   class MCAsmInfo;
21   class MCCodeEmitter;
22   class MCContext;
23   class MCExpr;
24   class MCInst;
25   class MCInstPrinter;
26   class MCSection;
27   class MCSymbol;
28   class StringRef;
29   class raw_ostream;
30
31   /// MCStreamer - Streaming machine code generation interface.  This interface
32   /// is intended to provide a programatic interface that is very similar to the
33   /// level that an assembler .s file provides.  It has callbacks to emit bytes,
34   /// handle directives, etc.  The implementation of this interface retains
35   /// state to know what the current section is etc.
36   ///
37   /// There are multiple implementations of this interface: one for writing out
38   /// a .s file, and implementations that write out .o files of various formats.
39   ///
40   class MCStreamer {
41   public:
42     enum SymbolAttr {
43       Global,         /// .globl
44       Hidden,         /// .hidden (ELF)
45       IndirectSymbol, /// .indirect_symbol (Apple)
46       Internal,       /// .internal (ELF)
47       LazyReference,  /// .lazy_reference (Apple)
48       NoDeadStrip,    /// .no_dead_strip (Apple)
49       PrivateExtern,  /// .private_extern (Apple)
50       Protected,      /// .protected (ELF)
51       Reference,      /// .reference (Apple)
52       Weak,           /// .weak
53       WeakDefinition, /// .weak_definition (Apple)
54       WeakReference,  /// .weak_reference (Apple)
55
56       SymbolAttrFirst = Global,
57       SymbolAttrLast = WeakReference
58     };
59
60     enum AssemblerFlag {
61       SubsectionsViaSymbols  /// .subsections_via_symbols (Apple)
62     };
63
64   private:
65     MCContext &Context;
66
67     MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT
68     MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT
69
70   protected:
71     MCStreamer(MCContext &Ctx);
72
73     /// CurSection - This is the current section code is being emitted to, it is
74     /// kept up to date by SwitchSection.
75     const MCSection *CurSection;
76
77   public:
78     virtual ~MCStreamer();
79
80     MCContext &getContext() const { return Context; }
81
82     /// @name Symbol & Section Management
83     /// @{
84     
85     /// getCurrentSection - Return the current seciton that the streamer is
86     /// emitting code to.
87     const MCSection *getCurrentSection() const { return CurSection; }
88
89     /// SwitchSection - Set the current section where code is being emitted to
90     /// @param Section.  This is required to update CurSection.
91     ///
92     /// This corresponds to assembler directives like .section, .text, etc.
93     virtual void SwitchSection(const MCSection *Section) = 0;
94     
95     /// EmitLabel - Emit a label for @param Symbol into the current section.
96     ///
97     /// This corresponds to an assembler statement such as:
98     ///   foo:
99     ///
100     /// @param Symbol - The symbol to emit. A given symbol should only be
101     /// emitted as a label once, and symbols emitted as a label should never be
102     /// used in an assignment.
103     virtual void EmitLabel(MCSymbol *Symbol) = 0;
104
105     /// EmitAssemblerFlag - Note in the output the specified @param Flag
106     virtual void EmitAssemblerFlag(AssemblerFlag Flag) = 0;
107
108     /// EmitAssignment - Emit an assignment of @param Value to @param Symbol.
109     ///
110     /// This corresponds to an assembler statement such as:
111     ///  symbol = value
112     ///
113     /// The assignment generates no code, but has the side effect of binding the
114     /// value in the current context. For the assembly streamer, this prints the
115     /// binding into the .s file.
116     ///
117     /// @param Symbol - The symbol being assigned to.
118     /// @param Value - The value for the symbol.
119     virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
120
121     /// EmitSymbolAttribute - Add the given @param Attribute to @param Symbol.
122     virtual void EmitSymbolAttribute(MCSymbol *Symbol,
123                                      SymbolAttr Attribute) = 0;
124
125     /// EmitSymbolDesc - Set the @param DescValue for the @param Symbol.
126     ///
127     /// @param Symbol - The symbol to have its n_desc field set.
128     /// @param DescValue - The value to set into the n_desc field.
129     virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
130
131     /// EmitCommonSymbol - Emit a common or local common symbol.
132     ///
133     /// @param Symbol - The common symbol to emit.
134     /// @param Size - The size of the common symbol.
135     /// @param ByteAlignment - The alignment of the symbol if
136     /// non-zero. This must be a power of 2 on some targets.
137     virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
138                                   unsigned ByteAlignment) = 0;
139
140     /// EmitZerofill - Emit a the zerofill section and an option symbol.
141     ///
142     /// @param Section - The zerofill section to create and or to put the symbol
143     /// @param Symbol - The zerofill symbol to emit, if non-NULL.
144     /// @param Size - The size of the zerofill symbol.
145     /// @param ByteAlignment - The alignment of the zerofill symbol if
146     /// non-zero. This must be a power of 2 on some targets.
147     virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
148                               unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
149
150     /// @}
151     /// @name Generating Data
152     /// @{
153
154     /// EmitBytes - Emit the bytes in \arg Data into the output.
155     ///
156     /// This is used to implement assembler directives such as .byte, .ascii,
157     /// etc.
158     virtual void EmitBytes(const StringRef &Data) = 0;
159
160     /// EmitValue - Emit the expression @param Value into the output as a native
161     /// integer of the given @param Size bytes.
162     ///
163     /// This is used to implement assembler directives such as .word, .quad,
164     /// etc.
165     ///
166     /// @param Value - The value to emit.
167     /// @param Size - The size of the integer (in bytes) to emit. This must
168     /// match a native machine width.
169     virtual void EmitValue(const MCExpr *Value, unsigned Size) = 0;
170
171     /// EmitValueToAlignment - Emit some number of copies of @param Value until
172     /// the byte alignment @param ByteAlignment is reached.
173     ///
174     /// If the number of bytes need to emit for the alignment is not a multiple
175     /// of @param ValueSize, then the contents of the emitted fill bytes is
176     /// undefined.
177     ///
178     /// This used to implement the .align assembler directive.
179     ///
180     /// @param ByteAlignment - The alignment to reach. This must be a power of
181     /// two on some targets.
182     /// @param Value - The value to use when filling bytes.
183     /// @param Size - The size of the integer (in bytes) to emit for @param
184     /// Value. This must match a native machine width.
185     /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
186     /// the alignment cannot be reached in this many bytes, no bytes are
187     /// emitted.
188     virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
189                                       unsigned ValueSize = 1,
190                                       unsigned MaxBytesToEmit = 0) = 0;
191
192     /// EmitValueToOffset - Emit some number of copies of @param Value until the
193     /// byte offset @param Offset is reached.
194     ///
195     /// This is used to implement assembler directives such as .org.
196     ///
197     /// @param Offset - The offset to reach. This may be an expression, but the
198     /// expression must be associated with the current section.
199     /// @param Value - The value to use when filling bytes.
200     virtual void EmitValueToOffset(const MCExpr *Offset,
201                                    unsigned char Value = 0) = 0;
202     
203     /// @}
204
205     /// EmitInstruction - Emit the given @param Instruction into the current
206     /// section.
207     virtual void EmitInstruction(const MCInst &Inst) = 0;
208
209     /// Finish - Finish emission of machine code and flush any output.
210     virtual void Finish() = 0;
211   };
212
213   /// createNullStreamer - Create a dummy machine code streamer, which does
214   /// nothing. This is useful for timing the assembler front end.
215   MCStreamer *createNullStreamer(MCContext &Ctx);
216
217   /// createAsmStreamer - Create a machine code streamer which will print out
218   /// assembly for the native target, suitable for compiling with a native
219   /// assembler.
220   MCStreamer *createAsmStreamer(MCContext &Ctx, raw_ostream &OS,
221                                 const MCAsmInfo &MAI,
222                                 MCInstPrinter *InstPrint = 0,
223                                 MCCodeEmitter *CE = 0);
224
225   // FIXME: These two may end up getting rolled into a single
226   // createObjectStreamer interface, which implements the assembler backend, and
227   // is parameterized on an output object file writer.
228
229   /// createMachOStream - Create a machine code streamer which will generative
230   /// Mach-O format object files.
231   MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS,
232                                   MCCodeEmitter *CE = 0);
233
234   /// createELFStreamer - Create a machine code streamer which will generative
235   /// ELF format object files.
236   MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS);
237
238 } // end namespace llvm
239
240 #endif