Browse Source

Begin work on making the type info table be constantly initialized

gingerBill 1 year ago
parent
commit
082324d7b9

+ 7 - 7
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));
 				}
 			}
 		}

+ 9 - 3
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 = {};

+ 11 - 0
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 &param_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"

+ 9 - 5
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) {

+ 23 - 3
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;

+ 1 - 1
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);

File diff suppressed because it is too large
+ 898 - 44
src/llvm_backend_type.cpp


+ 13 - 2
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));

Some files were not shown because too many files changed in this diff