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/Support/DataTypes.h"
18 #include "llvm/MC/MCDirectives.h"
19 #include "llvm/MC/MCDwarf.h"
31 class TargetAsmBackend;
32 class TargetLoweringObjectFile;
35 class formatted_raw_ostream;
37 /// MCStreamer - Streaming machine code generation interface. This interface
38 /// is intended to provide a programatic interface that is very similar to the
39 /// level that an assembler .s file provides. It has callbacks to emit bytes,
40 /// handle directives, etc. The implementation of this interface retains
41 /// state to know what the current section is etc.
43 /// There are multiple implementations of this interface: one for writing out
44 /// a .s file, and implementations that write out .o files of various formats.
49 MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT
50 MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT
52 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
53 bool isPCRel, unsigned AddrSpace);
55 std::vector<MCDwarfFrameInfo> FrameInfos;
56 MCDwarfFrameInfo *getCurrentFrameInfo();
57 void EnsureValidFrame();
60 MCStreamer(MCContext &Ctx);
62 /// CurSection - This is the current section code is being emitted to, it is
63 /// kept up to date by SwitchSection.
64 const MCSection *CurSection;
66 /// PrevSection - This is the previous section code is being emitted to, it
67 /// is kept up to date by SwitchSection.
68 const MCSection *PrevSection;
71 virtual ~MCStreamer();
73 MCContext &getContext() const { return Context; }
75 unsigned getNumFrameInfos() {
76 return FrameInfos.size();
79 const MCDwarfFrameInfo &getFrameInfo(unsigned i) {
83 /// @name Assembly File Formatting.
86 /// isVerboseAsm - Return true if this streamer supports verbose assembly
87 /// and if it is enabled.
88 virtual bool isVerboseAsm() const { return false; }
90 /// hasRawTextSupport - Return true if this asm streamer supports emitting
91 /// unformatted text to the .s file with EmitRawText.
92 virtual bool hasRawTextSupport() const { return false; }
94 /// AddComment - Add a comment that can be emitted to the generated .s
95 /// file if applicable as a QoI issue to make the output of the compiler
96 /// more readable. This only affects the MCAsmStreamer, and only when
97 /// verbose assembly output is enabled.
99 /// If the comment includes embedded \n's, they will each get the comment
100 /// prefix as appropriate. The added comment should not end with a \n.
101 virtual void AddComment(const Twine &T) {}
103 /// GetCommentOS - Return a raw_ostream that comments can be written to.
104 /// Unlike AddComment, you are required to terminate comments with \n if you
106 virtual raw_ostream &GetCommentOS();
108 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
109 virtual void AddBlankLine() {}
113 /// @name Symbol & Section Management
116 /// getCurrentSection - Return the current section that the streamer is
117 /// emitting code to.
118 const MCSection *getCurrentSection() const { return CurSection; }
120 /// getPreviousSection - Return the previous section that the streamer is
121 /// emitting code to.
122 const MCSection *getPreviousSection() const { return PrevSection; }
124 /// SwitchSection - Set the current section where code is being emitted to
125 /// @p Section. This is required to update CurSection.
127 /// This corresponds to assembler directives like .section, .text, etc.
128 virtual void SwitchSection(const MCSection *Section) = 0;
130 /// InitSections - Create the default sections and set the initial one.
131 virtual void InitSections() = 0;
133 /// EmitLabel - Emit a label for @p Symbol into the current section.
135 /// This corresponds to an assembler statement such as:
138 /// @param Symbol - The symbol to emit. A given symbol should only be
139 /// emitted as a label once, and symbols emitted as a label should never be
140 /// used in an assignment.
141 virtual void EmitLabel(MCSymbol *Symbol) = 0;
143 /// EmitAssemblerFlag - Note in the output the specified @p Flag
144 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0;
146 /// EmitThumbFunc - Note in the output that the specified @p Func is
147 /// a Thumb mode function (ARM target only).
148 virtual void EmitThumbFunc(MCSymbol *Func) = 0;
150 /// EmitAssignment - Emit an assignment of @p Value to @p Symbol.
152 /// This corresponds to an assembler statement such as:
155 /// The assignment generates no code, but has the side effect of binding the
156 /// value in the current context. For the assembly streamer, this prints the
157 /// binding into the .s file.
159 /// @param Symbol - The symbol being assigned to.
160 /// @param Value - The value for the symbol.
161 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
163 /// EmitWeakReference - Emit an weak reference from @p Alias to @p Symbol.
165 /// This corresponds to an assembler statement such as:
166 /// .weakref alias, symbol
168 /// @param Alias - The alias that is being created.
169 /// @param Symbol - The symbol being aliased.
170 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) = 0;
172 /// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol.
173 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
174 MCSymbolAttr Attribute) = 0;
176 /// EmitSymbolDesc - Set the @p DescValue for the @p Symbol.
178 /// @param Symbol - The symbol to have its n_desc field set.
179 /// @param DescValue - The value to set into the n_desc field.
180 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
182 /// BeginCOFFSymbolDef - Start emitting COFF symbol definition
184 /// @param Symbol - The symbol to have its External & Type fields set.
185 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) = 0;
187 /// EmitCOFFSymbolStorageClass - Emit the storage class of the symbol.
189 /// @param StorageClass - The storage class the symbol should have.
190 virtual void EmitCOFFSymbolStorageClass(int StorageClass) = 0;
192 /// EmitCOFFSymbolType - Emit the type of the symbol.
194 /// @param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
195 virtual void EmitCOFFSymbolType(int Type) = 0;
197 /// EndCOFFSymbolDef - Marks the end of the symbol definition.
198 virtual void EndCOFFSymbolDef() = 0;
200 /// EmitELFSize - Emit an ELF .size directive.
202 /// This corresponds to an assembler statement such as:
203 /// .size symbol, expression
205 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0;
207 /// EmitCommonSymbol - Emit a common symbol.
209 /// @param Symbol - The common symbol to emit.
210 /// @param Size - The size of the common symbol.
211 /// @param ByteAlignment - The alignment of the symbol if
212 /// non-zero. This must be a power of 2.
213 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
214 unsigned ByteAlignment) = 0;
216 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
218 /// @param Symbol - The common symbol to emit.
219 /// @param Size - The size of the common symbol.
220 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) = 0;
222 /// EmitZerofill - Emit the zerofill section and an optional symbol.
224 /// @param Section - The zerofill section to create and or to put the symbol
225 /// @param Symbol - The zerofill symbol to emit, if non-NULL.
226 /// @param Size - The size of the zerofill symbol.
227 /// @param ByteAlignment - The alignment of the zerofill symbol if
228 /// non-zero. This must be a power of 2 on some targets.
229 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
230 unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
232 /// EmitTBSSSymbol - Emit a thread local bss (.tbss) symbol.
234 /// @param Section - The thread local common section.
235 /// @param Symbol - The thread local common symbol to emit.
236 /// @param Size - The size of the symbol.
237 /// @param ByteAlignment - The alignment of the thread local common symbol
238 /// if non-zero. This must be a power of 2 on some targets.
239 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
240 uint64_t Size, unsigned ByteAlignment = 0) = 0;
242 /// @name Generating Data
245 /// EmitBytes - Emit the bytes in \arg Data into the output.
247 /// This is used to implement assembler directives such as .byte, .ascii,
249 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
251 /// EmitValue - Emit the expression @p Value into the output as a native
252 /// integer of the given @p Size bytes.
254 /// This is used to implement assembler directives such as .word, .quad,
257 /// @param Value - The value to emit.
258 /// @param Size - The size of the integer (in bytes) to emit. This must
259 /// match a native machine width.
260 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
261 bool isPCRel, unsigned AddrSpace) = 0;
263 void EmitValue(const MCExpr *Value, unsigned Size, unsigned AddrSpace = 0);
265 void EmitPCRelValue(const MCExpr *Value, unsigned Size,
266 unsigned AddrSpace = 0);
268 /// EmitIntValue - Special case of EmitValue that avoids the client having
269 /// to pass in a MCExpr for constant integers.
270 virtual void EmitIntValue(uint64_t Value, unsigned Size,
271 unsigned AddrSpace = 0);
273 /// EmitAbsValue - Emit the Value, but try to avoid relocations. On MachO
274 /// this is done by producing
277 void EmitAbsValue(const MCExpr *Value, unsigned Size,
278 unsigned AddrSpace = 0);
280 virtual void EmitULEB128Value(const MCExpr *Value,
281 unsigned AddrSpace = 0) = 0;
283 virtual void EmitSLEB128Value(const MCExpr *Value,
284 unsigned AddrSpace = 0) = 0;
286 /// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
287 /// client having to pass in a MCExpr for constant integers.
288 void EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace = 0);
290 /// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
291 /// client having to pass in a MCExpr for constant integers.
292 void EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace = 0);
294 /// EmitSymbolValue - Special case of EmitValue that avoids the client
295 /// having to pass in a MCExpr for MCSymbols.
296 void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
297 unsigned AddrSpace = 0);
299 void EmitPCRelSymbolValue(const MCSymbol *Sym, unsigned Size,
300 unsigned AddrSpace = 0);
302 /// EmitGPRel32Value - Emit the expression @p Value into the output as a
303 /// gprel32 (32-bit GP relative) value.
305 /// This is used to implement assembler directives such as .gprel32 on
306 /// targets that support them.
307 virtual void EmitGPRel32Value(const MCExpr *Value);
309 /// EmitFill - Emit NumBytes bytes worth of the value specified by
310 /// FillValue. This implements directives such as '.space'.
311 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
314 /// EmitZeros - Emit NumBytes worth of zeros. This is a convenience
315 /// function that just wraps EmitFill.
316 void EmitZeros(uint64_t NumBytes, unsigned AddrSpace) {
317 EmitFill(NumBytes, 0, AddrSpace);
321 /// EmitValueToAlignment - Emit some number of copies of @p Value until
322 /// the byte alignment @p ByteAlignment is reached.
324 /// If the number of bytes need to emit for the alignment is not a multiple
325 /// of @p ValueSize, then the contents of the emitted fill bytes is
328 /// This used to implement the .align assembler directive.
330 /// @param ByteAlignment - The alignment to reach. This must be a power of
331 /// two on some targets.
332 /// @param Value - The value to use when filling bytes.
333 /// @param ValueSize - The size of the integer (in bytes) to emit for
334 /// @p Value. This must match a native machine width.
335 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
336 /// the alignment cannot be reached in this many bytes, no bytes are
338 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
339 unsigned ValueSize = 1,
340 unsigned MaxBytesToEmit = 0) = 0;
342 /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment
345 /// This used to align code where the alignment bytes may be executed. This
346 /// can emit different bytes for different sizes to optimize execution.
348 /// @param ByteAlignment - The alignment to reach. This must be a power of
349 /// two on some targets.
350 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
351 /// the alignment cannot be reached in this many bytes, no bytes are
353 virtual void EmitCodeAlignment(unsigned ByteAlignment,
354 unsigned MaxBytesToEmit = 0) = 0;
356 /// EmitValueToOffset - Emit some number of copies of @p Value until the
357 /// byte offset @p Offset is reached.
359 /// This is used to implement assembler directives such as .org.
361 /// @param Offset - The offset to reach. This may be an expression, but the
362 /// expression must be associated with the current section.
363 /// @param Value - The value to use when filling bytes.
364 virtual void EmitValueToOffset(const MCExpr *Offset,
365 unsigned char Value = 0) = 0;
369 /// EmitFileDirective - Switch to a new logical file. This is used to
370 /// implement the '.file "foo.c"' assembler directive.
371 virtual void EmitFileDirective(StringRef Filename) = 0;
373 /// EmitDwarfFileDirective - Associate a filename with a specified logical
374 /// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler
376 virtual bool EmitDwarfFileDirective(unsigned FileNo,StringRef Filename);
378 /// EmitDwarfLocDirective - This implements the DWARF2
379 // '.loc fileno lineno ...' assembler directive.
380 virtual void EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
381 unsigned Column, unsigned Flags,
383 unsigned Discriminator);
385 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
386 const MCSymbol *LastLabel,
387 const MCSymbol *Label) = 0;
389 void EmitDwarfSetLineAddr(int64_t LineDelta, const MCSymbol *Label,
392 virtual bool EmitCFIStartProc();
393 virtual bool EmitCFIEndProc();
394 virtual bool EmitCFIDefCfaOffset(int64_t Offset);
395 virtual bool EmitCFIDefCfaRegister(int64_t Register);
396 virtual bool EmitCFIOffset(int64_t Register, int64_t Offset);
397 virtual bool EmitCFIPersonality(const MCSymbol *Sym,
399 virtual bool EmitCFILsda(const MCSymbol *Sym, unsigned Encoding);
401 /// EmitInstruction - Emit the given @p Instruction into the current
403 virtual void EmitInstruction(const MCInst &Inst) = 0;
405 /// EmitRawText - If this file is backed by a assembly streamer, this dumps
406 /// the specified string in the output .s file. This capability is
407 /// indicated by the hasRawTextSupport() predicate. By default this aborts.
408 virtual void EmitRawText(StringRef String);
409 void EmitRawText(const Twine &String);
411 /// Finish - Finish emission of machine code.
412 virtual void Finish() = 0;
415 /// createNullStreamer - Create a dummy machine code streamer, which does
416 /// nothing. This is useful for timing the assembler front end.
417 MCStreamer *createNullStreamer(MCContext &Ctx);
419 /// createAsmStreamer - Create a machine code streamer which will print out
420 /// assembly for the native target, suitable for compiling with a native
423 /// \param InstPrint - If given, the instruction printer to use. If not given
424 /// the MCInst representation will be printed. This method takes ownership of
427 /// \param CE - If given, a code emitter to use to show the instruction
428 /// encoding inline with the assembly. This method takes ownership of \arg CE.
430 /// \param TAB - If given, a target asm backend to use to show the fixup
431 /// information in conjunction with encoding information. This method takes
432 /// ownership of \arg TAB.
434 /// \param ShowInst - Whether to show the MCInst representation inline with
436 MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
439 MCInstPrinter *InstPrint = 0,
440 MCCodeEmitter *CE = 0,
441 TargetAsmBackend *TAB = 0,
442 bool ShowInst = false);
444 /// createMachOStreamer - Create a machine code streamer which will generate
445 /// Mach-O format object files.
447 /// Takes ownership of \arg TAB and \arg CE.
448 MCStreamer *createMachOStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
449 raw_ostream &OS, MCCodeEmitter *CE,
450 bool RelaxAll = false);
452 /// createWinCOFFStreamer - Create a machine code streamer which will
453 /// generate Microsoft COFF format object files.
455 /// Takes ownership of \arg TAB and \arg CE.
456 MCStreamer *createWinCOFFStreamer(MCContext &Ctx,
457 TargetAsmBackend &TAB,
458 MCCodeEmitter &CE, raw_ostream &OS,
459 bool RelaxAll = false);
461 /// createELFStreamer - Create a machine code streamer which will generate
462 /// ELF format object files.
463 MCStreamer *createELFStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
464 raw_ostream &OS, MCCodeEmitter *CE,
465 bool RelaxAll = false);
467 /// createLoggingStreamer - Create a machine code streamer which just logs the
468 /// API calls and then dispatches to another streamer.
470 /// The new streamer takes ownership of the \arg Child.
471 MCStreamer *createLoggingStreamer(MCStreamer *Child, raw_ostream &OS);
473 /// createPureStreamer - Create a machine code streamer which will generate
474 /// "pure" MC object files, for use with MC-JIT and testing tools.
476 /// Takes ownership of \arg TAB and \arg CE.
477 MCStreamer *createPureStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
478 raw_ostream &OS, MCCodeEmitter *CE);
480 } // end namespace llvm