84f33a7974af7ba81110ce8351db870b1b21e8c9
[oota-llvm.git] / lib / ExecutionEngine / JIT / JITEmitter.cpp
1 //===-- Emitter.cpp - Write machine code to executable memory -------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a MachineCodeEmitter object that is used by the JIT to
11 // write machine code to memory and remember where relocatable values are.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "jit"
16 #include "JIT.h"
17 #include "llvm/Constant.h"
18 #include "llvm/Module.h"
19 #include "llvm/CodeGen/MachineCodeEmitter.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineRelocation.h"
23 #include "llvm/Target/TargetData.h"
24 #include "llvm/Target/TargetJITInfo.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/System/Memory.h"
28 using namespace llvm;
29
30 namespace {
31   Statistic<> NumBytes("jit", "Number of bytes of machine code compiled");
32   JIT *TheJIT = 0;
33 }
34
35
36 //===----------------------------------------------------------------------===//
37 // JITMemoryManager code.
38 //
39 namespace {
40   /// JITMemoryManager - Manage memory for the JIT code generation in a logical,
41   /// sane way.  This splits a large block of MAP_NORESERVE'd memory into two
42   /// sections, one for function stubs, one for the functions themselves.  We
43   /// have to do this because we may need to emit a function stub while in the
44   /// middle of emitting a function, and we don't know how large the function we
45   /// are emitting is.  This never bothers to release the memory, because when
46   /// we are ready to destroy the JIT, the program exits.
47   class JITMemoryManager {
48     sys::MemoryBlock  MemBlock;  // Virtual memory block allocated RWX
49     unsigned char *MemBase;      // Base of block of memory, start of stub mem
50     unsigned char *FunctionBase; // Start of the function body area
51     unsigned char *CurStubPtr, *CurFunctionPtr;
52   public:
53     JITMemoryManager();
54     
55     inline unsigned char *allocateStub(unsigned StubSize);
56     inline unsigned char *startFunctionBody();
57     inline void endFunctionBody(unsigned char *FunctionEnd);    
58   };
59 }
60
61 JITMemoryManager::JITMemoryManager() {
62   // Allocate a 16M block of memory...
63   MemBlock = sys::Memory::AllocateRWX((16 << 20));
64   MemBase = reinterpret_cast<unsigned char*>(MemBlock.base());
65   FunctionBase = MemBase + 512*1024; // Use 512k for stubs
66
67   // Allocate stubs backwards from the function base, allocate functions forward
68   // from the function base.
69   CurStubPtr = CurFunctionPtr = FunctionBase;
70 }
71
72 unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) {
73   CurStubPtr -= StubSize;
74   if (CurStubPtr < MemBase) {
75     std::cerr << "JIT ran out of memory for function stubs!\n";
76     abort();
77   }
78   return CurStubPtr;
79 }
80
81 unsigned char *JITMemoryManager::startFunctionBody() {
82   // Round up to an even multiple of 8 bytes, this should eventually be target
83   // specific.
84   return (unsigned char*)(((intptr_t)CurFunctionPtr + 7) & ~7);
85 }
86
87 void JITMemoryManager::endFunctionBody(unsigned char *FunctionEnd) {
88   assert(FunctionEnd > CurFunctionPtr);
89   CurFunctionPtr = FunctionEnd;
90 }
91
92 //===----------------------------------------------------------------------===//
93 // JIT lazy compilation code.
94 //
95 namespace {
96   /// JITResolver - Keep track of, and resolve, call sites for functions that
97   /// have not yet been compiled.
98   class JITResolver {
99     /// The MCE to use to emit stubs with.
100     MachineCodeEmitter &MCE;
101
102     // FunctionToStubMap - Keep track of the stub created for a particular
103     // function so that we can reuse them if necessary.
104     std::map<Function*, void*> FunctionToStubMap;
105
106     // StubToFunctionMap - Keep track of the function that each stub corresponds
107     // to.
108     std::map<void*, Function*> StubToFunctionMap;
109
110   public:
111     JITResolver(MachineCodeEmitter &mce) : MCE(mce) {}
112
113     /// getFunctionStub - This returns a pointer to a function stub, creating
114     /// one on demand as needed.
115     void *getFunctionStub(Function *F);
116
117     /// JITCompilerFn - This function is called to resolve a stub to a compiled
118     /// address.  If the LLVM Function corresponding to the stub has not yet
119     /// been compiled, this function compiles it first.
120     static void *JITCompilerFn(void *Stub);
121   };
122 }
123
124 /// getJITResolver - This function returns the one instance of the JIT resolver.
125 ///
126 static JITResolver &getJITResolver(MachineCodeEmitter *MCE = 0) {
127   static JITResolver TheJITResolver(*MCE);
128   return TheJITResolver;
129 }
130
131 /// getFunctionStub - This returns a pointer to a function stub, creating
132 /// one on demand as needed.
133 void *JITResolver::getFunctionStub(Function *F) {
134   /// Get the target-specific JIT resolver function.
135   static TargetJITInfo::LazyResolverFn LazyResolverFn =
136     TheJIT->getJITInfo().getLazyResolverFunction(JITResolver::JITCompilerFn);
137
138   // If we already have a stub for this function, recycle it.
139   void *&Stub = FunctionToStubMap[F];
140   if (Stub) return Stub;
141
142   // Otherwise, codegen a new stub.  For now, the stub will call the lazy
143   // resolver function.
144   Stub = TheJIT->getJITInfo().emitFunctionStub((void*)LazyResolverFn, MCE);
145
146   // Finally, keep track of the stub-to-Function mapping so that the
147   // JITCompilerFn knows which function to compile!
148   StubToFunctionMap[Stub] = F;
149   return Stub;
150 }
151
152 /// JITCompilerFn - This function is called when a lazy compilation stub has
153 /// been entered.  It looks up which function this stub corresponds to, compiles
154 /// it if necessary, then returns the resultant function pointer.
155 void *JITResolver::JITCompilerFn(void *Stub) {
156   JITResolver &JR = getJITResolver();
157   
158   // The address given to us for the stub may not be exactly right, it might be
159   // a little bit after the stub.  As such, use upper_bound to find it.
160   std::map<void*, Function*>::iterator I =
161     JR.StubToFunctionMap.upper_bound(Stub);
162   assert(I != JR.StubToFunctionMap.begin() && "This is not a known stub!");
163   Function *F = (--I)->second;
164
165   // The target function will rewrite the stub so that the compilation callback
166   // function is no longer called from this stub.
167   JR.StubToFunctionMap.erase(I);
168
169   DEBUG(std::cerr << "Lazily resolving function '" << F->getName()
170                   << "' In stub ptr = " << Stub << " actual ptr = "
171                   << I->first << "\n");
172
173   void *Result = TheJIT->getPointerToFunction(F);
174
175   // We don't need to reuse this stub in the future, as F is now compiled.
176   JR.FunctionToStubMap.erase(F);
177
178   // FIXME: We could rewrite all references to this stub if we knew them.
179   return Result;
180 }
181
182
183 //===----------------------------------------------------------------------===//
184 // JIT MachineCodeEmitter code.
185 //
186 namespace {
187   /// Emitter - The JIT implementation of the MachineCodeEmitter, which is used
188   /// to output functions to memory for execution.
189   class Emitter : public MachineCodeEmitter {
190     JITMemoryManager MemMgr;
191
192     // CurBlock - The start of the current block of memory.  CurByte - The
193     // current byte being emitted to.
194     unsigned char *CurBlock, *CurByte;
195
196     // When outputting a function stub in the context of some other function, we
197     // save CurBlock and CurByte here.
198     unsigned char *SavedCurBlock, *SavedCurByte;
199
200     // ConstantPoolAddresses - Contains the location for each entry in the
201     // constant pool.
202     std::vector<void*> ConstantPoolAddresses;
203
204     /// Relocations - These are the relocations that the function needs, as
205     /// emitted.
206     std::vector<MachineRelocation> Relocations;
207   public:
208     Emitter(JIT &jit) { TheJIT = &jit; }
209
210     virtual void startFunction(MachineFunction &F);
211     virtual void finishFunction(MachineFunction &F);
212     virtual void emitConstantPool(MachineConstantPool *MCP);
213     virtual void startFunctionStub(unsigned StubSize);
214     virtual void* finishFunctionStub(const Function *F);
215     virtual void emitByte(unsigned char B);
216     virtual void emitWord(unsigned W);
217     virtual void emitWordAt(unsigned W, unsigned *Ptr);
218
219     virtual void addRelocation(const MachineRelocation &MR) {
220       Relocations.push_back(MR);
221     }
222
223     virtual uint64_t getCurrentPCValue();
224     virtual uint64_t getCurrentPCOffset();
225     virtual uint64_t getGlobalValueAddress(GlobalValue *V);
226     virtual uint64_t getGlobalValueAddress(const char *Name);
227     virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
228
229     // forceCompilationOf - Force the compilation of the specified function, and
230     // return its address, because we REALLY need the address now.
231     //
232     // FIXME: This is JIT specific!
233     //
234     virtual uint64_t forceCompilationOf(Function *F);
235
236   private:
237     void *getPointerToGlobal(GlobalValue *GV);
238   };
239 }
240
241 MachineCodeEmitter *JIT::createEmitter(JIT &jit) {
242   return new Emitter(jit);
243 }
244
245 void *Emitter::getPointerToGlobal(GlobalValue *V) {
246   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
247     /// FIXME: If we straightened things out, this could actually emit the
248     /// global immediately instead of queuing it for codegen later!
249     GlobalVariable *GV = cast<GlobalVariable>(V);
250     return TheJIT->getOrEmitGlobalVariable(GV);
251   }
252
253   // If we have already compiled the function, return a pointer to its body.
254   Function *F = cast<Function>(V);
255   void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F);
256   if (ResultPtr) return ResultPtr;
257
258   if (F->hasExternalLinkage()) {
259     // If this is an external function pointer, we can force the JIT to
260     // 'compile' it, which really just adds it to the map.
261     return TheJIT->getPointerToFunction(F);
262   }
263
264   // Otherwise, we have to emit a lazy resolving stub.
265   return getJITResolver(this).getFunctionStub(F);
266 }
267
268 void Emitter::startFunction(MachineFunction &F) {
269   CurByte = CurBlock = MemMgr.startFunctionBody();
270   TheJIT->addGlobalMapping(F.getFunction(), CurBlock);
271 }
272
273 void Emitter::finishFunction(MachineFunction &F) {
274   MemMgr.endFunctionBody(CurByte);
275   ConstantPoolAddresses.clear();
276   NumBytes += CurByte-CurBlock;
277
278   if (!Relocations.empty()) {
279     // Resolve the relocations to concrete pointers.
280     for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
281       MachineRelocation &MR = Relocations[i];
282       void *ResultPtr;
283       if (MR.isString())
284         ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString());
285       else
286         ResultPtr = getPointerToGlobal(MR.getGlobalValue());
287       MR.setResultPointer(ResultPtr);
288     }
289
290     TheJIT->getJITInfo().relocate(CurBlock, &Relocations[0],
291                                   Relocations.size());
292   }
293
294   DEBUG(std::cerr << "Finished CodeGen of [" << (void*)CurBlock
295                   << "] Function: " << F.getFunction()->getName()
296                   << ": " << CurByte-CurBlock << " bytes of text, "
297                   << Relocations.size() << " relocations\n");
298   Relocations.clear();
299 }
300
301 void Emitter::emitConstantPool(MachineConstantPool *MCP) {
302   const std::vector<Constant*> &Constants = MCP->getConstants();
303   if (Constants.empty()) return;
304
305   std::vector<unsigned> ConstantOffset;
306   ConstantOffset.reserve(Constants.size());
307
308   // Calculate how much space we will need for all the constants, and the offset
309   // each one will live in.
310   unsigned TotalSize = 0;
311   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
312     const Type *Ty = Constants[i]->getType();
313     unsigned Size      = TheJIT->getTargetData().getTypeSize(Ty);
314     unsigned Alignment = TheJIT->getTargetData().getTypeAlignment(Ty);
315     // Make sure to take into account the alignment requirements of the type.
316     TotalSize = (TotalSize + Alignment-1) & ~(Alignment-1);
317
318     // Remember the offset this element lives at.
319     ConstantOffset.push_back(TotalSize);
320     TotalSize += Size;   // Reserve space for the constant.
321   }
322
323   // Now that we know how much memory to allocate, do so.
324   char *Pool = new char[TotalSize];
325
326   // Actually output all of the constants, and remember their addresses.
327   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
328     void *Addr = Pool + ConstantOffset[i];
329     TheJIT->InitializeMemory(Constants[i], Addr);
330     ConstantPoolAddresses.push_back(Addr);
331   }
332 }
333
334 void Emitter::startFunctionStub(unsigned StubSize) {
335   SavedCurBlock = CurBlock;  SavedCurByte = CurByte;
336   CurByte = CurBlock = MemMgr.allocateStub(StubSize);
337 }
338
339 void *Emitter::finishFunctionStub(const Function *F) {
340   NumBytes += CurByte-CurBlock;
341   DEBUG(std::cerr << "Finished CodeGen of [0x" << (void*)CurBlock
342                   << "] Function stub for: " << (F ? F->getName() : "")
343                   << ": " << CurByte-CurBlock << " bytes of text\n");
344   std::swap(CurBlock, SavedCurBlock);
345   CurByte = SavedCurByte;
346   return SavedCurBlock;
347 }
348
349 void Emitter::emitByte(unsigned char B) {
350   *CurByte++ = B;   // Write the byte to memory
351 }
352
353 void Emitter::emitWord(unsigned W) {
354   // This won't work if the endianness of the host and target don't agree!  (For
355   // a JIT this can't happen though.  :)
356   *(unsigned*)CurByte = W;
357   CurByte += sizeof(unsigned);
358 }
359
360 void Emitter::emitWordAt(unsigned W, unsigned *Ptr) {
361   *Ptr = W;
362 }
363
364 uint64_t Emitter::getGlobalValueAddress(GlobalValue *V) {
365   // Try looking up the function to see if it is already compiled, if not return
366   // 0.
367   if (Function *F = dyn_cast<Function>(V)) {
368     void *Addr = TheJIT->getPointerToGlobalIfAvailable(F);
369     if (Addr == 0 && F->hasExternalLinkage()) {
370       // Do not output stubs for external functions.
371       Addr = TheJIT->getPointerToFunction(F);
372     }
373     return (intptr_t)Addr;
374   } else {
375     return (intptr_t)TheJIT->getOrEmitGlobalVariable(cast<GlobalVariable>(V));
376   }
377 }
378 uint64_t Emitter::getGlobalValueAddress(const char *Name) {
379   return (intptr_t)TheJIT->getPointerToNamedFunction(Name);
380 }
381
382 // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
383 // in the constant pool that was last emitted with the 'emitConstantPool'
384 // method.
385 //
386 uint64_t Emitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
387   assert(ConstantNum < ConstantPoolAddresses.size() &&
388          "Invalid ConstantPoolIndex!");
389   return (intptr_t)ConstantPoolAddresses[ConstantNum];
390 }
391
392 // getCurrentPCValue - This returns the address that the next emitted byte
393 // will be output to.
394 //
395 uint64_t Emitter::getCurrentPCValue() {
396   return (intptr_t)CurByte;
397 }
398
399 uint64_t Emitter::getCurrentPCOffset() {
400   return (intptr_t)CurByte-(intptr_t)CurBlock;
401 }
402
403 uint64_t Emitter::forceCompilationOf(Function *F) {
404   return (intptr_t)TheJIT->getPointerToFunction(F);
405 }
406
407 // getPointerToNamedFunction - This function is used as a global wrapper to
408 // JIT::getPointerToNamedFunction for the purpose of resolving symbols when
409 // bugpoint is debugging the JIT. In that scenario, we are loading an .so and
410 // need to resolve function(s) that are being mis-codegenerated, so we need to
411 // resolve their addresses at runtime, and this is the way to do it.
412 extern "C" {
413   void *getPointerToNamedFunction(const char *Name) {
414     Module &M = TheJIT->getModule();
415     if (Function *F = M.getNamedFunction(Name))
416       return TheJIT->getPointerToFunction(F);
417     return TheJIT->getPointerToNamedFunction(Name);
418   }
419 }