Here is the bulk of the sanitizing.
[oota-llvm.git] / lib / ExecutionEngine / JIT / JIT.cpp
1 //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
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 tool implements a just-in-time compiler for LLVM, allowing direct
11 // execution of LLVM bitcode in an efficient manner.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "JIT.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/ModuleProvider.h"
22 #include "llvm/CodeGen/MachineCodeEmitter.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/ExecutionEngine/GenericValue.h"
25 #include "llvm/Support/MutexGuard.h"
26 #include "llvm/System/DynamicLibrary.h"
27 #include "llvm/Target/TargetData.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetJITInfo.h"
30 using namespace llvm;
31
32 #ifdef __APPLE__ 
33 #include <AvailabilityMacros.h>
34 #if defined(MAC_OS_X_VERSION_10_4) && \
35     ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
36      (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
37       __APPLE_CC__ >= 5330))
38 // __dso_handle is resolved by Mac OS X dynamic linker.
39 extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
40 #endif
41 #endif
42
43 static struct RegisterJIT {
44   RegisterJIT() { JIT::Register(); }
45 } JITRegistrator;
46
47 namespace llvm {
48   void LinkInJIT() {
49   }
50 }
51
52 JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji)
53   : ExecutionEngine(MP), TM(tm), TJI(tji), jitstate(MP) {
54   setTargetData(TM.getTargetData());
55
56   // Initialize MCE
57   MCE = createEmitter(*this);
58
59   // Add target data
60   MutexGuard locked(lock);
61   FunctionPassManager &PM = jitstate.getPM(locked);
62   PM.add(new TargetData(*TM.getTargetData()));
63
64   // Turn the machine code intermediate representation into bytes in memory that
65   // may be executed.
66   if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
67     cerr << "Target does not support machine code emission!\n";
68     abort();
69   }
70   
71   // Initialize passes.
72   PM.doInitialization();
73 }
74
75 JIT::~JIT() {
76   delete MCE;
77   delete &TM;
78 }
79
80 /// run - Start execution with the specified function and arguments.
81 ///
82 GenericValue JIT::runFunction(Function *F,
83                               const std::vector<GenericValue> &ArgValues) {
84   assert(F && "Function *F was null at entry to run()");
85
86   void *FPtr = getPointerToFunction(F);
87   assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
88   const FunctionType *FTy = F->getFunctionType();
89   const Type *RetTy = FTy->getReturnType();
90
91   assert((FTy->getNumParams() <= ArgValues.size() || FTy->isVarArg()) &&
92          "Too many arguments passed into function!");
93   assert(FTy->getNumParams() == ArgValues.size() &&
94          "This doesn't support passing arguments through varargs (yet)!");
95
96   // Handle some common cases first.  These cases correspond to common `main'
97   // prototypes.
98   if (RetTy == Type::Int32Ty || RetTy == Type::Int32Ty || RetTy == Type::VoidTy) {
99     switch (ArgValues.size()) {
100     case 3:
101       if ((FTy->getParamType(0) == Type::Int32Ty ||
102            FTy->getParamType(0) == Type::Int32Ty) &&
103           isa<PointerType>(FTy->getParamType(1)) &&
104           isa<PointerType>(FTy->getParamType(2))) {
105         int (*PF)(int, char **, const char **) =
106           (int(*)(int, char **, const char **))(intptr_t)FPtr;
107
108         // Call the function.
109         GenericValue rv;
110         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), 
111                                  (char **)GVTOP(ArgValues[1]),
112                                  (const char **)GVTOP(ArgValues[2])));
113         return rv;
114       }
115       break;
116     case 2:
117       if ((FTy->getParamType(0) == Type::Int32Ty ||
118            FTy->getParamType(0) == Type::Int32Ty) &&
119           isa<PointerType>(FTy->getParamType(1))) {
120         int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
121
122         // Call the function.
123         GenericValue rv;
124         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), 
125                                  (char **)GVTOP(ArgValues[1])));
126         return rv;
127       }
128       break;
129     case 1:
130       if (FTy->getNumParams() == 1 &&
131           (FTy->getParamType(0) == Type::Int32Ty ||
132            FTy->getParamType(0) == Type::Int32Ty)) {
133         GenericValue rv;
134         int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
135         rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
136         return rv;
137       }
138       break;
139     }
140   }
141
142   // Handle cases where no arguments are passed first.
143   if (ArgValues.empty()) {
144     GenericValue rv;
145     switch (RetTy->getTypeID()) {
146     default: assert(0 && "Unknown return type for function call!");
147     case Type::IntegerTyID: {
148       unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
149       if (BitWidth == 1)
150         rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
151       else if (BitWidth <= 8)
152         rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
153       else if (BitWidth <= 16)
154         rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
155       else if (BitWidth <= 32)
156         rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
157       else if (BitWidth <= 64)
158         rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
159       else 
160         assert(0 && "Integer types > 64 bits not supported");
161       return rv;
162     }
163     case Type::VoidTyID:
164       rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
165       return rv;
166     case Type::FloatTyID:
167       rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
168       return rv;
169     case Type::DoubleTyID:
170       rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
171       return rv;
172     case Type::PointerTyID:
173       return PTOGV(((void*(*)())(intptr_t)FPtr)());
174     }
175   }
176
177   // Okay, this is not one of our quick and easy cases.  Because we don't have a
178   // full FFI, we have to codegen a nullary stub function that just calls the
179   // function we are interested in, passing in constants for all of the
180   // arguments.  Make this function and return.
181
182   // First, create the function.
183   FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false);
184   Function *Stub = new Function(STy, Function::InternalLinkage, "",
185                                 F->getParent());
186
187   // Insert a basic block.
188   BasicBlock *StubBB = new BasicBlock("", Stub);
189
190   // Convert all of the GenericValue arguments over to constants.  Note that we
191   // currently don't support varargs.
192   SmallVector<Value*, 8> Args;
193   for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
194     Constant *C = 0;
195     const Type *ArgTy = FTy->getParamType(i);
196     const GenericValue &AV = ArgValues[i];
197     switch (ArgTy->getTypeID()) {
198     default: assert(0 && "Unknown argument type for function call!");
199     case Type::IntegerTyID: C = ConstantInt::get(AV.IntVal); break;
200     case Type::FloatTyID:   C = ConstantFP ::get(ArgTy, AV.FloatVal);  break;
201     case Type::DoubleTyID:  C = ConstantFP ::get(ArgTy, AV.DoubleVal); break;
202     case Type::PointerTyID:
203       void *ArgPtr = GVTOP(AV);
204       if (sizeof(void*) == 4) {
205         C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr);
206       } else {
207         C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
208       }
209       C = ConstantExpr::getIntToPtr(C, ArgTy);  // Cast the integer to pointer
210       break;
211     }
212     Args.push_back(C);
213   }
214
215   CallInst *TheCall = new CallInst(F, &Args[0], Args.size(), "", StubBB);
216   TheCall->setTailCall();
217   if (TheCall->getType() != Type::VoidTy)
218     new ReturnInst(TheCall, StubBB);             // Return result of the call.
219   else
220     new ReturnInst(StubBB);                      // Just return void.
221
222   // Finally, return the value returned by our nullary stub function.
223   return runFunction(Stub, std::vector<GenericValue>());
224 }
225
226 /// runJITOnFunction - Run the FunctionPassManager full of
227 /// just-in-time compilation passes on F, hopefully filling in
228 /// GlobalAddress[F] with the address of F's machine code.
229 ///
230 void JIT::runJITOnFunction(Function *F) {
231   static bool isAlreadyCodeGenerating = false;
232   assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
233
234   MutexGuard locked(lock);
235
236   // JIT the function
237   isAlreadyCodeGenerating = true;
238   jitstate.getPM(locked).run(*F);
239   isAlreadyCodeGenerating = false;
240
241   // If the function referred to a global variable that had not yet been
242   // emitted, it allocates memory for the global, but doesn't emit it yet.  Emit
243   // all of these globals now.
244   while (!jitstate.getPendingGlobals(locked).empty()) {
245     const GlobalVariable *GV = jitstate.getPendingGlobals(locked).back();
246     jitstate.getPendingGlobals(locked).pop_back();
247     EmitGlobalVariable(GV);
248   }
249 }
250
251 /// getPointerToFunction - This method is used to get the address of the
252 /// specified function, compiling it if neccesary.
253 ///
254 void *JIT::getPointerToFunction(Function *F) {
255   MutexGuard locked(lock);
256
257   if (void *Addr = getPointerToGlobalIfAvailable(F))
258     return Addr;   // Check if function already code gen'd
259
260   // Make sure we read in the function if it exists in this Module.
261   if (F->hasNotBeenReadFromBitcode()) {
262     // Determine the module provider this function is provided by.
263     Module *M = F->getParent();
264     ModuleProvider *MP = 0;
265     for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
266       if (Modules[i]->getModule() == M) {
267         MP = Modules[i];
268         break;
269       }
270     }
271     assert(MP && "Function isn't in a module we know about!");
272     
273     std::string ErrorMsg;
274     if (MP->materializeFunction(F, &ErrorMsg)) {
275       cerr << "Error reading function '" << F->getName()
276            << "' from bitcode file: " << ErrorMsg << "\n";
277       abort();
278     }
279   }
280
281   if (F->isDeclaration()) {
282     void *Addr = getPointerToNamedFunction(F->getName());
283     addGlobalMapping(F, Addr);
284     return Addr;
285   }
286
287   runJITOnFunction(F);
288
289   void *Addr = getPointerToGlobalIfAvailable(F);
290   assert(Addr && "Code generation didn't add function to GlobalAddress table!");
291   return Addr;
292 }
293
294 /// getOrEmitGlobalVariable - Return the address of the specified global
295 /// variable, possibly emitting it to memory if needed.  This is used by the
296 /// Emitter.
297 void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
298   MutexGuard locked(lock);
299
300   void *Ptr = getPointerToGlobalIfAvailable(GV);
301   if (Ptr) return Ptr;
302
303   // If the global is external, just remember the address.
304   if (GV->isDeclaration()) {
305 #if defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_4) && \
306     ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
307      (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
308       __APPLE_CC__ >= 5330))
309     // Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
310     // of atexit). It passes the address of linker generated symbol __dso_handle
311     // to the function.
312     // This configuration change happened at version 5330.
313     if (GV->getName() == "__dso_handle")
314       return (void*)&__dso_handle;
315 #endif
316     Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
317     if (Ptr == 0) {
318       cerr << "Could not resolve external global address: "
319            << GV->getName() << "\n";
320       abort();
321     }
322   } else {
323     // If the global hasn't been emitted to memory yet, allocate space.  We will
324     // actually initialize the global after current function has finished
325     // compilation.
326     const Type *GlobalType = GV->getType()->getElementType();
327     size_t S = getTargetData()->getTypeSize(GlobalType);
328     size_t A = getTargetData()->getPrefTypeAlignment(GlobalType);
329     if (A <= 8) {
330       Ptr = malloc(S);
331     } else {
332       // Allocate S+A bytes of memory, then use an aligned pointer within that
333       // space.
334       Ptr = malloc(S+A);
335       unsigned MisAligned = ((intptr_t)Ptr & (A-1));
336       Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
337     }
338     jitstate.getPendingGlobals(locked).push_back(GV);
339   }
340   addGlobalMapping(GV, Ptr);
341   return Ptr;
342 }
343
344
345 /// recompileAndRelinkFunction - This method is used to force a function
346 /// which has already been compiled, to be compiled again, possibly
347 /// after it has been modified. Then the entry to the old copy is overwritten
348 /// with a branch to the new copy. If there was no old copy, this acts
349 /// just like JIT::getPointerToFunction().
350 ///
351 void *JIT::recompileAndRelinkFunction(Function *F) {
352   void *OldAddr = getPointerToGlobalIfAvailable(F);
353
354   // If it's not already compiled there is no reason to patch it up.
355   if (OldAddr == 0) { return getPointerToFunction(F); }
356
357   // Delete the old function mapping.
358   addGlobalMapping(F, 0);
359
360   // Recodegen the function
361   runJITOnFunction(F);
362
363   // Update state, forward the old function to the new function.
364   void *Addr = getPointerToGlobalIfAvailable(F);
365   assert(Addr && "Code generation didn't add function to GlobalAddress table!");
366   TJI.replaceMachineCodeForFunction(OldAddr, Addr);
367   return Addr;
368 }
369