zig/std/bootstrap.zig

54 lines
1.3 KiB
Zig
Raw Normal View History

// This file is in a package which has the root source file exposed as "@root".
2015-12-10 22:34:38 +00:00
const root = @import("@root");
2016-03-01 21:11:38 +00:00
const linux = @import("linux.zig");
2016-05-07 18:52:52 +01: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 10:31:57 +00:00
var argc: isize = undefined;
var argv: &&u8 = undefined;
2016-01-14 01:15:51 +00:00
2015-12-10 22:34:38 +00: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));
},
else => unreachable{},
}
call_main_and_exit()
}
2016-01-14 01:15:51 +00:00
fn call_main() -> %void {
var args: [argc][]u8 = undefined;
for (args) |arg, i| {
2016-01-14 01:15:51 +00:00
const ptr = argv[i];
2016-05-07 18:52:52 +01:00
args[i] = ptr[0...cstr.len(ptr)];
2016-01-14 01:15:51 +00:00
}
return root.main(args);
}
fn call_main_and_exit() -> unreachable {
2016-03-01 21:11:38 +00:00
call_main() %% linux.exit(1);
linux.exit(0);
2015-12-10 22:34:38 +00: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;
}