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"
19fe854034677f59baca1e38075e71f6efca247a03Chris Lattner#include "llvm/ADT/SmallVector.h"
204688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin#include "llvm/ADT/StringRef.h"
2136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/ValueHandle.h"
2236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/ValueMap.h"
23255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/MC/MCCodeGenInfo.h"
2450bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper#include "llvm/Support/ErrorHandling.h"
251f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/Mutex.h"
2698a366d547772010e94609e4584489b3e5ce0043Bill Wendling#include "llvm/Target/TargetMachine.h"
27d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne#include "llvm/Target/TargetOptions.h"
2850bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper#include <map>
2950bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper#include <string>
30255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include <vector>
31d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
32d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
33d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
349d87eb19be82b28d288e38eab3bbe145060a0701Reid Spencerstruct GenericValue;
35bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Constant;
3613a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizloclass DataLayout;
374c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskinclass ExecutionEngine;
38bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Function;
393ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattnerclass GlobalVariable;
40b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukmanclass GlobalValue;
41df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass JITEventListener;
42df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass JITMemoryManager;
43df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass MachineCodeInfo;
44bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Module;
45df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass MutexGuard;
461c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylorclass ObjectCache;
4713a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizloclass RTDyldMemoryManager;
489ea47179e647e806a2c67639bfead9d254514e59Dylan Noblesmithclass Triple;
49b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukmanclass Type;
50bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
5136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesnamespace object {
5236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  class Archive;
5336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  class ObjectFile;
5436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines}
5536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
5648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// \brief Helper class for helping synchronize access to the global address map
57cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines/// table.  Access to this class should be serialized under a mutex.
58ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerclass ExecutionEngineState {
594c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskinpublic:
6023e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin  struct AddressMapConfig : public ValueMapConfig<const GlobalValue*> {
6123e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin    typedef ExecutionEngineState *ExtraData;
6223e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin    static sys::Mutex *getMutex(ExecutionEngineState *EES);
6323e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin    static void onDelete(ExecutionEngineState *EES, const GlobalValue *Old);
6423e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin    static void onRAUW(ExecutionEngineState *, const GlobalValue *,
6523e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin                       const GlobalValue *);
664c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  };
674c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin
6823e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin  typedef ValueMap<const GlobalValue *, void *, AddressMapConfig>
6923e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin      GlobalAddressMapTy;
7023e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin
71ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerprivate:
724c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  ExecutionEngine &EE;
734c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin
74c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// GlobalAddressMap - A mapping between LLVM global values and their
75c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// actualized version...
7623e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin  GlobalAddressMapTy GlobalAddressMap;
77c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner
78c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
79c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// used to convert raw addresses into the LLVM global value that is emitted
80c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// at the address.  This map is not computed unless getGlobalValueAtAddress
81c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// is called at some point.
820d5bd59553375dc85ac04c81ef48ef74c9e7193eJeffrey Yasskin  std::map<void *, AssertingVH<const GlobalValue> > GlobalAddressReverseMap;
83ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
84ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerpublic:
8523e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin  ExecutionEngineState(ExecutionEngine &EE);
864c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin
87cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  GlobalAddressMapTy &getGlobalAddressMap() {
88ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer    return GlobalAddressMap;
89ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer  }
90ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
910d5bd59553375dc85ac04c81ef48ef74c9e7193eJeffrey Yasskin  std::map<void*, AssertingVH<const GlobalValue> > &
92cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  getGlobalAddressReverseMap() {
93ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer    return GlobalAddressReverseMap;
94ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer  }
95c89d27a440370455336202b2a8f25eb9c73e67bcJeffrey Yasskin
9648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// \brief Erase an entry from the mapping table.
9748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  ///
98c5252da873d547a19069eaf9030fec203f128f66Dmitri Gribenko  /// \returns The address that \p ToUnmap was happed to.
99cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  void *RemoveMapping(const GlobalValue *ToUnmap);
100ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer};
101ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
10248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// \brief Abstract interface for implementation execution of LLVM modules,
10348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// designed to support both interpreter and just-in-time (JIT) compiler
10448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// implementations.
105ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerclass ExecutionEngine {
10648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// The state object holding the global address mapping, which must be
10748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// accessed synchronously.
10848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  //
10948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  // FIXME: There is no particular need the entire map needs to be
11048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  // synchronized.  Wouldn't a reader-writer design be better here?
1114c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  ExecutionEngineState EEState;
11248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar
11348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// The target data for the platform for which execution is being performed.
11436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  const DataLayout *DL;
11548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar
11648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// Whether lazy JIT compilation is enabled.
117dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  bool CompilingLazily;
11848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar
11948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// Whether JIT compilation of external global variables is allowed.
120446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng  bool GVCompilationDisabled;
12148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar
12248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// Whether the JIT should perform lookups of external symbols (e.g.,
12348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// using dlsym).
124e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  bool SymbolSearchingDisabled;
125d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner
126dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// Whether the JIT should verify IR modules during compilation.
127dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  bool VerifyModules;
128dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
1294b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  friend class EngineBuilder;  // To allow access to JITCtor and InterpCtor.
1304b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
1311514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattnerprotected:
13248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// The list of Modules that we are JIT'ing from.  We use a SmallVector to
13348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// optimize for the case where there is only one module.
134f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  SmallVector<Module*, 1> Modules;
135f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
13636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void setDataLayout(const DataLayout *Val) { DL = Val; }
137f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
13846fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray  /// getMemoryforGV - Allocate memory for a global variable.
13948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  virtual char *getMemoryForGV(const GlobalVariable *GV);
140b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukman
141765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  // To avoid having libexecutionengine depend on the JIT and interpreter
1426d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar  // libraries, the execution engine implementations set these functions to ctor
1436d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar  // pointers at startup time if they are linked in.
1444688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  static ExecutionEngine *(*JITCtor)(
1454688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    Module *M,
1464688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    std::string *ErrorStr,
1474688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    JITMemoryManager *JMM,
1484688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    bool GVsWithCode,
149c5b28580a94e247300e5d3ccf532e153f2ae6f12Dylan Noblesmith    TargetMachine *TM);
1506d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar  static ExecutionEngine *(*MCJITCtor)(
1516d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar    Module *M,
1526d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar    std::string *ErrorStr,
15313a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo    RTDyldMemoryManager *MCJMM,
1546d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar    bool GVsWithCode,
155c5b28580a94e247300e5d3ccf532e153f2ae6f12Dylan Noblesmith    TargetMachine *TM);
156026fa8782cd59f79ac6335e54a0d1cc882a0b626Jim Grosbach  static ExecutionEngine *(*InterpCtor)(Module *M, std::string *ErrorStr);
157d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner
158d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// LazyFunctionCreator - If an unknown function is needed, this function
15948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// pointer is invoked to create it.  If this returns null, the JIT will
16048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// abort.
16148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  void *(*LazyFunctionCreator)(const std::string &);
162f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
163bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerpublic:
1646169453ba37ac353655f2475f336e66f31276752Andrew Kaylor  /// lock - This lock protects the ExecutionEngine, MCJIT, JIT, JITResolver and
165765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  /// JITEmitter classes.  It must be held while changing the internal state of
166765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner  /// any of those classes.
16748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  sys::Mutex lock;
168ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer
169f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  //===--------------------------------------------------------------------===//
1709f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  //  ExecutionEngine Startup
171f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  //===--------------------------------------------------------------------===//
172ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman
1739f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  virtual ~ExecutionEngine();
174bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
1759f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  /// create - This is the factory method for creating an execution engine which
1769f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner  /// is appropriate for the current machine.  This takes ownership of the
177f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// module.
17848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  ///
17948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// \param GVsWithCode - Allocating globals with code breaks
18048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// freeMachineCodeForFunction and is probably unsafe and bad for performance.
18148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// However, we have clients who depend on this behavior, so we must support
1827a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner  /// it.  Eventually, when we're willing to break some backwards compatibility,
18348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// this flag should be flipped to false, so that by default
18448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// freeMachineCodeForFunction works.
185f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  static ExecutionEngine *create(Module *M,
1869f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner                                 bool ForceInterpreter = false,
187dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                 std::string *ErrorStr = nullptr,
18898a366d547772010e94609e4584489b3e5ce0043Bill Wendling                                 CodeGenOpt::Level OptLevel =
189026fa8782cd59f79ac6335e54a0d1cc882a0b626Jim Grosbach                                 CodeGenOpt::Default,
190489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin                                 bool GVsWithCode = true);
191489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin
19234c9433004cabd4760987dce4804a91c84908219Chris Lattner  /// createJIT - This is the factory method for creating a JIT for the current
19334c9433004cabd4760987dce4804a91c84908219Chris Lattner  /// machine, it does not fall back to the interpreter.  This takes ownership
194f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// of the Module and JITMemoryManager if successful.
1951d929216916dc992f8643dda971aac58d47c7765Daniel Dunbar  ///
1961d929216916dc992f8643dda971aac58d47c7765Daniel Dunbar  /// Clients should make sure to initialize targets prior to calling this
1971d929216916dc992f8643dda971aac58d47c7765Daniel Dunbar  /// function.
198f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  static ExecutionEngine *createJIT(Module *M,
199dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                    std::string *ErrorStr = nullptr,
200dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                    JITMemoryManager *JMM = nullptr,
20198a366d547772010e94609e4584489b3e5ce0043Bill Wendling                                    CodeGenOpt::Level OptLevel =
202026fa8782cd59f79ac6335e54a0d1cc882a0b626Jim Grosbach                                    CodeGenOpt::Default,
20388b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher                                    bool GVsWithCode = true,
204439661395fd2a2a832dba01c65bc88718528313cEvan Cheng                                    Reloc::Model RM = Reloc::Default,
2053472766f9eb7d66f234c390ce1b3a8b76f0ee9ceDuncan Sands                                    CodeModel::Model CMM =
206026fa8782cd59f79ac6335e54a0d1cc882a0b626Jim Grosbach                                    CodeModel::JITDefault);
20798a366d547772010e94609e4584489b3e5ce0043Bill Wendling
208f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// addModule - Add a Module to the list of modules that we can JIT from.
209f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// Note that this takes ownership of the Module: when the ExecutionEngine is
210f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// destroyed, it destroys the Module as well.
211f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  virtual void addModule(Module *M) {
212f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin    Modules.push_back(M);
2132e40ae4de67ba37d02281d51e7dfd629205e1e30Chris Lattner  }
214f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
21536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// addObjectFile - Add an ObjectFile to the execution engine.
21636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ///
21736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// This method is only supported by MCJIT.  MCJIT will immediately load the
21836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// object into memory and adds its symbols to the list used to resolve
21936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// external symbols while preparing other objects for execution.
22036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ///
22136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// Objects added using this function will not be made executable until
22236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// needed by another object.
22336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ///
22436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// MCJIT will take ownership of the ObjectFile.
225dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  virtual void addObjectFile(std::unique_ptr<object::ObjectFile> O);
22636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
22736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// addArchive - Add an Archive to the execution engine.
22836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ///
22936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// This method is only supported by MCJIT.  MCJIT will use the archive to
23036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// resolve external symbols in objects it is loading.  If a symbol is found
23136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// in the Archive the contained object file will be extracted (in memory)
23236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// and loaded for possible execution.
23336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ///
23436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// MCJIT will take ownership of the Archive.
23536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  virtual void addArchive(object::Archive *A) {
23636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    llvm_unreachable("ExecutionEngine subclass doesn't implement addArchive.");
23736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
23836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
23948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  //===--------------------------------------------------------------------===//
2409f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner
24136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  const DataLayout *getDataLayout() const { return DL; }
2429f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner
243f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// removeModule - Remove a Module from the list of modules.  Returns true if
244f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// M is found.
245f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  virtual bool removeModule(Module *M);
24660789e419e04c260e36af9a1add5ad316313e490Nate Begeman
247fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  /// FindFunctionNamed - Search all of the active modules to find the one that
248fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  /// defines FnName.  This is very slow operation and shouldn't be used for
249fe854034677f59baca1e38075e71f6efca247a03Chris Lattner  /// general code.
2502ad18efdc73bc2356aa7fbf811d5ecbbaac0f2c9Andrew Kaylor  virtual Function *FindFunctionNamed(const char *FnName);
251f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
252ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner  /// runFunction - Execute the specified function with the specified arguments,
253ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner  /// and return the result.
254ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner  virtual GenericValue runFunction(Function *F,
255ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner                                const std::vector<GenericValue> &ArgValues) = 0;
256ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner
25745a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  /// getPointerToNamedFunction - This method returns the address of the
25845a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  /// specified function by using the dlsym function call.  As such it is only
25945a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  /// useful for resolving library symbols, not code generated symbols.
26045a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  ///
26145a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  /// If AbortOnFailure is false and no function with the given name is
26245a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  /// found, this function silently returns a null pointer. Otherwise,
26345a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  /// it prints a message to stderr and aborts.
26445a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  ///
2658e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// This function is deprecated for the MCJIT execution engine.
2668e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  ///
26736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// FIXME: the JIT and MCJIT interfaces should be disentangled or united
2682ad18efdc73bc2356aa7fbf811d5ecbbaac0f2c9Andrew Kaylor  /// again, if possible.
2692ad18efdc73bc2356aa7fbf811d5ecbbaac0f2c9Andrew Kaylor  ///
27045a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev  virtual void *getPointerToNamedFunction(const std::string &Name,
27145a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev                                          bool AbortOnFailure = true) = 0;
27245a93d6eb4c55876e94f91b471657f23fe2149aaDanil Malyshev
273020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// mapSectionAddress - map a section to its target address space value.
274020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// Map the address of a JIT section as returned from the memory manager
275020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// to the address in the target process as the running code will see it.
276020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  /// This is the address which will be used for relocation resolution.
277e940c1bb6c976539f07d6f440aeaacf7c25e1ddcJim Grosbach  virtual void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress) {
27850bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper    llvm_unreachable("Re-mapping of section addresses not supported with this "
27950bee42b54cd9aec5f49566307df2b0cf23afcf6Craig Topper                     "EE!");
280020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach  }
281020f4e861a9a32059f76377e787703c92ba55a98Jim Grosbach
2828e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// generateCodeForModule - Run code generationen for the specified module and
2838e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// load it into memory.
2848e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  ///
2858e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// When this function has completed, all code and data for the specified
2868e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// module, and any module on which this module depends, will be generated
2878e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// and loaded into memory, but relocations will not yet have been applied
2888e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// and all memory will be readable and writable but not executable.
2898e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  ///
2908e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// This function is primarily useful when generating code for an external
2918e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// target, allowing the client an opportunity to remap section addresses
2928e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// before relocations are applied.  Clients that intend to execute code
2938e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// locally can use the getFunctionAddress call, which will generate code
2948e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// and apply final preparations all in one step.
2958e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  ///
2968e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// This method has no effect for the legacy JIT engine or the interpeter.
2978e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  virtual void generateCodeForModule(Module *M) {}
2988e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor
299abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// finalizeObject - ensure the module is fully processed and is usable.
300abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  ///
301abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// It is the user-level function for completing the process of making the
302abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// object usable for execution.  It should be called after sections within an
303abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// object have been relocated using mapSectionAddress.  When this method is
304abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// called the MCJIT execution engine will reapply relocations for a loaded
305abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// object.  This method has no effect for the legacy JIT engine or the
306abb38fe8dec11b1ea7535f84fac8ad0f0af70addDavid Tweed  /// interpeter.
30728989889bf3aa3314562d438aece245b71176ec4Andrew Kaylor  virtual void finalizeObject() {}
30828989889bf3aa3314562d438aece245b71176ec4Andrew Kaylor
3099ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner  /// runStaticConstructorsDestructors - This method is used to execute all of
31048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// the static constructors or destructors for a program.
31148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  ///
31248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// \param isDtors - Run the destructors instead of constructors.
3132ad18efdc73bc2356aa7fbf811d5ecbbaac0f2c9Andrew Kaylor  virtual void runStaticConstructorsDestructors(bool isDtors);
31448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar
31518314dc741ab7dc4db02b199af77f43bd8551fd2Evan Cheng  /// runStaticConstructorsDestructors - This method is used to execute all of
31648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// the static constructors or destructors for a particular module.
31748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  ///
31848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// \param isDtors - Run the destructors instead of constructors.
31918314dc741ab7dc4db02b199af77f43bd8551fd2Evan Cheng  void runStaticConstructorsDestructors(Module *module, bool isDtors);
320f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
321f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
322e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner  /// runFunctionAsMain - This is a helper function which wraps runFunction to
323e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner  /// handle the common task of starting up main with the specified argc, argv,
324e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner  /// and envp parameters.
325e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner  int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
326e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner                        const char * const * envp);
327e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner
328e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner
329683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// addGlobalMapping - Tell the execution engine that the specified global is
330683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// at the specified location.  This is used internally as functions are JIT'd
331683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// and as global variables are laid out in memory.  It can and should also be
332683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// used by clients of the EE that want to have an LLVM global overlay
3334c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  /// existing data in memory.  Mappings are automatically removed when their
3344c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  /// GlobalValue is destroyed.
335683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  void addGlobalMapping(const GlobalValue *GV, void *Addr);
336f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
33748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// clearAllGlobalMappings - Clear all global mappings and start over again,
33848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// for use in dynamic compilation scenarios to move globals.
339683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  void clearAllGlobalMappings();
340f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
341f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
342f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  /// particular module, because it has been removed from the JIT.
343f049e07eb8930214941c72f8e4409df394de1567Nate Begeman  void clearGlobalMappingsFromModule(Module *M);
344f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
345f5feaf4fe381476b0ea567d7837c64b6590133e9Chris Lattner  /// updateGlobalMapping - Replace an existing mapping for GV with a new
346683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// address.  This updates both maps as required.  If "Addr" is null, the
347f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  /// entry for the global is removed from the mappings.  This returns the old
348f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  /// value of the pointer, or null if it was not in the map.
349f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  void *updateGlobalMapping(const GlobalValue *GV, void *Addr);
350f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
351895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// getPointerToGlobalIfAvailable - This returns the address of the specified
352683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// global value if it is has already been codegen'd, otherwise it returns
353683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  /// null.
3548e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  ///
3558e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// This function is deprecated for the MCJIT execution engine.  It doesn't
3568e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// seem to be needed in that case, but an equivalent can be added if it is.
357683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner  void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
35870bca51f92871c7f9d3eac9ab68292c149fab53cChris Lattner
359895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// getPointerToGlobal - This returns the address of the specified global
36048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// value. This may involve code generation if it's a function.
3618e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  ///
3628e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// This function is deprecated for the MCJIT execution engine.  Use
3638e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// getGlobalValueAddress instead.
364bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner  void *getPointerToGlobal(const GlobalValue *GV);
365bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
366895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// getPointerToFunction - The different EE's represent function bodies in
367895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// different ways.  They should each implement this to say what a function
3684c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  /// pointer should look like.  When F is destroyed, the ExecutionEngine will
3697a9034c4db248fe8b8cb82762881b51b221988d3Jeffrey Yasskin  /// remove its global mapping and free any machine code.  Be sure no threads
3707a9034c4db248fe8b8cb82762881b51b221988d3Jeffrey Yasskin  /// are running inside F when that happens.
3718e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  ///
3728e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// This function is deprecated for the MCJIT execution engine.  Use
3738e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// getFunctionAddress instead.
374fb0ef2e82cb2e80983c097100ae168af68ee8e7bBrian Gaeke  virtual void *getPointerToFunction(Function *F) = 0;
375bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
376f32a6a3091e0b01d17926f4b1cf78972854b8cb5Chris Lattner  /// getPointerToBasicBlock - The different EE's represent basic blocks in
377f32a6a3091e0b01d17926f4b1cf78972854b8cb5Chris Lattner  /// different ways.  Return the representation for a blockaddress of the
378f32a6a3091e0b01d17926f4b1cf78972854b8cb5Chris Lattner  /// specified block.
3798e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  ///
3808e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// This function will not be implemented for the MCJIT execution engine.
381f32a6a3091e0b01d17926f4b1cf78972854b8cb5Chris Lattner  virtual void *getPointerToBasicBlock(BasicBlock *BB) = 0;
382f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
383895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// getPointerToFunctionOrStub - If the specified function has been
384895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner  /// code-gen'd, return a pointer to the function.  If not, compile it, or use
3854c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  /// a stub to implement lazy compilation if available.  See
3864c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  /// getPointerToFunction for the requirements on destroying F.
3878e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  ///
3888e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// This function is deprecated for the MCJIT execution engine.  Use
3898e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// getFunctionAddress instead.
39018cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  virtual void *getPointerToFunctionOrStub(Function *F) {
39118cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner    // Default implementation, just codegen the function.
39218cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner    return getPointerToFunction(F);
39318cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  }
39418cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner
3958e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// getGlobalValueAddress - Return the address of the specified global
3968e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// value. This may involve code generation.
3978e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  ///
3988e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// This function should not be called with the JIT or interpreter engines.
3998e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  virtual uint64_t getGlobalValueAddress(const std::string &Name) {
4008e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor    // Default implementation for JIT and interpreter.  MCJIT will override this.
4018e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor    // JIT and interpreter clients should use getPointerToGlobal instead.
4028e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor    return 0;
4038e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  }
4048e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor
4058e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// getFunctionAddress - Return the address of the specified function.
4068e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// This may involve code generation.
4078e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  virtual uint64_t getFunctionAddress(const std::string &Name) {
4088e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor    // Default implementation for JIT and interpreter.  MCJIT will override this.
4098e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor    // JIT and interpreter clients should use getPointerToFunction instead.
4108e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor    return 0;
4118e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  }
4128e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor
413b3a847db0b991d3210706a2580428fdc2b6bf037Argyrios Kyrtzidis  // The JIT overrides a version that actually does this.
414dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  virtual void runJITOnFunction(Function *, MachineCodeInfo * = nullptr) { }
415b3a847db0b991d3210706a2580428fdc2b6bf037Argyrios Kyrtzidis
416c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// getGlobalValueAtAddress - Return the LLVM global value object that starts
417c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  /// at the specified address.
418c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  ///
419c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner  const GlobalValue *getGlobalValueAtAddress(void *Addr);
420c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner
42148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr.
42248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// Ptr is the address of the memory at which to store Val, cast to
42348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// GenericValue *.  It is not a pointer to a GenericValue containing the
42448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// address at which to store Val.
425f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner  void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
426db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner                          Type *Ty);
42748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar
42829794cba810ec249a9ede5ea77333a71579fd182Brian Gaeke  void InitializeMemory(const Constant *Init, void *Addr);
42929794cba810ec249a9ede5ea77333a71579fd182Brian Gaeke
43048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// recompileAndRelinkFunction - This method is used to force a function which
43148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// has already been compiled to be compiled again, possibly after it has been
43248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// modified.  Then the entry to the old copy is overwritten with a branch to
43348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// the new copy.  If there was no old copy, this acts just like
43448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar  /// VM::getPointerToFunction().
43518cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner  virtual void *recompileAndRelinkFunction(Function *F) = 0;
43618cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner
437e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  /// freeMachineCodeForFunction - Release memory in the ExecutionEngine
438e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  /// corresponding to the machine code emitted to execute this function, useful
439e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  /// for garbage-collecting generated code.
440e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman  virtual void freeMachineCodeForFunction(Function *F) = 0;
441e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman
4421514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  /// getOrEmitGlobalVariable - Return the address of the specified global
4431514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  /// variable, possibly emitting it to memory if needed.  This is used by the
4444c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin  /// Emitter.
4458e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  ///
4468e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// This function is deprecated for the MCJIT execution engine.  Use
4478e9ec015348c5419b905c2ca6e39534429eda073Andrew Kaylor  /// getGlobalValueAddress instead.
4481514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
44972ea0c9ffaa1700730c8ce36e9b73aef4b914988Galina Kistanova    return getPointerToGlobal((const GlobalValue *)GV);
4501514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  }
451df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin
452df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin  /// Registers a listener to be called back on various events within
453df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin  /// the JIT.  See JITEventListener.h for more details.  Does not
454df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin  /// take ownership of the argument.  The argument may be NULL, in
455df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin  /// which case these functions do nothing.
456df225c0253aa987830fbd3dfcb5d9f8f7f973336Bill Wendling  virtual void RegisterJITEventListener(JITEventListener *) {}
457df225c0253aa987830fbd3dfcb5d9f8f7f973336Bill Wendling  virtual void UnregisterJITEventListener(JITEventListener *) {}
458df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin
4591c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor  /// Sets the pre-compiled object cache.  The ownership of the ObjectCache is
4601c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor  /// not changed.  Supported by MCJIT but not JIT.
4611c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor  virtual void setObjectCache(ObjectCache *) {
4621c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor    llvm_unreachable("No support for an object cache");
4631c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor  }
4641c489455ea5fac43a5f20911dfb5486630eb0160Andrew Kaylor
46536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// setProcessAllSections (MCJIT Only): By default, only sections that are
46636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// "required for execution" are passed to the RTDyldMemoryManager, and other
46736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// sections are discarded. Passing 'true' to this method will cause
46836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// RuntimeDyld to pass all sections to its RTDyldMemoryManager regardless
46936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// of whether they are "required to execute" in the usual sense.
47036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ///
47136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// Rationale: Some MCJIT clients want to be able to inspect metadata
47236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// sections (e.g. Dwarf, Stack-maps) to enable functionality or analyze
47336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// performance. Passing these sections to the memory manager allows the
47436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// client to make policy about the relevant sections, rather than having
47536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// MCJIT do it.
47636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  virtual void setProcessAllSections(bool ProcessAllSections) {
47736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    llvm_unreachable("No support for ProcessAllSections option");
47836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
47936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
48036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// Return the target machine (if available).
481dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  virtual TargetMachine *getTargetMachine() { return nullptr; }
48236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
48318fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin  /// DisableLazyCompilation - When lazy compilation is off (the default), the
484dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// JIT will eagerly compile every function reachable from the argument to
485dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// getPointerToFunction.  If lazy compilation is turned on, the JIT will only
486dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// compile the one function and emit stubs to compile the rest when they're
487dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// first called.  If lazy compilation is turned off again while some lazy
488dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// stubs are still around, and one of those stubs is called, the program will
489dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// abort.
490dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  ///
491dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// In order to safely compile lazily in a threaded program, the user must
492dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// ensure that 1) only one thread at a time can call any particular lazy
493dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// stub, and 2) any thread modifying LLVM IR must hold the JIT's lock
494dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// (ExecutionEngine::lock) or otherwise ensure that no other thread calls a
495dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  /// lazy stub.  See http://llvm.org/PR5184 for details.
49618fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin  void DisableLazyCompilation(bool Disabled = true) {
49718fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin    CompilingLazily = !Disabled;
4981c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner  }
499dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin  bool isCompilingLazily() const {
500dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin    return CompilingLazily;
5011c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner  }
50218fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin  // Deprecated in favor of isCompilingLazily (to reduce double-negatives).
50318fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin  // Remove this in LLVM 2.8.
50418fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin  bool isLazyCompilationDisabled() const {
50518fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin    return !CompilingLazily;
50618fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin  }
507446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng
50877f86ad08775e0ed2a704ef09ffff3dbd6e04583Evan Cheng  /// DisableGVCompilation - If called, the JIT will abort if it's asked to
50977f86ad08775e0ed2a704ef09ffff3dbd6e04583Evan Cheng  /// allocate space and populate a GlobalVariable that is not internal to
51077f86ad08775e0ed2a704ef09ffff3dbd6e04583Evan Cheng  /// the module.
511446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng  void DisableGVCompilation(bool Disabled = true) {
512446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng    GVCompilationDisabled = Disabled;
513446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng  }
514446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng  bool isGVCompilationDisabled() const {
515446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng    return GVCompilationDisabled;
516446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng  }
517446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng
518e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
519e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  /// symbols with dlsym.  A client can still use InstallLazyFunctionCreator to
520e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  /// resolve symbols in a custom way.
521e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  void DisableSymbolSearching(bool Disabled = true) {
522e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner    SymbolSearchingDisabled = Disabled;
523e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  }
524e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  bool isSymbolSearchingDisabled() const {
525e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner    return SymbolSearchingDisabled;
526e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner  }
5276f348e458660063a40052b208bab96895c822877Jeffrey Yasskin
528dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// Enable/Disable IR module verification.
529dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  ///
530dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// Note: Module verification is enabled by default in Debug builds, and
531dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// disabled by default in Release. Use this method to override the default.
532dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  void setVerifyModules(bool Verify) {
533dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    VerifyModules = Verify;
534dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
535dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  bool getVerifyModules() const {
536dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return VerifyModules;
537dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
538dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
539d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// InstallLazyFunctionCreator - If an unknown function is needed, the
540d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// specified function pointer is invoked to create it.  If it returns null,
541d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  /// the JIT will abort.
542d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  void InstallLazyFunctionCreator(void* (*P)(const std::string &)) {
543d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner    LazyFunctionCreator = P;
544d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner  }
545f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach
54656adf152f6354a9b5609e059050fd2315ad5960cChris Lattnerprotected:
547f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  explicit ExecutionEngine(Module *M);
5489f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner
549bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner  void emitGlobals();
5503ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner
5511514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner  void EmitGlobalVariable(const GlobalVariable *GV);
5523ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner
553bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner  GenericValue getConstantValue(const Constant *C);
554f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach  void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
555db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner                           Type *Ty);
556bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner};
557bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner
5584b1511b027ce0b648b3379f2891816c25b46f515Reid Klecknernamespace EngineKind {
5594b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  // These are actually bitmasks that get or-ed together.
5604b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  enum Kind {
5614b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    JIT         = 0x1,
5624b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    Interpreter = 0x2
5634b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  };
5644b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  const static Kind Either = (Kind)(JIT | Interpreter);
5654b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner}
5664b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
5674b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner/// EngineBuilder - Builder class for ExecutionEngines.  Use this by
5684b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner/// stack-allocating a builder, chaining the various set* methods, and
5694b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner/// terminating it with a .create() call.
5704b1511b027ce0b648b3379f2891816c25b46f515Reid Klecknerclass EngineBuilder {
57148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbarprivate:
572f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  Module *M;
5734b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  EngineKind::Kind WhichEngine;
5744b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  std::string *ErrorStr;
5754b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  CodeGenOpt::Level OptLevel;
57613a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  RTDyldMemoryManager *MCJMM;
5774b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  JITMemoryManager *JMM;
5784b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  bool AllocateGVsWithCode;
579d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne  TargetOptions Options;
580439661395fd2a2a832dba01c65bc88718528313cEvan Cheng  Reloc::Model RelocModel;
58188b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher  CodeModel::Model CMModel;
5824688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  std::string MArch;
5834688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  std::string MCPU;
5844688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  SmallVector<std::string, 4> MAttrs;
5856d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar  bool UseMCJIT;
586dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  bool VerifyModules;
5874b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
5884b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// InitEngine - Does the common initialization of default options.
589cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  void InitEngine();
5904b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
59148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbarpublic:
5924b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// EngineBuilder - Constructor for EngineBuilder.  If create() is called and
593f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// is successful, the created engine takes ownership of the module.
594f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  EngineBuilder(Module *m) : M(m) {
5954b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    InitEngine();
5964b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  }
5974b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
5984b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// setEngineKind - Controls whether the user wants the interpreter, the JIT,
5994b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// or whichever engine works.  This option defaults to EngineKind::Either.
6004b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  EngineBuilder &setEngineKind(EngineKind::Kind w) {
6014b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    WhichEngine = w;
6024b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    return *this;
6034b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  }
60436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
60513a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// setMCJITMemoryManager - Sets the MCJIT memory manager to use. This allows
60613a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// clients to customize their memory allocation policies for the MCJIT. This
60713a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// is only appropriate for the MCJIT; setting this and configuring the builder
60813a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// to create anything other than MCJIT will cause a runtime error. If create()
60913a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// is called and is successful, the created engine takes ownership of the
61013a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// memory manager. This option defaults to NULL. Using this option nullifies
61113a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// the setJITMemoryManager() option.
61213a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  EngineBuilder &setMCJITMemoryManager(RTDyldMemoryManager *mcjmm) {
61313a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo    MCJMM = mcjmm;
614dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    JMM = nullptr;
61513a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo    return *this;
61613a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  }
6174b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
61813a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// setJITMemoryManager - Sets the JIT memory manager to use.  This allows
61913a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// clients to customize their memory allocation policies.  This is only
62013a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// appropriate for either JIT or MCJIT; setting this and configuring the
62113a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// builder to create an interpreter will cause a runtime error. If create()
62213a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// is called and is successful, the created engine takes ownership of the
62313a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// memory manager.  This option defaults to NULL. This option overrides
62413a3cf192887233fb9452ec5b7f841e4652c33c7Filip Pizlo  /// setMCJITMemoryManager() as well.
6254b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  EngineBuilder &setJITMemoryManager(JITMemoryManager *jmm) {
626dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    MCJMM = nullptr;
6274b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    JMM = jmm;
6284b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    return *this;
6294b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  }
6304b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
6314b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// setErrorStr - Set the error string to write to on error.  This option
6324b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// defaults to NULL.
6334b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  EngineBuilder &setErrorStr(std::string *e) {
6344b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    ErrorStr = e;
6354b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    return *this;
6364b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  }
6374b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
6384b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// setOptLevel - Set the optimization level for the JIT.  This option
6394b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// defaults to CodeGenOpt::Default.
6404b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  EngineBuilder &setOptLevel(CodeGenOpt::Level l) {
6414b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    OptLevel = l;
6424b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    return *this;
6434b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  }
6444b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
645d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne  /// setTargetOptions - Set the target options that the ExecutionEngine
646d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne  /// target is using. Defaults to TargetOptions().
647d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne  EngineBuilder &setTargetOptions(const TargetOptions &Opts) {
648d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne    Options = Opts;
649d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne    return *this;
650d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne  }
651d40e103ea5bca8a223fc8261b3322b9454e9af12Peter Collingbourne
652439661395fd2a2a832dba01c65bc88718528313cEvan Cheng  /// setRelocationModel - Set the relocation model that the ExecutionEngine
653439661395fd2a2a832dba01c65bc88718528313cEvan Cheng  /// target is using. Defaults to target specific default "Reloc::Default".
654439661395fd2a2a832dba01c65bc88718528313cEvan Cheng  EngineBuilder &setRelocationModel(Reloc::Model RM) {
655439661395fd2a2a832dba01c65bc88718528313cEvan Cheng    RelocModel = RM;
656439661395fd2a2a832dba01c65bc88718528313cEvan Cheng    return *this;
657439661395fd2a2a832dba01c65bc88718528313cEvan Cheng  }
658439661395fd2a2a832dba01c65bc88718528313cEvan Cheng
65988b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher  /// setCodeModel - Set the CodeModel that the ExecutionEngine target
66034ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng  /// data is using. Defaults to target specific default
66134ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng  /// "CodeModel::JITDefault".
66288b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher  EngineBuilder &setCodeModel(CodeModel::Model M) {
66388b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher    CMModel = M;
66488b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher    return *this;
66588b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher  }
66688b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher
6674b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// setAllocateGVsWithCode - Sets whether global values should be allocated
6684b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// into the same buffer as code.  For most applications this should be set
6694b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// to false.  Allocating globals with code breaks freeMachineCodeForFunction
6704b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// and is probably unsafe and bad for performance.  However, we have clients
6714b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// who depend on this behavior, so we must support it.  This option defaults
6724b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// to false so that users of the new API can safely use the new memory
6734b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  /// manager and free machine code.
6744b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  EngineBuilder &setAllocateGVsWithCode(bool a) {
6754b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    AllocateGVsWithCode = a;
6764b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner    return *this;
6774b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner  }
6784b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
6794688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  /// setMArch - Override the architecture set by the Module's triple.
6804688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  EngineBuilder &setMArch(StringRef march) {
6814688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    MArch.assign(march.begin(), march.end());
6824688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    return *this;
6834688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  }
6844688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin
6854688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  /// setMCPU - Target a specific cpu type.
6864688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  EngineBuilder &setMCPU(StringRef mcpu) {
6874688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    MCPU.assign(mcpu.begin(), mcpu.end());
6884688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    return *this;
6894688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  }
6904688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin
6916d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar  /// setUseMCJIT - Set whether the MC-JIT implementation should be used
6926d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar  /// (experimental).
693714b34fc24d5df02aff01bd64eb80e5945d663acJim Grosbach  EngineBuilder &setUseMCJIT(bool Value) {
6946d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar    UseMCJIT = Value;
695714b34fc24d5df02aff01bd64eb80e5945d663acJim Grosbach    return *this;
6966d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar  }
6976d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar
698dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// setVerifyModules - Set whether the JIT implementation should verify
699dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// IR modules during compilation.
700dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  EngineBuilder &setVerifyModules(bool Verify) {
701dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    VerifyModules = Verify;
702dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return *this;
703dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
704dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
7054688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  /// setMAttrs - Set cpu-specific attributes.
7064688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  template<typename StringSequence>
7074688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  EngineBuilder &setMAttrs(const StringSequence &mattrs) {
7084688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    MAttrs.clear();
7094688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    MAttrs.append(mattrs.begin(), mattrs.end());
7104688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin    return *this;
7114688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin  }
7124688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin
7138e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson  TargetMachine *selectTarget();
7148e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson
7152ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith  /// selectTarget - Pick a target either via -march or by guessing the native
7162ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith  /// arch.  Add any CPU features specified via -mcpu or -mattr.
7178e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson  TargetMachine *selectTarget(const Triple &TargetTriple,
7188e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson                              StringRef MArch,
7198e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson                              StringRef MCPU,
7208e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson                              const SmallVectorImpl<std::string>& MAttrs);
7218e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson
7228e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson  ExecutionEngine *create() {
7238e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson    return create(selectTarget());
7248e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson  }
7258e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson
7268e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson  ExecutionEngine *create(TargetMachine *TM);
7274b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner};
7284b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner
72940be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo// Create wrappers for C Binding types (see CBindingWrapping.h).
73040be1e85665d10f5444186f0e7106e368dd735b8Filip PizloDEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionEngine, LLVMExecutionEngineRef)
73143dfffe890f58468e2fe01f312978d7aa5303801Eric Christopher
732d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
733d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
734bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner#endif
735