authorgravatar for takeshi@tetrate.ioTakeshi Yoneda <takeshi@tetrate.io> 2021-06-09 17:07:06+09:00
committergravatar for takeshi@tetrate.ioTakeshi Yoneda <takeshi@tetrate.io> 2021-06-09 17:07:06+09:00
logbf568ec62a06fcea5f09c725529635425f1b8a76
treee30c6d3a7e773aa3a6ad0818c27a8c315475c2e8
parenta6ae5a77dac6466617cdf57357aa79c2b79403b3

cc,wasi: support WASI reactors via -mexec-model flag.

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>

6 files changed, 52 insertions(+), 8 deletions(-)

src/Compilation.zig+16-3
...@@ -603,6 +603,11 @@ pub const ClangPreprocessorMode = enum {...@@ -603,6 +603,11 @@ pub const ClangPreprocessorMode = enum {
603 stdout,603 stdout,
604};604};
605605
606pub const WasiExecModel = enum {
607 command,
608 reactor,
609};
610
606pub const InitOptions = struct {611pub const InitOptions = struct {
607 zig_lib_directory: Directory,612 zig_lib_directory: Directory,
608 local_cache_directory: Directory,613 local_cache_directory: Directory,
...@@ -725,6 +730,8 @@ pub const InitOptions = struct {...@@ -725,6 +730,8 @@ pub const InitOptions = struct {
725 test_filter: ?[]const u8 = null,730 test_filter: ?[]const u8 = null,
726 test_name_prefix: ?[]const u8 = null,731 test_name_prefix: ?[]const u8 = null,
727 subsystem: ?std.Target.SubSystem = null,732 subsystem: ?std.Target.SubSystem = null,
733 /// WASI-only. Type of WASI execution model ("command" or "reactor").
734 wasi_exec_model: ?WasiExecModel = null,
728};735};
729736
730fn addPackageTableToCacheHash(737fn addPackageTableToCacheHash(
...@@ -1340,6 +1347,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -1340,6 +1347,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
1340 .disable_lld_caching = options.disable_lld_caching,1347 .disable_lld_caching = options.disable_lld_caching,
1341 .subsystem = options.subsystem,1348 .subsystem = options.subsystem,
1342 .is_test = options.is_test,1349 .is_test = options.is_test,
1350 .wasi_exec_model = options.wasi_exec_model,
1343 });1351 });
1344 errdefer bin_file.destroy();1352 errdefer bin_file.destroy();
1345 comp.* = .{1353 comp.* = .{
...@@ -1441,9 +1449,14 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -1441,9 +1449,14 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
1441 .wasi_libc_crt_file = crt_file,1449 .wasi_libc_crt_file = crt_file,
1442 });1450 });
1443 }1451 }
1444 // TODO add logic deciding which crt1 we want here.1452 const crt_file: wasi_libc.CRTFile = if (comp.bin_file.options.wasi_exec_model) |exec_model| crt_file: {
1453 switch (exec_model) {
1454 .command => break :crt_file wasi_libc.CRTFile.crt1_command_o,
1455 .reactor => break :crt_file wasi_libc.CRTFile.crt1_reactor_o,
1456 }
1457 } else .crt1_o;
1445 comp.work_queue.writeAssumeCapacity(&[_]Job{1458 comp.work_queue.writeAssumeCapacity(&[_]Job{
1446 .{ .wasi_libc_crt_file = .crt1_o },1459 .{ .wasi_libc_crt_file = crt_file },
1447 .{ .wasi_libc_crt_file = .libc_a },1460 .{ .wasi_libc_crt_file = .libc_a },
1448 });1461 });
1449 }1462 }
...@@ -1868,7 +1881,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {...@@ -1868,7 +1881,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
18681881
1869 for (keys[1..]) |key, i| {1882 for (keys[1..]) |key, i| {
1870 err_msg.notes[i] = .{1883 err_msg.notes[i] = .{
1871 .src_loc = key.nodeOffsetSrcLoc(values[i+1]),1884 .src_loc = key.nodeOffsetSrcLoc(values[i + 1]),
1872 .msg = "also here",1885 .msg = "also here",
1873 };1886 };
1874 }1887 }
src/clang_options_data.zig+8-1
...@@ -5257,7 +5257,14 @@ jspd1("fxray-modes="),...@@ -5257,7 +5257,14 @@ jspd1("fxray-modes="),
5257 .psl = false,5257 .psl = false,
5258},5258},
5259jspd1("iwithsysroot"),5259jspd1("iwithsysroot"),
5260joinpd1("mexec-model="),5260.{
5261 .name = "mexec-model=",
5262 .syntax = .joined,
5263 .zig_equivalent = .exec_model,
5264 .pd1 = true,
5265 .pd2 = false,
5266 .psl = false,
5267},
5261joinpd1("mharden-sls="),5268joinpd1("mharden-sls="),
5262joinpd1("mhvx-length="),5269joinpd1("mhvx-length="),
5263jspd1("objc-isystem"),5270jspd1("objc-isystem"),
src/link.zig+3
...@@ -118,6 +118,9 @@ pub const Options = struct {...@@ -118,6 +118,9 @@ pub const Options = struct {
118 version: ?std.builtin.Version,118 version: ?std.builtin.Version,
119 libc_installation: ?*const LibCInstallation,119 libc_installation: ?*const LibCInstallation,
120120
121 /// WASI-only. Type of WASI execution model ("command" or "reactor").
122 wasi_exec_model: ?Compilation.WasiExecModel = null,
123
121 pub fn effectiveOutputMode(options: Options) std.builtin.OutputMode {124 pub fn effectiveOutputMode(options: Options) std.builtin.OutputMode {
122 return if (options.use_lld) .Obj else options.output_mode;125 return if (options.use_lld) .Obj else options.output_mode;
123 }126 }
src/link/Wasm.zig+11-4
...@@ -681,6 +681,12 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -681,6 +681,12 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
681 // Put stack before globals so that stack overflow results in segfault immediately681 // Put stack before globals so that stack overflow results in segfault immediately
682 // before corrupting globals. See https://github.com/ziglang/zig/issues/4496682 // before corrupting globals. See https://github.com/ziglang/zig/issues/4496
683 try argv.append("--stack-first");683 try argv.append("--stack-first");
684
685 // Reactor execution model does not have _start so lld doesn't look for it.
686 if (self.base.options.wasi_exec_model) |exec_model| blk: {
687 if (exec_model != .reactor) break :blk;
688 try argv.append("--no-entry");
689 }
684 } else {690 } else {
685 try argv.append("--no-entry"); // So lld doesn't look for _start.691 try argv.append("--no-entry"); // So lld doesn't look for _start.
686 try argv.append("--export-all");692 try argv.append("--export-all");
...@@ -692,10 +698,11 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {...@@ -692,10 +698,11 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
692 });698 });
693699
694 if (target.os.tag == .wasi) {700 if (target.os.tag == .wasi) {
695 if (self.base.options.link_libc and self.base.options.output_mode == .Exe) {701 const crt_name = if (self.base.options.wasi_exec_model) |exec_model|
696 // TODO work out if we want standard crt, a reactor or a command702 try std.fmt.allocPrint(arena, "crt1-{s}.o", .{@tagName(exec_model)})
697 try argv.append(try comp.get_libc_crt_file(arena, "crt1.o"));703 else
698 }704 "crt1.o";
705 try argv.append(try comp.get_libc_crt_file(arena, crt_name));
699706
700 const is_exe_or_dyn_lib = self.base.options.output_mode == .Exe or707 const is_exe_or_dyn_lib = self.base.options.output_mode == .Exe or
701 (self.base.options.output_mode == .Lib and self.base.options.link_mode == .Dynamic);708 (self.base.options.output_mode == .Lib and self.base.options.link_mode == .Dynamic);
src/main.zig+10
...@@ -613,6 +613,7 @@ fn buildOutputType(...@@ -613,6 +613,7 @@ fn buildOutputType(
613 var subsystem: ?std.Target.SubSystem = null;613 var subsystem: ?std.Target.SubSystem = null;
614 var major_subsystem_version: ?u32 = null;614 var major_subsystem_version: ?u32 = null;
615 var minor_subsystem_version: ?u32 = null;615 var minor_subsystem_version: ?u32 = null;
616 var wasi_exec_model: ?Compilation.WasiExecModel = null;
616617
617 var system_libs = std.ArrayList([]const u8).init(gpa);618 var system_libs = std.ArrayList([]const u8).init(gpa);
618 defer system_libs.deinit();619 defer system_libs.deinit();
...@@ -1254,6 +1255,13 @@ fn buildOutputType(...@@ -1254,6 +1255,13 @@ fn buildOutputType(
1254 .framework => try frameworks.append(it.only_arg),1255 .framework => try frameworks.append(it.only_arg),
1255 .nostdlibinc => want_native_include_dirs = false,1256 .nostdlibinc => want_native_include_dirs = false,
1256 .strip => strip = true,1257 .strip => strip = true,
1258 .exec_model => {
1259 if (std.mem.eql(u8, it.only_arg, "reactor")) {
1260 wasi_exec_model = Compilation.WasiExecModel.reactor;
1261 } else if (std.mem.eql(u8, it.only_arg, "command")) {
1262 wasi_exec_model = Compilation.WasiExecModel.command;
1263 }
1264 },
1257 }1265 }
1258 }1266 }
1259 // Parse linker args.1267 // Parse linker args.
...@@ -1969,6 +1977,7 @@ fn buildOutputType(...@@ -1969,6 +1977,7 @@ fn buildOutputType(
1969 .test_name_prefix = test_name_prefix,1977 .test_name_prefix = test_name_prefix,
1970 .disable_lld_caching = !have_enable_cache,1978 .disable_lld_caching = !have_enable_cache,
1971 .subsystem = subsystem,1979 .subsystem = subsystem,
1980 .wasi_exec_model = wasi_exec_model,
1972 }) catch |err| {1981 }) catch |err| {
1973 fatal("unable to create compilation: {s}", .{@errorName(err)});1982 fatal("unable to create compilation: {s}", .{@errorName(err)});
1974 };1983 };
...@@ -3341,6 +3350,7 @@ pub const ClangArgIterator = struct {...@@ -3341,6 +3350,7 @@ pub const ClangArgIterator = struct {
3341 red_zone,3350 red_zone,
3342 no_red_zone,3351 no_red_zone,
3343 strip,3352 strip,
3353 exec_model,
3344 };3354 };
33453355
3346 const Args = struct {3356 const Args = struct {
tools/update_clang_options.zig+4
...@@ -336,6 +336,10 @@ const known_options = [_]KnownOpt{...@@ -336,6 +336,10 @@ const known_options = [_]KnownOpt{
336 .name = "dynamiclib",336 .name = "dynamiclib",
337 .ident = "shared",337 .ident = "shared",
338 },338 },
339 .{
340 .name = "mexec-model",
341 .ident = "exec_model",
342 }
339};343};
340344
341const blacklisted_options = [_][]const u8{};345const blacklisted_options = [_][]const u8{};