X-Git-Url: http://deadsoftware.ru/gitweb?p=dsw-obn.git;a=blobdiff_plain;f=generator.c;h=685786f19f9af12ff75fdc30ab16801a75cb922d;hp=07e7bc470ee1b1fb9a337810e695fce530bf16d3;hb=063ba732ee8778c34a3781270b18d52481dbf0cd;hpb=d465a8b1fddbbabcf4f9739e80e9e82dc945028e diff --git a/generator.c b/generator.c index 07e7bc4..685786f 100644 --- a/generator.c +++ b/generator.c @@ -10,19 +10,6 @@ #include -/* - * oberon_object_t -> gen_var == gcc_jit_lvalue; - * oberon_type_t -> gen_type == gcc_jit_type; - * oberon_context_t -> gen_context == gen_context_t; - */ - -typedef struct -{ - gcc_jit_context * gcc_context; - gcc_jit_block * gcc_block; - gcc_jit_result * gcc_result; -} gen_context_t; - static void printcontext(oberon_context_t * ctx, char * s) { /* @@ -71,43 +58,80 @@ oberon_generator_destroy_context(oberon_context_t * ctx) void oberon_generator_init_type(oberon_context_t * ctx, oberon_type_t * type) { - printcontext(ctx, "oberon_generator_init_type"); + gen_type_t * gen_type = malloc(sizeof *gen_type); + memset(gen_type, 0, sizeof *gen_type); + type -> gen_type = gen_type; gen_context_t * gen_context = ctx -> gen_context; gcc_jit_context * gcc_context = gen_context -> gcc_context; - gcc_jit_type * gen_type; - if(type -> class == OBERON_TYPE_INTEGER) + gcc_jit_type * gcc_type; + if(type -> class == OBERON_TYPE_VOID) { - gen_type = gcc_jit_context_get_int_type(gcc_context, type -> size, 1); + gcc_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_VOID); + } + else if(type -> class == OBERON_TYPE_INTEGER) + { + gcc_type = gcc_jit_context_get_int_type(gcc_context, type -> size, 1); } else if(type -> class == OBERON_TYPE_BOOLEAN) { - gen_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_BOOL); + gcc_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_BOOL); + } + else if(type -> class == OBERON_TYPE_PROCEDURE) + { + gcc_type = NULL; // not used + } + else if(type -> class == OBERON_TYPE_ARRAY) + { + if(type -> dim != 1) + { + oberon_error(ctx, "multidimension and open arrays not supported"); + } + + gen_type_t * gen_base = type -> base -> gen_type; + gcc_jit_type * gcc_base = gen_base -> gcc_type; + + gcc_type = gcc_jit_context_new_array_type(gcc_context, NULL, gcc_base, type -> size); } else { oberon_error(ctx, "oberon_generator_init_type: invalid type class %i", type -> class); } - type -> gen_type = gen_type; + gen_type -> gcc_type = gcc_type; } void oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var) { - printcontext(ctx, "oberon_generator_init_var"); - assert(var -> class == OBERON_CLASS_VAR); - gen_context_t * gen_context = ctx -> gen_context; + gen_type_t * gen_type = var -> type -> gen_type; + + gen_var_t * gen_var = malloc(sizeof *gen_var); + memset(gen_var, 0, sizeof *gen_var); + var -> gen_var = gen_var; + gcc_jit_context * gcc_context = gen_context -> gcc_context; - gcc_jit_type * gen_type = var -> type -> gen_type; + gcc_jit_type * gcc_type = gen_type -> gcc_type; const char * name = var -> name; - gcc_jit_lvalue * gen_var; - gen_var = gcc_jit_context_new_global(gcc_context, NULL, GCC_JIT_GLOBAL_INTERNAL, gen_type, name); - - var -> gen_var = gen_var; + // TODO var param + if(var -> class == OBERON_CLASS_VAR) + { + gen_var -> gcc_lvalue = gcc_jit_context_new_global( + gcc_context, NULL, GCC_JIT_GLOBAL_INTERNAL, gcc_type, name + ); + } + else if(var -> class == OBERON_CLASS_PARAM) + { + gen_var -> gcc_param = gcc_jit_context_new_param(gcc_context, NULL, gcc_type, name); + gen_var -> gcc_lvalue = gcc_jit_param_as_lvalue(gen_var -> gcc_param); + } + else + { + oberon_error(ctx, "oberon_generator_init_var: invalid class %i", var -> class); + } } void @@ -117,18 +141,32 @@ oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * proc) gen_context_t * gen_context = ctx -> gen_context; gcc_jit_context * gcc_context = gen_context -> gcc_context; - //gcc_jit_type * gen_type = proc -> type -> gen_type; - const char * name = proc -> name; - gcc_jit_function * gen_proc; + gen_proc_t * gen_proc = malloc(sizeof *gen_proc); + memset(gen_proc, 0, sizeof *gen_proc); + proc -> gen_proc = gen_proc; - // TODO make real signature - gcc_jit_type * void_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_VOID); - gen_proc = gcc_jit_context_new_function( - gcc_context, NULL, GCC_JIT_FUNCTION_EXPORTED, void_type, name, 0, NULL, 0 + const char * name = proc -> name; + gen_type_t * gen_result_type = proc -> type -> base -> gen_type; + gcc_jit_type * result_type = gen_result_type -> gcc_type; + + /* Строим список параметров */ + int num_param = proc -> type -> num_decl; + oberon_object_t * o = proc -> type -> decl; + gcc_jit_param * params[num_param]; + for(int i = 0; i < num_param; i++) + { + gen_var_t * param_var = o -> gen_var; + params[i] = param_var -> gcc_param; + o = o -> next; + } + + gcc_jit_function * gcc_func; + gcc_func = gcc_jit_context_new_function( + gcc_context, NULL, GCC_JIT_FUNCTION_EXPORTED, result_type, name, num_param, params, 0 ); - proc -> gen_proc = gen_proc; + gen_proc -> gcc_func = gcc_func; } // ======================================================================= @@ -171,8 +209,9 @@ void oberon_generate_begin_proc(oberon_context_t * ctx, oberon_object_t * proc) { gen_context_t * gen_context = ctx -> gen_context; + gen_proc_t * gen_proc = proc -> gen_proc; - gcc_jit_function * func = proc -> gen_proc; + gcc_jit_function * func = gen_proc -> gcc_func; gcc_jit_block * gcc_block = gcc_jit_function_new_block(func, NULL); // TODO make stack for block @@ -194,11 +233,24 @@ void oberon_generate_end_proc(oberon_context_t * ctx) { gen_context_t * gen_context = ctx -> gen_context; - gcc_jit_block * gcc_block = gen_context -> gcc_block; + gen_context -> gcc_block = NULL; +} - gcc_jit_block_end_with_void_return(gcc_block, NULL); +void +oberon_generate_return(oberon_context_t * ctx, oberon_expr_t * expr) +{ + gen_context_t * gen_context = ctx -> gen_context; + gcc_jit_block * gcc_block = gen_context -> gcc_block; - gen_context -> gcc_block = NULL; + if(expr == NULL) + { + gcc_jit_block_end_with_void_return(gcc_block, NULL); + } + else + { + gcc_jit_rvalue * r = rvalue_from_expr(ctx, expr); + gcc_jit_block_end_with_return(gcc_block, NULL, r); + } } static gcc_jit_lvalue * @@ -214,7 +266,8 @@ lvalue_from_expr(oberon_context_t *ctx, oberon_expr_t * expr) { oberon_error(ctx, "invalid lvalue expression"); } - left = item -> var -> gen_var; + gen_var_t * gen_var = item -> var -> gen_var; + left = gen_var -> gcc_lvalue; } else { @@ -233,9 +286,10 @@ rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item) gcc_jit_rvalue * right; if(item -> mode == MODE_VAR) { - assert(item -> var -> class == OBERON_CLASS_VAR); - gcc_jit_lvalue * gen_var = item -> var -> gen_var; - right = gcc_jit_lvalue_as_rvalue(gen_var); + assert(item -> var -> class == OBERON_CLASS_VAR + || item -> var -> class == OBERON_CLASS_PARAM); + gen_var_t * gen_var = item -> var -> gen_var; + right = gcc_jit_lvalue_as_rvalue(gen_var -> gcc_lvalue); } else if(item -> mode == MODE_INTEGER) { @@ -257,10 +311,22 @@ rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item) else if(item -> mode == MODE_CALL) { assert(item -> var -> class == OBERON_CLASS_PROC); - /* TODO args */ - gcc_jit_function * func = item -> var -> gen_proc; + + gen_proc_t * gen_proc = item -> var -> gen_proc; + + int num_args = item -> num_args; + gcc_jit_rvalue *args[num_args]; + + oberon_expr_t * expr = item -> args; + for(int i = 0; i < num_args; i++) + { + args[i] = rvalue_from_expr(ctx, expr); + expr = expr -> next; + } + + gcc_jit_function * func = gen_proc -> gcc_func; right = gcc_jit_context_new_call( - gcc_context, NULL, func, 0, NULL + gcc_context, NULL, func, num_args, args ); } else @@ -303,8 +369,9 @@ rvalue_from_operator(oberon_context_t * ctx, oberon_oper_t * operator) gcc_jit_rvalue * right; gen_context_t * gen_context = ctx -> gen_context; + gen_type_t * gen_type = operator -> result -> gen_type; gcc_jit_context * gcc_context = gen_context -> gcc_context; - gcc_jit_type * result_type = operator -> result -> gen_type; + gcc_jit_type * result_type = gen_type -> gcc_type; int expr_type = op_table[operator -> op].type; if(expr_type == 0)