Skip to content

Commit

Permalink
Define WasmExecModel enum in std.builtin.
Browse files Browse the repository at this point in the history
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
  • Loading branch information
mathetake committed Jun 20, 2021
1 parent 8d110f9 commit 6769659
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 31 deletions.
7 changes: 7 additions & 0 deletions lib/std/builtin.zig
Expand Up @@ -455,6 +455,13 @@ pub const LinkMode = enum {
Dynamic,
};

/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const WasiExecModel = enum {
command,
reactor,
};

/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.
pub const Version = struct {
Expand Down
9 changes: 4 additions & 5 deletions lib/std/start.zig
Expand Up @@ -16,7 +16,7 @@ const native_os = builtin.os.tag;

var argc_argv_ptr: [*]usize = undefined;

const start_sym_name = if (native_arch.isMIPS()) "__start" else if (builtin.is_wasi_reactor_exec_model) "_initialize" else "_start";
const start_sym_name = if (native_arch.isMIPS()) "__start" else if (builtin.wasi_exec_model == .reactor) "_initialize" else "_start";

comptime {
// No matter what, we import the root file, so that any export, test, comptime
Expand Down Expand Up @@ -142,10 +142,9 @@ fn wasm_start() callconv(.C) void {
_ = @call(.{ .modifier = .always_inline }, callMain, .{});
},
.wasi => {
if (builtin.is_wasi_reactor_exec_model) {
_ = @call(.{ .modifier = .always_inline }, callMain, .{});
} else {
std.os.wasi.proc_exit(@call(.{ .modifier = .always_inline }, callMain, .{}));
switch (builtin.wasi_exec_model) {
.reactor => _ = @call(.{ .modifier = .always_inline }, callMain, .{}),
.command => std.os.wasi.proc_exit(@call(.{ .modifier = .always_inline }, callMain, .{})),
}
},
else => @compileError("unsupported OS"),
Expand Down
10 changes: 7 additions & 3 deletions src/Compilation.zig
Expand Up @@ -724,7 +724,7 @@ pub const InitOptions = struct {
test_name_prefix: ?[]const u8 = null,
subsystem: ?std.Target.SubSystem = null,
/// WASI-only. Type of WASI execution model ("command" or "reactor").
wasi_exec_model: ?[]const u8 = null,
wasi_exec_model: ?std.builtin.WasiExecModel = null,
};

fn addPackageTableToCacheHash(
Expand Down Expand Up @@ -3638,10 +3638,14 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: *Allocator) Alloc
std.zig.fmtId(@tagName(comp.bin_file.options.machine_code_model)),
});

const wasi_exec_model_fmt = if (comp.bin_file.options.wasi_exec_model) |model|
std.zig.fmtId(@tagName(model))
else
std.zig.fmtId(@tagName(std.builtin.WasiExecModel.command));
try buffer.writer().print(
\\pub const is_wasi_reactor_exec_model = {};
\\pub const wasi_exec_model = std.builtin.WasiExecModel.{};
\\
, .{if (comp.bin_file.options.wasi_exec_model) |model| std.mem.eql(u8, model, "reactor") else false});
, .{wasi_exec_model_fmt});

if (comp.bin_file.options.is_test) {
try buffer.appendSlice(
Expand Down
2 changes: 1 addition & 1 deletion src/link.zig
Expand Up @@ -119,7 +119,7 @@ pub const Options = struct {
libc_installation: ?*const LibCInstallation,

/// WASI-only. Type of WASI execution model ("command" or "reactor").
wasi_exec_model: ?[]const u8,
wasi_exec_model: ?std.builtin.WasiExecModel,

pub fn effectiveOutputMode(options: Options) std.builtin.OutputMode {
return if (options.use_lld) .Obj else options.output_mode;
Expand Down
21 changes: 10 additions & 11 deletions src/link/Wasm.zig
Expand Up @@ -682,17 +682,16 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
// before corrupting globals. See https://github.com/ziglang/zig/issues/4496
try argv.append("--stack-first");

if (self.base.options.wasi_exec_model) |model| {
if (std.mem.eql(u8, model, "reactor")) {
// Reactor execution model does not have _start so lld doesn't look for it.
try argv.append("--no-entry");
// Make sure "_initialize" is exported even if this is pure Zig WASI reactor
// where WASM_SYMBOL_EXPORTED flag in LLVM is not set on _initialize.
try argv.appendSlice(&[_][]const u8{
"--export",
"_initialize",
});
}
if (self.base.options.wasi_exec_model) |exec_model| blk: {
if (exec_model != .reactor) break :blk;
// Reactor execution model does not have _start so lld doesn't look for it.
try argv.append("--no-entry");
// Make sure "_initialize" is exported even if this is pure Zig WASI reactor
// where WASM_SYMBOL_EXPORTED flag in LLVM is not set on _initialize.
try argv.appendSlice(&[_][]const u8{
"--export",
"_initialize",
});
}
} else {
try argv.append("--no-entry"); // So lld doesn't look for _start.
Expand Down
17 changes: 14 additions & 3 deletions src/main.zig
Expand Up @@ -617,7 +617,7 @@ fn buildOutputType(
var subsystem: ?std.Target.SubSystem = null;
var major_subsystem_version: ?u32 = null;
var minor_subsystem_version: ?u32 = null;
var wasi_exec_model: ?[]const u8 = null;
var wasi_exec_model: ?std.builtin.WasiExecModel = null;

var system_libs = std.ArrayList([]const u8).init(gpa);
defer system_libs.deinit();
Expand Down Expand Up @@ -1067,7 +1067,12 @@ fn buildOutputType(
{
try clang_argv.append(arg);
} else if (mem.startsWith(u8, arg, "-mexec-model=")) {
wasi_exec_model = arg["-mexec-model=".len..];
const value = arg["-mexec-model=".len..];
if (std.mem.eql(u8, value, "reactor")) {
wasi_exec_model = .reactor;
} else if (std.mem.eql(u8, value, "command")) {
wasi_exec_model = .command;
}
} else {
fatal("unrecognized parameter: '{s}'", .{arg});
}
Expand Down Expand Up @@ -1273,7 +1278,13 @@ fn buildOutputType(
.framework => try frameworks.append(it.only_arg),
.nostdlibinc => want_native_include_dirs = false,
.strip => strip = true,
.exec_model => wasi_exec_model = it.only_arg,
.exec_model => {
if (std.mem.eql(u8, it.only_arg, "reactor")) {
wasi_exec_model = .reactor;
} else if (std.mem.eql(u8, it.only_arg, "command")) {
wasi_exec_model = .command;
}
},
}
}
// Parse linker args.
Expand Down
14 changes: 6 additions & 8 deletions src/wasi_libc.zig
Expand Up @@ -45,19 +45,17 @@ pub fn emulatedLibCRFileLibName(crt_file: CRTFile) []const u8 {
};
}

pub fn getExecModelCRTFIle(wasi_exec_model: ?[]const u8) CRTFile {
pub fn getExecModelCRTFIle(wasi_exec_model: ?std.builtin.WasiExecModel) CRTFile {
return if (wasi_exec_model) |model|
if (std.mem.eql(u8, model, "reactor"))
CRTFile.crt1_reactor_o
else if (std.mem.eql(u8, model, "command"))
CRTFile.crt1_command_o
else
unreachable
switch (model) {
.reactor => CRTFile.crt1_reactor_o,
.command => CRTFile.crt1_command_o,
}
else
CRTFile.crt1_o;
}

pub fn execModelCRTFileFullName(wasi_exec_model: ?[]const u8) []const u8 {
pub fn execModelCRTFileFullName(wasi_exec_model: ?std.builtin.WasiExecModel) []const u8 {
return switch (getExecModelCRTFIle(wasi_exec_model)) {
.crt1_o => "crt1.o",
.crt1_reactor_o => "crt1-reactor.o",
Expand Down

0 comments on commit 6769659

Please sign in to comment.