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