WIP: hello world

This commit is contained in:
Shritesh Bhattarai 2019-04-13 15:15:39 -05:00
parent 63f2e96eea
commit 72bcd5a4a5
5 changed files with 136 additions and 3 deletions

View File

@ -23,6 +23,7 @@ test "std.os" {
_ = @import("os/time.zig");
_ = @import("os/windows.zig");
_ = @import("os/uefi.zig");
_ = @import("os/wasi.zig");
_ = @import("os/get_app_data_dir.zig");
}
@ -33,6 +34,7 @@ pub const freebsd = @import("os/freebsd.zig");
pub const netbsd = @import("os/netbsd.zig");
pub const zen = @import("os/zen.zig");
pub const uefi = @import("os/uefi.zig");
pub const wasi = @import("os/wasi.zig");
pub const posix = switch (builtin.os) {
Os.linux => linux,
@ -40,6 +42,7 @@ pub const posix = switch (builtin.os) {
Os.freebsd => freebsd,
Os.netbsd => netbsd,
Os.zen => zen,
Os.wasi => wasi,
else => @compileError("Unsupported OS"),
};
@ -187,7 +190,7 @@ pub fn abort() noreturn {
c.abort();
}
switch (builtin.os) {
Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => {
Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd, Os.wasi => {
_ = posix.raise(posix.SIGABRT);
_ = posix.raise(posix.SIGKILL);
while (true) {}

105
std/os/wasi.zig Normal file
View File

@ -0,0 +1,105 @@
use @import("wasi/core.zig");
pub const STDIN_FILENO = 0;
pub const STDOUT_FILENO = 1;
pub const STDERR_FILENO = 2;
pub const ESUCCESS = 0;
pub const E2BIG = 1;
pub const EACCES = 2;
pub const EADDRINUSE = 3;
pub const EADDRNOTAVAIL = 4;
pub const EAFNOSUPPORT = 5;
pub const EAGAIN = 6;
pub const EALREADY = 7;
pub const EBADF = 8;
pub const EBADMSG = 9;
pub const EBUSY = 10;
pub const ECANCELED = 11;
pub const ECHILD = 12;
pub const ECONNABORTED = 13;
pub const ECONNREFUSED = 14;
pub const ECONNRESET = 15;
pub const EDEADLK = 16;
pub const EDESTADDRREQ = 17;
pub const EDOM = 18;
pub const EDQUOT = 19;
pub const EEXIST = 20;
pub const EFAULT = 21;
pub const EFBIG = 22;
pub const EHOSTUNREACH = 23;
pub const EIDRM = 24;
pub const EILSEQ = 25;
pub const EINPROGRESS = 26;
pub const EINTR = 27;
pub const EINVAL = 28;
pub const EIO = 29;
pub const EISCONN = 30;
pub const EISDIR = 31;
pub const ELOOP = 32;
pub const EMFILE = 33;
pub const EMLINK = 34;
pub const EMSGSIZE = 35;
pub const EMULTIHOP = 36;
pub const ENAMETOOLONG = 37;
pub const ENETDOWN = 38;
pub const ENETRESET = 39;
pub const ENETUNREACH = 40;
pub const ENFILE = 41;
pub const ENOBUFS = 42;
pub const ENODEV = 43;
pub const ENOENT = 44;
pub const ENOEXEC = 45;
pub const ENOLCK = 46;
pub const ENOLINK = 47;
pub const ENOMEM = 48;
pub const ENOMSG = 49;
pub const ENOPROTOOPT = 50;
pub const ENOSPC = 51;
pub const ENOSYS = 52;
pub const ENOTCONN = 53;
pub const ENOTDIR = 54;
pub const ENOTEMPTY = 55;
pub const ENOTRECOVERABLE = 56;
pub const ENOTSOCK = 57;
pub const ENOTSUP = 58;
pub const ENOTTY = 59;
pub const ENXIO = 60;
pub const EOVERFLOW = 61;
pub const EOWNERDEAD = 62;
pub const EPERM = 63;
pub const EPIPE = 64;
pub const EPROTO = 65;
pub const EPROTONOSUPPORT = 66;
pub const EPROTOTYPE = 67;
pub const ERANGE = 68;
pub const EROFS = 69;
pub const ESPIPE = 70;
pub const ESRCH = 71;
pub const ESTALE = 72;
pub const ETIMEDOUT = 73;
pub const ETXTBSY = 74;
pub const EXDEV = 75;
pub const ENOTCAPABLE = 76;
// TODO: figure out what's going on here
pub fn getErrno(r: usize) usize {
const signed_r = @bitCast(isize, r);
return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0;
}
pub fn exit(status: i32) noreturn {
__wasi_proc_exit(@bitCast(__wasi_exitcode_t, isize(status)));
}
pub fn write(fd: i32, buf: [*]const u8, count: usize) usize {
var nwritten: usize = undefined;
const iovs = []__wasi_ciovec_t{__wasi_ciovec_t{
.buf = buf,
.buf_len = count,
}};
_ = __wasi_fd_write(@bitCast(__wasi_fd_t, isize(fd)), &iovs[0], iovs.len, &nwritten);
return nwritten;
}

12
std/os/wasi/core.zig Normal file
View File

@ -0,0 +1,12 @@
pub const __wasi_errno_t = u16;
pub const __wasi_exitcode_t = u32;
pub const __wasi_fd_t = u32;
pub const __wasi_ciovec_t = extern struct {
buf: [*]const u8,
buf_len: usize,
};
pub extern fn __wasi_proc_exit(rval: __wasi_exitcode_t) noreturn;
pub extern fn __wasi_fd_write(fd: __wasi_fd_t, iovs: *const __wasi_ciovec_t, iovs_len: usize, nwritten: *usize) __wasi_errno_t;

View File

@ -14,6 +14,8 @@ comptime {
@export("main", main, strong_linkage);
} else if (builtin.os == builtin.Os.windows) {
@export("WinMainCRTStartup", WinMainCRTStartup, strong_linkage);
} else if (builtin.os == builtin.Os.wasi) {
@export("_start", wasiStart, strong_linkage);
} else {
@export("_start", _start, strong_linkage);
}
@ -43,6 +45,16 @@ nakedcc fn _start() noreturn {
@noInlineCall(posixCallMainAndExit);
}
nakedcc fn wasiStart() noreturn {
// TODO: Decide if alloc at init is acceptable for args and env
// @llvm.wasm.mem.grow.i32
// __wasi_args_get()
// __wasi_args_sizes_get()
// __wasi_environ_get()
// __wasi_environ_sizes_get()
std.os.posix.exit(callMain());
}
extern fn WinMainCRTStartup() noreturn {
@setAlignStack(16);
if (!builtin.single_threaded) {

View File

@ -9,8 +9,9 @@ const std = @import("std");
pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {
@setCold(true);
switch (builtin.os) {
// TODO: fix panic in zen.
builtin.Os.freestanding, builtin.Os.zen => {
// TODO: fix panic in zen
// TODO: fix panic in wasi
builtin.Os.freestanding, builtin.Os.zen, builtin.Os.wasi => {
while (true) {}
},
builtin.Os.uefi => {