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"
18 #include "llvm/MC/MCDirectives.h"
32 class formatted_raw_ostream;
34 /// MCStreamer - Streaming machine code generation interface. This interface
35 /// is intended to provide a programatic interface that is very similar to the
36 /// level that an assembler .s file provides. It has callbacks to emit bytes,
37 /// handle directives, etc. The implementation of this interface retains
38 /// state to know what the current section is etc.
40 /// There are multiple implementations of this interface: one for writing out
41 /// a .s file, and implementations that write out .o files of various formats.
46 MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT
47 MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT
50 MCStreamer(MCContext &Ctx);
52 /// CurSection - This is the current section code is being emitted to, it is
53 /// kept up to date by SwitchSection.
54 const MCSection *CurSection;
57 virtual ~MCStreamer();
59 MCContext &getContext() const { return Context; }
61 /// @name Assembly File Formatting.
64 /// AddComment - Add a comment that can be emitted to the generated .s
65 /// file if applicable as a QoI issue to make the output of the compiler
66 /// more readable. This only affects the MCAsmStreamer, and only when
67 /// verbose assembly output is enabled.
69 /// If the comment includes embedded \n's, they will each get the comment
70 /// prefix as appropriate. The added comment should not end with a \n.
71 virtual void AddComment(const Twine &T) {}
73 /// GetCommentOS - Return a raw_ostream that comments can be written to.
74 /// Unlike AddComment, you are required to terminate comments with \n if you
76 virtual raw_ostream &GetCommentOS();
78 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
79 virtual void AddBlankLine() {}
83 /// @name Symbol & Section Management
86 /// getCurrentSection - Return the current seciton that the streamer is
88 const MCSection *getCurrentSection() const { return CurSection; }
90 /// SwitchSection - Set the current section where code is being emitted to
91 /// @param Section. This is required to update CurSection.
93 /// This corresponds to assembler directives like .section, .text, etc.
94 virtual void SwitchSection(const MCSection *Section) = 0;
96 /// EmitLabel - Emit a label for @param Symbol into the current section.
98 /// This corresponds to an assembler statement such as:
101 /// @param Symbol - The symbol to emit. A given symbol should only be
102 /// emitted as a label once, and symbols emitted as a label should never be
103 /// used in an assignment.
104 virtual void EmitLabel(MCSymbol *Symbol) = 0;
106 /// EmitAssemblerFlag - Note in the output the specified @param Flag
107 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0;
109 /// EmitAssignment - Emit an assignment of @param Value to @param Symbol.
111 /// This corresponds to an assembler statement such as:
114 /// The assignment generates no code, but has the side effect of binding the
115 /// value in the current context. For the assembly streamer, this prints the
116 /// binding into the .s file.
118 /// @param Symbol - The symbol being assigned to.
119 /// @param Value - The value for the symbol.
120 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
122 /// EmitSymbolAttribute - Add the given @param Attribute to @param Symbol.
123 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
124 MCSymbolAttr Attribute) = 0;
126 /// EmitSymbolDesc - Set the @param DescValue for the @param Symbol.
128 /// @param Symbol - The symbol to have its n_desc field set.
129 /// @param DescValue - The value to set into the n_desc field.
130 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
133 /// EmitELFSize - Emit an ELF .size directive.
135 /// This corresponds to an assembler statement such as:
136 /// .size symbol, expression
138 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0;
140 /// EmitCommonSymbol - Emit a common symbol.
142 /// @param Symbol - The common symbol to emit.
143 /// @param Size - The size of the common symbol.
144 /// @param ByteAlignment - The alignment of the symbol if
145 /// non-zero. This must be a power of 2.
146 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
147 unsigned ByteAlignment) = 0;
149 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
151 /// @param Symbol - The common symbol to emit.
152 /// @param Size - The size of the common symbol.
153 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) = 0;
155 /// EmitZerofill - Emit a the zerofill section and an option symbol.
157 /// @param Section - The zerofill section to create and or to put the symbol
158 /// @param Symbol - The zerofill symbol to emit, if non-NULL.
159 /// @param Size - The size of the zerofill symbol.
160 /// @param ByteAlignment - The alignment of the zerofill symbol if
161 /// non-zero. This must be a power of 2 on some targets.
162 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
163 unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
166 /// @name Generating Data
169 /// EmitBytes - Emit the bytes in \arg Data into the output.
171 /// This is used to implement assembler directives such as .byte, .ascii,
173 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
175 /// EmitValue - Emit the expression @param Value into the output as a native
176 /// integer of the given @param Size bytes.
178 /// This is used to implement assembler directives such as .word, .quad,
181 /// @param Value - The value to emit.
182 /// @param Size - The size of the integer (in bytes) to emit. This must
183 /// match a native machine width.
184 virtual void EmitValue(const MCExpr *Value, unsigned Size,
185 unsigned AddrSpace) = 0;
187 /// EmitIntValue - Special case of EmitValue that avoids the client having
188 /// to pass in a MCExpr for constant integers.
189 virtual void EmitIntValue(uint64_t Value, unsigned Size,unsigned AddrSpace);
191 /// EmitGPRel32Value - Emit the expression @param Value into the output as a
192 /// gprel32 (32-bit GP relative) value.
194 /// This is used to implement assembler directives such as .gprel32 on
195 /// targets that support them.
196 virtual void EmitGPRel32Value(const MCExpr *Value) = 0;
198 /// EmitFill - Emit NumBytes bytes worth of the value specified by
199 /// FillValue. This implements directives such as '.space'.
200 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
203 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience
204 /// function that just wraps EmitFill.
205 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) {
206 EmitFill(NumBytes, 0, AddrSpace);
210 /// EmitValueToAlignment - Emit some number of copies of @param Value until
211 /// the byte alignment @param ByteAlignment is reached.
213 /// If the number of bytes need to emit for the alignment is not a multiple
214 /// of @param ValueSize, then the contents of the emitted fill bytes is
217 /// This used to implement the .align assembler directive.
219 /// @param ByteAlignment - The alignment to reach. This must be a power of
220 /// two on some targets.
221 /// @param Value - The value to use when filling bytes.
222 /// @param Size - The size of the integer (in bytes) to emit for @param
223 /// Value. This must match a native machine width.
224 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
225 /// the alignment cannot be reached in this many bytes, no bytes are
227 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
228 unsigned ValueSize = 1,
229 unsigned MaxBytesToEmit = 0) = 0;
231 /// EmitValueToOffset - Emit some number of copies of @param Value until the
232 /// byte offset @param Offset is reached.
234 /// This is used to implement assembler directives such as .org.
236 /// @param Offset - The offset to reach. This may be an expression, but the
237 /// expression must be associated with the current section.
238 /// @param Value - The value to use when filling bytes.
239 virtual void EmitValueToOffset(const MCExpr *Offset,
240 unsigned char Value = 0) = 0;
244 /// EmitFileDirective - Switch to a new logical file. This is used to
245 /// implement the '.file "foo.c"' assembler directive.
246 virtual void EmitFileDirective(StringRef Filename) = 0;
248 /// EmitDwarfFileDirective - Associate a filename with a specified logical
249 /// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler
251 virtual void EmitDwarfFileDirective(unsigned FileNo,StringRef Filename) = 0;
253 /// EmitInstruction - Emit the given @param Instruction into the current
255 virtual void EmitInstruction(const MCInst &Inst) = 0;
257 /// Finish - Finish emission of machine code and flush any output.
258 virtual void Finish() = 0;
261 /// createNullStreamer - Create a dummy machine code streamer, which does
262 /// nothing. This is useful for timing the assembler front end.
263 MCStreamer *createNullStreamer(MCContext &Ctx);
265 /// createAsmStreamer - Create a machine code streamer which will print out
266 /// assembly for the native target, suitable for compiling with a native
268 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
269 const MCAsmInfo &MAI, bool isLittleEndian,
271 MCInstPrinter *InstPrint = 0,
272 MCCodeEmitter *CE = 0);
274 // FIXME: These two may end up getting rolled into a single
275 // createObjectStreamer interface, which implements the assembler backend, and
276 // is parameterized on an output object file writer.
278 /// createMachOStream - Create a machine code streamer which will generative
279 /// Mach-O format object files.
280 MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS,
281 MCCodeEmitter *CE = 0);
283 /// createELFStreamer - Create a machine code streamer which will generative
284 /// ELF format object files.
285 MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS);
287 } // end namespace llvm