Skip to content

Commit

Permalink
Canonicalize generated procedures
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerBill committed Feb 24, 2025
1 parent 533f82d commit 46a8991
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 27 deletions.
39 changes: 14 additions & 25 deletions src/llvm_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/llvm_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 0 additions & 2 deletions src/llvm_backend_stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 46a8991

Please sign in to comment.