From a4155b0b4622ecf907a4d26061650c3e887fa487 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 25 Apr 2023 22:01:01 -0500 Subject: [PATCH] fixed: removed all random static variables --- compiler/include/astnodes.h | 6 ++- compiler/src/builtins.c | 5 --- compiler/src/entities.c | 4 +- compiler/src/lex.c | 7 +-- compiler/src/onyx.c | 22 ++++++---- compiler/src/types.c | 21 +++++---- core/container/result.onyx | 9 ++++ shared/include/bh.h | 88 +++++++++++++++++++++---------------- 8 files changed, 93 insertions(+), 69 deletions(-) diff --git a/compiler/include/astnodes.h b/compiler/include/astnodes.h index d02a97ac..59bfa39b 100644 --- a/compiler/include/astnodes.h +++ b/compiler/include/astnodes.h @@ -1780,6 +1780,11 @@ struct Context { 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; @@ -1821,7 +1826,6 @@ extern AstBasicType basic_type_v128; 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; diff --git a/compiler/src/builtins.c b/compiler/src/builtins.c index ecf770c5..42a6523e 100644 --- a/compiler/src/builtins.c +++ b/compiler/src/builtins.c @@ -36,8 +36,6 @@ AstBasicType basic_type_v128 = { Ast_Kind_Basic_Type, Ast_Flag_Comptime, &simd_ 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 } }; @@ -423,9 +421,6 @@ void prepare_builtins() { } 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) diff --git a/compiler/src/entities.c b/compiler/src/entities.c index 6cfac5a9..1c3ab58e 100644 --- a/compiler/src/entities.c +++ b/compiler/src/entities.c @@ -74,14 +74,12 @@ void entity_heap_init(EntityHeap* entities) { 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; diff --git a/compiler/src/lex.c b/compiler/src/lex.c index 45918bc9..20126a1d 100644 --- a/compiler/src/lex.c +++ b/compiler/src/lex.c @@ -3,9 +3,6 @@ #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", @@ -513,8 +510,8 @@ void onyx_lex_tokens(OnyxTokenizer* tokenizer) { 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) { diff --git a/compiler/src/onyx.c b/compiler/src/onyx.c index eb6fc9de..59668dd3 100644 --- a/compiler/src/onyx.c +++ b/compiler/src/onyx.c @@ -321,11 +321,11 @@ static Entity *runtime_info_foreign_entity; 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; @@ -394,6 +394,12 @@ static void context_init(CompileOptions* opts) { })); } + 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); @@ -796,8 +802,8 @@ static i32 onyx_compile() { // 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"); } @@ -914,9 +920,9 @@ CompilerProgress do_compilation(CompileOptions *compile_opts) { 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(); @@ -926,7 +932,7 @@ void cleanup_compilation() { context_free(); bh_scratch_free(&global_scratch); - // bh_managed_heap_free(&mh); + bh_managed_heap_free(&mh); } int main(int argc, char *argv[]) { diff --git a/compiler/src/types.c b/compiler/src/types.c index 3887ff13..8445b905 100644 --- a/compiler/src/types.c +++ b/compiler/src/types.c @@ -58,24 +58,27 @@ static Type* type_create(TypeKind kind, bh_allocator a, u32 extra_type_pointer_c } 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() { diff --git a/core/container/result.onyx b/core/container/result.onyx index 428a8eed..d3836b29 100644 --- a/core/container/result.onyx +++ b/core/container/result.onyx @@ -191,3 +191,12 @@ __implicit_bool_cast :: macro (r: Result($O, $E)) => r.status == .Ok; 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; +} + + diff --git a/shared/include/bh.h b/shared/include/bh.h index 5676e05d..8c95ed72 100644 --- a/shared/include/bh.h +++ b/shared/include/bh.h @@ -815,8 +815,15 @@ void bh_imap_clear(bh_imap* imap); // 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); @@ -1007,19 +1014,26 @@ BH_ALLOCATOR_PROC(bh_heap_allocator_proc) { // 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) { @@ -1031,47 +1045,45 @@ 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; } -- 2.25.1