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.hpp"
38 #include "toolbox/list.h"
40 #include "vm/builtin.h"
41 #include "vm/classcache.h"
42 #include "vm/exceptions.hpp"
43 #include "vm/options.h"
44 #include "vm/string.hpp"
46 #include "vm/jit/code.h"
47 #include "vm/jit/jit.hpp"
49 #include "vm/jit/optimizing/recompile.h"
52 /* global variables ***********************************************************/
54 static java_object_t *lock_thread_recompile;
55 static list_t *list_recompile_methods;
58 /* recompile_init **************************************************************
60 Initializes the recompilation system.
62 *******************************************************************************/
64 bool recompile_init(void)
66 TRACESUBSYSTEMINITIALIZATION("recompile_init");
68 /* initialize the recompile lock object */
70 lock_thread_recompile = NEW(java_object_t);
72 LOCK_INIT_OBJECT_LOCK(lock_thread_recompile);
74 /* create method list */
76 list_recompile_methods = list_create(OFFSET(list_method_entry, linkage));
84 /* recompile_replace_vftbl *****************************************************
88 *******************************************************************************/
90 static void recompile_replace_vftbl(methodinfo *m)
95 classcache_name_entry *nmen;
96 classcache_class_entry *clsen;
101 /* get current and previous codeinfo structure */
108 /* iterate over all classes */
110 for (slot = 0; slot < hashtable_classcache.size; slot++) {
111 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
113 for (; nmen; nmen = nmen->hashlink) {
114 /* iterate over all class entries */
116 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
122 /* Search for entrypoint of the previous codeinfo in
123 the vftbl and replace it with the current one. */
127 /* Is the class linked? Means, is the vftbl finished? */
129 if (!(c->state & CLASS_LINKED))
132 /* Does the class have a vftbl? Some internal classes
133 (e.g. $NEW$) are linked, but do not have a
139 for (i = 0; i < vftbl->vftbllength; i++) {
140 if (vftbl->table[i] == pcode->entrypoint) {
142 printf("replacing vftbl in: ");
145 vftbl->table[i] = code->entrypoint;
154 /* recompile_thread ************************************************************
158 *******************************************************************************/
160 static void recompile_thread(void)
162 list_method_entry *lme;
165 /* get the lock on the recompile lock object, so we can call wait */
167 LOCK_MONITOR_ENTER(lock_thread_recompile);
169 /* wait forever on that object till we are signaled */
171 LOCK_WAIT_FOREVER(lock_thread_recompile);
175 LOCK_MONITOR_EXIT(lock_thread_recompile);
177 /* get the next method and recompile it */
179 while ((lme = list_first(list_recompile_methods)) != NULL) {
180 /* recompile this method */
182 if (jit_recompile(lme->m) != NULL) {
183 /* replace in vftbl's */
185 recompile_replace_vftbl(lme->m);
188 /* XXX what is the right-thing(tm) to do here? */
190 exceptions_print_current_exception();
193 /* remove the compiled method */
195 list_remove(list_recompile_methods, lme);
199 FREE(lme, list_method_entry);
205 /* recompile_start_thread ******************************************************
207 Starts the recompilation thread.
209 *******************************************************************************/
211 bool recompile_start_thread(void)
215 name = utf_new_char("Recompiler");
217 if (!threads_thread_start_internal(name, recompile_thread))
220 /* everything's ok */
226 /* recompile_queue_method ******************************************************
228 Adds a method to the recompilation list and signal the
229 recompilation thread that there is some work to do.
231 *******************************************************************************/
233 void recompile_queue_method(methodinfo *m)
235 list_method_entry *lme;
237 /* create a method entry */
239 lme = NEW(list_method_entry);
242 /* and add it to the list */
244 list_add_last(list_recompile_methods, lme);
246 /* get the lock on the recompile lock object, so we can call notify */
248 LOCK_MONITOR_ENTER(lock_thread_recompile);
250 /* signal the recompiler thread */
252 LOCK_NOTIFY(lock_thread_recompile);
256 LOCK_MONITOR_EXIT(lock_thread_recompile);
261 * These are local overrides for various environment variables in Emacs.
262 * Please do not remove this and leave it at the end of the file, where
263 * Emacs will automagically detect them.
264 * ---------------------------------------------------------------------
267 * indent-tabs-mode: t