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