DEADSOFTWARE

JVM: Можно создавать динамические массивы
authorDeaDDooMER <deaddoomer@deadsoftware.ru>
Fri, 28 Jul 2017 18:22:25 +0000 (21:22 +0300)
committerDeaDDooMER <deaddoomer@deadsoftware.ru>
Fri, 28 Jul 2017 18:22:25 +0000 (21:22 +0300)
src/backends/jvm/generator-jvm.c
src/test.c

index 9408eca32328248fd37b8454c30b99d663ad7ebd..cbc834e58d5cf919926aae41423023dc4e259013 100644 (file)
@@ -256,7 +256,7 @@ jvm_generate_label(gen_proc_t * p, int label_id)
 }
 
 static void
-jvm_generate_new_static(gen_proc_t * p, oberon_type_t * type);
+jvm_generate_new(gen_proc_t * p, oberon_type_t * type, int num);
 
 /*
  * Функция jvm_generate_static_array_initialization генерирует код для
@@ -322,7 +322,7 @@ jvm_generate_static_array_initialization(gen_proc_t * p, oberon_type_t * type)
        {
                jvm_generate(p, 0, 1, "aload %i", reg_a);
                jvm_generate(p, 0, 1, "iload %i", reg_i);
-               jvm_generate_new_static(p, type -> base);
+               jvm_generate_new(p, type -> base, 0);
                jvm_generate(p, 3, 0, "aastore");
        }
        else
@@ -344,7 +344,7 @@ jvm_generate_static_array_initialization(gen_proc_t * p, oberon_type_t * type)
 }
 
 static void
-jvm_generate_new_static(gen_proc_t * p, oberon_type_t * type)
+jvm_generate_new(gen_proc_t * p, oberon_type_t * type, int num)
 {
        int dim;
        char * cname;
@@ -372,10 +372,21 @@ jvm_generate_new_static(gen_proc_t * p, oberon_type_t * type)
                        desc = jvm_get_descriptor(type);
                        while(base -> class == OBERON_TYPE_ARRAY)
                        {
+                               if(num > 0)
+                               {
+                                       assert(base -> size == 0);
+                                       num -= 1;
+                               }
+                               else
+                               {
+                                       assert(base -> size > 0);
+                                       jvm_generate_push_int(p, base -> size);
+                               }
                                dim += 1;
-                               jvm_generate_push_int(p, base -> size);
                                base = base -> base;
                        }
+
+                       assert(num == 0);
                        jvm_generate(p, dim, 1, "multianewarray %s %i", desc, dim);
 
                        if(base -> class == OBERON_TYPE_RECORD)
@@ -547,7 +558,7 @@ oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * rec)
                        || field -> type -> class == OBERON_TYPE_ARRAY)
                {
                        jvm_generate(p, 0, 1, "aload_0");
-                       jvm_generate_new_static(p, field -> type);
+                       jvm_generate_new(p, field -> type, 0);
                        jvm_generate_store(p, field -> type, field -> gen_var);
                }
                field = field -> next;
@@ -660,7 +671,7 @@ oberon_generate_begin_module(oberon_context_t * ctx)
                        if(x -> type -> class == OBERON_TYPE_ARRAY
                                || x -> type -> class == OBERON_TYPE_RECORD)
                        {
-                               jvm_generate_new_static(p, x -> type);
+                               jvm_generate_new(p, x -> type, 0);
                                jvm_generate_store(p, x -> type, x -> gen_var);
                        }
                }
@@ -817,18 +828,18 @@ jvm_push_var(gen_proc_t * p, oberon_object_t * var)
        }
 }
 
-static void
-jvm_generate_expr_new_static(gen_proc_t * p, oberon_type_t * type, int num, oberon_expr_t * arg)
-{
-       assert(num == 0);
-       jvm_generate_new_static(p, type);
-}
-
 static void
 jvm_generate_expr_new_pointer(gen_proc_t * p, oberon_type_t * type, int num, oberon_expr_t * arg)
 {
        assert(type -> class == OBERON_TYPE_POINTER);
-       jvm_generate_expr_new_static(p, type -> base, num, arg);
+
+       for(int i = 0; i < num; i++)
+       {
+               push_expr(p, arg);
+               arg = arg -> next;
+       }
+
+       jvm_generate_new(p, type -> base, num);
 }
 
 static void
index 92223cdc7b49b4643f79f9edad013f341b9db6a4..51a1250523aebc88646d6add7368a3c5fe5f8f48 100644 (file)
@@ -7,10 +7,16 @@
 static char source_test[] =
        "(* Main module *)"
        "MODULE Test;"
+       "TYPE"
+       "  Rec = POINTER TO RecDesc;"
+       "  RecDesc = RECORD x : INTEGER; END;"
        "VAR"
        "  g : ARRAY 4 OF INTEGER;"
        "  r : RECORD x : INTEGER; END;"
+       "  a : POINTER TO ARRAY OF ARRAY OF RecDesc;"
        "BEGIN"
+       "  NEW(a, 10, 10);"
+       "  a[9, 9].x := 666;"
        "  g[3] := 4;"
        "  r.x := 4546;"
        "END Test."