MCStreamer.h revision 6a4824c466bbfbcbe7dc4d95ec1e23a14ec73d87
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;
30class 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 at
66    /// all.
67    virtual bool isVerboseAsm() const { return false; }
68
69    /// AddComment - Add a comment that can be emitted to the generated .s
70    /// file if applicable as a QoI issue to make the output of the compiler
71    /// more readable.  This only affects the MCAsmStreamer, and only when
72    /// verbose assembly output is enabled.
73    ///
74    /// If the comment includes embedded \n's, they will each get the comment
75    /// prefix as appropriate.  The added comment should not end with a \n.
76    virtual void AddComment(const Twine &T) {}
77
78    /// GetCommentOS - Return a raw_ostream that comments can be written to.
79    /// Unlike AddComment, you are required to terminate comments with \n if you
80    /// use this method.
81    virtual raw_ostream &GetCommentOS();
82
83    /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
84    virtual void AddBlankLine() {}
85
86    /// @}
87
88    /// @name Symbol & Section Management
89    /// @{
90
91    /// getCurrentSection - Return the current section that the streamer is
92    /// emitting code to.
93    const MCSection *getCurrentSection() const { return CurSection; }
94
95    /// SwitchSection - Set the current section where code is being emitted to
96    /// @p Section.  This is required to update CurSection.
97    ///
98    /// This corresponds to assembler directives like .section, .text, etc.
99    virtual void SwitchSection(const MCSection *Section) = 0;
100
101    /// EmitLabel - Emit a label for @p Symbol into the current section.
102    ///
103    /// This corresponds to an assembler statement such as:
104    ///   foo:
105    ///
106    /// @param Symbol - The symbol to emit. A given symbol should only be
107    /// emitted as a label once, and symbols emitted as a label should never be
108    /// used in an assignment.
109    virtual void EmitLabel(MCSymbol *Symbol) = 0;
110
111    /// EmitAssemblerFlag - Note in the output the specified @p Flag
112    virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0;
113
114    /// EmitAssignment - Emit an assignment of @p Value to @p Symbol.
115    ///
116    /// This corresponds to an assembler statement such as:
117    ///  symbol = value
118    ///
119    /// The assignment generates no code, but has the side effect of binding the
120    /// value in the current context. For the assembly streamer, this prints the
121    /// binding into the .s file.
122    ///
123    /// @param Symbol - The symbol being assigned to.
124    /// @param Value - The value for the symbol.
125    virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
126
127    /// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol.
128    virtual void EmitSymbolAttribute(MCSymbol *Symbol,
129                                     MCSymbolAttr Attribute) = 0;
130
131    /// EmitSymbolDesc - Set the @p DescValue for the @p Symbol.
132    ///
133    /// @param Symbol - The symbol to have its n_desc field set.
134    /// @param DescValue - The value to set into the n_desc field.
135    virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
136
137
138    /// EmitELFSize - Emit an ELF .size directive.
139    ///
140    /// This corresponds to an assembler statement such as:
141    ///  .size symbol, expression
142    ///
143    virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0;
144
145    /// EmitCommonSymbol - Emit a common symbol.
146    ///
147    /// @param Symbol - The common symbol to emit.
148    /// @param Size - The size of the common symbol.
149    /// @param ByteAlignment - The alignment of the symbol if
150    /// non-zero. This must be a power of 2.
151    virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
152                                  unsigned ByteAlignment) = 0;
153
154    /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
155    ///
156    /// @param Symbol - The common symbol to emit.
157    /// @param Size - The size of the common symbol.
158    virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) = 0;
159
160    /// EmitZerofill - Emit a the zerofill section and an option symbol.
161    ///
162    /// @param Section - The zerofill section to create and or to put the symbol
163    /// @param Symbol - The zerofill symbol to emit, if non-NULL.
164    /// @param Size - The size of the zerofill symbol.
165    /// @param ByteAlignment - The alignment of the zerofill symbol if
166    /// non-zero. This must be a power of 2 on some targets.
167    virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
168                              unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
169
170    /// @}
171    /// @name Generating Data
172    /// @{
173
174    /// EmitBytes - Emit the bytes in \arg Data into the output.
175    ///
176    /// This is used to implement assembler directives such as .byte, .ascii,
177    /// etc.
178    virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
179
180    /// EmitValue - Emit the expression @p Value into the output as a native
181    /// integer of the given @p Size bytes.
182    ///
183    /// This is used to implement assembler directives such as .word, .quad,
184    /// etc.
185    ///
186    /// @param Value - The value to emit.
187    /// @param Size - The size of the integer (in bytes) to emit. This must
188    /// match a native machine width.
189    virtual void EmitValue(const MCExpr *Value, unsigned Size,
190                           unsigned AddrSpace) = 0;
191
192    /// EmitIntValue - Special case of EmitValue that avoids the client having
193    /// to pass in a MCExpr for constant integers.
194    virtual void EmitIntValue(uint64_t Value, unsigned Size,unsigned AddrSpace);
195
196    /// EmitSymbolValue - Special case of EmitValue that avoids the client
197    /// having to pass in a MCExpr for MCSymbols.
198    virtual void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
199                                 unsigned AddrSpace);
200
201    /// EmitGPRel32Value - Emit the expression @p Value into the output as a
202    /// gprel32 (32-bit GP relative) value.
203    ///
204    /// This is used to implement assembler directives such as .gprel32 on
205    /// targets that support them.
206    virtual void EmitGPRel32Value(const MCExpr *Value) = 0;
207
208    /// EmitFill - Emit NumBytes bytes worth of the value specified by
209    /// FillValue.  This implements directives such as '.space'.
210    virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
211                          unsigned AddrSpace);
212
213    /// EmitZeros - Emit NumBytes worth of zeros.  This is a convenience
214    /// function that just wraps EmitFill.
215    void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) {
216      EmitFill(NumBytes, 0, AddrSpace);
217    }
218
219
220    /// EmitValueToAlignment - Emit some number of copies of @p Value until
221    /// the byte alignment @p ByteAlignment is reached.
222    ///
223    /// If the number of bytes need to emit for the alignment is not a multiple
224    /// of @p ValueSize, then the contents of the emitted fill bytes is
225    /// undefined.
226    ///
227    /// This used to implement the .align assembler directive.
228    ///
229    /// @param ByteAlignment - The alignment to reach. This must be a power of
230    /// two on some targets.
231    /// @param Value - The value to use when filling bytes.
232    /// @param ValueSize - The size of the integer (in bytes) to emit for
233    /// @p Value. This must match a native machine width.
234    /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
235    /// the alignment cannot be reached in this many bytes, no bytes are
236    /// emitted.
237    virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
238                                      unsigned ValueSize = 1,
239                                      unsigned MaxBytesToEmit = 0) = 0;
240
241    /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment
242    /// is reached.
243    ///
244    /// This used to align code where the alignment bytes may be executed.  This
245    /// can emit different bytes for different sizes to optimize execution.
246    ///
247    /// @param ByteAlignment - The alignment to reach. This must be a power of
248    /// two on some targets.
249    /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
250    /// the alignment cannot be reached in this many bytes, no bytes are
251    /// emitted.
252    virtual void EmitCodeAlignment(unsigned ByteAlignment,
253                                   unsigned MaxBytesToEmit = 0) = 0;
254
255    /// EmitValueToOffset - Emit some number of copies of @p Value until the
256    /// byte offset @p Offset is reached.
257    ///
258    /// This is used to implement assembler directives such as .org.
259    ///
260    /// @param Offset - The offset to reach. This may be an expression, but the
261    /// expression must be associated with the current section.
262    /// @param Value - The value to use when filling bytes.
263    virtual void EmitValueToOffset(const MCExpr *Offset,
264                                   unsigned char Value = 0) = 0;
265
266    /// @}
267
268    /// EmitFileDirective - Switch to a new logical file.  This is used to
269    /// implement the '.file "foo.c"' assembler directive.
270    virtual void EmitFileDirective(StringRef Filename) = 0;
271
272    /// EmitDwarfFileDirective - Associate a filename with a specified logical
273    /// file number.  This implements the DWARF2 '.file 4 "foo.c"' assembler
274    /// directive.
275    virtual void EmitDwarfFileDirective(unsigned FileNo,StringRef Filename) = 0;
276
277    /// EmitInstruction - Emit the given @p Instruction into the current
278    /// section.
279    virtual void EmitInstruction(const MCInst &Inst) = 0;
280
281    /// Finish - Finish emission of machine code and flush any output.
282    virtual void Finish() = 0;
283  };
284
285  /// createNullStreamer - Create a dummy machine code streamer, which does
286  /// nothing. This is useful for timing the assembler front end.
287  MCStreamer *createNullStreamer(MCContext &Ctx);
288
289  /// createAsmStreamer - Create a machine code streamer which will print out
290  /// assembly for the native target, suitable for compiling with a native
291  /// assembler.
292  ///
293  /// \param InstPrint - If given, the instruction printer to use. If not given
294  /// the MCInst representation will be printed.  This method takes ownership of
295  /// InstPrint.
296  ///
297  /// \param CE - If given, a code emitter to use to show the instruction
298  /// encoding inline with the assembly.
299  ///
300  /// \param ShowInst - Whether to show the MCInst representation inline with
301  /// the assembly.
302  MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
303                                bool isLittleEndian, bool isVerboseAsm,
304                                MCInstPrinter *InstPrint = 0,
305                                MCCodeEmitter *CE = 0,
306                                bool ShowInst = false);
307
308  /// createMachOStream - Create a machine code streamer which will generative
309  /// Mach-O format object files.
310  MCStreamer *createMachOStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
311                                  raw_ostream &OS, MCCodeEmitter *CE);
312
313} // end namespace llvm
314
315#endif
316