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 /// 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 /// @param MakeAbsolute - If true, then the symbol should be given the
120 /// absolute value of @param Value, even if @param Value would be
121 /// relocatable expression. This corresponds to the ".set" directive.
122 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
123 bool MakeAbsolute = false) = 0;
125 /// EmitSymbolAttribute - Add the given @param Attribute to @param Symbol.
126 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
127 SymbolAttr Attribute) = 0;
129 /// EmitSymbolDesc - Set the @param DescValue for the @param Symbol.
131 /// @param Symbol - The symbol to have its n_desc field set.
132 /// @param DescValue - The value to set into the n_desc field.
133 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) = 0;
135 /// EmitCommonSymbol - Emit a common or local common symbol.
137 /// @param Symbol - The common symbol to emit.
138 /// @param Size - The size of the common symbol.
139 /// @param ByteAlignment - The alignment of the symbol if
140 /// non-zero. This must be a power of 2 on some targets.
141 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
142 unsigned ByteAlignment) = 0;
144 /// EmitZerofill - Emit a the zerofill section and an option symbol.
146 /// @param Section - The zerofill section to create and or to put the symbol
147 /// @param Symbol - The zerofill symbol to emit, if non-NULL.
148 /// @param Size - The size of the zerofill symbol.
149 /// @param ByteAlignment - The alignment of the zerofill symbol if
150 /// non-zero. This must be a power of 2 on some targets.
151 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
152 unsigned Size = 0,unsigned ByteAlignment = 0) = 0;
155 /// @name Generating Data
158 /// EmitBytes - Emit the bytes in \arg Data into the output.
160 /// This is used to implement assembler directives such as .byte, .ascii,
162 virtual void EmitBytes(const StringRef &Data) = 0;
164 /// EmitValue - Emit the expression @param Value into the output as a native
165 /// integer of the given @param Size bytes.
167 /// This is used to implement assembler directives such as .word, .quad,
170 /// @param Value - The value to emit.
171 /// @param Size - The size of the integer (in bytes) to emit. This must
172 /// match a native machine width.
173 virtual void EmitValue(const MCValue &Value, unsigned Size) = 0;
175 /// EmitValueToAlignment - Emit some number of copies of @param Value until
176 /// the byte alignment @param ByteAlignment is reached.
178 /// If the number of bytes need to emit for the alignment is not a multiple
179 /// of @param ValueSize, then the contents of the emitted fill bytes is
182 /// This used to implement the .align assembler directive.
184 /// @param ByteAlignment - The alignment to reach. This must be a power of
185 /// two on some targets.
186 /// @param Value - The value to use when filling bytes.
187 /// @param Size - The size of the integer (in bytes) to emit for @param
188 /// Value. This must match a native machine width.
189 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
190 /// the alignment cannot be reached in this many bytes, no bytes are
192 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
193 unsigned ValueSize = 1,
194 unsigned MaxBytesToEmit = 0) = 0;
196 /// EmitValueToOffset - Emit some number of copies of @param Value until the
197 /// byte offset @param Offset is reached.
199 /// This is used to implement assembler directives such as .org.
201 /// @param Offset - The offset to reach. This may be an expression, but the
202 /// expression must be associated with the current section.
203 /// @param Value - The value to use when filling bytes.
204 virtual void EmitValueToOffset(const MCValue &Offset,
205 unsigned char Value = 0) = 0;
209 /// EmitInstruction - Emit the given @param Instruction into the current
211 virtual void EmitInstruction(const MCInst &Inst) = 0;
213 /// Finish - Finish emission of machine code and flush any output.
214 virtual void Finish() = 0;
217 /// createNullStreamer - Create a dummy machine code streamer, which does
218 /// nothing. This is useful for timing the assembler front end.
219 MCStreamer *createNullStreamer(MCContext &Ctx);
221 /// createAsmStreamer - Create a machine code streamer which will print out
222 /// assembly for the native target, suitable for compiling with a native
225 /// \arg AP - If given, an AsmPrinter to use for printing instructions.
226 MCStreamer *createAsmStreamer(MCContext &Ctx, raw_ostream &OS,
227 const MCAsmInfo &MAI, AsmPrinter *AP = 0,
228 MCCodeEmitter *CE = 0);
230 // FIXME: These two may end up getting rolled into a single
231 // createObjectStreamer interface, which implements the assembler backend, and
232 // is parameterized on an output object file writer.
234 /// createMachOStream - Create a machine code streamer which will generative
235 /// Mach-O format object files.
236 MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS,
237 MCCodeEmitter *CE = 0);
239 /// createELFStreamer - Create a machine code streamer which will generative
240 /// ELF format object files.
241 MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS);
243 } // end namespace llvm