Browse Source

Canonicalize generated procedures

gingerBill 6 months ago
parent
commit
46a8991d0f
3 changed files with 17 additions and 27 deletions
  1. 14 25
      src/llvm_backend.cpp
  2. 3 0
      src/llvm_backend.hpp
  3. 0 2
      src/llvm_backend_stmt.cpp

+ 14 - 25
src/llvm_backend.cpp

@@ -233,6 +233,16 @@ gb_internal lbContextData *lb_push_context_onto_stack(lbProcedure *p, lbAddr ctx
 }
 
 
+gb_internal String lb_internal_gen_proc_name_from_type(char const *prefix, Type *type) {
+	gbString str = gb_string_make(permanent_allocator(), prefix);
+	gbString tcs = temp_canonical_string(type);
+	str = gb_string_appendc(str, CANONICAL_TYPE_SEPARATOR);
+	str = gb_string_append_length(str, tcs, gb_string_length(tcs));
+	String proc_name = make_string(cast(u8 const *)str, gb_string_length(str));
+	return proc_name;
+}
+
+
 gb_internal lbValue lb_equal_proc_for_type(lbModule *m, Type *type) {
 	type = base_type(type);
 	GB_ASSERT(is_type_comparable(type));
@@ -248,16 +258,8 @@ gb_internal lbValue lb_equal_proc_for_type(lbModule *m, Type *type) {
 		return {compare_proc->value, compare_proc->type};
 	}
 
-	static std::atomic<u32> proc_index;
 
-	char buf[32] = {};
-	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);
-
-	lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_equal_proc);
-	map_set(&m->equal_procs, type, p);
-	lb_begin_procedure_body(p);
+	String proc_name = lb_internal_gen_proc_name_from_type("__$equal", type);
 
 	// lb_add_attribute_to_proc(m, p->value, "readonly");
 	lb_add_attribute_to_proc(m, p->value, "nounwind");
@@ -416,12 +418,7 @@ gb_internal lbValue lb_hasher_proc_for_type(lbModule *m, Type *type) {
 		return {(*found)->value, (*found)->type};
 	}
 
-	static std::atomic<u32> proc_index;
-
-	char buf[32] = {};
-	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);
+	String proc_name = lb_internal_gen_proc_name_from_type("__$hasher", type);
 
 	lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_hasher_proc);
 	map_set(&m->hasher_procs, type, p);
@@ -583,12 +580,8 @@ gb_internal lbValue lb_map_get_proc_for_type(lbModule *m, Type *type) {
 		GB_ASSERT(*found != nullptr);
 		return {(*found)->value, (*found)->type};
 	}
-	static std::atomic<u32> proc_index;
 
-	char buf[32] = {};
-	isize n = gb_snprintf(buf, 32, "__$map_get-%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);
+	String proc_name = lb_internal_gen_proc_name_from_type("__$map_get", type);
 
 	lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_map_get_proc);
 	map_set(&m->map_get_procs, type, p);
@@ -764,12 +757,8 @@ gb_internal lbValue lb_map_set_proc_for_type(lbModule *m, Type *type) {
 		GB_ASSERT(*found != nullptr);
 		return {(*found)->value, (*found)->type};
 	}
-	static std::atomic<u32> proc_index;
 
-	char buf[32] = {};
-	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);
+	String proc_name = lb_internal_gen_proc_name_from_type("__$map_set", type);
 
 	lbProcedure *p = lb_create_dummy_procedure(m, proc_name, t_map_set_proc);
 	map_set(&m->map_set_procs, type, p);

+ 3 - 0
src/llvm_backend.hpp

@@ -610,6 +610,9 @@ gb_internal LLVMTypeRef llvm_array_type(LLVMTypeRef ElementType, uint64_t Elemen
 }
 
 
+gb_internal String lb_internal_gen_proc_name_from_type(char const *prefix, Type *type);
+
+
 gb_internal void lb_set_metadata_custom_u64(lbModule *m, LLVMValueRef v_ref, String name, u64 value);
 gb_internal u64 lb_get_metadata_custom_u64(lbModule *m, LLVMValueRef v_ref, String name);
 

+ 0 - 2
src/llvm_backend_stmt.cpp

@@ -5,8 +5,6 @@ gb_internal void lb_build_constant_value_decl(lbProcedure *p, AstValueDecl *vd)
 
 	auto *min_dep_set = &p->module->info->minimum_dependency_set;
 
-	static i32 global_guid = 0;
-
 	for (Ast *ident : vd->names) {
 		GB_ASSERT(ident->kind == Ast_Ident);
 		Entity *e = entity_of_node(ident);