diff --git a/generator.c b/generator.c
index 94bc1696a8f54f281e8868cfa02d7a51f1800cb0..b0887aee7c82587beed212c50e56cac179a32b72 100644 (file)
--- a/generator.c
+++ b/generator.c
#include <ctype.h>
#include <string.h>
#include <assert.h>
+#include <stdbool.h>
#include <gc.h>
{
void * p = GC_MALLOC(bytes);
memset(p, 0, bytes);
+ printf("allocated %lu bytes\n", bytes);
return p;
}
}
else if(type -> class == OBERON_TYPE_BOOLEAN)
{
- gcc_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_BOOL);
+ if(type -> size == sizeof(bool))
+ {
+ gcc_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_BOOL);
+ }
+ else
+ {
+ oberon_error(ctx, "generator: unsupported boolean size");
+ }
+ }
+ else if(type -> class == OBERON_TYPE_REAL)
+ {
+ if(type -> size == sizeof(float))
+ {
+ gcc_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_FLOAT);
+ }
+ else if(type -> size == sizeof(double))
+ {
+ gcc_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_DOUBLE);
+ }
+ else if(type -> size == sizeof(long double))
+ {
+ gcc_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_LONG_DOUBLE);
+ }
+ else
+ {
+ oberon_error(ctx, "generator: unsupported real size");
+ }
}
else if(type -> class == OBERON_TYPE_ARRAY)
{
@@ -323,10 +351,51 @@ 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 = (size == 0) ? (1) : (size);
+ 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
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;
right = gcc_jit_context_new_call_through_ptr(gcc_context, NULL, gcc_alloc, 1, &fnarg);
right = gcc_jit_context_new_cast(gcc_context, NULL, right, result_type);
}
+ else if(item -> mode == MODE_REAL)
+ {
+ gcc_jit_type * int_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_FLOAT);
+ right = gcc_jit_context_new_rvalue_from_int(gcc_context, int_type, item -> real);
+ }
else
{
oberon_error(ctx, "rvalue_from_item: invalid mode %i", item -> mode);