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 /// GetCommentOS - Return a raw_ostream that comments can be written to.
94 /// Unlike AddComment, you are required to terminate comments with \n if you
96 virtual raw_ostream &GetCommentOS();
98 /// @name Symbol & Section Management
101 /// getCurrentSection - Return the current seciton that the streamer is
102 /// emitting code to.
103 const MCSection *getCurrentSection() const { return CurSection; }
105 /// SwitchSection - Set the current section where code is being emitted to
106 /// @param Section. This is required to update CurSection.
108 /// This corresponds to assembler directives like .section, .text, etc.
109 virtual void SwitchSection(const MCSection *Section) = 0;
111 /// EmitLabel - Emit a label for @param Symbol into the current section.
113 /// This corresponds to an assembler statement such as:
116 /// @param Symbol - The symbol to emit. A given symbol should only be
117 /// emitted as a label once, and symbols emitted as a label should never be
118 /// used in an assignment.
119 virtual void EmitLabel(MCSymbol *Symbol) = 0;
121 /// EmitAssemblerFlag - Note in the output the specified @param Flag
122 virtual void EmitAssemblerFlag(AssemblerFlag Flag) = 0;
124 /// EmitAssignment - Emit an assignment of @param Value to @param Symbol.
126 /// This corresponds to an assembler statement such as:
129 /// The assignment generates no code, but has the side effect of binding the
130 /// value in the current context. For the assembly streamer, this prints the
131 /// binding into the .s file.
133 /// @param Symbol - The symbol being assigned to.
134 /// @param Value - The value for the symbol.
135 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
137 /// EmitSymbolAttribute - Add the given @param Attribute to @param Symbol.
138 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
139 SymbolAttr Attribute) = 0;
141 /// EmitSymbolDesc - Set the @param DescValue for the @param Symbol.
143 /// @param Symbol - The symbol to have its n_desc field set.
144 /// @param DescValue - The value to set into the n_desc field.
145 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
147 /// EmitCommonSymbol - Emit a common or local common symbol.
149 /// @param Symbol - The common symbol to emit.
150 /// @param Size - The size of the common symbol.
151 /// @param ByteAlignment - The alignment of the symbol if
152 /// non-zero. This must be a power of 2 on some targets.
153 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
154 unsigned ByteAlignment) = 0;
156 /// EmitZerofill - Emit a the zerofill section and an option symbol.
158 /// @param Section - The zerofill section to create and or to put the symbol
159 /// @param Symbol - The zerofill symbol to emit, if non-NULL.
160 /// @param Size - The size of the zerofill symbol.
161 /// @param ByteAlignment - The alignment of the zerofill symbol if
162 /// non-zero. This must be a power of 2 on some targets.
163 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
164 unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
167 /// @name Generating Data
170 /// EmitBytes - Emit the bytes in \arg Data into the output.
172 /// This is used to implement assembler directives such as .byte, .ascii,
174 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
176 /// EmitValue - Emit the expression @param Value into the output as a native
177 /// integer of the given @param Size bytes.
179 /// This is used to implement assembler directives such as .word, .quad,
182 /// @param Value - The value to emit.
183 /// @param Size - The size of the integer (in bytes) to emit. This must
184 /// match a native machine width.
185 virtual void EmitValue(const MCExpr *Value, unsigned Size,
186 unsigned AddrSpace) = 0;
188 /// EmitIntValue - Special case of EmitValue that avoids the client having
189 /// to pass in a MCExpr for constant integers.
190 virtual void EmitIntValue(uint64_t Value, unsigned Size,unsigned AddrSpace);
192 /// EmitFill - Emit NumBytes bytes worth of the value specified by
193 /// FillValue. This implements directives such as '.space'.
194 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
197 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience
198 /// function that just wraps EmitFill.
199 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) {
200 EmitFill(NumBytes, 0, AddrSpace);
204 /// EmitValueToAlignment - Emit some number of copies of @param Value until
205 /// the byte alignment @param ByteAlignment is reached.
207 /// If the number of bytes need to emit for the alignment is not a multiple
208 /// of @param ValueSize, then the contents of the emitted fill bytes is
211 /// This used to implement the .align assembler directive.
213 /// @param ByteAlignment - The alignment to reach. This must be a power of
214 /// two on some targets.
215 /// @param Value - The value to use when filling bytes.
216 /// @param Size - The size of the integer (in bytes) to emit for @param
217 /// Value. This must match a native machine width.
218 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
219 /// the alignment cannot be reached in this many bytes, no bytes are
221 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
222 unsigned ValueSize = 1,
223 unsigned MaxBytesToEmit = 0) = 0;
225 /// EmitValueToOffset - Emit some number of copies of @param Value until the
226 /// byte offset @param Offset is reached.
228 /// This is used to implement assembler directives such as .org.
230 /// @param Offset - The offset to reach. This may be an expression, but the
231 /// expression must be associated with the current section.
232 /// @param Value - The value to use when filling bytes.
233 virtual void EmitValueToOffset(const MCExpr *Offset,
234 unsigned char Value = 0) = 0;
238 /// EmitInstruction - Emit the given @param Instruction into the current
240 virtual void EmitInstruction(const MCInst &Inst) = 0;
242 /// Finish - Finish emission of machine code and flush any output.
243 virtual void Finish() = 0;
246 /// createNullStreamer - Create a dummy machine code streamer, which does
247 /// nothing. This is useful for timing the assembler front end.
248 MCStreamer *createNullStreamer(MCContext &Ctx);
250 /// createAsmStreamer - Create a machine code streamer which will print out
251 /// assembly for the native target, suitable for compiling with a native
253 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
254 const MCAsmInfo &MAI, bool isLittleEndian,
256 MCInstPrinter *InstPrint = 0,
257 MCCodeEmitter *CE = 0);
259 // FIXME: These two may end up getting rolled into a single
260 // createObjectStreamer interface, which implements the assembler backend, and
261 // is parameterized on an output object file writer.
263 /// createMachOStream - Create a machine code streamer which will generative
264 /// Mach-O format object files.
265 MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS,
266 MCCodeEmitter *CE = 0);
268 /// createELFStreamer - Create a machine code streamer which will generative
269 /// ELF format object files.
270 MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS);
272 } // end namespace llvm