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