6 #include <libcore/lc_opts.h>
7 #include <libcore/lc_opts_enum.h>
20 #include "bejavacoal.h"
26 /* Path to the jar file. A little OS dependent convenience. */
28 static char jar_file[512] = "y:\\user\\hack\\public\\coal.jar";
30 static char jar_file[512] = "/ben/hack/public/coal.jar";
33 static char cls_name[256] = "coalescing/mst/safe/Algo";
35 /* Name of the JVM dll/so */
36 static char jvm_lib[512] = { 0 };
38 static const lc_opt_table_entry_t options[] = {
39 LC_OPT_ENT_STR ("jvm", "absolute path to jvm dll", jvm_lib, sizeof(jvm_lib)),
40 LC_OPT_ENT_STR ("jar", "jar file of the coalescer", jar_file, sizeof(jar_file)),
41 LC_OPT_ENT_STR ("cls", "name of the class providing the factory", cls_name, sizeof(cls_name)),
45 void be_init_javacoal(void)
47 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
48 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
49 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
50 lc_opt_entry_t *jc_grp = lc_opt_get_grp(chordal_grp, "jc");
51 lc_opt_add_table(jc_grp, options);
54 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_javacoal);
58 typedef struct _jni_env_t {
65 Ugly code to retrieve the JVM dll/so file.
71 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
73 HINSTANCE hVM = LoadLibrary(vmlibpath);
74 return hVM ? GetProcAddress(hVM, sym) : NULL;
77 #define JRE_KEY "SOFTWARE\\JavaSoft\\Java Development Kit"
79 static char *locate_jvm_lib(char *path, size_t path_len)
83 DWORD version_len = sizeof(version);
84 DWORD dwPathLen = path_len;
87 RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_QUERY_VALUE, &hKey);
88 RegQueryValueEx(hKey, "CurrentVersion", NULL, NULL, (LPBYTE) version, &version_len);
91 _snprintf(buf, sizeof(buf), JRE_KEY "\\%s", version);
92 RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &hKey);
93 RegQueryValueEx(hKey, "JavaHome", NULL, NULL, (LPBYTE) path, &dwPathLen);
96 strncat(path, "\\jre\\bin\\server\\jvm.dll", path_len);
102 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
104 void *libVM = dlopen(vmlibpath, RTLD_LAZY);
105 return libVM ? dlsym(libVM, sym) : NULL;
108 static char *locate_jvm_lib(char *path, size_t n)
114 static int start_vm(jni_env_t *env, int argc, char *argv[])
121 long (JNICALL * create_func)(JavaVM **, void **, void *) = find_jvm_symbol(jvm_lib, "JNI_CreateJavaVM");
124 fprintf(stderr, "could not find JVM creation function\n");
128 memset(&args, 0, sizeof(args));
129 opts = malloc(argc * sizeof(opts[0]));
130 for(i = 0; i < argc; ++i) {
131 opts[i].optionString = argv[i];
132 opts[i].extraInfo = NULL;
135 args.version = JNI_VERSION_1_4;
136 args.nOptions = argc;
138 args.ignoreUnrecognized = JNI_FALSE;
140 ret = create_func(&env->jvm, (void **) &env->jni, &args);
143 fprintf(stderr, "JNI_CreateJavaVM returned errrocode %ld\n" , ret);
150 static void stop_vm(jni_env_t *env)
152 JavaVM *jvm = env->jvm;
153 (*jvm)->DetachCurrentThread(jvm);
154 (*jvm)->DestroyJavaVM(jvm);
157 static int jvm_inited = 0;
158 static jni_env_t env;
159 void (*old_int_handler)(int);
160 void (*old_abrt_handler)(int);
162 static void sig_jvm_destroy_at_exit(int signal)
169 old_abrt_handler(signal);
172 old_int_handler(signal);
178 static void jvm_destroy_at_exit(void)
180 sig_jvm_destroy_at_exit(0);
183 static jni_env_t *get_jvm(void)
190 if(strlen(jvm_lib) == 0) {
191 if(!locate_jvm_lib(jvm_lib, sizeof(jvm_lib))) {
192 fprintf(stderr, "could not find jvm library\n");
197 snprintf(cp_param, sizeof(cp_param), "-Djava.class.path=%s", jar_file);
199 if(!start_vm(&env, sizeof(args) / sizeof(args[0]), args)) {
200 fprintf(stderr, "Couldn't initialize java VM\n");
204 old_int_handler = signal(SIGINT, sig_jvm_destroy_at_exit);
205 old_abrt_handler = signal(SIGABRT, sig_jvm_destroy_at_exit);
206 atexit(jvm_destroy_at_exit);
213 static void check(jni_env_t *env, const char *file, int line)
215 JNIEnv *jni = env->jni;
216 jboolean exc = (*jni)->ExceptionCheck(jni);
218 fprintf(stderr, "%s:%d: ", file, line);
219 (*jni)->ExceptionDescribe(jni);
220 (*jni)->ExceptionClear(jni);
226 #define CHECK(env) check(env, __FILE__, __LINE__)
246 static const struct _mth_info_t mthis[mth_last] = {
247 { "addIntEdge", "(II)V" }, /* public void addIntEdge(int, int); */
248 { "addAffEdge", "(III)V" }, /* public void addAffEdge(int, int, int); */
249 { "setColor", "(II)V" }, /* public void setColor(int, int); */
250 //{ "setDebug", "(ILjava/lang/String;)V" }, /* public void setDebug(int, String); */
251 { "getColor", "(I)I" }, /* public int getColor(int); */
252 { "forbidColor", "(II)V" }, /* public void forbidColor(int, int); */
253 { "coalesce", "()V" }, /* public void coalesce(); */
254 { "dump", "(Ljava/lang/String;)V" }, /* public void dump(String); */
255 { "finish", "()V" } /* public void finish(); */
258 /* public static coalescing.Extern createExtern(java.lang.String, int, int, int); */
259 static const struct _mth_info_t mthi_factory = {
260 "createExtern", "(Ljava/lang/String;III)Lcoalescing/Extern;"
263 struct _be_java_coal_t {
268 jmethodID mth_ids[mth_last];
271 static void jc_call_void(be_java_coal_t *c, int mth_index, ...)
273 JNIEnv *jni = c->env->jni;
274 jmethodID mid = c->mth_ids[mth_index];
278 va_start(args, mth_index);
279 (*jni)->CallVoidMethodV(jni, c->obj, mid, args);
284 static int jc_call_int(be_java_coal_t *c, int mth_index, ...)
286 JNIEnv *jni = c->env->jni;
287 jmethodID mid = c->mth_ids[mth_index];
292 va_start(args, mth_index);
293 res = (*jni)->CallIntMethodV(jni, c->obj, mid, args);
300 be_java_coal_t *be_java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
303 jni_env_t *env = get_jvm();
304 JNIEnv *jni = env->jni;
310 c = malloc(sizeof(c[0]));
311 memset(c, 0, sizeof(c[0]));
314 /* Find the class we are are looking for. */
315 cls = (*jni)->FindClass(jni, cls_name);
318 /* Get the static factory method. */
319 fact = (*jni)->GetStaticMethodID(jni, cls, mthi_factory.name, mthi_factory.sig);
322 /* Call the factory. */
323 str = (*jni)->NewStringUTF(jni, graph_name);
325 c->obj = (*jni)->CallStaticObjectMethod(jni, cls, fact, str, n_nodes, n_regs, dbg_level);
327 c->cls = (*jni)->GetObjectClass(jni, c->obj);
329 /* Reference the created object. */
330 c->obj = (*jni)->NewGlobalRef(jni, c->obj);
333 /* Lookup the member methods of the object. */
334 for(i = 0; i < mth_last; ++i) {
335 c->mth_ids[i] = (*jni)->GetMethodID(jni, c->cls, mthis[i].name, mthis[i].sig);
342 void be_java_coal_destroy(be_java_coal_t *c) {
343 JNIEnv *jni = c->env->jni;
344 jc_call_void(c, mth_finish);
345 (*jni)->DeleteGlobalRef(jni, c->obj);
349 void be_java_coal_add_int_edge(be_java_coal_t *c, int n, int m)
351 jc_call_void(c, mth_add_int_edge, (jint) n, (jint) m);
354 void be_java_coal_add_aff_edge(be_java_coal_t *c, int n, int m, int weight)
356 jc_call_void(c, mth_add_aff_edge, (jint) n, (jint) m, (jint) weight);
359 void be_java_coal_set_color(be_java_coal_t *c, int n, int col)
361 jc_call_void(c, mth_set_color, (jint) n, (jint) col);
364 void be_java_coal_set_debug(be_java_coal_t *c, int n, const char *dbg)
367 JNIEnv *jni = c->env->jni;
368 jmethodID mid = c->mth_ids[mth_set_debug];
371 str = (*jni)->NewStringUTF(jni, dbg);
373 (*jni)->CallVoidMethod(jni, c->obj, mid, (jint) n, str);
378 void be_java_coal_forbid_color(be_java_coal_t *c, int n, int col)
380 jc_call_void(c, mth_forbid_color, (jint) n, (jint) col);
383 void be_java_coal_coalesce(be_java_coal_t *c)
385 jc_call_void(c, mth_coalesce);
388 void be_java_coal_dump(be_java_coal_t *c, const char *fn)
390 JNIEnv *jni = c->env->jni;
391 jmethodID mid = c->mth_ids[mth_dump];
394 str = (*jni)->NewStringUTF(jni, fn);
396 (*jni)->CallVoidMethod(jni, c->obj, mid, str);
400 int be_java_coal_get_color(be_java_coal_t *c, int n)
402 return jc_call_int(c, mth_get_color, (jint) n);
405 void be_java_coal_start_jvm(void)
412 be_java_coal_t *be_java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
414 assert(0 && "use --enable-jvm");
418 void be_java_coal_destroy(be_java_coal_t *c)
420 assert(0 && "use --enable-jvm");
424 void be_java_coal_add_int_edge(be_java_coal_t *c, int n, int m)
426 assert(0 && "use --enable-jvm");
429 void be_java_coal_add_aff_edge(be_java_coal_t *c, int n, int m, int weight)
431 assert(0 && "use --enable-jvm");
434 void be_java_coal_set_color(be_java_coal_t *c, int n, int col)
436 assert(0 && "use --enable-jvm");
439 void be_java_coal_set_debug(be_java_coal_t *c, int n, const char *dbg)
441 assert(0 && "use --enable-jvm");
444 void be_java_coal_forbid_color(be_java_coal_t *c, int n, int col)
446 assert(0 && "use --enable-jvm");
449 void be_java_coal_coalesce(be_java_coal_t *c)
451 assert(0 && "use --enable-jvm");
454 void be_java_coal_dump(be_java_coal_t *c, const char *fn)
456 assert(0 && "use --enable-jvm");
459 int be_java_coal_get_color(be_java_coal_t *c, int n)
461 assert(0 && "use --enable-jvm");
465 void be_java_coal_start_jvm(void)
469 #endif /* WITH_JVM */