forgot a file
[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_init_javacoal(void)
47 {
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);
53 }
54
55 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_javacoal);
56 #endif /* WITH_LIBCORE */
57
58 #include <jni.h>
59
60 typedef struct _jni_env_t {
61         JavaVM *jvm;
62         JNIEnv *jni;
63 } jni_env_t;
64
65 /*
66
67         Ugly code to retrieve the JVM dll/so file.
68
69 */
70
71 #ifdef _WIN32
72 /* Win32 version */
73 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
74 {
75         HINSTANCE hVM = LoadLibrary(vmlibpath);
76         return hVM ? GetProcAddress(hVM, sym) : NULL;
77 }
78
79 #define JRE_KEY "SOFTWARE\\JavaSoft\\Java Development Kit"
80
81 static char *locate_jvm_lib(char *path, size_t path_len)
82 {
83         char version[32];
84         char buf[256];
85         DWORD version_len = sizeof(version);
86         DWORD dwPathLen = path_len;
87         HKEY hKey;
88
89         RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_QUERY_VALUE, &hKey);
90         RegQueryValueEx(hKey, "CurrentVersion", NULL, NULL, (LPBYTE) version, &version_len);
91         RegCloseKey(hKey);
92
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);
96         RegCloseKey(hKey);
97
98         strncat(path, "\\jre\\bin\\server\\jvm.dll", path_len);
99         return path;
100 }
101
102 #else
103 /* Unix version */
104 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
105 {
106         void *libVM = dlopen(vmlibpath, RTLD_LAZY);
107         return libVM ? dlsym(libVM, sym) : NULL;
108 }
109
110 static char *locate_jvm_lib(char *path, size_t n)
111 {
112         return NULL;
113 }
114 #endif
115
116 static int start_vm(jni_env_t *env, int argc, char *argv[])
117 {
118         int i;
119         long ret;
120         JavaVMInitArgs args;
121         JavaVMOption *opts;
122
123         long (JNICALL * create_func)(JavaVM **, void **, void *) = find_jvm_symbol(jvm_lib, "JNI_CreateJavaVM");
124
125         if(!create_func) {
126                 fprintf(stderr, "could not find JVM creation function\n");
127                 exit(1);
128         }
129
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;
135         }
136
137         args.version  = JNI_VERSION_1_4;
138         args.nOptions = argc;
139         args.options  = opts;
140         args.ignoreUnrecognized = JNI_FALSE;
141
142         ret = create_func(&env->jvm, (void **) &env->jni, &args);
143         free(opts);
144         if(ret != JNI_OK) {
145                 fprintf(stderr, "JNI_CreateJavaVM returned errrocode %ld\n" , ret);
146                 return 0;
147         }
148
149         return 1;
150 }
151
152 static void stop_vm(jni_env_t *env)
153 {
154         JavaVM *jvm = env->jvm;
155         (*jvm)->DetachCurrentThread(jvm);
156         (*jvm)->DestroyJavaVM(jvm);
157 }
158
159 static int jvm_inited = 0;
160 static jni_env_t env;
161 void (*old_int_handler)(int);
162 void (*old_abrt_handler)(int);
163
164 static void sig_jvm_destroy_at_exit(int signal)
165 {
166         if(jvm_inited)
167                 stop_vm(&env);
168
169         switch(signal) {
170         case SIGABRT:
171                 old_abrt_handler(signal);
172                 break;
173         case SIGINT:
174                 old_int_handler(signal);
175                 break;
176         default:;
177         }
178 }
179
180 static void jvm_destroy_at_exit(void)
181 {
182         sig_jvm_destroy_at_exit(0);
183 }
184
185 static jni_env_t *get_jvm(void)
186 {
187         char cp_param[512];
188         char *args[1];
189
190         if(!jvm_inited) {
191                 /* Find the dll */
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");
195                                 exit(1);
196                         }
197                 }
198
199                 snprintf(cp_param, sizeof(cp_param), "-Djava.class.path=%s", jar_file);
200                 args[0] = cp_param;
201                 if(!start_vm(&env, sizeof(args) / sizeof(args[0]), args)) {
202                         fprintf(stderr, "Couldn't initialize java VM\n");
203                         abort();
204                 }
205                 jvm_inited = 1;
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);
209         }
210
211         return &env;
212 }
213
214
215 static void check(jni_env_t *env, const char *file, int line)
216 {
217         JNIEnv *jni = env->jni;
218         jboolean exc = (*jni)->ExceptionCheck(jni);
219         if(exc) {
220                 fprintf(stderr, "%s:%d: ", file, line);
221                 (*jni)->ExceptionDescribe(jni);
222                 (*jni)->ExceptionClear(jni);
223                 stop_vm(env);
224                 abort();
225         }
226 }
227
228 #define CHECK(env) check(env, __FILE__, __LINE__)
229
230 enum {
231         mth_add_int_edge,
232         mth_add_aff_edge,
233         mth_set_color,
234         //mth_set_debug,
235         mth_get_color,
236         mth_forbid_color,
237         mth_coalesce,
238         mth_dump,
239         mth_finish,
240         mth_last
241 };
242
243 struct _mth_info_t {
244         const char *name;
245         const char *sig;
246 };
247
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(); */
258 };
259
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;"
263 };
264
265 struct _be_java_coal_t {
266         jni_env_t *env;
267         jclass    cls;
268         jobject   obj;
269
270         jmethodID mth_ids[mth_last];
271 };
272
273 static void jc_call_void(be_java_coal_t *c, int mth_index, ...)
274 {
275         JNIEnv *jni   = c->env->jni;
276         jmethodID mid = c->mth_ids[mth_index];
277
278         va_list args;
279
280         va_start(args, mth_index);
281         (*jni)->CallVoidMethodV(jni, c->obj, mid, args);
282         CHECK(c->env);
283         va_end(args);
284 }
285
286 static int jc_call_int(be_java_coal_t *c, int mth_index, ...)
287 {
288         JNIEnv *jni   = c->env->jni;
289         jmethodID mid = c->mth_ids[mth_index];
290
291         int res;
292         va_list args;
293
294         va_start(args, mth_index);
295         res = (*jni)->CallIntMethodV(jni, c->obj, mid, args);
296         CHECK(c->env);
297         va_end(args);
298
299         return res;
300 }
301
302 be_java_coal_t *be_java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
303 {
304         be_java_coal_t *c;
305         jni_env_t *env = get_jvm();
306         JNIEnv *jni = env->jni;
307         jmethodID fact;
308         jclass cls;
309         jstring str;
310         int i;
311
312         c = malloc(sizeof(c[0]));
313         memset(c, 0, sizeof(c[0]));
314         c->env = env;
315
316         /* Find the class we are are looking for. */
317         cls = (*jni)->FindClass(jni, cls_name);
318         CHECK(env);
319
320         /* Get the static factory method. */
321         fact = (*jni)->GetStaticMethodID(jni, cls, mthi_factory.name, mthi_factory.sig);
322         CHECK(env);
323
324         /* Call the factory. */
325         str = (*jni)->NewStringUTF(jni, graph_name);
326         CHECK(env);
327         c->obj = (*jni)->CallStaticObjectMethod(jni, cls, fact, str, n_nodes, n_regs, dbg_level);
328         CHECK(env);
329         c->cls = (*jni)->GetObjectClass(jni, c->obj);
330
331         /* Reference the created object. */
332         c->obj = (*jni)->NewGlobalRef(jni, c->obj);
333         CHECK(env);
334
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);
338                 CHECK(env);
339         }
340
341         return c;
342 }
343
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);
348         free(c);
349 }
350
351 void be_java_coal_add_int_edge(be_java_coal_t *c, int n, int m)
352 {
353         jc_call_void(c, mth_add_int_edge, (jint) n, (jint) m);
354 }
355
356 void be_java_coal_add_aff_edge(be_java_coal_t *c, int n, int m, int weight)
357 {
358         jc_call_void(c, mth_add_aff_edge, (jint) n, (jint) m, (jint) weight);
359 }
360
361 void be_java_coal_set_color(be_java_coal_t *c, int n, int col)
362 {
363         jc_call_void(c, mth_set_color, (jint) n, (jint) col);
364 }
365
366 void be_java_coal_set_debug(be_java_coal_t *c, int n, const char *dbg)
367 {
368 #if 0
369         JNIEnv *jni   = c->env->jni;
370         jmethodID mid = c->mth_ids[mth_set_debug];
371         jstring str;
372
373         str = (*jni)->NewStringUTF(jni, dbg);
374         CHECK(c->env);
375         (*jni)->CallVoidMethod(jni, c->obj, mid, (jint) n, str);
376         CHECK(c->env);
377 #endif
378 }
379
380 void be_java_coal_forbid_color(be_java_coal_t *c, int n, int col)
381 {
382         jc_call_void(c, mth_forbid_color, (jint) n, (jint) col);
383 }
384
385 void be_java_coal_coalesce(be_java_coal_t *c)
386 {
387         jc_call_void(c, mth_coalesce);
388 }
389
390 void be_java_coal_dump(be_java_coal_t *c, const char *fn)
391 {
392         JNIEnv *jni   = c->env->jni;
393         jmethodID mid = c->mth_ids[mth_dump];
394         jstring str;
395
396         str = (*jni)->NewStringUTF(jni, fn);
397         CHECK(c->env);
398         (*jni)->CallVoidMethod(jni, c->obj, mid, str);
399         CHECK(c->env);
400 }
401
402 int be_java_coal_get_color(be_java_coal_t *c, int n)
403 {
404         return jc_call_int(c, mth_get_color, (jint) n);
405 }
406
407 void be_java_coal_start_jvm(void)
408 {
409         get_jvm();
410 }
411
412 #else
413
414 be_java_coal_t *be_java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
415 {
416         assert(0 && "use --enable-jvm");
417         return NULL;
418 }
419
420 void be_java_coal_destroy(be_java_coal_t *c)
421 {
422         assert(0 && "use --enable-jvm");
423 }
424
425
426 void be_java_coal_add_int_edge(be_java_coal_t *c, int n, int m)
427 {
428         assert(0 && "use --enable-jvm");
429 }
430
431 void be_java_coal_add_aff_edge(be_java_coal_t *c, int n, int m, int weight)
432 {
433         assert(0 && "use --enable-jvm");
434 }
435
436 void be_java_coal_set_color(be_java_coal_t *c, int n, int col)
437 {
438         assert(0 && "use --enable-jvm");
439 }
440
441 void be_java_coal_set_debug(be_java_coal_t *c, int n, const char *dbg)
442 {
443         assert(0 && "use --enable-jvm");
444 }
445
446 void be_java_coal_forbid_color(be_java_coal_t *c, int n, int col)
447 {
448         assert(0 && "use --enable-jvm");
449 }
450
451 void be_java_coal_coalesce(be_java_coal_t *c)
452 {
453         assert(0 && "use --enable-jvm");
454 }
455
456 void be_java_coal_dump(be_java_coal_t *c, const char *fn)
457 {
458         assert(0 && "use --enable-jvm");
459 }
460
461 int be_java_coal_get_color(be_java_coal_t *c, int n)
462 {
463         assert(0 && "use --enable-jvm");
464         return -1;
465 }
466
467 void be_java_coal_start_jvm(void)
468 {
469 }
470
471 #endif /* WITH_JVM */