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