DEADSOFTWARE

JVM: Добавлена инициализация модулей
[dsw-obn.git] / src / backends / jvm / generator-jvm.c
index 2456aa79cd9bafd5a6b4e71f26c5715ea250e9e8..bf5200bd786daafe7cff702b2731eb2b0552343a 100644 (file)
@@ -707,6 +707,8 @@ oberon_generator_init_module(oberon_context_t * ctx, oberon_module_t * mod)
        fprintf(class -> fp, ".class %s\n", mod -> name);
        fprintf(class -> fp, ".super java/lang/Object\n\n");
 
+       fprintf(class -> fp, ".field private static $INITIALIZED$ Z\n\n");
+
        m -> class = class;
 
        c -> current_m = m;
@@ -724,19 +726,33 @@ push_expr(gen_proc_t * p, oberon_expr_t * expr);
 void
 oberon_generate_begin_module(oberon_context_t * ctx)
 {
-       struct gen_class * class = ctx -> mod -> gen_mod -> class;
-       gen_proc_t * p = jvm_create_proc(class);
+       gen_proc_t * p;
+       int label_cont;
+       struct gen_class * class;
 
-       jvm_generate_function_header(p, "public", "<init>", "()V");
-       jvm_alloc_register_untyped(p -> rf, false);
-       jvm_generate(p, 0, 1, "aload_0");
-       jvm_generate(p, 1, 0, "invokespecial java/lang/Object/<init>()V");
+       class = ctx -> mod -> gen_mod -> class;
+
+       p = jvm_create_proc(class);
+       jvm_generate_function_header(p, "public static", "BEGIN", "()V");
+
+       label_cont = jvm_new_label_id(p);
+       jvm_generate(p, 0, 1, "getstatic %s/$INITIALIZED$ Z", class -> full_name);
+       jvm_generate(p, 1, 0, "ifeq L%i", label_cont);
+       jvm_generate(p, 0, 0, "return");
+       jvm_generate_label(p, label_cont);
+
+       jvm_generate(p, 0, 1, "iconst_1");
+       jvm_generate(p, 1, 0, "putstatic %s/$INITIALIZED$ Z", class -> full_name);
 
        /* Инициализация переменных объявленных в модуле */
        oberon_object_t * x = ctx -> mod -> decl -> list -> next;
        while(x != NULL)
        {
-               if(x -> class == OBERON_CLASS_VAR)
+               if(x -> class == OBERON_CLASS_MODULE)
+               {
+                       jvm_generate(p, 0, 0, "invokestatic %s/BEGIN()V", x -> module -> gen_mod -> class -> full_name);
+               }
+               else if(x -> class == OBERON_CLASS_VAR)
                {
                        jvm_generate_var_initialization(p, x -> gen_var);
                        if(x -> type -> class == OBERON_TYPE_ARRAY
@@ -760,6 +776,14 @@ oberon_generate_end_module(oberon_context_t * ctx)
 
        jvm_generate(p, 0, 0, "return");
        jvm_generate_function_end(class -> p);
+
+       p = jvm_create_proc(class);
+       jvm_generate_function_header(p, "private", "<init>", "()V");
+       jvm_alloc_register_untyped(p -> rf, false);
+       jvm_generate(p, 0, 1, "aload_0");
+       jvm_generate(p, 1, 0, "invokespecial java/lang/Object/<init>()V");
+       jvm_generate(p, 0, 0, "return");
+       jvm_generate_function_end(class -> p);
 }
 
 /*