Skip to content

Commit

Permalink
Make debug symbols use the new name canonicalization rules
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerBill committed Feb 20, 2025
1 parent 9dc4d5a commit 90a973c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
29 changes: 13 additions & 16 deletions src/llvm_backend_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ gb_internal LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) {
8*cast(unsigned)type_align_of(type),
lb_debug_type(m, type->EnumeratedArray.elem),
subscripts, gb_count_of(subscripts));
gbString name = type_to_string(type, temporary_allocator());
gbString name = temp_canonical_string(type);
return LLVMDIBuilderCreateTypedef(m->debug_builder, array_type, name, gb_string_length(name), nullptr, 0, nullptr, cast(u32)(8*type_align_of(type)));
}

Expand All @@ -852,16 +852,16 @@ gb_internal LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) {
Type *bt = base_type(type->Map.debug_metadata_type);
GB_ASSERT(bt->kind == Type_Struct);

return lb_debug_struct(m, type, bt, make_string_c(type_to_string(type, temporary_allocator())), nullptr, nullptr, 0);
return lb_debug_struct(m, type, bt, type_to_canonical_string(temporary_allocator(), type), nullptr, nullptr, 0);
}

case Type_Struct: return lb_debug_struct( m, type, type, make_string_c(type_to_string(type, temporary_allocator())), nullptr, nullptr, 0);
case Type_Slice: return lb_debug_slice( m, type, make_string_c(type_to_string(type, temporary_allocator())), nullptr, nullptr, 0);
case Type_DynamicArray: return lb_debug_dynamic_array(m, type, make_string_c(type_to_string(type, temporary_allocator())), nullptr, nullptr, 0);
case Type_Union: return lb_debug_union( m, type, make_string_c(type_to_string(type, temporary_allocator())), nullptr, nullptr, 0);
case Type_BitSet: return lb_debug_bitset( m, type, make_string_c(type_to_string(type, temporary_allocator())), nullptr, nullptr, 0);
case Type_Enum: return lb_debug_enum( m, type, make_string_c(type_to_string(type, temporary_allocator())), nullptr, nullptr, 0);
case Type_BitField: return lb_debug_bitfield( m, type, make_string_c(type_to_string(type, temporary_allocator())), nullptr, nullptr, 0);
case Type_Struct: return lb_debug_struct( m, type, type, type_to_canonical_string(temporary_allocator(), type), nullptr, nullptr, 0);
case Type_Slice: return lb_debug_slice( m, type, type_to_canonical_string(temporary_allocator(), type), nullptr, nullptr, 0);
case Type_DynamicArray: return lb_debug_dynamic_array(m, type, type_to_canonical_string(temporary_allocator(), type), nullptr, nullptr, 0);
case Type_Union: return lb_debug_union( m, type, type_to_canonical_string(temporary_allocator(), type), nullptr, nullptr, 0);
case Type_BitSet: return lb_debug_bitset( m, type, type_to_canonical_string(temporary_allocator(), type), nullptr, nullptr, 0);
case Type_Enum: return lb_debug_enum( m, type, type_to_canonical_string(temporary_allocator(), type), nullptr, nullptr, 0);
case Type_BitField: return lb_debug_bitfield( m, type, type_to_canonical_string(temporary_allocator(), type), nullptr, nullptr, 0);

case Type_Tuple:
if (type->Tuple.variables.count == 1) {
Expand Down Expand Up @@ -904,7 +904,7 @@ gb_internal LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) {
{
LLVMMetadataRef proc_underlying_type = lb_debug_type_internal_proc(m, type);
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());
gbString name = temp_canonical_string(type);
return LLVMDIBuilderCreateTypedef(m->debug_builder, pointer_type, name, gb_string_length(name), nullptr, 0, nullptr, cast(u32)(8*type_align_of(type)));
}
break;
Expand Down Expand Up @@ -987,10 +987,7 @@ gb_internal LLVMMetadataRef lb_debug_type(lbModule *m, Type *type) {
line = cast(unsigned)e->token.pos.line;
}

String name = type->Named.name;
if (type->Named.type_name && type->Named.type_name->pkg && type->Named.type_name->pkg->name.len != 0) {
name = concatenate3_strings(temporary_allocator(), type->Named.type_name->pkg->name, str_lit("."), type->Named.name);
}
String name = type_to_canonical_string(temporary_allocator(), type);

Type *bt = base_type(type->Named.base);

Expand Down Expand Up @@ -1187,8 +1184,8 @@ gb_internal void lb_add_debug_context_variable(lbProcedure *p, lbAddr const &ctx
gb_internal String debug_info_mangle_constant_name(Entity *e, gbAllocator const &allocator, bool *did_allocate_) {
String name = e->token.string;
if (e->pkg && e->pkg->name.len > 0) {
// NOTE(bill): C++ NONSENSE FOR DEBUG SHITE!
name = concatenate3_strings(allocator, e->pkg->name, str_lit("::"), name);
gbString s = string_canonical_entity_name(allocator, e);
name = make_string(cast(u8 const *)s, gb_string_length(s));
if (did_allocate_) *did_allocate_ = true;
}
return name;
Expand Down
22 changes: 18 additions & 4 deletions src/name_canonicalization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ gb_internal void write_canonical_entity_name(TypeWriter *w, Entity *e) {
return;
}

bool write_scope_index_suffix = false;

if (e->scope->flags & (ScopeFlag_Builtin)) {
goto write_base_name;
} else if ((e->scope->flags & (ScopeFlag_File | ScopeFlag_Pkg)) == 0 ||
Expand All @@ -438,7 +440,7 @@ gb_internal void write_canonical_entity_name(TypeWriter *w, Entity *e) {
Entity *parent = s->decl_info->entity;
write_canonical_parent_prefix(w, parent);
if (e->scope->index > 0) {
type_writer_append_fmt(w, CANONICAL_TYPE_SEPARATOR "[%d]", e->scope->index);
write_scope_index_suffix = true;
}

goto write_base_name;
Expand Down Expand Up @@ -491,21 +493,30 @@ gb_internal void write_canonical_entity_name(TypeWriter *w, Entity *e) {
type_writer_append(w, e->token.string.text, e->token.string.len);
}
}
return;
break;

case Entity_Constant:
// For debug symbols only
/*fallthrough*/
case Entity_Procedure:
case Entity_Variable:
type_writer_append(w, e->token.string.text, e->token.string.len);
if (is_type_polymorphic(e->type)) {
type_writer_appendc(w, CANONICAL_TYPE_SEPARATOR);
write_type_to_canonical_string(w, e->type);
}
return;
break;

default:
GB_PANIC("TODO(bill): entity kind %d", e->kind);
break;
}

if (write_scope_index_suffix) {
GB_ASSERT(e != nullptr && e->scope != nullptr);
type_writer_append_fmt(w, "[%d]", e->scope->index);
}

return;
}

Expand Down Expand Up @@ -661,7 +672,10 @@ gb_internal void write_type_to_canonical_string(TypeWriter *w, Type *type) {
type_writer_append(w, f->token.string.text, f->token.string.len);
type_writer_appendc(w, CANONICAL_TYPE_SEPARATOR);
write_type_to_canonical_string(w, f->type);
String tag = type->Struct.tags[i];
String tag = {};
if (type->Struct.tags != nullptr) {
tag = type->Struct.tags[i];
}
if (tag.len != 0) {
String s = quote_to_ascii(heap_allocator(), tag);
type_writer_append(w, s.text, s.len);
Expand Down

0 comments on commit 90a973c

Please sign in to comment.