From: Brendan Hansen Date: Sun, 23 Apr 2023 19:09:17 +0000 (-0500) Subject: fixed: more special global variable cleanup X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=c1bb01e3b6e06a54878edba3ab8094c800dd4471;p=onyx.git fixed: more special global variable cleanup --- diff --git a/compiler/include/astnodes.h b/compiler/include/astnodes.h index 2f2f7488..d02a97ac 100644 --- a/compiler/include/astnodes.h +++ b/compiler/include/astnodes.h @@ -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; diff --git a/compiler/src/astnodes.c b/compiler/src/astnodes.c index 1722dd21..8ad49225 100644 --- a/compiler/src/astnodes.c +++ b/compiler/src/astnodes.c @@ -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; } diff --git a/compiler/src/builtins.c b/compiler/src/builtins.c index 86487b13..ecf770c5 100644 --- a/compiler/src/builtins.c +++ b/compiler/src/builtins.c @@ -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) { diff --git a/compiler/src/errors.c b/compiler/src/errors.c index 6bfaa80a..a81ed4b2 100644 --- a/compiler/src/errors.c +++ b/compiler/src/errors.c @@ -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; diff --git a/compiler/src/onyx.c b/compiler/src/onyx.c index 35961c69..eb6fc9de 100644 --- a/compiler/src/onyx.c +++ b/compiler/src/onyx.c @@ -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; diff --git a/shared/include/bh.h b/shared/include/bh.h index 71e168db..5676e05d 100644 --- a/shared/include/bh.h +++ b/shared/include/bh.h @@ -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: {