Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 1 | //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 10 | // This tool implements a just-in-time compiler for LLVM, allowing direct |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 11 | // execution of LLVM bitcode in an efficient manner. |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 15 | #include "JIT.h" |
Chris Lattner | cc22e9f | 2004-08-16 00:14:18 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 18 | #include "llvm/Function.h" |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 19 | #include "llvm/GlobalVariable.h" |
Chris Lattner | cc22e9f | 2004-08-16 00:14:18 +0000 | [diff] [blame] | 20 | #include "llvm/Instructions.h" |
Misha Brukman | 0f4f7d9 | 2003-10-16 21:19:34 +0000 | [diff] [blame] | 21 | #include "llvm/ModuleProvider.h" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
Brian Gaeke | 9722294 | 2003-09-05 19:39:22 +0000 | [diff] [blame] | 23 | #include "llvm/ExecutionEngine/GenericValue.h" |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MutexGuard.h" |
Reid Spencer | df5a37e | 2004-11-29 14:11:29 +0000 | [diff] [blame] | 25 | #include "llvm/System/DynamicLibrary.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetJITInfo.h" |
Anton Korobeynikov | 3b30a6e | 2007-07-30 20:02:02 +0000 | [diff] [blame] | 29 | |
| 30 | #include "llvm/Config/config.h" |
| 31 | |
Chris Lattner | c19aade | 2003-12-08 08:06:28 +0000 | [diff] [blame] | 32 | using namespace llvm; |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 33 | |
Nate Begeman | b76ea74 | 2006-07-22 16:59:38 +0000 | [diff] [blame] | 34 | #ifdef __APPLE__ |
Anton Korobeynikov | 3b30a6e | 2007-07-30 20:02:02 +0000 | [diff] [blame] | 35 | // Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead |
| 36 | // of atexit). It passes the address of linker generated symbol __dso_handle |
| 37 | // to the function. |
| 38 | // This configuration change happened at version 5330. |
| 39 | # include <AvailabilityMacros.h> |
| 40 | # if defined(MAC_OS_X_VERSION_10_4) && \ |
| 41 | ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \ |
| 42 | (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \ |
| 43 | __APPLE_CC__ >= 5330)) |
| 44 | # ifndef HAVE___DSO_HANDLE |
| 45 | # define HAVE___DSO_HANDLE 1 |
| 46 | # endif |
| 47 | # endif |
Evan Cheng | 5f42c55 | 2006-07-21 23:06:20 +0000 | [diff] [blame] | 48 | #endif |
Anton Korobeynikov | 3b30a6e | 2007-07-30 20:02:02 +0000 | [diff] [blame] | 49 | |
| 50 | #if HAVE___DSO_HANDLE |
| 51 | extern void *__dso_handle __attribute__ ((__visibility__ ("hidden"))); |
Nate Begeman | b76ea74 | 2006-07-22 16:59:38 +0000 | [diff] [blame] | 52 | #endif |
Evan Cheng | 5f42c55 | 2006-07-21 23:06:20 +0000 | [diff] [blame] | 53 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 54 | namespace { |
| 55 | |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 56 | static struct RegisterJIT { |
| 57 | RegisterJIT() { JIT::Register(); } |
| 58 | } JITRegistrator; |
| 59 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Jeff Cohen | 2f51914 | 2006-03-24 02:53:49 +0000 | [diff] [blame] | 62 | namespace llvm { |
| 63 | void LinkInJIT() { |
| 64 | } |
| 65 | } |
| 66 | |
Nicolas Geoffray | d046fc6 | 2008-08-28 22:34:49 +0000 | [diff] [blame] | 67 | |
Evan Cheng | 2d5424d | 2009-02-01 06:42:27 +0000 | [diff] [blame^] | 68 | #if defined(__GNUC__) && !defined(__ARM__EABI__) |
Nicolas Geoffray | d046fc6 | 2008-08-28 22:34:49 +0000 | [diff] [blame] | 69 | |
| 70 | // libgcc defines the __register_frame function to dynamically register new |
| 71 | // dwarf frames for exception handling. This functionality is not portable |
| 72 | // across compilers and is only provided by GCC. We use the __register_frame |
| 73 | // function here so that code generated by the JIT cooperates with the unwinding |
| 74 | // runtime of libgcc. When JITting with exception handling enable, LLVM |
| 75 | // generates dwarf frames and registers it to libgcc with __register_frame. |
| 76 | // |
| 77 | // The __register_frame function works with Linux. |
| 78 | // |
| 79 | // Unfortunately, this functionality seems to be in libgcc after the unwinding |
| 80 | // library of libgcc for darwin was written. The code for darwin overwrites the |
| 81 | // value updated by __register_frame with a value fetched with "keymgr". |
| 82 | // "keymgr" is an obsolete functionality, which should be rewritten some day. |
| 83 | // In the meantime, since "keymgr" is on all libgccs shipped with apple-gcc, we |
| 84 | // need a workaround in LLVM which uses the "keymgr" to dynamically modify the |
| 85 | // values of an opaque key, used by libgcc to find dwarf tables. |
| 86 | |
Anton Korobeynikov | 299d9d7 | 2008-03-22 08:53:09 +0000 | [diff] [blame] | 87 | extern "C" void __register_frame(void*); |
Nicolas Geoffray | d046fc6 | 2008-08-28 22:34:49 +0000 | [diff] [blame] | 88 | |
Evan Cheng | 2d5424d | 2009-02-01 06:42:27 +0000 | [diff] [blame^] | 89 | #if defined(__APPLE__) |
Nicolas Geoffray | d046fc6 | 2008-08-28 22:34:49 +0000 | [diff] [blame] | 90 | |
| 91 | namespace { |
| 92 | |
| 93 | // LibgccObject - This is the structure defined in libgcc. There is no #include |
| 94 | // provided for this structure, so we also define it here. libgcc calls it |
| 95 | // "struct object". The structure is undocumented in libgcc. |
| 96 | struct LibgccObject { |
| 97 | void *unused1; |
| 98 | void *unused2; |
| 99 | void *unused3; |
| 100 | |
| 101 | /// frame - Pointer to the exception table. |
| 102 | void *frame; |
| 103 | |
| 104 | /// encoding - The encoding of the object? |
| 105 | union { |
| 106 | struct { |
| 107 | unsigned long sorted : 1; |
| 108 | unsigned long from_array : 1; |
| 109 | unsigned long mixed_encoding : 1; |
| 110 | unsigned long encoding : 8; |
| 111 | unsigned long count : 21; |
| 112 | } b; |
| 113 | size_t i; |
| 114 | } encoding; |
| 115 | |
| 116 | /// fde_end - libgcc defines this field only if some macro is defined. We |
| 117 | /// include this field even if it may not there, to make libgcc happy. |
| 118 | char *fde_end; |
| 119 | |
| 120 | /// next - At least we know it's a chained list! |
| 121 | struct LibgccObject *next; |
| 122 | }; |
| 123 | |
| 124 | // "kemgr" stuff. Apparently, all frame tables are stored there. |
| 125 | extern "C" void _keymgr_set_and_unlock_processwide_ptr(int, void *); |
| 126 | extern "C" void *_keymgr_get_and_lock_processwide_ptr(int); |
| 127 | #define KEYMGR_GCC3_DW2_OBJ_LIST 302 /* Dwarf2 object list */ |
| 128 | |
| 129 | /// LibgccObjectInfo - libgcc defines this struct as km_object_info. It |
| 130 | /// probably contains all dwarf tables that are loaded. |
| 131 | struct LibgccObjectInfo { |
| 132 | |
| 133 | /// seenObjects - LibgccObjects already parsed by the unwinding runtime. |
| 134 | /// |
| 135 | struct LibgccObject* seenObjects; |
| 136 | |
| 137 | /// unseenObjects - LibgccObjects not parsed yet by the unwinding runtime. |
| 138 | /// |
| 139 | struct LibgccObject* unseenObjects; |
| 140 | |
| 141 | unsigned unused[2]; |
| 142 | }; |
| 143 | |
| 144 | // for DW_EH_PE_omit |
| 145 | #include "llvm/Support/Dwarf.h" |
| 146 | |
| 147 | /// darwin_register_frame - Since __register_frame does not work with darwin's |
| 148 | /// libgcc,we provide our own function, which "tricks" libgcc by modifying the |
| 149 | /// "Dwarf2 object list" key. |
| 150 | void DarwinRegisterFrame(void* FrameBegin) { |
| 151 | // Get the key. |
| 152 | struct LibgccObjectInfo* LOI = (struct LibgccObjectInfo*) |
| 153 | _keymgr_get_and_lock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST); |
| 154 | |
| 155 | // Allocate a new LibgccObject to represent this frame. Deallocation of this |
| 156 | // object may be impossible: since darwin code in libgcc was written after |
| 157 | // the ability to dynamically register frames, things may crash if we |
| 158 | // deallocate it. |
| 159 | struct LibgccObject* ob = (struct LibgccObject*) |
| 160 | malloc(sizeof(struct LibgccObject)); |
| 161 | |
| 162 | // Do like libgcc for the values of the field. |
| 163 | ob->unused1 = (void *)-1; |
| 164 | ob->unused2 = 0; |
| 165 | ob->unused3 = 0; |
| 166 | ob->frame = FrameBegin; |
| 167 | ob->encoding.i = 0; |
| 168 | ob->encoding.b.encoding = llvm::dwarf::DW_EH_PE_omit; |
| 169 | |
| 170 | // Put the info on both places, as libgcc uses the first or the the second |
| 171 | // field. Note that we rely on having two pointers here. If fde_end was a |
| 172 | // char, things would get complicated. |
| 173 | ob->fde_end = (char*)LOI->unseenObjects; |
| 174 | ob->next = LOI->unseenObjects; |
| 175 | |
| 176 | // Update the key's unseenObjects list. |
| 177 | LOI->unseenObjects = ob; |
| 178 | |
| 179 | // Finally update the "key". Apparently, libgcc requires it. |
| 180 | _keymgr_set_and_unlock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST, |
| 181 | LOI); |
| 182 | |
| 183 | } |
| 184 | |
| 185 | } |
| 186 | #endif // __APPLE__ |
| 187 | #endif // __GNUC__ |
Anton Korobeynikov | 299d9d7 | 2008-03-22 08:53:09 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 34c9433 | 2007-12-06 01:34:04 +0000 | [diff] [blame] | 189 | /// createJIT - This is the factory method for creating a JIT for the current |
| 190 | /// machine, it does not fall back to the interpreter. This takes ownership |
| 191 | /// of the module provider. |
| 192 | ExecutionEngine *ExecutionEngine::createJIT(ModuleProvider *MP, |
| 193 | std::string *ErrorStr, |
Evan Cheng | 502f20b | 2008-08-08 08:11:34 +0000 | [diff] [blame] | 194 | JITMemoryManager *JMM, |
| 195 | bool Fast) { |
| 196 | ExecutionEngine *EE = JIT::createJIT(MP, ErrorStr, JMM, Fast); |
Chris Lattner | 34c9433 | 2007-12-06 01:34:04 +0000 | [diff] [blame] | 197 | if (!EE) return 0; |
| 198 | |
Chris Lattner | 34c9433 | 2007-12-06 01:34:04 +0000 | [diff] [blame] | 199 | // Make sure we can resolve symbols in the program as well. The zero arg |
| 200 | // to the function tells DynamicLibrary to load the program, not a library. |
| 201 | sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr); |
| 202 | return EE; |
| 203 | } |
| 204 | |
| 205 | JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji, |
Evan Cheng | 502f20b | 2008-08-08 08:11:34 +0000 | [diff] [blame] | 206 | JITMemoryManager *JMM, bool Fast) |
Nate Begeman | f049e07 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 207 | : ExecutionEngine(MP), TM(tm), TJI(tji) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 208 | setTargetData(TM.getTargetData()); |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 209 | |
Nate Begeman | f049e07 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 210 | jitstate = new JITState(MP); |
| 211 | |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 212 | // Initialize MCE |
Chris Lattner | 34c9433 | 2007-12-06 01:34:04 +0000 | [diff] [blame] | 213 | MCE = createEmitter(*this, JMM); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 214 | |
Brian Gaeke | 50872d5 | 2004-04-14 17:45:52 +0000 | [diff] [blame] | 215 | // Add target data |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 216 | MutexGuard locked(lock); |
Nate Begeman | f049e07 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 217 | FunctionPassManager &PM = jitstate->getPM(locked); |
Chris Lattner | b93b034 | 2006-05-04 21:18:40 +0000 | [diff] [blame] | 218 | PM.add(new TargetData(*TM.getTargetData())); |
Brian Gaeke | 50872d5 | 2004-04-14 17:45:52 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 220 | // Turn the machine code intermediate representation into bytes in memory that |
| 221 | // may be executed. |
Evan Cheng | 502f20b | 2008-08-08 08:11:34 +0000 | [diff] [blame] | 222 | if (TM.addPassesToEmitMachineCode(PM, *MCE, Fast)) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 223 | cerr << "Target does not support machine code emission!\n"; |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 224 | abort(); |
| 225 | } |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 226 | |
Nicolas Geoffray | a7ec87c | 2008-08-18 14:53:56 +0000 | [diff] [blame] | 227 | // Register routine for informing unwinding runtime about new EH frames |
Evan Cheng | 2d5424d | 2009-02-01 06:42:27 +0000 | [diff] [blame^] | 228 | #if defined(__GNUC__) && !defined(__ARM_EABI__) |
Nicolas Geoffray | d046fc6 | 2008-08-28 22:34:49 +0000 | [diff] [blame] | 229 | #if defined(__APPLE__) |
| 230 | struct LibgccObjectInfo* LOI = (struct LibgccObjectInfo*) |
| 231 | _keymgr_get_and_lock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST); |
| 232 | |
| 233 | // The key is created on demand, and libgcc creates it the first time an |
| 234 | // exception occurs. Since we need the key to register frames, we create |
| 235 | // it now. |
| 236 | if (!LOI) { |
| 237 | LOI = (LibgccObjectInfo*)malloc(sizeof(struct LibgccObjectInfo)); |
| 238 | _keymgr_set_and_unlock_processwide_ptr(KEYMGR_GCC3_DW2_OBJ_LIST, |
| 239 | LOI); |
| 240 | } |
| 241 | InstallExceptionTableRegister(DarwinRegisterFrame); |
| 242 | #else |
Nicolas Geoffray | a7ec87c | 2008-08-18 14:53:56 +0000 | [diff] [blame] | 243 | InstallExceptionTableRegister(__register_frame); |
Nicolas Geoffray | d046fc6 | 2008-08-28 22:34:49 +0000 | [diff] [blame] | 244 | #endif // __APPLE__ |
| 245 | #endif // __GNUC__ |
Nicolas Geoffray | a7ec87c | 2008-08-18 14:53:56 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 1911fd4 | 2006-09-04 04:14:57 +0000 | [diff] [blame] | 247 | // Initialize passes. |
| 248 | PM.doInitialization(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 251 | JIT::~JIT() { |
Nate Begeman | f049e07 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 252 | delete jitstate; |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 253 | delete MCE; |
| 254 | delete &TM; |
| 255 | } |
| 256 | |
Nate Begeman | f049e07 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 257 | /// addModuleProvider - Add a new ModuleProvider to the JIT. If we previously |
| 258 | /// removed the last ModuleProvider, we need re-initialize jitstate with a valid |
| 259 | /// ModuleProvider. |
| 260 | void JIT::addModuleProvider(ModuleProvider *MP) { |
| 261 | MutexGuard locked(lock); |
| 262 | |
| 263 | if (Modules.empty()) { |
| 264 | assert(!jitstate && "jitstate should be NULL if Modules vector is empty!"); |
| 265 | |
| 266 | jitstate = new JITState(MP); |
| 267 | |
| 268 | FunctionPassManager &PM = jitstate->getPM(locked); |
| 269 | PM.add(new TargetData(*TM.getTargetData())); |
| 270 | |
| 271 | // Turn the machine code intermediate representation into bytes in memory |
| 272 | // that may be executed. |
| 273 | if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) { |
| 274 | cerr << "Target does not support machine code emission!\n"; |
| 275 | abort(); |
| 276 | } |
| 277 | |
| 278 | // Initialize passes. |
| 279 | PM.doInitialization(); |
| 280 | } |
| 281 | |
| 282 | ExecutionEngine::addModuleProvider(MP); |
| 283 | } |
| 284 | |
| 285 | /// removeModuleProvider - If we are removing the last ModuleProvider, |
| 286 | /// invalidate the jitstate since the PassManager it contains references a |
| 287 | /// released ModuleProvider. |
| 288 | Module *JIT::removeModuleProvider(ModuleProvider *MP, std::string *E) { |
| 289 | Module *result = ExecutionEngine::removeModuleProvider(MP, E); |
| 290 | |
| 291 | MutexGuard locked(lock); |
| 292 | if (Modules.empty()) { |
| 293 | delete jitstate; |
| 294 | jitstate = 0; |
| 295 | } |
| 296 | |
| 297 | return result; |
| 298 | } |
| 299 | |
Nate Begeman | 60789e4 | 2009-01-23 19:27:28 +0000 | [diff] [blame] | 300 | /// deleteModuleProvider - Remove a ModuleProvider from the list of modules, |
| 301 | /// and deletes the ModuleProvider and owned Module. Avoids materializing |
| 302 | /// the underlying module. |
| 303 | void JIT::deleteModuleProvider(ModuleProvider *MP, std::string *E) { |
| 304 | ExecutionEngine::deleteModuleProvider(MP, E); |
| 305 | |
| 306 | MutexGuard locked(lock); |
| 307 | if (Modules.empty()) { |
| 308 | delete jitstate; |
| 309 | jitstate = 0; |
| 310 | } |
| 311 | } |
| 312 | |
Brian Gaeke | 70975ee | 2003-09-05 18:42:01 +0000 | [diff] [blame] | 313 | /// run - Start execution with the specified function and arguments. |
Chris Lattner | 05a1a30 | 2003-08-21 21:32:12 +0000 | [diff] [blame] | 314 | /// |
Chris Lattner | ff0f1bb | 2003-12-26 06:13:47 +0000 | [diff] [blame] | 315 | GenericValue JIT::runFunction(Function *F, |
| 316 | const std::vector<GenericValue> &ArgValues) { |
Chris Lattner | b47130c | 2004-08-15 23:29:50 +0000 | [diff] [blame] | 317 | assert(F && "Function *F was null at entry to run()"); |
Chris Lattner | b47130c | 2004-08-15 23:29:50 +0000 | [diff] [blame] | 318 | |
| 319 | void *FPtr = getPointerToFunction(F); |
Chris Lattner | 7c45d78 | 2004-08-15 23:31:43 +0000 | [diff] [blame] | 320 | assert(FPtr && "Pointer to fn's code was null after getPointerToFunction"); |
Chris Lattner | f7bedf4 | 2004-08-15 23:39:59 +0000 | [diff] [blame] | 321 | const FunctionType *FTy = F->getFunctionType(); |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 322 | const Type *RetTy = FTy->getReturnType(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 323 | |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 324 | assert((FTy->getNumParams() <= ArgValues.size() || FTy->isVarArg()) && |
| 325 | "Too many arguments passed into function!"); |
| 326 | assert(FTy->getNumParams() == ArgValues.size() && |
| 327 | "This doesn't support passing arguments through varargs (yet)!"); |
| 328 | |
Misha Brukman | ec84302 | 2004-10-22 23:35:57 +0000 | [diff] [blame] | 329 | // Handle some common cases first. These cases correspond to common `main' |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 330 | // prototypes. |
Chris Lattner | 79e038a | 2007-08-08 16:19:57 +0000 | [diff] [blame] | 331 | if (RetTy == Type::Int32Ty || RetTy == Type::VoidTy) { |
Chris Lattner | f7bedf4 | 2004-08-15 23:39:59 +0000 | [diff] [blame] | 332 | switch (ArgValues.size()) { |
| 333 | case 3: |
Chris Lattner | 79e038a | 2007-08-08 16:19:57 +0000 | [diff] [blame] | 334 | if (FTy->getParamType(0) == Type::Int32Ty && |
Chris Lattner | f7bedf4 | 2004-08-15 23:39:59 +0000 | [diff] [blame] | 335 | isa<PointerType>(FTy->getParamType(1)) && |
| 336 | isa<PointerType>(FTy->getParamType(2))) { |
| 337 | int (*PF)(int, char **, const char **) = |
Chris Lattner | 870286a | 2006-06-01 17:29:22 +0000 | [diff] [blame] | 338 | (int(*)(int, char **, const char **))(intptr_t)FPtr; |
Misha Brukman | ec84302 | 2004-10-22 23:35:57 +0000 | [diff] [blame] | 339 | |
Chris Lattner | f7bedf4 | 2004-08-15 23:39:59 +0000 | [diff] [blame] | 340 | // Call the function. |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 341 | GenericValue rv; |
Reid Spencer | 38f6a15 | 2007-03-06 03:11:31 +0000 | [diff] [blame] | 342 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), |
| 343 | (char **)GVTOP(ArgValues[1]), |
| 344 | (const char **)GVTOP(ArgValues[2]))); |
Chris Lattner | f7bedf4 | 2004-08-15 23:39:59 +0000 | [diff] [blame] | 345 | return rv; |
| 346 | } |
| 347 | break; |
Chris Lattner | 174f226 | 2004-08-16 01:07:04 +0000 | [diff] [blame] | 348 | case 2: |
Chris Lattner | 79e038a | 2007-08-08 16:19:57 +0000 | [diff] [blame] | 349 | if (FTy->getParamType(0) == Type::Int32Ty && |
Chris Lattner | 174f226 | 2004-08-16 01:07:04 +0000 | [diff] [blame] | 350 | isa<PointerType>(FTy->getParamType(1))) { |
Chris Lattner | 870286a | 2006-06-01 17:29:22 +0000 | [diff] [blame] | 351 | int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr; |
Misha Brukman | ec84302 | 2004-10-22 23:35:57 +0000 | [diff] [blame] | 352 | |
Chris Lattner | 174f226 | 2004-08-16 01:07:04 +0000 | [diff] [blame] | 353 | // Call the function. |
| 354 | GenericValue rv; |
Reid Spencer | 38f6a15 | 2007-03-06 03:11:31 +0000 | [diff] [blame] | 355 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), |
| 356 | (char **)GVTOP(ArgValues[1]))); |
Chris Lattner | 174f226 | 2004-08-16 01:07:04 +0000 | [diff] [blame] | 357 | return rv; |
| 358 | } |
| 359 | break; |
Chris Lattner | f7bedf4 | 2004-08-15 23:39:59 +0000 | [diff] [blame] | 360 | case 1: |
| 361 | if (FTy->getNumParams() == 1 && |
Chris Lattner | 79e038a | 2007-08-08 16:19:57 +0000 | [diff] [blame] | 362 | FTy->getParamType(0) == Type::Int32Ty) { |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 363 | GenericValue rv; |
Chris Lattner | 870286a | 2006-06-01 17:29:22 +0000 | [diff] [blame] | 364 | int (*PF)(int) = (int(*)(int))(intptr_t)FPtr; |
Reid Spencer | 38f6a15 | 2007-03-06 03:11:31 +0000 | [diff] [blame] | 365 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue())); |
Chris Lattner | f7bedf4 | 2004-08-15 23:39:59 +0000 | [diff] [blame] | 366 | return rv; |
| 367 | } |
| 368 | break; |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
| 372 | // Handle cases where no arguments are passed first. |
| 373 | if (ArgValues.empty()) { |
| 374 | GenericValue rv; |
| 375 | switch (RetTy->getTypeID()) { |
| 376 | default: assert(0 && "Unknown return type for function call!"); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 377 | case Type::IntegerTyID: { |
| 378 | unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth(); |
| 379 | if (BitWidth == 1) |
Reid Spencer | 38f6a15 | 2007-03-06 03:11:31 +0000 | [diff] [blame] | 380 | rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)()); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 381 | else if (BitWidth <= 8) |
Reid Spencer | 38f6a15 | 2007-03-06 03:11:31 +0000 | [diff] [blame] | 382 | rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)()); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 383 | else if (BitWidth <= 16) |
Reid Spencer | 38f6a15 | 2007-03-06 03:11:31 +0000 | [diff] [blame] | 384 | rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)()); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 385 | else if (BitWidth <= 32) |
Reid Spencer | 38f6a15 | 2007-03-06 03:11:31 +0000 | [diff] [blame] | 386 | rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)()); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 387 | else if (BitWidth <= 64) |
Reid Spencer | 38f6a15 | 2007-03-06 03:11:31 +0000 | [diff] [blame] | 388 | rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)()); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 389 | else |
| 390 | assert(0 && "Integer types > 64 bits not supported"); |
Chris Lattner | d297aea | 2004-08-15 23:34:48 +0000 | [diff] [blame] | 391 | return rv; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 392 | } |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 393 | case Type::VoidTyID: |
Reid Spencer | 38f6a15 | 2007-03-06 03:11:31 +0000 | [diff] [blame] | 394 | rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)()); |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 395 | return rv; |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 396 | case Type::FloatTyID: |
Chris Lattner | 870286a | 2006-06-01 17:29:22 +0000 | [diff] [blame] | 397 | rv.FloatVal = ((float(*)())(intptr_t)FPtr)(); |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 398 | return rv; |
| 399 | case Type::DoubleTyID: |
Chris Lattner | 870286a | 2006-06-01 17:29:22 +0000 | [diff] [blame] | 400 | rv.DoubleVal = ((double(*)())(intptr_t)FPtr)(); |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 401 | return rv; |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 402 | case Type::X86_FP80TyID: |
| 403 | case Type::FP128TyID: |
| 404 | case Type::PPC_FP128TyID: |
| 405 | assert(0 && "long double not supported yet"); |
| 406 | return rv; |
Chris Lattner | e5eab14 | 2004-08-15 23:53:06 +0000 | [diff] [blame] | 407 | case Type::PointerTyID: |
Chris Lattner | 870286a | 2006-06-01 17:29:22 +0000 | [diff] [blame] | 408 | return PTOGV(((void*(*)())(intptr_t)FPtr)()); |
Chris Lattner | d297aea | 2004-08-15 23:34:48 +0000 | [diff] [blame] | 409 | } |
Chris Lattner | ff0f1bb | 2003-12-26 06:13:47 +0000 | [diff] [blame] | 410 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 411 | |
Chris Lattner | cc22e9f | 2004-08-16 00:14:18 +0000 | [diff] [blame] | 412 | // Okay, this is not one of our quick and easy cases. Because we don't have a |
| 413 | // full FFI, we have to codegen a nullary stub function that just calls the |
| 414 | // function we are interested in, passing in constants for all of the |
| 415 | // arguments. Make this function and return. |
| 416 | |
| 417 | // First, create the function. |
| 418 | FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 419 | Function *Stub = Function::Create(STy, Function::InternalLinkage, "", |
| 420 | F->getParent()); |
Chris Lattner | cc22e9f | 2004-08-16 00:14:18 +0000 | [diff] [blame] | 421 | |
| 422 | // Insert a basic block. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 423 | BasicBlock *StubBB = BasicBlock::Create("", Stub); |
Chris Lattner | cc22e9f | 2004-08-16 00:14:18 +0000 | [diff] [blame] | 424 | |
| 425 | // Convert all of the GenericValue arguments over to constants. Note that we |
| 426 | // currently don't support varargs. |
Chris Lattner | 990b849 | 2007-02-13 06:01:22 +0000 | [diff] [blame] | 427 | SmallVector<Value*, 8> Args; |
Chris Lattner | cc22e9f | 2004-08-16 00:14:18 +0000 | [diff] [blame] | 428 | for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) { |
| 429 | Constant *C = 0; |
| 430 | const Type *ArgTy = FTy->getParamType(i); |
| 431 | const GenericValue &AV = ArgValues[i]; |
| 432 | switch (ArgTy->getTypeID()) { |
| 433 | default: assert(0 && "Unknown argument type for function call!"); |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 434 | case Type::IntegerTyID: |
| 435 | C = ConstantInt::get(AV.IntVal); |
| 436 | break; |
| 437 | case Type::FloatTyID: |
| 438 | C = ConstantFP::get(APFloat(AV.FloatVal)); |
| 439 | break; |
| 440 | case Type::DoubleTyID: |
| 441 | C = ConstantFP::get(APFloat(AV.DoubleVal)); |
| 442 | break; |
Dale Johannesen | 1abac0d | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 443 | case Type::PPC_FP128TyID: |
| 444 | case Type::X86_FP80TyID: |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 445 | case Type::FP128TyID: |
| 446 | C = ConstantFP::get(APFloat(AV.IntVal)); |
| 447 | break; |
Chris Lattner | cc22e9f | 2004-08-16 00:14:18 +0000 | [diff] [blame] | 448 | case Type::PointerTyID: |
| 449 | void *ArgPtr = GVTOP(AV); |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 450 | if (sizeof(void*) == 4) |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 451 | C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr); |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 452 | else |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 453 | C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr); |
Reid Spencer | 15f46d6 | 2006-12-12 01:17:41 +0000 | [diff] [blame] | 454 | C = ConstantExpr::getIntToPtr(C, ArgTy); // Cast the integer to pointer |
Chris Lattner | cc22e9f | 2004-08-16 00:14:18 +0000 | [diff] [blame] | 455 | break; |
| 456 | } |
| 457 | Args.push_back(C); |
| 458 | } |
| 459 | |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 460 | CallInst *TheCall = CallInst::Create(F, Args.begin(), Args.end(), |
| 461 | "", StubBB); |
Chris Lattner | cdfc51f | 2008-11-23 08:00:11 +0000 | [diff] [blame] | 462 | TheCall->setCallingConv(F->getCallingConv()); |
Chris Lattner | a471e04 | 2005-05-06 06:48:54 +0000 | [diff] [blame] | 463 | TheCall->setTailCall(); |
Chris Lattner | cc22e9f | 2004-08-16 00:14:18 +0000 | [diff] [blame] | 464 | if (TheCall->getType() != Type::VoidTy) |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 465 | ReturnInst::Create(TheCall, StubBB); // Return result of the call. |
Chris Lattner | cc22e9f | 2004-08-16 00:14:18 +0000 | [diff] [blame] | 466 | else |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 467 | ReturnInst::Create(StubBB); // Just return void. |
Chris Lattner | cc22e9f | 2004-08-16 00:14:18 +0000 | [diff] [blame] | 468 | |
| 469 | // Finally, return the value returned by our nullary stub function. |
| 470 | return runFunction(Stub, std::vector<GenericValue>()); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 471 | } |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 472 | |
| 473 | /// runJITOnFunction - Run the FunctionPassManager full of |
| 474 | /// just-in-time compilation passes on F, hopefully filling in |
| 475 | /// GlobalAddress[F] with the address of F's machine code. |
| 476 | /// |
| 477 | void JIT::runJITOnFunction(Function *F) { |
| 478 | static bool isAlreadyCodeGenerating = false; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 479 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 480 | MutexGuard locked(lock); |
Chris Lattner | 17f218e | 2007-08-13 20:08:16 +0000 | [diff] [blame] | 481 | assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!"); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 482 | |
| 483 | // JIT the function |
| 484 | isAlreadyCodeGenerating = true; |
Nate Begeman | f049e07 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 485 | jitstate->getPM(locked).run(*F); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 486 | isAlreadyCodeGenerating = false; |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 487 | |
| 488 | // If the function referred to a global variable that had not yet been |
| 489 | // emitted, it allocates memory for the global, but doesn't emit it yet. Emit |
| 490 | // all of these globals now. |
Nate Begeman | f049e07 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 491 | while (!jitstate->getPendingGlobals(locked).empty()) { |
| 492 | const GlobalVariable *GV = jitstate->getPendingGlobals(locked).back(); |
| 493 | jitstate->getPendingGlobals(locked).pop_back(); |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 494 | EmitGlobalVariable(GV); |
| 495 | } |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | /// getPointerToFunction - This method is used to get the address of the |
| 499 | /// specified function, compiling it if neccesary. |
| 500 | /// |
| 501 | void *JIT::getPointerToFunction(Function *F) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 502 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 503 | if (void *Addr = getPointerToGlobalIfAvailable(F)) |
| 504 | return Addr; // Check if function already code gen'd |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 505 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 506 | // Make sure we read in the function if it exists in this Module. |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 507 | if (F->hasNotBeenReadFromBitcode()) { |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 508 | // Determine the module provider this function is provided by. |
| 509 | Module *M = F->getParent(); |
| 510 | ModuleProvider *MP = 0; |
| 511 | for (unsigned i = 0, e = Modules.size(); i != e; ++i) { |
| 512 | if (Modules[i]->getModule() == M) { |
| 513 | MP = Modules[i]; |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | assert(MP && "Function isn't in a module we know about!"); |
| 518 | |
Chris Lattner | 5c72a3a | 2006-07-07 17:18:09 +0000 | [diff] [blame] | 519 | std::string ErrorMsg; |
| 520 | if (MP->materializeFunction(F, &ErrorMsg)) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 521 | cerr << "Error reading function '" << F->getName() |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 522 | << "' from bitcode file: " << ErrorMsg << "\n"; |
Chris Lattner | 0050ef8 | 2004-11-15 23:18:09 +0000 | [diff] [blame] | 523 | abort(); |
| 524 | } |
Mon P Wang | 1c341c8 | 2008-10-10 01:47:42 +0000 | [diff] [blame] | 525 | |
Dan Gohman | 69f9378 | 2009-01-05 05:32:42 +0000 | [diff] [blame] | 526 | // Now retry to get the address. |
| 527 | if (void *Addr = getPointerToGlobalIfAvailable(F)) |
| 528 | return Addr; |
Nicolas Geoffray | fd7d991 | 2008-04-20 08:33:02 +0000 | [diff] [blame] | 529 | } |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 530 | |
Mon P Wang | 77fcca8 | 2008-10-10 18:07:10 +0000 | [diff] [blame] | 531 | MutexGuard locked(lock); |
| 532 | |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 533 | if (F->isDeclaration()) { |
Dan Gohman | 69f9378 | 2009-01-05 05:32:42 +0000 | [diff] [blame] | 534 | bool AbortOnFailure = F->getLinkage() != GlobalValue::ExternalWeakLinkage; |
| 535 | void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure); |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 536 | addGlobalMapping(F, Addr); |
| 537 | return Addr; |
| 538 | } |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 539 | |
| 540 | runJITOnFunction(F); |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 541 | |
| 542 | void *Addr = getPointerToGlobalIfAvailable(F); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 543 | assert(Addr && "Code generation didn't add function to GlobalAddress table!"); |
| 544 | return Addr; |
| 545 | } |
| 546 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 547 | /// getOrEmitGlobalVariable - Return the address of the specified global |
| 548 | /// variable, possibly emitting it to memory if needed. This is used by the |
| 549 | /// Emitter. |
| 550 | void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 551 | MutexGuard locked(lock); |
| 552 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 553 | void *Ptr = getPointerToGlobalIfAvailable(GV); |
| 554 | if (Ptr) return Ptr; |
| 555 | |
| 556 | // If the global is external, just remember the address. |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 557 | if (GV->isDeclaration()) { |
Anton Korobeynikov | 3b30a6e | 2007-07-30 20:02:02 +0000 | [diff] [blame] | 558 | #if HAVE___DSO_HANDLE |
Evan Cheng | 5f42c55 | 2006-07-21 23:06:20 +0000 | [diff] [blame] | 559 | if (GV->getName() == "__dso_handle") |
| 560 | return (void*)&__dso_handle; |
Evan Cheng | b82ab94 | 2006-07-22 00:42:03 +0000 | [diff] [blame] | 561 | #endif |
Reid Spencer | df5a37e | 2004-11-29 14:11:29 +0000 | [diff] [blame] | 562 | Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str()); |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 563 | if (Ptr == 0) { |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 564 | cerr << "Could not resolve external global address: " |
| 565 | << GV->getName() << "\n"; |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 566 | abort(); |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 567 | addGlobalMapping(GV, Ptr); |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 568 | } |
| 569 | } else { |
Evan Cheng | 4d544a2 | 2009-01-16 19:14:49 +0000 | [diff] [blame] | 570 | // GlobalVariable's which are not "constant" will cause trouble in a server |
| 571 | // situation. It's returned in the same block of memory as code which may |
| 572 | // not be writable. |
| 573 | if (isGVCompilationDisabled() && !GV->isConstant()) { |
Evan Cheng | 77f86ad | 2008-12-09 07:31:49 +0000 | [diff] [blame] | 574 | cerr << "Compilation of non-internal GlobalValue is disabled!\n"; |
Evan Cheng | 446531e | 2008-09-24 16:25:55 +0000 | [diff] [blame] | 575 | abort(); |
| 576 | } |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 577 | // If the global hasn't been emitted to memory yet, allocate space and |
| 578 | // emit it into memory. It goes in the same array as the generated |
| 579 | // code, jump tables, etc. |
Chris Lattner | f5d438c | 2006-05-02 21:57:51 +0000 | [diff] [blame] | 580 | const Type *GlobalType = GV->getType()->getElementType(); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 581 | size_t S = getTargetData()->getTypePaddedSize(GlobalType); |
Duncan Sands | d102593 | 2008-01-29 06:23:44 +0000 | [diff] [blame] | 582 | size_t A = getTargetData()->getPreferredAlignment(GV); |
Nicolas Geoffray | 46fa139 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 583 | if (GV->isThreadLocal()) { |
| 584 | MutexGuard locked(lock); |
| 585 | Ptr = TJI.allocateThreadLocalMemory(S); |
Evan Cheng | b0b5349 | 2008-11-04 09:30:48 +0000 | [diff] [blame] | 586 | } else if (TJI.allocateSeparateGVMemory()) { |
| 587 | if (A <= 8) { |
| 588 | Ptr = malloc(S); |
| 589 | } else { |
| 590 | // Allocate S+A bytes of memory, then use an aligned pointer within that |
| 591 | // space. |
| 592 | Ptr = malloc(S+A); |
| 593 | unsigned MisAligned = ((intptr_t)Ptr & (A-1)); |
| 594 | Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0); |
| 595 | } |
Nicolas Geoffray | 46fa139 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 596 | } else { |
| 597 | Ptr = MCE->allocateSpace(S, A); |
| 598 | } |
Dale Johannesen | dd947ea | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 599 | addGlobalMapping(GV, Ptr); |
| 600 | EmitGlobalVariable(GV); |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 601 | } |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 602 | return Ptr; |
| 603 | } |
| 604 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 605 | /// recompileAndRelinkFunction - This method is used to force a function |
| 606 | /// which has already been compiled, to be compiled again, possibly |
| 607 | /// after it has been modified. Then the entry to the old copy is overwritten |
| 608 | /// with a branch to the new copy. If there was no old copy, this acts |
| 609 | /// just like JIT::getPointerToFunction(). |
| 610 | /// |
| 611 | void *JIT::recompileAndRelinkFunction(Function *F) { |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 612 | void *OldAddr = getPointerToGlobalIfAvailable(F); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 613 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 614 | // If it's not already compiled there is no reason to patch it up. |
| 615 | if (OldAddr == 0) { return getPointerToFunction(F); } |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 616 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 617 | // Delete the old function mapping. |
| 618 | addGlobalMapping(F, 0); |
| 619 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 620 | // Recodegen the function |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 621 | runJITOnFunction(F); |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 622 | |
| 623 | // Update state, forward the old function to the new function. |
| 624 | void *Addr = getPointerToGlobalIfAvailable(F); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 625 | assert(Addr && "Code generation didn't add function to GlobalAddress table!"); |
| 626 | TJI.replaceMachineCodeForFunction(OldAddr, Addr); |
| 627 | return Addr; |
| 628 | } |
Misha Brukman | 895eddf | 2004-11-07 23:58:46 +0000 | [diff] [blame] | 629 | |
Nicolas Geoffray | 46fa139 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 630 | /// getMemoryForGV - This method abstracts memory allocation of global |
| 631 | /// variable so that the JIT can allocate thread local variables depending |
| 632 | /// on the target. |
| 633 | /// |
| 634 | char* JIT::getMemoryForGV(const GlobalVariable* GV) { |
| 635 | const Type *ElTy = GV->getType()->getElementType(); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 636 | size_t GVSize = (size_t)getTargetData()->getTypePaddedSize(ElTy); |
Nicolas Geoffray | 46fa139 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 637 | if (GV->isThreadLocal()) { |
| 638 | MutexGuard locked(lock); |
| 639 | return TJI.allocateThreadLocalMemory(GVSize); |
| 640 | } else { |
| 641 | return new char[GVSize]; |
| 642 | } |
| 643 | } |