authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-30 18:39:04-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-30 18:39:04-07:00
logacf2e8fe6484a48cef76c20368ff06fe9d7b264e
treeb1deefdf85b727308494b86bf9165f8cca630328
parentd606811f555e6906d1f8189dd2bf123cf98faf91

fix start code for WebAssembly


3 files changed, 24 insertions(+), 19 deletions(-)

lib/std/start.zig+22-17
...@@ -65,9 +65,16 @@ comptime {...@@ -65,9 +65,16 @@ comptime {
65 }65 }
66 } else if (native_os == .uefi) {66 } else if (native_os == .uefi) {
67 if (!@hasDecl(root, "EfiMain")) @export(EfiMain, .{ .name = "EfiMain" });67 if (!@hasDecl(root, "EfiMain")) @export(EfiMain, .{ .name = "EfiMain" });
68 } else if (native_arch.isWasm()) {68 } else if (native_os == .wasi) {
69 const wasm_start_sym = if (builtin.wasi_exec_model == .reactor) "_initialize" else "_start";69 const wasm_start_sym = switch (builtin.wasi_exec_model) {
70 if (!@hasDecl(root, wasm_start_sym)) @export(wasm_start, .{ .name = wasm_start_sym });70 .reactor => "_initialize",
71 .command => "_start",
72 };
73 if (!@hasDecl(root, wasm_start_sym)) {
74 @export(wasi_start, .{ .name = wasm_start_sym });
75 }
76 } else if (native_arch.isWasm() and native_os == .freestanding) {
77 if (!@hasDecl(root, start_sym_name)) @export(wasm_freestanding_start, .{ .name = start_sym_name });
71 } else if (native_os != .other and native_os != .freestanding) {78 } else if (native_os != .other and native_os != .freestanding) {
72 if (!@hasDecl(root, start_sym_name)) @export(_start, .{ .name = start_sym_name });79 if (!@hasDecl(root, start_sym_name)) @export(_start, .{ .name = start_sym_name });
73 }80 }
...@@ -136,20 +143,18 @@ fn _DllMainCRTStartup(...@@ -136,20 +143,18 @@ fn _DllMainCRTStartup(
136 return std.os.windows.TRUE;143 return std.os.windows.TRUE;
137}144}
138145
139fn wasm_start() callconv(.C) void {146fn wasm_freestanding_start() callconv(.C) void {
140 // The entrypoint is marked inline because for some reason LLVM in release mode fails to inline it,147 // This is marked inline because for some reason LLVM in
141 // and we want fewer call frames in stack traces.148 // release mode fails to inline it, and we want fewer call frames in stack traces.
142 switch (native_os) {149 _ = @call(.{ .modifier = .always_inline }, callMain, .{});
143 .freestanding => {150}
144 _ = @call(.{ .modifier = .always_inline }, callMain, .{});151
145 },152fn wasi_start() callconv(.C) void {
146 .wasi => {153 // The function call is marked inline because for some reason LLVM in
147 switch (builtin.wasi_exec_model) {154 // release mode fails to inline it, and we want fewer call frames in stack traces.
148 .reactor => _ = @call(.{ .modifier = .always_inline }, callMain, .{}),155 switch (builtin.wasi_exec_model) {
149 .command => std.os.wasi.proc_exit(@call(.{ .modifier = .always_inline }, callMain, .{})),156 .reactor => _ = @call(.{ .modifier = .always_inline }, callMain, .{}),
150 }157 .command => std.os.wasi.proc_exit(@call(.{ .modifier = .always_inline }, callMain, .{})),
151 },
152 else => @compileError("unsupported OS"),
153 }158 }
154}159}
155160
test/stage2/darwin.zig+1-1
...@@ -14,7 +14,7 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -14,7 +14,7 @@ pub fn addCases(ctx: *TestContext) !void {
14 {14 {
15 var case = ctx.exe("hello world with updates", target);15 var case = ctx.exe("hello world with updates", target);
16 case.addError("", &[_][]const u8{16 case.addError("", &[_][]const u8{
17 ":86:9: error: struct 'test_case.test_case' has no member named 'main'",17 ":93:9: error: struct 'test_case.test_case' has no member named 'main'",
18 });18 });
1919
20 // Incorrect return type20 // Incorrect return type
test/stage2/test.zig+1-1
...@@ -24,7 +24,7 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -24,7 +24,7 @@ pub fn addCases(ctx: *TestContext) !void {
24 var case = ctx.exe("hello world with updates", linux_x64);24 var case = ctx.exe("hello world with updates", linux_x64);
2525
26 case.addError("", &[_][]const u8{26 case.addError("", &[_][]const u8{
27 ":86:9: error: struct 'test_case.test_case' has no member named 'main'",27 ":93:9: error: struct 'test_case.test_case' has no member named 'main'",
28 });28 });
2929
30 // Incorrect return type30 // Incorrect return type