From 43c3d5e348ce4772da6e283719fcef7e63d56ae5 Mon Sep 17 00:00:00 2001 From: Nikita Smith Date: Mon, 20 Apr 2026 13:19:34 -0700 Subject: [PATCH] test param flag --- src/rdi_from_dwarf/rdi_from_dwarf.c | 57 ++++++++++------------ src/rdi_from_dwarf/rdi_from_dwarf.h | 4 +- src/torture/torture_d2r.c | 75 ++++++++++++++++++++--------- 3 files changed, 80 insertions(+), 56 deletions(-) diff --git a/src/rdi_from_dwarf/rdi_from_dwarf.c b/src/rdi_from_dwarf/rdi_from_dwarf.c index 09f79400..72d937a4 100644 --- a/src/rdi_from_dwarf/rdi_from_dwarf.c +++ b/src/rdi_from_dwarf/rdi_from_dwarf.c @@ -2296,13 +2296,13 @@ d2r_range_list_from_tag(Arena *arena, DW_Input *input, DW_CompUnit *cu, U64 imag internal void d2r_convert_symbols(Arena *arena, D2R_TypeTable *type_table, - RDIM_Scope *global_scope, DW_Input *input, DW_CompUnit *cu, DW_Language cu_lang, U64 image_base, Arch arch, - DW_TagNode *root) + DW_TagNode *root, + RDIM_Unit *unit_rdi) { Temp scratch = scratch_begin(&arena, 1); for (D2R_TagIterator *it = d2r_tag_iterator_init(scratch.arena, root); it->tag_node != 0; d2r_tag_iterator_next(scratch.arena, it)) { @@ -2373,11 +2373,11 @@ d2r_convert_symbols(Arena *arena, String8 frame_base_expr = dw_exprloc_from_tag_attrib_kind(input, cu, tag, DW_AttribKind_FrameBase); // get proc container symbol - RDIM_Symbol *proc = rdim_symbol_chunk_list_push(arena, &g_d2r_shared.procs, D2R_PROC_CHUNK_CAP); + RDIM_Symbol *proc = rdim_symbol_chunk_list_push(arena, &unit_rdi->procedures, D2R_PROC_CHUNK_CAP); // make scope Rng1U64List ranges = d2r_range_list_from_tag(scratch.arena, input, cu, image_base, tag); - RDIM_Scope *root_scope = d2r_push_scope(arena, &g_d2r_shared.scopes, D2R_SCOPE_CHUNK_CAP, it->stack, ranges); + RDIM_Scope *root_scope = d2r_push_scope(arena, &unit_rdi->scopes, D2R_SCOPE_CHUNK_CAP, it->stack, ranges); root_scope->symbol = proc; // fill out proc @@ -2388,7 +2388,7 @@ d2r_convert_symbols(Arena *arena, proc->container_scope = 0; proc->container_type = container_type; proc->root_scope = root_scope; - proc->location_cases = d2r_locset_from_attrib(arena, &g_d2r_shared.scopes, root_scope, input, cu, image_base, arch, tag, DW_AttribKind_FrameBase); + proc->location_cases = d2r_locset_from_attrib(arena, &unit_rdi->scopes, root_scope, input, cu, image_base, arch, tag, DW_AttribKind_FrameBase); // sub program with user-defined parent tag is a method DW_Tag parent_tag = d2r_tag_iterator_parent_tag(it); @@ -2456,7 +2456,7 @@ d2r_convert_symbols(Arena *arena, // make scope Rng1U64List ranges = d2r_range_list_from_tag(scratch.arena, input, cu, image_base, tag); - RDIM_Scope *root_scope = d2r_push_scope(arena, &g_d2r_shared.scopes, D2R_SCOPE_CHUNK_CAP, it->stack, ranges); + RDIM_Scope *root_scope = d2r_push_scope(arena, &unit_rdi->scopes, D2R_SCOPE_CHUNK_CAP, it->stack, ranges); root_scope->inline_site = inline_site; } break; case DW_TagKind_Variable: { @@ -2471,7 +2471,7 @@ d2r_convert_symbols(Arena *arena, RDIM_Symbol *local = rdim_symbol_chunk_list_push(arena, &scope->locals, 8); local->name = name; local->type = type; - local->location_cases = d2r_var_locset_from_tag(arena, &g_d2r_shared.scopes, scope, input, cu, image_base, arch, tag); + local->location_cases = d2r_var_locset_from_tag(arena, &unit_rdi->scopes, scope, input, cu, image_base, arch, tag); } else { // NOTE: due to a bug in clang in stb_sprintf.h local variables @@ -2534,7 +2534,7 @@ d2r_convert_symbols(Arena *arena, param->is_param = 1; param->name = dw_string_from_tag_attrib_kind(input, cu, tag, DW_AttribKind_Name); param->type = d2r_type_from_attrib(type_table, input, cu, tag, DW_AttribKind_Type); - param->location_cases = d2r_var_locset_from_tag(arena, &g_d2r_shared.scopes, scope, input, cu, image_base, arch, tag); + param->location_cases = d2r_var_locset_from_tag(arena, &unit_rdi->scopes, scope, input, cu, image_base, arch, tag); } else { Assert(!"this is a local variable"); log_user_errorf(".debug_info+%llx out of scope formal parameter", tag.info_off); @@ -2546,7 +2546,7 @@ d2r_convert_symbols(Arena *arena, parent_tag.kind == DW_TagKind_InlinedSubroutine || parent_tag.kind == DW_TagKind_LexicalBlock) { Rng1U64List ranges = d2r_range_list_from_tag(scratch.arena, input, cu, image_base, tag); - d2r_push_scope(arena, &g_d2r_shared.scopes, D2R_SCOPE_CHUNK_CAP, it->stack, ranges); + d2r_push_scope(arena, &unit_rdi->scopes, D2R_SCOPE_CHUNK_CAP, it->stack, ranges); } } break; case DW_TagKind_CallSite: { @@ -3002,10 +3002,6 @@ d2r_convert(Arena *arena, D2R_ConvertParams *params) } ProfEnd(); - //////////////////////////////// - - RDIM_Scope *global_scope = rdim_scope_chunk_list_push(arena, &g_d2r_shared.scopes, D2R_SCOPE_CHUNK_CAP); - //////////////////////////////// RDIM_Type *builtin_types[RDI_TypeKind_Count] = {0}; @@ -3058,32 +3054,26 @@ d2r_convert(Arena *arena, D2R_ConvertParams *params) String8 cu_dir = dw_string_from_tag_attrib_kind(&input, cu, cu->tag, DW_AttribKind_CompDir); String8 cu_prod = dw_string_from_tag_attrib_kind(&input, cu, cu->tag, DW_AttribKind_Producer); DW_Language cu_lang = dw_const_u64_from_tag_attrib_kind(&input, cu, cu->tag, DW_AttribKind_Language); - + // init type table D2R_TypeTable *type_table = push_array(comp_temp.arena, D2R_TypeTable, 1); type_table->ht = hash_table_init(comp_temp.arena, 0x4000); type_table->types = &g_d2r_shared.types; type_table->type_chunk_cap = D2R_TYPE_CHUNK_CAP; type_table->builtin_types = builtin_types; - - // convert debug info - d2r_convert_types(arena, type_table, &input, cu, cu_lang, arch, tag_tree.root); - d2r_convert_udts(arena, type_table, &input, cu, cu_lang, tag_tree.root); - d2r_convert_symbols(arena, type_table, global_scope, &input, cu, cu_lang, image_base, arch, tag_tree.root); - - RDIM_Rng1U64ChunkList cu_voff_ranges = {0}; - if (cu_idx < cu_contrib_map.count) { - cu_voff_ranges = d2r_voff_ranges_from_cu_info_off(cu_contrib_map, cu_ranges.v[cu_idx].min); - } else { - Rng1U64List range_list = d2r_range_list_from_tag(scratch.arena, &input, cu, image_base, cu->tag); - for EachNode(n, Rng1U64Node, range_list.first) { - rdim_rng1u64_chunk_list_push(arena, &cu_voff_ranges, 512, (RDIM_Rng1U64){ .min = n->v.min, .max = n->v.max }); - } - } - + // convert compile unit + RDIM_Unit *unit = rdim_unit_chunk_list_push(arena, &g_d2r_shared.units, D2R_UNIT_CHUNK_CAP); { - RDIM_Unit *unit = rdim_unit_chunk_list_push(arena, &g_d2r_shared.units, D2R_UNIT_CHUNK_CAP); + RDIM_Rng1U64ChunkList cu_voff_ranges = {0}; + if (cu_idx < cu_contrib_map.count) { + cu_voff_ranges = d2r_voff_ranges_from_cu_info_off(cu_contrib_map, cu_ranges.v[cu_idx].min); + } else { + Rng1U64List range_list = d2r_range_list_from_tag(scratch.arena, &input, cu, image_base, cu->tag); + for EachNode(n, Rng1U64Node, range_list.first) { + rdim_rng1u64_chunk_list_push(arena, &cu_voff_ranges, 512, (RDIM_Rng1U64){ .min = n->v.min, .max = n->v.max }); + } + } unit->unit_name = cu_name; unit->compiler_name = cu_prod; unit->source_file = str8_zero(); // TODO @@ -3094,6 +3084,11 @@ d2r_convert(Arena *arena, D2R_ConvertParams *params) unit->line_table = cu_line_tables_rdi[cu_idx]; unit->voff_ranges = cu_voff_ranges; } + + // convert debug info + d2r_convert_types(arena, type_table, &input, cu, cu_lang, arch, tag_tree.root); + d2r_convert_udts(arena, type_table, &input, cu, cu_lang, tag_tree.root); + d2r_convert_symbols(arena, type_table, &input, cu, cu_lang, image_base, arch, tag_tree.root, unit); next_cu:; temp_end(comp_temp); diff --git a/src/rdi_from_dwarf/rdi_from_dwarf.h b/src/rdi_from_dwarf/rdi_from_dwarf.h index 8c05863e..63ccf208 100644 --- a/src/rdi_from_dwarf/rdi_from_dwarf.h +++ b/src/rdi_from_dwarf/rdi_from_dwarf.h @@ -42,8 +42,6 @@ typedef struct D2R_Shared RDIM_LineTableChunkList line_tables; RDIM_SymbolChunkList gvars; RDIM_SymbolChunkList tvars; - RDIM_SymbolChunkList procs; - RDIM_ScopeChunkList scopes; RDIM_InlineSiteChunkList inline_sites; } D2R_Shared; @@ -257,7 +255,7 @@ internal void d2r_convert_udts(Arena *arena, D2R_TypeTable *type_table, DW_Input internal RDIM_Scope * d2r_push_scope (Arena *arena, RDIM_ScopeChunkList *scopes, U64 scope_chunk_cap, D2R_TagFrame *tag_stack, Rng1U64List ranges); internal RDIM_Type ** d2r_collect_proc_params(Arena *arena, D2R_TypeTable *type_table, DW_Input *input, DW_CompUnit *cu, DW_TagNode *cur_node, U64 *param_count_out); internal Rng1U64List d2r_range_list_from_tag(Arena *arena, DW_Input *input, DW_CompUnit *cu, U64 image_base, DW_Tag tag); -internal void d2r_convert_symbols(Arena *arena, D2R_TypeTable *type_table, RDIM_Scope *global_scope, DW_Input *input, DW_CompUnit *cu, DW_Language cu_lang, U64 image_base, Arch arch, DW_TagNode *root); +internal void d2r_convert_symbols(Arena *arena, D2R_TypeTable *type_table, DW_Input *input, DW_CompUnit *cu, DW_Language cu_lang, U64 image_base, Arch arch, DW_TagNode *root, RDIM_Unit *unit_rdi); //////////////////////////////// //~ Line Table Conversion diff --git a/src/torture/torture_d2r.c b/src/torture/torture_d2r.c index 2cc85513..cf667d49 100644 --- a/src/torture/torture_d2r.c +++ b/src/torture/torture_d2r.c @@ -457,12 +457,18 @@ TEST(d2r_general) dw_writer_push_attrib_flag(writer, DW_AttribKind_External, 1); dw_writer_push_attrib_flag(writer, DW_AttribKind_Prototyped, 1); dw_writer_push_attrib_stringf(writer, DW_AttribKind_Name, "FooBar"); - // declare variable + // declare local dw_writer_tag_begin(writer, DW_TagKind_Variable); dw_writer_push_attrib_exprv(writer, DW_AttribKind_Location, DW_ExprEnc_Op(Reg7)); dw_writer_push_attrib_stringf(writer, DW_AttribKind_Name, "TestLocal"); dw_writer_push_attrib_ref(writer, DW_AttribKind_Type, char_type); dw_writer_tag_end(writer); + // declare param + dw_writer_tag_begin(writer, DW_TagKind_FormalParameter); + dw_writer_push_attrib_exprv(writer, DW_AttribKind_Location, DW_ExprEnc_Op(Reg7)); + dw_writer_push_attrib_stringf(writer, DW_AttribKind_Name, "TestParam"); + dw_writer_push_attrib_ref(writer, DW_AttribKind_Type, char_type); + dw_writer_tag_end(writer); dw_writer_tag_end(writer); } @@ -475,28 +481,53 @@ TEST(d2r_general) RDI_Scope *root_scope = rdi_root_scope_from_procedure(rdi, proc); T_Ok(root_scope); - T_Ok(root_scope->local_count == 1); + T_Ok(root_scope->local_count == 2); - RDI_Symbol *test_local = rdi_element_from_name_idx(rdi, LocalVariables, root_scope->local_first + 0); - T_Ok(test_local); - T_Ok(test_local->symbol_flags & RDI_SymbolFlag_IsParam); - String8 test_local_name = str8_from_rdi_string_idx(rdi, test_local->name_string_idx); - T_Ok(str8_match(test_local_name, str8_lit("TestLocal"), 0)); - - RDI_TypeNode *test_local_type = rdi_element_from_name_idx(rdi, TypeNodes, test_local->type_idx); - T_Ok(test_local_type); - T_Ok(test_local_type->kind == RDI_TypeKind_Alias); - T_Ok(test_local_type->flags == 0); - String8 alias_name = str8_from_rdi_string_idx(rdi, test_local_type->user_defined.name_string_idx); - T_Ok(str8_match(alias_name, str8_lit("char"), 0)); - - RDI_TypeNode *char_type = rdi_element_from_name_idx(rdi, TypeNodes, test_local_type->user_defined.direct_type_idx); - T_Ok(char_type); - T_Ok(char_type->kind == RDI_TypeKind_Char8); - T_Ok(char_type->flags == 0); - T_Ok(char_type->byte_size == 1); - String8 char_type_name = str8_from_rdi_string_idx(rdi, char_type->built_in.name_string_idx); - T_Ok(str8_match(char_type_name, str8_lit("Char8"), 0)); + { + RDI_Symbol *test_local = rdi_element_from_name_idx(rdi, LocalVariables, root_scope->local_first + 0); + T_Ok(test_local); + T_Ok((test_local->symbol_flags & RDI_SymbolFlag_IsParam) == 0); + String8 test_local_name = str8_from_rdi_string_idx(rdi, test_local->name_string_idx); + T_Ok(str8_match(test_local_name, str8_lit("TestLocal"), 0)); + + RDI_TypeNode *test_local_type = rdi_element_from_name_idx(rdi, TypeNodes, test_local->type_idx); + T_Ok(test_local_type); + T_Ok(test_local_type->kind == RDI_TypeKind_Alias); + T_Ok(test_local_type->flags == 0); + String8 alias_name = str8_from_rdi_string_idx(rdi, test_local_type->user_defined.name_string_idx); + T_Ok(str8_match(alias_name, str8_lit("char"), 0)); + + RDI_TypeNode *char_type = rdi_element_from_name_idx(rdi, TypeNodes, test_local_type->user_defined.direct_type_idx); + T_Ok(char_type); + T_Ok(char_type->kind == RDI_TypeKind_Char8); + T_Ok(char_type->flags == 0); + T_Ok(char_type->byte_size == 1); + String8 char_type_name = str8_from_rdi_string_idx(rdi, char_type->built_in.name_string_idx); + T_Ok(str8_match(char_type_name, str8_lit("Char8"), 0)); + } + + { + RDI_Symbol *test_local = rdi_element_from_name_idx(rdi, LocalVariables, root_scope->local_first + 1); + T_Ok(test_local); + T_Ok((test_local->symbol_flags & RDI_SymbolFlag_IsParam) != 0); + String8 test_local_name = str8_from_rdi_string_idx(rdi, test_local->name_string_idx); + T_Ok(str8_match(test_local_name, str8_lit("TestParam"), 0)); + + RDI_TypeNode *test_local_type = rdi_element_from_name_idx(rdi, TypeNodes, test_local->type_idx); + T_Ok(test_local_type); + T_Ok(test_local_type->kind == RDI_TypeKind_Alias); + T_Ok(test_local_type->flags == 0); + String8 alias_name = str8_from_rdi_string_idx(rdi, test_local_type->user_defined.name_string_idx); + T_Ok(str8_match(alias_name, str8_lit("char"), 0)); + + RDI_TypeNode *char_type = rdi_element_from_name_idx(rdi, TypeNodes, test_local_type->user_defined.direct_type_idx); + T_Ok(char_type); + T_Ok(char_type->kind == RDI_TypeKind_Char8); + T_Ok(char_type->flags == 0); + T_Ok(char_type->byte_size == 1); + String8 char_type_name = str8_from_rdi_string_idx(rdi, char_type->built_in.name_string_idx); + T_Ok(str8_match(char_type_name, str8_lit("Char8"), 0)); + } dw_writer_end(&writer); }