diff --git a/compiler/gen/src/llvm/build.rs b/compiler/gen/src/llvm/build.rs index 07174597ae..34f512406a 100644 --- a/compiler/gen/src/llvm/build.rs +++ b/compiler/gen/src/llvm/build.rs @@ -3,7 +3,7 @@ use crate::llvm::build_list::{ allocate_list, build_basic_phi2, clone_nonempty_list, empty_list, empty_polymorphic_list, incrementing_elem_loop, list_append, list_concat, list_get_unsafe, list_is_not_empty, list_join, list_len, list_map, list_prepend, list_repeat, list_reverse, list_set, list_single, - load_list_ptr, store_list, + load_list_ptr, store_list, LoopListArg, }; use crate::llvm::compare::{build_eq, build_neq}; use crate::llvm::convert::{ @@ -2021,8 +2021,10 @@ fn str_concat<'a, 'ctx, 'env>( builder, parent, ctx, - first_str_ptr, - first_str_len, + LoopListArg { + ptr: first_str_ptr, + len: first_str_len, + }, index_name, None, first_loop, @@ -2061,8 +2063,10 @@ fn str_concat<'a, 'ctx, 'env>( builder, parent, ctx, - second_str_ptr, - second_str_len, + LoopListArg { + ptr: second_str_ptr, + len: second_str_len, + }, index_name, Some(index_alloca), second_loop, diff --git a/compiler/gen/src/llvm/build_list.rs b/compiler/gen/src/llvm/build_list.rs index ba0984e9fb..66750627bf 100644 --- a/compiler/gen/src/llvm/build_list.rs +++ b/compiler/gen/src/llvm/build_list.rs @@ -258,8 +258,10 @@ pub fn list_join<'a, 'ctx, 'env>( builder, parent, ctx, - outer_list_ptr, - outer_list_len, + LoopListArg { + ptr: outer_list_ptr, + len: outer_list_len, + }, "#sum_index", None, sum_loop, @@ -322,8 +324,10 @@ pub fn list_join<'a, 'ctx, 'env>( builder, parent, ctx, - inner_list_ptr, - inner_list_len, + LoopListArg { + ptr: inner_list_ptr, + len: inner_list_len, + }, "#inner_index", None, inner_elem_loop, @@ -337,8 +341,10 @@ pub fn list_join<'a, 'ctx, 'env>( builder, parent, ctx, - outer_list_ptr, - outer_list_len, + LoopListArg { + ptr: outer_list_ptr, + len: outer_list_len, + }, "#inner_list_index", None, inner_list_loop, @@ -682,7 +688,13 @@ pub fn list_map<'a, 'ctx, 'env>( }; incrementing_elem_loop( - builder, parent, ctx, list_ptr, len, "#index", None, list_loop, + builder, + parent, + ctx, + LoopListArg { ptr: list_ptr, len }, + "#index", + None, + list_loop, ); store_list(env, ret_list_ptr, len) @@ -813,8 +825,10 @@ pub fn list_concat<'a, 'ctx, 'env>( builder, parent, ctx, - first_list_ptr, - first_list_len, + LoopListArg { + ptr: first_list_ptr, + len: first_list_len, + }, index_name, None, first_loop, @@ -857,8 +871,10 @@ pub fn list_concat<'a, 'ctx, 'env>( builder, parent, ctx, - second_list_ptr, - second_list_len, + LoopListArg { + ptr: second_list_ptr, + len: second_list_len, + }, index_name, Some(index_alloca), second_loop, @@ -895,14 +911,16 @@ pub fn list_concat<'a, 'ctx, 'env>( } } -// This helper simulates a basic for loop, where -// and index increments up from 0 to some end value +pub struct LoopListArg<'ctx> { + pub ptr: PointerValue<'ctx>, + pub len: IntValue<'ctx>, +} + pub fn incrementing_elem_loop<'ctx, LoopFn>( builder: &Builder<'ctx>, parent: FunctionValue<'ctx>, ctx: &'ctx Context, - list_ptr: PointerValue<'ctx>, - end: IntValue<'ctx>, + list: LoopListArg<'ctx>, index_name: &str, maybe_alloca: Option>, mut loop_fn: LoopFn, @@ -914,12 +932,12 @@ where builder, parent, ctx, - end, + list.len, index_name, maybe_alloca, |index| { // The pointer to the element in the list - let elem_ptr = unsafe { builder.build_in_bounds_gep(list_ptr, &[index], "load_index") }; + let elem_ptr = unsafe { builder.build_in_bounds_gep(list.ptr, &[index], "load_index") }; let elem = builder.build_load(elem_ptr, "get_elem");