zig/std/bootstrap.zig

54 lines
1.4 KiB
Zig
Raw Normal View History

// This file is in a package which has the root source file exposed as "@root".
2015-12-10 14:34:38 -08:00
const root = @import("@root");
2016-03-01 13:11:38 -08:00
const linux = @import("linux.zig");
2016-05-07 10:52:52 -07:00
const cstr = @import("cstr.zig");
const want_start_symbol = switch(@compile_var("os")) {
linux => true,
else => false,
};
const want_main_symbol = !want_start_symbol;
2016-01-04 02:31:57 -08:00
var argc: isize = undefined;
var argv: &&u8 = undefined;
2016-01-13 17:15:51 -08:00
2015-12-10 14:34:38 -08:00
#attribute("naked")
#condition(want_start_symbol)
export fn _start() -> unreachable {
switch (@compile_var("arch")) {
x86_64 => {
argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> isize));
argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));
},
i386 => {
argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> isize));
argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8));
},
2016-05-14 18:54:37 -07:00
else => @compile_err("unsupported arch"),
}
call_main_and_exit()
}
2016-01-13 17:15:51 -08:00
fn call_main() -> %void {
var args: [argc][]u8 = undefined;
for (args) |arg, i| {
2016-01-13 17:15:51 -08:00
const ptr = argv[i];
2016-05-07 10:52:52 -07:00
args[i] = ptr[0...cstr.len(ptr)];
2016-01-13 17:15:51 -08:00
}
return root.main(args);
}
fn call_main_and_exit() -> unreachable {
2016-03-01 13:11:38 -08:00
call_main() %% linux.exit(1);
linux.exit(0);
2015-12-10 14:34:38 -08:00
}
#condition(want_main_symbol)
export fn main(c_argc: i32, c_argv: &&u8) -> i32 {
argc = c_argc;
argv = c_argv;
call_main() %% return 1;
return 0;
}