summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: ee2ed29)
raw | patch | inline | side by side (parent: ee2ed29)
author | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Fri, 28 Jul 2017 12:56:49 +0000 (15:56 +0300) | ||
committer | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Fri, 28 Jul 2017 12:56:49 +0000 (15:56 +0300) |
.gitignore | patch | blob | history | |
JTest.java | patch | blob | history | |
jvm_test.sh | patch | blob | history | |
src/backends/jvm/generator-jvm.c | patch | blob | history | |
src/test.c | patch | blob | history |
diff --git a/.gitignore b/.gitignore
index 6cd2ab3a081c657a4b4dd104c2c87a7eb72f44b0..1bf4fb9a79270ea9bd14397187bd5ef89e816804 100644 (file)
--- a/.gitignore
+++ b/.gitignore
dump.txt
*.j
*.class
+*.jad
+JTest.java
diff --git a/JTest.java b/JTest.java
index 1ce17666f7f8b8b0732205ca22a0b6ba49b269a4..2cf5ff94dc9dbe7b5cd163370d20adbad9436442 100644 (file)
--- a/JTest.java
+++ b/JTest.java
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 2d8ee55d56865d7d8cce4c567d556fc3d2ac0876..6dcc9ad80d234870ab8b33036c7987dec029f247 100755 (executable)
--- a/jvm_test.sh
+++ b/jvm_test.sh
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
index 1d4f9702a8254b2a2064743f2a320c075176d742..af45a637eaca1fbbf4655e659cd3ec68f9eaaf18 100644 (file)
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");
}
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);
}
}
}
+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)
{
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)
{
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);
}
}
-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 7d3ce627460453a8d1bfb105585b689eb7d11a1d..2419c3b7c0045ea6906c517822afe5560bff59bc 100644 (file)
--- a/src/test.c
+++ b/src/test.c
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."
;