X-Git-Url: http://deadsoftware.ru/gitweb?p=dsw-obn.git;a=blobdiff_plain;f=src%2Fbackends%2Fjvm%2Fgenerator-jvm.c;h=24268e178a7474cf893ec5836b437fedc6197bcf;hp=559325964d72498d3bfd7cfee1f1faf9e7e02012;hb=023ef0d8349acdfe751bba1b50749361682f72ff;hpb=6e0d9d632fdf070f9777fe90b9add849562acaf2 diff --git a/src/backends/jvm/generator-jvm.c b/src/backends/jvm/generator-jvm.c index 5593259..24268e1 100644 --- a/src/backends/jvm/generator-jvm.c +++ b/src/backends/jvm/generator-jvm.c @@ -319,6 +319,7 @@ oberon_generator_init_context(oberon_context_t * ctx) memset(gen_context, 0, sizeof *gen_context); ctx -> gen_context = gen_context; + gen_context -> dir = "."; } void @@ -327,7 +328,7 @@ oberon_generator_destroy_context(oberon_context_t * ctx) } static void -oberon_generate_procedure_class(oberon_type_t * proc) +oberon_generate_procedure_class(const char * dir, oberon_type_t * proc) { FILE * fp; char * cname; @@ -335,7 +336,7 @@ oberon_generate_procedure_class(oberon_type_t * proc) struct gen_class * class; cname = jvm_get_class_full_name(proc); - class = jvm_create_class(cname); + class = jvm_create_class(dir, cname); fp = class -> fp; fprintf(fp, ".source SYSTEM\n"); @@ -357,7 +358,7 @@ oberon_generate_procedure_class(oberon_type_t * proc) } static void -oberon_generate_procedure_pointer_class(oberon_object_t * proc) +oberon_generate_procedure_pointer_class(const char * dir, oberon_object_t * proc) { FILE * fp; char * cname; @@ -367,7 +368,7 @@ oberon_generate_procedure_pointer_class(oberon_object_t * proc) struct gen_class * class; cname = jvm_get_field_full_name_safe(proc); - class = jvm_create_class(cname); + class = jvm_create_class(dir, cname); abscname = jvm_get_class_full_name(proc -> type); absdesc = jvm_get_descriptor(proc -> type); fp = class -> fp; @@ -378,7 +379,7 @@ oberon_generate_procedure_pointer_class(oberon_object_t * proc) fprintf(fp, ".field public static pointer %s\n\n", absdesc); - fprintf(fp, ".method ()V\n"); + fprintf(fp, ".method private ()V\n"); fprintf(fp, " aload_0\n"); fprintf(fp, " invokespecial %s/()V\n", abscname); fprintf(fp, " return\n"); @@ -419,13 +420,12 @@ oberon_generate_procedure_pointer_class(oberon_object_t * proc) use_size += t -> cell_size; param = param -> 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) + if(proc -> type -> base -> class == OBERON_TYPE_NOTYPE) { jvm_generate(p, 0, 0, "return"); } @@ -441,7 +441,7 @@ oberon_generate_procedure_pointer_class(oberon_object_t * proc) } static void -oberon_generate_record_class(gen_module_t * m, oberon_type_t * rec) +oberon_generate_record_class(const char * dir, gen_module_t * m, oberon_type_t * rec) { char * cname; struct gen_class * class; @@ -451,7 +451,7 @@ oberon_generate_record_class(gen_module_t * m, oberon_type_t * rec) m -> rec_id += 1; cname = jvm_get_class_full_name(rec); - class = jvm_create_class(cname); + class = jvm_create_class(dir, cname); fprintf(class -> fp, ".source %s\n", rec -> module -> name); fprintf(class -> fp, ".class public %s\n", cname); @@ -476,18 +476,13 @@ oberon_generator_init_type(oberon_context_t * ctx, oberon_type_t * type) memset(t, 0, sizeof *t); type -> gen_type = t; - if(type -> class != OBERON_TYPE_VOID) - { - t -> wide = jvm_is_wide_type(type); - t -> prefix = jvm_get_prefix(type); - t -> postfix = jvm_get_postfix(type); - } - t -> cell_size = jvm_cell_size_for_type(type); - t -> desc = jvm_get_descriptor(type); + gen_context_t * c; + c = ctx -> gen_context; + gen_module_t * m; switch(type -> class) { - case OBERON_TYPE_VOID: + case OBERON_TYPE_NOTYPE: case OBERON_TYPE_INTEGER: case OBERON_TYPE_BOOLEAN: case OBERON_TYPE_ARRAY: @@ -496,20 +491,33 @@ oberon_generator_init_type(oberon_context_t * ctx, oberon_type_t * type) case OBERON_TYPE_CHAR: case OBERON_TYPE_STRING: case OBERON_TYPE_SET: + case OBERON_TYPE_NIL: break; case OBERON_TYPE_RECORD: - ; - gen_module_t * m; m = type -> module -> gen_mod; - oberon_generate_record_class(m, type); + oberon_generate_record_class(c -> dir, m, type); break; case OBERON_TYPE_PROCEDURE: - oberon_generate_procedure_class(type); + oberon_generate_procedure_class(c -> dir, type); break; default: gen_error("oberon_generator_init_type: unk calss %i", type -> class); break; } + + if(type -> class != OBERON_TYPE_NOTYPE) + { + t -> wide = jvm_is_wide_type(type); + t -> prefix = jvm_get_prefix(type); + t -> postfix = jvm_get_postfix(type); + } + + t -> cell_size = jvm_cell_size_for_type(type); + + if(type -> class != OBERON_TYPE_NIL) + { + t -> desc = jvm_get_descriptor(type); + } } void @@ -522,10 +530,7 @@ oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * rec) oberon_object_t * field = rec -> decl; for(int i = 0; i < num; i++) { - char * name = field -> name; - gen_var_t * v = field -> gen_var; - gen_type_t * t = field -> type -> gen_type; - jvm_generate_and_init_field(class, v, name, t); + jvm_generate_var(field -> gen_var); field = field -> next; } @@ -548,7 +553,7 @@ oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * rec) for(int i = 0; i < num; i++) { jvm_generate(p, 0, 1, "aload_0"); - jvm_generate_variable_initialization(p, field -> gen_var); + jvm_generate_var_initialization(p, field -> gen_var); if(field -> type -> class == OBERON_TYPE_RECORD || field -> type -> class == OBERON_TYPE_ARRAY) @@ -566,14 +571,16 @@ oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * rec) /* Метод для копирования полей класса */ /* 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 * signature = new_string("(%s%s)V", rec -> gen_type -> desc, rec -> gen_type -> desc); jvm_generate_function_header(p, "public static", "$COPY$", signature); - 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"); + + gen_var_t * copy_dst = jvm_create_function_var(p, JVM_STORAGE_REGISTER, "dst", rec -> gen_type); + gen_var_t * copy_src = jvm_create_function_var(p, JVM_STORAGE_REGISTER, "src", rec -> gen_type); + jvm_generate_var(copy_dst); + jvm_generate_var(copy_src); + jvm_generate_var_initialization(p, copy_dst); + jvm_generate_var_initialization(p, copy_src); + num = rec -> num_decl; field = rec -> decl; for(int i = 0; i < num; i++) @@ -613,9 +620,6 @@ oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * rec) void oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var) { - gen_var_t * v = oberon_generator_new_var(); - var -> gen_var = v; - gen_module_t * m; m = ctx -> mod -> gen_mod; @@ -623,29 +627,33 @@ oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var) class = m -> class; gen_proc_t * p; + struct gen_class * c; + char * name = var -> name; + gen_type_t * t = var -> type -> gen_type; switch(var -> class) { case OBERON_CLASS_VAR_PARAM: p = var -> parent -> gen_proc; - jvm_init_var_param(p, v, var -> type -> gen_type); + var -> gen_var = jvm_create_function_var(p, JVM_STORAGE_FRAME_PARAM_VARPTR, name, t); break; case OBERON_CLASS_PARAM: p = var -> parent -> gen_proc; - jvm_init_param(p, v, var -> type -> gen_type); + var -> gen_var = jvm_create_function_var(p, JVM_STORAGE_FRAME_PARAM_VAR, name, t); break; case OBERON_CLASS_FIELD: - /* Заполняются при инициализации структуры */ - /* смотри oberon_generator_init_record() */ + c = var -> parent_type -> gen_type -> class; + var -> gen_var = jvm_create_class_var(c, JVM_STORAGE_FIELD_VAR, name, t); break; case OBERON_CLASS_VAR: if(var -> local) { p = var -> parent -> gen_proc; - jvm_init_named_local_var(p, v, var -> type -> gen_type); + var -> gen_var = jvm_create_function_var(p, JVM_STORAGE_FRAME_VAR, name, t); } else { - jvm_generate_and_init_global_var(class, v, var -> name, var -> type -> gen_type); + var -> gen_var = jvm_create_class_var(class, JVM_STORAGE_STATIC_VAR, name, t); + jvm_generate_var(var -> gen_var); } break; default: @@ -659,16 +667,18 @@ oberon_generator_init_temp_var(oberon_context_t * ctx, oberon_object_t * var) { assert(var -> class == OBERON_CLASS_VAR); - gen_var_t * v = oberon_generator_new_var(); - var -> gen_var = v; - gen_module_t * m; m = ctx -> mod -> gen_mod; gen_proc_t * p; p = m -> class -> p; - jvm_generate_and_init_local_var(p, v, var -> type -> gen_type); + gen_type_t * t; + t = var -> type -> gen_type; + + var -> gen_var = jvm_create_function_temp_var(p, t); + jvm_generate_var(var -> gen_var); + jvm_generate_var_initialization(p, var -> gen_var); } void @@ -676,6 +686,8 @@ 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); + proc -> gen_proc -> full_name = jvm_get_field_full_name_safe(proc); + jvm_create_static_links(proc); } void @@ -685,16 +697,21 @@ oberon_generator_init_module(oberon_context_t * ctx, oberon_module_t * mod) memset(m, 0, sizeof *m); mod -> gen_mod = m; + gen_context_t * c; + c = ctx -> gen_context; + struct gen_class * class; - class = jvm_create_class(mod -> name); + class = jvm_create_class(c -> dir, mod -> name); fprintf(class -> fp, ".source %s\n", mod -> name); fprintf(class -> fp, ".class %s\n", mod -> name); fprintf(class -> fp, ".super java/lang/Object\n\n"); + fprintf(class -> fp, ".field private static $INITIALIZED$ Z\n\n"); + m -> class = class; - ctx -> gen_context -> current_m = m; + c -> current_m = m; } void @@ -709,21 +726,35 @@ push_expr(gen_proc_t * p, oberon_expr_t * expr); void oberon_generate_begin_module(oberon_context_t * ctx) { - struct gen_class * class = ctx -> mod -> gen_mod -> class; - gen_proc_t * p = jvm_create_proc(class); + gen_proc_t * p; + int label_cont; + struct gen_class * class; - jvm_generate_function_header(p, "public", "", "()V"); - jvm_alloc_register_untyped(p -> rf, false); - jvm_generate(p, 0, 1, "aload_0"); - jvm_generate(p, 1, 0, "invokespecial java/lang/Object/()V"); + class = ctx -> mod -> gen_mod -> class; + + p = jvm_create_proc(class); + jvm_generate_function_header(p, "public static", "BEGIN", "()V"); + + label_cont = jvm_new_label_id(p); + jvm_generate(p, 0, 1, "getstatic %s/$INITIALIZED$ Z", class -> full_name); + jvm_generate(p, 1, 0, "ifeq L%i", label_cont); + jvm_generate(p, 0, 0, "return"); + jvm_generate_label(p, label_cont); + + jvm_generate(p, 0, 1, "iconst_1"); + jvm_generate(p, 1, 0, "putstatic %s/$INITIALIZED$ Z", class -> full_name); /* Инициализация переменных объявленных в модуле */ oberon_object_t * x = ctx -> mod -> decl -> list -> next; while(x != NULL) { - if(x -> class == OBERON_CLASS_VAR) + if(x -> class == OBERON_CLASS_MODULE) + { + jvm_generate(p, 0, 0, "invokestatic %s/BEGIN()V", x -> module -> gen_mod -> class -> full_name); + } + else if(x -> class == OBERON_CLASS_VAR) { - jvm_generate_variable_initialization(p, x -> gen_var); + jvm_generate_var_initialization(p, x -> gen_var); if(x -> type -> class == OBERON_TYPE_ARRAY || x -> type -> class == OBERON_TYPE_RECORD) { @@ -745,6 +776,14 @@ oberon_generate_end_module(oberon_context_t * ctx) jvm_generate(p, 0, 0, "return"); jvm_generate_function_end(class -> p); + + p = jvm_create_proc(class); + jvm_generate_function_header(p, "private", "", "()V"); + jvm_alloc_register_untyped(p -> rf, false); + jvm_generate(p, 0, 1, "aload_0"); + jvm_generate(p, 1, 0, "invokespecial java/lang/Object/()V"); + jvm_generate(p, 0, 0, "return"); + jvm_generate_function_end(class -> p); } /* @@ -802,11 +841,10 @@ jvm_generate_record_duplicate_and_replace(gen_proc_t * p, gen_var_t * v, oberon_ } static void -jvm_generate_local_initialization(gen_proc_t * p, oberon_object_t * x) +jvm_generate_local_object(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; @@ -815,17 +853,40 @@ jvm_generate_local_initialization(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_var_param_initialization(p, v, name); - break; case OBERON_CLASS_PARAM: - jvm_generate_param_initialization(p, v, name); + case OBERON_CLASS_VAR: + jvm_generate_var(v); + break; + case OBERON_CLASS_PROC: + break; + default: + gen_error("jvm_generate_local_initialization: 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: + case OBERON_CLASS_PARAM: case OBERON_CLASS_VAR: - jvm_generate_named_local_var_initialization(p, v, name); + jvm_generate_var_initialization(p, v); break; case OBERON_CLASS_PROC: break; @@ -834,7 +895,7 @@ jvm_generate_local_initialization(gen_proc_t * p, oberon_object_t * x) break; } - if(x -> class == OBERON_CLASS_PARAM || x -> class == OBERON_CLASS_VAR_PARAM) + if(x -> class == OBERON_CLASS_PARAM) { if(x -> type -> class == OBERON_TYPE_ARRAY) { @@ -853,20 +914,42 @@ oberon_generate_begin_proc(oberon_context_t * ctx, oberon_object_t * proc) gen_proc_t * p; char * name; char * signature; + oberon_object_t * var; p = proc -> gen_proc; - if(proc -> local == false) + if(proc -> local) { - oberon_generate_procedure_pointer_class(proc); + signature = jvm_get_local_procedure_signature(proc); + } + else + { + oberon_generate_procedure_pointer_class(p -> class -> dir, proc); + signature = jvm_get_procedure_signature(proc -> type); } - signature = jvm_get_procedure_signature(proc -> type); name = jvm_get_name(proc); jvm_generate_function_header(p, "public static", name, signature); - /* Инициализация */ - oberon_object_t * var = proc -> scope -> list -> next; + if(proc -> local) + { + jvm_generate_staticlinks(proc); + } + + jvm_generate_procedure_frame(proc); + + /* Создание параметров и переменных */ + var = proc -> scope -> list -> next; + while(var) + { + jvm_generate_local_object(p, var); + var = var -> next; + } + + jvm_generate_frame_initialization(p); + + /* Инициализация парамеров и переменных */ + var = proc -> scope -> list -> next; while(var) { jvm_generate_local_initialization(p, var); @@ -980,8 +1063,6 @@ jvm_generate_call_proc(gen_proc_t * p, oberon_item_t * desig) assert(desig -> var == NULL); assert(desig -> mode == MODE_CALL); - char * signature = NULL; - bool direct_call = false; if(desig -> parent -> mode == MODE_VAR) { @@ -991,11 +1072,8 @@ jvm_generate_call_proc(gen_proc_t * p, oberon_item_t * desig) } } - printf("direct_call == %i\n", direct_call); - oberon_type_t * procsig; procsig = desig -> parent -> result; - signature = jvm_get_procedure_signature(procsig); if(direct_call == false) { @@ -1003,6 +1081,11 @@ jvm_generate_call_proc(gen_proc_t * p, oberon_item_t * desig) push_item(p, desig -> parent); } + if(direct_call) + { + jvm_generate_push_static_links(p, desig -> parent -> var); + } + int args_cells = 0; int result_cells = jvm_cell_size_for_type(procsig -> base); @@ -1028,11 +1111,13 @@ jvm_generate_call_proc(gen_proc_t * p, oberon_item_t * desig) if(direct_call) { char * full_name = jvm_get_field_full_name(desig -> parent -> var); + char * signature = jvm_get_local_procedure_signature(desig -> parent -> var); jvm_generate(p, args_cells, result_cells, "invokestatic %s%s", full_name, signature); } else { char * cname = jvm_get_class_full_name(procsig); + char * signature = jvm_get_procedure_signature(procsig); jvm_generate(p, 1 + args_cells, result_cells, "invokevirtual %s/invoke%s", cname, signature); } } @@ -1082,29 +1167,35 @@ jvm_generate_expr_new_pointer(gen_proc_t * p, oberon_type_t * type, int num, obe jvm_generate_new(p, type -> base, num); } +static void +jvm_generate_cast_prefix(gen_proc_t * p, char prefix, char postfix) +{ + if(prefix == postfix) + { + return; + } + + int from_cell_size = jvm_cell_size_for_postfix(prefix); + int to_cell_size = jvm_cell_size_for_postfix(postfix); + jvm_generate(p, from_cell_size, to_cell_size, "%c2%c", prefix, postfix); +} + static void jvm_generate_cast_type(gen_proc_t * p, oberon_type_t * from, oberon_type_t * to) { if(to -> class == OBERON_TYPE_RECORD || to -> class == OBERON_TYPE_POINTER) { - char * full_name = jvm_get_class_full_name(to); - jvm_generate(p, 1, 1, "checkcast %s", full_name); + if(to -> class == OBERON_TYPE_POINTER && to -> base -> class == OBERON_TYPE_RECORD) + { + char * full_name = jvm_get_class_full_name(to); + jvm_generate(p, 1, 1, "checkcast %s", full_name); + } } else { char prefix = jvm_get_prefix(from); char postfix = jvm_get_postfix(to); - int from_cell_size = jvm_cell_size_for_type(from); - int to_cell_size = jvm_cell_size_for_type(to); - - jvm_generate_comment(p, "cast type class from %i(%i) to %i(%i)", from -> class, from -> size, to -> class, to -> size); - - if(prefix == postfix) - { - return; - } - - jvm_generate(p, from_cell_size, to_cell_size, "%c2%c", prefix, postfix); + jvm_generate_cast_prefix(p, prefix, postfix); } } @@ -1124,13 +1215,11 @@ push_item(gen_proc_t * p, oberon_item_t * item) } break; case MODE_INTEGER: + case MODE_BOOLEAN: case MODE_CHAR: case MODE_SET: jvm_generate_push_int_size(p, item -> integer, item -> result -> size); break; - case MODE_BOOLEAN: - jvm_generate_push_int_size(p, item -> boolean, item -> result -> size); - break; case MODE_CALL: jvm_generate_call_proc(p, item); break; @@ -1138,20 +1227,17 @@ push_item(gen_proc_t * p, oberon_item_t * item) ; char postfix = jvm_get_postfix(item -> result); int cell_size = jvm_cell_size_for_postfix(postfix); - assert(item -> parent -> is_item); - push_item(p, (oberon_item_t *) item -> parent); + push_item(p, item -> parent); push_expr(p, item -> args); jvm_generate(p, 1 + 1, cell_size, "%caload", postfix); break; case MODE_FIELD: - assert(item -> parent -> is_item); - push_item(p, (oberon_item_t *) item -> parent); + push_item(p, item -> parent); jvm_generate_load(p, item -> var -> gen_var); break; case MODE_DEREF: /* Все объекты в jvm представляются как указатели */ - assert(item -> parent -> is_item); - push_item(p, (oberon_item_t *) item -> parent); + push_item(p, item -> parent); break; case MODE_NIL: jvm_generate(p, 0, 1, "aconst_null"); @@ -1165,6 +1251,11 @@ push_item(gen_proc_t * p, oberon_item_t * item) case MODE_STRING: jvm_generate_push_string(p, item -> string, item -> result -> size); break; + case MODE_LEN: + push_item(p, item -> parent); + jvm_generate_array_len(p, item -> integer); + jvm_generate_cast_prefix(p, 'i', jvm_get_postfix(item -> result)); + break; default: gen_error("push_item: unk mode %i", item -> mode); break; @@ -1252,6 +1343,19 @@ jvm_generate_compare_op(gen_proc_t * p, oberon_type_t * t, int op) jvm_generate(p, 2 * cell_size, 1, "%ccmp%c", prefix, fop); jvm_generate(p, 1, 1, "if%s L%i", cmpop, label_true); } + else if(prefix == 'a') + { + if(t -> class == OBERON_TYPE_STRING + || (t -> class == OBERON_TYPE_POINTER && t -> base -> class == OBERON_TYPE_CHAR)) + { + jvm_generate(p, 2, 1, "invokestatic SYSTEM/STRCMP([B[B)I"); + jvm_generate(p, 1, 0, "if%s L%i", cmpop, label_true); + } + else + { + jvm_generate(p, 1, 0, "if_acmp%s L%i", cmpop, label_true); + } + } else { jvm_generate(p, 2 * cell_size, 0, "if_%ccmp%s L%i", prefix, cmpop, label_true); @@ -1284,6 +1388,9 @@ jvm_generate_operator(gen_proc_t * p, oberon_type_t * t, int op) case OP_ABS: jvm_generate_abs(p, prefix); break; + case OP_CAP: + jvm_generate(p, cell_size, cell_size, "invokestatic java/lang/Character/toUpperCase(I)I"); + break; case OP_ADD: jvm_generate(p, 2 * cell_size, cell_size, "%cadd", prefix); @@ -1307,6 +1414,7 @@ jvm_generate_operator(gen_proc_t * p, oberon_type_t * t, int op) jvm_generate(p, 2 * cell_size, cell_size, "%cand", prefix); break; case OP_DIFFERENCE: + /* (a - b) == a & ~b */ jvm_generate_push_int_size(p, -1, t -> size); jvm_generate(p, 2 * cell_size, cell_size, "%cxor", prefix); jvm_generate(p, 2 * cell_size, cell_size, "%cand", prefix); @@ -1389,10 +1497,12 @@ jvm_generate_range(gen_proc_t * p, oberon_expr_t * a, oberon_expr_t * b) } else { - ra = oberon_generator_new_var(); - rb = oberon_generator_new_var(); - jvm_generate_and_init_local_var(p, ra, type -> gen_type); - jvm_generate_and_init_local_var(p, rb, type -> gen_type); + ra = jvm_create_function_temp_var(p, type -> gen_type); + rb = jvm_create_function_temp_var(p, type -> gen_type); + jvm_generate_var(ra); + jvm_generate_var(rb); + jvm_generate_var_initialization(p, ra); + jvm_generate_var_initialization(p, rb); label_else = jvm_new_label_id(p); label_end = jvm_new_label_id(p); @@ -1431,6 +1541,7 @@ jvm_generate_in(gen_proc_t * p, oberon_expr_t * a, oberon_expr_t * b) int label_else = jvm_new_label_id(p); int label_end = jvm_new_label_id(p); + /* (a IN b) == (1 << a) & b */ jvm_generate_push_int_size(p, 1, t -> size); push_expr(p, a); jvm_generate(p, 2 * cell_size, cell_size, "%cshl", prefix); @@ -1450,6 +1561,51 @@ jvm_generate_in(gen_proc_t * p, oberon_expr_t * a, oberon_expr_t * b) jvm_generate_label(p, label_end); } +static void +jvm_generate_ash(gen_proc_t * p, oberon_expr_t * a, oberon_expr_t * b) +{ + oberon_type_t * t = a -> result; + int cell_size = jvm_cell_size_for_type(t); + char prefix = jvm_get_prefix(t); + int label_else = jvm_new_label_id(p); + int label_end = jvm_new_label_id(p); + + /* if b < 0 then a << b else a >> b end */ + + push_expr(p, a); + push_expr(p, b); + if(cell_size == 1) + { + jvm_generate(p, cell_size, 2 * cell_size, "dup"); + } + else + { + jvm_generate(p, cell_size, 2 * cell_size, "dup2"); + } + jvm_generate_push_int_size(p, 0, t -> size); + jvm_generate_compare_op(p, t, OP_LSS); + + jvm_generate(p, cell_size, 0, "ifne L%i", label_else); + jvm_generate(p, 2 * cell_size, cell_size, "%cshl", prefix); + jvm_generate(p, 0, 0, "goto L%i", label_end); + jvm_generate_label(p, label_else); + jvm_generate_abs(p, prefix); + jvm_generate(p, 2 * cell_size, cell_size, "%cshr", prefix); + jvm_generate_label(p, label_end); +} + +static void +jvm_generate_entier(gen_proc_t * p, oberon_expr_t * x, oberon_type_t * res) +{ + char prefix = jvm_get_prefix(x -> result); + char postfix = jvm_get_postfix(res); + + push_expr(p, x); + jvm_generate_cast_prefix(p, prefix, 'd'); + jvm_generate(p, 2, 2, "invokestatic java/lang/Math/floor(D)D"); + jvm_generate_cast_prefix(p, 'd', postfix); +} + static void push_operator(gen_proc_t * p, oberon_oper_t * oper) { @@ -1465,9 +1621,13 @@ push_operator(gen_proc_t * p, oberon_oper_t * oper) case OP_UNARY_MINUS: case OP_LOGIC_NOT: case OP_ABS: + case OP_CAP: push_expr(p, oper -> left); jvm_generate_operator(p, preq, op); break; + case OP_ENTIER: + jvm_generate_entier(p, oper -> left, oper -> result); + break; case OP_ADD: case OP_SUB: @@ -1510,6 +1670,10 @@ push_operator(gen_proc_t * p, oberon_oper_t * oper) case OP_IN: jvm_generate_in(p, oper -> left, oper -> right); break; + + case OP_ASH: + jvm_generate_ash(p, oper -> left, oper -> right); + break; default: gen_error("push_oper: unk op %i", op); break; @@ -1592,27 +1756,82 @@ oberon_generate_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_ } void -oberon_generate_code(oberon_context_t * ctx) +oberon_generate_copy(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst) +{ + gen_module_t * m; + gen_proc_t * p; + char * desc; + + m = ctx -> mod -> gen_mod; + p = m -> class -> p; + + push_expr(p, src); + push_expr(p, dst); + + desc = jvm_get_descriptor(dst -> result); + + jvm_generate(p, 2, 0, "invokestatic SYSTEM/COPY(%s%s)V", desc, desc); +} + +void +oberon_generate_assert(oberon_context_t * ctx, oberon_expr_t * cond) +{ + gen_module_t * m; + gen_proc_t * p; + + m = ctx -> mod -> gen_mod; + p = m -> class -> p; + + push_expr(p, cond); + jvm_generate(p, 1, 0, "invokestatic SYSTEM/ASSERT(Z)V"); +} + +void +oberon_generate_assert_n(oberon_context_t * ctx, oberon_expr_t * cond, int64_t n) { - printf("generate code\n"); + gen_module_t * m; + gen_proc_t * p; + + m = ctx -> mod -> gen_mod; + p = m -> class -> p; + + push_expr(p, cond); + jvm_generate_push_int_size(p, n, 8); + jvm_generate(p, 1 + 2, 0, "invokestatic SYSTEM/ASSERT(ZJ)V"); } void -oberon_generator_dump(oberon_context_t * ctx, char * path) +oberon_generate_trap(oberon_context_t * ctx, int64_t n) { - printf("jit: dump code\n"); + gen_module_t * m; + gen_proc_t * p; + + m = ctx -> mod -> gen_mod; + p = m -> class -> p; + + jvm_generate_push_int_size(p, n, 8); + jvm_generate(p, 2, 0, "invokestatic SYSTEM/TRAP(J)V"); } -void * -oberon_generator_get_procedure(oberon_context_t * ctx, const char * name) +void +oberon_generate_halt(oberon_context_t * ctx, int64_t n) { - printf("jit: get pointer to procedure %s\n", name); - return NULL; + gen_module_t * m; + gen_proc_t * p; + + m = ctx -> mod -> gen_mod; + p = m -> class -> p; + + jvm_generate_push_int_size(p, n, 8); + jvm_generate(p, 2, 0, "invokestatic SYSTEM/HALT(J)V"); } -void * -oberon_generator_get_var(oberon_context_t * ctx, const char * name) +void +oberon_set_out_directory(oberon_context_t * ctx, const char * path) { - printf("jit: get pointer to var %s\n", name); - return NULL; + gen_context_t * c; + + c = ctx -> gen_context; + + c -> dir = new_string(path); }