authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-02 15:02:17-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-02 15:02:17-05:00
logad214c7aa00fc82b243eb38061f41588a0c58867
treeea2273fa1d318a167c15633d94d8e8a7fed1bc7d
parentdcbd5ad1553c5994353de47babc0e07e02153bd8
signaturelock-open Commit is signed but in an unrecognized format.

bring your own OS layer in the std lib

closes #3784

7 files changed, 55 insertions(+), 21 deletions(-)

lib/std/debug.zig+4-2
...@@ -796,6 +796,9 @@ pub const OpenSelfDebugInfoError = error{...@@ -796,6 +796,9 @@ pub const OpenSelfDebugInfoError = error{
796pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo {796pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo {
797 if (builtin.strip_debug_info)797 if (builtin.strip_debug_info)
798 return error.MissingDebugInfo;798 return error.MissingDebugInfo;
799 if (@hasDecl(root, "os") and @hasDecl(root.os, "debug") and @hasDecl(root.os.debug, "openSelfDebugInfo")) {
800 return noasync root.os.debug.openSelfDebugInfo(allocator);
801 }
799 if (builtin.os == .windows) {802 if (builtin.os == .windows) {
800 return noasync openSelfDebugInfoWindows(allocator);803 return noasync openSelfDebugInfoWindows(allocator);
801 }804 }
...@@ -1722,8 +1725,7 @@ pub const DebugInfo = switch (builtin.os) {...@@ -1722,8 +1725,7 @@ pub const DebugInfo = switch (builtin.os) {
1722 sect_contribs: []pdb.SectionContribEntry,1725 sect_contribs: []pdb.SectionContribEntry,
1723 modules: []Module,1726 modules: []Module,
1724 },1727 },
1725 .linux, .freebsd, .netbsd, .dragonfly => DwarfInfo,1728 else => DwarfInfo,
1726 else => @compileError("Unsupported OS"),
1727};1729};
17281730
1729const PcRange = struct {1731const PcRange = struct {
lib/std/event/loop.zig+1-1
...@@ -61,7 +61,7 @@ pub const Loop = struct {...@@ -61,7 +61,7 @@ pub const Loop = struct {
61 base: ResumeNode,61 base: ResumeNode,
62 completion_key: usize,62 completion_key: usize,
63 },63 },
64 else => @compileError("unsupported OS"),64 else => struct {},
65 };65 };
6666
67 const KEventFd = struct {67 const KEventFd = struct {
lib/std/heap.zig+3
...@@ -1,4 +1,5 @@...@@ -1,4 +1,5 @@
1const std = @import("std.zig");1const std = @import("std.zig");
2const root = @import("root");
2const debug = std.debug;3const debug = std.debug;
3const assert = debug.assert;4const assert = debug.assert;
4const testing = std.testing;5const testing = std.testing;
...@@ -35,6 +36,8 @@ fn cShrink(self: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new...@@ -35,6 +36,8 @@ fn cShrink(self: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new
35/// Thread-safe and lock-free.36/// Thread-safe and lock-free.
36pub const page_allocator = if (std.Target.current.isWasm())37pub const page_allocator = if (std.Target.current.isWasm())
37 &wasm_page_allocator_state38 &wasm_page_allocator_state
39else if (std.Target.current.getOs() == .freestanding)
40 root.os.heap.page_allocator
38else41else
39 &page_allocator_state;42 &page_allocator_state;
4043
lib/std/os.zig+9-2
...@@ -14,6 +14,7 @@...@@ -14,6 +14,7 @@
14// Note: The Zig standard library does not support POSIX thread cancellation, and14// Note: The Zig standard library does not support POSIX thread cancellation, and
15// in general EINTR is handled by trying again.15// in general EINTR is handled by trying again.
1616
17const root = @import("root");
17const std = @import("std.zig");18const std = @import("std.zig");
18const builtin = @import("builtin");19const builtin = @import("builtin");
19const assert = std.debug.assert;20const assert = std.debug.assert;
...@@ -48,8 +49,14 @@ test "" {...@@ -48,8 +49,14 @@ test "" {
48 _ = @import("os/test.zig");49 _ = @import("os/test.zig");
49}50}
5051
51/// When linking libc, this is the C API. Otherwise, it is the OS-specific system interface.52/// Applications can override the `system` API layer in their root source file.
52pub const system = if (builtin.link_libc) std.c else switch (builtin.os) {53/// Otherwise, when linking libc, this is the C API.
54/// When not linking libc, it is the OS-specific system interface.
55pub const system = if (@hasDecl(root, "os") and root.os != @This())
56 root.os.system
57else if (builtin.link_libc)
58 std.c
59else switch (builtin.os) {
53 .macosx, .ios, .watchos, .tvos => darwin,60 .macosx, .ios, .watchos, .tvos => darwin,
54 .freebsd => freebsd,61 .freebsd => freebsd,
55 .linux => linux,62 .linux => linux,
lib/std/os/bits.zig+7-2
...@@ -1,7 +1,10 @@...@@ -1,7 +1,10 @@
1// Platform-dependent types and values that are used along with OS-specific APIs.1//! Platform-dependent types and values that are used along with OS-specific APIs.
2// These are imported into `std.c`, `std.os`, and `std.os.linux`.2//! These are imported into `std.c`, `std.os`, and `std.os.linux`.
3//! Root source files can define `os.bits` and these will additionally be added
4//! to the namespace.
35
4const builtin = @import("builtin");6const builtin = @import("builtin");
7const root = @import("root");
58
6pub usingnamespace switch (builtin.os) {9pub usingnamespace switch (builtin.os) {
7 .macosx, .ios, .tvos, .watchos => @import("bits/darwin.zig"),10 .macosx, .ios, .tvos, .watchos => @import("bits/darwin.zig"),
...@@ -14,6 +17,8 @@ pub usingnamespace switch (builtin.os) {...@@ -14,6 +17,8 @@ pub usingnamespace switch (builtin.os) {
14 else => struct {},17 else => struct {},
15};18};
1619
20pub usingnamespace if (@hasDecl(root, "os") and @hasDecl(root.os, "bits")) root.os.bits else struct {};
21
17pub const iovec = extern struct {22pub const iovec = extern struct {
18 iov_base: [*]u8,23 iov_base: [*]u8,
19 iov_len: usize,24 iov_len: usize,
lib/std/thread.zig+2-2
...@@ -19,7 +19,7 @@ pub const Thread = struct {...@@ -19,7 +19,7 @@ pub const Thread = struct {
19 else switch (builtin.os) {19 else switch (builtin.os) {
20 .linux => i32,20 .linux => i32,
21 .windows => windows.HANDLE,21 .windows => windows.HANDLE,
22 else => @compileError("Unsupported OS"),22 else => void,
23 };23 };
2424
25 /// Represents a unique ID per thread.25 /// Represents a unique ID per thread.
...@@ -45,7 +45,7 @@ pub const Thread = struct {...@@ -45,7 +45,7 @@ pub const Thread = struct {
45 alloc_start: *c_void,45 alloc_start: *c_void,
46 heap_handle: windows.HANDLE,46 heap_handle: windows.HANDLE,
47 },47 },
48 else => @compileError("Unsupported OS"),48 else => struct {},
49 };49 };
5050
51 /// Returns the ID of the calling thread.51 /// Returns the ID of the calling thread.
src/ir.cpp+29-12
...@@ -9015,7 +9015,6 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *sc...@@ -9015,7 +9015,6 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *sc
9015 if (irb->exec->first_err_trace_msg == nullptr) {9015 if (irb->exec->first_err_trace_msg == nullptr) {
9016 irb->exec->first_err_trace_msg = irb->codegen->trace_err;9016 irb->exec->first_err_trace_msg = irb->codegen->trace_err;
9017 }9017 }
9018 src_assert(irb->exec->first_err_trace_msg != nullptr, node);
9019 }9018 }
9020 return result;9019 return result;
9021}9020}
...@@ -10709,10 +10708,12 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -10709,10 +10708,12 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
10709 if (type_is_global_error_set(err_set_type)) {10708 if (type_is_global_error_set(err_set_type)) {
10710 continue;10709 continue;
10711 }10710 }
10712 if (!resolve_inferred_error_set(ira->codegen, cur_type, cur_inst->source_node)) {10711 bool allow_infer = cur_type->data.error_set.infer_fn != nullptr &&
10712 cur_type->data.error_set.infer_fn == ira->new_irb.exec->fn_entry;
10713 if (!allow_infer && !resolve_inferred_error_set(ira->codegen, cur_type, cur_inst->source_node)) {
10713 return ira->codegen->builtin_types.entry_invalid;10714 return ira->codegen->builtin_types.entry_invalid;
10714 }10715 }
10715 if (type_is_global_error_set(cur_type)) {10716 if (!allow_infer && type_is_global_error_set(cur_type)) {
10716 err_set_type = ira->codegen->builtin_types.entry_global_error_set;10717 err_set_type = ira->codegen->builtin_types.entry_global_error_set;
10717 prev_inst = cur_inst;10718 prev_inst = cur_inst;
10718 continue;10719 continue;
...@@ -10830,10 +10831,12 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -10830,10 +10831,12 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
10830 }10831 }
1083110832
10832 if (cur_type->id == ZigTypeIdErrorSet) {10833 if (cur_type->id == ZigTypeIdErrorSet) {
10833 if (!resolve_inferred_error_set(ira->codegen, cur_type, cur_inst->source_node)) {10834 bool allow_infer = cur_type->data.error_set.infer_fn != nullptr &&
10835 cur_type->data.error_set.infer_fn == ira->new_irb.exec->fn_entry;
10836 if (!allow_infer && !resolve_inferred_error_set(ira->codegen, cur_type, cur_inst->source_node)) {
10834 return ira->codegen->builtin_types.entry_invalid;10837 return ira->codegen->builtin_types.entry_invalid;
10835 }10838 }
10836 if (type_is_global_error_set(cur_type)) {10839 if (!allow_infer && type_is_global_error_set(cur_type)) {
10837 err_set_type = ira->codegen->builtin_types.entry_global_error_set;10840 err_set_type = ira->codegen->builtin_types.entry_global_error_set;
10838 continue;10841 continue;
10839 }10842 }
...@@ -10844,17 +10847,20 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -10844,17 +10847,20 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
10844 update_errors_helper(ira->codegen, &errors, &errors_count);10847 update_errors_helper(ira->codegen, &errors, &errors_count);
1084510848
10846 if (err_set_type == nullptr) {10849 if (err_set_type == nullptr) {
10850 bool allow_infer = false;
10847 if (prev_type->id == ZigTypeIdErrorUnion) {10851 if (prev_type->id == ZigTypeIdErrorUnion) {
10848 err_set_type = prev_type->data.error_union.err_set_type;10852 err_set_type = prev_type->data.error_union.err_set_type;
10853 allow_infer = err_set_type->data.error_set.infer_fn != nullptr &&
10854 err_set_type->data.error_set.infer_fn == ira->new_irb.exec->fn_entry;
10849 } else {10855 } else {
10850 err_set_type = cur_type;10856 err_set_type = cur_type;
10851 }10857 }
1085210858
10853 if (!resolve_inferred_error_set(ira->codegen, err_set_type, cur_inst->source_node)) {10859 if (!allow_infer && !resolve_inferred_error_set(ira->codegen, err_set_type, cur_inst->source_node)) {
10854 return ira->codegen->builtin_types.entry_invalid;10860 return ira->codegen->builtin_types.entry_invalid;
10855 }10861 }
1085610862
10857 if (type_is_global_error_set(err_set_type)) {10863 if (!allow_infer && type_is_global_error_set(err_set_type)) {
10858 err_set_type = ira->codegen->builtin_types.entry_global_error_set;10864 err_set_type = ira->codegen->builtin_types.entry_global_error_set;
10859 continue;10865 continue;
10860 }10866 }
...@@ -10908,15 +10914,22 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -10908,15 +10914,22 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
10908 if (prev_err_set_type == cur_err_set_type)10914 if (prev_err_set_type == cur_err_set_type)
10909 continue;10915 continue;
1091010916
10911 if (!resolve_inferred_error_set(ira->codegen, prev_err_set_type, cur_inst->source_node)) {10917 bool allow_infer_prev = prev_err_set_type->data.error_set.infer_fn != nullptr &&
10918 prev_err_set_type->data.error_set.infer_fn == ira->new_irb.exec->fn_entry;
10919 bool allow_infer_cur = cur_err_set_type->data.error_set.infer_fn != nullptr &&
10920 cur_err_set_type->data.error_set.infer_fn == ira->new_irb.exec->fn_entry;
10921
10922 if (!allow_infer_prev && !resolve_inferred_error_set(ira->codegen, prev_err_set_type, cur_inst->source_node)) {
10912 return ira->codegen->builtin_types.entry_invalid;10923 return ira->codegen->builtin_types.entry_invalid;
10913 }10924 }
1091410925
10915 if (!resolve_inferred_error_set(ira->codegen, cur_err_set_type, cur_inst->source_node)) {10926 if (!allow_infer_cur && !resolve_inferred_error_set(ira->codegen, cur_err_set_type, cur_inst->source_node)) {
10916 return ira->codegen->builtin_types.entry_invalid;10927 return ira->codegen->builtin_types.entry_invalid;
10917 }10928 }
1091810929
10919 if (type_is_global_error_set(prev_err_set_type) || type_is_global_error_set(cur_err_set_type)) {10930 if ((!allow_infer_prev && type_is_global_error_set(prev_err_set_type)) ||
10931 (!allow_infer_cur && type_is_global_error_set(cur_err_set_type)))
10932 {
10920 err_set_type = ira->codegen->builtin_types.entry_global_error_set;10933 err_set_type = ira->codegen->builtin_types.entry_global_error_set;
10921 continue;10934 continue;
10922 }10935 }
...@@ -11085,10 +11098,14 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -11085,10 +11098,14 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
11085 {11098 {
11086 if (err_set_type != nullptr) {11099 if (err_set_type != nullptr) {
11087 ZigType *cur_err_set_type = cur_type->data.error_union.err_set_type;11100 ZigType *cur_err_set_type = cur_type->data.error_union.err_set_type;
11088 if (!resolve_inferred_error_set(ira->codegen, cur_err_set_type, cur_inst->source_node)) {11101 bool allow_infer = cur_err_set_type->data.error_set.infer_fn != nullptr &&
11102 cur_err_set_type->data.error_set.infer_fn == ira->new_irb.exec->fn_entry;
11103 if (!allow_infer && !resolve_inferred_error_set(ira->codegen, cur_err_set_type, cur_inst->source_node)) {
11089 return ira->codegen->builtin_types.entry_invalid;11104 return ira->codegen->builtin_types.entry_invalid;
11090 }11105 }
11091 if (type_is_global_error_set(cur_err_set_type) || type_is_global_error_set(err_set_type)) {11106 if ((!allow_infer && type_is_global_error_set(cur_err_set_type)) ||
11107 type_is_global_error_set(err_set_type))
11108 {
11092 err_set_type = ira->codegen->builtin_types.entry_global_error_set;11109 err_set_type = ira->codegen->builtin_types.entry_global_error_set;
11093 prev_inst = cur_inst;11110 prev_inst = cur_inst;
11094 continue;11111 continue;