fixed: removed all random static variables
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 26 Apr 2023 03:01:01 +0000 (22:01 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 26 Apr 2023 03:01:01 +0000 (22:01 -0500)
compiler/include/astnodes.h
compiler/src/builtins.c
compiler/src/entities.c
compiler/src/lex.c
compiler/src/onyx.c
compiler/src/types.c
core/container/result.onyx
shared/include/bh.h

index d02a97ac91ce835abffc0ce9074da4290f4747d3..59bfa39b0a4b7651109ad5007c102482c498b554 100644 (file)
@@ -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;
index ecf770c50f91952632fdc2f2e10748946f701aa5..42a6523ec2fa52823ddae5ecf9dc71eb2efe2897 100644 (file)
@@ -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)
index 6cfac5a9c9bd515df868db7e12f7569276f6fe3b..1c3ab58ee8bd6c1f85edd813bda862dcfd040ccc 100644 (file)
@@ -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;
index 45918bc9efa20b3276295d02ccd2a855ab3490f1..20126a1d066748a06d8c17129ccf08028a2062b2 100644 (file)
@@ -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) {
index eb6fc9de02f782fa78ec3742c0e37b1a2d47fc86..59668dd3fc209650394ebe80d9aab80fd8d287ba 100644 (file)
@@ -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[]) {
index 3887ff13283a5fc9646e445c7d43ed4ab6ae2a6a..8445b905f03aca3c6093950990ab223ffd86e744 100644 (file)
@@ -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() {
index 428a8eed94f81911f87b4a32ba9fbd00d33d3294..d3836b290fdee0545adea47d4af5f453c754e195 100644 (file)
@@ -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;
+}
+
+
index 5676e05df052bed92740222d5e2996dea8bbf707..8c95ed72d961896b05a06b47bcd89aa361d9a70f 100644 (file)
@@ -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;
 }