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