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