DEADSOFTWARE

Добавлено вычисление размеров типа для аллокации
[dsw-obn.git] / generator.c
index 94bc1696a8f54f281e8868cfa02d7a51f1800cb0..179320aef7df8ffb4cf1aa4a6ecc3b4df4250f17 100644 (file)
@@ -4,6 +4,7 @@
 #include <ctype.h>
 #include <string.h>
 #include <assert.h>
+#include <stdbool.h>
 
 #include <gc.h>
 
@@ -19,6 +20,7 @@ __OBERON_ALLOC__ (size_t bytes)
 {
        void * p = GC_MALLOC(bytes);
        memset(p, 0, bytes);
+       printf("allocated %lu bytes\n", bytes);
        return p;
 }
 
@@ -323,10 +325,50 @@ static gcc_jit_rvalue * rvalue_from_item(oberon_context_t * ctx, oberon_item_t *
 static gcc_jit_rvalue * rvalue_from_expr(oberon_context_t * ctx, oberon_expr_t * expr);
 
 static int
-oberon_generator_get_type_size(oberon_type_t * type)
+oberon_generator_get_type_size(oberon_context_t * ctx, oberon_type_t * type)
 {
-       printf("TODO: oberon_generator_get_type_size\n");
-       return 128;
+       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 *= 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
@@ -578,9 +620,12 @@ rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item)
                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_NEWARR)
+       else if(item -> mode == MODE_NEW)
        {
-               int type_size = oberon_generator_get_type_size(item -> type);
+               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;