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"
27 /// MCStreamer - Streaming machine code generation interface. This interface
28 /// is intended to provide a programatic interface that is very similar to the
29 /// level that an assembler .s file provides. It has callbacks to emit bytes,
30 /// "emit directives", etc. The implementation of this interface retains
31 /// state to know what the current section is etc.
33 /// There are multiple implementations of this interface: one for writing out
34 /// a .s file, and implementations that write out .o files of various formats.
40 Hidden, /// .hidden (ELF)
41 IndirectSymbol, /// .indirect_symbol (Apple)
42 Internal, /// .internal (ELF)
43 LazyReference, /// .lazy_reference (Apple)
44 NoDeadStrip, /// .no_dead_strip (Apple)
45 PrivateExtern, /// .private_extern (Apple)
46 Protected, /// .protected (ELF)
47 Reference, /// .reference (Apple)
49 WeakDefinition, /// .weak_definition (Apple)
50 WeakReference, /// .weak_reference (Apple)
52 SymbolAttrFirst = Global,
53 SymbolAttrLast = WeakReference
59 MCStreamer(const MCStreamer&); // DO NOT IMPLEMENT
60 MCStreamer &operator=(const MCStreamer&); // DO NOT IMPLEMENT
63 MCStreamer(MCContext &Ctx);
66 virtual ~MCStreamer();
68 MCContext &getContext() const { return Context; }
70 /// @name Symbol & Section Management
73 /// SwitchSection - Set the current section where code is being emitted to
76 /// This corresponds to assembler directives like .section, .text, etc.
77 virtual void SwitchSection(MCSection *Section) = 0;
79 /// EmitLabel - Emit a label for @param Symbol into the current section.
81 /// This corresponds to an assembler statement such as:
84 /// @param Symbol - The symbol to emit. A given symbol should only be
85 /// emitted as a label once, and symbols emitted as a label should never be
86 /// used in an assignment.
88 // FIXME: What to do about the current section? Should we get rid of the
89 // symbol section in the constructor and initialize it here?
90 virtual void EmitLabel(MCSymbol *Symbol) = 0;
92 /// EmitAssignment - Emit an assignment of @param Value to @param Symbol.
94 /// This corresponds to an assembler statement such as:
97 /// The assignment generates no code, but has the side effect of binding the
98 /// value in the current context. For the assembly streamer, this prints the
99 /// binding into the .s file.
101 /// @param Symbol - The symbol being assigned to.
102 /// @param Value - The value for the symbol.
103 /// @param MakeAbsolute - If true, then the symbol should be given the
104 /// absolute value of @param Value, even if @param Value would be
105 /// relocatable expression. This corresponds to the ".set" directive.
106 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
107 bool MakeAbsolute = false) = 0;
109 /// EmitSymbolAttribute - Add the given @param Attribute to @param Symbol.
111 // FIXME: This doesn't make much sense, could we just have attributes be on
112 // the symbol and make the printer smart enough to add the right symbols?
113 // This should work as long as the order of attributes in the file doesn't
115 virtual void EmitSymbolAttribute(MCSymbol *Symbol,
116 SymbolAttr Attribute) = 0;
118 /// EmitCommonSymbol - Emit a common symbol of @param Size with the @param
119 /// Pow2Alignment if non-zero.
121 /// @param Symbol - The common symbol to emit.
122 /// @param Size - The size of the common symbol.
123 /// @param Pow2Alignment - The alignment of the common symbol if non-zero.
124 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
125 unsigned Pow2Alignment) = 0;
128 /// @name Generating Data
131 /// EmitBytes - Emit @param Length bytes starting at @param Data into the
134 /// This is used to implement assembler directives such as .byte, .ascii,
136 virtual void EmitBytes(const char *Data, unsigned Length) = 0;
138 /// EmitValue - Emit the expression @param Value into the output as a native
139 /// integer of the given @param Size bytes.
141 /// This is used to implement assembler directives such as .word, .quad,
144 /// @param Value - The value to emit.
145 /// @param Size - The size of the integer (in bytes) to emit. This must
146 /// match a native machine width.
147 virtual void EmitValue(const MCValue &Value, unsigned Size) = 0;
149 /// EmitValueToAlignment - Emit some number of copies of @param Value until
150 /// the byte alignment @param ByteAlignment is reached.
152 /// If the number of bytes need to emit for the alignment is not a multiple
153 /// of @param ValueSize, then the contents of the emitted fill bytes is
156 /// This used to implement the .align assembler directive.
158 /// @param ByteAlignment - The alignment to reach. This must be a power of
159 /// two on some targets.
160 /// @param Value - The value to use when filling bytes.
161 /// @param Size - The size of the integer (in bytes) to emit for @param
162 /// Value. This must match a native machine width.
163 /// @param MaxBytesToEmit - The maximum numbers of bytes to emit, or 0. If
164 /// the alignment cannot be reached in this many bytes, no bytes are
166 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
167 unsigned ValueSize = 1,
168 unsigned MaxBytesToEmit = 0) = 0;
170 /// EmitValueToOffset - Emit some number of copies of @param Value until the
171 /// byte offset @param Offset is reached.
173 /// This is used to implement assembler directives such as .org.
175 /// @param Offset - The offset to reach.This may be an expression, but the
176 /// expression must be associated with the current section.
177 /// @param Value - The value to use when filling bytes.
179 // FIXME: How are we going to signal failures out of this?
180 virtual void EmitValueToOffset(const MCValue &Offset,
181 unsigned char Value = 0) = 0;
185 /// EmitInstruction - Emit the given @param Instruction into the current
187 virtual void EmitInstruction(const MCInst &Inst) = 0;
189 /// Finish - Finish emission of machine code and flush any output.
190 virtual void Finish() = 0;
193 /// createAsmStreamer - Create a machine code streamer which will print out
194 /// assembly for the native target, suitable for compiling with a native
196 MCStreamer *createAsmStreamer(MCContext &Ctx, raw_ostream &OS);
198 // FIXME: These two may end up getting rolled into a single
199 // createObjectStreamer interface, which implements the assembler backend, and
200 // is parameterized on an output object file writer.
202 /// createMachOStream - Create a machine code streamer which will generative
203 /// Mach-O format object files.
204 MCStreamer *createMachOStreamer(MCContext &Ctx, raw_ostream &OS);
206 /// createELFStreamer - Create a machine code streamer which will generative
207 /// ELF format object files.
208 MCStreamer *createELFStreamer(MCContext &Ctx, raw_ostream &OS);
210 } // end namespace llvm