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