2009-04-11 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / mini-llvm-cpp.cpp
1 //
2 // mini-llvm-cpp.cpp: C++ support classes for the mono LLVM integration
3 //
4 // (C) 2009 Novell, Inc.
5 //
6
7 //
8 // We need to override some stuff in LLVM, but this cannot be done using the C
9 // interface, so we have to use some C++ code here.
10 // The things which we override are:
11 // - the default JIT code manager used by LLVM doesn't allocate memory using
12 //   MAP_32BIT, we require it.
13 // - add some callbacks so we can obtain the size of methods and their exception
14 //   tables.
15 //
16
17 //
18 // Mono's internal header files are not C++ clean, so avoid including them if 
19 // possible
20 //
21
22 #include <stdint.h>
23
24 #include <llvm/PassManager.h>
25 #include <llvm/ExecutionEngine/ExecutionEngine.h>
26 #include <llvm/ExecutionEngine/JITMemoryManager.h>
27 #include <llvm/Target/TargetOptions.h>
28 #include <llvm/Target/TargetData.h>
29 #include <llvm/Analysis/Verifier.h>
30 #include <llvm/Transforms/Scalar.h>
31
32 #include "llvm-c/Core.h"
33 #include "llvm-c/ExecutionEngine.h"
34
35 #include "mini-llvm-cpp.h"
36
37 using namespace llvm;
38
39 class MonoJITMemoryManager : public JITMemoryManager
40 {
41 private:
42         JITMemoryManager *mm;
43
44 public:
45         /* Callbacks installed by mono */
46         AllocCodeMemoryCb *alloc_cb;
47         FunctionEmittedCb *emitted_cb;
48
49         MonoJITMemoryManager ();
50         ~MonoJITMemoryManager ();
51
52         void setMemoryWritable (void);
53
54         void setMemoryExecutable (void);
55
56         void AllocateGOT();
57
58     unsigned char *getGOTBase() const {
59                 return mm->getGOTBase ();
60     }
61     
62     void *getDlsymTable() const {
63                 return mm->getDlsymTable ();
64     }
65       
66         void SetDlsymTable(void *ptr);
67   
68         unsigned char *startFunctionBody(const Function *F, 
69                                                                          uintptr_t &ActualSize);
70   
71         unsigned char *allocateStub(const GlobalValue* F, unsigned StubSize,
72                                                                  unsigned Alignment);
73   
74         void endFunctionBody(const Function *F, unsigned char *FunctionStart,
75                                                  unsigned char *FunctionEnd);
76
77         unsigned char *allocateSpace(intptr_t Size, unsigned Alignment);
78   
79         void deallocateMemForFunction(const Function *F);
80   
81         unsigned char*startExceptionTable(const Function* F,
82                                                                           uintptr_t &ActualSize);
83   
84         void endExceptionTable(const Function *F, unsigned char *TableStart,
85                                                    unsigned char *TableEnd, 
86                                                    unsigned char* FrameRegister);
87 };
88
89 MonoJITMemoryManager::MonoJITMemoryManager ()
90 {
91         SizeRequired = true;
92         mm = JITMemoryManager::CreateDefaultMemManager ();
93 }
94
95 MonoJITMemoryManager::~MonoJITMemoryManager ()
96 {
97 }
98
99 void
100 MonoJITMemoryManager::setMemoryWritable (void)
101 {
102 }
103
104 void
105 MonoJITMemoryManager::setMemoryExecutable (void)
106 {
107 }
108
109 void
110 MonoJITMemoryManager::AllocateGOT()
111 {
112         mm->AllocateGOT ();
113 }
114   
115 void
116 MonoJITMemoryManager::SetDlsymTable(void *ptr)
117 {
118         mm->SetDlsymTable (ptr);
119 }
120   
121 unsigned char *
122 MonoJITMemoryManager::startFunctionBody(const Function *F, 
123                                         uintptr_t &ActualSize)
124 {
125         return alloc_cb (wrap (F), ActualSize);
126 }
127   
128 unsigned char *
129 MonoJITMemoryManager::allocateStub(const GlobalValue* F, unsigned StubSize,
130                            unsigned Alignment)
131 {
132         return alloc_cb (wrap (F), StubSize);
133 }
134   
135 void
136 MonoJITMemoryManager::endFunctionBody(const Function *F, unsigned char *FunctionStart,
137                                   unsigned char *FunctionEnd)
138 {
139         emitted_cb (wrap (F), FunctionStart, FunctionEnd);
140 }
141
142 unsigned char *
143 MonoJITMemoryManager::allocateSpace(intptr_t Size, unsigned Alignment)
144 {
145         return new unsigned char [Size];
146 }
147   
148 void
149 MonoJITMemoryManager::deallocateMemForFunction(const Function *F)
150 {
151 }
152   
153 unsigned char*
154 MonoJITMemoryManager::startExceptionTable(const Function* F,
155                                           uintptr_t &ActualSize)
156 {
157         return alloc_cb (wrap (F), ActualSize);
158 }
159   
160 void
161 MonoJITMemoryManager::endExceptionTable(const Function *F, unsigned char *TableStart,
162                                         unsigned char *TableEnd, 
163                                         unsigned char* FrameRegister)
164 {
165 }
166
167 static MonoJITMemoryManager *mono_mm;
168
169 static FunctionPassManager *fpm;
170
171 void
172 mono_llvm_optimize_method (LLVMValueRef method)
173 {
174         verifyFunction (*(unwrap<Function> (method)));
175         fpm->run (*unwrap<Function> (method));
176 }
177
178 /* Missing overload for building an alloca with an alignment */
179 LLVMValueRef
180 mono_llvm_build_alloca (LLVMBuilderRef builder, LLVMTypeRef Ty, 
181                                                 LLVMValueRef ArraySize,
182                                                 int alignment, const char *Name)
183 {
184         return wrap (unwrap (builder)->Insert (new AllocaInst(unwrap (Ty), unwrap (ArraySize), alignment), Name));
185 }
186
187 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef, LLVMTypeRef Ty,
188                                   LLVMValueRef Val, const char *Name);
189
190
191 LLVMExecutionEngineRef
192 mono_llvm_create_ee (LLVMModuleProviderRef MP, AllocCodeMemoryCb *alloc_cb, FunctionEmittedCb *emitted_cb, ExceptionTableCb *exception_cb)
193 {
194   std::string Error;
195
196   mono_mm = new MonoJITMemoryManager ();
197   mono_mm->alloc_cb = alloc_cb;
198   mono_mm->emitted_cb = emitted_cb;
199
200   ExceptionHandling = true;
201
202   ExecutionEngine *EE = ExecutionEngine::createJIT (unwrap (MP), &Error, mono_mm, false);
203   EE->InstallExceptionTableRegister (exception_cb);
204
205   fpm = new FunctionPassManager (unwrap (MP));
206
207   fpm->add(new TargetData(*EE->getTargetData()));
208   /* Add a random set of passes */
209   /* Make this run-time configurable */
210   fpm->add(createInstructionCombiningPass());
211   fpm->add(createReassociatePass());
212   fpm->add(createGVNPass());
213   fpm->add(createCFGSimplificationPass());
214
215   return wrap(EE);
216 }