From cd8070f94f6865959949dbcec6e0e32cd88bb544 Mon Sep 17 00:00:00 2001 From: antlilja Date: Sat, 30 Jul 2022 11:39:49 +0200 Subject: [PATCH] Removed param_names from Fn inside Module.zig Removed the copy of param_names inside of Fn and changed to implementation of getParamName to fetch to parameter name from the ZIR. The signature of getParamName was also changed to take an additional *Module argument. --- src/Module.zig | 38 +++++++++++++++++++----------------- src/Sema.zig | 6 ------ src/Zir.zig | 21 ++++++++++++++++++++ src/arch/arm/CodeGen.zig | 2 +- src/arch/riscv64/CodeGen.zig | 2 +- src/arch/sparc64/CodeGen.zig | 2 +- src/arch/wasm/CodeGen.zig | 2 +- src/arch/x86_64/CodeGen.zig | 2 +- src/codegen/llvm.zig | 2 +- 9 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src/Module.zig b/src/Module.zig index 4ac2775515..b80e00ad59 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -1471,14 +1471,6 @@ pub const Fn = struct { /// TODO apply the same enhancement for param_names below to this field. anytype_args: [*]bool, - /// Prefer to use `getParamName` to access this because of the future improvement - /// we want to do mentioned in the TODO below. - /// Stored in gpa. - /// TODO: change param ZIR instructions to be embedded inside the function - /// ZIR instruction instead of before it, so that `zir_body_inst` can be used to - /// determine param names rather than redundantly storing them here. - param_names: []const [:0]const u8, - /// Precomputed hash for monomorphed_funcs. /// This is important because it may be accessed when resizing monomorphed_funcs /// while this Fn has already been added to the set, but does not have the @@ -1590,18 +1582,28 @@ pub const Fn = struct { gpa.destroy(node); it = next; } - - for (func.param_names) |param_name| { - gpa.free(param_name); - } - gpa.free(func.param_names); } - pub fn getParamName(func: Fn, index: u32) [:0]const u8 { - // TODO rework ZIR of parameters so that this function looks up - // param names in ZIR instead of redundantly saving them into Fn. - // const zir = func.owner_decl.getFileScope().zir; - return func.param_names[index]; + pub fn getParamName(func: Fn, mod: *Module, index: u32) [:0]const u8 { + const file = mod.declPtr(func.owner_decl).getFileScope(); + + const tags = file.zir.instructions.items(.tag); + const data = file.zir.instructions.items(.data); + + const param_body = file.zir.getParamBody(func.zir_body_inst); + const param = param_body[index]; + + return switch (tags[param]) { + .param, .param_comptime => blk: { + const extra = file.zir.extraData(Zir.Inst.Param, data[param].pl_tok.payload_index); + break :blk file.zir.nullTerminatedString(extra.data.name); + }, + .param_anytype, .param_anytype_comptime => blk: { + const param_data = data[param].str_tok; + break :blk param_data.get(file.zir); + }, + else => unreachable, + }; } pub fn hasInferredErrorSet(func: Fn, mod: *Module) bool { diff --git a/src/Sema.zig b/src/Sema.zig index a0829d6eb7..07e81dc5be 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -7753,11 +7753,6 @@ fn funcCommon( break :blk if (sema.comptime_args.len == 0) null else sema.comptime_args.ptr; } else null; - const param_names = try sema.gpa.alloc([:0]const u8, block.params.items.len); - for (param_names) |*param_name, i| { - param_name.* = try sema.gpa.dupeZ(u8, block.params.items[i].name); - } - const hash = new_func.hash; const fn_payload = try sema.arena.create(Value.Payload.Function); new_func.* = .{ @@ -7771,7 +7766,6 @@ fn funcCommon( .rbrace_line = src_locs.rbrace_line, .lbrace_column = @truncate(u16, src_locs.columns), .rbrace_column = @truncate(u16, src_locs.columns >> 16), - .param_names = param_names, .branch_quota = default_branch_quota, .is_noinline = is_noinline, }; diff --git a/src/Zir.zig b/src/Zir.zig index ccd677df0b..5af3c0038d 100644 --- a/src/Zir.zig +++ b/src/Zir.zig @@ -3909,6 +3909,27 @@ pub const FnInfo = struct { total_params_len: u32, }; +pub fn getParamBody(zir: Zir, fn_inst: Inst.Index) []const u32 { + const tags = zir.instructions.items(.tag); + const datas = zir.instructions.items(.data); + const inst_data = datas[fn_inst].pl_node; + + const param_block_index = switch (tags[fn_inst]) { + .func, .func_inferred => blk: { + const extra = zir.extraData(Inst.Func, inst_data.payload_index); + break :blk extra.data.param_block; + }, + .func_fancy => blk: { + const extra = zir.extraData(Inst.FuncFancy, inst_data.payload_index); + break :blk extra.data.param_block; + }, + else => unreachable, + }; + + const param_block = zir.extraData(Inst.Block, datas[param_block_index].pl_node.payload_index); + return zir.extra[param_block.end..][0..param_block.data.body_len]; +} + pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo { const tags = zir.instructions.items(.tag); const datas = zir.instructions.items(.data); diff --git a/src/arch/arm/CodeGen.zig b/src/arch/arm/CodeGen.zig index 93d98c41d3..a603f72d53 100644 --- a/src/arch/arm/CodeGen.zig +++ b/src/arch/arm/CodeGen.zig @@ -3372,7 +3372,7 @@ fn genArgDbgInfo(self: *Self, inst: Air.Inst.Index, arg_index: u32, stack_byte_c const mcv = self.args[arg_index]; const ty = self.air.instructions.items(.data)[inst].ty; - const name = self.mod_fn.getParamName(arg_index); + const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg_index); const name_with_null = name.ptr[0 .. name.len + 1]; switch (mcv) { diff --git a/src/arch/riscv64/CodeGen.zig b/src/arch/riscv64/CodeGen.zig index 220fb18699..d09c6278b5 100644 --- a/src/arch/riscv64/CodeGen.zig +++ b/src/arch/riscv64/CodeGen.zig @@ -1619,7 +1619,7 @@ fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) !void { fn genArgDbgInfo(self: *Self, inst: Air.Inst.Index, mcv: MCValue, arg_index: u32) !void { const ty = self.air.instructions.items(.data)[inst].ty; - const name = self.mod_fn.getParamName(arg_index); + const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg_index); const name_with_null = name.ptr[0 .. name.len + 1]; switch (mcv) { diff --git a/src/arch/sparc64/CodeGen.zig b/src/arch/sparc64/CodeGen.zig index 2c6a322fca..bc9fc115b2 100644 --- a/src/arch/sparc64/CodeGen.zig +++ b/src/arch/sparc64/CodeGen.zig @@ -2959,7 +2959,7 @@ fn finishAir(self: *Self, inst: Air.Inst.Index, result: MCValue, operands: [Live fn genArgDbgInfo(self: *Self, inst: Air.Inst.Index, mcv: MCValue, arg_index: u32) !void { const ty = self.air.instructions.items(.data)[inst].ty; - const name = self.mod_fn.getParamName(arg_index); + const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg_index); const name_with_null = name.ptr[0 .. name.len + 1]; switch (mcv) { diff --git a/src/arch/wasm/CodeGen.zig b/src/arch/wasm/CodeGen.zig index 91072d0b4c..dd9cfba548 100644 --- a/src/arch/wasm/CodeGen.zig +++ b/src/arch/wasm/CodeGen.zig @@ -1991,7 +1991,7 @@ fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!WValue { switch (self.debug_output) { .dwarf => |dwarf| { // TODO: Get the original arg index rather than wasm arg index - const name = self.mod_fn.getParamName(arg_index); + const name = self.mod_fn.getParamName(self.bin_file.base.options.module.?, arg_index); const leb_size = link.File.Wasm.getULEB128Size(arg.local); const dbg_info = &dwarf.dbg_info; try dbg_info.ensureUnusedCapacity(3 + leb_size + 5 + name.len + 1); diff --git a/src/arch/x86_64/CodeGen.zig b/src/arch/x86_64/CodeGen.zig index 09721c661f..d59a114e51 100644 --- a/src/arch/x86_64/CodeGen.zig +++ b/src/arch/x86_64/CodeGen.zig @@ -3789,7 +3789,7 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void { const ty = self.air.typeOfIndex(inst); const mcv = self.args[arg_index]; - const name = self.mod_fn.getParamName(arg_index); + const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg_index); const name_with_null = name.ptr[0 .. name.len + 1]; if (self.liveness.isUnused(inst)) diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 664edb0304..5ea7ffad67 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -7313,7 +7313,7 @@ pub const FuncGen = struct { const lbrace_col = func.lbrace_column + 1; const di_local_var = dib.createParameterVariable( self.di_scope.?, - func.getParamName(src_index).ptr, // TODO test 0 bit args + func.getParamName(self.dg.module, src_index).ptr, // TODO test 0 bit args self.di_file.?, lbrace_line, try self.dg.object.lowerDebugType(inst_ty, .full),