summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: bd4b622)
raw | patch | inline | side by side (parent: bd4b622)
author | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Sat, 29 Jul 2017 10:20:22 +0000 (13:20 +0300) | ||
committer | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Sat, 29 Jul 2017 10:20:22 +0000 (13:20 +0300) |
notes | patch | blob | history | |
src/backends/jvm/generator-jvm.c | patch | blob | history | |
src/test.c | patch | blob | history |
index 60b17bda4932bb27322d9bae00b186974f032d5f..3cc029712b28882beb17e8ea07e05d9bdbbbd1b3 100644 (file)
--- a/notes
+++ b/notes
+- Какие-то проблемы с определением типов наперёд:
+ (*
+ TYPE
+ R2 = RECORD r : R1 END;
+ R1 = RECORD END;
+ *)
+
+
- нет символов и строк
- нужен автокаст int -> real для DIV. Да и вообще каст типов.
index cbc834e58d5cf919926aae41423023dc4e259013..b46b27de1eedfd7066d64cd55f42f96b65128a77 100644 (file)
class -> p = p;
fprintf(class -> fp, ".method %s %s%s\n", access, name, signature);
+ fprintf(class -> fp, " start:\n");
}
void
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");
}
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
/*
* Функция jvm_generate_static_array_initialization генерирует код для
* статической инициализации массива. На входе массив, на выходе тот же массив.
- * arrayref -- arrayref
+ * (arrayref -- arrayref)
*/
static void
}
}
+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)
{
}
}
+/*
+ * Генерирует код для копирования полей из первой записи во вторую.
+ * ( 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);
+}
+
+
// ==========================================
// ==========================================
// ==========================================
field = field -> next;
}
+ /* Стандартный конструктор класса */
+ /* Инициализирует внутренние статические записи и массивы */
gen_proc_t * p = jvm_create_proc(class);
-
jvm_generate_function_header(p, "public", "<init>", "()V");
jvm_alloc_register_untyped(p -> rf, false);
jvm_generate(p, 0, 1, "aload_0");
jvm_generate(p, 1, 0, "invokespecial java/lang/Object/<init>()V");
-
num = rec -> num_decl;
field = rec -> decl;
for(int i = 0; i < num; i++)
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);
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 02912bbe97ae1e523f23c91eb2027900a8c15dee..b0e9bdafe6527405ecb9d9b0b9cb8bf02aae8dc2 100644 (file)
--- a/src/test.c
+++ b/src/test.c
"(* 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."
;