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