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
58 /// is 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 /// InitSections - Create the default sections and set the initial one.
114 virtual void InitSections() = 0;
116 /// EmitLabel - Emit a label for @p Symbol into the current section.
118 /// This corresponds to an assembler statement such as:
121 /// @param Symbol - The symbol to emit. A given symbol should only be
122 /// emitted as a label once, and symbols emitted as a label should never be
123 /// used in an assignment.
124 virtual void EmitLabel(MCSymbol *Symbol) = 0;
126 /// EmitAssemblerFlag - Note in the output the specified @p Flag
127 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0;
129 /// EmitThumbFunc - Note in the output that the specified @p Func is
130 /// a Thumb mode function (ARM target only).
131 virtual void EmitThumbFunc(MCSymbol *Func) = 0;
133 /// EmitAssignment - Emit an assignment of @p Value to @p Symbol.
135 /// This corresponds to an assembler statement such as:
138 /// The assignment generates no code, but has the side effect of binding the
139 /// value in the current context. For the assembly streamer, this prints the
140 /// binding into the .s file.
142 /// @param Symbol - The symbol being assigned to.
143 /// @param Value - The value for the symbol.
144 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
146 /// EmitWeakReference - Emit an weak reference from @p Alias to @p Symbol.
148 /// This corresponds to an assembler statement such as:
149 /// .weakref alias, symbol
151 /// @param Alias - The alias that is being created.
152 /// @param Symbol - The symbol being aliased.
153 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) = 0;
155 /// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol.
156 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
157 MCSymbolAttr Attribute) = 0;
159 /// EmitSymbolDesc - Set the @p DescValue for the @p Symbol.
161 /// @param Symbol - The symbol to have its n_desc field set.
162 /// @param DescValue - The value to set into the n_desc field.
163 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
165 /// BeginCOFFSymbolDef - Start emitting COFF symbol definition
167 /// @param Symbol - The symbol to have its External & Type fields set.
168 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) = 0;
170 /// EmitCOFFSymbolStorageClass - Emit the storage class of the symbol.
172 /// @param StorageClass - The storage class the symbol should have.
173 virtual void EmitCOFFSymbolStorageClass(int StorageClass) = 0;
175 /// EmitCOFFSymbolType - Emit the type of the symbol.
177 /// @param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
178 virtual void EmitCOFFSymbolType(int Type) = 0;
180 /// EndCOFFSymbolDef - Marks the end of the symbol definition.
181 virtual void EndCOFFSymbolDef() = 0;
183 /// EmitELFSize - Emit an ELF .size directive.
185 /// This corresponds to an assembler statement such as:
186 /// .size symbol, expression
188 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0;
190 /// EmitCommonSymbol - Emit a common symbol.
192 /// @param Symbol - The common symbol to emit.
193 /// @param Size - The size of the common symbol.
194 /// @param ByteAlignment - The alignment of the symbol if
195 /// non-zero. This must be a power of 2.
196 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
197 unsigned ByteAlignment) = 0;
199 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
201 /// @param Symbol - The common symbol to emit.
202 /// @param Size - The size of the common symbol.
203 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) = 0;
205 /// EmitZerofill - Emit the zerofill section and an optional symbol.
207 /// @param Section - The zerofill section to create and or to put the symbol
208 /// @param Symbol - The zerofill symbol to emit, if non-NULL.
209 /// @param Size - The size of the zerofill symbol.
210 /// @param ByteAlignment - The alignment of the zerofill symbol if
211 /// non-zero. This must be a power of 2 on some targets.
212 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
213 unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
215 /// EmitTBSSSymbol - Emit a thread local bss (.tbss) symbol.
217 /// @param Section - The thread local common section.
218 /// @param Symbol - The thread local common symbol to emit.
219 /// @param Size - The size of the symbol.
220 /// @param ByteAlignment - The alignment of the thread local common symbol
221 /// if non-zero. This must be a power of 2 on some targets.
222 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
223 uint64_t Size, unsigned ByteAlignment = 0) = 0;
225 /// @name Generating Data
228 /// EmitBytes - Emit the bytes in \arg Data into the output.
230 /// This is used to implement assembler directives such as .byte, .ascii,
232 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
234 /// EmitValue - Emit the expression @p Value into the output as a native
235 /// integer of the given @p Size bytes.
237 /// This is used to implement assembler directives such as .word, .quad,
240 /// @param Value - The value to emit.
241 /// @param Size - The size of the integer (in bytes) to emit. This must
242 /// match a native machine width.
243 virtual void EmitValue(const MCExpr *Value, unsigned Size,
244 unsigned AddrSpace = 0) = 0;
246 /// EmitIntValue - Special case of EmitValue that avoids the client having
247 /// to pass in a MCExpr for constant integers.
248 virtual void EmitIntValue(uint64_t Value, unsigned Size,
249 unsigned AddrSpace = 0);
252 virtual void EmitULEB128Value(const MCExpr *Value,
253 unsigned AddrSpace = 0) = 0;
255 virtual void EmitSLEB128Value(const MCExpr *Value,
256 unsigned AddrSpace = 0) = 0;
258 /// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
259 /// client having to pass in a MCExpr for constant integers.
260 virtual void EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace = 0);
262 /// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
263 /// client having to pass in a MCExpr for constant integers.
264 virtual void EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace = 0);
266 /// EmitSymbolValue - Special case of EmitValue that avoids the client
267 /// having to pass in a MCExpr for MCSymbols.
268 virtual void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
269 unsigned AddrSpace = 0);
271 /// EmitGPRel32Value - Emit the expression @p Value into the output as a
272 /// gprel32 (32-bit GP relative) value.
274 /// This is used to implement assembler directives such as .gprel32 on
275 /// targets that support them.
276 virtual void EmitGPRel32Value(const MCExpr *Value) = 0;
278 /// EmitFill - Emit NumBytes bytes worth of the value specified by
279 /// FillValue. This implements directives such as '.space'.
280 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
283 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience
284 /// function that just wraps EmitFill.
285 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) {
286 EmitFill(NumBytes, 0, AddrSpace);
290 /// EmitValueToAlignment - Emit some number of copies of @p Value until
291 /// the byte alignment @p ByteAlignment is reached.
293 /// If the number of bytes need to emit for the alignment is not a multiple
294 /// of @p ValueSize, then the contents of the emitted fill bytes is
297 /// This used to implement the .align assembler directive.
299 /// @param ByteAlignment - The alignment to reach. This must be a power of
300 /// two on some targets.
301 /// @param Value - The value to use when filling bytes.
302 /// @param ValueSize - The size of the integer (in bytes) to emit for
303 /// @p Value. This must match a native machine width.
304 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
305 /// the alignment cannot be reached in this many bytes, no bytes are
307 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
308 unsigned ValueSize = 1,
309 unsigned MaxBytesToEmit = 0) = 0;
311 /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment
314 /// This used to align code where the alignment bytes may be executed. This
315 /// can emit different bytes for different sizes to optimize execution.
317 /// @param ByteAlignment - The alignment to reach. This must be a power of
318 /// two on some targets.
319 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
320 /// the alignment cannot be reached in this many bytes, no bytes are
322 virtual void EmitCodeAlignment(unsigned ByteAlignment,
323 unsigned MaxBytesToEmit = 0) = 0;
325 /// EmitValueToOffset - Emit some number of copies of @p Value until the
326 /// byte offset @p Offset is reached.
328 /// This is used to implement assembler directives such as .org.
330 /// @param Offset - The offset to reach. This may be an expression, but the
331 /// expression must be associated with the current section.
332 /// @param Value - The value to use when filling bytes.
333 virtual void EmitValueToOffset(const MCExpr *Offset,
334 unsigned char Value = 0) = 0;
338 /// EmitFileDirective - Switch to a new logical file. This is used to
339 /// implement the '.file "foo.c"' assembler directive.
340 virtual void EmitFileDirective(StringRef Filename) = 0;
342 /// EmitDwarfFileDirective - Associate a filename with a specified logical
343 /// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler
345 virtual void EmitDwarfFileDirective(unsigned FileNo,StringRef Filename) = 0;
347 /// EmitInstruction - Emit the given @p Instruction into the current
349 virtual void EmitInstruction(const MCInst &Inst) = 0;
351 /// EmitRawText - If this file is backed by a assembly streamer, this dumps
352 /// the specified string in the output .s file. This capability is
353 /// indicated by the hasRawTextSupport() predicate. By default this aborts.
354 virtual void EmitRawText(StringRef String);
355 void EmitRawText(const Twine &String);
357 /// Finish - Finish emission of machine code.
358 virtual void Finish() = 0;
361 /// createNullStreamer - Create a dummy machine code streamer, which does
362 /// nothing. This is useful for timing the assembler front end.
363 MCStreamer *createNullStreamer(MCContext &Ctx);
365 /// createAsmStreamer - Create a machine code streamer which will print out
366 /// assembly for the native target, suitable for compiling with a native
369 /// \param InstPrint - If given, the instruction printer to use. If not given
370 /// the MCInst representation will be printed. This method takes ownership of
373 /// \param CE - If given, a code emitter to use to show the instruction
374 /// encoding inline with the assembly. This method takes ownership of \arg CE.
376 /// \param ShowInst - Whether to show the MCInst representation inline with
378 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
379 bool isLittleEndian, bool isVerboseAsm,
380 MCInstPrinter *InstPrint = 0,
381 MCCodeEmitter *CE = 0,
382 bool ShowInst = false);
384 /// createMachOStreamer - Create a machine code streamer which will generate
385 /// Mach-O format object files.
387 /// Takes ownership of \arg TAB and \arg CE.
388 MCStreamer *createMachOStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
389 raw_ostream &OS, MCCodeEmitter *CE,
390 bool RelaxAll = false);
392 /// createWinCOFFStreamer - Create a machine code streamer which will
393 /// generate Microsoft COFF format object files.
395 /// Takes ownership of \arg TAB and \arg CE.
396 MCStreamer *createWinCOFFStreamer(MCContext &Ctx,
397 TargetAsmBackend &TAB,
398 MCCodeEmitter &CE, raw_ostream &OS,
399 bool RelaxAll = false);
401 /// createELFStreamer - Create a machine code streamer which will generate
402 /// ELF format object files.
403 MCStreamer *createELFStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
404 raw_ostream &OS, MCCodeEmitter *CE,
405 bool RelaxAll = false);
407 /// createLoggingStreamer - Create a machine code streamer which just logs the
408 /// API calls and then dispatches to another streamer.
410 /// The new streamer takes ownership of the \arg Child.
411 MCStreamer *createLoggingStreamer(MCStreamer *Child, raw_ostream &OS);
413 } // end namespace llvm