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
35 #include "mm/memory.h"
37 #include "threads/lock-common.h"
38 #include "threads/threads-common.h"
40 #include "toolbox/list.h"
42 #include "vm/builtin.h"
43 #include "vm/exceptions.h"
44 #include "vm/stringlocal.h"
46 #include "vm/jit/code.h"
47 #include "vm/jit/jit.h"
49 #include "vm/jit/optimizing/recompile.h"
51 #include "vmcore/classcache.h"
54 /* global variables ***********************************************************/
56 static java_object_t *lock_thread_recompile;
57 static list_t *list_recompile_methods;
60 /* recompile_init **************************************************************
62 Initializes the recompilation system.
64 *******************************************************************************/
66 bool recompile_init(void)
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 (0, 0) on that object till we are signaled */
171 lock_wait_for_object(lock_thread_recompile, 0, 0);
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_object(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