PR163: descriptor_params_from_paramtypes is protected by a mutex now (per descriptor...
[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, 2009
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.hpp"
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                 // Get the next method form the queue and recompile it.
157                 while (r._methods.empty() == false) {
158                         methodinfo* m = r._methods.front();
159
160                         // Recompile this method.
161                         if (jit_recompile(m) != NULL) {
162                                 // Replace in vftbl's.
163                                 recompile_replace_vftbl(m);
164                         }
165                         else {
166                                 // XXX What is the right-thing(tm) to do here?
167                                 exceptions_print_current_exception();
168                         }
169
170                         // Remove the method from the queue.
171                         r._methods.pop();
172                 }
173         }
174 }
175
176
177 /**
178  * Start the recompilation thread.
179  *
180  * @return true on success, false otherwise.
181  */
182 bool Recompiler::start()
183 {
184         utf *name = utf_new_char("Recompiler");
185
186         if (!threads_thread_start_internal(name, (functionptr) &Recompiler::thread))
187                 return false;
188
189         return true;
190 }
191
192
193 /**
194  * Add a method to the recompilation queue and signal the
195  * recompilation thread that there is some work to do.
196  *
197  * @param m Method to recompile.
198  */
199 void Recompiler::queue_method(methodinfo *m)
200 {
201         // Add the method to the queue.
202         _methods.push(m);
203
204         // Enter the recompile mutex, so we can call notify.
205         _mutex.lock();
206
207         // Signal the recompiler thread.
208         _cond.signal();
209
210         // Leave the mutex.
211         _mutex.unlock();
212 }
213
214
215
216 // Legacy C interface.
217 extern "C" {
218         void Recompiler_queue_method(methodinfo* m) { VM::get_current()->get_recompiler().queue_method(m); }
219 }
220
221 /*
222  * These are local overrides for various environment variables in Emacs.
223  * Please do not remove this and leave it at the end of the file, where
224  * Emacs will automagically detect them.
225  * ---------------------------------------------------------------------
226  * Local variables:
227  * mode: c++
228  * indent-tabs-mode: t
229  * c-basic-offset: 4
230  * tab-width: 4
231  * End:
232  * vim:noexpandtab:sw=4:ts=4:
233  */