1 //===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the abstract interface that implements execution support
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_EXECUTION_ENGINE_H
16 #define LLVM_EXECUTION_ENGINE_H
18 #include "llvm/MC/MCCodeGenInfo.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/ValueMap.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/ValueHandle.h"
25 #include "llvm/Support/Mutex.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Target/TargetOptions.h"
36 class ExecutionEngine;
40 class JITEventListener;
41 class JITMemoryManager;
42 class MachineCodeInfo;
49 /// \brief Helper class for helping synchronize access to the global address map
51 class ExecutionEngineState {
53 struct AddressMapConfig : public ValueMapConfig<const GlobalValue*> {
54 typedef ExecutionEngineState *ExtraData;
55 static sys::Mutex *getMutex(ExecutionEngineState *EES);
56 static void onDelete(ExecutionEngineState *EES, const GlobalValue *Old);
57 static void onRAUW(ExecutionEngineState *, const GlobalValue *,
61 typedef ValueMap<const GlobalValue *, void *, AddressMapConfig>
67 /// GlobalAddressMap - A mapping between LLVM global values and their
68 /// actualized version...
69 GlobalAddressMapTy GlobalAddressMap;
71 /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
72 /// used to convert raw addresses into the LLVM global value that is emitted
73 /// at the address. This map is not computed unless getGlobalValueAtAddress
74 /// is called at some point.
75 std::map<void *, AssertingVH<const GlobalValue> > GlobalAddressReverseMap;
78 ExecutionEngineState(ExecutionEngine &EE);
80 GlobalAddressMapTy &getGlobalAddressMap(const MutexGuard &) {
81 return GlobalAddressMap;
84 std::map<void*, AssertingVH<const GlobalValue> > &
85 getGlobalAddressReverseMap(const MutexGuard &) {
86 return GlobalAddressReverseMap;
89 /// \brief Erase an entry from the mapping table.
91 /// \returns The address that \arg ToUnmap was happed to.
92 void *RemoveMapping(const MutexGuard &, const GlobalValue *ToUnmap);
95 /// \brief Abstract interface for implementation execution of LLVM modules,
96 /// designed to support both interpreter and just-in-time (JIT) compiler
98 class ExecutionEngine {
99 /// The state object holding the global address mapping, which must be
100 /// accessed synchronously.
102 // FIXME: There is no particular need the entire map needs to be
103 // synchronized. Wouldn't a reader-writer design be better here?
104 ExecutionEngineState EEState;
106 /// The target data for the platform for which execution is being performed.
107 const TargetData *TD;
109 /// Whether lazy JIT compilation is enabled.
110 bool CompilingLazily;
112 /// Whether JIT compilation of external global variables is allowed.
113 bool GVCompilationDisabled;
115 /// Whether the JIT should perform lookups of external symbols (e.g.,
117 bool SymbolSearchingDisabled;
119 friend class EngineBuilder; // To allow access to JITCtor and InterpCtor.
122 /// The list of Modules that we are JIT'ing from. We use a SmallVector to
123 /// optimize for the case where there is only one module.
124 SmallVector<Module*, 1> Modules;
126 void setTargetData(const TargetData *td) { TD = td; }
128 /// getMemoryforGV - Allocate memory for a global variable.
129 virtual char *getMemoryForGV(const GlobalVariable *GV);
131 // To avoid having libexecutionengine depend on the JIT and interpreter
132 // libraries, the execution engine implementations set these functions to ctor
133 // pointers at startup time if they are linked in.
134 static ExecutionEngine *(*JITCtor)(
136 std::string *ErrorStr,
137 JITMemoryManager *JMM,
140 static ExecutionEngine *(*MCJITCtor)(
142 std::string *ErrorStr,
143 JITMemoryManager *JMM,
146 static ExecutionEngine *(*InterpCtor)(Module *M, std::string *ErrorStr);
148 /// LazyFunctionCreator - If an unknown function is needed, this function
149 /// pointer is invoked to create it. If this returns null, the JIT will
151 void *(*LazyFunctionCreator)(const std::string &);
153 /// ExceptionTableRegister - If Exception Handling is set, the JIT will
154 /// register dwarf tables with this function.
155 typedef void (*EERegisterFn)(void*);
156 EERegisterFn ExceptionTableRegister;
157 EERegisterFn ExceptionTableDeregister;
158 /// This maps functions to their exception tables frames.
159 DenseMap<const Function*, void*> AllExceptionTables;
163 /// lock - This lock protects the ExecutionEngine, JIT, JITResolver and
164 /// JITEmitter classes. It must be held while changing the internal state of
165 /// any of those classes.
168 //===--------------------------------------------------------------------===//
169 // ExecutionEngine Startup
170 //===--------------------------------------------------------------------===//
172 virtual ~ExecutionEngine();
174 /// create - This is the factory method for creating an execution engine which
175 /// is appropriate for the current machine. This takes ownership of the
178 /// \param GVsWithCode - Allocating globals with code breaks
179 /// freeMachineCodeForFunction and is probably unsafe and bad for performance.
180 /// However, we have clients who depend on this behavior, so we must support
181 /// it. Eventually, when we're willing to break some backwards compatibility,
182 /// this flag should be flipped to false, so that by default
183 /// freeMachineCodeForFunction works.
184 static ExecutionEngine *create(Module *M,
185 bool ForceInterpreter = false,
186 std::string *ErrorStr = 0,
187 CodeGenOpt::Level OptLevel =
189 bool GVsWithCode = true);
191 /// createJIT - This is the factory method for creating a JIT for the current
192 /// machine, it does not fall back to the interpreter. This takes ownership
193 /// of the Module and JITMemoryManager if successful.
195 /// Clients should make sure to initialize targets prior to calling this
197 static ExecutionEngine *createJIT(Module *M,
198 std::string *ErrorStr = 0,
199 JITMemoryManager *JMM = 0,
200 CodeGenOpt::Level OptLevel =
202 bool GVsWithCode = true,
203 Reloc::Model RM = Reloc::Default,
204 CodeModel::Model CMM =
205 CodeModel::JITDefault);
207 /// addModule - Add a Module to the list of modules that we can JIT from.
208 /// Note that this takes ownership of the Module: when the ExecutionEngine is
209 /// destroyed, it destroys the Module as well.
210 virtual void addModule(Module *M) {
211 Modules.push_back(M);
214 //===--------------------------------------------------------------------===//
216 const TargetData *getTargetData() const { return TD; }
218 /// removeModule - Remove a Module from the list of modules. Returns true if
220 virtual bool removeModule(Module *M);
222 /// FindFunctionNamed - Search all of the active modules to find the one that
223 /// defines FnName. This is very slow operation and shouldn't be used for
225 Function *FindFunctionNamed(const char *FnName);
227 /// runFunction - Execute the specified function with the specified arguments,
228 /// and return the result.
229 virtual GenericValue runFunction(Function *F,
230 const std::vector<GenericValue> &ArgValues) = 0;
232 /// getPointerToNamedFunction - This method returns the address of the
233 /// specified function by using the dlsym function call. As such it is only
234 /// useful for resolving library symbols, not code generated symbols.
236 /// If AbortOnFailure is false and no function with the given name is
237 /// found, this function silently returns a null pointer. Otherwise,
238 /// it prints a message to stderr and aborts.
240 virtual void *getPointerToNamedFunction(const std::string &Name,
241 bool AbortOnFailure = true) = 0;
243 /// mapSectionAddress - map a section to its target address space value.
244 /// Map the address of a JIT section as returned from the memory manager
245 /// to the address in the target process as the running code will see it.
246 /// This is the address which will be used for relocation resolution.
247 virtual void mapSectionAddress(void *LocalAddress, uint64_t TargetAddress) {
248 llvm_unreachable("Re-mapping of section addresses not supported with this "
252 /// runStaticConstructorsDestructors - This method is used to execute all of
253 /// the static constructors or destructors for a program.
255 /// \param isDtors - Run the destructors instead of constructors.
256 void runStaticConstructorsDestructors(bool isDtors);
258 /// runStaticConstructorsDestructors - This method is used to execute all of
259 /// the static constructors or destructors for a particular module.
261 /// \param isDtors - Run the destructors instead of constructors.
262 void runStaticConstructorsDestructors(Module *module, bool isDtors);
265 /// runFunctionAsMain - This is a helper function which wraps runFunction to
266 /// handle the common task of starting up main with the specified argc, argv,
267 /// and envp parameters.
268 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
269 const char * const * envp);
272 /// addGlobalMapping - Tell the execution engine that the specified global is
273 /// at the specified location. This is used internally as functions are JIT'd
274 /// and as global variables are laid out in memory. It can and should also be
275 /// used by clients of the EE that want to have an LLVM global overlay
276 /// existing data in memory. Mappings are automatically removed when their
277 /// GlobalValue is destroyed.
278 void addGlobalMapping(const GlobalValue *GV, void *Addr);
280 /// clearAllGlobalMappings - Clear all global mappings and start over again,
281 /// for use in dynamic compilation scenarios to move globals.
282 void clearAllGlobalMappings();
284 /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
285 /// particular module, because it has been removed from the JIT.
286 void clearGlobalMappingsFromModule(Module *M);
288 /// updateGlobalMapping - Replace an existing mapping for GV with a new
289 /// address. This updates both maps as required. If "Addr" is null, the
290 /// entry for the global is removed from the mappings. This returns the old
291 /// value of the pointer, or null if it was not in the map.
292 void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
294 /// getPointerToGlobalIfAvailable - This returns the address of the specified
295 /// global value if it is has already been codegen'd, otherwise it returns
297 void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
299 /// getPointerToGlobal - This returns the address of the specified global
300 /// value. This may involve code generation if it's a function.
301 void *getPointerToGlobal(const GlobalValue *GV);
303 /// getPointerToFunction - The different EE's represent function bodies in
304 /// different ways. They should each implement this to say what a function
305 /// pointer should look like. When F is destroyed, the ExecutionEngine will
306 /// remove its global mapping and free any machine code. Be sure no threads
307 /// are running inside F when that happens.
308 virtual void *getPointerToFunction(Function *F) = 0;
310 /// getPointerToBasicBlock - The different EE's represent basic blocks in
311 /// different ways. Return the representation for a blockaddress of the
313 virtual void *getPointerToBasicBlock(BasicBlock *BB) = 0;
315 /// getPointerToFunctionOrStub - If the specified function has been
316 /// code-gen'd, return a pointer to the function. If not, compile it, or use
317 /// a stub to implement lazy compilation if available. See
318 /// getPointerToFunction for the requirements on destroying F.
319 virtual void *getPointerToFunctionOrStub(Function *F) {
320 // Default implementation, just codegen the function.
321 return getPointerToFunction(F);
324 // The JIT overrides a version that actually does this.
325 virtual void runJITOnFunction(Function *, MachineCodeInfo * = 0) { }
327 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
328 /// at the specified address.
330 const GlobalValue *getGlobalValueAtAddress(void *Addr);
332 /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr.
333 /// Ptr is the address of the memory at which to store Val, cast to
334 /// GenericValue *. It is not a pointer to a GenericValue containing the
335 /// address at which to store Val.
336 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
339 void InitializeMemory(const Constant *Init, void *Addr);
341 /// recompileAndRelinkFunction - This method is used to force a function which
342 /// has already been compiled to be compiled again, possibly after it has been
343 /// modified. Then the entry to the old copy is overwritten with a branch to
344 /// the new copy. If there was no old copy, this acts just like
345 /// VM::getPointerToFunction().
346 virtual void *recompileAndRelinkFunction(Function *F) = 0;
348 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
349 /// corresponding to the machine code emitted to execute this function, useful
350 /// for garbage-collecting generated code.
351 virtual void freeMachineCodeForFunction(Function *F) = 0;
353 /// getOrEmitGlobalVariable - Return the address of the specified global
354 /// variable, possibly emitting it to memory if needed. This is used by the
356 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
357 return getPointerToGlobal((GlobalValue*)GV);
360 /// Registers a listener to be called back on various events within
361 /// the JIT. See JITEventListener.h for more details. Does not
362 /// take ownership of the argument. The argument may be NULL, in
363 /// which case these functions do nothing.
364 virtual void RegisterJITEventListener(JITEventListener *) {}
365 virtual void UnregisterJITEventListener(JITEventListener *) {}
367 /// DisableLazyCompilation - When lazy compilation is off (the default), the
368 /// JIT will eagerly compile every function reachable from the argument to
369 /// getPointerToFunction. If lazy compilation is turned on, the JIT will only
370 /// compile the one function and emit stubs to compile the rest when they're
371 /// first called. If lazy compilation is turned off again while some lazy
372 /// stubs are still around, and one of those stubs is called, the program will
375 /// In order to safely compile lazily in a threaded program, the user must
376 /// ensure that 1) only one thread at a time can call any particular lazy
377 /// stub, and 2) any thread modifying LLVM IR must hold the JIT's lock
378 /// (ExecutionEngine::lock) or otherwise ensure that no other thread calls a
379 /// lazy stub. See http://llvm.org/PR5184 for details.
380 void DisableLazyCompilation(bool Disabled = true) {
381 CompilingLazily = !Disabled;
383 bool isCompilingLazily() const {
384 return CompilingLazily;
386 // Deprecated in favor of isCompilingLazily (to reduce double-negatives).
387 // Remove this in LLVM 2.8.
388 bool isLazyCompilationDisabled() const {
389 return !CompilingLazily;
392 /// DisableGVCompilation - If called, the JIT will abort if it's asked to
393 /// allocate space and populate a GlobalVariable that is not internal to
395 void DisableGVCompilation(bool Disabled = true) {
396 GVCompilationDisabled = Disabled;
398 bool isGVCompilationDisabled() const {
399 return GVCompilationDisabled;
402 /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
403 /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to
404 /// resolve symbols in a custom way.
405 void DisableSymbolSearching(bool Disabled = true) {
406 SymbolSearchingDisabled = Disabled;
408 bool isSymbolSearchingDisabled() const {
409 return SymbolSearchingDisabled;
412 /// InstallLazyFunctionCreator - If an unknown function is needed, the
413 /// specified function pointer is invoked to create it. If it returns null,
414 /// the JIT will abort.
415 void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
416 LazyFunctionCreator = P;
419 /// InstallExceptionTableRegister - The JIT will use the given function
420 /// to register the exception tables it generates.
421 void InstallExceptionTableRegister(EERegisterFn F) {
422 ExceptionTableRegister = F;
424 void InstallExceptionTableDeregister(EERegisterFn F) {
425 ExceptionTableDeregister = F;
428 /// RegisterTable - Registers the given pointer as an exception table. It
429 /// uses the ExceptionTableRegister function.
430 void RegisterTable(const Function *fn, void* res) {
431 if (ExceptionTableRegister) {
432 ExceptionTableRegister(res);
433 AllExceptionTables[fn] = res;
437 /// DeregisterTable - Deregisters the exception frame previously registered
438 /// for the given function.
439 void DeregisterTable(const Function *Fn) {
440 if (ExceptionTableDeregister) {
441 DenseMap<const Function*, void*>::iterator frame =
442 AllExceptionTables.find(Fn);
443 if(frame != AllExceptionTables.end()) {
444 ExceptionTableDeregister(frame->second);
445 AllExceptionTables.erase(frame);
450 /// DeregisterAllTables - Deregisters all previously registered pointers to an
451 /// exception tables. It uses the ExceptionTableoDeregister function.
452 void DeregisterAllTables();
455 explicit ExecutionEngine(Module *M);
459 void EmitGlobalVariable(const GlobalVariable *GV);
461 GenericValue getConstantValue(const Constant *C);
462 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
466 namespace EngineKind {
467 // These are actually bitmasks that get or-ed together.
472 const static Kind Either = (Kind)(JIT | Interpreter);
475 /// EngineBuilder - Builder class for ExecutionEngines. Use this by
476 /// stack-allocating a builder, chaining the various set* methods, and
477 /// terminating it with a .create() call.
478 class EngineBuilder {
481 EngineKind::Kind WhichEngine;
482 std::string *ErrorStr;
483 CodeGenOpt::Level OptLevel;
484 JITMemoryManager *JMM;
485 bool AllocateGVsWithCode;
486 TargetOptions Options;
487 Reloc::Model RelocModel;
488 CodeModel::Model CMModel;
491 SmallVector<std::string, 4> MAttrs;
494 /// InitEngine - Does the common initialization of default options.
496 WhichEngine = EngineKind::Either;
498 OptLevel = CodeGenOpt::Default;
500 Options = TargetOptions();
501 AllocateGVsWithCode = false;
502 RelocModel = Reloc::Default;
503 CMModel = CodeModel::JITDefault;
508 /// EngineBuilder - Constructor for EngineBuilder. If create() is called and
509 /// is successful, the created engine takes ownership of the module.
510 EngineBuilder(Module *m) : M(m) {
514 /// setEngineKind - Controls whether the user wants the interpreter, the JIT,
515 /// or whichever engine works. This option defaults to EngineKind::Either.
516 EngineBuilder &setEngineKind(EngineKind::Kind w) {
521 /// setJITMemoryManager - Sets the memory manager to use. This allows
522 /// clients to customize their memory allocation policies. If create() is
523 /// called and is successful, the created engine takes ownership of the
524 /// memory manager. This option defaults to NULL.
525 EngineBuilder &setJITMemoryManager(JITMemoryManager *jmm) {
530 /// setErrorStr - Set the error string to write to on error. This option
531 /// defaults to NULL.
532 EngineBuilder &setErrorStr(std::string *e) {
537 /// setOptLevel - Set the optimization level for the JIT. This option
538 /// defaults to CodeGenOpt::Default.
539 EngineBuilder &setOptLevel(CodeGenOpt::Level l) {
544 /// setTargetOptions - Set the target options that the ExecutionEngine
545 /// target is using. Defaults to TargetOptions().
546 EngineBuilder &setTargetOptions(const TargetOptions &Opts) {
551 /// setRelocationModel - Set the relocation model that the ExecutionEngine
552 /// target is using. Defaults to target specific default "Reloc::Default".
553 EngineBuilder &setRelocationModel(Reloc::Model RM) {
558 /// setCodeModel - Set the CodeModel that the ExecutionEngine target
559 /// data is using. Defaults to target specific default
560 /// "CodeModel::JITDefault".
561 EngineBuilder &setCodeModel(CodeModel::Model M) {
566 /// setAllocateGVsWithCode - Sets whether global values should be allocated
567 /// into the same buffer as code. For most applications this should be set
568 /// to false. Allocating globals with code breaks freeMachineCodeForFunction
569 /// and is probably unsafe and bad for performance. However, we have clients
570 /// who depend on this behavior, so we must support it. This option defaults
571 /// to false so that users of the new API can safely use the new memory
572 /// manager and free machine code.
573 EngineBuilder &setAllocateGVsWithCode(bool a) {
574 AllocateGVsWithCode = a;
578 /// setMArch - Override the architecture set by the Module's triple.
579 EngineBuilder &setMArch(StringRef march) {
580 MArch.assign(march.begin(), march.end());
584 /// setMCPU - Target a specific cpu type.
585 EngineBuilder &setMCPU(StringRef mcpu) {
586 MCPU.assign(mcpu.begin(), mcpu.end());
590 /// setUseMCJIT - Set whether the MC-JIT implementation should be used
592 EngineBuilder &setUseMCJIT(bool Value) {
597 /// setMAttrs - Set cpu-specific attributes.
598 template<typename StringSequence>
599 EngineBuilder &setMAttrs(const StringSequence &mattrs) {
601 MAttrs.append(mattrs.begin(), mattrs.end());
605 TargetMachine *selectTarget();
607 /// selectTarget - Pick a target either via -march or by guessing the native
608 /// arch. Add any CPU features specified via -mcpu or -mattr.
609 TargetMachine *selectTarget(const Triple &TargetTriple,
612 const SmallVectorImpl<std::string>& MAttrs);
614 ExecutionEngine *create() {
615 return create(selectTarget());
618 ExecutionEngine *create(TargetMachine *TM);
621 } // End llvm namespace