zig/std/bootstrap.zig

64 lines
1.6 KiB
Zig
Raw Normal View History

// This file is in a package which has the root source file exposed as "@root".
// It is included in the compilation unit when exporting an executable.
2015-12-11 06:34:38 +08:00
const root = @import("@root");
const std = @import("std");
const want_main_symbol = std.build.linking_libc;
const want_start_symbol = !want_main_symbol;
2016-01-04 18:31:57 +08:00
2017-02-24 05:13:57 +08:00
const exit = switch(@compileVar("os")) {
Os.linux => std.linux.exit,
Os.darwin => std.darwin.exit,
else => @compileError("Unsupported OS"),
2017-02-24 05:13:57 +08:00
};
var argc: usize = undefined;
var argv: &&u8 = undefined;
2016-01-14 09:15:51 +08:00
export nakedcc fn _start() -> unreachable {
@setFnVisible(this, want_start_symbol);
if (!want_start_symbol) {
@unreachable();
}
switch (@compileVar("arch")) {
2016-12-18 06:48:07 +08:00
Arch.x86_64 => {
argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> usize));
argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));
},
2016-12-18 06:48:07 +08:00
Arch.i386 => {
argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> usize));
argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8));
},
else => @compileError("unsupported arch"),
}
2016-08-17 13:42:50 +08:00
callMainAndExit()
}
2016-01-14 09:15:51 +08:00
2016-08-17 13:42:50 +08:00
fn callMain() -> %void {
2016-12-18 06:48:07 +08:00
const args = @alloca([]u8, argc);
for (args) |_, i| {
2016-01-14 09:15:51 +08:00
const ptr = argv[i];
2017-02-24 05:13:57 +08:00
args[i] = ptr[0...std.cstr.len(ptr)];
2016-01-14 09:15:51 +08:00
}
return root.main(args);
}
2016-08-17 13:42:50 +08:00
fn callMainAndExit() -> unreachable {
2017-02-24 05:13:57 +08:00
callMain() %% exit(1);
exit(0);
2015-12-11 06:34:38 +08:00
}
export fn main(c_argc: i32, c_argv: &&u8) -> i32 {
@setFnVisible(this, want_main_symbol);
if (!want_main_symbol) {
@unreachable();
}
argc = usize(c_argc);
argv = c_argv;
2016-08-17 13:42:50 +08:00
callMain() %% return 1;
return 0;
}