fixed: more special global variable cleanup
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 23 Apr 2023 19:09:17 +0000 (14:09 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Sun, 23 Apr 2023 19:09:17 +0000 (14:09 -0500)
compiler/include/astnodes.h
compiler/src/astnodes.c
compiler/src/builtins.c
compiler/src/errors.c
compiler/src/onyx.c
shared/include/bh.h

index 2f2f7488613b8fe02f5ddd1c565e5d0e26f28c4c..d02a97ac91ce835abffc0ce9074da4290f4747d3 100644 (file)
@@ -1707,6 +1707,10 @@ typedef struct CheckerData {
     bh_arr(Type **) expected_return_type_stack;
 } CheckerData;
 
+typedef struct ContextCaches {
+    bh_imap implicit_cast_to_bool_cache;
+} ContextCaches; 
+
 
 typedef struct CompileOptions CompileOptions;
 struct CompileOptions {
@@ -1770,6 +1774,10 @@ struct Context {
     struct OnyxDocInfo     *doc_info;
 
     CheckerData checker;
+    ContextCaches caches;
+    OnyxErrors errors;
+    b32 errors_enabled;
+
     u32 next_package_id;
     u32 next_scope_id;
 
index 1722dd21651caf21519c58ac9c354de79c0474b3..8ad492251b55e4d28e9a96ddd8ad5210b4e9b192 100644 (file)
@@ -1359,8 +1359,6 @@ b32 cast_is_legal(Type* from_, Type* to_, char** err_msg) {
 
 
 
-static bh_imap implicit_cast_to_bool_cache;
-
 TypeMatch implicit_cast_to_bool(AstTyped **pnode) {
     AstTyped *node = *pnode;
 
@@ -1402,21 +1400,21 @@ TypeMatch implicit_cast_to_bool(AstTyped **pnode) {
         return TYPE_MATCH_SUCCESS;
     }
     
-    if (implicit_cast_to_bool_cache.entries == NULL) {
-        bh_imap_init(&implicit_cast_to_bool_cache, global_heap_allocator, 8);
+    if (context.caches.implicit_cast_to_bool_cache.entries == NULL) {
+        bh_imap_init(&context.caches.implicit_cast_to_bool_cache, global_heap_allocator, 8);
     }
 
-    if (!bh_imap_has(&implicit_cast_to_bool_cache, (u64) node)) {
+    if (!bh_imap_has(&context.caches.implicit_cast_to_bool_cache, (u64) node)) {
         AstArgument *implicit_arg = make_argument(context.ast_alloc, node);
         
         Arguments *args = bh_alloc_item(context.ast_alloc, Arguments);
         bh_arr_new(context.ast_alloc, args->values, 1);
         bh_arr_push(args->values, (AstTyped *) implicit_arg);
 
-        bh_imap_put(&implicit_cast_to_bool_cache, (u64) node, (u64) args);
+        bh_imap_put(&context.caches.implicit_cast_to_bool_cache, (u64) node, (u64) args);
     }
     
-    Arguments *args = (Arguments *) bh_imap_get(&implicit_cast_to_bool_cache, (u64) node);
+    Arguments *args = (Arguments *) bh_imap_get(&context.caches.implicit_cast_to_bool_cache, (u64) node);
     AstFunction *overload = (AstFunction *) find_matching_overload_by_arguments(builtin_implicit_bool_cast->overloads, args);
 
     if (overload == NULL)                                       return TYPE_MATCH_FAILED;
@@ -1429,7 +1427,7 @@ TypeMatch implicit_cast_to_bool(AstTyped **pnode) {
     implicit_call->args.values = args->values;
 
     *(AstCall **) pnode = implicit_call;
-    bh_imap_delete(&implicit_cast_to_bool_cache, (u64) node);
+    bh_imap_delete(&context.caches.implicit_cast_to_bool_cache, (u64) node);
 
     return TYPE_MATCH_YIELD;
 }
index 86487b132d34e2c668eabe20b258dbaccc53ab1e..ecf770c50f91952632fdc2f2e10748946f701aa5 100644 (file)
@@ -405,6 +405,21 @@ void prepare_builtins() {
     builtin_run_init_procedures = NULL;
     init_procedures = NULL;
     builtin_implicit_bool_cast = NULL;
+
+    basic_type_void.scope = NULL;
+    basic_type_bool.scope = NULL;
+    basic_type_i8.scope = NULL;
+    basic_type_u8.scope = NULL;
+    basic_type_i16.scope = NULL;
+    basic_type_u16.scope = NULL;
+    basic_type_i32.scope = NULL;
+    basic_type_u32.scope = NULL;
+    basic_type_i64.scope = NULL;
+    basic_type_u64.scope = NULL;
+    basic_type_f32.scope = NULL;
+    basic_type_f64.scope = NULL;
+    basic_type_rawptr.scope = NULL;
+    basic_type_type_expr.scope = NULL;
 }
 
 void initialize_builtins(bh_allocator a) {
index 6bfaa80aec484a728a59b91d410781fdc5342362..a81ed4b2ad0ee85cfcaead980448d9b14fafdd7e 100644 (file)
@@ -1,16 +1,13 @@
 #include "errors.h"
 #include "utils.h"
 
-OnyxErrors errors;
-static b32 errors_enabled = 1;
-
 void onyx_errors_init(bh_arr(bh_file_contents)* files) {
-    errors.file_contents = files;
+    context.errors.file_contents = files;
 
-    bh_arena_init(&errors.msg_arena, global_heap_allocator, 16 * 1024);
-    errors.msg_alloc = bh_arena_allocator(&errors.msg_arena);
+    bh_arena_init(&context.errors.msg_arena, global_heap_allocator, 16 * 1024);
+    context.errors.msg_alloc = bh_arena_allocator(&context.errors.msg_arena);
 
-    bh_arr_new(global_heap_allocator, errors.errors, 4);
+    bh_arr_new(global_heap_allocator, context.errors.errors, 4);
 }
 
 static void print_detailed_message(OnyxError* err, bh_file_contents* fc) {
@@ -60,15 +57,15 @@ void onyx_errors_print() {
     //
     //                                      - brendanfh   2020/09/03
 
-    qsort(errors.errors, bh_arr_length(errors.errors), sizeof(OnyxError), errors_sort);
+    qsort(context.errors.errors, bh_arr_length(context.errors.errors), sizeof(OnyxError), errors_sort);
 
-    OnyxErrorRank last_rank = errors.errors[0].rank;
-    bh_arr_each(OnyxError, err, errors.errors) {
+    OnyxErrorRank last_rank = context.errors.errors[0].rank;
+    bh_arr_each(OnyxError, err, context.errors.errors) {
         if (!context.options->show_all_errors && last_rank != err->rank) break;
 
         if (err->pos.filename) {
             bh_file_contents file_contents = { 0 };
-            bh_arr_each(bh_file_contents, fc, *errors.file_contents) {
+            bh_arr_each(bh_file_contents, fc, *context.errors.file_contents) {
                 if (!strcmp(fc->filename, err->pos.filename)) {
                     file_contents = *fc;
                     break;
@@ -86,24 +83,24 @@ void onyx_errors_print() {
 }
 
 void onyx_errors_enable() {
-    errors_enabled = 1;
+    context.errors_enabled = 1;
 }
 
 void onyx_errors_disable() {
     if (context.cycle_detected) {
-        errors_enabled = 1;
+        context.errors_enabled = 1;
         return;
     }
     
-    errors_enabled = 0;
+    context.errors_enabled = 0;
 }
 
 b32 onyx_errors_are_enabled() {
-    return errors_enabled;
+    return context.errors_enabled;
 }
 
 b32 onyx_has_errors() {
-    bh_arr_each(OnyxError, err, errors.errors) {
+    bh_arr_each(OnyxError, err, context.errors.errors) {
         if (err->rank >= Error_Waiting_On) return 1;
     }
 
@@ -113,17 +110,17 @@ b32 onyx_has_errors() {
 void onyx_clear_errors() {
     if (context.cycle_detected) return;
 
-    bh_arr_set_length(errors.errors, 0);
+    bh_arr_set_length(context.errors.errors, 0);
 }
 
 void onyx_submit_error(OnyxError error) {
-    if (!errors_enabled) return;
+    if (!context.errors_enabled) return;
 
-    bh_arr_push(errors.errors, error);
+    bh_arr_push(context.errors.errors, error);
 }
 
 void onyx_report_error(OnyxFilePos pos, OnyxErrorRank rank, char * format, ...) {
-    if (!errors_enabled) return;
+    if (!context.errors_enabled) return;
 
     va_list vargs;
     va_start(vargs, format);
@@ -133,17 +130,17 @@ void onyx_report_error(OnyxFilePos pos, OnyxErrorRank rank, char * format, ...)
     OnyxError err = {
         .pos = pos,
         .rank = rank,
-        .text = bh_strdup(errors.msg_alloc, msg),
+        .text = bh_strdup(context.errors.msg_alloc, msg),
     };
 
-    bh_arr_push(errors.errors, err);
+    bh_arr_push(context.errors.errors, err);
 }
 
 void onyx_submit_warning(OnyxError error) {
-    if (!errors_enabled) return;
+    if (!context.errors_enabled) return;
 
     bh_file_contents file_contents = { 0 };
-    bh_arr_each(bh_file_contents, fc, *errors.file_contents) {
+    bh_arr_each(bh_file_contents, fc, *context.errors.file_contents) {
         if (!strcmp(fc->filename, error.pos.filename)) {
             file_contents = *fc;
             break;
@@ -155,7 +152,7 @@ void onyx_submit_warning(OnyxError error) {
 
 // This definitely doesn't do what I thought it did?
 void onyx_report_warning(OnyxFilePos pos, char* format, ...) {
-    if (!errors_enabled) return;
+    if (!context.errors_enabled) return;
 
     va_list vargs;
     va_start(vargs, format);
@@ -165,15 +162,15 @@ void onyx_report_warning(OnyxFilePos pos, char* format, ...) {
     OnyxError err = {
         .pos = pos,
         .rank = Error_Warning,
-        .text = bh_strdup(errors.msg_alloc, msg),
+        .text = bh_strdup(context.errors.msg_alloc, msg),
     };
 
-    bh_arr_push(errors.errors, err);
+    bh_arr_push(context.errors.errors, err);
 
     /*
 
     bh_file_contents file_contents = { 0 };
-    bh_arr_each(bh_file_contents, fc, *errors.file_contents) {
+    bh_arr_each(bh_file_contents, fc, *context.errors.file_contents) {
         if (!strcmp(fc->filename, pos.filename)) {
             file_contents = *fc;
             break;
index 35961c69e34221956dae9971e32fc80c70a2b02a..eb6fc9de02f782fa78ec3742c0e37b1a2d47fc86 100644 (file)
@@ -326,6 +326,9 @@ static void context_init(CompileOptions* opts) {
 
     memset(&context, 0, sizeof context);
 
+    // HACK
+    special_global_entities_remaining = 3;
+
     context.options = opts;
     context.cycle_detected = 0;
     context.cycle_almost_detected = 0;
index 71e168dbdf721cab25612974584c9b22d2fef7aa..5676e05df052bed92740222d5e2996dea8bbf707 100644 (file)
@@ -1050,13 +1050,15 @@ BH_ALLOCATOR_PROC(bh_managed_heap_allocator_proc) {
     } break;
 
     case bh_allocator_action_resize: {
-        bh_imap_delete(&mh->ptrs, (u64) prev_memory);
 #if defined(_BH_WINDOWS)
         retval = _aligned_realloc(prev_memory, size, alignment);
 #elif defined(_BH_LINUX)
         retval = realloc(prev_memory, size);
 #endif
-        bh_imap_put(&mh->ptrs, (u64) retval, 1);
+        if (prev_memory != retval) {
+            bh_imap_delete(&mh->ptrs, (u64) prev_memory);
+            bh_imap_put(&mh->ptrs, (u64) retval, 1);
+        }
     } break;
 
     case bh_allocator_action_free: {