From: DeaDDooMER Date: Mon, 7 Aug 2017 13:58:04 +0000 (+0300) Subject: JVM: Реализованы локальные процедуры (без доступа к локальным переменным уровнем... X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=6e0d9d632fdf070f9777fe90b9add849562acaf2;p=dsw-obn.git JVM: Реализованы локальные процедуры (без доступа к локальным переменным уровнем выше) --- diff --git a/src/backends/jvm/generator-jvm-abi.c b/src/backends/jvm/generator-jvm-abi.c index ed4a19a..fa34f1d 100644 --- a/src/backends/jvm/generator-jvm-abi.c +++ b/src/backends/jvm/generator-jvm-abi.c @@ -32,18 +32,21 @@ void jvm_generate_ldst_prepare(gen_proc_t * p, gen_var_t * v) { - char * full_name = v -> full_name; - char * desc = v -> type -> desc; - + char * desc; + char * full_name; switch(v -> storage) { case JVM_STORAGE_REGISTER: break; case JVM_STORAGE_STATIC: + full_name = v -> full_name; + desc = v -> type -> desc; jvm_generate(p, 0, 1, "getstatic %s [%s", full_name, desc); jvm_generate(p, 0, 1, "iconst_0"); break; case JVM_STORAGE_FIELD: + full_name = v -> full_name; + desc = v -> type -> desc; jvm_generate(p, 1, 1, "getfield %s [%s", full_name, desc); jvm_generate(p, 0, 1, "iconst_0"); break; @@ -133,25 +136,32 @@ void jvm_generate_and_init_local_var(gen_proc_t * p, gen_var_t * v, gen_type_t * t) { v -> storage = JVM_STORAGE_REGISTER; - v -> reg = jvm_alloc_register_untyped(p -> rf, t -> wide); + v -> reg = jvm_alloc_register_untyped(p -> rf, t -> wide); v -> type = t; } void -jvm_generate_and_init_named_local_var(gen_proc_t * p, gen_var_t * v, char * name, gen_type_t * t) +jvm_init_named_local_var(gen_proc_t * p, gen_var_t * v, gen_type_t * t) { - jvm_generate_and_init_local_var(p, v, t); - jvm_generate(p, 0, 0, ".var %i is %s %s from start to end", v -> reg, name, t -> desc); + v -> storage = JVM_STORAGE_REGISTER; + v -> reg = jvm_alloc_register_untyped(p -> rf, t -> wide); + v -> type = t; +} + +void +jvm_init_param(gen_proc_t * p, gen_var_t * v, gen_type_t * t) +{ + v -> storage = JVM_STORAGE_LOCAL; + v -> reg = jvm_alloc_register_untyped(p -> rf, t -> wide); + v -> type = t; } void -jvm_generate_and_init_var_param(gen_proc_t * p, gen_var_t * v, char * name, gen_type_t * t) +jvm_init_var_param(gen_proc_t * p, gen_var_t * v, gen_type_t * t) { v -> storage = JVM_STORAGE_VARPTR; - v -> reg = jvm_alloc_register_untyped(p -> rf, true); + v -> reg = jvm_alloc_register_untyped(p -> rf, true); v -> type = t; - jvm_generate(p, 0, 0, ".var %i is %s [%s from start to end", v -> reg, name, t -> desc); - jvm_generate(p, 0, 0, ".var %i is __%s_offset I from start to end", v -> reg + 1, name); } void @@ -172,17 +182,21 @@ jvm_generate_variable_initialization(gen_proc_t * p, gen_var_t * v) } void -jvm_generate_param_initialization(gen_proc_t * p, gen_var_t * v) +jvm_generate_named_local_var_initialization(gen_proc_t * p, gen_var_t * v, char * name) { - assert(v -> storage == JVM_STORAGE_REGISTER); + jvm_generate(p, 0, 0, ".var %i is %s %s from start to end", v -> reg, name, v -> type -> desc); +} +void +jvm_generate_param_initialization(gen_proc_t * p, gen_var_t * v, char * name) +{ int old_reg = v -> reg; bool wide = v -> type -> wide; int cell_size = v -> type -> cell_size; char prefix = v -> type -> prefix; char * desc = v -> type -> desc; - v -> storage = JVM_STORAGE_LOCAL; + jvm_generate(p, 0, 0, ".var %i is %s %s from start to end", old_reg, name, desc); v -> reg = jvm_alloc_register_untyped(p -> rf, wide); jvm_generate(p, 0, 1, "iconst_1"); @@ -194,3 +208,10 @@ jvm_generate_param_initialization(gen_proc_t * p, gen_var_t * v) jvm_generate(p, 0, cell_size, "%cload %i", prefix, old_reg); jvm_generate(p, 1 + 1 + cell_size, 0, "%castore", prefix); } + +void +jvm_generate_var_param_initialization(gen_proc_t * p, gen_var_t * v, char * name) +{ + jvm_generate(p, 0, 0, ".var %i is %s [%s from start to end", v -> reg, name, v -> type -> desc); + jvm_generate(p, 0, 0, ".var %i is __%s_offset I from start to end", v -> reg + 1, name); +} diff --git a/src/backends/jvm/generator-jvm-abi.h b/src/backends/jvm/generator-jvm-abi.h index 01bfefc..90f5d65 100644 --- a/src/backends/jvm/generator-jvm-abi.h +++ b/src/backends/jvm/generator-jvm-abi.h @@ -7,6 +7,8 @@ jvm_generate_load(gen_proc_t * p, gen_var_t * src); void jvm_generate_store(gen_proc_t * p, gen_var_t * dst); + + void jvm_generate_and_init_global_var(struct gen_class * class, gen_var_t * v, char * name, gen_type_t * t); @@ -16,14 +18,28 @@ jvm_generate_and_init_field(struct gen_class * class, gen_var_t * v, char * name void jvm_generate_and_init_local_var(gen_proc_t * p, gen_var_t * v, gen_type_t * t); + + void -jvm_generate_and_init_named_local_var(gen_proc_t * p, gen_var_t * v, char * name, gen_type_t * t); +jvm_init_named_local_var(gen_proc_t * p, gen_var_t * v, gen_type_t * t); void -jvm_generate_and_init_var_param(gen_proc_t * p, gen_var_t * v, char * name, gen_type_t * t); +jvm_init_param(gen_proc_t * p, gen_var_t * v, gen_type_t * t); + +void +jvm_init_var_param(gen_proc_t * p, gen_var_t * v, gen_type_t * t); + + void jvm_generate_variable_initialization(gen_proc_t * p, gen_var_t * v); void -jvm_generate_param_initialization(gen_proc_t * p, gen_var_t * v); +jvm_generate_named_local_var_initialization(gen_proc_t * p, gen_var_t * v, char * name); + +void +jvm_generate_param_initialization(gen_proc_t * p, gen_var_t * v, char * name); + +void +jvm_generate_var_param_initialization(gen_proc_t * p, gen_var_t * v, char * name); + diff --git a/src/backends/jvm/generator-jvm-basic.c b/src/backends/jvm/generator-jvm-basic.c index 277940c..1757cb6 100644 --- a/src/backends/jvm/generator-jvm-basic.c +++ b/src/backends/jvm/generator-jvm-basic.c @@ -274,17 +274,45 @@ jvm_get_postfix(oberon_type_t * type) return '!'; } +char * +jvm_get_name(oberon_object_t * x) +{ + switch(x -> class) + { + case OBERON_CLASS_VAR: + case OBERON_CLASS_VAR_PARAM: + case OBERON_CLASS_PARAM: + case OBERON_CLASS_FIELD: + return new_string(x -> name); + case OBERON_CLASS_PROC: + if(x -> parent) + { + return new_string("%s$%s", jvm_get_name(x -> parent), x -> name); + } + else + { + return new_string(x -> name); + } + default: + gen_error("jvm_get_name: wat"); + } + + return NULL; +} + char * jvm_get_field_full_name(oberon_object_t * x) { + char * parent; switch(x -> class) { case OBERON_CLASS_VAR: + return new_string("%s/%s", x -> module -> name, jvm_get_name(x)); 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); + return new_string("%s/%s", x -> module -> name, jvm_get_name(x)); + case OBERON_CLASS_FIELD: + parent = jvm_get_class_full_name(x -> parent_type); + return new_string("%s/%s", parent, jvm_get_name(x)); case OBERON_CLASS_MODULE: return new_string(x -> module -> name); default: diff --git a/src/backends/jvm/generator-jvm-basic.h b/src/backends/jvm/generator-jvm-basic.h index 121c89b..8d2b29c 100644 --- a/src/backends/jvm/generator-jvm-basic.h +++ b/src/backends/jvm/generator-jvm-basic.h @@ -20,6 +20,9 @@ jvm_get_prefix(oberon_type_t * type); char jvm_get_postfix(oberon_type_t * type); +char * +jvm_get_name(oberon_object_t * x); + char * jvm_get_field_full_name(oberon_object_t * x); diff --git a/src/backends/jvm/generator-jvm.c b/src/backends/jvm/generator-jvm.c index da8b06b..5593259 100644 --- a/src/backends/jvm/generator-jvm.c +++ b/src/backends/jvm/generator-jvm.c @@ -404,19 +404,19 @@ oberon_generate_procedure_pointer_class(oberon_object_t * proc) oberon_object_t * param = proc -> type -> decl; for(int i = 0; i < num; i++) { - gen_var_t * v = oberon_generator_new_var(); gen_type_t * t = param -> type -> gen_type; - use_size += t -> cell_size; if(param -> class == OBERON_CLASS_VAR_PARAM) { - jvm_generate_and_init_var_param(p, v, param -> name, t); - jvm_generate_ldst_prepare(p, v); + int reg = jvm_alloc_register_untyped(p -> rf, true); + jvm_generate(p, 0, 1, "aload %i", reg); + jvm_generate(p, 0, 1, "iload %i", reg + 1); } else { - jvm_generate_and_init_named_local_var(p, v, param -> name, t); - jvm_generate_load(p, v); + int reg = jvm_alloc_register_untyped(p -> rf, t -> wide); + jvm_generate(p, 0, t -> cell_size, "%cload %i", t -> prefix, reg); } + use_size += t -> cell_size; param = param -> next; } @@ -570,8 +570,10 @@ oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * rec) gen_var_t * copy_src = oberon_generator_new_var(); char * signature = new_string("(%s%s)V", rec -> gen_type -> desc, rec -> gen_type -> desc); jvm_generate_function_header(p, "public static", "$COPY$", signature); - jvm_generate_and_init_named_local_var(p, copy_dst, "dst", rec -> gen_type); - jvm_generate_and_init_named_local_var(p, copy_src, "src", rec -> gen_type); + jvm_init_param(p, copy_dst, rec -> gen_type); + jvm_init_param(p, copy_src, rec -> gen_type); + jvm_generate_param_initialization(p, copy_dst, "dst"); + jvm_generate_param_initialization(p, copy_src, "src"); num = rec -> num_decl; field = rec -> decl; for(int i = 0; i < num; i++) @@ -620,21 +622,28 @@ oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var) struct gen_class * class; class = m -> class; + gen_proc_t * p; switch(var -> class) { case OBERON_CLASS_VAR_PARAM: + p = var -> parent -> gen_proc; + jvm_init_var_param(p, v, var -> type -> gen_type); + break; case OBERON_CLASS_PARAM: - /* Заполняется при генерации функции */ - /* смотри jvm_init_local_object() */ + p = var -> parent -> gen_proc; + jvm_init_param(p, v, var -> type -> gen_type); break; case OBERON_CLASS_FIELD: /* Заполняются при инициализации структуры */ /* смотри oberon_generator_init_record() */ break; case OBERON_CLASS_VAR: - /* Локальные заполняются при генерации функции */ - /* смотри jvm_init_local_object() */ - if(var -> local == false) + if(var -> local) + { + p = var -> parent -> gen_proc; + jvm_init_named_local_var(p, v, var -> type -> gen_type); + } + else { jvm_generate_and_init_global_var(class, v, var -> name, var -> type -> gen_type); } @@ -667,15 +676,6 @@ oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * proc) { struct gen_class * class = proc -> module -> gen_mod -> class; proc -> gen_proc = jvm_create_proc(class); - - if(proc -> local) - { - gen_error("generator: local procedures not implemented"); - } - else - { - oberon_generate_procedure_pointer_class(proc); - } } void @@ -802,10 +802,11 @@ jvm_generate_record_duplicate_and_replace(gen_proc_t * p, gen_var_t * v, oberon_ } static void -jvm_init_local_object(gen_proc_t * p, oberon_object_t * x) +jvm_generate_local_initialization(gen_proc_t * p, oberon_object_t * x) { gen_var_t * v; struct gen_class * class; + char * name; v = x -> gen_var; class = p -> class; @@ -814,94 +815,63 @@ jvm_init_local_object(gen_proc_t * p, oberon_object_t * x) assert(class -> p); assert(x -> local); + name = jvm_get_name(x); switch(x -> class) { case OBERON_CLASS_VAR_PARAM: - jvm_generate_and_init_var_param(p, v, x -> name, x -> type -> gen_type); + jvm_generate_var_param_initialization(p, v, name); break; - case OBERON_CLASS_VAR: case OBERON_CLASS_PARAM: - jvm_generate_and_init_named_local_var(p, v, x -> name, x -> type -> gen_type); - break; - default: - gen_error("jvm_init_local_object: wat"); - break; - } -} - -static void -jvm_generate_local_initialization(gen_proc_t * p, oberon_object_t * x) -{ - gen_var_t * v; - struct gen_class * class; - - v = x -> gen_var; - class = p -> class; - - // Убеждаемся что сейчас находимся в функции - assert(class -> p); - assert(x -> local); - - switch(x -> class) - { - case OBERON_CLASS_VAR_PARAM: + jvm_generate_param_initialization(p, v, name); break; case OBERON_CLASS_VAR: - jvm_generate_variable_initialization(p, v); + jvm_generate_named_local_var_initialization(p, v, name); break; - case OBERON_CLASS_PARAM: - jvm_generate_param_initialization(p, v); + case OBERON_CLASS_PROC: break; default: gen_error("jvm_generate_local_initialization: wat"); break; } + + if(x -> class == OBERON_CLASS_PARAM || x -> class == OBERON_CLASS_VAR_PARAM) + { + if(x -> type -> class == OBERON_TYPE_ARRAY) + { + jvm_generate_array_duplicate_and_replace(p, v, x -> type); + } + else if(x -> type -> class == OBERON_TYPE_RECORD) + { + jvm_generate_record_duplicate_and_replace(p, v, x -> type); + } + } } void oberon_generate_begin_proc(oberon_context_t * ctx, oberon_object_t * proc) { gen_proc_t * p; + char * name; char * signature; p = proc -> gen_proc; - signature = jvm_get_procedure_signature(proc -> type); - jvm_generate_function_header(p, "public static", proc -> name, signature); - - /* Выделение регистров под параметры и переменные */ - oberon_object_t * var = proc -> scope -> list -> next; - while(var) + if(proc -> local == false) { - jvm_init_local_object(p, var); - var = var -> next; + oberon_generate_procedure_pointer_class(proc); } + signature = jvm_get_procedure_signature(proc -> type); + name = jvm_get_name(proc); + jvm_generate_function_header(p, "public static", name, signature); + /* Инициализация */ - var = proc -> scope -> list -> next; + oberon_object_t * var = proc -> scope -> list -> next; while(var) { jvm_generate_local_initialization(p, var); var = var -> next; } - - /* Копирование статических/открытых массивов и записей */ - var = proc -> type -> decl; - while(var) - { - if(var -> class == OBERON_CLASS_PARAM || var -> class == OBERON_CLASS_VAR_PARAM) - { - if(var -> type -> class == OBERON_TYPE_ARRAY) - { - jvm_generate_array_duplicate_and_replace(p, var -> gen_var, var -> type); - } - else if(var -> type -> class == OBERON_TYPE_RECORD) - { - jvm_generate_record_duplicate_and_replace(p, var -> gen_var, var -> type); - } - } - var = var -> next; - } } void diff --git a/src/oberon.c b/src/oberon.c index 58eb67c..66a3c70 100644 --- a/src/oberon.c +++ b/src/oberon.c @@ -2469,13 +2469,20 @@ oberon_proc_decl(oberon_context_t * ctx) signature = oberon_new_type_ptr(OBERON_TYPE_VOID); oberon_opt_formal_pars(ctx, &signature); - oberon_initialize_decl(ctx); + //oberon_initialize_decl(ctx); oberon_generator_init_type(ctx, signature); oberon_close_scope(ctx -> decl); oberon_object_t * proc; proc = oberon_find_object(ctx -> decl, name, 0); - if(proc != NULL) + if(proc == NULL) + { + proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, export, read_only, false); + proc -> type = signature; + proc -> scope = proc_scope; + oberon_generator_init_proc(ctx, proc); + } + else { if(proc -> class != OBERON_CLASS_PROC) { @@ -2497,16 +2504,15 @@ oberon_proc_decl(oberon_context_t * ctx) oberon_compare_signatures(ctx, proc -> type, signature); } - else + + proc_scope -> parent = proc; + oberon_object_t * param = proc_scope -> list -> next; + while(param) { - proc = oberon_define_object(ctx -> decl, name, OBERON_CLASS_PROC, export, read_only, false); - proc -> type = signature; - proc -> scope = proc_scope; - oberon_generator_init_proc(ctx, proc); + param -> parent = proc; + param = param -> next; } - proc -> scope -> parent = proc; - if(forward == 0) { proc -> linked = 1; diff --git a/src/test.c b/src/test.c index cd28a21..595df77 100644 --- a/src/test.c +++ b/src/test.c @@ -18,7 +18,12 @@ static char source_test[] = " r : R;" "" "PROCEDURE X(VAR x : INTEGER);" + " PROCEDURE Y;" + " BEGIN" + " Out.String('YYYyYYYyYY'); Out.Ln;" + " END Y;" "BEGIN" + " Y;" " x := x * 666;" "END X;" ""