blob: fa2c9842d9ef4b17a19d08c4fa81ca3be97a5fa3 [file] [log] [blame]
Eric Christopherbb498ca2011-04-22 03:07:06 +00001//===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===//
Daniel Dunbar6aec2982010-11-17 16:06:43 +00002//
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 Kaylor776054d2012-11-06 18:51:59 +000012#include "llvm/ExecutionEngine/JITEventListener.h"
Jim Grosbachf9229102011-03-22 01:06:42 +000013#include "llvm/ExecutionEngine/JITMemoryManager.h"
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000014#include "llvm/ExecutionEngine/MCJIT.h"
15#include "llvm/ExecutionEngine/ObjectBuffer.h"
16#include "llvm/ExecutionEngine/ObjectImage.h"
Andrew Kaylord2755af2013-04-29 17:49:40 +000017#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/DataLayout.h"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/Function.h"
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000021#include "llvm/IR/Module.h"
Jim Grosbachf9229102011-03-22 01:06:42 +000022#include "llvm/MC/MCAsmInfo.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000023#include "llvm/Support/DynamicLibrary.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/Support/ErrorHandling.h"
Jim Grosbachf9229102011-03-22 01:06:42 +000025#include "llvm/Support/MemoryBuffer.h"
Andrew Kaylorea708d12012-08-07 18:33:00 +000026#include "llvm/Support/MutexGuard.h"
Daniel Dunbar6aec2982010-11-17 16:06:43 +000027
28using namespace llvm;
29
30namespace {
31
32static struct RegisterJIT {
33 RegisterJIT() { MCJIT::Register(); }
34} JITRegistrator;
35
36}
37
38extern "C" void LLVMLinkInMCJIT() {
39}
40
41ExecutionEngine *MCJIT::createJIT(Module *M,
42 std::string *ErrorStr,
Filip Pizlo13a3cf12013-05-14 19:29:00 +000043 RTDyldMemoryManager *MemMgr,
Daniel Dunbar6aec2982010-11-17 16:06:43 +000044 bool GVsWithCode,
Dylan Noblesmithc5b28582011-05-13 21:51:29 +000045 TargetMachine *TM) {
Daniel Dunbar6aec2982010-11-17 16:06:43 +000046 // Try to register the program as a source of symbols to resolve against.
47 //
48 // FIXME: Don't do this here.
49 sys::DynamicLibrary::LoadLibraryPermanently(0, NULL);
50
Filip Pizlo13a3cf12013-05-14 19:29:00 +000051 return new MCJIT(M, TM, MemMgr ? MemMgr : new SectionMemoryManager(),
52 GVsWithCode);
Daniel Dunbar6aec2982010-11-17 16:06:43 +000053}
54
Jim Grosbach8005bcd2012-08-21 15:42:49 +000055MCJIT::MCJIT(Module *m, TargetMachine *tm, RTDyldMemoryManager *MM,
56 bool AllocateGVsWithCode)
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000057 : ExecutionEngine(m), TM(tm), Ctx(0), MemMgr(this, MM), Dyld(&MemMgr),
58 ObjCache(0) {
Jim Grosbach31649e62011-03-18 22:48:41 +000059
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000060 ModuleStates[m] = ModuleAdded;
Micah Villmow3574eca2012-10-08 16:38:25 +000061 setDataLayout(TM->getDataLayout());
Andrew Kaylorea708d12012-08-07 18:33:00 +000062}
63
64MCJIT::~MCJIT() {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000065 LoadedObjectMap::iterator it, end = LoadedObjects.end();
66 for (it = LoadedObjects.begin(); it != end; ++it) {
67 ObjectImage *Obj = it->second;
68 if (Obj) {
69 NotifyFreeingObject(*Obj);
70 delete Obj;
71 }
72 }
73 LoadedObjects.clear();
Andrew Kaylorea708d12012-08-07 18:33:00 +000074 delete TM;
75}
76
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000077void MCJIT::addModule(Module *M) {
78 Modules.push_back(M);
79 ModuleStates[M] = MCJITModuleState();
80}
81
Andrew Kaylor1c489452013-04-25 21:02:36 +000082void MCJIT::setObjectCache(ObjectCache* NewCache) {
83 ObjCache = NewCache;
84}
85
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000086ObjectBufferStream* MCJIT::emitObject(Module *M) {
87 // This must be a module which has already been added to this MCJIT instance.
88 assert(std::find(Modules.begin(), Modules.end(), M) != Modules.end());
89 assert(ModuleStates.find(M) != ModuleStates.end());
Andrew Kaylorea708d12012-08-07 18:33:00 +000090
91 // Get a thread lock to make sure we aren't trying to compile multiple times
92 MutexGuard locked(lock);
93
Andrew Kaylorea708d12012-08-07 18:33:00 +000094 // Re-compilation is not supported
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000095 assert(!ModuleStates[M].hasBeenEmitted());
Andrew Kaylorea708d12012-08-07 18:33:00 +000096
97 PassManager PM;
98
Micah Villmow3574eca2012-10-08 16:38:25 +000099 PM.add(new DataLayout(*TM->getDataLayout()));
Jim Grosbach31649e62011-03-18 22:48:41 +0000100
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000101 // The RuntimeDyld will take ownership of this shortly
Andrew Kaylor1c489452013-04-25 21:02:36 +0000102 OwningPtr<ObjectBufferStream> CompiledObject(new ObjectBufferStream());
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000103
Jim Grosbach31649e62011-03-18 22:48:41 +0000104 // Turn the machine code intermediate representation into bytes in memory
105 // that may be executed.
Andrew Kaylor1c489452013-04-25 21:02:36 +0000106 if (TM->addPassesToEmitMC(PM, Ctx, CompiledObject->getOStream(), false)) {
Jim Grosbach31649e62011-03-18 22:48:41 +0000107 report_fatal_error("Target does not support MC emission!");
108 }
109
110 // Initialize passes.
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000111 PM.run(*M);
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000112 // Flush the output buffer to get the generated code into memory
Andrew Kaylor1c489452013-04-25 21:02:36 +0000113 CompiledObject->flush();
114
115 // If we have an object cache, tell it about the new object.
116 // Note that we're using the compiled image, not the loaded image (as below).
117 if (ObjCache) {
118 // MemoryBuffer is a thin wrapper around the actual memory, so it's OK
119 // to create a temporary object here and delete it after the call.
120 OwningPtr<MemoryBuffer> MB(CompiledObject->getMemBuffer());
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000121 ObjCache->notifyObjectCompiled(M, MB.get());
Andrew Kaylor1c489452013-04-25 21:02:36 +0000122 }
123
124 return CompiledObject.take();
125}
126
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000127void MCJIT::generateCodeForModule(Module *M) {
128 // This must be a module which has already been added to this MCJIT instance.
129 assert(std::find(Modules.begin(), Modules.end(), M) != Modules.end());
130 assert(ModuleStates.find(M) != ModuleStates.end());
Andrew Kaylor1c489452013-04-25 21:02:36 +0000131
132 // Get a thread lock to make sure we aren't trying to load multiple times
133 MutexGuard locked(lock);
134
Andrew Kaylor1c489452013-04-25 21:02:36 +0000135 // Re-compilation is not supported
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000136 if (ModuleStates[M].hasBeenLoaded())
Andrew Kaylor1c489452013-04-25 21:02:36 +0000137 return;
138
139 OwningPtr<ObjectBuffer> ObjectToLoad;
140 // Try to load the pre-compiled object from cache if possible
141 if (0 != ObjCache) {
Andrew Kaylor40d81712013-06-28 21:40:16 +0000142 OwningPtr<MemoryBuffer> PreCompiledObject(ObjCache->getObject(M));
Andrew Kaylor1c489452013-04-25 21:02:36 +0000143 if (0 != PreCompiledObject.get())
144 ObjectToLoad.reset(new ObjectBuffer(PreCompiledObject.take()));
145 }
146
147 // If the cache did not contain a suitable object, compile the object
148 if (!ObjectToLoad) {
149 ObjectToLoad.reset(emitObject(M));
150 assert(ObjectToLoad.get() && "Compilation did not produce an object.");
151 }
Jim Grosbachf9229102011-03-22 01:06:42 +0000152
153 // Load the object into the dynamic linker.
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000154 // MCJIT now owns the ObjectImage pointer (via its LoadedObjects map).
155 ObjectImage *LoadedObject = Dyld.loadObject(ObjectToLoad.take());
156 LoadedObjects[M] = LoadedObject;
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000157 if (!LoadedObject)
Jim Grosbach8086f3b2011-03-23 19:51:34 +0000158 report_fatal_error(Dyld.getErrorString());
Andrew Kaylorea708d12012-08-07 18:33:00 +0000159
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000160 // FIXME: Make this optional, maybe even move it to a JIT event listener
161 LoadedObject->registerWithDebugger();
162
Andrew Kaylor776054d2012-11-06 18:51:59 +0000163 NotifyObjectEmitted(*LoadedObject);
164
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000165 ModuleStates[M] = ModuleLoaded;
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000166}
167
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000168void MCJIT::finalizeLoadedModules() {
169 // Resolve any outstanding relocations.
170 Dyld.resolveRelocations();
171
172 // Register EH frame data for any module we own which has been loaded
173 SmallVector<Module *, 1>::iterator end = Modules.end();
174 SmallVector<Module *, 1>::iterator it;
175 for (it = Modules.begin(); it != end; ++it) {
176 Module *M = *it;
177 assert(ModuleStates.find(M) != ModuleStates.end());
178
179 if (ModuleStates[M].hasBeenLoaded() &&
180 !ModuleStates[M].hasBeenFinalized()) {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000181 ModuleStates[M] = ModuleFinalized;
182 }
Andrew Kaylor28989882012-11-05 20:57:16 +0000183 }
184
Andrew Kaylor528f6d72013-10-11 21:25:48 +0000185 Dyld.registerEHFrames();
186
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000187 // Set page permissions.
188 MemMgr.finalizeMemory();
189}
190
191// FIXME: Rename this.
192void MCJIT::finalizeObject() {
193 // FIXME: This is a temporary hack to get around problems with calling
194 // finalize multiple times.
195 bool finalizeNeeded = false;
196 SmallVector<Module *, 1>::iterator end = Modules.end();
197 SmallVector<Module *, 1>::iterator it;
198 for (it = Modules.begin(); it != end; ++it) {
199 Module *M = *it;
200 assert(ModuleStates.find(M) != ModuleStates.end());
201 if (!ModuleStates[M].hasBeenFinalized())
202 finalizeNeeded = true;
203
204 // I don't really like this, but the C API depends on this behavior.
205 // I suppose it's OK for a deprecated function.
206 if (!ModuleStates[M].hasBeenLoaded())
207 generateCodeForModule(M);
208 }
209 if (!finalizeNeeded)
210 return;
211
212 // Resolve any outstanding relocations.
213 Dyld.resolveRelocations();
214
215 // Register EH frame data for any module we own which has been loaded
216 for (it = Modules.begin(); it != end; ++it) {
217 Module *M = *it;
218 assert(ModuleStates.find(M) != ModuleStates.end());
219
220 if (ModuleStates[M].hasBeenLoaded() &&
221 !ModuleStates[M].hasBeenFinalized()) {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000222 ModuleStates[M] = ModuleFinalized;
223 }
224 }
Andrew Kaylor53608a32012-11-15 23:50:01 +0000225
Andrew Kaylor528f6d72013-10-11 21:25:48 +0000226 Dyld.registerEHFrames();
227
Andrew Kaylor53608a32012-11-15 23:50:01 +0000228 // Set page permissions.
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000229 MemMgr.finalizeMemory();
230}
231
232void MCJIT::finalizeModule(Module *M) {
233 // This must be a module which has already been added to this MCJIT instance.
234 assert(std::find(Modules.begin(), Modules.end(), M) != Modules.end());
235 assert(ModuleStates.find(M) != ModuleStates.end());
236
237 if (ModuleStates[M].hasBeenFinalized())
238 return;
239
240 // If the module hasn't been compiled, just do that.
241 if (!ModuleStates[M].hasBeenLoaded())
242 generateCodeForModule(M);
243
244 // Resolve any outstanding relocations.
245 Dyld.resolveRelocations();
246
Andrew Kaylor528f6d72013-10-11 21:25:48 +0000247 Dyld.registerEHFrames();
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000248
249 // Set page permissions.
250 MemMgr.finalizeMemory();
251
252 ModuleStates[M] = ModuleFinalized;
Andrew Kaylor28989882012-11-05 20:57:16 +0000253}
254
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000255void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) {
256 report_fatal_error("not yet implemented");
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000257}
258
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000259uint64_t MCJIT::getExistingSymbolAddress(const std::string &Name) {
260 // Check with the RuntimeDyld to see if we already have this symbol.
261 if (Name[0] == '\1')
262 return Dyld.getSymbolLoadAddress(Name.substr(1));
263 return Dyld.getSymbolLoadAddress((TM->getMCAsmInfo()->getGlobalPrefix()
264 + Name));
265}
Jim Grosbach35ed8422012-09-05 16:50:40 +0000266
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000267Module *MCJIT::findModuleForSymbol(const std::string &Name,
268 bool CheckFunctionsOnly) {
269 // If it hasn't already been generated, see if it's in one of our modules.
270 SmallVector<Module *, 1>::iterator end = Modules.end();
271 SmallVector<Module *, 1>::iterator it;
272 for (it = Modules.begin(); it != end; ++it) {
273 Module *M = *it;
274 Function *F = M->getFunction(Name);
275 if (F && !F->empty())
276 return M;
277 if (!CheckFunctionsOnly) {
278 GlobalVariable *G = M->getGlobalVariable(Name);
279 if (G)
280 return M;
281 // FIXME: Do we need to worry about global aliases?
282 }
283 }
284 // We didn't find the symbol in any of our modules.
285 return NULL;
286}
287
288uint64_t MCJIT::getSymbolAddress(const std::string &Name,
289 bool CheckFunctionsOnly)
290{
291 // First, check to see if we already have this symbol.
292 uint64_t Addr = getExistingSymbolAddress(Name);
293 if (Addr)
294 return Addr;
295
296 // If it hasn't already been generated, see if it's in one of our modules.
297 Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
298 if (!M)
299 return 0;
300
301 // If this is in one of our modules, generate code for that module.
302 assert(ModuleStates.find(M) != ModuleStates.end());
303 // If the module code has already been generated, we won't find the symbol.
304 if (ModuleStates[M].hasBeenLoaded())
305 return 0;
306
307 // FIXME: We probably need to make sure we aren't in the process of
308 // loading or finalizing this module.
309 generateCodeForModule(M);
310
311 // Check the RuntimeDyld table again, it should be there now.
312 return getExistingSymbolAddress(Name);
313}
314
315uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) {
316 uint64_t Result = getSymbolAddress(Name, false);
317 if (Result != 0)
318 finalizeLoadedModules();
319 return Result;
320}
321
322uint64_t MCJIT::getFunctionAddress(const std::string &Name) {
323 uint64_t Result = getSymbolAddress(Name, true);
324 if (Result != 0)
325 finalizeLoadedModules();
326 return Result;
327}
328
329// Deprecated. Use getFunctionAddress instead.
330void *MCJIT::getPointerToFunction(Function *F) {
Andrew Kaylorea708d12012-08-07 18:33:00 +0000331
Jim Grosbach34714a02011-03-22 18:05:27 +0000332 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
333 bool AbortOnFailure = !F->hasExternalWeakLinkage();
334 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
335 addGlobalMapping(F, Addr);
336 return Addr;
337 }
338
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000339 // If this function doesn't belong to one of our modules, we're done.
340 Module *M = F->getParent();
341 if (std::find(Modules.begin(), Modules.end(), M) == Modules.end())
342 return NULL;
343
344 assert(ModuleStates.find(M) != ModuleStates.end());
345
346 // Make sure the relevant module has been compiled and loaded.
347 if (!ModuleStates[M].hasBeenLoaded())
348 generateCodeForModule(M);
349
Andrew Kaylorea708d12012-08-07 18:33:00 +0000350 // FIXME: Should the Dyld be retaining module information? Probably not.
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +0000351 // FIXME: Should we be using the mangler for this? Probably.
Jim Grosbach35ed8422012-09-05 16:50:40 +0000352 //
353 // This is the accessor for the target address, so make sure to check the
354 // load address of the symbol, not the local address.
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +0000355 StringRef BaseName = F->getName();
356 if (BaseName[0] == '\1')
Jim Grosbach35ed8422012-09-05 16:50:40 +0000357 return (void*)Dyld.getSymbolLoadAddress(BaseName.substr(1));
358 return (void*)Dyld.getSymbolLoadAddress((TM->getMCAsmInfo()->getGlobalPrefix()
Jim Grosbachc0ceedb2011-05-19 00:45:05 +0000359 + BaseName).str());
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000360}
361
362void *MCJIT::recompileAndRelinkFunction(Function *F) {
363 report_fatal_error("not yet implemented");
364}
365
366void MCJIT::freeMachineCodeForFunction(Function *F) {
367 report_fatal_error("not yet implemented");
368}
369
370GenericValue MCJIT::runFunction(Function *F,
371 const std::vector<GenericValue> &ArgValues) {
Jim Grosbach34714a02011-03-22 18:05:27 +0000372 assert(F && "Function *F was null at entry to run()");
373
Jim Grosbach31649e62011-03-18 22:48:41 +0000374 void *FPtr = getPointerToFunction(F);
Jim Grosbach34714a02011-03-22 18:05:27 +0000375 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000376 FunctionType *FTy = F->getFunctionType();
377 Type *RetTy = FTy->getReturnType();
Jim Grosbach34714a02011-03-22 18:05:27 +0000378
379 assert((FTy->getNumParams() == ArgValues.size() ||
380 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
381 "Wrong number of arguments passed into function!");
382 assert(FTy->getNumParams() == ArgValues.size() &&
383 "This doesn't support passing arguments through varargs (yet)!");
384
385 // Handle some common cases first. These cases correspond to common `main'
386 // prototypes.
387 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
388 switch (ArgValues.size()) {
389 case 3:
390 if (FTy->getParamType(0)->isIntegerTy(32) &&
391 FTy->getParamType(1)->isPointerTy() &&
392 FTy->getParamType(2)->isPointerTy()) {
393 int (*PF)(int, char **, const char **) =
394 (int(*)(int, char **, const char **))(intptr_t)FPtr;
395
396 // Call the function.
397 GenericValue rv;
398 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
399 (char **)GVTOP(ArgValues[1]),
400 (const char **)GVTOP(ArgValues[2])));
401 return rv;
402 }
403 break;
404 case 2:
405 if (FTy->getParamType(0)->isIntegerTy(32) &&
406 FTy->getParamType(1)->isPointerTy()) {
407 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
408
409 // Call the function.
410 GenericValue rv;
411 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
412 (char **)GVTOP(ArgValues[1])));
413 return rv;
414 }
415 break;
416 case 1:
417 if (FTy->getNumParams() == 1 &&
418 FTy->getParamType(0)->isIntegerTy(32)) {
419 GenericValue rv;
420 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
421 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
422 return rv;
423 }
424 break;
425 }
426 }
427
428 // Handle cases where no arguments are passed first.
429 if (ArgValues.empty()) {
430 GenericValue rv;
431 switch (RetTy->getTypeID()) {
432 default: llvm_unreachable("Unknown return type for function call!");
433 case Type::IntegerTyID: {
434 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
435 if (BitWidth == 1)
436 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
437 else if (BitWidth <= 8)
438 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
439 else if (BitWidth <= 16)
440 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
441 else if (BitWidth <= 32)
442 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
443 else if (BitWidth <= 64)
444 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
445 else
446 llvm_unreachable("Integer types > 64 bits not supported");
447 return rv;
448 }
449 case Type::VoidTyID:
450 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
451 return rv;
452 case Type::FloatTyID:
453 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
454 return rv;
455 case Type::DoubleTyID:
456 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
457 return rv;
458 case Type::X86_FP80TyID:
459 case Type::FP128TyID:
460 case Type::PPC_FP128TyID:
461 llvm_unreachable("long double not supported yet");
Jim Grosbach34714a02011-03-22 18:05:27 +0000462 case Type::PointerTyID:
463 return PTOGV(((void*(*)())(intptr_t)FPtr)());
464 }
465 }
466
Craig Topper85814382012-02-07 05:05:23 +0000467 llvm_unreachable("Full-featured argument passing not supported yet!");
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000468}
Danil Malyshev30b9e322012-03-28 21:46:36 +0000469
470void *MCJIT::getPointerToNamedFunction(const std::string &Name,
Eli Bendersky5fe01982012-04-29 12:40:47 +0000471 bool AbortOnFailure) {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000472 if (!isSymbolSearchingDisabled()) {
473 void *ptr = MemMgr.getPointerToNamedFunction(Name, false);
Danil Malyshev30b9e322012-03-28 21:46:36 +0000474 if (ptr)
475 return ptr;
476 }
477
478 /// If a LazyFunctionCreator is installed, use it to get/create the function.
479 if (LazyFunctionCreator)
480 if (void *RP = LazyFunctionCreator(Name))
481 return RP;
482
483 if (AbortOnFailure) {
484 report_fatal_error("Program used external function '"+Name+
Eli Bendersky5fe01982012-04-29 12:40:47 +0000485 "' which could not be resolved!");
Danil Malyshev30b9e322012-03-28 21:46:36 +0000486 }
487 return 0;
488}
Andrew Kaylor776054d2012-11-06 18:51:59 +0000489
490void MCJIT::RegisterJITEventListener(JITEventListener *L) {
491 if (L == NULL)
492 return;
493 MutexGuard locked(lock);
494 EventListeners.push_back(L);
495}
496void MCJIT::UnregisterJITEventListener(JITEventListener *L) {
497 if (L == NULL)
498 return;
499 MutexGuard locked(lock);
500 SmallVector<JITEventListener*, 2>::reverse_iterator I=
501 std::find(EventListeners.rbegin(), EventListeners.rend(), L);
502 if (I != EventListeners.rend()) {
503 std::swap(*I, EventListeners.back());
504 EventListeners.pop_back();
505 }
506}
507void MCJIT::NotifyObjectEmitted(const ObjectImage& Obj) {
508 MutexGuard locked(lock);
Andrew Kaylorb868e912013-10-04 00:49:38 +0000509 MemMgr.notifyObjectLoaded(this, &Obj);
Andrew Kaylor776054d2012-11-06 18:51:59 +0000510 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
511 EventListeners[I]->NotifyObjectEmitted(Obj);
512 }
513}
514void MCJIT::NotifyFreeingObject(const ObjectImage& Obj) {
515 MutexGuard locked(lock);
516 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
517 EventListeners[I]->NotifyFreeingObject(Obj);
518 }
519}
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000520
521uint64_t LinkingMemoryManager::getSymbolAddress(const std::string &Name) {
522 uint64_t Result = ParentEngine->getSymbolAddress(Name, false);
Andrew Kaylor52c90162013-10-01 16:42:50 +0000523 // If the symbols wasn't found and it begins with an underscore, try again
524 // without the underscore.
525 if (!Result && Name[0] == '_')
526 Result = ParentEngine->getSymbolAddress(Name.substr(1), false);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000527 if (Result)
528 return Result;
529 return ClientMM->getSymbolAddress(Name);
530}