authorgravatar for shritesh@shritesh.comShritesh Bhattarai <shritesh@shritesh.com> 2019-04-13 15:15:39-05:00
committergravatar for shritesh@shritesh.comShritesh Bhattarai <shritesh@shritesh.com> 2019-04-13 15:15:39-05:00
log72bcd5a4a50787b0407c21e61d660e59fa74e449
tree3f19328aa871e1c042bdaccaca0d24561b70ffc5
parent63f2e96eeaaa032ffa9ff11a4a632d8032883233

WIP: hello world


5 files changed, 136 insertions(+), 3 deletions(-)

std/os.zig+4-1
......@@ -23,6 +23,7 @@ test "std.os" {
2323 _ = @import("os/time.zig");
2424 _ = @import("os/windows.zig");
2525 _ = @import("os/uefi.zig");
26 _ = @import("os/wasi.zig");
2627 _ = @import("os/get_app_data_dir.zig");
2728}
2829
......@@ -33,6 +34,7 @@ pub const freebsd = @import("os/freebsd.zig");
3334pub const netbsd = @import("os/netbsd.zig");
3435pub const zen = @import("os/zen.zig");
3536pub const uefi = @import("os/uefi.zig");
37pub const wasi = @import("os/wasi.zig");
3638
3739pub const posix = switch (builtin.os) {
3840 Os.linux => linux,
......@@ -40,6 +42,7 @@ pub const posix = switch (builtin.os) {
4042 Os.freebsd => freebsd,
4143 Os.netbsd => netbsd,
4244 Os.zen => zen,
45 Os.wasi => wasi,
4346 else => @compileError("Unsupported OS"),
4447};
4548
......@@ -187,7 +190,7 @@ pub fn abort() noreturn {
187190 c.abort();
188191 }
189192 switch (builtin.os) {
190 Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => {
193 Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd, Os.wasi => {
191194 _ = posix.raise(posix.SIGABRT);
192195 _ = posix.raise(posix.SIGKILL);
193196 while (true) {}
std/os/wasi.zig created+105
......@@ -0,0 +1,105 @@
1use @import("wasi/core.zig");
2
3pub const STDIN_FILENO = 0;
4pub const STDOUT_FILENO = 1;
5pub const STDERR_FILENO = 2;
6
7pub const ESUCCESS = 0;
8pub const E2BIG = 1;
9pub const EACCES = 2;
10pub const EADDRINUSE = 3;
11pub const EADDRNOTAVAIL = 4;
12pub const EAFNOSUPPORT = 5;
13pub const EAGAIN = 6;
14pub const EALREADY = 7;
15pub const EBADF = 8;
16pub const EBADMSG = 9;
17pub const EBUSY = 10;
18pub const ECANCELED = 11;
19pub const ECHILD = 12;
20pub const ECONNABORTED = 13;
21pub const ECONNREFUSED = 14;
22pub const ECONNRESET = 15;
23pub const EDEADLK = 16;
24pub const EDESTADDRREQ = 17;
25pub const EDOM = 18;
26pub const EDQUOT = 19;
27pub const EEXIST = 20;
28pub const EFAULT = 21;
29pub const EFBIG = 22;
30pub const EHOSTUNREACH = 23;
31pub const EIDRM = 24;
32pub const EILSEQ = 25;
33pub const EINPROGRESS = 26;
34pub const EINTR = 27;
35pub const EINVAL = 28;
36pub const EIO = 29;
37pub const EISCONN = 30;
38pub const EISDIR = 31;
39pub const ELOOP = 32;
40pub const EMFILE = 33;
41pub const EMLINK = 34;
42pub const EMSGSIZE = 35;
43pub const EMULTIHOP = 36;
44pub const ENAMETOOLONG = 37;
45pub const ENETDOWN = 38;
46pub const ENETRESET = 39;
47pub const ENETUNREACH = 40;
48pub const ENFILE = 41;
49pub const ENOBUFS = 42;
50pub const ENODEV = 43;
51pub const ENOENT = 44;
52pub const ENOEXEC = 45;
53pub const ENOLCK = 46;
54pub const ENOLINK = 47;
55pub const ENOMEM = 48;
56pub const ENOMSG = 49;
57pub const ENOPROTOOPT = 50;
58pub const ENOSPC = 51;
59pub const ENOSYS = 52;
60pub const ENOTCONN = 53;
61pub const ENOTDIR = 54;
62pub const ENOTEMPTY = 55;
63pub const ENOTRECOVERABLE = 56;
64pub const ENOTSOCK = 57;
65pub const ENOTSUP = 58;
66pub const ENOTTY = 59;
67pub const ENXIO = 60;
68pub const EOVERFLOW = 61;
69pub const EOWNERDEAD = 62;
70pub const EPERM = 63;
71pub const EPIPE = 64;
72pub const EPROTO = 65;
73pub const EPROTONOSUPPORT = 66;
74pub const EPROTOTYPE = 67;
75pub const ERANGE = 68;
76pub const EROFS = 69;
77pub const ESPIPE = 70;
78pub const ESRCH = 71;
79pub const ESTALE = 72;
80pub const ETIMEDOUT = 73;
81pub const ETXTBSY = 74;
82pub const EXDEV = 75;
83pub const ENOTCAPABLE = 76;
84
85// TODO: figure out what's going on here
86pub fn getErrno(r: usize) usize {
87 const signed_r = @bitCast(isize, r);
88 return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0;
89}
90
91pub fn exit(status: i32) noreturn {
92 __wasi_proc_exit(@bitCast(__wasi_exitcode_t, isize(status)));
93}
94
95pub fn write(fd: i32, buf: [*]const u8, count: usize) usize {
96 var nwritten: usize = undefined;
97
98 const iovs = []__wasi_ciovec_t{__wasi_ciovec_t{
99 .buf = buf,
100 .buf_len = count,
101 }};
102
103 _ = __wasi_fd_write(@bitCast(__wasi_fd_t, isize(fd)), &iovs[0], iovs.len, &nwritten);
104 return nwritten;
105}
std/os/wasi/core.zig created+12
......@@ -0,0 +1,12 @@
1pub const __wasi_errno_t = u16;
2pub const __wasi_exitcode_t = u32;
3pub const __wasi_fd_t = u32;
4
5pub const __wasi_ciovec_t = extern struct {
6 buf: [*]const u8,
7 buf_len: usize,
8};
9
10pub extern fn __wasi_proc_exit(rval: __wasi_exitcode_t) noreturn;
11
12pub extern fn __wasi_fd_write(fd: __wasi_fd_t, iovs: *const __wasi_ciovec_t, iovs_len: usize, nwritten: *usize) __wasi_errno_t;
std/special/bootstrap.zig+12
......@@ -14,6 +14,8 @@ comptime {
1414 @export("main", main, strong_linkage);
1515 } else if (builtin.os == builtin.Os.windows) {
1616 @export("WinMainCRTStartup", WinMainCRTStartup, strong_linkage);
17 } else if (builtin.os == builtin.Os.wasi) {
18 @export("_start", wasiStart, strong_linkage);
1719 } else {
1820 @export("_start", _start, strong_linkage);
1921 }
......@@ -43,6 +45,16 @@ nakedcc fn _start() noreturn {
4345 @noInlineCall(posixCallMainAndExit);
4446}
4547
48nakedcc fn wasiStart() noreturn {
49 // TODO: Decide if alloc at init is acceptable for args and env
50 // @llvm.wasm.mem.grow.i32
51 // __wasi_args_get()
52 // __wasi_args_sizes_get()
53 // __wasi_environ_get()
54 // __wasi_environ_sizes_get()
55 std.os.posix.exit(callMain());
56}
57
4658extern fn WinMainCRTStartup() noreturn {
4759 @setAlignStack(16);
4860 if (!builtin.single_threaded) {
std/special/panic.zig+3-2
......@@ -9,8 +9,9 @@ const std = @import("std");
99pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {
1010 @setCold(true);
1111 switch (builtin.os) {
12 // TODO: fix panic in zen.
13 builtin.Os.freestanding, builtin.Os.zen => {
12 // TODO: fix panic in zen
13 // TODO: fix panic in wasi
14 builtin.Os.freestanding, builtin.Os.zen, builtin.Os.wasi => {
1415 while (true) {}
1516 },
1617 builtin.Os.uefi => {