ExecutionEngine.h revision 502f20b17ede40de84503010b7699b328a4f2867
1bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner//===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===//
2ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman//
36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//                     The LLVM Compiler Infrastructure
46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
7ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman//
86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===//
9bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner//
10bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner// This file defines the abstract interface that implements execution support
11bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner// for LLVM.
12bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner//
13bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner//===----------------------------------------------------------------------===//
14bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
159f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner#ifndef LLVM_EXECUTION_ENGINE_H
169f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner#define LLVM_EXECUTION_ENGINE_H
17bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
18bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner#include <vector>
19bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner#include <map>
202e99791a1ed49f11a2825a9a7590742e1ddc110aChris Lattner#include <cassert>
2120aa474f8fbebde588edc101b90e834df28ce4ceAlkis Evlogimenos#include <string>
22683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner#include "llvm/System/Mutex.h"
23fe854034677f59baca1e38075e71f6efca247a03Chris Lattner#include "llvm/ADT/SmallVector.h"
24d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
25d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
26d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
279d87eb19be82b28d288e38eab3bbe145060a0701Reid Spencerstruct GenericValue;
28bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Constant;
29bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Function;
303ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattnerclass GlobalVariable;
31b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukmanclass GlobalValue;
32bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Module;
337034adbce2fc7e761d745dbf586167380a69ee1aMisha Brukmanclass ModuleProvider;
34bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass TargetData;
35b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukmanclass Type;
36683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattnerclass MutexGuard;
379f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattnerclass JITMemoryManager;
38bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
39ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerclass ExecutionEngineState {
40ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerprivate:
41c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// GlobalAddressMap - A mapping between LLVM global values and their
42c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// actualized version...
43c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  std::map<const GlobalValue*, void *> GlobalAddressMap;
44c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner
45c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
46c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// used to convert raw addresses into the LLVM global value that is emitted
47c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// at the address.  This map is not computed unless getGlobalValueAtAddress
48c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// is called at some point.
49c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  std::map<void *, const GlobalValue*> GlobalAddressReverseMap;
50ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
51ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerpublic:
52765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  std::map<const GlobalValue*, void *> &
5313d57320bd212483463d4f8992d5787b29eda5dfBill Wendling  getGlobalAddressMap(const MutexGuard &) {
54ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer    return GlobalAddressMap;
55ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer  }
56ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
57765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  std::map<void*, const GlobalValue*> &
5813d57320bd212483463d4f8992d5787b29eda5dfBill Wendling  getGlobalAddressReverseMap(const MutexGuard &) {
59ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer    return GlobalAddressReverseMap;
60ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer  }
61ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer};
62ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
63ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
64ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerclass ExecutionEngine {
65ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer  const TargetData *TD;
66ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer  ExecutionEngineState state;
671c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner  bool LazyCompilationDisabled;
68e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  bool SymbolSearchingDisabled;
69d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner
701514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattnerprotected:
71fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  /// Modules - This is a list of ModuleProvider's that we are JIT'ing from.  We
72fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  /// use a smallvector to optimize for the case where there is only one module.
73fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  SmallVector<ModuleProvider*, 1> Modules;
74fe854034677f59baca1e38075e71f6efca247a03Chris Lattner
75a69571c7991813c93cba64e88eced6899ce93d81Owen Anderson  void setTargetData(const TargetData *td) {
76a69571c7991813c93cba64e88eced6899ce93d81Owen Anderson    TD = td;
77bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner  }
78b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukman
79765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  // To avoid having libexecutionengine depend on the JIT and interpreter
80765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  // libraries, the JIT and Interpreter set these functions to ctor pointers
81765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  // at startup time if they are linked in.
82502f20b17ede40de84503010b7699b328a4f2867Evan Cheng  typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*, std::string*,
83502f20b17ede40de84503010b7699b328a4f2867Evan Cheng                                       bool Fast);
84765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  static EECtorFn JITCtor, InterpCtor;
85d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner
86d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// LazyFunctionCreator - If an unknown function is needed, this function
87d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// pointer is invoked to create it. If this returns null, the JIT will abort.
88d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  void* (*LazyFunctionCreator)(const std::string &);
89d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner
90afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  /// ExceptionTableRegister - If Exception Handling is set, the JIT will
91afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  /// register dwarf tables with this function
92afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  typedef void (*EERegisterFn)(void*);
93afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  static EERegisterFn ExceptionTableRegister;
94afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
95bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerpublic:
96765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
97765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  /// JITEmitter classes.  It must be held while changing the internal state of
98765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  /// any of those classes.
99ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer  sys::Mutex lock; // Used to make this class and subclasses thread-safe
100ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
101f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  //===--------------------------------------------------------------------===//
1029f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  //  ExecutionEngine Startup
103f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  //===--------------------------------------------------------------------===//
104ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman
1059f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  virtual ~ExecutionEngine();
106bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
1079f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  /// create - This is the factory method for creating an execution engine which
1089f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  /// is appropriate for the current machine.  This takes ownership of the
1099f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  /// module provider.
1109f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  static ExecutionEngine *create(ModuleProvider *MP,
1119f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner                                 bool ForceInterpreter = false,
112502f20b17ede40de84503010b7699b328a4f2867Evan Cheng                                 std::string *ErrorStr = 0,
113502f20b17ede40de84503010b7699b328a4f2867Evan Cheng                                 bool Fast = false);
1149f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner
1159f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  /// create - This is the factory method for creating an execution engine which
1169f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  /// is appropriate for the current machine.  This takes ownership of the
1179f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  /// module.
1189f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  static ExecutionEngine *create(Module *M);
11934c9433004cabd4760987dce4804a91c84908219Chris Lattner
12034c9433004cabd4760987dce4804a91c84908219Chris Lattner  /// createJIT - This is the factory method for creating a JIT for the current
12134c9433004cabd4760987dce4804a91c84908219Chris Lattner  /// machine, it does not fall back to the interpreter.  This takes ownership
12234c9433004cabd4760987dce4804a91c84908219Chris Lattner  /// of the ModuleProvider and JITMemoryManager if successful.
12334c9433004cabd4760987dce4804a91c84908219Chris Lattner  static ExecutionEngine *createJIT(ModuleProvider *MP,
12434c9433004cabd4760987dce4804a91c84908219Chris Lattner                                    std::string *ErrorStr = 0,
125502f20b17ede40de84503010b7699b328a4f2867Evan Cheng                                    JITMemoryManager *JMM = 0,
126502f20b17ede40de84503010b7699b328a4f2867Evan Cheng                                    bool Fast = false);
12734c9433004cabd4760987dce4804a91c84908219Chris Lattner
1289f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner
1299f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner
1302e40ae4de67ba37d02281d51e7dfd629205e1e30Chris Lattner  /// addModuleProvider - Add a ModuleProvider to the list of modules that we
1312e40ae4de67ba37d02281d51e7dfd629205e1e30Chris Lattner  /// can JIT from.  Note that this takes ownership of the ModuleProvider: when
1322e40ae4de67ba37d02281d51e7dfd629205e1e30Chris Lattner  /// the ExecutionEngine is destroyed, it destroys the MP as well.
133f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  virtual void addModuleProvider(ModuleProvider *P) {
1342e40ae4de67ba37d02281d51e7dfd629205e1e30Chris Lattner    Modules.push_back(P);
1352e40ae4de67ba37d02281d51e7dfd629205e1e30Chris Lattner  }
1369f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner
1379f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  //===----------------------------------------------------------------------===//
1389f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner
1399f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  const TargetData *getTargetData() const { return TD; }
1409f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner
14173d0e211a39650fc7562e0f15ed21440dde2243aDevang Patel
14273d0e211a39650fc7562e0f15ed21440dde2243aDevang Patel  /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
14373d0e211a39650fc7562e0f15ed21440dde2243aDevang Patel  /// Release module from ModuleProvider.
144f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  virtual Module* removeModuleProvider(ModuleProvider *P,
145f049e07eb8930214941c72f8e4409df394de1567Nate Begeman                                       std::string *ErrInfo = 0);
14673d0e211a39650fc7562e0f15ed21440dde2243aDevang Patel
147fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  /// FindFunctionNamed - Search all of the active modules to find the one that
148fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  /// defines FnName.  This is very slow operation and shouldn't be used for
149fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  /// general code.
150fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  Function *FindFunctionNamed(const char *FnName);
151fe854034677f59baca1e38075e71f6efca247a03Chris Lattner
152ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner  /// runFunction - Execute the specified function with the specified arguments,
153ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner  /// and return the result.
154ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner  ///
155ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner  virtual GenericValue runFunction(Function *F,
156ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner                                const std::vector<GenericValue> &ArgValues) = 0;
157ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner
1589ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner  /// runStaticConstructorsDestructors - This method is used to execute all of
1599ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner  /// the static constructors or destructors for a module, depending on the
1609ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner  /// value of isDtors.
1619ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner  void runStaticConstructorsDestructors(bool isDtors);
1629ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner
1639ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner
164e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner  /// runFunctionAsMain - This is a helper function which wraps runFunction to
165e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner  /// handle the common task of starting up main with the specified argc, argv,
166e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner  /// and envp parameters.
167e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner  int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
168e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner                        const char * const * envp);
169e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner
170e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner
171683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// addGlobalMapping - Tell the execution engine that the specified global is
172683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// at the specified location.  This is used internally as functions are JIT'd
173683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// and as global variables are laid out in memory.  It can and should also be
174683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// used by clients of the EE that want to have an LLVM global overlay
175683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// existing data in memory.
176683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  void addGlobalMapping(const GlobalValue *GV, void *Addr);
177683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner
1780982d081056f64d7245ba26368d20de33e206ff9Reid Spencer  /// clearAllGlobalMappings - Clear all global mappings and start over again
1790982d081056f64d7245ba26368d20de33e206ff9Reid Spencer  /// use in dynamic compilation scenarios when you want to move globals
180683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  void clearAllGlobalMappings();
181683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner
182f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
183f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  /// particular module, because it has been removed from the JIT.
184f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  void clearGlobalMappingsFromModule(Module *M);
185f049e07eb8930214941c72f8e4409df394de1567Nate Begeman
186f5feaf4fe381476b0ea567d7837c64b6590133e9Chris Lattner  /// updateGlobalMapping - Replace an existing mapping for GV with a new
187683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// address.  This updates both maps as required.  If "Addr" is null, the
188f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  /// entry for the global is removed from the mappings.  This returns the old
189f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  /// value of the pointer, or null if it was not in the map.
190f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
191683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner
192895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// getPointerToGlobalIfAvailable - This returns the address of the specified
193683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// global value if it is has already been codegen'd, otherwise it returns
194683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// null.
195895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  ///
196683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
19770bca51f92871c7f9d3eac9ab68292c149fab53cChris Lattner
198895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// getPointerToGlobal - This returns the address of the specified global
199895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// value.  This may involve code generation if it's a function.
200895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  ///
201bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner  void *getPointerToGlobal(const GlobalValue *GV);
202bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
203895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// getPointerToFunction - The different EE's represent function bodies in
204895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// different ways.  They should each implement this to say what a function
205895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// pointer should look like.
206895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  ///
207fb0ef2e82cb2e80983c097100ae168af68ee8e7bBrian Gaeke  virtual void *getPointerToFunction(Function *F) = 0;
208bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
209895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// getPointerToFunctionOrStub - If the specified function has been
210895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// code-gen'd, return a pointer to the function.  If not, compile it, or use
211895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// a stub to implement lazy compilation if available.
212895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  ///
21318cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  virtual void *getPointerToFunctionOrStub(Function *F) {
21418cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner    // Default implementation, just codegen the function.
21518cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner    return getPointerToFunction(F);
21618cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  }
21718cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner
218c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// getGlobalValueAtAddress - Return the LLVM global value object that starts
219c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// at the specified address.
220c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  ///
221c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  const GlobalValue *getGlobalValueAtAddress(void *Addr);
222c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner
223c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner
224f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
225f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner                          const Type *Ty);
22629794cba810ec249a9ede5ea77333a71579fd182Brian Gaeke  void InitializeMemory(const Constant *Init, void *Addr);
22729794cba810ec249a9ede5ea77333a71579fd182Brian Gaeke
22818cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  /// recompileAndRelinkFunction - This method is used to force a function
229895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// which has already been compiled to be compiled again, possibly
23018cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  /// after it has been modified. Then the entry to the old copy is overwritten
23118cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  /// with a branch to the new copy. If there was no old copy, this acts
23218cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  /// just like VM::getPointerToFunction().
23318cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  ///
23418cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  virtual void *recompileAndRelinkFunction(Function *F) = 0;
23518cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner
236e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
237e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  /// corresponding to the machine code emitted to execute this function, useful
238e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  /// for garbage-collecting generated code.
239e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  ///
240e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  virtual void freeMachineCodeForFunction(Function *F) = 0;
241e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman
2421514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  /// getOrEmitGlobalVariable - Return the address of the specified global
2431514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  /// variable, possibly emitting it to memory if needed.  This is used by the
2441514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  /// Emitter.
2451514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
2461514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner    return getPointerToGlobal((GlobalValue*)GV);
2471514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  }
2481c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner
2491c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner  /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
250e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  /// is ever attempted.
251e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  void DisableLazyCompilation(bool Disabled = true) {
252e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner    LazyCompilationDisabled = Disabled;
2531c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner  }
2541c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner  bool isLazyCompilationDisabled() const {
2551c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner    return LazyCompilationDisabled;
2561c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner  }
257e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
258e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  /// symbols with dlsym.  A client can still use InstallLazyFunctionCreator to
259e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  /// resolve symbols in a custom way.
260e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  void DisableSymbolSearching(bool Disabled = true) {
261e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner    SymbolSearchingDisabled = Disabled;
262e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  }
263e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  bool isSymbolSearchingDisabled() const {
264e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner    return SymbolSearchingDisabled;
265e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  }
266d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner
267d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner
268d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// InstallLazyFunctionCreator - If an unknown function is needed, the
269d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// specified function pointer is invoked to create it.  If it returns null,
270d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// the JIT will abort.
271d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
272d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner    LazyFunctionCreator = P;
273d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  }
274afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
275afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  /// InstallExceptionTableRegister - The JIT will use the given function
276afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  /// to register the exception tables it generates.
277afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  static void InstallExceptionTableRegister(void (*F)(void*)) {
278afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    ExceptionTableRegister = F;
279afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
280afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
281afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  /// RegisterTable - Registers the given pointer as an exception table. It uses
282afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  /// the ExceptionTableRegister function.
283afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  static void RegisterTable(void* res) {
284afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    if (ExceptionTableRegister)
285afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      ExceptionTableRegister(res);
286afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
2871514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner
28856adf152f6354a9b5609e059050fd2315ad5960cChris Lattnerprotected:
289ded2b0d0fb0d4fa09198e3d05da529d2c97214c3Dan Gohman  explicit ExecutionEngine(ModuleProvider *P);
2909f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner
291bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner  void emitGlobals();
2923ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner
2933ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner  // EmitGlobalVariable - This method emits the specified global variable to the
2943ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner  // address specified in GlobalAddresses, or allocates new memory if it's not
2953ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner  // already in the map.
2961514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  void EmitGlobalVariable(const GlobalVariable *GV);
2973ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner
298bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner  GenericValue getConstantValue(const Constant *C);
29947567bdcb5ba3886eaf3fa5f0e0b912a24c12347Reid Spencer  void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
30047567bdcb5ba3886eaf3fa5f0e0b912a24c12347Reid Spencer                           const Type *Ty);
301bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner};
302bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
303d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
304d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
305bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner#endif
306