From: DeaDDooMER Date: Sat, 29 Jul 2017 10:20:22 +0000 (+0300) Subject: JVM: Добавлено копирование записей X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=0c39f03a14be51224b1a44a74121740076956243;p=dsw-obn.git JVM: Добавлено копирование записей --- diff --git a/notes b/notes index 60b17bd..3cc0297 100644 --- a/notes +++ b/notes @@ -1,3 +1,11 @@ +- Какие-то проблемы с определением типов наперёд: + (* + TYPE + R2 = RECORD r : R1 END; + R1 = RECORD END; + *) + + - нет символов и строк - нужен автокаст int -> real для DIV. Да и вообще каст типов. diff --git a/src/backends/jvm/generator-jvm.c b/src/backends/jvm/generator-jvm.c index cbc834e..b46b27d 100644 --- a/src/backends/jvm/generator-jvm.c +++ b/src/backends/jvm/generator-jvm.c @@ -48,6 +48,7 @@ jvm_generate_function_header(gen_proc_t * p, char * access, char * name, char * class -> p = p; fprintf(class -> fp, ".method %s %s%s\n", access, name, signature); + fprintf(class -> fp, " start:\n"); } void @@ -66,6 +67,7 @@ jvm_generate_function_end(gen_proc_t * p) fprintf(class -> fp, " .limit stack %i \t; current(%i)\n", max_pointer, pointer); fprintf(class -> fp, " .limit locals %i \t; current(%i)\n", max_locals, locals); + fprintf(class -> fp, " end:\n"); fprintf(class -> fp, ".end method\n\n"); } @@ -104,7 +106,11 @@ static void jvm_stack_pop(gen_proc_t * p, unsigned size) { p -> stack -> pointer -= size; - assert(p -> stack -> pointer >= 0); + + if(p -> stack -> pointer < 0) + { + printf("WARING: stack pointer %i\n", p -> stack -> pointer); + } } static void @@ -261,7 +267,7 @@ jvm_generate_new(gen_proc_t * p, oberon_type_t * type, int num); /* * Функция jvm_generate_static_array_initialization генерирует код для * статической инициализации массива. На входе массив, на выходе тот же массив. - * arrayref -- arrayref + * (arrayref -- arrayref) */ static void @@ -400,6 +406,32 @@ jvm_generate_new(gen_proc_t * p, oberon_type_t * type, int num) } } +static void +jvm_generate_load(gen_proc_t * p, oberon_type_t * src_type, gen_var_t * src) +{ + char prefix; + int cell_size; + + cell_size = jvm_cell_size_for_type(src_type); + + switch(src -> storage) + { + case JVM_STORAGE_REGISTER: + prefix = jvm_get_prefix(src_type); + 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", src -> full_name, src -> desc); + break; + case JVM_STORAGE_FIELD: + jvm_generate(p, 1, cell_size, "getfield %s %s", src -> full_name, src -> desc); + break; + default: + gen_error("jvm_generate_load: unknow storage type %i", src -> storage); + break; + } +} + static void jvm_generate_store(gen_proc_t * p, oberon_type_t * src_type, gen_var_t * dst) { @@ -426,6 +458,22 @@ jvm_generate_store(gen_proc_t * p, oberon_type_t * src_type, gen_var_t * dst) } } +/* + * Генерирует код для копирования полей из первой записи во вторую. + * ( aref_dst aref_src -- ) + * dst := src; + */ + +static void +jvm_generate_copy_record(gen_proc_t * p, oberon_type_t * rec) +{ + assert(rec -> class == OBERON_TYPE_RECORD); + char * desc = jvm_get_descriptor(rec); + char * cname = jvm_get_class_full_name(rec); + jvm_generate(p, 1 + 1, 0, "invokestatic %s/$COPY$(%s%s)V", cname, desc, desc); +} + + // ========================================== // ========================================== // ========================================== @@ -543,13 +591,13 @@ oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * rec) field = field -> next; } + /* Стандартный конструктор класса */ + /* Инициализирует внутренние статические записи и массивы */ gen_proc_t * p = jvm_create_proc(class); - jvm_generate_function_header(p, "public", "", "()V"); jvm_alloc_register_untyped(p -> rf, false); jvm_generate(p, 0, 1, "aload_0"); jvm_generate(p, 1, 0, "invokespecial java/lang/Object/()V"); - num = rec -> num_decl; field = rec -> decl; for(int i = 0; i < num; i++) @@ -562,8 +610,45 @@ oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * rec) jvm_generate_store(p, field -> type, field -> gen_var); } field = field -> next; - } + } + jvm_generate(p, 0, 0, "return"); + jvm_generate_function_end(p); + /* Метод для копирования полей класса */ + /* 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 * desc = jvm_get_descriptor(rec); + char * signature = new_string("(%s%s)V", desc, desc); + jvm_generate_function_header(p, "public static", "$COPY$", signature); + jvm_generate_and_init_named_local_var(copy_dst, p, false, "dst", desc); + jvm_generate_and_init_named_local_var(copy_src, p, false, "src", desc); + num = rec -> num_decl; + field = rec -> decl; + for(int i = 0; i < num; i++) + { + if(field -> type -> class == OBERON_TYPE_RECORD) + { + jvm_generate_load(p, rec, copy_dst); + jvm_generate_load(p, field -> type, field -> gen_var); + jvm_generate_load(p, rec, copy_src); + jvm_generate_load(p, field -> type, field -> gen_var); + jvm_generate_copy_record(p, field -> type); + } + else if(field -> type -> class == OBERON_TYPE_ARRAY) + { + gen_error("copy array not implemented"); + } + else + { + jvm_generate_load(p, rec, copy_dst); + jvm_generate_load(p, rec, copy_src); + jvm_generate_load(p, field -> type, field -> gen_var); + jvm_generate_store(p, field -> type, field -> gen_var); + } + field = field -> next; + } jvm_generate(p, 0, 0, "return"); jvm_generate_function_end(p); @@ -1132,10 +1217,11 @@ store_expr(gen_proc_t * p, oberon_expr_t * dst, oberon_expr_t * src) else if(dst -> result -> class == OBERON_TYPE_RECORD || src -> result -> class == OBERON_TYPE_RECORD) { - gen_error("record copy not implemented"); + push_expr(p, dst); + push_expr(p, src); + jvm_generate_copy_record(p, dst -> result); } - - switch(item -> mode) + else switch(item -> mode) { case MODE_VAR: push_expr(p, src); diff --git a/src/test.c b/src/test.c index 02912bb..b0e9bda 100644 --- a/src/test.c +++ b/src/test.c @@ -8,18 +8,24 @@ static char source_test[] = "(* Main module *)" "MODULE Test;" "TYPE" + " RecA = RECORD" + " i, j, k : INTEGER;" + " END;" + "" " Rec = POINTER TO RecDesc;" - " RecDesc = RECORD x : INTEGER; END;" + " RecDesc = RECORD" + " x, y, z : INTEGER;" + " r, q, s : RecA;" + " p, c, g : Rec;" + " END;" + "" "VAR" - " g : ARRAY 4 OF INTEGER;" - " r : RECORD x : INTEGER; END;" - " a : POINTER TO ARRAY OF ARRAY OF Rec;" + " a, b : Rec;" + "" "BEGIN" - " NEW(a, 10, 10);" - " NEW(a[9, 9]);" - " a[9, 9].x := 666;" - " g[3] := 4;" - " r.x := 4546;" + " NEW(a);" + " NEW(b);" + " a^ := b^;" "END Test." ;