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