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