1 /* src/vm/jit/optimizing/recompiler.cpp - 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
31 #include "mm/memory.h"
33 #include "threads/condition.hpp"
34 #include "threads/mutex.hpp"
35 #include "threads/thread.hpp"
37 #include "vm/classcache.hpp"
38 #include "vm/exceptions.hpp"
39 #include "vm/options.h"
40 #include "vm/string.hpp"
42 #include "vm/jit/builtin.hpp"
43 #include "vm/jit/code.hpp"
44 #include "vm/jit/jit.hpp"
46 #include "vm/jit/optimizing/recompiler.hpp"
50 * Stop the worker thread.
52 Recompiler::~Recompiler()
54 // Set the running flag to false.
57 // Now signal the worker thread.
60 // TODO We should wait here until the thread exits.
64 /* recompile_replace_vftbl *****************************************************
68 *******************************************************************************/
70 static void recompile_replace_vftbl(methodinfo *m)
75 classcache_name_entry *nmen;
76 classcache_class_entry *clsen;
81 /* get current and previous codeinfo structure */
88 /* iterate over all classes */
90 for (slot = 0; slot < hashtable_classcache.size; slot++) {
91 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
93 for (; nmen; nmen = nmen->hashlink) {
94 /* iterate over all class entries */
96 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
102 /* Search for entrypoint of the previous codeinfo in
103 the vftbl and replace it with the current one. */
107 /* Is the class linked? Means, is the vftbl finished? */
109 if (!(c->state & CLASS_LINKED))
112 /* Does the class have a vftbl? Some internal classes
113 (e.g. $NEW$) are linked, but do not have a
119 for (i = 0; i < vftbl->vftbllength; i++) {
120 if (vftbl->table[i] == pcode->entrypoint) {
122 printf("replacing vftbl in: ");
125 vftbl->table[i] = code->entrypoint;
135 * The actual recompilation thread.
137 void Recompiler::thread()
139 // FIXME This just works for one recompiler.
140 Recompiler& r = VM::get_current()->get_recompiler();
142 while (r._run == true) {
143 // Enter the recompile mutex, so we can call wait.
146 // Wait forever on that condition until we are signaled.
147 r._cond.wait(r._mutex);
152 // FIXME Move this into the for loop.
157 assert(r._methods.empty() == false);
159 // Get the next method form the queue and recompile it.
160 while (r._methods.empty() == false) {
161 methodinfo* m = r._methods.front();
163 // Recompile this method.
164 if (jit_recompile(m) != NULL) {
165 // Replace in vftbl's.
166 recompile_replace_vftbl(m);
169 // XXX What is the right-thing(tm) to do here?
170 exceptions_print_current_exception();
173 // Remove the method from the queue.
181 * Start the recompilation thread.
183 * @return true on success, false otherwise.
185 bool Recompiler::start()
187 utf *name = utf_new_char("Recompiler");
189 if (!threads_thread_start_internal(name, (functionptr) &Recompiler::thread))
197 * Add a method to the recompilation queue and signal the
198 * recompilation thread that there is some work to do.
200 * @param m Method to recompile.
202 void Recompiler::queue_method(methodinfo *m)
204 // Add the method to the queue.
207 // Enter the recompile mutex, so we can call notify.
210 // Signal the recompiler thread.
219 // Legacy C interface.
221 void Recompiler_queue_method(methodinfo* m) { VM::get_current()->get_recompiler().queue_method(m); }
225 * These are local overrides for various environment variables in Emacs.
226 * Please do not remove this and leave it at the end of the file, where
227 * Emacs will automagically detect them.
228 * ---------------------------------------------------------------------
231 * indent-tabs-mode: t
235 * vim:noexpandtab:sw=4:ts=4: