blob: bf1e804cd412364444be90c0ba1b1d8dbf0728e0 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- JIT.h - Class definition for the JIT --------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the top-level JIT data structure.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef JIT_H
15#define JIT_H
16
17#include "llvm/ExecutionEngine/ExecutionEngine.h"
18#include "llvm/PassManager.h"
19#include <map>
20
21namespace llvm {
22
23class Function;
24class GlobalValue;
25class Constant;
26class TargetMachine;
27class TargetJITInfo;
28class MachineCodeEmitter;
29
30class JITState {
31private:
32 FunctionPassManager PM; // Passes to compile a function
33
34 /// PendingGlobals - Global variables which have had memory allocated for them
35 /// while a function was code generated, but which have not been initialized
36 /// yet.
37 std::vector<const GlobalVariable*> PendingGlobals;
38
39public:
40 JITState(ModuleProvider *MP) : PM(MP) {}
41
42 FunctionPassManager &getPM(const MutexGuard &L) {
43 return PM;
44 }
45
46 std::vector<const GlobalVariable*> &getPendingGlobals(const MutexGuard &L) {
47 return PendingGlobals;
48 }
49};
50
51
52class JIT : public ExecutionEngine {
53 TargetMachine &TM; // The current target we are compiling to
54 TargetJITInfo &TJI; // The JITInfo for the target we are compiling to
55 MachineCodeEmitter *MCE; // MCE object
56
57 JITState jitstate;
58
Chris Lattner4db98aa2007-12-06 01:34:04 +000059 JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
60 JITMemoryManager *JMM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061public:
62 ~JIT();
63
64 static void Register() {
65 JITCtor = create;
66 }
67
68 /// getJITInfo - Return the target JIT information structure.
69 ///
70 TargetJITInfo &getJITInfo() const { return TJI; }
71
72 /// create - Create an return a new JIT compiler if there is one available
73 /// for the current target. Otherwise, return null.
74 ///
Chris Lattner4db98aa2007-12-06 01:34:04 +000075 static ExecutionEngine *create(ModuleProvider *MP, std::string *Err) {
76 return createJIT(MP, Err, 0);
77 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078
79 /// run - Start execution with the specified function and arguments.
80 ///
81 virtual GenericValue runFunction(Function *F,
82 const std::vector<GenericValue> &ArgValues);
83
84 /// getPointerToNamedFunction - This method returns the address of the
85 /// specified function by using the dlsym function call. As such it is only
86 /// useful for resolving library symbols, not code generated symbols.
87 ///
88 void *getPointerToNamedFunction(const std::string &Name);
89
90 // CompilationCallback - Invoked the first time that a call site is found,
91 // which causes lazy compilation of the target function.
92 //
93 static void CompilationCallback();
94
95 /// getPointerToFunction - This returns the address of the specified function,
96 /// compiling it if necessary.
97 ///
98 void *getPointerToFunction(Function *F);
99
100 /// getOrEmitGlobalVariable - Return the address of the specified global
101 /// variable, possibly emitting it to memory if needed. This is used by the
102 /// Emitter.
103 void *getOrEmitGlobalVariable(const GlobalVariable *GV);
104
105 /// getPointerToFunctionOrStub - If the specified function has been
106 /// code-gen'd, return a pointer to the function. If not, compile it, or use
107 /// a stub to implement lazy compilation if available.
108 ///
109 void *getPointerToFunctionOrStub(Function *F);
110
111 /// recompileAndRelinkFunction - This method is used to force a function
112 /// which has already been compiled, to be compiled again, possibly
113 /// after it has been modified. Then the entry to the old copy is overwritten
114 /// with a branch to the new copy. If there was no old copy, this acts
115 /// just like JIT::getPointerToFunction().
116 ///
117 void *recompileAndRelinkFunction(Function *F);
118
119 /// freeMachineCodeForFunction - deallocate memory used to code-generate this
120 /// Function.
121 ///
122 void freeMachineCodeForFunction(Function *F);
123
124 /// getCodeEmitter - Return the code emitter this JIT is emitting into.
125 MachineCodeEmitter *getCodeEmitter() const { return MCE; }
Chris Lattner4db98aa2007-12-06 01:34:04 +0000126
127 static ExecutionEngine *createJIT(ModuleProvider *MP, std::string *Err,
128 JITMemoryManager *JMM);
129
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130private:
Chris Lattnere44be002007-12-06 01:08:09 +0000131 static MachineCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 void runJITOnFunction (Function *F);
133};
134
135} // End llvm namespace
136
137#endif