From a7f8c78358a0d7258a3175a82dbe8ebee008ea03 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 12 Jun 2023 12:52:17 +0100 Subject: Change how "$anon" mangling works to use an atomic value instead --- src/llvm_backend.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/llvm_backend.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 34a401c33..01d18d868 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -787,7 +787,8 @@ gb_internal lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &pr // parent$count isize name_len = prefix_name.len + 1 + 8 + 1; char *name_text = gb_alloc_array(permanent_allocator(), char, name_len); - i32 name_id = cast(i32)m->gen->anonymous_proc_lits.count; + static std::atomic name_id; + name_id.fetch_add(1); name_len = gb_snprintf(name_text, name_len, "%.*s$anon-%d", LIT(prefix_name), name_id); String name = make_string((u8 *)name_text, name_len-1); -- cgit v1.2.3 From ede98814587319815dc7050b8ff96132bf683592 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 12 Jun 2023 12:54:36 +0100 Subject: Keep compilers happy --- src/llvm_backend.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/llvm_backend.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 01d18d868..2897d3ca7 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -790,7 +790,7 @@ gb_internal lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &pr static std::atomic name_id; name_id.fetch_add(1); - name_len = gb_snprintf(name_text, name_len, "%.*s$anon-%d", LIT(prefix_name), name_id); + name_len = gb_snprintf(name_text, name_len, "%.*s$anon-%d", LIT(prefix_name), name_id.load()); String name = make_string((u8 *)name_text, name_len-1); Type *type = type_of_expr(expr); -- cgit v1.2.3 From dca0fae781c9a21cacda44eaa9fd08d1929e0854 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 12 Jun 2023 13:42:51 +0100 Subject: Improve lb_generate_anonymous_proc_lit (again) --- src/llvm_backend.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/llvm_backend.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 2897d3ca7..1acd15ad6 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -785,12 +785,10 @@ gb_internal lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &pr // NOTE(bill): Generate a new name // parent$count - isize name_len = prefix_name.len + 1 + 8 + 1; + isize name_len = prefix_name.len + 6 + 11; char *name_text = gb_alloc_array(permanent_allocator(), char, name_len); static std::atomic name_id; - name_id.fetch_add(1); - - name_len = gb_snprintf(name_text, name_len, "%.*s$anon-%d", LIT(prefix_name), name_id.load()); + name_len = gb_snprintf(name_text, name_len, "%.*s$anon-%d", LIT(prefix_name), name_id.fetch_add(1)); String name = make_string((u8 *)name_text, name_len-1); Type *type = type_of_expr(expr); -- cgit v1.2.3 From 2022a7615aeb476521ad4eac168b2cdc231ba931 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 12 Jun 2023 14:10:18 +0100 Subject: Make all id suffixes use atomics where possible --- src/llvm_backend.cpp | 13 +++++++------ src/llvm_backend.hpp | 2 +- src/llvm_backend_general.cpp | 7 +++---- 3 files changed, 11 insertions(+), 11 deletions(-) (limited to 'src/llvm_backend.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 1acd15ad6..4fe8d4a00 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -157,10 +157,10 @@ gb_internal lbValue lb_equal_proc_for_type(lbModule *m, Type *type) { return {compare_proc->value, compare_proc->type}; } - static u32 proc_index = 0; + static std::atomic proc_index; char buf[32] = {}; - isize n = gb_snprintf(buf, 32, "__$equal%u", ++proc_index); + isize n = gb_snprintf(buf, 32, "__$equal%u", 1+proc_index.fetch_add(1)); char *str = gb_alloc_str_len(permanent_allocator(), buf, n-1); String proc_name = make_string_c(str); @@ -656,10 +656,10 @@ gb_internal lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) { GB_ASSERT(*found != nullptr); return {(*found)->value, (*found)->type}; } - static u32 proc_index = 0; + static std::atomic proc_index; char buf[32] = {}; - isize n = gb_snprintf(buf, 32, "__$map_set-%u", ++proc_index); + isize n = gb_snprintf(buf, 32, "__$map_set-%u", 1+proc_index.fetch_add(1)); char *str = gb_alloc_str_len(permanent_allocator(), buf, n-1); String proc_name = make_string_c(str); @@ -774,7 +774,8 @@ gb_internal lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) { gb_internal lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, Ast *expr, lbProcedure *parent) { - MUTEX_GUARD(&m->gen->anonymous_proc_lits_mutex); + mutex_lock(&m->gen->anonymous_proc_lits_mutex); + defer (mutex_unlock(&m->gen->anonymous_proc_lits_mutex)); lbProcedure **found = map_get(&m->gen->anonymous_proc_lits, expr); if (found) { @@ -788,7 +789,7 @@ gb_internal lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &pr isize name_len = prefix_name.len + 6 + 11; char *name_text = gb_alloc_array(permanent_allocator(), char, name_len); static std::atomic name_id; - name_len = gb_snprintf(name_text, name_len, "%.*s$anon-%d", LIT(prefix_name), name_id.fetch_add(1)); + name_len = gb_snprintf(name_text, name_len, "%.*s$anon-%d", LIT(prefix_name), 1+name_id.fetch_add(1)); String name = make_string((u8 *)name_text, name_len-1); Type *type = type_of_expr(expr); diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 4c4d9703d..6e785d492 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -164,7 +164,7 @@ struct lbModule { PtrMap map_get_procs; PtrMap map_set_procs; - u32 nested_type_name_guid; + std::atomic nested_type_name_guid; Array procedures_to_generate; Array global_procedures_and_types_to_create; diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index e5f3e3081..176e53042 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -1426,7 +1426,7 @@ gb_internal String lb_set_nested_type_name_ir_mangled_name(Entity *e, lbProcedur if (p != nullptr) { isize name_len = p->name.len + 1 + ts_name.len + 1 + 10 + 1; char *name_text = gb_alloc_array(permanent_allocator(), char, name_len); - u32 guid = ++p->module->nested_type_name_guid; + u32 guid = 1+p->module->nested_type_name_guid.fetch_add(1); name_len = gb_snprintf(name_text, name_len, "%.*s.%.*s-%u", LIT(p->name), LIT(ts_name), guid); String name = make_string(cast(u8 *)name_text, name_len-1); @@ -1436,9 +1436,8 @@ gb_internal String lb_set_nested_type_name_ir_mangled_name(Entity *e, lbProcedur // NOTE(bill): a nested type be required before its parameter procedure exists. Just give it a temp name for now isize name_len = 9 + 1 + ts_name.len + 1 + 10 + 1; char *name_text = gb_alloc_array(permanent_allocator(), char, name_len); - static u32 guid = 0; - guid += 1; - name_len = gb_snprintf(name_text, name_len, "_internal.%.*s-%u", LIT(ts_name), guid); + static std::atomic guid; + name_len = gb_snprintf(name_text, name_len, "_internal.%.*s-%u", LIT(ts_name), 1+guid.fetch_add(1)); String name = make_string(cast(u8 *)name_text, name_len-1); e->TypeName.ir_mangled_name = name; -- cgit v1.2.3 From 296674e18b5ce3ba90013fb5d599251c66d8bd18 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 12 Jun 2023 14:53:05 +0100 Subject: Rename `ODIN_DISALLOW_RTTI` to `ODIN_NO_RTTI`; Remove dead command line flags --- core/reflect/reflect.odin | 2 +- core/runtime/core.odin | 2 +- core/runtime/error_checks.odin | 2 +- core/runtime/print.odin | 4 ++-- src/build_settings.cpp | 6 +++--- src/check_builtin.cpp | 4 ++-- src/checker.cpp | 8 ++++---- src/llvm_backend.cpp | 4 ++-- src/llvm_backend_expr.cpp | 6 +++--- src/llvm_backend_type.cpp | 6 +++--- src/llvm_backend_utility.cpp | 8 ++++---- src/main.cpp | 16 +--------------- 12 files changed, 27 insertions(+), 41 deletions(-) (limited to 'src/llvm_backend.cpp') diff --git a/core/reflect/reflect.odin b/core/reflect/reflect.odin index f8343ead2..2eb31c21b 100644 --- a/core/reflect/reflect.odin +++ b/core/reflect/reflect.odin @@ -132,7 +132,7 @@ type_info_core :: runtime.type_info_core type_info_base_without_enum :: type_info_core -when !ODIN_DISALLOW_RTTI { +when !ODIN_NO_RTTI { typeid_base :: runtime.typeid_base typeid_core :: runtime.typeid_core typeid_base_without_enum :: typeid_core diff --git a/core/runtime/core.odin b/core/runtime/core.odin index 058ca6161..83504c9ee 100644 --- a/core/runtime/core.odin +++ b/core/runtime/core.odin @@ -566,7 +566,7 @@ __type_info_of :: proc "contextless" (id: typeid) -> ^Type_Info #no_bounds_check return &type_table[n] } -when !ODIN_DISALLOW_RTTI { +when !ODIN_NO_RTTI { typeid_base :: proc "contextless" (id: typeid) -> typeid { ti := type_info_of(id) ti = type_info_base(ti) diff --git a/core/runtime/error_checks.odin b/core/runtime/error_checks.odin index 8539c724d..a667b9065 100644 --- a/core/runtime/error_checks.odin +++ b/core/runtime/error_checks.odin @@ -122,7 +122,7 @@ matrix_bounds_check_error :: proc "contextless" (file: string, line, column: i32 } -when ODIN_DISALLOW_RTTI { +when ODIN_NO_RTTI { type_assertion_check :: proc "contextless" (ok: bool, file: string, line, column: i32) { if ok { return diff --git a/core/runtime/print.odin b/core/runtime/print.odin index 326a29667..732ed9c12 100644 --- a/core/runtime/print.odin +++ b/core/runtime/print.odin @@ -5,7 +5,7 @@ _INTEGER_DIGITS :: "0123456789abcdefghijklmnopqrstuvwxyz" @(private="file") _INTEGER_DIGITS_VAR := _INTEGER_DIGITS -when !ODIN_DISALLOW_RTTI { +when !ODIN_NO_RTTI { print_any_single :: proc "contextless" (arg: any) { x := arg if loc, ok := x.(Source_Code_Location); ok { @@ -234,7 +234,7 @@ print_caller_location :: proc "contextless" (using loc: Source_Code_Location) { } } print_typeid :: proc "contextless" (id: typeid) { - when ODIN_DISALLOW_RTTI { + when ODIN_NO_RTTI { if id == nil { print_string("nil") } else { diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 92e0df38b..866631f9a 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -309,7 +309,7 @@ struct BuildContext { bool copy_file_contents; - bool disallow_rtti; + bool no_rtti; bool dynamic_map_calls; @@ -1227,8 +1227,8 @@ gb_internal void init_build_context(TargetMetrics *cross_target) { if (bc->metrics.os == TargetOs_freestanding) { bc->no_entry_point = true; } else { - if (bc->disallow_rtti) { - gb_printf_err("-disallow-rtti is only allowed on freestanding targets\n"); + if (bc->no_rtti) { + gb_printf_err("-no-rtti is only allowed on freestanding targets\n"); gb_exit(1); } } diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 46ee6b7f9..269a0ec48 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -2063,7 +2063,7 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As if (c->scope->flags&ScopeFlag_Global) { compiler_error("'type_info_of' Cannot be declared within the runtime package due to how the internals of the compiler works"); } - if (build_context.disallow_rtti) { + if (build_context.no_rtti) { error(call, "'%.*s' has been disallowed", LIT(builtin_name)); return false; } @@ -2106,7 +2106,7 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As if (c->scope->flags&ScopeFlag_Global) { compiler_error("'typeid_of' Cannot be declared within the runtime package due to how the internals of the compiler works"); } - if (build_context.disallow_rtti) { + if (build_context.no_rtti) { error(call, "'%.*s' has been disallowed", LIT(builtin_name)); return false; } diff --git a/src/checker.cpp b/src/checker.cpp index a8227fc2e..354cdadd3 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -32,7 +32,7 @@ gb_internal bool is_operand_uninit(Operand o) { } gb_internal bool check_rtti_type_disallowed(Token const &token, Type *type, char const *format) { - if (build_context.disallow_rtti && type) { + if (build_context.no_rtti && type) { if (is_type_any(type)) { gbString t = type_to_string(type); error(token, format, t); @@ -1054,7 +1054,7 @@ gb_internal void init_universal(void) { add_global_bool_constant("ODIN_TEST", bc->command_kind == Command_test); add_global_bool_constant("ODIN_NO_ENTRY_POINT", bc->no_entry_point); add_global_bool_constant("ODIN_FOREIGN_ERROR_PROCEDURES", bc->ODIN_FOREIGN_ERROR_PROCEDURES); - add_global_bool_constant("ODIN_DISALLOW_RTTI", bc->disallow_rtti); + add_global_bool_constant("ODIN_NO_RTTI", bc->no_rtti); add_global_bool_constant("ODIN_VALGRIND_SUPPORT", bc->ODIN_VALGRIND_SUPPORT); @@ -1742,7 +1742,7 @@ gb_internal void add_implicit_entity(CheckerContext *c, Ast *clause, Entity *e) gb_internal void add_type_info_type_internal(CheckerContext *c, Type *t); gb_internal void add_type_info_type(CheckerContext *c, Type *t) { - if (build_context.disallow_rtti) { + if (build_context.no_rtti) { return; } if (t == nullptr) { @@ -2343,7 +2343,7 @@ gb_internal void generate_minimum_dependency_set(Checker *c, Entity *start) { str_lit("__multi3"), ); - FORCE_ADD_RUNTIME_ENTITIES(!build_context.disallow_rtti, + FORCE_ADD_RUNTIME_ENTITIES(!build_context.no_rtti, // Odin types str_lit("Type_Info"), diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 4fe8d4a00..525ac8a9d 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1048,7 +1048,7 @@ struct lbGlobalVariable { }; gb_internal lbProcedure *lb_create_startup_type_info(lbModule *m) { - if (build_context.disallow_rtti) { + if (build_context.no_rtti) { return nullptr; } Type *proc_type = alloc_type_proc(nullptr, nullptr, 0, nullptr, 0, false, ProcCC_CDecl); @@ -2170,7 +2170,7 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { TIME_SECTION("LLVM Global Variables"); - if (!build_context.disallow_rtti) { + if (!build_context.no_rtti) { lbModule *m = default_module; { // Add type info data diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index f95e351ce..619b40fda 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -2984,7 +2984,7 @@ gb_internal lbValue lb_build_unary_and(lbProcedure *p, Ast *expr) { isize arg_count = 6; - if (build_context.disallow_rtti) { + if (build_context.no_rtti) { arg_count = 4; } @@ -2996,7 +2996,7 @@ gb_internal lbValue lb_build_unary_and(lbProcedure *p, Ast *expr) { args[2] = lb_const_int(p->module, t_i32, pos.line); args[3] = lb_const_int(p->module, t_i32, pos.column); - if (!build_context.disallow_rtti) { + if (!build_context.no_rtti) { args[4] = lb_typeid(p->module, src_type); args[5] = lb_typeid(p->module, dst_type); } @@ -3012,7 +3012,7 @@ gb_internal lbValue lb_build_unary_and(lbProcedure *p, Ast *expr) { } lbValue data_ptr = lb_emit_struct_ev(p, v, 0); if ((p->state_flags & StateFlag_no_type_assert) == 0) { - GB_ASSERT(!build_context.disallow_rtti); + GB_ASSERT(!build_context.no_rtti); lbValue any_id = lb_emit_struct_ev(p, v, 1); diff --git a/src/llvm_backend_type.cpp b/src/llvm_backend_type.cpp index 1e26fd6bd..a54fde748 100644 --- a/src/llvm_backend_type.cpp +++ b/src/llvm_backend_type.cpp @@ -15,7 +15,7 @@ gb_internal isize lb_type_info_index(CheckerInfo *info, Type *type, bool err_on_ } gb_internal lbValue lb_typeid(lbModule *m, Type *type) { - GB_ASSERT(!build_context.disallow_rtti); + GB_ASSERT(!build_context.no_rtti); type = default_type(type); @@ -92,7 +92,7 @@ gb_internal lbValue lb_typeid(lbModule *m, Type *type) { } gb_internal lbValue lb_type_info(lbModule *m, Type *type) { - GB_ASSERT(!build_context.disallow_rtti); + GB_ASSERT(!build_context.no_rtti); type = default_type(type); @@ -141,7 +141,7 @@ gb_internal lbValue lb_type_info_member_tags_offset(lbProcedure *p, isize count) gb_internal void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info data - if (build_context.disallow_rtti) { + if (build_context.no_rtti) { return; } diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp index 0c26382ed..2ecad1703 100644 --- a/src/llvm_backend_utility.cpp +++ b/src/llvm_backend_utility.cpp @@ -721,7 +721,7 @@ gb_internal lbValue lb_emit_union_cast(lbProcedure *p, lbValue value, Type *type Type *dst_type = tuple->Tuple.variables[0]->type; isize arg_count = 7; - if (build_context.disallow_rtti) { + if (build_context.no_rtti) { arg_count = 4; } @@ -733,7 +733,7 @@ gb_internal lbValue lb_emit_union_cast(lbProcedure *p, lbValue value, Type *type args[2] = lb_const_int(m, t_i32, pos.line); args[3] = lb_const_int(m, t_i32, pos.column); - if (!build_context.disallow_rtti) { + if (!build_context.no_rtti) { args[4] = lb_typeid(m, src_type); args[5] = lb_typeid(m, dst_type); args[6] = lb_emit_conv(p, value_, t_rawptr); @@ -797,7 +797,7 @@ gb_internal lbAddr lb_emit_any_cast_addr(lbProcedure *p, lbValue value, Type *ty lbValue ok = lb_emit_load(p, lb_emit_struct_ep(p, v.addr, 1)); isize arg_count = 7; - if (build_context.disallow_rtti) { + if (build_context.no_rtti) { arg_count = 4; } auto args = array_make(permanent_allocator(), arg_count); @@ -807,7 +807,7 @@ gb_internal lbAddr lb_emit_any_cast_addr(lbProcedure *p, lbValue value, Type *ty args[2] = lb_const_int(m, t_i32, pos.line); args[3] = lb_const_int(m, t_i32, pos.column); - if (!build_context.disallow_rtti) { + if (!build_context.no_rtti) { args[4] = any_typeid; args[5] = dst_typeid; args[6] = lb_emit_struct_ev(p, value, 0); diff --git a/src/main.cpp b/src/main.cpp index 029c9f896..2198b4536 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -655,7 +655,6 @@ enum BuildFlagKind { BuildFlag_ShowDebugMessages, BuildFlag_Vet, BuildFlag_VetExtra, - BuildFlag_UseLLVMApi, BuildFlag_IgnoreUnknownAttributes, BuildFlag_ExtraLinkerFlags, BuildFlag_ExtraAssemblerFlags, @@ -671,7 +670,6 @@ enum BuildFlagKind { BuildFlag_DisallowDo, BuildFlag_DefaultToNilAllocator, - BuildFlag_InsertSemicolon, BuildFlag_StrictStyle, BuildFlag_StrictStyleInitOnly, BuildFlag_ForeignErrorProcedures, @@ -834,7 +832,6 @@ gb_internal bool parse_build_flags(Array args) { add_flag(&build_flags, BuildFlag_ShowDebugMessages, str_lit("show-debug-messages"), BuildFlagParam_None, Command_all); add_flag(&build_flags, BuildFlag_Vet, str_lit("vet"), BuildFlagParam_None, Command__does_check); add_flag(&build_flags, BuildFlag_VetExtra, str_lit("vet-extra"), BuildFlagParam_None, Command__does_check); - add_flag(&build_flags, BuildFlag_UseLLVMApi, str_lit("llvm-api"), BuildFlagParam_None, Command__does_build); add_flag(&build_flags, BuildFlag_IgnoreUnknownAttributes, str_lit("ignore-unknown-attributes"), BuildFlagParam_None, Command__does_check); add_flag(&build_flags, BuildFlag_ExtraLinkerFlags, str_lit("extra-linker-flags"), BuildFlagParam_String, Command__does_build); add_flag(&build_flags, BuildFlag_ExtraAssemblerFlags, str_lit("extra-assembler-flags"), BuildFlagParam_String, Command__does_build); @@ -849,7 +846,6 @@ gb_internal bool parse_build_flags(Array args) { add_flag(&build_flags, BuildFlag_DisallowDo, str_lit("disallow-do"), BuildFlagParam_None, Command__does_check); add_flag(&build_flags, BuildFlag_DefaultToNilAllocator, str_lit("default-to-nil-allocator"), BuildFlagParam_None, Command__does_check); - add_flag(&build_flags, BuildFlag_InsertSemicolon, str_lit("insert-semicolon"), BuildFlagParam_None, Command__does_check); add_flag(&build_flags, BuildFlag_StrictStyle, str_lit("strict-style"), BuildFlagParam_None, Command__does_check); add_flag(&build_flags, BuildFlag_StrictStyleInitOnly, str_lit("strict-style-init-only"), BuildFlagParam_None, Command__does_check); add_flag(&build_flags, BuildFlag_ForeignErrorProcedures, str_lit("foreign-error-procedures"), BuildFlagParam_None, Command__does_check); @@ -1373,11 +1369,6 @@ gb_internal bool parse_build_flags(Array args) { build_context.vet_extra = true; break; } - case BuildFlag_UseLLVMApi: { - gb_printf_err("-llvm-api flag is not required any more\n"); - bad_flags = true; - break; - } case BuildFlag_IgnoreUnknownAttributes: build_context.ignore_unknown_attributes = true; break; @@ -1454,7 +1445,7 @@ gb_internal bool parse_build_flags(Array args) { gb_printf_err("'-disallow-rtti' has been replaced with '-no-rtti'\n"); bad_flags = true; } - build_context.disallow_rtti = true; + build_context.no_rtti = true; break; case BuildFlag_DynamicMapCalls: build_context.dynamic_map_calls = true; @@ -1465,11 +1456,6 @@ gb_internal bool parse_build_flags(Array args) { case BuildFlag_ForeignErrorProcedures: build_context.ODIN_FOREIGN_ERROR_PROCEDURES = true; break; - case BuildFlag_InsertSemicolon: { - gb_printf_err("-insert-semicolon flag is not required any more\n"); - bad_flags = true; - break; - } case BuildFlag_StrictStyle: { if (build_context.strict_style_init_only) { gb_printf_err("-strict-style and -strict-style-init-only cannot be used together\n"); -- cgit v1.2.3 From 581eebb197cc9905248d59a084a68bd2e64c9830 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 13 Jun 2023 13:14:59 +0100 Subject: Fix a race condition when produced anonymous procedure literals with `-use-separate-modules` --- src/checker.hpp | 3 ++ src/llvm_backend.cpp | 53 +------------------------ src/llvm_backend.hpp | 4 +- src/llvm_backend_const.cpp | 1 + src/llvm_backend_general.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 99 insertions(+), 55 deletions(-) (limited to 'src/llvm_backend.cpp') diff --git a/src/checker.hpp b/src/checker.hpp index 1a95e2772..b06d0a8f9 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -199,6 +199,9 @@ struct DeclInfo { BlockingMutex type_and_value_mutex; Array labels; + + // NOTE(bill): this is to prevent a race condition since these procedure literals can be created anywhere at any time + struct lbModule *code_gen_module; }; // ProcInfo stores the information needed for checking a procedure diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 525ac8a9d..730610ad9 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -772,56 +772,6 @@ gb_internal lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) { return {p->value, p->type}; } - -gb_internal lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, Ast *expr, lbProcedure *parent) { - mutex_lock(&m->gen->anonymous_proc_lits_mutex); - defer (mutex_unlock(&m->gen->anonymous_proc_lits_mutex)); - - lbProcedure **found = map_get(&m->gen->anonymous_proc_lits, expr); - if (found) { - return lb_find_procedure_value_from_entity(m, (*found)->entity); - } - - ast_node(pl, ProcLit, expr); - - // NOTE(bill): Generate a new name - // parent$count - isize name_len = prefix_name.len + 6 + 11; - char *name_text = gb_alloc_array(permanent_allocator(), char, name_len); - static std::atomic name_id; - name_len = gb_snprintf(name_text, name_len, "%.*s$anon-%d", LIT(prefix_name), 1+name_id.fetch_add(1)); - String name = make_string((u8 *)name_text, name_len-1); - - Type *type = type_of_expr(expr); - - Token token = {}; - token.pos = ast_token(expr).pos; - token.kind = Token_Ident; - token.string = name; - Entity *e = alloc_entity_procedure(nullptr, token, type, pl->tags); - e->file = expr->file(); - e->decl_info = pl->decl; - e->code_gen_module = m; - e->flags |= EntityFlag_ProcBodyChecked; - lbProcedure *p = lb_create_procedure(m, e); - - lbValue value = {}; - value.value = p->value; - value.type = p->type; - - array_add(&m->procedures_to_generate, p); - if (parent != nullptr) { - array_add(&parent->children, p); - } else { - string_map_set(&m->members, name, value); - } - - map_set(&m->gen->anonymous_proc_lits, expr, p); - - return value; -} - - gb_internal lbValue lb_gen_map_cell_info_ptr(lbModule *m, Type *type) { lbAddr *found = map_get(&m->map_cell_info_map, type); if (found) { @@ -1513,7 +1463,7 @@ gb_internal WORKER_TASK_PROC(lb_generate_missing_procedures_to_check_worker_proc lbModule *m = cast(lbModule *)data; for (isize i = 0; i < m->missing_procedures_to_check.count; i++) { lbProcedure *p = m->missing_procedures_to_check[i]; - debugf("Generate missing procedure: %.*s\n", LIT(p->name)); + debugf("Generate missing procedure: %.*s module %p\n", LIT(p->name), m); lb_generate_procedure(m, p); } return 0; @@ -1577,7 +1527,6 @@ gb_internal void lb_llvm_module_passes(lbGenerator *gen, bool do_threading) { thread_pool_wait(); } - gb_internal String lb_filepath_ll_for_module(lbModule *m) { String path = concatenate3_strings(permanent_allocator(), build_context.build_paths[BuildPath_Output].basename, diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 6e785d492..3aa13b488 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -201,7 +201,7 @@ struct lbGenerator { PtrMap modules_through_ctx; lbModule default_module; - BlockingMutex anonymous_proc_lits_mutex; + RecursiveMutex anonymous_proc_lits_mutex; PtrMap anonymous_proc_lits; BlockingMutex foreign_mutex; @@ -545,6 +545,8 @@ gb_internal gb_inline i64 lb_max_zero_init_size(void) { gb_internal LLVMTypeRef OdinLLVMGetArrayElementType(LLVMTypeRef type); gb_internal LLVMTypeRef OdinLLVMGetVectorElementType(LLVMTypeRef type); +gb_internal String lb_filepath_ll_for_module(lbModule *m); + #define LB_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime" #define LB_CLEANUP_RUNTIME_PROC_NAME "__$cleanup_runtime" #define LB_STARTUP_TYPE_INFO_PROC_NAME "__$startup_type_info" diff --git a/src/llvm_backend_const.cpp b/src/llvm_backend_const.cpp index c9d2f5b26..efe1e4d45 100644 --- a/src/llvm_backend_const.cpp +++ b/src/llvm_backend_const.cpp @@ -473,6 +473,7 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bo if (value.kind == ExactValue_Procedure) { lbValue res = {}; Ast *expr = unparen_expr(value.value_procedure); + GB_ASSERT(expr != nullptr); if (expr->kind == Ast_ProcLit) { res = lb_generate_anonymous_proc_lit(m, str_lit("_proclit"), expr); } else { diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 176e53042..9333f13a4 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -334,10 +334,35 @@ gb_internal bool lb_is_instr_terminating(LLVMValueRef instr) { return false; } +gb_internal lbModule *lb_module_of_expr(lbGenerator *gen, Ast *expr) { + GB_ASSERT(expr != nullptr); + lbModule **found = nullptr; + AstFile *file = expr->file(); + if (file) { + found = map_get(&gen->modules, cast(void *)file); + if (found) { + return *found; + } + + if (file->pkg) { + found = map_get(&gen->modules, cast(void *)file->pkg); + if (found) { + return *found; + } + } + } + + return &gen->default_module; +} gb_internal lbModule *lb_module_of_entity(lbGenerator *gen, Entity *e) { GB_ASSERT(e != nullptr); lbModule **found = nullptr; + if (e->kind == Entity_Procedure && + e->decl_info && + e->decl_info->code_gen_module) { + return e->decl_info->code_gen_module; + } if (e->file) { found = map_get(&gen->modules, cast(void *)e->file); if (found) { @@ -2661,9 +2686,12 @@ gb_internal lbValue lb_find_ident(lbProcedure *p, lbModule *m, Entity *e, Ast *e gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) { + lbGenerator *gen = m->gen; + GB_ASSERT(is_type_proc(e->type)); e = strip_entity_wrapping(e); GB_ASSERT(e != nullptr); + GB_ASSERT(e->kind == Entity_Procedure); lbValue *found = nullptr; rw_mutex_shared_lock(&m->values_mutex); @@ -2677,20 +2705,24 @@ gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) lbModule *other_module = m; if (USE_SEPARATE_MODULES) { - other_module = lb_module_of_entity(m->gen, e); + other_module = lb_module_of_entity(gen, e); } if (other_module == m) { - debugf("Missing Procedure (lb_find_procedure_value_from_entity): %.*s\n", LIT(e->token.string)); + debugf("Missing Procedure (lb_find_procedure_value_from_entity): %.*s module %p\n", LIT(e->token.string), m); } ignore_body = other_module != m; lbProcedure *missing_proc = lb_create_procedure(m, e, ignore_body); if (ignore_body) { + mutex_lock(&gen->anonymous_proc_lits_mutex); + defer (mutex_unlock(&gen->anonymous_proc_lits_mutex)); + GB_ASSERT(other_module != nullptr); rw_mutex_shared_lock(&other_module->values_mutex); auto *found = map_get(&other_module->values, e); rw_mutex_shared_unlock(&other_module->values_mutex); if (found == nullptr) { + // THIS IS THE RACE CONDITION lbProcedure *missing_proc_in_other_module = lb_create_procedure(other_module, e, false); array_add(&other_module->missing_procedures_to_check, missing_proc_in_other_module); } @@ -2707,6 +2739,63 @@ gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) } + +gb_internal lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, Ast *expr, lbProcedure *parent) { + lbGenerator *gen = m->gen; + + mutex_lock(&gen->anonymous_proc_lits_mutex); + defer (mutex_unlock(&gen->anonymous_proc_lits_mutex)); + + TokenPos pos = ast_token(expr).pos; + lbProcedure **found = map_get(&gen->anonymous_proc_lits, expr); + if (found) { + return lb_find_procedure_value_from_entity(m, (*found)->entity); + } + + ast_node(pl, ProcLit, expr); + + // NOTE(bill): Generate a new name + // parent$count + isize name_len = prefix_name.len + 6 + 11; + char *name_text = gb_alloc_array(permanent_allocator(), char, name_len); + static std::atomic name_id; + name_len = gb_snprintf(name_text, name_len, "%.*s$anon-%d", LIT(prefix_name), 1+name_id.fetch_add(1)); + String name = make_string((u8 *)name_text, name_len-1); + + Type *type = type_of_expr(expr); + + GB_ASSERT(pl->decl->entity == nullptr); + Token token = {}; + token.pos = ast_token(expr).pos; + token.kind = Token_Ident; + token.string = name; + Entity *e = alloc_entity_procedure(nullptr, token, type, pl->tags); + e->file = expr->file(); + + // NOTE(bill): this is to prevent a race condition since these procedure literals can be created anywhere at any time + pl->decl->code_gen_module = m; + e->decl_info = pl->decl; + pl->decl->entity = e; + e->flags |= EntityFlag_ProcBodyChecked; + + lbProcedure *p = lb_create_procedure(m, e); + GB_ASSERT(e->code_gen_module == m); + + lbValue value = {}; + value.value = p->value; + value.type = p->type; + + map_set(&gen->anonymous_proc_lits, expr, p); + array_add(&m->procedures_to_generate, p); + if (parent != nullptr) { + array_add(&parent->children, p); + } else { + string_map_set(&m->members, name, value); + } + return value; +} + + gb_internal lbAddr lb_add_global_generated(lbModule *m, Type *type, lbValue value, Entity **entity_) { GB_ASSERT(type != nullptr); type = default_type(type); -- cgit v1.2.3 From 8b8310711e4924678e605278fca1f8dbf517b903 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 26 Jun 2023 17:36:27 +0100 Subject: Fix #2594 zero sized union code generation --- src/llvm_backend.cpp | 4 +++- src/llvm_backend_general.cpp | 1 + src/llvm_backend_type.cpp | 5 ++--- 3 files changed, 6 insertions(+), 4 deletions(-) (limited to 'src/llvm_backend.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 730610ad9..12abe7b16 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -218,7 +218,9 @@ gb_internal lbValue lb_equal_proc_for_type(lbModule *m, Type *type) { LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 0, false)); } else if (type->kind == Type_Union) { - if (is_type_union_maybe_pointer(type)) { + if (type_size_of(type) == 0) { + LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_bool), 1, false)); + } else if (is_type_union_maybe_pointer(type)) { Type *v = type->Union.variants[0]; Type *pv = alloc_type_pointer(v); diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 9333f13a4..017eeca2e 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -1323,6 +1323,7 @@ gb_internal lbValue lb_emit_union_tag_value(lbProcedure *p, lbValue u) { gb_internal void lb_emit_store_union_variant_tag(lbProcedure *p, lbValue parent, Type *variant_type) { Type *t = type_deref(parent.type); + GB_ASSERT(is_type_union(t)); if (is_type_union_maybe_pointer(t) || type_size_of(t) == 0) { // No tag needed! diff --git a/src/llvm_backend_type.cpp b/src/llvm_backend_type.cpp index a54fde748..4716733cc 100644 --- a/src/llvm_backend_type.cpp +++ b/src/llvm_backend_type.cpp @@ -654,10 +654,9 @@ gb_internal void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup lbValue count = lb_const_int(m, t_int, variant_count); vals[0] = llvm_const_slice(m, memory_types, count); - i64 tag_size = union_tag_size(t); - i64 tag_offset = align_formula(t->Union.variant_block_size, tag_size); - + i64 tag_size = union_tag_size(t); if (tag_size > 0) { + i64 tag_offset = align_formula(t->Union.variant_block_size, tag_size); vals[1] = lb_const_int(m, t_uintptr, tag_offset).value; vals[2] = lb_type_info(m, union_tag_type(t)).value; } else { -- cgit v1.2.3 From 7cdf37eaf6ec9b4f2f4adf527eea55c7bc716472 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Mon, 3 Jul 2023 01:22:36 +0200 Subject: exit with code 1 on `odin test` failure --- src/llvm_backend.cpp | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) (limited to 'src/llvm_backend.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 12abe7b16..6a4046d8d 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1825,25 +1825,45 @@ gb_internal lbProcedure *lb_create_main_procedure(lbModule *m, lbProcedure *star TEMPORARY_ALLOCATOR_GUARD(); auto args = array_make(temporary_allocator(), 1); args[0] = lb_addr_load(p, all_tests_slice); - lb_emit_call(p, runner, args); + lbValue result = lb_emit_call(p, runner, args); + + lbBlock *block_success = lb_create_block(p, "success"); + lbBlock *block_failure = lb_create_block(p, "failure"); + + lbValue result_success = lb_emit_comp(p, Token_CmpEq, result, lb_const_bool(m, t_bool, true)); + lb_emit_if(p, result_success, block_success, block_failure); + + lbValue exit_runner = lb_find_package_value(m, str_lit("os"), str_lit("exit")); + + lb_start_block(p, block_success); + { + auto exit_args = array_make(temporary_allocator(), 1); + exit_args[0] = lb_const_int(m, t_int, 0); + lb_emit_call(p, exit_runner, exit_args); + } + + lb_start_block(p, block_failure); + { + auto exit_args = array_make(temporary_allocator(), 1); + exit_args[0] = lb_const_int(m, t_int, 1); + lb_emit_call(p, exit_runner, exit_args); + } } else { if (m->info->entry_point != nullptr) { lbValue entry_point = lb_find_procedure_value_from_entity(m, m->info->entry_point); lb_emit_call(p, entry_point, {}, ProcInlining_no_inline); } - } - - - if (call_cleanup) { - lbValue cleanup_runtime_value = {cleanup_runtime->value, cleanup_runtime->type}; - lb_emit_call(p, cleanup_runtime_value, {}, ProcInlining_none); - } + if (call_cleanup) { + lbValue cleanup_runtime_value = {cleanup_runtime->value, cleanup_runtime->type}; + lb_emit_call(p, cleanup_runtime_value, {}, ProcInlining_none); + } - if (is_dll_main) { - LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_i32), 1, false)); - } else { - LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_i32), 0, false)); + if (is_dll_main) { + LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_i32), 1, false)); + } else { + LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_i32), 0, false)); + } } lb_end_procedure_body(p); -- cgit v1.2.3 From 52700d6a840d5635cf8ebc3f9ebf516f9c47f615 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Tue, 11 Jul 2023 19:30:44 +0200 Subject: use lb_emit_select instead of lb_emit_if for exit check --- src/llvm_backend.cpp | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) (limited to 'src/llvm_backend.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 6a4046d8d..90716dde0 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1827,30 +1827,12 @@ gb_internal lbProcedure *lb_create_main_procedure(lbModule *m, lbProcedure *star args[0] = lb_addr_load(p, all_tests_slice); lbValue result = lb_emit_call(p, runner, args); - lbBlock *block_success = lb_create_block(p, "success"); - lbBlock *block_failure = lb_create_block(p, "failure"); - - lbValue result_success = lb_emit_comp(p, Token_CmpEq, result, lb_const_bool(m, t_bool, true)); - lb_emit_if(p, result_success, block_success, block_failure); - lbValue exit_runner = lb_find_package_value(m, str_lit("os"), str_lit("exit")); - - lb_start_block(p, block_success); - { - auto exit_args = array_make(temporary_allocator(), 1); - exit_args[0] = lb_const_int(m, t_int, 0); - lb_emit_call(p, exit_runner, exit_args); - } - - lb_start_block(p, block_failure); - { - auto exit_args = array_make(temporary_allocator(), 1); - exit_args[0] = lb_const_int(m, t_int, 1); - lb_emit_call(p, exit_runner, exit_args); - } + auto exit_args = array_make(temporary_allocator(), 1); + exit_args[0] = lb_emit_select(p, result, lb_const_int(m, t_int, 0), lb_const_int(m, t_int, 1)); + lb_emit_call(p, exit_runner, exit_args, ProcInlining_none); } else { - if (m->info->entry_point != nullptr) { - lbValue entry_point = lb_find_procedure_value_from_entity(m, m->info->entry_point); + if (m->info->entry_point != nullptr) { lbValue entry_point = lb_find_procedure_value_from_entity(m, m->info->entry_point); lb_emit_call(p, entry_point, {}, ProcInlining_no_inline); } -- cgit v1.2.3 From 3cce9721257f921b81bd757c453fecb8b5308227 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Tue, 11 Jul 2023 19:33:14 +0200 Subject: fix indentation --- src/llvm_backend.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/llvm_backend.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 90716dde0..938c9b2ac 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1832,19 +1832,20 @@ gb_internal lbProcedure *lb_create_main_procedure(lbModule *m, lbProcedure *star exit_args[0] = lb_emit_select(p, result, lb_const_int(m, t_int, 0), lb_const_int(m, t_int, 1)); lb_emit_call(p, exit_runner, exit_args, ProcInlining_none); } else { - if (m->info->entry_point != nullptr) { lbValue entry_point = lb_find_procedure_value_from_entity(m, m->info->entry_point); + if (m->info->entry_point != nullptr) { + lbValue entry_point = lb_find_procedure_value_from_entity(m, m->info->entry_point); lb_emit_call(p, entry_point, {}, ProcInlining_no_inline); } if (call_cleanup) { - lbValue cleanup_runtime_value = {cleanup_runtime->value, cleanup_runtime->type}; - lb_emit_call(p, cleanup_runtime_value, {}, ProcInlining_none); + lbValue cleanup_runtime_value = {cleanup_runtime->value, cleanup_runtime->type}; + lb_emit_call(p, cleanup_runtime_value, {}, ProcInlining_none); } if (is_dll_main) { - LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_i32), 1, false)); + LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_i32), 1, false)); } else { - LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_i32), 0, false)); + LLVMBuildRet(p->builder, LLVMConstInt(lb_type(m, t_i32), 0, false)); } } -- cgit v1.2.3