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 java_objectheader *lock_thread_recompile;
59 static list_t *list_recompile_methods;
62 /* recompile_init **************************************************************
64 Initializes the recompilation system.
66 *******************************************************************************/
68 bool recompile_init(void)
70 /* initialize the recompile lock object */
72 lock_thread_recompile = NEW(java_objectheader);
74 lock_init_object_lock(lock_thread_recompile);
76 /* create method list */
78 list_recompile_methods = list_create(OFFSET(list_method_entry, linkage));
86 /* recompile_replace_vftbl *****************************************************
90 *******************************************************************************/
92 static void recompile_replace_vftbl(methodinfo *m)
97 classcache_name_entry *nmen;
98 classcache_class_entry *clsen;
103 /* get current and previous codeinfo structure */
110 /* iterate over all classes */
112 for (slot = 0; slot < hashtable_classcache.size; slot++) {
113 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
115 for (; nmen; nmen = nmen->hashlink) {
116 /* iterate over all class entries */
118 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
124 /* Search for entrypoint of the previous codeinfo in
125 the vftbl and replace it with the current one. */
129 /* Is the class linked? Means, is the vftbl finished? */
131 if (!(c->state & CLASS_LINKED))
134 /* Does the class have a vftbl? Some internal classes
135 (e.g. $NEW$) are linked, but do not have a
141 for (i = 0; i < vftbl->vftbllength; i++) {
142 if (vftbl->table[i] == pcode->entrypoint) {
144 printf("replacing vftbl in: ");
147 vftbl->table[i] = code->entrypoint;
156 /* recompile_thread ************************************************************
160 *******************************************************************************/
162 static void recompile_thread(void)
164 list_method_entry *lme;
167 /* get the lock on the recompile lock object, so we can call wait */
169 lock_monitor_enter(lock_thread_recompile);
171 /* wait forever (0, 0) on that object till we are signaled */
173 lock_wait_for_object(lock_thread_recompile, 0, 0);
177 lock_monitor_exit(lock_thread_recompile);
179 /* get the next method and recompile it */
181 while ((lme = list_first(list_recompile_methods)) != NULL) {
182 /* recompile this method */
184 if (jit_recompile(lme->m) != NULL) {
185 /* replace in vftbl's */
187 recompile_replace_vftbl(lme->m);
190 /* XXX what is the right-thing(tm) to do here? */
192 exceptions_print_current_exception();
195 /* remove the compiled method */
197 list_remove(list_recompile_methods, lme);
201 FREE(lme, list_method_entry);
207 /* recompile_start_thread ******************************************************
209 Starts the recompilation thread.
211 *******************************************************************************/
213 bool recompile_start_thread(void)
217 name = utf_new_char("Recompiler");
219 if (!threads_thread_start_internal(name, recompile_thread))
222 /* everything's ok */
228 /* recompile_queue_method ******************************************************
230 Adds a method to the recompilation list and signal the
231 recompilation thread that there is some work to do.
233 *******************************************************************************/
235 void recompile_queue_method(methodinfo *m)
237 list_method_entry *lme;
239 /* create a method entry */
241 lme = NEW(list_method_entry);
244 /* and add it to the list */
246 list_add_last(list_recompile_methods, lme);
248 /* get the lock on the recompile lock object, so we can call notify */
250 lock_monitor_enter(lock_thread_recompile);
252 /* signal the recompiler thread */
254 lock_notify_object(lock_thread_recompile);
258 lock_monitor_exit(lock_thread_recompile);
263 * These are local overrides for various environment variables in Emacs.
264 * Please do not remove this and leave it at the end of the file, where
265 * Emacs will automagically detect them.
266 * ---------------------------------------------------------------------
269 * indent-tabs-mode: t