DEADSOFTWARE

Добавлены объявления типов-записей и типов-процедур, добавлены и исправлены селекторы...
[dsw-obn.git] / generator.c
index c633e542131722832c5d586e732e18d7f04d85f2..4ffba1173bf23582df014dea6d89d290656afcd3 100644 (file)
 
 #include <libgccjit.h>
 
-/*
- * oberon_var_t -> gvar == gcc_jit_lvalue;
- * oberon_type_t -> gtype == gcc_jit_type;
- * oberon_context_t -> gctx == 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,90 +58,170 @@ 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 = NULL;
+       gcc_jit_struct * gcc_struct = NULL;
+       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)
+       {
+               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 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);
        }
 
-       type -> gen_type = gen_type;
+       gen_type -> gcc_type = gcc_type;
+       gen_type -> gcc_struct = gcc_struct;
 }
 
 void
-oberon_generator_init_var(oberon_context_t * ctx, oberon_var_t * var)
+oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var)
 {
-       printcontext(ctx, "oberon_generator_init_var");
-
        gen_context_t * gen_context = ctx -> gen_context;
-       gcc_jit_context * gcc_context = gen_context -> gcc_context;
-       gcc_jit_type * gen_type = var -> type -> gen_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);
+       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;
-}
-
-// =======================================================================
-//   GENERATOR
-// ======================================================================= 
 
-static gcc_jit_rvalue *
-oberon_generate_rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item)
-{
-       printcontext(ctx, "oberon_generate_rvalue_from_item");
-
-       gen_context_t * gen_context = ctx -> gen_context;
        gcc_jit_context * gcc_context = gen_context -> gcc_context;
+       gcc_jit_type * gcc_type = gen_type -> gcc_type;
+       const char * name = var -> name;
 
-       gcc_jit_rvalue * right;
-       if(item -> mode == MODE_VAR)
+       // 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_jit_lvalue * gen_var = item -> var -> gen_var;
-               right = gcc_jit_lvalue_as_rvalue(gen_var);
-               printf("PUSH (var) %s\n", item -> var -> name);
+               gcc_lvalue = gcc_jit_context_new_global(
+                       gcc_context, NULL, GCC_JIT_GLOBAL_INTERNAL, gcc_type, name
+               );
        }
-       else if(item -> mode == MODE_INTEGER)
+       else if(var -> class == OBERON_CLASS_PARAM)
        {
-               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);
-               printf("PUSH (int) %i\n", item -> integer);
+               gcc_param = gcc_jit_context_new_param(gcc_context, NULL, gcc_type, name);
+               gcc_lvalue = gcc_jit_param_as_lvalue(gcc_param);
        }
-       else if(item -> mode == MODE_BOOLEAN)
+       else if(var -> class == OBERON_CLASS_FIELD)
        {
-               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);                   
-               }
-               printf("PUSH (bool) %i\n", item -> boolean);
+               gcc_field = gcc_jit_context_new_field(gcc_context, NULL, gcc_type, name);
        }
        else
        {
-               oberon_error(ctx, "oberon_generate_push: invalid mode %i", item -> mode);
+               oberon_error(ctx, "oberon_generator_init_var: invalid class %i", var -> class);
        }
 
-       return right;
+       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)
+{
+       assert(proc -> class == OBERON_CLASS_PROC);
+
+       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;
+
+       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
+       );
+
+       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);
+
 void
 oberon_generate_begin_module(oberon_context_t * ctx)
 {
@@ -186,28 +253,276 @@ oberon_generate_end_module(oberon_context_t * ctx)
 }
 
 void
-oberon_generate_assign(oberon_context_t * ctx, oberon_item_t * src, oberon_item_t * dst)
+oberon_generate_begin_proc(oberon_context_t * ctx, oberon_object_t * proc)
 {
-       printcontext(ctx, "oberon_generate_assign");
+       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);
+
+       // TODO make stack for block
+       gen_context -> gcc_block = 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;
+
+       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;
+       gen_context -> 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;
 
+       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)
+       {
+               gen_var_t * gen_var = item -> var -> gen_var;
+               left = gen_var -> gcc_lvalue;
+       }
+       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)
+       {
+               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
+       {
+               oberon_error(ctx, "invalid lvalue expression");
+       }
+
+       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)
+       {
+               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)
+       {
+               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)
+       {
+               assert(item -> var -> class == OBERON_CLASS_PROC);
+
+               gen_proc_t * gen_proc = item -> var -> gen_proc;
 
-       right = oberon_generate_rvalue_from_item(ctx, src);
+               int num_args = item -> num_args;
+               gcc_jit_rvalue *args[num_args];
 
-       if(dst -> mode == MODE_VAR)
+               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, num_args, args
+               );
+       }
+       else if(item -> mode == MODE_INDEX)
        {
-               printf("STORE %s\n", dst -> var -> name);
-               left = dst -> var -> gen_var;
+               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
        {
-               oberon_error(ctx, "oberon_generate_assign: invalid assignment");
-       }       
+               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_LOGICAL_NEGATE },
+       { 0, .unary_op = GCC_JIT_UNARY_OP_MINUS },
+
+       { 1, .binary_op = GCC_JIT_BINARY_OP_PLUS },
+       { 1, .binary_op = GCC_JIT_BINARY_OP_MINUS },
+       { 1, .binary_op = GCC_JIT_BINARY_OP_DIVIDE },
+       { 1, .binary_op = GCC_JIT_BINARY_OP_MODULO },
+       { 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);
+
+       gen_context_t * gen_context = ctx -> gen_context;
+       gcc_jit_block * gcc_block = gen_context -> gcc_block;
        gcc_jit_block_add_assignment(gcc_block, NULL, left, right);
 }