authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2019-11-06 21:29:18+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2019-11-07 10:30:37+02:00
log6dd4a276de7ea7c283d1d1ef6361a718225022d3
tree589377b21fa3cb3b54b0f6c8a1966c607cf41c2b
parent110e575497595330ac94b4cc57c0f6dc47c9f519
signaturelock-open Commit is signed but in an unrecognized format.

self hosted compiler: update to new std.event


7 files changed, 136 insertions(+), 159 deletions(-)

src-self-hosted/codegen.zig+1-1
......@@ -79,7 +79,7 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
7979 .builder = builder,
8080 .dibuilder = dibuilder,
8181 .context = context,
82 .lock = event.Lock.init(comp.loop),
82 .lock = event.Lock.init(),
8383 .arena = &code.arena.allocator,
8484 };
8585
src-self-hosted/compilation.zig+52-55
......@@ -35,9 +35,9 @@ const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
3535
3636/// Data that is local to the event loop.
3737pub const ZigCompiler = struct {
38 loop: *event.Loop,
3938 llvm_handle_pool: std.atomic.Stack(*llvm.Context),
4039 lld_lock: event.Lock,
40 allocator: *Allocator,
4141
4242 /// TODO pool these so that it doesn't have to lock
4343 prng: event.Locked(std.rand.DefaultPrng),
......@@ -46,7 +46,7 @@ pub const ZigCompiler = struct {
4646
4747 var lazy_init_targets = std.lazyInit(void);
4848
49 pub fn init(loop: *event.Loop) !ZigCompiler {
49 pub fn init(allocator: *Allocator) !ZigCompiler {
5050 lazy_init_targets.get() orelse {
5151 Target.initializeAll();
5252 lazy_init_targets.resolve();
......@@ -57,11 +57,11 @@ pub const ZigCompiler = struct {
5757 const seed = mem.readIntNative(u64, &seed_bytes);
5858
5959 return ZigCompiler{
60 .loop = loop,
61 .lld_lock = event.Lock.init(loop),
60 .allocator = allocator,
61 .lld_lock = event.Lock.init(),
6262 .llvm_handle_pool = std.atomic.Stack(*llvm.Context).init(),
63 .prng = event.Locked(std.rand.DefaultPrng).init(loop, std.rand.DefaultPrng.init(seed)),
64 .native_libc = event.Future(LibCInstallation).init(loop),
63 .prng = event.Locked(std.rand.DefaultPrng).init(std.rand.DefaultPrng.init(seed)),
64 .native_libc = event.Future(LibCInstallation).init(),
6565 };
6666 }
6767
......@@ -70,7 +70,7 @@ pub const ZigCompiler = struct {
7070 self.lld_lock.deinit();
7171 while (self.llvm_handle_pool.pop()) |node| {
7272 llvm.ContextDispose(node.data);
73 self.loop.allocator.destroy(node);
73 self.allocator.destroy(node);
7474 }
7575 }
7676
......@@ -82,19 +82,19 @@ pub const ZigCompiler = struct {
8282 const context_ref = llvm.ContextCreate() orelse return error.OutOfMemory;
8383 errdefer llvm.ContextDispose(context_ref);
8484
85 const node = try self.loop.allocator.create(std.atomic.Stack(*llvm.Context).Node);
85 const node = try self.allocator.create(std.atomic.Stack(*llvm.Context).Node);
8686 node.* = std.atomic.Stack(*llvm.Context).Node{
8787 .next = undefined,
8888 .data = context_ref,
8989 };
90 errdefer self.loop.allocator.destroy(node);
90 errdefer self.allocator.destroy(node);
9191
9292 return LlvmHandle{ .node = node };
9393 }
9494
9595 pub async fn getNativeLibC(self: *ZigCompiler) !*LibCInstallation {
9696 if (self.native_libc.start()) |ptr| return ptr;
97 try self.native_libc.data.findNative(self.loop);
97 try self.native_libc.data.findNative(self.allocator);
9898 self.native_libc.resolve();
9999 return &self.native_libc.data;
100100 }
......@@ -122,7 +122,6 @@ pub const LlvmHandle = struct {
122122
123123pub const Compilation = struct {
124124 zig_compiler: *ZigCompiler,
125 loop: *event.Loop,
126125 name: Buffer,
127126 llvm_triple: Buffer,
128127 root_src_path: ?[]const u8,
......@@ -228,7 +227,7 @@ pub const Compilation = struct {
228227 deinit_group: event.Group(void),
229228
230229 // destroy_frame: @Frame(createAsync),
231 main_loop_frame: @Frame(Compilation.mainLoop),
230 // main_loop_frame: @Frame(Compilation.mainLoop),
232231 main_loop_future: event.Future(void),
233232
234233 have_err_ret_tracing: bool,
......@@ -348,7 +347,7 @@ pub const Compilation = struct {
348347 zig_lib_dir: []const u8,
349348 ) !*Compilation {
350349 var optional_comp: ?*Compilation = null;
351 const frame = async createAsync(
350 var frame = async createAsync(
352351 &optional_comp,
353352 zig_compiler,
354353 name,
......@@ -359,7 +358,7 @@ pub const Compilation = struct {
359358 is_static,
360359 zig_lib_dir,
361360 );
362 return optional_comp orelse await frame;
361 return optional_comp orelse if (await frame) |_| unreachable else |err| err;
363362 }
364363
365364 async fn createAsync(
......@@ -374,10 +373,9 @@ pub const Compilation = struct {
374373 zig_lib_dir: []const u8,
375374 ) !void {
376375
377 const loop = zig_compiler.loop;
376 const allocator = zig_compiler.allocator;
378377 var comp = Compilation{
379 .loop = loop,
380 .arena_allocator = std.heap.ArenaAllocator.init(loop.allocator),
378 .arena_allocator = std.heap.ArenaAllocator.init(allocator),
381379 .zig_compiler = zig_compiler,
382380 .events = undefined,
383381 .root_src_path = root_src_path,
......@@ -387,10 +385,10 @@ pub const Compilation = struct {
387385 .build_mode = build_mode,
388386 .zig_lib_dir = zig_lib_dir,
389387 .zig_std_dir = undefined,
390 .tmp_dir = event.Future(BuildError![]u8).init(loop),
391 .destroy_frame = @frame(),
392 .main_loop_frame = undefined,
393 .main_loop_future = event.Future(void).init(loop),
388 .tmp_dir = event.Future(BuildError![]u8).init(),
389 // .destroy_frame = @frame(),
390 // .main_loop_frame = undefined,
391 .main_loop_future = event.Future(void).init(),
394392
395393 .name = undefined,
396394 .llvm_triple = undefined,
......@@ -419,7 +417,7 @@ pub const Compilation = struct {
419417 .rpath_list = [_][]const u8{},
420418 .assembly_files = [_][]const u8{},
421419 .link_objects = [_][]const u8{},
422 .fn_link_set = event.Locked(FnLinkSet).init(loop, FnLinkSet.init()),
420 .fn_link_set = event.Locked(FnLinkSet).init(FnLinkSet.init()),
423421 .windows_subsystem_windows = false,
424422 .windows_subsystem_console = false,
425423 .link_libs_list = undefined,
......@@ -431,14 +429,14 @@ pub const Compilation = struct {
431429 .test_name_prefix = null,
432430 .emit_file_type = Emit.Binary,
433431 .link_out_file = null,
434 .exported_symbol_names = event.Locked(Decl.Table).init(loop, Decl.Table.init(loop.allocator)),
435 .prelink_group = event.Group(BuildError!void).init(loop),
436 .deinit_group = event.Group(void).init(loop),
437 .compile_errors = event.Locked(CompileErrList).init(loop, CompileErrList.init(loop.allocator)),
438 .int_type_table = event.Locked(IntTypeTable).init(loop, IntTypeTable.init(loop.allocator)),
439 .array_type_table = event.Locked(ArrayTypeTable).init(loop, ArrayTypeTable.init(loop.allocator)),
440 .ptr_type_table = event.Locked(PtrTypeTable).init(loop, PtrTypeTable.init(loop.allocator)),
441 .fn_type_table = event.Locked(FnTypeTable).init(loop, FnTypeTable.init(loop.allocator)),
432 .exported_symbol_names = event.Locked(Decl.Table).init(Decl.Table.init(allocator)),
433 .prelink_group = event.Group(BuildError!void).init(allocator),
434 .deinit_group = event.Group(void).init(allocator),
435 .compile_errors = event.Locked(CompileErrList).init(CompileErrList.init(allocator)),
436 .int_type_table = event.Locked(IntTypeTable).init(IntTypeTable.init(allocator)),
437 .array_type_table = event.Locked(ArrayTypeTable).init(ArrayTypeTable.init(allocator)),
438 .ptr_type_table = event.Locked(PtrTypeTable).init(PtrTypeTable.init(allocator)),
439 .fn_type_table = event.Locked(FnTypeTable).init(FnTypeTable.init(allocator)),
442440 .c_int_types = undefined,
443441
444442 .meta_type = undefined,
......@@ -519,8 +517,8 @@ pub const Compilation = struct {
519517 comp.target_layout_str = llvm.CopyStringRepOfTargetData(comp.target_data_ref) orelse return error.OutOfMemory;
520518 defer llvm.DisposeMessage(comp.target_layout_str);
521519
522 comp.events = try event.Channel(Event).create(comp.loop, 0);
523 defer comp.events.destroy();
520 comp.events.init([0]Event{});
521 defer comp.events.deinit();
524522
525523 if (root_src_path) |root_src| {
526524 const dirname = std.fs.path.dirname(root_src) orelse ".";
......@@ -533,13 +531,13 @@ pub const Compilation = struct {
533531 comp.root_package = try Package.create(comp.arena(), ".", "");
534532 }
535533
536 comp.fs_watch = try fs.Watch(*Scope.Root).create(loop, 16);
534 comp.fs_watch = try fs.Watch(*Scope.Root).create(16);
537535 defer comp.fs_watch.destroy();
538536
539537 try comp.initTypes();
540538 defer comp.primitive_type_table.deinit();
541539
542 comp.main_loop_frame = async comp.mainLoop() catch unreachable;
540 // comp.main_loop_frame = async comp.mainLoop();
543541 // Set this to indicate that initialization completed successfully.
544542 // from here on out we must not return an error.
545543 // This must occur before the first suspend/await.
......@@ -552,7 +550,7 @@ pub const Compilation = struct {
552550
553551 if (comp.tmp_dir.getOrNull()) |tmp_dir_result| if (tmp_dir_result.*) |tmp_dir| {
554552 // TODO evented I/O?
555 std.fs.deleteTree(comp.arena(), tmp_dir) catch {};
553 std.fs.deleteTree(tmp_dir) catch {};
556554 } else |_| {};
557555 }
558556
......@@ -601,7 +599,7 @@ pub const Compilation = struct {
601599 .ref_count = std.atomic.Int(usize).init(3), // 3 because it references itself twice
602600 },
603601 .id = builtin.TypeId.Type,
604 .abi_alignment = Type.AbiAlignment.init(comp.loop),
602 .abi_alignment = Type.AbiAlignment.init(),
605603 },
606604 .value = undefined,
607605 };
......@@ -619,7 +617,7 @@ pub const Compilation = struct {
619617 .ref_count = std.atomic.Int(usize).init(1),
620618 },
621619 .id = builtin.TypeId.Void,
622 .abi_alignment = Type.AbiAlignment.init(comp.loop),
620 .abi_alignment = Type.AbiAlignment.init(),
623621 },
624622 };
625623 assert((try comp.primitive_type_table.put(comp.void_type.base.name, &comp.void_type.base)) == null);
......@@ -634,7 +632,7 @@ pub const Compilation = struct {
634632 .ref_count = std.atomic.Int(usize).init(1),
635633 },
636634 .id = builtin.TypeId.NoReturn,
637 .abi_alignment = Type.AbiAlignment.init(comp.loop),
635 .abi_alignment = Type.AbiAlignment.init(),
638636 },
639637 };
640638 assert((try comp.primitive_type_table.put(comp.noreturn_type.base.name, &comp.noreturn_type.base)) == null);
......@@ -649,7 +647,7 @@ pub const Compilation = struct {
649647 .ref_count = std.atomic.Int(usize).init(1),
650648 },
651649 .id = builtin.TypeId.ComptimeInt,
652 .abi_alignment = Type.AbiAlignment.init(comp.loop),
650 .abi_alignment = Type.AbiAlignment.init(),
653651 },
654652 };
655653 assert((try comp.primitive_type_table.put(comp.comptime_int_type.base.name, &comp.comptime_int_type.base)) == null);
......@@ -664,7 +662,7 @@ pub const Compilation = struct {
664662 .ref_count = std.atomic.Int(usize).init(1),
665663 },
666664 .id = builtin.TypeId.Bool,
667 .abi_alignment = Type.AbiAlignment.init(comp.loop),
665 .abi_alignment = Type.AbiAlignment.init(),
668666 },
669667 };
670668 assert((try comp.primitive_type_table.put(comp.bool_type.base.name, &comp.bool_type.base)) == null);
......@@ -718,7 +716,7 @@ pub const Compilation = struct {
718716 .ref_count = std.atomic.Int(usize).init(1),
719717 },
720718 .id = builtin.TypeId.Int,
721 .abi_alignment = Type.AbiAlignment.init(comp.loop),
719 .abi_alignment = Type.AbiAlignment.init(),
722720 },
723721 .key = Type.Int.Key{
724722 .is_signed = cint.is_signed,
......@@ -739,7 +737,7 @@ pub const Compilation = struct {
739737 .ref_count = std.atomic.Int(usize).init(1),
740738 },
741739 .id = builtin.TypeId.Int,
742 .abi_alignment = Type.AbiAlignment.init(comp.loop),
740 .abi_alignment = Type.AbiAlignment.init(),
743741 },
744742 .key = Type.Int.Key{
745743 .is_signed = false,
......@@ -751,8 +749,8 @@ pub const Compilation = struct {
751749 }
752750
753751 pub fn destroy(self: *Compilation) void {
754 await self.main_loop_frame;
755 resume self.destroy_frame;
752 // await self.main_loop_frame;
753 // resume self.destroy_frame;
756754 }
757755
758756 fn start(self: *Compilation) void {
......@@ -794,7 +792,7 @@ pub const Compilation = struct {
794792 }
795793
796794 // First, get an item from the watch channel, waiting on the channel.
797 var group = event.Group(BuildError!void).init(self.loop);
795 var group = event.Group(BuildError!void).init(self.gpa());
798796 {
799797 const ev = (self.fs_watch.channel.get()) catch |err| {
800798 build_result = err;
......@@ -826,7 +824,6 @@ pub const Compilation = struct {
826824 async fn rebuildFile(self: *Compilation, root_scope: *Scope.Root) !void {
827825 const tree_scope = blk: {
828826 const source_code = fs.readFile(
829 self.loop,
830827 root_scope.realpath,
831828 max_src_size,
832829 ) catch |err| {
......@@ -858,10 +855,10 @@ pub const Compilation = struct {
858855 const locked_table = root_scope.decls.table.acquireWrite();
859856 defer locked_table.release();
860857
861 var decl_group = event.Group(BuildError!void).init(self.loop);
858 var decl_group = event.Group(BuildError!void).init(self.gpa());
862859 defer decl_group.deinit();
863860
864 try await try async self.rebuildChangedDecls(
861 try self.rebuildChangedDecls(
865862 &decl_group,
866863 locked_table.value,
867864 root_scope.decls,
......@@ -935,7 +932,7 @@ pub const Compilation = struct {
935932 .id = Decl.Id.Fn,
936933 .name = name,
937934 .visib = parseVisibToken(tree_scope.tree, fn_proto.visib_token),
938 .resolution = event.Future(BuildError!void).init(self.loop),
935 .resolution = event.Future(BuildError!void).init(),
939936 .parent_scope = &decl_scope.base,
940937 .tree_scope = tree_scope,
941938 },
......@@ -975,8 +972,8 @@ pub const Compilation = struct {
975972 };
976973 defer root_scope.base.deref(self);
977974
978 assert((try await try async self.fs_watch.addFile(root_scope.realpath, root_scope)) == null);
979 try await try async self.rebuildFile(root_scope);
975 assert((try self.fs_watch.addFile(root_scope.realpath, root_scope)) == null);
976 try self.rebuildFile(root_scope);
980977 }
981978 }
982979
......@@ -1039,7 +1036,7 @@ pub const Compilation = struct {
10391036 tree_scope: *Scope.AstTree,
10401037 scope: *Scope,
10411038 comptime_node: *ast.Node.Comptime,
1042 ) !void {
1039 ) BuildError!void {
10431040 const void_type = Type.Void.get(comp);
10441041 defer void_type.base.base.deref(comp);
10451042
......@@ -1062,7 +1059,7 @@ pub const Compilation = struct {
10621059 self: *Compilation,
10631060 decl: *Decl,
10641061 locked_table: *Decl.Table,
1065 ) !void {
1062 ) BuildError!void {
10661063 const is_export = decl.isExported(decl.tree_scope.tree);
10671064
10681065 if (is_export) {
......@@ -1166,14 +1163,14 @@ pub const Compilation = struct {
11661163
11671164 /// cancels itself so no need to await or cancel the promise.
11681165 async fn startFindingNativeLibC(self: *Compilation) void {
1169 self.loop.yield();
1166 std.event.Loop.instance.?.yield();
11701167 // we don't care if it fails, we're just trying to kick off the future resolution
11711168 _ = (self.zig_compiler.getNativeLibC()) catch return;
11721169 }
11731170
11741171 /// General Purpose Allocator. Must free when done.
11751172 fn gpa(self: Compilation) *mem.Allocator {
1176 return self.loop.allocator;
1173 return self.zig_compiler.allocator;
11771174 }
11781175
11791176 /// Arena Allocator. Automatically freed when the Compilation is destroyed.
src-self-hosted/libc_installation.zig+52-51
......@@ -4,6 +4,7 @@ const event = std.event;
44const Target = @import("target.zig").Target;
55const c = @import("c.zig");
66const fs = std.fs;
7const Allocator = std.mem.Allocator;
78
89/// See the render function implementation for documentation of the fields.
910pub const LibCInstallation = struct {
......@@ -29,7 +30,7 @@ pub const LibCInstallation = struct {
2930
3031 pub fn parse(
3132 self: *LibCInstallation,
32 allocator: *std.mem.Allocator,
33 allocator: *Allocator,
3334 libc_file: []const u8,
3435 stderr: *std.io.OutStream(fs.File.WriteError),
3536 ) !void {
......@@ -141,10 +142,10 @@ pub const LibCInstallation = struct {
141142 }
142143
143144 /// Finds the default, native libc.
144 pub async fn findNative(self: *LibCInstallation, loop: *event.Loop) !void {
145 pub async fn findNative(self: *LibCInstallation, allocator: *Allocator) !void {
145146 self.initEmpty();
146 var group = event.Group(FindError!void).init(loop);
147 errdefer group.deinit();
147 var group = event.Group(FindError!void).init(allocator);
148 // errdefer group.deinit();
148149 var windows_sdk: ?*c.ZigWindowsSDK = null;
149150 errdefer if (windows_sdk) |sdk| c.zig_free_windows_sdk(@ptrCast(?[*]c.ZigWindowsSDK, sdk));
150151
......@@ -156,11 +157,11 @@ pub const LibCInstallation = struct {
156157 windows_sdk = sdk;
157158
158159 if (sdk.msvc_lib_dir_ptr != 0) {
159 self.msvc_lib_dir = try std.mem.dupe(loop.allocator, u8, sdk.msvc_lib_dir_ptr[0..sdk.msvc_lib_dir_len]);
160 self.msvc_lib_dir = try std.mem.dupe(allocator, u8, sdk.msvc_lib_dir_ptr[0..sdk.msvc_lib_dir_len]);
160161 }
161 try group.call(findNativeKernel32LibDir, self, loop, sdk);
162 try group.call(findNativeIncludeDirWindows, self, loop, sdk);
163 try group.call(findNativeLibDirWindows, self, loop, sdk);
162 try group.call(findNativeKernel32LibDir, self, sdk);
163 try group.call(findNativeIncludeDirWindows, self, sdk);
164 try group.call(findNativeLibDirWindows, self, sdk);
164165 },
165166 c.ZigFindWindowsSdkError.OutOfMemory => return error.OutOfMemory,
166167 c.ZigFindWindowsSdkError.NotFound => return error.NotFound,
......@@ -168,20 +169,20 @@ pub const LibCInstallation = struct {
168169 }
169170 },
170171 .linux => {
171 try group.call(findNativeIncludeDirLinux, self, loop);
172 try group.call(findNativeLibDirLinux, self, loop);
173 try group.call(findNativeStaticLibDir, self, loop);
174 try group.call(findNativeDynamicLinker, self, loop);
172 try group.call(findNativeIncludeDirLinux, self, allocator);
173 try group.call(findNativeLibDirLinux, self, allocator);
174 try group.call(findNativeStaticLibDir, self, allocator);
175 try group.call(findNativeDynamicLinker, self, allocator);
175176 },
176177 .macosx, .freebsd, .netbsd => {
177 self.include_dir = try std.mem.dupe(loop.allocator, u8, "/usr/include");
178 self.include_dir = try std.mem.dupe(allocator, u8, "/usr/include");
178179 },
179180 else => @compileError("unimplemented: find libc for this OS"),
180181 }
181182 return group.wait();
182183 }
183184
184 async fn findNativeIncludeDirLinux(self: *LibCInstallation, loop: *event.Loop) !void {
185 async fn findNativeIncludeDirLinux(self: *LibCInstallation, allocator: *Allocator) FindError!void {
185186 const cc_exe = std.os.getenv("CC") orelse "cc";
186187 const argv = [_][]const u8{
187188 cc_exe,
......@@ -191,7 +192,7 @@ pub const LibCInstallation = struct {
191192 "/dev/null",
192193 };
193194 // TODO make this use event loop
194 const errorable_result = std.ChildProcess.exec(loop.allocator, argv, null, null, 1024 * 1024);
195 const errorable_result = std.ChildProcess.exec(allocator, argv, null, null, 1024 * 1024);
195196 const exec_result = if (std.debug.runtime_safety) blk: {
196197 break :blk errorable_result catch unreachable;
197198 } else blk: {
......@@ -201,8 +202,8 @@ pub const LibCInstallation = struct {
201202 };
202203 };
203204 defer {
204 loop.allocator.free(exec_result.stdout);
205 loop.allocator.free(exec_result.stderr);
205 allocator.free(exec_result.stdout);
206 allocator.free(exec_result.stderr);
206207 }
207208
208209 switch (exec_result.term) {
......@@ -215,7 +216,7 @@ pub const LibCInstallation = struct {
215216 }
216217
217218 var it = std.mem.tokenize(exec_result.stderr, "\n\r");
218 var search_paths = std.ArrayList([]const u8).init(loop.allocator);
219 var search_paths = std.ArrayList([]const u8).init(allocator);
219220 defer search_paths.deinit();
220221 while (it.next()) |line| {
221222 if (line.len != 0 and line[0] == ' ') {
......@@ -231,11 +232,11 @@ pub const LibCInstallation = struct {
231232 while (path_i < search_paths.len) : (path_i += 1) {
232233 const search_path_untrimmed = search_paths.at(search_paths.len - path_i - 1);
233234 const search_path = std.mem.trimLeft(u8, search_path_untrimmed, " ");
234 const stdlib_path = try fs.path.join(loop.allocator, [_][]const u8{ search_path, "stdlib.h" });
235 defer loop.allocator.free(stdlib_path);
235 const stdlib_path = try fs.path.join(allocator, [_][]const u8{ search_path, "stdlib.h" });
236 defer allocator.free(stdlib_path);
236237
237238 if (try fileExists(stdlib_path)) {
238 self.include_dir = try std.mem.dupe(loop.allocator, u8, search_path);
239 self.include_dir = try std.mem.dupe(allocator, u8, search_path);
239240 return;
240241 }
241242 }
......@@ -243,11 +244,11 @@ pub const LibCInstallation = struct {
243244 return error.LibCStdLibHeaderNotFound;
244245 }
245246
246 async fn findNativeIncludeDirWindows(self: *LibCInstallation, loop: *event.Loop, sdk: *c.ZigWindowsSDK) !void {
247 async fn findNativeIncludeDirWindows(self: *LibCInstallation, allocator: *Allocator, sdk: *c.ZigWindowsSDK) !void {
247248 var search_buf: [2]Search = undefined;
248249 const searches = fillSearch(&search_buf, sdk);
249250
250 var result_buf = try std.Buffer.initSize(loop.allocator, 0);
251 var result_buf = try std.Buffer.initSize(allocator, 0);
251252 defer result_buf.deinit();
252253
253254 for (searches) |search| {
......@@ -256,10 +257,10 @@ pub const LibCInstallation = struct {
256257 try stream.print("{}\\Include\\{}\\ucrt", search.path, search.version);
257258
258259 const stdlib_path = try fs.path.join(
259 loop.allocator,
260 allocator,
260261 [_][]const u8{ result_buf.toSliceConst(), "stdlib.h" },
261262 );
262 defer loop.allocator.free(stdlib_path);
263 defer allocator.free(stdlib_path);
263264
264265 if (try fileExists(stdlib_path)) {
265266 self.include_dir = result_buf.toOwnedSlice();
......@@ -270,11 +271,11 @@ pub const LibCInstallation = struct {
270271 return error.LibCStdLibHeaderNotFound;
271272 }
272273
273 async fn findNativeLibDirWindows(self: *LibCInstallation, loop: *event.Loop, sdk: *c.ZigWindowsSDK) FindError!void {
274 async fn findNativeLibDirWindows(self: *LibCInstallation, allocator: *Allocator, sdk: *c.ZigWindowsSDK) FindError!void {
274275 var search_buf: [2]Search = undefined;
275276 const searches = fillSearch(&search_buf, sdk);
276277
277 var result_buf = try std.Buffer.initSize(loop.allocator, 0);
278 var result_buf = try std.Buffer.initSize(allocator, 0);
278279 defer result_buf.deinit();
279280
280281 for (searches) |search| {
......@@ -288,10 +289,10 @@ pub const LibCInstallation = struct {
288289 else => return error.UnsupportedArchitecture,
289290 }
290291 const ucrt_lib_path = try fs.path.join(
291 loop.allocator,
292 allocator,
292293 [_][]const u8{ result_buf.toSliceConst(), "ucrt.lib" },
293294 );
294 defer loop.allocator.free(ucrt_lib_path);
295 defer allocator.free(ucrt_lib_path);
295296 if (try fileExists(ucrt_lib_path)) {
296297 self.lib_dir = result_buf.toOwnedSlice();
297298 return;
......@@ -300,15 +301,15 @@ pub const LibCInstallation = struct {
300301 return error.LibCRuntimeNotFound;
301302 }
302303
303 async fn findNativeLibDirLinux(self: *LibCInstallation, loop: *event.Loop) FindError!void {
304 self.lib_dir = try ccPrintFileName(loop, "crt1.o", true);
304 async fn findNativeLibDirLinux(self: *LibCInstallation, allocator: *Allocator) FindError!void {
305 self.lib_dir = try ccPrintFileName(allocator, "crt1.o", true);
305306 }
306307
307 async fn findNativeStaticLibDir(self: *LibCInstallation, loop: *event.Loop) FindError!void {
308 self.static_lib_dir = try ccPrintFileName(loop, "crtbegin.o", true);
308 async fn findNativeStaticLibDir(self: *LibCInstallation, allocator: *Allocator) FindError!void {
309 self.static_lib_dir = try ccPrintFileName(allocator, "crtbegin.o", true);
309310 }
310311
311 async fn findNativeDynamicLinker(self: *LibCInstallation, loop: *event.Loop) FindError!void {
312 async fn findNativeDynamicLinker(self: *LibCInstallation, allocator: *Allocator) FindError!void {
312313 var dyn_tests = [_]DynTest{
313314 DynTest{
314315 .name = "ld-linux-x86-64.so.2",
......@@ -319,10 +320,10 @@ pub const LibCInstallation = struct {
319320 .result = null,
320321 },
321322 };
322 var group = event.Group(FindError!void).init(loop);
323 var group = event.Group(FindError!void).init(allocator);
323324 errdefer group.deinit();
324325 for (dyn_tests) |*dyn_test| {
325 try group.call(testNativeDynamicLinker, self, loop, dyn_test);
326 try group.call(testNativeDynamicLinker, self, allocator, dyn_test);
326327 }
327328 try group.wait();
328329 for (dyn_tests) |*dyn_test| {
......@@ -338,8 +339,8 @@ pub const LibCInstallation = struct {
338339 result: ?[]const u8,
339340 };
340341
341 async fn testNativeDynamicLinker(self: *LibCInstallation, loop: *event.Loop, dyn_test: *DynTest) FindError!void {
342 if (ccPrintFileName(loop, dyn_test.name, false)) |result| {
342 async fn testNativeDynamicLinker(self: *LibCInstallation, allocator: *Allocator, dyn_test: *DynTest) FindError!void {
343 if (ccPrintFileName(allocator, dyn_test.name, false)) |result| {
343344 dyn_test.result = result;
344345 return;
345346 } else |err| switch (err) {
......@@ -348,11 +349,11 @@ pub const LibCInstallation = struct {
348349 }
349350 }
350351
351 async fn findNativeKernel32LibDir(self: *LibCInstallation, loop: *event.Loop, sdk: *c.ZigWindowsSDK) FindError!void {
352 async fn findNativeKernel32LibDir(self: *LibCInstallation, allocator: *Allocator, sdk: *c.ZigWindowsSDK) FindError!void {
352353 var search_buf: [2]Search = undefined;
353354 const searches = fillSearch(&search_buf, sdk);
354355
355 var result_buf = try std.Buffer.initSize(loop.allocator, 0);
356 var result_buf = try std.Buffer.initSize(allocator, 0);
356357 defer result_buf.deinit();
357358
358359 for (searches) |search| {
......@@ -366,10 +367,10 @@ pub const LibCInstallation = struct {
366367 else => return error.UnsupportedArchitecture,
367368 }
368369 const kernel32_path = try fs.path.join(
369 loop.allocator,
370 allocator,
370371 [_][]const u8{ result_buf.toSliceConst(), "kernel32.lib" },
371372 );
372 defer loop.allocator.free(kernel32_path);
373 defer allocator.free(kernel32_path);
373374 if (try fileExists(kernel32_path)) {
374375 self.kernel32_lib_dir = result_buf.toOwnedSlice();
375376 return;
......@@ -391,15 +392,15 @@ pub const LibCInstallation = struct {
391392};
392393
393394/// caller owns returned memory
394async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bool) ![]u8 {
395async fn ccPrintFileName(allocator: *Allocator, o_file: []const u8, want_dirname: bool) ![]u8 {
395396 const cc_exe = std.os.getenv("CC") orelse "cc";
396 const arg1 = try std.fmt.allocPrint(loop.allocator, "-print-file-name={}", o_file);
397 defer loop.allocator.free(arg1);
397 const arg1 = try std.fmt.allocPrint(allocator, "-print-file-name={}", o_file);
398 defer allocator.free(arg1);
398399 const argv = [_][]const u8{ cc_exe, arg1 };
399400
400401 // TODO This simulates evented I/O for the child process exec
401 loop.yield();
402 const errorable_result = std.ChildProcess.exec(loop.allocator, argv, null, null, 1024 * 1024);
402 std.event.Loop.instance.?.yield();
403 const errorable_result = std.ChildProcess.exec(allocator, argv, null, null, 1024 * 1024);
403404 const exec_result = if (std.debug.runtime_safety) blk: {
404405 break :blk errorable_result catch unreachable;
405406 } else blk: {
......@@ -409,8 +410,8 @@ async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bo
409410 };
410411 };
411412 defer {
412 loop.allocator.free(exec_result.stdout);
413 loop.allocator.free(exec_result.stderr);
413 allocator.free(exec_result.stdout);
414 allocator.free(exec_result.stderr);
414415 }
415416 switch (exec_result.term) {
416417 .Exited => |code| {
......@@ -425,9 +426,9 @@ async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bo
425426 const dirname = fs.path.dirname(line) orelse return error.LibCRuntimeNotFound;
426427
427428 if (want_dirname) {
428 return std.mem.dupe(loop.allocator, u8, dirname);
429 return std.mem.dupe(allocator, u8, dirname);
429430 } else {
430 return std.mem.dupe(loop.allocator, u8, line);
431 return std.mem.dupe(allocator, u8, line);
431432 }
432433}
433434
src-self-hosted/main.zig+25-39
......@@ -26,6 +26,8 @@ var stderr_file: fs.File = undefined;
2626var stderr: *io.OutStream(fs.File.WriteError) = undefined;
2727var stdout: *io.OutStream(fs.File.WriteError) = undefined;
2828
29pub const io_mode = .evented;
30
2931pub const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
3032
3133const usage =
......@@ -386,11 +388,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
386388
387389 var override_libc: LibCInstallation = undefined;
388390
389 var loop: event.Loop = undefined;
390 try loop.initMultiThreaded(allocator);
391 defer loop.deinit();
392
393 var zig_compiler = try ZigCompiler.init(&loop);
391 var zig_compiler = try ZigCompiler.init(allocator);
394392 defer zig_compiler.deinit();
395393
396394 var comp = try Compilation.create(
......@@ -406,7 +404,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
406404 defer comp.destroy();
407405
408406 if (flags.single("libc")) |libc_path| {
409 parseLibcPaths(loop.allocator, &override_libc, libc_path);
407 parseLibcPaths(allocator, &override_libc, libc_path);
410408 comp.override_libc = &override_libc;
411409 }
412410
......@@ -466,8 +464,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
466464 comp.link_objects = link_objects;
467465
468466 comp.start();
469 const frame = try async processBuildEvents(comp, color);
470 loop.run();
467 const frame = async processBuildEvents(comp, color);
471468}
472469
473470async fn processBuildEvents(comp: *Compilation, color: errmsg.Color) void {
......@@ -539,7 +536,7 @@ const Fmt = struct {
539536 seen: event.Locked(SeenMap),
540537 any_error: bool,
541538 color: errmsg.Color,
542 loop: *event.Loop,
539 allocator: *Allocator,
543540
544541 const SeenMap = std.StringHashMap(void);
545542};
......@@ -570,16 +567,10 @@ fn cmdLibC(allocator: *Allocator, args: []const []const u8) !void {
570567 },
571568 }
572569
573 var loop: event.Loop = undefined;
574 try loop.initMultiThreaded(allocator);
575 defer loop.deinit();
576
577 var zig_compiler = try ZigCompiler.init(&loop);
570 var zig_compiler = try ZigCompiler.init(allocator);
578571 defer zig_compiler.deinit();
579572
580573 const frame = async findLibCAsync(&zig_compiler);
581
582 loop.run();
583574}
584575
585576async fn findLibCAsync(zig_compiler: *ZigCompiler) void {
......@@ -656,15 +647,11 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
656647 process.exit(1);
657648 }
658649
659 var loop: event.Loop = undefined;
660 try loop.initMultiThreaded(allocator);
661 defer loop.deinit();
662
663650 return asyncFmtMain(
651 allocator,
664652 &flags,
665653 color,
666654 );
667 // loop.run();
668655}
669656
670657const FmtError = error{
......@@ -690,20 +677,20 @@ const FmtError = error{
690677} || fs.File.OpenError;
691678
692679async fn asyncFmtMain(
693 loop: *event.Loop,
680 allocator: *Allocator,
694681 flags: *const Args,
695682 color: errmsg.Color,
696683) FmtError!void {
697684 var fmt = Fmt{
698 .seen = event.Locked(Fmt.SeenMap).init(loop, Fmt.SeenMap.init(loop.allocator)),
685 .allocator = allocator,
686 .seen = event.Locked(Fmt.SeenMap).init(Fmt.SeenMap.init(allocator)),
699687 .any_error = false,
700688 .color = color,
701 .loop = loop,
702689 };
703690
704691 const check_mode = flags.present("check");
705692
706 var group = event.Group(FmtError!void).init(loop);
693 var group = event.Group(FmtError!void).init(allocator);
707694 for (flags.positionals.toSliceConst()) |file_path| {
708695 try group.call(fmtPath, &fmt, file_path, check_mode);
709696 }
......@@ -714,8 +701,8 @@ async fn asyncFmtMain(
714701}
715702
716703async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtError!void {
717 const file_path = try std.mem.dupe(fmt.loop.allocator, u8, file_path_ref);
718 defer fmt.loop.allocator.free(file_path);
704 const file_path = try std.mem.dupe(fmt.allocator, u8, file_path_ref);
705 defer fmt.allocator.free(file_path);
719706
720707 {
721708 const held = fmt.seen.acquire();
......@@ -724,20 +711,19 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro
724711 if (try held.value.put(file_path, {})) |_| return;
725712 }
726713
727 const source_code = (await try async event.fs.readFile(
728 fmt.loop,
714 const source_code = event.fs.readFile(
729715 file_path,
730716 max_src_size,
731 )) catch |err| switch (err) {
717 ) catch |err| switch (err) {
732718 error.IsDir, error.AccessDenied => {
733719 // TODO make event based (and dir.next())
734720 var dir = try fs.Dir.open(file_path);
735721 defer dir.close();
736722
737 var group = event.Group(FmtError!void).init(fmt.loop);
723 var group = event.Group(FmtError!void).init(fmt.allocator);
738724 while (try dir.next()) |entry| {
739725 if (entry.kind == fs.Dir.Entry.Kind.Directory or mem.endsWith(u8, entry.name, ".zig")) {
740 const full_path = try fs.path.join(fmt.loop.allocator, [_][]const u8{ file_path, entry.name });
726 const full_path = try fs.path.join(fmt.allocator, [_][]const u8{ file_path, entry.name });
741727 try group.call(fmtPath, fmt, full_path, check_mode);
742728 }
743729 }
......@@ -750,9 +736,9 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro
750736 return;
751737 },
752738 };
753 defer fmt.loop.allocator.free(source_code);
739 defer fmt.allocator.free(source_code);
754740
755 const tree = std.zig.parse(fmt.loop.allocator, source_code) catch |err| {
741 const tree = std.zig.parse(fmt.allocator, source_code) catch |err| {
756742 try stderr.print("error parsing file '{}': {}\n", file_path, err);
757743 fmt.any_error = true;
758744 return;
......@@ -761,8 +747,8 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro
761747
762748 var error_it = tree.errors.iterator(0);
763749 while (error_it.next()) |parse_error| {
764 const msg = try errmsg.Msg.createFromParseError(fmt.loop.allocator, parse_error, tree, file_path);
765 defer fmt.loop.allocator.destroy(msg);
750 const msg = try errmsg.Msg.createFromParseError(fmt.allocator, parse_error, tree, file_path);
751 defer fmt.allocator.destroy(msg);
766752
767753 try msg.printToFile(stderr_file, fmt.color);
768754 }
......@@ -772,17 +758,17 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro
772758 }
773759
774760 if (check_mode) {
775 const anything_changed = try std.zig.render(fmt.loop.allocator, io.null_out_stream, tree);
761 const anything_changed = try std.zig.render(fmt.allocator, io.null_out_stream, tree);
776762 if (anything_changed) {
777763 try stderr.print("{}\n", file_path);
778764 fmt.any_error = true;
779765 }
780766 } else {
781767 // TODO make this evented
782 const baf = try io.BufferedAtomicFile.create(fmt.loop.allocator, file_path);
768 const baf = try io.BufferedAtomicFile.create(fmt.allocator, file_path);
783769 defer baf.destroy();
784770
785 const anything_changed = try std.zig.render(fmt.loop.allocator, baf.stream(), tree);
771 const anything_changed = try std.zig.render(fmt.allocator, baf.stream(), tree);
786772 if (anything_changed) {
787773 try stderr.print("{}\n", file_path);
788774 try baf.finish();
src-self-hosted/scope.zig+1-1
......@@ -184,7 +184,7 @@ pub const Scope = struct {
184184 const self = try comp.gpa().create(Decls);
185185 self.* = Decls{
186186 .base = undefined,
187 .table = event.RwLocked(Decl.Table).init(comp.loop, Decl.Table.init(comp.gpa())),
187 .table = event.RwLocked(Decl.Table).init(Decl.Table.init(comp.gpa())),
188188 };
189189 self.base.init(Id.Decls, parent);
190190 return self;
src-self-hosted/test.zig+4-11
......@@ -24,7 +24,6 @@ const file1 = "1.zig";
2424const allocator = std.heap.c_allocator;
2525
2626pub const TestContext = struct {
27 loop: std.event.Loop,
2827 zig_compiler: ZigCompiler,
2928 zig_lib_dir: []u8,
3029 file_index: std.atomic.Int(usize),
......@@ -36,20 +35,16 @@ pub const TestContext = struct {
3635 fn init(self: *TestContext) !void {
3736 self.* = TestContext{
3837 .any_err = {},
39 .loop = undefined,
4038 .zig_compiler = undefined,
4139 .zig_lib_dir = undefined,
4240 .group = undefined,
4341 .file_index = std.atomic.Int(usize).init(0),
4442 };
4543
46 try self.loop.initSingleThreaded(allocator);
47 errdefer self.loop.deinit();
48
49 self.zig_compiler = try ZigCompiler.init(&self.loop);
44 self.zig_compiler = try ZigCompiler.init();
5045 errdefer self.zig_compiler.deinit();
5146
52 self.group = std.event.Group(anyerror!void).init(&self.loop);
47 self.group = std.event.Group(anyerror!void).init(allocator);
5348 errdefer self.group.deinit();
5449
5550 self.zig_lib_dir = try introspect.resolveZigLibDir(allocator);
......@@ -63,13 +58,11 @@ pub const TestContext = struct {
6358 std.fs.deleteTree(tmp_dir_name) catch {};
6459 allocator.free(self.zig_lib_dir);
6560 self.zig_compiler.deinit();
66 self.loop.deinit();
6761 }
6862
6963 fn run(self: *TestContext) !void {
70 const handle = try self.loop.call(waitForGroup, self);
71 defer await handle;
72 self.loop.run();
64 const handle = try std.event.Loop.instance.?.call(waitForGroup, self);
65 await handle;
7366 return self.any_err;
7467 }
7568
src-self-hosted/type.zig+1-1
......@@ -178,7 +178,7 @@ pub const Type = struct {
178178 },
179179 .id = id,
180180 .name = name,
181 .abi_alignment = AbiAlignment.init(comp.loop),
181 .abi_alignment = AbiAlignment.init(),
182182 };
183183 }
184184