1 /* src/vm/jit/optimizing/recompile.c - recompilation system
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
8 This file is part of CACAO.
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.
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.
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
25 $Id: cacao.c 4357 2006-01-22 23:33:38Z twisti $
37 #include "mm/memory.h"
39 #include "threads/lock-common.h"
40 #include "threads/threads-common.h"
42 #include "toolbox/list.h"
44 #include "vm/builtin.h"
45 #include "vm/exceptions.h"
46 #include "vm/stringlocal.h"
48 #include "vm/jit/code.h"
49 #include "vm/jit/jit.h"
51 #include "vm/jit/optimizing/recompile.h"
53 #include "vmcore/classcache.h"
56 /* global variables ***********************************************************/
58 static threadobject *thread_recompile;
59 static java_objectheader *lock_thread_recompile;
60 static list_t *list_recompile_methods;
63 /* recompile_init **************************************************************
65 Initializes the recompilation system.
67 *******************************************************************************/
69 bool recompile_init(void)
71 /* initialize the recompile lock object */
73 lock_thread_recompile = NEW(java_objectheader);
75 lock_init_object_lock(lock_thread_recompile);
77 /* create method list */
79 list_recompile_methods = list_create(OFFSET(list_method_entry, linkage));
87 /* recompile_replace_vftbl *****************************************************
91 *******************************************************************************/
93 static void recompile_replace_vftbl(methodinfo *m)
98 classcache_name_entry *nmen;
99 classcache_class_entry *clsen;
104 /* get current and previous codeinfo structure */
111 /* iterate over all classes */
113 for (slot = 0; slot < hashtable_classcache.size; slot++) {
114 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
116 for (; nmen; nmen = nmen->hashlink) {
117 /* iterate over all class entries */
119 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
125 /* Search for entrypoint of the previous codeinfo in
126 the vftbl and replace it with the current one. */
130 /* Is the class linked? Means, is the vftbl finished? */
132 if (!(c->state & CLASS_LINKED))
135 /* Does the class have a vftbl? Some internal classes
136 (e.g. $NEW$) are linked, but do not have a
142 for (i = 0; i < vftbl->vftbllength; i++) {
143 if (vftbl->table[i] == pcode->entrypoint) {
145 printf("replacing vftbl in: ");
148 vftbl->table[i] = code->entrypoint;
157 /* recompile_thread ************************************************************
161 *******************************************************************************/
163 static void recompile_thread(void)
165 list_method_entry *lme;
168 /* get the lock on the recompile lock object, so we can call wait */
170 lock_monitor_enter(lock_thread_recompile);
172 /* wait forever (0, 0) on that object till we are signaled */
174 lock_wait_for_object(lock_thread_recompile, 0, 0);
178 lock_monitor_exit(lock_thread_recompile);
180 /* get the next method and recompile it */
182 while ((lme = list_first(list_recompile_methods)) != NULL) {
183 /* recompile this method */
185 if (jit_recompile(lme->m) != NULL) {
186 /* replace in vftbl's */
188 recompile_replace_vftbl(lme->m);
191 /* XXX what is the right-thing(tm) to do here? */
193 exceptions_print_current_exception();
196 /* remove the compiled method */
198 list_remove(list_recompile_methods, lme);
202 FREE(lme, list_method_entry);
208 /* recompile_start_thread ******************************************************
210 Starts the recompilation thread.
212 *******************************************************************************/
214 bool recompile_start_thread(void)
218 name = utf_new_char("Recompiler");
220 thread_recompile = threads_thread_create_internal(name);
222 if (thread_recompile == NULL)
225 /* actually start the recompilation thread */
227 threads_start_thread(thread_recompile, recompile_thread);
229 /* everything's ok */
235 /* recompile_queue_method ******************************************************
237 Adds a method to the recompilation list and signal the
238 recompilation thread that there is some work to do.
240 *******************************************************************************/
242 void recompile_queue_method(methodinfo *m)
244 list_method_entry *lme;
246 /* create a method entry */
248 lme = NEW(list_method_entry);
251 /* and add it to the list */
253 list_add_last(list_recompile_methods, lme);
255 /* get the lock on the recompile lock object, so we can call notify */
257 lock_monitor_enter(lock_thread_recompile);
259 /* signal the recompiler thread */
261 lock_notify_object(lock_thread_recompile);
265 lock_monitor_exit(lock_thread_recompile);
270 * These are local overrides for various environment variables in Emacs.
271 * Please do not remove this and leave it at the end of the file, where
272 * Emacs will automagically detect them.
273 * ---------------------------------------------------------------------
276 * indent-tabs-mode: t