1 /* src/vm/jit/optimizing/recompile.c - recompilation system
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
33 #include "mm/memory.h"
35 #include "threads/lock-common.h"
36 #include "threads/thread.h"
38 #include "toolbox/list.h"
40 #include "vm/builtin.h"
41 #include "vm/exceptions.h"
42 #include "vm/stringlocal.h"
44 #include "vm/jit/code.h"
45 #include "vm/jit/jit.h"
47 #include "vm/jit/optimizing/recompile.h"
49 #include "vmcore/classcache.h"
50 #include "vmcore/options.h"
53 /* global variables ***********************************************************/
55 static java_object_t *lock_thread_recompile;
56 static list_t *list_recompile_methods;
59 /* recompile_init **************************************************************
61 Initializes the recompilation system.
63 *******************************************************************************/
65 bool recompile_init(void)
67 TRACESUBSYSTEMINITIALIZATION("recompile_init");
69 /* initialize the recompile lock object */
71 lock_thread_recompile = NEW(java_object_t);
73 LOCK_INIT_OBJECT_LOCK(lock_thread_recompile);
75 /* create method list */
77 list_recompile_methods = list_create(OFFSET(list_method_entry, linkage));
85 /* recompile_replace_vftbl *****************************************************
89 *******************************************************************************/
91 static void recompile_replace_vftbl(methodinfo *m)
96 classcache_name_entry *nmen;
97 classcache_class_entry *clsen;
102 /* get current and previous codeinfo structure */
109 /* iterate over all classes */
111 for (slot = 0; slot < hashtable_classcache.size; slot++) {
112 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
114 for (; nmen; nmen = nmen->hashlink) {
115 /* iterate over all class entries */
117 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
123 /* Search for entrypoint of the previous codeinfo in
124 the vftbl and replace it with the current one. */
128 /* Is the class linked? Means, is the vftbl finished? */
130 if (!(c->state & CLASS_LINKED))
133 /* Does the class have a vftbl? Some internal classes
134 (e.g. $NEW$) are linked, but do not have a
140 for (i = 0; i < vftbl->vftbllength; i++) {
141 if (vftbl->table[i] == pcode->entrypoint) {
143 printf("replacing vftbl in: ");
146 vftbl->table[i] = code->entrypoint;
155 /* recompile_thread ************************************************************
159 *******************************************************************************/
161 static void recompile_thread(void)
163 list_method_entry *lme;
166 /* get the lock on the recompile lock object, so we can call wait */
168 LOCK_MONITOR_ENTER(lock_thread_recompile);
170 /* wait forever on that object till we are signaled */
172 LOCK_WAIT_FOREVER(lock_thread_recompile);
176 LOCK_MONITOR_EXIT(lock_thread_recompile);
178 /* get the next method and recompile it */
180 while ((lme = list_first(list_recompile_methods)) != NULL) {
181 /* recompile this method */
183 if (jit_recompile(lme->m) != NULL) {
184 /* replace in vftbl's */
186 recompile_replace_vftbl(lme->m);
189 /* XXX what is the right-thing(tm) to do here? */
191 exceptions_print_current_exception();
194 /* remove the compiled method */
196 list_remove(list_recompile_methods, lme);
200 FREE(lme, list_method_entry);
206 /* recompile_start_thread ******************************************************
208 Starts the recompilation thread.
210 *******************************************************************************/
212 bool recompile_start_thread(void)
216 name = utf_new_char("Recompiler");
218 if (!threads_thread_start_internal(name, recompile_thread))
221 /* everything's ok */
227 /* recompile_queue_method ******************************************************
229 Adds a method to the recompilation list and signal the
230 recompilation thread that there is some work to do.
232 *******************************************************************************/
234 void recompile_queue_method(methodinfo *m)
236 list_method_entry *lme;
238 /* create a method entry */
240 lme = NEW(list_method_entry);
243 /* and add it to the list */
245 list_add_last(list_recompile_methods, lme);
247 /* get the lock on the recompile lock object, so we can call notify */
249 LOCK_MONITOR_ENTER(lock_thread_recompile);
251 /* signal the recompiler thread */
253 LOCK_NOTIFY(lock_thread_recompile);
257 LOCK_MONITOR_EXIT(lock_thread_recompile);
262 * These are local overrides for various environment variables in Emacs.
263 * Please do not remove this and leave it at the end of the file, where
264 * Emacs will automagically detect them.
265 * ---------------------------------------------------------------------
268 * indent-tabs-mode: t