ExecutionEngine.h revision 34ad6db8b958fdc0d38e122edf753b5326e69b03
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> 21439661395fd2a2a832dba01c65bc88718528313cEvan Cheng#include "llvm/MC/MCCodeGenInfo.h" 22fe854034677f59baca1e38075e71f6efca247a03Chris Lattner#include "llvm/ADT/SmallVector.h" 234688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin#include "llvm/ADT/StringRef.h" 2423e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin#include "llvm/ADT/ValueMap.h" 25515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher#include "llvm/ADT/DenseMap.h" 264c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin#include "llvm/Support/ValueHandle.h" 271f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/Mutex.h" 2898a366d547772010e94609e4584489b3e5ce0043Bill Wendling#include "llvm/Target/TargetMachine.h" 29d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 30d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm { 31d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 329d87eb19be82b28d288e38eab3bbe145060a0701Reid Spencerstruct GenericValue; 33bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Constant; 344c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskinclass ExecutionEngine; 35bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Function; 363ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattnerclass GlobalVariable; 37b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukmanclass GlobalValue; 38df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass JITEventListener; 39df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass JITMemoryManager; 40df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass MachineCodeInfo; 41bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass Module; 42df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskinclass MutexGuard; 43bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerclass TargetData; 44b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukmanclass Type; 45bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner 4648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// \brief Helper class for helping synchronize access to the global address map 4748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// table. 48ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerclass ExecutionEngineState { 494c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskinpublic: 5023e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin struct AddressMapConfig : public ValueMapConfig<const GlobalValue*> { 5123e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin typedef ExecutionEngineState *ExtraData; 5223e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin static sys::Mutex *getMutex(ExecutionEngineState *EES); 5323e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin static void onDelete(ExecutionEngineState *EES, const GlobalValue *Old); 5423e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin static void onRAUW(ExecutionEngineState *, const GlobalValue *, 5523e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin const GlobalValue *); 564c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin }; 574c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin 5823e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin typedef ValueMap<const GlobalValue *, void *, AddressMapConfig> 5923e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin GlobalAddressMapTy; 6023e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin 61ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerprivate: 624c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin ExecutionEngine &EE; 634c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin 64c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// GlobalAddressMap - A mapping between LLVM global values and their 65c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// actualized version... 6623e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin GlobalAddressMapTy GlobalAddressMap; 67c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner 68c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap, 69c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// used to convert raw addresses into the LLVM global value that is emitted 70c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// at the address. This map is not computed unless getGlobalValueAtAddress 71c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// is called at some point. 720d5bd59553375dc85ac04c81ef48ef74c9e7193eJeffrey Yasskin std::map<void *, AssertingVH<const GlobalValue> > GlobalAddressReverseMap; 73ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer 74ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerpublic: 7523e5fcfec4640955fec41dc8348f467adf1a3e56Jeffrey Yasskin ExecutionEngineState(ExecutionEngine &EE); 764c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin 7748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar GlobalAddressMapTy &getGlobalAddressMap(const MutexGuard &) { 78ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer return GlobalAddressMap; 79ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer } 80ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer 810d5bd59553375dc85ac04c81ef48ef74c9e7193eJeffrey Yasskin std::map<void*, AssertingVH<const GlobalValue> > & 8213d57320bd212483463d4f8992d5787b29eda5dfBill Wendling getGlobalAddressReverseMap(const MutexGuard &) { 83ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer return GlobalAddressReverseMap; 84ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer } 85c89d27a440370455336202b2a8f25eb9c73e67bcJeffrey Yasskin 8648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// \brief Erase an entry from the mapping table. 8748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// 8848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// \returns The address that \arg ToUnmap was happed to. 89c89d27a440370455336202b2a8f25eb9c73e67bcJeffrey Yasskin void *RemoveMapping(const MutexGuard &, const GlobalValue *ToUnmap); 90ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer}; 91ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer 9248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// \brief Abstract interface for implementation execution of LLVM modules, 9348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// designed to support both interpreter and just-in-time (JIT) compiler 9448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar/// implementations. 95ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencerclass ExecutionEngine { 9648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// The state object holding the global address mapping, which must be 9748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// accessed synchronously. 9848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar // 9948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar // FIXME: There is no particular need the entire map needs to be 10048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar // synchronized. Wouldn't a reader-writer design be better here? 1014c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin ExecutionEngineState EEState; 10248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar 10348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// The target data for the platform for which execution is being performed. 10448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar const TargetData *TD; 10548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar 10648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// Whether lazy JIT compilation is enabled. 107dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin bool CompilingLazily; 10848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar 10948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// Whether JIT compilation of external global variables is allowed. 110446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng bool GVCompilationDisabled; 11148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar 11248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// Whether the JIT should perform lookups of external symbols (e.g., 11348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// using dlsym). 114e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner bool SymbolSearchingDisabled; 115d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner 1164b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner friend class EngineBuilder; // To allow access to JITCtor and InterpCtor. 1174b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 1181514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattnerprotected: 11948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// The list of Modules that we are JIT'ing from. We use a SmallVector to 12048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// optimize for the case where there is only one module. 121f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin SmallVector<Module*, 1> Modules; 122f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 123a69571c7991813c93cba64e88eced6899ce93d81Owen Anderson void setTargetData(const TargetData *td) { 124a69571c7991813c93cba64e88eced6899ce93d81Owen Anderson TD = td; 125bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner } 126f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 12746fa139e26be6ebc00be2fb45820c2560dd22a32Nicolas Geoffray /// getMemoryforGV - Allocate memory for a global variable. 12848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar virtual char *getMemoryForGV(const GlobalVariable *GV); 129b6c54ed8f50a351989993a5ef88507abc6d63e2dMisha Brukman 130765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner // To avoid having libexecutionengine depend on the JIT and interpreter 1316d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar // libraries, the execution engine implementations set these functions to ctor 1326d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar // pointers at startup time if they are linked in. 1334688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin static ExecutionEngine *(*JITCtor)( 1344688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin Module *M, 1354688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin std::string *ErrorStr, 1364688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin JITMemoryManager *JMM, 1374688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin CodeGenOpt::Level OptLevel, 1384688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin bool GVsWithCode, 139c5b28580a94e247300e5d3ccf532e153f2ae6f12Dylan Noblesmith TargetMachine *TM); 1406d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar static ExecutionEngine *(*MCJITCtor)( 1416d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar Module *M, 1426d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar std::string *ErrorStr, 1436d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar JITMemoryManager *JMM, 1446d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar CodeGenOpt::Level OptLevel, 1456d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar bool GVsWithCode, 146c5b28580a94e247300e5d3ccf532e153f2ae6f12Dylan Noblesmith TargetMachine *TM); 147f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin static ExecutionEngine *(*InterpCtor)(Module *M, 1484b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner std::string *ErrorStr); 149d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner 150d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner /// LazyFunctionCreator - If an unknown function is needed, this function 15148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// pointer is invoked to create it. If this returns null, the JIT will 15248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// abort. 15348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar void *(*LazyFunctionCreator)(const std::string &); 154f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 155b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands /// ExceptionTableRegister - If Exception Handling is set, the JIT will 156b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands /// register dwarf tables with this function. 157afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray typedef void (*EERegisterFn)(void*); 158b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands EERegisterFn ExceptionTableRegister; 159b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands EERegisterFn ExceptionTableDeregister; 160515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher /// This maps functions to their exception tables frames. 161515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher DenseMap<const Function*, void*> AllExceptionTables; 162515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher 163afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray 164bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattnerpublic: 16548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// lock - This lock protects the ExecutionEngine, JIT, JITResolver and 166765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner /// JITEmitter classes. It must be held while changing the internal state of 167765c93cefda367d8e5a8e0afcd610a7e15bbd987Chris Lattner /// any of those classes. 16848dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar sys::Mutex lock; 169ee448630bdf7eb6037fe2c50518d32010c433ca3Reid Spencer 170f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner //===--------------------------------------------------------------------===// 1719f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner // ExecutionEngine Startup 172f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner //===--------------------------------------------------------------------===// 173ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman 1749f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner virtual ~ExecutionEngine(); 175bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner 1769f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner /// create - This is the factory method for creating an execution engine which 1779f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner /// is appropriate for the current machine. This takes ownership of the 178f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin /// module. 17948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// 18048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// \param GVsWithCode - Allocating globals with code breaks 18148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// freeMachineCodeForFunction and is probably unsafe and bad for performance. 18248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// However, we have clients who depend on this behavior, so we must support 1837a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner /// it. Eventually, when we're willing to break some backwards compatibility, 18448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// this flag should be flipped to false, so that by default 18548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// freeMachineCodeForFunction works. 186f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin static ExecutionEngine *create(Module *M, 1879f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner bool ForceInterpreter = false, 188502f20b17ede40de84503010b7699b328a4f2867Evan Cheng std::string *ErrorStr = 0, 18998a366d547772010e94609e4584489b3e5ce0043Bill Wendling CodeGenOpt::Level OptLevel = 190489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin CodeGenOpt::Default, 191489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin bool GVsWithCode = true); 192489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin 19334c9433004cabd4760987dce4804a91c84908219Chris Lattner /// createJIT - This is the factory method for creating a JIT for the current 19434c9433004cabd4760987dce4804a91c84908219Chris Lattner /// machine, it does not fall back to the interpreter. This takes ownership 195f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin /// of the Module and JITMemoryManager if successful. 1961d929216916dc992f8643dda971aac58d47c7765Daniel Dunbar /// 1971d929216916dc992f8643dda971aac58d47c7765Daniel Dunbar /// Clients should make sure to initialize targets prior to calling this 1981d929216916dc992f8643dda971aac58d47c7765Daniel Dunbar /// function. 199f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin static ExecutionEngine *createJIT(Module *M, 20034c9433004cabd4760987dce4804a91c84908219Chris Lattner std::string *ErrorStr = 0, 201502f20b17ede40de84503010b7699b328a4f2867Evan Cheng JITMemoryManager *JMM = 0, 20298a366d547772010e94609e4584489b3e5ce0043Bill Wendling CodeGenOpt::Level OptLevel = 203489393d7b92107cc3de17d8dbe7dd11ab7395fdcJeffrey Yasskin CodeGenOpt::Default, 20488b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher bool GVsWithCode = true, 205439661395fd2a2a832dba01c65bc88718528313cEvan Cheng Reloc::Model RM = Reloc::Default, 2063472766f9eb7d66f234c390ce1b3a8b76f0ee9ceDuncan Sands CodeModel::Model CMM = 2073472766f9eb7d66f234c390ce1b3a8b76f0ee9ceDuncan Sands CodeModel::Default); 20898a366d547772010e94609e4584489b3e5ce0043Bill Wendling 209f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin /// addModule - Add a Module to the list of modules that we can JIT from. 210f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin /// Note that this takes ownership of the Module: when the ExecutionEngine is 211f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin /// destroyed, it destroys the Module as well. 212f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin virtual void addModule(Module *M) { 213f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin Modules.push_back(M); 2142e40ae4de67ba37d02281d51e7dfd629205e1e30Chris Lattner } 215f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 21648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar //===--------------------------------------------------------------------===// 2179f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner 2189f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner const TargetData *getTargetData() const { return TD; } 2199f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner 220f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin /// removeModule - Remove a Module from the list of modules. Returns true if 221f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin /// M is found. 222f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin virtual bool removeModule(Module *M); 22360789e419e04c260e36af9a1add5ad316313e490Nate Begeman 224fe854034677f59baca1e38075e71f6efca247a03Chris Lattner /// FindFunctionNamed - Search all of the active modules to find the one that 225fe854034677f59baca1e38075e71f6efca247a03Chris Lattner /// defines FnName. This is very slow operation and shouldn't be used for 226fe854034677f59baca1e38075e71f6efca247a03Chris Lattner /// general code. 227fe854034677f59baca1e38075e71f6efca247a03Chris Lattner Function *FindFunctionNamed(const char *FnName); 228f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 229ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner /// runFunction - Execute the specified function with the specified arguments, 230ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner /// and return the result. 231ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner virtual GenericValue runFunction(Function *F, 232ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner const std::vector<GenericValue> &ArgValues) = 0; 233ff65e36be0f4fbe776ee6bb39b7ab14ebe3895ebChris Lattner 2349ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner /// runStaticConstructorsDestructors - This method is used to execute all of 23548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// the static constructors or destructors for a program. 23648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// 23748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// \param isDtors - Run the destructors instead of constructors. 2389ca6cdaee91fddcd3ea57dedcd624c14c7a40f65Chris Lattner void runStaticConstructorsDestructors(bool isDtors); 23948dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar 24018314dc741ab7dc4db02b199af77f43bd8551fd2Evan Cheng /// runStaticConstructorsDestructors - This method is used to execute all of 24148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// the static constructors or destructors for a particular module. 24248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// 24348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// \param isDtors - Run the destructors instead of constructors. 24418314dc741ab7dc4db02b199af77f43bd8551fd2Evan Cheng void runStaticConstructorsDestructors(Module *module, bool isDtors); 245f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 246f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 247e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner /// runFunctionAsMain - This is a helper function which wraps runFunction to 248e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner /// handle the common task of starting up main with the specified argc, argv, 249e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner /// and envp parameters. 250e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv, 251e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner const char * const * envp); 252e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner 253e5dbbf2bddf7fc28d9f63a5e978af12184c80adaChris Lattner 254683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner /// addGlobalMapping - Tell the execution engine that the specified global is 255683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner /// at the specified location. This is used internally as functions are JIT'd 256683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner /// and as global variables are laid out in memory. It can and should also be 257683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner /// used by clients of the EE that want to have an LLVM global overlay 2584c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin /// existing data in memory. Mappings are automatically removed when their 2594c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin /// GlobalValue is destroyed. 260683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner void addGlobalMapping(const GlobalValue *GV, void *Addr); 261f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 26248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// clearAllGlobalMappings - Clear all global mappings and start over again, 26348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// for use in dynamic compilation scenarios to move globals. 264683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner void clearAllGlobalMappings(); 265f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 266f049e07eb8930214941c72f8e4409df394de1567Nate Begeman /// clearGlobalMappingsFromModule - Clear all global mappings that came from a 267f049e07eb8930214941c72f8e4409df394de1567Nate Begeman /// particular module, because it has been removed from the JIT. 268f049e07eb8930214941c72f8e4409df394de1567Nate Begeman void clearGlobalMappingsFromModule(Module *M); 269f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 270f5feaf4fe381476b0ea567d7837c64b6590133e9Chris Lattner /// updateGlobalMapping - Replace an existing mapping for GV with a new 271683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner /// address. This updates both maps as required. If "Addr" is null, the 272f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner /// entry for the global is removed from the mappings. This returns the old 273f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner /// value of the pointer, or null if it was not in the map. 274f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner void *updateGlobalMapping(const GlobalValue *GV, void *Addr); 275f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 276895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner /// getPointerToGlobalIfAvailable - This returns the address of the specified 277683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner /// global value if it is has already been codegen'd, otherwise it returns 278683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner /// null. 279683d1bb712d8f8fc2d727a160da029c9cf40a423Chris Lattner void *getPointerToGlobalIfAvailable(const GlobalValue *GV); 28070bca51f92871c7f9d3eac9ab68292c149fab53cChris Lattner 281895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner /// getPointerToGlobal - This returns the address of the specified global 28248dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// value. This may involve code generation if it's a function. 283bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner void *getPointerToGlobal(const GlobalValue *GV); 284bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner 285895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner /// getPointerToFunction - The different EE's represent function bodies in 286895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner /// different ways. They should each implement this to say what a function 2874c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin /// pointer should look like. When F is destroyed, the ExecutionEngine will 2887a9034c4db248fe8b8cb82762881b51b221988d3Jeffrey Yasskin /// remove its global mapping and free any machine code. Be sure no threads 2897a9034c4db248fe8b8cb82762881b51b221988d3Jeffrey Yasskin /// are running inside F when that happens. 290fb0ef2e82cb2e80983c097100ae168af68ee8e7bBrian Gaeke virtual void *getPointerToFunction(Function *F) = 0; 291bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner 292f32a6a3091e0b01d17926f4b1cf78972854b8cb5Chris Lattner /// getPointerToBasicBlock - The different EE's represent basic blocks in 293f32a6a3091e0b01d17926f4b1cf78972854b8cb5Chris Lattner /// different ways. Return the representation for a blockaddress of the 294f32a6a3091e0b01d17926f4b1cf78972854b8cb5Chris Lattner /// specified block. 295f32a6a3091e0b01d17926f4b1cf78972854b8cb5Chris Lattner virtual void *getPointerToBasicBlock(BasicBlock *BB) = 0; 296f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 297895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner /// getPointerToFunctionOrStub - If the specified function has been 298895be4bae2c4f9a2fd2234f0329f618274e67eabChris Lattner /// code-gen'd, return a pointer to the function. If not, compile it, or use 2994c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin /// a stub to implement lazy compilation if available. See 3004c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin /// getPointerToFunction for the requirements on destroying F. 30118cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner virtual void *getPointerToFunctionOrStub(Function *F) { 30218cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner // Default implementation, just codegen the function. 30318cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner return getPointerToFunction(F); 30418cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner } 30518cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner 306b3a847db0b991d3210706a2580428fdc2b6bf037Argyrios Kyrtzidis // The JIT overrides a version that actually does this. 3075e0644c73e0415e85c035ad1306a86554dd26df7Bill Wendling virtual void runJITOnFunction(Function *, MachineCodeInfo * = 0) { } 308b3a847db0b991d3210706a2580428fdc2b6bf037Argyrios Kyrtzidis 309c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// getGlobalValueAtAddress - Return the LLVM global value object that starts 310c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// at the specified address. 311c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner /// 312c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner const GlobalValue *getGlobalValueAtAddress(void *Addr); 313c8a07d7378699b8d11ce2a1549b984d84706da19Chris Lattner 31448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. 31548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// Ptr is the address of the memory at which to store Val, cast to 31648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// GenericValue *. It is not a pointer to a GenericValue containing the 31748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// address at which to store Val. 318f4cc3096fd893cdef5b5c2664ebff8c13a07ad51Chris Lattner void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr, 319db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Ty); 32048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar 32129794cba810ec249a9ede5ea77333a71579fd182Brian Gaeke void InitializeMemory(const Constant *Init, void *Addr); 32229794cba810ec249a9ede5ea77333a71579fd182Brian Gaeke 32348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// recompileAndRelinkFunction - This method is used to force a function which 32448dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// has already been compiled to be compiled again, possibly after it has been 32548dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// modified. Then the entry to the old copy is overwritten with a branch to 32648dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// the new copy. If there was no old copy, this acts just like 32748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// VM::getPointerToFunction(). 32818cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner virtual void *recompileAndRelinkFunction(Function *F) = 0; 32918cb4c3c747ac2439ca9fb1218edd8993df69d7aChris Lattner 330e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman /// freeMachineCodeForFunction - Release memory in the ExecutionEngine 331e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman /// corresponding to the machine code emitted to execute this function, useful 332e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman /// for garbage-collecting generated code. 333e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman virtual void freeMachineCodeForFunction(Function *F) = 0; 334e81aaf238c32dbc34274ede09fb16aa78dae9f53Misha Brukman 3351514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner /// getOrEmitGlobalVariable - Return the address of the specified global 3361514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner /// variable, possibly emitting it to memory if needed. This is used by the 3374c5b23b24f230607fa18a162519875a91a5e89e0Jeffrey Yasskin /// Emitter. 3381514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) { 3391514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner return getPointerToGlobal((GlobalValue*)GV); 3401514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner } 341df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin 342df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin /// Registers a listener to be called back on various events within 343df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin /// the JIT. See JITEventListener.h for more details. Does not 344df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin /// take ownership of the argument. The argument may be NULL, in 345df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin /// which case these functions do nothing. 346df225c0253aa987830fbd3dfcb5d9f8f7f973336Bill Wendling virtual void RegisterJITEventListener(JITEventListener *) {} 347df225c0253aa987830fbd3dfcb5d9f8f7f973336Bill Wendling virtual void UnregisterJITEventListener(JITEventListener *) {} 348df5a7daff9c7664bff8b713e8ed5155319bc6041Jeffrey Yasskin 34918fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin /// DisableLazyCompilation - When lazy compilation is off (the default), the 350dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// JIT will eagerly compile every function reachable from the argument to 351dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// getPointerToFunction. If lazy compilation is turned on, the JIT will only 352dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// compile the one function and emit stubs to compile the rest when they're 353dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// first called. If lazy compilation is turned off again while some lazy 354dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// stubs are still around, and one of those stubs is called, the program will 355dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// abort. 356dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// 357dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// In order to safely compile lazily in a threaded program, the user must 358dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// ensure that 1) only one thread at a time can call any particular lazy 359dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// stub, and 2) any thread modifying LLVM IR must hold the JIT's lock 360dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// (ExecutionEngine::lock) or otherwise ensure that no other thread calls a 361dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin /// lazy stub. See http://llvm.org/PR5184 for details. 36218fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin void DisableLazyCompilation(bool Disabled = true) { 36318fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin CompilingLazily = !Disabled; 3641c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner } 365dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin bool isCompilingLazily() const { 366dc85724f703bddf6988b6b3f20203beab775f32bJeffrey Yasskin return CompilingLazily; 3671c1a44699049cf56713a46ccaef7c747e4a888a3Chris Lattner } 36818fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin // Deprecated in favor of isCompilingLazily (to reduce double-negatives). 36918fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin // Remove this in LLVM 2.8. 37018fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin bool isLazyCompilationDisabled() const { 37118fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin return !CompilingLazily; 37218fec73e29d68b9c7473720507f51de39b0873b1Jeffrey Yasskin } 373446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng 37477f86ad08775e0ed2a704ef09ffff3dbd6e04583Evan Cheng /// DisableGVCompilation - If called, the JIT will abort if it's asked to 37577f86ad08775e0ed2a704ef09ffff3dbd6e04583Evan Cheng /// allocate space and populate a GlobalVariable that is not internal to 37677f86ad08775e0ed2a704ef09ffff3dbd6e04583Evan Cheng /// the module. 377446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng void DisableGVCompilation(bool Disabled = true) { 378446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng GVCompilationDisabled = Disabled; 379446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng } 380446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng bool isGVCompilationDisabled() const { 381446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng return GVCompilationDisabled; 382446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng } 383446531e7bb4355117b070493ca8e81e4b123ef18Evan Cheng 384e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown 385e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to 386e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner /// resolve symbols in a custom way. 387e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner void DisableSymbolSearching(bool Disabled = true) { 388e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner SymbolSearchingDisabled = Disabled; 389e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner } 390e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner bool isSymbolSearchingDisabled() const { 391e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner return SymbolSearchingDisabled; 392e846db667529eeeae531ce3b7382e18888d1eba0Chris Lattner } 3936f348e458660063a40052b208bab96895c822877Jeffrey Yasskin 394d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner /// InstallLazyFunctionCreator - If an unknown function is needed, the 395d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner /// specified function pointer is invoked to create it. If it returns null, 396d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner /// the JIT will abort. 397d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner void InstallLazyFunctionCreator(void* (*P)(const std::string &)) { 398d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner LazyFunctionCreator = P; 399d958a5a9feea7239a73c2068f43f237db550f46eChris Lattner } 400f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 401afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray /// InstallExceptionTableRegister - The JIT will use the given function 402afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray /// to register the exception tables it generates. 403b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands void InstallExceptionTableRegister(EERegisterFn F) { 404afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray ExceptionTableRegister = F; 405afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } 406b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands void InstallExceptionTableDeregister(EERegisterFn F) { 407b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands ExceptionTableDeregister = F; 408b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands } 409f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach 41048dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// RegisterTable - Registers the given pointer as an exception table. It 41148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// uses the ExceptionTableRegister function. 412515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher void RegisterTable(const Function *fn, void* res) { 413b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands if (ExceptionTableRegister) { 414afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray ExceptionTableRegister(res); 415515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher AllExceptionTables[fn] = res; 416515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher } 417515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher } 418515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher 419f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach /// DeregisterTable - Deregisters the exception frame previously registered 420f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach /// for the given function. 421515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher void DeregisterTable(const Function *Fn) { 422515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher if (ExceptionTableDeregister) { 423f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach DenseMap<const Function*, void*>::iterator frame = 424f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach AllExceptionTables.find(Fn); 425515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher if(frame != AllExceptionTables.end()) { 426515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher ExceptionTableDeregister(frame->second); 427515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher AllExceptionTables.erase(frame); 428515c67ee77f8d9c417efc0fe04615d269bfb70e4Eric Christopher } 429b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands } 430afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } 4311514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner 432b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands /// DeregisterAllTables - Deregisters all previously registered pointers to an 43348dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbar /// exception tables. It uses the ExceptionTableoDeregister function. 434b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands void DeregisterAllTables(); 435b35fd448cea32da671ecd3ecaad3cc637598c6e0Duncan Sands 43656adf152f6354a9b5609e059050fd2315ad5960cChris Lattnerprotected: 437f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin explicit ExecutionEngine(Module *M); 4389f2f142d255bc96f109dd5c6524a485937b1f3a1Chris Lattner 439bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner void emitGlobals(); 4403ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner 4411514b5b334c72e1e190fe1ce2bce7bf7d8a91c1aChris Lattner void EmitGlobalVariable(const GlobalVariable *GV); 4423ddc05bdde6ed65f93340ea467d362e80c0ceb9cChris Lattner 443bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner GenericValue getConstantValue(const Constant *C); 444f5b23368d27ef7dd44b2c45b5c08812ac99c7661Jim Grosbach void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr, 445db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Ty); 446bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner}; 447bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner 4484b1511b027ce0b648b3379f2891816c25b46f515Reid Klecknernamespace EngineKind { 4494b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner // These are actually bitmasks that get or-ed together. 4504b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner enum Kind { 4514b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner JIT = 0x1, 4524b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner Interpreter = 0x2 4534b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner }; 4544b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner const static Kind Either = (Kind)(JIT | Interpreter); 4554b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner} 4564b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 4574b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner/// EngineBuilder - Builder class for ExecutionEngines. Use this by 4584b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner/// stack-allocating a builder, chaining the various set* methods, and 4594b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner/// terminating it with a .create() call. 4604b1511b027ce0b648b3379f2891816c25b46f515Reid Klecknerclass EngineBuilder { 46148dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbarprivate: 462f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin Module *M; 4634b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner EngineKind::Kind WhichEngine; 4644b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner std::string *ErrorStr; 4654b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner CodeGenOpt::Level OptLevel; 4664b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner JITMemoryManager *JMM; 4674b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner bool AllocateGVsWithCode; 468439661395fd2a2a832dba01c65bc88718528313cEvan Cheng Reloc::Model RelocModel; 46988b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher CodeModel::Model CMModel; 4704688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin std::string MArch; 4714688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin std::string MCPU; 4724688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin SmallVector<std::string, 4> MAttrs; 4736d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar bool UseMCJIT; 4744b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 4754b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// InitEngine - Does the common initialization of default options. 4764b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner void InitEngine() { 4774b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner WhichEngine = EngineKind::Either; 4784b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner ErrorStr = NULL; 4794b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner OptLevel = CodeGenOpt::Default; 4804b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner JMM = NULL; 4814b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner AllocateGVsWithCode = false; 482439661395fd2a2a832dba01c65bc88718528313cEvan Cheng RelocModel = Reloc::Default; 48334ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng CMModel = CodeModel::JITDefault; 4846d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar UseMCJIT = false; 4854b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner } 4864b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 48748dd875be12006060260526e4a1df0bae48dd5c9Daniel Dunbarpublic: 4884b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// EngineBuilder - Constructor for EngineBuilder. If create() is called and 489f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin /// is successful, the created engine takes ownership of the module. 490f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin EngineBuilder(Module *m) : M(m) { 4914b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner InitEngine(); 4924b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner } 4934b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 4944b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// setEngineKind - Controls whether the user wants the interpreter, the JIT, 4954b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// or whichever engine works. This option defaults to EngineKind::Either. 4964b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner EngineBuilder &setEngineKind(EngineKind::Kind w) { 4974b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner WhichEngine = w; 4984b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner return *this; 4994b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner } 5004b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 5014b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// setJITMemoryManager - Sets the memory manager to use. This allows 5024b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// clients to customize their memory allocation policies. If create() is 5034b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// called and is successful, the created engine takes ownership of the 5044b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// memory manager. This option defaults to NULL. 5054b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner EngineBuilder &setJITMemoryManager(JITMemoryManager *jmm) { 5064b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner JMM = jmm; 5074b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner return *this; 5084b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner } 5094b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 5104b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// setErrorStr - Set the error string to write to on error. This option 5114b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// defaults to NULL. 5124b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner EngineBuilder &setErrorStr(std::string *e) { 5134b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner ErrorStr = e; 5144b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner return *this; 5154b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner } 5164b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 5174b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// setOptLevel - Set the optimization level for the JIT. This option 5184b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// defaults to CodeGenOpt::Default. 5194b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner EngineBuilder &setOptLevel(CodeGenOpt::Level l) { 5204b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner OptLevel = l; 5214b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner return *this; 5224b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner } 5234b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 524439661395fd2a2a832dba01c65bc88718528313cEvan Cheng /// setRelocationModel - Set the relocation model that the ExecutionEngine 525439661395fd2a2a832dba01c65bc88718528313cEvan Cheng /// target is using. Defaults to target specific default "Reloc::Default". 526439661395fd2a2a832dba01c65bc88718528313cEvan Cheng EngineBuilder &setRelocationModel(Reloc::Model RM) { 527439661395fd2a2a832dba01c65bc88718528313cEvan Cheng RelocModel = RM; 528439661395fd2a2a832dba01c65bc88718528313cEvan Cheng return *this; 529439661395fd2a2a832dba01c65bc88718528313cEvan Cheng } 530439661395fd2a2a832dba01c65bc88718528313cEvan Cheng 53188b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher /// setCodeModel - Set the CodeModel that the ExecutionEngine target 53234ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng /// data is using. Defaults to target specific default 53334ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng /// "CodeModel::JITDefault". 53488b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher EngineBuilder &setCodeModel(CodeModel::Model M) { 53588b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher CMModel = M; 53688b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher return *this; 53788b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher } 53888b5aca20a6dd0a8b15ff620bdee59aae567d245Eric Christopher 5394b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// setAllocateGVsWithCode - Sets whether global values should be allocated 5404b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// into the same buffer as code. For most applications this should be set 5414b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// to false. Allocating globals with code breaks freeMachineCodeForFunction 5424b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// and is probably unsafe and bad for performance. However, we have clients 5434b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// who depend on this behavior, so we must support it. This option defaults 5444b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// to false so that users of the new API can safely use the new memory 5454b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner /// manager and free machine code. 5464b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner EngineBuilder &setAllocateGVsWithCode(bool a) { 5474b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner AllocateGVsWithCode = a; 5484b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner return *this; 5494b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner } 5504b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 5514688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin /// setMArch - Override the architecture set by the Module's triple. 5524688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin EngineBuilder &setMArch(StringRef march) { 5534688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin MArch.assign(march.begin(), march.end()); 5544688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin return *this; 5554688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin } 5564688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin 5574688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin /// setMCPU - Target a specific cpu type. 5584688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin EngineBuilder &setMCPU(StringRef mcpu) { 5594688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin MCPU.assign(mcpu.begin(), mcpu.end()); 5604688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin return *this; 5614688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin } 5624688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin 5636d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar /// setUseMCJIT - Set whether the MC-JIT implementation should be used 5646d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar /// (experimental). 565714b34fc24d5df02aff01bd64eb80e5945d663acJim Grosbach EngineBuilder &setUseMCJIT(bool Value) { 5666d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar UseMCJIT = Value; 567714b34fc24d5df02aff01bd64eb80e5945d663acJim Grosbach return *this; 5686d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar } 5696d135972bf4e7fdc4de6b0538d6a3b91a06e3a5dDaniel Dunbar 5704688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin /// setMAttrs - Set cpu-specific attributes. 5714688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin template<typename StringSequence> 5724688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin EngineBuilder &setMAttrs(const StringSequence &mattrs) { 5734688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin MAttrs.clear(); 5744688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin MAttrs.append(mattrs.begin(), mattrs.end()); 5754688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin return *this; 5764688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin } 5774688261c20735f5ead2f08695acdeb727db31894Jeffrey Yasskin 5782ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith /// selectTarget - Pick a target either via -march or by guessing the native 5792ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith /// arch. Add any CPU features specified via -mcpu or -mattr. 5802ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith static TargetMachine *selectTarget(Module *M, 5812ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith StringRef MArch, 5822ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith StringRef MCPU, 5832ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith const SmallVectorImpl<std::string>& MAttrs, 584439661395fd2a2a832dba01c65bc88718528313cEvan Cheng Reloc::Model RM, 58534ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng CodeModel::Model CM, 5862ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith std::string *Err); 5872ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith 5884b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner ExecutionEngine *create(); 5894b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner}; 5904b1511b027ce0b648b3379f2891816c25b46f515Reid Kleckner 591d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace 592d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 593bd199fb1148b9e16c4e6f3d0ee386c2505a55b71Chris Lattner#endif 594