7 #include <libcore/lc_opts.h>
8 #include <libcore/lc_opts_enum.h>
9 #endif /* WITH_LIBCORE */
24 #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 teh 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 java_coal_register_options(lc_opt_entry_t *grp)
48 lc_opt_entry_t *jc_grp = lc_opt_get_grp(grp, "jc");
49 lc_opt_add_table(jc_grp, options);
56 typedef struct _jni_env_t {
64 Ugly code to retrieve the JVM dll/so file.
70 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
72 HINSTANCE hVM = LoadLibrary(vmlibpath);
73 return hVM ? GetProcAddress(hVM, sym) : NULL;
76 #define JRE_KEY "SOFTWARE\\JavaSoft\\Java Development Kit"
78 static char *locate_jvm_lib(char *path, size_t path_len)
82 DWORD version_len = sizeof(version);
83 DWORD dwPathLen = path_len;
86 RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_QUERY_VALUE, &hKey);
87 RegQueryValueEx(hKey, "CurrentVersion", NULL, NULL, (LPBYTE) version, &version_len);
90 _snprintf(buf, sizeof(buf), JRE_KEY "\\%s", version);
91 RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &hKey);
92 RegQueryValueEx(hKey, "JavaHome", NULL, NULL, (LPBYTE) path, &dwPathLen);
95 strncat(path, "\\jre\\bin\\server\\jvm.dll", path_len);
101 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
103 void *libVM = dlopen(vmlibpath, RTLD_LAZY);
104 return libVM ? dlsym(libVM, sym) : NULL;
107 static char *locate_jvm_lib(char *path, size_t n)
114 static int start_vm(jni_env_t *env, int argc, char *argv[])
122 long (JNICALL * create_func)(JavaVM **, void **, void *) = find_jvm_symbol(jvm_lib, "JNI_CreateJavaVM");
125 fprintf(stderr, "could not find JVM creation function\n");
129 memset(&args, 0, sizeof(args));
130 opts = malloc(argc * sizeof(opts[0]));
131 for(i = 0; i < argc; ++i) {
132 opts[i].optionString = argv[i];
133 opts[i].extraInfo = NULL;
136 args.version = JNI_VERSION_1_4;
137 args.nOptions = argc;
139 args.ignoreUnrecognized = JNI_FALSE;
141 ret = create_func(&env->jvm, (void **) &env->jni, &args);
149 static void stop_vm(jni_env_t *env)
151 JavaVM *jvm = env->jvm;
152 (*jvm)->DetachCurrentThread(jvm);
153 (*jvm)->DestroyJavaVM(jvm);
156 static int jvm_inited = 0;
157 static jni_env_t env;
158 void (*old_int_handler)(int);
159 void (*old_abrt_handler)(int);
161 static void sig_jvm_destroy_at_exit(int signal)
168 old_abrt_handler(signal);
171 old_int_handler(signal);
177 static void jvm_destroy_at_exit(void)
179 sig_jvm_destroy_at_exit(0);
182 static jni_env_t *get_jvm(void)
189 if(strlen(jvm_lib) == 0) {
190 if(!locate_jvm_lib(jvm_lib, sizeof(jvm_lib))) {
191 fprintf(stderr, "could not find jvm library\n");
196 snprintf(cp_param, sizeof(cp_param), "-Djava.class.path=%s", jar_file);
198 start_vm(&env, sizeof(args) / sizeof(args[0]), args);
200 old_int_handler = signal(SIGINT, sig_jvm_destroy_at_exit);
201 old_abrt_handler = signal(SIGABRT, sig_jvm_destroy_at_exit);
202 atexit(jvm_destroy_at_exit);
208 static void check(jni_env_t *env, const char *file, int line)
210 JNIEnv *jni = env->jni;
211 jboolean exc = (*jni)->ExceptionCheck(jni);
213 fprintf(stderr, "%s:%d: ", file, line);
214 (*jni)->ExceptionDescribe(jni);
215 (*jni)->ExceptionClear(jni);
221 #define CHECK(env) check(env, __FILE__, __LINE__)
240 static const struct _mth_info_t mthis[mth_last] = {
241 { "addIntEdge", "(II)V" }, /* public void addIntEdge(int, int); */
242 { "addAffEdge", "(III)V" }, /* public void addAffEdge(int, int, int); */
243 { "setColor", "(II)V" }, /* public void setColor(int, int); */
244 { "getColor", "(I)I" }, /* public int getColor(int); */
245 { "forbidColor", "(II)V" }, /* public void forbidColor(int, int); */
246 { "coalesce", "()V" }, /* public void coalesce(); */
247 { "dump", "(Ljava/lang/String;)V" } /* public void dump(String); */
248 { "finish", "()V" } /* public void finish(); */
251 /* public static coalescing.Extern createExtern(java.lang.String, int, int, int); */
252 static const struct _mth_info_t mthi_factory = {
253 "createExtern", "(Ljava/lang/String;III)Lcoalescing/Extern;"
256 struct _java_coal_t {
261 jmethodID mth_ids[mth_last];
264 static void jc_call_void(java_coal_t *c, int mth_index, ...)
266 JNIEnv *jni = c->env->jni;
267 jmethodID mid = c->mth_ids[mth_index];
271 va_start(args, mth_index);
272 (*jni)->CallVoidMethodV(jni, c->obj, mid, args);
277 static int jc_call_int(java_coal_t *c, int mth_index, ...)
279 JNIEnv *jni = c->env->jni;
280 jmethodID mid = c->mth_ids[mth_index];
285 va_start(args, mth_index);
286 res = (*jni)->CallIntMethodV(jni, c->obj, mid, args);
293 java_coal_t *java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
296 jni_env_t *env = get_jvm();
297 JNIEnv *jni = env->jni;
303 c = malloc(sizeof(c[0]));
304 memset(c, 0, sizeof(c[0]));
307 /* Find the class we are are looking for. */
308 cls = (*jni)->FindClass(jni, cls_name);
311 /* Get the static factory method. */
312 fact = (*jni)->GetStaticMethodID(jni, cls, mthi_factory.name, mthi_factory.sig);
315 /* Call the factory. */
316 str = (*jni)->NewStringUTF(jni, graph_name);
318 c->obj = (*jni)->CallStaticObjectMethod(jni, cls, fact, str, n_nodes, n_regs, dbg_level);
320 c->cls = (*jni)->GetObjectClass(jni, c->obj);
322 /* Reference the created object. */
323 c->obj = (*jni)->NewGlobalRef(jni, c->obj);
326 /* Lookup the member methods of the object. */
327 for(i = 0; i < mth_last; ++i) {
328 c->mth_ids[i] = (*jni)->GetMethodID(jni, c->cls, mthis[i].name, mthis[i].sig);
335 void java_coal_destroy(java_coal_t *c) {
336 JNIEnv *jni = c->env->jni;
337 jc_call_void(c, mth_finish);
338 (*jni)->DeleteGlobalRef(jni, c->obj);
342 void java_coal_add_int_edge(java_coal_t *c, int n, int m)
344 jc_call_void(c, mth_add_int_edge, (jint) n, (jint) m);
347 void java_coal_add_aff_edge(java_coal_t *c, int n, int m, int weight)
349 jc_call_void(c, mth_add_aff_edge, (jint) n, (jint) m, (jint) weight);
352 void java_coal_set_color(java_coal_t *c, int n, int col)
354 jc_call_void(c, mth_set_color, (jint) n, (jint) col);
357 void java_coal_forbid_color(java_coal_t *c, int n, int col)
359 jc_call_void(c, mth_forbid_color, (jint) n, (jint) col);
362 void java_coal_coalesce(java_coal_t *c)
364 jc_call_void(c, mth_coalesce);
367 void java_coal_dump(java_coal_t *c, const char *fn)
369 JNIEnv *jni = c->env->jni;
370 jmethodID mid = c->mth_ids[mth_dump];
373 str = (*jni)->NewStringUTF(jni, fn);
375 (*jni)->CallVoidMethod(jni, c->obj, mid, str);
379 int java_coal_get_color(java_coal_t *c, int n)
381 return jc_call_int(c, mth_get_color, (jint) n);
386 java_coal_t *java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
388 assert(0 && "use --enable-jvm");
392 void java_coal_destroy(java_coal_t *c)
394 assert(0 && "use --enable-jvm");
398 void java_coal_add_int_edge(java_coal_t *c, int n, int m)
400 assert(0 && "use --enable-jvm");
403 void java_coal_add_aff_edge(java_coal_t *c, int n, int m, int weight)
405 assert(0 && "use --enable-jvm");
408 void java_coal_set_color(java_coal_t *c, int n, int col)
410 assert(0 && "use --enable-jvm");
413 void java_coal_forbid_color(java_coal_t *c, int n, int col)
415 assert(0 && "use --enable-jvm");
418 void java_coal_coalesce(java_coal_t *c)
420 assert(0 && "use --enable-jvm");
423 void java_coal_dump(java_coal_t *c, const char *fn)
425 assert(0 && "use --enable-jvm");
428 int java_coal_get_color(java_coal_t *c, int n)
430 assert(0 && "use --enable-jvm");
435 #endif /* WITH_JVM */