authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-08-30 03:06:52+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-08-30 03:06:52+01:00
logd997ddaa102bb9ba5f1e8480b8c78f7d102b5512
tree57905ad345e6e4e7cebdb4597b6790f073dfa0b1
parente9a00ba7f4ef2546cd0c98559002431c749374fe
parentc3fb30803f4fbe62abb4bfad348c585e9ab80234
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #21231 from mlugg/field-decl-name-conflict

AstGen: disallow fields and decls from sharing names

51 files changed, 1263 insertions(+), 1323 deletions(-)

CMakeLists.txt+2-2
......@@ -613,7 +613,7 @@ set(ZIG_STAGE2_SOURCES
613613 src/link/Elf/relocatable.zig
614614 src/link/Elf/relocation.zig
615615 src/link/Elf/synthetic_sections.zig
616 src/link/Elf/thunks.zig
616 src/link/Elf/Thunk.zig
617617 src/link/MachO.zig
618618 src/link/MachO/Archive.zig
619619 src/link/MachO/Atom.zig
......@@ -638,7 +638,7 @@ set(ZIG_STAGE2_SOURCES
638638 src/link/MachO/load_commands.zig
639639 src/link/MachO/relocatable.zig
640640 src/link/MachO/synthetic.zig
641 src/link/MachO/thunks.zig
641 src/link/MachO/Thunk.zig
642642 src/link/MachO/uuid.zig
643643 src/link/NvPtx.zig
644644 src/link/Plan9.zig
lib/std/crypto/poly1305.zig+5-5
......@@ -12,7 +12,7 @@ pub const Poly1305 = struct {
1212 // accumulated hash
1313 h: [3]u64 = [_]u64{ 0, 0, 0 },
1414 // random number added at the end (from the secret key)
15 pad: [2]u64,
15 end_pad: [2]u64,
1616 // how many bytes are waiting to be processed in a partial block
1717 leftover: usize = 0,
1818 // partial block buffer
......@@ -24,7 +24,7 @@ pub const Poly1305 = struct {
2424 mem.readInt(u64, key[0..8], .little) & 0x0ffffffc0fffffff,
2525 mem.readInt(u64, key[8..16], .little) & 0x0ffffffc0ffffffc,
2626 },
27 .pad = [_]u64{
27 .end_pad = [_]u64{
2828 mem.readInt(u64, key[16..24], .little),
2929 mem.readInt(u64, key[24..32], .little),
3030 },
......@@ -177,9 +177,9 @@ pub const Poly1305 = struct {
177177 h1 ^= mask & (h1 ^ h_p1);
178178
179179 // Add the first half of the key, we intentionally don't use @addWithOverflow() here.
180 st.h[0] = h0 +% st.pad[0];
181 const c = ((h0 & st.pad[0]) | ((h0 | st.pad[0]) & ~st.h[0])) >> 63;
182 st.h[1] = h1 +% st.pad[1] +% c;
180 st.h[0] = h0 +% st.end_pad[0];
181 const c = ((h0 & st.end_pad[0]) | ((h0 | st.end_pad[0]) & ~st.h[0])) >> 63;
182 st.h[1] = h1 +% st.end_pad[1] +% c;
183183
184184 mem.writeInt(u64, out[0..8], st.h[0], .little);
185185 mem.writeInt(u64, out[8..16], st.h[1], .little);
lib/std/debug/Pdb.zig+61-62
......@@ -63,18 +63,18 @@ pub fn deinit(self: *Pdb) void {
6363}
6464
6565pub fn parseDbiStream(self: *Pdb) !void {
66 var stream = self.getStream(pdb.StreamType.Dbi) orelse
66 var stream = self.getStream(pdb.StreamType.dbi) orelse
6767 return error.InvalidDebugInfo;
6868 const reader = stream.reader();
6969
7070 const header = try reader.readStruct(std.pdb.DbiStreamHeader);
71 if (header.VersionHeader != 19990903) // V70, only value observed by LLVM team
71 if (header.version_header != 19990903) // V70, only value observed by LLVM team
7272 return error.UnknownPDBVersion;
7373 // if (header.Age != age)
7474 // return error.UnmatchingPDB;
7575
76 const mod_info_size = header.ModInfoSize;
77 const section_contrib_size = header.SectionContributionSize;
76 const mod_info_size = header.mod_info_size;
77 const section_contrib_size = header.section_contribution_size;
7878
7979 var modules = std.ArrayList(Module).init(self.allocator);
8080 errdefer modules.deinit();
......@@ -143,7 +143,7 @@ pub fn parseDbiStream(self: *Pdb) !void {
143143}
144144
145145pub fn parseInfoStream(self: *Pdb) !void {
146 var stream = self.getStream(pdb.StreamType.Pdb) orelse
146 var stream = self.getStream(pdb.StreamType.pdb) orelse
147147 return error.InvalidDebugInfo;
148148 const reader = stream.reader();
149149
......@@ -168,23 +168,23 @@ pub fn parseInfoStream(self: *Pdb) !void {
168168 try reader.readNoEof(name_bytes);
169169
170170 const HashTableHeader = extern struct {
171 Size: u32,
172 Capacity: u32,
171 size: u32,
172 capacity: u32,
173173
174174 fn maxLoad(cap: u32) u32 {
175175 return cap * 2 / 3 + 1;
176176 }
177177 };
178178 const hash_tbl_hdr = try reader.readStruct(HashTableHeader);
179 if (hash_tbl_hdr.Capacity == 0)
179 if (hash_tbl_hdr.capacity == 0)
180180 return error.InvalidDebugInfo;
181181
182 if (hash_tbl_hdr.Size > HashTableHeader.maxLoad(hash_tbl_hdr.Capacity))
182 if (hash_tbl_hdr.size > HashTableHeader.maxLoad(hash_tbl_hdr.capacity))
183183 return error.InvalidDebugInfo;
184184
185185 const present = try readSparseBitVector(&reader, self.allocator);
186186 defer self.allocator.free(present);
187 if (present.len != hash_tbl_hdr.Size)
187 if (present.len != hash_tbl_hdr.size)
188188 return error.InvalidDebugInfo;
189189 const deleted = try readSparseBitVector(&reader, self.allocator);
190190 defer self.allocator.free(deleted);
......@@ -212,19 +212,19 @@ pub fn getSymbolName(self: *Pdb, module: *Module, address: u64) ?[]const u8 {
212212
213213 var symbol_i: usize = 0;
214214 while (symbol_i != module.symbols.len) {
215 const prefix = @as(*align(1) pdb.RecordPrefix, @ptrCast(&module.symbols[symbol_i]));
216 if (prefix.RecordLen < 2)
215 const prefix: *align(1) pdb.RecordPrefix = @ptrCast(&module.symbols[symbol_i]);
216 if (prefix.record_len < 2)
217217 return null;
218 switch (prefix.RecordKind) {
219 .S_LPROC32, .S_GPROC32 => {
220 const proc_sym = @as(*align(1) pdb.ProcSym, @ptrCast(&module.symbols[symbol_i + @sizeOf(pdb.RecordPrefix)]));
221 if (address >= proc_sym.CodeOffset and address < proc_sym.CodeOffset + proc_sym.CodeSize) {
222 return std.mem.sliceTo(@as([*:0]u8, @ptrCast(&proc_sym.Name[0])), 0);
218 switch (prefix.record_kind) {
219 .lproc32, .gproc32 => {
220 const proc_sym: *align(1) pdb.ProcSym = @ptrCast(&module.symbols[symbol_i + @sizeOf(pdb.RecordPrefix)]);
221 if (address >= proc_sym.code_offset and address < proc_sym.code_offset + proc_sym.code_size) {
222 return std.mem.sliceTo(@as([*:0]u8, @ptrCast(&proc_sym.name[0])), 0);
223223 }
224224 },
225225 else => {},
226226 }
227 symbol_i += prefix.RecordLen + @sizeOf(u16);
227 symbol_i += prefix.record_len + @sizeOf(u16);
228228 }
229229
230230 return null;
......@@ -238,44 +238,44 @@ pub fn getLineNumberInfo(self: *Pdb, module: *Module, address: u64) !std.debug.S
238238 var skip_len: usize = undefined;
239239 const checksum_offset = module.checksum_offset orelse return error.MissingDebugInfo;
240240 while (sect_offset != subsect_info.len) : (sect_offset += skip_len) {
241 const subsect_hdr = @as(*align(1) pdb.DebugSubsectionHeader, @ptrCast(&subsect_info[sect_offset]));
242 skip_len = subsect_hdr.Length;
241 const subsect_hdr: *align(1) pdb.DebugSubsectionHeader = @ptrCast(&subsect_info[sect_offset]);
242 skip_len = subsect_hdr.length;
243243 sect_offset += @sizeOf(pdb.DebugSubsectionHeader);
244244
245 switch (subsect_hdr.Kind) {
246 .Lines => {
245 switch (subsect_hdr.kind) {
246 .lines => {
247247 var line_index = sect_offset;
248248
249 const line_hdr = @as(*align(1) pdb.LineFragmentHeader, @ptrCast(&subsect_info[line_index]));
250 if (line_hdr.RelocSegment == 0)
249 const line_hdr: *align(1) pdb.LineFragmentHeader = @ptrCast(&subsect_info[line_index]);
250 if (line_hdr.reloc_segment == 0)
251251 return error.MissingDebugInfo;
252252 line_index += @sizeOf(pdb.LineFragmentHeader);
253 const frag_vaddr_start = line_hdr.RelocOffset;
254 const frag_vaddr_end = frag_vaddr_start + line_hdr.CodeSize;
253 const frag_vaddr_start = line_hdr.reloc_offset;
254 const frag_vaddr_end = frag_vaddr_start + line_hdr.code_size;
255255
256256 if (address >= frag_vaddr_start and address < frag_vaddr_end) {
257257 // There is an unknown number of LineBlockFragmentHeaders (and their accompanying line and column records)
258258 // from now on. We will iterate through them, and eventually find a SourceLocation that we're interested in,
259259 // breaking out to :subsections. If not, we will make sure to not read anything outside of this subsection.
260 const subsection_end_index = sect_offset + subsect_hdr.Length;
260 const subsection_end_index = sect_offset + subsect_hdr.length;
261261
262262 while (line_index < subsection_end_index) {
263 const block_hdr = @as(*align(1) pdb.LineBlockFragmentHeader, @ptrCast(&subsect_info[line_index]));
263 const block_hdr: *align(1) pdb.LineBlockFragmentHeader = @ptrCast(&subsect_info[line_index]);
264264 line_index += @sizeOf(pdb.LineBlockFragmentHeader);
265265 const start_line_index = line_index;
266266
267 const has_column = line_hdr.Flags.LF_HaveColumns;
267 const has_column = line_hdr.flags.have_columns;
268268
269269 // All line entries are stored inside their line block by ascending start address.
270270 // Heuristic: we want to find the last line entry
271271 // that has a vaddr_start <= address.
272272 // This is done with a simple linear search.
273273 var line_i: u32 = 0;
274 while (line_i < block_hdr.NumLines) : (line_i += 1) {
275 const line_num_entry = @as(*align(1) pdb.LineNumberEntry, @ptrCast(&subsect_info[line_index]));
274 while (line_i < block_hdr.num_lines) : (line_i += 1) {
275 const line_num_entry: *align(1) pdb.LineNumberEntry = @ptrCast(&subsect_info[line_index]);
276276 line_index += @sizeOf(pdb.LineNumberEntry);
277277
278 const vaddr_start = frag_vaddr_start + line_num_entry.Offset;
278 const vaddr_start = frag_vaddr_start + line_num_entry.offset;
279279 if (address < vaddr_start) {
280280 break;
281281 }
......@@ -283,28 +283,27 @@ pub fn getLineNumberInfo(self: *Pdb, module: *Module, address: u64) !std.debug.S
283283
284284 // line_i == 0 would mean that no matching pdb.LineNumberEntry was found.
285285 if (line_i > 0) {
286 const subsect_index = checksum_offset + block_hdr.NameIndex;
287 const chksum_hdr = @as(*align(1) pdb.FileChecksumEntryHeader, @ptrCast(&module.subsect_info[subsect_index]));
288 const strtab_offset = @sizeOf(pdb.StringTableHeader) + chksum_hdr.FileNameOffset;
286 const subsect_index = checksum_offset + block_hdr.name_index;
287 const chksum_hdr: *align(1) pdb.FileChecksumEntryHeader = @ptrCast(&module.subsect_info[subsect_index]);
288 const strtab_offset = @sizeOf(pdb.StringTableHeader) + chksum_hdr.file_name_offset;
289289 try self.string_table.?.seekTo(strtab_offset);
290290 const source_file_name = try self.string_table.?.reader().readUntilDelimiterAlloc(self.allocator, 0, 1024);
291291
292292 const line_entry_idx = line_i - 1;
293293
294294 const column = if (has_column) blk: {
295 const start_col_index = start_line_index + @sizeOf(pdb.LineNumberEntry) * block_hdr.NumLines;
295 const start_col_index = start_line_index + @sizeOf(pdb.LineNumberEntry) * block_hdr.num_lines;
296296 const col_index = start_col_index + @sizeOf(pdb.ColumnNumberEntry) * line_entry_idx;
297 const col_num_entry = @as(*align(1) pdb.ColumnNumberEntry, @ptrCast(&subsect_info[col_index]));
298 break :blk col_num_entry.StartColumn;
297 const col_num_entry: *align(1) pdb.ColumnNumberEntry = @ptrCast(&subsect_info[col_index]);
298 break :blk col_num_entry.start_column;
299299 } else 0;
300300
301301 const found_line_index = start_line_index + line_entry_idx * @sizeOf(pdb.LineNumberEntry);
302302 const line_num_entry: *align(1) pdb.LineNumberEntry = @ptrCast(&subsect_info[found_line_index]);
303 const flags: *align(1) pdb.LineNumberEntry.Flags = @ptrCast(&line_num_entry.Flags);
304303
305304 return .{
306305 .file_name = source_file_name,
307 .line = flags.Start,
306 .line = line_num_entry.flags.start,
308307 .column = column,
309308 };
310309 }
......@@ -335,12 +334,12 @@ pub fn getModule(self: *Pdb, index: usize) !?*Module {
335334 return mod;
336335
337336 // At most one can be non-zero.
338 if (mod.mod_info.C11ByteSize != 0 and mod.mod_info.C13ByteSize != 0)
337 if (mod.mod_info.c11_byte_size != 0 and mod.mod_info.c13_byte_size != 0)
339338 return error.InvalidDebugInfo;
340 if (mod.mod_info.C13ByteSize == 0)
339 if (mod.mod_info.c13_byte_size == 0)
341340 return error.InvalidDebugInfo;
342341
343 const stream = self.getStreamById(mod.mod_info.ModuleSymStream) orelse
342 const stream = self.getStreamById(mod.mod_info.module_sym_stream) orelse
344343 return error.MissingDebugInfo;
345344 const reader = stream.reader();
346345
......@@ -348,23 +347,23 @@ pub fn getModule(self: *Pdb, index: usize) !?*Module {
348347 if (signature != 4)
349348 return error.InvalidDebugInfo;
350349
351 mod.symbols = try self.allocator.alloc(u8, mod.mod_info.SymByteSize - 4);
350 mod.symbols = try self.allocator.alloc(u8, mod.mod_info.sym_byte_size - 4);
352351 errdefer self.allocator.free(mod.symbols);
353352 try reader.readNoEof(mod.symbols);
354353
355 mod.subsect_info = try self.allocator.alloc(u8, mod.mod_info.C13ByteSize);
354 mod.subsect_info = try self.allocator.alloc(u8, mod.mod_info.c13_byte_size);
356355 errdefer self.allocator.free(mod.subsect_info);
357356 try reader.readNoEof(mod.subsect_info);
358357
359358 var sect_offset: usize = 0;
360359 var skip_len: usize = undefined;
361360 while (sect_offset != mod.subsect_info.len) : (sect_offset += skip_len) {
362 const subsect_hdr = @as(*align(1) pdb.DebugSubsectionHeader, @ptrCast(&mod.subsect_info[sect_offset]));
363 skip_len = subsect_hdr.Length;
361 const subsect_hdr: *align(1) pdb.DebugSubsectionHeader = @ptrCast(&mod.subsect_info[sect_offset]);
362 skip_len = subsect_hdr.length;
364363 sect_offset += @sizeOf(pdb.DebugSubsectionHeader);
365364
366 switch (subsect_hdr.Kind) {
367 .FileChecksums => {
365 switch (subsect_hdr.kind) {
366 .file_checksums => {
368367 mod.checksum_offset = sect_offset;
369368 break;
370369 },
......@@ -401,30 +400,30 @@ const Msf = struct {
401400 const superblock = try in.readStruct(pdb.SuperBlock);
402401
403402 // Sanity checks
404 if (!std.mem.eql(u8, &superblock.FileMagic, pdb.SuperBlock.file_magic))
403 if (!std.mem.eql(u8, &superblock.file_magic, pdb.SuperBlock.expect_magic))
405404 return error.InvalidDebugInfo;
406 if (superblock.FreeBlockMapBlock != 1 and superblock.FreeBlockMapBlock != 2)
405 if (superblock.free_block_map_block != 1 and superblock.free_block_map_block != 2)
407406 return error.InvalidDebugInfo;
408407 const file_len = try file.getEndPos();
409 if (superblock.NumBlocks * superblock.BlockSize != file_len)
408 if (superblock.num_blocks * superblock.block_size != file_len)
410409 return error.InvalidDebugInfo;
411 switch (superblock.BlockSize) {
410 switch (superblock.block_size) {
412411 // llvm only supports 4096 but we can handle any of these values
413412 512, 1024, 2048, 4096 => {},
414413 else => return error.InvalidDebugInfo,
415414 }
416415
417 const dir_block_count = blockCountFromSize(superblock.NumDirectoryBytes, superblock.BlockSize);
418 if (dir_block_count > superblock.BlockSize / @sizeOf(u32))
416 const dir_block_count = blockCountFromSize(superblock.num_directory_bytes, superblock.block_size);
417 if (dir_block_count > superblock.block_size / @sizeOf(u32))
419418 return error.UnhandledBigDirectoryStream; // cf. BlockMapAddr comment.
420419
421 try file.seekTo(superblock.BlockSize * superblock.BlockMapAddr);
420 try file.seekTo(superblock.block_size * superblock.block_map_addr);
422421 const dir_blocks = try allocator.alloc(u32, dir_block_count);
423422 for (dir_blocks) |*b| {
424423 b.* = try in.readInt(u32, .little);
425424 }
426425 var directory = MsfStream.init(
427 superblock.BlockSize,
426 superblock.block_size,
428427 file,
429428 dir_blocks,
430429 );
......@@ -440,7 +439,7 @@ const Msf = struct {
440439 const Nil = 0xFFFFFFFF;
441440 for (stream_sizes) |*s| {
442441 const size = try directory.reader().readInt(u32, .little);
443 s.* = if (size == Nil) 0 else blockCountFromSize(size, superblock.BlockSize);
442 s.* = if (size == Nil) 0 else blockCountFromSize(size, superblock.block_size);
444443 }
445444
446445 const streams = try allocator.alloc(MsfStream, stream_count);
......@@ -455,15 +454,15 @@ const Msf = struct {
455454 var j: u32 = 0;
456455 while (j < size) : (j += 1) {
457456 const block_id = try directory.reader().readInt(u32, .little);
458 const n = (block_id % superblock.BlockSize);
457 const n = (block_id % superblock.block_size);
459458 // 0 is for pdb.SuperBlock, 1 and 2 for FPMs.
460 if (block_id == 0 or n == 1 or n == 2 or block_id * superblock.BlockSize > file_len)
459 if (block_id == 0 or n == 1 or n == 2 or block_id * superblock.block_size > file_len)
461460 return error.InvalidBlockIndex;
462461 blocks[j] = block_id;
463462 }
464463
465464 stream.* = MsfStream.init(
466 superblock.BlockSize,
465 superblock.block_size,
467466 file,
468467 blocks,
469468 );
......@@ -471,7 +470,7 @@ const Msf = struct {
471470 }
472471
473472 const end = directory.pos;
474 if (end - begin != superblock.NumDirectoryBytes)
473 if (end - begin != superblock.num_directory_bytes)
475474 return error.InvalidStreamDirectory;
476475
477476 return Msf{
lib/std/debug/SelfInfo.zig+5-5
......@@ -732,14 +732,14 @@ pub const Module = switch (native_os) {
732732 fn getSymbolFromPdb(self: *@This(), relocated_address: usize) !?std.debug.Symbol {
733733 var coff_section: *align(1) const coff.SectionHeader = undefined;
734734 const mod_index = for (self.pdb.?.sect_contribs) |sect_contrib| {
735 if (sect_contrib.Section > self.coff_section_headers.len) continue;
735 if (sect_contrib.section > self.coff_section_headers.len) continue;
736736 // Remember that SectionContribEntry.Section is 1-based.
737 coff_section = &self.coff_section_headers[sect_contrib.Section - 1];
737 coff_section = &self.coff_section_headers[sect_contrib.section - 1];
738738
739 const vaddr_start = coff_section.virtual_address + sect_contrib.Offset;
740 const vaddr_end = vaddr_start + sect_contrib.Size;
739 const vaddr_start = coff_section.virtual_address + sect_contrib.offset;
740 const vaddr_end = vaddr_start + sect_contrib.size;
741741 if (relocated_address >= vaddr_start and relocated_address < vaddr_end) {
742 break sect_contrib.ModuleIndex;
742 break sect_contrib.module_index;
743743 }
744744 } else {
745745 // we have no information to add to the address
lib/std/enums.zig+1-1
......@@ -1501,7 +1501,7 @@ test values {
15011501 X,
15021502 Y,
15031503 Z,
1504 pub const X = 1;
1504 const A = 1;
15051505 };
15061506 try testing.expectEqualSlices(E, &.{ .X, .Y, .Z }, values(E));
15071507}
lib/std/heap/sbrk_allocator.zig+1-3
......@@ -7,7 +7,7 @@ const assert = std.debug.assert;
77
88pub fn SbrkAllocator(comptime sbrk: *const fn (n: usize) usize) type {
99 return struct {
10 pub const vtable = Allocator.VTable{
10 pub const vtable: Allocator.VTable = .{
1111 .alloc = alloc,
1212 .resize = resize,
1313 .free = free,
......@@ -15,8 +15,6 @@ pub fn SbrkAllocator(comptime sbrk: *const fn (n: usize) usize) type {
1515
1616 pub const Error = Allocator.Error;
1717
18 lock: std.Thread.Mutex = .{},
19
2018 const max_usize = math.maxInt(usize);
2119 const ushift = math.Log2Int(usize);
2220 const bigpage_size = 64 * 1024;
lib/std/meta.zig+2-2
......@@ -294,7 +294,7 @@ test declarations {
294294 pub fn a() void {}
295295 };
296296 const U1 = union {
297 a: u8,
297 b: u8,
298298
299299 pub fn a() void {}
300300 };
......@@ -334,7 +334,7 @@ test declarationInfo {
334334 pub fn a() void {}
335335 };
336336 const U1 = union {
337 a: u8,
337 b: u8,
338338
339339 pub fn a() void {}
340340 };
lib/std/os/uefi/device_path.zig+30-30
......@@ -4,38 +4,38 @@ const uefi = std.os.uefi;
44const Guid = uefi.Guid;
55
66pub const DevicePath = union(Type) {
7 Hardware: Hardware,
8 Acpi: Acpi,
9 Messaging: Messaging,
10 Media: Media,
11 BiosBootSpecification: BiosBootSpecification,
12 End: End,
7 hardware: Hardware,
8 acpi: Acpi,
9 messaging: Messaging,
10 media: Media,
11 bios_boot_specification: BiosBootSpecification,
12 end: End,
1313
1414 pub const Type = enum(u8) {
15 Hardware = 0x01,
16 Acpi = 0x02,
17 Messaging = 0x03,
18 Media = 0x04,
19 BiosBootSpecification = 0x05,
20 End = 0x7f,
15 hardware = 0x01,
16 acpi = 0x02,
17 messaging = 0x03,
18 media = 0x04,
19 bios_boot_specification = 0x05,
20 end = 0x7f,
2121 _,
2222 };
2323
2424 pub const Hardware = union(Subtype) {
25 Pci: *const PciDevicePath,
26 PcCard: *const PcCardDevicePath,
27 MemoryMapped: *const MemoryMappedDevicePath,
28 Vendor: *const VendorDevicePath,
29 Controller: *const ControllerDevicePath,
30 Bmc: *const BmcDevicePath,
25 pci: *const PciDevicePath,
26 pc_card: *const PcCardDevicePath,
27 memory_mapped: *const MemoryMappedDevicePath,
28 vendor: *const VendorDevicePath,
29 controller: *const ControllerDevicePath,
30 bmc: *const BmcDevicePath,
3131
3232 pub const Subtype = enum(u8) {
33 Pci = 1,
34 PcCard = 2,
35 MemoryMapped = 3,
36 Vendor = 4,
37 Controller = 5,
38 Bmc = 6,
33 pci = 1,
34 pc_card = 2,
35 memory_mapped = 3,
36 vendor = 4,
37 controller = 5,
38 bmc = 6,
3939 _,
4040 };
4141
......@@ -151,14 +151,14 @@ pub const DevicePath = union(Type) {
151151 };
152152
153153 pub const Acpi = union(Subtype) {
154 Acpi: *const BaseAcpiDevicePath,
155 ExpandedAcpi: *const ExpandedAcpiDevicePath,
156 Adr: *const AdrDevicePath,
154 acpi: *const BaseAcpiDevicePath,
155 expanded_acpi: *const ExpandedAcpiDevicePath,
156 adr: *const AdrDevicePath,
157157
158158 pub const Subtype = enum(u8) {
159 Acpi = 1,
160 ExpandedAcpi = 2,
161 Adr = 3,
159 acpi = 1,
160 expanded_acpi = 2,
161 adr = 3,
162162 _,
163163 };
164164
lib/std/pdb.zig+321-322
......@@ -20,297 +20,297 @@ const ArrayList = std.ArrayList;
2020
2121/// https://llvm.org/docs/PDB/DbiStream.html#stream-header
2222pub const DbiStreamHeader = extern struct {
23 VersionSignature: i32,
24 VersionHeader: u32,
25 Age: u32,
26 GlobalStreamIndex: u16,
27 BuildNumber: u16,
28 PublicStreamIndex: u16,
29 PdbDllVersion: u16,
30 SymRecordStream: u16,
31 PdbDllRbld: u16,
32 ModInfoSize: u32,
33 SectionContributionSize: u32,
34 SectionMapSize: u32,
35 SourceInfoSize: i32,
36 TypeServerSize: i32,
37 MFCTypeServerIndex: u32,
38 OptionalDbgHeaderSize: i32,
39 ECSubstreamSize: i32,
40 Flags: u16,
41 Machine: u16,
42 Padding: u32,
23 version_signature: i32,
24 version_header: u32,
25 age: u32,
26 global_stream_index: u16,
27 build_number: u16,
28 public_stream_index: u16,
29 pdb_dll_version: u16,
30 sym_record_stream: u16,
31 pdb_dll_rbld: u16,
32 mod_info_size: u32,
33 section_contribution_size: u32,
34 section_map_size: u32,
35 source_info_size: i32,
36 type_server_size: i32,
37 mfc_type_server_index: u32,
38 optional_dbg_header_size: i32,
39 ec_substream_size: i32,
40 flags: u16,
41 machine: u16,
42 padding: u32,
4343};
4444
4545pub const SectionContribEntry = extern struct {
4646 /// COFF Section index, 1-based
47 Section: u16,
48 Padding1: [2]u8,
49 Offset: u32,
50 Size: u32,
51 Characteristics: u32,
52 ModuleIndex: u16,
53 Padding2: [2]u8,
54 DataCrc: u32,
55 RelocCrc: u32,
47 section: u16,
48 padding1: [2]u8,
49 offset: u32,
50 size: u32,
51 characteristics: u32,
52 module_index: u16,
53 padding2: [2]u8,
54 data_crc: u32,
55 reloc_crc: u32,
5656};
5757
5858pub const ModInfo = extern struct {
59 Unused1: u32,
60 SectionContr: SectionContribEntry,
61 Flags: u16,
62 ModuleSymStream: u16,
63 SymByteSize: u32,
64 C11ByteSize: u32,
65 C13ByteSize: u32,
66 SourceFileCount: u16,
67 Padding: [2]u8,
68 Unused2: u32,
69 SourceFileNameIndex: u32,
70 PdbFilePathNameIndex: u32,
59 unused1: u32,
60 section_contr: SectionContribEntry,
61 flags: u16,
62 module_sym_stream: u16,
63 sym_byte_size: u32,
64 c11_byte_size: u32,
65 c13_byte_size: u32,
66 source_file_count: u16,
67 padding: [2]u8,
68 unused2: u32,
69 source_file_name_index: u32,
70 pdb_file_path_name_index: u32,
7171 // These fields are variable length
72 //ModuleName: char[],
73 //ObjFileName: char[],
72 //module_name: char[],
73 //obj_file_name: char[],
7474};
7575
7676pub const SectionMapHeader = extern struct {
7777 /// Number of segment descriptors
78 Count: u16,
78 count: u16,
7979
8080 /// Number of logical segment descriptors
81 LogCount: u16,
81 log_count: u16,
8282};
8383
8484pub const SectionMapEntry = extern struct {
8585 /// See the SectionMapEntryFlags enum below.
86 Flags: u16,
86 flags: u16,
8787
8888 /// Logical overlay number
89 Ovl: u16,
89 ovl: u16,
9090
9191 /// Group index into descriptor array.
92 Group: u16,
93 Frame: u16,
92 group: u16,
93 frame: u16,
9494
9595 /// Byte index of segment / group name in string table, or 0xFFFF.
96 SectionName: u16,
96 section_name: u16,
9797
9898 /// Byte index of class in string table, or 0xFFFF.
99 ClassName: u16,
99 class_name: u16,
100100
101101 /// Byte offset of the logical segment within physical segment. If group is set in flags, this is the offset of the group.
102 Offset: u32,
102 offset: u32,
103103
104104 /// Byte count of the segment or group.
105 SectionLength: u32,
105 section_length: u32,
106106};
107107
108108pub const StreamType = enum(u16) {
109 Pdb = 1,
110 Tpi = 2,
111 Dbi = 3,
112 Ipi = 4,
109 pdb = 1,
110 tpi = 2,
111 dbi = 3,
112 ipi = 4,
113113};
114114
115115/// Duplicate copy of SymbolRecordKind, but using the official CV names. Useful
116116/// for reference purposes and when dealing with unknown record types.
117117pub const SymbolKind = enum(u16) {
118 S_COMPILE = 1,
119 S_REGISTER_16t = 2,
120 S_CONSTANT_16t = 3,
121 S_UDT_16t = 4,
122 S_SSEARCH = 5,
123 S_SKIP = 7,
124 S_CVRESERVE = 8,
125 S_OBJNAME_ST = 9,
126 S_ENDARG = 10,
127 S_COBOLUDT_16t = 11,
128 S_MANYREG_16t = 12,
129 S_RETURN = 13,
130 S_ENTRYTHIS = 14,
131 S_BPREL16 = 256,
132 S_LDATA16 = 257,
133 S_GDATA16 = 258,
134 S_PUB16 = 259,
135 S_LPROC16 = 260,
136 S_GPROC16 = 261,
137 S_THUNK16 = 262,
138 S_BLOCK16 = 263,
139 S_WITH16 = 264,
140 S_LABEL16 = 265,
141 S_CEXMODEL16 = 266,
142 S_VFTABLE16 = 267,
143 S_REGREL16 = 268,
144 S_BPREL32_16t = 512,
145 S_LDATA32_16t = 513,
146 S_GDATA32_16t = 514,
147 S_PUB32_16t = 515,
148 S_LPROC32_16t = 516,
149 S_GPROC32_16t = 517,
150 S_THUNK32_ST = 518,
151 S_BLOCK32_ST = 519,
152 S_WITH32_ST = 520,
153 S_LABEL32_ST = 521,
154 S_CEXMODEL32 = 522,
155 S_VFTABLE32_16t = 523,
156 S_REGREL32_16t = 524,
157 S_LTHREAD32_16t = 525,
158 S_GTHREAD32_16t = 526,
159 S_SLINK32 = 527,
160 S_LPROCMIPS_16t = 768,
161 S_GPROCMIPS_16t = 769,
162 S_PROCREF_ST = 1024,
163 S_DATAREF_ST = 1025,
164 S_ALIGN = 1026,
165 S_LPROCREF_ST = 1027,
166 S_OEM = 1028,
167 S_TI16_MAX = 4096,
168 S_REGISTER_ST = 4097,
169 S_CONSTANT_ST = 4098,
170 S_UDT_ST = 4099,
171 S_COBOLUDT_ST = 4100,
172 S_MANYREG_ST = 4101,
173 S_BPREL32_ST = 4102,
174 S_LDATA32_ST = 4103,
175 S_GDATA32_ST = 4104,
176 S_PUB32_ST = 4105,
177 S_LPROC32_ST = 4106,
178 S_GPROC32_ST = 4107,
179 S_VFTABLE32 = 4108,
180 S_REGREL32_ST = 4109,
181 S_LTHREAD32_ST = 4110,
182 S_GTHREAD32_ST = 4111,
183 S_LPROCMIPS_ST = 4112,
184 S_GPROCMIPS_ST = 4113,
185 S_COMPILE2_ST = 4115,
186 S_MANYREG2_ST = 4116,
187 S_LPROCIA64_ST = 4117,
188 S_GPROCIA64_ST = 4118,
189 S_LOCALSLOT_ST = 4119,
190 S_PARAMSLOT_ST = 4120,
191 S_ANNOTATION = 4121,
192 S_GMANPROC_ST = 4122,
193 S_LMANPROC_ST = 4123,
194 S_RESERVED1 = 4124,
195 S_RESERVED2 = 4125,
196 S_RESERVED3 = 4126,
197 S_RESERVED4 = 4127,
198 S_LMANDATA_ST = 4128,
199 S_GMANDATA_ST = 4129,
200 S_MANFRAMEREL_ST = 4130,
201 S_MANREGISTER_ST = 4131,
202 S_MANSLOT_ST = 4132,
203 S_MANMANYREG_ST = 4133,
204 S_MANREGREL_ST = 4134,
205 S_MANMANYREG2_ST = 4135,
206 S_MANTYPREF = 4136,
207 S_UNAMESPACE_ST = 4137,
208 S_ST_MAX = 4352,
209 S_WITH32 = 4356,
210 S_MANYREG = 4362,
211 S_LPROCMIPS = 4372,
212 S_GPROCMIPS = 4373,
213 S_MANYREG2 = 4375,
214 S_LPROCIA64 = 4376,
215 S_GPROCIA64 = 4377,
216 S_LOCALSLOT = 4378,
217 S_PARAMSLOT = 4379,
218 S_MANFRAMEREL = 4382,
219 S_MANREGISTER = 4383,
220 S_MANSLOT = 4384,
221 S_MANMANYREG = 4385,
222 S_MANREGREL = 4386,
223 S_MANMANYREG2 = 4387,
224 S_UNAMESPACE = 4388,
225 S_DATAREF = 4390,
226 S_ANNOTATIONREF = 4392,
227 S_TOKENREF = 4393,
228 S_GMANPROC = 4394,
229 S_LMANPROC = 4395,
230 S_ATTR_FRAMEREL = 4398,
231 S_ATTR_REGISTER = 4399,
232 S_ATTR_REGREL = 4400,
233 S_ATTR_MANYREG = 4401,
234 S_SEPCODE = 4402,
235 S_LOCAL_2005 = 4403,
236 S_DEFRANGE_2005 = 4404,
237 S_DEFRANGE2_2005 = 4405,
238 S_DISCARDED = 4411,
239 S_LPROCMIPS_ID = 4424,
240 S_GPROCMIPS_ID = 4425,
241 S_LPROCIA64_ID = 4426,
242 S_GPROCIA64_ID = 4427,
243 S_DEFRANGE_HLSL = 4432,
244 S_GDATA_HLSL = 4433,
245 S_LDATA_HLSL = 4434,
246 S_LOCAL_DPC_GROUPSHARED = 4436,
247 S_DEFRANGE_DPC_PTR_TAG = 4439,
248 S_DPC_SYM_TAG_MAP = 4440,
249 S_ARMSWITCHTABLE = 4441,
250 S_POGODATA = 4444,
251 S_INLINESITE2 = 4445,
252 S_MOD_TYPEREF = 4447,
253 S_REF_MINIPDB = 4448,
254 S_PDBMAP = 4449,
255 S_GDATA_HLSL32 = 4450,
256 S_LDATA_HLSL32 = 4451,
257 S_GDATA_HLSL32_EX = 4452,
258 S_LDATA_HLSL32_EX = 4453,
259 S_FASTLINK = 4455,
260 S_INLINEES = 4456,
261 S_END = 6,
262 S_INLINESITE_END = 4430,
263 S_PROC_ID_END = 4431,
264 S_THUNK32 = 4354,
265 S_TRAMPOLINE = 4396,
266 S_SECTION = 4406,
267 S_COFFGROUP = 4407,
268 S_EXPORT = 4408,
269 S_LPROC32 = 4367,
270 S_GPROC32 = 4368,
271 S_LPROC32_ID = 4422,
272 S_GPROC32_ID = 4423,
273 S_LPROC32_DPC = 4437,
274 S_LPROC32_DPC_ID = 4438,
275 S_REGISTER = 4358,
276 S_PUB32 = 4366,
277 S_PROCREF = 4389,
278 S_LPROCREF = 4391,
279 S_ENVBLOCK = 4413,
280 S_INLINESITE = 4429,
281 S_LOCAL = 4414,
282 S_DEFRANGE = 4415,
283 S_DEFRANGE_SUBFIELD = 4416,
284 S_DEFRANGE_REGISTER = 4417,
285 S_DEFRANGE_FRAMEPOINTER_REL = 4418,
286 S_DEFRANGE_SUBFIELD_REGISTER = 4419,
287 S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE = 4420,
288 S_DEFRANGE_REGISTER_REL = 4421,
289 S_BLOCK32 = 4355,
290 S_LABEL32 = 4357,
291 S_OBJNAME = 4353,
292 S_COMPILE2 = 4374,
293 S_COMPILE3 = 4412,
294 S_FRAMEPROC = 4114,
295 S_CALLSITEINFO = 4409,
296 S_FILESTATIC = 4435,
297 S_HEAPALLOCSITE = 4446,
298 S_FRAMECOOKIE = 4410,
299 S_CALLEES = 4442,
300 S_CALLERS = 4443,
301 S_UDT = 4360,
302 S_COBOLUDT = 4361,
303 S_BUILDINFO = 4428,
304 S_BPREL32 = 4363,
305 S_REGREL32 = 4369,
306 S_CONSTANT = 4359,
307 S_MANCONSTANT = 4397,
308 S_LDATA32 = 4364,
309 S_GDATA32 = 4365,
310 S_LMANDATA = 4380,
311 S_GMANDATA = 4381,
312 S_LTHREAD32 = 4370,
313 S_GTHREAD32 = 4371,
118 compile = 1,
119 register_16t = 2,
120 constant_16t = 3,
121 udt_16t = 4,
122 ssearch = 5,
123 skip = 7,
124 cvreserve = 8,
125 objname_st = 9,
126 endarg = 10,
127 coboludt_16t = 11,
128 manyreg_16t = 12,
129 @"return" = 13,
130 entrythis = 14,
131 bprel16 = 256,
132 ldata16 = 257,
133 gdata16 = 258,
134 pub16 = 259,
135 lproc16 = 260,
136 gproc16 = 261,
137 thunk16 = 262,
138 block16 = 263,
139 with16 = 264,
140 label16 = 265,
141 cexmodel16 = 266,
142 vftable16 = 267,
143 regrel16 = 268,
144 bprel32_16t = 512,
145 ldata32_16t = 513,
146 gdata32_16t = 514,
147 pub32_16t = 515,
148 lproc32_16t = 516,
149 gproc32_16t = 517,
150 thunk32_st = 518,
151 block32_st = 519,
152 with32_st = 520,
153 label32_st = 521,
154 cexmodel32 = 522,
155 vftable32_16t = 523,
156 regrel32_16t = 524,
157 lthread32_16t = 525,
158 gthread32_16t = 526,
159 slink32 = 527,
160 lprocmips_16t = 768,
161 gprocmips_16t = 769,
162 procref_st = 1024,
163 dataref_st = 1025,
164 @"align" = 1026,
165 lprocref_st = 1027,
166 oem = 1028,
167 ti16_max = 4096,
168 register_st = 4097,
169 constant_st = 4098,
170 udt_st = 4099,
171 coboludt_st = 4100,
172 manyreg_st = 4101,
173 bprel32_st = 4102,
174 ldata32_st = 4103,
175 gdata32_st = 4104,
176 pub32_st = 4105,
177 lproc32_st = 4106,
178 gproc32_st = 4107,
179 vftable32 = 4108,
180 regrel32_st = 4109,
181 lthread32_st = 4110,
182 gthread32_st = 4111,
183 lprocmips_st = 4112,
184 gprocmips_st = 4113,
185 compile2_st = 4115,
186 manyreg2_st = 4116,
187 lprocia64_st = 4117,
188 gprocia64_st = 4118,
189 localslot_st = 4119,
190 paramslot_st = 4120,
191 annotation = 4121,
192 gmanproc_st = 4122,
193 lmanproc_st = 4123,
194 reserved1 = 4124,
195 reserved2 = 4125,
196 reserved3 = 4126,
197 reserved4 = 4127,
198 lmandata_st = 4128,
199 gmandata_st = 4129,
200 manframerel_st = 4130,
201 manregister_st = 4131,
202 manslot_st = 4132,
203 manmanyreg_st = 4133,
204 manregrel_st = 4134,
205 manmanyreg2_st = 4135,
206 mantypref = 4136,
207 unamespace_st = 4137,
208 st_max = 4352,
209 with32 = 4356,
210 manyreg = 4362,
211 lprocmips = 4372,
212 gprocmips = 4373,
213 manyreg2 = 4375,
214 lprocia64 = 4376,
215 gprocia64 = 4377,
216 localslot = 4378,
217 paramslot = 4379,
218 manframerel = 4382,
219 manregister = 4383,
220 manslot = 4384,
221 manmanyreg = 4385,
222 manregrel = 4386,
223 manmanyreg2 = 4387,
224 unamespace = 4388,
225 dataref = 4390,
226 annotationref = 4392,
227 tokenref = 4393,
228 gmanproc = 4394,
229 lmanproc = 4395,
230 attr_framerel = 4398,
231 attr_register = 4399,
232 attr_regrel = 4400,
233 attr_manyreg = 4401,
234 sepcode = 4402,
235 local_2005 = 4403,
236 defrange_2005 = 4404,
237 defrange2_2005 = 4405,
238 discarded = 4411,
239 lprocmips_id = 4424,
240 gprocmips_id = 4425,
241 lprocia64_id = 4426,
242 gprocia64_id = 4427,
243 defrange_hlsl = 4432,
244 gdata_hlsl = 4433,
245 ldata_hlsl = 4434,
246 local_dpc_groupshared = 4436,
247 defrange_dpc_ptr_tag = 4439,
248 dpc_sym_tag_map = 4440,
249 armswitchtable = 4441,
250 pogodata = 4444,
251 inlinesite2 = 4445,
252 mod_typeref = 4447,
253 ref_minipdb = 4448,
254 pdbmap = 4449,
255 gdata_hlsl32 = 4450,
256 ldata_hlsl32 = 4451,
257 gdata_hlsl32_ex = 4452,
258 ldata_hlsl32_ex = 4453,
259 fastlink = 4455,
260 inlinees = 4456,
261 end = 6,
262 inlinesite_end = 4430,
263 proc_id_end = 4431,
264 thunk32 = 4354,
265 trampoline = 4396,
266 section = 4406,
267 coffgroup = 4407,
268 @"export" = 4408,
269 lproc32 = 4367,
270 gproc32 = 4368,
271 lproc32_id = 4422,
272 gproc32_id = 4423,
273 lproc32_dpc = 4437,
274 lproc32_dpc_id = 4438,
275 register = 4358,
276 pub32 = 4366,
277 procref = 4389,
278 lprocref = 4391,
279 envblock = 4413,
280 inlinesite = 4429,
281 local = 4414,
282 defrange = 4415,
283 defrange_subfield = 4416,
284 defrange_register = 4417,
285 defrange_framepointer_rel = 4418,
286 defrange_subfield_register = 4419,
287 defrange_framepointer_rel_full_scope = 4420,
288 defrange_register_rel = 4421,
289 block32 = 4355,
290 label32 = 4357,
291 objname = 4353,
292 compile2 = 4374,
293 compile3 = 4412,
294 frameproc = 4114,
295 callsiteinfo = 4409,
296 filestatic = 4435,
297 heapallocsite = 4446,
298 framecookie = 4410,
299 callees = 4442,
300 callers = 4443,
301 udt = 4360,
302 coboludt = 4361,
303 buildinfo = 4428,
304 bprel32 = 4363,
305 regrel32 = 4369,
306 constant = 4359,
307 manconstant = 4397,
308 ldata32 = 4364,
309 gdata32 = 4365,
310 lmandata = 4380,
311 gmandata = 4381,
312 lthread32 = 4370,
313 gthread32 = 4371,
314314};
315315
316316pub const TypeIndex = u32;
......@@ -320,28 +320,28 @@ pub const TypeIndex = u32;
320320// we should define RecordPrefix as part of the ProcSym structure.
321321// This might be important when we start generating PDB in self-hosted with our own PE linker.
322322pub const ProcSym = extern struct {
323 Parent: u32,
324 End: u32,
325 Next: u32,
326 CodeSize: u32,
327 DbgStart: u32,
328 DbgEnd: u32,
329 FunctionType: TypeIndex,
330 CodeOffset: u32,
331 Segment: u16,
332 Flags: ProcSymFlags,
333 Name: [1]u8, // null-terminated
323 parent: u32,
324 end: u32,
325 next: u32,
326 code_size: u32,
327 dbg_start: u32,
328 dbg_end: u32,
329 function_type: TypeIndex,
330 code_offset: u32,
331 segment: u16,
332 flags: ProcSymFlags,
333 name: [1]u8, // null-terminated
334334};
335335
336336pub const ProcSymFlags = packed struct {
337 HasFP: bool,
338 HasIRET: bool,
339 HasFRET: bool,
340 IsNoReturn: bool,
341 IsUnreachable: bool,
342 HasCustomCallingConv: bool,
343 IsNoInline: bool,
344 HasOptimizedDebugInfo: bool,
337 has_fp: bool,
338 has_iret: bool,
339 has_fret: bool,
340 is_no_return: bool,
341 is_unreachable: bool,
342 has_custom_calling_conv: bool,
343 is_no_inline: bool,
344 has_optimized_debug_info: bool,
345345};
346346
347347pub const SectionContrSubstreamVersion = enum(u32) {
......@@ -351,11 +351,11 @@ pub const SectionContrSubstreamVersion = enum(u32) {
351351};
352352
353353pub const RecordPrefix = extern struct {
354 /// Record length, starting from &RecordKind.
355 RecordLen: u16,
354 /// Record length, starting from &record_kind.
355 record_len: u16,
356356
357357 /// Record kind enum (SymRecordKind or TypeRecordKind)
358 RecordKind: SymbolKind,
358 record_kind: SymbolKind,
359359};
360360
361361/// The following variable length array appears immediately after the header.
......@@ -364,19 +364,19 @@ pub const RecordPrefix = extern struct {
364364/// Each `LineBlockFragmentHeader` as specified below.
365365pub const LineFragmentHeader = extern struct {
366366 /// Code offset of line contribution.
367 RelocOffset: u32,
367 reloc_offset: u32,
368368
369369 /// Code segment of line contribution.
370 RelocSegment: u16,
371 Flags: LineFlags,
370 reloc_segment: u16,
371 flags: LineFlags,
372372
373373 /// Code size of this line contribution.
374 CodeSize: u32,
374 code_size: u32,
375375};
376376
377377pub const LineFlags = packed struct {
378378 /// CV_LINES_HAVE_COLUMNS
379 LF_HaveColumns: bool,
379 have_columns: bool,
380380 unused: u15,
381381};
382382
......@@ -389,110 +389,109 @@ pub const LineBlockFragmentHeader = extern struct {
389389 /// checksums buffer. The checksum entry then
390390 /// contains another offset into the string
391391 /// table of the actual name.
392 NameIndex: u32,
393 NumLines: u32,
392 name_index: u32,
393 num_lines: u32,
394394
395395 /// code size of block, in bytes
396 BlockSize: u32,
396 block_size: u32,
397397};
398398
399399pub const LineNumberEntry = extern struct {
400400 /// Offset to start of code bytes for line number
401 Offset: u32,
402 Flags: u32,
401 offset: u32,
402 flags: Flags,
403403
404 /// TODO runtime crash when I make the actual type of Flags this
405 pub const Flags = packed struct {
404 pub const Flags = packed struct(u32) {
406405 /// Start line number
407 Start: u24,
406 start: u24,
408407 /// Delta of lines to the end of the expression. Still unclear.
409408 // TODO figure out the point of this field.
410 End: u7,
411 IsStatement: bool,
409 end: u7,
410 is_statement: bool,
412411 };
413412};
414413
415414pub const ColumnNumberEntry = extern struct {
416 StartColumn: u16,
417 EndColumn: u16,
415 start_column: u16,
416 end_column: u16,
418417};
419418
420419/// Checksum bytes follow.
421420pub const FileChecksumEntryHeader = extern struct {
422421 /// Byte offset of filename in global string table.
423 FileNameOffset: u32,
422 file_name_offset: u32,
424423 /// Number of bytes of checksum.
425 ChecksumSize: u8,
424 checksum_size: u8,
426425 /// FileChecksumKind
427 ChecksumKind: u8,
426 checksum_kind: u8,
428427};
429428
430429pub const DebugSubsectionKind = enum(u32) {
431 None = 0,
432 Symbols = 0xf1,
433 Lines = 0xf2,
434 StringTable = 0xf3,
435 FileChecksums = 0xf4,
436 FrameData = 0xf5,
437 InlineeLines = 0xf6,
438 CrossScopeImports = 0xf7,
439 CrossScopeExports = 0xf8,
430 none = 0,
431 symbols = 0xf1,
432 lines = 0xf2,
433 string_table = 0xf3,
434 file_checksums = 0xf4,
435 frame_data = 0xf5,
436 inlinee_lines = 0xf6,
437 cross_scope_imports = 0xf7,
438 cross_scope_exports = 0xf8,
440439
441440 // These appear to relate to .Net assembly info.
442 ILLines = 0xf9,
443 FuncMDTokenMap = 0xfa,
444 TypeMDTokenMap = 0xfb,
445 MergedAssemblyInput = 0xfc,
441 il_lines = 0xf9,
442 func_md_token_map = 0xfa,
443 type_md_token_map = 0xfb,
444 merged_assembly_input = 0xfc,
446445
447 CoffSymbolRVA = 0xfd,
446 coff_symbol_rva = 0xfd,
448447};
449448
450449pub const DebugSubsectionHeader = extern struct {
451450 /// codeview::DebugSubsectionKind enum
452 Kind: DebugSubsectionKind,
451 kind: DebugSubsectionKind,
453452
454453 /// number of bytes occupied by this record.
455 Length: u32,
454 length: u32,
456455};
457456
458457pub const StringTableHeader = extern struct {
459458 /// PDBStringTableSignature
460 Signature: u32,
459 signature: u32,
461460 /// 1 or 2
462 HashVersion: u32,
461 hash_version: u32,
463462 /// Number of bytes of names buffer.
464 ByteSize: u32,
463 byte_size: u32,
465464};
466465
467466// https://llvm.org/docs/PDB/MsfFile.html#the-superblock
468467pub const SuperBlock = extern struct {
469468 /// The LLVM docs list a space between C / C++ but empirically this is not the case.
470 pub const file_magic = "Microsoft C/C++ MSF 7.00\r\n\x1a\x44\x53\x00\x00\x00";
469 pub const expect_magic = "Microsoft C/C++ MSF 7.00\r\n\x1a\x44\x53\x00\x00\x00";
471470
472 FileMagic: [file_magic.len]u8,
471 file_magic: [expect_magic.len]u8,
473472
474473 /// The block size of the internal file system. Valid values are 512, 1024,
475474 /// 2048, and 4096 bytes. Certain aspects of the MSF file layout vary depending
476475 /// on the block sizes. For the purposes of LLVM, we handle only block sizes of
477476 /// 4KiB, and all further discussion assumes a block size of 4KiB.
478 BlockSize: u32,
477 block_size: u32,
479478
480479 /// The index of a block within the file, at which begins a bitfield representing
481480 /// the set of all blocks within the file which are “free” (i.e. the data within
482481 /// that block is not used). See The Free Block Map for more information. Important:
483482 /// FreeBlockMapBlock can only be 1 or 2!
484 FreeBlockMapBlock: u32,
483 free_block_map_block: u32,
485484
486485 /// The total number of blocks in the file. NumBlocks * BlockSize should equal the
487486 /// size of the file on disk.
488 NumBlocks: u32,
487 num_blocks: u32,
489488
490489 /// The size of the stream directory, in bytes. The stream directory contains
491490 /// information about each stream’s size and the set of blocks that it occupies.
492491 /// It will be described in more detail later.
493 NumDirectoryBytes: u32,
492 num_directory_bytes: u32,
494493
495 Unknown: u32,
494 unknown: u32,
496495 /// The index of a block within the MSF file. At this block is an array of
497496 /// ulittle32_t’s listing the blocks that the stream directory resides on.
498497 /// For large MSF files, the stream directory (which describes the block
......@@ -508,5 +507,5 @@ pub const SuperBlock = extern struct {
508507 // This would mean the Stream Directory is bigger than BlockSize / sizeof(u32)
509508 // blocks. We're not even close to this with a 1GB pdb file, and LLVM didn't
510509 // implement it so we're kind of safe making this assumption for now.
511 BlockMapAddr: u32,
510 block_map_addr: u32,
512511};
lib/std/zig/AstGen.zig+152-196
......@@ -4053,7 +4053,7 @@ fn fnDecl(
40534053 // The source slice is added towards the *end* of this function.
40544054 astgen.src_hasher.update(std.mem.asBytes(&astgen.source_column));
40554055
4056 // missing function name already happened in scanDecls()
4056 // missing function name already happened in scanContainer()
40574057 const fn_name_token = fn_proto.name_token orelse return error.AnalysisFail;
40584058
40594059 // We insert this at the beginning so that its instruction index marks the
......@@ -5019,7 +5019,7 @@ fn structDeclInner(
50195019 }
50205020 };
50215021
5022 const decl_count = try astgen.scanDecls(&namespace, container_decl.ast.members);
5022 const decl_count = try astgen.scanContainer(&namespace, container_decl.ast.members, .@"struct");
50235023 const field_count: u32 = @intCast(container_decl.ast.members.len - decl_count);
50245024
50255025 const bits_per_field = 4;
......@@ -5088,15 +5088,6 @@ fn structDeclInner(
50885088 astgen.src_hasher.update(tree.getNodeSource(backing_int_node));
50895089 }
50905090
5091 var sfba = std.heap.stackFallback(256, astgen.arena);
5092 const sfba_allocator = sfba.get();
5093
5094 var duplicate_names = std.AutoArrayHashMap(Zir.NullTerminatedString, std.ArrayListUnmanaged(Ast.TokenIndex)).init(sfba_allocator);
5095 try duplicate_names.ensureTotalCapacity(field_count);
5096
5097 // When there aren't errors, use this to avoid a second iteration.
5098 var any_duplicate = false;
5099
51005091 var known_non_opv = false;
51015092 var known_comptime_only = false;
51025093 var any_comptime_fields = false;
......@@ -5117,16 +5108,6 @@ fn structDeclInner(
51175108 assert(!member.ast.tuple_like);
51185109
51195110 wip_members.appendToField(@intFromEnum(field_name));
5120
5121 const gop = try duplicate_names.getOrPut(field_name);
5122
5123 if (gop.found_existing) {
5124 try gop.value_ptr.append(sfba_allocator, member.ast.main_token);
5125 any_duplicate = true;
5126 } else {
5127 gop.value_ptr.* = .{};
5128 try gop.value_ptr.append(sfba_allocator, member.ast.main_token);
5129 }
51305111 } else if (!member.ast.tuple_like) {
51315112 return astgen.failTok(member.ast.main_token, "tuple field has a name", .{});
51325113 }
......@@ -5211,32 +5192,6 @@ fn structDeclInner(
52115192 }
52125193 }
52135194
5214 if (any_duplicate) {
5215 var it = duplicate_names.iterator();
5216
5217 while (it.next()) |entry| {
5218 const record = entry.value_ptr.*;
5219 if (record.items.len > 1) {
5220 var error_notes = std.ArrayList(u32).init(astgen.arena);
5221
5222 for (record.items[1..]) |duplicate| {
5223 try error_notes.append(try astgen.errNoteTok(duplicate, "duplicate field here", .{}));
5224 }
5225
5226 try error_notes.append(try astgen.errNoteNode(node, "struct declared here", .{}));
5227
5228 try astgen.appendErrorTokNotes(
5229 record.items[0],
5230 "duplicate struct field name",
5231 .{},
5232 error_notes.items,
5233 );
5234 }
5235 }
5236
5237 return error.AnalysisFail;
5238 }
5239
52405195 var fields_hash: std.zig.SrcHash = undefined;
52415196 astgen.src_hasher.final(&fields_hash);
52425197
......@@ -5317,7 +5272,7 @@ fn unionDeclInner(
53175272 };
53185273 defer block_scope.unstack();
53195274
5320 const decl_count = try astgen.scanDecls(&namespace, members);
5275 const decl_count = try astgen.scanContainer(&namespace, members, .@"union");
53215276 const field_count: u32 = @intCast(members.len - decl_count);
53225277
53235278 if (layout != .auto and (auto_enum_tok != null or arg_node != 0)) {
......@@ -5348,15 +5303,6 @@ fn unionDeclInner(
53485303 astgen.src_hasher.update(astgen.tree.getNodeSource(arg_node));
53495304 }
53505305
5351 var sfba = std.heap.stackFallback(256, astgen.arena);
5352 const sfba_allocator = sfba.get();
5353
5354 var duplicate_names = std.AutoArrayHashMap(Zir.NullTerminatedString, std.ArrayListUnmanaged(Ast.TokenIndex)).init(sfba_allocator);
5355 try duplicate_names.ensureTotalCapacity(field_count);
5356
5357 // When there aren't errors, use this to avoid a second iteration.
5358 var any_duplicate = false;
5359
53605306 for (members) |member_node| {
53615307 var member = switch (try containerMember(&block_scope, &namespace.base, &wip_members, member_node)) {
53625308 .decl => continue,
......@@ -5374,16 +5320,6 @@ fn unionDeclInner(
53745320 const field_name = try astgen.identAsString(member.ast.main_token);
53755321 wip_members.appendToField(@intFromEnum(field_name));
53765322
5377 const gop = try duplicate_names.getOrPut(field_name);
5378
5379 if (gop.found_existing) {
5380 try gop.value_ptr.append(sfba_allocator, member.ast.main_token);
5381 any_duplicate = true;
5382 } else {
5383 gop.value_ptr.* = .{};
5384 try gop.value_ptr.append(sfba_allocator, member.ast.main_token);
5385 }
5386
53875323 const doc_comment_index = try astgen.docCommentAsString(member.firstToken());
53885324 wip_members.appendToField(@intFromEnum(doc_comment_index));
53895325
......@@ -5438,32 +5374,6 @@ fn unionDeclInner(
54385374 }
54395375 }
54405376
5441 if (any_duplicate) {
5442 var it = duplicate_names.iterator();
5443
5444 while (it.next()) |entry| {
5445 const record = entry.value_ptr.*;
5446 if (record.items.len > 1) {
5447 var error_notes = std.ArrayList(u32).init(astgen.arena);
5448
5449 for (record.items[1..]) |duplicate| {
5450 try error_notes.append(try astgen.errNoteTok(duplicate, "duplicate field here", .{}));
5451 }
5452
5453 try error_notes.append(try astgen.errNoteNode(node, "union declared here", .{}));
5454
5455 try astgen.appendErrorTokNotes(
5456 record.items[0],
5457 "duplicate union field name",
5458 .{},
5459 error_notes.items,
5460 );
5461 }
5462 }
5463
5464 return error.AnalysisFail;
5465 }
5466
54675377 var fields_hash: std.zig.SrcHash = undefined;
54685378 astgen.src_hasher.final(&fields_hash);
54695379
......@@ -5666,7 +5576,7 @@ fn containerDecl(
56665576 };
56675577 defer block_scope.unstack();
56685578
5669 _ = try astgen.scanDecls(&namespace, container_decl.ast.members);
5579 _ = try astgen.scanContainer(&namespace, container_decl.ast.members, .@"enum");
56705580 namespace.base.tag = .namespace;
56715581
56725582 const arg_inst: Zir.Inst.Ref = if (container_decl.ast.arg != 0)
......@@ -5687,15 +5597,6 @@ fn containerDecl(
56875597 }
56885598 astgen.src_hasher.update(&.{@intFromBool(nonexhaustive)});
56895599
5690 var sfba = std.heap.stackFallback(256, astgen.arena);
5691 const sfba_allocator = sfba.get();
5692
5693 var duplicate_names = std.AutoArrayHashMap(Zir.NullTerminatedString, std.ArrayListUnmanaged(Ast.TokenIndex)).init(sfba_allocator);
5694 try duplicate_names.ensureTotalCapacity(counts.total_fields);
5695
5696 // When there aren't errors, use this to avoid a second iteration.
5697 var any_duplicate = false;
5698
56995600 for (container_decl.ast.members) |member_node| {
57005601 if (member_node == counts.nonexhaustive_node)
57015602 continue;
......@@ -5712,16 +5613,6 @@ fn containerDecl(
57125613 const field_name = try astgen.identAsString(member.ast.main_token);
57135614 wip_members.appendToField(@intFromEnum(field_name));
57145615
5715 const gop = try duplicate_names.getOrPut(field_name);
5716
5717 if (gop.found_existing) {
5718 try gop.value_ptr.append(sfba_allocator, member.ast.main_token);
5719 any_duplicate = true;
5720 } else {
5721 gop.value_ptr.* = .{};
5722 try gop.value_ptr.append(sfba_allocator, member.ast.main_token);
5723 }
5724
57255616 const doc_comment_index = try astgen.docCommentAsString(member.firstToken());
57265617 wip_members.appendToField(@intFromEnum(doc_comment_index));
57275618
......@@ -5748,32 +5639,6 @@ fn containerDecl(
57485639 }
57495640 }
57505641
5751 if (any_duplicate) {
5752 var it = duplicate_names.iterator();
5753
5754 while (it.next()) |entry| {
5755 const record = entry.value_ptr.*;
5756 if (record.items.len > 1) {
5757 var error_notes = std.ArrayList(u32).init(astgen.arena);
5758
5759 for (record.items[1..]) |duplicate| {
5760 try error_notes.append(try astgen.errNoteTok(duplicate, "duplicate field here", .{}));
5761 }
5762
5763 try error_notes.append(try astgen.errNoteNode(node, "enum declared here", .{}));
5764
5765 try astgen.appendErrorTokNotes(
5766 record.items[0],
5767 "duplicate enum field name",
5768 .{},
5769 error_notes.items,
5770 );
5771 }
5772 }
5773
5774 return error.AnalysisFail;
5775 }
5776
57775642 if (!block_scope.isEmpty()) {
57785643 _ = try block_scope.addBreak(.break_inline, decl_inst, .void_value);
57795644 }
......@@ -5833,7 +5698,7 @@ fn containerDecl(
58335698 };
58345699 defer block_scope.unstack();
58355700
5836 const decl_count = try astgen.scanDecls(&namespace, container_decl.ast.members);
5701 const decl_count = try astgen.scanContainer(&namespace, container_decl.ast.members, .@"opaque");
58375702
58385703 var wip_members = try WipMembers.init(gpa, &astgen.scratch, decl_count, 0, 0, 0);
58395704 defer wip_members.deinit();
......@@ -13594,31 +13459,67 @@ fn advanceSourceCursor(astgen: *AstGen, end: usize) void {
1359413459 astgen.source_column = column;
1359513460}
1359613461
13597fn scanDecls(astgen: *AstGen, namespace: *Scope.Namespace, members: []const Ast.Node.Index) !u32 {
13462/// Detects name conflicts for decls and fields, and populates `namespace.decls` with all named declarations.
13463/// Returns the number of declarations in the namespace, including unnamed declarations (e.g. `comptime` decls).
13464fn scanContainer(
13465 astgen: *AstGen,
13466 namespace: *Scope.Namespace,
13467 members: []const Ast.Node.Index,
13468 container_kind: enum { @"struct", @"union", @"enum", @"opaque" },
13469) !u32 {
1359813470 const gpa = astgen.gpa;
1359913471 const tree = astgen.tree;
1360013472 const node_tags = tree.nodes.items(.tag);
1360113473 const main_tokens = tree.nodes.items(.main_token);
1360213474 const token_tags = tree.tokens.items(.tag);
1360313475
13604 // We don't have shadowing for test names, so we just track those for duplicate reporting locally.
13605 var named_tests: std.AutoHashMapUnmanaged(Zir.NullTerminatedString, Ast.Node.Index) = .{};
13606 var decltests: std.AutoHashMapUnmanaged(Zir.NullTerminatedString, Ast.Node.Index) = .{};
13476 // This type forms a linked list of source tokens declaring the same name.
13477 const NameEntry = struct {
13478 tok: Ast.TokenIndex,
13479 /// Using a linked list here simplifies memory management, and is acceptable since
13480 ///ewntries are only allocated in error situations. The entries are allocated into the
13481 /// AstGen arena.
13482 next: ?*@This(),
13483 };
13484
13485 // The maps below are allocated into this SFBA to avoid using the GPA for small namespaces.
13486 var sfba_state = std.heap.stackFallback(512, astgen.gpa);
13487 const sfba = sfba_state.get();
13488
13489 var names: std.AutoArrayHashMapUnmanaged(Zir.NullTerminatedString, NameEntry) = .{};
13490 var test_names: std.AutoArrayHashMapUnmanaged(Zir.NullTerminatedString, NameEntry) = .{};
13491 var decltest_names: std.AutoArrayHashMapUnmanaged(Zir.NullTerminatedString, NameEntry) = .{};
1360713492 defer {
13608 named_tests.deinit(gpa);
13609 decltests.deinit(gpa);
13493 names.deinit(sfba);
13494 test_names.deinit(sfba);
13495 decltest_names.deinit(sfba);
1361013496 }
1361113497
13498 var any_duplicates = false;
1361213499 var decl_count: u32 = 0;
1361313500 for (members) |member_node| {
13614 const name_token = switch (node_tags[member_node]) {
13501 const Kind = enum { decl, field };
13502 const kind: Kind, const name_token = switch (node_tags[member_node]) {
13503 .container_field_init,
13504 .container_field_align,
13505 .container_field,
13506 => blk: {
13507 var full = tree.fullContainerField(member_node).?;
13508 switch (container_kind) {
13509 .@"struct", .@"opaque" => {},
13510 .@"union", .@"enum" => full.convertToNonTupleLike(astgen.tree.nodes),
13511 }
13512 if (full.ast.tuple_like) continue;
13513 break :blk .{ .field, full.ast.main_token };
13514 },
13515
1361513516 .global_var_decl,
1361613517 .local_var_decl,
1361713518 .simple_var_decl,
1361813519 .aligned_var_decl,
1361913520 => blk: {
1362013521 decl_count += 1;
13621 break :blk main_tokens[member_node] + 1;
13522 break :blk .{ .decl, main_tokens[member_node] + 1 };
1362213523 },
1362313524
1362413525 .fn_proto_simple,
......@@ -13630,12 +13531,10 @@ fn scanDecls(astgen: *AstGen, namespace: *Scope.Namespace, members: []const Ast.
1363013531 decl_count += 1;
1363113532 const ident = main_tokens[member_node] + 1;
1363213533 if (token_tags[ident] != .identifier) {
13633 switch (astgen.failNode(member_node, "missing function name", .{})) {
13634 error.AnalysisFail => continue,
13635 error.OutOfMemory => return error.OutOfMemory,
13636 }
13534 try astgen.appendErrorNode(member_node, "missing function name", .{});
13535 continue;
1363713536 }
13638 break :blk ident;
13537 break :blk .{ .decl, ident };
1363913538 },
1364013539
1364113540 .@"comptime", .@"usingnamespace" => {
......@@ -13648,70 +13547,87 @@ fn scanDecls(astgen: *AstGen, namespace: *Scope.Namespace, members: []const Ast.
1364813547 // We don't want shadowing detection here, and test names work a bit differently, so
1364913548 // we must do the redeclaration detection ourselves.
1365013549 const test_name_token = main_tokens[member_node] + 1;
13550 const new_ent: NameEntry = .{
13551 .tok = test_name_token,
13552 .next = null,
13553 };
1365113554 switch (token_tags[test_name_token]) {
1365213555 else => {}, // unnamed test
1365313556 .string_literal => {
1365413557 const name = try astgen.strLitAsString(test_name_token);
13655 const gop = try named_tests.getOrPut(gpa, name.index);
13558 const gop = try test_names.getOrPut(sfba, name.index);
1365613559 if (gop.found_existing) {
13657 const name_slice = astgen.string_bytes.items[@intFromEnum(name.index)..][0..name.len];
13658 const name_duped = try gpa.dupe(u8, name_slice);
13659 defer gpa.free(name_duped);
13660 try astgen.appendErrorNodeNotes(member_node, "duplicate test name '{s}'", .{name_duped}, &.{
13661 try astgen.errNoteNode(gop.value_ptr.*, "other test here", .{}),
13662 });
13560 var e = gop.value_ptr;
13561 while (e.next) |n| e = n;
13562 e.next = try astgen.arena.create(NameEntry);
13563 e.next.?.* = new_ent;
13564 any_duplicates = true;
1366313565 } else {
13664 gop.value_ptr.* = member_node;
13566 gop.value_ptr.* = new_ent;
1366513567 }
1366613568 },
1366713569 .identifier => {
1366813570 const name = try astgen.identAsString(test_name_token);
13669 const gop = try decltests.getOrPut(gpa, name);
13571 const gop = try decltest_names.getOrPut(sfba, name);
1367013572 if (gop.found_existing) {
13671 const name_slice = mem.span(astgen.nullTerminatedString(name));
13672 const name_duped = try gpa.dupe(u8, name_slice);
13673 defer gpa.free(name_duped);
13674 try astgen.appendErrorNodeNotes(member_node, "duplicate decltest '{s}'", .{name_duped}, &.{
13675 try astgen.errNoteNode(gop.value_ptr.*, "other decltest here", .{}),
13676 });
13573 var e = gop.value_ptr;
13574 while (e.next) |n| e = n;
13575 e.next = try astgen.arena.create(NameEntry);
13576 e.next.?.* = new_ent;
13577 any_duplicates = true;
1367713578 } else {
13678 gop.value_ptr.* = member_node;
13579 gop.value_ptr.* = new_ent;
1367913580 }
1368013581 },
1368113582 }
1368213583 continue;
1368313584 },
1368413585
13685 else => continue,
13586 else => unreachable,
1368613587 };
1368713588
13589 const name_str_index = try astgen.identAsString(name_token);
13590
13591 if (kind == .decl) {
13592 // Put the name straight into `decls`, even if there are compile errors.
13593 // This avoids incorrect "undeclared identifier" errors later on.
13594 try namespace.decls.put(gpa, name_str_index, member_node);
13595 }
13596
13597 {
13598 const gop = try names.getOrPut(sfba, name_str_index);
13599 const new_ent: NameEntry = .{
13600 .tok = name_token,
13601 .next = null,
13602 };
13603 if (gop.found_existing) {
13604 var e = gop.value_ptr;
13605 while (e.next) |n| e = n;
13606 e.next = try astgen.arena.create(NameEntry);
13607 e.next.?.* = new_ent;
13608 any_duplicates = true;
13609 continue;
13610 } else {
13611 gop.value_ptr.* = new_ent;
13612 }
13613 }
13614
13615 // For fields, we only needed the duplicate check! Decls have some more checks to do, though.
13616 switch (kind) {
13617 .decl => {},
13618 .field => continue,
13619 }
13620
1368813621 const token_bytes = astgen.tree.tokenSlice(name_token);
1368913622 if (token_bytes[0] != '@' and isPrimitive(token_bytes)) {
13690 switch (astgen.failTokNotes(name_token, "name shadows primitive '{s}'", .{
13623 try astgen.appendErrorTokNotes(name_token, "name shadows primitive '{s}'", .{
1369113624 token_bytes,
13692 }, &[_]u32{
13625 }, &.{
1369313626 try astgen.errNoteTok(name_token, "consider using @\"{s}\" to disambiguate", .{
1369413627 token_bytes,
1369513628 }),
13696 })) {
13697 error.AnalysisFail => continue,
13698 error.OutOfMemory => return error.OutOfMemory,
13699 }
13700 }
13701
13702 const name_str_index = try astgen.identAsString(name_token);
13703 const gop = try namespace.decls.getOrPut(gpa, name_str_index);
13704 if (gop.found_existing) {
13705 const name = try gpa.dupe(u8, mem.span(astgen.nullTerminatedString(name_str_index)));
13706 defer gpa.free(name);
13707 switch (astgen.failNodeNotes(member_node, "redeclaration of '{s}'", .{
13708 name,
13709 }, &[_]u32{
13710 try astgen.errNoteNode(gop.value_ptr.*, "other declaration here", .{}),
13711 })) {
13712 error.AnalysisFail => continue,
13713 error.OutOfMemory => return error.OutOfMemory,
13714 }
13629 });
13630 continue;
1371513631 }
1371613632
1371713633 var s = namespace.parent;
......@@ -13719,30 +13635,32 @@ fn scanDecls(astgen: *AstGen, namespace: *Scope.Namespace, members: []const Ast.
1371913635 .local_val => {
1372013636 const local_val = s.cast(Scope.LocalVal).?;
1372113637 if (local_val.name == name_str_index) {
13722 return astgen.failTokNotes(name_token, "declaration '{s}' shadows {s} from outer scope", .{
13638 try astgen.appendErrorTokNotes(name_token, "declaration '{s}' shadows {s} from outer scope", .{
1372313639 token_bytes, @tagName(local_val.id_cat),
13724 }, &[_]u32{
13640 }, &.{
1372513641 try astgen.errNoteTok(
1372613642 local_val.token_src,
1372713643 "previous declaration here",
1372813644 .{},
1372913645 ),
1373013646 });
13647 break;
1373113648 }
1373213649 s = local_val.parent;
1373313650 },
1373413651 .local_ptr => {
1373513652 const local_ptr = s.cast(Scope.LocalPtr).?;
1373613653 if (local_ptr.name == name_str_index) {
13737 return astgen.failTokNotes(name_token, "declaration '{s}' shadows {s} from outer scope", .{
13654 try astgen.appendErrorTokNotes(name_token, "declaration '{s}' shadows {s} from outer scope", .{
1373813655 token_bytes, @tagName(local_ptr.id_cat),
13739 }, &[_]u32{
13656 }, &.{
1374013657 try astgen.errNoteTok(
1374113658 local_ptr.token_src,
1374213659 "previous declaration here",
1374313660 .{},
1374413661 ),
1374513662 });
13663 break;
1374613664 }
1374713665 s = local_ptr.parent;
1374813666 },
......@@ -13751,8 +13669,46 @@ fn scanDecls(astgen: *AstGen, namespace: *Scope.Namespace, members: []const Ast.
1375113669 .defer_normal, .defer_error => s = s.cast(Scope.Defer).?.parent,
1375213670 .top => break,
1375313671 };
13754 gop.value_ptr.* = member_node;
1375513672 }
13673
13674 if (!any_duplicates) return decl_count;
13675
13676 for (names.keys(), names.values()) |name, first| {
13677 if (first.next == null) continue;
13678 var notes: std.ArrayListUnmanaged(u32) = .{};
13679 var prev: NameEntry = first;
13680 while (prev.next) |cur| : (prev = cur.*) {
13681 try notes.append(astgen.arena, try astgen.errNoteTok(cur.tok, "duplicate name here", .{}));
13682 }
13683 try notes.append(astgen.arena, try astgen.errNoteNode(namespace.node, "{s} declared here", .{@tagName(container_kind)}));
13684 const name_duped = try astgen.arena.dupe(u8, mem.span(astgen.nullTerminatedString(name)));
13685 try astgen.appendErrorTokNotes(first.tok, "duplicate {s} member name '{s}'", .{ @tagName(container_kind), name_duped }, notes.items);
13686 }
13687
13688 for (test_names.keys(), test_names.values()) |name, first| {
13689 if (first.next == null) continue;
13690 var notes: std.ArrayListUnmanaged(u32) = .{};
13691 var prev: NameEntry = first;
13692 while (prev.next) |cur| : (prev = cur.*) {
13693 try notes.append(astgen.arena, try astgen.errNoteTok(cur.tok, "duplicate test here", .{}));
13694 }
13695 try notes.append(astgen.arena, try astgen.errNoteNode(namespace.node, "{s} declared here", .{@tagName(container_kind)}));
13696 const name_duped = try astgen.arena.dupe(u8, mem.span(astgen.nullTerminatedString(name)));
13697 try astgen.appendErrorTokNotes(first.tok, "duplicate test name '{s}'", .{name_duped}, notes.items);
13698 }
13699
13700 for (decltest_names.keys(), decltest_names.values()) |name, first| {
13701 if (first.next == null) continue;
13702 var notes: std.ArrayListUnmanaged(u32) = .{};
13703 var prev: NameEntry = first;
13704 while (prev.next) |cur| : (prev = cur.*) {
13705 try notes.append(astgen.arena, try astgen.errNoteTok(cur.tok, "duplicate decltest here", .{}));
13706 }
13707 try notes.append(astgen.arena, try astgen.errNoteNode(namespace.node, "{s} declared here", .{@tagName(container_kind)}));
13708 const name_duped = try astgen.arena.dupe(u8, mem.span(astgen.nullTerminatedString(name)));
13709 try astgen.appendErrorTokNotes(first.tok, "duplicate decltest '{s}'", .{name_duped}, notes.items);
13710 }
13711
1375613712 return decl_count;
1375713713}
1375813714
src/arch/aarch64/bits.zig+4-4
......@@ -1069,7 +1069,7 @@ pub const Instruction = union(enum) {
10691069 };
10701070 }
10711071
1072 fn bitfield(
1072 fn initBitfield(
10731073 opc: u2,
10741074 n: u1,
10751075 rd: Register,
......@@ -1579,7 +1579,7 @@ pub const Instruction = union(enum) {
15791579 64 => 0b1,
15801580 else => unreachable, // unexpected register size
15811581 };
1582 return bitfield(0b00, n, rd, rn, immr, imms);
1582 return initBitfield(0b00, n, rd, rn, immr, imms);
15831583 }
15841584
15851585 pub fn bfm(rd: Register, rn: Register, immr: u6, imms: u6) Instruction {
......@@ -1588,7 +1588,7 @@ pub const Instruction = union(enum) {
15881588 64 => 0b1,
15891589 else => unreachable, // unexpected register size
15901590 };
1591 return bitfield(0b01, n, rd, rn, immr, imms);
1591 return initBitfield(0b01, n, rd, rn, immr, imms);
15921592 }
15931593
15941594 pub fn ubfm(rd: Register, rn: Register, immr: u6, imms: u6) Instruction {
......@@ -1597,7 +1597,7 @@ pub const Instruction = union(enum) {
15971597 64 => 0b1,
15981598 else => unreachable, // unexpected register size
15991599 };
1600 return bitfield(0b10, n, rd, rn, immr, imms);
1600 return initBitfield(0b10, n, rd, rn, immr, imms);
16011601 }
16021602
16031603 pub fn asrImmediate(rd: Register, rn: Register, shift: u6) Instruction {
src/arch/arm/bits.zig+10-10
......@@ -662,7 +662,7 @@ pub const Instruction = union(enum) {
662662 };
663663 }
664664
665 fn multiply(
665 fn initMultiply(
666666 cond: Condition,
667667 set_cond: u1,
668668 rd: Register,
......@@ -864,7 +864,7 @@ pub const Instruction = union(enum) {
864864 };
865865 }
866866
867 fn branch(cond: Condition, offset: i26, link: u1) Instruction {
867 fn initBranch(cond: Condition, offset: i26, link: u1) Instruction {
868868 return Instruction{
869869 .branch = .{
870870 .cond = @intFromEnum(cond),
......@@ -900,7 +900,7 @@ pub const Instruction = union(enum) {
900900 };
901901 }
902902
903 fn breakpoint(imm: u16) Instruction {
903 fn initBreakpoint(imm: u16) Instruction {
904904 return Instruction{
905905 .breakpoint = .{
906906 .imm12 = @as(u12, @truncate(imm >> 4)),
......@@ -1087,19 +1087,19 @@ pub const Instruction = union(enum) {
10871087 // Multiply
10881088
10891089 pub fn mul(cond: Condition, rd: Register, rn: Register, rm: Register) Instruction {
1090 return multiply(cond, 0, rd, rn, rm, null);
1090 return initMultiply(cond, 0, rd, rn, rm, null);
10911091 }
10921092
10931093 pub fn muls(cond: Condition, rd: Register, rn: Register, rm: Register) Instruction {
1094 return multiply(cond, 1, rd, rn, rm, null);
1094 return initMultiply(cond, 1, rd, rn, rm, null);
10951095 }
10961096
10971097 pub fn mla(cond: Condition, rd: Register, rn: Register, rm: Register, ra: Register) Instruction {
1098 return multiply(cond, 0, rd, rn, rm, ra);
1098 return initMultiply(cond, 0, rd, rn, rm, ra);
10991099 }
11001100
11011101 pub fn mlas(cond: Condition, rd: Register, rn: Register, rm: Register, ra: Register) Instruction {
1102 return multiply(cond, 1, rd, rn, rm, ra);
1102 return initMultiply(cond, 1, rd, rn, rm, ra);
11031103 }
11041104
11051105 // Multiply long
......@@ -1261,11 +1261,11 @@ pub const Instruction = union(enum) {
12611261 // Branch
12621262
12631263 pub fn b(cond: Condition, offset: i26) Instruction {
1264 return branch(cond, offset, 0);
1264 return initBranch(cond, offset, 0);
12651265 }
12661266
12671267 pub fn bl(cond: Condition, offset: i26) Instruction {
1268 return branch(cond, offset, 1);
1268 return initBranch(cond, offset, 1);
12691269 }
12701270
12711271 // Branch and exchange
......@@ -1289,7 +1289,7 @@ pub const Instruction = union(enum) {
12891289 // Breakpoint
12901290
12911291 pub fn bkpt(imm: u16) Instruction {
1292 return breakpoint(imm);
1292 return initBreakpoint(imm);
12931293 }
12941294
12951295 // Aliases
src/arch/x86_64/CodeGen.zig+1-1
......@@ -15563,7 +15563,7 @@ fn genLazySymbolRef(
1556315563 .mov => try self.asmRegisterMemory(
1556415564 .{ ._, tag },
1556515565 reg.to64(),
15566 Memory.sib(.qword, .{ .base = .{ .reg = reg.to64() } }),
15566 Memory.initSib(.qword, .{ .base = .{ .reg = reg.to64() } }),
1556715567 ),
1556815568 else => unreachable,
1556915569 }
src/arch/x86_64/Disassembler.zig+8-8
......@@ -95,7 +95,7 @@ pub fn next(dis: *Disassembler) Error!?Instruction {
9595
9696 if (modrm.rip()) {
9797 return inst(act_enc, .{
98 .op1 = .{ .mem = Memory.rip(Memory.PtrSize.fromBitSize(act_enc.data.ops[0].memBitSize()), disp) },
98 .op1 = .{ .mem = Memory.initRip(Memory.PtrSize.fromBitSize(act_enc.data.ops[0].memBitSize()), disp) },
9999 .op2 = op2,
100100 });
101101 }
......@@ -106,7 +106,7 @@ pub fn next(dis: *Disassembler) Error!?Instruction {
106106 else
107107 parseGpRegister(modrm.op2, prefixes.rex.b, prefixes.rex, 64);
108108 return inst(act_enc, .{
109 .op1 = .{ .mem = Memory.sib(Memory.PtrSize.fromBitSize(act_enc.data.ops[0].memBitSize()), .{
109 .op1 = .{ .mem = Memory.initSib(Memory.PtrSize.fromBitSize(act_enc.data.ops[0].memBitSize()), .{
110110 .base = if (base) |base_reg| .{ .reg = base_reg } else .none,
111111 .scale_index = scale_index,
112112 .disp = disp,
......@@ -119,14 +119,14 @@ pub fn next(dis: *Disassembler) Error!?Instruction {
119119 const offset = try dis.parseOffset();
120120 return inst(enc, .{
121121 .op1 = .{ .reg = Register.rax.toBitSize(enc.data.ops[0].regBitSize()) },
122 .op2 = .{ .mem = Memory.moffs(seg, offset) },
122 .op2 = .{ .mem = Memory.initMoffs(seg, offset) },
123123 });
124124 },
125125 .td => {
126126 const seg = segmentRegister(prefixes.legacy);
127127 const offset = try dis.parseOffset();
128128 return inst(enc, .{
129 .op1 = .{ .mem = Memory.moffs(seg, offset) },
129 .op1 = .{ .mem = Memory.initMoffs(seg, offset) },
130130 .op2 = .{ .reg = Register.rax.toBitSize(enc.data.ops[1].regBitSize()) },
131131 });
132132 },
......@@ -153,7 +153,7 @@ pub fn next(dis: *Disassembler) Error!?Instruction {
153153
154154 if (modrm.rip()) {
155155 return inst(enc, .{
156 .op1 = .{ .mem = Memory.rip(Memory.PtrSize.fromBitSize(dst_bit_size), disp) },
156 .op1 = .{ .mem = Memory.initRip(Memory.PtrSize.fromBitSize(dst_bit_size), disp) },
157157 .op2 = .{ .reg = parseGpRegister(modrm.op1, prefixes.rex.r, prefixes.rex, src_bit_size) },
158158 .op3 = op3,
159159 });
......@@ -165,7 +165,7 @@ pub fn next(dis: *Disassembler) Error!?Instruction {
165165 else
166166 parseGpRegister(modrm.op2, prefixes.rex.b, prefixes.rex, 64);
167167 return inst(enc, .{
168 .op1 = .{ .mem = Memory.sib(Memory.PtrSize.fromBitSize(dst_bit_size), .{
168 .op1 = .{ .mem = Memory.initSib(Memory.PtrSize.fromBitSize(dst_bit_size), .{
169169 .base = if (base) |base_reg| .{ .reg = base_reg } else .none,
170170 .scale_index = scale_index,
171171 .disp = disp,
......@@ -203,7 +203,7 @@ pub fn next(dis: *Disassembler) Error!?Instruction {
203203 if (modrm.rip()) {
204204 return inst(enc, .{
205205 .op1 = .{ .reg = parseGpRegister(modrm.op1, prefixes.rex.r, prefixes.rex, dst_bit_size) },
206 .op2 = .{ .mem = Memory.rip(Memory.PtrSize.fromBitSize(src_bit_size), disp) },
206 .op2 = .{ .mem = Memory.initRip(Memory.PtrSize.fromBitSize(src_bit_size), disp) },
207207 .op3 = op3,
208208 });
209209 }
......@@ -215,7 +215,7 @@ pub fn next(dis: *Disassembler) Error!?Instruction {
215215 parseGpRegister(modrm.op2, prefixes.rex.b, prefixes.rex, 64);
216216 return inst(enc, .{
217217 .op1 = .{ .reg = parseGpRegister(modrm.op1, prefixes.rex.r, prefixes.rex, dst_bit_size) },
218 .op2 = .{ .mem = Memory.sib(Memory.PtrSize.fromBitSize(src_bit_size), .{
218 .op2 = .{ .mem = Memory.initSib(Memory.PtrSize.fromBitSize(src_bit_size), .{
219219 .base = if (base) |base_reg| .{ .reg = base_reg } else .none,
220220 .scale_index = scale_index,
221221 .disp = disp,
src/arch/x86_64/Lower.zig+21-21
......@@ -200,13 +200,13 @@ pub fn lowerMir(lower: *Lower, index: Mir.Inst.Index) Error!struct {
200200 });
201201 try lower.emit(.none, .lea, &.{
202202 .{ .reg = inst.data.ri.r1 },
203 .{ .mem = Memory.sib(.qword, .{
203 .{ .mem = Memory.initSib(.qword, .{
204204 .base = .{ .reg = inst.data.ri.r1 },
205205 .disp = -page_size,
206206 }) },
207207 });
208208 try lower.emit(.none, .@"test", &.{
209 .{ .mem = Memory.sib(.dword, .{
209 .{ .mem = Memory.initSib(.dword, .{
210210 .base = .{ .reg = inst.data.ri.r1 },
211211 }) },
212212 .{ .reg = inst.data.ri.r1.to32() },
......@@ -220,7 +220,7 @@ pub fn lowerMir(lower: *Lower, index: Mir.Inst.Index) Error!struct {
220220 var offset = page_size;
221221 while (offset < @as(i32, @bitCast(inst.data.ri.i))) : (offset += page_size) {
222222 try lower.emit(.none, .@"test", &.{
223 .{ .mem = Memory.sib(.dword, .{
223 .{ .mem = Memory.initSib(.dword, .{
224224 .base = .{ .reg = inst.data.ri.r1 },
225225 .disp = -offset,
226226 }) },
......@@ -246,7 +246,7 @@ pub fn lowerMir(lower: *Lower, index: Mir.Inst.Index) Error!struct {
246246 },
247247 .pseudo_probe_adjust_loop_rr => {
248248 try lower.emit(.none, .@"test", &.{
249 .{ .mem = Memory.sib(.dword, .{
249 .{ .mem = Memory.initSib(.dword, .{
250250 .base = .{ .reg = inst.data.rr.r1 },
251251 .scale_index = .{ .scale = 1, .index = inst.data.rr.r2 },
252252 .disp = -page_size,
......@@ -417,7 +417,7 @@ fn emit(lower: *Lower, prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand)
417417 lower.result_insts[lower.result_insts_len] =
418418 try Instruction.new(.none, .lea, &[_]Operand{
419419 .{ .reg = .rdi },
420 .{ .mem = Memory.rip(mem_op.sib.ptr_size, 0) },
420 .{ .mem = Memory.initRip(mem_op.sib.ptr_size, 0) },
421421 });
422422 lower.result_insts_len += 1;
423423 _ = lower.reloc(.{
......@@ -430,7 +430,7 @@ fn emit(lower: *Lower, prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand)
430430 lower.result_insts_len += 1;
431431 _ = lower.reloc(.{ .linker_dtpoff = sym_index }, 0);
432432 emit_mnemonic = .lea;
433 break :op .{ .mem = Memory.sib(mem_op.sib.ptr_size, .{
433 break :op .{ .mem = Memory.initSib(mem_op.sib.ptr_size, .{
434434 .base = .{ .reg = .rax },
435435 .disp = std.math.minInt(i32),
436436 }) };
......@@ -439,12 +439,12 @@ fn emit(lower: *Lower, prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand)
439439 lower.result_insts[lower.result_insts_len] =
440440 try Instruction.new(.none, .mov, &[_]Operand{
441441 .{ .reg = .rax },
442 .{ .mem = Memory.sib(.qword, .{ .base = .{ .reg = .fs } }) },
442 .{ .mem = Memory.initSib(.qword, .{ .base = .{ .reg = .fs } }) },
443443 });
444444 lower.result_insts_len += 1;
445445 _ = lower.reloc(.{ .linker_reloc = sym_index }, 0);
446446 emit_mnemonic = .lea;
447 break :op .{ .mem = Memory.sib(mem_op.sib.ptr_size, .{
447 break :op .{ .mem = Memory.initSib(mem_op.sib.ptr_size, .{
448448 .base = .{ .reg = .rax },
449449 .disp = std.math.minInt(i32),
450450 }) };
......@@ -455,7 +455,7 @@ fn emit(lower: *Lower, prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand)
455455 if (lower.pic) switch (mnemonic) {
456456 .lea => {
457457 if (elf_sym.flags.is_extern_ptr) emit_mnemonic = .mov;
458 break :op .{ .mem = Memory.rip(mem_op.sib.ptr_size, 0) };
458 break :op .{ .mem = Memory.initRip(mem_op.sib.ptr_size, 0) };
459459 },
460460 .mov => {
461461 if (elf_sym.flags.is_extern_ptr) {
......@@ -463,25 +463,25 @@ fn emit(lower: *Lower, prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand)
463463 lower.result_insts[lower.result_insts_len] =
464464 try Instruction.new(.none, .mov, &[_]Operand{
465465 .{ .reg = reg.to64() },
466 .{ .mem = Memory.rip(.qword, 0) },
466 .{ .mem = Memory.initRip(.qword, 0) },
467467 });
468468 lower.result_insts_len += 1;
469 break :op .{ .mem = Memory.sib(mem_op.sib.ptr_size, .{ .base = .{
469 break :op .{ .mem = Memory.initSib(mem_op.sib.ptr_size, .{ .base = .{
470470 .reg = reg.to64(),
471471 } }) };
472472 }
473 break :op .{ .mem = Memory.rip(mem_op.sib.ptr_size, 0) };
473 break :op .{ .mem = Memory.initRip(mem_op.sib.ptr_size, 0) };
474474 },
475475 else => unreachable,
476476 } else switch (mnemonic) {
477 .call => break :op .{ .mem = Memory.sib(mem_op.sib.ptr_size, .{
477 .call => break :op .{ .mem = Memory.initSib(mem_op.sib.ptr_size, .{
478478 .base = .{ .reg = .ds },
479479 }) },
480480 .lea => {
481481 emit_mnemonic = .mov;
482482 break :op .{ .imm = Immediate.s(0) };
483483 },
484 .mov => break :op .{ .mem = Memory.sib(mem_op.sib.ptr_size, .{
484 .mov => break :op .{ .mem = Memory.initSib(mem_op.sib.ptr_size, .{
485485 .base = .{ .reg = .ds },
486486 }) },
487487 else => unreachable,
......@@ -495,12 +495,12 @@ fn emit(lower: *Lower, prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand)
495495 lower.result_insts[lower.result_insts_len] =
496496 try Instruction.new(.none, .mov, &[_]Operand{
497497 .{ .reg = .rdi },
498 .{ .mem = Memory.rip(mem_op.sib.ptr_size, 0) },
498 .{ .mem = Memory.initRip(mem_op.sib.ptr_size, 0) },
499499 });
500500 lower.result_insts_len += 1;
501501 lower.result_insts[lower.result_insts_len] =
502502 try Instruction.new(.none, .call, &[_]Operand{
503 .{ .mem = Memory.sib(.qword, .{ .base = .{ .reg = .rdi } }) },
503 .{ .mem = Memory.initSib(.qword, .{ .base = .{ .reg = .rdi } }) },
504504 });
505505 lower.result_insts_len += 1;
506506 emit_mnemonic = .mov;
......@@ -511,7 +511,7 @@ fn emit(lower: *Lower, prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand)
511511 break :op switch (mnemonic) {
512512 .lea => {
513513 if (macho_sym.flags.is_extern_ptr) emit_mnemonic = .mov;
514 break :op .{ .mem = Memory.rip(mem_op.sib.ptr_size, 0) };
514 break :op .{ .mem = Memory.initRip(mem_op.sib.ptr_size, 0) };
515515 },
516516 .mov => {
517517 if (macho_sym.flags.is_extern_ptr) {
......@@ -519,14 +519,14 @@ fn emit(lower: *Lower, prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand)
519519 lower.result_insts[lower.result_insts_len] =
520520 try Instruction.new(.none, .mov, &[_]Operand{
521521 .{ .reg = reg.to64() },
522 .{ .mem = Memory.rip(.qword, 0) },
522 .{ .mem = Memory.initRip(.qword, 0) },
523523 });
524524 lower.result_insts_len += 1;
525 break :op .{ .mem = Memory.sib(mem_op.sib.ptr_size, .{ .base = .{
525 break :op .{ .mem = Memory.initSib(mem_op.sib.ptr_size, .{ .base = .{
526526 .reg = reg.to64(),
527527 } }) };
528528 }
529 break :op .{ .mem = Memory.rip(mem_op.sib.ptr_size, 0) };
529 break :op .{ .mem = Memory.initRip(mem_op.sib.ptr_size, 0) };
530530 },
531531 else => unreachable,
532532 };
......@@ -701,7 +701,7 @@ fn generic(lower: *Lower, inst: Mir.Inst) Error!void {
701701 }, extra.off);
702702 break :ops &.{
703703 .{ .reg = reg },
704 .{ .mem = Memory.rip(Memory.PtrSize.fromBitSize(reg.bitSize()), 0) },
704 .{ .mem = Memory.initRip(Memory.PtrSize.fromBitSize(reg.bitSize()), 0) },
705705 };
706706 },
707707 else => return lower.fail("TODO lower {s} {s}", .{ @tagName(inst.tag), @tagName(inst.ops) }),
src/arch/x86_64/Mir.zig+3-3
......@@ -1234,9 +1234,9 @@ pub const Memory = struct {
12341234 .rm => {
12351235 if (mem.info.base == .reg and @as(Register, @enumFromInt(mem.base)) == .rip) {
12361236 assert(mem.info.index == .none and mem.info.scale == .@"1");
1237 return encoder.Instruction.Memory.rip(mem.info.size, @bitCast(mem.off));
1237 return encoder.Instruction.Memory.initRip(mem.info.size, @bitCast(mem.off));
12381238 }
1239 return encoder.Instruction.Memory.sib(mem.info.size, .{
1239 return encoder.Instruction.Memory.initSib(mem.info.size, .{
12401240 .disp = @bitCast(mem.off),
12411241 .base = switch (mem.info.base) {
12421242 .none => .none,
......@@ -1258,7 +1258,7 @@ pub const Memory = struct {
12581258 },
12591259 .off => {
12601260 assert(mem.info.base == .reg);
1261 return encoder.Instruction.Memory.moffs(
1261 return encoder.Instruction.Memory.initMoffs(
12621262 @enumFromInt(mem.base),
12631263 @as(u64, mem.extra) << 32 | mem.off,
12641264 );
src/arch/x86_64/encoder.zig+77-77
......@@ -110,12 +110,12 @@ pub const Instruction = struct {
110110 offset: u64,
111111 };
112112
113 pub fn moffs(reg: Register, offset: u64) Memory {
113 pub fn initMoffs(reg: Register, offset: u64) Memory {
114114 assert(reg.class() == .segment);
115115 return .{ .moffs = .{ .seg = reg, .offset = offset } };
116116 }
117117
118 pub fn sib(ptr_size: PtrSize, args: struct {
118 pub fn initSib(ptr_size: PtrSize, args: struct {
119119 disp: i32 = 0,
120120 base: Base = .none,
121121 scale_index: ?ScaleIndex = null,
......@@ -129,7 +129,7 @@ pub const Instruction = struct {
129129 } };
130130 }
131131
132 pub fn rip(ptr_size: PtrSize, displacement: i32) Memory {
132 pub fn initRip(ptr_size: PtrSize, displacement: i32) Memory {
133133 return .{ .rip = .{ .ptr_size = ptr_size, .disp = displacement } };
134134 }
135135
......@@ -1266,7 +1266,7 @@ test "lower MI encoding" {
12661266 try expectEqualHexStrings("\x49\xC7\xC4\x00\x10\x00\x00", enc.code(), "mov r12, 0x1000");
12671267
12681268 try enc.encode(.mov, &.{
1269 .{ .mem = Instruction.Memory.sib(.byte, .{ .base = .{ .reg = .r12 } }) },
1269 .{ .mem = Instruction.Memory.initSib(.byte, .{ .base = .{ .reg = .r12 } }) },
12701270 .{ .imm = Instruction.Immediate.u(0x10) },
12711271 });
12721272 try expectEqualHexStrings("\x41\xC6\x04\x24\x10", enc.code(), "mov BYTE PTR [r12], 0x10");
......@@ -1290,13 +1290,13 @@ test "lower MI encoding" {
12901290 try expectEqualHexStrings("\x48\xc7\xc0\x10\x00\x00\x00", enc.code(), "mov rax, 0x10");
12911291
12921292 try enc.encode(.mov, &.{
1293 .{ .mem = Instruction.Memory.sib(.dword, .{ .base = .{ .reg = .r11 } }) },
1293 .{ .mem = Instruction.Memory.initSib(.dword, .{ .base = .{ .reg = .r11 } }) },
12941294 .{ .imm = Instruction.Immediate.u(0x10) },
12951295 });
12961296 try expectEqualHexStrings("\x41\xc7\x03\x10\x00\x00\x00", enc.code(), "mov DWORD PTR [r11], 0x10");
12971297
12981298 try enc.encode(.mov, &.{
1299 .{ .mem = Instruction.Memory.rip(.qword, 0x10) },
1299 .{ .mem = Instruction.Memory.initRip(.qword, 0x10) },
13001300 .{ .imm = Instruction.Immediate.u(0x10) },
13011301 });
13021302 try expectEqualHexStrings(
......@@ -1306,25 +1306,25 @@ test "lower MI encoding" {
13061306 );
13071307
13081308 try enc.encode(.mov, &.{
1309 .{ .mem = Instruction.Memory.sib(.qword, .{ .base = .{ .reg = .rbp }, .disp = -8 }) },
1309 .{ .mem = Instruction.Memory.initSib(.qword, .{ .base = .{ .reg = .rbp }, .disp = -8 }) },
13101310 .{ .imm = Instruction.Immediate.u(0x10) },
13111311 });
13121312 try expectEqualHexStrings("\x48\xc7\x45\xf8\x10\x00\x00\x00", enc.code(), "mov QWORD PTR [rbp - 8], 0x10");
13131313
13141314 try enc.encode(.mov, &.{
1315 .{ .mem = Instruction.Memory.sib(.word, .{ .base = .{ .reg = .rbp }, .disp = -2 }) },
1315 .{ .mem = Instruction.Memory.initSib(.word, .{ .base = .{ .reg = .rbp }, .disp = -2 }) },
13161316 .{ .imm = Instruction.Immediate.s(-16) },
13171317 });
13181318 try expectEqualHexStrings("\x66\xC7\x45\xFE\xF0\xFF", enc.code(), "mov WORD PTR [rbp - 2], -16");
13191319
13201320 try enc.encode(.mov, &.{
1321 .{ .mem = Instruction.Memory.sib(.byte, .{ .base = .{ .reg = .rbp }, .disp = -1 }) },
1321 .{ .mem = Instruction.Memory.initSib(.byte, .{ .base = .{ .reg = .rbp }, .disp = -1 }) },
13221322 .{ .imm = Instruction.Immediate.u(0x10) },
13231323 });
13241324 try expectEqualHexStrings("\xC6\x45\xFF\x10", enc.code(), "mov BYTE PTR [rbp - 1], 0x10");
13251325
13261326 try enc.encode(.mov, &.{
1327 .{ .mem = Instruction.Memory.sib(.qword, .{
1327 .{ .mem = Instruction.Memory.initSib(.qword, .{
13281328 .base = .{ .reg = .ds },
13291329 .disp = 0x10000000,
13301330 .scale_index = .{ .scale = 2, .index = .rcx },
......@@ -1338,13 +1338,13 @@ test "lower MI encoding" {
13381338 );
13391339
13401340 try enc.encode(.adc, &.{
1341 .{ .mem = Instruction.Memory.sib(.byte, .{ .base = .{ .reg = .rbp }, .disp = -0x10 }) },
1341 .{ .mem = Instruction.Memory.initSib(.byte, .{ .base = .{ .reg = .rbp }, .disp = -0x10 }) },
13421342 .{ .imm = Instruction.Immediate.u(0x10) },
13431343 });
13441344 try expectEqualHexStrings("\x80\x55\xF0\x10", enc.code(), "adc BYTE PTR [rbp - 0x10], 0x10");
13451345
13461346 try enc.encode(.adc, &.{
1347 .{ .mem = Instruction.Memory.rip(.qword, 0) },
1347 .{ .mem = Instruction.Memory.initRip(.qword, 0) },
13481348 .{ .imm = Instruction.Immediate.u(0x10) },
13491349 });
13501350 try expectEqualHexStrings("\x48\x83\x15\x00\x00\x00\x00\x10", enc.code(), "adc QWORD PTR [rip], 0x10");
......@@ -1356,7 +1356,7 @@ test "lower MI encoding" {
13561356 try expectEqualHexStrings("\x48\x83\xD0\x10", enc.code(), "adc rax, 0x10");
13571357
13581358 try enc.encode(.add, &.{
1359 .{ .mem = Instruction.Memory.sib(.dword, .{ .base = .{ .reg = .rdx }, .disp = -8 }) },
1359 .{ .mem = Instruction.Memory.initSib(.dword, .{ .base = .{ .reg = .rdx }, .disp = -8 }) },
13601360 .{ .imm = Instruction.Immediate.u(0x10) },
13611361 });
13621362 try expectEqualHexStrings("\x83\x42\xF8\x10", enc.code(), "add DWORD PTR [rdx - 8], 0x10");
......@@ -1368,13 +1368,13 @@ test "lower MI encoding" {
13681368 try expectEqualHexStrings("\x48\x83\xC0\x10", enc.code(), "add rax, 0x10");
13691369
13701370 try enc.encode(.add, &.{
1371 .{ .mem = Instruction.Memory.sib(.qword, .{ .base = .{ .reg = .rbp }, .disp = -0x10 }) },
1371 .{ .mem = Instruction.Memory.initSib(.qword, .{ .base = .{ .reg = .rbp }, .disp = -0x10 }) },
13721372 .{ .imm = Instruction.Immediate.s(-0x10) },
13731373 });
13741374 try expectEqualHexStrings("\x48\x83\x45\xF0\xF0", enc.code(), "add QWORD PTR [rbp - 0x10], -0x10");
13751375
13761376 try enc.encode(.@"and", &.{
1377 .{ .mem = Instruction.Memory.sib(.dword, .{ .base = .{ .reg = .ds }, .disp = 0x10000000 }) },
1377 .{ .mem = Instruction.Memory.initSib(.dword, .{ .base = .{ .reg = .ds }, .disp = 0x10000000 }) },
13781378 .{ .imm = Instruction.Immediate.u(0x10) },
13791379 });
13801380 try expectEqualHexStrings(
......@@ -1384,7 +1384,7 @@ test "lower MI encoding" {
13841384 );
13851385
13861386 try enc.encode(.@"and", &.{
1387 .{ .mem = Instruction.Memory.sib(.dword, .{ .base = .{ .reg = .es }, .disp = 0x10000000 }) },
1387 .{ .mem = Instruction.Memory.initSib(.dword, .{ .base = .{ .reg = .es }, .disp = 0x10000000 }) },
13881388 .{ .imm = Instruction.Immediate.u(0x10) },
13891389 });
13901390 try expectEqualHexStrings(
......@@ -1394,7 +1394,7 @@ test "lower MI encoding" {
13941394 );
13951395
13961396 try enc.encode(.@"and", &.{
1397 .{ .mem = Instruction.Memory.sib(.dword, .{ .base = .{ .reg = .r12 }, .disp = 0x10000000 }) },
1397 .{ .mem = Instruction.Memory.initSib(.dword, .{ .base = .{ .reg = .r12 }, .disp = 0x10000000 }) },
13981398 .{ .imm = Instruction.Immediate.u(0x10) },
13991399 });
14001400 try expectEqualHexStrings(
......@@ -1404,7 +1404,7 @@ test "lower MI encoding" {
14041404 );
14051405
14061406 try enc.encode(.sub, &.{
1407 .{ .mem = Instruction.Memory.sib(.dword, .{ .base = .{ .reg = .r11 }, .disp = 0x10000000 }) },
1407 .{ .mem = Instruction.Memory.initSib(.dword, .{ .base = .{ .reg = .r11 }, .disp = 0x10000000 }) },
14081408 .{ .imm = Instruction.Immediate.u(0x10) },
14091409 });
14101410 try expectEqualHexStrings(
......@@ -1419,25 +1419,25 @@ test "lower RM encoding" {
14191419
14201420 try enc.encode(.mov, &.{
14211421 .{ .reg = .rax },
1422 .{ .mem = Instruction.Memory.sib(.qword, .{ .base = .{ .reg = .r11 } }) },
1422 .{ .mem = Instruction.Memory.initSib(.qword, .{ .base = .{ .reg = .r11 } }) },
14231423 });
14241424 try expectEqualHexStrings("\x49\x8b\x03", enc.code(), "mov rax, QWORD PTR [r11]");
14251425
14261426 try enc.encode(.mov, &.{
14271427 .{ .reg = .rbx },
1428 .{ .mem = Instruction.Memory.sib(.qword, .{ .base = .{ .reg = .ds }, .disp = 0x10 }) },
1428 .{ .mem = Instruction.Memory.initSib(.qword, .{ .base = .{ .reg = .ds }, .disp = 0x10 }) },
14291429 });
14301430 try expectEqualHexStrings("\x48\x8B\x1C\x25\x10\x00\x00\x00", enc.code(), "mov rbx, QWORD PTR ds:0x10");
14311431
14321432 try enc.encode(.mov, &.{
14331433 .{ .reg = .rax },
1434 .{ .mem = Instruction.Memory.sib(.qword, .{ .base = .{ .reg = .rbp }, .disp = -4 }) },
1434 .{ .mem = Instruction.Memory.initSib(.qword, .{ .base = .{ .reg = .rbp }, .disp = -4 }) },
14351435 });
14361436 try expectEqualHexStrings("\x48\x8B\x45\xFC", enc.code(), "mov rax, QWORD PTR [rbp - 4]");
14371437
14381438 try enc.encode(.mov, &.{
14391439 .{ .reg = .rax },
1440 .{ .mem = Instruction.Memory.sib(.qword, .{
1440 .{ .mem = Instruction.Memory.initSib(.qword, .{
14411441 .base = .{ .reg = .rbp },
14421442 .scale_index = .{ .scale = 1, .index = .rcx },
14431443 .disp = -8,
......@@ -1447,7 +1447,7 @@ test "lower RM encoding" {
14471447
14481448 try enc.encode(.mov, &.{
14491449 .{ .reg = .eax },
1450 .{ .mem = Instruction.Memory.sib(.dword, .{
1450 .{ .mem = Instruction.Memory.initSib(.dword, .{
14511451 .base = .{ .reg = .rbp },
14521452 .scale_index = .{ .scale = 4, .index = .rdx },
14531453 .disp = -4,
......@@ -1457,7 +1457,7 @@ test "lower RM encoding" {
14571457
14581458 try enc.encode(.mov, &.{
14591459 .{ .reg = .rax },
1460 .{ .mem = Instruction.Memory.sib(.qword, .{
1460 .{ .mem = Instruction.Memory.initSib(.qword, .{
14611461 .base = .{ .reg = .rbp },
14621462 .scale_index = .{ .scale = 8, .index = .rcx },
14631463 .disp = -8,
......@@ -1467,7 +1467,7 @@ test "lower RM encoding" {
14671467
14681468 try enc.encode(.mov, &.{
14691469 .{ .reg = .r8b },
1470 .{ .mem = Instruction.Memory.sib(.byte, .{
1470 .{ .mem = Instruction.Memory.initSib(.byte, .{
14711471 .base = .{ .reg = .rsi },
14721472 .scale_index = .{ .scale = 1, .index = .rcx },
14731473 .disp = -24,
......@@ -1483,7 +1483,7 @@ test "lower RM encoding" {
14831483 try expectEqualHexStrings("\x48\x8C\xC8", enc.code(), "mov rax, cs");
14841484
14851485 try enc.encode(.mov, &.{
1486 .{ .mem = Instruction.Memory.sib(.word, .{ .base = .{ .reg = .rbp }, .disp = -16 }) },
1486 .{ .mem = Instruction.Memory.initSib(.word, .{ .base = .{ .reg = .rbp }, .disp = -16 }) },
14871487 .{ .reg = .fs },
14881488 });
14891489 try expectEqualHexStrings("\x8C\x65\xF0", enc.code(), "mov WORD PTR [rbp - 16], fs");
......@@ -1514,19 +1514,19 @@ test "lower RM encoding" {
15141514
15151515 try enc.encode(.movsx, &.{
15161516 .{ .reg = .eax },
1517 .{ .mem = Instruction.Memory.sib(.word, .{ .base = .{ .reg = .rbp } }) },
1517 .{ .mem = Instruction.Memory.initSib(.word, .{ .base = .{ .reg = .rbp } }) },
15181518 });
15191519 try expectEqualHexStrings("\x0F\xBF\x45\x00", enc.code(), "movsx eax, BYTE PTR [rbp]");
15201520
15211521 try enc.encode(.movsx, &.{
15221522 .{ .reg = .eax },
1523 .{ .mem = Instruction.Memory.sib(.byte, .{ .scale_index = .{ .index = .rax, .scale = 2 } }) },
1523 .{ .mem = Instruction.Memory.initSib(.byte, .{ .scale_index = .{ .index = .rax, .scale = 2 } }) },
15241524 });
15251525 try expectEqualHexStrings("\x0F\xBE\x04\x45\x00\x00\x00\x00", enc.code(), "movsx eax, BYTE PTR [rax * 2]");
15261526
15271527 try enc.encode(.movsx, &.{
15281528 .{ .reg = .ax },
1529 .{ .mem = Instruction.Memory.rip(.byte, 0x10) },
1529 .{ .mem = Instruction.Memory.initRip(.byte, 0x10) },
15301530 });
15311531 try expectEqualHexStrings("\x66\x0F\xBE\x05\x10\x00\x00\x00", enc.code(), "movsx ax, BYTE PTR [rip + 0x10]");
15321532
......@@ -1544,37 +1544,37 @@ test "lower RM encoding" {
15441544
15451545 try enc.encode(.lea, &.{
15461546 .{ .reg = .rax },
1547 .{ .mem = Instruction.Memory.rip(.qword, 0x10) },
1547 .{ .mem = Instruction.Memory.initRip(.qword, 0x10) },
15481548 });
15491549 try expectEqualHexStrings("\x48\x8D\x05\x10\x00\x00\x00", enc.code(), "lea rax, QWORD PTR [rip + 0x10]");
15501550
15511551 try enc.encode(.lea, &.{
15521552 .{ .reg = .rax },
1553 .{ .mem = Instruction.Memory.rip(.dword, 0x10) },
1553 .{ .mem = Instruction.Memory.initRip(.dword, 0x10) },
15541554 });
15551555 try expectEqualHexStrings("\x48\x8D\x05\x10\x00\x00\x00", enc.code(), "lea rax, DWORD PTR [rip + 0x10]");
15561556
15571557 try enc.encode(.lea, &.{
15581558 .{ .reg = .eax },
1559 .{ .mem = Instruction.Memory.rip(.dword, 0x10) },
1559 .{ .mem = Instruction.Memory.initRip(.dword, 0x10) },
15601560 });
15611561 try expectEqualHexStrings("\x8D\x05\x10\x00\x00\x00", enc.code(), "lea eax, DWORD PTR [rip + 0x10]");
15621562
15631563 try enc.encode(.lea, &.{
15641564 .{ .reg = .eax },
1565 .{ .mem = Instruction.Memory.rip(.word, 0x10) },
1565 .{ .mem = Instruction.Memory.initRip(.word, 0x10) },
15661566 });
15671567 try expectEqualHexStrings("\x8D\x05\x10\x00\x00\x00", enc.code(), "lea eax, WORD PTR [rip + 0x10]");
15681568
15691569 try enc.encode(.lea, &.{
15701570 .{ .reg = .ax },
1571 .{ .mem = Instruction.Memory.rip(.byte, 0x10) },
1571 .{ .mem = Instruction.Memory.initRip(.byte, 0x10) },
15721572 });
15731573 try expectEqualHexStrings("\x66\x8D\x05\x10\x00\x00\x00", enc.code(), "lea ax, BYTE PTR [rip + 0x10]");
15741574
15751575 try enc.encode(.lea, &.{
15761576 .{ .reg = .rsi },
1577 .{ .mem = Instruction.Memory.sib(.qword, .{
1577 .{ .mem = Instruction.Memory.initSib(.qword, .{
15781578 .base = .{ .reg = .rbp },
15791579 .scale_index = .{ .scale = 1, .index = .rcx },
15801580 }) },
......@@ -1583,31 +1583,31 @@ test "lower RM encoding" {
15831583
15841584 try enc.encode(.add, &.{
15851585 .{ .reg = .r11 },
1586 .{ .mem = Instruction.Memory.sib(.qword, .{ .base = .{ .reg = .ds }, .disp = 0x10000000 }) },
1586 .{ .mem = Instruction.Memory.initSib(.qword, .{ .base = .{ .reg = .ds }, .disp = 0x10000000 }) },
15871587 });
15881588 try expectEqualHexStrings("\x4C\x03\x1C\x25\x00\x00\x00\x10", enc.code(), "add r11, QWORD PTR ds:0x10000000");
15891589
15901590 try enc.encode(.add, &.{
15911591 .{ .reg = .r12b },
1592 .{ .mem = Instruction.Memory.sib(.byte, .{ .base = .{ .reg = .ds }, .disp = 0x10000000 }) },
1592 .{ .mem = Instruction.Memory.initSib(.byte, .{ .base = .{ .reg = .ds }, .disp = 0x10000000 }) },
15931593 });
15941594 try expectEqualHexStrings("\x44\x02\x24\x25\x00\x00\x00\x10", enc.code(), "add r11b, BYTE PTR ds:0x10000000");
15951595
15961596 try enc.encode(.add, &.{
15971597 .{ .reg = .r12b },
1598 .{ .mem = Instruction.Memory.sib(.byte, .{ .base = .{ .reg = .fs }, .disp = 0x10000000 }) },
1598 .{ .mem = Instruction.Memory.initSib(.byte, .{ .base = .{ .reg = .fs }, .disp = 0x10000000 }) },
15991599 });
16001600 try expectEqualHexStrings("\x64\x44\x02\x24\x25\x00\x00\x00\x10", enc.code(), "add r11b, BYTE PTR fs:0x10000000");
16011601
16021602 try enc.encode(.sub, &.{
16031603 .{ .reg = .r11 },
1604 .{ .mem = Instruction.Memory.sib(.qword, .{ .base = .{ .reg = .r13 }, .disp = 0x10000000 }) },
1604 .{ .mem = Instruction.Memory.initSib(.qword, .{ .base = .{ .reg = .r13 }, .disp = 0x10000000 }) },
16051605 });
16061606 try expectEqualHexStrings("\x4D\x2B\x9D\x00\x00\x00\x10", enc.code(), "sub r11, QWORD PTR [r13 + 0x10000000]");
16071607
16081608 try enc.encode(.sub, &.{
16091609 .{ .reg = .r11 },
1610 .{ .mem = Instruction.Memory.sib(.qword, .{ .base = .{ .reg = .r12 }, .disp = 0x10000000 }) },
1610 .{ .mem = Instruction.Memory.initSib(.qword, .{ .base = .{ .reg = .r12 }, .disp = 0x10000000 }) },
16111611 });
16121612 try expectEqualHexStrings("\x4D\x2B\x9C\x24\x00\x00\x00\x10", enc.code(), "sub r11, QWORD PTR [r12 + 0x10000000]");
16131613
......@@ -1630,7 +1630,7 @@ test "lower RMI encoding" {
16301630
16311631 try enc.encode(.imul, &.{
16321632 .{ .reg = .r11 },
1633 .{ .mem = Instruction.Memory.rip(.qword, -16) },
1633 .{ .mem = Instruction.Memory.initRip(.qword, -16) },
16341634 .{ .imm = Instruction.Immediate.s(-1024) },
16351635 });
16361636 try expectEqualHexStrings(
......@@ -1641,7 +1641,7 @@ test "lower RMI encoding" {
16411641
16421642 try enc.encode(.imul, &.{
16431643 .{ .reg = .bx },
1644 .{ .mem = Instruction.Memory.sib(.word, .{ .base = .{ .reg = .rbp }, .disp = -16 }) },
1644 .{ .mem = Instruction.Memory.initSib(.word, .{ .base = .{ .reg = .rbp }, .disp = -16 }) },
16451645 .{ .imm = Instruction.Immediate.s(-1024) },
16461646 });
16471647 try expectEqualHexStrings(
......@@ -1652,7 +1652,7 @@ test "lower RMI encoding" {
16521652
16531653 try enc.encode(.imul, &.{
16541654 .{ .reg = .bx },
1655 .{ .mem = Instruction.Memory.sib(.word, .{ .base = .{ .reg = .rbp }, .disp = -16 }) },
1655 .{ .mem = Instruction.Memory.initSib(.word, .{ .base = .{ .reg = .rbp }, .disp = -16 }) },
16561656 .{ .imm = Instruction.Immediate.u(1024) },
16571657 });
16581658 try expectEqualHexStrings(
......@@ -1672,19 +1672,19 @@ test "lower MR encoding" {
16721672 try expectEqualHexStrings("\x48\x89\xD8", enc.code(), "mov rax, rbx");
16731673
16741674 try enc.encode(.mov, &.{
1675 .{ .mem = Instruction.Memory.sib(.qword, .{ .base = .{ .reg = .rbp }, .disp = -4 }) },
1675 .{ .mem = Instruction.Memory.initSib(.qword, .{ .base = .{ .reg = .rbp }, .disp = -4 }) },
16761676 .{ .reg = .r11 },
16771677 });
16781678 try expectEqualHexStrings("\x4c\x89\x5d\xfc", enc.code(), "mov QWORD PTR [rbp - 4], r11");
16791679
16801680 try enc.encode(.mov, &.{
1681 .{ .mem = Instruction.Memory.rip(.qword, 0x10) },
1681 .{ .mem = Instruction.Memory.initRip(.qword, 0x10) },
16821682 .{ .reg = .r12 },
16831683 });
16841684 try expectEqualHexStrings("\x4C\x89\x25\x10\x00\x00\x00", enc.code(), "mov QWORD PTR [rip + 0x10], r12");
16851685
16861686 try enc.encode(.mov, &.{
1687 .{ .mem = Instruction.Memory.sib(.qword, .{
1687 .{ .mem = Instruction.Memory.initSib(.qword, .{
16881688 .base = .{ .reg = .r11 },
16891689 .scale_index = .{ .scale = 2, .index = .r12 },
16901690 .disp = 0x10,
......@@ -1694,13 +1694,13 @@ test "lower MR encoding" {
16941694 try expectEqualHexStrings("\x4F\x89\x6C\x63\x10", enc.code(), "mov QWORD PTR [r11 + 2 * r12 + 0x10], r13");
16951695
16961696 try enc.encode(.mov, &.{
1697 .{ .mem = Instruction.Memory.rip(.word, -0x10) },
1697 .{ .mem = Instruction.Memory.initRip(.word, -0x10) },
16981698 .{ .reg = .r12w },
16991699 });
17001700 try expectEqualHexStrings("\x66\x44\x89\x25\xF0\xFF\xFF\xFF", enc.code(), "mov WORD PTR [rip - 0x10], r12w");
17011701
17021702 try enc.encode(.mov, &.{
1703 .{ .mem = Instruction.Memory.sib(.byte, .{
1703 .{ .mem = Instruction.Memory.initSib(.byte, .{
17041704 .base = .{ .reg = .r11 },
17051705 .scale_index = .{ .scale = 2, .index = .r12 },
17061706 .disp = 0x10,
......@@ -1710,25 +1710,25 @@ test "lower MR encoding" {
17101710 try expectEqualHexStrings("\x47\x88\x6C\x63\x10", enc.code(), "mov BYTE PTR [r11 + 2 * r12 + 0x10], r13b");
17111711
17121712 try enc.encode(.add, &.{
1713 .{ .mem = Instruction.Memory.sib(.byte, .{ .base = .{ .reg = .ds }, .disp = 0x10000000 }) },
1713 .{ .mem = Instruction.Memory.initSib(.byte, .{ .base = .{ .reg = .ds }, .disp = 0x10000000 }) },
17141714 .{ .reg = .r12b },
17151715 });
17161716 try expectEqualHexStrings("\x44\x00\x24\x25\x00\x00\x00\x10", enc.code(), "add BYTE PTR ds:0x10000000, r12b");
17171717
17181718 try enc.encode(.add, &.{
1719 .{ .mem = Instruction.Memory.sib(.dword, .{ .base = .{ .reg = .ds }, .disp = 0x10000000 }) },
1719 .{ .mem = Instruction.Memory.initSib(.dword, .{ .base = .{ .reg = .ds }, .disp = 0x10000000 }) },
17201720 .{ .reg = .r12d },
17211721 });
17221722 try expectEqualHexStrings("\x44\x01\x24\x25\x00\x00\x00\x10", enc.code(), "add DWORD PTR [ds:0x10000000], r12d");
17231723
17241724 try enc.encode(.add, &.{
1725 .{ .mem = Instruction.Memory.sib(.dword, .{ .base = .{ .reg = .gs }, .disp = 0x10000000 }) },
1725 .{ .mem = Instruction.Memory.initSib(.dword, .{ .base = .{ .reg = .gs }, .disp = 0x10000000 }) },
17261726 .{ .reg = .r12d },
17271727 });
17281728 try expectEqualHexStrings("\x65\x44\x01\x24\x25\x00\x00\x00\x10", enc.code(), "add DWORD PTR [gs:0x10000000], r12d");
17291729
17301730 try enc.encode(.sub, &.{
1731 .{ .mem = Instruction.Memory.sib(.qword, .{ .base = .{ .reg = .r11 }, .disp = 0x10000000 }) },
1731 .{ .mem = Instruction.Memory.initSib(.qword, .{ .base = .{ .reg = .r11 }, .disp = 0x10000000 }) },
17321732 .{ .reg = .r12 },
17331733 });
17341734 try expectEqualHexStrings("\x4D\x29\xA3\x00\x00\x00\x10", enc.code(), "sub QWORD PTR [r11 + 0x10000000], r12");
......@@ -1743,12 +1743,12 @@ test "lower M encoding" {
17431743 try expectEqualHexStrings("\x41\xFF\xD4", enc.code(), "call r12");
17441744
17451745 try enc.encode(.call, &.{
1746 .{ .mem = Instruction.Memory.sib(.qword, .{ .base = .{ .reg = .r12 } }) },
1746 .{ .mem = Instruction.Memory.initSib(.qword, .{ .base = .{ .reg = .r12 } }) },
17471747 });
17481748 try expectEqualHexStrings("\x41\xFF\x14\x24", enc.code(), "call QWORD PTR [r12]");
17491749
17501750 try enc.encode(.call, &.{
1751 .{ .mem = Instruction.Memory.sib(.qword, .{
1751 .{ .mem = Instruction.Memory.initSib(.qword, .{
17521752 .base = .none,
17531753 .scale_index = .{ .index = .r11, .scale = 2 },
17541754 }) },
......@@ -1756,7 +1756,7 @@ test "lower M encoding" {
17561756 try expectEqualHexStrings("\x42\xFF\x14\x5D\x00\x00\x00\x00", enc.code(), "call QWORD PTR [r11 * 2]");
17571757
17581758 try enc.encode(.call, &.{
1759 .{ .mem = Instruction.Memory.sib(.qword, .{
1759 .{ .mem = Instruction.Memory.initSib(.qword, .{
17601760 .base = .none,
17611761 .scale_index = .{ .index = .r12, .scale = 2 },
17621762 }) },
......@@ -1764,7 +1764,7 @@ test "lower M encoding" {
17641764 try expectEqualHexStrings("\x42\xFF\x14\x65\x00\x00\x00\x00", enc.code(), "call QWORD PTR [r12 * 2]");
17651765
17661766 try enc.encode(.call, &.{
1767 .{ .mem = Instruction.Memory.sib(.qword, .{ .base = .{ .reg = .gs } }) },
1767 .{ .mem = Instruction.Memory.initSib(.qword, .{ .base = .{ .reg = .gs } }) },
17681768 });
17691769 try expectEqualHexStrings("\x65\xFF\x14\x25\x00\x00\x00\x00", enc.code(), "call gs:0x0");
17701770
......@@ -1774,22 +1774,22 @@ test "lower M encoding" {
17741774 try expectEqualHexStrings("\xE8\x00\x00\x00\x00", enc.code(), "call 0x0");
17751775
17761776 try enc.encode(.push, &.{
1777 .{ .mem = Instruction.Memory.sib(.qword, .{ .base = .{ .reg = .rbp } }) },
1777 .{ .mem = Instruction.Memory.initSib(.qword, .{ .base = .{ .reg = .rbp } }) },
17781778 });
17791779 try expectEqualHexStrings("\xFF\x75\x00", enc.code(), "push QWORD PTR [rbp]");
17801780
17811781 try enc.encode(.push, &.{
1782 .{ .mem = Instruction.Memory.sib(.word, .{ .base = .{ .reg = .rbp } }) },
1782 .{ .mem = Instruction.Memory.initSib(.word, .{ .base = .{ .reg = .rbp } }) },
17831783 });
17841784 try expectEqualHexStrings("\x66\xFF\x75\x00", enc.code(), "push QWORD PTR [rbp]");
17851785
17861786 try enc.encode(.pop, &.{
1787 .{ .mem = Instruction.Memory.rip(.qword, 0) },
1787 .{ .mem = Instruction.Memory.initRip(.qword, 0) },
17881788 });
17891789 try expectEqualHexStrings("\x8F\x05\x00\x00\x00\x00", enc.code(), "pop QWORD PTR [rip]");
17901790
17911791 try enc.encode(.pop, &.{
1792 .{ .mem = Instruction.Memory.rip(.word, 0) },
1792 .{ .mem = Instruction.Memory.initRip(.word, 0) },
17931793 });
17941794 try expectEqualHexStrings("\x66\x8F\x05\x00\x00\x00\x00", enc.code(), "pop WORD PTR [rbp]");
17951795
......@@ -1870,48 +1870,48 @@ test "lower FD/TD encoding" {
18701870
18711871 try enc.encode(.mov, &.{
18721872 .{ .reg = .rax },
1873 .{ .mem = Instruction.Memory.moffs(.cs, 0x10) },
1873 .{ .mem = Instruction.Memory.initMoffs(.cs, 0x10) },
18741874 });
18751875 try expectEqualHexStrings("\x2E\x48\xA1\x10\x00\x00\x00\x00\x00\x00\x00", enc.code(), "movabs rax, cs:0x10");
18761876
18771877 try enc.encode(.mov, &.{
18781878 .{ .reg = .eax },
1879 .{ .mem = Instruction.Memory.moffs(.fs, 0x10) },
1879 .{ .mem = Instruction.Memory.initMoffs(.fs, 0x10) },
18801880 });
18811881 try expectEqualHexStrings("\x64\xA1\x10\x00\x00\x00\x00\x00\x00\x00", enc.code(), "movabs eax, fs:0x10");
18821882
18831883 try enc.encode(.mov, &.{
18841884 .{ .reg = .ax },
1885 .{ .mem = Instruction.Memory.moffs(.gs, 0x10) },
1885 .{ .mem = Instruction.Memory.initMoffs(.gs, 0x10) },
18861886 });
18871887 try expectEqualHexStrings("\x65\x66\xA1\x10\x00\x00\x00\x00\x00\x00\x00", enc.code(), "movabs ax, gs:0x10");
18881888
18891889 try enc.encode(.mov, &.{
18901890 .{ .reg = .al },
1891 .{ .mem = Instruction.Memory.moffs(.ds, 0x10) },
1891 .{ .mem = Instruction.Memory.initMoffs(.ds, 0x10) },
18921892 });
18931893 try expectEqualHexStrings("\xA0\x10\x00\x00\x00\x00\x00\x00\x00", enc.code(), "movabs al, ds:0x10");
18941894
18951895 try enc.encode(.mov, &.{
1896 .{ .mem = Instruction.Memory.moffs(.cs, 0x10) },
1896 .{ .mem = Instruction.Memory.initMoffs(.cs, 0x10) },
18971897 .{ .reg = .rax },
18981898 });
18991899 try expectEqualHexStrings("\x2E\x48\xA3\x10\x00\x00\x00\x00\x00\x00\x00", enc.code(), "movabs cs:0x10, rax");
19001900
19011901 try enc.encode(.mov, &.{
1902 .{ .mem = Instruction.Memory.moffs(.fs, 0x10) },
1902 .{ .mem = Instruction.Memory.initMoffs(.fs, 0x10) },
19031903 .{ .reg = .eax },
19041904 });
19051905 try expectEqualHexStrings("\x64\xA3\x10\x00\x00\x00\x00\x00\x00\x00", enc.code(), "movabs fs:0x10, eax");
19061906
19071907 try enc.encode(.mov, &.{
1908 .{ .mem = Instruction.Memory.moffs(.gs, 0x10) },
1908 .{ .mem = Instruction.Memory.initMoffs(.gs, 0x10) },
19091909 .{ .reg = .ax },
19101910 });
19111911 try expectEqualHexStrings("\x65\x66\xA3\x10\x00\x00\x00\x00\x00\x00\x00", enc.code(), "movabs gs:0x10, ax");
19121912
19131913 try enc.encode(.mov, &.{
1914 .{ .mem = Instruction.Memory.moffs(.ds, 0x10) },
1914 .{ .mem = Instruction.Memory.initMoffs(.ds, 0x10) },
19151915 .{ .reg = .al },
19161916 });
19171917 try expectEqualHexStrings("\xA2\x10\x00\x00\x00\x00\x00\x00\x00", enc.code(), "movabs ds:0x10, al");
......@@ -1949,16 +1949,16 @@ test "invalid instruction" {
19491949 .{ .reg = .al },
19501950 });
19511951 try invalidInstruction(.call, &.{
1952 .{ .mem = Instruction.Memory.rip(.dword, 0) },
1952 .{ .mem = Instruction.Memory.initRip(.dword, 0) },
19531953 });
19541954 try invalidInstruction(.call, &.{
1955 .{ .mem = Instruction.Memory.rip(.word, 0) },
1955 .{ .mem = Instruction.Memory.initRip(.word, 0) },
19561956 });
19571957 try invalidInstruction(.call, &.{
1958 .{ .mem = Instruction.Memory.rip(.byte, 0) },
1958 .{ .mem = Instruction.Memory.initRip(.byte, 0) },
19591959 });
19601960 try invalidInstruction(.mov, &.{
1961 .{ .mem = Instruction.Memory.rip(.word, 0x10) },
1961 .{ .mem = Instruction.Memory.initRip(.word, 0x10) },
19621962 .{ .reg = .r12 },
19631963 });
19641964 try invalidInstruction(.lea, &.{
......@@ -1967,7 +1967,7 @@ test "invalid instruction" {
19671967 });
19681968 try invalidInstruction(.lea, &.{
19691969 .{ .reg = .al },
1970 .{ .mem = Instruction.Memory.rip(.byte, 0) },
1970 .{ .mem = Instruction.Memory.initRip(.byte, 0) },
19711971 });
19721972 try invalidInstruction(.pop, &.{
19731973 .{ .reg = .r12b },
......@@ -1992,7 +1992,7 @@ fn cannotEncode(mnemonic: Instruction.Mnemonic, ops: []const Instruction.Operand
19921992
19931993test "cannot encode" {
19941994 try cannotEncode(.@"test", &.{
1995 .{ .mem = Instruction.Memory.sib(.byte, .{ .base = .{ .reg = .r12 } }) },
1995 .{ .mem = Instruction.Memory.initSib(.byte, .{ .base = .{ .reg = .r12 } }) },
19961996 .{ .reg = .ah },
19971997 });
19981998 try cannotEncode(.@"test", &.{
......@@ -2369,7 +2369,7 @@ const Assembler = struct {
23692369 if (res.rip) {
23702370 if (res.base != null or res.scale_index != null or res.offset != null)
23712371 return error.InvalidMemoryOperand;
2372 return Instruction.Memory.rip(ptr_size orelse .qword, res.disp orelse 0);
2372 return Instruction.Memory.initRip(ptr_size orelse .qword, res.disp orelse 0);
23732373 }
23742374 if (res.base) |base| {
23752375 if (res.rip)
......@@ -2377,9 +2377,9 @@ const Assembler = struct {
23772377 if (res.offset) |offset| {
23782378 if (res.scale_index != null or res.disp != null)
23792379 return error.InvalidMemoryOperand;
2380 return Instruction.Memory.moffs(base, offset);
2380 return Instruction.Memory.initMoffs(base, offset);
23812381 }
2382 return Instruction.Memory.sib(ptr_size orelse .qword, .{
2382 return Instruction.Memory.initSib(ptr_size orelse .qword, .{
23832383 .base = .{ .reg = base },
23842384 .scale_index = res.scale_index,
23852385 .disp = res.disp orelse 0,
src/codegen.zig+2-11
......@@ -836,16 +836,6 @@ pub const GenResult = union(enum) {
836836 /// Traditionally, this corresponds to emitting a relocation in a relocatable object file.
837837 lea_symbol: u32,
838838 };
839
840 fn fail(
841 gpa: Allocator,
842 src_loc: Zcu.LazySrcLoc,
843 comptime format: []const u8,
844 args: anytype,
845 ) Allocator.Error!GenResult {
846 const msg = try ErrorMsg.create(gpa, src_loc, format, args);
847 return .{ .fail = msg };
848 }
849839};
850840
851841fn genNavRef(
......@@ -935,7 +925,8 @@ fn genNavRef(
935925 const atom = p9.getAtom(atom_index);
936926 return .{ .mcv = .{ .memory = atom.getOffsetTableAddress(p9) } };
937927 } else {
938 return GenResult.fail(gpa, src_loc, "TODO genNavRef for target {}", .{target});
928 const msg = try ErrorMsg.create(gpa, src_loc, "TODO genNavRef for target {}", .{target});
929 return .{ .fail = msg };
939930 }
940931}
941932
src/codegen/llvm/BitcodeReader.zig+9-9
......@@ -33,9 +33,9 @@ pub const Block = struct {
3333 .abbrevs = .{ .abbrevs = .{} },
3434 };
3535
36 const set_bid: u32 = 1;
37 const block_name: u32 = 2;
38 const set_record_name: u32 = 3;
36 const set_bid_id: u32 = 1;
37 const block_name_id: u32 = 2;
38 const set_record_name_id: u32 = 3;
3939
4040 fn deinit(info: *Info, allocator: std.mem.Allocator) void {
4141 allocator.free(info.block_name);
......@@ -61,7 +61,7 @@ pub const Record = struct {
6161 assert(record.id == Abbrev.Builtin.define_abbrev.toRecordId());
6262 var i: usize = 0;
6363 while (i < record.operands.len) switch (record.operands[i]) {
64 Abbrev.Operand.literal => {
64 Abbrev.Operand.literal_id => {
6565 try operands.append(.{ .literal = record.operands[i + 1] });
6666 i += 2;
6767 },
......@@ -211,7 +211,7 @@ fn nextRecord(bc: *BitcodeReader) !?Record {
211211 .align_32_bits, .block_len => return error.UnsupportedArrayElement,
212212 .abbrev_op => switch (try bc.readFixed(u1, 1)) {
213213 1 => try operands.appendSlice(&.{
214 Abbrev.Operand.literal,
214 Abbrev.Operand.literal_id,
215215 try bc.readVbr(u64, 8),
216216 }),
217217 0 => {
......@@ -334,9 +334,9 @@ fn parseBlockInfoBlock(bc: *BitcodeReader) !void {
334334 try record.toOwnedAbbrev(bc.allocator),
335335 );
336336 },
337 Block.Info.set_bid => block_id = std.math.cast(u32, record.operands[0]) orelse
337 Block.Info.set_bid_id => block_id = std.math.cast(u32, record.operands[0]) orelse
338338 return error.Overflow,
339 Block.Info.block_name => if (bc.keep_names) {
339 Block.Info.block_name_id => if (bc.keep_names) {
340340 const gop = try bc.block_info.getOrPut(bc.allocator, block_id orelse
341341 return error.UnspecifiedBlockId);
342342 if (!gop.found_existing) gop.value_ptr.* = Block.Info.default;
......@@ -346,7 +346,7 @@ fn parseBlockInfoBlock(bc: *BitcodeReader) !void {
346346 byte.* = std.math.cast(u8, operand) orelse return error.InvalidName;
347347 gop.value_ptr.block_name = name;
348348 },
349 Block.Info.set_record_name => if (bc.keep_names) {
349 Block.Info.set_record_name_id => if (bc.keep_names) {
350350 const gop = try bc.block_info.getOrPut(bc.allocator, block_id orelse
351351 return error.UnspecifiedBlockId);
352352 if (!gop.found_existing) gop.value_ptr.* = Block.Info.default;
......@@ -467,7 +467,7 @@ const Abbrev = struct {
467467 block_len,
468468 abbrev_op,
469469
470 const literal = std.math.maxInt(u64);
470 const literal_id = std.math.maxInt(u64);
471471 const Encoding = enum(u3) {
472472 fixed = 1,
473473 vbr = 2,
src/link/Elf.zig+84-3
......@@ -3467,7 +3467,7 @@ fn updateSectionSizes(self: *Elf) !void {
34673467 if (atom_list.items.len == 0) continue;
34683468
34693469 // Create jump/branch range extenders if needed.
3470 try thunks.createThunks(shdr, @intCast(shndx), self);
3470 try self.createThunks(shdr, @intCast(shndx));
34713471 }
34723472 }
34733473
......@@ -5576,6 +5576,88 @@ fn defaultEntrySymbolName(cpu_arch: std.Target.Cpu.Arch) []const u8 {
55765576 };
55775577}
55785578
5579fn createThunks(elf_file: *Elf, shdr: *elf.Elf64_Shdr, shndx: u32) !void {
5580 const gpa = elf_file.base.comp.gpa;
5581 const cpu_arch = elf_file.getTarget().cpu.arch;
5582 // A branch will need an extender if its target is larger than
5583 // `2^(jump_bits - 1) - margin` where margin is some arbitrary number.
5584 const max_distance = switch (cpu_arch) {
5585 .aarch64 => 0x500_000,
5586 .x86_64, .riscv64 => unreachable,
5587 else => @panic("unhandled arch"),
5588 };
5589 const atoms = elf_file.sections.items(.atom_list)[shndx].items;
5590 assert(atoms.len > 0);
5591
5592 for (atoms) |ref| {
5593 elf_file.atom(ref).?.value = -1;
5594 }
5595
5596 var i: usize = 0;
5597 while (i < atoms.len) {
5598 const start = i;
5599 const start_atom = elf_file.atom(atoms[start]).?;
5600 assert(start_atom.alive);
5601 start_atom.value = try advanceSection(shdr, start_atom.size, start_atom.alignment);
5602 i += 1;
5603
5604 while (i < atoms.len) : (i += 1) {
5605 const atom_ptr = elf_file.atom(atoms[i]).?;
5606 assert(atom_ptr.alive);
5607 if (@as(i64, @intCast(atom_ptr.alignment.forward(shdr.sh_size))) - start_atom.value >= max_distance)
5608 break;
5609 atom_ptr.value = try advanceSection(shdr, atom_ptr.size, atom_ptr.alignment);
5610 }
5611
5612 // Insert a thunk at the group end
5613 const thunk_index = try elf_file.addThunk();
5614 const thunk_ptr = elf_file.thunk(thunk_index);
5615 thunk_ptr.output_section_index = shndx;
5616
5617 // Scan relocs in the group and create trampolines for any unreachable callsite
5618 for (atoms[start..i]) |ref| {
5619 const atom_ptr = elf_file.atom(ref).?;
5620 const file_ptr = atom_ptr.file(elf_file).?;
5621 log.debug("atom({}) {s}", .{ ref, atom_ptr.name(elf_file) });
5622 for (atom_ptr.relocs(elf_file)) |rel| {
5623 const is_reachable = switch (cpu_arch) {
5624 .aarch64 => r: {
5625 const r_type: elf.R_AARCH64 = @enumFromInt(rel.r_type());
5626 if (r_type != .CALL26 and r_type != .JUMP26) break :r true;
5627 const target_ref = file_ptr.resolveSymbol(rel.r_sym(), elf_file);
5628 const target = elf_file.symbol(target_ref).?;
5629 if (target.flags.has_plt) break :r false;
5630 if (atom_ptr.output_section_index != target.output_section_index) break :r false;
5631 const target_atom = target.atom(elf_file).?;
5632 if (target_atom.value == -1) break :r false;
5633 const saddr = atom_ptr.address(elf_file) + @as(i64, @intCast(rel.r_offset));
5634 const taddr = target.address(.{}, elf_file);
5635 _ = math.cast(i28, taddr + rel.r_addend - saddr) orelse break :r false;
5636 break :r true;
5637 },
5638 .x86_64, .riscv64 => unreachable,
5639 else => @panic("unsupported arch"),
5640 };
5641 if (is_reachable) continue;
5642 const target = file_ptr.resolveSymbol(rel.r_sym(), elf_file);
5643 try thunk_ptr.symbols.put(gpa, target, {});
5644 }
5645 atom_ptr.addExtra(.{ .thunk = thunk_index }, elf_file);
5646 }
5647
5648 thunk_ptr.value = try advanceSection(shdr, thunk_ptr.size(elf_file), Atom.Alignment.fromNonzeroByteUnits(2));
5649
5650 log.debug("thunk({d}) : {}", .{ thunk_index, thunk_ptr.fmt(elf_file) });
5651 }
5652}
5653fn advanceSection(shdr: *elf.Elf64_Shdr, adv_size: u64, alignment: Atom.Alignment) !i64 {
5654 const offset = alignment.forward(shdr.sh_size);
5655 const padding = offset - shdr.sh_size;
5656 shdr.sh_size += padding + adv_size;
5657 shdr.sh_addralign = @max(shdr.sh_addralign, alignment.toByteUnits() orelse 1);
5658 return @intCast(offset);
5659}
5660
55795661const std = @import("std");
55805662const build_options = @import("build_options");
55815663const builtin = @import("builtin");
......@@ -5598,7 +5680,6 @@ const musl = @import("../musl.zig");
55985680const relocatable = @import("Elf/relocatable.zig");
55995681const relocation = @import("Elf/relocation.zig");
56005682const target_util = @import("../target.zig");
5601const thunks = @import("Elf/thunks.zig");
56025683const trace = @import("../tracy.zig").trace;
56035684const synthetic_sections = @import("Elf/synthetic_sections.zig");
56045685
......@@ -5636,7 +5717,7 @@ const PltGotSection = synthetic_sections.PltGotSection;
56365717const SharedObject = @import("Elf/SharedObject.zig");
56375718const Symbol = @import("Elf/Symbol.zig");
56385719const StringTable = @import("StringTable.zig");
5639const Thunk = thunks.Thunk;
5720const Thunk = @import("Elf/Thunk.zig");
56405721const Value = @import("../Value.zig");
56415722const VerneedSection = synthetic_sections.VerneedSection;
56425723const ZigObject = @import("Elf/ZigObject.zig");
src/link/Elf/Atom.zig+1-1
......@@ -2251,6 +2251,6 @@ const Fde = eh_frame.Fde;
22512251const File = @import("file.zig").File;
22522252const Object = @import("Object.zig");
22532253const Symbol = @import("Symbol.zig");
2254const Thunk = @import("thunks.zig").Thunk;
2254const Thunk = @import("Thunk.zig");
22552255const ZigObject = @import("ZigObject.zig");
22562256const dev = @import("../../dev.zig");
src/link/Elf/Thunk.zig created+144
......@@ -0,0 +1,144 @@
1value: i64 = 0,
2output_section_index: u32 = 0,
3symbols: std.AutoArrayHashMapUnmanaged(Elf.Ref, void) = .{},
4output_symtab_ctx: Elf.SymtabCtx = .{},
5
6pub fn deinit(thunk: *Thunk, allocator: Allocator) void {
7 thunk.symbols.deinit(allocator);
8}
9
10pub fn size(thunk: Thunk, elf_file: *Elf) usize {
11 const cpu_arch = elf_file.getTarget().cpu.arch;
12 return thunk.symbols.keys().len * trampolineSize(cpu_arch);
13}
14
15pub fn address(thunk: Thunk, elf_file: *Elf) i64 {
16 const shdr = elf_file.sections.items(.shdr)[thunk.output_section_index];
17 return @as(i64, @intCast(shdr.sh_addr)) + thunk.value;
18}
19
20pub fn targetAddress(thunk: Thunk, ref: Elf.Ref, elf_file: *Elf) i64 {
21 const cpu_arch = elf_file.getTarget().cpu.arch;
22 return thunk.address(elf_file) + @as(i64, @intCast(thunk.symbols.getIndex(ref).? * trampolineSize(cpu_arch)));
23}
24
25pub fn write(thunk: Thunk, elf_file: *Elf, writer: anytype) !void {
26 switch (elf_file.getTarget().cpu.arch) {
27 .aarch64 => try aarch64.write(thunk, elf_file, writer),
28 .x86_64, .riscv64 => unreachable,
29 else => @panic("unhandled arch"),
30 }
31}
32
33pub fn calcSymtabSize(thunk: *Thunk, elf_file: *Elf) void {
34 thunk.output_symtab_ctx.nlocals = @as(u32, @intCast(thunk.symbols.keys().len));
35 for (thunk.symbols.keys()) |ref| {
36 const sym = elf_file.symbol(ref).?;
37 thunk.output_symtab_ctx.strsize += @as(u32, @intCast(sym.name(elf_file).len + "$thunk".len + 1));
38 }
39}
40
41pub fn writeSymtab(thunk: Thunk, elf_file: *Elf) void {
42 const cpu_arch = elf_file.getTarget().cpu.arch;
43 for (thunk.symbols.keys(), thunk.output_symtab_ctx.ilocal..) |ref, ilocal| {
44 const sym = elf_file.symbol(ref).?;
45 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
46 elf_file.strtab.appendSliceAssumeCapacity(sym.name(elf_file));
47 elf_file.strtab.appendSliceAssumeCapacity("$thunk");
48 elf_file.strtab.appendAssumeCapacity(0);
49 elf_file.symtab.items[ilocal] = .{
50 .st_name = st_name,
51 .st_info = elf.STT_FUNC,
52 .st_other = 0,
53 .st_shndx = @intCast(thunk.output_section_index),
54 .st_value = @intCast(thunk.targetAddress(ref, elf_file)),
55 .st_size = trampolineSize(cpu_arch),
56 };
57 }
58}
59
60fn trampolineSize(cpu_arch: std.Target.Cpu.Arch) usize {
61 return switch (cpu_arch) {
62 .aarch64 => aarch64.trampoline_size,
63 .x86_64, .riscv64 => unreachable,
64 else => @panic("unhandled arch"),
65 };
66}
67
68pub fn format(
69 thunk: Thunk,
70 comptime unused_fmt_string: []const u8,
71 options: std.fmt.FormatOptions,
72 writer: anytype,
73) !void {
74 _ = thunk;
75 _ = unused_fmt_string;
76 _ = options;
77 _ = writer;
78 @compileError("do not format Thunk directly");
79}
80
81pub fn fmt(thunk: Thunk, elf_file: *Elf) std.fmt.Formatter(format2) {
82 return .{ .data = .{
83 .thunk = thunk,
84 .elf_file = elf_file,
85 } };
86}
87
88const FormatContext = struct {
89 thunk: Thunk,
90 elf_file: *Elf,
91};
92
93fn format2(
94 ctx: FormatContext,
95 comptime unused_fmt_string: []const u8,
96 options: std.fmt.FormatOptions,
97 writer: anytype,
98) !void {
99 _ = options;
100 _ = unused_fmt_string;
101 const thunk = ctx.thunk;
102 const elf_file = ctx.elf_file;
103 try writer.print("@{x} : size({x})\n", .{ thunk.value, thunk.size(elf_file) });
104 for (thunk.symbols.keys()) |ref| {
105 const sym = elf_file.symbol(ref).?;
106 try writer.print(" {} : {s} : @{x}\n", .{ ref, sym.name(elf_file), sym.value });
107 }
108}
109
110pub const Index = u32;
111
112const aarch64 = struct {
113 fn write(thunk: Thunk, elf_file: *Elf, writer: anytype) !void {
114 for (thunk.symbols.keys(), 0..) |ref, i| {
115 const sym = elf_file.symbol(ref).?;
116 const saddr = thunk.address(elf_file) + @as(i64, @intCast(i * trampoline_size));
117 const taddr = sym.address(.{}, elf_file);
118 const pages = try util.calcNumberOfPages(saddr, taddr);
119 try writer.writeInt(u32, Instruction.adrp(.x16, pages).toU32(), .little);
120 const off: u12 = @truncate(@as(u64, @bitCast(taddr)));
121 try writer.writeInt(u32, Instruction.add(.x16, .x16, off, false).toU32(), .little);
122 try writer.writeInt(u32, Instruction.br(.x16).toU32(), .little);
123 }
124 }
125
126 const trampoline_size = 3 * @sizeOf(u32);
127
128 const util = @import("../aarch64.zig");
129 const Instruction = util.Instruction;
130};
131
132const assert = std.debug.assert;
133const elf = std.elf;
134const log = std.log.scoped(.link);
135const math = std.math;
136const mem = std.mem;
137const std = @import("std");
138
139const Allocator = mem.Allocator;
140const Atom = @import("Atom.zig");
141const Elf = @import("../Elf.zig");
142const Symbol = @import("Symbol.zig");
143
144const Thunk = @This();
src/link/Elf/thunks.zig deleted-234
......@@ -1,234 +0,0 @@
1pub fn createThunks(shdr: *elf.Elf64_Shdr, shndx: u32, elf_file: *Elf) !void {
2 const gpa = elf_file.base.comp.gpa;
3 const cpu_arch = elf_file.getTarget().cpu.arch;
4 const max_distance = maxAllowedDistance(cpu_arch);
5 const atoms = elf_file.sections.items(.atom_list)[shndx].items;
6 assert(atoms.len > 0);
7
8 for (atoms) |ref| {
9 elf_file.atom(ref).?.value = -1;
10 }
11
12 var i: usize = 0;
13 while (i < atoms.len) {
14 const start = i;
15 const start_atom = elf_file.atom(atoms[start]).?;
16 assert(start_atom.alive);
17 start_atom.value = try advance(shdr, start_atom.size, start_atom.alignment);
18 i += 1;
19
20 while (i < atoms.len) : (i += 1) {
21 const atom = elf_file.atom(atoms[i]).?;
22 assert(atom.alive);
23 if (@as(i64, @intCast(atom.alignment.forward(shdr.sh_size))) - start_atom.value >= max_distance)
24 break;
25 atom.value = try advance(shdr, atom.size, atom.alignment);
26 }
27
28 // Insert a thunk at the group end
29 const thunk_index = try elf_file.addThunk();
30 const thunk = elf_file.thunk(thunk_index);
31 thunk.output_section_index = shndx;
32
33 // Scan relocs in the group and create trampolines for any unreachable callsite
34 for (atoms[start..i]) |ref| {
35 const atom = elf_file.atom(ref).?;
36 const file = atom.file(elf_file).?;
37 log.debug("atom({}) {s}", .{ ref, atom.name(elf_file) });
38 for (atom.relocs(elf_file)) |rel| {
39 const is_reachable = switch (cpu_arch) {
40 .aarch64 => aarch64.isReachable(atom, rel, elf_file),
41 .x86_64, .riscv64 => unreachable,
42 else => @panic("unsupported arch"),
43 };
44 if (is_reachable) continue;
45 const target = file.resolveSymbol(rel.r_sym(), elf_file);
46 try thunk.symbols.put(gpa, target, {});
47 }
48 atom.addExtra(.{ .thunk = thunk_index }, elf_file);
49 }
50
51 thunk.value = try advance(shdr, thunk.size(elf_file), Atom.Alignment.fromNonzeroByteUnits(2));
52
53 log.debug("thunk({d}) : {}", .{ thunk_index, thunk.fmt(elf_file) });
54 }
55}
56
57fn advance(shdr: *elf.Elf64_Shdr, size: u64, alignment: Atom.Alignment) !i64 {
58 const offset = alignment.forward(shdr.sh_size);
59 const padding = offset - shdr.sh_size;
60 shdr.sh_size += padding + size;
61 shdr.sh_addralign = @max(shdr.sh_addralign, alignment.toByteUnits() orelse 1);
62 return @intCast(offset);
63}
64
65/// A branch will need an extender if its target is larger than
66/// `2^(jump_bits - 1) - margin` where margin is some arbitrary number.
67fn maxAllowedDistance(cpu_arch: std.Target.Cpu.Arch) u32 {
68 return switch (cpu_arch) {
69 .aarch64 => 0x500_000,
70 .x86_64, .riscv64 => unreachable,
71 else => @panic("unhandled arch"),
72 };
73}
74
75pub const Thunk = struct {
76 value: i64 = 0,
77 output_section_index: u32 = 0,
78 symbols: std.AutoArrayHashMapUnmanaged(Elf.Ref, void) = .{},
79 output_symtab_ctx: Elf.SymtabCtx = .{},
80
81 pub fn deinit(thunk: *Thunk, allocator: Allocator) void {
82 thunk.symbols.deinit(allocator);
83 }
84
85 pub fn size(thunk: Thunk, elf_file: *Elf) usize {
86 const cpu_arch = elf_file.getTarget().cpu.arch;
87 return thunk.symbols.keys().len * trampolineSize(cpu_arch);
88 }
89
90 pub fn address(thunk: Thunk, elf_file: *Elf) i64 {
91 const shdr = elf_file.sections.items(.shdr)[thunk.output_section_index];
92 return @as(i64, @intCast(shdr.sh_addr)) + thunk.value;
93 }
94
95 pub fn targetAddress(thunk: Thunk, ref: Elf.Ref, elf_file: *Elf) i64 {
96 const cpu_arch = elf_file.getTarget().cpu.arch;
97 return thunk.address(elf_file) + @as(i64, @intCast(thunk.symbols.getIndex(ref).? * trampolineSize(cpu_arch)));
98 }
99
100 pub fn write(thunk: Thunk, elf_file: *Elf, writer: anytype) !void {
101 switch (elf_file.getTarget().cpu.arch) {
102 .aarch64 => try aarch64.write(thunk, elf_file, writer),
103 .x86_64, .riscv64 => unreachable,
104 else => @panic("unhandled arch"),
105 }
106 }
107
108 pub fn calcSymtabSize(thunk: *Thunk, elf_file: *Elf) void {
109 thunk.output_symtab_ctx.nlocals = @as(u32, @intCast(thunk.symbols.keys().len));
110 for (thunk.symbols.keys()) |ref| {
111 const sym = elf_file.symbol(ref).?;
112 thunk.output_symtab_ctx.strsize += @as(u32, @intCast(sym.name(elf_file).len + "$thunk".len + 1));
113 }
114 }
115
116 pub fn writeSymtab(thunk: Thunk, elf_file: *Elf) void {
117 const cpu_arch = elf_file.getTarget().cpu.arch;
118 for (thunk.symbols.keys(), thunk.output_symtab_ctx.ilocal..) |ref, ilocal| {
119 const sym = elf_file.symbol(ref).?;
120 const st_name = @as(u32, @intCast(elf_file.strtab.items.len));
121 elf_file.strtab.appendSliceAssumeCapacity(sym.name(elf_file));
122 elf_file.strtab.appendSliceAssumeCapacity("$thunk");
123 elf_file.strtab.appendAssumeCapacity(0);
124 elf_file.symtab.items[ilocal] = .{
125 .st_name = st_name,
126 .st_info = elf.STT_FUNC,
127 .st_other = 0,
128 .st_shndx = @intCast(thunk.output_section_index),
129 .st_value = @intCast(thunk.targetAddress(ref, elf_file)),
130 .st_size = trampolineSize(cpu_arch),
131 };
132 }
133 }
134
135 fn trampolineSize(cpu_arch: std.Target.Cpu.Arch) usize {
136 return switch (cpu_arch) {
137 .aarch64 => aarch64.trampoline_size,
138 .x86_64, .riscv64 => unreachable,
139 else => @panic("unhandled arch"),
140 };
141 }
142
143 pub fn format(
144 thunk: Thunk,
145 comptime unused_fmt_string: []const u8,
146 options: std.fmt.FormatOptions,
147 writer: anytype,
148 ) !void {
149 _ = thunk;
150 _ = unused_fmt_string;
151 _ = options;
152 _ = writer;
153 @compileError("do not format Thunk directly");
154 }
155
156 pub fn fmt(thunk: Thunk, elf_file: *Elf) std.fmt.Formatter(format2) {
157 return .{ .data = .{
158 .thunk = thunk,
159 .elf_file = elf_file,
160 } };
161 }
162
163 const FormatContext = struct {
164 thunk: Thunk,
165 elf_file: *Elf,
166 };
167
168 fn format2(
169 ctx: FormatContext,
170 comptime unused_fmt_string: []const u8,
171 options: std.fmt.FormatOptions,
172 writer: anytype,
173 ) !void {
174 _ = options;
175 _ = unused_fmt_string;
176 const thunk = ctx.thunk;
177 const elf_file = ctx.elf_file;
178 try writer.print("@{x} : size({x})\n", .{ thunk.value, thunk.size(elf_file) });
179 for (thunk.symbols.keys()) |ref| {
180 const sym = elf_file.symbol(ref).?;
181 try writer.print(" {} : {s} : @{x}\n", .{ ref, sym.name(elf_file), sym.value });
182 }
183 }
184
185 pub const Index = u32;
186};
187
188const aarch64 = struct {
189 fn isReachable(atom: *const Atom, rel: elf.Elf64_Rela, elf_file: *Elf) bool {
190 const r_type: elf.R_AARCH64 = @enumFromInt(rel.r_type());
191 if (r_type != .CALL26 and r_type != .JUMP26) return true;
192 const file = atom.file(elf_file).?;
193 const target_ref = file.resolveSymbol(rel.r_sym(), elf_file);
194 const target = elf_file.symbol(target_ref).?;
195 if (target.flags.has_plt) return false;
196 if (atom.output_section_index != target.output_section_index) return false;
197 const target_atom = target.atom(elf_file).?;
198 if (target_atom.value == -1) return false;
199 const saddr = atom.address(elf_file) + @as(i64, @intCast(rel.r_offset));
200 const taddr = target.address(.{}, elf_file);
201 _ = math.cast(i28, taddr + rel.r_addend - saddr) orelse return false;
202 return true;
203 }
204
205 fn write(thunk: Thunk, elf_file: *Elf, writer: anytype) !void {
206 for (thunk.symbols.keys(), 0..) |ref, i| {
207 const sym = elf_file.symbol(ref).?;
208 const saddr = thunk.address(elf_file) + @as(i64, @intCast(i * trampoline_size));
209 const taddr = sym.address(.{}, elf_file);
210 const pages = try util.calcNumberOfPages(saddr, taddr);
211 try writer.writeInt(u32, Instruction.adrp(.x16, pages).toU32(), .little);
212 const off: u12 = @truncate(@as(u64, @bitCast(taddr)));
213 try writer.writeInt(u32, Instruction.add(.x16, .x16, off, false).toU32(), .little);
214 try writer.writeInt(u32, Instruction.br(.x16).toU32(), .little);
215 }
216 }
217
218 const trampoline_size = 3 * @sizeOf(u32);
219
220 const util = @import("../aarch64.zig");
221 const Instruction = util.Instruction;
222};
223
224const assert = std.debug.assert;
225const elf = std.elf;
226const log = std.log.scoped(.link);
227const math = std.math;
228const mem = std.mem;
229const std = @import("std");
230
231const Allocator = mem.Allocator;
232const Atom = @import("Atom.zig");
233const Elf = @import("../Elf.zig");
234const Symbol = @import("Symbol.zig");
src/link/MachO.zig+111-19
......@@ -64,10 +64,10 @@ stubs_helper: StubsHelperSection = .{},
6464objc_stubs: ObjcStubsSection = .{},
6565la_symbol_ptr: LaSymbolPtrSection = .{},
6666tlv_ptr: TlvPtrSection = .{},
67rebase: Rebase = .{},
68bind: Bind = .{},
69weak_bind: WeakBind = .{},
70lazy_bind: LazyBind = .{},
67rebase_section: Rebase = .{},
68bind_section: Bind = .{},
69weak_bind_section: WeakBind = .{},
70lazy_bind_section: LazyBind = .{},
7171export_trie: ExportTrie = .{},
7272unwind_info: UnwindInfo = .{},
7373data_in_code: DataInCode = .{},
......@@ -324,10 +324,10 @@ pub fn deinit(self: *MachO) void {
324324 self.stubs.deinit(gpa);
325325 self.objc_stubs.deinit(gpa);
326326 self.tlv_ptr.deinit(gpa);
327 self.rebase.deinit(gpa);
328 self.bind.deinit(gpa);
329 self.weak_bind.deinit(gpa);
330 self.lazy_bind.deinit(gpa);
327 self.rebase_section.deinit(gpa);
328 self.bind_section.deinit(gpa);
329 self.weak_bind_section.deinit(gpa);
330 self.lazy_bind_section.deinit(gpa);
331331 self.export_trie.deinit(gpa);
332332 self.unwind_info.deinit(gpa);
333333 self.data_in_code.deinit(gpa);
......@@ -2005,7 +2005,7 @@ fn calcSectionSizeWorker(self: *MachO, sect_id: u8) void {
20052005fn createThunksWorker(self: *MachO, sect_id: u8) void {
20062006 const tracy = trace(@src());
20072007 defer tracy.end();
2008 thunks.createThunks(sect_id, self) catch |err| {
2008 self.createThunks(sect_id) catch |err| {
20092009 const header = self.sections.items(.header)[sect_id];
20102010 self.reportUnexpectedError("failed to create thunks and calculate size of section '{s},{s}': {s}", .{
20112011 header.segName(),
......@@ -2562,7 +2562,7 @@ fn updateLazyBindSizeWorker(self: *MachO) void {
25622562 defer tracy.end();
25632563 const doWork = struct {
25642564 fn doWork(macho_file: *MachO) !void {
2565 try macho_file.lazy_bind.updateSize(macho_file);
2565 try macho_file.lazy_bind_section.updateSize(macho_file);
25662566 const sect_id = macho_file.stubs_helper_sect_index.?;
25672567 const out = &macho_file.sections.items(.out)[sect_id];
25682568 var stream = std.io.fixedBufferStream(out.items);
......@@ -2585,9 +2585,9 @@ pub fn updateLinkeditSizeWorker(self: *MachO, tag: enum {
25852585 data_in_code,
25862586}) void {
25872587 const res = switch (tag) {
2588 .rebase => self.rebase.updateSize(self),
2589 .bind => self.bind.updateSize(self),
2590 .weak_bind => self.weak_bind.updateSize(self),
2588 .rebase => self.rebase_section.updateSize(self),
2589 .bind => self.bind_section.updateSize(self),
2590 .weak_bind => self.weak_bind_section.updateSize(self),
25912591 .export_trie => self.export_trie.updateSize(self),
25922592 .data_in_code => self.data_in_code.updateSize(self),
25932593 };
......@@ -2640,13 +2640,13 @@ fn writeDyldInfo(self: *MachO) !void {
26402640 var stream = std.io.fixedBufferStream(buffer);
26412641 const writer = stream.writer();
26422642
2643 try self.rebase.write(writer);
2643 try self.rebase_section.write(writer);
26442644 try stream.seekTo(cmd.bind_off - base_off);
2645 try self.bind.write(writer);
2645 try self.bind_section.write(writer);
26462646 try stream.seekTo(cmd.weak_bind_off - base_off);
2647 try self.weak_bind.write(writer);
2647 try self.weak_bind_section.write(writer);
26482648 try stream.seekTo(cmd.lazy_bind_off - base_off);
2649 try self.lazy_bind.write(writer);
2649 try self.lazy_bind_section.write(writer);
26502650 try stream.seekTo(cmd.export_off - base_off);
26512651 try self.export_trie.write(writer);
26522652 try self.base.file.?.pwriteAll(buffer, cmd.rebase_off);
......@@ -4602,7 +4602,6 @@ const load_commands = @import("MachO/load_commands.zig");
46024602const relocatable = @import("MachO/relocatable.zig");
46034603const tapi = @import("tapi.zig");
46044604const target_util = @import("../target.zig");
4605const thunks = @import("MachO/thunks.zig");
46064605const trace = @import("../tracy.zig").trace;
46074606const synthetic = @import("MachO/synthetic.zig");
46084607
......@@ -4641,7 +4640,7 @@ const StringTable = @import("StringTable.zig");
46414640const StubsSection = synthetic.StubsSection;
46424641const StubsHelperSection = synthetic.StubsHelperSection;
46434642const Symbol = @import("MachO/Symbol.zig");
4644const Thunk = thunks.Thunk;
4643const Thunk = @import("MachO/Thunk.zig");
46454644const TlvPtrSection = synthetic.TlvPtrSection;
46464645const Value = @import("../Value.zig");
46474646const UnwindInfo = @import("MachO/UnwindInfo.zig");
......@@ -5292,3 +5291,96 @@ pub const KernE = enum(u32) {
52925291 NOT_FOUND = 56,
52935292 _,
52945293};
5294
5295fn createThunks(macho_file: *MachO, sect_id: u8) !void {
5296 const tracy = trace(@src());
5297 defer tracy.end();
5298
5299 const gpa = macho_file.base.comp.gpa;
5300 const slice = macho_file.sections.slice();
5301 const header = &slice.items(.header)[sect_id];
5302 const thnks = &slice.items(.thunks)[sect_id];
5303 const atoms = slice.items(.atoms)[sect_id].items;
5304 assert(atoms.len > 0);
5305
5306 for (atoms) |ref| {
5307 ref.getAtom(macho_file).?.value = @bitCast(@as(i64, -1));
5308 }
5309
5310 var i: usize = 0;
5311 while (i < atoms.len) {
5312 const start = i;
5313 const start_atom = atoms[start].getAtom(macho_file).?;
5314 assert(start_atom.isAlive());
5315 start_atom.value = advanceSection(header, start_atom.size, start_atom.alignment);
5316 i += 1;
5317
5318 while (i < atoms.len and
5319 header.size - start_atom.value < max_allowed_distance) : (i += 1)
5320 {
5321 const atom = atoms[i].getAtom(macho_file).?;
5322 assert(atom.isAlive());
5323 atom.value = advanceSection(header, atom.size, atom.alignment);
5324 }
5325
5326 // Insert a thunk at the group end
5327 const thunk_index = try macho_file.addThunk();
5328 const thunk = macho_file.getThunk(thunk_index);
5329 thunk.out_n_sect = sect_id;
5330 try thnks.append(gpa, thunk_index);
5331
5332 // Scan relocs in the group and create trampolines for any unreachable callsite
5333 try scanThunkRelocs(thunk_index, gpa, atoms[start..i], macho_file);
5334 thunk.value = advanceSection(header, thunk.size(), .@"4");
5335
5336 log.debug("thunk({d}) : {}", .{ thunk_index, thunk.fmt(macho_file) });
5337 }
5338}
5339
5340fn advanceSection(sect: *macho.section_64, adv_size: u64, alignment: Atom.Alignment) u64 {
5341 const offset = alignment.forward(sect.size);
5342 const padding = offset - sect.size;
5343 sect.size += padding + adv_size;
5344 sect.@"align" = @max(sect.@"align", alignment.toLog2Units());
5345 return offset;
5346}
5347
5348fn scanThunkRelocs(thunk_index: Thunk.Index, gpa: Allocator, atoms: []const MachO.Ref, macho_file: *MachO) !void {
5349 const tracy = trace(@src());
5350 defer tracy.end();
5351
5352 const thunk = macho_file.getThunk(thunk_index);
5353
5354 for (atoms) |ref| {
5355 const atom = ref.getAtom(macho_file).?;
5356 log.debug("atom({d}) {s}", .{ atom.atom_index, atom.getName(macho_file) });
5357 for (atom.getRelocs(macho_file)) |rel| {
5358 if (rel.type != .branch) continue;
5359 if (isReachable(atom, rel, macho_file)) continue;
5360 try thunk.symbols.put(gpa, rel.getTargetSymbolRef(atom.*, macho_file), {});
5361 }
5362 atom.addExtra(.{ .thunk = thunk_index }, macho_file);
5363 }
5364}
5365
5366fn isReachable(atom: *const Atom, rel: Relocation, macho_file: *MachO) bool {
5367 const target = rel.getTargetSymbol(atom.*, macho_file);
5368 if (target.getSectionFlags().stubs or target.getSectionFlags().objc_stubs) return false;
5369 if (atom.out_n_sect != target.getOutputSectionIndex(macho_file)) return false;
5370 const target_atom = target.getAtom(macho_file).?;
5371 if (target_atom.value == @as(u64, @bitCast(@as(i64, -1)))) return false;
5372 const saddr = @as(i64, @intCast(atom.getAddress(macho_file))) + @as(i64, @intCast(rel.offset - atom.off));
5373 const taddr: i64 = @intCast(rel.getTargetAddress(atom.*, macho_file));
5374 _ = math.cast(i28, taddr + rel.addend - saddr) orelse return false;
5375 return true;
5376}
5377
5378/// Branch instruction has 26 bits immediate but is 4 byte aligned.
5379const jump_bits = @bitSizeOf(i28);
5380const max_distance = (1 << (jump_bits - 1));
5381
5382/// A branch will need an extender if its target is larger than
5383/// `2^(jump_bits - 1) - margin` where margin is some arbitrary number.
5384/// mold uses 5MiB margin, while ld64 uses 4MiB margin. We will follow mold
5385/// and assume margin to be 5MiB.
5386const max_allowed_distance = max_distance - 0x500_000;
src/link/MachO/Atom.zig+1-1
......@@ -1220,6 +1220,6 @@ const MachO = @import("../MachO.zig");
12201220const Object = @import("Object.zig");
12211221const Relocation = @import("Relocation.zig");
12221222const Symbol = @import("Symbol.zig");
1223const Thunk = @import("thunks.zig").Thunk;
1223const Thunk = @import("Thunk.zig");
12241224const UnwindInfo = @import("UnwindInfo.zig");
12251225const dev = @import("../../dev.zig");
src/link/MachO/Thunk.zig created+125
......@@ -0,0 +1,125 @@
1value: u64 = 0,
2out_n_sect: u8 = 0,
3symbols: std.AutoArrayHashMapUnmanaged(MachO.Ref, void) = .{},
4output_symtab_ctx: MachO.SymtabCtx = .{},
5
6pub fn deinit(thunk: *Thunk, allocator: Allocator) void {
7 thunk.symbols.deinit(allocator);
8}
9
10pub fn size(thunk: Thunk) usize {
11 return thunk.symbols.keys().len * trampoline_size;
12}
13
14pub fn getAddress(thunk: Thunk, macho_file: *MachO) u64 {
15 const header = macho_file.sections.items(.header)[thunk.out_n_sect];
16 return header.addr + thunk.value;
17}
18
19pub fn getTargetAddress(thunk: Thunk, ref: MachO.Ref, macho_file: *MachO) u64 {
20 return thunk.getAddress(macho_file) + thunk.symbols.getIndex(ref).? * trampoline_size;
21}
22
23pub fn write(thunk: Thunk, macho_file: *MachO, writer: anytype) !void {
24 for (thunk.symbols.keys(), 0..) |ref, i| {
25 const sym = ref.getSymbol(macho_file).?;
26 const saddr = thunk.getAddress(macho_file) + i * trampoline_size;
27 const taddr = sym.getAddress(.{}, macho_file);
28 const pages = try aarch64.calcNumberOfPages(@intCast(saddr), @intCast(taddr));
29 try writer.writeInt(u32, aarch64.Instruction.adrp(.x16, pages).toU32(), .little);
30 const off: u12 = @truncate(taddr);
31 try writer.writeInt(u32, aarch64.Instruction.add(.x16, .x16, off, false).toU32(), .little);
32 try writer.writeInt(u32, aarch64.Instruction.br(.x16).toU32(), .little);
33 }
34}
35
36pub fn calcSymtabSize(thunk: *Thunk, macho_file: *MachO) void {
37 thunk.output_symtab_ctx.nlocals = @as(u32, @intCast(thunk.symbols.keys().len));
38 for (thunk.symbols.keys()) |ref| {
39 const sym = ref.getSymbol(macho_file).?;
40 thunk.output_symtab_ctx.strsize += @as(u32, @intCast(sym.getName(macho_file).len + "__thunk".len + 1));
41 }
42}
43
44pub fn writeSymtab(thunk: Thunk, macho_file: *MachO, ctx: anytype) void {
45 var n_strx = thunk.output_symtab_ctx.stroff;
46 for (thunk.symbols.keys(), thunk.output_symtab_ctx.ilocal..) |ref, ilocal| {
47 const sym = ref.getSymbol(macho_file).?;
48 const name = sym.getName(macho_file);
49 const out_sym = &ctx.symtab.items[ilocal];
50 out_sym.n_strx = n_strx;
51 @memcpy(ctx.strtab.items[n_strx..][0..name.len], name);
52 n_strx += @intCast(name.len);
53 @memcpy(ctx.strtab.items[n_strx..][0.."__thunk".len], "__thunk");
54 n_strx += @intCast("__thunk".len);
55 ctx.strtab.items[n_strx] = 0;
56 n_strx += 1;
57 out_sym.n_type = macho.N_SECT;
58 out_sym.n_sect = @intCast(thunk.out_n_sect + 1);
59 out_sym.n_value = @intCast(thunk.getTargetAddress(ref, macho_file));
60 out_sym.n_desc = 0;
61 }
62}
63
64pub fn format(
65 thunk: Thunk,
66 comptime unused_fmt_string: []const u8,
67 options: std.fmt.FormatOptions,
68 writer: anytype,
69) !void {
70 _ = thunk;
71 _ = unused_fmt_string;
72 _ = options;
73 _ = writer;
74 @compileError("do not format Thunk directly");
75}
76
77pub fn fmt(thunk: Thunk, macho_file: *MachO) std.fmt.Formatter(format2) {
78 return .{ .data = .{
79 .thunk = thunk,
80 .macho_file = macho_file,
81 } };
82}
83
84const FormatContext = struct {
85 thunk: Thunk,
86 macho_file: *MachO,
87};
88
89fn format2(
90 ctx: FormatContext,
91 comptime unused_fmt_string: []const u8,
92 options: std.fmt.FormatOptions,
93 writer: anytype,
94) !void {
95 _ = options;
96 _ = unused_fmt_string;
97 const thunk = ctx.thunk;
98 const macho_file = ctx.macho_file;
99 try writer.print("@{x} : size({x})\n", .{ thunk.value, thunk.size() });
100 for (thunk.symbols.keys()) |ref| {
101 const sym = ref.getSymbol(macho_file).?;
102 try writer.print(" {} : {s} : @{x}\n", .{ ref, sym.getName(macho_file), sym.value });
103 }
104}
105
106const trampoline_size = 3 * @sizeOf(u32);
107
108pub const Index = u32;
109
110const aarch64 = @import("../aarch64.zig");
111const assert = std.debug.assert;
112const log = std.log.scoped(.link);
113const macho = std.macho;
114const math = std.math;
115const mem = std.mem;
116const std = @import("std");
117const trace = @import("../../tracy.zig").trace;
118
119const Allocator = mem.Allocator;
120const Atom = @import("Atom.zig");
121const MachO = @import("../MachO.zig");
122const Relocation = @import("Relocation.zig");
123const Symbol = @import("Symbol.zig");
124
125const Thunk = @This();
src/link/MachO/synthetic.zig+1-1
......@@ -204,7 +204,7 @@ pub const StubsHelperSection = struct {
204204 for (macho_file.stubs.symbols.items) |ref| {
205205 const sym = ref.getSymbol(macho_file).?;
206206 if (sym.flags.weak) continue;
207 const offset = macho_file.lazy_bind.offsets.items[idx];
207 const offset = macho_file.lazy_bind_section.offsets.items[idx];
208208 const source: i64 = @intCast(sect.addr + preamble_size + entry_size * idx);
209209 const target: i64 = @intCast(sect.addr);
210210 switch (cpu_arch) {
src/link/MachO/thunks.zig deleted-218
......@@ -1,218 +0,0 @@
1pub fn createThunks(sect_id: u8, macho_file: *MachO) !void {
2 const tracy = trace(@src());
3 defer tracy.end();
4
5 const gpa = macho_file.base.comp.gpa;
6 const slice = macho_file.sections.slice();
7 const header = &slice.items(.header)[sect_id];
8 const thnks = &slice.items(.thunks)[sect_id];
9 const atoms = slice.items(.atoms)[sect_id].items;
10 assert(atoms.len > 0);
11
12 for (atoms) |ref| {
13 ref.getAtom(macho_file).?.value = @bitCast(@as(i64, -1));
14 }
15
16 var i: usize = 0;
17 while (i < atoms.len) {
18 const start = i;
19 const start_atom = atoms[start].getAtom(macho_file).?;
20 assert(start_atom.isAlive());
21 start_atom.value = advance(header, start_atom.size, start_atom.alignment);
22 i += 1;
23
24 while (i < atoms.len and
25 header.size - start_atom.value < max_allowed_distance) : (i += 1)
26 {
27 const atom = atoms[i].getAtom(macho_file).?;
28 assert(atom.isAlive());
29 atom.value = advance(header, atom.size, atom.alignment);
30 }
31
32 // Insert a thunk at the group end
33 const thunk_index = try macho_file.addThunk();
34 const thunk = macho_file.getThunk(thunk_index);
35 thunk.out_n_sect = sect_id;
36 try thnks.append(gpa, thunk_index);
37
38 // Scan relocs in the group and create trampolines for any unreachable callsite
39 try scanRelocs(thunk_index, gpa, atoms[start..i], macho_file);
40 thunk.value = advance(header, thunk.size(), .@"4");
41
42 log.debug("thunk({d}) : {}", .{ thunk_index, thunk.fmt(macho_file) });
43 }
44}
45
46fn advance(sect: *macho.section_64, size: u64, alignment: Atom.Alignment) u64 {
47 const offset = alignment.forward(sect.size);
48 const padding = offset - sect.size;
49 sect.size += padding + size;
50 sect.@"align" = @max(sect.@"align", alignment.toLog2Units());
51 return offset;
52}
53
54fn scanRelocs(thunk_index: Thunk.Index, gpa: Allocator, atoms: []const MachO.Ref, macho_file: *MachO) !void {
55 const tracy = trace(@src());
56 defer tracy.end();
57
58 const thunk = macho_file.getThunk(thunk_index);
59
60 for (atoms) |ref| {
61 const atom = ref.getAtom(macho_file).?;
62 log.debug("atom({d}) {s}", .{ atom.atom_index, atom.getName(macho_file) });
63 for (atom.getRelocs(macho_file)) |rel| {
64 if (rel.type != .branch) continue;
65 if (isReachable(atom, rel, macho_file)) continue;
66 try thunk.symbols.put(gpa, rel.getTargetSymbolRef(atom.*, macho_file), {});
67 }
68 atom.addExtra(.{ .thunk = thunk_index }, macho_file);
69 }
70}
71
72fn isReachable(atom: *const Atom, rel: Relocation, macho_file: *MachO) bool {
73 const target = rel.getTargetSymbol(atom.*, macho_file);
74 if (target.getSectionFlags().stubs or target.getSectionFlags().objc_stubs) return false;
75 if (atom.out_n_sect != target.getOutputSectionIndex(macho_file)) return false;
76 const target_atom = target.getAtom(macho_file).?;
77 if (target_atom.value == @as(u64, @bitCast(@as(i64, -1)))) return false;
78 const saddr = @as(i64, @intCast(atom.getAddress(macho_file))) + @as(i64, @intCast(rel.offset - atom.off));
79 const taddr: i64 = @intCast(rel.getTargetAddress(atom.*, macho_file));
80 _ = math.cast(i28, taddr + rel.addend - saddr) orelse return false;
81 return true;
82}
83
84pub const Thunk = struct {
85 value: u64 = 0,
86 out_n_sect: u8 = 0,
87 symbols: std.AutoArrayHashMapUnmanaged(MachO.Ref, void) = .{},
88 output_symtab_ctx: MachO.SymtabCtx = .{},
89
90 pub fn deinit(thunk: *Thunk, allocator: Allocator) void {
91 thunk.symbols.deinit(allocator);
92 }
93
94 pub fn size(thunk: Thunk) usize {
95 return thunk.symbols.keys().len * trampoline_size;
96 }
97
98 pub fn getAddress(thunk: Thunk, macho_file: *MachO) u64 {
99 const header = macho_file.sections.items(.header)[thunk.out_n_sect];
100 return header.addr + thunk.value;
101 }
102
103 pub fn getTargetAddress(thunk: Thunk, ref: MachO.Ref, macho_file: *MachO) u64 {
104 return thunk.getAddress(macho_file) + thunk.symbols.getIndex(ref).? * trampoline_size;
105 }
106
107 pub fn write(thunk: Thunk, macho_file: *MachO, writer: anytype) !void {
108 for (thunk.symbols.keys(), 0..) |ref, i| {
109 const sym = ref.getSymbol(macho_file).?;
110 const saddr = thunk.getAddress(macho_file) + i * trampoline_size;
111 const taddr = sym.getAddress(.{}, macho_file);
112 const pages = try aarch64.calcNumberOfPages(@intCast(saddr), @intCast(taddr));
113 try writer.writeInt(u32, aarch64.Instruction.adrp(.x16, pages).toU32(), .little);
114 const off: u12 = @truncate(taddr);
115 try writer.writeInt(u32, aarch64.Instruction.add(.x16, .x16, off, false).toU32(), .little);
116 try writer.writeInt(u32, aarch64.Instruction.br(.x16).toU32(), .little);
117 }
118 }
119
120 pub fn calcSymtabSize(thunk: *Thunk, macho_file: *MachO) void {
121 thunk.output_symtab_ctx.nlocals = @as(u32, @intCast(thunk.symbols.keys().len));
122 for (thunk.symbols.keys()) |ref| {
123 const sym = ref.getSymbol(macho_file).?;
124 thunk.output_symtab_ctx.strsize += @as(u32, @intCast(sym.getName(macho_file).len + "__thunk".len + 1));
125 }
126 }
127
128 pub fn writeSymtab(thunk: Thunk, macho_file: *MachO, ctx: anytype) void {
129 var n_strx = thunk.output_symtab_ctx.stroff;
130 for (thunk.symbols.keys(), thunk.output_symtab_ctx.ilocal..) |ref, ilocal| {
131 const sym = ref.getSymbol(macho_file).?;
132 const name = sym.getName(macho_file);
133 const out_sym = &ctx.symtab.items[ilocal];
134 out_sym.n_strx = n_strx;
135 @memcpy(ctx.strtab.items[n_strx..][0..name.len], name);
136 n_strx += @intCast(name.len);
137 @memcpy(ctx.strtab.items[n_strx..][0.."__thunk".len], "__thunk");
138 n_strx += @intCast("__thunk".len);
139 ctx.strtab.items[n_strx] = 0;
140 n_strx += 1;
141 out_sym.n_type = macho.N_SECT;
142 out_sym.n_sect = @intCast(thunk.out_n_sect + 1);
143 out_sym.n_value = @intCast(thunk.getTargetAddress(ref, macho_file));
144 out_sym.n_desc = 0;
145 }
146 }
147
148 pub fn format(
149 thunk: Thunk,
150 comptime unused_fmt_string: []const u8,
151 options: std.fmt.FormatOptions,
152 writer: anytype,
153 ) !void {
154 _ = thunk;
155 _ = unused_fmt_string;
156 _ = options;
157 _ = writer;
158 @compileError("do not format Thunk directly");
159 }
160
161 pub fn fmt(thunk: Thunk, macho_file: *MachO) std.fmt.Formatter(format2) {
162 return .{ .data = .{
163 .thunk = thunk,
164 .macho_file = macho_file,
165 } };
166 }
167
168 const FormatContext = struct {
169 thunk: Thunk,
170 macho_file: *MachO,
171 };
172
173 fn format2(
174 ctx: FormatContext,
175 comptime unused_fmt_string: []const u8,
176 options: std.fmt.FormatOptions,
177 writer: anytype,
178 ) !void {
179 _ = options;
180 _ = unused_fmt_string;
181 const thunk = ctx.thunk;
182 const macho_file = ctx.macho_file;
183 try writer.print("@{x} : size({x})\n", .{ thunk.value, thunk.size() });
184 for (thunk.symbols.keys()) |ref| {
185 const sym = ref.getSymbol(macho_file).?;
186 try writer.print(" {} : {s} : @{x}\n", .{ ref, sym.getName(macho_file), sym.value });
187 }
188 }
189
190 const trampoline_size = 3 * @sizeOf(u32);
191
192 pub const Index = u32;
193};
194
195/// Branch instruction has 26 bits immediate but is 4 byte aligned.
196const jump_bits = @bitSizeOf(i28);
197const max_distance = (1 << (jump_bits - 1));
198
199/// A branch will need an extender if its target is larger than
200/// `2^(jump_bits - 1) - margin` where margin is some arbitrary number.
201/// mold uses 5MiB margin, while ld64 uses 4MiB margin. We will follow mold
202/// and assume margin to be 5MiB.
203const max_allowed_distance = max_distance - 0x500_000;
204
205const aarch64 = @import("../aarch64.zig");
206const assert = std.debug.assert;
207const log = std.log.scoped(.link);
208const macho = std.macho;
209const math = std.math;
210const mem = std.mem;
211const std = @import("std");
212const trace = @import("../../tracy.zig").trace;
213
214const Allocator = mem.Allocator;
215const Atom = @import("Atom.zig");
216const MachO = @import("../MachO.zig");
217const Relocation = @import("Relocation.zig");
218const Symbol = @import("Symbol.zig");
test/behavior/call.zig+4-4
......@@ -549,7 +549,7 @@ test "call function pointer in comptime field" {
549549 auto: [max_len]u8 = undefined,
550550 offset: u64 = 0,
551551
552 comptime capacity: *const fn () u64 = capacity,
552 comptime capacityFn: *const fn () u64 = capacity,
553553
554554 const max_len: u64 = 32;
555555
......@@ -558,9 +558,9 @@ test "call function pointer in comptime field" {
558558 }
559559 };
560560
561 const a: Auto = .{ .offset = 16, .capacity = Auto.capacity };
562 try std.testing.expect(a.capacity() == 32);
563 try std.testing.expect((a.capacity)() == 32);
561 const a: Auto = .{ .offset = 16, .capacityFn = Auto.capacity };
562 try std.testing.expect(a.capacityFn() == 32);
563 try std.testing.expect((a.capacityFn)() == 32);
564564}
565565
566566test "generic function pointer can be called" {
test/behavior/packed-union.zig+2-2
......@@ -149,12 +149,12 @@ test "packed union initialized with a runtime value" {
149149 value: u63,
150150 fields: Fields,
151151
152 fn value() i64 {
152 fn getValue() i64 {
153153 return 1341;
154154 }
155155 };
156156
157 const timestamp: i64 = ID.value();
157 const timestamp: i64 = ID.getValue();
158158 const id = ID{ .fields = Fields{
159159 .timestamp = @as(u50, @intCast(timestamp)),
160160 .random_bits = 420,
test/behavior/struct.zig+3-3
......@@ -1529,15 +1529,15 @@ test "function pointer in struct returns the struct" {
15291529
15301530 const A = struct {
15311531 const A = @This();
1532 f: *const fn () A,
1532 ptr: *const fn () A,
15331533
15341534 fn f() A {
1535 return .{ .f = f };
1535 return .{ .ptr = f };
15361536 }
15371537 };
15381538 var a = A.f();
15391539 _ = &a;
1540 try expect(a.f == A.f);
1540 try expect(a.ptr == A.f);
15411541}
15421542
15431543test "no dependency loop on optional field wrapped in generic function" {
test/behavior/union.zig+4-16
......@@ -155,18 +155,6 @@ test "unions embedded in aggregate types" {
155155 }
156156}
157157
158test "access a member of tagged union with conflicting enum tag name" {
159 const Bar = union(enum) {
160 A: A,
161 B: B,
162
163 const A = u8;
164 const B = void;
165 };
166
167 comptime assert(Bar.A == u8);
168}
169
170158test "constant tagged union with payload" {
171159 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
172160 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
......@@ -1417,10 +1405,10 @@ test "union field ptr - zero sized payload" {
14171405 const U = union {
14181406 foo: void,
14191407 bar: void,
1420 fn bar(_: *void) void {}
1408 fn qux(_: *void) void {}
14211409 };
14221410 var u: U = .{ .foo = {} };
1423 U.bar(&u.foo);
1411 U.qux(&u.foo);
14241412}
14251413
14261414test "union field ptr - zero sized field" {
......@@ -1431,10 +1419,10 @@ test "union field ptr - zero sized field" {
14311419 const U = union {
14321420 foo: void,
14331421 bar: u32,
1434 fn bar(_: *void) void {}
1422 fn qux(_: *void) void {}
14351423 };
14361424 var u: U = .{ .foo = {} };
1437 U.bar(&u.foo);
1425 U.qux(&u.foo);
14381426}
14391427
14401428test "packed union in packed struct" {
test/cases/compile_errors/colliding_invalid_top_level_functions.zig+3-4
......@@ -5,10 +5,9 @@ export fn entry() usize {
55}
66
77// error
8// backend=stage2
9// target=native
108//
11// :2:1: error: redeclaration of 'func'
12// :1:1: note: other declaration here
9// :1:4: error: duplicate struct member name 'func'
10// :2:4: note: duplicate name here
11// :1:1: note: struct declared here
1312// :1:11: error: use of undeclared identifier 'bogus'
1413// :2:11: error: use of undeclared identifier 'bogus'
test/cases/compile_errors/decl_shadows_local.zig+3-2
......@@ -2,6 +2,7 @@ fn foo(a: usize) void {
22 struct {
33 const a = 1;
44 };
5 _ = a;
56}
67fn bar(a: usize) void {
78 struct {
......@@ -18,5 +19,5 @@ fn bar(a: usize) void {
1819//
1920// :3:15: error: declaration 'a' shadows function parameter from outer scope
2021// :1:8: note: previous declaration here
21// :9:19: error: declaration 'a' shadows function parameter from outer scope
22// :6:8: note: previous declaration here
22// :10:19: error: declaration 'a' shadows function parameter from outer scope
23// :7:8: note: previous declaration here
test/cases/compile_errors/duplicate_enum_field.zig+2-2
......@@ -12,6 +12,6 @@ export fn entry() void {
1212// backend=stage2
1313// target=native
1414//
15// :2:5: error: duplicate enum field name
16// :3:5: note: duplicate field here
15// :2:5: error: duplicate enum member name 'Bar'
16// :3:5: note: duplicate name here
1717// :1:13: note: enum declared here
test/cases/compile_errors/duplicate_struct_field.zig+5-5
......@@ -24,10 +24,10 @@ export fn b() void {
2424// backend=stage2
2525// target=native
2626//
27// :2:5: error: duplicate struct field name
28// :3:5: note: duplicate field here
27// :2:5: error: duplicate struct member name 'Bar'
28// :3:5: note: duplicate name here
2929// :1:13: note: struct declared here
30// :7:5: error: duplicate struct field name
31// :9:5: note: duplicate field here
32// :10:5: note: duplicate field here
30// :7:5: error: duplicate struct member name 'a'
31// :9:5: note: duplicate name here
32// :10:5: note: duplicate name here
3333// :6:11: note: struct declared here
test/cases/compile_errors/duplicate_union_field.zig+2-4
......@@ -8,9 +8,7 @@ export fn entry() void {
88}
99
1010// error
11// backend=stage2
12// target=native
1311//
14// :2:5: error: duplicate union field name
15// :3:5: note: duplicate field here
12// :2:5: error: duplicate union member name 'Bar'
13// :3:5: note: duplicate name here
1614// :1:13: note: union declared here
test/cases/compile_errors/error_in_struct_initializer_doesnt_crash_the_compiler.zig+2-4
......@@ -8,9 +8,7 @@ pub export fn entry() void {
88}
99
1010// error
11// backend=stage2
12// target=native
1311//
14// :3:9: error: duplicate struct field name
15// :4:9: note: duplicate field here
12// :3:9: error: duplicate struct member name 'e'
13// :4:9: note: duplicate name here
1614// :2:22: note: struct declared here
test/cases/compile_errors/field_decl_name_conflict.zig created+18
......@@ -0,0 +1,18 @@
1foo: u32,
2bar: u32,
3qux: u32,
4
5const foo = 123;
6
7var bar: u8 = undefined;
8fn bar() void {}
9
10// error
11//
12// :1:1: error: duplicate struct member name 'foo'
13// :5:7: note: duplicate name here
14// :1:1: note: struct declared here
15// :2:1: error: duplicate struct member name 'bar'
16// :7:5: note: duplicate name here
17// :8:4: note: duplicate name here
18// :1:1: note: struct declared here
test/cases/compile_errors/invalid_duplicate_test_decl_name.zig+3-2
......@@ -6,5 +6,6 @@ test "thingy" {}
66// target=native
77// is_test=true
88//
9// :2:1: error: duplicate test name 'thingy'
10// :1:1: note: other test here
9// :1:6: error: duplicate test name 'thingy'
10// :2:6: note: duplicate test here
11// :1:1: note: struct declared here
test/cases/compile_errors/invalid_store_to_comptime_field.zig+4-4
......@@ -25,21 +25,21 @@ pub export fn entry3() void {
2525 const U = struct {
2626 comptime foo: u32 = 1,
2727 bar: u32,
28 fn foo(x: @This()) void {
28 fn qux(x: @This()) void {
2929 _ = x;
3030 }
3131 };
32 _ = U.foo(U{ .foo = 2, .bar = 2 });
32 _ = U.qux(U{ .foo = 2, .bar = 2 });
3333}
3434pub export fn entry4() void {
3535 const U = struct {
3636 comptime foo: u32 = 1,
3737 bar: u32,
38 fn foo(x: @This()) void {
38 fn qux(x: @This()) void {
3939 _ = x;
4040 }
4141 };
42 _ = U.foo(.{ .foo = 2, .bar = 2 });
42 _ = U.qux(.{ .foo = 2, .bar = 2 });
4343}
4444pub export fn entry5() void {
4545 comptime var y = .{ 1, 2 };
test/cases/compile_errors/multiple_function_definitions.zig+3-4
......@@ -5,8 +5,7 @@ export fn entry() void {
55}
66
77// error
8// backend=stage2
9// target=native
108//
11// :2:1: error: redeclaration of 'a'
12// :1:1: note: other declaration here
9// :1:4: error: duplicate struct member name 'a'
10// :2:4: note: duplicate name here
11// :1:1: note: struct declared here
test/cases/compile_errors/redefinition_of_enums.zig+3-2
......@@ -5,5 +5,6 @@ const A = enum { x };
55// backend=stage2
66// target=native
77//
8// :2:1: error: redeclaration of 'A'
9// :1:1: note: other declaration here
8// :1:7: error: duplicate struct member name 'A'
9// :2:7: note: duplicate name here
10// :1:1: note: struct declared here
test/cases/compile_errors/redefinition_of_global_variables.zig+3-2
......@@ -5,5 +5,6 @@ var a: i32 = 2;
55// backend=stage2
66// target=native
77//
8// :2:1: error: redeclaration of 'a'
9// :1:1: note: other declaration here
8// :1:5: error: duplicate struct member name 'a'
9// :2:5: note: duplicate name here
10// :1:1: note: struct declared here
test/cases/compile_errors/redefinition_of_struct.zig+3-2
......@@ -5,5 +5,6 @@ const A = struct { y: i32 };
55// backend=stage2
66// target=native
77//
8// :2:1: error: redeclaration of 'A'
9// :1:1: note: other declaration here
8// :1:7: error: duplicate struct member name 'A'
9// :2:7: note: duplicate name here
10// :1:1: note: struct declared here
test/cases/compile_errors/struct_duplicate_field_name.zig+2-2
......@@ -11,6 +11,6 @@ export fn entry() void {
1111// error
1212// target=native
1313//
14// :2:5: error: duplicate struct field name
15// :3:5: note: duplicate field here
14// :2:5: error: duplicate struct member name 'foo'
15// :3:5: note: duplicate name here
1616// :1:11: note: struct declared here
test/cases/compile_errors/union_duplicate_enum_field.zig+2-2
......@@ -12,6 +12,6 @@ export fn foo() void {
1212// error
1313// target=native
1414//
15// :3:5: error: duplicate union field name
16// :4:5: note: duplicate field here
15// :3:5: error: duplicate union member name 'a'
16// :4:5: note: duplicate name here
1717// :2:11: note: union declared here
test/cases/compile_errors/union_duplicate_field_definition.zig+2-2
......@@ -11,6 +11,6 @@ export fn entry() void {
1111// error
1212// target=native
1313//
14// :2:5: error: duplicate union field name
15// :3:5: note: duplicate field here
14// :2:5: error: duplicate union member name 'foo'
15// :3:5: note: duplicate name here
1616// :1:11: note: union declared here
test/cases/function_redeclaration.zig+3-2
......@@ -8,7 +8,8 @@ fn foo() void {
88
99// error
1010//
11// :3:1: error: redeclaration of 'entry'
12// :2:1: note: other declaration here
11// :2:4: error: duplicate struct member name 'entry'
12// :3:4: note: duplicate name here
13// :2:1: note: struct declared here
1314// :6:9: error: local variable shadows declaration of 'foo'
1415// :5:1: note: declared here
test/cases/global_variable_redeclaration.zig+3-2
......@@ -4,5 +4,6 @@ var foo = true;
44
55// error
66//
7// :3:1: error: redeclaration of 'foo'
8// :2:1: note: other declaration here
7// :2:5: error: duplicate struct member name 'foo'
8// :3:5: note: duplicate name here
9// :2:1: note: struct declared here
test/compile_errors.zig+2-2
......@@ -92,7 +92,7 @@ pub fn addCases(ctx: *Cases, b: *std.Build) !void {
9292 \\const a = @import("a.zig");
9393 \\
9494 \\export fn entry() void {
95 \\ _ = a.S.foo(a.S{ .foo = 2, .bar = 2 });
95 \\ _ = a.S.qux(a.S{ .foo = 2, .bar = 2 });
9696 \\}
9797 , &[_][]const u8{
9898 ":4:23: error: value stored in comptime field does not match the default value of the field",
......@@ -102,7 +102,7 @@ pub fn addCases(ctx: *Cases, b: *std.Build) !void {
102102 \\pub const S = struct {
103103 \\ comptime foo: u32 = 1,
104104 \\ bar: u32,
105 \\ pub fn foo(x: @This()) void {
105 \\ pub fn qux(x: @This()) void {
106106 \\ _ = x;
107107 \\ }
108108 \\};