7 #include <libcore/lc_opts.h>
8 #include <libcore/lc_opts_enum.h>
9 #endif /* WITH_LIBCORE */
22 #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 };
39 static const lc_opt_table_entry_t options[] = {
40 LC_OPT_ENT_STR ("jvm", "absolute path to jvm dll", jvm_lib, sizeof(jvm_lib)),
41 LC_OPT_ENT_STR ("jar", "jar file of the coalescer", jar_file, sizeof(jar_file)),
42 LC_OPT_ENT_STR ("cls", "name of the class providing the factory", cls_name, sizeof(cls_name)),
46 void be_init_javacoal(void)
48 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
49 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
50 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
51 lc_opt_entry_t *jc_grp = lc_opt_get_grp(grp, "jc");
52 lc_opt_add_table(jc_grp, options);
55 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_javacoal);
56 #endif /* WITH_LIBCORE */
60 typedef struct _jni_env_t {
67 Ugly code to retrieve the JVM dll/so file.
73 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
75 HINSTANCE hVM = LoadLibrary(vmlibpath);
76 return hVM ? GetProcAddress(hVM, sym) : NULL;
79 #define JRE_KEY "SOFTWARE\\JavaSoft\\Java Development Kit"
81 static char *locate_jvm_lib(char *path, size_t path_len)
85 DWORD version_len = sizeof(version);
86 DWORD dwPathLen = path_len;
89 RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_QUERY_VALUE, &hKey);
90 RegQueryValueEx(hKey, "CurrentVersion", NULL, NULL, (LPBYTE) version, &version_len);
93 _snprintf(buf, sizeof(buf), JRE_KEY "\\%s", version);
94 RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &hKey);
95 RegQueryValueEx(hKey, "JavaHome", NULL, NULL, (LPBYTE) path, &dwPathLen);
98 strncat(path, "\\jre\\bin\\server\\jvm.dll", path_len);
104 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
106 void *libVM = dlopen(vmlibpath, RTLD_LAZY);
107 return libVM ? dlsym(libVM, sym) : NULL;
110 static char *locate_jvm_lib(char *path, size_t n)
116 static int start_vm(jni_env_t *env, int argc, char *argv[])
123 long (JNICALL * create_func)(JavaVM **, void **, void *) = find_jvm_symbol(jvm_lib, "JNI_CreateJavaVM");
126 fprintf(stderr, "could not find JVM creation function\n");
130 memset(&args, 0, sizeof(args));
131 opts = malloc(argc * sizeof(opts[0]));
132 for(i = 0; i < argc; ++i) {
133 opts[i].optionString = argv[i];
134 opts[i].extraInfo = NULL;
137 args.version = JNI_VERSION_1_4;
138 args.nOptions = argc;
140 args.ignoreUnrecognized = JNI_FALSE;
142 ret = create_func(&env->jvm, (void **) &env->jni, &args);
145 fprintf(stderr, "JNI_CreateJavaVM returned errrocode %ld\n" , ret);
152 static void stop_vm(jni_env_t *env)
154 JavaVM *jvm = env->jvm;
155 (*jvm)->DetachCurrentThread(jvm);
156 (*jvm)->DestroyJavaVM(jvm);
159 static int jvm_inited = 0;
160 static jni_env_t env;
161 void (*old_int_handler)(int);
162 void (*old_abrt_handler)(int);
164 static void sig_jvm_destroy_at_exit(int signal)
171 old_abrt_handler(signal);
174 old_int_handler(signal);
180 static void jvm_destroy_at_exit(void)
182 sig_jvm_destroy_at_exit(0);
185 static jni_env_t *get_jvm(void)
192 if(strlen(jvm_lib) == 0) {
193 if(!locate_jvm_lib(jvm_lib, sizeof(jvm_lib))) {
194 fprintf(stderr, "could not find jvm library\n");
199 snprintf(cp_param, sizeof(cp_param), "-Djava.class.path=%s", jar_file);
201 if(!start_vm(&env, sizeof(args) / sizeof(args[0]), args)) {
202 fprintf(stderr, "Couldn't initialize java VM\n");
206 old_int_handler = signal(SIGINT, sig_jvm_destroy_at_exit);
207 old_abrt_handler = signal(SIGABRT, sig_jvm_destroy_at_exit);
208 atexit(jvm_destroy_at_exit);
215 static void check(jni_env_t *env, const char *file, int line)
217 JNIEnv *jni = env->jni;
218 jboolean exc = (*jni)->ExceptionCheck(jni);
220 fprintf(stderr, "%s:%d: ", file, line);
221 (*jni)->ExceptionDescribe(jni);
222 (*jni)->ExceptionClear(jni);
228 #define CHECK(env) check(env, __FILE__, __LINE__)
248 static const struct _mth_info_t mthis[mth_last] = {
249 { "addIntEdge", "(II)V" }, /* public void addIntEdge(int, int); */
250 { "addAffEdge", "(III)V" }, /* public void addAffEdge(int, int, int); */
251 { "setColor", "(II)V" }, /* public void setColor(int, int); */
252 //{ "setDebug", "(ILjava/lang/String;)V" }, /* public void setDebug(int, String); */
253 { "getColor", "(I)I" }, /* public int getColor(int); */
254 { "forbidColor", "(II)V" }, /* public void forbidColor(int, int); */
255 { "coalesce", "()V" }, /* public void coalesce(); */
256 { "dump", "(Ljava/lang/String;)V" }, /* public void dump(String); */
257 { "finish", "()V" } /* public void finish(); */
260 /* public static coalescing.Extern createExtern(java.lang.String, int, int, int); */
261 static const struct _mth_info_t mthi_factory = {
262 "createExtern", "(Ljava/lang/String;III)Lcoalescing/Extern;"
265 struct _be_java_coal_t {
270 jmethodID mth_ids[mth_last];
273 static void jc_call_void(be_java_coal_t *c, int mth_index, ...)
275 JNIEnv *jni = c->env->jni;
276 jmethodID mid = c->mth_ids[mth_index];
280 va_start(args, mth_index);
281 (*jni)->CallVoidMethodV(jni, c->obj, mid, args);
286 static int jc_call_int(be_java_coal_t *c, int mth_index, ...)
288 JNIEnv *jni = c->env->jni;
289 jmethodID mid = c->mth_ids[mth_index];
294 va_start(args, mth_index);
295 res = (*jni)->CallIntMethodV(jni, c->obj, mid, args);
302 be_java_coal_t *be_java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
305 jni_env_t *env = get_jvm();
306 JNIEnv *jni = env->jni;
312 c = malloc(sizeof(c[0]));
313 memset(c, 0, sizeof(c[0]));
316 /* Find the class we are are looking for. */
317 cls = (*jni)->FindClass(jni, cls_name);
320 /* Get the static factory method. */
321 fact = (*jni)->GetStaticMethodID(jni, cls, mthi_factory.name, mthi_factory.sig);
324 /* Call the factory. */
325 str = (*jni)->NewStringUTF(jni, graph_name);
327 c->obj = (*jni)->CallStaticObjectMethod(jni, cls, fact, str, n_nodes, n_regs, dbg_level);
329 c->cls = (*jni)->GetObjectClass(jni, c->obj);
331 /* Reference the created object. */
332 c->obj = (*jni)->NewGlobalRef(jni, c->obj);
335 /* Lookup the member methods of the object. */
336 for(i = 0; i < mth_last; ++i) {
337 c->mth_ids[i] = (*jni)->GetMethodID(jni, c->cls, mthis[i].name, mthis[i].sig);
344 void be_java_coal_destroy(be_java_coal_t *c) {
345 JNIEnv *jni = c->env->jni;
346 jc_call_void(c, mth_finish);
347 (*jni)->DeleteGlobalRef(jni, c->obj);
351 void be_java_coal_add_int_edge(be_java_coal_t *c, int n, int m)
353 jc_call_void(c, mth_add_int_edge, (jint) n, (jint) m);
356 void be_java_coal_add_aff_edge(be_java_coal_t *c, int n, int m, int weight)
358 jc_call_void(c, mth_add_aff_edge, (jint) n, (jint) m, (jint) weight);
361 void be_java_coal_set_color(be_java_coal_t *c, int n, int col)
363 jc_call_void(c, mth_set_color, (jint) n, (jint) col);
366 void be_java_coal_set_debug(be_java_coal_t *c, int n, const char *dbg)
369 JNIEnv *jni = c->env->jni;
370 jmethodID mid = c->mth_ids[mth_set_debug];
373 str = (*jni)->NewStringUTF(jni, dbg);
375 (*jni)->CallVoidMethod(jni, c->obj, mid, (jint) n, str);
380 void be_java_coal_forbid_color(be_java_coal_t *c, int n, int col)
382 jc_call_void(c, mth_forbid_color, (jint) n, (jint) col);
385 void be_java_coal_coalesce(be_java_coal_t *c)
387 jc_call_void(c, mth_coalesce);
390 void be_java_coal_dump(be_java_coal_t *c, const char *fn)
392 JNIEnv *jni = c->env->jni;
393 jmethodID mid = c->mth_ids[mth_dump];
396 str = (*jni)->NewStringUTF(jni, fn);
398 (*jni)->CallVoidMethod(jni, c->obj, mid, str);
402 int be_java_coal_get_color(be_java_coal_t *c, int n)
404 return jc_call_int(c, mth_get_color, (jint) n);
407 void be_java_coal_start_jvm(void)
414 be_java_coal_t *be_java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
416 assert(0 && "use --enable-jvm");
420 void be_java_coal_destroy(be_java_coal_t *c)
422 assert(0 && "use --enable-jvm");
426 void be_java_coal_add_int_edge(be_java_coal_t *c, int n, int m)
428 assert(0 && "use --enable-jvm");
431 void be_java_coal_add_aff_edge(be_java_coal_t *c, int n, int m, int weight)
433 assert(0 && "use --enable-jvm");
436 void be_java_coal_set_color(be_java_coal_t *c, int n, int col)
438 assert(0 && "use --enable-jvm");
441 void be_java_coal_set_debug(be_java_coal_t *c, int n, const char *dbg)
443 assert(0 && "use --enable-jvm");
446 void be_java_coal_forbid_color(be_java_coal_t *c, int n, int col)
448 assert(0 && "use --enable-jvm");
451 void be_java_coal_coalesce(be_java_coal_t *c)
453 assert(0 && "use --enable-jvm");
456 void be_java_coal_dump(be_java_coal_t *c, const char *fn)
458 assert(0 && "use --enable-jvm");
461 int be_java_coal_get_color(be_java_coal_t *c, int n)
463 assert(0 && "use --enable-jvm");
467 void be_java_coal_start_jvm(void)
471 #endif /* WITH_JVM */