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
15674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H
16674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H
17bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
1843dfffe890f58468e2fe01f312978d7aa5303801Eric Christopher#include "llvm-c/ExecutionEngine.h"
19255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/ADT/DenseMap.h"
20fe854034677f59baca1e38075e71f6efca247a03Chris Lattner#include "llvm/ADT/SmallVector.h"
214688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin#include "llvm/ADT/StringRef.h"
2223e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin#include "llvm/ADT/ValueMap.h"
23255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/MC/MCCodeGenInfo.h"
2450bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper#include "llvm/Support/ErrorHandling.h"
251f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/Mutex.h"
26255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/Support/ValueHandle.h"
2798a366d547772010e94609e4584489b3e5ce0043Bill Wendling#include "llvm/Target/TargetMachine.h"
28d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne#include "llvm/Target/TargetOptions.h"
2950bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper#include <map>
3050bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper#include <string>
31255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include <vector>
32d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
33d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
34d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
359d87eb19be82b28d288e38eab3bbe145060a0701Reid Spencerstruct GenericValue;
36bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Constant;
3713a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizloclass DataLayout;
384c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskinclass ExecutionEngine;
39bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Function;
403ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattnerclass GlobalVariable;
41b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukmanclass GlobalValue;
42df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass JITEventListener;
43df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass JITMemoryManager;
44df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass MachineCodeInfo;
45bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Module;
46df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass MutexGuard;
471c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylorclass ObjectCache;
4813a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizloclass RTDyldMemoryManager;
499ea47179e647e806a2c67639bfead9d254514e59Dylan Noblesmithclass Triple;
50b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukmanclass Type;
51bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
5248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// \brief Helper class for helping synchronize access to the global address map
5348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// table.
54ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerclass ExecutionEngineState {
554c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskinpublic:
5623e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin  struct AddressMapConfig : public ValueMapConfig<const GlobalValue*> {
5723e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin    typedef ExecutionEngineState *ExtraData;
5823e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin    static sys::Mutex *getMutex(ExecutionEngineState *EES);
5923e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin    static void onDelete(ExecutionEngineState *EES, const GlobalValue *Old);
6023e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin    static void onRAUW(ExecutionEngineState *, const GlobalValue *,
6123e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin                       const GlobalValue *);
624c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  };
634c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin
6423e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin  typedef ValueMap<const GlobalValue *, void *, AddressMapConfig>
6523e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin      GlobalAddressMapTy;
6623e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin
67ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerprivate:
684c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  ExecutionEngine &EE;
694c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin
70c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// GlobalAddressMap - A mapping between LLVM global values and their
71c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// actualized version...
7223e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin  GlobalAddressMapTy GlobalAddressMap;
73c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner
74c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
75c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// used to convert raw addresses into the LLVM global value that is emitted
76c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// at the address.  This map is not computed unless getGlobalValueAtAddress
77c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// is called at some point.
780d5bd59553375dc85ac04c81ef48ef74c9e7193eJeffrey Yasskin  std::map<void *, AssertingVH<const GlobalValue> > GlobalAddressReverseMap;
79ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
80ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerpublic:
8123e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin  ExecutionEngineState(ExecutionEngine &EE);
824c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin
8348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  GlobalAddressMapTy &getGlobalAddressMap(const MutexGuard &) {
84ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer    return GlobalAddressMap;
85ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer  }
86ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
870d5bd59553375dc85ac04c81ef48ef74c9e7193eJeffrey Yasskin  std::map<void*, AssertingVH<const GlobalValue> > &
8813d57320bd212483463d4f8992d5787b29eda5dfBill Wendling  getGlobalAddressReverseMap(const MutexGuard &) {
89ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer    return GlobalAddressReverseMap;
90ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer  }
91c89d27a440370455336202b2a8f25eb9c73e67bcJeffrey Yasskin
9248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// \brief Erase an entry from the mapping table.
9348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  ///
94c5252da873d547a19069eaf9030fec203f128f66Dmitri Gribenko  /// \returns The address that \p ToUnmap was happed to.
95c89d27a440370455336202b2a8f25eb9c73e67bcJeffrey Yasskin  void *RemoveMapping(const MutexGuard &, const GlobalValue *ToUnmap);
96ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer};
97ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
9848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// \brief Abstract interface for implementation execution of LLVM modules,
9948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// designed to support both interpreter and just-in-time (JIT) compiler
10048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// implementations.
101ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerclass ExecutionEngine {
10248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// The state object holding the global address mapping, which must be
10348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// accessed synchronously.
10448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  //
10548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  // FIXME: There is no particular need the entire map needs to be
10648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  // synchronized.  Wouldn't a reader-writer design be better here?
1074c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  ExecutionEngineState EEState;
10848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar
10948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// The target data for the platform for which execution is being performed.
1103574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow  const DataLayout *TD;
11148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar
11248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// Whether lazy JIT compilation is enabled.
113dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  bool CompilingLazily;
11448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar
11548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// Whether JIT compilation of external global variables is allowed.
116446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng  bool GVCompilationDisabled;
11748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar
11848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// Whether the JIT should perform lookups of external symbols (e.g.,
11948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// using dlsym).
120e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  bool SymbolSearchingDisabled;
121d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner
1224b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  friend class EngineBuilder;  // To allow access to JITCtor and InterpCtor.
1234b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
1241514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattnerprotected:
12548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// The list of Modules that we are JIT'ing from.  We use a SmallVector to
12648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// optimize for the case where there is only one module.
127f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  SmallVector<Module*, 1> Modules;
128f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
1293574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow  void setDataLayout(const DataLayout *td) { TD = td; }
130f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
13146fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray  /// getMemoryforGV - Allocate memory for a global variable.
13248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  virtual char *getMemoryForGV(const GlobalVariable *GV);
133b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukman
134765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  // To avoid having libexecutionengine depend on the JIT and interpreter
1356d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar  // libraries, the execution engine implementations set these functions to ctor
1366d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar  // pointers at startup time if they are linked in.
1374688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  static ExecutionEngine *(*JITCtor)(
1384688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    Module *M,
1394688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    std::string *ErrorStr,
1404688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    JITMemoryManager *JMM,
1414688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    bool GVsWithCode,
142c5b28580a94e247300e5d3ccf532e153f2ae6f12Dylan Noblesmith    TargetMachine *TM);
1436d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar  static ExecutionEngine *(*MCJITCtor)(
1446d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar    Module *M,
1456d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar    std::string *ErrorStr,
14613a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo    RTDyldMemoryManager *MCJMM,
1476d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar    bool GVsWithCode,
148c5b28580a94e247300e5d3ccf532e153f2ae6f12Dylan Noblesmith    TargetMachine *TM);
149026fa8782cd59f79ac6335e54a0d1cc882a0b626Jim Grosbach  static ExecutionEngine *(*InterpCtor)(Module *M, std::string *ErrorStr);
150d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner
151d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// LazyFunctionCreator - If an unknown function is needed, this function
15248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// pointer is invoked to create it.  If this returns null, the JIT will
15348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// abort.
15448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  void *(*LazyFunctionCreator)(const std::string &);
155f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
156b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands  /// ExceptionTableRegister - If Exception Handling is set, the JIT will
157b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands  /// register dwarf tables with this function.
158afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  typedef void (*EERegisterFn)(void*);
159b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands  EERegisterFn ExceptionTableRegister;
160b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands  EERegisterFn ExceptionTableDeregister;
161515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher  /// This maps functions to their exception tables frames.
162515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher  DenseMap<const Function*, void*> AllExceptionTables;
163515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher
164afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
165bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerpublic:
16648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// lock - This lock protects the ExecutionEngine, JIT, JITResolver and
167765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  /// JITEmitter classes.  It must be held while changing the internal state of
168765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  /// any of those classes.
16948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  sys::Mutex lock;
170ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
171f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  //===--------------------------------------------------------------------===//
1729f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  //  ExecutionEngine Startup
173f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  //===--------------------------------------------------------------------===//
174ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman
1759f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  virtual ~ExecutionEngine();
176bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
1779f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  /// create - This is the factory method for creating an execution engine which
1789f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  /// is appropriate for the current machine.  This takes ownership of the
179f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// module.
18048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  ///
18148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// \param GVsWithCode - Allocating globals with code breaks
18248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// freeMachineCodeForFunction and is probably unsafe and bad for performance.
18348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// However, we have clients who depend on this behavior, so we must support
1847a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner  /// it.  Eventually, when we're willing to break some backwards compatibility,
18548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// this flag should be flipped to false, so that by default
18648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// freeMachineCodeForFunction works.
187f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  static ExecutionEngine *create(Module *M,
1889f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner                                 bool ForceInterpreter = false,
189502f20b17ede40de84503010b7699b328a4f2867Evan Cheng                                 std::string *ErrorStr = 0,
19098a366d547772010e94609e4584489b3e5ce0043Bill Wendling                                 CodeGenOpt::Level OptLevel =
191026fa8782cd59f79ac6335e54a0d1cc882a0b626Jim Grosbach                                 CodeGenOpt::Default,
192489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin                                 bool GVsWithCode = true);
193489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin
19434c9433004cabd4760987dce4804a91c84908219Chris Lattner  /// createJIT - This is the factory method for creating a JIT for the current
19534c9433004cabd4760987dce4804a91c84908219Chris Lattner  /// machine, it does not fall back to the interpreter.  This takes ownership
196f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// of the Module and JITMemoryManager if successful.
1971d929216916dc992f8643dda971aac58d47c7765Daniel Dunbar  ///
1981d929216916dc992f8643dda971aac58d47c7765Daniel Dunbar  /// Clients should make sure to initialize targets prior to calling this
1991d929216916dc992f8643dda971aac58d47c7765Daniel Dunbar  /// function.
200f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  static ExecutionEngine *createJIT(Module *M,
20134c9433004cabd4760987dce4804a91c84908219Chris Lattner                                    std::string *ErrorStr = 0,
202502f20b17ede40de84503010b7699b328a4f2867Evan Cheng                                    JITMemoryManager *JMM = 0,
20398a366d547772010e94609e4584489b3e5ce0043Bill Wendling                                    CodeGenOpt::Level OptLevel =
204026fa8782cd59f79ac6335e54a0d1cc882a0b626Jim Grosbach                                    CodeGenOpt::Default,
20588b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher                                    bool GVsWithCode = true,
206439661395fd2a2a832dba01c65bc88718528313cEvan Cheng                                    Reloc::Model RM = Reloc::Default,
2073472766f9eb7d66f234c390ce1b3a8b76f0ee9ceDuncan Sands                                    CodeModel::Model CMM =
208026fa8782cd59f79ac6335e54a0d1cc882a0b626Jim Grosbach                                    CodeModel::JITDefault);
20998a366d547772010e94609e4584489b3e5ce0043Bill Wendling
210f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// addModule - Add a Module to the list of modules that we can JIT from.
211f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// Note that this takes ownership of the Module: when the ExecutionEngine is
212f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// destroyed, it destroys the Module as well.
213f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  virtual void addModule(Module *M) {
214f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin    Modules.push_back(M);
2152e40ae4de67ba37d02281d51e7dfd629205e1e30Chris Lattner  }
216f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
21748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  //===--------------------------------------------------------------------===//
2189f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner
2193574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow  const DataLayout *getDataLayout() const { return TD; }
2209f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner
221f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// removeModule - Remove a Module from the list of modules.  Returns true if
222f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// M is found.
223f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  virtual bool removeModule(Module *M);
22460789e419e04c260e36af9a1add5ad316313e490Nate Begeman
225fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  /// FindFunctionNamed - Search all of the active modules to find the one that
226fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  /// defines FnName.  This is very slow operation and shouldn't be used for
227fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  /// general code.
228fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  Function *FindFunctionNamed(const char *FnName);
229f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
230ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner  /// runFunction - Execute the specified function with the specified arguments,
231ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner  /// and return the result.
232ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner  virtual GenericValue runFunction(Function *F,
233ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner                                const std::vector<GenericValue> &ArgValues) = 0;
234ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner
23545a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  /// getPointerToNamedFunction - This method returns the address of the
23645a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  /// specified function by using the dlsym function call.  As such it is only
23745a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  /// useful for resolving library symbols, not code generated symbols.
23845a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  ///
23945a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  /// If AbortOnFailure is false and no function with the given name is
24045a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  /// found, this function silently returns a null pointer. Otherwise,
24145a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  /// it prints a message to stderr and aborts.
24245a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  ///
24345a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  virtual void *getPointerToNamedFunction(const std::string &Name,
24445a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev                                          bool AbortOnFailure = true) = 0;
24545a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev
246020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// mapSectionAddress - map a section to its target address space value.
247020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// Map the address of a JIT section as returned from the memory manager
248020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// to the address in the target process as the running code will see it.
249020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// This is the address which will be used for relocation resolution.
250e940c1bb6c976539f07d6f440aeaacf7c25e1ddcJim Grosbach  virtual void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress) {
25150bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper    llvm_unreachable("Re-mapping of section addresses not supported with this "
25250bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper                     "EE!");
253020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  }
254020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach
255abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// finalizeObject - ensure the module is fully processed and is usable.
256abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  ///
257abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// It is the user-level function for completing the process of making the
258abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// object usable for execution.  It should be called after sections within an
259abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// object have been relocated using mapSectionAddress.  When this method is
260abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// called the MCJIT execution engine will reapply relocations for a loaded
261abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// object.  This method has no effect for the legacy JIT engine or the
262abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// interpeter.
26328989889bf3aa3314562d438aece245b71176ec4Andrew Kaylor  virtual void finalizeObject() {}
26428989889bf3aa3314562d438aece245b71176ec4Andrew Kaylor
2659ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner  /// runStaticConstructorsDestructors - This method is used to execute all of
26648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// the static constructors or destructors for a program.
26748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  ///
26848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// \param isDtors - Run the destructors instead of constructors.
2699ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner  void runStaticConstructorsDestructors(bool isDtors);
27048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar
27118314dc741ab7dc4db02b199af77f43bd8551fd2Evan Cheng  /// runStaticConstructorsDestructors - This method is used to execute all of
27248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// the static constructors or destructors for a particular module.
27348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  ///
27448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// \param isDtors - Run the destructors instead of constructors.
27518314dc741ab7dc4db02b199af77f43bd8551fd2Evan Cheng  void runStaticConstructorsDestructors(Module *module, bool isDtors);
276f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
277f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
278e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner  /// runFunctionAsMain - This is a helper function which wraps runFunction to
279e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner  /// handle the common task of starting up main with the specified argc, argv,
280e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner  /// and envp parameters.
281e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner  int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
282e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner                        const char * const * envp);
283e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner
284e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner
285683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// addGlobalMapping - Tell the execution engine that the specified global is
286683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// at the specified location.  This is used internally as functions are JIT'd
287683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// and as global variables are laid out in memory.  It can and should also be
288683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// used by clients of the EE that want to have an LLVM global overlay
2894c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  /// existing data in memory.  Mappings are automatically removed when their
2904c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  /// GlobalValue is destroyed.
291683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  void addGlobalMapping(const GlobalValue *GV, void *Addr);
292f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
29348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// clearAllGlobalMappings - Clear all global mappings and start over again,
29448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// for use in dynamic compilation scenarios to move globals.
295683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  void clearAllGlobalMappings();
296f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
297f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
298f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  /// particular module, because it has been removed from the JIT.
299f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  void clearGlobalMappingsFromModule(Module *M);
300f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
301f5feaf4fe381476b0ea567d7837c64b6590133e9Chris Lattner  /// updateGlobalMapping - Replace an existing mapping for GV with a new
302683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// address.  This updates both maps as required.  If "Addr" is null, the
303f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  /// entry for the global is removed from the mappings.  This returns the old
304f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  /// value of the pointer, or null if it was not in the map.
305f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
306f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
307895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// getPointerToGlobalIfAvailable - This returns the address of the specified
308683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// global value if it is has already been codegen'd, otherwise it returns
309683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// null.
310683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
31170bca51f92871c7f9d3eac9ab68292c149fab53cChris Lattner
312895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// getPointerToGlobal - This returns the address of the specified global
31348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// value. This may involve code generation if it's a function.
314bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner  void *getPointerToGlobal(const GlobalValue *GV);
315bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
316895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// getPointerToFunction - The different EE's represent function bodies in
317895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// different ways.  They should each implement this to say what a function
3184c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  /// pointer should look like.  When F is destroyed, the ExecutionEngine will
3197a9034c4db248fe8b8cb82762881b51b221988d3Jeffrey Yasskin  /// remove its global mapping and free any machine code.  Be sure no threads
3207a9034c4db248fe8b8cb82762881b51b221988d3Jeffrey Yasskin  /// are running inside F when that happens.
321fb0ef2e82cb2e80983c097100ae168af68ee8e7bBrian Gaeke  virtual void *getPointerToFunction(Function *F) = 0;
322bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
323f32a6a3091e0b01d17926f4b1cf78972854b8cb5Chris Lattner  /// getPointerToBasicBlock - The different EE's represent basic blocks in
324f32a6a3091e0b01d17926f4b1cf78972854b8cb5Chris Lattner  /// different ways.  Return the representation for a blockaddress of the
325f32a6a3091e0b01d17926f4b1cf78972854b8cb5Chris Lattner  /// specified block.
326f32a6a3091e0b01d17926f4b1cf78972854b8cb5Chris Lattner  virtual void *getPointerToBasicBlock(BasicBlock *BB) = 0;
327f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
328895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// getPointerToFunctionOrStub - If the specified function has been
329895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// code-gen'd, return a pointer to the function.  If not, compile it, or use
3304c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  /// a stub to implement lazy compilation if available.  See
3314c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  /// getPointerToFunction for the requirements on destroying F.
33218cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  virtual void *getPointerToFunctionOrStub(Function *F) {
33318cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner    // Default implementation, just codegen the function.
33418cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner    return getPointerToFunction(F);
33518cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  }
33618cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner
337b3a847db0b991d3210706a2580428fdc2b6bf037Argyrios Kyrtzidis  // The JIT overrides a version that actually does this.
3385e0644c73e0415e85c035ad1306a86554dd26df7Bill Wendling  virtual void runJITOnFunction(Function *, MachineCodeInfo * = 0) { }
339b3a847db0b991d3210706a2580428fdc2b6bf037Argyrios Kyrtzidis
340c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// getGlobalValueAtAddress - Return the LLVM global value object that starts
341c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// at the specified address.
342c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  ///
343c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  const GlobalValue *getGlobalValueAtAddress(void *Addr);
344c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner
34548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr.
34648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// Ptr is the address of the memory at which to store Val, cast to
34748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// GenericValue *.  It is not a pointer to a GenericValue containing the
34848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// address at which to store Val.
349f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
350db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner                          Type *Ty);
35148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar
35229794cba810ec249a9ede5ea77333a71579fd182Brian Gaeke  void InitializeMemory(const Constant *Init, void *Addr);
35329794cba810ec249a9ede5ea77333a71579fd182Brian Gaeke
35448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// recompileAndRelinkFunction - This method is used to force a function which
35548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// has already been compiled to be compiled again, possibly after it has been
35648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// modified.  Then the entry to the old copy is overwritten with a branch to
35748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// the new copy.  If there was no old copy, this acts just like
35848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// VM::getPointerToFunction().
35918cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  virtual void *recompileAndRelinkFunction(Function *F) = 0;
36018cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner
361e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
362e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  /// corresponding to the machine code emitted to execute this function, useful
363e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  /// for garbage-collecting generated code.
364e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  virtual void freeMachineCodeForFunction(Function *F) = 0;
365e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman
3661514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  /// getOrEmitGlobalVariable - Return the address of the specified global
3671514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  /// variable, possibly emitting it to memory if needed.  This is used by the
3684c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  /// Emitter.
3691514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
37072ea0c9ffaa1700730c8ce36e9b73aef4b914988Galina Kistanova    return getPointerToGlobal((const GlobalValue *)GV);
3711514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  }
372df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin
373df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin  /// Registers a listener to be called back on various events within
374df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin  /// the JIT.  See JITEventListener.h for more details.  Does not
375df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin  /// take ownership of the argument.  The argument may be NULL, in
376df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin  /// which case these functions do nothing.
377df225c0253aa987830fbd3dfcb5d9f8f7f973336Bill Wendling  virtual void RegisterJITEventListener(JITEventListener *) {}
378df225c0253aa987830fbd3dfcb5d9f8f7f973336Bill Wendling  virtual void UnregisterJITEventListener(JITEventListener *) {}
379df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin
3801c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor  /// Sets the pre-compiled object cache.  The ownership of the ObjectCache is
3811c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor  /// not changed.  Supported by MCJIT but not JIT.
3821c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor  virtual void setObjectCache(ObjectCache *) {
3831c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor    llvm_unreachable("No support for an object cache");
3841c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor  }
3851c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor
38618fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin  /// DisableLazyCompilation - When lazy compilation is off (the default), the
387dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// JIT will eagerly compile every function reachable from the argument to
388dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// getPointerToFunction.  If lazy compilation is turned on, the JIT will only
389dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// compile the one function and emit stubs to compile the rest when they're
390dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// first called.  If lazy compilation is turned off again while some lazy
391dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// stubs are still around, and one of those stubs is called, the program will
392dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// abort.
393dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  ///
394dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// In order to safely compile lazily in a threaded program, the user must
395dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// ensure that 1) only one thread at a time can call any particular lazy
396dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// stub, and 2) any thread modifying LLVM IR must hold the JIT's lock
397dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// (ExecutionEngine::lock) or otherwise ensure that no other thread calls a
398dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// lazy stub.  See http://llvm.org/PR5184 for details.
39918fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin  void DisableLazyCompilation(bool Disabled = true) {
40018fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin    CompilingLazily = !Disabled;
4011c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner  }
402dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  bool isCompilingLazily() const {
403dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin    return CompilingLazily;
4041c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner  }
40518fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin  // Deprecated in favor of isCompilingLazily (to reduce double-negatives).
40618fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin  // Remove this in LLVM 2.8.
40718fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin  bool isLazyCompilationDisabled() const {
40818fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin    return !CompilingLazily;
40918fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin  }
410446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng
41177f86ad08775e0ed2a704ef09ffff3dbd6e04583Evan Cheng  /// DisableGVCompilation - If called, the JIT will abort if it's asked to
41277f86ad08775e0ed2a704ef09ffff3dbd6e04583Evan Cheng  /// allocate space and populate a GlobalVariable that is not internal to
41377f86ad08775e0ed2a704ef09ffff3dbd6e04583Evan Cheng  /// the module.
414446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng  void DisableGVCompilation(bool Disabled = true) {
415446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng    GVCompilationDisabled = Disabled;
416446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng  }
417446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng  bool isGVCompilationDisabled() const {
418446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng    return GVCompilationDisabled;
419446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng  }
420446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng
421e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
422e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  /// symbols with dlsym.  A client can still use InstallLazyFunctionCreator to
423e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  /// resolve symbols in a custom way.
424e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  void DisableSymbolSearching(bool Disabled = true) {
425e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner    SymbolSearchingDisabled = Disabled;
426e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  }
427e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  bool isSymbolSearchingDisabled() const {
428e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner    return SymbolSearchingDisabled;
429e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  }
4306f348e458660063a40052b208bab96895c822877Jeffrey Yasskin
431d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// InstallLazyFunctionCreator - If an unknown function is needed, the
432d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// specified function pointer is invoked to create it.  If it returns null,
433d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// the JIT will abort.
434d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
435d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner    LazyFunctionCreator = P;
436d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  }
437f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
438afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  /// InstallExceptionTableRegister - The JIT will use the given function
439afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  /// to register the exception tables it generates.
440b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands  void InstallExceptionTableRegister(EERegisterFn F) {
441afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    ExceptionTableRegister = F;
442afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
443b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands  void InstallExceptionTableDeregister(EERegisterFn F) {
444b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands    ExceptionTableDeregister = F;
445b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands  }
446f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
44748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// RegisterTable - Registers the given pointer as an exception table.  It
44848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// uses the ExceptionTableRegister function.
449515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher  void RegisterTable(const Function *fn, void* res) {
450b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands    if (ExceptionTableRegister) {
451afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      ExceptionTableRegister(res);
452515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher      AllExceptionTables[fn] = res;
453515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher    }
454515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher  }
455515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher
456f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach  /// DeregisterTable - Deregisters the exception frame previously registered
457f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach  /// for the given function.
458515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher  void DeregisterTable(const Function *Fn) {
459515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher    if (ExceptionTableDeregister) {
460f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach      DenseMap<const Function*, void*>::iterator frame =
461f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach        AllExceptionTables.find(Fn);
462515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher      if(frame != AllExceptionTables.end()) {
463515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher        ExceptionTableDeregister(frame->second);
464515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher        AllExceptionTables.erase(frame);
465515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher      }
466b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands    }
467afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
4681514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner
469b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands  /// DeregisterAllTables - Deregisters all previously registered pointers to an
47048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// exception tables.  It uses the ExceptionTableoDeregister function.
471b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands  void DeregisterAllTables();
472b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands
47356adf152f6354a9b5609e059050fd2315ad5960cChris Lattnerprotected:
474f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  explicit ExecutionEngine(Module *M);
4759f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner
476bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner  void emitGlobals();
4773ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner
4781514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  void EmitGlobalVariable(const GlobalVariable *GV);
4793ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner
480bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner  GenericValue getConstantValue(const Constant *C);
481f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach  void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
482db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner                           Type *Ty);
483bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner};
484bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
4854b1511b027ce0b648b3379f2891816c25b46f515Reid Klecknernamespace EngineKind {
4864b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  // These are actually bitmasks that get or-ed together.
4874b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  enum Kind {
4884b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    JIT         = 0x1,
4894b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    Interpreter = 0x2
4904b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  };
4914b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  const static Kind Either = (Kind)(JIT | Interpreter);
4924b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner}
4934b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
4944b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner/// EngineBuilder - Builder class for ExecutionEngines.  Use this by
4954b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner/// stack-allocating a builder, chaining the various set* methods, and
4964b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner/// terminating it with a .create() call.
4974b1511b027ce0b648b3379f2891816c25b46f515Reid Klecknerclass EngineBuilder {
49848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbarprivate:
499f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  Module *M;
5004b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  EngineKind::Kind WhichEngine;
5014b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  std::string *ErrorStr;
5024b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  CodeGenOpt::Level OptLevel;
50313a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  RTDyldMemoryManager *MCJMM;
5044b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  JITMemoryManager *JMM;
5054b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  bool AllocateGVsWithCode;
506d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne  TargetOptions Options;
507439661395fd2a2a832dba01c65bc88718528313cEvan Cheng  Reloc::Model RelocModel;
50888b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher  CodeModel::Model CMModel;
5094688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  std::string MArch;
5104688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  std::string MCPU;
5114688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  SmallVector<std::string, 4> MAttrs;
5126d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar  bool UseMCJIT;
5134b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
5144b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// InitEngine - Does the common initialization of default options.
5154b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  void InitEngine() {
5164b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    WhichEngine = EngineKind::Either;
5174b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    ErrorStr = NULL;
5184b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    OptLevel = CodeGenOpt::Default;
51913a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo    MCJMM = NULL;
5204b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    JMM = NULL;
521d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne    Options = TargetOptions();
5224b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    AllocateGVsWithCode = false;
523439661395fd2a2a832dba01c65bc88718528313cEvan Cheng    RelocModel = Reloc::Default;
52434ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng    CMModel = CodeModel::JITDefault;
5256d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar    UseMCJIT = false;
5264b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  }
5274b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
52848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbarpublic:
5294b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// EngineBuilder - Constructor for EngineBuilder.  If create() is called and
530f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// is successful, the created engine takes ownership of the module.
531f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  EngineBuilder(Module *m) : M(m) {
5324b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    InitEngine();
5334b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  }
5344b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
5354b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// setEngineKind - Controls whether the user wants the interpreter, the JIT,
5364b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// or whichever engine works.  This option defaults to EngineKind::Either.
5374b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  EngineBuilder &setEngineKind(EngineKind::Kind w) {
5384b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    WhichEngine = w;
5394b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    return *this;
5404b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  }
54113a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo
54213a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// setMCJITMemoryManager - Sets the MCJIT memory manager to use. This allows
54313a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// clients to customize their memory allocation policies for the MCJIT. This
54413a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// is only appropriate for the MCJIT; setting this and configuring the builder
54513a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// to create anything other than MCJIT will cause a runtime error. If create()
54613a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// is called and is successful, the created engine takes ownership of the
54713a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// memory manager. This option defaults to NULL. Using this option nullifies
54813a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// the setJITMemoryManager() option.
54913a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  EngineBuilder &setMCJITMemoryManager(RTDyldMemoryManager *mcjmm) {
55013a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo    MCJMM = mcjmm;
55113a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo    JMM = NULL;
55213a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo    return *this;
55313a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  }
5544b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
55513a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// setJITMemoryManager - Sets the JIT memory manager to use.  This allows
55613a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// clients to customize their memory allocation policies.  This is only
55713a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// appropriate for either JIT or MCJIT; setting this and configuring the
55813a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// builder to create an interpreter will cause a runtime error. If create()
55913a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// is called and is successful, the created engine takes ownership of the
56013a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// memory manager.  This option defaults to NULL. This option overrides
56113a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// setMCJITMemoryManager() as well.
5624b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  EngineBuilder &setJITMemoryManager(JITMemoryManager *jmm) {
56313a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo    MCJMM = NULL;
5644b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    JMM = jmm;
5654b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    return *this;
5664b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  }
5674b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
5684b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// setErrorStr - Set the error string to write to on error.  This option
5694b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// defaults to NULL.
5704b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  EngineBuilder &setErrorStr(std::string *e) {
5714b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    ErrorStr = e;
5724b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    return *this;
5734b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  }
5744b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
5754b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// setOptLevel - Set the optimization level for the JIT.  This option
5764b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// defaults to CodeGenOpt::Default.
5774b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  EngineBuilder &setOptLevel(CodeGenOpt::Level l) {
5784b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    OptLevel = l;
5794b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    return *this;
5804b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  }
5814b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
582d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne  /// setTargetOptions - Set the target options that the ExecutionEngine
583d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne  /// target is using. Defaults to TargetOptions().
584d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne  EngineBuilder &setTargetOptions(const TargetOptions &Opts) {
585d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne    Options = Opts;
586d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne    return *this;
587d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne  }
588d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne
589439661395fd2a2a832dba01c65bc88718528313cEvan Cheng  /// setRelocationModel - Set the relocation model that the ExecutionEngine
590439661395fd2a2a832dba01c65bc88718528313cEvan Cheng  /// target is using. Defaults to target specific default "Reloc::Default".
591439661395fd2a2a832dba01c65bc88718528313cEvan Cheng  EngineBuilder &setRelocationModel(Reloc::Model RM) {
592439661395fd2a2a832dba01c65bc88718528313cEvan Cheng    RelocModel = RM;
593439661395fd2a2a832dba01c65bc88718528313cEvan Cheng    return *this;
594439661395fd2a2a832dba01c65bc88718528313cEvan Cheng  }
595439661395fd2a2a832dba01c65bc88718528313cEvan Cheng
59688b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher  /// setCodeModel - Set the CodeModel that the ExecutionEngine target
59734ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng  /// data is using. Defaults to target specific default
59834ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng  /// "CodeModel::JITDefault".
59988b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher  EngineBuilder &setCodeModel(CodeModel::Model M) {
60088b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher    CMModel = M;
60188b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher    return *this;
60288b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher  }
60388b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher
6044b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// setAllocateGVsWithCode - Sets whether global values should be allocated
6054b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// into the same buffer as code.  For most applications this should be set
6064b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// to false.  Allocating globals with code breaks freeMachineCodeForFunction
6074b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// and is probably unsafe and bad for performance.  However, we have clients
6084b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// who depend on this behavior, so we must support it.  This option defaults
6094b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// to false so that users of the new API can safely use the new memory
6104b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// manager and free machine code.
6114b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  EngineBuilder &setAllocateGVsWithCode(bool a) {
6124b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    AllocateGVsWithCode = a;
6134b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    return *this;
6144b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  }
6154b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
6164688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  /// setMArch - Override the architecture set by the Module's triple.
6174688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  EngineBuilder &setMArch(StringRef march) {
6184688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    MArch.assign(march.begin(), march.end());
6194688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    return *this;
6204688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  }
6214688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin
6224688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  /// setMCPU - Target a specific cpu type.
6234688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  EngineBuilder &setMCPU(StringRef mcpu) {
6244688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    MCPU.assign(mcpu.begin(), mcpu.end());
6254688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    return *this;
6264688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  }
6274688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin
6286d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar  /// setUseMCJIT - Set whether the MC-JIT implementation should be used
6296d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar  /// (experimental).
630714b34fc24d5df02aff01bd64eb80e5945d663acJim Grosbach  EngineBuilder &setUseMCJIT(bool Value) {
6316d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar    UseMCJIT = Value;
632714b34fc24d5df02aff01bd64eb80e5945d663acJim Grosbach    return *this;
6336d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar  }
6346d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar
6354688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  /// setMAttrs - Set cpu-specific attributes.
6364688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  template<typename StringSequence>
6374688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  EngineBuilder &setMAttrs(const StringSequence &mattrs) {
6384688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    MAttrs.clear();
6394688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    MAttrs.append(mattrs.begin(), mattrs.end());
6404688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    return *this;
6414688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  }
6424688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin
6438e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson  TargetMachine *selectTarget();
6448e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson
6452ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith  /// selectTarget - Pick a target either via -march or by guessing the native
6462ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith  /// arch.  Add any CPU features specified via -mcpu or -mattr.
6478e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson  TargetMachine *selectTarget(const Triple &TargetTriple,
6488e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson                              StringRef MArch,
6498e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson                              StringRef MCPU,
6508e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson                              const SmallVectorImpl<std::string>& MAttrs);
6518e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson
6528e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson  ExecutionEngine *create() {
6538e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson    return create(selectTarget());
6548e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson  }
6558e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson
6568e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson  ExecutionEngine *create(TargetMachine *TM);
6574b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner};
6584b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
65940be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo// Create wrappers for C Binding types (see CBindingWrapping.h).
66040be1e85665d10f5444186f0e7106e368dd735b8Filip PizloDEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionEngine, LLVMExecutionEngineRef)
66143dfffe890f58468e2fe01f312978d7aa5303801Eric Christopher
662d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
663d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
664bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner#endif
665