From 9a82efe08fea56335f331158344b17e6d5280a85 Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Fri, 28 Jul 2017 15:56:49 +0300 Subject: [PATCH] =?utf8?q?JVM:=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?utf8?q?=D0=B5=D0=BD=D0=B0=20=D0=B8=D0=BD=D0=B8=D1=86=D0=B8=D0=B0=D0=BB?= =?utf8?q?=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BC=D0=B0=D1=81=D1=81?= =?utf8?q?=D0=B8=D0=B2=D0=B0=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=BD?= =?utf8?q?=D0=BE=D0=B3=D0=BE=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20NEW?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + JTest.java | 9 +- jvm_test.sh | 3 + src/backends/jvm/generator-jvm.c | 202 +++++++++++++++++++++++-------- src/test.c | 12 +- 5 files changed, 171 insertions(+), 57 deletions(-) diff --git a/.gitignore b/.gitignore index 6cd2ab3..1bf4fb9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ dump.txt *.j *.class +*.jad +JTest.java diff --git a/JTest.java b/JTest.java index 1ce1766..2cf5ff9 100644 --- a/JTest.java +++ b/JTest.java @@ -1,9 +1,12 @@ class JTest { - boolean[] z; + static class REC {} - void x(boolean y) + public JTest() { - z[666] = y; + 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(); } } diff --git a/jvm_test.sh b/jvm_test.sh index 2d8ee55..6dcc9ad 100755 --- a/jvm_test.sh +++ b/jvm_test.sh @@ -10,6 +10,9 @@ mkdir -p classes javac -d classes Launcher.java jasmin -d classes *.j +# -a -- for asm as comments +jad -o -b -noinner classes/* + proguard -injars classes \ -libraryjars /usr/lib/jvm/java-8-openjdk/jre/lib/rt.jar \ -dontshrink -dontobfuscate -keep class Test -keep class Launcher diff --git a/src/backends/jvm/generator-jvm.c b/src/backends/jvm/generator-jvm.c index 1d4f970..af45a63 100644 --- a/src/backends/jvm/generator-jvm.c +++ b/src/backends/jvm/generator-jvm.c @@ -59,8 +59,13 @@ jvm_generate_function_end(gen_proc_t * p) assert(class -> p); class -> p = NULL; - fprintf(class -> fp, " .limit stack %i\n", p -> stack -> max_pointer); - fprintf(class -> fp, " .limit locals %i\n", p -> rf -> max_used); + int pointer = p -> stack -> pointer; + int max_pointer = p -> stack -> max_pointer; + int locals = p -> rf -> num_used; + int max_locals = p -> rf -> max_used; + + 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 method\n\n"); } @@ -111,8 +116,8 @@ jvm_generate(gen_proc_t * p, unsigned get, unsigned push, char * format, ...) jvm_stack_pop(p, get); fprintf(p -> class -> fp, " "); vfprintf(p -> class -> fp, format, ptr); - fprintf(p -> class -> fp, "\n"); jvm_stack_push(p, push); + fprintf(p -> class -> fp, " \t ;>>>> %i -- %i : current_stack(%i)\n", get, push, p -> stack -> pointer); va_end(ptr); } @@ -185,6 +190,51 @@ jvm_generate_push_float(gen_proc_t * p, double f, int size) } } +static gen_var_t * +oberon_generator_new_var() +{ + gen_var_t * v = GC_MALLOC(sizeof *v); + memset(v, 0, sizeof *v); + return v; +} + +static void +jvm_generate_and_init_global_var(gen_var_t * v, struct gen_class * class, char * access, char * name, char * desc) +{ + assert(class -> p == NULL); + fprintf(class -> fp, ".field %s static %s %s\n\n", access, name, desc); + + v -> storage = JVM_STORAGE_STATIC; + v -> full_name = new_string("%s/%s", class -> full_name, name); + v -> desc = new_string(desc); +} + +static void +jvm_generate_and_init_field(gen_var_t * v, struct gen_class * class, char * access, char * name, char * desc) +{ + assert(class -> p == NULL); + fprintf(class -> fp, ".field %s %s %s\n\n", access, name, desc); + + v -> storage = JVM_STORAGE_FIELD; + v -> full_name = new_string("%s/%s", class -> full_name, name); + v -> desc = new_string(desc); +} + +static void +jvm_generate_and_init_local_var(gen_var_t * v, gen_proc_t * p, bool wide) +{ + v -> storage = JVM_STORAGE_REGISTER; + v -> reg = jvm_alloc_register_untyped(p -> rf, wide); +} + +static void +jvm_generate_and_init_named_local_var(gen_var_t * v, gen_proc_t * p, bool wide, char * name, char * desc) +{ + jvm_generate_and_init_local_var(v, p, wide); + v -> desc = new_string(desc); + jvm_generate(p, 0, 0, ".var %i is %s %s from start to end", v -> reg, name, desc); +} + static int jvm_new_label_id(gen_proc_t * p) { @@ -205,12 +255,101 @@ jvm_generate_label(gen_proc_t * p, int label_id) jvm_generate(p, 0, 0, "L%i:", label_id); } +static void +jvm_generate_new_static(gen_proc_t * p, oberon_type_t * type); + +/* + * Функция jvm_generate_static_array_initialization генерирует код для + * статической инициализации массива. На входе массив, на выходе тот же массив. + * arrayref -- arrayref + */ + +static void +jvm_generate_static_array_initialization(gen_proc_t * p, oberon_type_t * type) +{ + /* for(int i = 0; i < a.length; i++) */ + /* a[i] := new(base); */ + /* init(a[i]) */ + + /* a := pop(array) */ + gen_var_t * va = oberon_generator_new_var(); + jvm_generate_and_init_local_var(va, p, false); + int reg_a = va -> reg; + /* ссылка уже на стеке */ + jvm_generate(p, 1, 0, "astore %i", reg_a); + + /* int i = 0; */ + gen_var_t * vi = oberon_generator_new_var(); + jvm_generate_and_init_local_var(vi, p, false); + int reg_i = vi -> reg; + jvm_generate(p, 0, 1, "iconst_0"); + jvm_generate(p, 1, 0, "istore %i", reg_i); + + /* int l := a.length */ + gen_var_t * vl = oberon_generator_new_var(); + jvm_generate_and_init_local_var(vl, p, false); + int reg_l = vl -> reg; + jvm_generate(p, 0, 1, "aload %i", reg_a); + jvm_generate(p, 1, 1, "arraylength"); + jvm_generate(p, 1, 0, "istore %i", reg_l); + + int label_start = jvm_new_label_id(p); + int label_end = jvm_new_label_id(p); + + /* start: */ + /* if (i >= l) goto end; */ + /* body*/ + /* i += 1;*/ + /* goto start;*/ + /* end: */ + /* push a */ + + jvm_generate_label(p, label_start); + jvm_generate(p, 0, 1, "iload %i", reg_i); + jvm_generate(p, 0, 1, "iload %i", reg_l); + jvm_generate(p, 2, 0, "if_icmpge L%i", label_end); + + if(type -> base -> class == OBERON_TYPE_ARRAY) + { + /* Инициализируем следующую размерность */ + jvm_generate(p, 0, 1, "aload %i", reg_a); + jvm_generate(p, 0, 1, "iload %i", reg_i); + jvm_generate(p, 0, 1, "aaload"); + jvm_generate_static_array_initialization(p, type -> base); + jvm_generate(p, 1, 0, "pop"); + } + else if(type -> base -> class == OBERON_TYPE_RECORD) + { + 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(p, 3, 0, "aastore"); + } + else + { + assert(0); + } + + /* i += 1; */ + /* goto start */ + + jvm_generate(p, 0, 0, "iinc %i %i", reg_i, 1); + jvm_generate(p, 0, 0, "goto L%i", label_start); + jvm_generate_label(p, label_end); + + /* push(a) */ + jvm_generate(p, 0, 1, "aload %i", reg_a); + + /* TODO освобождение регистров */ +} + static void jvm_generate_new_static(gen_proc_t * p, oberon_type_t * type) { int dim; char * cname; char * desc; + oberon_type_t * base; switch(type -> class) { @@ -229,14 +368,20 @@ jvm_generate_new_static(gen_proc_t * p, oberon_type_t * type) break; case OBERON_TYPE_ARRAY: dim = 0; + base = type; desc = jvm_get_descriptor(type); - while(type -> class == OBERON_TYPE_ARRAY) + while(base -> class == OBERON_TYPE_ARRAY) { dim += 1; - jvm_generate_push_int(p, type -> size); - type = type -> base; + jvm_generate_push_int(p, base -> size); + base = base -> base; } jvm_generate(p, dim, 1, "multianewarray %s %i", desc, dim); + + if(base -> class == OBERON_TYPE_RECORD) + { + jvm_generate_static_array_initialization(p, type); + } break; default: gen_error("jvm_generate_new_static: unk type class %i", type -> class); @@ -270,51 +415,6 @@ jvm_generate_store(gen_proc_t * p, oberon_type_t * src_type, gen_var_t * dst) } } -static gen_var_t * -oberon_generator_new_var() -{ - gen_var_t * v = GC_MALLOC(sizeof *v); - memset(v, 0, sizeof *v); - return v; -} - -static void -jvm_generate_and_init_global_var(gen_var_t * v, struct gen_class * class, char * access, char * name, char * desc) -{ - assert(class -> p == NULL); - fprintf(class -> fp, ".field %s static %s %s\n\n", access, name, desc); - - v -> storage = JVM_STORAGE_STATIC; - v -> full_name = new_string("%s/%s", class -> full_name, name); - v -> desc = new_string(desc); -} - -static void -jvm_generate_and_init_field(gen_var_t * v, struct gen_class * class, char * access, char * name, char * desc) -{ - assert(class -> p == NULL); - fprintf(class -> fp, ".field %s %s %s\n\n", access, name, desc); - - v -> storage = JVM_STORAGE_FIELD; - v -> full_name = new_string("%s/%s", class -> full_name, name); - v -> desc = new_string(desc); -} - -static void -jvm_generate_and_init_local_var(gen_var_t * v, gen_proc_t * p, bool wide) -{ - v -> storage = JVM_STORAGE_REGISTER; - v -> reg = jvm_alloc_register_untyped(p -> rf, wide); -} - -static void -jvm_generate_and_init_named_local_var(gen_var_t * v, gen_proc_t * p, bool wide, char * name, char * desc) -{ - jvm_generate_and_init_local_var(v, p, wide); - v -> desc = new_string(desc); - jvm_generate(p, 0, 0, ".var %i is %s %s from start to end", v -> reg, name, desc); -} - // ========================================== // ========================================== // ========================================== diff --git a/src/test.c b/src/test.c index 7d3ce62..2419c3b 100644 --- a/src/test.c +++ b/src/test.c @@ -7,11 +7,17 @@ static char source_test[] = "(* Main module *)" "MODULE Test;" + "TYPE" + " Packed = ARRAY 16, 32 OF RECORD a : INTEGER; r : RECORD x : INTEGER; END; END;" + " IArray = ARRAY 16, 32, 666 OF INTEGER;" "VAR" - " l : POINTER TO ARRAY 16 OF BOOLEAN;" + " k : POINTER TO Packed;" + " ii : POINTER TO IArray;" "BEGIN" - " NEW(l);" - " l^ := l^;" + " NEW(k);" + " k[1, 2].a := 666;" + " k[5, 2].r.x := 456;" + " NEW(ii);" "END Test." ; -- 2.29.2