u32 next_package_id;
u32 next_scope_id;
+ u32 next_type_id;
+ u32 next_entity_id;
+
+ u64 lexer_lines_processed;
+ u64 lexer_tokens_processed;
u32 cycle_almost_detected : 2;
b32 cycle_detected : 1;
extern Type type_auto_return;
extern AstBasicType basic_type_auto_return;
-extern OnyxToken builtin_package_token;
extern AstGlobal builtin_heap_start;
extern AstGlobal builtin_stack_top;
extern AstGlobal builtin_tls_base;
Type type_auto_return = { 0 };
AstBasicType basic_type_auto_return = { Ast_Kind_Basic_Type, 0, &simd_token, NULL, NULL, 0, NULL, &type_auto_return };
-OnyxToken builtin_package_token = { Token_Type_Symbol, 7, "builtin ", { 0 } };
-
static OnyxToken builtin_heap_start_token = { Token_Type_Symbol, 12, "__heap_start ", { 0 } };
static OnyxToken builtin_stack_top_token = { Token_Type_Symbol, 11, "__stack_top ", { 0 } };
static OnyxToken builtin_tls_base_token = { Token_Type_Symbol, 10, "__tls_base ", { 0 } };
}
void initialize_builtins(bh_allocator a) {
- // HACK
- builtin_package_token.text = bh_strdup(global_heap_allocator, builtin_package_token.text);
-
BuiltinSymbol* bsym = (BuiltinSymbol *) &builtin_symbols[0];
while (bsym->sym != NULL) {
if (bsym->package == NULL)
bh_arena_init(&entities->entity_arena, global_heap_allocator, 32 * 1024);
}
-static u32 next_entity_id = 0;
-
// Allocates the entity in the entity heap. Don't quite feel this is necessary...
Entity* entity_heap_register(EntityHeap* entities, Entity e) {
bh_allocator alloc = bh_arena_allocator(&entities->entity_arena);
Entity* entity = bh_alloc_item(alloc, Entity);
*entity = e;
- entity->id = next_entity_id++;
+ entity->id = context.next_entity_id++;
entity->macro_attempts = 0;
entity->micro_attempts = 0;
entity->entered_in_queue = 0;
#include "utils.h"
#include "errors.h"
-u64 lexer_lines_processed = 0;
-u64 lexer_tokens_processed = 0;
-
static const char* token_type_names[] = {
"TOKEN_TYPE_UNKNOWN",
"TOKEN_TYPE_END_STREAM",
tk = onyx_get_token(tokenizer);
} while (tk->type != Token_Type_End_Stream);
- lexer_lines_processed += tokenizer->line_number - 1;
- lexer_tokens_processed += bh_arr_length(tokenizer->tokens);
+ context.lexer_lines_processed += tokenizer->line_number - 1;
+ context.lexer_tokens_processed += bh_arr_length(tokenizer->tokens);
}
b32 token_equals(OnyxToken* tkn1, OnyxToken* tkn2) {
static Entity *runtime_info_proc_tags_entity;
static void context_init(CompileOptions* opts) {
+ memset(&context, 0, sizeof context);
+
types_init();
prepare_builtins();
- memset(&context, 0, sizeof context);
-
// HACK
special_global_entities_remaining = 3;
}));
}
+ builtin_heap_start.entity = NULL;
+ builtin_stack_top.entity = NULL;
+ builtin_tls_base.entity = NULL;
+ builtin_tls_size.entity = NULL;
+ builtin_closure_base.entity = NULL;
+
add_entities_for_node(NULL, (AstNode *) &builtin_stack_top, context.global_scope, NULL);
add_entities_for_node(NULL, (AstNode *) &builtin_heap_start, context.global_scope, NULL);
add_entities_for_node(NULL, (AstNode *) &builtin_tls_base, context.global_scope, NULL);
// TODO: Replace these with bh_printf when padded formatting is added.
printf("\nStatistics:\n");
printf(" Time taken: %lf seconds\n", (double) duration / 1000);
- printf(" Processed %ld lines (%f lines/second).\n", lexer_lines_processed, ((f32) 1000 * lexer_lines_processed) / (duration));
- printf(" Processed %ld tokens (%f tokens/second).\n", lexer_tokens_processed, ((f32) 1000 * lexer_tokens_processed) / (duration));
+ printf(" Processed %ld lines (%f lines/second).\n", context.lexer_lines_processed, ((f32) 1000 * context.lexer_lines_processed) / (duration));
+ printf(" Processed %ld tokens (%f tokens/second).\n", context.lexer_tokens_processed, ((f32) 1000 * context.lexer_tokens_processed) / (duration));
printf("\n");
}
bh_scratch_init(&global_scratch, bh_heap_allocator(), 256 * 1024); // NOTE: 256 KiB
global_scratch_allocator = bh_scratch_allocator(&global_scratch);
- // bh_managed_heap_init(&mh);
- // global_heap_allocator = bh_managed_heap_allocator(&mh);
- global_heap_allocator = bh_heap_allocator();
+ bh_managed_heap_init(&mh);
+ global_heap_allocator = bh_managed_heap_allocator(&mh);
+ // global_heap_allocator = bh_heap_allocator();
context_init(compile_opts);
return onyx_compile();
context_free();
bh_scratch_free(&global_scratch);
- // bh_managed_heap_free(&mh);
+ bh_managed_heap_free(&mh);
}
int main(int argc, char *argv[]) {
}
static void type_register(Type* type) {
- static u32 next_unique_id = 1;
- type->id = next_unique_id++;
+ type->id = ++context.next_type_id;
if (type->ast_type) type->ast_type->type_id = type->id;
bh_imap_put(&type_map, type->id, (u64) type);
}
void types_init() {
- bh_imap_init(&type_map, global_heap_allocator, 255);
- bh_imap_init(&type_pointer_map, global_heap_allocator, 255);
- bh_imap_init(&type_multi_pointer_map, global_heap_allocator, 255);
- bh_imap_init(&type_array_map, global_heap_allocator, 255);
- bh_imap_init(&type_slice_map, global_heap_allocator, 255);
- bh_imap_init(&type_dynarr_map, global_heap_allocator, 255);
- bh_imap_init(&type_vararg_map, global_heap_allocator, 255);
+#define MAKE_MAP(x) (memset(&x, 0, sizeof(x)), bh_imap_init(&x, global_heap_allocator, 255))
+ MAKE_MAP(type_map);
+ MAKE_MAP(type_pointer_map);
+ MAKE_MAP(type_multi_pointer_map);
+ MAKE_MAP(type_array_map);
+ MAKE_MAP(type_slice_map);
+ MAKE_MAP(type_dynarr_map);
+ MAKE_MAP(type_vararg_map);
+
+ type_func_map = NULL;
sh_new_arena(type_func_map);
fori (i, 0, Basic_Kind_Count) type_register(&basic_types[i]);
+#undef MAKE_MAP
}
void types_dump_type_info() {
return return .{ .Err, .{ error = res.__data.error } };
}
+
+#operator ?? macro (r: Result($T, $E), v: T) -> T {
+ r_ = r;
+ if r_.status == .Ok do return r_.__data.value;
+
+ return v;
+}
+
+
// MANAGED HEAP ALLOCATOR
+static const u64 bh_managed_heap_magic_number = 0x1337cafedeadbeef;
+
+typedef struct bh_managed_heap__link {
+ struct bh_managed_heap__link *prev, *next;
+ u64 magic_number;
+} bh_managed_heap__link;
+
typedef struct bh_managed_heap {
- bh_imap ptrs;
+ bh_managed_heap__link *first;
} bh_managed_heap;
void bh_managed_heap_init(bh_managed_heap* mh);
// MANAGED HEAP ALLOCATOR IMPLEMENTATION
void bh_managed_heap_init(bh_managed_heap* mh) {
- bh_imap_init(&mh->ptrs, bh_heap_allocator(), 512);
+ mh->first = NULL;
}
void bh_managed_heap_free(bh_managed_heap* mh) {
- bh_arr_each(bh__imap_entry, p, mh->ptrs.entries) {
+ bh_managed_heap__link *l = mh->first;
+ while (l) {
+ bh_managed_heap__link *n = l->next;
+ if (l->magic_number == bh_managed_heap_magic_number) {
+ l->magic_number = 0;
#if defined(_BH_WINDOWS)
- _aligned_free((void *) p->key);
+ _aligned_free((void *) l);
#elif defined(_BH_LINUX)
- free((void *) p->key);
+ free((void *) l);
#endif
+ }
+
+ l = n;
}
- bh_imap_free(&mh->ptrs);
+ mh->first = NULL;
}
bh_allocator bh_managed_heap_allocator(bh_managed_heap* mh) {
BH_ALLOCATOR_PROC(bh_managed_heap_allocator_proc) {
bh_managed_heap* mh = (bh_managed_heap *) data;
- ptr retval = NULL;
- switch (action) {
- case bh_allocator_action_alloc: {
-#if defined(_BH_WINDOWS)
- retval = _aligned_malloc(size, alignment);
-#elif defined(_BH_LINUX)
- i32 success = posix_memalign(&retval, alignment, size);
-#endif
+ bh_managed_heap__link *old = NULL;
+ if (prev_memory) {
+ old = ((bh_managed_heap__link *) prev_memory) - 1;
- if (flags & bh_allocator_flag_clear && retval != NULL) {
- memset(retval, 0, size);
+ if (old->magic_number != bh_managed_heap_magic_number) {
+ return bh_heap_allocator_proc(NULL, action, size, alignment, prev_memory, flags);
}
+ }
- if (retval != NULL)
- bh_imap_put(&mh->ptrs, (u64) retval, 1);
- } break;
+ if (old && (action == bh_allocator_action_resize || action == bh_allocator_action_free)) {
+ if (old->prev) {
+ old->prev->next = old->next;
+ } else {
+ mh->first = old->next;
+ }
- case bh_allocator_action_resize: {
-#if defined(_BH_WINDOWS)
- retval = _aligned_realloc(prev_memory, size, alignment);
-#elif defined(_BH_LINUX)
- retval = realloc(prev_memory, size);
-#endif
- if (prev_memory != retval) {
- bh_imap_delete(&mh->ptrs, (u64) prev_memory);
- bh_imap_put(&mh->ptrs, (u64) retval, 1);
+ if (old->next) {
+ old->next->prev = old->prev;
}
- } break;
+ }
- case bh_allocator_action_free: {
- bh_imap_delete(&mh->ptrs, (u64) prev_memory);
-#if defined(_BH_WINDOWS)
- _aligned_free(prev_memory);
-#elif defined(_BH_LINUX)
- free(prev_memory);
-#endif
- } break;
+ bh_managed_heap__link *newptr = bh_heap_allocator_proc(NULL, action, size + sizeof(*old), alignment, old, flags);
+
+ if (action == bh_allocator_action_alloc || action == bh_allocator_action_resize) {
+ if (newptr) {
+ newptr->magic_number = bh_managed_heap_magic_number;
+ newptr->next = mh->first;
+ newptr->prev = NULL;
+
+ if (mh->first != NULL) {
+ mh->first->prev = newptr;
+ }
+
+ mh->first = newptr;
+ }
}
- return retval;
+ return newptr + 1;
}