Skip to content

Commit a072d82

Browse files
authored
Merge pull request #25592 from ziglang/init-std.Io
std: Introduce `Io` Interface
2 parents b2bc44e + 16185f6 commit a072d82

File tree

143 files changed

+17689
-9743
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

143 files changed

+17689
-9743
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,6 @@ set(ZIG_STAGE2_SOURCES
413413
lib/std/Thread/Futex.zig
414414
lib/std/Thread/Mutex.zig
415415
lib/std/Thread/Pool.zig
416-
lib/std/Thread/ResetEvent.zig
417416
lib/std/Thread/WaitGroup.zig
418417
lib/std/array_hash_map.zig
419418
lib/std/array_list.zig

README.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,15 @@ This produces a `zig2` executable in the current working directory. This is a
7676
[without LLVM extensions](https://github.com/ziglang/zig/issues/16270), and is
7777
therefore lacking these features:
7878
- Release mode optimizations
79-
- [aarch64 machine code backend](https://github.com/ziglang/zig/issues/21172)
80-
- [@cImport](https://github.com/ziglang/zig/issues/20630)
81-
- [zig translate-c](https://github.com/ziglang/zig/issues/20875)
82-
- [Ability to compile assembly files](https://github.com/ziglang/zig/issues/21169)
8379
- [Some ELF linking features](https://github.com/ziglang/zig/issues/17749)
84-
- [Most COFF/PE linking features](https://github.com/ziglang/zig/issues/17751)
80+
- [Some COFF/PE linking features](https://github.com/ziglang/zig/issues/17751)
8581
- [Some WebAssembly linking features](https://github.com/ziglang/zig/issues/17750)
86-
- [Ability to create import libs from def files](https://github.com/ziglang/zig/issues/17807)
8782
- [Ability to create static archives from object files](https://github.com/ziglang/zig/issues/9828)
83+
- [Ability to compile assembly files](https://github.com/ziglang/zig/issues/21169)
8884
- Ability to compile C, C++, Objective-C, and Objective-C++ files
8985

90-
However, a compiler built this way does provide a C backend, which may be
91-
useful for creating system packages of Zig projects using the system C
92-
toolchain. **In this case, LLVM is not needed!**
93-
94-
Furthermore, a compiler built this way provides an LLVM backend that produces
95-
bitcode files, which may be compiled into object files via a system Clang
86+
Even when built this way, Zig provides an LLVM backend that produces bitcode
87+
files, which may be optimized and compiled into object files via a system Clang
9688
package. This can be used to produce system packages of Zig applications
9789
without the Zig package dependency on LLVM.
9890

ci/x86_64-windows-debug.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Enter-VsDevShell -VsInstallPath "C:\Program Files (x86)\Microsoft Visual Studio\
9595
CheckLastExitCode
9696

9797
Write-Output "Build and run behavior tests with msvc..."
98-
& cl.exe -I..\lib test-x86_64-windows-msvc.c compiler_rt-x86_64-windows-msvc.c /W3 /Z7 -link -nologo -debug -subsystem:console kernel32.lib ntdll.lib libcmt.lib
98+
& cl.exe -I..\lib test-x86_64-windows-msvc.c compiler_rt-x86_64-windows-msvc.c /W3 /Z7 -link -nologo -debug -subsystem:console kernel32.lib ntdll.lib libcmt.lib ws2_32.lib
9999
CheckLastExitCode
100100

101101
& .\test-x86_64-windows-msvc.exe

ci/x86_64-windows-release.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Enter-VsDevShell -VsInstallPath "C:\Program Files (x86)\Microsoft Visual Studio\
113113
CheckLastExitCode
114114

115115
Write-Output "Build and run behavior tests with msvc..."
116-
& cl.exe -I..\lib test-x86_64-windows-msvc.c compiler_rt-x86_64-windows-msvc.c /W3 /Z7 -link -nologo -debug -subsystem:console kernel32.lib ntdll.lib libcmt.lib
116+
& cl.exe -I..\lib test-x86_64-windows-msvc.c compiler_rt-x86_64-windows-msvc.c /W3 /Z7 -link -nologo -debug -subsystem:console kernel32.lib ntdll.lib libcmt.lib ws2_32.lib
117117
CheckLastExitCode
118118

119119
& .\test-x86_64-windows-msvc.exe

lib/compiler/aro/aro/Compilation.zig

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("std");
2+
const Io = std.Io;
23
const assert = std.debug.assert;
34
const EpochSeconds = std.time.epoch.EpochSeconds;
45
const mem = std.mem;
@@ -113,7 +114,7 @@ pub const Environment = struct {
113114
if (parsed > max_timestamp) return error.InvalidEpoch;
114115
return .{ .provided = parsed };
115116
} else {
116-
const timestamp = std.math.cast(u64, std.time.timestamp()) orelse return error.InvalidEpoch;
117+
const timestamp = std.math.cast(u64, 0) orelse return error.InvalidEpoch;
117118
return .{ .system = std.math.clamp(timestamp, 0, max_timestamp) };
118119
}
119120
}
@@ -124,6 +125,7 @@ const Compilation = @This();
124125
gpa: Allocator,
125126
/// Allocations in this arena live all the way until `Compilation.deinit`.
126127
arena: Allocator,
128+
io: Io,
127129
diagnostics: *Diagnostics,
128130

129131
code_gen_options: CodeGenOptions = .default,
@@ -157,21 +159,23 @@ type_store: TypeStore = .{},
157159
ms_cwd_source_id: ?Source.Id = null,
158160
cwd: std.fs.Dir,
159161

160-
pub fn init(gpa: Allocator, arena: Allocator, diagnostics: *Diagnostics, cwd: std.fs.Dir) Compilation {
162+
pub fn init(gpa: Allocator, arena: Allocator, io: Io, diagnostics: *Diagnostics, cwd: std.fs.Dir) Compilation {
161163
return .{
162164
.gpa = gpa,
163165
.arena = arena,
166+
.io = io,
164167
.diagnostics = diagnostics,
165168
.cwd = cwd,
166169
};
167170
}
168171

169172
/// Initialize Compilation with default environment,
170173
/// pragma handlers and emulation mode set to target.
171-
pub fn initDefault(gpa: Allocator, arena: Allocator, diagnostics: *Diagnostics, cwd: std.fs.Dir) !Compilation {
174+
pub fn initDefault(gpa: Allocator, arena: Allocator, io: Io, diagnostics: *Diagnostics, cwd: std.fs.Dir) !Compilation {
172175
var comp: Compilation = .{
173176
.gpa = gpa,
174177
.arena = arena,
178+
.io = io,
175179
.diagnostics = diagnostics,
176180
.environment = try Environment.loadAll(gpa),
177181
.cwd = cwd,
@@ -222,14 +226,14 @@ pub const SystemDefinesMode = enum {
222226
include_system_defines,
223227
};
224228

225-
fn generateSystemDefines(comp: *Compilation, w: *std.Io.Writer) !void {
229+
fn generateSystemDefines(comp: *Compilation, w: *Io.Writer) !void {
226230
const define = struct {
227-
fn define(_w: *std.Io.Writer, name: []const u8) !void {
231+
fn define(_w: *Io.Writer, name: []const u8) !void {
228232
try _w.print("#define {s} 1\n", .{name});
229233
}
230234
}.define;
231235
const defineStd = struct {
232-
fn defineStd(_w: *std.Io.Writer, name: []const u8, is_gnu: bool) !void {
236+
fn defineStd(_w: *Io.Writer, name: []const u8, is_gnu: bool) !void {
233237
if (is_gnu) {
234238
try _w.print("#define {s} 1\n", .{name});
235239
}
@@ -956,7 +960,7 @@ fn generateSystemDefines(comp: *Compilation, w: *std.Io.Writer) !void {
956960
pub fn generateBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefinesMode) AddSourceError!Source {
957961
try comp.type_store.initNamedTypes(comp);
958962

959-
var allocating: std.Io.Writer.Allocating = try .initCapacity(comp.gpa, 2 << 13);
963+
var allocating: Io.Writer.Allocating = try .initCapacity(comp.gpa, 2 << 13);
960964
defer allocating.deinit();
961965

962966
comp.writeBuiltinMacros(system_defines_mode, &allocating.writer) catch |err| switch (err) {
@@ -970,7 +974,7 @@ pub fn generateBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefi
970974
return comp.addSourceFromOwnedBuffer("<builtin>", contents, .user);
971975
}
972976

973-
fn writeBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefinesMode, w: *std.Io.Writer) !void {
977+
fn writeBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefinesMode, w: *Io.Writer) !void {
974978
if (system_defines_mode == .include_system_defines) {
975979
try w.writeAll(
976980
\\#define __VERSION__ "Aro
@@ -1018,7 +1022,7 @@ fn writeBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefinesMode
10181022
}
10191023
}
10201024

1021-
fn generateFloatMacros(w: *std.Io.Writer, prefix: []const u8, semantics: target_util.FPSemantics, ext: []const u8) !void {
1025+
fn generateFloatMacros(w: *Io.Writer, prefix: []const u8, semantics: target_util.FPSemantics, ext: []const u8) !void {
10221026
const denormMin = semantics.chooseValue(
10231027
[]const u8,
10241028
.{
@@ -1093,7 +1097,7 @@ fn generateFloatMacros(w: *std.Io.Writer, prefix: []const u8, semantics: target_
10931097
try w.print("#define __{s}_MIN__ {s}{s}\n", .{ prefix, min, ext });
10941098
}
10951099

1096-
fn generateTypeMacro(comp: *const Compilation, w: *std.Io.Writer, name: []const u8, qt: QualType) !void {
1100+
fn generateTypeMacro(comp: *const Compilation, w: *Io.Writer, name: []const u8, qt: QualType) !void {
10971101
try w.print("#define {s} ", .{name});
10981102
try qt.print(comp, w);
10991103
try w.writeByte('\n');
@@ -1128,7 +1132,7 @@ fn generateFastOrLeastType(
11281132
bits: usize,
11291133
kind: enum { least, fast },
11301134
signedness: std.builtin.Signedness,
1131-
w: *std.Io.Writer,
1135+
w: *Io.Writer,
11321136
) !void {
11331137
const ty = comp.intLeastN(bits, signedness); // defining the fast types as the least types is permitted
11341138

@@ -1158,7 +1162,7 @@ fn generateFastOrLeastType(
11581162
try comp.generateFmt(prefix, w, ty);
11591163
}
11601164

1161-
fn generateFastAndLeastWidthTypes(comp: *Compilation, w: *std.Io.Writer) !void {
1165+
fn generateFastAndLeastWidthTypes(comp: *Compilation, w: *Io.Writer) !void {
11621166
const sizes = [_]usize{ 8, 16, 32, 64 };
11631167
for (sizes) |size| {
11641168
try comp.generateFastOrLeastType(size, .least, .signed, w);
@@ -1168,7 +1172,7 @@ fn generateFastAndLeastWidthTypes(comp: *Compilation, w: *std.Io.Writer) !void {
11681172
}
11691173
}
11701174

1171-
fn generateExactWidthTypes(comp: *Compilation, w: *std.Io.Writer) !void {
1175+
fn generateExactWidthTypes(comp: *Compilation, w: *Io.Writer) !void {
11721176
try comp.generateExactWidthType(w, .schar);
11731177

11741178
if (QualType.short.sizeof(comp) > QualType.char.sizeof(comp)) {
@@ -1216,7 +1220,7 @@ fn generateExactWidthTypes(comp: *Compilation, w: *std.Io.Writer) !void {
12161220
}
12171221
}
12181222

1219-
fn generateFmt(comp: *const Compilation, prefix: []const u8, w: *std.Io.Writer, qt: QualType) !void {
1223+
fn generateFmt(comp: *const Compilation, prefix: []const u8, w: *Io.Writer, qt: QualType) !void {
12201224
const unsigned = qt.signedness(comp) == .unsigned;
12211225
const modifier = qt.formatModifier(comp);
12221226
const formats = if (unsigned) "ouxX" else "di";
@@ -1225,15 +1229,15 @@ fn generateFmt(comp: *const Compilation, prefix: []const u8, w: *std.Io.Writer,
12251229
}
12261230
}
12271231

1228-
fn generateSuffixMacro(comp: *const Compilation, prefix: []const u8, w: *std.Io.Writer, qt: QualType) !void {
1232+
fn generateSuffixMacro(comp: *const Compilation, prefix: []const u8, w: *Io.Writer, qt: QualType) !void {
12291233
return w.print("#define {s}_C_SUFFIX__ {s}\n", .{ prefix, qt.intValueSuffix(comp) });
12301234
}
12311235

12321236
/// Generate the following for a type:
12331237
/// Name macro (e.g. #define __UINT32_TYPE__ unsigned int)
12341238
/// Format strings (e.g. #define __UINT32_FMTu__ "u")
12351239
/// Suffix macro (e.g. #define __UINT32_C_SUFFIX__ U)
1236-
fn generateExactWidthType(comp: *Compilation, w: *std.Io.Writer, original_qt: QualType) !void {
1240+
fn generateExactWidthType(comp: *Compilation, w: *Io.Writer, original_qt: QualType) !void {
12371241
var qt = original_qt;
12381242
const width = qt.sizeof(comp) * 8;
12391243
const unsigned = qt.signedness(comp) == .unsigned;
@@ -1266,7 +1270,7 @@ pub fn hasHalfPrecisionFloatABI(comp: *const Compilation) bool {
12661270
return comp.langopts.allow_half_args_and_returns or target_util.hasHalfPrecisionFloatABI(comp.target);
12671271
}
12681272

1269-
fn generateIntMax(comp: *const Compilation, w: *std.Io.Writer, name: []const u8, qt: QualType) !void {
1273+
fn generateIntMax(comp: *const Compilation, w: *Io.Writer, name: []const u8, qt: QualType) !void {
12701274
const unsigned = qt.signedness(comp) == .unsigned;
12711275
const max: u128 = switch (qt.bitSizeof(comp)) {
12721276
8 => if (unsigned) std.math.maxInt(u8) else std.math.maxInt(i8),
@@ -1290,7 +1294,7 @@ pub fn wcharMax(comp: *const Compilation) u32 {
12901294
};
12911295
}
12921296

1293-
fn generateExactWidthIntMax(comp: *Compilation, w: *std.Io.Writer, original_qt: QualType) !void {
1297+
fn generateExactWidthIntMax(comp: *Compilation, w: *Io.Writer, original_qt: QualType) !void {
12941298
var qt = original_qt;
12951299
const bit_count: u8 = @intCast(qt.sizeof(comp) * 8);
12961300
const unsigned = qt.signedness(comp) == .unsigned;
@@ -1307,16 +1311,16 @@ fn generateExactWidthIntMax(comp: *Compilation, w: *std.Io.Writer, original_qt:
13071311
return comp.generateIntMax(w, name, qt);
13081312
}
13091313

1310-
fn generateIntWidth(comp: *Compilation, w: *std.Io.Writer, name: []const u8, qt: QualType) !void {
1314+
fn generateIntWidth(comp: *Compilation, w: *Io.Writer, name: []const u8, qt: QualType) !void {
13111315
try w.print("#define __{s}_WIDTH__ {d}\n", .{ name, qt.sizeof(comp) * 8 });
13121316
}
13131317

1314-
fn generateIntMaxAndWidth(comp: *Compilation, w: *std.Io.Writer, name: []const u8, qt: QualType) !void {
1318+
fn generateIntMaxAndWidth(comp: *Compilation, w: *Io.Writer, name: []const u8, qt: QualType) !void {
13151319
try comp.generateIntMax(w, name, qt);
13161320
try comp.generateIntWidth(w, name, qt);
13171321
}
13181322

1319-
fn generateSizeofType(comp: *Compilation, w: *std.Io.Writer, name: []const u8, qt: QualType) !void {
1323+
fn generateSizeofType(comp: *Compilation, w: *Io.Writer, name: []const u8, qt: QualType) !void {
13201324
try w.print("#define {s} {d}\n", .{ name, qt.sizeof(comp) });
13211325
}
13221326

@@ -1797,7 +1801,7 @@ pub const IncludeType = enum {
17971801
angle_brackets,
17981802
};
17991803

1800-
fn getPathContents(comp: *Compilation, path: []const u8, limit: std.Io.Limit) ![]u8 {
1804+
fn getPathContents(comp: *Compilation, path: []const u8, limit: Io.Limit) ![]u8 {
18011805
if (mem.indexOfScalar(u8, path, 0) != null) {
18021806
return error.FileNotFound;
18031807
}
@@ -1807,11 +1811,12 @@ fn getPathContents(comp: *Compilation, path: []const u8, limit: std.Io.Limit) ![
18071811
return comp.getFileContents(file, limit);
18081812
}
18091813

1810-
fn getFileContents(comp: *Compilation, file: std.fs.File, limit: std.Io.Limit) ![]u8 {
1814+
fn getFileContents(comp: *Compilation, file: std.fs.File, limit: Io.Limit) ![]u8 {
1815+
const io = comp.io;
18111816
var file_buf: [4096]u8 = undefined;
1812-
var file_reader = file.reader(&file_buf);
1817+
var file_reader = file.reader(io, &file_buf);
18131818

1814-
var allocating: std.Io.Writer.Allocating = .init(comp.gpa);
1819+
var allocating: Io.Writer.Allocating = .init(comp.gpa);
18151820
defer allocating.deinit();
18161821
if (file_reader.getSize()) |size| {
18171822
const limited_size = limit.minInt64(size);
@@ -1838,7 +1843,7 @@ pub fn findEmbed(
18381843
includer_token_source: Source.Id,
18391844
/// angle bracket vs quotes
18401845
include_type: IncludeType,
1841-
limit: std.Io.Limit,
1846+
limit: Io.Limit,
18421847
opt_dep_file: ?*DepFile,
18431848
) !?[]u8 {
18441849
if (std.fs.path.isAbsolute(filename)) {
@@ -2002,8 +2007,7 @@ pub fn locSlice(comp: *const Compilation, loc: Source.Location) []const u8 {
20022007
pub fn getSourceMTimeUncached(comp: *const Compilation, source_id: Source.Id) ?u64 {
20032008
const source = comp.getSource(source_id);
20042009
if (comp.cwd.statFile(source.path)) |stat| {
2005-
const mtime = @divTrunc(stat.mtime, std.time.ns_per_s);
2006-
return std.math.cast(u64, mtime);
2010+
return std.math.cast(u64, stat.mtime.toSeconds());
20072011
} else |_| {
20082012
return null;
20092013
}

lib/compiler/aro/aro/Driver.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ pub fn parseArgs(
273273
macro_buf: *std.ArrayList(u8),
274274
args: []const []const u8,
275275
) (Compilation.Error || std.Io.Writer.Error)!bool {
276+
const io = d.comp.io;
276277
var i: usize = 1;
277278
var comment_arg: []const u8 = "";
278279
var hosted: ?bool = null;
@@ -772,7 +773,7 @@ pub fn parseArgs(
772773
opts.arch_os_abi, @errorName(e),
773774
}),
774775
};
775-
d.comp.target = std.zig.system.resolveTargetQuery(query) catch |e| {
776+
d.comp.target = std.zig.system.resolveTargetQuery(io, query) catch |e| {
776777
return d.fatal("unable to resolve target: {s}", .{errorDescription(e)});
777778
};
778779
}
@@ -916,8 +917,7 @@ pub fn errorDescription(e: anyerror) []const u8 {
916917
error.NotDir => "is not a directory",
917918
error.NotOpenForReading => "file is not open for reading",
918919
error.NotOpenForWriting => "file is not open for writing",
919-
error.InvalidUtf8 => "path is not valid UTF-8",
920-
error.InvalidWtf8 => "path is not valid WTF-8",
920+
error.BadPathName => "bad path name",
921921
error.FileBusy => "file is busy",
922922
error.NameTooLong => "file name is too long",
923923
error.AccessDenied => "access denied",

0 commit comments

Comments
 (0)