diff --git a/CMakeLists.txt b/CMakeLists.txt index b1306d732b92261b690c75eb69ad437ddd33e551..f891cfad0fc38eeae542c0963f6ce33ba36894d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -201,41 +201,41 @@ install(TARGETS zig DESTINATION bin) install(FILES ${C_HEADERS} DESTINATION ${C_HEADERS_DEST}) -install(FILES "${CMAKE_SOURCE_DIR}/std/bootstrap.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/build.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/builtin.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/c/darwin.zig" DESTINATION "${ZIG_STD_DEST}/c") install(FILES "${CMAKE_SOURCE_DIR}/std/c/index.zig" DESTINATION "${ZIG_STD_DEST}/c") install(FILES "${CMAKE_SOURCE_DIR}/std/c/linux.zig" DESTINATION "${ZIG_STD_DEST}/c") install(FILES "${CMAKE_SOURCE_DIR}/std/c/windows.zig" DESTINATION "${ZIG_STD_DEST}/c") -install(FILES "${CMAKE_SOURCE_DIR}/std/compiler_rt.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/cstr.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/darwin.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/darwin_x86_64.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/debug.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/dwarf.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/elf.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/empty.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/endian.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/errno.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/fmt.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/hash_map.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/index.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/io.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/linux.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/linux_i386.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/linux_x86_64.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/list.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/math.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/mem.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/net.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/os.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/panic.zig" DESTINATION "${ZIG_STD_DEST}") +install(FILES "${CMAKE_SOURCE_DIR}/std/os/darwin.zig" DESTINATION "${ZIG_STD_DEST}/os") +install(FILES "${CMAKE_SOURCE_DIR}/std/os/darwin_x86_64.zig" DESTINATION "${ZIG_STD_DEST}/os") +install(FILES "${CMAKE_SOURCE_DIR}/std/os/errno.zig" DESTINATION "${ZIG_STD_DEST}/os") +install(FILES "${CMAKE_SOURCE_DIR}/std/os/index.zig" DESTINATION "${ZIG_STD_DEST}/os") +install(FILES "${CMAKE_SOURCE_DIR}/std/os/linux.zig" DESTINATION "${ZIG_STD_DEST}/os") +install(FILES "${CMAKE_SOURCE_DIR}/std/os/linux_i386.zig" DESTINATION "${ZIG_STD_DEST}/os") +install(FILES "${CMAKE_SOURCE_DIR}/std/os/linux_x86_64.zig" DESTINATION "${ZIG_STD_DEST}/os") +install(FILES "${CMAKE_SOURCE_DIR}/std/os/windows.zig" DESTINATION "${ZIG_STD_DEST}/os") install(FILES "${CMAKE_SOURCE_DIR}/std/rand.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/rand_test.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/sort.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/test_runner.zig" DESTINATION "${ZIG_STD_DEST}") -install(FILES "${CMAKE_SOURCE_DIR}/std/windows.zig" DESTINATION "${ZIG_STD_DEST}") +install(FILES "${CMAKE_SOURCE_DIR}/std/special/bootstrap.zig" DESTINATION "${ZIG_STD_DEST}/special") +install(FILES "${CMAKE_SOURCE_DIR}/std/special/builtin.zig" DESTINATION "${ZIG_STD_DEST}/special") +install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt.zig" DESTINATION "${ZIG_STD_DEST}/special") +install(FILES "${CMAKE_SOURCE_DIR}/std/special/panic.zig" DESTINATION "${ZIG_STD_DEST}/special") +install(FILES "${CMAKE_SOURCE_DIR}/std/special/test_runner.zig" DESTINATION "${ZIG_STD_DEST}/special") +install(FILES "${CMAKE_SOURCE_DIR}/std/target.zig" DESTINATION "${ZIG_STD_DEST}") add_executable(run_tests ${TEST_SOURCES}) target_link_libraries(run_tests) diff --git a/src/all_types.hpp b/src/all_types.hpp index a244e9abe0545f117c282fb5acec7db6f63bbc96..732381b2d0876568c6fe6383c07dc9de04b7cd34 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1362,6 +1362,7 @@ struct CodeGen { Buf *libc_static_lib_dir; Buf *libc_include_dir; Buf *zig_std_dir; + Buf *zig_std_special_dir; Buf *dynamic_linker; Buf *ar_path; Buf triple_str; diff --git a/src/analyze.cpp b/src/analyze.cpp index 1ba430be15b1df4fe13af9063c3b651470003249..380e384644362275c6a6d261c4f754e6a134a22d 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -2874,11 +2874,8 @@ void preview_use_decl(CodeGen *g, AstNode *node) { ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *abs_full_path, Buf *src_dirname, Buf *src_basename, Buf *source_code) { - Buf *full_path = buf_alloc(); - os_path_join(src_dirname, src_basename, full_path); - if (g->verbose) { - fprintf(stderr, "\nOriginal Source (%s):\n", buf_ptr(full_path)); + fprintf(stderr, "\nOriginal Source (%s):\n", buf_ptr(abs_full_path)); fprintf(stderr, "----------------\n"); fprintf(stderr, "%s\n", buf_ptr(source_code)); @@ -2890,7 +2887,7 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, tokenize(source_code, &tokenization); if (tokenization.err) { - ErrorMsg *err = err_msg_create_with_line(full_path, tokenization.err_line, tokenization.err_column, + ErrorMsg *err = err_msg_create_with_line(abs_full_path, tokenization.err_line, tokenization.err_column, source_code, tokenization.line_offsets, tokenization.err); print_err_msg(err, g->err_color); @@ -2908,7 +2905,7 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, import_entry->package = package; import_entry->source_code = source_code; import_entry->line_offsets = tokenization.line_offsets; - import_entry->path = full_path; + import_entry->path = abs_full_path; import_entry->root = ast_parse(source_code, tokenization.tokens, import_entry, g->err_color, &g->next_node_index); diff --git a/src/codegen.cpp b/src/codegen.cpp index 5dea908a11209f981e8d75b08259e6be5820cd97..4a99a543b71e686385452693e0f124a06cab13e1 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -80,6 +80,9 @@ CodeGen *codegen_create(Buf *root_source_dir, const ZigTarget *target) { g->root_package->package_table.put(buf_create_from_str("std"), g->std_package); g->zig_std_dir = buf_create_from_str(ZIG_STD_DIR); + g->zig_std_special_dir = buf_alloc(); + os_path_join(g->zig_std_dir, buf_sprintf("special"), g->zig_std_special_dir); + if (target) { // cross compiling, so we can't rely on all the configured stuff since @@ -4237,10 +4240,9 @@ void codegen_parseh(CodeGen *g, Buf *src_dirname, Buf *src_basename, Buf *source } static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package, const char *basename) { - Buf *std_dir = g->zig_std_dir; Buf *code_basename = buf_create_from_str(basename); Buf path_to_code_src = BUF_INIT; - os_path_join(std_dir, code_basename, &path_to_code_src); + os_path_join(g->zig_std_special_dir, code_basename, &path_to_code_src); Buf *abs_full_path = buf_alloc(); int err; if ((err = os_path_real(&path_to_code_src, abs_full_path))) { @@ -4251,18 +4253,18 @@ static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package zig_panic("unable to open '%s': %s", buf_ptr(&path_to_code_src), err_str(err)); } - return add_source_file(g, package, abs_full_path, std_dir, code_basename, import_code); + return add_source_file(g, package, abs_full_path, g->zig_std_special_dir, code_basename, import_code); } static PackageTableEntry *create_bootstrap_pkg(CodeGen *g) { - PackageTableEntry *package = new_package(buf_ptr(g->zig_std_dir), ""); + PackageTableEntry *package = new_package(buf_ptr(g->zig_std_special_dir), ""); package->package_table.put(buf_create_from_str("std"), g->std_package); package->package_table.put(buf_create_from_str("@root"), g->root_package); return package; } static PackageTableEntry *create_panic_pkg(CodeGen *g) { - PackageTableEntry *package = new_package(buf_ptr(g->zig_std_dir), ""); + PackageTableEntry *package = new_package(buf_ptr(g->zig_std_special_dir), ""); package->package_table.put(buf_create_from_str("std"), g->std_package); return package; } diff --git a/src/ir.cpp b/src/ir.cpp index 14fa780a40921692966cec4c3031d45373e266ba..9619ffd8ba64312f07139ae1c82ad942d2fed54e 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -10385,7 +10385,10 @@ static TypeTableEntry *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructi // try it as a filename target_package = import->package; import_target_path = import_target_str; - search_dir = &import->package->root_src_dir; + + // search relative to importing file + search_dir = buf_alloc(); + os_path_dirname(import->path, search_dir); } Buf full_path = BUF_INIT; diff --git a/src/link.cpp b/src/link.cpp index d0af76a47dd69618682f8d290cd2d92ec2a10238..faf9fd26e06e16ca1020f61d9f2e0fe6592906ca 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -42,10 +42,9 @@ static const char *get_o_file_extension(CodeGen *g) { static Buf *build_o(CodeGen *parent_gen, const char *oname) { Buf *source_basename = buf_sprintf("%s.zig", oname); - Buf *std_dir_path = parent_gen->zig_std_dir; ZigTarget *child_target = parent_gen->is_native_target ? nullptr : &parent_gen->zig_target; - CodeGen *child_gen = codegen_create(std_dir_path, child_target); + CodeGen *child_gen = codegen_create(parent_gen->zig_std_special_dir, child_target); child_gen->link_libc = parent_gen->link_libc; child_gen->link_libs.resize(parent_gen->link_libs.length); @@ -70,13 +69,14 @@ static Buf *build_o(CodeGen *parent_gen, const char *oname) { codegen_set_mios_version_min(child_gen, parent_gen->mios_version_min); Buf *full_path = buf_alloc(); - os_path_join(std_dir_path, source_basename, full_path); + os_path_join(parent_gen->zig_std_special_dir, source_basename, full_path); + Buf source_code = BUF_INIT; if (os_fetch_file_path(full_path, &source_code)) { zig_panic("unable to fetch file: %s\n", buf_ptr(full_path)); } - codegen_add_root_code(child_gen, std_dir_path, source_basename, &source_code); + codegen_add_root_code(child_gen, parent_gen->zig_std_special_dir, source_basename, &source_code); const char *o_ext = get_o_file_extension(child_gen); Buf *o_out = buf_sprintf("%s%s", oname, o_ext); codegen_link(child_gen, buf_ptr(o_out)); diff --git a/std/bootstrap.zig b/std/bootstrap.zig deleted file mode 100644 index e4e844b3125197a48f74b2436b92f151260a34fe..0000000000000000000000000000000000000000 --- a/std/bootstrap.zig +++ /dev/null @@ -1,63 +0,0 @@ -// This file is in a package which has the root source file exposed as "@root". -// It is included in the compilation unit when exporting an executable. - -const root = @import("@root"); -const std = @import("std"); - -const want_main_symbol = std.build.linking_libc; -const want_start_symbol = !want_main_symbol; - -const exit = switch(@compileVar("os")) { - Os.linux => std.linux.exit, - Os.darwin => std.darwin.exit, - else => @compileError("Unsupported OS"), -}; - -var argc: usize = undefined; -var argv: &&u8 = undefined; - -export nakedcc fn _start() -> noreturn { - @setFnVisible(this, want_start_symbol); - if (!want_start_symbol) { - unreachable; - } - - switch (@compileVar("arch")) { - Arch.x86_64 => { - argc = asm("mov %[argc], [rsp]": [argc] "=r" (-> usize)); - argv = asm("lea %[argv], [rsp + 8h]": [argv] "=r" (-> &&u8)); - }, - Arch.i386 => { - argc = asm("mov %[argc], [esp]": [argc] "=r" (-> usize)); - argv = asm("lea %[argv], [esp + 4h]": [argv] "=r" (-> &&u8)); - }, - else => @compileError("unsupported arch"), - } - callMainAndExit() -} - -fn callMain() -> %void { - const args = @alloca([]u8, argc); - for (args) |_, i| { - const ptr = argv[i]; - args[i] = ptr[0...std.cstr.len(ptr)]; - } - return root.main(args); -} - -fn callMainAndExit() -> noreturn { - callMain() %% exit(1); - exit(0); -} - -export fn main(c_argc: i32, c_argv: &&u8) -> i32 { - @setFnVisible(this, want_main_symbol); - if (!want_main_symbol) { - unreachable; - } - - argc = usize(c_argc); - argv = c_argv; - callMain() %% return 1; - return 0; -} diff --git a/std/build.zig b/std/build.zig deleted file mode 100644 index f6b6f7177de32c0751b1199eca975728957747cc..0000000000000000000000000000000000000000 --- a/std/build.zig +++ /dev/null @@ -1,15 +0,0 @@ -const mem = @import("mem.zig"); - -pub const linking_libc = linkingLibrary("c"); - -pub fn linkingLibrary(lib_name: []const u8) -> bool { - // TODO shouldn't need this if - if (@compileVar("link_libs").len != 0) { - for (@compileVar("link_libs")) |link_lib| { - if (mem.eql(u8, link_lib, lib_name)) { - return true; - } - } - } - return false; -} diff --git a/std/builtin.zig b/std/builtin.zig deleted file mode 100644 index 136cbd9da4f7576fa5cfaba04b257996e23e3779..0000000000000000000000000000000000000000 --- a/std/builtin.zig +++ /dev/null @@ -1,36 +0,0 @@ -// These functions are provided when not linking against libc because LLVM -// sometimes generates code that calls them. - -// Note that these functions do not return `dest`, like the libc API. -// The semantics of these functions is dictated by the corresponding -// LLVM intrinsics, not by the libc API. - -export fn memset(dest: ?&u8, c: u8, n: usize) { - @setDebugSafety(this, false); - - if (n == 0) - return; - - const d = ??dest; - var index: usize = 0; - while (index != n; index += 1) - d[index] = c; -} - -export fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) { - @setDebugSafety(this, false); - - if (n == 0) - return; - - const d = ??dest; - const s = ??src; - var index: usize = 0; - while (index != n; index += 1) - d[index] = s[index]; -} - -// Avoid dragging in the debug safety mechanisms into this .o file. -pub fn panic(message: []const u8) -> noreturn { - unreachable; -} diff --git a/std/c/index.zig b/std/c/index.zig index 05ef9e438463b33843b443bcd1de789964a576fd..c8aab299a46a980340bb2499495404da67b78565 100644 --- a/std/c/index.zig +++ b/std/c/index.zig @@ -1,13 +1,13 @@ -pub use @import("errno.zig"); +pub use @import("../os/errno.zig"); pub use switch(@compileVar("os")) { - Os.linux => @import("c/linux.zig"), - Os.windows => @import("c/windows.zig"), - Os.darwin, Os.macosx, Os.ios => @import("c/darwin.zig"), + Os.linux => @import("linux.zig"), + Os.windows => @import("windows.zig"), + Os.darwin, Os.macosx, Os.ios => @import("darwin.zig"), else => empty_import, }; pub extern fn abort() -> noreturn; -const empty_import = @import("empty.zig"); +const empty_import = @import("../empty.zig"); diff --git a/std/compiler_rt.zig b/std/compiler_rt.zig deleted file mode 100644 index f6e4c021d15e6459bf5cda1b4cef88d7bb528484..0000000000000000000000000000000000000000 --- a/std/compiler_rt.zig +++ /dev/null @@ -1,515 +0,0 @@ -// Avoid dragging in the debug safety mechanisms into this .o file, -// unless we're trying to test this file. -pub fn panic(message: []const u8) -> noreturn { - if (@compileVar("is_test")) { - @import("std").debug.panic(message); - } else { - unreachable; - } -} - -const CHAR_BIT = 8; -const du_int = u64; -const di_int = i64; -const si_int = c_int; -const su_int = c_uint; - -const udwords = [2]su_int; -const low = if (@compileVar("is_big_endian")) 1 else 0; -const high = 1 - low; - -export fn __udivdi3(a: du_int, b: du_int) -> du_int { - @setDebugSafety(this, false); - return __udivmoddi4(a, b, null); -} - -fn du_int_to_udwords(x: du_int) -> udwords { - @setDebugSafety(this, false); - return *(&udwords)(&x); -} - -export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int { - @setDebugSafety(this, false); - - const n_uword_bits = @sizeOf(su_int) * CHAR_BIT; - const n_udword_bits = @sizeOf(du_int) * CHAR_BIT; - var n = du_int_to_udwords(a); - var d = du_int_to_udwords(b); - var q: udwords = undefined; - var r: udwords = undefined; - var sr: c_uint = undefined; - // special cases, X is unknown, K != 0 - if (n[high] == 0) { - if (d[high] == 0) { - // 0 X - // --- - // 0 X - if (const rem ?= maybe_rem) { - *rem = n[low] % d[low]; - } - return n[low] / d[low]; - } - // 0 X - // --- - // K X - if (const rem ?= maybe_rem) { - *rem = n[low]; - } - return 0; - } - // n[high] != 0 - if (d[low] == 0) { - if (d[high] == 0) { - // K X - // --- - // 0 0 - if (var rem ?= maybe_rem) { - *rem = n[high] % d[low]; - } - return n[high] / d[low]; - } - // d[high] != 0 - if (n[low] == 0) { - // K 0 - // --- - // K 0 - if (var rem ?= maybe_rem) { - r[high] = n[high] % d[high]; - r[low] = 0; - *rem = *(&du_int)(&r[0]); - } - return n[high] / d[high]; - } - // K K - // --- - // K 0 - // if d is a power of 2 - if ((d[high] & (d[high] - 1)) == 0) { - if (var rem ?= maybe_rem) { - r[low] = n[low]; - r[high] = n[high] & (d[high] - 1); - *rem = *(&du_int)(&r[0]); - } - return n[high] >> @ctz(d[high]); - } - // K K - // --- - // K 0 - sr = @clz(su_int(d[high])) - @clz(su_int(n[high])); - // 0 <= sr <= n_uword_bits - 2 or sr large - if (sr > n_uword_bits - 2) { - if (var rem ?= maybe_rem) { - *rem = *(&du_int)(&n[0]); - } - return 0; - } - sr += 1; - // 1 <= sr <= n_uword_bits - 1 - // q.all = n.all << (n_udword_bits - sr); - q[low] = 0; - q[high] = n[low] << (n_uword_bits - sr); - // r.all = n.all >> sr; - r[high] = n[high] >> sr; - r[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr); - } else { - // d[low] != 0 - if (d[high] == 0) { - // K X - // --- - // 0 K - // if d is a power of 2 - if ((d[low] & (d[low] - 1)) == 0) { - if (var rem ?= maybe_rem) { - *rem = n[low] & (d[low] - 1); - } - if (d[low] == 1) { - return *(&du_int)(&n[0]); - } - sr = @ctz(d[low]); - q[high] = n[high] >> sr; - q[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr); - return *(&du_int)(&q[0]); - } - // K X - // --- - // 0 K - sr = 1 + n_uword_bits + @clz(su_int(d[low])) - @clz(su_int(n[high])); - // 2 <= sr <= n_udword_bits - 1 - // q.all = n.all << (n_udword_bits - sr); - // r.all = n.all >> sr; - if (sr == n_uword_bits) { - q[low] = 0; - q[high] = n[low]; - r[high] = 0; - r[low] = n[high]; - } else if (sr < n_uword_bits) { - // 2 <= sr <= n_uword_bits - 1 - q[low] = 0; - q[high] = n[low] << (n_uword_bits - sr); - r[high] = n[high] >> sr; - r[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr); - } else { - // n_uword_bits + 1 <= sr <= n_udword_bits - 1 - q[low] = n[low] << (n_udword_bits - sr); - q[high] = (n[high] << (n_udword_bits - sr)) | - (n[low] >> (sr - n_uword_bits)); - r[high] = 0; - r[low] = n[high] >> (sr - n_uword_bits); - } - } else { - // K X - // --- - // K K - sr = @clz(su_int(d[high])) - @clz(su_int(n[high])); - // 0 <= sr <= n_uword_bits - 1 or sr large - if (sr > n_uword_bits - 1) { - if (var rem ?= maybe_rem) { - *rem = *(&du_int)(&n[0]); - } - return 0; - } - sr += 1; - // 1 <= sr <= n_uword_bits - // q.all = n.all << (n_udword_bits - sr); - q[low] = 0; - if (sr == n_uword_bits) { - q[high] = n[low]; - r[high] = 0; - r[low] = n[high]; - } else { - q[high] = n[low] << (n_uword_bits - sr); - r[high] = n[high] >> sr; - r[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr); - } - } - } - // Not a special case - // q and r are initialized with: - // q.all = n.all << (n_udword_bits - sr); - // r.all = n.all >> sr; - // 1 <= sr <= n_udword_bits - 1 - var carry: su_int = 0; - while (sr > 0) { - // r:q = ((r:q) << 1) | carry - r[high] = (r[high] << 1) | (r[low] >> (n_uword_bits - 1)); - r[low] = (r[low] << 1) | (q[high] >> (n_uword_bits - 1)); - q[high] = (q[high] << 1) | (q[low] >> (n_uword_bits - 1)); - q[low] = (q[low] << 1) | carry; - // carry = 0; - // if (r.all >= d.all) - // { - // r.all -= d.all; - // carry = 1; - // } - const s: di_int = (di_int)(*(&du_int)(&d[0]) - *(&du_int)(&r[0]) - 1) >> (n_udword_bits - 1); - carry = su_int(s & 1); - *(&du_int)(&r[0]) -= *(&du_int)(&d[0]) & u64(s); - - sr -= 1; - } - *(&du_int)(&q[0]) = (*(&du_int)(&q[0]) << 1) | u64(carry); - if (var rem ?= maybe_rem) { - *rem = *(&du_int)(&r[0]); - } - return *(&du_int)(&q[0]); -} - -export fn __umoddi3(a: du_int, b: du_int) -> du_int { - @setDebugSafety(this, false); - - var r: du_int = undefined; - _ = __udivmoddi4(a, b, &r); - return r; -} - -fn isArmArch() -> bool { - return switch (@compileVar("arch")) { - Arch.armv8_2a, - Arch.armv8_1a, - Arch.armv8, - Arch.armv8m_baseline, - Arch.armv8m_mainline, - Arch.armv7, - Arch.armv7em, - Arch.armv7m, - Arch.armv7s, - Arch.armv7k, - Arch.armv6, - Arch.armv6m, - Arch.armv6k, - Arch.armv6t2, - Arch.armv5, - Arch.armv5te, - Arch.armv4t, - Arch.armeb => true, - else => false, - }; -} - -export nakedcc fn __aeabi_uidivmod() { - @setDebugSafety(this, false); - - if (comptime isArmArch()) { - asm volatile ( - \\ push { lr } - \\ sub sp, sp, #4 - \\ mov r2, sp - \\ bl __udivmodsi4 - \\ ldr r1, [sp] - \\ add sp, sp, #4 - \\ pop { pc } - ::: "r2", "r1"); - unreachable; - } - - @setFnVisible(this, false); -} - -export fn __udivmodsi4(a: su_int, b: su_int, rem: &su_int) -> su_int { - @setDebugSafety(this, false); - - const d = __udivsi3(a, b); - *rem = su_int(si_int(a) -% (si_int(d) * si_int(b))); - return d; -} - - -// TODO make this an alias instead of an extra function call -// https://github.com/andrewrk/zig/issues/256 - -export fn __aeabi_uidiv(n: su_int, d: su_int) -> su_int { - @setDebugSafety(this, false); - - return __udivsi3(n, d); -} - -export fn __udivsi3(n: su_int, d: su_int) -> su_int { - @setDebugSafety(this, false); - - const n_uword_bits: c_uint = @sizeOf(su_int) * CHAR_BIT; - // special cases - if (d == 0) - return 0; // ?! - if (n == 0) - return 0; - var sr: c_uint = @clz(d) - @clz(n); - // 0 <= sr <= n_uword_bits - 1 or sr large - if (sr > n_uword_bits - 1) // d > r - return 0; - if (sr == n_uword_bits - 1) // d == 1 - return n; - sr += 1; - // 1 <= sr <= n_uword_bits - 1 - // Not a special case - var q: su_int = n << (n_uword_bits - sr); - var r: su_int = n >> sr; - var carry: su_int = 0; - while (sr > 0; sr -= 1) { - // r:q = ((r:q) << 1) | carry - r = (r << 1) | (q >> (n_uword_bits - 1)); - q = (q << 1) | carry; - // carry = 0; - // if (r.all >= d.all) - // { - // r.all -= d.all; - // carry = 1; - // } - const s = si_int(d - r - 1) >> si_int(n_uword_bits - 1); - carry = su_int(s & 1); - r -= d & su_int(s); - } - q = (q << 1) | carry; - return q; -} - -test "test_umoddi3" { - test_one_umoddi3(0, 1, 0); - test_one_umoddi3(2, 1, 0); - test_one_umoddi3(0x8000000000000000, 1, 0x0); - test_one_umoddi3(0x8000000000000000, 2, 0x0); - test_one_umoddi3(0xFFFFFFFFFFFFFFFF, 2, 0x1); -} - -fn test_one_umoddi3(a: du_int, b: du_int, expected_r: du_int) { - const r = __umoddi3(a, b); - assert(r == expected_r); -} - -test "test_udivmoddi4" { - const cases = [][4]du_int { - []du_int{0x0000000000000000, 0x0000000000000001, 0x0000000000000000, 0x0000000000000000}, - []du_int{0x0000000080000000, 0x0000000100000001, 0x0000000000000000, 0x0000000080000000}, - []du_int{0x7FFFFFFF00000001, 0x0000000000000001, 0x7FFFFFFF00000001, 0x0000000000000000}, - []du_int{0x7FFFFFFF7FFFFFFF, 0xFFFFFFFFFFFFFFFF, 0x0000000000000000, 0x7FFFFFFF7FFFFFFF}, - []du_int{0x8000000000000002, 0xFFFFFFFFFFFFFFFE, 0x0000000000000000, 0x8000000000000002}, - []du_int{0x80000000FFFFFFFD, 0xFFFFFFFFFFFFFFFD, 0x0000000000000000, 0x80000000FFFFFFFD}, - []du_int{0xFFFFFFFD00000010, 0xFFFFFFFF80000000, 0x0000000000000000, 0xFFFFFFFD00000010}, - []du_int{0xFFFFFFFDFFFFFFFF, 0xFFFFFFFF7FFFFFFF, 0x0000000000000000, 0xFFFFFFFDFFFFFFFF}, - []du_int{0xFFFFFFFE0747AE14, 0xFFFFFFFF0747AE14, 0x0000000000000000, 0xFFFFFFFE0747AE14}, - []du_int{0xFFFFFFFF00000001, 0xFFFFFFFF078644FA, 0x0000000000000000, 0xFFFFFFFF00000001}, - []du_int{0xFFFFFFFF80000000, 0xFFFFFFFF00000010, 0x0000000000000001, 0x000000007FFFFFF0}, - []du_int{0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0x0000000000000001, 0x0000000000000000}, - }; - - for (cases) |case| { - test_one_udivmoddi4(case[0], case[1], case[2], case[3]); - } -} - -fn test_one_udivmoddi4(a: du_int, b: du_int, expected_q: du_int, expected_r: du_int) { - var r: du_int = undefined; - const q = __udivmoddi4(a, b, &r); - assert(q == expected_q); - assert(r == expected_r); -} - -test "test_udivsi3" { - const cases = [][3]su_int { - []su_int{0x00000000, 0x00000001, 0x00000000}, - []su_int{0x00000000, 0x00000002, 0x00000000}, - []su_int{0x00000000, 0x00000003, 0x00000000}, - []su_int{0x00000000, 0x00000010, 0x00000000}, - []su_int{0x00000000, 0x078644FA, 0x00000000}, - []su_int{0x00000000, 0x0747AE14, 0x00000000}, - []su_int{0x00000000, 0x7FFFFFFF, 0x00000000}, - []su_int{0x00000000, 0x80000000, 0x00000000}, - []su_int{0x00000000, 0xFFFFFFFD, 0x00000000}, - []su_int{0x00000000, 0xFFFFFFFE, 0x00000000}, - []su_int{0x00000000, 0xFFFFFFFF, 0x00000000}, - []su_int{0x00000001, 0x00000001, 0x00000001}, - []su_int{0x00000001, 0x00000002, 0x00000000}, - []su_int{0x00000001, 0x00000003, 0x00000000}, - []su_int{0x00000001, 0x00000010, 0x00000000}, - []su_int{0x00000001, 0x078644FA, 0x00000000}, - []su_int{0x00000001, 0x0747AE14, 0x00000000}, - []su_int{0x00000001, 0x7FFFFFFF, 0x00000000}, - []su_int{0x00000001, 0x80000000, 0x00000000}, - []su_int{0x00000001, 0xFFFFFFFD, 0x00000000}, - []su_int{0x00000001, 0xFFFFFFFE, 0x00000000}, - []su_int{0x00000001, 0xFFFFFFFF, 0x00000000}, - []su_int{0x00000002, 0x00000001, 0x00000002}, - []su_int{0x00000002, 0x00000002, 0x00000001}, - []su_int{0x00000002, 0x00000003, 0x00000000}, - []su_int{0x00000002, 0x00000010, 0x00000000}, - []su_int{0x00000002, 0x078644FA, 0x00000000}, - []su_int{0x00000002, 0x0747AE14, 0x00000000}, - []su_int{0x00000002, 0x7FFFFFFF, 0x00000000}, - []su_int{0x00000002, 0x80000000, 0x00000000}, - []su_int{0x00000002, 0xFFFFFFFD, 0x00000000}, - []su_int{0x00000002, 0xFFFFFFFE, 0x00000000}, - []su_int{0x00000002, 0xFFFFFFFF, 0x00000000}, - []su_int{0x00000003, 0x00000001, 0x00000003}, - []su_int{0x00000003, 0x00000002, 0x00000001}, - []su_int{0x00000003, 0x00000003, 0x00000001}, - []su_int{0x00000003, 0x00000010, 0x00000000}, - []su_int{0x00000003, 0x078644FA, 0x00000000}, - []su_int{0x00000003, 0x0747AE14, 0x00000000}, - []su_int{0x00000003, 0x7FFFFFFF, 0x00000000}, - []su_int{0x00000003, 0x80000000, 0x00000000}, - []su_int{0x00000003, 0xFFFFFFFD, 0x00000000}, - []su_int{0x00000003, 0xFFFFFFFE, 0x00000000}, - []su_int{0x00000003, 0xFFFFFFFF, 0x00000000}, - []su_int{0x00000010, 0x00000001, 0x00000010}, - []su_int{0x00000010, 0x00000002, 0x00000008}, - []su_int{0x00000010, 0x00000003, 0x00000005}, - []su_int{0x00000010, 0x00000010, 0x00000001}, - []su_int{0x00000010, 0x078644FA, 0x00000000}, - []su_int{0x00000010, 0x0747AE14, 0x00000000}, - []su_int{0x00000010, 0x7FFFFFFF, 0x00000000}, - []su_int{0x00000010, 0x80000000, 0x00000000}, - []su_int{0x00000010, 0xFFFFFFFD, 0x00000000}, - []su_int{0x00000010, 0xFFFFFFFE, 0x00000000}, - []su_int{0x00000010, 0xFFFFFFFF, 0x00000000}, - []su_int{0x078644FA, 0x00000001, 0x078644FA}, - []su_int{0x078644FA, 0x00000002, 0x03C3227D}, - []su_int{0x078644FA, 0x00000003, 0x028216FE}, - []su_int{0x078644FA, 0x00000010, 0x0078644F}, - []su_int{0x078644FA, 0x078644FA, 0x00000001}, - []su_int{0x078644FA, 0x0747AE14, 0x00000001}, - []su_int{0x078644FA, 0x7FFFFFFF, 0x00000000}, - []su_int{0x078644FA, 0x80000000, 0x00000000}, - []su_int{0x078644FA, 0xFFFFFFFD, 0x00000000}, - []su_int{0x078644FA, 0xFFFFFFFE, 0x00000000}, - []su_int{0x078644FA, 0xFFFFFFFF, 0x00000000}, - []su_int{0x0747AE14, 0x00000001, 0x0747AE14}, - []su_int{0x0747AE14, 0x00000002, 0x03A3D70A}, - []su_int{0x0747AE14, 0x00000003, 0x026D3A06}, - []su_int{0x0747AE14, 0x00000010, 0x00747AE1}, - []su_int{0x0747AE14, 0x078644FA, 0x00000000}, - []su_int{0x0747AE14, 0x0747AE14, 0x00000001}, - []su_int{0x0747AE14, 0x7FFFFFFF, 0x00000000}, - []su_int{0x0747AE14, 0x80000000, 0x00000000}, - []su_int{0x0747AE14, 0xFFFFFFFD, 0x00000000}, - []su_int{0x0747AE14, 0xFFFFFFFE, 0x00000000}, - []su_int{0x0747AE14, 0xFFFFFFFF, 0x00000000}, - []su_int{0x7FFFFFFF, 0x00000001, 0x7FFFFFFF}, - []su_int{0x7FFFFFFF, 0x00000002, 0x3FFFFFFF}, - []su_int{0x7FFFFFFF, 0x00000003, 0x2AAAAAAA}, - []su_int{0x7FFFFFFF, 0x00000010, 0x07FFFFFF}, - []su_int{0x7FFFFFFF, 0x078644FA, 0x00000011}, - []su_int{0x7FFFFFFF, 0x0747AE14, 0x00000011}, - []su_int{0x7FFFFFFF, 0x7FFFFFFF, 0x00000001}, - []su_int{0x7FFFFFFF, 0x80000000, 0x00000000}, - []su_int{0x7FFFFFFF, 0xFFFFFFFD, 0x00000000}, - []su_int{0x7FFFFFFF, 0xFFFFFFFE, 0x00000000}, - []su_int{0x7FFFFFFF, 0xFFFFFFFF, 0x00000000}, - []su_int{0x80000000, 0x00000001, 0x80000000}, - []su_int{0x80000000, 0x00000002, 0x40000000}, - []su_int{0x80000000, 0x00000003, 0x2AAAAAAA}, - []su_int{0x80000000, 0x00000010, 0x08000000}, - []su_int{0x80000000, 0x078644FA, 0x00000011}, - []su_int{0x80000000, 0x0747AE14, 0x00000011}, - []su_int{0x80000000, 0x7FFFFFFF, 0x00000001}, - []su_int{0x80000000, 0x80000000, 0x00000001}, - []su_int{0x80000000, 0xFFFFFFFD, 0x00000000}, - []su_int{0x80000000, 0xFFFFFFFE, 0x00000000}, - []su_int{0x80000000, 0xFFFFFFFF, 0x00000000}, - []su_int{0xFFFFFFFD, 0x00000001, 0xFFFFFFFD}, - []su_int{0xFFFFFFFD, 0x00000002, 0x7FFFFFFE}, - []su_int{0xFFFFFFFD, 0x00000003, 0x55555554}, - []su_int{0xFFFFFFFD, 0x00000010, 0x0FFFFFFF}, - []su_int{0xFFFFFFFD, 0x078644FA, 0x00000022}, - []su_int{0xFFFFFFFD, 0x0747AE14, 0x00000023}, - []su_int{0xFFFFFFFD, 0x7FFFFFFF, 0x00000001}, - []su_int{0xFFFFFFFD, 0x80000000, 0x00000001}, - []su_int{0xFFFFFFFD, 0xFFFFFFFD, 0x00000001}, - []su_int{0xFFFFFFFD, 0xFFFFFFFE, 0x00000000}, - []su_int{0xFFFFFFFD, 0xFFFFFFFF, 0x00000000}, - []su_int{0xFFFFFFFE, 0x00000001, 0xFFFFFFFE}, - []su_int{0xFFFFFFFE, 0x00000002, 0x7FFFFFFF}, - []su_int{0xFFFFFFFE, 0x00000003, 0x55555554}, - []su_int{0xFFFFFFFE, 0x00000010, 0x0FFFFFFF}, - []su_int{0xFFFFFFFE, 0x078644FA, 0x00000022}, - []su_int{0xFFFFFFFE, 0x0747AE14, 0x00000023}, - []su_int{0xFFFFFFFE, 0x7FFFFFFF, 0x00000002}, - []su_int{0xFFFFFFFE, 0x80000000, 0x00000001}, - []su_int{0xFFFFFFFE, 0xFFFFFFFD, 0x00000001}, - []su_int{0xFFFFFFFE, 0xFFFFFFFE, 0x00000001}, - []su_int{0xFFFFFFFE, 0xFFFFFFFF, 0x00000000}, - []su_int{0xFFFFFFFF, 0x00000001, 0xFFFFFFFF}, - []su_int{0xFFFFFFFF, 0x00000002, 0x7FFFFFFF}, - []su_int{0xFFFFFFFF, 0x00000003, 0x55555555}, - []su_int{0xFFFFFFFF, 0x00000010, 0x0FFFFFFF}, - []su_int{0xFFFFFFFF, 0x078644FA, 0x00000022}, - []su_int{0xFFFFFFFF, 0x0747AE14, 0x00000023}, - []su_int{0xFFFFFFFF, 0x7FFFFFFF, 0x00000002}, - []su_int{0xFFFFFFFF, 0x80000000, 0x00000001}, - []su_int{0xFFFFFFFF, 0xFFFFFFFD, 0x00000001}, - []su_int{0xFFFFFFFF, 0xFFFFFFFE, 0x00000001}, - []su_int{0xFFFFFFFF, 0xFFFFFFFF, 0x00000001}, - }; - - for (cases) |case| { - test_one_udivsi3(case[0], case[1], case[2]); - } -} - -fn test_one_udivsi3(a: su_int, b: su_int, expected_q: su_int) { - const q: su_int = __udivsi3(a, b); - assert(q == expected_q); -} - - -fn assert(ok: bool) { - if (!ok) unreachable; -} diff --git a/std/darwin.zig b/std/darwin.zig deleted file mode 100644 index 647fee40e2ff6941ba3f5b4acde16d79183d55f3..0000000000000000000000000000000000000000 --- a/std/darwin.zig +++ /dev/null @@ -1,118 +0,0 @@ - -const arch = switch (@compileVar("arch")) { - Arch.x86_64 => @import("darwin_x86_64.zig"), - else => @compileError("unsupported arch"), -}; - -const errno = @import("errno.zig"); - -pub const O_LARGEFILE = 0x0000; -pub const O_RDONLY = 0x0000; - -pub const SEEK_SET = 0x0; -pub const SEEK_CUR = 0x1; -pub const SEEK_END = 0x2; - -pub const SIGHUP = 1; -pub const SIGINT = 2; -pub const SIGQUIT = 3; -pub const SIGILL = 4; -pub const SIGTRAP = 5; -pub const SIGABRT = 6; -pub const SIGIOT = SIGABRT; -pub const SIGBUS = 7; -pub const SIGFPE = 8; -pub const SIGKILL = 9; -pub const SIGUSR1 = 10; -pub const SIGSEGV = 11; -pub const SIGUSR2 = 12; -pub const SIGPIPE = 13; -pub const SIGALRM = 14; -pub const SIGTERM = 15; -pub const SIGSTKFLT = 16; -pub const SIGCHLD = 17; -pub const SIGCONT = 18; -pub const SIGSTOP = 19; -pub const SIGTSTP = 20; -pub const SIGTTIN = 21; -pub const SIGTTOU = 22; -pub const SIGURG = 23; -pub const SIGXCPU = 24; -pub const SIGXFSZ = 25; -pub const SIGVTALRM = 26; -pub const SIGPROF = 27; -pub const SIGWINCH = 28; -pub const SIGIO = 29; -pub const SIGPOLL = 29; -pub const SIGPWR = 30; -pub const SIGSYS = 31; -pub const SIGUNUSED = SIGSYS; - -pub fn exit(status: usize) -> noreturn { - _ = arch.syscall1(arch.SYS_exit, status); - unreachable -} - -/// Get the errno from a syscall return value, or 0 for no error. -pub fn getErrno(r: usize) -> usize { - const signed_r = *(&isize)(&r); - if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0 -} - -pub fn write(fd: i32, buf: &const u8, count: usize) -> usize { - arch.syscall3(arch.SYS_write, usize(fd), usize(buf), count) -} - -pub fn close(fd: i32) -> usize { - arch.syscall1(arch.SYS_close, usize(fd)) -} - -pub fn open_c(path: &const u8, flags: usize, perm: usize) -> usize { - arch.syscall3(arch.SYS_open, usize(path), flags, perm) -} - -pub fn open(path: []const u8, flags: usize, perm: usize) -> usize { - const buf = @alloca(u8, path.len + 1); - @memcpy(&buf[0], &path[0], path.len); - buf[path.len] = 0; - return open_c(buf.ptr, flags, perm); -} - -pub fn read(fd: i32, buf: &u8, count: usize) -> usize { - arch.syscall3(arch.SYS_read, usize(fd), usize(buf), count) -} - -pub fn lseek(fd: i32, offset: usize, ref_pos: usize) -> usize { - arch.syscall3(arch.SYS_lseek, usize(fd), offset, ref_pos) -} - -pub const stat = arch.stat; -pub const timespec = arch.timespec; - -pub fn fstat(fd: i32, stat_buf: &stat) -> usize { - arch.syscall2(arch.SYS_fstat, usize(fd), usize(stat_buf)) -} - -error Unexpected; - -pub fn getrandom(buf: &u8, count: usize) -> usize { - const rr = open_c(c"/dev/urandom", O_LARGEFILE | O_RDONLY, 0); - - if(getErrno(rr) > 0) return rr; - - var fd: i32 = i32(rr); - const readRes = read(fd, buf, count); - readRes -} - -pub fn raise(sig: i32) -> i32 { - // TODO investigate whether we need to block signals before calling kill - // like we do in the linux version of raise - - //var set: sigset_t = undefined; - //blockAppSignals(&set); - const pid = i32(arch.syscall0(arch.SYS_getpid)); - const ret = i32(arch.syscall2(arch.SYS_kill, usize(pid), usize(sig))); - //restoreSignals(&set); - return ret; -} diff --git a/std/darwin_x86_64.zig b/std/darwin_x86_64.zig deleted file mode 100644 index ea6cc5a37a407dcbc11358bb6dac07790988f8fd..0000000000000000000000000000000000000000 --- a/std/darwin_x86_64.zig +++ /dev/null @@ -1,87 +0,0 @@ - -pub const SYSCALL_CLASS_SHIFT = 24; -pub const SYSCALL_CLASS_MASK = 0xFF << SYSCALL_CLASS_SHIFT; -// pub const SYSCALL_NUMBER_MASK = ~SYSCALL_CLASS_MASK; // ~ modifier not supported yet - -pub const SYSCALL_CLASS_NONE = 0; // Invalid -pub const SYSCALL_CLASS_MACH = 1; // Mach -pub const SYSCALL_CLASS_UNIX = 2; // Unix/BSD -pub const SYSCALL_CLASS_MDEP = 3; // Machine-dependent -pub const SYSCALL_CLASS_DIAG = 4; // Diagnostics - -// TODO: use the above constants to create the below values - -pub const SYS_exit = 0x2000001; -pub const SYS_read = 0x2000003; -pub const SYS_write = 0x2000004; -pub const SYS_open = 0x2000005; -pub const SYS_close = 0x2000006; -pub const SYS_kill = 0x2000025; -pub const SYS_getpid = 0x2000030; -pub const SYS_fstat = 0x20000BD; -pub const SYS_lseek = 0x20000C7; - -pub inline fn syscall0(number: usize) -> usize { - asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number) - : "rcx", "r11") -} - -pub inline fn syscall1(number: usize, arg1: usize) -> usize { - asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1) - : "rcx", "r11") -} - -pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) -> usize { - asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1), - [arg2] "{rsi}" (arg2) - : "rcx", "r11") -} - -pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) -> usize { - asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1), - [arg2] "{rsi}" (arg2), - [arg3] "{rdx}" (arg3) - : "rcx", "r11") -} - - - - -pub const stat = extern struct { - dev: u32, - mode: u16, - nlink: u16, - ino: u64, - uid: u32, - gid: u32, - rdev: u64, - - atim: timespec, - mtim: timespec, - ctim: timespec, - - size: u64, - blocks: u64, - blksize: u32, - flags: u32, - gen: u32, - lspare: i32, - qspare: [2]u64, - -}; - -pub const timespec = extern struct { - tv_sec: isize, - tv_nsec: isize, -}; diff --git a/std/debug.zig b/std/debug.zig index 6a469c3c40d452af22dcf152765799de3d020ac8..2fff730a93bbed24642b7fdbab119591525ffb99 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -1,6 +1,6 @@ const mem = @import("mem.zig"); const io = @import("io.zig"); -const os = @import("os.zig"); +const os = @import("os/index.zig"); const elf = @import("elf.zig"); const DW = @import("dwarf.zig"); const List = @import("list.zig").List; diff --git a/std/errno.zig b/std/errno.zig deleted file mode 100644 index 6f50b502258e8c009d5b2655fce53d078503966d..0000000000000000000000000000000000000000 --- a/std/errno.zig +++ /dev/null @@ -1,146 +0,0 @@ -pub const EPERM = 1; // Operation not permitted -pub const ENOENT = 2; // No such file or directory -pub const ESRCH = 3; // No such process -pub const EINTR = 4; // Interrupted system call -pub const EIO = 5; // I/O error -pub const ENXIO = 6; // No such device or address -pub const E2BIG = 7; // Arg list too long -pub const ENOEXEC = 8; // Exec format error -pub const EBADF = 9; // Bad file number -pub const ECHILD = 10; // No child processes -pub const EAGAIN = 11; // Try again -pub const ENOMEM = 12; // Out of memory -pub const EACCES = 13; // Permission denied -pub const EFAULT = 14; // Bad address -pub const ENOTBLK = 15; // Block device required -pub const EBUSY = 16; // Device or resource busy -pub const EEXIST = 17; // File exists -pub const EXDEV = 18; // Cross-device link -pub const ENODEV = 19; // No such device -pub const ENOTDIR = 20; // Not a directory -pub const EISDIR = 21; // Is a directory -pub const EINVAL = 22; // Invalid argument -pub const ENFILE = 23; // File table overflow -pub const EMFILE = 24; // Too many open files -pub const ENOTTY = 25; // Not a typewriter -pub const ETXTBSY = 26; // Text file busy -pub const EFBIG = 27; // File too large -pub const ENOSPC = 28; // No space left on device -pub const ESPIPE = 29; // Illegal seek -pub const EROFS = 30; // Read-only file system -pub const EMLINK = 31; // Too many links -pub const EPIPE = 32; // Broken pipe -pub const EDOM = 33; // Math argument out of domain of func -pub const ERANGE = 34; // Math result not representable -pub const EDEADLK = 35; // Resource deadlock would occur -pub const ENAMETOOLONG = 36; // File name too long -pub const ENOLCK = 37; // No record locks available -pub const ENOSYS = 38; // Function not implemented -pub const ENOTEMPTY = 39; // Directory not empty -pub const ELOOP = 40; // Too many symbolic links encountered -pub const EWOULDBLOCK = EAGAIN; // Operation would block -pub const ENOMSG = 42; // No message of desired type -pub const EIDRM = 43; // Identifier removed -pub const ECHRNG = 44; // Channel number out of range -pub const EL2NSYNC = 45; // Level 2 not synchronized -pub const EL3HLT = 46; // Level 3 halted -pub const EL3RST = 47; // Level 3 reset -pub const ELNRNG = 48; // Link number out of range -pub const EUNATCH = 49; // Protocol driver not attached -pub const ENOCSI = 50; // No CSI structure available -pub const EL2HLT = 51; // Level 2 halted -pub const EBADE = 52; // Invalid exchange -pub const EBADR = 53; // Invalid request descriptor -pub const EXFULL = 54; // Exchange full -pub const ENOANO = 55; // No anode -pub const EBADRQC = 56; // Invalid request code -pub const EBADSLT = 57; // Invalid slot - -pub const EBFONT = 59; // Bad font file format -pub const ENOSTR = 60; // Device not a stream -pub const ENODATA = 61; // No data available -pub const ETIME = 62; // Timer expired -pub const ENOSR = 63; // Out of streams resources -pub const ENONET = 64; // Machine is not on the network -pub const ENOPKG = 65; // Package not installed -pub const EREMOTE = 66; // Object is remote -pub const ENOLINK = 67; // Link has been severed -pub const EADV = 68; // Advertise error -pub const ESRMNT = 69; // Srmount error -pub const ECOMM = 70; // Communication error on send -pub const EPROTO = 71; // Protocol error -pub const EMULTIHOP = 72; // Multihop attempted -pub const EDOTDOT = 73; // RFS specific error -pub const EBADMSG = 74; // Not a data message -pub const EOVERFLOW = 75; // Value too large for defined data type -pub const ENOTUNIQ = 76; // Name not unique on network -pub const EBADFD = 77; // File descriptor in bad state -pub const EREMCHG = 78; // Remote address changed -pub const ELIBACC = 79; // Can not access a needed shared library -pub const ELIBBAD = 80; // Accessing a corrupted shared library -pub const ELIBSCN = 81; // .lib section in a.out corrupted -pub const ELIBMAX = 82; // Attempting to link in too many shared libraries -pub const ELIBEXEC = 83; // Cannot exec a shared library directly -pub const EILSEQ = 84; // Illegal byte sequence -pub const ERESTART = 85; // Interrupted system call should be restarted -pub const ESTRPIPE = 86; // Streams pipe error -pub const EUSERS = 87; // Too many users -pub const ENOTSOCK = 88; // Socket operation on non-socket -pub const EDESTADDRREQ = 89; // Destination address required -pub const EMSGSIZE = 90; // Message too long -pub const EPROTOTYPE = 91; // Protocol wrong type for socket -pub const ENOPROTOOPT = 92; // Protocol not available -pub const EPROTONOSUPPORT = 93; // Protocol not supported -pub const ESOCKTNOSUPPORT = 94; // Socket type not supported -pub const EOPNOTSUPP = 95; // Operation not supported on transport endpoint -pub const EPFNOSUPPORT = 96; // Protocol family not supported -pub const EAFNOSUPPORT = 97; // Address family not supported by protocol -pub const EADDRINUSE = 98; // Address already in use -pub const EADDRNOTAVAIL = 99; // Cannot assign requested address -pub const ENETDOWN = 100; // Network is down -pub const ENETUNREACH = 101; // Network is unreachable -pub const ENETRESET = 102; // Network dropped connection because of reset -pub const ECONNABORTED = 103; // Software caused connection abort -pub const ECONNRESET = 104; // Connection reset by peer -pub const ENOBUFS = 105; // No buffer space available -pub const EISCONN = 106; // Transport endpoint is already connected -pub const ENOTCONN = 107; // Transport endpoint is not connected -pub const ESHUTDOWN = 108; // Cannot send after transport endpoint shutdown -pub const ETOOMANYREFS = 109; // Too many references: cannot splice -pub const ETIMEDOUT = 110; // Connection timed out -pub const ECONNREFUSED = 111; // Connection refused -pub const EHOSTDOWN = 112; // Host is down -pub const EHOSTUNREACH = 113; // No route to host -pub const EALREADY = 114; // Operation already in progress -pub const EINPROGRESS = 115; // Operation now in progress -pub const ESTALE = 116; // Stale NFS file handle -pub const EUCLEAN = 117; // Structure needs cleaning -pub const ENOTNAM = 118; // Not a XENIX named type file -pub const ENAVAIL = 119; // No XENIX semaphores available -pub const EISNAM = 120; // Is a named type file -pub const EREMOTEIO = 121; // Remote I/O error -pub const EDQUOT = 122; // Quota exceeded - -pub const ENOMEDIUM = 123; // No medium found -pub const EMEDIUMTYPE = 124; // Wrong medium type - -// nameserver query return codes -pub const ENSROK = 0; // DNS server returned answer with no data -pub const ENSRNODATA = 160; // DNS server returned answer with no data -pub const ENSRFORMERR = 161; // DNS server claims query was misformatted -pub const ENSRSERVFAIL = 162; // DNS server returned general failure -pub const ENSRNOTFOUND = 163; // Domain name not found -pub const ENSRNOTIMP = 164; // DNS server does not implement requested operation -pub const ENSRREFUSED = 165; // DNS server refused query -pub const ENSRBADQUERY = 166; // Misformatted DNS query -pub const ENSRBADNAME = 167; // Misformatted domain name -pub const ENSRBADFAMILY = 168; // Unsupported address family -pub const ENSRBADRESP = 169; // Misformatted DNS reply -pub const ENSRCONNREFUSED = 170; // Could not contact DNS servers -pub const ENSRTIMEOUT = 171; // Timeout while contacting DNS servers -pub const ENSROF = 172; // End of file -pub const ENSRFILE = 173; // Error reading file -pub const ENSRNOMEM = 174; // Out of memory -pub const ENSRDESTRUCTION = 175; // Application terminated lookup -pub const ENSRQUERYDOMAINTOOLONG = 176; // Domain name is too long -pub const ENSRCNAMELOOP = 177; // Domain name is too long diff --git a/std/index.zig b/std/index.zig index 0a0b9557ad6cea6b5db1a873e1fc07accbda0fc5..626a3749f7f567cbd68c78881509098136a5f507 100644 --- a/std/index.zig +++ b/std/index.zig @@ -1,6 +1,7 @@ -pub const build = @import("build.zig"); +pub const c = @import("c/index.zig"); pub const cstr = @import("cstr.zig"); pub const debug = @import("debug.zig"); +pub const empty_import = @import("empty.zig"); pub const fmt = @import("fmt.zig"); pub const hash_map = @import("hash_map.zig"); pub const io = @import("io.zig"); @@ -8,15 +9,7 @@ pub const list = @import("list.zig"); pub const math = @import("math.zig"); pub const mem = @import("mem.zig"); pub const net = @import("net.zig"); -pub const os = @import("os.zig"); +pub const os = @import("os/index.zig"); pub const rand = @import("rand.zig"); pub const sort = @import("sort.zig"); -pub const linux = switch(@compileVar("os")) { - Os.linux => @import("linux.zig"), - else => empty_import, -}; -pub const darwin = switch(@compileVar("os")) { - Os.darwin => @import("darwin.zig"), - else => empty_import, -}; -pub const empty_import = @import("empty.zig"); +pub const target = @import("target.zig"); diff --git a/std/io.zig b/std/io.zig index 50733c9e13e00c19f0e35a82a84f8e02e028b189..109ed4072eee521c708ca567e02afaff73197617 100644 --- a/std/io.zig +++ b/std/io.zig @@ -1,14 +1,14 @@ const system = switch(@compileVar("os")) { - Os.linux => @import("linux.zig"), - Os.darwin => @import("darwin.zig"), + Os.linux => @import("os/linux.zig"), + Os.darwin => @import("os/darwin.zig"), else => @compileError("Unsupported OS"), }; -const errno = @import("errno.zig"); +const errno = @import("os/errno.zig"); const math = @import("math.zig"); const debug = @import("debug.zig"); const assert = debug.assert; -const os = @import("os.zig"); +const os = @import("os/index.zig"); const mem = @import("mem.zig"); const Buffer0 = @import("cstr.zig").Buffer0; const fmt = @import("fmt.zig"); diff --git a/std/linux.zig b/std/linux.zig deleted file mode 100644 index be86136deea678b6aedb3af6a9216a63c8895c5a..0000000000000000000000000000000000000000 --- a/std/linux.zig +++ /dev/null @@ -1,492 +0,0 @@ -const arch = switch (@compileVar("arch")) { - Arch.x86_64 => @import("linux_x86_64.zig"), - Arch.i386 => @import("linux_i386.zig"), - else => @compileError("unsupported arch"), -}; -const errno = @import("errno.zig"); - -pub const MMAP_PROT_NONE = 0; -pub const MMAP_PROT_READ = 1; -pub const MMAP_PROT_WRITE = 2; -pub const MMAP_PROT_EXEC = 4; - -pub const MMAP_MAP_FILE = 0; -pub const MMAP_MAP_SHARED = 1; -pub const MMAP_MAP_PRIVATE = 2; -pub const MMAP_MAP_FIXED = 16; -pub const MMAP_MAP_ANON = 32; - -pub const SIGHUP = 1; -pub const SIGINT = 2; -pub const SIGQUIT = 3; -pub const SIGILL = 4; -pub const SIGTRAP = 5; -pub const SIGABRT = 6; -pub const SIGIOT = SIGABRT; -pub const SIGBUS = 7; -pub const SIGFPE = 8; -pub const SIGKILL = 9; -pub const SIGUSR1 = 10; -pub const SIGSEGV = 11; -pub const SIGUSR2 = 12; -pub const SIGPIPE = 13; -pub const SIGALRM = 14; -pub const SIGTERM = 15; -pub const SIGSTKFLT = 16; -pub const SIGCHLD = 17; -pub const SIGCONT = 18; -pub const SIGSTOP = 19; -pub const SIGTSTP = 20; -pub const SIGTTIN = 21; -pub const SIGTTOU = 22; -pub const SIGURG = 23; -pub const SIGXCPU = 24; -pub const SIGXFSZ = 25; -pub const SIGVTALRM = 26; -pub const SIGPROF = 27; -pub const SIGWINCH = 28; -pub const SIGIO = 29; -pub const SIGPOLL = 29; -pub const SIGPWR = 30; -pub const SIGSYS = 31; -pub const SIGUNUSED = SIGSYS; - -pub const O_RDONLY = 0o0; -pub const O_WRONLY = 0o1; -pub const O_RDWR = 0o2; - -pub const O_CREAT = arch.O_CREAT; -pub const O_EXCL = arch.O_EXCL; -pub const O_NOCTTY = arch.O_NOCTTY; -pub const O_TRUNC = arch.O_TRUNC; -pub const O_APPEND = arch.O_APPEND; -pub const O_NONBLOCK = arch.O_NONBLOCK; -pub const O_DSYNC = arch.O_DSYNC; -pub const O_SYNC = arch.O_SYNC; -pub const O_RSYNC = arch.O_RSYNC; -pub const O_DIRECTORY = arch.O_DIRECTORY; -pub const O_NOFOLLOW = arch.O_NOFOLLOW; -pub const O_CLOEXEC = arch.O_CLOEXEC; - -pub const O_ASYNC = arch.O_ASYNC; -pub const O_DIRECT = arch.O_DIRECT; -pub const O_LARGEFILE = arch.O_LARGEFILE; -pub const O_NOATIME = arch.O_NOATIME; -pub const O_PATH = arch.O_PATH; -pub const O_TMPFILE = arch.O_TMPFILE; -pub const O_NDELAY = arch.O_NDELAY; - -pub const SEEK_SET = 0; -pub const SEEK_CUR = 1; -pub const SEEK_END = 2; - -const SIG_BLOCK = 0; -const SIG_UNBLOCK = 1; -const SIG_SETMASK = 2; - -pub const SOCK_STREAM = 1; -pub const SOCK_DGRAM = 2; -pub const SOCK_RAW = 3; -pub const SOCK_RDM = 4; -pub const SOCK_SEQPACKET = 5; -pub const SOCK_DCCP = 6; -pub const SOCK_PACKET = 10; -pub const SOCK_CLOEXEC = 0o2000000; -pub const SOCK_NONBLOCK = 0o4000; - - -pub const PROTO_ip = 0o000; -pub const PROTO_icmp = 0o001; -pub const PROTO_igmp = 0o002; -pub const PROTO_ggp = 0o003; -pub const PROTO_ipencap = 0o004; -pub const PROTO_st = 0o005; -pub const PROTO_tcp = 0o006; -pub const PROTO_egp = 0o010; -pub const PROTO_pup = 0o014; -pub const PROTO_udp = 0o021; -pub const PROTO_hmp = 0o024; -pub const PROTO_xns_idp = 0o026; -pub const PROTO_rdp = 0o033; -pub const PROTO_iso_tp4 = 0o035; -pub const PROTO_xtp = 0o044; -pub const PROTO_ddp = 0o045; -pub const PROTO_idpr_cmtp = 0o046; -pub const PROTO_ipv6 = 0o051; -pub const PROTO_ipv6_route = 0o053; -pub const PROTO_ipv6_frag = 0o054; -pub const PROTO_idrp = 0o055; -pub const PROTO_rsvp = 0o056; -pub const PROTO_gre = 0o057; -pub const PROTO_esp = 0o062; -pub const PROTO_ah = 0o063; -pub const PROTO_skip = 0o071; -pub const PROTO_ipv6_icmp = 0o072; -pub const PROTO_ipv6_nonxt = 0o073; -pub const PROTO_ipv6_opts = 0o074; -pub const PROTO_rspf = 0o111; -pub const PROTO_vmtp = 0o121; -pub const PROTO_ospf = 0o131; -pub const PROTO_ipip = 0o136; -pub const PROTO_encap = 0o142; -pub const PROTO_pim = 0o147; -pub const PROTO_raw = 0o377; - -pub const PF_UNSPEC = 0; -pub const PF_LOCAL = 1; -pub const PF_UNIX = PF_LOCAL; -pub const PF_FILE = PF_LOCAL; -pub const PF_INET = 2; -pub const PF_AX25 = 3; -pub const PF_IPX = 4; -pub const PF_APPLETALK = 5; -pub const PF_NETROM = 6; -pub const PF_BRIDGE = 7; -pub const PF_ATMPVC = 8; -pub const PF_X25 = 9; -pub const PF_INET6 = 10; -pub const PF_ROSE = 11; -pub const PF_DECnet = 12; -pub const PF_NETBEUI = 13; -pub const PF_SECURITY = 14; -pub const PF_KEY = 15; -pub const PF_NETLINK = 16; -pub const PF_ROUTE = PF_NETLINK; -pub const PF_PACKET = 17; -pub const PF_ASH = 18; -pub const PF_ECONET = 19; -pub const PF_ATMSVC = 20; -pub const PF_RDS = 21; -pub const PF_SNA = 22; -pub const PF_IRDA = 23; -pub const PF_PPPOX = 24; -pub const PF_WANPIPE = 25; -pub const PF_LLC = 26; -pub const PF_IB = 27; -pub const PF_MPLS = 28; -pub const PF_CAN = 29; -pub const PF_TIPC = 30; -pub const PF_BLUETOOTH = 31; -pub const PF_IUCV = 32; -pub const PF_RXRPC = 33; -pub const PF_ISDN = 34; -pub const PF_PHONET = 35; -pub const PF_IEEE802154 = 36; -pub const PF_CAIF = 37; -pub const PF_ALG = 38; -pub const PF_NFC = 39; -pub const PF_VSOCK = 40; -pub const PF_MAX = 41; - -pub const AF_UNSPEC = PF_UNSPEC; -pub const AF_LOCAL = PF_LOCAL; -pub const AF_UNIX = AF_LOCAL; -pub const AF_FILE = AF_LOCAL; -pub const AF_INET = PF_INET; -pub const AF_AX25 = PF_AX25; -pub const AF_IPX = PF_IPX; -pub const AF_APPLETALK = PF_APPLETALK; -pub const AF_NETROM = PF_NETROM; -pub const AF_BRIDGE = PF_BRIDGE; -pub const AF_ATMPVC = PF_ATMPVC; -pub const AF_X25 = PF_X25; -pub const AF_INET6 = PF_INET6; -pub const AF_ROSE = PF_ROSE; -pub const AF_DECnet = PF_DECnet; -pub const AF_NETBEUI = PF_NETBEUI; -pub const AF_SECURITY = PF_SECURITY; -pub const AF_KEY = PF_KEY; -pub const AF_NETLINK = PF_NETLINK; -pub const AF_ROUTE = PF_ROUTE; -pub const AF_PACKET = PF_PACKET; -pub const AF_ASH = PF_ASH; -pub const AF_ECONET = PF_ECONET; -pub const AF_ATMSVC = PF_ATMSVC; -pub const AF_RDS = PF_RDS; -pub const AF_SNA = PF_SNA; -pub const AF_IRDA = PF_IRDA; -pub const AF_PPPOX = PF_PPPOX; -pub const AF_WANPIPE = PF_WANPIPE; -pub const AF_LLC = PF_LLC; -pub const AF_IB = PF_IB; -pub const AF_MPLS = PF_MPLS; -pub const AF_CAN = PF_CAN; -pub const AF_TIPC = PF_TIPC; -pub const AF_BLUETOOTH = PF_BLUETOOTH; -pub const AF_IUCV = PF_IUCV; -pub const AF_RXRPC = PF_RXRPC; -pub const AF_ISDN = PF_ISDN; -pub const AF_PHONET = PF_PHONET; -pub const AF_IEEE802154 = PF_IEEE802154; -pub const AF_CAIF = PF_CAIF; -pub const AF_ALG = PF_ALG; -pub const AF_NFC = PF_NFC; -pub const AF_VSOCK = PF_VSOCK; -pub const AF_MAX = PF_MAX; - -/// Get the errno from a syscall return value, or 0 for no error. -pub fn getErrno(r: usize) -> usize { - const signed_r = *(&isize)(&r); - if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0 -} - -pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: usize, fd: i32, offset: usize) - -> usize -{ - arch.syscall6(arch.SYS_mmap, usize(address), length, prot, flags, usize(fd), offset) -} - -pub fn munmap(address: &u8, length: usize) -> usize { - arch.syscall2(arch.SYS_munmap, usize(address), length) -} - -pub fn read(fd: i32, buf: &u8, count: usize) -> usize { - arch.syscall3(arch.SYS_read, usize(fd), usize(buf), count) -} - -pub fn pread(fd: i32, buf: &u8, count: usize, offset: usize) -> usize { - arch.syscall4(arch.SYS_pread, usize(fd), usize(buf), count, offset) -} - -pub fn write(fd: i32, buf: &const u8, count: usize) -> usize { - arch.syscall3(arch.SYS_write, usize(fd), usize(buf), count) -} - -pub fn pwrite(fd: i32, buf: &const u8, count: usize, offset: usize) -> usize { - arch.syscall4(arch.SYS_pwrite, usize(fd), usize(buf), count, offset) -} - -pub fn open_c(path: &const u8, flags: usize, perm: usize) -> usize { - arch.syscall3(arch.SYS_open, usize(path), flags, perm) -} - -pub fn open(path: []const u8, flags: usize, perm: usize) -> usize { - const buf = @alloca(u8, path.len + 1); - @memcpy(&buf[0], &path[0], path.len); - buf[path.len] = 0; - return open_c(buf.ptr, flags, perm); -} - -pub fn create_c(path: &const u8, perm: usize) -> usize { - arch.syscall2(arch.SYS_creat, usize(path), perm) -} - -pub fn create(path: []const u8, perm: usize) -> usize { - const buf = @alloca(u8, path.len + 1); - @memcpy(&buf[0], &path[0], path.len); - buf[path.len] = 0; - return create_c(buf.ptr, perm); -} - -pub fn openat_c(dirfd: i32, path: &const u8, flags: usize, mode: usize) -> usize { - arch.syscall4(arch.SYS_openat, usize(dirfd), usize(path), flags, mode) -} - -pub fn openat(dirfd: i32, path: []const u8, flags: usize, mode: usize) -> usize { - const buf = @alloca(u8, path.len + 1); - @memcpy(&buf[0], &path[0], path.len); - buf[path.len] = 0; - return openat_c(dirfd, buf.ptr, flags, mode); -} - -pub fn close(fd: i32) -> usize { - arch.syscall1(arch.SYS_close, usize(fd)) -} - -pub fn lseek(fd: i32, offset: usize, ref_pos: usize) -> usize { - arch.syscall3(arch.SYS_lseek, usize(fd), offset, ref_pos) -} - -pub fn exit(status: i32) -> noreturn { - _ = arch.syscall1(arch.SYS_exit, usize(status)); - unreachable -} - -pub fn getrandom(buf: &u8, count: usize, flags: u32) -> usize { - arch.syscall3(arch.SYS_getrandom, usize(buf), count, usize(flags)) -} - -pub fn kill(pid: i32, sig: i32) -> i32 { - i32(arch.syscall2(arch.SYS_kill, usize(pid), usize(sig))) -} - -const NSIG = 65; -const sigset_t = [128]u8; -const all_mask = []u8 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }; -const app_mask = []u8 { 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, }; - -pub fn raise(sig: i32) -> i32 { - var set: sigset_t = undefined; - blockAppSignals(&set); - const tid = i32(arch.syscall0(arch.SYS_gettid)); - const ret = i32(arch.syscall2(arch.SYS_tkill, usize(tid), usize(sig))); - restoreSignals(&set); - return ret; -} - -fn blockAllSignals(set: &sigset_t) { - _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, usize(&all_mask), usize(set), NSIG/8); -} - -fn blockAppSignals(set: &sigset_t) { - _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, usize(&app_mask), usize(set), NSIG/8); -} - -fn restoreSignals(set: &sigset_t) { - _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_SETMASK, usize(set), 0, NSIG/8); -} - - -pub const sa_family_t = u16; -pub const socklen_t = u32; -pub const in_addr = u32; -pub const in6_addr = [16]u8; - -pub const sockaddr = extern struct { - family: sa_family_t, - port: u16, - data: [12]u8, -}; - -pub const sockaddr_in = extern struct { - family: sa_family_t, - port: u16, - addr: in_addr, - zero: [8]u8, -}; - -pub const sockaddr_in6 = extern struct { - family: sa_family_t, - port: u16, - flowinfo: u32, - addr: in6_addr, - scope_id: u32, -}; - -pub const iovec = extern struct { - iov_base: &u8, - iov_len: usize, -}; - -// -//const IF_NAMESIZE = 16; -// -//export struct ifreq { -// ifrn_name: [IF_NAMESIZE]u8, -// union { -// ifru_addr: sockaddr, -// ifru_dstaddr: sockaddr, -// ifru_broadaddr: sockaddr, -// ifru_netmask: sockaddr, -// ifru_hwaddr: sockaddr, -// ifru_flags: i16, -// ifru_ivalue: i32, -// ifru_mtu: i32, -// ifru_map: ifmap, -// ifru_slave: [IF_NAMESIZE]u8, -// ifru_newname: [IF_NAMESIZE]u8, -// ifru_data: &u8, -// } ifr_ifru; -//} -// - -pub fn getsockname(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize { - arch.syscall3(arch.SYS_getsockname, usize(fd), usize(addr), usize(len)) -} - -pub fn getpeername(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize { - arch.syscall3(arch.SYS_getpeername, usize(fd), usize(addr), usize(len)) -} - -pub fn socket(domain: i32, socket_type: i32, protocol: i32) -> usize { - arch.syscall3(arch.SYS_socket, usize(domain), usize(socket_type), usize(protocol)) -} - -pub fn setsockopt(fd: i32, level: i32, optname: i32, optval: &const u8, optlen: socklen_t) -> usize { - arch.syscall5(arch.SYS_setsockopt, usize(fd), usize(level), usize(optname), usize(optval), usize(optlen)) -} - -pub fn getsockopt(fd: i32, level: i32, optname: i32, noalias optval: &u8, noalias optlen: &socklen_t) -> usize { - arch.syscall5(arch.SYS_getsockopt, usize(fd), usize(level), usize(optname), usize(optval), usize(optlen)) -} - -pub fn sendmsg(fd: i32, msg: &const arch.msghdr, flags: u32) -> usize { - arch.syscall3(arch.SYS_sendmsg, usize(fd), usize(msg), flags) -} - -pub fn connect(fd: i32, addr: &const sockaddr, len: socklen_t) -> usize { - arch.syscall3(arch.SYS_connect, usize(fd), usize(addr), usize(len)) -} - -pub fn recvmsg(fd: i32, msg: &arch.msghdr, flags: u32) -> usize { - arch.syscall3(arch.SYS_recvmsg, usize(fd), usize(msg), flags) -} - -pub fn recvfrom(fd: i32, noalias buf: &u8, len: usize, flags: u32, - noalias addr: ?&sockaddr, noalias alen: ?&socklen_t) -> usize -{ - arch.syscall6(arch.SYS_recvfrom, usize(fd), usize(buf), len, flags, usize(addr), usize(alen)) -} - -pub fn shutdown(fd: i32, how: i32) -> usize { - arch.syscall2(arch.SYS_shutdown, usize(fd), usize(how)) -} - -pub fn bind(fd: i32, addr: &const sockaddr, len: socklen_t) -> usize { - arch.syscall3(arch.SYS_bind, usize(fd), usize(addr), usize(len)) -} - -pub fn listen(fd: i32, backlog: i32) -> usize { - arch.syscall2(arch.SYS_listen, usize(fd), usize(backlog)) -} - -pub fn sendto(fd: i32, buf: &const u8, len: usize, flags: u32, addr: ?&const sockaddr, alen: socklen_t) -> usize { - arch.syscall6(arch.SYS_sendto, usize(fd), usize(buf), len, flags, usize(addr), usize(alen)) -} - -pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) -> usize { - arch.syscall4(arch.SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), usize(&fd[0])) -} - -pub fn accept(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize { - accept4(fd, addr, len, 0) -} - -pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags: u32) -> usize { - arch.syscall4(arch.SYS_accept4, usize(fd), usize(addr), usize(len), flags) -} - -// error NameTooLong; -// error SystemResources; -// error Io; -// -// pub fn if_nametoindex(name: []u8) -> %u32 { -// var ifr: ifreq = undefined; -// -// if (name.len >= ifr.ifr_name.len) { -// return error.NameTooLong; -// } -// -// const socket_ret = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); -// const socket_err = getErrno(socket_ret); -// if (socket_err > 0) { -// return error.SystemResources; -// } -// const socket_fd = i32(socket_ret); -// @memcpy(&ifr.ifr_name[0], &name[0], name.len); -// ifr.ifr_name[name.len] = 0; -// const ioctl_ret = ioctl(socket_fd, SIOCGIFINDEX, &ifr); -// close(socket_fd); -// const ioctl_err = getErrno(ioctl_ret); -// if (ioctl_err > 0) { -// return error.Io; -// } -// return ifr.ifr_ifindex; -// } - -pub const stat = arch.stat; -pub const timespec = arch.timespec; - -pub fn fstat(fd: i32, stat_buf: &stat) -> usize { - arch.syscall2(arch.SYS_fstat, usize(fd), usize(stat_buf)) -} diff --git a/std/linux_i386.zig b/std/linux_i386.zig deleted file mode 100644 index 218043307e31e12479113c1cf0f255981b84b541..0000000000000000000000000000000000000000 --- a/std/linux_i386.zig +++ /dev/null @@ -1,497 +0,0 @@ -const linux = @import("linux.zig"); -const socklen_t = linux.socklen_t; -const iovec = linux.iovec; - -pub const SYS_restart_syscall = 0; -pub const SYS_exit = 1; -pub const SYS_fork = 2; -pub const SYS_read = 3; -pub const SYS_write = 4; -pub const SYS_open = 5; -pub const SYS_close = 6; -pub const SYS_waitpid = 7; -pub const SYS_creat = 8; -pub const SYS_link = 9; -pub const SYS_unlink = 10; -pub const SYS_execve = 11; -pub const SYS_chdir = 12; -pub const SYS_time = 13; -pub const SYS_mknod = 14; -pub const SYS_chmod = 15; -pub const SYS_lchown = 16; -pub const SYS_break = 17; -pub const SYS_oldstat = 18; -pub const SYS_lseek = 19; -pub const SYS_getpid = 20; -pub const SYS_mount = 21; -pub const SYS_umount = 22; -pub const SYS_setuid = 23; -pub const SYS_getuid = 24; -pub const SYS_stime = 25; -pub const SYS_ptrace = 26; -pub const SYS_alarm = 27; -pub const SYS_oldfstat = 28; -pub const SYS_pause = 29; -pub const SYS_utime = 30; -pub const SYS_stty = 31; -pub const SYS_gtty = 32; -pub const SYS_access = 33; -pub const SYS_nice = 34; -pub const SYS_ftime = 35; -pub const SYS_sync = 36; -pub const SYS_kill = 37; -pub const SYS_rename = 38; -pub const SYS_mkdir = 39; -pub const SYS_rmdir = 40; -pub const SYS_dup = 41; -pub const SYS_pipe = 42; -pub const SYS_times = 43; -pub const SYS_prof = 44; -pub const SYS_brk = 45; -pub const SYS_setgid = 46; -pub const SYS_getgid = 47; -pub const SYS_signal = 48; -pub const SYS_geteuid = 49; -pub const SYS_getegid = 50; -pub const SYS_acct = 51; -pub const SYS_umount2 = 52; -pub const SYS_lock = 53; -pub const SYS_ioctl = 54; -pub const SYS_fcntl = 55; -pub const SYS_mpx = 56; -pub const SYS_setpgid = 57; -pub const SYS_ulimit = 58; -pub const SYS_oldolduname = 59; -pub const SYS_umask = 60; -pub const SYS_chroot = 61; -pub const SYS_ustat = 62; -pub const SYS_dup2 = 63; -pub const SYS_getppid = 64; -pub const SYS_getpgrp = 65; -pub const SYS_setsid = 66; -pub const SYS_sigaction = 67; -pub const SYS_sgetmask = 68; -pub const SYS_ssetmask = 69; -pub const SYS_setreuid = 70; -pub const SYS_setregid = 71; -pub const SYS_sigsuspend = 72; -pub const SYS_sigpending = 73; -pub const SYS_sethostname = 74; -pub const SYS_setrlimit = 75; -pub const SYS_getrlimit = 76; -pub const SYS_getrusage = 77; -pub const SYS_gettimeofday = 78; -pub const SYS_settimeofday = 79; -pub const SYS_getgroups = 80; -pub const SYS_setgroups = 81; -pub const SYS_select = 82; -pub const SYS_symlink = 83; -pub const SYS_oldlstat = 84; -pub const SYS_readlink = 85; -pub const SYS_uselib = 86; -pub const SYS_swapon = 87; -pub const SYS_reboot = 88; -pub const SYS_readdir = 89; -pub const SYS_mmap = 90; -pub const SYS_munmap = 91; -pub const SYS_truncate = 92; -pub const SYS_ftruncate = 93; -pub const SYS_fchmod = 94; -pub const SYS_fchown = 95; -pub const SYS_getpriority = 96; -pub const SYS_setpriority = 97; -pub const SYS_profil = 98; -pub const SYS_statfs = 99; -pub const SYS_fstatfs = 100; -pub const SYS_ioperm = 101; -pub const SYS_socketcall = 102; -pub const SYS_syslog = 103; -pub const SYS_setitimer = 104; -pub const SYS_getitimer = 105; -pub const SYS_stat = 106; -pub const SYS_lstat = 107; -pub const SYS_fstat = 108; -pub const SYS_olduname = 109; -pub const SYS_iopl = 110; -pub const SYS_vhangup = 111; -pub const SYS_idle = 112; -pub const SYS_vm86old = 113; -pub const SYS_wait4 = 114; -pub const SYS_swapoff = 115; -pub const SYS_sysinfo = 116; -pub const SYS_ipc = 117; -pub const SYS_fsync = 118; -pub const SYS_sigreturn = 119; -pub const SYS_clone = 120; -pub const SYS_setdomainname = 121; -pub const SYS_uname = 122; -pub const SYS_modify_ldt = 123; -pub const SYS_adjtimex = 124; -pub const SYS_mprotect = 125; -pub const SYS_sigprocmask = 126; -pub const SYS_create_module = 127; -pub const SYS_init_module = 128; -pub const SYS_delete_module = 129; -pub const SYS_get_kernel_syms = 130; -pub const SYS_quotactl = 131; -pub const SYS_getpgid = 132; -pub const SYS_fchdir = 133; -pub const SYS_bdflush = 134; -pub const SYS_sysfs = 135; -pub const SYS_personality = 136; -pub const SYS_afs_syscall = 137; -pub const SYS_setfsuid = 138; -pub const SYS_setfsgid = 139; -pub const SYS__llseek = 140; -pub const SYS_getdents = 141; -pub const SYS__newselect = 142; -pub const SYS_flock = 143; -pub const SYS_msync = 144; -pub const SYS_readv = 145; -pub const SYS_writev = 146; -pub const SYS_getsid = 147; -pub const SYS_fdatasync = 148; -pub const SYS__sysctl = 149; -pub const SYS_mlock = 150; -pub const SYS_munlock = 151; -pub const SYS_mlockall = 152; -pub const SYS_munlockall = 153; -pub const SYS_sched_setparam = 154; -pub const SYS_sched_getparam = 155; -pub const SYS_sched_setscheduler = 156; -pub const SYS_sched_getscheduler = 157; -pub const SYS_sched_yield = 158; -pub const SYS_sched_get_priority_max = 159; -pub const SYS_sched_get_priority_min = 160; -pub const SYS_sched_rr_get_interval = 161; -pub const SYS_nanosleep = 162; -pub const SYS_mremap = 163; -pub const SYS_setresuid = 164; -pub const SYS_getresuid = 165; -pub const SYS_vm86 = 166; -pub const SYS_query_module = 167; -pub const SYS_poll = 168; -pub const SYS_nfsservctl = 169; -pub const SYS_setresgid = 170; -pub const SYS_getresgid = 171; -pub const SYS_prctl = 172; -pub const SYS_rt_sigreturn = 173; -pub const SYS_rt_sigaction = 174; -pub const SYS_rt_sigprocmask = 175; -pub const SYS_rt_sigpending = 176; -pub const SYS_rt_sigtimedwait = 177; -pub const SYS_rt_sigqueueinfo = 178; -pub const SYS_rt_sigsuspend = 179; -pub const SYS_pread64 = 180; -pub const SYS_pwrite64 = 181; -pub const SYS_chown = 182; -pub const SYS_getcwd = 183; -pub const SYS_capget = 184; -pub const SYS_capset = 185; -pub const SYS_sigaltstack = 186; -pub const SYS_sendfile = 187; -pub const SYS_getpmsg = 188; -pub const SYS_putpmsg = 189; -pub const SYS_vfork = 190; -pub const SYS_ugetrlimit = 191; -pub const SYS_mmap2 = 192; -pub const SYS_truncate64 = 193; -pub const SYS_ftruncate64 = 194; -pub const SYS_stat64 = 195; -pub const SYS_lstat64 = 196; -pub const SYS_fstat64 = 197; -pub const SYS_lchown32 = 198; -pub const SYS_getuid32 = 199; -pub const SYS_getgid32 = 200; -pub const SYS_geteuid32 = 201; -pub const SYS_getegid32 = 202; -pub const SYS_setreuid32 = 203; -pub const SYS_setregid32 = 204; -pub const SYS_getgroups32 = 205; -pub const SYS_setgroups32 = 206; -pub const SYS_fchown32 = 207; -pub const SYS_setresuid32 = 208; -pub const SYS_getresuid32 = 209; -pub const SYS_setresgid32 = 210; -pub const SYS_getresgid32 = 211; -pub const SYS_chown32 = 212; -pub const SYS_setuid32 = 213; -pub const SYS_setgid32 = 214; -pub const SYS_setfsuid32 = 215; -pub const SYS_setfsgid32 = 216; -pub const SYS_pivot_root = 217; -pub const SYS_mincore = 218; -pub const SYS_madvise = 219; -pub const SYS_madvise1 = 219; -pub const SYS_getdents64 = 220; -pub const SYS_fcntl64 = 221; -pub const SYS_gettid = 224; -pub const SYS_readahead = 225; -pub const SYS_setxattr = 226; -pub const SYS_lsetxattr = 227; -pub const SYS_fsetxattr = 228; -pub const SYS_getxattr = 229; -pub const SYS_lgetxattr = 230; -pub const SYS_fgetxattr = 231; -pub const SYS_listxattr = 232; -pub const SYS_llistxattr = 233; -pub const SYS_flistxattr = 234; -pub const SYS_removexattr = 235; -pub const SYS_lremovexattr = 236; -pub const SYS_fremovexattr = 237; -pub const SYS_tkill = 238; -pub const SYS_sendfile64 = 239; -pub const SYS_futex = 240; -pub const SYS_sched_setaffinity = 241; -pub const SYS_sched_getaffinity = 242; -pub const SYS_set_thread_area = 243; -pub const SYS_get_thread_area = 244; -pub const SYS_io_setup = 245; -pub const SYS_io_destroy = 246; -pub const SYS_io_getevents = 247; -pub const SYS_io_submit = 248; -pub const SYS_io_cancel = 249; -pub const SYS_fadvise64 = 250; -pub const SYS_exit_group = 252; -pub const SYS_lookup_dcookie = 253; -pub const SYS_epoll_create = 254; -pub const SYS_epoll_ctl = 255; -pub const SYS_epoll_wait = 256; -pub const SYS_remap_file_pages = 257; -pub const SYS_set_tid_address = 258; -pub const SYS_timer_create = 259; -pub const SYS_timer_settime = SYS_timer_create+1; -pub const SYS_timer_gettime = SYS_timer_create+2; -pub const SYS_timer_getoverrun = SYS_timer_create+3; -pub const SYS_timer_delete = SYS_timer_create+4; -pub const SYS_clock_settime = SYS_timer_create+5; -pub const SYS_clock_gettime = SYS_timer_create+6; -pub const SYS_clock_getres = SYS_timer_create+7; -pub const SYS_clock_nanosleep = SYS_timer_create+8; -pub const SYS_statfs64 = 268; -pub const SYS_fstatfs64 = 269; -pub const SYS_tgkill = 270; -pub const SYS_utimes = 271; -pub const SYS_fadvise64_64 = 272; -pub const SYS_vserver = 273; -pub const SYS_mbind = 274; -pub const SYS_get_mempolicy = 275; -pub const SYS_set_mempolicy = 276; -pub const SYS_mq_open = 277; -pub const SYS_mq_unlink = SYS_mq_open+1; -pub const SYS_mq_timedsend = SYS_mq_open+2; -pub const SYS_mq_timedreceive = SYS_mq_open+3; -pub const SYS_mq_notify = SYS_mq_open+4; -pub const SYS_mq_getsetattr = SYS_mq_open+5; -pub const SYS_kexec_load = 283; -pub const SYS_waitid = 284; -pub const SYS_add_key = 286; -pub const SYS_request_key = 287; -pub const SYS_keyctl = 288; -pub const SYS_ioprio_set = 289; -pub const SYS_ioprio_get = 290; -pub const SYS_inotify_init = 291; -pub const SYS_inotify_add_watch = 292; -pub const SYS_inotify_rm_watch = 293; -pub const SYS_migrate_pages = 294; -pub const SYS_openat = 295; -pub const SYS_mkdirat = 296; -pub const SYS_mknodat = 297; -pub const SYS_fchownat = 298; -pub const SYS_futimesat = 299; -pub const SYS_fstatat64 = 300; -pub const SYS_unlinkat = 301; -pub const SYS_renameat = 302; -pub const SYS_linkat = 303; -pub const SYS_symlinkat = 304; -pub const SYS_readlinkat = 305; -pub const SYS_fchmodat = 306; -pub const SYS_faccessat = 307; -pub const SYS_pselect6 = 308; -pub const SYS_ppoll = 309; -pub const SYS_unshare = 310; -pub const SYS_set_robust_list = 311; -pub const SYS_get_robust_list = 312; -pub const SYS_splice = 313; -pub const SYS_sync_file_range = 314; -pub const SYS_tee = 315; -pub const SYS_vmsplice = 316; -pub const SYS_move_pages = 317; -pub const SYS_getcpu = 318; -pub const SYS_epoll_pwait = 319; -pub const SYS_utimensat = 320; -pub const SYS_signalfd = 321; -pub const SYS_timerfd_create = 322; -pub const SYS_eventfd = 323; -pub const SYS_fallocate = 324; -pub const SYS_timerfd_settime = 325; -pub const SYS_timerfd_gettime = 326; -pub const SYS_signalfd4 = 327; -pub const SYS_eventfd2 = 328; -pub const SYS_epoll_create1 = 329; -pub const SYS_dup3 = 330; -pub const SYS_pipe2 = 331; -pub const SYS_inotify_init1 = 332; -pub const SYS_preadv = 333; -pub const SYS_pwritev = 334; -pub const SYS_rt_tgsigqueueinfo = 335; -pub const SYS_perf_event_open = 336; -pub const SYS_recvmmsg = 337; -pub const SYS_fanotify_init = 338; -pub const SYS_fanotify_mark = 339; -pub const SYS_prlimit64 = 340; -pub const SYS_name_to_handle_at = 341; -pub const SYS_open_by_handle_at = 342; -pub const SYS_clock_adjtime = 343; -pub const SYS_syncfs = 344; -pub const SYS_sendmmsg = 345; -pub const SYS_setns = 346; -pub const SYS_process_vm_readv = 347; -pub const SYS_process_vm_writev = 348; -pub const SYS_kcmp = 349; -pub const SYS_finit_module = 350; -pub const SYS_sched_setattr = 351; -pub const SYS_sched_getattr = 352; -pub const SYS_renameat2 = 353; -pub const SYS_seccomp = 354; -pub const SYS_getrandom = 355; -pub const SYS_memfd_create = 356; -pub const SYS_bpf = 357; -pub const SYS_execveat = 358; -pub const SYS_socket = 359; -pub const SYS_socketpair = 360; -pub const SYS_bind = 361; -pub const SYS_connect = 362; -pub const SYS_listen = 363; -pub const SYS_accept4 = 364; -pub const SYS_getsockopt = 365; -pub const SYS_setsockopt = 366; -pub const SYS_getsockname = 367; -pub const SYS_getpeername = 368; -pub const SYS_sendto = 369; -pub const SYS_sendmsg = 370; -pub const SYS_recvfrom = 371; -pub const SYS_recvmsg = 372; -pub const SYS_shutdown = 373; -pub const SYS_userfaultfd = 374; -pub const SYS_membarrier = 375; -pub const SYS_mlock2 = 376; - - -pub const O_CREAT = 0o100; -pub const O_EXCL = 0o200; -pub const O_NOCTTY = 0o400; -pub const O_TRUNC = 0o1000; -pub const O_APPEND = 0o2000; -pub const O_NONBLOCK = 0o4000; -pub const O_DSYNC = 0o10000; -pub const O_SYNC = 0o4010000; -pub const O_RSYNC = 0o4010000; -pub const O_DIRECTORY = 0o200000; -pub const O_NOFOLLOW = 0o400000; -pub const O_CLOEXEC = 0o2000000; - -pub const O_ASYNC = 0o20000; -pub const O_DIRECT = 0o40000; -pub const O_LARGEFILE = 0o100000; -pub const O_NOATIME = 0o1000000; -pub const O_PATH = 0o10000000; -pub const O_TMPFILE = 0o20200000; -pub const O_NDELAY = O_NONBLOCK; - -pub const F_DUPFD = 0; -pub const F_GETFD = 1; -pub const F_SETFD = 2; -pub const F_GETFL = 3; -pub const F_SETFL = 4; - -pub const F_SETOWN = 8; -pub const F_GETOWN = 9; -pub const F_SETSIG = 10; -pub const F_GETSIG = 11; - -pub const F_GETLK = 12; -pub const F_SETLK = 13; -pub const F_SETLKW = 14; - -pub const F_SETOWN_EX = 15; -pub const F_GETOWN_EX = 16; - -pub const F_GETOWNER_UIDS = 17; - -pub inline fn syscall0(number: usize) -> usize { - asm volatile ("int 80h" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number)) -} - -pub inline fn syscall1(number: usize, arg1: usize) -> usize { - asm volatile ("int 80h" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1)) -} - -pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) -> usize { - asm volatile ("int 80h" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1), - [arg2] "{ecx}" (arg2)) -} - -pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) -> usize { - asm volatile ("int 80h" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1), - [arg2] "{ecx}" (arg2), - [arg3] "{edx}" (arg3)) -} - -pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) -> usize { - asm volatile ("int 80h" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1), - [arg2] "{ecx}" (arg2), - [arg3] "{edx}" (arg3), - [arg4] "{esi}" (arg4)) -} - -pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, - arg4: usize, arg5: usize) -> usize -{ - asm volatile ("int 80h" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1), - [arg2] "{ecx}" (arg2), - [arg3] "{edx}" (arg3), - [arg4] "{esi}" (arg4), - [arg5] "{edi}" (arg5)) -} - -pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, - arg4: usize, arg5: usize, arg6: usize) -> usize -{ - asm volatile ("int 80h" - : [ret] "={eax}" (-> usize) - : [number] "{eax}" (number), - [arg1] "{ebx}" (arg1), - [arg2] "{ecx}" (arg2), - [arg3] "{edx}" (arg3), - [arg4] "{esi}" (arg4), - [arg5] "{edi}" (arg5), - [arg6] "{ebp}" (arg6)) -} - -export struct msghdr { - msg_name: &u8, - msg_namelen: socklen_t, - msg_iov: &iovec, - msg_iovlen: i32, - msg_control: &u8, - msg_controllen: socklen_t, - msg_flags: i32, -} diff --git a/std/linux_x86_64.zig b/std/linux_x86_64.zig deleted file mode 100644 index 0f8f163ce2046398cc89be6f12bac14b2be893c4..0000000000000000000000000000000000000000 --- a/std/linux_x86_64.zig +++ /dev/null @@ -1,480 +0,0 @@ -const linux = @import("linux.zig"); -const socklen_t = linux.socklen_t; -const iovec = linux.iovec; - -pub const SYS_read = 0; -pub const SYS_write = 1; -pub const SYS_open = 2; -pub const SYS_close = 3; -pub const SYS_stat = 4; -pub const SYS_fstat = 5; -pub const SYS_lstat = 6; -pub const SYS_poll = 7; -pub const SYS_lseek = 8; -pub const SYS_mmap = 9; -pub const SYS_mprotect = 10; -pub const SYS_munmap = 11; -pub const SYS_brk = 12; -pub const SYS_rt_sigaction = 13; -pub const SYS_rt_sigprocmask = 14; -pub const SYS_rt_sigreturn = 15; -pub const SYS_ioctl = 16; -pub const SYS_pread = 17; -pub const SYS_pwrite = 18; -pub const SYS_readv = 19; -pub const SYS_writev = 20; -pub const SYS_access = 21; -pub const SYS_pipe = 22; -pub const SYS_select = 23; -pub const SYS_sched_yield = 24; -pub const SYS_mremap = 25; -pub const SYS_msync = 26; -pub const SYS_mincore = 27; -pub const SYS_madvise = 28; -pub const SYS_shmget = 29; -pub const SYS_shmat = 30; -pub const SYS_shmctl = 31; -pub const SYS_dup = 32; -pub const SYS_dup2 = 33; -pub const SYS_pause = 34; -pub const SYS_nanosleep = 35; -pub const SYS_getitimer = 36; -pub const SYS_alarm = 37; -pub const SYS_setitimer = 38; -pub const SYS_getpid = 39; -pub const SYS_sendfile = 40; -pub const SYS_socket = 41; -pub const SYS_connect = 42; -pub const SYS_accept = 43; -pub const SYS_sendto = 44; -pub const SYS_recvfrom = 45; -pub const SYS_sendmsg = 46; -pub const SYS_recvmsg = 47; -pub const SYS_shutdown = 48; -pub const SYS_bind = 49; -pub const SYS_listen = 50; -pub const SYS_getsockname = 51; -pub const SYS_getpeername = 52; -pub const SYS_socketpair = 53; -pub const SYS_setsockopt = 54; -pub const SYS_getsockopt = 55; -pub const SYS_clone = 56; -pub const SYS_fork = 57; -pub const SYS_vfork = 58; -pub const SYS_execve = 59; -pub const SYS_exit = 60; -pub const SYS_wait4 = 61; -pub const SYS_kill = 62; -pub const SYS_uname = 63; -pub const SYS_semget = 64; -pub const SYS_semop = 65; -pub const SYS_semctl = 66; -pub const SYS_shmdt = 67; -pub const SYS_msgget = 68; -pub const SYS_msgsnd = 69; -pub const SYS_msgrcv = 70; -pub const SYS_msgctl = 71; -pub const SYS_fcntl = 72; -pub const SYS_flock = 73; -pub const SYS_fsync = 74; -pub const SYS_fdatasync = 75; -pub const SYS_truncate = 76; -pub const SYS_ftruncate = 77; -pub const SYS_getdents = 78; -pub const SYS_getcwd = 79; -pub const SYS_chdir = 80; -pub const SYS_fchdir = 81; -pub const SYS_rename = 82; -pub const SYS_mkdir = 83; -pub const SYS_rmdir = 84; -pub const SYS_creat = 85; -pub const SYS_link = 86; -pub const SYS_unlink = 87; -pub const SYS_symlink = 88; -pub const SYS_readlink = 89; -pub const SYS_chmod = 90; -pub const SYS_fchmod = 91; -pub const SYS_chown = 92; -pub const SYS_fchown = 93; -pub const SYS_lchown = 94; -pub const SYS_umask = 95; -pub const SYS_gettimeofday = 96; -pub const SYS_getrlimit = 97; -pub const SYS_getrusage = 98; -pub const SYS_sysinfo = 99; -pub const SYS_times = 100; -pub const SYS_ptrace = 101; -pub const SYS_getuid = 102; -pub const SYS_syslog = 103; -pub const SYS_getgid = 104; -pub const SYS_setuid = 105; -pub const SYS_setgid = 106; -pub const SYS_geteuid = 107; -pub const SYS_getegid = 108; -pub const SYS_setpgid = 109; -pub const SYS_getppid = 110; -pub const SYS_getpgrp = 111; -pub const SYS_setsid = 112; -pub const SYS_setreuid = 113; -pub const SYS_setregid = 114; -pub const SYS_getgroups = 115; -pub const SYS_setgroups = 116; -pub const SYS_setresuid = 117; -pub const SYS_getresuid = 118; -pub const SYS_setresgid = 119; -pub const SYS_getresgid = 120; -pub const SYS_getpgid = 121; -pub const SYS_setfsuid = 122; -pub const SYS_setfsgid = 123; -pub const SYS_getsid = 124; -pub const SYS_capget = 125; -pub const SYS_capset = 126; -pub const SYS_rt_sigpending = 127; -pub const SYS_rt_sigtimedwait = 128; -pub const SYS_rt_sigqueueinfo = 129; -pub const SYS_rt_sigsuspend = 130; -pub const SYS_sigaltstack = 131; -pub const SYS_utime = 132; -pub const SYS_mknod = 133; -pub const SYS_uselib = 134; -pub const SYS_personality = 135; -pub const SYS_ustat = 136; -pub const SYS_statfs = 137; -pub const SYS_fstatfs = 138; -pub const SYS_sysfs = 139; -pub const SYS_getpriority = 140; -pub const SYS_setpriority = 141; -pub const SYS_sched_setparam = 142; -pub const SYS_sched_getparam = 143; -pub const SYS_sched_setscheduler = 144; -pub const SYS_sched_getscheduler = 145; -pub const SYS_sched_get_priority_max = 146; -pub const SYS_sched_get_priority_min = 147; -pub const SYS_sched_rr_get_interval = 148; -pub const SYS_mlock = 149; -pub const SYS_munlock = 150; -pub const SYS_mlockall = 151; -pub const SYS_munlockall = 152; -pub const SYS_vhangup = 153; -pub const SYS_modify_ldt = 154; -pub const SYS_pivot_root = 155; -pub const SYS__sysctl = 156; -pub const SYS_prctl = 157; -pub const SYS_arch_prctl = 158; -pub const SYS_adjtimex = 159; -pub const SYS_setrlimit = 160; -pub const SYS_chroot = 161; -pub const SYS_sync = 162; -pub const SYS_acct = 163; -pub const SYS_settimeofday = 164; -pub const SYS_mount = 165; -pub const SYS_umount2 = 166; -pub const SYS_swapon = 167; -pub const SYS_swapoff = 168; -pub const SYS_reboot = 169; -pub const SYS_sethostname = 170; -pub const SYS_setdomainname = 171; -pub const SYS_iopl = 172; -pub const SYS_ioperm = 173; -pub const SYS_create_module = 174; -pub const SYS_init_module = 175; -pub const SYS_delete_module = 176; -pub const SYS_get_kernel_syms = 177; -pub const SYS_query_module = 178; -pub const SYS_quotactl = 179; -pub const SYS_nfsservctl = 180; -pub const SYS_getpmsg = 181; -pub const SYS_putpmsg = 182; -pub const SYS_afs_syscall = 183; -pub const SYS_tuxcall = 184; -pub const SYS_security = 185; -pub const SYS_gettid = 186; -pub const SYS_readahead = 187; -pub const SYS_setxattr = 188; -pub const SYS_lsetxattr = 189; -pub const SYS_fsetxattr = 190; -pub const SYS_getxattr = 191; -pub const SYS_lgetxattr = 192; -pub const SYS_fgetxattr = 193; -pub const SYS_listxattr = 194; -pub const SYS_llistxattr = 195; -pub const SYS_flistxattr = 196; -pub const SYS_removexattr = 197; -pub const SYS_lremovexattr = 198; -pub const SYS_fremovexattr = 199; -pub const SYS_tkill = 200; -pub const SYS_time = 201; -pub const SYS_futex = 202; -pub const SYS_sched_setaffinity = 203; -pub const SYS_sched_getaffinity = 204; -pub const SYS_set_thread_area = 205; -pub const SYS_io_setup = 206; -pub const SYS_io_destroy = 207; -pub const SYS_io_getevents = 208; -pub const SYS_io_submit = 209; -pub const SYS_io_cancel = 210; -pub const SYS_get_thread_area = 211; -pub const SYS_lookup_dcookie = 212; -pub const SYS_epoll_create = 213; -pub const SYS_epoll_ctl_old = 214; -pub const SYS_epoll_wait_old = 215; -pub const SYS_remap_file_pages = 216; -pub const SYS_getdents64 = 217; -pub const SYS_set_tid_address = 218; -pub const SYS_restart_syscall = 219; -pub const SYS_semtimedop = 220; -pub const SYS_fadvise64 = 221; -pub const SYS_timer_create = 222; -pub const SYS_timer_settime = 223; -pub const SYS_timer_gettime = 224; -pub const SYS_timer_getoverrun = 225; -pub const SYS_timer_delete = 226; -pub const SYS_clock_settime = 227; -pub const SYS_clock_gettime = 228; -pub const SYS_clock_getres = 229; -pub const SYS_clock_nanosleep = 230; -pub const SYS_exit_group = 231; -pub const SYS_epoll_wait = 232; -pub const SYS_epoll_ctl = 233; -pub const SYS_tgkill = 234; -pub const SYS_utimes = 235; -pub const SYS_vserver = 236; -pub const SYS_mbind = 237; -pub const SYS_set_mempolicy = 238; -pub const SYS_get_mempolicy = 239; -pub const SYS_mq_open = 240; -pub const SYS_mq_unlink = 241; -pub const SYS_mq_timedsend = 242; -pub const SYS_mq_timedreceive = 243; -pub const SYS_mq_notify = 244; -pub const SYS_mq_getsetattr = 245; -pub const SYS_kexec_load = 246; -pub const SYS_waitid = 247; -pub const SYS_add_key = 248; -pub const SYS_request_key = 249; -pub const SYS_keyctl = 250; -pub const SYS_ioprio_set = 251; -pub const SYS_ioprio_get = 252; -pub const SYS_inotify_init = 253; -pub const SYS_inotify_add_watch = 254; -pub const SYS_inotify_rm_watch = 255; -pub const SYS_migrate_pages = 256; -pub const SYS_openat = 257; -pub const SYS_mkdirat = 258; -pub const SYS_mknodat = 259; -pub const SYS_fchownat = 260; -pub const SYS_futimesat = 261; -pub const SYS_newfstatat = 262; -pub const SYS_unlinkat = 263; -pub const SYS_renameat = 264; -pub const SYS_linkat = 265; -pub const SYS_symlinkat = 266; -pub const SYS_readlinkat = 267; -pub const SYS_fchmodat = 268; -pub const SYS_faccessat = 269; -pub const SYS_pselect6 = 270; -pub const SYS_ppoll = 271; -pub const SYS_unshare = 272; -pub const SYS_set_robust_list = 273; -pub const SYS_get_robust_list = 274; -pub const SYS_splice = 275; -pub const SYS_tee = 276; -pub const SYS_sync_file_range = 277; -pub const SYS_vmsplice = 278; -pub const SYS_move_pages = 279; -pub const SYS_utimensat = 280; -pub const SYS_epoll_pwait = 281; -pub const SYS_signalfd = 282; -pub const SYS_timerfd_create = 283; -pub const SYS_eventfd = 284; -pub const SYS_fallocate = 285; -pub const SYS_timerfd_settime = 286; -pub const SYS_timerfd_gettime = 287; -pub const SYS_accept4 = 288; -pub const SYS_signalfd4 = 289; -pub const SYS_eventfd2 = 290; -pub const SYS_epoll_create1 = 291; -pub const SYS_dup3 = 292; -pub const SYS_pipe2 = 293; -pub const SYS_inotify_init1 = 294; -pub const SYS_preadv = 295; -pub const SYS_pwritev = 296; -pub const SYS_rt_tgsigqueueinfo = 297; -pub const SYS_perf_event_open = 298; -pub const SYS_recvmmsg = 299; -pub const SYS_fanotify_init = 300; -pub const SYS_fanotify_mark = 301; -pub const SYS_prlimit64 = 302; -pub const SYS_name_to_handle_at = 303; -pub const SYS_open_by_handle_at = 304; -pub const SYS_clock_adjtime = 305; -pub const SYS_syncfs = 306; -pub const SYS_sendmmsg = 307; -pub const SYS_setns = 308; -pub const SYS_getcpu = 309; -pub const SYS_process_vm_readv = 310; -pub const SYS_process_vm_writev = 311; -pub const SYS_kcmp = 312; -pub const SYS_finit_module = 313; -pub const SYS_sched_setattr = 314; -pub const SYS_sched_getattr = 315; -pub const SYS_renameat2 = 316; -pub const SYS_seccomp = 317; -pub const SYS_getrandom = 318; -pub const SYS_memfd_create = 319; -pub const SYS_kexec_file_load = 320; -pub const SYS_bpf = 321; -pub const SYS_execveat = 322; -pub const SYS_userfaultfd = 323; -pub const SYS_membarrier = 324; -pub const SYS_mlock2 = 325; - -pub const O_CREAT = 0o100; -pub const O_EXCL = 0o200; -pub const O_NOCTTY = 0o400; -pub const O_TRUNC = 0o1000; -pub const O_APPEND = 0o2000; -pub const O_NONBLOCK = 0o4000; -pub const O_DSYNC = 0o10000; -pub const O_SYNC = 0o4010000; -pub const O_RSYNC = 0o4010000; -pub const O_DIRECTORY = 0o200000; -pub const O_NOFOLLOW = 0o400000; -pub const O_CLOEXEC = 0o2000000; - -pub const O_ASYNC = 0o20000; -pub const O_DIRECT = 0o40000; -pub const O_LARGEFILE = 0; -pub const O_NOATIME = 0o1000000; -pub const O_PATH = 0o10000000; -pub const O_TMPFILE = 0o20200000; -pub const O_NDELAY = O_NONBLOCK; - -pub const F_DUPFD = 0; -pub const F_GETFD = 1; -pub const F_SETFD = 2; -pub const F_GETFL = 3; -pub const F_SETFL = 4; - -pub const F_SETOWN = 8; -pub const F_GETOWN = 9; -pub const F_SETSIG = 10; -pub const F_GETSIG = 11; - -pub const F_GETLK = 5; -pub const F_SETLK = 6; -pub const F_SETLKW = 7; - -pub const F_SETOWN_EX = 15; -pub const F_GETOWN_EX = 16; - -pub const F_GETOWNER_UIDS = 17; - -pub inline fn syscall0(number: usize) -> usize { - asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number) - : "rcx", "r11") -} - -pub inline fn syscall1(number: usize, arg1: usize) -> usize { - asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1) - : "rcx", "r11") -} - -pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) -> usize { - asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1), - [arg2] "{rsi}" (arg2) - : "rcx", "r11") -} - -pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) -> usize { - asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1), - [arg2] "{rsi}" (arg2), - [arg3] "{rdx}" (arg3) - : "rcx", "r11") -} - -pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) -> usize { - asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1), - [arg2] "{rsi}" (arg2), - [arg3] "{rdx}" (arg3), - [arg4] "{r10}" (arg4) - : "rcx", "r11") -} - -pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) -> usize { - asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1), - [arg2] "{rsi}" (arg2), - [arg3] "{rdx}" (arg3), - [arg4] "{r10}" (arg4), - [arg5] "{r8}" (arg5) - : "rcx", "r11") -} - -pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, - arg5: usize, arg6: usize) -> usize -{ - asm volatile ("syscall" - : [ret] "={rax}" (-> usize) - : [number] "{rax}" (number), - [arg1] "{rdi}" (arg1), - [arg2] "{rsi}" (arg2), - [arg3] "{rdx}" (arg3), - [arg4] "{r10}" (arg4), - [arg5] "{r8}" (arg5), - [arg6] "{r9}" (arg6) - : "rcx", "r11") -} - -pub const msghdr = extern struct { - msg_name: &u8, - msg_namelen: socklen_t, - msg_iov: &iovec, - msg_iovlen: i32, - __pad1: i32, - msg_control: &u8, - msg_controllen: socklen_t, - __pad2: socklen_t, - msg_flags: i32, -}; - -pub const stat = extern struct { - dev: u64, - ino: u64, - nlink: usize, - - mode: u32, - uid: u32, - gid: u32, - __pad0: u32, - rdev: u64, - size: i64, - blksize: isize, - blocks: i64, - - atim: timespec, - mtim: timespec, - ctim: timespec, - __unused: [3]isize, -}; - -pub const timespec = extern struct { - tv_sec: isize, - tv_nsec: isize, -}; diff --git a/std/mem.zig b/std/mem.zig index c67cf11db62a7c1c24405ca0a6f3851d5d110cf2..f374e40cf4fa97fb52ad0372c35e1542ceba26ae 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -1,6 +1,6 @@ const assert = @import("debug.zig").assert; const math = @import("math.zig"); -const os = @import("os.zig"); +const os = @import("os/index.zig"); const io = @import("io.zig"); pub const Cmp = math.Cmp; diff --git a/std/net.zig b/std/net.zig index e1c8bf5d3f4876f1f97311fdfe247bd61bca2fe3..280484c80cca331fbe2ddb940340667e2bb6f565 100644 --- a/std/net.zig +++ b/std/net.zig @@ -1,5 +1,5 @@ -const linux = @import("linux.zig"); -const errno = @import("errno.zig"); +const linux = @import("os/linux.zig"); +const errno = @import("os/errno.zig"); const assert = @import("debug.zig").assert; const endian = @import("endian.zig"); diff --git a/std/os.zig b/std/os.zig deleted file mode 100644 index 68a52f5ff130d888023b0f2df5b3fafc5dd6d17c..0000000000000000000000000000000000000000 --- a/std/os.zig +++ /dev/null @@ -1,74 +0,0 @@ -const posix = switch(@compileVar("os")) { - Os.linux => @import("linux.zig"), - Os.darwin, Os.macosx, Os.ios => @import("darwin.zig"), - else => @compileError("Unsupported OS"), -}; -const windows = @import("windows.zig"); -const errno = @import("errno.zig"); -const linking_libc = @import("build.zig").linking_libc; -const c = @import("c/index.zig"); - -error Unexpected; - -/// Fills `buf` with random bytes. If linking against libc, this calls the -/// appropriate OS-specific library call. Otherwise it uses the zig standard -/// library implementation. -pub fn getRandomBytes(buf: []u8) -> %void { - while (true) { - const err = switch (@compileVar("os")) { - Os.linux => { - if (linking_libc) { - if (c.getrandom(buf.ptr, buf.len, 0) == -1) *c._errno() else 0 - } else { - posix.getErrno(posix.getrandom(buf.ptr, buf.len, 0)) - } - }, - Os.darwin, Os.macosx, Os.ios => { - if (linking_libc) { - if (posix.getrandom(buf.ptr, buf.len) == -1) *c._errno() else 0 - } else { - posix.getErrno(posix.getrandom(buf.ptr, buf.len)) - } - }, - Os.windows => { - var hCryptProv: windows.HCRYPTPROV = undefined; - if (!windows.CryptAcquireContext(&hCryptProv, null, null, windows.PROV_RSA_FULL, 0)) { - return error.Unexpected; - } - defer _ = windows.CryptReleaseContext(hCryptProv, 0); - - if (!windows.CryptGenRandom(hCryptProv, windows.DWORD(buf.len), buf.ptr)) { - return error.Unexpected; - } - return; - }, - else => @compileError("Unsupported OS"), - }; - if (err > 0) { - return switch (err) { - errno.EINVAL => unreachable, - errno.EFAULT => unreachable, - errno.EINTR => continue, - else => error.Unexpected, - } - } - return; - } -} - -/// Raises a signal in the current kernel thread, ending its execution. -/// If linking against libc, this calls the abort() libc function. Otherwise -/// it uses the zig standard library implementation. -pub coldcc fn abort() -> noreturn { - if (linking_libc) { - c.abort(); - } - switch (@compileVar("os")) { - Os.linux => { - _ = posix.raise(posix.SIGABRT); - _ = posix.raise(posix.SIGKILL); - while (true) {} - }, - else => @compileError("Unsupported OS"), - } -} diff --git a/std/os/darwin.zig b/std/os/darwin.zig new file mode 100644 index 0000000000000000000000000000000000000000..647fee40e2ff6941ba3f5b4acde16d79183d55f3 --- /dev/null +++ b/std/os/darwin.zig @@ -0,0 +1,118 @@ + +const arch = switch (@compileVar("arch")) { + Arch.x86_64 => @import("darwin_x86_64.zig"), + else => @compileError("unsupported arch"), +}; + +const errno = @import("errno.zig"); + +pub const O_LARGEFILE = 0x0000; +pub const O_RDONLY = 0x0000; + +pub const SEEK_SET = 0x0; +pub const SEEK_CUR = 0x1; +pub const SEEK_END = 0x2; + +pub const SIGHUP = 1; +pub const SIGINT = 2; +pub const SIGQUIT = 3; +pub const SIGILL = 4; +pub const SIGTRAP = 5; +pub const SIGABRT = 6; +pub const SIGIOT = SIGABRT; +pub const SIGBUS = 7; +pub const SIGFPE = 8; +pub const SIGKILL = 9; +pub const SIGUSR1 = 10; +pub const SIGSEGV = 11; +pub const SIGUSR2 = 12; +pub const SIGPIPE = 13; +pub const SIGALRM = 14; +pub const SIGTERM = 15; +pub const SIGSTKFLT = 16; +pub const SIGCHLD = 17; +pub const SIGCONT = 18; +pub const SIGSTOP = 19; +pub const SIGTSTP = 20; +pub const SIGTTIN = 21; +pub const SIGTTOU = 22; +pub const SIGURG = 23; +pub const SIGXCPU = 24; +pub const SIGXFSZ = 25; +pub const SIGVTALRM = 26; +pub const SIGPROF = 27; +pub const SIGWINCH = 28; +pub const SIGIO = 29; +pub const SIGPOLL = 29; +pub const SIGPWR = 30; +pub const SIGSYS = 31; +pub const SIGUNUSED = SIGSYS; + +pub fn exit(status: usize) -> noreturn { + _ = arch.syscall1(arch.SYS_exit, status); + unreachable +} + +/// Get the errno from a syscall return value, or 0 for no error. +pub fn getErrno(r: usize) -> usize { + const signed_r = *(&isize)(&r); + if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0 +} + +pub fn write(fd: i32, buf: &const u8, count: usize) -> usize { + arch.syscall3(arch.SYS_write, usize(fd), usize(buf), count) +} + +pub fn close(fd: i32) -> usize { + arch.syscall1(arch.SYS_close, usize(fd)) +} + +pub fn open_c(path: &const u8, flags: usize, perm: usize) -> usize { + arch.syscall3(arch.SYS_open, usize(path), flags, perm) +} + +pub fn open(path: []const u8, flags: usize, perm: usize) -> usize { + const buf = @alloca(u8, path.len + 1); + @memcpy(&buf[0], &path[0], path.len); + buf[path.len] = 0; + return open_c(buf.ptr, flags, perm); +} + +pub fn read(fd: i32, buf: &u8, count: usize) -> usize { + arch.syscall3(arch.SYS_read, usize(fd), usize(buf), count) +} + +pub fn lseek(fd: i32, offset: usize, ref_pos: usize) -> usize { + arch.syscall3(arch.SYS_lseek, usize(fd), offset, ref_pos) +} + +pub const stat = arch.stat; +pub const timespec = arch.timespec; + +pub fn fstat(fd: i32, stat_buf: &stat) -> usize { + arch.syscall2(arch.SYS_fstat, usize(fd), usize(stat_buf)) +} + +error Unexpected; + +pub fn getrandom(buf: &u8, count: usize) -> usize { + const rr = open_c(c"/dev/urandom", O_LARGEFILE | O_RDONLY, 0); + + if(getErrno(rr) > 0) return rr; + + var fd: i32 = i32(rr); + const readRes = read(fd, buf, count); + readRes +} + +pub fn raise(sig: i32) -> i32 { + // TODO investigate whether we need to block signals before calling kill + // like we do in the linux version of raise + + //var set: sigset_t = undefined; + //blockAppSignals(&set); + const pid = i32(arch.syscall0(arch.SYS_getpid)); + const ret = i32(arch.syscall2(arch.SYS_kill, usize(pid), usize(sig))); + //restoreSignals(&set); + return ret; +} diff --git a/std/os/darwin_x86_64.zig b/std/os/darwin_x86_64.zig new file mode 100644 index 0000000000000000000000000000000000000000..ea6cc5a37a407dcbc11358bb6dac07790988f8fd --- /dev/null +++ b/std/os/darwin_x86_64.zig @@ -0,0 +1,87 @@ + +pub const SYSCALL_CLASS_SHIFT = 24; +pub const SYSCALL_CLASS_MASK = 0xFF << SYSCALL_CLASS_SHIFT; +// pub const SYSCALL_NUMBER_MASK = ~SYSCALL_CLASS_MASK; // ~ modifier not supported yet + +pub const SYSCALL_CLASS_NONE = 0; // Invalid +pub const SYSCALL_CLASS_MACH = 1; // Mach +pub const SYSCALL_CLASS_UNIX = 2; // Unix/BSD +pub const SYSCALL_CLASS_MDEP = 3; // Machine-dependent +pub const SYSCALL_CLASS_DIAG = 4; // Diagnostics + +// TODO: use the above constants to create the below values + +pub const SYS_exit = 0x2000001; +pub const SYS_read = 0x2000003; +pub const SYS_write = 0x2000004; +pub const SYS_open = 0x2000005; +pub const SYS_close = 0x2000006; +pub const SYS_kill = 0x2000025; +pub const SYS_getpid = 0x2000030; +pub const SYS_fstat = 0x20000BD; +pub const SYS_lseek = 0x20000C7; + +pub inline fn syscall0(number: usize) -> usize { + asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number) + : "rcx", "r11") +} + +pub inline fn syscall1(number: usize, arg1: usize) -> usize { + asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1) + : "rcx", "r11") +} + +pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) -> usize { + asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1), + [arg2] "{rsi}" (arg2) + : "rcx", "r11") +} + +pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) -> usize { + asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1), + [arg2] "{rsi}" (arg2), + [arg3] "{rdx}" (arg3) + : "rcx", "r11") +} + + + + +pub const stat = extern struct { + dev: u32, + mode: u16, + nlink: u16, + ino: u64, + uid: u32, + gid: u32, + rdev: u64, + + atim: timespec, + mtim: timespec, + ctim: timespec, + + size: u64, + blocks: u64, + blksize: u32, + flags: u32, + gen: u32, + lspare: i32, + qspare: [2]u64, + +}; + +pub const timespec = extern struct { + tv_sec: isize, + tv_nsec: isize, +}; diff --git a/std/os/errno.zig b/std/os/errno.zig new file mode 100644 index 0000000000000000000000000000000000000000..6f50b502258e8c009d5b2655fce53d078503966d --- /dev/null +++ b/std/os/errno.zig @@ -0,0 +1,146 @@ +pub const EPERM = 1; // Operation not permitted +pub const ENOENT = 2; // No such file or directory +pub const ESRCH = 3; // No such process +pub const EINTR = 4; // Interrupted system call +pub const EIO = 5; // I/O error +pub const ENXIO = 6; // No such device or address +pub const E2BIG = 7; // Arg list too long +pub const ENOEXEC = 8; // Exec format error +pub const EBADF = 9; // Bad file number +pub const ECHILD = 10; // No child processes +pub const EAGAIN = 11; // Try again +pub const ENOMEM = 12; // Out of memory +pub const EACCES = 13; // Permission denied +pub const EFAULT = 14; // Bad address +pub const ENOTBLK = 15; // Block device required +pub const EBUSY = 16; // Device or resource busy +pub const EEXIST = 17; // File exists +pub const EXDEV = 18; // Cross-device link +pub const ENODEV = 19; // No such device +pub const ENOTDIR = 20; // Not a directory +pub const EISDIR = 21; // Is a directory +pub const EINVAL = 22; // Invalid argument +pub const ENFILE = 23; // File table overflow +pub const EMFILE = 24; // Too many open files +pub const ENOTTY = 25; // Not a typewriter +pub const ETXTBSY = 26; // Text file busy +pub const EFBIG = 27; // File too large +pub const ENOSPC = 28; // No space left on device +pub const ESPIPE = 29; // Illegal seek +pub const EROFS = 30; // Read-only file system +pub const EMLINK = 31; // Too many links +pub const EPIPE = 32; // Broken pipe +pub const EDOM = 33; // Math argument out of domain of func +pub const ERANGE = 34; // Math result not representable +pub const EDEADLK = 35; // Resource deadlock would occur +pub const ENAMETOOLONG = 36; // File name too long +pub const ENOLCK = 37; // No record locks available +pub const ENOSYS = 38; // Function not implemented +pub const ENOTEMPTY = 39; // Directory not empty +pub const ELOOP = 40; // Too many symbolic links encountered +pub const EWOULDBLOCK = EAGAIN; // Operation would block +pub const ENOMSG = 42; // No message of desired type +pub const EIDRM = 43; // Identifier removed +pub const ECHRNG = 44; // Channel number out of range +pub const EL2NSYNC = 45; // Level 2 not synchronized +pub const EL3HLT = 46; // Level 3 halted +pub const EL3RST = 47; // Level 3 reset +pub const ELNRNG = 48; // Link number out of range +pub const EUNATCH = 49; // Protocol driver not attached +pub const ENOCSI = 50; // No CSI structure available +pub const EL2HLT = 51; // Level 2 halted +pub const EBADE = 52; // Invalid exchange +pub const EBADR = 53; // Invalid request descriptor +pub const EXFULL = 54; // Exchange full +pub const ENOANO = 55; // No anode +pub const EBADRQC = 56; // Invalid request code +pub const EBADSLT = 57; // Invalid slot + +pub const EBFONT = 59; // Bad font file format +pub const ENOSTR = 60; // Device not a stream +pub const ENODATA = 61; // No data available +pub const ETIME = 62; // Timer expired +pub const ENOSR = 63; // Out of streams resources +pub const ENONET = 64; // Machine is not on the network +pub const ENOPKG = 65; // Package not installed +pub const EREMOTE = 66; // Object is remote +pub const ENOLINK = 67; // Link has been severed +pub const EADV = 68; // Advertise error +pub const ESRMNT = 69; // Srmount error +pub const ECOMM = 70; // Communication error on send +pub const EPROTO = 71; // Protocol error +pub const EMULTIHOP = 72; // Multihop attempted +pub const EDOTDOT = 73; // RFS specific error +pub const EBADMSG = 74; // Not a data message +pub const EOVERFLOW = 75; // Value too large for defined data type +pub const ENOTUNIQ = 76; // Name not unique on network +pub const EBADFD = 77; // File descriptor in bad state +pub const EREMCHG = 78; // Remote address changed +pub const ELIBACC = 79; // Can not access a needed shared library +pub const ELIBBAD = 80; // Accessing a corrupted shared library +pub const ELIBSCN = 81; // .lib section in a.out corrupted +pub const ELIBMAX = 82; // Attempting to link in too many shared libraries +pub const ELIBEXEC = 83; // Cannot exec a shared library directly +pub const EILSEQ = 84; // Illegal byte sequence +pub const ERESTART = 85; // Interrupted system call should be restarted +pub const ESTRPIPE = 86; // Streams pipe error +pub const EUSERS = 87; // Too many users +pub const ENOTSOCK = 88; // Socket operation on non-socket +pub const EDESTADDRREQ = 89; // Destination address required +pub const EMSGSIZE = 90; // Message too long +pub const EPROTOTYPE = 91; // Protocol wrong type for socket +pub const ENOPROTOOPT = 92; // Protocol not available +pub const EPROTONOSUPPORT = 93; // Protocol not supported +pub const ESOCKTNOSUPPORT = 94; // Socket type not supported +pub const EOPNOTSUPP = 95; // Operation not supported on transport endpoint +pub const EPFNOSUPPORT = 96; // Protocol family not supported +pub const EAFNOSUPPORT = 97; // Address family not supported by protocol +pub const EADDRINUSE = 98; // Address already in use +pub const EADDRNOTAVAIL = 99; // Cannot assign requested address +pub const ENETDOWN = 100; // Network is down +pub const ENETUNREACH = 101; // Network is unreachable +pub const ENETRESET = 102; // Network dropped connection because of reset +pub const ECONNABORTED = 103; // Software caused connection abort +pub const ECONNRESET = 104; // Connection reset by peer +pub const ENOBUFS = 105; // No buffer space available +pub const EISCONN = 106; // Transport endpoint is already connected +pub const ENOTCONN = 107; // Transport endpoint is not connected +pub const ESHUTDOWN = 108; // Cannot send after transport endpoint shutdown +pub const ETOOMANYREFS = 109; // Too many references: cannot splice +pub const ETIMEDOUT = 110; // Connection timed out +pub const ECONNREFUSED = 111; // Connection refused +pub const EHOSTDOWN = 112; // Host is down +pub const EHOSTUNREACH = 113; // No route to host +pub const EALREADY = 114; // Operation already in progress +pub const EINPROGRESS = 115; // Operation now in progress +pub const ESTALE = 116; // Stale NFS file handle +pub const EUCLEAN = 117; // Structure needs cleaning +pub const ENOTNAM = 118; // Not a XENIX named type file +pub const ENAVAIL = 119; // No XENIX semaphores available +pub const EISNAM = 120; // Is a named type file +pub const EREMOTEIO = 121; // Remote I/O error +pub const EDQUOT = 122; // Quota exceeded + +pub const ENOMEDIUM = 123; // No medium found +pub const EMEDIUMTYPE = 124; // Wrong medium type + +// nameserver query return codes +pub const ENSROK = 0; // DNS server returned answer with no data +pub const ENSRNODATA = 160; // DNS server returned answer with no data +pub const ENSRFORMERR = 161; // DNS server claims query was misformatted +pub const ENSRSERVFAIL = 162; // DNS server returned general failure +pub const ENSRNOTFOUND = 163; // Domain name not found +pub const ENSRNOTIMP = 164; // DNS server does not implement requested operation +pub const ENSRREFUSED = 165; // DNS server refused query +pub const ENSRBADQUERY = 166; // Misformatted DNS query +pub const ENSRBADNAME = 167; // Misformatted domain name +pub const ENSRBADFAMILY = 168; // Unsupported address family +pub const ENSRBADRESP = 169; // Misformatted DNS reply +pub const ENSRCONNREFUSED = 170; // Could not contact DNS servers +pub const ENSRTIMEOUT = 171; // Timeout while contacting DNS servers +pub const ENSROF = 172; // End of file +pub const ENSRFILE = 173; // Error reading file +pub const ENSRNOMEM = 174; // Out of memory +pub const ENSRDESTRUCTION = 175; // Application terminated lookup +pub const ENSRQUERYDOMAINTOOLONG = 176; // Domain name is too long +pub const ENSRCNAMELOOP = 177; // Domain name is too long diff --git a/std/os/index.zig b/std/os/index.zig new file mode 100644 index 0000000000000000000000000000000000000000..63e74924fa08bed59f2f772626bcba2fb3ca2c33 --- /dev/null +++ b/std/os/index.zig @@ -0,0 +1,78 @@ +pub const windows = @import("windows.zig"); +pub const darwin = @import("darwin.zig"); +pub const linux = @import("linux.zig"); +pub const posix = switch(@compileVar("os")) { + Os.linux => linux, + Os.darwin, Os.macosx, Os.ios => darwin, + Os.windows => windows, + else => @compileError("Unsupported OS"), +}; + +const errno = @import("errno.zig"); +const linking_libc = @import("../target.zig").linking_libc; +const c = @import("../c/index.zig"); + +error Unexpected; + +/// Fills `buf` with random bytes. If linking against libc, this calls the +/// appropriate OS-specific library call. Otherwise it uses the zig standard +/// library implementation. +pub fn getRandomBytes(buf: []u8) -> %void { + while (true) { + const err = switch (@compileVar("os")) { + Os.linux => { + if (linking_libc) { + if (c.getrandom(buf.ptr, buf.len, 0) == -1) *c._errno() else 0 + } else { + posix.getErrno(posix.getrandom(buf.ptr, buf.len, 0)) + } + }, + Os.darwin, Os.macosx, Os.ios => { + if (linking_libc) { + if (posix.getrandom(buf.ptr, buf.len) == -1) *c._errno() else 0 + } else { + posix.getErrno(posix.getrandom(buf.ptr, buf.len)) + } + }, + Os.windows => { + var hCryptProv: windows.HCRYPTPROV = undefined; + if (!windows.CryptAcquireContext(&hCryptProv, null, null, windows.PROV_RSA_FULL, 0)) { + return error.Unexpected; + } + defer _ = windows.CryptReleaseContext(hCryptProv, 0); + + if (!windows.CryptGenRandom(hCryptProv, windows.DWORD(buf.len), buf.ptr)) { + return error.Unexpected; + } + return; + }, + else => @compileError("Unsupported OS"), + }; + if (err > 0) { + return switch (err) { + errno.EINVAL => unreachable, + errno.EFAULT => unreachable, + errno.EINTR => continue, + else => error.Unexpected, + } + } + return; + } +} + +/// Raises a signal in the current kernel thread, ending its execution. +/// If linking against libc, this calls the abort() libc function. Otherwise +/// it uses the zig standard library implementation. +pub coldcc fn abort() -> noreturn { + if (linking_libc) { + c.abort(); + } + switch (@compileVar("os")) { + Os.linux => { + _ = posix.raise(posix.SIGABRT); + _ = posix.raise(posix.SIGKILL); + while (true) {} + }, + else => @compileError("Unsupported OS"), + } +} diff --git a/std/os/linux.zig b/std/os/linux.zig new file mode 100644 index 0000000000000000000000000000000000000000..be86136deea678b6aedb3af6a9216a63c8895c5a --- /dev/null +++ b/std/os/linux.zig @@ -0,0 +1,492 @@ +const arch = switch (@compileVar("arch")) { + Arch.x86_64 => @import("linux_x86_64.zig"), + Arch.i386 => @import("linux_i386.zig"), + else => @compileError("unsupported arch"), +}; +const errno = @import("errno.zig"); + +pub const MMAP_PROT_NONE = 0; +pub const MMAP_PROT_READ = 1; +pub const MMAP_PROT_WRITE = 2; +pub const MMAP_PROT_EXEC = 4; + +pub const MMAP_MAP_FILE = 0; +pub const MMAP_MAP_SHARED = 1; +pub const MMAP_MAP_PRIVATE = 2; +pub const MMAP_MAP_FIXED = 16; +pub const MMAP_MAP_ANON = 32; + +pub const SIGHUP = 1; +pub const SIGINT = 2; +pub const SIGQUIT = 3; +pub const SIGILL = 4; +pub const SIGTRAP = 5; +pub const SIGABRT = 6; +pub const SIGIOT = SIGABRT; +pub const SIGBUS = 7; +pub const SIGFPE = 8; +pub const SIGKILL = 9; +pub const SIGUSR1 = 10; +pub const SIGSEGV = 11; +pub const SIGUSR2 = 12; +pub const SIGPIPE = 13; +pub const SIGALRM = 14; +pub const SIGTERM = 15; +pub const SIGSTKFLT = 16; +pub const SIGCHLD = 17; +pub const SIGCONT = 18; +pub const SIGSTOP = 19; +pub const SIGTSTP = 20; +pub const SIGTTIN = 21; +pub const SIGTTOU = 22; +pub const SIGURG = 23; +pub const SIGXCPU = 24; +pub const SIGXFSZ = 25; +pub const SIGVTALRM = 26; +pub const SIGPROF = 27; +pub const SIGWINCH = 28; +pub const SIGIO = 29; +pub const SIGPOLL = 29; +pub const SIGPWR = 30; +pub const SIGSYS = 31; +pub const SIGUNUSED = SIGSYS; + +pub const O_RDONLY = 0o0; +pub const O_WRONLY = 0o1; +pub const O_RDWR = 0o2; + +pub const O_CREAT = arch.O_CREAT; +pub const O_EXCL = arch.O_EXCL; +pub const O_NOCTTY = arch.O_NOCTTY; +pub const O_TRUNC = arch.O_TRUNC; +pub const O_APPEND = arch.O_APPEND; +pub const O_NONBLOCK = arch.O_NONBLOCK; +pub const O_DSYNC = arch.O_DSYNC; +pub const O_SYNC = arch.O_SYNC; +pub const O_RSYNC = arch.O_RSYNC; +pub const O_DIRECTORY = arch.O_DIRECTORY; +pub const O_NOFOLLOW = arch.O_NOFOLLOW; +pub const O_CLOEXEC = arch.O_CLOEXEC; + +pub const O_ASYNC = arch.O_ASYNC; +pub const O_DIRECT = arch.O_DIRECT; +pub const O_LARGEFILE = arch.O_LARGEFILE; +pub const O_NOATIME = arch.O_NOATIME; +pub const O_PATH = arch.O_PATH; +pub const O_TMPFILE = arch.O_TMPFILE; +pub const O_NDELAY = arch.O_NDELAY; + +pub const SEEK_SET = 0; +pub const SEEK_CUR = 1; +pub const SEEK_END = 2; + +const SIG_BLOCK = 0; +const SIG_UNBLOCK = 1; +const SIG_SETMASK = 2; + +pub const SOCK_STREAM = 1; +pub const SOCK_DGRAM = 2; +pub const SOCK_RAW = 3; +pub const SOCK_RDM = 4; +pub const SOCK_SEQPACKET = 5; +pub const SOCK_DCCP = 6; +pub const SOCK_PACKET = 10; +pub const SOCK_CLOEXEC = 0o2000000; +pub const SOCK_NONBLOCK = 0o4000; + + +pub const PROTO_ip = 0o000; +pub const PROTO_icmp = 0o001; +pub const PROTO_igmp = 0o002; +pub const PROTO_ggp = 0o003; +pub const PROTO_ipencap = 0o004; +pub const PROTO_st = 0o005; +pub const PROTO_tcp = 0o006; +pub const PROTO_egp = 0o010; +pub const PROTO_pup = 0o014; +pub const PROTO_udp = 0o021; +pub const PROTO_hmp = 0o024; +pub const PROTO_xns_idp = 0o026; +pub const PROTO_rdp = 0o033; +pub const PROTO_iso_tp4 = 0o035; +pub const PROTO_xtp = 0o044; +pub const PROTO_ddp = 0o045; +pub const PROTO_idpr_cmtp = 0o046; +pub const PROTO_ipv6 = 0o051; +pub const PROTO_ipv6_route = 0o053; +pub const PROTO_ipv6_frag = 0o054; +pub const PROTO_idrp = 0o055; +pub const PROTO_rsvp = 0o056; +pub const PROTO_gre = 0o057; +pub const PROTO_esp = 0o062; +pub const PROTO_ah = 0o063; +pub const PROTO_skip = 0o071; +pub const PROTO_ipv6_icmp = 0o072; +pub const PROTO_ipv6_nonxt = 0o073; +pub const PROTO_ipv6_opts = 0o074; +pub const PROTO_rspf = 0o111; +pub const PROTO_vmtp = 0o121; +pub const PROTO_ospf = 0o131; +pub const PROTO_ipip = 0o136; +pub const PROTO_encap = 0o142; +pub const PROTO_pim = 0o147; +pub const PROTO_raw = 0o377; + +pub const PF_UNSPEC = 0; +pub const PF_LOCAL = 1; +pub const PF_UNIX = PF_LOCAL; +pub const PF_FILE = PF_LOCAL; +pub const PF_INET = 2; +pub const PF_AX25 = 3; +pub const PF_IPX = 4; +pub const PF_APPLETALK = 5; +pub const PF_NETROM = 6; +pub const PF_BRIDGE = 7; +pub const PF_ATMPVC = 8; +pub const PF_X25 = 9; +pub const PF_INET6 = 10; +pub const PF_ROSE = 11; +pub const PF_DECnet = 12; +pub const PF_NETBEUI = 13; +pub const PF_SECURITY = 14; +pub const PF_KEY = 15; +pub const PF_NETLINK = 16; +pub const PF_ROUTE = PF_NETLINK; +pub const PF_PACKET = 17; +pub const PF_ASH = 18; +pub const PF_ECONET = 19; +pub const PF_ATMSVC = 20; +pub const PF_RDS = 21; +pub const PF_SNA = 22; +pub const PF_IRDA = 23; +pub const PF_PPPOX = 24; +pub const PF_WANPIPE = 25; +pub const PF_LLC = 26; +pub const PF_IB = 27; +pub const PF_MPLS = 28; +pub const PF_CAN = 29; +pub const PF_TIPC = 30; +pub const PF_BLUETOOTH = 31; +pub const PF_IUCV = 32; +pub const PF_RXRPC = 33; +pub const PF_ISDN = 34; +pub const PF_PHONET = 35; +pub const PF_IEEE802154 = 36; +pub const PF_CAIF = 37; +pub const PF_ALG = 38; +pub const PF_NFC = 39; +pub const PF_VSOCK = 40; +pub const PF_MAX = 41; + +pub const AF_UNSPEC = PF_UNSPEC; +pub const AF_LOCAL = PF_LOCAL; +pub const AF_UNIX = AF_LOCAL; +pub const AF_FILE = AF_LOCAL; +pub const AF_INET = PF_INET; +pub const AF_AX25 = PF_AX25; +pub const AF_IPX = PF_IPX; +pub const AF_APPLETALK = PF_APPLETALK; +pub const AF_NETROM = PF_NETROM; +pub const AF_BRIDGE = PF_BRIDGE; +pub const AF_ATMPVC = PF_ATMPVC; +pub const AF_X25 = PF_X25; +pub const AF_INET6 = PF_INET6; +pub const AF_ROSE = PF_ROSE; +pub const AF_DECnet = PF_DECnet; +pub const AF_NETBEUI = PF_NETBEUI; +pub const AF_SECURITY = PF_SECURITY; +pub const AF_KEY = PF_KEY; +pub const AF_NETLINK = PF_NETLINK; +pub const AF_ROUTE = PF_ROUTE; +pub const AF_PACKET = PF_PACKET; +pub const AF_ASH = PF_ASH; +pub const AF_ECONET = PF_ECONET; +pub const AF_ATMSVC = PF_ATMSVC; +pub const AF_RDS = PF_RDS; +pub const AF_SNA = PF_SNA; +pub const AF_IRDA = PF_IRDA; +pub const AF_PPPOX = PF_PPPOX; +pub const AF_WANPIPE = PF_WANPIPE; +pub const AF_LLC = PF_LLC; +pub const AF_IB = PF_IB; +pub const AF_MPLS = PF_MPLS; +pub const AF_CAN = PF_CAN; +pub const AF_TIPC = PF_TIPC; +pub const AF_BLUETOOTH = PF_BLUETOOTH; +pub const AF_IUCV = PF_IUCV; +pub const AF_RXRPC = PF_RXRPC; +pub const AF_ISDN = PF_ISDN; +pub const AF_PHONET = PF_PHONET; +pub const AF_IEEE802154 = PF_IEEE802154; +pub const AF_CAIF = PF_CAIF; +pub const AF_ALG = PF_ALG; +pub const AF_NFC = PF_NFC; +pub const AF_VSOCK = PF_VSOCK; +pub const AF_MAX = PF_MAX; + +/// Get the errno from a syscall return value, or 0 for no error. +pub fn getErrno(r: usize) -> usize { + const signed_r = *(&isize)(&r); + if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0 +} + +pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: usize, fd: i32, offset: usize) + -> usize +{ + arch.syscall6(arch.SYS_mmap, usize(address), length, prot, flags, usize(fd), offset) +} + +pub fn munmap(address: &u8, length: usize) -> usize { + arch.syscall2(arch.SYS_munmap, usize(address), length) +} + +pub fn read(fd: i32, buf: &u8, count: usize) -> usize { + arch.syscall3(arch.SYS_read, usize(fd), usize(buf), count) +} + +pub fn pread(fd: i32, buf: &u8, count: usize, offset: usize) -> usize { + arch.syscall4(arch.SYS_pread, usize(fd), usize(buf), count, offset) +} + +pub fn write(fd: i32, buf: &const u8, count: usize) -> usize { + arch.syscall3(arch.SYS_write, usize(fd), usize(buf), count) +} + +pub fn pwrite(fd: i32, buf: &const u8, count: usize, offset: usize) -> usize { + arch.syscall4(arch.SYS_pwrite, usize(fd), usize(buf), count, offset) +} + +pub fn open_c(path: &const u8, flags: usize, perm: usize) -> usize { + arch.syscall3(arch.SYS_open, usize(path), flags, perm) +} + +pub fn open(path: []const u8, flags: usize, perm: usize) -> usize { + const buf = @alloca(u8, path.len + 1); + @memcpy(&buf[0], &path[0], path.len); + buf[path.len] = 0; + return open_c(buf.ptr, flags, perm); +} + +pub fn create_c(path: &const u8, perm: usize) -> usize { + arch.syscall2(arch.SYS_creat, usize(path), perm) +} + +pub fn create(path: []const u8, perm: usize) -> usize { + const buf = @alloca(u8, path.len + 1); + @memcpy(&buf[0], &path[0], path.len); + buf[path.len] = 0; + return create_c(buf.ptr, perm); +} + +pub fn openat_c(dirfd: i32, path: &const u8, flags: usize, mode: usize) -> usize { + arch.syscall4(arch.SYS_openat, usize(dirfd), usize(path), flags, mode) +} + +pub fn openat(dirfd: i32, path: []const u8, flags: usize, mode: usize) -> usize { + const buf = @alloca(u8, path.len + 1); + @memcpy(&buf[0], &path[0], path.len); + buf[path.len] = 0; + return openat_c(dirfd, buf.ptr, flags, mode); +} + +pub fn close(fd: i32) -> usize { + arch.syscall1(arch.SYS_close, usize(fd)) +} + +pub fn lseek(fd: i32, offset: usize, ref_pos: usize) -> usize { + arch.syscall3(arch.SYS_lseek, usize(fd), offset, ref_pos) +} + +pub fn exit(status: i32) -> noreturn { + _ = arch.syscall1(arch.SYS_exit, usize(status)); + unreachable +} + +pub fn getrandom(buf: &u8, count: usize, flags: u32) -> usize { + arch.syscall3(arch.SYS_getrandom, usize(buf), count, usize(flags)) +} + +pub fn kill(pid: i32, sig: i32) -> i32 { + i32(arch.syscall2(arch.SYS_kill, usize(pid), usize(sig))) +} + +const NSIG = 65; +const sigset_t = [128]u8; +const all_mask = []u8 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }; +const app_mask = []u8 { 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, }; + +pub fn raise(sig: i32) -> i32 { + var set: sigset_t = undefined; + blockAppSignals(&set); + const tid = i32(arch.syscall0(arch.SYS_gettid)); + const ret = i32(arch.syscall2(arch.SYS_tkill, usize(tid), usize(sig))); + restoreSignals(&set); + return ret; +} + +fn blockAllSignals(set: &sigset_t) { + _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, usize(&all_mask), usize(set), NSIG/8); +} + +fn blockAppSignals(set: &sigset_t) { + _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_BLOCK, usize(&app_mask), usize(set), NSIG/8); +} + +fn restoreSignals(set: &sigset_t) { + _ = arch.syscall4(arch.SYS_rt_sigprocmask, SIG_SETMASK, usize(set), 0, NSIG/8); +} + + +pub const sa_family_t = u16; +pub const socklen_t = u32; +pub const in_addr = u32; +pub const in6_addr = [16]u8; + +pub const sockaddr = extern struct { + family: sa_family_t, + port: u16, + data: [12]u8, +}; + +pub const sockaddr_in = extern struct { + family: sa_family_t, + port: u16, + addr: in_addr, + zero: [8]u8, +}; + +pub const sockaddr_in6 = extern struct { + family: sa_family_t, + port: u16, + flowinfo: u32, + addr: in6_addr, + scope_id: u32, +}; + +pub const iovec = extern struct { + iov_base: &u8, + iov_len: usize, +}; + +// +//const IF_NAMESIZE = 16; +// +//export struct ifreq { +// ifrn_name: [IF_NAMESIZE]u8, +// union { +// ifru_addr: sockaddr, +// ifru_dstaddr: sockaddr, +// ifru_broadaddr: sockaddr, +// ifru_netmask: sockaddr, +// ifru_hwaddr: sockaddr, +// ifru_flags: i16, +// ifru_ivalue: i32, +// ifru_mtu: i32, +// ifru_map: ifmap, +// ifru_slave: [IF_NAMESIZE]u8, +// ifru_newname: [IF_NAMESIZE]u8, +// ifru_data: &u8, +// } ifr_ifru; +//} +// + +pub fn getsockname(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize { + arch.syscall3(arch.SYS_getsockname, usize(fd), usize(addr), usize(len)) +} + +pub fn getpeername(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize { + arch.syscall3(arch.SYS_getpeername, usize(fd), usize(addr), usize(len)) +} + +pub fn socket(domain: i32, socket_type: i32, protocol: i32) -> usize { + arch.syscall3(arch.SYS_socket, usize(domain), usize(socket_type), usize(protocol)) +} + +pub fn setsockopt(fd: i32, level: i32, optname: i32, optval: &const u8, optlen: socklen_t) -> usize { + arch.syscall5(arch.SYS_setsockopt, usize(fd), usize(level), usize(optname), usize(optval), usize(optlen)) +} + +pub fn getsockopt(fd: i32, level: i32, optname: i32, noalias optval: &u8, noalias optlen: &socklen_t) -> usize { + arch.syscall5(arch.SYS_getsockopt, usize(fd), usize(level), usize(optname), usize(optval), usize(optlen)) +} + +pub fn sendmsg(fd: i32, msg: &const arch.msghdr, flags: u32) -> usize { + arch.syscall3(arch.SYS_sendmsg, usize(fd), usize(msg), flags) +} + +pub fn connect(fd: i32, addr: &const sockaddr, len: socklen_t) -> usize { + arch.syscall3(arch.SYS_connect, usize(fd), usize(addr), usize(len)) +} + +pub fn recvmsg(fd: i32, msg: &arch.msghdr, flags: u32) -> usize { + arch.syscall3(arch.SYS_recvmsg, usize(fd), usize(msg), flags) +} + +pub fn recvfrom(fd: i32, noalias buf: &u8, len: usize, flags: u32, + noalias addr: ?&sockaddr, noalias alen: ?&socklen_t) -> usize +{ + arch.syscall6(arch.SYS_recvfrom, usize(fd), usize(buf), len, flags, usize(addr), usize(alen)) +} + +pub fn shutdown(fd: i32, how: i32) -> usize { + arch.syscall2(arch.SYS_shutdown, usize(fd), usize(how)) +} + +pub fn bind(fd: i32, addr: &const sockaddr, len: socklen_t) -> usize { + arch.syscall3(arch.SYS_bind, usize(fd), usize(addr), usize(len)) +} + +pub fn listen(fd: i32, backlog: i32) -> usize { + arch.syscall2(arch.SYS_listen, usize(fd), usize(backlog)) +} + +pub fn sendto(fd: i32, buf: &const u8, len: usize, flags: u32, addr: ?&const sockaddr, alen: socklen_t) -> usize { + arch.syscall6(arch.SYS_sendto, usize(fd), usize(buf), len, flags, usize(addr), usize(alen)) +} + +pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) -> usize { + arch.syscall4(arch.SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), usize(&fd[0])) +} + +pub fn accept(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize { + accept4(fd, addr, len, 0) +} + +pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags: u32) -> usize { + arch.syscall4(arch.SYS_accept4, usize(fd), usize(addr), usize(len), flags) +} + +// error NameTooLong; +// error SystemResources; +// error Io; +// +// pub fn if_nametoindex(name: []u8) -> %u32 { +// var ifr: ifreq = undefined; +// +// if (name.len >= ifr.ifr_name.len) { +// return error.NameTooLong; +// } +// +// const socket_ret = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); +// const socket_err = getErrno(socket_ret); +// if (socket_err > 0) { +// return error.SystemResources; +// } +// const socket_fd = i32(socket_ret); +// @memcpy(&ifr.ifr_name[0], &name[0], name.len); +// ifr.ifr_name[name.len] = 0; +// const ioctl_ret = ioctl(socket_fd, SIOCGIFINDEX, &ifr); +// close(socket_fd); +// const ioctl_err = getErrno(ioctl_ret); +// if (ioctl_err > 0) { +// return error.Io; +// } +// return ifr.ifr_ifindex; +// } + +pub const stat = arch.stat; +pub const timespec = arch.timespec; + +pub fn fstat(fd: i32, stat_buf: &stat) -> usize { + arch.syscall2(arch.SYS_fstat, usize(fd), usize(stat_buf)) +} diff --git a/std/os/linux_i386.zig b/std/os/linux_i386.zig new file mode 100644 index 0000000000000000000000000000000000000000..218043307e31e12479113c1cf0f255981b84b541 --- /dev/null +++ b/std/os/linux_i386.zig @@ -0,0 +1,497 @@ +const linux = @import("linux.zig"); +const socklen_t = linux.socklen_t; +const iovec = linux.iovec; + +pub const SYS_restart_syscall = 0; +pub const SYS_exit = 1; +pub const SYS_fork = 2; +pub const SYS_read = 3; +pub const SYS_write = 4; +pub const SYS_open = 5; +pub const SYS_close = 6; +pub const SYS_waitpid = 7; +pub const SYS_creat = 8; +pub const SYS_link = 9; +pub const SYS_unlink = 10; +pub const SYS_execve = 11; +pub const SYS_chdir = 12; +pub const SYS_time = 13; +pub const SYS_mknod = 14; +pub const SYS_chmod = 15; +pub const SYS_lchown = 16; +pub const SYS_break = 17; +pub const SYS_oldstat = 18; +pub const SYS_lseek = 19; +pub const SYS_getpid = 20; +pub const SYS_mount = 21; +pub const SYS_umount = 22; +pub const SYS_setuid = 23; +pub const SYS_getuid = 24; +pub const SYS_stime = 25; +pub const SYS_ptrace = 26; +pub const SYS_alarm = 27; +pub const SYS_oldfstat = 28; +pub const SYS_pause = 29; +pub const SYS_utime = 30; +pub const SYS_stty = 31; +pub const SYS_gtty = 32; +pub const SYS_access = 33; +pub const SYS_nice = 34; +pub const SYS_ftime = 35; +pub const SYS_sync = 36; +pub const SYS_kill = 37; +pub const SYS_rename = 38; +pub const SYS_mkdir = 39; +pub const SYS_rmdir = 40; +pub const SYS_dup = 41; +pub const SYS_pipe = 42; +pub const SYS_times = 43; +pub const SYS_prof = 44; +pub const SYS_brk = 45; +pub const SYS_setgid = 46; +pub const SYS_getgid = 47; +pub const SYS_signal = 48; +pub const SYS_geteuid = 49; +pub const SYS_getegid = 50; +pub const SYS_acct = 51; +pub const SYS_umount2 = 52; +pub const SYS_lock = 53; +pub const SYS_ioctl = 54; +pub const SYS_fcntl = 55; +pub const SYS_mpx = 56; +pub const SYS_setpgid = 57; +pub const SYS_ulimit = 58; +pub const SYS_oldolduname = 59; +pub const SYS_umask = 60; +pub const SYS_chroot = 61; +pub const SYS_ustat = 62; +pub const SYS_dup2 = 63; +pub const SYS_getppid = 64; +pub const SYS_getpgrp = 65; +pub const SYS_setsid = 66; +pub const SYS_sigaction = 67; +pub const SYS_sgetmask = 68; +pub const SYS_ssetmask = 69; +pub const SYS_setreuid = 70; +pub const SYS_setregid = 71; +pub const SYS_sigsuspend = 72; +pub const SYS_sigpending = 73; +pub const SYS_sethostname = 74; +pub const SYS_setrlimit = 75; +pub const SYS_getrlimit = 76; +pub const SYS_getrusage = 77; +pub const SYS_gettimeofday = 78; +pub const SYS_settimeofday = 79; +pub const SYS_getgroups = 80; +pub const SYS_setgroups = 81; +pub const SYS_select = 82; +pub const SYS_symlink = 83; +pub const SYS_oldlstat = 84; +pub const SYS_readlink = 85; +pub const SYS_uselib = 86; +pub const SYS_swapon = 87; +pub const SYS_reboot = 88; +pub const SYS_readdir = 89; +pub const SYS_mmap = 90; +pub const SYS_munmap = 91; +pub const SYS_truncate = 92; +pub const SYS_ftruncate = 93; +pub const SYS_fchmod = 94; +pub const SYS_fchown = 95; +pub const SYS_getpriority = 96; +pub const SYS_setpriority = 97; +pub const SYS_profil = 98; +pub const SYS_statfs = 99; +pub const SYS_fstatfs = 100; +pub const SYS_ioperm = 101; +pub const SYS_socketcall = 102; +pub const SYS_syslog = 103; +pub const SYS_setitimer = 104; +pub const SYS_getitimer = 105; +pub const SYS_stat = 106; +pub const SYS_lstat = 107; +pub const SYS_fstat = 108; +pub const SYS_olduname = 109; +pub const SYS_iopl = 110; +pub const SYS_vhangup = 111; +pub const SYS_idle = 112; +pub const SYS_vm86old = 113; +pub const SYS_wait4 = 114; +pub const SYS_swapoff = 115; +pub const SYS_sysinfo = 116; +pub const SYS_ipc = 117; +pub const SYS_fsync = 118; +pub const SYS_sigreturn = 119; +pub const SYS_clone = 120; +pub const SYS_setdomainname = 121; +pub const SYS_uname = 122; +pub const SYS_modify_ldt = 123; +pub const SYS_adjtimex = 124; +pub const SYS_mprotect = 125; +pub const SYS_sigprocmask = 126; +pub const SYS_create_module = 127; +pub const SYS_init_module = 128; +pub const SYS_delete_module = 129; +pub const SYS_get_kernel_syms = 130; +pub const SYS_quotactl = 131; +pub const SYS_getpgid = 132; +pub const SYS_fchdir = 133; +pub const SYS_bdflush = 134; +pub const SYS_sysfs = 135; +pub const SYS_personality = 136; +pub const SYS_afs_syscall = 137; +pub const SYS_setfsuid = 138; +pub const SYS_setfsgid = 139; +pub const SYS__llseek = 140; +pub const SYS_getdents = 141; +pub const SYS__newselect = 142; +pub const SYS_flock = 143; +pub const SYS_msync = 144; +pub const SYS_readv = 145; +pub const SYS_writev = 146; +pub const SYS_getsid = 147; +pub const SYS_fdatasync = 148; +pub const SYS__sysctl = 149; +pub const SYS_mlock = 150; +pub const SYS_munlock = 151; +pub const SYS_mlockall = 152; +pub const SYS_munlockall = 153; +pub const SYS_sched_setparam = 154; +pub const SYS_sched_getparam = 155; +pub const SYS_sched_setscheduler = 156; +pub const SYS_sched_getscheduler = 157; +pub const SYS_sched_yield = 158; +pub const SYS_sched_get_priority_max = 159; +pub const SYS_sched_get_priority_min = 160; +pub const SYS_sched_rr_get_interval = 161; +pub const SYS_nanosleep = 162; +pub const SYS_mremap = 163; +pub const SYS_setresuid = 164; +pub const SYS_getresuid = 165; +pub const SYS_vm86 = 166; +pub const SYS_query_module = 167; +pub const SYS_poll = 168; +pub const SYS_nfsservctl = 169; +pub const SYS_setresgid = 170; +pub const SYS_getresgid = 171; +pub const SYS_prctl = 172; +pub const SYS_rt_sigreturn = 173; +pub const SYS_rt_sigaction = 174; +pub const SYS_rt_sigprocmask = 175; +pub const SYS_rt_sigpending = 176; +pub const SYS_rt_sigtimedwait = 177; +pub const SYS_rt_sigqueueinfo = 178; +pub const SYS_rt_sigsuspend = 179; +pub const SYS_pread64 = 180; +pub const SYS_pwrite64 = 181; +pub const SYS_chown = 182; +pub const SYS_getcwd = 183; +pub const SYS_capget = 184; +pub const SYS_capset = 185; +pub const SYS_sigaltstack = 186; +pub const SYS_sendfile = 187; +pub const SYS_getpmsg = 188; +pub const SYS_putpmsg = 189; +pub const SYS_vfork = 190; +pub const SYS_ugetrlimit = 191; +pub const SYS_mmap2 = 192; +pub const SYS_truncate64 = 193; +pub const SYS_ftruncate64 = 194; +pub const SYS_stat64 = 195; +pub const SYS_lstat64 = 196; +pub const SYS_fstat64 = 197; +pub const SYS_lchown32 = 198; +pub const SYS_getuid32 = 199; +pub const SYS_getgid32 = 200; +pub const SYS_geteuid32 = 201; +pub const SYS_getegid32 = 202; +pub const SYS_setreuid32 = 203; +pub const SYS_setregid32 = 204; +pub const SYS_getgroups32 = 205; +pub const SYS_setgroups32 = 206; +pub const SYS_fchown32 = 207; +pub const SYS_setresuid32 = 208; +pub const SYS_getresuid32 = 209; +pub const SYS_setresgid32 = 210; +pub const SYS_getresgid32 = 211; +pub const SYS_chown32 = 212; +pub const SYS_setuid32 = 213; +pub const SYS_setgid32 = 214; +pub const SYS_setfsuid32 = 215; +pub const SYS_setfsgid32 = 216; +pub const SYS_pivot_root = 217; +pub const SYS_mincore = 218; +pub const SYS_madvise = 219; +pub const SYS_madvise1 = 219; +pub const SYS_getdents64 = 220; +pub const SYS_fcntl64 = 221; +pub const SYS_gettid = 224; +pub const SYS_readahead = 225; +pub const SYS_setxattr = 226; +pub const SYS_lsetxattr = 227; +pub const SYS_fsetxattr = 228; +pub const SYS_getxattr = 229; +pub const SYS_lgetxattr = 230; +pub const SYS_fgetxattr = 231; +pub const SYS_listxattr = 232; +pub const SYS_llistxattr = 233; +pub const SYS_flistxattr = 234; +pub const SYS_removexattr = 235; +pub const SYS_lremovexattr = 236; +pub const SYS_fremovexattr = 237; +pub const SYS_tkill = 238; +pub const SYS_sendfile64 = 239; +pub const SYS_futex = 240; +pub const SYS_sched_setaffinity = 241; +pub const SYS_sched_getaffinity = 242; +pub const SYS_set_thread_area = 243; +pub const SYS_get_thread_area = 244; +pub const SYS_io_setup = 245; +pub const SYS_io_destroy = 246; +pub const SYS_io_getevents = 247; +pub const SYS_io_submit = 248; +pub const SYS_io_cancel = 249; +pub const SYS_fadvise64 = 250; +pub const SYS_exit_group = 252; +pub const SYS_lookup_dcookie = 253; +pub const SYS_epoll_create = 254; +pub const SYS_epoll_ctl = 255; +pub const SYS_epoll_wait = 256; +pub const SYS_remap_file_pages = 257; +pub const SYS_set_tid_address = 258; +pub const SYS_timer_create = 259; +pub const SYS_timer_settime = SYS_timer_create+1; +pub const SYS_timer_gettime = SYS_timer_create+2; +pub const SYS_timer_getoverrun = SYS_timer_create+3; +pub const SYS_timer_delete = SYS_timer_create+4; +pub const SYS_clock_settime = SYS_timer_create+5; +pub const SYS_clock_gettime = SYS_timer_create+6; +pub const SYS_clock_getres = SYS_timer_create+7; +pub const SYS_clock_nanosleep = SYS_timer_create+8; +pub const SYS_statfs64 = 268; +pub const SYS_fstatfs64 = 269; +pub const SYS_tgkill = 270; +pub const SYS_utimes = 271; +pub const SYS_fadvise64_64 = 272; +pub const SYS_vserver = 273; +pub const SYS_mbind = 274; +pub const SYS_get_mempolicy = 275; +pub const SYS_set_mempolicy = 276; +pub const SYS_mq_open = 277; +pub const SYS_mq_unlink = SYS_mq_open+1; +pub const SYS_mq_timedsend = SYS_mq_open+2; +pub const SYS_mq_timedreceive = SYS_mq_open+3; +pub const SYS_mq_notify = SYS_mq_open+4; +pub const SYS_mq_getsetattr = SYS_mq_open+5; +pub const SYS_kexec_load = 283; +pub const SYS_waitid = 284; +pub const SYS_add_key = 286; +pub const SYS_request_key = 287; +pub const SYS_keyctl = 288; +pub const SYS_ioprio_set = 289; +pub const SYS_ioprio_get = 290; +pub const SYS_inotify_init = 291; +pub const SYS_inotify_add_watch = 292; +pub const SYS_inotify_rm_watch = 293; +pub const SYS_migrate_pages = 294; +pub const SYS_openat = 295; +pub const SYS_mkdirat = 296; +pub const SYS_mknodat = 297; +pub const SYS_fchownat = 298; +pub const SYS_futimesat = 299; +pub const SYS_fstatat64 = 300; +pub const SYS_unlinkat = 301; +pub const SYS_renameat = 302; +pub const SYS_linkat = 303; +pub const SYS_symlinkat = 304; +pub const SYS_readlinkat = 305; +pub const SYS_fchmodat = 306; +pub const SYS_faccessat = 307; +pub const SYS_pselect6 = 308; +pub const SYS_ppoll = 309; +pub const SYS_unshare = 310; +pub const SYS_set_robust_list = 311; +pub const SYS_get_robust_list = 312; +pub const SYS_splice = 313; +pub const SYS_sync_file_range = 314; +pub const SYS_tee = 315; +pub const SYS_vmsplice = 316; +pub const SYS_move_pages = 317; +pub const SYS_getcpu = 318; +pub const SYS_epoll_pwait = 319; +pub const SYS_utimensat = 320; +pub const SYS_signalfd = 321; +pub const SYS_timerfd_create = 322; +pub const SYS_eventfd = 323; +pub const SYS_fallocate = 324; +pub const SYS_timerfd_settime = 325; +pub const SYS_timerfd_gettime = 326; +pub const SYS_signalfd4 = 327; +pub const SYS_eventfd2 = 328; +pub const SYS_epoll_create1 = 329; +pub const SYS_dup3 = 330; +pub const SYS_pipe2 = 331; +pub const SYS_inotify_init1 = 332; +pub const SYS_preadv = 333; +pub const SYS_pwritev = 334; +pub const SYS_rt_tgsigqueueinfo = 335; +pub const SYS_perf_event_open = 336; +pub const SYS_recvmmsg = 337; +pub const SYS_fanotify_init = 338; +pub const SYS_fanotify_mark = 339; +pub const SYS_prlimit64 = 340; +pub const SYS_name_to_handle_at = 341; +pub const SYS_open_by_handle_at = 342; +pub const SYS_clock_adjtime = 343; +pub const SYS_syncfs = 344; +pub const SYS_sendmmsg = 345; +pub const SYS_setns = 346; +pub const SYS_process_vm_readv = 347; +pub const SYS_process_vm_writev = 348; +pub const SYS_kcmp = 349; +pub const SYS_finit_module = 350; +pub const SYS_sched_setattr = 351; +pub const SYS_sched_getattr = 352; +pub const SYS_renameat2 = 353; +pub const SYS_seccomp = 354; +pub const SYS_getrandom = 355; +pub const SYS_memfd_create = 356; +pub const SYS_bpf = 357; +pub const SYS_execveat = 358; +pub const SYS_socket = 359; +pub const SYS_socketpair = 360; +pub const SYS_bind = 361; +pub const SYS_connect = 362; +pub const SYS_listen = 363; +pub const SYS_accept4 = 364; +pub const SYS_getsockopt = 365; +pub const SYS_setsockopt = 366; +pub const SYS_getsockname = 367; +pub const SYS_getpeername = 368; +pub const SYS_sendto = 369; +pub const SYS_sendmsg = 370; +pub const SYS_recvfrom = 371; +pub const SYS_recvmsg = 372; +pub const SYS_shutdown = 373; +pub const SYS_userfaultfd = 374; +pub const SYS_membarrier = 375; +pub const SYS_mlock2 = 376; + + +pub const O_CREAT = 0o100; +pub const O_EXCL = 0o200; +pub const O_NOCTTY = 0o400; +pub const O_TRUNC = 0o1000; +pub const O_APPEND = 0o2000; +pub const O_NONBLOCK = 0o4000; +pub const O_DSYNC = 0o10000; +pub const O_SYNC = 0o4010000; +pub const O_RSYNC = 0o4010000; +pub const O_DIRECTORY = 0o200000; +pub const O_NOFOLLOW = 0o400000; +pub const O_CLOEXEC = 0o2000000; + +pub const O_ASYNC = 0o20000; +pub const O_DIRECT = 0o40000; +pub const O_LARGEFILE = 0o100000; +pub const O_NOATIME = 0o1000000; +pub const O_PATH = 0o10000000; +pub const O_TMPFILE = 0o20200000; +pub const O_NDELAY = O_NONBLOCK; + +pub const F_DUPFD = 0; +pub const F_GETFD = 1; +pub const F_SETFD = 2; +pub const F_GETFL = 3; +pub const F_SETFL = 4; + +pub const F_SETOWN = 8; +pub const F_GETOWN = 9; +pub const F_SETSIG = 10; +pub const F_GETSIG = 11; + +pub const F_GETLK = 12; +pub const F_SETLK = 13; +pub const F_SETLKW = 14; + +pub const F_SETOWN_EX = 15; +pub const F_GETOWN_EX = 16; + +pub const F_GETOWNER_UIDS = 17; + +pub inline fn syscall0(number: usize) -> usize { + asm volatile ("int 80h" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number)) +} + +pub inline fn syscall1(number: usize, arg1: usize) -> usize { + asm volatile ("int 80h" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1)) +} + +pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) -> usize { + asm volatile ("int 80h" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1), + [arg2] "{ecx}" (arg2)) +} + +pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) -> usize { + asm volatile ("int 80h" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1), + [arg2] "{ecx}" (arg2), + [arg3] "{edx}" (arg3)) +} + +pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) -> usize { + asm volatile ("int 80h" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1), + [arg2] "{ecx}" (arg2), + [arg3] "{edx}" (arg3), + [arg4] "{esi}" (arg4)) +} + +pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, + arg4: usize, arg5: usize) -> usize +{ + asm volatile ("int 80h" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1), + [arg2] "{ecx}" (arg2), + [arg3] "{edx}" (arg3), + [arg4] "{esi}" (arg4), + [arg5] "{edi}" (arg5)) +} + +pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, + arg4: usize, arg5: usize, arg6: usize) -> usize +{ + asm volatile ("int 80h" + : [ret] "={eax}" (-> usize) + : [number] "{eax}" (number), + [arg1] "{ebx}" (arg1), + [arg2] "{ecx}" (arg2), + [arg3] "{edx}" (arg3), + [arg4] "{esi}" (arg4), + [arg5] "{edi}" (arg5), + [arg6] "{ebp}" (arg6)) +} + +export struct msghdr { + msg_name: &u8, + msg_namelen: socklen_t, + msg_iov: &iovec, + msg_iovlen: i32, + msg_control: &u8, + msg_controllen: socklen_t, + msg_flags: i32, +} diff --git a/std/os/linux_x86_64.zig b/std/os/linux_x86_64.zig new file mode 100644 index 0000000000000000000000000000000000000000..0f8f163ce2046398cc89be6f12bac14b2be893c4 --- /dev/null +++ b/std/os/linux_x86_64.zig @@ -0,0 +1,480 @@ +const linux = @import("linux.zig"); +const socklen_t = linux.socklen_t; +const iovec = linux.iovec; + +pub const SYS_read = 0; +pub const SYS_write = 1; +pub const SYS_open = 2; +pub const SYS_close = 3; +pub const SYS_stat = 4; +pub const SYS_fstat = 5; +pub const SYS_lstat = 6; +pub const SYS_poll = 7; +pub const SYS_lseek = 8; +pub const SYS_mmap = 9; +pub const SYS_mprotect = 10; +pub const SYS_munmap = 11; +pub const SYS_brk = 12; +pub const SYS_rt_sigaction = 13; +pub const SYS_rt_sigprocmask = 14; +pub const SYS_rt_sigreturn = 15; +pub const SYS_ioctl = 16; +pub const SYS_pread = 17; +pub const SYS_pwrite = 18; +pub const SYS_readv = 19; +pub const SYS_writev = 20; +pub const SYS_access = 21; +pub const SYS_pipe = 22; +pub const SYS_select = 23; +pub const SYS_sched_yield = 24; +pub const SYS_mremap = 25; +pub const SYS_msync = 26; +pub const SYS_mincore = 27; +pub const SYS_madvise = 28; +pub const SYS_shmget = 29; +pub const SYS_shmat = 30; +pub const SYS_shmctl = 31; +pub const SYS_dup = 32; +pub const SYS_dup2 = 33; +pub const SYS_pause = 34; +pub const SYS_nanosleep = 35; +pub const SYS_getitimer = 36; +pub const SYS_alarm = 37; +pub const SYS_setitimer = 38; +pub const SYS_getpid = 39; +pub const SYS_sendfile = 40; +pub const SYS_socket = 41; +pub const SYS_connect = 42; +pub const SYS_accept = 43; +pub const SYS_sendto = 44; +pub const SYS_recvfrom = 45; +pub const SYS_sendmsg = 46; +pub const SYS_recvmsg = 47; +pub const SYS_shutdown = 48; +pub const SYS_bind = 49; +pub const SYS_listen = 50; +pub const SYS_getsockname = 51; +pub const SYS_getpeername = 52; +pub const SYS_socketpair = 53; +pub const SYS_setsockopt = 54; +pub const SYS_getsockopt = 55; +pub const SYS_clone = 56; +pub const SYS_fork = 57; +pub const SYS_vfork = 58; +pub const SYS_execve = 59; +pub const SYS_exit = 60; +pub const SYS_wait4 = 61; +pub const SYS_kill = 62; +pub const SYS_uname = 63; +pub const SYS_semget = 64; +pub const SYS_semop = 65; +pub const SYS_semctl = 66; +pub const SYS_shmdt = 67; +pub const SYS_msgget = 68; +pub const SYS_msgsnd = 69; +pub const SYS_msgrcv = 70; +pub const SYS_msgctl = 71; +pub const SYS_fcntl = 72; +pub const SYS_flock = 73; +pub const SYS_fsync = 74; +pub const SYS_fdatasync = 75; +pub const SYS_truncate = 76; +pub const SYS_ftruncate = 77; +pub const SYS_getdents = 78; +pub const SYS_getcwd = 79; +pub const SYS_chdir = 80; +pub const SYS_fchdir = 81; +pub const SYS_rename = 82; +pub const SYS_mkdir = 83; +pub const SYS_rmdir = 84; +pub const SYS_creat = 85; +pub const SYS_link = 86; +pub const SYS_unlink = 87; +pub const SYS_symlink = 88; +pub const SYS_readlink = 89; +pub const SYS_chmod = 90; +pub const SYS_fchmod = 91; +pub const SYS_chown = 92; +pub const SYS_fchown = 93; +pub const SYS_lchown = 94; +pub const SYS_umask = 95; +pub const SYS_gettimeofday = 96; +pub const SYS_getrlimit = 97; +pub const SYS_getrusage = 98; +pub const SYS_sysinfo = 99; +pub const SYS_times = 100; +pub const SYS_ptrace = 101; +pub const SYS_getuid = 102; +pub const SYS_syslog = 103; +pub const SYS_getgid = 104; +pub const SYS_setuid = 105; +pub const SYS_setgid = 106; +pub const SYS_geteuid = 107; +pub const SYS_getegid = 108; +pub const SYS_setpgid = 109; +pub const SYS_getppid = 110; +pub const SYS_getpgrp = 111; +pub const SYS_setsid = 112; +pub const SYS_setreuid = 113; +pub const SYS_setregid = 114; +pub const SYS_getgroups = 115; +pub const SYS_setgroups = 116; +pub const SYS_setresuid = 117; +pub const SYS_getresuid = 118; +pub const SYS_setresgid = 119; +pub const SYS_getresgid = 120; +pub const SYS_getpgid = 121; +pub const SYS_setfsuid = 122; +pub const SYS_setfsgid = 123; +pub const SYS_getsid = 124; +pub const SYS_capget = 125; +pub const SYS_capset = 126; +pub const SYS_rt_sigpending = 127; +pub const SYS_rt_sigtimedwait = 128; +pub const SYS_rt_sigqueueinfo = 129; +pub const SYS_rt_sigsuspend = 130; +pub const SYS_sigaltstack = 131; +pub const SYS_utime = 132; +pub const SYS_mknod = 133; +pub const SYS_uselib = 134; +pub const SYS_personality = 135; +pub const SYS_ustat = 136; +pub const SYS_statfs = 137; +pub const SYS_fstatfs = 138; +pub const SYS_sysfs = 139; +pub const SYS_getpriority = 140; +pub const SYS_setpriority = 141; +pub const SYS_sched_setparam = 142; +pub const SYS_sched_getparam = 143; +pub const SYS_sched_setscheduler = 144; +pub const SYS_sched_getscheduler = 145; +pub const SYS_sched_get_priority_max = 146; +pub const SYS_sched_get_priority_min = 147; +pub const SYS_sched_rr_get_interval = 148; +pub const SYS_mlock = 149; +pub const SYS_munlock = 150; +pub const SYS_mlockall = 151; +pub const SYS_munlockall = 152; +pub const SYS_vhangup = 153; +pub const SYS_modify_ldt = 154; +pub const SYS_pivot_root = 155; +pub const SYS__sysctl = 156; +pub const SYS_prctl = 157; +pub const SYS_arch_prctl = 158; +pub const SYS_adjtimex = 159; +pub const SYS_setrlimit = 160; +pub const SYS_chroot = 161; +pub const SYS_sync = 162; +pub const SYS_acct = 163; +pub const SYS_settimeofday = 164; +pub const SYS_mount = 165; +pub const SYS_umount2 = 166; +pub const SYS_swapon = 167; +pub const SYS_swapoff = 168; +pub const SYS_reboot = 169; +pub const SYS_sethostname = 170; +pub const SYS_setdomainname = 171; +pub const SYS_iopl = 172; +pub const SYS_ioperm = 173; +pub const SYS_create_module = 174; +pub const SYS_init_module = 175; +pub const SYS_delete_module = 176; +pub const SYS_get_kernel_syms = 177; +pub const SYS_query_module = 178; +pub const SYS_quotactl = 179; +pub const SYS_nfsservctl = 180; +pub const SYS_getpmsg = 181; +pub const SYS_putpmsg = 182; +pub const SYS_afs_syscall = 183; +pub const SYS_tuxcall = 184; +pub const SYS_security = 185; +pub const SYS_gettid = 186; +pub const SYS_readahead = 187; +pub const SYS_setxattr = 188; +pub const SYS_lsetxattr = 189; +pub const SYS_fsetxattr = 190; +pub const SYS_getxattr = 191; +pub const SYS_lgetxattr = 192; +pub const SYS_fgetxattr = 193; +pub const SYS_listxattr = 194; +pub const SYS_llistxattr = 195; +pub const SYS_flistxattr = 196; +pub const SYS_removexattr = 197; +pub const SYS_lremovexattr = 198; +pub const SYS_fremovexattr = 199; +pub const SYS_tkill = 200; +pub const SYS_time = 201; +pub const SYS_futex = 202; +pub const SYS_sched_setaffinity = 203; +pub const SYS_sched_getaffinity = 204; +pub const SYS_set_thread_area = 205; +pub const SYS_io_setup = 206; +pub const SYS_io_destroy = 207; +pub const SYS_io_getevents = 208; +pub const SYS_io_submit = 209; +pub const SYS_io_cancel = 210; +pub const SYS_get_thread_area = 211; +pub const SYS_lookup_dcookie = 212; +pub const SYS_epoll_create = 213; +pub const SYS_epoll_ctl_old = 214; +pub const SYS_epoll_wait_old = 215; +pub const SYS_remap_file_pages = 216; +pub const SYS_getdents64 = 217; +pub const SYS_set_tid_address = 218; +pub const SYS_restart_syscall = 219; +pub const SYS_semtimedop = 220; +pub const SYS_fadvise64 = 221; +pub const SYS_timer_create = 222; +pub const SYS_timer_settime = 223; +pub const SYS_timer_gettime = 224; +pub const SYS_timer_getoverrun = 225; +pub const SYS_timer_delete = 226; +pub const SYS_clock_settime = 227; +pub const SYS_clock_gettime = 228; +pub const SYS_clock_getres = 229; +pub const SYS_clock_nanosleep = 230; +pub const SYS_exit_group = 231; +pub const SYS_epoll_wait = 232; +pub const SYS_epoll_ctl = 233; +pub const SYS_tgkill = 234; +pub const SYS_utimes = 235; +pub const SYS_vserver = 236; +pub const SYS_mbind = 237; +pub const SYS_set_mempolicy = 238; +pub const SYS_get_mempolicy = 239; +pub const SYS_mq_open = 240; +pub const SYS_mq_unlink = 241; +pub const SYS_mq_timedsend = 242; +pub const SYS_mq_timedreceive = 243; +pub const SYS_mq_notify = 244; +pub const SYS_mq_getsetattr = 245; +pub const SYS_kexec_load = 246; +pub const SYS_waitid = 247; +pub const SYS_add_key = 248; +pub const SYS_request_key = 249; +pub const SYS_keyctl = 250; +pub const SYS_ioprio_set = 251; +pub const SYS_ioprio_get = 252; +pub const SYS_inotify_init = 253; +pub const SYS_inotify_add_watch = 254; +pub const SYS_inotify_rm_watch = 255; +pub const SYS_migrate_pages = 256; +pub const SYS_openat = 257; +pub const SYS_mkdirat = 258; +pub const SYS_mknodat = 259; +pub const SYS_fchownat = 260; +pub const SYS_futimesat = 261; +pub const SYS_newfstatat = 262; +pub const SYS_unlinkat = 263; +pub const SYS_renameat = 264; +pub const SYS_linkat = 265; +pub const SYS_symlinkat = 266; +pub const SYS_readlinkat = 267; +pub const SYS_fchmodat = 268; +pub const SYS_faccessat = 269; +pub const SYS_pselect6 = 270; +pub const SYS_ppoll = 271; +pub const SYS_unshare = 272; +pub const SYS_set_robust_list = 273; +pub const SYS_get_robust_list = 274; +pub const SYS_splice = 275; +pub const SYS_tee = 276; +pub const SYS_sync_file_range = 277; +pub const SYS_vmsplice = 278; +pub const SYS_move_pages = 279; +pub const SYS_utimensat = 280; +pub const SYS_epoll_pwait = 281; +pub const SYS_signalfd = 282; +pub const SYS_timerfd_create = 283; +pub const SYS_eventfd = 284; +pub const SYS_fallocate = 285; +pub const SYS_timerfd_settime = 286; +pub const SYS_timerfd_gettime = 287; +pub const SYS_accept4 = 288; +pub const SYS_signalfd4 = 289; +pub const SYS_eventfd2 = 290; +pub const SYS_epoll_create1 = 291; +pub const SYS_dup3 = 292; +pub const SYS_pipe2 = 293; +pub const SYS_inotify_init1 = 294; +pub const SYS_preadv = 295; +pub const SYS_pwritev = 296; +pub const SYS_rt_tgsigqueueinfo = 297; +pub const SYS_perf_event_open = 298; +pub const SYS_recvmmsg = 299; +pub const SYS_fanotify_init = 300; +pub const SYS_fanotify_mark = 301; +pub const SYS_prlimit64 = 302; +pub const SYS_name_to_handle_at = 303; +pub const SYS_open_by_handle_at = 304; +pub const SYS_clock_adjtime = 305; +pub const SYS_syncfs = 306; +pub const SYS_sendmmsg = 307; +pub const SYS_setns = 308; +pub const SYS_getcpu = 309; +pub const SYS_process_vm_readv = 310; +pub const SYS_process_vm_writev = 311; +pub const SYS_kcmp = 312; +pub const SYS_finit_module = 313; +pub const SYS_sched_setattr = 314; +pub const SYS_sched_getattr = 315; +pub const SYS_renameat2 = 316; +pub const SYS_seccomp = 317; +pub const SYS_getrandom = 318; +pub const SYS_memfd_create = 319; +pub const SYS_kexec_file_load = 320; +pub const SYS_bpf = 321; +pub const SYS_execveat = 322; +pub const SYS_userfaultfd = 323; +pub const SYS_membarrier = 324; +pub const SYS_mlock2 = 325; + +pub const O_CREAT = 0o100; +pub const O_EXCL = 0o200; +pub const O_NOCTTY = 0o400; +pub const O_TRUNC = 0o1000; +pub const O_APPEND = 0o2000; +pub const O_NONBLOCK = 0o4000; +pub const O_DSYNC = 0o10000; +pub const O_SYNC = 0o4010000; +pub const O_RSYNC = 0o4010000; +pub const O_DIRECTORY = 0o200000; +pub const O_NOFOLLOW = 0o400000; +pub const O_CLOEXEC = 0o2000000; + +pub const O_ASYNC = 0o20000; +pub const O_DIRECT = 0o40000; +pub const O_LARGEFILE = 0; +pub const O_NOATIME = 0o1000000; +pub const O_PATH = 0o10000000; +pub const O_TMPFILE = 0o20200000; +pub const O_NDELAY = O_NONBLOCK; + +pub const F_DUPFD = 0; +pub const F_GETFD = 1; +pub const F_SETFD = 2; +pub const F_GETFL = 3; +pub const F_SETFL = 4; + +pub const F_SETOWN = 8; +pub const F_GETOWN = 9; +pub const F_SETSIG = 10; +pub const F_GETSIG = 11; + +pub const F_GETLK = 5; +pub const F_SETLK = 6; +pub const F_SETLKW = 7; + +pub const F_SETOWN_EX = 15; +pub const F_GETOWN_EX = 16; + +pub const F_GETOWNER_UIDS = 17; + +pub inline fn syscall0(number: usize) -> usize { + asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number) + : "rcx", "r11") +} + +pub inline fn syscall1(number: usize, arg1: usize) -> usize { + asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1) + : "rcx", "r11") +} + +pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) -> usize { + asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1), + [arg2] "{rsi}" (arg2) + : "rcx", "r11") +} + +pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) -> usize { + asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1), + [arg2] "{rsi}" (arg2), + [arg3] "{rdx}" (arg3) + : "rcx", "r11") +} + +pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) -> usize { + asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1), + [arg2] "{rsi}" (arg2), + [arg3] "{rdx}" (arg3), + [arg4] "{r10}" (arg4) + : "rcx", "r11") +} + +pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) -> usize { + asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1), + [arg2] "{rsi}" (arg2), + [arg3] "{rdx}" (arg3), + [arg4] "{r10}" (arg4), + [arg5] "{r8}" (arg5) + : "rcx", "r11") +} + +pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, + arg5: usize, arg6: usize) -> usize +{ + asm volatile ("syscall" + : [ret] "={rax}" (-> usize) + : [number] "{rax}" (number), + [arg1] "{rdi}" (arg1), + [arg2] "{rsi}" (arg2), + [arg3] "{rdx}" (arg3), + [arg4] "{r10}" (arg4), + [arg5] "{r8}" (arg5), + [arg6] "{r9}" (arg6) + : "rcx", "r11") +} + +pub const msghdr = extern struct { + msg_name: &u8, + msg_namelen: socklen_t, + msg_iov: &iovec, + msg_iovlen: i32, + __pad1: i32, + msg_control: &u8, + msg_controllen: socklen_t, + __pad2: socklen_t, + msg_flags: i32, +}; + +pub const stat = extern struct { + dev: u64, + ino: u64, + nlink: usize, + + mode: u32, + uid: u32, + gid: u32, + __pad0: u32, + rdev: u64, + size: i64, + blksize: isize, + blocks: i64, + + atim: timespec, + mtim: timespec, + ctim: timespec, + __unused: [3]isize, +}; + +pub const timespec = extern struct { + tv_sec: isize, + tv_nsec: isize, +}; diff --git a/std/os/windows.zig b/std/os/windows.zig new file mode 100644 index 0000000000000000000000000000000000000000..f713f61abea3be338404f19cf9e44863d6578cfa --- /dev/null +++ b/std/os/windows.zig @@ -0,0 +1,17 @@ +pub extern fn CryptAcquireContext(phProv: &HCRYPTPROV, pszContainer: LPCTSTR, + pszProvider: LPCTSTR, dwProvType: DWORD, dwFlags: DWORD) -> bool; + +pub extern fn CryptReleaseContext(hProv: HCRYPTPROV, dwFlags: DWORD) -> bool; + +pub extern fn CryptGenRandom(hProv: HCRYPTPROV, dwLen: DWORD, pbBuffer: &BYTE) -> bool; + +pub const PROV_RSA_FULL = 1; + + +pub const BYTE = u8; +pub const DWORD = u32; +// TODO something about unicode WCHAR vs char +pub const TCHAR = u8; +pub const LPCTSTR = ?&const TCHAR; +pub const ULONG_PTR = usize; +pub const HCRYPTPROV = ULONG_PTR; diff --git a/std/panic.zig b/std/panic.zig deleted file mode 100644 index 9b58a4d9c8230f8e0a70383bdc7bf6f1700d6d97..0000000000000000000000000000000000000000 --- a/std/panic.zig +++ /dev/null @@ -1,12 +0,0 @@ -// This file is included if and only if the user's main source file does not -// include a public panic function. -// If this file wants to import other files *by name*, support for that would -// have to be added in the compiler. - -pub coldcc fn panic(message: []const u8) -> noreturn { - if (@compileVar("os") == Os.freestanding) { - while (true) {} - } else { - @import("std").debug.panic(message); - } -} diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig new file mode 100644 index 0000000000000000000000000000000000000000..c9a36a07037702f9261a4e7948bded527abb10d1 --- /dev/null +++ b/std/special/bootstrap.zig @@ -0,0 +1,59 @@ +// This file is in a package which has the root source file exposed as "@root". +// It is included in the compilation unit when exporting an executable. + +const root = @import("@root"); +const std = @import("std"); + +const want_main_symbol = std.target.linking_libc; +const want_start_symbol = !want_main_symbol; + +const exit = std.os.posix.exit; + +var argc: usize = undefined; +var argv: &&u8 = undefined; + +export nakedcc fn _start() -> noreturn { + @setFnVisible(this, want_start_symbol); + if (!want_start_symbol) { + unreachable; + } + + switch (@compileVar("arch")) { + Arch.x86_64 => { + argc = asm("mov %[argc], [rsp]": [argc] "=r" (-> usize)); + argv = asm("lea %[argv], [rsp + 8h]": [argv] "=r" (-> &&u8)); + }, + Arch.i386 => { + argc = asm("mov %[argc], [esp]": [argc] "=r" (-> usize)); + argv = asm("lea %[argv], [esp + 4h]": [argv] "=r" (-> &&u8)); + }, + else => @compileError("unsupported arch"), + } + callMainAndExit() +} + +fn callMain() -> %void { + const args = @alloca([]u8, argc); + for (args) |_, i| { + const ptr = argv[i]; + args[i] = ptr[0...std.cstr.len(ptr)]; + } + return root.main(args); +} + +fn callMainAndExit() -> noreturn { + callMain() %% exit(1); + exit(0); +} + +export fn main(c_argc: i32, c_argv: &&u8) -> i32 { + @setFnVisible(this, want_main_symbol); + if (!want_main_symbol) { + unreachable; + } + + argc = usize(c_argc); + argv = c_argv; + callMain() %% return 1; + return 0; +} diff --git a/std/special/builtin.zig b/std/special/builtin.zig new file mode 100644 index 0000000000000000000000000000000000000000..136cbd9da4f7576fa5cfaba04b257996e23e3779 --- /dev/null +++ b/std/special/builtin.zig @@ -0,0 +1,36 @@ +// These functions are provided when not linking against libc because LLVM +// sometimes generates code that calls them. + +// Note that these functions do not return `dest`, like the libc API. +// The semantics of these functions is dictated by the corresponding +// LLVM intrinsics, not by the libc API. + +export fn memset(dest: ?&u8, c: u8, n: usize) { + @setDebugSafety(this, false); + + if (n == 0) + return; + + const d = ??dest; + var index: usize = 0; + while (index != n; index += 1) + d[index] = c; +} + +export fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) { + @setDebugSafety(this, false); + + if (n == 0) + return; + + const d = ??dest; + const s = ??src; + var index: usize = 0; + while (index != n; index += 1) + d[index] = s[index]; +} + +// Avoid dragging in the debug safety mechanisms into this .o file. +pub fn panic(message: []const u8) -> noreturn { + unreachable; +} diff --git a/std/special/compiler_rt.zig b/std/special/compiler_rt.zig new file mode 100644 index 0000000000000000000000000000000000000000..f6e4c021d15e6459bf5cda1b4cef88d7bb528484 --- /dev/null +++ b/std/special/compiler_rt.zig @@ -0,0 +1,515 @@ +// Avoid dragging in the debug safety mechanisms into this .o file, +// unless we're trying to test this file. +pub fn panic(message: []const u8) -> noreturn { + if (@compileVar("is_test")) { + @import("std").debug.panic(message); + } else { + unreachable; + } +} + +const CHAR_BIT = 8; +const du_int = u64; +const di_int = i64; +const si_int = c_int; +const su_int = c_uint; + +const udwords = [2]su_int; +const low = if (@compileVar("is_big_endian")) 1 else 0; +const high = 1 - low; + +export fn __udivdi3(a: du_int, b: du_int) -> du_int { + @setDebugSafety(this, false); + return __udivmoddi4(a, b, null); +} + +fn du_int_to_udwords(x: du_int) -> udwords { + @setDebugSafety(this, false); + return *(&udwords)(&x); +} + +export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int { + @setDebugSafety(this, false); + + const n_uword_bits = @sizeOf(su_int) * CHAR_BIT; + const n_udword_bits = @sizeOf(du_int) * CHAR_BIT; + var n = du_int_to_udwords(a); + var d = du_int_to_udwords(b); + var q: udwords = undefined; + var r: udwords = undefined; + var sr: c_uint = undefined; + // special cases, X is unknown, K != 0 + if (n[high] == 0) { + if (d[high] == 0) { + // 0 X + // --- + // 0 X + if (const rem ?= maybe_rem) { + *rem = n[low] % d[low]; + } + return n[low] / d[low]; + } + // 0 X + // --- + // K X + if (const rem ?= maybe_rem) { + *rem = n[low]; + } + return 0; + } + // n[high] != 0 + if (d[low] == 0) { + if (d[high] == 0) { + // K X + // --- + // 0 0 + if (var rem ?= maybe_rem) { + *rem = n[high] % d[low]; + } + return n[high] / d[low]; + } + // d[high] != 0 + if (n[low] == 0) { + // K 0 + // --- + // K 0 + if (var rem ?= maybe_rem) { + r[high] = n[high] % d[high]; + r[low] = 0; + *rem = *(&du_int)(&r[0]); + } + return n[high] / d[high]; + } + // K K + // --- + // K 0 + // if d is a power of 2 + if ((d[high] & (d[high] - 1)) == 0) { + if (var rem ?= maybe_rem) { + r[low] = n[low]; + r[high] = n[high] & (d[high] - 1); + *rem = *(&du_int)(&r[0]); + } + return n[high] >> @ctz(d[high]); + } + // K K + // --- + // K 0 + sr = @clz(su_int(d[high])) - @clz(su_int(n[high])); + // 0 <= sr <= n_uword_bits - 2 or sr large + if (sr > n_uword_bits - 2) { + if (var rem ?= maybe_rem) { + *rem = *(&du_int)(&n[0]); + } + return 0; + } + sr += 1; + // 1 <= sr <= n_uword_bits - 1 + // q.all = n.all << (n_udword_bits - sr); + q[low] = 0; + q[high] = n[low] << (n_uword_bits - sr); + // r.all = n.all >> sr; + r[high] = n[high] >> sr; + r[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr); + } else { + // d[low] != 0 + if (d[high] == 0) { + // K X + // --- + // 0 K + // if d is a power of 2 + if ((d[low] & (d[low] - 1)) == 0) { + if (var rem ?= maybe_rem) { + *rem = n[low] & (d[low] - 1); + } + if (d[low] == 1) { + return *(&du_int)(&n[0]); + } + sr = @ctz(d[low]); + q[high] = n[high] >> sr; + q[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr); + return *(&du_int)(&q[0]); + } + // K X + // --- + // 0 K + sr = 1 + n_uword_bits + @clz(su_int(d[low])) - @clz(su_int(n[high])); + // 2 <= sr <= n_udword_bits - 1 + // q.all = n.all << (n_udword_bits - sr); + // r.all = n.all >> sr; + if (sr == n_uword_bits) { + q[low] = 0; + q[high] = n[low]; + r[high] = 0; + r[low] = n[high]; + } else if (sr < n_uword_bits) { + // 2 <= sr <= n_uword_bits - 1 + q[low] = 0; + q[high] = n[low] << (n_uword_bits - sr); + r[high] = n[high] >> sr; + r[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr); + } else { + // n_uword_bits + 1 <= sr <= n_udword_bits - 1 + q[low] = n[low] << (n_udword_bits - sr); + q[high] = (n[high] << (n_udword_bits - sr)) | + (n[low] >> (sr - n_uword_bits)); + r[high] = 0; + r[low] = n[high] >> (sr - n_uword_bits); + } + } else { + // K X + // --- + // K K + sr = @clz(su_int(d[high])) - @clz(su_int(n[high])); + // 0 <= sr <= n_uword_bits - 1 or sr large + if (sr > n_uword_bits - 1) { + if (var rem ?= maybe_rem) { + *rem = *(&du_int)(&n[0]); + } + return 0; + } + sr += 1; + // 1 <= sr <= n_uword_bits + // q.all = n.all << (n_udword_bits - sr); + q[low] = 0; + if (sr == n_uword_bits) { + q[high] = n[low]; + r[high] = 0; + r[low] = n[high]; + } else { + q[high] = n[low] << (n_uword_bits - sr); + r[high] = n[high] >> sr; + r[low] = (n[high] << (n_uword_bits - sr)) | (n[low] >> sr); + } + } + } + // Not a special case + // q and r are initialized with: + // q.all = n.all << (n_udword_bits - sr); + // r.all = n.all >> sr; + // 1 <= sr <= n_udword_bits - 1 + var carry: su_int = 0; + while (sr > 0) { + // r:q = ((r:q) << 1) | carry + r[high] = (r[high] << 1) | (r[low] >> (n_uword_bits - 1)); + r[low] = (r[low] << 1) | (q[high] >> (n_uword_bits - 1)); + q[high] = (q[high] << 1) | (q[low] >> (n_uword_bits - 1)); + q[low] = (q[low] << 1) | carry; + // carry = 0; + // if (r.all >= d.all) + // { + // r.all -= d.all; + // carry = 1; + // } + const s: di_int = (di_int)(*(&du_int)(&d[0]) - *(&du_int)(&r[0]) - 1) >> (n_udword_bits - 1); + carry = su_int(s & 1); + *(&du_int)(&r[0]) -= *(&du_int)(&d[0]) & u64(s); + + sr -= 1; + } + *(&du_int)(&q[0]) = (*(&du_int)(&q[0]) << 1) | u64(carry); + if (var rem ?= maybe_rem) { + *rem = *(&du_int)(&r[0]); + } + return *(&du_int)(&q[0]); +} + +export fn __umoddi3(a: du_int, b: du_int) -> du_int { + @setDebugSafety(this, false); + + var r: du_int = undefined; + _ = __udivmoddi4(a, b, &r); + return r; +} + +fn isArmArch() -> bool { + return switch (@compileVar("arch")) { + Arch.armv8_2a, + Arch.armv8_1a, + Arch.armv8, + Arch.armv8m_baseline, + Arch.armv8m_mainline, + Arch.armv7, + Arch.armv7em, + Arch.armv7m, + Arch.armv7s, + Arch.armv7k, + Arch.armv6, + Arch.armv6m, + Arch.armv6k, + Arch.armv6t2, + Arch.armv5, + Arch.armv5te, + Arch.armv4t, + Arch.armeb => true, + else => false, + }; +} + +export nakedcc fn __aeabi_uidivmod() { + @setDebugSafety(this, false); + + if (comptime isArmArch()) { + asm volatile ( + \\ push { lr } + \\ sub sp, sp, #4 + \\ mov r2, sp + \\ bl __udivmodsi4 + \\ ldr r1, [sp] + \\ add sp, sp, #4 + \\ pop { pc } + ::: "r2", "r1"); + unreachable; + } + + @setFnVisible(this, false); +} + +export fn __udivmodsi4(a: su_int, b: su_int, rem: &su_int) -> su_int { + @setDebugSafety(this, false); + + const d = __udivsi3(a, b); + *rem = su_int(si_int(a) -% (si_int(d) * si_int(b))); + return d; +} + + +// TODO make this an alias instead of an extra function call +// https://github.com/andrewrk/zig/issues/256 + +export fn __aeabi_uidiv(n: su_int, d: su_int) -> su_int { + @setDebugSafety(this, false); + + return __udivsi3(n, d); +} + +export fn __udivsi3(n: su_int, d: su_int) -> su_int { + @setDebugSafety(this, false); + + const n_uword_bits: c_uint = @sizeOf(su_int) * CHAR_BIT; + // special cases + if (d == 0) + return 0; // ?! + if (n == 0) + return 0; + var sr: c_uint = @clz(d) - @clz(n); + // 0 <= sr <= n_uword_bits - 1 or sr large + if (sr > n_uword_bits - 1) // d > r + return 0; + if (sr == n_uword_bits - 1) // d == 1 + return n; + sr += 1; + // 1 <= sr <= n_uword_bits - 1 + // Not a special case + var q: su_int = n << (n_uword_bits - sr); + var r: su_int = n >> sr; + var carry: su_int = 0; + while (sr > 0; sr -= 1) { + // r:q = ((r:q) << 1) | carry + r = (r << 1) | (q >> (n_uword_bits - 1)); + q = (q << 1) | carry; + // carry = 0; + // if (r.all >= d.all) + // { + // r.all -= d.all; + // carry = 1; + // } + const s = si_int(d - r - 1) >> si_int(n_uword_bits - 1); + carry = su_int(s & 1); + r -= d & su_int(s); + } + q = (q << 1) | carry; + return q; +} + +test "test_umoddi3" { + test_one_umoddi3(0, 1, 0); + test_one_umoddi3(2, 1, 0); + test_one_umoddi3(0x8000000000000000, 1, 0x0); + test_one_umoddi3(0x8000000000000000, 2, 0x0); + test_one_umoddi3(0xFFFFFFFFFFFFFFFF, 2, 0x1); +} + +fn test_one_umoddi3(a: du_int, b: du_int, expected_r: du_int) { + const r = __umoddi3(a, b); + assert(r == expected_r); +} + +test "test_udivmoddi4" { + const cases = [][4]du_int { + []du_int{0x0000000000000000, 0x0000000000000001, 0x0000000000000000, 0x0000000000000000}, + []du_int{0x0000000080000000, 0x0000000100000001, 0x0000000000000000, 0x0000000080000000}, + []du_int{0x7FFFFFFF00000001, 0x0000000000000001, 0x7FFFFFFF00000001, 0x0000000000000000}, + []du_int{0x7FFFFFFF7FFFFFFF, 0xFFFFFFFFFFFFFFFF, 0x0000000000000000, 0x7FFFFFFF7FFFFFFF}, + []du_int{0x8000000000000002, 0xFFFFFFFFFFFFFFFE, 0x0000000000000000, 0x8000000000000002}, + []du_int{0x80000000FFFFFFFD, 0xFFFFFFFFFFFFFFFD, 0x0000000000000000, 0x80000000FFFFFFFD}, + []du_int{0xFFFFFFFD00000010, 0xFFFFFFFF80000000, 0x0000000000000000, 0xFFFFFFFD00000010}, + []du_int{0xFFFFFFFDFFFFFFFF, 0xFFFFFFFF7FFFFFFF, 0x0000000000000000, 0xFFFFFFFDFFFFFFFF}, + []du_int{0xFFFFFFFE0747AE14, 0xFFFFFFFF0747AE14, 0x0000000000000000, 0xFFFFFFFE0747AE14}, + []du_int{0xFFFFFFFF00000001, 0xFFFFFFFF078644FA, 0x0000000000000000, 0xFFFFFFFF00000001}, + []du_int{0xFFFFFFFF80000000, 0xFFFFFFFF00000010, 0x0000000000000001, 0x000000007FFFFFF0}, + []du_int{0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0x0000000000000001, 0x0000000000000000}, + }; + + for (cases) |case| { + test_one_udivmoddi4(case[0], case[1], case[2], case[3]); + } +} + +fn test_one_udivmoddi4(a: du_int, b: du_int, expected_q: du_int, expected_r: du_int) { + var r: du_int = undefined; + const q = __udivmoddi4(a, b, &r); + assert(q == expected_q); + assert(r == expected_r); +} + +test "test_udivsi3" { + const cases = [][3]su_int { + []su_int{0x00000000, 0x00000001, 0x00000000}, + []su_int{0x00000000, 0x00000002, 0x00000000}, + []su_int{0x00000000, 0x00000003, 0x00000000}, + []su_int{0x00000000, 0x00000010, 0x00000000}, + []su_int{0x00000000, 0x078644FA, 0x00000000}, + []su_int{0x00000000, 0x0747AE14, 0x00000000}, + []su_int{0x00000000, 0x7FFFFFFF, 0x00000000}, + []su_int{0x00000000, 0x80000000, 0x00000000}, + []su_int{0x00000000, 0xFFFFFFFD, 0x00000000}, + []su_int{0x00000000, 0xFFFFFFFE, 0x00000000}, + []su_int{0x00000000, 0xFFFFFFFF, 0x00000000}, + []su_int{0x00000001, 0x00000001, 0x00000001}, + []su_int{0x00000001, 0x00000002, 0x00000000}, + []su_int{0x00000001, 0x00000003, 0x00000000}, + []su_int{0x00000001, 0x00000010, 0x00000000}, + []su_int{0x00000001, 0x078644FA, 0x00000000}, + []su_int{0x00000001, 0x0747AE14, 0x00000000}, + []su_int{0x00000001, 0x7FFFFFFF, 0x00000000}, + []su_int{0x00000001, 0x80000000, 0x00000000}, + []su_int{0x00000001, 0xFFFFFFFD, 0x00000000}, + []su_int{0x00000001, 0xFFFFFFFE, 0x00000000}, + []su_int{0x00000001, 0xFFFFFFFF, 0x00000000}, + []su_int{0x00000002, 0x00000001, 0x00000002}, + []su_int{0x00000002, 0x00000002, 0x00000001}, + []su_int{0x00000002, 0x00000003, 0x00000000}, + []su_int{0x00000002, 0x00000010, 0x00000000}, + []su_int{0x00000002, 0x078644FA, 0x00000000}, + []su_int{0x00000002, 0x0747AE14, 0x00000000}, + []su_int{0x00000002, 0x7FFFFFFF, 0x00000000}, + []su_int{0x00000002, 0x80000000, 0x00000000}, + []su_int{0x00000002, 0xFFFFFFFD, 0x00000000}, + []su_int{0x00000002, 0xFFFFFFFE, 0x00000000}, + []su_int{0x00000002, 0xFFFFFFFF, 0x00000000}, + []su_int{0x00000003, 0x00000001, 0x00000003}, + []su_int{0x00000003, 0x00000002, 0x00000001}, + []su_int{0x00000003, 0x00000003, 0x00000001}, + []su_int{0x00000003, 0x00000010, 0x00000000}, + []su_int{0x00000003, 0x078644FA, 0x00000000}, + []su_int{0x00000003, 0x0747AE14, 0x00000000}, + []su_int{0x00000003, 0x7FFFFFFF, 0x00000000}, + []su_int{0x00000003, 0x80000000, 0x00000000}, + []su_int{0x00000003, 0xFFFFFFFD, 0x00000000}, + []su_int{0x00000003, 0xFFFFFFFE, 0x00000000}, + []su_int{0x00000003, 0xFFFFFFFF, 0x00000000}, + []su_int{0x00000010, 0x00000001, 0x00000010}, + []su_int{0x00000010, 0x00000002, 0x00000008}, + []su_int{0x00000010, 0x00000003, 0x00000005}, + []su_int{0x00000010, 0x00000010, 0x00000001}, + []su_int{0x00000010, 0x078644FA, 0x00000000}, + []su_int{0x00000010, 0x0747AE14, 0x00000000}, + []su_int{0x00000010, 0x7FFFFFFF, 0x00000000}, + []su_int{0x00000010, 0x80000000, 0x00000000}, + []su_int{0x00000010, 0xFFFFFFFD, 0x00000000}, + []su_int{0x00000010, 0xFFFFFFFE, 0x00000000}, + []su_int{0x00000010, 0xFFFFFFFF, 0x00000000}, + []su_int{0x078644FA, 0x00000001, 0x078644FA}, + []su_int{0x078644FA, 0x00000002, 0x03C3227D}, + []su_int{0x078644FA, 0x00000003, 0x028216FE}, + []su_int{0x078644FA, 0x00000010, 0x0078644F}, + []su_int{0x078644FA, 0x078644FA, 0x00000001}, + []su_int{0x078644FA, 0x0747AE14, 0x00000001}, + []su_int{0x078644FA, 0x7FFFFFFF, 0x00000000}, + []su_int{0x078644FA, 0x80000000, 0x00000000}, + []su_int{0x078644FA, 0xFFFFFFFD, 0x00000000}, + []su_int{0x078644FA, 0xFFFFFFFE, 0x00000000}, + []su_int{0x078644FA, 0xFFFFFFFF, 0x00000000}, + []su_int{0x0747AE14, 0x00000001, 0x0747AE14}, + []su_int{0x0747AE14, 0x00000002, 0x03A3D70A}, + []su_int{0x0747AE14, 0x00000003, 0x026D3A06}, + []su_int{0x0747AE14, 0x00000010, 0x00747AE1}, + []su_int{0x0747AE14, 0x078644FA, 0x00000000}, + []su_int{0x0747AE14, 0x0747AE14, 0x00000001}, + []su_int{0x0747AE14, 0x7FFFFFFF, 0x00000000}, + []su_int{0x0747AE14, 0x80000000, 0x00000000}, + []su_int{0x0747AE14, 0xFFFFFFFD, 0x00000000}, + []su_int{0x0747AE14, 0xFFFFFFFE, 0x00000000}, + []su_int{0x0747AE14, 0xFFFFFFFF, 0x00000000}, + []su_int{0x7FFFFFFF, 0x00000001, 0x7FFFFFFF}, + []su_int{0x7FFFFFFF, 0x00000002, 0x3FFFFFFF}, + []su_int{0x7FFFFFFF, 0x00000003, 0x2AAAAAAA}, + []su_int{0x7FFFFFFF, 0x00000010, 0x07FFFFFF}, + []su_int{0x7FFFFFFF, 0x078644FA, 0x00000011}, + []su_int{0x7FFFFFFF, 0x0747AE14, 0x00000011}, + []su_int{0x7FFFFFFF, 0x7FFFFFFF, 0x00000001}, + []su_int{0x7FFFFFFF, 0x80000000, 0x00000000}, + []su_int{0x7FFFFFFF, 0xFFFFFFFD, 0x00000000}, + []su_int{0x7FFFFFFF, 0xFFFFFFFE, 0x00000000}, + []su_int{0x7FFFFFFF, 0xFFFFFFFF, 0x00000000}, + []su_int{0x80000000, 0x00000001, 0x80000000}, + []su_int{0x80000000, 0x00000002, 0x40000000}, + []su_int{0x80000000, 0x00000003, 0x2AAAAAAA}, + []su_int{0x80000000, 0x00000010, 0x08000000}, + []su_int{0x80000000, 0x078644FA, 0x00000011}, + []su_int{0x80000000, 0x0747AE14, 0x00000011}, + []su_int{0x80000000, 0x7FFFFFFF, 0x00000001}, + []su_int{0x80000000, 0x80000000, 0x00000001}, + []su_int{0x80000000, 0xFFFFFFFD, 0x00000000}, + []su_int{0x80000000, 0xFFFFFFFE, 0x00000000}, + []su_int{0x80000000, 0xFFFFFFFF, 0x00000000}, + []su_int{0xFFFFFFFD, 0x00000001, 0xFFFFFFFD}, + []su_int{0xFFFFFFFD, 0x00000002, 0x7FFFFFFE}, + []su_int{0xFFFFFFFD, 0x00000003, 0x55555554}, + []su_int{0xFFFFFFFD, 0x00000010, 0x0FFFFFFF}, + []su_int{0xFFFFFFFD, 0x078644FA, 0x00000022}, + []su_int{0xFFFFFFFD, 0x0747AE14, 0x00000023}, + []su_int{0xFFFFFFFD, 0x7FFFFFFF, 0x00000001}, + []su_int{0xFFFFFFFD, 0x80000000, 0x00000001}, + []su_int{0xFFFFFFFD, 0xFFFFFFFD, 0x00000001}, + []su_int{0xFFFFFFFD, 0xFFFFFFFE, 0x00000000}, + []su_int{0xFFFFFFFD, 0xFFFFFFFF, 0x00000000}, + []su_int{0xFFFFFFFE, 0x00000001, 0xFFFFFFFE}, + []su_int{0xFFFFFFFE, 0x00000002, 0x7FFFFFFF}, + []su_int{0xFFFFFFFE, 0x00000003, 0x55555554}, + []su_int{0xFFFFFFFE, 0x00000010, 0x0FFFFFFF}, + []su_int{0xFFFFFFFE, 0x078644FA, 0x00000022}, + []su_int{0xFFFFFFFE, 0x0747AE14, 0x00000023}, + []su_int{0xFFFFFFFE, 0x7FFFFFFF, 0x00000002}, + []su_int{0xFFFFFFFE, 0x80000000, 0x00000001}, + []su_int{0xFFFFFFFE, 0xFFFFFFFD, 0x00000001}, + []su_int{0xFFFFFFFE, 0xFFFFFFFE, 0x00000001}, + []su_int{0xFFFFFFFE, 0xFFFFFFFF, 0x00000000}, + []su_int{0xFFFFFFFF, 0x00000001, 0xFFFFFFFF}, + []su_int{0xFFFFFFFF, 0x00000002, 0x7FFFFFFF}, + []su_int{0xFFFFFFFF, 0x00000003, 0x55555555}, + []su_int{0xFFFFFFFF, 0x00000010, 0x0FFFFFFF}, + []su_int{0xFFFFFFFF, 0x078644FA, 0x00000022}, + []su_int{0xFFFFFFFF, 0x0747AE14, 0x00000023}, + []su_int{0xFFFFFFFF, 0x7FFFFFFF, 0x00000002}, + []su_int{0xFFFFFFFF, 0x80000000, 0x00000001}, + []su_int{0xFFFFFFFF, 0xFFFFFFFD, 0x00000001}, + []su_int{0xFFFFFFFF, 0xFFFFFFFE, 0x00000001}, + []su_int{0xFFFFFFFF, 0xFFFFFFFF, 0x00000001}, + }; + + for (cases) |case| { + test_one_udivsi3(case[0], case[1], case[2]); + } +} + +fn test_one_udivsi3(a: su_int, b: su_int, expected_q: su_int) { + const q: su_int = __udivsi3(a, b); + assert(q == expected_q); +} + + +fn assert(ok: bool) { + if (!ok) unreachable; +} diff --git a/std/special/panic.zig b/std/special/panic.zig new file mode 100644 index 0000000000000000000000000000000000000000..9b58a4d9c8230f8e0a70383bdc7bf6f1700d6d97 --- /dev/null +++ b/std/special/panic.zig @@ -0,0 +1,12 @@ +// This file is included if and only if the user's main source file does not +// include a public panic function. +// If this file wants to import other files *by name*, support for that would +// have to be added in the compiler. + +pub coldcc fn panic(message: []const u8) -> noreturn { + if (@compileVar("os") == Os.freestanding) { + while (true) {} + } else { + @import("std").debug.panic(message); + } +} diff --git a/std/special/test_runner.zig b/std/special/test_runner.zig new file mode 100644 index 0000000000000000000000000000000000000000..f4948016710ebe0296ea0cbb3139602ada655fda --- /dev/null +++ b/std/special/test_runner.zig @@ -0,0 +1,18 @@ +const io = @import("std").io; + +const TestFn = struct { + name: []u8, + func: extern fn(), +}; + +extern var zig_test_fn_list: []TestFn; + +pub fn main(args: [][]u8) -> %void { + for (zig_test_fn_list) |testFn, i| { + %%io.stderr.printf("Test {}/{} {}...", i + 1, zig_test_fn_list.len, testFn.name); + + testFn.func(); + + %%io.stderr.printf("OK\n"); + } +} diff --git a/std/target.zig b/std/target.zig new file mode 100644 index 0000000000000000000000000000000000000000..f6b6f7177de32c0751b1199eca975728957747cc --- /dev/null +++ b/std/target.zig @@ -0,0 +1,15 @@ +const mem = @import("mem.zig"); + +pub const linking_libc = linkingLibrary("c"); + +pub fn linkingLibrary(lib_name: []const u8) -> bool { + // TODO shouldn't need this if + if (@compileVar("link_libs").len != 0) { + for (@compileVar("link_libs")) |link_lib| { + if (mem.eql(u8, link_lib, lib_name)) { + return true; + } + } + } + return false; +} diff --git a/std/test_runner.zig b/std/test_runner.zig deleted file mode 100644 index f4948016710ebe0296ea0cbb3139602ada655fda..0000000000000000000000000000000000000000 --- a/std/test_runner.zig +++ /dev/null @@ -1,18 +0,0 @@ -const io = @import("std").io; - -const TestFn = struct { - name: []u8, - func: extern fn(), -}; - -extern var zig_test_fn_list: []TestFn; - -pub fn main(args: [][]u8) -> %void { - for (zig_test_fn_list) |testFn, i| { - %%io.stderr.printf("Test {}/{} {}...", i + 1, zig_test_fn_list.len, testFn.name); - - testFn.func(); - - %%io.stderr.printf("OK\n"); - } -} diff --git a/std/windows.zig b/std/windows.zig deleted file mode 100644 index f713f61abea3be338404f19cf9e44863d6578cfa..0000000000000000000000000000000000000000 --- a/std/windows.zig +++ /dev/null @@ -1,17 +0,0 @@ -pub extern fn CryptAcquireContext(phProv: &HCRYPTPROV, pszContainer: LPCTSTR, - pszProvider: LPCTSTR, dwProvType: DWORD, dwFlags: DWORD) -> bool; - -pub extern fn CryptReleaseContext(hProv: HCRYPTPROV, dwFlags: DWORD) -> bool; - -pub extern fn CryptGenRandom(hProv: HCRYPTPROV, dwLen: DWORD, pbBuffer: &BYTE) -> bool; - -pub const PROV_RSA_FULL = 1; - - -pub const BYTE = u8; -pub const DWORD = u32; -// TODO something about unicode WCHAR vs char -pub const TCHAR = u8; -pub const LPCTSTR = ?&const TCHAR; -pub const ULONG_PTR = usize; -pub const HCRYPTPROV = ULONG_PTR; diff --git a/test/cases/import.zig b/test/cases/import.zig index b5bbbc211423663233d0508decf0e305d6ab67e8..d931d836b8cb6413a0e5bf06bc38cd74e88d5e0e 100644 --- a/test/cases/import.zig +++ b/test/cases/import.zig @@ -1,5 +1,5 @@ const assert = @import("std").debug.assert; -const a_namespace = @import("cases/import/a_namespace.zig"); +const a_namespace = @import("import/a_namespace.zig"); test "callFnViaNamespaceLookup" { assert(a_namespace.foo() == 1234); diff --git a/test/cases/namespace_depends_on_compile_var/index.zig b/test/cases/namespace_depends_on_compile_var/index.zig index 8c57d21946c79a7807ea04d94ce6b5b307485978..aa0748e07831d4976acbea75f9f1f4cc33a1d613 100644 --- a/test/cases/namespace_depends_on_compile_var/index.zig +++ b/test/cases/namespace_depends_on_compile_var/index.zig @@ -8,6 +8,6 @@ test "namespaceDependsOnCompileVar" { } } const some_namespace = switch(@compileVar("os")) { - Os.linux => @import("cases/namespace_depends_on_compile_var/a.zig"), - else => @import("cases/namespace_depends_on_compile_var/b.zig"), + Os.linux => @import("a.zig"), + else => @import("b.zig"), }; diff --git a/test/cases/pub_enum/index.zig b/test/cases/pub_enum/index.zig index 4935f210bac4bd01f8a640bb08f16bace8ec6b68..a5600f6de13be35bff97dba94a01d3113c82ae28 100644 --- a/test/cases/pub_enum/index.zig +++ b/test/cases/pub_enum/index.zig @@ -1,4 +1,4 @@ -const other = @import("cases/pub_enum/other.zig"); +const other = @import("other.zig"); const assert = @import("std").debug.assert; test "pubEnum" { diff --git a/test/run_tests.cpp b/test/run_tests.cpp index 8256881fea77e1cbee4ec3cac6eeb245b935327d..8f5acab6d7178166dd3dd99945c68140b36ff7b4 100644 --- a/test/run_tests.cpp +++ b/test/run_tests.cpp @@ -1254,8 +1254,7 @@ export fn entry() -> usize { @sizeOf(@typeOf(seventh_fib_number)) } const resource = @embedFile("bogus.txt"); export fn entry() -> usize { @sizeOf(@typeOf(resource)) } - )SOURCE", 1, ".tmp_source.zig:2:29: error: unable to find './bogus.txt'"); - + )SOURCE", 2, ".tmp_source.zig:2:29: error: unable to find '", "/bogus.txt'"); add_compile_fail_case("non-const expression in struct literal outside function", R"SOURCE( const Foo = struct {