aa61989136de157bb5bf04a944fa5e4389a78fff
[cacao.git] / src / vm / jit / optimizing / recompile.c
1 /* src/vm/jit/optimizing/recompile.c - 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 <stdlib.h>
30
31 #include "vm/types.h"
32
33 #include "mm/memory.h"
34
35 #include "threads/lock-common.h"
36 #include "threads/thread.h"
37
38 #include "toolbox/list.h"
39
40 #include "vm/builtin.h"
41 #include "vm/exceptions.h"
42 #include "vm/stringlocal.h"
43
44 #include "vm/jit/code.h"
45 #include "vm/jit/jit.h"
46
47 #include "vm/jit/optimizing/recompile.h"
48
49 #include "vmcore/classcache.h"
50 #include "vmcore/options.h"
51
52
53 /* global variables ***********************************************************/
54
55 static java_object_t *lock_thread_recompile;
56 static list_t        *list_recompile_methods;
57
58
59 /* recompile_init **************************************************************
60
61    Initializes the recompilation system.
62
63 *******************************************************************************/
64
65 bool recompile_init(void)
66 {
67         TRACESUBSYSTEMINITIALIZATION("recompile_init");
68
69         /* initialize the recompile lock object */
70
71         lock_thread_recompile = NEW(java_object_t);
72
73         LOCK_INIT_OBJECT_LOCK(lock_thread_recompile);
74
75         /* create method list */
76
77         list_recompile_methods = list_create(OFFSET(list_method_entry, linkage));
78
79         /* everything's ok */
80
81         return true;
82 }
83
84
85 /* recompile_replace_vftbl *****************************************************
86
87    XXX
88
89 *******************************************************************************/
90
91 static void recompile_replace_vftbl(methodinfo *m)
92 {
93         codeinfo               *code;
94         codeinfo               *pcode;
95         u4                      slot;
96         classcache_name_entry  *nmen;
97         classcache_class_entry *clsen;
98         classinfo              *c;
99         vftbl_t                *vftbl;
100         s4                      i;
101
102         /* get current and previous codeinfo structure */
103
104         code  = m->code;
105         pcode = code->prev;
106
107         assert(pcode);
108
109         /* iterate over all classes */
110
111         for (slot = 0; slot < hashtable_classcache.size; slot++) {
112                 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
113
114                 for (; nmen; nmen = nmen->hashlink) {
115                         /* iterate over all class entries */
116
117                         for (clsen = nmen->classes; clsen; clsen = clsen->next) {
118                                 c = clsen->classobj;
119
120                                 if (c == NULL)
121                                         continue;
122
123                                 /* Search for entrypoint of the previous codeinfo in
124                                    the vftbl and replace it with the current one. */
125
126                                 vftbl = c->vftbl;
127
128                                 /* Is the class linked? Means, is the vftbl finished? */
129
130                                 if (!(c->state & CLASS_LINKED))
131                                         continue;
132
133                                 /* Does the class have a vftbl? Some internal classes
134                                    (e.g. $NEW$) are linked, but do not have a
135                                    vftbl. */
136
137                                 if (vftbl == NULL)
138                                         continue;
139
140                                 for (i = 0; i < vftbl->vftbllength; i++) {
141                                         if (vftbl->table[i] == pcode->entrypoint) {
142 #if !defined(NDEBUG)
143                                                 printf("replacing vftbl in: ");
144                                                 class_println(c);
145 #endif
146                                                 vftbl->table[i] = code->entrypoint;
147                                         }
148                                 }
149                         }
150                 }
151         }
152 }
153
154
155 /* recompile_thread ************************************************************
156
157    XXX
158
159 *******************************************************************************/
160
161 static void recompile_thread(void)
162 {
163         list_method_entry *lme;
164
165         while (true) {
166                 /* get the lock on the recompile lock object, so we can call wait */
167
168                 LOCK_MONITOR_ENTER(lock_thread_recompile);
169
170                 /* wait forever on that object till we are signaled */
171         
172                 LOCK_WAIT_FOREVER(lock_thread_recompile);
173
174                 /* leave the lock */
175
176                 LOCK_MONITOR_EXIT(lock_thread_recompile);
177
178                 /* get the next method and recompile it */
179
180                 while ((lme = list_first(list_recompile_methods)) != NULL) {
181                         /* recompile this method */
182
183                         if (jit_recompile(lme->m) != NULL) {
184                                 /* replace in vftbl's */
185
186                                 recompile_replace_vftbl(lme->m);
187                         }
188                         else {
189                                 /* XXX what is the right-thing(tm) to do here? */
190
191                                 exceptions_print_current_exception();
192                         }
193
194                         /* remove the compiled method */
195
196                         list_remove(list_recompile_methods, lme);
197
198                         /* free the entry */
199
200                         FREE(lme, list_method_entry);
201                 }
202         }
203 }
204
205
206 /* recompile_start_thread ******************************************************
207
208    Starts the recompilation thread.
209
210 *******************************************************************************/
211
212 bool recompile_start_thread(void)
213 {
214         utf *name;
215
216         name = utf_new_char("Recompiler");
217
218         if (!threads_thread_start_internal(name, recompile_thread))
219                 return false;
220
221         /* everything's ok */
222
223         return true;
224 }
225
226
227 /* recompile_queue_method ******************************************************
228
229    Adds a method to the recompilation list and signal the
230    recompilation thread that there is some work to do.
231
232 *******************************************************************************/
233
234 void recompile_queue_method(methodinfo *m)
235 {
236         list_method_entry *lme;
237
238         /* create a method entry */
239
240         lme = NEW(list_method_entry);
241         lme->m = m;
242
243         /* and add it to the list */
244
245         list_add_last(list_recompile_methods, lme);
246
247         /* get the lock on the recompile lock object, so we can call notify */
248
249         LOCK_MONITOR_ENTER(lock_thread_recompile);
250
251         /* signal the recompiler thread */
252         
253         LOCK_NOTIFY(lock_thread_recompile);
254
255         /* leave the lock */
256
257         LOCK_MONITOR_EXIT(lock_thread_recompile);
258 }
259
260
261 /*
262  * These are local overrides for various environment variables in Emacs.
263  * Please do not remove this and leave it at the end of the file, where
264  * Emacs will automagically detect them.
265  * ---------------------------------------------------------------------
266  * Local variables:
267  * mode: c
268  * indent-tabs-mode: t
269  * c-basic-offset: 4
270  * tab-width: 4
271  * End:
272  */