zig/std/bootstrap.zig

55 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");
2016-08-16 22:42:50 -07:00
const want_start_symbol = switch(@compileVar("os")) {
2016-12-17 14:48:07 -08:00
Os.linux => true,
else => false,
};
const want_main_symbol = !want_start_symbol;
2016-01-04 02:31:57 -08:00
var argc: usize = undefined;
var argv: &&u8 = undefined;
2016-01-13 17:15:51 -08:00
export nakedcc fn _start() -> unreachable {
@setFnVisible(this, want_start_symbol);
inline switch (@compileVar("arch")) {
2016-12-17 14: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-17 14: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-16 22:42:50 -07:00
callMainAndExit()
}
2016-01-13 17:15:51 -08:00
2016-08-16 22:42:50 -07:00
fn callMain() -> %void {
2016-12-17 14:48:07 -08:00
const args = @alloca([]u8, argc);
for (args) |_, 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);
}
2016-08-16 22:42:50 -07:00
fn callMainAndExit() -> unreachable {
callMain() %% linux.exit(1);
2016-03-01 13:11:38 -08:00
linux.exit(0);
2015-12-10 14:34:38 -08:00
}
export fn main(c_argc: i32, c_argv: &&u8) -> i32 {
@setFnVisible(this, want_main_symbol);
argc = usize(c_argc);
argv = c_argv;
2016-08-16 22:42:50 -07:00
callMain() %% return 1;
return 0;
}