DEADSOFTWARE

JVM: Добавлены фреймы (без передачи их локальным функциям)
authorDeaDDooMER <deaddoomer@deadsoftware.ru>
Tue, 8 Aug 2017 20:37:01 +0000 (23:37 +0300)
committerDeaDDooMER <deaddoomer@deadsoftware.ru>
Tue, 8 Aug 2017 20:37:01 +0000 (23:37 +0300)
src/backends/jvm/generator-jvm-abi.c
src/backends/jvm/generator-jvm-abi.h
src/backends/jvm/generator-jvm-asm.c
src/backends/jvm/generator-jvm-asm.h
src/backends/jvm/generator-jvm.c
src/backends/jvm/generator-jvm.h
src/test.c

index fa34f1dfd8190149b12569aca64b7bc29b767d64..0db2cc88b29bf3454d99b390ea5ff4bc151e0804 100644 (file)
  *   +++ всем переменным и полям в дескриптор добавляется "["
  *         Это позволит делать ссылки на переменные в VAR-параметрах.
  *   --- Каждая процедура снабжается параметром-фреймом.
- *       Это позволит реализовать локальные процедуры и средства рефлекции над стеком.
+ *         Это позволит реализовать локальные процедуры и средства рефлекции над стеком.
  */ 
 
 void
 jvm_generate_ldst_prepare(gen_proc_t * p, gen_var_t * v)
 {
-       char * desc;
-       char * full_name;
+       char * desc = v -> type -> desc;
+       char * full_name = v -> full_name;
        switch(v -> storage)
        {
                case JVM_STORAGE_REGISTER:
-                       break;
                case JVM_STORAGE_STATIC:
-                       full_name = v -> full_name;
-                       desc = v -> type -> desc;
+               case JVM_STORAGE_FIELD:
+               case JVM_STORAGE_PARAM:
+                       break;
+               case JVM_STORAGE_REGISTER_VAR:
+               case JVM_STORAGE_PARAM_VAR:
+                       jvm_generate(p, 0, 1, "aload %i", v -> reg);
+                       jvm_generate(p, 0, 1, "iconst_0");
+                       break;
+               case JVM_STORAGE_STATIC_VAR:
                        jvm_generate(p, 0, 1, "getstatic %s [%s", full_name, desc);
                        jvm_generate(p, 0, 1, "iconst_0");
                        break;
-               case JVM_STORAGE_FIELD:
-                       full_name = v -> full_name;
-                       desc = v -> type -> desc;
+               case JVM_STORAGE_FIELD_VAR:
                        jvm_generate(p, 1, 1, "getfield %s [%s", full_name, desc);
                        jvm_generate(p, 0, 1, "iconst_0");
                        break;
-               case JVM_STORAGE_LOCAL:
+               case JVM_STORAGE_PARAM_VARPTR:
                        jvm_generate(p, 0, 1, "aload %i", v -> reg);
+                       jvm_generate(p, 0, 1, "iload %i", v -> reg + 1);
+                       break;
+               case JVM_STORAGE_FRAME:
+               case JVM_STORAGE_FRAME_PARAM:
+                       jvm_generate_load(p, p -> frame_v);
+                       break;
+               case JVM_STORAGE_FRAME_VAR:
+               case JVM_STORAGE_FRAME_PARAM_VAR:
+                       jvm_generate_load(p, p -> frame_v);
+                       jvm_generate(p, 1, 1, "getfield %s [%s", full_name, desc);
                        jvm_generate(p, 0, 1, "iconst_0");
                        break;
-               case JVM_STORAGE_VARPTR:
-                       jvm_generate(p, 0, 1, "aload %i", v -> reg);
-                       jvm_generate(p, 0, 1, "iload %i", v -> reg + 1);
+               case JVM_STORAGE_FRAME_PARAM_VARPTR:
+                       jvm_generate_load(p, p -> frame_v);
+                       jvm_generate(p, 1, 1, "getfield %s [%s", full_name, desc);
+                       jvm_generate_load(p, p -> frame_v);
+                       jvm_generate(p, 1, 1, "getfield %s$offset I", full_name);
                        break;
                default:
                        gen_error("jvm_abi_obn_generate_ldst_prepare: wat %i", v -> storage);
@@ -68,19 +84,37 @@ jvm_generate_load(gen_proc_t * p, gen_var_t * src)
 {
        char prefix = src -> type -> prefix;
        int cell_size = src -> type -> cell_size;
+       char * full_name = src -> full_name;
+       char * desc = src -> type -> desc;
 
        switch(src -> storage)
        {
                case JVM_STORAGE_REGISTER:
+               case JVM_STORAGE_PARAM:
                        jvm_generate(p, 0, cell_size, "%cload %i", prefix, src -> reg);
                        break;
                case JVM_STORAGE_STATIC:
+                       jvm_generate(p, 0, cell_size, "getstatic %s %s", full_name, desc);
+                       break;
                case JVM_STORAGE_FIELD:
-               case JVM_STORAGE_LOCAL:
-               case JVM_STORAGE_VARPTR:
+                       jvm_generate(p, 0, cell_size, "getfield %s %s", full_name, desc);
+                       break;
+               case JVM_STORAGE_REGISTER_VAR:
+               case JVM_STORAGE_STATIC_VAR:
+               case JVM_STORAGE_FIELD_VAR:
+               case JVM_STORAGE_PARAM_VAR:
+               case JVM_STORAGE_PARAM_VARPTR:
+               case JVM_STORAGE_FRAME_VAR:
+               case JVM_STORAGE_FRAME_PARAM_VAR:
+               case JVM_STORAGE_FRAME_PARAM_VARPTR:
                        jvm_generate_ldst_prepare(p, src);
                        jvm_generate(p, 1 + 1, cell_size, "%caload", prefix);
                        break;
+               case JVM_STORAGE_FRAME:
+               case JVM_STORAGE_FRAME_PARAM:
+                       jvm_generate_ldst_prepare(p, src);
+                       jvm_generate(p, 1, cell_size, "getfield %s %s", full_name, desc);
+                       break;
                default:
                        gen_error("jvm_generate_load: unknow storage type %i", src -> storage);
                        break;
@@ -92,126 +126,389 @@ jvm_generate_store(gen_proc_t * p, gen_var_t * dst)
 {
        char prefix = dst -> type -> prefix;
        int cell_size = dst -> type -> cell_size;
+       char * full_name = dst -> full_name;
+       char * desc = dst -> type -> desc;
 
        switch(dst -> storage)
        {
                case JVM_STORAGE_REGISTER:
+               case JVM_STORAGE_PARAM:
                        jvm_generate(p, cell_size, 0, "%cstore %i", prefix, dst -> reg);
                        break;
                case JVM_STORAGE_STATIC:
+                       jvm_generate(p, 0, cell_size, "putstatic %s %s", full_name, desc);
+                       break;
                case JVM_STORAGE_FIELD:
-               case JVM_STORAGE_LOCAL:
-               case JVM_STORAGE_VARPTR:
+                       jvm_generate(p, 0, cell_size, "putfield %s %s", full_name, desc);
+                       break;
+               case JVM_STORAGE_REGISTER_VAR:
+               case JVM_STORAGE_STATIC_VAR:
+               case JVM_STORAGE_FIELD_VAR:
+               case JVM_STORAGE_PARAM_VAR:
+               case JVM_STORAGE_PARAM_VARPTR:
+               case JVM_STORAGE_FRAME_VAR:
+               case JVM_STORAGE_FRAME_PARAM_VAR:
+               case JVM_STORAGE_FRAME_PARAM_VARPTR:
                        jvm_generate(p, 1 + 1 + cell_size, 0, "%castore", prefix);
                        break;
+               case JVM_STORAGE_FRAME:
+               case JVM_STORAGE_FRAME_PARAM:
+                       jvm_generate(p, 0, cell_size, "putfield %s %s", full_name, desc);
+                       break;
                default:
                        gen_error("jvm_generate_store: unknow storage type %i", dst -> storage);
                        break;
        }
 }
 
-void
-jvm_generate_and_init_global_var(struct gen_class * class, gen_var_t * v, char * name, gen_type_t * t)
+static gen_var_t *
+jvm_new_var()
 {
-       assert(class -> p == NULL);
-        fprintf(class -> fp, ".field public static %s [%s\n\n", name, t -> desc);
-
-        v -> storage = JVM_STORAGE_STATIC;
-        v -> full_name = new_string("%s/%s", class -> full_name, name);
-       v -> type = t;
+       gen_var_t * v = GC_MALLOC(sizeof *v);
+       memset(v, 0, sizeof *v);
+       return v;
 }
 
-void
-jvm_generate_and_init_field(struct gen_class * class, gen_var_t * v, char * name, gen_type_t * t)
+gen_var_t *
+jvm_create_class_var(struct gen_class * class, enum gen_storage storage, char * name, gen_type_t * t)
 {
+       gen_var_t * v = jvm_new_var();
+       v -> storage = storage;
+       v -> name = new_string(name);
+       v -> type = t;
+       v -> class = class;
+
        assert(class -> p == NULL);
-        fprintf(class -> fp, ".field public %s [%s\n\n", name, t -> desc);
+       switch(storage)
+       {
+               case JVM_STORAGE_STATIC:
+               case JVM_STORAGE_FIELD:
+               case JVM_STORAGE_STATIC_VAR:
+               case JVM_STORAGE_FIELD_VAR:
+                       v -> full_name = new_string("%s/%s", class -> full_name, name);
+                       break;
+               default:
+                       gen_error("jvm_create_class_var: invalid storage %i", storage);
+                       break;
+       }
 
-        v -> storage = JVM_STORAGE_FIELD;
-        v -> full_name = new_string("%s/%s", class -> full_name, name);
-       v -> type = t;
+       return v;
 }
 
-void
-jvm_generate_and_init_local_var(gen_proc_t * p, gen_var_t * v, gen_type_t * t)
+gen_var_t *
+jvm_create_function_var(gen_proc_t * p, enum gen_storage storage, char * name, gen_type_t * t)
 {
-       v -> storage = JVM_STORAGE_REGISTER;
-       v -> reg = jvm_alloc_register_untyped(p -> rf, t -> wide);
+       gen_var_t * v = jvm_new_var();
+       v -> storage = storage;
+       v -> name = new_string(name);
        v -> type = t;
+       v -> p = p;
+
+       switch(storage)
+       {
+               case JVM_STORAGE_REGISTER:
+               case JVM_STORAGE_PARAM:
+               case JVM_STORAGE_PARAM_VAR:
+                       v -> reg = jvm_alloc_register_untyped(p -> rf, t -> wide);
+                       break;
+               case JVM_STORAGE_PARAM_VARPTR:
+                       /* Содержит пару массив+индекс */
+                       v -> reg = jvm_alloc_register_untyped(p -> rf, true);
+                       break;
+               case JVM_STORAGE_REGISTER_VAR:
+                       /* Содержит массив */
+                       v -> reg = jvm_alloc_register_untyped(p -> rf, false);
+                       break;
+               case JVM_STORAGE_FRAME:
+               case JVM_STORAGE_FRAME_VAR:
+                       v -> full_name = new_string("%s$FRAME/%s", p -> full_name, name);
+                       break;
+               case JVM_STORAGE_FRAME_PARAM:
+               case JVM_STORAGE_FRAME_PARAM_VAR:
+                       v -> reg = jvm_alloc_register_untyped(p -> rf, t -> wide);
+                       v -> full_name = new_string("%s$FRAME/%s", p -> full_name, name);
+                       break;
+               case JVM_STORAGE_FRAME_PARAM_VARPTR:
+                       /* Содержит пару массив+индекс */
+                       v -> reg = jvm_alloc_register_untyped(p -> rf, true);
+                       v -> full_name = new_string("%s$FRAME/%s", p -> full_name, name);
+                       break;
+               default:
+                       gen_error("jvm_create_class_var: invalid storage %i", storage);
+                       break;
+       }
+
+       return v;
 }
 
-void
-jvm_init_named_local_var(gen_proc_t * p, gen_var_t * v, gen_type_t * t)
+gen_var_t *
+jvm_create_function_temp_var(gen_proc_t * p, gen_type_t * t)
 {
+       gen_var_t * v = jvm_new_var();
        v -> storage = JVM_STORAGE_REGISTER;
        v -> reg = jvm_alloc_register_untyped(p -> rf, t -> wide);
+       v -> name = new_string("$TEMP_%i$", v -> reg);
        v -> type = t;
+       v -> p = p;
+       return v;
 }
 
 void
-jvm_init_param(gen_proc_t * p, gen_var_t * v, gen_type_t * t)
+jvm_generate_var(gen_var_t * v)
 {
-       v -> storage = JVM_STORAGE_LOCAL;
-       v -> reg = jvm_alloc_register_untyped(p -> rf, t -> wide);
-       v -> type = t;
+       enum gen_storage storage = v -> storage;
+       int reg = v -> reg;
+       char * name = v -> name;
+       char * desc = v -> type -> desc;
+       gen_proc_t * p = v -> p;
+
+       switch(v -> storage)
+       {
+               case JVM_STORAGE_REGISTER:
+               case JVM_STORAGE_PARAM:
+               case JVM_STORAGE_PARAM_VAR:
+                       jvm_generate(p, 0, 0, ".var %i is %s %s from start to end", reg, name, desc);
+                       break;
+               case JVM_STORAGE_REGISTER_VAR:
+                       jvm_generate(p, 0, 0, ".var %i is %s [%s from start to end", reg, name, desc);
+                       break;
+               case JVM_STORAGE_PARAM_VARPTR:
+                       jvm_generate(p, 0, 0, ".var %i is %s [%s from start to end", reg, name, desc);
+                       jvm_generate(p, 0, 0, ".var %i is %s$offset I from start to end", reg + 1, name);
+                       break;
+               case JVM_STORAGE_STATIC:
+                       fprintf(v -> class -> fp, ".field public static %s %s\n\n", name, desc);
+                       break;
+               case JVM_STORAGE_STATIC_VAR:
+                       fprintf(v -> class -> fp, ".field public static %s [%s\n\n", name, desc);
+                       break;
+               case JVM_STORAGE_FIELD:
+                       fprintf(v -> class -> fp, ".field public %s %s\n\n", name, desc);
+                       break;
+               case JVM_STORAGE_FIELD_VAR:
+                       fprintf(v -> class -> fp, ".field public %s [%s\n\n", name, desc);
+                       break;
+               case JVM_STORAGE_FRAME:
+               case JVM_STORAGE_FRAME_VAR:
+               case JVM_STORAGE_FRAME_PARAM:
+               case JVM_STORAGE_FRAME_PARAM_VAR:
+               case JVM_STORAGE_FRAME_PARAM_VARPTR:
+                       /* См. ниже jvm_generate_frame_class() */
+                       break;
+               default:
+                       gen_error("jvm_generate_var: invalid storage %i", storage);
+                       break;
+       }
 }
 
-void
-jvm_init_var_param(gen_proc_t * p, gen_var_t * v, gen_type_t * t)
+static void
+jvm_generate_param_initialization(gen_proc_t * p, gen_var_t * v)
 {
-       v -> storage = JVM_STORAGE_VARPTR;
-       v -> reg = jvm_alloc_register_untyped(p -> rf, true);
-       v -> type = t;
+       int old_reg = v -> reg;
+       int cell_size = v -> type -> cell_size;
+       char prefix = v -> type -> prefix;
+       char * name = v -> name;
+       char * desc = v -> type -> desc;
+
+       v -> reg = jvm_alloc_register_untyped(p -> rf, false);
+       jvm_generate(p, 0, 0, ".var %i is $%s [%s from start to end", v -> reg, name, desc);
+
+       jvm_generate(p, 0, 1, "iconst_1");
+       jvm_generate(p, 1, 1, "multianewarray [%s 1", desc);
+       jvm_generate(p, 1, 2, "dup");
+       jvm_generate(p, 1, 0, "astore %i", v -> reg);
+
+       jvm_generate(p, 0, 1, "iconst_0");
+       jvm_generate(p, 0, cell_size, "%cload %i", prefix, old_reg);
+       jvm_generate(p, 1 + 1 + cell_size, 0, "%castore", prefix);
 }
 
 void
-jvm_generate_variable_initialization(gen_proc_t * p, gen_var_t * v)
+jvm_generate_var_initialization(gen_proc_t * p, gen_var_t * v)
 {
-       if(v -> storage == JVM_STORAGE_STATIC)
+       if(v -> storage == JVM_STORAGE_REGISTER_VAR)
+       {
+               jvm_generate(p, 0, 1, "iconst_1");
+               jvm_generate(p, 1, 1, "multianewarray [%s 1", v -> type -> desc);
+               jvm_generate(p, 1, 0, "astore %i", v -> reg);
+       }
+       else if(v -> storage == JVM_STORAGE_STATIC_VAR)
        {
                jvm_generate(p, 0, 1, "iconst_1");
                jvm_generate(p, 1, 1, "multianewarray [%s 1", v -> type -> desc);
                jvm_generate(p, 1, 0, "putstatic %s [%s", v -> full_name, v -> type -> desc);
        }
-       else if(v -> storage == JVM_STORAGE_FIELD)
+       else if(v -> storage == JVM_STORAGE_FIELD_VAR)
        {
                jvm_generate(p, 0, 1, "iconst_1");
                jvm_generate(p, 1, 1, "multianewarray [%s 1", v -> type -> desc);
                jvm_generate(p, 1, 0, "putfield %s [%s", v -> full_name, v -> type -> desc);
        }
+       else if(v -> storage == JVM_STORAGE_PARAM_VAR)
+       {
+               jvm_generate_param_initialization(p, v);
+       }
+       else if(v -> storage == JVM_STORAGE_FRAME_PARAM
+               || v -> storage == JVM_STORAGE_FRAME_PARAM_VAR)
+       {
+               jvm_generate_ldst_prepare(p, v);
+               jvm_generate(p, 0, v -> type -> cell_size, "%cload %i", v -> type -> prefix, v -> reg);
+               jvm_generate_store(p, v);
+       }
+       else if(v -> storage == JVM_STORAGE_FRAME_PARAM_VARPTR)
+       {
+               jvm_generate_load(p, p -> frame_v);
+               jvm_generate(p, 0, 1, "aload %i", v -> reg);
+               jvm_generate(p, 1, 0, "putfield %s [%s", v -> full_name, v -> type -> desc);
+
+               jvm_generate_load(p, p -> frame_v);
+               jvm_generate(p, 0, 1, "iload %i", v -> reg + 1);
+               jvm_generate(p, 1, 0, "putfield %s$offset I", v -> full_name);
+       }
 }
 
-void
-jvm_generate_named_local_var_initialization(gen_proc_t * p, gen_var_t * v, char * name)
+static void
+jvm_generate_abstract_frame_class()
 {
-        jvm_generate(p, 0, 0, ".var %i is %s %s from start to end", v -> reg, name, v -> type -> desc);
+       FILE * fp;
+       char * cname;
+       struct gen_class * class;
+
+       cname = "SYSTEM$FRAME";
+       class = jvm_create_class(cname);
+       fp = class -> fp;
+
+       fprintf(fp, ".source SYSTEM\n");
+       fprintf(fp, ".class  public abstract %s\n", cname);
+       fprintf(fp, ".super  java/lang/Object\n\n");
+
+       fprintf(fp, ".field public $UP L%s;\n\n", cname);
+
+       fprintf(fp, ".method <init>()V\n");
+       fprintf(fp, "  aload_0\n");
+       fprintf(fp, "  invokespecial java/lang/Object/<init>()V\n");
+       fprintf(fp, "  return\n");
+       fprintf(fp, ".end method\n\n");
+
+       jvm_destroy_class(class);
+}
+
+static struct gen_class *
+jvm_generate_frame_class(oberon_object_t * proc)
+{
+       FILE * fp;
+       char * cname;
+       char * abscname;
+       struct gen_class * class;
+       oberon_object_t * var;
+
+       abscname = "SYSTEM$FRAME";
+       cname = jvm_get_field_full_name_safe(proc);
+       cname = new_string("%s$FRAME", cname);
+       class = jvm_create_class(cname);
+       fp = class -> fp;
+
+       class -> full_name = cname;
+
+       fprintf(fp, ".source %s\n", proc -> module -> name);
+       fprintf(fp, ".class  public %s\n", cname);
+       fprintf(fp, ".super  %s\n\n", abscname);
+
+       var = proc -> scope -> list -> next;
+       while(var)
+       {
+               if(var -> class == OBERON_CLASS_VAR
+                       || var -> class == OBERON_CLASS_PARAM
+                       || var -> class == OBERON_CLASS_VAR_PARAM)
+               {
+                       gen_var_t * v = var -> gen_var;
+                       if(v -> storage == JVM_STORAGE_FRAME
+                               || v -> storage == JVM_STORAGE_FRAME_PARAM)
+                       {
+                               fprintf(fp, ".field public %s %s\n\n", v -> name, v -> type -> desc);
+                       }
+                       else if(v -> storage == JVM_STORAGE_FRAME_VAR
+                               || v -> storage == JVM_STORAGE_FRAME_PARAM_VAR)
+                       {
+                               fprintf(fp, ".field public %s [%s\n\n", v -> name, v -> type -> desc);
+                       }
+                       else if(v -> storage == JVM_STORAGE_FRAME_PARAM_VARPTR)
+                       {
+                               fprintf(fp, ".field public %s [%s\n", v -> name, v -> type -> desc);
+                               fprintf(fp, ".field public %s$offset I\n\n", v -> name);
+                       }
+               }
+               var = var -> next;
+       }
+
+       fprintf(fp, ".method <init>()V\n");
+       fprintf(fp, "  .limit stack 2\n");
+       fprintf(fp, "  aload_0\n");
+       fprintf(fp, "  invokespecial %s/<init>()V\n", abscname);
+
+       var = proc -> scope -> list -> next;
+       while(var)
+       {
+               if(var -> class == OBERON_CLASS_VAR
+                       || var -> class == OBERON_CLASS_PARAM
+                       || var -> class == OBERON_CLASS_VAR_PARAM)
+               {
+                       gen_var_t * v = var -> gen_var;
+                       if(v -> storage == JVM_STORAGE_FRAME_VAR
+                               || v -> storage == JVM_STORAGE_FRAME_PARAM_VAR)
+                       {
+                               fprintf(fp, "  aload_0\n");
+                               fprintf(fp, "  iconst_1\n");
+                               fprintf(fp, "  multianewarray [%s 1\n", v -> type -> desc);
+                               fprintf(fp, "  putfield %s [%s\n", v -> full_name, v -> type -> desc);
+                       }
+               }
+               var = var -> next;
+       }
+
+       fprintf(fp, "  return\n");
+       fprintf(fp, ".end method\n\n");
+
+       jvm_destroy_class(class);
+
+       return class;
 }
 
 void
-jvm_generate_param_initialization(gen_proc_t * p, gen_var_t * v, char * name)
+jvm_generate_procedure_frame(oberon_object_t * proc)
 {
-       int old_reg = v -> reg;
-       bool wide = v -> type -> wide;
-       int cell_size = v -> type -> cell_size;
-       char prefix = v -> type -> prefix;
-       char * desc = v -> type -> desc;
+       gen_proc_t * p;
+       gen_type_t * t;
+       gen_var_t * v;
+       struct gen_class * class;
 
-        jvm_generate(p, 0, 0, ".var %i is %s %s from start to end", old_reg, name, desc);
-        v -> reg = jvm_alloc_register_untyped(p -> rf, wide);
+       p = proc -> gen_proc;
 
-       jvm_generate(p, 0, 1, "iconst_1");
-       jvm_generate(p, 1, 1, "multianewarray [%s 1", desc);
-       jvm_generate(p, 1, 2, "dup");
-       jvm_generate(p, 1, 0, "astore %i", v -> reg);
+       jvm_generate_abstract_frame_class();
+       class = jvm_generate_frame_class(proc);
 
-       jvm_generate(p, 0, 1, "iconst_0");
-       jvm_generate(p, 0, cell_size, "%cload %i", prefix, old_reg);
-       jvm_generate(p, 1 + 1 + cell_size, 0, "%castore", prefix);
+       t = GC_MALLOC(sizeof *t);
+       memset(t, 0, sizeof *t);
+       t -> class = class;
+       t -> wide = false;
+       t -> prefix = 'a';
+       t -> postfix = 'a';
+       t -> cell_size = 1;
+       t -> desc = new_string("L%s;", class -> full_name);
+
+       v = jvm_create_function_var(p, JVM_STORAGE_REGISTER, "$FP$", t);
+
+       p -> frame_v = v;
+       p -> frame_t = t;
 }
 
 void
-jvm_generate_var_param_initialization(gen_proc_t * p, gen_var_t * v, char * name)
+jvm_generate_frame_initialization(gen_proc_t * p)
 {
-       jvm_generate(p, 0, 0, ".var %i is %s [%s from start to end", v -> reg, name, v -> type -> desc);
-       jvm_generate(p, 0, 0, ".var %i is __%s_offset I from start to end", v -> reg + 1, name);
+       jvm_generate_var(p -> frame_v);
+       jvm_generate_ldst_prepare(p, p -> frame_v);
+       jvm_generate(p, 0, 1, "new %s", p -> frame_t -> class -> full_name);
+       jvm_generate(p, 1, 2, "dup");
+       jvm_generate(p, 1, 0, "invokespecial %s/<init>()V", p -> frame_t -> class -> full_name);
+       jvm_generate_store(p, p -> frame_v);
 }
index 90f5d653ccada22664742c12a98621f271cc9c7d..027315628b179a053d332a7d6cdfbeeb08c87179 100644 (file)
@@ -1,4 +1,4 @@
-void 
+void
 jvm_generate_ldst_prepare(gen_proc_t * p, gen_var_t * v);
 
 void
@@ -7,39 +7,23 @@ jvm_generate_load(gen_proc_t * p, gen_var_t * src);
 void
 jvm_generate_store(gen_proc_t * p, gen_var_t * dst);
 
+gen_var_t *
+jvm_create_class_var(struct gen_class * class, enum gen_storage storage, char * name, gen_type_t * t);
 
+gen_var_t *
+jvm_create_function_var(gen_proc_t * p, enum gen_storage storage, char * name, gen_type_t * t);
 
-void
-jvm_generate_and_init_global_var(struct gen_class * class, gen_var_t * v, char * name, gen_type_t * t);
+gen_var_t *
+jvm_create_function_temp_var(gen_proc_t * p, gen_type_t * t);
 
 void
-jvm_generate_and_init_field(struct gen_class * class, gen_var_t * v, char * name, gen_type_t * t);
+jvm_generate_var(gen_var_t * v);
 
 void
-jvm_generate_and_init_local_var(gen_proc_t * p, gen_var_t * v, gen_type_t * t);
-
-
+jvm_generate_var_initialization(gen_proc_t * p, gen_var_t * v);
 
 void
-jvm_init_named_local_var(gen_proc_t * p, gen_var_t * v, gen_type_t * t);
+jvm_generate_procedure_frame(oberon_object_t * proc);
 
 void
-jvm_init_param(gen_proc_t * p, gen_var_t * v, gen_type_t * t);
-
-void
-jvm_init_var_param(gen_proc_t * p, gen_var_t * v, gen_type_t * t);
-
-
-
-void
-jvm_generate_variable_initialization(gen_proc_t * p, gen_var_t * v);
-
-void
-jvm_generate_named_local_var_initialization(gen_proc_t * p, gen_var_t * v, char * name);
-
-void
-jvm_generate_param_initialization(gen_proc_t * p, gen_var_t * v, char * name);
-
-void
-jvm_generate_var_param_initialization(gen_proc_t * p, gen_var_t * v, char * name);
-
+jvm_generate_frame_initialization(gen_proc_t * p);
index 50f3af92ee8706957784bd4a55667b9e4a8b9e82..4aee145479330f49cb12751047bf244bc14b72a4 100644 (file)
@@ -279,14 +279,6 @@ jvm_generate_push_string(gen_proc_t * p, char * str, int char_size)
        }
 }
 
-gen_var_t *
-oberon_generator_new_var()
-{
-       gen_var_t * v = GC_MALLOC(sizeof *v);
-       memset(v, 0, sizeof *v);
-       return v;       
-}
-
 int
 jvm_new_label_id(gen_proc_t * p)
 {
index da287a0f3f05915d0c9f86f134d3e2345c2a0344..2b5911cdd23045b5bf93f3ebbe7ec44f35f991ff 100644 (file)
@@ -7,9 +7,6 @@ jvm_destroy_class(struct gen_class * class);
 gen_proc_t *
 jvm_create_proc(struct gen_class * class);
 
-gen_var_t *
-oberon_generator_new_var();
-
 
 
 void
index 559325964d72498d3bfd7cfee1f1faf9e7e02012..450105525a0aa9b290fd7e0c3f7c91e5904dfdd9 100644 (file)
@@ -378,7 +378,7 @@ oberon_generate_procedure_pointer_class(oberon_object_t * proc)
 
        fprintf(fp, ".field public static pointer %s\n\n", absdesc);
 
-       fprintf(fp, ".method <init>()V\n");
+       fprintf(fp, ".method private <init>()V\n");
        fprintf(fp, "  aload_0\n");
        fprintf(fp, "  invokespecial %s/<init>()V\n", abscname);
        fprintf(fp, "  return\n");
@@ -419,7 +419,6 @@ oberon_generate_procedure_pointer_class(oberon_object_t * proc)
                use_size += t -> cell_size;
                param = param -> next;
        }
-
        
        char * full_name = jvm_get_field_full_name(proc);
        int cell_size = jvm_cell_size_for_type(proc -> type -> base);
@@ -522,10 +521,7 @@ oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * rec)
        oberon_object_t * field = rec -> decl;
        for(int i = 0; i < num; i++)
        {
-               char * name = field -> name;
-               gen_var_t * v = field -> gen_var;
-               gen_type_t * t = field -> type -> gen_type;
-               jvm_generate_and_init_field(class, v, name, t);
+               jvm_generate_var(field -> gen_var);
                field = field -> next;
        }
 
@@ -548,7 +544,7 @@ oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * rec)
        for(int i = 0; i < num; i++)
        {
                jvm_generate(p, 0, 1, "aload_0");
-               jvm_generate_variable_initialization(p, field -> gen_var);
+               jvm_generate_var_initialization(p, field -> gen_var);
 
                if(field -> type -> class == OBERON_TYPE_RECORD
                        || field -> type -> class == OBERON_TYPE_ARRAY)
@@ -566,14 +562,16 @@ oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * rec)
        /* Метод для копирования полей класса */
        /* reg0 == src -> reg1 == dst */
        p = jvm_create_proc(class);
-       gen_var_t * copy_dst = oberon_generator_new_var();
-       gen_var_t * copy_src = oberon_generator_new_var();
        char * signature = new_string("(%s%s)V", rec -> gen_type -> desc, rec -> gen_type -> desc);
        jvm_generate_function_header(p, "public static", "$COPY$", signature);
-       jvm_init_param(p, copy_dst, rec -> gen_type);
-       jvm_init_param(p, copy_src, rec -> gen_type);
-       jvm_generate_param_initialization(p, copy_dst, "dst");
-       jvm_generate_param_initialization(p, copy_src, "src");
+
+       gen_var_t * copy_dst = jvm_create_function_var(p, JVM_STORAGE_REGISTER, "dst", rec -> gen_type);
+       gen_var_t * copy_src = jvm_create_function_var(p, JVM_STORAGE_REGISTER, "src", rec -> gen_type);
+       jvm_generate_var(copy_dst);
+       jvm_generate_var(copy_src);
+       jvm_generate_var_initialization(p, copy_dst);
+       jvm_generate_var_initialization(p, copy_src);
+
        num = rec -> num_decl;
        field = rec -> decl;
        for(int i = 0; i < num; i++)
@@ -613,9 +611,6 @@ oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * rec)
 void
 oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var)
 {
-       gen_var_t * v = oberon_generator_new_var();
-       var -> gen_var = v;
-
        gen_module_t * m;
        m = ctx -> mod -> gen_mod;
 
@@ -623,29 +618,33 @@ oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var)
        class = m -> class;
 
        gen_proc_t * p;
+       struct gen_class * c;
+       char * name = var -> name;
+       gen_type_t * t = var -> type -> gen_type;
        switch(var -> class)
        {
                case OBERON_CLASS_VAR_PARAM:
                        p = var -> parent -> gen_proc;
-                       jvm_init_var_param(p, v, var -> type -> gen_type);
+                       var -> gen_var = jvm_create_function_var(p, JVM_STORAGE_FRAME_PARAM_VARPTR, name, t);
                        break;
                case OBERON_CLASS_PARAM:
                        p = var -> parent -> gen_proc;
-                       jvm_init_param(p, v, var -> type -> gen_type);
+                       var -> gen_var = jvm_create_function_var(p, JVM_STORAGE_FRAME_PARAM_VAR, name, t);
                        break;
                case OBERON_CLASS_FIELD:
-                       /* Заполняются при инициализации структуры */
-                       /* смотри oberon_generator_init_record() */
+                       c = var -> parent_type -> gen_type -> class;
+                       var -> gen_var = jvm_create_class_var(c, JVM_STORAGE_FIELD_VAR, name, t);
                        break;
                case OBERON_CLASS_VAR:
                        if(var -> local)
                        {
                                p = var -> parent -> gen_proc;
-                               jvm_init_named_local_var(p, v, var -> type -> gen_type);
+                               var -> gen_var = jvm_create_function_var(p, JVM_STORAGE_FRAME_VAR, name, t);
                        }
                        else
                        {
-                               jvm_generate_and_init_global_var(class, v, var -> name, var -> type -> gen_type);
+                               var -> gen_var = jvm_create_class_var(class, JVM_STORAGE_STATIC_VAR, name, t);
+                               jvm_generate_var(var -> gen_var);
                        }
                        break;
                default:
@@ -659,16 +658,18 @@ oberon_generator_init_temp_var(oberon_context_t * ctx, oberon_object_t * var)
 {
        assert(var -> class == OBERON_CLASS_VAR);
 
-       gen_var_t * v = oberon_generator_new_var();
-       var -> gen_var = v;
-
        gen_module_t * m;
        m = ctx -> mod -> gen_mod;
 
        gen_proc_t * p;
        p = m -> class -> p;
 
-       jvm_generate_and_init_local_var(p, v, var -> type -> gen_type);
+       gen_type_t * t;
+       t = var -> type -> gen_type;
+
+       var -> gen_var = jvm_create_function_temp_var(p, t);
+       jvm_generate_var(var -> gen_var);
+       jvm_generate_var_initialization(p, var -> gen_var);
 }
 
 void
@@ -676,6 +677,7 @@ oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * proc)
 {
        struct gen_class * class = proc -> module -> gen_mod -> class;
        proc -> gen_proc = jvm_create_proc(class);
+       proc -> gen_proc -> full_name = jvm_get_field_full_name_safe(proc);
 }
 
 void
@@ -723,7 +725,7 @@ oberon_generate_begin_module(oberon_context_t * ctx)
        {
                if(x -> class == OBERON_CLASS_VAR)
                {
-                       jvm_generate_variable_initialization(p, x -> gen_var);
+                       jvm_generate_var_initialization(p, x -> gen_var);
                        if(x -> type -> class == OBERON_TYPE_ARRAY
                                || x -> type -> class == OBERON_TYPE_RECORD)
                        {
@@ -802,11 +804,10 @@ jvm_generate_record_duplicate_and_replace(gen_proc_t * p, gen_var_t * v, oberon_
 }
 
 static void
-jvm_generate_local_initialization(gen_proc_t * p, oberon_object_t * x)
+jvm_generate_local_object(gen_proc_t * p, oberon_object_t * x)
 {
        gen_var_t * v;
        struct gen_class * class;
-       char * name;
 
        v = x -> gen_var;
        class = p -> class;
@@ -815,17 +816,40 @@ jvm_generate_local_initialization(gen_proc_t * p, oberon_object_t * x)
        assert(class -> p);
        assert(x -> local);
 
-       name = jvm_get_name(x);
        switch(x -> class)
        {
                case OBERON_CLASS_VAR_PARAM:
-                       jvm_generate_var_param_initialization(p, v, name);
-                       break;
                case OBERON_CLASS_PARAM:
-                       jvm_generate_param_initialization(p, v, name);
+               case OBERON_CLASS_VAR:
+                       jvm_generate_var(v);
                        break;
+               case OBERON_CLASS_PROC:
+                       break;
+               default:
+                       gen_error("jvm_generate_local_initialization: wat");
+                       break;
+       }
+}
+
+static void
+jvm_generate_local_initialization(gen_proc_t * p, oberon_object_t * x)
+{
+       gen_var_t * v;
+       struct gen_class * class;
+
+       v = x -> gen_var;
+       class = p -> class;
+
+       // Убеждаемся что сейчас находимся в функции
+       assert(class -> p);
+       assert(x -> local);
+
+       switch(x -> class)
+       {
+               case OBERON_CLASS_VAR_PARAM:
+               case OBERON_CLASS_PARAM:
                case OBERON_CLASS_VAR:
-                       jvm_generate_named_local_var_initialization(p, v, name);
+                       jvm_generate_var_initialization(p, v);
                        break;
                case OBERON_CLASS_PROC:
                        break;
@@ -834,7 +858,7 @@ jvm_generate_local_initialization(gen_proc_t * p, oberon_object_t * x)
                        break;
        }
 
-       if(x -> class == OBERON_CLASS_PARAM || x -> class == OBERON_CLASS_VAR_PARAM)
+       if(x -> class == OBERON_CLASS_PARAM)
        {
                if(x -> type -> class == OBERON_TYPE_ARRAY)
                {
@@ -853,6 +877,7 @@ oberon_generate_begin_proc(oberon_context_t * ctx, oberon_object_t * proc)
        gen_proc_t * p;
        char * name;
        char * signature;
+       oberon_object_t * var;
 
        p = proc -> gen_proc;
 
@@ -865,8 +890,19 @@ oberon_generate_begin_proc(oberon_context_t * ctx, oberon_object_t * proc)
        name = jvm_get_name(proc);
        jvm_generate_function_header(p, "public static", name, signature);
 
-       /* Инициализация */
-       oberon_object_t * var = proc -> scope -> list -> next;
+       /* Создание парамеров и переменных */
+       var = proc -> scope -> list -> next;
+       while(var)
+       {
+               jvm_generate_local_object(p, var);
+               var = var -> next;
+       }
+
+       jvm_generate_procedure_frame(proc);
+       jvm_generate_frame_initialization(p);
+
+       /* Инициализация парамеров и переменных */
+       var = proc -> scope -> list -> next;
        while(var)
        {
                jvm_generate_local_initialization(p, var);
@@ -1389,10 +1425,12 @@ jvm_generate_range(gen_proc_t * p, oberon_expr_t * a, oberon_expr_t * b)
        }
        else
        {
-               ra = oberon_generator_new_var();
-               rb = oberon_generator_new_var();
-               jvm_generate_and_init_local_var(p, ra, type -> gen_type);
-               jvm_generate_and_init_local_var(p, rb, type -> gen_type);
+               ra = jvm_create_function_temp_var(p, type -> gen_type);
+               rb = jvm_create_function_temp_var(p, type -> gen_type);
+               jvm_generate_var(ra);
+               jvm_generate_var(rb);
+               jvm_generate_var_initialization(p, ra);
+               jvm_generate_var_initialization(p, rb);
                label_else = jvm_new_label_id(p);
                label_end = jvm_new_label_id(p);
 
index 242aa70b10f8f2b17b21926b06de0e9192b7021c..6786cf44dc05d467128d15ff2c8efb018bcc9e0d 100644 (file)
@@ -28,17 +28,32 @@ struct gen_class
 enum gen_storage
 {
        JVM_STORAGE_UNKNOWN,
+       /* Обыкновенные Java-переменные */
        JVM_STORAGE_REGISTER,
        JVM_STORAGE_STATIC,
        JVM_STORAGE_FIELD,
-       JVM_STORAGE_LOCAL,
-       JVM_STORAGE_VARPTR
+       JVM_STORAGE_PARAM,
+       /* Переменные на которые можно делать указатели без кеширования */
+       JVM_STORAGE_REGISTER_VAR,
+       JVM_STORAGE_STATIC_VAR,
+       JVM_STORAGE_FIELD_VAR,
+       JVM_STORAGE_PARAM_VAR,
+       JVM_STORAGE_PARAM_VARPTR,
+       /* Локальные переменные доступные локальным функциям */
+       JVM_STORAGE_FRAME,
+       JVM_STORAGE_FRAME_VAR,
+       JVM_STORAGE_FRAME_PARAM,
+       JVM_STORAGE_FRAME_PARAM_VAR,
+       JVM_STORAGE_FRAME_PARAM_VARPTR
 };
 
 
 struct gen_proc_t
 {
+       char * full_name;
        int label_id;
+       gen_var_t * frame_v;
+       gen_type_t * frame_t;
        struct gen_register_file * rf;
        struct gen_stack * stack;
        struct gen_class * class;
@@ -61,7 +76,11 @@ struct gen_var_t
        enum gen_storage storage;
        gen_type_t * type;
        int reg;
+
+       char * name;
        char * full_name;
+       gen_proc_t * p;
+       struct gen_class * class;
 };
 
 struct gen_context_t
index 595df77a7af072cf374214cd89c64b44c853fcaf..fb76fca317b3174656c7c48fb9483dfa175e2a91 100644 (file)
@@ -9,28 +9,23 @@ static char source_test[] =
        "MODULE Test;"
        "IMPORT Out;"
        ""
-       "TYPE"
-       "  R = RECORD iii : INTEGER; END;"
+       "VAR i : INTEGER;"
        ""
-       "VAR"
-       "  i : INTEGER;"
-       "  a : ARRAY 3 OF INTEGER;"
-       "  r : R;"
-       ""
-       "PROCEDURE X(VAR x : INTEGER);"
+       "PROCEDURE X(VAR x : INTEGER; y : INTEGER);"
+       "  VAR"
+       "    q, w, r : INTEGER;"
        "  PROCEDURE Y;"
-       "  BEGIN"
-       "    Out.String('YYYyYYYyYY'); Out.Ln;"
        "  END Y;"
        "BEGIN"
+       "  x := 54646;"
+       "  q := 1;"
        "  Y;"
-       "  x := x * 666;"
        "END X;"
        ""
        "BEGIN;"
-       "  r.iii := 2;"
-       "  X(r.iii);"
-       "  Out.Int(r.iii, 0); Out.Ln;"
+       "  i := 1;"
+       "  X(i, i);"
+       "  Out.Int(i, 0); Out.Ln;"
        "END Test."
 ;