MCStreamer.h revision 89b9372605db2ce3b0085c84089e389f7bc1fbdd
1//===- MCStreamer.h - High-level Streaming Machine Code Output --*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the MCStreamer class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_MC_MCSTREAMER_H
15#define LLVM_MC_MCSTREAMER_H
16
17#include "llvm/Support/DataTypes.h"
18#include "llvm/MC/MCDirectives.h"
19#include "llvm/MC/MCDwarf.h"
20
21namespace llvm {
22  class MCAsmInfo;
23  class MCCodeEmitter;
24  class MCContext;
25  class MCExpr;
26  class MCInst;
27  class MCInstPrinter;
28  class MCSection;
29  class MCSymbol;
30  class StringRef;
31  class TargetAsmBackend;
32  class TargetLoweringObjectFile;
33  class Twine;
34  class raw_ostream;
35  class formatted_raw_ostream;
36
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.
42  ///
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.
45  ///
46  class MCStreamer {
47    MCContext &Context;
48
49    MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT
50    MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT
51
52    void EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
53                         bool isPCRel, unsigned AddrSpace);
54
55  protected:
56    MCStreamer(MCContext &Ctx);
57
58    /// CurSection - This is the current section code is being emitted to, it is
59    /// kept up to date by SwitchSection.
60    const MCSection *CurSection;
61
62    /// PrevSection - This is the previous section code is being emitted to, it
63    /// is kept up to date by SwitchSection.
64    const MCSection *PrevSection;
65
66    std::vector<MCDwarfFrameInfo> FrameInfos;
67    MCDwarfFrameInfo *getCurrentFrameInfo();
68    void EnsureValidFrame();
69
70  public:
71    virtual ~MCStreamer();
72
73    MCContext &getContext() const { return Context; }
74
75    unsigned getNumFrameInfos() {
76      return FrameInfos.size();
77    }
78
79    const MCDwarfFrameInfo &getFrameInfo(unsigned i) {
80      return FrameInfos[i];
81    }
82
83    /// @name Assembly File Formatting.
84    /// @{
85
86    /// isVerboseAsm - Return true if this streamer supports verbose assembly
87    /// and if it is enabled.
88    virtual bool isVerboseAsm() const { return false; }
89
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; }
93
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.
98    ///
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) {}
102
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
105    /// use this method.
106    virtual raw_ostream &GetCommentOS();
107
108    /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
109    virtual void AddBlankLine() {}
110
111    /// @}
112
113    /// @name Symbol & Section Management
114    /// @{
115
116    /// getCurrentSection - Return the current section that the streamer is
117    /// emitting code to.
118    const MCSection *getCurrentSection() const { return CurSection; }
119
120    /// getPreviousSection - Return the previous section that the streamer is
121    /// emitting code to.
122    const MCSection *getPreviousSection() const { return PrevSection; }
123
124    /// SwitchSection - Set the current section where code is being emitted to
125    /// @p Section.  This is required to update CurSection.
126    ///
127    /// This corresponds to assembler directives like .section, .text, etc.
128    virtual void SwitchSection(const MCSection *Section) = 0;
129
130    /// InitSections - Create the default sections and set the initial one.
131    virtual void InitSections() = 0;
132
133    /// EmitLabel - Emit a label for @p Symbol into the current section.
134    ///
135    /// This corresponds to an assembler statement such as:
136    ///   foo:
137    ///
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;
142
143    /// EmitAssemblerFlag - Note in the output the specified @p Flag
144    virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) = 0;
145
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;
149
150    /// EmitAssignment - Emit an assignment of @p Value to @p Symbol.
151    ///
152    /// This corresponds to an assembler statement such as:
153    ///  symbol = value
154    ///
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.
158    ///
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;
162
163    /// EmitWeakReference - Emit an weak reference from @p Alias to @p Symbol.
164    ///
165    /// This corresponds to an assembler statement such as:
166    ///  .weakref alias, symbol
167    ///
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;
171
172    /// EmitSymbolAttribute - Add the given @p Attribute to @p Symbol.
173    virtual void EmitSymbolAttribute(MCSymbol *Symbol,
174                                     MCSymbolAttr Attribute) = 0;
175
176    /// EmitSymbolDesc - Set the @p DescValue for the @p Symbol.
177    ///
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;
181
182    /// BeginCOFFSymbolDef - Start emitting COFF symbol definition
183    ///
184    /// @param Symbol - The symbol to have its External & Type fields set.
185    virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) = 0;
186
187    /// EmitCOFFSymbolStorageClass - Emit the storage class of the symbol.
188    ///
189    /// @param StorageClass - The storage class the symbol should have.
190    virtual void EmitCOFFSymbolStorageClass(int StorageClass) = 0;
191
192    /// EmitCOFFSymbolType - Emit the type of the symbol.
193    ///
194    /// @param Type - A COFF type identifier (see COFF::SymbolType in X86COFF.h)
195    virtual void EmitCOFFSymbolType(int Type) = 0;
196
197    /// EndCOFFSymbolDef - Marks the end of the symbol definition.
198    virtual void EndCOFFSymbolDef() = 0;
199
200    /// EmitELFSize - Emit an ELF .size directive.
201    ///
202    /// This corresponds to an assembler statement such as:
203    ///  .size symbol, expression
204    ///
205    virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) = 0;
206
207    /// EmitCommonSymbol - Emit a common symbol.
208    ///
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;
215
216    /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
217    ///
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;
221
222    /// EmitZerofill - Emit the zerofill section and an optional symbol.
223    ///
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;
231
232    /// EmitTBSSSymbol - Emit a thread local bss (.tbss) symbol.
233    ///
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;
241    /// @}
242    /// @name Generating Data
243    /// @{
244
245    /// EmitBytes - Emit the bytes in \arg Data into the output.
246    ///
247    /// This is used to implement assembler directives such as .byte, .ascii,
248    /// etc.
249    virtual void EmitBytes(StringRef Data, unsigned AddrSpace) = 0;
250
251    /// EmitValue - Emit the expression @p Value into the output as a native
252    /// integer of the given @p Size bytes.
253    ///
254    /// This is used to implement assembler directives such as .word, .quad,
255    /// etc.
256    ///
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;
262
263    void EmitValue(const MCExpr *Value, unsigned Size, unsigned AddrSpace = 0);
264
265    void EmitPCRelValue(const MCExpr *Value, unsigned Size,
266                        unsigned AddrSpace = 0);
267
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);
272
273    /// EmitAbsValue - Emit the Value, but try to avoid relocations. On MachO
274    /// this is done by producing
275    /// foo = value
276    /// .long foo
277    void EmitAbsValue(const MCExpr *Value, unsigned Size,
278                      unsigned AddrSpace = 0);
279
280    virtual void EmitULEB128Value(const MCExpr *Value,
281                                  unsigned AddrSpace = 0) = 0;
282
283    virtual void EmitSLEB128Value(const MCExpr *Value,
284                                  unsigned AddrSpace = 0) = 0;
285
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);
289
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);
293
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);
298
299    void EmitPCRelSymbolValue(const MCSymbol *Sym, unsigned Size,
300                              unsigned AddrSpace = 0);
301
302    /// EmitGPRel32Value - Emit the expression @p Value into the output as a
303    /// gprel32 (32-bit GP relative) value.
304    ///
305    /// This is used to implement assembler directives such as .gprel32 on
306    /// targets that support them.
307    virtual void EmitGPRel32Value(const MCExpr *Value);
308
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,
312                          unsigned AddrSpace);
313
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);
318    }
319
320
321    /// EmitValueToAlignment - Emit some number of copies of @p Value until
322    /// the byte alignment @p ByteAlignment is reached.
323    ///
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
326    /// undefined.
327    ///
328    /// This used to implement the .align assembler directive.
329    ///
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
337    /// emitted.
338    virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
339                                      unsigned ValueSize = 1,
340                                      unsigned MaxBytesToEmit = 0) = 0;
341
342    /// EmitCodeAlignment - Emit nops until the byte alignment @p ByteAlignment
343    /// is reached.
344    ///
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.
347    ///
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
352    /// emitted.
353    virtual void EmitCodeAlignment(unsigned ByteAlignment,
354                                   unsigned MaxBytesToEmit = 0) = 0;
355
356    /// EmitValueToOffset - Emit some number of copies of @p Value until the
357    /// byte offset @p Offset is reached.
358    ///
359    /// This is used to implement assembler directives such as .org.
360    ///
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;
366
367    /// @}
368
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;
372
373    /// EmitDwarfFileDirective - Associate a filename with a specified logical
374    /// file number.  This implements the DWARF2 '.file 4 "foo.c"' assembler
375    /// directive.
376    virtual bool EmitDwarfFileDirective(unsigned FileNo,StringRef Filename);
377
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,
382                                       unsigned Isa,
383                                       unsigned Discriminator);
384
385    virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
386                                          const MCSymbol *LastLabel,
387                                          const MCSymbol *Label) = 0;
388
389    void EmitDwarfSetLineAddr(int64_t LineDelta, const MCSymbol *Label,
390                              int PointerSize);
391
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);
398    virtual bool EmitCFILsda(const MCSymbol *Sym);
399
400    /// EmitInstruction - Emit the given @p Instruction into the current
401    /// section.
402    virtual void EmitInstruction(const MCInst &Inst) = 0;
403
404    /// EmitRawText - If this file is backed by a assembly streamer, this dumps
405    /// the specified string in the output .s file.  This capability is
406    /// indicated by the hasRawTextSupport() predicate.  By default this aborts.
407    virtual void EmitRawText(StringRef String);
408    void EmitRawText(const Twine &String);
409
410    /// Finish - Finish emission of machine code.
411    virtual void Finish() = 0;
412  };
413
414  /// createNullStreamer - Create a dummy machine code streamer, which does
415  /// nothing. This is useful for timing the assembler front end.
416  MCStreamer *createNullStreamer(MCContext &Ctx);
417
418  /// createAsmStreamer - Create a machine code streamer which will print out
419  /// assembly for the native target, suitable for compiling with a native
420  /// assembler.
421  ///
422  /// \param InstPrint - If given, the instruction printer to use. If not given
423  /// the MCInst representation will be printed.  This method takes ownership of
424  /// InstPrint.
425  ///
426  /// \param CE - If given, a code emitter to use to show the instruction
427  /// encoding inline with the assembly. This method takes ownership of \arg CE.
428  ///
429  /// \param ShowInst - Whether to show the MCInst representation inline with
430  /// the assembly.
431  MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
432                                bool isVerboseAsm,
433                                bool useLoc,
434                                MCInstPrinter *InstPrint = 0,
435                                MCCodeEmitter *CE = 0,
436                                bool ShowInst = false);
437
438  /// createMachOStreamer - Create a machine code streamer which will generate
439  /// Mach-O format object files.
440  ///
441  /// Takes ownership of \arg TAB and \arg CE.
442  MCStreamer *createMachOStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
443                                  raw_ostream &OS, MCCodeEmitter *CE,
444                                  bool RelaxAll = false);
445
446  /// createWinCOFFStreamer - Create a machine code streamer which will
447  /// generate Microsoft COFF format object files.
448  ///
449  /// Takes ownership of \arg TAB and \arg CE.
450  MCStreamer *createWinCOFFStreamer(MCContext &Ctx,
451                                    TargetAsmBackend &TAB,
452                                    MCCodeEmitter &CE, raw_ostream &OS,
453                                    bool RelaxAll = false);
454
455  /// createELFStreamer - Create a machine code streamer which will generate
456  /// ELF format object files.
457  MCStreamer *createELFStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
458				raw_ostream &OS, MCCodeEmitter *CE,
459				bool RelaxAll = false);
460
461  /// createLoggingStreamer - Create a machine code streamer which just logs the
462  /// API calls and then dispatches to another streamer.
463  ///
464  /// The new streamer takes ownership of the \arg Child.
465  MCStreamer *createLoggingStreamer(MCStreamer *Child, raw_ostream &OS);
466
467  /// createPureStreamer - Create a machine code streamer which will generate
468  /// "pure" MC object files, for use with MC-JIT and testing tools.
469  ///
470  /// Takes ownership of \arg TAB and \arg CE.
471  MCStreamer *createPureStreamer(MCContext &Ctx, TargetAsmBackend &TAB,
472                                 raw_ostream &OS, MCCodeEmitter *CE);
473
474} // end namespace llvm
475
476#endif
477