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