diff options
| author | gingerBill <bill@gingerbill.org> | 2023-09-22 14:39:51 +0100 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2023-09-22 14:39:51 +0100 |
| commit | 082324d7b97e521f5d8dada40629da4e689197c9 (patch) | |
| tree | 6a7e993cd7a7d9428732db3568ae755b73d428c9 /src | |
| parent | 5e99289d7afd34cf785b15f727ed2f2d61aa6ffc (diff) | |
Begin work on making the type info table be constantly initialized
Diffstat (limited to 'src')
| -rw-r--r-- | src/llvm_abi.cpp | 14 | ||||
| -rw-r--r-- | src/llvm_backend.cpp | 12 | ||||
| -rw-r--r-- | src/llvm_backend.hpp | 11 | ||||
| -rw-r--r-- | src/llvm_backend_const.cpp | 14 | ||||
| -rw-r--r-- | src/llvm_backend_general.cpp | 26 | ||||
| -rw-r--r-- | src/llvm_backend_opt.cpp | 2 | ||||
| -rw-r--r-- | src/llvm_backend_type.cpp | 920 | ||||
| -rw-r--r-- | src/llvm_backend_utility.cpp | 15 |
8 files changed, 960 insertions, 54 deletions
diff --git a/src/llvm_abi.cpp b/src/llvm_abi.cpp index b0045d869..857b255f3 100644 --- a/src/llvm_abi.cpp +++ b/src/llvm_abi.cpp @@ -1130,14 +1130,14 @@ namespace lbAbiArm64 { return non_struct(c, return_type); } else if (is_homogenous_aggregate(c, return_type, &homo_base_type, &homo_member_count)) { if (is_homogenous_aggregate_small_enough(homo_base_type, homo_member_count)) { - return lb_arg_type_direct(return_type, LLVMArrayType(homo_base_type, homo_member_count), nullptr, nullptr); + return lb_arg_type_direct(return_type, llvm_array_type(homo_base_type, homo_member_count), nullptr, nullptr); } else { //TODO(Platin): do i need to create stuff that can handle the diffrent return type? // else this needs a fix in llvm_backend_proc as we would need to cast it to the correct array type LB_ABI_MODIFY_RETURN_IF_TUPLE_MACRO(); - //LLVMTypeRef array_type = LLVMArrayType(homo_base_type, homo_member_count); + //LLVMTypeRef array_type = llvm_array_type(homo_base_type, homo_member_count); LLVMAttributeRef attr = lb_create_enum_attribute_with_type(c, "sret", return_type); return lb_arg_type_indirect(return_type, attr); } @@ -1155,7 +1155,7 @@ namespace lbAbiArm64 { cast_type = LLVMInt64TypeInContext(c); } else { unsigned count = cast(unsigned)((size+7)/8); - cast_type = LLVMArrayType(LLVMInt64TypeInContext(c), count); + cast_type = llvm_array_type(LLVMInt64TypeInContext(c), count); } return lb_arg_type_direct(return_type, cast_type, nullptr, nullptr); } else { @@ -1180,7 +1180,7 @@ namespace lbAbiArm64 { args[i] = non_struct(c, type); } else if (is_homogenous_aggregate(c, type, &homo_base_type, &homo_member_count)) { if (is_homogenous_aggregate_small_enough(homo_base_type, homo_member_count)) { - args[i] = lb_arg_type_direct(type, LLVMArrayType(homo_base_type, homo_member_count), nullptr, nullptr); + args[i] = lb_arg_type_direct(type, llvm_array_type(homo_base_type, homo_member_count), nullptr, nullptr); } else { args[i] = lb_arg_type_indirect(type, nullptr);; } @@ -1198,7 +1198,7 @@ namespace lbAbiArm64 { cast_type = LLVMIntTypeInContext(c, 64); } else { unsigned count = cast(unsigned)((size+7)/8); - cast_type = LLVMArrayType(LLVMIntTypeInContext(c, 64), count); + cast_type = llvm_array_type(LLVMIntTypeInContext(c, 64), count); } args[i] = lb_arg_type_direct(type, cast_type, nullptr, nullptr); } else { @@ -1439,10 +1439,10 @@ namespace lbAbiArm32 { args[i] = lb_arg_type_indirect(t, nullptr); } else if (a <= 4) { unsigned n = cast(unsigned)((sz + 3) / 4); - args[i] = lb_arg_type_direct(LLVMArrayType(LLVMIntTypeInContext(c, 32), n)); + args[i] = lb_arg_type_direct(llvm_array_type(LLVMIntTypeInContext(c, 32), n)); } else { unsigned n = cast(unsigned)((sz + 7) / 8); - args[i] = lb_arg_type_direct(LLVMArrayType(LLVMIntTypeInContext(c, 64), n)); + args[i] = lb_arg_type_direct(llvm_array_type(LLVMIntTypeInContext(c, 64), n)); } } } diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 439140901..db5af0647 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -2225,10 +2225,16 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { { // Add type info data isize max_type_info_count = info->minimum_dependency_type_info_set.count+1; - // gb_printf_err("max_type_info_count: %td\n", max_type_info_count); 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))); + + // IMPORTANT NOTE(bill): As LLVM does not have a union type, an array of unions cannot be initialized + // at compile time without cheating in some way. This means to emulate an array of unions is to use + // a giant packed struct of "corrected" data types. + + LLVMTypeRef internal_llvm_type = lb_setup_type_info_data_internal_type(m, max_type_info_count); + + LLVMValueRef g = LLVMAddGlobal(m->mod, internal_llvm_type, LB_TYPE_INFO_DATA_NAME); + LLVMSetInitializer(g, LLVMConstNull(internal_llvm_type)); LLVMSetLinkage(g, USE_SEPARATE_MODULES ? LLVMExternalLinkage : LLVMInternalLinkage); lbValue value = {}; diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 048233c08..13a250f00 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -451,6 +451,7 @@ gb_internal lbValue lb_emit_runtime_call(lbProcedure *p, char const *c_name, Arr gb_internal lbValue lb_emit_ptr_offset(lbProcedure *p, lbValue ptr, lbValue index); +gb_internal lbValue lb_const_ptr_offset(lbModule *m, 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); @@ -497,6 +498,7 @@ gb_internal lbValue lb_find_value_from_entity(lbModule *m, Entity *e); 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); +gb_internal lbValue lb_const_source_code_location_const(lbModule *m, String const &procedure, TokenPos const &pos); gb_internal lbValue lb_handle_param_value(lbProcedure *p, Type *parameter_type, ParameterValue const ¶m_value, TokenPos const &pos); @@ -553,6 +555,15 @@ gb_internal LLVMTypeRef OdinLLVMGetVectorElementType(LLVMTypeRef type); gb_internal String lb_filepath_ll_for_module(lbModule *m); + +gb_internal LLVMTypeRef llvm_array_type(LLVMTypeRef ElementType, uint64_t ElementCount) { +#if LB_USE_NEW_PASS_SYSTEM + return LLVMArrayType2(ElementType, ElementCount); +#else + return LLVMArrayType(ElementType, cast(unsigned)ElementCount); +#endif +} + #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 f3c3871ff..a16b2293c 100644 --- a/src/llvm_backend_const.cpp +++ b/src/llvm_backend_const.cpp @@ -284,14 +284,12 @@ gb_internal lbValue lb_expr_untyped_const_to_typed(lbModule *m, Ast *expr, Type return lb_const_value(m, t, tv.value); } -gb_internal lbValue lb_emit_source_code_location_const(lbProcedure *p, String const &procedure, TokenPos const &pos) { - lbModule *m = p->module; - +gb_internal lbValue lb_const_source_code_location_const(lbModule *m, String const &procedure, TokenPos const &pos) { LLVMValueRef fields[4] = {}; - fields[0]/*file*/ = lb_find_or_add_entity_string(p->module, get_file_path_string(pos.file_id)).value; + fields[0]/*file*/ = lb_find_or_add_entity_string(m, get_file_path_string(pos.file_id)).value; fields[1]/*line*/ = lb_const_int(m, t_i32, pos.line).value; fields[2]/*column*/ = lb_const_int(m, t_i32, pos.column).value; - fields[3]/*procedure*/ = lb_find_or_add_entity_string(p->module, procedure).value; + fields[3]/*procedure*/ = lb_find_or_add_entity_string(m, procedure).value; lbValue res = {}; res.value = llvm_const_named_struct(m, t_source_code_location, fields, gb_count_of(fields)); @@ -299,6 +297,12 @@ gb_internal lbValue lb_emit_source_code_location_const(lbProcedure *p, String co return res; } + +gb_internal lbValue lb_emit_source_code_location_const(lbProcedure *p, String const &procedure, TokenPos const &pos) { + lbModule *m = p->module; + return lb_const_source_code_location_const(m, procedure, pos); +} + gb_internal lbValue lb_emit_source_code_location_const(lbProcedure *p, Ast *node) { String proc_name = {}; if (p->entity) { diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index ccc0f2a54..0f0df7ca5 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -278,6 +278,13 @@ gb_internal lbValue lb_zero(lbModule *m, Type *t) { v.type = t; return v; } +gb_internal LLVMValueRef llvm_const_extract_value(lbModule *m, LLVMValueRef agg, unsigned index) { + LLVMValueRef res = agg; + GB_ASSERT(LLVMIsConstant(res)); + res = LLVMBuildExtractValue(m->const_dummy_builder, res, index, ""); + GB_ASSERT(LLVMIsConstant(res)); + return res; +} gb_internal LLVMValueRef llvm_const_extract_value(lbModule *m, LLVMValueRef agg, unsigned *indices, isize count) { // return LLVMConstExtractValue(value, indices, count); @@ -290,6 +297,19 @@ gb_internal LLVMValueRef llvm_const_extract_value(lbModule *m, LLVMValueRef agg, return res; } +gb_internal LLVMValueRef llvm_const_insert_value(lbModule *m, LLVMValueRef agg, LLVMValueRef val, unsigned index) { + GB_ASSERT(LLVMIsConstant(agg)); + GB_ASSERT(LLVMIsConstant(val)); + + LLVMValueRef extracted_value = val; + LLVMValueRef nested = llvm_const_extract_value(m, agg, index); + GB_ASSERT(LLVMIsConstant(nested)); + extracted_value = LLVMBuildInsertValue(m->const_dummy_builder, nested, extracted_value, index, ""); + GB_ASSERT(LLVMIsConstant(extracted_value)); + return extracted_value; +} + + gb_internal LLVMValueRef llvm_const_insert_value(lbModule *m, LLVMValueRef agg, LLVMValueRef val, unsigned *indices, isize count) { GB_ASSERT(LLVMIsConstant(agg)); GB_ASSERT(LLVMIsConstant(val)); @@ -1919,14 +1939,14 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { case Type_Array: { m->internal_type_level += 1; - LLVMTypeRef t = LLVMArrayType(lb_type(m, type->Array.elem), cast(unsigned)type->Array.count); + LLVMTypeRef t = llvm_array_type(lb_type(m, type->Array.elem), type->Array.count); m->internal_type_level -= 1; return t; } case Type_EnumeratedArray: { m->internal_type_level += 1; - LLVMTypeRef t = LLVMArrayType(lb_type(m, type->EnumeratedArray.elem), cast(unsigned)type->EnumeratedArray.count); + LLVMTypeRef t = llvm_array_type(lb_type(m, type->EnumeratedArray.elem), type->EnumeratedArray.count); m->internal_type_level -= 1; return t; } @@ -2160,7 +2180,7 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { m->internal_type_level -= 1; LLVMTypeRef elem = lb_type(m, type->Matrix.elem); - LLVMTypeRef t = LLVMArrayType(elem, cast(unsigned)elem_count); + LLVMTypeRef t = llvm_array_type(elem, elem_count); m->internal_type_level += 1; return t; diff --git a/src/llvm_backend_opt.cpp b/src/llvm_backend_opt.cpp index 055cb5bcb..4424bde7c 100644 --- a/src/llvm_backend_opt.cpp +++ b/src/llvm_backend_opt.cpp @@ -443,7 +443,7 @@ gb_internal void lb_append_to_compiler_used(lbModule *m, LLVMValueRef func) { } LLVMTypeRef Int8PtrTy = LLVMPointerType(LLVMInt8TypeInContext(m->ctx), 0); - LLVMTypeRef ATy = LLVMArrayType(Int8PtrTy, operands); + LLVMTypeRef ATy = llvm_array_type(Int8PtrTy, operands); constants[operands - 1] = LLVMConstBitCast(func, Int8PtrTy); LLVMValueRef initializer = LLVMConstArray(Int8PtrTy, constants, operands); diff --git a/src/llvm_backend_type.cpp b/src/llvm_backend_type.cpp index 62a67f96a..3654e8005 100644 --- a/src/llvm_backend_type.cpp +++ b/src/llvm_backend_type.cpp @@ -14,18 +14,16 @@ gb_internal isize lb_type_info_index(CheckerInfo *info, Type *type, bool err_on_ return -1; } -gb_internal lbValue lb_typeid(lbModule *m, Type *type) { +gb_internal u64 lb_typeid_kind(lbModule *m, Type *type, u64 id=0) { GB_ASSERT(!build_context.no_rtti); type = default_type(type); - u64 id = cast(u64)lb_type_info_index(m->info, type); - GB_ASSERT(id >= 0); + if (id == 0) { + id = cast(u64)lb_type_info_index(m->info, type); + } u64 kind = Typeid_Invalid; - u64 named = is_type_named(type) && type->kind != Type_Basic; - u64 special = 0; - u64 reserved = 0; Type *bt = base_type(type); TypeKind tk = bt->kind; @@ -61,6 +59,22 @@ gb_internal lbValue lb_typeid(lbModule *m, Type *type) { case Type_SoaPointer: kind = Typeid_SoaPointer; break; } + return kind; +} + +gb_internal lbValue lb_typeid(lbModule *m, Type *type) { + GB_ASSERT(!build_context.no_rtti); + + type = default_type(type); + + u64 id = cast(u64)lb_type_info_index(m->info, type); + GB_ASSERT(id >= 0); + + u64 kind = lb_typeid_kind(m, type, id); + u64 named = is_type_named(type) && type->kind != Type_Basic; + u64 special = 0; + u64 reserved = 0; + if (is_type_cstring(type)) { special = 1; } else if (is_type_integer(type) && !is_type_unsigned(type)) { @@ -107,38 +121,874 @@ gb_internal LLVMTypeRef lb_get_procedure_raw_type(lbModule *m, Type *type) { return lb_type_internal_for_procedures_raw(m, type); } +gb_internal lbValue lb_const_array_epi(lbModule *m, lbValue value, isize index) { + GB_ASSERT(is_type_pointer(value.type)); + Type *type = type_deref(value.type); + + LLVMValueRef indices[2] = { + LLVMConstInt(lb_type(m, t_int), 0, false), + LLVMConstInt(lb_type(m, t_int), cast(unsigned long long)index, false), + }; + LLVMTypeRef llvm_type = lb_type(m, type); + lbValue res = {}; + Type *ptr = base_array_type(type); + res.type = alloc_type_pointer(ptr); + GB_ASSERT(LLVMIsConstant(value.value)); + res.value = LLVMConstGEP2(llvm_type, value.value, indices, gb_count_of(indices)); + return res; +} + -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); +gb_internal lbValue lb_type_info_member_types_offset(lbModule *m, isize count) { + GB_ASSERT(m == &m->gen->default_module); + lbValue offset = lb_const_array_epi(m, 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; } -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); +gb_internal lbValue lb_type_info_member_names_offset(lbModule *m, isize count) { + GB_ASSERT(m == &m->gen->default_module); + lbValue offset = lb_const_array_epi(m, 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; } -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); +gb_internal lbValue lb_type_info_member_offsets_offset(lbModule *m, isize count) { + GB_ASSERT(m == &m->gen->default_module); + lbValue offset = lb_const_array_epi(m, 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; } -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); +gb_internal lbValue lb_type_info_member_usings_offset(lbModule *m, isize count) { + GB_ASSERT(m == &m->gen->default_module); + lbValue offset = lb_const_array_epi(m, 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; } -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); +gb_internal lbValue lb_type_info_member_tags_offset(lbModule *m, isize count) { + GB_ASSERT(m == &m->gen->default_module); + lbValue offset = lb_const_array_epi(m, lb_global_type_info_member_tags.addr, lb_global_type_info_member_tags_index); lb_global_type_info_member_tags_index += cast(i32)count; return offset; } +enum {LB_USE_GIANT_PACKED_STRUCT = false}; + +gb_internal LLVMTypeRef lb_setup_type_info_data_internal_type(lbModule *m, isize max_type_info_count) { + if (!LB_USE_GIANT_PACKED_STRUCT) { + Type *t = alloc_type_array(t_type_info, max_type_info_count); + return lb_type(m, t); + } + CheckerInfo *info = m->gen->info; + + LLVMTypeRef *element_types = gb_alloc_array(heap_allocator(), LLVMTypeRef, max_type_info_count); + defer (gb_free(heap_allocator(), element_types)); + + auto entries_handled = slice_make<bool>(heap_allocator(), max_type_info_count); + defer (gb_free(heap_allocator(), entries_handled.data)); + entries_handled[0] = true; + + element_types[0] = lb_type(m, t_type_info); + + Type *tibt = base_type(t_type_info); + GB_ASSERT(tibt->kind == Type_Struct); + Type *ut = base_type(tibt->Struct.fields[tibt->Struct.fields.count-1]->type); + GB_ASSERT(ut->kind == Type_Union); + + GB_ASSERT(tibt->Struct.fields.count == 5); + LLVMTypeRef stypes[6] = {}; + stypes[0] = lb_type(m, tibt->Struct.fields[0]->type); + stypes[1] = lb_type(m, tibt->Struct.fields[1]->type); + stypes[2] = lb_type(m, tibt->Struct.fields[2]->type); + isize variant_index = 0; + if (build_context.int_size == 8) { + stypes[3] = lb_type(m, t_i32); // padding + stypes[4] = lb_type(m, tibt->Struct.fields[3]->type); + variant_index = 5; + } else { + stypes[3] = lb_type(m, tibt->Struct.fields[3]->type); + variant_index = 4; + } + + LLVMTypeRef modified_types[32] = {}; + GB_ASSERT(gb_count_of(modified_types) >= ut->Union.variants.count); + modified_types[0] = element_types[0]; + + i64 tag_offset = ut->Union.variant_block_size; + LLVMTypeRef tag = lb_type(m, union_tag_type(ut)); + + for_array(i, ut->Union.variants) { + Type *t = ut->Union.variants[i]; + LLVMTypeRef padding = llvm_array_type(lb_type(m, t_u8), tag_offset-type_size_of(t)); + + LLVMTypeRef vtypes[3] = {}; + vtypes[0] = lb_type(m, t); + vtypes[1] = padding; + vtypes[2] = tag; + LLVMTypeRef variant_type = LLVMStructType(vtypes, gb_count_of(vtypes), true); + + stypes[variant_index] = variant_type; + LLVMTypeRef modified_type = LLVMStructType(stypes, cast(unsigned)(variant_index+1), false); + + modified_types[i] = modified_type; + } + + for_array(type_info_type_index, info->type_info_types) { + Type *t = info->type_info_types[type_info_type_index]; + if (t == nullptr || t == t_invalid) { + continue; + } + isize entry_index = lb_type_info_index(info, t, false); + if (entry_index <= 0) { + continue; + } + + if (entries_handled[entry_index]) { + continue; + } + entries_handled[entry_index] = true; + + + if (t->kind == Type_Named) { + element_types[entry_index] = modified_types[0]; + } else { + i64 variant_index = lb_typeid_kind(m, t); + element_types[entry_index] = modified_types[variant_index]; + } + + GB_ASSERT(element_types[entry_index] != nullptr); + } + + for_array(i, entries_handled) { + GB_ASSERT(entries_handled[i]); + } + + return LLVMStructType(element_types, cast(unsigned)max_type_info_count, true); +} + + +gb_internal void lb_setup_type_info_data_giant_packed_struct(lbModule *m, i64 global_type_info_data_entity_count) { // NOTE(bill): Setup type_info data + CheckerInfo *info = m->info; + + // Useful types + Entity *type_info_flags_entity = find_core_entity(info->checker, str_lit("Type_Info_Flags")); + Type *t_type_info_flags = type_info_flags_entity->type; + gb_unused(t_type_info_flags); + + Type *ut = base_type(t_type_info); + GB_ASSERT(ut->kind == Type_Struct); + ut = base_type(ut->Struct.fields[ut->Struct.fields.count-1]->type); + GB_ASSERT(ut->kind == Type_Union); + + auto entries_handled = slice_make<bool>(heap_allocator(), cast(isize)global_type_info_data_entity_count); + defer (gb_free(heap_allocator(), entries_handled.data)); + entries_handled[0] = true; + + + LLVMValueRef giant_struct = lb_global_type_info_data_ptr(m).value; + LLVMTypeRef giant_struct_type = LLVMGlobalGetValueType(giant_struct); + GB_ASSERT(LLVMGetTypeKind(giant_struct_type) == LLVMStructTypeKind); + + LLVMValueRef *giant_const_values = gb_alloc_array(heap_allocator(), LLVMValueRef, global_type_info_data_entity_count); + defer (gb_free(heap_allocator(), giant_const_values)); + + giant_const_values[0] = LLVMConstNull(LLVMStructGetTypeAtIndex(giant_struct_type, 0)); + + + LLVMValueRef *small_const_values = gb_alloc_array(heap_allocator(), LLVMValueRef, 6); + defer (gb_free(heap_allocator(), small_const_values)); + + for_array(type_info_type_index, info->type_info_types) { + Type *t = info->type_info_types[type_info_type_index]; + if (t == nullptr || t == t_invalid) { + continue; + } + + isize entry_index = lb_type_info_index(info, t, false); + if (entry_index <= 0) { + continue; + } + + if (entries_handled[entry_index]) { + continue; + } + entries_handled[entry_index] = true; + + + LLVMTypeRef stype = LLVMStructGetTypeAtIndex(giant_struct_type, cast(unsigned)entry_index); + + i64 size = type_size_of(t); + i64 align = type_align_of(t); + u32 flags = type_info_flags_of_type(t); + lbValue id = lb_typeid(m, t); + GB_ASSERT_MSG(align != 0, "%lld %s", align, type_to_string(t)); + + lbValue type_info_flags = lb_const_int(m, t_type_info_flags, flags); + + small_const_values[0] = LLVMConstInt(lb_type(m, t_int), size, true); + small_const_values[1] = LLVMConstInt(lb_type(m, t_int), align, true); + small_const_values[2] = type_info_flags.value; + + unsigned variant_index = 0; + if (build_context.int_size == 8) { + small_const_values[3] = LLVMConstNull(LLVMStructGetTypeAtIndex(stype, 3)); + small_const_values[4] = id.value; + variant_index = 5; + } else { + small_const_values[3] = id.value; + variant_index = 4; + } + + LLVMTypeRef full_variant_type = LLVMStructGetTypeAtIndex(stype, variant_index); + unsigned full_variant_elem_count = LLVMCountStructElementTypes(full_variant_type); + if (full_variant_elem_count != 2) { + GB_ASSERT_MSG(LLVMCountStructElementTypes(full_variant_type) == 3, "%lld %s", entry_index, type_to_string(t)); // blob, padding, tag + } + + LLVMValueRef variant_value = nullptr; + Type *tag_type = nullptr; + + switch (t->kind) { + case Type_Named: { + tag_type = t_type_info_named; + + LLVMValueRef pkg_name = nullptr; + if (t->Named.type_name->pkg) { + pkg_name = lb_const_string(m, t->Named.type_name->pkg->name).value; + } else { + pkg_name = LLVMConstNull(lb_type(m, t_string)); + } + + String proc_name = {}; + if (t->Named.type_name->parent_proc_decl) { + DeclInfo *decl = t->Named.type_name->parent_proc_decl; + if (decl->entity && decl->entity->kind == Entity_Procedure) { + proc_name = decl->entity->token.string; + } + } + TokenPos pos = t->Named.type_name->token.pos; + + lbValue loc = lb_const_source_code_location_const(m, proc_name, pos); + + LLVMValueRef vals[4] = { + lb_const_string(m, t->Named.type_name->token.string).value, + lb_type_info(m, t->Named.base).value, + pkg_name, + loc.value + }; + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + break; + } + + case Type_Basic: + switch (t->Basic.kind) { + case Basic_bool: + case Basic_b8: + case Basic_b16: + case Basic_b32: + case Basic_b64: + tag_type = t_type_info_boolean; + break; + + case Basic_i8: + case Basic_u8: + case Basic_i16: + case Basic_u16: + case Basic_i32: + case Basic_u32: + case Basic_i64: + case Basic_u64: + case Basic_i128: + case Basic_u128: + + case Basic_i16le: + case Basic_u16le: + case Basic_i32le: + case Basic_u32le: + case Basic_i64le: + case Basic_u64le: + case Basic_i128le: + case Basic_u128le: + case Basic_i16be: + case Basic_u16be: + case Basic_i32be: + case Basic_u32be: + case Basic_i64be: + case Basic_u64be: + case Basic_i128be: + case Basic_u128be: + + case Basic_int: + case Basic_uint: + case Basic_uintptr: { + tag_type = t_type_info_integer; + + lbValue is_signed = lb_const_bool(m, t_bool, (t->Basic.flags & BasicFlag_Unsigned) == 0); + // NOTE(bill): This is matches the runtime layout + u8 endianness_value = 0; + if (t->Basic.flags & BasicFlag_EndianLittle) { + endianness_value = 1; + } else if (t->Basic.flags & BasicFlag_EndianBig) { + endianness_value = 2; + } + lbValue endianness = lb_const_int(m, t_u8, endianness_value); + + LLVMValueRef vals[2] = { + is_signed.value, + endianness.value, + }; + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + break; + } + + case Basic_rune: + tag_type = t_type_info_rune; + break; + + case Basic_f16: + case Basic_f32: + case Basic_f64: + case Basic_f16le: + case Basic_f32le: + case Basic_f64le: + case Basic_f16be: + case Basic_f32be: + case Basic_f64be: + { + tag_type = t_type_info_float; + + // NOTE(bill): This is matches the runtime layout + u8 endianness_value = 0; + if (t->Basic.flags & BasicFlag_EndianLittle) { + endianness_value = 1; + } else if (t->Basic.flags & BasicFlag_EndianBig) { + endianness_value = 2; + } + lbValue endianness = lb_const_int(m, t_u8, endianness_value); + + LLVMValueRef vals[1] = { + endianness.value, + }; + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + } + break; + + case Basic_complex32: + case Basic_complex64: + case Basic_complex128: + tag_type = t_type_info_complex; + break; + + case Basic_quaternion64: + case Basic_quaternion128: + case Basic_quaternion256: + tag_type = t_type_info_quaternion; + break; + + case Basic_rawptr: + tag_type = t_type_info_pointer; + break; + + case Basic_string: + tag_type = t_type_info_string; + break; + + case Basic_cstring: + { + tag_type = t_type_info_string; + LLVMValueRef vals[1] = { + lb_const_bool(m, t_bool, true).value, + }; + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + } + break; + + case Basic_any: + tag_type = t_type_info_any; + break; + + case Basic_typeid: + tag_type = t_type_info_typeid; + break; + } + break; + + case Type_Pointer: { + tag_type = t_type_info_pointer; + lbValue gep = lb_type_info(m, t->Pointer.elem); + + LLVMValueRef vals[1] = { + gep.value, + }; + + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + break; + } + case Type_MultiPointer: { + tag_type = t_type_info_multi_pointer; + lbValue gep = lb_type_info(m, t->MultiPointer.elem); + + LLVMValueRef vals[1] = { + gep.value, + }; + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + break; + } + case Type_SoaPointer: { + tag_type = t_type_info_soa_pointer; + lbValue gep = lb_type_info(m, t->SoaPointer.elem); + + LLVMValueRef vals[1] = { + gep.value, + }; + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + break; + } + case Type_Array: { + tag_type = t_type_info_array; + i64 ez = type_size_of(t->Array.elem); + + LLVMValueRef vals[3] = { + lb_type_info(m, t->Array.elem).value, + lb_const_int(m, t_int, ez).value, + lb_const_int(m, t_int, t->Array.count).value, + }; + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + break; + } + case Type_EnumeratedArray: { + tag_type = t_type_info_enumerated_array; + + LLVMValueRef vals[7] = { + lb_type_info(m, t->EnumeratedArray.elem).value, + lb_type_info(m, t->EnumeratedArray.index).value, + lb_const_int(m, t_int, type_size_of(t->EnumeratedArray.elem)).value, + lb_const_int(m, t_int, t->EnumeratedArray.count).value, + + // Unions + lb_const_value(m, t_type_info_enum_value, *t->EnumeratedArray.min_value).value, + lb_const_value(m, t_type_info_enum_value, *t->EnumeratedArray.max_value).value, + + lb_const_bool(m, t_bool, t->EnumeratedArray.is_sparse).value, + }; + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + break; + } + case Type_DynamicArray: { + tag_type = t_type_info_dynamic_array; + + LLVMValueRef vals[2] = { + lb_type_info(m, t->DynamicArray.elem).value, + lb_const_int(m, t_int, type_size_of(t->DynamicArray.elem)).value, + }; + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + break; + } + case Type_Slice: { + tag_type = t_type_info_slice; + + LLVMValueRef vals[2] = { + lb_type_info(m, t->Slice.elem).value, + lb_const_int(m, t_int, type_size_of(t->Slice.elem)).value, + }; + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + break; + } + case Type_Proc: { + tag_type = t_type_info_procedure; + + LLVMValueRef params = LLVMConstNull(lb_type(m, t_type_info_ptr)); + LLVMValueRef results = LLVMConstNull(lb_type(m, t_type_info_ptr)); + if (t->Proc.params != nullptr) { + params = lb_type_info(m, t->Proc.params).value; + } + if (t->Proc.results != nullptr) { + results = lb_type_info(m, t->Proc.results).value; + } + + LLVMValueRef vals[4] = { + params, + results, + lb_const_bool(m, t_bool, t->Proc.variadic).value, + lb_const_int(m, t_u8, t->Proc.calling_convention).value, + }; + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + break; + } + case Type_Tuple: { + tag_type = t_type_info_parameters; + // TODO(bill) + // lbValue memory_types = lb_type_info_member_types_offset(m, t->Tuple.variables.count); + // lbValue memory_names = lb_type_info_member_names_offset(m, t->Tuple.variables.count); + + + // for_array(i, t->Tuple.variables) { + // // NOTE(bill): offset is not used for tuples + // Entity *f = t->Tuple.variables[i]; + + // lbValue index = lb_const_int(m, t_int, i); + // lbValue type_info = lb_const_ptr_offset(m, memory_types, index); + + // // TODO(bill): Make this constant if possible, 'lb_const_store' does not work + // lb_emit_store(p, type_info, lb_type_info(m, f->type)); + // if (f->token.string.len > 0) { + // lbValue name = lb_const_ptr_offset(m, memory_names, index); + // lb_emit_store(p, name, lb_const_string(m, f->token.string)); + // } + // } + + // lbValue count = lb_const_int(m, t_int, t->Tuple.variables.count); + + // LLVMValueRef types_slice = llvm_const_slice(m, memory_types, count); + // LLVMValueRef names_slice = llvm_const_slice(m, memory_names, count); + + // LLVMValueRef vals[2] = { + // types_slice, + // names_slice, + // }; + + // variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + break; + } + + case Type_Enum: + tag_type = t_type_info_enum; + + { + GB_ASSERT(t->Enum.base_type != nullptr); + // GB_ASSERT_MSG(type_size_of(t_type_info_enum_value) == 16, "%lld == 16", cast(long long)type_size_of(t_type_info_enum_value)); + + + LLVMValueRef vals[3] = {}; + vals[0] = lb_type_info(m, t->Enum.base_type).value; + if (t->Enum.fields.count > 0) { + auto fields = t->Enum.fields; + lbValue name_array = lb_generate_global_array(m, t_string, fields.count, + str_lit("$enum_names"), cast(i64)entry_index); + lbValue value_array = lb_generate_global_array(m, t_type_info_enum_value, fields.count, + str_lit("$enum_values"), cast(i64)entry_index); + + + LLVMValueRef *name_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, fields.count); + LLVMValueRef *value_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, fields.count); + + GB_ASSERT(is_type_integer(t->Enum.base_type)); + + for_array(i, fields) { + name_values[i] = lb_const_string(m, fields[i]->token.string).value; + value_values[i] = lb_const_value(m, t_i64, fields[i]->Constant.value).value; + } + + LLVMValueRef name_init = llvm_const_array(lb_type(m, t_string), name_values, cast(unsigned)fields.count); + LLVMValueRef value_init = llvm_const_array(lb_type(m, t_type_info_enum_value), value_values, cast(unsigned)fields.count); + LLVMSetInitializer(name_array.value, name_init); + LLVMSetInitializer(value_array.value, value_init); + LLVMSetGlobalConstant(name_array.value, true); + LLVMSetGlobalConstant(value_array.value, true); + + lbValue v_count = lb_const_int(m, t_int, fields.count); + + vals[1] = llvm_const_slice(m, lbValue{name_array.value, alloc_type_pointer(t_string)}, v_count); + vals[2] = llvm_const_slice(m, lbValue{value_array.value, alloc_type_pointer(t_type_info_enum_value)}, v_count); + } else { + vals[1] = LLVMConstNull(lb_type(m, base_type(t_type_info_enum)->Struct.fields[1]->type)); + vals[2] = LLVMConstNull(lb_type(m, base_type(t_type_info_enum)->Struct.fields[2]->type)); + } + + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + } + break; + + case Type_Union: { + tag_type = t_type_info_union; + + { + LLVMValueRef vals[7] = {}; + + isize variant_count = gb_max(0, t->Union.variants.count); + lbValue memory_types = lb_type_info_member_types_offset(m, variant_count); + + for (isize variant_index = 0; variant_index < variant_count; variant_index++) { + // TODO(bill) + // Type *vt = t->Union.variants[variant_index]; + // lbValue tip = lb_type_info(m, vt); + + // lbValue index = lb_const_int(m, t_int, variant_index); + // lbValue type_info = lb_const_ptr_offset(m, memory_types, index); + // lb_emit_store(p, type_info, lb_type_info(m, vt)); + } + + lbValue count = lb_const_int(m, t_int, variant_count); + vals[0] = llvm_const_slice(m, memory_types, count); + + i64 tag_size = union_tag_size(t); + if (tag_size > 0) { + i64 tag_offset = align_formula(t->Union.variant_block_size, tag_size); + vals[1] = lb_const_int(m, t_uintptr, tag_offset).value; + vals[2] = lb_type_info(m, union_tag_type(t)).value; + } else { + vals[1] = lb_const_int(m, t_uintptr, 0).value; + vals[2] = LLVMConstNull(lb_type(m, t_type_info_ptr)); + } + + if (is_type_comparable(t) && !is_type_simple_compare(t)) { + vals[3] = lb_equal_proc_for_type(m, t).value; + } + + vals[4] = lb_const_bool(m, t_bool, t->Union.custom_align != 0).value; + vals[5] = lb_const_bool(m, t_bool, t->Union.kind == UnionType_no_nil).value; + vals[6] = lb_const_bool(m, t_bool, t->Union.kind == UnionType_shared_nil).value; + + for (isize i = 0; i < gb_count_of(vals); i++) { + if (vals[i] == nullptr) { + vals[i] = LLVMConstNull(lb_type(m, get_struct_field_type(tag_type, i))); + } + } + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + } + + break; + } + + case Type_Struct: { + tag_type = t_type_info_struct; + + LLVMValueRef vals[13] = {}; + + { + lbValue is_packed = lb_const_bool(m, t_bool, t->Struct.is_packed); + lbValue is_raw_union = lb_const_bool(m, t_bool, t->Struct.is_raw_union); + lbValue is_no_copy = lb_const_bool(m, t_bool, t->Struct.is_no_copy); + lbValue is_custom_align = lb_const_bool(m, t_bool, t->Struct.custom_align != 0); + vals[5] = is_packed.value; + vals[6] = is_raw_union.value; + vals[7] = is_no_copy.value; + vals[8] = is_custom_align.value; + if (is_type_comparable(t) && !is_type_simple_compare(t)) { + vals[9] = lb_equal_proc_for_type(m, t).value; + } + + + if (t->Struct.soa_kind != StructSoa_None) { + Type *kind_type = get_struct_field_type(tag_type, 10); + + lbValue soa_kind = lb_const_value(m, kind_type, exact_value_i64(t->Struct.soa_kind)); + lbValue soa_type = lb_type_info(m, t->Struct.soa_elem); + lbValue soa_len = lb_const_int(m, t_int, t->Struct.soa_count); + + vals[10] = soa_kind.value; + vals[11] = soa_type.value; + vals[12] = soa_len.value; + } + } + + isize count = t->Struct.fields.count; + if (count > 0) { + lbValue memory_types = lb_type_info_member_types_offset (m, count); + lbValue memory_names = lb_type_info_member_names_offset (m, count); + lbValue memory_offsets = lb_type_info_member_offsets_offset(m, count); + lbValue memory_usings = lb_type_info_member_usings_offset (m, count); + lbValue memory_tags = lb_type_info_member_tags_offset (m, count); + + type_set_offsets(t); // NOTE(bill): Just incase the offsets have not been set yet + for (isize source_index = 0; source_index < count; source_index++) { + Entity *f = t->Struct.fields[source_index]; + lbValue tip = lb_type_info(m, f->type); + i64 foffset = 0; + if (!t->Struct.is_raw_union) { + GB_ASSERT(t->Struct.offsets != nullptr); + GB_ASSERT(0 <= f->Variable.field_index && f->Variable.field_index < count); + foffset = t->Struct.offsets[source_index]; + } + GB_ASSERT(f->kind == Entity_Variable && f->flags & EntityFlag_Field); + + lbValue index = lb_const_int(m, t_int, source_index); + lbValue type_info = lb_const_ptr_offset(m, memory_types, index); + lbValue offset = lb_const_ptr_offset(m, memory_offsets, index); + lbValue is_using = lb_const_ptr_offset(m, memory_usings, index); + + // TODO(bill) + // lb_emit_store(p, type_info, lb_type_info(m, f->type)); + // if (f->token.string.len > 0) { + // lbValue name = lb_emit_ptr_offset(p, memory_names, index); + // lb_emit_store(p, name, lb_const_string(m, f->token.string)); + // } + // lb_emit_store(p, offset, lb_const_int(m, t_uintptr, foffset)); + // lb_emit_store(p, is_using, lb_const_bool(m, t_bool, (f->flags&EntityFlag_Using) != 0)); + + // if (t->Struct.tags != nullptr) { + // String tag_string = t->Struct.tags[source_index]; + // if (tag_string.len > 0) { + // lbValue tag_ptr = lb_emit_ptr_offset(p, memory_tags, index); + // lb_emit_store(p, tag_ptr, lb_const_string(m, tag_string)); + // } + // } + + } + + lbValue cv = lb_const_int(m, t_int, count); + vals[0] = llvm_const_slice(m, memory_types, cv); + vals[1] = llvm_const_slice(m, memory_names, cv); + vals[2] = llvm_const_slice(m, memory_offsets, cv); + vals[3] = llvm_const_slice(m, memory_usings, cv); + vals[4] = llvm_const_slice(m, memory_tags, cv); + } + for (isize i = 0; i < gb_count_of(vals); i++) { + if (vals[i] == nullptr) { + vals[i] = LLVMConstNull(lb_type(m, get_struct_field_type(tag_type, i))); + } + } + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + break; + } + + case Type_Map: { + tag_type = t_type_info_map; + init_map_internal_types(t); + + LLVMValueRef vals[3] = { + lb_type_info(m, t->Map.key).value, + lb_type_info(m, t->Map.value).value, + lb_gen_map_info_ptr(m, t).value + }; + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + break; + } + + case Type_BitSet: + { + tag_type = t_type_info_bit_set; + + GB_ASSERT(is_type_typed(t->BitSet.elem)); + + + LLVMValueRef vals[4] = { + lb_type_info(m, t->BitSet.elem).value, + LLVMConstNull(lb_type(m, t_type_info_ptr)), + lb_const_int(m, t_i64, t->BitSet.lower).value, + lb_const_int(m, t_i64, t->BitSet.upper).value, + }; + if (t->BitSet.underlying != nullptr) { + vals[1] = lb_type_info(m, t->BitSet.underlying).value; + } + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + } + break; + + case Type_SimdVector: + { + tag_type = t_type_info_simd_vector; + + LLVMValueRef vals[3] = {}; + + vals[0] = lb_type_info(m, t->SimdVector.elem).value; + vals[1] = lb_const_int(m, t_int, type_size_of(t->SimdVector.elem)).value; + vals[2] = lb_const_int(m, t_int, t->SimdVector.count).value; + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + } + break; + + case Type_RelativePointer: + { + tag_type = t_type_info_relative_pointer; + LLVMValueRef vals[2] = { + lb_type_info(m, t->RelativePointer.pointer_type).value, + lb_type_info(m, t->RelativePointer.base_integer).value, + }; + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + } + break; + + case Type_RelativeMultiPointer: + { + tag_type = t_type_info_relative_multi_pointer; + LLVMValueRef vals[2] = { + lb_type_info(m, t->RelativeMultiPointer.pointer_type).value, + lb_type_info(m, t->RelativeMultiPointer.base_integer).value, + }; + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + } + break; + + case Type_Matrix: + { + tag_type = t_type_info_matrix; + i64 ez = type_size_of(t->Matrix.elem); + + LLVMValueRef vals[5] = { + lb_type_info(m, t->Matrix.elem).value, + lb_const_int(m, t_int, ez).value, + lb_const_int(m, t_int, matrix_type_stride_in_elems(t)).value, + lb_const_int(m, t_int, t->Matrix.row_count).value, + lb_const_int(m, t_int, t->Matrix.column_count).value, + }; + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + } + break; + } + + + + i64 tag_index = 0; + if (tag_type != nullptr) { + tag_index = union_variant_index(ut, tag_type); + } + + LLVMValueRef full_variant_values[3] = {}; + + if (full_variant_elem_count == 2) { + if (variant_value == nullptr) { + full_variant_values[0] = LLVMConstNull(LLVMStructGetTypeAtIndex(full_variant_type, 0)); + full_variant_values[1] = LLVMConstInt(LLVMStructGetTypeAtIndex(full_variant_type, 1), tag_index, false); + } else { + full_variant_values[0] = variant_value; + full_variant_values[1] = LLVMConstInt(LLVMStructGetTypeAtIndex(full_variant_type, 1), tag_index, false); + } + } else { + if (variant_value == nullptr) { + variant_value = LLVMConstNull(LLVMStructGetTypeAtIndex(full_variant_type, 0)); + } else { + GB_ASSERT_MSG(LLVMStructGetTypeAtIndex(full_variant_type, 0) == LLVMTypeOf(variant_value), + "\n%s -> %s\n%s vs %s\n", + type_to_string(t), LLVMPrintValueToString(variant_value), + LLVMPrintTypeToString(LLVMStructGetTypeAtIndex(full_variant_type, 0)), LLVMPrintTypeToString(LLVMTypeOf(variant_value)) + ); + } + + full_variant_values[0] = variant_value; + full_variant_values[1] = LLVMConstNull(LLVMStructGetTypeAtIndex(full_variant_type, 1)); + full_variant_values[2] = LLVMConstInt(LLVMStructGetTypeAtIndex(full_variant_type, 2), tag_index, false); + } + LLVMValueRef full_variant_value = LLVMConstNamedStruct(full_variant_type, full_variant_values, full_variant_elem_count); + + small_const_values[variant_index] = full_variant_value; + + giant_const_values[entry_index] = LLVMConstNamedStruct(stype, small_const_values, variant_index+1); + } + + LLVMValueRef giant_const = LLVMConstNamedStruct(giant_struct_type, giant_const_values, cast(unsigned)global_type_info_data_entity_count); + LLVMSetInitializer(giant_struct, giant_const); +} + gb_internal void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info data if (build_context.no_rtti) { @@ -147,17 +997,17 @@ gb_internal void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup lbModule *m = p->module; CheckerInfo *info = m->info; - + i64 global_type_info_data_entity_count = 0; { // NOTE(bill): Set the type_table slice with the global backing array lbValue global_type_table = lb_find_runtime_value(m, str_lit("type_table")); Type *type = base_type(lb_global_type_info_data_entity->type); - GB_ASSERT(is_type_array(type)); + GB_ASSERT(type->kind == Type_Array); global_type_info_data_entity_count = type->Array.count; - LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)}; - LLVMValueRef data = LLVMConstInBoundsGEP2(lb_type(m, lb_global_type_info_data_entity->type), lb_global_type_info_data_ptr(m).value, indices, gb_count_of(indices)); + LLVMValueRef data = lb_global_type_info_data_ptr(m).value; + data = LLVMConstPointerCast(data, lb_type(m, alloc_type_pointer(type->Array.elem))); LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), type->Array.count, true); Type *t = type_deref(global_type_table.type); GB_ASSERT(is_type_slice(t)); @@ -166,6 +1016,10 @@ gb_internal void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup LLVMSetInitializer(global_type_table.value, slice); } + if (LB_USE_GIANT_PACKED_STRUCT) { + lb_setup_type_info_data_giant_packed_struct(m, global_type_info_data_entity_count); + return; + } // Useful types Entity *type_info_flags_entity = find_core_entity(info->checker, str_lit("Type_Info_Flags")); @@ -543,8 +1397,8 @@ gb_internal void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup case Type_Tuple: { tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_parameters_ptr); - lbValue memory_types = lb_type_info_member_types_offset(p, t->Tuple.variables.count); - lbValue memory_names = lb_type_info_member_names_offset(p, t->Tuple.variables.count); + lbValue memory_types = lb_type_info_member_types_offset(m, t->Tuple.variables.count); + lbValue memory_names = lb_type_info_member_names_offset(m, t->Tuple.variables.count); for_array(i, t->Tuple.variables) { @@ -639,7 +1493,7 @@ gb_internal void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup LLVMValueRef vals[7] = {}; isize variant_count = gb_max(0, t->Union.variants.count); - lbValue memory_types = lb_type_info_member_types_offset(p, variant_count); + lbValue memory_types = lb_type_info_member_types_offset(m, variant_count); // NOTE(bill): Zeroth is nil so ignore it for (isize variant_index = 0; variant_index < variant_count; variant_index++) { @@ -723,11 +1577,11 @@ gb_internal void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup isize count = t->Struct.fields.count; if (count > 0) { - lbValue memory_types = lb_type_info_member_types_offset (p, count); - lbValue memory_names = lb_type_info_member_names_offset (p, count); - lbValue memory_offsets = lb_type_info_member_offsets_offset(p, count); - lbValue memory_usings = lb_type_info_member_usings_offset (p, count); - lbValue memory_tags = lb_type_info_member_tags_offset (p, count); + lbValue memory_types = lb_type_info_member_types_offset (m, count); + lbValue memory_names = lb_type_info_member_names_offset (m, count); + lbValue memory_offsets = lb_type_info_member_offsets_offset(m, count); + lbValue memory_usings = lb_type_info_member_usings_offset (m, count); + lbValue memory_tags = lb_type_info_member_tags_offset (m, count); type_set_offsets(t); // NOTE(bill): Just incase the offsets have not been set yet for (isize source_index = 0; source_index < count; source_index++) { diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp index 8dd6b14b6..d8dbfd736 100644 --- a/src/llvm_backend_utility.cpp +++ b/src/llvm_backend_utility.cpp @@ -980,12 +980,12 @@ gb_internal LLVMTypeRef lb_type_padding_filler(lbModule *m, i64 padding, i64 pad GB_ASSERT_MSG(elem != nullptr, "Invalid lb_type_padding_filler padding and padding_align: %lld", padding_align); if (len != 1) { - return LLVMArrayType(elem, cast(unsigned)len); + return llvm_array_type(elem, len); } else { return elem; } } else { - return LLVMArrayType(lb_type(m, t_u8), cast(unsigned)padding); + return llvm_array_type(lb_type(m, t_u8), padding); } } @@ -1437,6 +1437,17 @@ gb_internal lbValue lb_emit_ptr_offset(lbProcedure *p, lbValue ptr, lbValue inde return res; } +gb_internal lbValue lb_const_ptr_offset(lbModule *m, lbValue ptr, lbValue index) { + LLVMValueRef indices[1] = {index.value}; + lbValue res = {}; + res.type = ptr.type; + LLVMTypeRef type = lb_type(m, type_deref(res.type, true)); + + GB_ASSERT(lb_is_const(ptr) && lb_is_const(index)); + res.value = LLVMConstGEP2(type, ptr.value, indices, 1); + return res; +} + 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)); |