Move the search for the appropriate AND instruction
[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 #include "llvm/ADT/DenseMap.h"
25
26 using namespace std;
27
28 namespace llvm {
29
30 class MachineBasicBlock;
31 class MachineConstantPool;
32 class MachineJumpTableInfo;
33 class MachineFunction;
34 class MachineModuleInfo;
35 class MachineRelocation;
36 class Value;
37 class GlobalValue;
38 class Function;
39   
40 /// JITCodeEmitter - This class defines two sorts of methods: those for
41 /// emitting the actual bytes of machine code, and those for emitting auxillary
42 /// structures, such as jump tables, relocations, etc.
43 ///
44 /// Emission of machine code is complicated by the fact that we don't (in
45 /// general) know the size of the machine code that we're about to emit before
46 /// we emit it.  As such, we preallocate a certain amount of memory, and set the
47 /// BufferBegin/BufferEnd pointers to the start and end of the buffer.  As we
48 /// emit machine instructions, we advance the CurBufferPtr to indicate the
49 /// location of the next byte to emit.  In the case of a buffer overflow (we
50 /// need to emit more machine code than we have allocated space for), the
51 /// CurBufferPtr will saturate to BufferEnd and ignore stores.  Once the entire
52 /// function has been emitted, the overflow condition is checked, and if it has
53 /// occurred, more memory is allocated, and we reemit the code into it.
54 /// 
55 class JITCodeEmitter : public MachineCodeEmitter {
56 public:
57   virtual ~JITCodeEmitter() {}
58
59   /// startFunction - This callback is invoked when the specified function is
60   /// about to be code generated.  This initializes the BufferBegin/End/Ptr
61   /// fields.
62   ///
63   virtual void startFunction(MachineFunction &F) = 0;
64
65   /// finishFunction - This callback is invoked when the specified function has
66   /// finished code generation.  If a buffer overflow has occurred, this method
67   /// returns true (the callee is required to try again), otherwise it returns
68   /// false.
69   ///
70   virtual bool finishFunction(MachineFunction &F) = 0;
71   
72   /// allocIndirectGV - Allocates and fills storage for an indirect
73   /// GlobalValue, and returns the address.
74   virtual void *allocIndirectGV(const GlobalValue *GV,
75                                 const uint8_t *Buffer, size_t Size,
76                                 unsigned Alignment) = 0;
77
78   /// emitByte - This callback is invoked when a byte needs to be written to the
79   /// output stream.
80   ///
81   void emitByte(uint8_t B) {
82     if (CurBufferPtr != BufferEnd)
83       *CurBufferPtr++ = B;
84   }
85
86   /// emitWordLE - This callback is invoked when a 32-bit word needs to be
87   /// written to the output stream in little-endian format.
88   ///
89   void emitWordLE(uint32_t W) {
90     if (4 <= BufferEnd-CurBufferPtr) {
91       *CurBufferPtr++ = (uint8_t)(W >>  0);
92       *CurBufferPtr++ = (uint8_t)(W >>  8);
93       *CurBufferPtr++ = (uint8_t)(W >> 16);
94       *CurBufferPtr++ = (uint8_t)(W >> 24);
95     } else {
96       CurBufferPtr = BufferEnd;
97     }
98   }
99   
100   /// emitWordBE - This callback is invoked when a 32-bit word needs to be
101   /// written to the output stream in big-endian format.
102   ///
103   void emitWordBE(uint32_t W) {
104     if (4 <= BufferEnd-CurBufferPtr) {
105       *CurBufferPtr++ = (uint8_t)(W >> 24);
106       *CurBufferPtr++ = (uint8_t)(W >> 16);
107       *CurBufferPtr++ = (uint8_t)(W >>  8);
108       *CurBufferPtr++ = (uint8_t)(W >>  0);
109     } else {
110       CurBufferPtr = BufferEnd;
111     }
112   }
113
114   /// emitDWordLE - This callback is invoked when a 64-bit word needs to be
115   /// written to the output stream in little-endian format.
116   ///
117   void emitDWordLE(uint64_t W) {
118     if (8 <= BufferEnd-CurBufferPtr) {
119       *CurBufferPtr++ = (uint8_t)(W >>  0);
120       *CurBufferPtr++ = (uint8_t)(W >>  8);
121       *CurBufferPtr++ = (uint8_t)(W >> 16);
122       *CurBufferPtr++ = (uint8_t)(W >> 24);
123       *CurBufferPtr++ = (uint8_t)(W >> 32);
124       *CurBufferPtr++ = (uint8_t)(W >> 40);
125       *CurBufferPtr++ = (uint8_t)(W >> 48);
126       *CurBufferPtr++ = (uint8_t)(W >> 56);
127     } else {
128       CurBufferPtr = BufferEnd;
129     }
130   }
131   
132   /// emitDWordBE - This callback is invoked when a 64-bit word needs to be
133   /// written to the output stream in big-endian format.
134   ///
135   void emitDWordBE(uint64_t W) {
136     if (8 <= BufferEnd-CurBufferPtr) {
137       *CurBufferPtr++ = (uint8_t)(W >> 56);
138       *CurBufferPtr++ = (uint8_t)(W >> 48);
139       *CurBufferPtr++ = (uint8_t)(W >> 40);
140       *CurBufferPtr++ = (uint8_t)(W >> 32);
141       *CurBufferPtr++ = (uint8_t)(W >> 24);
142       *CurBufferPtr++ = (uint8_t)(W >> 16);
143       *CurBufferPtr++ = (uint8_t)(W >>  8);
144       *CurBufferPtr++ = (uint8_t)(W >>  0);
145     } else {
146       CurBufferPtr = BufferEnd;
147     }
148   }
149
150   /// emitAlignment - Move the CurBufferPtr pointer up to the specified
151   /// alignment (saturated to BufferEnd of course).
152   void emitAlignment(unsigned Alignment) {
153     if (Alignment == 0) Alignment = 1;
154     uint8_t *NewPtr = (uint8_t*)RoundUpToAlignment((uintptr_t)CurBufferPtr,
155                                                    Alignment);
156     CurBufferPtr = std::min(NewPtr, BufferEnd);
157   }
158
159   /// emitAlignmentWithFill - Similar to emitAlignment, except that the
160   /// extra bytes are filled with the provided byte.
161   void emitAlignmentWithFill(unsigned Alignment, uint8_t Fill) {
162     if (Alignment == 0) Alignment = 1;
163     uint8_t *NewPtr = (uint8_t*)RoundUpToAlignment((uintptr_t)CurBufferPtr,
164                                                    Alignment);
165     // Fail if we don't have room.
166     if (NewPtr > BufferEnd) {
167       CurBufferPtr = BufferEnd;
168       return;
169     }
170     while (CurBufferPtr < NewPtr) {
171       *CurBufferPtr++ = Fill;
172     }
173   }
174
175   /// emitULEB128Bytes - This callback is invoked when a ULEB128 needs to be
176   /// written to the output stream.
177   void emitULEB128Bytes(uint64_t Value, unsigned PadTo = 0) {
178     do {
179       uint8_t Byte = Value & 0x7f;
180       Value >>= 7;
181       if (Value || PadTo != 0) Byte |= 0x80;
182       emitByte(Byte);
183     } while (Value);
184
185     if (PadTo) {
186       do {
187         uint8_t Byte = (PadTo > 1) ? 0x80 : 0x0;
188         emitByte(Byte);
189       } while (--PadTo);
190     }
191   }
192   
193   /// emitSLEB128Bytes - This callback is invoked when a SLEB128 needs to be
194   /// written to the output stream.
195   void emitSLEB128Bytes(int64_t Value) {
196     int32_t Sign = Value >> (8 * sizeof(Value) - 1);
197     bool IsMore;
198   
199     do {
200       uint8_t Byte = Value & 0x7f;
201       Value >>= 7;
202       IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
203       if (IsMore) Byte |= 0x80;
204       emitByte(Byte);
205     } while (IsMore);
206   }
207
208   /// emitString - This callback is invoked when a String needs to be
209   /// written to the output stream.
210   void emitString(const std::string &String) {
211     for (unsigned i = 0, N = static_cast<unsigned>(String.size());
212          i < N; ++i) {
213       uint8_t C = String[i];
214       emitByte(C);
215     }
216     emitByte(0);
217   }
218   
219   /// emitInt32 - Emit a int32 directive.
220   void emitInt32(uint32_t Value) {
221     if (4 <= BufferEnd-CurBufferPtr) {
222       *((uint32_t*)CurBufferPtr) = Value;
223       CurBufferPtr += 4;
224     } else {
225       CurBufferPtr = BufferEnd;
226     }
227   }
228
229   /// emitInt64 - Emit a int64 directive.
230   void emitInt64(uint64_t Value) {
231     if (8 <= BufferEnd-CurBufferPtr) {
232       *((uint64_t*)CurBufferPtr) = Value;
233       CurBufferPtr += 8;
234     } else {
235       CurBufferPtr = BufferEnd;
236     }
237   }
238   
239   /// emitInt32At - Emit the Int32 Value in Addr.
240   void emitInt32At(uintptr_t *Addr, uintptr_t Value) {
241     if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
242       (*(uint32_t*)Addr) = (uint32_t)Value;
243   }
244   
245   /// emitInt64At - Emit the Int64 Value in Addr.
246   void emitInt64At(uintptr_t *Addr, uintptr_t Value) {
247     if (Addr >= (uintptr_t*)BufferBegin && Addr < (uintptr_t*)BufferEnd)
248       (*(uint64_t*)Addr) = (uint64_t)Value;
249   }
250   
251   
252   /// emitLabel - Emits a label
253   virtual void emitLabel(MCSymbol *Label) = 0;
254
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);
260     void *Result;
261     
262     // Check for buffer overflow.
263     if (Size >= (uintptr_t)(BufferEnd-CurBufferPtr)) {
264       CurBufferPtr = BufferEnd;
265       Result = 0;
266     } else {
267       // Allocate the space.
268       Result = CurBufferPtr;
269       CurBufferPtr += Size;
270     }
271     
272     return Result;
273   }
274
275   /// allocateGlobal - Allocate memory for a global.  Unlike allocateSpace,
276   /// this method does not allocate memory in the current output buffer,
277   /// because a global may live longer than the current function.
278   virtual void *allocateGlobal(uintptr_t Size, unsigned Alignment) = 0;
279
280   /// StartMachineBasicBlock - This should be called by the target when a new
281   /// basic block is about to be emitted.  This way the MCE knows where the
282   /// start of the block is, and can implement getMachineBasicBlockAddress.
283   virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) = 0;
284   
285   /// getCurrentPCValue - This returns the address that the next emitted byte
286   /// will be output to.
287   ///
288   virtual uintptr_t getCurrentPCValue() const {
289     return (uintptr_t)CurBufferPtr;
290   }
291
292   /// getCurrentPCOffset - Return the offset from the start of the emitted
293   /// buffer that we are currently writing to.
294   uintptr_t getCurrentPCOffset() const {
295     return CurBufferPtr-BufferBegin;
296   }
297
298   /// earlyResolveAddresses - True if the code emitter can use symbol addresses 
299   /// during code emission time. The JIT is capable of doing this because it
300   /// creates jump tables or constant pools in memory on the fly while the
301   /// object code emitters rely on a linker to have real addresses and should
302   /// use relocations instead.
303   bool earlyResolveAddresses() const { return true; }
304
305   /// addRelocation - Whenever a relocatable address is needed, it should be
306   /// noted with this interface.
307   virtual void addRelocation(const MachineRelocation &MR) = 0;
308   
309   /// FIXME: These should all be handled with relocations!
310   
311   /// getConstantPoolEntryAddress - Return the address of the 'Index' entry in
312   /// the constant pool that was last emitted with the emitConstantPool method.
313   ///
314   virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const = 0;
315
316   /// getJumpTableEntryAddress - Return the address of the jump table with index
317   /// 'Index' in the function that last called initJumpTableInfo.
318   ///
319   virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const = 0;
320   
321   /// getMachineBasicBlockAddress - Return the address of the specified
322   /// MachineBasicBlock, only usable after the label for the MBB has been
323   /// emitted.
324   ///
325   virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const= 0;
326
327   /// getLabelAddress - Return the address of the specified Label, only usable
328   /// after the Label has been emitted.
329   ///
330   virtual uintptr_t getLabelAddress(MCSymbol *Label) const = 0;
331   
332   /// Specifies the MachineModuleInfo object. This is used for exception handling
333   /// purposes.
334   virtual void setModuleInfo(MachineModuleInfo* Info) = 0;
335
336   /// getLabelLocations - Return the label locations map of the label IDs to
337   /// their address.
338   virtual DenseMap<MCSymbol*, uintptr_t> *getLabelLocations() { return 0; }
339 };
340
341 } // End llvm namespace
342
343 #endif