LEFT | RIGHT |
1 //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===// | 1 //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This tool implements a just-in-time compiler for LLVM, allowing direct | 10 // This tool implements a just-in-time compiler for LLVM, allowing direct |
11 // execution of LLVM bitcode in an efficient manner. | 11 // execution of LLVM bitcode in an efficient manner. |
12 // | 12 // |
13 //===----------------------------------------------------------------------===// | 13 //===----------------------------------------------------------------------===// |
14 | 14 |
15 #include "JIT.h" | 15 #include "JIT.h" |
16 #include "llvm/Constants.h" | 16 #include "llvm/Constants.h" |
17 #include "llvm/DerivedTypes.h" | 17 #include "llvm/DerivedTypes.h" |
18 #include "llvm/Function.h" | 18 #include "llvm/Function.h" |
19 #include "llvm/GlobalVariable.h" | 19 #include "llvm/GlobalVariable.h" |
20 #include "llvm/Instructions.h" | 20 #include "llvm/Instructions.h" |
21 #include "llvm/ModuleProvider.h" | 21 #include "llvm/ModuleProvider.h" |
22 #include "llvm/CodeGen/JITCodeEmitter.h" | 22 #include "llvm/CodeGen/JITCodeEmitter.h" |
23 #include "llvm/CodeGen/MachineCodeInfo.h" | 23 #include "llvm/CodeGen/MachineCodeInfo.h" |
24 #include "llvm/ExecutionEngine/GenericValue.h" | 24 #include "llvm/ExecutionEngine/GenericValue.h" |
25 #include "llvm/ExecutionEngine/JITEventListener.h" | 25 #include "llvm/ExecutionEngine/JITEventListener.h" |
26 #include "llvm/Target/TargetData.h" | 26 #include "llvm/Target/TargetData.h" |
27 #include "llvm/Target/TargetMachine.h" | 27 #include "llvm/Target/TargetMachine.h" |
28 #include "llvm/Target/TargetJITInfo.h" | 28 #include "llvm/Target/TargetJITInfo.h" |
29 #include "llvm/Support/Dwarf.h" | 29 #include "llvm/Support/Dwarf.h" |
| 30 #include "llvm/Support/ErrorHandling.h" |
30 #include "llvm/Support/MutexGuard.h" | 31 #include "llvm/Support/MutexGuard.h" |
31 #include "llvm/System/DynamicLibrary.h" | 32 #include "llvm/System/DynamicLibrary.h" |
32 #include "llvm/Config/config.h" | 33 #include "llvm/Config/config.h" |
33 | 34 |
34 using namespace llvm; | 35 using namespace llvm; |
35 | 36 |
36 #ifdef __APPLE__· | 37 #ifdef __APPLE__· |
37 // Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead | 38 // Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead |
38 // of atexit). It passes the address of linker generated symbol __dso_handle | 39 // of atexit). It passes the address of linker generated symbol __dso_handle |
39 // to the function. | 40 // to the function. |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 } | 190 } |
190 #endif // __APPLE__ | 191 #endif // __APPLE__ |
191 #endif // __GNUC__ | 192 #endif // __GNUC__ |
192 | 193 |
193 /// createJIT - This is the factory method for creating a JIT for the current | 194 /// createJIT - This is the factory method for creating a JIT for the current |
194 /// machine, it does not fall back to the interpreter. This takes ownership | 195 /// machine, it does not fall back to the interpreter. This takes ownership |
195 /// of the module provider. | 196 /// of the module provider. |
196 ExecutionEngine *ExecutionEngine::createJIT(ModuleProvider *MP, | 197 ExecutionEngine *ExecutionEngine::createJIT(ModuleProvider *MP, |
197 std::string *ErrorStr, | 198 std::string *ErrorStr, |
198 JITMemoryManager *JMM, | 199 JITMemoryManager *JMM, |
199 CodeGenOpt::Level OptLevel) { | 200 CodeGenOpt::Level OptLevel, |
200 ExecutionEngine *EE = JIT::createJIT(MP, ErrorStr, JMM, OptLevel); | 201 bool GVsWithCode) { |
201 if (!EE) return 0; | 202 return JIT::createJIT(MP, ErrorStr, JMM, OptLevel, GVsWithCode); |
202 ·· | 203 } |
| 204 |
| 205 ExecutionEngine *JIT::createJIT(ModuleProvider *MP, |
| 206 std::string *ErrorStr, |
| 207 JITMemoryManager *JMM, |
| 208 CodeGenOpt::Level OptLevel, |
| 209 bool GVsWithCode) { |
203 // Make sure we can resolve symbols in the program as well. The zero arg | 210 // Make sure we can resolve symbols in the program as well. The zero arg |
204 // to the function tells DynamicLibrary to load the program, not a library. | 211 // to the function tells DynamicLibrary to load the program, not a library. |
205 sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr); | 212 if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr)) |
206 return EE; | 213 return 0; |
| 214 |
| 215 // Pick a target either via -march or by guessing the native arch. |
| 216 TargetMachine *TM = JIT::selectTarget(MP, ErrorStr); |
| 217 if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0; |
| 218 |
| 219 // If the target supports JIT code generation, create a the JIT. |
| 220 if (TargetJITInfo *TJ = TM->getJITInfo()) { |
| 221 return new JIT(MP, *TM, *TJ, JMM, OptLevel, GVsWithCode); |
| 222 } else { |
| 223 if (ErrorStr) |
| 224 *ErrorStr = "target does not support JIT code generation"; |
| 225 return 0; |
| 226 } |
207 } | 227 } |
208 | 228 |
209 JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji, | 229 JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji, |
210 JITMemoryManager *JMM, CodeGenOpt::Level OptLevel) | 230 JITMemoryManager *JMM, CodeGenOpt::Level OptLevel, bool GVsWithCode) |
211 : ExecutionEngine(MP), TM(tm), TJI(tji) { | 231 : ExecutionEngine(MP), TM(tm), TJI(tji), AllocateGVsWithCode(GVsWithCode) { |
212 setTargetData(TM.getTargetData()); | 232 setTargetData(TM.getTargetData()); |
213 | 233 |
214 jitstate = new JITState(MP); | 234 jitstate = new JITState(MP); |
215 | 235 |
216 // Initialize JCE | 236 // Initialize JCE |
217 JCE = createEmitter(*this, JMM, TM); | 237 JCE = createEmitter(*this, JMM, TM); |
218 | 238 |
219 // Add target data | 239 // Add target data |
220 MutexGuard locked(lock); | 240 MutexGuard locked(lock); |
221 FunctionPassManager &PM = jitstate->getPM(locked); | 241 FunctionPassManager &PM = jitstate->getPM(locked); |
222 PM.add(new TargetData(*TM.getTargetData())); | 242 PM.add(new TargetData(*TM.getTargetData())); |
223 | 243 |
224 // Turn the machine code intermediate representation into bytes in memory that | 244 // Turn the machine code intermediate representation into bytes in memory that |
225 // may be executed. | 245 // may be executed. |
226 if (TM.addPassesToEmitMachineCode(PM, *JCE, OptLevel)) { | 246 if (TM.addPassesToEmitMachineCode(PM, *JCE, OptLevel)) { |
227 cerr << "Target does not support machine code emission!\n"; | 247 llvm_report_error("Target does not support machine code emission!"); |
228 abort(); | |
229 } | 248 } |
230 ·· | 249 ·· |
231 // Register routine for informing unwinding runtime about new EH frames | 250 // Register routine for informing unwinding runtime about new EH frames |
232 #if defined(__GNUC__) && !defined(__ARM_EABI__) | 251 #if defined(__GNUC__) && !defined(__ARM_EABI__) |
233 #if USE_KEYMGR | 252 #if USE_KEYMGR |
234 struct LibgccObjectInfo* LOI = (struct LibgccObjectInfo*) | 253 struct LibgccObjectInfo* LOI = (struct LibgccObjectInfo*) |
235 _keymgr_get_and_lock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST); | 254 _keymgr_get_and_lock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST); |
236 ·· | 255 ·· |
237 // The key is created on demand, and libgcc creates it the first time an | 256 // The key is created on demand, and libgcc creates it the first time an |
238 // exception occurs. Since we need the key to register frames, we create | 257 // exception occurs. Since we need the key to register frames, we create |
(...skipping 27 matching lines...) Expand all Loading... |
266 assert(!jitstate && "jitstate should be NULL if Modules vector is empty!"); | 285 assert(!jitstate && "jitstate should be NULL if Modules vector is empty!"); |
267 | 286 |
268 jitstate = new JITState(MP); | 287 jitstate = new JITState(MP); |
269 | 288 |
270 FunctionPassManager &PM = jitstate->getPM(locked); | 289 FunctionPassManager &PM = jitstate->getPM(locked); |
271 PM.add(new TargetData(*TM.getTargetData())); | 290 PM.add(new TargetData(*TM.getTargetData())); |
272 | 291 |
273 // Turn the machine code intermediate representation into bytes in memory | 292 // Turn the machine code intermediate representation into bytes in memory |
274 // that may be executed. | 293 // that may be executed. |
275 if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) { | 294 if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) { |
276 cerr << "Target does not support machine code emission!\n"; | 295 llvm_report_error("Target does not support machine code emission!"); |
277 abort(); | |
278 } | 296 } |
279 ···· | 297 ···· |
280 // Initialize passes. | 298 // Initialize passes. |
281 PM.doInitialization(); | 299 PM.doInitialization(); |
282 } | 300 } |
283 ·· | 301 ·· |
284 ExecutionEngine::addModuleProvider(MP); | 302 ExecutionEngine::addModuleProvider(MP); |
285 } | 303 } |
286 | 304 |
287 /// removeModuleProvider - If we are removing the last ModuleProvider,· | 305 /// removeModuleProvider - If we are removing the last ModuleProvider,· |
(...skipping 11 matching lines...) Expand all Loading... |
299 ·· | 317 ·· |
300 if (!jitstate && !Modules.empty()) { | 318 if (!jitstate && !Modules.empty()) { |
301 jitstate = new JITState(Modules[0]); | 319 jitstate = new JITState(Modules[0]); |
302 | 320 |
303 FunctionPassManager &PM = jitstate->getPM(locked); | 321 FunctionPassManager &PM = jitstate->getPM(locked); |
304 PM.add(new TargetData(*TM.getTargetData())); | 322 PM.add(new TargetData(*TM.getTargetData())); |
305 ···· | 323 ···· |
306 // Turn the machine code intermediate representation into bytes in memory | 324 // Turn the machine code intermediate representation into bytes in memory |
307 // that may be executed. | 325 // that may be executed. |
308 if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) { | 326 if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) { |
309 cerr << "Target does not support machine code emission!\n"; | 327 llvm_report_error("Target does not support machine code emission!"); |
310 abort(); | |
311 } | 328 } |
312 ···· | 329 ···· |
313 // Initialize passes. | 330 // Initialize passes. |
314 PM.doInitialization(); | 331 PM.doInitialization(); |
315 }···· | 332 }···· |
316 return result; | 333 return result; |
317 } | 334 } |
318 | 335 |
319 /// deleteModuleProvider - Remove a ModuleProvider from the list of modules, | 336 /// deleteModuleProvider - Remove a ModuleProvider from the list of modules, |
320 /// and deletes the ModuleProvider and owned Module. Avoids materializing· | 337 /// and deletes the ModuleProvider and owned Module. Avoids materializing· |
(...skipping 10 matching lines...) Expand all Loading... |
331 | 348 |
332 if (!jitstate && !Modules.empty()) { | 349 if (!jitstate && !Modules.empty()) { |
333 jitstate = new JITState(Modules[0]); | 350 jitstate = new JITState(Modules[0]); |
334 ···· | 351 ···· |
335 FunctionPassManager &PM = jitstate->getPM(locked); | 352 FunctionPassManager &PM = jitstate->getPM(locked); |
336 PM.add(new TargetData(*TM.getTargetData())); | 353 PM.add(new TargetData(*TM.getTargetData())); |
337 ···· | 354 ···· |
338 // Turn the machine code intermediate representation into bytes in memory | 355 // Turn the machine code intermediate representation into bytes in memory |
339 // that may be executed. | 356 // that may be executed. |
340 if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) { | 357 if (TM.addPassesToEmitMachineCode(PM, *JCE, CodeGenOpt::Default)) { |
341 cerr << "Target does not support machine code emission!\n"; | 358 llvm_report_error("Target does not support machine code emission!"); |
342 abort(); | |
343 } | 359 } |
344 ···· | 360 ···· |
345 // Initialize passes. | 361 // Initialize passes. |
346 PM.doInitialization(); | 362 PM.doInitialization(); |
347 }···· | 363 }···· |
348 } | 364 } |
349 | 365 |
350 /// run - Start execution with the specified function and arguments. | 366 /// run - Start execution with the specified function and arguments. |
351 /// | 367 /// |
352 GenericValue JIT::runFunction(Function *F, | 368 GenericValue JIT::runFunction(Function *F, |
353 const std::vector<GenericValue> &ArgValues) { | 369 const std::vector<GenericValue> &ArgValues) { |
354 assert(F && "Function *F was null at entry to run()"); | 370 assert(F && "Function *F was null at entry to run()"); |
355 | 371 |
356 void *FPtr = getPointerToFunction(F); | 372 void *FPtr = getPointerToFunction(F); |
357 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction"); | 373 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction"); |
358 const FunctionType *FTy = F->getFunctionType(); | 374 const FunctionType *FTy = F->getFunctionType(); |
359 const Type *RetTy = FTy->getReturnType(); | 375 const Type *RetTy = FTy->getReturnType(); |
360 | 376 |
361 assert((FTy->getNumParams() == ArgValues.size() || | 377 assert((FTy->getNumParams() == ArgValues.size() || |
362 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) && | 378 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) && |
363 "Wrong number of arguments passed into function!"); | 379 "Wrong number of arguments passed into function!"); |
364 assert(FTy->getNumParams() == ArgValues.size() && | 380 assert(FTy->getNumParams() == ArgValues.size() && |
365 "This doesn't support passing arguments through varargs (yet)!"); | 381 "This doesn't support passing arguments through varargs (yet)!"); |
366 | 382 |
367 // Handle some common cases first. These cases correspond to common `main' | 383 // Handle some common cases first. These cases correspond to common `main' |
368 // prototypes. | 384 // prototypes. |
369 if (RetTy == Type::Int32Ty || RetTy == Type::VoidTy) { | 385 if (RetTy == Type::getInt32Ty(F->getContext()) || |
| 386 RetTy == Type::getVoidTy(F->getContext())) { |
370 switch (ArgValues.size()) { | 387 switch (ArgValues.size()) { |
371 case 3: | 388 case 3: |
372 if (FTy->getParamType(0) == Type::Int32Ty && | 389 if (FTy->getParamType(0) == Type::getInt32Ty(F->getContext()) && |
373 isa<PointerType>(FTy->getParamType(1)) && | 390 isa<PointerType>(FTy->getParamType(1)) && |
374 isa<PointerType>(FTy->getParamType(2))) { | 391 isa<PointerType>(FTy->getParamType(2))) { |
375 int (*PF)(int, char **, const char **) = | 392 int (*PF)(int, char **, const char **) = |
376 (int(*)(int, char **, const char **))(intptr_t)FPtr; | 393 (int(*)(int, char **, const char **))(intptr_t)FPtr; |
377 | 394 |
378 // Call the function. | 395 // Call the function. |
379 GenericValue rv; | 396 GenericValue rv; |
380 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),· | 397 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),· |
381 (char **)GVTOP(ArgValues[1]), | 398 (char **)GVTOP(ArgValues[1]), |
382 (const char **)GVTOP(ArgValues[2]))); | 399 (const char **)GVTOP(ArgValues[2]))); |
383 return rv; | 400 return rv; |
384 } | 401 } |
385 break; | 402 break; |
386 case 2: | 403 case 2: |
387 if (FTy->getParamType(0) == Type::Int32Ty && | 404 if (FTy->getParamType(0) == Type::getInt32Ty(F->getContext()) && |
388 isa<PointerType>(FTy->getParamType(1))) { | 405 isa<PointerType>(FTy->getParamType(1))) { |
389 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr; | 406 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr; |
390 | 407 |
391 // Call the function. | 408 // Call the function. |
392 GenericValue rv; | 409 GenericValue rv; |
393 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),· | 410 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),· |
394 (char **)GVTOP(ArgValues[1]))); | 411 (char **)GVTOP(ArgValues[1]))); |
395 return rv; | 412 return rv; |
396 } | 413 } |
397 break; | 414 break; |
398 case 1: | 415 case 1: |
399 if (FTy->getNumParams() == 1 && | 416 if (FTy->getNumParams() == 1 && |
400 FTy->getParamType(0) == Type::Int32Ty) { | 417 FTy->getParamType(0) == Type::getInt32Ty(F->getContext())) { |
401 GenericValue rv; | 418 GenericValue rv; |
402 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr; | 419 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr; |
403 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue())); | 420 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue())); |
404 return rv; | 421 return rv; |
405 } | 422 } |
406 break; | 423 break; |
407 } | 424 } |
408 } | 425 } |
409 | 426 |
410 // Handle cases where no arguments are passed first. | 427 // Handle cases where no arguments are passed first. |
411 if (ArgValues.empty()) { | 428 if (ArgValues.empty()) { |
412 GenericValue rv; | 429 GenericValue rv; |
413 switch (RetTy->getTypeID()) { | 430 switch (RetTy->getTypeID()) { |
414 default: assert(0 && "Unknown return type for function call!"); | 431 default: llvm_unreachable("Unknown return type for function call!"); |
415 case Type::IntegerTyID: { | 432 case Type::IntegerTyID: { |
416 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth(); | 433 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth(); |
417 if (BitWidth == 1) | 434 if (BitWidth == 1) |
418 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)()); | 435 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)()); |
419 else if (BitWidth <= 8) | 436 else if (BitWidth <= 8) |
420 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)()); | 437 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)()); |
421 else if (BitWidth <= 16) | 438 else if (BitWidth <= 16) |
422 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)()); | 439 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)()); |
423 else if (BitWidth <= 32) | 440 else if (BitWidth <= 32) |
424 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)()); | 441 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)()); |
425 else if (BitWidth <= 64) | 442 else if (BitWidth <= 64) |
426 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)()); | 443 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)()); |
427 else· | 444 else· |
428 assert(0 && "Integer types > 64 bits not supported"); | 445 llvm_unreachable("Integer types > 64 bits not supported"); |
429 return rv; | 446 return rv; |
430 } | 447 } |
431 case Type::VoidTyID: | 448 case Type::VoidTyID: |
432 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)()); | 449 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)()); |
433 return rv; | 450 return rv; |
434 case Type::FloatTyID: | 451 case Type::FloatTyID: |
435 rv.FloatVal = ((float(*)())(intptr_t)FPtr)(); | 452 rv.FloatVal = ((float(*)())(intptr_t)FPtr)(); |
436 return rv; | 453 return rv; |
437 case Type::DoubleTyID: | 454 case Type::DoubleTyID: |
438 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)(); | 455 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)(); |
439 return rv; | 456 return rv; |
440 case Type::X86_FP80TyID: | 457 case Type::X86_FP80TyID: |
441 case Type::FP128TyID: | 458 case Type::FP128TyID: |
442 case Type::PPC_FP128TyID: | 459 case Type::PPC_FP128TyID: |
443 assert(0 && "long double not supported yet"); | 460 llvm_unreachable("long double not supported yet"); |
444 return rv; | 461 return rv; |
445 case Type::PointerTyID: | 462 case Type::PointerTyID: |
446 return PTOGV(((void*(*)())(intptr_t)FPtr)()); | 463 return PTOGV(((void*(*)())(intptr_t)FPtr)()); |
447 } | 464 } |
448 } | 465 } |
449 | 466 |
450 // Okay, this is not one of our quick and easy cases. Because we don't have a | 467 // Okay, this is not one of our quick and easy cases. Because we don't have a |
451 // full FFI, we have to codegen a nullary stub function that just calls the | 468 // full FFI, we have to codegen a nullary stub function that just calls the |
452 // function we are interested in, passing in constants for all of the | 469 // function we are interested in, passing in constants for all of the |
453 // arguments. Make this function and return. | 470 // arguments. Make this function and return. |
454 | 471 |
455 // First, create the function. | 472 // First, create the function. |
456 FunctionType *STy=FunctionType::get(RetTy, false); | 473 FunctionType *STy=FunctionType::get(RetTy, false); |
457 Function *Stub = Function::Create(STy, Function::InternalLinkage, "", | 474 Function *Stub = Function::Create(STy, Function::InternalLinkage, "", |
458 F->getParent()); | 475 F->getParent()); |
459 | 476 |
460 // Insert a basic block. | 477 // Insert a basic block. |
461 BasicBlock *StubBB = BasicBlock::Create("", Stub); | 478 BasicBlock *StubBB = BasicBlock::Create(F->getContext(), "", Stub); |
462 | 479 |
463 // Convert all of the GenericValue arguments over to constants. Note that we | 480 // Convert all of the GenericValue arguments over to constants. Note that we |
464 // currently don't support varargs. | 481 // currently don't support varargs. |
465 SmallVector<Value*, 8> Args; | 482 SmallVector<Value*, 8> Args; |
466 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) { | 483 for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) { |
467 Constant *C = 0; | 484 Constant *C = 0; |
468 const Type *ArgTy = FTy->getParamType(i); | 485 const Type *ArgTy = FTy->getParamType(i); |
469 const GenericValue &AV = ArgValues[i]; | 486 const GenericValue &AV = ArgValues[i]; |
470 switch (ArgTy->getTypeID()) { | 487 switch (ArgTy->getTypeID()) { |
471 default: assert(0 && "Unknown argument type for function call!"); | 488 default: llvm_unreachable("Unknown argument type for function call!"); |
472 case Type::IntegerTyID: | 489 case Type::IntegerTyID: |
473 C = ConstantInt::get(AV.IntVal); | 490 C = ConstantInt::get(F->getContext(), AV.IntVal); |
474 break; | 491 break; |
475 case Type::FloatTyID: | 492 case Type::FloatTyID: |
476 C = ConstantFP::get(APFloat(AV.FloatVal)); | 493 C = ConstantFP::get(F->getContext(), APFloat(AV.FloatVal)); |
477 break; | 494 break; |
478 case Type::DoubleTyID: | 495 case Type::DoubleTyID: |
479 C = ConstantFP::get(APFloat(AV.DoubleVal)); | 496 C = ConstantFP::get(F->getContext(), APFloat(AV.DoubleVal)); |
480 break; | 497 break; |
481 case Type::PPC_FP128TyID: | 498 case Type::PPC_FP128TyID: |
482 case Type::X86_FP80TyID: | 499 case Type::X86_FP80TyID: |
483 case Type::FP128TyID: | 500 case Type::FP128TyID: |
484 C = ConstantFP::get(APFloat(AV.IntVal)); | 501 C = ConstantFP::get(F->getContext(), APFloat(AV.IntVal)); |
485 break; | 502 break; |
486 case Type::PointerTyID: | 503 case Type::PointerTyID: |
487 void *ArgPtr = GVTOP(AV); | 504 void *ArgPtr = GVTOP(AV); |
488 if (sizeof(void*) == 4) | 505 if (sizeof(void*) == 4) |
489 C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr); | 506 C = ConstantInt::get(Type::getInt32Ty(F->getContext()),· |
| 507 (int)(intptr_t)ArgPtr); |
490 else | 508 else |
491 C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr); | 509 C = ConstantInt::get(Type::getInt64Ty(F->getContext()), |
492 C = ConstantExpr::getIntToPtr(C, ArgTy); // Cast the integer to pointer | 510 (intptr_t)ArgPtr); |
| 511 // Cast the integer to pointer |
| 512 C = ConstantExpr::getIntToPtr(C, ArgTy); |
493 break; | 513 break; |
494 } | 514 } |
495 Args.push_back(C); | 515 Args.push_back(C); |
496 } | 516 } |
497 | 517 |
498 CallInst *TheCall = CallInst::Create(F, Args.begin(), Args.end(), | 518 CallInst *TheCall = CallInst::Create(F, Args.begin(), Args.end(), |
499 "", StubBB); | 519 "", StubBB); |
500 TheCall->setCallingConv(F->getCallingConv()); | 520 TheCall->setCallingConv(F->getCallingConv()); |
501 TheCall->setTailCall(); | 521 TheCall->setTailCall(); |
502 if (TheCall->getType() != Type::VoidTy) | 522 if (TheCall->getType() != Type::getVoidTy(F->getContext())) |
503 ReturnInst::Create(TheCall, StubBB); // Return result of the call. | 523 // Return result of the call. |
| 524 ReturnInst::Create(F->getContext(), TheCall, StubBB); |
504 else | 525 else |
505 ReturnInst::Create(StubBB); // Just return void. | 526 ReturnInst::Create(F->getContext(), StubBB); // Just return void. |
506 | 527 |
507 // Finally, return the value returned by our nullary stub function. | 528 // Finally, return the value returned by our nullary stub function. |
508 return runFunction(Stub, std::vector<GenericValue>()); | 529 return runFunction(Stub, std::vector<GenericValue>()); |
509 } | 530 } |
510 | 531 |
511 void JIT::RegisterJITEventListener(JITEventListener *L) { | 532 void JIT::RegisterJITEventListener(JITEventListener *L) { |
512 if (L == NULL) | 533 if (L == NULL) |
513 return; | 534 return; |
514 MutexGuard locked(lock); | 535 MutexGuard locked(lock); |
515 EventListeners.push_back(L); | 536 EventListeners.push_back(L); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
622 for (unsigned i = 0, e = Modules.size(); i != e; ++i) { | 643 for (unsigned i = 0, e = Modules.size(); i != e; ++i) { |
623 if (Modules[i]->getModule() == M) { | 644 if (Modules[i]->getModule() == M) { |
624 MP = Modules[i]; | 645 MP = Modules[i]; |
625 break; | 646 break; |
626 } | 647 } |
627 } | 648 } |
628 assert(MP && "Function isn't in a module we know about!"); | 649 assert(MP && "Function isn't in a module we know about!"); |
629 ···· | 650 ···· |
630 std::string ErrorMsg; | 651 std::string ErrorMsg; |
631 if (MP->materializeFunction(F, &ErrorMsg)) { | 652 if (MP->materializeFunction(F, &ErrorMsg)) { |
632 cerr << "Error reading function '" << F->getName() | 653 llvm_report_error("Error reading function '" + F->getName()+ |
633 << "' from bitcode file: " << ErrorMsg << "\n"; | 654 "' from bitcode file: " + ErrorMsg); |
634 abort(); | |
635 } | 655 } |
636 | 656 |
637 // Now retry to get the address. | 657 // Now retry to get the address. |
638 if (void *Addr = getPointerToGlobalIfAvailable(F)) | 658 if (void *Addr = getPointerToGlobalIfAvailable(F)) |
639 return Addr; | 659 return Addr; |
640 } | 660 } |
641 | 661 |
642 if (F->isDeclaration()) { | 662 if (F->isDeclaration()) { |
643 bool AbortOnFailure = | 663 bool AbortOnFailure = |
644 !areDlsymStubsEnabled() && !F->hasExternalWeakLinkage(); | 664 !areDlsymStubsEnabled() && !F->hasExternalWeakLinkage(); |
(...skipping 17 matching lines...) Expand all Loading... |
662 | 682 |
663 void *Ptr = getPointerToGlobalIfAvailable(GV); | 683 void *Ptr = getPointerToGlobalIfAvailable(GV); |
664 if (Ptr) return Ptr; | 684 if (Ptr) return Ptr; |
665 | 685 |
666 // If the global is external, just remember the address. | 686 // If the global is external, just remember the address. |
667 if (GV->isDeclaration()) { | 687 if (GV->isDeclaration()) { |
668 #if HAVE___DSO_HANDLE | 688 #if HAVE___DSO_HANDLE |
669 if (GV->getName() == "__dso_handle") | 689 if (GV->getName() == "__dso_handle") |
670 return (void*)&__dso_handle; | 690 return (void*)&__dso_handle; |
671 #endif | 691 #endif |
672 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str()); | 692 Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName()); |
673 if (Ptr == 0 && !areDlsymStubsEnabled()) { | 693 if (Ptr == 0 && !areDlsymStubsEnabled()) { |
674 cerr << "Could not resolve external global address: " | 694 llvm_report_error("Could not resolve external global address: " |
675 << GV->getName() << "\n"; | 695 +GV->getName()); |
676 abort(); | |
677 } | 696 } |
678 addGlobalMapping(GV, Ptr); | 697 addGlobalMapping(GV, Ptr); |
679 } else { | 698 } else { |
680 // GlobalVariable's which are not "constant" will cause trouble in a server | |
681 // situation. It's returned in the same block of memory as code which may | |
682 // not be writable. | |
683 if (isGVCompilationDisabled() && !GV->isConstant()) { | |
684 cerr << "Compilation of non-internal GlobalValue is disabled!\n"; | |
685 abort(); | |
686 } | |
687 // If the global hasn't been emitted to memory yet, allocate space and | 699 // If the global hasn't been emitted to memory yet, allocate space and |
688 // emit it into memory. It goes in the same array as the generated | 700 // emit it into memory. |
689 // code, jump tables, etc. | 701 Ptr = getMemoryForGV(GV); |
690 const Type *GlobalType = GV->getType()->getElementType(); | |
691 size_t S = getTargetData()->getTypeAllocSize(GlobalType); | |
692 size_t A = getTargetData()->getPreferredAlignment(GV); | |
693 if (GV->isThreadLocal()) { | |
694 MutexGuard locked(lock); | |
695 Ptr = TJI.allocateThreadLocalMemory(S); | |
696 } else if (TJI.allocateSeparateGVMemory()) { | |
697 if (A <= 8) { | |
698 Ptr = malloc(S); | |
699 } else { | |
700 // Allocate S+A bytes of memory, then use an aligned pointer within that | |
701 // space. | |
702 Ptr = malloc(S+A); | |
703 unsigned MisAligned = ((intptr_t)Ptr & (A-1)); | |
704 Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0); | |
705 } | |
706 } else { | |
707 Ptr = JCE->allocateSpace(S, A); | |
708 } | |
709 addGlobalMapping(GV, Ptr); | 702 addGlobalMapping(GV, Ptr); |
710 EmitGlobalVariable(GV); | 703 EmitGlobalVariable(GV); // Initialize the variable. |
711 } | 704 } |
712 return Ptr; | 705 return Ptr; |
713 } | 706 } |
714 | 707 |
715 /// recompileAndRelinkFunction - This method is used to force a function | 708 /// recompileAndRelinkFunction - This method is used to force a function |
716 /// which has already been compiled, to be compiled again, possibly | 709 /// which has already been compiled, to be compiled again, possibly |
717 /// after it has been modified. Then the entry to the old copy is overwritten | 710 /// after it has been modified. Then the entry to the old copy is overwritten |
718 /// with a branch to the new copy. If there was no old copy, this acts | 711 /// with a branch to the new copy. If there was no old copy, this acts |
719 /// just like JIT::getPointerToFunction(). | 712 /// just like JIT::getPointerToFunction(). |
720 /// | 713 /// |
(...skipping 14 matching lines...) Expand all Loading... |
735 assert(Addr && "Code generation didn't add function to GlobalAddress table!"); | 728 assert(Addr && "Code generation didn't add function to GlobalAddress table!"); |
736 TJI.replaceMachineCodeForFunction(OldAddr, Addr); | 729 TJI.replaceMachineCodeForFunction(OldAddr, Addr); |
737 return Addr; | 730 return Addr; |
738 } | 731 } |
739 | 732 |
740 /// getMemoryForGV - This method abstracts memory allocation of global | 733 /// getMemoryForGV - This method abstracts memory allocation of global |
741 /// variable so that the JIT can allocate thread local variables depending | 734 /// variable so that the JIT can allocate thread local variables depending |
742 /// on the target. | 735 /// on the target. |
743 /// | 736 /// |
744 char* JIT::getMemoryForGV(const GlobalVariable* GV) { | 737 char* JIT::getMemoryForGV(const GlobalVariable* GV) { |
745 const Type *ElTy = GV->getType()->getElementType(); | 738 char *Ptr; |
746 size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy); | 739 |
| 740 // GlobalVariable's which are not "constant" will cause trouble in a server |
| 741 // situation. It's returned in the same block of memory as code which may |
| 742 // not be writable. |
| 743 if (isGVCompilationDisabled() && !GV->isConstant()) { |
| 744 llvm_report_error("Compilation of non-internal GlobalValue is disabled!"); |
| 745 } |
| 746 |
| 747 // Some applications require globals and code to live together, so they may |
| 748 // be allocated into the same buffer, but in general globals are allocated |
| 749 // through the memory manager which puts them near the code but not in the |
| 750 // same buffer. |
| 751 const Type *GlobalType = GV->getType()->getElementType(); |
| 752 size_t S = getTargetData()->getTypeAllocSize(GlobalType); |
| 753 size_t A = getTargetData()->getPreferredAlignment(GV); |
747 if (GV->isThreadLocal()) { | 754 if (GV->isThreadLocal()) { |
748 MutexGuard locked(lock); | 755 MutexGuard locked(lock); |
749 return TJI.allocateThreadLocalMemory(GVSize); | 756 Ptr = TJI.allocateThreadLocalMemory(S); |
| 757 } else if (TJI.allocateSeparateGVMemory()) { |
| 758 if (A <= 8) { |
| 759 Ptr = (char*)malloc(S); |
| 760 } else { |
| 761 // Allocate S+A bytes of memory, then use an aligned pointer within that |
| 762 // space. |
| 763 Ptr = (char*)malloc(S+A); |
| 764 unsigned MisAligned = ((intptr_t)Ptr & (A-1)); |
| 765 Ptr = Ptr + (MisAligned ? (A-MisAligned) : 0); |
| 766 } |
| 767 } else if (AllocateGVsWithCode) { |
| 768 Ptr = (char*)JCE->allocateSpace(S, A); |
750 } else { | 769 } else { |
751 return new char[GVSize]; | 770 Ptr = (char*)JCE->allocateGlobal(S, A); |
752 } | 771 } |
| 772 return Ptr; |
753 } | 773 } |
754 | 774 |
755 void JIT::addPendingFunction(Function *F) { | 775 void JIT::addPendingFunction(Function *F) { |
756 MutexGuard locked(lock); | 776 MutexGuard locked(lock); |
757 jitstate->getPendingFunctions(locked).push_back(F); | 777 jitstate->getPendingFunctions(locked).push_back(F); |
758 } | 778 } |
759 | 779 |
760 | 780 |
761 JITEventListener::~JITEventListener() {} | 781 JITEventListener::~JITEventListener() {} |
LEFT | RIGHT |