mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 23:31:12 +00:00
Refactored the phi2 away
This commit is contained in:
parent
fb7cbfdce4
commit
d66cf913a9
2 changed files with 107 additions and 117 deletions
|
@ -1604,138 +1604,126 @@ fn call_with_args<'a, 'ctx, 'env>(
|
||||||
|
|
||||||
let list_len = load_list_len(builder, wrapper_struct);
|
let list_len = load_list_len(builder, wrapper_struct);
|
||||||
|
|
||||||
// list_len > 0
|
match list_layout {
|
||||||
// We have to do a loop below, continuously adding the `elem`
|
Layout::Builtin(Builtin::List(elem_layout)) => {
|
||||||
// to the output list `List elem` until we have reached the
|
// Allocate space for the new array that we'll copy into.
|
||||||
// number of repeats. This `comparison` is used to check
|
let elem_bytes = elem_layout.stack_size(env.ptr_bytes) as u64;
|
||||||
// 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 = || {
|
let elem_type =
|
||||||
match list_layout {
|
basic_type_from_layout(env.arena, ctx, elem_layout, env.ptr_bytes);
|
||||||
Layout::Builtin(Builtin::List(elem_layout)) => {
|
|
||||||
// Allocate space for the new array that we'll copy into.
|
|
||||||
let elem_bytes = elem_layout.stack_size(env.ptr_bytes) as u64;
|
|
||||||
|
|
||||||
let elem_type =
|
let ptr_type = get_ptr_type(&elem_type, AddressSpace::Generic);
|
||||||
basic_type_from_layout(env.arena, ctx, elem_layout, env.ptr_bytes);
|
|
||||||
|
|
||||||
let ptr_type = get_ptr_type(&elem_type, AddressSpace::Generic);
|
let reversed_list_ptr = {
|
||||||
|
let len_type = env.ptr_int();
|
||||||
|
let len = len_type.const_int(elem_bytes, false);
|
||||||
|
|
||||||
let list_ptr = load_list_ptr(builder, wrapper_struct, ptr_type);
|
env.builder
|
||||||
|
.build_array_malloc(elem_type, len, "create_reversed_list_ptr")
|
||||||
|
.unwrap()
|
||||||
|
|
||||||
let reversed_list_ptr = {
|
// TODO check if malloc returned null; if so, runtime error for OOM!
|
||||||
let len_type = env.ptr_int();
|
};
|
||||||
let len = len_type.const_int(elem_bytes, false);
|
|
||||||
|
|
||||||
env.builder
|
let index_name = "#index";
|
||||||
.build_array_malloc(elem_type, len, "create_list_ptr")
|
let start_alloca = builder.build_alloca(ctx.i64_type(), index_name);
|
||||||
.unwrap()
|
|
||||||
|
|
||||||
// TODO check if malloc returned null; if so, runtime error for OOM!
|
builder.build_store(start_alloca, list_len);
|
||||||
};
|
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
builder.build_store(start_alloca, list_len);
|
// #index = #index - 1
|
||||||
|
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",
|
||||||
|
);
|
||||||
|
|
||||||
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
|
let elem_ptr = unsafe {
|
||||||
.build_load(start_alloca, index_name)
|
builder.build_in_bounds_gep(list_ptr, &[curr_index], "load_index")
|
||||||
.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);
|
let reverse_elem_ptr = unsafe {
|
||||||
|
builder.build_in_bounds_gep(
|
||||||
let elem_ptr = unsafe {
|
reversed_list_ptr,
|
||||||
builder.build_in_bounds_gep(list_ptr, &[curr_index], "load_index")
|
&[builder.build_int_sub(
|
||||||
};
|
|
||||||
|
|
||||||
// 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));
|
|
||||||
|
|
||||||
// #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,
|
|
||||||
list_len,
|
list_len,
|
||||||
Builtin::WRAPPER_LEN,
|
builder.build_int_add(
|
||||||
"insert_len",
|
curr_index,
|
||||||
)
|
ctx.i64_type().const_int(1, false),
|
||||||
.unwrap();
|
"curr_index_plus_one",
|
||||||
|
),
|
||||||
builder.build_bitcast(
|
"next_index",
|
||||||
struct_val.into_struct_value(),
|
)],
|
||||||
collection(ctx, ptr_bytes),
|
"load_index_reversed_list",
|
||||||
"cast_collection",
|
|
||||||
)
|
)
|
||||||
}
|
};
|
||||||
_ => {
|
|
||||||
unreachable!("Invalid List layout for List.get: {:?}", list_layout);
|
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,
|
||||||
|
list_len,
|
||||||
|
Builtin::WRAPPER_LEN,
|
||||||
|
"insert_len",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
builder.build_bitcast(
|
||||||
|
struct_val.into_struct_value(),
|
||||||
|
collection(ctx, ptr_bytes),
|
||||||
|
"cast_collection",
|
||||||
|
)
|
||||||
}
|
}
|
||||||
};
|
Layout::Builtin(Builtin::EmptyList) => empty_list(env),
|
||||||
|
_ => {
|
||||||
let build_else = || empty_list(env);
|
unreachable!("Invalid List layout for List.get: {:?}", list_layout);
|
||||||
|
}
|
||||||
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 => {
|
Symbol::INT_DIV_UNSAFE => {
|
||||||
debug_assert!(args.len() == 2);
|
debug_assert!(args.len() == 2);
|
||||||
|
|
|
@ -518,6 +518,8 @@ mod gen_builtins {
|
||||||
#[test]
|
#[test]
|
||||||
fn list_reverse() {
|
fn list_reverse() {
|
||||||
assert_evals_to!("List.reverse [1, 2, 3]", &[3, 2, 1], &'static [i64]);
|
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]
|
#[test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue