ExecutionEngine.h revision 193323
1103285Sikob//===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===//
2103285Sikob//
3103285Sikob//                     The LLVM Compiler Infrastructure
4103285Sikob//
5103285Sikob// This file is distributed under the University of Illinois Open Source
6103285Sikob// License. See LICENSE.TXT for details.
7103285Sikob//
8103285Sikob//===----------------------------------------------------------------------===//
9103285Sikob//
10103285Sikob// This file defines the abstract interface that implements execution support
11103285Sikob// for LLVM.
12103285Sikob//
13103285Sikob//===----------------------------------------------------------------------===//
14103285Sikob
15103285Sikob#ifndef LLVM_EXECUTION_ENGINE_H
16103285Sikob#define LLVM_EXECUTION_ENGINE_H
17103285Sikob
18103285Sikob#include <vector>
19103285Sikob#include <map>
20103285Sikob#include <string>
21103285Sikob#include "llvm/ADT/SmallVector.h"
22103285Sikob#include "llvm/System/Mutex.h"
23103285Sikob#include "llvm/Target/TargetMachine.h"
24103285Sikob
25103285Sikobnamespace llvm {
26103285Sikob
27103285Sikobstruct GenericValue;
28103285Sikobclass Constant;
29103285Sikobclass Function;
30103285Sikobclass GlobalVariable;
31103285Sikobclass GlobalValue;
32103285Sikobclass Module;
33103285Sikobclass ModuleProvider;
34103285Sikobclass TargetData;
35103285Sikobclass Type;
36103285Sikobclass MutexGuard;
37103285Sikobclass JITMemoryManager;
38103285Sikobclass MachineCodeInfo;
39103285Sikob
40103285Sikobclass ExecutionEngineState {
41103285Sikobprivate:
42103285Sikob  /// GlobalAddressMap - A mapping between LLVM global values and their
43103285Sikob  /// actualized version...
44103285Sikob  std::map<const GlobalValue*, void *> GlobalAddressMap;
45103285Sikob
46103285Sikob  /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
47103285Sikob  /// used to convert raw addresses into the LLVM global value that is emitted
48103285Sikob  /// at the address.  This map is not computed unless getGlobalValueAtAddress
49103285Sikob  /// is called at some point.
50103285Sikob  std::map<void *, const GlobalValue*> GlobalAddressReverseMap;
51103285Sikob
52103285Sikobpublic:
53103285Sikob  std::map<const GlobalValue*, void *> &
54103285Sikob  getGlobalAddressMap(const MutexGuard &) {
55103285Sikob    return GlobalAddressMap;
56103285Sikob  }
57106810Ssimokawa
58106810Ssimokawa  std::map<void*, const GlobalValue*> &
59103285Sikob  getGlobalAddressReverseMap(const MutexGuard &) {
60103285Sikob    return GlobalAddressReverseMap;
61108281Ssimokawa  }
62106810Ssimokawa};
63106810Ssimokawa
64110582Ssimokawa
65106810Ssimokawaclass ExecutionEngine {
66103285Sikob  const TargetData *TD;
67103285Sikob  ExecutionEngineState state;
68103285Sikob  bool LazyCompilationDisabled;
69103285Sikob  bool GVCompilationDisabled;
70103285Sikob  bool SymbolSearchingDisabled;
71106816Ssimokawa  bool DlsymStubsEnabled;
72106816Ssimokawa
73106816Ssimokawaprotected:
74106816Ssimokawa  /// Modules - This is a list of ModuleProvider's that we are JIT'ing from.  We
75106816Ssimokawa  /// use a smallvector to optimize for the case where there is only one module.
76106810Ssimokawa  SmallVector<ModuleProvider*, 1> Modules;
77106816Ssimokawa
78106816Ssimokawa  void setTargetData(const TargetData *td) {
79106816Ssimokawa    TD = td;
80106816Ssimokawa  }
81103285Sikob
82103285Sikob  /// getMemoryforGV - Allocate memory for a global variable.
83103285Sikob  virtual char* getMemoryForGV(const GlobalVariable* GV);
84110269Ssimokawa
85106804Ssimokawa  // To avoid having libexecutionengine depend on the JIT and interpreter
86103285Sikob  // libraries, the JIT and Interpreter set these functions to ctor pointers
87106804Ssimokawa  // at startup time if they are linked in.
88106810Ssimokawa  typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*, std::string*,
89106810Ssimokawa                                       CodeGenOpt::Level OptLevel);
90106816Ssimokawa  static EECtorFn JITCtor, InterpCtor;
91106816Ssimokawa
92106816Ssimokawa  /// LazyFunctionCreator - If an unknown function is needed, this function
93106816Ssimokawa  /// pointer is invoked to create it. If this returns null, the JIT will abort.
94106816Ssimokawa  void* (*LazyFunctionCreator)(const std::string &);
95110195Ssimokawa
96106804Ssimokawa  /// ExceptionTableRegister - If Exception Handling is set, the JIT will
97106816Ssimokawa  /// register dwarf tables with this function
98106816Ssimokawa  typedef void (*EERegisterFn)(void*);
99106816Ssimokawa  static EERegisterFn ExceptionTableRegister;
100106816Ssimokawa
101106804Ssimokawapublic:
102103285Sikob  /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
103106804Ssimokawa  /// JITEmitter classes.  It must be held while changing the internal state of
104103285Sikob  /// any of those classes.
105106816Ssimokawa  sys::Mutex lock; // Used to make this class and subclasses thread-safe
106103285Sikob
107106816Ssimokawa  //===--------------------------------------------------------------------===//
108106816Ssimokawa  //  ExecutionEngine Startup
109106816Ssimokawa  //===--------------------------------------------------------------------===//
110106816Ssimokawa
111106816Ssimokawa  virtual ~ExecutionEngine();
112106816Ssimokawa
113106816Ssimokawa  /// create - This is the factory method for creating an execution engine which
114106816Ssimokawa  /// is appropriate for the current machine.  This takes ownership of the
115106816Ssimokawa  /// module provider.
116106816Ssimokawa  static ExecutionEngine *create(ModuleProvider *MP,
117106816Ssimokawa                                 bool ForceInterpreter = false,
118106816Ssimokawa                                 std::string *ErrorStr = 0,
119106816Ssimokawa                                 CodeGenOpt::Level OptLevel =
120106816Ssimokawa                                   CodeGenOpt::Default);
121106816Ssimokawa
122106816Ssimokawa  /// create - This is the factory method for creating an execution engine which
123106816Ssimokawa  /// is appropriate for the current machine.  This takes ownership of the
124106816Ssimokawa  /// module.
125106816Ssimokawa  static ExecutionEngine *create(Module *M);
126103285Sikob
127103285Sikob  /// createJIT - This is the factory method for creating a JIT for the current
128103285Sikob  /// machine, it does not fall back to the interpreter.  This takes ownership
129103285Sikob  /// of the ModuleProvider and JITMemoryManager if successful.
130103285Sikob  static ExecutionEngine *createJIT(ModuleProvider *MP,
131103285Sikob                                    std::string *ErrorStr = 0,
132103285Sikob                                    JITMemoryManager *JMM = 0,
133106816Ssimokawa                                    CodeGenOpt::Level OptLevel =
134106816Ssimokawa                                      CodeGenOpt::Default);
135106804Ssimokawa
136106810Ssimokawa  /// addModuleProvider - Add a ModuleProvider to the list of modules that we
137103285Sikob  /// can JIT from.  Note that this takes ownership of the ModuleProvider: when
138106804Ssimokawa  /// the ExecutionEngine is destroyed, it destroys the MP as well.
139103285Sikob  virtual void addModuleProvider(ModuleProvider *P) {
140103285Sikob    Modules.push_back(P);
141103285Sikob  }
142103285Sikob
143103285Sikob  //===----------------------------------------------------------------------===//
144106810Ssimokawa
145106810Ssimokawa  const TargetData *getTargetData() const { return TD; }
146106810Ssimokawa
147106810Ssimokawa
148106810Ssimokawa  /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
149106810Ssimokawa  /// Relases the Module from the ModuleProvider, materializing it in the
150106810Ssimokawa  /// process, and returns the materialized Module.
151106810Ssimokawa  virtual Module* removeModuleProvider(ModuleProvider *P,
152106810Ssimokawa                                       std::string *ErrInfo = 0);
153106810Ssimokawa
154106810Ssimokawa  /// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
155106810Ssimokawa  /// and deletes the ModuleProvider and owned Module.  Avoids materializing
156106816Ssimokawa  /// the underlying module.
157106810Ssimokawa  virtual void deleteModuleProvider(ModuleProvider *P,std::string *ErrInfo = 0);
158106810Ssimokawa
159106810Ssimokawa  /// FindFunctionNamed - Search all of the active modules to find the one that
160106810Ssimokawa  /// defines FnName.  This is very slow operation and shouldn't be used for
161110072Ssimokawa  /// general code.
162106810Ssimokawa  Function *FindFunctionNamed(const char *FnName);
163106810Ssimokawa
164106810Ssimokawa  /// runFunction - Execute the specified function with the specified arguments,
165106810Ssimokawa  /// and return the result.
166106810Ssimokawa  ///
167106810Ssimokawa  virtual GenericValue runFunction(Function *F,
168106810Ssimokawa                                const std::vector<GenericValue> &ArgValues) = 0;
169106816Ssimokawa
170106816Ssimokawa  /// runStaticConstructorsDestructors - This method is used to execute all of
171106810Ssimokawa  /// the static constructors or destructors for a program, depending on the
172106810Ssimokawa  /// value of isDtors.
173106810Ssimokawa  void runStaticConstructorsDestructors(bool isDtors);
174106810Ssimokawa  /// runStaticConstructorsDestructors - This method is used to execute all of
175106810Ssimokawa  /// the static constructors or destructors for a module, depending on the
176106810Ssimokawa  /// value of isDtors.
177106810Ssimokawa  void runStaticConstructorsDestructors(Module *module, bool isDtors);
178106810Ssimokawa
179106810Ssimokawa
180103285Sikob  /// runFunctionAsMain - This is a helper function which wraps runFunction to
181106810Ssimokawa  /// handle the common task of starting up main with the specified argc, argv,
182106810Ssimokawa  /// and envp parameters.
183106804Ssimokawa  int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
184103285Sikob                        const char * const * envp);
185103285Sikob
186106804Ssimokawa
187106804Ssimokawa  /// addGlobalMapping - Tell the execution engine that the specified global is
188103285Sikob  /// at the specified location.  This is used internally as functions are JIT'd
189103285Sikob  /// and as global variables are laid out in memory.  It can and should also be
190103285Sikob  /// used by clients of the EE that want to have an LLVM global overlay
191103285Sikob  /// existing data in memory.  After adding a mapping for GV, you must not
192106816Ssimokawa  /// destroy it until you've removed the mapping.
193106804Ssimokawa  void addGlobalMapping(const GlobalValue *GV, void *Addr);
194103285Sikob
195106804Ssimokawa  /// clearAllGlobalMappings - Clear all global mappings and start over again
196103285Sikob  /// use in dynamic compilation scenarios when you want to move globals
197103285Sikob  void clearAllGlobalMappings();
198103285Sikob
199103285Sikob  /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
200103285Sikob  /// particular module, because it has been removed from the JIT.
201103285Sikob  void clearGlobalMappingsFromModule(Module *M);
202103285Sikob
203103285Sikob  /// updateGlobalMapping - Replace an existing mapping for GV with a new
204106816Ssimokawa  /// address.  This updates both maps as required.  If "Addr" is null, the
205106810Ssimokawa  /// entry for the global is removed from the mappings.  This returns the old
206106810Ssimokawa  /// value of the pointer, or null if it was not in the map.
207103285Sikob  void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
208106804Ssimokawa
209103285Sikob  /// getPointerToGlobalIfAvailable - This returns the address of the specified
210103285Sikob  /// global value if it is has already been codegen'd, otherwise it returns
211103285Sikob  /// null.
212103285Sikob  ///
213110337Ssimokawa  void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
214110337Ssimokawa
215110337Ssimokawa  /// getPointerToGlobal - This returns the address of the specified global
216110337Ssimokawa  /// value.  This may involve code generation if it's a function.  After
217110337Ssimokawa  /// getting a pointer to GV, it and all globals it transitively refers to have
218110337Ssimokawa  /// been passed to addGlobalMapping.  You must clear the mapping for each
219110337Ssimokawa  /// referred-to global before destroying it.  If a referred-to global RTG is a
220110337Ssimokawa  /// function and this ExecutionEngine is a JIT compiler, calling
221110337Ssimokawa  /// updateGlobalMapping(RTG, 0) will leak the function's machine code, so you
222110337Ssimokawa  /// should call freeMachineCodeForFunction(RTG) instead.  Note that
223110337Ssimokawa  /// optimizations can move and delete non-external GlobalValues without
224110337Ssimokawa  /// notifying the ExecutionEngine.
225110337Ssimokawa  ///
226110337Ssimokawa  void *getPointerToGlobal(const GlobalValue *GV);
227110337Ssimokawa
228110337Ssimokawa  /// getPointerToFunction - The different EE's represent function bodies in
229110337Ssimokawa  /// different ways.  They should each implement this to say what a function
230110337Ssimokawa  /// pointer should look like.  See getPointerToGlobal for the requirements on
231110337Ssimokawa  /// destroying F and any GlobalValues it refers to.
232110406Ssimokawa  ///
233110337Ssimokawa  virtual void *getPointerToFunction(Function *F) = 0;
234110337Ssimokawa
235110337Ssimokawa  /// getPointerToFunctionOrStub - If the specified function has been
236110337Ssimokawa  /// code-gen'd, return a pointer to the function.  If not, compile it, or use
237110337Ssimokawa  /// a stub to implement lazy compilation if available.  See getPointerToGlobal
238110337Ssimokawa  /// for the requirements on destroying F and any GlobalValues it refers to.
239110337Ssimokawa  ///
240110337Ssimokawa  virtual void *getPointerToFunctionOrStub(Function *F) {
241110337Ssimokawa    // Default implementation, just codegen the function.
242110337Ssimokawa    return getPointerToFunction(F);
243110337Ssimokawa  }
244110337Ssimokawa
245110337Ssimokawa  // The JIT overrides a version that actually does this.
246110337Ssimokawa  virtual void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0) { }
247110337Ssimokawa
248110337Ssimokawa  /// getGlobalValueAtAddress - Return the LLVM global value object that starts
249110337Ssimokawa  /// at the specified address.
250103285Sikob  ///
251103285Sikob  const GlobalValue *getGlobalValueAtAddress(void *Addr);
252103285Sikob
253110582Ssimokawa
254110582Ssimokawa  void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
255110582Ssimokawa                          const Type *Ty);
256110582Ssimokawa  void InitializeMemory(const Constant *Init, void *Addr);
257110582Ssimokawa
258110582Ssimokawa  /// recompileAndRelinkFunction - This method is used to force a function
259110582Ssimokawa  /// which has already been compiled to be compiled again, possibly
260110582Ssimokawa  /// after it has been modified. Then the entry to the old copy is overwritten
261110582Ssimokawa  /// with a branch to the new copy. If there was no old copy, this acts
262103285Sikob  /// just like VM::getPointerToFunction().
263103285Sikob  ///
264103285Sikob  virtual void *recompileAndRelinkFunction(Function *F) = 0;
265103285Sikob
266103285Sikob  /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
267110582Ssimokawa  /// corresponding to the machine code emitted to execute this function, useful
268110582Ssimokawa  /// for garbage-collecting generated code.
269103285Sikob  ///
270103285Sikob  virtual void freeMachineCodeForFunction(Function *F) = 0;
271103285Sikob
272103285Sikob  /// getOrEmitGlobalVariable - Return the address of the specified global
273103285Sikob  /// variable, possibly emitting it to memory if needed.  This is used by the
274103285Sikob  /// Emitter.  See getPointerToGlobal for the requirements on destroying GV and
275103285Sikob  /// any GlobalValues it refers to.
276103285Sikob  virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
277106810Ssimokawa    return getPointerToGlobal((GlobalValue*)GV);
278103285Sikob  }
279110337Ssimokawa
280103285Sikob  /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
281103285Sikob  /// is ever attempted.
282103285Sikob  void DisableLazyCompilation(bool Disabled = true) {
283103285Sikob    LazyCompilationDisabled = Disabled;
284103285Sikob  }
285103285Sikob  bool isLazyCompilationDisabled() const {
286103285Sikob    return LazyCompilationDisabled;
287110582Ssimokawa  }
288106810Ssimokawa
289110577Ssimokawa  /// DisableGVCompilation - If called, the JIT will abort if it's asked to
290110577Ssimokawa  /// allocate space and populate a GlobalVariable that is not internal to
291110577Ssimokawa  /// the module.
292106810Ssimokawa  void DisableGVCompilation(bool Disabled = true) {
293106810Ssimokawa    GVCompilationDisabled = Disabled;
294103285Sikob  }
295110465Ssimokawa  bool isGVCompilationDisabled() const {
296103285Sikob    return GVCompilationDisabled;
297103285Sikob  }
298103285Sikob
299110337Ssimokawa  /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
300110337Ssimokawa  /// symbols with dlsym.  A client can still use InstallLazyFunctionCreator to
301110337Ssimokawa  /// resolve symbols in a custom way.
302106804Ssimokawa  void DisableSymbolSearching(bool Disabled = true) {
303110465Ssimokawa    SymbolSearchingDisabled = Disabled;
304110465Ssimokawa  }
305110465Ssimokawa  bool isSymbolSearchingDisabled() const {
306110465Ssimokawa    return SymbolSearchingDisabled;
307110337Ssimokawa  }
308110337Ssimokawa
309110337Ssimokawa  /// EnableDlsymStubs -
310110406Ssimokawa  void EnableDlsymStubs(bool Enabled = true) {
311110406Ssimokawa    DlsymStubsEnabled = Enabled;
312110406Ssimokawa  }
313110406Ssimokawa  bool areDlsymStubsEnabled() const {
314110337Ssimokawa    return DlsymStubsEnabled;
315110337Ssimokawa  }
316110337Ssimokawa
317110337Ssimokawa  /// InstallLazyFunctionCreator - If an unknown function is needed, the
318106804Ssimokawa  /// specified function pointer is invoked to create it.  If it returns null,
319110465Ssimokawa  /// the JIT will abort.
320110465Ssimokawa  void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
321110465Ssimokawa    LazyFunctionCreator = P;
322110465Ssimokawa  }
323110337Ssimokawa
324110337Ssimokawa  /// InstallExceptionTableRegister - The JIT will use the given function
325110337Ssimokawa  /// to register the exception tables it generates.
326110406Ssimokawa  static void InstallExceptionTableRegister(void (*F)(void*)) {
327110406Ssimokawa    ExceptionTableRegister = F;
328110406Ssimokawa  }
329110406Ssimokawa
330110337Ssimokawa  /// RegisterTable - Registers the given pointer as an exception table. It uses
331110337Ssimokawa  /// the ExceptionTableRegister function.
332103285Sikob  static void RegisterTable(void* res) {
333103285Sikob    if (ExceptionTableRegister)
334103285Sikob      ExceptionTableRegister(res);
335103285Sikob  }
336103285Sikob
337103285Sikobprotected:
338110337Ssimokawa  explicit ExecutionEngine(ModuleProvider *P);
339110337Ssimokawa
340110337Ssimokawa  void emitGlobals();
341110337Ssimokawa
342110337Ssimokawa  // EmitGlobalVariable - This method emits the specified global variable to the
343110337Ssimokawa  // address specified in GlobalAddresses, or allocates new memory if it's not
344110337Ssimokawa  // already in the map.
345110337Ssimokawa  void EmitGlobalVariable(const GlobalVariable *GV);
346110337Ssimokawa
347110337Ssimokawa  GenericValue getConstantValue(const Constant *C);
348110337Ssimokawa  void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
349110337Ssimokawa                           const Type *Ty);
350110582Ssimokawa};
351110337Ssimokawa
352110577Ssimokawa} // End llvm namespace
353110577Ssimokawa
354110577Ssimokawa#endif
355110337Ssimokawa