blob: da4164e6476a518c058f92b345d518c68713e979 [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 Kaylor2ad18ef2013-10-24 00:19:14 +000017#include "llvm/PassManager.h"
Andrew Kaylord2755af2013-04-29 17:49:40 +000018#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/DataLayout.h"
20#include "llvm/IR/DerivedTypes.h"
21#include "llvm/IR/Function.h"
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000022#include "llvm/IR/Module.h"
Jim Grosbachf9229102011-03-22 01:06:42 +000023#include "llvm/MC/MCAsmInfo.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000024#include "llvm/Support/DynamicLibrary.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/Support/ErrorHandling.h"
Jim Grosbachf9229102011-03-22 01:06:42 +000026#include "llvm/Support/MemoryBuffer.h"
Andrew Kaylorea708d12012-08-07 18:33:00 +000027#include "llvm/Support/MutexGuard.h"
Daniel Dunbar6aec2982010-11-17 16:06:43 +000028
29using namespace llvm;
30
31namespace {
32
33static struct RegisterJIT {
34 RegisterJIT() { MCJIT::Register(); }
35} JITRegistrator;
36
37}
38
39extern "C" void LLVMLinkInMCJIT() {
40}
41
42ExecutionEngine *MCJIT::createJIT(Module *M,
43 std::string *ErrorStr,
Filip Pizlo13a3cf12013-05-14 19:29:00 +000044 RTDyldMemoryManager *MemMgr,
Daniel Dunbar6aec2982010-11-17 16:06:43 +000045 bool GVsWithCode,
Dylan Noblesmithc5b28582011-05-13 21:51:29 +000046 TargetMachine *TM) {
Daniel Dunbar6aec2982010-11-17 16:06:43 +000047 // Try to register the program as a source of symbols to resolve against.
48 //
49 // FIXME: Don't do this here.
50 sys::DynamicLibrary::LoadLibraryPermanently(0, NULL);
51
Filip Pizlo13a3cf12013-05-14 19:29:00 +000052 return new MCJIT(M, TM, MemMgr ? MemMgr : new SectionMemoryManager(),
53 GVsWithCode);
Daniel Dunbar6aec2982010-11-17 16:06:43 +000054}
55
Jim Grosbach8005bcd2012-08-21 15:42:49 +000056MCJIT::MCJIT(Module *m, TargetMachine *tm, RTDyldMemoryManager *MM,
57 bool AllocateGVsWithCode)
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000058 : ExecutionEngine(m), TM(tm), Ctx(0), MemMgr(this, MM), Dyld(&MemMgr),
59 ObjCache(0) {
Jim Grosbach31649e62011-03-18 22:48:41 +000060
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +000061 OwnedModules.addModule(m);
Micah Villmow3574eca2012-10-08 16:38:25 +000062 setDataLayout(TM->getDataLayout());
Andrew Kaylorea708d12012-08-07 18:33:00 +000063}
64
65MCJIT::~MCJIT() {
Andrew Kaylor61694532013-10-21 17:42:06 +000066 MutexGuard locked(lock);
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +000067 // FIXME: We are managing our modules, so we do not want the base class
68 // ExecutionEngine to manage them as well. To avoid double destruction
69 // of the first (and only) module added in ExecutionEngine constructor
70 // we remove it from EE and will destruct it ourselves.
71 //
72 // It may make sense to move our module manager (based on SmallStPtr) back
73 // into EE if the JIT and Interpreter can live with it.
74 // If so, additional functions: addModule, removeModule, FindFunctionNamed,
75 // runStaticConstructorsDestructors could be moved back to EE as well.
76 //
77 Modules.clear();
Andrew Kaylor43507d02013-10-16 00:14:21 +000078 Dyld.deregisterEHFrames();
Chandler Carruth81798732013-10-24 09:52:56 +000079
80 LoadedObjectMap::iterator it, end = LoadedObjects.end();
81 for (it = LoadedObjects.begin(); it != end; ++it) {
82 ObjectImage *Obj = it->second;
83 if (Obj) {
84 NotifyFreeingObject(*Obj);
85 delete Obj;
86 }
87 }
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000088 LoadedObjects.clear();
Andrew Kaylorea708d12012-08-07 18:33:00 +000089 delete TM;
90}
91
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000092void MCJIT::addModule(Module *M) {
Andrew Kaylor61694532013-10-21 17:42:06 +000093 MutexGuard locked(lock);
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +000094 OwnedModules.addModule(M);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +000095}
96
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +000097bool MCJIT::removeModule(Module *M) {
98 MutexGuard locked(lock);
99 return OwnedModules.removeModule(M);
100}
101
102
103
Andrew Kaylor1c489452013-04-25 21:02:36 +0000104void MCJIT::setObjectCache(ObjectCache* NewCache) {
Andrew Kaylor61694532013-10-21 17:42:06 +0000105 MutexGuard locked(lock);
Andrew Kaylor1c489452013-04-25 21:02:36 +0000106 ObjCache = NewCache;
107}
108
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000109ObjectBufferStream* MCJIT::emitObject(Module *M) {
Andrew Kaylor61694532013-10-21 17:42:06 +0000110 MutexGuard locked(lock);
111
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000112 // This must be a module which has already been added but not loaded to this
113 // MCJIT instance, since these conditions are tested by our caller,
114 // generateCodeForModule.
Andrew Kaylorea708d12012-08-07 18:33:00 +0000115
116 PassManager PM;
117
Micah Villmow3574eca2012-10-08 16:38:25 +0000118 PM.add(new DataLayout(*TM->getDataLayout()));
Jim Grosbach31649e62011-03-18 22:48:41 +0000119
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000120 // The RuntimeDyld will take ownership of this shortly
Andrew Kaylor1c489452013-04-25 21:02:36 +0000121 OwningPtr<ObjectBufferStream> CompiledObject(new ObjectBufferStream());
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000122
Jim Grosbach31649e62011-03-18 22:48:41 +0000123 // Turn the machine code intermediate representation into bytes in memory
124 // that may be executed.
Andrew Kaylor1c489452013-04-25 21:02:36 +0000125 if (TM->addPassesToEmitMC(PM, Ctx, CompiledObject->getOStream(), false)) {
Jim Grosbach31649e62011-03-18 22:48:41 +0000126 report_fatal_error("Target does not support MC emission!");
127 }
128
129 // Initialize passes.
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000130 PM.run(*M);
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000131 // Flush the output buffer to get the generated code into memory
Andrew Kaylor1c489452013-04-25 21:02:36 +0000132 CompiledObject->flush();
133
134 // If we have an object cache, tell it about the new object.
135 // Note that we're using the compiled image, not the loaded image (as below).
136 if (ObjCache) {
137 // MemoryBuffer is a thin wrapper around the actual memory, so it's OK
138 // to create a temporary object here and delete it after the call.
139 OwningPtr<MemoryBuffer> MB(CompiledObject->getMemBuffer());
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000140 ObjCache->notifyObjectCompiled(M, MB.get());
Andrew Kaylor1c489452013-04-25 21:02:36 +0000141 }
142
143 return CompiledObject.take();
144}
145
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000146void MCJIT::generateCodeForModule(Module *M) {
Andrew Kaylor61694532013-10-21 17:42:06 +0000147 // Get a thread lock to make sure we aren't trying to load multiple times
148 MutexGuard locked(lock);
149
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000150 // This must be a module which has already been added to this MCJIT instance.
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000151 assert(OwnedModules.ownsModule(M) &&
152 "MCJIT::generateCodeForModule: Unknown module.");
Andrew Kaylor1c489452013-04-25 21:02:36 +0000153
Andrew Kaylor1c489452013-04-25 21:02:36 +0000154 // Re-compilation is not supported
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000155 if (OwnedModules.hasModuleBeenLoaded(M))
Andrew Kaylor1c489452013-04-25 21:02:36 +0000156 return;
157
158 OwningPtr<ObjectBuffer> ObjectToLoad;
159 // Try to load the pre-compiled object from cache if possible
160 if (0 != ObjCache) {
Andrew Kaylor40d81712013-06-28 21:40:16 +0000161 OwningPtr<MemoryBuffer> PreCompiledObject(ObjCache->getObject(M));
Andrew Kaylor1c489452013-04-25 21:02:36 +0000162 if (0 != PreCompiledObject.get())
163 ObjectToLoad.reset(new ObjectBuffer(PreCompiledObject.take()));
164 }
165
166 // If the cache did not contain a suitable object, compile the object
167 if (!ObjectToLoad) {
168 ObjectToLoad.reset(emitObject(M));
169 assert(ObjectToLoad.get() && "Compilation did not produce an object.");
170 }
Jim Grosbachf9229102011-03-22 01:06:42 +0000171
172 // Load the object into the dynamic linker.
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000173 // MCJIT now owns the ObjectImage pointer (via its LoadedObjects map).
174 ObjectImage *LoadedObject = Dyld.loadObject(ObjectToLoad.take());
175 LoadedObjects[M] = LoadedObject;
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000176 if (!LoadedObject)
Jim Grosbach8086f3b2011-03-23 19:51:34 +0000177 report_fatal_error(Dyld.getErrorString());
Andrew Kaylorea708d12012-08-07 18:33:00 +0000178
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000179 // FIXME: Make this optional, maybe even move it to a JIT event listener
180 LoadedObject->registerWithDebugger();
181
Andrew Kaylor776054d2012-11-06 18:51:59 +0000182 NotifyObjectEmitted(*LoadedObject);
183
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000184 OwnedModules.markModuleAsLoaded(M);
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000185}
186
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000187void MCJIT::finalizeLoadedModules() {
Andrew Kaylor61694532013-10-21 17:42:06 +0000188 MutexGuard locked(lock);
189
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000190 // Resolve any outstanding relocations.
191 Dyld.resolveRelocations();
192
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000193 OwnedModules.markAllLoadedModulesAsFinalized();
194
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000195 // Register EH frame data for any module we own which has been loaded
Andrew Kaylor528f6d72013-10-11 21:25:48 +0000196 Dyld.registerEHFrames();
197
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000198 // Set page permissions.
199 MemMgr.finalizeMemory();
200}
201
202// FIXME: Rename this.
203void MCJIT::finalizeObject() {
Andrew Kaylor61694532013-10-21 17:42:06 +0000204 MutexGuard locked(lock);
205
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000206 for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
207 E = OwnedModules.end_added();
208 I != E; ++I) {
209 Module *M = *I;
210 generateCodeForModule(M);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000211 }
Andrew Kaylor53608a32012-11-15 23:50:01 +0000212
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000213 finalizeLoadedModules();
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000214}
215
216void MCJIT::finalizeModule(Module *M) {
Andrew Kaylor61694532013-10-21 17:42:06 +0000217 MutexGuard locked(lock);
218
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000219 // This must be a module which has already been added to this MCJIT instance.
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000220 assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module.");
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000221
222 // If the module hasn't been compiled, just do that.
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000223 if (!OwnedModules.hasModuleBeenLoaded(M))
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000224 generateCodeForModule(M);
225
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000226 finalizeLoadedModules();
Andrew Kaylor28989882012-11-05 20:57:16 +0000227}
228
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000229void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) {
230 report_fatal_error("not yet implemented");
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000231}
232
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000233uint64_t MCJIT::getExistingSymbolAddress(const std::string &Name) {
234 // Check with the RuntimeDyld to see if we already have this symbol.
235 if (Name[0] == '\1')
236 return Dyld.getSymbolLoadAddress(Name.substr(1));
237 return Dyld.getSymbolLoadAddress((TM->getMCAsmInfo()->getGlobalPrefix()
238 + Name));
239}
Jim Grosbach35ed8422012-09-05 16:50:40 +0000240
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000241Module *MCJIT::findModuleForSymbol(const std::string &Name,
242 bool CheckFunctionsOnly) {
Andrew Kaylor61694532013-10-21 17:42:06 +0000243 MutexGuard locked(lock);
244
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000245 // If it hasn't already been generated, see if it's in one of our modules.
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000246 for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
247 E = OwnedModules.end_added();
248 I != E; ++I) {
249 Module *M = *I;
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000250 Function *F = M->getFunction(Name);
251 if (F && !F->empty())
252 return M;
253 if (!CheckFunctionsOnly) {
254 GlobalVariable *G = M->getGlobalVariable(Name);
255 if (G)
256 return M;
257 // FIXME: Do we need to worry about global aliases?
258 }
259 }
260 // We didn't find the symbol in any of our modules.
261 return NULL;
262}
263
264uint64_t MCJIT::getSymbolAddress(const std::string &Name,
265 bool CheckFunctionsOnly)
266{
Andrew Kaylor61694532013-10-21 17:42:06 +0000267 MutexGuard locked(lock);
268
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000269 // First, check to see if we already have this symbol.
270 uint64_t Addr = getExistingSymbolAddress(Name);
271 if (Addr)
272 return Addr;
273
274 // If it hasn't already been generated, see if it's in one of our modules.
275 Module *M = findModuleForSymbol(Name, CheckFunctionsOnly);
276 if (!M)
277 return 0;
278
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000279 generateCodeForModule(M);
280
281 // Check the RuntimeDyld table again, it should be there now.
282 return getExistingSymbolAddress(Name);
283}
284
285uint64_t MCJIT::getGlobalValueAddress(const std::string &Name) {
Andrew Kaylor61694532013-10-21 17:42:06 +0000286 MutexGuard locked(lock);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000287 uint64_t Result = getSymbolAddress(Name, false);
288 if (Result != 0)
289 finalizeLoadedModules();
290 return Result;
291}
292
293uint64_t MCJIT::getFunctionAddress(const std::string &Name) {
Andrew Kaylor61694532013-10-21 17:42:06 +0000294 MutexGuard locked(lock);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000295 uint64_t Result = getSymbolAddress(Name, true);
296 if (Result != 0)
297 finalizeLoadedModules();
298 return Result;
299}
300
301// Deprecated. Use getFunctionAddress instead.
302void *MCJIT::getPointerToFunction(Function *F) {
Andrew Kaylor61694532013-10-21 17:42:06 +0000303 MutexGuard locked(lock);
Andrew Kaylorea708d12012-08-07 18:33:00 +0000304
Jim Grosbach34714a02011-03-22 18:05:27 +0000305 if (F->isDeclaration() || F->hasAvailableExternallyLinkage()) {
306 bool AbortOnFailure = !F->hasExternalWeakLinkage();
307 void *Addr = getPointerToNamedFunction(F->getName(), AbortOnFailure);
308 addGlobalMapping(F, Addr);
309 return Addr;
310 }
311
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000312 Module *M = F->getParent();
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000313 bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000314
315 // Make sure the relevant module has been compiled and loaded.
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000316 if (HasBeenAddedButNotLoaded)
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000317 generateCodeForModule(M);
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000318 else if (!OwnedModules.hasModuleBeenLoaded(M))
319 // If this function doesn't belong to one of our modules, we're done.
320 return NULL;
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000321
Andrew Kaylorea708d12012-08-07 18:33:00 +0000322 // FIXME: Should the Dyld be retaining module information? Probably not.
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +0000323 // FIXME: Should we be using the mangler for this? Probably.
Jim Grosbach35ed8422012-09-05 16:50:40 +0000324 //
325 // This is the accessor for the target address, so make sure to check the
326 // load address of the symbol, not the local address.
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +0000327 StringRef BaseName = F->getName();
328 if (BaseName[0] == '\1')
Jim Grosbach35ed8422012-09-05 16:50:40 +0000329 return (void*)Dyld.getSymbolLoadAddress(BaseName.substr(1));
330 return (void*)Dyld.getSymbolLoadAddress((TM->getMCAsmInfo()->getGlobalPrefix()
Jim Grosbachc0ceedb2011-05-19 00:45:05 +0000331 + BaseName).str());
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000332}
333
334void *MCJIT::recompileAndRelinkFunction(Function *F) {
335 report_fatal_error("not yet implemented");
336}
337
338void MCJIT::freeMachineCodeForFunction(Function *F) {
339 report_fatal_error("not yet implemented");
340}
341
Andrew Kaylor2ad18ef2013-10-24 00:19:14 +0000342void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
343 bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) {
344 for (; I != E; ++I) {
345 ExecutionEngine::runStaticConstructorsDestructors(*I, isDtors);
346 }
347}
348
349void MCJIT::runStaticConstructorsDestructors(bool isDtors) {
350 // Execute global ctors/dtors for each module in the program.
351 runStaticConstructorsDestructorsInModulePtrSet(
352 isDtors, OwnedModules.begin_added(), OwnedModules.end_added());
353 runStaticConstructorsDestructorsInModulePtrSet(
354 isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded());
355 runStaticConstructorsDestructorsInModulePtrSet(
356 isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized());
357}
358
359Function *MCJIT::FindFunctionNamedInModulePtrSet(const char *FnName,
360 ModulePtrSet::iterator I,
361 ModulePtrSet::iterator E) {
362 for (; I != E; ++I) {
363 if (Function *F = (*I)->getFunction(FnName))
364 return F;
365 }
366 return 0;
367}
368
369Function *MCJIT::FindFunctionNamed(const char *FnName) {
370 Function *F = FindFunctionNamedInModulePtrSet(
371 FnName, OwnedModules.begin_added(), OwnedModules.end_added());
372 if (!F)
373 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(),
374 OwnedModules.end_loaded());
375 if (!F)
376 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(),
377 OwnedModules.end_finalized());
378 return F;
379}
380
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000381GenericValue MCJIT::runFunction(Function *F,
382 const std::vector<GenericValue> &ArgValues) {
Jim Grosbach34714a02011-03-22 18:05:27 +0000383 assert(F && "Function *F was null at entry to run()");
384
Jim Grosbach31649e62011-03-18 22:48:41 +0000385 void *FPtr = getPointerToFunction(F);
Jim Grosbach34714a02011-03-22 18:05:27 +0000386 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000387 FunctionType *FTy = F->getFunctionType();
388 Type *RetTy = FTy->getReturnType();
Jim Grosbach34714a02011-03-22 18:05:27 +0000389
390 assert((FTy->getNumParams() == ArgValues.size() ||
391 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
392 "Wrong number of arguments passed into function!");
393 assert(FTy->getNumParams() == ArgValues.size() &&
394 "This doesn't support passing arguments through varargs (yet)!");
395
396 // Handle some common cases first. These cases correspond to common `main'
397 // prototypes.
398 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
399 switch (ArgValues.size()) {
400 case 3:
401 if (FTy->getParamType(0)->isIntegerTy(32) &&
402 FTy->getParamType(1)->isPointerTy() &&
403 FTy->getParamType(2)->isPointerTy()) {
404 int (*PF)(int, char **, const char **) =
405 (int(*)(int, char **, const char **))(intptr_t)FPtr;
406
407 // Call the function.
408 GenericValue rv;
409 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
410 (char **)GVTOP(ArgValues[1]),
411 (const char **)GVTOP(ArgValues[2])));
412 return rv;
413 }
414 break;
415 case 2:
416 if (FTy->getParamType(0)->isIntegerTy(32) &&
417 FTy->getParamType(1)->isPointerTy()) {
418 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
419
420 // Call the function.
421 GenericValue rv;
422 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
423 (char **)GVTOP(ArgValues[1])));
424 return rv;
425 }
426 break;
427 case 1:
428 if (FTy->getNumParams() == 1 &&
429 FTy->getParamType(0)->isIntegerTy(32)) {
430 GenericValue rv;
431 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
432 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
433 return rv;
434 }
435 break;
436 }
437 }
438
439 // Handle cases where no arguments are passed first.
440 if (ArgValues.empty()) {
441 GenericValue rv;
442 switch (RetTy->getTypeID()) {
443 default: llvm_unreachable("Unknown return type for function call!");
444 case Type::IntegerTyID: {
445 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
446 if (BitWidth == 1)
447 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
448 else if (BitWidth <= 8)
449 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
450 else if (BitWidth <= 16)
451 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
452 else if (BitWidth <= 32)
453 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
454 else if (BitWidth <= 64)
455 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
456 else
457 llvm_unreachable("Integer types > 64 bits not supported");
458 return rv;
459 }
460 case Type::VoidTyID:
461 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
462 return rv;
463 case Type::FloatTyID:
464 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
465 return rv;
466 case Type::DoubleTyID:
467 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
468 return rv;
469 case Type::X86_FP80TyID:
470 case Type::FP128TyID:
471 case Type::PPC_FP128TyID:
472 llvm_unreachable("long double not supported yet");
Jim Grosbach34714a02011-03-22 18:05:27 +0000473 case Type::PointerTyID:
474 return PTOGV(((void*(*)())(intptr_t)FPtr)());
475 }
476 }
477
Craig Topper85814382012-02-07 05:05:23 +0000478 llvm_unreachable("Full-featured argument passing not supported yet!");
Daniel Dunbar6aec2982010-11-17 16:06:43 +0000479}
Danil Malyshev30b9e322012-03-28 21:46:36 +0000480
481void *MCJIT::getPointerToNamedFunction(const std::string &Name,
Eli Bendersky5fe01982012-04-29 12:40:47 +0000482 bool AbortOnFailure) {
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000483 if (!isSymbolSearchingDisabled()) {
484 void *ptr = MemMgr.getPointerToNamedFunction(Name, false);
Danil Malyshev30b9e322012-03-28 21:46:36 +0000485 if (ptr)
486 return ptr;
487 }
488
489 /// If a LazyFunctionCreator is installed, use it to get/create the function.
490 if (LazyFunctionCreator)
491 if (void *RP = LazyFunctionCreator(Name))
492 return RP;
493
494 if (AbortOnFailure) {
495 report_fatal_error("Program used external function '"+Name+
Eli Bendersky5fe01982012-04-29 12:40:47 +0000496 "' which could not be resolved!");
Danil Malyshev30b9e322012-03-28 21:46:36 +0000497 }
498 return 0;
499}
Andrew Kaylor776054d2012-11-06 18:51:59 +0000500
501void MCJIT::RegisterJITEventListener(JITEventListener *L) {
502 if (L == NULL)
503 return;
504 MutexGuard locked(lock);
505 EventListeners.push_back(L);
506}
507void MCJIT::UnregisterJITEventListener(JITEventListener *L) {
508 if (L == NULL)
509 return;
510 MutexGuard locked(lock);
511 SmallVector<JITEventListener*, 2>::reverse_iterator I=
512 std::find(EventListeners.rbegin(), EventListeners.rend(), L);
513 if (I != EventListeners.rend()) {
514 std::swap(*I, EventListeners.back());
515 EventListeners.pop_back();
516 }
517}
518void MCJIT::NotifyObjectEmitted(const ObjectImage& Obj) {
519 MutexGuard locked(lock);
Andrew Kaylorb868e912013-10-04 00:49:38 +0000520 MemMgr.notifyObjectLoaded(this, &Obj);
Andrew Kaylor776054d2012-11-06 18:51:59 +0000521 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
522 EventListeners[I]->NotifyObjectEmitted(Obj);
523 }
524}
525void MCJIT::NotifyFreeingObject(const ObjectImage& Obj) {
526 MutexGuard locked(lock);
527 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
528 EventListeners[I]->NotifyFreeingObject(Obj);
529 }
530}
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000531
532uint64_t LinkingMemoryManager::getSymbolAddress(const std::string &Name) {
533 uint64_t Result = ParentEngine->getSymbolAddress(Name, false);
Andrew Kaylor52c90162013-10-01 16:42:50 +0000534 // If the symbols wasn't found and it begins with an underscore, try again
535 // without the underscore.
536 if (!Result && Name[0] == '_')
537 Result = ParentEngine->getSymbolAddress(Name.substr(1), false);
Andrew Kaylor8e9ec012013-10-01 01:47:35 +0000538 if (Result)
539 return Result;
540 return ClientMM->getSymbolAddress(Name);
541}