Refactored the phi2 away

This commit is contained in:
Chad Stearns 2020-07-03 14:20:34 -04:00
parent fb7cbfdce4
commit d66cf913a9
2 changed files with 107 additions and 117 deletions

View file

@ -1604,21 +1604,6 @@ fn call_with_args<'a, 'ctx, 'env>(
let list_len = load_list_len(builder, wrapper_struct);
// list_len > 0
// We have to do a loop below, continuously adding the `elem`
// to the output list `List elem` until we have reached the
// number of repeats. This `comparison` is used to check
// if we need to do any looping; because if we dont, then we
// dont need to allocate memory for the index or the check
// if list_len == 0
let comparison = builder.build_int_compare(
IntPredicate::NE,
list_len,
ctx.i64_type().const_int(0, false),
"atleastzero",
);
let build_then = || {
match list_layout {
Layout::Builtin(Builtin::List(elem_layout)) => {
// Allocate space for the new array that we'll copy into.
@ -1629,14 +1614,12 @@ fn call_with_args<'a, 'ctx, 'env>(
let ptr_type = get_ptr_type(&elem_type, AddressSpace::Generic);
let list_ptr = load_list_ptr(builder, wrapper_struct, ptr_type);
let reversed_list_ptr = {
let len_type = env.ptr_int();
let len = len_type.const_int(elem_bytes, false);
env.builder
.build_array_malloc(elem_type, len, "create_list_ptr")
.build_array_malloc(elem_type, len, "create_reversed_list_ptr")
.unwrap()
// TODO check if malloc returned null; if so, runtime error for OOM!
@ -1663,13 +1646,31 @@ fn call_with_args<'a, 'ctx, 'env>(
builder.build_store(start_alloca, next_index);
let list_ptr = load_list_ptr(builder, wrapper_struct, ptr_type);
let elem_ptr = unsafe {
builder.build_in_bounds_gep(list_ptr, &[curr_index], "load_index")
};
let reverse_elem_ptr = unsafe {
builder.build_in_bounds_gep(
reversed_list_ptr,
&[builder.build_int_sub(
list_len,
builder.build_int_add(
curr_index,
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(elem_ptr, builder.build_load(elem_ptr, "List.get"));
builder.build_store(elem_ptr, ctx.i64_type().const_int(1, false));
builder.build_store(reverse_elem_ptr, elem);
// #index != 0
let end_cond = builder.build_int_compare(
@ -1718,24 +1719,11 @@ fn call_with_args<'a, 'ctx, 'env>(
"cast_collection",
)
}
Layout::Builtin(Builtin::EmptyList) => empty_list(env),
_ => {
unreachable!("Invalid List layout for List.get: {:?}", list_layout);
}
}
};
let build_else = || empty_list(env);
let struct_type = collection(ctx, env.ptr_bytes);
build_basic_phi2(
env,
parent,
comparison,
build_then,
build_else,
BasicTypeEnum::StructType(struct_type),
)
}
Symbol::INT_DIV_UNSAFE => {
debug_assert!(args.len() == 2);

View file

@ -518,6 +518,8 @@ mod gen_builtins {
#[test]
fn list_reverse() {
assert_evals_to!("List.reverse [1, 2, 3]", &[3, 2, 1], &'static [i64]);
assert_evals_to!("List.reverse [4]", &[4], &'static [i64]);
assert_evals_to!("List.reverse []", &[], &'static [i64]);
}
#[test]