DEADSOFTWARE

JVM: Реализованы переменные-процедуры в генераторе
authorDeaDDooMER <deaddoomer@deadsoftware.ru>
Sat, 29 Jul 2017 18:57:52 +0000 (21:57 +0300)
committerDeaDDooMER <deaddoomer@deadsoftware.ru>
Sat, 29 Jul 2017 18:57:52 +0000 (21:57 +0300)
JTest.java
src/backends/jvm/generator-jvm-basic.c
src/backends/jvm/generator-jvm-basic.h
src/backends/jvm/generator-jvm.c
src/test.c

index 2cf5ff94dc9dbe7b5cd163370d20adbad9436442..0e621c810eeccced5045ef3bfccff3394b6b83f0 100644 (file)
@@ -1,12 +1,8 @@
 class JTest
 {
-       static class REC {}
+       public static JTest pointer;
 
-       public JTest()
        {
-               REC[][] ccooll = new REC[16][32];
-               for(int i = 0; i < ccooll.length; i++)
-                       for(int j = 0; j < ccooll[i].length; j++)
-                               ccooll[i][j] = new REC();
+               pointer = this;
        }
 }
index d092f8e799aa1d38af2f368cb0d5840026fb1329..865ffdd4177f7625fb1f5fdef57b2f3ba6c0cb39 100644 (file)
@@ -205,6 +205,27 @@ jvm_get_field_full_name(oberon_object_t * x)
        return NULL;
 }
 
+char *
+jvm_get_field_full_name_safe(oberon_object_t * x)
+{
+       switch(x -> class)
+       {
+               case OBERON_CLASS_VAR:
+               case OBERON_CLASS_PROC:
+                       return new_string("%s$%s", x -> module -> name, x -> name);
+               case OBERON_CLASS_FIELD:;
+                       char * rec_name = jvm_get_class_full_name(x -> parent_type);
+                       return new_string("%s$%s", rec_name, x -> name);
+               case OBERON_CLASS_MODULE:
+                       return new_string(x -> module -> name);
+               default:
+                       gen_error("jvm_get_field_full_name: wat");
+                       break;
+       }
+
+       return NULL;
+}
+
 char *
 jvm_get_class_full_name(oberon_type_t * type)
 {
index 2d4312f28278c9c20e6386e2114638364ed3e6d2..121c89b7817147679feb41faa7330c970c3c16ba 100644 (file)
@@ -23,6 +23,9 @@ jvm_get_postfix(oberon_type_t * type);
 char *
 jvm_get_field_full_name(oberon_object_t * x);
 
+char *
+jvm_get_field_full_name_safe(oberon_object_t * x);
+
 char *
 jvm_get_class_full_name(oberon_type_t * type);
 
index dc79ea11cd0cbb656036a37d67d9a512c46517ae..f0768d8b69d5d9fda94829c343d116d44a2b0e7f 100644 (file)
@@ -610,6 +610,14 @@ jvm_generate_copy_array(gen_proc_t * p, oberon_type_t * arr)
        }
 }
 
+static void
+jvm_generate_push_procedure_pointer(gen_proc_t * p, oberon_object_t * proc)
+{
+       char * full_name = jvm_get_field_full_name_safe(proc);
+       char * desc = jvm_get_descriptor(proc -> type);
+       jvm_generate(p, 0, 1, "getstatic %s/pointer %s", full_name, desc);
+}
+
 // ==========================================
 // ==========================================
 // ==========================================
@@ -644,7 +652,7 @@ oberon_generate_procedure_class(oberon_type_t * proc)
        fprintf(fp, ".class  public abstract %s\n", cname);
        fprintf(fp, ".super  java/lang/Object\n\n");
 
-       fprintf(fp, ".method public <init>()V\n");
+       fprintf(fp, ".method <init>()V\n");
        fprintf(fp, "  aload_0\n");
        fprintf(fp, "  invokespecial java/lang/Object/<init>()V\n");
        fprintf(fp, "  return\n");
@@ -658,6 +666,85 @@ oberon_generate_procedure_class(oberon_type_t * proc)
        jvm_destroy_class(class);
 }
 
+static void
+oberon_generate_procedure_pointer_class(oberon_object_t * proc)
+{
+       FILE * fp;
+       char * cname;
+       char * abscname;
+       char * absdesc;
+       char * signature;
+       struct gen_class * class;
+
+       cname = jvm_get_field_full_name_safe(proc);
+       class = jvm_create_class(cname);
+       abscname = jvm_get_class_full_name(proc -> type);
+       absdesc = jvm_get_descriptor(proc -> type);
+       fp = class -> fp;
+
+       fprintf(fp, ".source %s\n", proc -> module -> name);
+       fprintf(fp, ".class  public %s\n", cname);
+       fprintf(fp, ".super  %s\n\n", abscname);
+
+       fprintf(fp, ".field public static pointer %s\n\n", absdesc);
+
+       fprintf(fp, ".method <init>()V\n");
+       fprintf(fp, "  aload_0\n");
+       fprintf(fp, "  invokespecial %s/<init>()V\n", abscname);
+       fprintf(fp, "  aload_0\n");
+       fprintf(fp, "  putstatic %s/pointer %s\n", cname, absdesc);
+       fprintf(fp, "  return\n");
+       fprintf(fp, ".end method\n\n");
+
+       signature = jvm_get_procedure_signature(proc -> type);
+
+       gen_proc_t * p;
+       gen_var_t * this_v;
+
+       p = jvm_create_proc(class);
+       jvm_generate_function_header(p, "public", "invoke", signature);
+
+       this_v = oberon_generator_new_var();
+       jvm_generate_and_init_local_var(this_v, p, false);
+
+       gen_var_t * v;
+       int use_size = 0;
+       int num = proc -> type -> num_decl;
+       oberon_object_t * arg = proc -> type -> decl;
+       for(int i = 0; i < num; i++)
+       {
+               v = oberon_generator_new_var();
+               bool wide = jvm_is_wide_type(arg -> type);
+               char * desc = jvm_get_descriptor(arg -> type);
+               jvm_generate_and_init_named_local_var(v, p, wide, arg -> name, desc);
+
+               jvm_generate_load(p, arg -> type, v);
+
+               use_size += (wide) ? (2) : (1);
+
+               arg = arg -> next;
+       }
+
+       char * full_name = jvm_get_field_full_name(proc);
+       int cell_size = jvm_cell_size_for_type(proc -> type -> base);
+
+       jvm_generate(p, use_size, cell_size, "invokestatic %s%s", full_name, signature);
+
+       if(proc -> type -> base -> class == OBERON_TYPE_VOID)
+       {
+               jvm_generate(p, 0, 0, "return");
+       }
+       else
+       {
+               char prefix = jvm_get_prefix(proc -> type -> base);
+               jvm_generate(p, cell_size, 0, "%creturn", prefix);
+       }
+
+       jvm_generate_function_end(p);
+
+       jvm_destroy_class(class);
+}
+
 static void
 oberon_generate_record_class(gen_module_t * m, oberon_type_t * rec)
 {
@@ -846,6 +933,8 @@ oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * proc)
        {
                gen_error("generator: local procedures not implemented");
        }
+
+       oberon_generate_procedure_pointer_class(proc);
 }
 
 void
@@ -900,6 +989,13 @@ oberon_generate_begin_module(oberon_context_t * ctx)
                                jvm_generate_store(p, x -> type, x -> gen_var);
                        }
                }
+               else if(x -> class == OBERON_CLASS_PROC)
+               {
+                       char * cname = jvm_get_field_full_name_safe(x);
+                       jvm_generate(p, 0, 1, "new %s", cname);
+                       jvm_generate(p, 1, 0, "invokespecial %s/<init>()V", cname);
+               }
+
                x = x -> next;
        }
 }
@@ -1058,6 +1154,12 @@ jvm_generate_call_proc(gen_proc_t * p, oberon_expr_t * desig)
        fullname = jvm_get_field_full_name(proc);
        signature = jvm_get_procedure_signature(proc -> type);
 
+       if(proc -> class != OBERON_CLASS_PROC)
+       {
+               /* Загружаем указатель на процедуру */
+               jvm_generate_load(p, proc -> type, proc -> gen_var);
+       }
+
        int args_cells = 0;
        int result_cells = jvm_cell_size_for_type(proc -> type -> base);
 
@@ -1070,7 +1172,17 @@ jvm_generate_call_proc(gen_proc_t * p, oberon_expr_t * desig)
                arg = arg -> next;
        }
 
-       jvm_generate(p, args_cells, result_cells, "invokestatic %s%s", fullname, signature);
+       if(proc -> class == OBERON_CLASS_PROC)
+       {
+               /* Обычная статическая процедура */
+               jvm_generate(p, args_cells, result_cells, "invokestatic %s%s", fullname, signature);            
+       }
+       else
+       {
+               /* Процедура-переменная */
+               char * class = jvm_get_class_full_name(proc -> type);
+               jvm_generate(p, 1 + args_cells, result_cells, "invokevirtual %s/invoke%s", class, signature);
+       }
 }
 
 void
@@ -1123,7 +1235,14 @@ push_item(gen_proc_t * p, oberon_item_t * item)
        switch(item -> mode)
        {
                case MODE_VAR:
-                       jvm_generate_load(p, item -> result, item -> var -> gen_var);
+                       if(item -> var -> class == OBERON_CLASS_PROC)
+                       {
+                               jvm_generate_push_procedure_pointer(p, item -> var);
+                       }
+                       else
+                       {
+                               jvm_generate_load(p, item -> result, item -> var -> gen_var);
+                       }
                        break;
                case MODE_INTEGER:
                        jvm_generate_push_int(p, item -> integer);
index 2b9fd062da0926c61a72b7d0842b9fd02f1eebf0..18417ba34fa6241d16ae44c53733960a57e2be91 100644 (file)
@@ -8,26 +8,27 @@ static char source_test[] =
        "(* Main module *)"
        "MODULE Test;"
        "TYPE"
-       "  Arr = ARRAY 32, 768 OF INTEGER;"
-       "  RecDesc = RECORD x, y, z : INTEGER; END;"
+       "  P = PROCEDURE;"
+       "  F = PROCEDURE (x : INTEGER) : INTEGER;"
        ""
        "VAR"
-       "  z : Arr;"
-       "  r : RecDesc;"
+       "  p : P;"
+       "  f : F;"
+       "  i : INTEGER;"
        ""
-       "PROCEDURE TestRecordCopy(rrr : RecDesc);"
-       "END TestRecordCopy;"
+       "PROCEDURE Pow(x : INTEGER) : INTEGER;"
+       "BEGIN"
+       "  RETURN x * x;"
+       "END Pow;"
        ""
-       "PROCEDURE TestArrayCopy(aaa : Arr);"
-       "END TestArrayCopy;"
-       ""
-       "PROCEDURE TestOpenArrayCopy(ppp : ARRAY OF ARRAY OF INTEGER);"
-       "END TestOpenArrayCopy;"
+       "PROCEDURE Do;"
+       "END Do;"
        ""
        "BEGIN;"
-       "  TestRecordCopy(r);"
-       "  TestArrayCopy(z);"
-       "  TestOpenArrayCopy(z);"
+       "  p := Do;"
+       "  f := Pow;"
+       "  i := f(7);"
+       "  p;"
        "END Test."
 ;