diff --git a/generator.c b/generator.c
index 991b6dd83c416be4d19a81a0722c30f29d6e12d5..179320aef7df8ffb4cf1aa4a6ecc3b4df4250f17 100644 (file)
--- a/generator.c
+++ b/generator.c
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
+#include <stdbool.h>
+
+#include <gc.h>
#include "oberon.h"
#include "generator.h"
#include "oberon.h"
#include "generator.h"
+// =======================================================================
+// INTERNAL FUNCTIONS
+// =======================================================================
+
+static void *
+__OBERON_ALLOC__ (size_t bytes)
+{
+ void * p = GC_MALLOC(bytes);
+ memset(p, 0, bytes);
+ printf("allocated %lu bytes\n", bytes);
+ return p;
+}
+
// =======================================================================
// ALLOC
// =======================================================================
// =======================================================================
// ALLOC
// =======================================================================
ctx -> gen_context = gen_context;
gen_context -> gcc_context = gcc_context;
ctx -> gen_context = gen_context;
gen_context -> gcc_context = gcc_context;
+
+ gcc_jit_type * void_ptr_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_VOID_PTR);
+ gcc_jit_type * size_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_SIZE_T);
+ gcc_jit_type * alloc_ptr_type = gcc_jit_context_new_function_ptr_type(
+ gcc_context, NULL, void_ptr_type, 1, &size_type, 0
+ );
+ gen_context -> gcc_alloc = gcc_jit_context_new_global(
+ gcc_context, NULL, GCC_JIT_GLOBAL_EXPORTED, alloc_ptr_type, "__OBERON_ALLOC__"
+ );
}
void
}
void
return;
}
return;
}
- char parent[256];
- parent[0] = 0;
-
+ int add_module_prefix;
switch(x -> class)
{
case OBERON_CLASS_FIELD:
case OBERON_CLASS_PARAM:
case OBERON_CLASS_VAR_PARAM:
/* В локальных областях префиксы излишни */
switch(x -> class)
{
case OBERON_CLASS_FIELD:
case OBERON_CLASS_PARAM:
case OBERON_CLASS_VAR_PARAM:
/* В локальных областях префиксы излишни */
+ add_module_prefix = 0;
break;
default:
break;
default:
- oberon_generator_get_full_name(parent, 256, x -> parent);
+ add_module_prefix = 1;
break;
}
break;
}
+ char parent[256];
+ oberon_generator_get_full_name(parent, 256, x -> parent);
+
if(strlen(parent) > 0)
{
snprintf(name, max_len, "%s_%s", parent, x -> name);
}
if(strlen(parent) > 0)
{
snprintf(name, max_len, "%s_%s", parent, x -> name);
}
+ else if(add_module_prefix)
+ {
+ snprintf(name, max_len, "%s_%s", x -> module -> name, x -> name);
+ }
else
{
snprintf(name, max_len, "%s", x -> name);
else
{
snprintf(name, max_len, "%s", x -> name);
else
{
gcc_lvalue = gcc_jit_context_new_global(
else
{
gcc_lvalue = gcc_jit_context_new_global(
- gcc_context, NULL, GCC_JIT_GLOBAL_INTERNAL, gcc_type, name
+ gcc_context, NULL, GCC_JIT_GLOBAL_EXPORTED, gcc_type, name
);
}
}
);
}
}
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);
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);
+static int
+oberon_generator_get_type_size(oberon_context_t * ctx, oberon_type_t * type)
+{
+ 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
oberon_generate_begin_module(oberon_context_t * ctx)
{
void
oberon_generate_begin_module(oberon_context_t * ctx)
{
}
else
{
}
else
{
- oberon_error(ctx, "invalid lvalue expression");
+ oberon_error(ctx, "lvalue_from_item: invalid mode %i", item -> mode);
}
return left;
}
return left;
gcc_jit_type * type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_VOID_PTR);
right = gcc_jit_context_null(gcc_context, type);
}
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_NEW)
+ {
+ 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;
+ oberon_expr_t * arg = item -> args;
+ for(int i = 0; i < num; i++)
+ {
+ array_size *= arg -> item.integer;
+ arg = arg -> next;
+ }
+
+ gcc_jit_type * size_type;
+ size_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_SIZE_T);
+
+ gcc_jit_rvalue * fnarg;
+ fnarg = gcc_jit_context_new_rvalue_from_int(gcc_context, size_type, array_size);
+
+ gcc_jit_type * result_type = item -> result -> gen_type -> gcc_type;
+ gcc_jit_rvalue * gcc_alloc = gcc_jit_lvalue_as_rvalue(gen_context -> gcc_alloc);
+ 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
{
oberon_error(ctx, "rvalue_from_item: invalid mode %i", item -> mode);
else
{
oberon_error(ctx, "rvalue_from_item: invalid mode %i", item -> mode);
gen_context -> gcc_result = gcc_result;
gen_context -> gcc_result = gcc_result;
+ typedef void * (*TOberonAlloc)(size_t);
+ TOberonAlloc * fn_alloc_ptr = gcc_jit_result_get_global(gcc_result, "__OBERON_ALLOC__");
+ *fn_alloc_ptr = __OBERON_ALLOC__;
+
// ctx -> mod -> begin = gcc_jit_result_get_code(gcc_result, "BEGIN");
}
// ctx -> mod -> begin = gcc_jit_result_get_code(gcc_result, "BEGIN");
}