authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-07 11:17:42-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-07 11:17:42-07:00
log52b8239a22aa37fe3914427cd4e2905231769e59
treecd60ca825c14b5befbcddf674bdb7d3feda81d23
parent338f155a02b72117ff710f72c8578e7d2f8eb296
parent533bfc68bf8b4ad7ffbe5814a622f200dc345b69

Merge remote-tracking branch 'origin/master' into llvm11


84 files changed, 1591 insertions(+), 734 deletions(-)

doc/langref.html.in+15-15
......@@ -2156,7 +2156,7 @@ test "pointer casting" {
21562156
21572157test "pointer child type" {
21582158 // pointer types have a `child` field which tells you the type they point to.
2159 assert((*u32).Child == u32);
2159 assert(@typeInfo(*u32).Pointer.child == u32);
21602160}
21612161 {#code_end#}
21622162 {#header_open|Alignment#}
......@@ -2184,7 +2184,7 @@ test "variable alignment" {
21842184 assert(@TypeOf(&x) == *i32);
21852185 assert(*i32 == *align(align_of_i32) i32);
21862186 if (std.Target.current.cpu.arch == .x86_64) {
2187 assert((*i32).alignment == 4);
2187 assert(@typeInfo(*i32).Pointer.alignment == 4);
21882188 }
21892189}
21902190 {#code_end#}
......@@ -2202,7 +2202,7 @@ const assert = @import("std").debug.assert;
22022202var foo: u8 align(4) = 100;
22032203
22042204test "global variable alignment" {
2205 assert(@TypeOf(&foo).alignment == 4);
2205 assert(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4);
22062206 assert(@TypeOf(&foo) == *align(4) u8);
22072207 const as_pointer_to_array: *[1]u8 = &foo;
22082208 const as_slice: []u8 = as_pointer_to_array;
......@@ -4310,8 +4310,8 @@ test "fn type inference" {
43104310const assert = @import("std").debug.assert;
43114311
43124312test "fn reflection" {
4313 assert(@TypeOf(assert).ReturnType == void);
4314 assert(@TypeOf(assert).is_var_args == false);
4313 assert(@typeInfo(@TypeOf(assert)).Fn.return_type.? == void);
4314 assert(@typeInfo(@TypeOf(assert)).Fn.is_var_args == false);
43154315}
43164316 {#code_end#}
43174317 {#header_close#}
......@@ -4611,10 +4611,10 @@ test "error union" {
46114611 foo = error.SomeError;
46124612
46134613 // Use compile-time reflection to access the payload type of an error union:
4614 comptime assert(@TypeOf(foo).Payload == i32);
4614 comptime assert(@typeInfo(@TypeOf(foo)).ErrorUnion.payload == i32);
46154615
46164616 // Use compile-time reflection to access the error set type of an error union:
4617 comptime assert(@TypeOf(foo).ErrorSet == anyerror);
4617 comptime assert(@typeInfo(@TypeOf(foo)).ErrorUnion.error_set == anyerror);
46184618}
46194619 {#code_end#}
46204620 {#header_open|Merging Error Sets#}
......@@ -4991,7 +4991,7 @@ test "optional type" {
49914991 foo = 1234;
49924992
49934993 // Use compile-time reflection to access the child type of the optional:
4994 comptime assert(@TypeOf(foo).Child == i32);
4994 comptime assert(@typeInfo(@TypeOf(foo)).Optional.child == i32);
49954995}
49964996 {#code_end#}
49974997 {#header_close#}
......@@ -6889,7 +6889,7 @@ fn func(y: *i32) void {
68896889 This builtin function atomically dereferences a pointer and returns the value.
68906890 </p>
68916891 <p>
6892 {#syntax#}T{#endsyntax#} must be a {#syntax#}bool{#endsyntax#}, a float,
6892 {#syntax#}T{#endsyntax#} must be a pointer, a {#syntax#}bool{#endsyntax#}, a float,
68936893 an integer or an enum.
68946894 </p>
68956895 {#header_close#}
......@@ -6899,7 +6899,7 @@ fn func(y: *i32) void {
68996899 This builtin function atomically modifies memory and then returns the previous value.
69006900 </p>
69016901 <p>
6902 {#syntax#}T{#endsyntax#} must be a {#syntax#}bool{#endsyntax#}, a float,
6902 {#syntax#}T{#endsyntax#} must be a pointer, a {#syntax#}bool{#endsyntax#}, a float,
69036903 an integer or an enum.
69046904 </p>
69056905 <p>
......@@ -6925,7 +6925,7 @@ fn func(y: *i32) void {
69256925 This builtin function atomically stores a value.
69266926 </p>
69276927 <p>
6928 {#syntax#}T{#endsyntax#} must be a {#syntax#}bool{#endsyntax#}, a float,
6928 {#syntax#}T{#endsyntax#} must be a pointer, a {#syntax#}bool{#endsyntax#}, a float,
69296929 an integer or an enum.
69306930 </p>
69316931 {#header_close#}
......@@ -7208,10 +7208,10 @@ fn cmpxchgStrongButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_v
72087208 more efficiently in machine instructions.
72097209 </p>
72107210 <p>
7211 {#syntax#}T{#endsyntax#} must be a {#syntax#}bool{#endsyntax#}, a float,
7211 {#syntax#}T{#endsyntax#} must be a pointer, a {#syntax#}bool{#endsyntax#}, a float,
72127212 an integer or an enum.
72137213 </p>
7214 <p>{#syntax#}@TypeOf(ptr).alignment{#endsyntax#} must be {#syntax#}>= @sizeOf(T).{#endsyntax#}</p>
7214 <p>{#syntax#}@typeInfo(@TypeOf(ptr)).Pointer.alignment{#endsyntax#} must be {#syntax#}>= @sizeOf(T).{#endsyntax#}</p>
72157215 {#see_also|Compile Variables|cmpxchgWeak#}
72167216 {#header_close#}
72177217 {#header_open|@cmpxchgWeak#}
......@@ -7237,10 +7237,10 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val
72377237 However if you need a stronger guarantee, use {#link|@cmpxchgStrong#}.
72387238 </p>
72397239 <p>
7240 {#syntax#}T{#endsyntax#} must be a {#syntax#}bool{#endsyntax#}, a float,
7240 {#syntax#}T{#endsyntax#} must be a pointer, a {#syntax#}bool{#endsyntax#}, a float,
72417241 an integer or an enum.
72427242 </p>
7243 <p>{#syntax#}@TypeOf(ptr).alignment{#endsyntax#} must be {#syntax#}>= @sizeOf(T).{#endsyntax#}</p>
7243 <p>{#syntax#}@typeInfo(@TypeOf(ptr)).Pointer.alignment{#endsyntax#} must be {#syntax#}>= @sizeOf(T).{#endsyntax#}</p>
72447244 {#see_also|Compile Variables|cmpxchgStrong#}
72457245 {#header_close#}
72467246
lib/std/array_list.zig+10-2
......@@ -46,7 +46,11 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
4646 /// Deinitialize with `deinit` or use `toOwnedSlice`.
4747 pub fn initCapacity(allocator: *Allocator, num: usize) !Self {
4848 var self = Self.init(allocator);
49 try self.ensureCapacity(num);
49
50 const new_memory = try self.allocator.allocAdvanced(T, alignment, num, .at_least);
51 self.items.ptr = new_memory.ptr;
52 self.capacity = new_memory.len;
53
5054 return self;
5155 }
5256
......@@ -366,7 +370,11 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
366370 /// Deinitialize with `deinit` or use `toOwnedSlice`.
367371 pub fn initCapacity(allocator: *Allocator, num: usize) !Self {
368372 var self = Self{};
369 try self.ensureCapacity(allocator, num);
373
374 const new_memory = try self.allocator.allocAdvanced(T, alignment, num, .at_least);
375 self.items.ptr = new_memory.ptr;
376 self.capacity = new_memory.len;
377
370378 return self;
371379 }
372380
lib/std/child_process.zig+3-5
......@@ -275,9 +275,7 @@ pub const ChildProcess = struct {
275275 }
276276
277277 fn handleWaitResult(self: *ChildProcess, status: u32) void {
278 // TODO https://github.com/ziglang/zig/issues/3190
279 var term = self.cleanupAfterWait(status);
280 self.term = term;
278 self.term = self.cleanupAfterWait(status);
281279 }
282280
283281 fn cleanupStreams(self: *ChildProcess) void {
......@@ -487,8 +485,8 @@ pub const ChildProcess = struct {
487485 const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore);
488486
489487 const nul_handle = if (any_ignore)
490 windows.OpenFile(&[_]u16{ 'N', 'U', 'L' }, .{
491 .dir = std.fs.cwd().fd,
488 // "\Device\Null" or "\??\NUL"
489 windows.OpenFile(&[_]u16{ '\\', 'D', 'e', 'v', 'i', 'c', 'e', '\\', 'N', 'u', 'l', 'l' }, .{
492490 .access_mask = windows.GENERIC_READ | windows.SYNCHRONIZE,
493491 .share_access = windows.FILE_SHARE_READ,
494492 .creation = windows.OPEN_EXISTING,
lib/std/coff.zig+66
......@@ -18,11 +18,77 @@ const IMAGE_FILE_MACHINE_I386 = 0x014c;
1818const IMAGE_FILE_MACHINE_IA64 = 0x0200;
1919const IMAGE_FILE_MACHINE_AMD64 = 0x8664;
2020
21pub const MachineType = enum(u16) {
22 Unknown = 0x0,
23 /// Matsushita AM33
24 AM33 = 0x1d3,
25 /// x64
26 X64 = 0x8664,
27 /// ARM little endian
28 ARM = 0x1c0,
29 /// ARM64 little endian
30 ARM64 = 0xaa64,
31 /// ARM Thumb-2 little endian
32 ARMNT = 0x1c4,
33 /// EFI byte code
34 EBC = 0xebc,
35 /// Intel 386 or later processors and compatible processors
36 I386 = 0x14c,
37 /// Intel Itanium processor family
38 IA64 = 0x200,
39 /// Mitsubishi M32R little endian
40 M32R = 0x9041,
41 /// MIPS16
42 MIPS16 = 0x266,
43 /// MIPS with FPU
44 MIPSFPU = 0x366,
45 /// MIPS16 with FPU
46 MIPSFPU16 = 0x466,
47 /// Power PC little endian
48 POWERPC = 0x1f0,
49 /// Power PC with floating point support
50 POWERPCFP = 0x1f1,
51 /// MIPS little endian
52 R4000 = 0x166,
53 /// RISC-V 32-bit address space
54 RISCV32 = 0x5032,
55 /// RISC-V 64-bit address space
56 RISCV64 = 0x5064,
57 /// RISC-V 128-bit address space
58 RISCV128 = 0x5128,
59 /// Hitachi SH3
60 SH3 = 0x1a2,
61 /// Hitachi SH3 DSP
62 SH3DSP = 0x1a3,
63 /// Hitachi SH4
64 SH4 = 0x1a6,
65 /// Hitachi SH5
66 SH5 = 0x1a8,
67 /// Thumb
68 Thumb = 0x1c2,
69 /// MIPS little-endian WCE v2
70 WCEMIPSV2 = 0x169,
71};
72
2173// OptionalHeader.magic values
2274// see https://msdn.microsoft.com/en-us/library/windows/desktop/ms680339(v=vs.85).aspx
2375const IMAGE_NT_OPTIONAL_HDR32_MAGIC = 0x10b;
2476const IMAGE_NT_OPTIONAL_HDR64_MAGIC = 0x20b;
2577
78// Image Characteristics
79pub const IMAGE_FILE_RELOCS_STRIPPED = 0x1;
80pub const IMAGE_FILE_DEBUG_STRIPPED = 0x200;
81pub const IMAGE_FILE_EXECUTABLE_IMAGE = 0x2;
82pub const IMAGE_FILE_32BIT_MACHINE = 0x100;
83pub const IMAGE_FILE_LARGE_ADDRESS_AWARE = 0x20;
84
85// Section flags
86pub const IMAGE_SCN_CNT_INITIALIZED_DATA = 0x40;
87pub const IMAGE_SCN_MEM_READ = 0x40000000;
88pub const IMAGE_SCN_CNT_CODE = 0x20;
89pub const IMAGE_SCN_MEM_EXECUTE = 0x20000000;
90pub const IMAGE_SCN_MEM_WRITE = 0x80000000;
91
2692const IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16;
2793const IMAGE_DEBUG_TYPE_CODEVIEW = 2;
2894const DEBUG_DIRECTORY = 6;
lib/std/debug/leb128.zig+24-23
......@@ -9,10 +9,10 @@ const testing = std.testing;
99/// Read a single unsigned LEB128 value from the given reader as type T,
1010/// or error.Overflow if the value cannot fit.
1111pub fn readULEB128(comptime T: type, reader: anytype) !T {
12 const U = if (T.bit_count < 8) u8 else T;
12 const U = if (@typeInfo(T).Int.bits < 8) u8 else T;
1313 const ShiftT = std.math.Log2Int(U);
1414
15 const max_group = (U.bit_count + 6) / 7;
15 const max_group = (@typeInfo(U).Int.bits + 6) / 7;
1616
1717 var value = @as(U, 0);
1818 var group = @as(ShiftT, 0);
......@@ -40,7 +40,7 @@ pub fn readULEB128(comptime T: type, reader: anytype) !T {
4040/// Write a single unsigned integer as unsigned LEB128 to the given writer.
4141pub fn writeULEB128(writer: anytype, uint_value: anytype) !void {
4242 const T = @TypeOf(uint_value);
43 const U = if (T.bit_count < 8) u8 else T;
43 const U = if (@typeInfo(T).Int.bits < 8) u8 else T;
4444 var value = @intCast(U, uint_value);
4545
4646 while (true) {
......@@ -68,7 +68,7 @@ pub fn readULEB128Mem(comptime T: type, ptr: *[]const u8) !T {
6868/// returning the number of bytes written.
6969pub fn writeULEB128Mem(ptr: []u8, uint_value: anytype) !usize {
7070 const T = @TypeOf(uint_value);
71 const max_group = (T.bit_count + 6) / 7;
71 const max_group = (@typeInfo(T).Int.bits + 6) / 7;
7272 var buf = std.io.fixedBufferStream(ptr);
7373 try writeULEB128(buf.writer(), uint_value);
7474 return buf.pos;
......@@ -77,11 +77,11 @@ pub fn writeULEB128Mem(ptr: []u8, uint_value: anytype) !usize {
7777/// Read a single signed LEB128 value from the given reader as type T,
7878/// or error.Overflow if the value cannot fit.
7979pub fn readILEB128(comptime T: type, reader: anytype) !T {
80 const S = if (T.bit_count < 8) i8 else T;
81 const U = std.meta.Int(false, S.bit_count);
80 const S = if (@typeInfo(T).Int.bits < 8) i8 else T;
81 const U = std.meta.Int(false, @typeInfo(S).Int.bits);
8282 const ShiftU = std.math.Log2Int(U);
8383
84 const max_group = (U.bit_count + 6) / 7;
84 const max_group = (@typeInfo(U).Int.bits + 6) / 7;
8585
8686 var value = @as(U, 0);
8787 var group = @as(ShiftU, 0);
......@@ -97,7 +97,7 @@ pub fn readILEB128(comptime T: type, reader: anytype) !T {
9797 if (@bitCast(S, temp) >= 0) return error.Overflow;
9898
9999 // and all the overflowed bits are 1
100 const remaining_shift = @intCast(u3, U.bit_count - @as(u16, shift));
100 const remaining_shift = @intCast(u3, @typeInfo(U).Int.bits - @as(u16, shift));
101101 const remaining_bits = @bitCast(i8, byte | 0x80) >> remaining_shift;
102102 if (remaining_bits != -1) return error.Overflow;
103103 }
......@@ -127,8 +127,8 @@ pub fn readILEB128(comptime T: type, reader: anytype) !T {
127127/// Write a single signed integer as signed LEB128 to the given writer.
128128pub fn writeILEB128(writer: anytype, int_value: anytype) !void {
129129 const T = @TypeOf(int_value);
130 const S = if (T.bit_count < 8) i8 else T;
131 const U = std.meta.Int(false, S.bit_count);
130 const S = if (@typeInfo(T).Int.bits < 8) i8 else T;
131 const U = std.meta.Int(false, @typeInfo(S).Int.bits);
132132
133133 var value = @intCast(S, int_value);
134134
......@@ -173,7 +173,7 @@ pub fn writeILEB128Mem(ptr: []u8, int_value: anytype) !usize {
173173/// different value without shifting all the following code.
174174pub fn writeUnsignedFixed(comptime l: usize, ptr: *[l]u8, int: std.meta.Int(false, l * 7)) void {
175175 const T = @TypeOf(int);
176 const U = if (T.bit_count < 8) u8 else T;
176 const U = if (@typeInfo(T).Int.bits < 8) u8 else T;
177177 var value = @intCast(U, int);
178178
179179 comptime var i = 0;
......@@ -346,28 +346,29 @@ test "deserialize unsigned LEB128" {
346346
347347fn test_write_leb128(value: anytype) !void {
348348 const T = @TypeOf(value);
349 const t_signed = @typeInfo(T).Int.is_signed;
349350
350 const writeStream = if (T.is_signed) writeILEB128 else writeULEB128;
351 const writeMem = if (T.is_signed) writeILEB128Mem else writeULEB128Mem;
352 const readStream = if (T.is_signed) readILEB128 else readULEB128;
353 const readMem = if (T.is_signed) readILEB128Mem else readULEB128Mem;
351 const writeStream = if (t_signed) writeILEB128 else writeULEB128;
352 const writeMem = if (t_signed) writeILEB128Mem else writeULEB128Mem;
353 const readStream = if (t_signed) readILEB128 else readULEB128;
354 const readMem = if (t_signed) readILEB128Mem else readULEB128Mem;
354355
355356 // decode to a larger bit size too, to ensure sign extension
356357 // is working as expected
357 const larger_type_bits = ((T.bit_count + 8) / 8) * 8;
358 const B = std.meta.Int(T.is_signed, larger_type_bits);
358 const larger_type_bits = ((@typeInfo(T).Int.bits + 8) / 8) * 8;
359 const B = std.meta.Int(t_signed, larger_type_bits);
359360
360361 const bytes_needed = bn: {
361 const S = std.meta.Int(T.is_signed, @sizeOf(T) * 8);
362 if (T.bit_count <= 7) break :bn @as(u16, 1);
362 const S = std.meta.Int(t_signed, @sizeOf(T) * 8);
363 if (@typeInfo(T).Int.bits <= 7) break :bn @as(u16, 1);
363364
364365 const unused_bits = if (value < 0) @clz(T, ~value) else @clz(T, value);
365 const used_bits: u16 = (T.bit_count - unused_bits) + @boolToInt(T.is_signed);
366 const used_bits: u16 = (@typeInfo(T).Int.bits - unused_bits) + @boolToInt(t_signed);
366367 if (used_bits <= 7) break :bn @as(u16, 1);
367368 break :bn ((used_bits + 6) / 7);
368369 };
369370
370 const max_groups = if (T.bit_count == 0) 1 else (T.bit_count + 6) / 7;
371 const max_groups = if (@typeInfo(T).Int.bits == 0) 1 else (@typeInfo(T).Int.bits + 6) / 7;
371372
372373 var buf: [max_groups]u8 = undefined;
373374 var fbs = std.io.fixedBufferStream(&buf);
......@@ -414,7 +415,7 @@ test "serialize unsigned LEB128" {
414415 const T = std.meta.Int(false, t);
415416 const min = std.math.minInt(T);
416417 const max = std.math.maxInt(T);
417 var i = @as(std.meta.Int(false, T.bit_count + 1), min);
418 var i = @as(std.meta.Int(false, @typeInfo(T).Int.bits + 1), min);
418419
419420 while (i <= max) : (i += 1) try test_write_leb128(@intCast(T, i));
420421 }
......@@ -432,7 +433,7 @@ test "serialize signed LEB128" {
432433 const T = std.meta.Int(true, t);
433434 const min = std.math.minInt(T);
434435 const max = std.math.maxInt(T);
435 var i = @as(std.meta.Int(true, T.bit_count + 1), min);
436 var i = @as(std.meta.Int(true, @typeInfo(T).Int.bits + 1), min);
436437
437438 while (i <= max) : (i += 1) try test_write_leb128(@intCast(T, i));
438439 }
lib/std/fmt.zig+13-10
......@@ -82,6 +82,8 @@ fn peekIsAlign(comptime fmt: []const u8) bool {
8282/// This allows user types to be formatted in a logical manner instead of dumping all fields of the type.
8383///
8484/// A user type may be a `struct`, `vector`, `union` or `enum` type.
85///
86/// To print literal curly braces, escape them by writing them twice, e.g. `{{` or `}}`.
8587pub fn format(
8688 writer: anytype,
8789 comptime fmt: []const u8,
......@@ -91,7 +93,7 @@ pub fn format(
9193 if (@typeInfo(@TypeOf(args)) != .Struct) {
9294 @compileError("Expected tuple or struct argument, found " ++ @typeName(@TypeOf(args)));
9395 }
94 if (args.len > ArgSetType.bit_count) {
96 if (args.len > @typeInfo(ArgSetType).Int.bits) {
9597 @compileError("32 arguments max are supported per format call");
9698 }
9799
......@@ -325,7 +327,7 @@ pub fn formatType(
325327 max_depth: usize,
326328) @TypeOf(writer).Error!void {
327329 if (comptime std.mem.eql(u8, fmt, "*")) {
328 try writer.writeAll(@typeName(@TypeOf(value).Child));
330 try writer.writeAll(@typeName(@typeInfo(@TypeOf(value)).Pointer.child));
329331 try writer.writeAll("@");
330332 try formatInt(@ptrToInt(value), 16, false, FormatOptions{}, writer);
331333 return;
......@@ -430,12 +432,12 @@ pub fn formatType(
430432 if (info.child == u8) {
431433 return formatText(value, fmt, options, writer);
432434 }
433 return format(writer, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) });
435 return format(writer, "{}@{x}", .{ @typeName(@typeInfo(T).Pointer.child), @ptrToInt(value) });
434436 },
435437 .Enum, .Union, .Struct => {
436438 return formatType(value.*, fmt, options, writer, max_depth);
437439 },
438 else => return format(writer, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }),
440 else => return format(writer, "{}@{x}", .{ @typeName(@typeInfo(T).Pointer.child), @ptrToInt(value) }),
439441 },
440442 .Many, .C => {
441443 if (ptr_info.sentinel) |sentinel| {
......@@ -446,7 +448,7 @@ pub fn formatType(
446448 return formatText(mem.span(value), fmt, options, writer);
447449 }
448450 }
449 return format(writer, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) });
451 return format(writer, "{}@{x}", .{ @typeName(@typeInfo(T).Pointer.child), @ptrToInt(value) });
450452 },
451453 .Slice => {
452454 if (fmt.len > 0 and ((fmt[0] == 'x') or (fmt[0] == 'X'))) {
......@@ -536,7 +538,7 @@ pub fn formatIntValue(
536538 radix = 10;
537539 uppercase = false;
538540 } else if (comptime std.mem.eql(u8, fmt, "c")) {
539 if (@TypeOf(int_value).bit_count <= 8) {
541 if (@typeInfo(@TypeOf(int_value)).Int.bits <= 8) {
540542 return formatAsciiChar(@as(u8, int_value), options, writer);
541543 } else {
542544 @compileError("Cannot print integer that is larger than 8 bits as a ascii");
......@@ -945,7 +947,7 @@ pub fn formatInt(
945947 } else
946948 value;
947949
948 if (@TypeOf(int_value).is_signed) {
950 if (@typeInfo(@TypeOf(int_value)).Int.is_signed) {
949951 return formatIntSigned(int_value, base, uppercase, options, writer);
950952 } else {
951953 return formatIntUnsigned(int_value, base, uppercase, options, writer);
......@@ -987,9 +989,10 @@ fn formatIntUnsigned(
987989 writer: anytype,
988990) !void {
989991 assert(base >= 2);
990 var buf: [math.max(@TypeOf(value).bit_count, 1)]u8 = undefined;
991 const min_int_bits = comptime math.max(@TypeOf(value).bit_count, @TypeOf(base).bit_count);
992 const MinInt = std.meta.Int(@TypeOf(value).is_signed, min_int_bits);
992 const value_info = @typeInfo(@TypeOf(value)).Int;
993 var buf: [math.max(value_info.bits, 1)]u8 = undefined;
994 const min_int_bits = comptime math.max(value_info.bits, @typeInfo(@TypeOf(base)).Int.bits);
995 const MinInt = std.meta.Int(value_info.is_signed, min_int_bits);
993996 var a: MinInt = value;
994997 var index: usize = buf.len;
995998
lib/std/fmt/parse_float.zig+1-1
......@@ -374,7 +374,7 @@ test "fmt.parseFloat" {
374374 const epsilon = 1e-7;
375375
376376 inline for ([_]type{ f16, f32, f64, f128 }) |T| {
377 const Z = std.meta.Int(false, T.bit_count);
377 const Z = std.meta.Int(false, @typeInfo(T).Float.bits);
378378
379379 testing.expectError(error.InvalidCharacter, parseFloat(T, ""));
380380 testing.expectError(error.InvalidCharacter, parseFloat(T, " 1"));
lib/std/fs.zig+9-3
......@@ -1437,26 +1437,32 @@ pub const Dir = struct {
14371437 /// On success, caller owns returned buffer.
14381438 /// If the file is larger than `max_bytes`, returns `error.FileTooBig`.
14391439 pub fn readFileAlloc(self: Dir, allocator: *mem.Allocator, file_path: []const u8, max_bytes: usize) ![]u8 {
1440 return self.readFileAllocOptions(allocator, file_path, max_bytes, @alignOf(u8), null);
1440 return self.readFileAllocOptions(allocator, file_path, max_bytes, null, @alignOf(u8), null);
14411441 }
14421442
14431443 /// On success, caller owns returned buffer.
14441444 /// If the file is larger than `max_bytes`, returns `error.FileTooBig`.
1445 /// If `size_hint` is specified the initial buffer size is calculated using
1446 /// that value, otherwise the effective file size is used instead.
14451447 /// Allows specifying alignment and a sentinel value.
14461448 pub fn readFileAllocOptions(
14471449 self: Dir,
14481450 allocator: *mem.Allocator,
14491451 file_path: []const u8,
14501452 max_bytes: usize,
1453 size_hint: ?usize,
14511454 comptime alignment: u29,
14521455 comptime optional_sentinel: ?u8,
14531456 ) !(if (optional_sentinel) |s| [:s]align(alignment) u8 else []align(alignment) u8) {
14541457 var file = try self.openFile(file_path, .{});
14551458 defer file.close();
14561459
1457 const stat_size = try file.getEndPos();
1460 // If the file size doesn't fit a usize it'll be certainly greater than
1461 // `max_bytes`
1462 const stat_size = size_hint orelse math.cast(usize, try file.getEndPos()) catch
1463 return error.FileTooBig;
14581464
1459 return file.readAllAllocOptions(allocator, stat_size, max_bytes, alignment, optional_sentinel);
1465 return file.readToEndAllocOptions(allocator, max_bytes, stat_size, alignment, optional_sentinel);
14601466 }
14611467
14621468 pub const DeleteTreeError = error{
lib/std/fs/file.zig+29-11
......@@ -363,31 +363,49 @@ pub const File = struct {
363363 try os.futimens(self.handle, &times);
364364 }
365365
366 /// Reads all the bytes from the current position to the end of the file.
366367 /// On success, caller owns returned buffer.
367368 /// If the file is larger than `max_bytes`, returns `error.FileTooBig`.
368 pub fn readAllAlloc(self: File, allocator: *mem.Allocator, stat_size: u64, max_bytes: usize) ![]u8 {
369 return self.readAllAllocOptions(allocator, stat_size, max_bytes, @alignOf(u8), null);
369 pub fn readToEndAlloc(self: File, allocator: *mem.Allocator, max_bytes: usize) ![]u8 {
370 return self.readToEndAllocOptions(allocator, max_bytes, null, @alignOf(u8), null);
370371 }
371372
373 /// Reads all the bytes from the current position to the end of the file.
372374 /// On success, caller owns returned buffer.
373375 /// If the file is larger than `max_bytes`, returns `error.FileTooBig`.
376 /// If `size_hint` is specified the initial buffer size is calculated using
377 /// that value, otherwise an arbitrary value is used instead.
374378 /// Allows specifying alignment and a sentinel value.
375 pub fn readAllAllocOptions(
379 pub fn readToEndAllocOptions(
376380 self: File,
377381 allocator: *mem.Allocator,
378 stat_size: u64,
379382 max_bytes: usize,
383 size_hint: ?usize,
380384 comptime alignment: u29,
381385 comptime optional_sentinel: ?u8,
382386 ) !(if (optional_sentinel) |s| [:s]align(alignment) u8 else []align(alignment) u8) {
383 const size = math.cast(usize, stat_size) catch math.maxInt(usize);
384 if (size > max_bytes) return error.FileTooBig;
385
386 const buf = try allocator.allocWithOptions(u8, size, alignment, optional_sentinel);
387 errdefer allocator.free(buf);
387 // If no size hint is provided fall back to the size=0 code path
388 const size = size_hint orelse 0;
389
390 // The file size returned by stat is used as hint to set the buffer
391 // size. If the reported size is zero, as it happens on Linux for files
392 // in /proc, a small buffer is allocated instead.
393 const initial_cap = (if (size > 0) size else 1024) + @boolToInt(optional_sentinel != null);
394 var array_list = try std.ArrayListAligned(u8, alignment).initCapacity(allocator, initial_cap);
395 defer array_list.deinit();
396
397 self.reader().readAllArrayList(&array_list, max_bytes) catch |err| switch (err) {
398 error.StreamTooLong => return error.FileTooBig,
399 else => |e| return e,
400 };
388401
389 try self.reader().readNoEof(buf);
390 return buf;
402 if (optional_sentinel) |sentinel| {
403 try array_list.append(sentinel);
404 const buf = array_list.toOwnedSlice();
405 return buf[0 .. buf.len - 1 :sentinel];
406 } else {
407 return array_list.toOwnedSlice();
408 }
391409 }
392410
393411 pub const ReadError = os.ReadError;
lib/std/fs/test.zig+5-5
......@@ -188,30 +188,30 @@ test "readAllAlloc" {
188188 var file = try tmp_dir.dir.createFile("test_file", .{ .read = true });
189189 defer file.close();
190190
191 const buf1 = try file.readAllAlloc(testing.allocator, 0, 1024);
191 const buf1 = try file.readToEndAlloc(testing.allocator, 1024);
192192 defer testing.allocator.free(buf1);
193193 testing.expect(buf1.len == 0);
194194
195195 const write_buf: []const u8 = "this is a test.\nthis is a test.\nthis is a test.\nthis is a test.\n";
196196 try file.writeAll(write_buf);
197197 try file.seekTo(0);
198 const file_size = try file.getEndPos();
199198
200199 // max_bytes > file_size
201 const buf2 = try file.readAllAlloc(testing.allocator, file_size, 1024);
200 const buf2 = try file.readToEndAlloc(testing.allocator, 1024);
202201 defer testing.allocator.free(buf2);
203202 testing.expectEqual(write_buf.len, buf2.len);
204203 testing.expect(std.mem.eql(u8, write_buf, buf2));
205204 try file.seekTo(0);
206205
207206 // max_bytes == file_size
208 const buf3 = try file.readAllAlloc(testing.allocator, file_size, write_buf.len);
207 const buf3 = try file.readToEndAlloc(testing.allocator, write_buf.len);
209208 defer testing.allocator.free(buf3);
210209 testing.expectEqual(write_buf.len, buf3.len);
211210 testing.expect(std.mem.eql(u8, write_buf, buf3));
211 try file.seekTo(0);
212212
213213 // max_bytes < file_size
214 testing.expectError(error.FileTooBig, file.readAllAlloc(testing.allocator, file_size, write_buf.len - 1));
214 testing.expectError(error.FileTooBig, file.readToEndAlloc(testing.allocator, write_buf.len - 1));
215215}
216216
217217test "directory operations on files" {
lib/std/hash/auto_hash.zig+1-1
......@@ -113,7 +113,7 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void {
113113 .Array => hashArray(hasher, key, strat),
114114
115115 .Vector => |info| {
116 if (info.child.bit_count % 8 == 0) {
116 if (std.meta.bitCount(info.child) % 8 == 0) {
117117 // If there's no unused bits in the child type, we can just hash
118118 // this as an array of bytes.
119119 hasher.update(mem.asBytes(&key));
lib/std/heap.zig+5-1
......@@ -915,6 +915,10 @@ pub fn testAllocator(base_allocator: *mem.Allocator) !void {
915915 testing.expect(slice.len == 10);
916916
917917 allocator.free(slice);
918
919 const zero_bit_ptr = try allocator.create(u0);
920 zero_bit_ptr.* = 0;
921 allocator.destroy(zero_bit_ptr);
918922}
919923
920924pub fn testAllocatorAligned(base_allocator: *mem.Allocator, comptime alignment: u29) !void {
......@@ -952,7 +956,7 @@ pub fn testAllocatorLargeAlignment(base_allocator: *mem.Allocator) mem.Allocator
952956 // very near usize?
953957 if (mem.page_size << 2 > maxInt(usize)) return;
954958
955 const USizeShift = std.meta.Int(false, std.math.log2(usize.bit_count));
959 const USizeShift = std.meta.Int(false, std.math.log2(std.meta.bitCount(usize)));
956960 const large_align = @as(u29, mem.page_size << 2);
957961
958962 var align_mask: usize = undefined;
lib/std/io/reader.zig+5-5
......@@ -198,28 +198,28 @@ pub fn Reader(
198198
199199 /// Reads a native-endian integer
200200 pub fn readIntNative(self: Self, comptime T: type) !T {
201 const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8);
201 const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8);
202202 return mem.readIntNative(T, &bytes);
203203 }
204204
205205 /// Reads a foreign-endian integer
206206 pub fn readIntForeign(self: Self, comptime T: type) !T {
207 const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8);
207 const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8);
208208 return mem.readIntForeign(T, &bytes);
209209 }
210210
211211 pub fn readIntLittle(self: Self, comptime T: type) !T {
212 const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8);
212 const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8);
213213 return mem.readIntLittle(T, &bytes);
214214 }
215215
216216 pub fn readIntBig(self: Self, comptime T: type) !T {
217 const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8);
217 const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8);
218218 return mem.readIntBig(T, &bytes);
219219 }
220220
221221 pub fn readInt(self: Self, comptime T: type, endian: builtin.Endian) !T {
222 const bytes = try self.readBytesNoEof((T.bit_count + 7) / 8);
222 const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8);
223223 return mem.readInt(T, &bytes, endian);
224224 }
225225
lib/std/io/serialization.zig+3-3
......@@ -60,7 +60,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
6060
6161 const U = std.meta.Int(false, t_bit_count);
6262 const Log2U = math.Log2Int(U);
63 const int_size = (U.bit_count + 7) / 8;
63 const int_size = (t_bit_count + 7) / 8;
6464
6565 if (packing == .Bit) {
6666 const result = try self.in_stream.readBitsNoEof(U, t_bit_count);
......@@ -73,7 +73,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
7373
7474 if (int_size == 1) {
7575 if (t_bit_count == 8) return @bitCast(T, buffer[0]);
76 const PossiblySignedByte = std.meta.Int(T.is_signed, 8);
76 const PossiblySignedByte = std.meta.Int(@typeInfo(T).Int.is_signed, 8);
7777 return @truncate(T, @bitCast(PossiblySignedByte, buffer[0]));
7878 }
7979
......@@ -247,7 +247,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
247247
248248 const U = std.meta.Int(false, t_bit_count);
249249 const Log2U = math.Log2Int(U);
250 const int_size = (U.bit_count + 7) / 8;
250 const int_size = (t_bit_count + 7) / 8;
251251
252252 const u_value = @bitCast(U, value);
253253
lib/std/io/writer.zig+5-5
......@@ -53,7 +53,7 @@ pub fn Writer(
5353 /// Write a native-endian integer.
5454 /// TODO audit non-power-of-two int sizes
5555 pub fn writeIntNative(self: Self, comptime T: type, value: T) Error!void {
56 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
56 var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
5757 mem.writeIntNative(T, &bytes, value);
5858 return self.writeAll(&bytes);
5959 }
......@@ -61,28 +61,28 @@ pub fn Writer(
6161 /// Write a foreign-endian integer.
6262 /// TODO audit non-power-of-two int sizes
6363 pub fn writeIntForeign(self: Self, comptime T: type, value: T) Error!void {
64 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
64 var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
6565 mem.writeIntForeign(T, &bytes, value);
6666 return self.writeAll(&bytes);
6767 }
6868
6969 /// TODO audit non-power-of-two int sizes
7070 pub fn writeIntLittle(self: Self, comptime T: type, value: T) Error!void {
71 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
71 var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
7272 mem.writeIntLittle(T, &bytes, value);
7373 return self.writeAll(&bytes);
7474 }
7575
7676 /// TODO audit non-power-of-two int sizes
7777 pub fn writeIntBig(self: Self, comptime T: type, value: T) Error!void {
78 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
78 var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
7979 mem.writeIntBig(T, &bytes, value);
8080 return self.writeAll(&bytes);
8181 }
8282
8383 /// TODO audit non-power-of-two int sizes
8484 pub fn writeInt(self: Self, comptime T: type, value: T, endian: builtin.Endian) Error!void {
85 var bytes: [(T.bit_count + 7) / 8]u8 = undefined;
85 var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
8686 mem.writeInt(T, &bytes, value, endian);
8787 return self.writeAll(&bytes);
8888 }
lib/std/log.zig+4
......@@ -127,6 +127,10 @@ fn log(
127127 if (@enumToInt(message_level) <= @enumToInt(level)) {
128128 if (@hasDecl(root, "log")) {
129129 root.log(message_level, scope, format, args);
130 } else if (std.Target.current.os.tag == .freestanding) {
131 // On freestanding one must provide a log function; we do not have
132 // any I/O configured.
133 return;
130134 } else if (builtin.mode != .ReleaseSmall) {
131135 const held = std.debug.getStderrMutex().acquire();
132136 defer held.release();
lib/std/math.zig+32-31
......@@ -195,7 +195,7 @@ test "" {
195195pub fn floatMantissaBits(comptime T: type) comptime_int {
196196 assert(@typeInfo(T) == .Float);
197197
198 return switch (T.bit_count) {
198 return switch (@typeInfo(T).Float.bits) {
199199 16 => 10,
200200 32 => 23,
201201 64 => 52,
......@@ -208,7 +208,7 @@ pub fn floatMantissaBits(comptime T: type) comptime_int {
208208pub fn floatExponentBits(comptime T: type) comptime_int {
209209 assert(@typeInfo(T) == .Float);
210210
211 return switch (T.bit_count) {
211 return switch (@typeInfo(T).Float.bits) {
212212 16 => 5,
213213 32 => 8,
214214 64 => 11,
......@@ -347,9 +347,9 @@ pub fn shlExact(comptime T: type, a: T, shift_amt: Log2Int(T)) !T {
347347/// A negative shift amount results in a right shift.
348348pub fn shl(comptime T: type, a: T, shift_amt: anytype) T {
349349 const abs_shift_amt = absCast(shift_amt);
350 const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else @intCast(Log2Int(T), abs_shift_amt);
350 const casted_shift_amt = if (abs_shift_amt >= @typeInfo(T).Int.bits) return 0 else @intCast(Log2Int(T), abs_shift_amt);
351351
352 if (@TypeOf(shift_amt) == comptime_int or @TypeOf(shift_amt).is_signed) {
352 if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.is_signed) {
353353 if (shift_amt < 0) {
354354 return a >> casted_shift_amt;
355355 }
......@@ -373,9 +373,9 @@ test "math.shl" {
373373/// A negative shift amount results in a left shift.
374374pub fn shr(comptime T: type, a: T, shift_amt: anytype) T {
375375 const abs_shift_amt = absCast(shift_amt);
376 const casted_shift_amt = if (abs_shift_amt >= T.bit_count) return 0 else @intCast(Log2Int(T), abs_shift_amt);
376 const casted_shift_amt = if (abs_shift_amt >= @typeInfo(T).Int.bits) return 0 else @intCast(Log2Int(T), abs_shift_amt);
377377
378 if (@TypeOf(shift_amt) == comptime_int or @TypeOf(shift_amt).is_signed) {
378 if (@TypeOf(shift_amt) == comptime_int or @typeInfo(@TypeOf(shift_amt)).Int.is_signed) {
379379 if (shift_amt >= 0) {
380380 return a >> casted_shift_amt;
381381 } else {
......@@ -400,11 +400,11 @@ test "math.shr" {
400400/// Rotates right. Only unsigned values can be rotated.
401401/// Negative shift values results in shift modulo the bit count.
402402pub fn rotr(comptime T: type, x: T, r: anytype) T {
403 if (T.is_signed) {
403 if (@typeInfo(T).Int.is_signed) {
404404 @compileError("cannot rotate signed integer");
405405 } else {
406 const ar = @mod(r, T.bit_count);
407 return shr(T, x, ar) | shl(T, x, T.bit_count - ar);
406 const ar = @mod(r, @typeInfo(T).Int.bits);
407 return shr(T, x, ar) | shl(T, x, @typeInfo(T).Int.bits - ar);
408408 }
409409}
410410
......@@ -419,11 +419,11 @@ test "math.rotr" {
419419/// Rotates left. Only unsigned values can be rotated.
420420/// Negative shift values results in shift modulo the bit count.
421421pub fn rotl(comptime T: type, x: T, r: anytype) T {
422 if (T.is_signed) {
422 if (@typeInfo(T).Int.is_signed) {
423423 @compileError("cannot rotate signed integer");
424424 } else {
425 const ar = @mod(r, T.bit_count);
426 return shl(T, x, ar) | shr(T, x, T.bit_count - ar);
425 const ar = @mod(r, @typeInfo(T).Int.bits);
426 return shl(T, x, ar) | shr(T, x, @typeInfo(T).Int.bits - ar);
427427 }
428428}
429429
......@@ -438,7 +438,7 @@ test "math.rotl" {
438438pub fn Log2Int(comptime T: type) type {
439439 // comptime ceil log2
440440 comptime var count = 0;
441 comptime var s = T.bit_count - 1;
441 comptime var s = @typeInfo(T).Int.bits - 1;
442442 inline while (s != 0) : (s >>= 1) {
443443 count += 1;
444444 }
......@@ -524,7 +524,7 @@ fn testOverflow() void {
524524pub fn absInt(x: anytype) !@TypeOf(x) {
525525 const T = @TypeOf(x);
526526 comptime assert(@typeInfo(T) == .Int); // must pass an integer to absInt
527 comptime assert(T.is_signed); // must pass a signed integer to absInt
527 comptime assert(@typeInfo(T).Int.is_signed); // must pass a signed integer to absInt
528528
529529 if (x == minInt(@TypeOf(x))) {
530530 return error.Overflow;
......@@ -557,7 +557,7 @@ fn testAbsFloat() void {
557557pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T {
558558 @setRuntimeSafety(false);
559559 if (denominator == 0) return error.DivisionByZero;
560 if (@typeInfo(T) == .Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow;
560 if (@typeInfo(T) == .Int and @typeInfo(T).Int.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow;
561561 return @divTrunc(numerator, denominator);
562562}
563563
......@@ -578,7 +578,7 @@ fn testDivTrunc() void {
578578pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T {
579579 @setRuntimeSafety(false);
580580 if (denominator == 0) return error.DivisionByZero;
581 if (@typeInfo(T) == .Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow;
581 if (@typeInfo(T) == .Int and @typeInfo(T).Int.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow;
582582 return @divFloor(numerator, denominator);
583583}
584584
......@@ -652,7 +652,7 @@ fn testDivCeil() void {
652652pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {
653653 @setRuntimeSafety(false);
654654 if (denominator == 0) return error.DivisionByZero;
655 if (@typeInfo(T) == .Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow;
655 if (@typeInfo(T) == .Int and @typeInfo(T).Int.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow;
656656 const result = @divTrunc(numerator, denominator);
657657 if (result * denominator != numerator) return error.UnexpectedRemainder;
658658 return result;
......@@ -757,10 +757,10 @@ test "math.absCast" {
757757
758758/// Returns the negation of the integer parameter.
759759/// Result is a signed integer.
760pub fn negateCast(x: anytype) !std.meta.Int(true, @TypeOf(x).bit_count) {
761 if (@TypeOf(x).is_signed) return negate(x);
760pub fn negateCast(x: anytype) !std.meta.Int(true, std.meta.bitCount(@TypeOf(x))) {
761 if (@typeInfo(@TypeOf(x)).Int.is_signed) return negate(x);
762762
763 const int = std.meta.Int(true, @TypeOf(x).bit_count);
763 const int = std.meta.Int(true, std.meta.bitCount(@TypeOf(x)));
764764 if (x > -minInt(int)) return error.Overflow;
765765
766766 if (x == -minInt(int)) return minInt(int);
......@@ -823,7 +823,7 @@ pub fn floorPowerOfTwo(comptime T: type, value: T) T {
823823 var x = value;
824824
825825 comptime var i = 1;
826 inline while (T.bit_count > i) : (i *= 2) {
826 inline while (@typeInfo(T).Int.bits > i) : (i *= 2) {
827827 x |= (x >> i);
828828 }
829829
......@@ -847,13 +847,13 @@ fn testFloorPowerOfTwo() void {
847847/// Returns the next power of two (if the value is not already a power of two).
848848/// Only unsigned integers can be used. Zero is not an allowed input.
849849/// Result is a type with 1 more bit than the input type.
850pub fn ceilPowerOfTwoPromote(comptime T: type, value: T) std.meta.Int(T.is_signed, T.bit_count + 1) {
850pub fn ceilPowerOfTwoPromote(comptime T: type, value: T) std.meta.Int(@typeInfo(T).Int.is_signed, @typeInfo(T).Int.bits + 1) {
851851 comptime assert(@typeInfo(T) == .Int);
852 comptime assert(!T.is_signed);
852 comptime assert(!@typeInfo(T).Int.is_signed);
853853 assert(value != 0);
854 comptime const PromotedType = std.meta.Int(T.is_signed, T.bit_count + 1);
854 comptime const PromotedType = std.meta.Int(@typeInfo(T).Int.is_signed, @typeInfo(T).Int.bits + 1);
855855 comptime const shiftType = std.math.Log2Int(PromotedType);
856 return @as(PromotedType, 1) << @intCast(shiftType, T.bit_count - @clz(T, value - 1));
856 return @as(PromotedType, 1) << @intCast(shiftType, @typeInfo(T).Int.bits - @clz(T, value - 1));
857857}
858858
859859/// Returns the next power of two (if the value is not already a power of two).
......@@ -861,9 +861,10 @@ pub fn ceilPowerOfTwoPromote(comptime T: type, value: T) std.meta.Int(T.is_signe
861861/// If the value doesn't fit, returns an error.
862862pub fn ceilPowerOfTwo(comptime T: type, value: T) (error{Overflow}!T) {
863863 comptime assert(@typeInfo(T) == .Int);
864 comptime assert(!T.is_signed);
865 comptime const PromotedType = std.meta.Int(T.is_signed, T.bit_count + 1);
866 comptime const overflowBit = @as(PromotedType, 1) << T.bit_count;
864 const info = @typeInfo(T).Int;
865 comptime assert(!info.is_signed);
866 comptime const PromotedType = std.meta.Int(info.is_signed, info.bits + 1);
867 comptime const overflowBit = @as(PromotedType, 1) << info.bits;
867868 var x = ceilPowerOfTwoPromote(T, value);
868869 if (overflowBit & x != 0) {
869870 return error.Overflow;
......@@ -911,7 +912,7 @@ fn testCeilPowerOfTwo() !void {
911912
912913pub fn log2_int(comptime T: type, x: T) Log2Int(T) {
913914 assert(x != 0);
914 return @intCast(Log2Int(T), T.bit_count - 1 - @clz(T, x));
915 return @intCast(Log2Int(T), @typeInfo(T).Int.bits - 1 - @clz(T, x));
915916}
916917
917918pub fn log2_int_ceil(comptime T: type, x: T) Log2Int(T) {
......@@ -1008,8 +1009,8 @@ test "max value type" {
10081009 testing.expect(x == 2147483647);
10091010}
10101011
1011pub fn mulWide(comptime T: type, a: T, b: T) std.meta.Int(T.is_signed, T.bit_count * 2) {
1012 const ResultInt = std.meta.Int(T.is_signed, T.bit_count * 2);
1012pub fn mulWide(comptime T: type, a: T, b: T) std.meta.Int(@typeInfo(T).Int.is_signed, @typeInfo(T).Int.bits * 2) {
1013 const ResultInt = std.meta.Int(@typeInfo(T).Int.is_signed, @typeInfo(T).Int.bits * 2);
10131014 return @as(ResultInt, a) * @as(ResultInt, b);
10141015}
10151016
lib/std/math/big.zig+6-5
......@@ -9,14 +9,15 @@ const assert = std.debug.assert;
99pub const Rational = @import("big/rational.zig").Rational;
1010pub const int = @import("big/int.zig");
1111pub const Limb = usize;
12pub const DoubleLimb = std.meta.IntType(false, 2 * Limb.bit_count);
13pub const SignedDoubleLimb = std.meta.IntType(true, DoubleLimb.bit_count);
12const limb_info = @typeInfo(Limb).Int;
13pub const DoubleLimb = std.meta.IntType(false, 2 * limb_info.bits);
14pub const SignedDoubleLimb = std.meta.IntType(true, 2 * limb_info.bits);
1415pub const Log2Limb = std.math.Log2Int(Limb);
1516
1617comptime {
17 assert(std.math.floorPowerOfTwo(usize, Limb.bit_count) == Limb.bit_count);
18 assert(Limb.bit_count <= 64); // u128 set is unsupported
19 assert(Limb.is_signed == false);
18 assert(std.math.floorPowerOfTwo(usize, limb_info.bits) == limb_info.bits);
19 assert(limb_info.bits <= 64); // u128 set is unsupported
20 assert(limb_info.is_signed == false);
2021}
2122
2223test "" {
lib/std/math/big/int.zig+44-43
......@@ -6,6 +6,7 @@
66const std = @import("../../std.zig");
77const math = std.math;
88const Limb = std.math.big.Limb;
9const limb_bits = @typeInfo(Limb).Int.bits;
910const DoubleLimb = std.math.big.DoubleLimb;
1011const SignedDoubleLimb = std.math.big.SignedDoubleLimb;
1112const Log2Limb = std.math.big.Log2Limb;
......@@ -28,7 +29,7 @@ pub fn calcLimbLen(scalar: anytype) usize {
2829 },
2930 .ComptimeInt => {
3031 const w_value = if (scalar < 0) -scalar else scalar;
31 return @divFloor(math.log2(w_value), Limb.bit_count) + 1;
32 return @divFloor(math.log2(w_value), limb_bits) + 1;
3233 },
3334 else => @compileError("parameter must be a primitive integer type"),
3435 }
......@@ -54,7 +55,7 @@ pub fn calcSetStringLimbsBufferLen(base: u8, string_len: usize) usize {
5455}
5556
5657pub fn calcSetStringLimbCount(base: u8, string_len: usize) usize {
57 return (string_len + (Limb.bit_count / base - 1)) / (Limb.bit_count / base);
58 return (string_len + (limb_bits / base - 1)) / (limb_bits / base);
5859}
5960
6061/// a + b * c + *carry, sets carry to the overflow bits
......@@ -68,7 +69,7 @@ pub fn addMulLimbWithCarry(a: Limb, b: Limb, c: Limb, carry: *Limb) Limb {
6869 // r2 = b * c
6970 const bc = @as(DoubleLimb, math.mulWide(Limb, b, c));
7071 const r2 = @truncate(Limb, bc);
71 const c2 = @truncate(Limb, bc >> Limb.bit_count);
72 const c2 = @truncate(Limb, bc >> limb_bits);
7273
7374 // r1 = r1 + r2
7475 const c3: Limb = @boolToInt(@addWithOverflow(Limb, r1, r2, &r1));
......@@ -181,7 +182,7 @@ pub const Mutable = struct {
181182
182183 switch (@typeInfo(T)) {
183184 .Int => |info| {
184 const UT = if (T.is_signed) std.meta.Int(false, T.bit_count - 1) else T;
185 const UT = if (info.is_signed) std.meta.Int(false, info.bits - 1) else T;
185186
186187 const needed_limbs = @sizeOf(UT) / @sizeOf(Limb);
187188 assert(needed_limbs <= self.limbs.len); // value too big
......@@ -190,7 +191,7 @@ pub const Mutable = struct {
190191
191192 var w_value: UT = if (value < 0) @intCast(UT, -value) else @intCast(UT, value);
192193
193 if (info.bits <= Limb.bit_count) {
194 if (info.bits <= limb_bits) {
194195 self.limbs[0] = @as(Limb, w_value);
195196 self.len += 1;
196197 } else {
......@@ -200,15 +201,15 @@ pub const Mutable = struct {
200201 self.len += 1;
201202
202203 // TODO: shift == 64 at compile-time fails. Fails on u128 limbs.
203 w_value >>= Limb.bit_count / 2;
204 w_value >>= Limb.bit_count / 2;
204 w_value >>= limb_bits / 2;
205 w_value >>= limb_bits / 2;
205206 }
206207 }
207208 },
208209 .ComptimeInt => {
209210 comptime var w_value = if (value < 0) -value else value;
210211
211 const req_limbs = @divFloor(math.log2(w_value), Limb.bit_count) + 1;
212 const req_limbs = @divFloor(math.log2(w_value), limb_bits) + 1;
212213 assert(req_limbs <= self.limbs.len); // value too big
213214
214215 self.len = req_limbs;
......@@ -217,14 +218,14 @@ pub const Mutable = struct {
217218 if (w_value <= maxInt(Limb)) {
218219 self.limbs[0] = w_value;
219220 } else {
220 const mask = (1 << Limb.bit_count) - 1;
221 const mask = (1 << limb_bits) - 1;
221222
222223 comptime var i = 0;
223224 inline while (w_value != 0) : (i += 1) {
224225 self.limbs[i] = w_value & mask;
225226
226 w_value >>= Limb.bit_count / 2;
227 w_value >>= Limb.bit_count / 2;
227 w_value >>= limb_bits / 2;
228 w_value >>= limb_bits / 2;
228229 }
229230 }
230231 },
......@@ -506,7 +507,7 @@ pub const Mutable = struct {
506507 /// `a.limbs.len + (shift / (@sizeOf(Limb) * 8))`.
507508 pub fn shiftLeft(r: *Mutable, a: Const, shift: usize) void {
508509 llshl(r.limbs[0..], a.limbs[0..a.limbs.len], shift);
509 r.normalize(a.limbs.len + (shift / Limb.bit_count) + 1);
510 r.normalize(a.limbs.len + (shift / limb_bits) + 1);
510511 r.positive = a.positive;
511512 }
512513
......@@ -516,7 +517,7 @@ pub const Mutable = struct {
516517 /// Asserts there is enough memory to fit the result. The upper bound Limb count is
517518 /// `a.limbs.len - (shift / (@sizeOf(Limb) * 8))`.
518519 pub fn shiftRight(r: *Mutable, a: Const, shift: usize) void {
519 if (a.limbs.len <= shift / Limb.bit_count) {
520 if (a.limbs.len <= shift / limb_bits) {
520521 r.len = 1;
521522 r.positive = true;
522523 r.limbs[0] = 0;
......@@ -524,7 +525,7 @@ pub const Mutable = struct {
524525 }
525526
526527 const r_len = llshr(r.limbs[0..], a.limbs[0..a.limbs.len], shift);
527 r.len = a.limbs.len - (shift / Limb.bit_count);
528 r.len = a.limbs.len - (shift / limb_bits);
528529 r.positive = a.positive;
529530 }
530531
......@@ -772,7 +773,7 @@ pub const Mutable = struct {
772773 }
773774
774775 if (ab_zero_limb_count != 0) {
775 rem.shiftLeft(rem.toConst(), ab_zero_limb_count * Limb.bit_count);
776 rem.shiftLeft(rem.toConst(), ab_zero_limb_count * limb_bits);
776777 }
777778 }
778779
......@@ -803,10 +804,10 @@ pub const Mutable = struct {
803804 };
804805 tmp.limbs[0] = 0;
805806
806 // Normalize so y > Limb.bit_count / 2 (i.e. leading bit is set) and even
807 // Normalize so y > limb_bits / 2 (i.e. leading bit is set) and even
807808 var norm_shift = @clz(Limb, y.limbs[y.len - 1]);
808809 if (norm_shift == 0 and y.toConst().isOdd()) {
809 norm_shift = Limb.bit_count;
810 norm_shift = limb_bits;
810811 }
811812 x.shiftLeft(x.toConst(), norm_shift);
812813 y.shiftLeft(y.toConst(), norm_shift);
......@@ -820,7 +821,7 @@ pub const Mutable = struct {
820821 mem.set(Limb, q.limbs[0..q.len], 0);
821822
822823 // 2.
823 tmp.shiftLeft(y.toConst(), Limb.bit_count * (n - t));
824 tmp.shiftLeft(y.toConst(), limb_bits * (n - t));
824825 while (x.toConst().order(tmp.toConst()) != .lt) {
825826 q.limbs[n - t] += 1;
826827 x.sub(x.toConst(), tmp.toConst());
......@@ -833,7 +834,7 @@ pub const Mutable = struct {
833834 if (x.limbs[i] == y.limbs[t]) {
834835 q.limbs[i - t - 1] = maxInt(Limb);
835836 } else {
836 const num = (@as(DoubleLimb, x.limbs[i]) << Limb.bit_count) | @as(DoubleLimb, x.limbs[i - 1]);
837 const num = (@as(DoubleLimb, x.limbs[i]) << limb_bits) | @as(DoubleLimb, x.limbs[i - 1]);
837838 const z = @intCast(Limb, num / @as(DoubleLimb, y.limbs[t]));
838839 q.limbs[i - t - 1] = if (z > maxInt(Limb)) maxInt(Limb) else @as(Limb, z);
839840 }
......@@ -862,11 +863,11 @@ pub const Mutable = struct {
862863 // 3.3
863864 tmp.set(q.limbs[i - t - 1]);
864865 tmp.mul(tmp.toConst(), y.toConst(), mul_limb_buf, allocator);
865 tmp.shiftLeft(tmp.toConst(), Limb.bit_count * (i - t - 1));
866 tmp.shiftLeft(tmp.toConst(), limb_bits * (i - t - 1));
866867 x.sub(x.toConst(), tmp.toConst());
867868
868869 if (!x.positive) {
869 tmp.shiftLeft(y.toConst(), Limb.bit_count * (i - t - 1));
870 tmp.shiftLeft(y.toConst(), limb_bits * (i - t - 1));
870871 x.add(x.toConst(), tmp.toConst());
871872 q.limbs[i - t - 1] -= 1;
872873 }
......@@ -949,7 +950,7 @@ pub const Const = struct {
949950
950951 /// Returns the number of bits required to represent the absolute value of an integer.
951952 pub fn bitCountAbs(self: Const) usize {
952 return (self.limbs.len - 1) * Limb.bit_count + (Limb.bit_count - @clz(Limb, self.limbs[self.limbs.len - 1]));
953 return (self.limbs.len - 1) * limb_bits + (limb_bits - @clz(Limb, self.limbs[self.limbs.len - 1]));
953954 }
954955
955956 /// Returns the number of bits required to represent the integer in twos-complement form.
......@@ -1019,10 +1020,10 @@ pub const Const = struct {
10191020 /// Returns an error if self cannot be narrowed into the requested type without truncation.
10201021 pub fn to(self: Const, comptime T: type) ConvertError!T {
10211022 switch (@typeInfo(T)) {
1022 .Int => {
1023 const UT = std.meta.Int(false, T.bit_count);
1023 .Int => |info| {
1024 const UT = std.meta.Int(false, info.bits);
10241025
1025 if (self.bitCountTwosComp() > T.bit_count) {
1026 if (self.bitCountTwosComp() > info.bits) {
10261027 return error.TargetTooSmall;
10271028 }
10281029
......@@ -1033,12 +1034,12 @@ pub const Const = struct {
10331034 } else {
10341035 for (self.limbs[0..self.limbs.len]) |_, ri| {
10351036 const limb = self.limbs[self.limbs.len - ri - 1];
1036 r <<= Limb.bit_count;
1037 r <<= limb_bits;
10371038 r |= limb;
10381039 }
10391040 }
10401041
1041 if (!T.is_signed) {
1042 if (!info.is_signed) {
10421043 return if (self.positive) @intCast(T, r) else error.NegativeIntoUnsigned;
10431044 } else {
10441045 if (self.positive) {
......@@ -1149,7 +1150,7 @@ pub const Const = struct {
11491150
11501151 outer: for (self.limbs[0..self.limbs.len]) |limb| {
11511152 var shift: usize = 0;
1152 while (shift < Limb.bit_count) : (shift += base_shift) {
1153 while (shift < limb_bits) : (shift += base_shift) {
11531154 const r = @intCast(u8, (limb >> @intCast(Log2Limb, shift)) & @as(Limb, base - 1));
11541155 const ch = std.fmt.digitToChar(r, uppercase);
11551156 string[digits_len] = ch;
......@@ -1295,7 +1296,7 @@ pub const Const = struct {
12951296/// Memory is allocated as needed to ensure operations never overflow. The range
12961297/// is bounded only by available memory.
12971298pub const Managed = struct {
1298 pub const sign_bit: usize = 1 << (usize.bit_count - 1);
1299 pub const sign_bit: usize = 1 << (@typeInfo(usize).Int.bits - 1);
12991300
13001301 /// Default number of limbs to allocate on creation of a `Managed`.
13011302 pub const default_capacity = 4;
......@@ -1448,7 +1449,7 @@ pub const Managed = struct {
14481449 for (self.limbs[0..self.len()]) |limb| {
14491450 std.debug.warn("{x} ", .{limb});
14501451 }
1451 std.debug.warn("capacity={} positive={}\n", .{ self.limbs.len, self.positive });
1452 std.debug.warn("capacity={} positive={}\n", .{ self.limbs.len, self.isPositive() });
14521453 }
14531454
14541455 /// Negate the sign.
......@@ -1716,7 +1717,7 @@ pub const Managed = struct {
17161717
17171718 /// r = a << shift, in other words, r = a * 2^shift
17181719 pub fn shiftLeft(r: *Managed, a: Managed, shift: usize) !void {
1719 try r.ensureCapacity(a.len() + (shift / Limb.bit_count) + 1);
1720 try r.ensureCapacity(a.len() + (shift / limb_bits) + 1);
17201721 var m = r.toMutable();
17211722 m.shiftLeft(a.toConst(), shift);
17221723 r.setMetadata(m.positive, m.len);
......@@ -1724,13 +1725,13 @@ pub const Managed = struct {
17241725
17251726 /// r = a >> shift
17261727 pub fn shiftRight(r: *Managed, a: Managed, shift: usize) !void {
1727 if (a.len() <= shift / Limb.bit_count) {
1728 if (a.len() <= shift / limb_bits) {
17281729 r.metadata = 1;
17291730 r.limbs[0] = 0;
17301731 return;
17311732 }
17321733
1733 try r.ensureCapacity(a.len() - (shift / Limb.bit_count));
1734 try r.ensureCapacity(a.len() - (shift / limb_bits));
17341735 var m = r.toMutable();
17351736 m.shiftRight(a.toConst(), shift);
17361737 r.setMetadata(m.positive, m.len);
......@@ -2021,7 +2022,7 @@ fn lldiv1(quo: []Limb, rem: *Limb, a: []const Limb, b: Limb) void {
20212022 rem.* = 0;
20222023 for (a) |_, ri| {
20232024 const i = a.len - ri - 1;
2024 const pdiv = ((@as(DoubleLimb, rem.*) << Limb.bit_count) | a[i]);
2025 const pdiv = ((@as(DoubleLimb, rem.*) << limb_bits) | a[i]);
20252026
20262027 if (pdiv == 0) {
20272028 quo[i] = 0;
......@@ -2042,10 +2043,10 @@ fn lldiv1(quo: []Limb, rem: *Limb, a: []const Limb, b: Limb) void {
20422043fn llshl(r: []Limb, a: []const Limb, shift: usize) void {
20432044 @setRuntimeSafety(debug_safety);
20442045 assert(a.len >= 1);
2045 assert(r.len >= a.len + (shift / Limb.bit_count) + 1);
2046 assert(r.len >= a.len + (shift / limb_bits) + 1);
20462047
2047 const limb_shift = shift / Limb.bit_count + 1;
2048 const interior_limb_shift = @intCast(Log2Limb, shift % Limb.bit_count);
2048 const limb_shift = shift / limb_bits + 1;
2049 const interior_limb_shift = @intCast(Log2Limb, shift % limb_bits);
20492050
20502051 var carry: Limb = 0;
20512052 var i: usize = 0;
......@@ -2057,7 +2058,7 @@ fn llshl(r: []Limb, a: []const Limb, shift: usize) void {
20572058 r[dst_i] = carry | @call(.{ .modifier = .always_inline }, math.shr, .{
20582059 Limb,
20592060 src_digit,
2060 Limb.bit_count - @intCast(Limb, interior_limb_shift),
2061 limb_bits - @intCast(Limb, interior_limb_shift),
20612062 });
20622063 carry = (src_digit << interior_limb_shift);
20632064 }
......@@ -2069,10 +2070,10 @@ fn llshl(r: []Limb, a: []const Limb, shift: usize) void {
20692070fn llshr(r: []Limb, a: []const Limb, shift: usize) void {
20702071 @setRuntimeSafety(debug_safety);
20712072 assert(a.len >= 1);
2072 assert(r.len >= a.len - (shift / Limb.bit_count));
2073 assert(r.len >= a.len - (shift / limb_bits));
20732074
2074 const limb_shift = shift / Limb.bit_count;
2075 const interior_limb_shift = @intCast(Log2Limb, shift % Limb.bit_count);
2075 const limb_shift = shift / limb_bits;
2076 const interior_limb_shift = @intCast(Log2Limb, shift % limb_bits);
20762077
20772078 var carry: Limb = 0;
20782079 var i: usize = 0;
......@@ -2085,7 +2086,7 @@ fn llshr(r: []Limb, a: []const Limb, shift: usize) void {
20852086 carry = @call(.{ .modifier = .always_inline }, math.shl, .{
20862087 Limb,
20872088 src_digit,
2088 Limb.bit_count - @intCast(Limb, interior_limb_shift),
2089 limb_bits - @intCast(Limb, interior_limb_shift),
20892090 });
20902091 }
20912092}
......@@ -2135,7 +2136,7 @@ fn fixedIntFromSignedDoubleLimb(A: SignedDoubleLimb, storage: []Limb) Mutable {
21352136 const A_is_positive = A >= 0;
21362137 const Au = @intCast(DoubleLimb, if (A < 0) -A else A);
21372138 storage[0] = @truncate(Limb, Au);
2138 storage[1] = @truncate(Limb, Au >> Limb.bit_count);
2139 storage[1] = @truncate(Limb, Au >> limb_bits);
21392140 return .{
21402141 .limbs = storage[0..2],
21412142 .positive = A_is_positive,
lib/std/math/big/int_test.zig+3-3
......@@ -23,13 +23,13 @@ test "big.int comptime_int set" {
2323 var a = try Managed.initSet(testing.allocator, s);
2424 defer a.deinit();
2525
26 const s_limb_count = 128 / Limb.bit_count;
26 const s_limb_count = 128 / @typeInfo(Limb).Int.bits;
2727
2828 comptime var i: usize = 0;
2929 inline while (i < s_limb_count) : (i += 1) {
3030 const result = @as(Limb, s & maxInt(Limb));
31 s >>= Limb.bit_count / 2;
32 s >>= Limb.bit_count / 2;
31 s >>= @typeInfo(Limb).Int.bits / 2;
32 s >>= @typeInfo(Limb).Int.bits / 2;
3333 testing.expect(a.limbs[i] == result);
3434 }
3535}
lib/std/math/big/rational.zig+9-7
......@@ -136,7 +136,7 @@ pub const Rational = struct {
136136 // Translated from golang.go/src/math/big/rat.go.
137137 debug.assert(@typeInfo(T) == .Float);
138138
139 const UnsignedInt = std.meta.Int(false, T.bit_count);
139 const UnsignedInt = std.meta.Int(false, @typeInfo(T).Float.bits);
140140 const f_bits = @bitCast(UnsignedInt, f);
141141
142142 const exponent_bits = math.floatExponentBits(T);
......@@ -194,8 +194,8 @@ pub const Rational = struct {
194194 // TODO: Indicate whether the result is not exact.
195195 debug.assert(@typeInfo(T) == .Float);
196196
197 const fsize = T.bit_count;
198 const BitReprType = std.meta.Int(false, T.bit_count);
197 const fsize = @typeInfo(T).Float.bits;
198 const BitReprType = std.meta.Int(false, fsize);
199199
200200 const msize = math.floatMantissaBits(T);
201201 const msize1 = msize + 1;
......@@ -475,16 +475,18 @@ pub const Rational = struct {
475475fn extractLowBits(a: Int, comptime T: type) T {
476476 testing.expect(@typeInfo(T) == .Int);
477477
478 if (T.bit_count <= Limb.bit_count) {
478 const t_bits = @typeInfo(T).Int.bits;
479 const limb_bits = @typeInfo(Limb).Int.bits;
480 if (t_bits <= limb_bits) {
479481 return @truncate(T, a.limbs[0]);
480482 } else {
481483 var r: T = 0;
482484 comptime var i: usize = 0;
483485
484 // Remainder is always 0 since if T.bit_count >= Limb.bit_count -> Limb | T and both
486 // Remainder is always 0 since if t_bits >= limb_bits -> Limb | T and both
485487 // are powers of two.
486 inline while (i < T.bit_count / Limb.bit_count) : (i += 1) {
487 r |= math.shl(T, a.limbs[i], i * Limb.bit_count);
488 inline while (i < t_bits / limb_bits) : (i += 1) {
489 r |= math.shl(T, a.limbs[i], i * limb_bits);
488490 }
489491
490492 return r;
lib/std/math/cos.zig+1-1
......@@ -49,7 +49,7 @@ const pi4c = 2.69515142907905952645E-15;
4949const m4pi = 1.273239544735162542821171882678754627704620361328125;
5050
5151fn cos_(comptime T: type, x_: T) T {
52 const I = std.meta.Int(true, T.bit_count);
52 const I = std.meta.Int(true, @typeInfo(T).Float.bits);
5353
5454 var x = x_;
5555 if (math.isNan(x) or math.isInf(x)) {
lib/std/math/pow.zig+2-2
......@@ -128,7 +128,7 @@ pub fn pow(comptime T: type, x: T, y: T) T {
128128 if (yf != 0 and x < 0) {
129129 return math.nan(T);
130130 }
131 if (yi >= 1 << (T.bit_count - 1)) {
131 if (yi >= 1 << (@typeInfo(T).Float.bits - 1)) {
132132 return math.exp(y * math.ln(x));
133133 }
134134
......@@ -150,7 +150,7 @@ pub fn pow(comptime T: type, x: T, y: T) T {
150150 var xe = r2.exponent;
151151 var x1 = r2.significand;
152152
153 var i = @floatToInt(std.meta.Int(true, T.bit_count), yi);
153 var i = @floatToInt(std.meta.Int(true, @typeInfo(T).Float.bits), yi);
154154 while (i != 0) : (i >>= 1) {
155155 const overflow_shift = math.floatExponentBits(T) + 1;
156156 if (xe < -(1 << overflow_shift) or (1 << overflow_shift) < xe) {
lib/std/math/sin.zig+1-1
......@@ -50,7 +50,7 @@ const pi4c = 2.69515142907905952645E-15;
5050const m4pi = 1.273239544735162542821171882678754627704620361328125;
5151
5252fn sin_(comptime T: type, x_: T) T {
53 const I = std.meta.Int(true, T.bit_count);
53 const I = std.meta.Int(true, @typeInfo(T).Float.bits);
5454
5555 var x = x_;
5656 if (x == 0 or math.isNan(x)) {
lib/std/math/sqrt.zig+3-3
......@@ -36,10 +36,10 @@ pub fn sqrt(x: anytype) Sqrt(@TypeOf(x)) {
3636 }
3737}
3838
39fn sqrt_int(comptime T: type, value: T) std.meta.Int(false, T.bit_count / 2) {
39fn sqrt_int(comptime T: type, value: T) std.meta.Int(false, @typeInfo(T).Int.bits / 2) {
4040 var op = value;
4141 var res: T = 0;
42 var one: T = 1 << (T.bit_count - 2);
42 var one: T = 1 << (@typeInfo(T).Int.bits - 2);
4343
4444 // "one" starts at the highest power of four <= than the argument.
4545 while (one > op) {
......@@ -55,7 +55,7 @@ fn sqrt_int(comptime T: type, value: T) std.meta.Int(false, T.bit_count / 2) {
5555 one >>= 2;
5656 }
5757
58 const ResultType = std.meta.Int(false, T.bit_count / 2);
58 const ResultType = std.meta.Int(false, @typeInfo(T).Int.bits / 2);
5959 return @intCast(ResultType, res);
6060}
6161
lib/std/math/tan.zig+1-1
......@@ -43,7 +43,7 @@ const pi4c = 2.69515142907905952645E-15;
4343const m4pi = 1.273239544735162542821171882678754627704620361328125;
4444
4545fn tan_(comptime T: type, x_: T) T {
46 const I = std.meta.Int(true, T.bit_count);
46 const I = std.meta.Int(true, @typeInfo(T).Float.bits);
4747
4848 var x = x_;
4949 if (x == 0 or math.isNan(x)) {
lib/std/mem.zig+21-21
......@@ -949,7 +949,7 @@ pub fn readVarInt(comptime ReturnType: type, bytes: []const u8, endian: builtin.
949949/// This function cannot fail and cannot cause undefined behavior.
950950/// Assumes the endianness of memory is native. This means the function can
951951/// simply pointer cast memory.
952pub fn readIntNative(comptime T: type, bytes: *const [@divExact(T.bit_count, 8)]u8) T {
952pub fn readIntNative(comptime T: type, bytes: *const [@divExact(@typeInfo(T).Int.bits, 8)]u8) T {
953953 return @ptrCast(*align(1) const T, bytes).*;
954954}
955955
......@@ -957,7 +957,7 @@ pub fn readIntNative(comptime T: type, bytes: *const [@divExact(T.bit_count, 8)]
957957/// The bit count of T must be evenly divisible by 8.
958958/// This function cannot fail and cannot cause undefined behavior.
959959/// Assumes the endianness of memory is foreign, so it must byte-swap.
960pub fn readIntForeign(comptime T: type, bytes: *const [@divExact(T.bit_count, 8)]u8) T {
960pub fn readIntForeign(comptime T: type, bytes: *const [@divExact(@typeInfo(T).Int.bits, 8)]u8) T {
961961 return @byteSwap(T, readIntNative(T, bytes));
962962}
963963
......@@ -971,18 +971,18 @@ pub const readIntBig = switch (builtin.endian) {
971971 .Big => readIntNative,
972972};
973973
974/// Asserts that bytes.len >= T.bit_count / 8. Reads the integer starting from index 0
974/// Asserts that bytes.len >= @typeInfo(T).Int.bits / 8. Reads the integer starting from index 0
975975/// and ignores extra bytes.
976976/// The bit count of T must be evenly divisible by 8.
977977/// Assumes the endianness of memory is native. This means the function can
978978/// simply pointer cast memory.
979979pub fn readIntSliceNative(comptime T: type, bytes: []const u8) T {
980 const n = @divExact(T.bit_count, 8);
980 const n = @divExact(@typeInfo(T).Int.bits, 8);
981981 assert(bytes.len >= n);
982982 return readIntNative(T, bytes[0..n]);
983983}
984984
985/// Asserts that bytes.len >= T.bit_count / 8. Reads the integer starting from index 0
985/// Asserts that bytes.len >= @typeInfo(T).Int.bits / 8. Reads the integer starting from index 0
986986/// and ignores extra bytes.
987987/// The bit count of T must be evenly divisible by 8.
988988/// Assumes the endianness of memory is foreign, so it must byte-swap.
......@@ -1003,7 +1003,7 @@ pub const readIntSliceBig = switch (builtin.endian) {
10031003/// Reads an integer from memory with bit count specified by T.
10041004/// The bit count of T must be evenly divisible by 8.
10051005/// This function cannot fail and cannot cause undefined behavior.
1006pub fn readInt(comptime T: type, bytes: *const [@divExact(T.bit_count, 8)]u8, endian: builtin.Endian) T {
1006pub fn readInt(comptime T: type, bytes: *const [@divExact(@typeInfo(T).Int.bits, 8)]u8, endian: builtin.Endian) T {
10071007 if (endian == builtin.endian) {
10081008 return readIntNative(T, bytes);
10091009 } else {
......@@ -1011,11 +1011,11 @@ pub fn readInt(comptime T: type, bytes: *const [@divExact(T.bit_count, 8)]u8, en
10111011 }
10121012}
10131013
1014/// Asserts that bytes.len >= T.bit_count / 8. Reads the integer starting from index 0
1014/// Asserts that bytes.len >= @typeInfo(T).Int.bits / 8. Reads the integer starting from index 0
10151015/// and ignores extra bytes.
10161016/// The bit count of T must be evenly divisible by 8.
10171017pub fn readIntSlice(comptime T: type, bytes: []const u8, endian: builtin.Endian) T {
1018 const n = @divExact(T.bit_count, 8);
1018 const n = @divExact(@typeInfo(T).Int.bits, 8);
10191019 assert(bytes.len >= n);
10201020 return readInt(T, bytes[0..n], endian);
10211021}
......@@ -1060,7 +1060,7 @@ test "readIntBig and readIntLittle" {
10601060/// accepts any integer bit width.
10611061/// This function stores in native endian, which means it is implemented as a simple
10621062/// memory store.
1063pub fn writeIntNative(comptime T: type, buf: *[(T.bit_count + 7) / 8]u8, value: T) void {
1063pub fn writeIntNative(comptime T: type, buf: *[(@typeInfo(T).Int.bits + 7) / 8]u8, value: T) void {
10641064 @ptrCast(*align(1) T, buf).* = value;
10651065}
10661066
......@@ -1068,7 +1068,7 @@ pub fn writeIntNative(comptime T: type, buf: *[(T.bit_count + 7) / 8]u8, value:
10681068/// This function always succeeds, has defined behavior for all inputs, but
10691069/// the integer bit width must be divisible by 8.
10701070/// This function stores in foreign endian, which means it does a @byteSwap first.
1071pub fn writeIntForeign(comptime T: type, buf: *[@divExact(T.bit_count, 8)]u8, value: T) void {
1071pub fn writeIntForeign(comptime T: type, buf: *[@divExact(@typeInfo(T).Int.bits, 8)]u8, value: T) void {
10721072 writeIntNative(T, buf, @byteSwap(T, value));
10731073}
10741074
......@@ -1085,7 +1085,7 @@ pub const writeIntBig = switch (builtin.endian) {
10851085/// Writes an integer to memory, storing it in twos-complement.
10861086/// This function always succeeds, has defined behavior for all inputs, but
10871087/// the integer bit width must be divisible by 8.
1088pub fn writeInt(comptime T: type, buffer: *[@divExact(T.bit_count, 8)]u8, value: T, endian: builtin.Endian) void {
1088pub fn writeInt(comptime T: type, buffer: *[@divExact(@typeInfo(T).Int.bits, 8)]u8, value: T, endian: builtin.Endian) void {
10891089 if (endian == builtin.endian) {
10901090 return writeIntNative(T, buffer, value);
10911091 } else {
......@@ -1094,19 +1094,19 @@ pub fn writeInt(comptime T: type, buffer: *[@divExact(T.bit_count, 8)]u8, value:
10941094}
10951095
10961096/// Writes a twos-complement little-endian integer to memory.
1097/// Asserts that buf.len >= T.bit_count / 8.
1097/// Asserts that buf.len >= @typeInfo(T).Int.bits / 8.
10981098/// The bit count of T must be divisible by 8.
10991099/// Any extra bytes in buffer after writing the integer are set to zero. To
11001100/// avoid the branch to check for extra buffer bytes, use writeIntLittle
11011101/// instead.
11021102pub fn writeIntSliceLittle(comptime T: type, buffer: []u8, value: T) void {
1103 assert(buffer.len >= @divExact(T.bit_count, 8));
1103 assert(buffer.len >= @divExact(@typeInfo(T).Int.bits, 8));
11041104
1105 if (T.bit_count == 0)
1105 if (@typeInfo(T).Int.bits == 0)
11061106 return set(u8, buffer, 0);
11071107
11081108 // TODO I want to call writeIntLittle here but comptime eval facilities aren't good enough
1109 const uint = std.meta.Int(false, T.bit_count);
1109 const uint = std.meta.Int(false, @typeInfo(T).Int.bits);
11101110 var bits = @truncate(uint, value);
11111111 for (buffer) |*b| {
11121112 b.* = @truncate(u8, bits);
......@@ -1115,18 +1115,18 @@ pub fn writeIntSliceLittle(comptime T: type, buffer: []u8, value: T) void {
11151115}
11161116
11171117/// Writes a twos-complement big-endian integer to memory.
1118/// Asserts that buffer.len >= T.bit_count / 8.
1118/// Asserts that buffer.len >= @typeInfo(T).Int.bits / 8.
11191119/// The bit count of T must be divisible by 8.
11201120/// Any extra bytes in buffer before writing the integer are set to zero. To
11211121/// avoid the branch to check for extra buffer bytes, use writeIntBig instead.
11221122pub fn writeIntSliceBig(comptime T: type, buffer: []u8, value: T) void {
1123 assert(buffer.len >= @divExact(T.bit_count, 8));
1123 assert(buffer.len >= @divExact(@typeInfo(T).Int.bits, 8));
11241124
1125 if (T.bit_count == 0)
1125 if (@typeInfo(T).Int.bits == 0)
11261126 return set(u8, buffer, 0);
11271127
11281128 // TODO I want to call writeIntBig here but comptime eval facilities aren't good enough
1129 const uint = std.meta.Int(false, T.bit_count);
1129 const uint = std.meta.Int(false, @typeInfo(T).Int.bits);
11301130 var bits = @truncate(uint, value);
11311131 var index: usize = buffer.len;
11321132 while (index != 0) {
......@@ -1147,13 +1147,13 @@ pub const writeIntSliceForeign = switch (builtin.endian) {
11471147};
11481148
11491149/// Writes a twos-complement integer to memory, with the specified endianness.
1150/// Asserts that buf.len >= T.bit_count / 8.
1150/// Asserts that buf.len >= @typeInfo(T).Int.bits / 8.
11511151/// The bit count of T must be evenly divisible by 8.
11521152/// Any extra bytes in buffer not part of the integer are set to zero, with
11531153/// respect to endianness. To avoid the branch to check for extra buffer bytes,
11541154/// use writeInt instead.
11551155pub fn writeIntSlice(comptime T: type, buffer: []u8, value: T, endian: builtin.Endian) void {
1156 comptime assert(T.bit_count % 8 == 0);
1156 comptime assert(@typeInfo(T).Int.bits % 8 == 0);
11571157 return switch (endian) {
11581158 .Little => writeIntSliceLittle(T, buffer, value),
11591159 .Big => writeIntSliceBig(T, buffer, value),
lib/std/mem/Allocator.zig+4-4
......@@ -159,7 +159,7 @@ fn moveBytes(
159159/// Returns a pointer to undefined memory.
160160/// Call `destroy` with the result to free the memory.
161161pub fn create(self: *Allocator, comptime T: type) Error!*T {
162 if (@sizeOf(T) == 0) return &(T{});
162 if (@sizeOf(T) == 0) return @as(*T, undefined);
163163 const slice = try self.allocAdvancedWithRetAddr(T, null, 1, .exact, @returnAddress());
164164 return &slice[0];
165165}
......@@ -167,11 +167,11 @@ pub fn create(self: *Allocator, comptime T: type) Error!*T {
167167/// `ptr` should be the return value of `create`, or otherwise
168168/// have the same address and alignment property.
169169pub fn destroy(self: *Allocator, ptr: anytype) void {
170 const T = @TypeOf(ptr).Child;
170 const info = @typeInfo(@TypeOf(ptr)).Pointer;
171 const T = info.child;
171172 if (@sizeOf(T) == 0) return;
172173 const non_const_ptr = @intToPtr([*]u8, @ptrToInt(ptr));
173 const ptr_align = @typeInfo(@TypeOf(ptr)).Pointer.alignment;
174 _ = self.shrinkBytes(non_const_ptr[0..@sizeOf(T)], ptr_align, 0, 0, @returnAddress());
174 _ = self.shrinkBytes(non_const_ptr[0..@sizeOf(T)], info.alignment, 0, 0, @returnAddress());
175175}
176176
177177/// Allocates an array of `n` items of type `T` and sets all the
lib/std/os.zig+1-1
......@@ -4526,7 +4526,7 @@ pub fn res_mkquery(
45264526 // Make a reasonably unpredictable id
45274527 var ts: timespec = undefined;
45284528 clock_gettime(CLOCK_REALTIME, &ts) catch {};
4529 const UInt = std.meta.Int(false, @TypeOf(ts.tv_nsec).bit_count);
4529 const UInt = std.meta.Int(false, std.meta.bitCount(@TypeOf(ts.tv_nsec)));
45304530 const unsec = @bitCast(UInt, ts.tv_nsec);
45314531 const id = @truncate(u32, unsec + unsec / 65536);
45324532 q[0] = @truncate(u8, id / 256);
lib/std/os/bits/linux.zig+1-1
......@@ -846,7 +846,7 @@ pub const SIG_ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize));
846846pub const SIG_DFL = @intToPtr(?Sigaction.sigaction_fn, 0);
847847pub const SIG_IGN = @intToPtr(?Sigaction.sigaction_fn, 1);
848848
849pub const empty_sigset = [_]u32{0} ** sigset_t.len;
849pub const empty_sigset = [_]u32{0} ** @typeInfo(sigset_t).Array.len;
850850
851851pub const signalfd_siginfo = extern struct {
852852 signo: u32,
lib/std/os/linux.zig+5-3
......@@ -829,17 +829,19 @@ pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigacti
829829 return 0;
830830}
831831
832const usize_bits = @typeInfo(usize).Int.bits;
833
832834pub fn sigaddset(set: *sigset_t, sig: u6) void {
833835 const s = sig - 1;
834836 // shift in musl: s&8*sizeof *set->__bits-1
835 const shift = @intCast(u5, s & (usize.bit_count - 1));
837 const shift = @intCast(u5, s & (usize_bits - 1));
836838 const val = @intCast(u32, 1) << shift;
837 (set.*)[@intCast(usize, s) / usize.bit_count] |= val;
839 (set.*)[@intCast(usize, s) / usize_bits] |= val;
838840}
839841
840842pub fn sigismember(set: *const sigset_t, sig: u6) bool {
841843 const s = sig - 1;
842 return ((set.*)[@intCast(usize, s) / usize.bit_count] & (@intCast(usize, 1) << (s & (usize.bit_count - 1)))) != 0;
844 return ((set.*)[@intCast(usize, s) / usize_bits] & (@intCast(usize, 1) << (s & (usize_bits - 1)))) != 0;
843845}
844846
845847pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
lib/std/os/windows/ws2_32.zig+1-1
......@@ -12,7 +12,7 @@ pub const SOCKET_ERROR = -1;
1212pub const WSADESCRIPTION_LEN = 256;
1313pub const WSASYS_STATUS_LEN = 128;
1414
15pub const WSADATA = if (usize.bit_count == u64.bit_count)
15pub const WSADATA = if (@sizeOf(usize) == @sizeOf(u64))
1616 extern struct {
1717 wVersion: WORD,
1818 wHighVersion: WORD,
lib/std/pdb.zig+1-1
......@@ -636,7 +636,7 @@ const MsfStream = struct {
636636 blocks: []u32 = undefined,
637637 block_size: u32 = undefined,
638638
639 pub const Error = @TypeOf(read).ReturnType.ErrorSet;
639 pub const Error = @typeInfo(@typeInfo(@TypeOf(read)).Fn.return_type.?).ErrorUnion.error_set;
640640
641641 fn init(block_size: u32, file: File, blocks: []u32) MsfStream {
642642 const stream = MsfStream{
lib/std/rand.zig+33-24
......@@ -51,8 +51,9 @@ pub const Random = struct {
5151 /// Returns a random int `i` such that `0 <= i <= maxInt(T)`.
5252 /// `i` is evenly distributed.
5353 pub fn int(r: *Random, comptime T: type) T {
54 const UnsignedT = std.meta.Int(false, T.bit_count);
55 const ByteAlignedT = std.meta.Int(false, @divTrunc(T.bit_count + 7, 8) * 8);
54 const bits = @typeInfo(T).Int.bits;
55 const UnsignedT = std.meta.Int(false, bits);
56 const ByteAlignedT = std.meta.Int(false, @divTrunc(bits + 7, 8) * 8);
5657
5758 var rand_bytes: [@sizeOf(ByteAlignedT)]u8 = undefined;
5859 r.bytes(rand_bytes[0..]);
......@@ -68,10 +69,11 @@ pub const Random = struct {
6869 /// Constant-time implementation off `uintLessThan`.
6970 /// The results of this function may be biased.
7071 pub fn uintLessThanBiased(r: *Random, comptime T: type, less_than: T) T {
71 comptime assert(T.is_signed == false);
72 comptime assert(T.bit_count <= 64); // TODO: workaround: LLVM ERROR: Unsupported library call operation!
72 comptime assert(@typeInfo(T).Int.is_signed == false);
73 const bits = @typeInfo(T).Int.bits;
74 comptime assert(bits <= 64); // TODO: workaround: LLVM ERROR: Unsupported library call operation!
7375 assert(0 < less_than);
74 if (T.bit_count <= 32) {
76 if (bits <= 32) {
7577 return @intCast(T, limitRangeBiased(u32, r.int(u32), less_than));
7678 } else {
7779 return @intCast(T, limitRangeBiased(u64, r.int(u64), less_than));
......@@ -87,13 +89,15 @@ pub const Random = struct {
8789 /// this function is guaranteed to return.
8890 /// If you need deterministic runtime bounds, use `uintLessThanBiased`.
8991 pub fn uintLessThan(r: *Random, comptime T: type, less_than: T) T {
90 comptime assert(T.is_signed == false);
91 comptime assert(T.bit_count <= 64); // TODO: workaround: LLVM ERROR: Unsupported library call operation!
92 comptime assert(@typeInfo(T).Int.is_signed == false);
93 const bits = @typeInfo(T).Int.bits;
94 comptime assert(bits <= 64); // TODO: workaround: LLVM ERROR: Unsupported library call operation!
9295 assert(0 < less_than);
9396 // Small is typically u32
94 const Small = std.meta.Int(false, @divTrunc(T.bit_count + 31, 32) * 32);
97 const small_bits = @divTrunc(bits + 31, 32) * 32;
98 const Small = std.meta.Int(false, small_bits);
9599 // Large is typically u64
96 const Large = std.meta.Int(false, Small.bit_count * 2);
100 const Large = std.meta.Int(false, small_bits * 2);
97101
98102 // adapted from:
99103 // http://www.pcg-random.org/posts/bounded-rands.html
......@@ -105,7 +109,7 @@ pub const Random = struct {
105109 // TODO: workaround for https://github.com/ziglang/zig/issues/1770
106110 // should be:
107111 // var t: Small = -%less_than;
108 var t: Small = @bitCast(Small, -%@bitCast(std.meta.Int(true, Small.bit_count), @as(Small, less_than)));
112 var t: Small = @bitCast(Small, -%@bitCast(std.meta.Int(true, small_bits), @as(Small, less_than)));
109113
110114 if (t >= less_than) {
111115 t -= less_than;
......@@ -119,13 +123,13 @@ pub const Random = struct {
119123 l = @truncate(Small, m);
120124 }
121125 }
122 return @intCast(T, m >> Small.bit_count);
126 return @intCast(T, m >> small_bits);
123127 }
124128
125129 /// Constant-time implementation off `uintAtMost`.
126130 /// The results of this function may be biased.
127131 pub fn uintAtMostBiased(r: *Random, comptime T: type, at_most: T) T {
128 assert(T.is_signed == false);
132 assert(@typeInfo(T).Int.is_signed == false);
129133 if (at_most == maxInt(T)) {
130134 // have the full range
131135 return r.int(T);
......@@ -137,7 +141,7 @@ pub const Random = struct {
137141 /// See `uintLessThan`, which this function uses in most cases,
138142 /// for commentary on the runtime of this function.
139143 pub fn uintAtMost(r: *Random, comptime T: type, at_most: T) T {
140 assert(T.is_signed == false);
144 assert(@typeInfo(T).Int.is_signed == false);
141145 if (at_most == maxInt(T)) {
142146 // have the full range
143147 return r.int(T);
......@@ -149,9 +153,10 @@ pub const Random = struct {
149153 /// The results of this function may be biased.
150154 pub fn intRangeLessThanBiased(r: *Random, comptime T: type, at_least: T, less_than: T) T {
151155 assert(at_least < less_than);
152 if (T.is_signed) {
156 const info = @typeInfo(T).Int;
157 if (info.is_signed) {
153158 // Two's complement makes this math pretty easy.
154 const UnsignedT = std.meta.Int(false, T.bit_count);
159 const UnsignedT = std.meta.Int(false, info.bits);
155160 const lo = @bitCast(UnsignedT, at_least);
156161 const hi = @bitCast(UnsignedT, less_than);
157162 const result = lo +% r.uintLessThanBiased(UnsignedT, hi -% lo);
......@@ -167,9 +172,10 @@ pub const Random = struct {
167172 /// for commentary on the runtime of this function.
168173 pub fn intRangeLessThan(r: *Random, comptime T: type, at_least: T, less_than: T) T {
169174 assert(at_least < less_than);
170 if (T.is_signed) {
175 const info = @typeInfo(T).Int;
176 if (info.is_signed) {
171177 // Two's complement makes this math pretty easy.
172 const UnsignedT = std.meta.Int(false, T.bit_count);
178 const UnsignedT = std.meta.Int(false, info.bits);
173179 const lo = @bitCast(UnsignedT, at_least);
174180 const hi = @bitCast(UnsignedT, less_than);
175181 const result = lo +% r.uintLessThan(UnsignedT, hi -% lo);
......@@ -184,9 +190,10 @@ pub const Random = struct {
184190 /// The results of this function may be biased.
185191 pub fn intRangeAtMostBiased(r: *Random, comptime T: type, at_least: T, at_most: T) T {
186192 assert(at_least <= at_most);
187 if (T.is_signed) {
193 const info = @typeInfo(T).Int;
194 if (info.is_signed) {
188195 // Two's complement makes this math pretty easy.
189 const UnsignedT = std.meta.Int(false, T.bit_count);
196 const UnsignedT = std.meta.Int(false, info.bits);
190197 const lo = @bitCast(UnsignedT, at_least);
191198 const hi = @bitCast(UnsignedT, at_most);
192199 const result = lo +% r.uintAtMostBiased(UnsignedT, hi -% lo);
......@@ -202,9 +209,10 @@ pub const Random = struct {
202209 /// for commentary on the runtime of this function.
203210 pub fn intRangeAtMost(r: *Random, comptime T: type, at_least: T, at_most: T) T {
204211 assert(at_least <= at_most);
205 if (T.is_signed) {
212 const info = @typeInfo(T).Int;
213 if (info.is_signed) {
206214 // Two's complement makes this math pretty easy.
207 const UnsignedT = std.meta.Int(false, T.bit_count);
215 const UnsignedT = std.meta.Int(false, info.bits);
208216 const lo = @bitCast(UnsignedT, at_least);
209217 const hi = @bitCast(UnsignedT, at_most);
210218 const result = lo +% r.uintAtMost(UnsignedT, hi -% lo);
......@@ -280,14 +288,15 @@ pub const Random = struct {
280288/// into an integer 0 <= result < less_than.
281289/// This function introduces a minor bias.
282290pub fn limitRangeBiased(comptime T: type, random_int: T, less_than: T) T {
283 comptime assert(T.is_signed == false);
284 const T2 = std.meta.Int(false, T.bit_count * 2);
291 comptime assert(@typeInfo(T).Int.is_signed == false);
292 const bits = @typeInfo(T).Int.bits;
293 const T2 = std.meta.Int(false, bits * 2);
285294
286295 // adapted from:
287296 // http://www.pcg-random.org/posts/bounded-rands.html
288297 // "Integer Multiplication (Biased)"
289298 var m: T2 = @as(T2, random_int) * @as(T2, less_than);
290 return @intCast(T, m >> T.bit_count);
299 return @intCast(T, m >> bits);
291300}
292301
293302const SequentialPrng = struct {
lib/std/special/build_runner.zig+1-1
......@@ -133,7 +133,7 @@ pub fn main() !void {
133133}
134134
135135fn runBuild(builder: *Builder) anyerror!void {
136 switch (@typeInfo(@TypeOf(root.build).ReturnType)) {
136 switch (@typeInfo(@typeInfo(@TypeOf(root.build)).Fn.return_type.?)) {
137137 .Void => root.build(builder),
138138 .ErrorUnion => try root.build(builder),
139139 else => @compileError("expected return type of build to be 'void' or '!void'"),
lib/std/special/c.zig+3-2
......@@ -516,11 +516,12 @@ export fn roundf(a: f32) f32 {
516516fn generic_fmod(comptime T: type, x: T, y: T) T {
517517 @setRuntimeSafety(false);
518518
519 const uint = std.meta.Int(false, T.bit_count);
519 const bits = @typeInfo(T).Float.bits;
520 const uint = std.meta.Int(false, bits);
520521 const log2uint = math.Log2Int(uint);
521522 const digits = if (T == f32) 23 else 52;
522523 const exp_bits = if (T == f32) 9 else 12;
523 const bits_minus_1 = T.bit_count - 1;
524 const bits_minus_1 = bits - 1;
524525 const mask = if (T == f32) 0xff else 0x7ff;
525526 var ux = @bitCast(uint, x);
526527 var uy = @bitCast(uint, y);
lib/std/special/compiler_rt/addXf3.zig+10-8
......@@ -59,23 +59,25 @@ pub fn __aeabi_dsub(a: f64, b: f64) callconv(.AAPCS) f64 {
5959}
6060
6161// TODO: restore inline keyword, see: https://github.com/ziglang/zig/issues/2154
62fn normalize(comptime T: type, significand: *std.meta.Int(false, T.bit_count)) i32 {
63 const Z = std.meta.Int(false, T.bit_count);
64 const S = std.meta.Int(false, T.bit_count - @clz(Z, @as(Z, T.bit_count) - 1));
62fn normalize(comptime T: type, significand: *std.meta.Int(false, @typeInfo(T).Float.bits)) i32 {
63 const bits = @typeInfo(T).Float.bits;
64 const Z = std.meta.Int(false, bits);
65 const S = std.meta.Int(false, bits - @clz(Z, @as(Z, bits) - 1));
6566 const significandBits = std.math.floatMantissaBits(T);
6667 const implicitBit = @as(Z, 1) << significandBits;
6768
68 const shift = @clz(std.meta.Int(false, T.bit_count), significand.*) - @clz(Z, implicitBit);
69 const shift = @clz(std.meta.Int(false, bits), significand.*) - @clz(Z, implicitBit);
6970 significand.* <<= @intCast(S, shift);
7071 return 1 - shift;
7172}
7273
7374// TODO: restore inline keyword, see: https://github.com/ziglang/zig/issues/2154
7475fn addXf3(comptime T: type, a: T, b: T) T {
75 const Z = std.meta.Int(false, T.bit_count);
76 const S = std.meta.Int(false, T.bit_count - @clz(Z, @as(Z, T.bit_count) - 1));
76 const bits = @typeInfo(T).Float.bits;
77 const Z = std.meta.Int(false, bits);
78 const S = std.meta.Int(false, bits - @clz(Z, @as(Z, bits) - 1));
7779
78 const typeWidth = T.bit_count;
80 const typeWidth = bits;
7981 const significandBits = std.math.floatMantissaBits(T);
8082 const exponentBits = std.math.floatExponentBits(T);
8183
......@@ -187,7 +189,7 @@ fn addXf3(comptime T: type, a: T, b: T) T {
187189 // If partial cancellation occured, we need to left-shift the result
188190 // and adjust the exponent:
189191 if (aSignificand < implicitBit << 3) {
190 const shift = @intCast(i32, @clz(Z, aSignificand)) - @intCast(i32, @clz(std.meta.Int(false, T.bit_count), implicitBit << 3));
192 const shift = @intCast(i32, @clz(Z, aSignificand)) - @intCast(i32, @clz(std.meta.Int(false, bits), implicitBit << 3));
191193 aSignificand <<= @intCast(S, shift);
192194 aExponent -= shift;
193195 }
lib/std/special/compiler_rt/aulldiv.zig+2-2
......@@ -7,8 +7,8 @@ const builtin = @import("builtin");
77
88pub fn _alldiv(a: i64, b: i64) callconv(.Stdcall) i64 {
99 @setRuntimeSafety(builtin.is_test);
10 const s_a = a >> (i64.bit_count - 1);
11 const s_b = b >> (i64.bit_count - 1);
10 const s_a = a >> (64 - 1);
11 const s_b = b >> (64 - 1);
1212
1313 const an = (a ^ s_a) -% s_a;
1414 const bn = (b ^ s_b) -% s_b;
lib/std/special/compiler_rt/aullrem.zig+2-2
......@@ -7,8 +7,8 @@ const builtin = @import("builtin");
77
88pub fn _allrem(a: i64, b: i64) callconv(.Stdcall) i64 {
99 @setRuntimeSafety(builtin.is_test);
10 const s_a = a >> (i64.bit_count - 1);
11 const s_b = b >> (i64.bit_count - 1);
10 const s_a = a >> (64 - 1);
11 const s_b = b >> (64 - 1);
1212
1313 const an = (a ^ s_a) -% s_a;
1414 const bn = (b ^ s_b) -% s_b;
lib/std/special/compiler_rt/compareXf2.zig+4-3
......@@ -27,8 +27,9 @@ const GE = extern enum(i32) {
2727pub fn cmp(comptime T: type, comptime RT: type, a: T, b: T) RT {
2828 @setRuntimeSafety(builtin.is_test);
2929
30 const srep_t = std.meta.Int(true, T.bit_count);
31 const rep_t = std.meta.Int(false, T.bit_count);
30 const bits = @typeInfo(T).Float.bits;
31 const srep_t = std.meta.Int(true, bits);
32 const rep_t = std.meta.Int(false, bits);
3233
3334 const significandBits = std.math.floatMantissaBits(T);
3435 const exponentBits = std.math.floatExponentBits(T);
......@@ -73,7 +74,7 @@ pub fn cmp(comptime T: type, comptime RT: type, a: T, b: T) RT {
7374pub fn unordcmp(comptime T: type, a: T, b: T) i32 {
7475 @setRuntimeSafety(builtin.is_test);
7576
76 const rep_t = std.meta.Int(false, T.bit_count);
77 const rep_t = std.meta.Int(false, @typeInfo(T).Float.bits);
7778
7879 const significandBits = std.math.floatMantissaBits(T);
7980 const exponentBits = std.math.floatExponentBits(T);
lib/std/special/compiler_rt/divdf3.zig+4-5
......@@ -12,10 +12,9 @@ const builtin = @import("builtin");
1212
1313pub fn __divdf3(a: f64, b: f64) callconv(.C) f64 {
1414 @setRuntimeSafety(builtin.is_test);
15 const Z = std.meta.Int(false, f64.bit_count);
16 const SignedZ = std.meta.Int(true, f64.bit_count);
15 const Z = std.meta.Int(false, 64);
16 const SignedZ = std.meta.Int(true, 64);
1717
18 const typeWidth = f64.bit_count;
1918 const significandBits = std.math.floatMantissaBits(f64);
2019 const exponentBits = std.math.floatExponentBits(f64);
2120
......@@ -317,9 +316,9 @@ pub fn wideMultiply(comptime Z: type, a: Z, b: Z, hi: *Z, lo: *Z) void {
317316 }
318317}
319318
320pub fn normalize(comptime T: type, significand: *std.meta.Int(false, T.bit_count)) i32 {
319pub fn normalize(comptime T: type, significand: *std.meta.Int(false, @typeInfo(T).Float.bits)) i32 {
321320 @setRuntimeSafety(builtin.is_test);
322 const Z = std.meta.Int(false, T.bit_count);
321 const Z = std.meta.Int(false, @typeInfo(T).Float.bits);
323322 const significandBits = std.math.floatMantissaBits(T);
324323 const implicitBit = @as(Z, 1) << significandBits;
325324
lib/std/special/compiler_rt/divsf3.zig+3-4
......@@ -12,9 +12,8 @@ const builtin = @import("builtin");
1212
1313pub fn __divsf3(a: f32, b: f32) callconv(.C) f32 {
1414 @setRuntimeSafety(builtin.is_test);
15 const Z = std.meta.Int(false, f32.bit_count);
15 const Z = std.meta.Int(false, 32);
1616
17 const typeWidth = f32.bit_count;
1817 const significandBits = std.math.floatMantissaBits(f32);
1918 const exponentBits = std.math.floatExponentBits(f32);
2019
......@@ -190,9 +189,9 @@ pub fn __divsf3(a: f32, b: f32) callconv(.C) f32 {
190189 }
191190}
192191
193fn normalize(comptime T: type, significand: *std.meta.Int(false, T.bit_count)) i32 {
192fn normalize(comptime T: type, significand: *std.meta.Int(false, @typeInfo(T).Float.bits)) i32 {
194193 @setRuntimeSafety(builtin.is_test);
195 const Z = std.meta.Int(false, T.bit_count);
194 const Z = std.meta.Int(false, @typeInfo(T).Float.bits);
196195 const significandBits = std.math.floatMantissaBits(T);
197196 const implicitBit = @as(Z, 1) << significandBits;
198197
lib/std/special/compiler_rt/divtf3.zig+2-3
......@@ -11,10 +11,9 @@ const wideMultiply = @import("divdf3.zig").wideMultiply;
1111
1212pub fn __divtf3(a: f128, b: f128) callconv(.C) f128 {
1313 @setRuntimeSafety(builtin.is_test);
14 const Z = std.meta.Int(false, f128.bit_count);
15 const SignedZ = std.meta.Int(true, f128.bit_count);
14 const Z = std.meta.Int(false, 128);
15 const SignedZ = std.meta.Int(true, 128);
1616
17 const typeWidth = f128.bit_count;
1817 const significandBits = std.math.floatMantissaBits(f128);
1918 const exponentBits = std.math.floatExponentBits(f128);
2019
lib/std/special/compiler_rt/divti3.zig+2-2
......@@ -9,8 +9,8 @@ const builtin = @import("builtin");
99pub fn __divti3(a: i128, b: i128) callconv(.C) i128 {
1010 @setRuntimeSafety(builtin.is_test);
1111
12 const s_a = a >> (i128.bit_count - 1);
13 const s_b = b >> (i128.bit_count - 1);
12 const s_a = a >> (128 - 1);
13 const s_b = b >> (128 - 1);
1414
1515 const an = (a ^ s_a) -% s_a;
1616 const bn = (b ^ s_b) -% s_b;
lib/std/special/compiler_rt/fixint.zig+5-4
......@@ -28,7 +28,7 @@ pub fn fixint(comptime fp_t: type, comptime fixint_t: type, a: fp_t) fixint_t {
2828 else => unreachable,
2929 };
3030
31 const typeWidth = rep_t.bit_count;
31 const typeWidth = @typeInfo(rep_t).Int.bits;
3232 const exponentBits = (typeWidth - significandBits - 1);
3333 const signBit = (@as(rep_t, 1) << (significandBits + exponentBits));
3434 const maxExponent = ((1 << exponentBits) - 1);
......@@ -50,12 +50,13 @@ pub fn fixint(comptime fp_t: type, comptime fixint_t: type, a: fp_t) fixint_t {
5050 if (exponent < 0) return 0;
5151
5252 // The unsigned result needs to be large enough to handle an fixint_t or rep_t
53 const fixuint_t = std.meta.Int(false, fixint_t.bit_count);
54 const UintResultType = if (fixint_t.bit_count > rep_t.bit_count) fixuint_t else rep_t;
53 const fixint_bits = @typeInfo(fixint_t).Int.bits;
54 const fixuint_t = std.meta.Int(false, fixint_bits);
55 const UintResultType = if (fixint_bits > typeWidth) fixuint_t else rep_t;
5556 var uint_result: UintResultType = undefined;
5657
5758 // If the value is too large for the integer type, saturate.
58 if (@intCast(usize, exponent) >= fixint_t.bit_count) {
59 if (@intCast(usize, exponent) >= fixint_bits) {
5960 return if (negative) @as(fixint_t, minInt(fixint_t)) else @as(fixint_t, maxInt(fixint_t));
6061 }
6162
lib/std/special/compiler_rt/fixuint.zig+3-3
......@@ -15,14 +15,14 @@ pub fn fixuint(comptime fp_t: type, comptime fixuint_t: type, a: fp_t) fixuint_t
1515 f128 => u128,
1616 else => unreachable,
1717 };
18 const srep_t = @import("std").meta.Int(true, rep_t.bit_count);
18 const typeWidth = @typeInfo(rep_t).Int.bits;
19 const srep_t = @import("std").meta.Int(true, typeWidth);
1920 const significandBits = switch (fp_t) {
2021 f32 => 23,
2122 f64 => 52,
2223 f128 => 112,
2324 else => unreachable,
2425 };
25 const typeWidth = rep_t.bit_count;
2626 const exponentBits = (typeWidth - significandBits - 1);
2727 const signBit = (@as(rep_t, 1) << (significandBits + exponentBits));
2828 const maxExponent = ((1 << exponentBits) - 1);
......@@ -44,7 +44,7 @@ pub fn fixuint(comptime fp_t: type, comptime fixuint_t: type, a: fp_t) fixuint_t
4444 if (sign == -1 or exponent < 0) return 0;
4545
4646 // If the value is too large for the integer type, saturate.
47 if (@intCast(c_uint, exponent) >= fixuint_t.bit_count) return ~@as(fixuint_t, 0);
47 if (@intCast(c_uint, exponent) >= @typeInfo(fixuint_t).Int.bits) return ~@as(fixuint_t, 0);
4848
4949 // If 0 <= exponent < significandBits, right shift to get the result.
5050 // Otherwise, shift left.
lib/std/special/compiler_rt/floatXisf.zig+5-4
......@@ -12,15 +12,16 @@ const FLT_MANT_DIG = 24;
1212fn __floatXisf(comptime T: type, arg: T) f32 {
1313 @setRuntimeSafety(builtin.is_test);
1414
15 const Z = std.meta.Int(false, T.bit_count);
16 const S = std.meta.Int(false, T.bit_count - @clz(Z, @as(Z, T.bit_count) - 1));
15 const bits = @typeInfo(T).Int.bits;
16 const Z = std.meta.Int(false, bits);
17 const S = std.meta.Int(false, bits - @clz(Z, @as(Z, bits) - 1));
1718
1819 if (arg == 0) {
1920 return @as(f32, 0.0);
2021 }
2122
2223 var ai = arg;
23 const N: u32 = T.bit_count;
24 const N: u32 = bits;
2425 const si = ai >> @intCast(S, (N - 1));
2526 ai = ((ai ^ si) -% si);
2627 var a = @bitCast(Z, ai);
......@@ -66,7 +67,7 @@ fn __floatXisf(comptime T: type, arg: T) f32 {
6667 // a is now rounded to FLT_MANT_DIG bits
6768 }
6869
69 const s = @bitCast(Z, arg) >> (T.bit_count - 32);
70 const s = @bitCast(Z, arg) >> (@typeInfo(T).Int.bits - 32);
7071 const r = (@intCast(u32, s) & 0x80000000) | // sign
7172 (@intCast(u32, (e + 127)) << 23) | // exponent
7273 (@truncate(u32, a) & 0x007fffff); // mantissa-high
lib/std/special/compiler_rt/floatsiXf.zig+4-3
......@@ -10,8 +10,9 @@ const maxInt = std.math.maxInt;
1010fn floatsiXf(comptime T: type, a: i32) T {
1111 @setRuntimeSafety(builtin.is_test);
1212
13 const Z = std.meta.Int(false, T.bit_count);
14 const S = std.meta.Int(false, T.bit_count - @clz(Z, @as(Z, T.bit_count) - 1));
13 const bits = @typeInfo(T).Float.bits;
14 const Z = std.meta.Int(false, bits);
15 const S = std.meta.Int(false, bits - @clz(Z, @as(Z, bits) - 1));
1516
1617 if (a == 0) {
1718 return @as(T, 0.0);
......@@ -22,7 +23,7 @@ fn floatsiXf(comptime T: type, a: i32) T {
2223 const exponentBias = ((1 << exponentBits - 1) - 1);
2324
2425 const implicitBit = @as(Z, 1) << significandBits;
25 const signBit = @as(Z, 1 << Z.bit_count - 1);
26 const signBit = @as(Z, 1 << bits - 1);
2627
2728 const sign = a >> 31;
2829 // Take absolute value of a via abs(x) = (x^(x >> 31)) - (x >> 31).
lib/std/special/compiler_rt/floatundisf.zig+1-1
......@@ -15,7 +15,7 @@ pub fn __floatundisf(arg: u64) callconv(.C) f32 {
1515 if (arg == 0) return 0;
1616
1717 var a = arg;
18 const N: usize = @TypeOf(a).bit_count;
18 const N: usize = @typeInfo(@TypeOf(a)).Int.bits;
1919 // Number of significant digits
2020 const sd = N - @clz(u64, a);
2121 // 8 exponent
lib/std/special/compiler_rt/floatunditf.zig+1-1
......@@ -19,7 +19,7 @@ pub fn __floatunditf(a: u64) callconv(.C) f128 {
1919 const exponent_bias = (1 << (exponent_bits - 1)) - 1;
2020 const implicit_bit = 1 << mantissa_bits;
2121
22 const exp: u128 = (u64.bit_count - 1) - @clz(u64, a);
22 const exp: u128 = (64 - 1) - @clz(u64, a);
2323 const shift: u7 = mantissa_bits - @intCast(u7, exp);
2424
2525 var result: u128 = (@intCast(u128, a) << shift) ^ implicit_bit;
lib/std/special/compiler_rt/floatunsitf.zig+1-1
......@@ -19,7 +19,7 @@ pub fn __floatunsitf(a: u64) callconv(.C) f128 {
1919 const exponent_bias = (1 << (exponent_bits - 1)) - 1;
2020 const implicit_bit = 1 << mantissa_bits;
2121
22 const exp = (u64.bit_count - 1) - @clz(u64, a);
22 const exp = (64 - 1) - @clz(u64, a);
2323 const shift = mantissa_bits - @intCast(u7, exp);
2424
2525 // TODO(#1148): @bitCast alignment error
lib/std/special/compiler_rt/int.zig+1-1
......@@ -219,7 +219,7 @@ fn test_one_divsi3(a: i32, b: i32, expected_q: i32) void {
219219pub fn __udivsi3(n: u32, d: u32) callconv(.C) u32 {
220220 @setRuntimeSafety(builtin.is_test);
221221
222 const n_uword_bits: c_uint = u32.bit_count;
222 const n_uword_bits: c_uint = 32;
223223 // special cases
224224 if (d == 0) return 0; // ?!
225225 if (n == 0) return 0;
lib/std/special/compiler_rt/modti3.zig+2-2
......@@ -14,8 +14,8 @@ const compiler_rt = @import("../compiler_rt.zig");
1414pub fn __modti3(a: i128, b: i128) callconv(.C) i128 {
1515 @setRuntimeSafety(builtin.is_test);
1616
17 const s_a = a >> (i128.bit_count - 1); // s = a < 0 ? -1 : 0
18 const s_b = b >> (i128.bit_count - 1); // s = b < 0 ? -1 : 0
17 const s_a = a >> (128 - 1); // s = a < 0 ? -1 : 0
18 const s_b = b >> (128 - 1); // s = b < 0 ? -1 : 0
1919
2020 const an = (a ^ s_a) -% s_a; // negate if s == -1
2121 const bn = (b ^ s_b) -% s_b; // negate if s == -1
lib/std/special/compiler_rt/mulXf3.zig+5-5
......@@ -33,9 +33,9 @@ pub fn __aeabi_dmul(a: f64, b: f64) callconv(.C) f64 {
3333
3434fn mulXf3(comptime T: type, a: T, b: T) T {
3535 @setRuntimeSafety(builtin.is_test);
36 const Z = std.meta.Int(false, T.bit_count);
36 const typeWidth = @typeInfo(T).Float.bits;
37 const Z = std.meta.Int(false, typeWidth);
3738
38 const typeWidth = T.bit_count;
3939 const significandBits = std.math.floatMantissaBits(T);
4040 const exponentBits = std.math.floatExponentBits(T);
4141
......@@ -269,9 +269,9 @@ fn wideMultiply(comptime Z: type, a: Z, b: Z, hi: *Z, lo: *Z) void {
269269 }
270270}
271271
272fn normalize(comptime T: type, significand: *std.meta.Int(false, T.bit_count)) i32 {
272fn normalize(comptime T: type, significand: *std.meta.Int(false, @typeInfo(T).Float.bits)) i32 {
273273 @setRuntimeSafety(builtin.is_test);
274 const Z = std.meta.Int(false, T.bit_count);
274 const Z = std.meta.Int(false, @typeInfo(T).Float.bits);
275275 const significandBits = std.math.floatMantissaBits(T);
276276 const implicitBit = @as(Z, 1) << significandBits;
277277
......@@ -282,7 +282,7 @@ fn normalize(comptime T: type, significand: *std.meta.Int(false, T.bit_count)) i
282282
283283fn wideRightShiftWithSticky(comptime Z: type, hi: *Z, lo: *Z, count: u32) void {
284284 @setRuntimeSafety(builtin.is_test);
285 const typeWidth = Z.bit_count;
285 const typeWidth = @typeInfo(Z).Int.bits;
286286 const S = std.math.Log2Int(Z);
287287 if (count < typeWidth) {
288288 const sticky = @truncate(u8, lo.* << @intCast(S, typeWidth -% count));
lib/std/special/compiler_rt/mulodi4.zig+1-1
......@@ -11,7 +11,7 @@ const minInt = std.math.minInt;
1111pub fn __mulodi4(a: i64, b: i64, overflow: *c_int) callconv(.C) i64 {
1212 @setRuntimeSafety(builtin.is_test);
1313
14 const min = @bitCast(i64, @as(u64, 1 << (i64.bit_count - 1)));
14 const min = @bitCast(i64, @as(u64, 1 << (64 - 1)));
1515 const max = ~min;
1616
1717 overflow.* = 0;
lib/std/special/compiler_rt/muloti4.zig+3-3
......@@ -9,7 +9,7 @@ const compiler_rt = @import("../compiler_rt.zig");
99pub fn __muloti4(a: i128, b: i128, overflow: *c_int) callconv(.C) i128 {
1010 @setRuntimeSafety(builtin.is_test);
1111
12 const min = @bitCast(i128, @as(u128, 1 << (i128.bit_count - 1)));
12 const min = @bitCast(i128, @as(u128, 1 << (128 - 1)));
1313 const max = ~min;
1414 overflow.* = 0;
1515
......@@ -27,9 +27,9 @@ pub fn __muloti4(a: i128, b: i128, overflow: *c_int) callconv(.C) i128 {
2727 return r;
2828 }
2929
30 const sa = a >> (i128.bit_count - 1);
30 const sa = a >> (128 - 1);
3131 const abs_a = (a ^ sa) -% sa;
32 const sb = b >> (i128.bit_count - 1);
32 const sb = b >> (128 - 1);
3333 const abs_b = (b ^ sb) -% sb;
3434
3535 if (abs_a < 2 or abs_b < 2) {
lib/std/special/compiler_rt/negXf2.zig+1-2
......@@ -24,9 +24,8 @@ pub fn __aeabi_dneg(arg: f64) callconv(.AAPCS) f64 {
2424}
2525
2626fn negXf2(comptime T: type, a: T) T {
27 const Z = std.meta.Int(false, T.bit_count);
27 const Z = std.meta.Int(false, @typeInfo(T).Float.bits);
2828
29 const typeWidth = T.bit_count;
3029 const significandBits = std.math.floatMantissaBits(T);
3130 const exponentBits = std.math.floatExponentBits(T);
3231
lib/std/special/compiler_rt/shift.zig+13-12
......@@ -9,8 +9,9 @@ const Log2Int = std.math.Log2Int;
99
1010fn Dwords(comptime T: type, comptime signed_half: bool) type {
1111 return extern union {
12 pub const HalfTU = std.meta.Int(false, @divExact(T.bit_count, 2));
13 pub const HalfTS = std.meta.Int(true, @divExact(T.bit_count, 2));
12 pub const bits = @divExact(@typeInfo(T).Int.bits, 2);
13 pub const HalfTU = std.meta.Int(false, bits);
14 pub const HalfTS = std.meta.Int(true, bits);
1415 pub const HalfT = if (signed_half) HalfTS else HalfTU;
1516
1617 all: T,
......@@ -30,15 +31,15 @@ pub fn ashlXi3(comptime T: type, a: T, b: i32) T {
3031 const input = dwords{ .all = a };
3132 var output: dwords = undefined;
3233
33 if (b >= dwords.HalfT.bit_count) {
34 if (b >= dwords.bits) {
3435 output.s.low = 0;
35 output.s.high = input.s.low << @intCast(S, b - dwords.HalfT.bit_count);
36 output.s.high = input.s.low << @intCast(S, b - dwords.bits);
3637 } else if (b == 0) {
3738 return a;
3839 } else {
3940 output.s.low = input.s.low << @intCast(S, b);
4041 output.s.high = input.s.high << @intCast(S, b);
41 output.s.high |= input.s.low >> @intCast(S, dwords.HalfT.bit_count - b);
42 output.s.high |= input.s.low >> @intCast(S, dwords.bits - b);
4243 }
4344
4445 return output.all;
......@@ -53,14 +54,14 @@ pub fn ashrXi3(comptime T: type, a: T, b: i32) T {
5354 const input = dwords{ .all = a };
5455 var output: dwords = undefined;
5556
56 if (b >= dwords.HalfT.bit_count) {
57 output.s.high = input.s.high >> (dwords.HalfT.bit_count - 1);
58 output.s.low = input.s.high >> @intCast(S, b - dwords.HalfT.bit_count);
57 if (b >= dwords.bits) {
58 output.s.high = input.s.high >> (dwords.bits - 1);
59 output.s.low = input.s.high >> @intCast(S, b - dwords.bits);
5960 } else if (b == 0) {
6061 return a;
6162 } else {
6263 output.s.high = input.s.high >> @intCast(S, b);
63 output.s.low = input.s.high << @intCast(S, dwords.HalfT.bit_count - b);
64 output.s.low = input.s.high << @intCast(S, dwords.bits - b);
6465 // Avoid sign-extension here
6566 output.s.low |= @bitCast(
6667 dwords.HalfT,
......@@ -80,14 +81,14 @@ pub fn lshrXi3(comptime T: type, a: T, b: i32) T {
8081 const input = dwords{ .all = a };
8182 var output: dwords = undefined;
8283
83 if (b >= dwords.HalfT.bit_count) {
84 if (b >= dwords.bits) {
8485 output.s.high = 0;
85 output.s.low = input.s.high >> @intCast(S, b - dwords.HalfT.bit_count);
86 output.s.low = input.s.high >> @intCast(S, b - dwords.bits);
8687 } else if (b == 0) {
8788 return a;
8889 } else {
8990 output.s.high = input.s.high >> @intCast(S, b);
90 output.s.low = input.s.high << @intCast(S, dwords.HalfT.bit_count - b);
91 output.s.low = input.s.high << @intCast(S, dwords.bits - b);
9192 output.s.low |= input.s.low >> @intCast(S, b);
9293 }
9394
lib/std/special/compiler_rt/truncXfYf2.zig+2-2
......@@ -50,7 +50,7 @@ fn truncXfYf2(comptime dst_t: type, comptime src_t: type, a: src_t) dst_t {
5050
5151 // Various constants whose values follow from the type parameters.
5252 // Any reasonable optimizer will fold and propagate all of these.
53 const srcBits = src_t.bit_count;
53 const srcBits = @typeInfo(src_t).Float.bits;
5454 const srcExpBits = srcBits - srcSigBits - 1;
5555 const srcInfExp = (1 << srcExpBits) - 1;
5656 const srcExpBias = srcInfExp >> 1;
......@@ -65,7 +65,7 @@ fn truncXfYf2(comptime dst_t: type, comptime src_t: type, a: src_t) dst_t {
6565 const srcQNaN = 1 << (srcSigBits - 1);
6666 const srcNaNCode = srcQNaN - 1;
6767
68 const dstBits = dst_t.bit_count;
68 const dstBits = @typeInfo(dst_t).Float.bits;
6969 const dstExpBits = dstBits - dstSigBits - 1;
7070 const dstInfExp = (1 << dstExpBits) - 1;
7171 const dstExpBias = dstInfExp >> 1;
lib/std/special/compiler_rt/udivmod.zig+36-34
......@@ -15,8 +15,10 @@ const high = 1 - low;
1515pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem: ?*DoubleInt) DoubleInt {
1616 @setRuntimeSafety(is_test);
1717
18 const SingleInt = @import("std").meta.Int(false, @divExact(DoubleInt.bit_count, 2));
19 const SignedDoubleInt = @import("std").meta.Int(true, DoubleInt.bit_count);
18 const double_int_bits = @typeInfo(DoubleInt).Int.bits;
19 const single_int_bits = @divExact(double_int_bits, 2);
20 const SingleInt = @import("std").meta.Int(false, single_int_bits);
21 const SignedDoubleInt = @import("std").meta.Int(true, double_int_bits);
2022 const Log2SingleInt = @import("std").math.Log2Int(SingleInt);
2123
2224 const n = @ptrCast(*const [2]SingleInt, &a).*; // TODO issue #421
......@@ -82,21 +84,21 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
8284 // ---
8385 // K 0
8486 sr = @bitCast(c_uint, @as(c_int, @clz(SingleInt, d[high])) - @as(c_int, @clz(SingleInt, n[high])));
85 // 0 <= sr <= SingleInt.bit_count - 2 or sr large
86 if (sr > SingleInt.bit_count - 2) {
87 // 0 <= sr <= single_int_bits - 2 or sr large
88 if (sr > single_int_bits - 2) {
8789 if (maybe_rem) |rem| {
8890 rem.* = a;
8991 }
9092 return 0;
9193 }
9294 sr += 1;
93 // 1 <= sr <= SingleInt.bit_count - 1
94 // q.all = a << (DoubleInt.bit_count - sr);
95 // 1 <= sr <= single_int_bits - 1
96 // q.all = a << (double_int_bits - sr);
9597 q[low] = 0;
96 q[high] = n[low] << @intCast(Log2SingleInt, SingleInt.bit_count - sr);
98 q[high] = n[low] << @intCast(Log2SingleInt, single_int_bits - sr);
9799 // r.all = a >> sr;
98100 r[high] = n[high] >> @intCast(Log2SingleInt, sr);
99 r[low] = (n[high] << @intCast(Log2SingleInt, SingleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
101 r[low] = (n[high] << @intCast(Log2SingleInt, single_int_bits - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
100102 } else {
101103 // d[low] != 0
102104 if (d[high] == 0) {
......@@ -113,74 +115,74 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
113115 }
114116 sr = @ctz(SingleInt, d[low]);
115117 q[high] = n[high] >> @intCast(Log2SingleInt, sr);
116 q[low] = (n[high] << @intCast(Log2SingleInt, SingleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
118 q[low] = (n[high] << @intCast(Log2SingleInt, single_int_bits - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
117119 return @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &q[0]).*; // TODO issue #421
118120 }
119121 // K X
120122 // ---
121123 // 0 K
122 sr = 1 + SingleInt.bit_count + @as(c_uint, @clz(SingleInt, d[low])) - @as(c_uint, @clz(SingleInt, n[high]));
123 // 2 <= sr <= DoubleInt.bit_count - 1
124 // q.all = a << (DoubleInt.bit_count - sr);
124 sr = 1 + single_int_bits + @as(c_uint, @clz(SingleInt, d[low])) - @as(c_uint, @clz(SingleInt, n[high]));
125 // 2 <= sr <= double_int_bits - 1
126 // q.all = a << (double_int_bits - sr);
125127 // r.all = a >> sr;
126 if (sr == SingleInt.bit_count) {
128 if (sr == single_int_bits) {
127129 q[low] = 0;
128130 q[high] = n[low];
129131 r[high] = 0;
130132 r[low] = n[high];
131 } else if (sr < SingleInt.bit_count) {
132 // 2 <= sr <= SingleInt.bit_count - 1
133 } else if (sr < single_int_bits) {
134 // 2 <= sr <= single_int_bits - 1
133135 q[low] = 0;
134 q[high] = n[low] << @intCast(Log2SingleInt, SingleInt.bit_count - sr);
136 q[high] = n[low] << @intCast(Log2SingleInt, single_int_bits - sr);
135137 r[high] = n[high] >> @intCast(Log2SingleInt, sr);
136 r[low] = (n[high] << @intCast(Log2SingleInt, SingleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
138 r[low] = (n[high] << @intCast(Log2SingleInt, single_int_bits - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
137139 } else {
138 // SingleInt.bit_count + 1 <= sr <= DoubleInt.bit_count - 1
139 q[low] = n[low] << @intCast(Log2SingleInt, DoubleInt.bit_count - sr);
140 q[high] = (n[high] << @intCast(Log2SingleInt, DoubleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr - SingleInt.bit_count));
140 // single_int_bits + 1 <= sr <= double_int_bits - 1
141 q[low] = n[low] << @intCast(Log2SingleInt, double_int_bits - sr);
142 q[high] = (n[high] << @intCast(Log2SingleInt, double_int_bits - sr)) | (n[low] >> @intCast(Log2SingleInt, sr - single_int_bits));
141143 r[high] = 0;
142 r[low] = n[high] >> @intCast(Log2SingleInt, sr - SingleInt.bit_count);
144 r[low] = n[high] >> @intCast(Log2SingleInt, sr - single_int_bits);
143145 }
144146 } else {
145147 // K X
146148 // ---
147149 // K K
148150 sr = @bitCast(c_uint, @as(c_int, @clz(SingleInt, d[high])) - @as(c_int, @clz(SingleInt, n[high])));
149 // 0 <= sr <= SingleInt.bit_count - 1 or sr large
150 if (sr > SingleInt.bit_count - 1) {
151 // 0 <= sr <= single_int_bits - 1 or sr large
152 if (sr > single_int_bits - 1) {
151153 if (maybe_rem) |rem| {
152154 rem.* = a;
153155 }
154156 return 0;
155157 }
156158 sr += 1;
157 // 1 <= sr <= SingleInt.bit_count
158 // q.all = a << (DoubleInt.bit_count - sr);
159 // 1 <= sr <= single_int_bits
160 // q.all = a << (double_int_bits - sr);
159161 // r.all = a >> sr;
160162 q[low] = 0;
161 if (sr == SingleInt.bit_count) {
163 if (sr == single_int_bits) {
162164 q[high] = n[low];
163165 r[high] = 0;
164166 r[low] = n[high];
165167 } else {
166168 r[high] = n[high] >> @intCast(Log2SingleInt, sr);
167 r[low] = (n[high] << @intCast(Log2SingleInt, SingleInt.bit_count - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
168 q[high] = n[low] << @intCast(Log2SingleInt, SingleInt.bit_count - sr);
169 r[low] = (n[high] << @intCast(Log2SingleInt, single_int_bits - sr)) | (n[low] >> @intCast(Log2SingleInt, sr));
170 q[high] = n[low] << @intCast(Log2SingleInt, single_int_bits - sr);
169171 }
170172 }
171173 }
172174 // Not a special case
173175 // q and r are initialized with:
174 // q.all = a << (DoubleInt.bit_count - sr);
176 // q.all = a << (double_int_bits - sr);
175177 // r.all = a >> sr;
176 // 1 <= sr <= DoubleInt.bit_count - 1
178 // 1 <= sr <= double_int_bits - 1
177179 var carry: u32 = 0;
178180 var r_all: DoubleInt = undefined;
179181 while (sr > 0) : (sr -= 1) {
180182 // r:q = ((r:q) << 1) | carry
181 r[high] = (r[high] << 1) | (r[low] >> (SingleInt.bit_count - 1));
182 r[low] = (r[low] << 1) | (q[high] >> (SingleInt.bit_count - 1));
183 q[high] = (q[high] << 1) | (q[low] >> (SingleInt.bit_count - 1));
183 r[high] = (r[high] << 1) | (r[low] >> (single_int_bits - 1));
184 r[low] = (r[low] << 1) | (q[high] >> (single_int_bits - 1));
185 q[high] = (q[high] << 1) | (q[low] >> (single_int_bits - 1));
184186 q[low] = (q[low] << 1) | carry;
185187 // carry = 0;
186188 // if (r.all >= b)
......@@ -189,7 +191,7 @@ pub fn udivmod(comptime DoubleInt: type, a: DoubleInt, b: DoubleInt, maybe_rem:
189191 // carry = 1;
190192 // }
191193 r_all = @ptrCast(*align(@alignOf(SingleInt)) DoubleInt, &r[0]).*; // TODO issue #421
192 const s: SignedDoubleInt = @bitCast(SignedDoubleInt, b -% r_all -% 1) >> (DoubleInt.bit_count - 1);
194 const s: SignedDoubleInt = @bitCast(SignedDoubleInt, b -% r_all -% 1) >> (double_int_bits - 1);
193195 carry = @intCast(u32, s & 1);
194196 r_all -= b & @bitCast(DoubleInt, s);
195197 r = @ptrCast(*[2]SingleInt, &r_all).*; // TODO issue #421
lib/std/start.zig+2-2
......@@ -67,7 +67,7 @@ fn EfiMain(handle: uefi.Handle, system_table: *uefi.tables.SystemTable) callconv
6767 uefi.handle = handle;
6868 uefi.system_table = system_table;
6969
70 switch (@TypeOf(root.main).ReturnType) {
70 switch (@typeInfo(@TypeOf(root.main)).Fn.return_type.?) {
7171 noreturn => {
7272 root.main();
7373 },
......@@ -239,7 +239,7 @@ fn callMainAsync(loop: *std.event.Loop) callconv(.Async) u8 {
239239// This is not marked inline because it is called with @asyncCall when
240240// there is an event loop.
241241pub fn callMain() u8 {
242 switch (@typeInfo(@TypeOf(root.main).ReturnType)) {
242 switch (@typeInfo(@typeInfo(@TypeOf(root.main)).Fn.return_type.?)) {
243243 .NoReturn => {
244244 root.main();
245245 },
lib/std/target.zig+58
......@@ -468,6 +468,7 @@ pub const Target = struct {
468468 /// TODO Get rid of this one.
469469 unknown,
470470 coff,
471 pe,
471472 elf,
472473 macho,
473474 wasm,
......@@ -771,6 +772,63 @@ pub const Target = struct {
771772 };
772773 }
773774
775 pub fn toCoffMachine(arch: Arch) std.coff.MachineType {
776 return switch (arch) {
777 .avr => .Unknown,
778 .msp430 => .Unknown,
779 .arc => .Unknown,
780 .arm => .ARM,
781 .armeb => .Unknown,
782 .hexagon => .Unknown,
783 .le32 => .Unknown,
784 .mips => .Unknown,
785 .mipsel => .Unknown,
786 .powerpc => .POWERPC,
787 .r600 => .Unknown,
788 .riscv32 => .RISCV32,
789 .sparc => .Unknown,
790 .sparcel => .Unknown,
791 .tce => .Unknown,
792 .tcele => .Unknown,
793 .thumb => .Thumb,
794 .thumbeb => .Thumb,
795 .i386 => .I386,
796 .xcore => .Unknown,
797 .nvptx => .Unknown,
798 .amdil => .Unknown,
799 .hsail => .Unknown,
800 .spir => .Unknown,
801 .kalimba => .Unknown,
802 .shave => .Unknown,
803 .lanai => .Unknown,
804 .wasm32 => .Unknown,
805 .renderscript32 => .Unknown,
806 .aarch64_32 => .ARM64,
807 .aarch64 => .ARM64,
808 .aarch64_be => .Unknown,
809 .mips64 => .Unknown,
810 .mips64el => .Unknown,
811 .powerpc64 => .Unknown,
812 .powerpc64le => .Unknown,
813 .riscv64 => .RISCV64,
814 .x86_64 => .X64,
815 .nvptx64 => .Unknown,
816 .le64 => .Unknown,
817 .amdil64 => .Unknown,
818 .hsail64 => .Unknown,
819 .spir64 => .Unknown,
820 .wasm64 => .Unknown,
821 .renderscript64 => .Unknown,
822 .amdgcn => .Unknown,
823 .bpfel => .Unknown,
824 .bpfeb => .Unknown,
825 .sparcv9 => .Unknown,
826 .s390x => .Unknown,
827 .ve => .Unknown,
828 .spu_2 => .Unknown,
829 };
830 }
831
774832 pub fn endian(arch: Arch) builtin.Endian {
775833 return switch (arch) {
776834 .avr,
lib/std/thread.zig+3-3
......@@ -166,7 +166,7 @@ pub const Thread = struct {
166166 fn threadMain(raw_arg: windows.LPVOID) callconv(.C) windows.DWORD {
167167 const arg = if (@sizeOf(Context) == 0) {} else @ptrCast(*Context, @alignCast(@alignOf(Context), raw_arg)).*;
168168
169 switch (@typeInfo(@TypeOf(startFn).ReturnType)) {
169 switch (@typeInfo(@typeInfo(@TypeOf(startFn)).Fn.return_type.?)) {
170170 .NoReturn => {
171171 startFn(arg);
172172 },
......@@ -227,7 +227,7 @@ pub const Thread = struct {
227227 fn linuxThreadMain(ctx_addr: usize) callconv(.C) u8 {
228228 const arg = if (@sizeOf(Context) == 0) {} else @intToPtr(*const Context, ctx_addr).*;
229229
230 switch (@typeInfo(@TypeOf(startFn).ReturnType)) {
230 switch (@typeInfo(@typeInfo(@TypeOf(startFn)).Fn.return_type.?)) {
231231 .NoReturn => {
232232 startFn(arg);
233233 },
......@@ -259,7 +259,7 @@ pub const Thread = struct {
259259 fn posixThreadMain(ctx: ?*c_void) callconv(.C) ?*c_void {
260260 const arg = if (@sizeOf(Context) == 0) {} else @ptrCast(*Context, @alignCast(@alignOf(Context), ctx)).*;
261261
262 switch (@typeInfo(@TypeOf(startFn).ReturnType)) {
262 switch (@typeInfo(@typeInfo(@TypeOf(startFn)).Fn.return_type.?)) {
263263 .NoReturn => {
264264 startFn(arg);
265265 },
lib/std/zig.zig+1-1
......@@ -22,7 +22,7 @@ pub const SrcHash = [16]u8;
2222/// If it is long, blake3 hash is computed.
2323pub fn hashSrc(src: []const u8) SrcHash {
2424 var out: SrcHash = undefined;
25 if (src.len <= SrcHash.len) {
25 if (src.len <= @typeInfo(SrcHash).Array.len) {
2626 std.mem.copy(u8, &out, src);
2727 std.mem.set(u8, out[src.len..], 0);
2828 } else {
src-self-hosted/Module.zig+7
......@@ -626,6 +626,7 @@ pub const Scope = struct {
626626 module.gpa,
627627 self.sub_file_path,
628628 std.math.maxInt(u32),
629 null,
629630 1,
630631 0,
631632 );
......@@ -723,6 +724,7 @@ pub const Scope = struct {
723724 module.gpa,
724725 self.sub_file_path,
725726 std.math.maxInt(u32),
727 null,
726728 1,
727729 0,
728730 );
......@@ -1820,6 +1822,9 @@ fn analyzeContainer(self: *Module, container_scope: *Scope.Container) !void {
18201822 try self.markOutdatedDecl(decl);
18211823 decl.contents_hash = contents_hash;
18221824 } else switch (self.bin_file.tag) {
1825 .coff => {
1826 // TODO Implement for COFF
1827 },
18231828 .elf => if (decl.fn_link.elf.len != 0) {
18241829 // TODO Look into detecting when this would be unnecessary by storing enough state
18251830 // in `Decl` to notice that the line number did not change.
......@@ -2078,12 +2083,14 @@ fn allocateNewDecl(
20782083 .deletion_flag = false,
20792084 .contents_hash = contents_hash,
20802085 .link = switch (self.bin_file.tag) {
2086 .coff => .{ .coff = link.File.Coff.TextBlock.empty },
20812087 .elf => .{ .elf = link.File.Elf.TextBlock.empty },
20822088 .macho => .{ .macho = link.File.MachO.TextBlock.empty },
20832089 .c => .{ .c = {} },
20842090 .wasm => .{ .wasm = {} },
20852091 },
20862092 .fn_link = switch (self.bin_file.tag) {
2093 .coff => .{ .coff = {} },
20872094 .elf => .{ .elf = link.File.Elf.SrcFn.empty },
20882095 .macho => .{ .macho = link.File.MachO.SrcFn.empty },
20892096 .c => .{ .c = {} },
src-self-hosted/codegen.zig+166-112
......@@ -59,14 +59,21 @@ pub const GenerateSymbolError = error{
5959 AnalysisFail,
6060};
6161
62pub const DebugInfoOutput = union(enum) {
63 dwarf: struct {
64 dbg_line: *std.ArrayList(u8),
65 dbg_info: *std.ArrayList(u8),
66 dbg_info_type_relocs: *link.File.DbgInfoTypeRelocsTable,
67 },
68 none,
69};
70
6271pub fn generateSymbol(
6372 bin_file: *link.File,
6473 src: usize,
6574 typed_value: TypedValue,
6675 code: *std.ArrayList(u8),
67 dbg_line: *std.ArrayList(u8),
68 dbg_info: *std.ArrayList(u8),
69 dbg_info_type_relocs: *link.File.DbgInfoTypeRelocsTable,
76 debug_output: DebugInfoOutput,
7077) GenerateSymbolError!Result {
7178 const tracy = trace(@src());
7279 defer tracy.end();
......@@ -76,56 +83,56 @@ pub fn generateSymbol(
7683 switch (bin_file.options.target.cpu.arch) {
7784 .wasm32 => unreachable, // has its own code path
7885 .wasm64 => unreachable, // has its own code path
79 .arm => return Function(.arm).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
80 .armeb => return Function(.armeb).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
81 //.aarch64 => return Function(.aarch64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
82 //.aarch64_be => return Function(.aarch64_be).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
83 //.aarch64_32 => return Function(.aarch64_32).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
84 //.arc => return Function(.arc).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
85 //.avr => return Function(.avr).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
86 //.bpfel => return Function(.bpfel).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
87 //.bpfeb => return Function(.bpfeb).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
88 //.hexagon => return Function(.hexagon).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
89 //.mips => return Function(.mips).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
90 //.mipsel => return Function(.mipsel).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
91 //.mips64 => return Function(.mips64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
92 //.mips64el => return Function(.mips64el).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
93 //.msp430 => return Function(.msp430).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
94 //.powerpc => return Function(.powerpc).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
95 //.powerpc64 => return Function(.powerpc64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
96 //.powerpc64le => return Function(.powerpc64le).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
97 //.r600 => return Function(.r600).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
98 //.amdgcn => return Function(.amdgcn).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
99 //.riscv32 => return Function(.riscv32).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
100 .riscv64 => return Function(.riscv64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
101 //.sparc => return Function(.sparc).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
102 //.sparcv9 => return Function(.sparcv9).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
103 //.sparcel => return Function(.sparcel).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
104 //.s390x => return Function(.s390x).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
105 .spu_2 => return Function(.spu_2).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
106 //.tce => return Function(.tce).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
107 //.tcele => return Function(.tcele).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
108 //.thumb => return Function(.thumb).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
109 //.thumbeb => return Function(.thumbeb).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
110 //.i386 => return Function(.i386).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
111 .x86_64 => return Function(.x86_64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
112 //.xcore => return Function(.xcore).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
113 //.nvptx => return Function(.nvptx).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
114 //.nvptx64 => return Function(.nvptx64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
115 //.le32 => return Function(.le32).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
116 //.le64 => return Function(.le64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
117 //.amdil => return Function(.amdil).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
118 //.amdil64 => return Function(.amdil64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
119 //.hsail => return Function(.hsail).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
120 //.hsail64 => return Function(.hsail64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
121 //.spir => return Function(.spir).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
122 //.spir64 => return Function(.spir64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
123 //.kalimba => return Function(.kalimba).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
124 //.shave => return Function(.shave).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
125 //.lanai => return Function(.lanai).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
126 //.renderscript32 => return Function(.renderscript32).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
127 //.renderscript64 => return Function(.renderscript64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
128 //.ve => return Function(.ve).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
86 .arm => return Function(.arm).generateSymbol(bin_file, src, typed_value, code, debug_output),
87 .armeb => return Function(.armeb).generateSymbol(bin_file, src, typed_value, code, debug_output),
88 //.aarch64 => return Function(.aarch64).generateSymbol(bin_file, src, typed_value, code, debug_output),
89 //.aarch64_be => return Function(.aarch64_be).generateSymbol(bin_file, src, typed_value, code, debug_output),
90 //.aarch64_32 => return Function(.aarch64_32).generateSymbol(bin_file, src, typed_value, code, debug_output),
91 //.arc => return Function(.arc).generateSymbol(bin_file, src, typed_value, code, debug_output),
92 //.avr => return Function(.avr).generateSymbol(bin_file, src, typed_value, code, debug_output),
93 //.bpfel => return Function(.bpfel).generateSymbol(bin_file, src, typed_value, code, debug_output),
94 //.bpfeb => return Function(.bpfeb).generateSymbol(bin_file, src, typed_value, code, debug_output),
95 //.hexagon => return Function(.hexagon).generateSymbol(bin_file, src, typed_value, code, debug_output),
96 //.mips => return Function(.mips).generateSymbol(bin_file, src, typed_value, code, debug_output),
97 //.mipsel => return Function(.mipsel).generateSymbol(bin_file, src, typed_value, code, debug_output),
98 //.mips64 => return Function(.mips64).generateSymbol(bin_file, src, typed_value, code, debug_output),
99 //.mips64el => return Function(.mips64el).generateSymbol(bin_file, src, typed_value, code, debug_output),
100 //.msp430 => return Function(.msp430).generateSymbol(bin_file, src, typed_value, code, debug_output),
101 //.powerpc => return Function(.powerpc).generateSymbol(bin_file, src, typed_value, code, debug_output),
102 //.powerpc64 => return Function(.powerpc64).generateSymbol(bin_file, src, typed_value, code, debug_output),
103 //.powerpc64le => return Function(.powerpc64le).generateSymbol(bin_file, src, typed_value, code, debug_output),
104 //.r600 => return Function(.r600).generateSymbol(bin_file, src, typed_value, code, debug_output),
105 //.amdgcn => return Function(.amdgcn).generateSymbol(bin_file, src, typed_value, code, debug_output),
106 //.riscv32 => return Function(.riscv32).generateSymbol(bin_file, src, typed_value, code, debug_output),
107 .riscv64 => return Function(.riscv64).generateSymbol(bin_file, src, typed_value, code, debug_output),
108 //.sparc => return Function(.sparc).generateSymbol(bin_file, src, typed_value, code, debug_output),
109 //.sparcv9 => return Function(.sparcv9).generateSymbol(bin_file, src, typed_value, code, debug_output),
110 //.sparcel => return Function(.sparcel).generateSymbol(bin_file, src, typed_value, code, debug_output),
111 //.s390x => return Function(.s390x).generateSymbol(bin_file, src, typed_value, code, debug_output),
112 .spu_2 => return Function(.spu_2).generateSymbol(bin_file, src, typed_value, code, debug_output),
113 //.tce => return Function(.tce).generateSymbol(bin_file, src, typed_value, code, debug_output),
114 //.tcele => return Function(.tcele).generateSymbol(bin_file, src, typed_value, code, debug_output),
115 //.thumb => return Function(.thumb).generateSymbol(bin_file, src, typed_value, code, debug_output),
116 //.thumbeb => return Function(.thumbeb).generateSymbol(bin_file, src, typed_value, code, debug_output),
117 //.i386 => return Function(.i386).generateSymbol(bin_file, src, typed_value, code, debug_output),
118 .x86_64 => return Function(.x86_64).generateSymbol(bin_file, src, typed_value, code, debug_output),
119 //.xcore => return Function(.xcore).generateSymbol(bin_file, src, typed_value, code, debug_output),
120 //.nvptx => return Function(.nvptx).generateSymbol(bin_file, src, typed_value, code, debug_output),
121 //.nvptx64 => return Function(.nvptx64).generateSymbol(bin_file, src, typed_value, code, debug_output),
122 //.le32 => return Function(.le32).generateSymbol(bin_file, src, typed_value, code, debug_output),
123 //.le64 => return Function(.le64).generateSymbol(bin_file, src, typed_value, code, debug_output),
124 //.amdil => return Function(.amdil).generateSymbol(bin_file, src, typed_value, code, debug_output),
125 //.amdil64 => return Function(.amdil64).generateSymbol(bin_file, src, typed_value, code, debug_output),
126 //.hsail => return Function(.hsail).generateSymbol(bin_file, src, typed_value, code, debug_output),
127 //.hsail64 => return Function(.hsail64).generateSymbol(bin_file, src, typed_value, code, debug_output),
128 //.spir => return Function(.spir).generateSymbol(bin_file, src, typed_value, code, debug_output),
129 //.spir64 => return Function(.spir64).generateSymbol(bin_file, src, typed_value, code, debug_output),
130 //.kalimba => return Function(.kalimba).generateSymbol(bin_file, src, typed_value, code, debug_output),
131 //.shave => return Function(.shave).generateSymbol(bin_file, src, typed_value, code, debug_output),
132 //.lanai => return Function(.lanai).generateSymbol(bin_file, src, typed_value, code, debug_output),
133 //.renderscript32 => return Function(.renderscript32).generateSymbol(bin_file, src, typed_value, code, debug_output),
134 //.renderscript64 => return Function(.renderscript64).generateSymbol(bin_file, src, typed_value, code, debug_output),
135 //.ve => return Function(.ve).generateSymbol(bin_file, src, typed_value, code, debug_output),
129136 else => @panic("Backend architectures that don't have good support yet are commented out, to improve compilation performance. If you are interested in one of these other backends feel free to uncomment them. Eventually these will be completed, but stage1 is slow and a memory hog."),
130137 }
131138 },
......@@ -139,7 +146,7 @@ pub fn generateSymbol(
139146 switch (try generateSymbol(bin_file, src, .{
140147 .ty = typed_value.ty.elemType(),
141148 .val = sentinel,
142 }, code, dbg_line, dbg_info, dbg_info_type_relocs)) {
149 }, code, debug_output)) {
143150 .appended => return Result{ .appended = {} },
144151 .externally_managed => |slice| {
145152 code.appendSliceAssumeCapacity(slice);
......@@ -239,9 +246,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
239246 target: *const std.Target,
240247 mod_fn: *const Module.Fn,
241248 code: *std.ArrayList(u8),
242 dbg_line: *std.ArrayList(u8),
243 dbg_info: *std.ArrayList(u8),
244 dbg_info_type_relocs: *link.File.DbgInfoTypeRelocsTable,
249 debug_output: DebugInfoOutput,
245250 err_msg: ?*ErrorMsg,
246251 args: []MCValue,
247252 ret_mcv: MCValue,
......@@ -419,9 +424,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
419424 src: usize,
420425 typed_value: TypedValue,
421426 code: *std.ArrayList(u8),
422 dbg_line: *std.ArrayList(u8),
423 dbg_info: *std.ArrayList(u8),
424 dbg_info_type_relocs: *link.File.DbgInfoTypeRelocsTable,
427 debug_output: DebugInfoOutput,
425428 ) GenerateSymbolError!Result {
426429 const module_fn = typed_value.val.cast(Value.Payload.Function).?.func;
427430
......@@ -457,9 +460,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
457460 .bin_file = bin_file,
458461 .mod_fn = module_fn,
459462 .code = code,
460 .dbg_line = dbg_line,
461 .dbg_info = dbg_info,
462 .dbg_info_type_relocs = dbg_info_type_relocs,
463 .debug_output = debug_output,
463464 .err_msg = null,
464465 .args = undefined, // populated after `resolveCallingConventionValues`
465466 .ret_mcv = undefined, // populated after `resolveCallingConventionValues`
......@@ -598,35 +599,50 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
598599 }
599600
600601 fn dbgSetPrologueEnd(self: *Self) InnerError!void {
601 try self.dbg_line.append(DW.LNS_set_prologue_end);
602 try self.dbgAdvancePCAndLine(self.prev_di_src);
602 switch (self.debug_output) {
603 .dwarf => |dbg_out| {
604 try dbg_out.dbg_line.append(DW.LNS_set_prologue_end);
605 try self.dbgAdvancePCAndLine(self.prev_di_src);
606 },
607 .none => {},
608 }
603609 }
604610
605611 fn dbgSetEpilogueBegin(self: *Self) InnerError!void {
606 try self.dbg_line.append(DW.LNS_set_epilogue_begin);
607 try self.dbgAdvancePCAndLine(self.prev_di_src);
612 switch (self.debug_output) {
613 .dwarf => |dbg_out| {
614 try dbg_out.dbg_line.append(DW.LNS_set_epilogue_begin);
615 try self.dbgAdvancePCAndLine(self.prev_di_src);
616 },
617 .none => {},
618 }
608619 }
609620
610621 fn dbgAdvancePCAndLine(self: *Self, src: usize) InnerError!void {
611 // TODO Look into improving the performance here by adding a token-index-to-line
612 // lookup table, and changing ir.Inst from storing byte offset to token. Currently
613 // this involves scanning over the source code for newlines
614 // (but only from the previous byte offset to the new one).
615 const delta_line = std.zig.lineDelta(self.source, self.prev_di_src, src);
616 const delta_pc = self.code.items.len - self.prev_di_pc;
617622 self.prev_di_src = src;
618623 self.prev_di_pc = self.code.items.len;
619 // TODO Look into using the DWARF special opcodes to compress this data. It lets you emit
620 // single-byte opcodes that add different numbers to both the PC and the line number
621 // at the same time.
622 try self.dbg_line.ensureCapacity(self.dbg_line.items.len + 11);
623 self.dbg_line.appendAssumeCapacity(DW.LNS_advance_pc);
624 leb128.writeULEB128(self.dbg_line.writer(), delta_pc) catch unreachable;
625 if (delta_line != 0) {
626 self.dbg_line.appendAssumeCapacity(DW.LNS_advance_line);
627 leb128.writeILEB128(self.dbg_line.writer(), delta_line) catch unreachable;
624 switch (self.debug_output) {
625 .dwarf => |dbg_out| {
626 // TODO Look into improving the performance here by adding a token-index-to-line
627 // lookup table, and changing ir.Inst from storing byte offset to token. Currently
628 // this involves scanning over the source code for newlines
629 // (but only from the previous byte offset to the new one).
630 const delta_line = std.zig.lineDelta(self.source, self.prev_di_src, src);
631 const delta_pc = self.code.items.len - self.prev_di_pc;
632 // TODO Look into using the DWARF special opcodes to compress this data. It lets you emit
633 // single-byte opcodes that add different numbers to both the PC and the line number
634 // at the same time.
635 try dbg_out.dbg_line.ensureCapacity(dbg_out.dbg_line.items.len + 11);
636 dbg_out.dbg_line.appendAssumeCapacity(DW.LNS_advance_pc);
637 leb128.writeULEB128(dbg_out.dbg_line.writer(), delta_pc) catch unreachable;
638 if (delta_line != 0) {
639 dbg_out.dbg_line.appendAssumeCapacity(DW.LNS_advance_line);
640 leb128.writeILEB128(dbg_out.dbg_line.writer(), delta_line) catch unreachable;
641 }
642 dbg_out.dbg_line.appendAssumeCapacity(DW.LNS_copy);
643 },
644 .none => {},
628645 }
629 self.dbg_line.appendAssumeCapacity(DW.LNS_copy);
630646 }
631647
632648 /// Asserts there is already capacity to insert into top branch inst_table.
......@@ -654,18 +670,23 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
654670 /// Adds a Type to the .debug_info at the current position. The bytes will be populated later,
655671 /// after codegen for this symbol is done.
656672 fn addDbgInfoTypeReloc(self: *Self, ty: Type) !void {
657 assert(ty.hasCodeGenBits());
658 const index = self.dbg_info.items.len;
659 try self.dbg_info.resize(index + 4); // DW.AT_type, DW.FORM_ref4
660
661 const gop = try self.dbg_info_type_relocs.getOrPut(self.gpa, ty);
662 if (!gop.found_existing) {
663 gop.entry.value = .{
664 .off = undefined,
665 .relocs = .{},
666 };
673 switch (self.debug_output) {
674 .dwarf => |dbg_out| {
675 assert(ty.hasCodeGenBits());
676 const index = dbg_out.dbg_info.items.len;
677 try dbg_out.dbg_info.resize(index + 4); // DW.AT_type, DW.FORM_ref4
678
679 const gop = try dbg_out.dbg_info_type_relocs.getOrPut(self.gpa, ty);
680 if (!gop.found_existing) {
681 gop.entry.value = .{
682 .off = undefined,
683 .relocs = .{},
684 };
685 }
686 try gop.entry.value.relocs.append(self.gpa, @intCast(u32, index));
687 },
688 .none => {},
667689 }
668 try gop.entry.value.relocs.append(self.gpa, @intCast(u32, index));
669690 }
670691
671692 fn genFuncInst(self: *Self, inst: *ir.Inst) !MCValue {
......@@ -1258,14 +1279,19 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
12581279 self.registers.putAssumeCapacityNoClobber(toCanonicalReg(reg), &inst.base);
12591280 self.markRegUsed(reg);
12601281
1261 try self.dbg_info.ensureCapacity(self.dbg_info.items.len + 8 + name_with_null.len);
1262 self.dbg_info.appendAssumeCapacity(link.File.Elf.abbrev_parameter);
1263 self.dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT_location, DW.FORM_exprloc
1264 1, // ULEB128 dwarf expression length
1265 reg.dwarfLocOp(),
1266 });
1267 try self.addDbgInfoTypeReloc(inst.base.ty); // DW.AT_type, DW.FORM_ref4
1268 self.dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT_name, DW.FORM_string
1282 switch (self.debug_output) {
1283 .dwarf => |dbg_out| {
1284 try dbg_out.dbg_info.ensureCapacity(dbg_out.dbg_info.items.len + 8 + name_with_null.len);
1285 dbg_out.dbg_info.appendAssumeCapacity(link.File.Elf.abbrev_parameter);
1286 dbg_out.dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT_location, DW.FORM_exprloc
1287 1, // ULEB128 dwarf expression length
1288 reg.dwarfLocOp(),
1289 });
1290 try self.addDbgInfoTypeReloc(inst.base.ty); // DW.AT_type, DW.FORM_ref4
1291 dbg_out.dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT_name, DW.FORM_string
1292 },
1293 .none => {},
1294 }
12691295 },
12701296 else => {},
12711297 }
......@@ -1302,7 +1328,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
13021328
13031329 // Due to incremental compilation, how function calls are generated depends
13041330 // on linking.
1305 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
1331 if (self.bin_file.tag == link.File.Elf.base_tag or self.bin_file.tag == link.File.Coff.base_tag) {
13061332 switch (arch) {
13071333 .x86_64 => {
13081334 for (info.args) |mc_arg, arg_i| {
......@@ -1341,10 +1367,17 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
13411367 if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
13421368 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
13431369 const func = func_val.func;
1344 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
1370
13451371 const ptr_bits = self.target.cpu.arch.ptrBitWidth();
13461372 const ptr_bytes: u64 = @divExact(ptr_bits, 8);
1347 const got_addr = @intCast(u32, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * ptr_bytes);
1373 const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {
1374 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
1375 break :blk @intCast(u32, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * ptr_bytes);
1376 } else if (self.bin_file.cast(link.File.Coff)) |coff_file|
1377 @intCast(u32, coff_file.offset_table_virtual_address + func.owner_decl.link.coff.offset_table_index * ptr_bytes)
1378 else
1379 unreachable;
1380
13481381 // ff 14 25 xx xx xx xx call [addr]
13491382 try self.code.ensureCapacity(self.code.items.len + 7);
13501383 self.code.appendSliceAssumeCapacity(&[3]u8{ 0xff, 0x14, 0x25 });
......@@ -1362,10 +1395,16 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
13621395 if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
13631396 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
13641397 const func = func_val.func;
1365 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
1398
13661399 const ptr_bits = self.target.cpu.arch.ptrBitWidth();
13671400 const ptr_bytes: u64 = @divExact(ptr_bits, 8);
1368 const got_addr = @intCast(u32, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * ptr_bytes);
1401 const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {
1402 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
1403 break :blk @intCast(u32, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * ptr_bytes);
1404 } else if (self.bin_file.cast(link.File.Coff)) |coff_file|
1405 coff_file.offset_table_virtual_address + func.owner_decl.link.coff.offset_table_index * ptr_bytes
1406 else
1407 unreachable;
13691408
13701409 try self.genSetReg(inst.base.src, .ra, .{ .memory = got_addr });
13711410 mem.writeIntLittle(u32, try self.code.addManyAsArray(4), Instruction.jalr(.ra, 0, .ra).toU32());
......@@ -1383,8 +1422,14 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
13831422 }
13841423 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
13851424 const func = func_val.func;
1386 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
1387 const got_addr = @intCast(u16, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * 2);
1425 const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {
1426 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
1427 break :blk @intCast(u16, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * 2);
1428 } else if (self.bin_file.cast(link.File.Coff)) |coff_file|
1429 @intCast(u16, coff_file.offset_table_virtual_address + func.owner_decl.link.coff.offset_table_index * 2)
1430 else
1431 unreachable;
1432
13881433 const return_type = func.owner_decl.typed_value.most_recent.typed_value.ty.fnReturnType();
13891434 // First, push the return address, then jump; if noreturn, don't bother with the first step
13901435 // TODO: implement packed struct -> u16 at comptime and move the bitcast here
......@@ -1420,10 +1465,15 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
14201465 if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
14211466 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
14221467 const func = func_val.func;
1423 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
14241468 const ptr_bits = self.target.cpu.arch.ptrBitWidth();
14251469 const ptr_bytes: u64 = @divExact(ptr_bits, 8);
1426 const got_addr = @intCast(u32, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * ptr_bytes);
1470 const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {
1471 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
1472 break :blk @intCast(u32, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * ptr_bytes);
1473 } else if (self.bin_file.cast(link.File.Coff)) |coff_file|
1474 coff_file.offset_table_virtual_address + func.owner_decl.link.coff.offset_table_index * ptr_bytes
1475 else
1476 unreachable;
14271477
14281478 // TODO only works with leaf functions
14291479 // at the moment, which works fine for
......@@ -1983,7 +2033,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
19832033
19842034 if (mem.eql(u8, inst.asm_source, "syscall")) {
19852035 try self.code.appendSlice(&[_]u8{ 0x0f, 0x05 });
1986 } else {
2036 } else if (inst.asm_source.len != 0) {
19872037 return self.fail(inst.base.src, "TODO implement support for more x86 assembly instructions", .{});
19882038 }
19892039
......@@ -2541,6 +2591,10 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
25412591 const got = &macho_file.sections.items[macho_file.got_section_index.?];
25422592 const got_addr = got.addr + decl.link.macho.offset_table_index.? * ptr_bytes;
25432593 return MCValue{ .memory = got_addr };
2594 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
2595 const decl = payload.decl;
2596 const got_addr = coff_file.offset_table_virtual_address + decl.link.coff.offset_table_index * ptr_bytes;
2597 return MCValue{ .memory = got_addr };
25442598 } else {
25452599 return self.fail(src, "TODO codegen non-ELF const Decl pointer", .{});
25462600 }
src-self-hosted/link.zig+20-2
......@@ -34,6 +34,7 @@ pub const File = struct {
3434
3535 pub const LinkBlock = union {
3636 elf: Elf.TextBlock,
37 coff: Coff.TextBlock,
3738 macho: MachO.TextBlock,
3839 c: void,
3940 wasm: void,
......@@ -41,6 +42,7 @@ pub const File = struct {
4142
4243 pub const LinkFn = union {
4344 elf: Elf.SrcFn,
45 coff: Coff.SrcFn,
4446 macho: MachO.SrcFn,
4547 c: void,
4648 wasm: ?Wasm.FnData,
......@@ -66,7 +68,7 @@ pub const File = struct {
6668 pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, options: Options) !*File {
6769 switch (options.object_format) {
6870 .unknown => unreachable,
69 .coff => return error.TODOImplementCoff,
71 .coff, .pe => return Coff.openPath(allocator, dir, sub_path, options),
7072 .elf => return Elf.openPath(allocator, dir, sub_path, options),
7173 .macho => return MachO.openPath(allocator, dir, sub_path, options),
7274 .wasm => return Wasm.openPath(allocator, dir, sub_path, options),
......@@ -85,7 +87,7 @@ pub const File = struct {
8587
8688 pub fn makeWritable(base: *File, dir: fs.Dir, sub_path: []const u8) !void {
8789 switch (base.tag) {
88 .elf, .macho => {
90 .coff, .elf, .macho => {
8991 if (base.file != null) return;
9092 base.file = try dir.createFile(sub_path, .{
9193 .truncate = false,
......@@ -112,6 +114,7 @@ pub const File = struct {
112114 /// after allocateDeclIndexes for any given Decl.
113115 pub fn updateDecl(base: *File, module: *Module, decl: *Module.Decl) !void {
114116 switch (base.tag) {
117 .coff => return @fieldParentPtr(Coff, "base", base).updateDecl(module, decl),
115118 .elf => return @fieldParentPtr(Elf, "base", base).updateDecl(module, decl),
116119 .macho => return @fieldParentPtr(MachO, "base", base).updateDecl(module, decl),
117120 .c => return @fieldParentPtr(C, "base", base).updateDecl(module, decl),
......@@ -121,6 +124,7 @@ pub const File = struct {
121124
122125 pub fn updateDeclLineNumber(base: *File, module: *Module, decl: *Module.Decl) !void {
123126 switch (base.tag) {
127 .coff => return @fieldParentPtr(Coff, "base", base).updateDeclLineNumber(module, decl),
124128 .elf => return @fieldParentPtr(Elf, "base", base).updateDeclLineNumber(module, decl),
125129 .macho => return @fieldParentPtr(MachO, "base", base).updateDeclLineNumber(module, decl),
126130 .c, .wasm => {},
......@@ -131,6 +135,7 @@ pub const File = struct {
131135 /// any given Decl.
132136 pub fn allocateDeclIndexes(base: *File, decl: *Module.Decl) !void {
133137 switch (base.tag) {
138 .coff => return @fieldParentPtr(Coff, "base", base).allocateDeclIndexes(decl),
134139 .elf => return @fieldParentPtr(Elf, "base", base).allocateDeclIndexes(decl),
135140 .macho => return @fieldParentPtr(MachO, "base", base).allocateDeclIndexes(decl),
136141 .c, .wasm => {},
......@@ -140,6 +145,7 @@ pub const File = struct {
140145 pub fn deinit(base: *File) void {
141146 if (base.file) |f| f.close();
142147 switch (base.tag) {
148 .coff => @fieldParentPtr(Coff, "base", base).deinit(),
143149 .elf => @fieldParentPtr(Elf, "base", base).deinit(),
144150 .macho => @fieldParentPtr(MachO, "base", base).deinit(),
145151 .c => @fieldParentPtr(C, "base", base).deinit(),
......@@ -149,6 +155,11 @@ pub const File = struct {
149155
150156 pub fn destroy(base: *File) void {
151157 switch (base.tag) {
158 .coff => {
159 const parent = @fieldParentPtr(Coff, "base", base);
160 parent.deinit();
161 base.allocator.destroy(parent);
162 },
152163 .elf => {
153164 const parent = @fieldParentPtr(Elf, "base", base);
154165 parent.deinit();
......@@ -177,6 +188,7 @@ pub const File = struct {
177188 defer tracy.end();
178189
179190 try switch (base.tag) {
191 .coff => @fieldParentPtr(Coff, "base", base).flush(module),
180192 .elf => @fieldParentPtr(Elf, "base", base).flush(module),
181193 .macho => @fieldParentPtr(MachO, "base", base).flush(module),
182194 .c => @fieldParentPtr(C, "base", base).flush(module),
......@@ -186,6 +198,7 @@ pub const File = struct {
186198
187199 pub fn freeDecl(base: *File, decl: *Module.Decl) void {
188200 switch (base.tag) {
201 .coff => @fieldParentPtr(Coff, "base", base).freeDecl(decl),
189202 .elf => @fieldParentPtr(Elf, "base", base).freeDecl(decl),
190203 .macho => @fieldParentPtr(MachO, "base", base).freeDecl(decl),
191204 .c => unreachable,
......@@ -195,6 +208,7 @@ pub const File = struct {
195208
196209 pub fn errorFlags(base: *File) ErrorFlags {
197210 return switch (base.tag) {
211 .coff => @fieldParentPtr(Coff, "base", base).error_flags,
198212 .elf => @fieldParentPtr(Elf, "base", base).error_flags,
199213 .macho => @fieldParentPtr(MachO, "base", base).error_flags,
200214 .c => return .{ .no_entry_point_found = false },
......@@ -211,6 +225,7 @@ pub const File = struct {
211225 exports: []const *Module.Export,
212226 ) !void {
213227 switch (base.tag) {
228 .coff => return @fieldParentPtr(Coff, "base", base).updateDeclExports(module, decl, exports),
214229 .elf => return @fieldParentPtr(Elf, "base", base).updateDeclExports(module, decl, exports),
215230 .macho => return @fieldParentPtr(MachO, "base", base).updateDeclExports(module, decl, exports),
216231 .c => return {},
......@@ -220,6 +235,7 @@ pub const File = struct {
220235
221236 pub fn getDeclVAddr(base: *File, decl: *const Module.Decl) u64 {
222237 switch (base.tag) {
238 .coff => return @fieldParentPtr(Coff, "base", base).getDeclVAddr(decl),
223239 .elf => return @fieldParentPtr(Elf, "base", base).getDeclVAddr(decl),
224240 .macho => return @fieldParentPtr(MachO, "base", base).getDeclVAddr(decl),
225241 .c => unreachable,
......@@ -228,6 +244,7 @@ pub const File = struct {
228244 }
229245
230246 pub const Tag = enum {
247 coff,
231248 elf,
232249 macho,
233250 c,
......@@ -239,6 +256,7 @@ pub const File = struct {
239256 };
240257
241258 pub const C = @import("link/C.zig");
259 pub const Coff = @import("link/Coff.zig");
242260 pub const Elf = @import("link/Elf.zig");
243261 pub const MachO = @import("link/MachO.zig");
244262 pub const Wasm = @import("link/Wasm.zig");
src-self-hosted/link/Coff.zig created+792
......@@ -0,0 +1,792 @@
1const Coff = @This();
2
3const std = @import("std");
4const log = std.log.scoped(.link);
5const Allocator = std.mem.Allocator;
6const assert = std.debug.assert;
7const fs = std.fs;
8
9const trace = @import("../tracy.zig").trace;
10const Module = @import("../Module.zig");
11const codegen = @import("../codegen.zig");
12const link = @import("../link.zig");
13
14const allocation_padding = 4 / 3;
15const minimum_text_block_size = 64 * allocation_padding;
16
17const section_alignment = 4096;
18const file_alignment = 512;
19const image_base = 0x400_000;
20const section_table_size = 2 * 40;
21comptime {
22 std.debug.assert(std.mem.isAligned(image_base, section_alignment));
23}
24
25pub const base_tag: link.File.Tag = .coff;
26
27const msdos_stub = @embedFile("msdos-stub.bin");
28
29base: link.File,
30ptr_width: enum { p32, p64 },
31error_flags: link.File.ErrorFlags = .{},
32
33text_block_free_list: std.ArrayListUnmanaged(*TextBlock) = .{},
34last_text_block: ?*TextBlock = null,
35
36/// Section table file pointer.
37section_table_offset: u32 = 0,
38/// Section data file pointer.
39section_data_offset: u32 = 0,
40/// Optiona header file pointer.
41optional_header_offset: u32 = 0,
42
43/// Absolute virtual address of the offset table when the executable is loaded in memory.
44offset_table_virtual_address: u32 = 0,
45/// Current size of the offset table on disk, must be a multiple of `file_alignment`
46offset_table_size: u32 = 0,
47/// Contains absolute virtual addresses
48offset_table: std.ArrayListUnmanaged(u64) = .{},
49/// Free list of offset table indices
50offset_table_free_list: std.ArrayListUnmanaged(u32) = .{},
51
52/// Virtual address of the entry point procedure relative to `image_base`
53entry_addr: ?u32 = null,
54
55/// Absolute virtual address of the text section when the executable is loaded in memory.
56text_section_virtual_address: u32 = 0,
57/// Current size of the `.text` section on disk, must be a multiple of `file_alignment`
58text_section_size: u32 = 0,
59
60offset_table_size_dirty: bool = false,
61text_section_size_dirty: bool = false,
62/// This flag is set when the virtual size of the whole image file when loaded in memory has changed
63/// and needs to be updated in the optional header.
64size_of_image_dirty: bool = false,
65
66pub const TextBlock = struct {
67 /// Offset of the code relative to the start of the text section
68 text_offset: u32,
69 /// Used size of the text block
70 size: u32,
71 /// This field is undefined for symbols with size = 0.
72 offset_table_index: u32,
73 /// Points to the previous and next neighbors, based on the `text_offset`.
74 /// This can be used to find, for example, the capacity of this `TextBlock`.
75 prev: ?*TextBlock,
76 next: ?*TextBlock,
77
78 pub const empty = TextBlock{
79 .text_offset = 0,
80 .size = 0,
81 .offset_table_index = undefined,
82 .prev = null,
83 .next = null,
84 };
85
86 /// Returns how much room there is to grow in virtual address space.
87 fn capacity(self: TextBlock) u64 {
88 if (self.next) |next| {
89 return next.text_offset - self.text_offset;
90 }
91 // This is the last block, the capacity is only limited by the address space.
92 return std.math.maxInt(u32) - self.text_offset;
93 }
94
95 fn freeListEligible(self: TextBlock) bool {
96 // No need to keep a free list node for the last block.
97 const next = self.next orelse return false;
98 const cap = next.text_offset - self.text_offset;
99 const ideal_cap = self.size * allocation_padding;
100 if (cap <= ideal_cap) return false;
101 const surplus = cap - ideal_cap;
102 return surplus >= minimum_text_block_size;
103 }
104
105 /// Absolute virtual address of the text block when the file is loaded in memory.
106 fn getVAddr(self: TextBlock, coff: Coff) u32 {
107 return coff.text_section_virtual_address + self.text_offset;
108 }
109};
110
111pub const SrcFn = void;
112
113pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, options: link.Options) !*link.File {
114 assert(options.object_format == .coff);
115
116 const file = try dir.createFile(sub_path, .{ .truncate = false, .read = true, .mode = link.determineMode(options) });
117 errdefer file.close();
118
119 var coff_file = try allocator.create(Coff);
120 errdefer allocator.destroy(coff_file);
121
122 coff_file.* = openFile(allocator, file, options) catch |err| switch (err) {
123 error.IncrFailed => try createFile(allocator, file, options),
124 else => |e| return e,
125 };
126
127 return &coff_file.base;
128}
129
130/// Returns error.IncrFailed if incremental update could not be performed.
131fn openFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff {
132 switch (options.output_mode) {
133 .Exe => {},
134 .Obj => return error.IncrFailed,
135 .Lib => return error.IncrFailed,
136 }
137 var self: Coff = .{
138 .base = .{
139 .file = file,
140 .tag = .coff,
141 .options = options,
142 .allocator = allocator,
143 },
144 .ptr_width = switch (options.target.cpu.arch.ptrBitWidth()) {
145 32 => .p32,
146 64 => .p64,
147 else => return error.UnsupportedELFArchitecture,
148 },
149 };
150 errdefer self.deinit();
151
152 // TODO implement reading the PE/COFF file
153 return error.IncrFailed;
154}
155
156/// Truncates the existing file contents and overwrites the contents.
157/// Returns an error if `file` is not already open with +read +write +seek abilities.
158fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff {
159 // TODO Write object specific relocations, COFF symbol table, then enable object file output.
160 switch (options.output_mode) {
161 .Exe => {},
162 .Obj => return error.TODOImplementWritingObjFiles,
163 .Lib => return error.TODOImplementWritingLibFiles,
164 }
165 var self: Coff = .{
166 .base = .{
167 .tag = .coff,
168 .options = options,
169 .allocator = allocator,
170 .file = file,
171 },
172 .ptr_width = switch (options.target.cpu.arch.ptrBitWidth()) {
173 32 => .p32,
174 64 => .p64,
175 else => return error.UnsupportedCOFFArchitecture,
176 },
177 };
178 errdefer self.deinit();
179
180 var coff_file_header_offset: u32 = 0;
181 if (options.output_mode == .Exe) {
182 // Write the MS-DOS stub and the PE signature
183 try self.base.file.?.pwriteAll(msdos_stub ++ "PE\x00\x00", 0);
184 coff_file_header_offset = msdos_stub.len + 4;
185 }
186
187 // COFF file header
188 const data_directory_count = 0;
189 var hdr_data: [112 + data_directory_count * 8 + section_table_size]u8 = undefined;
190 var index: usize = 0;
191
192 const machine = self.base.options.target.cpu.arch.toCoffMachine();
193 if (machine == .Unknown) {
194 return error.UnsupportedCOFFArchitecture;
195 }
196 std.mem.writeIntLittle(u16, hdr_data[0..2], @enumToInt(machine));
197 index += 2;
198
199 // Number of sections (we only use .got, .text)
200 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 2);
201 index += 2;
202 // TimeDateStamp (u32), PointerToSymbolTable (u32), NumberOfSymbols (u32)
203 std.mem.set(u8, hdr_data[index..][0..12], 0);
204 index += 12;
205
206 const optional_header_size = switch (options.output_mode) {
207 .Exe => data_directory_count * 8 + switch (self.ptr_width) {
208 .p32 => @as(u16, 96),
209 .p64 => 112,
210 },
211 else => 0,
212 };
213
214 const section_table_offset = coff_file_header_offset + 20 + optional_header_size;
215 const default_offset_table_size = file_alignment;
216 const default_size_of_code = 0;
217
218 self.section_data_offset = std.mem.alignForwardGeneric(u32, self.section_table_offset + section_table_size, file_alignment);
219 const section_data_relative_virtual_address = std.mem.alignForwardGeneric(u32, self.section_table_offset + section_table_size, section_alignment);
220 self.offset_table_virtual_address = image_base + section_data_relative_virtual_address;
221 self.offset_table_size = default_offset_table_size;
222 self.section_table_offset = section_table_offset;
223 self.text_section_virtual_address = image_base + section_data_relative_virtual_address + section_alignment;
224 self.text_section_size = default_size_of_code;
225
226 // Size of file when loaded in memory
227 const size_of_image = std.mem.alignForwardGeneric(u32, self.text_section_virtual_address - image_base + default_size_of_code, section_alignment);
228
229 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], optional_header_size);
230 index += 2;
231
232 // Characteristics
233 var characteristics: u16 = std.coff.IMAGE_FILE_DEBUG_STRIPPED | std.coff.IMAGE_FILE_RELOCS_STRIPPED; // TODO Remove debug info stripped flag when necessary
234 if (options.output_mode == .Exe) {
235 characteristics |= std.coff.IMAGE_FILE_EXECUTABLE_IMAGE;
236 }
237 switch (self.ptr_width) {
238 .p32 => characteristics |= std.coff.IMAGE_FILE_32BIT_MACHINE,
239 .p64 => characteristics |= std.coff.IMAGE_FILE_LARGE_ADDRESS_AWARE,
240 }
241 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], characteristics);
242 index += 2;
243
244 assert(index == 20);
245 try self.base.file.?.pwriteAll(hdr_data[0..index], coff_file_header_offset);
246
247 if (options.output_mode == .Exe) {
248 self.optional_header_offset = coff_file_header_offset + 20;
249 // Optional header
250 index = 0;
251 std.mem.writeIntLittle(u16, hdr_data[0..2], switch (self.ptr_width) {
252 .p32 => @as(u16, 0x10b),
253 .p64 => 0x20b,
254 });
255 index += 2;
256
257 // Linker version (u8 + u8)
258 std.mem.set(u8, hdr_data[index..][0..2], 0);
259 index += 2;
260
261 // SizeOfCode (UNUSED, u32), SizeOfInitializedData (u32), SizeOfUninitializedData (u32), AddressOfEntryPoint (u32), BaseOfCode (UNUSED, u32)
262 std.mem.set(u8, hdr_data[index..][0..20], 0);
263 index += 20;
264
265 if (self.ptr_width == .p32) {
266 // Base of data relative to the image base (UNUSED)
267 std.mem.set(u8, hdr_data[index..][0..4], 0);
268 index += 4;
269
270 // Image base address
271 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], image_base);
272 index += 4;
273 } else {
274 // Image base address
275 std.mem.writeIntLittle(u64, hdr_data[index..][0..8], image_base);
276 index += 8;
277 }
278
279 // Section alignment
280 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], section_alignment);
281 index += 4;
282 // File alignment
283 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], file_alignment);
284 index += 4;
285 // Required OS version, 6.0 is vista
286 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 6);
287 index += 2;
288 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 0);
289 index += 2;
290 // Image version
291 std.mem.set(u8, hdr_data[index..][0..4], 0);
292 index += 4;
293 // Required subsystem version, same as OS version
294 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 6);
295 index += 2;
296 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 0);
297 index += 2;
298 // Reserved zeroes (u32)
299 std.mem.set(u8, hdr_data[index..][0..4], 0);
300 index += 4;
301 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], size_of_image);
302 index += 4;
303 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], self.section_data_offset);
304 index += 4;
305 // CheckSum (u32)
306 std.mem.set(u8, hdr_data[index..][0..4], 0);
307 index += 4;
308 // Subsystem, TODO: Let users specify the subsystem, always CUI for now
309 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 3);
310 index += 2;
311 // DLL characteristics
312 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 0x0);
313 index += 2;
314
315 switch (self.ptr_width) {
316 .p32 => {
317 // Size of stack reserve + commit
318 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x1_000_000);
319 index += 4;
320 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x1_000);
321 index += 4;
322 // Size of heap reserve + commit
323 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x100_000);
324 index += 4;
325 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x1_000);
326 index += 4;
327 },
328 .p64 => {
329 // Size of stack reserve + commit
330 std.mem.writeIntLittle(u64, hdr_data[index..][0..8], 0x1_000_000);
331 index += 8;
332 std.mem.writeIntLittle(u64, hdr_data[index..][0..8], 0x1_000);
333 index += 8;
334 // Size of heap reserve + commit
335 std.mem.writeIntLittle(u64, hdr_data[index..][0..8], 0x100_000);
336 index += 8;
337 std.mem.writeIntLittle(u64, hdr_data[index..][0..8], 0x1_000);
338 index += 8;
339 },
340 }
341
342 // Reserved zeroes
343 std.mem.set(u8, hdr_data[index..][0..4], 0);
344 index += 4;
345
346 // Number of data directories
347 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], data_directory_count);
348 index += 4;
349 // Initialize data directories to zero
350 std.mem.set(u8, hdr_data[index..][0 .. data_directory_count * 8], 0);
351 index += data_directory_count * 8;
352
353 assert(index == optional_header_size);
354 }
355
356 // Write section table.
357 // First, the .got section
358 hdr_data[index..][0..8].* = ".got\x00\x00\x00\x00".*;
359 index += 8;
360 if (options.output_mode == .Exe) {
361 // Virtual size (u32)
362 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], default_offset_table_size);
363 index += 4;
364 // Virtual address (u32)
365 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], self.offset_table_virtual_address - image_base);
366 index += 4;
367 } else {
368 std.mem.set(u8, hdr_data[index..][0..8], 0);
369 index += 8;
370 }
371 // Size of raw data (u32)
372 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], default_offset_table_size);
373 index += 4;
374 // File pointer to the start of the section
375 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], self.section_data_offset);
376 index += 4;
377 // Pointer to relocations (u32), PointerToLinenumbers (u32), NumberOfRelocations (u16), NumberOfLinenumbers (u16)
378 std.mem.set(u8, hdr_data[index..][0..12], 0);
379 index += 12;
380 // Section flags
381 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], std.coff.IMAGE_SCN_CNT_INITIALIZED_DATA | std.coff.IMAGE_SCN_MEM_READ);
382 index += 4;
383 // Then, the .text section
384 hdr_data[index..][0..8].* = ".text\x00\x00\x00".*;
385 index += 8;
386 if (options.output_mode == .Exe) {
387 // Virtual size (u32)
388 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], default_size_of_code);
389 index += 4;
390 // Virtual address (u32)
391 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], self.text_section_virtual_address - image_base);
392 index += 4;
393 } else {
394 std.mem.set(u8, hdr_data[index..][0..8], 0);
395 index += 8;
396 }
397 // Size of raw data (u32)
398 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], default_size_of_code);
399 index += 4;
400 // File pointer to the start of the section
401 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], self.section_data_offset + default_offset_table_size);
402 index += 4;
403 // Pointer to relocations (u32), PointerToLinenumbers (u32), NumberOfRelocations (u16), NumberOfLinenumbers (u16)
404 std.mem.set(u8, hdr_data[index..][0..12], 0);
405 index += 12;
406 // Section flags
407 std.mem.writeIntLittle(
408 u32,
409 hdr_data[index..][0..4],
410 std.coff.IMAGE_SCN_CNT_CODE | std.coff.IMAGE_SCN_MEM_EXECUTE | std.coff.IMAGE_SCN_MEM_READ | std.coff.IMAGE_SCN_MEM_WRITE,
411 );
412 index += 4;
413
414 assert(index == optional_header_size + section_table_size);
415 try self.base.file.?.pwriteAll(hdr_data[0..index], self.optional_header_offset);
416 try self.base.file.?.setEndPos(self.section_data_offset + default_offset_table_size + default_size_of_code);
417
418 return self;
419}
420
421pub fn allocateDeclIndexes(self: *Coff, decl: *Module.Decl) !void {
422 try self.offset_table.ensureCapacity(self.base.allocator, self.offset_table.items.len + 1);
423
424 if (self.offset_table_free_list.popOrNull()) |i| {
425 decl.link.coff.offset_table_index = i;
426 } else {
427 decl.link.coff.offset_table_index = @intCast(u32, self.offset_table.items.len);
428 _ = self.offset_table.addOneAssumeCapacity();
429
430 const entry_size = self.base.options.target.cpu.arch.ptrBitWidth() / 8;
431 if (self.offset_table.items.len > self.offset_table_size / entry_size) {
432 self.offset_table_size_dirty = true;
433 }
434 }
435
436 self.offset_table.items[decl.link.coff.offset_table_index] = 0;
437}
438
439fn allocateTextBlock(self: *Coff, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 {
440 const new_block_min_capacity = new_block_size * allocation_padding;
441
442 // We use these to indicate our intention to update metadata, placing the new block,
443 // and possibly removing a free list node.
444 // It would be simpler to do it inside the for loop below, but that would cause a
445 // problem if an error was returned later in the function. So this action
446 // is actually carried out at the end of the function, when errors are no longer possible.
447 var block_placement: ?*TextBlock = null;
448 var free_list_removal: ?usize = null;
449
450 const vaddr = blk: {
451 var i: usize = 0;
452 while (i < self.text_block_free_list.items.len) {
453 const free_block = self.text_block_free_list.items[i];
454
455 const next_block_text_offset = free_block.text_offset + free_block.capacity();
456 const new_block_text_offset = std.mem.alignForwardGeneric(u64, free_block.getVAddr(self.*) + free_block.size, alignment) - self.text_section_virtual_address;
457 if (new_block_text_offset < next_block_text_offset and next_block_text_offset - new_block_text_offset >= new_block_min_capacity) {
458 block_placement = free_block;
459
460 const remaining_capacity = next_block_text_offset - new_block_text_offset - new_block_min_capacity;
461 if (remaining_capacity < minimum_text_block_size) {
462 free_list_removal = i;
463 }
464
465 break :blk new_block_text_offset + self.text_section_virtual_address;
466 } else {
467 if (!free_block.freeListEligible()) {
468 _ = self.text_block_free_list.swapRemove(i);
469 } else {
470 i += 1;
471 }
472 continue;
473 }
474 } else if (self.last_text_block) |last| {
475 const new_block_vaddr = std.mem.alignForwardGeneric(u64, last.getVAddr(self.*) + last.size, alignment);
476 block_placement = last;
477 break :blk new_block_vaddr;
478 } else {
479 break :blk self.text_section_virtual_address;
480 }
481 };
482
483 const expand_text_section = block_placement == null or block_placement.?.next == null;
484 if (expand_text_section) {
485 const needed_size = @intCast(u32, std.mem.alignForwardGeneric(u64, vaddr + new_block_size - self.text_section_virtual_address, file_alignment));
486 if (needed_size > self.text_section_size) {
487 const current_text_section_virtual_size = std.mem.alignForwardGeneric(u32, self.text_section_size, section_alignment);
488 const new_text_section_virtual_size = std.mem.alignForwardGeneric(u32, needed_size, section_alignment);
489 if (current_text_section_virtual_size != new_text_section_virtual_size) {
490 self.size_of_image_dirty = true;
491 // Write new virtual size
492 var buf: [4]u8 = undefined;
493 std.mem.writeIntLittle(u32, &buf, new_text_section_virtual_size);
494 try self.base.file.?.pwriteAll(&buf, self.section_table_offset + 40 + 8);
495 }
496
497 self.text_section_size = needed_size;
498 self.text_section_size_dirty = true;
499 }
500 self.last_text_block = text_block;
501 }
502 text_block.text_offset = @intCast(u32, vaddr - self.text_section_virtual_address);
503 text_block.size = @intCast(u32, new_block_size);
504
505 // This function can also reallocate a text block.
506 // In this case we need to "unplug" it from its previous location before
507 // plugging it in to its new location.
508 if (text_block.prev) |prev| {
509 prev.next = text_block.next;
510 }
511 if (text_block.next) |next| {
512 next.prev = text_block.prev;
513 }
514
515 if (block_placement) |big_block| {
516 text_block.prev = big_block;
517 text_block.next = big_block.next;
518 big_block.next = text_block;
519 } else {
520 text_block.prev = null;
521 text_block.next = null;
522 }
523 if (free_list_removal) |i| {
524 _ = self.text_block_free_list.swapRemove(i);
525 }
526 return vaddr;
527}
528
529fn growTextBlock(self: *Coff, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 {
530 const block_vaddr = text_block.getVAddr(self.*);
531 const align_ok = std.mem.alignBackwardGeneric(u64, block_vaddr, alignment) == block_vaddr;
532 const need_realloc = !align_ok or new_block_size > text_block.capacity();
533 if (!need_realloc) return @as(u64, block_vaddr);
534 return self.allocateTextBlock(text_block, new_block_size, alignment);
535}
536
537fn shrinkTextBlock(self: *Coff, text_block: *TextBlock, new_block_size: u64) void {
538 text_block.size = @intCast(u32, new_block_size);
539 if (text_block.capacity() - text_block.size >= minimum_text_block_size) {
540 self.text_block_free_list.append(self.base.allocator, text_block) catch {};
541 }
542}
543
544fn freeTextBlock(self: *Coff, text_block: *TextBlock) void {
545 var already_have_free_list_node = false;
546 {
547 var i: usize = 0;
548 // TODO turn text_block_free_list into a hash map
549 while (i < self.text_block_free_list.items.len) {
550 if (self.text_block_free_list.items[i] == text_block) {
551 _ = self.text_block_free_list.swapRemove(i);
552 continue;
553 }
554 if (self.text_block_free_list.items[i] == text_block.prev) {
555 already_have_free_list_node = true;
556 }
557 i += 1;
558 }
559 }
560 if (self.last_text_block == text_block) {
561 self.last_text_block = text_block.prev;
562 }
563 if (text_block.prev) |prev| {
564 prev.next = text_block.next;
565
566 if (!already_have_free_list_node and prev.freeListEligible()) {
567 // The free list is heuristics, it doesn't have to be perfect, so we can
568 // ignore the OOM here.
569 self.text_block_free_list.append(self.base.allocator, prev) catch {};
570 }
571 }
572
573 if (text_block.next) |next| {
574 next.prev = text_block.prev;
575 }
576}
577
578fn writeOffsetTableEntry(self: *Coff, index: usize) !void {
579 const entry_size = self.base.options.target.cpu.arch.ptrBitWidth() / 8;
580 const endian = self.base.options.target.cpu.arch.endian();
581
582 const offset_table_start = self.section_data_offset;
583 if (self.offset_table_size_dirty) {
584 const current_raw_size = self.offset_table_size;
585 const new_raw_size = self.offset_table_size * 2;
586 log.debug("growing offset table from raw size {} to {}\n", .{ current_raw_size, new_raw_size });
587
588 // Move the text section to a new place in the executable
589 const current_text_section_start = self.section_data_offset + current_raw_size;
590 const new_text_section_start = self.section_data_offset + new_raw_size;
591
592 const amt = try self.base.file.?.copyRangeAll(current_text_section_start, self.base.file.?, new_text_section_start, self.text_section_size);
593 if (amt != self.text_section_size) return error.InputOutput;
594
595 // Write the new raw size in the .got header
596 var buf: [8]u8 = undefined;
597 std.mem.writeIntLittle(u32, buf[0..4], new_raw_size);
598 try self.base.file.?.pwriteAll(buf[0..4], self.section_table_offset + 16);
599 // Write the new .text section file offset in the .text section header
600 std.mem.writeIntLittle(u32, buf[0..4], new_text_section_start);
601 try self.base.file.?.pwriteAll(buf[0..4], self.section_table_offset + 40 + 20);
602
603 const current_virtual_size = std.mem.alignForwardGeneric(u32, self.offset_table_size, section_alignment);
604 const new_virtual_size = std.mem.alignForwardGeneric(u32, new_raw_size, section_alignment);
605 // If we had to move in the virtual address space, we need to fix the VAs in the offset table, as well as the virtual address of the `.text` section
606 // and the virutal size of the `.got` section
607
608 if (new_virtual_size != current_virtual_size) {
609 log.debug("growing offset table from virtual size {} to {}\n", .{ current_virtual_size, new_virtual_size });
610 self.size_of_image_dirty = true;
611 const va_offset = new_virtual_size - current_virtual_size;
612
613 // Write .got virtual size
614 std.mem.writeIntLittle(u32, buf[0..4], new_virtual_size);
615 try self.base.file.?.pwriteAll(buf[0..4], self.section_table_offset + 8);
616
617 // Write .text new virtual address
618 self.text_section_virtual_address = self.text_section_virtual_address + va_offset;
619 std.mem.writeIntLittle(u32, buf[0..4], self.text_section_virtual_address - image_base);
620 try self.base.file.?.pwriteAll(buf[0..4], self.section_table_offset + 40 + 12);
621
622 // Fix the VAs in the offset table
623 for (self.offset_table.items) |*va, idx| {
624 if (va.* != 0) {
625 va.* += va_offset;
626
627 switch (entry_size) {
628 4 => {
629 std.mem.writeInt(u32, buf[0..4], @intCast(u32, va.*), endian);
630 try self.base.file.?.pwriteAll(buf[0..4], offset_table_start + idx * entry_size);
631 },
632 8 => {
633 std.mem.writeInt(u64, &buf, va.*, endian);
634 try self.base.file.?.pwriteAll(&buf, offset_table_start + idx * entry_size);
635 },
636 else => unreachable,
637 }
638 }
639 }
640 }
641 self.offset_table_size = new_raw_size;
642 self.offset_table_size_dirty = false;
643 }
644 // Write the new entry
645 switch (entry_size) {
646 4 => {
647 var buf: [4]u8 = undefined;
648 std.mem.writeInt(u32, &buf, @intCast(u32, self.offset_table.items[index]), endian);
649 try self.base.file.?.pwriteAll(&buf, offset_table_start + index * entry_size);
650 },
651 8 => {
652 var buf: [8]u8 = undefined;
653 std.mem.writeInt(u64, &buf, self.offset_table.items[index], endian);
654 try self.base.file.?.pwriteAll(&buf, offset_table_start + index * entry_size);
655 },
656 else => unreachable,
657 }
658}
659
660pub fn updateDecl(self: *Coff, module: *Module, decl: *Module.Decl) !void {
661 // TODO COFF/PE debug information
662 // TODO Implement exports
663 const tracy = trace(@src());
664 defer tracy.end();
665
666 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
667 defer code_buffer.deinit();
668
669 const typed_value = decl.typed_value.most_recent.typed_value;
670 const res = try codegen.generateSymbol(&self.base, decl.src(), typed_value, &code_buffer, .none);
671 const code = switch (res) {
672 .externally_managed => |x| x,
673 .appended => code_buffer.items,
674 .fail => |em| {
675 decl.analysis = .codegen_failure;
676 try module.failed_decls.put(module.gpa, decl, em);
677 return;
678 },
679 };
680
681 const required_alignment = typed_value.ty.abiAlignment(self.base.options.target);
682 const curr_size = decl.link.coff.size;
683 if (curr_size != 0) {
684 const capacity = decl.link.coff.capacity();
685 const need_realloc = code.len > capacity or
686 !std.mem.isAlignedGeneric(u32, decl.link.coff.text_offset, required_alignment);
687 if (need_realloc) {
688 const curr_vaddr = self.getDeclVAddr(decl);
689 const vaddr = try self.growTextBlock(&decl.link.coff, code.len, required_alignment);
690 log.debug("growing {} from 0x{x} to 0x{x}\n", .{ decl.name, curr_vaddr, vaddr });
691 if (vaddr != curr_vaddr) {
692 log.debug(" (writing new offset table entry)\n", .{});
693 self.offset_table.items[decl.link.coff.offset_table_index] = vaddr;
694 try self.writeOffsetTableEntry(decl.link.coff.offset_table_index);
695 }
696 } else if (code.len < curr_size) {
697 self.shrinkTextBlock(&decl.link.coff, code.len);
698 }
699 } else {
700 const vaddr = try self.allocateTextBlock(&decl.link.coff, code.len, required_alignment);
701 log.debug("allocated text block for {} at 0x{x} (size: {Bi})\n", .{ std.mem.spanZ(decl.name), vaddr, code.len });
702 errdefer self.freeTextBlock(&decl.link.coff);
703 self.offset_table.items[decl.link.coff.offset_table_index] = vaddr;
704 try self.writeOffsetTableEntry(decl.link.coff.offset_table_index);
705 }
706
707 // Write the code into the file
708 try self.base.file.?.pwriteAll(code, self.section_data_offset + self.offset_table_size + decl.link.coff.text_offset);
709
710 // Since we updated the vaddr and the size, each corresponding export symbol also needs to be updated.
711 const decl_exports = module.decl_exports.get(decl) orelse &[0]*Module.Export{};
712 return self.updateDeclExports(module, decl, decl_exports);
713}
714
715pub fn freeDecl(self: *Coff, decl: *Module.Decl) void {
716 // Appending to free lists is allowed to fail because the free lists are heuristics based anyway.
717 self.freeTextBlock(&decl.link.coff);
718 self.offset_table_free_list.append(self.base.allocator, decl.link.coff.offset_table_index) catch {};
719}
720
721pub fn updateDeclExports(self: *Coff, module: *Module, decl: *const Module.Decl, exports: []const *Module.Export) !void {
722 for (exports) |exp| {
723 if (exp.options.section) |section_name| {
724 if (!std.mem.eql(u8, section_name, ".text")) {
725 try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1);
726 module.failed_exports.putAssumeCapacityNoClobber(
727 exp,
728 try Module.ErrorMsg.create(self.base.allocator, 0, "Unimplemented: ExportOptions.section", .{}),
729 );
730 continue;
731 }
732 }
733 if (std.mem.eql(u8, exp.options.name, "_start")) {
734 self.entry_addr = decl.link.coff.getVAddr(self.*) - image_base;
735 } else {
736 try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1);
737 module.failed_exports.putAssumeCapacityNoClobber(
738 exp,
739 try Module.ErrorMsg.create(self.base.allocator, 0, "Unimplemented: Exports other than '_start'", .{}),
740 );
741 continue;
742 }
743 }
744}
745
746pub fn flush(self: *Coff, module: *Module) !void {
747 if (self.text_section_size_dirty) {
748 // Write the new raw size in the .text header
749 var buf: [4]u8 = undefined;
750 std.mem.writeIntLittle(u32, &buf, self.text_section_size);
751 try self.base.file.?.pwriteAll(&buf, self.section_table_offset + 40 + 16);
752 try self.base.file.?.setEndPos(self.section_data_offset + self.offset_table_size + self.text_section_size);
753 self.text_section_size_dirty = false;
754 }
755
756 if (self.base.options.output_mode == .Exe and self.size_of_image_dirty) {
757 const new_size_of_image = std.mem.alignForwardGeneric(u32, self.text_section_virtual_address - image_base + self.text_section_size, section_alignment);
758 var buf: [4]u8 = undefined;
759 std.mem.writeIntLittle(u32, &buf, new_size_of_image);
760 try self.base.file.?.pwriteAll(&buf, self.optional_header_offset + 56);
761 self.size_of_image_dirty = false;
762 }
763
764 if (self.entry_addr == null and self.base.options.output_mode == .Exe) {
765 log.debug("flushing. no_entry_point_found = true\n", .{});
766 self.error_flags.no_entry_point_found = true;
767 } else {
768 log.debug("flushing. no_entry_point_found = false\n", .{});
769 self.error_flags.no_entry_point_found = false;
770
771 if (self.base.options.output_mode == .Exe) {
772 // Write AddressOfEntryPoint
773 var buf: [4]u8 = undefined;
774 std.mem.writeIntLittle(u32, &buf, self.entry_addr.?);
775 try self.base.file.?.pwriteAll(&buf, self.optional_header_offset + 16);
776 }
777 }
778}
779
780pub fn getDeclVAddr(self: *Coff, decl: *const Module.Decl) u64 {
781 return self.text_section_virtual_address + decl.link.coff.text_offset;
782}
783
784pub fn updateDeclLineNumber(self: *Coff, module: *Module, decl: *Module.Decl) !void {
785 // TODO Implement this
786}
787
788pub fn deinit(self: *Coff) void {
789 self.text_block_free_list.deinit(self.base.allocator);
790 self.offset_table.deinit(self.base.allocator);
791 self.offset_table_free_list.deinit(self.base.allocator);
792}
src-self-hosted/link/Elf.zig+7-1
......@@ -1735,7 +1735,13 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {
17351735 } else {
17361736 // TODO implement .debug_info for global variables
17371737 }
1738 const res = try codegen.generateSymbol(&self.base, decl.src(), typed_value, &code_buffer, &dbg_line_buffer, &dbg_info_buffer, &dbg_info_type_relocs);
1738 const res = try codegen.generateSymbol(&self.base, decl.src(), typed_value, &code_buffer, .{
1739 .dwarf = .{
1740 .dbg_line = &dbg_line_buffer,
1741 .dbg_info = &dbg_info_buffer,
1742 .dbg_info_type_relocs = &dbg_info_type_relocs,
1743 },
1744 });
17391745 const code = switch (res) {
17401746 .externally_managed => |x| x,
17411747 .appended => code_buffer.items,
src-self-hosted/link/MachO.zig+1-24
......@@ -316,31 +316,8 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
316316 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
317317 defer code_buffer.deinit();
318318
319 var dbg_line_buffer = std.ArrayList(u8).init(self.base.allocator);
320 defer dbg_line_buffer.deinit();
321
322 var dbg_info_buffer = std.ArrayList(u8).init(self.base.allocator);
323 defer dbg_info_buffer.deinit();
324
325 var dbg_info_type_relocs: File.DbgInfoTypeRelocsTable = .{};
326 defer {
327 var it = dbg_info_type_relocs.iterator();
328 while (it.next()) |entry| {
329 entry.value.relocs.deinit(self.base.allocator);
330 }
331 dbg_info_type_relocs.deinit(self.base.allocator);
332 }
333
334319 const typed_value = decl.typed_value.most_recent.typed_value;
335 const res = try codegen.generateSymbol(
336 &self.base,
337 decl.src(),
338 typed_value,
339 &code_buffer,
340 &dbg_line_buffer,
341 &dbg_info_buffer,
342 &dbg_info_type_relocs,
343 );
320 const res = try codegen.generateSymbol(&self.base, decl.src(), typed_value, &code_buffer, .none);
344321
345322 const code = switch (res) {
346323 .externally_managed => |x| x,
src-self-hosted/link/msdos-stub.bin created
Binary files /dev/null and b/src-self-hosted/link/msdos-stub.bin differ
src-self-hosted/main.zig+16-7
......@@ -153,8 +153,8 @@ const usage_build_generic =
153153 \\ elf Executable and Linking Format
154154 \\ c Compile to C source code
155155 \\ wasm WebAssembly
156 \\ pe Portable Executable (Windows)
156157 \\ coff (planned) Common Object File Format (Windows)
157 \\ pe (planned) Portable Executable (Windows)
158158 \\ macho (planned) macOS relocatables
159159 \\ hex (planned) Intel IHEX
160160 \\ raw (planned) Dump machine code directly
......@@ -451,7 +451,7 @@ fn buildOutputType(
451451 } else if (mem.eql(u8, ofmt, "coff")) {
452452 break :blk .coff;
453453 } else if (mem.eql(u8, ofmt, "pe")) {
454 break :blk .coff;
454 break :blk .pe;
455455 } else if (mem.eql(u8, ofmt, "macho")) {
456456 break :blk .macho;
457457 } else if (mem.eql(u8, ofmt, "wasm")) {
......@@ -524,17 +524,19 @@ fn buildOutputType(
524524 try stderr.print("\nUnable to parse command: {}\n", .{@errorName(err)});
525525 continue;
526526 }) |line| {
527 if (mem.eql(u8, line, "update")) {
527 const actual_line = mem.trimRight(u8, line, "\r\n ");
528
529 if (mem.eql(u8, actual_line, "update")) {
528530 if (output_mode == .Exe) {
529531 try module.makeBinFileWritable();
530532 }
531533 try updateModule(gpa, &module, zir_out_path);
532 } else if (mem.eql(u8, line, "exit")) {
534 } else if (mem.eql(u8, actual_line, "exit")) {
533535 break;
534 } else if (mem.eql(u8, line, "help")) {
536 } else if (mem.eql(u8, actual_line, "help")) {
535537 try stderr.writeAll(repl_help);
536538 } else {
537 try stderr.print("unknown command: {}\n", .{line});
539 try stderr.print("unknown command: {}\n", .{actual_line});
538540 }
539541 } else {
540542 break;
......@@ -742,6 +744,7 @@ const FmtError = error{
742744 LinkQuotaExceeded,
743745 FileBusy,
744746 EndOfStream,
747 Unseekable,
745748 NotOpenForWriting,
746749} || fs.File.OpenError;
747750
......@@ -805,7 +808,13 @@ fn fmtPathFile(
805808 if (stat.kind == .Directory)
806809 return error.IsDir;
807810
808 const source_code = source_file.readAllAlloc(fmt.gpa, stat.size, max_src_size) catch |err| switch (err) {
811 const source_code = source_file.readToEndAllocOptions(
812 fmt.gpa,
813 max_src_size,
814 stat.size,
815 @alignOf(u8),
816 null,
817 ) catch |err| switch (err) {
809818 error.ConnectionResetByPeer => unreachable,
810819 error.ConnectionTimedOut => unreachable,
811820 error.NotOpenForReading => unreachable,
src-self-hosted/stage2.zig-1
......@@ -615,7 +615,6 @@ export fn stage2_libc_parse(stage1_libc: *Stage2LibCInstallation, libc_file_z: [
615615 error.NotOpenForWriting => unreachable,
616616 error.NotOpenForReading => unreachable,
617617 error.Unexpected => return .Unexpected,
618 error.EndOfStream => return .EndOfFile,
619618 error.IsDir => return .IsDir,
620619 error.ConnectionResetByPeer => unreachable,
621620 error.ConnectionTimedOut => unreachable,
src/analyze.cpp+1-1
......@@ -1810,7 +1810,7 @@ Error type_allowed_in_extern(CodeGen *g, ZigType *type_entry, bool *result) {
18101810ZigType *get_auto_err_set_type(CodeGen *g, ZigFn *fn_entry) {
18111811 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
18121812 buf_resize(&err_set_type->name, 0);
1813 buf_appendf(&err_set_type->name, "@TypeOf(%s).ReturnType.ErrorSet", buf_ptr(&fn_entry->symbol_name));
1813 buf_appendf(&err_set_type->name, "@typeInfo(@typeInfo(@TypeOf(%s)).Fn.return_type.?).ErrorUnion.error_set", buf_ptr(&fn_entry->symbol_name));
18141814 err_set_type->data.error_set.err_count = 0;
18151815 err_set_type->data.error_set.errors = nullptr;
18161816 err_set_type->data.error_set.infer_fn = fn_entry;
src/ir.cpp+3-161
......@@ -22836,167 +22836,9 @@ static IrInstGen *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstSrcFiel
2283622836 bool ptr_is_volatile = false;
2283722837 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base, const_val,
2283822838 err_set_type, ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
22839 } else if (child_type->id == ZigTypeIdInt) {
22840 if (buf_eql_str(field_name, "bit_count")) {
22841 bool ptr_is_const = true;
22842 bool ptr_is_volatile = false;
22843 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
22844 create_const_unsigned_negative(ira->codegen, ira->codegen->builtin_types.entry_num_lit_int,
22845 child_type->data.integral.bit_count, false),
22846 ira->codegen->builtin_types.entry_num_lit_int,
22847 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
22848 } else if (buf_eql_str(field_name, "is_signed")) {
22849 bool ptr_is_const = true;
22850 bool ptr_is_volatile = false;
22851 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
22852 create_const_bool(ira->codegen, child_type->data.integral.is_signed),
22853 ira->codegen->builtin_types.entry_bool,
22854 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
22855 } else {
22856 ir_add_error(ira, &field_ptr_instruction->base.base,
22857 buf_sprintf("type '%s' has no member called '%s'",
22858 buf_ptr(&child_type->name), buf_ptr(field_name)));
22859 return ira->codegen->invalid_inst_gen;
22860 }
22861 } else if (child_type->id == ZigTypeIdFloat) {
22862 if (buf_eql_str(field_name, "bit_count")) {
22863 bool ptr_is_const = true;
22864 bool ptr_is_volatile = false;
22865 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
22866 create_const_unsigned_negative(ira->codegen, ira->codegen->builtin_types.entry_num_lit_int,
22867 child_type->data.floating.bit_count, false),
22868 ira->codegen->builtin_types.entry_num_lit_int,
22869 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
22870 } else {
22871 ir_add_error(ira, &field_ptr_instruction->base.base,
22872 buf_sprintf("type '%s' has no member called '%s'",
22873 buf_ptr(&child_type->name), buf_ptr(field_name)));
22874 return ira->codegen->invalid_inst_gen;
22875 }
22876 } else if (child_type->id == ZigTypeIdPointer) {
22877 if (buf_eql_str(field_name, "Child")) {
22878 bool ptr_is_const = true;
22879 bool ptr_is_volatile = false;
22880 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
22881 create_const_type(ira->codegen, child_type->data.pointer.child_type),
22882 ira->codegen->builtin_types.entry_type,
22883 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
22884 } else if (buf_eql_str(field_name, "alignment")) {
22885 bool ptr_is_const = true;
22886 bool ptr_is_volatile = false;
22887 if ((err = type_resolve(ira->codegen, child_type->data.pointer.child_type,
22888 ResolveStatusAlignmentKnown)))
22889 {
22890 return ira->codegen->invalid_inst_gen;
22891 }
22892 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
22893 create_const_unsigned_negative(ira->codegen, ira->codegen->builtin_types.entry_num_lit_int,
22894 get_ptr_align(ira->codegen, child_type), false),
22895 ira->codegen->builtin_types.entry_num_lit_int,
22896 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
22897 } else {
22898 ir_add_error(ira, &field_ptr_instruction->base.base,
22899 buf_sprintf("type '%s' has no member called '%s'",
22900 buf_ptr(&child_type->name), buf_ptr(field_name)));
22901 return ira->codegen->invalid_inst_gen;
22902 }
22903 } else if (child_type->id == ZigTypeIdArray) {
22904 if (buf_eql_str(field_name, "Child")) {
22905 bool ptr_is_const = true;
22906 bool ptr_is_volatile = false;
22907 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
22908 create_const_type(ira->codegen, child_type->data.array.child_type),
22909 ira->codegen->builtin_types.entry_type,
22910 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
22911 } else if (buf_eql_str(field_name, "len")) {
22912 bool ptr_is_const = true;
22913 bool ptr_is_volatile = false;
22914 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
22915 create_const_unsigned_negative(ira->codegen, ira->codegen->builtin_types.entry_num_lit_int,
22916 child_type->data.array.len, false),
22917 ira->codegen->builtin_types.entry_num_lit_int,
22918 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
22919 } else {
22920 ir_add_error(ira, &field_ptr_instruction->base.base,
22921 buf_sprintf("type '%s' has no member called '%s'",
22922 buf_ptr(&child_type->name), buf_ptr(field_name)));
22923 return ira->codegen->invalid_inst_gen;
22924 }
22925 } else if (child_type->id == ZigTypeIdErrorUnion) {
22926 if (buf_eql_str(field_name, "Payload")) {
22927 bool ptr_is_const = true;
22928 bool ptr_is_volatile = false;
22929 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
22930 create_const_type(ira->codegen, child_type->data.error_union.payload_type),
22931 ira->codegen->builtin_types.entry_type,
22932 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
22933 } else if (buf_eql_str(field_name, "ErrorSet")) {
22934 bool ptr_is_const = true;
22935 bool ptr_is_volatile = false;
22936 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
22937 create_const_type(ira->codegen, child_type->data.error_union.err_set_type),
22938 ira->codegen->builtin_types.entry_type,
22939 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
22940 } else {
22941 ir_add_error(ira, &field_ptr_instruction->base.base,
22942 buf_sprintf("type '%s' has no member called '%s'",
22943 buf_ptr(&child_type->name), buf_ptr(field_name)));
22944 return ira->codegen->invalid_inst_gen;
22945 }
22946 } else if (child_type->id == ZigTypeIdOptional) {
22947 if (buf_eql_str(field_name, "Child")) {
22948 bool ptr_is_const = true;
22949 bool ptr_is_volatile = false;
22950 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
22951 create_const_type(ira->codegen, child_type->data.maybe.child_type),
22952 ira->codegen->builtin_types.entry_type,
22953 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
22954 } else {
22955 ir_add_error(ira, &field_ptr_instruction->base.base,
22956 buf_sprintf("type '%s' has no member called '%s'",
22957 buf_ptr(&child_type->name), buf_ptr(field_name)));
22958 return ira->codegen->invalid_inst_gen;
22959 }
22960 } else if (child_type->id == ZigTypeIdFn) {
22961 if (buf_eql_str(field_name, "ReturnType")) {
22962 if (child_type->data.fn.fn_type_id.return_type == nullptr) {
22963 // Return type can only ever be null, if the function is generic
22964 assert(child_type->data.fn.is_generic);
22965
22966 ir_add_error(ira, &field_ptr_instruction->base.base,
22967 buf_sprintf("ReturnType has not been resolved because '%s' is generic", buf_ptr(&child_type->name)));
22968 return ira->codegen->invalid_inst_gen;
22969 }
22970
22971 bool ptr_is_const = true;
22972 bool ptr_is_volatile = false;
22973 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
22974 create_const_type(ira->codegen, child_type->data.fn.fn_type_id.return_type),
22975 ira->codegen->builtin_types.entry_type,
22976 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
22977 } else if (buf_eql_str(field_name, "is_var_args")) {
22978 bool ptr_is_const = true;
22979 bool ptr_is_volatile = false;
22980 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
22981 create_const_bool(ira->codegen, child_type->data.fn.fn_type_id.is_var_args),
22982 ira->codegen->builtin_types.entry_bool,
22983 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
22984 } else if (buf_eql_str(field_name, "arg_count")) {
22985 bool ptr_is_const = true;
22986 bool ptr_is_volatile = false;
22987 return ir_get_const_ptr(ira, &field_ptr_instruction->base.base,
22988 create_const_usize(ira->codegen, child_type->data.fn.fn_type_id.param_count),
22989 ira->codegen->builtin_types.entry_usize,
22990 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile, 0);
22991 } else {
22992 ir_add_error(ira, &field_ptr_instruction->base.base,
22993 buf_sprintf("type '%s' has no member called '%s'",
22994 buf_ptr(&child_type->name), buf_ptr(field_name)));
22995 return ira->codegen->invalid_inst_gen;
22996 }
2299722839 } else {
2299822840 ir_add_error(ira, &field_ptr_instruction->base.base,
22999 buf_sprintf("type '%s' does not support field access", buf_ptr(&child_type->name)));
22841 buf_sprintf("type '%s' does not support field access", buf_ptr(&container_type->name)));
2300022842 return ira->codegen->invalid_inst_gen;
2300122843 }
2300222844 } else if (field_ptr_instruction->initializing) {
......@@ -26753,7 +26595,7 @@ static IrInstGen *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstSrcCmpxch
2675326595
2675426596 if (operand_type->id == ZigTypeIdFloat) {
2675526597 ir_add_error(ira, &instruction->type_value->child->base,
26756 buf_sprintf("expected integer, enum or pointer type, found '%s'", buf_ptr(&operand_type->name)));
26598 buf_sprintf("expected bool, integer, enum or pointer type, found '%s'", buf_ptr(&operand_type->name)));
2675726599 return ira->codegen->invalid_inst_gen;
2675826600 }
2675926601
......@@ -30408,7 +30250,7 @@ static ZigType *ir_resolve_atomic_operand_type(IrAnalyze *ira, IrInstGen *op) {
3040830250 return ira->codegen->builtin_types.entry_invalid;
3040930251 if (operand_ptr_type == nullptr) {
3041030252 ir_add_error(ira, &op->base,
30411 buf_sprintf("expected integer, float, enum or pointer type, found '%s'",
30253 buf_sprintf("expected bool, integer, float, enum or pointer type, found '%s'",
3041230254 buf_ptr(&operand_type->name)));
3041330255 return ira->codegen->builtin_types.entry_invalid;
3041430256 }
test/compile_errors.zig+8-17
......@@ -176,11 +176,11 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
176176 , &[_][]const u8{
177177 "tmp.zig:2:17: error: expected type 'u32', found 'error{Ohno}'",
178178 "tmp.zig:1:17: note: function cannot return an error",
179 "tmp.zig:8:5: error: expected type 'void', found '@TypeOf(bar).ReturnType.ErrorSet'",
179 "tmp.zig:8:5: error: expected type 'void', found '@typeInfo(@typeInfo(@TypeOf(bar)).Fn.return_type.?).ErrorUnion.error_set'",
180180 "tmp.zig:7:17: note: function cannot return an error",
181 "tmp.zig:11:15: error: expected type 'u32', found '@TypeOf(bar).ReturnType.ErrorSet!u32'",
181 "tmp.zig:11:15: error: expected type 'u32', found '@typeInfo(@typeInfo(@TypeOf(bar)).Fn.return_type.?).ErrorUnion.error_set!u32'",
182182 "tmp.zig:10:17: note: function cannot return an error",
183 "tmp.zig:15:14: error: expected type 'u32', found '@TypeOf(bar).ReturnType.ErrorSet!u32'",
183 "tmp.zig:15:14: error: expected type 'u32', found '@typeInfo(@typeInfo(@TypeOf(bar)).Fn.return_type.?).ErrorUnion.error_set!u32'",
184184 "tmp.zig:14:5: note: cannot store an error in type 'u32'",
185185 });
186186
......@@ -899,7 +899,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
899899 \\ _ = @cmpxchgWeak(f32, &x, 1, 2, .SeqCst, .SeqCst);
900900 \\}
901901 , &[_][]const u8{
902 "tmp.zig:3:22: error: expected integer, enum or pointer type, found 'f32'",
902 "tmp.zig:3:22: error: expected bool, integer, enum or pointer type, found 'f32'",
903903 });
904904
905905 cases.add("atomicrmw with float op not .Xchg, .Add or .Sub",
......@@ -1224,7 +1224,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
12241224 \\ };
12251225 \\}
12261226 , &[_][]const u8{
1227 "tmp.zig:11:25: error: expected type 'u32', found '@TypeOf(get_uval).ReturnType.ErrorSet!u32'",
1227 "tmp.zig:11:25: error: expected type 'u32', found '@typeInfo(@typeInfo(@TypeOf(get_uval)).Fn.return_type.?).ErrorUnion.error_set!u32'",
12281228 });
12291229
12301230 cases.add("assigning to struct or union fields that are not optionals with a function that returns an optional",
......@@ -1929,7 +1929,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
19291929 \\ const info = @TypeOf(slice).unknown;
19301930 \\}
19311931 , &[_][]const u8{
1932 "tmp.zig:3:32: error: type '[]i32' does not support field access",
1932 "tmp.zig:3:32: error: type 'type' does not support field access",
19331933 });
19341934
19351935 cases.add("peer cast then implicit cast const pointer to mutable C pointer",
......@@ -3542,7 +3542,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
35423542 \\ }
35433543 \\}
35443544 , &[_][]const u8{
3545 "tmp.zig:5:14: error: duplicate switch value: '@TypeOf(foo).ReturnType.ErrorSet.Foo'",
3545 "tmp.zig:5:14: error: duplicate switch value: '@typeInfo(@typeInfo(@TypeOf(foo)).Fn.return_type.?).ErrorUnion.error_set.Foo'",
35463546 "tmp.zig:3:14: note: other value is here",
35473547 });
35483548
......@@ -3674,7 +3674,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
36743674 \\ try foo();
36753675 \\}
36763676 , &[_][]const u8{
3677 "tmp.zig:5:5: error: cannot resolve inferred error set '@TypeOf(foo).ReturnType.ErrorSet': function 'foo' not fully analyzed yet",
3677 "tmp.zig:5:5: error: cannot resolve inferred error set '@typeInfo(@typeInfo(@TypeOf(foo)).Fn.return_type.?).ErrorUnion.error_set': function 'foo' not fully analyzed yet",
36783678 });
36793679
36803680 cases.add("implicit cast of error set not a subset",
......@@ -7206,15 +7206,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
72067206 "tmp.zig:7:24: error: accessing union field 'Bar' while field 'Baz' is set",
72077207 });
72087208
7209 cases.add("getting return type of generic function",
7210 \\fn generic(a: anytype) void {}
7211 \\comptime {
7212 \\ _ = @TypeOf(generic).ReturnType;
7213 \\}
7214 , &[_][]const u8{
7215 "tmp.zig:3:25: error: ReturnType has not been resolved because 'fn(anytype) anytype' is generic",
7216 });
7217
72187209 cases.add("unsupported modifier at start of asm output constraint",
72197210 \\export fn foo() void {
72207211 \\ var bar: u32 = 3;
test/stage1/behavior/align.zig+1-1
......@@ -5,7 +5,7 @@ const builtin = @import("builtin");
55var foo: u8 align(4) = 100;
66
77test "global variable alignment" {
8 comptime expect(@TypeOf(&foo).alignment == 4);
8 comptime expect(@typeInfo(@TypeOf(&foo)).Pointer.alignment == 4);
99 comptime expect(@TypeOf(&foo) == *align(4) u8);
1010 {
1111 const slice = @as(*[1]u8, &foo)[0..];
test/stage1/behavior/array.zig-10
......@@ -136,16 +136,6 @@ test "array literal with specified size" {
136136 expect(array[1] == 2);
137137}
138138
139test "array child property" {
140 var x: [5]i32 = undefined;
141 expect(@TypeOf(x).Child == i32);
142}
143
144test "array len property" {
145 var x: [5]i32 = undefined;
146 expect(@TypeOf(x).len == 5);
147}
148
149139test "array len field" {
150140 var arr = [4]u8{ 0, 0, 0, 0 };
151141 var ptr = &arr;
test/stage1/behavior/async_fn.zig+3-3
......@@ -331,7 +331,7 @@ test "async fn with inferred error set" {
331331 fn doTheTest() void {
332332 var frame: [1]@Frame(middle) = undefined;
333333 var fn_ptr = middle;
334 var result: @TypeOf(fn_ptr).ReturnType.ErrorSet!void = undefined;
334 var result: @typeInfo(@typeInfo(@TypeOf(fn_ptr)).Fn.return_type.?).ErrorUnion.error_set!void = undefined;
335335 _ = @asyncCall(std.mem.sliceAsBytes(frame[0..]), &result, fn_ptr, .{});
336336 resume global_frame;
337337 std.testing.expectError(error.Fail, result);
......@@ -950,7 +950,7 @@ test "@asyncCall with comptime-known function, but not awaited directly" {
950950
951951 fn doTheTest() void {
952952 var frame: [1]@Frame(middle) = undefined;
953 var result: @TypeOf(middle).ReturnType.ErrorSet!void = undefined;
953 var result: @typeInfo(@typeInfo(@TypeOf(middle)).Fn.return_type.?).ErrorUnion.error_set!void = undefined;
954954 _ = @asyncCall(std.mem.sliceAsBytes(frame[0..]), &result, middle, .{});
955955 resume global_frame;
956956 std.testing.expectError(error.Fail, result);
......@@ -1018,7 +1018,7 @@ test "@TypeOf an async function call of generic fn with error union type" {
10181018 const S = struct {
10191019 fn func(comptime x: anytype) anyerror!i32 {
10201020 const T = @TypeOf(async func(x));
1021 comptime expect(T == @TypeOf(@frame()).Child);
1021 comptime expect(T == @typeInfo(@TypeOf(@frame())).Pointer.child);
10221022 return undefined;
10231023 }
10241024 };
test/stage1/behavior/bit_shifting.zig+7-5
......@@ -2,16 +2,18 @@ const std = @import("std");
22const expect = std.testing.expect;
33
44fn ShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, comptime V: type) type {
5 expect(Key == std.meta.Int(false, Key.bit_count));
6 expect(Key.bit_count >= mask_bit_count);
5 const key_bits = @typeInfo(Key).Int.bits;
6 expect(Key == std.meta.Int(false, key_bits));
7 expect(key_bits >= mask_bit_count);
8 const shard_key_bits = mask_bit_count;
79 const ShardKey = std.meta.Int(false, mask_bit_count);
8 const shift_amount = Key.bit_count - ShardKey.bit_count;
10 const shift_amount = key_bits - shard_key_bits;
911 return struct {
1012 const Self = @This();
11 shards: [1 << ShardKey.bit_count]?*Node,
13 shards: [1 << shard_key_bits]?*Node,
1214
1315 pub fn create() Self {
14 return Self{ .shards = [_]?*Node{null} ** (1 << ShardKey.bit_count) };
16 return Self{ .shards = [_]?*Node{null} ** (1 << shard_key_bits) };
1517 }
1618
1719 fn getShardKey(key: Key) ShardKey {
test/stage1/behavior/bugs/5487.zig+2-2
......@@ -3,8 +3,8 @@ const io = @import("std").io;
33pub fn write(_: void, bytes: []const u8) !usize {
44 return 0;
55}
6pub fn outStream() io.OutStream(void, @TypeOf(write).ReturnType.ErrorSet, write) {
7 return io.OutStream(void, @TypeOf(write).ReturnType.ErrorSet, write){ .context = {} };
6pub fn outStream() io.OutStream(void, @typeInfo(@typeInfo(@TypeOf(write)).Fn.return_type.?).ErrorUnion.error_set, write) {
7 return io.OutStream(void, @typeInfo(@typeInfo(@TypeOf(write)).Fn.return_type.?).ErrorUnion.error_set, write){ .context = {} };
88}
99
1010test "crash" {
test/stage1/behavior/error.zig+2-2
......@@ -84,8 +84,8 @@ fn testErrorUnionType() void {
8484 const x: anyerror!i32 = 1234;
8585 if (x) |value| expect(value == 1234) else |_| unreachable;
8686 expect(@typeInfo(@TypeOf(x)) == .ErrorUnion);
87 expect(@typeInfo(@TypeOf(x).ErrorSet) == .ErrorSet);
88 expect(@TypeOf(x).ErrorSet == anyerror);
87 expect(@typeInfo(@typeInfo(@TypeOf(x)).ErrorUnion.error_set) == .ErrorSet);
88 expect(@typeInfo(@TypeOf(x)).ErrorUnion.error_set == anyerror);
8989}
9090
9191test "error set type" {
test/stage1/behavior/misc.zig-10
......@@ -24,12 +24,6 @@ test "call disabled extern fn" {
2424 disabledExternFn();
2525}
2626
27test "floating point primitive bit counts" {
28 expect(f16.bit_count == 16);
29 expect(f32.bit_count == 32);
30 expect(f64.bit_count == 64);
31}
32
3327test "short circuit" {
3428 testShortCircuit(false, true);
3529 comptime testShortCircuit(false, true);
......@@ -577,10 +571,6 @@ test "slice string literal has correct type" {
577571 comptime expect(@TypeOf(array[runtime_zero..]) == []const i32);
578572}
579573
580test "pointer child field" {
581 expect((*u32).Child == u32);
582}
583
584574test "struct inside function" {
585575 testStructInFn();
586576 comptime testStructInFn();
test/stage1/behavior/reflection.zig+7-15
......@@ -2,23 +2,15 @@ const expect = @import("std").testing.expect;
22const mem = @import("std").mem;
33const reflection = @This();
44
5test "reflection: array, pointer, optional, error union type child" {
6 comptime {
7 expect(([10]u8).Child == u8);
8 expect((*u8).Child == u8);
9 expect((anyerror!u8).Payload == u8);
10 expect((?u8).Child == u8);
11 }
12}
13
145test "reflection: function return type, var args, and param types" {
156 comptime {
16 expect(@TypeOf(dummy).ReturnType == i32);
17 expect(!@TypeOf(dummy).is_var_args);
18 expect(@TypeOf(dummy).arg_count == 3);
19 expect(@typeInfo(@TypeOf(dummy)).Fn.args[0].arg_type.? == bool);
20 expect(@typeInfo(@TypeOf(dummy)).Fn.args[1].arg_type.? == i32);
21 expect(@typeInfo(@TypeOf(dummy)).Fn.args[2].arg_type.? == f32);
7 const info = @typeInfo(@TypeOf(dummy)).Fn;
8 expect(info.return_type.? == i32);
9 expect(!info.is_var_args);
10 expect(info.args.len == 3);
11 expect(info.args[0].arg_type.? == bool);
12 expect(info.args[1].arg_type.? == i32);
13 expect(info.args[2].arg_type.? == f32);
2214 }
2315}
2416