diff options
| author | Jacob Young <jacobly0@users.noreply.github.com> | 2024-06-15 16:10:53 -0400 |
|---|---|---|
| committer | Jacob Young <jacobly0@users.noreply.github.com> | 2024-07-07 22:59:52 -0400 |
| commit | 525f341f33af9b8aad53931fd5511f00a82cb090 (patch) | |
| tree | cec3280498c1122858580946ac5e31f8feb807ce /src/link/Elf | |
| parent | 8f20e81b8816aadd8ceb1b04bd3727cc1d124464 (diff) | |
| download | zig-525f341f33af9b8aad53931fd5511f00a82cb090.tar.gz zig-525f341f33af9b8aad53931fd5511f00a82cb090.zip | |
Zcu: introduce `PerThread` and pass to all the functions
Diffstat (limited to 'src/link/Elf')
| -rw-r--r-- | src/link/Elf/ZigObject.zig | 115 |
1 files changed, 60 insertions, 55 deletions
diff --git a/src/link/Elf/ZigObject.zig b/src/link/Elf/ZigObject.zig index 56311dd64b..8cfa5e701f 100644 --- a/src/link/Elf/ZigObject.zig +++ b/src/link/Elf/ZigObject.zig @@ -158,16 +158,17 @@ pub fn deinit(self: *ZigObject, allocator: Allocator) void { } } -pub fn flushModule(self: *ZigObject, elf_file: *Elf) !void { +pub fn flushModule(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !void { // Handle any lazy symbols that were emitted by incremental compilation. if (self.lazy_syms.getPtr(.none)) |metadata| { - const zcu = elf_file.base.comp.module.?; + const pt: Zcu.PerThread = .{ .zcu = elf_file.base.comp.module.?, .tid = tid }; // Most lazy symbols can be updated on first use, but // anyerror needs to wait for everything to be flushed. if (metadata.text_state != .unused) self.updateLazySymbol( elf_file, - link.File.LazySymbol.initDecl(.code, null, zcu), + pt, + link.File.LazySymbol.initDecl(.code, null, pt.zcu), metadata.text_symbol_index, ) catch |err| return switch (err) { error.CodegenFail => error.FlushFailure, @@ -175,7 +176,8 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf) !void { }; if (metadata.rodata_state != .unused) self.updateLazySymbol( elf_file, - link.File.LazySymbol.initDecl(.const_data, null, zcu), + pt, + link.File.LazySymbol.initDecl(.const_data, null, pt.zcu), metadata.rodata_symbol_index, ) catch |err| return switch (err) { error.CodegenFail => error.FlushFailure, @@ -188,8 +190,8 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf) !void { } if (self.dwarf) |*dw| { - const zcu = elf_file.base.comp.module.?; - try dw.flushModule(zcu); + const pt: Zcu.PerThread = .{ .zcu = elf_file.base.comp.module.?, .tid = tid }; + try dw.flushModule(pt); // TODO I need to re-think how to handle ZigObject's debug sections AND debug sections // extracted from input object files correctly. @@ -202,7 +204,7 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf) !void { const text_shdr = elf_file.shdrs.items[elf_file.zig_text_section_index.?]; const low_pc = text_shdr.sh_addr; const high_pc = text_shdr.sh_addr + text_shdr.sh_size; - try dw.writeDbgInfoHeader(zcu, low_pc, high_pc); + try dw.writeDbgInfoHeader(pt.zcu, low_pc, high_pc); self.debug_info_header_dirty = false; } @@ -684,6 +686,7 @@ pub fn getAnonDeclVAddr( pub fn lowerAnonDecl( self: *ZigObject, elf_file: *Elf, + pt: Zcu.PerThread, decl_val: InternPool.Index, explicit_alignment: InternPool.Alignment, src_loc: Module.LazySrcLoc, @@ -692,7 +695,7 @@ pub fn lowerAnonDecl( const mod = elf_file.base.comp.module.?; const ty = Type.fromInterned(mod.intern_pool.typeOf(decl_val)); const decl_alignment = switch (explicit_alignment) { - .none => ty.abiAlignment(mod), + .none => ty.abiAlignment(pt), else => explicit_alignment, }; if (self.anon_decls.get(decl_val)) |metadata| { @@ -708,6 +711,7 @@ pub fn lowerAnonDecl( }) catch unreachable; const res = self.lowerConst( elf_file, + pt, name, val, decl_alignment, @@ -733,10 +737,11 @@ pub fn lowerAnonDecl( pub fn getOrCreateMetadataForLazySymbol( self: *ZigObject, elf_file: *Elf, + pt: Zcu.PerThread, lazy_sym: link.File.LazySymbol, ) !Symbol.Index { - const gpa = elf_file.base.comp.gpa; - const mod = elf_file.base.comp.module.?; + const mod = pt.zcu; + const gpa = mod.gpa; const gop = try self.lazy_syms.getOrPut(gpa, lazy_sym.getDecl(mod)); errdefer _ = if (!gop.found_existing) self.lazy_syms.pop(); if (!gop.found_existing) gop.value_ptr.* = .{}; @@ -766,7 +771,7 @@ pub fn getOrCreateMetadataForLazySymbol( metadata.state.* = .pending_flush; const symbol_index = metadata.symbol_index.*; // anyerror needs to be deferred until flushModule - if (lazy_sym.getDecl(mod) != .none) try self.updateLazySymbol(elf_file, lazy_sym, symbol_index); + if (lazy_sym.getDecl(mod) != .none) try self.updateLazySymbol(elf_file, pt, lazy_sym, symbol_index); return symbol_index; } @@ -893,6 +898,7 @@ fn getDeclShdrIndex( fn updateDeclCode( self: *ZigObject, elf_file: *Elf, + pt: Zcu.PerThread, decl_index: InternPool.DeclIndex, sym_index: Symbol.Index, shdr_index: u32, @@ -900,13 +906,13 @@ fn updateDeclCode( stt_bits: u8, ) !void { const gpa = elf_file.base.comp.gpa; - const mod = elf_file.base.comp.module.?; + const mod = pt.zcu; const decl = mod.declPtr(decl_index); const decl_name = try decl.fullyQualifiedName(mod); log.debug("updateDeclCode {}{*}", .{ decl_name.fmt(&mod.intern_pool), decl }); - const required_alignment = decl.getAlignment(mod).max( + const required_alignment = decl.getAlignment(pt).max( target_util.minFunctionAlignment(mod.getTarget()), ); @@ -994,19 +1000,20 @@ fn updateDeclCode( fn updateTlv( self: *ZigObject, elf_file: *Elf, + pt: Zcu.PerThread, decl_index: InternPool.DeclIndex, sym_index: Symbol.Index, shndx: u32, code: []const u8, ) !void { - const gpa = elf_file.base.comp.gpa; - const mod = elf_file.base.comp.module.?; + const mod = pt.zcu; + const gpa = mod.gpa; const decl = mod.declPtr(decl_index); const decl_name = try decl.fullyQualifiedName(mod); log.debug("updateTlv {} ({*})", .{ decl_name.fmt(&mod.intern_pool), decl }); - const required_alignment = decl.getAlignment(mod); + const required_alignment = decl.getAlignment(pt); const sym = elf_file.symbol(sym_index); const esym = &self.local_esyms.items(.elf_sym)[sym.esym_index]; @@ -1048,7 +1055,7 @@ fn updateTlv( pub fn updateFunc( self: *ZigObject, elf_file: *Elf, - mod: *Module, + pt: Zcu.PerThread, func_index: InternPool.Index, air: Air, liveness: Liveness, @@ -1056,6 +1063,7 @@ pub fn updateFunc( const tracy = trace(@src()); defer tracy.end(); + const mod = pt.zcu; const gpa = elf_file.base.comp.gpa; const func = mod.funcInfo(func_index); const decl_index = func.owner_decl; @@ -1068,29 +1076,19 @@ pub fn updateFunc( var code_buffer = std.ArrayList(u8).init(gpa); defer code_buffer.deinit(); - var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(mod, decl_index) else null; + var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(pt, decl_index) else null; defer if (decl_state) |*ds| ds.deinit(); - const res = if (decl_state) |*ds| - try codegen.generateFunction( - &elf_file.base, - decl.navSrcLoc(mod), - func_index, - air, - liveness, - &code_buffer, - .{ .dwarf = ds }, - ) - else - try codegen.generateFunction( - &elf_file.base, - decl.navSrcLoc(mod), - func_index, - air, - liveness, - &code_buffer, - .none, - ); + const res = try codegen.generateFunction( + &elf_file.base, + pt, + decl.navSrcLoc(mod), + func_index, + air, + liveness, + &code_buffer, + if (decl_state) |*ds| .{ .dwarf = ds } else .none, + ); const code = switch (res) { .ok => code_buffer.items, @@ -1102,12 +1100,12 @@ pub fn updateFunc( }; const shndx = try self.getDeclShdrIndex(elf_file, decl, code); - try self.updateDeclCode(elf_file, decl_index, sym_index, shndx, code, elf.STT_FUNC); + try self.updateDeclCode(elf_file, pt, decl_index, sym_index, shndx, code, elf.STT_FUNC); if (decl_state) |*ds| { const sym = elf_file.symbol(sym_index); try self.dwarf.?.commitDeclState( - mod, + pt, decl_index, @intCast(sym.address(.{}, elf_file)), sym.atom(elf_file).?.size, @@ -1121,12 +1119,13 @@ pub fn updateFunc( pub fn updateDecl( self: *ZigObject, elf_file: *Elf, - mod: *Module, + pt: Zcu.PerThread, decl_index: InternPool.DeclIndex, ) link.File.UpdateDeclError!void { const tracy = trace(@src()); defer tracy.end(); + const mod = pt.zcu; const decl = mod.declPtr(decl_index); if (decl.val.getExternFunc(mod)) |_| { @@ -1150,19 +1149,19 @@ pub fn updateDecl( var code_buffer = std.ArrayList(u8).init(gpa); defer code_buffer.deinit(); - var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(mod, decl_index) else null; + var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(pt, decl_index) else null; defer if (decl_state) |*ds| ds.deinit(); // TODO implement .debug_info for global variables const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val; const res = if (decl_state) |*ds| - try codegen.generateSymbol(&elf_file.base, decl.navSrcLoc(mod), decl_val, &code_buffer, .{ + try codegen.generateSymbol(&elf_file.base, pt, decl.navSrcLoc(mod), decl_val, &code_buffer, .{ .dwarf = ds, }, .{ .parent_atom_index = sym_index, }) else - try codegen.generateSymbol(&elf_file.base, decl.navSrcLoc(mod), decl_val, &code_buffer, .none, .{ + try codegen.generateSymbol(&elf_file.base, pt, decl.navSrcLoc(mod), decl_val, &code_buffer, .none, .{ .parent_atom_index = sym_index, }); @@ -1177,14 +1176,14 @@ pub fn updateDecl( const shndx = try self.getDeclShdrIndex(elf_file, decl, code); if (elf_file.shdrs.items[shndx].sh_flags & elf.SHF_TLS != 0) - try self.updateTlv(elf_file, decl_index, sym_index, shndx, code) + try self.updateTlv(elf_file, pt, decl_index, sym_index, shndx, code) else - try self.updateDeclCode(elf_file, decl_index, sym_index, shndx, code, elf.STT_OBJECT); + try self.updateDeclCode(elf_file, pt, decl_index, sym_index, shndx, code, elf.STT_OBJECT); if (decl_state) |*ds| { const sym = elf_file.symbol(sym_index); try self.dwarf.?.commitDeclState( - mod, + pt, decl_index, @intCast(sym.address(.{}, elf_file)), sym.atom(elf_file).?.size, @@ -1198,11 +1197,12 @@ pub fn updateDecl( fn updateLazySymbol( self: *ZigObject, elf_file: *Elf, + pt: Zcu.PerThread, sym: link.File.LazySymbol, symbol_index: Symbol.Index, ) !void { - const gpa = elf_file.base.comp.gpa; - const mod = elf_file.base.comp.module.?; + const mod = pt.zcu; + const gpa = mod.gpa; var required_alignment: InternPool.Alignment = .none; var code_buffer = std.ArrayList(u8).init(gpa); @@ -1211,7 +1211,7 @@ fn updateLazySymbol( const name_str_index = blk: { const name = try std.fmt.allocPrint(gpa, "__lazy_{s}_{}", .{ @tagName(sym.kind), - sym.ty.fmt(mod), + sym.ty.fmt(pt), }); defer gpa.free(name); break :blk try self.strtab.insert(gpa, name); @@ -1220,6 +1220,7 @@ fn updateLazySymbol( const src = sym.ty.srcLocOrNull(mod) orelse Module.LazySrcLoc.unneeded; const res = try codegen.generateLazySymbol( &elf_file.base, + pt, src, sym, &required_alignment, @@ -1273,6 +1274,7 @@ fn updateLazySymbol( pub fn lowerUnnamedConst( self: *ZigObject, elf_file: *Elf, + pt: Zcu.PerThread, val: Value, decl_index: InternPool.DeclIndex, ) !u32 { @@ -1291,9 +1293,10 @@ pub fn lowerUnnamedConst( const ty = val.typeOf(mod); const sym_index = switch (try self.lowerConst( elf_file, + pt, name, val, - ty.abiAlignment(mod), + ty.abiAlignment(pt), elf_file.zig_data_rel_ro_section_index.?, decl.navSrcLoc(mod), )) { @@ -1318,20 +1321,21 @@ const LowerConstResult = union(enum) { fn lowerConst( self: *ZigObject, elf_file: *Elf, + pt: Zcu.PerThread, name: []const u8, val: Value, required_alignment: InternPool.Alignment, output_section_index: u32, src_loc: Module.LazySrcLoc, ) !LowerConstResult { - const gpa = elf_file.base.comp.gpa; + const gpa = pt.zcu.gpa; var code_buffer = std.ArrayList(u8).init(gpa); defer code_buffer.deinit(); const sym_index = try self.addAtom(elf_file); - const res = try codegen.generateSymbol(&elf_file.base, src_loc, val, &code_buffer, .{ + const res = try codegen.generateSymbol(&elf_file.base, pt, src_loc, val, &code_buffer, .{ .none = {}, }, .{ .parent_atom_index = sym_index, @@ -1373,13 +1377,14 @@ fn lowerConst( pub fn updateExports( self: *ZigObject, elf_file: *Elf, - mod: *Module, + pt: Zcu.PerThread, exported: Module.Exported, export_indices: []const u32, ) link.File.UpdateExportsError!void { const tracy = trace(@src()); defer tracy.end(); + const mod = pt.zcu; const gpa = elf_file.base.comp.gpa; const metadata = switch (exported) { .decl_index => |decl_index| blk: { @@ -1388,7 +1393,7 @@ pub fn updateExports( }, .value => |value| self.anon_decls.getPtr(value) orelse blk: { const first_exp = mod.all_exports.items[export_indices[0]]; - const res = try self.lowerAnonDecl(elf_file, value, .none, first_exp.src); + const res = try self.lowerAnonDecl(elf_file, pt, value, .none, first_exp.src); switch (res) { .ok => {}, .fail => |em| { |
