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 {...@@ -1120,8 +1120,8 @@ pub const File = struct {
1120 kind: Kind,1120 kind: Kind,
1121 ty: Type,1121 ty: Type,
11221122
1123 pub fn initDecl(kind: Kind, decl: Module.Decl.OptionalIndex, mod: *Module) LazySymbol {1123 pub fn initDecl(kind: Kind, decl: ?Module.Decl.Index, mod: *Module) LazySymbol {
1124 return .{ .kind = kind, .ty = if (decl.unwrap()) |decl_index|1124 return .{ .kind = kind, .ty = if (decl) |decl_index|
1125 mod.declPtr(decl_index).val.castTag(.ty).?.data1125 mod.declPtr(decl_index).val.castTag(.ty).?.data
1126 else1126 else
1127 Type.anyerror };1127 Type.anyerror };
src/link/Coff.zig+14-9
...@@ -1136,7 +1136,11 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In...@@ -1136,7 +1136,11 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: Module.Decl.In
1136 return atom.getSymbolIndex().?;1136 return atom.getSymbolIndex().?;
1137}1137}
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 {
1140 if (build_options.skip_non_native and builtin.object_format != .coff) {1144 if (build_options.skip_non_native and builtin.object_format != .coff) {
1141 @panic("Attempted to compile for object format that was disabled by build configuration");1145 @panic("Attempted to compile for object format that was disabled by build configuration");
1142 }1146 }
...@@ -1146,6 +1150,8 @@ pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) !...@@ -1146,6 +1150,8 @@ pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) !
1146 const tracy = trace(@src());1150 const tracy = trace(@src());
1147 defer tracy.end();1151 defer tracy.end();
11481152
1153 try self.updateLazySymbol(decl_index);
1154
1149 const decl = module.declPtr(decl_index);1155 const decl = module.declPtr(decl_index);
11501156
1151 if (decl.val.tag() == .extern_fn) {1157 if (decl.val.tag() == .extern_fn) {
...@@ -1188,7 +1194,8 @@ pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) !...@@ -1188,7 +1194,8 @@ pub fn updateDecl(self: *Coff, module: *Module, decl_index: Module.Decl.Index) !
1188 return self.updateDeclExports(module, decl_index, module.getDeclExports(decl_index));1194 return self.updateDeclExports(module, decl_index, module.getDeclExports(decl_index));
1189}1195}
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;
1192 const mod = self.base.options.module.?;1199 const mod = self.base.options.module.?;
1193 if (metadata.text_atom) |atom| try self.updateLazySymbolAtom(1200 if (metadata.text_atom) |atom| try self.updateLazySymbolAtom(
1194 link.File.LazySymbol.initDecl(.code, decl, mod),1201 link.File.LazySymbol.initDecl(.code, decl, mod),
...@@ -1402,7 +1409,7 @@ pub fn updateDeclExports(...@@ -1402,7 +1409,7 @@ pub fn updateDeclExports(
1402 module: *Module,1409 module: *Module,
1403 decl_index: Module.Decl.Index,1410 decl_index: Module.Decl.Index,
1404 exports: []const *Module.Export,1411 exports: []const *Module.Export,
1405) !void {1412) link.File.UpdateDeclExportsError!void {
1406 if (build_options.skip_non_native and builtin.object_format != .coff) {1413 if (build_options.skip_non_native and builtin.object_format != .coff) {
1407 @panic("Attempted to compile for object format that was disabled by build configuration");1414 @panic("Attempted to compile for object format that was disabled by build configuration");
1408 }1415 }
...@@ -1599,12 +1606,10 @@ pub fn flushModule(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod...@@ -1599,12 +1606,10 @@ pub fn flushModule(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
15991606
1600 // Most lazy symbols can be updated when the corresponding decl is,1607 // Most lazy symbols can be updated when the corresponding decl is,
1601 // so we only have to worry about the one without an associated decl.1608 // so we only have to worry about the one without an associated decl.
1602 if (self.lazy_syms.get(.none)) |metadata| {1609 self.updateLazySymbol(null) catch |err| switch (err) {
1603 self.updateLazySymbol(.none, metadata) catch |err| switch (err) {1610 error.CodegenFail => return error.FlushFailure,
1604 error.CodegenFail => return error.FlushFailure,1611 else => |e| return e,
1605 else => |e| return e,1612 };
1606 };
1607 }
16081613
1609 const gpa = self.base.allocator;1614 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...@@ -1034,12 +1034,10 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10341034
1035 // Most lazy symbols can be updated when the corresponding decl is,1035 // Most lazy symbols can be updated when the corresponding decl is,
1036 // so we only have to worry about the one without an associated decl.1036 // so we only have to worry about the one without an associated decl.
1037 if (self.lazy_syms.get(.none)) |metadata| {1037 self.updateLazySymbol(null) catch |err| switch (err) {
1038 self.updateLazySymbol(.none, metadata) catch |err| switch (err) {1038 error.CodegenFail => return error.FlushFailure,
1039 error.CodegenFail => return error.FlushFailure,1039 else => |e| return e,
1040 else => |e| return e,1040 };
1041 };
1042 }
10431041
1044 // TODO This linker code currently assumes there is only 1 compilation unit and it1042 // TODO This linker code currently assumes there is only 1 compilation unit and it
1045 // corresponds to the Zig source code.1043 // corresponds to the Zig source code.
...@@ -2579,7 +2577,11 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven...@@ -2579,7 +2577,11 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven
2579 return self.updateDeclExports(module, decl_index, module.getDeclExports(decl_index));2577 return self.updateDeclExports(module, decl_index, module.getDeclExports(decl_index));
2580}2578}
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 {
2583 if (build_options.skip_non_native and builtin.object_format != .elf) {2585 if (build_options.skip_non_native and builtin.object_format != .elf) {
2584 @panic("Attempted to compile for object format that was disabled by build configuration");2586 @panic("Attempted to compile for object format that was disabled by build configuration");
2585 }2587 }
...@@ -2590,6 +2592,8 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v...@@ -2590,6 +2592,8 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v
2590 const tracy = trace(@src());2592 const tracy = trace(@src());
2591 defer tracy.end();2593 defer tracy.end();
25922594
2595 try self.updateLazySymbol(decl_index);
2596
2593 const decl = module.declPtr(decl_index);2597 const decl = module.declPtr(decl_index);
25942598
2595 if (decl.val.tag() == .extern_fn) {2599 if (decl.val.tag() == .extern_fn) {
...@@ -2656,7 +2660,8 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v...@@ -2656,7 +2660,8 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v
2656 return self.updateDeclExports(module, decl_index, module.getDeclExports(decl_index));2660 return self.updateDeclExports(module, decl_index, module.getDeclExports(decl_index));
2657}2661}
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;
2660 const mod = self.base.options.module.?;2665 const mod = self.base.options.module.?;
2661 if (metadata.text_atom) |atom| try self.updateLazySymbolAtom(2666 if (metadata.text_atom) |atom| try self.updateLazySymbolAtom(
2662 File.LazySymbol.initDecl(.code, decl, mod),2667 File.LazySymbol.initDecl(.code, decl, mod),
...@@ -2810,7 +2815,7 @@ pub fn updateDeclExports(...@@ -2810,7 +2815,7 @@ pub fn updateDeclExports(
2810 module: *Module,2815 module: *Module,
2811 decl_index: Module.Decl.Index,2816 decl_index: Module.Decl.Index,
2812 exports: []const *Module.Export,2817 exports: []const *Module.Export,
2813) !void {2818) File.UpdateDeclExportsError!void {
2814 if (build_options.skip_non_native and builtin.object_format != .elf) {2819 if (build_options.skip_non_native and builtin.object_format != .elf) {
2815 @panic("Attempted to compile for object format that was disabled by build configuration");2820 @panic("Attempted to compile for object format that was disabled by build configuration");
2816 }2821 }
src/link/MachO.zig+9-8
...@@ -495,12 +495,10 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No...@@ -495,12 +495,10 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
495495
496 // Most lazy symbols can be updated when the corresponding decl is,496 // Most lazy symbols can be updated when the corresponding decl is,
497 // so we only have to worry about the one without an associated decl.497 // so we only have to worry about the one without an associated decl.
498 if (self.lazy_syms.get(.none)) |metadata| {498 self.updateLazySymbol(null) catch |err| switch (err) {
499 self.updateLazySymbol(.none, metadata) catch |err| switch (err) {499 error.CodegenFail => return error.FlushFailure,
500 error.CodegenFail => return error.FlushFailure,500 else => |e| return e,
501 else => |e| return e,501 };
502 };
503 }
504502
505 const module = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented;503 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)...@@ -1962,6 +1960,8 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)
1962 const tracy = trace(@src());1960 const tracy = trace(@src());
1963 defer tracy.end();1961 defer tracy.end();
19641962
1963 try self.updateLazySymbol(decl_index);
1964
1965 const decl = module.declPtr(decl_index);1965 const decl = module.declPtr(decl_index);
19661966
1967 if (decl.val.tag() == .extern_fn) {1967 if (decl.val.tag() == .extern_fn) {
...@@ -2036,7 +2036,8 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)...@@ -2036,7 +2036,8 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)
2036 try self.updateDeclExports(module, decl_index, module.getDeclExports(decl_index));2036 try self.updateDeclExports(module, decl_index, module.getDeclExports(decl_index));
2037}2037}
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;
2040 const mod = self.base.options.module.?;2041 const mod = self.base.options.module.?;
2041 if (metadata.text_atom) |atom| try self.updateLazySymbolAtom(2042 if (metadata.text_atom) |atom| try self.updateLazySymbolAtom(
2042 File.LazySymbol.initDecl(.code, decl, mod),2043 File.LazySymbol.initDecl(.code, decl, mod),
...@@ -2353,7 +2354,7 @@ pub fn updateDeclExports(...@@ -2353,7 +2354,7 @@ pub fn updateDeclExports(
2353 module: *Module,2354 module: *Module,
2354 decl_index: Module.Decl.Index,2355 decl_index: Module.Decl.Index,
2355 exports: []const *Module.Export,2356 exports: []const *Module.Export,
2356) !void {2357) File.UpdateDeclExportsError!void {
2357 if (build_options.skip_non_native and builtin.object_format != .macho) {2358 if (build_options.skip_non_native and builtin.object_format != .macho) {
2358 @panic("Attempted to compile for object format that was disabled by build configuration");2359 @panic("Attempted to compile for object format that was disabled by build configuration");
2359 }2360 }