Add ELF ObjectWriter and Streamer support.
[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/System/DataTypes.h"
18 #include "llvm/MC/MCDirectives.h"
19
20 namespace llvm {
21   class MCAsmInfo;
22   class MCCodeEmitter;
23   class MCContext;
24   class MCExpr;
25   class MCInst;
26   class MCInstPrinter;
27   class MCSection;
28   class MCSymbol;
29   class StringRef;
30   class TargetAsmBackend;
31   class Twine;
32   class raw_ostream;
33   class formatted_raw_ostream;
34
35   /// MCStreamer - Streaming machine code generation interface.  This interface
36   /// is intended to provide a programatic interface that is very similar to the
37   /// level that an assembler .s file provides.  It has callbacks to emit bytes,
38   /// handle directives, etc.  The implementation of this interface retains
39   /// state to know what the current section is etc.
40   ///
41   /// There are multiple implementations of this interface: one for writing out
42   /// a .s file, and implementations that write out .o files of various formats.
43   ///
44   class MCStreamer {
45     MCContext &Context;
46
47     MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT
48     MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT
49
50   protected:
51     MCStreamer(MCContext &Ctx);
52
53     /// CurSection - This is the current section code is being emitted to, it is
54     /// kept up to date by SwitchSection.
55     const MCSection *CurSection;
56
57   public:
58     virtual ~MCStreamer();
59
60     MCContext &getContext() const { return Context; }
61
62     /// @name Assembly File Formatting.
63     /// @{
64     
65     /// isVerboseAsm - Return true if this streamer supports verbose assembly
66     /// and if it is enabled.
67     virtual bool isVerboseAsm() const { return false; }
68     
69     /// hasRawTextSupport - Return true if this asm streamer supports emitting
70     /// unformatted text to the .s file with EmitRawText.
71     virtual bool hasRawTextSupport() const { return false; }
72
73     /// AddComment - Add a comment that can be emitted to the generated .s
74     /// file if applicable as a QoI issue to make the output of the compiler
75     /// more readable.  This only affects the MCAsmStreamer, and only when
76     /// verbose assembly output is enabled.
77     ///
78     /// If the comment includes embedded \n's, they will each get the comment
79     /// prefix as appropriate.  The added comment should not end with a \n.
80     virtual void AddComment(const Twine &T) {}
81     
82     /// GetCommentOS - Return a raw_ostream that comments can be written to.
83     /// Unlike AddComment, you are required to terminate comments with \n if you
84     /// use this method.
85     virtual raw_ostream &GetCommentOS();
86     
87     /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
88     virtual void AddBlankLine() {}
89     
90     /// @}
91     
92     /// @name Symbol & Section Management
93     /// @{
94     
95     /// getCurrentSection - Return the current section that the streamer is
96     /// emitting code to.
97     const MCSection *getCurrentSection() const { return CurSection; }
98
99     /// SwitchSection - Set the current section where code is being emitted to
100     /// @p Section.  This is required to update CurSection.
101     ///
102     /// This corresponds to assembler directives like .section, .text, etc.
103     virtual void SwitchSection(const MCSection *Section) = 0;
104     
105     /// EmitLabel - Emit a label for @p Symbol into the current section.
106     ///
107     /// This corresponds to an assembler statement such as:
108     ///   foo:
109     ///
110     /// @param Symbol - The symbol to emit. A given symbol should only be
111     /// emitted as a label once, and symbols emitted as a label should never be
112     /// used in an assignment.
113     virtual void EmitLabel(MCSymbol *Symbol) = 0;
114
115     /// EmitAssemblerFlag - Note in the output the specified @p Flag
116     virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0;
117
118     /// EmitAssignment - Emit an assignment of @p Value to @p Symbol.
119     ///
120     /// This corresponds to an assembler statement such as:
121     ///  symbol = value
122     ///
123     /// The assignment generates no code, but has the side effect of binding the
124     /// value in the current context. For the assembly streamer, this prints the
125     /// binding into the .s file.
126     ///
127     /// @param Symbol - The symbol being assigned to.
128     /// @param Value - The value for the symbol.
129     virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
130
131     /// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol.
132     virtual void EmitSymbolAttribute(MCSymbol *Symbol,
133                                      MCSymbolAttr Attribute) = 0;
134
135     /// EmitSymbolDesc - Set the @p DescValue for the @p Symbol.
136     ///
137     /// @param Symbol - The symbol to have its n_desc field set.
138     /// @param DescValue - The value to set into the n_desc field.
139     virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
140
141     /// BeginCOFFSymbolDef - Start emitting COFF symbol definition
142     ///
143     /// @param Symbol - The symbol to have its External & Type fields set.
144     virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) = 0;
145
146     /// EmitCOFFSymbolStorageClass - Emit the storage class of the symbol.
147     ///
148     /// @param StorageClass - The storage class the symbol should have.
149     virtual void EmitCOFFSymbolStorageClass(int StorageClass) = 0;
150
151     /// EmitCOFFSymbolType - Emit the type of the symbol.
152     ///
153     /// @param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
154     virtual void EmitCOFFSymbolType(int Type) = 0;
155
156     /// EndCOFFSymbolDef - Marks the end of the symbol definition.
157     virtual void EndCOFFSymbolDef() = 0;
158
159     /// EmitELFSize - Emit an ELF .size directive.
160     ///
161     /// This corresponds to an assembler statement such as:
162     ///  .size symbol, expression
163     ///
164     virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0;
165     
166     /// EmitCommonSymbol - Emit a common symbol.
167     ///
168     /// @param Symbol - The common symbol to emit.
169     /// @param Size - The size of the common symbol.
170     /// @param ByteAlignment - The alignment of the symbol if
171     /// non-zero. This must be a power of 2.
172     virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
173                                   unsigned ByteAlignment) = 0;
174
175     /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
176     ///
177     /// @param Symbol - The common symbol to emit.
178     /// @param Size - The size of the common symbol.
179     virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) = 0;
180     
181     /// EmitZerofill - Emit the zerofill section and an optional symbol.
182     ///
183     /// @param Section - The zerofill section to create and or to put the symbol
184     /// @param Symbol - The zerofill symbol to emit, if non-NULL.
185     /// @param Size - The size of the zerofill symbol.
186     /// @param ByteAlignment - The alignment of the zerofill symbol if
187     /// non-zero. This must be a power of 2 on some targets.
188     virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
189                               unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
190
191     /// EmitTBSSSymbol - Emit a thread local bss (.tbss) symbol.
192     ///
193     /// @param Section - The thread local common section.
194     /// @param Symbol - The thread local common symbol to emit.
195     /// @param Size - The size of the symbol.
196     /// @param ByteAlignment - The alignment of the thread local common symbol
197     /// if non-zero.  This must be a power of 2 on some targets.
198     virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
199                                 uint64_t Size, unsigned ByteAlignment = 0) = 0;                                
200     /// @}
201     /// @name Generating Data
202     /// @{
203
204     /// EmitBytes - Emit the bytes in \arg Data into the output.
205     ///
206     /// This is used to implement assembler directives such as .byte, .ascii,
207     /// etc.
208     virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
209
210     /// EmitValue - Emit the expression @p Value into the output as a native
211     /// integer of the given @p Size bytes.
212     ///
213     /// This is used to implement assembler directives such as .word, .quad,
214     /// etc.
215     ///
216     /// @param Value - The value to emit.
217     /// @param Size - The size of the integer (in bytes) to emit. This must
218     /// match a native machine width.
219     virtual void EmitValue(const MCExpr *Value, unsigned Size,
220                            unsigned AddrSpace = 0) = 0;
221
222     /// EmitIntValue - Special case of EmitValue that avoids the client having
223     /// to pass in a MCExpr for constant integers.
224     virtual void EmitIntValue(uint64_t Value, unsigned Size,
225                               unsigned AddrSpace = 0);
226     
227     /// EmitSymbolValue - Special case of EmitValue that avoids the client
228     /// having to pass in a MCExpr for MCSymbols.
229     virtual void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
230                                  unsigned AddrSpace);
231     
232     /// EmitGPRel32Value - Emit the expression @p Value into the output as a
233     /// gprel32 (32-bit GP relative) value.
234     ///
235     /// This is used to implement assembler directives such as .gprel32 on
236     /// targets that support them.
237     virtual void EmitGPRel32Value(const MCExpr *Value) = 0;
238     
239     /// EmitFill - Emit NumBytes bytes worth of the value specified by
240     /// FillValue.  This implements directives such as '.space'.
241     virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
242                           unsigned AddrSpace);
243     
244     /// EmitZeros - Emit NumBytes worth of zeros.  This is a convenience
245     /// function that just wraps EmitFill.
246     void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) {
247       EmitFill(NumBytes, 0, AddrSpace);
248     }
249
250     
251     /// EmitValueToAlignment - Emit some number of copies of @p Value until
252     /// the byte alignment @p ByteAlignment is reached.
253     ///
254     /// If the number of bytes need to emit for the alignment is not a multiple
255     /// of @p ValueSize, then the contents of the emitted fill bytes is
256     /// undefined.
257     ///
258     /// This used to implement the .align assembler directive.
259     ///
260     /// @param ByteAlignment - The alignment to reach. This must be a power of
261     /// two on some targets.
262     /// @param Value - The value to use when filling bytes.
263     /// @param ValueSize - The size of the integer (in bytes) to emit for
264     /// @p Value. This must match a native machine width.
265     /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
266     /// the alignment cannot be reached in this many bytes, no bytes are
267     /// emitted.
268     virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
269                                       unsigned ValueSize = 1,
270                                       unsigned MaxBytesToEmit = 0) = 0;
271
272     /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment
273     /// is reached.
274     ///
275     /// This used to align code where the alignment bytes may be executed.  This
276     /// can emit different bytes for different sizes to optimize execution.
277     ///
278     /// @param ByteAlignment - The alignment to reach. This must be a power of
279     /// two on some targets.
280     /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
281     /// the alignment cannot be reached in this many bytes, no bytes are
282     /// emitted.
283     virtual void EmitCodeAlignment(unsigned ByteAlignment,
284                                    unsigned MaxBytesToEmit = 0) = 0;
285
286     /// EmitValueToOffset - Emit some number of copies of @p Value until the
287     /// byte offset @p Offset is reached.
288     ///
289     /// This is used to implement assembler directives such as .org.
290     ///
291     /// @param Offset - The offset to reach. This may be an expression, but the
292     /// expression must be associated with the current section.
293     /// @param Value - The value to use when filling bytes.
294     virtual void EmitValueToOffset(const MCExpr *Offset,
295                                    unsigned char Value = 0) = 0;
296     
297     /// @}
298     
299     /// EmitFileDirective - Switch to a new logical file.  This is used to
300     /// implement the '.file "foo.c"' assembler directive.
301     virtual void EmitFileDirective(StringRef Filename) = 0;
302     
303     /// EmitDwarfFileDirective - Associate a filename with a specified logical
304     /// file number.  This implements the DWARF2 '.file 4 "foo.c"' assembler
305     /// directive.
306     virtual void EmitDwarfFileDirective(unsigned FileNo,StringRef Filename) = 0;
307
308     /// EmitInstruction - Emit the given @p Instruction into the current
309     /// section.
310     virtual void EmitInstruction(const MCInst &Inst) = 0;
311
312     /// EmitRawText - If this file is backed by a assembly streamer, this dumps
313     /// the specified string in the output .s file.  This capability is
314     /// indicated by the hasRawTextSupport() predicate.  By default this aborts.
315     virtual void EmitRawText(StringRef String);
316     void EmitRawText(const Twine &String);
317     
318     /// Finish - Finish emission of machine code.
319     virtual void Finish() = 0;
320   };
321
322   /// createNullStreamer - Create a dummy machine code streamer, which does
323   /// nothing. This is useful for timing the assembler front end.
324   MCStreamer *createNullStreamer(MCContext &Ctx);
325
326   /// createAsmStreamer - Create a machine code streamer which will print out
327   /// assembly for the native target, suitable for compiling with a native
328   /// assembler.
329   ///
330   /// \param InstPrint - If given, the instruction printer to use. If not given
331   /// the MCInst representation will be printed.  This method takes ownership of
332   /// InstPrint.
333   ///
334   /// \param CE - If given, a code emitter to use to show the instruction
335   /// encoding inline with the assembly. This method takes ownership of \arg CE.
336   ///
337   /// \param ShowInst - Whether to show the MCInst representation inline with
338   /// the assembly.
339   MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
340                                 bool isLittleEndian, bool isVerboseAsm,
341                                 MCInstPrinter *InstPrint = 0,
342                                 MCCodeEmitter *CE = 0,
343                                 bool ShowInst = false);
344
345   /// createMachOStreamer - Create a machine code streamer which will generate
346   /// Mach-O format object files.
347   ///
348   /// Takes ownership of \arg TAB and \arg CE.
349   MCStreamer *createMachOStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
350                                   raw_ostream &OS, MCCodeEmitter *CE,
351                                   bool RelaxAll = false);
352
353   /// createWinCOFFStreamer - Create a machine code streamer which will
354   /// generate Microsoft COFF format object files.
355   ///
356   /// Takes ownership of \arg TAB and \arg CE.
357   MCStreamer *createWinCOFFStreamer(MCContext &Ctx,
358                                     TargetAsmBackend &TAB,
359                                     MCCodeEmitter &CE, raw_ostream &OS,
360                                     bool RelaxAll = false);
361
362   /// createELFStreamer - Create a machine code streamer which will generate
363   /// ELF format object files.
364   MCStreamer *createELFStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
365                                 raw_ostream &OS, MCCodeEmitter *CE,
366                                 bool RelaxAll = false);
367
368   /// createLoggingStreamer - Create a machine code streamer which just logs the
369   /// API calls and then dispatches to another streamer.
370   ///
371   /// The new streamer takes ownership of the \arg Child.
372   MCStreamer *createLoggingStreamer(MCStreamer *Child, raw_ostream &OS);
373
374 } // end namespace llvm
375
376 #endif