authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-17 13:32:43-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-05-17 13:32:43-07:00
log7edef4f3fd8e260c69142741e750b3e6893434b8
tree5bde5c32cf492ab7ebef830a0a423a37777d4aaf
parent2c710382a888e8f45b958bdf3e77213cc18c2733

add beginning of print stack trace function

introduce std.debug and move std.assert to std.debug.assert add mem.copy

9 files changed, 31 insertions(+), 9 deletions(-)

CMakeLists.txt+1
...@@ -219,6 +219,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/mem.zig" DESTINATION "${ZIG_STD_DEST}")...@@ -219,6 +219,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/mem.zig" DESTINATION "${ZIG_STD_DEST}")
219install(FILES "${CMAKE_SOURCE_DIR}/std/list.zig" DESTINATION "${ZIG_STD_DEST}")219install(FILES "${CMAKE_SOURCE_DIR}/std/list.zig" DESTINATION "${ZIG_STD_DEST}")
220install(FILES "${CMAKE_SOURCE_DIR}/std/hash_map.zig" DESTINATION "${ZIG_STD_DEST}")220install(FILES "${CMAKE_SOURCE_DIR}/std/hash_map.zig" DESTINATION "${ZIG_STD_DEST}")
221install(FILES "${CMAKE_SOURCE_DIR}/std/empty.zig" DESTINATION "${ZIG_STD_DEST}")221install(FILES "${CMAKE_SOURCE_DIR}/std/empty.zig" DESTINATION "${ZIG_STD_DEST}")
222install(FILES "${CMAKE_SOURCE_DIR}/std/debug.zig" DESTINATION "${ZIG_STD_DEST}")
222223
223add_executable(run_tests ${TEST_SOURCES})224add_executable(run_tests ${TEST_SOURCES})
224target_link_libraries(run_tests)225target_link_libraries(run_tests)
std/debug.zig created+16
...@@ -0,0 +1,16 @@
1const io = @import("io.zig");
2
3pub fn assert(b: bool) {
4 if (!b) unreachable{}
5}
6
7pub fn print_stack_trace() {
8 var maybe_fp: ?&const u8 = @frame_address();
9 while (true) {
10 const fp = maybe_fp ?? break;
11 const return_address = *(&const usize)(usize(fp) + @sizeof(usize));
12 %%io.stderr.print_u64(return_address);
13 %%io.stderr.printf("\n");
14 maybe_fp = *(&const ?&const u8)(fp);
15 }
16}
std/hash_map.zig+1-1
...@@ -1,4 +1,4 @@...@@ -1,4 +1,4 @@
1const assert = @import("index.zig").assert;1const assert = @import("debug.zig").assert;
2const math = @import("math.zig");2const math = @import("math.zig");
3const mem = @import("mem.zig");3const mem = @import("mem.zig");
4const Allocator = mem.Allocator;4const Allocator = mem.Allocator;
std/index.zig+1-4
...@@ -8,13 +8,10 @@ pub const net = @import("net.zig");...@@ -8,13 +8,10 @@ pub const net = @import("net.zig");
8pub const list = @import("list.zig");8pub const list = @import("list.zig");
9pub const hash_map = @import("hash_map.zig");9pub const hash_map = @import("hash_map.zig");
10pub const mem = @import("mem.zig");10pub const mem = @import("mem.zig");
11pub const debug = @import("debug.zig");
11pub const linux = switch(@compile_var("os")) {12pub const linux = switch(@compile_var("os")) {
12 linux => @import("linux.zig"),13 linux => @import("linux.zig"),
13 else => null_import,14 else => null_import,
14};15};
1516
16pub fn assert(b: bool) {
17 if (!b) unreachable{}
18}
19
20const null_import = @import("empty.zig");17const null_import = @import("empty.zig");
std/list.zig+1-1
...@@ -1,4 +1,4 @@...@@ -1,4 +1,4 @@
1const assert = @import("index.zig").assert;1const assert = @import("debug.zig").assert;
2const mem = @import("mem.zig");2const mem = @import("mem.zig");
3const Allocator = mem.Allocator;3const Allocator = mem.Allocator;
44
std/mem.zig+8
...@@ -1,3 +1,5 @@...@@ -1,3 +1,5 @@
1const assert = @import("debug.zig").assert;
2
1pub error NoMem;3pub error NoMem;
24
3pub type Context = u8;5pub type Context = u8;
...@@ -8,3 +10,9 @@ pub struct Allocator {...@@ -8,3 +10,9 @@ pub struct Allocator {
8 context: ?&Context,10 context: ?&Context,
9}11}
1012
13/// Copy all of source into dest at position 0.
14/// dest.len must be >= source.len.
15pub fn copy(T)(dest: []T, source: []T) {
16 assert(dest.len >= source.len);
17 @memcpy(dest.ptr, source.ptr, @sizeof(T) * source.len);
18}
std/net.zig+1-1
...@@ -1,6 +1,6 @@...@@ -1,6 +1,6 @@
1const linux = @import("linux.zig");1const linux = @import("linux.zig");
2const errno = @import("errno.zig");2const errno = @import("errno.zig");
3const assert = @import("index.zig").assert;3const assert = @import("debug.zig").assert;
44
5pub error SigInterrupt;5pub error SigInterrupt;
6pub error Unexpected;6pub error Unexpected;
std/str.zig+1-1
...@@ -1,4 +1,4 @@...@@ -1,4 +1,4 @@
1const assert = @import("index.zig").assert;1const assert = @import("debug.zig").assert;
22
3pub const eql = slice_eql(u8);3pub const eql = slice_eql(u8);
44
test/self_hosted.zig+1-1
...@@ -1,5 +1,5 @@...@@ -1,5 +1,5 @@
1const std = @import("std");1const std = @import("std");
2const assert = std.assert;2const assert = std.debug.assert;
3const str = std.str;3const str = std.str;
4const cstr = std.cstr;4const cstr = std.cstr;
5const other = @import("other.zig");5const other = @import("other.zig");