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 #if defined(ENABLE_THREADS)
40 # include "threads/threads-common.h"
42 # include "threads/native/lock.h"
43 # include "threads/native/threads.h"
46 #include "toolbox/list.h"
48 #include "vm/builtin.h"
49 #include "vm/exceptions.h"
50 #include "vm/stringlocal.h"
52 #include "vm/jit/code.h"
53 #include "vm/jit/jit.h"
55 #include "vm/jit/optimizing/recompile.h"
57 #include "vmcore/classcache.h"
60 /* global variables ***********************************************************/
62 static threadobject *thread_recompile;
63 static java_objectheader *lock_thread_recompile;
64 static list *list_recompile_methods;
67 /* recompile_init **************************************************************
69 Initializes the recompilation system.
71 *******************************************************************************/
73 bool recompile_init(void)
75 /* initialize the recompile lock object */
77 lock_thread_recompile = NEW(java_objectheader);
79 lock_init_object_lock(lock_thread_recompile);
81 /* create method list */
83 list_recompile_methods = list_create(OFFSET(list_method_entry, linkage));
91 /* recompile_replace_vftbl *****************************************************
95 *******************************************************************************/
97 static void recompile_replace_vftbl(methodinfo *m)
102 classcache_name_entry *nmen;
103 classcache_class_entry *clsen;
108 /* get current and previous codeinfo structure */
115 /* iterate over all classes */
117 for (slot = 0; slot < hashtable_classcache.size; slot++) {
118 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
120 for (; nmen; nmen = nmen->hashlink) {
121 /* iterate over all class entries */
123 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
129 /* Search for entrypoint of the previous codeinfo in
130 the vftbl and replace it with the current one. */
134 /* Is the class linked? Means, is the vftbl finished? */
136 if (!(c->state & CLASS_LINKED))
139 /* Does the class have a vftbl? Some internal classes
140 (e.g. $NEW$) are linked, but do not have a
146 for (i = 0; i < vftbl->vftbllength; i++) {
147 if (vftbl->table[i] == pcode->entrypoint) {
149 printf("replacing vftbl in: ");
152 vftbl->table[i] = code->entrypoint;
161 /* recompile_thread ************************************************************
165 *******************************************************************************/
167 static void recompile_thread(void)
169 list_method_entry *lme;
172 /* get the lock on the recompile lock object, so we can call wait */
174 lock_monitor_enter(lock_thread_recompile);
176 /* wait forever (0, 0) on that object till we are signaled */
178 lock_wait_for_object(lock_thread_recompile, 0, 0);
182 lock_monitor_exit(lock_thread_recompile);
184 /* get the next method and recompile it */
186 while ((lme = list_first(list_recompile_methods)) != NULL) {
187 /* recompile this method */
189 if (jit_recompile(lme->m) != NULL) {
190 /* replace in vftbl's */
192 recompile_replace_vftbl(lme->m);
195 /* XXX what is the right-thing(tm) to do here? */
197 exceptions_print_current_exception();
200 /* remove the compiled method */
202 list_remove(list_recompile_methods, lme);
206 FREE(lme, list_method_entry);
212 /* recompile_start_thread ******************************************************
214 Starts the recompilation thread.
216 *******************************************************************************/
218 bool recompile_start_thread(void)
222 name = utf_new_char("Recompiler");
224 thread_recompile = threads_create_thread(name);
226 if (thread_recompile == NULL)
229 /* actually start the recompilation thread */
231 threads_start_thread(thread_recompile, recompile_thread);
233 /* everything's ok */
239 /* recompile_queue_method ******************************************************
241 Adds a method to the recompilation list and signal the
242 recompilation thread that there is some work to do.
244 *******************************************************************************/
246 void recompile_queue_method(methodinfo *m)
248 list_method_entry *lme;
250 /* create a method entry */
252 lme = NEW(list_method_entry);
255 /* and add it to the list */
257 list_add_last(list_recompile_methods, lme);
259 /* get the lock on the recompile lock object, so we can call notify */
261 lock_monitor_enter(lock_thread_recompile);
263 /* signal the recompiler thread */
265 lock_notify_object(lock_thread_recompile);
269 lock_monitor_exit(lock_thread_recompile);
274 * These are local overrides for various environment variables in Emacs.
275 * Please do not remove this and leave it at the end of the file, where
276 * Emacs will automagically detect them.
277 * ---------------------------------------------------------------------
280 * indent-tabs-mode: t