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