1 //===- MCStreamer.h - High-level Streaming Machine Code Output --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the MCStreamer class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_MC_MCSTREAMER_H
15 #define LLVM_MC_MCSTREAMER_H
17 #include "llvm/System/DataTypes.h"
31 class formatted_raw_ostream;
33 /// MCStreamer - Streaming machine code generation interface. This interface
34 /// is intended to provide a programatic interface that is very similar to the
35 /// level that an assembler .s file provides. It has callbacks to emit bytes,
36 /// handle directives, etc. The implementation of this interface retains
37 /// state to know what the current section is etc.
39 /// There are multiple implementations of this interface: one for writing out
40 /// a .s file, and implementations that write out .o files of various formats.
46 Hidden, /// .hidden (ELF)
47 IndirectSymbol, /// .indirect_symbol (Apple)
48 Internal, /// .internal (ELF)
49 LazyReference, /// .lazy_reference (Apple)
50 NoDeadStrip, /// .no_dead_strip (Apple)
51 PrivateExtern, /// .private_extern (Apple)
52 Protected, /// .protected (ELF)
53 Reference, /// .reference (Apple)
55 WeakDefinition, /// .weak_definition (Apple)
56 WeakReference, /// .weak_reference (Apple)
58 SymbolAttrFirst = Global,
59 SymbolAttrLast = WeakReference
63 SubsectionsViaSymbols /// .subsections_via_symbols (Apple)
69 MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT
70 MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT
73 MCStreamer(MCContext &Ctx);
75 /// CurSection - This is the current section code is being emitted to, it is
76 /// kept up to date by SwitchSection.
77 const MCSection *CurSection;
80 virtual ~MCStreamer();
82 MCContext &getContext() const { return Context; }
84 /// AddComment - Add a comment that can be emitted to the generated .s
85 /// file if applicable as a QoI issue to make the output of the compiler
86 /// more readable. This only affects the MCAsmStreamer, and only when
87 /// verbose assembly output is enabled.
89 /// If the comment includes embedded \n's, they will each get the comment
90 /// prefix as appropriate. The added comment should not end with a \n.
91 virtual void AddComment(const Twine &T) {}
93 /// @name Symbol & Section Management
96 /// getCurrentSection - Return the current seciton that the streamer is
98 const MCSection *getCurrentSection() const { return CurSection; }
100 /// SwitchSection - Set the current section where code is being emitted to
101 /// @param Section. This is required to update CurSection.
103 /// This corresponds to assembler directives like .section, .text, etc.
104 virtual void SwitchSection(const MCSection *Section) = 0;
106 /// EmitLabel - Emit a label for @param Symbol into the current section.
108 /// This corresponds to an assembler statement such as:
111 /// @param Symbol - The symbol to emit. A given symbol should only be
112 /// emitted as a label once, and symbols emitted as a label should never be
113 /// used in an assignment.
114 virtual void EmitLabel(MCSymbol *Symbol) = 0;
116 /// EmitAssemblerFlag - Note in the output the specified @param Flag
117 virtual void EmitAssemblerFlag(AssemblerFlag Flag) = 0;
119 /// EmitAssignment - Emit an assignment of @param Value to @param Symbol.
121 /// This corresponds to an assembler statement such as:
124 /// The assignment generates no code, but has the side effect of binding the
125 /// value in the current context. For the assembly streamer, this prints the
126 /// binding into the .s file.
128 /// @param Symbol - The symbol being assigned to.
129 /// @param Value - The value for the symbol.
130 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
132 /// EmitSymbolAttribute - Add the given @param Attribute to @param Symbol.
133 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
134 SymbolAttr Attribute) = 0;
136 /// EmitSymbolDesc - Set the @param DescValue for the @param Symbol.
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;
142 /// EmitCommonSymbol - Emit a common or local common symbol.
144 /// @param Symbol - The common symbol to emit.
145 /// @param Size - The size of the common symbol.
146 /// @param ByteAlignment - The alignment of the symbol if
147 /// non-zero. This must be a power of 2 on some targets.
148 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
149 unsigned ByteAlignment) = 0;
151 /// EmitZerofill - Emit a the zerofill section and an option symbol.
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 ByteAlignment - The alignment of the zerofill symbol if
157 /// non-zero. This must be a power of 2 on some targets.
158 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
159 unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
162 /// @name Generating Data
165 /// EmitBytes - Emit the bytes in \arg Data into the output.
167 /// This is used to implement assembler directives such as .byte, .ascii,
169 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
171 /// EmitValue - Emit the expression @param Value into the output as a native
172 /// integer of the given @param Size bytes.
174 /// This is used to implement assembler directives such as .word, .quad,
177 /// @param Value - The value to emit.
178 /// @param Size - The size of the integer (in bytes) to emit. This must
179 /// match a native machine width.
180 virtual void EmitValue(const MCExpr *Value, unsigned Size,
181 unsigned AddrSpace) = 0;
183 /// EmitIntValue - Special case of EmitValue that avoids the client having
184 /// to pass in a MCExpr for constant integers.
185 virtual void EmitIntValue(uint64_t Value, unsigned Size,unsigned AddrSpace);
187 /// EmitFill - Emit NumBytes bytes worth of the value specified by
188 /// FillValue. This implements directives such as '.space'.
189 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
192 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience
193 /// function that just wraps EmitFill.
194 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) {
195 EmitFill(NumBytes, 0, AddrSpace);
199 /// EmitValueToAlignment - Emit some number of copies of @param Value until
200 /// the byte alignment @param ByteAlignment is reached.
202 /// If the number of bytes need to emit for the alignment is not a multiple
203 /// of @param ValueSize, then the contents of the emitted fill bytes is
206 /// This used to implement the .align assembler directive.
208 /// @param ByteAlignment - The alignment to reach. This must be a power of
209 /// two on some targets.
210 /// @param Value - The value to use when filling bytes.
211 /// @param Size - The size of the integer (in bytes) to emit for @param
212 /// Value. This must match a native machine width.
213 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
214 /// the alignment cannot be reached in this many bytes, no bytes are
216 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
217 unsigned ValueSize = 1,
218 unsigned MaxBytesToEmit = 0) = 0;
220 /// EmitValueToOffset - Emit some number of copies of @param Value until the
221 /// byte offset @param Offset is reached.
223 /// This is used to implement assembler directives such as .org.
225 /// @param Offset - The offset to reach. This may be an expression, but the
226 /// expression must be associated with the current section.
227 /// @param Value - The value to use when filling bytes.
228 virtual void EmitValueToOffset(const MCExpr *Offset,
229 unsigned char Value = 0) = 0;
233 /// EmitInstruction - Emit the given @param Instruction into the current
235 virtual void EmitInstruction(const MCInst &Inst) = 0;
237 /// Finish - Finish emission of machine code and flush any output.
238 virtual void Finish() = 0;
241 /// createNullStreamer - Create a dummy machine code streamer, which does
242 /// nothing. This is useful for timing the assembler front end.
243 MCStreamer *createNullStreamer(MCContext &Ctx);
245 /// createAsmStreamer - Create a machine code streamer which will print out
246 /// assembly for the native target, suitable for compiling with a native
248 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
249 const MCAsmInfo &MAI, bool isLittleEndian,
251 MCInstPrinter *InstPrint = 0,
252 MCCodeEmitter *CE = 0);
254 // FIXME: These two may end up getting rolled into a single
255 // createObjectStreamer interface, which implements the assembler backend, and
256 // is parameterized on an output object file writer.
258 /// createMachOStream - Create a machine code streamer which will generative
259 /// Mach-O format object files.
260 MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS,
261 MCCodeEmitter *CE = 0);
263 /// createELFStreamer - Create a machine code streamer which will generative
264 /// ELF format object files.
265 MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS);
267 } // end namespace llvm