blob: bcd0886976fb8f7415a545a22e61aa4033648db8 [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 Kaylor43507d02013-10-16 00:14:21 +000065 Dyld.deregisterEHFrames();
66
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000067 LoadedObjectMap::iterator it, end = LoadedObjects.end();
68 for (it = LoadedObjects.begin(); it != end; ++it) {
69 ObjectImage *Obj = it->second;
70 if (Obj) {
71 NotifyFreeingObject(*Obj);
72 delete Obj;
73 }
74 }
75 LoadedObjects.clear();
Andrew Kaylorea708d12012-08-07 18:33:00 +000076 delete TM;
77}
78
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000079void MCJIT::addModule(Module *M) {
80 Modules.push_back(M);
81 ModuleStates[M] = MCJITModuleState();
82}
83
Andrew Kaylor1c489452013-04-25 21:02:36 +000084void MCJIT::setObjectCache(ObjectCache* NewCache) {
85 ObjCache = NewCache;
86}
87
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000088ObjectBufferStream* MCJIT::emitObject(Module *M) {
89 // This must be a module which has already been added to this MCJIT instance.
90 assert(std::find(Modules.begin(), Modules.end(), M) != Modules.end());
91 assert(ModuleStates.find(M) != ModuleStates.end());
Andrew Kaylorea708d12012-08-07 18:33:00 +000092
93 // Get a thread lock to make sure we aren't trying to compile multiple times
94 MutexGuard locked(lock);
95
Andrew Kaylorea708d12012-08-07 18:33:00 +000096 // Re-compilation is not supported
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000097 assert(!ModuleStates[M].hasBeenEmitted());
Andrew Kaylorea708d12012-08-07 18:33:00 +000098
99 PassManager PM;
100
Micah Villmow3574eca2012-10-08 16:38:25 +0000101 PM.add(new DataLayout(*TM->getDataLayout()));
Jim Grosbach31649e62011-03-18 22:48:41 +0000102
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000103 // The RuntimeDyld will take ownership of this shortly
Andrew Kaylor1c489452013-04-25 21:02:36 +0000104 OwningPtr<ObjectBufferStream> CompiledObject(new ObjectBufferStream());
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000105
Jim Grosbach31649e62011-03-18 22:48:41 +0000106 // Turn the machine code intermediate representation into bytes in memory
107 // that may be executed.
Andrew Kaylor1c489452013-04-25 21:02:36 +0000108 if (TM->addPassesToEmitMC(PM, Ctx, CompiledObject->getOStream(), false)) {
Jim Grosbach31649e62011-03-18 22:48:41 +0000109 report_fatal_error("Target does not support MC emission!");
110 }
111
112 // Initialize passes.
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000113 PM.run(*M);
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000114 // Flush the output buffer to get the generated code into memory
Andrew Kaylor1c489452013-04-25 21:02:36 +0000115 CompiledObject->flush();
116
117 // If we have an object cache, tell it about the new object.
118 // Note that we're using the compiled image, not the loaded image (as below).
119 if (ObjCache) {
120 // MemoryBuffer is a thin wrapper around the actual memory, so it's OK
121 // to create a temporary object here and delete it after the call.
122 OwningPtr<MemoryBuffer> MB(CompiledObject->getMemBuffer());
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000123 ObjCache->notifyObjectCompiled(M, MB.get());
Andrew Kaylor1c489452013-04-25 21:02:36 +0000124 }
125
126 return CompiledObject.take();
127}
128
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000129void MCJIT::generateCodeForModule(Module *M) {
130 // This must be a module which has already been added to this MCJIT instance.
131 assert(std::find(Modules.begin(), Modules.end(), M) != Modules.end());
132 assert(ModuleStates.find(M) != ModuleStates.end());
Andrew Kaylor1c489452013-04-25 21:02:36 +0000133
134 // Get a thread lock to make sure we aren't trying to load multiple times
135 MutexGuard locked(lock);
136
Andrew Kaylor1c489452013-04-25 21:02:36 +0000137 // Re-compilation is not supported
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000138 if (ModuleStates[M].hasBeenLoaded())
Andrew Kaylor1c489452013-04-25 21:02:36 +0000139 return;
140
141 OwningPtr<ObjectBuffer> ObjectToLoad;
142 // Try to load the pre-compiled object from cache if possible
143 if (0 != ObjCache) {
Andrew Kaylor40d81712013-06-28 21:40:16 +0000144 OwningPtr<MemoryBuffer> PreCompiledObject(ObjCache->getObject(M));
Andrew Kaylor1c489452013-04-25 21:02:36 +0000145 if (0 != PreCompiledObject.get())
146 ObjectToLoad.reset(new ObjectBuffer(PreCompiledObject.take()));
147 }
148
149 // If the cache did not contain a suitable object, compile the object
150 if (!ObjectToLoad) {
151 ObjectToLoad.reset(emitObject(M));
152 assert(ObjectToLoad.get() && "Compilation did not produce an object.");
153 }
Jim Grosbachf9229102011-03-22 01:06:42 +0000154
155 // Load the object into the dynamic linker.
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000156 // MCJIT now owns the ObjectImage pointer (via its LoadedObjects map).
157 ObjectImage *LoadedObject = Dyld.loadObject(ObjectToLoad.take());
158 LoadedObjects[M] = LoadedObject;
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000159 if (!LoadedObject)
Jim Grosbach8086f3b2011-03-23 19:51:34 +0000160 report_fatal_error(Dyld.getErrorString());
Andrew Kaylorea708d12012-08-07 18:33:00 +0000161
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000162 // FIXME: Make this optional, maybe even move it to a JIT event listener
163 LoadedObject->registerWithDebugger();
164
Andrew Kaylor776054d2012-11-06 18:51:59 +0000165 NotifyObjectEmitted(*LoadedObject);
166
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000167 ModuleStates[M] = ModuleLoaded;
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000168}
169
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000170void MCJIT::finalizeLoadedModules() {
171 // Resolve any outstanding relocations.
172 Dyld.resolveRelocations();
173
174 // Register EH frame data for any module we own which has been loaded
175 SmallVector<Module *, 1>::iterator end = Modules.end();
176 SmallVector<Module *, 1>::iterator it;
177 for (it = Modules.begin(); it != end; ++it) {
178 Module *M = *it;
179 assert(ModuleStates.find(M) != ModuleStates.end());
180
181 if (ModuleStates[M].hasBeenLoaded() &&
182 !ModuleStates[M].hasBeenFinalized()) {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000183 ModuleStates[M] = ModuleFinalized;
184 }
Andrew Kaylor28989882012-11-05 20:57:16 +0000185 }
186
Andrew Kaylor528f6d72013-10-11 21:25:48 +0000187 Dyld.registerEHFrames();
188
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000189 // Set page permissions.
190 MemMgr.finalizeMemory();
191}
192
193// FIXME: Rename this.
194void MCJIT::finalizeObject() {
195 // FIXME: This is a temporary hack to get around problems with calling
196 // finalize multiple times.
197 bool finalizeNeeded = false;
198 SmallVector<Module *, 1>::iterator end = Modules.end();
199 SmallVector<Module *, 1>::iterator it;
200 for (it = Modules.begin(); it != end; ++it) {
201 Module *M = *it;
202 assert(ModuleStates.find(M) != ModuleStates.end());
203 if (!ModuleStates[M].hasBeenFinalized())
204 finalizeNeeded = true;
205
206 // I don't really like this, but the C API depends on this behavior.
207 // I suppose it's OK for a deprecated function.
208 if (!ModuleStates[M].hasBeenLoaded())
209 generateCodeForModule(M);
210 }
211 if (!finalizeNeeded)
212 return;
213
214 // Resolve any outstanding relocations.
215 Dyld.resolveRelocations();
216
217 // Register EH frame data for any module we own which has been loaded
218 for (it = Modules.begin(); it != end; ++it) {
219 Module *M = *it;
220 assert(ModuleStates.find(M) != ModuleStates.end());
221
222 if (ModuleStates[M].hasBeenLoaded() &&
223 !ModuleStates[M].hasBeenFinalized()) {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000224 ModuleStates[M] = ModuleFinalized;
225 }
226 }
Andrew Kaylor53608a32012-11-15 23:50:01 +0000227
Andrew Kaylor528f6d72013-10-11 21:25:48 +0000228 Dyld.registerEHFrames();
229
Andrew Kaylor53608a32012-11-15 23:50:01 +0000230 // Set page permissions.
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000231 MemMgr.finalizeMemory();
232}
233
234void MCJIT::finalizeModule(Module *M) {
235 // This must be a module which has already been added to this MCJIT instance.
236 assert(std::find(Modules.begin(), Modules.end(), M) != Modules.end());
237 assert(ModuleStates.find(M) != ModuleStates.end());
238
239 if (ModuleStates[M].hasBeenFinalized())
240 return;
241
242 // If the module hasn't been compiled, just do that.
243 if (!ModuleStates[M].hasBeenLoaded())
244 generateCodeForModule(M);
245
246 // Resolve any outstanding relocations.
247 Dyld.resolveRelocations();
248
Andrew Kaylor528f6d72013-10-11 21:25:48 +0000249 Dyld.registerEHFrames();
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000250
251 // Set page permissions.
252 MemMgr.finalizeMemory();
253
254 ModuleStates[M] = ModuleFinalized;
Andrew Kaylor28989882012-11-05 20:57:16 +0000255}
256
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000257void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) {
258 report_fatal_error("not yet implemented");
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000259}
260
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000261uint64_t MCJIT::getExistingSymbolAddress(const std::string &Name) {
262 // Check with the RuntimeDyld to see if we already have this symbol.
263 if (Name[0] == '\1')
264 return Dyld.getSymbolLoadAddress(Name.substr(1));
265 return Dyld.getSymbolLoadAddress((TM->getMCAsmInfo()->getGlobalPrefix()
266 + Name));
267}
Jim Grosbach35ed8422012-09-05 16:50:40 +0000268
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000269Module *MCJIT::findModuleForSymbol(const std::string &Name,
270 bool CheckFunctionsOnly) {
271 // If it hasn't already been generated, see if it's in one of our modules.
272 SmallVector<Module *, 1>::iterator end = Modules.end();
273 SmallVector<Module *, 1>::iterator it;
274 for (it = Modules.begin(); it != end; ++it) {
275 Module *M = *it;
276 Function *F = M->getFunction(Name);
277 if (F && !F->empty())
278 return M;
279 if (!CheckFunctionsOnly) {
280 GlobalVariable *G = M->getGlobalVariable(Name);
281 if (G)
282 return M;
283 // FIXME: Do we need to worry about global aliases?
284 }
285 }
286 // We didn't find the symbol in any of our modules.
287 return NULL;
288}
289
290uint64_t MCJIT::getSymbolAddress(const std::string &Name,
291 bool CheckFunctionsOnly)
292{
293 // First, check to see if we already have this symbol.
294 uint64_t Addr = getExistingSymbolAddress(Name);
295 if (Addr)
296 return Addr;
297
298 // If it hasn't already been generated, see if it's in one of our modules.
299 Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
300 if (!M)
301 return 0;
302
303 // If this is in one of our modules, generate code for that module.
304 assert(ModuleStates.find(M) != ModuleStates.end());
305 // If the module code has already been generated, we won't find the symbol.
306 if (ModuleStates[M].hasBeenLoaded())
307 return 0;
308
309 // FIXME: We probably need to make sure we aren't in the process of
310 // loading or finalizing this module.
311 generateCodeForModule(M);
312
313 // Check the RuntimeDyld table again, it should be there now.
314 return getExistingSymbolAddress(Name);
315}
316
317uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) {
318 uint64_t Result = getSymbolAddress(Name, false);
319 if (Result != 0)
320 finalizeLoadedModules();
321 return Result;
322}
323
324uint64_t MCJIT::getFunctionAddress(const std::string &Name) {
325 uint64_t Result = getSymbolAddress(Name, true);
326 if (Result != 0)
327 finalizeLoadedModules();
328 return Result;
329}
330
331// Deprecated. Use getFunctionAddress instead.
332void *MCJIT::getPointerToFunction(Function *F) {
Andrew Kaylorea708d12012-08-07 18:33:00 +0000333
Jim Grosbach34714a02011-03-22 18:05:27 +0000334 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
335 bool AbortOnFailure = !F->hasExternalWeakLinkage();
336 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
337 addGlobalMapping(F, Addr);
338 return Addr;
339 }
340
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000341 // If this function doesn't belong to one of our modules, we're done.
342 Module *M = F->getParent();
343 if (std::find(Modules.begin(), Modules.end(), M) == Modules.end())
344 return NULL;
345
346 assert(ModuleStates.find(M) != ModuleStates.end());
347
348 // Make sure the relevant module has been compiled and loaded.
349 if (!ModuleStates[M].hasBeenLoaded())
350 generateCodeForModule(M);
351
Andrew Kaylorea708d12012-08-07 18:33:00 +0000352 // FIXME: Should the Dyld be retaining module information? Probably not.
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +0000353 // FIXME: Should we be using the mangler for this? Probably.
Jim Grosbach35ed8422012-09-05 16:50:40 +0000354 //
355 // This is the accessor for the target address, so make sure to check the
356 // load address of the symbol, not the local address.
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +0000357 StringRef BaseName = F->getName();
358 if (BaseName[0] == '\1')
Jim Grosbach35ed8422012-09-05 16:50:40 +0000359 return (void*)Dyld.getSymbolLoadAddress(BaseName.substr(1));
360 return (void*)Dyld.getSymbolLoadAddress((TM->getMCAsmInfo()->getGlobalPrefix()
Jim Grosbachc0ceedb2011-05-19 00:45:05 +0000361 + BaseName).str());
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000362}
363
364void *MCJIT::recompileAndRelinkFunction(Function *F) {
365 report_fatal_error("not yet implemented");
366}
367
368void MCJIT::freeMachineCodeForFunction(Function *F) {
369 report_fatal_error("not yet implemented");
370}
371
372GenericValue MCJIT::runFunction(Function *F,
373 const std::vector<GenericValue> &ArgValues) {
Jim Grosbach34714a02011-03-22 18:05:27 +0000374 assert(F && "Function *F was null at entry to run()");
375
Jim Grosbach31649e62011-03-18 22:48:41 +0000376 void *FPtr = getPointerToFunction(F);
Jim Grosbach34714a02011-03-22 18:05:27 +0000377 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000378 FunctionType *FTy = F->getFunctionType();
379 Type *RetTy = FTy->getReturnType();
Jim Grosbach34714a02011-03-22 18:05:27 +0000380
381 assert((FTy->getNumParams() == ArgValues.size() ||
382 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
383 "Wrong number of arguments passed into function!");
384 assert(FTy->getNumParams() == ArgValues.size() &&
385 "This doesn't support passing arguments through varargs (yet)!");
386
387 // Handle some common cases first. These cases correspond to common `main'
388 // prototypes.
389 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
390 switch (ArgValues.size()) {
391 case 3:
392 if (FTy->getParamType(0)->isIntegerTy(32) &&
393 FTy->getParamType(1)->isPointerTy() &&
394 FTy->getParamType(2)->isPointerTy()) {
395 int (*PF)(int, char **, const char **) =
396 (int(*)(int, char **, const char **))(intptr_t)FPtr;
397
398 // Call the function.
399 GenericValue rv;
400 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
401 (char **)GVTOP(ArgValues[1]),
402 (const char **)GVTOP(ArgValues[2])));
403 return rv;
404 }
405 break;
406 case 2:
407 if (FTy->getParamType(0)->isIntegerTy(32) &&
408 FTy->getParamType(1)->isPointerTy()) {
409 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
410
411 // Call the function.
412 GenericValue rv;
413 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
414 (char **)GVTOP(ArgValues[1])));
415 return rv;
416 }
417 break;
418 case 1:
419 if (FTy->getNumParams() == 1 &&
420 FTy->getParamType(0)->isIntegerTy(32)) {
421 GenericValue rv;
422 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
423 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
424 return rv;
425 }
426 break;
427 }
428 }
429
430 // Handle cases where no arguments are passed first.
431 if (ArgValues.empty()) {
432 GenericValue rv;
433 switch (RetTy->getTypeID()) {
434 default: llvm_unreachable("Unknown return type for function call!");
435 case Type::IntegerTyID: {
436 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
437 if (BitWidth == 1)
438 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
439 else if (BitWidth <= 8)
440 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
441 else if (BitWidth <= 16)
442 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
443 else if (BitWidth <= 32)
444 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
445 else if (BitWidth <= 64)
446 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
447 else
448 llvm_unreachable("Integer types > 64 bits not supported");
449 return rv;
450 }
451 case Type::VoidTyID:
452 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
453 return rv;
454 case Type::FloatTyID:
455 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
456 return rv;
457 case Type::DoubleTyID:
458 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
459 return rv;
460 case Type::X86_FP80TyID:
461 case Type::FP128TyID:
462 case Type::PPC_FP128TyID:
463 llvm_unreachable("long double not supported yet");
Jim Grosbach34714a02011-03-22 18:05:27 +0000464 case Type::PointerTyID:
465 return PTOGV(((void*(*)())(intptr_t)FPtr)());
466 }
467 }
468
Craig Topper85814382012-02-07 05:05:23 +0000469 llvm_unreachable("Full-featured argument passing not supported yet!");
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000470}
Danil Malyshev30b9e322012-03-28 21:46:36 +0000471
472void *MCJIT::getPointerToNamedFunction(const std::string &Name,
Eli Bendersky5fe01982012-04-29 12:40:47 +0000473 bool AbortOnFailure) {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000474 if (!isSymbolSearchingDisabled()) {
475 void *ptr = MemMgr.getPointerToNamedFunction(Name, false);
Danil Malyshev30b9e322012-03-28 21:46:36 +0000476 if (ptr)
477 return ptr;
478 }
479
480 /// If a LazyFunctionCreator is installed, use it to get/create the function.
481 if (LazyFunctionCreator)
482 if (void *RP = LazyFunctionCreator(Name))
483 return RP;
484
485 if (AbortOnFailure) {
486 report_fatal_error("Program used external function '"+Name+
Eli Bendersky5fe01982012-04-29 12:40:47 +0000487 "' which could not be resolved!");
Danil Malyshev30b9e322012-03-28 21:46:36 +0000488 }
489 return 0;
490}
Andrew Kaylor776054d2012-11-06 18:51:59 +0000491
492void MCJIT::RegisterJITEventListener(JITEventListener *L) {
493 if (L == NULL)
494 return;
495 MutexGuard locked(lock);
496 EventListeners.push_back(L);
497}
498void MCJIT::UnregisterJITEventListener(JITEventListener *L) {
499 if (L == NULL)
500 return;
501 MutexGuard locked(lock);
502 SmallVector<JITEventListener*, 2>::reverse_iterator I=
503 std::find(EventListeners.rbegin(), EventListeners.rend(), L);
504 if (I != EventListeners.rend()) {
505 std::swap(*I, EventListeners.back());
506 EventListeners.pop_back();
507 }
508}
509void MCJIT::NotifyObjectEmitted(const ObjectImage& Obj) {
510 MutexGuard locked(lock);
Andrew Kaylorb868e912013-10-04 00:49:38 +0000511 MemMgr.notifyObjectLoaded(this, &Obj);
Andrew Kaylor776054d2012-11-06 18:51:59 +0000512 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
513 EventListeners[I]->NotifyObjectEmitted(Obj);
514 }
515}
516void MCJIT::NotifyFreeingObject(const ObjectImage& Obj) {
517 MutexGuard locked(lock);
518 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
519 EventListeners[I]->NotifyFreeingObject(Obj);
520 }
521}
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000522
523uint64_t LinkingMemoryManager::getSymbolAddress(const std::string &Name) {
524 uint64_t Result = ParentEngine->getSymbolAddress(Name, false);
Andrew Kaylor52c90162013-10-01 16:42:50 +0000525 // If the symbols wasn't found and it begins with an underscore, try again
526 // without the underscore.
527 if (!Result && Name[0] == '_')
528 Result = ParentEngine->getSymbolAddress(Name.substr(1), false);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000529 if (Result)
530 return Result;
531 return ClientMM->getSymbolAddress(Name);
532}