X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=generator.c;h=709e5a65cbfb513cd502e2bf7d70383b89468449;hb=1bf625553dc35ac4a5c1afceb6950fd44776a424;hp=e4970fd995b0e5af87d12cb93f5c6d36bfe24359;hpb=342c8f1a44765e744c64e14a3b8f1aa4031c5f62;p=dsw-obn.git diff --git a/generator.c b/generator.c index e4970fd..709e5a6 100644 --- a/generator.c +++ b/generator.c @@ -8,8 +8,6 @@ #include "oberon.h" #include "generator.h" -#include - static void printcontext(oberon_context_t * ctx, char * s) { /* @@ -29,6 +27,24 @@ static void printcontext(oberon_context_t * ctx, char * s) // ALLOC // ======================================================================= +static void +oberon_generator_open_block(gen_context_t * gen_context, gcc_jit_block * gcc_block) +{ + gen_block_t * block = malloc(sizeof *block); + memset(block, 0, sizeof *block); + + block -> gcc_block = gcc_block; + block -> up = gen_context -> block; + + gen_context -> block = block; +} + +static void +oberon_generator_close_block(gen_context_t * gen_context) +{ + gen_context -> block = gen_context -> block -> up; +} + void oberon_generator_init_context(oberon_context_t * ctx) { @@ -79,6 +95,27 @@ oberon_generator_init_type(oberon_context_t * ctx, oberon_type_t * type) { gcc_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_BOOL); } + else if(type -> class == OBERON_TYPE_ARRAY) + { + 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 if(type -> class == OBERON_TYPE_RECORD) + { + 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) + { + gen_type_t * gen_base = type -> base -> gen_type; + gcc_jit_type * gcc_base = gen_base -> gcc_type; + gcc_type = gcc_jit_type_get_pointer(gcc_base); + } else if(type -> class == OBERON_TYPE_PROCEDURE) { int num_params = type -> num_decl; @@ -98,45 +135,42 @@ oberon_generator_init_type(oberon_context_t * ctx, oberon_type_t * type) gcc_context, NULL, result_type, num_params, params, 0 ); } - 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 if(type -> class == OBERON_TYPE_RECORD) - { - // TODO type exstension - - int num_fields = type -> num_decl; - gcc_jit_field * fields[num_fields]; - oberon_object_t * o = type -> decl; - for(int i = 0; i < num_fields; i++) - { - assert(o -> class == OBERON_CLASS_FIELD); - gen_var_t * var = o -> gen_var; - fields[i] = var -> gcc_field; - o = o -> next; - } - - gcc_struct = gcc_jit_context_new_struct_type(gcc_context, NULL, "", num_fields, fields); - gcc_type = gcc_jit_struct_as_type(gcc_struct); - } else { oberon_error(ctx, "oberon_generator_init_type: invalid type class %i", type -> class); } + assert(gcc_type); gen_type -> gcc_type = gcc_type; gen_type -> gcc_struct = gcc_struct; } +void +oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * type) +{ + assert(type -> class == OBERON_TYPE_RECORD); + + gen_type_t * gen_type = type -> gen_type; + gcc_jit_struct * gcc_struct = gen_type -> gcc_struct; + + // TODO type exstension + + int num_fields = type -> num_decl; + gcc_jit_field * fields[num_fields]; + oberon_object_t * o = type -> decl; + for(int i = 0; i < num_fields; i++) + { + assert(o -> class == OBERON_CLASS_FIELD); + gen_var_t * var = o -> gen_var; + fields[i] = var -> gcc_field; + o = o -> next; + } + + gcc_jit_struct_set_fields (gcc_struct, NULL, num_fields, fields); + + //gcc_struct = gcc_jit_context_new_struct_type(gcc_context, NULL, "", num_fields, fields); +} + void oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var) { @@ -151,21 +185,36 @@ oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var) gcc_jit_type * gcc_type = gen_type -> gcc_type; const char * name = var -> name; - // TODO var param gcc_jit_lvalue * gcc_lvalue = NULL; gcc_jit_param * gcc_param = NULL; gcc_jit_field * gcc_field = NULL; if(var -> class == OBERON_CLASS_VAR) { - gcc_lvalue = gcc_jit_context_new_global( - gcc_context, NULL, GCC_JIT_GLOBAL_INTERNAL, gcc_type, name - ); + if(var -> local) + { + gen_proc_t * gen_func = var -> parent -> gen_proc; + gcc_jit_function * func = gen_func -> gcc_func; + + gcc_lvalue = gcc_jit_function_new_local(func, NULL, gcc_type, name); + } + else + { + gcc_lvalue = gcc_jit_context_new_global( + gcc_context, NULL, GCC_JIT_GLOBAL_INTERNAL, gcc_type, name + ); + } } else if(var -> class == OBERON_CLASS_PARAM) { gcc_param = gcc_jit_context_new_param(gcc_context, NULL, gcc_type, name); gcc_lvalue = gcc_jit_param_as_lvalue(gcc_param); } + else if(var -> class == OBERON_CLASS_VAR_PARAM) + { + gcc_type = gcc_jit_type_get_pointer(gcc_type); + gcc_param = gcc_jit_context_new_param(gcc_context, NULL, gcc_type, name); + gcc_lvalue = gcc_jit_param_as_lvalue(gcc_param); + } else if(var -> class == OBERON_CLASS_FIELD) { gcc_field = gcc_jit_context_new_field(gcc_context, NULL, gcc_type, name); @@ -183,7 +232,10 @@ oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var) void oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * proc) { - assert(proc -> class == OBERON_CLASS_PROC); + if(proc -> local) + { + oberon_error(ctx, "generator: local procedures not supported"); + } gen_context_t * gen_context = ctx -> gen_context; gcc_jit_context * gcc_context = gen_context -> gcc_context; @@ -236,7 +288,7 @@ oberon_generate_begin_module(oberon_context_t * ctx) ); gcc_jit_block * gcc_block = gcc_jit_function_new_block(func, NULL); - gen_context -> gcc_block = gcc_block; + oberon_generator_open_block(gen_context, gcc_block); } void @@ -245,11 +297,11 @@ 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 -> gcc_block; + gcc_jit_block * gcc_block = gen_context -> block -> gcc_block; gcc_jit_block_end_with_void_return(gcc_block, NULL); - gen_context -> gcc_block = NULL; + oberon_generator_close_block(gen_context); } void @@ -261,15 +313,15 @@ oberon_generate_begin_proc(oberon_context_t * ctx, oberon_object_t * 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 - gen_context -> gcc_block = gcc_block; + oberon_generator_open_block(gen_context, gcc_block); } void oberon_generate_call_proc(oberon_context_t * ctx, oberon_expr_t * desig) { gen_context_t * gen_context = ctx -> gen_context; - gcc_jit_block * block = gen_context -> gcc_block; + gen_block_t * gen_block = gen_context -> block; + gcc_jit_block * block = gen_block -> gcc_block; gcc_jit_rvalue * return_value; return_value = rvalue_from_expr(ctx, desig); @@ -280,14 +332,15 @@ void oberon_generate_end_proc(oberon_context_t * ctx) { gen_context_t * gen_context = ctx -> gen_context; - gen_context -> gcc_block = NULL; + oberon_generator_close_block(gen_context); } 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_block_t * gen_block = gen_context -> block; + gcc_jit_block * gcc_block = gen_block -> gcc_block; if(expr == NULL) { @@ -312,6 +365,11 @@ lvalue_from_item(oberon_context_t * ctx, oberon_item_t * item) { gen_var_t * gen_var = item -> var -> gen_var; left = gen_var -> gcc_lvalue; + if(item -> var -> class == OBERON_CLASS_VAR_PARAM) + { + gcc_jit_rvalue * r = gcc_jit_lvalue_as_rvalue(left); + left = gcc_jit_rvalue_dereference(r, NULL); + } } else if(item -> mode == MODE_INDEX) { @@ -322,12 +380,18 @@ lvalue_from_item(oberon_context_t * ctx, oberon_item_t * item) } else if(item -> mode == MODE_FIELD) { + printf("lvalue_from_item: %s\n", item -> var -> name); gen_var_t * gen_var = item -> var -> gen_var; gcc_jit_field * gcc_field = gen_var -> gcc_field; gcc_jit_lvalue * parent = lvalue_from_item(ctx, item -> parent); left = gcc_jit_lvalue_access_field(parent, NULL, gcc_field); } + else if(item -> mode == MODE_DEREF) + { + gcc_jit_rvalue * parent = rvalue_from_item(ctx, item -> parent); + left = gcc_jit_rvalue_dereference(parent, NULL); + } else { oberon_error(ctx, "invalid lvalue expression"); @@ -364,10 +428,8 @@ 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 - || 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); + gcc_jit_lvalue * left = lvalue_from_item(ctx, item); + right = gcc_jit_lvalue_as_rvalue(left); } else if(item -> mode == MODE_INTEGER) { @@ -390,16 +452,27 @@ rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item) { assert(item -> var -> class == OBERON_CLASS_PROC); + oberon_type_t * signature = item -> var -> type; 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; + oberon_object_t * arg_param = signature -> decl; for(int i = 0; i < num_args; i++) { - args[i] = rvalue_from_expr(ctx, expr); + if(arg_param -> class == OBERON_CLASS_VAR_PARAM) + { + gcc_jit_lvalue * left = lvalue_from_expr(ctx, expr); + args[i] = gcc_jit_lvalue_get_address(left, NULL); + } + else + { + args[i] = rvalue_from_expr(ctx, expr); + } expr = expr -> next; + arg_param = arg_param -> next; } gcc_jit_function * func = gen_proc -> gcc_func; @@ -420,6 +493,16 @@ rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item) gcc_jit_rvalue * parent = rvalue_from_item(ctx, item -> parent); right = gcc_jit_rvalue_access_field(parent, NULL, gcc_field); } + else if(item -> mode == MODE_DEREF) + { + gcc_jit_lvalue * left = lvalue_from_item(ctx, item); + right = gcc_jit_lvalue_as_rvalue(left); + } + else if(item -> mode == MODE_NIL) + { + 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 { oberon_error(ctx, "rvalue_from_item: invalid mode %i", item -> mode); @@ -436,14 +519,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 }, @@ -522,8 +610,21 @@ oberon_generate_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_ gcc_jit_rvalue * right; right = rvalue_from_expr(ctx, src); + if(src -> is_item) + { + if(src -> item.mode == MODE_NIL) + { + gen_context_t * gen_context = ctx -> gen_context; + gcc_jit_context * gcc_context = gen_context -> gcc_context; + gen_type_t * gen_type = dst -> result -> gen_type; + gcc_jit_type * cast_to_type = gen_type -> gcc_type; + right = gcc_jit_context_new_cast(gcc_context, NULL, right, cast_to_type); + } + } + gen_context_t * gen_context = ctx -> gen_context; - gcc_jit_block * gcc_block = gen_context -> gcc_block; + gen_block_t * gen_block = gen_context -> block; + gcc_jit_block * gcc_block = gen_block -> gcc_block; gcc_jit_block_add_assignment(gcc_block, NULL, left, right); }