authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-04-29 19:57:44-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-05-01 19:22:52-04:00
log372bc960b8315ea1ff17b369b0b33485d5e34cfb
tree8f492631dec579c28b8430e80213f6ad4d286dd9
parentf37ca3fa7370c501c630c53b370fecdeb313e3be

link: update decl-specific lazy symbols


4 files changed, 39 insertions(+), 28 deletions(-)

src/link.zig+2-2
......@@ -1120,8 +1120,8 @@ pub const File = struct {
11201120 kind: Kind,
11211121 ty: Type,
11221122
1123 pub fn initDecl(kind: Kind, decl: Module.Decl.OptionalIndex, mod: *Module) LazySymbol {
1124 return .{ .kind = kind, .ty = if (decl.unwrap()) |decl_index|
1123 pub fn initDecl(kind: Kind, decl: ?Module.Decl.Index, mod: *Module) LazySymbol {
1124 return .{ .kind = kind, .ty = if (decl) |decl_index|
11251125 mod.declPtr(decl_index).val.castTag(.ty).?.data
11261126 else
11271127 Type.anyerror };
src/link/Coff.zig+14-9
......@@ -1136,7 +1136,11 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
11361136 return atom.getSymbolIndex().?;
11371137}
11381138
1139pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) !void {
1139pub fn updateDecl(
1140 self: *Coff,
1141 module: *Module,
1142 decl_index: Module.Decl.Index,
1143) link.File.UpdateDeclError!void {
11401144 if (build_options.skip_non_native and builtin.object_format != .coff) {
11411145 @panic("Attempted to compile for object format that was disabled by build configuration");
11421146 }
......@@ -1146,6 +1150,8 @@ pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) !
11461150 const tracy = trace(@src());
11471151 defer tracy.end();
11481152
1153 try self.updateLazySymbol(decl_index);
1154
11491155 const decl = module.declPtr(decl_index);
11501156
11511157 if (decl.val.tag() == .extern_fn) {
......@@ -1188,7 +1194,8 @@ pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) !
11881194 return self.updateDeclExports(module, decl_index, module.getDeclExports(decl_index));
11891195}
11901196
1191fn updateLazySymbol(self: *Coff, decl: Module.Decl.OptionalIndex, metadata: LazySymbolMetadata) !void {
1197fn updateLazySymbol(self: *Coff, decl: ?Module.Decl.Index) !void {
1198 const metadata = self.lazy_syms.get(Module.Decl.OptionalIndex.init(decl)) orelse return;
11921199 const mod = self.base.options.module.?;
11931200 if (metadata.text_atom) |atom| try self.updateLazySymbolAtom(
11941201 link.File.LazySymbol.initDecl(.code, decl, mod),
......@@ -1402,7 +1409,7 @@ pub fn updateDeclExports(
14021409 module: *Module,
14031410 decl_index: Module.Decl.Index,
14041411 exports: []const *Module.Export,
1405) !void {
1412) link.File.UpdateDeclExportsError!void {
14061413 if (build_options.skip_non_native and builtin.object_format != .coff) {
14071414 @panic("Attempted to compile for object format that was disabled by build configuration");
14081415 }
......@@ -1599,12 +1606,10 @@ pub fn flushModule(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
15991606
16001607 // Most lazy symbols can be updated when the corresponding decl is,
16011608 // so we only have to worry about the one without an associated decl.
1602 if (self.lazy_syms.get(.none)) |metadata| {
1603 self.updateLazySymbol(.none, metadata) catch |err| switch (err) {
1604 error.CodegenFail => return error.FlushFailure,
1605 else => |e| return e,
1606 };
1607 }
1609 self.updateLazySymbol(null) catch |err| switch (err) {
1610 error.CodegenFail => return error.FlushFailure,
1611 else => |e| return e,
1612 };
16081613
16091614 const gpa = self.base.allocator;
16101615
src/link/Elf.zig+14-9
......@@ -1034,12 +1034,10 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10341034
10351035 // Most lazy symbols can be updated when the corresponding decl is,
10361036 // so we only have to worry about the one without an associated decl.
1037 if (self.lazy_syms.get(.none)) |metadata| {
1038 self.updateLazySymbol(.none, metadata) catch |err| switch (err) {
1039 error.CodegenFail => return error.FlushFailure,
1040 else => |e| return e,
1041 };
1042 }
1037 self.updateLazySymbol(null) catch |err| switch (err) {
1038 error.CodegenFail => return error.FlushFailure,
1039 else => |e| return e,
1040 };
10431041
10441042 // TODO This linker code currently assumes there is only 1 compilation unit and it
10451043 // corresponds to the Zig source code.
......@@ -2579,7 +2577,11 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven
25792577 return self.updateDeclExports(module, decl_index, module.getDeclExports(decl_index));
25802578}
25812579
2582pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !void {
2580pub fn updateDecl(
2581 self: *Elf,
2582 module: *Module,
2583 decl_index: Module.Decl.Index,
2584) File.UpdateDeclError!void {
25832585 if (build_options.skip_non_native and builtin.object_format != .elf) {
25842586 @panic("Attempted to compile for object format that was disabled by build configuration");
25852587 }
......@@ -2590,6 +2592,8 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v
25902592 const tracy = trace(@src());
25912593 defer tracy.end();
25922594
2595 try self.updateLazySymbol(decl_index);
2596
25932597 const decl = module.declPtr(decl_index);
25942598
25952599 if (decl.val.tag() == .extern_fn) {
......@@ -2656,7 +2660,8 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v
26562660 return self.updateDeclExports(module, decl_index, module.getDeclExports(decl_index));
26572661}
26582662
2659fn updateLazySymbol(self: *Elf, decl: Module.Decl.OptionalIndex, metadata: LazySymbolMetadata) !void {
2663fn updateLazySymbol(self: *Elf, decl: ?Module.Decl.Index) !void {
2664 const metadata = self.lazy_syms.get(Module.Decl.OptionalIndex.init(decl)) orelse return;
26602665 const mod = self.base.options.module.?;
26612666 if (metadata.text_atom) |atom| try self.updateLazySymbolAtom(
26622667 File.LazySymbol.initDecl(.code, decl, mod),
......@@ -2810,7 +2815,7 @@ pub fn updateDeclExports(
28102815 module: *Module,
28112816 decl_index: Module.Decl.Index,
28122817 exports: []const *Module.Export,
2813) !void {
2818) File.UpdateDeclExportsError!void {
28142819 if (build_options.skip_non_native and builtin.object_format != .elf) {
28152820 @panic("Attempted to compile for object format that was disabled by build configuration");
28162821 }
src/link/MachO.zig+9-8
......@@ -495,12 +495,10 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
495495
496496 // Most lazy symbols can be updated when the corresponding decl is,
497497 // so we only have to worry about the one without an associated decl.
498 if (self.lazy_syms.get(.none)) |metadata| {
499 self.updateLazySymbol(.none, metadata) catch |err| switch (err) {
500 error.CodegenFail => return error.FlushFailure,
501 else => |e| return e,
502 };
503 }
498 self.updateLazySymbol(null) catch |err| switch (err) {
499 error.CodegenFail => return error.FlushFailure,
500 else => |e| return e,
501 };
504502
505503 const module = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented;
506504
......@@ -1962,6 +1960,8 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)
19621960 const tracy = trace(@src());
19631961 defer tracy.end();
19641962
1963 try self.updateLazySymbol(decl_index);
1964
19651965 const decl = module.declPtr(decl_index);
19661966
19671967 if (decl.val.tag() == .extern_fn) {
......@@ -2036,7 +2036,8 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)
20362036 try self.updateDeclExports(module, decl_index, module.getDeclExports(decl_index));
20372037}
20382038
2039fn updateLazySymbol(self: *MachO, decl: Module.Decl.OptionalIndex, metadata: LazySymbolMetadata) !void {
2039fn updateLazySymbol(self: *MachO, decl: ?Module.Decl.Index) !void {
2040 const metadata = self.lazy_syms.get(Module.Decl.OptionalIndex.init(decl)) orelse return;
20402041 const mod = self.base.options.module.?;
20412042 if (metadata.text_atom) |atom| try self.updateLazySymbolAtom(
20422043 File.LazySymbol.initDecl(.code, decl, mod),
......@@ -2353,7 +2354,7 @@ pub fn updateDeclExports(
23532354 module: *Module,
23542355 decl_index: Module.Decl.Index,
23552356 exports: []const *Module.Export,
2356) !void {
2357) File.UpdateDeclExportsError!void {
23572358 if (build_options.skip_non_native and builtin.object_format != .macho) {
23582359 @panic("Attempted to compile for object format that was disabled by build configuration");
23592360 }