| author | |
| committer | |
| log | 0c32b0b4ad32549193b46d33026d72b5e4a17bd9 |
| tree | 31070922081ac6517013ca8a76991f79456364e7 |
| parent | 18ed87c695398a1fbe972d0f8d212365a9832883 |
7 files changed, 112 insertions(+), 2 deletions(-)
CMakeLists.txt+2| ... | ... | @@ -214,6 +214,8 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/math.zig" DESTINATION "${ZIG_STD_DEST}") |
| 214 | 214 | install(FILES "${CMAKE_SOURCE_DIR}/std/index.zig" DESTINATION "${ZIG_STD_DEST}") |
| 215 | 215 | install(FILES "${CMAKE_SOURCE_DIR}/std/linux_x86_64.zig" DESTINATION "${ZIG_STD_DEST}") |
| 216 | 216 | install(FILES "${CMAKE_SOURCE_DIR}/std/linux_i386.zig" DESTINATION "${ZIG_STD_DEST}") |
| 217 | install(FILES "${CMAKE_SOURCE_DIR}/std/mem.zig" DESTINATION "${ZIG_STD_DEST}") | |
| 218 | install(FILES "${CMAKE_SOURCE_DIR}/std/list.zig" DESTINATION "${ZIG_STD_DEST}") | |
| 217 | 219 | |
| 218 | 220 | add_executable(run_tests ${TEST_SOURCES}) |
| 219 | 221 | target_link_libraries(run_tests) |
src/codegen.cpp+3| ... | ... | @@ -2386,6 +2386,7 @@ static LLVMValueRef gen_if_bool_expr_raw(CodeGen *g, AstNode *source_node, LLVMV |
| 2386 | 2386 | LLVMPositionBuilderAtEnd(g->builder, then_block); |
| 2387 | 2387 | LLVMValueRef then_expr_result = gen_expr(g, then_node); |
| 2388 | 2388 | if (then_endif_reachable) { |
| 2389 | clear_debug_source_node(g); | |
| 2389 | 2390 | LLVMBuildBr(g->builder, endif_block); |
| 2390 | 2391 | } |
| 2391 | 2392 | LLVMBasicBlockRef after_then_block = LLVMGetInsertBlock(g->builder); |
| ... | ... | @@ -2393,6 +2394,7 @@ static LLVMValueRef gen_if_bool_expr_raw(CodeGen *g, AstNode *source_node, LLVMV |
| 2393 | 2394 | LLVMPositionBuilderAtEnd(g->builder, else_block); |
| 2394 | 2395 | LLVMValueRef else_expr_result = gen_expr(g, else_node); |
| 2395 | 2396 | if (else_endif_reachable) { |
| 2397 | clear_debug_source_node(g); | |
| 2396 | 2398 | LLVMBuildBr(g->builder, endif_block); |
| 2397 | 2399 | } |
| 2398 | 2400 | LLVMBasicBlockRef after_else_block = LLVMGetInsertBlock(g->builder); |
| ... | ... | @@ -2400,6 +2402,7 @@ static LLVMValueRef gen_if_bool_expr_raw(CodeGen *g, AstNode *source_node, LLVMV |
| 2400 | 2402 | if (then_endif_reachable || else_endif_reachable) { |
| 2401 | 2403 | LLVMPositionBuilderAtEnd(g->builder, endif_block); |
| 2402 | 2404 | if (use_then_value && use_else_value) { |
| 2405 | set_debug_source_node(g, source_node); | |
| 2403 | 2406 | LLVMValueRef phi = LLVMBuildPhi(g->builder, LLVMTypeOf(then_expr_result), ""); |
| 2404 | 2407 | LLVMValueRef incoming_values[2] = {then_expr_result, else_expr_result}; |
| 2405 | 2408 | LLVMBasicBlockRef incoming_blocks[2] = {after_then_block, after_else_block}; |
std/index.zig+2| ... | ... | @@ -5,6 +5,8 @@ pub const math = @import("math.zig"); |
| 5 | 5 | pub const str = @import("str.zig"); |
| 6 | 6 | pub const cstr = @import("cstr.zig"); |
| 7 | 7 | pub const net = @import("net.zig"); |
| 8 | pub const list = @import("list.zig"); | |
| 9 | pub const mem = @import("mem.zig"); | |
| 8 | 10 | |
| 9 | 11 | pub fn assert(b: bool) { |
| 10 | 12 | if (!b) unreachable{} |
std/list.zig created+92| ... | ... | @@ -0,0 +1,92 @@ |
| 1 | const assert = @import("index.zig").assert; | |
| 2 | const mem = @import("mem.zig"); | |
| 3 | const Allocator = mem.Allocator; | |
| 4 | ||
| 5 | /* | |
| 6 | fn List(T: type) -> type { | |
| 7 | List(T, 8) | |
| 8 | } | |
| 9 | */ | |
| 10 | ||
| 11 | pub struct SmallList(T: type, STATIC_SIZE: isize) { | |
| 12 | items: []T, | |
| 13 | length: isize, | |
| 14 | prealloc_items: [STATIC_SIZE]T, | |
| 15 | allocator: &Allocator, | |
| 16 | ||
| 17 | pub fn init(l: &SmallList(T, STATIC_SIZE), allocator: &Allocator) { | |
| 18 | l.items = l.prealloc_items[0...]; | |
| 19 | l.length = 0; | |
| 20 | l.allocator = allocator; | |
| 21 | } | |
| 22 | ||
| 23 | pub fn deinit(l: &SmallList(T, STATIC_SIZE)) { | |
| 24 | if (l.items.ptr == &l.prealloc_items[0]) { | |
| 25 | l.allocator.free(l.allocator, ([]u8)(l.items)); | |
| 26 | } | |
| 27 | } | |
| 28 | ||
| 29 | pub fn append(l: &SmallList(T, STATIC_SIZE), item: T) -> %void { | |
| 30 | const new_length = l.length + 1; | |
| 31 | %return l.ensure_capacity(new_length); | |
| 32 | l.items[l.length] = item; | |
| 33 | l.length = new_length; | |
| 34 | } | |
| 35 | ||
| 36 | pub fn ensure_capacity(l: &SmallList(T, STATIC_SIZE), new_capacity: isize) -> %void { | |
| 37 | const old_capacity = l.items.len; | |
| 38 | var better_capacity = old_capacity; | |
| 39 | while (better_capacity < new_capacity) { | |
| 40 | better_capacity *= 2; | |
| 41 | } | |
| 42 | if (better_capacity != old_capacity) { | |
| 43 | const alloc_bytes = better_capacity * @sizeof(T); | |
| 44 | if (l.items.ptr == &l.prealloc_items[0]) { | |
| 45 | l.items = ([]T)(%return l.allocator.alloc(l.allocator, alloc_bytes)); | |
| 46 | @memcpy(l.items.ptr, &l.prealloc_items[0], old_capacity * @sizeof(T)); | |
| 47 | } else { | |
| 48 | l.items = ([]T)(%return l.allocator.realloc(l.allocator, ([]u8)(l.items), alloc_bytes)); | |
| 49 | } | |
| 50 | } | |
| 51 | } | |
| 52 | } | |
| 53 | ||
| 54 | var global_allocator = Allocator { | |
| 55 | .alloc = global_alloc, | |
| 56 | .realloc = global_realloc, | |
| 57 | .free = global_free, | |
| 58 | .context = null, | |
| 59 | }; | |
| 60 | ||
| 61 | var some_mem: [200]u8 = undefined; | |
| 62 | var some_mem_index: isize = 0; | |
| 63 | ||
| 64 | fn global_alloc(self: &Allocator, n: isize) -> %[]u8 { | |
| 65 | const result = some_mem[some_mem_index ... some_mem_index + n]; | |
| 66 | some_mem_index += n; | |
| 67 | return result; | |
| 68 | } | |
| 69 | ||
| 70 | fn global_realloc(self: &Allocator, old_mem: []u8, new_size: isize) -> %[]u8 { | |
| 71 | const result = %return global_alloc(self, new_size); | |
| 72 | @memcpy(result.ptr, old_mem.ptr, old_mem.len); | |
| 73 | return result; | |
| 74 | } | |
| 75 | ||
| 76 | fn global_free(self: &Allocator, old_mem: []u8) { | |
| 77 | } | |
| 78 | ||
| 79 | #attribute("test") | |
| 80 | fn basic_list_test() { | |
| 81 | var list: SmallList(i32, 4) = undefined; | |
| 82 | list.init(&global_allocator); | |
| 83 | defer list.deinit(); | |
| 84 | ||
| 85 | {var i: isize = 0; while (i < 10; i += 1) { | |
| 86 | %%list.append(i32(i + 1)); | |
| 87 | }} | |
| 88 | ||
| 89 | {var i: isize = 0; while (i < 10; i += 1) { | |
| 90 | assert(list.items[i] == i32(i + 1)); | |
| 91 | }} | |
| 92 | } |
std/mem.zig created+10| ... | ... | @@ -0,0 +1,10 @@ |
| 1 | pub error NoMem; | |
| 2 | ||
| 3 | pub type Context = u8; | |
| 4 | pub struct Allocator { | |
| 5 | alloc: fn (self: &Allocator, n: isize) -> %[]u8, | |
| 6 | realloc: fn (self: &Allocator, old_mem: []u8, new_size: isize) -> %[]u8, | |
| 7 | free: fn (self: &Allocator, mem: []u8), | |
| 8 | context: ?&Context, | |
| 9 | } | |
| 10 |
test/run_tests.cpp+1-1| ... | ... | @@ -1666,7 +1666,7 @@ static void run_self_hosted_test(bool is_release_mode) { |
| 1666 | 1666 | Termination term; |
| 1667 | 1667 | os_exec_process(zig_exe, args, &term, &zig_stderr, &zig_stdout); |
| 1668 | 1668 | |
| 1669 | if (term.how != TerminationIdClean) { | |
| 1669 | if (term.how != TerminationIdClean || term.code != 0) { | |
| 1670 | 1670 | printf("\nSelf-hosted tests failed:\n"); |
| 1671 | 1671 | printf("./zig"); |
| 1672 | 1672 | for (int i = 0; i < args.length; i += 1) { |
test/self_hosted.zig+2-1| ... | ... | @@ -1622,7 +1622,8 @@ fn div_exact(a: u32, b: u32) -> u32 { |
| 1622 | 1622 | |
| 1623 | 1623 | #attribute("test") |
| 1624 | 1624 | fn null_literal_outside_function() { |
| 1625 | assert(here_is_a_null_literal.context == null); | |
| 1625 | const is_null = if (const _ ?= here_is_a_null_literal.context) false else true; | |
| 1626 | assert(is_null); | |
| 1626 | 1627 | } |
| 1627 | 1628 | struct SillyStruct { |
| 1628 | 1629 | context: ?i32, |