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/CodeGen/MachineCodeEmitter.h"
28 class MachineBasicBlock;
29 class MachineConstantPool;
30 class MachineJumpTableInfo;
31 class MachineFunction;
32 class MachineModuleInfo;
33 class MachineRelocation;
38 /// JITCodeEmitter - This class defines two sorts of methods: those for
39 /// emitting the actual bytes of machine code, and those for emitting auxillary
40 /// structures, such as jump tables, relocations, etc.
42 /// Emission of machine code is complicated by the fact that we don't (in
43 /// general) know the size of the machine code that we're about to emit before
44 /// we emit it. As such, we preallocate a certain amount of memory, and set the
45 /// BufferBegin/BufferEnd pointers to the start and end of the buffer. As we
46 /// emit machine instructions, we advance the CurBufferPtr to indicate the
47 /// location of the next byte to emit. In the case of a buffer overflow (we
48 /// need to emit more machine code than we have allocated space for), the
49 /// CurBufferPtr will saturate to BufferEnd and ignore stores. Once the entire
50 /// function has been emitted, the overflow condition is checked, and if it has
51 /// occurred, more memory is allocated, and we reemit the code into it.
53 class JITCodeEmitter : public MachineCodeEmitter {
55 virtual ~JITCodeEmitter() {}
57 /// startFunction - This callback is invoked when the specified function is
58 /// about to be code generated. This initializes the BufferBegin/End/Ptr
61 virtual void startFunction(MachineFunction &F) = 0;
63 /// finishFunction - This callback is invoked when the specified function has
64 /// finished code generation. If a buffer overflow has occurred, this method
65 /// returns true (the callee is required to try again), otherwise it returns
68 virtual bool finishFunction(MachineFunction &F) = 0;
70 /// startGVStub - This callback is invoked when the JIT needs the
71 /// address of a GV (e.g. function) that has not been code generated yet.
72 /// The StubSize specifies the total size required by the stub.
74 virtual void startGVStub(const GlobalValue* GV, unsigned StubSize,
75 unsigned Alignment = 1) = 0;
77 /// startGVStub - This callback is invoked when the JIT needs the address of a
78 /// GV (e.g. function) that has not been code generated yet. Buffer points to
79 /// memory already allocated for this stub.
81 virtual void startGVStub(const GlobalValue* GV, void *Buffer,
82 unsigned StubSize) = 0;
84 /// finishGVStub - This callback is invoked to terminate a GV stub.
86 virtual void *finishGVStub(const GlobalValue* F) = 0;
88 /// emitByte - This callback is invoked when a byte needs to be written to the
91 void emitByte(uint8_t B) {
92 if (CurBufferPtr != BufferEnd)
96 /// emitWordLE - This callback is invoked when a 32-bit word needs to be
97 /// written to the output stream in little-endian format.
99 void emitWordLE(uint32_t W) {
100 if (4 <= BufferEnd-CurBufferPtr) {
101 *CurBufferPtr++ = (uint8_t)(W >> 0);
102 *CurBufferPtr++ = (uint8_t)(W >> 8);
103 *CurBufferPtr++ = (uint8_t)(W >> 16);
104 *CurBufferPtr++ = (uint8_t)(W >> 24);
106 CurBufferPtr = BufferEnd;
110 /// emitWordBE - This callback is invoked when a 32-bit word needs to be
111 /// written to the output stream in big-endian format.
113 void emitWordBE(uint32_t W) {
114 if (4 <= BufferEnd-CurBufferPtr) {
115 *CurBufferPtr++ = (uint8_t)(W >> 24);
116 *CurBufferPtr++ = (uint8_t)(W >> 16);
117 *CurBufferPtr++ = (uint8_t)(W >> 8);
118 *CurBufferPtr++ = (uint8_t)(W >> 0);
120 CurBufferPtr = BufferEnd;
124 /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
125 /// written to the output stream in little-endian format.
127 void emitDWordLE(uint64_t W) {
128 if (8 <= BufferEnd-CurBufferPtr) {
129 *CurBufferPtr++ = (uint8_t)(W >> 0);
130 *CurBufferPtr++ = (uint8_t)(W >> 8);
131 *CurBufferPtr++ = (uint8_t)(W >> 16);
132 *CurBufferPtr++ = (uint8_t)(W >> 24);
133 *CurBufferPtr++ = (uint8_t)(W >> 32);
134 *CurBufferPtr++ = (uint8_t)(W >> 40);
135 *CurBufferPtr++ = (uint8_t)(W >> 48);
136 *CurBufferPtr++ = (uint8_t)(W >> 56);
138 CurBufferPtr = BufferEnd;
142 /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
143 /// written to the output stream in big-endian format.
145 void emitDWordBE(uint64_t W) {
146 if (8 <= BufferEnd-CurBufferPtr) {
147 *CurBufferPtr++ = (uint8_t)(W >> 56);
148 *CurBufferPtr++ = (uint8_t)(W >> 48);
149 *CurBufferPtr++ = (uint8_t)(W >> 40);
150 *CurBufferPtr++ = (uint8_t)(W >> 32);
151 *CurBufferPtr++ = (uint8_t)(W >> 24);
152 *CurBufferPtr++ = (uint8_t)(W >> 16);
153 *CurBufferPtr++ = (uint8_t)(W >> 8);
154 *CurBufferPtr++ = (uint8_t)(W >> 0);
156 CurBufferPtr = BufferEnd;
160 /// emitAlignment - Move the CurBufferPtr pointer up the the specified
161 /// alignment (saturated to BufferEnd of course).
162 void emitAlignment(unsigned Alignment) {
163 if (Alignment == 0) Alignment = 1;
165 if(Alignment <= (uintptr_t)(BufferEnd-CurBufferPtr)) {
166 // Move the current buffer ptr up to the specified alignment.
168 (uint8_t*)(((uintptr_t)CurBufferPtr+Alignment-1) &
169 ~(uintptr_t)(Alignment-1));
171 CurBufferPtr = BufferEnd;
176 /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
177 /// written to the output stream.
178 void emitULEB128Bytes(uint64_t Value) {
180 uint8_t Byte = Value & 0x7f;
182 if (Value) Byte |= 0x80;
187 /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
188 /// written to the output stream.
189 void emitSLEB128Bytes(int64_t Value) {
190 int32_t Sign = Value >> (8 * sizeof(Value) - 1);
194 uint8_t Byte = Value & 0x7f;
196 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
197 if (IsMore) Byte |= 0x80;
202 /// emitString - This callback is invoked when a String needs to be
203 /// written to the output stream.
204 void emitString(const std::string &String) {
205 for (unsigned i = 0, N = static_cast<unsigned>(String.size());
207 uint8_t C = String[i];
213 /// emitInt32 - Emit a int32 directive.
214 void emitInt32(uint32_t Value) {
215 if (4 <= BufferEnd-CurBufferPtr) {
216 *((uint32_t*)CurBufferPtr) = Value;
219 CurBufferPtr = BufferEnd;
223 /// emitInt64 - Emit a int64 directive.
224 void emitInt64(uint64_t Value) {
225 if (8 <= BufferEnd-CurBufferPtr) {
226 *((uint64_t*)CurBufferPtr) = Value;
229 CurBufferPtr = BufferEnd;
233 /// emitInt32At - Emit the Int32 Value in Addr.
234 void emitInt32At(uintptr_t *Addr, uintptr_t Value) {
235 if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
236 (*(uint32_t*)Addr) = (uint32_t)Value;
239 /// emitInt64At - Emit the Int64 Value in Addr.
240 void emitInt64At(uintptr_t *Addr, uintptr_t Value) {
241 if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
242 (*(uint64_t*)Addr) = (uint64_t)Value;
246 /// emitLabel - Emits a label
247 virtual void emitLabel(uint64_t LabelID) = 0;
249 /// allocateSpace - Allocate a block of space in the current output buffer,
250 /// returning null (and setting conditions to indicate buffer overflow) on
251 /// failure. Alignment is the alignment in bytes of the buffer desired.
252 virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) {
253 emitAlignment(Alignment);
256 // Check for buffer overflow.
257 if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
258 CurBufferPtr = BufferEnd;
261 // Allocate the space.
262 Result = CurBufferPtr;
263 CurBufferPtr += Size;
269 /// allocateGlobal - Allocate memory for a global. Unlike allocateSpace,
270 /// this method does not allocate memory in the current output buffer,
271 /// because a global may live longer than the current function.
272 virtual void *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0;
274 /// StartMachineBasicBlock - This should be called by the target when a new
275 /// basic block is about to be emitted. This way the MCE knows where the
276 /// start of the block is, and can implement getMachineBasicBlockAddress.
277 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
279 /// getCurrentPCValue - This returns the address that the next emitted byte
280 /// will be output to.
282 virtual uintptr_t getCurrentPCValue() const {
283 return (uintptr_t)CurBufferPtr;
286 /// getCurrentPCOffset - Return the offset from the start of the emitted
287 /// buffer that we are currently writing to.
288 uintptr_t getCurrentPCOffset() const {
289 return CurBufferPtr-BufferBegin;
292 /// addRelocation - Whenever a relocatable address is needed, it should be
293 /// noted with this interface.
294 virtual void addRelocation(const MachineRelocation &MR) = 0;
296 /// FIXME: These should all be handled with relocations!
298 /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
299 /// the constant pool that was last emitted with the emitConstantPool method.
301 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
303 /// getJumpTableEntryAddress - Return the address of the jump table with index
304 /// 'Index' in the function that last called initJumpTableInfo.
306 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
308 /// getMachineBasicBlockAddress - Return the address of the specified
309 /// MachineBasicBlock, only usable after the label for the MBB has been
312 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
314 /// getLabelAddress - Return the address of the specified LabelID, only usable
315 /// after the LabelID has been emitted.
317 virtual uintptr_t getLabelAddress(uint64_t LabelID) const = 0;
319 /// Specifies the MachineModuleInfo object. This is used for exception handling
321 virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
324 } // End llvm namespace