diff --git a/example/expressions/expressions.zig b/example/expressions/expressions.zig index cab306c466..4a1090939a 100644 --- a/example/expressions/expressions.zig +++ b/example/expressions/expressions.zig @@ -1,10 +1,6 @@ export executable "expressions"; -#link("c") -extern { - fn puts(s: &const u8) -> i32; - fn exit(code: i32) -> unreachable; -} +use "std.zig"; fn other_exit() -> unreachable { if (true) { exit(0); } @@ -12,21 +8,21 @@ fn other_exit() -> unreachable { unreachable; } -export fn _start() -> unreachable { +export fn main(argc: isize, argv: &&u8, env: &&u8) -> unreachable { const a : i32 = 1; const b = 2 as i32; // const c : i32; // not yet support for const variables // const d; // parse error if (a + b == 3) { const no_conflict : i32 = 5; - if (no_conflict == 5) { puts(c"OK 1"); } + if (no_conflict == 5) { print_str("OK 1\n" as string); } } const c = { const no_conflict : i32 = 10; no_conflict }; - if (c == 10) { puts(c"OK 2"); } + if (c == 10) { print_str("OK 2\n" as string); } void_fun(1, void, 2); @@ -49,7 +45,7 @@ loop_start: if i == 3 { goto done; } - puts(c"loop"); + print_str("loop\n" as string); i = i + 1; goto loop_start; done: diff --git a/std/std.zig b/std/std.zig index 8b19628441..ea3b0d20f6 100644 --- a/std/std.zig +++ b/std/std.zig @@ -1,6 +1,20 @@ const SYS_write : isize = 1; +const SYS_exit : isize = 60; const stdout_fileno : isize = 1; +fn syscall1(number: isize, arg1: isize) -> isize { + var result : isize; + asm volatile (" + mov %[number], %%rax + mov %[arg1], %%rdi + syscall + mov %%rax, %[ret]" + : [ret] "=m" (result) + : [number] "r" (number), [arg1] "r" (arg1) + : "rcx", "r11", "rax", "rdi"); + return result; +} + fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize { var result : isize; asm volatile (" @@ -20,6 +34,11 @@ pub fn write(fd: isize, buf: &const u8, count: usize) -> isize { return syscall3(SYS_write, fd, buf as isize, count as isize); } +pub fn exit(status: i32) -> unreachable { + syscall1(SYS_exit, status as isize); + unreachable; +} + // TODO error handling // TODO handle buffering and flushing pub fn print_str(str : string) -> isize {