blob: 9e89e3c0e1d93c8eab9f57344a7b2b33e006bd06 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Anton Korobeynikov52f44db2007-07-30 20:02:02 +000030
31#include "llvm/Config/config.h"
32
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033using namespace llvm;
34
35#ifdef __APPLE__
Anton Korobeynikov52f44db2007-07-30 20:02:02 +000036// Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
37// of atexit). It passes the address of linker generated symbol __dso_handle
38// to the function.
39// This configuration change happened at version 5330.
40# include <AvailabilityMacros.h>
41# if defined(MAC_OS_X_VERSION_10_4) && \
42 ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
43 (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
44 __APPLE_CC__ >= 5330))
45# ifndef HAVE___DSO_HANDLE
46# define HAVE___DSO_HANDLE 1
47# endif
48# endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049#endif
Anton Korobeynikov52f44db2007-07-30 20:02:02 +000050
51#if HAVE___DSO_HANDLE
52extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053#endif
54
55static struct RegisterJIT {
56 RegisterJIT() { JIT::Register(); }
57} JITRegistrator;
58
59namespace llvm {
60 void LinkInJIT() {
61 }
62}
63
Anton Korobeynikov6556e9e2008-03-22 08:53:09 +000064#if defined (__GNUC__)
65extern "C" void __register_frame(void*);
66#endif
67
Chris Lattner4db98aa2007-12-06 01:34:04 +000068/// createJIT - This is the factory method for creating a JIT for the current
69/// machine, it does not fall back to the interpreter. This takes ownership
70/// of the module provider.
71ExecutionEngine *ExecutionEngine::createJIT(ModuleProvider *MP,
72 std::string *ErrorStr,
73 JITMemoryManager *JMM) {
74 ExecutionEngine *EE = JIT::createJIT(MP, ErrorStr, JMM);
75 if (!EE) return 0;
76
Anton Korobeynikov6556e9e2008-03-22 08:53:09 +000077 // Register routine for informing unwinding runtime about new EH frames
78#if defined(__GNUC__)
79 EE->InstallExceptionTableRegister(__register_frame);
80#endif
81
Chris Lattner4db98aa2007-12-06 01:34:04 +000082 // Make sure we can resolve symbols in the program as well. The zero arg
83 // to the function tells DynamicLibrary to load the program, not a library.
84 sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr);
85 return EE;
86}
87
88JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
89 JITMemoryManager *JMM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 : ExecutionEngine(MP), TM(tm), TJI(tji), jitstate(MP) {
91 setTargetData(TM.getTargetData());
92
93 // Initialize MCE
Chris Lattner4db98aa2007-12-06 01:34:04 +000094 MCE = createEmitter(*this, JMM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095
96 // Add target data
97 MutexGuard locked(lock);
98 FunctionPassManager &PM = jitstate.getPM(locked);
99 PM.add(new TargetData(*TM.getTargetData()));
100
101 // Turn the machine code intermediate representation into bytes in memory that
102 // may be executed.
103 if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
104 cerr << "Target does not support machine code emission!\n";
105 abort();
106 }
107
108 // Initialize passes.
109 PM.doInitialization();
110}
111
112JIT::~JIT() {
113 delete MCE;
114 delete &TM;
115}
116
117/// run - Start execution with the specified function and arguments.
118///
119GenericValue JIT::runFunction(Function *F,
120 const std::vector<GenericValue> &ArgValues) {
121 assert(F && "Function *F was null at entry to run()");
122
123 void *FPtr = getPointerToFunction(F);
124 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
125 const FunctionType *FTy = F->getFunctionType();
126 const Type *RetTy = FTy->getReturnType();
127
128 assert((FTy->getNumParams() <= ArgValues.size() || FTy->isVarArg()) &&
129 "Too many arguments passed into function!");
130 assert(FTy->getNumParams() == ArgValues.size() &&
131 "This doesn't support passing arguments through varargs (yet)!");
132
133 // Handle some common cases first. These cases correspond to common `main'
134 // prototypes.
Chris Lattnerbbf22702007-08-08 16:19:57 +0000135 if (RetTy == Type::Int32Ty || RetTy == Type::VoidTy) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 switch (ArgValues.size()) {
137 case 3:
Chris Lattnerbbf22702007-08-08 16:19:57 +0000138 if (FTy->getParamType(0) == Type::Int32Ty &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 isa<PointerType>(FTy->getParamType(1)) &&
140 isa<PointerType>(FTy->getParamType(2))) {
141 int (*PF)(int, char **, const char **) =
142 (int(*)(int, char **, const char **))(intptr_t)FPtr;
143
144 // Call the function.
145 GenericValue rv;
146 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
147 (char **)GVTOP(ArgValues[1]),
148 (const char **)GVTOP(ArgValues[2])));
149 return rv;
150 }
151 break;
152 case 2:
Chris Lattnerbbf22702007-08-08 16:19:57 +0000153 if (FTy->getParamType(0) == Type::Int32Ty &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 isa<PointerType>(FTy->getParamType(1))) {
155 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
156
157 // Call the function.
158 GenericValue rv;
159 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
160 (char **)GVTOP(ArgValues[1])));
161 return rv;
162 }
163 break;
164 case 1:
165 if (FTy->getNumParams() == 1 &&
Chris Lattnerbbf22702007-08-08 16:19:57 +0000166 FTy->getParamType(0) == Type::Int32Ty) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 GenericValue rv;
168 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
169 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
170 return rv;
171 }
172 break;
173 }
174 }
175
176 // Handle cases where no arguments are passed first.
177 if (ArgValues.empty()) {
178 GenericValue rv;
179 switch (RetTy->getTypeID()) {
180 default: assert(0 && "Unknown return type for function call!");
181 case Type::IntegerTyID: {
182 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
183 if (BitWidth == 1)
184 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
185 else if (BitWidth <= 8)
186 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
187 else if (BitWidth <= 16)
188 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
189 else if (BitWidth <= 32)
190 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
191 else if (BitWidth <= 64)
192 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
193 else
194 assert(0 && "Integer types > 64 bits not supported");
195 return rv;
196 }
197 case Type::VoidTyID:
198 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
199 return rv;
200 case Type::FloatTyID:
201 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
202 return rv;
203 case Type::DoubleTyID:
204 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
205 return rv;
Dale Johannesenc560da62007-09-17 18:44:13 +0000206 case Type::X86_FP80TyID:
207 case Type::FP128TyID:
208 case Type::PPC_FP128TyID:
209 assert(0 && "long double not supported yet");
210 return rv;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 case Type::PointerTyID:
212 return PTOGV(((void*(*)())(intptr_t)FPtr)());
213 }
214 }
215
216 // Okay, this is not one of our quick and easy cases. Because we don't have a
217 // full FFI, we have to codegen a nullary stub function that just calls the
218 // function we are interested in, passing in constants for all of the
219 // arguments. Make this function and return.
220
221 // First, create the function.
222 FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false);
Gabor Greifd6da1d02008-04-06 20:25:17 +0000223 Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
224 F->getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225
226 // Insert a basic block.
Gabor Greifd6da1d02008-04-06 20:25:17 +0000227 BasicBlock *StubBB = BasicBlock::Create("", Stub);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228
229 // Convert all of the GenericValue arguments over to constants. Note that we
230 // currently don't support varargs.
231 SmallVector<Value*, 8> Args;
232 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
233 Constant *C = 0;
234 const Type *ArgTy = FTy->getParamType(i);
235 const GenericValue &AV = ArgValues[i];
236 switch (ArgTy->getTypeID()) {
237 default: assert(0 && "Unknown argument type for function call!");
Chris Lattner5e0610f2008-04-20 00:41:09 +0000238 case Type::IntegerTyID:
239 C = ConstantInt::get(AV.IntVal);
240 break;
241 case Type::FloatTyID:
242 C = ConstantFP::get(APFloat(AV.FloatVal));
243 break;
244 case Type::DoubleTyID:
245 C = ConstantFP::get(APFloat(AV.DoubleVal));
246 break;
Dale Johannesenc560da62007-09-17 18:44:13 +0000247 case Type::PPC_FP128TyID:
248 case Type::X86_FP80TyID:
Chris Lattner5e0610f2008-04-20 00:41:09 +0000249 case Type::FP128TyID:
250 C = ConstantFP::get(APFloat(AV.IntVal));
251 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 case Type::PointerTyID:
253 void *ArgPtr = GVTOP(AV);
Chris Lattner5e0610f2008-04-20 00:41:09 +0000254 if (sizeof(void*) == 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr);
Chris Lattner5e0610f2008-04-20 00:41:09 +0000256 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 C = ConstantExpr::getIntToPtr(C, ArgTy); // Cast the integer to pointer
259 break;
260 }
261 Args.push_back(C);
262 }
263
Gabor Greifd6da1d02008-04-06 20:25:17 +0000264 CallInst *TheCall = CallInst::Create(F, Args.begin(), Args.end(), "", StubBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 TheCall->setTailCall();
266 if (TheCall->getType() != Type::VoidTy)
Gabor Greifd6da1d02008-04-06 20:25:17 +0000267 ReturnInst::Create(TheCall, StubBB); // Return result of the call.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 else
Gabor Greifd6da1d02008-04-06 20:25:17 +0000269 ReturnInst::Create(StubBB); // Just return void.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270
271 // Finally, return the value returned by our nullary stub function.
272 return runFunction(Stub, std::vector<GenericValue>());
273}
274
275/// runJITOnFunction - Run the FunctionPassManager full of
276/// just-in-time compilation passes on F, hopefully filling in
277/// GlobalAddress[F] with the address of F's machine code.
278///
279void JIT::runJITOnFunction(Function *F) {
280 static bool isAlreadyCodeGenerating = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281
282 MutexGuard locked(lock);
Chris Lattner700fb1d2007-08-13 20:08:16 +0000283 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284
285 // JIT the function
286 isAlreadyCodeGenerating = true;
287 jitstate.getPM(locked).run(*F);
288 isAlreadyCodeGenerating = false;
289
290 // If the function referred to a global variable that had not yet been
291 // emitted, it allocates memory for the global, but doesn't emit it yet. Emit
292 // all of these globals now.
293 while (!jitstate.getPendingGlobals(locked).empty()) {
294 const GlobalVariable *GV = jitstate.getPendingGlobals(locked).back();
295 jitstate.getPendingGlobals(locked).pop_back();
296 EmitGlobalVariable(GV);
297 }
298}
299
300/// getPointerToFunction - This method is used to get the address of the
301/// specified function, compiling it if neccesary.
302///
303void *JIT::getPointerToFunction(Function *F) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304
305 if (void *Addr = getPointerToGlobalIfAvailable(F))
306 return Addr; // Check if function already code gen'd
307
308 // Make sure we read in the function if it exists in this Module.
309 if (F->hasNotBeenReadFromBitcode()) {
310 // Determine the module provider this function is provided by.
311 Module *M = F->getParent();
312 ModuleProvider *MP = 0;
313 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
314 if (Modules[i]->getModule() == M) {
315 MP = Modules[i];
316 break;
317 }
318 }
319 assert(MP && "Function isn't in a module we know about!");
320
321 std::string ErrorMsg;
322 if (MP->materializeFunction(F, &ErrorMsg)) {
323 cerr << "Error reading function '" << F->getName()
324 << "' from bitcode file: " << ErrorMsg << "\n";
325 abort();
326 }
327 }
Nicolas Geoffray8ae32352008-04-20 08:33:02 +0000328
329 if (void *Addr = getPointerToGlobalIfAvailable(F)) {
330 return Addr;
331 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332
Nicolas Geoffray8ae32352008-04-20 08:33:02 +0000333 MutexGuard locked(lock);
334
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335 if (F->isDeclaration()) {
336 void *Addr = getPointerToNamedFunction(F->getName());
337 addGlobalMapping(F, Addr);
338 return Addr;
339 }
340
341 runJITOnFunction(F);
342
343 void *Addr = getPointerToGlobalIfAvailable(F);
344 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
345 return Addr;
346}
347
348/// getOrEmitGlobalVariable - Return the address of the specified global
349/// variable, possibly emitting it to memory if needed. This is used by the
350/// Emitter.
351void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
352 MutexGuard locked(lock);
353
354 void *Ptr = getPointerToGlobalIfAvailable(GV);
355 if (Ptr) return Ptr;
356
357 // If the global is external, just remember the address.
358 if (GV->isDeclaration()) {
Anton Korobeynikov52f44db2007-07-30 20:02:02 +0000359#if HAVE___DSO_HANDLE
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360 if (GV->getName() == "__dso_handle")
361 return (void*)&__dso_handle;
362#endif
363 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
364 if (Ptr == 0) {
365 cerr << "Could not resolve external global address: "
366 << GV->getName() << "\n";
367 abort();
368 }
369 } else {
370 // If the global hasn't been emitted to memory yet, allocate space. We will
371 // actually initialize the global after current function has finished
372 // compilation.
373 const Type *GlobalType = GV->getType()->getElementType();
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000374 size_t S = getTargetData()->getABITypeSize(GlobalType);
Duncan Sands935686e2008-01-29 06:23:44 +0000375 size_t A = getTargetData()->getPreferredAlignment(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 if (A <= 8) {
377 Ptr = malloc(S);
378 } else {
379 // Allocate S+A bytes of memory, then use an aligned pointer within that
380 // space.
381 Ptr = malloc(S+A);
382 unsigned MisAligned = ((intptr_t)Ptr & (A-1));
383 Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
384 }
385 jitstate.getPendingGlobals(locked).push_back(GV);
386 }
387 addGlobalMapping(GV, Ptr);
388 return Ptr;
389}
390
391
392/// recompileAndRelinkFunction - This method is used to force a function
393/// which has already been compiled, to be compiled again, possibly
394/// after it has been modified. Then the entry to the old copy is overwritten
395/// with a branch to the new copy. If there was no old copy, this acts
396/// just like JIT::getPointerToFunction().
397///
398void *JIT::recompileAndRelinkFunction(Function *F) {
399 void *OldAddr = getPointerToGlobalIfAvailable(F);
400
401 // If it's not already compiled there is no reason to patch it up.
402 if (OldAddr == 0) { return getPointerToFunction(F); }
403
404 // Delete the old function mapping.
405 addGlobalMapping(F, 0);
406
407 // Recodegen the function
408 runJITOnFunction(F);
409
410 // Update state, forward the old function to the new function.
411 void *Addr = getPointerToGlobalIfAvailable(F);
412 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
413 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
414 return Addr;
415}
416