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