Move DataTypes.h to include/llvm/System, update all users. This breaks the last
[oota-llvm.git] / include / llvm / CodeGen / JITCodeEmitter.h
1 //===-- llvm/CodeGen/JITCodeEmitter.h - Code emission ----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.).
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_CODEGEN_JITCODEEMITTER_H
18 #define LLVM_CODEGEN_JITCODEEMITTER_H
19
20 #include <string>
21 #include "llvm/System/DataTypes.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/CodeGen/MachineCodeEmitter.h"
24
25 using namespace std;
26
27 namespace llvm {
28
29 class MachineBasicBlock;
30 class MachineConstantPool;
31 class MachineJumpTableInfo;
32 class MachineFunction;
33 class MachineModuleInfo;
34 class MachineRelocation;
35 class Value;
36 class GlobalValue;
37 class Function;
38
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.
42 ///
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.
53 /// 
54 class JITCodeEmitter : public MachineCodeEmitter {
55 public:
56   virtual ~JITCodeEmitter() {}
57
58   /// startFunction - This callback is invoked when the specified function is
59   /// about to be code generated.  This initializes the BufferBegin/End/Ptr
60   /// fields.
61   ///
62   virtual void startFunction(MachineFunction &F) = 0;
63
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
67   /// false.
68   ///
69   virtual bool finishFunction(MachineFunction &F) = 0;
70   
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.
74   ///
75   virtual void startGVStub(const GlobalValue* GV, unsigned StubSize,
76                            unsigned Alignment = 1) = 0;
77
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.
81   ///
82   virtual void startGVStub(const GlobalValue* GV, void *Buffer,
83                            unsigned StubSize) = 0;
84   
85   /// finishGVStub - This callback is invoked to terminate a GV stub.
86   ///
87   virtual void *finishGVStub(const GlobalValue* F) = 0;
88
89   /// emitByte - This callback is invoked when a byte needs to be written to the
90   /// output stream.
91   ///
92   void emitByte(uint8_t B) {
93     if (CurBufferPtr != BufferEnd)
94       *CurBufferPtr++ = B;
95   }
96
97   /// emitWordLE - This callback is invoked when a 32-bit word needs to be
98   /// written to the output stream in little-endian format.
99   ///
100   void emitWordLE(uint32_t 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);
106     } else {
107       CurBufferPtr = BufferEnd;
108     }
109   }
110   
111   /// emitWordBE - This callback is invoked when a 32-bit word needs to be
112   /// written to the output stream in big-endian format.
113   ///
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);
120     } else {
121       CurBufferPtr = BufferEnd;
122     }
123   }
124
125   /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
126   /// written to the output stream in little-endian format.
127   ///
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);
138     } else {
139       CurBufferPtr = BufferEnd;
140     }
141   }
142   
143   /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
144   /// written to the output stream in big-endian format.
145   ///
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);
156     } else {
157       CurBufferPtr = BufferEnd;
158     }
159   }
160
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;
165     uint8_t *NewPtr = (uint8_t*)RoundUpToAlignment((uintptr_t)CurBufferPtr,
166                                                    Alignment);
167     CurBufferPtr = std::min(NewPtr, BufferEnd);
168   }
169
170   /// emitAlignmentWithFill - Similar to emitAlignment, except that the
171   /// extra bytes are filled with the provided byte.
172   void emitAlignmentWithFill(unsigned Alignment, uint8_t Fill) {
173     if (Alignment == 0) Alignment = 1;
174     uint8_t *NewPtr = (uint8_t*)RoundUpToAlignment((uintptr_t)CurBufferPtr,
175                                                    Alignment);
176     // Fail if we don't have room.
177     if (NewPtr > BufferEnd) {
178       CurBufferPtr = BufferEnd;
179       return;
180     }
181     while (CurBufferPtr < NewPtr) {
182       *CurBufferPtr++ = Fill;
183     }
184   }
185
186   /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
187   /// written to the output stream.
188   void emitULEB128Bytes(uint64_t Value) {
189     do {
190       uint8_t Byte = Value & 0x7f;
191       Value >>= 7;
192       if (Value) Byte |= 0x80;
193       emitByte(Byte);
194     } while (Value);
195   }
196   
197   /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
198   /// written to the output stream.
199   void emitSLEB128Bytes(int64_t Value) {
200     int32_t Sign = Value >> (8 * sizeof(Value) - 1);
201     bool IsMore;
202   
203     do {
204       uint8_t Byte = Value & 0x7f;
205       Value >>= 7;
206       IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
207       if (IsMore) Byte |= 0x80;
208       emitByte(Byte);
209     } while (IsMore);
210   }
211
212   /// emitString - This callback is invoked when a String needs to be
213   /// written to the output stream.
214   void emitString(const std::string &String) {
215     for (unsigned i = 0, N = static_cast<unsigned>(String.size());
216          i < N; ++i) {
217       uint8_t C = String[i];
218       emitByte(C);
219     }
220     emitByte(0);
221   }
222   
223   /// emitInt32 - Emit a int32 directive.
224   void emitInt32(uint32_t Value) {
225     if (4 <= BufferEnd-CurBufferPtr) {
226       *((uint32_t*)CurBufferPtr) = Value;
227       CurBufferPtr += 4;
228     } else {
229       CurBufferPtr = BufferEnd;
230     }
231   }
232
233   /// emitInt64 - Emit a int64 directive.
234   void emitInt64(uint64_t Value) {
235     if (8 <= BufferEnd-CurBufferPtr) {
236       *((uint64_t*)CurBufferPtr) = Value;
237       CurBufferPtr += 8;
238     } else {
239       CurBufferPtr = BufferEnd;
240     }
241   }
242   
243   /// emitInt32At - Emit the Int32 Value in Addr.
244   void emitInt32At(uintptr_t *Addr, uintptr_t Value) {
245     if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
246       (*(uint32_t*)Addr) = (uint32_t)Value;
247   }
248   
249   /// emitInt64At - Emit the Int64 Value in Addr.
250   void emitInt64At(uintptr_t *Addr, uintptr_t Value) {
251     if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
252       (*(uint64_t*)Addr) = (uint64_t)Value;
253   }
254   
255   
256   /// emitLabel - Emits a label
257   virtual void emitLabel(uint64_t LabelID) = 0;
258
259   /// allocateSpace - Allocate a block of space in the current output buffer,
260   /// returning null (and setting conditions to indicate buffer overflow) on
261   /// failure.  Alignment is the alignment in bytes of the buffer desired.
262   virtual void *allocateSpace(uintptr_t Size, unsigned Alignment) {
263     emitAlignment(Alignment);
264     void *Result;
265     
266     // Check for buffer overflow.
267     if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
268       CurBufferPtr = BufferEnd;
269       Result = 0;
270     } else {
271       // Allocate the space.
272       Result = CurBufferPtr;
273       CurBufferPtr += Size;
274     }
275     
276     return Result;
277   }
278
279   /// allocateGlobal - Allocate memory for a global.  Unlike allocateSpace,
280   /// this method does not allocate memory in the current output buffer,
281   /// because a global may live longer than the current function.
282   virtual void *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0;
283
284   /// StartMachineBasicBlock - This should be called by the target when a new
285   /// basic block is about to be emitted.  This way the MCE knows where the
286   /// start of the block is, and can implement getMachineBasicBlockAddress.
287   virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
288   
289   /// getCurrentPCValue - This returns the address that the next emitted byte
290   /// will be output to.
291   ///
292   virtual uintptr_t getCurrentPCValue() const {
293     return (uintptr_t)CurBufferPtr;
294   }
295
296   /// getCurrentPCOffset - Return the offset from the start of the emitted
297   /// buffer that we are currently writing to.
298   uintptr_t getCurrentPCOffset() const {
299     return CurBufferPtr-BufferBegin;
300   }
301
302   /// earlyResolveAddresses - True if the code emitter can use symbol addresses 
303   /// during code emission time. The JIT is capable of doing this because it
304   /// creates jump tables or constant pools in memory on the fly while the
305   /// object code emitters rely on a linker to have real addresses and should
306   /// use relocations instead.
307   bool earlyResolveAddresses() const { return true; }
308
309   /// addRelocation - Whenever a relocatable address is needed, it should be
310   /// noted with this interface.
311   virtual void addRelocation(const MachineRelocation &MR) = 0;
312   
313   /// FIXME: These should all be handled with relocations!
314   
315   /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
316   /// the constant pool that was last emitted with the emitConstantPool method.
317   ///
318   virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
319
320   /// getJumpTableEntryAddress - Return the address of the jump table with index
321   /// 'Index' in the function that last called initJumpTableInfo.
322   ///
323   virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
324   
325   /// getMachineBasicBlockAddress - Return the address of the specified
326   /// MachineBasicBlock, only usable after the label for the MBB has been
327   /// emitted.
328   ///
329   virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
330
331   /// getLabelAddress - Return the address of the specified LabelID, only usable
332   /// after the LabelID has been emitted.
333   ///
334   virtual uintptr_t getLabelAddress(uint64_t LabelID) const = 0;
335   
336   /// Specifies the MachineModuleInfo object. This is used for exception handling
337   /// purposes.
338   virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
339 };
340
341 } // End llvm namespace
342
343 #endif