diff options
Diffstat (limited to 'src/link')
| -rw-r--r-- | src/link/C.zig | 53 | ||||
| -rw-r--r-- | src/link/Coff.zig | 18 | ||||
| -rw-r--r-- | src/link/Elf.zig | 64 | ||||
| -rw-r--r-- | src/link/MachO.zig | 128 | ||||
| -rw-r--r-- | src/link/MachO/Archive.zig | 12 | ||||
| -rw-r--r-- | src/link/MachO/DebugSymbols.zig | 40 | ||||
| -rw-r--r-- | src/link/MachO/Dylib.zig | 6 | ||||
| -rw-r--r-- | src/link/MachO/Zld.zig | 43 | ||||
| -rw-r--r-- | src/link/SpirV.zig | 8 | ||||
| -rw-r--r-- | src/link/Wasm.zig | 14 |
10 files changed, 199 insertions, 187 deletions
diff --git a/src/link/C.zig b/src/link/C.zig index 5fb62c20af..6cb219db41 100644 --- a/src/link/C.zig +++ b/src/link/C.zig @@ -70,8 +70,8 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio } pub fn deinit(self: *C) void { - for (self.decl_table.items()) |entry| { - self.freeDecl(entry.key); + for (self.decl_table.keys()) |key| { + deinitDecl(self.base.allocator, key); } self.decl_table.deinit(self.base.allocator); } @@ -80,13 +80,17 @@ pub fn allocateDeclIndexes(self: *C, decl: *Module.Decl) !void {} pub fn freeDecl(self: *C, decl: *Module.Decl) void { _ = self.decl_table.swapRemove(decl); - decl.link.c.code.deinit(self.base.allocator); - decl.fn_link.c.fwd_decl.deinit(self.base.allocator); - var it = decl.fn_link.c.typedefs.iterator(); - while (it.next()) |some| { - self.base.allocator.free(some.value.rendered); + deinitDecl(self.base.allocator, decl); +} + +fn deinitDecl(gpa: *Allocator, decl: *Module.Decl) void { + decl.link.c.code.deinit(gpa); + decl.fn_link.c.fwd_decl.deinit(gpa); + var it = decl.fn_link.c.typedefs.valueIterator(); + while (it.next()) |value| { + gpa.free(value.rendered); } - decl.fn_link.c.typedefs.deinit(self.base.allocator); + decl.fn_link.c.typedefs.deinit(gpa); } pub fn updateDecl(self: *C, module: *Module, decl: *Module.Decl) !void { @@ -101,9 +105,9 @@ pub fn updateDecl(self: *C, module: *Module, decl: *Module.Decl) !void { const code = &decl.link.c.code; fwd_decl.shrinkRetainingCapacity(0); { - var it = typedefs.iterator(); - while (it.next()) |entry| { - module.gpa.free(entry.value.rendered); + var it = typedefs.valueIterator(); + while (it.next()) |value| { + module.gpa.free(value.rendered); } } typedefs.clearRetainingCapacity(); @@ -128,9 +132,9 @@ pub fn updateDecl(self: *C, module: *Module, decl: *Module.Decl) !void { object.blocks.deinit(module.gpa); object.code.deinit(); object.dg.fwd_decl.deinit(); - var it = object.dg.typedefs.iterator(); - while (it.next()) |some| { - module.gpa.free(some.value.rendered); + var it = object.dg.typedefs.valueIterator(); + while (it.next()) |value| { + module.gpa.free(value.rendered); } object.dg.typedefs.deinit(); } @@ -194,31 +198,30 @@ pub fn flushModule(self: *C, comp: *Compilation) !void { if (module.global_error_set.size == 0) break :render_errors; var it = module.global_error_set.iterator(); while (it.next()) |entry| { - try err_typedef_writer.print("#define zig_error_{s} {d}\n", .{ entry.key, entry.value }); + try err_typedef_writer.print("#define zig_error_{s} {d}\n", .{ entry.key_ptr.*, entry.value_ptr.* }); } try err_typedef_writer.writeByte('\n'); } var fn_count: usize = 0; - var typedefs = std.HashMap(Type, []const u8, Type.hash, Type.eql, std.hash_map.default_max_load_percentage).init(comp.gpa); + var typedefs = std.HashMap(Type, []const u8, Type.HashContext, std.hash_map.default_max_load_percentage).init(comp.gpa); defer typedefs.deinit(); // Typedefs, forward decls and non-functions first. // TODO: performance investigation: would keeping a list of Decls that we should // generate, rather than querying here, be faster? - for (self.decl_table.items()) |kv| { - const decl = kv.key; + for (self.decl_table.keys()) |decl| { if (!decl.has_tv) continue; const buf = buf: { if (decl.val.castTag(.function)) |_| { var it = decl.fn_link.c.typedefs.iterator(); while (it.next()) |new| { - if (typedefs.get(new.key)) |previous| { - try err_typedef_writer.print("typedef {s} {s};\n", .{ previous, new.value.name }); + if (typedefs.get(new.key_ptr.*)) |previous| { + try err_typedef_writer.print("typedef {s} {s};\n", .{ previous, new.value_ptr.name }); } else { try typedefs.ensureCapacity(typedefs.capacity() + 1); - try err_typedef_writer.writeAll(new.value.rendered); - typedefs.putAssumeCapacityNoClobber(new.key, new.value.name); + try err_typedef_writer.writeAll(new.value_ptr.rendered); + typedefs.putAssumeCapacityNoClobber(new.key_ptr.*, new.value_ptr.name); } } fn_count += 1; @@ -242,8 +245,7 @@ pub fn flushModule(self: *C, comp: *Compilation) !void { // Now the function bodies. try all_buffers.ensureCapacity(all_buffers.items.len + fn_count); - for (self.decl_table.items()) |kv| { - const decl = kv.key; + for (self.decl_table.keys()) |decl| { if (!decl.has_tv) continue; if (decl.val.castTag(.function)) |_| { const buf = decl.link.c.code.items; @@ -278,8 +280,7 @@ pub fn flushEmitH(module: *Module) !void { .iov_len = zig_h.len, }); - for (emit_h.decl_table.items()) |kv| { - const decl = kv.key; + for (emit_h.decl_table.keys()) |decl| { const decl_emit_h = decl.getEmitH(module); const buf = decl_emit_h.fwd_decl.items; all_buffers.appendAssumeCapacity(.{ diff --git a/src/link/Coff.zig b/src/link/Coff.zig index 611250c943..983e78e9e9 100644 --- a/src/link/Coff.zig +++ b/src/link/Coff.zig @@ -735,7 +735,7 @@ pub fn updateDeclExports(self: *Coff, module: *Module, decl: *Module.Decl, expor for (exports) |exp| { if (exp.options.section) |section_name| { if (!mem.eql(u8, section_name, ".text")) { - try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1); + try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.count() + 1); module.failed_exports.putAssumeCapacityNoClobber( exp, try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(), "Unimplemented: ExportOptions.section", .{}), @@ -746,7 +746,7 @@ pub fn updateDeclExports(self: *Coff, module: *Module, decl: *Module.Decl, expor if (mem.eql(u8, exp.options.name, "_start")) { self.entry_addr = decl.link.coff.getVAddr(self.*) - default_image_base; } else { - try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1); + try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.count() + 1); module.failed_exports.putAssumeCapacityNoClobber( exp, try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(), "Unimplemented: Exports other than '_start'", .{}), @@ -861,8 +861,8 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void { self.base.releaseLock(); try man.addListOfFiles(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - _ = try man.addFile(entry.key.status.success.object_path, null); + for (comp.c_object_table.keys()) |key| { + _ = try man.addFile(key.status.success.object_path, null); } try man.addOptionalFile(module_obj_path); man.hash.addOptional(self.base.options.stack_size_override); @@ -928,7 +928,7 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void { break :blk self.base.options.objects[0]; if (comp.c_object_table.count() != 0) - break :blk comp.c_object_table.items()[0].key.status.success.object_path; + break :blk comp.c_object_table.keys()[0].status.success.object_path; if (module_obj_path) |p| break :blk p; @@ -1026,8 +1026,8 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void { try argv.appendSlice(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - try argv.append(entry.key.status.success.object_path); + for (comp.c_object_table.keys()) |key| { + try argv.append(key.status.success.object_path); } if (module_obj_path) |p| { @@ -1221,8 +1221,8 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void { try argv.append(comp.compiler_rt_static_lib.?.full_object_path); } - for (self.base.options.system_libs.items()) |entry| { - const lib_basename = try allocPrint(arena, "{s}.lib", .{entry.key}); + for (self.base.options.system_libs.keys()) |key| { + const lib_basename = try allocPrint(arena, "{s}.lib", .{key}); if (comp.crt_files.get(lib_basename)) |crt_file| { try argv.append(crt_file.full_object_path); } else { diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 599d6fa2bf..5d99a4c3f6 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -1318,8 +1318,8 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { try man.addOptionalFile(self.base.options.linker_script); try man.addOptionalFile(self.base.options.version_script); try man.addListOfFiles(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - _ = try man.addFile(entry.key.status.success.object_path, null); + for (comp.c_object_table.keys()) |key| { + _ = try man.addFile(key.status.success.object_path, null); } try man.addOptionalFile(module_obj_path); try man.addOptionalFile(compiler_rt_path); @@ -1394,7 +1394,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { break :blk self.base.options.objects[0]; if (comp.c_object_table.count() != 0) - break :blk comp.c_object_table.items()[0].key.status.success.object_path; + break :blk comp.c_object_table.keys()[0].status.success.object_path; if (module_obj_path) |p| break :blk p; @@ -1518,8 +1518,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { var test_path = std.ArrayList(u8).init(self.base.allocator); defer test_path.deinit(); for (self.base.options.lib_dirs) |lib_dir_path| { - for (self.base.options.system_libs.items()) |entry| { - const link_lib = entry.key; + for (self.base.options.system_libs.keys()) |link_lib| { test_path.shrinkRetainingCapacity(0); const sep = fs.path.sep_str; try test_path.writer().print("{s}" ++ sep ++ "lib{s}.so", .{ lib_dir_path, link_lib }); @@ -1568,8 +1567,8 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { // Positional arguments to the linker such as object files. try argv.appendSlice(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - try argv.append(entry.key.status.success.object_path); + for (comp.c_object_table.keys()) |key| { + try argv.append(key.status.success.object_path); } if (module_obj_path) |p| { @@ -1598,10 +1597,9 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void { // Shared libraries. if (is_exe_or_dyn_lib) { - const system_libs = self.base.options.system_libs.items(); + const system_libs = self.base.options.system_libs.keys(); try argv.ensureCapacity(argv.items.len + system_libs.len); - for (system_libs) |entry| { - const link_lib = entry.key; + for (system_libs) |link_lib| { // By this time, we depend on these libs being dynamically linked libraries and not static libraries // (the check for that needs to be earlier), but they could be full paths to .so files, in which // case we want to avoid prepending "-l". @@ -2168,9 +2166,9 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void { var dbg_info_type_relocs: File.DbgInfoTypeRelocsTable = .{}; defer { - var it = dbg_info_type_relocs.iterator(); - while (it.next()) |entry| { - entry.value.relocs.deinit(self.base.allocator); + var it = dbg_info_type_relocs.valueIterator(); + while (it.next()) |value| { + value.relocs.deinit(self.base.allocator); } dbg_info_type_relocs.deinit(self.base.allocator); } @@ -2235,12 +2233,12 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void { if (fn_ret_has_bits) { const gop = try dbg_info_type_relocs.getOrPut(self.base.allocator, fn_ret_type); if (!gop.found_existing) { - gop.entry.value = .{ + gop.value_ptr.* = .{ .off = undefined, .relocs = .{}, }; } - try gop.entry.value.relocs.append(self.base.allocator, @intCast(u32, dbg_info_buffer.items.len)); + try gop.value_ptr.relocs.append(self.base.allocator, @intCast(u32, dbg_info_buffer.items.len)); dbg_info_buffer.items.len += 4; // DW.AT_type, DW.FORM_ref4 } dbg_info_buffer.appendSliceAssumeCapacity(decl_name_with_null); // DW.AT_name, DW.FORM_string @@ -2448,24 +2446,28 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void { // Now we emit the .debug_info types of the Decl. These will count towards the size of // the buffer, so we have to do it before computing the offset, and we can't perform the actual // relocations yet. - var it = dbg_info_type_relocs.iterator(); - while (it.next()) |entry| { - entry.value.off = @intCast(u32, dbg_info_buffer.items.len); - try self.addDbgInfoType(entry.key, &dbg_info_buffer); + { + var it = dbg_info_type_relocs.iterator(); + while (it.next()) |entry| { + entry.value_ptr.off = @intCast(u32, dbg_info_buffer.items.len); + try self.addDbgInfoType(entry.key_ptr.*, &dbg_info_buffer); + } } try self.updateDeclDebugInfoAllocation(text_block, @intCast(u32, dbg_info_buffer.items.len)); - // Now that we have the offset assigned we can finally perform type relocations. - it = dbg_info_type_relocs.iterator(); - while (it.next()) |entry| { - for (entry.value.relocs.items) |off| { - mem.writeInt( - u32, - dbg_info_buffer.items[off..][0..4], - text_block.dbg_info_off + entry.value.off, - target_endian, - ); + { + // Now that we have the offset assigned we can finally perform type relocations. + var it = dbg_info_type_relocs.valueIterator(); + while (it.next()) |value| { + for (value.relocs.items) |off| { + mem.writeInt( + u32, + dbg_info_buffer.items[off..][0..4], + text_block.dbg_info_off + value.off, + target_endian, + ); + } } } @@ -2636,7 +2638,7 @@ pub fn updateDeclExports( for (exports) |exp| { if (exp.options.section) |section_name| { if (!mem.eql(u8, section_name, ".text")) { - try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1); + try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.count() + 1); module.failed_exports.putAssumeCapacityNoClobber( exp, try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(), "Unimplemented: ExportOptions.section", .{}), @@ -2654,7 +2656,7 @@ pub fn updateDeclExports( }, .Weak => elf.STB_WEAK, .LinkOnce => { - try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1); + try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.count() + 1); module.failed_exports.putAssumeCapacityNoClobber( exp, try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(), "Unimplemented: GlobalLinkage.LinkOnce", .{}), diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 8785005a93..d4d7de15e4 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -567,8 +567,8 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { try man.addOptionalFile(self.base.options.linker_script); try man.addOptionalFile(self.base.options.version_script); try man.addListOfFiles(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - _ = try man.addFile(entry.key.status.success.object_path, null); + for (comp.c_object_table.keys()) |key| { + _ = try man.addFile(key.status.success.object_path, null); } try man.addOptionalFile(module_obj_path); // We can skip hashing libc and libc++ components that we are in charge of building from Zig @@ -632,7 +632,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { break :blk self.base.options.objects[0]; if (comp.c_object_table.count() != 0) - break :blk comp.c_object_table.items()[0].key.status.success.object_path; + break :blk comp.c_object_table.keys()[0].status.success.object_path; if (module_obj_path) |p| break :blk p; @@ -682,8 +682,8 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { try positionals.appendSlice(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - try positionals.append(entry.key.status.success.object_path); + for (comp.c_object_table.keys()) |key| { + try positionals.append(key.status.success.object_path); } if (module_obj_path) |p| { @@ -702,9 +702,8 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { var libs = std.ArrayList([]const u8).init(arena); var search_lib_names = std.ArrayList([]const u8).init(arena); - const system_libs = self.base.options.system_libs.items(); - for (system_libs) |entry| { - const link_lib = entry.key; + const system_libs = self.base.options.system_libs.keys(); + for (system_libs) |link_lib| { // By this time, we depend on these libs being dynamically linked libraries and not static libraries // (the check for that needs to be earlier), but they could be full paths to .dylib files, in which // case we want to avoid prepending "-l". @@ -804,8 +803,8 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { var rpaths = std.ArrayList([]const u8).init(arena); try rpaths.ensureCapacity(rpath_table.count()); - for (rpath_table.items()) |entry| { - rpaths.appendAssumeCapacity(entry.key); + for (rpath_table.keys()) |*key| { + rpaths.appendAssumeCapacity(key.*); } if (self.base.options.verbose_link) { @@ -973,8 +972,8 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { // Positional arguments to the linker such as object files. try argv.appendSlice(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - try argv.append(entry.key.status.success.object_path); + for (comp.c_object_table.keys()) |key| { + try argv.append(key.status.success.object_path); } if (module_obj_path) |p| { try argv.append(p); @@ -986,10 +985,9 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { } // Shared libraries. - const system_libs = self.base.options.system_libs.items(); + const system_libs = self.base.options.system_libs.keys(); try argv.ensureCapacity(argv.items.len + system_libs.len); - for (system_libs) |entry| { - const link_lib = entry.key; + for (system_libs) |link_lib| { // By this time, we depend on these libs being dynamically linked libraries and not static libraries // (the check for that needs to be earlier), but they could be full paths to .dylib files, in which // case we want to avoid prepending "-l". @@ -1153,12 +1151,12 @@ pub fn deinit(self: *MachO) void { if (self.d_sym) |*ds| { ds.deinit(self.base.allocator); } - for (self.lazy_imports.items()) |*entry| { - self.base.allocator.free(entry.key); + for (self.lazy_imports.keys()) |*key| { + self.base.allocator.free(key.*); } self.lazy_imports.deinit(self.base.allocator); - for (self.nonlazy_imports.items()) |*entry| { - self.base.allocator.free(entry.key); + for (self.nonlazy_imports.keys()) |*key| { + self.base.allocator.free(key.*); } self.nonlazy_imports.deinit(self.base.allocator); self.pie_fixups.deinit(self.base.allocator); @@ -1167,9 +1165,9 @@ pub fn deinit(self: *MachO) void { self.offset_table.deinit(self.base.allocator); self.offset_table_free_list.deinit(self.base.allocator); { - var it = self.string_table_directory.iterator(); - while (it.next()) |entry| { - self.base.allocator.free(entry.key); + var it = self.string_table_directory.keyIterator(); + while (it.next()) |key| { + self.base.allocator.free(key.*); } } self.string_table_directory.deinit(self.base.allocator); @@ -1318,9 +1316,9 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void { if (debug_buffers) |*dbg| { dbg.dbg_line_buffer.deinit(); dbg.dbg_info_buffer.deinit(); - var it = dbg.dbg_info_type_relocs.iterator(); - while (it.next()) |entry| { - entry.value.relocs.deinit(self.base.allocator); + var it = dbg.dbg_info_type_relocs.valueIterator(); + while (it.next()) |value| { + value.relocs.deinit(self.base.allocator); } dbg.dbg_info_type_relocs.deinit(self.base.allocator); } @@ -1543,7 +1541,7 @@ pub fn updateDeclExports( if (exp.options.section) |section_name| { if (!mem.eql(u8, section_name, "__text")) { - try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1); + try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.count() + 1); module.failed_exports.putAssumeCapacityNoClobber( exp, try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(), "Unimplemented: ExportOptions.section", .{}), @@ -1578,7 +1576,7 @@ pub fn updateDeclExports( n_desc |= macho.N_WEAK_DEF; }, .LinkOnce => { - try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1); + try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.count() + 1); module.failed_exports.putAssumeCapacityNoClobber( exp, try Module.ErrorMsg.create(self.base.allocator, decl.srcLoc(), "Unimplemented: GlobalLinkage.LinkOnce", .{}), @@ -2259,7 +2257,7 @@ pub fn populateMissingMetadata(self: *MachO) !void { self.load_commands_dirty = true; } if (!self.nonlazy_imports.contains("dyld_stub_binder")) { - const index = @intCast(u32, self.nonlazy_imports.items().len); + const index = @intCast(u32, self.nonlazy_imports.count()); const name = try self.base.allocator.dupe(u8, "dyld_stub_binder"); const offset = try self.makeString("dyld_stub_binder"); try self.nonlazy_imports.putNoClobber(self.base.allocator, name, .{ @@ -2440,7 +2438,7 @@ fn updateString(self: *MachO, old_str_off: u32, new_name: []const u8) !u32 { } pub fn addExternSymbol(self: *MachO, name: []const u8) !u32 { - const index = @intCast(u32, self.lazy_imports.items().len); + const index = @intCast(u32, self.lazy_imports.count()); const offset = try self.makeString(name); const sym_name = try self.base.allocator.dupe(u8, name); const dylib_ordinal = 1; // TODO this is now hardcoded, since we only support libSystem. @@ -2627,7 +2625,7 @@ fn writeOffsetTableEntry(self: *MachO, index: usize) !void { break :blk self.locals.items[got_entry.symbol]; }, .Extern => { - break :blk self.nonlazy_imports.items()[got_entry.symbol].value.symbol; + break :blk self.nonlazy_imports.values()[got_entry.symbol].symbol; }, } }; @@ -2910,7 +2908,7 @@ fn relocateSymbolTable(self: *MachO) !void { const symtab = &self.load_commands.items[self.symtab_cmd_index.?].Symtab; const nlocals = self.locals.items.len; const nglobals = self.globals.items.len; - const nundefs = self.lazy_imports.items().len + self.nonlazy_imports.items().len; + const nundefs = self.lazy_imports.count() + self.nonlazy_imports.count(); const nsyms = nlocals + nglobals + nundefs; if (symtab.nsyms < nsyms) { @@ -2957,15 +2955,15 @@ fn writeAllGlobalAndUndefSymbols(self: *MachO) !void { const nlocals = self.locals.items.len; const nglobals = self.globals.items.len; - const nundefs = self.lazy_imports.items().len + self.nonlazy_imports.items().len; + const nundefs = self.lazy_imports.count() + self.nonlazy_imports.count(); var undefs = std.ArrayList(macho.nlist_64).init(self.base.allocator); defer undefs.deinit(); try undefs.ensureCapacity(nundefs); - for (self.lazy_imports.items()) |entry| { - undefs.appendAssumeCapacity(entry.value.symbol); + for (self.lazy_imports.values()) |*value| { + undefs.appendAssumeCapacity(value.symbol); } - for (self.nonlazy_imports.items()) |entry| { - undefs.appendAssumeCapacity(entry.value.symbol); + for (self.nonlazy_imports.values()) |*value| { + undefs.appendAssumeCapacity(value.symbol); } const locals_off = symtab.symoff; @@ -3005,10 +3003,10 @@ fn writeIndirectSymbolTable(self: *MachO) !void { const la_symbol_ptr = &data_segment.sections.items[self.la_symbol_ptr_section_index.?]; const dysymtab = &self.load_commands.items[self.dysymtab_cmd_index.?].Dysymtab; - const lazy = self.lazy_imports.items(); + const lazy_count = self.lazy_imports.count(); const got_entries = self.offset_table.items; const allocated_size = self.allocatedSizeLinkedit(dysymtab.indirectsymoff); - const nindirectsyms = @intCast(u32, lazy.len * 2 + got_entries.len); + const nindirectsyms = @intCast(u32, lazy_count * 2 + got_entries.len); const needed_size = @intCast(u32, nindirectsyms * @sizeOf(u32)); if (needed_size > allocated_size) { @@ -3027,12 +3025,15 @@ fn writeIndirectSymbolTable(self: *MachO) !void { var writer = stream.writer(); stubs.reserved1 = 0; - for (lazy) |_, i| { - const symtab_idx = @intCast(u32, dysymtab.iundefsym + i); - try writer.writeIntLittle(u32, symtab_idx); + { + var i: usize = 0; + while (i < lazy_count) : (i += 1) { + const symtab_idx = @intCast(u32, dysymtab.iundefsym + i); + try writer.writeIntLittle(u32, symtab_idx); + } } - const base_id = @intCast(u32, lazy.len); + const base_id = @intCast(u32, lazy_count); got.reserved1 = base_id; for (got_entries) |entry| { switch (entry.kind) { @@ -3047,9 +3048,12 @@ fn writeIndirectSymbolTable(self: *MachO) !void { } la_symbol_ptr.reserved1 = got.reserved1 + @intCast(u32, got_entries.len); - for (lazy) |_, i| { - const symtab_idx = @intCast(u32, dysymtab.iundefsym + i); - try writer.writeIntLittle(u32, symtab_idx); + { + var i: usize = 0; + while (i < lazy_count) : (i += 1) { + const symtab_idx = @intCast(u32, dysymtab.iundefsym + i); + try writer.writeIntLittle(u32, symtab_idx); + } } try self.base.file.?.pwriteAll(buf, dysymtab.indirectsymoff); @@ -3183,15 +3187,15 @@ fn writeRebaseInfoTable(self: *MachO) !void { } if (self.la_symbol_ptr_section_index) |idx| { - try pointers.ensureCapacity(pointers.items.len + self.lazy_imports.items().len); + try pointers.ensureCapacity(pointers.items.len + self.lazy_imports.count()); const seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment; const sect = seg.sections.items[idx]; const base_offset = sect.addr - seg.inner.vmaddr; const segment_id = self.data_segment_cmd_index.?; - for (self.lazy_imports.items()) |entry| { + for (self.lazy_imports.values()) |*value| { pointers.appendAssumeCapacity(.{ - .offset = base_offset + entry.value.index * @sizeOf(u64), + .offset = base_offset + value.index * @sizeOf(u64), .segment_id = segment_id, }); } @@ -3241,12 +3245,13 @@ fn writeBindingInfoTable(self: *MachO) !void { for (self.offset_table.items) |entry| { if (entry.kind == .Local) continue; - const import = self.nonlazy_imports.items()[entry.symbol]; + const import_key = self.nonlazy_imports.keys()[entry.symbol]; + const import_ordinal = self.nonlazy_imports.values()[entry.symbol].dylib_ordinal; try pointers.append(.{ .offset = base_offset + entry.index * @sizeOf(u64), .segment_id = segment_id, - .dylib_ordinal = import.value.dylib_ordinal, - .name = import.key, + .dylib_ordinal = import_ordinal, + .name = import_key, }); } } @@ -3286,18 +3291,21 @@ fn writeLazyBindingInfoTable(self: *MachO) !void { defer pointers.deinit(); if (self.la_symbol_ptr_section_index) |idx| { - try pointers.ensureCapacity(self.lazy_imports.items().len); + try pointers.ensureCapacity(self.lazy_imports.count()); const seg = self.load_commands.items[self.data_segment_cmd_index.?].Segment; const sect = seg.sections.items[idx]; const base_offset = sect.addr - seg.inner.vmaddr; const segment_id = @intCast(u16, self.data_segment_cmd_index.?); - for (self.lazy_imports.items()) |entry| { + const slice = self.lazy_imports.entries.slice(); + const keys = slice.items(.key); + const values = slice.items(.value); + for (keys) |*key, i| { pointers.appendAssumeCapacity(.{ - .offset = base_offset + entry.value.index * @sizeOf(u64), + .offset = base_offset + values[i].index * @sizeOf(u64), .segment_id = segment_id, - .dylib_ordinal = entry.value.dylib_ordinal, - .name = entry.key, + .dylib_ordinal = values[i].dylib_ordinal, + .name = key.*, }); } } @@ -3329,7 +3337,7 @@ fn writeLazyBindingInfoTable(self: *MachO) !void { } fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void { - if (self.lazy_imports.items().len == 0) return; + if (self.lazy_imports.count() == 0) return; var stream = std.io.fixedBufferStream(buffer); var reader = stream.reader(); @@ -3375,7 +3383,7 @@ fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void { else => {}, } } - assert(self.lazy_imports.items().len <= offsets.items.len); + assert(self.lazy_imports.count() <= offsets.items.len); const stub_size: u4 = switch (self.base.options.target.cpu.arch) { .x86_64 => 10, @@ -3388,9 +3396,9 @@ fn populateLazyBindOffsetsInStubHelper(self: *MachO, buffer: []const u8) !void { else => unreachable, }; var buf: [@sizeOf(u32)]u8 = undefined; - for (self.lazy_imports.items()) |_, i| { + for (offsets.items[0..self.lazy_imports.count()]) |offset, i| { const placeholder_off = self.stub_helper_stubs_start_off.? + i * stub_size + off; - mem.writeIntLittle(u32, &buf, offsets.items[i]); + mem.writeIntLittle(u32, &buf, offset); try self.base.file.?.pwriteAll(&buf, placeholder_off); } } diff --git a/src/link/MachO/Archive.zig b/src/link/MachO/Archive.zig index f519aa27be..f47228077c 100644 --- a/src/link/MachO/Archive.zig +++ b/src/link/MachO/Archive.zig @@ -92,9 +92,11 @@ pub fn init(allocator: *Allocator) Archive { } pub fn deinit(self: *Archive) void { - for (self.toc.items()) |*entry| { - self.allocator.free(entry.key); - entry.value.deinit(self.allocator); + for (self.toc.keys()) |*key| { + self.allocator.free(key.*); + } + for (self.toc.values()) |*value| { + value.deinit(self.allocator); } self.toc.deinit(self.allocator); @@ -187,10 +189,10 @@ fn parseTableOfContents(self: *Archive, reader: anytype) !void { defer if (res.found_existing) self.allocator.free(owned_name); if (!res.found_existing) { - res.entry.value = .{}; + res.value_ptr.* = .{}; } - try res.entry.value.append(self.allocator, object_offset); + try res.value_ptr.append(self.allocator, object_offset); } } diff --git a/src/link/MachO/DebugSymbols.zig b/src/link/MachO/DebugSymbols.zig index 7c653d1983..ca6e5157b3 100644 --- a/src/link/MachO/DebugSymbols.zig +++ b/src/link/MachO/DebugSymbols.zig @@ -997,12 +997,12 @@ pub fn initDeclDebugBuffers( if (fn_ret_has_bits) { const gop = try dbg_info_type_relocs.getOrPut(allocator, fn_ret_type); if (!gop.found_existing) { - gop.entry.value = .{ + gop.value_ptr.* = .{ .off = undefined, .relocs = .{}, }; } - try gop.entry.value.relocs.append(allocator, @intCast(u32, dbg_info_buffer.items.len)); + try gop.value_ptr.relocs.append(allocator, @intCast(u32, dbg_info_buffer.items.len)); dbg_info_buffer.items.len += 4; // DW.AT_type, DW.FORM_ref4 } dbg_info_buffer.appendSliceAssumeCapacity(decl_name_with_null); // DW.AT_name, DW.FORM_string @@ -1158,26 +1158,30 @@ pub fn commitDeclDebugInfo( if (dbg_info_buffer.items.len == 0) return; - // Now we emit the .debug_info types of the Decl. These will count towards the size of - // the buffer, so we have to do it before computing the offset, and we can't perform the actual - // relocations yet. - var it = dbg_info_type_relocs.iterator(); - while (it.next()) |entry| { - entry.value.off = @intCast(u32, dbg_info_buffer.items.len); - try self.addDbgInfoType(entry.key, dbg_info_buffer, target); + { + // Now we emit the .debug_info types of the Decl. These will count towards the size of + // the buffer, so we have to do it before computing the offset, and we can't perform the actual + // relocations yet. + var it = dbg_info_type_relocs.iterator(); + while (it.next()) |entry| { + entry.value_ptr.off = @intCast(u32, dbg_info_buffer.items.len); + try self.addDbgInfoType(entry.key_ptr.*, dbg_info_buffer, target); + } } try self.updateDeclDebugInfoAllocation(allocator, text_block, @intCast(u32, dbg_info_buffer.items.len)); - // Now that we have the offset assigned we can finally perform type relocations. - it = dbg_info_type_relocs.iterator(); - while (it.next()) |entry| { - for (entry.value.relocs.items) |off| { - mem.writeIntLittle( - u32, - dbg_info_buffer.items[off..][0..4], - text_block.dbg_info_off + entry.value.off, - ); + { + // Now that we have the offset assigned we can finally perform type relocations. + var it = dbg_info_type_relocs.valueIterator(); + while (it.next()) |value| { + for (value.relocs.items) |off| { + mem.writeIntLittle( + u32, + dbg_info_buffer.items[off..][0..4], + text_block.dbg_info_off + value.off, + ); + } } } diff --git a/src/link/MachO/Dylib.zig b/src/link/MachO/Dylib.zig index d7039f7f6c..a2a703dc08 100644 --- a/src/link/MachO/Dylib.zig +++ b/src/link/MachO/Dylib.zig @@ -50,9 +50,9 @@ pub fn deinit(self: *Dylib) void { } self.load_commands.deinit(self.allocator); - for (self.symbols.items()) |entry| { - entry.value.deinit(self.allocator); - self.allocator.destroy(entry.value); + for (self.symbols.values()) |value| { + value.deinit(self.allocator); + self.allocator.destroy(value); } self.symbols.deinit(self.allocator); diff --git a/src/link/MachO/Zld.zig b/src/link/MachO/Zld.zig index e1ad69b925..c5362b8d7d 100644 --- a/src/link/MachO/Zld.zig +++ b/src/link/MachO/Zld.zig @@ -168,9 +168,9 @@ pub fn deinit(self: *Zld) void { self.strtab.deinit(self.allocator); { - var it = self.strtab_dir.iterator(); - while (it.next()) |entry| { - self.allocator.free(entry.key); + var it = self.strtab_dir.keyIterator(); + while (it.next()) |key| { + self.allocator.free(key.*); } } self.strtab_dir.deinit(self.allocator); @@ -954,9 +954,8 @@ fn sortSections(self: *Zld) !void { } } - var it = self.mappings.iterator(); - while (it.next()) |entry| { - const mapping = &entry.value; + var it = self.mappings.valueIterator(); + while (it.next()) |mapping| { if (self.text_segment_cmd_index.? == mapping.target_seg_id) { const new_index = text_index_mapping.get(mapping.target_sect_id) orelse unreachable; mapping.target_sect_id = new_index; @@ -1400,16 +1399,16 @@ fn resolveSymbolsInObject(self: *Zld, object: *Object) !void { if (sym.cast(Symbol.Regular)) |reg| { if (reg.linkage == .translation_unit) continue; // Symbol local to TU. - if (self.unresolved.swapRemove(sym.name)) |entry| { + if (self.unresolved.fetchSwapRemove(sym.name)) |kv| { // Create link to the global. - entry.value.alias = sym; + kv.value.alias = sym; } - const entry = self.globals.getEntry(sym.name) orelse { + const sym_ptr = self.globals.getPtr(sym.name) orelse { // Put new global symbol into the symbol table. try self.globals.putNoClobber(self.allocator, sym.name, sym); continue; }; - const g_sym = entry.value; + const g_sym = sym_ptr.*; const g_reg = g_sym.cast(Symbol.Regular) orelse unreachable; switch (g_reg.linkage) { @@ -1432,7 +1431,7 @@ fn resolveSymbolsInObject(self: *Zld, object: *Object) !void { } g_sym.alias = sym; - entry.value = sym; + sym_ptr.* = sym; } else if (sym.cast(Symbol.Unresolved)) |und| { if (self.globals.get(sym.name)) |g_sym| { sym.alias = g_sym; @@ -1458,8 +1457,7 @@ fn resolveSymbols(self: *Zld) !void { while (true) { if (next_sym == self.unresolved.count()) break; - const entry = self.unresolved.items()[next_sym]; - const sym = entry.value; + const sym = self.unresolved.values()[next_sym]; var reset: bool = false; for (self.archives.items) |archive| { @@ -1492,8 +1490,8 @@ fn resolveSymbols(self: *Zld) !void { defer unresolved.deinit(); try unresolved.ensureCapacity(self.unresolved.count()); - for (self.unresolved.items()) |entry| { - unresolved.appendAssumeCapacity(entry.value); + for (self.unresolved.values()) |value| { + unresolved.appendAssumeCapacity(value); } self.unresolved.clearAndFree(self.allocator); @@ -2780,8 +2778,7 @@ fn writeSymbolTable(self: *Zld) !void { var undefs = std.ArrayList(macho.nlist_64).init(self.allocator); defer undefs.deinit(); - for (self.imports.items()) |entry| { - const sym = entry.value; + for (self.imports.values()) |sym| { const ordinal = ordinal: { const dylib = sym.cast(Symbol.Proxy).?.dylib orelse break :ordinal 1; // TODO handle libSystem break :ordinal dylib.ordinal.?; @@ -3071,9 +3068,9 @@ pub fn parseName(name: *const [16]u8) []const u8 { fn printSymbols(self: *Zld) void { log.debug("globals", .{}); - for (self.globals.items()) |entry| { - const sym = entry.value.cast(Symbol.Regular) orelse unreachable; - log.debug(" | {s} @ {*}", .{ sym.base.name, entry.value }); + for (self.globals.values()) |value| { + const sym = value.cast(Symbol.Regular) orelse unreachable; + log.debug(" | {s} @ {*}", .{ sym.base.name, value }); log.debug(" => alias of {*}", .{sym.base.alias}); log.debug(" => linkage {s}", .{sym.linkage}); log.debug(" => defined in {s}", .{sym.file.name.?}); @@ -3091,9 +3088,9 @@ fn printSymbols(self: *Zld) void { } } log.debug("proxies", .{}); - for (self.imports.items()) |entry| { - const sym = entry.value.cast(Symbol.Proxy) orelse unreachable; - log.debug(" | {s} @ {*}", .{ sym.base.name, entry.value }); + for (self.imports.values()) |value| { + const sym = value.cast(Symbol.Proxy) orelse unreachable; + log.debug(" | {s} @ {*}", .{ sym.base.name, value }); log.debug(" => alias of {*}", .{sym.base.alias}); log.debug(" => defined in libSystem.B.dylib", .{}); } diff --git a/src/link/SpirV.zig b/src/link/SpirV.zig index a84a777e61..f80e4aec01 100644 --- a/src/link/SpirV.zig +++ b/src/link/SpirV.zig @@ -114,7 +114,7 @@ pub fn updateDeclExports( ) !void {} pub fn freeDecl(self: *SpirV, decl: *Module.Decl) void { - self.decl_table.removeAssertDiscard(decl); + assert(self.decl_table.swapRemove(decl)); } pub fn flush(self: *SpirV, comp: *Compilation) !void { @@ -141,8 +141,7 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { // declarations which don't generate a result? // TODO: fn_link is used here, but thats probably not the right field. It will work anyway though. { - for (self.decl_table.items()) |entry| { - const decl = entry.key; + for (self.decl_table.keys()) |decl| { if (!decl.has_tv) continue; decl.fn_link.spirv.id = spv.allocResultId(); @@ -154,8 +153,7 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { var decl_gen = codegen.DeclGen.init(&spv); defer decl_gen.deinit(); - for (self.decl_table.items()) |entry| { - const decl = entry.key; + for (self.decl_table.keys()) |decl| { if (!decl.has_tv) continue; if (try decl_gen.gen(decl)) |msg| { diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index fe72017327..77283384be 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -422,8 +422,8 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { const header_offset = try reserveVecSectionHeader(file); const writer = file.writer(); var count: u32 = 0; - for (module.decl_exports.entries.items) |entry| { - for (entry.value) |exprt| { + for (module.decl_exports.values()) |exports| { + for (exports) |exprt| { // Export name length + name try leb.writeULEB128(writer, @intCast(u32, exprt.options.name.len)); try writer.writeAll(exprt.options.name); @@ -590,8 +590,8 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { self.base.releaseLock(); try man.addListOfFiles(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - _ = try man.addFile(entry.key.status.success.object_path, null); + for (comp.c_object_table.keys()) |key| { + _ = try man.addFile(key.status.success.object_path, null); } try man.addOptionalFile(module_obj_path); try man.addOptionalFile(compiler_rt_path); @@ -638,7 +638,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { break :blk self.base.options.objects[0]; if (comp.c_object_table.count() != 0) - break :blk comp.c_object_table.items()[0].key.status.success.object_path; + break :blk comp.c_object_table.keys()[0].status.success.object_path; if (module_obj_path) |p| break :blk p; @@ -712,8 +712,8 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { // Positional arguments to the linker such as object files. try argv.appendSlice(self.base.options.objects); - for (comp.c_object_table.items()) |entry| { - try argv.append(entry.key.status.success.object_path); + for (comp.c_object_table.keys()) |key| { + try argv.append(key.status.success.object_path); } if (module_obj_path) |p| { try argv.append(p); |
