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)...@@ -79,7 +79,7 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
79 .builder = builder,79 .builder = builder,
80 .dibuilder = dibuilder,80 .dibuilder = dibuilder,
81 .context = context,81 .context = context,
82 .lock = event.Lock.init(comp.loop),82 .lock = event.Lock.init(),
83 .arena = &code.arena.allocator,83 .arena = &code.arena.allocator,
84 };84 };
8585
src-self-hosted/compilation.zig+52-55
...@@ -35,9 +35,9 @@ const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB...@@ -35,9 +35,9 @@ const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
3535
36/// Data that is local to the event loop.36/// Data that is local to the event loop.
37pub const ZigCompiler = struct {37pub const ZigCompiler = struct {
38 loop: *event.Loop,
39 llvm_handle_pool: std.atomic.Stack(*llvm.Context),38 llvm_handle_pool: std.atomic.Stack(*llvm.Context),
40 lld_lock: event.Lock,39 lld_lock: event.Lock,
40 allocator: *Allocator,
4141
42 /// TODO pool these so that it doesn't have to lock42 /// TODO pool these so that it doesn't have to lock
43 prng: event.Locked(std.rand.DefaultPrng),43 prng: event.Locked(std.rand.DefaultPrng),
...@@ -46,7 +46,7 @@ pub const ZigCompiler = struct {...@@ -46,7 +46,7 @@ pub const ZigCompiler = struct {
4646
47 var lazy_init_targets = std.lazyInit(void);47 var lazy_init_targets = std.lazyInit(void);
4848
49 pub fn init(loop: *event.Loop) !ZigCompiler {49 pub fn init(allocator: *Allocator) !ZigCompiler {
50 lazy_init_targets.get() orelse {50 lazy_init_targets.get() orelse {
51 Target.initializeAll();51 Target.initializeAll();
52 lazy_init_targets.resolve();52 lazy_init_targets.resolve();
...@@ -57,11 +57,11 @@ pub const ZigCompiler = struct {...@@ -57,11 +57,11 @@ pub const ZigCompiler = struct {
57 const seed = mem.readIntNative(u64, &seed_bytes);57 const seed = mem.readIntNative(u64, &seed_bytes);
5858
59 return ZigCompiler{59 return ZigCompiler{
60 .loop = loop,60 .allocator = allocator,
61 .lld_lock = event.Lock.init(loop),61 .lld_lock = event.Lock.init(),
62 .llvm_handle_pool = std.atomic.Stack(*llvm.Context).init(),62 .llvm_handle_pool = std.atomic.Stack(*llvm.Context).init(),
63 .prng = event.Locked(std.rand.DefaultPrng).init(loop, std.rand.DefaultPrng.init(seed)),63 .prng = event.Locked(std.rand.DefaultPrng).init(std.rand.DefaultPrng.init(seed)),
64 .native_libc = event.Future(LibCInstallation).init(loop),64 .native_libc = event.Future(LibCInstallation).init(),
65 };65 };
66 }66 }
6767
...@@ -70,7 +70,7 @@ pub const ZigCompiler = struct {...@@ -70,7 +70,7 @@ pub const ZigCompiler = struct {
70 self.lld_lock.deinit();70 self.lld_lock.deinit();
71 while (self.llvm_handle_pool.pop()) |node| {71 while (self.llvm_handle_pool.pop()) |node| {
72 llvm.ContextDispose(node.data);72 llvm.ContextDispose(node.data);
73 self.loop.allocator.destroy(node);73 self.allocator.destroy(node);
74 }74 }
75 }75 }
7676
...@@ -82,19 +82,19 @@ pub const ZigCompiler = struct {...@@ -82,19 +82,19 @@ pub const ZigCompiler = struct {
82 const context_ref = llvm.ContextCreate() orelse return error.OutOfMemory;82 const context_ref = llvm.ContextCreate() orelse return error.OutOfMemory;
83 errdefer llvm.ContextDispose(context_ref);83 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);
86 node.* = std.atomic.Stack(*llvm.Context).Node{86 node.* = std.atomic.Stack(*llvm.Context).Node{
87 .next = undefined,87 .next = undefined,
88 .data = context_ref,88 .data = context_ref,
89 };89 };
90 errdefer self.loop.allocator.destroy(node);90 errdefer self.allocator.destroy(node);
9191
92 return LlvmHandle{ .node = node };92 return LlvmHandle{ .node = node };
93 }93 }
9494
95 pub async fn getNativeLibC(self: *ZigCompiler) !*LibCInstallation {95 pub async fn getNativeLibC(self: *ZigCompiler) !*LibCInstallation {
96 if (self.native_libc.start()) |ptr| return ptr;96 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);
98 self.native_libc.resolve();98 self.native_libc.resolve();
99 return &self.native_libc.data;99 return &self.native_libc.data;
100 }100 }
...@@ -122,7 +122,6 @@ pub const LlvmHandle = struct {...@@ -122,7 +122,6 @@ pub const LlvmHandle = struct {
122122
123pub const Compilation = struct {123pub const Compilation = struct {
124 zig_compiler: *ZigCompiler,124 zig_compiler: *ZigCompiler,
125 loop: *event.Loop,
126 name: Buffer,125 name: Buffer,
127 llvm_triple: Buffer,126 llvm_triple: Buffer,
128 root_src_path: ?[]const u8,127 root_src_path: ?[]const u8,
...@@ -228,7 +227,7 @@ pub const Compilation = struct {...@@ -228,7 +227,7 @@ pub const Compilation = struct {
228 deinit_group: event.Group(void),227 deinit_group: event.Group(void),
229228
230 // destroy_frame: @Frame(createAsync),229 // destroy_frame: @Frame(createAsync),
231 main_loop_frame: @Frame(Compilation.mainLoop),230 // main_loop_frame: @Frame(Compilation.mainLoop),
232 main_loop_future: event.Future(void),231 main_loop_future: event.Future(void),
233232
234 have_err_ret_tracing: bool,233 have_err_ret_tracing: bool,
...@@ -348,7 +347,7 @@ pub const Compilation = struct {...@@ -348,7 +347,7 @@ pub const Compilation = struct {
348 zig_lib_dir: []const u8,347 zig_lib_dir: []const u8,
349 ) !*Compilation {348 ) !*Compilation {
350 var optional_comp: ?*Compilation = null;349 var optional_comp: ?*Compilation = null;
351 const frame = async createAsync(350 var frame = async createAsync(
352 &optional_comp,351 &optional_comp,
353 zig_compiler,352 zig_compiler,
354 name,353 name,
...@@ -359,7 +358,7 @@ pub const Compilation = struct {...@@ -359,7 +358,7 @@ pub const Compilation = struct {
359 is_static,358 is_static,
360 zig_lib_dir,359 zig_lib_dir,
361 );360 );
362 return optional_comp orelse await frame;361 return optional_comp orelse if (await frame) |_| unreachable else |err| err;
363 }362 }
364363
365 async fn createAsync(364 async fn createAsync(
...@@ -374,10 +373,9 @@ pub const Compilation = struct {...@@ -374,10 +373,9 @@ pub const Compilation = struct {
374 zig_lib_dir: []const u8,373 zig_lib_dir: []const u8,
375 ) !void {374 ) !void {
376375
377 const loop = zig_compiler.loop;376 const allocator = zig_compiler.allocator;
378 var comp = Compilation{377 var comp = Compilation{
379 .loop = loop,378 .arena_allocator = std.heap.ArenaAllocator.init(allocator),
380 .arena_allocator = std.heap.ArenaAllocator.init(loop.allocator),
381 .zig_compiler = zig_compiler,379 .zig_compiler = zig_compiler,
382 .events = undefined,380 .events = undefined,
383 .root_src_path = root_src_path,381 .root_src_path = root_src_path,
...@@ -387,10 +385,10 @@ pub const Compilation = struct {...@@ -387,10 +385,10 @@ pub const Compilation = struct {
387 .build_mode = build_mode,385 .build_mode = build_mode,
388 .zig_lib_dir = zig_lib_dir,386 .zig_lib_dir = zig_lib_dir,
389 .zig_std_dir = undefined,387 .zig_std_dir = undefined,
390 .tmp_dir = event.Future(BuildError![]u8).init(loop),388 .tmp_dir = event.Future(BuildError![]u8).init(),
391 .destroy_frame = @frame(),389 // .destroy_frame = @frame(),
392 .main_loop_frame = undefined,390 // .main_loop_frame = undefined,
393 .main_loop_future = event.Future(void).init(loop),391 .main_loop_future = event.Future(void).init(),
394392
395 .name = undefined,393 .name = undefined,
396 .llvm_triple = undefined,394 .llvm_triple = undefined,
...@@ -419,7 +417,7 @@ pub const Compilation = struct {...@@ -419,7 +417,7 @@ pub const Compilation = struct {
419 .rpath_list = [_][]const u8{},417 .rpath_list = [_][]const u8{},
420 .assembly_files = [_][]const u8{},418 .assembly_files = [_][]const u8{},
421 .link_objects = [_][]const u8{},419 .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()),
423 .windows_subsystem_windows = false,421 .windows_subsystem_windows = false,
424 .windows_subsystem_console = false,422 .windows_subsystem_console = false,
425 .link_libs_list = undefined,423 .link_libs_list = undefined,
...@@ -431,14 +429,14 @@ pub const Compilation = struct {...@@ -431,14 +429,14 @@ pub const Compilation = struct {
431 .test_name_prefix = null,429 .test_name_prefix = null,
432 .emit_file_type = Emit.Binary,430 .emit_file_type = Emit.Binary,
433 .link_out_file = null,431 .link_out_file = null,
434 .exported_symbol_names = event.Locked(Decl.Table).init(loop, Decl.Table.init(loop.allocator)),432 .exported_symbol_names = event.Locked(Decl.Table).init(Decl.Table.init(allocator)),
435 .prelink_group = event.Group(BuildError!void).init(loop),433 .prelink_group = event.Group(BuildError!void).init(allocator),
436 .deinit_group = event.Group(void).init(loop),434 .deinit_group = event.Group(void).init(allocator),
437 .compile_errors = event.Locked(CompileErrList).init(loop, CompileErrList.init(loop.allocator)),435 .compile_errors = event.Locked(CompileErrList).init(CompileErrList.init(allocator)),
438 .int_type_table = event.Locked(IntTypeTable).init(loop, IntTypeTable.init(loop.allocator)),436 .int_type_table = event.Locked(IntTypeTable).init(IntTypeTable.init(allocator)),
439 .array_type_table = event.Locked(ArrayTypeTable).init(loop, ArrayTypeTable.init(loop.allocator)),437 .array_type_table = event.Locked(ArrayTypeTable).init(ArrayTypeTable.init(allocator)),
440 .ptr_type_table = event.Locked(PtrTypeTable).init(loop, PtrTypeTable.init(loop.allocator)),438 .ptr_type_table = event.Locked(PtrTypeTable).init(PtrTypeTable.init(allocator)),
441 .fn_type_table = event.Locked(FnTypeTable).init(loop, FnTypeTable.init(loop.allocator)),439 .fn_type_table = event.Locked(FnTypeTable).init(FnTypeTable.init(allocator)),
442 .c_int_types = undefined,440 .c_int_types = undefined,
443441
444 .meta_type = undefined,442 .meta_type = undefined,
...@@ -519,8 +517,8 @@ pub const Compilation = struct {...@@ -519,8 +517,8 @@ pub const Compilation = struct {
519 comp.target_layout_str = llvm.CopyStringRepOfTargetData(comp.target_data_ref) orelse return error.OutOfMemory;517 comp.target_layout_str = llvm.CopyStringRepOfTargetData(comp.target_data_ref) orelse return error.OutOfMemory;
520 defer llvm.DisposeMessage(comp.target_layout_str);518 defer llvm.DisposeMessage(comp.target_layout_str);
521519
522 comp.events = try event.Channel(Event).create(comp.loop, 0);520 comp.events.init([0]Event{});
523 defer comp.events.destroy();521 defer comp.events.deinit();
524522
525 if (root_src_path) |root_src| {523 if (root_src_path) |root_src| {
526 const dirname = std.fs.path.dirname(root_src) orelse ".";524 const dirname = std.fs.path.dirname(root_src) orelse ".";
...@@ -533,13 +531,13 @@ pub const Compilation = struct {...@@ -533,13 +531,13 @@ pub const Compilation = struct {
533 comp.root_package = try Package.create(comp.arena(), ".", "");531 comp.root_package = try Package.create(comp.arena(), ".", "");
534 }532 }
535533
536 comp.fs_watch = try fs.Watch(*Scope.Root).create(loop, 16);534 comp.fs_watch = try fs.Watch(*Scope.Root).create(16);
537 defer comp.fs_watch.destroy();535 defer comp.fs_watch.destroy();
538536
539 try comp.initTypes();537 try comp.initTypes();
540 defer comp.primitive_type_table.deinit();538 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();
543 // Set this to indicate that initialization completed successfully.541 // Set this to indicate that initialization completed successfully.
544 // from here on out we must not return an error.542 // from here on out we must not return an error.
545 // This must occur before the first suspend/await.543 // This must occur before the first suspend/await.
...@@ -552,7 +550,7 @@ pub const Compilation = struct {...@@ -552,7 +550,7 @@ pub const Compilation = struct {
552550
553 if (comp.tmp_dir.getOrNull()) |tmp_dir_result| if (tmp_dir_result.*) |tmp_dir| {551 if (comp.tmp_dir.getOrNull()) |tmp_dir_result| if (tmp_dir_result.*) |tmp_dir| {
554 // TODO evented I/O?552 // TODO evented I/O?
555 std.fs.deleteTree(comp.arena(), tmp_dir) catch {};553 std.fs.deleteTree(tmp_dir) catch {};
556 } else |_| {};554 } else |_| {};
557 }555 }
558556
...@@ -601,7 +599,7 @@ pub const Compilation = struct {...@@ -601,7 +599,7 @@ pub const Compilation = struct {
601 .ref_count = std.atomic.Int(usize).init(3), // 3 because it references itself twice599 .ref_count = std.atomic.Int(usize).init(3), // 3 because it references itself twice
602 },600 },
603 .id = builtin.TypeId.Type,601 .id = builtin.TypeId.Type,
604 .abi_alignment = Type.AbiAlignment.init(comp.loop),602 .abi_alignment = Type.AbiAlignment.init(),
605 },603 },
606 .value = undefined,604 .value = undefined,
607 };605 };
...@@ -619,7 +617,7 @@ pub const Compilation = struct {...@@ -619,7 +617,7 @@ pub const Compilation = struct {
619 .ref_count = std.atomic.Int(usize).init(1),617 .ref_count = std.atomic.Int(usize).init(1),
620 },618 },
621 .id = builtin.TypeId.Void,619 .id = builtin.TypeId.Void,
622 .abi_alignment = Type.AbiAlignment.init(comp.loop),620 .abi_alignment = Type.AbiAlignment.init(),
623 },621 },
624 };622 };
625 assert((try comp.primitive_type_table.put(comp.void_type.base.name, &comp.void_type.base)) == null);623 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 {...@@ -634,7 +632,7 @@ pub const Compilation = struct {
634 .ref_count = std.atomic.Int(usize).init(1),632 .ref_count = std.atomic.Int(usize).init(1),
635 },633 },
636 .id = builtin.TypeId.NoReturn,634 .id = builtin.TypeId.NoReturn,
637 .abi_alignment = Type.AbiAlignment.init(comp.loop),635 .abi_alignment = Type.AbiAlignment.init(),
638 },636 },
639 };637 };
640 assert((try comp.primitive_type_table.put(comp.noreturn_type.base.name, &comp.noreturn_type.base)) == null);638 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 {...@@ -649,7 +647,7 @@ pub const Compilation = struct {
649 .ref_count = std.atomic.Int(usize).init(1),647 .ref_count = std.atomic.Int(usize).init(1),
650 },648 },
651 .id = builtin.TypeId.ComptimeInt,649 .id = builtin.TypeId.ComptimeInt,
652 .abi_alignment = Type.AbiAlignment.init(comp.loop),650 .abi_alignment = Type.AbiAlignment.init(),
653 },651 },
654 };652 };
655 assert((try comp.primitive_type_table.put(comp.comptime_int_type.base.name, &comp.comptime_int_type.base)) == null);653 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 {...@@ -664,7 +662,7 @@ pub const Compilation = struct {
664 .ref_count = std.atomic.Int(usize).init(1),662 .ref_count = std.atomic.Int(usize).init(1),
665 },663 },
666 .id = builtin.TypeId.Bool,664 .id = builtin.TypeId.Bool,
667 .abi_alignment = Type.AbiAlignment.init(comp.loop),665 .abi_alignment = Type.AbiAlignment.init(),
668 },666 },
669 };667 };
670 assert((try comp.primitive_type_table.put(comp.bool_type.base.name, &comp.bool_type.base)) == null);668 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 {...@@ -718,7 +716,7 @@ pub const Compilation = struct {
718 .ref_count = std.atomic.Int(usize).init(1),716 .ref_count = std.atomic.Int(usize).init(1),
719 },717 },
720 .id = builtin.TypeId.Int,718 .id = builtin.TypeId.Int,
721 .abi_alignment = Type.AbiAlignment.init(comp.loop),719 .abi_alignment = Type.AbiAlignment.init(),
722 },720 },
723 .key = Type.Int.Key{721 .key = Type.Int.Key{
724 .is_signed = cint.is_signed,722 .is_signed = cint.is_signed,
...@@ -739,7 +737,7 @@ pub const Compilation = struct {...@@ -739,7 +737,7 @@ pub const Compilation = struct {
739 .ref_count = std.atomic.Int(usize).init(1),737 .ref_count = std.atomic.Int(usize).init(1),
740 },738 },
741 .id = builtin.TypeId.Int,739 .id = builtin.TypeId.Int,
742 .abi_alignment = Type.AbiAlignment.init(comp.loop),740 .abi_alignment = Type.AbiAlignment.init(),
743 },741 },
744 .key = Type.Int.Key{742 .key = Type.Int.Key{
745 .is_signed = false,743 .is_signed = false,
...@@ -751,8 +749,8 @@ pub const Compilation = struct {...@@ -751,8 +749,8 @@ pub const Compilation = struct {
751 }749 }
752750
753 pub fn destroy(self: *Compilation) void {751 pub fn destroy(self: *Compilation) void {
754 await self.main_loop_frame;752 // await self.main_loop_frame;
755 resume self.destroy_frame;753 // resume self.destroy_frame;
756 }754 }
757755
758 fn start(self: *Compilation) void {756 fn start(self: *Compilation) void {
...@@ -794,7 +792,7 @@ pub const Compilation = struct {...@@ -794,7 +792,7 @@ pub const Compilation = struct {
794 }792 }
795793
796 // First, get an item from the watch channel, waiting on the channel.794 // 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());
798 {796 {
799 const ev = (self.fs_watch.channel.get()) catch |err| {797 const ev = (self.fs_watch.channel.get()) catch |err| {
800 build_result = err;798 build_result = err;
...@@ -826,7 +824,6 @@ pub const Compilation = struct {...@@ -826,7 +824,6 @@ pub const Compilation = struct {
826 async fn rebuildFile(self: *Compilation, root_scope: *Scope.Root) !void {824 async fn rebuildFile(self: *Compilation, root_scope: *Scope.Root) !void {
827 const tree_scope = blk: {825 const tree_scope = blk: {
828 const source_code = fs.readFile(826 const source_code = fs.readFile(
829 self.loop,
830 root_scope.realpath,827 root_scope.realpath,
831 max_src_size,828 max_src_size,
832 ) catch |err| {829 ) catch |err| {
...@@ -858,10 +855,10 @@ pub const Compilation = struct {...@@ -858,10 +855,10 @@ pub const Compilation = struct {
858 const locked_table = root_scope.decls.table.acquireWrite();855 const locked_table = root_scope.decls.table.acquireWrite();
859 defer locked_table.release();856 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());
862 defer decl_group.deinit();859 defer decl_group.deinit();
863860
864 try await try async self.rebuildChangedDecls(861 try self.rebuildChangedDecls(
865 &decl_group,862 &decl_group,
866 locked_table.value,863 locked_table.value,
867 root_scope.decls,864 root_scope.decls,
...@@ -935,7 +932,7 @@ pub const Compilation = struct {...@@ -935,7 +932,7 @@ pub const Compilation = struct {
935 .id = Decl.Id.Fn,932 .id = Decl.Id.Fn,
936 .name = name,933 .name = name,
937 .visib = parseVisibToken(tree_scope.tree, fn_proto.visib_token),934 .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(),
939 .parent_scope = &decl_scope.base,936 .parent_scope = &decl_scope.base,
940 .tree_scope = tree_scope,937 .tree_scope = tree_scope,
941 },938 },
...@@ -975,8 +972,8 @@ pub const Compilation = struct {...@@ -975,8 +972,8 @@ pub const Compilation = struct {
975 };972 };
976 defer root_scope.base.deref(self);973 defer root_scope.base.deref(self);
977974
978 assert((try await try async self.fs_watch.addFile(root_scope.realpath, root_scope)) == null);975 assert((try self.fs_watch.addFile(root_scope.realpath, root_scope)) == null);
979 try await try async self.rebuildFile(root_scope);976 try self.rebuildFile(root_scope);
980 }977 }
981 }978 }
982979
...@@ -1039,7 +1036,7 @@ pub const Compilation = struct {...@@ -1039,7 +1036,7 @@ pub const Compilation = struct {
1039 tree_scope: *Scope.AstTree,1036 tree_scope: *Scope.AstTree,
1040 scope: *Scope,1037 scope: *Scope,
1041 comptime_node: *ast.Node.Comptime,1038 comptime_node: *ast.Node.Comptime,
1042 ) !void {1039 ) BuildError!void {
1043 const void_type = Type.Void.get(comp);1040 const void_type = Type.Void.get(comp);
1044 defer void_type.base.base.deref(comp);1041 defer void_type.base.base.deref(comp);
10451042
...@@ -1062,7 +1059,7 @@ pub const Compilation = struct {...@@ -1062,7 +1059,7 @@ pub const Compilation = struct {
1062 self: *Compilation,1059 self: *Compilation,
1063 decl: *Decl,1060 decl: *Decl,
1064 locked_table: *Decl.Table,1061 locked_table: *Decl.Table,
1065 ) !void {1062 ) BuildError!void {
1066 const is_export = decl.isExported(decl.tree_scope.tree);1063 const is_export = decl.isExported(decl.tree_scope.tree);
10671064
1068 if (is_export) {1065 if (is_export) {
...@@ -1166,14 +1163,14 @@ pub const Compilation = struct {...@@ -1166,14 +1163,14 @@ pub const Compilation = struct {
11661163
1167 /// cancels itself so no need to await or cancel the promise.1164 /// cancels itself so no need to await or cancel the promise.
1168 async fn startFindingNativeLibC(self: *Compilation) void {1165 async fn startFindingNativeLibC(self: *Compilation) void {
1169 self.loop.yield();1166 std.event.Loop.instance.?.yield();
1170 // we don't care if it fails, we're just trying to kick off the future resolution1167 // we don't care if it fails, we're just trying to kick off the future resolution
1171 _ = (self.zig_compiler.getNativeLibC()) catch return;1168 _ = (self.zig_compiler.getNativeLibC()) catch return;
1172 }1169 }
11731170
1174 /// General Purpose Allocator. Must free when done.1171 /// General Purpose Allocator. Must free when done.
1175 fn gpa(self: Compilation) *mem.Allocator {1172 fn gpa(self: Compilation) *mem.Allocator {
1176 return self.loop.allocator;1173 return self.zig_compiler.allocator;
1177 }1174 }
11781175
1179 /// Arena Allocator. Automatically freed when the Compilation is destroyed.1176 /// 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;...@@ -4,6 +4,7 @@ const event = std.event;
4const Target = @import("target.zig").Target;4const Target = @import("target.zig").Target;
5const c = @import("c.zig");5const c = @import("c.zig");
6const fs = std.fs;6const fs = std.fs;
7const Allocator = std.mem.Allocator;
78
8/// See the render function implementation for documentation of the fields.9/// See the render function implementation for documentation of the fields.
9pub const LibCInstallation = struct {10pub const LibCInstallation = struct {
...@@ -29,7 +30,7 @@ pub const LibCInstallation = struct {...@@ -29,7 +30,7 @@ pub const LibCInstallation = struct {
2930
30 pub fn parse(31 pub fn parse(
31 self: *LibCInstallation,32 self: *LibCInstallation,
32 allocator: *std.mem.Allocator,33 allocator: *Allocator,
33 libc_file: []const u8,34 libc_file: []const u8,
34 stderr: *std.io.OutStream(fs.File.WriteError),35 stderr: *std.io.OutStream(fs.File.WriteError),
35 ) !void {36 ) !void {
...@@ -141,10 +142,10 @@ pub const LibCInstallation = struct {...@@ -141,10 +142,10 @@ pub const LibCInstallation = struct {
141 }142 }
142143
143 /// Finds the default, native libc.144 /// 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 {
145 self.initEmpty();146 self.initEmpty();
146 var group = event.Group(FindError!void).init(loop);147 var group = event.Group(FindError!void).init(allocator);
147 errdefer group.deinit();148 // errdefer group.deinit();
148 var windows_sdk: ?*c.ZigWindowsSDK = null;149 var windows_sdk: ?*c.ZigWindowsSDK = null;
149 errdefer if (windows_sdk) |sdk| c.zig_free_windows_sdk(@ptrCast(?[*]c.ZigWindowsSDK, sdk));150 errdefer if (windows_sdk) |sdk| c.zig_free_windows_sdk(@ptrCast(?[*]c.ZigWindowsSDK, sdk));
150151
...@@ -156,11 +157,11 @@ pub const LibCInstallation = struct {...@@ -156,11 +157,11 @@ pub const LibCInstallation = struct {
156 windows_sdk = sdk;157 windows_sdk = sdk;
157158
158 if (sdk.msvc_lib_dir_ptr != 0) {159 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]);
160 }161 }
161 try group.call(findNativeKernel32LibDir, self, loop, sdk);162 try group.call(findNativeKernel32LibDir, self, sdk);
162 try group.call(findNativeIncludeDirWindows, self, loop, sdk);163 try group.call(findNativeIncludeDirWindows, self, sdk);
163 try group.call(findNativeLibDirWindows, self, loop, sdk);164 try group.call(findNativeLibDirWindows, self, sdk);
164 },165 },
165 c.ZigFindWindowsSdkError.OutOfMemory => return error.OutOfMemory,166 c.ZigFindWindowsSdkError.OutOfMemory => return error.OutOfMemory,
166 c.ZigFindWindowsSdkError.NotFound => return error.NotFound,167 c.ZigFindWindowsSdkError.NotFound => return error.NotFound,
...@@ -168,20 +169,20 @@ pub const LibCInstallation = struct {...@@ -168,20 +169,20 @@ pub const LibCInstallation = struct {
168 }169 }
169 },170 },
170 .linux => {171 .linux => {
171 try group.call(findNativeIncludeDirLinux, self, loop);172 try group.call(findNativeIncludeDirLinux, self, allocator);
172 try group.call(findNativeLibDirLinux, self, loop);173 try group.call(findNativeLibDirLinux, self, allocator);
173 try group.call(findNativeStaticLibDir, self, loop);174 try group.call(findNativeStaticLibDir, self, allocator);
174 try group.call(findNativeDynamicLinker, self, loop);175 try group.call(findNativeDynamicLinker, self, allocator);
175 },176 },
176 .macosx, .freebsd, .netbsd => {177 .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");
178 },179 },
179 else => @compileError("unimplemented: find libc for this OS"),180 else => @compileError("unimplemented: find libc for this OS"),
180 }181 }
181 return group.wait();182 return group.wait();
182 }183 }
183184
184 async fn findNativeIncludeDirLinux(self: *LibCInstallation, loop: *event.Loop) !void {185 async fn findNativeIncludeDirLinux(self: *LibCInstallation, allocator: *Allocator) FindError!void {
185 const cc_exe = std.os.getenv("CC") orelse "cc";186 const cc_exe = std.os.getenv("CC") orelse "cc";
186 const argv = [_][]const u8{187 const argv = [_][]const u8{
187 cc_exe,188 cc_exe,
...@@ -191,7 +192,7 @@ pub const LibCInstallation = struct {...@@ -191,7 +192,7 @@ pub const LibCInstallation = struct {
191 "/dev/null",192 "/dev/null",
192 };193 };
193 // TODO make this use event loop194 // 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);
195 const exec_result = if (std.debug.runtime_safety) blk: {196 const exec_result = if (std.debug.runtime_safety) blk: {
196 break :blk errorable_result catch unreachable;197 break :blk errorable_result catch unreachable;
197 } else blk: {198 } else blk: {
...@@ -201,8 +202,8 @@ pub const LibCInstallation = struct {...@@ -201,8 +202,8 @@ pub const LibCInstallation = struct {
201 };202 };
202 };203 };
203 defer {204 defer {
204 loop.allocator.free(exec_result.stdout);205 allocator.free(exec_result.stdout);
205 loop.allocator.free(exec_result.stderr);206 allocator.free(exec_result.stderr);
206 }207 }
207208
208 switch (exec_result.term) {209 switch (exec_result.term) {
...@@ -215,7 +216,7 @@ pub const LibCInstallation = struct {...@@ -215,7 +216,7 @@ pub const LibCInstallation = struct {
215 }216 }
216217
217 var it = std.mem.tokenize(exec_result.stderr, "\n\r");218 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);
219 defer search_paths.deinit();220 defer search_paths.deinit();
220 while (it.next()) |line| {221 while (it.next()) |line| {
221 if (line.len != 0 and line[0] == ' ') {222 if (line.len != 0 and line[0] == ' ') {
...@@ -231,11 +232,11 @@ pub const LibCInstallation = struct {...@@ -231,11 +232,11 @@ pub const LibCInstallation = struct {
231 while (path_i < search_paths.len) : (path_i += 1) {232 while (path_i < search_paths.len) : (path_i += 1) {
232 const search_path_untrimmed = search_paths.at(search_paths.len - path_i - 1);233 const search_path_untrimmed = search_paths.at(search_paths.len - path_i - 1);
233 const search_path = std.mem.trimLeft(u8, search_path_untrimmed, " ");234 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 const stdlib_path = try fs.path.join(allocator, [_][]const u8{ search_path, "stdlib.h" });
235 defer loop.allocator.free(stdlib_path);236 defer allocator.free(stdlib_path);
236237
237 if (try fileExists(stdlib_path)) {238 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);
239 return;240 return;
240 }241 }
241 }242 }
...@@ -243,11 +244,11 @@ pub const LibCInstallation = struct {...@@ -243,11 +244,11 @@ pub const LibCInstallation = struct {
243 return error.LibCStdLibHeaderNotFound;244 return error.LibCStdLibHeaderNotFound;
244 }245 }
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 {
247 var search_buf: [2]Search = undefined;248 var search_buf: [2]Search = undefined;
248 const searches = fillSearch(&search_buf, sdk);249 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);
251 defer result_buf.deinit();252 defer result_buf.deinit();
252253
253 for (searches) |search| {254 for (searches) |search| {
...@@ -256,10 +257,10 @@ pub const LibCInstallation = struct {...@@ -256,10 +257,10 @@ pub const LibCInstallation = struct {
256 try stream.print("{}\\Include\\{}\\ucrt", search.path, search.version);257 try stream.print("{}\\Include\\{}\\ucrt", search.path, search.version);
257258
258 const stdlib_path = try fs.path.join(259 const stdlib_path = try fs.path.join(
259 loop.allocator,260 allocator,
260 [_][]const u8{ result_buf.toSliceConst(), "stdlib.h" },261 [_][]const u8{ result_buf.toSliceConst(), "stdlib.h" },
261 );262 );
262 defer loop.allocator.free(stdlib_path);263 defer allocator.free(stdlib_path);
263264
264 if (try fileExists(stdlib_path)) {265 if (try fileExists(stdlib_path)) {
265 self.include_dir = result_buf.toOwnedSlice();266 self.include_dir = result_buf.toOwnedSlice();
...@@ -270,11 +271,11 @@ pub const LibCInstallation = struct {...@@ -270,11 +271,11 @@ pub const LibCInstallation = struct {
270 return error.LibCStdLibHeaderNotFound;271 return error.LibCStdLibHeaderNotFound;
271 }272 }
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 {
274 var search_buf: [2]Search = undefined;275 var search_buf: [2]Search = undefined;
275 const searches = fillSearch(&search_buf, sdk);276 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);
278 defer result_buf.deinit();279 defer result_buf.deinit();
279280
280 for (searches) |search| {281 for (searches) |search| {
...@@ -288,10 +289,10 @@ pub const LibCInstallation = struct {...@@ -288,10 +289,10 @@ pub const LibCInstallation = struct {
288 else => return error.UnsupportedArchitecture,289 else => return error.UnsupportedArchitecture,
289 }290 }
290 const ucrt_lib_path = try fs.path.join(291 const ucrt_lib_path = try fs.path.join(
291 loop.allocator,292 allocator,
292 [_][]const u8{ result_buf.toSliceConst(), "ucrt.lib" },293 [_][]const u8{ result_buf.toSliceConst(), "ucrt.lib" },
293 );294 );
294 defer loop.allocator.free(ucrt_lib_path);295 defer allocator.free(ucrt_lib_path);
295 if (try fileExists(ucrt_lib_path)) {296 if (try fileExists(ucrt_lib_path)) {
296 self.lib_dir = result_buf.toOwnedSlice();297 self.lib_dir = result_buf.toOwnedSlice();
297 return;298 return;
...@@ -300,15 +301,15 @@ pub const LibCInstallation = struct {...@@ -300,15 +301,15 @@ pub const LibCInstallation = struct {
300 return error.LibCRuntimeNotFound;301 return error.LibCRuntimeNotFound;
301 }302 }
302303
303 async fn findNativeLibDirLinux(self: *LibCInstallation, loop: *event.Loop) FindError!void {304 async fn findNativeLibDirLinux(self: *LibCInstallation, allocator: *Allocator) FindError!void {
304 self.lib_dir = try ccPrintFileName(loop, "crt1.o", true);305 self.lib_dir = try ccPrintFileName(allocator, "crt1.o", true);
305 }306 }
306307
307 async fn findNativeStaticLibDir(self: *LibCInstallation, loop: *event.Loop) FindError!void {308 async fn findNativeStaticLibDir(self: *LibCInstallation, allocator: *Allocator) FindError!void {
308 self.static_lib_dir = try ccPrintFileName(loop, "crtbegin.o", true);309 self.static_lib_dir = try ccPrintFileName(allocator, "crtbegin.o", true);
309 }310 }
310311
311 async fn findNativeDynamicLinker(self: *LibCInstallation, loop: *event.Loop) FindError!void {312 async fn findNativeDynamicLinker(self: *LibCInstallation, allocator: *Allocator) FindError!void {
312 var dyn_tests = [_]DynTest{313 var dyn_tests = [_]DynTest{
313 DynTest{314 DynTest{
314 .name = "ld-linux-x86-64.so.2",315 .name = "ld-linux-x86-64.so.2",
...@@ -319,10 +320,10 @@ pub const LibCInstallation = struct {...@@ -319,10 +320,10 @@ pub const LibCInstallation = struct {
319 .result = null,320 .result = null,
320 },321 },
321 };322 };
322 var group = event.Group(FindError!void).init(loop);323 var group = event.Group(FindError!void).init(allocator);
323 errdefer group.deinit();324 errdefer group.deinit();
324 for (dyn_tests) |*dyn_test| {325 for (dyn_tests) |*dyn_test| {
325 try group.call(testNativeDynamicLinker, self, loop, dyn_test);326 try group.call(testNativeDynamicLinker, self, allocator, dyn_test);
326 }327 }
327 try group.wait();328 try group.wait();
328 for (dyn_tests) |*dyn_test| {329 for (dyn_tests) |*dyn_test| {
...@@ -338,8 +339,8 @@ pub const LibCInstallation = struct {...@@ -338,8 +339,8 @@ pub const LibCInstallation = struct {
338 result: ?[]const u8,339 result: ?[]const u8,
339 };340 };
340341
341 async fn testNativeDynamicLinker(self: *LibCInstallation, loop: *event.Loop, dyn_test: *DynTest) FindError!void {342 async fn testNativeDynamicLinker(self: *LibCInstallation, allocator: *Allocator, dyn_test: *DynTest) FindError!void {
342 if (ccPrintFileName(loop, dyn_test.name, false)) |result| {343 if (ccPrintFileName(allocator, dyn_test.name, false)) |result| {
343 dyn_test.result = result;344 dyn_test.result = result;
344 return;345 return;
345 } else |err| switch (err) {346 } else |err| switch (err) {
...@@ -348,11 +349,11 @@ pub const LibCInstallation = struct {...@@ -348,11 +349,11 @@ pub const LibCInstallation = struct {
348 }349 }
349 }350 }
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 {
352 var search_buf: [2]Search = undefined;353 var search_buf: [2]Search = undefined;
353 const searches = fillSearch(&search_buf, sdk);354 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);
356 defer result_buf.deinit();357 defer result_buf.deinit();
357358
358 for (searches) |search| {359 for (searches) |search| {
...@@ -366,10 +367,10 @@ pub const LibCInstallation = struct {...@@ -366,10 +367,10 @@ pub const LibCInstallation = struct {
366 else => return error.UnsupportedArchitecture,367 else => return error.UnsupportedArchitecture,
367 }368 }
368 const kernel32_path = try fs.path.join(369 const kernel32_path = try fs.path.join(
369 loop.allocator,370 allocator,
370 [_][]const u8{ result_buf.toSliceConst(), "kernel32.lib" },371 [_][]const u8{ result_buf.toSliceConst(), "kernel32.lib" },
371 );372 );
372 defer loop.allocator.free(kernel32_path);373 defer allocator.free(kernel32_path);
373 if (try fileExists(kernel32_path)) {374 if (try fileExists(kernel32_path)) {
374 self.kernel32_lib_dir = result_buf.toOwnedSlice();375 self.kernel32_lib_dir = result_buf.toOwnedSlice();
375 return;376 return;
...@@ -391,15 +392,15 @@ pub const LibCInstallation = struct {...@@ -391,15 +392,15 @@ pub const LibCInstallation = struct {
391};392};
392393
393/// caller owns returned memory394/// 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 {
395 const cc_exe = std.os.getenv("CC") orelse "cc";396 const cc_exe = std.os.getenv("CC") orelse "cc";
396 const arg1 = try std.fmt.allocPrint(loop.allocator, "-print-file-name={}", o_file);397 const arg1 = try std.fmt.allocPrint(allocator, "-print-file-name={}", o_file);
397 defer loop.allocator.free(arg1);398 defer allocator.free(arg1);
398 const argv = [_][]const u8{ cc_exe, arg1 };399 const argv = [_][]const u8{ cc_exe, arg1 };
399400
400 // TODO This simulates evented I/O for the child process exec401 // TODO This simulates evented I/O for the child process exec
401 loop.yield();402 std.event.Loop.instance.?.yield();
402 const errorable_result = std.ChildProcess.exec(loop.allocator, argv, null, null, 1024 * 1024);403 const errorable_result = std.ChildProcess.exec(allocator, argv, null, null, 1024 * 1024);
403 const exec_result = if (std.debug.runtime_safety) blk: {404 const exec_result = if (std.debug.runtime_safety) blk: {
404 break :blk errorable_result catch unreachable;405 break :blk errorable_result catch unreachable;
405 } else blk: {406 } else blk: {
...@@ -409,8 +410,8 @@ async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bo...@@ -409,8 +410,8 @@ async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bo
409 };410 };
410 };411 };
411 defer {412 defer {
412 loop.allocator.free(exec_result.stdout);413 allocator.free(exec_result.stdout);
413 loop.allocator.free(exec_result.stderr);414 allocator.free(exec_result.stderr);
414 }415 }
415 switch (exec_result.term) {416 switch (exec_result.term) {
416 .Exited => |code| {417 .Exited => |code| {
...@@ -425,9 +426,9 @@ async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bo...@@ -425,9 +426,9 @@ async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bo
425 const dirname = fs.path.dirname(line) orelse return error.LibCRuntimeNotFound;426 const dirname = fs.path.dirname(line) orelse return error.LibCRuntimeNotFound;
426427
427 if (want_dirname) {428 if (want_dirname) {
428 return std.mem.dupe(loop.allocator, u8, dirname);429 return std.mem.dupe(allocator, u8, dirname);
429 } else {430 } else {
430 return std.mem.dupe(loop.allocator, u8, line);431 return std.mem.dupe(allocator, u8, line);
431 }432 }
432}433}
433434
src-self-hosted/main.zig+25-39
...@@ -26,6 +26,8 @@ var stderr_file: fs.File = undefined;...@@ -26,6 +26,8 @@ var stderr_file: fs.File = undefined;
26var stderr: *io.OutStream(fs.File.WriteError) = undefined;26var stderr: *io.OutStream(fs.File.WriteError) = undefined;
27var stdout: *io.OutStream(fs.File.WriteError) = undefined;27var stdout: *io.OutStream(fs.File.WriteError) = undefined;
2828
29pub const io_mode = .evented;
30
29pub const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB31pub const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
3032
31const usage =33const usage =
...@@ -386,11 +388,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co...@@ -386,11 +388,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
386388
387 var override_libc: LibCInstallation = undefined;389 var override_libc: LibCInstallation = undefined;
388390
389 var loop: event.Loop = undefined;391 var zig_compiler = try ZigCompiler.init(allocator);
390 try loop.initMultiThreaded(allocator);
391 defer loop.deinit();
392
393 var zig_compiler = try ZigCompiler.init(&loop);
394 defer zig_compiler.deinit();392 defer zig_compiler.deinit();
395393
396 var comp = try Compilation.create(394 var comp = try Compilation.create(
...@@ -406,7 +404,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co...@@ -406,7 +404,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
406 defer comp.destroy();404 defer comp.destroy();
407405
408 if (flags.single("libc")) |libc_path| {406 if (flags.single("libc")) |libc_path| {
409 parseLibcPaths(loop.allocator, &override_libc, libc_path);407 parseLibcPaths(allocator, &override_libc, libc_path);
410 comp.override_libc = &override_libc;408 comp.override_libc = &override_libc;
411 }409 }
412410
...@@ -466,8 +464,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co...@@ -466,8 +464,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
466 comp.link_objects = link_objects;464 comp.link_objects = link_objects;
467465
468 comp.start();466 comp.start();
469 const frame = try async processBuildEvents(comp, color);467 const frame = async processBuildEvents(comp, color);
470 loop.run();
471}468}
472469
473async fn processBuildEvents(comp: *Compilation, color: errmsg.Color) void {470async fn processBuildEvents(comp: *Compilation, color: errmsg.Color) void {
...@@ -539,7 +536,7 @@ const Fmt = struct {...@@ -539,7 +536,7 @@ const Fmt = struct {
539 seen: event.Locked(SeenMap),536 seen: event.Locked(SeenMap),
540 any_error: bool,537 any_error: bool,
541 color: errmsg.Color,538 color: errmsg.Color,
542 loop: *event.Loop,539 allocator: *Allocator,
543540
544 const SeenMap = std.StringHashMap(void);541 const SeenMap = std.StringHashMap(void);
545};542};
...@@ -570,16 +567,10 @@ fn cmdLibC(allocator: *Allocator, args: []const []const u8) !void {...@@ -570,16 +567,10 @@ fn cmdLibC(allocator: *Allocator, args: []const []const u8) !void {
570 },567 },
571 }568 }
572569
573 var loop: event.Loop = undefined;570 var zig_compiler = try ZigCompiler.init(allocator);
574 try loop.initMultiThreaded(allocator);
575 defer loop.deinit();
576
577 var zig_compiler = try ZigCompiler.init(&loop);
578 defer zig_compiler.deinit();571 defer zig_compiler.deinit();
579572
580 const frame = async findLibCAsync(&zig_compiler);573 const frame = async findLibCAsync(&zig_compiler);
581
582 loop.run();
583}574}
584575
585async fn findLibCAsync(zig_compiler: *ZigCompiler) void {576async fn findLibCAsync(zig_compiler: *ZigCompiler) void {
...@@ -656,15 +647,11 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {...@@ -656,15 +647,11 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
656 process.exit(1);647 process.exit(1);
657 }648 }
658649
659 var loop: event.Loop = undefined;
660 try loop.initMultiThreaded(allocator);
661 defer loop.deinit();
662
663 return asyncFmtMain(650 return asyncFmtMain(
651 allocator,
664 &flags,652 &flags,
665 color,653 color,
666 );654 );
667 // loop.run();
668}655}
669656
670const FmtError = error{657const FmtError = error{
...@@ -690,20 +677,20 @@ const FmtError = error{...@@ -690,20 +677,20 @@ const FmtError = error{
690} || fs.File.OpenError;677} || fs.File.OpenError;
691678
692async fn asyncFmtMain(679async fn asyncFmtMain(
693 loop: *event.Loop,680 allocator: *Allocator,
694 flags: *const Args,681 flags: *const Args,
695 color: errmsg.Color,682 color: errmsg.Color,
696) FmtError!void {683) FmtError!void {
697 var fmt = Fmt{684 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)),
699 .any_error = false,687 .any_error = false,
700 .color = color,688 .color = color,
701 .loop = loop,
702 };689 };
703690
704 const check_mode = flags.present("check");691 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);
707 for (flags.positionals.toSliceConst()) |file_path| {694 for (flags.positionals.toSliceConst()) |file_path| {
708 try group.call(fmtPath, &fmt, file_path, check_mode);695 try group.call(fmtPath, &fmt, file_path, check_mode);
709 }696 }
...@@ -714,8 +701,8 @@ async fn asyncFmtMain(...@@ -714,8 +701,8 @@ async fn asyncFmtMain(
714}701}
715702
716async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtError!void {703async 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);704 const file_path = try std.mem.dupe(fmt.allocator, u8, file_path_ref);
718 defer fmt.loop.allocator.free(file_path);705 defer fmt.allocator.free(file_path);
719706
720 {707 {
721 const held = fmt.seen.acquire();708 const held = fmt.seen.acquire();
...@@ -724,20 +711,19 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro...@@ -724,20 +711,19 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro
724 if (try held.value.put(file_path, {})) |_| return;711 if (try held.value.put(file_path, {})) |_| return;
725 }712 }
726713
727 const source_code = (await try async event.fs.readFile(714 const source_code = event.fs.readFile(
728 fmt.loop,
729 file_path,715 file_path,
730 max_src_size,716 max_src_size,
731 )) catch |err| switch (err) {717 ) catch |err| switch (err) {
732 error.IsDir, error.AccessDenied => {718 error.IsDir, error.AccessDenied => {
733 // TODO make event based (and dir.next())719 // TODO make event based (and dir.next())
734 var dir = try fs.Dir.open(file_path);720 var dir = try fs.Dir.open(file_path);
735 defer dir.close();721 defer dir.close();
736722
737 var group = event.Group(FmtError!void).init(fmt.loop);723 var group = event.Group(FmtError!void).init(fmt.allocator);
738 while (try dir.next()) |entry| {724 while (try dir.next()) |entry| {
739 if (entry.kind == fs.Dir.Entry.Kind.Directory or mem.endsWith(u8, entry.name, ".zig")) {725 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 });
741 try group.call(fmtPath, fmt, full_path, check_mode);727 try group.call(fmtPath, fmt, full_path, check_mode);
742 }728 }
743 }729 }
...@@ -750,9 +736,9 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro...@@ -750,9 +736,9 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro
750 return;736 return;
751 },737 },
752 };738 };
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| {
756 try stderr.print("error parsing file '{}': {}\n", file_path, err);742 try stderr.print("error parsing file '{}': {}\n", file_path, err);
757 fmt.any_error = true;743 fmt.any_error = true;
758 return;744 return;
...@@ -761,8 +747,8 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro...@@ -761,8 +747,8 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro
761747
762 var error_it = tree.errors.iterator(0);748 var error_it = tree.errors.iterator(0);
763 while (error_it.next()) |parse_error| {749 while (error_it.next()) |parse_error| {
764 const msg = try errmsg.Msg.createFromParseError(fmt.loop.allocator, parse_error, tree, file_path);750 const msg = try errmsg.Msg.createFromParseError(fmt.allocator, parse_error, tree, file_path);
765 defer fmt.loop.allocator.destroy(msg);751 defer fmt.allocator.destroy(msg);
766752
767 try msg.printToFile(stderr_file, fmt.color);753 try msg.printToFile(stderr_file, fmt.color);
768 }754 }
...@@ -772,17 +758,17 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro...@@ -772,17 +758,17 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro
772 }758 }
773759
774 if (check_mode) {760 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);
776 if (anything_changed) {762 if (anything_changed) {
777 try stderr.print("{}\n", file_path);763 try stderr.print("{}\n", file_path);
778 fmt.any_error = true;764 fmt.any_error = true;
779 }765 }
780 } else {766 } else {
781 // TODO make this evented767 // 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);
783 defer baf.destroy();769 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);
786 if (anything_changed) {772 if (anything_changed) {
787 try stderr.print("{}\n", file_path);773 try stderr.print("{}\n", file_path);
788 try baf.finish();774 try baf.finish();
src-self-hosted/scope.zig+1-1
...@@ -184,7 +184,7 @@ pub const Scope = struct {...@@ -184,7 +184,7 @@ pub const Scope = struct {
184 const self = try comp.gpa().create(Decls);184 const self = try comp.gpa().create(Decls);
185 self.* = Decls{185 self.* = Decls{
186 .base = undefined,186 .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())),
188 };188 };
189 self.base.init(Id.Decls, parent);189 self.base.init(Id.Decls, parent);
190 return self;190 return self;
src-self-hosted/test.zig+4-11
...@@ -24,7 +24,6 @@ const file1 = "1.zig";...@@ -24,7 +24,6 @@ const file1 = "1.zig";
24const allocator = std.heap.c_allocator;24const allocator = std.heap.c_allocator;
2525
26pub const TestContext = struct {26pub const TestContext = struct {
27 loop: std.event.Loop,
28 zig_compiler: ZigCompiler,27 zig_compiler: ZigCompiler,
29 zig_lib_dir: []u8,28 zig_lib_dir: []u8,
30 file_index: std.atomic.Int(usize),29 file_index: std.atomic.Int(usize),
...@@ -36,20 +35,16 @@ pub const TestContext = struct {...@@ -36,20 +35,16 @@ pub const TestContext = struct {
36 fn init(self: *TestContext) !void {35 fn init(self: *TestContext) !void {
37 self.* = TestContext{36 self.* = TestContext{
38 .any_err = {},37 .any_err = {},
39 .loop = undefined,
40 .zig_compiler = undefined,38 .zig_compiler = undefined,
41 .zig_lib_dir = undefined,39 .zig_lib_dir = undefined,
42 .group = undefined,40 .group = undefined,
43 .file_index = std.atomic.Int(usize).init(0),41 .file_index = std.atomic.Int(usize).init(0),
44 };42 };
4543
46 try self.loop.initSingleThreaded(allocator);44 self.zig_compiler = try ZigCompiler.init();
47 errdefer self.loop.deinit();
48
49 self.zig_compiler = try ZigCompiler.init(&self.loop);
50 errdefer self.zig_compiler.deinit();45 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);
53 errdefer self.group.deinit();48 errdefer self.group.deinit();
5449
55 self.zig_lib_dir = try introspect.resolveZigLibDir(allocator);50 self.zig_lib_dir = try introspect.resolveZigLibDir(allocator);
...@@ -63,13 +58,11 @@ pub const TestContext = struct {...@@ -63,13 +58,11 @@ pub const TestContext = struct {
63 std.fs.deleteTree(tmp_dir_name) catch {};58 std.fs.deleteTree(tmp_dir_name) catch {};
64 allocator.free(self.zig_lib_dir);59 allocator.free(self.zig_lib_dir);
65 self.zig_compiler.deinit();60 self.zig_compiler.deinit();
66 self.loop.deinit();
67 }61 }
6862
69 fn run(self: *TestContext) !void {63 fn run(self: *TestContext) !void {
70 const handle = try self.loop.call(waitForGroup, self);64 const handle = try std.event.Loop.instance.?.call(waitForGroup, self);
71 defer await handle;65 await handle;
72 self.loop.run();
73 return self.any_err;66 return self.any_err;
74 }67 }
7568
src-self-hosted/type.zig+1-1
...@@ -178,7 +178,7 @@ pub const Type = struct {...@@ -178,7 +178,7 @@ pub const Type = struct {
178 },178 },
179 .id = id,179 .id = id,
180 .name = name,180 .name = name,
181 .abi_alignment = AbiAlignment.init(comp.loop),181 .abi_alignment = AbiAlignment.init(),
182 };182 };
183 }183 }
184184