Merged.
[cacao.git] / src / vm / jit / optimizing / recompiler.cpp
1 /* src/vm/jit/optimizing/recompiler.cpp - recompilation system
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdint.h>
30
31 #include "mm/memory.h"
32
33 #include "threads/condition.hpp"
34 #include "threads/mutex.hpp"
35 #include "threads/thread.hpp"
36
37 #include "vm/classcache.hpp"
38 #include "vm/exceptions.hpp"
39 #include "vm/options.h"
40 #include "vm/string.hpp"
41
42 #include "vm/jit/builtin.hpp"
43 #include "vm/jit/code.hpp"
44 #include "vm/jit/jit.hpp"
45
46 #include "vm/jit/optimizing/recompiler.hpp"
47
48
49 /**
50  * Stop the worker thread.
51  */
52 Recompiler::~Recompiler()
53 {
54         // Set the running flag to false.
55         _run = false;
56
57         // Now signal the worker thread.
58         _cond.signal();
59
60         // TODO We should wait here until the thread exits.
61 }
62
63
64 /* recompile_replace_vftbl *****************************************************
65
66    XXX
67
68 *******************************************************************************/
69
70 static void recompile_replace_vftbl(methodinfo *m)
71 {
72         codeinfo               *code;
73         codeinfo               *pcode;
74         u4                      slot;
75         classcache_name_entry  *nmen;
76         classcache_class_entry *clsen;
77         classinfo              *c;
78         vftbl_t                *vftbl;
79         s4                      i;
80
81         /* get current and previous codeinfo structure */
82
83         code  = m->code;
84         pcode = code->prev;
85
86         assert(pcode);
87
88         /* iterate over all classes */
89
90         for (slot = 0; slot < hashtable_classcache.size; slot++) {
91                 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
92
93                 for (; nmen; nmen = nmen->hashlink) {
94                         /* iterate over all class entries */
95
96                         for (clsen = nmen->classes; clsen; clsen = clsen->next) {
97                                 c = clsen->classobj;
98
99                                 if (c == NULL)
100                                         continue;
101
102                                 /* Search for entrypoint of the previous codeinfo in
103                                    the vftbl and replace it with the current one. */
104
105                                 vftbl = c->vftbl;
106
107                                 /* Is the class linked? Means, is the vftbl finished? */
108
109                                 if (!(c->state & CLASS_LINKED))
110                                         continue;
111
112                                 /* Does the class have a vftbl? Some internal classes
113                                    (e.g. $NEW$) are linked, but do not have a
114                                    vftbl. */
115
116                                 if (vftbl == NULL)
117                                         continue;
118
119                                 for (i = 0; i < vftbl->vftbllength; i++) {
120                                         if (vftbl->table[i] == pcode->entrypoint) {
121 #if !defined(NDEBUG)
122                                                 printf("replacing vftbl in: ");
123                                                 class_println(c);
124 #endif
125                                                 vftbl->table[i] = code->entrypoint;
126                                         }
127                                 }
128                         }
129                 }
130         }
131 }
132
133
134 /**
135  * The actual recompilation thread.
136  */
137 void Recompiler::thread()
138 {
139         // FIXME This just works for one recompiler.
140         Recompiler& r = VM::get_current()->get_recompiler();
141
142         while (r._run == true) {
143                 // Enter the recompile mutex, so we can call wait.
144                 r._mutex.lock();
145
146                 // Wait forever on that condition until we are signaled.
147                 r._cond.wait(r._mutex);
148
149                 // Leave the mutex.
150                 r._mutex.unlock();
151
152                 // FIXME Move this into the for loop.
153                 if (r._run == false)
154                         break;
155
156                 // Sanity check.
157                 assert(r._methods.empty() == false);
158
159                 // Get the next method form the queue and recompile it.
160                 while (r._methods.empty() == false) {
161                         methodinfo* m = r._methods.front();
162
163                         // Recompile this method.
164                         if (jit_recompile(m) != NULL) {
165                                 // Replace in vftbl's.
166                                 recompile_replace_vftbl(m);
167                         }
168                         else {
169                                 // XXX What is the right-thing(tm) to do here?
170                                 exceptions_print_current_exception();
171                         }
172
173                         // Remove the method from the queue.
174                         r._methods.pop();
175                 }
176         }
177 }
178
179
180 /**
181  * Start the recompilation thread.
182  *
183  * @return true on success, false otherwise.
184  */
185 bool Recompiler::start()
186 {
187         utf *name = utf_new_char("Recompiler");
188
189         if (!threads_thread_start_internal(name, (functionptr) &Recompiler::thread))
190                 return false;
191
192         return true;
193 }
194
195
196 /**
197  * Add a method to the recompilation queue and signal the
198  * recompilation thread that there is some work to do.
199  *
200  * @param m Method to recompile.
201  */
202 void Recompiler::queue_method(methodinfo *m)
203 {
204         // Add the method to the queue.
205         _methods.push(m);
206
207         // Enter the recompile mutex, so we can call notify.
208         _mutex.lock();
209
210         // Signal the recompiler thread.
211         _cond.signal();
212
213         // Leave the mutex.
214         _mutex.unlock();
215 }
216
217
218
219 // Legacy C interface.
220 extern "C" {
221         void Recompiler_queue_method(methodinfo* m) { VM::get_current()->get_recompiler().queue_method(m); }
222 }
223
224 /*
225  * These are local overrides for various environment variables in Emacs.
226  * Please do not remove this and leave it at the end of the file, where
227  * Emacs will automagically detect them.
228  * ---------------------------------------------------------------------
229  * Local variables:
230  * mode: c++
231  * indent-tabs-mode: t
232  * c-basic-offset: 4
233  * tab-width: 4
234  * End:
235  * vim:noexpandtab:sw=4:ts=4:
236  */