Eric Christopher | bb498ca | 2011-04-22 03:07:06 +0000 | [diff] [blame] | 1 | //===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===// |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "MCJIT.h" |
| 11 | #include "llvm/ExecutionEngine/GenericValue.h" |
Andrew Kaylor | 776054d | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 12 | #include "llvm/ExecutionEngine/JITEventListener.h" |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 13 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 14 | #include "llvm/ExecutionEngine/MCJIT.h" |
| 15 | #include "llvm/ExecutionEngine/ObjectBuffer.h" |
| 16 | #include "llvm/ExecutionEngine/ObjectImage.h" |
Andrew Kaylor | d2755af | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 17 | #include "llvm/ExecutionEngine/SectionMemoryManager.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DataLayout.h" |
| 19 | #include "llvm/IR/DerivedTypes.h" |
| 20 | #include "llvm/IR/Function.h" |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCAsmInfo.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 22 | #include "llvm/Support/DynamicLibrary.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MemoryBuffer.h" |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MutexGuard.h" |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | static struct RegisterJIT { |
| 32 | RegisterJIT() { MCJIT::Register(); } |
| 33 | } JITRegistrator; |
| 34 | |
| 35 | } |
| 36 | |
| 37 | extern "C" void LLVMLinkInMCJIT() { |
| 38 | } |
| 39 | |
| 40 | ExecutionEngine *MCJIT::createJIT(Module *M, |
| 41 | std::string *ErrorStr, |
| 42 | JITMemoryManager *JMM, |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 43 | bool GVsWithCode, |
Dylan Noblesmith | c5b2858 | 2011-05-13 21:51:29 +0000 | [diff] [blame] | 44 | TargetMachine *TM) { |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 45 | // Try to register the program as a source of symbols to resolve against. |
| 46 | // |
| 47 | // FIXME: Don't do this here. |
| 48 | sys::DynamicLibrary::LoadLibraryPermanently(0, NULL); |
| 49 | |
Andrew Kaylor | d2755af | 2013-04-29 17:49:40 +0000 | [diff] [blame] | 50 | return new MCJIT(M, TM, JMM ? JMM : new SectionMemoryManager(), GVsWithCode); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Jim Grosbach | 8005bcd | 2012-08-21 15:42:49 +0000 | [diff] [blame] | 53 | MCJIT::MCJIT(Module *m, TargetMachine *tm, RTDyldMemoryManager *MM, |
| 54 | bool AllocateGVsWithCode) |
Filip Pizlo | 0e1327e | 2013-05-01 22:58:00 +0000 | [diff] [blame] | 55 | : ExecutionEngine(m), TM(tm), Ctx(0), |
| 56 | MemMgr(MM ? MM : new SectionMemoryManager()), Dyld(MemMgr), |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 57 | IsLoaded(false), M(m), ObjCache(0) { |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 58 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 59 | setDataLayout(TM->getDataLayout()); |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | MCJIT::~MCJIT() { |
Andrew Kaylor | 776054d | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 63 | if (LoadedObject) |
Andrew Kaylor | a082892 | 2012-11-06 19:06:46 +0000 | [diff] [blame] | 64 | NotifyFreeingObject(*LoadedObject.get()); |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 65 | delete MemMgr; |
| 66 | delete TM; |
| 67 | } |
| 68 | |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 69 | void MCJIT::setObjectCache(ObjectCache* NewCache) { |
| 70 | ObjCache = NewCache; |
| 71 | } |
| 72 | |
| 73 | ObjectBufferStream* MCJIT::emitObject(Module *m) { |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 74 | /// Currently, MCJIT only supports a single module and the module passed to |
| 75 | /// this function call is expected to be the contained module. The module |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 76 | /// is passed as a parameter here to prepare for multiple module support in |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 77 | /// the future. |
| 78 | assert(M == m); |
| 79 | |
| 80 | // Get a thread lock to make sure we aren't trying to compile multiple times |
| 81 | MutexGuard locked(lock); |
| 82 | |
| 83 | // FIXME: Track compilation state on a per-module basis when multiple modules |
| 84 | // are supported. |
| 85 | // Re-compilation is not supported |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 86 | assert(!IsLoaded); |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 87 | |
| 88 | PassManager PM; |
| 89 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 90 | PM.add(new DataLayout(*TM->getDataLayout())); |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 91 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 92 | // The RuntimeDyld will take ownership of this shortly |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 93 | OwningPtr<ObjectBufferStream> CompiledObject(new ObjectBufferStream()); |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 94 | |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 95 | // Turn the machine code intermediate representation into bytes in memory |
| 96 | // that may be executed. |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 97 | if (TM->addPassesToEmitMC(PM, Ctx, CompiledObject->getOStream(), false)) { |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 98 | report_fatal_error("Target does not support MC emission!"); |
| 99 | } |
| 100 | |
| 101 | // Initialize passes. |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 102 | PM.run(*m); |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 103 | // Flush the output buffer to get the generated code into memory |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 104 | CompiledObject->flush(); |
| 105 | |
| 106 | // If we have an object cache, tell it about the new object. |
| 107 | // Note that we're using the compiled image, not the loaded image (as below). |
| 108 | if (ObjCache) { |
| 109 | // MemoryBuffer is a thin wrapper around the actual memory, so it's OK |
| 110 | // to create a temporary object here and delete it after the call. |
| 111 | OwningPtr<MemoryBuffer> MB(CompiledObject->getMemBuffer()); |
| 112 | ObjCache->notifyObjectCompiled(m, MB.get()); |
| 113 | } |
| 114 | |
| 115 | return CompiledObject.take(); |
| 116 | } |
| 117 | |
| 118 | void MCJIT::loadObject(Module *M) { |
| 119 | |
| 120 | // Get a thread lock to make sure we aren't trying to load multiple times |
| 121 | MutexGuard locked(lock); |
| 122 | |
| 123 | // FIXME: Track compilation state on a per-module basis when multiple modules |
| 124 | // are supported. |
| 125 | // Re-compilation is not supported |
| 126 | if (IsLoaded) |
| 127 | return; |
| 128 | |
| 129 | OwningPtr<ObjectBuffer> ObjectToLoad; |
| 130 | // Try to load the pre-compiled object from cache if possible |
| 131 | if (0 != ObjCache) { |
| 132 | OwningPtr<MemoryBuffer> PreCompiledObject(ObjCache->getObjectCopy(M)); |
| 133 | if (0 != PreCompiledObject.get()) |
| 134 | ObjectToLoad.reset(new ObjectBuffer(PreCompiledObject.take())); |
| 135 | } |
| 136 | |
| 137 | // If the cache did not contain a suitable object, compile the object |
| 138 | if (!ObjectToLoad) { |
| 139 | ObjectToLoad.reset(emitObject(M)); |
| 140 | assert(ObjectToLoad.get() && "Compilation did not produce an object."); |
| 141 | } |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 142 | |
| 143 | // Load the object into the dynamic linker. |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 144 | // handing off ownership of the buffer |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 145 | LoadedObject.reset(Dyld.loadObject(ObjectToLoad.take())); |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 146 | if (!LoadedObject) |
Jim Grosbach | 8086f3b | 2011-03-23 19:51:34 +0000 | [diff] [blame] | 147 | report_fatal_error(Dyld.getErrorString()); |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 148 | |
Jim Grosbach | 69e8132 | 2011-04-13 15:28:10 +0000 | [diff] [blame] | 149 | // Resolve any relocations. |
| 150 | Dyld.resolveRelocations(); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 151 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 152 | // FIXME: Make this optional, maybe even move it to a JIT event listener |
| 153 | LoadedObject->registerWithDebugger(); |
| 154 | |
Andrew Kaylor | 776054d | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 155 | NotifyObjectEmitted(*LoadedObject); |
| 156 | |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 157 | // FIXME: Add support for per-module compilation state |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 158 | IsLoaded = true; |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Andrew Kaylor | 2898988 | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 161 | // FIXME: Add a parameter to identify which object is being finalized when |
| 162 | // MCJIT supports multiple modules. |
Andrew Kaylor | 53608a3 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 163 | // FIXME: Provide a way to separate code emission, relocations and page |
| 164 | // protection in the interface. |
Andrew Kaylor | 2898988 | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 165 | void MCJIT::finalizeObject() { |
| 166 | // If the module hasn't been compiled, just do that. |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 167 | if (!IsLoaded) { |
| 168 | // If the call to Dyld.resolveRelocations() is removed from loadObject() |
Andrew Kaylor | 2898988 | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 169 | // we'll need to do that here. |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 170 | loadObject(M); |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame^] | 171 | } else { |
| 172 | // Resolve any relocations. |
| 173 | Dyld.resolveRelocations(); |
Andrew Kaylor | 2898988 | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame^] | 176 | StringRef EHData = Dyld.getEHFrameSection(); |
| 177 | if (!EHData.empty()) |
| 178 | MemMgr->registerEHFrames(EHData); |
Andrew Kaylor | 53608a3 | 2012-11-15 23:50:01 +0000 | [diff] [blame] | 179 | |
| 180 | // Set page permissions. |
| 181 | MemMgr->applyPermissions(); |
Andrew Kaylor | 2898988 | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 184 | void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) { |
| 185 | report_fatal_error("not yet implemented"); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void *MCJIT::getPointerToFunction(Function *F) { |
Jim Grosbach | 35ed842 | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 189 | // FIXME: This should really return a uint64_t since it's a pointer in the |
| 190 | // target address space, not our local address space. That's part of the |
| 191 | // ExecutionEngine interface, though. Fix that when the old JIT finally |
| 192 | // dies. |
| 193 | |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 194 | // FIXME: Add support for per-module compilation state |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 195 | if (!IsLoaded) |
| 196 | loadObject(M); |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 197 | |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 198 | if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) { |
| 199 | bool AbortOnFailure = !F->hasExternalWeakLinkage(); |
| 200 | void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure); |
| 201 | addGlobalMapping(F, Addr); |
| 202 | return Addr; |
| 203 | } |
| 204 | |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 205 | // FIXME: Should the Dyld be retaining module information? Probably not. |
Jim Grosbach | 3ec2c7c | 2011-05-18 23:53:21 +0000 | [diff] [blame] | 206 | // FIXME: Should we be using the mangler for this? Probably. |
Jim Grosbach | 35ed842 | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 207 | // |
| 208 | // This is the accessor for the target address, so make sure to check the |
| 209 | // load address of the symbol, not the local address. |
Jim Grosbach | 3ec2c7c | 2011-05-18 23:53:21 +0000 | [diff] [blame] | 210 | StringRef BaseName = F->getName(); |
| 211 | if (BaseName[0] == '\1') |
Jim Grosbach | 35ed842 | 2012-09-05 16:50:40 +0000 | [diff] [blame] | 212 | return (void*)Dyld.getSymbolLoadAddress(BaseName.substr(1)); |
| 213 | return (void*)Dyld.getSymbolLoadAddress((TM->getMCAsmInfo()->getGlobalPrefix() |
Jim Grosbach | c0ceedb | 2011-05-19 00:45:05 +0000 | [diff] [blame] | 214 | + BaseName).str()); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void *MCJIT::recompileAndRelinkFunction(Function *F) { |
| 218 | report_fatal_error("not yet implemented"); |
| 219 | } |
| 220 | |
| 221 | void MCJIT::freeMachineCodeForFunction(Function *F) { |
| 222 | report_fatal_error("not yet implemented"); |
| 223 | } |
| 224 | |
| 225 | GenericValue MCJIT::runFunction(Function *F, |
| 226 | const std::vector<GenericValue> &ArgValues) { |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 227 | assert(F && "Function *F was null at entry to run()"); |
| 228 | |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 229 | void *FPtr = getPointerToFunction(F); |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 230 | assert(FPtr && "Pointer to fn's code was null after getPointerToFunction"); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 231 | FunctionType *FTy = F->getFunctionType(); |
| 232 | Type *RetTy = FTy->getReturnType(); |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 233 | |
| 234 | assert((FTy->getNumParams() == ArgValues.size() || |
| 235 | (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) && |
| 236 | "Wrong number of arguments passed into function!"); |
| 237 | assert(FTy->getNumParams() == ArgValues.size() && |
| 238 | "This doesn't support passing arguments through varargs (yet)!"); |
| 239 | |
| 240 | // Handle some common cases first. These cases correspond to common `main' |
| 241 | // prototypes. |
| 242 | if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) { |
| 243 | switch (ArgValues.size()) { |
| 244 | case 3: |
| 245 | if (FTy->getParamType(0)->isIntegerTy(32) && |
| 246 | FTy->getParamType(1)->isPointerTy() && |
| 247 | FTy->getParamType(2)->isPointerTy()) { |
| 248 | int (*PF)(int, char **, const char **) = |
| 249 | (int(*)(int, char **, const char **))(intptr_t)FPtr; |
| 250 | |
| 251 | // Call the function. |
| 252 | GenericValue rv; |
| 253 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), |
| 254 | (char **)GVTOP(ArgValues[1]), |
| 255 | (const char **)GVTOP(ArgValues[2]))); |
| 256 | return rv; |
| 257 | } |
| 258 | break; |
| 259 | case 2: |
| 260 | if (FTy->getParamType(0)->isIntegerTy(32) && |
| 261 | FTy->getParamType(1)->isPointerTy()) { |
| 262 | int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr; |
| 263 | |
| 264 | // Call the function. |
| 265 | GenericValue rv; |
| 266 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(), |
| 267 | (char **)GVTOP(ArgValues[1]))); |
| 268 | return rv; |
| 269 | } |
| 270 | break; |
| 271 | case 1: |
| 272 | if (FTy->getNumParams() == 1 && |
| 273 | FTy->getParamType(0)->isIntegerTy(32)) { |
| 274 | GenericValue rv; |
| 275 | int (*PF)(int) = (int(*)(int))(intptr_t)FPtr; |
| 276 | rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue())); |
| 277 | return rv; |
| 278 | } |
| 279 | break; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // Handle cases where no arguments are passed first. |
| 284 | if (ArgValues.empty()) { |
| 285 | GenericValue rv; |
| 286 | switch (RetTy->getTypeID()) { |
| 287 | default: llvm_unreachable("Unknown return type for function call!"); |
| 288 | case Type::IntegerTyID: { |
| 289 | unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth(); |
| 290 | if (BitWidth == 1) |
| 291 | rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)()); |
| 292 | else if (BitWidth <= 8) |
| 293 | rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)()); |
| 294 | else if (BitWidth <= 16) |
| 295 | rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)()); |
| 296 | else if (BitWidth <= 32) |
| 297 | rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)()); |
| 298 | else if (BitWidth <= 64) |
| 299 | rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)()); |
| 300 | else |
| 301 | llvm_unreachable("Integer types > 64 bits not supported"); |
| 302 | return rv; |
| 303 | } |
| 304 | case Type::VoidTyID: |
| 305 | rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)()); |
| 306 | return rv; |
| 307 | case Type::FloatTyID: |
| 308 | rv.FloatVal = ((float(*)())(intptr_t)FPtr)(); |
| 309 | return rv; |
| 310 | case Type::DoubleTyID: |
| 311 | rv.DoubleVal = ((double(*)())(intptr_t)FPtr)(); |
| 312 | return rv; |
| 313 | case Type::X86_FP80TyID: |
| 314 | case Type::FP128TyID: |
| 315 | case Type::PPC_FP128TyID: |
| 316 | llvm_unreachable("long double not supported yet"); |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 317 | case Type::PointerTyID: |
| 318 | return PTOGV(((void*(*)())(intptr_t)FPtr)()); |
| 319 | } |
| 320 | } |
| 321 | |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 322 | llvm_unreachable("Full-featured argument passing not supported yet!"); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 323 | } |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 324 | |
| 325 | void *MCJIT::getPointerToNamedFunction(const std::string &Name, |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 326 | bool AbortOnFailure) { |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 327 | // FIXME: Add support for per-module compilation state |
Andrew Kaylor | 1c48945 | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 328 | if (!IsLoaded) |
| 329 | loadObject(M); |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 330 | |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 331 | if (!isSymbolSearchingDisabled() && MemMgr) { |
| 332 | void *ptr = MemMgr->getPointerToNamedFunction(Name, false); |
| 333 | if (ptr) |
| 334 | return ptr; |
| 335 | } |
| 336 | |
| 337 | /// If a LazyFunctionCreator is installed, use it to get/create the function. |
| 338 | if (LazyFunctionCreator) |
| 339 | if (void *RP = LazyFunctionCreator(Name)) |
| 340 | return RP; |
| 341 | |
| 342 | if (AbortOnFailure) { |
| 343 | report_fatal_error("Program used external function '"+Name+ |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 344 | "' which could not be resolved!"); |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 345 | } |
| 346 | return 0; |
| 347 | } |
Andrew Kaylor | 776054d | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 348 | |
| 349 | void MCJIT::RegisterJITEventListener(JITEventListener *L) { |
| 350 | if (L == NULL) |
| 351 | return; |
| 352 | MutexGuard locked(lock); |
| 353 | EventListeners.push_back(L); |
| 354 | } |
| 355 | void MCJIT::UnregisterJITEventListener(JITEventListener *L) { |
| 356 | if (L == NULL) |
| 357 | return; |
| 358 | MutexGuard locked(lock); |
| 359 | SmallVector<JITEventListener*, 2>::reverse_iterator I= |
| 360 | std::find(EventListeners.rbegin(), EventListeners.rend(), L); |
| 361 | if (I != EventListeners.rend()) { |
| 362 | std::swap(*I, EventListeners.back()); |
| 363 | EventListeners.pop_back(); |
| 364 | } |
| 365 | } |
| 366 | void MCJIT::NotifyObjectEmitted(const ObjectImage& Obj) { |
| 367 | MutexGuard locked(lock); |
| 368 | for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) { |
| 369 | EventListeners[I]->NotifyObjectEmitted(Obj); |
| 370 | } |
| 371 | } |
| 372 | void MCJIT::NotifyFreeingObject(const ObjectImage& Obj) { |
| 373 | MutexGuard locked(lock); |
| 374 | for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) { |
| 375 | EventListeners[I]->NotifyFreeingObject(Obj); |
| 376 | } |
| 377 | } |