Added finish() method
[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 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_ERR)
144                 return 0;
145
146         return 1;
147 }
148
149 static void stop_vm(jni_env_t *env)
150 {
151         JavaVM *jvm = env->jvm;
152         (*jvm)->DetachCurrentThread(jvm);
153         (*jvm)->DestroyJavaVM(jvm);
154 }
155
156 static int jvm_inited = 0;
157 static jni_env_t env;
158 void (*old_int_handler)(int);
159 void (*old_abrt_handler)(int);
160
161 static void sig_jvm_destroy_at_exit(int signal)
162 {
163         if(jvm_inited)
164                 stop_vm(&env);
165
166         switch(signal) {
167         case SIGABRT:
168                 old_abrt_handler(signal);
169                 break;
170         case SIGINT:
171                 old_int_handler(signal);
172                 break;
173         default:;
174         }
175 }
176
177 static void jvm_destroy_at_exit(void)
178 {
179         sig_jvm_destroy_at_exit(0);
180 }
181
182 static jni_env_t *get_jvm(void)
183 {
184         char cp_param[512];
185         char *args[1];
186
187         if(!jvm_inited) {
188                 /* Find the dll */
189                 if(strlen(jvm_lib) == 0) {
190                         if(!locate_jvm_lib(jvm_lib, sizeof(jvm_lib))) {
191                                 fprintf(stderr, "could not find jvm library\n");
192                                 exit(1);
193                         }
194                 }
195
196                 snprintf(cp_param, sizeof(cp_param), "-Djava.class.path=%s", jar_file);
197                 args[0] = cp_param;
198                 start_vm(&env, sizeof(args) / sizeof(args[0]), args);
199                 jvm_inited = 1;
200                 old_int_handler  = signal(SIGINT,  sig_jvm_destroy_at_exit);
201                 old_abrt_handler = signal(SIGABRT, sig_jvm_destroy_at_exit);
202                 atexit(jvm_destroy_at_exit);
203         }
204
205         return &env;
206 }
207
208 static void check(jni_env_t *env, const char *file, int line)
209 {
210         JNIEnv *jni = env->jni;
211         jboolean exc = (*jni)->ExceptionCheck(jni);
212         if(exc) {
213                 fprintf(stderr, "%s:%d: ", file, line);
214                 (*jni)->ExceptionDescribe(jni);
215                 (*jni)->ExceptionClear(jni);
216                 stop_vm(env);
217                 exit(1);
218         }
219 }
220
221 #define CHECK(env) check(env, __FILE__, __LINE__)
222
223 enum {
224         mth_add_int_edge,
225         mth_add_aff_edge,
226         mth_set_color,
227         mth_get_color,
228         mth_forbid_color,
229         mth_coalesce,
230         mth_dump,
231         mth_finish,
232         mth_last
233 };
234
235 struct _mth_info_t {
236         const char *name;
237         const char *sig;
238 };
239
240 static const struct _mth_info_t mthis[mth_last] = {
241         { "addIntEdge",  "(II)V"                   }, /* public void addIntEdge(int, int); */
242         { "addAffEdge",  "(III)V"                  }, /* public void addAffEdge(int, int, int); */
243         { "setColor",    "(II)V"                   }, /* public void setColor(int, int); */
244         { "getColor",    "(I)I"                    }, /* public int getColor(int); */
245         { "forbidColor", "(II)V"                   }, /* public void forbidColor(int, int); */
246         { "coalesce",    "()V"                     }, /* public void coalesce(); */
247         { "dump",        "(Ljava/lang/String;)V"   }  /* public void dump(String); */
248         { "finish",      "()V"                     }  /* public void finish(); */
249 };
250
251 /* public static coalescing.Extern createExtern(java.lang.String, int, int, int); */
252 static const struct _mth_info_t mthi_factory = {
253         "createExtern", "(Ljava/lang/String;III)Lcoalescing/Extern;"
254 };
255
256 struct _java_coal_t {
257         jni_env_t *env;
258         jclass    cls;
259         jobject   obj;
260
261         jmethodID mth_ids[mth_last];
262 };
263
264 static void jc_call_void(java_coal_t *c, int mth_index, ...)
265 {
266         JNIEnv *jni   = c->env->jni;
267         jmethodID mid = c->mth_ids[mth_index];
268
269         va_list args;
270
271         va_start(args, mth_index);
272         (*jni)->CallVoidMethodV(jni, c->obj, mid, args);
273         CHECK(c->env);
274         va_end(args);
275 }
276
277 static int jc_call_int(java_coal_t *c, int mth_index, ...)
278 {
279         JNIEnv *jni   = c->env->jni;
280         jmethodID mid = c->mth_ids[mth_index];
281
282         int res;
283         va_list args;
284
285         va_start(args, mth_index);
286         res = (*jni)->CallIntMethodV(jni, c->obj, mid, args);
287         CHECK(c->env);
288         va_end(args);
289
290         return res;
291 }
292
293 java_coal_t *java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
294 {
295         java_coal_t *c;
296         jni_env_t *env = get_jvm();
297         JNIEnv *jni = env->jni;
298         jmethodID fact;
299         jclass cls;
300         jstring str;
301         int i;
302
303         c = malloc(sizeof(c[0]));
304         memset(c, 0, sizeof(c[0]));
305         c->env = env;
306
307         /* Find the class we are are looking for. */
308         cls = (*jni)->FindClass(jni, cls_name);
309         CHECK(env);
310
311         /* Get the static factory method. */
312         fact = (*jni)->GetStaticMethodID(jni, cls, mthi_factory.name, mthi_factory.sig);
313         CHECK(env);
314
315         /* Call the factory. */
316         str = (*jni)->NewStringUTF(jni, graph_name);
317         CHECK(env);
318         c->obj = (*jni)->CallStaticObjectMethod(jni, cls, fact, str, n_nodes, n_regs, dbg_level);
319         CHECK(env);
320         c->cls = (*jni)->GetObjectClass(jni, c->obj);
321
322         /* Reference the created object. */
323         c->obj = (*jni)->NewGlobalRef(jni, c->obj);
324         CHECK(env);
325
326         /* Lookup the member methods of the object. */
327         for(i = 0; i < mth_last; ++i) {
328                 c->mth_ids[i] = (*jni)->GetMethodID(jni, c->cls, mthis[i].name, mthis[i].sig);
329                 CHECK(env);
330         }
331
332         return c;
333 }
334
335 void java_coal_destroy(java_coal_t *c) {
336         JNIEnv *jni = c->env->jni;
337         jc_call_void(c, mth_finish);
338         (*jni)->DeleteGlobalRef(jni, c->obj);
339         free(c);
340 }
341
342 void java_coal_add_int_edge(java_coal_t *c, int n, int m)
343 {
344         jc_call_void(c, mth_add_int_edge, (jint) n, (jint) m);
345 }
346
347 void java_coal_add_aff_edge(java_coal_t *c, int n, int m, int weight)
348 {
349         jc_call_void(c, mth_add_aff_edge, (jint) n, (jint) m, (jint) weight);
350 }
351
352 void java_coal_set_color(java_coal_t *c, int n, int col)
353 {
354         jc_call_void(c, mth_set_color, (jint) n, (jint) col);
355 }
356
357 void java_coal_forbid_color(java_coal_t *c, int n, int col)
358 {
359         jc_call_void(c, mth_forbid_color, (jint) n, (jint) col);
360 }
361
362 void java_coal_coalesce(java_coal_t *c)
363 {
364         jc_call_void(c, mth_coalesce);
365 }
366
367 void java_coal_dump(java_coal_t *c, const char *fn)
368 {
369         JNIEnv *jni   = c->env->jni;
370         jmethodID mid = c->mth_ids[mth_dump];
371         jstring str;
372
373         str = (*jni)->NewStringUTF(jni, fn);
374         CHECK(c->env);
375         (*jni)->CallVoidMethod(jni, c->obj, mid, str);
376         CHECK(c->env);
377 }
378
379 int java_coal_get_color(java_coal_t *c, int n)
380 {
381         return jc_call_int(c, mth_get_color, (jint) n);
382 }
383
384 #else
385
386 java_coal_t *java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
387 {
388         assert(0 && "use --enable-jvm");
389         return NULL;
390 }
391
392 void java_coal_destroy(java_coal_t *c)
393 {
394         assert(0 && "use --enable-jvm");
395 }
396
397
398 void java_coal_add_int_edge(java_coal_t *c, int n, int m)
399 {
400         assert(0 && "use --enable-jvm");
401 }
402
403 void java_coal_add_aff_edge(java_coal_t *c, int n, int m, int weight)
404 {
405         assert(0 && "use --enable-jvm");
406 }
407
408 void java_coal_set_color(java_coal_t *c, int n, int col)
409 {
410         assert(0 && "use --enable-jvm");
411 }
412
413 void java_coal_forbid_color(java_coal_t *c, int n, int col)
414 {
415         assert(0 && "use --enable-jvm");
416 }
417
418 void java_coal_coalesce(java_coal_t *c)
419 {
420         assert(0 && "use --enable-jvm");
421 }
422
423 void java_coal_dump(java_coal_t *c, const char *fn)
424 {
425         assert(0 && "use --enable-jvm");
426 }
427
428 int java_coal_get_color(java_coal_t *c, int n)
429 {
430         assert(0 && "use --enable-jvm");
431         return -1;
432 }
433
434
435 #endif /* WITH_JVM */