DEADSOFTWARE

Реализованы неявные объявления типов наперёд
[dsw-obn.git] / generator.c
index 685786f19f9af12ff75fdc30ab16801a75cb922d..a802a96f4e071c64496e5df09f6d28cbfad24a58 100644 (file)
@@ -65,7 +65,8 @@ oberon_generator_init_type(oberon_context_t * ctx, oberon_type_t * type)
        gen_context_t * gen_context = ctx -> gen_context;
        gcc_jit_context * gcc_context = gen_context -> gcc_context;
 
-       gcc_jit_type * gcc_type;
+       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);
@@ -78,10 +79,6 @@ 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_PROCEDURE)
-       {
-               gcc_type = NULL; // not used
-       }
        else if(type -> class == OBERON_TYPE_ARRAY)
        {
                if(type -> dim != 1)
@@ -94,12 +91,70 @@ oberon_generator_init_type(oberon_context_t * ctx, oberon_type_t * type)
 
                gcc_type = gcc_jit_context_new_array_type(gcc_context, NULL, gcc_base, type -> size);
        }
+       else if(type -> class == OBERON_TYPE_RECORD)
+       {
+               gcc_struct = gcc_jit_context_new_opaque_struct(gcc_context, NULL, "");
+               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);
 }
 
 void
@@ -117,21 +172,32 @@ oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var)
        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)
        {
-               gen_var -> gcc_lvalue = gcc_jit_context_new_global(
+               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);
+               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
@@ -173,6 +239,7 @@ oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * proc)
 //   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
@@ -253,6 +320,42 @@ oberon_generate_return(oberon_context_t * ctx, oberon_expr_t * expr)
        }
 }
 
+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)
 {
@@ -262,12 +365,7 @@ lvalue_from_expr(oberon_context_t *ctx, oberon_expr_t * expr)
        if(expr -> is_item)
        {
                item = (oberon_item_t *) expr;
-               if(item -> mode != MODE_VAR)
-               {
-                       oberon_error(ctx, "invalid lvalue expression");
-               }
-               gen_var_t * gen_var = item -> var -> gen_var;
-               left = gen_var -> gcc_lvalue;
+               left = lvalue_from_item(ctx, item);
        }
        else
        {
@@ -329,9 +427,22 @@ rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item)
                        gcc_context, NULL, func, num_args, args
                );
        }
+       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
        {
-               oberon_error(ctx, "oberon_generate_push: invalid mode %i", item -> mode);
+               oberon_error(ctx, "rvalue_from_item: invalid mode %i", item -> mode);
        }
 
        return right;
@@ -350,6 +461,7 @@ struct {
 
        { 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_LOGICAL_AND },
@@ -447,3 +559,11 @@ oberon_generate_code(oberon_context_t * ctx)
        gen_context -> gcc_result = gcc_result;
        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);
+}