1 /* src/vm/jit/optimizing/recompile.c - recompilation system
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
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 Contact: cacao@cacaojvm.org
27 Authors: Christian Thalinger
29 $Id: cacao.c 4357 2006-01-22 23:33:38Z twisti $
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"
46 #if defined(ENABLE_THREADS)
47 # include "threads/native/lock.h"
48 # include "threads/native/threads.h"
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"
59 /* global variables ***********************************************************/
61 static threadobject *thread_recompile;
62 static java_objectheader *lock_thread_recompile;
63 static list *list_recompile_methods;
66 /* recompile_init **************************************************************
68 Initializes the recompilation system.
70 *******************************************************************************/
72 bool recompile_init(void)
74 /* initialize the recompile lock object */
76 lock_thread_recompile = NEW(java_objectheader);
78 lock_init_object_lock(lock_thread_recompile);
80 /* create method list */
82 list_recompile_methods = list_create(OFFSET(list_method_entry, linkage));
90 /* recompile_replace_vftbl *****************************************************
94 *******************************************************************************/
96 static void recompile_replace_vftbl(methodinfo *m)
101 classcache_name_entry *nmen;
102 classcache_class_entry *clsen;
107 /* get current and previous codeinfo structure */
114 /* iterate over all classes */
116 for (slot = 0; slot < hashtable_classcache.size; slot++) {
117 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
119 for (; nmen; nmen = nmen->hashlink) {
120 /* iterate over all class entries */
122 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
128 /* Search for entrypoint of the previous codeinfo in
129 the vftbl and replace it with the current one. */
133 /* Is the class linked? Means, is the vftbl finished? */
135 if (!(c->state & CLASS_LINKED))
138 /* Does the class have a vftbl? Some internal classes
139 (e.g. $NEW$) are linked, but do not have a
145 for (i = 0; i < vftbl->vftbllength; i++) {
146 if (vftbl->table[i] == pcode->entrypoint) {
148 printf("replacing vftbl in: ");
151 vftbl->table[i] = code->entrypoint;
160 /* recompile_thread ************************************************************
164 *******************************************************************************/
166 static void recompile_thread(void)
168 list_method_entry *lme;
171 /* get the lock on the recompile lock object, so we can call wait */
173 lock_monitor_enter(lock_thread_recompile);
175 /* wait forever (0, 0) on that object till we are signaled */
177 lock_wait_for_object(lock_thread_recompile, 0, 0);
181 lock_monitor_exit(lock_thread_recompile);
183 /* get the next method and recompile it */
185 while ((lme = list_first(list_recompile_methods)) != NULL) {
186 /* recompile this method */
188 if (jit_recompile(lme->m) != NULL) {
189 /* replace in vftbl's */
191 recompile_replace_vftbl(lme->m);
194 /* XXX what is the right-thing(tm) to do here? */
196 exceptions_print_exception(*exceptionptr);
199 /* remove the compiled method */
201 list_remove(list_recompile_methods, lme);
205 FREE(lme, list_method_entry);
211 /* recompile_start_thread ******************************************************
213 Starts the recompilation thread.
215 *******************************************************************************/
217 bool recompile_start_thread(void)
219 #if defined(WITH_CLASSPATH_GNU)
220 java_lang_VMThread *vmt;
223 /* create the profile object */
225 thread_recompile = (threadobject *) builtin_new(class_java_lang_Thread);
227 if (thread_recompile == NULL)
230 #if defined(WITH_CLASSPATH_GNU)
231 vmt = (java_lang_VMThread *) builtin_new(class_java_lang_VMThread);
233 vmt->thread = (java_lang_Thread *) thread_recompile;
235 thread_recompile->o.vmThread = vmt;
238 thread_recompile->o.name = javastring_new_from_ascii("Recompiler");
239 thread_recompile->o.daemon = true;
240 thread_recompile->o.priority = 5;
242 /* actually start the recompilation thread */
244 threads_start_thread(thread_recompile, recompile_thread);
246 /* everything's ok */
252 /* recompile_queue_method ******************************************************
254 Adds a method to the recompilation list and signal the
255 recompilation thread that there is some work to do.
257 *******************************************************************************/
259 void recompile_queue_method(methodinfo *m)
261 list_method_entry *lme;
263 /* create a method entry */
265 lme = NEW(list_method_entry);
268 /* and add it to the list */
270 list_add_last(list_recompile_methods, lme);
272 /* get the lock on the recompile lock object, so we can call notify */
274 lock_monitor_enter(lock_thread_recompile);
276 /* signal the recompiler thread */
278 lock_notify_object(lock_thread_recompile);
282 lock_monitor_exit(lock_thread_recompile);
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 * ---------------------------------------------------------------------
293 * indent-tabs-mode: t