MCStreamer.h revision 33adcfb4d217f5f23d9bde8ba02b8e48f9605fc5
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/Support/DataTypes.h"
18
19namespace llvm {
20  class AsmPrinter;
21  class MCContext;
22  class MCValue;
23  class MCInst;
24  class MCSection;
25  class MCSymbol;
26  class StringRef;
27  class raw_ostream;
28  class MCAsmInfo;
29
30  /// MCStreamer - Streaming machine code generation interface.  This interface
31  /// is intended to provide a programatic interface that is very similar to the
32  /// level that an assembler .s file provides.  It has callbacks to emit bytes,
33  /// "emit directives", etc.  The implementation of this interface retains
34  /// state to know what the current section is etc.
35  ///
36  /// There are multiple implementations of this interface: one for writing out
37  /// a .s file, and implementations that write out .o files of various formats.
38  ///
39  class MCStreamer {
40  public:
41    enum SymbolAttr {
42      Global,         /// .globl
43      Hidden,         /// .hidden (ELF)
44      IndirectSymbol, /// .indirect_symbol (Apple)
45      Internal,       /// .internal (ELF)
46      LazyReference,  /// .lazy_reference (Apple)
47      NoDeadStrip,    /// .no_dead_strip (Apple)
48      PrivateExtern,  /// .private_extern (Apple)
49      Protected,      /// .protected (ELF)
50      Reference,      /// .reference (Apple)
51      Weak,           /// .weak
52      WeakDefinition, /// .weak_definition (Apple)
53      WeakReference,  /// .weak_reference (Apple)
54
55      SymbolAttrFirst = Global,
56      SymbolAttrLast = WeakReference
57    };
58
59    enum AssemblerFlag {
60      SubsectionsViaSymbols  /// .subsections_via_symbols (Apple)
61    };
62
63  private:
64    MCContext &Context;
65
66    MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT
67    MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT
68
69  protected:
70    MCStreamer(MCContext &Ctx);
71
72    /// CurSection - This is the current section code is being emitted to, it is
73    /// kept up to date by SwitchSection.
74    const MCSection *CurSection;
75  public:
76    virtual ~MCStreamer();
77
78    MCContext &getContext() const { return Context; }
79
80    /// @name Symbol & Section Management
81    /// @{
82
83    /// SwitchSection - Set the current section where code is being emitted to
84    /// @param Section.  This is required to update CurSection.
85    ///
86    /// This corresponds to assembler directives like .section, .text, etc.
87    virtual void SwitchSection(const MCSection *Section) = 0;
88
89
90    /// getCurrentSection - Return the current seciton that the streamer is
91    /// emitting code to.
92    const MCSection *getCurrentSection() const { return CurSection; }
93
94    /// EmitLabel - Emit a label for @param Symbol into the current section.
95    ///
96    /// This corresponds to an assembler statement such as:
97    ///   foo:
98    ///
99    /// @param Symbol - The symbol to emit. A given symbol should only be
100    /// emitted as a label once, and symbols emitted as a label should never be
101    /// used in an assignment.
102    //
103    // FIXME: What to do about the current section? Should we get rid of the
104    // symbol section in the constructor and initialize it here?
105    virtual void EmitLabel(MCSymbol *Symbol) = 0;
106
107    /// EmitAssemblerFlag - Note in the output the specified @param Flag
108    virtual void EmitAssemblerFlag(AssemblerFlag Flag) = 0;
109
110    /// EmitAssignment - Emit an assignment of @param Value to @param Symbol.
111    ///
112    /// This corresponds to an assembler statement such as:
113    ///  symbol = value
114    ///
115    /// The assignment generates no code, but has the side effect of binding the
116    /// value in the current context. For the assembly streamer, this prints the
117    /// binding into the .s file.
118    ///
119    /// @param Symbol - The symbol being assigned to.
120    /// @param Value - The value for the symbol.
121    /// @param MakeAbsolute - If true, then the symbol should be given the
122    /// absolute value of @param Value, even if @param Value would be
123    /// relocatable expression. This corresponds to the ".set" directive.
124    virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
125                                bool MakeAbsolute = false) = 0;
126
127    /// EmitSymbolAttribute - Add the given @param Attribute to @param Symbol.
128    //
129    // FIXME: This doesn't make much sense, could we just have attributes be on
130    // the symbol and make the printer smart enough to add the right symbols?
131    // This should work as long as the order of attributes in the file doesn't
132    // matter.
133    virtual void EmitSymbolAttribute(MCSymbol *Symbol,
134                                     SymbolAttr Attribute) = 0;
135
136    /// EmitSymbolDesc - Set the @param DescValue for the @param Symbol.
137    ///
138    /// @param Symbol - The symbol to have its n_desc field set.
139    /// @param DescValue - The value to set into the n_desc field.
140    virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
141
142    /// EmitLocalSymbol - Emit a local symbol of @param Value to @param Symbol.
143    ///
144    /// @param Symbol - The local symbol being created.
145    /// @param Value - The value for the symbol.
146    virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) = 0;
147
148    /// EmitCommonSymbol - Emit a common or local common symbol of @param Size
149    /// with the @param Pow2Alignment if non-zero.
150    ///
151    /// @param Symbol - The common symbol to emit.
152    /// @param Size - The size of the common symbol.
153    /// @param Pow2Alignment - The alignment of the common symbol if non-zero.
154    /// @param IsLocal - If true, then the symbol is to be a local common
155    virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
156                                  unsigned Pow2Alignment, bool IsLocal) = 0;
157
158    /// EmitZerofill - Emit a the zerofill section and possiblity a symbol, if
159    /// @param Symbol is non-NULL, for @param Size and with the @param
160    /// Pow2Alignment if non-zero.
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 Pow2Alignment - The alignment of the zerofill symbol if non-zero.
166    virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = 0,
167                              unsigned Size = 0,unsigned Pow2Alignment = 0) = 0;
168
169    /// @}
170    /// @name Generating Data
171    /// @{
172
173    /// EmitBytes - Emit the bytes in @param Data into the output.
174    ///
175    /// This is used to implement assembler directives such as .byte, .ascii,
176    /// etc.
177    virtual void EmitBytes(const StringRef &Data) = 0;
178
179    /// EmitValue - Emit the expression @param Value into the output as a native
180    /// integer of the given @param Size bytes.
181    ///
182    /// This is used to implement assembler directives such as .word, .quad,
183    /// etc.
184    ///
185    /// @param Value - The value to emit.
186    /// @param Size - The size of the integer (in bytes) to emit. This must
187    /// match a native machine width.
188    virtual void EmitValue(const MCValue &Value, unsigned Size) = 0;
189
190    /// EmitValueToAlignment - Emit some number of copies of @param Value until
191    /// the byte alignment @param ByteAlignment is reached.
192    ///
193    /// If the number of bytes need to emit for the alignment is not a multiple
194    /// of @param ValueSize, then the contents of the emitted fill bytes is
195    /// undefined.
196    ///
197    /// This used to implement the .align assembler directive.
198    ///
199    /// @param ByteAlignment - The alignment to reach. This must be a power of
200    /// two on some targets.
201    /// @param Value - The value to use when filling bytes.
202    /// @param Size - The size of the integer (in bytes) to emit for @param
203    /// Value. This must match a native machine width.
204    /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
205    /// the alignment cannot be reached in this many bytes, no bytes are
206    /// emitted.
207    virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
208                                      unsigned ValueSize = 1,
209                                      unsigned MaxBytesToEmit = 0) = 0;
210
211    /// EmitValueToOffset - Emit some number of copies of @param Value until the
212    /// byte offset @param Offset is reached.
213    ///
214    /// This is used to implement assembler directives such as .org.
215    ///
216    /// @param Offset - The offset to reach.This may be an expression, but the
217    /// expression must be associated with the current section.
218    /// @param Value - The value to use when filling bytes.
219    //
220    // FIXME: How are we going to signal failures out of this?
221    virtual void EmitValueToOffset(const MCValue &Offset,
222                                   unsigned char Value = 0) = 0;
223
224    /// @}
225
226    /// EmitInstruction - Emit the given @param Instruction into the current
227    /// section.
228    virtual void EmitInstruction(const MCInst &Inst) = 0;
229
230    /// Finish - Finish emission of machine code and flush any output.
231    virtual void Finish() = 0;
232  };
233
234  /// createNullStreamer - Create a dummy machine code streamer, which does
235  /// nothing. This is useful for timing the assembler front end.
236  MCStreamer *createNullStreamer(MCContext &Ctx);
237
238  /// createAsmStreamer - Create a machine code streamer which will print out
239  /// assembly for the native target, suitable for compiling with a native
240  /// assembler.
241  ///
242  /// \arg AP - If given, an AsmPrinter to use for printing instructions.
243  MCStreamer *createAsmStreamer(MCContext &Ctx, raw_ostream &OS,
244                                const MCAsmInfo &MAI, AsmPrinter *AP = 0);
245
246  // FIXME: These two may end up getting rolled into a single
247  // createObjectStreamer interface, which implements the assembler backend, and
248  // is parameterized on an output object file writer.
249
250  /// createMachOStream - Create a machine code streamer which will generative
251  /// Mach-O format object files.
252  MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS);
253
254  /// createELFStreamer - Create a machine code streamer which will generative
255  /// ELF format object files.
256  MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS);
257
258} // end namespace llvm
259
260#endif
261