Give function a return type
[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
18 #include <signal.h>
19 #include <stdlib.h>
20 #include <assert.h>
21 #include <stdio.h>
22
23 #include "bejavacoal.h"
24
25 /* Path to the jar file. A little OS dependent convenience. */
26 #ifdef _WIN32
27 static char jar_file[512] = "y:\\user\\hack\\public\\coal.jar";
28 #else
29 static char jar_file[512] = "/ben/hack/public/coal.jar";
30 #endif
31
32 static char cls_name[256] = "coalescing/mst/safe/Algo";
33
34 /* Name of teh JVM dll/so */
35 static char jvm_lib[512] = { 0 };
36
37 #ifdef WITH_LIBCORE
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 java_coal_register_options(lc_opt_entry_t *grp)
46 {
47         lc_opt_entry_t *jc_grp = lc_opt_get_grp(grp, "jc");
48         lc_opt_add_table(jc_grp, options);
49 }
50 #endif
51
52 #ifdef WITH_JVM
53 #include <jni.h>
54
55 typedef struct _jni_env_t {
56         JavaVM *jvm;
57         JNIEnv *jni;
58 } jni_env_t;
59 #endif
60
61 /*
62
63         Ugly code to retrieve the JVM dll/so file.
64
65 */
66
67 #ifdef _WIN32
68 /* Win32 version */
69 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
70 {
71         HINSTANCE hVM = LoadLibrary(vmlibpath);
72         return hVM ? GetProcAddress(hVM, sym) : NULL;
73 }
74
75 #define JRE_KEY "SOFTWARE\\JavaSoft\\Java Development Kit"
76
77 static char *locate_jvm_lib(char *path, size_t path_len)
78 {
79         char version[32];
80         char buf[256];
81         DWORD version_len = sizeof(version);
82         DWORD dwPathLen = path_len;
83         HKEY hKey;
84
85         RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_QUERY_VALUE, &hKey);
86         RegQueryValueEx(hKey, "CurrentVersion", NULL, NULL, (LPBYTE) version, &version_len);
87         RegCloseKey(hKey);
88
89         _snprintf(buf, sizeof(buf), JRE_KEY "\\%s", version);
90         RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &hKey);
91         RegQueryValueEx(hKey, "JavaHome", NULL, NULL, (LPBYTE) path, &dwPathLen);
92         RegCloseKey(hKey);
93
94         strncat(path, "\\jre\\bin\\server\\jvm.dll", path_len);
95         return path;
96 }
97
98 #else
99 /* Unix version */
100 static void *find_jvm_symbol(const char *vmlibpath, const char *sym)
101 {
102         void *libVM = dlopen(vmlibpath, RTLD_LAZY);
103         return libVM ? dlsym(libVM, sym) : NULL;
104 }
105
106 static char *locate_jvm_lib(char *path, size_t n)
107 {
108         return NULL;
109 }
110 #endif
111
112 #ifdef WITH_JVM
113 static int start_vm(jni_env_t *env, int argc, char *argv[])
114 {
115         int i;
116         long ret;
117         JavaVMInitArgs args;
118         JavaVMOption *opts;
119
120         int result = 0;
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_ERR)
143                 return 0;
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                 start_vm(&env, sizeof(args) / sizeof(args[0]), args);
198                 jvm_inited = 1;
199                 old_int_handler  = signal(SIGINT,  sig_jvm_destroy_at_exit);
200                 old_abrt_handler = signal(SIGABRT, sig_jvm_destroy_at_exit);
201                 atexit(jvm_destroy_at_exit);
202         }
203
204         return &env;
205 }
206
207 static void check(jni_env_t *env, const char *file, int line)
208 {
209         JNIEnv *jni = env->jni;
210         jboolean exc = (*jni)->ExceptionCheck(jni);
211         if(exc) {
212                 fprintf(stderr, "%s:%d: ", file, line);
213                 (*jni)->ExceptionDescribe(jni);
214                 (*jni)->ExceptionClear(jni);
215                 stop_vm(env);
216                 exit(1);
217         }
218 }
219
220 #define CHECK(env) check(env, __FILE__, __LINE__)
221
222 enum {
223         mth_add_int_edge,
224         mth_add_aff_edge,
225         mth_set_color,
226         mth_get_color,
227         mth_forbid_color,
228         mth_coalesce,
229         mth_dump,
230         mth_last
231 };
232
233 struct _mth_info_t {
234         const char *name;
235         const char *sig;
236 };
237
238 static const struct _mth_info_t mthis[mth_last] = {
239         { "addIntEdge",  "(II)V"                   }, /* public void addIntEdge(int, int); */
240         { "addAffEdge",  "(III)V"                  }, /* public void addAffEdge(int, int, int); */
241         { "setColor",    "(II)V"                   }, /* public void setColor(int, int); */
242         { "getColor",    "(I)I"                    }, /* public int getColor(int); */
243         { "forbidColor", "(II)V"                   }, /* public void forbidColor(int, int); */
244         { "coalesce",    "()V"                     }, /* public void coalesce(); */
245         { "dump",        "(Ljava/lang/String;)V"   }  /* public void dump(String); */
246 };
247
248 /* public static coalescing.Extern createExtern(java.lang.String, int, int, int); */
249 static const struct _mth_info_t mthi_factory = {
250         "createExtern", "(Ljava/lang/String;III)Lcoalescing/Extern;"
251 };
252
253 struct _java_coal_t {
254         jni_env_t *env;
255         jclass    cls;
256         jobject   obj;
257
258         jmethodID mth_ids[mth_last];
259 };
260
261 static void jc_call_void(java_coal_t *c, int mth_index, ...)
262 {
263         JNIEnv *jni   = c->env->jni;
264         jmethodID mid = c->mth_ids[mth_index];
265
266         va_list args;
267
268         va_start(args, mth_index);
269         (*jni)->CallVoidMethodV(jni, c->obj, mid, args);
270         CHECK(c->env);
271         va_end(args);
272 }
273
274 static int jc_call_int(java_coal_t *c, int mth_index, ...)
275 {
276         JNIEnv *jni   = c->env->jni;
277         jmethodID mid = c->mth_ids[mth_index];
278
279         int res;
280         va_list args;
281
282         va_start(args, mth_index);
283         res = (*jni)->CallIntMethodV(jni, c->obj, mid, args);
284         CHECK(c->env);
285         va_end(args);
286
287         return res;
288 }
289
290 java_coal_t *java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
291 {
292         java_coal_t *c;
293         jni_env_t *env = get_jvm();
294         JNIEnv *jni = env->jni;
295         jmethodID fact;
296         jclass cls;
297         jstring str;
298         int i;
299
300         c = malloc(sizeof(c[0]));
301         memset(c, 0, sizeof(c[0]));
302         c->env = env;
303
304         /* Find the class we are are looking for. */
305         cls = (*jni)->FindClass(jni, cls_name);
306         CHECK(env);
307
308         /* Get the static factory method. */
309         fact = (*jni)->GetStaticMethodID(jni, cls, mthi_factory.name, mthi_factory.sig);
310         CHECK(env);
311
312         /* Call the factory. */
313         str = (*jni)->NewStringUTF(jni, graph_name);
314         CHECK(env);
315         c->obj = (*jni)->CallStaticObjectMethod(jni, cls, fact, str, n_nodes, n_regs, dbg_level);
316         CHECK(env);
317         c->cls = (*jni)->GetObjectClass(jni, c->obj);
318
319         /* Reference the created object. */
320         c->obj = (*jni)->NewGlobalRef(jni, c->obj);
321         CHECK(env);
322
323         /* Lookup the member methods of the object. */
324         for(i = 0; i < mth_last; ++i) {
325                 c->mth_ids[i] = (*jni)->GetMethodID(jni, c->cls, mthis[i].name, mthis[i].sig);
326                 CHECK(env);
327         }
328
329         return c;
330 }
331
332 void java_coal_destroy(java_coal_t *c) {
333         JNIEnv *jni = c->env->jni;
334         (*jni)->DeleteGlobalRef(jni, c->obj);
335         free(c);
336 }
337
338 void java_coal_add_int_edge(java_coal_t *c, int n, int m)
339 {
340         jc_call_void(c, mth_add_int_edge, (jint) n, (jint) m);
341 }
342
343 void java_coal_add_aff_edge(java_coal_t *c, int n, int m, int weight)
344 {
345         jc_call_void(c, mth_add_aff_edge, (jint) n, (jint) m, (jint) weight);
346 }
347
348 void java_coal_set_color(java_coal_t *c, int n, int col)
349 {
350         jc_call_void(c, mth_set_color, (jint) n, (jint) col);
351 }
352
353 void java_coal_forbid_color(java_coal_t *c, int n, int col)
354 {
355         jc_call_void(c, mth_forbid_color, (jint) n, (jint) col);
356 }
357
358 void java_coal_coalesce(java_coal_t *c)
359 {
360         jc_call_void(c, mth_coalesce);
361 }
362
363 void java_coal_dump(java_coal_t *c, const char *fn)
364 {
365         JNIEnv *jni   = c->env->jni;
366         jmethodID mid = c->mth_ids[mth_dump];
367         jstring str;
368
369         str = (*jni)->NewStringUTF(jni, fn);
370         CHECK(c->env);
371         (*jni)->CallVoidMethod(jni, c->obj, mid, str);
372         CHECK(c->env);
373 }
374
375 int java_coal_get_color(java_coal_t *c, int n)
376 {
377         return jc_call_int(c, mth_get_color, (jint) n);
378 }
379
380 #else
381
382 java_coal_t *java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
383 {
384         assert(0 && "use --enable-jvm");
385         return NULL;
386 }
387
388 void java_coal_destroy(java_coal_t *c)
389 {
390         assert(0 && "use --enable-jvm");
391 }
392
393
394 void java_coal_add_int_edge(java_coal_t *c, int n, int m)
395 {
396         assert(0 && "use --enable-jvm");
397 }
398
399 void java_coal_add_aff_edge(java_coal_t *c, int n, int m, int weight)
400 {
401         assert(0 && "use --enable-jvm");
402 }
403
404 void java_coal_set_color(java_coal_t *c, int n, int col)
405 {
406         assert(0 && "use --enable-jvm");
407 }
408
409 void java_coal_forbid_color(java_coal_t *c, int n, int col)
410 {
411         assert(0 && "use --enable-jvm");
412 }
413
414 void java_coal_coalesce(java_coal_t *c)
415 {
416         assert(0 && "use --enable-jvm");
417 }
418
419 void java_coal_dump(java_coal_t *c, const char *fn)
420 {
421         assert(0 && "use --enable-jvm");
422 }
423
424 int java_coal_get_color(java_coal_t *c, int n)
425 {
426         assert(0 && "use --enable-jvm");
427         return -1;
428 }
429
430
431 #endif /* WITH_JVM */