added function to retrieve irn ops
[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 /* Path to the jar file. A little OS dependent convenience. */
25 #ifdef _WIN32
26 static char jar_file[512] = "y:\\user\\hack\\public\\coal.jar";
27 #else
28 static char jar_file[512] = "/ben/hack/public/coal.jar";
29 #endif
30
31 static char cls_name[256] = "coalescing/mst/safe/Algo";
32
33 /* Name of teh JVM dll/so */
34 static char jvm_lib[512] = { 0 };
35
36 #ifdef WITH_LIBCORE
37 static const lc_opt_table_entry_t options[] = {
38         LC_OPT_ENT_STR      ("jvm",  "absolute path to jvm dll",                    jvm_lib, sizeof(jvm_lib)),
39         LC_OPT_ENT_STR      ("jar",  "jar file of the coalescer",                   jar_file, sizeof(jar_file)),
40         LC_OPT_ENT_STR      ("cls",  "name of the class providing the factory",     cls_name, sizeof(cls_name)),
41         { NULL }
42 };
43
44 void be_java_coal_register_options(lc_opt_entry_t *grp)
45 {
46         lc_opt_entry_t *jc_grp = lc_opt_get_grp(grp, "jc");
47         lc_opt_add_table(jc_grp, options);
48 }
49 #endif
50
51 #ifdef WITH_JVM
52 #include <jni.h>
53
54 typedef struct _jni_env_t {
55         JavaVM *jvm;
56         JNIEnv *jni;
57 } jni_env_t;
58 #endif
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 #ifdef WITH_JVM
112 static int start_vm(jni_env_t *env, int argc, char *argv[])
113 {
114         int i;
115         long ret;
116         JavaVMInitArgs args;
117         JavaVMOption *opts;
118
119         long (JNICALL * create_func)(JavaVM **, void **, void *) = find_jvm_symbol(jvm_lib, "JNI_CreateJavaVM");
120
121         if(!create_func) {
122                 fprintf(stderr, "could not find JVM creation function\n");
123                 exit(1);
124         }
125
126         memset(&args, 0, sizeof(args));
127         opts = malloc(argc * sizeof(opts[0]));
128         for(i = 0; i < argc; ++i) {
129                 opts[i].optionString = argv[i];
130                 opts[i].extraInfo    = NULL;
131         }
132
133         args.version  = JNI_VERSION_1_4;
134         args.nOptions = argc;
135         args.options  = opts;
136         args.ignoreUnrecognized = JNI_FALSE;
137
138         ret = create_func(&env->jvm, (void **) &env->jni, &args);
139         free(opts);
140         if(ret != JNI_OK) {
141                 fprintf(stderr, "JNI_CreateJavaVM returned errrocode %ld\n" , ret);
142                 return 0;
143         }
144
145         return 1;
146 }
147
148 static void stop_vm(jni_env_t *env)
149 {
150         JavaVM *jvm = env->jvm;
151         (*jvm)->DetachCurrentThread(jvm);
152         (*jvm)->DestroyJavaVM(jvm);
153 }
154
155 static int jvm_inited = 0;
156 static jni_env_t env;
157 void (*old_int_handler)(int);
158 void (*old_abrt_handler)(int);
159
160 static void sig_jvm_destroy_at_exit(int signal)
161 {
162         if(jvm_inited)
163                 stop_vm(&env);
164
165         switch(signal) {
166         case SIGABRT:
167                 old_abrt_handler(signal);
168                 break;
169         case SIGINT:
170                 old_int_handler(signal);
171                 break;
172         default:;
173         }
174 }
175
176 static void jvm_destroy_at_exit(void)
177 {
178         sig_jvm_destroy_at_exit(0);
179 }
180
181 static jni_env_t *get_jvm(void)
182 {
183         char cp_param[512];
184         char *args[1];
185
186         if(!jvm_inited) {
187                 /* Find the dll */
188                 if(strlen(jvm_lib) == 0) {
189                         if(!locate_jvm_lib(jvm_lib, sizeof(jvm_lib))) {
190                                 fprintf(stderr, "could not find jvm library\n");
191                                 exit(1);
192                         }
193                 }
194
195                 snprintf(cp_param, sizeof(cp_param), "-Djava.class.path=%s", jar_file);
196                 args[0] = cp_param;
197                 if(!start_vm(&env, sizeof(args) / sizeof(args[0]), args)) {
198                         fprintf(stderr, "Couldn't initialize java VM\n");
199                         abort();
200                 }
201                 jvm_inited = 1;
202                 old_int_handler  = signal(SIGINT,  sig_jvm_destroy_at_exit);
203                 old_abrt_handler = signal(SIGABRT, sig_jvm_destroy_at_exit);
204                 atexit(jvm_destroy_at_exit);
205         }
206
207         return &env;
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_get_color,
230         mth_forbid_color,
231         mth_coalesce,
232         mth_dump,
233         mth_finish,
234         mth_last
235 };
236
237 struct _mth_info_t {
238         const char *name;
239         const char *sig;
240 };
241
242 static const struct _mth_info_t mthis[mth_last] = {
243         { "addIntEdge",  "(II)V"                   }, /* public void addIntEdge(int, int); */
244         { "addAffEdge",  "(III)V"                  }, /* public void addAffEdge(int, int, int); */
245         { "setColor",    "(II)V"                   }, /* public void setColor(int, int); */
246         { "getColor",    "(I)I"                    }, /* public int getColor(int); */
247         { "forbidColor", "(II)V"                   }, /* public void forbidColor(int, int); */
248         { "coalesce",    "()V"                     }, /* public void coalesce(); */
249         { "dump",        "(Ljava/lang/String;)V"   }, /* public void dump(String); */
250         { "finish",      "()V"                     }  /* public void finish(); */
251 };
252
253 /* public static coalescing.Extern createExtern(java.lang.String, int, int, int); */
254 static const struct _mth_info_t mthi_factory = {
255         "createExtern", "(Ljava/lang/String;III)Lcoalescing/Extern;"
256 };
257
258 struct _be_java_coal_t {
259         jni_env_t *env;
260         jclass    cls;
261         jobject   obj;
262
263         jmethodID mth_ids[mth_last];
264 };
265
266 static void jc_call_void(be_java_coal_t *c, int mth_index, ...)
267 {
268         JNIEnv *jni   = c->env->jni;
269         jmethodID mid = c->mth_ids[mth_index];
270
271         va_list args;
272
273         va_start(args, mth_index);
274         (*jni)->CallVoidMethodV(jni, c->obj, mid, args);
275         CHECK(c->env);
276         va_end(args);
277 }
278
279 static int jc_call_int(be_java_coal_t *c, int mth_index, ...)
280 {
281         JNIEnv *jni   = c->env->jni;
282         jmethodID mid = c->mth_ids[mth_index];
283
284         int res;
285         va_list args;
286
287         va_start(args, mth_index);
288         res = (*jni)->CallIntMethodV(jni, c->obj, mid, args);
289         CHECK(c->env);
290         va_end(args);
291
292         return res;
293 }
294
295 be_java_coal_t *be_java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
296 {
297         be_java_coal_t *c;
298         jni_env_t *env = get_jvm();
299         JNIEnv *jni = env->jni;
300         jmethodID fact;
301         jclass cls;
302         jstring str;
303         int i;
304
305         c = malloc(sizeof(c[0]));
306         memset(c, 0, sizeof(c[0]));
307         c->env = env;
308
309         /* Find the class we are are looking for. */
310         cls = (*jni)->FindClass(jni, cls_name);
311         CHECK(env);
312
313         /* Get the static factory method. */
314         fact = (*jni)->GetStaticMethodID(jni, cls, mthi_factory.name, mthi_factory.sig);
315         CHECK(env);
316
317         /* Call the factory. */
318         str = (*jni)->NewStringUTF(jni, graph_name);
319         CHECK(env);
320         c->obj = (*jni)->CallStaticObjectMethod(jni, cls, fact, str, n_nodes, n_regs, dbg_level);
321         CHECK(env);
322         c->cls = (*jni)->GetObjectClass(jni, c->obj);
323
324         /* Reference the created object. */
325         c->obj = (*jni)->NewGlobalRef(jni, c->obj);
326         CHECK(env);
327
328         /* Lookup the member methods of the object. */
329         for(i = 0; i < mth_last; ++i) {
330                 c->mth_ids[i] = (*jni)->GetMethodID(jni, c->cls, mthis[i].name, mthis[i].sig);
331                 CHECK(env);
332         }
333
334         return c;
335 }
336
337 void be_java_coal_destroy(be_java_coal_t *c) {
338         JNIEnv *jni = c->env->jni;
339         jc_call_void(c, mth_finish);
340         (*jni)->DeleteGlobalRef(jni, c->obj);
341         free(c);
342 }
343
344 void be_java_coal_add_int_edge(be_java_coal_t *c, int n, int m)
345 {
346         jc_call_void(c, mth_add_int_edge, (jint) n, (jint) m);
347 }
348
349 void be_java_coal_add_aff_edge(be_java_coal_t *c, int n, int m, int weight)
350 {
351         jc_call_void(c, mth_add_aff_edge, (jint) n, (jint) m, (jint) weight);
352 }
353
354 void be_java_coal_set_color(be_java_coal_t *c, int n, int col)
355 {
356         jc_call_void(c, mth_set_color, (jint) n, (jint) col);
357 }
358
359 void be_java_coal_forbid_color(be_java_coal_t *c, int n, int col)
360 {
361         jc_call_void(c, mth_forbid_color, (jint) n, (jint) col);
362 }
363
364 void be_java_coal_coalesce(be_java_coal_t *c)
365 {
366         jc_call_void(c, mth_coalesce);
367 }
368
369 void be_java_coal_dump(be_java_coal_t *c, const char *fn)
370 {
371         JNIEnv *jni   = c->env->jni;
372         jmethodID mid = c->mth_ids[mth_dump];
373         jstring str;
374
375         str = (*jni)->NewStringUTF(jni, fn);
376         CHECK(c->env);
377         (*jni)->CallVoidMethod(jni, c->obj, mid, str);
378         CHECK(c->env);
379 }
380
381 int be_java_coal_get_color(be_java_coal_t *c, int n)
382 {
383         return jc_call_int(c, mth_get_color, (jint) n);
384 }
385
386 #else
387
388 be_java_coal_t *be_java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
389 {
390         assert(0 && "use --enable-jvm");
391         return NULL;
392 }
393
394 void be_java_coal_destroy(be_java_coal_t *c)
395 {
396         assert(0 && "use --enable-jvm");
397 }
398
399
400 void be_java_coal_add_int_edge(be_java_coal_t *c, int n, int m)
401 {
402         assert(0 && "use --enable-jvm");
403 }
404
405 void be_java_coal_add_aff_edge(be_java_coal_t *c, int n, int m, int weight)
406 {
407         assert(0 && "use --enable-jvm");
408 }
409
410 void be_java_coal_set_color(be_java_coal_t *c, int n, int col)
411 {
412         assert(0 && "use --enable-jvm");
413 }
414
415 void be_java_coal_forbid_color(be_java_coal_t *c, int n, int col)
416 {
417         assert(0 && "use --enable-jvm");
418 }
419
420 void be_java_coal_coalesce(be_java_coal_t *c)
421 {
422         assert(0 && "use --enable-jvm");
423 }
424
425 void be_java_coal_dump(be_java_coal_t *c, const char *fn)
426 {
427         assert(0 && "use --enable-jvm");
428 }
429
430 int be_java_coal_get_color(be_java_coal_t *c, int n)
431 {
432         assert(0 && "use --enable-jvm");
433         return -1;
434 }
435
436
437 #endif /* WITH_JVM */