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;
31 class TargetLoweringObjectFile;
34 class formatted_raw_ostream;
36 /// MCStreamer - Streaming machine code generation interface. This interface
37 /// is intended to provide a programatic interface that is very similar to the
38 /// level that an assembler .s file provides. It has callbacks to emit bytes,
39 /// handle directives, etc. The implementation of this interface retains
40 /// state to know what the current section is etc.
42 /// There are multiple implementations of this interface: one for writing out
43 /// a .s file, and implementations that write out .o files of various formats.
48 MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT
49 MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT
52 MCStreamer(MCContext &Ctx);
54 /// CurSection - This is the current section code is being emitted to, it is
55 /// kept up to date by SwitchSection.
56 const MCSection *CurSection;
58 /// PrevSection - This is the previous section code is being emitted to, it
59 /// is kept up to date by SwitchSection.
60 const MCSection *PrevSection;
63 virtual ~MCStreamer();
65 MCContext &getContext() const { return Context; }
67 /// @name Assembly File Formatting.
70 /// isVerboseAsm - Return true if this streamer supports verbose assembly
71 /// and if it is enabled.
72 virtual bool isVerboseAsm() const { return false; }
74 /// hasRawTextSupport - Return true if this asm streamer supports emitting
75 /// unformatted text to the .s file with EmitRawText.
76 virtual bool hasRawTextSupport() const { return false; }
78 /// AddComment - Add a comment that can be emitted to the generated .s
79 /// file if applicable as a QoI issue to make the output of the compiler
80 /// more readable. This only affects the MCAsmStreamer, and only when
81 /// verbose assembly output is enabled.
83 /// If the comment includes embedded \n's, they will each get the comment
84 /// prefix as appropriate. The added comment should not end with a \n.
85 virtual void AddComment(const Twine &T) {}
87 /// GetCommentOS - Return a raw_ostream that comments can be written to.
88 /// Unlike AddComment, you are required to terminate comments with \n if you
90 virtual raw_ostream &GetCommentOS();
92 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
93 virtual void AddBlankLine() {}
97 /// @name Symbol & Section Management
100 /// getCurrentSection - Return the current section that the streamer is
101 /// emitting code to.
102 const MCSection *getCurrentSection() const { return CurSection; }
104 /// getPreviousSection - Return the previous section that the streamer is
105 /// emitting code to.
106 const MCSection *getPreviousSection() const { return PrevSection; }
108 /// SwitchSection - Set the current section where code is being emitted to
109 /// @p Section. This is required to update CurSection.
111 /// This corresponds to assembler directives like .section, .text, etc.
112 virtual void SwitchSection(const MCSection *Section) = 0;
114 /// InitSections - Create the default sections and set the initial one.
115 virtual void InitSections() = 0;
117 /// EmitLabel - Emit a label for @p Symbol into the current section.
119 /// This corresponds to an assembler statement such as:
122 /// @param Symbol - The symbol to emit. A given symbol should only be
123 /// emitted as a label once, and symbols emitted as a label should never be
124 /// used in an assignment.
125 virtual void EmitLabel(MCSymbol *Symbol) = 0;
127 /// EmitAssemblerFlag - Note in the output the specified @p Flag
128 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0;
130 /// EmitThumbFunc - Note in the output that the specified @p Func is
131 /// a Thumb mode function (ARM target only).
132 virtual void EmitThumbFunc(MCSymbol *Func) = 0;
134 /// EmitAssignment - Emit an assignment of @p Value to @p Symbol.
136 /// This corresponds to an assembler statement such as:
139 /// The assignment generates no code, but has the side effect of binding the
140 /// value in the current context. For the assembly streamer, this prints the
141 /// binding into the .s file.
143 /// @param Symbol - The symbol being assigned to.
144 /// @param Value - The value for the symbol.
145 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
147 /// EmitWeakReference - Emit an weak reference from @p Alias to @p Symbol.
149 /// This corresponds to an assembler statement such as:
150 /// .weakref alias, symbol
152 /// @param Alias - The alias that is being created.
153 /// @param Symbol - The symbol being aliased.
154 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) = 0;
156 /// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol.
157 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
158 MCSymbolAttr Attribute) = 0;
160 /// EmitSymbolDesc - Set the @p DescValue for the @p Symbol.
162 /// @param Symbol - The symbol to have its n_desc field set.
163 /// @param DescValue - The value to set into the n_desc field.
164 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
166 /// BeginCOFFSymbolDef - Start emitting COFF symbol definition
168 /// @param Symbol - The symbol to have its External & Type fields set.
169 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) = 0;
171 /// EmitCOFFSymbolStorageClass - Emit the storage class of the symbol.
173 /// @param StorageClass - The storage class the symbol should have.
174 virtual void EmitCOFFSymbolStorageClass(int StorageClass) = 0;
176 /// EmitCOFFSymbolType - Emit the type of the symbol.
178 /// @param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
179 virtual void EmitCOFFSymbolType(int Type) = 0;
181 /// EndCOFFSymbolDef - Marks the end of the symbol definition.
182 virtual void EndCOFFSymbolDef() = 0;
184 /// EmitELFSize - Emit an ELF .size directive.
186 /// This corresponds to an assembler statement such as:
187 /// .size symbol, expression
189 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0;
191 /// EmitCommonSymbol - Emit a common symbol.
193 /// @param Symbol - The common symbol to emit.
194 /// @param Size - The size of the common symbol.
195 /// @param ByteAlignment - The alignment of the symbol if
196 /// non-zero. This must be a power of 2.
197 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
198 unsigned ByteAlignment) = 0;
200 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
202 /// @param Symbol - The common symbol to emit.
203 /// @param Size - The size of the common symbol.
204 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) = 0;
206 /// EmitZerofill - Emit the zerofill section and an optional symbol.
208 /// @param Section - The zerofill section to create and or to put the symbol
209 /// @param Symbol - The zerofill symbol to emit, if non-NULL.
210 /// @param Size - The size of the zerofill symbol.
211 /// @param ByteAlignment - The alignment of the zerofill symbol if
212 /// non-zero. This must be a power of 2 on some targets.
213 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
214 unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
216 /// EmitTBSSSymbol - Emit a thread local bss (.tbss) symbol.
218 /// @param Section - The thread local common section.
219 /// @param Symbol - The thread local common symbol to emit.
220 /// @param Size - The size of the symbol.
221 /// @param ByteAlignment - The alignment of the thread local common symbol
222 /// if non-zero. This must be a power of 2 on some targets.
223 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
224 uint64_t Size, unsigned ByteAlignment = 0) = 0;
226 /// @name Generating Data
229 /// EmitBytes - Emit the bytes in \arg Data into the output.
231 /// This is used to implement assembler directives such as .byte, .ascii,
233 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
235 /// EmitValue - Emit the expression @p Value into the output as a native
236 /// integer of the given @p Size bytes.
238 /// This is used to implement assembler directives such as .word, .quad,
241 /// @param Value - The value to emit.
242 /// @param Size - The size of the integer (in bytes) to emit. This must
243 /// match a native machine width.
244 virtual void EmitValue(const MCExpr *Value, unsigned Size,
245 unsigned AddrSpace = 0) = 0;
247 /// EmitIntValue - Special case of EmitValue that avoids the client having
248 /// to pass in a MCExpr for constant integers.
249 virtual void EmitIntValue(uint64_t Value, unsigned Size,
250 unsigned AddrSpace = 0);
253 virtual void EmitULEB128Value(const MCExpr *Value,
254 unsigned AddrSpace = 0) = 0;
256 virtual void EmitSLEB128Value(const MCExpr *Value,
257 unsigned AddrSpace = 0) = 0;
259 /// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
260 /// client having to pass in a MCExpr for constant integers.
261 virtual void EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace = 0);
263 /// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
264 /// client having to pass in a MCExpr for constant integers.
265 virtual void EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace = 0);
267 /// EmitSymbolValue - Special case of EmitValue that avoids the client
268 /// having to pass in a MCExpr for MCSymbols.
269 virtual void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
270 unsigned AddrSpace = 0);
272 /// EmitGPRel32Value - Emit the expression @p Value into the output as a
273 /// gprel32 (32-bit GP relative) value.
275 /// This is used to implement assembler directives such as .gprel32 on
276 /// targets that support them.
277 virtual void EmitGPRel32Value(const MCExpr *Value) = 0;
279 /// EmitFill - Emit NumBytes bytes worth of the value specified by
280 /// FillValue. This implements directives such as '.space'.
281 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
284 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience
285 /// function that just wraps EmitFill.
286 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) {
287 EmitFill(NumBytes, 0, AddrSpace);
291 /// EmitValueToAlignment - Emit some number of copies of @p Value until
292 /// the byte alignment @p ByteAlignment is reached.
294 /// If the number of bytes need to emit for the alignment is not a multiple
295 /// of @p ValueSize, then the contents of the emitted fill bytes is
298 /// This used to implement the .align assembler directive.
300 /// @param ByteAlignment - The alignment to reach. This must be a power of
301 /// two on some targets.
302 /// @param Value - The value to use when filling bytes.
303 /// @param ValueSize - The size of the integer (in bytes) to emit for
304 /// @p Value. This must match a native machine width.
305 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
306 /// the alignment cannot be reached in this many bytes, no bytes are
308 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
309 unsigned ValueSize = 1,
310 unsigned MaxBytesToEmit = 0) = 0;
312 /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment
315 /// This used to align code where the alignment bytes may be executed. This
316 /// can emit different bytes for different sizes to optimize execution.
318 /// @param ByteAlignment - The alignment to reach. This must be a power of
319 /// two on some targets.
320 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
321 /// the alignment cannot be reached in this many bytes, no bytes are
323 virtual void EmitCodeAlignment(unsigned ByteAlignment,
324 unsigned MaxBytesToEmit = 0) = 0;
326 /// EmitValueToOffset - Emit some number of copies of @p Value until the
327 /// byte offset @p Offset is reached.
329 /// This is used to implement assembler directives such as .org.
331 /// @param Offset - The offset to reach. This may be an expression, but the
332 /// expression must be associated with the current section.
333 /// @param Value - The value to use when filling bytes.
334 virtual void EmitValueToOffset(const MCExpr *Offset,
335 unsigned char Value = 0) = 0;
339 /// EmitFileDirective - Switch to a new logical file. This is used to
340 /// implement the '.file "foo.c"' assembler directive.
341 virtual void EmitFileDirective(StringRef Filename) = 0;
343 /// EmitDwarfFileDirective - Associate a filename with a specified logical
344 /// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler
346 virtual bool EmitDwarfFileDirective(unsigned FileNo,StringRef Filename);
348 /// EmitDwarfLocDirective - This implements the DWARF2
349 // '.loc fileno lineno ...' assembler directive.
350 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
351 unsigned Column, unsigned Flags,
353 unsigned Discriminator);
355 virtual bool EmitCFIStartProc();
356 virtual bool EmitCFIEndProc();
357 virtual bool EmitCFIDefCfaOffset(int64_t Offset);
358 virtual bool EmitCFIDefCfaRegister(int64_t Register);
359 virtual bool EmitCFIOffset(int64_t Register, int64_t Offset);
360 virtual bool EmitCFIPersonality(const MCSymbol *Sym);
361 virtual bool EmitCFILsda(const MCSymbol *Sym);
363 /// EmitInstruction - Emit the given @p Instruction into the current
365 virtual void EmitInstruction(const MCInst &Inst) = 0;
367 /// EmitRawText - If this file is backed by a assembly streamer, this dumps
368 /// the specified string in the output .s file. This capability is
369 /// indicated by the hasRawTextSupport() predicate. By default this aborts.
370 virtual void EmitRawText(StringRef String);
371 void EmitRawText(const Twine &String);
373 /// Finish - Finish emission of machine code.
374 virtual void Finish() = 0;
377 /// createNullStreamer - Create a dummy machine code streamer, which does
378 /// nothing. This is useful for timing the assembler front end.
379 MCStreamer *createNullStreamer(MCContext &Ctx);
381 /// createAsmStreamer - Create a machine code streamer which will print out
382 /// assembly for the native target, suitable for compiling with a native
385 /// \param InstPrint - If given, the instruction printer to use. If not given
386 /// the MCInst representation will be printed. This method takes ownership of
389 /// \param CE - If given, a code emitter to use to show the instruction
390 /// encoding inline with the assembly. This method takes ownership of \arg CE.
392 /// \param ShowInst - Whether to show the MCInst representation inline with
394 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
395 bool isLittleEndian, bool isVerboseAsm,
396 MCInstPrinter *InstPrint = 0,
397 MCCodeEmitter *CE = 0,
398 bool ShowInst = false);
400 MCStreamer *createAsmStreamerNoLoc(MCContext &Ctx, formatted_raw_ostream &OS,
401 bool isLittleEndian, bool isVerboseAsm,
402 const TargetLoweringObjectFile *TLOF,
404 MCInstPrinter *InstPrint = 0,
405 MCCodeEmitter *CE = 0,
406 bool ShowInst = false);
408 /// createMachOStreamer - Create a machine code streamer which will generate
409 /// Mach-O format object files.
411 /// Takes ownership of \arg TAB and \arg CE.
412 MCStreamer *createMachOStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
413 raw_ostream &OS, MCCodeEmitter *CE,
414 bool RelaxAll = false);
416 /// createWinCOFFStreamer - Create a machine code streamer which will
417 /// generate Microsoft COFF format object files.
419 /// Takes ownership of \arg TAB and \arg CE.
420 MCStreamer *createWinCOFFStreamer(MCContext &Ctx,
421 TargetAsmBackend &TAB,
422 MCCodeEmitter &CE, raw_ostream &OS,
423 bool RelaxAll = false);
425 /// createELFStreamer - Create a machine code streamer which will generate
426 /// ELF format object files.
427 MCStreamer *createELFStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
428 raw_ostream &OS, MCCodeEmitter *CE,
429 bool RelaxAll = false);
431 /// createLoggingStreamer - Create a machine code streamer which just logs the
432 /// API calls and then dispatches to another streamer.
434 /// The new streamer takes ownership of the \arg Child.
435 MCStreamer *createLoggingStreamer(MCStreamer *Child, raw_ostream &OS);
437 /// createPureStreamer - Create a machine code streamer which will generate
438 /// "pure" MC object files, for use with MC-JIT and testing tools.
440 /// Takes ownership of \arg TAB and \arg CE.
441 MCStreamer *createPureStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
442 raw_ostream &OS, MCCodeEmitter *CE);
444 } // end namespace llvm