2 * Copyright (C) 1995-2007 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
25 #include <libcore/lc_opts.h>
26 #include <libcore/lc_opts_enum.h>
39 #include "bejavacoal.h"
45 /* Path to the jar file. A little OS dependent convenience. */
47 static char jar_file[512] = "y:\\user\\hack\\public\\coal.jar";
49 static char jar_file[512] = "/ben/hack/public/coal.jar";
52 static char cls_name[256] = "coalescing/mst/safe/Algo";
54 /* Name of the JVM dll/so */
55 static char jvm_lib[512] = { 0 };
57 static const lc_opt_table_entry_t options[] = {
58 LC_OPT_ENT_STR ("jvm", "absolute path to jvm dll", jvm_lib, sizeof(jvm_lib)),
59 LC_OPT_ENT_STR ("jar", "jar file of the coalescer", jar_file, sizeof(jar_file)),
60 LC_OPT_ENT_STR ("cls", "name of the class providing the factory", cls_name, sizeof(cls_name)),
64 void be_init_javacoal(void)
66 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
67 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
68 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
69 lc_opt_entry_t *jc_grp = lc_opt_get_grp(chordal_grp, "jc");
70 lc_opt_add_table(jc_grp, options);
73 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_javacoal);
77 typedef struct _jni_env_t {
84 Ugly code to retrieve the JVM dll/so file.
90 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
92 HINSTANCE hVM = LoadLibrary(vmlibpath);
93 return hVM ? GetProcAddress(hVM, sym) : NULL;
96 #define JRE_KEY "SOFTWARE\\JavaSoft\\Java Development Kit"
98 static char *locate_jvm_lib(char *path, size_t path_len)
102 DWORD version_len = sizeof(version);
103 DWORD dwPathLen = path_len;
106 RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_QUERY_VALUE, &hKey);
107 RegQueryValueEx(hKey, "CurrentVersion", NULL, NULL, (LPBYTE) version, &version_len);
110 _snprintf(buf, sizeof(buf), JRE_KEY "\\%s", version);
111 RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &hKey);
112 RegQueryValueEx(hKey, "JavaHome", NULL, NULL, (LPBYTE) path, &dwPathLen);
115 strncat(path, "\\jre\\bin\\server\\jvm.dll", path_len);
121 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
123 void *libVM = dlopen(vmlibpath, RTLD_LAZY);
124 return libVM ? dlsym(libVM, sym) : NULL;
127 static char *locate_jvm_lib(char *path, size_t n)
133 static int start_vm(jni_env_t *env, int argc, char *argv[])
140 long (JNICALL * create_func)(JavaVM **, void **, void *) = find_jvm_symbol(jvm_lib, "JNI_CreateJavaVM");
143 fprintf(stderr, "could not find JVM creation function\n");
147 memset(&args, 0, sizeof(args));
148 opts = malloc(argc * sizeof(opts[0]));
149 for(i = 0; i < argc; ++i) {
150 opts[i].optionString = argv[i];
151 opts[i].extraInfo = NULL;
154 args.version = JNI_VERSION_1_4;
155 args.nOptions = argc;
157 args.ignoreUnrecognized = JNI_FALSE;
159 ret = create_func(&env->jvm, (void **) &env->jni, &args);
162 fprintf(stderr, "JNI_CreateJavaVM returned errrocode %ld\n" , ret);
169 static void stop_vm(jni_env_t *env)
171 JavaVM *jvm = env->jvm;
172 (*jvm)->DetachCurrentThread(jvm);
173 (*jvm)->DestroyJavaVM(jvm);
176 static int jvm_inited = 0;
177 static jni_env_t env;
178 void (*old_int_handler)(int);
179 void (*old_abrt_handler)(int);
181 static void sig_jvm_destroy_at_exit(int signal)
188 old_abrt_handler(signal);
191 old_int_handler(signal);
197 static void jvm_destroy_at_exit(void)
199 sig_jvm_destroy_at_exit(0);
202 static jni_env_t *get_jvm(void)
209 if(strlen(jvm_lib) == 0) {
210 if(!locate_jvm_lib(jvm_lib, sizeof(jvm_lib))) {
211 fprintf(stderr, "could not find jvm library\n");
216 snprintf(cp_param, sizeof(cp_param), "-Djava.class.path=%s", jar_file);
218 if(!start_vm(&env, sizeof(args) / sizeof(args[0]), args)) {
219 fprintf(stderr, "Couldn't initialize java VM\n");
223 old_int_handler = signal(SIGINT, sig_jvm_destroy_at_exit);
224 old_abrt_handler = signal(SIGABRT, sig_jvm_destroy_at_exit);
225 atexit(jvm_destroy_at_exit);
232 static void check(jni_env_t *env, const char *file, int line)
234 JNIEnv *jni = env->jni;
235 jboolean exc = (*jni)->ExceptionCheck(jni);
237 fprintf(stderr, "%s:%d: ", file, line);
238 (*jni)->ExceptionDescribe(jni);
239 (*jni)->ExceptionClear(jni);
245 #define CHECK(env) check(env, __FILE__, __LINE__)
265 static const struct _mth_info_t mthis[mth_last] = {
266 { "addIntEdge", "(II)V" }, /* public void addIntEdge(int, int); */
267 { "addAffEdge", "(III)V" }, /* public void addAffEdge(int, int, int); */
268 { "setColor", "(II)V" }, /* public void setColor(int, int); */
269 //{ "setDebug", "(ILjava/lang/String;)V" }, /* public void setDebug(int, String); */
270 { "getColor", "(I)I" }, /* public int getColor(int); */
271 { "forbidColor", "(II)V" }, /* public void forbidColor(int, int); */
272 { "coalesce", "()V" }, /* public void coalesce(); */
273 { "dump", "(Ljava/lang/String;)V" }, /* public void dump(String); */
274 { "finish", "()V" } /* public void finish(); */
277 /* public static coalescing.Extern createExtern(java.lang.String, int, int, int); */
278 static const struct _mth_info_t mthi_factory = {
279 "createExtern", "(Ljava/lang/String;III)Lcoalescing/Extern;"
282 struct _be_java_coal_t {
287 jmethodID mth_ids[mth_last];
290 static void jc_call_void(be_java_coal_t *c, int mth_index, ...)
292 JNIEnv *jni = c->env->jni;
293 jmethodID mid = c->mth_ids[mth_index];
297 va_start(args, mth_index);
298 (*jni)->CallVoidMethodV(jni, c->obj, mid, args);
303 static int jc_call_int(be_java_coal_t *c, int mth_index, ...)
305 JNIEnv *jni = c->env->jni;
306 jmethodID mid = c->mth_ids[mth_index];
311 va_start(args, mth_index);
312 res = (*jni)->CallIntMethodV(jni, c->obj, mid, args);
319 be_java_coal_t *be_java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
322 jni_env_t *env = get_jvm();
323 JNIEnv *jni = env->jni;
329 c = malloc(sizeof(c[0]));
330 memset(c, 0, sizeof(c[0]));
333 /* Find the class we are are looking for. */
334 cls = (*jni)->FindClass(jni, cls_name);
337 /* Get the static factory method. */
338 fact = (*jni)->GetStaticMethodID(jni, cls, mthi_factory.name, mthi_factory.sig);
341 /* Call the factory. */
342 str = (*jni)->NewStringUTF(jni, graph_name);
344 c->obj = (*jni)->CallStaticObjectMethod(jni, cls, fact, str, n_nodes, n_regs, dbg_level);
346 c->cls = (*jni)->GetObjectClass(jni, c->obj);
348 /* Reference the created object. */
349 c->obj = (*jni)->NewGlobalRef(jni, c->obj);
352 /* Lookup the member methods of the object. */
353 for(i = 0; i < mth_last; ++i) {
354 c->mth_ids[i] = (*jni)->GetMethodID(jni, c->cls, mthis[i].name, mthis[i].sig);
361 void be_java_coal_destroy(be_java_coal_t *c) {
362 JNIEnv *jni = c->env->jni;
363 jc_call_void(c, mth_finish);
364 (*jni)->DeleteGlobalRef(jni, c->obj);
368 void be_java_coal_add_int_edge(be_java_coal_t *c, int n, int m)
370 jc_call_void(c, mth_add_int_edge, (jint) n, (jint) m);
373 void be_java_coal_add_aff_edge(be_java_coal_t *c, int n, int m, int weight)
375 jc_call_void(c, mth_add_aff_edge, (jint) n, (jint) m, (jint) weight);
378 void be_java_coal_set_color(be_java_coal_t *c, int n, int col)
380 jc_call_void(c, mth_set_color, (jint) n, (jint) col);
383 void be_java_coal_set_debug(be_java_coal_t *c, int n, const char *dbg)
386 JNIEnv *jni = c->env->jni;
387 jmethodID mid = c->mth_ids[mth_set_debug];
390 str = (*jni)->NewStringUTF(jni, dbg);
392 (*jni)->CallVoidMethod(jni, c->obj, mid, (jint) n, str);
397 void be_java_coal_forbid_color(be_java_coal_t *c, int n, int col)
399 jc_call_void(c, mth_forbid_color, (jint) n, (jint) col);
402 void be_java_coal_coalesce(be_java_coal_t *c)
404 jc_call_void(c, mth_coalesce);
407 void be_java_coal_dump(be_java_coal_t *c, const char *fn)
409 JNIEnv *jni = c->env->jni;
410 jmethodID mid = c->mth_ids[mth_dump];
413 str = (*jni)->NewStringUTF(jni, fn);
415 (*jni)->CallVoidMethod(jni, c->obj, mid, str);
419 int be_java_coal_get_color(be_java_coal_t *c, int n)
421 return jc_call_int(c, mth_get_color, (jint) n);
424 void be_java_coal_start_jvm(void)
431 be_java_coal_t *be_java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
433 assert(0 && "use --enable-jvm");
437 void be_java_coal_destroy(be_java_coal_t *c)
439 assert(0 && "use --enable-jvm");
443 void be_java_coal_add_int_edge(be_java_coal_t *c, int n, int m)
445 assert(0 && "use --enable-jvm");
448 void be_java_coal_add_aff_edge(be_java_coal_t *c, int n, int m, int weight)
450 assert(0 && "use --enable-jvm");
453 void be_java_coal_set_color(be_java_coal_t *c, int n, int col)
455 assert(0 && "use --enable-jvm");
458 void be_java_coal_set_debug(be_java_coal_t *c, int n, const char *dbg)
460 assert(0 && "use --enable-jvm");
463 void be_java_coal_forbid_color(be_java_coal_t *c, int n, int col)
465 assert(0 && "use --enable-jvm");
468 void be_java_coal_coalesce(be_java_coal_t *c)
470 assert(0 && "use --enable-jvm");
473 void be_java_coal_dump(be_java_coal_t *c, const char *fn)
475 assert(0 && "use --enable-jvm");
478 int be_java_coal_get_color(be_java_coal_t *c, int n)
480 assert(0 && "use --enable-jvm");
484 void be_java_coal_start_jvm(void)
488 #endif /* WITH_JVM */