blob: 5841d918de4add99f6f3a3385757dcda74e72586 [file] [log] [blame]
Eric Christopher5c896f72011-04-22 03:07:06 +00001//===-- MCJIT.cpp - MC-based Just-in-Time Compiler ------------------------===//
Daniel Dunbar7e5d8a72010-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 Kaylord8ffd9c2012-11-06 18:51:59 +000012#include "llvm/ExecutionEngine/JITEventListener.h"
Jim Grosbach348a5482011-03-22 01:06:42 +000013#include "llvm/ExecutionEngine/JITMemoryManager.h"
Andrew Kayloradc70562012-10-02 21:18:39 +000014#include "llvm/ExecutionEngine/MCJIT.h"
15#include "llvm/ExecutionEngine/ObjectBuffer.h"
16#include "llvm/ExecutionEngine/ObjectImage.h"
Andrew Kaylorc89fc822013-10-24 00:19:14 +000017#include "llvm/PassManager.h"
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000018#include "llvm/ExecutionEngine/SectionMemoryManager.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/DataLayout.h"
20#include "llvm/IR/DerivedTypes.h"
21#include "llvm/IR/Function.h"
Andrew Kaylorea395922013-10-01 01:47:35 +000022#include "llvm/IR/Module.h"
Jim Grosbach348a5482011-03-22 01:06:42 +000023#include "llvm/MC/MCAsmInfo.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000024#include "llvm/Support/DynamicLibrary.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/ErrorHandling.h"
Jim Grosbach348a5482011-03-22 01:06:42 +000026#include "llvm/Support/MemoryBuffer.h"
Andrew Kaylor1a568c32012-08-07 18:33:00 +000027#include "llvm/Support/MutexGuard.h"
Rafael Espindola3e3a3f12013-11-28 08:59:52 +000028#include "llvm/Target/Mangler.h"
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000029
30using namespace llvm;
31
32namespace {
33
34static struct RegisterJIT {
35 RegisterJIT() { MCJIT::Register(); }
36} JITRegistrator;
37
38}
39
40extern "C" void LLVMLinkInMCJIT() {
41}
42
43ExecutionEngine *MCJIT::createJIT(Module *M,
44 std::string *ErrorStr,
Filip Pizlo9bc53e82013-05-14 19:29:00 +000045 RTDyldMemoryManager *MemMgr,
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000046 bool GVsWithCode,
Dylan Noblesmith8418fdc2011-05-13 21:51:29 +000047 TargetMachine *TM) {
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000048 // Try to register the program as a source of symbols to resolve against.
49 //
50 // FIXME: Don't do this here.
51 sys::DynamicLibrary::LoadLibraryPermanently(0, NULL);
52
Filip Pizlo9bc53e82013-05-14 19:29:00 +000053 return new MCJIT(M, TM, MemMgr ? MemMgr : new SectionMemoryManager(),
54 GVsWithCode);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +000055}
56
Jim Grosbachbea67532012-08-21 15:42:49 +000057MCJIT::MCJIT(Module *m, TargetMachine *tm, RTDyldMemoryManager *MM,
58 bool AllocateGVsWithCode)
Andrew Kaylorea395922013-10-01 01:47:35 +000059 : ExecutionEngine(m), TM(tm), Ctx(0), MemMgr(this, MM), Dyld(&MemMgr),
60 ObjCache(0) {
Jim Grosbach7b162492011-03-18 22:48:41 +000061
Andrew Kaylorc89fc822013-10-24 00:19:14 +000062 OwnedModules.addModule(m);
Micah Villmowcdfe20b2012-10-08 16:38:25 +000063 setDataLayout(TM->getDataLayout());
Andrew Kaylor1a568c32012-08-07 18:33:00 +000064}
65
66MCJIT::~MCJIT() {
Andrew Kaylor4fba0492013-10-21 17:42:06 +000067 MutexGuard locked(lock);
Andrew Kaylorc89fc822013-10-24 00:19:14 +000068 // FIXME: We are managing our modules, so we do not want the base class
69 // ExecutionEngine to manage them as well. To avoid double destruction
70 // of the first (and only) module added in ExecutionEngine constructor
71 // we remove it from EE and will destruct it ourselves.
72 //
73 // It may make sense to move our module manager (based on SmallStPtr) back
74 // into EE if the JIT and Interpreter can live with it.
75 // If so, additional functions: addModule, removeModule, FindFunctionNamed,
76 // runStaticConstructorsDestructors could be moved back to EE as well.
77 //
78 Modules.clear();
Andrew Kaylorc442a762013-10-16 00:14:21 +000079 Dyld.deregisterEHFrames();
Chandler Carruthd55d1592013-10-24 09:52:56 +000080
81 LoadedObjectMap::iterator it, end = LoadedObjects.end();
82 for (it = LoadedObjects.begin(); it != end; ++it) {
83 ObjectImage *Obj = it->second;
84 if (Obj) {
85 NotifyFreeingObject(*Obj);
86 delete Obj;
87 }
88 }
Andrew Kaylorea395922013-10-01 01:47:35 +000089 LoadedObjects.clear();
Andrew Kaylor1a568c32012-08-07 18:33:00 +000090 delete TM;
91}
92
Andrew Kaylorea395922013-10-01 01:47:35 +000093void MCJIT::addModule(Module *M) {
Andrew Kaylor4fba0492013-10-21 17:42:06 +000094 MutexGuard locked(lock);
Andrew Kaylorc89fc822013-10-24 00:19:14 +000095 OwnedModules.addModule(M);
Andrew Kaylorea395922013-10-01 01:47:35 +000096}
97
Andrew Kaylorc89fc822013-10-24 00:19:14 +000098bool MCJIT::removeModule(Module *M) {
99 MutexGuard locked(lock);
100 return OwnedModules.removeModule(M);
101}
102
103
104
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000105void MCJIT::setObjectCache(ObjectCache* NewCache) {
Andrew Kaylor4fba0492013-10-21 17:42:06 +0000106 MutexGuard locked(lock);
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000107 ObjCache = NewCache;
108}
109
Andrew Kaylorea395922013-10-01 01:47:35 +0000110ObjectBufferStream* MCJIT::emitObject(Module *M) {
Andrew Kaylor4fba0492013-10-21 17:42:06 +0000111 MutexGuard locked(lock);
112
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000113 // This must be a module which has already been added but not loaded to this
114 // MCJIT instance, since these conditions are tested by our caller,
115 // generateCodeForModule.
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000116
117 PassManager PM;
118
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000119 PM.add(new DataLayout(*TM->getDataLayout()));
Jim Grosbach7b162492011-03-18 22:48:41 +0000120
Andrew Kayloradc70562012-10-02 21:18:39 +0000121 // The RuntimeDyld will take ownership of this shortly
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000122 OwningPtr<ObjectBufferStream> CompiledObject(new ObjectBufferStream());
Andrew Kayloradc70562012-10-02 21:18:39 +0000123
Jim Grosbach7b162492011-03-18 22:48:41 +0000124 // Turn the machine code intermediate representation into bytes in memory
125 // that may be executed.
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000126 if (TM->addPassesToEmitMC(PM, Ctx, CompiledObject->getOStream(), false)) {
Jim Grosbach7b162492011-03-18 22:48:41 +0000127 report_fatal_error("Target does not support MC emission!");
128 }
129
130 // Initialize passes.
Andrew Kaylorea395922013-10-01 01:47:35 +0000131 PM.run(*M);
Andrew Kayloradc70562012-10-02 21:18:39 +0000132 // Flush the output buffer to get the generated code into memory
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000133 CompiledObject->flush();
134
135 // If we have an object cache, tell it about the new object.
136 // Note that we're using the compiled image, not the loaded image (as below).
137 if (ObjCache) {
138 // MemoryBuffer is a thin wrapper around the actual memory, so it's OK
139 // to create a temporary object here and delete it after the call.
140 OwningPtr<MemoryBuffer> MB(CompiledObject->getMemBuffer());
Andrew Kaylorea395922013-10-01 01:47:35 +0000141 ObjCache->notifyObjectCompiled(M, MB.get());
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000142 }
143
144 return CompiledObject.take();
145}
146
Andrew Kaylorea395922013-10-01 01:47:35 +0000147void MCJIT::generateCodeForModule(Module *M) {
Andrew Kaylor4fba0492013-10-21 17:42:06 +0000148 // Get a thread lock to make sure we aren't trying to load multiple times
149 MutexGuard locked(lock);
150
Andrew Kaylorea395922013-10-01 01:47:35 +0000151 // This must be a module which has already been added to this MCJIT instance.
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000152 assert(OwnedModules.ownsModule(M) &&
153 "MCJIT::generateCodeForModule: Unknown module.");
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000154
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000155 // Re-compilation is not supported
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000156 if (OwnedModules.hasModuleBeenLoaded(M))
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000157 return;
158
159 OwningPtr<ObjectBuffer> ObjectToLoad;
160 // Try to load the pre-compiled object from cache if possible
161 if (0 != ObjCache) {
Andrew Kaylorb595f532013-06-28 21:40:16 +0000162 OwningPtr<MemoryBuffer> PreCompiledObject(ObjCache->getObject(M));
Andrew Kaylorced4e8f2013-04-25 21:02:36 +0000163 if (0 != PreCompiledObject.get())
164 ObjectToLoad.reset(new ObjectBuffer(PreCompiledObject.take()));
165 }
166
167 // If the cache did not contain a suitable object, compile the object
168 if (!ObjectToLoad) {
169 ObjectToLoad.reset(emitObject(M));
170 assert(ObjectToLoad.get() && "Compilation did not produce an object.");
171 }
Jim Grosbach348a5482011-03-22 01:06:42 +0000172
173 // Load the object into the dynamic linker.
Andrew Kaylorea395922013-10-01 01:47:35 +0000174 // MCJIT now owns the ObjectImage pointer (via its LoadedObjects map).
175 ObjectImage *LoadedObject = Dyld.loadObject(ObjectToLoad.take());
176 LoadedObjects[M] = LoadedObject;
Andrew Kayloradc70562012-10-02 21:18:39 +0000177 if (!LoadedObject)
Jim Grosbachc114d892011-03-23 19:51:34 +0000178 report_fatal_error(Dyld.getErrorString());
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000179
Andrew Kayloradc70562012-10-02 21:18:39 +0000180 // FIXME: Make this optional, maybe even move it to a JIT event listener
181 LoadedObject->registerWithDebugger();
182
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +0000183 NotifyObjectEmitted(*LoadedObject);
184
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000185 OwnedModules.markModuleAsLoaded(M);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000186}
187
Andrew Kaylorea395922013-10-01 01:47:35 +0000188void MCJIT::finalizeLoadedModules() {
Andrew Kaylor4fba0492013-10-21 17:42:06 +0000189 MutexGuard locked(lock);
190
Andrew Kaylorea395922013-10-01 01:47:35 +0000191 // Resolve any outstanding relocations.
192 Dyld.resolveRelocations();
193
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000194 OwnedModules.markAllLoadedModulesAsFinalized();
195
Andrew Kaylorea395922013-10-01 01:47:35 +0000196 // Register EH frame data for any module we own which has been loaded
Andrew Kaylor7bb13442013-10-11 21:25:48 +0000197 Dyld.registerEHFrames();
198
Andrew Kaylorea395922013-10-01 01:47:35 +0000199 // Set page permissions.
200 MemMgr.finalizeMemory();
201}
202
203// FIXME: Rename this.
204void MCJIT::finalizeObject() {
Andrew Kaylor4fba0492013-10-21 17:42:06 +0000205 MutexGuard locked(lock);
206
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000207 for (ModulePtrSet::iterator I = OwnedModules.begin_added(),
208 E = OwnedModules.end_added();
209 I != E; ++I) {
210 Module *M = *I;
211 generateCodeForModule(M);
Andrew Kaylorea395922013-10-01 01:47:35 +0000212 }
Andrew Kaylora342cb92012-11-15 23:50:01 +0000213
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000214 finalizeLoadedModules();
Andrew Kaylorea395922013-10-01 01:47:35 +0000215}
216
217void MCJIT::finalizeModule(Module *M) {
Andrew Kaylor4fba0492013-10-21 17:42:06 +0000218 MutexGuard locked(lock);
219
Andrew Kaylorea395922013-10-01 01:47:35 +0000220 // This must be a module which has already been added to this MCJIT instance.
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000221 assert(OwnedModules.ownsModule(M) && "MCJIT::finalizeModule: Unknown module.");
Andrew Kaylorea395922013-10-01 01:47:35 +0000222
223 // If the module hasn't been compiled, just do that.
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000224 if (!OwnedModules.hasModuleBeenLoaded(M))
Andrew Kaylorea395922013-10-01 01:47:35 +0000225 generateCodeForModule(M);
226
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000227 finalizeLoadedModules();
Andrew Kaylora714efc2012-11-05 20:57:16 +0000228}
229
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000230void *MCJIT::getPointerToBasicBlock(BasicBlock *BB) {
231 report_fatal_error("not yet implemented");
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000232}
233
Andrew Kaylorea395922013-10-01 01:47:35 +0000234uint64_t MCJIT::getExistingSymbolAddress(const std::string &Name) {
Rafael Espindola3e3a3f12013-11-28 08:59:52 +0000235 Mangler Mang(TM);
236 SmallString<128> FullName;
237 Mang.getNameWithPrefix(FullName, Name);
238 return Dyld.getSymbolLoadAddress(FullName);
Andrew Kaylorea395922013-10-01 01:47:35 +0000239}
Jim Grosbachdc1123f2012-09-05 16:50:40 +0000240
Andrew Kaylorea395922013-10-01 01:47:35 +0000241Module *MCJIT::findModuleForSymbol(const std::string &Name,
242 bool CheckFunctionsOnly) {
Andrew Kaylor4fba0492013-10-21 17:42:06 +0000243 MutexGuard locked(lock);
244
Andrew Kaylorea395922013-10-01 01:47:35 +0000245 // If it hasn't already been generated, see if it's in one of our modules.
Andrew Kaylorc89fc822013-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 Kaylorea395922013-10-01 01:47:35 +0000250 Function *F = M->getFunction(Name);
Andrew Kaylor515b1da2013-11-15 22:10:21 +0000251 if (F && !F->isDeclaration())
Andrew Kaylorea395922013-10-01 01:47:35 +0000252 return M;
253 if (!CheckFunctionsOnly) {
254 GlobalVariable *G = M->getGlobalVariable(Name);
Andrew Kaylor515b1da2013-11-15 22:10:21 +0000255 if (G && !G->isDeclaration())
Andrew Kaylorea395922013-10-01 01:47:35 +0000256 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 Kaylor4fba0492013-10-21 17:42:06 +0000267 MutexGuard locked(lock);
268
Andrew Kaylorea395922013-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 Kaylorea395922013-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 Kaylor4fba0492013-10-21 17:42:06 +0000286 MutexGuard locked(lock);
Andrew Kaylorea395922013-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 Kaylor4fba0492013-10-21 17:42:06 +0000294 MutexGuard locked(lock);
Andrew Kaylorea395922013-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 Kaylor4fba0492013-10-21 17:42:06 +0000303 MutexGuard locked(lock);
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000304
Jim Grosbachd5274402011-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 Kaylorea395922013-10-01 01:47:35 +0000312 Module *M = F->getParent();
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000313 bool HasBeenAddedButNotLoaded = OwnedModules.hasModuleBeenAddedButNotLoaded(M);
Andrew Kaylorea395922013-10-01 01:47:35 +0000314
315 // Make sure the relevant module has been compiled and loaded.
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000316 if (HasBeenAddedButNotLoaded)
Andrew Kaylorea395922013-10-01 01:47:35 +0000317 generateCodeForModule(M);
Andrew Kaylorc89fc822013-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 Kaylorea395922013-10-01 01:47:35 +0000321
Andrew Kaylor1a568c32012-08-07 18:33:00 +0000322 // FIXME: Should the Dyld be retaining module information? Probably not.
Jim Grosbachdc1123f2012-09-05 16:50:40 +0000323 //
324 // This is the accessor for the target address, so make sure to check the
325 // load address of the symbol, not the local address.
Rafael Espindola3e3a3f12013-11-28 08:59:52 +0000326 Mangler Mang(TM);
327 SmallString<128> Name;
328 Mang.getNameWithPrefix(Name, F, false);
329 return (void*)Dyld.getSymbolLoadAddress(Name);
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000330}
331
332void *MCJIT::recompileAndRelinkFunction(Function *F) {
333 report_fatal_error("not yet implemented");
334}
335
336void MCJIT::freeMachineCodeForFunction(Function *F) {
337 report_fatal_error("not yet implemented");
338}
339
Andrew Kaylorc89fc822013-10-24 00:19:14 +0000340void MCJIT::runStaticConstructorsDestructorsInModulePtrSet(
341 bool isDtors, ModulePtrSet::iterator I, ModulePtrSet::iterator E) {
342 for (; I != E; ++I) {
343 ExecutionEngine::runStaticConstructorsDestructors(*I, isDtors);
344 }
345}
346
347void MCJIT::runStaticConstructorsDestructors(bool isDtors) {
348 // Execute global ctors/dtors for each module in the program.
349 runStaticConstructorsDestructorsInModulePtrSet(
350 isDtors, OwnedModules.begin_added(), OwnedModules.end_added());
351 runStaticConstructorsDestructorsInModulePtrSet(
352 isDtors, OwnedModules.begin_loaded(), OwnedModules.end_loaded());
353 runStaticConstructorsDestructorsInModulePtrSet(
354 isDtors, OwnedModules.begin_finalized(), OwnedModules.end_finalized());
355}
356
357Function *MCJIT::FindFunctionNamedInModulePtrSet(const char *FnName,
358 ModulePtrSet::iterator I,
359 ModulePtrSet::iterator E) {
360 for (; I != E; ++I) {
361 if (Function *F = (*I)->getFunction(FnName))
362 return F;
363 }
364 return 0;
365}
366
367Function *MCJIT::FindFunctionNamed(const char *FnName) {
368 Function *F = FindFunctionNamedInModulePtrSet(
369 FnName, OwnedModules.begin_added(), OwnedModules.end_added());
370 if (!F)
371 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_loaded(),
372 OwnedModules.end_loaded());
373 if (!F)
374 F = FindFunctionNamedInModulePtrSet(FnName, OwnedModules.begin_finalized(),
375 OwnedModules.end_finalized());
376 return F;
377}
378
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000379GenericValue MCJIT::runFunction(Function *F,
380 const std::vector<GenericValue> &ArgValues) {
Jim Grosbachd5274402011-03-22 18:05:27 +0000381 assert(F && "Function *F was null at entry to run()");
382
Jim Grosbach7b162492011-03-18 22:48:41 +0000383 void *FPtr = getPointerToFunction(F);
Jim Grosbachd5274402011-03-22 18:05:27 +0000384 assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
Chris Lattner229907c2011-07-18 04:54:35 +0000385 FunctionType *FTy = F->getFunctionType();
386 Type *RetTy = FTy->getReturnType();
Jim Grosbachd5274402011-03-22 18:05:27 +0000387
388 assert((FTy->getNumParams() == ArgValues.size() ||
389 (FTy->isVarArg() && FTy->getNumParams() <= ArgValues.size())) &&
390 "Wrong number of arguments passed into function!");
391 assert(FTy->getNumParams() == ArgValues.size() &&
392 "This doesn't support passing arguments through varargs (yet)!");
393
394 // Handle some common cases first. These cases correspond to common `main'
395 // prototypes.
396 if (RetTy->isIntegerTy(32) || RetTy->isVoidTy()) {
397 switch (ArgValues.size()) {
398 case 3:
399 if (FTy->getParamType(0)->isIntegerTy(32) &&
400 FTy->getParamType(1)->isPointerTy() &&
401 FTy->getParamType(2)->isPointerTy()) {
402 int (*PF)(int, char **, const char **) =
403 (int(*)(int, char **, const char **))(intptr_t)FPtr;
404
405 // Call the function.
406 GenericValue rv;
407 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
408 (char **)GVTOP(ArgValues[1]),
409 (const char **)GVTOP(ArgValues[2])));
410 return rv;
411 }
412 break;
413 case 2:
414 if (FTy->getParamType(0)->isIntegerTy(32) &&
415 FTy->getParamType(1)->isPointerTy()) {
416 int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
417
418 // Call the function.
419 GenericValue rv;
420 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
421 (char **)GVTOP(ArgValues[1])));
422 return rv;
423 }
424 break;
425 case 1:
426 if (FTy->getNumParams() == 1 &&
427 FTy->getParamType(0)->isIntegerTy(32)) {
428 GenericValue rv;
429 int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
430 rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
431 return rv;
432 }
433 break;
434 }
435 }
436
437 // Handle cases where no arguments are passed first.
438 if (ArgValues.empty()) {
439 GenericValue rv;
440 switch (RetTy->getTypeID()) {
441 default: llvm_unreachable("Unknown return type for function call!");
442 case Type::IntegerTyID: {
443 unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
444 if (BitWidth == 1)
445 rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
446 else if (BitWidth <= 8)
447 rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
448 else if (BitWidth <= 16)
449 rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
450 else if (BitWidth <= 32)
451 rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
452 else if (BitWidth <= 64)
453 rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
454 else
455 llvm_unreachable("Integer types > 64 bits not supported");
456 return rv;
457 }
458 case Type::VoidTyID:
459 rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
460 return rv;
461 case Type::FloatTyID:
462 rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
463 return rv;
464 case Type::DoubleTyID:
465 rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
466 return rv;
467 case Type::X86_FP80TyID:
468 case Type::FP128TyID:
469 case Type::PPC_FP128TyID:
470 llvm_unreachable("long double not supported yet");
Jim Grosbachd5274402011-03-22 18:05:27 +0000471 case Type::PointerTyID:
472 return PTOGV(((void*(*)())(intptr_t)FPtr)());
473 }
474 }
475
Craig Toppera2886c22012-02-07 05:05:23 +0000476 llvm_unreachable("Full-featured argument passing not supported yet!");
Daniel Dunbar7e5d8a72010-11-17 16:06:43 +0000477}
Danil Malyshevbfee5422012-03-28 21:46:36 +0000478
479void *MCJIT::getPointerToNamedFunction(const std::string &Name,
Eli Bendersky0e2ac5b2012-04-29 12:40:47 +0000480 bool AbortOnFailure) {
Andrew Kaylorea395922013-10-01 01:47:35 +0000481 if (!isSymbolSearchingDisabled()) {
482 void *ptr = MemMgr.getPointerToNamedFunction(Name, false);
Danil Malyshevbfee5422012-03-28 21:46:36 +0000483 if (ptr)
484 return ptr;
485 }
486
487 /// If a LazyFunctionCreator is installed, use it to get/create the function.
488 if (LazyFunctionCreator)
489 if (void *RP = LazyFunctionCreator(Name))
490 return RP;
491
492 if (AbortOnFailure) {
493 report_fatal_error("Program used external function '"+Name+
Eli Bendersky0e2ac5b2012-04-29 12:40:47 +0000494 "' which could not be resolved!");
Danil Malyshevbfee5422012-03-28 21:46:36 +0000495 }
496 return 0;
497}
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +0000498
499void MCJIT::RegisterJITEventListener(JITEventListener *L) {
500 if (L == NULL)
501 return;
502 MutexGuard locked(lock);
503 EventListeners.push_back(L);
504}
505void MCJIT::UnregisterJITEventListener(JITEventListener *L) {
506 if (L == NULL)
507 return;
508 MutexGuard locked(lock);
509 SmallVector<JITEventListener*, 2>::reverse_iterator I=
510 std::find(EventListeners.rbegin(), EventListeners.rend(), L);
511 if (I != EventListeners.rend()) {
512 std::swap(*I, EventListeners.back());
513 EventListeners.pop_back();
514 }
515}
516void MCJIT::NotifyObjectEmitted(const ObjectImage& Obj) {
517 MutexGuard locked(lock);
Andrew Kaylor1b2cfb62013-10-04 00:49:38 +0000518 MemMgr.notifyObjectLoaded(this, &Obj);
Andrew Kaylord8ffd9c2012-11-06 18:51:59 +0000519 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
520 EventListeners[I]->NotifyObjectEmitted(Obj);
521 }
522}
523void MCJIT::NotifyFreeingObject(const ObjectImage& Obj) {
524 MutexGuard locked(lock);
525 for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
526 EventListeners[I]->NotifyFreeingObject(Obj);
527 }
528}
Andrew Kaylorea395922013-10-01 01:47:35 +0000529
530uint64_t LinkingMemoryManager::getSymbolAddress(const std::string &Name) {
531 uint64_t Result = ParentEngine->getSymbolAddress(Name, false);
Andrew Kaylor89bdd102013-10-01 16:42:50 +0000532 // If the symbols wasn't found and it begins with an underscore, try again
533 // without the underscore.
534 if (!Result && Name[0] == '_')
535 Result = ParentEngine->getSymbolAddress(Name.substr(1), false);
Andrew Kaylorea395922013-10-01 01:47:35 +0000536 if (Result)
537 return Result;
538 return ClientMM->getSymbolAddress(Name);
539}