From b224b07b1d47fd3ae165d652be4a9a3a10d52181 Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Sun, 30 Jul 2017 18:01:53 +0300 Subject: [PATCH] =?utf8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D1=8B=20?= =?utf8?q?=D0=B1=D1=8D=D0=BA=D1=8D=D0=BD=D0=B4=D1=8B=20dummy=20=D0=B8=20li?= =?utf8?q?bgccjit.=20=D0=9A=D0=BE=D0=BF=D0=B8=D0=BB=D1=8F=D1=82=D0=BE?= =?utf8?q?=D1=80=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=BE=D1=80?= =?utf8?q?=D0=B8=D0=B5=D0=BD=D1=82=D0=BE=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=20?= =?utf8?q?=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=B4=D0=BB=D1=8F=20jvm?= =?utf8?q?=20=D0=B8=20=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=B4=D0=BB?= =?utf8?q?=D1=8F=20=D0=B4=D0=B0=D0=BB=D1=8C=D0=BD=D0=B5=D0=B9=D1=88=D0=B5?= =?utf8?q?=D0=B9=20=D1=80=D0=B0=D1=81=D0=BA=D1=80=D1=83=D1=82=D0=BA=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- make.sh | 9 +- src/backends/dummy/generator-dummy.c | 118 --- src/backends/dummy/generator-dummy.h | 24 - src/backends/libgccjit/generator-libgccjit.c | 852 ------------------- src/backends/libgccjit/generator-libgccjit.h | 34 - src/backends/libgccjit/notes | 63 -- 6 files changed, 1 insertion(+), 1099 deletions(-) delete mode 100644 src/backends/dummy/generator-dummy.c delete mode 100644 src/backends/dummy/generator-dummy.h delete mode 100644 src/backends/libgccjit/generator-libgccjit.c delete mode 100644 src/backends/libgccjit/generator-libgccjit.h delete mode 100644 src/backends/libgccjit/notes diff --git a/make.sh b/make.sh index 561238e..64ae77f 100755 --- a/make.sh +++ b/make.sh @@ -6,17 +6,10 @@ CC="gcc" CFLAGS="-g -Wall -Werror -std=c11 -lgc" case "$1" in - dummy) - $CC $CFLAGS src/*.c src/backends/dummy/*.c - ;; jvm) $CC $CFLAGS src/*.c src/backends/jvm/*.c ;; - libgccjit) - CFLAGS="-lgccjit $CFLAGS" - $CC $CFLAGS src/*.c src/backends/libgccjit/*.c - ;; ***) echo "use: make.sh " - echo "list of backends: dummy jvm libgccjit" + echo "list of backends: jvm" esac diff --git a/src/backends/dummy/generator-dummy.c b/src/backends/dummy/generator-dummy.c deleted file mode 100644 index fc709a0..0000000 --- a/src/backends/dummy/generator-dummy.c +++ /dev/null @@ -1,118 +0,0 @@ -#include -#include - -#include - -#include "../../../include/oberon.h" -#include "../../oberon-internals.h" -#include "generator-dummy.h" - -void -oberon_generator_init_context(oberon_context_t * ctx) -{ - gen_context_t * gen_context = GC_MALLOC(sizeof *gen_context); - memset(gen_context, 0, sizeof *gen_context); - - ctx -> gen_context = gen_context; -} - -void -oberon_generator_destroy_context(oberon_context_t * ctx) -{ - ctx -> gen_context = NULL; -} - -void -oberon_generator_init_type(oberon_context_t * ctx, oberon_type_t * type) -{ - type -> gen_type = NULL; -} - -void -oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * type) -{ - assert(type -> class == OBERON_TYPE_RECORD); - // init record fields -} - -void -oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var) -{ - var -> gen_var = NULL; -} - -void -oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * proc) -{ - proc -> gen_proc = NULL; -} - -// ======================================================================= -// GENERATOR -// ======================================================================= - -void -oberon_generate_begin_module(oberon_context_t * ctx) -{ - -} - -void -oberon_generate_end_module(oberon_context_t * ctx) -{ - -} - -void -oberon_generate_begin_proc(oberon_context_t * ctx, oberon_object_t * proc) -{ - -} - -void -oberon_generate_call_proc(oberon_context_t * ctx, oberon_expr_t * desig) -{ - -} - -void -oberon_generate_end_proc(oberon_context_t * ctx) -{ - -} - -void -oberon_generate_return(oberon_context_t * ctx, oberon_expr_t * expr) -{ - -} - -void -oberon_generate_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst) -{ - -} - -void -oberon_generate_code(oberon_context_t * ctx) -{ - -} - -void -oberon_generator_dump(oberon_context_t * ctx, char * path) -{ - -} - -void * -oberon_generator_get_procedure(oberon_context_t * ctx, const char * name) -{ - return NULL; -} - -void * -oberon_generator_get_var(oberon_context_t * ctx, const char * name) -{ - return NULL; -} diff --git a/src/backends/dummy/generator-dummy.h b/src/backends/dummy/generator-dummy.h deleted file mode 100644 index 499b90b..0000000 --- a/src/backends/dummy/generator-dummy.h +++ /dev/null @@ -1,24 +0,0 @@ -struct gen_proc_t -{ - -}; - -struct gen_type_t -{ - -}; - -struct gen_var_t -{ - -}; - -struct gen_block_t -{ - -}; - -struct gen_context_t -{ - -}; diff --git a/src/backends/libgccjit/generator-libgccjit.c b/src/backends/libgccjit/generator-libgccjit.c deleted file mode 100644 index a3bd277..0000000 --- a/src/backends/libgccjit/generator-libgccjit.c +++ /dev/null @@ -1,852 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "../../../include/oberon.h" -#include "../../oberon-internals.h" -#include "generator-libgccjit.h" - -// ======================================================================= -// INTERNAL FUNCTIONS -// ======================================================================= - -static void * -__OBERON_ALLOC__ (size_t bytes) -{ - void * p = GC_MALLOC(bytes); - memset(p, 0, bytes); - printf("allocated %lu bytes\n", bytes); - return p; -} - -// ======================================================================= -// 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) -{ - gen_context_t * gen_context = malloc(sizeof *gen_context); - memset(gen_context, 0, sizeof *gen_context); - - gcc_jit_context * gcc_context; - gcc_context = gcc_jit_context_acquire(); - - ctx -> gen_context = gen_context; - gen_context -> gcc_context = gcc_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) -{ - gen_context_t * gen_context = ctx -> gen_context; - gcc_jit_context * gcc_context = gen_context -> gcc_context; - - gcc_jit_context_release(gcc_context); -} - -void -oberon_generator_init_type(oberon_context_t * ctx, oberon_type_t * 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 * gcc_type = NULL; - gcc_jit_struct * gcc_struct = NULL; - if(type -> class == OBERON_TYPE_VOID) - { - 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) - { - 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) - { - 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; - gcc_jit_type * params[num_params]; - oberon_object_t * o = type -> decl; - for(int i = 0; i < num_params; i++) - { - gen_type_t * gen_type = o -> type -> gen_type; - params[i] = gen_type -> gcc_type; - o = o -> next; - } - - gen_type_t * base = type -> base -> gen_type; - gcc_jit_type * result_type = base -> gcc_type; - - gcc_type = gcc_jit_context_new_function_ptr_type( - gcc_context, NULL, result_type, num_params, params, 0 - ); - } - 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); -} - -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) -{ - 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 * gcc_type = gen_type -> gcc_type; - - char name[256]; - oberon_generator_get_full_name(name, 256, var); - - gcc_jit_lvalue * gcc_lvalue = NULL; - gcc_jit_param * gcc_param = NULL; - gcc_jit_field * gcc_field = NULL; - if(var -> class == OBERON_CLASS_VAR) - { - 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_EXPORTED, 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); - } - else - { - oberon_error(ctx, "oberon_generator_init_var: invalid class %i", var -> class); - } - - gen_var -> gcc_lvalue = gcc_lvalue; - gen_var -> gcc_param = gcc_param; - gen_var -> gcc_field = gcc_field; -} - -void -oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * 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; - - gen_proc_t * gen_proc = malloc(sizeof *gen_proc); - memset(gen_proc, 0, sizeof *gen_proc); - proc -> gen_proc = gen_proc; - - 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; - - /* Строим список параметров */ - 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 - ); - - gen_proc -> gcc_func = gcc_func; -} - -// ======================================================================= -// GENERATOR -// ======================================================================= - -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) -{ - 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, name, 0, NULL, 0 - ); - gcc_jit_block * gcc_block = gcc_jit_function_new_block(func, NULL); - - oberon_generator_open_block(gen_context, gcc_block); -} - -void -oberon_generate_end_module(oberon_context_t * ctx) -{ - gen_context_t * gen_context = ctx -> gen_context; - gcc_jit_block * gcc_block = gen_context -> block -> gcc_block; - - gcc_jit_block_end_with_void_return(gcc_block, NULL); - - oberon_generator_close_block(gen_context); -} - -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 = gen_proc -> gcc_func; - gcc_jit_block * gcc_block = gcc_jit_function_new_block(func, NULL); - - 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; - 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); - gcc_jit_block_add_eval(block, NULL, return_value); -} - -void -oberon_generate_end_proc(oberon_context_t * ctx) -{ - gen_context_t * gen_context = ctx -> gen_context; - 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; - gen_block_t * gen_block = gen_context -> block; - gcc_jit_block * gcc_block = gen_block -> gcc_block; - - 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 * -lvalue_from_item(oberon_context_t * ctx, oberon_item_t * item) -{ - gen_context_t * gen_context = ctx -> gen_context; - gcc_jit_context * gcc_context = gen_context -> gcc_context; - - gcc_jit_lvalue * left; - - 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) - { - gcc_jit_rvalue * r = gcc_jit_lvalue_as_rvalue(left); - left = gcc_jit_rvalue_dereference(r, NULL); - } - } - else if(item -> mode == MODE_INDEX) - { - assert(item -> num_args == 1); - gcc_jit_rvalue * parent = rvalue_from_item(ctx, item -> parent); - gcc_jit_rvalue * index = rvalue_from_expr(ctx, item -> args); - left = gcc_jit_context_new_array_access(gcc_context, NULL, parent, index); - } - 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, "lvalue_from_item: invalid mode %i", item -> mode); - } - - return left; -} - -static gcc_jit_lvalue * -lvalue_from_expr(oberon_context_t *ctx, oberon_expr_t * expr) -{ - gcc_jit_lvalue * left; - oberon_item_t * item; - - if(expr -> is_item) - { - item = (oberon_item_t *) expr; - left = lvalue_from_item(ctx, item); - } - else - { - oberon_error(ctx, "invalid lvalue expression"); - } - - return left; -} - -static gcc_jit_rvalue * -rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item) -{ - gen_context_t * gen_context = ctx -> gen_context; - gcc_jit_context * gcc_context = gen_context -> gcc_context; - - gcc_jit_rvalue * right; - if(item -> mode == MODE_VAR) - { - gcc_jit_lvalue * left = lvalue_from_item(ctx, item); - right = gcc_jit_lvalue_as_rvalue(left); - } - else if(item -> mode == MODE_INTEGER) - { - gcc_jit_type * int_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_INT); - right = gcc_jit_context_new_rvalue_from_int(gcc_context, int_type, item -> integer); - } - else if(item -> mode == MODE_BOOLEAN) - { - gcc_jit_type * bool_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_BOOL); - if(item -> boolean) - { - right = gcc_jit_context_one(gcc_context, bool_type); - } - else - { - right = gcc_jit_context_zero(gcc_context, bool_type); - } - } - else if(item -> mode == MODE_CALL) - { - 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++) - { - 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_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) - { - gcc_jit_lvalue * left = lvalue_from_item(ctx, item); - right = gcc_jit_lvalue_as_rvalue(left); - } - else if(item -> mode == MODE_FIELD) - { - gen_var_t * gen_var = item -> var -> gen_var; - gcc_jit_field * gcc_field = gen_var -> gcc_field; - - 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 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); - } - - return right; -} - -struct { - int type; // 0 - unary, 1 - binary, 2 - comp - union { - enum gcc_jit_unary_op unary_op; - enum gcc_jit_binary_op binary_op; - enum gcc_jit_comparison comp_op; - }; -} op_table[] = { - { 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 }, - - { 2, .comp_op = GCC_JIT_COMPARISON_EQ }, - { 2, .comp_op = GCC_JIT_COMPARISON_NE }, - { 2, .comp_op = GCC_JIT_COMPARISON_LT }, - { 2, .comp_op = GCC_JIT_COMPARISON_LE }, - { 2, .comp_op = GCC_JIT_COMPARISON_GT }, - { 2, .comp_op = GCC_JIT_COMPARISON_GE } -}; - -static gcc_jit_rvalue * -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 = gen_type -> gcc_type; - - int expr_type = op_table[operator -> op].type; - if(expr_type == 0) - { - enum gcc_jit_unary_op op = op_table[operator -> op].unary_op; - gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left); - right = gcc_jit_context_new_unary_op(gcc_context, NULL, op, result_type, l); - } - else if(expr_type == 1) - { - enum gcc_jit_unary_op op = op_table[operator -> op].binary_op; - gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left); - gcc_jit_rvalue * r = rvalue_from_expr(ctx, operator -> right); - right = gcc_jit_context_new_binary_op(gcc_context, NULL, op, result_type, l, r); - } - else if(expr_type == 2) - { - enum gcc_jit_comparison op = op_table[operator -> op].comp_op; - gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left); - gcc_jit_rvalue * r = rvalue_from_expr(ctx, operator -> right); - right = gcc_jit_context_new_comparison(gcc_context, NULL, op, l, r); - } - else - { - oberon_error(ctx, "rvalue_from_operator: wat"); - } - - return right; -} - -static gcc_jit_rvalue * -rvalue_from_expr(oberon_context_t * ctx, oberon_expr_t * expr) -{ - gcc_jit_rvalue * right; - - if(expr -> is_item) - { - oberon_item_t * item = (oberon_item_t *) expr; - right = rvalue_from_item(ctx, item); - } - else - { - oberon_oper_t * operator = (oberon_oper_t *) expr; - right = rvalue_from_operator(ctx, operator); - } - - return right; -} - -void -oberon_generate_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst) -{ - gcc_jit_lvalue * left; - left = lvalue_from_expr(ctx, dst); - - 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); - } - } - - 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; - gcc_jit_block_add_assignment(gcc_block, NULL, left, right); -} - -void -oberon_generate_code(oberon_context_t * ctx) -{ - gen_context_t * gen_context = ctx -> gen_context; - gcc_jit_context * gcc_context = gen_context -> gcc_context; - - gcc_jit_result * gcc_result; - gcc_result = gcc_jit_context_compile(gcc_context); - - gen_context -> gcc_result = gcc_result; - - 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 -oberon_generator_dump(oberon_context_t * ctx, char * path) -{ - gen_context_t * gen_context = ctx -> gen_context; - 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); -} diff --git a/src/backends/libgccjit/generator-libgccjit.h b/src/backends/libgccjit/generator-libgccjit.h deleted file mode 100644 index ba79933..0000000 --- a/src/backends/libgccjit/generator-libgccjit.h +++ /dev/null @@ -1,34 +0,0 @@ -#include - -struct gen_proc_t -{ - gcc_jit_function * gcc_func; -}; - -struct gen_type_t -{ - gcc_jit_type * gcc_type; - gcc_jit_struct * gcc_struct; -}; - -struct gen_var_t -{ - gcc_jit_lvalue * gcc_lvalue; - gcc_jit_param * gcc_param; - gcc_jit_field * gcc_field; -}; - -struct gen_block_t -{ - gcc_jit_block * gcc_block; - gen_block_t * up; -}; - -struct gen_context_t -{ - gcc_jit_context * gcc_context; - gcc_jit_result * gcc_result; - gen_block_t * block; - unsigned record_count; - gcc_jit_lvalue * gcc_alloc; -}; diff --git a/src/backends/libgccjit/notes b/src/backends/libgccjit/notes deleted file mode 100644 index 978f4f6..0000000 --- a/src/backends/libgccjit/notes +++ /dev/null @@ -1,63 +0,0 @@ -# 24.07.2017 - -- libgccjit годится разве что для хелловорлдов. - Переходим на jvm. - -- открытые массивы работкают криво как статические аргументы процедур - Случай 1: не проходит проверки libgccjit - (* - TYPE Ar = ARRAY OF INTEGER; - VAR a : POINTER TO Ar; - - (* так же и с VAR-параметром *) - PROCEDURE Ax(x : Ar); - END Ax; - - Ax(a); - *) - Случай 2: массив должен быть указателем, да и ещё копироваться - (* - TYPE Ar = ARRAY OF INTEGER; - VAR a : POINTER TO Ar; - - (* при использовании VAR-параметра работает *) - PROCEDURE Ax(x : Ar); - END Ax; - - Ax(a^); - *) - -- импортируемые модули не инициализируются (секция begin) - -- не реализовано расширение типа record: - libgccjit не умеет в классы. Проверки в рантайме надо делать вручную. -- нет проверок границ массивов в рантайме: - Потому что как минимум нет дескрипторов типа. - Возможно можно заюзать это: - https://gcc.gnu.org/onlinedocs/gcc/Pointer-Bounds-Checker-builtins.html -- При вычислении размера структур не учитывается вравнивание. - в libgccjit нет средств получения размера типов, в т.ч. структур - Как происходит выравнивание и есть ли оно вообще по дефолту - не понятно: - Нужно ли для получения выровненных структур использовать gcc_jit_type_get_aligned? - Пересекается с этим: - https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html - Требуемые выравнивания для типов можно получить через встроинную функцию __alignof__(T) - https://gcc.gnu.org/onlinedocs/gcc/Alignment.html - Возможный алгоритм выравнивания: - https://stackoverflow.com/questions/6963998/how-does-gcc-calculate-the-required-space-for-a-structure - http://www.rendoc.tech/questions/834444/how-does-gcc-calculate-the-required-space-for-a-structure - Есть ещё что-то для проверки границ объектов: - https://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html -- не понятен результат присваивания статических/разыменованных структур и массивов - (* reca := recb; *) - побайтовое копирование? -- не реализовано присваивание процедур к переменным-процедурам: - в libgccjit нет средств получения указателя на функцию. - Как решение-костыль - получение указателя в основной программе и сохранение в переменной. -- не реализован автокаст: - Не критично: libgccjit сам разруливает типы разных размеров. -- не реализованы локальные процедуры: - libgccjit не умеет в локальные функции. - Обойти можно костылём как в jvm. -- нет проверок переполнения как в рантайме, так и в компилтайме. - Возможно можно заюзать это: - https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html -- 2.29.2