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"
30 class TargetAsmBackend;
33 class formatted_raw_ostream;
35 /// MCStreamer - Streaming machine code generation interface. This interface
36 /// is intended to provide a programatic interface that is very similar to the
37 /// level that an assembler .s file provides. It has callbacks to emit bytes,
38 /// handle directives, etc. The implementation of this interface retains
39 /// state to know what the current section is etc.
41 /// There are multiple implementations of this interface: one for writing out
42 /// a .s file, and implementations that write out .o files of various formats.
47 MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT
48 MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT
51 MCStreamer(MCContext &Ctx);
53 /// CurSection - This is the current section code is being emitted to, it is
54 /// kept up to date by SwitchSection.
55 const MCSection *CurSection;
58 virtual ~MCStreamer();
60 MCContext &getContext() const { return Context; }
62 /// @name Assembly File Formatting.
65 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
67 virtual bool isVerboseAsm() const { return false; }
69 /// AddComment - Add a comment that can be emitted to the generated .s
70 /// file if applicable as a QoI issue to make the output of the compiler
71 /// more readable. This only affects the MCAsmStreamer, and only when
72 /// verbose assembly output is enabled.
74 /// If the comment includes embedded \n's, they will each get the comment
75 /// prefix as appropriate. The added comment should not end with a \n.
76 virtual void AddComment(const Twine &T) {}
78 /// GetCommentOS - Return a raw_ostream that comments can be written to.
79 /// Unlike AddComment, you are required to terminate comments with \n if you
81 virtual raw_ostream &GetCommentOS();
83 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
84 virtual void AddBlankLine() {}
88 /// @name Symbol & Section Management
91 /// getCurrentSection - Return the current section that the streamer is
93 const MCSection *getCurrentSection() const { return CurSection; }
95 /// SwitchSection - Set the current section where code is being emitted to
96 /// @p Section. This is required to update CurSection.
98 /// This corresponds to assembler directives like .section, .text, etc.
99 virtual void SwitchSection(const MCSection *Section) = 0;
101 /// EmitLabel - Emit a label for @p Symbol into the current section.
103 /// This corresponds to an assembler statement such as:
106 /// @param Symbol - The symbol to emit. A given symbol should only be
107 /// emitted as a label once, and symbols emitted as a label should never be
108 /// used in an assignment.
109 virtual void EmitLabel(MCSymbol *Symbol) = 0;
111 /// EmitAssemblerFlag - Note in the output the specified @p Flag
112 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0;
114 /// EmitAssignment - Emit an assignment of @p Value to @p Symbol.
116 /// This corresponds to an assembler statement such as:
119 /// The assignment generates no code, but has the side effect of binding the
120 /// value in the current context. For the assembly streamer, this prints the
121 /// binding into the .s file.
123 /// @param Symbol - The symbol being assigned to.
124 /// @param Value - The value for the symbol.
125 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
127 /// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol.
128 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
129 MCSymbolAttr Attribute) = 0;
131 /// EmitSymbolDesc - Set the @p DescValue for the @p Symbol.
133 /// @param Symbol - The symbol to have its n_desc field set.
134 /// @param DescValue - The value to set into the n_desc field.
135 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
138 /// EmitELFSize - Emit an ELF .size directive.
140 /// This corresponds to an assembler statement such as:
141 /// .size symbol, expression
143 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0;
145 /// EmitCommonSymbol - Emit a common symbol.
147 /// @param Symbol - The common symbol to emit.
148 /// @param Size - The size of the common symbol.
149 /// @param ByteAlignment - The alignment of the symbol if
150 /// non-zero. This must be a power of 2.
151 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
152 unsigned ByteAlignment) = 0;
154 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
156 /// @param Symbol - The common symbol to emit.
157 /// @param Size - The size of the common symbol.
158 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) = 0;
160 /// EmitZerofill - Emit a the zerofill section and an option symbol.
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 ByteAlignment - The alignment of the zerofill symbol if
166 /// non-zero. This must be a power of 2 on some targets.
167 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
168 unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
171 /// @name Generating Data
174 /// EmitBytes - Emit the bytes in \arg Data into the output.
176 /// This is used to implement assembler directives such as .byte, .ascii,
178 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
180 /// EmitValue - Emit the expression @p Value into the output as a native
181 /// integer of the given @p Size bytes.
183 /// This is used to implement assembler directives such as .word, .quad,
186 /// @param Value - The value to emit.
187 /// @param Size - The size of the integer (in bytes) to emit. This must
188 /// match a native machine width.
189 virtual void EmitValue(const MCExpr *Value, unsigned Size,
190 unsigned AddrSpace) = 0;
192 /// EmitIntValue - Special case of EmitValue that avoids the client having
193 /// to pass in a MCExpr for constant integers.
194 virtual void EmitIntValue(uint64_t Value, unsigned Size,unsigned AddrSpace);
196 /// EmitSymbolValue - Special case of EmitValue that avoids the client
197 /// having to pass in a MCExpr for MCSymbols.
198 virtual void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
201 /// EmitGPRel32Value - Emit the expression @p Value into the output as a
202 /// gprel32 (32-bit GP relative) value.
204 /// This is used to implement assembler directives such as .gprel32 on
205 /// targets that support them.
206 virtual void EmitGPRel32Value(const MCExpr *Value) = 0;
208 /// EmitFill - Emit NumBytes bytes worth of the value specified by
209 /// FillValue. This implements directives such as '.space'.
210 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
213 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience
214 /// function that just wraps EmitFill.
215 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) {
216 EmitFill(NumBytes, 0, AddrSpace);
220 /// EmitValueToAlignment - Emit some number of copies of @p Value until
221 /// the byte alignment @p ByteAlignment is reached.
223 /// If the number of bytes need to emit for the alignment is not a multiple
224 /// of @p ValueSize, then the contents of the emitted fill bytes is
227 /// This used to implement the .align assembler directive.
229 /// @param ByteAlignment - The alignment to reach. This must be a power of
230 /// two on some targets.
231 /// @param Value - The value to use when filling bytes.
232 /// @param ValueSize - The size of the integer (in bytes) to emit for
233 /// @p Value. This must match a native machine width.
234 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
235 /// the alignment cannot be reached in this many bytes, no bytes are
237 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
238 unsigned ValueSize = 1,
239 unsigned MaxBytesToEmit = 0) = 0;
241 /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment
244 /// This used to align code where the alignment bytes may be executed. This
245 /// can emit different bytes for different sizes to optimize execution.
247 /// @param ByteAlignment - The alignment to reach. This must be a power of
248 /// two on some targets.
249 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
250 /// the alignment cannot be reached in this many bytes, no bytes are
252 virtual void EmitCodeAlignment(unsigned ByteAlignment,
253 unsigned MaxBytesToEmit = 0) = 0;
255 /// EmitValueToOffset - Emit some number of copies of @p Value until the
256 /// byte offset @p Offset is reached.
258 /// This is used to implement assembler directives such as .org.
260 /// @param Offset - The offset to reach. This may be an expression, but the
261 /// expression must be associated with the current section.
262 /// @param Value - The value to use when filling bytes.
263 virtual void EmitValueToOffset(const MCExpr *Offset,
264 unsigned char Value = 0) = 0;
268 /// EmitFileDirective - Switch to a new logical file. This is used to
269 /// implement the '.file "foo.c"' assembler directive.
270 virtual void EmitFileDirective(StringRef Filename) = 0;
272 /// EmitDwarfFileDirective - Associate a filename with a specified logical
273 /// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler
275 virtual void EmitDwarfFileDirective(unsigned FileNo,StringRef Filename) = 0;
277 /// EmitInstruction - Emit the given @p Instruction into the current
279 virtual void EmitInstruction(const MCInst &Inst) = 0;
281 /// Finish - Finish emission of machine code and flush any output.
282 virtual void Finish() = 0;
285 /// createNullStreamer - Create a dummy machine code streamer, which does
286 /// nothing. This is useful for timing the assembler front end.
287 MCStreamer *createNullStreamer(MCContext &Ctx);
289 /// createAsmStreamer - Create a machine code streamer which will print out
290 /// assembly for the native target, suitable for compiling with a native
293 /// \param InstPrint - If given, the instruction printer to use. If not given
294 /// the MCInst representation will be printed. This method takes ownership of
297 /// \param CE - If given, a code emitter to use to show the instruction
298 /// encoding inline with the assembly.
300 /// \param ShowInst - Whether to show the MCInst representation inline with
302 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
303 bool isLittleEndian, bool isVerboseAsm,
304 MCInstPrinter *InstPrint = 0,
305 MCCodeEmitter *CE = 0,
306 bool ShowInst = false);
308 /// createMachOStream - Create a machine code streamer which will generative
309 /// Mach-O format object files.
310 MCStreamer *createMachOStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
311 raw_ostream &OS, MCCodeEmitter *CE);
313 } // end namespace llvm