1 //===-- llvm/CodeGen/JITCodeEmitter.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_JITCODEEMITTER_H
18 #define LLVM_CODEGEN_JITCODEEMITTER_H
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/Streams.h"
23 #include "llvm/CodeGen/MachineCodeEmitter.h"
29 class MachineBasicBlock;
30 class MachineConstantPool;
31 class MachineJumpTableInfo;
32 class MachineFunction;
33 class MachineModuleInfo;
34 class MachineRelocation;
39 /// JITCodeEmitter - This class defines two sorts of methods: those for
40 /// emitting the actual bytes of machine code, and those for emitting auxillary
41 /// structures, such as jump tables, relocations, etc.
43 /// Emission of machine code is complicated by the fact that we don't (in
44 /// general) know the size of the machine code that we're about to emit before
45 /// we emit it. As such, we preallocate a certain amount of memory, and set the
46 /// BufferBegin/BufferEnd pointers to the start and end of the buffer. As we
47 /// emit machine instructions, we advance the CurBufferPtr to indicate the
48 /// location of the next byte to emit. In the case of a buffer overflow (we
49 /// need to emit more machine code than we have allocated space for), the
50 /// CurBufferPtr will saturate to BufferEnd and ignore stores. Once the entire
51 /// function has been emitted, the overflow condition is checked, and if it has
52 /// occurred, more memory is allocated, and we reemit the code into it.
54 class JITCodeEmitter : public MachineCodeEmitter {
56 virtual ~JITCodeEmitter() {}
58 /// startFunction - This callback is invoked when the specified function is
59 /// about to be code generated. This initializes the BufferBegin/End/Ptr
62 virtual void startFunction(MachineFunction &F) = 0;
64 /// finishFunction - This callback is invoked when the specified function has
65 /// finished code generation. If a buffer overflow has occurred, this method
66 /// returns true (the callee is required to try again), otherwise it returns
69 virtual bool finishFunction(MachineFunction &F) = 0;
71 /// startGVStub - This callback is invoked when the JIT needs the
72 /// address of a GV (e.g. function) that has not been code generated yet.
73 /// The StubSize specifies the total size required by the stub.
75 virtual void startGVStub(const GlobalValue* GV, unsigned StubSize,
76 unsigned Alignment = 1) = 0;
78 /// startGVStub - This callback is invoked when the JIT needs the address of a
79 /// GV (e.g. function) that has not been code generated yet. Buffer points to
80 /// memory already allocated for this stub.
82 virtual void startGVStub(const GlobalValue* GV, void *Buffer,
83 unsigned StubSize) = 0;
85 /// finishGVStub - This callback is invoked to terminate a GV stub.
87 virtual void *finishGVStub(const GlobalValue* F) = 0;
89 /// emitByte - This callback is invoked when a byte needs to be written to the
92 void emitByte(uint8_t B) {
93 if (CurBufferPtr != BufferEnd)
97 /// emitWordLE - This callback is invoked when a 32-bit word needs to be
98 /// written to the output stream in little-endian format.
100 void emitWordLE(unsigned W) {
101 if (4 <= BufferEnd-CurBufferPtr) {
102 *CurBufferPtr++ = (uint8_t)(W >> 0);
103 *CurBufferPtr++ = (uint8_t)(W >> 8);
104 *CurBufferPtr++ = (uint8_t)(W >> 16);
105 *CurBufferPtr++ = (uint8_t)(W >> 24);
107 CurBufferPtr = BufferEnd;
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(unsigned 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 the 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(unsigned 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(int32_t Value) {
191 int32_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;
247 /// emitLabel - Emits a label
248 virtual void emitLabel(uint64_t LabelID) = 0;
250 /// allocateSpace - Allocate a block of space in the current output buffer,
251 /// returning null (and setting conditions to indicate buffer overflow) on
252 /// failure. Alignment is the alignment in bytes of the buffer desired.
253 virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) {
254 emitAlignment(Alignment);
257 // Check for buffer overflow.
258 if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
259 CurBufferPtr = BufferEnd;
262 // Allocate the space.
263 Result = CurBufferPtr;
264 CurBufferPtr += Size;
270 /// StartMachineBasicBlock - This should be called by the target when a new
271 /// basic block is about to be emitted. This way the MCE knows where the
272 /// start of the block is, and can implement getMachineBasicBlockAddress.
273 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
275 /// getCurrentPCValue - This returns the address that the next emitted byte
276 /// will be output to.
278 virtual uintptr_t getCurrentPCValue() const {
279 return (uintptr_t)CurBufferPtr;
282 /// getCurrentPCOffset - Return the offset from the start of the emitted
283 /// buffer that we are currently writing to.
284 uintptr_t getCurrentPCOffset() const {
285 return CurBufferPtr-BufferBegin;
288 /// addRelocation - Whenever a relocatable address is needed, it should be
289 /// noted with this interface.
290 virtual void addRelocation(const MachineRelocation &MR) = 0;
292 /// FIXME: These should all be handled with relocations!
294 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
295 /// the constant pool that was last emitted with the emitConstantPool method.
297 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
299 /// getJumpTableEntryAddress - Return the address of the jump table with index
300 /// 'Index' in the function that last called initJumpTableInfo.
302 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
304 /// getMachineBasicBlockAddress - Return the address of the specified
305 /// MachineBasicBlock, only usable after the label for the MBB has been
308 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
310 /// getLabelAddress - Return the address of the specified LabelID, only usable
311 /// after the LabelID has been emitted.
313 virtual uintptr_t getLabelAddress(uint64_t LabelID) const = 0;
315 /// Specifies the MachineModuleInfo object. This is used for exception handling
317 virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
320 } // End llvm namespace