7 #include <libcore/lc_opts.h>
8 #include <libcore/lc_opts_enum.h>
9 #endif /* WITH_LIBCORE */
15 #include "bejavacoal.h"
17 static char jar_file[256] = "/ben/hack/public/coal.jar";
18 static char cls_name[256] = "coalescing/mst/safe/Algo";
21 static const lc_opt_table_entry_t options[] = {
22 LC_OPT_ENT_STR ("jar", "jar file of the coalescer", jar_file, sizeof(jar_file)),
23 LC_OPT_ENT_STR ("cls", "name of the class providing the factory", cls_name, sizeof(cls_name)),
27 void java_coal_register_options(lc_opt_entry_t *grp)
29 lc_opt_entry_t *jc_grp = lc_opt_get_grp(grp, "jc");
30 lc_opt_add_table(jc_grp, options);
34 typedef struct _jni_env_t {
39 static int start_vm(jni_env_t *env, int argc, char *argv[])
46 memset(&args, 0, sizeof(args));
47 opts = malloc(argc * sizeof(opts[0]));
48 for(i = 0; i < argc; ++i) {
49 opts[i].optionString = argv[i];
50 opts[i].extraInfo = NULL;
53 args.version = JNI_VERSION_1_4;
56 args.ignoreUnrecognized = JNI_FALSE;
58 ret = JNI_CreateJavaVM(&env->jvm, (void **) &env->jni, &args);
66 static void stop_vm(jni_env_t *env)
68 JavaVM *jvm = env->jvm;
69 (*jvm)->DetachCurrentThread(jvm);
70 (*jvm)->DestroyJavaVM(jvm);
75 static int jvm_inited = 0;
78 static void jvm_destroy_at_exit(void)
84 static jni_env_t *get_jvm(void)
91 snprintf(cp_param, sizeof(cp_param), "-Djava.class.path=%s", jar_file);
93 start_vm(&env, sizeof(args) / sizeof(args[0]), args);
95 atexit(jvm_destroy_at_exit);
101 static void check(jni_env_t *env, const char *file, int line)
103 JNIEnv *jni = env->jni;
104 jboolean exc = (*jni)->ExceptionCheck(jni);
106 fprintf(stderr, "%s:%d: ", file, line);
107 (*jni)->ExceptionDescribe(jni);
108 (*jni)->ExceptionClear(jni);
114 #define CHECK(env) check(env, __FILE__, __LINE__)
118 typedef struct _jni_env_t jni_env_t;
137 static const struct _mth_info_t mthis[mth_last] = {
138 { "addIntEdge", "(II)V" }, /* public void addIntEdge(int, int); */
139 { "addAffEdge", "(III)V" }, /* public void addAffEdge(int, int, int); */
140 { "setColor", "(II)V" }, /* public void setColor(int, int); */
141 { "forbidColor", "(II)V" }, /* public void forbidColor(int, int); */
142 { "getColor", "(I)I" }, /* public int getColor(int); */
143 { "coalesce", "()V" }, /* public void coalesce(); */
144 { "dump", "(Ljava/lang/String;)V" } /* public void dump(String); */
147 /* public static coalescing.Extern createExtern(java.lang.String, int, int, int); */
148 static const struct _mth_info_t mthi_factory = {
149 "createExtern", "(Ljava/lang/String;III)Lcoalescing/Extern;"
152 struct _java_coal_t {
157 jmethodID mth_ids[mth_last];
160 typedef struct _java_coal_t java_coal_t;
162 static void jc_call_void(java_coal_t *c, int mth_index, ...)
164 JNIEnv *jni = c->env->jni;
165 jmethodID mid = c->mth_ids[mth_index];
169 va_start(args, mth_index);
170 (*jni)->CallVoidMethodV(jni, c->obj, mid, args);
175 static int jc_call_int(java_coal_t *c, int mth_index, ...)
177 JNIEnv *jni = c->env->jni;
178 jmethodID mid = c->mth_ids[mth_index];
183 va_start(args, mth_index);
184 res = (*jni)->CallIntMethodV(jni, c->obj, mid, args);
191 java_coal_t *java_coal_init(const char *graph_name, int n_nodes, int n_regs, int dbg_level)
194 jni_env_t *env = get_jvm();
195 JNIEnv *jni = env->jni;
201 c = malloc(sizeof(c[0]));
202 memset(c, 0, sizeof(c[0]));
205 /* Find the class we are are looking for. */
206 cls = (*jni)->FindClass(jni, cls_name);
209 /* Get the static factory method. */
210 fact = (*jni)->GetStaticMethodID(jni, cls, mthi_factory.name, mthi_factory.sig);
213 /* Call the factory. */
214 str = (*jni)->NewStringUTF(jni, graph_name);
216 c->obj = (*jni)->CallStaticObjectMethod(jni, cls, fact, str, n_nodes, n_regs, dbg_level);
218 c->cls = (*jni)->GetObjectClass(jni, c->obj);
220 /* Reference the created object. */
221 c->obj = (*jni)->NewGlobalRef(jni, c->obj);
224 /* Lookup the member methods of the object. */
225 for(i = 0; i < mth_last; ++i) {
226 c->mth_ids[i] = (*jni)->GetMethodID(jni, c->cls, mthis[i].name, mthis[i].sig);
233 void java_coal_destroy(java_coal_t *c) {
234 JNIEnv *jni = c->env->jni;
235 (*jni)->DeleteGlobalRef(jni, c->obj);
239 void java_coal_add_int_edge(java_coal_t *c, int n, int m)
241 jc_call_void(c, mth_add_int_edge, (jint) n, (jint) m);
244 void java_coal_add_aff_edge(java_coal_t *c, int n, int m, int weight)
246 jc_call_void(c, mth_add_aff_edge, (jint) n, (jint) m, (jint) weight);
249 void java_coal_set_color(java_coal_t *c, int n, int col)
251 jc_call_void(c, mth_set_color, (jint) n, (jint) col);
254 void java_coal_forbid_color(java_coal_t *c, int n, int col)
256 jc_call_void(c, mth_forbid_color, (jint) n, (jint) col);
259 void java_coal_coalesce(java_coal_t *c)
261 jc_call_void(c, mth_coalesce);
264 void java_coal_dump(java_coal_t *c, const char *fn)
266 JNIEnv *jni = c->env->jni;
267 jmethodID mid = c->mth_ids[mth_dump];
270 str = (*jni)->NewStringUTF(jni, fn);
272 (*jni)->CallVoidMethod(jni, c->obj, mid, str);
276 int java_coal_get_color(java_coal_t *c, int n)
278 return jc_call_int(c, mth_get_color, (jint) n);