7 #include <libcore/lc_opts.h>
8 #include <libcore/lc_opts_enum.h>
9 #endif /* WITH_LIBCORE */
22 #include "bejavacoal.h"
28 /* Path to the jar file. A little OS dependent convenience. */
30 static char jar_file[512] = "y:\\user\\hack\\public\\coal.jar";
32 static char jar_file[512] = "/ben/hack/public/coal.jar";
35 static char cls_name[256] = "coalescing/mst/safe/Algo";
37 /* Name of the JVM dll/so */
38 static char jvm_lib[512] = { 0 };
41 static const lc_opt_table_entry_t options[] = {
42 LC_OPT_ENT_STR ("jvm", "absolute path to jvm dll", jvm_lib, sizeof(jvm_lib)),
43 LC_OPT_ENT_STR ("jar", "jar file of the coalescer", jar_file, sizeof(jar_file)),
44 LC_OPT_ENT_STR ("cls", "name of the class providing the factory", cls_name, sizeof(cls_name)),
48 void be_init_javacoal(void)
50 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
51 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
52 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
53 lc_opt_entry_t *jc_grp = lc_opt_get_grp(chordal_grp, "jc");
54 lc_opt_add_table(jc_grp, options);
57 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_javacoal);
58 #endif /* WITH_LIBCORE */
62 typedef struct _jni_env_t {
69 Ugly code to retrieve the JVM dll/so file.
75 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
77 HINSTANCE hVM = LoadLibrary(vmlibpath);
78 return hVM ? GetProcAddress(hVM, sym) : NULL;
81 #define JRE_KEY "SOFTWARE\\JavaSoft\\Java Development Kit"
83 static char *locate_jvm_lib(char *path, size_t path_len)
87 DWORD version_len = sizeof(version);
88 DWORD dwPathLen = path_len;
91 RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_QUERY_VALUE, &hKey);
92 RegQueryValueEx(hKey, "CurrentVersion", NULL, NULL, (LPBYTE) version, &version_len);
95 _snprintf(buf, sizeof(buf), JRE_KEY "\\%s", version);
96 RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &hKey);
97 RegQueryValueEx(hKey, "JavaHome", NULL, NULL, (LPBYTE) path, &dwPathLen);
100 strncat(path, "\\jre\\bin\\server\\jvm.dll", path_len);
106 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
108 void *libVM = dlopen(vmlibpath, RTLD_LAZY);
109 return libVM ? dlsym(libVM, sym) : NULL;
112 static char *locate_jvm_lib(char *path, size_t n)
118 static int start_vm(jni_env_t *env, int argc, char *argv[])
125 long (JNICALL * create_func)(JavaVM **, void **, void *) = find_jvm_symbol(jvm_lib, "JNI_CreateJavaVM");
128 fprintf(stderr, "could not find JVM creation function\n");
132 memset(&args, 0, sizeof(args));
133 opts = malloc(argc * sizeof(opts[0]));
134 for(i = 0; i < argc; ++i) {
135 opts[i].optionString = argv[i];
136 opts[i].extraInfo = NULL;
139 args.version = JNI_VERSION_1_4;
140 args.nOptions = argc;
142 args.ignoreUnrecognized = JNI_FALSE;
144 ret = create_func(&env->jvm, (void **) &env->jni, &args);
147 fprintf(stderr, "JNI_CreateJavaVM returned errrocode %ld\n" , ret);
154 static void stop_vm(jni_env_t *env)
156 JavaVM *jvm = env->jvm;
157 (*jvm)->DetachCurrentThread(jvm);
158 (*jvm)->DestroyJavaVM(jvm);
161 static int jvm_inited = 0;
162 static jni_env_t env;
163 void (*old_int_handler)(int);
164 void (*old_abrt_handler)(int);
166 static void sig_jvm_destroy_at_exit(int signal)
173 old_abrt_handler(signal);
176 old_int_handler(signal);
182 static void jvm_destroy_at_exit(void)
184 sig_jvm_destroy_at_exit(0);
187 static jni_env_t *get_jvm(void)
194 if(strlen(jvm_lib) == 0) {
195 if(!locate_jvm_lib(jvm_lib, sizeof(jvm_lib))) {
196 fprintf(stderr, "could not find jvm library\n");
201 snprintf(cp_param, sizeof(cp_param), "-Djava.class.path=%s", jar_file);
203 if(!start_vm(&env, sizeof(args) / sizeof(args[0]), args)) {
204 fprintf(stderr, "Couldn't initialize java VM\n");
208 old_int_handler = signal(SIGINT, sig_jvm_destroy_at_exit);
209 old_abrt_handler = signal(SIGABRT, sig_jvm_destroy_at_exit);
210 atexit(jvm_destroy_at_exit);
217 static void check(jni_env_t *env, const char *file, int line)
219 JNIEnv *jni = env->jni;
220 jboolean exc = (*jni)->ExceptionCheck(jni);
222 fprintf(stderr, "%s:%d: ", file, line);
223 (*jni)->ExceptionDescribe(jni);
224 (*jni)->ExceptionClear(jni);
230 #define CHECK(env) check(env, __FILE__, __LINE__)
250 static const struct _mth_info_t mthis[mth_last] = {
251 { "addIntEdge", "(II)V" }, /* public void addIntEdge(int, int); */
252 { "addAffEdge", "(III)V" }, /* public void addAffEdge(int, int, int); */
253 { "setColor", "(II)V" }, /* public void setColor(int, int); */
254 //{ "setDebug", "(ILjava/lang/String;)V" }, /* public void setDebug(int, String); */
255 { "getColor", "(I)I" }, /* public int getColor(int); */
256 { "forbidColor", "(II)V" }, /* public void forbidColor(int, int); */
257 { "coalesce", "()V" }, /* public void coalesce(); */
258 { "dump", "(Ljava/lang/String;)V" }, /* public void dump(String); */
259 { "finish", "()V" } /* public void finish(); */
262 /* public static coalescing.Extern createExtern(java.lang.String, int, int, int); */
263 static const struct _mth_info_t mthi_factory = {
264 "createExtern", "(Ljava/lang/String;III)Lcoalescing/Extern;"
267 struct _be_java_coal_t {
272 jmethodID mth_ids[mth_last];
275 static void jc_call_void(be_java_coal_t *c, int mth_index, ...)
277 JNIEnv *jni = c->env->jni;
278 jmethodID mid = c->mth_ids[mth_index];
282 va_start(args, mth_index);
283 (*jni)->CallVoidMethodV(jni, c->obj, mid, args);
288 static int jc_call_int(be_java_coal_t *c, int mth_index, ...)
290 JNIEnv *jni = c->env->jni;
291 jmethodID mid = c->mth_ids[mth_index];
296 va_start(args, mth_index);
297 res = (*jni)->CallIntMethodV(jni, c->obj, mid, args);
304 be_java_coal_t *be_java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
307 jni_env_t *env = get_jvm();
308 JNIEnv *jni = env->jni;
314 c = malloc(sizeof(c[0]));
315 memset(c, 0, sizeof(c[0]));
318 /* Find the class we are are looking for. */
319 cls = (*jni)->FindClass(jni, cls_name);
322 /* Get the static factory method. */
323 fact = (*jni)->GetStaticMethodID(jni, cls, mthi_factory.name, mthi_factory.sig);
326 /* Call the factory. */
327 str = (*jni)->NewStringUTF(jni, graph_name);
329 c->obj = (*jni)->CallStaticObjectMethod(jni, cls, fact, str, n_nodes, n_regs, dbg_level);
331 c->cls = (*jni)->GetObjectClass(jni, c->obj);
333 /* Reference the created object. */
334 c->obj = (*jni)->NewGlobalRef(jni, c->obj);
337 /* Lookup the member methods of the object. */
338 for(i = 0; i < mth_last; ++i) {
339 c->mth_ids[i] = (*jni)->GetMethodID(jni, c->cls, mthis[i].name, mthis[i].sig);
346 void be_java_coal_destroy(be_java_coal_t *c) {
347 JNIEnv *jni = c->env->jni;
348 jc_call_void(c, mth_finish);
349 (*jni)->DeleteGlobalRef(jni, c->obj);
353 void be_java_coal_add_int_edge(be_java_coal_t *c, int n, int m)
355 jc_call_void(c, mth_add_int_edge, (jint) n, (jint) m);
358 void be_java_coal_add_aff_edge(be_java_coal_t *c, int n, int m, int weight)
360 jc_call_void(c, mth_add_aff_edge, (jint) n, (jint) m, (jint) weight);
363 void be_java_coal_set_color(be_java_coal_t *c, int n, int col)
365 jc_call_void(c, mth_set_color, (jint) n, (jint) col);
368 void be_java_coal_set_debug(be_java_coal_t *c, int n, const char *dbg)
371 JNIEnv *jni = c->env->jni;
372 jmethodID mid = c->mth_ids[mth_set_debug];
375 str = (*jni)->NewStringUTF(jni, dbg);
377 (*jni)->CallVoidMethod(jni, c->obj, mid, (jint) n, str);
382 void be_java_coal_forbid_color(be_java_coal_t *c, int n, int col)
384 jc_call_void(c, mth_forbid_color, (jint) n, (jint) col);
387 void be_java_coal_coalesce(be_java_coal_t *c)
389 jc_call_void(c, mth_coalesce);
392 void be_java_coal_dump(be_java_coal_t *c, const char *fn)
394 JNIEnv *jni = c->env->jni;
395 jmethodID mid = c->mth_ids[mth_dump];
398 str = (*jni)->NewStringUTF(jni, fn);
400 (*jni)->CallVoidMethod(jni, c->obj, mid, str);
404 int be_java_coal_get_color(be_java_coal_t *c, int n)
406 return jc_call_int(c, mth_get_color, (jint) n);
409 void be_java_coal_start_jvm(void)
416 be_java_coal_t *be_java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
418 assert(0 && "use --enable-jvm");
422 void be_java_coal_destroy(be_java_coal_t *c)
424 assert(0 && "use --enable-jvm");
428 void be_java_coal_add_int_edge(be_java_coal_t *c, int n, int m)
430 assert(0 && "use --enable-jvm");
433 void be_java_coal_add_aff_edge(be_java_coal_t *c, int n, int m, int weight)
435 assert(0 && "use --enable-jvm");
438 void be_java_coal_set_color(be_java_coal_t *c, int n, int col)
440 assert(0 && "use --enable-jvm");
443 void be_java_coal_set_debug(be_java_coal_t *c, int n, const char *dbg)
445 assert(0 && "use --enable-jvm");
448 void be_java_coal_forbid_color(be_java_coal_t *c, int n, int col)
450 assert(0 && "use --enable-jvm");
453 void be_java_coal_coalesce(be_java_coal_t *c)
455 assert(0 && "use --enable-jvm");
458 void be_java_coal_dump(be_java_coal_t *c, const char *fn)
460 assert(0 && "use --enable-jvm");
463 int be_java_coal_get_color(be_java_coal_t *c, int n)
465 assert(0 && "use --enable-jvm");
469 void be_java_coal_start_jvm(void)
473 #endif /* WITH_JVM */