From ca8c23e448bef1ce350ff8cf0e2cb787ffb48d96 Mon Sep 17 00:00:00 2001 From: Folkert Date: Wed, 3 Mar 2021 20:05:02 +0100 Subject: [PATCH] fix up zig code --- compiler/builtins/bitcode/src/list.zig | 48 ++++++++++++++++++++++---- compiler/gen/src/llvm/build.rs | 4 +-- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/compiler/builtins/bitcode/src/list.zig b/compiler/builtins/bitcode/src/list.zig index cdcd659208..5b8847c92d 100644 --- a/compiler/builtins/bitcode/src/list.zig +++ b/compiler/builtins/bitcode/src/list.zig @@ -95,6 +95,7 @@ pub const RocList = extern struct { const dest_ptr = first_slot; @memcpy(dest_ptr, source_ptr, old_length * element_width); + @memset(dest_ptr + old_length * element_width, 0, delta_length * element_width); } // NOTE the newly added elements are left uninitialized @@ -246,19 +247,35 @@ pub fn listWalk(list: RocList, stepper: Opaque, stepper_caller: Caller2, accum: return; } - @memcpy(output orelse unreachable, accum orelse unreachable, accum_width); + if (list.isEmpty()) { + @memcpy(output orelse unreachable, accum orelse unreachable, accum_width); + return; + } + + const alloc: [*]u8 = @ptrCast([*]u8, std.heap.c_allocator.alloc(u8, accum_width) catch unreachable); + var b1 = output orelse unreachable; + var b2 = alloc; + + @memcpy(b2, accum orelse unreachable, accum_width); if (list.bytes) |source_ptr| { var i: usize = 0; const size = list.len(); while (i < size) : (i += 1) { const element = source_ptr + i * element_width; - stepper_caller(stepper, element, output, output); - } + stepper_caller(stepper, element, b2, b1); - const data_bytes = list.len() * element_width; - utils.decref(std.heap.c_allocator, alignment, list.bytes, data_bytes); + const temp = b1; + b2 = b1; + b1 = temp; + } } + + @memcpy(output orelse unreachable, b2, accum_width); + std.heap.c_allocator.free(alloc[0..accum_width]); + + const data_bytes = list.len() * element_width; + utils.decref(std.heap.c_allocator, alignment, list.bytes, data_bytes); } pub fn listWalkBackwards(list: RocList, stepper: Opaque, stepper_caller: Caller2, accum: Opaque, alignment: usize, element_width: usize, accum_width: usize, output: Opaque) callconv(.C) void { @@ -266,7 +283,16 @@ pub fn listWalkBackwards(list: RocList, stepper: Opaque, stepper_caller: Caller2 return; } - @memcpy(output orelse unreachable, accum orelse unreachable, accum_width); + if (list.isEmpty()) { + @memcpy(output orelse unreachable, accum orelse unreachable, accum_width); + return; + } + + const alloc: [*]u8 = @ptrCast([*]u8, std.heap.c_allocator.alloc(u8, accum_width) catch unreachable); + var b1 = output orelse unreachable; + var b2 = alloc; + + @memcpy(b2, accum orelse unreachable, accum_width); if (list.bytes) |source_ptr| { const size = list.len(); @@ -275,11 +301,21 @@ pub fn listWalkBackwards(list: RocList, stepper: Opaque, stepper_caller: Caller2 i -= 1; const element = source_ptr + i * element_width; stepper_caller(stepper, element, output, output); + + const temp = b1; + b2 = b1; + b1 = temp; } const data_bytes = list.len() * element_width; utils.decref(std.heap.c_allocator, alignment, list.bytes, data_bytes); } + + @memcpy(output orelse unreachable, b2, accum_width); + std.heap.c_allocator.free(alloc[0..accum_width]); + + const data_bytes = list.len() * element_width; + utils.decref(std.heap.c_allocator, alignment, list.bytes, data_bytes); } // List.contains : List k, k -> Bool diff --git a/compiler/gen/src/llvm/build.rs b/compiler/gen/src/llvm/build.rs index 506dec1253..923fc7147f 100644 --- a/compiler/gen/src/llvm/build.rs +++ b/compiler/gen/src/llvm/build.rs @@ -1104,7 +1104,7 @@ pub fn build_exp_expr<'a, 'ctx, 'env>( let tag_field_layouts = &fields[*tag_id as usize]; for (field_symbol, tag_field_layout) in arguments.iter().zip(tag_field_layouts.iter()) { - let (val, val_layout) = load_symbol_and_layout(scope, field_symbol); + let (val, _val_layout) = load_symbol_and_layout(scope, field_symbol); // Zero-sized fields have no runtime representation. // The layout of the struct expects them to be dropped! @@ -1127,7 +1127,7 @@ pub fn build_exp_expr<'a, 'ctx, 'env>( field_vals.push(ptr); } else { // this check fails for recursive tag unions, but can be helpful while debugging - debug_assert_eq!(tag_field_layout, val_layout); + // debug_assert_eq!(tag_field_layout, val_layout); field_vals.push(val); }