blob: b9ff06e701f0fe2b1dfba19270f52eafafec633f [file] [log] [blame]
Daniel Dunbar6aec2982010-11-17 16:06:43 +00001//===-- MCJIT.h - Class definition for the MCJIT ----------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_LIB_EXECUTIONENGINE_MCJIT_H
11#define LLVM_LIB_EXECUTIONENGINE_MCJIT_H
12
Jim Grosbach31649e62011-03-18 22:48:41 +000013#include "llvm/PassManager.h"
Daniel Dunbar6aec2982010-11-17 16:06:43 +000014#include "llvm/ExecutionEngine/ExecutionEngine.h"
Jim Grosbachf9229102011-03-22 01:06:42 +000015#include "llvm/ExecutionEngine/RuntimeDyld.h"
Daniel Dunbar6aec2982010-11-17 16:06:43 +000016
17namespace llvm {
18
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000019class ObjectImage;
20
Jim Grosbach31649e62011-03-18 22:48:41 +000021// FIXME: This makes all kinds of horrible assumptions for the time being,
22// like only having one module, not needing to worry about multi-threading,
23// blah blah. Purely in get-it-up-and-limping mode for now.
24
Daniel Dunbar6aec2982010-11-17 16:06:43 +000025class MCJIT : public ExecutionEngine {
Jim Grosbach8005bcd2012-08-21 15:42:49 +000026 MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
27 bool AllocateGVsWithCode);
Jim Grosbach31649e62011-03-18 22:48:41 +000028
29 TargetMachine *TM;
30 MCContext *Ctx;
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000031 RTDyldMemoryManager *MemMgr;
Andrew Kaylorea708d12012-08-07 18:33:00 +000032 RuntimeDyld Dyld;
Jim Grosbach31649e62011-03-18 22:48:41 +000033
Andrew Kaylorea708d12012-08-07 18:33:00 +000034 // FIXME: Add support for multiple modules
35 bool isCompiled;
Jim Grosbach31649e62011-03-18 22:48:41 +000036 Module *M;
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000037 OwningPtr<ObjectImage> LoadedObject;
Jim Grosbach31649e62011-03-18 22:48:41 +000038
Daniel Dunbar6aec2982010-11-17 16:06:43 +000039public:
40 ~MCJIT();
41
42 /// @name ExecutionEngine interface implementation
43 /// @{
44
45 virtual void *getPointerToBasicBlock(BasicBlock *BB);
46
47 virtual void *getPointerToFunction(Function *F);
48
49 virtual void *recompileAndRelinkFunction(Function *F);
50
51 virtual void freeMachineCodeForFunction(Function *F);
52
53 virtual GenericValue runFunction(Function *F,
54 const std::vector<GenericValue> &ArgValues);
55
Jim Grosbach34714a02011-03-22 18:05:27 +000056 /// getPointerToNamedFunction - This method returns the address of the
57 /// specified function by using the dlsym function call. As such it is only
58 /// useful for resolving library symbols, not code generated symbols.
59 ///
60 /// If AbortOnFailure is false and no function with the given name is
61 /// found, this function silently returns a null pointer. Otherwise,
62 /// it prints a message to stderr and aborts.
63 ///
Danil Malyshev45a93d62012-01-05 21:16:14 +000064 virtual void *getPointerToNamedFunction(const std::string &Name,
65 bool AbortOnFailure = true);
Danil Malyshev30b9e322012-03-28 21:46:36 +000066
Jim Grosbach020f4e82012-01-16 23:50:55 +000067 /// mapSectionAddress - map a section to its target address space value.
68 /// Map the address of a JIT section as returned from the memory manager
69 /// to the address in the target process as the running code will see it.
70 /// This is the address which will be used for relocation resolution.
Jim Grosbache940c1b2012-09-13 21:50:06 +000071 virtual void mapSectionAddress(const void *LocalAddress,
72 uint64_t TargetAddress) {
Jim Grosbach020f4e82012-01-16 23:50:55 +000073 Dyld.mapSectionAddress(LocalAddress, TargetAddress);
74 }
75
Daniel Dunbar6aec2982010-11-17 16:06:43 +000076 /// @}
77 /// @name (Private) Registration Interfaces
78 /// @{
79
80 static void Register() {
81 MCJITCtor = createJIT;
82 }
83
Daniel Dunbar6aec2982010-11-17 16:06:43 +000084 static ExecutionEngine *createJIT(Module *M,
85 std::string *ErrorStr,
86 JITMemoryManager *JMM,
Daniel Dunbar6aec2982010-11-17 16:06:43 +000087 bool GVsWithCode,
Dylan Noblesmithc5b28582011-05-13 21:51:29 +000088 TargetMachine *TM);
Daniel Dunbar6aec2982010-11-17 16:06:43 +000089
90 // @}
Andrew Kaylorea708d12012-08-07 18:33:00 +000091
92protected:
93 /// emitObject -- Generate a JITed object in memory from the specified module
94 /// Currently, MCJIT only supports a single module and the module passed to
95 /// this function call is expected to be the contained module. The module
96 /// is passed as a parameter here to prepare for multiple module support in
97 /// the future.
98 void emitObject(Module *M);
Daniel Dunbar6aec2982010-11-17 16:06:43 +000099};
100
101} // End llvm namespace
102
103#endif