1 //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This tool implements a just-in-time compiler for LLVM, allowing direct
11 // execution of LLVM bitcode in an efficient manner.
13 //===----------------------------------------------------------------------===//
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"
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")));
43 static struct RegisterJIT {
44 RegisterJIT() { JIT::Register(); }
52 JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji)
53 : ExecutionEngine(MP), TM(tm), TJI(tji), jitstate(MP) {
54 setTargetData(TM.getTargetData());
57 MCE = createEmitter(*this);
60 MutexGuard locked(lock);
61 FunctionPassManager &PM = jitstate.getPM(locked);
62 PM.add(new TargetData(*TM.getTargetData()));
64 // Turn the machine code intermediate representation into bytes in memory that
66 if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
67 cerr << "Target does not support machine code emission!\n";
72 PM.doInitialization();
80 /// run - Start execution with the specified function and arguments.
82 GenericValue JIT::runFunction(Function *F,
83 const std::vector<GenericValue> &ArgValues) {
84 assert(F && "Function *F was null at entry to run()");
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();
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)!");
96 // Handle some common cases first. These cases correspond to common `main'
98 if (RetTy == Type::Int32Ty || RetTy == Type::Int32Ty || RetTy == Type::VoidTy) {
99 switch (ArgValues.size()) {
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;
108 // Call the function.
110 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
111 (char **)GVTOP(ArgValues[1]),
112 (const char **)GVTOP(ArgValues[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;
122 // Call the function.
124 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
125 (char **)GVTOP(ArgValues[1])));
130 if (FTy->getNumParams() == 1 &&
131 (FTy->getParamType(0) == Type::Int32Ty ||
132 FTy->getParamType(0) == Type::Int32Ty)) {
134 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
135 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
142 // Handle cases where no arguments are passed first.
143 if (ArgValues.empty()) {
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();
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)());
160 assert(0 && "Integer types > 64 bits not supported");
164 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
166 case Type::FloatTyID:
167 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
169 case Type::DoubleTyID:
170 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
172 case Type::PointerTyID:
173 return PTOGV(((void*(*)())(intptr_t)FPtr)());
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.
182 // First, create the function.
183 FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false);
184 Function *Stub = new Function(STy, Function::InternalLinkage, "",
187 // Insert a basic block.
188 BasicBlock *StubBB = new BasicBlock("", Stub);
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) {
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);
207 C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
209 C = ConstantExpr::getIntToPtr(C, ArgTy); // Cast the integer to pointer
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.
220 new ReturnInst(StubBB); // Just return void.
222 // Finally, return the value returned by our nullary stub function.
223 return runFunction(Stub, std::vector<GenericValue>());
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.
230 void JIT::runJITOnFunction(Function *F) {
231 static bool isAlreadyCodeGenerating = false;
232 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
234 MutexGuard locked(lock);
237 isAlreadyCodeGenerating = true;
238 jitstate.getPM(locked).run(*F);
239 isAlreadyCodeGenerating = false;
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);
251 /// getPointerToFunction - This method is used to get the address of the
252 /// specified function, compiling it if neccesary.
254 void *JIT::getPointerToFunction(Function *F) {
255 MutexGuard locked(lock);
257 if (void *Addr = getPointerToGlobalIfAvailable(F))
258 return Addr; // Check if function already code gen'd
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) {
271 assert(MP && "Function isn't in a module we know about!");
273 std::string ErrorMsg;
274 if (MP->materializeFunction(F, &ErrorMsg)) {
275 cerr << "Error reading function '" << F->getName()
276 << "' from bitcode file: " << ErrorMsg << "\n";
281 if (F->isDeclaration()) {
282 void *Addr = getPointerToNamedFunction(F->getName());
283 addGlobalMapping(F, Addr);
289 void *Addr = getPointerToGlobalIfAvailable(F);
290 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
294 /// getOrEmitGlobalVariable - Return the address of the specified global
295 /// variable, possibly emitting it to memory if needed. This is used by the
297 void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
298 MutexGuard locked(lock);
300 void *Ptr = getPointerToGlobalIfAvailable(GV);
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
312 // This configuration change happened at version 5330.
313 if (GV->getName() == "__dso_handle")
314 return (void*)&__dso_handle;
316 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
318 cerr << "Could not resolve external global address: "
319 << GV->getName() << "\n";
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
326 const Type *GlobalType = GV->getType()->getElementType();
327 size_t S = getTargetData()->getTypeSize(GlobalType);
328 size_t A = getTargetData()->getPrefTypeAlignment(GlobalType);
332 // Allocate S+A bytes of memory, then use an aligned pointer within that
335 unsigned MisAligned = ((intptr_t)Ptr & (A-1));
336 Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
338 jitstate.getPendingGlobals(locked).push_back(GV);
340 addGlobalMapping(GV, Ptr);
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().
351 void *JIT::recompileAndRelinkFunction(Function *F) {
352 void *OldAddr = getPointerToGlobalIfAvailable(F);
354 // If it's not already compiled there is no reason to patch it up.
355 if (OldAddr == 0) { return getPointerToFunction(F); }
357 // Delete the old function mapping.
358 addGlobalMapping(F, 0);
360 // Recodegen the function
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);