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