zig/std/bootstrap.zig

37 lines
875 B
Zig
Raw Normal View History

2016-01-15 17:45:52 -08:00
import "syscall.zig";
2015-12-10 14:34:38 -08:00
2016-01-04 02:31:57 -08:00
// The compiler treats this file special by implicitly importing the function `main`
// from the root source file.
2016-01-18 20:13:14 -08:00
var argc: isize;
var argv: &&u8;
2016-01-13 17:15:51 -08:00
var env: &&u8;
2015-12-10 14:34:38 -08:00
#attribute("naked")
2016-01-13 17:15:51 -08:00
export fn _start() unreachable => {
2016-01-18 20:13:14 -08:00
argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> isize));
argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));
2016-01-13 17:15:51 -08:00
env = asm("lea 0x10(%%rsp,%%rdi,8), %[env]": [env] "=r" (-> &&u8));
call_main()
}
2016-01-13 17:15:51 -08:00
2016-01-18 20:13:14 -08:00
fn strlen(ptr: &const u8) isize => {
var count: isize = 0;
while (ptr[count] != 0) {
count += 1;
}
return count;
}
2016-01-13 17:15:51 -08:00
fn call_main() unreachable => {
var args: [argc][]u8;
for (arg, args, i) {
2016-01-13 17:15:51 -08:00
const ptr = argv[i];
args[i] = ptr[0...strlen(ptr)];
}
2016-01-23 01:14:01 -08:00
// TODO: replace the i32 cast with:
// main(args) %% exit(1)
// exit(0)
exit(i32(main(args)))
2015-12-10 14:34:38 -08:00
}