blob: 5d7fd0c343a49583c5d49cd9973e33e832fd76af [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 Kaylor61694532013-10-21 17:42:06 +000065 MutexGuard locked(lock);
Andrew Kaylor43507d02013-10-16 00:14:21 +000066 Dyld.deregisterEHFrames();
67
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000068 LoadedObjectMap::iterator it, end = LoadedObjects.end();
69 for (it = LoadedObjects.begin(); it != end; ++it) {
70 ObjectImage *Obj = it->second;
71 if (Obj) {
72 NotifyFreeingObject(*Obj);
73 delete Obj;
74 }
75 }
76 LoadedObjects.clear();
Andrew Kaylorea708d12012-08-07 18:33:00 +000077 delete TM;
78}
79
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000080void MCJIT::addModule(Module *M) {
Andrew Kaylor61694532013-10-21 17:42:06 +000081 MutexGuard locked(lock);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000082 Modules.push_back(M);
83 ModuleStates[M] = MCJITModuleState();
84}
85
Andrew Kaylor1c489452013-04-25 21:02:36 +000086void MCJIT::setObjectCache(ObjectCache* NewCache) {
Andrew Kaylor61694532013-10-21 17:42:06 +000087 MutexGuard locked(lock);
Andrew Kaylor1c489452013-04-25 21:02:36 +000088 ObjCache = NewCache;
89}
90
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000091ObjectBufferStream* MCJIT::emitObject(Module *M) {
Andrew Kaylor61694532013-10-21 17:42:06 +000092 MutexGuard locked(lock);
93
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000094 // This must be a module which has already been added to this MCJIT instance.
95 assert(std::find(Modules.begin(), Modules.end(), M) != Modules.end());
96 assert(ModuleStates.find(M) != ModuleStates.end());
Andrew Kaylorea708d12012-08-07 18:33:00 +000097
Andrew Kaylorea708d12012-08-07 18:33:00 +000098 // Re-compilation is not supported
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000099 assert(!ModuleStates[M].hasBeenEmitted());
Andrew Kaylorea708d12012-08-07 18:33:00 +0000100
101 PassManager PM;
102
Micah Villmow3574eca2012-10-08 16:38:25 +0000103 PM.add(new DataLayout(*TM->getDataLayout()));
Jim Grosbach31649e62011-03-18 22:48:41 +0000104
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000105 // The RuntimeDyld will take ownership of this shortly
Andrew Kaylor1c489452013-04-25 21:02:36 +0000106 OwningPtr<ObjectBufferStream> CompiledObject(new ObjectBufferStream());
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000107
Jim Grosbach31649e62011-03-18 22:48:41 +0000108 // Turn the machine code intermediate representation into bytes in memory
109 // that may be executed.
Andrew Kaylor1c489452013-04-25 21:02:36 +0000110 if (TM->addPassesToEmitMC(PM, Ctx, CompiledObject->getOStream(), false)) {
Jim Grosbach31649e62011-03-18 22:48:41 +0000111 report_fatal_error("Target does not support MC emission!");
112 }
113
114 // Initialize passes.
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000115 PM.run(*M);
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000116 // Flush the output buffer to get the generated code into memory
Andrew Kaylor1c489452013-04-25 21:02:36 +0000117 CompiledObject->flush();
118
119 // If we have an object cache, tell it about the new object.
120 // Note that we're using the compiled image, not the loaded image (as below).
121 if (ObjCache) {
122 // MemoryBuffer is a thin wrapper around the actual memory, so it's OK
123 // to create a temporary object here and delete it after the call.
124 OwningPtr<MemoryBuffer> MB(CompiledObject->getMemBuffer());
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000125 ObjCache->notifyObjectCompiled(M, MB.get());
Andrew Kaylor1c489452013-04-25 21:02:36 +0000126 }
127
128 return CompiledObject.take();
129}
130
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000131void MCJIT::generateCodeForModule(Module *M) {
Andrew Kaylor61694532013-10-21 17:42:06 +0000132 // Get a thread lock to make sure we aren't trying to load multiple times
133 MutexGuard locked(lock);
134
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000135 // This must be a module which has already been added to this MCJIT instance.
136 assert(std::find(Modules.begin(), Modules.end(), M) != Modules.end());
137 assert(ModuleStates.find(M) != ModuleStates.end());
Andrew Kaylor1c489452013-04-25 21:02:36 +0000138
Andrew Kaylor1c489452013-04-25 21:02:36 +0000139 // Re-compilation is not supported
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000140 if (ModuleStates[M].hasBeenLoaded())
Andrew Kaylor1c489452013-04-25 21:02:36 +0000141 return;
142
143 OwningPtr<ObjectBuffer> ObjectToLoad;
144 // Try to load the pre-compiled object from cache if possible
145 if (0 != ObjCache) {
Andrew Kaylor40d81712013-06-28 21:40:16 +0000146 OwningPtr<MemoryBuffer> PreCompiledObject(ObjCache->getObject(M));
Andrew Kaylor1c489452013-04-25 21:02:36 +0000147 if (0 != PreCompiledObject.get())
148 ObjectToLoad.reset(new ObjectBuffer(PreCompiledObject.take()));
149 }
150
151 // If the cache did not contain a suitable object, compile the object
152 if (!ObjectToLoad) {
153 ObjectToLoad.reset(emitObject(M));
154 assert(ObjectToLoad.get() && "Compilation did not produce an object.");
155 }
Jim Grosbachf9229102011-03-22 01:06:42 +0000156
157 // Load the object into the dynamic linker.
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000158 // MCJIT now owns the ObjectImage pointer (via its LoadedObjects map).
159 ObjectImage *LoadedObject = Dyld.loadObject(ObjectToLoad.take());
160 LoadedObjects[M] = LoadedObject;
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000161 if (!LoadedObject)
Jim Grosbach8086f3b2011-03-23 19:51:34 +0000162 report_fatal_error(Dyld.getErrorString());
Andrew Kaylorea708d12012-08-07 18:33:00 +0000163
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000164 // FIXME: Make this optional, maybe even move it to a JIT event listener
165 LoadedObject->registerWithDebugger();
166
Andrew Kaylor776054d2012-11-06 18:51:59 +0000167 NotifyObjectEmitted(*LoadedObject);
168
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000169 ModuleStates[M] = ModuleLoaded;
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000170}
171
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000172void MCJIT::finalizeLoadedModules() {
Andrew Kaylor61694532013-10-21 17:42:06 +0000173 MutexGuard locked(lock);
174
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000175 // Resolve any outstanding relocations.
176 Dyld.resolveRelocations();
177
178 // Register EH frame data for any module we own which has been loaded
179 SmallVector<Module *, 1>::iterator end = Modules.end();
180 SmallVector<Module *, 1>::iterator it;
181 for (it = Modules.begin(); it != end; ++it) {
182 Module *M = *it;
183 assert(ModuleStates.find(M) != ModuleStates.end());
184
185 if (ModuleStates[M].hasBeenLoaded() &&
186 !ModuleStates[M].hasBeenFinalized()) {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000187 ModuleStates[M] = ModuleFinalized;
188 }
Andrew Kaylor28989882012-11-05 20:57:16 +0000189 }
190
Andrew Kaylor528f6d72013-10-11 21:25:48 +0000191 Dyld.registerEHFrames();
192
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000193 // Set page permissions.
194 MemMgr.finalizeMemory();
195}
196
197// FIXME: Rename this.
198void MCJIT::finalizeObject() {
Andrew Kaylor61694532013-10-21 17:42:06 +0000199 MutexGuard locked(lock);
200
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000201 // FIXME: This is a temporary hack to get around problems with calling
202 // finalize multiple times.
203 bool finalizeNeeded = false;
204 SmallVector<Module *, 1>::iterator end = Modules.end();
205 SmallVector<Module *, 1>::iterator it;
206 for (it = Modules.begin(); it != end; ++it) {
207 Module *M = *it;
208 assert(ModuleStates.find(M) != ModuleStates.end());
209 if (!ModuleStates[M].hasBeenFinalized())
210 finalizeNeeded = true;
211
212 // I don't really like this, but the C API depends on this behavior.
213 // I suppose it's OK for a deprecated function.
214 if (!ModuleStates[M].hasBeenLoaded())
215 generateCodeForModule(M);
216 }
217 if (!finalizeNeeded)
218 return;
219
220 // Resolve any outstanding relocations.
221 Dyld.resolveRelocations();
222
223 // Register EH frame data for any module we own which has been loaded
224 for (it = Modules.begin(); it != end; ++it) {
225 Module *M = *it;
226 assert(ModuleStates.find(M) != ModuleStates.end());
227
228 if (ModuleStates[M].hasBeenLoaded() &&
229 !ModuleStates[M].hasBeenFinalized()) {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000230 ModuleStates[M] = ModuleFinalized;
231 }
232 }
Andrew Kaylor53608a32012-11-15 23:50:01 +0000233
Andrew Kaylor528f6d72013-10-11 21:25:48 +0000234 Dyld.registerEHFrames();
235
Andrew Kaylor53608a32012-11-15 23:50:01 +0000236 // Set page permissions.
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000237 MemMgr.finalizeMemory();
238}
239
240void MCJIT::finalizeModule(Module *M) {
Andrew Kaylor61694532013-10-21 17:42:06 +0000241 MutexGuard locked(lock);
242
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000243 // This must be a module which has already been added to this MCJIT instance.
244 assert(std::find(Modules.begin(), Modules.end(), M) != Modules.end());
245 assert(ModuleStates.find(M) != ModuleStates.end());
246
247 if (ModuleStates[M].hasBeenFinalized())
248 return;
249
250 // If the module hasn't been compiled, just do that.
251 if (!ModuleStates[M].hasBeenLoaded())
252 generateCodeForModule(M);
253
254 // Resolve any outstanding relocations.
255 Dyld.resolveRelocations();
256
Andrew Kaylor528f6d72013-10-11 21:25:48 +0000257 Dyld.registerEHFrames();
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000258
259 // Set page permissions.
260 MemMgr.finalizeMemory();
261
262 ModuleStates[M] = ModuleFinalized;
Andrew Kaylor28989882012-11-05 20:57:16 +0000263}
264
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000265void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) {
266 report_fatal_error("not yet implemented");
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000267}
268
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000269uint64_t MCJIT::getExistingSymbolAddress(const std::string &Name) {
270 // Check with the RuntimeDyld to see if we already have this symbol.
271 if (Name[0] == '\1')
272 return Dyld.getSymbolLoadAddress(Name.substr(1));
273 return Dyld.getSymbolLoadAddress((TM->getMCAsmInfo()->getGlobalPrefix()
274 + Name));
275}
Jim Grosbach35ed8422012-09-05 16:50:40 +0000276
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000277Module *MCJIT::findModuleForSymbol(const std::string &Name,
278 bool CheckFunctionsOnly) {
Andrew Kaylor61694532013-10-21 17:42:06 +0000279 MutexGuard locked(lock);
280
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000281 // If it hasn't already been generated, see if it's in one of our modules.
282 SmallVector<Module *, 1>::iterator end = Modules.end();
283 SmallVector<Module *, 1>::iterator it;
284 for (it = Modules.begin(); it != end; ++it) {
285 Module *M = *it;
286 Function *F = M->getFunction(Name);
287 if (F && !F->empty())
288 return M;
289 if (!CheckFunctionsOnly) {
290 GlobalVariable *G = M->getGlobalVariable(Name);
291 if (G)
292 return M;
293 // FIXME: Do we need to worry about global aliases?
294 }
295 }
296 // We didn't find the symbol in any of our modules.
297 return NULL;
298}
299
300uint64_t MCJIT::getSymbolAddress(const std::string &Name,
301 bool CheckFunctionsOnly)
302{
Andrew Kaylor61694532013-10-21 17:42:06 +0000303 MutexGuard locked(lock);
304
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000305 // First, check to see if we already have this symbol.
306 uint64_t Addr = getExistingSymbolAddress(Name);
307 if (Addr)
308 return Addr;
309
310 // If it hasn't already been generated, see if it's in one of our modules.
311 Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
312 if (!M)
313 return 0;
314
315 // If this is in one of our modules, generate code for that module.
316 assert(ModuleStates.find(M) != ModuleStates.end());
317 // If the module code has already been generated, we won't find the symbol.
318 if (ModuleStates[M].hasBeenLoaded())
319 return 0;
320
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000321 generateCodeForModule(M);
322
323 // Check the RuntimeDyld table again, it should be there now.
324 return getExistingSymbolAddress(Name);
325}
326
327uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) {
Andrew Kaylor61694532013-10-21 17:42:06 +0000328 MutexGuard locked(lock);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000329 uint64_t Result = getSymbolAddress(Name, false);
330 if (Result != 0)
331 finalizeLoadedModules();
332 return Result;
333}
334
335uint64_t MCJIT::getFunctionAddress(const std::string &Name) {
Andrew Kaylor61694532013-10-21 17:42:06 +0000336 MutexGuard locked(lock);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000337 uint64_t Result = getSymbolAddress(Name, true);
338 if (Result != 0)
339 finalizeLoadedModules();
340 return Result;
341}
342
343// Deprecated. Use getFunctionAddress instead.
344void *MCJIT::getPointerToFunction(Function *F) {
Andrew Kaylor61694532013-10-21 17:42:06 +0000345 MutexGuard locked(lock);
Andrew Kaylorea708d12012-08-07 18:33:00 +0000346
Jim Grosbach34714a02011-03-22 18:05:27 +0000347 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
348 bool AbortOnFailure = !F->hasExternalWeakLinkage();
349 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
350 addGlobalMapping(F, Addr);
351 return Addr;
352 }
353
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000354 // If this function doesn't belong to one of our modules, we're done.
355 Module *M = F->getParent();
356 if (std::find(Modules.begin(), Modules.end(), M) == Modules.end())
357 return NULL;
358
359 assert(ModuleStates.find(M) != ModuleStates.end());
360
361 // Make sure the relevant module has been compiled and loaded.
362 if (!ModuleStates[M].hasBeenLoaded())
363 generateCodeForModule(M);
364
Andrew Kaylorea708d12012-08-07 18:33:00 +0000365 // FIXME: Should the Dyld be retaining module information? Probably not.
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +0000366 // FIXME: Should we be using the mangler for this? Probably.
Jim Grosbach35ed8422012-09-05 16:50:40 +0000367 //
368 // This is the accessor for the target address, so make sure to check the
369 // load address of the symbol, not the local address.
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +0000370 StringRef BaseName = F->getName();
371 if (BaseName[0] == '\1')
Jim Grosbach35ed8422012-09-05 16:50:40 +0000372 return (void*)Dyld.getSymbolLoadAddress(BaseName.substr(1));
373 return (void*)Dyld.getSymbolLoadAddress((TM->getMCAsmInfo()->getGlobalPrefix()
Jim Grosbachc0ceedb2011-05-19 00:45:05 +0000374 + BaseName).str());
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000375}
376
377void *MCJIT::recompileAndRelinkFunction(Function *F) {
378 report_fatal_error("not yet implemented");
379}
380
381void MCJIT::freeMachineCodeForFunction(Function *F) {
382 report_fatal_error("not yet implemented");
383}
384
385GenericValue MCJIT::runFunction(Function *F,
386 const std::vector<GenericValue> &ArgValues) {
Jim Grosbach34714a02011-03-22 18:05:27 +0000387 assert(F && "Function *F was null at entry to run()");
388
Jim Grosbach31649e62011-03-18 22:48:41 +0000389 void *FPtr = getPointerToFunction(F);
Jim Grosbach34714a02011-03-22 18:05:27 +0000390 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000391 FunctionType *FTy = F->getFunctionType();
392 Type *RetTy = FTy->getReturnType();
Jim Grosbach34714a02011-03-22 18:05:27 +0000393
394 assert((FTy->getNumParams() == ArgValues.size() ||
395 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
396 "Wrong number of arguments passed into function!");
397 assert(FTy->getNumParams() == ArgValues.size() &&
398 "This doesn't support passing arguments through varargs (yet)!");
399
400 // Handle some common cases first. These cases correspond to common `main'
401 // prototypes.
402 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
403 switch (ArgValues.size()) {
404 case 3:
405 if (FTy->getParamType(0)->isIntegerTy(32) &&
406 FTy->getParamType(1)->isPointerTy() &&
407 FTy->getParamType(2)->isPointerTy()) {
408 int (*PF)(int, char **, const char **) =
409 (int(*)(int, char **, const 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 (const char **)GVTOP(ArgValues[2])));
416 return rv;
417 }
418 break;
419 case 2:
420 if (FTy->getParamType(0)->isIntegerTy(32) &&
421 FTy->getParamType(1)->isPointerTy()) {
422 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
423
424 // Call the function.
425 GenericValue rv;
426 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
427 (char **)GVTOP(ArgValues[1])));
428 return rv;
429 }
430 break;
431 case 1:
432 if (FTy->getNumParams() == 1 &&
433 FTy->getParamType(0)->isIntegerTy(32)) {
434 GenericValue rv;
435 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
436 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
437 return rv;
438 }
439 break;
440 }
441 }
442
443 // Handle cases where no arguments are passed first.
444 if (ArgValues.empty()) {
445 GenericValue rv;
446 switch (RetTy->getTypeID()) {
447 default: llvm_unreachable("Unknown return type for function call!");
448 case Type::IntegerTyID: {
449 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
450 if (BitWidth == 1)
451 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
452 else if (BitWidth <= 8)
453 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
454 else if (BitWidth <= 16)
455 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
456 else if (BitWidth <= 32)
457 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
458 else if (BitWidth <= 64)
459 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
460 else
461 llvm_unreachable("Integer types > 64 bits not supported");
462 return rv;
463 }
464 case Type::VoidTyID:
465 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
466 return rv;
467 case Type::FloatTyID:
468 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
469 return rv;
470 case Type::DoubleTyID:
471 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
472 return rv;
473 case Type::X86_FP80TyID:
474 case Type::FP128TyID:
475 case Type::PPC_FP128TyID:
476 llvm_unreachable("long double not supported yet");
Jim Grosbach34714a02011-03-22 18:05:27 +0000477 case Type::PointerTyID:
478 return PTOGV(((void*(*)())(intptr_t)FPtr)());
479 }
480 }
481
Craig Topper85814382012-02-07 05:05:23 +0000482 llvm_unreachable("Full-featured argument passing not supported yet!");
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000483}
Danil Malyshev30b9e322012-03-28 21:46:36 +0000484
485void *MCJIT::getPointerToNamedFunction(const std::string &Name,
Eli Bendersky5fe01982012-04-29 12:40:47 +0000486 bool AbortOnFailure) {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000487 if (!isSymbolSearchingDisabled()) {
488 void *ptr = MemMgr.getPointerToNamedFunction(Name, false);
Danil Malyshev30b9e322012-03-28 21:46:36 +0000489 if (ptr)
490 return ptr;
491 }
492
493 /// If a LazyFunctionCreator is installed, use it to get/create the function.
494 if (LazyFunctionCreator)
495 if (void *RP = LazyFunctionCreator(Name))
496 return RP;
497
498 if (AbortOnFailure) {
499 report_fatal_error("Program used external function '"+Name+
Eli Bendersky5fe01982012-04-29 12:40:47 +0000500 "' which could not be resolved!");
Danil Malyshev30b9e322012-03-28 21:46:36 +0000501 }
502 return 0;
503}
Andrew Kaylor776054d2012-11-06 18:51:59 +0000504
505void MCJIT::RegisterJITEventListener(JITEventListener *L) {
506 if (L == NULL)
507 return;
508 MutexGuard locked(lock);
509 EventListeners.push_back(L);
510}
511void MCJIT::UnregisterJITEventListener(JITEventListener *L) {
512 if (L == NULL)
513 return;
514 MutexGuard locked(lock);
515 SmallVector<JITEventListener*, 2>::reverse_iterator I=
516 std::find(EventListeners.rbegin(), EventListeners.rend(), L);
517 if (I != EventListeners.rend()) {
518 std::swap(*I, EventListeners.back());
519 EventListeners.pop_back();
520 }
521}
522void MCJIT::NotifyObjectEmitted(const ObjectImage& Obj) {
523 MutexGuard locked(lock);
Andrew Kaylorb868e912013-10-04 00:49:38 +0000524 MemMgr.notifyObjectLoaded(this, &Obj);
Andrew Kaylor776054d2012-11-06 18:51:59 +0000525 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
526 EventListeners[I]->NotifyObjectEmitted(Obj);
527 }
528}
529void MCJIT::NotifyFreeingObject(const ObjectImage& Obj) {
530 MutexGuard locked(lock);
531 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
532 EventListeners[I]->NotifyFreeingObject(Obj);
533 }
534}
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000535
536uint64_t LinkingMemoryManager::getSymbolAddress(const std::string &Name) {
537 uint64_t Result = ParentEngine->getSymbolAddress(Name, false);
Andrew Kaylor52c90162013-10-01 16:42:50 +0000538 // If the symbols wasn't found and it begins with an underscore, try again
539 // without the underscore.
540 if (!Result && Name[0] == '_')
541 Result = ParentEngine->getSymbolAddress(Name.substr(1), false);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000542 if (Result)
543 return Result;
544 return ClientMM->getSymbolAddress(Name);
545}