ExecutionEngine.h revision 489393d7b92107cc3de17d8dbe7dd11ab7395fdc
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>
2020aa474f8fbebde588edc101b90e834df28ce4ceAlkis Evlogimenos#include <string>
21fe854034677f59baca1e38075e71f6efca247a03Chris Lattner#include "llvm/ADT/SmallVector.h"
2298a366d547772010e94609e4584489b3e5ce0043Bill Wendling#include "llvm/System/Mutex.h"
2398a366d547772010e94609e4584489b3e5ce0043Bill Wendling#include "llvm/Target/TargetMachine.h"
24d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
25d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
26d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
279d87eb19be82b28d288e38eab3bbe145060a0701Reid Spencerstruct GenericValue;
28bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Constant;
29bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Function;
303ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattnerclass GlobalVariable;
31b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukmanclass GlobalValue;
32df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass JITEventListener;
33df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass JITMemoryManager;
34df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass MachineCodeInfo;
35bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Module;
367034adbce2fc7e761d745dbf586167380a69ee1aMisha Brukmanclass ModuleProvider;
37df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass MutexGuard;
38bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass TargetData;
39b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukmanclass Type;
40bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
41ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerclass ExecutionEngineState {
42ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerprivate:
43c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// GlobalAddressMap - A mapping between LLVM global values and their
44c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// actualized version...
45c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  std::map<const GlobalValue*, void *> GlobalAddressMap;
46c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner
47c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
48c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// used to convert raw addresses into the LLVM global value that is emitted
49c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// at the address.  This map is not computed unless getGlobalValueAtAddress
50c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// is called at some point.
51c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  std::map<void *, const GlobalValue*> GlobalAddressReverseMap;
52ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
53ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerpublic:
54765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  std::map<const GlobalValue*, void *> &
5513d57320bd212483463d4f8992d5787b29eda5dfBill Wendling  getGlobalAddressMap(const MutexGuard &) {
56ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer    return GlobalAddressMap;
57ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer  }
58ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
59765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  std::map<void*, const GlobalValue*> &
6013d57320bd212483463d4f8992d5787b29eda5dfBill Wendling  getGlobalAddressReverseMap(const MutexGuard &) {
61ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer    return GlobalAddressReverseMap;
62ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer  }
63ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer};
64ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
65ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
66ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerclass ExecutionEngine {
67ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer  const TargetData *TD;
68ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer  ExecutionEngineState state;
691c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner  bool LazyCompilationDisabled;
70446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng  bool GVCompilationDisabled;
71e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  bool SymbolSearchingDisabled;
72d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  bool DlsymStubsEnabled;
73d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner
741514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattnerprotected:
75fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  /// Modules - This is a list of ModuleProvider's that we are JIT'ing from.  We
76fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  /// use a smallvector to optimize for the case where there is only one module.
77fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  SmallVector<ModuleProvider*, 1> Modules;
78fe854034677f59baca1e38075e71f6efca247a03Chris Lattner
79a69571c7991813c93cba64e88eced6899ce93d81Owen Anderson  void setTargetData(const TargetData *td) {
80a69571c7991813c93cba64e88eced6899ce93d81Owen Anderson    TD = td;
81bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner  }
8246fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray
8346fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray  /// getMemoryforGV - Allocate memory for a global variable.
8446fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray  virtual char* getMemoryForGV(const GlobalVariable* GV);
85b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukman
86765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  // To avoid having libexecutionengine depend on the JIT and interpreter
87765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  // libraries, the JIT and Interpreter set these functions to ctor pointers
88765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  // at startup time if they are linked in.
89502f20b17ede40de84503010b7699b328a4f2867Evan Cheng  typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*, std::string*,
90489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin                                       CodeGenOpt::Level OptLevel,
91489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin                                       bool GVsWithCode);
92765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  static EECtorFn JITCtor, InterpCtor;
93d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner
94d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// LazyFunctionCreator - If an unknown function is needed, this function
95d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// pointer is invoked to create it. If this returns null, the JIT will abort.
96d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  void* (*LazyFunctionCreator)(const std::string &);
97d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner
98afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  /// ExceptionTableRegister - If Exception Handling is set, the JIT will
99afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  /// register dwarf tables with this function
100afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  typedef void (*EERegisterFn)(void*);
101afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  static EERegisterFn ExceptionTableRegister;
102afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
103bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerpublic:
104765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and
105765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  /// JITEmitter classes.  It must be held while changing the internal state of
106765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  /// any of those classes.
107ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer  sys::Mutex lock; // Used to make this class and subclasses thread-safe
108ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
109f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  //===--------------------------------------------------------------------===//
1109f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  //  ExecutionEngine Startup
111f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  //===--------------------------------------------------------------------===//
112ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman
1139f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  virtual ~ExecutionEngine();
114bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris 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 provider.
1189f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  static ExecutionEngine *create(ModuleProvider *MP,
1199f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner                                 bool ForceInterpreter = false,
120502f20b17ede40de84503010b7699b328a4f2867Evan Cheng                                 std::string *ErrorStr = 0,
12198a366d547772010e94609e4584489b3e5ce0043Bill Wendling                                 CodeGenOpt::Level OptLevel =
122489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin                                   CodeGenOpt::Default,
123489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin                                 // Allocating globals with code breaks
124489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin                                 // freeMachineCodeForFunction and is probably
125489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin                                 // unsafe and bad for performance.  However,
126489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin                                 // we have clients who depend on this
127489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin                                 // behavior, so we must support it.
128489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin                                 // Eventually, when we're willing to break
129489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin                                 // some backwards compatability, this flag
130489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin                                 // should be flipped to false, so that by
131489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin                                 // default freeMachineCodeForFunction works.
132489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin                                 bool GVsWithCode = true);
133489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin
1349f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  /// create - This is the factory method for creating an execution engine which
1359f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  /// is appropriate for the current machine.  This takes ownership of the
1369f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  /// module.
1379f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  static ExecutionEngine *create(Module *M);
13834c9433004cabd4760987dce4804a91c84908219Chris Lattner
13934c9433004cabd4760987dce4804a91c84908219Chris Lattner  /// createJIT - This is the factory method for creating a JIT for the current
14034c9433004cabd4760987dce4804a91c84908219Chris Lattner  /// machine, it does not fall back to the interpreter.  This takes ownership
14134c9433004cabd4760987dce4804a91c84908219Chris Lattner  /// of the ModuleProvider and JITMemoryManager if successful.
14234c9433004cabd4760987dce4804a91c84908219Chris Lattner  static ExecutionEngine *createJIT(ModuleProvider *MP,
14334c9433004cabd4760987dce4804a91c84908219Chris Lattner                                    std::string *ErrorStr = 0,
144502f20b17ede40de84503010b7699b328a4f2867Evan Cheng                                    JITMemoryManager *JMM = 0,
14598a366d547772010e94609e4584489b3e5ce0043Bill Wendling                                    CodeGenOpt::Level OptLevel =
146489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin                                      CodeGenOpt::Default,
147489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin                                    bool GVsWithCode = true);
14898a366d547772010e94609e4584489b3e5ce0043Bill Wendling
1492e40ae4de67ba37d02281d51e7dfd629205e1e30Chris Lattner  /// addModuleProvider - Add a ModuleProvider to the list of modules that we
1502e40ae4de67ba37d02281d51e7dfd629205e1e30Chris Lattner  /// can JIT from.  Note that this takes ownership of the ModuleProvider: when
1512e40ae4de67ba37d02281d51e7dfd629205e1e30Chris Lattner  /// the ExecutionEngine is destroyed, it destroys the MP as well.
152f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  virtual void addModuleProvider(ModuleProvider *P) {
1532e40ae4de67ba37d02281d51e7dfd629205e1e30Chris Lattner    Modules.push_back(P);
1542e40ae4de67ba37d02281d51e7dfd629205e1e30Chris Lattner  }
1559f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner
1569f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  //===----------------------------------------------------------------------===//
1579f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner
1589f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  const TargetData *getTargetData() const { return TD; }
1599f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner
16073d0e211a39650fc7562e0f15ed21440dde2243aDevang Patel
16173d0e211a39650fc7562e0f15ed21440dde2243aDevang Patel  /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
16260789e419e04c260e36af9a1add5ad316313e490Nate Begeman  /// Relases the Module from the ModuleProvider, materializing it in the
16360789e419e04c260e36af9a1add5ad316313e490Nate Begeman  /// process, and returns the materialized Module.
164f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  virtual Module* removeModuleProvider(ModuleProvider *P,
165f049e07eb8930214941c72f8e4409df394de1567Nate Begeman                                       std::string *ErrInfo = 0);
16673d0e211a39650fc7562e0f15ed21440dde2243aDevang Patel
16760789e419e04c260e36af9a1add5ad316313e490Nate Begeman  /// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
16860789e419e04c260e36af9a1add5ad316313e490Nate Begeman  /// and deletes the ModuleProvider and owned Module.  Avoids materializing
16960789e419e04c260e36af9a1add5ad316313e490Nate Begeman  /// the underlying module.
17060789e419e04c260e36af9a1add5ad316313e490Nate Begeman  virtual void deleteModuleProvider(ModuleProvider *P,std::string *ErrInfo = 0);
17160789e419e04c260e36af9a1add5ad316313e490Nate Begeman
172fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  /// FindFunctionNamed - Search all of the active modules to find the one that
173fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  /// defines FnName.  This is very slow operation and shouldn't be used for
174fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  /// general code.
175fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  Function *FindFunctionNamed(const char *FnName);
176fe854034677f59baca1e38075e71f6efca247a03Chris Lattner
177ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner  /// runFunction - Execute the specified function with the specified arguments,
178ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner  /// and return the result.
179ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner  ///
180ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner  virtual GenericValue runFunction(Function *F,
181ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner                                const std::vector<GenericValue> &ArgValues) = 0;
182ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner
1839ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner  /// runStaticConstructorsDestructors - This method is used to execute all of
18418314dc741ab7dc4db02b199af77f43bd8551fd2Evan Cheng  /// the static constructors or destructors for a program, depending on the
1859ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner  /// value of isDtors.
1869ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner  void runStaticConstructorsDestructors(bool isDtors);
18718314dc741ab7dc4db02b199af77f43bd8551fd2Evan Cheng  /// runStaticConstructorsDestructors - This method is used to execute all of
18818314dc741ab7dc4db02b199af77f43bd8551fd2Evan Cheng  /// the static constructors or destructors for a module, depending on the
18918314dc741ab7dc4db02b199af77f43bd8551fd2Evan Cheng  /// value of isDtors.
19018314dc741ab7dc4db02b199af77f43bd8551fd2Evan Cheng  void runStaticConstructorsDestructors(Module *module, bool isDtors);
1919ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner
1929ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner
193e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner  /// runFunctionAsMain - This is a helper function which wraps runFunction to
194e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner  /// handle the common task of starting up main with the specified argc, argv,
195e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner  /// and envp parameters.
196e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner  int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
197e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner                        const char * const * envp);
198e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner
199e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner
200683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// addGlobalMapping - Tell the execution engine that the specified global is
201683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// at the specified location.  This is used internally as functions are JIT'd
202683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// and as global variables are laid out in memory.  It can and should also be
203683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// used by clients of the EE that want to have an LLVM global overlay
204552672d28516f71344d7ce5ae3edd8a251cefc42Chris Lattner  /// existing data in memory.  After adding a mapping for GV, you must not
205552672d28516f71344d7ce5ae3edd8a251cefc42Chris Lattner  /// destroy it until you've removed the mapping.
206683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  void addGlobalMapping(const GlobalValue *GV, void *Addr);
207683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner
2080982d081056f64d7245ba26368d20de33e206ff9Reid Spencer  /// clearAllGlobalMappings - Clear all global mappings and start over again
2090982d081056f64d7245ba26368d20de33e206ff9Reid Spencer  /// use in dynamic compilation scenarios when you want to move globals
210683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  void clearAllGlobalMappings();
211683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner
212f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
213f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  /// particular module, because it has been removed from the JIT.
214f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  void clearGlobalMappingsFromModule(Module *M);
215f049e07eb8930214941c72f8e4409df394de1567Nate Begeman
216f5feaf4fe381476b0ea567d7837c64b6590133e9Chris Lattner  /// updateGlobalMapping - Replace an existing mapping for GV with a new
217683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// address.  This updates both maps as required.  If "Addr" is null, the
218f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  /// entry for the global is removed from the mappings.  This returns the old
219f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  /// value of the pointer, or null if it was not in the map.
220f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
221683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner
222895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// getPointerToGlobalIfAvailable - This returns the address of the specified
223683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// global value if it is has already been codegen'd, otherwise it returns
224683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// null.
225895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  ///
226683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
22770bca51f92871c7f9d3eac9ab68292c149fab53cChris Lattner
228895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// getPointerToGlobal - This returns the address of the specified global
229552672d28516f71344d7ce5ae3edd8a251cefc42Chris Lattner  /// value.  This may involve code generation if it's a function.  After
230552672d28516f71344d7ce5ae3edd8a251cefc42Chris Lattner  /// getting a pointer to GV, it and all globals it transitively refers to have
231552672d28516f71344d7ce5ae3edd8a251cefc42Chris Lattner  /// been passed to addGlobalMapping.  You must clear the mapping for each
232552672d28516f71344d7ce5ae3edd8a251cefc42Chris Lattner  /// referred-to global before destroying it.  If a referred-to global RTG is a
233552672d28516f71344d7ce5ae3edd8a251cefc42Chris Lattner  /// function and this ExecutionEngine is a JIT compiler, calling
234552672d28516f71344d7ce5ae3edd8a251cefc42Chris Lattner  /// updateGlobalMapping(RTG, 0) will leak the function's machine code, so you
235552672d28516f71344d7ce5ae3edd8a251cefc42Chris Lattner  /// should call freeMachineCodeForFunction(RTG) instead.  Note that
236552672d28516f71344d7ce5ae3edd8a251cefc42Chris Lattner  /// optimizations can move and delete non-external GlobalValues without
237552672d28516f71344d7ce5ae3edd8a251cefc42Chris Lattner  /// notifying the ExecutionEngine.
238895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  ///
239bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner  void *getPointerToGlobal(const GlobalValue *GV);
240bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
241895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// getPointerToFunction - The different EE's represent function bodies in
242895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// different ways.  They should each implement this to say what a function
243552672d28516f71344d7ce5ae3edd8a251cefc42Chris Lattner  /// pointer should look like.  See getPointerToGlobal for the requirements on
244552672d28516f71344d7ce5ae3edd8a251cefc42Chris Lattner  /// destroying F and any GlobalValues it refers to.
245895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  ///
246fb0ef2e82cb2e80983c097100ae168af68ee8e7bBrian Gaeke  virtual void *getPointerToFunction(Function *F) = 0;
247bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
248895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// getPointerToFunctionOrStub - If the specified function has been
249895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// code-gen'd, return a pointer to the function.  If not, compile it, or use
250552672d28516f71344d7ce5ae3edd8a251cefc42Chris Lattner  /// a stub to implement lazy compilation if available.  See getPointerToGlobal
251552672d28516f71344d7ce5ae3edd8a251cefc42Chris Lattner  /// for the requirements on destroying F and any GlobalValues it refers to.
252895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  ///
25318cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  virtual void *getPointerToFunctionOrStub(Function *F) {
25418cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner    // Default implementation, just codegen the function.
25518cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner    return getPointerToFunction(F);
25618cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  }
25718cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner
258b3a847db0b991d3210706a2580428fdc2b6bf037Argyrios Kyrtzidis  // The JIT overrides a version that actually does this.
2595e0644c73e0415e85c035ad1306a86554dd26df7Bill Wendling  virtual void runJITOnFunction(Function *, MachineCodeInfo * = 0) { }
260b3a847db0b991d3210706a2580428fdc2b6bf037Argyrios Kyrtzidis
261c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// getGlobalValueAtAddress - Return the LLVM global value object that starts
262c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// at the specified address.
263c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  ///
264c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  const GlobalValue *getGlobalValueAtAddress(void *Addr);
265c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner
266c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner
267f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
268f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner                          const Type *Ty);
26929794cba810ec249a9ede5ea77333a71579fd182Brian Gaeke  void InitializeMemory(const Constant *Init, void *Addr);
27029794cba810ec249a9ede5ea77333a71579fd182Brian Gaeke
27118cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  /// recompileAndRelinkFunction - This method is used to force a function
272895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// which has already been compiled to be compiled again, possibly
27318cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  /// after it has been modified. Then the entry to the old copy is overwritten
27418cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  /// with a branch to the new copy. If there was no old copy, this acts
27518cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  /// just like VM::getPointerToFunction().
27618cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  ///
27718cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  virtual void *recompileAndRelinkFunction(Function *F) = 0;
27818cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner
279e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
280e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  /// corresponding to the machine code emitted to execute this function, useful
281e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  /// for garbage-collecting generated code.
282e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  ///
283e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  virtual void freeMachineCodeForFunction(Function *F) = 0;
284e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman
2851514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  /// getOrEmitGlobalVariable - Return the address of the specified global
2861514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  /// variable, possibly emitting it to memory if needed.  This is used by the
287552672d28516f71344d7ce5ae3edd8a251cefc42Chris Lattner  /// Emitter.  See getPointerToGlobal for the requirements on destroying GV and
288552672d28516f71344d7ce5ae3edd8a251cefc42Chris Lattner  /// any GlobalValues it refers to.
2891514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
2901514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner    return getPointerToGlobal((GlobalValue*)GV);
2911514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  }
292df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin
293df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin  /// Registers a listener to be called back on various events within
294df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin  /// the JIT.  See JITEventListener.h for more details.  Does not
295df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin  /// take ownership of the argument.  The argument may be NULL, in
296df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin  /// which case these functions do nothing.
297df225c0253aa987830fbd3dfcb5d9f8f7f973336Bill Wendling  virtual void RegisterJITEventListener(JITEventListener *) {}
298df225c0253aa987830fbd3dfcb5d9f8f7f973336Bill Wendling  virtual void UnregisterJITEventListener(JITEventListener *) {}
299df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin
3001c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner  /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation
301e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  /// is ever attempted.
302e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  void DisableLazyCompilation(bool Disabled = true) {
303e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner    LazyCompilationDisabled = Disabled;
3041c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner  }
3051c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner  bool isLazyCompilationDisabled() const {
3061c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner    return LazyCompilationDisabled;
3071c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner  }
308446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng
30977f86ad08775e0ed2a704ef09ffff3dbd6e04583Evan Cheng  /// DisableGVCompilation - If called, the JIT will abort if it's asked to
31077f86ad08775e0ed2a704ef09ffff3dbd6e04583Evan Cheng  /// allocate space and populate a GlobalVariable that is not internal to
31177f86ad08775e0ed2a704ef09ffff3dbd6e04583Evan Cheng  /// the module.
312446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng  void DisableGVCompilation(bool Disabled = true) {
313446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng    GVCompilationDisabled = Disabled;
314446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng  }
315446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng  bool isGVCompilationDisabled() const {
316446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng    return GVCompilationDisabled;
317446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng  }
318446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng
319e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
320e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  /// symbols with dlsym.  A client can still use InstallLazyFunctionCreator to
321e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  /// resolve symbols in a custom way.
322e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  void DisableSymbolSearching(bool Disabled = true) {
323e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner    SymbolSearchingDisabled = Disabled;
324e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  }
325e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  bool isSymbolSearchingDisabled() const {
326e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner    return SymbolSearchingDisabled;
327e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  }
328d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner
329d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  /// EnableDlsymStubs -
330d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  void EnableDlsymStubs(bool Enabled = true) {
331d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    DlsymStubsEnabled = Enabled;
332d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  }
333d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  bool areDlsymStubsEnabled() const {
334d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman    return DlsymStubsEnabled;
335d6b7a242d345fd79a337afd384bb586c5619cfe7Nate Begeman  }
336d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner
337d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// InstallLazyFunctionCreator - If an unknown function is needed, the
338d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// specified function pointer is invoked to create it.  If it returns null,
339d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// the JIT will abort.
340d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
341d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner    LazyFunctionCreator = P;
342d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  }
343afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
344afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  /// InstallExceptionTableRegister - The JIT will use the given function
345afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  /// to register the exception tables it generates.
346afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  static void InstallExceptionTableRegister(void (*F)(void*)) {
347afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    ExceptionTableRegister = F;
348afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
349afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
350afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  /// RegisterTable - Registers the given pointer as an exception table. It uses
351afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  /// the ExceptionTableRegister function.
352afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  static void RegisterTable(void* res) {
353afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    if (ExceptionTableRegister)
354afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      ExceptionTableRegister(res);
355afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
3561514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner
35756adf152f6354a9b5609e059050fd2315ad5960cChris Lattnerprotected:
358ded2b0d0fb0d4fa09198e3d05da529d2c97214c3Dan Gohman  explicit ExecutionEngine(ModuleProvider *P);
3599f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner
360bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner  void emitGlobals();
3613ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner
3623ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner  // EmitGlobalVariable - This method emits the specified global variable to the
3633ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner  // address specified in GlobalAddresses, or allocates new memory if it's not
3643ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner  // already in the map.
3651514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  void EmitGlobalVariable(const GlobalVariable *GV);
3663ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner
367bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner  GenericValue getConstantValue(const Constant *C);
36847567bdcb5ba3886eaf3fa5f0e0b912a24c12347Reid Spencer  void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
36947567bdcb5ba3886eaf3fa5f0e0b912a24c12347Reid Spencer                           const Type *Ty);
370bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner};
371bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
372d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
373d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
374bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner#endif
375