From 7bb96abf74bcb31d9a3ecb820b0b6565813bf324 Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Sat, 29 Jul 2017 21:57:52 +0300 Subject: [PATCH] =?utf8?q?JVM:=20=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?utf8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D1=8B=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC?= =?utf8?q?=D0=B5=D0=BD=D0=BD=D1=8B=D0=B5-=D0=BF=D1=80=D0=BE=D1=86=D0=B5?= =?utf8?q?=D0=B4=D1=83=D1=80=D1=8B=20=D0=B2=20=D0=B3=D0=B5=D0=BD=D0=B5?= =?utf8?q?=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- JTest.java | 8 +- src/backends/jvm/generator-jvm-basic.c | 21 +++++ src/backends/jvm/generator-jvm-basic.h | 3 + src/backends/jvm/generator-jvm.c | 125 ++++++++++++++++++++++++- src/test.c | 29 +++--- 5 files changed, 163 insertions(+), 23 deletions(-) diff --git a/JTest.java b/JTest.java index 2cf5ff9..0e621c8 100644 --- a/JTest.java +++ b/JTest.java @@ -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; } } diff --git a/src/backends/jvm/generator-jvm-basic.c b/src/backends/jvm/generator-jvm-basic.c index d092f8e..865ffdd 100644 --- a/src/backends/jvm/generator-jvm-basic.c +++ b/src/backends/jvm/generator-jvm-basic.c @@ -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) { diff --git a/src/backends/jvm/generator-jvm-basic.h b/src/backends/jvm/generator-jvm-basic.h index 2d4312f..121c89b 100644 --- a/src/backends/jvm/generator-jvm-basic.h +++ b/src/backends/jvm/generator-jvm-basic.h @@ -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); diff --git a/src/backends/jvm/generator-jvm.c b/src/backends/jvm/generator-jvm.c index dc79ea1..f0768d8 100644 --- a/src/backends/jvm/generator-jvm.c +++ b/src/backends/jvm/generator-jvm.c @@ -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 ()V\n"); + fprintf(fp, ".method ()V\n"); fprintf(fp, " aload_0\n"); fprintf(fp, " invokespecial java/lang/Object/()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 ()V\n"); + fprintf(fp, " aload_0\n"); + fprintf(fp, " invokespecial %s/()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/()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); diff --git a/src/test.c b/src/test.c index 2b9fd06..18417ba 100644 --- a/src/test.c +++ b/src/test.c @@ -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." ; -- 2.29.2