From 7edef4f3fd8e260c69142741e750b3e6893434b8 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 17 May 2016 13:32:43 -0700 Subject: [PATCH] add beginning of print stack trace function introduce std.debug and move std.assert to std.debug.assert add mem.copy --- CMakeLists.txt | 1 + std/debug.zig | 16 ++++++++++++++++ std/hash_map.zig | 2 +- std/index.zig | 5 +---- std/list.zig | 2 +- std/mem.zig | 8 ++++++++ std/net.zig | 2 +- std/str.zig | 2 +- test/self_hosted.zig | 2 +- 9 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 std/debug.zig diff --git a/CMakeLists.txt b/CMakeLists.txt index 1feb0fc954..78e796ca22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -219,6 +219,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/mem.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/list.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/hash_map.zig" DESTINATION "${ZIG_STD_DEST}") install(FILES "${CMAKE_SOURCE_DIR}/std/empty.zig" DESTINATION "${ZIG_STD_DEST}") +install(FILES "${CMAKE_SOURCE_DIR}/std/debug.zig" DESTINATION "${ZIG_STD_DEST}") add_executable(run_tests ${TEST_SOURCES}) target_link_libraries(run_tests) diff --git a/std/debug.zig b/std/debug.zig new file mode 100644 index 0000000000..1a289b645e --- /dev/null +++ b/std/debug.zig @@ -0,0 +1,16 @@ +const io = @import("io.zig"); + +pub fn assert(b: bool) { + if (!b) unreachable{} +} + +pub fn print_stack_trace() { + var maybe_fp: ?&const u8 = @frame_address(); + while (true) { + const fp = maybe_fp ?? break; + const return_address = *(&const usize)(usize(fp) + @sizeof(usize)); + %%io.stderr.print_u64(return_address); + %%io.stderr.printf("\n"); + maybe_fp = *(&const ?&const u8)(fp); + } +} diff --git a/std/hash_map.zig b/std/hash_map.zig index 86cd3daee8..f5147fdf2f 100644 --- a/std/hash_map.zig +++ b/std/hash_map.zig @@ -1,4 +1,4 @@ -const assert = @import("index.zig").assert; +const assert = @import("debug.zig").assert; const math = @import("math.zig"); const mem = @import("mem.zig"); const Allocator = mem.Allocator; diff --git a/std/index.zig b/std/index.zig index 5f2ac7fd8c..b22a34e59a 100644 --- a/std/index.zig +++ b/std/index.zig @@ -8,13 +8,10 @@ pub const net = @import("net.zig"); pub const list = @import("list.zig"); pub const hash_map = @import("hash_map.zig"); pub const mem = @import("mem.zig"); +pub const debug = @import("debug.zig"); pub const linux = switch(@compile_var("os")) { linux => @import("linux.zig"), else => null_import, }; -pub fn assert(b: bool) { - if (!b) unreachable{} -} - const null_import = @import("empty.zig"); diff --git a/std/list.zig b/std/list.zig index 923a177345..31300a071e 100644 --- a/std/list.zig +++ b/std/list.zig @@ -1,4 +1,4 @@ -const assert = @import("index.zig").assert; +const assert = @import("debug.zig").assert; const mem = @import("mem.zig"); const Allocator = mem.Allocator; diff --git a/std/mem.zig b/std/mem.zig index a21706b921..d2a1ab049f 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -1,3 +1,5 @@ +const assert = @import("debug.zig").assert; + pub error NoMem; pub type Context = u8; @@ -8,3 +10,9 @@ pub struct Allocator { context: ?&Context, } +/// Copy all of source into dest at position 0. +/// dest.len must be >= source.len. +pub fn copy(T)(dest: []T, source: []T) { + assert(dest.len >= source.len); + @memcpy(dest.ptr, source.ptr, @sizeof(T) * source.len); +} diff --git a/std/net.zig b/std/net.zig index cf0c12672e..f98b6fbadf 100644 --- a/std/net.zig +++ b/std/net.zig @@ -1,6 +1,6 @@ const linux = @import("linux.zig"); const errno = @import("errno.zig"); -const assert = @import("index.zig").assert; +const assert = @import("debug.zig").assert; pub error SigInterrupt; pub error Unexpected; diff --git a/std/str.zig b/std/str.zig index 529c8ae7a0..06879e9be5 100644 --- a/std/str.zig +++ b/std/str.zig @@ -1,4 +1,4 @@ -const assert = @import("index.zig").assert; +const assert = @import("debug.zig").assert; pub const eql = slice_eql(u8); diff --git a/test/self_hosted.zig b/test/self_hosted.zig index 0a9ef68972..748d4ccc0b 100644 --- a/test/self_hosted.zig +++ b/test/self_hosted.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const assert = std.assert; +const assert = std.debug.assert; const str = std.str; const cstr = std.cstr; const other = @import("other.zig");