DEADSOFTWARE

Исправлен экспорт полей и экспорт для "только чтения"
[dsw-obn.git] / generator.c
index 4c3a984a6b226d5377ffb5fdcfa0ca5979ffd212..ff4896d3880de3b018213680a62c216a192e05da 100644 (file)
@@ -8,26 +8,27 @@
 #include "oberon.h"
 #include "generator.h"
 
-#include <libgccjit.h>
+// =======================================================================
+//   ALLOC
+// ======================================================================= 
 
-static void printcontext(oberon_context_t * ctx, char * s)
+static void
+oberon_generator_open_block(gen_context_t * gen_context, gcc_jit_block * gcc_block)
 {
-/*
-       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);
-*/
+       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;
 }
 
-// =======================================================================
-//   ALLOC
-// ======================================================================= 
+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)
@@ -40,15 +41,11 @@ 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");
 }
 
 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;
 
@@ -87,7 +84,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)
@@ -151,6 +152,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)
 {
@@ -163,16 +204,28 @@ 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;
        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)
        {
@@ -202,7 +255,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;
@@ -211,7 +267,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;
 
@@ -244,31 +302,30 @@ static gcc_jit_rvalue * rvalue_from_expr(oberon_context_t * ctx, oberon_expr_t *
 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);
 
-       gen_context -> gcc_block = gcc_block;
+       oberon_generator_open_block(gen_context, gcc_block);
 }
 
 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 -> 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
@@ -280,15 +337,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);
@@ -299,14 +356,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)
        {
@@ -329,6 +387,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)
@@ -416,8 +479,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;
 
@@ -441,10 +502,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)
        {
@@ -485,14 +564,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 },
 
@@ -583,8 +667,11 @@ 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;
-       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);
 }
 
@@ -598,7 +685,8 @@ 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");
+
+//     ctx -> mod -> begin = gcc_jit_result_get_code(gcc_result, "BEGIN");
 }
 
 void
@@ -608,3 +696,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);
+}