1 //===-- llvm/CodeGen/MachineCodeEmitter.h - Code emission -------*- 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 defines an abstract interface that is used by the machine code
11 // emission framework to output the code. This allows machine code emission to
12 // be separated from concerns such as resolution of call targets, and where the
13 // machine code will be written (memory or disk, f.e.).
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_CODEGEN_MACHINECODEEMITTER_H
18 #define LLVM_CODEGEN_MACHINECODEEMITTER_H
20 #include "llvm/Support/DataTypes.h"
21 #include "llvm/Support/DebugLoc.h"
26 class MachineBasicBlock;
27 class MachineConstantPool;
28 class MachineJumpTableInfo;
29 class MachineFunction;
30 class MachineModuleInfo;
31 class MachineRelocation;
37 /// MachineCodeEmitter - This class defines two sorts of methods: those for
38 /// emitting the actual bytes of machine code, and those for emitting auxiliary
39 /// structures, such as jump tables, relocations, etc.
41 /// Emission of machine code is complicated by the fact that we don't (in
42 /// general) know the size of the machine code that we're about to emit before
43 /// we emit it. As such, we preallocate a certain amount of memory, and set the
44 /// BufferBegin/BufferEnd pointers to the start and end of the buffer. As we
45 /// emit machine instructions, we advance the CurBufferPtr to indicate the
46 /// location of the next byte to emit. In the case of a buffer overflow (we
47 /// need to emit more machine code than we have allocated space for), the
48 /// CurBufferPtr will saturate to BufferEnd and ignore stores. Once the entire
49 /// function has been emitted, the overflow condition is checked, and if it has
50 /// occurred, more memory is allocated, and we reemit the code into it.
52 class MachineCodeEmitter {
53 virtual void anchor();
55 /// BufferBegin/BufferEnd - Pointers to the start and end of the memory
56 /// allocated for this code buffer.
57 uint8_t *BufferBegin, *BufferEnd;
58 /// CurBufferPtr - Pointer to the next byte of memory to fill when emitting
59 /// code. This is guaranteed to be in the range [BufferBegin,BufferEnd]. If
60 /// this pointer is at BufferEnd, it will never move due to code emission, and
61 /// all code emission requests will be ignored (this is the buffer overflow
63 uint8_t *CurBufferPtr;
66 virtual ~MachineCodeEmitter() {}
68 /// startFunction - This callback is invoked when the specified function is
69 /// about to be code generated. This initializes the BufferBegin/End/Ptr
72 virtual void startFunction(MachineFunction &F) = 0;
74 /// finishFunction - This callback is invoked when the specified function has
75 /// finished code generation. If a buffer overflow has occurred, this method
76 /// returns true (the callee is required to try again), otherwise it returns
79 virtual bool finishFunction(MachineFunction &F) = 0;
81 /// emitByte - This callback is invoked when a byte needs to be written to the
84 void emitByte(uint8_t B) {
85 if (CurBufferPtr != BufferEnd)
89 /// emitWordLE - This callback is invoked when a 32-bit word needs to be
90 /// written to the output stream in little-endian format.
92 void emitWordLE(uint32_t W) {
93 if (4 <= BufferEnd-CurBufferPtr) {
94 emitWordLEInto(CurBufferPtr, W);
96 CurBufferPtr = BufferEnd;
100 /// emitWordLEInto - This callback is invoked when a 32-bit word needs to be
101 /// written to an arbitrary buffer in little-endian format. Buf must have at
102 /// least 4 bytes of available space.
104 static void emitWordLEInto(uint8_t *&Buf, uint32_t W) {
105 *Buf++ = (uint8_t)(W >> 0);
106 *Buf++ = (uint8_t)(W >> 8);
107 *Buf++ = (uint8_t)(W >> 16);
108 *Buf++ = (uint8_t)(W >> 24);
111 /// emitWordBE - This callback is invoked when a 32-bit word needs to be
112 /// written to the output stream in big-endian format.
114 void emitWordBE(uint32_t W) {
115 if (4 <= BufferEnd-CurBufferPtr) {
116 *CurBufferPtr++ = (uint8_t)(W >> 24);
117 *CurBufferPtr++ = (uint8_t)(W >> 16);
118 *CurBufferPtr++ = (uint8_t)(W >> 8);
119 *CurBufferPtr++ = (uint8_t)(W >> 0);
121 CurBufferPtr = BufferEnd;
125 /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
126 /// written to the output stream in little-endian format.
128 void emitDWordLE(uint64_t W) {
129 if (8 <= BufferEnd-CurBufferPtr) {
130 *CurBufferPtr++ = (uint8_t)(W >> 0);
131 *CurBufferPtr++ = (uint8_t)(W >> 8);
132 *CurBufferPtr++ = (uint8_t)(W >> 16);
133 *CurBufferPtr++ = (uint8_t)(W >> 24);
134 *CurBufferPtr++ = (uint8_t)(W >> 32);
135 *CurBufferPtr++ = (uint8_t)(W >> 40);
136 *CurBufferPtr++ = (uint8_t)(W >> 48);
137 *CurBufferPtr++ = (uint8_t)(W >> 56);
139 CurBufferPtr = BufferEnd;
143 /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
144 /// written to the output stream in big-endian format.
146 void emitDWordBE(uint64_t W) {
147 if (8 <= BufferEnd-CurBufferPtr) {
148 *CurBufferPtr++ = (uint8_t)(W >> 56);
149 *CurBufferPtr++ = (uint8_t)(W >> 48);
150 *CurBufferPtr++ = (uint8_t)(W >> 40);
151 *CurBufferPtr++ = (uint8_t)(W >> 32);
152 *CurBufferPtr++ = (uint8_t)(W >> 24);
153 *CurBufferPtr++ = (uint8_t)(W >> 16);
154 *CurBufferPtr++ = (uint8_t)(W >> 8);
155 *CurBufferPtr++ = (uint8_t)(W >> 0);
157 CurBufferPtr = BufferEnd;
161 /// emitAlignment - Move the CurBufferPtr pointer up to the specified
162 /// alignment (saturated to BufferEnd of course).
163 void emitAlignment(unsigned Alignment) {
164 if (Alignment == 0) Alignment = 1;
166 if(Alignment <= (uintptr_t)(BufferEnd-CurBufferPtr)) {
167 // Move the current buffer ptr up to the specified alignment.
169 (uint8_t*)(((uintptr_t)CurBufferPtr+Alignment-1) &
170 ~(uintptr_t)(Alignment-1));
172 CurBufferPtr = BufferEnd;
177 /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
178 /// written to the output stream.
179 void emitULEB128Bytes(uint64_t Value) {
181 uint8_t Byte = Value & 0x7f;
183 if (Value) Byte |= 0x80;
188 /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
189 /// written to the output stream.
190 void emitSLEB128Bytes(uint64_t Value) {
191 uint64_t Sign = Value >> (8 * sizeof(Value) - 1);
195 uint8_t Byte = Value & 0x7f;
197 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
198 if (IsMore) Byte |= 0x80;
203 /// emitString - This callback is invoked when a String needs to be
204 /// written to the output stream.
205 void emitString(const std::string &String) {
206 for (unsigned i = 0, N = static_cast<unsigned>(String.size());
208 uint8_t C = String[i];
214 /// emitInt32 - Emit a int32 directive.
215 void emitInt32(int32_t Value) {
216 if (4 <= BufferEnd-CurBufferPtr) {
217 *((uint32_t*)CurBufferPtr) = Value;
220 CurBufferPtr = BufferEnd;
224 /// emitInt64 - Emit a int64 directive.
225 void emitInt64(uint64_t Value) {
226 if (8 <= BufferEnd-CurBufferPtr) {
227 *((uint64_t*)CurBufferPtr) = Value;
230 CurBufferPtr = BufferEnd;
234 /// emitInt32At - Emit the Int32 Value in Addr.
235 void emitInt32At(uintptr_t *Addr, uintptr_t Value) {
236 if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
237 (*(uint32_t*)Addr) = (uint32_t)Value;
240 /// emitInt64At - Emit the Int64 Value in Addr.
241 void emitInt64At(uintptr_t *Addr, uintptr_t Value) {
242 if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
243 (*(uint64_t*)Addr) = (uint64_t)Value;
246 /// processDebugLoc - Records debug location information about a
247 /// MachineInstruction. This is called before emitting any bytes associated
248 /// with the instruction. Even if successive instructions have the same debug
249 /// location, this method will be called for each one.
250 virtual void processDebugLoc(DebugLoc DL, bool BeforePrintintInsn) {}
252 /// emitLabel - Emits a label
253 virtual void emitLabel(MCSymbol *Label) = 0;
255 /// allocateSpace - Allocate a block of space in the current output buffer,
256 /// returning null (and setting conditions to indicate buffer overflow) on
257 /// failure. Alignment is the alignment in bytes of the buffer desired.
258 virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) {
259 emitAlignment(Alignment);
262 // Check for buffer overflow.
263 if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
264 CurBufferPtr = BufferEnd;
267 // Allocate the space.
268 Result = CurBufferPtr;
269 CurBufferPtr += Size;
275 /// StartMachineBasicBlock - This should be called by the target when a new
276 /// basic block is about to be emitted. This way the MCE knows where the
277 /// start of the block is, and can implement getMachineBasicBlockAddress.
278 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
280 /// getCurrentPCValue - This returns the address that the next emitted byte
281 /// will be output to.
283 virtual uintptr_t getCurrentPCValue() const {
284 return (uintptr_t)CurBufferPtr;
287 /// getCurrentPCOffset - Return the offset from the start of the emitted
288 /// buffer that we are currently writing to.
289 virtual uintptr_t getCurrentPCOffset() const {
290 return CurBufferPtr-BufferBegin;
293 /// earlyResolveAddresses - True if the code emitter can use symbol addresses
294 /// during code emission time. The JIT is capable of doing this because it
295 /// creates jump tables or constant pools in memory on the fly while the
296 /// object code emitters rely on a linker to have real addresses and should
297 /// use relocations instead.
298 virtual bool earlyResolveAddresses() const = 0;
300 /// addRelocation - Whenever a relocatable address is needed, it should be
301 /// noted with this interface.
302 virtual void addRelocation(const MachineRelocation &MR) = 0;
304 /// FIXME: These should all be handled with relocations!
306 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
307 /// the constant pool that was last emitted with the emitConstantPool method.
309 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
311 /// getJumpTableEntryAddress - Return the address of the jump table with index
312 /// 'Index' in the function that last called initJumpTableInfo.
314 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
316 /// getMachineBasicBlockAddress - Return the address of the specified
317 /// MachineBasicBlock, only usable after the label for the MBB has been
320 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
322 /// getLabelAddress - Return the address of the specified Label, only usable
323 /// after the LabelID has been emitted.
325 virtual uintptr_t getLabelAddress(MCSymbol *Label) const = 0;
327 /// Specifies the MachineModuleInfo object. This is used for exception handling
329 virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
332 } // End llvm namespace