From 34a048f7daaf93b16ae4121bf5238f9008f3465b Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 9 Dec 2022 11:29:28 +0000 Subject: Replace compiler for loops for the hash-table types to simplify code usage --- src/llvm_backend_general.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 777755794..3ca98845a 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -137,8 +137,8 @@ bool lb_init_generator(lbGenerator *gen, Checker *c) { ptr_set_init(&gen->foreign_libraries_set, heap_allocator(), 1024); if (USE_SEPARATE_MODULES) { - for_array(i, gen->info->packages.entries) { - AstPackage *pkg = gen->info->packages.entries[i].value; + for (auto const &entry : gen->info->packages) { + AstPackage *pkg = entry.value; auto m = gb_alloc_item(permanent_allocator(), lbModule); m->pkg = pkg; @@ -153,8 +153,8 @@ bool lb_init_generator(lbGenerator *gen, Checker *c) { lb_init_module(&gen->default_module, c); - for_array(i, gen->modules.entries) { - lbModule *m = gen->modules.entries[i].value; + for (auto const &entry : gen->modules) { + lbModule *m = entry.value; LLVMContextRef ctx = LLVMGetModuleContext(m->mod); map_set(&gen->modules_through_ctx, ctx, m); } -- cgit v1.2.3 From 6cdec65ca1fd13a4d86d83a6715cbaaff7115cd7 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 18 Dec 2022 22:32:05 +0000 Subject: `gb_internal` LLVM backend --- src/llvm_abi.cpp | 154 ++++++++++++------------ src/llvm_backend.cpp | 70 +++++------ src/llvm_backend.hpp | 274 +++++++++++++++++++++---------------------- src/llvm_backend_const.cpp | 60 +++++----- src/llvm_backend_debug.cpp | 44 +++---- src/llvm_backend_expr.cpp | 80 ++++++------- src/llvm_backend_general.cpp | 182 ++++++++++++++-------------- src/llvm_backend_opt.cpp | 32 ++--- src/llvm_backend_proc.cpp | 55 +++++---- src/llvm_backend_stmt.cpp | 90 +++++++------- src/llvm_backend_type.cpp | 20 ++-- src/llvm_backend_utility.cpp | 194 +++++++++++++++--------------- 12 files changed, 627 insertions(+), 628 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_abi.cpp b/src/llvm_abi.cpp index 166fcb3ee..7b82fddeb 100644 --- a/src/llvm_abi.cpp +++ b/src/llvm_abi.cpp @@ -18,21 +18,21 @@ struct lbArgType { }; -i64 lb_sizeof(LLVMTypeRef type); -i64 lb_alignof(LLVMTypeRef type); +gb_internal i64 lb_sizeof(LLVMTypeRef type); +gb_internal i64 lb_alignof(LLVMTypeRef type); -lbArgType lb_arg_type_direct(LLVMTypeRef type, LLVMTypeRef cast_type, LLVMTypeRef pad_type, LLVMAttributeRef attr) { +gb_internal lbArgType lb_arg_type_direct(LLVMTypeRef type, LLVMTypeRef cast_type, LLVMTypeRef pad_type, LLVMAttributeRef attr) { return lbArgType{lbArg_Direct, type, cast_type, pad_type, attr, nullptr, 0, false}; } -lbArgType lb_arg_type_direct(LLVMTypeRef type) { +gb_internal lbArgType lb_arg_type_direct(LLVMTypeRef type) { return lb_arg_type_direct(type, nullptr, nullptr, nullptr); } -lbArgType lb_arg_type_indirect(LLVMTypeRef type, LLVMAttributeRef attr) { +gb_internal lbArgType lb_arg_type_indirect(LLVMTypeRef type, LLVMAttributeRef attr) { return lbArgType{lbArg_Indirect, type, nullptr, nullptr, attr, nullptr, 0, false}; } -lbArgType lb_arg_type_indirect_byval(LLVMContextRef c, LLVMTypeRef type) { +gb_internal lbArgType lb_arg_type_indirect_byval(LLVMContextRef c, LLVMTypeRef type) { i64 alignment = lb_alignof(type); alignment = gb_max(alignment, 8); @@ -41,7 +41,7 @@ lbArgType lb_arg_type_indirect_byval(LLVMContextRef c, LLVMTypeRef type) { return lbArgType{lbArg_Indirect, type, nullptr, nullptr, byval_attr, align_attr, alignment, true}; } -lbArgType lb_arg_type_ignore(LLVMTypeRef type) { +gb_internal lbArgType lb_arg_type_ignore(LLVMTypeRef type) { return lbArgType{lbArg_Ignore, type, nullptr, nullptr, nullptr, nullptr, 0, false}; } @@ -55,24 +55,24 @@ struct lbFunctionType { isize original_arg_count; }; -gbAllocator lb_function_type_args_allocator(void) { +gb_internal gbAllocator lb_function_type_args_allocator(void) { return heap_allocator(); } -i64 llvm_align_formula(i64 off, i64 a) { +gb_internal gb_inline i64 llvm_align_formula(i64 off, i64 a) { return (off + a - 1) / a * a; } -bool lb_is_type_kind(LLVMTypeRef type, LLVMTypeKind kind) { +gb_internal bool lb_is_type_kind(LLVMTypeRef type, LLVMTypeKind kind) { if (type == nullptr) { return false; } return LLVMGetTypeKind(type) == kind; } -LLVMTypeRef lb_function_type_to_llvm_raw(lbFunctionType *ft, bool is_var_arg) { +gb_internal LLVMTypeRef lb_function_type_to_llvm_raw(lbFunctionType *ft, bool is_var_arg) { unsigned arg_count = cast(unsigned)ft->args.count; unsigned offset = 0; @@ -130,7 +130,7 @@ LLVMTypeRef lb_function_type_to_llvm_raw(lbFunctionType *ft, bool is_var_arg) { // } -void lb_add_function_type_attributes(LLVMValueRef fn, lbFunctionType *ft, ProcCallingConvention calling_convention) { +gb_internal void lb_add_function_type_attributes(LLVMValueRef fn, lbFunctionType *ft, ProcCallingConvention calling_convention) { if (ft == nullptr) { return; } @@ -201,7 +201,7 @@ void lb_add_function_type_attributes(LLVMValueRef fn, lbFunctionType *ft, ProcCa } -i64 lb_sizeof(LLVMTypeRef type) { +gb_internal i64 lb_sizeof(LLVMTypeRef type) { LLVMTypeKind kind = LLVMGetTypeKind(type); switch (kind) { case LLVMVoidTypeKind: @@ -267,7 +267,7 @@ i64 lb_sizeof(LLVMTypeRef type) { return 0; } -i64 lb_alignof(LLVMTypeRef type) { +gb_internal i64 lb_alignof(LLVMTypeRef type) { LLVMTypeKind kind = LLVMGetTypeKind(type); switch (kind) { case LLVMVoidTypeKind: @@ -333,7 +333,7 @@ typedef LB_ABI_INFO(lbAbiInfoType); typedef LB_ABI_COMPUTE_RETURN_TYPE(lbAbiComputeReturnType); -lbArgType lb_abi_modify_return_is_tuple(lbFunctionType *ft, LLVMContextRef c, LLVMTypeRef return_type, lbAbiComputeReturnType *compute_return_type) { +gb_internal lbArgType lb_abi_modify_return_is_tuple(lbFunctionType *ft, LLVMContextRef c, LLVMTypeRef return_type, lbAbiComputeReturnType *compute_return_type) { GB_ASSERT(return_type != nullptr); GB_ASSERT(compute_return_type != nullptr); @@ -370,10 +370,10 @@ lbArgType lb_abi_modify_return_is_tuple(lbFunctionType *ft, LLVMContextRef c, LL // NOTE(bill): I hate `namespace` in C++ but this is just because I don't want to prefix everything namespace lbAbi386 { - Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count); - LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type); + gb_internal Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count); + gb_internal LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type); - LB_ABI_INFO(abi_info) { + gb_internal LB_ABI_INFO(abi_info) { lbFunctionType *ft = gb_alloc_item(permanent_allocator(), lbFunctionType); ft->ctx = c; ft->args = compute_arg_types(c, arg_types, arg_count); @@ -382,7 +382,7 @@ namespace lbAbi386 { return ft; } - lbArgType non_struct(LLVMContextRef c, LLVMTypeRef type, bool is_return) { + gb_internal lbArgType non_struct(LLVMContextRef c, LLVMTypeRef type, bool is_return) { if (!is_return && lb_sizeof(type) > 8) { return lb_arg_type_indirect(type, nullptr); } @@ -409,7 +409,7 @@ namespace lbAbi386 { return lb_arg_type_direct(type, nullptr, nullptr, attr); } - Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count) { + gb_internal Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count) { auto args = array_make(lb_function_type_args_allocator(), arg_count); for (unsigned i = 0; i < arg_count; i++) { @@ -429,7 +429,7 @@ namespace lbAbi386 { return args; } - LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type) { + gb_internal LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type) { if (!return_is_defined) { return lb_arg_type_direct(LLVMVoidTypeInContext(c)); } else if (lb_is_type_kind(return_type, LLVMStructTypeKind) || lb_is_type_kind(return_type, LLVMArrayTypeKind)) { @@ -451,10 +451,10 @@ namespace lbAbi386 { }; namespace lbAbiAmd64Win64 { - Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count); - LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type); + gb_internal Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count); + gb_internal LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type); - LB_ABI_INFO(abi_info) { + gb_internal LB_ABI_INFO(abi_info) { lbFunctionType *ft = gb_alloc_item(permanent_allocator(), lbFunctionType); ft->ctx = c; ft->args = compute_arg_types(c, arg_types, arg_count); @@ -463,7 +463,7 @@ namespace lbAbiAmd64Win64 { return ft; } - Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count) { + gb_internal Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count) { auto args = array_make(lb_function_type_args_allocator(), arg_count); for (unsigned i = 0; i < arg_count; i++) { @@ -489,7 +489,7 @@ namespace lbAbiAmd64Win64 { return args; } - LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type) { + gb_internal LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type) { if (!return_is_defined) { return lb_arg_type_direct(LLVMVoidTypeInContext(c)); } else if (lb_is_type_kind(return_type, LLVMStructTypeKind) || lb_is_type_kind(return_type, LLVMArrayTypeKind)) { @@ -530,7 +530,7 @@ namespace lbAbiAmd64SysV { RegClass_Memory, }; - bool is_sse(RegClass reg_class) { + gb_internal bool is_sse(RegClass reg_class) { switch (reg_class) { case RegClass_SSEFs: case RegClass_SSEFv: @@ -546,7 +546,7 @@ namespace lbAbiAmd64SysV { return false; } - void all_mem(Array *cs) { + gb_internal void all_mem(Array *cs) { for_array(i, *cs) { (*cs)[i] = RegClass_Memory; } @@ -558,14 +558,14 @@ namespace lbAbiAmd64SysV { Amd64TypeAttribute_StructRect, }; - LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type); - void classify_with(LLVMTypeRef t, Array *cls, i64 ix, i64 off); - void fixup(LLVMTypeRef t, Array *cls); - lbArgType amd64_type(LLVMContextRef c, LLVMTypeRef type, Amd64TypeAttributeKind attribute_kind, ProcCallingConvention calling_convention); - Array classify(LLVMTypeRef t); - LLVMTypeRef llreg(LLVMContextRef c, Array const ®_classes); + gb_internal LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type); + gb_internal void classify_with(LLVMTypeRef t, Array *cls, i64 ix, i64 off); + gb_internal void fixup(LLVMTypeRef t, Array *cls); + gb_internal lbArgType amd64_type(LLVMContextRef c, LLVMTypeRef type, Amd64TypeAttributeKind attribute_kind, ProcCallingConvention calling_convention); + gb_internal Array classify(LLVMTypeRef t); + gb_internal LLVMTypeRef llreg(LLVMContextRef c, Array const ®_classes); - LB_ABI_INFO(abi_info) { + gb_internal LB_ABI_INFO(abi_info) { lbFunctionType *ft = gb_alloc_item(permanent_allocator(), lbFunctionType); ft->ctx = c; ft->calling_convention = calling_convention; @@ -584,7 +584,7 @@ namespace lbAbiAmd64SysV { return ft; } - bool is_mem_cls(Array const &cls, Amd64TypeAttributeKind attribute_kind) { + gb_internal bool is_mem_cls(Array const &cls, Amd64TypeAttributeKind attribute_kind) { if (attribute_kind == Amd64TypeAttribute_ByVal) { if (cls.count == 0) { return false; @@ -600,7 +600,7 @@ namespace lbAbiAmd64SysV { return false; } - bool is_register(LLVMTypeRef type) { + gb_internal bool is_register(LLVMTypeRef type) { LLVMTypeKind kind = LLVMGetTypeKind(type); i64 sz = lb_sizeof(type); if (sz == 0) { @@ -617,7 +617,7 @@ namespace lbAbiAmd64SysV { return false; } - bool is_llvm_type_slice_like(LLVMTypeRef type) { + gb_internal bool is_llvm_type_slice_like(LLVMTypeRef type) { if (!lb_is_type_kind(type, LLVMStructTypeKind)) { return false; } @@ -633,7 +633,7 @@ namespace lbAbiAmd64SysV { } - lbArgType amd64_type(LLVMContextRef c, LLVMTypeRef type, Amd64TypeAttributeKind attribute_kind, ProcCallingConvention calling_convention) { + gb_internal lbArgType amd64_type(LLVMContextRef c, LLVMTypeRef type, Amd64TypeAttributeKind attribute_kind, ProcCallingConvention calling_convention) { if (is_register(type)) { LLVMAttributeRef attribute = nullptr; if (type == LLVMInt1TypeInContext(c)) { @@ -668,7 +668,7 @@ namespace lbAbiAmd64SysV { } } - lbArgType non_struct(LLVMContextRef c, LLVMTypeRef type) { + gb_internal lbArgType non_struct(LLVMContextRef c, LLVMTypeRef type) { LLVMAttributeRef attr = nullptr; LLVMTypeRef i1 = LLVMInt1TypeInContext(c); if (type == i1) { @@ -677,7 +677,7 @@ namespace lbAbiAmd64SysV { return lb_arg_type_direct(type, nullptr, nullptr, attr); } - Array classify(LLVMTypeRef t) { + gb_internal Array classify(LLVMTypeRef t) { i64 sz = lb_sizeof(t); i64 words = (sz + 7)/8; auto reg_classes = array_make(heap_allocator(), cast(isize)words); @@ -690,7 +690,7 @@ namespace lbAbiAmd64SysV { return reg_classes; } - void unify(Array *cls, i64 i, RegClass const newv) { + gb_internal void unify(Array *cls, i64 i, RegClass const newv) { RegClass const oldv = (*cls)[cast(isize)i]; if (oldv == newv) { return; @@ -726,7 +726,7 @@ namespace lbAbiAmd64SysV { (*cls)[cast(isize)i] = to_write; } - void fixup(LLVMTypeRef t, Array *cls) { + gb_internal void fixup(LLVMTypeRef t, Array *cls) { i64 i = 0; i64 e = cls->count; if (e > 2 && (lb_is_type_kind(t, LLVMStructTypeKind) || @@ -773,7 +773,7 @@ namespace lbAbiAmd64SysV { } } - unsigned llvec_len(Array const ®_classes, isize offset) { + gb_internal unsigned llvec_len(Array const ®_classes, isize offset) { unsigned len = 1; for (isize i = offset; i < reg_classes.count; i++) { if (reg_classes[i] != RegClass_SSEUp) { @@ -785,7 +785,7 @@ namespace lbAbiAmd64SysV { } - LLVMTypeRef llreg(LLVMContextRef c, Array const ®_classes) { + gb_internal LLVMTypeRef llreg(LLVMContextRef c, Array const ®_classes) { auto types = array_make(heap_allocator(), 0, reg_classes.count); for (isize i = 0; i < reg_classes.count; /**/) { RegClass reg_class = reg_classes[i]; @@ -854,7 +854,7 @@ namespace lbAbiAmd64SysV { return LLVMStructTypeInContext(c, types.data, cast(unsigned)types.count, false); } - void classify_with(LLVMTypeRef t, Array *cls, i64 ix, i64 off) { + gb_internal void classify_with(LLVMTypeRef t, Array *cls, i64 ix, i64 off) { i64 t_align = lb_alignof(t); i64 t_size = lb_sizeof(t); @@ -955,7 +955,7 @@ namespace lbAbiAmd64SysV { } } - LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type) { + gb_internal LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type) { if (!return_is_defined) { return lb_arg_type_direct(LLVMVoidTypeInContext(c)); } else if (lb_is_type_kind(return_type, LLVMStructTypeKind)) { @@ -980,11 +980,11 @@ namespace lbAbiAmd64SysV { namespace lbAbiArm64 { - Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count); - LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type); - bool is_homogenous_aggregate(LLVMContextRef c, LLVMTypeRef type, LLVMTypeRef *base_type_, unsigned *member_count_); + gb_internal Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count); + gb_internal LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type); + gb_internal bool is_homogenous_aggregate(LLVMContextRef c, LLVMTypeRef type, LLVMTypeRef *base_type_, unsigned *member_count_); - LB_ABI_INFO(abi_info) { + gb_internal LB_ABI_INFO(abi_info) { lbFunctionType *ft = gb_alloc_item(permanent_allocator(), lbFunctionType); ft->ctx = c; ft->ret = compute_return_type(ft, c, return_type, return_is_defined, return_is_tuple); @@ -993,7 +993,7 @@ namespace lbAbiArm64 { return ft; } - bool is_register(LLVMTypeRef type) { + gb_internal bool is_register(LLVMTypeRef type) { LLVMTypeKind kind = LLVMGetTypeKind(type); switch (kind) { case LLVMIntegerTypeKind: @@ -1006,7 +1006,7 @@ namespace lbAbiArm64 { return false; } - lbArgType non_struct(LLVMContextRef c, LLVMTypeRef type) { + gb_internal lbArgType non_struct(LLVMContextRef c, LLVMTypeRef type) { LLVMAttributeRef attr = nullptr; LLVMTypeRef i1 = LLVMInt1TypeInContext(c); if (type == i1) { @@ -1015,7 +1015,7 @@ namespace lbAbiArm64 { return lb_arg_type_direct(type, nullptr, nullptr, attr); } - bool is_homogenous_array(LLVMContextRef c, LLVMTypeRef type, LLVMTypeRef *base_type_, unsigned *member_count_) { + gb_internal bool is_homogenous_array(LLVMContextRef c, LLVMTypeRef type, LLVMTypeRef *base_type_, unsigned *member_count_) { GB_ASSERT(lb_is_type_kind(type, LLVMArrayTypeKind)); unsigned len = LLVMGetArrayLength(type); if (len == 0) { @@ -1032,7 +1032,7 @@ namespace lbAbiArm64 { } return false; } - bool is_homogenous_struct(LLVMContextRef c, LLVMTypeRef type, LLVMTypeRef *base_type_, unsigned *member_count_) { + gb_internal bool is_homogenous_struct(LLVMContextRef c, LLVMTypeRef type, LLVMTypeRef *base_type_, unsigned *member_count_) { GB_ASSERT(lb_is_type_kind(type, LLVMStructTypeKind)); unsigned elem_count = LLVMCountStructElementTypes(type); if (elem_count == 0) { @@ -1075,7 +1075,7 @@ namespace lbAbiArm64 { } - bool is_homogenous_aggregate(LLVMContextRef c, LLVMTypeRef type, LLVMTypeRef *base_type_, unsigned *member_count_) { + gb_internal bool is_homogenous_aggregate(LLVMContextRef c, LLVMTypeRef type, LLVMTypeRef *base_type_, unsigned *member_count_) { LLVMTypeKind kind = LLVMGetTypeKind(type); switch (kind) { case LLVMFloatTypeKind: @@ -1091,11 +1091,11 @@ namespace lbAbiArm64 { return false; } - unsigned is_homogenous_aggregate_small_enough(LLVMTypeRef base_type, unsigned member_count) { + gb_internal unsigned is_homogenous_aggregate_small_enough(LLVMTypeRef base_type, unsigned member_count) { return (member_count <= 4); } - LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type) { + gb_internal LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type) { LLVMTypeRef homo_base_type = nullptr; unsigned homo_member_count = 0; @@ -1142,7 +1142,7 @@ namespace lbAbiArm64 { } } - Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count) { + gb_internal Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count) { auto args = array_make(lb_function_type_args_allocator(), arg_count); for (unsigned i = 0; i < arg_count; i++) { @@ -1188,12 +1188,12 @@ namespace lbAbiWasm { The approach taken optimizes for passing things in multiple registers/arguments if possible rather than by pointer. */ - Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count); - LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type); + gb_internal Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count); + gb_internal LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type); enum {MAX_DIRECT_STRUCT_SIZE = 32}; - LB_ABI_INFO(abi_info) { + gb_internal LB_ABI_INFO(abi_info) { lbFunctionType *ft = gb_alloc_item(permanent_allocator(), lbFunctionType); ft->ctx = c; ft->args = compute_arg_types(c, arg_types, arg_count); @@ -1202,7 +1202,7 @@ namespace lbAbiWasm { return ft; } - lbArgType non_struct(LLVMContextRef c, LLVMTypeRef type, bool is_return) { + gb_internal lbArgType non_struct(LLVMContextRef c, LLVMTypeRef type, bool is_return) { if (!is_return && type == LLVMIntTypeInContext(c, 128)) { LLVMTypeRef cast_type = LLVMVectorType(LLVMInt64TypeInContext(c), 2); return lb_arg_type_direct(type, cast_type, nullptr, nullptr); @@ -1220,7 +1220,7 @@ namespace lbAbiWasm { return lb_arg_type_direct(type, nullptr, nullptr, attr); } - bool is_basic_register_type(LLVMTypeRef type) { + gb_internal bool is_basic_register_type(LLVMTypeRef type) { switch (LLVMGetTypeKind(type)) { case LLVMHalfTypeKind: case LLVMFloatTypeKind: @@ -1233,7 +1233,7 @@ namespace lbAbiWasm { return false; } - bool type_can_be_direct(LLVMTypeRef type) { + gb_internal bool type_can_be_direct(LLVMTypeRef type) { LLVMTypeKind kind = LLVMGetTypeKind(type); i64 sz = lb_sizeof(type); if (sz == 0) { @@ -1259,7 +1259,7 @@ namespace lbAbiWasm { return false; } - lbArgType is_struct(LLVMContextRef c, LLVMTypeRef type) { + gb_internal lbArgType is_struct(LLVMContextRef c, LLVMTypeRef type) { LLVMTypeKind kind = LLVMGetTypeKind(type); GB_ASSERT(kind == LLVMArrayTypeKind || kind == LLVMStructTypeKind); @@ -1274,7 +1274,7 @@ namespace lbAbiWasm { } - Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count) { + gb_internal Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count) { auto args = array_make(lb_function_type_args_allocator(), arg_count); for (unsigned i = 0; i < arg_count; i++) { @@ -1289,7 +1289,7 @@ namespace lbAbiWasm { return args; } - LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type) { + gb_internal LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type) { if (!return_is_defined) { return lb_arg_type_direct(LLVMVoidTypeInContext(c)); } else if (lb_is_type_kind(return_type, LLVMStructTypeKind) || lb_is_type_kind(return_type, LLVMArrayTypeKind)) { @@ -1315,10 +1315,10 @@ namespace lbAbiWasm { } namespace lbAbiArm32 { - Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count, ProcCallingConvention calling_convention); - lbArgType compute_return_type(LLVMContextRef c, LLVMTypeRef return_type, bool return_is_defined); + gb_internal Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count, ProcCallingConvention calling_convention); + gb_internal lbArgType compute_return_type(LLVMContextRef c, LLVMTypeRef return_type, bool return_is_defined); - LB_ABI_INFO(abi_info) { + gb_internal LB_ABI_INFO(abi_info) { lbFunctionType *ft = gb_alloc_item(permanent_allocator(), lbFunctionType); ft->ctx = c; ft->args = compute_arg_types(c, arg_types, arg_count, calling_convention); @@ -1327,7 +1327,7 @@ namespace lbAbiArm32 { return ft; } - bool is_register(LLVMTypeRef type, bool is_return) { + gb_internal bool is_register(LLVMTypeRef type, bool is_return) { LLVMTypeKind kind = LLVMGetTypeKind(type); switch (kind) { case LLVMHalfTypeKind: @@ -1346,7 +1346,7 @@ namespace lbAbiArm32 { return false; } - lbArgType non_struct(LLVMContextRef c, LLVMTypeRef type, bool is_return) { + gb_internal lbArgType non_struct(LLVMContextRef c, LLVMTypeRef type, bool is_return) { LLVMAttributeRef attr = nullptr; LLVMTypeRef i1 = LLVMInt1TypeInContext(c); if (type == i1) { @@ -1355,7 +1355,7 @@ namespace lbAbiArm32 { return lb_arg_type_direct(type, nullptr, nullptr, attr); } - Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count, ProcCallingConvention calling_convention) { + gb_internal Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count, ProcCallingConvention calling_convention) { auto args = array_make(lb_function_type_args_allocator(), arg_count); for (unsigned i = 0; i < arg_count; i++) { @@ -1380,7 +1380,7 @@ namespace lbAbiArm32 { return args; } - lbArgType compute_return_type(LLVMContextRef c, LLVMTypeRef return_type, bool return_is_defined) { + gb_internal lbArgType compute_return_type(LLVMContextRef c, LLVMTypeRef return_type, bool return_is_defined) { if (!return_is_defined) { return lb_arg_type_direct(LLVMVoidTypeInContext(c)); } else if (!is_register(return_type, true)) { @@ -1397,7 +1397,7 @@ namespace lbAbiArm32 { }; -LB_ABI_INFO(lb_get_abi_info_internal) { +gb_internal LB_ABI_INFO(lb_get_abi_info_internal) { switch (calling_convention) { case ProcCC_None: case ProcCC_InlineAsm: @@ -1451,7 +1451,7 @@ LB_ABI_INFO(lb_get_abi_info_internal) { } -LB_ABI_INFO(lb_get_abi_info) { +gb_internal LB_ABI_INFO(lb_get_abi_info) { lbFunctionType *ft = lb_get_abi_info_internal( c, arg_types, arg_count, diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 0a44b0939..ca4b3b683 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -22,7 +22,7 @@ #include "llvm_backend_proc.cpp" -void lb_add_foreign_library_path(lbModule *m, Entity *e) { +gb_internal void lb_add_foreign_library_path(lbModule *m, Entity *e) { if (e == nullptr) { return; } @@ -36,7 +36,7 @@ void lb_add_foreign_library_path(lbModule *m, Entity *e) { mutex_unlock(&m->gen->foreign_mutex); } -GB_COMPARE_PROC(foreign_library_cmp) { +gb_internal GB_COMPARE_PROC(foreign_library_cmp) { int cmp = 0; Entity *x = *(Entity **)a; Entity *y = *(Entity **)b; @@ -78,7 +78,7 @@ GB_COMPARE_PROC(foreign_library_cmp) { return i32_cmp(x->token.pos.offset, y->token.pos.offset); } -void lb_set_entity_from_other_modules_linkage_correctly(lbModule *other_module, Entity *e, String const &name) { +gb_internal void lb_set_entity_from_other_modules_linkage_correctly(lbModule *other_module, Entity *e, String const &name) { if (other_module == nullptr) { return; } @@ -95,7 +95,7 @@ void lb_set_entity_from_other_modules_linkage_correctly(lbModule *other_module, } } -void lb_emit_init_context(lbProcedure *p, lbAddr addr) { +gb_internal void lb_emit_init_context(lbProcedure *p, lbAddr addr) { GB_ASSERT(addr.kind == lbAddr_Context); GB_ASSERT(addr.ctx.sel.index.count == 0); @@ -104,7 +104,7 @@ void lb_emit_init_context(lbProcedure *p, lbAddr addr) { lb_emit_runtime_call(p, "__init_context", args); } -lbContextData *lb_push_context_onto_stack_from_implicit_parameter(lbProcedure *p) { +gb_internal lbContextData *lb_push_context_onto_stack_from_implicit_parameter(lbProcedure *p) { Type *pt = base_type(p->type); GB_ASSERT(pt->kind == Type_Proc); GB_ASSERT(pt->Proc.calling_convention == ProcCC_Odin); @@ -131,7 +131,7 @@ lbContextData *lb_push_context_onto_stack_from_implicit_parameter(lbProcedure *p return cd; } -lbContextData *lb_push_context_onto_stack(lbProcedure *p, lbAddr ctx) { +gb_internal lbContextData *lb_push_context_onto_stack(lbProcedure *p, lbAddr ctx) { ctx.kind = lbAddr_Context; lbContextData *cd = array_add_and_get(&p->context_stack); cd->ctx = ctx; @@ -140,7 +140,7 @@ lbContextData *lb_push_context_onto_stack(lbProcedure *p, lbAddr ctx) { } -lbValue lb_equal_proc_for_type(lbModule *m, Type *type) { +gb_internal lbValue lb_equal_proc_for_type(lbModule *m, Type *type) { type = base_type(type); GB_ASSERT(is_type_comparable(type)); @@ -279,7 +279,7 @@ lbValue lb_equal_proc_for_type(lbModule *m, Type *type) { return {compare_proc->value, compare_proc->type}; } -lbValue lb_simple_compare_hash(lbProcedure *p, Type *type, lbValue data, lbValue seed) { +gb_internal lbValue lb_simple_compare_hash(lbProcedure *p, Type *type, lbValue data, lbValue seed) { GB_ASSERT_MSG(is_type_simple_compare(type), "%s", type_to_string(type)); auto args = array_make(permanent_allocator(), 3); @@ -289,11 +289,11 @@ lbValue lb_simple_compare_hash(lbProcedure *p, Type *type, lbValue data, lbValue return lb_emit_runtime_call(p, "default_hasher", args); } -void lb_add_callsite_force_inline(lbProcedure *p, lbValue ret_value) { +gb_internal void lb_add_callsite_force_inline(lbProcedure *p, lbValue ret_value) { LLVMAddCallSiteAttribute(ret_value.value, LLVMAttributeIndex_FunctionIndex, lb_create_enum_attribute(p->module->ctx, "alwaysinline")); } -lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) { +gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) { type = core_type(type); GB_ASSERT_MSG(is_type_valid_for_keys(type), "%s", type_to_string(type)); @@ -458,7 +458,7 @@ lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) { } -lbValue lb_map_get_proc_for_type(lbModule *m, Type *type) { +gb_internal lbValue lb_map_get_proc_for_type(lbModule *m, Type *type) { GB_ASSERT(build_context.use_static_map_calls); type = base_type(type); GB_ASSERT(type->kind == Type_Map); @@ -605,13 +605,13 @@ lbValue lb_map_get_proc_for_type(lbModule *m, Type *type) { return {p->value, p->type}; } -void lb_debug_print(lbProcedure *p, String const &str) { +gb_internal void lb_debug_print(lbProcedure *p, String const &str) { auto args = array_make(heap_allocator(), 1); args[0] = lb_const_string(p->module, str); lb_emit_runtime_call(p, "print_string", args); } -lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) { +gb_internal lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) { GB_ASSERT(build_context.use_static_map_calls); type = base_type(type); GB_ASSERT(type->kind == Type_Map); @@ -731,7 +731,7 @@ lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) { } -lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, Ast *expr, lbProcedure *parent) { +gb_internal lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, Ast *expr, lbProcedure *parent) { lbProcedure **found = map_get(&m->gen->anonymous_proc_lits, expr); if (found) { return lb_find_procedure_value_from_entity(m, (*found)->entity); @@ -778,7 +778,7 @@ lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, A } -lbValue lb_gen_map_cell_info_ptr(lbModule *m, Type *type) { +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) { return found->addr; @@ -802,7 +802,7 @@ lbValue lb_gen_map_cell_info_ptr(lbModule *m, Type *type) { return addr.addr; } -lbValue lb_gen_map_info_ptr(lbModule *m, Type *map_type) { +gb_internal lbValue lb_gen_map_info_ptr(lbModule *m, Type *map_type) { map_type = base_type(map_type); GB_ASSERT(map_type->kind == Type_Map); @@ -833,7 +833,7 @@ lbValue lb_gen_map_info_ptr(lbModule *m, Type *map_type) { return addr.addr; } -lbValue lb_const_hash(lbModule *m, lbValue key, Type *key_type) { +gb_internal lbValue lb_const_hash(lbModule *m, lbValue key, Type *key_type) { if (true) { return {}; } @@ -880,7 +880,7 @@ lbValue lb_const_hash(lbModule *m, lbValue key, Type *key_type) { return hashed_key; } -lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_type, lbValue *key_ptr_) { +gb_internal lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_type, lbValue *key_ptr_) { lbValue key_ptr = lb_address_from_load_or_generate_local(p, key); key_ptr = lb_emit_conv(p, key_ptr, t_rawptr); @@ -899,7 +899,7 @@ lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_type, lbValue return hashed_key; } -lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key) { +gb_internal lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key) { Type *map_type = base_type(type_deref(map_ptr.type)); GB_ASSERT(map_type->kind == Type_Map); @@ -928,8 +928,8 @@ lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, return lb_emit_conv(p, ptr, alloc_type_pointer(map_type->Map.value)); } -void lb_internal_dynamic_map_set(lbProcedure *p, lbValue const &map_ptr, Type *map_type, - lbValue const &map_key, lbValue const &map_value, Ast *node) { +gb_internal void lb_internal_dynamic_map_set(lbProcedure *p, lbValue const &map_ptr, Type *map_type, + lbValue const &map_key, lbValue const &map_value, Ast *node) { map_type = base_type(map_type); GB_ASSERT(map_type->kind == Type_Map); @@ -962,7 +962,7 @@ void lb_internal_dynamic_map_set(lbProcedure *p, lbValue const &map_ptr, Type *m } } -lbValue lb_dynamic_map_reserve(lbProcedure *p, lbValue const &map_ptr, isize const capacity, TokenPos const &pos) { +gb_internal lbValue lb_dynamic_map_reserve(lbProcedure *p, lbValue const &map_ptr, isize const capacity, TokenPos const &pos) { GB_ASSERT(!build_context.no_dynamic_literals); String proc_name = {}; @@ -986,7 +986,7 @@ struct lbGlobalVariable { bool is_initialized; }; -lbProcedure *lb_create_startup_type_info(lbModule *m) { +gb_internal lbProcedure *lb_create_startup_type_info(lbModule *m) { if (build_context.disallow_rtti) { return nullptr; } @@ -1020,7 +1020,7 @@ lbProcedure *lb_create_startup_type_info(lbModule *m) { return p; } -lbProcedure *lb_create_objc_names(lbModule *main_module) { +gb_internal lbProcedure *lb_create_objc_names(lbModule *main_module) { if (build_context.metrics.os != TargetOs_darwin) { return nullptr; } @@ -1031,7 +1031,7 @@ lbProcedure *lb_create_objc_names(lbModule *main_module) { return p; } -void lb_finalize_objc_names(lbProcedure *p) { +gb_internal void lb_finalize_objc_names(lbProcedure *p) { if (p == nullptr) { return; } @@ -1066,7 +1066,7 @@ void lb_finalize_objc_names(lbProcedure *p) { } -lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProcedure *startup_type_info, lbProcedure *objc_names, Array &global_variables) { // Startup Runtime +gb_internal lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProcedure *startup_type_info, lbProcedure *objc_names, Array &global_variables) { // Startup Runtime LLVMPassManagerRef default_function_pass_manager = LLVMCreateFunctionPassManagerForModule(main_module->mod); lb_populate_function_pass_manager(main_module, default_function_pass_manager, false, build_context.optimization_level); LLVMFinalizeFunctionPassManager(default_function_pass_manager); @@ -1181,7 +1181,7 @@ lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProcedure *start } -lbProcedure *lb_create_main_procedure(lbModule *m, lbProcedure *startup_runtime) { +gb_internal lbProcedure *lb_create_main_procedure(lbModule *m, lbProcedure *startup_runtime) { LLVMPassManagerRef default_function_pass_manager = LLVMCreateFunctionPassManagerForModule(m->mod); lb_populate_function_pass_manager(m, default_function_pass_manager, false, build_context.optimization_level); LLVMFinalizeFunctionPassManager(default_function_pass_manager); @@ -1331,7 +1331,7 @@ lbProcedure *lb_create_main_procedure(lbModule *m, lbProcedure *startup_runtime) return p; } -String lb_filepath_ll_for_module(lbModule *m) { +gb_internal String lb_filepath_ll_for_module(lbModule *m) { String path = concatenate3_strings(permanent_allocator(), build_context.build_paths[BuildPath_Output].basename, STR_LIT("/"), @@ -1347,7 +1347,7 @@ String lb_filepath_ll_for_module(lbModule *m) { return path; } -String lb_filepath_obj_for_module(lbModule *m) { +gb_internal String lb_filepath_obj_for_module(lbModule *m) { String path = concatenate3_strings(permanent_allocator(), build_context.build_paths[BuildPath_Output].basename, STR_LIT("/"), @@ -1397,7 +1397,7 @@ String lb_filepath_obj_for_module(lbModule *m) { } -bool lb_is_module_empty(lbModule *m) { +gb_internal bool lb_is_module_empty(lbModule *m) { if (LLVMGetFirstFunction(m->mod) == nullptr && LLVMGetFirstGlobal(m->mod) == nullptr) { return true; @@ -1426,7 +1426,7 @@ struct lbLLVMEmitWorker { lbModule *m; }; -WORKER_TASK_PROC(lb_llvm_emit_worker_proc) { +gb_internal WORKER_TASK_PROC(lb_llvm_emit_worker_proc) { GB_ASSERT(MULTITHREAD_OBJECT_GENERATION); char *llvm_error = nullptr; @@ -1441,7 +1441,7 @@ WORKER_TASK_PROC(lb_llvm_emit_worker_proc) { return 0; } -WORKER_TASK_PROC(lb_llvm_function_pass_worker_proc) { +gb_internal WORKER_TASK_PROC(lb_llvm_function_pass_worker_proc) { GB_ASSERT(MULTITHREAD_OBJECT_GENERATION); auto m = cast(lbModule *)data; @@ -1526,7 +1526,7 @@ struct lbLLVMModulePassWorkerData { LLVMTargetMachineRef target_machine; }; -WORKER_TASK_PROC(lb_llvm_module_pass_worker_proc) { +gb_internal WORKER_TASK_PROC(lb_llvm_module_pass_worker_proc) { auto wd = cast(lbLLVMModulePassWorkerData *)data; LLVMPassManagerRef module_pass_manager = LLVMCreatePassManager(); lb_populate_module_pass_manager(wd->target_machine, module_pass_manager, build_context.optimization_level); @@ -1535,7 +1535,7 @@ WORKER_TASK_PROC(lb_llvm_module_pass_worker_proc) { } -void lb_generate_procedure(lbModule *m, lbProcedure *p) { +gb_internal void lb_generate_procedure(lbModule *m, lbProcedure *p) { if (p->is_done) { return; } @@ -1575,7 +1575,7 @@ void lb_generate_procedure(lbModule *m, lbProcedure *p) { } -void lb_generate_code(lbGenerator *gen) { +gb_internal void lb_generate_code(lbGenerator *gen) { TIME_SECTION("LLVM Initializtion"); isize thread_count = gb_max(build_context.thread_count, 1); diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index a85056579..9f7caa3bb 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -322,197 +322,197 @@ struct lbProcedure { #define LLVMBuildPtrDiff2(Builder__, Ty__, LHS__, RHS__, Name__) LLVMBuildPtrDiff(Builder__, LHS__, RHS__, Name__) #endif -bool lb_init_generator(lbGenerator *gen, Checker *c); +gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c); -String lb_mangle_name(lbModule *m, Entity *e); -String lb_get_entity_name(lbModule *m, Entity *e, String name = {}); +gb_internal String lb_mangle_name(lbModule *m, Entity *e); +gb_internal String lb_get_entity_name(lbModule *m, Entity *e, String name = {}); -LLVMAttributeRef lb_create_enum_attribute(LLVMContextRef ctx, char const *name, u64 value=0); -LLVMAttributeRef lb_create_enum_attribute_with_type(LLVMContextRef ctx, char const *name, LLVMTypeRef type); -void lb_add_proc_attribute_at_index(lbProcedure *p, isize index, char const *name, u64 value); -void lb_add_proc_attribute_at_index(lbProcedure *p, isize index, char const *name); -lbProcedure *lb_create_procedure(lbModule *module, Entity *entity, bool ignore_body=false); -void lb_end_procedure(lbProcedure *p); +gb_internal LLVMAttributeRef lb_create_enum_attribute(LLVMContextRef ctx, char const *name, u64 value=0); +gb_internal LLVMAttributeRef lb_create_enum_attribute_with_type(LLVMContextRef ctx, char const *name, LLVMTypeRef type); +gb_internal void lb_add_proc_attribute_at_index(lbProcedure *p, isize index, char const *name, u64 value); +gb_internal void lb_add_proc_attribute_at_index(lbProcedure *p, isize index, char const *name); +gb_internal lbProcedure *lb_create_procedure(lbModule *module, Entity *entity, bool ignore_body=false); +gb_internal void lb_end_procedure(lbProcedure *p); -LLVMTypeRef lb_type(lbModule *m, Type *type); -LLVMTypeRef llvm_get_element_type(LLVMTypeRef type); +gb_internal LLVMTypeRef lb_type(lbModule *m, Type *type); +gb_internal LLVMTypeRef llvm_get_element_type(LLVMTypeRef type); -lbBlock *lb_create_block(lbProcedure *p, char const *name, bool append=false); +gb_internal lbBlock *lb_create_block(lbProcedure *p, char const *name, bool append=false); -lbValue lb_const_nil(lbModule *m, Type *type); -lbValue lb_const_undef(lbModule *m, Type *type); -lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_local=true); -lbValue lb_const_bool(lbModule *m, Type *type, bool value); -lbValue lb_const_int(lbModule *m, Type *type, u64 value); +gb_internal lbValue lb_const_nil(lbModule *m, Type *type); +gb_internal lbValue lb_const_undef(lbModule *m, Type *type); +gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_local=true); +gb_internal lbValue lb_const_bool(lbModule *m, Type *type, bool value); +gb_internal lbValue lb_const_int(lbModule *m, Type *type, u64 value); -lbAddr lb_addr(lbValue addr); -Type *lb_addr_type(lbAddr const &addr); -LLVMTypeRef llvm_addr_type(lbModule *module, lbValue addr_val); -void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value); -lbValue lb_addr_load(lbProcedure *p, lbAddr const &addr); -lbValue lb_emit_load(lbProcedure *p, lbValue v); -void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value); +gb_internal lbAddr lb_addr(lbValue addr); +gb_internal Type *lb_addr_type(lbAddr const &addr); +gb_internal LLVMTypeRef llvm_addr_type(lbModule *module, lbValue addr_val); +gb_internal void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value); +gb_internal lbValue lb_addr_load(lbProcedure *p, lbAddr const &addr); +gb_internal lbValue lb_emit_load(lbProcedure *p, lbValue v); +gb_internal void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value); -void lb_build_stmt(lbProcedure *p, Ast *stmt); -lbValue lb_build_expr(lbProcedure *p, Ast *expr); -lbAddr lb_build_addr(lbProcedure *p, Ast *expr); -void lb_build_stmt_list(lbProcedure *p, Array const &stmts); +gb_internal void lb_build_stmt(lbProcedure *p, Ast *stmt); +gb_internal lbValue lb_build_expr(lbProcedure *p, Ast *expr); +gb_internal lbAddr lb_build_addr(lbProcedure *p, Ast *expr); +gb_internal void lb_build_stmt_list(lbProcedure *p, Array const &stmts); -lbValue lb_emit_epi(lbProcedure *p, lbValue const &value, isize index); -lbValue lb_emit_epi(lbModule *m, lbValue const &value, isize index); -lbValue lb_emit_array_epi(lbModule *m, lbValue s, isize index); -lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index); -lbValue lb_emit_struct_ev(lbProcedure *p, lbValue s, i32 index); -lbValue lb_emit_tuple_ev(lbProcedure *p, lbValue value, i32 index); -lbValue lb_emit_array_epi(lbProcedure *p, lbValue value, isize index); -lbValue lb_emit_array_ep(lbProcedure *p, lbValue s, lbValue index); -lbValue lb_emit_deep_field_gep(lbProcedure *p, lbValue e, Selection sel); -lbValue lb_emit_deep_field_ev(lbProcedure *p, lbValue e, Selection sel); +gb_internal lbValue lb_emit_epi(lbProcedure *p, lbValue const &value, isize index); +gb_internal lbValue lb_emit_epi(lbModule *m, lbValue const &value, isize index); +gb_internal lbValue lb_emit_array_epi(lbModule *m, lbValue s, isize index); +gb_internal lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index); +gb_internal lbValue lb_emit_struct_ev(lbProcedure *p, lbValue s, i32 index); +gb_internal lbValue lb_emit_tuple_ev(lbProcedure *p, lbValue value, i32 index); +gb_internal lbValue lb_emit_array_epi(lbProcedure *p, lbValue value, isize index); +gb_internal lbValue lb_emit_array_ep(lbProcedure *p, lbValue s, lbValue index); +gb_internal lbValue lb_emit_deep_field_gep(lbProcedure *p, lbValue e, Selection sel); +gb_internal lbValue lb_emit_deep_field_ev(lbProcedure *p, lbValue e, Selection sel); -lbValue lb_emit_matrix_ep(lbProcedure *p, lbValue s, lbValue row, lbValue column); -lbValue lb_emit_matrix_epi(lbProcedure *p, lbValue s, isize row, isize column); -lbValue lb_emit_matrix_ev(lbProcedure *p, lbValue s, isize row, isize column); +gb_internal lbValue lb_emit_matrix_ep(lbProcedure *p, lbValue s, lbValue row, lbValue column); +gb_internal lbValue lb_emit_matrix_epi(lbProcedure *p, lbValue s, isize row, isize column); +gb_internal lbValue lb_emit_matrix_ev(lbProcedure *p, lbValue s, isize row, isize column); -lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type); -lbValue lb_emit_byte_swap(lbProcedure *p, lbValue value, Type *end_type); -void lb_emit_defer_stmts(lbProcedure *p, lbDeferExitKind kind, lbBlock *block); -lbValue lb_emit_transmute(lbProcedure *p, lbValue value, Type *t); -lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue right); -lbValue lb_emit_call(lbProcedure *p, lbValue value, Array const &args, ProcInlining inlining = ProcInlining_none); -lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t); -lbValue lb_emit_comp_against_nil(lbProcedure *p, TokenKind op_kind, lbValue x); +gb_internal lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type); +gb_internal lbValue lb_emit_byte_swap(lbProcedure *p, lbValue value, Type *end_type); +gb_internal void lb_emit_defer_stmts(lbProcedure *p, lbDeferExitKind kind, lbBlock *block); +gb_internal lbValue lb_emit_transmute(lbProcedure *p, lbValue value, Type *t); +gb_internal lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue right); +gb_internal lbValue lb_emit_call(lbProcedure *p, lbValue value, Array const &args, ProcInlining inlining = ProcInlining_none); +gb_internal lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t); +gb_internal lbValue lb_emit_comp_against_nil(lbProcedure *p, TokenKind op_kind, lbValue x); -void lb_emit_jump(lbProcedure *p, lbBlock *target_block); -void lb_emit_if(lbProcedure *p, lbValue cond, lbBlock *true_block, lbBlock *false_block); -void lb_start_block(lbProcedure *p, lbBlock *b); +gb_internal void lb_emit_jump(lbProcedure *p, lbBlock *target_block); +gb_internal void lb_emit_if(lbProcedure *p, lbValue cond, lbBlock *true_block, lbBlock *false_block); +gb_internal void lb_start_block(lbProcedure *p, lbBlock *b); -lbValue lb_build_call_expr(lbProcedure *p, Ast *expr); +gb_internal lbValue lb_build_call_expr(lbProcedure *p, Ast *expr); -lbAddr lb_find_or_generate_context_ptr(lbProcedure *p); -lbContextData *lb_push_context_onto_stack(lbProcedure *p, lbAddr ctx); -lbContextData *lb_push_context_onto_stack_from_implicit_parameter(lbProcedure *p); +gb_internal lbAddr lb_find_or_generate_context_ptr(lbProcedure *p); +gb_internal lbContextData *lb_push_context_onto_stack(lbProcedure *p, lbAddr ctx); +gb_internal lbContextData *lb_push_context_onto_stack_from_implicit_parameter(lbProcedure *p); -lbAddr lb_add_global_generated(lbModule *m, Type *type, lbValue value={}, Entity **entity_=nullptr); -lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e=nullptr, bool zero_init=true, bool force_no_init=false); +gb_internal lbAddr lb_add_global_generated(lbModule *m, Type *type, lbValue value={}, Entity **entity_=nullptr); +gb_internal lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e=nullptr, bool zero_init=true, bool force_no_init=false); -void lb_add_foreign_library_path(lbModule *m, Entity *e); +gb_internal void lb_add_foreign_library_path(lbModule *m, Entity *e); -lbValue lb_typeid(lbModule *m, Type *type); +gb_internal lbValue lb_typeid(lbModule *m, Type *type); -lbValue lb_address_from_load_or_generate_local(lbProcedure *p, lbValue value); -lbValue lb_address_from_load(lbProcedure *p, lbValue value); -void lb_add_defer_node(lbProcedure *p, isize scope_index, Ast *stmt); -lbAddr lb_add_local_generated(lbProcedure *p, Type *type, bool zero_init); +gb_internal lbValue lb_address_from_load_or_generate_local(lbProcedure *p, lbValue value); +gb_internal lbValue lb_address_from_load(lbProcedure *p, lbValue value); +gb_internal void lb_add_defer_node(lbProcedure *p, isize scope_index, Ast *stmt); +gb_internal lbAddr lb_add_local_generated(lbProcedure *p, Type *type, bool zero_init); -lbValue lb_emit_runtime_call(lbProcedure *p, char const *c_name, Array const &args); +gb_internal lbValue lb_emit_runtime_call(lbProcedure *p, char const *c_name, Array const &args); -lbValue lb_emit_ptr_offset(lbProcedure *p, lbValue ptr, lbValue index); -lbValue lb_string_elem(lbProcedure *p, lbValue string); -lbValue lb_string_len(lbProcedure *p, lbValue string); -lbValue lb_cstring_len(lbProcedure *p, lbValue value); -lbValue lb_array_elem(lbProcedure *p, lbValue array_ptr); -lbValue lb_slice_elem(lbProcedure *p, lbValue slice); -lbValue lb_slice_len(lbProcedure *p, lbValue slice); -lbValue lb_dynamic_array_elem(lbProcedure *p, lbValue da); -lbValue lb_dynamic_array_len(lbProcedure *p, lbValue da); -lbValue lb_dynamic_array_cap(lbProcedure *p, lbValue da); -lbValue lb_dynamic_array_allocator(lbProcedure *p, lbValue da); -lbValue lb_map_len(lbProcedure *p, lbValue value); -lbValue lb_map_cap(lbProcedure *p, lbValue value); -lbValue lb_soa_struct_len(lbProcedure *p, lbValue value); -void lb_emit_increment(lbProcedure *p, lbValue addr); -lbValue lb_emit_select(lbProcedure *p, lbValue cond, lbValue x, lbValue y); +gb_internal lbValue lb_emit_ptr_offset(lbProcedure *p, lbValue ptr, lbValue index); +gb_internal lbValue lb_string_elem(lbProcedure *p, lbValue string); +gb_internal lbValue lb_string_len(lbProcedure *p, lbValue string); +gb_internal lbValue lb_cstring_len(lbProcedure *p, lbValue value); +gb_internal lbValue lb_array_elem(lbProcedure *p, lbValue array_ptr); +gb_internal lbValue lb_slice_elem(lbProcedure *p, lbValue slice); +gb_internal lbValue lb_slice_len(lbProcedure *p, lbValue slice); +gb_internal lbValue lb_dynamic_array_elem(lbProcedure *p, lbValue da); +gb_internal lbValue lb_dynamic_array_len(lbProcedure *p, lbValue da); +gb_internal lbValue lb_dynamic_array_cap(lbProcedure *p, lbValue da); +gb_internal lbValue lb_dynamic_array_allocator(lbProcedure *p, lbValue da); +gb_internal lbValue lb_map_len(lbProcedure *p, lbValue value); +gb_internal lbValue lb_map_cap(lbProcedure *p, lbValue value); +gb_internal lbValue lb_soa_struct_len(lbProcedure *p, lbValue value); +gb_internal void lb_emit_increment(lbProcedure *p, lbValue addr); +gb_internal lbValue lb_emit_select(lbProcedure *p, lbValue cond, lbValue x, lbValue y); -lbValue lb_emit_mul_add(lbProcedure *p, lbValue a, lbValue b, lbValue c, Type *t); +gb_internal lbValue lb_emit_mul_add(lbProcedure *p, lbValue a, lbValue b, lbValue c, Type *t); -void lb_fill_slice(lbProcedure *p, lbAddr const &slice, lbValue base_elem, lbValue len); +gb_internal void lb_fill_slice(lbProcedure *p, lbAddr const &slice, lbValue base_elem, lbValue len); -lbValue lb_type_info(lbModule *m, Type *type); +gb_internal lbValue lb_type_info(lbModule *m, Type *type); -lbValue lb_find_or_add_entity_string(lbModule *m, String const &str); -lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, Ast *expr, lbProcedure *parent = nullptr); +gb_internal lbValue lb_find_or_add_entity_string(lbModule *m, String const &str); +gb_internal lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, Ast *expr, lbProcedure *parent = nullptr); -bool lb_is_const(lbValue value); -bool lb_is_const_or_global(lbValue value); -bool lb_is_const_nil(lbValue value); -String lb_get_const_string(lbModule *m, lbValue value); +gb_internal bool lb_is_const(lbValue value); +gb_internal bool lb_is_const_or_global(lbValue value); +gb_internal bool lb_is_const_nil(lbValue value); +gb_internal String lb_get_const_string(lbModule *m, lbValue value); -lbValue lb_generate_local_array(lbProcedure *p, Type *elem_type, i64 count, bool zero_init=true); -lbValue lb_generate_global_array(lbModule *m, Type *elem_type, i64 count, String prefix, i64 id); -lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_type, lbValue *key_ptr_); -lbValue lb_gen_map_cell_info_ptr(lbModule *m, Type *type); -lbValue lb_gen_map_info_ptr(lbModule *m, Type *map_type); +gb_internal lbValue lb_generate_local_array(lbProcedure *p, Type *elem_type, i64 count, bool zero_init=true); +gb_internal lbValue lb_generate_global_array(lbModule *m, Type *elem_type, i64 count, String prefix, i64 id); +gb_internal lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_type, lbValue *key_ptr_); +gb_internal lbValue lb_gen_map_cell_info_ptr(lbModule *m, Type *type); +gb_internal lbValue lb_gen_map_info_ptr(lbModule *m, Type *map_type); -lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key); -void lb_internal_dynamic_map_set(lbProcedure *p, lbValue const &map_ptr, Type *map_type, lbValue const &map_key, lbValue const &map_value, Ast *node); -lbValue lb_dynamic_map_reserve(lbProcedure *p, lbValue const &map_ptr, isize const capacity, TokenPos const &pos); +gb_internal lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key); +gb_internal void lb_internal_dynamic_map_set(lbProcedure *p, lbValue const &map_ptr, Type *map_type, lbValue const &map_key, lbValue const &map_value, Ast *node); +gb_internal lbValue lb_dynamic_map_reserve(lbProcedure *p, lbValue const &map_ptr, isize const capacity, TokenPos const &pos); -lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e); -lbValue lb_find_value_from_entity(lbModule *m, Entity *e); +gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e); +gb_internal lbValue lb_find_value_from_entity(lbModule *m, Entity *e); -void lb_store_type_case_implicit(lbProcedure *p, Ast *clause, lbValue value); -lbAddr lb_store_range_stmt_val(lbProcedure *p, Ast *stmt_val, lbValue value); -lbValue lb_emit_source_code_location_const(lbProcedure *p, String const &procedure, TokenPos const &pos); +gb_internal void lb_store_type_case_implicit(lbProcedure *p, Ast *clause, lbValue value); +gb_internal lbAddr lb_store_range_stmt_val(lbProcedure *p, Ast *stmt_val, lbValue value); +gb_internal lbValue lb_emit_source_code_location_const(lbProcedure *p, String const &procedure, TokenPos const &pos); -lbValue lb_handle_param_value(lbProcedure *p, Type *parameter_type, ParameterValue const ¶m_value, TokenPos const &pos); +gb_internal lbValue lb_handle_param_value(lbProcedure *p, Type *parameter_type, ParameterValue const ¶m_value, TokenPos const &pos); -lbValue lb_equal_proc_for_type(lbModule *m, Type *type); -lbValue lb_hasher_proc_for_type(lbModule *m, Type *type); -lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t); +gb_internal lbValue lb_equal_proc_for_type(lbModule *m, Type *type); +gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type); +gb_internal lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t); -LLVMMetadataRef lb_debug_type(lbModule *m, Type *type); +gb_internal LLVMMetadataRef lb_debug_type(lbModule *m, Type *type); -lbValue lb_emit_count_ones(lbProcedure *p, lbValue x, Type *type); -lbValue lb_emit_count_zeros(lbProcedure *p, lbValue x, Type *type); -lbValue lb_emit_count_trailing_zeros(lbProcedure *p, lbValue x, Type *type); -lbValue lb_emit_count_leading_zeros(lbProcedure *p, lbValue x, Type *type); -lbValue lb_emit_reverse_bits(lbProcedure *p, lbValue x, Type *type); +gb_internal lbValue lb_emit_count_ones(lbProcedure *p, lbValue x, Type *type); +gb_internal lbValue lb_emit_count_zeros(lbProcedure *p, lbValue x, Type *type); +gb_internal lbValue lb_emit_count_trailing_zeros(lbProcedure *p, lbValue x, Type *type); +gb_internal lbValue lb_emit_count_leading_zeros(lbProcedure *p, lbValue x, Type *type); +gb_internal lbValue lb_emit_reverse_bits(lbProcedure *p, lbValue x, Type *type); -lbValue lb_emit_bit_set_card(lbProcedure *p, lbValue x); +gb_internal lbValue lb_emit_bit_set_card(lbProcedure *p, lbValue x); -void lb_mem_zero_addr(lbProcedure *p, LLVMValueRef ptr, Type *type); +gb_internal void lb_mem_zero_addr(lbProcedure *p, LLVMValueRef ptr, Type *type); -void lb_build_nested_proc(lbProcedure *p, AstProcLit *pd, Entity *e); -lbValue lb_emit_logical_binary_expr(lbProcedure *p, TokenKind op, Ast *left, Ast *right, Type *type); -lbValue lb_build_cond(lbProcedure *p, Ast *cond, lbBlock *true_block, lbBlock *false_block); +gb_internal void lb_build_nested_proc(lbProcedure *p, AstProcLit *pd, Entity *e); +gb_internal lbValue lb_emit_logical_binary_expr(lbProcedure *p, TokenKind op, Ast *left, Ast *right, Type *type); +gb_internal lbValue lb_build_cond(lbProcedure *p, Ast *cond, lbBlock *true_block, lbBlock *false_block); -LLVMValueRef llvm_const_named_struct(lbModule *m, Type *t, LLVMValueRef *values, isize value_count_); -LLVMValueRef llvm_const_named_struct_internal(LLVMTypeRef t, LLVMValueRef *values, isize value_count_); -void lb_set_entity_from_other_modules_linkage_correctly(lbModule *other_module, Entity *e, String const &name); +gb_internal LLVMValueRef llvm_const_named_struct(lbModule *m, Type *t, LLVMValueRef *values, isize value_count_); +gb_internal LLVMValueRef llvm_const_named_struct_internal(LLVMTypeRef t, LLVMValueRef *values, isize value_count_); +gb_internal void lb_set_entity_from_other_modules_linkage_correctly(lbModule *other_module, Entity *e, String const &name); -lbValue lb_expr_untyped_const_to_typed(lbModule *m, Ast *expr, Type *t); -bool lb_is_expr_untyped_const(Ast *expr); +gb_internal lbValue lb_expr_untyped_const_to_typed(lbModule *m, Ast *expr, Type *t); +gb_internal bool lb_is_expr_untyped_const(Ast *expr); -LLVMValueRef llvm_alloca(lbProcedure *p, LLVMTypeRef llvm_type, isize alignment, char const *name = ""); +gb_internal LLVMValueRef llvm_alloca(lbProcedure *p, LLVMTypeRef llvm_type, isize alignment, char const *name = ""); -void lb_mem_zero_ptr(lbProcedure *p, LLVMValueRef ptr, Type *type, unsigned alignment); +gb_internal void lb_mem_zero_ptr(lbProcedure *p, LLVMValueRef ptr, Type *type, unsigned alignment); -void lb_emit_init_context(lbProcedure *p, lbAddr addr); +gb_internal void lb_emit_init_context(lbProcedure *p, lbAddr addr); -lbStructFieldRemapping lb_get_struct_remapping(lbModule *m, Type *t); -LLVMTypeRef lb_type_padding_filler(lbModule *m, i64 padding, i64 padding_align); +gb_internal lbStructFieldRemapping lb_get_struct_remapping(lbModule *m, Type *t); +gb_internal LLVMTypeRef lb_type_padding_filler(lbModule *m, i64 padding, i64 padding_align); -LLVMValueRef llvm_basic_shuffle(lbProcedure *p, LLVMValueRef vector, LLVMValueRef mask); +gb_internal LLVMValueRef llvm_basic_shuffle(lbProcedure *p, LLVMValueRef vector, LLVMValueRef mask); -LLVMValueRef lb_call_intrinsic(lbProcedure *p, const char *name, LLVMValueRef* args, unsigned arg_count, LLVMTypeRef* types, unsigned type_count); -void lb_mem_copy_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile=false); -void lb_mem_copy_non_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile=false); -LLVMValueRef lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, LLVMValueRef len, unsigned alignment, bool is_volatile); +gb_internal LLVMValueRef lb_call_intrinsic(lbProcedure *p, const char *name, LLVMValueRef* args, unsigned arg_count, LLVMTypeRef* types, unsigned type_count); +gb_internal void lb_mem_copy_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile=false); +gb_internal void lb_mem_copy_non_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile=false); +gb_internal LLVMValueRef lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, LLVMValueRef len, unsigned alignment, bool is_volatile); -i64 lb_max_zero_init_size(void) { +gb_internal gb_inline i64 lb_max_zero_init_size(void) { return cast(i64)(4*build_context.word_size); } -LLVMTypeRef OdinLLVMGetArrayElementType(LLVMTypeRef type); -LLVMTypeRef OdinLLVMGetVectorElementType(LLVMTypeRef type); +gb_internal LLVMTypeRef OdinLLVMGetArrayElementType(LLVMTypeRef type); +gb_internal LLVMTypeRef OdinLLVMGetVectorElementType(LLVMTypeRef type); #define LB_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime" #define LB_STARTUP_TYPE_INFO_PROC_NAME "__$startup_type_info" @@ -632,7 +632,7 @@ enum : LLVMAttributeIndex { }; -char const *llvm_linkage_strings[] = { +gb_global char const *llvm_linkage_strings[] = { "external linkage", "available externally linkage", "link once any linkage", diff --git a/src/llvm_backend_const.cpp b/src/llvm_backend_const.cpp index dff5298c5..ee564bbf1 100644 --- a/src/llvm_backend_const.cpp +++ b/src/llvm_backend_const.cpp @@ -1,4 +1,4 @@ -bool lb_is_const(lbValue value) { +gb_internal bool lb_is_const(lbValue value) { LLVMValueRef v = value.value; if (is_type_untyped_nil(value.type) || is_type_untyped_undef(value.type)) { // TODO(bill): Is this correct behaviour? @@ -10,7 +10,7 @@ bool lb_is_const(lbValue value) { return false; } -bool lb_is_const_or_global(lbValue value) { +gb_internal bool lb_is_const_or_global(lbValue value) { if (lb_is_const(value)) { return true; } @@ -29,7 +29,7 @@ bool lb_is_const_or_global(lbValue value) { } -bool lb_is_elem_const(Ast *elem, Type *elem_type) { +gb_internal bool lb_is_elem_const(Ast *elem, Type *elem_type) { if (!elem_type_can_be_constant(elem_type)) { return false; } @@ -42,7 +42,7 @@ bool lb_is_elem_const(Ast *elem, Type *elem_type) { } -bool lb_is_const_nil(lbValue value) { +gb_internal bool lb_is_const_nil(lbValue value) { LLVMValueRef v = value.value; if (LLVMIsConstant(v)) { if (LLVMIsAConstantAggregateZero(v)) { @@ -55,7 +55,7 @@ bool lb_is_const_nil(lbValue value) { } -bool lb_is_expr_constant_zero(Ast *expr) { +gb_internal bool lb_is_expr_constant_zero(Ast *expr) { GB_ASSERT(expr != nullptr); auto v = exact_value_to_integer(expr->tav.value); if (v.kind == ExactValue_Integer) { @@ -64,7 +64,7 @@ bool lb_is_expr_constant_zero(Ast *expr) { return false; } -String lb_get_const_string(lbModule *m, lbValue value) { +gb_internal String lb_get_const_string(lbModule *m, lbValue value) { GB_ASSERT(lb_is_const(value)); GB_ASSERT(LLVMIsConstant(value.value)); @@ -92,7 +92,7 @@ String lb_get_const_string(lbModule *m, lbValue value) { } -LLVMValueRef llvm_const_cast(LLVMValueRef val, LLVMTypeRef dst) { +gb_internal LLVMValueRef llvm_const_cast(LLVMValueRef val, LLVMTypeRef dst) { LLVMTypeRef src = LLVMTypeOf(val); if (src == dst) { return val; @@ -116,7 +116,7 @@ LLVMValueRef llvm_const_cast(LLVMValueRef val, LLVMTypeRef dst) { } -lbValue lb_const_ptr_cast(lbModule *m, lbValue value, Type *t) { +gb_internal lbValue lb_const_ptr_cast(lbModule *m, lbValue value, Type *t) { GB_ASSERT(is_type_internally_pointer_like(value.type)); GB_ASSERT(is_type_internally_pointer_like(t)); GB_ASSERT(lb_is_const(value)); @@ -127,7 +127,7 @@ lbValue lb_const_ptr_cast(lbModule *m, lbValue value, Type *t) { return res; } -LLVMValueRef llvm_const_named_struct(lbModule *m, Type *t, LLVMValueRef *values, isize value_count_) { +gb_internal LLVMValueRef llvm_const_named_struct(lbModule *m, Type *t, LLVMValueRef *values, isize value_count_) { LLVMTypeRef struct_type = lb_type(m, t); GB_ASSERT(LLVMGetTypeKind(struct_type) == LLVMStructTypeKind); @@ -157,7 +157,7 @@ LLVMValueRef llvm_const_named_struct(lbModule *m, Type *t, LLVMValueRef *values, return llvm_const_named_struct_internal(struct_type, values_with_padding, values_with_padding_count); } -LLVMValueRef llvm_const_named_struct_internal(LLVMTypeRef t, LLVMValueRef *values, isize value_count_) { +gb_internal LLVMValueRef llvm_const_named_struct_internal(LLVMTypeRef t, LLVMValueRef *values, isize value_count_) { unsigned value_count = cast(unsigned)value_count_; unsigned elem_count = LLVMCountStructElementTypes(t); GB_ASSERT_MSG(value_count == elem_count, "%s %u %u", LLVMPrintTypeToString(t), value_count, elem_count); @@ -168,7 +168,7 @@ LLVMValueRef llvm_const_named_struct_internal(LLVMTypeRef t, LLVMValueRef *value return LLVMConstNamedStruct(t, values, value_count); } -LLVMValueRef llvm_const_array(LLVMTypeRef elem_type, LLVMValueRef *values, isize value_count_) { +gb_internal LLVMValueRef llvm_const_array(LLVMTypeRef elem_type, LLVMValueRef *values, isize value_count_) { unsigned value_count = cast(unsigned)value_count_; for (unsigned i = 0; i < value_count; i++) { values[i] = llvm_const_cast(values[i], elem_type); @@ -176,7 +176,7 @@ LLVMValueRef llvm_const_array(LLVMTypeRef elem_type, LLVMValueRef *values, isize return LLVMConstArray(elem_type, values, value_count); } -LLVMValueRef llvm_const_slice(lbModule *m, lbValue data, lbValue len) { +gb_internal LLVMValueRef llvm_const_slice(lbModule *m, lbValue data, lbValue len) { GB_ASSERT(is_type_pointer(data.type) || is_type_multi_pointer(data.type)); GB_ASSERT(are_types_identical(len.type, t_int)); LLVMValueRef vals[2] = { @@ -187,38 +187,38 @@ LLVMValueRef llvm_const_slice(lbModule *m, lbValue data, lbValue len) { } -lbValue lb_const_nil(lbModule *m, Type *type) { +gb_internal lbValue lb_const_nil(lbModule *m, Type *type) { LLVMValueRef v = LLVMConstNull(lb_type(m, type)); return lbValue{v, type}; } -lbValue lb_const_undef(lbModule *m, Type *type) { +gb_internal lbValue lb_const_undef(lbModule *m, Type *type) { LLVMValueRef v = LLVMGetUndef(lb_type(m, type)); return lbValue{v, type}; } -lbValue lb_const_int(lbModule *m, Type *type, u64 value) { +gb_internal lbValue lb_const_int(lbModule *m, Type *type, u64 value) { lbValue res = {}; res.value = LLVMConstInt(lb_type(m, type), cast(unsigned long long)value, !is_type_unsigned(type)); res.type = type; return res; } -lbValue lb_const_string(lbModule *m, String const &value) { +gb_internal lbValue lb_const_string(lbModule *m, String const &value) { return lb_const_value(m, t_string, exact_value_string(value)); } -lbValue lb_const_bool(lbModule *m, Type *type, bool value) { +gb_internal lbValue lb_const_bool(lbModule *m, Type *type, bool value) { lbValue res = {}; res.value = LLVMConstInt(lb_type(m, type), value, false); res.type = type; return res; } -LLVMValueRef lb_const_f16(lbModule *m, f32 f, Type *type=t_f16) { +gb_internal LLVMValueRef lb_const_f16(lbModule *m, f32 f, Type *type=t_f16) { GB_ASSERT(type_size_of(type) == 2); u16 u = f32_to_f16(f); @@ -229,7 +229,7 @@ LLVMValueRef lb_const_f16(lbModule *m, f32 f, Type *type=t_f16) { return LLVMConstBitCast(i, lb_type(m, type)); } -LLVMValueRef lb_const_f32(lbModule *m, f32 f, Type *type=t_f32) { +gb_internal LLVMValueRef lb_const_f32(lbModule *m, f32 f, Type *type=t_f32) { GB_ASSERT(type_size_of(type) == 4); u32 u = bit_cast(f); if (is_type_different_to_arch_endianness(type)) { @@ -241,7 +241,7 @@ LLVMValueRef lb_const_f32(lbModule *m, f32 f, Type *type=t_f32) { -bool lb_is_expr_untyped_const(Ast *expr) { +gb_internal bool lb_is_expr_untyped_const(Ast *expr) { auto const &tv = type_and_value_of_expr(expr); if (is_type_untyped(tv.type)) { return tv.value.kind != ExactValue_Invalid; @@ -250,13 +250,13 @@ bool lb_is_expr_untyped_const(Ast *expr) { } -lbValue lb_expr_untyped_const_to_typed(lbModule *m, Ast *expr, Type *t) { +gb_internal lbValue lb_expr_untyped_const_to_typed(lbModule *m, Ast *expr, Type *t) { GB_ASSERT(is_type_typed(t)); auto const &tv = type_and_value_of_expr(expr); return lb_const_value(m, t, tv.value); } -lbValue lb_emit_source_code_location_const(lbProcedure *p, String const &procedure, TokenPos const &pos) { +gb_internal lbValue lb_emit_source_code_location_const(lbProcedure *p, String const &procedure, TokenPos const &pos) { lbModule *m = p->module; LLVMValueRef fields[4] = {}; @@ -271,7 +271,7 @@ lbValue lb_emit_source_code_location_const(lbProcedure *p, String const &procedu return res; } -lbValue lb_emit_source_code_location_const(lbProcedure *p, Ast *node) { +gb_internal lbValue lb_emit_source_code_location_const(lbProcedure *p, Ast *node) { String proc_name = {}; if (p->entity) { proc_name = p->entity->token.string; @@ -284,7 +284,7 @@ lbValue lb_emit_source_code_location_const(lbProcedure *p, Ast *node) { } -lbValue lb_emit_source_code_location_as_global_ptr(lbProcedure *p, String const &procedure, TokenPos const &pos) { +gb_internal lbValue lb_emit_source_code_location_as_global_ptr(lbProcedure *p, String const &procedure, TokenPos const &pos) { lbValue loc = lb_emit_source_code_location_const(p, procedure, pos); lbAddr addr = lb_add_global_generated(p->module, loc.type, loc, nullptr); lb_make_global_private_const(addr); @@ -292,24 +292,24 @@ lbValue lb_emit_source_code_location_as_global_ptr(lbProcedure *p, String const } -lbValue lb_emit_source_code_location_as_global_ptr(lbProcedure *p, Ast *node) { +gb_internal lbValue lb_emit_source_code_location_as_global_ptr(lbProcedure *p, Ast *node) { lbValue loc = lb_emit_source_code_location_const(p, node); lbAddr addr = lb_add_global_generated(p->module, loc.type, loc, nullptr); lb_make_global_private_const(addr); return addr.addr; } -lbValue lb_emit_source_code_location_as_global(lbProcedure *p, String const &procedure, TokenPos const &pos) { +gb_internal lbValue lb_emit_source_code_location_as_global(lbProcedure *p, String const &procedure, TokenPos const &pos) { return lb_emit_load(p, lb_emit_source_code_location_as_global_ptr(p, procedure, pos)); } -lbValue lb_emit_source_code_location_as_global(lbProcedure *p, Ast *node) { +gb_internal lbValue lb_emit_source_code_location_as_global(lbProcedure *p, Ast *node) { return lb_emit_load(p, lb_emit_source_code_location_as_global_ptr(p, node)); } -LLVMValueRef lb_build_constant_array_values(lbModule *m, Type *type, Type *elem_type, isize count, LLVMValueRef *values, bool allow_local) { +gb_internal LLVMValueRef lb_build_constant_array_values(lbModule *m, Type *type, Type *elem_type, isize count, LLVMValueRef *values, bool allow_local) { bool is_local = allow_local && m->curr_procedure != nullptr; bool is_const = true; if (is_local) { @@ -341,7 +341,7 @@ LLVMValueRef lb_build_constant_array_values(lbModule *m, Type *type, Type *elem_ return llvm_const_array(lb_type(m, elem_type), values, cast(unsigned int)count); } -LLVMValueRef lb_big_int_to_llvm(lbModule *m, Type *original_type, BigInt const *a) { +gb_internal LLVMValueRef lb_big_int_to_llvm(lbModule *m, Type *original_type, BigInt const *a) { if (big_int_is_zero(a)) { return LLVMConstNull(lb_type(m, original_type)); } @@ -387,7 +387,7 @@ LLVMValueRef lb_big_int_to_llvm(lbModule *m, Type *original_type, BigInt const * } -lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_local) { +gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_local) { LLVMContextRef ctx = m->ctx; type = default_type(type); diff --git a/src/llvm_backend_debug.cpp b/src/llvm_backend_debug.cpp index 65cefcd39..849416579 100644 --- a/src/llvm_backend_debug.cpp +++ b/src/llvm_backend_debug.cpp @@ -1,4 +1,4 @@ -LLVMMetadataRef lb_get_llvm_metadata(lbModule *m, void *key) { +gb_internal LLVMMetadataRef lb_get_llvm_metadata(lbModule *m, void *key) { if (key == nullptr) { return nullptr; } @@ -8,20 +8,20 @@ LLVMMetadataRef lb_get_llvm_metadata(lbModule *m, void *key) { } return nullptr; } -void lb_set_llvm_metadata(lbModule *m, void *key, LLVMMetadataRef value) { +gb_internal void lb_set_llvm_metadata(lbModule *m, void *key, LLVMMetadataRef value) { if (key != nullptr) { map_set(&m->debug_values, key, value); } } -LLVMMetadataRef lb_get_llvm_file_metadata_from_node(lbModule *m, Ast *node) { +gb_internal LLVMMetadataRef lb_get_llvm_file_metadata_from_node(lbModule *m, Ast *node) { if (node == nullptr) { return nullptr; } return lb_get_llvm_metadata(m, node->file()); } -LLVMMetadataRef lb_get_current_debug_scope(lbProcedure *p) { +gb_internal LLVMMetadataRef lb_get_current_debug_scope(lbProcedure *p) { GB_ASSERT_MSG(p->debug_info != nullptr, "missing debug information for %.*s", LIT(p->name)); for (isize i = p->scope_stack.count-1; i >= 0; i--) { @@ -34,21 +34,21 @@ LLVMMetadataRef lb_get_current_debug_scope(lbProcedure *p) { return p->debug_info; } -LLVMMetadataRef lb_debug_location_from_token_pos(lbProcedure *p, TokenPos pos) { +gb_internal LLVMMetadataRef lb_debug_location_from_token_pos(lbProcedure *p, TokenPos pos) { LLVMMetadataRef scope = lb_get_current_debug_scope(p); GB_ASSERT_MSG(scope != nullptr, "%.*s", LIT(p->name)); return LLVMDIBuilderCreateDebugLocation(p->module->ctx, cast(unsigned)pos.line, cast(unsigned)pos.column, scope, nullptr); } -LLVMMetadataRef lb_debug_location_from_ast(lbProcedure *p, Ast *node) { +gb_internal LLVMMetadataRef lb_debug_location_from_ast(lbProcedure *p, Ast *node) { GB_ASSERT(node != nullptr); return lb_debug_location_from_token_pos(p, ast_token(node).pos); } -LLVMMetadataRef lb_debug_end_location_from_ast(lbProcedure *p, Ast *node) { +gb_internal LLVMMetadataRef lb_debug_end_location_from_ast(lbProcedure *p, Ast *node) { GB_ASSERT(node != nullptr); return lb_debug_location_from_token_pos(p, ast_end_token(node).pos); } -LLVMMetadataRef lb_debug_type_internal_proc(lbModule *m, Type *type) { +gb_internal LLVMMetadataRef lb_debug_type_internal_proc(lbModule *m, Type *type) { i64 size = type_size_of(type); // Check size gb_unused(size); @@ -93,7 +93,7 @@ LLVMMetadataRef lb_debug_type_internal_proc(lbModule *m, Type *type) { return LLVMDIBuilderCreateSubroutineType(m->debug_builder, file, parameters, parameter_count, flags); } -LLVMMetadataRef lb_debug_struct_field(lbModule *m, String const &name, Type *type, u64 offset_in_bits) { +gb_internal LLVMMetadataRef lb_debug_struct_field(lbModule *m, String const &name, Type *type, u64 offset_in_bits) { unsigned field_line = 1; LLVMDIFlags field_flags = LLVMDIFlagZero; @@ -107,7 +107,7 @@ LLVMMetadataRef lb_debug_struct_field(lbModule *m, String const &name, Type *typ field_flags, lb_debug_type(m, type) ); } -LLVMMetadataRef lb_debug_basic_struct(lbModule *m, String const &name, u64 size_in_bits, u32 align_in_bits, LLVMMetadataRef *elements, unsigned element_count) { +gb_internal LLVMMetadataRef lb_debug_basic_struct(lbModule *m, String const &name, u64 size_in_bits, u32 align_in_bits, LLVMMetadataRef *elements, unsigned element_count) { AstPackage *pkg = m->info->runtime_package; GB_ASSERT(pkg->files.count != 0); LLVMMetadataRef file = lb_get_llvm_metadata(m, pkg->files[0]); @@ -117,7 +117,7 @@ LLVMMetadataRef lb_debug_basic_struct(lbModule *m, String const &name, u64 size_ } -LLVMMetadataRef lb_debug_type_basic_type(lbModule *m, String const &name, u64 size_in_bits, LLVMDWARFTypeEncoding encoding, LLVMDIFlags flags = LLVMDIFlagZero) { +gb_internal LLVMMetadataRef lb_debug_type_basic_type(lbModule *m, String const &name, u64 size_in_bits, LLVMDWARFTypeEncoding encoding, LLVMDIFlags flags = LLVMDIFlagZero) { LLVMMetadataRef basic_type = LLVMDIBuilderCreateBasicType(m->debug_builder, cast(char const *)name.text, name.len, size_in_bits, encoding, flags); #if 1 LLVMMetadataRef final_decl = LLVMDIBuilderCreateTypedef(m->debug_builder, basic_type, cast(char const *)name.text, name.len, nullptr, 0, nullptr, cast(u32)size_in_bits); @@ -127,7 +127,7 @@ LLVMMetadataRef lb_debug_type_basic_type(lbModule *m, String const &name, u64 si #endif } -LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) { +gb_internal LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) { i64 size = type_size_of(type); // Check size gb_unused(size); @@ -474,7 +474,7 @@ LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) { return nullptr; } -LLVMMetadataRef lb_get_base_scope_metadata(lbModule *m, Scope *scope) { +gb_internal LLVMMetadataRef lb_get_base_scope_metadata(lbModule *m, Scope *scope) { LLVMMetadataRef found = nullptr; for (;;) { if (scope == nullptr) { @@ -496,7 +496,7 @@ LLVMMetadataRef lb_get_base_scope_metadata(lbModule *m, Scope *scope) { } } -LLVMMetadataRef lb_debug_type(lbModule *m, Type *type) { +gb_internal LLVMMetadataRef lb_debug_type(lbModule *m, Type *type) { GB_ASSERT(type != nullptr); LLVMMetadataRef found = lb_get_llvm_metadata(m, type); if (found != nullptr) { @@ -615,7 +615,7 @@ LLVMMetadataRef lb_debug_type(lbModule *m, Type *type) { return dt; } -void lb_debug_complete_types(lbModule *m) { +gb_internal void lb_debug_complete_types(lbModule *m) { /* unsigned const word_size = cast(unsigned)build_context.word_size; */ unsigned const word_bits = cast(unsigned)(8*build_context.word_size); @@ -962,7 +962,7 @@ void lb_debug_complete_types(lbModule *m) { -void lb_add_debug_local_variable(lbProcedure *p, LLVMValueRef ptr, Type *type, Token const &token) { +gb_internal void lb_add_debug_local_variable(lbProcedure *p, LLVMValueRef ptr, Type *type, Token const &token) { if (p->debug_info == nullptr) { return; } @@ -1024,7 +1024,7 @@ void lb_add_debug_local_variable(lbProcedure *p, LLVMValueRef ptr, Type *type, T LLVMDIBuilderInsertDeclareAtEnd(m->debug_builder, storage, var_info, llvm_expr, llvm_debug_loc, block); } -void lb_add_debug_param_variable(lbProcedure *p, LLVMValueRef ptr, Type *type, Token const &token, unsigned arg_number, lbBlock *block, lbArgKind arg_kind) { +gb_internal void lb_add_debug_param_variable(lbProcedure *p, LLVMValueRef ptr, Type *type, Token const &token, unsigned arg_number, lbBlock *block, lbArgKind arg_kind) { if (p->debug_info == nullptr) { return; } @@ -1097,7 +1097,7 @@ void lb_add_debug_param_variable(lbProcedure *p, LLVMValueRef ptr, Type *type, T } -void lb_add_debug_context_variable(lbProcedure *p, lbAddr const &ctx) { +gb_internal void lb_add_debug_context_variable(lbProcedure *p, lbAddr const &ctx) { if (!p->debug_info || !p->body) { return; } @@ -1125,7 +1125,7 @@ void lb_add_debug_context_variable(lbProcedure *p, lbAddr const &ctx) { } -String debug_info_mangle_constant_name(Entity *e, bool *did_allocate_) { +gb_internal String debug_info_mangle_constant_name(Entity *e, bool *did_allocate_) { String name = e->token.string; if (e->pkg && e->pkg->name.len > 0) { // NOTE(bill): C++ NONSENSE FOR DEBUG SHITE! @@ -1135,7 +1135,7 @@ String debug_info_mangle_constant_name(Entity *e, bool *did_allocate_) { return name; } -void add_debug_info_global_variable_expr(lbModule *m, String const &name, LLVMMetadataRef dtype, LLVMMetadataRef expr) { +gb_internal void add_debug_info_global_variable_expr(lbModule *m, String const &name, LLVMMetadataRef dtype, LLVMMetadataRef expr) { LLVMMetadataRef scope = nullptr; LLVMMetadataRef file = nullptr; unsigned line = 0; @@ -1151,7 +1151,7 @@ void add_debug_info_global_variable_expr(lbModule *m, String const &name, LLVMMe expr, decl, 8/*AlignInBits*/); } -void add_debug_info_for_global_constant_internal_i64(lbModule *m, Entity *e, LLVMMetadataRef dtype, i64 v) { +gb_internal void add_debug_info_for_global_constant_internal_i64(lbModule *m, Entity *e, LLVMMetadataRef dtype, i64 v) { LLVMMetadataRef expr = LLVMDIBuilderCreateConstantValueExpression(m->debug_builder, v); bool did_allocate = false; @@ -1167,7 +1167,7 @@ void add_debug_info_for_global_constant_internal_i64(lbModule *m, Entity *e, LLV } } -void add_debug_info_for_global_constant_from_entity(lbGenerator *gen, Entity *e) { +gb_internal void add_debug_info_for_global_constant_from_entity(lbGenerator *gen, Entity *e) { if (e == nullptr || e->kind != Entity_Constant) { return; } diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index 2c1bfecd6..d574caf4c 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -1,6 +1,6 @@ -lbValue lb_emit_arith_matrix(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type, bool component_wise); +gb_internal lbValue lb_emit_arith_matrix(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type, bool component_wise); -lbValue lb_emit_logical_binary_expr(lbProcedure *p, TokenKind op, Ast *left, Ast *right, Type *type) { +gb_internal lbValue lb_emit_logical_binary_expr(lbProcedure *p, TokenKind op, Ast *left, Ast *right, Type *type) { lbModule *m = p->module; lbBlock *rhs = lb_create_block(p, "logical.cmp.rhs"); @@ -113,7 +113,7 @@ lbValue lb_emit_logical_binary_expr(lbProcedure *p, TokenKind op, Ast *left, Ast } -lbValue lb_emit_unary_arith(lbProcedure *p, TokenKind op, lbValue x, Type *type) { +gb_internal lbValue lb_emit_unary_arith(lbProcedure *p, TokenKind op, lbValue x, Type *type) { switch (op) { case Token_Add: return x; @@ -283,7 +283,7 @@ lbValue lb_emit_unary_arith(lbProcedure *p, TokenKind op, lbValue x, Type *type) return res; } -bool lb_try_direct_vector_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type, lbValue *res_) { +gb_internal bool lb_try_direct_vector_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type, lbValue *res_) { GB_ASSERT(is_type_array_like(type)); Type *elem_type = base_array_type(type); @@ -418,7 +418,7 @@ bool lb_try_direct_vector_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbVal } -lbValue lb_emit_arith_array(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type) { +gb_internal lbValue lb_emit_arith_array(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type) { GB_ASSERT(is_type_array_like(lhs.type) || is_type_array_like(rhs.type)); lhs = lb_emit_conv(p, lhs, type); @@ -490,7 +490,7 @@ lbValue lb_emit_arith_array(lbProcedure *p, TokenKind op, lbValue lhs, lbValue r } } -bool lb_is_matrix_simdable(Type *t) { +gb_internal bool lb_is_matrix_simdable(Type *t) { Type *mt = base_type(t); GB_ASSERT(mt->kind == Type_Matrix); @@ -534,7 +534,7 @@ bool lb_is_matrix_simdable(Type *t) { } -LLVMValueRef lb_matrix_to_vector(lbProcedure *p, lbValue matrix) { +gb_internal LLVMValueRef lb_matrix_to_vector(lbProcedure *p, lbValue matrix) { Type *mt = base_type(matrix.type); GB_ASSERT(mt->kind == Type_Matrix); LLVMTypeRef elem_type = lb_type(p->module, mt->Matrix.elem); @@ -554,7 +554,7 @@ LLVMValueRef lb_matrix_to_vector(lbProcedure *p, lbValue matrix) { #endif } -LLVMValueRef lb_matrix_trimmed_vector_mask(lbProcedure *p, Type *mt) { +gb_internal LLVMValueRef lb_matrix_trimmed_vector_mask(lbProcedure *p, Type *mt) { mt = base_type(mt); GB_ASSERT(mt->kind == Type_Matrix); @@ -574,7 +574,7 @@ LLVMValueRef lb_matrix_trimmed_vector_mask(lbProcedure *p, Type *mt) { return mask; } -LLVMValueRef lb_matrix_to_trimmed_vector(lbProcedure *p, lbValue m) { +gb_internal LLVMValueRef lb_matrix_to_trimmed_vector(lbProcedure *p, lbValue m) { LLVMValueRef vector = lb_matrix_to_vector(p, m); Type *mt = base_type(m.type); @@ -592,7 +592,7 @@ LLVMValueRef lb_matrix_to_trimmed_vector(lbProcedure *p, lbValue m) { } -lbValue lb_emit_matrix_tranpose(lbProcedure *p, lbValue m, Type *type) { +gb_internal lbValue lb_emit_matrix_tranpose(lbProcedure *p, lbValue m, Type *type) { if (is_type_array(m.type)) { i32 rank = type_math_rank(m.type); if (rank == 2) { @@ -669,7 +669,7 @@ lbValue lb_emit_matrix_tranpose(lbProcedure *p, lbValue m, Type *type) { return lb_addr_load(p, res); } -lbValue lb_matrix_cast_vector_to_type(lbProcedure *p, LLVMValueRef vector, Type *type) { +gb_internal lbValue lb_matrix_cast_vector_to_type(lbProcedure *p, LLVMValueRef vector, Type *type) { lbAddr res = lb_add_local_generated(p, type, true); LLVMValueRef res_ptr = res.addr.value; unsigned alignment = cast(unsigned)gb_max(type_align_of(type), lb_alignof(LLVMTypeOf(vector))); @@ -681,7 +681,7 @@ lbValue lb_matrix_cast_vector_to_type(lbProcedure *p, LLVMValueRef vector, Type return lb_addr_load(p, res); } -lbValue lb_emit_matrix_flatten(lbProcedure *p, lbValue m, Type *type) { +gb_internal lbValue lb_emit_matrix_flatten(lbProcedure *p, lbValue m, Type *type) { if (is_type_array(m.type)) { // no-op m.type = type; @@ -710,7 +710,7 @@ lbValue lb_emit_matrix_flatten(lbProcedure *p, lbValue m, Type *type) { } -lbValue lb_emit_outer_product(lbProcedure *p, lbValue a, lbValue b, Type *type) { +gb_internal lbValue lb_emit_outer_product(lbProcedure *p, lbValue a, lbValue b, Type *type) { Type *mt = base_type(type); Type *at = base_type(a.type); Type *bt = base_type(b.type); @@ -741,7 +741,7 @@ lbValue lb_emit_outer_product(lbProcedure *p, lbValue a, lbValue b, Type *type) } -lbValue lb_emit_matrix_mul(lbProcedure *p, lbValue lhs, lbValue rhs, Type *type) { +gb_internal lbValue lb_emit_matrix_mul(lbProcedure *p, lbValue lhs, lbValue rhs, Type *type) { // TODO(bill): Handle edge case for f16 types on x86(-64) platforms Type *xt = base_type(lhs.type); @@ -828,7 +828,7 @@ lbValue lb_emit_matrix_mul(lbProcedure *p, lbValue lhs, lbValue rhs, Type *type) } } -lbValue lb_emit_matrix_mul_vector(lbProcedure *p, lbValue lhs, lbValue rhs, Type *type) { +gb_internal lbValue lb_emit_matrix_mul_vector(lbProcedure *p, lbValue lhs, lbValue rhs, Type *type) { // TODO(bill): Handle edge case for f16 types on x86(-64) platforms Type *mt = base_type(lhs.type); @@ -897,7 +897,7 @@ lbValue lb_emit_matrix_mul_vector(lbProcedure *p, lbValue lhs, lbValue rhs, Type return lb_addr_load(p, res); } -lbValue lb_emit_vector_mul_matrix(lbProcedure *p, lbValue lhs, lbValue rhs, Type *type) { +gb_internal lbValue lb_emit_vector_mul_matrix(lbProcedure *p, lbValue lhs, lbValue rhs, Type *type) { // TODO(bill): Handle edge case for f16 types on x86(-64) platforms Type *mt = base_type(rhs.type); @@ -984,7 +984,7 @@ lbValue lb_emit_vector_mul_matrix(lbProcedure *p, lbValue lhs, lbValue rhs, Type -lbValue lb_emit_arith_matrix(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type, bool component_wise) { +gb_internal lbValue lb_emit_arith_matrix(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type, bool component_wise) { GB_ASSERT(is_type_matrix(lhs.type) || is_type_matrix(rhs.type)); if (op == Token_Mul && !component_wise) { @@ -1056,7 +1056,7 @@ lbValue lb_emit_arith_matrix(lbProcedure *p, TokenKind op, lbValue lhs, lbValue -lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type) { +gb_internal lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type) { if (is_type_array_like(lhs.type) || is_type_array_like(rhs.type)) { return lb_emit_arith_array(p, op, lhs, rhs, type); } else if (is_type_matrix(lhs.type) || is_type_matrix(rhs.type)) { @@ -1325,7 +1325,7 @@ handle_op: return {}; } -lbValue lb_build_binary_expr(lbProcedure *p, Ast *expr) { +gb_internal lbValue lb_build_binary_expr(lbProcedure *p, Ast *expr) { ast_node(be, BinaryExpr, expr); TypeAndValue tv = type_and_value_of_expr(expr); @@ -1472,7 +1472,7 @@ lbValue lb_build_binary_expr(lbProcedure *p, Ast *expr) { return {}; } -lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) { +gb_internal lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) { lbModule *m = p->module; t = reduce_tuple_to_single_type(t); @@ -2202,7 +2202,7 @@ lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) { return {}; } -lbValue lb_compare_records(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue right, Type *type) { +gb_internal lbValue lb_compare_records(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue right, Type *type) { GB_ASSERT((is_type_struct(type) || is_type_union(type)) && is_type_comparable(type)); lbValue left_ptr = lb_address_from_load_or_generate_local(p, left); lbValue right_ptr = lb_address_from_load_or_generate_local(p, right); @@ -2230,7 +2230,7 @@ lbValue lb_compare_records(lbProcedure *p, TokenKind op_kind, lbValue left, lbVa -lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue right) { +gb_internal lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue right) { Type *a = core_type(left.type); Type *b = core_type(right.type); @@ -2642,7 +2642,7 @@ lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue ri -lbValue lb_emit_comp_against_nil(lbProcedure *p, TokenKind op_kind, lbValue x) { +gb_internal lbValue lb_emit_comp_against_nil(lbProcedure *p, TokenKind op_kind, lbValue x) { lbValue res = {}; res.type = t_llvm_bool; Type *t = x.type; @@ -2803,7 +2803,7 @@ lbValue lb_emit_comp_against_nil(lbProcedure *p, TokenKind op_kind, lbValue x) { return {}; } -lbValue lb_make_soa_pointer(lbProcedure *p, Type *type, lbValue const &addr, lbValue const &index) { +gb_internal lbValue lb_make_soa_pointer(lbProcedure *p, Type *type, lbValue const &addr, lbValue const &index) { lbAddr v = lb_add_local_generated(p, type, false); lbValue ptr = lb_emit_struct_ep(p, v.addr, 0); lbValue idx = lb_emit_struct_ep(p, v.addr, 1); @@ -2813,7 +2813,7 @@ lbValue lb_make_soa_pointer(lbProcedure *p, Type *type, lbValue const &addr, lbV return lb_addr_load(p, v); } -lbValue lb_build_unary_and(lbProcedure *p, Ast *expr) { +gb_internal lbValue lb_build_unary_and(lbProcedure *p, Ast *expr) { ast_node(ue, UnaryExpr, expr); auto tv = type_and_value_of_expr(expr); @@ -3023,8 +3023,8 @@ lbValue lb_build_unary_and(lbProcedure *p, Ast *expr) { return lb_build_addr_ptr(p, ue->expr); } -lbValue lb_build_expr_internal(lbProcedure *p, Ast *expr); -lbValue lb_build_expr(lbProcedure *p, Ast *expr) { +gb_internal lbValue lb_build_expr_internal(lbProcedure *p, Ast *expr); +gb_internal lbValue lb_build_expr(lbProcedure *p, Ast *expr) { u16 prev_state_flags = p->state_flags; defer (p->state_flags = prev_state_flags); @@ -3080,7 +3080,7 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) { return res; } -lbValue lb_build_expr_internal(lbProcedure *p, Ast *expr) { +gb_internal lbValue lb_build_expr_internal(lbProcedure *p, Ast *expr) { lbModule *m = p->module; expr = unparen_expr(expr); @@ -3355,10 +3355,10 @@ lbValue lb_build_expr_internal(lbProcedure *p, Ast *expr) { return {}; } -lbAddr lb_get_soa_variable_addr(lbProcedure *p, Entity *e) { +gb_internal lbAddr lb_get_soa_variable_addr(lbProcedure *p, Entity *e) { return map_must_get(&p->module->soa_values, e); } -lbValue lb_get_using_variable(lbProcedure *p, Entity *e) { +gb_internal lbValue lb_get_using_variable(lbProcedure *p, Entity *e) { GB_ASSERT(e->kind == Entity_Variable && e->flags & EntityFlag_Using); String name = e->token.string; Entity *parent = e->using_parent; @@ -3393,7 +3393,7 @@ lbValue lb_get_using_variable(lbProcedure *p, Entity *e) { -lbAddr lb_build_addr_from_entity(lbProcedure *p, Entity *e, Ast *expr) { +gb_internal lbAddr lb_build_addr_from_entity(lbProcedure *p, Entity *e, Ast *expr) { GB_ASSERT(e != nullptr); if (e->kind == Entity_Constant) { Type *t = default_type(type_of_expr(expr)); @@ -3427,7 +3427,7 @@ lbAddr lb_build_addr_from_entity(lbProcedure *p, Entity *e, Ast *expr) { return lb_addr(v); } -lbAddr lb_build_array_swizzle_addr(lbProcedure *p, AstCallExpr *ce, TypeAndValue const &tv) { +gb_internal lbAddr lb_build_array_swizzle_addr(lbProcedure *p, AstCallExpr *ce, TypeAndValue const &tv) { isize index_count = ce->args.count-1; lbAddr addr = lb_build_addr(p, ce->args[0]); if (index_count == 0) { @@ -3463,8 +3463,8 @@ lbAddr lb_build_array_swizzle_addr(lbProcedure *p, AstCallExpr *ce, TypeAndValue } -lbAddr lb_build_addr_internal(lbProcedure *p, Ast *expr); -lbAddr lb_build_addr(lbProcedure *p, Ast *expr) { +gb_internal lbAddr lb_build_addr_internal(lbProcedure *p, Ast *expr); +gb_internal lbAddr lb_build_addr(lbProcedure *p, Ast *expr) { expr = unparen_expr(expr); // IMPORTANT NOTE(bill): @@ -3489,7 +3489,7 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) { return addr; } -void lb_build_addr_compound_lit_populate(lbProcedure *p, Slice const &elems, Array *temp_data, Type *compound_type) { +gb_internal void lb_build_addr_compound_lit_populate(lbProcedure *p, Slice const &elems, Array *temp_data, Type *compound_type) { Type *bt = base_type(compound_type); Type *et = nullptr; switch (bt->kind) { @@ -3595,7 +3595,7 @@ void lb_build_addr_compound_lit_populate(lbProcedure *p, Slice const &ele } } } -void lb_build_addr_compound_lit_assign_array(lbProcedure *p, Array const &temp_data) { +gb_internal void lb_build_addr_compound_lit_assign_array(lbProcedure *p, Array const &temp_data) { for_array(i, temp_data) { auto td = temp_data[i]; if (td.value.value != nullptr) { @@ -3614,7 +3614,7 @@ void lb_build_addr_compound_lit_assign_array(lbProcedure *p, Arrayexpr)); @@ -3833,7 +3833,7 @@ lbAddr lb_build_addr_index_expr(lbProcedure *p, Ast *expr) { } -lbAddr lb_build_addr_slice_expr(lbProcedure *p, Ast *expr) { +gb_internal lbAddr lb_build_addr_slice_expr(lbProcedure *p, Ast *expr) { ast_node(se, SliceExpr, expr); lbValue low = lb_const_int(p->module, t_int, 0); @@ -4031,7 +4031,7 @@ lbAddr lb_build_addr_slice_expr(lbProcedure *p, Ast *expr) { } -lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) { +gb_internal lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) { ast_node(cl, CompoundLit, expr); Type *type = type_of_expr(expr); @@ -4383,7 +4383,7 @@ lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) { } -lbAddr lb_build_addr_internal(lbProcedure *p, Ast *expr) { +gb_internal lbAddr lb_build_addr_internal(lbProcedure *p, Ast *expr) { switch (expr->kind) { case_ast_node(i, Implicit, expr); lbAddr v = {}; diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 3ca98845a..f30038da8 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -1,4 +1,4 @@ -void lb_add_debug_local_variable(lbProcedure *p, LLVMValueRef ptr, Type *type, Token const &token); +gb_internal void lb_add_debug_local_variable(lbProcedure *p, LLVMValueRef ptr, Type *type, Token const &token); gb_global Entity *lb_global_type_info_data_entity = {}; gb_global lbAddr lb_global_type_info_member_types = {}; @@ -15,7 +15,7 @@ gb_global isize lb_global_type_info_member_usings_index = 0; gb_global isize lb_global_type_info_member_tags_index = 0; -void lb_init_module(lbModule *m, Checker *c) { +gb_internal void lb_init_module(lbModule *m, Checker *c) { m->info = &c->info; gbString module_name = gb_string_make(heap_allocator(), "odin_package"); @@ -82,7 +82,7 @@ void lb_init_module(lbModule *m, Checker *c) { } -bool lb_init_generator(lbGenerator *gen, Checker *c) { +gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) { if (global_error_collector.count != 0) { return false; } @@ -164,7 +164,7 @@ bool lb_init_generator(lbGenerator *gen, Checker *c) { -lbValue lb_global_type_info_data_ptr(lbModule *m) { +gb_internal lbValue lb_global_type_info_data_ptr(lbModule *m) { lbValue v = lb_find_value_from_entity(m, lb_global_type_info_data_entity); return v; } @@ -187,7 +187,7 @@ struct lbCompoundLitElemTempData { }; -lbLoopData lb_loop_start(lbProcedure *p, isize count, Type *index_type=t_i32) { +gb_internal lbLoopData lb_loop_start(lbProcedure *p, isize count, Type *index_type=t_i32) { lbLoopData data = {}; lbValue max = lb_const_int(p->module, t_int, count); @@ -210,7 +210,7 @@ lbLoopData lb_loop_start(lbProcedure *p, isize count, Type *index_type=t_i32) { return data; } -void lb_loop_end(lbProcedure *p, lbLoopData const &data) { +gb_internal void lb_loop_end(lbProcedure *p, lbLoopData const &data) { if (data.idx_addr.addr.value != nullptr) { lb_emit_increment(p, data.idx_addr.addr); lb_emit_jump(p, data.loop); @@ -219,19 +219,19 @@ void lb_loop_end(lbProcedure *p, lbLoopData const &data) { } -void lb_make_global_private_const(LLVMValueRef global_data) { +gb_internal void lb_make_global_private_const(LLVMValueRef global_data) { LLVMSetLinkage(global_data, LLVMPrivateLinkage); LLVMSetUnnamedAddress(global_data, LLVMGlobalUnnamedAddr); LLVMSetGlobalConstant(global_data, true); } -void lb_make_global_private_const(lbAddr const &addr) { +gb_internal void lb_make_global_private_const(lbAddr const &addr) { lb_make_global_private_const(addr.addr.value); } // This emits a GEP at 0, index -lbValue lb_emit_epi(lbProcedure *p, lbValue const &value, isize index) { +gb_internal lbValue lb_emit_epi(lbProcedure *p, lbValue const &value, isize index) { GB_ASSERT(is_type_pointer(value.type)); Type *type = type_deref(value.type); @@ -251,7 +251,7 @@ lbValue lb_emit_epi(lbProcedure *p, lbValue const &value, isize index) { return res; } // This emits a GEP at 0, index -lbValue lb_emit_epi(lbModule *m, lbValue const &value, isize index) { +gb_internal lbValue lb_emit_epi(lbModule *m, lbValue const &value, isize index) { GB_ASSERT(is_type_pointer(value.type)); GB_ASSERT(LLVMIsConstant(value.value)); Type *type = type_deref(value.type); @@ -269,17 +269,17 @@ lbValue lb_emit_epi(lbModule *m, lbValue const &value, isize index) { -LLVMValueRef llvm_zero(lbModule *m) { +gb_internal LLVMValueRef llvm_zero(lbModule *m) { return LLVMConstInt(lb_type(m, t_int), 0, false); } -LLVMValueRef llvm_zero32(lbModule *m) { +gb_internal LLVMValueRef llvm_zero32(lbModule *m) { return LLVMConstInt(lb_type(m, t_i32), 0, false); } -LLVMValueRef llvm_one(lbModule *m) { +gb_internal LLVMValueRef llvm_one(lbModule *m) { return LLVMConstInt(lb_type(m, t_i32), 1, false); } -LLVMValueRef llvm_alloca(lbProcedure *p, LLVMTypeRef llvm_type, isize alignment, char const *name) { +gb_internal LLVMValueRef llvm_alloca(lbProcedure *p, LLVMTypeRef llvm_type, isize alignment, char const *name) { LLVMPositionBuilderAtEnd(p->builder, p->decl_block->block); LLVMValueRef val = LLVMBuildAlloca(p->builder, llvm_type, name); @@ -290,20 +290,20 @@ LLVMValueRef llvm_alloca(lbProcedure *p, LLVMTypeRef llvm_type, isize alignment, return val; } -lbValue lb_zero(lbModule *m, Type *t) { +gb_internal lbValue lb_zero(lbModule *m, Type *t) { lbValue v = {}; v.value = LLVMConstInt(lb_type(m, t), 0, false); v.type = t; return v; } -LLVMValueRef llvm_cstring(lbModule *m, String const &str) { +gb_internal LLVMValueRef llvm_cstring(lbModule *m, String const &str) { lbValue v = lb_find_or_add_entity_string(m, str); unsigned indices[1] = {0}; return LLVMConstExtractValue(v.value, indices, gb_count_of(indices)); } -bool lb_is_instr_terminating(LLVMValueRef instr) { +gb_internal bool lb_is_instr_terminating(LLVMValueRef instr) { if (instr != nullptr) { LLVMOpcode op = LLVMGetInstructionOpcode(instr); switch (op) { @@ -322,7 +322,7 @@ bool lb_is_instr_terminating(LLVMValueRef instr) { -lbModule *lb_pkg_module(lbGenerator *gen, AstPackage *pkg) { +gb_internal lbModule *lb_pkg_module(lbGenerator *gen, AstPackage *pkg) { auto *found = map_get(&gen->modules, pkg); if (found) { return *found; @@ -331,7 +331,7 @@ lbModule *lb_pkg_module(lbGenerator *gen, AstPackage *pkg) { } -lbAddr lb_addr(lbValue addr) { +gb_internal lbAddr lb_addr(lbValue addr) { lbAddr v = {lbAddr_Default, addr}; if (addr.type != nullptr && is_type_relative_pointer(type_deref(addr.type))) { GB_ASSERT(is_type_pointer(addr.type)); @@ -344,7 +344,7 @@ lbAddr lb_addr(lbValue addr) { } -lbAddr lb_addr_map(lbValue addr, lbValue map_key, Type *map_type, Type *map_result) { +gb_internal lbAddr lb_addr_map(lbValue addr, lbValue map_key, Type *map_type, Type *map_result) { GB_ASSERT(is_type_pointer(addr.type)); Type *mt = type_deref(addr.type); GB_ASSERT(is_type_map(mt)); @@ -357,14 +357,14 @@ lbAddr lb_addr_map(lbValue addr, lbValue map_key, Type *map_type, Type *map_resu } -lbAddr lb_addr_soa_variable(lbValue addr, lbValue index, Ast *index_expr) { +gb_internal lbAddr lb_addr_soa_variable(lbValue addr, lbValue index, Ast *index_expr) { lbAddr v = {lbAddr_SoaVariable, addr}; v.soa.index = index; v.soa.index_expr = index_expr; return v; } -lbAddr lb_addr_swizzle(lbValue addr, Type *array_type, u8 swizzle_count, u8 swizzle_indices[4]) { +gb_internal lbAddr lb_addr_swizzle(lbValue addr, Type *array_type, u8 swizzle_count, u8 swizzle_indices[4]) { GB_ASSERT(is_type_array(array_type)); GB_ASSERT(1 < swizzle_count && swizzle_count <= 4); lbAddr v = {lbAddr_Swizzle, addr}; @@ -374,7 +374,7 @@ lbAddr lb_addr_swizzle(lbValue addr, Type *array_type, u8 swizzle_count, u8 swiz return v; } -lbAddr lb_addr_swizzle_large(lbValue addr, Type *array_type, Slice const &swizzle_indices) { +gb_internal lbAddr lb_addr_swizzle_large(lbValue addr, Type *array_type, Slice const &swizzle_indices) { GB_ASSERT_MSG(is_type_array(array_type), "%s", type_to_string(array_type)); lbAddr v = {lbAddr_SwizzleLarge, addr}; v.swizzle_large.type = array_type; @@ -382,7 +382,7 @@ lbAddr lb_addr_swizzle_large(lbValue addr, Type *array_type, Slice const &s return v; } -Type *lb_addr_type(lbAddr const &addr) { +gb_internal Type *lb_addr_type(lbAddr const &addr) { if (addr.addr.value == nullptr) { return nullptr; } @@ -411,7 +411,7 @@ Type *lb_addr_type(lbAddr const &addr) { return type_deref(addr.addr.type); } -lbValue lb_addr_get_ptr(lbProcedure *p, lbAddr const &addr) { +gb_internal lbValue lb_addr_get_ptr(lbProcedure *p, lbAddr const &addr) { if (addr.addr.value == nullptr) { GB_PANIC("Illegal addr -> nullptr"); return {}; @@ -462,12 +462,12 @@ lbValue lb_addr_get_ptr(lbProcedure *p, lbAddr const &addr) { } -lbValue lb_build_addr_ptr(lbProcedure *p, Ast *expr) { +gb_internal lbValue lb_build_addr_ptr(lbProcedure *p, Ast *expr) { lbAddr addr = lb_build_addr(p, expr); return lb_addr_get_ptr(p, addr); } -void lb_emit_bounds_check(lbProcedure *p, Token token, lbValue index, lbValue len) { +gb_internal void lb_emit_bounds_check(lbProcedure *p, Token token, lbValue index, lbValue len) { if (build_context.no_bounds_check) { return; } @@ -492,7 +492,7 @@ void lb_emit_bounds_check(lbProcedure *p, Token token, lbValue index, lbValue le lb_emit_runtime_call(p, "bounds_check_error", args); } -void lb_emit_matrix_bounds_check(lbProcedure *p, Token token, lbValue row_index, lbValue column_index, lbValue row_count, lbValue column_count) { +gb_internal void lb_emit_matrix_bounds_check(lbProcedure *p, Token token, lbValue row_index, lbValue column_index, lbValue row_count, lbValue column_count) { if (build_context.no_bounds_check) { return; } @@ -522,7 +522,7 @@ void lb_emit_matrix_bounds_check(lbProcedure *p, Token token, lbValue row_index, } -void lb_emit_multi_pointer_slice_bounds_check(lbProcedure *p, Token token, lbValue low, lbValue high) { +gb_internal void lb_emit_multi_pointer_slice_bounds_check(lbProcedure *p, Token token, lbValue low, lbValue high) { if (build_context.no_bounds_check) { return; } @@ -547,7 +547,7 @@ void lb_emit_multi_pointer_slice_bounds_check(lbProcedure *p, Token token, lbVal lb_emit_runtime_call(p, "multi_pointer_slice_expr_error", args); } -void lb_emit_slice_bounds_check(lbProcedure *p, Token token, lbValue low, lbValue high, lbValue len, bool lower_value_used) { +gb_internal void lb_emit_slice_bounds_check(lbProcedure *p, Token token, lbValue low, lbValue high, lbValue len, bool lower_value_used) { if (build_context.no_bounds_check) { return; } @@ -585,14 +585,14 @@ void lb_emit_slice_bounds_check(lbProcedure *p, Token token, lbValue low, lbValu } } -unsigned lb_try_get_alignment(LLVMValueRef addr_ptr, unsigned default_alignment) { +gb_internal unsigned lb_try_get_alignment(LLVMValueRef addr_ptr, unsigned default_alignment) { if (LLVMIsAGlobalValue(addr_ptr) || LLVMIsAAllocaInst(addr_ptr) || LLVMIsALoadInst(addr_ptr)) { return LLVMGetAlignment(addr_ptr); } return default_alignment; } -bool lb_try_update_alignment(LLVMValueRef addr_ptr, unsigned alignment) { +gb_internal bool lb_try_update_alignment(LLVMValueRef addr_ptr, unsigned alignment) { if (LLVMIsAGlobalValue(addr_ptr) || LLVMIsAAllocaInst(addr_ptr) || LLVMIsALoadInst(addr_ptr)) { if (LLVMGetAlignment(addr_ptr) < alignment) { if (LLVMIsAAllocaInst(addr_ptr) || LLVMIsAGlobalValue(addr_ptr)) { @@ -604,15 +604,15 @@ bool lb_try_update_alignment(LLVMValueRef addr_ptr, unsigned alignment) { return false; } -bool lb_try_update_alignment(lbValue ptr, unsigned alignment) { +gb_internal bool lb_try_update_alignment(lbValue ptr, unsigned alignment) { return lb_try_update_alignment(ptr.value, alignment); } -bool lb_can_try_to_inline_array_arith(Type *t) { +gb_internal bool lb_can_try_to_inline_array_arith(Type *t) { return type_size_of(t) <= build_context.max_simd_align; } -bool lb_try_vector_cast(lbModule *m, lbValue ptr, LLVMTypeRef *vector_type_) { +gb_internal bool lb_try_vector_cast(lbModule *m, lbValue ptr, LLVMTypeRef *vector_type_) { Type *array_type = base_type(type_deref(ptr.type)); GB_ASSERT(is_type_array_like(array_type)); i64 count = get_array_type_count(array_type); @@ -647,7 +647,7 @@ bool lb_try_vector_cast(lbModule *m, lbValue ptr, LLVMTypeRef *vector_type_) { return false; } -void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value) { +gb_internal void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value) { if (addr.addr.value == nullptr) { return; } @@ -874,7 +874,7 @@ void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value) { lb_emit_store(p, addr.addr, value); } -void lb_const_store(lbValue ptr, lbValue value) { +gb_internal void lb_const_store(lbValue ptr, lbValue value) { GB_ASSERT(lb_is_const(ptr)); GB_ASSERT(lb_is_const(value)); GB_ASSERT(is_type_pointer(ptr.type)); @@ -882,7 +882,7 @@ void lb_const_store(lbValue ptr, lbValue value) { } -bool lb_is_type_proc_recursive(Type *t) { +gb_internal bool lb_is_type_proc_recursive(Type *t) { for (;;) { if (t == nullptr) { return false; @@ -902,7 +902,7 @@ bool lb_is_type_proc_recursive(Type *t) { } } -void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value) { +gb_internal void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value) { GB_ASSERT(value.value != nullptr); Type *a = type_deref(ptr.type); @@ -978,11 +978,11 @@ void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value) { } } -LLVMTypeRef llvm_addr_type(lbModule *module, lbValue addr_val) { +gb_internal LLVMTypeRef llvm_addr_type(lbModule *module, lbValue addr_val) { return lb_type(module, type_deref(addr_val.type)); } -lbValue lb_emit_load(lbProcedure *p, lbValue value) { +gb_internal lbValue lb_emit_load(lbProcedure *p, lbValue value) { GB_ASSERT(value.value != nullptr); if (is_type_multi_pointer(value.type)) { Type *vt = base_type(value.type); @@ -1003,7 +1003,7 @@ lbValue lb_emit_load(lbProcedure *p, lbValue value) { return lbValue{v, t}; } -lbValue lb_addr_load(lbProcedure *p, lbAddr const &addr) { +gb_internal lbValue lb_addr_load(lbProcedure *p, lbAddr const &addr) { GB_ASSERT(addr.addr.value != nullptr); @@ -1243,11 +1243,11 @@ lbValue lb_addr_load(lbProcedure *p, lbAddr const &addr) { return lb_emit_load(p, addr.addr); } -lbValue lb_const_union_tag(lbModule *m, Type *u, Type *v) { +gb_internal lbValue lb_const_union_tag(lbModule *m, Type *u, Type *v) { return lb_const_value(m, union_tag_type(u), exact_value_i64(union_variant_index(u, v))); } -lbValue lb_emit_union_tag_ptr(lbProcedure *p, lbValue u) { +gb_internal lbValue lb_emit_union_tag_ptr(lbProcedure *p, lbValue u) { Type *t = u.type; GB_ASSERT_MSG(is_type_pointer(t) && is_type_union(type_deref(t)), "%s", type_to_string(t)); @@ -1269,14 +1269,14 @@ lbValue lb_emit_union_tag_ptr(lbProcedure *p, lbValue u) { return tag_ptr; } -lbValue lb_emit_union_tag_value(lbProcedure *p, lbValue u) { +gb_internal lbValue lb_emit_union_tag_value(lbProcedure *p, lbValue u) { lbValue ptr = lb_address_from_load_or_generate_local(p, u); lbValue tag_ptr = lb_emit_union_tag_ptr(p, ptr); return lb_emit_load(p, tag_ptr); } -void lb_emit_store_union_variant_tag(lbProcedure *p, lbValue parent, Type *variant_type) { +gb_internal void lb_emit_store_union_variant_tag(lbProcedure *p, lbValue parent, Type *variant_type) { Type *t = type_deref(parent.type); if (is_type_union_maybe_pointer(t) || type_size_of(t) == 0) { @@ -1287,7 +1287,7 @@ void lb_emit_store_union_variant_tag(lbProcedure *p, lbValue parent, Type *varia } } -void lb_emit_store_union_variant(lbProcedure *p, lbValue parent, lbValue variant, Type *variant_type) { +gb_internal void lb_emit_store_union_variant(lbProcedure *p, lbValue parent, lbValue variant, Type *variant_type) { Type *pt = base_type(type_deref(parent.type)); GB_ASSERT(pt->kind == Type_Union); if (pt->Union.kind == UnionType_shared_nil) { @@ -1320,14 +1320,14 @@ void lb_emit_store_union_variant(lbProcedure *p, lbValue parent, lbValue variant } -void lb_clone_struct_type(LLVMTypeRef dst, LLVMTypeRef src) { +gb_internal void lb_clone_struct_type(LLVMTypeRef dst, LLVMTypeRef src) { unsigned field_count = LLVMCountStructElementTypes(src); LLVMTypeRef *fields = gb_alloc_array(temporary_allocator(), LLVMTypeRef, field_count); LLVMGetStructElementTypes(src, fields); LLVMStructSetBody(dst, fields, field_count, LLVMIsPackedStruct(src)); } -LLVMTypeRef lb_alignment_prefix_type_hack(lbModule *m, i64 alignment) { +gb_internal LLVMTypeRef lb_alignment_prefix_type_hack(lbModule *m, i64 alignment) { switch (alignment) { case 1: return LLVMArrayType(lb_type(m, t_u8), 0); @@ -1342,7 +1342,7 @@ LLVMTypeRef lb_alignment_prefix_type_hack(lbModule *m, i64 alignment) { } } -String lb_mangle_name(lbModule *m, Entity *e) { +gb_internal String lb_mangle_name(lbModule *m, Entity *e) { String name = e->token.string; AstPackage *pkg = e->pkg; @@ -1384,7 +1384,7 @@ String lb_mangle_name(lbModule *m, Entity *e) { return mangled_name; } -String lb_set_nested_type_name_ir_mangled_name(Entity *e, lbProcedure *p) { +gb_internal String lb_set_nested_type_name_ir_mangled_name(Entity *e, lbProcedure *p) { // NOTE(bill, 2020-03-08): A polymorphic procedure may take a nested type declaration // and as a result, the declaration does not have time to determine what it should be @@ -1440,7 +1440,7 @@ String lb_set_nested_type_name_ir_mangled_name(Entity *e, lbProcedure *p) { } } -String lb_get_entity_name(lbModule *m, Entity *e, String default_name) { +gb_internal String lb_get_entity_name(lbModule *m, Entity *e, String default_name) { if (e != nullptr && e->kind == Entity_TypeName && e->TypeName.ir_mangled_name.len != 0) { return e->TypeName.ir_mangled_name; } @@ -1488,7 +1488,7 @@ String lb_get_entity_name(lbModule *m, Entity *e, String default_name) { } -LLVMTypeRef lb_type_internal_for_procedures_raw(lbModule *m, Type *type) { +gb_internal LLVMTypeRef lb_type_internal_for_procedures_raw(lbModule *m, Type *type) { Type *original_type = type; type = base_type(original_type); GB_ASSERT(type->kind == Type_Proc); @@ -1607,7 +1607,7 @@ LLVMTypeRef lb_type_internal_for_procedures_raw(lbModule *m, Type *type) { return new_abi_fn_type; } -LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { +gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { LLVMContextRef ctx = m->ctx; i64 size = type_size_of(type); // Check size gb_unused(size); @@ -2145,7 +2145,7 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { return LLVMInt32TypeInContext(ctx); } -LLVMTypeRef lb_type(lbModule *m, Type *type) { +gb_internal LLVMTypeRef lb_type(lbModule *m, Type *type) { type = default_type(type); LLVMTypeRef *found = map_get(&m->types, type); @@ -2164,7 +2164,7 @@ LLVMTypeRef lb_type(lbModule *m, Type *type) { return llvm_type; } -lbFunctionType *lb_get_function_type(lbModule *m, Type *pt) { +gb_internal lbFunctionType *lb_get_function_type(lbModule *m, Type *pt) { lbFunctionType **ft_found = nullptr; ft_found = map_get(&m->function_type_map, pt); if (!ft_found) { @@ -2177,7 +2177,7 @@ lbFunctionType *lb_get_function_type(lbModule *m, Type *pt) { return *ft_found; } -void lb_ensure_abi_function_type(lbModule *m, lbProcedure *p) { +gb_internal void lb_ensure_abi_function_type(lbModule *m, lbProcedure *p) { if (p->abi_function_type != nullptr) { return; } @@ -2192,20 +2192,20 @@ void lb_ensure_abi_function_type(lbModule *m, lbProcedure *p) { GB_ASSERT(p->abi_function_type != nullptr); } -void lb_add_entity(lbModule *m, Entity *e, lbValue val) { +gb_internal void lb_add_entity(lbModule *m, Entity *e, lbValue val) { if (e != nullptr) { map_set(&m->values, e, val); } } -void lb_add_member(lbModule *m, String const &name, lbValue val) { +gb_internal void lb_add_member(lbModule *m, String const &name, lbValue val) { if (name.len > 0) { string_map_set(&m->members, name, val); } } -void lb_add_member(lbModule *m, StringHashKey const &key, lbValue val) { +gb_internal void lb_add_member(lbModule *m, StringHashKey const &key, lbValue val) { string_map_set(&m->members, key, val); } -void lb_add_procedure_value(lbModule *m, lbProcedure *p) { +gb_internal void lb_add_procedure_value(lbModule *m, lbProcedure *p) { if (p->entity != nullptr) { map_set(&m->procedure_values, p->value, p->entity); } @@ -2214,7 +2214,7 @@ void lb_add_procedure_value(lbModule *m, lbProcedure *p) { -LLVMAttributeRef lb_create_enum_attribute_with_type(LLVMContextRef ctx, char const *name, LLVMTypeRef type) { +gb_internal LLVMAttributeRef lb_create_enum_attribute_with_type(LLVMContextRef ctx, char const *name, LLVMTypeRef type) { unsigned kind = 0; String s = make_string_c(name); @@ -2243,7 +2243,7 @@ LLVMAttributeRef lb_create_enum_attribute_with_type(LLVMContextRef ctx, char con #endif } -LLVMAttributeRef lb_create_enum_attribute(LLVMContextRef ctx, char const *name, u64 value) { +gb_internal LLVMAttributeRef lb_create_enum_attribute(LLVMContextRef ctx, char const *name, u64 value) { String s = make_string_c(name); // NOTE(2021-02-25, bill); All this attributes require a type associated with them @@ -2264,23 +2264,23 @@ LLVMAttributeRef lb_create_enum_attribute(LLVMContextRef ctx, char const *name, return LLVMCreateEnumAttribute(ctx, kind, value); } -void lb_add_proc_attribute_at_index(lbProcedure *p, isize index, char const *name, u64 value) { +gb_internal void lb_add_proc_attribute_at_index(lbProcedure *p, isize index, char const *name, u64 value) { LLVMAttributeRef attr = lb_create_enum_attribute(p->module->ctx, name, value); GB_ASSERT(attr != nullptr); LLVMAddAttributeAtIndex(p->value, cast(unsigned)index, attr); } -void lb_add_proc_attribute_at_index(lbProcedure *p, isize index, char const *name) { +gb_internal void lb_add_proc_attribute_at_index(lbProcedure *p, isize index, char const *name) { lb_add_proc_attribute_at_index(p, index, name, 0); } -void lb_add_attribute_to_proc(lbModule *m, LLVMValueRef proc_value, char const *name, u64 value=0) { +gb_internal void lb_add_attribute_to_proc(lbModule *m, LLVMValueRef proc_value, char const *name, u64 value=0) { LLVMAddAttributeAtIndex(proc_value, LLVMAttributeIndex_FunctionIndex, lb_create_enum_attribute(m->ctx, name, value)); } -void lb_add_edge(lbBlock *from, lbBlock *to) { +gb_internal void lb_add_edge(lbBlock *from, lbBlock *to) { LLVMValueRef instr = LLVMGetLastInstruction(from->block); if (instr == nullptr || !LLVMIsATerminatorInst(instr)) { array_add(&from->succs, to); @@ -2289,7 +2289,7 @@ void lb_add_edge(lbBlock *from, lbBlock *to) { } -lbBlock *lb_create_block(lbProcedure *p, char const *name, bool append) { +gb_internal lbBlock *lb_create_block(lbProcedure *p, char const *name, bool append) { lbBlock *b = gb_alloc_item(permanent_allocator(), lbBlock); b->block = LLVMCreateBasicBlockInContext(p->module->ctx, name); b->appended = false; @@ -2309,7 +2309,7 @@ lbBlock *lb_create_block(lbProcedure *p, char const *name, bool append) { return b; } -void lb_emit_jump(lbProcedure *p, lbBlock *target_block) { +gb_internal void lb_emit_jump(lbProcedure *p, lbBlock *target_block) { if (p->curr_block == nullptr) { return; } @@ -2323,7 +2323,7 @@ void lb_emit_jump(lbProcedure *p, lbBlock *target_block) { p->curr_block = nullptr; } -void lb_emit_if(lbProcedure *p, lbValue cond, lbBlock *true_block, lbBlock *false_block) { +gb_internal void lb_emit_if(lbProcedure *p, lbValue cond, lbBlock *true_block, lbBlock *false_block) { lbBlock *b = p->curr_block; if (b == nullptr) { return; @@ -2342,20 +2342,20 @@ void lb_emit_if(lbProcedure *p, lbValue cond, lbBlock *true_block, lbBlock *fals } -gb_inline LLVMTypeRef OdinLLVMGetInternalElementType(LLVMTypeRef type) { +gb_internal gb_inline LLVMTypeRef OdinLLVMGetInternalElementType(LLVMTypeRef type) { return LLVMGetElementType(type); } -LLVMTypeRef OdinLLVMGetArrayElementType(LLVMTypeRef type) { +gb_internal LLVMTypeRef OdinLLVMGetArrayElementType(LLVMTypeRef type) { GB_ASSERT(lb_is_type_kind(type, LLVMArrayTypeKind)); return OdinLLVMGetInternalElementType(type); } -LLVMTypeRef OdinLLVMGetVectorElementType(LLVMTypeRef type) { +gb_internal LLVMTypeRef OdinLLVMGetVectorElementType(LLVMTypeRef type) { GB_ASSERT(lb_is_type_kind(type, LLVMVectorTypeKind)); return OdinLLVMGetInternalElementType(type); } -LLVMValueRef OdinLLVMBuildTransmute(lbProcedure *p, LLVMValueRef val, LLVMTypeRef dst_type) { +gb_internal LLVMValueRef OdinLLVMBuildTransmute(lbProcedure *p, LLVMValueRef val, LLVMTypeRef dst_type) { LLVMContextRef ctx = p->module->ctx; LLVMTypeRef src_type = LLVMTypeOf(val); @@ -2445,7 +2445,7 @@ general_end:; -LLVMValueRef lb_find_or_add_entity_string_ptr(lbModule *m, String const &str) { +gb_internal LLVMValueRef lb_find_or_add_entity_string_ptr(lbModule *m, String const &str) { StringHashKey key = string_hash_string(str); LLVMValueRef *found = string_map_get(&m->const_strings, key); if (found != nullptr) { @@ -2477,7 +2477,7 @@ LLVMValueRef lb_find_or_add_entity_string_ptr(lbModule *m, String const &str) { } } -lbValue lb_find_or_add_entity_string(lbModule *m, String const &str) { +gb_internal lbValue lb_find_or_add_entity_string(lbModule *m, String const &str) { LLVMValueRef ptr = nullptr; if (str.len != 0) { ptr = lb_find_or_add_entity_string_ptr(m, str); @@ -2493,7 +2493,7 @@ lbValue lb_find_or_add_entity_string(lbModule *m, String const &str) { return res; } -lbValue lb_find_or_add_entity_string_byte_slice(lbModule *m, String const &str) { +gb_internal lbValue lb_find_or_add_entity_string_byte_slice(lbModule *m, String const &str) { LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)}; LLVMValueRef data = LLVMConstStringInContext(m->ctx, cast(char const *)str.text, @@ -2529,7 +2529,7 @@ lbValue lb_find_or_add_entity_string_byte_slice(lbModule *m, String const &str) res.type = t_u8_slice; return res; } -lbValue lb_find_or_add_entity_string_byte_slice_with_type(lbModule *m, String const &str, Type *slice_type) { +gb_internal lbValue lb_find_or_add_entity_string_byte_slice_with_type(lbModule *m, String const &str, Type *slice_type) { GB_ASSERT(is_type_slice(slice_type)); LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)}; LLVMValueRef data = LLVMConstStringInContext(m->ctx, @@ -2579,7 +2579,7 @@ lbValue lb_find_or_add_entity_string_byte_slice_with_type(lbModule *m, String co -lbValue lb_find_ident(lbProcedure *p, lbModule *m, Entity *e, Ast *expr) { +gb_internal lbValue lb_find_ident(lbProcedure *p, lbModule *m, Entity *e, Ast *expr) { if (e->flags & EntityFlag_Param) { // NOTE(bill): Bypass the stack copied variable for // direct parameters as there is no need for the direct load @@ -2633,7 +2633,7 @@ lbValue lb_find_ident(lbProcedure *p, lbModule *m, Entity *e, Ast *expr) { } -lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) { +gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) { GB_ASSERT(is_type_proc(e->type)); e = strip_entity_wrapping(e); GB_ASSERT(e != nullptr); @@ -2668,7 +2668,7 @@ lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) { } -lbAddr lb_add_global_generated(lbModule *m, Type *type, lbValue value, Entity **entity_) { +gb_internal lbAddr lb_add_global_generated(lbModule *m, Type *type, lbValue value, Entity **entity_) { GB_ASSERT(type != nullptr); type = default_type(type); @@ -2700,23 +2700,23 @@ lbAddr lb_add_global_generated(lbModule *m, Type *type, lbValue value, Entity ** return lb_addr(g); } -lbValue lb_find_runtime_value(lbModule *m, String const &name) { +gb_internal lbValue lb_find_runtime_value(lbModule *m, String const &name) { AstPackage *p = m->info->runtime_package; Entity *e = scope_lookup_current(p->scope, name); return lb_find_value_from_entity(m, e); } -lbValue lb_find_package_value(lbModule *m, String const &pkg, String const &name) { +gb_internal lbValue lb_find_package_value(lbModule *m, String const &pkg, String const &name) { Entity *e = find_entity_in_pkg(m->info, pkg, name); return lb_find_value_from_entity(m, e); } -lbValue lb_generate_local_array(lbProcedure *p, Type *elem_type, i64 count, bool zero_init) { +gb_internal lbValue lb_generate_local_array(lbProcedure *p, Type *elem_type, i64 count, bool zero_init) { lbAddr addr = lb_add_local_generated(p, alloc_type_array(elem_type, count), zero_init); return lb_addr_get_ptr(p, addr); } -lbValue lb_find_value_from_entity(lbModule *m, Entity *e) { +gb_internal lbValue lb_find_value_from_entity(lbModule *m, Entity *e) { e = strip_entity_wrapping(e); GB_ASSERT(e != nullptr); @@ -2788,7 +2788,7 @@ lbValue lb_find_value_from_entity(lbModule *m, Entity *e) { return {}; } -lbValue lb_generate_global_array(lbModule *m, Type *elem_type, i64 count, String prefix, i64 id) { +gb_internal lbValue lb_generate_global_array(lbModule *m, Type *elem_type, i64 count, String prefix, i64 id) { Token token = {Token_Ident}; isize name_len = prefix.len + 1 + 20; @@ -2813,7 +2813,7 @@ lbValue lb_generate_global_array(lbModule *m, Type *elem_type, i64 count, String -lbValue lb_build_cond(lbProcedure *p, Ast *cond, lbBlock *true_block, lbBlock *false_block) { +gb_internal lbValue lb_build_cond(lbProcedure *p, Ast *cond, lbBlock *true_block, lbBlock *false_block) { GB_ASSERT(cond != nullptr); GB_ASSERT(true_block != nullptr); GB_ASSERT(false_block != nullptr); @@ -2868,7 +2868,7 @@ lbValue lb_build_cond(lbProcedure *p, Ast *cond, lbBlock *true_block, lbBlock *f } -lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e, bool zero_init, bool force_no_init) { +gb_internal lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e, bool zero_init, bool force_no_init) { GB_ASSERT(p->decl_block != p->curr_block); LLVMPositionBuilderAtEnd(p->builder, p->decl_block->block); @@ -2917,18 +2917,18 @@ lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e, bool zero_init, bool return lb_addr(val); } -lbAddr lb_add_local_generated(lbProcedure *p, Type *type, bool zero_init) { +gb_internal lbAddr lb_add_local_generated(lbProcedure *p, Type *type, bool zero_init) { return lb_add_local(p, type, nullptr, zero_init); } -lbAddr lb_add_local_generated_temp(lbProcedure *p, Type *type, i64 min_alignment) { +gb_internal lbAddr lb_add_local_generated_temp(lbProcedure *p, Type *type, i64 min_alignment) { lbAddr res = lb_add_local(p, type, nullptr, false, true); lb_try_update_alignment(res.addr, cast(unsigned)min_alignment); return res; } -void lb_set_linkage_from_entity_flags(lbModule *m, LLVMValueRef value, u64 flags) { +gb_internal void lb_set_linkage_from_entity_flags(lbModule *m, LLVMValueRef value, u64 flags) { if (flags & EntityFlag_CustomLinkage_Internal) { LLVMSetLinkage(value, LLVMInternalLinkage); } else if (flags & EntityFlag_CustomLinkage_Strong) { diff --git a/src/llvm_backend_opt.cpp b/src/llvm_backend_opt.cpp index e2f51b868..533264e62 100644 --- a/src/llvm_backend_opt.cpp +++ b/src/llvm_backend_opt.cpp @@ -32,12 +32,12 @@ **************************************************************************/ -void lb_populate_function_pass_manager(lbModule *m, LLVMPassManagerRef fpm, bool ignore_memcpy_pass, i32 optimization_level); -void lb_add_function_simplifcation_passes(LLVMPassManagerRef mpm, i32 optimization_level); -void lb_populate_module_pass_manager(LLVMTargetMachineRef target_machine, LLVMPassManagerRef mpm, i32 optimization_level); -void lb_populate_function_pass_manager_specific(lbModule *m, LLVMPassManagerRef fpm, i32 optimization_level); +gb_internal void lb_populate_function_pass_manager(lbModule *m, LLVMPassManagerRef fpm, bool ignore_memcpy_pass, i32 optimization_level); +gb_internal void lb_add_function_simplifcation_passes(LLVMPassManagerRef mpm, i32 optimization_level); +gb_internal void lb_populate_module_pass_manager(LLVMTargetMachineRef target_machine, LLVMPassManagerRef mpm, i32 optimization_level); +gb_internal void lb_populate_function_pass_manager_specific(lbModule *m, LLVMPassManagerRef fpm, i32 optimization_level); -LLVMBool lb_must_preserve_predicate_callback(LLVMValueRef value, void *user_data) { +gb_internal LLVMBool lb_must_preserve_predicate_callback(LLVMValueRef value, void *user_data) { lbModule *m = cast(lbModule *)user_data; if (m == nullptr) { return false; @@ -55,7 +55,7 @@ LLVMBool lb_must_preserve_predicate_callback(LLVMValueRef value, void *user_data #define LLVM_ADD_CONSTANT_VALUE_PASS(fpm) #endif -void lb_basic_populate_function_pass_manager(LLVMPassManagerRef fpm, i32 optimization_level) { +gb_internal void lb_basic_populate_function_pass_manager(LLVMPassManagerRef fpm, i32 optimization_level) { if (false && optimization_level == 0 && build_context.ODIN_DEBUG) { LLVMAddMergedLoadStoreMotionPass(fpm); } else { @@ -68,7 +68,7 @@ void lb_basic_populate_function_pass_manager(LLVMPassManagerRef fpm, i32 optimiz } } -void lb_populate_function_pass_manager(lbModule *m, LLVMPassManagerRef fpm, bool ignore_memcpy_pass, i32 optimization_level) { +gb_internal void lb_populate_function_pass_manager(lbModule *m, LLVMPassManagerRef fpm, bool ignore_memcpy_pass, i32 optimization_level) { // NOTE(bill): Treat -opt:3 as if it was -opt:2 // TODO(bill): Determine which opt definitions should exist in the first place optimization_level = gb_clamp(optimization_level, 0, 2); @@ -102,7 +102,7 @@ void lb_populate_function_pass_manager(lbModule *m, LLVMPassManagerRef fpm, bool #endif } -void lb_populate_function_pass_manager_specific(lbModule *m, LLVMPassManagerRef fpm, i32 optimization_level) { +gb_internal void lb_populate_function_pass_manager_specific(lbModule *m, LLVMPassManagerRef fpm, i32 optimization_level) { // NOTE(bill): Treat -opt:3 as if it was -opt:2 // TODO(bill): Determine which opt definitions should exist in the first place optimization_level = gb_clamp(optimization_level, 0, 2); @@ -141,7 +141,7 @@ void lb_populate_function_pass_manager_specific(lbModule *m, LLVMPassManagerRef #endif } -void lb_add_function_simplifcation_passes(LLVMPassManagerRef mpm, i32 optimization_level) { +gb_internal void lb_add_function_simplifcation_passes(LLVMPassManagerRef mpm, i32 optimization_level) { LLVMAddCFGSimplificationPass(mpm); LLVMAddJumpThreadingPass(mpm); @@ -177,7 +177,7 @@ void lb_add_function_simplifcation_passes(LLVMPassManagerRef mpm, i32 optimizati } -void lb_populate_module_pass_manager(LLVMTargetMachineRef target_machine, LLVMPassManagerRef mpm, i32 optimization_level) { +gb_internal void lb_populate_module_pass_manager(LLVMTargetMachineRef target_machine, LLVMPassManagerRef mpm, i32 optimization_level) { // NOTE(bill): Treat -opt:3 as if it was -opt:2 // TODO(bill): Determine which opt definitions should exist in the first place @@ -266,7 +266,7 @@ void lb_populate_module_pass_manager(LLVMTargetMachineRef target_machine, LLVMPa optimization of Odin programs **************************************************************************/ -void lb_run_remove_dead_instruction_pass(lbProcedure *p) { +gb_internal void lb_run_remove_dead_instruction_pass(lbProcedure *p) { isize removal_count = 0; isize pass_count = 0; isize const max_pass_count = 10; @@ -358,7 +358,7 @@ void lb_run_remove_dead_instruction_pass(lbProcedure *p) { } -void lb_run_function_pass_manager(LLVMPassManagerRef fpm, lbProcedure *p) { +gb_internal void lb_run_function_pass_manager(LLVMPassManagerRef fpm, lbProcedure *p) { LLVMRunFunctionPassManager(fpm, p->value); // NOTE(bill): LLVMAddDCEPass doesn't seem to be exported in the official DLL's for LLVM // which means we cannot rely upon it @@ -367,7 +367,7 @@ void lb_run_function_pass_manager(LLVMPassManagerRef fpm, lbProcedure *p) { lb_run_remove_dead_instruction_pass(p); } -void llvm_delete_function(LLVMValueRef func) { +gb_internal void llvm_delete_function(LLVMValueRef func) { // for (LLVMBasicBlockRef block = LLVMGetFirstBasicBlock(func); block != nullptr; /**/) { // LLVMBasicBlockRef curr_block = block; // block = LLVMGetNextBasicBlock(block); @@ -382,7 +382,7 @@ void llvm_delete_function(LLVMValueRef func) { LLVMDeleteFunction(func); } -void lb_append_to_compiler_used(lbModule *m, LLVMValueRef func) { +gb_internal void lb_append_to_compiler_used(lbModule *m, LLVMValueRef func) { LLVMValueRef global = LLVMGetNamedGlobal(m->mod, "llvm.compiler.used"); LLVMValueRef *constants; @@ -419,7 +419,7 @@ void lb_append_to_compiler_used(lbModule *m, LLVMValueRef func) { LLVMSetInitializer(global, initializer); } -void lb_run_remove_unused_function_pass(lbModule *m) { +gb_internal void lb_run_remove_unused_function_pass(lbModule *m) { isize removal_count = 0; isize pass_count = 0; isize const max_pass_count = 10; @@ -470,7 +470,7 @@ void lb_run_remove_unused_function_pass(lbModule *m) { } -void lb_run_remove_unused_globals_pass(lbModule *m) { +gb_internal void lb_run_remove_unused_globals_pass(lbModule *m) { isize removal_count = 0; isize pass_count = 0; isize const max_pass_count = 10; diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 597efb0ba..0789fb2c1 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -1,5 +1,4 @@ -LLVMValueRef lb_call_intrinsic(lbProcedure *p, const char *name, LLVMValueRef* args, unsigned arg_count, LLVMTypeRef* types, unsigned type_count) -{ +gb_internal LLVMValueRef lb_call_intrinsic(lbProcedure *p, const char *name, LLVMValueRef* args, unsigned arg_count, LLVMTypeRef* types, unsigned type_count) { unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); GB_ASSERT_MSG(id != 0, "Unable to find %s", name); LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, type_count); @@ -7,7 +6,7 @@ LLVMValueRef lb_call_intrinsic(lbProcedure *p, const char *name, LLVMValueRef* a return LLVMBuildCall2(p->builder, call_type, ip, args, arg_count, ""); } -void lb_mem_copy_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile) { +gb_internal void lb_mem_copy_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile) { dst = lb_emit_conv(p, dst, t_rawptr); src = lb_emit_conv(p, src, t_rawptr); len = lb_emit_conv(p, len, t_int); @@ -36,7 +35,7 @@ void lb_mem_copy_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue l -void lb_mem_copy_non_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile) { +gb_internal void lb_mem_copy_non_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile) { dst = lb_emit_conv(p, dst, t_rawptr); src = lb_emit_conv(p, src, t_rawptr); len = lb_emit_conv(p, len, t_int); @@ -65,7 +64,7 @@ void lb_mem_copy_non_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbVal } -lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool ignore_body) { +gb_internal lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool ignore_body) { GB_ASSERT(entity != nullptr); GB_ASSERT(entity->kind == Entity_Procedure); if (!entity->Procedure.is_foreign) { @@ -320,7 +319,7 @@ lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool ignore_body) return p; } -lbProcedure *lb_create_dummy_procedure(lbModule *m, String link_name, Type *type) { +gb_internal lbProcedure *lb_create_dummy_procedure(lbModule *m, String link_name, Type *type) { { lbValue *found = string_map_get(&m->members, link_name); GB_ASSERT_MSG(found == nullptr, "failed to create dummy procedure for: %.*s", LIT(link_name)); @@ -384,7 +383,7 @@ lbProcedure *lb_create_dummy_procedure(lbModule *m, String link_name, Type *type } -lbValue lb_value_param(lbProcedure *p, Entity *e, Type *abi_type, i32 index, lbParamPasskind *kind_) { +gb_internal lbValue lb_value_param(lbProcedure *p, Entity *e, Type *abi_type, i32 index, lbParamPasskind *kind_) { lbParamPasskind kind = lbParamPass_Value; if (e != nullptr && !are_types_identical(abi_type, e->type)) { @@ -427,7 +426,7 @@ lbValue lb_value_param(lbProcedure *p, Entity *e, Type *abi_type, i32 index, lbP -void lb_start_block(lbProcedure *p, lbBlock *b) { +gb_internal void lb_start_block(lbProcedure *p, lbBlock *b) { GB_ASSERT(b != nullptr); if (!b->appended) { b->appended = true; @@ -437,7 +436,7 @@ void lb_start_block(lbProcedure *p, lbBlock *b) { p->curr_block = b; } -void lb_set_debug_position_to_procedure_begin(lbProcedure *p) { +gb_internal void lb_set_debug_position_to_procedure_begin(lbProcedure *p) { if (p->debug_info == nullptr) { return; } @@ -454,7 +453,7 @@ void lb_set_debug_position_to_procedure_begin(lbProcedure *p) { } } -void lb_set_debug_position_to_procedure_end(lbProcedure *p) { +gb_internal void lb_set_debug_position_to_procedure_end(lbProcedure *p) { if (p->debug_info == nullptr) { return; } @@ -471,7 +470,7 @@ void lb_set_debug_position_to_procedure_end(lbProcedure *p) { } } -void lb_begin_procedure_body(lbProcedure *p) { +gb_internal void lb_begin_procedure_body(lbProcedure *p) { DeclInfo *decl = decl_info_of_entity(p->entity); if (decl != nullptr) { for_array(i, decl->labels) { @@ -686,7 +685,7 @@ void lb_begin_procedure_body(lbProcedure *p) { lb_start_block(p, p->entry_block); } -void lb_end_procedure_body(lbProcedure *p) { +gb_internal void lb_end_procedure_body(lbProcedure *p) { lb_set_debug_position_to_procedure_begin(p); LLVMPositionBuilderAtEnd(p->builder, p->decl_block->block); @@ -720,11 +719,11 @@ void lb_end_procedure_body(lbProcedure *p) { p->curr_block = nullptr; p->state_flags = 0; } -void lb_end_procedure(lbProcedure *p) { +gb_internal void lb_end_procedure(lbProcedure *p) { LLVMDisposeBuilder(p->builder); } -void lb_build_nested_proc(lbProcedure *p, AstProcLit *pd, Entity *e) { +gb_internal void lb_build_nested_proc(lbProcedure *p, AstProcLit *pd, Entity *e) { GB_ASSERT(pd->body != nullptr); lbModule *m = p->module; auto *min_dep_set = &m->info->minimum_dependency_set; @@ -766,7 +765,7 @@ void lb_build_nested_proc(lbProcedure *p, AstProcLit *pd, Entity *e) { -Array lb_value_to_array(lbProcedure *p, lbValue value) { +gb_internal Array lb_value_to_array(lbProcedure *p, lbValue value) { Array array = {}; Type *t = base_type(value.type); if (t == nullptr) { @@ -783,7 +782,7 @@ Array lb_value_to_array(lbProcedure *p, lbValue value) { -lbValue lb_emit_call_internal(lbProcedure *p, lbValue value, lbValue return_ptr, Array const &processed_args, Type *abi_rt, lbAddr context_ptr, ProcInlining inlining) { +gb_internal lbValue lb_emit_call_internal(lbProcedure *p, lbValue value, lbValue return_ptr, Array const &processed_args, Type *abi_rt, lbAddr context_ptr, ProcInlining inlining) { GB_ASSERT(p->module->ctx == LLVMGetTypeContext(LLVMTypeOf(value.value))); unsigned arg_count = cast(unsigned)processed_args.count; @@ -892,20 +891,20 @@ lbValue lb_emit_call_internal(lbProcedure *p, lbValue value, lbValue return_ptr, } -lbValue lb_lookup_runtime_procedure(lbModule *m, String const &name) { +gb_internal lbValue lb_lookup_runtime_procedure(lbModule *m, String const &name) { AstPackage *pkg = m->info->runtime_package; Entity *e = scope_lookup_current(pkg->scope, name); return lb_find_procedure_value_from_entity(m, e); } -lbValue lb_emit_runtime_call(lbProcedure *p, char const *c_name, Array const &args) { +gb_internal lbValue lb_emit_runtime_call(lbProcedure *p, char const *c_name, Array const &args) { String name = make_string_c(c_name); lbValue proc = lb_lookup_runtime_procedure(p->module, name); return lb_emit_call(p, proc, args); } -lbValue lb_emit_conjugate(lbProcedure *p, lbValue val, Type *type) { +gb_internal lbValue lb_emit_conjugate(lbProcedure *p, lbValue val, Type *type) { lbValue res = {}; Type *t = val.type; if (is_type_complex(t)) { @@ -956,7 +955,7 @@ lbValue lb_emit_conjugate(lbProcedure *p, lbValue val, Type *type) { return lb_emit_load(p, res); } -lbValue lb_emit_call(lbProcedure *p, lbValue value, Array const &args, ProcInlining inlining) { +gb_internal lbValue lb_emit_call(lbProcedure *p, lbValue value, Array const &args, ProcInlining inlining) { lbModule *m = p->module; Type *pt = base_type(value.type); @@ -1166,7 +1165,7 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array const &args, return result; } -LLVMValueRef llvm_splat_float(i64 count, LLVMTypeRef type, f64 value) { +gb_internal LLVMValueRef llvm_splat_float(i64 count, LLVMTypeRef type, f64 value) { LLVMValueRef v = LLVMConstReal(type, value); LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, count); for (i64 i = 0; i < count; i++) { @@ -1174,7 +1173,7 @@ LLVMValueRef llvm_splat_float(i64 count, LLVMTypeRef type, f64 value) { } return LLVMConstVector(values, cast(unsigned)count); } -LLVMValueRef llvm_splat_int(i64 count, LLVMTypeRef type, i64 value, bool is_signed=false) { +gb_internal LLVMValueRef llvm_splat_int(i64 count, LLVMTypeRef type, i64 value, bool is_signed=false) { LLVMValueRef v = LLVMConstInt(type, value, is_signed); LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, count); for (i64 i = 0; i < count; i++) { @@ -1184,7 +1183,7 @@ LLVMValueRef llvm_splat_int(i64 count, LLVMTypeRef type, i64 value, bool is_sign } -lbValue lb_build_builtin_simd_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, BuiltinProcId builtin_id) { +gb_internal lbValue lb_build_builtin_simd_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, BuiltinProcId builtin_id) { ast_node(ce, CallExpr, expr); lbModule *m = p->module; @@ -1600,7 +1599,7 @@ lbValue lb_build_builtin_simd_proc(lbProcedure *p, Ast *expr, TypeAndValue const } -lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, BuiltinProcId id) { +gb_internal lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, BuiltinProcId id) { ast_node(ce, CallExpr, expr); if (BuiltinProc__simd_begin < id && id < BuiltinProc__simd_end) { @@ -2980,7 +2979,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, } -lbValue lb_handle_param_value(lbProcedure *p, Type *parameter_type, ParameterValue const ¶m_value, TokenPos const &pos) { +gb_internal lbValue lb_handle_param_value(lbProcedure *p, Type *parameter_type, ParameterValue const ¶m_value, TokenPos const &pos) { switch (param_value.kind) { case ParameterValue_Constant: if (is_type_constant_type(parameter_type)) { @@ -3015,9 +3014,9 @@ lbValue lb_handle_param_value(lbProcedure *p, Type *parameter_type, ParameterVal } -lbValue lb_build_call_expr_internal(lbProcedure *p, Ast *expr); +gb_internal lbValue lb_build_call_expr_internal(lbProcedure *p, Ast *expr); -lbValue lb_build_call_expr(lbProcedure *p, Ast *expr) { +gb_internal lbValue lb_build_call_expr(lbProcedure *p, Ast *expr) { expr = unparen_expr(expr); ast_node(ce, CallExpr, expr); @@ -3030,7 +3029,7 @@ lbValue lb_build_call_expr(lbProcedure *p, Ast *expr) { } return res; } -lbValue lb_build_call_expr_internal(lbProcedure *p, Ast *expr) { +gb_internal lbValue lb_build_call_expr_internal(lbProcedure *p, Ast *expr) { lbModule *m = p->module; TypeAndValue tv = type_and_value_of_expr(expr); diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 46144aa00..66c422071 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -1,4 +1,4 @@ -void lb_build_constant_value_decl(lbProcedure *p, AstValueDecl *vd) { +gb_internal void lb_build_constant_value_decl(lbProcedure *p, AstValueDecl *vd) { if (vd == nullptr || vd->is_mutable) { return; } @@ -105,7 +105,7 @@ void lb_build_constant_value_decl(lbProcedure *p, AstValueDecl *vd) { } -void lb_build_stmt_list(lbProcedure *p, Slice const &stmts) { +gb_internal void lb_build_stmt_list(lbProcedure *p, Slice const &stmts) { for_array(i, stmts) { Ast *stmt = stmts[i]; switch (stmt->kind) { @@ -125,7 +125,7 @@ void lb_build_stmt_list(lbProcedure *p, Slice const &stmts) { -lbBranchBlocks lb_lookup_branch_blocks(lbProcedure *p, Ast *ident) { +gb_internal lbBranchBlocks lb_lookup_branch_blocks(lbProcedure *p, Ast *ident) { GB_ASSERT(ident->kind == Ast_Ident); Entity *e = entity_of_node(ident); GB_ASSERT(e->kind == Entity_Label); @@ -142,7 +142,7 @@ lbBranchBlocks lb_lookup_branch_blocks(lbProcedure *p, Ast *ident) { } -lbTargetList *lb_push_target_list(lbProcedure *p, Ast *label, lbBlock *break_, lbBlock *continue_, lbBlock *fallthrough_) { +gb_internal lbTargetList *lb_push_target_list(lbProcedure *p, Ast *label, lbBlock *break_, lbBlock *continue_, lbBlock *fallthrough_) { lbTargetList *tl = gb_alloc_item(permanent_allocator(), lbTargetList); tl->prev = p->target_list; tl->break_ = break_; @@ -170,11 +170,11 @@ lbTargetList *lb_push_target_list(lbProcedure *p, Ast *label, lbBlock *break_, l return tl; } -void lb_pop_target_list(lbProcedure *p) { +gb_internal void lb_pop_target_list(lbProcedure *p) { p->target_list = p->target_list->prev; } -void lb_open_scope(lbProcedure *p, Scope *s) { +gb_internal void lb_open_scope(lbProcedure *p, Scope *s) { lbModule *m = p->module; if (m->debug_builder) { LLVMMetadataRef curr_metadata = lb_get_llvm_metadata(m, s); @@ -211,7 +211,7 @@ void lb_open_scope(lbProcedure *p, Scope *s) { } -void lb_close_scope(lbProcedure *p, lbDeferExitKind kind, lbBlock *block, bool pop_stack=true) { +gb_internal void lb_close_scope(lbProcedure *p, lbDeferExitKind kind, lbBlock *block, bool pop_stack=true) { lb_emit_defer_stmts(p, kind, block); GB_ASSERT(p->scope_index > 0); @@ -230,7 +230,7 @@ void lb_close_scope(lbProcedure *p, lbDeferExitKind kind, lbBlock *block, bool p array_pop(&p->scope_stack); } -void lb_build_when_stmt(lbProcedure *p, AstWhenStmt *ws) { +gb_internal void lb_build_when_stmt(lbProcedure *p, AstWhenStmt *ws) { TypeAndValue tv = type_and_value_of_expr(ws->cond); GB_ASSERT(is_type_boolean(tv.type)); GB_ASSERT(tv.value.kind == ExactValue_Bool); @@ -253,8 +253,8 @@ void lb_build_when_stmt(lbProcedure *p, AstWhenStmt *ws) { -void lb_build_range_indexed(lbProcedure *p, lbValue expr, Type *val_type, lbValue count_ptr, - lbValue *val_, lbValue *idx_, lbBlock **loop_, lbBlock **done_) { +gb_internal void lb_build_range_indexed(lbProcedure *p, lbValue expr, Type *val_type, lbValue count_ptr, + lbValue *val_, lbValue *idx_, lbBlock **loop_, lbBlock **done_) { lbModule *m = p->module; lbValue count = {}; @@ -342,7 +342,7 @@ void lb_build_range_indexed(lbProcedure *p, lbValue expr, Type *val_type, lbValu if (done_) *done_ = done; } -lbValue lb_map_cell_index_static(lbProcedure *p, Type *type, lbValue cells_ptr, lbValue index) { +gb_internal lbValue lb_map_cell_index_static(lbProcedure *p, Type *type, lbValue cells_ptr, lbValue index) { i64 size, len; i64 elem_sz = type_size_of(type); map_cell_size_and_len(type, &size, &len); @@ -378,7 +378,7 @@ lbValue lb_map_cell_index_static(lbProcedure *p, Type *type, lbValue cells_ptr, return lb_emit_ptr_offset(p, elems_ptr, data_index); } -void lb_map_kvh_data_static(lbProcedure *p, lbValue map_value, lbValue *ks_, lbValue *vs_, lbValue *hs_) { +gb_internal void lb_map_kvh_data_static(lbProcedure *p, lbValue map_value, lbValue *ks_, lbValue *vs_, lbValue *hs_) { lbValue capacity = lb_map_cap(p, map_value); lbValue ks = lb_map_data_uintptr(p, map_value); lbValue vs = {}; @@ -388,7 +388,7 @@ void lb_map_kvh_data_static(lbProcedure *p, lbValue map_value, lbValue *ks_, lbV if (hs_) *hs_ = hs; } -lbValue lb_map_hash_is_valid(lbProcedure *p, lbValue hash) { +gb_internal lbValue lb_map_hash_is_valid(lbProcedure *p, lbValue hash) { // N :: size_of(uintptr)*8 - 1 // (hash != 0) & (hash>>N == 0) @@ -404,8 +404,8 @@ lbValue lb_map_hash_is_valid(lbProcedure *p, lbValue hash) { return lb_emit_arith(p, Token_And, not_deleted, not_empty, t_uintptr); } -void lb_build_range_map(lbProcedure *p, lbValue expr, Type *val_type, - lbValue *val_, lbValue *key_, lbBlock **loop_, lbBlock **done_) { +gb_internal void lb_build_range_map(lbProcedure *p, lbValue expr, Type *val_type, + lbValue *val_, lbValue *key_, lbBlock **loop_, lbBlock **done_) { lbModule *m = p->module; Type *type = base_type(type_deref(expr.type)); @@ -466,8 +466,8 @@ void lb_build_range_map(lbProcedure *p, lbValue expr, Type *val_type, -void lb_build_range_string(lbProcedure *p, lbValue expr, Type *val_type, - lbValue *val_, lbValue *idx_, lbBlock **loop_, lbBlock **done_) { +gb_internal void lb_build_range_string(lbProcedure *p, lbValue expr, Type *val_type, + lbValue *val_, lbValue *idx_, lbBlock **loop_, lbBlock **done_) { lbModule *m = p->module; lbValue count = lb_const_int(m, t_int, 0); Type *expr_type = base_type(expr.type); @@ -526,8 +526,8 @@ void lb_build_range_string(lbProcedure *p, lbValue expr, Type *val_type, } -void lb_build_range_interval(lbProcedure *p, AstBinaryExpr *node, - AstRangeStmt *rs, Scope *scope) { +gb_internal void lb_build_range_interval(lbProcedure *p, AstBinaryExpr *node, + AstRangeStmt *rs, Scope *scope) { bool ADD_EXTRA_WRAPPING_CHECK = true; lbModule *m = p->module; @@ -630,7 +630,7 @@ void lb_build_range_interval(lbProcedure *p, AstBinaryExpr *node, lb_start_block(p, done); } -void lb_build_range_enum(lbProcedure *p, Type *enum_type, Type *val_type, lbValue *val_, lbValue *idx_, lbBlock **loop_, lbBlock **done_) { +gb_internal void lb_build_range_enum(lbProcedure *p, Type *enum_type, Type *val_type, lbValue *val_, lbValue *idx_, lbBlock **loop_, lbBlock **done_) { lbModule *m = p->module; Type *t = enum_type; @@ -683,8 +683,8 @@ void lb_build_range_enum(lbProcedure *p, Type *enum_type, Type *val_type, lbValu if (done_) *done_ = done; } -void lb_build_range_tuple(lbProcedure *p, Ast *expr, Type *val0_type, Type *val1_type, - lbValue *val0_, lbValue *val1_, lbBlock **loop_, lbBlock **done_) { +gb_internal void lb_build_range_tuple(lbProcedure *p, Ast *expr, Type *val0_type, Type *val1_type, + lbValue *val0_, lbValue *val1_, lbBlock **loop_, lbBlock **done_) { lbBlock *loop = lb_create_block(p, "for.tuple.loop"); lb_emit_jump(p, loop); lb_start_block(p, loop); @@ -709,7 +709,7 @@ void lb_build_range_tuple(lbProcedure *p, Ast *expr, Type *val0_type, Type *val1 if (done_) *done_ = done; } -void lb_build_range_stmt_struct_soa(lbProcedure *p, AstRangeStmt *rs, Scope *scope) { +gb_internal void lb_build_range_stmt_struct_soa(lbProcedure *p, AstRangeStmt *rs, Scope *scope) { Ast *expr = unparen_expr(rs->expr); TypeAndValue tav = type_and_value_of_expr(expr); @@ -778,7 +778,7 @@ void lb_build_range_stmt_struct_soa(lbProcedure *p, AstRangeStmt *rs, Scope *sco } -void lb_build_range_stmt(lbProcedure *p, AstRangeStmt *rs, Scope *scope) { +gb_internal void lb_build_range_stmt(lbProcedure *p, AstRangeStmt *rs, Scope *scope) { Ast *expr = unparen_expr(rs->expr); if (is_ast_range(expr)) { @@ -923,7 +923,7 @@ void lb_build_range_stmt(lbProcedure *p, AstRangeStmt *rs, Scope *scope) { lb_start_block(p, done); } -void lb_build_unroll_range_stmt(lbProcedure *p, AstUnrollRangeStmt *rs, Scope *scope) { +gb_internal void lb_build_unroll_range_stmt(lbProcedure *p, AstUnrollRangeStmt *rs, Scope *scope) { lbModule *m = p->module; lb_open_scope(p, scope); // Open scope here @@ -1089,7 +1089,7 @@ void lb_build_unroll_range_stmt(lbProcedure *p, AstUnrollRangeStmt *rs, Scope *s lb_close_scope(p, lbDeferExit_Default, nullptr); } -bool lb_switch_stmt_can_be_trivial_jump_table(AstSwitchStmt *ss, bool *default_found_) { +gb_internal bool lb_switch_stmt_can_be_trivial_jump_table(AstSwitchStmt *ss, bool *default_found_) { if (ss->tag == nullptr) { return false; } @@ -1138,7 +1138,7 @@ bool lb_switch_stmt_can_be_trivial_jump_table(AstSwitchStmt *ss, bool *default_f } -void lb_build_switch_stmt(lbProcedure *p, AstSwitchStmt *ss, Scope *scope) { +gb_internal void lb_build_switch_stmt(lbProcedure *p, AstSwitchStmt *ss, Scope *scope) { lb_open_scope(p, scope); if (ss->init != nullptr) { @@ -1300,7 +1300,7 @@ void lb_build_switch_stmt(lbProcedure *p, AstSwitchStmt *ss, Scope *scope) { lb_close_scope(p, lbDeferExit_Default, done); } -void lb_store_type_case_implicit(lbProcedure *p, Ast *clause, lbValue value) { +gb_internal void lb_store_type_case_implicit(lbProcedure *p, Ast *clause, lbValue value) { Entity *e = implicit_entity_of_node(clause); GB_ASSERT(e != nullptr); if (e->flags & EntityFlag_Value) { @@ -1315,7 +1315,7 @@ void lb_store_type_case_implicit(lbProcedure *p, Ast *clause, lbValue value) { } } -lbAddr lb_store_range_stmt_val(lbProcedure *p, Ast *stmt_val, lbValue value) { +gb_internal lbAddr lb_store_range_stmt_val(lbProcedure *p, Ast *stmt_val, lbValue value) { Entity *e = entity_of_node(stmt_val); if (e == nullptr) { return {}; @@ -1335,7 +1335,7 @@ lbAddr lb_store_range_stmt_val(lbProcedure *p, Ast *stmt_val, lbValue value) { return addr; } -void lb_type_case_body(lbProcedure *p, Ast *label, Ast *clause, lbBlock *body, lbBlock *done) { +gb_internal void lb_type_case_body(lbProcedure *p, Ast *label, Ast *clause, lbBlock *body, lbBlock *done) { ast_node(cc, CaseClause, clause); lb_push_target_list(p, label, done, nullptr, nullptr); @@ -1346,7 +1346,7 @@ void lb_type_case_body(lbProcedure *p, Ast *label, Ast *clause, lbBlock *body, l lb_emit_jump(p, done); } -void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss) { +gb_internal void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss) { lbModule *m = p->module; lb_open_scope(p, ss->scope); @@ -1480,7 +1480,7 @@ void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss) { } -void lb_build_static_variables(lbProcedure *p, AstValueDecl *vd) { +gb_internal void lb_build_static_variables(lbProcedure *p, AstValueDecl *vd) { for_array(i, vd->names) { lbValue value = {}; if (vd->values.count > 0) { @@ -1543,7 +1543,7 @@ void lb_build_static_variables(lbProcedure *p, AstValueDecl *vd) { lb_add_member(p->module, mangled_name, global_val); } } -void lb_append_tuple_values(lbProcedure *p, Array *dst_values, lbValue src_value) { +gb_internal void lb_append_tuple_values(lbProcedure *p, Array *dst_values, lbValue src_value) { Type *t = src_value.type; if (t->kind == Type_Tuple) { lbTupleFix *tf = map_get(&p->tuple_fix_map, src_value.value); @@ -1563,7 +1563,7 @@ void lb_append_tuple_values(lbProcedure *p, Array *dst_values, lbValue } -void lb_build_assignment(lbProcedure *p, Array &lvals, Slice const &values) { +gb_internal void lb_build_assignment(lbProcedure *p, Array &lvals, Slice const &values) { if (values.count == 0) { return; } @@ -1584,7 +1584,7 @@ void lb_build_assignment(lbProcedure *p, Array &lvals, Slice cons } } -void lb_build_return_stmt_internal(lbProcedure *p, lbValue res) { +gb_internal void lb_build_return_stmt_internal(lbProcedure *p, lbValue res) { lbFunctionType *ft = lb_get_function_type(p->module, p->type); bool return_by_pointer = ft->ret.kind == lbArg_Indirect; bool split_returns = ft->multiple_return_original_type != nullptr; @@ -1621,7 +1621,7 @@ void lb_build_return_stmt_internal(lbProcedure *p, lbValue res) { LLVMBuildRet(p->builder, ret_val); } } -void lb_build_return_stmt(lbProcedure *p, Slice const &return_results) { +gb_internal void lb_build_return_stmt(lbProcedure *p, Slice const &return_results) { lb_ensure_abi_function_type(p->module, p); lbValue res = {}; @@ -1765,7 +1765,7 @@ void lb_build_return_stmt(lbProcedure *p, Slice const &return_results) { lb_build_return_stmt_internal(p, res); } -void lb_build_if_stmt(lbProcedure *p, Ast *node) { +gb_internal void lb_build_if_stmt(lbProcedure *p, Ast *node) { ast_node(is, IfStmt, node); lb_open_scope(p, is->scope); // Scope #1 defer (lb_close_scope(p, lbDeferExit_Default, nullptr)); @@ -1852,7 +1852,7 @@ void lb_build_if_stmt(lbProcedure *p, Ast *node) { lb_start_block(p, done); } -void lb_build_for_stmt(lbProcedure *p, Ast *node) { +gb_internal void lb_build_for_stmt(lbProcedure *p, Ast *node) { ast_node(fs, ForStmt, node); lb_open_scope(p, fs->scope); // Open Scope here @@ -1912,7 +1912,7 @@ void lb_build_for_stmt(lbProcedure *p, Ast *node) { lb_close_scope(p, lbDeferExit_Default, nullptr); } -void lb_build_assign_stmt_array(lbProcedure *p, TokenKind op, lbAddr const &lhs, lbValue const &value) { +gb_internal void lb_build_assign_stmt_array(lbProcedure *p, TokenKind op, lbAddr const &lhs, lbValue const &value) { GB_ASSERT(op != Token_Eq); Type *lhs_type = lb_addr_type(lhs); @@ -2055,7 +2055,7 @@ void lb_build_assign_stmt_array(lbProcedure *p, TokenKind op, lbAddr const &lhs, lb_loop_end(p, loop_data); } } -void lb_build_assign_stmt(lbProcedure *p, AstAssignStmt *as) { +gb_internal void lb_build_assign_stmt(lbProcedure *p, AstAssignStmt *as) { if (as->op.kind == Token_Eq) { auto lvals = array_make(permanent_allocator(), 0, as->lhs.count); @@ -2113,7 +2113,7 @@ void lb_build_assign_stmt(lbProcedure *p, AstAssignStmt *as) { } -void lb_build_stmt(lbProcedure *p, Ast *node) { +gb_internal void lb_build_stmt(lbProcedure *p, Ast *node) { Ast *prev_stmt = p->curr_stmt; defer (p->curr_stmt = prev_stmt); p->curr_stmt = node; @@ -2308,7 +2308,7 @@ void lb_build_stmt(lbProcedure *p, Ast *node) { -void lb_build_defer_stmt(lbProcedure *p, lbDefer const &d) { +gb_internal void lb_build_defer_stmt(lbProcedure *p, lbDefer const &d) { if (p->curr_block == nullptr) { return; } @@ -2337,7 +2337,7 @@ void lb_build_defer_stmt(lbProcedure *p, lbDefer const &d) { } } -void lb_emit_defer_stmts(lbProcedure *p, lbDeferExitKind kind, lbBlock *block) { +gb_internal void lb_emit_defer_stmts(lbProcedure *p, lbDeferExitKind kind, lbBlock *block) { isize count = p->defer_stmts.count; isize i = count; while (i --> 0) { @@ -2364,7 +2364,7 @@ void lb_emit_defer_stmts(lbProcedure *p, lbDeferExitKind kind, lbBlock *block) { } } -void lb_add_defer_node(lbProcedure *p, isize scope_index, Ast *stmt) { +gb_internal void lb_add_defer_node(lbProcedure *p, isize scope_index, Ast *stmt) { Type *pt = base_type(p->type); GB_ASSERT(pt->kind == Type_Proc); if (pt->Proc.calling_convention == ProcCC_Odin) { @@ -2379,7 +2379,7 @@ void lb_add_defer_node(lbProcedure *p, isize scope_index, Ast *stmt) { d->stmt = stmt; } -void lb_add_defer_proc(lbProcedure *p, isize scope_index, lbValue deferred, Array const &result_as_args) { +gb_internal void lb_add_defer_proc(lbProcedure *p, isize scope_index, lbValue deferred, Array const &result_as_args) { Type *pt = base_type(p->type); GB_ASSERT(pt->kind == Type_Proc); if (pt->Proc.calling_convention == ProcCC_Odin) { diff --git a/src/llvm_backend_type.cpp b/src/llvm_backend_type.cpp index 307d9304b..26bb614e6 100644 --- a/src/llvm_backend_type.cpp +++ b/src/llvm_backend_type.cpp @@ -1,4 +1,4 @@ -isize lb_type_info_index(CheckerInfo *info, Type *type, bool err_on_not_found=true) { +gb_internal isize lb_type_info_index(CheckerInfo *info, Type *type, bool err_on_not_found=true) { auto *set = &info->minimum_dependency_type_info_set; isize index = type_info_index(info, type, err_on_not_found); if (index >= 0) { @@ -13,7 +13,7 @@ isize lb_type_info_index(CheckerInfo *info, Type *type, bool err_on_not_found=tr return -1; } -lbValue lb_typeid(lbModule *m, Type *type) { +gb_internal lbValue lb_typeid(lbModule *m, Type *type) { GB_ASSERT(!build_context.disallow_rtti); type = default_type(type); @@ -90,7 +90,7 @@ lbValue lb_typeid(lbModule *m, Type *type) { return res; } -lbValue lb_type_info(lbModule *m, Type *type) { +gb_internal lbValue lb_type_info(lbModule *m, Type *type) { GB_ASSERT(!build_context.disallow_rtti); type = default_type(type); @@ -102,36 +102,36 @@ lbValue lb_type_info(lbModule *m, Type *type) { return lb_emit_array_epi(m, data, index); } -LLVMTypeRef lb_get_procedure_raw_type(lbModule *m, Type *type) { +gb_internal LLVMTypeRef lb_get_procedure_raw_type(lbModule *m, Type *type) { return lb_type_internal_for_procedures_raw(m, type); } -lbValue lb_type_info_member_types_offset(lbProcedure *p, isize count) { +gb_internal lbValue lb_type_info_member_types_offset(lbProcedure *p, isize count) { GB_ASSERT(p->module == &p->module->gen->default_module); lbValue offset = lb_emit_array_epi(p, lb_global_type_info_member_types.addr, lb_global_type_info_member_types_index); lb_global_type_info_member_types_index += cast(i32)count; return offset; } -lbValue lb_type_info_member_names_offset(lbProcedure *p, isize count) { +gb_internal lbValue lb_type_info_member_names_offset(lbProcedure *p, isize count) { GB_ASSERT(p->module == &p->module->gen->default_module); lbValue offset = lb_emit_array_epi(p, lb_global_type_info_member_names.addr, lb_global_type_info_member_names_index); lb_global_type_info_member_names_index += cast(i32)count; return offset; } -lbValue lb_type_info_member_offsets_offset(lbProcedure *p, isize count) { +gb_internal lbValue lb_type_info_member_offsets_offset(lbProcedure *p, isize count) { GB_ASSERT(p->module == &p->module->gen->default_module); lbValue offset = lb_emit_array_epi(p, lb_global_type_info_member_offsets.addr, lb_global_type_info_member_offsets_index); lb_global_type_info_member_offsets_index += cast(i32)count; return offset; } -lbValue lb_type_info_member_usings_offset(lbProcedure *p, isize count) { +gb_internal lbValue lb_type_info_member_usings_offset(lbProcedure *p, isize count) { GB_ASSERT(p->module == &p->module->gen->default_module); lbValue offset = lb_emit_array_epi(p, lb_global_type_info_member_usings.addr, lb_global_type_info_member_usings_index); lb_global_type_info_member_usings_index += cast(i32)count; return offset; } -lbValue lb_type_info_member_tags_offset(lbProcedure *p, isize count) { +gb_internal lbValue lb_type_info_member_tags_offset(lbProcedure *p, isize count) { GB_ASSERT(p->module == &p->module->gen->default_module); lbValue offset = lb_emit_array_epi(p, lb_global_type_info_member_tags.addr, lb_global_type_info_member_tags_index); lb_global_type_info_member_tags_index += cast(i32)count; @@ -139,7 +139,7 @@ lbValue lb_type_info_member_tags_offset(lbProcedure *p, isize count) { } -void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info data +gb_internal void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info data if (build_context.disallow_rtti) { return; } diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp index 3ae9aba8f..94b900278 100644 --- a/src/llvm_backend_utility.cpp +++ b/src/llvm_backend_utility.cpp @@ -1,6 +1,6 @@ -lbValue lb_lookup_runtime_procedure(lbModule *m, String const &name); +gb_internal lbValue lb_lookup_runtime_procedure(lbModule *m, String const &name); -bool lb_is_type_aggregate(Type *t) { +gb_internal bool lb_is_type_aggregate(Type *t) { t = base_type(t); switch (t->kind) { case Type_Basic: @@ -39,7 +39,7 @@ bool lb_is_type_aggregate(Type *t) { return false; } -void lb_emit_unreachable(lbProcedure *p) { +gb_internal void lb_emit_unreachable(lbProcedure *p) { LLVMValueRef instr = LLVMGetLastInstruction(p->curr_block->block); if (instr == nullptr || !lb_is_instr_terminating(instr)) { lb_call_intrinsic(p, "llvm.trap", nullptr, 0, nullptr, 0); @@ -47,7 +47,7 @@ void lb_emit_unreachable(lbProcedure *p) { } } -lbValue lb_correct_endianness(lbProcedure *p, lbValue value) { +gb_internal lbValue lb_correct_endianness(lbProcedure *p, lbValue value) { Type *src = core_type(value.type); GB_ASSERT(is_type_integer(src) || is_type_float(src)); if (is_type_different_to_arch_endianness(src)) { @@ -57,7 +57,7 @@ lbValue lb_correct_endianness(lbProcedure *p, lbValue value) { return value; } -LLVMValueRef lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, LLVMValueRef len, unsigned alignment, bool is_volatile) { +gb_internal LLVMValueRef lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, LLVMValueRef len, unsigned alignment, bool is_volatile) { bool is_inlinable = false; i64 const_len = 0; @@ -103,7 +103,7 @@ LLVMValueRef lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, LLVMValu } -void lb_mem_zero_ptr(lbProcedure *p, LLVMValueRef ptr, Type *type, unsigned alignment) { +gb_internal void lb_mem_zero_ptr(lbProcedure *p, LLVMValueRef ptr, Type *type, unsigned alignment) { LLVMTypeRef llvm_type = lb_type(p->module, type); LLVMTypeKind kind = LLVMGetTypeKind(llvm_type); @@ -123,7 +123,7 @@ void lb_mem_zero_ptr(lbProcedure *p, LLVMValueRef ptr, Type *type, unsigned alig } } -lbValue lb_emit_select(lbProcedure *p, lbValue cond, lbValue x, lbValue y) { +gb_internal lbValue lb_emit_select(lbProcedure *p, lbValue cond, lbValue x, lbValue y) { cond = lb_emit_conv(p, cond, t_llvm_bool); lbValue res = {}; res.value = LLVMBuildSelect(p->builder, cond.value, x.value, y.value, ""); @@ -131,19 +131,19 @@ lbValue lb_emit_select(lbProcedure *p, lbValue cond, lbValue x, lbValue y) { return res; } -lbValue lb_emit_min(lbProcedure *p, Type *t, lbValue x, lbValue y) { +gb_internal lbValue lb_emit_min(lbProcedure *p, Type *t, lbValue x, lbValue y) { x = lb_emit_conv(p, x, t); y = lb_emit_conv(p, y, t); return lb_emit_select(p, lb_emit_comp(p, Token_Lt, x, y), x, y); } -lbValue lb_emit_max(lbProcedure *p, Type *t, lbValue x, lbValue y) { +gb_internal lbValue lb_emit_max(lbProcedure *p, Type *t, lbValue x, lbValue y) { x = lb_emit_conv(p, x, t); y = lb_emit_conv(p, y, t); return lb_emit_select(p, lb_emit_comp(p, Token_Gt, x, y), x, y); } -lbValue lb_emit_clamp(lbProcedure *p, Type *t, lbValue x, lbValue min, lbValue max) { +gb_internal lbValue lb_emit_clamp(lbProcedure *p, Type *t, lbValue x, lbValue min, lbValue max) { lbValue z = {}; z = lb_emit_max(p, t, x, min); z = lb_emit_min(p, t, z, max); @@ -152,7 +152,7 @@ lbValue lb_emit_clamp(lbProcedure *p, Type *t, lbValue x, lbValue min, lbValue m -lbValue lb_emit_string(lbProcedure *p, lbValue str_elem, lbValue str_len) { +gb_internal lbValue lb_emit_string(lbProcedure *p, lbValue str_elem, lbValue str_len) { if (false && lb_is_const(str_elem) && lb_is_const(str_len)) { LLVMValueRef values[2] = { str_elem.value, @@ -171,7 +171,7 @@ lbValue lb_emit_string(lbProcedure *p, lbValue str_elem, lbValue str_len) { } -lbValue lb_emit_transmute(lbProcedure *p, lbValue value, Type *t) { +gb_internal lbValue lb_emit_transmute(lbProcedure *p, lbValue value, Type *t) { Type *src_type = value.type; if (are_types_identical(t, src_type)) { return value; @@ -259,7 +259,7 @@ lbValue lb_emit_transmute(lbProcedure *p, lbValue value, Type *t) { return res; } -lbValue lb_copy_value_to_ptr(lbProcedure *p, lbValue val, Type *new_type, i64 alignment) { +gb_internal lbValue lb_copy_value_to_ptr(lbProcedure *p, lbValue val, Type *new_type, i64 alignment) { i64 type_alignment = type_align_of(new_type); if (alignment < type_alignment) { alignment = type_alignment; @@ -274,7 +274,7 @@ lbValue lb_copy_value_to_ptr(lbProcedure *p, lbValue val, Type *new_type, i64 al } -lbValue lb_soa_zip(lbProcedure *p, AstCallExpr *ce, TypeAndValue const &tv) { +gb_internal lbValue lb_soa_zip(lbProcedure *p, AstCallExpr *ce, TypeAndValue const &tv) { GB_ASSERT(ce->args.count > 0); auto slices = slice_make(temporary_allocator(), ce->args.count); @@ -305,7 +305,7 @@ lbValue lb_soa_zip(lbProcedure *p, AstCallExpr *ce, TypeAndValue const &tv) { return lb_addr_load(p, res); } -lbValue lb_soa_unzip(lbProcedure *p, AstCallExpr *ce, TypeAndValue const &tv) { +gb_internal lbValue lb_soa_unzip(lbProcedure *p, AstCallExpr *ce, TypeAndValue const &tv) { GB_ASSERT(ce->args.count == 1); lbValue arg = lb_build_expr(p, ce->args[0]); @@ -331,7 +331,7 @@ lbValue lb_soa_unzip(lbProcedure *p, AstCallExpr *ce, TypeAndValue const &tv) { return lb_addr_load(p, res); } -void lb_emit_try_lhs_rhs(lbProcedure *p, Ast *arg, TypeAndValue const &tv, lbValue *lhs_, lbValue *rhs_) { +gb_internal void lb_emit_try_lhs_rhs(lbProcedure *p, Ast *arg, TypeAndValue const &tv, lbValue *lhs_, lbValue *rhs_) { lbValue lhs = {}; lbValue rhs = {}; @@ -360,7 +360,7 @@ void lb_emit_try_lhs_rhs(lbProcedure *p, Ast *arg, TypeAndValue const &tv, lbVal } -lbValue lb_emit_try_has_value(lbProcedure *p, lbValue rhs) { +gb_internal lbValue lb_emit_try_has_value(lbProcedure *p, lbValue rhs) { lbValue has_value = {}; if (is_type_boolean(rhs.type)) { has_value = rhs; @@ -373,7 +373,7 @@ lbValue lb_emit_try_has_value(lbProcedure *p, lbValue rhs) { } -lbValue lb_emit_or_else(lbProcedure *p, Ast *arg, Ast *else_expr, TypeAndValue const &tv) { +gb_internal lbValue lb_emit_or_else(lbProcedure *p, Ast *arg, Ast *else_expr, TypeAndValue const &tv) { if (arg->state_flags & StateFlag_DirectiveWasFalse) { return lb_build_expr(p, else_expr); } @@ -435,10 +435,10 @@ lbValue lb_emit_or_else(lbProcedure *p, Ast *arg, Ast *else_expr, TypeAndValue c } } -void lb_build_return_stmt(lbProcedure *p, Slice const &return_results); -void lb_build_return_stmt_internal(lbProcedure *p, lbValue res); +gb_internal void lb_build_return_stmt(lbProcedure *p, Slice const &return_results); +gb_internal void lb_build_return_stmt_internal(lbProcedure *p, lbValue res); -lbValue lb_emit_or_return(lbProcedure *p, Ast *arg, TypeAndValue const &tv) { +gb_internal lbValue lb_emit_or_return(lbProcedure *p, Ast *arg, TypeAndValue const &tv) { lbValue lhs = {}; lbValue rhs = {}; lb_emit_try_lhs_rhs(p, arg, tv, &lhs, &rhs); @@ -479,7 +479,7 @@ lbValue lb_emit_or_return(lbProcedure *p, Ast *arg, TypeAndValue const &tv) { } -void lb_emit_increment(lbProcedure *p, lbValue addr) { +gb_internal void lb_emit_increment(lbProcedure *p, lbValue addr) { GB_ASSERT(is_type_pointer(addr.type)); Type *type = type_deref(addr.type); lbValue v_one = lb_const_value(p->module, type, exact_value_i64(1)); @@ -487,7 +487,7 @@ void lb_emit_increment(lbProcedure *p, lbValue addr) { } -lbValue lb_emit_byte_swap(lbProcedure *p, lbValue value, Type *end_type) { +gb_internal lbValue lb_emit_byte_swap(lbProcedure *p, lbValue value, Type *end_type) { GB_ASSERT(type_size_of(value.type) == type_size_of(end_type)); if (type_size_of(value.type) < 2) { @@ -526,7 +526,7 @@ lbValue lb_emit_byte_swap(lbProcedure *p, lbValue value, Type *end_type) { -lbValue lb_emit_count_ones(lbProcedure *p, lbValue x, Type *type) { +gb_internal lbValue lb_emit_count_ones(lbProcedure *p, lbValue x, Type *type) { x = lb_emit_conv(p, x, type); char const *name = "llvm.ctpop"; @@ -539,7 +539,7 @@ lbValue lb_emit_count_ones(lbProcedure *p, lbValue x, Type *type) { return res; } -lbValue lb_emit_count_zeros(lbProcedure *p, lbValue x, Type *type) { +gb_internal lbValue lb_emit_count_zeros(lbProcedure *p, lbValue x, Type *type) { Type *elem = base_array_type(type); i64 sz = 8*type_size_of(elem); lbValue size = lb_const_int(p->module, elem, cast(u64)sz); @@ -550,7 +550,7 @@ lbValue lb_emit_count_zeros(lbProcedure *p, lbValue x, Type *type) { -lbValue lb_emit_count_trailing_zeros(lbProcedure *p, lbValue x, Type *type) { +gb_internal lbValue lb_emit_count_trailing_zeros(lbProcedure *p, lbValue x, Type *type) { x = lb_emit_conv(p, x, type); char const *name = "llvm.cttz"; @@ -566,7 +566,7 @@ lbValue lb_emit_count_trailing_zeros(lbProcedure *p, lbValue x, Type *type) { return res; } -lbValue lb_emit_count_leading_zeros(lbProcedure *p, lbValue x, Type *type) { +gb_internal lbValue lb_emit_count_leading_zeros(lbProcedure *p, lbValue x, Type *type) { x = lb_emit_conv(p, x, type); char const *name = "llvm.ctlz"; @@ -584,7 +584,7 @@ lbValue lb_emit_count_leading_zeros(lbProcedure *p, lbValue x, Type *type) { -lbValue lb_emit_reverse_bits(lbProcedure *p, lbValue x, Type *type) { +gb_internal lbValue lb_emit_reverse_bits(lbProcedure *p, lbValue x, Type *type) { x = lb_emit_conv(p, x, type); char const *name = "llvm.bitreverse"; @@ -599,7 +599,7 @@ lbValue lb_emit_reverse_bits(lbProcedure *p, lbValue x, Type *type) { } -lbValue lb_emit_bit_set_card(lbProcedure *p, lbValue x) { +gb_internal lbValue lb_emit_bit_set_card(lbProcedure *p, lbValue x) { GB_ASSERT(is_type_bit_set(x.type)); Type *underlying = bit_set_to_int(x.type); lbValue card = lb_emit_count_ones(p, x, underlying); @@ -607,7 +607,7 @@ lbValue lb_emit_bit_set_card(lbProcedure *p, lbValue x) { } -lbValue lb_emit_union_cast_only_ok_check(lbProcedure *p, lbValue value, Type *type, TokenPos pos) { +gb_internal lbValue lb_emit_union_cast_only_ok_check(lbProcedure *p, lbValue value, Type *type, TokenPos pos) { GB_ASSERT(is_type_tuple(type)); lbModule *m = p->module; @@ -654,7 +654,7 @@ lbValue lb_emit_union_cast_only_ok_check(lbProcedure *p, lbValue value, Type *ty return lb_addr_load(p, v); } -lbValue lb_emit_union_cast(lbProcedure *p, lbValue value, Type *type, TokenPos pos) { +gb_internal lbValue lb_emit_union_cast(lbProcedure *p, lbValue value, Type *type, TokenPos pos) { lbModule *m = p->module; Type *src_type = value.type; @@ -753,7 +753,7 @@ lbValue lb_emit_union_cast(lbProcedure *p, lbValue value, Type *type, TokenPos p return lb_addr_load(p, v); } -lbAddr lb_emit_any_cast_addr(lbProcedure *p, lbValue value, Type *type, TokenPos pos) { +gb_internal lbAddr lb_emit_any_cast_addr(lbProcedure *p, lbValue value, Type *type, TokenPos pos) { lbModule *m = p->module; Type *src_type = value.type; @@ -826,13 +826,13 @@ lbAddr lb_emit_any_cast_addr(lbProcedure *p, lbValue value, Type *type, TokenPos } return v; } -lbValue lb_emit_any_cast(lbProcedure *p, lbValue value, Type *type, TokenPos pos) { +gb_internal lbValue lb_emit_any_cast(lbProcedure *p, lbValue value, Type *type, TokenPos pos) { return lb_addr_load(p, lb_emit_any_cast_addr(p, value, type, pos)); } -lbAddr lb_find_or_generate_context_ptr(lbProcedure *p) { +gb_internal lbAddr lb_find_or_generate_context_ptr(lbProcedure *p) { if (p->context_stack.count > 0) { return p->context_stack[p->context_stack.count-1].ctx; } @@ -850,7 +850,7 @@ lbAddr lb_find_or_generate_context_ptr(lbProcedure *p) { return c; } -lbValue lb_address_from_load_or_generate_local(lbProcedure *p, lbValue value) { +gb_internal lbValue lb_address_from_load_or_generate_local(lbProcedure *p, lbValue value) { if (LLVMIsALoadInst(value.value)) { lbValue res = {}; res.value = LLVMGetOperand(value.value, 0); @@ -864,7 +864,7 @@ lbValue lb_address_from_load_or_generate_local(lbProcedure *p, lbValue value) { lb_addr_store(p, res, value); return res.addr; } -lbValue lb_address_from_load(lbProcedure *p, lbValue value) { +gb_internal lbValue lb_address_from_load(lbProcedure *p, lbValue value) { if (LLVMIsALoadInst(value.value)) { lbValue res = {}; res.value = LLVMGetOperand(value.value, 0); @@ -877,7 +877,7 @@ lbValue lb_address_from_load(lbProcedure *p, lbValue value) { } -lbStructFieldRemapping lb_get_struct_remapping(lbModule *m, Type *t) { +gb_internal lbStructFieldRemapping lb_get_struct_remapping(lbModule *m, Type *t) { t = base_type(t); LLVMTypeRef struct_type = lb_type(m, t); auto *field_remapping = map_get(&m->struct_field_remapping, cast(void *)struct_type); @@ -888,7 +888,7 @@ lbStructFieldRemapping lb_get_struct_remapping(lbModule *m, Type *t) { return *field_remapping; } -i32 lb_convert_struct_index(lbModule *m, Type *t, i32 index) { +gb_internal i32 lb_convert_struct_index(lbModule *m, Type *t, i32 index) { if (t->kind == Type_Struct) { auto field_remapping = lb_get_struct_remapping(m, t); index = field_remapping[index]; @@ -896,7 +896,7 @@ i32 lb_convert_struct_index(lbModule *m, Type *t, i32 index) { return index; } -LLVMTypeRef lb_type_padding_filler(lbModule *m, i64 padding, i64 padding_align) { +gb_internal LLVMTypeRef lb_type_padding_filler(lbModule *m, i64 padding, i64 padding_align) { // NOTE(bill): limit to `[N x u64]` to prevent ABI issues padding_align = gb_clamp(padding_align, 1, 8); if (padding % padding_align == 0) { @@ -921,7 +921,7 @@ LLVMTypeRef lb_type_padding_filler(lbModule *m, i64 padding, i64 padding_align) } -char const *llvm_type_kinds[] = { +gb_global char const *llvm_type_kinds[] = { "LLVMVoidTypeKind", "LLVMHalfTypeKind", "LLVMFloatTypeKind", @@ -973,7 +973,7 @@ gb_internal lbValue lb_emit_struct_ep_internal(lbProcedure *p, lbValue s, i32 in } } -lbValue lb_emit_tuple_ep(lbProcedure *p, lbValue ptr, i32 index) { +gb_internal lbValue lb_emit_tuple_ep(lbProcedure *p, lbValue ptr, i32 index) { Type *t = type_deref(ptr.type); GB_ASSERT(is_type_tuple(t)); Type *result_type = t->Tuple.variables[index]->type; @@ -991,7 +991,7 @@ lbValue lb_emit_tuple_ep(lbProcedure *p, lbValue ptr, i32 index) { } -lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index) { +gb_internal lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index) { GB_ASSERT(is_type_pointer(s.type)); Type *t = base_type(type_deref(s.type)); Type *result_type = nullptr; @@ -1074,7 +1074,7 @@ lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index) { return lb_emit_struct_ep_internal(p, s, index, result_type); } -lbValue lb_emit_tuple_ev(lbProcedure *p, lbValue value, i32 index) { +gb_internal lbValue lb_emit_tuple_ev(lbProcedure *p, lbValue value, i32 index) { Type *t = value.type; GB_ASSERT(is_type_tuple(t)); Type *result_type = t->Tuple.variables[index]->type; @@ -1104,7 +1104,7 @@ lbValue lb_emit_tuple_ev(lbProcedure *p, lbValue value, i32 index) { return res; } -lbValue lb_emit_struct_ev(lbProcedure *p, lbValue s, i32 index) { +gb_internal lbValue lb_emit_struct_ev(lbProcedure *p, lbValue s, i32 index) { Type *t = base_type(s.type); if (is_type_tuple(t)) { return lb_emit_tuple_ev(p, s, index); @@ -1223,7 +1223,7 @@ lbValue lb_emit_struct_ev(lbProcedure *p, lbValue s, i32 index) { return res; } -lbValue lb_emit_deep_field_gep(lbProcedure *p, lbValue e, Selection sel) { +gb_internal lbValue lb_emit_deep_field_gep(lbProcedure *p, lbValue e, Selection sel) { GB_ASSERT(sel.index.count > 0); Type *type = type_deref(e.type); @@ -1311,14 +1311,14 @@ lbValue lb_emit_deep_field_gep(lbProcedure *p, lbValue e, Selection sel) { } -lbValue lb_emit_deep_field_ev(lbProcedure *p, lbValue e, Selection sel) { +gb_internal lbValue lb_emit_deep_field_ev(lbProcedure *p, lbValue e, Selection sel) { lbValue ptr = lb_address_from_load_or_generate_local(p, e); lbValue res = lb_emit_deep_field_gep(p, ptr, sel); return lb_emit_load(p, res); } -lbValue lb_emit_array_ep(lbProcedure *p, lbValue s, lbValue index) { +gb_internal lbValue lb_emit_array_ep(lbProcedure *p, lbValue s, lbValue index) { Type *t = s.type; GB_ASSERT_MSG(is_type_pointer(t), "%s", type_to_string(t)); Type *st = base_type(type_deref(t)); @@ -1341,7 +1341,7 @@ lbValue lb_emit_array_ep(lbProcedure *p, lbValue s, lbValue index) { return res; } -lbValue lb_emit_array_epi(lbProcedure *p, lbValue s, isize index) { +gb_internal lbValue lb_emit_array_epi(lbProcedure *p, lbValue s, isize index) { Type *t = s.type; GB_ASSERT(is_type_pointer(t)); Type *st = base_type(type_deref(t)); @@ -1349,7 +1349,7 @@ lbValue lb_emit_array_epi(lbProcedure *p, lbValue s, isize index) { GB_ASSERT(0 <= index); return lb_emit_epi(p, s, index); } -lbValue lb_emit_array_epi(lbModule *m, lbValue s, isize index) { +gb_internal lbValue lb_emit_array_epi(lbModule *m, lbValue s, isize index) { Type *t = s.type; GB_ASSERT(is_type_pointer(t)); Type *st = base_type(type_deref(t)); @@ -1358,7 +1358,7 @@ lbValue lb_emit_array_epi(lbModule *m, lbValue s, isize index) { return lb_emit_epi(m, s, index); } -lbValue lb_emit_ptr_offset(lbProcedure *p, lbValue ptr, lbValue index) { +gb_internal lbValue lb_emit_ptr_offset(lbProcedure *p, lbValue ptr, lbValue index) { index = lb_emit_conv(p, index, t_int); LLVMValueRef indices[1] = {index.value}; lbValue res = {}; @@ -1373,7 +1373,7 @@ lbValue lb_emit_ptr_offset(lbProcedure *p, lbValue ptr, lbValue index) { return res; } -lbValue lb_emit_matrix_epi(lbProcedure *p, lbValue s, isize row, isize column) { +gb_internal lbValue lb_emit_matrix_epi(lbProcedure *p, lbValue s, isize row, isize column) { Type *t = s.type; GB_ASSERT(is_type_pointer(t)); Type *mt = base_type(type_deref(t)); @@ -1391,7 +1391,7 @@ lbValue lb_emit_matrix_epi(lbProcedure *p, lbValue s, isize row, isize column) { return lb_emit_epi(p, s, offset); } -lbValue lb_emit_matrix_ep(lbProcedure *p, lbValue s, lbValue row, lbValue column) { +gb_internal lbValue lb_emit_matrix_ep(lbProcedure *p, lbValue s, lbValue row, lbValue column) { Type *t = s.type; GB_ASSERT(is_type_pointer(t)); Type *mt = base_type(type_deref(t)); @@ -1423,7 +1423,7 @@ lbValue lb_emit_matrix_ep(lbProcedure *p, lbValue s, lbValue row, lbValue column } -lbValue lb_emit_matrix_ev(lbProcedure *p, lbValue s, isize row, isize column) { +gb_internal lbValue lb_emit_matrix_ev(lbProcedure *p, lbValue s, isize row, isize column) { Type *st = base_type(s.type); GB_ASSERT_MSG(is_type_matrix(st), "%s", type_to_string(st)); @@ -1433,14 +1433,14 @@ lbValue lb_emit_matrix_ev(lbProcedure *p, lbValue s, isize row, isize column) { } -void lb_fill_slice(lbProcedure *p, lbAddr const &slice, lbValue base_elem, lbValue len) { +gb_internal void lb_fill_slice(lbProcedure *p, lbAddr const &slice, lbValue base_elem, lbValue len) { Type *t = lb_addr_type(slice); GB_ASSERT(is_type_slice(t)); lbValue ptr = lb_addr_get_ptr(p, slice); lb_emit_store(p, lb_emit_struct_ep(p, ptr, 0), base_elem); lb_emit_store(p, lb_emit_struct_ep(p, ptr, 1), len); } -void lb_fill_string(lbProcedure *p, lbAddr const &string, lbValue base_elem, lbValue len) { +gb_internal void lb_fill_string(lbProcedure *p, lbAddr const &string, lbValue base_elem, lbValue len) { Type *t = lb_addr_type(string); GB_ASSERT(is_type_string(t)); lbValue ptr = lb_addr_get_ptr(p, string); @@ -1448,18 +1448,18 @@ void lb_fill_string(lbProcedure *p, lbAddr const &string, lbValue base_elem, lbV lb_emit_store(p, lb_emit_struct_ep(p, ptr, 1), len); } -lbValue lb_string_elem(lbProcedure *p, lbValue string) { +gb_internal lbValue lb_string_elem(lbProcedure *p, lbValue string) { Type *t = base_type(string.type); GB_ASSERT(t->kind == Type_Basic && t->Basic.kind == Basic_string); return lb_emit_struct_ev(p, string, 0); } -lbValue lb_string_len(lbProcedure *p, lbValue string) { +gb_internal lbValue lb_string_len(lbProcedure *p, lbValue string) { Type *t = base_type(string.type); GB_ASSERT_MSG(t->kind == Type_Basic && t->Basic.kind == Basic_string, "%s", type_to_string(t)); return lb_emit_struct_ev(p, string, 1); } -lbValue lb_cstring_len(lbProcedure *p, lbValue value) { +gb_internal lbValue lb_cstring_len(lbProcedure *p, lbValue value) { GB_ASSERT(is_type_cstring(value.type)); auto args = array_make(permanent_allocator(), 1); args[0] = lb_emit_conv(p, value, t_cstring); @@ -1467,43 +1467,43 @@ lbValue lb_cstring_len(lbProcedure *p, lbValue value) { } -lbValue lb_array_elem(lbProcedure *p, lbValue array_ptr) { +gb_internal lbValue lb_array_elem(lbProcedure *p, lbValue array_ptr) { Type *t = type_deref(array_ptr.type); GB_ASSERT(is_type_array(t)); return lb_emit_struct_ep(p, array_ptr, 0); } -lbValue lb_slice_elem(lbProcedure *p, lbValue slice) { +gb_internal lbValue lb_slice_elem(lbProcedure *p, lbValue slice) { GB_ASSERT(is_type_slice(slice.type)); return lb_emit_struct_ev(p, slice, 0); } -lbValue lb_slice_len(lbProcedure *p, lbValue slice) { +gb_internal lbValue lb_slice_len(lbProcedure *p, lbValue slice) { GB_ASSERT(is_type_slice(slice.type) || is_type_relative_slice(slice.type)); return lb_emit_struct_ev(p, slice, 1); } -lbValue lb_dynamic_array_elem(lbProcedure *p, lbValue da) { +gb_internal lbValue lb_dynamic_array_elem(lbProcedure *p, lbValue da) { GB_ASSERT(is_type_dynamic_array(da.type)); return lb_emit_struct_ev(p, da, 0); } -lbValue lb_dynamic_array_len(lbProcedure *p, lbValue da) { +gb_internal lbValue lb_dynamic_array_len(lbProcedure *p, lbValue da) { GB_ASSERT(is_type_dynamic_array(da.type)); return lb_emit_struct_ev(p, da, 1); } -lbValue lb_dynamic_array_cap(lbProcedure *p, lbValue da) { +gb_internal lbValue lb_dynamic_array_cap(lbProcedure *p, lbValue da) { GB_ASSERT(is_type_dynamic_array(da.type)); return lb_emit_struct_ev(p, da, 2); } -lbValue lb_dynamic_array_allocator(lbProcedure *p, lbValue da) { +gb_internal lbValue lb_dynamic_array_allocator(lbProcedure *p, lbValue da) { GB_ASSERT(is_type_dynamic_array(da.type)); return lb_emit_struct_ev(p, da, 3); } -lbValue lb_map_len(lbProcedure *p, lbValue value) { +gb_internal lbValue lb_map_len(lbProcedure *p, lbValue value) { GB_ASSERT_MSG(is_type_map(value.type) || are_types_identical(value.type, t_raw_map), "%s", type_to_string(value.type)); lbValue len = lb_emit_struct_ev(p, value, 1); return lb_emit_conv(p, len, t_int); } -lbValue lb_map_len_ptr(lbProcedure *p, lbValue map_ptr) { +gb_internal lbValue lb_map_len_ptr(lbProcedure *p, lbValue map_ptr) { Type *type = map_ptr.type; GB_ASSERT(is_type_pointer(type)); type = type_deref(type); @@ -1511,7 +1511,7 @@ lbValue lb_map_len_ptr(lbProcedure *p, lbValue map_ptr) { return lb_emit_struct_ep(p, map_ptr, 1); } -lbValue lb_map_cap(lbProcedure *p, lbValue value) { +gb_internal lbValue lb_map_cap(lbProcedure *p, lbValue value) { GB_ASSERT_MSG(is_type_map(value.type) || are_types_identical(value.type, t_raw_map), "%s", type_to_string(value.type)); lbValue zero = lb_const_int(p->module, t_uintptr, 0); lbValue one = lb_const_int(p->module, t_uintptr, 1); @@ -1525,7 +1525,7 @@ lbValue lb_map_cap(lbProcedure *p, lbValue value) { return lb_emit_conv(p, lb_emit_select(p, cmp, zero, cap), t_int); } -lbValue lb_map_data_uintptr(lbProcedure *p, lbValue value) { +gb_internal lbValue lb_map_data_uintptr(lbProcedure *p, lbValue value) { GB_ASSERT(is_type_map(value.type) || are_types_identical(value.type, t_raw_map)); lbValue data = lb_emit_struct_ev(p, value, 0); u64 mask_value = 0; @@ -1539,7 +1539,7 @@ lbValue lb_map_data_uintptr(lbProcedure *p, lbValue value) { } -lbValue lb_soa_struct_len(lbProcedure *p, lbValue value) { +gb_internal lbValue lb_soa_struct_len(lbProcedure *p, lbValue value) { Type *t = base_type(value.type); bool is_ptr = false; if (is_type_pointer(t)) { @@ -1572,7 +1572,7 @@ lbValue lb_soa_struct_len(lbProcedure *p, lbValue value) { return lb_emit_struct_ev(p, value, cast(i32)n); } -lbValue lb_soa_struct_cap(lbProcedure *p, lbValue value) { +gb_internal lbValue lb_soa_struct_cap(lbProcedure *p, lbValue value) { Type *t = base_type(value.type); bool is_ptr = false; @@ -1604,7 +1604,7 @@ lbValue lb_soa_struct_cap(lbProcedure *p, lbValue value) { return lb_emit_struct_ev(p, value, cast(i32)n); } -lbValue lb_emit_mul_add(lbProcedure *p, lbValue a, lbValue b, lbValue c, Type *t) { +gb_internal lbValue lb_emit_mul_add(lbProcedure *p, lbValue a, lbValue b, lbValue c, Type *t) { lbModule *m = p->module; a = lb_emit_conv(p, a, t); @@ -1647,7 +1647,7 @@ lbValue lb_emit_mul_add(lbProcedure *p, lbValue a, lbValue b, lbValue c, Type *t } } -LLVMValueRef llvm_mask_iota(lbModule *m, unsigned start, unsigned count) { +gb_internal LLVMValueRef llvm_mask_iota(lbModule *m, unsigned start, unsigned count) { auto iota = slice_make(temporary_allocator(), count); for (unsigned i = 0; i < count; i++) { iota[i] = lb_const_int(m, t_u32, start+i).value; @@ -1655,7 +1655,7 @@ LLVMValueRef llvm_mask_iota(lbModule *m, unsigned start, unsigned count) { return LLVMConstVector(iota.data, count); } -LLVMValueRef llvm_mask_zero(lbModule *m, unsigned count) { +gb_internal LLVMValueRef llvm_mask_zero(lbModule *m, unsigned count) { return LLVMConstNull(LLVMVectorType(lb_type(m, t_u32), count)); } @@ -1663,16 +1663,16 @@ LLVMValueRef llvm_mask_zero(lbModule *m, unsigned count) { // #define LLVM_VECTOR_DUMMY_VALUE(type) LLVMConstNull((type)) -LLVMValueRef llvm_basic_shuffle(lbProcedure *p, LLVMValueRef vector, LLVMValueRef mask) { +gb_internal LLVMValueRef llvm_basic_shuffle(lbProcedure *p, LLVMValueRef vector, LLVMValueRef mask) { return LLVMBuildShuffleVector(p->builder, vector, LLVM_VECTOR_DUMMY_VALUE(LLVMTypeOf(vector)), mask, ""); } -LLVMValueRef llvm_basic_const_shuffle(LLVMValueRef vector, LLVMValueRef mask) { +gb_internal LLVMValueRef llvm_basic_const_shuffle(LLVMValueRef vector, LLVMValueRef mask) { return LLVMConstShuffleVector(vector, LLVM_VECTOR_DUMMY_VALUE(LLVMTypeOf(vector)), mask); } -LLVMValueRef llvm_vector_broadcast(lbProcedure *p, LLVMValueRef value, unsigned count) { +gb_internal LLVMValueRef llvm_vector_broadcast(lbProcedure *p, LLVMValueRef value, unsigned count) { GB_ASSERT(count > 0); if (LLVMIsConstant(value)) { LLVMValueRef single = LLVMConstVector(&value, 1); @@ -1692,7 +1692,7 @@ LLVMValueRef llvm_vector_broadcast(lbProcedure *p, LLVMValueRef value, unsigned return llvm_basic_shuffle(p, single, mask); } -LLVMValueRef llvm_vector_shuffle_reduction(lbProcedure *p, LLVMValueRef value, LLVMOpcode op_code) { +gb_internal LLVMValueRef llvm_vector_shuffle_reduction(lbProcedure *p, LLVMValueRef value, LLVMOpcode op_code) { LLVMTypeRef original_vector_type = LLVMTypeOf(value); GB_ASSERT(LLVMGetTypeKind(original_vector_type) == LLVMVectorTypeKind); @@ -1719,7 +1719,7 @@ LLVMValueRef llvm_vector_shuffle_reduction(lbProcedure *p, LLVMValueRef value, L return LLVMBuildExtractElement(p->builder, value, v_zero32, ""); } -LLVMValueRef llvm_vector_expand_to_power_of_two(lbProcedure *p, LLVMValueRef value) { +gb_internal LLVMValueRef llvm_vector_expand_to_power_of_two(lbProcedure *p, LLVMValueRef value) { LLVMTypeRef vector_type = LLVMTypeOf(value); unsigned len = LLVMGetVectorSize(vector_type); if (len == 1) { @@ -1734,7 +1734,7 @@ LLVMValueRef llvm_vector_expand_to_power_of_two(lbProcedure *p, LLVMValueRef val return LLVMBuildShuffleVector(p->builder, value, LLVMConstNull(vector_type), mask, ""); } -LLVMValueRef llvm_vector_reduce_add(lbProcedure *p, LLVMValueRef value) { +gb_internal LLVMValueRef llvm_vector_reduce_add(lbProcedure *p, LLVMValueRef value) { LLVMTypeRef type = LLVMTypeOf(value); GB_ASSERT(LLVMGetTypeKind(type) == LLVMVectorTypeKind); LLVMTypeRef elem = OdinLLVMGetVectorElementType(type); @@ -1810,7 +1810,7 @@ LLVMValueRef llvm_vector_reduce_add(lbProcedure *p, LLVMValueRef value) { #endif } -LLVMValueRef llvm_vector_add(lbProcedure *p, LLVMValueRef a, LLVMValueRef b) { +gb_internal LLVMValueRef llvm_vector_add(lbProcedure *p, LLVMValueRef a, LLVMValueRef b) { GB_ASSERT(LLVMTypeOf(a) == LLVMTypeOf(b)); LLVMTypeRef elem = OdinLLVMGetVectorElementType(LLVMTypeOf(a)); @@ -1821,7 +1821,7 @@ LLVMValueRef llvm_vector_add(lbProcedure *p, LLVMValueRef a, LLVMValueRef b) { return LLVMBuildFAdd(p->builder, a, b, ""); } -LLVMValueRef llvm_vector_mul(lbProcedure *p, LLVMValueRef a, LLVMValueRef b) { +gb_internal LLVMValueRef llvm_vector_mul(lbProcedure *p, LLVMValueRef a, LLVMValueRef b) { GB_ASSERT(LLVMTypeOf(a) == LLVMTypeOf(b)); LLVMTypeRef elem = OdinLLVMGetVectorElementType(LLVMTypeOf(a)); @@ -1833,11 +1833,11 @@ LLVMValueRef llvm_vector_mul(lbProcedure *p, LLVMValueRef a, LLVMValueRef b) { } -LLVMValueRef llvm_vector_dot(lbProcedure *p, LLVMValueRef a, LLVMValueRef b) { +gb_internal LLVMValueRef llvm_vector_dot(lbProcedure *p, LLVMValueRef a, LLVMValueRef b) { return llvm_vector_reduce_add(p, llvm_vector_mul(p, a, b)); } -LLVMValueRef llvm_vector_mul_add(lbProcedure *p, LLVMValueRef a, LLVMValueRef b, LLVMValueRef c) { +gb_internal LLVMValueRef llvm_vector_mul_add(lbProcedure *p, LLVMValueRef a, LLVMValueRef b, LLVMValueRef c) { LLVMTypeRef t = LLVMTypeOf(a); GB_ASSERT(t == LLVMTypeOf(b)); @@ -1871,7 +1871,7 @@ LLVMValueRef llvm_vector_mul_add(lbProcedure *p, LLVMValueRef a, LLVMValueRef b, } } -LLVMValueRef llvm_get_inline_asm(LLVMTypeRef func_type, String const &str, String const &clobbers, bool has_side_effects=true, bool is_align_stack=false, LLVMInlineAsmDialect dialect=LLVMInlineAsmDialectATT) { +gb_internal LLVMValueRef llvm_get_inline_asm(LLVMTypeRef func_type, String const &str, String const &clobbers, bool has_side_effects=true, bool is_align_stack=false, LLVMInlineAsmDialect dialect=LLVMInlineAsmDialectATT) { return LLVMGetInlineAsm(func_type, cast(char *)str.text, cast(size_t)str.len, cast(char *)clobbers.text, cast(size_t)clobbers.len, @@ -1884,7 +1884,7 @@ LLVMValueRef llvm_get_inline_asm(LLVMTypeRef func_type, String const &str, Strin } -void lb_set_wasm_import_attributes(LLVMValueRef value, Entity *entity, String import_name) { +gb_internal void lb_set_wasm_import_attributes(LLVMValueRef value, Entity *entity, String import_name) { if (!is_arch_wasm()) { return; } @@ -1906,7 +1906,7 @@ void lb_set_wasm_import_attributes(LLVMValueRef value, Entity *entity, String im } -void lb_set_wasm_export_attributes(LLVMValueRef value, String export_name) { +gb_internal void lb_set_wasm_export_attributes(LLVMValueRef value, String export_name) { if (!is_arch_wasm()) { return; } @@ -1918,7 +1918,7 @@ void lb_set_wasm_export_attributes(LLVMValueRef value, String export_name) { -lbAddr lb_handle_objc_find_or_register_selector(lbProcedure *p, String const &name) { +gb_internal lbAddr lb_handle_objc_find_or_register_selector(lbProcedure *p, String const &name) { lbAddr *found = string_map_get(&p->module->objc_selectors, name); if (found) { return *found; @@ -1938,7 +1938,7 @@ lbAddr lb_handle_objc_find_or_register_selector(lbProcedure *p, String const &na } } -lbValue lb_handle_objc_find_selector(lbProcedure *p, Ast *expr) { +gb_internal lbValue lb_handle_objc_find_selector(lbProcedure *p, Ast *expr) { ast_node(ce, CallExpr, expr); auto tav = ce->args[0]->tav; @@ -1947,7 +1947,7 @@ lbValue lb_handle_objc_find_selector(lbProcedure *p, Ast *expr) { return lb_addr_load(p, lb_handle_objc_find_or_register_selector(p, name)); } -lbValue lb_handle_objc_register_selector(lbProcedure *p, Ast *expr) { +gb_internal lbValue lb_handle_objc_register_selector(lbProcedure *p, Ast *expr) { ast_node(ce, CallExpr, expr); lbModule *m = p->module; @@ -1964,7 +1964,7 @@ lbValue lb_handle_objc_register_selector(lbProcedure *p, Ast *expr) { return lb_addr_load(p, dst); } -lbAddr lb_handle_objc_find_or_register_class(lbProcedure *p, String const &name) { +gb_internal lbAddr lb_handle_objc_find_or_register_class(lbProcedure *p, String const &name) { lbAddr *found = string_map_get(&p->module->objc_classes, name); if (found) { return *found; @@ -1984,7 +1984,7 @@ lbAddr lb_handle_objc_find_or_register_class(lbProcedure *p, String const &name) } } -lbValue lb_handle_objc_find_class(lbProcedure *p, Ast *expr) { +gb_internal lbValue lb_handle_objc_find_class(lbProcedure *p, Ast *expr) { ast_node(ce, CallExpr, expr); auto tav = ce->args[0]->tav; @@ -1993,7 +1993,7 @@ lbValue lb_handle_objc_find_class(lbProcedure *p, Ast *expr) { return lb_addr_load(p, lb_handle_objc_find_or_register_class(p, name)); } -lbValue lb_handle_objc_register_class(lbProcedure *p, Ast *expr) { +gb_internal lbValue lb_handle_objc_register_class(lbProcedure *p, Ast *expr) { ast_node(ce, CallExpr, expr); lbModule *m = p->module; @@ -2013,7 +2013,7 @@ lbValue lb_handle_objc_register_class(lbProcedure *p, Ast *expr) { } -lbValue lb_handle_objc_id(lbProcedure *p, Ast *expr) { +gb_internal lbValue lb_handle_objc_id(lbProcedure *p, Ast *expr) { TypeAndValue const &tav = type_and_value_of_expr(expr); if (tav.mode == Addressing_Type) { Type *type = tav.type; @@ -2044,7 +2044,7 @@ lbValue lb_handle_objc_id(lbProcedure *p, Ast *expr) { return lb_build_expr(p, expr); } -lbValue lb_handle_objc_send(lbProcedure *p, Ast *expr) { +gb_internal lbValue lb_handle_objc_send(lbProcedure *p, Ast *expr) { ast_node(ce, CallExpr, expr); lbModule *m = p->module; @@ -2087,7 +2087,7 @@ lbValue lb_handle_objc_send(lbProcedure *p, Ast *expr) { -LLVMAtomicOrdering llvm_atomic_ordering_from_odin(ExactValue const &value) { +gb_internal LLVMAtomicOrdering llvm_atomic_ordering_from_odin(ExactValue const &value) { GB_ASSERT(value.kind == ExactValue_Integer); i64 v = exact_value_to_i64(value); switch (v) { @@ -2103,7 +2103,7 @@ LLVMAtomicOrdering llvm_atomic_ordering_from_odin(ExactValue const &value) { } -LLVMAtomicOrdering llvm_atomic_ordering_from_odin(Ast *expr) { +gb_internal LLVMAtomicOrdering llvm_atomic_ordering_from_odin(Ast *expr) { ExactValue value = type_and_value_of_expr(expr).value; return llvm_atomic_ordering_from_odin(value); } -- cgit v1.2.3 From c1f5be24e28c41efbbbe6d116d533b55d48bbf82 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 18 Dec 2022 22:49:10 +0000 Subject: Remove dead code in the compiler --- build.bat | 1 + src/build_settings.cpp | 6 +- src/check_decl.cpp | 10 - src/check_expr.cpp | 24 - src/check_stmt.cpp | 6 - src/checker.cpp | 140 ++---- src/checker.hpp | 8 - src/common.cpp | 9 + src/docs.cpp | 9 - src/entity.cpp | 8 - src/exact_value.cpp | 86 ++-- src/llvm_backend.cpp | 10 +- src/llvm_backend_debug.cpp | 6 - src/llvm_backend_general.cpp | 68 --- src/llvm_backend_opt.cpp | 20 +- src/llvm_backend_proc.cpp | 88 ++-- src/llvm_backend_stmt.cpp | 10 - src/llvm_backend_utility.cpp | 12 - src/main.cpp | 77 +--- src/parser.cpp | 36 -- src/parser.hpp | 5 - src/parser_pos.cpp | 2 - src/query_data.cpp | 1030 ------------------------------------------ src/range_cache.cpp | 18 +- src/string.cpp | 14 - src/string_map.cpp | 2 - src/threading.cpp | 50 +- src/types.cpp | 75 --- 28 files changed, 169 insertions(+), 1661 deletions(-) delete mode 100644 src/query_data.cpp (limited to 'src/llvm_backend_general.cpp') diff --git a/build.bat b/build.bat index 7391bd95f..d7a89fe20 100644 --- a/build.bat +++ b/build.bat @@ -62,6 +62,7 @@ if %release_mode% EQU 0 ( rem Debug set compiler_warnings= ^ -W4 -WX ^ -wd4100 -wd4101 -wd4127 -wd4146 ^ + -wd4505 ^ -wd4456 -wd4457 set compiler_includes= ^ diff --git a/src/build_settings.cpp b/src/build_settings.cpp index d66db8099..7d796d775 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -149,7 +149,6 @@ enum CommandKind : u32 { Command_run = 1<<0, Command_build = 1<<1, Command_check = 1<<3, - Command_query = 1<<4, Command_doc = 1<<5, Command_version = 1<<6, Command_test = 1<<7, @@ -157,7 +156,7 @@ enum CommandKind : u32 { Command_strip_semicolon = 1<<8, Command_bug_report = 1<<9, - Command__does_check = Command_run|Command_build|Command_check|Command_query|Command_doc|Command_test|Command_strip_semicolon, + Command__does_check = Command_run|Command_build|Command_check|Command_doc|Command_test|Command_strip_semicolon, Command__does_build = Command_run|Command_build|Command_test, Command_all = ~(u32)0, }; @@ -166,7 +165,6 @@ gb_global char const *odin_command_strings[32] = { "run", "build", "check", - "query", "doc", "version", "test", @@ -316,8 +314,6 @@ struct BuildContext { u32 cmd_doc_flags; Array extra_packages; - QueryDataSetSettings query_data_set_settings; - StringSet test_names; gbAffinity affinity; diff --git a/src/check_decl.cpp b/src/check_decl.cpp index a976c1b73..d982a69fc 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -232,16 +232,6 @@ gb_internal Ast *remove_type_alias_clutter(Ast *node) { } } -gb_internal isize total_attribute_count(DeclInfo *decl) { - isize attribute_count = 0; - for_array(i, decl->attributes) { - Ast *attr = decl->attributes[i]; - if (attr->kind != Ast_Attribute) continue; - attribute_count += attr->Attribute.elems.count; - } - return attribute_count; -} - gb_internal Type *clone_enum_type(CheckerContext *ctx, Type *original_enum_type, Type *named_type) { // NOTE(bill, 2022-02-05): Stupid edge case for `distinct` declarations // diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 3003e07b6..ed1ddd1f1 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -5103,16 +5103,6 @@ gb_internal bool check_unpack_arguments(CheckerContext *ctx, Entity **lhs, isize return optional_ok; } - -gb_internal bool is_expr_constant_zero(Ast *expr) { - GB_ASSERT(expr != nullptr); - auto v = exact_value_to_integer(expr->tav.value); - if (v.kind == ExactValue_Integer) { - return big_int_cmp_zero(&v.value_integer) == 0; - } - return false; -} - gb_internal isize get_procedure_param_count_excluding_defaults(Type *pt, isize *param_count_) { GB_ASSERT(pt != nullptr); GB_ASSERT(pt->kind == Type_Proc); @@ -5429,20 +5419,6 @@ gb_internal isize lookup_procedure_parameter(TypeProc *pt, String parameter_name } return -1; } -gb_internal isize lookup_procedure_result(TypeProc *pt, String result_name) { - isize result_count = pt->result_count; - for (isize i = 0; i < result_count; i++) { - Entity *e = pt->results->Tuple.variables[i]; - String name = e->token.string; - if (is_blank_ident(name)) { - continue; - } - if (name == result_name) { - return i; - } - } - return -1; -} gb_internal CALL_ARGUMENT_CHECKER(check_named_call_arguments) { ast_node(ce, CallExpr, call); diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index cae9c3537..73adbed8b 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -1520,12 +1520,6 @@ gb_internal void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) } case_end; - case_ast_node(ts, TagStmt, node); - // TODO(bill): Tag Statements - error(node, "Tag statements are not supported yet"); - check_stmt(ctx, ts->stmt, flags); - case_end; - case_ast_node(as, AssignStmt, node); switch (as->op.kind) { case Token_Eq: { diff --git a/src/checker.cpp b/src/checker.cpp index e2c430dc2..f130a965f 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -61,18 +61,6 @@ gb_internal void scope_reserve(Scope *scope, isize capacity) { } } -gb_internal i32 is_scope_an_ancestor(Scope *parent, Scope *child) { - i32 i = 0; - while (child != nullptr) { - if (parent == child) { - return i; - } - child = child->parent; - i++; - } - return -1; -} - gb_internal void entity_graph_node_set_destroy(EntityGraphNodeSet *s) { if (s->hashes.data != nullptr) { ptr_set_destroy(s); @@ -86,9 +74,9 @@ gb_internal void entity_graph_node_set_add(EntityGraphNodeSet *s, EntityGraphNod ptr_set_add(s, n); } -gb_internal bool entity_graph_node_set_exists(EntityGraphNodeSet *s, EntityGraphNode *n) { - return ptr_set_exists(s, n); -} +// gb_internal bool entity_graph_node_set_exists(EntityGraphNodeSet *s, EntityGraphNode *n) { +// return ptr_set_exists(s, n); +// } gb_internal void entity_graph_node_set_remove(EntityGraphNodeSet *s, EntityGraphNode *n) { ptr_set_remove(s, n); @@ -139,13 +127,13 @@ gb_internal void import_graph_node_set_add(ImportGraphNodeSet *s, ImportGraphNod ptr_set_add(s, n); } -gb_internal bool import_graph_node_set_exists(ImportGraphNodeSet *s, ImportGraphNode *n) { - return ptr_set_exists(s, n); -} +// gb_internal bool import_graph_node_set_exists(ImportGraphNodeSet *s, ImportGraphNode *n) { +// return ptr_set_exists(s, n); +// } -gb_internal void import_graph_node_set_remove(ImportGraphNodeSet *s, ImportGraphNode *n) { - ptr_set_remove(s, n); -} +// gb_internal void import_graph_node_set_remove(ImportGraphNodeSet *s, ImportGraphNode *n) { +// ptr_set_remove(s, n); +// } gb_internal ImportGraphNode *import_graph_node_create(gbAllocator a, AstPackage *pkg) { ImportGraphNode *n = gb_alloc_item(a, ImportGraphNode); @@ -186,15 +174,6 @@ gb_internal void import_graph_node_swap(ImportGraphNode **data, isize i, isize j y->index = i; } -gb_internal GB_COMPARE_PROC(ast_node_cmp) { - Ast *x = *cast(Ast **)a; - Ast *y = *cast(Ast **)b; - Token i = ast_token(x); - Token j = ast_token(y); - return token_pos_cmp(i.pos, j.pos); -} - - @@ -213,27 +192,27 @@ gb_internal DeclInfo *make_decl_info(Scope *scope, DeclInfo *parent) { return d; } -gb_internal void destroy_declaration_info(DeclInfo *d) { - ptr_set_destroy(&d->deps); - array_free(&d->labels); -} +// gb_internal void destroy_declaration_info(DeclInfo *d) { +// ptr_set_destroy(&d->deps); +// array_free(&d->labels); +// } -gb_internal bool decl_info_has_init(DeclInfo *d) { - if (d->init_expr != nullptr) { - return true; - } - if (d->proc_lit != nullptr) { - switch (d->proc_lit->kind) { - case_ast_node(pl, ProcLit, d->proc_lit); - if (pl->body != nullptr) { - return true; - } - case_end; - } - } +// gb_internal bool decl_info_has_init(DeclInfo *d) { +// if (d->init_expr != nullptr) { +// return true; +// } +// if (d->proc_lit != nullptr) { +// switch (d->proc_lit->kind) { +// case_ast_node(pl, ProcLit, d->proc_lit); +// if (pl->body != nullptr) { +// return true; +// } +// case_end; +// } +// } - return false; -} +// return false; +// } @@ -528,11 +507,6 @@ struct VettedEntity { Entity *entity; Entity *other; }; -gb_internal void init_vetted_entity(VettedEntity *ve, VettedEntityKind kind, Entity *entity, Entity *other=nullptr) { - ve->kind = kind; - ve->entity = entity; - ve->other = other; -} gb_internal GB_COMPARE_PROC(vetted_entity_variable_pos_cmp) { @@ -1144,7 +1118,7 @@ gb_internal void init_checker_info(CheckerInfo *i) { - i->allow_identifier_uses = build_context.query_data_set_settings.kind == QueryDataSet_GoToDefinitions; + i->allow_identifier_uses = false; if (i->allow_identifier_uses) { array_init(&i->identifier_uses, a); } @@ -1226,13 +1200,10 @@ gb_internal CheckerContext make_checker_context(Checker *c) { ctx.type_path = new_checker_type_path(); ctx.type_level = 0; - ctx.poly_path = new_checker_poly_path(); - ctx.poly_level = 0; return ctx; } gb_internal void destroy_checker_context(CheckerContext *ctx) { destroy_checker_type_path(ctx->type_path); - destroy_checker_poly_path(ctx->poly_path); } gb_internal void add_curr_ast_file(CheckerContext *ctx, AstFile *file) { @@ -1348,17 +1319,17 @@ gb_internal DeclInfo *decl_info_of_entity(Entity *e) { return nullptr; } -gb_internal DeclInfo *decl_info_of_ident(Ast *ident) { - return decl_info_of_entity(entity_of_node(ident)); -} +// gb_internal DeclInfo *decl_info_of_ident(Ast *ident) { +// return decl_info_of_entity(entity_of_node(ident)); +// } -gb_internal AstFile *ast_file_of_filename(CheckerInfo *i, String filename) { - AstFile **found = string_map_get(&i->files, filename); - if (found != nullptr) { - return *found; - } - return nullptr; -} +// gb_internal AstFile *ast_file_of_filename(CheckerInfo *i, String filename) { +// AstFile **found = string_map_get(&i->files, filename); +// if (found != nullptr) { +// return *found; +// } +// return nullptr; +// } gb_internal ExprInfo *check_get_expr_info(CheckerContext *c, Ast *expr) { if (c->untyped != nullptr) { ExprInfo **found = map_get(c->untyped, expr); @@ -2684,32 +2655,6 @@ gb_internal Entity *check_type_path_pop(CheckerContext *c) { } -gb_internal CheckerPolyPath *new_checker_poly_path(void) { - gbAllocator a = heap_allocator(); - auto *pp = gb_alloc_item(a, CheckerPolyPath); - array_init(pp, a, 0, 16); - return pp; -} - -gb_internal void destroy_checker_poly_path(CheckerPolyPath *pp) { - array_free(pp); - gb_free(heap_allocator(), pp); -} - - -gb_internal void check_poly_path_push(CheckerContext *c, Type *t) { - GB_ASSERT(c->poly_path != nullptr); - GB_ASSERT(t != nullptr); - GB_ASSERT(is_type_polymorphic(t)); - array_add(c->poly_path, t); -} - -gb_internal Type *check_poly_path_pop(CheckerContext *c) { - GB_ASSERT(c->poly_path != nullptr); - return array_pop(c->poly_path); -} - - gb_internal Array proc_group_entities(CheckerContext *c, Operand o) { Array procs = {}; @@ -2878,13 +2823,6 @@ gb_internal ExactValue check_decl_attribute_value(CheckerContext *c, Ast *value) return ev; } -gb_internal Type *check_decl_attribute_type(CheckerContext *c, Ast *value) { - if (value != nullptr) { - return check_type(c, value); - } - return nullptr; -} - #define ATTRIBUTE_USER_TAG_NAME "tag" diff --git a/src/checker.hpp b/src/checker.hpp index e1efd5b89..58f6a027c 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -397,8 +397,6 @@ struct CheckerContext { CheckerTypePath *type_path; isize type_level; // TODO(bill): Actually handle correctly - CheckerPolyPath *poly_path; - isize poly_level; // TODO(bill): Actually handle correctly UntypedExprInfoMap *untyped; @@ -489,12 +487,6 @@ gb_internal void destroy_checker_type_path(CheckerTypePath *tp); gb_internal void check_type_path_push(CheckerContext *c, Entity *e); gb_internal Entity *check_type_path_pop (CheckerContext *c); -gb_internal CheckerPolyPath *new_checker_poly_path(); -gb_internal void destroy_checker_poly_path(CheckerPolyPath *); - -gb_internal void check_poly_path_push(CheckerContext *c, Type *t); -gb_internal Type *check_poly_path_pop (CheckerContext *c); - gb_internal void init_core_context(Checker *c); gb_internal void init_mem_allocator(Checker *c); diff --git a/src/common.cpp b/src/common.cpp index 09203e633..3624446f1 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -50,6 +50,10 @@ gb_internal void debugf(char const *fmt, ...); #include "string.cpp" #include "range_cache.cpp" +#if defined(GB_SYSTEM_WINDOWS) + #pragma warning(push) + #pragma warning(disable: 4505) +#endif gb_internal gb_inline bool is_power_of_two(i64 x) { if (x <= 0) { @@ -900,3 +904,8 @@ gb_internal Slice did_you_mean_results(DidYouMeanAnswers *d) } return slice_array(d->distances, 0, count); } + + +#if defined(GB_SYSTEM_WINDOWS) + #pragma warning(pop) +#endif \ No newline at end of file diff --git a/src/docs.cpp b/src/docs.cpp index 33b1e8361..b1efa2b46 100644 --- a/src/docs.cpp +++ b/src/docs.cpp @@ -85,15 +85,6 @@ gb_internal void print_doc_line(i32 indent, char const *fmt, ...) { va_end(va); gb_printf("\n"); } -gb_internal void print_doc_line_no_newline(i32 indent, char const *fmt, ...) { - while (indent --> 0) { - gb_printf("\t"); - } - va_list va; - va_start(va, fmt); - gb_printf_va(fmt, va); - va_end(va); -} gb_internal void print_doc_line_no_newline(i32 indent, String const &data) { while (indent --> 0) { gb_printf("\t"); diff --git a/src/entity.cpp b/src/entity.cpp index 6a3a69950..0605a293a 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -404,14 +404,6 @@ gb_internal Entity *alloc_entity_proc_group(Scope *scope, Token token, Type *typ return entity; } - -gb_internal Entity *alloc_entity_builtin(Scope *scope, Token token, Type *type, i32 id) { - Entity *entity = alloc_entity(Entity_Builtin, scope, token, type); - entity->Builtin.id = id; - entity->state = EntityState_Resolved; - return entity; -} - gb_internal Entity *alloc_entity_import_name(Scope *scope, Token token, Type *type, String path, String name, Scope *import_scope) { Entity *entity = alloc_entity(Entity_ImportName, scope, token, type); diff --git a/src/exact_value.cpp b/src/exact_value.cpp index 453909a15..f4c85505d 100644 --- a/src/exact_value.cpp +++ b/src/exact_value.cpp @@ -15,16 +15,6 @@ struct Quaternion256 { f64 imag, jmag, kmag, real; }; -gb_internal Quaternion256 quaternion256_inverse(Quaternion256 x) { - f64 invmag2 = 1.0 / (x.real*x.real + x.imag*x.imag + x.jmag*x.jmag + x.kmag*x.kmag); - x.real = +x.real * invmag2; - x.imag = -x.imag * invmag2; - x.jmag = -x.jmag * invmag2; - x.kmag = -x.kmag * invmag2; - return x; -} - - enum ExactValueKind { ExactValue_Invalid = 0, @@ -453,44 +443,44 @@ gb_internal ExactValue exact_value_kmag(ExactValue v) { return r; } -gb_internal ExactValue exact_value_make_imag(ExactValue v) { - switch (v.kind) { - case ExactValue_Integer: - return exact_value_complex(0, exact_value_to_float(v).value_float); - case ExactValue_Float: - return exact_value_complex(0, v.value_float); - default: - GB_PANIC("Expected an integer or float type for 'exact_value_make_imag'"); - } - ExactValue r = {ExactValue_Invalid}; - return r; -} - -gb_internal ExactValue exact_value_make_jmag(ExactValue v) { - switch (v.kind) { - case ExactValue_Integer: - return exact_value_quaternion(0, 0, exact_value_to_float(v).value_float, 0); - case ExactValue_Float: - return exact_value_quaternion(0, 0, v.value_float, 0); - default: - GB_PANIC("Expected an integer or float type for 'exact_value_make_imag'"); - } - ExactValue r = {ExactValue_Invalid}; - return r; -} - -gb_internal ExactValue exact_value_make_kmag(ExactValue v) { - switch (v.kind) { - case ExactValue_Integer: - return exact_value_quaternion(0, 0, 0, exact_value_to_float(v).value_float); - case ExactValue_Float: - return exact_value_quaternion(0, 0, 0, v.value_float); - default: - GB_PANIC("Expected an integer or float type for 'exact_value_make_imag'"); - } - ExactValue r = {ExactValue_Invalid}; - return r; -} +// gb_internal ExactValue exact_value_make_imag(ExactValue v) { +// switch (v.kind) { +// case ExactValue_Integer: +// return exact_value_complex(0, exact_value_to_float(v).value_float); +// case ExactValue_Float: +// return exact_value_complex(0, v.value_float); +// default: +// GB_PANIC("Expected an integer or float type for 'exact_value_make_imag'"); +// } +// ExactValue r = {ExactValue_Invalid}; +// return r; +// } + +// gb_internal ExactValue exact_value_make_jmag(ExactValue v) { +// switch (v.kind) { +// case ExactValue_Integer: +// return exact_value_quaternion(0, 0, exact_value_to_float(v).value_float, 0); +// case ExactValue_Float: +// return exact_value_quaternion(0, 0, v.value_float, 0); +// default: +// GB_PANIC("Expected an integer or float type for 'exact_value_make_jmag'"); +// } +// ExactValue r = {ExactValue_Invalid}; +// return r; +// } + +// gb_internal ExactValue exact_value_make_kmag(ExactValue v) { +// switch (v.kind) { +// case ExactValue_Integer: +// return exact_value_quaternion(0, 0, 0, exact_value_to_float(v).value_float); +// case ExactValue_Float: +// return exact_value_quaternion(0, 0, 0, v.value_float); +// default: +// GB_PANIC("Expected an integer or float type for 'exact_value_make_kmag'"); +// } +// ExactValue r = {ExactValue_Invalid}; +// return r; +// } gb_internal i64 exact_value_to_i64(ExactValue v) { v = exact_value_to_integer(v); diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index ca4b3b683..c5bc96eb9 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -605,11 +605,11 @@ gb_internal lbValue lb_map_get_proc_for_type(lbModule *m, Type *type) { return {p->value, p->type}; } -gb_internal void lb_debug_print(lbProcedure *p, String const &str) { - auto args = array_make(heap_allocator(), 1); - args[0] = lb_const_string(p->module, str); - lb_emit_runtime_call(p, "print_string", args); -} +// gb_internal void lb_debug_print(lbProcedure *p, String const &str) { +// auto args = array_make(heap_allocator(), 1); +// args[0] = lb_const_string(p->module, str); +// lb_emit_runtime_call(p, "print_string", args); +// } gb_internal lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) { GB_ASSERT(build_context.use_static_map_calls); diff --git a/src/llvm_backend_debug.cpp b/src/llvm_backend_debug.cpp index 849416579..55c4370a2 100644 --- a/src/llvm_backend_debug.cpp +++ b/src/llvm_backend_debug.cpp @@ -14,12 +14,6 @@ gb_internal void lb_set_llvm_metadata(lbModule *m, void *key, LLVMMetadataRef va } } -gb_internal LLVMMetadataRef lb_get_llvm_file_metadata_from_node(lbModule *m, Ast *node) { - if (node == nullptr) { - return nullptr; - } - return lb_get_llvm_metadata(m, node->file()); -} gb_internal LLVMMetadataRef lb_get_current_debug_scope(lbProcedure *p) { GB_ASSERT_MSG(p->debug_info != nullptr, "missing debug information for %.*s", LIT(p->name)); diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index f30038da8..e5aa95f10 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -272,12 +272,6 @@ gb_internal lbValue lb_emit_epi(lbModule *m, lbValue const &value, isize index) gb_internal LLVMValueRef llvm_zero(lbModule *m) { return LLVMConstInt(lb_type(m, t_int), 0, false); } -gb_internal LLVMValueRef llvm_zero32(lbModule *m) { - return LLVMConstInt(lb_type(m, t_i32), 0, false); -} -gb_internal LLVMValueRef llvm_one(lbModule *m) { - return LLVMConstInt(lb_type(m, t_i32), 1, false); -} gb_internal LLVMValueRef llvm_alloca(lbProcedure *p, LLVMTypeRef llvm_type, isize alignment, char const *name) { LLVMPositionBuilderAtEnd(p->builder, p->decl_block->block); @@ -874,14 +868,6 @@ gb_internal void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value) { lb_emit_store(p, addr.addr, value); } -gb_internal void lb_const_store(lbValue ptr, lbValue value) { - GB_ASSERT(lb_is_const(ptr)); - GB_ASSERT(lb_is_const(value)); - GB_ASSERT(is_type_pointer(ptr.type)); - LLVMSetInitializer(ptr.value, value.value); -} - - gb_internal bool lb_is_type_proc_recursive(Type *t) { for (;;) { if (t == nullptr) { @@ -1327,21 +1313,6 @@ gb_internal void lb_clone_struct_type(LLVMTypeRef dst, LLVMTypeRef src) { LLVMStructSetBody(dst, fields, field_count, LLVMIsPackedStruct(src)); } -gb_internal LLVMTypeRef lb_alignment_prefix_type_hack(lbModule *m, i64 alignment) { - switch (alignment) { - case 1: - return LLVMArrayType(lb_type(m, t_u8), 0); - case 2: - return LLVMArrayType(lb_type(m, t_u16), 0); - case 4: - return LLVMArrayType(lb_type(m, t_u32), 0); - case 8: - return LLVMArrayType(lb_type(m, t_u64), 0); - default: case 16: - return LLVMArrayType(LLVMVectorType(lb_type(m, t_u32), 4), 0); - } -} - gb_internal String lb_mangle_name(lbModule *m, Entity *e) { String name = e->token.string; @@ -2202,9 +2173,6 @@ gb_internal void lb_add_member(lbModule *m, String const &name, lbValue val) { string_map_set(&m->members, name, val); } } -gb_internal void lb_add_member(lbModule *m, StringHashKey const &key, lbValue val) { - string_map_set(&m->members, key, val); -} gb_internal void lb_add_procedure_value(lbModule *m, lbProcedure *p) { if (p->entity != nullptr) { map_set(&m->procedure_values, p->value, p->entity); @@ -2493,42 +2461,6 @@ gb_internal lbValue lb_find_or_add_entity_string(lbModule *m, String const &str) return res; } -gb_internal lbValue lb_find_or_add_entity_string_byte_slice(lbModule *m, String const &str) { - LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)}; - LLVMValueRef data = LLVMConstStringInContext(m->ctx, - cast(char const *)str.text, - cast(unsigned)str.len, - false); - - - char *name = nullptr; - { - isize max_len = 7+8+1; - name = gb_alloc_array(permanent_allocator(), char, max_len); - u32 id = m->gen->global_array_index.fetch_add(1); - isize len = gb_snprintf(name, max_len, "csbs$%x", id); - len -= 1; - } - LLVMTypeRef type = LLVMTypeOf(data); - LLVMValueRef global_data = LLVMAddGlobal(m->mod, type, name); - LLVMSetInitializer(global_data, data); - lb_make_global_private_const(global_data); - LLVMSetAlignment(global_data, 1); - - LLVMValueRef ptr = nullptr; - if (str.len != 0) { - ptr = LLVMConstInBoundsGEP2(type, global_data, indices, 2); - } else { - ptr = LLVMConstNull(lb_type(m, t_u8_ptr)); - } - LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), str.len, true); - LLVMValueRef values[2] = {ptr, len}; - - lbValue res = {}; - res.value = llvm_const_named_struct(m, t_u8_slice, values, 2); - res.type = t_u8_slice; - return res; -} gb_internal lbValue lb_find_or_add_entity_string_byte_slice_with_type(lbModule *m, String const &str, Type *slice_type) { GB_ASSERT(is_type_slice(slice_type)); LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)}; diff --git a/src/llvm_backend_opt.cpp b/src/llvm_backend_opt.cpp index 533264e62..fd6d94361 100644 --- a/src/llvm_backend_opt.cpp +++ b/src/llvm_backend_opt.cpp @@ -37,16 +37,16 @@ gb_internal void lb_add_function_simplifcation_passes(LLVMPassManagerRef mpm, i3 gb_internal void lb_populate_module_pass_manager(LLVMTargetMachineRef target_machine, LLVMPassManagerRef mpm, i32 optimization_level); gb_internal void lb_populate_function_pass_manager_specific(lbModule *m, LLVMPassManagerRef fpm, i32 optimization_level); -gb_internal LLVMBool lb_must_preserve_predicate_callback(LLVMValueRef value, void *user_data) { - lbModule *m = cast(lbModule *)user_data; - if (m == nullptr) { - return false; - } - if (value == nullptr) { - return false; - } - return LLVMIsAAllocaInst(value) != nullptr; -} +// gb_internal LLVMBool lb_must_preserve_predicate_callback(LLVMValueRef value, void *user_data) { +// lbModule *m = cast(lbModule *)user_data; +// if (m == nullptr) { +// return false; +// } +// if (value == nullptr) { +// return false; +// } +// return LLVMIsAAllocaInst(value) != nullptr; +// } #if LLVM_VERSION_MAJOR < 12 diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 0789fb2c1..384d29ca7 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -383,46 +383,46 @@ gb_internal lbProcedure *lb_create_dummy_procedure(lbModule *m, String link_name } -gb_internal lbValue lb_value_param(lbProcedure *p, Entity *e, Type *abi_type, i32 index, lbParamPasskind *kind_) { - lbParamPasskind kind = lbParamPass_Value; - - if (e != nullptr && !are_types_identical(abi_type, e->type)) { - if (is_type_pointer(abi_type)) { - GB_ASSERT(e->kind == Entity_Variable); - Type *av = core_type(type_deref(abi_type)); - if (are_types_identical(av, core_type(e->type))) { - kind = lbParamPass_Pointer; - if (e->flags&EntityFlag_Value) { - kind = lbParamPass_ConstRef; - } - } else { - kind = lbParamPass_BitCast; - } - } else if (is_type_integer(abi_type)) { - kind = lbParamPass_Integer; - } else if (abi_type == t_llvm_bool) { - kind = lbParamPass_Value; - } else if (is_type_boolean(abi_type)) { - kind = lbParamPass_Integer; - } else if (is_type_simd_vector(abi_type)) { - kind = lbParamPass_BitCast; - } else if (is_type_float(abi_type)) { - kind = lbParamPass_BitCast; - } else if (is_type_tuple(abi_type)) { - kind = lbParamPass_Tuple; - } else if (is_type_proc(abi_type)) { - kind = lbParamPass_Value; - } else { - GB_PANIC("Invalid abi type pass kind %s", type_to_string(abi_type)); - } - } - - if (kind_) *kind_ = kind; - lbValue res = {}; - res.value = LLVMGetParam(p->value, cast(unsigned)index); - res.type = abi_type; - return res; -} +// gb_internal lbValue lb_value_param(lbProcedure *p, Entity *e, Type *abi_type, i32 index, lbParamPasskind *kind_) { +// lbParamPasskind kind = lbParamPass_Value; + +// if (e != nullptr && !are_types_identical(abi_type, e->type)) { +// if (is_type_pointer(abi_type)) { +// GB_ASSERT(e->kind == Entity_Variable); +// Type *av = core_type(type_deref(abi_type)); +// if (are_types_identical(av, core_type(e->type))) { +// kind = lbParamPass_Pointer; +// if (e->flags&EntityFlag_Value) { +// kind = lbParamPass_ConstRef; +// } +// } else { +// kind = lbParamPass_BitCast; +// } +// } else if (is_type_integer(abi_type)) { +// kind = lbParamPass_Integer; +// } else if (abi_type == t_llvm_bool) { +// kind = lbParamPass_Value; +// } else if (is_type_boolean(abi_type)) { +// kind = lbParamPass_Integer; +// } else if (is_type_simd_vector(abi_type)) { +// kind = lbParamPass_BitCast; +// } else if (is_type_float(abi_type)) { +// kind = lbParamPass_BitCast; +// } else if (is_type_tuple(abi_type)) { +// kind = lbParamPass_Tuple; +// } else if (is_type_proc(abi_type)) { +// kind = lbParamPass_Value; +// } else { +// GB_PANIC("Invalid abi type pass kind %s", type_to_string(abi_type)); +// } +// } + +// if (kind_) *kind_ = kind; +// lbValue res = {}; +// res.value = LLVMGetParam(p->value, cast(unsigned)index); +// res.type = abi_type; +// return res; +// } @@ -1165,14 +1165,6 @@ gb_internal lbValue lb_emit_call(lbProcedure *p, lbValue value, Array c return result; } -gb_internal LLVMValueRef llvm_splat_float(i64 count, LLVMTypeRef type, f64 value) { - LLVMValueRef v = LLVMConstReal(type, value); - LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, count); - for (i64 i = 0; i < count; i++) { - values[i] = v; - } - return LLVMConstVector(values, cast(unsigned)count); -} gb_internal LLVMValueRef llvm_splat_int(i64 count, LLVMTypeRef type, i64 value, bool is_signed=false) { LLVMValueRef v = LLVMConstInt(type, value, is_signed); LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, count); diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 66c422071..6400a8a9d 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -378,16 +378,6 @@ gb_internal lbValue lb_map_cell_index_static(lbProcedure *p, Type *type, lbValue return lb_emit_ptr_offset(p, elems_ptr, data_index); } -gb_internal void lb_map_kvh_data_static(lbProcedure *p, lbValue map_value, lbValue *ks_, lbValue *vs_, lbValue *hs_) { - lbValue capacity = lb_map_cap(p, map_value); - lbValue ks = lb_map_data_uintptr(p, map_value); - lbValue vs = {}; - lbValue hs = {}; - if (ks_) *ks_ = ks; - if (vs_) *vs_ = vs; - if (hs_) *hs_ = hs; -} - gb_internal lbValue lb_map_hash_is_valid(lbProcedure *p, lbValue hash) { // N :: size_of(uintptr)*8 - 1 // (hash != 0) & (hash>>N == 0) diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp index 94b900278..dbed32b82 100644 --- a/src/llvm_backend_utility.cpp +++ b/src/llvm_backend_utility.cpp @@ -599,14 +599,6 @@ gb_internal lbValue lb_emit_reverse_bits(lbProcedure *p, lbValue x, Type *type) } -gb_internal lbValue lb_emit_bit_set_card(lbProcedure *p, lbValue x) { - GB_ASSERT(is_type_bit_set(x.type)); - Type *underlying = bit_set_to_int(x.type); - lbValue card = lb_emit_count_ones(p, x, underlying); - return lb_emit_conv(p, card, t_int); -} - - gb_internal lbValue lb_emit_union_cast_only_ok_check(lbProcedure *p, lbValue value, Type *type, TokenPos pos) { GB_ASSERT(is_type_tuple(type)); lbModule *m = p->module; @@ -1493,10 +1485,6 @@ gb_internal lbValue lb_dynamic_array_cap(lbProcedure *p, lbValue da) { GB_ASSERT(is_type_dynamic_array(da.type)); return lb_emit_struct_ev(p, da, 2); } -gb_internal lbValue lb_dynamic_array_allocator(lbProcedure *p, lbValue da) { - GB_ASSERT(is_type_dynamic_array(da.type)); - return lb_emit_struct_ev(p, da, 3); -} gb_internal lbValue lb_map_len(lbProcedure *p, lbValue value) { GB_ASSERT_MSG(is_type_map(value.type) || are_types_identical(value.type, t_raw_map), "%s", type_to_string(value.type)); diff --git a/src/main.cpp b/src/main.cpp index 614130bb6..c7250aa8b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,7 +3,14 @@ #include "common.cpp" #include "timings.cpp" #include "tokenizer.cpp" +#if defined(GB_SYSTEM_WINDOWS) + #pragma warning(push) + #pragma warning(disable: 4505) +#endif #include "big_int.cpp" +#if defined(GB_SYSTEM_WINDOWS) + #pragma warning(pop) +#endif #include "exact_value.cpp" #include "build_settings.cpp" @@ -58,7 +65,6 @@ gb_global Timings global_timings = {0}; #endif #endif -#include "query_data.cpp" #include "bug_report.cpp" // NOTE(bill): 'name' is used in debugging and profiling modes @@ -573,7 +579,6 @@ gb_internal void usage(String argv0) { print_usage_line(1, " one must contain the program's entry point, all must be in the same package."); print_usage_line(1, "run same as 'build', but also then runs the newly compiled executable."); print_usage_line(1, "check parse, and type check a directory of .odin files"); - print_usage_line(1, "query parse, type check, and output a .json file containing information about the program"); print_usage_line(1, "strip-semicolon parse, type check, and remove unneeded semicolons from the entire program"); print_usage_line(1, "test build and runs procedures with the attribute @(test) in the initial package"); print_usage_line(1, "doc generate documentation on a directory of .odin files"); @@ -817,12 +822,6 @@ gb_internal bool parse_build_flags(Array args) { add_flag(&build_flags, BuildFlag_UseStaticMapCalls, str_lit("use-static-map-calls"), BuildFlagParam_None, Command__does_check); - - add_flag(&build_flags, BuildFlag_Compact, str_lit("compact"), BuildFlagParam_None, Command_query); - add_flag(&build_flags, BuildFlag_GlobalDefinitions, str_lit("global-definitions"), BuildFlagParam_None, Command_query); - add_flag(&build_flags, BuildFlag_GoToDefinitions, str_lit("go-to-definitions"), BuildFlagParam_None, Command_query); - - add_flag(&build_flags, BuildFlag_Short, str_lit("short"), BuildFlagParam_None, Command_doc); add_flag(&build_flags, BuildFlag_AllPackages, str_lit("all-packages"), BuildFlagParam_None, Command_doc); add_flag(&build_flags, BuildFlag_DocFormat, str_lit("doc-format"), BuildFlagParam_None, Command_doc); @@ -1445,39 +1444,6 @@ gb_internal bool parse_build_flags(Array args) { build_context.strict_style_init_only = true; break; } - case BuildFlag_Compact: { - if (!build_context.query_data_set_settings.ok) { - gb_printf_err("Invalid use of -compact flag, only allowed with 'odin query'\n"); - bad_flags = true; - } else { - build_context.query_data_set_settings.compact = true; - } - break; - } - case BuildFlag_GlobalDefinitions: { - if (!build_context.query_data_set_settings.ok) { - gb_printf_err("Invalid use of -global-definitions flag, only allowed with 'odin query'\n"); - bad_flags = true; - } else if (build_context.query_data_set_settings.kind != QueryDataSet_Invalid) { - gb_printf_err("Invalid use of -global-definitions flag, a previous flag for 'odin query' was set\n"); - bad_flags = true; - } else { - build_context.query_data_set_settings.kind = QueryDataSet_GlobalDefinitions; - } - break; - } - case BuildFlag_GoToDefinitions: { - if (!build_context.query_data_set_settings.ok) { - gb_printf_err("Invalid use of -go-to-definitions flag, only allowed with 'odin query'\n"); - bad_flags = true; - } else if (build_context.query_data_set_settings.kind != QueryDataSet_Invalid) { - gb_printf_err("Invalid use of -global-definitions flag, a previous flag for 'odin query' was set\n"); - bad_flags = true; - } else { - build_context.query_data_set_settings.kind = QueryDataSet_GoToDefinitions; - } - break; - } case BuildFlag_Short: build_context.cmd_doc_flags |= CmdDocFlag_Short; break; @@ -1638,16 +1604,6 @@ gb_internal bool parse_build_flags(Array args) { gb_printf_err("`-export-timings:` requires `-show-timings` or `-show-more-timings` to be present\n"); bad_flags = true; } - - if (build_context.query_data_set_settings.ok) { - if (build_context.query_data_set_settings.kind == QueryDataSet_Invalid) { - gb_printf_err("'odin query' requires a flag determining the kind of query data set to be returned\n"); - gb_printf_err("\t-global-definitions : outputs a JSON file of global definitions\n"); - gb_printf_err("\t-go-to-definitions : outputs a OGTD binary file of go to definitions for identifiers within an Odin project\n"); - bad_flags = true; - } - } - return !bad_flags; } @@ -1931,8 +1887,6 @@ gb_internal void print_show_help(String const arg0, String const &command) { print_usage_line(3, "odin check filename.odin -file # Type check single-file package, must contain entry point."); } else if (command == "test") { print_usage_line(1, "test Build ands runs procedures with the attribute @(test) in the initial package"); - } else if (command == "query") { - print_usage_line(1, "query [experimental] Parse, type check, and output a .json file containing information about the program"); } else if (command == "doc") { print_usage_line(1, "doc generate documentation from a directory of .odin files"); print_usage_line(2, "Examples:"); @@ -2627,15 +2581,6 @@ int main(int arg_count, char const **arg_ptr) { build_context.command_kind = Command_strip_semicolon; build_context.no_output_files = true; init_filename = args[2]; - } else if (command == "query") { - if (args.count < 3) { - usage(args[0]); - return 1; - } - build_context.command_kind = Command_query; - build_context.no_output_files = true; - build_context.query_data_set_settings.ok = true; - init_filename = args[2]; } else if (command == "doc") { if (args.count < 3) { usage(args[0]); @@ -2824,12 +2769,8 @@ int main(int arg_count, char const **arg_ptr) { print_show_unused(checker); } - if (build_context.query_data_set_settings.ok) { - generate_and_print_query_data(checker, &global_timings); - } else { - if (build_context.show_timings) { - show_timings(checker, &global_timings); - } + if (build_context.show_timings) { + show_timings(checker, &global_timings); } if (global_error_collector.count != 0) { diff --git a/src/parser.cpp b/src/parser.cpp index 1606f5b47..2ccdac7fa 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -237,9 +237,6 @@ gb_internal Ast *clone_ast(Ast *node) { case Ast_ExprStmt: n->ExprStmt.expr = clone_ast(n->ExprStmt.expr); break; - case Ast_TagStmt: - n->TagStmt.stmt = clone_ast(n->TagStmt.stmt); - break; case Ast_AssignStmt: n->AssignStmt.lhs = clone_ast_array(n->AssignStmt.lhs); n->AssignStmt.rhs = clone_ast_array(n->AssignStmt.rhs); @@ -497,14 +494,6 @@ gb_internal Ast *ast_tag_expr(AstFile *f, Token token, Token name, Ast *expr) { return result; } -gb_internal Ast *ast_tag_stmt(AstFile *f, Token token, Token name, Ast *stmt) { - Ast *result = alloc_ast_node(f, Ast_TagStmt); - result->TagStmt.token = token; - result->TagStmt.name = name; - result->TagStmt.stmt = stmt; - return result; -} - gb_internal Ast *ast_unary_expr(AstFile *f, Token op, Ast *expr) { Ast *result = alloc_ast_node(f, Ast_UnaryExpr); result->UnaryExpr.op = op; @@ -1308,16 +1297,6 @@ gb_internal Token advance_token(AstFile *f) { return prev; } -gb_internal bool peek_token_kind(AstFile *f, TokenKind kind) { - for (isize i = f->curr_token_index+1; i < f->tokens.count; i++) { - Token tok = f->tokens[i]; - if (kind != Token_Comment && tok.kind == Token_Comment) { - continue; - } - return tok.kind == kind; - } - return false; -} gb_internal Token peek_token(AstFile *f) { for (isize i = f->curr_token_index+1; i < f->tokens.count; i++) { @@ -1440,17 +1419,6 @@ gb_internal Token expect_operator(AstFile *f) { return prev; } -gb_internal Token expect_keyword(AstFile *f) { - Token prev = f->curr_token; - if (!gb_is_between(prev.kind, Token__KeywordBegin+1, Token__KeywordEnd-1)) { - String p = token_to_string(prev); - syntax_error(f->curr_token, "Expected a keyword, got '%.*s'", - LIT(p)); - } - advance_token(f); - return prev; -} - gb_internal bool allow_token(AstFile *f, TokenKind kind) { Token prev = f->curr_token; if (prev.kind == kind) { @@ -1957,10 +1925,6 @@ gb_internal bool ast_on_same_line(Token const &x, Ast *yp) { return x.pos.line == y.pos.line; } -gb_internal bool ast_on_same_line(Ast *x, Ast *y) { - return ast_on_same_line(ast_token(x), y); -} - gb_internal Ast *parse_force_inlining_operand(AstFile *f, Token token) { Ast *expr = parse_unary_expr(f, false); Ast *e = strip_or_return_expr(expr); diff --git a/src/parser.hpp b/src/parser.hpp index a2d2c038e..f7b3e51ae 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -457,11 +457,6 @@ AST_KIND(_StmtBegin, "", bool) \ AST_KIND(BadStmt, "bad statement", struct { Token begin, end; }) \ AST_KIND(EmptyStmt, "empty statement", struct { Token token; }) \ AST_KIND(ExprStmt, "expression statement", struct { Ast *expr; } ) \ - AST_KIND(TagStmt, "tag statement", struct { \ - Token token; \ - Token name; \ - Ast * stmt; \ - }) \ AST_KIND(AssignStmt, "assign statement", struct { \ Token op; \ Slice lhs, rhs; \ diff --git a/src/parser_pos.cpp b/src/parser_pos.cpp index 19a525e2e..fb7f0c9c2 100644 --- a/src/parser_pos.cpp +++ b/src/parser_pos.cpp @@ -53,7 +53,6 @@ gb_internal Token ast_token(Ast *node) { case Ast_BadStmt: return node->BadStmt.begin; case Ast_EmptyStmt: return node->EmptyStmt.token; case Ast_ExprStmt: return ast_token(node->ExprStmt.expr); - case Ast_TagStmt: return node->TagStmt.token; case Ast_AssignStmt: return node->AssignStmt.op; case Ast_BlockStmt: return node->BlockStmt.open; case Ast_IfStmt: return node->IfStmt.token; @@ -197,7 +196,6 @@ Token ast_end_token(Ast *node) { case Ast_BadStmt: return node->BadStmt.end; case Ast_EmptyStmt: return node->EmptyStmt.token; case Ast_ExprStmt: return ast_end_token(node->ExprStmt.expr); - case Ast_TagStmt: return ast_end_token(node->TagStmt.stmt); case Ast_AssignStmt: if (node->AssignStmt.rhs.count > 0) { return ast_end_token(node->AssignStmt.rhs[node->AssignStmt.rhs.count-1]); diff --git a/src/query_data.cpp b/src/query_data.cpp deleted file mode 100644 index 86487a058..000000000 --- a/src/query_data.cpp +++ /dev/null @@ -1,1030 +0,0 @@ -struct QueryValue; -struct QueryValuePair; - -gb_global gbAllocator query_value_allocator = {}; - -enum QueryKind { - Query_Invalid, - Query_String, - Query_Boolean, - Query_Integer, - Query_Float, - Query_Array, - Query_Map, -}; - -struct QueryValuePair { - String key; - QueryValue *value; -}; - - -struct QueryValue { - QueryKind kind; - bool packed; -}; - -struct QueryValueString : QueryValue { - QueryValueString(String const &v) { - kind = Query_String; - value = v; - packed = false; - } - String value; -}; - -struct QueryValueBoolean : QueryValue { - QueryValueBoolean(bool v) { - kind = Query_Boolean; - value = v; - packed = false; - } - bool value; -}; - -struct QueryValueInteger : QueryValue { - QueryValueInteger(i64 v) { - kind = Query_Integer; - value = v; - packed = false; - } - i64 value; -}; - -struct QueryValueFloat : QueryValue { - QueryValueFloat(f64 v) { - kind = Query_Float; - value = v; - packed = false; - } - f64 value; -}; - -struct QueryValueArray : QueryValue { - QueryValueArray() { - kind = Query_Array; - array_init(&value, query_value_allocator); - packed = false; - } - QueryValueArray(Array const &v) { - kind = Query_Array; - value = v; - packed = false; - } - Array value; - - void reserve(isize cap) { - array_reserve(&value, cap); - } - void add(QueryValue *v) { - array_add(&value, v); - } - void add(char const *v) { - add(make_string_c(cast(char *)v)); - } - void add(String const &v) { - auto val = gb_alloc_item(query_value_allocator, QueryValueString); - *val = QueryValueString(v); - add(val); - } - void add(bool v) { - auto val = gb_alloc_item(query_value_allocator, QueryValueBoolean); - *val = QueryValueBoolean(v); - add(val); - } - void add(i64 v) { - auto val = gb_alloc_item(query_value_allocator, QueryValueInteger); - *val = QueryValueInteger(v); - add(val); - } - void add(f64 v) { - auto val = gb_alloc_item(query_value_allocator, QueryValueFloat); - *val = QueryValueFloat(v); - add(val); - } -}; - -struct QueryValueMap : QueryValue { - QueryValueMap() { - kind = Query_Map; - array_init(&value, query_value_allocator); - packed = false; - } - QueryValueMap(Array const &v) { - kind = Query_Map; - value = v; - packed = false; - } - Array value; - - - void reserve(isize cap) { - array_reserve(&value, cap); - } - void add(char const *k, QueryValue *v) { - add(make_string_c(cast(char *)k), v); - } - void add(String const &k, QueryValue *v) { - QueryValuePair kv = {k, v}; - array_add(&value, kv); - } - - void add(char const *k, String const &v) { - auto val = gb_alloc_item(query_value_allocator, QueryValueString); - *val = QueryValueString(v); - add(k, val); - } - void add(char const *k, char const *v) { - add(k, make_string_c(cast(char *)v)); - } - void add(char const *k, bool v) { - auto val = gb_alloc_item(query_value_allocator, QueryValueBoolean); - *val = QueryValueBoolean(v); - add(k, val); - } - void add(char const *k, i64 v) { - auto val = gb_alloc_item(query_value_allocator, QueryValueInteger); - *val = QueryValueInteger(v); - add(k, val); - } - void add(char const *k, f64 v) { - auto val = gb_alloc_item(query_value_allocator, QueryValueFloat); - *val = QueryValueFloat(v); - add(k, val); - } - void add(String const &k, String const &v) { - auto val = gb_alloc_item(query_value_allocator, QueryValueString); - *val = QueryValueString(v); - add(k, val); - } - void add(String const &k, char const *v) { - add(k, make_string_c(cast(char *)v)); - } - void add(String const &k, bool v) { - auto val = gb_alloc_item(query_value_allocator, QueryValueBoolean); - *val = QueryValueBoolean(v); - add(k, val); - } - void add(String const &k, i64 v) { - auto val = gb_alloc_item(query_value_allocator, QueryValueInteger); - *val = QueryValueInteger(v); - add(k, val); - } - void add(String const &k, f64 v) { - auto val = gb_alloc_item(query_value_allocator, QueryValueFloat); - *val = QueryValueFloat(v); - add(k, val); - } -}; - - -#define DEF_QUERY_PROC(TYPE, VALUETYPE, NAME) TYPE *NAME(VALUETYPE value) { \ - auto v = gb_alloc_item(query_value_allocator, TYPE); \ - *v = TYPE(value); \ - return v; \ -} -#define DEF_QUERY_PROC0(TYPE, NAME) TYPE *NAME() { \ - auto v = gb_alloc_item(query_value_allocator, TYPE); \ - *v = TYPE(); \ - return v; \ -} - -gb_internal DEF_QUERY_PROC(QueryValueString, String const &, query_value_string); -gb_internal DEF_QUERY_PROC(QueryValueBoolean, bool, query_value_boolean); -gb_internal DEF_QUERY_PROC(QueryValueInteger, i64, query_value_integer); -gb_internal DEF_QUERY_PROC(QueryValueFloat, f64, query_value_float); -gb_internal DEF_QUERY_PROC(QueryValueArray, Array const &, query_value_array); -gb_internal DEF_QUERY_PROC(QueryValueMap, Array const &, query_value_map); -gb_internal DEF_QUERY_PROC0(QueryValueArray, query_value_array); -gb_internal DEF_QUERY_PROC0(QueryValueMap, query_value_map); - -gb_internal isize qprintf(bool format, isize indent, char const *fmt, ...) { - if (format) while (indent --> 0) { - gb_printf("\t"); - } - va_list va; - va_start(va, fmt); - isize res = gb_printf_va(fmt, va); - va_end(va); - return res; -} - -gb_internal bool qv_valid_char(u8 c) { - if (c >= 0x80) { - return false; - } - - switch (c) { - case '\"': - case '\n': - case '\r': - case '\t': - case '\v': - case '\f': - return false; - } - - return true; -} - -gb_internal void print_query_data_as_json(QueryValue *value, bool format = true, isize indent = 0) { - if (value == nullptr) { - gb_printf("null"); - return; - } - switch (value->kind) { - case Query_String: { - auto v = cast(QueryValueString *)value; - String name = v->value; - isize extra = 0; - for (isize i = 0; i < name.len; i++) { - u8 c = name[i]; - if (!qv_valid_char(c)) { - extra += 5; - } - } - - if (extra == 0) { - gb_printf("\"%.*s\"", LIT(name)); - return; - } - - char const hex_table[] = "0123456789ABCDEF"; - isize buf_len = name.len + extra + 2 + 1; - - u8 *buf = gb_alloc_array(temporary_allocator(), u8, buf_len); - - isize j = 0; - - for (isize i = 0; i < name.len; i++) { - u8 c = name[i]; - if (qv_valid_char(c)) { - buf[j+0] = c; - j += 1; - } else if (c == '"') { - buf[j+0] = '\\'; - buf[j+1] = '\"'; - j += 2; - } else { - switch (c) { - case '\n': buf[j+0] = '\\'; buf[j+1] = 'n'; j += 2; break; - case '\r': buf[j+0] = '\\'; buf[j+1] = 'r'; j += 2; break; - case '\t': buf[j+0] = '\\'; buf[j+1] = 't'; j += 2; break; - case '\v': buf[j+0] = '\\'; buf[j+1] = 'v'; j += 2; break; - case '\f': - default: - buf[j+0] = '\\'; - buf[j+1] = hex_table[0]; - buf[j+2] = hex_table[0]; - buf[j+3] = hex_table[c >> 4]; - buf[j+4] = hex_table[c & 0x0f]; - j += 5; - break; - } - } - } - - gb_printf("\"%s\"", buf); - return; - } - case Query_Boolean: { - auto v = cast(QueryValueBoolean *)value; - if (v->value) { - gb_printf("true"); - } else { - gb_printf("false"); - } - return; - } - case Query_Integer: { - auto v = cast(QueryValueInteger *)value; - gb_printf("%lld", cast(long long)v->value); - return; - } - case Query_Float: { - auto v = cast(QueryValueFloat *)value; - gb_printf("%f", v->value); - return; - } - case Query_Array: { - auto v = cast(QueryValueArray *)value; - if (v->value.count > 0) { - bool ff = format && !v->packed; - gb_printf("["); - if (ff) gb_printf("\n"); - for_array(i, v->value) { - qprintf(ff, indent+1, ""); - print_query_data_as_json(v->value[i], ff, indent+1); - if (i < v->value.count-1) { - gb_printf(","); - if (!ff && format) { - gb_printf(" "); - } - } - if (ff) gb_printf("\n"); - } - qprintf(ff, indent, "]"); - } else { - gb_printf("[]"); - } - return; - } - case Query_Map: { - auto v = cast(QueryValueMap *)value; - if (v->value.count > 0) { - bool ff = format && !v->packed; - gb_printf("{"); - if (ff) gb_printf("\n"); - for_array(i, v->value) { - auto kv = v->value[i]; - qprintf(ff, indent+1, "\"%.*s\":", LIT(kv.key)); - if (format) gb_printf(" "); - print_query_data_as_json(kv.value, ff, indent+1); - if (i < v->value.count-1) { - gb_printf(","); - if (!ff && format) { - gb_printf(" "); - } - } - if (ff) gb_printf("\n"); - } - qprintf(ff, indent, "}"); - } else { - gb_printf("{}"); - } - return; - } - } -} - - - -gb_internal int query_data_package_compare(void const *a, void const *b) { - AstPackage *x = *cast(AstPackage *const *)a; - AstPackage *y = *cast(AstPackage *const *)b; - - if (x == y) { - return 0; - } - - if (x != nullptr && y != nullptr) { - return string_compare(x->name, y->name); - } else if (x != nullptr && y == nullptr) { - return -1; - } else if (x == nullptr && y != nullptr) { - return +1; - } - return 0; -} - -gb_internal int query_data_definition_compare(void const *a, void const *b) { - Entity *x = *cast(Entity *const *)a; - Entity *y = *cast(Entity *const *)b; - - if (x == y) { - return 0; - } else if (x != nullptr && y == nullptr) { - return -1; - } else if (x == nullptr && y != nullptr) { - return +1; - } - - if (x->pkg != y->pkg) { - i32 res = query_data_package_compare(&x->pkg, &y->pkg); - if (res != 0) { - return res; - } - } - - return string_compare(x->token.string, y->token.string); -} - -gb_internal int entity_name_compare(void const *a, void const *b) { - Entity *x = *cast(Entity *const *)a; - Entity *y = *cast(Entity *const *)b; - if (x == y) { - return 0; - } else if (x != nullptr && y == nullptr) { - return -1; - } else if (x == nullptr && y != nullptr) { - return +1; - } - return string_compare(x->token.string, y->token.string); -} - - -gb_internal void generate_and_print_query_data_global_definitions(Checker *c, Timings *timings); -gb_internal void generate_and_print_query_data_go_to_definitions(Checker *c); - -gb_internal void generate_and_print_query_data(Checker *c, Timings *timings) { - query_value_allocator = heap_allocator(); - switch (build_context.query_data_set_settings.kind) { - case QueryDataSet_GlobalDefinitions: - generate_and_print_query_data_global_definitions(c, timings); - return; - case QueryDataSet_GoToDefinitions: - generate_and_print_query_data_go_to_definitions(c); - return; - } -} - - -gb_internal void generate_and_print_query_data_global_definitions(Checker *c, Timings *timings) { - auto *root = query_value_map(); - - if (global_error_collector.errors.count > 0) { - auto *errors = query_value_array(); - root->add("errors", errors); - for_array(i, global_error_collector.errors) { - String err = string_trim_whitespace(global_error_collector.errors[i]); - errors->add(err); - } - - } - - { // Packages - auto *packages = query_value_array(); - root->add("packages", packages); - - auto sorted_packages = array_make(query_value_allocator, 0, c->info.packages.entries.count); - defer (array_free(&sorted_packages)); - - for (auto const &entry : c->info.packages) { - AstPackage *pkg = entry.value; - if (pkg != nullptr) { - array_add(&sorted_packages, pkg); - } - } - gb_sort_array(sorted_packages.data, sorted_packages.count, query_data_package_compare); - packages->reserve(sorted_packages.count); - - for_array(i, sorted_packages) { - AstPackage *pkg = sorted_packages[i]; - String name = pkg->name; - String fullpath = pkg->fullpath; - - auto *files = query_value_array(); - files->reserve(pkg->files.count); - for_array(j, pkg->files) { - AstFile *f = pkg->files[j]; - files->add(f->fullpath); - } - - auto *package = query_value_map(); - package->reserve(3); - packages->add(package); - - package->add("name", pkg->name); - package->add("fullpath", pkg->fullpath); - package->add("files", files); - } - } - - if (c->info.definitions.count > 0) { - auto *definitions = query_value_array(); - root->add("definitions", definitions); - - auto sorted_definitions = array_make(query_value_allocator, 0, c->info.definitions.count); - defer (array_free(&sorted_definitions)); - - for_array(i, c->info.definitions) { - Entity *e = c->info.definitions[i]; - String name = e->token.string; - if (is_blank_ident(name)) { - continue; - } - if ((e->scope->flags & (ScopeFlag_Pkg|ScopeFlag_File)) == 0) { - continue; - } - if (e->parent_proc_decl != nullptr) { - continue; - } - switch (e->kind) { - case Entity_Builtin: - case Entity_Nil: - case Entity_Label: - continue; - } - if (e->pkg == nullptr) { - continue; - } - if (e->token.pos.line == 0) { - continue; - } - if (e->kind == Entity_Procedure) { - Type *t = base_type(e->type); - if (t->kind != Type_Proc) { - continue; - } - if (t->Proc.is_poly_specialized) { - continue; - } - } - if (e->kind == Entity_TypeName) { - Type *t = base_type(e->type); - if (t->kind == Type_Struct) { - if (t->Struct.is_poly_specialized) { - continue; - } - } - if (t->kind == Type_Union) { - if (t->Union.is_poly_specialized) { - continue; - } - } - } - - array_add(&sorted_definitions, e); - } - - gb_sort_array(sorted_definitions.data, sorted_definitions.count, query_data_definition_compare); - definitions->reserve(sorted_definitions.count); - - for_array(i, sorted_definitions) { - Entity *e = sorted_definitions[i]; - String name = e->token.string; - - auto *def = query_value_map(); - def->reserve(16); - definitions->add(def); - - def->add("package", e->pkg->name); - def->add("name", name); - def->add("filepath", get_file_path_string(e->token.pos.file_id)); - def->add("line", cast(i64)e->token.pos.line); - def->add("column", cast(i64)e->token.pos.column); - def->add("file_offset", cast(i64)e->token.pos.offset); - - switch (e->kind) { - case Entity_Constant: def->add("kind", str_lit("constant")); break; - case Entity_Variable: def->add("kind", str_lit("variable")); break; - case Entity_TypeName: def->add("kind", str_lit("type name")); break; - case Entity_Procedure: def->add("kind", str_lit("procedure")); break; - case Entity_ProcGroup: def->add("kind", str_lit("procedure group")); break; - case Entity_ImportName: def->add("kind", str_lit("import name")); break; - case Entity_LibraryName: def->add("kind", str_lit("library name")); break; - default: GB_PANIC("Invalid entity kind to be added"); - } - - - if (e->type != nullptr && e->type != t_invalid) { - Type *t = e->type; - Type *bt = t; - - switch (e->kind) { - case Entity_TypeName: - if (!e->TypeName.is_type_alias) { - bt = base_type(t); - } - break; - } - - { - gbString str = type_to_string(t); - String type_str = make_string(cast(u8 *)str, gb_string_length(str)); - def->add("type", type_str); - } - if (t != bt) { - gbString str = type_to_string(bt); - String type_str = make_string(cast(u8 *)str, gb_string_length(str)); - def->add("base_type", type_str); - } - { - String type_kind = {}; - Type *bt = base_type(t); - switch (bt->kind) { - case Type_Pointer: type_kind = str_lit("pointer"); break; - case Type_Array: type_kind = str_lit("array"); break; - case Type_Slice: type_kind = str_lit("slice"); break; - case Type_DynamicArray: type_kind = str_lit("dynamic array"); break; - case Type_Map: type_kind = str_lit("map"); break; - case Type_Struct: type_kind = str_lit("struct"); break; - case Type_Union: type_kind = str_lit("union"); break; - case Type_Enum: type_kind = str_lit("enum"); break; - case Type_Proc: type_kind = str_lit("procedure"); break; - case Type_BitSet: type_kind = str_lit("bit set"); break; - case Type_SimdVector: type_kind = str_lit("simd vector"); break; - - case Type_Generic: - case Type_Tuple: - GB_PANIC("Invalid definition type"); - break; - } - if (type_kind.len > 0) { - def->add("type_kind", type_kind); - } - } - } - - if (e->kind == Entity_TypeName) { - def->add("size", type_size_of(e->type)); - def->add("align", type_align_of(e->type)); - - - if (is_type_struct(e->type)) { - auto *data = query_value_map(); - data->reserve(6); - - def->add("data", data); - - Type *t = base_type(e->type); - GB_ASSERT(t->kind == Type_Struct); - - if (t->Struct.is_polymorphic) { - data->add("polymorphic", cast(bool)t->Struct.is_polymorphic); - } - if (t->Struct.is_poly_specialized) { - data->add("polymorphic_specialized", cast(bool)t->Struct.is_poly_specialized); - } - if (t->Struct.is_packed) { - data->add("packed", cast(bool)t->Struct.is_packed); - } - if (t->Struct.is_raw_union) { - data->add("raw_union", cast(bool)t->Struct.is_raw_union); - } - - auto *fields = query_value_array(); - data->add("fields", fields); - fields->reserve(t->Struct.fields.count); - fields->packed = true; - - for_array(j, t->Struct.fields) { - Entity *e = t->Struct.fields[j]; - String name = e->token.string; - if (is_blank_ident(name)) { - continue; - } - - fields->add(name); - } - } else if (is_type_union(e->type)) { - auto *data = query_value_map(); - data->reserve(4); - - def->add("data", data); - Type *t = base_type(e->type); - GB_ASSERT(t->kind == Type_Union); - - if (t->Union.is_polymorphic) { - data->add("polymorphic", cast(bool)t->Union.is_polymorphic); - } - if (t->Union.is_poly_specialized) { - data->add("polymorphic_specialized", cast(bool)t->Union.is_poly_specialized); - } - - auto *variants = query_value_array(); - variants->reserve(t->Union.variants.count); - data->add("variants", variants); - - for_array(j, t->Union.variants) { - Type *vt = t->Union.variants[j]; - - gbString str = type_to_string(vt); - String type_str = make_string(cast(u8 *)str, gb_string_length(str)); - variants->add(type_str); - } - } - } - - if (e->kind == Entity_Procedure) { - Type *t = base_type(e->type); - GB_ASSERT(t->kind == Type_Proc); - - bool is_polymorphic = t->Proc.is_polymorphic; - bool is_poly_specialized = t->Proc.is_poly_specialized; - bool ok = is_polymorphic || is_poly_specialized; - if (ok) { - auto *data = query_value_map(); - data->reserve(4); - - def->add("data", data); - if (is_polymorphic) { - data->add("polymorphic", cast(bool)is_polymorphic); - } - if (is_poly_specialized) { - data->add("polymorphic_specialized", cast(bool)is_poly_specialized); - } - } - } - - if (e->kind == Entity_ProcGroup) { - auto *procedures = query_value_array(); - procedures->reserve(e->ProcGroup.entities.count); - - for_array(j, e->ProcGroup.entities) { - Entity *p = e->ProcGroup.entities[j]; - - auto *procedure = query_value_map(); - procedure->reserve(2); - procedure->packed = true; - - procedures->add(procedure); - - procedure->add("package", p->pkg->name); - procedure->add("name", p->token.string); - } - def->add("procedures", procedures); - } - - DeclInfo *di = e->decl_info; - if (di != nullptr) { - if (di->is_using) { - def->add("using", query_value_boolean(true)); - } - } - } - } - - if (build_context.show_timings) { - Timings *t = timings; - timings__stop_current_section(t); - t->total.finish = time_stamp_time_now(); - isize max_len = gb_min(36, t->total.label.len); - for_array(i, t->sections) { - TimeStamp ts = t->sections[i]; - max_len = gb_max(max_len, ts.label.len); - } - t->total_time_seconds = time_stamp_as_s(t->total, t->freq); - - auto *tims = query_value_map(); - tims->reserve(8); - root->add("timings", tims); - tims->add("time_unit", str_lit("s")); - - tims->add(t->total.label, cast(f64)t->total_time_seconds); - - - Parser *p = c->parser; - if (p != nullptr) { - isize lines = p->total_line_count; - isize tokens = p->total_token_count; - isize files = 0; - isize packages = p->packages.count; - isize total_file_size = 0; - for_array(i, p->packages) { - files += p->packages[i]->files.count; - for_array(j, p->packages[i]->files) { - AstFile *file = p->packages[i]->files[j]; - total_file_size += file->tokenizer.end - file->tokenizer.start; - } - } - - tims->add("total_lines", cast(i64)lines); - tims->add("total_tokens", cast(i64)tokens); - tims->add("total_files", cast(i64)files); - tims->add("total_packages", cast(i64)packages); - tims->add("total_file_size", cast(i64)total_file_size); - - auto *sections = query_value_map(); - sections->reserve(t->sections.count); - tims->add("sections", sections); - for_array(i, t->sections) { - TimeStamp ts = t->sections[i]; - f64 section_time = time_stamp_as_s(ts, t->freq); - - auto *section = query_value_map(); - section->reserve(2); - sections->add(ts.label, section); - section->add("time", cast(f64)section_time); - section->add("total_fraction", cast(f64)(section_time/t->total_time_seconds)); - } - } - } - - - print_query_data_as_json(root, !build_context.query_data_set_settings.compact); - gb_printf("\n"); -} - - - -template -struct BinaryArray { - u32 offset; // Offset in bytes from the top of the file - u32 length; // Number of elements in array of type T -}; - -template -gb_internal Array binary_array_from_data(BinaryArray ba, void *data) { - Array res = {}; - res.data = cast(T *)(cast(u8 *)data + ba.offset); - res.count = ba.length; - res.capacity = ba.length; - return res; -} - -typedef BinaryArray BinaryString; - -struct GoToDefIdent { - u64 use_offset; // offset of identifier use in bytes from the start of the file that contains it - u32 len; // length in bytes of the identifier - u32 def_file_id; - u64 def_offset; // offset of entity definition in bytes from the start of the file that contains it -}; - -struct GoToDefFile { - u32 id; - BinaryString path; - BinaryArray idents; -}; - -struct GoToDefHeader { - u8 magic[4]; // ogtd (odin-go-to-definitions) - u32 version; // 1 - BinaryArray files; -}; - -struct GoToDefFileMap { - AstFile *f; - u32 id; - Array idents; -}; - - -gb_internal int go_to_def_file_map_compare(void const *a, void const *b) { - GoToDefFileMap const *x = cast(GoToDefFileMap const *)a; - GoToDefFileMap const *y = cast(GoToDefFileMap const *)b; - if (x == y) { - return 0; - } else if (x != nullptr && y == nullptr) { - return -1; - } else if (x == nullptr && y != nullptr) { - return +1; - } - if (x->f->id < y->f->id) { - return -1; - } else if (x->f->id > y->f->id) { - return +1; - } - return 0; -} - -gb_internal int quick_ident_compare(void const *a, void const *b) { - Ast *x = *cast(Ast **)a; - Ast *y = *cast(Ast **)b; - - // NOTE(bill): This assumes that the file is same - if (x->Ident.token.pos.offset < y->Ident.token.pos.offset) { - return -1; - } else if (x->Ident.token.pos.offset > y->Ident.token.pos.offset) { - return +1; - } - return 0; -} - - -gb_internal void generate_and_print_query_data_go_to_definitions(Checker *c) { - GB_ASSERT(c->info.allow_identifier_uses); - - gbAllocator a = query_value_allocator; - - isize file_path_memory_needed = 0; - auto files = array_make(a, 0, c->info.files.entries.count); - for (auto const &entry : c->info.files) { - AstFile *f = entry.value; - file_path_memory_needed += f->fullpath.len+1; // add NUL terminator - - - GoToDefFileMap x = {}; - x.f = f; - array_init(&x.idents, a); - array_add(&files, x); - } - gb_sort_array(files.data, files.count, go_to_def_file_map_compare); - - auto file_id_map_to_index = array_make(a, files[files.count-1].f->id + 1); - for_array(i, file_id_map_to_index) { - file_id_map_to_index[i] = -1; - } - for_array(i, files) { - file_id_map_to_index[files[i].f->id] = i; - } - - - - for_array(i, c->info.identifier_uses) { - Ast *ast = c->info.identifier_uses[i]; - GB_ASSERT(ast->kind == Ast_Ident); - TokenPos pos = ast->Ident.token.pos; - Entity *e = ast->Ident.entity; - if (e == nullptr) { - continue; - } - - - AstFile **use_file_found = string_map_get(&c->info.files, get_file_path_string(pos.file_id)); - GB_ASSERT(use_file_found != nullptr); - AstFile *use_file = *use_file_found; - GB_ASSERT(use_file != nullptr); - - if (e->scope == nullptr) { - GB_ASSERT(e->flags & EntityFlag_Field); - continue; - } - if (e->scope->flags & ScopeFlag_Global) { - continue; - } - - isize idx = file_id_map_to_index[use_file->id]; - if (idx >= 0) { - array_add(&files[idx].idents, ast); - } else { - // TODO(bill): Handle invalid map case? - } - } - - for_array(i, files) { - GoToDefFileMap *f = &files[i]; - gb_sort_array(f->idents.data, f->idents.count, quick_ident_compare); - // gb_printf_err("%lld %.*s -> %lld\n", f->f->id, LIT(f->f->fullpath), f->idents.count); - } - - - - isize data_min_size = 0; - - u32 header_offset = cast(u32)data_min_size; gb_unused(header_offset); - data_min_size += gb_size_of(GoToDefHeader); - data_min_size = align_formula_isize(data_min_size, 8); - - u32 file_offset = cast(u32)data_min_size; - data_min_size += gb_size_of(GoToDefFile) * files.count; - data_min_size = align_formula_isize(data_min_size, 8); - - u32 file_path_offset = cast(u32)data_min_size; - data_min_size += file_path_memory_needed; - data_min_size = align_formula_isize(data_min_size, 8); - - u32 idents_offset = cast(u32)data_min_size; - data_min_size += gb_size_of(GoToDefIdent) * c->info.identifier_uses.count; - - - auto data = array_make(a, 0, data_min_size); - defer (array_free(&data)); - - GoToDefHeader header = {}; - gb_memmove(header.magic, "ogtd", 4); - header.version = 1; - header.files.length = cast(u32)files.count; - header.files.offset = file_offset; - - array_add_elems(&data, cast(u8 *)&header, gb_size_of(header)); - - array_resize(&data, data_min_size); - - auto binary_files = binary_array_from_data(header.files, data.data); - - u32 file_path_offset_index = file_path_offset; - u32 idents_offset_index = idents_offset; - for_array(i, files) { - GoToDefFileMap *f_map = &files[i]; - AstFile *f = f_map->f; - binary_files[i].id = cast(u32)f->id; - - binary_files[i].path.offset = file_path_offset_index; - binary_files[i].path.length = cast(u32)f->fullpath.len; - - binary_files[i].idents.offset = idents_offset_index; - binary_files[i].idents.length = cast(u32)f_map->idents.count; - - auto path = binary_array_from_data(binary_files[i].path, data.data); - gb_memmove(path.data, f->fullpath.text, f->fullpath.len); - path.data[f->fullpath.len] = 0; - - - auto idents = binary_array_from_data(binary_files[i].idents, data.data); - for_array(j, f_map->idents) { - Ast *ast = f_map->idents[j]; - GB_ASSERT(ast->kind == Ast_Ident); - - Entity *e = ast->Ident.entity; - TokenPos def = e->token.pos; - AstFile *def_file = e->file; - - if (def_file == nullptr) { - auto *def_file_found = string_map_get(&c->info.files, get_file_path_string(e->token.pos.file_id)); - if (def_file_found == nullptr) { - continue; - } - def_file = *def_file_found; - } - - isize file_index = file_id_map_to_index[def_file->id]; - GB_ASSERT(file_index >= 0); - - idents[j].use_offset = cast(u64)ast->Ident.token.pos.offset; - idents[j].len = cast(u32)ast->Ident.token.string.len; - idents[j].def_file_id = cast(u32)def_file->id; - idents[j].def_offset = cast(u64)e->token.pos.offset; - - // gb_printf_err("%llu %llu %llu %llu\n", idents[j].len, idents[j].use_offset, idents[j].def_file_id, idents[j].def_offset); - } - - file_path_offset_index += cast(u32)(f->fullpath.len + 1); - idents_offset_index += cast(u32)(f_map->idents.count * gb_size_of(GoToDefIdent)); - } - - - gb_file_write(gb_file_get_standard(gbFileStandard_Output), data.data, data.count*gb_size_of(*data.data)); -} - diff --git a/src/range_cache.cpp b/src/range_cache.cpp index 1f98c4b9e..fc85e2a2e 100644 --- a/src/range_cache.cpp +++ b/src/range_cache.cpp @@ -59,12 +59,12 @@ gb_internal bool range_cache_add_range(RangeCache *c, i64 lo, i64 hi) { } -gb_internal bool range_cache_index_exists(RangeCache *c, i64 index) { - for_array(i, c->ranges) { - RangeValue v = c->ranges[i]; - if (v.lo <= index && index <= v.hi) { - return true; - } - } - return false; -} +// gb_internal bool range_cache_index_exists(RangeCache *c, i64 index) { +// for_array(i, c->ranges) { +// RangeValue v = c->ranges[i]; +// if (v.lo <= index && index <= v.hi) { +// return true; +// } +// } +// return false; +// } diff --git a/src/string.cpp b/src/string.cpp index aeb31c7b0..8cce0f1ef 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -89,14 +89,6 @@ gb_internal char *alloc_cstring(gbAllocator a, String s) { return c_str; } -gb_internal char *cstring_duplicate(gbAllocator a, char const *s) { - isize len = gb_strlen(s); - char *c_str = gb_alloc_array(a, char, len+1); - gb_memmove(c_str, s, len); - c_str[len] = '\0'; - return c_str; -} - gb_internal gb_inline bool str_eq_ignore_case(String const &a, String const &b) { @@ -166,12 +158,6 @@ gb_internal isize string_index_byte(String const &s, u8 x) { return -1; } -gb_internal GB_COMPARE_PROC(string_cmp_proc) { - String x = *(String *)a; - String y = *(String *)b; - return string_compare(x, y); -} - gb_internal gb_inline bool str_eq(String const &a, String const &b) { if (a.len != b.len) return false; return memcmp(a.text, b.text, a.len) == 0; diff --git a/src/string_map.cpp b/src/string_map.cpp index e289d4c9b..9f9374ece 100644 --- a/src/string_map.cpp +++ b/src/string_map.cpp @@ -18,8 +18,6 @@ gb_internal gb_inline bool string_hash_key_equal(StringHashKey const &a, StringH } return false; } -gb_internal bool operator==(StringHashKey const &a, StringHashKey const &b) { return string_hash_key_equal(a, b); } -gb_internal bool operator!=(StringHashKey const &a, StringHashKey const &b) { return !string_hash_key_equal(a, b); } template struct StringMapEntry { diff --git a/src/threading.cpp b/src/threading.cpp index 511d1b477..b74d087b4 100644 --- a/src/threading.cpp +++ b/src/threading.cpp @@ -1,6 +1,10 @@ #if defined(GB_SYSTEM_LINUX) #include #endif +#if defined(GB_SYSTEM_WINDOWS) + #pragma warning(push) + #pragma warning(disable: 4505) +#endif struct BlockingMutex; struct RecursiveMutex; @@ -269,48 +273,6 @@ struct MutexGuard { } #endif - - -struct Barrier { - BlockingMutex mutex; - Condition cond; - isize index; - isize generation_id; - isize thread_count; -}; - -gb_internal void barrier_init(Barrier *b, isize thread_count) { - mutex_init(&b->mutex); - condition_init(&b->cond); - b->index = 0; - b->generation_id = 0; - b->thread_count = 0; -} - -gb_internal void barrier_destroy(Barrier *b) { - condition_destroy(&b->cond); - mutex_destroy(&b->mutex); -} - -// Returns true if it is the leader -gb_internal bool barrier_wait(Barrier *b) { - mutex_lock(&b->mutex); - defer (mutex_unlock(&b->mutex)); - isize local_gen = b->generation_id; - b->index += 1; - if (b->index < b->thread_count) { - while (local_gen == b->generation_id && b->index < b->thread_count) { - condition_wait(&b->cond, &b->mutex); - } - return false; - } - b->index = 0; - b->generation_id += 1; - condition_broadcast(&b->cond); - return true; -} - - gb_internal u32 thread_current_id(void) { @@ -494,3 +456,7 @@ gb_internal void thread_set_name(Thread *t, char const *name) { #endif } + +#if defined(GB_SYSTEM_WINDOWS) + #pragma warning(pop) +#endif \ No newline at end of file diff --git a/src/types.cpp b/src/types.cpp index c8e24f01d..b1d3883c6 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -430,15 +430,6 @@ gb_internal Selection sub_selection(Selection const &sel, isize offset) { return res; } -gb_internal Selection sub_selection_with_length(Selection const &sel, isize offset, isize len) { - Selection res = {}; - res.index.data = sel.index.data + offset; - res.index.count = gb_max(len, gb_max(sel.index.count - offset, 0)); - res.index.capacity = res.index.count; - return res; -} - - gb_global Type basic_types[] = { {Type_Basic, {Basic_Invalid, 0, 0, STR_LIT("invalid type")}}, @@ -1089,15 +1080,6 @@ gb_internal Type *alloc_type_proc(Scope *scope, Type *params, isize param_count, gb_internal bool is_type_valid_for_keys(Type *t); -gb_internal Type *alloc_type_map(i64 count, Type *key, Type *value) { - if (key != nullptr) { - GB_ASSERT(value != nullptr); - } - Type *t = alloc_type(Type_Map); - t->Map.key = key; - t->Map.value = value; - return t; -} gb_internal Type *alloc_type_bit_set() { Type *t = alloc_type(Type_BitSet); @@ -1152,19 +1134,6 @@ gb_internal bool is_type_named(Type *t) { } return t->kind == Type_Named; } -gb_internal bool is_type_named_alias(Type *t) { - if (!is_type_named(t)) { - return false; - } - Entity *e = t->Named.type_name; - if (e == nullptr) { - return false; - } - if (e->kind != Entity_TypeName) { - return false; - } - return e->TypeName.is_type_alias; -} gb_internal bool is_type_boolean(Type *t) { // t = core_type(t); @@ -1329,27 +1298,6 @@ gb_internal bool is_type_complex_or_quaternion(Type *t) { } return false; } -gb_internal bool is_type_f16(Type *t) { - t = core_type(t); - if (t->kind == Type_Basic) { - return t->Basic.kind == Basic_f16; - } - return false; -} -gb_internal bool is_type_f32(Type *t) { - t = core_type(t); - if (t->kind == Type_Basic) { - return t->Basic.kind == Basic_f32; - } - return false; -} -gb_internal bool is_type_f64(Type *t) { - t = core_type(t); - if (t->kind == Type_Basic) { - return t->Basic.kind == Basic_f64; - } - return false; -} gb_internal bool is_type_pointer(Type *t) { t = base_type(t); if (t->kind == Type_Basic) { @@ -1550,10 +1498,6 @@ gb_internal bool is_type_asm_proc(Type *t) { t = base_type(t); return t->kind == Type_Proc && t->Proc.calling_convention == ProcCC_InlineAsm; } -gb_internal bool is_type_poly_proc(Type *t) { - t = base_type(t); - return t->kind == Type_Proc && t->Proc.is_polymorphic; -} gb_internal bool is_type_simd_vector(Type *t) { t = base_type(t); return t->kind == Type_SimdVector; @@ -1915,11 +1859,6 @@ gb_internal bool is_type_empty_union(Type *t) { t = base_type(t); return t->kind == Type_Union && t->Union.variants.count == 0; } -gb_internal bool is_type_empty_struct(Type *t) { - t = base_type(t); - return t->kind == Type_Struct && !t->Struct.is_raw_union && t->Struct.fields.count == 0; -} - gb_internal bool is_type_valid_for_keys(Type *t) { t = core_type(t); @@ -4051,20 +3990,6 @@ gb_internal Type *reduce_tuple_to_single_type(Type *original_type) { return original_type; } - -gb_internal Type *alloc_type_struct_from_field_types(Type **field_types, isize field_count, bool is_packed) { - Type *t = alloc_type_struct(); - t->Struct.fields = slice_make(heap_allocator(), field_count); - - Scope *scope = nullptr; - for_array(i, t->Struct.fields) { - t->Struct.fields[i] = alloc_entity_field(scope, blank_token, field_types[i], false, cast(i32)i, EntityState_Resolved); - } - t->Struct.is_packed = is_packed; - - return t; -} - gb_internal Type *alloc_type_tuple_from_field_types(Type **field_types, isize field_count, bool is_packed, bool must_be_tuple) { if (field_count == 0) { return nullptr; -- cgit v1.2.3 From 5c519f0e8dada6b15166a257d22a07f2316a394f Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 1 Jan 2023 16:19:21 +0000 Subject: Remove the synchronization primitive init/destroy calls --- src/build_settings.cpp | 1 - src/checker.cpp | 36 ------------------------------------ src/common_memory.cpp | 2 -- src/entity.cpp | 1 - src/error.cpp | 4 ---- src/llvm_backend_general.cpp | 1 - src/main.cpp | 5 ----- src/parser.cpp | 11 ----------- src/queue.cpp | 2 -- src/string.cpp | 5 ----- src/thread_pool.cpp | 5 ----- src/threading.cpp | 35 +---------------------------------- src/types.cpp | 4 ---- 13 files changed, 1 insertion(+), 111 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 080e9dddc..97b512b81 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -1363,7 +1363,6 @@ gb_internal bool init_build_paths(String init_filename) { array_init(&bc->build_paths, permanent_allocator(), BuildPathCOUNT); string_set_init(&bc->target_features_set, heap_allocator(), 1024); - mutex_init(&bc->target_features_mutex); // [BuildPathMainPackage] Turn given init path into a `Path`, which includes normalizing it into a full path. bc->build_paths[BuildPath_Main_Package] = path_from_string(ha, init_filename); diff --git a/src/checker.cpp b/src/checker.cpp index b78da2827..7141b0698 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -184,7 +184,6 @@ gb_internal void init_decl_info(DeclInfo *d, Scope *scope, DeclInfo *parent) { ptr_set_init(&d->deps, heap_allocator()); ptr_set_init(&d->type_info_deps, heap_allocator()); array_init (&d->labels, heap_allocator()); - mutex_init(&d->proc_checked_mutex); } gb_internal DeclInfo *make_decl_info(Scope *scope, DeclInfo *parent) { @@ -225,7 +224,6 @@ gb_internal Scope *create_scope(CheckerInfo *info, Scope *parent, isize init_ele s->parent = parent; string_map_init(&s->elements, heap_allocator(), init_elements_capacity); ptr_set_init(&s->imported, heap_allocator(), 0); - mutex_init(&s->mutex); if (parent != nullptr && parent != builtin_pkg->scope) { Scope *prev_head_child = parent->head_child.exchange(s, std::memory_order_acq_rel); @@ -306,7 +304,6 @@ gb_internal void destroy_scope(Scope *scope) { string_map_destroy(&scope->elements); ptr_set_destroy(&scope->imported); - mutex_destroy(&scope->mutex); // NOTE(bill): No need to free scope as it "should" be allocated in an arena (except for the global scope) } @@ -1134,24 +1131,9 @@ gb_internal void init_checker_info(CheckerInfo *i) { TIME_SECTION("checker info: mutexes"); - mutex_init(&i->gen_procs_mutex); - mutex_init(&i->gen_types_mutex); - mutex_init(&i->lazy_mutex); - mutex_init(&i->builtin_mutex); - mutex_init(&i->global_untyped_mutex); - mutex_init(&i->type_info_mutex); - mutex_init(&i->deps_mutex); - mutex_init(&i->type_and_value_mutex); - mutex_init(&i->identifier_uses_mutex); - mutex_init(&i->foreign_mutex); - - semaphore_init(&i->collect_semaphore); - mpmc_init(&i->intrinsics_entry_point_usage, a, 1<<10); // just waste some memory here, even if it probably never used - mutex_init(&i->objc_types_mutex); map_init(&i->objc_msgSend_types, a); - mutex_init(&i->load_file_mutex); string_map_init(&i->load_file_cache, a); } @@ -1175,20 +1157,7 @@ gb_internal void destroy_checker_info(CheckerInfo *i) { mpmc_destroy(&i->required_global_variable_queue); mpmc_destroy(&i->required_foreign_imports_through_force_queue); - mutex_destroy(&i->gen_procs_mutex); - mutex_destroy(&i->gen_types_mutex); - mutex_destroy(&i->lazy_mutex); - mutex_destroy(&i->builtin_mutex); - mutex_destroy(&i->global_untyped_mutex); - mutex_destroy(&i->type_info_mutex); - mutex_destroy(&i->deps_mutex); - mutex_destroy(&i->type_and_value_mutex); - mutex_destroy(&i->identifier_uses_mutex); - mutex_destroy(&i->foreign_mutex); - - mutex_destroy(&i->objc_types_mutex); map_destroy(&i->objc_msgSend_types); - mutex_init(&i->load_file_mutex); string_map_destroy(&i->load_file_cache); } @@ -1201,11 +1170,9 @@ gb_internal CheckerContext make_checker_context(Checker *c) { ctx.type_path = new_checker_type_path(); ctx.type_level = 0; - mutex_init(&ctx.mutex); return ctx; } gb_internal void destroy_checker_context(CheckerContext *ctx) { - mutex_destroy(&ctx->mutex); destroy_checker_type_path(ctx->type_path); } @@ -1264,7 +1231,6 @@ gb_internal void init_checker(Checker *c) { // NOTE(bill): 1 Mi elements should be enough on average mpmc_init(&c->procs_to_check_queue, heap_allocator(), 1<<20); - semaphore_init(&c->procs_to_check_semaphore); mpmc_init(&c->global_untyped_queue, a, 1<<20); @@ -1277,8 +1243,6 @@ gb_internal void destroy_checker(Checker *c) { destroy_checker_context(&c->builtin_ctx); mpmc_destroy(&c->procs_to_check_queue); - semaphore_destroy(&c->procs_to_check_semaphore); - mpmc_destroy(&c->global_untyped_queue); } diff --git a/src/common_memory.cpp b/src/common_memory.cpp index c8a62756a..2022554cf 100644 --- a/src/common_memory.cpp +++ b/src/common_memory.cpp @@ -42,8 +42,6 @@ gb_global BlockingMutex global_memory_allocator_mutex; gb_internal void platform_virtual_memory_init(void); gb_internal void virtual_memory_init(void) { - mutex_init(&global_memory_block_mutex); - mutex_init(&global_memory_allocator_mutex); platform_virtual_memory_init(); } diff --git a/src/entity.cpp b/src/entity.cpp index 0605a293a..f82a2fb05 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -154,7 +154,6 @@ struct TypeNameObjCMetadata { gb_internal TypeNameObjCMetadata *create_type_name_obj_c_metadata() { TypeNameObjCMetadata *md = gb_alloc_item(permanent_allocator(), TypeNameObjCMetadata); md->mutex = gb_alloc_item(permanent_allocator(), BlockingMutex); - mutex_init(md->mutex); array_init(&md->type_entries, heap_allocator()); array_init(&md->value_entries, heap_allocator()); return md; diff --git a/src/error.cpp b/src/error.cpp index 085e1a8dd..a0bb4ad5b 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -22,10 +22,6 @@ gb_internal bool any_errors(void) { } gb_internal void init_global_error_collector(void) { - mutex_init(&global_error_collector.mutex); - mutex_init(&global_error_collector.block_mutex); - mutex_init(&global_error_collector.error_out_mutex); - mutex_init(&global_error_collector.string_mutex); array_init(&global_error_collector.errors, heap_allocator()); array_init(&global_error_collector.error_buffer, heap_allocator()); array_init(&global_file_path_strings, heap_allocator(), 1, 4096); diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index e5aa95f10..0508c6171 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -132,7 +132,6 @@ gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) { map_init(&gen->anonymous_proc_lits, heap_allocator(), 1024); - mutex_init(&gen->foreign_mutex); array_init(&gen->foreign_libraries, heap_allocator(), 0, 1024); ptr_set_init(&gen->foreign_libraries_set, heap_allocator(), 1024); diff --git a/src/main.cpp b/src/main.cpp index 6d910c7bf..184ab471e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2498,15 +2498,10 @@ int main(int arg_count, char const **arg_ptr) { MAIN_TIME_SECTION("initialization"); virtual_memory_init(); - mutex_init(&fullpath_mutex); - mutex_init(&hash_exact_value_mutex); - mutex_init(&global_type_name_objc_metadata_mutex); - init_string_buffer_memory(); init_string_interner(); init_global_error_collector(); init_keyword_hash_table(); - init_type_mutex(); if (!check_env()) { return 1; diff --git a/src/parser.cpp b/src/parser.cpp index e07f26004..344dcb20d 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -4858,10 +4858,6 @@ gb_internal bool init_parser(Parser *p) { GB_ASSERT(p != nullptr); string_set_init(&p->imported_files, heap_allocator()); array_init(&p->packages, heap_allocator()); - mutex_init(&p->imported_files_mutex); - mutex_init(&p->file_decl_mutex); - mutex_init(&p->packages_mutex); - mutex_init(&p->file_error_mutex); return true; } @@ -4878,10 +4874,6 @@ gb_internal void destroy_parser(Parser *p) { } array_free(&p->packages); string_set_destroy(&p->imported_files); - mutex_destroy(&p->imported_files_mutex); - mutex_destroy(&p->file_decl_mutex); - mutex_destroy(&p->packages_mutex); - mutex_destroy(&p->file_error_mutex); } @@ -4978,9 +4970,6 @@ gb_internal AstPackage *try_add_import_path(Parser *p, String const &path, Strin pkg->fullpath = path; array_init(&pkg->files, heap_allocator()); pkg->foreign_files.allocator = heap_allocator(); - mutex_init(&pkg->files_mutex); - mutex_init(&pkg->foreign_files_mutex); - // NOTE(bill): Single file initial package if (kind == Package_Init && string_ends_with(path, FILE_EXT)) { diff --git a/src/queue.cpp b/src/queue.cpp index 4de5ac5e5..8f279bb21 100644 --- a/src/queue.cpp +++ b/src/queue.cpp @@ -52,7 +52,6 @@ gb_internal void mpmc_init(MPMCQueue *q, gbAllocator a, isize size_i) { size = next_pow2(size); GB_ASSERT(gb_is_power_of_two(size)); - mutex_init(&q->mutex); q->mask = size-1; q->allocator = a; q->nodes = gb_alloc_array(a, T, size); @@ -65,7 +64,6 @@ gb_internal void mpmc_init(MPMCQueue *q, gbAllocator a, isize size_i) { template gb_internal void mpmc_destroy(MPMCQueue *q) { - mutex_destroy(&q->mutex); gb_free(q->allocator, q->nodes); gb_free(q->allocator, q->indices); } diff --git a/src/string.cpp b/src/string.cpp index 8cce0f1ef..a2254d100 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -1,10 +1,5 @@ gb_global BlockingMutex string_buffer_mutex = {}; -gb_internal void init_string_buffer_memory(void) { - mutex_init(&string_buffer_mutex); -} - - // NOTE(bill): Used for UTF-8 strings struct String { u8 * text; diff --git a/src/thread_pool.cpp b/src/thread_pool.cpp index 57ed5e3c5..522b96d09 100644 --- a/src/thread_pool.cpp +++ b/src/thread_pool.cpp @@ -23,9 +23,6 @@ struct ThreadPool { }; gb_internal void thread_pool_init(ThreadPool *pool, gbAllocator const &a, isize thread_count, char const *worker_name) { - mutex_init(&pool->task_lock); - condition_init(&pool->tasks_available); - pool->allocator = a; slice_init(&pool->threads, a, thread_count + 1); @@ -54,8 +51,6 @@ gb_internal void thread_pool_destroy(ThreadPool *pool) { } gb_free(pool->allocator, pool->threads.data); - mutex_destroy(&pool->task_lock); - condition_destroy(&pool->tasks_available); } void thread_pool_queue_push(Thread *thread, WorkerTask task) { diff --git a/src/threading.cpp b/src/threading.cpp index 7dd1247e7..fb71a2c29 100644 --- a/src/threading.cpp +++ b/src/threading.cpp @@ -48,30 +48,22 @@ gb_internal void futex_wait(Futex *addr, Footex val); gb_internal void futex_signal(Futex *addr); gb_internal void futex_broadcast(Futex *addr); -gb_internal void mutex_init (BlockingMutex *m); -gb_internal void mutex_destroy (BlockingMutex *m); gb_internal void mutex_lock (BlockingMutex *m); gb_internal bool mutex_try_lock(BlockingMutex *m); gb_internal void mutex_unlock (BlockingMutex *m); -gb_internal void mutex_init (RecursiveMutex *m); -gb_internal void mutex_destroy (RecursiveMutex *m); + gb_internal void mutex_lock (RecursiveMutex *m); gb_internal bool mutex_try_lock(RecursiveMutex *m); gb_internal void mutex_unlock (RecursiveMutex *m); -gb_internal void semaphore_init (Semaphore *s); -gb_internal void semaphore_destroy(Semaphore *s); gb_internal void semaphore_post (Semaphore *s, i32 count); gb_internal void semaphore_wait (Semaphore *s); gb_internal void semaphore_release(Semaphore *s) { semaphore_post(s, 1); } -gb_internal void condition_init(Condition *c); -gb_internal void condition_destroy(Condition *c); gb_internal void condition_broadcast(Condition *c); gb_internal void condition_signal(Condition *c); gb_internal void condition_wait(Condition *c, BlockingMutex *m); -gb_internal void condition_wait_with_timeout(Condition *c, BlockingMutex *m, u32 timeout_in_ms); gb_internal u32 thread_current_id(void); @@ -122,12 +114,7 @@ struct RecursiveMutex { Futex owner; i32 recursion; }; -gb_internal void mutex_init(RecursiveMutex *m) { - -} -gb_internal void mutex_destroy(RecursiveMutex *m) { -} gb_internal void mutex_lock(RecursiveMutex *m) { Futex tid = cast(i32)thread_current_id(); for (;;) { @@ -166,12 +153,6 @@ struct Semaphore { Futex count; }; -gb_internal void semaphore_init(Semaphore *s) { - -} -gb_internal void semaphore_destroy(Semaphore *s) { - -} gb_internal void semaphore_post(Semaphore *s, i32 count) { s->count.fetch_add(count, std::memory_order_release); if (s->count == 1) { @@ -198,10 +179,6 @@ gb_internal void semaphore_wait(Semaphore *s) { struct BlockingMutex { SRWLOCK srwlock; }; - gb_internal void mutex_init(BlockingMutex *m) { - } - gb_internal void mutex_destroy(BlockingMutex *m) { - } gb_internal void mutex_lock(BlockingMutex *m) { AcquireSRWLockExclusive(&m->srwlock); } @@ -229,10 +206,6 @@ gb_internal void semaphore_wait(Semaphore *s) { gb_internal void condition_wait(Condition *c, BlockingMutex *m) { SleepConditionVariableSRW(&c->cond, &m->srwlock, INFINITE, 0); } - gb_internal void condition_wait_with_timeout(Condition *c, BlockingMutex *m, u32 timeout_in_ms) { - SleepConditionVariableSRW(&c->cond, &m->srwlock, timeout_in_ms, 0); - } - #else enum Internal_Mutex_State : i32 { Internal_Mutex_State_Unlocked = 0, @@ -251,9 +224,6 @@ gb_internal void semaphore_wait(Semaphore *s) { } }; - gb_internal void mutex_init(BlockingMutex *m) {}; - gb_internal void mutex_destroy(BlockingMutex *m) {}; - gb_no_inline gb_internal void mutex_lock_slow(BlockingMutex *m, i32 curr_state) { i32 new_state = curr_state; for (i32 spin = 0; spin < 100; spin++) { @@ -323,9 +293,6 @@ gb_internal void semaphore_wait(Semaphore *s) { } }; - gb_internal void condition_init(Condition *c) {} - gb_internal void condition_destroy(Condition *c) {} - gb_internal void condition_broadcast(Condition *c) { c->state().fetch_add(1, std::memory_order_release); futex_broadcast(&c->state()); diff --git a/src/types.cpp b/src/types.cpp index 5bddfc79e..afe0b7d5d 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -808,10 +808,6 @@ gb_internal void type_path_pop(TypePath *tp) { #define FAILURE_SIZE 0 #define FAILURE_ALIGNMENT 0 -gb_internal void init_type_mutex(void) { - mutex_init(&g_type_mutex); -} - gb_internal bool type_ptr_set_update(PtrSet *s, Type *t) { if (ptr_set_exists(s, t)) { return true; -- cgit v1.2.3 From 529383f5b17d74f66bebb8679820a69476635b6a Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 2 Jan 2023 15:30:04 +0000 Subject: Correct a race condition when checking the procedure body --- src/build_settings.cpp | 1 + src/check_decl.cpp | 25 ++--- src/check_expr.cpp | 23 ++++- src/checker.cpp | 224 ++++++++++++++++++++++++++++++++++--------- src/checker.hpp | 21 +++- src/llvm_backend.cpp | 6 ++ src/llvm_backend.hpp | 1 + src/llvm_backend_expr.cpp | 6 +- src/llvm_backend_general.cpp | 5 +- src/llvm_backend_proc.cpp | 3 +- src/llvm_backend_stmt.cpp | 6 +- src/main.cpp | 5 + 12 files changed, 260 insertions(+), 66 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 97b512b81..f59b5c0f7 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -291,6 +291,7 @@ struct BuildContext { bool show_error_line; bool ignore_lazy; + bool ignore_llvm_build; bool use_subsystem_windows; bool ignore_microsoft_magic; diff --git a/src/check_decl.cpp b/src/check_decl.cpp index 4e3c1b405..8f95c1a49 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -1419,9 +1419,9 @@ struct ProcUsingVar { }; -gb_internal void check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *decl, Type *type, Ast *body) { +gb_internal bool check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *decl, Type *type, Ast *body) { if (body == nullptr) { - return; + return false; } GB_ASSERT(body->kind == Ast_BlockStmt); @@ -1502,7 +1502,7 @@ gb_internal void check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *de MUTEX_GUARD_BLOCK(ctx->scope->mutex) for_array(i, using_entities) { Entity *e = using_entities[i].e; Entity *uvar = using_entities[i].uvar; - Entity *prev = scope_insert(ctx->scope, uvar, false); + Entity *prev = scope_insert_no_mutex(ctx->scope, uvar); if (prev != nullptr) { error(e->token, "Namespace collision while 'using' procedure argument '%.*s' of: %.*s", LIT(e->token.string), LIT(prev->token.string)); error_line("%.*s != %.*s\n", LIT(uvar->token.string), LIT(prev->token.string)); @@ -1514,7 +1514,7 @@ gb_internal void check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *de bool where_clause_ok = evaluate_where_clauses(ctx, nullptr, decl->scope, &decl->proc_lit->ProcLit.where_clauses, !decl->where_clauses_evaluated); if (!where_clause_ok) { // NOTE(bill, 2019-08-31): Don't check the body as the where clauses failed - return; + return false; } check_open_scope(ctx, body); @@ -1526,7 +1526,12 @@ gb_internal void check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *de // NOTE(bill): Don't err here } - GB_ASSERT(decl->defer_use_checked == false); + GB_ASSERT(decl->proc_checked_state != ProcCheckedState_Checked); + if (decl->defer_use_checked) { + GB_ASSERT(is_type_polymorphic(type, true)); + error(token, "Defer Use Checked: %.*s", LIT(decl->entity->token.string)); + GB_ASSERT(decl->defer_use_checked == false); + } check_stmt_list(ctx, bs->stmts, Stmt_CheckScopeDecls); @@ -1575,10 +1580,8 @@ gb_internal void check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *de if (decl->parent != nullptr) { Scope *ps = decl->parent->scope; if (ps->flags & (ScopeFlag_File & ScopeFlag_Pkg & ScopeFlag_Global)) { - return; - } else { - mutex_lock(&ctx->info->deps_mutex); - + return true; + } else MUTEX_GUARD_BLOCK(&ctx->info->deps_mutex) { // NOTE(bill): Add the dependencies from the procedure literal (lambda) // But only at the procedure level for (auto const &entry : decl->deps) { @@ -1589,8 +1592,8 @@ gb_internal void check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *de Type *t = entry.ptr; ptr_set_add(&decl->parent->type_info_deps, t); } - - mutex_unlock(&ctx->info->deps_mutex); } } + + return true; } diff --git a/src/check_expr.cpp b/src/check_expr.cpp index eb9f76547..5445e73c7 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -86,7 +86,6 @@ gb_internal Entity * find_polymorphic_record_entity (CheckerContext *c, Type *or gb_internal void check_not_tuple (CheckerContext *c, Operand *operand); gb_internal void convert_to_typed (CheckerContext *c, Operand *operand, Type *target_type); gb_internal gbString expr_to_string (Ast *expression); -gb_internal void check_proc_body (CheckerContext *c, Token token, DeclInfo *decl, Type *type, Ast *body); gb_internal void update_untyped_expr_type (CheckerContext *c, Ast *e, Type *type, bool final); gb_internal bool check_is_terminating (Ast *node, String const &label); gb_internal bool check_has_break (Ast *stmt, String const &label, bool implicit); @@ -478,6 +477,22 @@ gb_internal bool find_or_generate_polymorphic_procedure(CheckerContext *old_c, E if (poly_proc_data) { poly_proc_data->gen_entity = other; } + + DeclInfo *decl = other->decl_info; + if (decl->proc_checked_state != ProcCheckedState_Checked) { + ProcInfo *proc_info = gb_alloc_item(permanent_allocator(), ProcInfo); + proc_info->file = other->file; + proc_info->token = other->token; + proc_info->decl = decl; + proc_info->type = other->type; + proc_info->body = decl->proc_lit->ProcLit.body; + proc_info->tags = other->Procedure.tags;; + proc_info->generated_from_polymorphic = true; + proc_info->poly_def_node = poly_def_node; + + check_procedure_later(nctx.checker, proc_info); + } + return true; } } @@ -518,7 +533,8 @@ gb_internal bool find_or_generate_polymorphic_procedure(CheckerContext *old_c, E d->gen_proc_type = final_proc_type; d->type_expr = pl->type; d->proc_lit = proc_lit; - d->proc_checked = false; + d->proc_checked_state = ProcCheckedState_Unchecked; + d->defer_use_checked = false; Entity *entity = alloc_entity_procedure(nullptr, token, final_proc_type, tags); entity->identifier = ident; @@ -528,7 +544,8 @@ gb_internal bool find_or_generate_polymorphic_procedure(CheckerContext *old_c, E entity->scope = scope->parent; entity->file = base_entity->file; entity->pkg = base_entity->pkg; - entity->flags &= ~EntityFlag_ProcBodyChecked; + entity->flags = 0; + d->entity = entity; AstFile *file = nullptr; { diff --git a/src/checker.cpp b/src/checker.cpp index d3a9c3d2c..f4c9b6822 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -1,3 +1,5 @@ +#define DEBUG_CHECK_ALL_PROCEDURES 1 + #include "entity.cpp" #include "types.cpp" @@ -179,6 +181,7 @@ gb_internal void import_graph_node_swap(ImportGraphNode **data, isize i, isize j gb_internal void init_decl_info(DeclInfo *d, Scope *scope, DeclInfo *parent) { + gb_zero_item(d); d->parent = parent; d->scope = scope; ptr_set_init(&d->deps, heap_allocator()); @@ -438,9 +441,44 @@ gb_internal Entity *scope_lookup(Scope *s, String const &name) { return entity; } +gb_internal Entity *scope_insert_with_name_no_mutex(Scope *s, String const &name, Entity *entity) { + if (name == "") { + return nullptr; + } + StringHashKey key = string_hash_string(name); + Entity **found = nullptr; + Entity *result = nullptr; + + found = string_map_get(&s->elements, key); + + if (found) { + if (entity != *found) { + result = *found; + } + goto end; + } + if (s->parent != nullptr && (s->parent->flags & ScopeFlag_Proc) != 0) { + found = string_map_get(&s->parent->elements, key); + if (found) { + if ((*found)->flags & EntityFlag_Result) { + if (entity != *found) { + result = *found; + } + goto end; + } + } + } + + string_map_set(&s->elements, key, entity); + if (entity->scope == nullptr) { + entity->scope = s; + } +end:; + return result; +} -gb_internal Entity *scope_insert_with_name(Scope *s, String const &name, Entity *entity, bool use_mutex=true) { +gb_internal Entity *scope_insert_with_name(Scope *s, String const &name, Entity *entity) { if (name == "") { return nullptr; } @@ -448,9 +486,8 @@ gb_internal Entity *scope_insert_with_name(Scope *s, String const &name, Entity Entity **found = nullptr; Entity *result = nullptr; - if (use_mutex) mutex_lock(&s->mutex); - defer (if (use_mutex) mutex_unlock(&s->mutex)); - + MUTEX_GUARD(&s->mutex); + found = string_map_get(&s->elements, key); if (found) { @@ -479,9 +516,14 @@ end:; return result; } -gb_internal Entity *scope_insert(Scope *s, Entity *entity, bool use_mutex) { +gb_internal Entity *scope_insert(Scope *s, Entity *entity) { String name = entity->token.string; - return scope_insert_with_name(s, name, entity, use_mutex); + return scope_insert_with_name(s, name, entity); +} + +gb_internal Entity *scope_insert_no_mutex(Scope *s, Entity *entity) { + String name = entity->token.string; + return scope_insert_with_name_no_mutex(s, name, entity); } @@ -1135,6 +1177,9 @@ gb_internal void init_checker_info(CheckerInfo *i) { map_init(&i->objc_msgSend_types, a); string_map_init(&i->load_file_cache, a); + + array_init(&i->all_procedures, heap_allocator()); + } gb_internal void destroy_checker_info(CheckerInfo *i) { @@ -1934,6 +1979,7 @@ gb_internal void add_type_info_type_internal(CheckerContext *c, Type *t) { gb_global std::atomic global_procedure_body_in_worker_queue = false; +gb_global std::atomic global_after_checking_procedure_bodies = false; gb_internal WORKER_TASK_PROC(check_proc_info_worker_proc); @@ -1941,12 +1987,24 @@ gb_internal void check_procedure_later(Checker *c, ProcInfo *info) { GB_ASSERT(info != nullptr); GB_ASSERT(info->decl != nullptr); - if (global_procedure_body_in_worker_queue) { + if (global_after_checking_procedure_bodies) { + Entity *e = info->decl->entity; + debugf("CHECK PROCEDURE LATER! %.*s :: %s {...}\n", LIT(e->token.string), type_to_string(e->type)); + } + + if (global_procedure_body_in_worker_queue.load()) { thread_pool_add_task(check_proc_info_worker_proc, info); } else { - GB_ASSERT(global_procedure_body_in_worker_queue == false); array_add(&c->procs_to_check, info); } + + if (DEBUG_CHECK_ALL_PROCEDURES) { + MUTEX_GUARD_BLOCK(&c->info.all_procedures_mutex) { + GB_ASSERT(info != nullptr); + GB_ASSERT(info->decl != nullptr); + array_add(&c->info.all_procedures, info); + } + } } gb_internal void check_procedure_later(Checker *c, AstFile *file, Token token, DeclInfo *decl, Type *type, Ast *body, u64 tags) { @@ -5010,24 +5068,26 @@ gb_internal bool check_proc_info(Checker *c, ProcInfo *pi, UntypedExprInfoMap *u if (pi->type == nullptr) { return false; } - Entity *e = pi->decl->entity; - MUTEX_GUARD_BLOCK(&pi->decl->proc_checked_mutex) { - if (pi->decl->proc_checked) { - if (e != nullptr) { - GB_ASSERT(e->flags & EntityFlag_ProcBodyChecked); - } - return true; - } - if (e != nullptr && (e->flags & EntityFlag_ProcBodyChecked) != 0) { - GB_ASSERT(pi->decl->proc_checked); - return true; + MUTEX_GUARD(&pi->decl->proc_checked_mutex); + + Entity *e = pi->decl->entity; + switch (pi->decl->proc_checked_state.load()) { + case ProcCheckedState_InProgress: + if (e) { + GB_ASSERT(global_procedure_body_in_worker_queue.load()); } - pi->decl->proc_checked = true; + return false; + case ProcCheckedState_Checked: if (e != nullptr) { - e->flags |= EntityFlag_ProcBodyChecked; + GB_ASSERT(e->flags & EntityFlag_ProcBodyChecked); } + return true; + case ProcCheckedState_Unchecked: + // okay + break; } + pi->decl->proc_checked_state.store(ProcCheckedState_InProgress); GB_ASSERT(pi->type->kind == Type_Proc); TypeProc *pt = &pi->type->Proc; @@ -5039,17 +5099,21 @@ gb_internal bool check_proc_info(Checker *c, ProcInfo *pi, UntypedExprInfoMap *u token = ast_token(pi->poly_def_node); } error(token, "Unspecialized polymorphic procedure '%.*s'", LIT(name)); + pi->decl->proc_checked_state.store(ProcCheckedState_Unchecked); return false; } if (pt->is_polymorphic && pt->is_poly_specialized) { + Entity *e = pi->decl->entity; + GB_ASSERT(e != nullptr); if ((e->flags & EntityFlag_Used) == 0) { // NOTE(bill, 2019-08-31): It was never used, don't check + // NOTE(bill, 2023-01-02): This may need to be checked again if it is used elsewhere? + pi->decl->proc_checked_state.store(ProcCheckedState_Unchecked); return false; } } - CheckerContext ctx = make_checker_context(c); defer (destroy_checker_context(&ctx)); reset_checker_context(&ctx, pi->file, untyped); @@ -5077,14 +5141,34 @@ gb_internal bool check_proc_info(Checker *c, ProcInfo *pi, UntypedExprInfoMap *u ctx.state_flags &= ~StateFlag_type_assert; } - check_proc_body(&ctx, pi->token, pi->decl, pi->type, pi->body); + bool body_was_checked = check_proc_body(&ctx, pi->token, pi->decl, pi->type, pi->body); + + if (body_was_checked) { + pi->decl->proc_checked_state.store(ProcCheckedState_Checked); + if (pi->body) { + Entity *e = pi->decl->entity; + if (e != nullptr) { + e->flags |= EntityFlag_ProcBodyChecked; + } + } + } else { + pi->decl->proc_checked_state.store(ProcCheckedState_Unchecked); + if (pi->body) { + Entity *e = pi->decl->entity; + if (e != nullptr) { + e->flags &= ~EntityFlag_ProcBodyChecked; + } + } + } + add_untyped_expressions(&c->info, ctx.untyped); + return true; } GB_STATIC_ASSERT(sizeof(isize) == sizeof(void *)); -gb_internal bool consume_proc_info_queue(Checker *c, ProcInfo *pi, UntypedExprInfoMap *untyped); +gb_internal bool consume_proc_info(Checker *c, ProcInfo *pi, UntypedExprInfoMap *untyped); gb_internal void check_unchecked_bodies(Checker *c) { // NOTE(2021-02-26, bill): Sanity checker @@ -5092,10 +5176,15 @@ gb_internal void check_unchecked_bodies(Checker *c) { // even ones which should not exist, due to the multithreaded nature of the parser // HACK TODO(2021-02-26, bill): Actually fix this race condition + GB_ASSERT(c->procs_to_check.count == 0); + UntypedExprInfoMap untyped = {}; map_init(&untyped, heap_allocator()); defer (map_destroy(&untyped)); + // use the `procs_to_check` array + global_procedure_body_in_worker_queue = false; + for (auto const &entry : c->info.minimum_dependency_set) { Entity *e = entry.ptr; if (e == nullptr || e->kind != Entity_Procedure) { @@ -5122,19 +5211,22 @@ gb_internal void check_unchecked_bodies(Checker *c) { } debugf("unchecked: %.*s\n", LIT(e->token.string)); - array_add(&c->procs_to_check, pi); + check_procedure_later(c, pi); } } - for (ProcInfo *pi : c->procs_to_check) { - Entity *e = pi->decl->entity; - if (consume_proc_info_queue(c, pi, &untyped)) { - add_dependency_to_set(c, e); - GB_ASSERT(e->flags & EntityFlag_ProcBodyChecked); + if (!global_procedure_body_in_worker_queue) { + for_array(i, c->procs_to_check) { + ProcInfo *pi = c->procs_to_check[i]; + consume_proc_info(c, pi, &untyped); } + array_clear(&c->procs_to_check); + } else { + thread_pool_wait(); } - thread_pool_wait(); + global_procedure_body_in_worker_queue = false; + global_after_checking_procedure_bodies = true; } gb_internal void check_test_procedures(Checker *c) { @@ -5172,8 +5264,15 @@ gb_internal void check_test_procedures(Checker *c) { gb_global std::atomic total_bodies_checked; -gb_internal bool consume_proc_info_queue(Checker *c, ProcInfo *pi, UntypedExprInfoMap *untyped) { +gb_internal bool consume_proc_info(Checker *c, ProcInfo *pi, UntypedExprInfoMap *untyped) { GB_ASSERT(pi->decl != nullptr); + switch (pi->decl->proc_checked_state.load()) { + case ProcCheckedState_InProgress: + return false; + case ProcCheckedState_Checked: + return true; + } + if (pi->decl->parent && pi->decl->parent->entity) { Entity *parent = pi->decl->parent->entity; // NOTE(bill): Only check a nested procedure if its parent's body has been checked first @@ -5187,9 +5286,11 @@ gb_internal bool consume_proc_info_queue(Checker *c, ProcInfo *pi, UntypedExprIn if (untyped) { map_clear(untyped); } - bool ok = check_proc_info(c, pi, untyped); - total_bodies_checked.fetch_add(1, std::memory_order_relaxed); - return ok; + if (check_proc_info(c, pi, untyped)) { + total_bodies_checked.fetch_add(1, std::memory_order_relaxed); + return true; + } + return false; } struct CheckProcedureBodyWorkerData { @@ -5218,9 +5319,11 @@ gb_internal WORKER_TASK_PROC(check_proc_info_worker_proc) { } } map_clear(untyped); - bool ok = check_proc_info(c, pi, untyped); - total_bodies_checked.fetch_add(1, std::memory_order_relaxed); - return !ok; + if (check_proc_info(c, pi, untyped)) { + total_bodies_checked.fetch_add(1, std::memory_order_relaxed); + return 0; + } + return 1; } @@ -5247,7 +5350,7 @@ gb_internal void check_procedure_bodies(Checker *c) { if (thread_count == 1) { UntypedExprInfoMap *untyped = &check_procedure_bodies_worker_data[0].untyped; for_array(i, c->procs_to_check) { - consume_proc_info_queue(c, c->procs_to_check[i], untyped); + consume_proc_info(c, c->procs_to_check[i], untyped); } array_clear(&c->procs_to_check); @@ -5266,9 +5369,6 @@ gb_internal void check_procedure_bodies(Checker *c) { thread_pool_wait(); - debugf("Total Procedure Bodies Checked: %td\n", total_bodies_checked.load(std::memory_order_relaxed)); - - global_procedure_body_in_worker_queue = false; } gb_internal void add_untyped_expressions(CheckerInfo *cinfo, UntypedExprInfoMap *untyped) { @@ -5662,9 +5762,6 @@ gb_internal void check_parsed_files(Checker *c) { TIME_SECTION("check test procedures"); check_test_procedures(c); - TIME_SECTION("check bodies have all been checked"); - check_unchecked_bodies(c); - TIME_SECTION("add type info for type definitions"); add_type_info_for_type_definitions(c); check_merge_queues_into_arrays(c); @@ -5672,6 +5769,12 @@ gb_internal void check_parsed_files(Checker *c) { TIME_SECTION("generate minimum dependency set"); generate_minimum_dependency_set(c, c->info.entry_point); + TIME_SECTION("check bodies have all been checked"); + check_unchecked_bodies(c); + + check_merge_queues_into_arrays(c); + + TIME_SECTION("check entry point"); if (build_context.build_mode == BuildMode_Executable && !build_context.no_entry_point && build_context.command_kind != Command_test) { Scope *s = c->info.init_scope; @@ -5694,11 +5797,39 @@ gb_internal void check_parsed_files(Checker *c) { } } + thread_pool_wait(); + GB_ASSERT(c->procs_to_check.count == 0); + + if (DEBUG_CHECK_ALL_PROCEDURES) { + UntypedExprInfoMap untyped = {}; + map_init(&untyped, heap_allocator()); + defer (map_destroy(&untyped)); + + for_array(i, c->info.all_procedures) { + ProcInfo *pi = c->info.all_procedures[i]; + GB_ASSERT(pi != nullptr); + GB_ASSERT(pi->decl != nullptr); + Entity *e = pi->decl->entity; + auto proc_checked_state = pi->decl->proc_checked_state.load(); + if (e && ((e->flags & EntityFlag_ProcBodyChecked) == 0)) { + if ((e->flags & EntityFlag_Used) != 0) { + debugf("%.*s :: %s\n", LIT(e->token.string), type_to_string(e->type)); + debugf("proc body unchecked\n"); + debugf("Checked State: %s\n\n", ProcCheckedState_strings[proc_checked_state]); + + consume_proc_info(c, pi, &untyped); + } + } + } + } + + debugf("Total Procedure Bodies Checked: %td\n", total_bodies_checked.load(std::memory_order_relaxed)); + TIME_SECTION("check unique package names"); check_unique_package_names(c); - TIME_SECTION("sanity checks"); + check_merge_queues_into_arrays(c); GB_ASSERT(c->info.entity_queue.count.load(std::memory_order_relaxed) == 0); GB_ASSERT(c->info.definition_queue.count.load(std::memory_order_relaxed) == 0); @@ -5717,5 +5848,6 @@ gb_internal void check_parsed_files(Checker *c) { } } + TIME_SECTION("type check finish"); } diff --git a/src/checker.hpp b/src/checker.hpp index eaad1fa63..37aff41b1 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -142,6 +142,20 @@ typedef DECL_ATTRIBUTE_PROC(DeclAttributeProc); gb_internal void check_decl_attributes(CheckerContext *c, Array const &attributes, DeclAttributeProc *proc, AttributeContext *ac); +enum ProcCheckedState : u8 { + ProcCheckedState_Unchecked, + ProcCheckedState_InProgress, + ProcCheckedState_Checked, + + ProcCheckedState_COUNT +}; + +char const *ProcCheckedState_strings[ProcCheckedState_COUNT] { + "Unchecked", + "In Progress", + "Checked", +}; + // DeclInfo is used to store information of certain declarations to allow for "any order" usage struct DeclInfo { DeclInfo * parent; // NOTE(bill): only used for procedure literals at the moment @@ -157,7 +171,7 @@ struct DeclInfo { Type * gen_proc_type; // Precalculated bool is_using; bool where_clauses_evaluated; - bool proc_checked; + std::atomic proc_checked_state; BlockingMutex proc_checked_mutex; isize defer_used; bool defer_use_checked; @@ -375,6 +389,9 @@ struct CheckerInfo { BlockingMutex load_file_mutex; StringMap load_file_cache; + + BlockingMutex all_procedures_mutex;; + Array all_procedures; }; struct CheckerContext { @@ -458,7 +475,7 @@ gb_internal Entity *entity_of_node(Ast *expr); gb_internal Entity *scope_lookup_current(Scope *s, String const &name); gb_internal Entity *scope_lookup (Scope *s, String const &name); gb_internal void scope_lookup_parent (Scope *s, String const &name, Scope **scope_, Entity **entity_); -gb_internal Entity *scope_insert (Scope *s, Entity *entity, bool use_mutex=true); +gb_internal Entity *scope_insert (Scope *s, Entity *entity); gb_internal void add_type_and_value (CheckerInfo *i, Ast *expression, AddressingMode mode, Type *type, ExactValue value); diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 1c401552e..3e62f678a 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -2255,6 +2255,12 @@ gb_internal void lb_generate_code(lbGenerator *gen) { } } + if (build_context.ignore_llvm_build) { + gb_printf_err("LLVM SUCCESS!\n"); + gb_exit(1); + return; + } + if (do_threading && non_empty_module_count > 1) { for (auto const &entry : gen->modules) { lbModule *m = entry.value; diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 9f7caa3bb..50fb5701f 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -301,6 +301,7 @@ struct lbProcedure { lbBlock * curr_block; lbTargetList * target_list; PtrMap direct_parameters; + PtrMap local_entity_map; Ast *curr_stmt; diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index d574caf4c..28a68b065 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -3404,7 +3404,11 @@ gb_internal lbAddr lb_build_addr_from_entity(lbProcedure *p, Entity *e, Ast *exp lbValue v = {}; - lbValue *found = map_get(&p->module->values, e); + lbValue *found = nullptr; + found = map_get(&p->local_entity_map, e); + if (found == nullptr) { + found = map_get(&p->module->values, e); + } if (found) { v = *found; } else if (e->kind == Entity_Variable && e->flags & EntityFlag_Using) { diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 0508c6171..940c94a13 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -2714,7 +2714,6 @@ gb_internal lbValue lb_find_value_from_entity(lbModule *m, Entity *e) { return g; } } - GB_PANIC("\n\tError in: %s, missing value '%.*s'\n", token_pos_to_string(e->token.pos), LIT(e->token.string)); return {}; } @@ -2845,6 +2844,10 @@ gb_internal lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e, bool zero lb_mem_zero_ptr(p, ptr, type, alignment); } + if (e != nullptr) { + map_set(&p->local_entity_map, e, val); + } + return lb_addr(val); } diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 384d29ca7..9691afebc 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -68,7 +68,7 @@ gb_internal lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool i GB_ASSERT(entity != nullptr); GB_ASSERT(entity->kind == Entity_Procedure); if (!entity->Procedure.is_foreign) { - GB_ASSERT_MSG(entity->flags & EntityFlag_ProcBodyChecked, "%.*s :: %s", LIT(entity->token.string), type_to_string(entity->type)); + GB_ASSERT_MSG(entity->flags & EntityFlag_ProcBodyChecked, "%.*s :: %s (was parapoly: %d)", LIT(entity->token.string), type_to_string(entity->type), is_type_polymorphic(entity->type, true)); } String link_name = {}; @@ -487,6 +487,7 @@ gb_internal void lb_begin_procedure_body(lbProcedure *p) { lb_start_block(p, p->entry_block); map_init(&p->direct_parameters, heap_allocator()); + map_init(&p->local_entity_map, heap_allocator()); GB_ASSERT(p->type != nullptr); diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 6400a8a9d..cdfb28aa7 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -1531,6 +1531,9 @@ gb_internal void lb_build_static_variables(lbProcedure *p, AstValueDecl *vd) { lbValue global_val = {global, alloc_type_pointer(e->type)}; lb_add_entity(p->module, e, global_val); lb_add_member(p->module, mangled_name, global_val); + if (e) { + map_set(&p->local_entity_map, e, global_val); + } } } gb_internal void lb_append_tuple_values(lbProcedure *p, Array *dst_values, lbValue src_value) { @@ -2188,9 +2191,10 @@ gb_internal void lb_build_stmt(lbProcedure *p, Ast *node) { for_array(i, vd->names) { Ast *name = vd->names[i]; if (!is_blank_ident(name)) { + GB_ASSERT(name->kind == Ast_Ident); Entity *e = entity_of_node(name); TokenPos pos = ast_token(name).pos; - GB_ASSERT_MSG(e != nullptr, "%s", token_pos_to_string(pos)); + GB_ASSERT_MSG(e != nullptr, "\n%s missing entity for %.*s", token_pos_to_string(pos), LIT(name->Ident.token.string)); if (e->flags & EntityFlag_Static) { // NOTE(bill): If one of the entities is static, they all are is_static = true; diff --git a/src/main.cpp b/src/main.cpp index 3ad0e160f..42d6f8e87 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -660,6 +660,7 @@ enum BuildFlagKind { // internal use only BuildFlag_InternalIgnoreLazy, + BuildFlag_InternalIgnoreLLVMBuild, #if defined(GB_SYSTEM_WINDOWS) BuildFlag_IgnoreVsSearch, @@ -832,6 +833,7 @@ gb_internal bool parse_build_flags(Array args) { add_flag(&build_flags, BuildFlag_ErrorPosStyle, str_lit("error-pos-style"), BuildFlagParam_String, Command_all); add_flag(&build_flags, BuildFlag_InternalIgnoreLazy, str_lit("internal-ignore-lazy"), BuildFlagParam_None, Command_all); + add_flag(&build_flags, BuildFlag_InternalIgnoreLLVMBuild, str_lit("internal-ignore-llvm-build"),BuildFlagParam_None, Command_all); #if defined(GB_SYSTEM_WINDOWS) add_flag(&build_flags, BuildFlag_IgnoreVsSearch, str_lit("ignore-vs-search"), BuildFlagParam_None, Command__does_build); @@ -1491,6 +1493,9 @@ gb_internal bool parse_build_flags(Array args) { case BuildFlag_InternalIgnoreLazy: build_context.ignore_lazy = true; break; + case BuildFlag_InternalIgnoreLLVMBuild: + build_context.ignore_llvm_build = true; + break; #if defined(GB_SYSTEM_WINDOWS) case BuildFlag_IgnoreVsSearch: { GB_ASSERT(value.kind == ExactValue_Invalid); -- cgit v1.2.3 From fa562ec5d60319f5cd7e85bb337bd21feb7ceeb8 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 2 Jan 2023 15:40:25 +0000 Subject: Remove unneeded `local_entity_map` --- src/llvm_backend.hpp | 1 - src/llvm_backend_expr.cpp | 6 +----- src/llvm_backend_general.cpp | 4 ---- src/llvm_backend_proc.cpp | 1 - src/llvm_backend_stmt.cpp | 3 --- 5 files changed, 1 insertion(+), 14 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 50fb5701f..9f7caa3bb 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -301,7 +301,6 @@ struct lbProcedure { lbBlock * curr_block; lbTargetList * target_list; PtrMap direct_parameters; - PtrMap local_entity_map; Ast *curr_stmt; diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index 28a68b065..d574caf4c 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -3404,11 +3404,7 @@ gb_internal lbAddr lb_build_addr_from_entity(lbProcedure *p, Entity *e, Ast *exp lbValue v = {}; - lbValue *found = nullptr; - found = map_get(&p->local_entity_map, e); - if (found == nullptr) { - found = map_get(&p->module->values, e); - } + lbValue *found = map_get(&p->module->values, e); if (found) { v = *found; } else if (e->kind == Entity_Variable && e->flags & EntityFlag_Using) { diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 940c94a13..22628e895 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -2844,10 +2844,6 @@ gb_internal lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e, bool zero lb_mem_zero_ptr(p, ptr, type, alignment); } - if (e != nullptr) { - map_set(&p->local_entity_map, e, val); - } - return lb_addr(val); } diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 9691afebc..7245bdd80 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -487,7 +487,6 @@ gb_internal void lb_begin_procedure_body(lbProcedure *p) { lb_start_block(p, p->entry_block); map_init(&p->direct_parameters, heap_allocator()); - map_init(&p->local_entity_map, heap_allocator()); GB_ASSERT(p->type != nullptr); diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index cdfb28aa7..06abebc78 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -1531,9 +1531,6 @@ gb_internal void lb_build_static_variables(lbProcedure *p, AstValueDecl *vd) { lbValue global_val = {global, alloc_type_pointer(e->type)}; lb_add_entity(p->module, e, global_val); lb_add_member(p->module, mangled_name, global_val); - if (e) { - map_set(&p->local_entity_map, e, global_val); - } } } gb_internal void lb_append_tuple_values(lbProcedure *p, Array *dst_values, lbValue src_value) { -- cgit v1.2.3 From 600f2b7284b8974a18827242c18e790dab0cf06a Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 3 Jan 2023 11:53:59 +0000 Subject: Use heap_allocator for all hash set types --- src/build_settings.cpp | 2 +- src/check_builtin.cpp | 2 +- src/check_decl.cpp | 2 +- src/check_expr.cpp | 1 - src/check_stmt.cpp | 1 - src/checker.cpp | 24 +++++++----------------- src/llvm_backend_general.cpp | 2 +- src/llvm_backend_type.cpp | 2 +- src/main.cpp | 8 ++++---- src/parser.cpp | 2 +- src/ptr_set.cpp | 21 +++++++++++++++------ src/string_set.cpp | 18 +++++++++++++----- 12 files changed, 45 insertions(+), 40 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 04d1ada93..75615a901 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -1369,7 +1369,7 @@ gb_internal bool init_build_paths(String init_filename) { // NOTE(Jeroen): We're pre-allocating BuildPathCOUNT slots so that certain paths are always at the same enumerated index. array_init(&bc->build_paths, permanent_allocator(), BuildPathCOUNT); - string_set_init(&bc->target_features_set, heap_allocator(), 1024); + string_set_init(&bc->target_features_set, 1024); // [BuildPathMainPackage] Turn given init path into a `Path`, which includes normalizing it into a full path. bc->build_paths[BuildPath_Main_Package] = path_from_string(ha, init_filename); diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 99d956f5e..36dc9b7a1 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -3086,7 +3086,7 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As } } StringSet name_set = {}; - string_set_init(&name_set, heap_allocator(), 2*ce->args.count); + string_set_init(&name_set, 2*ce->args.count); for_array(i, ce->args) { String name = {}; diff --git a/src/check_decl.cpp b/src/check_decl.cpp index e3486924c..0c1a7c325 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -1235,7 +1235,7 @@ gb_internal void check_proc_group_decl(CheckerContext *ctx, Entity *&pg_entity, pg_entity->type = t_invalid; PtrSet entity_set = {}; - ptr_set_init(&entity_set, heap_allocator(), 2*pg->args.count); + ptr_set_init(&entity_set, 2*pg->args.count); for_array(i, pg->args) { Ast *arg = pg->args[i]; diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 3c998fc44..7a00b5353 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -196,7 +196,6 @@ gb_internal void check_did_you_mean_objc_entity(String const &name, Entity *e, b MUTEX_GUARD(objc_metadata->mutex); StringSet set = {}; - string_set_init(&set, heap_allocator()); defer (string_set_destroy(&set)); populate_check_did_you_mean_objc_entity(&set, e, is_type); diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index cf111e84c..945ba8f02 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -1185,7 +1185,6 @@ gb_internal void check_type_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_ } PtrSet seen = {}; - ptr_set_init(&seen, heap_allocator()); defer (ptr_set_destroy(&seen)); for_array(i, bs->stmts) { diff --git a/src/checker.cpp b/src/checker.cpp index ccd0f3627..8da659461 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -72,7 +72,7 @@ gb_internal void entity_graph_node_set_destroy(EntityGraphNodeSet *s) { gb_internal void entity_graph_node_set_add(EntityGraphNodeSet *s, EntityGraphNode *n) { if (s->hashes.data == nullptr) { - ptr_set_init(s, heap_allocator()); + ptr_set_init(s); } ptr_set_add(s, n); } @@ -118,15 +118,10 @@ gb_internal void entity_graph_node_swap(EntityGraphNode **data, isize i, isize j gb_internal void import_graph_node_set_destroy(ImportGraphNodeSet *s) { - if (s->hashes.data != nullptr) { - ptr_set_destroy(s); - } + ptr_set_destroy(s); } gb_internal void import_graph_node_set_add(ImportGraphNodeSet *s, ImportGraphNode *n) { - if (s->hashes.data == nullptr) { - ptr_set_init(s, heap_allocator()); - } ptr_set_add(s, n); } @@ -185,8 +180,8 @@ gb_internal void init_decl_info(DeclInfo *d, Scope *scope, DeclInfo *parent) { gb_zero_item(d); d->parent = parent; d->scope = scope; - ptr_set_init(&d->deps, heap_allocator()); - ptr_set_init(&d->type_info_deps, heap_allocator()); + ptr_set_init(&d->deps); + ptr_set_init(&d->type_info_deps); array_init (&d->labels, heap_allocator()); } @@ -227,7 +222,7 @@ gb_internal Scope *create_scope(CheckerInfo *info, Scope *parent, isize init_ele Scope *s = gb_alloc_item(permanent_allocator(), Scope); s->parent = parent; string_map_init(&s->elements, heap_allocator(), init_elements_capacity); - ptr_set_init(&s->imported, heap_allocator(), 0); + ptr_set_init(&s->imported, 0); if (parent != nullptr && parent != builtin_pkg->scope) { Scope *prev_head_child = parent->head_child.exchange(s, std::memory_order_acq_rel); @@ -2270,8 +2265,8 @@ gb_internal void generate_minimum_dependency_set(Checker *c, Entity *start) { isize entity_count = c->info.entities.count; isize min_dep_set_cap = next_pow2_isize(entity_count*4); // empirically determined factor - ptr_set_init(&c->info.minimum_dependency_set, heap_allocator(), min_dep_set_cap); - ptr_set_init(&c->info.minimum_dependency_type_info_set, heap_allocator()); + ptr_set_init(&c->info.minimum_dependency_set, min_dep_set_cap); + ptr_set_init(&c->info.minimum_dependency_type_info_set); #define FORCE_ADD_RUNTIME_ENTITIES(condition, ...) do { \ if (condition) { \ @@ -3388,7 +3383,6 @@ gb_internal void check_decl_attributes(CheckerContext *c, Array const &at } StringSet set = {}; - string_set_init(&set, heap_allocator()); defer (string_set_destroy(&set)); for_array(i, attributes) { @@ -4759,7 +4753,6 @@ gb_internal void check_import_entities(Checker *c) { auto pq = priority_queue_create(dep_graph, import_graph_node_cmp, import_graph_node_swap); PtrSet emitted = {}; - ptr_set_init(&emitted, heap_allocator()); defer (ptr_set_destroy(&emitted)); Array package_order = {}; @@ -4773,7 +4766,6 @@ gb_internal void check_import_entities(Checker *c) { if (n->dep_count > 0) { PtrSet visited = {}; - ptr_set_init(&visited, heap_allocator()); defer (ptr_set_destroy(&visited)); auto path = find_import_path(c, pkg, pkg, &visited); @@ -4927,7 +4919,6 @@ gb_internal Array find_entity_path(Entity *start, Entity *end, PtrSet< bool made_visited = false; if (visited == nullptr) { made_visited = true; - ptr_set_init(&visited_, heap_allocator()); visited = &visited_; } defer (if (made_visited) { @@ -4990,7 +4981,6 @@ gb_internal void calculate_global_init_order(Checker *c) { auto pq = priority_queue_create(dep_graph, entity_graph_node_cmp, entity_graph_node_swap); PtrSet emitted = {}; - ptr_set_init(&emitted, heap_allocator()); defer (ptr_set_destroy(&emitted)); TIME_SECTION("calculate_global_init_order: queue sort"); diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 22628e895..75675474a 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -133,7 +133,7 @@ gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) { array_init(&gen->foreign_libraries, heap_allocator(), 0, 1024); - ptr_set_init(&gen->foreign_libraries_set, heap_allocator(), 1024); + ptr_set_init(&gen->foreign_libraries_set, 1024); if (USE_SEPARATE_MODULES) { for (auto const &entry : gen->info->packages) { diff --git a/src/llvm_backend_type.cpp b/src/llvm_backend_type.cpp index 26bb614e6..c306cdead 100644 --- a/src/llvm_backend_type.cpp +++ b/src/llvm_backend_type.cpp @@ -2,7 +2,7 @@ gb_internal isize lb_type_info_index(CheckerInfo *info, Type *type, bool err_on_ auto *set = &info->minimum_dependency_type_info_set; isize index = type_info_index(info, type, err_on_not_found); if (index >= 0) { - isize i = ptr_entry_index(set, index); + isize i = ptr_set_entry_index(set, index); if (i >= 0) { return i+1; } diff --git a/src/main.cpp b/src/main.cpp index ad9d6b0ef..91dcbdb01 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -212,11 +212,11 @@ gb_internal i32 linker_stage(lbGenerator *gen) { StringSet libs = {}; - string_set_init(&libs, heap_allocator(), 64); + string_set_init(&libs, 64); defer (string_set_destroy(&libs)); StringSet asm_files = {}; - string_set_init(&asm_files, heap_allocator(), 64); + string_set_init(&asm_files, 64); defer (string_set_destroy(&asm_files)); for_array(j, gen->foreign_libraries) { @@ -371,7 +371,7 @@ gb_internal i32 linker_stage(lbGenerator *gen) { defer (gb_string_free(lib_str)); StringSet libs = {}; - string_set_init(&libs, heap_allocator(), 64); + string_set_init(&libs, 64); defer (string_set_destroy(&libs)); for_array(j, gen->foreign_libraries) { @@ -2518,7 +2518,7 @@ int main(int arg_count, char const **arg_ptr) { map_init(&build_context.defined_values, heap_allocator()); build_context.extra_packages.allocator = heap_allocator(); - string_set_init(&build_context.test_names, heap_allocator()); + string_set_init(&build_context.test_names); Array args = setup_args(arg_count, arg_ptr); diff --git a/src/parser.cpp b/src/parser.cpp index 4d2a8ecf4..046469c16 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -4854,7 +4854,7 @@ gb_internal void destroy_ast_file(AstFile *f) { gb_internal bool init_parser(Parser *p) { GB_ASSERT(p != nullptr); - string_set_init(&p->imported_files, heap_allocator()); + string_set_init(&p->imported_files); array_init(&p->packages, heap_allocator()); return true; } diff --git a/src/ptr_set.cpp b/src/ptr_set.cpp index 9ecf1043e..affde5c2f 100644 --- a/src/ptr_set.cpp +++ b/src/ptr_set.cpp @@ -6,11 +6,11 @@ struct PtrSetEntry { template struct PtrSet { - Slice hashes; + Slice hashes; Array> entries; }; -template gb_internal void ptr_set_init (PtrSet *s, gbAllocator a, isize capacity = 16); +template gb_internal void ptr_set_init (PtrSet *s, isize capacity = 16); template gb_internal void ptr_set_destroy(PtrSet *s); template gb_internal T ptr_set_add (PtrSet *s, T ptr); template gb_internal bool ptr_set_update (PtrSet *s, T ptr); // returns true if it previously existed @@ -21,15 +21,18 @@ template gb_internal void ptr_set_grow (PtrSet *s); template gb_internal void ptr_set_rehash (PtrSet *s, isize new_count); template gb_internal void ptr_set_reserve(PtrSet *h, isize cap); +gb_internal gbAllocator ptr_set_allocator(void) { + return heap_allocator(); +} template -gb_internal void ptr_set_init(PtrSet *s, gbAllocator a, isize capacity) { +gb_internal void ptr_set_init(PtrSet *s, isize capacity) { if (capacity != 0) { capacity = next_pow2_isize(gb_max(16, capacity)); } - slice_init(&s->hashes, a, capacity); - array_init(&s->entries, a, 0, capacity); + slice_init(&s->hashes, ptr_set_allocator(), capacity); + array_init(&s->entries, ptr_set_allocator(), 0, capacity); for (isize i = 0; i < capacity; i++) { s->hashes.data[i] = MAP_SENTINEL; } @@ -37,6 +40,9 @@ gb_internal void ptr_set_init(PtrSet *s, gbAllocator a, isize capacity) { template gb_internal void ptr_set_destroy(PtrSet *s) { + if (s->entries.allocator.proc == nullptr) { + s->entries.allocator = ptr_set_allocator(); + } slice_free(&s->hashes, s->entries.allocator); array_free(&s->entries); } @@ -118,6 +124,9 @@ gb_internal void ptr_set_reset_entries(PtrSet *s) { template gb_internal void ptr_set_reserve(PtrSet *s, isize cap) { + if (s->entries.allocator.proc == nullptr) { + s->entries.allocator = ptr_set_allocator(); + } array_reserve(&s->entries, cap); if (s->entries.count*2 < s->hashes.count) { return; @@ -139,7 +148,7 @@ gb_internal gb_inline bool ptr_set_exists(PtrSet *s, T ptr) { } template -gb_internal gb_inline isize ptr_entry_index(PtrSet *s, T ptr) { +gb_internal gb_inline isize ptr_set_entry_index(PtrSet *s, T ptr) { isize index = ptr_set__find(s, ptr).entry_index; if (index != MAP_SENTINEL) { return index; diff --git a/src/string_set.cpp b/src/string_set.cpp index 1c97d253e..753afa9bf 100644 --- a/src/string_set.cpp +++ b/src/string_set.cpp @@ -10,7 +10,7 @@ struct StringSet { }; -gb_internal void string_set_init (StringSet *s, gbAllocator a, isize capacity = 16); +gb_internal void string_set_init (StringSet *s, isize capacity = 16); gb_internal void string_set_destroy(StringSet *s); gb_internal void string_set_add (StringSet *s, String const &str); gb_internal bool string_set_update (StringSet *s, String const &str); // returns true if it previously existed @@ -20,18 +20,24 @@ gb_internal void string_set_clear (StringSet *s); gb_internal void string_set_grow (StringSet *s); gb_internal void string_set_rehash (StringSet *s, isize new_count); +gb_internal gbAllocator string_set_allocator(void) { + return heap_allocator(); +} -gb_internal gb_inline void string_set_init(StringSet *s, gbAllocator a, isize capacity) { +gb_internal gb_inline void string_set_init(StringSet *s, isize capacity) { capacity = next_pow2_isize(gb_max(16, capacity)); - slice_init(&s->hashes, a, capacity); - array_init(&s->entries, a, 0, capacity); + slice_init(&s->hashes, string_set_allocator(), capacity); + array_init(&s->entries, string_set_allocator(), 0, capacity); for (isize i = 0; i < capacity; i++) { s->hashes.data[i] = MAP_SENTINEL; } } gb_internal gb_inline void string_set_destroy(StringSet *s) { + if (s->entries.allocator.proc == nullptr) { + s->entries.allocator = string_set_allocator(); + } slice_free(&s->hashes, s->entries.allocator); array_free(&s->entries); } @@ -106,6 +112,9 @@ gb_internal void string_set_reset_entries(StringSet *s) { } gb_internal void string_set_reserve(StringSet *s, isize cap) { + if (s->entries.allocator.proc == nullptr) { + s->entries.allocator = string_set_allocator(); + } array_reserve(&s->entries, cap); if (s->entries.count*2 < s->hashes.count) { return; @@ -217,7 +226,6 @@ gb_internal gb_inline void string_set_clear(StringSet *s) { } - gb_internal StringSetEntry *begin(StringSet &m) { return m.entries.data; } -- cgit v1.2.3 From 252be0fb417f9cdde5e9c4b348cd995a20433aea Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 3 Jan 2023 11:59:52 +0000 Subject: Make all maps use heap allocator implicitly --- src/check_builtin.cpp | 2 +- src/check_expr.cpp | 3 +-- src/check_stmt.cpp | 1 - src/checker.cpp | 33 +++++++++++++++------------------ src/common.cpp | 2 +- src/docs_writer.cpp | 11 +++++------ src/llvm_backend_general.cpp | 44 ++++++++++++++++++++++---------------------- src/llvm_backend_proc.cpp | 10 +++++----- src/main.cpp | 2 +- src/ptr_map.cpp | 18 ++++++++++++++---- src/string_map.cpp | 18 ++++++++++++++---- 11 files changed, 79 insertions(+), 65 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 36dc9b7a1..af196234e 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -1110,7 +1110,7 @@ gb_internal bool cache_load_file_directive(CheckerContext *c, Ast *call, String new_cache->path = path; new_cache->data = data; new_cache->file_error = file_error; - string_map_init(&new_cache->hashes, heap_allocator(), 32); + string_map_init(&new_cache->hashes, 32); string_map_set(&c->info->load_file_cache, path, new_cache); if (cache_) *cache_ = new_cache; } else { diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 7a00b5353..030bfb8e6 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -5753,7 +5753,7 @@ gb_internal CallArgumentData check_call_arguments(CheckerContext *c, Operand *op // in order to improve the type inference system StringMap type_hint_map = {}; // Key: String - string_map_init(&type_hint_map, heap_allocator(), 2*args.count); + string_map_init(&type_hint_map, 2*args.count); defer (string_map_destroy(&type_hint_map)); Type *ptype = nullptr; @@ -8283,7 +8283,6 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast * bool is_partial = cl->tag && (cl->tag->BasicDirective.name.string == "partial"); SeenMap seen = {}; // NOTE(bill): Multimap, Key: ExactValue - map_init(&seen, heap_allocator()); defer (map_destroy(&seen)); if (cl->elems.count > 0 && cl->elems[0]->kind == Ast_FieldValue) { diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index 945ba8f02..7192b16b5 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -929,7 +929,6 @@ gb_internal void check_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags } SeenMap seen = {}; // NOTE(bill): Multimap, Key: ExactValue - map_init(&seen, heap_allocator()); defer (map_destroy(&seen)); for_array(stmt_index, bs->stmts) { diff --git a/src/checker.cpp b/src/checker.cpp index 8da659461..8779d9d45 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -221,7 +221,7 @@ gb_internal DeclInfo *make_decl_info(Scope *scope, DeclInfo *parent) { gb_internal Scope *create_scope(CheckerInfo *info, Scope *parent, isize init_elements_capacity=DEFAULT_SCOPE_CAPACITY) { Scope *s = gb_alloc_item(permanent_allocator(), Scope); s->parent = parent; - string_map_init(&s->elements, heap_allocator(), init_elements_capacity); + string_map_init(&s->elements, init_elements_capacity); ptr_set_init(&s->imported, 0); if (parent != nullptr && parent != builtin_pkg->scope) { @@ -1135,14 +1135,14 @@ gb_internal void init_checker_info(CheckerInfo *i) { array_init(&i->definitions, a); array_init(&i->entities, a); - map_init(&i->global_untyped, a); - string_map_init(&i->foreigns, a); - map_init(&i->gen_procs, a); - map_init(&i->gen_types, a); + map_init(&i->global_untyped); + string_map_init(&i->foreigns); + map_init(&i->gen_procs); + map_init(&i->gen_types); array_init(&i->type_info_types, a); - map_init(&i->type_info_map, a); - string_map_init(&i->files, a); - string_map_init(&i->packages, a); + map_init(&i->type_info_map); + string_map_init(&i->files); + string_map_init(&i->packages); array_init(&i->variable_init_order, a); array_init(&i->testing_procedures, a, 0, 0); array_init(&i->init_procedures, a, 0, 0); @@ -1160,8 +1160,8 @@ gb_internal void init_checker_info(CheckerInfo *i) { mpmc_init(&i->intrinsics_entry_point_usage, a, 1<<10); // just waste some memory here, even if it probably never used - map_init(&i->objc_msgSend_types, a); - string_map_init(&i->load_file_cache, a); + map_init(&i->objc_msgSend_types); + string_map_init(&i->load_file_cache); array_init(&i->all_procedures, heap_allocator()); @@ -2490,7 +2490,7 @@ gb_internal bool is_entity_a_dependency(Entity *e) { gb_internal Array generate_entity_dependency_graph(CheckerInfo *info, gbAllocator allocator) { PtrMap M = {}; - map_init(&M, allocator, info->entities.count); + map_init(&M, info->entities.count); defer (map_destroy(&M)); for_array(i, info->entities) { Entity *e = info->entities[i]; @@ -4200,7 +4200,7 @@ gb_internal void add_import_dependency_node(Checker *c, Ast *decl, PtrMap generate_import_dependency_graph(Checker *c) { PtrMap M = {}; - map_init(&M, heap_allocator(), 2*c->parser->packages.count); + map_init(&M, 2*c->parser->packages.count); defer (map_destroy(&M)); for_array(i, c->parser->packages) { @@ -4688,7 +4688,7 @@ gb_internal void check_collect_entities_all(Checker *c) { auto *wd = &collect_entity_worker_data[i]; wd->c = c; wd->ctx = make_checker_context(c); - map_init(&wd->untyped, heap_allocator()); + map_init(&wd->untyped); } for (auto const &entry : c->info.files.entries) { @@ -4804,7 +4804,6 @@ gb_internal void check_import_entities(Checker *c) { CheckerContext ctx = make_checker_context(c); UntypedExprInfoMap untyped = {}; - map_init(&untyped, heap_allocator()); defer (map_destroy(&untyped)); isize min_pkg_index = 0; @@ -5159,7 +5158,6 @@ gb_internal void check_unchecked_bodies(Checker *c) { GB_ASSERT(c->procs_to_check.count == 0); UntypedExprInfoMap untyped = {}; - map_init(&untyped, heap_allocator()); defer (map_destroy(&untyped)); // use the `procs_to_check` array @@ -5212,7 +5210,6 @@ gb_internal void check_unchecked_bodies(Checker *c) { gb_internal void check_safety_all_procedures_for_unchecked(Checker *c) { GB_ASSERT(DEBUG_CHECK_ALL_PROCEDURES); UntypedExprInfoMap untyped = {}; - map_init(&untyped, heap_allocator()); defer (map_destroy(&untyped)); @@ -5345,7 +5342,7 @@ gb_internal void check_procedure_bodies(Checker *c) { for (isize i = 0; i < thread_count; i++) { check_procedure_bodies_worker_data[i].c = c; - map_init(&check_procedure_bodies_worker_data[i].untyped, heap_allocator()); + map_init(&check_procedure_bodies_worker_data[i].untyped); } defer (for (isize i = 0; i < thread_count; i++) { @@ -5545,7 +5542,7 @@ gb_internal void check_deferred_procedures(Checker *c) { gb_internal void check_unique_package_names(Checker *c) { StringMap pkgs = {}; // Key: package name - string_map_init(&pkgs, heap_allocator(), 2*c->info.packages.entries.count); + string_map_init(&pkgs, 2*c->info.packages.entries.count); defer (string_map_destroy(&pkgs)); for (auto const &entry : c->info.packages) { diff --git a/src/common.cpp b/src/common.cpp index 3b6ea59e8..199a263a1 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -373,7 +373,7 @@ gb_internal char const *string_intern(String const &string) { } gb_internal void init_string_interner(void) { - map_init(&string_intern_map, heap_allocator()); + map_init(&string_intern_map); } diff --git a/src/docs_writer.cpp b/src/docs_writer.cpp index bab97158d..2aefe29eb 100644 --- a/src/docs_writer.cpp +++ b/src/docs_writer.cpp @@ -53,13 +53,12 @@ gb_internal void odin_doc_writer_item_tracker_init(OdinDocWriterItemTracker * gb_internal void odin_doc_writer_prepare(OdinDocWriter *w) { w->state = OdinDocWriterState_Preparing; - gbAllocator a = heap_allocator(); - string_map_init(&w->string_cache, a); + string_map_init(&w->string_cache); - map_init(&w->file_cache, a); - map_init(&w->pkg_cache, a); - map_init(&w->entity_cache, a); - map_init(&w->type_cache, a); + map_init(&w->file_cache); + map_init(&w->pkg_cache); + map_init(&w->entity_cache); + map_init(&w->type_cache); odin_doc_writer_item_tracker_init(&w->files, 1); odin_doc_writer_item_tracker_init(&w->pkgs, 1); diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 75675474a..a849929f0 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -55,30 +55,30 @@ gb_internal void lb_init_module(lbModule *m, Checker *c) { } gbAllocator a = heap_allocator(); - map_init(&m->types, a); - map_init(&m->func_raw_types, a); - map_init(&m->struct_field_remapping, a); - map_init(&m->values, a); - map_init(&m->soa_values, a); - string_map_init(&m->members, a); - map_init(&m->procedure_values, a); - string_map_init(&m->procedures, a); - string_map_init(&m->const_strings, a); - map_init(&m->function_type_map, a); - map_init(&m->equal_procs, a); - map_init(&m->hasher_procs, a); - map_init(&m->map_get_procs, a); - map_init(&m->map_set_procs, a); + map_init(&m->types); + map_init(&m->func_raw_types); + map_init(&m->struct_field_remapping); + map_init(&m->values); + map_init(&m->soa_values); + string_map_init(&m->members); + map_init(&m->procedure_values); + string_map_init(&m->procedures); + string_map_init(&m->const_strings); + map_init(&m->function_type_map); + map_init(&m->equal_procs); + map_init(&m->hasher_procs); + map_init(&m->map_get_procs); + map_init(&m->map_set_procs); array_init(&m->procedures_to_generate, a, 0, 1024); array_init(&m->missing_procedures_to_check, a, 0, 16); - map_init(&m->debug_values, a); + map_init(&m->debug_values); array_init(&m->debug_incomplete_types, a, 0, 1024); - string_map_init(&m->objc_classes, a); - string_map_init(&m->objc_selectors, a); + string_map_init(&m->objc_classes); + string_map_init(&m->objc_selectors); - map_init(&m->map_info_map, a, 0); - map_init(&m->map_cell_info_map, a, 0); + map_init(&m->map_info_map, 0); + map_init(&m->map_cell_info_map, 0); } @@ -127,9 +127,9 @@ gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) { gen->info = &c->info; - map_init(&gen->modules, permanent_allocator(), gen->info->packages.entries.count*2); - map_init(&gen->modules_through_ctx, permanent_allocator(), gen->info->packages.entries.count*2); - map_init(&gen->anonymous_proc_lits, heap_allocator(), 1024); + map_init(&gen->modules, gen->info->packages.entries.count*2); + map_init(&gen->modules_through_ctx, gen->info->packages.entries.count*2); + map_init(&gen->anonymous_proc_lits, 1024); array_init(&gen->foreign_libraries, heap_allocator(), 0, 1024); diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 7245bdd80..c66462bc1 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -119,9 +119,9 @@ gb_internal lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool i p->branch_blocks.allocator = a; p->context_stack.allocator = a; p->scope_stack.allocator = a; - map_init(&p->selector_values, a, 0); - map_init(&p->selector_addr, a, 0); - map_init(&p->tuple_fix_map, a, 0); + map_init(&p->selector_values, 0); + map_init(&p->selector_addr, 0); + map_init(&p->tuple_fix_map, 0); if (p->is_foreign) { lb_add_foreign_library_path(p->module, entity->Procedure.foreign_library); @@ -345,7 +345,7 @@ gb_internal lbProcedure *lb_create_dummy_procedure(lbModule *m, String link_name p->blocks.allocator = a; p->branch_blocks.allocator = a; p->context_stack.allocator = a; - map_init(&p->tuple_fix_map, a, 0); + map_init(&p->tuple_fix_map, 0); char *c_link_name = alloc_cstring(permanent_allocator(), p->name); @@ -486,7 +486,7 @@ gb_internal void lb_begin_procedure_body(lbProcedure *p) { p->entry_block = lb_create_block(p, "entry", true); lb_start_block(p, p->entry_block); - map_init(&p->direct_parameters, heap_allocator()); + map_init(&p->direct_parameters); GB_ASSERT(p->type != nullptr); diff --git a/src/main.cpp b/src/main.cpp index 91dcbdb01..7ac78241e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2516,7 +2516,7 @@ int main(int arg_count, char const **arg_ptr) { add_library_collection(str_lit("core"), get_fullpath_relative(heap_allocator(), odin_root_dir(), str_lit("core"))); add_library_collection(str_lit("vendor"), get_fullpath_relative(heap_allocator(), odin_root_dir(), str_lit("vendor"))); - map_init(&build_context.defined_values, heap_allocator()); + map_init(&build_context.defined_values); build_context.extra_packages.allocator = heap_allocator(); string_set_init(&build_context.test_names); diff --git a/src/ptr_map.cpp b/src/ptr_map.cpp index 434680e91..083cd6697 100644 --- a/src/ptr_map.cpp +++ b/src/ptr_map.cpp @@ -46,7 +46,7 @@ gb_internal gb_inline u32 ptr_map_hash_key(void const *key) { } -template gb_internal void map_init (PtrMap *h, gbAllocator a, isize capacity = 16); +template gb_internal void map_init (PtrMap *h, isize capacity = 16); template gb_internal void map_destroy (PtrMap *h); template gb_internal V * map_get (PtrMap *h, K key); template gb_internal void map_set (PtrMap *h, K key, V const &value); @@ -68,11 +68,15 @@ template gb_internal void multi_map_remove (PtrMap< template gb_internal void multi_map_remove_all(PtrMap *h, K key); #endif +gb_internal gbAllocator map_allocator(void) { + return heap_allocator(); +} + template -gb_internal gb_inline void map_init(PtrMap *h, gbAllocator a, isize capacity) { +gb_internal gb_inline void map_init(PtrMap *h, isize capacity) { capacity = next_pow2_isize(capacity); - slice_init(&h->hashes, a, capacity); - array_init(&h->entries, a, 0, capacity); + slice_init(&h->hashes, map_allocator(), capacity); + array_init(&h->entries, map_allocator(), 0, capacity); for (isize i = 0; i < capacity; i++) { h->hashes.data[i] = MAP_SENTINEL; } @@ -80,6 +84,9 @@ gb_internal gb_inline void map_init(PtrMap *h, gbAllocator a, isize capaci template gb_internal gb_inline void map_destroy(PtrMap *h) { + if (h->entries.allocator.proc == nullptr) { + h->entries.allocator = map_allocator(); + } slice_free(&h->hashes, h->entries.allocator); array_free(&h->entries); } @@ -162,6 +169,9 @@ gb_internal void map_reset_entries(PtrMap *h) { template gb_internal void map_reserve(PtrMap *h, isize cap) { + if (h->entries.allocator.proc == nullptr) { + h->entries.allocator = map_allocator(); + } array_reserve(&h->entries, cap); if (h->entries.count*2 < h->hashes.count) { return; diff --git a/src/string_map.cpp b/src/string_map.cpp index 9f9374ece..b5db63e90 100644 --- a/src/string_map.cpp +++ b/src/string_map.cpp @@ -35,7 +35,7 @@ struct StringMap { }; -template gb_internal void string_map_init (StringMap *h, gbAllocator a, isize capacity = 16); +template gb_internal void string_map_init (StringMap *h, isize capacity = 16); template gb_internal void string_map_destroy (StringMap *h); template gb_internal T * string_map_get (StringMap *h, char const *key); @@ -56,11 +56,15 @@ template gb_internal void string_map_grow (StringMap template gb_internal void string_map_rehash (StringMap *h, isize new_count); template gb_internal void string_map_reserve (StringMap *h, isize cap); +gb_internal gbAllocator string_map_allocator(void) { + return heap_allocator(); +} + template -gb_internal gb_inline void string_map_init(StringMap *h, gbAllocator a, isize capacity) { +gb_internal gb_inline void string_map_init(StringMap *h, isize capacity) { capacity = next_pow2_isize(capacity); - slice_init(&h->hashes, a, capacity); - array_init(&h->entries, a, 0, capacity); + slice_init(&h->hashes, string_map_allocator(), capacity); + array_init(&h->entries, string_map_allocator(), 0, capacity); for (isize i = 0; i < capacity; i++) { h->hashes.data[i] = MAP_SENTINEL; } @@ -68,6 +72,9 @@ gb_internal gb_inline void string_map_init(StringMap *h, gbAllocator a, isize template gb_internal gb_inline void string_map_destroy(StringMap *h) { + if (h->entries.allocator.proc == nullptr) { + h->entries.allocator = string_map_allocator(); + } slice_free(&h->hashes, h->entries.allocator); array_free(&h->entries); } @@ -147,6 +154,9 @@ gb_internal void string_map_reset_entries(StringMap *h) { template gb_internal void string_map_reserve(StringMap *h, isize cap) { + if (h->entries.allocator.proc == nullptr) { + h->entries.allocator = string_map_allocator(); + } array_reserve(&h->entries, cap); if (h->entries.count*2 < h->hashes.count) { return; -- cgit v1.2.3 From 213a0499a1964e0bc5d2c48cd3b4450b45f59314 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 5 Jan 2023 12:29:16 +0000 Subject: Begin multithreading the llvm backend when `-use-separate-modules` is enabled --- src/llvm_backend.cpp | 46 +++++++++++++++++++++++++++++--------------- src/llvm_backend.hpp | 3 +++ src/llvm_backend_general.cpp | 21 +++++++++++++++++--- src/llvm_backend_stmt.cpp | 2 +- 4 files changed, 53 insertions(+), 19 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 192e5cc56..f3c4dd50d 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -730,6 +730,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); + lbProcedure **found = map_get(&m->gen->anonymous_proc_lits, expr); if (found) { return lb_find_procedure_value_from_entity(m, (*found)->entity); @@ -1526,6 +1528,10 @@ struct lbLLVMModulePassWorkerData { gb_internal WORKER_TASK_PROC(lb_llvm_module_pass_worker_proc) { auto wd = cast(lbLLVMModulePassWorkerData *)data; + + lb_run_remove_unused_function_pass(wd->m); + lb_run_remove_unused_globals_pass(wd->m); + LLVMPassManagerRef module_pass_manager = LLVMCreatePassManager(); lb_populate_module_pass_manager(wd->target_machine, module_pass_manager, build_context.optimization_level); LLVMRunPassManager(module_pass_manager, wd->m->mod); @@ -2155,29 +2161,47 @@ gb_internal void lb_generate_code(lbGenerator *gen) { } } + isize non_empty_module_count = 0; + for (auto const &entry : gen->modules) { + lbModule *m = entry.value; + if (!lb_is_module_empty(m)) { + non_empty_module_count += 1; + } + } + if (non_empty_module_count <= 1) { + do_threading = false; + } TIME_SECTION("LLVM Function Pass"); for (auto const &entry : gen->modules) { lbModule *m = entry.value; - lb_llvm_function_pass_worker_proc(m); + // if (do_threading) { + // thread_pool_add_task(lb_llvm_function_pass_worker_proc, m); + // } else { + lb_llvm_function_pass_worker_proc(m); + // } } + thread_pool_wait(); TIME_SECTION("LLVM Module Pass"); - for (auto const &entry : gen->modules) { lbModule *m = entry.value; - lb_run_remove_unused_function_pass(m); - lb_run_remove_unused_globals_pass(m); - auto wd = gb_alloc_item(permanent_allocator(), lbLLVMModulePassWorkerData); wd->m = m; wd->target_machine = m->target_machine; - lb_llvm_module_pass_worker_proc(wd); + if (do_threading) { + thread_pool_add_task(lb_llvm_module_pass_worker_proc, wd); + } else { + lb_llvm_module_pass_worker_proc(wd); + } } + thread_pool_wait(); + TIME_SECTION("LLVM Module Verification"); + llvm_error = nullptr; defer (LLVMDisposeMessage(llvm_error)); @@ -2245,21 +2269,13 @@ gb_internal void lb_generate_code(lbGenerator *gen) { TIME_SECTION("LLVM Object Generation"); - isize non_empty_module_count = 0; - for (auto const &entry : gen->modules) { - lbModule *m = entry.value; - if (!lb_is_module_empty(m)) { - non_empty_module_count += 1; - } - } - if (build_context.ignore_llvm_build) { gb_printf_err("LLVM SUCCESS!\n"); gb_exit(1); return; } - if (do_threading && non_empty_module_count > 1) { + if (do_threading) { for (auto const &entry : gen->modules) { lbModule *m = entry.value; if (lb_is_module_empty(m)) { diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 9f7caa3bb..d824b99cf 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -132,6 +132,8 @@ struct lbModule { PtrMap struct_field_remapping; // Key: LLVMTypeRef or Type * i32 internal_type_level; + RecursiveMutex values_mutex; + PtrMap values; PtrMap soa_values; StringMap members; @@ -178,6 +180,7 @@ struct lbGenerator { PtrMap modules_through_ctx; lbModule default_module; + BlockingMutex anonymous_proc_lits_mutex; PtrMap anonymous_proc_lits; BlockingMutex foreign_mutex; diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index a849929f0..c09648825 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -316,6 +316,7 @@ gb_internal bool lb_is_instr_terminating(LLVMValueRef instr) { gb_internal lbModule *lb_pkg_module(lbGenerator *gen, AstPackage *pkg) { + // NOTE(bill): no need for a mutex since it's immutable auto *found = map_get(&gen->modules, pkg); if (found) { return *found; @@ -1354,7 +1355,7 @@ gb_internal String lb_mangle_name(lbModule *m, Entity *e) { return mangled_name; } -gb_internal String lb_set_nested_type_name_ir_mangled_name(Entity *e, lbProcedure *p) { +gb_internal String lb_set_nested_type_name_ir_mangled_name(Entity *e, lbProcedure *p, lbModule *module) { // NOTE(bill, 2020-03-08): A polymorphic procedure may take a nested type declaration // and as a result, the declaration does not have time to determine what it should be @@ -1421,7 +1422,7 @@ gb_internal String lb_get_entity_name(lbModule *m, Entity *e, String default_nam } if (e->kind == Entity_TypeName && (e->scope->flags & ScopeFlag_File) == 0) { - return lb_set_nested_type_name_ir_mangled_name(e, nullptr); + return lb_set_nested_type_name_ir_mangled_name(e, nullptr, m); } String name = {}; @@ -2164,19 +2165,25 @@ gb_internal void lb_ensure_abi_function_type(lbModule *m, lbProcedure *p) { gb_internal void lb_add_entity(lbModule *m, Entity *e, lbValue val) { if (e != nullptr) { + mutex_lock(&m->values_mutex); map_set(&m->values, e, val); + mutex_unlock(&m->values_mutex); } } gb_internal void lb_add_member(lbModule *m, String const &name, lbValue val) { if (name.len > 0) { + mutex_lock(&m->values_mutex); string_map_set(&m->members, name, val); + mutex_unlock(&m->values_mutex); } } gb_internal void lb_add_procedure_value(lbModule *m, lbProcedure *p) { + mutex_lock(&m->values_mutex); if (p->entity != nullptr) { map_set(&m->procedure_values, p->value, p->entity); } string_map_set(&m->procedures, p->name, p); + mutex_unlock(&m->values_mutex); } @@ -2519,6 +2526,8 @@ gb_internal lbValue lb_find_ident(lbProcedure *p, lbModule *m, Entity *e, Ast *e return *found; } } + mutex_lock(&m->values_mutex); + defer (mutex_unlock(&m->values_mutex)); auto *found = map_get(&m->values, e); if (found) { @@ -2538,7 +2547,6 @@ gb_internal lbValue lb_find_ident(lbProcedure *p, lbModule *m, Entity *e, Ast *e if (USE_SEPARATE_MODULES) { lbModule *other_module = lb_pkg_module(m->gen, e->pkg); if (other_module != m) { - String name = lb_get_entity_name(other_module, e); lb_set_entity_from_other_modules_linkage_correctly(other_module, e, name); @@ -2569,6 +2577,9 @@ gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) e = strip_entity_wrapping(e); GB_ASSERT(e != nullptr); + mutex_lock(&m->values_mutex); + defer (mutex_unlock(&m->values_mutex)); + auto *found = map_get(&m->values, e); if (found) { return *found; @@ -2657,6 +2668,10 @@ gb_internal lbValue lb_find_value_from_entity(lbModule *m, Entity *e) { return lb_find_procedure_value_from_entity(m, e); } + mutex_lock(&m->values_mutex); + defer (mutex_unlock(&m->values_mutex)); + + auto *found = map_get(&m->values, e); if (found) { return *found; diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 0e6f75118..73b4e251f 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -32,7 +32,7 @@ gb_internal void lb_build_constant_value_decl(lbProcedure *p, AstValueDecl *vd) continue; } - lb_set_nested_type_name_ir_mangled_name(e, p); + lb_set_nested_type_name_ir_mangled_name(e, p, p->module); } for_array(i, vd->names) { -- cgit v1.2.3 From 8ef406324bd500cfd9f3d857e3a5d51adce33374 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 5 Jan 2023 17:26:51 +0000 Subject: Multi thread more of the backend where possible --- src/llvm_backend.cpp | 126 ++++++++++++++++++++++++++++--------------- src/llvm_backend.hpp | 1 + src/llvm_backend_general.cpp | 1 + 3 files changed, 85 insertions(+), 43 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 6109ca247..e959b4741 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1159,7 +1159,24 @@ gb_internal lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProc return p; } -gb_internal void lb_create_global_procedures_and_types(lbGenerator *gen, CheckerInfo *info) { +gb_internal WORKER_TASK_PROC(lb_generate_procedures_and_types_per_module) { + lbModule *m = cast(lbModule *)data; + for (Entity *e : m->global_procedures_and_types_to_create) { + String mangled_name = lb_get_entity_name(m, e); + + switch (e->kind) { + case Entity_TypeName: + lb_type(m, e->type); + break; + case Entity_Procedure: + array_add(&m->procedures_to_generate, lb_create_procedure(m, e)); + break; + } + } + return 0; +} + +gb_internal void lb_create_global_procedures_and_types(lbGenerator *gen, CheckerInfo *info, bool do_threading) { auto *min_dep_set = &info->minimum_dependency_set; for (Entity *e : info->entities) { @@ -1208,20 +1225,19 @@ gb_internal void lb_create_global_procedures_and_types(lbGenerator *gen, Checker m = lb_pkg_module(gen, e->pkg); } - String mangled_name = lb_get_entity_name(m, e); + array_add(&m->global_procedures_and_types_to_create, e); + } - switch (e->kind) { - case Entity_TypeName: - lb_type(m, e->type); - break; - case Entity_Procedure: - { - lbProcedure *p = lb_create_procedure(m, e); - array_add(&m->procedures_to_generate, p); - } - break; + for (auto const &entry : gen->modules) { + lbModule *m = entry.value; + if (do_threading) { + thread_pool_add_task(lb_generate_procedures_and_types_per_module, m); + } else { + lb_generate_procedures_and_types_per_module(m); } } + + thread_pool_wait(); } gb_internal void lb_generate_procedure(lbModule *m, lbProcedure *p); @@ -1277,7 +1293,8 @@ gb_internal void lb_llvm_function_pass_per_function_internal(lbModule *module, l lb_run_function_pass_manager(pass_manager, p); } -gb_internal void lb_llvm_function_pass_per_module(lbModule *m) { +gb_internal WORKER_TASK_PROC(lb_llvm_function_pass_per_module) { + lbModule *m = cast(lbModule *)data; { GB_ASSERT(m->function_pass_managers[lbFunctionPassManager_default] == nullptr); @@ -1354,6 +1371,8 @@ gb_internal void lb_llvm_function_pass_per_module(lbModule *m) { lbProcedure *p = entry.value; lb_llvm_function_pass_per_function_internal(m, p); } + + return 0; } @@ -1432,10 +1451,14 @@ gb_internal void lb_debug_info_complete_types_and_finalize(lbGenerator *gen) { } } -gb_internal void lb_llvm_function_passes(lbGenerator *gen) { +gb_internal void lb_llvm_function_passes(lbGenerator *gen, bool do_threading) { for (auto const &entry : gen->modules) { lbModule *m = entry.value; - lb_llvm_function_pass_per_module(m); + if (do_threading) { + thread_pool_add_task(lb_llvm_function_pass_per_module, m); + } else { + lb_llvm_function_pass_per_module(m); + } } thread_pool_wait(); } @@ -1523,25 +1546,41 @@ gb_internal String lb_filepath_obj_for_module(lbModule *m) { return concatenate_strings(permanent_allocator(), path, ext); } -gb_internal bool lb_llvm_module_verification(lbGenerator *gen, char **llvm_error_ptr) { +gb_internal WORKER_TASK_PROC(lb_llvm_module_verification_worker_proc) { + char *llvm_error = nullptr; + defer (LLVMDisposeMessage(llvm_error)); + lbModule *m = cast(lbModule *)data; + if (LLVMVerifyModule(m->mod, LLVMReturnStatusAction, &llvm_error)) { + gb_printf_err("LLVM Error:\n%s\n", llvm_error); + if (build_context.keep_temp_files) { + TIME_SECTION("LLVM Print Module to File"); + String filepath_ll = lb_filepath_ll_for_module(m); + if (LLVMPrintModuleToFile(m->mod, cast(char const *)filepath_ll.text, &llvm_error)) { + gb_printf_err("LLVM Error: %s\n", llvm_error); + gb_exit(1); + return false; + } + } + gb_exit(1); + return 1; + } + return 0; +} + + +gb_internal bool lb_llvm_module_verification(lbGenerator *gen, bool do_threading) { for (auto const &entry : gen->modules) { lbModule *m = entry.value; - if (LLVMVerifyModule(m->mod, LLVMReturnStatusAction, llvm_error_ptr)) { - gb_printf_err("LLVM Error:\n%s\n", *llvm_error_ptr); - if (build_context.keep_temp_files) { - TIME_SECTION("LLVM Print Module to File"); - String filepath_ll = lb_filepath_ll_for_module(m); - if (LLVMPrintModuleToFile(m->mod, cast(char const *)filepath_ll.text, llvm_error_ptr)) { - gb_printf_err("LLVM Error: %s\n", *llvm_error_ptr); - gb_exit(1); - return false; - } + if (do_threading) { + thread_pool_add_task(lb_llvm_module_verification_worker_proc, m); + } else { + if (lb_llvm_module_verification_worker_proc(m)) { + return false; } - gb_exit(1); - return false; } } - *llvm_error_ptr = nullptr; + thread_pool_wait(); + return true; } @@ -1558,7 +1597,12 @@ gb_internal void lb_add_foreign_library_paths(lbGenerator *gen) { } } -gb_internal bool lb_llvm_object_generation(lbGenerator *gen, LLVMCodeGenFileType code_gen_file_type, bool do_threading) { +gb_internal bool lb_llvm_object_generation(lbGenerator *gen, bool do_threading) { + LLVMCodeGenFileType code_gen_file_type = LLVMObjectFile; + if (build_context.build_mode == BuildMode_Assembly) { + code_gen_file_type = LLVMAssemblyFile; + } + char *llvm_error = nullptr; defer (LLVMDisposeMessage(llvm_error)); @@ -2281,13 +2325,13 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { } } - TIME_SECTION("LLVM Global Procedures and Types"); - lb_create_global_procedures_and_types(gen, info); - if (gen->modules.entries.count <= 1) { do_threading = false; } + TIME_SECTION("LLVM Global Procedures and Types"); + lb_create_global_procedures_and_types(gen, info, do_threading); + TIME_SECTION("LLVM Procedure Generation"); lb_generate_procedures(gen, do_threading); @@ -2324,7 +2368,7 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { TIME_SECTION("LLVM Function Pass"); - lb_llvm_function_passes(gen); + lb_llvm_function_passes(gen, do_threading); TIME_SECTION("LLVM Module Pass"); lb_llvm_module_passes(gen, do_threading); @@ -2332,18 +2376,14 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { TIME_SECTION("LLVM Module Verification"); - llvm_error = nullptr; - defer (LLVMDisposeMessage(llvm_error)); - LLVMCodeGenFileType code_gen_file_type = LLVMObjectFile; - if (build_context.build_mode == BuildMode_Assembly) { - code_gen_file_type = LLVMAssemblyFile; - } - - if (!lb_llvm_module_verification(gen, &llvm_error)) { + if (!lb_llvm_module_verification(gen, do_threading)) { return false; } + llvm_error = nullptr; + defer (LLVMDisposeMessage(llvm_error)); + if (build_context.keep_temp_files || build_context.build_mode == BuildMode_LLVM_IR) { TIME_SECTION("LLVM Print Module to File"); @@ -2377,7 +2417,7 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { gb_printf_err("LLVM object generation has been ignored!\n"); return false; } - if (!lb_llvm_object_generation(gen, code_gen_file_type, do_threading)) { + if (!lb_llvm_object_generation(gen, do_threading)) { return false; } diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 98b657256..90dfbc311 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -163,6 +163,7 @@ struct lbModule { u32 nested_type_name_guid; Array procedures_to_generate; + Array global_procedures_and_types_to_create; lbProcedure *curr_procedure; diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index c09648825..dca8c829d 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -70,6 +70,7 @@ gb_internal void lb_init_module(lbModule *m, Checker *c) { map_init(&m->map_get_procs); map_init(&m->map_set_procs); array_init(&m->procedures_to_generate, a, 0, 1024); + array_init(&m->global_procedures_and_types_to_create, a, 0, 1024); array_init(&m->missing_procedures_to_check, a, 0, 16); map_init(&m->debug_values); array_init(&m->debug_incomplete_types, a, 0, 1024); -- cgit v1.2.3 From 402a165b60dc9de1ef047b44e47c2d38e5cbed6d Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 12 Jan 2023 16:59:16 +0000 Subject: Correct missing procedures in other build modules which cause a linkage problem --- src/checker.cpp | 2 +- src/llvm_backend.cpp | 11 +++-------- src/llvm_backend_general.cpp | 10 +++++++++- src/llvm_backend_proc.cpp | 14 ++++++++------ 4 files changed, 21 insertions(+), 16 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/checker.cpp b/src/checker.cpp index 7be3c71d1..be3ce1a77 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -5092,7 +5092,7 @@ gb_internal bool check_proc_info(Checker *c, ProcInfo *pi, UntypedExprInfoMap *u if ((e->flags & EntityFlag_Used) == 0) { // NOTE(bill, 2019-08-31): It was never used, don't check // NOTE(bill, 2023-01-02): This may need to be checked again if it is used elsewhere? - pi->decl->proc_checked_state.store(ProcCheckedState_Unchecked); + pi->decl->proc_checked_state.store(ProcCheckedState_Unchecked); return false; } } diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index fef222817..4c954b58f 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1852,6 +1852,7 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { isize worker_count = thread_count-1; bool do_threading = !!(LLVMIsMultithreaded() && USE_SEPARATE_MODULES && MULTITHREAD_OBJECT_GENERATION && worker_count > 0); + do_threading = false; lbModule *default_module = &gen->default_module; CheckerInfo *info = gen->info; @@ -2067,9 +2068,7 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { Type *t = alloc_type_array(t_type_info, max_type_info_count); LLVMValueRef g = LLVMAddGlobal(m->mod, lb_type(m, t), LB_TYPE_INFO_DATA_NAME); LLVMSetInitializer(g, LLVMConstNull(lb_type(m, t))); - if (!USE_SEPARATE_MODULES) { - LLVMSetLinkage(g, LLVMInternalLinkage); - } + LLVMSetLinkage(g, USE_SEPARATE_MODULES ? LLVMExternalLinkage : LLVMInternalLinkage); lbValue value = {}; value.value = g; @@ -2241,11 +2240,7 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { LLVMSetLinkage(g.value, LLVMDLLExportLinkage); LLVMSetDLLStorageClass(g.value, LLVMDLLExportStorageClass); } else if (!is_foreign) { - if (USE_SEPARATE_MODULES) { - LLVMSetLinkage(g.value, LLVMExternalLinkage); - } else { - LLVMSetLinkage(g.value, LLVMInternalLinkage); - } + LLVMSetLinkage(g.value, USE_SEPARATE_MODULES ? LLVMExternalLinkage : LLVMInternalLinkage); } lb_set_linkage_from_entity_flags(m, g.value, e->flags); diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index dca8c829d..cae3ab1ee 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -2598,7 +2598,15 @@ gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) ignore_body = other_module != m; lbProcedure *missing_proc = lb_create_procedure(m, e, ignore_body); - if (!ignore_body) { + if (ignore_body) { + mutex_lock(&other_module->values_mutex); + auto *found = map_get(&other_module->values, e); + if (found == nullptr) { + 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); + } + mutex_unlock(&other_module->values_mutex); + } else { array_add(&m->missing_procedures_to_check, missing_proc); } found = map_get(&m->values, e); diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index c66462bc1..fd654cec9 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -190,11 +190,6 @@ gb_internal lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool i lb_add_attribute_to_proc(m, p->value, "cold"); } - lbValue proc_value = {p->value, p->type}; - lb_add_entity(m, entity, proc_value); - lb_add_member(m, p->name, proc_value); - lb_add_procedure_value(m, p); - if (p->is_export) { LLVMSetLinkage(p->value, LLVMDLLExportLinkage); LLVMSetDLLStorageClass(p->value, LLVMDLLExportStorageClass); @@ -202,7 +197,9 @@ gb_internal lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool i lb_set_wasm_export_attributes(p->value, p->name); } else if (!p->is_foreign) { - if (!USE_SEPARATE_MODULES) { + if (USE_SEPARATE_MODULES) { + LLVMSetLinkage(p->value, LLVMExternalLinkage); + } else { LLVMSetLinkage(p->value, LLVMInternalLinkage); // NOTE(bill): if a procedure is defined in package runtime and uses a custom link name, @@ -316,6 +313,11 @@ gb_internal lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool i } } + lbValue proc_value = {p->value, p->type}; + lb_add_entity(m, entity, proc_value); + lb_add_member(m, p->name, proc_value); + lb_add_procedure_value(m, p); + return p; } -- cgit v1.2.3 From 3b22c6620cea89394ad7a2b80da5f2529a4fae20 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 12 Jan 2023 17:13:25 +0000 Subject: Begin to generalize modules away from `AstPackage *` in `-use-separate-modules` --- src/llvm_backend.cpp | 2 +- src/llvm_backend.hpp | 2 +- src/llvm_backend_debug.cpp | 2 +- src/llvm_backend_general.cpp | 29 +++++++++++++++++------------ src/llvm_backend_proc.cpp | 2 +- 5 files changed, 21 insertions(+), 16 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 4c954b58f..f0d51be73 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1222,7 +1222,7 @@ gb_internal void lb_create_global_procedures_and_types(lbGenerator *gen, Checker lbModule *m = &gen->default_module; if (USE_SEPARATE_MODULES) { - m = lb_pkg_module(gen, e->pkg); + m = lb_module_of_entity(gen, e); } array_add(&m->global_procedures_and_types_to_create, e); diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index de4deffd4..4288ae16e 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -191,7 +191,7 @@ struct lbGenerator { Array output_temp_paths; String output_base; String output_name; - PtrMap modules; + PtrMap modules; // key is `AstPackage *` (`void *` is used for future use) PtrMap modules_through_ctx; lbModule default_module; diff --git a/src/llvm_backend_debug.cpp b/src/llvm_backend_debug.cpp index 9bf4063d6..83f6da04e 100644 --- a/src/llvm_backend_debug.cpp +++ b/src/llvm_backend_debug.cpp @@ -1177,7 +1177,7 @@ gb_internal void add_debug_info_for_global_constant_from_entity(lbGenerator *gen } lbModule *m = &gen->default_module; if (USE_SEPARATE_MODULES) { - m = lb_pkg_module(gen, e->pkg); + m = lb_module_of_entity(gen, e); } if (is_type_integer(e->type)) { diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index cae3ab1ee..516c46396 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -143,13 +143,13 @@ gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) { auto m = gb_alloc_item(permanent_allocator(), lbModule); m->pkg = pkg; m->gen = gen; - map_set(&gen->modules, pkg, m); + map_set(&gen->modules, cast(void *)pkg, m); lb_init_module(m, c); } } gen->default_module.gen = gen; - map_set(&gen->modules, cast(AstPackage *)nullptr, &gen->default_module); + map_set(&gen->modules, cast(void *)nullptr, &gen->default_module); lb_init_module(&gen->default_module, c); @@ -315,17 +315,22 @@ gb_internal bool lb_is_instr_terminating(LLVMValueRef instr) { } - -gb_internal lbModule *lb_pkg_module(lbGenerator *gen, AstPackage *pkg) { - // NOTE(bill): no need for a mutex since it's immutable - auto *found = map_get(&gen->modules, pkg); - if (found) { - return *found; +gb_internal lbModule *lb_module_of_entity(lbGenerator *gen, Entity *e) { + GB_ASSERT(e != nullptr); + if (e->pkg) { + lbModule **found = nullptr; + found = map_get(&gen->modules, cast(void *)e->file); + if (found) { + return *found; + } + found = map_get(&gen->modules, cast(void *)e->pkg); + if (found) { + return *found; + } } return &gen->default_module; } - gb_internal lbAddr lb_addr(lbValue addr) { lbAddr v = {lbAddr_Default, addr}; if (addr.type != nullptr && is_type_relative_pointer(type_deref(addr.type))) { @@ -2546,7 +2551,7 @@ gb_internal lbValue lb_find_ident(lbProcedure *p, lbModule *m, Entity *e, Ast *e return lb_find_procedure_value_from_entity(m, e); } if (USE_SEPARATE_MODULES) { - lbModule *other_module = lb_pkg_module(m->gen, e->pkg); + lbModule *other_module = lb_module_of_entity(m->gen, e); if (other_module != m) { String name = lb_get_entity_name(other_module, e); @@ -2590,7 +2595,7 @@ gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) lbModule *other_module = m; if (USE_SEPARATE_MODULES) { - other_module = lb_pkg_module(m->gen, e->pkg); + other_module = lb_module_of_entity(m->gen, e); } if (other_module == m) { debugf("Missing Procedure (lb_find_procedure_value_from_entity): %.*s\n", LIT(e->token.string)); @@ -2687,7 +2692,7 @@ gb_internal lbValue lb_find_value_from_entity(lbModule *m, Entity *e) { } if (USE_SEPARATE_MODULES) { - lbModule *other_module = lb_pkg_module(m->gen, e->pkg); + lbModule *other_module = lb_module_of_entity(m->gen, e); // TODO(bill): correct this logic bool is_external = other_module != m; diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index fd654cec9..f156d1ca0 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -74,7 +74,7 @@ gb_internal lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool i String link_name = {}; if (ignore_body) { - lbModule *other_module = lb_pkg_module(m->gen, entity->pkg); + lbModule *other_module = lb_module_of_entity(m->gen, entity); link_name = lb_get_entity_name(other_module, entity); } else { link_name = lb_get_entity_name(m, entity); -- cgit v1.2.3 From 4a8c37dd529e9f09583101f086833cec2c1b71ce Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 12 Jan 2023 21:45:02 +0000 Subject: Prepare for arbitrary separate modules --- src/llvm_backend.cpp | 16 ++++++++++++---- src/llvm_backend.hpp | 3 ++- src/llvm_backend_general.cpp | 16 ++++++++++++++-- 3 files changed, 28 insertions(+), 7 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index f0d51be73..aa49fd055 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1488,7 +1488,12 @@ gb_internal String lb_filepath_ll_for_module(lbModule *m) { build_context.build_paths[BuildPath_Output].name ); - if (m->pkg) { + if (m->file) { + char buf[32] = {}; + isize n = gb_snprintf(buf, gb_size_of(buf), "-%u", m->file->id); + String suffix = make_string((u8 *)buf, n-1); + path = concatenate_strings(permanent_allocator(), path, suffix); + } else if (m->pkg) { path = concatenate3_strings(permanent_allocator(), path, STR_LIT("-"), m->pkg->name); } else if (USE_SEPARATE_MODULES) { path = concatenate_strings(permanent_allocator(), path, STR_LIT("-builtin")); @@ -1504,7 +1509,12 @@ gb_internal String lb_filepath_obj_for_module(lbModule *m) { build_context.build_paths[BuildPath_Output].name ); - if (m->pkg) { + if (m->file) { + char buf[32] = {}; + isize n = gb_snprintf(buf, gb_size_of(buf), "-%u", m->file->id); + String suffix = make_string((u8 *)buf, n-1); + path = concatenate_strings(permanent_allocator(), path, suffix); + } else if (m->pkg) { path = concatenate3_strings(permanent_allocator(), path, STR_LIT("-"), m->pkg->name); } @@ -1852,7 +1862,6 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { isize worker_count = thread_count-1; bool do_threading = !!(LLVMIsMultithreaded() && USE_SEPARATE_MODULES && MULTITHREAD_OBJECT_GENERATION && worker_count > 0); - do_threading = false; lbModule *default_module = &gen->default_module; CheckerInfo *info = gen->info; @@ -2388,7 +2397,6 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { if (lb_is_module_empty(m)) { continue; } - String filepath_ll = lb_filepath_ll_for_module(m); if (LLVMPrintModuleToFile(m->mod, cast(char const *)filepath_ll.text, &llvm_error)) { gb_printf_err("LLVM Error: %s\n", llvm_error); diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 4288ae16e..beb9a99b6 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -135,7 +135,8 @@ struct lbModule { LLVMTargetMachineRef target_machine; CheckerInfo *info; - AstPackage *pkg; // associated + AstPackage *pkg; // possibly associated + AstFile *file; // possibly associated PtrMap types; PtrMap func_raw_types; diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 516c46396..7eb024daf 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -19,7 +19,9 @@ gb_internal void lb_init_module(lbModule *m, Checker *c) { m->info = &c->info; gbString module_name = gb_string_make(heap_allocator(), "odin_package"); - if (m->pkg) { + if (m->file) { + module_name = gb_string_append_fmt(module_name, "-%u", m->file->id+1); + } else if (m->pkg) { module_name = gb_string_appendc(module_name, "-"); module_name = gb_string_append_length(module_name, m->pkg->name.text, m->pkg->name.len); } else if (USE_SEPARATE_MODULES) { @@ -139,12 +141,22 @@ gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) { if (USE_SEPARATE_MODULES) { for (auto const &entry : gen->info->packages) { AstPackage *pkg = entry.value; - + #if 1 auto m = gb_alloc_item(permanent_allocator(), lbModule); m->pkg = pkg; m->gen = gen; map_set(&gen->modules, cast(void *)pkg, m); lb_init_module(m, c); + #else + for (AstFile *file : pkg->files) { + auto m = gb_alloc_item(permanent_allocator(), lbModule); + m->file = file; + m->pkg = pkg; + m->gen = gen; + map_set(&gen->modules, cast(void *)file, m); + lb_init_module(m, c); + } + #endif } } -- cgit v1.2.3 From 1e211255270eb43aaf3646d7e62da63b0b855e17 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 12 Jan 2023 21:49:53 +0000 Subject: Split out object generation more --- src/llvm_backend_general.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 7eb024daf..15cb3f2be 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -329,12 +329,14 @@ gb_internal bool lb_is_instr_terminating(LLVMValueRef instr) { gb_internal lbModule *lb_module_of_entity(lbGenerator *gen, Entity *e) { GB_ASSERT(e != nullptr); - if (e->pkg) { - lbModule **found = nullptr; + lbModule **found = nullptr; + if (e->file) { found = map_get(&gen->modules, cast(void *)e->file); if (found) { return *found; } + } + if (e->pkg) { found = map_get(&gen->modules, cast(void *)e->pkg); if (found) { return *found; -- cgit v1.2.3 From 1064bcd0602c9ff86e2a304ecb46b8d86bb07d52 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 13 Jan 2023 12:12:41 +0000 Subject: Clean up use of `StringMap` --- src/checker.cpp | 11 +++---- src/llvm_backend.cpp | 1 + src/llvm_backend_general.cpp | 1 + src/string_map.cpp | 75 +++++++++++++++++++------------------------- 4 files changed, 39 insertions(+), 49 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/checker.cpp b/src/checker.cpp index be3ce1a77..770e0d473 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -58,11 +58,8 @@ gb_internal void scope_reset(Scope *scope) { rw_mutex_unlock(&scope->mutex); } -gb_internal void scope_reserve(Scope *scope, isize capacity) { - isize cap = 2*capacity; - if (cap > scope->elements.hashes.count) { - string_map_rehash(&scope->elements, capacity); - } +gb_internal void scope_reserve(Scope *scope, isize count) { + string_map_reserve(&scope->elements, 2*count); } gb_internal void entity_graph_node_set_destroy(EntityGraphNodeSet *s) { @@ -4699,7 +4696,7 @@ gb_internal void check_collect_entities_all(Checker *c) { map_init(&wd->untyped); } - for (auto const &entry : c->info.files.entries) { + for (auto const &entry : c->info.files) { AstFile *f = entry.value; thread_pool_add_task(check_collect_entities_all_worker_proc, f); } @@ -4739,7 +4736,7 @@ gb_internal void check_export_entities(Checker *c) { wd->ctx = make_checker_context(c); } - for (auto const &entry : c->info.packages.entries) { + for (auto const &entry : c->info.packages) { AstPackage *pkg = entry.value; thread_pool_add_task(check_export_entities_worker_proc, pkg); } diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index aa49fd055..148511028 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1625,6 +1625,7 @@ gb_internal bool lb_llvm_object_generation(lbGenerator *gen, bool do_threading) String filepath_ll = lb_filepath_ll_for_module(m); String filepath_obj = lb_filepath_obj_for_module(m); + gb_printf_err("%.*s\n", LIT(filepath_obj)); array_add(&gen->output_object_paths, filepath_obj); array_add(&gen->output_temp_paths, filepath_ll); diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 15cb3f2be..c4f6ccc91 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -148,6 +148,7 @@ gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) { map_set(&gen->modules, cast(void *)pkg, m); lb_init_module(m, c); #else + // NOTE(bill): Probably per file is not a good idea, so leave this for later for (AstFile *file : pkg->files) { auto m = gb_alloc_item(permanent_allocator(), lbModule); m->file = file; diff --git a/src/string_map.cpp b/src/string_map.cpp index facd00bb0..f7ecc4acc 100644 --- a/src/string_map.cpp +++ b/src/string_map.cpp @@ -35,8 +35,6 @@ struct StringMapEntry { template struct StringMap { - using K = String; - using V = T; Slice hashes; Array > entries; }; @@ -53,15 +51,14 @@ template gb_internal T & string_map_must_get (StringMap template gb_internal T & string_map_must_get (StringMap *h, String const &key); template gb_internal T & string_map_must_get (StringMap *h, StringHashKey const &key); -template gb_internal void string_map_set (StringMap *h, StringHashKey const &key, T const &value); -template gb_internal void string_map_set (StringMap *h, String const &key, T const &value); template gb_internal void string_map_set (StringMap *h, char const *key, T const &value); +template gb_internal void string_map_set (StringMap *h, String const &key, T const &value); +template gb_internal void string_map_set (StringMap *h, StringHashKey const &key, T const &value); -template gb_internal void string_map_remove (StringMap *h, StringHashKey const &key); +// template gb_internal void string_map_remove (StringMap *h, StringHashKey const &key); template gb_internal void string_map_clear (StringMap *h); template gb_internal void string_map_grow (StringMap *h); -template gb_internal void string_map_rehash (StringMap *h, isize new_count); -template gb_internal void string_map_reserve (StringMap *h, isize cap); +template gb_internal void string_map_reserve (StringMap *h, isize new_count); gb_internal gbAllocator string_map_allocator(void) { return heap_allocator(); @@ -137,7 +134,7 @@ gb_internal b32 string_map__full(StringMap *h) { template gb_inline void string_map_grow(StringMap *h) { isize new_count = gb_max(h->hashes.count<<1, 16); - string_map_rehash(h, new_count); + string_map_reserve(h, new_count); } @@ -172,12 +169,6 @@ gb_internal void string_map_reserve(StringMap *h, isize cap) { string_map_reset_entries(h); } - -template -gb_internal void string_map_rehash(StringMap *h, isize new_count) { - string_map_reserve(h, new_count); -} - template gb_internal T *string_map_get(StringMap *h, StringHashKey const &key) { MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL}; @@ -259,34 +250,34 @@ gb_internal gb_inline void string_map_set(StringMap *h, char const *key, T co } -template -gb_internal void string_map__erase(StringMap *h, MapFindResult const &fr) { - MapFindResult last; - if (fr.entry_prev == MAP_SENTINEL) { - h->hashes.data[fr.hash_index] = h->entries.data[fr.entry_index].next; - } else { - h->entries.data[fr.entry_prev].next = h->entries.data[fr.entry_index].next; - } - if (fr.entry_index == h->entries.count-1) { - array_pop(&h->entries); - return; - } - h->entries.data[fr.entry_index] = h->entries.data[h->entries.count-1]; - last = string_map__find(h, h->entries.data[fr.entry_index].key); - if (last.entry_prev != MAP_SENTINEL) { - h->entries.data[last.entry_prev].next = fr.entry_index; - } else { - h->hashes.data[last.hash_index] = fr.entry_index; - } -} - -template -gb_internal void string_map_remove(StringMap *h, StringHashKey const &key) { - MapFindResult fr = string_map__find(h, key); - if (fr.entry_index != MAP_SENTINEL) { - string_map__erase(h, fr); - } -} +// template +// gb_internal void string_map__erase(StringMap *h, MapFindResult const &fr) { +// MapFindResult last; +// if (fr.entry_prev == MAP_SENTINEL) { +// h->hashes.data[fr.hash_index] = h->entries.data[fr.entry_index].next; +// } else { +// h->entries.data[fr.entry_prev].next = h->entries.data[fr.entry_index].next; +// } +// if (fr.entry_index == h->entries.count-1) { +// array_pop(&h->entries); +// return; +// } +// h->entries.data[fr.entry_index] = h->entries.data[h->entries.count-1]; +// last = string_map__find(h, h->entries.data[fr.entry_index].key); +// if (last.entry_prev != MAP_SENTINEL) { +// h->entries.data[last.entry_prev].next = fr.entry_index; +// } else { +// h->hashes.data[last.hash_index] = fr.entry_index; +// } +// } + +// template +// gb_internal void string_map_remove(StringMap *h, StringHashKey const &key) { +// MapFindResult fr = string_map__find(h, key); +// if (fr.entry_index != MAP_SENTINEL) { +// string_map__erase(h, fr); +// } +// } template gb_internal gb_inline void string_map_clear(StringMap *h) { -- cgit v1.2.3 From 1ab90de4931f07ea61b1195de602f282a853568b Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 14 Jan 2023 12:33:42 +0000 Subject: Minimize `StringMap` structure usage --- src/check_expr.cpp | 2 +- src/check_stmt.cpp | 4 +- src/checker.cpp | 10 +- src/common_memory.cpp | 7 +- src/docs.cpp | 4 +- src/docs_writer.cpp | 4 +- src/llvm_backend.cpp | 4 +- src/llvm_backend_general.cpp | 4 +- src/string_map.cpp | 226 ++++++++++++++++++++++++------------------- 9 files changed, 146 insertions(+), 119 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 52efd9a66..ff74a2547 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -234,7 +234,7 @@ gb_internal void check_did_you_mean_type(String const &name, Slice con gb_internal void check_did_you_mean_scope(String const &name, Scope *scope, char const *prefix = "") { ERROR_BLOCK(); - DidYouMeanAnswers d = did_you_mean_make(heap_allocator(), scope->elements.entries.count, name); + DidYouMeanAnswers d = did_you_mean_make(heap_allocator(), scope->elements.count, name); defer (did_you_mean_destroy(&d)); rw_mutex_shared_lock(&scope->mutex); diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index a2a688b13..21df1d0ea 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -626,7 +626,7 @@ gb_internal bool check_using_stmt_entity(CheckerContext *ctx, AstUsingStmt *us, defer (rw_mutex_unlock(&scope->mutex)); for (auto const &entry : scope->elements) { - String name = entry.key.string; + String name = entry.key; Entity *decl = entry.value; if (!is_entity_exported(decl)) continue; @@ -1323,7 +1323,7 @@ gb_internal void check_block_stmt_for_errors(CheckerContext *ctx, Ast *body) { ast_node(bs, BlockStmt, body); // NOTE(bill, 2020-09-23): This logic is prevent common erros with block statements // e.g. if cond { x := 123; } // this is an error - if (bs->scope != nullptr && bs->scope->elements.entries.count > 0) { + if (bs->scope != nullptr && bs->scope->elements.count > 0) { if (bs->scope->parent->node != nullptr) { switch (bs->scope->parent->node->kind) { case Ast_IfStmt: diff --git a/src/checker.cpp b/src/checker.cpp index 770e0d473..39a132060 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -765,7 +765,7 @@ gb_internal AstPackage *get_core_package(CheckerInfo *info, String name) { gb_printf_err("Fullpath: %.*s\n", LIT(path)); for (auto const &entry : info->packages) { - gb_printf_err("%.*s\n", LIT(entry.key.string)); + gb_printf_err("%.*s\n", LIT(entry.key)); } GB_ASSERT_MSG(found != nullptr, "Missing core package %.*s", LIT(name)); } @@ -3891,16 +3891,16 @@ gb_internal bool correct_single_type_alias(CheckerContext *c, Entity *e) { } gb_internal bool correct_type_alias_in_scope_backwards(CheckerContext *c, Scope *s) { - isize n = s->elements.entries.count; bool correction = false; - for (isize i = n-1; i >= 0; i--) { + u32 n = s->elements.count; + for (u32 i = n-1; i < n; i--) { correction |= correct_single_type_alias(c, s->elements.entries[i].value); } return correction; } gb_internal bool correct_type_alias_in_scope_forwards(CheckerContext *c, Scope *s) { - isize n = s->elements.entries.count; bool correction = false; + u32 n = s->elements.count; for (isize i = 0; i < n; i++) { correction |= correct_single_type_alias(c, s->elements.entries[i].value); } @@ -5539,7 +5539,7 @@ gb_internal void check_deferred_procedures(Checker *c) { gb_internal void check_unique_package_names(Checker *c) { StringMap pkgs = {}; // Key: package name - string_map_init(&pkgs, 2*c->info.packages.entries.count); + string_map_init(&pkgs, 2*c->info.packages.count); defer (string_map_destroy(&pkgs)); for (auto const &entry : c->info.packages) { diff --git a/src/common_memory.cpp b/src/common_memory.cpp index 7d5d1f27a..22b91d8cb 100644 --- a/src/common_memory.cpp +++ b/src/common_memory.cpp @@ -509,15 +509,15 @@ gb_internal GB_ALLOCATOR_PROC(heap_allocator_proc) { template -gb_internal void resize_array_raw(T **array, gbAllocator const &a, isize old_count, isize new_count) { +gb_internal isize resize_array_raw(T **array, gbAllocator const &a, isize old_count, isize new_count) { GB_ASSERT(new_count >= 0); if (new_count == 0) { gb_free(a, *array); *array = nullptr; - return; + return 0; } if (new_count < old_count) { - return; + return old_count; } isize old_size = old_count * gb_size_of(T); isize new_size = new_count * gb_size_of(T); @@ -525,5 +525,6 @@ gb_internal void resize_array_raw(T **array, gbAllocator const &a, isize old_cou auto new_data = cast(T *)gb_resize_align(a, *array, old_size, new_size, alignment); GB_ASSERT(new_data != nullptr); *array = new_data; + return new_count; } diff --git a/src/docs.cpp b/src/docs.cpp index b1efa2b46..54bb0c7a2 100644 --- a/src/docs.cpp +++ b/src/docs.cpp @@ -210,7 +210,7 @@ gb_internal void print_doc_package(CheckerInfo *info, AstPackage *pkg) { } if (pkg->scope != nullptr) { - auto entities = array_make(heap_allocator(), 0, pkg->scope->elements.entries.count); + auto entities = array_make(heap_allocator(), 0, pkg->scope->elements.count); defer (array_free(&entities)); for (auto const &entry : pkg->scope->elements) { Entity *e = entry.value; @@ -348,7 +348,7 @@ gb_internal void generate_documentation(Checker *c) { odin_doc_write(info, output_file_path); } else { - auto pkgs = array_make(permanent_allocator(), 0, info->packages.entries.count); + auto pkgs = array_make(permanent_allocator(), 0, info->packages.count); for (auto const &entry : info->packages) { AstPackage *pkg = entry.value; if (build_context.cmd_doc_flags & CmdDocFlag_AllPackages) { diff --git a/src/docs_writer.cpp b/src/docs_writer.cpp index 2aefe29eb..ea0946153 100644 --- a/src/docs_writer.cpp +++ b/src/docs_writer.cpp @@ -977,7 +977,7 @@ gb_internal OdinDocArray odin_doc_add_pkg_entries(OdinDocWrit defer (array_free(&entries)); for (auto const &element : pkg->scope->elements) { - String name = element.key.string; + String name = element.key; Entity *e = element.value; switch (e->kind) { case Entity_Invalid: @@ -1016,7 +1016,7 @@ gb_internal OdinDocArray odin_doc_add_pkg_entries(OdinDocWrit gb_internal void odin_doc_write_docs(OdinDocWriter *w) { - auto pkgs = array_make(heap_allocator(), 0, w->info->packages.entries.count); + auto pkgs = array_make(heap_allocator(), 0, w->info->packages.count); defer (array_free(&pkgs)); for (auto const &entry : w->info->packages) { AstPackage *pkg = entry.value; diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 148511028..6cd224324 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1035,14 +1035,14 @@ gb_internal void lb_finalize_objc_names(lbProcedure *p) { LLVMSetLinkage(p->value, LLVMInternalLinkage); lb_begin_procedure_body(p); for (auto const &entry : m->objc_classes) { - String name = entry.key.string; + String name = entry.key; args[0] = lb_const_value(m, t_cstring, exact_value_string(name)); lbValue ptr = lb_emit_runtime_call(p, "objc_lookUpClass", args); lb_addr_store(p, entry.value, ptr); } for (auto const &entry : m->objc_selectors) { - String name = entry.key.string; + String name = entry.key; args[0] = lb_const_value(m, t_cstring, exact_value_string(name)); lbValue ptr = lb_emit_runtime_call(p, "sel_registerName", args); lb_addr_store(p, entry.value, ptr); diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index c4f6ccc91..e7c9a0985 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -130,8 +130,8 @@ gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) { gen->info = &c->info; - map_init(&gen->modules, gen->info->packages.entries.count*2); - map_init(&gen->modules_through_ctx, gen->info->packages.entries.count*2); + map_init(&gen->modules, gen->info->packages.count*2); + map_init(&gen->modules_through_ctx, gen->info->packages.count*2); map_init(&gen->anonymous_proc_lits, 1024); diff --git a/src/string_map.cpp b/src/string_map.cpp index f7ecc4acc..3bd08d09f 100644 --- a/src/string_map.cpp +++ b/src/string_map.cpp @@ -1,3 +1,5 @@ +GB_STATIC_ASSERT(sizeof(MapIndex) == sizeof(u32)); + struct StringHashKey { u32 hash; String string; @@ -9,101 +11,108 @@ struct StringHashKey { return this->string; } }; +gb_internal gb_inline u32 string_hash(String const &s) { + return fnv32a(s.text, s.len) & 0x7fffffff; +} gb_internal gb_inline StringHashKey string_hash_string(String const &s) { StringHashKey hash_key = {}; - hash_key.hash = fnv32a(s.text, s.len); + hash_key.hash = string_hash(s); hash_key.string = s; return hash_key; } - -gb_internal gb_inline bool string_hash_key_equal(StringHashKey const &a, StringHashKey const &b) { - if (a.hash == b.hash) { - // NOTE(bill): If two string's hashes collide, compare the strings themselves - return a.string == b.string; - } - return false; -} - template struct StringMapEntry { - StringHashKey key; + String key; + u32 hash; MapIndex next; T value; }; template struct StringMap { - Slice hashes; - Array > entries; + MapIndex * hashes; + usize hashes_count; + StringMapEntry *entries; + u32 count; + u32 entries_capacity; }; -template gb_internal void string_map_init (StringMap *h, isize capacity = 16); -template gb_internal void string_map_destroy (StringMap *h); +template gb_internal void string_map_init (StringMap *h, usize capacity = 16); +template gb_internal void string_map_destroy (StringMap *h); -template gb_internal T * string_map_get (StringMap *h, char const *key); -template gb_internal T * string_map_get (StringMap *h, String const &key); -template gb_internal T * string_map_get (StringMap *h, StringHashKey const &key); +template gb_internal T * string_map_get (StringMap *h, char const *key); +template gb_internal T * string_map_get (StringMap *h, String const &key); +template gb_internal T * string_map_get (StringMap *h, StringHashKey const &key); -template gb_internal T & string_map_must_get (StringMap *h, char const *key); -template gb_internal T & string_map_must_get (StringMap *h, String const &key); -template gb_internal T & string_map_must_get (StringMap *h, StringHashKey const &key); +template gb_internal T & string_map_must_get(StringMap *h, char const *key); +template gb_internal T & string_map_must_get(StringMap *h, String const &key); +template gb_internal T & string_map_must_get(StringMap *h, StringHashKey const &key); -template gb_internal void string_map_set (StringMap *h, char const *key, T const &value); -template gb_internal void string_map_set (StringMap *h, String const &key, T const &value); -template gb_internal void string_map_set (StringMap *h, StringHashKey const &key, T const &value); +template gb_internal void string_map_set (StringMap *h, char const *key, T const &value); +template gb_internal void string_map_set (StringMap *h, String const &key, T const &value); +template gb_internal void string_map_set (StringMap *h, StringHashKey const &key, T const &value); -// template gb_internal void string_map_remove (StringMap *h, StringHashKey const &key); -template gb_internal void string_map_clear (StringMap *h); -template gb_internal void string_map_grow (StringMap *h); -template gb_internal void string_map_reserve (StringMap *h, isize new_count); +// template gb_internal void string_map_remove (StringMap *h, StringHashKey const &key); +template gb_internal void string_map_clear (StringMap *h); +template gb_internal void string_map_grow (StringMap *h); +template gb_internal void string_map_reserve (StringMap *h, usize new_count); gb_internal gbAllocator string_map_allocator(void) { return heap_allocator(); } template -gb_internal gb_inline void string_map_init(StringMap *h, isize capacity) { +gb_internal gb_inline void string_map_init(StringMap *h, usize capacity) { capacity = next_pow2_isize(capacity); - slice_init(&h->hashes, string_map_allocator(), capacity); - array_init(&h->entries, string_map_allocator(), 0, capacity); - for (isize i = 0; i < capacity; i++) { - h->hashes.data[i] = MAP_SENTINEL; - } + string_map_reserve(h, capacity); } template gb_internal gb_inline void string_map_destroy(StringMap *h) { - if (h->entries.allocator.proc == nullptr) { - h->entries.allocator = string_map_allocator(); - } - slice_free(&h->hashes, h->entries.allocator); - array_free(&h->entries); + gb_free(string_map_allocator(), h->hashes); + gb_free(string_map_allocator(), h->entries); } + +template +gb_internal void string_map__resize_hashes(StringMap *h, usize count) { + h->hashes_count = cast(u32)resize_array_raw(&h->hashes, string_map_allocator(), h->hashes_count, count); +} + + +template +gb_internal void string_map__reserve_entries(StringMap *h, usize capacity) { + h->entries_capacity = cast(u32)resize_array_raw(&h->entries, string_map_allocator(), h->entries_capacity, capacity); +} + + template -gb_internal MapIndex string_map__add_entry(StringMap *h, StringHashKey const &key) { +gb_internal MapIndex string_map__add_entry(StringMap *h, u32 hash, String const &key) { StringMapEntry e = {}; e.key = key; + e.hash = hash; e.next = MAP_SENTINEL; - array_add(&h->entries, e); - return cast(MapIndex)(h->entries.count-1); + string_map__reserve_entries(h, h->count+1); + h->entries[h->count++] = e; + return cast(MapIndex)(h->count-1); } template -gb_internal MapFindResult string_map__find(StringMap *h, StringHashKey const &key) { +gb_internal MapFindResult string_map__find(StringMap *h, u32 hash, String const &key) { MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL}; - if (h->hashes.count != 0) { - fr.hash_index = cast(MapIndex)(key.hash & (h->hashes.count-1)); - fr.entry_index = h->hashes.data[fr.hash_index]; + if (h->hashes_count != 0) { + fr.hash_index = cast(MapIndex)(hash & (h->hashes_count-1)); + fr.entry_index = h->hashes[fr.hash_index]; while (fr.entry_index != MAP_SENTINEL) { - if (string_hash_key_equal(h->entries.data[fr.entry_index].key, key)) { + auto *entry = &h->entries[fr.entry_index]; + if (entry->hash == hash && entry->key == key) { return fr; } fr.entry_prev = fr.entry_index; - fr.entry_index = h->entries.data[fr.entry_index].next; + fr.entry_index = entry->next; } } return fr; @@ -112,15 +121,16 @@ gb_internal MapFindResult string_map__find(StringMap *h, StringHashKey const template gb_internal MapFindResult string_map__find_from_entry(StringMap *h, StringMapEntry *e) { MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL}; - if (h->hashes.count != 0) { - fr.hash_index = cast(MapIndex)(e->key.hash & (h->hashes.count-1)); - fr.entry_index = h->hashes.data[fr.hash_index]; + if (h->hashes_count != 0) { + fr.hash_index = cast(MapIndex)(e->hash & (h->hashes_count-1)); + fr.entry_index = h->hashes[fr.hash_index]; while (fr.entry_index != MAP_SENTINEL) { - if (&h->entries.data[fr.entry_index] == e) { + auto *entry = &h->entries[fr.entry_index]; + if (entry == e) { return fr; } fr.entry_prev = fr.entry_index; - fr.entry_index = h->entries.data[fr.entry_index].next; + fr.entry_index = entry->next; } } return fr; @@ -128,24 +138,24 @@ gb_internal MapFindResult string_map__find_from_entry(StringMap *h, StringMap template gb_internal b32 string_map__full(StringMap *h) { - return 0.75f * h->hashes.count <= h->entries.count; + return 0.75f * h->hashes_count <= h->count; } template gb_inline void string_map_grow(StringMap *h) { - isize new_count = gb_max(h->hashes.count<<1, 16); + isize new_count = gb_max(h->hashes_count<<1, 16); string_map_reserve(h, new_count); } template gb_internal void string_map_reset_entries(StringMap *h) { - for (isize i = 0; i < h->hashes.count; i++) { - h->hashes.data[i] = MAP_SENTINEL; + for (u32 i = 0; i < h->hashes_count; i++) { + h->hashes[i] = MAP_SENTINEL; } - for (isize i = 0; i < h->entries.count; i++) { + for (isize i = 0; i < h->count; i++) { MapFindResult fr; - StringMapEntry *e = &h->entries.data[i]; + StringMapEntry *e = &h->entries[i]; e->next = MAP_SENTINEL; fr = string_map__find_from_entry(h, e); if (fr.entry_prev == MAP_SENTINEL) { @@ -157,27 +167,24 @@ gb_internal void string_map_reset_entries(StringMap *h) { } template -gb_internal void string_map_reserve(StringMap *h, isize cap) { - if (h->entries.allocator.proc == nullptr) { - h->entries.allocator = string_map_allocator(); - } - array_reserve(&h->entries, cap); - if (h->entries.count*2 < h->hashes.count) { +gb_internal void string_map_reserve(StringMap *h, usize cap) { + if (h->count*2 < h->hashes_count) { return; } - slice_resize(&h->hashes, h->entries.allocator, cap*2); + string_map__reserve_entries(h, cap); + string_map__resize_hashes(h, cap*2); string_map_reset_entries(h); } template -gb_internal T *string_map_get(StringMap *h, StringHashKey const &key) { +gb_internal T *string_map_get(StringMap *h, u32 hash, String const &key) { MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL}; - if (h->hashes.count != 0) { - fr.hash_index = cast(MapIndex)(key.hash & (h->hashes.count-1)); - fr.entry_index = h->hashes.data[fr.hash_index]; + if (h->hashes_count != 0) { + fr.hash_index = cast(MapIndex)(hash & (h->hashes_count-1)); + fr.entry_index = h->hashes[fr.hash_index]; while (fr.entry_index != MAP_SENTINEL) { - auto *entry = &h->entries.data[fr.entry_index]; - if (string_hash_key_equal(entry->key, key)) { + auto *entry = &h->entries[fr.entry_index]; + if (entry->hash == hash && entry->key == key) { return &entry->value; } fr.entry_prev = fr.entry_index; @@ -187,52 +194,65 @@ gb_internal T *string_map_get(StringMap *h, StringHashKey const &key) { return nullptr; } + +template +gb_internal gb_inline T *string_map_get(StringMap *h, StringHashKey const &key) { + return string_map_get(h, key.hash, key.string); +} + template gb_internal gb_inline T *string_map_get(StringMap *h, String const &key) { - return string_map_get(h, string_hash_string(key)); + return string_map_get(h, string_hash(key), key); } template gb_internal gb_inline T *string_map_get(StringMap *h, char const *key) { - return string_map_get(h, string_hash_string(make_string_c(key))); + String k = make_string_c(key); + return string_map_get(h, string_hash(k), k); } template -gb_internal T &string_map_must_get(StringMap *h, StringHashKey const &key) { - isize index = string_map__find(h, key).entry_index; +gb_internal T &string_map_must_get(StringMap *h, u32 hash, String const &key) { + isize index = string_map__find(h, hash, key).entry_index; GB_ASSERT(index != MAP_SENTINEL); - return h->entries.data[index].value; + return h->entries[index].value; +} + +template +gb_internal T &string_map_must_get(StringMap *h, StringHashKey const &key) { + return string_map_must_get(h, key.hash, key.string); } template gb_internal gb_inline T &string_map_must_get(StringMap *h, String const &key) { - return string_map_must_get(h, string_hash_string(key)); + return string_map_must_get(h, string_hash(key), key); } template gb_internal gb_inline T &string_map_must_get(StringMap *h, char const *key) { - return string_map_must_get(h, string_hash_string(make_string_c(key))); + String k = make_string_c(key); + return string_map_must_get(h, string_hash(k), k); } template -gb_internal void string_map_set(StringMap *h, StringHashKey const &key, T const &value) { +gb_internal void string_map_set(StringMap *h, u32 hash, String const &key, T const &value) { MapIndex index; MapFindResult fr; - if (h->hashes.count == 0) { + if (h->hashes_count == 0) { string_map_grow(h); } - fr = string_map__find(h, key); + fr = string_map__find(h, hash, key); if (fr.entry_index != MAP_SENTINEL) { index = fr.entry_index; } else { - index = string_map__add_entry(h, key); + index = string_map__add_entry(h, hash, key); if (fr.entry_prev != MAP_SENTINEL) { - h->entries.data[fr.entry_prev].next = index; + h->entries[fr.entry_prev].next = index; } else { - h->hashes.data[fr.hash_index] = index; + h->hashes[fr.hash_index] = index; } } - h->entries.data[index].value = value; + h->entries[index].value = value; if (string_map__full(h)) { string_map_grow(h); @@ -249,25 +269,31 @@ gb_internal gb_inline void string_map_set(StringMap *h, char const *key, T co string_map_set(h, string_hash_string(make_string_c(key)), value); } +template +gb_internal gb_inline void string_map_set(StringMap *h, StringHashKey const &key, T const &value) { + string_map_set(h, key.hash, key.string, value); +} + + // template // gb_internal void string_map__erase(StringMap *h, MapFindResult const &fr) { // MapFindResult last; // if (fr.entry_prev == MAP_SENTINEL) { -// h->hashes.data[fr.hash_index] = h->entries.data[fr.entry_index].next; +// h->hashes[fr.hash_index] = h->entries[fr.entry_index].next; // } else { -// h->entries.data[fr.entry_prev].next = h->entries.data[fr.entry_index].next; +// h->entries[fr.entry_prev].next = h->entries[fr.entry_index].next; // } -// if (fr.entry_index == h->entries.count-1) { +// if (fr.entry_index == h->count-1) { // array_pop(&h->entries); // return; // } -// h->entries.data[fr.entry_index] = h->entries.data[h->entries.count-1]; -// last = string_map__find(h, h->entries.data[fr.entry_index].key); +// h->entries[fr.entry_index] = h->entries[h->count-1]; +// last = string_map__find(h, h->entries[fr.entry_index].key); // if (last.entry_prev != MAP_SENTINEL) { -// h->entries.data[last.entry_prev].next = fr.entry_index; +// h->entries[last.entry_prev].next = fr.entry_index; // } else { -// h->hashes.data[last.hash_index] = fr.entry_index; +// h->hashes[last.hash_index] = fr.entry_index; // } // } @@ -281,9 +307,9 @@ gb_internal gb_inline void string_map_set(StringMap *h, char const *key, T co template gb_internal gb_inline void string_map_clear(StringMap *h) { - array_clear(&h->entries); - for (isize i = 0; i < h->hashes.count; i++) { - h->hashes.data[i] = MAP_SENTINEL; + h->count = 0; + for (u32 i = 0; i < h->hashes_count; i++) { + h->hashes[i] = MAP_SENTINEL; } } @@ -291,20 +317,20 @@ gb_internal gb_inline void string_map_clear(StringMap *h) { template gb_internal StringMapEntry *begin(StringMap &m) noexcept { - return m.entries.data; + return m.entries; } template gb_internal StringMapEntry const *begin(StringMap const &m) noexcept { - return m.entries.data; + return m.entries; } template gb_internal StringMapEntry *end(StringMap &m) { - return m.entries.data + m.entries.count; + return m.entries + m.count; } template gb_internal StringMapEntry const *end(StringMap const &m) noexcept { - return m.entries.data + m.entries.count; + return m.entries + m.count; } \ No newline at end of file -- cgit v1.2.3 From e488cf460193e213d418efead104d4e0c1afd9c2 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 16 Jan 2023 15:09:29 +0000 Subject: Enforce atomic on hasher id --- src/llvm_backend.cpp | 4 ++-- src/llvm_backend_general.cpp | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index b3364b89e..bc8056ec9 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -304,10 +304,10 @@ gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) { return {(*found)->value, (*found)->type}; } - static u32 proc_index = 0; + static std::atomic proc_index = 0; char buf[32] = {}; - isize n = gb_snprintf(buf, 32, "__$hasher%u", ++proc_index); + isize n = gb_snprintf(buf, 32, "__$hasher%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); diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index e7c9a0985..1dd36ba14 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -2619,6 +2619,7 @@ gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) lbProcedure *missing_proc = lb_create_procedure(m, e, ignore_body); if (ignore_body) { + GB_ASSERT(other_module != nullptr); mutex_lock(&other_module->values_mutex); auto *found = map_get(&other_module->values, e); if (found == nullptr) { -- cgit v1.2.3 From b289a27c4eb26f0e2984c0cbb1a9a7e07cc40325 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 16 Jan 2023 17:04:37 +0000 Subject: Move the mutex up a little --- src/llvm_backend_general.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 1dd36ba14..7846c302a 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -2622,11 +2622,11 @@ gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) GB_ASSERT(other_module != nullptr); mutex_lock(&other_module->values_mutex); auto *found = map_get(&other_module->values, e); + mutex_unlock(&other_module->values_mutex); if (found == nullptr) { 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); } - mutex_unlock(&other_module->values_mutex); } else { array_add(&m->missing_procedures_to_check, missing_proc); } -- cgit v1.2.3 From 65c0255e7ef82fe45fbc5e55a2c642e96b81343a Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 16 Jan 2023 18:05:58 +0000 Subject: Replace `RecursiveMutex` with a `BlockingMutex` --- src/llvm_backend.hpp | 2 +- src/llvm_backend_debug.cpp | 5 ++--- src/llvm_backend_general.cpp | 20 +++++++++++--------- 3 files changed, 14 insertions(+), 13 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index beb9a99b6..8237b880f 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -143,7 +143,7 @@ struct lbModule { PtrMap struct_field_remapping; // Key: LLVMTypeRef or Type * i32 internal_type_level; - RecursiveMutex values_mutex; + BlockingMutex values_mutex; PtrMap values; PtrMap soa_values; diff --git a/src/llvm_backend_debug.cpp b/src/llvm_backend_debug.cpp index 83f6da04e..fc9e63274 100644 --- a/src/llvm_backend_debug.cpp +++ b/src/llvm_backend_debug.cpp @@ -495,15 +495,14 @@ gb_internal LLVMMetadataRef lb_get_base_scope_metadata(lbModule *m, Scope *scope } gb_internal LLVMMetadataRef lb_debug_type(lbModule *m, Type *type) { - mutex_lock(&m->debug_values_mutex); - defer (mutex_unlock(&m->debug_values_mutex)); - GB_ASSERT(type != nullptr); LLVMMetadataRef found = lb_get_llvm_metadata(m, type); if (found != nullptr) { return found; } + MUTEX_GUARD(&m->debug_values_mutex); + if (type->kind == Type_Named) { LLVMMetadataRef file = nullptr; unsigned line = 0; diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 7846c302a..5bee5d32c 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -2547,11 +2547,14 @@ gb_internal lbValue lb_find_ident(lbProcedure *p, lbModule *m, Entity *e, Ast *e return *found; } } + + lbValue *found = nullptr; mutex_lock(&m->values_mutex); - defer (mutex_unlock(&m->values_mutex)); + found = map_get(&m->values, e); + mutex_unlock(&m->values_mutex); - auto *found = map_get(&m->values, e); if (found) { + auto v = *found; // NOTE(bill): This is because pointers are already pointers in LLVM if (is_type_proc(v.type)) { @@ -2598,10 +2601,10 @@ gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) e = strip_entity_wrapping(e); GB_ASSERT(e != nullptr); + lbValue *found = nullptr; mutex_lock(&m->values_mutex); - defer (mutex_unlock(&m->values_mutex)); - - auto *found = map_get(&m->values, e); + found = map_get(&m->values, e); + mutex_unlock(&m->values_mutex); if (found) { return *found; } @@ -2698,11 +2701,10 @@ gb_internal lbValue lb_find_value_from_entity(lbModule *m, Entity *e) { return lb_find_procedure_value_from_entity(m, e); } + lbValue *found = nullptr; mutex_lock(&m->values_mutex); - defer (mutex_unlock(&m->values_mutex)); - - - auto *found = map_get(&m->values, e); + found = map_get(&m->values, e); + mutex_unlock(&m->values_mutex); if (found) { return *found; } -- cgit v1.2.3 From 0d059aa797cf5a52648f2ff9dab38a16bda7a7a0 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 16 Jan 2023 18:08:28 +0000 Subject: Replace `BlockingMutex` with `RwMutex` --- src/llvm_backend.hpp | 2 +- src/llvm_backend_general.cpp | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 8237b880f..85f1078f2 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -143,7 +143,7 @@ struct lbModule { PtrMap struct_field_remapping; // Key: LLVMTypeRef or Type * i32 internal_type_level; - BlockingMutex values_mutex; + RwMutex values_mutex; PtrMap values; PtrMap soa_values; diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 5bee5d32c..2c171239a 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -2186,25 +2186,25 @@ gb_internal void lb_ensure_abi_function_type(lbModule *m, lbProcedure *p) { gb_internal void lb_add_entity(lbModule *m, Entity *e, lbValue val) { if (e != nullptr) { - mutex_lock(&m->values_mutex); + rw_mutex_lock(&m->values_mutex); map_set(&m->values, e, val); - mutex_unlock(&m->values_mutex); + rw_mutex_unlock(&m->values_mutex); } } gb_internal void lb_add_member(lbModule *m, String const &name, lbValue val) { if (name.len > 0) { - mutex_lock(&m->values_mutex); + rw_mutex_lock(&m->values_mutex); string_map_set(&m->members, name, val); - mutex_unlock(&m->values_mutex); + rw_mutex_unlock(&m->values_mutex); } } gb_internal void lb_add_procedure_value(lbModule *m, lbProcedure *p) { - mutex_lock(&m->values_mutex); + rw_mutex_lock(&m->values_mutex); if (p->entity != nullptr) { map_set(&m->procedure_values, p->value, p->entity); } string_map_set(&m->procedures, p->name, p); - mutex_unlock(&m->values_mutex); + rw_mutex_unlock(&m->values_mutex); } @@ -2549,9 +2549,9 @@ gb_internal lbValue lb_find_ident(lbProcedure *p, lbModule *m, Entity *e, Ast *e } lbValue *found = nullptr; - mutex_lock(&m->values_mutex); + rw_mutex_shared_lock(&m->values_mutex); found = map_get(&m->values, e); - mutex_unlock(&m->values_mutex); + rw_mutex_shared_unlock(&m->values_mutex); if (found) { @@ -2602,9 +2602,9 @@ gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) GB_ASSERT(e != nullptr); lbValue *found = nullptr; - mutex_lock(&m->values_mutex); + rw_mutex_shared_lock(&m->values_mutex); found = map_get(&m->values, e); - mutex_unlock(&m->values_mutex); + rw_mutex_shared_unlock(&m->values_mutex); if (found) { return *found; } @@ -2623,9 +2623,9 @@ gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e) lbProcedure *missing_proc = lb_create_procedure(m, e, ignore_body); if (ignore_body) { GB_ASSERT(other_module != nullptr); - mutex_lock(&other_module->values_mutex); + rw_mutex_shared_lock(&other_module->values_mutex); auto *found = map_get(&other_module->values, e); - mutex_unlock(&other_module->values_mutex); + rw_mutex_shared_unlock(&other_module->values_mutex); if (found == nullptr) { 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); @@ -2702,9 +2702,9 @@ gb_internal lbValue lb_find_value_from_entity(lbModule *m, Entity *e) { } lbValue *found = nullptr; - mutex_lock(&m->values_mutex); + rw_mutex_shared_lock(&m->values_mutex); found = map_get(&m->values, e); - mutex_unlock(&m->values_mutex); + rw_mutex_shared_unlock(&m->values_mutex); if (found) { return *found; } -- cgit v1.2.3 From ccf4b48865f8d3c6f78f72cda5cfd5e46b108c02 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 27 Jan 2023 11:47:00 +0000 Subject: Add extra checks for multiple assignments when emitting stores --- src/llvm_backend.hpp | 1 + src/llvm_backend_general.cpp | 12 +++++++----- src/llvm_backend_stmt.cpp | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 85f1078f2..e151d2be1 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -324,6 +324,7 @@ struct lbProcedure { lbBlock * curr_block; lbTargetList * target_list; PtrMap direct_parameters; + bool in_multi_assignment; Ast *curr_stmt; diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 2c171239a..73cf0d1b9 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -937,7 +937,7 @@ gb_internal void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value) { enum {MAX_STORE_SIZE = 64}; - if (lb_sizeof(LLVMTypeOf(value.value)) > MAX_STORE_SIZE) { + if (!p->in_multi_assignment && lb_sizeof(LLVMTypeOf(value.value)) > MAX_STORE_SIZE) { if (LLVMIsALoadInst(value.value)) { LLVMValueRef dst_ptr = ptr.value; LLVMValueRef src_ptr_original = LLVMGetOperand(value.value, 0); @@ -964,15 +964,16 @@ gb_internal void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value) { } } + LLVMValueRef instr = nullptr; if (lb_is_type_proc_recursive(a)) { // NOTE(bill, 2020-11-11): Because of certain LLVM rules, a procedure value may be // stored as regular pointer with no procedure information LLVMTypeRef rawptr_type = lb_type(p->module, t_rawptr); LLVMTypeRef rawptr_ptr_type = LLVMPointerType(rawptr_type, 0); - LLVMBuildStore(p->builder, - LLVMBuildPointerCast(p->builder, value.value, rawptr_type, ""), - LLVMBuildPointerCast(p->builder, ptr.value, rawptr_ptr_type, "")); + instr = LLVMBuildStore(p->builder, + LLVMBuildPointerCast(p->builder, value.value, rawptr_type, ""), + LLVMBuildPointerCast(p->builder, ptr.value, rawptr_ptr_type, "")); } else { Type *ca = core_type(a); if (ca->kind == Type_Basic || ca->kind == Type_Proc) { @@ -981,8 +982,9 @@ gb_internal void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value) { GB_ASSERT_MSG(are_types_identical(a, value.type), "%s != %s", type_to_string(a), type_to_string(value.type)); } - LLVMBuildStore(p->builder, value.value, ptr.value); + instr = LLVMBuildStore(p->builder, value.value, ptr.value); } + LLVMSetVolatile(instr, p->in_multi_assignment); } gb_internal LLVMTypeRef llvm_addr_type(lbModule *module, lbValue addr_val) { diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index c268ab09a..2284649e2 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -1555,12 +1555,25 @@ gb_internal void lb_build_assignment(lbProcedure *p, Array &lvals, Slice lb_append_tuple_values(p, &inits, init); } + bool prev_in_assignment = p->in_multi_assignment; + + isize lval_count = 0; + for (lbAddr const &lval : lvals) { + if (lval.addr.value != nullptr) { + // check if it is not a blank identifier + lval_count += 1; + } + } + p->in_multi_assignment = lval_count > 1; + GB_ASSERT(lvals.count == inits.count); for_array(i, inits) { lbAddr lval = lvals[i]; lbValue init = inits[i]; lb_addr_store(p, lval, init); } + + p->in_multi_assignment = prev_in_assignment; } gb_internal void lb_build_return_stmt_internal(lbProcedure *p, lbValue res) { @@ -2047,6 +2060,7 @@ gb_internal void lb_build_assign_stmt(lbProcedure *p, AstAssignStmt *as) { lb_build_assignment(p, lvals, as->rhs); return; } + GB_ASSERT(as->lhs.count == 1); GB_ASSERT(as->rhs.count == 1); // NOTE(bill): Only 1 += 1 is allowed, no tuples -- cgit v1.2.3 From 0f697a0f2678aa5522c6671662da550ce9a8bed5 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 14 Feb 2023 23:52:23 +0000 Subject: Move in_multi_assignment check tighter --- src/llvm_backend_general.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 73cf0d1b9..9b6b527e6 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -937,8 +937,8 @@ gb_internal void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value) { enum {MAX_STORE_SIZE = 64}; - if (!p->in_multi_assignment && lb_sizeof(LLVMTypeOf(value.value)) > MAX_STORE_SIZE) { - if (LLVMIsALoadInst(value.value)) { + if (lb_sizeof(LLVMTypeOf(value.value)) > MAX_STORE_SIZE) { + if (!p->in_multi_assignment && LLVMIsALoadInst(value.value)) { LLVMValueRef dst_ptr = ptr.value; LLVMValueRef src_ptr_original = LLVMGetOperand(value.value, 0); LLVMValueRef src_ptr = LLVMBuildPointerCast(p->builder, src_ptr_original, LLVMTypeOf(dst_ptr), ""); -- cgit v1.2.3 From 48685e8bf15dada1734ad6463fafb7d00c86b2b0 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 14 Feb 2023 23:52:36 +0000 Subject: Remove set volatile for store --- src/llvm_backend_general.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 9b6b527e6..89ed593a2 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -984,7 +984,7 @@ gb_internal void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value) { instr = LLVMBuildStore(p->builder, value.value, ptr.value); } - LLVMSetVolatile(instr, p->in_multi_assignment); + // LLVMSetVolatile(instr, p->in_multi_assignment); } gb_internal LLVMTypeRef llvm_addr_type(lbModule *module, lbValue addr_val) { -- cgit v1.2.3 From c6c4ad618831b34bf7226edfe976032f9417efc0 Mon Sep 17 00:00:00 2001 From: Phil Homan Date: Wed, 15 Feb 2023 16:48:27 -0800 Subject: fix #by_ptr argument overrides for Linux --- src/llvm_backend_general.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 89ed593a2..246613724 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -1586,6 +1586,10 @@ gb_internal LLVMTypeRef lb_type_internal_for_procedures_raw(lbModule *m, Type *t if (params_by_ptr[i]) { // NOTE(bill): The parameter needs to be passed "indirectly", override it ft->args[i].kind = lbArg_Indirect; + ft->args[i].attribute = nullptr; + ft->args[i].align_attribute = nullptr; + ft->args[i].byval_alignment = 0; + ft->args[i].is_byval = false; } } -- cgit v1.2.3 From 99460c9e3293738607764d2c3d0cecddd7576e01 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 17 Feb 2023 14:26:22 +0000 Subject: Minimize stack wastage with compound literals defining variables --- src/llvm_backend.hpp | 2 ++ src/llvm_backend_const.cpp | 4 ++++ src/llvm_backend_general.cpp | 1 + src/llvm_backend_stmt.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 45 insertions(+), 2 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 0b3bc2a5a..4a78ca3a9 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -182,6 +182,8 @@ struct lbModule { PtrMap map_cell_info_map; // address of runtime.Map_Info PtrMap map_info_map; // address of runtime.Map_Cell_Info + PtrMap exact_value_compound_literal_addr_map; // Key: Ast_CompoundLit + LLVMPassManagerRef function_pass_managers[lbFunctionPassManager_COUNT]; }; diff --git a/src/llvm_backend_const.cpp b/src/llvm_backend_const.cpp index fc1598024..72c2a0495 100644 --- a/src/llvm_backend_const.cpp +++ b/src/llvm_backend_const.cpp @@ -460,6 +460,8 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bo LLVMValueRef ptr = LLVMBuildInBoundsGEP2(p->builder, llvm_type, array_data, indices, 2, ""); LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true); lbAddr slice = lb_add_local_generated(p, type, false); + map_set(&m->exact_value_compound_literal_addr_map, value.value_compound, slice); + lb_fill_slice(p, slice, {ptr, alloc_type_pointer(elem)}, {len, t_int}); return lb_addr_load(p, slice); } @@ -1042,6 +1044,8 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bo GB_ASSERT(is_local); lbProcedure *p = m->curr_procedure; lbAddr v = lb_add_local_generated(p, res.type, true); + map_set(&m->exact_value_compound_literal_addr_map, value.value_compound, v); + LLVMBuildStore(p->builder, constant_value, v.addr.value); for (isize i = 0; i < value_count; i++) { LLVMValueRef val = old_values[i]; diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 246613724..e398873ee 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -82,6 +82,7 @@ gb_internal void lb_init_module(lbModule *m, Checker *c) { map_init(&m->map_info_map, 0); map_init(&m->map_cell_info_map, 0); + map_init(&m->exact_value_compound_literal_addr_map, 1024); } diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 2284649e2..fad74e9eb 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -1523,7 +1523,8 @@ gb_internal void lb_build_static_variables(lbProcedure *p, AstValueDecl *vd) { lb_add_member(p->module, mangled_name, global_val); } } -gb_internal void lb_append_tuple_values(lbProcedure *p, Array *dst_values, lbValue src_value) { +gb_internal isize lb_append_tuple_values(lbProcedure *p, Array *dst_values, lbValue src_value) { + isize init_count = dst_values->count; Type *t = src_value.type; if (t->kind == Type_Tuple) { lbTupleFix *tf = map_get(&p->tuple_fix_map, src_value.value); @@ -1540,6 +1541,7 @@ gb_internal void lb_append_tuple_values(lbProcedure *p, Array *dst_valu } else { array_add(dst_values, src_value); } + return dst_values->count - init_count; } @@ -2218,7 +2220,41 @@ gb_internal void lb_build_stmt(lbProcedure *p, Ast *node) { } array_add(&lvals, lval); } - lb_build_assignment(p, lvals, vd->values); + + auto const &values = vd->values; + if (values.count > 0) { + auto inits = array_make(permanent_allocator(), 0, lvals.count); + + isize lval_index = 0; + for (Ast *rhs : values) { + rhs = unparen_expr(rhs); + lbValue init = lb_build_expr(p, rhs); + #if 1 + // NOTE(bill, 2023-02-17): lb_const_value might produce a stack local variable for the + // compound literal, so reusing that variable should minimize the stack wastage + if (rhs->kind == Ast_CompoundLit) { + lbAddr *comp_lit_addr = map_get(&p->module->exact_value_compound_literal_addr_map, rhs); + if (comp_lit_addr) { + Entity *e = entity_of_node(vd->names[lval_index]); + if (e) { + lb_add_entity(p->module, e, comp_lit_addr->addr); + lvals[lval_index] = {}; // do nothing so that nothing will assign to it + } + } + } + #endif + + lval_index += lb_append_tuple_values(p, &inits, init); + } + GB_ASSERT(lval_index == lvals.count); + + GB_ASSERT(lvals.count == inits.count); + for_array(i, inits) { + lbAddr lval = lvals[i]; + lbValue init = inits[i]; + lb_addr_store(p, lval, init); + } + } case_end; case_ast_node(as, AssignStmt, node); -- cgit v1.2.3 From 0602a16ad66a02e315a71de721885e351b723a2b Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 16 Mar 2023 12:44:03 +0000 Subject: Reserve memory for procedures when generating the LLVM IR --- src/llvm_backend_general.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index e398873ee..52ea4f399 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -63,7 +63,6 @@ gb_internal void lb_init_module(lbModule *m, Checker *c) { map_init(&m->values); map_init(&m->soa_values); string_map_init(&m->members); - map_init(&m->procedure_values); string_map_init(&m->procedures); string_map_init(&m->const_strings); map_init(&m->function_type_map); @@ -71,7 +70,13 @@ gb_internal void lb_init_module(lbModule *m, Checker *c) { map_init(&m->hasher_procs); map_init(&m->map_get_procs); map_init(&m->map_set_procs); - array_init(&m->procedures_to_generate, a, 0, 1024); + if (build_context.use_separate_modules) { + array_init(&m->procedures_to_generate, a, 0, 1<<10); + map_init(&m->procedure_values, 1<<11); + } else { + array_init(&m->procedures_to_generate, a, 0, c->info.all_procedures.count); + map_init(&m->procedure_values, c->info.all_procedures.count*2); + } array_init(&m->global_procedures_and_types_to_create, a, 0, 1024); array_init(&m->missing_procedures_to_check, a, 0, 16); map_init(&m->debug_values); -- cgit v1.2.3 From 8dc70f797cf8b4315d689ce94ab004faadb015b7 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 16 Mar 2023 15:16:17 +0000 Subject: Increase use of `temporary_allocator()` where possible --- src/llvm_backend.cpp | 63 +++++++++++++++++++++++++++++--------------- src/llvm_backend_expr.cpp | 31 ++++++++++++++++------ src/llvm_backend_general.cpp | 9 +++++-- src/llvm_backend_proc.cpp | 14 +++++----- 4 files changed, 79 insertions(+), 38 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 543f5d833..d8ff01554 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -96,10 +96,12 @@ gb_internal void lb_set_entity_from_other_modules_linkage_correctly(lbModule *ot } gb_internal void lb_emit_init_context(lbProcedure *p, lbAddr addr) { + TEMPORARY_ALLOCATOR_GUARD(); + GB_ASSERT(addr.kind == lbAddr_Context); GB_ASSERT(addr.ctx.sel.index.count == 0); - auto args = array_make(permanent_allocator(), 1); + auto args = array_make(temporary_allocator(), 1); args[0] = addr.addr; lb_emit_runtime_call(p, "__init_context", args); } @@ -279,9 +281,11 @@ gb_internal lbValue lb_equal_proc_for_type(lbModule *m, Type *type) { } gb_internal lbValue lb_simple_compare_hash(lbProcedure *p, Type *type, lbValue data, lbValue seed) { + TEMPORARY_ALLOCATOR_GUARD(); + GB_ASSERT_MSG(is_type_simple_compare(type), "%s", type_to_string(type)); - auto args = array_make(permanent_allocator(), 3); + auto args = array_make(temporary_allocator(), 3); args[0] = data; args[1] = seed; args[2] = lb_const_int(p->module, t_int, type_size_of(type)); @@ -334,11 +338,13 @@ gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) { return {p->value, p->type}; } + TEMPORARY_ALLOCATOR_GUARD(); + if (type->kind == Type_Struct) { type_set_offsets(type); data = lb_emit_conv(p, data, t_u8_ptr); - auto args = array_make(permanent_allocator(), 2); + auto args = array_make(temporary_allocator(), 2); for_array(i, type->Struct.fields) { GB_ASSERT(type->Struct.offsets != nullptr); i64 offset = type->Struct.offsets[i]; @@ -352,7 +358,7 @@ gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) { } LLVMBuildRet(p->builder, seed.value); } else if (type->kind == Type_Union) { - auto args = array_make(permanent_allocator(), 2); + auto args = array_make(temporary_allocator(), 2); if (is_type_union_maybe_pointer(type)) { Type *v = type->Union.variants[0]; @@ -396,7 +402,7 @@ gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) { lbAddr pres = lb_add_local_generated(p, t_uintptr, false); lb_addr_store(p, pres, seed); - auto args = array_make(permanent_allocator(), 2); + auto args = array_make(temporary_allocator(), 2); lbValue elem_hasher = lb_hasher_proc_for_type(m, type->Array.elem); auto loop_data = lb_loop_start(p, cast(isize)type->Array.count, t_i32); @@ -417,7 +423,7 @@ gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) { lbAddr res = lb_add_local_generated(p, t_uintptr, false); lb_addr_store(p, res, seed); - auto args = array_make(permanent_allocator(), 2); + auto args = array_make(temporary_allocator(), 2); lbValue elem_hasher = lb_hasher_proc_for_type(m, type->EnumeratedArray.elem); auto loop_data = lb_loop_start(p, cast(isize)type->EnumeratedArray.count, t_i32); @@ -435,14 +441,14 @@ gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) { lbValue vres = lb_addr_load(p, res); LLVMBuildRet(p->builder, vres.value); } else if (is_type_cstring(type)) { - auto args = array_make(permanent_allocator(), 2); + auto args = array_make(temporary_allocator(), 2); args[0] = data; args[1] = seed; lbValue res = lb_emit_runtime_call(p, "default_hasher_cstring", args); lb_add_callsite_force_inline(p, res); LLVMBuildRet(p->builder, res.value); } else if (is_type_string(type)) { - auto args = array_make(permanent_allocator(), 2); + auto args = array_make(temporary_allocator(), 2); args[0] = data; args[1] = seed; lbValue res = lb_emit_runtime_call(p, "default_hasher_string", args); @@ -527,7 +533,8 @@ gb_internal lbValue lb_map_get_proc_for_type(lbModule *m, Type *type) { lbValue mask = lb_emit_conv(p, lb_emit_arith(p, Token_Sub, capacity, lb_const_int(m, t_int, 1), t_int), t_uintptr); { - auto args = array_make(heap_allocator(), 2); + TEMPORARY_ALLOCATOR_GUARD(); + auto args = array_make(temporary_allocator(), 2); args[0] = map; args[1] = h; lb_addr_store(p, pos, lb_emit_runtime_call(p, "map_desired_position", args)); @@ -554,7 +561,8 @@ gb_internal lbValue lb_map_get_proc_for_type(lbModule *m, Type *type) { lb_start_block(p, probe_block); { - auto args = array_make(heap_allocator(), 3); + TEMPORARY_ALLOCATOR_GUARD(); + auto args = array_make(temporary_allocator(), 3); args[0] = map; args[1] = element_hash; args[2] = lb_addr_load(p, pos); @@ -612,6 +620,8 @@ gb_internal lbValue lb_map_get_proc_for_type(lbModule *m, Type *type) { // } gb_internal lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) { + TEMPORARY_ALLOCATOR_GUARD(); + GB_ASSERT(!build_context.dynamic_map_calls); type = base_type(type); GB_ASSERT(type->kind == Type_Map); @@ -673,7 +683,7 @@ gb_internal lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) { { lbValue map_get_proc = lb_map_get_proc_for_type(m, type); - auto args = array_make(permanent_allocator(), 3); + auto args = array_make(temporary_allocator(), 3); args[0] = lb_emit_conv(p, map_ptr, t_rawptr); args[1] = hash; args[2] = key_ptr; @@ -699,7 +709,7 @@ gb_internal lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) { lbValue map_info = lb_gen_map_info_ptr(p->module, type); { - auto args = array_make(permanent_allocator(), 3); + auto args = array_make(temporary_allocator(), 3); args[0] = lb_emit_conv(p, map_ptr, t_rawptr); args[1] = map_info; args[2] = lb_emit_load(p, location_ptr); @@ -713,7 +723,7 @@ gb_internal lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) { lb_start_block(p, insert_block); { - auto args = array_make(permanent_allocator(), 5); + auto args = array_make(temporary_allocator(), 5); args[0] = lb_emit_conv(p, map_ptr, t_rawptr); args[1] = map_info; args[2] = hash; @@ -883,6 +893,8 @@ gb_internal lbValue lb_const_hash(lbModule *m, lbValue key, Type *key_type) { } gb_internal lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_type, lbValue *key_ptr_) { + TEMPORARY_ALLOCATOR_GUARD(); + lbValue key_ptr = lb_address_from_load_or_generate_local(p, key); key_ptr = lb_emit_conv(p, key_ptr, t_rawptr); @@ -892,7 +904,7 @@ gb_internal lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_t if (hashed_key.value == nullptr) { lbValue hasher = lb_hasher_proc_for_type(p->module, key_type); - auto args = array_make(permanent_allocator(), 2); + auto args = array_make(temporary_allocator(), 2); args[0] = key_ptr; args[1] = lb_const_int(p->module, t_uintptr, 0); hashed_key = lb_emit_call(p, hasher, args); @@ -902,6 +914,8 @@ gb_internal lbValue lb_gen_map_key_hash(lbProcedure *p, lbValue key, Type *key_t } gb_internal lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue const &map_ptr, lbValue const &key) { + TEMPORARY_ALLOCATOR_GUARD(); + Type *map_type = base_type(type_deref(map_ptr.type)); GB_ASSERT(map_type->kind == Type_Map); @@ -910,7 +924,7 @@ gb_internal lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue cons lbValue hash = lb_gen_map_key_hash(p, key, map_type->Map.key, &key_ptr); if (build_context.dynamic_map_calls) { - auto args = array_make(permanent_allocator(), 4); + auto args = array_make(temporary_allocator(), 4); args[0] = lb_emit_transmute(p, map_ptr, t_raw_map_ptr); args[1] = lb_gen_map_info_ptr(p->module, map_type); args[2] = hash; @@ -920,7 +934,7 @@ gb_internal lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue cons } else { lbValue map_get_proc = lb_map_get_proc_for_type(p->module, map_type); - auto args = array_make(permanent_allocator(), 3); + auto args = array_make(temporary_allocator(), 3); args[0] = lb_emit_conv(p, map_ptr, t_rawptr); args[1] = hash; args[2] = key_ptr; @@ -932,6 +946,8 @@ gb_internal lbValue lb_internal_dynamic_map_get_ptr(lbProcedure *p, lbValue cons gb_internal void lb_internal_dynamic_map_set(lbProcedure *p, lbValue const &map_ptr, Type *map_type, lbValue const &map_key, lbValue const &map_value, Ast *node) { + TEMPORARY_ALLOCATOR_GUARD(); + map_type = base_type(map_type); GB_ASSERT(map_type->kind == Type_Map); @@ -942,7 +958,7 @@ gb_internal void lb_internal_dynamic_map_set(lbProcedure *p, lbValue const &map_ lbValue value_ptr = lb_address_from_load_or_generate_local(p, v); if (build_context.dynamic_map_calls) { - auto args = array_make(permanent_allocator(), 6); + auto args = array_make(temporary_allocator(), 6); args[0] = lb_emit_conv(p, map_ptr, t_raw_map_ptr); args[1] = lb_gen_map_info_ptr(p->module, map_type); args[2] = hash; @@ -953,7 +969,7 @@ gb_internal void lb_internal_dynamic_map_set(lbProcedure *p, lbValue const &map_ } else { lbValue map_set_proc = lb_map_set_proc_for_type(p->module, map_type); - auto args = array_make(permanent_allocator(), 5); + auto args = array_make(temporary_allocator(), 5); args[0] = lb_emit_conv(p, map_ptr, t_rawptr); args[1] = hash; args[2] = lb_emit_conv(p, key_ptr, t_rawptr); @@ -967,12 +983,14 @@ gb_internal void lb_internal_dynamic_map_set(lbProcedure *p, lbValue const &map_ gb_internal lbValue lb_dynamic_map_reserve(lbProcedure *p, lbValue const &map_ptr, isize const capacity, TokenPos const &pos) { GB_ASSERT(!build_context.no_dynamic_literals); + TEMPORARY_ALLOCATOR_GUARD(); + String proc_name = {}; if (p->entity) { proc_name = p->entity->token.string; } - auto args = array_make(permanent_allocator(), 4); + auto args = array_make(temporary_allocator(), 4); args[0] = lb_emit_conv(p, map_ptr, t_rawptr); args[1] = lb_gen_map_info_ptr(p->module, type_deref(map_ptr.type)); args[2] = lb_const_int(p->module, t_uint, capacity); @@ -1032,7 +1050,9 @@ gb_internal void lb_finalize_objc_names(lbProcedure *p) { } lbModule *m = p->module; - auto args = array_make(permanent_allocator(), 1); + TEMPORARY_ALLOCATOR_GUARD(); + + auto args = array_make(temporary_allocator(), 1); LLVMSetLinkage(p->value, LLVMInternalLinkage); lb_begin_procedure_body(p); @@ -1811,7 +1831,8 @@ gb_internal lbProcedure *lb_create_main_procedure(lbModule *m, lbProcedure *star lbValue runner = lb_find_package_value(m, str_lit("testing"), str_lit("runner")); - auto args = array_make(permanent_allocator(), 1); + 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); } else { diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index 3676847b4..028e90f51 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -1072,7 +1072,9 @@ gb_internal lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbV Type *ft = base_complex_elem_type(type); if (op == Token_Quo) { - auto args = array_make(permanent_allocator(), 2); + TEMPORARY_ALLOCATOR_GUARD(); + + auto args = array_make(temporary_allocator(), 2); args[0] = lhs; args[1] = rhs; @@ -1146,7 +1148,9 @@ gb_internal lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbV return lb_addr_load(p, res); } else if (op == Token_Mul) { - auto args = array_make(permanent_allocator(), 2); + TEMPORARY_ALLOCATOR_GUARD(); + + auto args = array_make(temporary_allocator(), 2); args[0] = lhs; args[1] = rhs; @@ -1156,7 +1160,9 @@ gb_internal lbValue lb_emit_arith(lbProcedure *p, TokenKind op, lbValue lhs, lbV default: GB_PANIC("Unknown float type"); break; } } else if (op == Token_Quo) { - auto args = array_make(permanent_allocator(), 2); + TEMPORARY_ALLOCATOR_GUARD(); + + auto args = array_make(temporary_allocator(), 2); args[0] = lhs; args[1] = rhs; @@ -1647,8 +1653,10 @@ gb_internal lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) { } if (are_types_identical(src, t_cstring) && are_types_identical(dst, t_string)) { + TEMPORARY_ALLOCATOR_GUARD(); + lbValue c = lb_emit_conv(p, value, t_cstring); - auto args = array_make(permanent_allocator(), 1); + auto args = array_make(temporary_allocator(), 1); args[0] = c; lbValue s = lb_emit_runtime_call(p, "cstring_to_string", args); return lb_emit_conv(p, s, dst); @@ -1792,6 +1800,8 @@ gb_internal lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) { } if (is_type_integer_128bit(dst)) { + TEMPORARY_ALLOCATOR_GUARD(); + auto args = array_make(temporary_allocator(), 1); args[0] = value; char const *call = "fixunsdfdi"; @@ -1825,6 +1835,8 @@ gb_internal lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) { } if (is_type_integer_128bit(src)) { + TEMPORARY_ALLOCATOR_GUARD(); + auto args = array_make(temporary_allocator(), 1); args[0] = value; char const *call = "floattidf"; @@ -2219,16 +2231,17 @@ gb_internal lbValue lb_compare_records(lbProcedure *p, TokenKind op_kind, lbValu } GB_PANIC("invalid operator"); } + TEMPORARY_ALLOCATOR_GUARD(); if (is_type_simple_compare(type)) { // TODO(bill): Test to see if this is actually faster!!!! - auto args = array_make(permanent_allocator(), 3); + auto args = array_make(temporary_allocator(), 3); args[0] = lb_emit_conv(p, left_ptr, t_rawptr); args[1] = lb_emit_conv(p, right_ptr, t_rawptr); args[2] = lb_const_int(p->module, t_int, type_size_of(type)); res = lb_emit_runtime_call(p, "memory_equal", args); } else { lbValue value = lb_equal_proc_for_type(p->module, type); - auto args = array_make(permanent_allocator(), 2); + auto args = array_make(temporary_allocator(), 2); args[0] = lb_emit_conv(p, left_ptr, t_rawptr); args[1] = lb_emit_conv(p, right_ptr, t_rawptr); res = lb_emit_call(p, value, args); @@ -4052,6 +4065,8 @@ gb_internal lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) { lbAddr v = lb_add_local_generated(p, type, true); + TEMPORARY_ALLOCATOR_GUARD(); + Type *et = nullptr; switch (bt->kind) { case Type_Array: et = bt->Array.elem; break; @@ -4247,7 +4262,7 @@ gb_internal lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) { i64 item_count = gb_max(cl->max_count, cl->elems.count); { - auto args = array_make(permanent_allocator(), 5); + auto args = array_make(temporary_allocator(), 5); args[0] = lb_emit_conv(p, lb_addr_get_ptr(p, v), t_rawptr); args[1] = size; args[2] = align; @@ -4267,7 +4282,7 @@ gb_internal lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) { lb_build_addr_compound_lit_assign_array(p, temp_data); { - auto args = array_make(permanent_allocator(), 6); + auto args = array_make(temporary_allocator(), 6); args[0] = lb_emit_conv(p, v.addr, t_rawptr); args[1] = size; args[2] = align; diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 52ea4f399..aa887418f 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -496,6 +496,8 @@ gb_internal void lb_emit_bounds_check(lbProcedure *p, Token token, lbValue index return; } + TEMPORARY_ALLOCATOR_GUARD(); + index = lb_emit_conv(p, index, t_int); len = lb_emit_conv(p, len, t_int); @@ -503,7 +505,7 @@ gb_internal void lb_emit_bounds_check(lbProcedure *p, Token token, lbValue index lbValue line = lb_const_int(p->module, t_i32, token.pos.line); lbValue column = lb_const_int(p->module, t_i32, token.pos.column); - auto args = array_make(permanent_allocator(), 5); + auto args = array_make(temporary_allocator(), 5); args[0] = file; args[1] = line; args[2] = column; @@ -521,6 +523,8 @@ gb_internal void lb_emit_matrix_bounds_check(lbProcedure *p, Token token, lbValu return; } + TEMPORARY_ALLOCATOR_GUARD(); + row_index = lb_emit_conv(p, row_index, t_int); column_index = lb_emit_conv(p, column_index, t_int); row_count = lb_emit_conv(p, row_count, t_int); @@ -530,7 +534,7 @@ gb_internal void lb_emit_matrix_bounds_check(lbProcedure *p, Token token, lbValu lbValue line = lb_const_int(p->module, t_i32, token.pos.line); lbValue column = lb_const_int(p->module, t_i32, token.pos.column); - auto args = array_make(permanent_allocator(), 7); + auto args = array_make(temporary_allocator(), 7); args[0] = file; args[1] = line; args[2] = column; @@ -1336,6 +1340,7 @@ gb_internal void lb_emit_store_union_variant(lbProcedure *p, lbValue parent, lbV gb_internal void lb_clone_struct_type(LLVMTypeRef dst, LLVMTypeRef src) { + TEMPORARY_ALLOCATOR_GUARD(); unsigned field_count = LLVMCountStructElementTypes(src); LLVMTypeRef *fields = gb_alloc_array(temporary_allocator(), LLVMTypeRef, field_count); LLVMGetStructElementTypes(src, fields); diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 5fd860b29..02748663b 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -776,16 +776,16 @@ gb_internal void lb_build_nested_proc(lbProcedure *p, AstProcLit *pd, Entity *e) -gb_internal Array lb_value_to_array(lbProcedure *p, lbValue value) { +gb_internal Array lb_value_to_array(lbProcedure *p, gbAllocator const &allocator, lbValue value) { Array array = {}; Type *t = base_type(value.type); if (t == nullptr) { // Do nothing } else if (is_type_tuple(t)) { - array = array_make(permanent_allocator(), 0, t->Tuple.variables.count); + array = array_make(allocator, 0, t->Tuple.variables.count); lb_append_tuple_values(p, &array, value); } else { - array = array_make(permanent_allocator(), 1); + array = array_make(allocator, 1); array[0] = value; } return array; @@ -1175,15 +1175,15 @@ gb_internal lbValue lb_emit_call(lbProcedure *p, lbValue value, Array c case DeferredProcedure_none: break; case DeferredProcedure_in: - result_as_args = in_args; + result_as_args = array_clone(heap_allocator(), in_args); break; case DeferredProcedure_out: - result_as_args = lb_value_to_array(p, result); + result_as_args = lb_value_to_array(p, heap_allocator(), result); break; case DeferredProcedure_in_out: { - auto out_args = lb_value_to_array(p, result); - array_init(&result_as_args, permanent_allocator(), in_args.count + out_args.count); + auto out_args = lb_value_to_array(p, heap_allocator(), result); + array_init(&result_as_args, heap_allocator(), in_args.count + out_args.count); array_copy(&result_as_args, in_args, 0); array_copy(&result_as_args, out_args, in_args.count); } -- cgit v1.2.3 From 843eaf8893e3a7df9645c0bcba86c71a01a14765 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 18 Apr 2023 17:20:50 +0100 Subject: Fix race condition with -use-separate-modules due to type determination --- src/llvm_backend.hpp | 8 +++++--- src/llvm_backend_general.cpp | 6 ++++++ src/llvm_backend_utility.cpp | 7 +++++++ 3 files changed, 18 insertions(+), 3 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 73e125f41..964195223 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -139,9 +139,11 @@ struct lbModule { AstPackage *pkg; // possibly associated AstFile *file; // possibly associated - PtrMap types; - PtrMap func_raw_types; - PtrMap struct_field_remapping; // Key: LLVMTypeRef or Type * + PtrMap types; // mutex: types_mutex + PtrMap struct_field_remapping; // Key: LLVMTypeRef or Type *, mutex: types_mutex + PtrMap func_raw_types; // mutex: func_raw_types_mutex + RecursiveMutex types_mutex; + RecursiveMutex func_raw_types_mutex; i32 internal_type_level; RwMutex values_mutex; diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index aa887418f..7d2f574fe 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -1498,6 +1498,9 @@ gb_internal LLVMTypeRef lb_type_internal_for_procedures_raw(lbModule *m, Type *t type = base_type(original_type); GB_ASSERT(type->kind == Type_Proc); + mutex_lock(&m->func_raw_types_mutex); + defer (mutex_unlock(&m->func_raw_types_mutex)); + LLVMTypeRef *found = map_get(&m->func_raw_types, type); if (found) { return *found; @@ -2157,6 +2160,9 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { gb_internal LLVMTypeRef lb_type(lbModule *m, Type *type) { type = default_type(type); + mutex_lock(&m->types_mutex); + defer (mutex_unlock(&m->types_mutex)); + LLVMTypeRef *found = map_get(&m->types, type); if (found) { return *found; diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp index 19df9ab06..ddae2243b 100644 --- a/src/llvm_backend_utility.cpp +++ b/src/llvm_backend_utility.cpp @@ -910,11 +910,18 @@ gb_internal lbValue lb_address_from_load_if_readonly_parameter(lbProcedure *p, l gb_internal lbStructFieldRemapping lb_get_struct_remapping(lbModule *m, Type *t) { t = base_type(t); + LLVMTypeRef struct_type = lb_type(m, t); + + mutex_lock(&m->types_mutex); + auto *field_remapping = map_get(&m->struct_field_remapping, cast(void *)struct_type); if (field_remapping == nullptr) { field_remapping = map_get(&m->struct_field_remapping, cast(void *)t); } + + mutex_unlock(&m->types_mutex); + GB_ASSERT_MSG(field_remapping != nullptr, "%s", type_to_string(t)); return *field_remapping; } -- cgit v1.2.3 From cde442fa2cb8ebd2a2e305b75f677348c17bb768 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 20 Apr 2023 11:55:18 +0100 Subject: Add internal padding to types where ptr size != int size --- src/llvm_backend_general.cpp | 60 +++++++++++++++++++++++++++++++++----------- src/llvm_backend_utility.cpp | 28 ++++++++++++++++++++- 2 files changed, 72 insertions(+), 16 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 7d2f574fe..5108e7c39 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -1626,6 +1626,8 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { GB_ASSERT(type != t_invalid); + bool bigger_int = build_context.word_size != build_context.int_size; + switch (type->kind) { case Type_Basic: switch (type->Basic.kind) { @@ -1760,8 +1762,8 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { return type; } - case Basic_int: return LLVMIntTypeInContext(ctx, 8*cast(unsigned)build_context.word_size); - case Basic_uint: return LLVMIntTypeInContext(ctx, 8*cast(unsigned)build_context.word_size); + case Basic_int: return LLVMIntTypeInContext(ctx, 8*cast(unsigned)build_context.int_size); + case Basic_uint: return LLVMIntTypeInContext(ctx, 8*cast(unsigned)build_context.int_size); case Basic_uintptr: return LLVMIntTypeInContext(ctx, 8*cast(unsigned)build_context.word_size); @@ -1922,23 +1924,43 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { case Type_Slice: { - LLVMTypeRef fields[2] = { - LLVMPointerType(lb_type(m, type->Slice.elem), 0), // data - lb_type(m, t_int), // len - }; - return LLVMStructTypeInContext(ctx, fields, 2, false); + if (bigger_int) { + LLVMTypeRef fields[3] = { + LLVMPointerType(lb_type(m, type->Slice.elem), 0), // data + lb_type_padding_filler(m, build_context.word_size, build_context.word_size), // padding + lb_type(m, t_int), // len + }; + return LLVMStructTypeInContext(ctx, fields, gb_count_of(fields), false); + } else { + LLVMTypeRef fields[2] = { + LLVMPointerType(lb_type(m, type->Slice.elem), 0), // data + lb_type(m, t_int), // len + }; + return LLVMStructTypeInContext(ctx, fields, gb_count_of(fields), false); + } } break; case Type_DynamicArray: { - LLVMTypeRef fields[4] = { - LLVMPointerType(lb_type(m, type->DynamicArray.elem), 0), // data - lb_type(m, t_int), // len - lb_type(m, t_int), // cap - lb_type(m, t_allocator), // allocator - }; - return LLVMStructTypeInContext(ctx, fields, 4, false); + if (bigger_int) { + LLVMTypeRef fields[5] = { + LLVMPointerType(lb_type(m, type->DynamicArray.elem), 0), // data + lb_type_padding_filler(m, build_context.word_size, build_context.word_size), // padding + lb_type(m, t_int), // len + lb_type(m, t_int), // cap + lb_type(m, t_allocator), // allocator + }; + return LLVMStructTypeInContext(ctx, fields, gb_count_of(fields), false); + } else { + LLVMTypeRef fields[4] = { + LLVMPointerType(lb_type(m, type->DynamicArray.elem), 0), // data + lb_type(m, t_int), // len + lb_type(m, t_int), // cap + lb_type(m, t_allocator), // allocator + }; + return LLVMStructTypeInContext(ctx, fields, gb_count_of(fields), false); + } } break; @@ -2145,9 +2167,17 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { case Type_SoaPointer: { unsigned field_count = 2; + if (bigger_int) { + field_count = 3; + } LLVMTypeRef *fields = gb_alloc_array(permanent_allocator(), LLVMTypeRef, field_count); fields[0] = LLVMPointerType(lb_type(m, type->Pointer.elem), 0); - fields[1] = LLVMIntTypeInContext(ctx, 8*cast(unsigned)build_context.word_size); + if (bigger_int) { + fields[1] = lb_type_padding_filler(m, build_context.word_size, build_context.word_size); + fields[2] = LLVMIntTypeInContext(ctx, 8*cast(unsigned)build_context.int_size); + } else { + fields[1] = LLVMIntTypeInContext(ctx, 8*cast(unsigned)build_context.int_size); + } return LLVMStructTypeInContext(ctx, fields, field_count, false); } diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp index ddae2243b..99deca5e9 100644 --- a/src/llvm_backend_utility.cpp +++ b/src/llvm_backend_utility.cpp @@ -929,7 +929,33 @@ gb_internal lbStructFieldRemapping lb_get_struct_remapping(lbModule *m, Type *t) gb_internal i32 lb_convert_struct_index(lbModule *m, Type *t, i32 index) { if (t->kind == Type_Struct) { auto field_remapping = lb_get_struct_remapping(m, t); - index = field_remapping[index]; + return field_remapping[index]; + } else if (build_context.word_size != build_context.int_size) { + switch (t->kind) { + case Type_Slice: + GB_ASSERT(build_context.word_size*2 == build_context.int_size); + switch (index) { + case 0: return 0; // data + case 1: return 2; // len + } + break; + case Type_DynamicArray: + GB_ASSERT(build_context.word_size*2 == build_context.int_size); + switch (index) { + case 0: return 0; // data + case 1: return 2; // len + case 2: return 3; // cap + case 3: return 4; // allocator + } + break; + case Type_SoaPointer: + GB_ASSERT(build_context.word_size*2 == build_context.int_size); + switch (index) { + case 0: return 0; // data + case 1: return 2; // offset + } + break; + } } return index; } -- cgit v1.2.3 From 685f7d0feae7b7bbfee5b24f6e6dc6751366d36e Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 20 Apr 2023 12:18:13 +0100 Subject: Rename `word_size` to `ptr_size` internally to make it clearer --- src/build_settings.cpp | 18 +++++++++--------- src/llvm_abi.cpp | 8 ++++---- src/llvm_backend.hpp | 2 +- src/llvm_backend_debug.cpp | 30 +++++++++++++++--------------- src/llvm_backend_general.cpp | 12 ++++++------ src/llvm_backend_proc.cpp | 4 ++-- src/llvm_backend_stmt.cpp | 2 +- src/llvm_backend_type.cpp | 4 ++-- src/llvm_backend_utility.cpp | 10 +++++----- src/types.cpp | 44 ++++++++++++++++++++++---------------------- 10 files changed, 67 insertions(+), 67 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 63997c2d1..af6c5a6d4 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -116,7 +116,7 @@ gb_global String const ODIN_VERSION = str_lit(ODIN_VERSION_RAW); struct TargetMetrics { TargetOsKind os; TargetArchKind arch; - isize word_size; + isize ptr_size; isize int_size; isize max_align; isize max_simd_align; @@ -238,10 +238,10 @@ struct BuildContext { TargetEndianKind endian_kind; // In bytes - i64 word_size; // Size of a pointer, must be >= 4 + i64 ptr_size; // Size of a pointer, must be >= 4 i64 int_size; // Size of a int/uint, must be >= 4 - i64 max_align; // max alignment, must be >= 1 (and typically >= word_size) - i64 max_simd_align; // max alignment, must be >= 1 (and typically >= word_size) + i64 max_align; // max alignment, must be >= 1 (and typically >= ptr_size) + i64 max_simd_align; // max alignment, must be >= 1 (and typically >= ptr_size) CommandKind command_kind; String command; @@ -1192,14 +1192,14 @@ gb_internal void init_build_context(TargetMetrics *cross_target) { GB_ASSERT(metrics->os != TargetOs_Invalid); GB_ASSERT(metrics->arch != TargetArch_Invalid); - GB_ASSERT(metrics->word_size > 1); + GB_ASSERT(metrics->ptr_size > 1); GB_ASSERT(metrics->int_size > 1); GB_ASSERT(metrics->max_align > 1); GB_ASSERT(metrics->max_simd_align > 1); - GB_ASSERT(metrics->int_size >= metrics->word_size); - if (metrics->int_size > metrics->word_size) { - GB_ASSERT(metrics->int_size == 2*metrics->word_size); + GB_ASSERT(metrics->int_size >= metrics->ptr_size); + if (metrics->int_size > metrics->ptr_size) { + GB_ASSERT(metrics->int_size == 2*metrics->ptr_size); } @@ -1208,7 +1208,7 @@ gb_internal void init_build_context(TargetMetrics *cross_target) { bc->ODIN_OS = target_os_names[metrics->os]; bc->ODIN_ARCH = target_arch_names[metrics->arch]; bc->endian_kind = target_endians[metrics->arch]; - bc->word_size = metrics->word_size; + bc->ptr_size = metrics->ptr_size; bc->int_size = metrics->int_size; bc->max_align = metrics->max_align; bc->max_simd_align = metrics->max_simd_align; diff --git a/src/llvm_abi.cpp b/src/llvm_abi.cpp index 3fc68644d..6308d7bbf 100644 --- a/src/llvm_abi.cpp +++ b/src/llvm_abi.cpp @@ -218,7 +218,7 @@ gb_internal i64 lb_sizeof(LLVMTypeRef type) { case LLVMDoubleTypeKind: return 8; case LLVMPointerTypeKind: - return build_context.word_size; + return build_context.ptr_size; case LLVMStructTypeKind: { unsigned field_count = LLVMCountStructElementTypes(type); @@ -275,7 +275,7 @@ gb_internal i64 lb_alignof(LLVMTypeRef type) { case LLVMIntegerTypeKind: { unsigned w = LLVMGetIntTypeWidth(type); - return gb_clamp((w + 7)/8, 1, build_context.word_size); + return gb_clamp((w + 7)/8, 1, build_context.ptr_size); } case LLVMHalfTypeKind: return 2; @@ -284,7 +284,7 @@ gb_internal i64 lb_alignof(LLVMTypeRef type) { case LLVMDoubleTypeKind: return 8; case LLVMPointerTypeKind: - return build_context.word_size; + return build_context.ptr_size; case LLVMStructTypeKind: { if (LLVMIsPackedStruct(type)) { @@ -388,7 +388,7 @@ namespace lbAbi386 { } if (build_context.metrics.os == TargetOs_windows && - build_context.word_size == 8 && + build_context.ptr_size == 8 && lb_is_type_kind(type, LLVMIntegerTypeKind) && type == LLVMIntTypeInContext(c, 128)) { // NOTE(bill): Because Windows AMD64 is weird diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 964195223..c626bb9a5 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -539,7 +539,7 @@ gb_internal void lb_mem_copy_non_overlapping(lbProcedure *p, lbValue dst, lbValu gb_internal LLVMValueRef lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, LLVMValueRef len, unsigned alignment, bool is_volatile); gb_internal gb_inline i64 lb_max_zero_init_size(void) { - return cast(i64)(4*build_context.word_size); + return cast(i64)(4*build_context.int_size); } gb_internal LLVMTypeRef OdinLLVMGetArrayElementType(LLVMTypeRef type); diff --git a/src/llvm_backend_debug.cpp b/src/llvm_backend_debug.cpp index d31179191..616ad3045 100644 --- a/src/llvm_backend_debug.cpp +++ b/src/llvm_backend_debug.cpp @@ -52,8 +52,8 @@ gb_internal LLVMMetadataRef lb_debug_type_internal_proc(lbModule *m, Type *type) GB_ASSERT(type != t_invalid); - /* unsigned const word_size = cast(unsigned)build_context.word_size; - unsigned const word_bits = cast(unsigned)(8*build_context.word_size); */ + /* unsigned const ptr_size = cast(unsigned)build_context.ptr_size; + unsigned const ptr_bits = cast(unsigned)(8*build_context.ptr_size); */ GB_ASSERT(type->kind == Type_Proc); unsigned parameter_count = 1; @@ -131,9 +131,9 @@ gb_internal LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) { GB_ASSERT(type != t_invalid); - /* unsigned const word_size = cast(unsigned)build_context.word_size; */ + /* unsigned const ptr_size = cast(unsigned)build_context.ptr_size; */ unsigned const int_bits = cast(unsigned)(8*build_context.int_size); - unsigned const word_bits = cast(unsigned)(8*build_context.word_size); + unsigned const ptr_bits = cast(unsigned)(8*build_context.ptr_size); switch (type->kind) { case Type_Basic: @@ -165,10 +165,10 @@ gb_internal LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) { case Basic_int: return lb_debug_type_basic_type(m, str_lit("int"), int_bits, LLVMDWARFTypeEncoding_Signed); case Basic_uint: return lb_debug_type_basic_type(m, str_lit("uint"), int_bits, LLVMDWARFTypeEncoding_Unsigned); - case Basic_uintptr: return lb_debug_type_basic_type(m, str_lit("uintptr"), word_bits, LLVMDWARFTypeEncoding_Unsigned); + case Basic_uintptr: return lb_debug_type_basic_type(m, str_lit("uintptr"), ptr_bits, LLVMDWARFTypeEncoding_Unsigned); case Basic_typeid: - return lb_debug_type_basic_type(m, str_lit("typeid"), word_bits, LLVMDWARFTypeEncoding_Unsigned); + return lb_debug_type_basic_type(m, str_lit("typeid"), ptr_bits, LLVMDWARFTypeEncoding_Unsigned); // Endian Specific Types case Basic_i16le: return lb_debug_type_basic_type(m, str_lit("i16le"), 16, LLVMDWARFTypeEncoding_Signed, LLVMDIFlagLittleEndian); @@ -252,7 +252,7 @@ gb_internal LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) { case Basic_rawptr: { LLVMMetadataRef void_type = lb_debug_type_basic_type(m, str_lit("void"), 8, LLVMDWARFTypeEncoding_Unsigned); - return LLVMDIBuilderCreatePointerType(m->debug_builder, void_type, word_bits, word_bits, LLVMDWARFTypeEncoding_Address, "rawptr", 6); + return LLVMDIBuilderCreatePointerType(m->debug_builder, void_type, ptr_bits, ptr_bits, LLVMDWARFTypeEncoding_Address, "rawptr", 6); } case Basic_string: { @@ -264,14 +264,14 @@ gb_internal LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) { case Basic_cstring: { LLVMMetadataRef char_type = lb_debug_type_basic_type(m, str_lit("char"), 8, LLVMDWARFTypeEncoding_Unsigned); - return LLVMDIBuilderCreatePointerType(m->debug_builder, char_type, word_bits, word_bits, 0, "cstring", 7); + return LLVMDIBuilderCreatePointerType(m->debug_builder, char_type, ptr_bits, ptr_bits, 0, "cstring", 7); } case Basic_any: { LLVMMetadataRef elements[2] = {}; elements[0] = lb_debug_struct_field(m, str_lit("data"), t_rawptr, 0); - elements[1] = lb_debug_struct_field(m, str_lit("id"), t_typeid, word_bits); - return lb_debug_basic_struct(m, str_lit("any"), 2*word_bits, word_bits, elements, gb_count_of(elements)); + elements[1] = lb_debug_struct_field(m, str_lit("id"), t_typeid, ptr_bits); + return lb_debug_basic_struct(m, str_lit("any"), 2*ptr_bits, ptr_bits, elements, gb_count_of(elements)); } // Untyped types @@ -295,9 +295,9 @@ gb_internal LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) { case Type_SoaPointer: return LLVMDIBuilderCreatePointerType(m->debug_builder, lb_debug_type(m, type->SoaPointer.elem), int_bits, int_bits, 0, nullptr, 0); case Type_Pointer: - return LLVMDIBuilderCreatePointerType(m->debug_builder, lb_debug_type(m, type->Pointer.elem), word_bits, word_bits, 0, nullptr, 0); + return LLVMDIBuilderCreatePointerType(m->debug_builder, lb_debug_type(m, type->Pointer.elem), ptr_bits, ptr_bits, 0, nullptr, 0); case Type_MultiPointer: - return LLVMDIBuilderCreatePointerType(m->debug_builder, lb_debug_type(m, type->MultiPointer.elem), word_bits, word_bits, 0, nullptr, 0); + return LLVMDIBuilderCreatePointerType(m->debug_builder, lb_debug_type(m, type->MultiPointer.elem), ptr_bits, ptr_bits, 0, nullptr, 0); case Type_Array: { LLVMMetadataRef subscripts[1] = {}; @@ -417,7 +417,7 @@ gb_internal LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) { case Type_Proc: { LLVMMetadataRef proc_underlying_type = lb_debug_type_internal_proc(m, type); - LLVMMetadataRef pointer_type = LLVMDIBuilderCreatePointerType(m->debug_builder, proc_underlying_type, word_bits, word_bits, 0, nullptr, 0); + LLVMMetadataRef pointer_type = LLVMDIBuilderCreatePointerType(m->debug_builder, proc_underlying_type, ptr_bits, ptr_bits, 0, nullptr, 0); gbString name = type_to_string(type, temporary_allocator()); return LLVMDIBuilderCreateTypedef(m->debug_builder, pointer_type, name, gb_string_length(name), nullptr, 0, nullptr, cast(u32)(8*type_align_of(type))); } @@ -618,8 +618,8 @@ gb_internal LLVMMetadataRef lb_debug_type(lbModule *m, Type *type) { } gb_internal void lb_debug_complete_types(lbModule *m) { - /* unsigned const word_size = cast(unsigned)build_context.word_size; */ - unsigned const word_bits = cast(unsigned)(8*build_context.word_size); + /* unsigned const ptr_size = cast(unsigned)build_context.ptr_size; */ + unsigned const ptr_bits = cast(unsigned)(8*build_context.ptr_size); unsigned const int_bits = cast(unsigned)(8*build_context.int_size); for_array(debug_incomplete_type_index, m->debug_incomplete_types) { diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 5108e7c39..3d38416e0 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -1626,7 +1626,7 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { GB_ASSERT(type != t_invalid); - bool bigger_int = build_context.word_size != build_context.int_size; + bool bigger_int = build_context.ptr_size != build_context.int_size; switch (type->kind) { case Type_Basic: @@ -1765,7 +1765,7 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { case Basic_int: return LLVMIntTypeInContext(ctx, 8*cast(unsigned)build_context.int_size); case Basic_uint: return LLVMIntTypeInContext(ctx, 8*cast(unsigned)build_context.int_size); - case Basic_uintptr: return LLVMIntTypeInContext(ctx, 8*cast(unsigned)build_context.word_size); + case Basic_uintptr: return LLVMIntTypeInContext(ctx, 8*cast(unsigned)build_context.ptr_size); case Basic_rawptr: return LLVMPointerType(LLVMInt8TypeInContext(ctx), 0); case Basic_string: @@ -1800,7 +1800,7 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { return type; } - case Basic_typeid: return LLVMIntTypeInContext(m->ctx, 8*cast(unsigned)build_context.word_size); + case Basic_typeid: return LLVMIntTypeInContext(m->ctx, 8*cast(unsigned)build_context.ptr_size); // Endian Specific Types case Basic_i16le: return LLVMInt16TypeInContext(ctx); @@ -1927,7 +1927,7 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { if (bigger_int) { LLVMTypeRef fields[3] = { LLVMPointerType(lb_type(m, type->Slice.elem), 0), // data - lb_type_padding_filler(m, build_context.word_size, build_context.word_size), // padding + lb_type_padding_filler(m, build_context.ptr_size, build_context.ptr_size), // padding lb_type(m, t_int), // len }; return LLVMStructTypeInContext(ctx, fields, gb_count_of(fields), false); @@ -1946,7 +1946,7 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { if (bigger_int) { LLVMTypeRef fields[5] = { LLVMPointerType(lb_type(m, type->DynamicArray.elem), 0), // data - lb_type_padding_filler(m, build_context.word_size, build_context.word_size), // padding + lb_type_padding_filler(m, build_context.ptr_size, build_context.ptr_size), // padding lb_type(m, t_int), // len lb_type(m, t_int), // cap lb_type(m, t_allocator), // allocator @@ -2173,7 +2173,7 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { LLVMTypeRef *fields = gb_alloc_array(permanent_allocator(), LLVMTypeRef, field_count); fields[0] = LLVMPointerType(lb_type(m, type->Pointer.elem), 0); if (bigger_int) { - fields[1] = lb_type_padding_filler(m, build_context.word_size, build_context.word_size); + fields[1] = lb_type_padding_filler(m, build_context.ptr_size, build_context.ptr_size); fields[2] = LLVMIntTypeInContext(ctx, 8*cast(unsigned)build_context.int_size); } else { fields[1] = LLVMIntTypeInContext(ctx, 8*cast(unsigned)build_context.int_size); diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index ddf058668..651ebf35c 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -14,7 +14,7 @@ gb_internal void lb_mem_copy_overlapping(lbProcedure *p, lbValue dst, lbValue sr char const *name = "llvm.memmove"; if (LLVMIsConstant(len.value)) { i64 const_len = cast(i64)LLVMConstIntGetSExtValue(len.value); - if (const_len <= 4*build_context.word_size) { + if (const_len <= 4*build_context.int_size) { name = "llvm.memmove.inline"; } } @@ -43,7 +43,7 @@ gb_internal void lb_mem_copy_non_overlapping(lbProcedure *p, lbValue dst, lbValu char const *name = "llvm.memcpy"; if (LLVMIsConstant(len.value)) { i64 const_len = cast(i64)LLVMConstIntGetSExtValue(len.value); - if (const_len <= 4*build_context.word_size) { + if (const_len <= 4*build_context.int_size) { name = "llvm.memcpy.inline"; } } diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 125913ac5..00e02092d 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -1652,7 +1652,7 @@ gb_internal void lb_build_return_stmt_internal(lbProcedure *p, lbValue res) { if (res.value != nullptr) { LLVMValueRef res_val = res.value; i64 sz = type_size_of(res.type); - if (LLVMIsALoadInst(res_val) && sz > build_context.word_size) { + if (LLVMIsALoadInst(res_val) && sz > build_context.int_size) { lbValue ptr = lb_address_from_load_or_generate_local(p, res); lb_mem_copy_non_overlapping(p, p->return_ptr.addr, ptr, lb_const_int(p->module, t_int, sz)); } else { diff --git a/src/llvm_backend_type.cpp b/src/llvm_backend_type.cpp index 3af10112f..eba41a55c 100644 --- a/src/llvm_backend_type.cpp +++ b/src/llvm_backend_type.cpp @@ -68,7 +68,7 @@ gb_internal lbValue lb_typeid(lbModule *m, Type *type) { } u64 data = 0; - if (build_context.word_size == 4) { + if (build_context.ptr_size == 4) { GB_ASSERT(id <= (1u<<24u)); data |= (id &~ (1u<<24)) << 0u; // index data |= (kind &~ (1u<<5)) << 24u; // kind @@ -76,7 +76,7 @@ gb_internal lbValue lb_typeid(lbModule *m, Type *type) { data |= (special &~ (1u<<1)) << 30u; // kind data |= (reserved &~ (1u<<1)) << 31u; // kind } else { - GB_ASSERT(build_context.word_size == 8); + GB_ASSERT(build_context.ptr_size == 8); GB_ASSERT(id <= (1ull<<56u)); data |= (id &~ (1ull<<56)) << 0ul; // index data |= (kind &~ (1ull<<5)) << 56ull; // kind diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp index 758657657..4499803e5 100644 --- a/src/llvm_backend_utility.cpp +++ b/src/llvm_backend_utility.cpp @@ -930,17 +930,17 @@ gb_internal i32 lb_convert_struct_index(lbModule *m, Type *t, i32 index) { if (t->kind == Type_Struct) { auto field_remapping = lb_get_struct_remapping(m, t); return field_remapping[index]; - } else if (build_context.word_size != build_context.int_size) { + } else if (build_context.ptr_size != build_context.int_size) { switch (t->kind) { case Type_Slice: - GB_ASSERT(build_context.word_size*2 == build_context.int_size); + GB_ASSERT(build_context.ptr_size*2 == build_context.int_size); switch (index) { case 0: return 0; // data case 1: return 2; // len } break; case Type_DynamicArray: - GB_ASSERT(build_context.word_size*2 == build_context.int_size); + GB_ASSERT(build_context.ptr_size*2 == build_context.int_size); switch (index) { case 0: return 0; // data case 1: return 2; // len @@ -949,7 +949,7 @@ gb_internal i32 lb_convert_struct_index(lbModule *m, Type *t, i32 index) { } break; case Type_SoaPointer: - GB_ASSERT(build_context.word_size*2 == build_context.int_size); + GB_ASSERT(build_context.ptr_size*2 == build_context.int_size); switch (index) { case 0: return 0; // data case 1: return 2; // offset @@ -1589,7 +1589,7 @@ gb_internal lbValue lb_map_data_uintptr(lbProcedure *p, lbValue value) { GB_ASSERT(is_type_map(value.type) || are_types_identical(value.type, t_raw_map)); lbValue data = lb_emit_struct_ev(p, value, 0); u64 mask_value = 0; - if (build_context.word_size == 4) { + if (build_context.ptr_size == 4) { mask_value = 0xfffffffful & ~(MAP_CACHE_LINE_SIZE-1); } else { mask_value = 0xffffffffffffffffull & ~(MAP_CACHE_LINE_SIZE-1); diff --git a/src/types.cpp b/src/types.cpp index 0ce2f127f..ddfedf1e1 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -3411,15 +3411,15 @@ gb_internal i64 type_size_of(Type *t) { GB_ASSERT_MSG(is_type_typed(t), "%s", type_to_string(t)); switch (t->Basic.kind) { case Basic_string: size = 2*build_context.int_size; break; - case Basic_cstring: size = build_context.word_size; break; - case Basic_any: size = 2*build_context.word_size; break; - case Basic_typeid: size = build_context.word_size; break; + case Basic_cstring: size = build_context.ptr_size; break; + case Basic_any: size = 2*build_context.ptr_size; break; + case Basic_typeid: size = build_context.ptr_size; break; case Basic_int: case Basic_uint: size = build_context.int_size; break; case Basic_uintptr: case Basic_rawptr: - size = build_context.word_size; + size = build_context.ptr_size; break; default: size = t->Basic.size; @@ -3474,14 +3474,14 @@ gb_internal i64 type_align_of_internal(Type *t, TypePath *path) { GB_ASSERT(is_type_typed(t)); switch (t->Basic.kind) { case Basic_string: return build_context.int_size; - case Basic_cstring: return build_context.word_size; - case Basic_any: return build_context.word_size; - case Basic_typeid: return build_context.word_size; + case Basic_cstring: return build_context.ptr_size; + case Basic_any: return build_context.ptr_size; + case Basic_typeid: return build_context.ptr_size; case Basic_int: case Basic_uint: return build_context.int_size; case Basic_uintptr: case Basic_rawptr: - return build_context.word_size; + return build_context.ptr_size; case Basic_complex32: case Basic_complex64: case Basic_complex128: return type_size_of_internal(t, path) / 2; @@ -3532,7 +3532,7 @@ gb_internal i64 type_align_of_internal(Type *t, TypePath *path) { } break; case Type_Map: - return build_context.word_size; + return build_context.ptr_size; case Type_Enum: return type_align_of_internal(t->Enum.base_type, path); @@ -3615,7 +3615,7 @@ gb_internal i64 type_align_of_internal(Type *t, TypePath *path) { return build_context.int_size; } - // NOTE(bill): Things that are bigger than build_context.word_size, are actually comprised of smaller types + // NOTE(bill): Things that are bigger than build_context.ptr_size, are actually comprised of smaller types // TODO(bill): Is this correct for 128-bit types (integers)? return gb_clamp(next_pow2(type_size_of_internal(t, path)), 1, build_context.max_align); } @@ -3698,22 +3698,22 @@ gb_internal i64 type_size_of_internal(Type *t, TypePath *path) { } switch (kind) { case Basic_string: return 2*build_context.int_size; - case Basic_cstring: return build_context.word_size; - case Basic_any: return 2*build_context.word_size; - case Basic_typeid: return build_context.word_size; + case Basic_cstring: return build_context.ptr_size; + case Basic_any: return 2*build_context.ptr_size; + case Basic_typeid: return build_context.ptr_size; case Basic_int: case Basic_uint: return build_context.int_size; case Basic_uintptr: case Basic_rawptr: - return build_context.word_size; + return build_context.ptr_size; } } break; case Type_Pointer: - return build_context.word_size; + return build_context.ptr_size; case Type_MultiPointer: - return build_context.word_size; + return build_context.ptr_size; case Type_SoaPointer: return build_context.int_size*2; @@ -3753,7 +3753,7 @@ gb_internal i64 type_size_of_internal(Type *t, TypePath *path) { case Type_DynamicArray: // data + len + cap + allocator(procedure+data) - return 3*build_context.int_size + 2*build_context.word_size; + return 3*build_context.int_size + 2*build_context.ptr_size; case Type_Map: /* @@ -3763,7 +3763,7 @@ gb_internal i64 type_size_of_internal(Type *t, TypePath *path) { allocator: runtime.Allocator, // 2 words } */ - return (1 + 1 + 2)*build_context.word_size; + return (1 + 1 + 2)*build_context.ptr_size; case Type_Tuple: { i64 count, align, size; @@ -3889,7 +3889,7 @@ gb_internal i64 type_size_of_internal(Type *t, TypePath *path) { } // Catch all - return build_context.word_size; + return build_context.ptr_size; } gb_internal i64 type_offset_of(Type *t, i32 index) { @@ -3914,8 +3914,8 @@ gb_internal i64 type_offset_of(Type *t, i32 index) { } } else if (t->Basic.kind == Basic_any) { switch (index) { - case 0: return 0; // type_info - case 1: return build_context.word_size; // data + case 0: return 0; // type_info + case 1: return build_context.ptr_size; // data } } } else if (t->kind == Type_Slice) { @@ -3934,7 +3934,7 @@ gb_internal i64 type_offset_of(Type *t, i32 index) { } else if (t->kind == Type_Union) { /* i64 s = */ type_size_of(t); switch (index) { - case -1: return align_formula(t->Union.variant_block_size, build_context.word_size); // __type_info + case -1: return align_formula(t->Union.variant_block_size, build_context.ptr_size); // __type_info } } return 0; -- cgit v1.2.3 From 8bf32ac697ea21ff3b37e5b31fe0fc10e700c9a4 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 22 May 2023 12:53:29 +0100 Subject: Minor change to handling of propagation of errors with `---` as a value --- src/check_decl.cpp | 9 ++++----- src/check_expr.cpp | 45 +++++++++++++++++++++----------------------- src/check_type.cpp | 6 +++--- src/checker.cpp | 4 ++-- src/llvm_backend.cpp | 10 ++-------- src/llvm_backend_const.cpp | 2 +- src/llvm_backend_debug.cpp | 2 +- src/llvm_backend_expr.cpp | 16 ++++++++-------- src/llvm_backend_general.cpp | 4 ++-- src/llvm_backend_proc.cpp | 8 ++++---- src/parser.cpp | 14 +++++++------- src/parser.hpp | 2 +- src/parser_pos.cpp | 4 ++-- src/tokenizer.cpp | 6 +++--- src/types.cpp | 19 ++++++++----------- 15 files changed, 69 insertions(+), 82 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/check_decl.cpp b/src/check_decl.cpp index b16215571..84b0d39d8 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -70,13 +70,12 @@ gb_internal Type *check_init_variable(CheckerContext *ctx, Entity *e, Operand *o // NOTE(bill): Use the type of the operand Type *t = operand->type; if (is_type_untyped(t)) { - if (t == t_invalid || is_type_untyped_nil(t)) { - error(e->token, "Invalid use of untyped nil in %.*s", LIT(context_name)); + if (is_type_untyped_uninit(t)) { + error(e->token, "Invalid use of --- in %.*s", LIT(context_name)); e->type = t_invalid; return nullptr; - } - if (t == t_invalid || is_type_untyped_undef(t)) { - error(e->token, "Invalid use of --- in %.*s", LIT(context_name)); + } else if (t == t_invalid || is_type_untyped_nil(t)) { + error(e->token, "Invalid use of untyped nil in %.*s", LIT(context_name)); e->type = t_invalid; return nullptr; } diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 20a690e1e..830b5315d 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -646,11 +646,8 @@ gb_internal i64 check_distance_between_types(CheckerContext *c, Operand *operand Type *src = base_type(s); Type *dst = base_type(type); - if (is_type_untyped_undef(src)) { - if (type_has_undef(dst)) { - return 1; - } - return -1; + if (is_type_untyped_uninit(src)) { + return 1; } if (is_type_untyped_nil(src)) { @@ -993,13 +990,13 @@ gb_internal void check_assignment(CheckerContext *c, Operand *operand, Type *typ if (is_type_untyped(operand->type)) { Type *target_type = type; if (type == nullptr || is_type_any(type)) { - if (type == nullptr && is_type_untyped_nil(operand->type)) { - error(operand->expr, "Use of untyped nil in %.*s", LIT(context_name)); + if (type == nullptr && is_type_untyped_uninit(operand->type)) { + error(operand->expr, "Use of --- in %.*s", LIT(context_name)); operand->mode = Addressing_Invalid; return; } - if (type == nullptr && is_type_untyped_undef(operand->type)) { - error(operand->expr, "Use of --- in %.*s", LIT(context_name)); + if (type == nullptr && is_type_untyped_nil(operand->type)) { + error(operand->expr, "Use of untyped nil in %.*s", LIT(context_name)); operand->mode = Addressing_Invalid; return; } @@ -3969,7 +3966,7 @@ gb_internal void convert_to_typed(CheckerContext *c, Operand *operand, Type *tar case Type_Union: - if (!is_operand_nil(*operand) && !is_operand_undef(*operand)) { + if (!is_operand_nil(*operand) && !is_operand_uninit(*operand)) { TEMPORARY_ALLOCATOR_GUARD(); isize count = t->Union.variants.count; @@ -4036,8 +4033,8 @@ gb_internal void convert_to_typed(CheckerContext *c, Operand *operand, Type *tar error_line("\n\n"); return; - } else if (is_type_untyped_undef(operand->type) && type_has_undef(target_type)) { - target_type = t_untyped_undef; + } else if (is_type_untyped_uninit(operand->type)) { + target_type = t_untyped_uninit; } else if (!is_type_untyped_nil(operand->type) || !type_has_nil(target_type)) { begin_error_block(); defer (end_error_block()); @@ -4070,8 +4067,8 @@ gb_internal void convert_to_typed(CheckerContext *c, Operand *operand, Type *tar default: - if (is_type_untyped_undef(operand->type) && type_has_undef(target_type)) { - target_type = t_untyped_undef; + if (is_type_untyped_uninit(operand->type)) { + target_type = t_untyped_uninit; } else if (is_type_untyped_nil(operand->type) && type_has_nil(target_type)) { target_type = t_untyped_nil; } else { @@ -4083,8 +4080,8 @@ gb_internal void convert_to_typed(CheckerContext *c, Operand *operand, Type *tar } if (is_type_any(target_type) && is_type_untyped(operand->type)) { - if (is_type_untyped_nil(operand->type) && is_type_untyped_undef(operand->type)) { - + if (is_type_untyped_nil(operand->type) && is_type_untyped_uninit(operand->type)) { + } else { target_type = default_type(operand->type); } @@ -5197,9 +5194,9 @@ gb_internal bool check_unpack_arguments(CheckerContext *ctx, Entity **lhs, isize } Ast *rhs_expr = unparen_expr(rhs[i]); - if (allow_undef && rhs_expr != nullptr && rhs_expr->kind == Ast_Undef) { + if (allow_undef && rhs_expr != nullptr && rhs_expr->kind == Ast_Uninit) { // NOTE(bill): Just handle this very specific logic here - o.type = t_untyped_undef; + o.type = t_untyped_uninit; o.mode = Addressing_Value; o.expr = rhs[i]; add_type_and_value(c, rhs[i], o.mode, o.type, o.value); @@ -7167,11 +7164,11 @@ gb_internal bool check_set_index_data(Operand *o, Type *t, bool indirection, i64 } gb_internal bool ternary_compare_types(Type *x, Type *y) { - if (is_type_untyped_undef(x) && type_has_undef(y)) { + if (is_type_untyped_uninit(x)) { return true; } else if (is_type_untyped_nil(x) && type_has_nil(y)) { return true; - } else if (is_type_untyped_undef(y) && type_has_undef(x)) { + } else if (is_type_untyped_uninit(y)) { return true; } else if (is_type_untyped_nil(y) && type_has_nil(x)) { return true; @@ -7708,7 +7705,7 @@ gb_internal ExprKind check_ternary_if_expr(CheckerContext *c, Operand *o, Ast *n } o->type = x.type; - if (is_type_untyped_nil(o->type) || is_type_untyped_undef(o->type)) { + if (is_type_untyped_nil(o->type) || is_type_untyped_uninit(o->type)) { o->type = y.type; } @@ -9601,9 +9598,9 @@ gb_internal ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast check_ident(c, o, node, nullptr, type_hint, false); case_end; - case_ast_node(u, Undef, node); + case_ast_node(u, Uninit, node); o->mode = Addressing_Value; - o->type = t_untyped_undef; + o->type = t_untyped_uninit; error(node, "Use of --- outside of variable declaration"); case_end; @@ -10167,7 +10164,7 @@ gb_internal gbString write_expr_to_string(gbString str, Ast *node, bool shorthan str = string_append_string(str, bd->name.string); case_end; - case_ast_node(ud, Undef, node); + case_ast_node(ud, Uninit, node); str = gb_string_appendc(str, "---"); case_end; diff --git a/src/check_type.cpp b/src/check_type.cpp index dfe774f6b..bbfc25a12 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -135,7 +135,7 @@ gb_internal void check_struct_fields(CheckerContext *ctx, Ast *node, Slicemodule, var.var); - if (is_type_untyped_undef(init.type)) { - // LLVMSetInitializer(var.var.value, LLVMGetUndef(global_type)); - LLVMSetInitializer(var.var.value, LLVMConstNull(global_type)); - var.is_initialized = true; - continue; - } else if (is_type_untyped_nil(init.type)) { + if (is_type_untyped_nil(init.type)) { LLVMSetInitializer(var.var.value, LLVMConstNull(global_type)); var.is_initialized = true; continue; @@ -2363,8 +2358,7 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { } } } - if (!var.is_initialized && - (is_type_untyped_nil(tav.type) || is_type_untyped_undef(tav.type))) { + if (!var.is_initialized && is_type_untyped_nil(tav.type)) { var.is_initialized = true; } } diff --git a/src/llvm_backend_const.cpp b/src/llvm_backend_const.cpp index 8db6e2a1f..ea25a4594 100644 --- a/src/llvm_backend_const.cpp +++ b/src/llvm_backend_const.cpp @@ -1,6 +1,6 @@ gb_internal bool lb_is_const(lbValue value) { LLVMValueRef v = value.value; - if (is_type_untyped_nil(value.type) || is_type_untyped_undef(value.type)) { + if (is_type_untyped_nil(value.type)) { // TODO(bill): Is this correct behaviour? return true; } diff --git a/src/llvm_backend_debug.cpp b/src/llvm_backend_debug.cpp index fd26c41a0..bec719a4a 100644 --- a/src/llvm_backend_debug.cpp +++ b/src/llvm_backend_debug.cpp @@ -282,7 +282,7 @@ gb_internal LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) { case Basic_UntypedString: GB_PANIC("Basic_UntypedString"); break; case Basic_UntypedRune: GB_PANIC("Basic_UntypedRune"); break; case Basic_UntypedNil: GB_PANIC("Basic_UntypedNil"); break; - case Basic_UntypedUndef: GB_PANIC("Basic_UntypedUndef"); break; + case Basic_UntypedUninit: GB_PANIC("Basic_UntypedUninit"); break; default: GB_PANIC("Basic Unhandled"); break; } diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index 69443c9a3..143e38a8d 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -1486,12 +1486,12 @@ gb_internal lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) { GB_ASSERT(src != nullptr); GB_ASSERT(dst != nullptr); + if (is_type_untyped_uninit(src)) { + return lb_const_undef(m, t); + } if (is_type_untyped_nil(src)) { return lb_const_nil(m, t); } - if (is_type_untyped_undef(src)) { - return lb_const_undef(m, t); - } if (LLVMIsConstant(value.value)) { if (is_type_any(dst)) { @@ -2132,12 +2132,12 @@ gb_internal lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) { if (is_type_any(dst)) { + if (is_type_untyped_uninit(src)) { + return lb_const_undef(p->module, t); + } if (is_type_untyped_nil(src)) { return lb_const_nil(p->module, t); } - if (is_type_untyped_undef(src)) { - return lb_const_undef(p->module, t); - } lbAddr result = lb_add_local_generated(p, t, true); @@ -3136,11 +3136,11 @@ gb_internal lbValue lb_build_expr_internal(lbProcedure *p, Ast *expr) { return lb_addr_load(p, lb_build_addr(p, expr)); case_end; - case_ast_node(u, Undef, expr) + case_ast_node(u, Uninit, expr) lbValue res = {}; if (is_type_untyped(type)) { res.value = nullptr; - res.type = t_untyped_undef; + res.type = t_untyped_uninit; } else { res.value = LLVMGetUndef(lb_type(m, type)); res.type = type; diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 7d2f574fe..628c1e5fa 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -677,7 +677,7 @@ gb_internal void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value) { return; } GB_ASSERT(value.type != nullptr); - if (is_type_untyped_undef(value.type)) { + if (is_type_untyped_uninit(value.type)) { Type *t = lb_addr_type(addr); value.type = t; value.value = LLVMGetUndef(lb_type(p->module, t)); @@ -1828,7 +1828,7 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { case Basic_UntypedString: GB_PANIC("Basic_UntypedString"); break; case Basic_UntypedRune: GB_PANIC("Basic_UntypedRune"); break; case Basic_UntypedNil: GB_PANIC("Basic_UntypedNil"); break; - case Basic_UntypedUndef: GB_PANIC("Basic_UntypedUndef"); break; + case Basic_UntypedUninit: GB_PANIC("Basic_UntypedUninit"); break; } break; case Type_Named: diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index ddf058668..b8353a466 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -3218,10 +3218,10 @@ gb_internal lbValue lb_build_call_expr_internal(lbProcedure *p, Ast *expr) { Entity *e = params->variables[i]; if (args[i].type == nullptr) { continue; + } else if (is_type_untyped_uninit(args[i].type)) { + args[i] = lb_const_undef(m, e->type); } else if (is_type_untyped_nil(args[i].type)) { args[i] = lb_const_nil(m, e->type); - } else if (is_type_untyped_undef(args[i].type)) { - args[i] = lb_const_undef(m, e->type); } } @@ -3409,10 +3409,10 @@ gb_internal lbValue lb_build_call_expr_internal(lbProcedure *p, Ast *expr) { Entity *e = param_tuple->variables[i]; if (args[i].type == nullptr) { continue; + } else if (is_type_untyped_uninit(args[i].type)) { + args[i] = lb_const_undef(m, e->type); } else if (is_type_untyped_nil(args[i].type)) { args[i] = lb_const_nil(m, e->type); - } else if (is_type_untyped_undef(args[i].type)) { - args[i] = lb_const_undef(m, e->type); } } } diff --git a/src/parser.cpp b/src/parser.cpp index 698ba99ab..bb492fca9 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -115,7 +115,7 @@ gb_internal Ast *clone_ast(Ast *node, AstFile *f) { n->Ident.entity = nullptr; break; case Ast_Implicit: break; - case Ast_Undef: break; + case Ast_Uninit: break; case Ast_BasicLit: break; case Ast_BasicDirective: break; @@ -646,9 +646,9 @@ gb_internal Ast *ast_implicit(AstFile *f, Token token) { result->Implicit = token; return result; } -gb_internal Ast *ast_undef(AstFile *f, Token token) { - Ast *result = alloc_ast_node(f, Ast_Undef); - result->Undef = token; +gb_internal Ast *ast_uninit(AstFile *f, Token token) { + Ast *result = alloc_ast_node(f, Ast_Uninit); + result->Uninit = token; return result; } @@ -2092,8 +2092,8 @@ gb_internal Ast *parse_operand(AstFile *f, bool lhs) { case Token_Ident: return parse_ident(f); - case Token_Undef: - return ast_undef(f, expect_token(f, Token_Undef)); + case Token_Uninit: + return ast_uninit(f, expect_token(f, Token_Uninit)); case Token_context: return ast_implicit(f, expect_token(f, Token_context)); @@ -2292,7 +2292,7 @@ gb_internal Ast *parse_operand(AstFile *f, bool lhs) { skip_possible_newline_for_literal(f); - if (allow_token(f, Token_Undef)) { + if (allow_token(f, Token_Uninit)) { if (where_token.kind != Token_Invalid) { syntax_error(where_token, "'where' clauses are not allowed on procedure literals without a defined body (replaced with ---)"); } diff --git a/src/parser.hpp b/src/parser.hpp index aea3bbf21..d4883f287 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -373,7 +373,7 @@ gb_global char const *union_type_kind_strings[UnionType_COUNT] = { Entity *entity; \ }) \ AST_KIND(Implicit, "implicit", Token) \ - AST_KIND(Undef, "undef", Token) \ + AST_KIND(Uninit, "uninitialized value", Token) \ AST_KIND(BasicLit, "basic literal", struct { \ Token token; \ }) \ diff --git a/src/parser_pos.cpp b/src/parser_pos.cpp index 1274f05a0..52d49e897 100644 --- a/src/parser_pos.cpp +++ b/src/parser_pos.cpp @@ -2,7 +2,7 @@ gb_internal Token ast_token(Ast *node) { switch (node->kind) { case Ast_Ident: return node->Ident.token; case Ast_Implicit: return node->Implicit; - case Ast_Undef: return node->Undef; + case Ast_Uninit: return node->Uninit; case Ast_BasicLit: return node->BasicLit.token; case Ast_BasicDirective: return node->BasicDirective.token; case Ast_ProcGroup: return node->ProcGroup.token; @@ -137,7 +137,7 @@ Token ast_end_token(Ast *node) { return empty_token; case Ast_Ident: return node->Ident.token; case Ast_Implicit: return node->Implicit; - case Ast_Undef: return node->Undef; + case Ast_Uninit: return node->Uninit; case Ast_BasicLit: return node->BasicLit.token; case Ast_BasicDirective: return node->BasicDirective.token; case Ast_ProcGroup: return node->ProcGroup.close; diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 547a864fb..17a396b9f 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -54,7 +54,7 @@ TOKEN_KIND(Token__AssignOpEnd, ""), \ TOKEN_KIND(Token_Increment, "++"), \ TOKEN_KIND(Token_Decrement, "--"), \ TOKEN_KIND(Token_ArrowRight,"->"), \ - TOKEN_KIND(Token_Undef, "---"), \ + TOKEN_KIND(Token_Uninit, "---"), \ \ TOKEN_KIND(Token__ComparisonBegin, ""), \ TOKEN_KIND(Token_CmpEq, "=="), \ @@ -917,7 +917,7 @@ gb_internal void tokenizer_get_token(Tokenizer *t, Token *token, int repeat=0) { token->kind = Token_Decrement; if (t->curr_rune == '-') { advance_to_next_rune(t); - token->kind = Token_Undef; + token->kind = Token_Uninit; } break; case '>': @@ -1078,7 +1078,7 @@ semicolon_check:; case Token_Imag: case Token_Rune: case Token_String: - case Token_Undef: + case Token_Uninit: /*fallthrough*/ case Token_Question: case Token_Pointer: diff --git a/src/types.cpp b/src/types.cpp index 70c4bcdbe..35fbb719b 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -83,7 +83,7 @@ enum BasicKind { Basic_UntypedString, Basic_UntypedRune, Basic_UntypedNil, - Basic_UntypedUndef, + Basic_UntypedUninit, Basic_COUNT, @@ -515,7 +515,7 @@ gb_global Type basic_types[] = { {Type_Basic, {Basic_UntypedString, BasicFlag_String | BasicFlag_Untyped, 0, STR_LIT("untyped string")}}, {Type_Basic, {Basic_UntypedRune, BasicFlag_Integer | BasicFlag_Untyped, 0, STR_LIT("untyped rune")}}, {Type_Basic, {Basic_UntypedNil, BasicFlag_Untyped, 0, STR_LIT("untyped nil")}}, - {Type_Basic, {Basic_UntypedUndef, BasicFlag_Untyped, 0, STR_LIT("untyped undefined")}}, + {Type_Basic, {Basic_UntypedUninit, BasicFlag_Untyped, 0, STR_LIT("untyped uninitialized")}}, }; // gb_global Type basic_type_aliases[] = { @@ -589,7 +589,7 @@ gb_global Type *t_untyped_quaternion = &basic_types[Basic_UntypedQuaternion]; gb_global Type *t_untyped_string = &basic_types[Basic_UntypedString]; gb_global Type *t_untyped_rune = &basic_types[Basic_UntypedRune]; gb_global Type *t_untyped_nil = &basic_types[Basic_UntypedNil]; -gb_global Type *t_untyped_undef = &basic_types[Basic_UntypedUndef]; +gb_global Type *t_untyped_uninit = &basic_types[Basic_UntypedUninit]; @@ -1866,14 +1866,15 @@ gb_internal bool is_type_typeid(Type *t) { } gb_internal bool is_type_untyped_nil(Type *t) { t = base_type(t); - return (t->kind == Type_Basic && t->Basic.kind == Basic_UntypedNil); + // NOTE(bill): checking for `nil` or `---` at once is just to improve the error handling + return (t->kind == Type_Basic && (t->Basic.kind == Basic_UntypedNil || t->Basic.kind == Basic_UntypedUninit)); } -gb_internal bool is_type_untyped_undef(Type *t) { +gb_internal bool is_type_untyped_uninit(Type *t) { t = base_type(t); - return (t->kind == Type_Basic && t->Basic.kind == Basic_UntypedUndef); + // NOTE(bill): checking for `nil` or `---` at once is just to improve the error handling + return (t->kind == Type_Basic && t->Basic.kind == Basic_UntypedUninit); } - gb_internal bool is_type_empty_union(Type *t) { t = base_type(t); return t->kind == Type_Union && t->Union.variants.count == 0; @@ -2206,10 +2207,6 @@ gb_internal bool is_type_polymorphic(Type *t, bool or_specialized=false) { } -gb_internal gb_inline bool type_has_undef(Type *t) { - return true; -} - gb_internal bool type_has_nil(Type *t) { t = base_type(t); switch (t->kind) { -- cgit v1.2.3 From 2bc5e0ebd71f1337cf5c3820cb4b623a29e90fbe Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 7 Jun 2023 00:10:39 +0100 Subject: Fix non-constant compound literals of slices --- src/llvm_backend_const.cpp | 22 ++++++++++++++++++++-- src/llvm_backend_expr.cpp | 5 +++-- src/llvm_backend_general.cpp | 26 +++++++++++++++++++------- src/llvm_backend_stmt.cpp | 1 + 4 files changed, 43 insertions(+), 11 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_backend_const.cpp b/src/llvm_backend_const.cpp index c8f1fea0f..c9d2f5b26 100644 --- a/src/llvm_backend_const.cpp +++ b/src/llvm_backend_const.cpp @@ -131,6 +131,25 @@ gb_internal lbValue lb_const_ptr_cast(lbModule *m, lbValue value, Type *t) { return res; } + +gb_internal LLVMValueRef llvm_const_string_internal(lbModule *m, Type *t, LLVMValueRef data, LLVMValueRef len) { + if (build_context.metrics.ptr_size < build_context.metrics.int_size) { + LLVMValueRef values[3] = { + data, + LLVMConstNull(lb_type(m, t_i32)), + len, + }; + return llvm_const_named_struct_internal(lb_type(m, t), values, 3); + } else { + LLVMValueRef values[2] = { + data, + len, + }; + return llvm_const_named_struct_internal(lb_type(m, t), values, 2); + } +} + + gb_internal LLVMValueRef llvm_const_named_struct(lbModule *m, Type *t, LLVMValueRef *values, isize value_count_) { LLVMTypeRef struct_type = lb_type(m, t); GB_ASSERT(LLVMGetTypeKind(struct_type) == LLVMStructTypeKind); @@ -659,10 +678,9 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bo ptr = LLVMConstNull(lb_type(m, t_u8_ptr)); } LLVMValueRef str_len = LLVMConstInt(lb_type(m, t_int), value.value_string.len, true); - LLVMValueRef values[2] = {ptr, str_len}; GB_ASSERT(is_type_string(original_type)); - res.value = llvm_const_named_struct(m, original_type, values, 2); + res.value = llvm_const_string_internal(m, original_type, ptr, str_len); } return res; diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index b2adc254d..f95e351ce 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -4230,11 +4230,12 @@ gb_internal lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) { lbValue count = {}; count.type = t_int; + unsigned len_index = lb_convert_struct_index(p->module, type, 1); if (lb_is_const(slice)) { - unsigned indices[1] = {1}; + unsigned indices[1] = {len_index}; count.value = LLVMConstExtractValue(slice.value, indices, gb_count_of(indices)); } else { - count.value = LLVMBuildExtractValue(p->builder, slice.value, 1, ""); + count.value = LLVMBuildExtractValue(p->builder, slice.value, len_index, ""); } lb_fill_slice(p, v, data, count); } diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 5cb339eb7..7f25d57b2 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -1,4 +1,5 @@ gb_internal void lb_add_debug_local_variable(lbProcedure *p, LLVMValueRef ptr, Type *type, Token const &token); +gb_internal LLVMValueRef llvm_const_string_internal(lbModule *m, Type *t, LLVMValueRef data, LLVMValueRef len); gb_global Entity *lb_global_type_info_data_entity = {}; gb_global lbAddr lb_global_type_info_member_types = {}; @@ -1776,11 +1777,23 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { return type; } type = LLVMStructCreateNamed(ctx, name); - LLVMTypeRef fields[2] = { - LLVMPointerType(lb_type(m, t_u8), 0), - lb_type(m, t_int), - }; - LLVMStructSetBody(type, fields, 2, false); + + if (build_context.metrics.ptr_size < build_context.metrics.int_size) { + GB_ASSERT(build_context.metrics.ptr_size == 4); + GB_ASSERT(build_context.metrics.int_size == 8); + LLVMTypeRef fields[3] = { + LLVMPointerType(lb_type(m, t_u8), 0), + lb_type(m, t_i32), + lb_type(m, t_int), + }; + LLVMStructSetBody(type, fields, 3, false); + } else { + LLVMTypeRef fields[2] = { + LLVMPointerType(lb_type(m, t_u8), 0), + lb_type(m, t_int), + }; + LLVMStructSetBody(type, fields, 2, false); + } return type; } case Basic_cstring: return LLVMPointerType(LLVMInt8TypeInContext(ctx), 0); @@ -2533,10 +2546,9 @@ gb_internal lbValue lb_find_or_add_entity_string(lbModule *m, String const &str) ptr = LLVMConstNull(lb_type(m, t_u8_ptr)); } LLVMValueRef str_len = LLVMConstInt(lb_type(m, t_int), str.len, true); - LLVMValueRef values[2] = {ptr, str_len}; lbValue res = {}; - res.value = llvm_const_named_struct(m, t_string, values, 2); + res.value = llvm_const_string_internal(m, t_string, ptr, str_len); res.type = t_string; return res; } diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 35fd2b7de..275d1f728 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -2471,6 +2471,7 @@ gb_internal void lb_build_stmt(lbProcedure *p, Ast *node) { } GB_ASSERT(lval_index == lvals.count); + for_array(i, vd->names) { Ast *name = vd->names[i]; if (!is_blank_ident(name) && !lvals_preused[i]) { -- cgit v1.2.3 From f622a8393cd3e288cefe2f5636b0aa1ab58fe0ca Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 7 Jun 2023 00:30:14 +0100 Subject: Change ABI for wasm64p32 on slices and structs --- src/llvm_abi.cpp | 58 ++++++++++++++++++++++++++++++++------------ src/llvm_backend_general.cpp | 2 +- 2 files changed, 43 insertions(+), 17 deletions(-) (limited to 'src/llvm_backend_general.cpp') diff --git a/src/llvm_abi.cpp b/src/llvm_abi.cpp index 79edbe83e..071f34627 100644 --- a/src/llvm_abi.cpp +++ b/src/llvm_abi.cpp @@ -326,7 +326,7 @@ gb_internal i64 lb_alignof(LLVMTypeRef type) { } -#define LB_ABI_INFO(name) lbFunctionType *name(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count, LLVMTypeRef return_type, bool return_is_defined, bool return_is_tuple, ProcCallingConvention calling_convention) +#define LB_ABI_INFO(name) lbFunctionType *name(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count, LLVMTypeRef return_type, bool return_is_defined, bool return_is_tuple, ProcCallingConvention calling_convention, Type *original_type) typedef LB_ABI_INFO(lbAbiInfoType); #define LB_ABI_COMPUTE_RETURN_TYPE(name) lbArgType name(lbFunctionType *ft, LLVMContextRef c, LLVMTypeRef return_type, bool return_is_defined, bool return_is_tuple) @@ -1217,7 +1217,7 @@ namespace lbAbiWasm { The approach taken optimizes for passing things in multiple registers/arguments if possible rather than by pointer. */ - gb_internal Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count, ProcCallingConvention calling_convention); + gb_internal Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count, ProcCallingConvention calling_convention, Type *original_type); gb_internal LB_ABI_COMPUTE_RETURN_TYPE(compute_return_type); enum {MAX_DIRECT_STRUCT_SIZE = 32}; @@ -1225,7 +1225,7 @@ namespace lbAbiWasm { gb_internal LB_ABI_INFO(abi_info) { lbFunctionType *ft = gb_alloc_item(permanent_allocator(), lbFunctionType); ft->ctx = c; - ft->args = compute_arg_types(c, arg_types, arg_count, calling_convention); + ft->args = compute_arg_types(c, arg_types, arg_count, calling_convention, original_type); ft->ret = compute_return_type(ft, c, return_type, return_is_defined, return_is_tuple); ft->calling_convention = calling_convention; return ft; @@ -1315,15 +1315,39 @@ namespace lbAbiWasm { return lb_arg_type_indirect(type, nullptr); } + gb_internal lbArgType pseudo_slice(LLVMContextRef c, LLVMTypeRef type, ProcCallingConvention calling_convention) { + if (build_context.metrics.ptr_size < build_context.metrics.int_size && + type_can_be_direct(type, calling_convention)) { + LLVMTypeRef types[2] = { + LLVMStructGetTypeAtIndex(type, 0), + // ignore padding + LLVMStructGetTypeAtIndex(type, 2) + }; + LLVMTypeRef new_type = LLVMStructTypeInContext(c, types, gb_count_of(types), false); + return lb_arg_type_direct(new_type, type, nullptr, nullptr); + } else { + return is_struct(c, type, calling_convention); + } + } - gb_internal Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count, ProcCallingConvention calling_convention) { + gb_internal Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count, ProcCallingConvention calling_convention, + Type *original_type) { auto args = array_make(lb_function_type_args_allocator(), arg_count); + GB_ASSERT(original_type->kind == Type_Proc); + GB_ASSERT(cast(isize)arg_count == original_type->Proc.param_count); + auto const ¶ms = original_type->Proc.params->Tuple.variables; + for (unsigned i = 0; i < arg_count; i++) { LLVMTypeRef t = arg_types[i]; LLVMTypeKind kind = LLVMGetTypeKind(t); if (kind == LLVMStructTypeKind || kind == LLVMArrayTypeKind) { - args[i] = is_struct(c, t, calling_convention); + Type *ptype = params[i]->type; + if (is_type_slice(ptype) || is_type_string(ptype)) { + args[i] = pseudo_slice(c, t, calling_convention); + } else { + args[i] = is_struct(c, t, calling_convention); + } } else { args[i] = non_struct(c, t, false); } @@ -1460,32 +1484,33 @@ gb_internal LB_ABI_INFO(lb_get_abi_info_internal) { } case ProcCC_Win64: GB_ASSERT(build_context.metrics.arch == TargetArch_amd64); - return lbAbiAmd64Win64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); + return lbAbiAmd64Win64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention, original_type); case ProcCC_SysV: GB_ASSERT(build_context.metrics.arch == TargetArch_amd64); - return lbAbiAmd64SysV::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); + return lbAbiAmd64SysV::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention, original_type); } switch (build_context.metrics.arch) { case TargetArch_amd64: if (build_context.metrics.os == TargetOs_windows) { - return lbAbiAmd64Win64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); + return lbAbiAmd64Win64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention, original_type); } else if (build_context.metrics.abi == TargetABI_Win64) { - return lbAbiAmd64Win64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); + return lbAbiAmd64Win64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention, original_type); } else if (build_context.metrics.abi == TargetABI_SysV) { - return lbAbiAmd64SysV::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); + return lbAbiAmd64SysV::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention, original_type); } else { - return lbAbiAmd64SysV::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); + return lbAbiAmd64SysV::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention, original_type); } case TargetArch_i386: - return lbAbi386::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); + return lbAbi386::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention, original_type); case TargetArch_arm32: - return lbAbiArm32::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); + return lbAbiArm32::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention, original_type); case TargetArch_arm64: - return lbAbiArm64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); + return lbAbiArm64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention, original_type); case TargetArch_wasm32: + return lbAbiWasm::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention, original_type); case TargetArch_wasm64p32: - return lbAbiWasm::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); + return lbAbiWasm::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention, original_type); } GB_PANIC("Unsupported ABI"); @@ -1499,7 +1524,8 @@ gb_internal LB_ABI_INFO(lb_get_abi_info) { arg_types, arg_count, return_type, return_is_defined, ALLOW_SPLIT_MULTI_RETURNS && return_is_tuple && is_calling_convention_odin(calling_convention), - calling_convention); + calling_convention, + base_type(original_type)); // NOTE(bill): this is handled here rather than when developing the type in `lb_type_internal_for_procedures_raw` diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 7f25d57b2..e5f3e3081 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -1580,7 +1580,7 @@ gb_internal LLVMTypeRef lb_type_internal_for_procedures_raw(lbModule *m, Type *t } } GB_ASSERT(param_index == param_count); - lbFunctionType *ft = lb_get_abi_info(m->ctx, params, param_count, ret, ret != nullptr, return_is_tuple, type->Proc.calling_convention); + lbFunctionType *ft = lb_get_abi_info(m->ctx, params, param_count, ret, ret != nullptr, return_is_tuple, type->Proc.calling_convention, type); { for_array(j, ft->args) { auto arg = ft->args[j]; -- 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_general.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 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_general.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