X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=generator.c;h=b0887aee7c82587beed212c50e56cac179a32b72;hb=e763da864f7330c2b53029782c6b0d85543eb4d2;hp=419a2b22c4c2dbb765a9656163f701c645637eb8;hpb=2d029d2c2b27639e3a2b6c43e63788b00110818e;p=dsw-obn.git diff --git a/generator.c b/generator.c index 419a2b2..b0887ae 100644 --- a/generator.c +++ b/generator.c @@ -4,23 +4,24 @@ #include #include #include +#include + +#include #include "oberon.h" #include "generator.h" -static void printcontext(oberon_context_t * ctx, char * s) +// ======================================================================= +// INTERNAL FUNCTIONS +// ======================================================================= + +static void * +__OBERON_ALLOC__ (size_t bytes) { -/* - gen_context_t * gen_context = ctx -> gen_context; - gcc_jit_context * gcc_context = gen_context -> gcc_context; - gcc_jit_block * gcc_block = gen_context -> gcc_block; - - printf("%s:\n", s); - printf(" ctx = %p:\n", ctx); - printf(" gctx = %p:\n", gctx); - printf(" context = %p:\n", context); - printf(" block = %p:\n", block); -*/ + void * p = GC_MALLOC(bytes); + memset(p, 0, bytes); + printf("allocated %lu bytes\n", bytes); + return p; } // ======================================================================= @@ -57,14 +58,19 @@ oberon_generator_init_context(oberon_context_t * ctx) ctx -> gen_context = gen_context; gen_context -> gcc_context = gcc_context; - printcontext(ctx, "oberon_generator_init_context"); + gcc_jit_type * void_ptr_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_VOID_PTR); + gcc_jit_type * size_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_SIZE_T); + gcc_jit_type * alloc_ptr_type = gcc_jit_context_new_function_ptr_type( + gcc_context, NULL, void_ptr_type, 1, &size_type, 0 + ); + gen_context -> gcc_alloc = gcc_jit_context_new_global( + gcc_context, NULL, GCC_JIT_GLOBAL_EXPORTED, alloc_ptr_type, "__OBERON_ALLOC__" + ); } void oberon_generator_destroy_context(oberon_context_t * ctx) { - printcontext(ctx, "oberon_generator_destroy_context"); - gen_context_t * gen_context = ctx -> gen_context; gcc_jit_context * gcc_context = gen_context -> gcc_context; @@ -93,7 +99,33 @@ oberon_generator_init_type(oberon_context_t * ctx, oberon_type_t * type) } else if(type -> class == OBERON_TYPE_BOOLEAN) { - gcc_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_BOOL); + if(type -> size == sizeof(bool)) + { + gcc_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_BOOL); + } + else + { + oberon_error(ctx, "generator: unsupported boolean size"); + } + } + else if(type -> class == OBERON_TYPE_REAL) + { + if(type -> size == sizeof(float)) + { + gcc_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_FLOAT); + } + else if(type -> size == sizeof(double)) + { + gcc_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_DOUBLE); + } + else if(type -> size == sizeof(long double)) + { + gcc_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_LONG_DOUBLE); + } + else + { + oberon_error(ctx, "generator: unsupported real size"); + } } else if(type -> class == OBERON_TYPE_ARRAY) { @@ -103,7 +135,11 @@ oberon_generator_init_type(oberon_context_t * ctx, oberon_type_t * type) } else if(type -> class == OBERON_TYPE_RECORD) { - gcc_struct = gcc_jit_context_new_opaque_struct(gcc_context, NULL, ""); + char name[32]; + snprintf(name, 32, "RECORD%u", gen_context -> record_count); + gen_context -> record_count += 1; + + gcc_struct = gcc_jit_context_new_opaque_struct(gcc_context, NULL, name); gcc_type = gcc_jit_struct_as_type(gcc_struct); } else if(type -> class == OBERON_TYPE_POINTER) @@ -167,6 +203,46 @@ oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * type) //gcc_struct = gcc_jit_context_new_struct_type(gcc_context, NULL, "", num_fields, fields); } +static void +oberon_generator_get_full_name(char * name, int max_len, oberon_object_t * x) +{ + if(!x) + { + name[0] = 0; + return; + } + + int add_module_prefix; + switch(x -> class) + { + case OBERON_CLASS_FIELD: + case OBERON_CLASS_PARAM: + case OBERON_CLASS_VAR_PARAM: + /* В локальных областях префиксы излишни */ + add_module_prefix = 0; + break; + default: + add_module_prefix = 1; + break; + } + + char parent[256]; + oberon_generator_get_full_name(parent, 256, x -> parent); + + if(strlen(parent) > 0) + { + snprintf(name, max_len, "%s_%s", parent, x -> name); + } + else if(add_module_prefix) + { + snprintf(name, max_len, "%s_%s", x -> module -> name, x -> name); + } + else + { + snprintf(name, max_len, "%s", x -> name); + } +} + void oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var) { @@ -179,7 +255,9 @@ oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var) gcc_jit_context * gcc_context = gen_context -> gcc_context; gcc_jit_type * gcc_type = gen_type -> gcc_type; - const char * name = var -> name; + + char name[256]; + oberon_generator_get_full_name(name, 256, var); gcc_jit_lvalue * gcc_lvalue = NULL; gcc_jit_param * gcc_param = NULL; @@ -196,7 +274,7 @@ oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var) else { gcc_lvalue = gcc_jit_context_new_global( - gcc_context, NULL, GCC_JIT_GLOBAL_INTERNAL, gcc_type, name + gcc_context, NULL, GCC_JIT_GLOBAL_EXPORTED, gcc_type, name ); } } @@ -240,7 +318,9 @@ oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * proc) memset(gen_proc, 0, sizeof *gen_proc); proc -> gen_proc = gen_proc; - const char * name = proc -> name; + char name[256]; + oberon_generator_get_full_name(name, 256, proc); + gen_type_t * gen_result_type = proc -> type -> base -> gen_type; gcc_jit_type * result_type = gen_result_type -> gcc_type; @@ -270,17 +350,66 @@ oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * proc) static gcc_jit_rvalue * rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item); static gcc_jit_rvalue * rvalue_from_expr(oberon_context_t * ctx, oberon_expr_t * expr); +static int +oberon_generator_get_type_size(oberon_context_t * ctx, oberon_type_t * type) +{ + int size = 0; + switch(type -> class) + { + case OBERON_TYPE_INTEGER: + size = type -> size; + printf("int size: %i\n", size); + break; + case OBERON_TYPE_BOOLEAN: + size = sizeof(bool); + printf("bool size: %i\n", size); + break; + case OBERON_TYPE_PROCEDURE: + case OBERON_TYPE_POINTER: + size = sizeof(void*); + printf("ptr size: %i\n", size); + break; + case OBERON_TYPE_ARRAY: + size = type -> size; + type = type -> base; + size = (size == 0) ? (1) : (size); + size *= oberon_generator_get_type_size(ctx, type); + printf("array size: %i\n", size); + break; + case OBERON_TYPE_RECORD: + { + int num = type -> num_decl; + oberon_object_t * arg = type -> decl; + for(int i = 0; i < num; i++) + { + oberon_type_t * x; + x = arg -> type; + size += oberon_generator_get_type_size(ctx, x); + arg = arg -> next; + } + } + printf("struct size: %i\n", size); + break; + default: + oberon_error(ctx, "oberon_generator_get_type_size: wat"); + break; + } + + return size; +} + void oberon_generate_begin_module(oberon_context_t * ctx) { - printcontext(ctx, "oberon_generate_begin_module"); - gen_context_t * gen_context = ctx -> gen_context; gcc_jit_context * gcc_context = gen_context -> gcc_context; + char name[256]; + snprintf(name, 256, "%s_BEGIN", ctx -> mod -> name); + gcc_jit_type * void_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_VOID); gcc_jit_function * func = gcc_jit_context_new_function( - gcc_context, NULL, GCC_JIT_FUNCTION_EXPORTED, void_type, "BEGIN", 0, NULL, 0 + gcc_context, NULL, GCC_JIT_FUNCTION_EXPORTED, void_type, name, 0, NULL, 0 ); gcc_jit_block * gcc_block = gcc_jit_function_new_block(func, NULL); @@ -290,8 +419,6 @@ oberon_generate_begin_module(oberon_context_t * ctx) void oberon_generate_end_module(oberon_context_t * ctx) { - printcontext(ctx, "oberon_generate_end_module"); - gen_context_t * gen_context = ctx -> gen_context; gcc_jit_block * gcc_block = gen_context -> block -> gcc_block; @@ -359,6 +486,11 @@ lvalue_from_item(oberon_context_t * ctx, oberon_item_t * item) if(item -> mode == MODE_VAR) { + if(item -> var -> class == OBERON_CLASS_PROC) + { + oberon_error(ctx, "casting static procedure to pointer not supported by generator"); + } + gen_var_t * gen_var = item -> var -> gen_var; left = gen_var -> gcc_lvalue; if(item -> var -> class == OBERON_CLASS_VAR_PARAM) @@ -390,7 +522,7 @@ lvalue_from_item(oberon_context_t * ctx, oberon_item_t * item) } else { - oberon_error(ctx, "invalid lvalue expression"); + oberon_error(ctx, "lvalue_from_item: invalid mode %i", item -> mode); } return left; @@ -446,8 +578,6 @@ rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item) } else if(item -> mode == MODE_CALL) { - assert(item -> var -> class == OBERON_CLASS_PROC); - oberon_type_t * signature = item -> var -> type; gen_proc_t * gen_proc = item -> var -> gen_proc; @@ -471,10 +601,28 @@ rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item) arg_param = arg_param -> next; } - gcc_jit_function * func = gen_proc -> gcc_func; - right = gcc_jit_context_new_call( - gcc_context, NULL, func, num_args, args - ); + gcc_jit_rvalue * fnptr; + gcc_jit_function * func; + switch(item -> var -> class) + { + case OBERON_CLASS_PROC: + func = gen_proc -> gcc_func; + right = gcc_jit_context_new_call( + gcc_context, NULL, func, num_args, args + ); + break; + case OBERON_CLASS_VAR: + case OBERON_CLASS_VAR_PARAM: + case OBERON_CLASS_PARAM: + fnptr = gcc_jit_lvalue_as_rvalue(item -> var -> gen_var -> gcc_lvalue); + right = gcc_jit_context_new_call_through_ptr( + gcc_context, NULL, fnptr, num_args, args + ); + break; + default: + assert(0); + break; + } } else if(item -> mode == MODE_INDEX) { @@ -499,6 +647,38 @@ rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item) gcc_jit_type * type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_VOID_PTR); right = gcc_jit_context_null(gcc_context, type); } + else if(item -> mode == MODE_NEW) + { + assert(item -> result -> class == OBERON_TYPE_POINTER); + + oberon_type_t * type = item -> result -> base; + int type_size = oberon_generator_get_type_size(ctx, type); + int array_size = type_size; + + int num = item -> num_args; + oberon_expr_t * arg = item -> args; + for(int i = 0; i < num; i++) + { + array_size *= arg -> item.integer; + arg = arg -> next; + } + + gcc_jit_type * size_type; + size_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_SIZE_T); + + gcc_jit_rvalue * fnarg; + fnarg = gcc_jit_context_new_rvalue_from_int(gcc_context, size_type, array_size); + + gcc_jit_type * result_type = item -> result -> gen_type -> gcc_type; + gcc_jit_rvalue * gcc_alloc = gcc_jit_lvalue_as_rvalue(gen_context -> gcc_alloc); + right = gcc_jit_context_new_call_through_ptr(gcc_context, NULL, gcc_alloc, 1, &fnarg); + right = gcc_jit_context_new_cast(gcc_context, NULL, right, result_type); + } + else if(item -> mode == MODE_REAL) + { + gcc_jit_type * int_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_FLOAT); + right = gcc_jit_context_new_rvalue_from_int(gcc_context, int_type, item -> real); + } else { oberon_error(ctx, "rvalue_from_item: invalid mode %i", item -> mode); @@ -515,14 +695,19 @@ struct { enum gcc_jit_comparison comp_op; }; } op_table[] = { - { 0, .unary_op = GCC_JIT_UNARY_OP_LOGICAL_NEGATE }, { 0, .unary_op = GCC_JIT_UNARY_OP_MINUS }, + { 0, .unary_op = GCC_JIT_UNARY_OP_BITWISE_NEGATE }, + { 0, .unary_op = GCC_JIT_UNARY_OP_LOGICAL_NEGATE }, + { 0, .unary_op = GCC_JIT_UNARY_OP_ABS }, { 1, .binary_op = GCC_JIT_BINARY_OP_PLUS }, { 1, .binary_op = GCC_JIT_BINARY_OP_MINUS }, { 1, .binary_op = GCC_JIT_BINARY_OP_MULT }, { 1, .binary_op = GCC_JIT_BINARY_OP_DIVIDE }, { 1, .binary_op = GCC_JIT_BINARY_OP_MODULO }, + { 1, .binary_op = GCC_JIT_BINARY_OP_BITWISE_AND }, + { 1, .binary_op = GCC_JIT_BINARY_OP_BITWISE_XOR }, + { 1, .binary_op = GCC_JIT_BINARY_OP_BITWISE_OR }, { 1, .binary_op = GCC_JIT_BINARY_OP_LOGICAL_AND }, { 1, .binary_op = GCC_JIT_BINARY_OP_LOGICAL_OR }, @@ -613,6 +798,8 @@ oberon_generate_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_ } } + printf("oberon_generate_assign: class %i := class %i\n", dst -> result -> class, src -> result -> class); + gen_context_t * gen_context = ctx -> gen_context; gen_block_t * gen_block = gen_context -> block; gcc_jit_block * gcc_block = gen_block -> gcc_block; @@ -629,7 +816,12 @@ oberon_generate_code(oberon_context_t * ctx) gcc_result = gcc_jit_context_compile(gcc_context); gen_context -> gcc_result = gcc_result; - ctx -> mod -> begin = gcc_jit_result_get_code(gcc_result, "BEGIN"); + + typedef void * (*TOberonAlloc)(size_t); + TOberonAlloc * fn_alloc_ptr = gcc_jit_result_get_global(gcc_result, "__OBERON_ALLOC__"); + *fn_alloc_ptr = __OBERON_ALLOC__; + +// ctx -> mod -> begin = gcc_jit_result_get_code(gcc_result, "BEGIN"); } void @@ -639,3 +831,21 @@ oberon_generator_dump(oberon_context_t * ctx, char * path) gcc_jit_context * gcc_context = gen_context -> gcc_context; gcc_jit_context_dump_to_file(gcc_context, path, 0); } + +void * +oberon_generator_get_procedure(oberon_context_t * ctx, const char * name) +{ + gen_context_t * gen_context = ctx -> gen_context; + gcc_jit_result * gcc_result = gen_context -> gcc_result; + + return gcc_jit_result_get_code(gcc_result, name); +} + +void * +oberon_generator_get_var(oberon_context_t * ctx, const char * name) +{ + gen_context_t * gen_context = ctx -> gen_context; + gcc_jit_result * gcc_result = gen_context -> gcc_result; + + return gcc_jit_result_get_global(gcc_result, name); +}