mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 15:21:12 +00:00
if_non_empty helper function
This commit is contained in:
parent
a0a00d3521
commit
a83891011d
1 changed files with 112 additions and 124 deletions
|
@ -396,149 +396,86 @@ pub fn list_reverse<'a, 'ctx, 'env>(
|
||||||
) -> BasicValueEnum<'ctx> {
|
) -> BasicValueEnum<'ctx> {
|
||||||
let (_, list_layout) = load_symbol_and_layout(env, scope, list);
|
let (_, list_layout) = load_symbol_and_layout(env, scope, list);
|
||||||
|
|
||||||
match list_layout {
|
let non_empty_fn =
|
||||||
Layout::Builtin(Builtin::EmptyList) => empty_list(env),
|
|elem_layout: &Layout<'a>, len: IntValue<'ctx>, wrapper_struct: StructValue<'ctx>| {
|
||||||
|
|
||||||
Layout::Builtin(Builtin::List(_, elem_layout)) => {
|
|
||||||
let wrapper_struct = load_symbol(env, scope, list).into_struct_value();
|
|
||||||
|
|
||||||
let builder = env.builder;
|
let builder = env.builder;
|
||||||
let ctx = env.context;
|
let ctx = env.context;
|
||||||
|
|
||||||
let len = list_len(builder, wrapper_struct);
|
// Allocate space for the new array that we'll copy into.
|
||||||
|
let elem_type = basic_type_from_layout(env.arena, ctx, elem_layout, env.ptr_bytes);
|
||||||
|
|
||||||
// list_len > 0
|
let ptr_type = get_ptr_type(&elem_type, AddressSpace::Generic);
|
||||||
// We do this check to avoid allocating memory. If the input
|
|
||||||
// list is empty, then we can just return an empty list.
|
|
||||||
let comparison = builder.build_int_compare(
|
|
||||||
IntPredicate::UGT,
|
|
||||||
len,
|
|
||||||
ctx.i64_type().const_int(0, false),
|
|
||||||
"greaterthanzero",
|
|
||||||
);
|
|
||||||
|
|
||||||
let build_then = || {
|
let reversed_list_ptr = allocate_list(env, elem_layout, len);
|
||||||
// Allocate space for the new array that we'll copy into.
|
|
||||||
let elem_type = basic_type_from_layout(env.arena, ctx, elem_layout, env.ptr_bytes);
|
|
||||||
|
|
||||||
let ptr_type = get_ptr_type(&elem_type, AddressSpace::Generic);
|
// TODO check if malloc returned null; if so, runtime error for OOM!
|
||||||
|
|
||||||
let reversed_list_ptr = allocate_list(env, elem_layout, len);
|
let index_name = "#index";
|
||||||
|
let start_alloca = builder.build_alloca(ctx.i64_type(), index_name);
|
||||||
|
|
||||||
// TODO check if malloc returned null; if so, runtime error for OOM!
|
// Start at the last element in the list.
|
||||||
|
let last_elem_index =
|
||||||
|
builder.build_int_sub(len, ctx.i64_type().const_int(1, false), "lastelemindex");
|
||||||
|
builder.build_store(start_alloca, last_elem_index);
|
||||||
|
|
||||||
let index_name = "#index";
|
let loop_bb = ctx.append_basic_block(parent, "loop");
|
||||||
let start_alloca = builder.build_alloca(ctx.i64_type(), index_name);
|
builder.build_unconditional_branch(loop_bb);
|
||||||
|
builder.position_at_end(loop_bb);
|
||||||
|
|
||||||
// Start at the last element in the list.
|
// #index = #index - 1
|
||||||
let last_elem_index =
|
let curr_index = builder
|
||||||
builder.build_int_sub(len, ctx.i64_type().const_int(1, false), "lastelemindex");
|
.build_load(start_alloca, index_name)
|
||||||
builder.build_store(start_alloca, last_elem_index);
|
.into_int_value();
|
||||||
|
let next_index =
|
||||||
|
builder.build_int_sub(curr_index, ctx.i64_type().const_int(1, false), "nextindex");
|
||||||
|
|
||||||
let loop_bb = ctx.append_basic_block(parent, "loop");
|
builder.build_store(start_alloca, next_index);
|
||||||
builder.build_unconditional_branch(loop_bb);
|
|
||||||
builder.position_at_end(loop_bb);
|
|
||||||
|
|
||||||
// #index = #index - 1
|
let list_ptr = load_list_ptr(builder, wrapper_struct, ptr_type);
|
||||||
let curr_index = builder
|
|
||||||
.build_load(start_alloca, index_name)
|
|
||||||
.into_int_value();
|
|
||||||
let next_index = builder.build_int_sub(
|
|
||||||
curr_index,
|
|
||||||
ctx.i64_type().const_int(1, false),
|
|
||||||
"nextindex",
|
|
||||||
);
|
|
||||||
|
|
||||||
builder.build_store(start_alloca, next_index);
|
// The pointer to the element in the input list
|
||||||
|
let elem_ptr =
|
||||||
|
unsafe { builder.build_in_bounds_gep(list_ptr, &[curr_index], "load_index") };
|
||||||
|
|
||||||
let list_ptr = load_list_ptr(builder, wrapper_struct, ptr_type);
|
// The pointer to the element in the reversed list
|
||||||
|
let reverse_elem_ptr = unsafe {
|
||||||
// The pointer to the element in the input list
|
builder.build_in_bounds_gep(
|
||||||
let elem_ptr =
|
reversed_list_ptr,
|
||||||
unsafe { builder.build_in_bounds_gep(list_ptr, &[curr_index], "load_index") };
|
&[builder.build_int_sub(
|
||||||
|
len,
|
||||||
// The pointer to the element in the reversed list
|
builder.build_int_add(
|
||||||
let reverse_elem_ptr = unsafe {
|
curr_index,
|
||||||
builder.build_in_bounds_gep(
|
ctx.i64_type().const_int(1, false),
|
||||||
reversed_list_ptr,
|
"curr_index_plus_one",
|
||||||
&[builder.build_int_sub(
|
),
|
||||||
len,
|
"next_index",
|
||||||
builder.build_int_add(
|
)],
|
||||||
curr_index,
|
"load_index_reversed_list",
|
||||||
ctx.i64_type().const_int(1, false),
|
|
||||||
"curr_index_plus_one",
|
|
||||||
),
|
|
||||||
"next_index",
|
|
||||||
)],
|
|
||||||
"load_index_reversed_list",
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
let elem = builder.build_load(elem_ptr, "get_elem");
|
|
||||||
|
|
||||||
// Mutate the new array in-place to change the element.
|
|
||||||
builder.build_store(reverse_elem_ptr, elem);
|
|
||||||
|
|
||||||
// #index != 0
|
|
||||||
let end_cond = builder.build_int_compare(
|
|
||||||
IntPredicate::NE,
|
|
||||||
ctx.i64_type().const_int(0, false),
|
|
||||||
curr_index,
|
|
||||||
"loopcond",
|
|
||||||
);
|
|
||||||
|
|
||||||
let after_bb = ctx.append_basic_block(parent, "afterloop");
|
|
||||||
|
|
||||||
builder.build_conditional_branch(end_cond, loop_bb, after_bb);
|
|
||||||
builder.position_at_end(after_bb);
|
|
||||||
|
|
||||||
let ptr_bytes = env.ptr_bytes;
|
|
||||||
let int_type = ptr_int(ctx, ptr_bytes);
|
|
||||||
let ptr_as_int =
|
|
||||||
builder.build_ptr_to_int(reversed_list_ptr, int_type, "list_cast_ptr");
|
|
||||||
let struct_type = collection(ctx, ptr_bytes);
|
|
||||||
|
|
||||||
let mut struct_val;
|
|
||||||
|
|
||||||
// Store the pointer
|
|
||||||
struct_val = builder
|
|
||||||
.build_insert_value(
|
|
||||||
struct_type.get_undef(),
|
|
||||||
ptr_as_int,
|
|
||||||
Builtin::WRAPPER_PTR,
|
|
||||||
"insert_ptr",
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Store the length
|
|
||||||
struct_val = builder
|
|
||||||
.build_insert_value(struct_val, len, Builtin::WRAPPER_LEN, "insert_len")
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
builder.build_bitcast(
|
|
||||||
struct_val.into_struct_value(),
|
|
||||||
collection(ctx, ptr_bytes),
|
|
||||||
"cast_collection",
|
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let build_else = || empty_list(env);
|
let elem = builder.build_load(elem_ptr, "get_elem");
|
||||||
|
|
||||||
let struct_type = collection(ctx, env.ptr_bytes);
|
// Mutate the new array in-place to change the element.
|
||||||
|
builder.build_store(reverse_elem_ptr, elem);
|
||||||
|
|
||||||
build_basic_phi2(
|
// #index != 0
|
||||||
env,
|
let end_cond = builder.build_int_compare(
|
||||||
parent,
|
IntPredicate::NE,
|
||||||
comparison,
|
ctx.i64_type().const_int(0, false),
|
||||||
build_then,
|
curr_index,
|
||||||
build_else,
|
"loopcond",
|
||||||
BasicTypeEnum::StructType(struct_type),
|
);
|
||||||
)
|
|
||||||
}
|
let after_bb = ctx.append_basic_block(parent, "afterloop");
|
||||||
_ => {
|
|
||||||
unreachable!("Invalid List layout for List.reverse {:?}", list_layout);
|
builder.build_conditional_branch(end_cond, loop_bb, after_bb);
|
||||||
}
|
builder.position_at_end(after_bb);
|
||||||
}
|
|
||||||
|
store_list(env, reversed_list_ptr, len)
|
||||||
|
};
|
||||||
|
|
||||||
|
if_non_empty(env, parent, scope, non_empty_fn, list, list_layout)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn list_get_unsafe<'a, 'ctx, 'env>(
|
pub fn list_get_unsafe<'a, 'ctx, 'env>(
|
||||||
|
@ -1090,6 +1027,57 @@ where
|
||||||
index_alloca
|
index_alloca
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn if_non_empty<'a, 'ctx, 'env, 'b, NonEmptyFn>(
|
||||||
|
env: &Env<'a, 'ctx, 'env>,
|
||||||
|
parent: FunctionValue<'ctx>,
|
||||||
|
scope: &Scope<'a, 'ctx>,
|
||||||
|
mut build_non_empty: NonEmptyFn,
|
||||||
|
list: &Symbol,
|
||||||
|
list_layout: &'b Layout<'a>,
|
||||||
|
) -> BasicValueEnum<'ctx>
|
||||||
|
where
|
||||||
|
NonEmptyFn: FnMut(&Layout<'a>, IntValue<'ctx>, StructValue<'ctx>) -> BasicValueEnum<'ctx>,
|
||||||
|
{
|
||||||
|
match list_layout {
|
||||||
|
Layout::Builtin(Builtin::EmptyList) => empty_list(env),
|
||||||
|
|
||||||
|
Layout::Builtin(Builtin::List(_, elem_layout)) => {
|
||||||
|
let wrapper_struct = load_symbol(env, scope, list).into_struct_value();
|
||||||
|
|
||||||
|
let builder = env.builder;
|
||||||
|
let ctx = env.context;
|
||||||
|
|
||||||
|
let len = list_len(builder, wrapper_struct);
|
||||||
|
|
||||||
|
// list_len > 0
|
||||||
|
// We do this check to avoid allocating memory. If the input
|
||||||
|
// list is empty, then we can just return an empty list.
|
||||||
|
let comparison = builder.build_int_compare(
|
||||||
|
IntPredicate::UGT,
|
||||||
|
len,
|
||||||
|
ctx.i64_type().const_int(0, false),
|
||||||
|
"greaterthanzero",
|
||||||
|
);
|
||||||
|
|
||||||
|
let build_empty = || empty_list(env);
|
||||||
|
|
||||||
|
let struct_type = collection(ctx, env.ptr_bytes);
|
||||||
|
|
||||||
|
build_basic_phi2(
|
||||||
|
env,
|
||||||
|
parent,
|
||||||
|
comparison,
|
||||||
|
|| build_non_empty(elem_layout, len, wrapper_struct),
|
||||||
|
build_empty,
|
||||||
|
BasicTypeEnum::StructType(struct_type),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
unreachable!("Invalid List layout for List.reverse {:?}", list_layout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn build_basic_phi2<'a, 'ctx, 'env, PassFn, FailFn>(
|
pub fn build_basic_phi2<'a, 'ctx, 'env, PassFn, FailFn>(
|
||||||
env: &Env<'a, 'ctx, 'env>,
|
env: &Env<'a, 'ctx, 'env>,
|
||||||
parent: FunctionValue<'ctx>,
|
parent: FunctionValue<'ctx>,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue