MCStreamer.h revision 84a2926fb7ab388d688a133b0b375a26e669fd55
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#ifndef LLVM_MC_MCSTREAMER_H
11#define LLVM_MC_MCSTREAMER_H
12
13#include "llvm/Support/DataTypes.h"
14
15namespace llvm {
16  class MCContext;
17  class MCValue;
18  class MCInst;
19  class MCSection;
20  class MCSymbol;
21  class raw_ostream;
22
23  /// MCStreamer - Streaming machine code generation interface.
24  class MCStreamer {
25  public:
26    enum SymbolAttr {
27      Global,         /// .globl
28      Hidden,         /// .hidden (ELF)
29      IndirectSymbol, /// .indirect_symbol (Apple)
30      Internal,       /// .internal (ELF)
31      LazyReference,  /// .lazy_reference (Apple)
32      NoDeadStrip,    /// .no_dead_strip (Apple)
33      PrivateExtern,  /// .private_extern (Apple)
34      Protected,      /// .protected (ELF)
35      Reference,      /// .reference (Apple)
36      Weak,           /// .weak
37      WeakDefinition, /// .weak_definition (Apple)
38      WeakReference,  /// .weak_reference (Apple)
39
40      SymbolAttrFirst = Global,
41      SymbolAttrLast = WeakReference
42    };
43
44  private:
45    MCContext &Context;
46
47    MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT
48    MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT
49
50  protected:
51    MCStreamer(MCContext &Ctx);
52
53  public:
54    virtual ~MCStreamer();
55
56    MCContext &getContext() const { return Context; }
57
58    /// @name Symbol & Section Management
59    /// @{
60
61    /// SwitchSection - Set the current section where code is being emitted to
62    /// @param Section.
63    ///
64    /// This corresponds to assembler directives like .section, .text, etc.
65    virtual void SwitchSection(MCSection *Section) = 0;
66
67    /// EmitLabel - Emit a label for @param Symbol into the current section.
68    ///
69    /// This corresponds to an assembler statement such as:
70    ///   foo:
71    ///
72    /// @param Symbol - The symbol to emit. A given symbol should only be
73    /// emitted as a label once, and symbols emitted as a label should never be
74    /// used in an assignment.
75    //
76    // FIXME: What to do about the current section? Should we get rid of the
77    // symbol section in the constructor and initialize it here?
78    virtual void EmitLabel(MCSymbol *Symbol) = 0;
79
80    /// EmitAssignment - Emit an assignment of @param Value to @param Symbol.
81    ///
82    /// This corresponds to an assembler statement such as:
83    ///  symbol = value
84    ///
85    /// The assignment generates no code, but has the side effect of binding the
86    /// value in the current context. For the assembly streamer, this prints the
87    /// binding into the .s file.
88    ///
89    /// @param Symbol - The symbol being assigned to.
90    /// @param Value - The value for the symbol.
91    /// @param MakeAbsolute - If true, then the symbol should be given the
92    /// absolute value of @param Value, even if @param Value would be
93    /// relocatable expression. This corresponds to the ".set" directive.
94    virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
95                                bool MakeAbsolute = false) = 0;
96
97    /// EmitSymbolAttribute - Add the given @param Attribute to @param Symbol.
98    //
99    // FIXME: This doesn't make much sense, could we just have attributes be on
100    // the symbol and make the printer smart enough to add the right symbols?
101    // This should work as long as the order of attributes in the file doesn't
102    // matter.
103    virtual void EmitSymbolAttribute(MCSymbol *Symbol,
104                                     SymbolAttr Attribute) = 0;
105
106    /// @}
107    /// @name Generating Data
108    /// @{
109
110    /// EmitBytes - Emit @param Length bytes starting at @param Data into the
111    /// output.
112    ///
113    /// This is used to implement assembler directives such as .byte, .ascii,
114    /// etc.
115    virtual void EmitBytes(const char *Data, unsigned Length) = 0;
116
117    /// EmitValue - Emit the expression @param Value into the output as a native
118    /// integer of the given @param Size bytes.
119    ///
120    /// This is used to implement assembler directives such as .word, .quad,
121    /// etc.
122    ///
123    /// @param Value - The value to emit.
124    /// @param Size - The size of the integer (in bytes) to emit. This must
125    /// match a native machine width.
126    virtual void EmitValue(const MCValue &Value, unsigned Size) = 0;
127
128    /// EmitValueToAlignment - Emit some number of copies of @param Value until
129    /// the byte alignment @param ByteAlignment is reached.
130    ///
131    /// If the number of bytes need to emit for the alignment is not a multiple
132    /// of @param ValueSize, then the contents of the emitted fill bytes is
133    /// undefined.
134    ///
135    /// This used to implement the .align assembler directive.
136    ///
137    /// @param ByteAlignment - The alignment to reach. This must be a power of
138    /// two.
139    /// @param Value - The value to use when filling bytes.
140    /// @param Size - The size of the integer (in bytes) to emit for @param
141    /// Value. This must match a native machine width.
142    /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
143    /// the alignment cannot be reached in this many bytes, no bytes are
144    /// emitted.
145    virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
146                                      unsigned ValueSize = 1,
147                                      unsigned MaxBytesToEmit = 0) = 0;
148
149    /// EmitValueToOffset - Emit some number of copies of @param Value until the
150    /// byte offset @param Offset is reached.
151    ///
152    /// This is used to implement assembler directives such as .org.
153    ///
154    /// @param Offset - The offset to reach.This may be an expression, but the
155    /// expression must be associated with the current section.
156    /// @param Value - The value to use when filling bytes.
157    //
158    // FIXME: How are we going to signal failures out of this?
159    virtual void EmitValueToOffset(const MCValue &Offset,
160                                   unsigned char Value = 0) = 0;
161
162    /// @}
163
164    /// EmitInstruction - Emit the given @param Instruction into the current
165    /// section.
166    virtual void EmitInstruction(const MCInst &Inst) = 0;
167
168    /// Finish - Finish emission of machine code and flush any output.
169    virtual void Finish() = 0;
170  };
171
172  /// createAsmStreamer - Create a machine code streamer which will print out
173  /// assembly for the native target, suitable for compiling with a native
174  /// assembler.
175  MCStreamer *createAsmStreamer(MCContext &Ctx, raw_ostream &OS);
176
177  // FIXME: These two may end up getting rolled into a single
178  // createObjectStreamer interface, which implements the assembler backend, and
179  // is parameterized on an output object file writer.
180
181  /// createMachOStream - Create a machine code streamer which will generative
182  /// Mach-O format object files.
183  MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS);
184
185  /// createELFStreamer - Create a machine code streamer which will generative
186  /// ELF format object files.
187  MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS);
188
189} // end namespace llvm
190
191#endif
192