- renamed access offset functions
[libfirm] / ir / be / bejavacoal.c
1
2 #ifdef HAVE_CONFIG_H
3 #include "config.h"
4 #endif
5
6 #ifdef WITH_LIBCORE
7 #include <libcore/lc_opts.h>
8 #include <libcore/lc_opts_enum.h>
9 #endif /* WITH_LIBCORE */
10
11 #ifdef _WIN32
12 #include <windows.h>
13 #else
14 #include <dlfcn.h>
15 #endif
16
17 #include <signal.h>
18 #include <stdlib.h>
19 #include <assert.h>
20 #include <stdio.h>
21
22 #include "bejavacoal.h"
23
24 #ifdef WITH_JVM
25
26 /* Path to the jar file. A little OS dependent convenience. */
27 #ifdef _WIN32
28 static char jar_file[512] = "y:\\user\\hack\\public\\coal.jar";
29 #else
30 static char jar_file[512] = "/ben/hack/public/coal.jar";
31 #endif
32
33 static char cls_name[256] = "coalescing/mst/safe/Algo";
34
35 /* Name of the JVM dll/so */
36 static char jvm_lib[512] = { 0 };
37
38 #ifdef WITH_LIBCORE
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)),
43         { NULL }
44 };
45
46 void be_java_coal_register_options(lc_opt_entry_t *grp)
47 {
48         lc_opt_entry_t *jc_grp = lc_opt_get_grp(grp, "jc");
49         lc_opt_add_table(jc_grp, options);
50 }
51 #endif /* WITH_LIBCORE */
52
53 #include <jni.h>
54
55 typedef struct _jni_env_t {
56         JavaVM *jvm;
57         JNIEnv *jni;
58 } jni_env_t;
59
60 /*
61
62         Ugly code to retrieve the JVM dll/so file.
63
64 */
65
66 #ifdef _WIN32
67 /* Win32 version */
68 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
69 {
70         HINSTANCE hVM = LoadLibrary(vmlibpath);
71         return hVM ? GetProcAddress(hVM, sym) : NULL;
72 }
73
74 #define JRE_KEY "SOFTWARE\\JavaSoft\\Java Development Kit"
75
76 static char *locate_jvm_lib(char *path, size_t path_len)
77 {
78         char version[32];
79         char buf[256];
80         DWORD version_len = sizeof(version);
81         DWORD dwPathLen = path_len;
82         HKEY hKey;
83
84         RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_QUERY_VALUE, &hKey);
85         RegQueryValueEx(hKey, "CurrentVersion", NULL, NULL, (LPBYTE) version, &version_len);
86         RegCloseKey(hKey);
87
88         _snprintf(buf, sizeof(buf), JRE_KEY "\\%s", version);
89         RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &hKey);
90         RegQueryValueEx(hKey, "JavaHome", NULL, NULL, (LPBYTE) path, &dwPathLen);
91         RegCloseKey(hKey);
92
93         strncat(path, "\\jre\\bin\\server\\jvm.dll", path_len);
94         return path;
95 }
96
97 #else
98 /* Unix version */
99 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
100 {
101         void *libVM = dlopen(vmlibpath, RTLD_LAZY);
102         return libVM ? dlsym(libVM, sym) : NULL;
103 }
104
105 static char *locate_jvm_lib(char *path, size_t n)
106 {
107         return NULL;
108 }
109 #endif
110
111 static int start_vm(jni_env_t *env, int argc, char *argv[])
112 {
113         int i;
114         long ret;
115         JavaVMInitArgs args;
116         JavaVMOption *opts;
117
118         long (JNICALL * create_func)(JavaVM **, void **, void *) = find_jvm_symbol(jvm_lib, "JNI_CreateJavaVM");
119
120         if(!create_func) {
121                 fprintf(stderr, "could not find JVM creation function\n");
122                 exit(1);
123         }
124
125         memset(&args, 0, sizeof(args));
126         opts = malloc(argc * sizeof(opts[0]));
127         for(i = 0; i < argc; ++i) {
128                 opts[i].optionString = argv[i];
129                 opts[i].extraInfo    = NULL;
130         }
131
132         args.version  = JNI_VERSION_1_4;
133         args.nOptions = argc;
134         args.options  = opts;
135         args.ignoreUnrecognized = JNI_FALSE;
136
137         ret = create_func(&env->jvm, (void **) &env->jni, &args);
138         free(opts);
139         if(ret != JNI_OK) {
140                 fprintf(stderr, "JNI_CreateJavaVM returned errrocode %ld\n" , ret);
141                 return 0;
142         }
143
144         return 1;
145 }
146
147 static void stop_vm(jni_env_t *env)
148 {
149         JavaVM *jvm = env->jvm;
150         (*jvm)->DetachCurrentThread(jvm);
151         (*jvm)->DestroyJavaVM(jvm);
152 }
153
154 static int jvm_inited = 0;
155 static jni_env_t env;
156 void (*old_int_handler)(int);
157 void (*old_abrt_handler)(int);
158
159 static void sig_jvm_destroy_at_exit(int signal)
160 {
161         if(jvm_inited)
162                 stop_vm(&env);
163
164         switch(signal) {
165         case SIGABRT:
166                 old_abrt_handler(signal);
167                 break;
168         case SIGINT:
169                 old_int_handler(signal);
170                 break;
171         default:;
172         }
173 }
174
175 static void jvm_destroy_at_exit(void)
176 {
177         sig_jvm_destroy_at_exit(0);
178 }
179
180 static jni_env_t *get_jvm(void)
181 {
182         char cp_param[512];
183         char *args[1];
184
185         if(!jvm_inited) {
186                 /* Find the dll */
187                 if(strlen(jvm_lib) == 0) {
188                         if(!locate_jvm_lib(jvm_lib, sizeof(jvm_lib))) {
189                                 fprintf(stderr, "could not find jvm library\n");
190                                 exit(1);
191                         }
192                 }
193
194                 snprintf(cp_param, sizeof(cp_param), "-Djava.class.path=%s", jar_file);
195                 args[0] = cp_param;
196                 if(!start_vm(&env, sizeof(args) / sizeof(args[0]), args)) {
197                         fprintf(stderr, "Couldn't initialize java VM\n");
198                         abort();
199                 }
200                 jvm_inited = 1;
201                 old_int_handler  = signal(SIGINT,  sig_jvm_destroy_at_exit);
202                 old_abrt_handler = signal(SIGABRT, sig_jvm_destroy_at_exit);
203                 atexit(jvm_destroy_at_exit);
204         }
205
206         return &env;
207 }
208
209
210 static void check(jni_env_t *env, const char *file, int line)
211 {
212         JNIEnv *jni = env->jni;
213         jboolean exc = (*jni)->ExceptionCheck(jni);
214         if(exc) {
215                 fprintf(stderr, "%s:%d: ", file, line);
216                 (*jni)->ExceptionDescribe(jni);
217                 (*jni)->ExceptionClear(jni);
218                 stop_vm(env);
219                 abort();
220         }
221 }
222
223 #define CHECK(env) check(env, __FILE__, __LINE__)
224
225 enum {
226         mth_add_int_edge,
227         mth_add_aff_edge,
228         mth_set_color,
229         //mth_set_debug,
230         mth_get_color,
231         mth_forbid_color,
232         mth_coalesce,
233         mth_dump,
234         mth_finish,
235         mth_last
236 };
237
238 struct _mth_info_t {
239         const char *name;
240         const char *sig;
241 };
242
243 static const struct _mth_info_t mthis[mth_last] = {
244         { "addIntEdge",  "(II)V"                   }, /* public void addIntEdge(int, int); */
245         { "addAffEdge",  "(III)V"                  }, /* public void addAffEdge(int, int, int); */
246         { "setColor",    "(II)V"                   }, /* public void setColor(int, int); */
247         //{ "setDebug",    "(ILjava/lang/String;)V"  }, /* public void setDebug(int, String); */
248         { "getColor",    "(I)I"                    }, /* public int getColor(int); */
249         { "forbidColor", "(II)V"                   }, /* public void forbidColor(int, int); */
250         { "coalesce",    "()V"                     }, /* public void coalesce(); */
251         { "dump",        "(Ljava/lang/String;)V"   }, /* public void dump(String); */
252         { "finish",      "()V"                     }  /* public void finish(); */
253 };
254
255 /* public static coalescing.Extern createExtern(java.lang.String, int, int, int); */
256 static const struct _mth_info_t mthi_factory = {
257         "createExtern", "(Ljava/lang/String;III)Lcoalescing/Extern;"
258 };
259
260 struct _be_java_coal_t {
261         jni_env_t *env;
262         jclass    cls;
263         jobject   obj;
264
265         jmethodID mth_ids[mth_last];
266 };
267
268 static void jc_call_void(be_java_coal_t *c, int mth_index, ...)
269 {
270         JNIEnv *jni   = c->env->jni;
271         jmethodID mid = c->mth_ids[mth_index];
272
273         va_list args;
274
275         va_start(args, mth_index);
276         (*jni)->CallVoidMethodV(jni, c->obj, mid, args);
277         CHECK(c->env);
278         va_end(args);
279 }
280
281 static int jc_call_int(be_java_coal_t *c, int mth_index, ...)
282 {
283         JNIEnv *jni   = c->env->jni;
284         jmethodID mid = c->mth_ids[mth_index];
285
286         int res;
287         va_list args;
288
289         va_start(args, mth_index);
290         res = (*jni)->CallIntMethodV(jni, c->obj, mid, args);
291         CHECK(c->env);
292         va_end(args);
293
294         return res;
295 }
296
297 be_java_coal_t *be_java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
298 {
299         be_java_coal_t *c;
300         jni_env_t *env = get_jvm();
301         JNIEnv *jni = env->jni;
302         jmethodID fact;
303         jclass cls;
304         jstring str;
305         int i;
306
307         c = malloc(sizeof(c[0]));
308         memset(c, 0, sizeof(c[0]));
309         c->env = env;
310
311         /* Find the class we are are looking for. */
312         cls = (*jni)->FindClass(jni, cls_name);
313         CHECK(env);
314
315         /* Get the static factory method. */
316         fact = (*jni)->GetStaticMethodID(jni, cls, mthi_factory.name, mthi_factory.sig);
317         CHECK(env);
318
319         /* Call the factory. */
320         str = (*jni)->NewStringUTF(jni, graph_name);
321         CHECK(env);
322         c->obj = (*jni)->CallStaticObjectMethod(jni, cls, fact, str, n_nodes, n_regs, dbg_level);
323         CHECK(env);
324         c->cls = (*jni)->GetObjectClass(jni, c->obj);
325
326         /* Reference the created object. */
327         c->obj = (*jni)->NewGlobalRef(jni, c->obj);
328         CHECK(env);
329
330         /* Lookup the member methods of the object. */
331         for(i = 0; i < mth_last; ++i) {
332                 c->mth_ids[i] = (*jni)->GetMethodID(jni, c->cls, mthis[i].name, mthis[i].sig);
333                 CHECK(env);
334         }
335
336         return c;
337 }
338
339 void be_java_coal_destroy(be_java_coal_t *c) {
340         JNIEnv *jni = c->env->jni;
341         jc_call_void(c, mth_finish);
342         (*jni)->DeleteGlobalRef(jni, c->obj);
343         free(c);
344 }
345
346 void be_java_coal_add_int_edge(be_java_coal_t *c, int n, int m)
347 {
348         jc_call_void(c, mth_add_int_edge, (jint) n, (jint) m);
349 }
350
351 void be_java_coal_add_aff_edge(be_java_coal_t *c, int n, int m, int weight)
352 {
353         jc_call_void(c, mth_add_aff_edge, (jint) n, (jint) m, (jint) weight);
354 }
355
356 void be_java_coal_set_color(be_java_coal_t *c, int n, int col)
357 {
358         jc_call_void(c, mth_set_color, (jint) n, (jint) col);
359 }
360
361 void be_java_coal_set_debug(be_java_coal_t *c, int n, const char *dbg)
362 {
363 #if 0
364         JNIEnv *jni   = c->env->jni;
365         jmethodID mid = c->mth_ids[mth_set_debug];
366         jstring str;
367
368         str = (*jni)->NewStringUTF(jni, dbg);
369         CHECK(c->env);
370         (*jni)->CallVoidMethod(jni, c->obj, mid, (jint) n, str);
371         CHECK(c->env);
372 #endif
373 }
374
375 void be_java_coal_forbid_color(be_java_coal_t *c, int n, int col)
376 {
377         jc_call_void(c, mth_forbid_color, (jint) n, (jint) col);
378 }
379
380 void be_java_coal_coalesce(be_java_coal_t *c)
381 {
382         jc_call_void(c, mth_coalesce);
383 }
384
385 void be_java_coal_dump(be_java_coal_t *c, const char *fn)
386 {
387         JNIEnv *jni   = c->env->jni;
388         jmethodID mid = c->mth_ids[mth_dump];
389         jstring str;
390
391         str = (*jni)->NewStringUTF(jni, fn);
392         CHECK(c->env);
393         (*jni)->CallVoidMethod(jni, c->obj, mid, str);
394         CHECK(c->env);
395 }
396
397 int be_java_coal_get_color(be_java_coal_t *c, int n)
398 {
399         return jc_call_int(c, mth_get_color, (jint) n);
400 }
401
402 void be_java_coal_start_jvm(void)
403 {
404         get_jvm();
405 }
406
407 #else
408
409 be_java_coal_t *be_java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
410 {
411         assert(0 && "use --enable-jvm");
412         return NULL;
413 }
414
415 void be_java_coal_destroy(be_java_coal_t *c)
416 {
417         assert(0 && "use --enable-jvm");
418 }
419
420
421 void be_java_coal_add_int_edge(be_java_coal_t *c, int n, int m)
422 {
423         assert(0 && "use --enable-jvm");
424 }
425
426 void be_java_coal_add_aff_edge(be_java_coal_t *c, int n, int m, int weight)
427 {
428         assert(0 && "use --enable-jvm");
429 }
430
431 void be_java_coal_set_color(be_java_coal_t *c, int n, int col)
432 {
433         assert(0 && "use --enable-jvm");
434 }
435
436 void be_java_coal_set_debug(be_java_coal_t *c, int n, const char *dbg)
437 {
438         assert(0 && "use --enable-jvm");
439 }
440
441 void be_java_coal_forbid_color(be_java_coal_t *c, int n, int col)
442 {
443         assert(0 && "use --enable-jvm");
444 }
445
446 void be_java_coal_coalesce(be_java_coal_t *c)
447 {
448         assert(0 && "use --enable-jvm");
449 }
450
451 void be_java_coal_dump(be_java_coal_t *c, const char *fn)
452 {
453         assert(0 && "use --enable-jvm");
454 }
455
456 int be_java_coal_get_color(be_java_coal_t *c, int n)
457 {
458         assert(0 && "use --enable-jvm");
459         return -1;
460 }
461
462 void be_java_coal_start_jvm(void)
463 {
464 }
465
466
467 #endif /* WITH_JVM */