DEADSOFTWARE

Добавлен NIL и автоматическое разыменование указателей
[dsw-obn.git] / generator.c
index 4ffba1173bf23582df014dea6d89d290656afcd3..24529e0e26add6f595560626773029ce65f903e6 100644 (file)
@@ -79,6 +79,23 @@ 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)
+       {
+               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;
@@ -98,45 +115,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)
 {
@@ -322,12 +336,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");
@@ -420,6 +440,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);
@@ -441,6 +471,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 },
@@ -521,6 +552,18 @@ 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;
        gcc_jit_block_add_assignment(gcc_block, NULL, left, right);
@@ -538,3 +581,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);
+}