5ada79b8b9cf128cc2fe74458ed030fd96fc5da8
[cacao.git] / src / vm / jit / optimizing / recompile.c
1 /* src/vm/jit/optimizing/recompile.c - recompilation system
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: cacao.c 4357 2006-01-22 23:33:38Z twisti $
32
33 */
34
35
36 #include "config.h"
37
38 #include <assert.h>
39 #include <stdlib.h>
40
41 #include "vm/types.h"
42
43 #include "mm/memory.h"
44 #include "native/jni.h"
45 #include "native/include/java_lang_Thread.h"
46 #include "native/include/java_lang_VMThread.h"
47
48 #if defined(ENABLE_THREADS)
49 # include "threads/native/lock.h"
50 # include "threads/native/threads.h"
51 #endif
52
53 #include "toolbox/list.h"
54 #include "vm/builtin.h"
55 #include "vm/classcache.h"
56 #include "vm/exceptions.h"
57 #include "vm/stringlocal.h"
58 #include "vm/jit/optimizing/recompile.h"
59
60
61 /* global variables ***********************************************************/
62
63 static java_lang_VMThread *recompile_vmthread;
64 static java_objectheader *lock_recompile_thread;
65 static list *list_recompile_methods;
66
67
68 /* recompile_init **************************************************************
69
70    Initializes the recompilation system.
71
72 *******************************************************************************/
73
74 bool recompile_init(void)
75 {
76         /* initialize the recompile lock object */
77
78         lock_recompile_thread = NEW(java_objectheader);
79
80         lock_init_object_lock(lock_recompile_thread);
81
82         /* create method list */
83
84         list_recompile_methods = list_create(OFFSET(list_method_entry, linkage));
85
86         /* everything's ok */
87
88         return true;
89 }
90
91
92 /* recompile_replace_vftbl *****************************************************
93
94    XXX
95
96 *******************************************************************************/
97
98 static void recompile_replace_vftbl(methodinfo *m)
99 {
100         codeinfo               *code;
101         codeinfo               *pcode;
102         u4                      slot;
103         classcache_name_entry  *nmen;
104         classcache_class_entry *clsen;
105         classinfo              *c;
106         vftbl_t                *vftbl;
107         s4                      i;
108
109         /* get current and previous codeinfo structure */
110
111         code  = m->code;
112         pcode = code->prev;
113
114         assert(pcode);
115
116         /* iterate over all classes */
117
118         for (slot = 0; slot < hashtable_classcache.size; slot++) {
119                 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
120
121                 for (; nmen; nmen = nmen->hashlink) {
122                         /* iterate over all class entries */
123
124                         for (clsen = nmen->classes; clsen; clsen = clsen->next) {
125                                 c = clsen->classobj;
126
127                                 if (c == NULL)
128                                         continue;
129
130                                 /* Search for entrypoint of the previous codeinfo in
131                                    the vftbl and replace it with the current one. */
132
133                                 vftbl = c->vftbl;
134
135                                 /* Is the class linked? Means, is the vftbl finished? */
136
137                                 if (!(c->state & CLASS_LINKED))
138                                         continue;
139
140                                 /* Does the class have a vftbl? Some internal classes
141                                    (e.g. $NEW$) are linked, but do not have a
142                                    vftbl. */
143
144                                 if (vftbl == NULL)
145                                         continue;
146
147                                 for (i = 0; i < vftbl->vftbllength; i++) {
148                                         if (vftbl->table[i] == pcode->entrypoint) {
149 #if !defined(NDEBUG)
150                                                 printf("replacing vftbl in: ");
151                                                 class_println(c);
152 #endif
153                                                 vftbl->table[i] = code->entrypoint;
154                                         }
155                                 }
156                         }
157                 }
158         }
159 }
160
161
162 /* recompile_thread ************************************************************
163
164    XXX
165
166 *******************************************************************************/
167
168 static void recompile_thread(void)
169 {
170         list_method_entry *lme;
171
172         while (true) {
173                 /* get the lock on the recompile lock object, so we can call wait */
174
175                 lock_monitor_enter(lock_recompile_thread);
176
177                 /* wait forever (0, 0) on that object till we are signaled */
178         
179                 lock_wait_for_object(lock_recompile_thread, 0, 0);
180
181                 /* leave the lock */
182
183                 lock_monitor_exit(lock_recompile_thread);
184
185                 /* get the next method and recompile it */
186
187                 while ((lme = list_first(list_recompile_methods)) != NULL) {
188                         /* recompile this method */
189
190                         if (jit_recompile(lme->m) != NULL) {
191                                 /* replace in vftbl's */
192
193                                 recompile_replace_vftbl(lme->m);
194                         }
195                         else {
196                                 /* XXX what is the right-thing(tm) to do here? */
197
198                                 exceptions_print_exception(*exceptionptr);
199                         }
200
201                         /* remove the compiled method */
202
203                         list_remove(list_recompile_methods, lme);
204
205                         /* free the entry */
206
207                         FREE(lme, list_method_entry);
208                 }
209         }
210 }
211
212
213 /* recompile_start_thread ******************************************************
214
215    Starts the recompilation thread.
216
217 *******************************************************************************/
218
219 bool recompile_start_thread(void)
220 {
221         java_lang_Thread *t;
222
223         /* create the profile object */
224
225         recompile_vmthread =
226                 (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
227
228         if (recompile_vmthread == NULL)
229                 return false;
230
231         t = (java_lang_Thread *) builtin_new(class_java_lang_Thread);
232
233         t->vmThread = recompile_vmthread;
234         t->name     = javastring_new_from_ascii("Recompiler");
235         t->daemon   = true;
236         t->priority = 5;
237
238         recompile_vmthread->thread = t;
239
240         /* actually start the recompilation thread */
241
242         threads_start_thread(t, recompile_thread);
243
244         /* everything's ok */
245
246         return true;
247 }
248
249
250 /* recompile_queue_method ******************************************************
251
252    Adds a method to the recompilation list and signal the
253    recompilation thread that there is some work to do.
254
255 *******************************************************************************/
256
257 void recompile_queue_method(methodinfo *m)
258 {
259         list_method_entry *lme;
260
261         /* create a method entry */
262
263         lme = NEW(list_method_entry);
264         lme->m = m;
265
266         /* and add it to the list */
267
268         list_add_last(list_recompile_methods, lme);
269
270         /* get the lock on the recompile lock object, so we can call notify */
271
272         lock_monitor_enter(lock_recompile_thread);
273
274         /* signal the recompiler thread */
275         
276         lock_notify_object(lock_recompile_thread);
277
278         /* leave the lock */
279
280         lock_monitor_exit(lock_recompile_thread);
281 }
282
283
284 /*
285  * These are local overrides for various environment variables in Emacs.
286  * Please do not remove this and leave it at the end of the file, where
287  * Emacs will automagically detect them.
288  * ---------------------------------------------------------------------
289  * Local variables:
290  * mode: c
291  * indent-tabs-mode: t
292  * c-basic-offset: 4
293  * tab-width: 4
294  * End:
295  */