2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Interface for external Java coalescer.
23 * @author Sebastian Hack
31 #include "lc_opts_enum.h"
45 #include "bejavacoal.h"
51 /* Path to the jar file. A little OS dependent convenience. */
53 static char jar_file[512] = "y:\\user\\hack\\public\\coal.jar";
55 static char jar_file[512] = "/ben/hack/public/coal.jar";
58 static char cls_name[256] = "coalescing/mst/safe/Algo";
60 /* Name of the JVM dll/so */
61 static char jvm_lib[512] = { 0 };
63 static const lc_opt_table_entry_t options[] = {
64 LC_OPT_ENT_STR ("jvm", "absolute path to jvm dll", jvm_lib, sizeof(jvm_lib)),
65 LC_OPT_ENT_STR ("jar", "jar file of the coalescer", jar_file, sizeof(jar_file)),
66 LC_OPT_ENT_STR ("cls", "name of the class providing the factory", cls_name, sizeof(cls_name)),
70 void be_init_javacoal(void)
72 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
73 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
74 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
75 lc_opt_entry_t *jc_grp = lc_opt_get_grp(chordal_grp, "jc");
76 lc_opt_add_table(jc_grp, options);
79 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_javacoal);
83 typedef struct _jni_env_t {
90 Ugly code to retrieve the JVM dll/so file.
96 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
98 HINSTANCE hVM = LoadLibrary(vmlibpath);
99 return hVM ? GetProcAddress(hVM, sym) : NULL;
102 #define JRE_KEY "SOFTWARE\\JavaSoft\\Java Development Kit"
104 static char *locate_jvm_lib(char *path, size_t path_len)
108 DWORD version_len = sizeof(version);
109 DWORD dwPathLen = path_len;
112 RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_QUERY_VALUE, &hKey);
113 RegQueryValueEx(hKey, "CurrentVersion", NULL, NULL, (LPBYTE) version, &version_len);
116 _snprintf(buf, sizeof(buf), JRE_KEY "\\%s", version);
117 RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &hKey);
118 RegQueryValueEx(hKey, "JavaHome", NULL, NULL, (LPBYTE) path, &dwPathLen);
121 strncat(path, "\\jre\\bin\\server\\jvm.dll", path_len);
127 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
129 void *libVM = dlopen(vmlibpath, RTLD_LAZY);
130 return libVM ? dlsym(libVM, sym) : NULL;
133 static char *locate_jvm_lib(char *path, size_t n)
141 static int start_vm(jni_env_t *env, int argc, char *argv[])
148 long (JNICALL * create_func)(JavaVM **, void **, void *) = find_jvm_symbol(jvm_lib, "JNI_CreateJavaVM");
151 fprintf(stderr, "could not find JVM creation function\n");
155 memset(&args, 0, sizeof(args));
156 opts = xmalloc(argc * sizeof(opts[0]));
157 for(i = 0; i < argc; ++i) {
158 opts[i].optionString = argv[i];
159 opts[i].extraInfo = NULL;
162 args.version = JNI_VERSION_1_4;
163 args.nOptions = argc;
165 args.ignoreUnrecognized = JNI_FALSE;
167 ret = create_func(&env->jvm, (void **) &env->jni, &args);
170 fprintf(stderr, "JNI_CreateJavaVM returned errrocode %ld\n" , ret);
177 static void stop_vm(jni_env_t *env)
179 JavaVM *jvm = env->jvm;
180 (*jvm)->DetachCurrentThread(jvm);
181 (*jvm)->DestroyJavaVM(jvm);
184 static int jvm_inited = 0;
185 static jni_env_t env;
186 void (*old_int_handler)(int);
187 void (*old_abrt_handler)(int);
189 static void sig_jvm_destroy_at_exit(int signal)
196 old_abrt_handler(signal);
199 old_int_handler(signal);
205 static void jvm_destroy_at_exit(void)
207 sig_jvm_destroy_at_exit(0);
210 static jni_env_t *get_jvm(void)
217 if(strlen(jvm_lib) == 0) {
218 if(!locate_jvm_lib(jvm_lib, sizeof(jvm_lib))) {
219 fprintf(stderr, "could not find jvm library\n");
224 snprintf(cp_param, sizeof(cp_param), "-Djava.class.path=%s", jar_file);
226 if(!start_vm(&env, sizeof(args) / sizeof(args[0]), args)) {
227 fprintf(stderr, "Couldn't initialize java VM\n");
231 old_int_handler = signal(SIGINT, sig_jvm_destroy_at_exit);
232 old_abrt_handler = signal(SIGABRT, sig_jvm_destroy_at_exit);
233 atexit(jvm_destroy_at_exit);
240 static void check(jni_env_t *env, const char *file, int line)
242 JNIEnv *jni = env->jni;
243 jboolean exc = (*jni)->ExceptionCheck(jni);
245 fprintf(stderr, "%s:%d: ", file, line);
246 (*jni)->ExceptionDescribe(jni);
247 (*jni)->ExceptionClear(jni);
253 #define CHECK(env) check(env, __FILE__, __LINE__)
273 static const struct _mth_info_t mthis[mth_last] = {
274 { "addIntEdge", "(II)V" }, /* public void addIntEdge(int, int); */
275 { "addAffEdge", "(III)V" }, /* public void addAffEdge(int, int, int); */
276 { "setColor", "(II)V" }, /* public void setColor(int, int); */
277 //{ "setDebug", "(ILjava/lang/String;)V" }, /* public void setDebug(int, String); */
278 { "getColor", "(I)I" }, /* public int getColor(int); */
279 { "forbidColor", "(II)V" }, /* public void forbidColor(int, int); */
280 { "coalesce", "()V" }, /* public void coalesce(); */
281 { "dump", "(Ljava/lang/String;)V" }, /* public void dump(String); */
282 { "finish", "()V" } /* public void finish(); */
285 /* public static coalescing.Extern createExtern(java.lang.String, int, int, int); */
286 static const struct _mth_info_t mthi_factory = {
287 "createExtern", "(Ljava/lang/String;III)Lcoalescing/Extern;"
290 struct _be_java_coal_t {
295 jmethodID mth_ids[mth_last];
298 static void jc_call_void(be_java_coal_t *c, int mth_index, ...)
300 JNIEnv *jni = c->env->jni;
301 jmethodID mid = c->mth_ids[mth_index];
305 va_start(args, mth_index);
306 (*jni)->CallVoidMethodV(jni, c->obj, mid, args);
311 static int jc_call_int(be_java_coal_t *c, int mth_index, ...)
313 JNIEnv *jni = c->env->jni;
314 jmethodID mid = c->mth_ids[mth_index];
319 va_start(args, mth_index);
320 res = (*jni)->CallIntMethodV(jni, c->obj, mid, args);
327 be_java_coal_t *be_java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
330 jni_env_t *env = get_jvm();
331 JNIEnv *jni = env->jni;
337 c = xmalloc(sizeof(c[0]));
338 memset(c, 0, sizeof(c[0]));
341 /* Find the class we are are looking for. */
342 cls = (*jni)->FindClass(jni, cls_name);
345 /* Get the static factory method. */
346 fact = (*jni)->GetStaticMethodID(jni, cls, mthi_factory.name, mthi_factory.sig);
349 /* Call the factory. */
350 str = (*jni)->NewStringUTF(jni, graph_name);
352 c->obj = (*jni)->CallStaticObjectMethod(jni, cls, fact, str, n_nodes, n_regs, dbg_level);
354 c->cls = (*jni)->GetObjectClass(jni, c->obj);
356 /* Reference the created object. */
357 c->obj = (*jni)->NewGlobalRef(jni, c->obj);
360 /* Lookup the member methods of the object. */
361 for(i = 0; i < mth_last; ++i) {
362 c->mth_ids[i] = (*jni)->GetMethodID(jni, c->cls, mthis[i].name, mthis[i].sig);
369 void be_java_coal_destroy(be_java_coal_t *c) {
370 JNIEnv *jni = c->env->jni;
371 jc_call_void(c, mth_finish);
372 (*jni)->DeleteGlobalRef(jni, c->obj);
376 void be_java_coal_add_int_edge(be_java_coal_t *c, int n, int m)
378 jc_call_void(c, mth_add_int_edge, (jint) n, (jint) m);
381 void be_java_coal_add_aff_edge(be_java_coal_t *c, int n, int m, int weight)
383 jc_call_void(c, mth_add_aff_edge, (jint) n, (jint) m, (jint) weight);
386 void be_java_coal_set_color(be_java_coal_t *c, int n, int col)
388 jc_call_void(c, mth_set_color, (jint) n, (jint) col);
391 void be_java_coal_set_debug(be_java_coal_t *c, int n, const char *dbg)
397 JNIEnv *jni = c->env->jni;
398 jmethodID mid = c->mth_ids[mth_set_debug];
401 str = (*jni)->NewStringUTF(jni, dbg);
403 (*jni)->CallVoidMethod(jni, c->obj, mid, (jint) n, str);
408 void be_java_coal_forbid_color(be_java_coal_t *c, int n, int col)
410 jc_call_void(c, mth_forbid_color, (jint) n, (jint) col);
413 void be_java_coal_coalesce(be_java_coal_t *c)
415 jc_call_void(c, mth_coalesce);
418 void be_java_coal_dump(be_java_coal_t *c, const char *fn)
420 JNIEnv *jni = c->env->jni;
421 jmethodID mid = c->mth_ids[mth_dump];
424 str = (*jni)->NewStringUTF(jni, fn);
426 (*jni)->CallVoidMethod(jni, c->obj, mid, str);
430 int be_java_coal_get_color(be_java_coal_t *c, int n)
432 return jc_call_int(c, mth_get_color, (jint) n);
435 void be_java_coal_start_jvm(void)
440 #else /* ! WITH_JVM */
442 void be_java_coal_start_jvm(void)
446 #endif /* WITH_JVM */