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"
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 /// handle 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;
78 virtual ~MCStreamer();
80 MCContext &getContext() const { return Context; }
82 /// @name Symbol & Section Management
85 /// getCurrentSection - Return the current seciton that the streamer is
87 const MCSection *getCurrentSection() const { return CurSection; }
89 /// SwitchSection - Set the current section where code is being emitted to
90 /// @param Section. This is required to update CurSection.
92 /// This corresponds to assembler directives like .section, .text, etc.
93 virtual void SwitchSection(const MCSection *Section) = 0;
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.
103 virtual void EmitLabel(MCSymbol *Symbol) = 0;
105 /// EmitAssemblerFlag - Note in the output the specified @param Flag
106 virtual void EmitAssemblerFlag(AssemblerFlag Flag) = 0;
108 /// EmitAssignment - Emit an assignment of @param Value to @param Symbol.
110 /// This corresponds to an assembler statement such as:
113 /// The assignment generates no code, but has the side effect of binding the
114 /// value in the current context. For the assembly streamer, this prints the
115 /// binding into the .s file.
117 /// @param Symbol - The symbol being assigned to.
118 /// @param Value - The value for the symbol.
119 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) = 0;
121 /// EmitSymbolAttribute - Add the given @param Attribute to @param Symbol.
122 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
123 SymbolAttr Attribute) = 0;
125 /// EmitSymbolDesc - Set the @param DescValue for the @param Symbol.
127 /// @param Symbol - The symbol to have its n_desc field set.
128 /// @param DescValue - The value to set into the n_desc field.
129 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
131 /// EmitCommonSymbol - Emit a common or local common symbol.
133 /// @param Symbol - The common symbol to emit.
134 /// @param Size - The size of the common symbol.
135 /// @param ByteAlignment - The alignment of the symbol if
136 /// non-zero. This must be a power of 2 on some targets.
137 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
138 unsigned ByteAlignment) = 0;
140 /// EmitZerofill - Emit a the zerofill section and an option symbol.
142 /// @param Section - The zerofill section to create and or to put the symbol
143 /// @param Symbol - The zerofill symbol to emit, if non-NULL.
144 /// @param Size - The size of the zerofill symbol.
145 /// @param ByteAlignment - The alignment of the zerofill symbol if
146 /// non-zero. This must be a power of 2 on some targets.
147 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
148 unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
151 /// @name Generating Data
154 /// EmitBytes - Emit the bytes in \arg Data into the output.
156 /// This is used to implement assembler directives such as .byte, .ascii,
158 virtual void EmitBytes(const StringRef &Data) = 0;
160 /// EmitValue - Emit the expression @param Value into the output as a native
161 /// integer of the given @param Size bytes.
163 /// This is used to implement assembler directives such as .word, .quad,
166 /// @param Value - The value to emit.
167 /// @param Size - The size of the integer (in bytes) to emit. This must
168 /// match a native machine width.
169 virtual void EmitValue(const MCExpr *Value, unsigned Size) = 0;
171 /// EmitValueToAlignment - Emit some number of copies of @param Value until
172 /// the byte alignment @param ByteAlignment is reached.
174 /// If the number of bytes need to emit for the alignment is not a multiple
175 /// of @param ValueSize, then the contents of the emitted fill bytes is
178 /// This used to implement the .align assembler directive.
180 /// @param ByteAlignment - The alignment to reach. This must be a power of
181 /// two on some targets.
182 /// @param Value - The value to use when filling bytes.
183 /// @param Size - The size of the integer (in bytes) to emit for @param
184 /// Value. This must match a native machine width.
185 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
186 /// the alignment cannot be reached in this many bytes, no bytes are
188 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
189 unsigned ValueSize = 1,
190 unsigned MaxBytesToEmit = 0) = 0;
192 /// EmitValueToOffset - Emit some number of copies of @param Value until the
193 /// byte offset @param Offset is reached.
195 /// This is used to implement assembler directives such as .org.
197 /// @param Offset - The offset to reach. This may be an expression, but the
198 /// expression must be associated with the current section.
199 /// @param Value - The value to use when filling bytes.
200 virtual void EmitValueToOffset(const MCExpr *Offset,
201 unsigned char Value = 0) = 0;
205 /// EmitInstruction - Emit the given @param Instruction into the current
207 virtual void EmitInstruction(const MCInst &Inst) = 0;
209 /// Finish - Finish emission of machine code and flush any output.
210 virtual void Finish() = 0;
213 /// createNullStreamer - Create a dummy machine code streamer, which does
214 /// nothing. This is useful for timing the assembler front end.
215 MCStreamer *createNullStreamer(MCContext &Ctx);
217 /// createAsmStreamer - Create a machine code streamer which will print out
218 /// assembly for the native target, suitable for compiling with a native
220 MCStreamer *createAsmStreamer(MCContext &Ctx, raw_ostream &OS,
221 const MCAsmInfo &MAI,
222 MCInstPrinter *InstPrint = 0,
223 MCCodeEmitter *CE = 0);
225 // FIXME: These two may end up getting rolled into a single
226 // createObjectStreamer interface, which implements the assembler backend, and
227 // is parameterized on an output object file writer.
229 /// createMachOStream - Create a machine code streamer which will generative
230 /// Mach-O format object files.
231 MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS,
232 MCCodeEmitter *CE = 0);
234 /// createELFStreamer - Create a machine code streamer which will generative
235 /// ELF format object files.
236 MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS);
238 } // end namespace llvm