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 {
603603 stdout,
604604};
605605
606pub const WasiExecModel = enum {
607 command,
608 reactor,
609};
610
606611pub const InitOptions = struct {
607612 zig_lib_directory: Directory,
608613 local_cache_directory: Directory,
......@@ -725,6 +730,8 @@ pub const InitOptions = struct {
725730 test_filter: ?[]const u8 = null,
726731 test_name_prefix: ?[]const u8 = null,
727732 subsystem: ?std.Target.SubSystem = null,
733 /// WASI-only. Type of WASI execution model ("command" or "reactor").
734 wasi_exec_model: ?WasiExecModel = null,
728735};
729736
730737fn addPackageTableToCacheHash(
......@@ -1340,6 +1347,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
13401347 .disable_lld_caching = options.disable_lld_caching,
13411348 .subsystem = options.subsystem,
13421349 .is_test = options.is_test,
1350 .wasi_exec_model = options.wasi_exec_model,
13431351 });
13441352 errdefer bin_file.destroy();
13451353 comp.* = .{
......@@ -1441,9 +1449,14 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
14411449 .wasi_libc_crt_file = crt_file,
14421450 });
14431451 }
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;
14451458 comp.work_queue.writeAssumeCapacity(&[_]Job{
1446 .{ .wasi_libc_crt_file = .crt1_o },
1459 .{ .wasi_libc_crt_file = crt_file },
14471460 .{ .wasi_libc_crt_file = .libc_a },
14481461 });
14491462 }
......@@ -1868,7 +1881,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
18681881
18691882 for (keys[1..]) |key, i| {
18701883 err_msg.notes[i] = .{
1871 .src_loc = key.nodeOffsetSrcLoc(values[i+1]),
1884 .src_loc = key.nodeOffsetSrcLoc(values[i + 1]),
18721885 .msg = "also here",
18731886 };
18741887 }
src/clang_options_data.zig+8-1
......@@ -5257,7 +5257,14 @@ jspd1("fxray-modes="),
52575257 .psl = false,
52585258},
52595259jspd1("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},
52615268joinpd1("mharden-sls="),
52625269joinpd1("mhvx-length="),
52635270jspd1("objc-isystem"),
src/link.zig+3
......@@ -118,6 +118,9 @@ pub const Options = struct {
118118 version: ?std.builtin.Version,
119119 libc_installation: ?*const LibCInstallation,
120120
121 /// WASI-only. Type of WASI execution model ("command" or "reactor").
122 wasi_exec_model: ?Compilation.WasiExecModel = null,
123
121124 pub fn effectiveOutputMode(options: Options) std.builtin.OutputMode {
122125 return if (options.use_lld) .Obj else options.output_mode;
123126 }
src/link/Wasm.zig+11-4
......@@ -681,6 +681,12 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
681681 // Put stack before globals so that stack overflow results in segfault immediately
682682 // before corrupting globals. See https://github.com/ziglang/zig/issues/4496
683683 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 }
684690 } else {
685691 try argv.append("--no-entry"); // So lld doesn't look for _start.
686692 try argv.append("--export-all");
......@@ -692,10 +698,11 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
692698 });
693699
694700 if (target.os.tag == .wasi) {
695 if (self.base.options.link_libc and self.base.options.output_mode == .Exe) {
696 // TODO work out if we want standard crt, a reactor or a command
697 try argv.append(try comp.get_libc_crt_file(arena, "crt1.o"));
698 }
701 const crt_name = if (self.base.options.wasi_exec_model) |exec_model|
702 try std.fmt.allocPrint(arena, "crt1-{s}.o", .{@tagName(exec_model)})
703 else
704 "crt1.o";
705 try argv.append(try comp.get_libc_crt_file(arena, crt_name));
699706
700707 const is_exe_or_dyn_lib = self.base.options.output_mode == .Exe or
701708 (self.base.options.output_mode == .Lib and self.base.options.link_mode == .Dynamic);
src/main.zig+10
......@@ -613,6 +613,7 @@ fn buildOutputType(
613613 var subsystem: ?std.Target.SubSystem = null;
614614 var major_subsystem_version: ?u32 = null;
615615 var minor_subsystem_version: ?u32 = null;
616 var wasi_exec_model: ?Compilation.WasiExecModel = null;
616617
617618 var system_libs = std.ArrayList([]const u8).init(gpa);
618619 defer system_libs.deinit();
......@@ -1254,6 +1255,13 @@ fn buildOutputType(
12541255 .framework => try frameworks.append(it.only_arg),
12551256 .nostdlibinc => want_native_include_dirs = false,
12561257 .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 },
12571265 }
12581266 }
12591267 // Parse linker args.
......@@ -1969,6 +1977,7 @@ fn buildOutputType(
19691977 .test_name_prefix = test_name_prefix,
19701978 .disable_lld_caching = !have_enable_cache,
19711979 .subsystem = subsystem,
1980 .wasi_exec_model = wasi_exec_model,
19721981 }) catch |err| {
19731982 fatal("unable to create compilation: {s}", .{@errorName(err)});
19741983 };
......@@ -3341,6 +3350,7 @@ pub const ClangArgIterator = struct {
33413350 red_zone,
33423351 no_red_zone,
33433352 strip,
3353 exec_model,
33443354 };
33453355
33463356 const Args = struct {
tools/update_clang_options.zig+4
......@@ -336,6 +336,10 @@ const known_options = [_]KnownOpt{
336336 .name = "dynamiclib",
337337 .ident = "shared",
338338 },
339 .{
340 .name = "mexec-model",
341 .ident = "exec_model",
342 }
339343};
340344
341345const blacklisted_options = [_][]const u8{};