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