ExecutionEngine.h revision 7a2bdde0a0eebcd2125055e0eacaca040f0b766c
1bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner//===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===// 2ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman// 36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// The LLVM Compiler Infrastructure 46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// 57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source 67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details. 7ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman// 86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===// 9bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner// 10bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner// This file defines the abstract interface that implements execution support 11bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner// for LLVM. 12bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner// 13bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner//===----------------------------------------------------------------------===// 14bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner 159f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner#ifndef LLVM_EXECUTION_ENGINE_H 169f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner#define LLVM_EXECUTION_ENGINE_H 17bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner 18bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner#include <vector> 19bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner#include <map> 2020aa474f8fbebde588edc101b90e834df28ce4ceAlkis Evlogimenos#include <string> 21fe854034677f59baca1e38075e71f6efca247a03Chris Lattner#include "llvm/ADT/SmallVector.h" 224688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin#include "llvm/ADT/StringRef.h" 2323e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin#include "llvm/ADT/ValueMap.h" 24515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher#include "llvm/ADT/DenseMap.h" 254c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin#include "llvm/Support/ValueHandle.h" 261f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/Mutex.h" 2798a366d547772010e94609e4584489b3e5ce0043Bill Wendling#include "llvm/Target/TargetMachine.h" 28d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 29d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm { 30d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 319d87eb19be82b28d288e38eab3bbe145060a0701Reid Spencerstruct GenericValue; 32bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Constant; 334c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskinclass ExecutionEngine; 34bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Function; 353ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattnerclass GlobalVariable; 36b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukmanclass GlobalValue; 37df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass JITEventListener; 38df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass JITMemoryManager; 39df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass MachineCodeInfo; 40bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Module; 41df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass MutexGuard; 42bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass TargetData; 43b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukmanclass Type; 44bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner 4548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// \brief Helper class for helping synchronize access to the global address map 4648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// table. 47ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerclass ExecutionEngineState { 484c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskinpublic: 4923e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin struct AddressMapConfig : public ValueMapConfig<const GlobalValue*> { 5023e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin typedef ExecutionEngineState *ExtraData; 5123e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin static sys::Mutex *getMutex(ExecutionEngineState *EES); 5223e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin static void onDelete(ExecutionEngineState *EES, const GlobalValue *Old); 5323e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin static void onRAUW(ExecutionEngineState *, const GlobalValue *, 5423e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin const GlobalValue *); 554c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin }; 564c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin 5723e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin typedef ValueMap<const GlobalValue *, void *, AddressMapConfig> 5823e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin GlobalAddressMapTy; 5923e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin 60ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerprivate: 614c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin ExecutionEngine &EE; 624c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin 63c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// GlobalAddressMap - A mapping between LLVM global values and their 64c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// actualized version... 6523e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin GlobalAddressMapTy GlobalAddressMap; 66c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner 67c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap, 68c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// used to convert raw addresses into the LLVM global value that is emitted 69c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// at the address. This map is not computed unless getGlobalValueAtAddress 70c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// is called at some point. 710d5bd59553375dc85ac04c81ef48ef74c9e7193eJeffrey Yasskin std::map<void *, AssertingVH<const GlobalValue> > GlobalAddressReverseMap; 72ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer 73ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerpublic: 7423e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin ExecutionEngineState(ExecutionEngine &EE); 754c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin 7648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar GlobalAddressMapTy &getGlobalAddressMap(const MutexGuard &) { 77ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer return GlobalAddressMap; 78ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer } 79ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer 800d5bd59553375dc85ac04c81ef48ef74c9e7193eJeffrey Yasskin std::map<void*, AssertingVH<const GlobalValue> > & 8113d57320bd212483463d4f8992d5787b29eda5dfBill Wendling getGlobalAddressReverseMap(const MutexGuard &) { 82ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer return GlobalAddressReverseMap; 83ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer } 84c89d27a440370455336202b2a8f25eb9c73e67bcJeffrey Yasskin 8548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// \brief Erase an entry from the mapping table. 8648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// 8748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// \returns The address that \arg ToUnmap was happed to. 88c89d27a440370455336202b2a8f25eb9c73e67bcJeffrey Yasskin void *RemoveMapping(const MutexGuard &, const GlobalValue *ToUnmap); 89ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer}; 90ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer 9148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// \brief Abstract interface for implementation execution of LLVM modules, 9248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// designed to support both interpreter and just-in-time (JIT) compiler 9348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// implementations. 94ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerclass ExecutionEngine { 9548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// The state object holding the global address mapping, which must be 9648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// accessed synchronously. 9748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar // 9848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar // FIXME: There is no particular need the entire map needs to be 9948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar // synchronized. Wouldn't a reader-writer design be better here? 1004c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin ExecutionEngineState EEState; 10148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar 10248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// The target data for the platform for which execution is being performed. 10348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar const TargetData *TD; 10448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar 10548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// Whether lazy JIT compilation is enabled. 106dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin bool CompilingLazily; 10748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar 10848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// Whether JIT compilation of external global variables is allowed. 109446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng bool GVCompilationDisabled; 11048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar 11148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// Whether the JIT should perform lookups of external symbols (e.g., 11248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// using dlsym). 113e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner bool SymbolSearchingDisabled; 114d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner 1154b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner friend class EngineBuilder; // To allow access to JITCtor and InterpCtor. 1164b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 1171514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattnerprotected: 11848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// The list of Modules that we are JIT'ing from. We use a SmallVector to 11948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// optimize for the case where there is only one module. 120f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin SmallVector<Module*, 1> Modules; 121f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 122a69571c7991813c93cba64e88eced6899ce93d81Owen Anderson void setTargetData(const TargetData *td) { 123a69571c7991813c93cba64e88eced6899ce93d81Owen Anderson TD = td; 124bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner } 125f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 12646fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray /// getMemoryforGV - Allocate memory for a global variable. 12748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar virtual char *getMemoryForGV(const GlobalVariable *GV); 128b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukman 129765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner // To avoid having libexecutionengine depend on the JIT and interpreter 1306d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar // libraries, the execution engine implementations set these functions to ctor 1316d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar // pointers at startup time if they are linked in. 1324688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin static ExecutionEngine *(*JITCtor)( 1334688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin Module *M, 1344688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin std::string *ErrorStr, 1354688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin JITMemoryManager *JMM, 1364688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin CodeGenOpt::Level OptLevel, 1374688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin bool GVsWithCode, 1384688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin CodeModel::Model CMM, 1394688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin StringRef MArch, 1404688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin StringRef MCPU, 1414688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin const SmallVectorImpl<std::string>& MAttrs); 1426d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar static ExecutionEngine *(*MCJITCtor)( 1436d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar Module *M, 1446d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar std::string *ErrorStr, 1456d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar JITMemoryManager *JMM, 1466d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar CodeGenOpt::Level OptLevel, 1476d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar bool GVsWithCode, 1486d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar CodeModel::Model CMM, 1496d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar StringRef MArch, 1506d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar StringRef MCPU, 1516d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar const SmallVectorImpl<std::string>& MAttrs); 152f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin static ExecutionEngine *(*InterpCtor)(Module *M, 1534b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner std::string *ErrorStr); 154d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner 155d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner /// LazyFunctionCreator - If an unknown function is needed, this function 15648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// pointer is invoked to create it. If this returns null, the JIT will 15748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// abort. 15848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar void *(*LazyFunctionCreator)(const std::string &); 159f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 160b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands /// ExceptionTableRegister - If Exception Handling is set, the JIT will 161b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands /// register dwarf tables with this function. 162afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray typedef void (*EERegisterFn)(void*); 163b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands EERegisterFn ExceptionTableRegister; 164b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands EERegisterFn ExceptionTableDeregister; 165515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher /// This maps functions to their exception tables frames. 166515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher DenseMap<const Function*, void*> AllExceptionTables; 167515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher 168afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray 169bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerpublic: 17048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// lock - This lock protects the ExecutionEngine, JIT, JITResolver and 171765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner /// JITEmitter classes. It must be held while changing the internal state of 172765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner /// any of those classes. 17348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar sys::Mutex lock; 174ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer 175f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner //===--------------------------------------------------------------------===// 1769f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner // ExecutionEngine Startup 177f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner //===--------------------------------------------------------------------===// 178ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman 1799f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner virtual ~ExecutionEngine(); 180bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner 1819f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner /// create - This is the factory method for creating an execution engine which 1829f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner /// is appropriate for the current machine. This takes ownership of the 183f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin /// module. 18448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// 18548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// \param GVsWithCode - Allocating globals with code breaks 18648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// freeMachineCodeForFunction and is probably unsafe and bad for performance. 18748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// However, we have clients who depend on this behavior, so we must support 1887a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner /// it. Eventually, when we're willing to break some backwards compatibility, 18948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// this flag should be flipped to false, so that by default 19048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// freeMachineCodeForFunction works. 191f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin static ExecutionEngine *create(Module *M, 1929f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner bool ForceInterpreter = false, 193502f20b17ede40de84503010b7699b328a4f2867Evan Cheng std::string *ErrorStr = 0, 19498a366d547772010e94609e4584489b3e5ce0043Bill Wendling CodeGenOpt::Level OptLevel = 195489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin CodeGenOpt::Default, 196489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin bool GVsWithCode = true); 197489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin 19834c9433004cabd4760987dce4804a91c84908219Chris Lattner /// createJIT - This is the factory method for creating a JIT for the current 19934c9433004cabd4760987dce4804a91c84908219Chris Lattner /// machine, it does not fall back to the interpreter. This takes ownership 200f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin /// of the Module and JITMemoryManager if successful. 2011d929216916dc992f8643dda971aac58d47c7765Daniel Dunbar /// 2021d929216916dc992f8643dda971aac58d47c7765Daniel Dunbar /// Clients should make sure to initialize targets prior to calling this 2031d929216916dc992f8643dda971aac58d47c7765Daniel Dunbar /// function. 204f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin static ExecutionEngine *createJIT(Module *M, 20534c9433004cabd4760987dce4804a91c84908219Chris Lattner std::string *ErrorStr = 0, 206502f20b17ede40de84503010b7699b328a4f2867Evan Cheng JITMemoryManager *JMM = 0, 20798a366d547772010e94609e4584489b3e5ce0043Bill Wendling CodeGenOpt::Level OptLevel = 208489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin CodeGenOpt::Default, 20988b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher bool GVsWithCode = true, 2103472766f9eb7d66f234c390ce1b3a8b76f0ee9ceDuncan Sands CodeModel::Model CMM = 2113472766f9eb7d66f234c390ce1b3a8b76f0ee9ceDuncan Sands CodeModel::Default); 21298a366d547772010e94609e4584489b3e5ce0043Bill Wendling 213f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin /// addModule - Add a Module to the list of modules that we can JIT from. 214f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin /// Note that this takes ownership of the Module: when the ExecutionEngine is 215f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin /// destroyed, it destroys the Module as well. 216f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin virtual void addModule(Module *M) { 217f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin Modules.push_back(M); 2182e40ae4de67ba37d02281d51e7dfd629205e1e30Chris Lattner } 219f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 22048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar //===--------------------------------------------------------------------===// 2219f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner 2229f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner const TargetData *getTargetData() const { return TD; } 2239f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner 224f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin /// removeModule - Remove a Module from the list of modules. Returns true if 225f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin /// M is found. 226f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin virtual bool removeModule(Module *M); 22760789e419e04c260e36af9a1add5ad316313e490Nate Begeman 228fe854034677f59baca1e38075e71f6efca247a03Chris Lattner /// FindFunctionNamed - Search all of the active modules to find the one that 229fe854034677f59baca1e38075e71f6efca247a03Chris Lattner /// defines FnName. This is very slow operation and shouldn't be used for 230fe854034677f59baca1e38075e71f6efca247a03Chris Lattner /// general code. 231fe854034677f59baca1e38075e71f6efca247a03Chris Lattner Function *FindFunctionNamed(const char *FnName); 232f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 233ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner /// runFunction - Execute the specified function with the specified arguments, 234ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner /// and return the result. 235ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner virtual GenericValue runFunction(Function *F, 236ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner const std::vector<GenericValue> &ArgValues) = 0; 237ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner 2389ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner /// runStaticConstructorsDestructors - This method is used to execute all of 23948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// the static constructors or destructors for a program. 24048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// 24148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// \param isDtors - Run the destructors instead of constructors. 2429ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner void runStaticConstructorsDestructors(bool isDtors); 24348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar 24418314dc741ab7dc4db02b199af77f43bd8551fd2Evan Cheng /// runStaticConstructorsDestructors - This method is used to execute all of 24548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// the static constructors or destructors for a particular module. 24648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// 24748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// \param isDtors - Run the destructors instead of constructors. 24818314dc741ab7dc4db02b199af77f43bd8551fd2Evan Cheng void runStaticConstructorsDestructors(Module *module, bool isDtors); 249f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 250f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 251e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner /// runFunctionAsMain - This is a helper function which wraps runFunction to 252e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner /// handle the common task of starting up main with the specified argc, argv, 253e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner /// and envp parameters. 254e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv, 255e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner const char * const * envp); 256e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner 257e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner 258683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner /// addGlobalMapping - Tell the execution engine that the specified global is 259683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner /// at the specified location. This is used internally as functions are JIT'd 260683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner /// and as global variables are laid out in memory. It can and should also be 261683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner /// used by clients of the EE that want to have an LLVM global overlay 2624c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin /// existing data in memory. Mappings are automatically removed when their 2634c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin /// GlobalValue is destroyed. 264683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner void addGlobalMapping(const GlobalValue *GV, void *Addr); 265f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 26648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// clearAllGlobalMappings - Clear all global mappings and start over again, 26748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// for use in dynamic compilation scenarios to move globals. 268683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner void clearAllGlobalMappings(); 269f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 270f049e07eb8930214941c72f8e4409df394de1567Nate Begeman /// clearGlobalMappingsFromModule - Clear all global mappings that came from a 271f049e07eb8930214941c72f8e4409df394de1567Nate Begeman /// particular module, because it has been removed from the JIT. 272f049e07eb8930214941c72f8e4409df394de1567Nate Begeman void clearGlobalMappingsFromModule(Module *M); 273f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 274f5feaf4fe381476b0ea567d7837c64b6590133e9Chris Lattner /// updateGlobalMapping - Replace an existing mapping for GV with a new 275683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner /// address. This updates both maps as required. If "Addr" is null, the 276f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner /// entry for the global is removed from the mappings. This returns the old 277f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner /// value of the pointer, or null if it was not in the map. 278f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner void *updateGlobalMapping(const GlobalValue *GV, void *Addr); 279f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 280895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner /// getPointerToGlobalIfAvailable - This returns the address of the specified 281683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner /// global value if it is has already been codegen'd, otherwise it returns 282683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner /// null. 283683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner void *getPointerToGlobalIfAvailable(const GlobalValue *GV); 28470bca51f92871c7f9d3eac9ab68292c149fab53cChris Lattner 285895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner /// getPointerToGlobal - This returns the address of the specified global 28648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// value. This may involve code generation if it's a function. 287bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner void *getPointerToGlobal(const GlobalValue *GV); 288bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner 289895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner /// getPointerToFunction - The different EE's represent function bodies in 290895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner /// different ways. They should each implement this to say what a function 2914c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin /// pointer should look like. When F is destroyed, the ExecutionEngine will 2927a9034c4db248fe8b8cb82762881b51b221988d3Jeffrey Yasskin /// remove its global mapping and free any machine code. Be sure no threads 2937a9034c4db248fe8b8cb82762881b51b221988d3Jeffrey Yasskin /// are running inside F when that happens. 294fb0ef2e82cb2e80983c097100ae168af68ee8e7bBrian Gaeke virtual void *getPointerToFunction(Function *F) = 0; 295bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner 296f32a6a3091e0b01d17926f4b1cf78972854b8cb5Chris Lattner /// getPointerToBasicBlock - The different EE's represent basic blocks in 297f32a6a3091e0b01d17926f4b1cf78972854b8cb5Chris Lattner /// different ways. Return the representation for a blockaddress of the 298f32a6a3091e0b01d17926f4b1cf78972854b8cb5Chris Lattner /// specified block. 299f32a6a3091e0b01d17926f4b1cf78972854b8cb5Chris Lattner virtual void *getPointerToBasicBlock(BasicBlock *BB) = 0; 300f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 301895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner /// getPointerToFunctionOrStub - If the specified function has been 302895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner /// code-gen'd, return a pointer to the function. If not, compile it, or use 3034c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin /// a stub to implement lazy compilation if available. See 3044c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin /// getPointerToFunction for the requirements on destroying F. 30518cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner virtual void *getPointerToFunctionOrStub(Function *F) { 30618cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner // Default implementation, just codegen the function. 30718cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner return getPointerToFunction(F); 30818cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner } 30918cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner 310b3a847db0b991d3210706a2580428fdc2b6bf037Argyrios Kyrtzidis // The JIT overrides a version that actually does this. 3115e0644c73e0415e85c035ad1306a86554dd26df7Bill Wendling virtual void runJITOnFunction(Function *, MachineCodeInfo * = 0) { } 312b3a847db0b991d3210706a2580428fdc2b6bf037Argyrios Kyrtzidis 313c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// getGlobalValueAtAddress - Return the LLVM global value object that starts 314c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// at the specified address. 315c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// 316c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner const GlobalValue *getGlobalValueAtAddress(void *Addr); 317c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner 31848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. 31948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// Ptr is the address of the memory at which to store Val, cast to 32048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// GenericValue *. It is not a pointer to a GenericValue containing the 32148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// address at which to store Val. 322f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr, 323f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner const Type *Ty); 32448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar 32529794cba810ec249a9ede5ea77333a71579fd182Brian Gaeke void InitializeMemory(const Constant *Init, void *Addr); 32629794cba810ec249a9ede5ea77333a71579fd182Brian Gaeke 32748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// recompileAndRelinkFunction - This method is used to force a function which 32848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// has already been compiled to be compiled again, possibly after it has been 32948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// modified. Then the entry to the old copy is overwritten with a branch to 33048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// the new copy. If there was no old copy, this acts just like 33148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// VM::getPointerToFunction(). 33218cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner virtual void *recompileAndRelinkFunction(Function *F) = 0; 33318cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner 334e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman /// freeMachineCodeForFunction - Release memory in the ExecutionEngine 335e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman /// corresponding to the machine code emitted to execute this function, useful 336e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman /// for garbage-collecting generated code. 337e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman virtual void freeMachineCodeForFunction(Function *F) = 0; 338e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman 3391514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner /// getOrEmitGlobalVariable - Return the address of the specified global 3401514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner /// variable, possibly emitting it to memory if needed. This is used by the 3414c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin /// Emitter. 3421514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) { 3431514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner return getPointerToGlobal((GlobalValue*)GV); 3441514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner } 345df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin 346df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin /// Registers a listener to be called back on various events within 347df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin /// the JIT. See JITEventListener.h for more details. Does not 348df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin /// take ownership of the argument. The argument may be NULL, in 349df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin /// which case these functions do nothing. 350df225c0253aa987830fbd3dfcb5d9f8f7f973336Bill Wendling virtual void RegisterJITEventListener(JITEventListener *) {} 351df225c0253aa987830fbd3dfcb5d9f8f7f973336Bill Wendling virtual void UnregisterJITEventListener(JITEventListener *) {} 352df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin 35318fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin /// DisableLazyCompilation - When lazy compilation is off (the default), the 354dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// JIT will eagerly compile every function reachable from the argument to 355dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// getPointerToFunction. If lazy compilation is turned on, the JIT will only 356dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// compile the one function and emit stubs to compile the rest when they're 357dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// first called. If lazy compilation is turned off again while some lazy 358dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// stubs are still around, and one of those stubs is called, the program will 359dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// abort. 360dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// 361dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// In order to safely compile lazily in a threaded program, the user must 362dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// ensure that 1) only one thread at a time can call any particular lazy 363dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// stub, and 2) any thread modifying LLVM IR must hold the JIT's lock 364dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// (ExecutionEngine::lock) or otherwise ensure that no other thread calls a 365dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// lazy stub. See http://llvm.org/PR5184 for details. 36618fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin void DisableLazyCompilation(bool Disabled = true) { 36718fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin CompilingLazily = !Disabled; 3681c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner } 369dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin bool isCompilingLazily() const { 370dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin return CompilingLazily; 3711c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner } 37218fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin // Deprecated in favor of isCompilingLazily (to reduce double-negatives). 37318fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin // Remove this in LLVM 2.8. 37418fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin bool isLazyCompilationDisabled() const { 37518fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin return !CompilingLazily; 37618fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin } 377446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng 37877f86ad08775e0ed2a704ef09ffff3dbd6e04583Evan Cheng /// DisableGVCompilation - If called, the JIT will abort if it's asked to 37977f86ad08775e0ed2a704ef09ffff3dbd6e04583Evan Cheng /// allocate space and populate a GlobalVariable that is not internal to 38077f86ad08775e0ed2a704ef09ffff3dbd6e04583Evan Cheng /// the module. 381446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng void DisableGVCompilation(bool Disabled = true) { 382446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng GVCompilationDisabled = Disabled; 383446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng } 384446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng bool isGVCompilationDisabled() const { 385446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng return GVCompilationDisabled; 386446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng } 387446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng 388e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown 389e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to 390e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner /// resolve symbols in a custom way. 391e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner void DisableSymbolSearching(bool Disabled = true) { 392e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner SymbolSearchingDisabled = Disabled; 393e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner } 394e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner bool isSymbolSearchingDisabled() const { 395e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner return SymbolSearchingDisabled; 396e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner } 3976f348e458660063a40052b208bab96895c822877Jeffrey Yasskin 398d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner /// InstallLazyFunctionCreator - If an unknown function is needed, the 399d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner /// specified function pointer is invoked to create it. If it returns null, 400d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner /// the JIT will abort. 401d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner void InstallLazyFunctionCreator(void* (*P)(const std::string &)) { 402d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner LazyFunctionCreator = P; 403d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner } 404f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 405afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray /// InstallExceptionTableRegister - The JIT will use the given function 406afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray /// to register the exception tables it generates. 407b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands void InstallExceptionTableRegister(EERegisterFn F) { 408afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray ExceptionTableRegister = F; 409afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } 410b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands void InstallExceptionTableDeregister(EERegisterFn F) { 411b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands ExceptionTableDeregister = F; 412b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands } 413f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 41448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// RegisterTable - Registers the given pointer as an exception table. It 41548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// uses the ExceptionTableRegister function. 416515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher void RegisterTable(const Function *fn, void* res) { 417b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands if (ExceptionTableRegister) { 418afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray ExceptionTableRegister(res); 419515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher AllExceptionTables[fn] = res; 420515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher } 421515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher } 422515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher 423f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach /// DeregisterTable - Deregisters the exception frame previously registered 424f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach /// for the given function. 425515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher void DeregisterTable(const Function *Fn) { 426515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher if (ExceptionTableDeregister) { 427f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach DenseMap<const Function*, void*>::iterator frame = 428f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach AllExceptionTables.find(Fn); 429515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher if(frame != AllExceptionTables.end()) { 430515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher ExceptionTableDeregister(frame->second); 431515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher AllExceptionTables.erase(frame); 432515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher } 433b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands } 434afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } 4351514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner 436b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands /// DeregisterAllTables - Deregisters all previously registered pointers to an 43748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// exception tables. It uses the ExceptionTableoDeregister function. 438b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands void DeregisterAllTables(); 439b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands 44056adf152f6354a9b5609e059050fd2315ad5960cChris Lattnerprotected: 441f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin explicit ExecutionEngine(Module *M); 4429f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner 443bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner void emitGlobals(); 4443ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner 4451514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner void EmitGlobalVariable(const GlobalVariable *GV); 4463ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner 447bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner GenericValue getConstantValue(const Constant *C); 448f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr, 44947567bdcb5ba3886eaf3fa5f0e0b912a24c12347Reid Spencer const Type *Ty); 450bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner}; 451bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner 4524b1511b027ce0b648b3379f2891816c25b46f515Reid Klecknernamespace EngineKind { 4534b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner // These are actually bitmasks that get or-ed together. 4544b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner enum Kind { 4554b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner JIT = 0x1, 4564b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner Interpreter = 0x2 4574b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner }; 4584b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner const static Kind Either = (Kind)(JIT | Interpreter); 4594b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner} 4604b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 4614b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner/// EngineBuilder - Builder class for ExecutionEngines. Use this by 4624b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner/// stack-allocating a builder, chaining the various set* methods, and 4634b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner/// terminating it with a .create() call. 4644b1511b027ce0b648b3379f2891816c25b46f515Reid Klecknerclass EngineBuilder { 46548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbarprivate: 466f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin Module *M; 4674b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner EngineKind::Kind WhichEngine; 4684b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner std::string *ErrorStr; 4694b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner CodeGenOpt::Level OptLevel; 4704b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner JITMemoryManager *JMM; 4714b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner bool AllocateGVsWithCode; 47288b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher CodeModel::Model CMModel; 4734688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin std::string MArch; 4744688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin std::string MCPU; 4754688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin SmallVector<std::string, 4> MAttrs; 4766d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar bool UseMCJIT; 4774b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 4784b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// InitEngine - Does the common initialization of default options. 4794b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner void InitEngine() { 4804b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner WhichEngine = EngineKind::Either; 4814b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner ErrorStr = NULL; 4824b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner OptLevel = CodeGenOpt::Default; 4834b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner JMM = NULL; 4844b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner AllocateGVsWithCode = false; 48588b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher CMModel = CodeModel::Default; 4866d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar UseMCJIT = false; 4874b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner } 4884b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 48948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbarpublic: 4904b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// EngineBuilder - Constructor for EngineBuilder. If create() is called and 491f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin /// is successful, the created engine takes ownership of the module. 492f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin EngineBuilder(Module *m) : M(m) { 4934b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner InitEngine(); 4944b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner } 4954b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 4964b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// setEngineKind - Controls whether the user wants the interpreter, the JIT, 4974b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// or whichever engine works. This option defaults to EngineKind::Either. 4984b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner EngineBuilder &setEngineKind(EngineKind::Kind w) { 4994b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner WhichEngine = w; 5004b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner return *this; 5014b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner } 5024b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 5034b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// setJITMemoryManager - Sets the memory manager to use. This allows 5044b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// clients to customize their memory allocation policies. If create() is 5054b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// called and is successful, the created engine takes ownership of the 5064b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// memory manager. This option defaults to NULL. 5074b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner EngineBuilder &setJITMemoryManager(JITMemoryManager *jmm) { 5084b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner JMM = jmm; 5094b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner return *this; 5104b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner } 5114b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 5124b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// setErrorStr - Set the error string to write to on error. This option 5134b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// defaults to NULL. 5144b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner EngineBuilder &setErrorStr(std::string *e) { 5154b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner ErrorStr = e; 5164b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner return *this; 5174b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner } 5184b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 5194b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// setOptLevel - Set the optimization level for the JIT. This option 5204b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// defaults to CodeGenOpt::Default. 5214b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner EngineBuilder &setOptLevel(CodeGenOpt::Level l) { 5224b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner OptLevel = l; 5234b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner return *this; 5244b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner } 5254b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 52688b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher /// setCodeModel - Set the CodeModel that the ExecutionEngine target 52788b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher /// data is using. Defaults to target specific default "CodeModel::Default". 52888b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher EngineBuilder &setCodeModel(CodeModel::Model M) { 52988b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher CMModel = M; 53088b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher return *this; 53188b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher } 53288b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher 5334b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// setAllocateGVsWithCode - Sets whether global values should be allocated 5344b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// into the same buffer as code. For most applications this should be set 5354b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// to false. Allocating globals with code breaks freeMachineCodeForFunction 5364b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// and is probably unsafe and bad for performance. However, we have clients 5374b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// who depend on this behavior, so we must support it. This option defaults 5384b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// to false so that users of the new API can safely use the new memory 5394b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// manager and free machine code. 5404b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner EngineBuilder &setAllocateGVsWithCode(bool a) { 5414b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner AllocateGVsWithCode = a; 5424b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner return *this; 5434b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner } 5444b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 5454688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin /// setMArch - Override the architecture set by the Module's triple. 5464688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin EngineBuilder &setMArch(StringRef march) { 5474688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin MArch.assign(march.begin(), march.end()); 5484688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin return *this; 5494688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin } 5504688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin 5514688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin /// setMCPU - Target a specific cpu type. 5524688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin EngineBuilder &setMCPU(StringRef mcpu) { 5534688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin MCPU.assign(mcpu.begin(), mcpu.end()); 5544688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin return *this; 5554688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin } 5564688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin 5576d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar /// setUseMCJIT - Set whether the MC-JIT implementation should be used 5586d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar /// (experimental). 559714b34fc24d5df02aff01bd64eb80e5945d663acJim Grosbach EngineBuilder &setUseMCJIT(bool Value) { 5606d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar UseMCJIT = Value; 561714b34fc24d5df02aff01bd64eb80e5945d663acJim Grosbach return *this; 5626d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar } 5636d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar 5644688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin /// setMAttrs - Set cpu-specific attributes. 5654688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin template<typename StringSequence> 5664688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin EngineBuilder &setMAttrs(const StringSequence &mattrs) { 5674688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin MAttrs.clear(); 5684688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin MAttrs.append(mattrs.begin(), mattrs.end()); 5694688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin return *this; 5704688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin } 5714688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin 5724b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner ExecutionEngine *create(); 5734b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner}; 5744b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 575d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace 576d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 577bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner#endif 578