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"
31 /// MCStreamer - Streaming machine code generation interface. This interface
32 /// is intended to provide a programatic interface that is very similar to the
33 /// level that an assembler .s file provides. It has callbacks to emit bytes,
34 /// "emit directives", etc. The implementation of this interface retains
35 /// state to know what the current section is etc.
37 /// There are multiple implementations of this interface: one for writing out
38 /// a .s file, and implementations that write out .o files of various formats.
44 Hidden, /// .hidden (ELF)
45 IndirectSymbol, /// .indirect_symbol (Apple)
46 Internal, /// .internal (ELF)
47 LazyReference, /// .lazy_reference (Apple)
48 NoDeadStrip, /// .no_dead_strip (Apple)
49 PrivateExtern, /// .private_extern (Apple)
50 Protected, /// .protected (ELF)
51 Reference, /// .reference (Apple)
53 WeakDefinition, /// .weak_definition (Apple)
54 WeakReference, /// .weak_reference (Apple)
56 SymbolAttrFirst = Global,
57 SymbolAttrLast = WeakReference
61 SubsectionsViaSymbols /// .subsections_via_symbols (Apple)
67 MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT
68 MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT
71 MCStreamer(MCContext &Ctx);
73 /// CurSection - This is the current section code is being emitted to, it is
74 /// kept up to date by SwitchSection.
75 const MCSection *CurSection;
77 virtual ~MCStreamer();
79 MCContext &getContext() const { return Context; }
81 /// @name Symbol & Section Management
84 /// SwitchSection - Set the current section where code is being emitted to
85 /// @param Section. This is required to update CurSection.
87 /// This corresponds to assembler directives like .section, .text, etc.
88 virtual void SwitchSection(const MCSection *Section) = 0;
91 /// getCurrentSection - Return the current seciton that the streamer is
93 const MCSection *getCurrentSection() const { return CurSection; }
95 /// EmitLabel - Emit a label for @param Symbol into the current section.
97 /// This corresponds to an assembler statement such as:
100 /// @param Symbol - The symbol to emit. A given symbol should only be
101 /// emitted as a label once, and symbols emitted as a label should never be
102 /// used in an assignment.
104 // FIXME: What to do about the current section? Should we get rid of the
105 // symbol section in the constructor and initialize it here?
106 virtual void EmitLabel(MCSymbol *Symbol) = 0;
108 /// EmitAssemblerFlag - Note in the output the specified @param Flag
109 virtual void EmitAssemblerFlag(AssemblerFlag Flag) = 0;
111 /// EmitAssignment - Emit an assignment of @param Value to @param Symbol.
113 /// This corresponds to an assembler statement such as:
116 /// The assignment generates no code, but has the side effect of binding the
117 /// value in the current context. For the assembly streamer, this prints the
118 /// binding into the .s file.
120 /// @param Symbol - The symbol being assigned to.
121 /// @param Value - The value for the symbol.
122 /// @param MakeAbsolute - If true, then the symbol should be given the
123 /// absolute value of @param Value, even if @param Value would be
124 /// relocatable expression. This corresponds to the ".set" directive.
125 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
126 bool MakeAbsolute = false) = 0;
128 /// EmitSymbolAttribute - Add the given @param Attribute to @param Symbol.
130 // FIXME: This doesn't make much sense, could we just have attributes be on
131 // the symbol and make the printer smart enough to add the right symbols?
132 // This should work as long as the order of attributes in the file doesn't
134 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
135 SymbolAttr Attribute) = 0;
137 /// EmitSymbolDesc - Set the @param DescValue for the @param Symbol.
139 /// @param Symbol - The symbol to have its n_desc field set.
140 /// @param DescValue - The value to set into the n_desc field.
141 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
143 /// EmitLocalSymbol - Emit a local symbol of @param Value to @param Symbol.
145 /// @param Symbol - The local symbol being created.
146 /// @param Value - The value for the symbol.
147 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) = 0;
149 /// EmitCommonSymbol - Emit a common or local common symbol of @param Size
150 /// with the @param Pow2Alignment if non-zero.
152 /// @param Symbol - The common symbol to emit.
153 /// @param Size - The size of the common symbol.
154 /// @param Pow2Alignment - The alignment of the common symbol if non-zero.
155 /// @param IsLocal - If true, then the symbol is to be a local common
156 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
157 unsigned Pow2Alignment, bool IsLocal) = 0;
159 /// EmitZerofill - Emit a the zerofill section and possiblity a symbol, if
160 /// @param Symbol is non-NULL, for @param Size and with the @param
161 /// Pow2Alignment if non-zero.
163 /// @param Section - The zerofill section to create and or to put the symbol
164 /// @param Symbol - The zerofill symbol to emit, if non-NULL.
165 /// @param Size - The size of the zerofill symbol.
166 /// @param Pow2Alignment - The alignment of the zerofill symbol if non-zero.
167 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = 0,
168 unsigned Size = 0,unsigned Pow2Alignment = 0) = 0;
171 /// @name Generating Data
174 /// EmitBytes - Emit the bytes in @param Data into the output.
176 /// This is used to implement assembler directives such as .byte, .ascii,
178 virtual void EmitBytes(const StringRef &Data) = 0;
180 /// EmitValue - Emit the expression @param Value into the output as a native
181 /// integer of the given @param Size bytes.
183 /// This is used to implement assembler directives such as .word, .quad,
186 /// @param Value - The value to emit.
187 /// @param Size - The size of the integer (in bytes) to emit. This must
188 /// match a native machine width.
189 virtual void EmitValue(const MCValue &Value, unsigned Size) = 0;
191 /// EmitValueToAlignment - Emit some number of copies of @param Value until
192 /// the byte alignment @param ByteAlignment is reached.
194 /// If the number of bytes need to emit for the alignment is not a multiple
195 /// of @param ValueSize, then the contents of the emitted fill bytes is
198 /// This used to implement the .align assembler directive.
200 /// @param ByteAlignment - The alignment to reach. This must be a power of
201 /// two on some targets.
202 /// @param Value - The value to use when filling bytes.
203 /// @param Size - The size of the integer (in bytes) to emit for @param
204 /// Value. This must match a native machine width.
205 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
206 /// the alignment cannot be reached in this many bytes, no bytes are
208 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
209 unsigned ValueSize = 1,
210 unsigned MaxBytesToEmit = 0) = 0;
212 /// EmitValueToOffset - Emit some number of copies of @param Value until the
213 /// byte offset @param Offset is reached.
215 /// This is used to implement assembler directives such as .org.
217 /// @param Offset - The offset to reach.This may be an expression, but the
218 /// expression must be associated with the current section.
219 /// @param Value - The value to use when filling bytes.
221 // FIXME: How are we going to signal failures out of this?
222 virtual void EmitValueToOffset(const MCValue &Offset,
223 unsigned char Value = 0) = 0;
227 /// EmitInstruction - Emit the given @param Instruction into the current
229 virtual void EmitInstruction(const MCInst &Inst) = 0;
231 /// Finish - Finish emission of machine code and flush any output.
232 virtual void Finish() = 0;
235 /// createNullStreamer - Create a dummy machine code streamer, which does
236 /// nothing. This is useful for timing the assembler front end.
237 MCStreamer *createNullStreamer(MCContext &Ctx);
239 /// createAsmStreamer - Create a machine code streamer which will print out
240 /// assembly for the native target, suitable for compiling with a native
243 /// \arg AP - If given, an AsmPrinter to use for printing instructions.
244 MCStreamer *createAsmStreamer(MCContext &Ctx, raw_ostream &OS,
245 const MCAsmInfo &MAI, AsmPrinter *AP = 0,
246 MCCodeEmitter *CE = 0);
248 // FIXME: These two may end up getting rolled into a single
249 // createObjectStreamer interface, which implements the assembler backend, and
250 // is parameterized on an output object file writer.
252 /// createMachOStream - Create a machine code streamer which will generative
253 /// Mach-O format object files.
254 MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS);
256 /// createELFStreamer - Create a machine code streamer which will generative
257 /// ELF format object files.
258 MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS);
260 } // end namespace llvm