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;
57 /// PrevSection - This is the previous section code is being emitted to, it is
58 /// kept up to date by SwitchSection.
59 const MCSection *PrevSection;
62 virtual ~MCStreamer();
64 MCContext &getContext() const { return Context; }
66 /// @name Assembly File Formatting.
69 /// isVerboseAsm - Return true if this streamer supports verbose assembly
70 /// and if it is enabled.
71 virtual bool isVerboseAsm() const { return false; }
73 /// hasRawTextSupport - Return true if this asm streamer supports emitting
74 /// unformatted text to the .s file with EmitRawText.
75 virtual bool hasRawTextSupport() const { return false; }
77 /// AddComment - Add a comment that can be emitted to the generated .s
78 /// file if applicable as a QoI issue to make the output of the compiler
79 /// more readable. This only affects the MCAsmStreamer, and only when
80 /// verbose assembly output is enabled.
82 /// If the comment includes embedded \n's, they will each get the comment
83 /// prefix as appropriate. The added comment should not end with a \n.
84 virtual void AddComment(const Twine &T) {}
86 /// GetCommentOS - Return a raw_ostream that comments can be written to.
87 /// Unlike AddComment, you are required to terminate comments with \n if you
89 virtual raw_ostream &GetCommentOS();
91 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
92 virtual void AddBlankLine() {}
96 /// @name Symbol & Section Management
99 /// getCurrentSection - Return the current section that the streamer is
100 /// emitting code to.
101 const MCSection *getCurrentSection() const { return CurSection; }
103 /// getPreviousSection - Return the previous section that the streamer is
104 /// emitting code to.
105 const MCSection *getPreviousSection() const { return PrevSection; }
107 /// SwitchSection - Set the current section where code is being emitted to
108 /// @p Section. This is required to update CurSection.
110 /// This corresponds to assembler directives like .section, .text, etc.
111 virtual void SwitchSection(const MCSection *Section) = 0;
113 /// EmitLabel - Emit a label for @p Symbol into the current section.
115 /// This corresponds to an assembler statement such as:
118 /// @param Symbol - The symbol to emit. A given symbol should only be
119 /// emitted as a label once, and symbols emitted as a label should never be
120 /// used in an assignment.
121 virtual void EmitLabel(MCSymbol *Symbol) = 0;
123 /// EmitAssemblerFlag - Note in the output the specified @p Flag
124 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0;
126 /// EmitAssignment - Emit an assignment of @p Value to @p Symbol.
128 /// This corresponds to an assembler statement such as:
131 /// The assignment generates no code, but has the side effect of binding the
132 /// value in the current context. For the assembly streamer, this prints the
133 /// binding into the .s file.
135 /// @param Symbol - The symbol being assigned to.
136 /// @param Value - The value for the symbol.
137 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
139 /// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol.
140 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
141 MCSymbolAttr Attribute) = 0;
143 /// EmitSymbolDesc - Set the @p DescValue for the @p Symbol.
145 /// @param Symbol - The symbol to have its n_desc field set.
146 /// @param DescValue - The value to set into the n_desc field.
147 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
149 /// BeginCOFFSymbolDef - Start emitting COFF symbol definition
151 /// @param Symbol - The symbol to have its External & Type fields set.
152 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) = 0;
154 /// EmitCOFFSymbolStorageClass - Emit the storage class of the symbol.
156 /// @param StorageClass - The storage class the symbol should have.
157 virtual void EmitCOFFSymbolStorageClass(int StorageClass) = 0;
159 /// EmitCOFFSymbolType - Emit the type of the symbol.
161 /// @param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
162 virtual void EmitCOFFSymbolType(int Type) = 0;
164 /// EndCOFFSymbolDef - Marks the end of the symbol definition.
165 virtual void EndCOFFSymbolDef() = 0;
167 /// EmitELFSize - Emit an ELF .size directive.
169 /// This corresponds to an assembler statement such as:
170 /// .size symbol, expression
172 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0;
174 /// EmitCommonSymbol - Emit a common symbol.
176 /// @param Symbol - The common symbol to emit.
177 /// @param Size - The size of the common symbol.
178 /// @param ByteAlignment - The alignment of the symbol if
179 /// non-zero. This must be a power of 2.
180 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
181 unsigned ByteAlignment) = 0;
183 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
185 /// @param Symbol - The common symbol to emit.
186 /// @param Size - The size of the common symbol.
187 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) = 0;
189 /// EmitZerofill - Emit the zerofill section and an optional symbol.
191 /// @param Section - The zerofill section to create and or to put the symbol
192 /// @param Symbol - The zerofill symbol to emit, if non-NULL.
193 /// @param Size - The size of the zerofill symbol.
194 /// @param ByteAlignment - The alignment of the zerofill symbol if
195 /// non-zero. This must be a power of 2 on some targets.
196 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
197 unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
199 /// EmitTBSSSymbol - Emit a thread local bss (.tbss) symbol.
201 /// @param Section - The thread local common section.
202 /// @param Symbol - The thread local common symbol to emit.
203 /// @param Size - The size of the symbol.
204 /// @param ByteAlignment - The alignment of the thread local common symbol
205 /// if non-zero. This must be a power of 2 on some targets.
206 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
207 uint64_t Size, unsigned ByteAlignment = 0) = 0;
209 /// @name Generating Data
212 /// EmitBytes - Emit the bytes in \arg Data into the output.
214 /// This is used to implement assembler directives such as .byte, .ascii,
216 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
218 /// EmitValue - Emit the expression @p Value into the output as a native
219 /// integer of the given @p Size bytes.
221 /// This is used to implement assembler directives such as .word, .quad,
224 /// @param Value - The value to emit.
225 /// @param Size - The size of the integer (in bytes) to emit. This must
226 /// match a native machine width.
227 virtual void EmitValue(const MCExpr *Value, unsigned Size,
228 unsigned AddrSpace = 0) = 0;
230 /// EmitIntValue - Special case of EmitValue that avoids the client having
231 /// to pass in a MCExpr for constant integers.
232 virtual void EmitIntValue(uint64_t Value, unsigned Size,
233 unsigned AddrSpace = 0);
235 /// EmitSymbolValue - Special case of EmitValue that avoids the client
236 /// having to pass in a MCExpr for MCSymbols.
237 virtual void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
240 /// EmitGPRel32Value - Emit the expression @p Value into the output as a
241 /// gprel32 (32-bit GP relative) value.
243 /// This is used to implement assembler directives such as .gprel32 on
244 /// targets that support them.
245 virtual void EmitGPRel32Value(const MCExpr *Value) = 0;
247 /// EmitFill - Emit NumBytes bytes worth of the value specified by
248 /// FillValue. This implements directives such as '.space'.
249 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
252 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience
253 /// function that just wraps EmitFill.
254 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) {
255 EmitFill(NumBytes, 0, AddrSpace);
259 /// EmitValueToAlignment - Emit some number of copies of @p Value until
260 /// the byte alignment @p ByteAlignment is reached.
262 /// If the number of bytes need to emit for the alignment is not a multiple
263 /// of @p ValueSize, then the contents of the emitted fill bytes is
266 /// This used to implement the .align assembler directive.
268 /// @param ByteAlignment - The alignment to reach. This must be a power of
269 /// two on some targets.
270 /// @param Value - The value to use when filling bytes.
271 /// @param ValueSize - The size of the integer (in bytes) to emit for
272 /// @p Value. This must match a native machine width.
273 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
274 /// the alignment cannot be reached in this many bytes, no bytes are
276 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
277 unsigned ValueSize = 1,
278 unsigned MaxBytesToEmit = 0) = 0;
280 /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment
283 /// This used to align code where the alignment bytes may be executed. This
284 /// can emit different bytes for different sizes to optimize execution.
286 /// @param ByteAlignment - The alignment to reach. This must be a power of
287 /// two on some targets.
288 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
289 /// the alignment cannot be reached in this many bytes, no bytes are
291 virtual void EmitCodeAlignment(unsigned ByteAlignment,
292 unsigned MaxBytesToEmit = 0) = 0;
294 /// EmitValueToOffset - Emit some number of copies of @p Value until the
295 /// byte offset @p Offset is reached.
297 /// This is used to implement assembler directives such as .org.
299 /// @param Offset - The offset to reach. This may be an expression, but the
300 /// expression must be associated with the current section.
301 /// @param Value - The value to use when filling bytes.
302 virtual void EmitValueToOffset(const MCExpr *Offset,
303 unsigned char Value = 0) = 0;
307 /// EmitFileDirective - Switch to a new logical file. This is used to
308 /// implement the '.file "foo.c"' assembler directive.
309 virtual void EmitFileDirective(StringRef Filename) = 0;
311 /// EmitDwarfFileDirective - Associate a filename with a specified logical
312 /// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler
314 virtual void EmitDwarfFileDirective(unsigned FileNo,StringRef Filename) = 0;
316 /// EmitInstruction - Emit the given @p Instruction into the current
318 virtual void EmitInstruction(const MCInst &Inst) = 0;
320 /// EmitRawText - If this file is backed by a assembly streamer, this dumps
321 /// the specified string in the output .s file. This capability is
322 /// indicated by the hasRawTextSupport() predicate. By default this aborts.
323 virtual void EmitRawText(StringRef String);
324 void EmitRawText(const Twine &String);
326 /// Finish - Finish emission of machine code.
327 virtual void Finish() = 0;
330 /// createNullStreamer - Create a dummy machine code streamer, which does
331 /// nothing. This is useful for timing the assembler front end.
332 MCStreamer *createNullStreamer(MCContext &Ctx);
334 /// createAsmStreamer - Create a machine code streamer which will print out
335 /// assembly for the native target, suitable for compiling with a native
338 /// \param InstPrint - If given, the instruction printer to use. If not given
339 /// the MCInst representation will be printed. This method takes ownership of
342 /// \param CE - If given, a code emitter to use to show the instruction
343 /// encoding inline with the assembly. This method takes ownership of \arg CE.
345 /// \param ShowInst - Whether to show the MCInst representation inline with
347 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
348 bool isLittleEndian, bool isVerboseAsm,
349 MCInstPrinter *InstPrint = 0,
350 MCCodeEmitter *CE = 0,
351 bool ShowInst = false);
353 /// createMachOStreamer - Create a machine code streamer which will generate
354 /// Mach-O format object files.
356 /// Takes ownership of \arg TAB and \arg CE.
357 MCStreamer *createMachOStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
358 raw_ostream &OS, MCCodeEmitter *CE,
359 bool RelaxAll = false);
361 /// createWinCOFFStreamer - Create a machine code streamer which will
362 /// generate Microsoft COFF format object files.
364 /// Takes ownership of \arg TAB and \arg CE.
365 MCStreamer *createWinCOFFStreamer(MCContext &Ctx,
366 TargetAsmBackend &TAB,
367 MCCodeEmitter &CE, raw_ostream &OS,
368 bool RelaxAll = false);
370 /// createELFStreamer - Create a machine code streamer which will generate
371 /// ELF format object files.
372 MCStreamer *createELFStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
373 raw_ostream &OS, MCCodeEmitter *CE,
374 bool RelaxAll = false);
376 /// createLoggingStreamer - Create a machine code streamer which just logs the
377 /// API calls and then dispatches to another streamer.
379 /// The new streamer takes ownership of the \arg Child.
380 MCStreamer *createLoggingStreamer(MCStreamer *Child, raw_ostream &OS);
382 } // end namespace llvm