zig/std/bootstrap.zig
Andrew Kelley d6856859d3 improvements for windows and libc integration
* standard library knows if it is linking against libc and will
   sometimes call libc functions in that case instead of providing
   redundant definitions
 * fix infinite loop bug when resolving use declarations
 * allow calling the same C function from different C imports.
   closes #277
 * push more logic from compiler to std/bootstrap.zig
 * standard library provides way to access errno
   closes #274
 * fix compile error in standard library for windows
 * add implementation of getRandomBytes for windows
2017-03-23 02:59:58 -04:00

64 lines
1.6 KiB
Zig

// 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.
const root = @import("@root");
const std = @import("std");
const want_main_symbol = std.build.linking_libc;
const want_start_symbol = !want_main_symbol;
const exit = switch(@compileVar("os")) {
Os.linux => std.linux.exit,
Os.darwin => std.darwin.exit,
else => @compileError("Unsupported OS"),
};
var argc: usize = undefined;
var argv: &&u8 = undefined;
export nakedcc fn _start() -> unreachable {
@setFnVisible(this, want_start_symbol);
if (!want_start_symbol) {
@unreachable();
}
switch (@compileVar("arch")) {
Arch.x86_64 => {
argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> usize));
argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));
},
Arch.i386 => {
argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> usize));
argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8));
},
else => @compileError("unsupported arch"),
}
callMainAndExit()
}
fn callMain() -> %void {
const args = @alloca([]u8, argc);
for (args) |_, i| {
const ptr = argv[i];
args[i] = ptr[0...std.cstr.len(ptr)];
}
return root.main(args);
}
fn callMainAndExit() -> unreachable {
callMain() %% exit(1);
exit(0);
}
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;
callMain() %% return 1;
return 0;
}