if_non_empty helper function

This commit is contained in:
Chad Stearns 2020-08-29 19:56:55 -04:00
parent a0a00d3521
commit a83891011d

View file

@ -396,28 +396,11 @@ 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);
// 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_then = || {
// Allocate space for the new array that we'll copy into. // 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 elem_type = basic_type_from_layout(env.arena, ctx, elem_layout, env.ptr_bytes);
@ -443,11 +426,8 @@ pub fn list_reverse<'a, 'ctx, 'env>(
let curr_index = builder let curr_index = builder
.build_load(start_alloca, index_name) .build_load(start_alloca, index_name)
.into_int_value(); .into_int_value();
let next_index = builder.build_int_sub( let next_index =
curr_index, builder.build_int_sub(curr_index, ctx.i64_type().const_int(1, false), "nextindex");
ctx.i64_type().const_int(1, false),
"nextindex",
);
builder.build_store(start_alloca, next_index); builder.build_store(start_alloca, next_index);
@ -492,53 +472,10 @@ pub fn list_reverse<'a, 'ctx, 'env>(
builder.build_conditional_branch(end_cond, loop_bb, after_bb); builder.build_conditional_branch(end_cond, loop_bb, after_bb);
builder.position_at_end(after_bb); builder.position_at_end(after_bb);
let ptr_bytes = env.ptr_bytes; store_list(env, reversed_list_ptr, len)
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); if_non_empty(env, parent, scope, non_empty_fn, list, list_layout)
let struct_type = collection(ctx, env.ptr_bytes);
build_basic_phi2(
env,
parent,
comparison,
build_then,
build_else,
BasicTypeEnum::StructType(struct_type),
)
}
_ => {
unreachable!("Invalid List layout for List.reverse {:?}", 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>,