diff options
Diffstat (limited to 'std')
| -rw-r--r-- | std/bootstrap.zig | 29 | ||||
| -rw-r--r-- | std/index.zig | 4 | ||||
| -rw-r--r-- | std/io.zig (renamed from std/std.zig) | 54 | ||||
| -rw-r--r-- | std/os.zig | 16 | ||||
| -rw-r--r-- | std/rand.zig | 28 | ||||
| -rw-r--r-- | std/test_runner.zig | 22 | ||||
| -rw-r--r-- | std/test_runner_libc.zig | 4 | ||||
| -rw-r--r-- | std/test_runner_nolibc.zig | 4 |
8 files changed, 83 insertions, 78 deletions
diff --git a/std/bootstrap.zig b/std/bootstrap.zig index f0f177dbfd..a047500d48 100644 --- a/std/bootstrap.zig +++ b/std/bootstrap.zig @@ -1,7 +1,7 @@ -import "syscall.zig"; +// This file is in a package which has the root source file exposed as "@root". -// The compiler treats this file special by implicitly importing the function `main` -// from the root source file as the symbol `zig_user_main`. +const root = @import("@root"); +const syscall = @import("syscall.zig"); const want_start_symbol = switch(@compile_var("os")) { linux => true, @@ -26,7 +26,7 @@ export fn _start() -> unreachable { }, else => unreachable{}, } - call_main() + call_main_and_exit() } fn strlen(ptr: &const u8) -> isize { @@ -37,23 +37,24 @@ fn strlen(ptr: &const u8) -> isize { return count; } -fn call_main() -> unreachable { +fn call_main() -> %void { var args: [argc][]u8 = undefined; for (args) |arg, i| { const ptr = argv[i]; args[i] = ptr[0...strlen(ptr)]; } - zig_user_main(args) %% exit(1); - exit(0); + return root.main(args); +} + +fn call_main_and_exit() -> unreachable { + call_main() %% syscall.exit(1); + syscall.exit(0); } #condition(want_main_symbol) -export fn main(argc: i32, argv: &&u8) -> i32 { - var args: [argc][]u8 = undefined; - for (args) |arg, i| { - const ptr = argv[i]; - args[i] = ptr[0...strlen(ptr)]; - } - zig_user_main(args) %% return 1; +export fn main(c_argc: i32, c_argv: &&u8) -> i32 { + argc = c_argc; + argv = c_argv; + call_main() %% return 1; return 0; } diff --git a/std/index.zig b/std/index.zig new file mode 100644 index 0000000000..88827dc798 --- /dev/null +++ b/std/index.zig @@ -0,0 +1,4 @@ +pub const Rand = @import("rand.zig").Rand; +pub const io = @import("io.zig"); +pub const os = @import("os.zig"); +pub const math = @import("math.zig"); diff --git a/std/std.zig b/std/io.zig index 5d1e7a78ca..272459e4e7 100644 --- a/std/std.zig +++ b/std/io.zig @@ -1,6 +1,6 @@ -import "syscall.zig"; -import "errno.zig"; -import "math.zig"; +const syscall = @import("syscall.zig"); +const errno = @import("errno.zig"); +const math = @import("math.zig"); pub const stdin_fileno = 0; pub const stdout_fileno = 1; @@ -55,7 +55,7 @@ pub struct OutStream { const dest_space_left = os.buffer.len - os.index; while (src_bytes_left > 0) { - const copy_amt = min_isize(dest_space_left, src_bytes_left); + const copy_amt = math.min_isize(dest_space_left, src_bytes_left); @memcpy(&os.buffer[os.index], &str[src_index], copy_amt); os.index += copy_amt; if (os.index == os.buffer.len) { @@ -105,19 +105,19 @@ pub struct OutStream { } pub fn flush(os: &OutStream) -> %void { - const amt_written = write(os.fd, &os.buffer[0], os.index); + const amt_written = syscall.write(os.fd, &os.buffer[0], os.index); os.index = 0; if (amt_written < 0) { return switch (-amt_written) { - EINVAL => unreachable{}, - EDQUOT => error.DiskQuota, - EFBIG => error.FileTooBig, - EINTR => error.SigInterrupt, - EIO => error.Io, - ENOSPC => error.NoSpaceLeft, - EPERM => error.BadPerm, - EPIPE => error.PipeFail, - else => error.Unexpected, + errno.EINVAL => unreachable{}, + errno.EDQUOT => error.DiskQuota, + errno.EFBIG => error.FileTooBig, + errno.EINTR => error.SigInterrupt, + errno.EIO => error.Io, + errno.ENOSPC => error.NoSpaceLeft, + errno.EPERM => error.BadPerm, + errno.EPIPE => error.PipeFail, + else => error.Unexpected, } } } @@ -139,15 +139,15 @@ pub struct InStream { fd: isize, pub fn read(is: &InStream, buf: []u8) -> %isize { - const amt_read = read(is.fd, &buf[0], buf.len); + const amt_read = syscall.read(is.fd, &buf[0], buf.len); if (amt_read < 0) { return switch (-amt_read) { - EINVAL => unreachable{}, - EFAULT => unreachable{}, - EBADF => error.BadFd, - EINTR => error.SigInterrupt, - EIO => error.Io, - else => error.Unexpected, + errno.EINVAL => unreachable{}, + errno.EFAULT => unreachable{}, + errno.EBADF => error.BadFd, + errno.EINTR => error.SigInterrupt, + errno.EIO => error.Io, + else => error.Unexpected, } } return amt_read; @@ -168,8 +168,8 @@ pub struct InStream { #attribute("cold") pub fn abort() -> unreachable { - raise(SIGABRT); - raise(SIGKILL); + syscall.raise(syscall.SIGABRT); + syscall.raise(syscall.SIGKILL); while (true) {} } @@ -253,15 +253,15 @@ pub fn buf_print_f64(out_buf: []u8, x: f64, decimals: isize) -> isize { decs = max_u64_base10_digits - 1; } - if (x == f64_get_pos_inf()) { + if (x == math.f64_get_pos_inf()) { const buf2 = "+Inf"; @memcpy(&out_buf[0], &buf2[0], buf2.len); return 4; - } else if (x == f64_get_neg_inf()) { + } else if (x == math.f64_get_neg_inf()) { const buf2 = "-Inf"; @memcpy(&out_buf[0], &buf2[0], buf2.len); return 4; - } else if (f64_is_nan(x)) { + } else if (math.f64_is_nan(x)) { const buf2 = "NaN"; @memcpy(&out_buf[0], &buf2[0], buf2.len); return 3; @@ -275,7 +275,7 @@ pub fn buf_print_f64(out_buf: []u8, x: f64, decimals: isize) -> isize { // 11 exponent bits // 52 significand bits (+ 1 implicit always non-zero bit) - const bits = f64_to_bits(x); + const bits = math.f64_to_bits(x); if (bits & (1 << 63) != 0) { buf[0] = '-'; len += 1; diff --git a/std/os.zig b/std/os.zig index 75cc163f21..287b76191d 100644 --- a/std/os.zig +++ b/std/os.zig @@ -1,19 +1,19 @@ -import "syscall.zig"; -import "errno.zig"; +const syscall = @import("syscall.zig"); +const errno = @import("errno.zig"); pub error SigInterrupt; pub error Unexpected; -pub fn os_get_random_bytes(buf: []u8) -> %void { +pub fn get_random_bytes(buf: []u8) -> %void { switch (@compile_var("os")) { linux => { - const amt_got = getrandom(buf.ptr, buf.len, 0); + const amt_got = syscall.getrandom(buf.ptr, buf.len, 0); if (amt_got < 0) { return switch (-amt_got) { - EINVAL => unreachable{}, - EFAULT => unreachable{}, - EINTR => error.SigInterrupt, - else => error.Unexpected, + errno.EINVAL => unreachable{}, + errno.EFAULT => unreachable{}, + errno.EINTR => error.SigInterrupt, + else => error.Unexpected, } } }, diff --git a/std/rand.zig b/std/rand.zig index 9149304cf8..5d2d547652 100644 --- a/std/rand.zig +++ b/std/rand.zig @@ -84,26 +84,26 @@ pub struct Rand { } return bytes_left; } -} -/// Initialize random state with the given seed. -pub fn rand_new(seed: u32) -> Rand { - var r: Rand = undefined; - r.index = 0; - r.array[0] = seed; - var i : isize = 1; - var prev_value: u64 = seed; - while (i < ARRAY_SIZE) { - r.array[i] = u32((prev_value ^ (prev_value << 30)) * 0x6c078965 + u32(i)); - prev_value = r.array[i]; - i += 1; + /// Initialize random state with the given seed. + pub fn init(seed: u32) -> Rand { + var r: Rand = undefined; + r.index = 0; + r.array[0] = seed; + var i : isize = 1; + var prev_value: u64 = seed; + while (i < ARRAY_SIZE) { + r.array[i] = u32((prev_value ^ (prev_value << 30)) * 0x6c078965 + u32(i)); + prev_value = r.array[i]; + i += 1; + } + return r; } - return r; } #attribute("test") fn test_float32() { - var r = rand_new(42); + var r = Rand.init(42); // TODO for loop with range var i: i32 = 0; diff --git a/std/test_runner.zig b/std/test_runner.zig index 6715371cd2..5123eff8e2 100644 --- a/std/test_runner.zig +++ b/std/test_runner.zig @@ -1,4 +1,4 @@ -import "std.zig"; +const io = @import("std").io; struct TestFn { name: []u8, @@ -9,19 +9,19 @@ extern var zig_test_fn_list: []TestFn; pub fn run_tests() -> %void { for (zig_test_fn_list) |test_fn, i| { - %%stderr.print_str("Test "); - %%stderr.print_i64(i + 1); - %%stderr.print_str("/"); - %%stderr.print_i64(zig_test_fn_list.len); - %%stderr.print_str(" "); - %%stderr.print_str(test_fn.name); - %%stderr.print_str("..."); - %%stderr.flush(); + %%io.stderr.print_str("Test "); + %%io.stderr.print_i64(i + 1); + %%io.stderr.print_str("/"); + %%io.stderr.print_i64(zig_test_fn_list.len); + %%io.stderr.print_str(" "); + %%io.stderr.print_str(test_fn.name); + %%io.stderr.print_str("..."); + %%io.stderr.flush(); test_fn.func(); - %%stderr.print_str("OK\n"); - %%stderr.flush(); + %%io.stderr.print_str("OK\n"); + %%io.stderr.flush(); } } diff --git a/std/test_runner_libc.zig b/std/test_runner_libc.zig index 9f7c9e7faa..ff39c3325c 100644 --- a/std/test_runner_libc.zig +++ b/std/test_runner_libc.zig @@ -1,6 +1,6 @@ -import "test_runner.zig"; +const test_runner = @import("test_runner.zig"); export fn main(argc: c_int, argv: &&u8) -> c_int { - run_tests() %% return -1; + test_runner.run_tests() %% return -1; return 0; } diff --git a/std/test_runner_nolibc.zig b/std/test_runner_nolibc.zig index 4e27ca551b..b8b5a2c2d9 100644 --- a/std/test_runner_nolibc.zig +++ b/std/test_runner_nolibc.zig @@ -1,5 +1,5 @@ -import "test_runner.zig"; +const test_runner = @import("test_runner.zig"); pub fn main(args: [][]u8) -> %void { - return run_tests(); + return test_runner.run_tests(); } |
