mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 06:44:46 +00:00
Merge remote-tracking branch 'origin/trunk' into refcount
This commit is contained in:
commit
078c6df677
53 changed files with 3604 additions and 2377 deletions
|
@ -1828,7 +1828,7 @@ fn list_push<'a, 'ctx, 'env>(
|
|||
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);
|
||||
|
||||
let elems_ptr = load_list_ptr(builder, original_wrapper, ptr_type);
|
||||
let list_ptr = load_list_ptr(builder, original_wrapper, ptr_type);
|
||||
|
||||
// The output list length, which is the old list length + 1
|
||||
let new_list_len = env.builder.build_int_add(
|
||||
|
@ -1837,7 +1837,6 @@ fn list_push<'a, 'ctx, 'env>(
|
|||
"new_list_length",
|
||||
);
|
||||
|
||||
let ctx = env.context;
|
||||
let ptr_bytes = env.ptr_bytes;
|
||||
|
||||
// Calculate the number of bytes we'll need to allocate.
|
||||
|
@ -1863,7 +1862,7 @@ fn list_push<'a, 'ctx, 'env>(
|
|||
// one we just malloc'd.
|
||||
//
|
||||
// TODO how do we decide when to do the small memcpy vs the normal one?
|
||||
builder.build_memcpy(clone_ptr, ptr_bytes, elems_ptr, ptr_bytes, list_size);
|
||||
builder.build_memcpy(clone_ptr, ptr_bytes, list_ptr, ptr_bytes, list_size);
|
||||
} else {
|
||||
panic!("TODO Cranelift currently only knows how to clone list elements that are Copy.");
|
||||
}
|
||||
|
@ -1887,17 +1886,105 @@ fn list_push<'a, 'ctx, 'env>(
|
|||
.build_insert_value(struct_val, new_list_len, Builtin::WRAPPER_LEN, "insert_len")
|
||||
.unwrap();
|
||||
|
||||
let answer = builder.build_bitcast(
|
||||
struct_val.into_struct_value(),
|
||||
collection(ctx, ptr_bytes),
|
||||
"cast_collection",
|
||||
);
|
||||
|
||||
let elem_ptr = unsafe { builder.build_in_bounds_gep(clone_ptr, &[list_len], "load_index") };
|
||||
|
||||
builder.build_store(elem_ptr, elem);
|
||||
|
||||
answer
|
||||
builder.build_bitcast(
|
||||
struct_val.into_struct_value(),
|
||||
collection(ctx, ptr_bytes),
|
||||
"cast_collection",
|
||||
)
|
||||
}
|
||||
|
||||
/// List.prepend List elem, elem -> List elem
|
||||
fn list_prepend<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
original_wrapper: StructValue<'ctx>,
|
||||
elem: BasicValueEnum<'ctx>,
|
||||
elem_layout: &Layout<'a>,
|
||||
) -> BasicValueEnum<'ctx> {
|
||||
let builder = env.builder;
|
||||
let ctx = env.context;
|
||||
|
||||
// Load the usize length from the wrapper.
|
||||
let list_len = load_list_len(builder, original_wrapper);
|
||||
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);
|
||||
let list_ptr = load_list_ptr(builder, original_wrapper, ptr_type);
|
||||
|
||||
// The output list length, which is the old list length + 1
|
||||
let new_list_len = env.builder.build_int_add(
|
||||
ctx.i64_type().const_int(1 as u64, false),
|
||||
list_len,
|
||||
"new_list_length",
|
||||
);
|
||||
|
||||
let ptr_bytes = env.ptr_bytes;
|
||||
|
||||
// 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 clone_ptr = builder
|
||||
.build_array_malloc(elem_type, new_list_len, "list_ptr")
|
||||
.unwrap();
|
||||
let int_type = ptr_int(ctx, ptr_bytes);
|
||||
let ptr_as_int = builder.build_ptr_to_int(clone_ptr, int_type, "list_cast_ptr");
|
||||
|
||||
builder.build_store(clone_ptr, elem);
|
||||
|
||||
let index_1_ptr = unsafe {
|
||||
builder.build_in_bounds_gep(
|
||||
clone_ptr,
|
||||
&[ctx.i64_type().const_int(1 as u64, false)],
|
||||
"load_index",
|
||||
)
|
||||
};
|
||||
|
||||
// Calculate the number of bytes we'll need to allocate.
|
||||
let elem_bytes = env
|
||||
.ptr_int()
|
||||
.const_int(elem_layout.stack_size(env.ptr_bytes) as u64, false);
|
||||
|
||||
// This is the size of the list coming in, before we have added an element
|
||||
// to the beginning.
|
||||
let list_size = env
|
||||
.builder
|
||||
.build_int_mul(elem_bytes, list_len, "mul_old_len_by_elem_bytes");
|
||||
|
||||
if elem_layout.safe_to_memcpy() {
|
||||
// Copy the bytes from the original array into the new
|
||||
// one we just malloc'd.
|
||||
//
|
||||
// TODO how do we decide when to do the small memcpy vs the normal one?
|
||||
builder.build_memcpy(index_1_ptr, ptr_bytes, list_ptr, ptr_bytes, list_size);
|
||||
} else {
|
||||
panic!("TODO Cranelift currently only knows how to clone list elements that are Copy.");
|
||||
}
|
||||
|
||||
// Create a fresh wrapper struct for the newly populated array
|
||||
let struct_type = collection(ctx, env.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, new_list_len, Builtin::WRAPPER_LEN, "insert_len")
|
||||
.unwrap();
|
||||
|
||||
builder.build_bitcast(
|
||||
struct_val.into_struct_value(),
|
||||
collection(ctx, ptr_bytes),
|
||||
"cast_collection",
|
||||
)
|
||||
}
|
||||
|
||||
fn list_set<'a, 'ctx, 'env>(
|
||||
|
@ -2177,8 +2264,8 @@ fn run_low_level<'a, 'ctx, 'env>(
|
|||
}
|
||||
}
|
||||
}
|
||||
ListAppend => list_append(env, scope, parent, args),
|
||||
ListPush => {
|
||||
ListConcat => list_concat(env, scope, parent, args),
|
||||
ListAppend => {
|
||||
// List.push List elem, elem -> List elem
|
||||
debug_assert_eq!(args.len(), 2);
|
||||
|
||||
|
@ -2187,6 +2274,15 @@ fn run_low_level<'a, 'ctx, 'env>(
|
|||
|
||||
list_push(env, original_wrapper, elem, elem_layout)
|
||||
}
|
||||
ListPrepend => {
|
||||
// List.prepend List elem, elem -> List elem
|
||||
debug_assert_eq!(args.len(), 2);
|
||||
|
||||
let original_wrapper = load_symbol(env, scope, &args[0]).into_struct_value();
|
||||
let (elem, elem_layout) = load_symbol_and_layout(env, scope, &args[1]);
|
||||
|
||||
list_prepend(env, original_wrapper, elem, elem_layout)
|
||||
}
|
||||
NumAbs | NumNeg | NumRound | NumSqrtUnchecked | NumSin | NumCos | NumToFloat => {
|
||||
debug_assert_eq!(args.len(), 1);
|
||||
|
||||
|
@ -2395,13 +2491,13 @@ fn build_int_binop<'a, 'ctx, 'env>(
|
|||
}
|
||||
}
|
||||
|
||||
fn list_append<'a, 'ctx, 'env>(
|
||||
fn list_concat<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
scope: &Scope<'a, 'ctx>,
|
||||
parent: FunctionValue<'ctx>,
|
||||
args: &[Symbol],
|
||||
) -> BasicValueEnum<'ctx> {
|
||||
// List.append : List elem, List elem -> List elem
|
||||
// List.concat : List elem, List elem -> List elem
|
||||
debug_assert_eq!(args.len(), 2);
|
||||
|
||||
// This implementation is quite long, let me explain what is complicating it. Here are our
|
||||
|
@ -2489,7 +2585,7 @@ fn list_append<'a, 'ctx, 'env>(
|
|||
}
|
||||
_ => {
|
||||
unreachable!(
|
||||
"Invalid List layout for second input list of List.append: {:?}",
|
||||
"Invalid List layout for second input list of List.concat: {:?}",
|
||||
second_list_layout
|
||||
);
|
||||
}
|
||||
|
@ -2557,7 +2653,7 @@ fn list_append<'a, 'ctx, 'env>(
|
|||
// FIRST LOOP
|
||||
{
|
||||
let first_loop_bb =
|
||||
ctx.append_basic_block(parent, "first_list_append_loop");
|
||||
ctx.append_basic_block(parent, "first_list_concat_loop");
|
||||
|
||||
builder.build_unconditional_branch(first_loop_bb);
|
||||
builder.position_at_end(first_loop_bb);
|
||||
|
@ -2628,7 +2724,7 @@ fn list_append<'a, 'ctx, 'env>(
|
|||
// SECOND LOOP
|
||||
{
|
||||
let second_loop_bb =
|
||||
ctx.append_basic_block(parent, "second_list_append_loop");
|
||||
ctx.append_basic_block(parent, "second_list_concat_loop");
|
||||
|
||||
builder.build_unconditional_branch(second_loop_bb);
|
||||
builder.position_at_end(second_loop_bb);
|
||||
|
@ -2754,7 +2850,7 @@ fn list_append<'a, 'ctx, 'env>(
|
|||
}
|
||||
_ => {
|
||||
unreachable!(
|
||||
"Invalid List layout for second input list of List.append: {:?}",
|
||||
"Invalid List layout for second input list of List.concat: {:?}",
|
||||
second_list_layout
|
||||
);
|
||||
}
|
||||
|
@ -2799,7 +2895,7 @@ fn list_append<'a, 'ctx, 'env>(
|
|||
}
|
||||
_ => {
|
||||
unreachable!(
|
||||
"Invalid List layout for second input list of List.append: {:?}",
|
||||
"Invalid List layout for second input list of List.concat: {:?}",
|
||||
second_list_layout
|
||||
);
|
||||
}
|
||||
|
@ -2817,7 +2913,7 @@ fn list_append<'a, 'ctx, 'env>(
|
|||
}
|
||||
_ => {
|
||||
unreachable!(
|
||||
"Invalid List layout for first list in List.append : {:?}",
|
||||
"Invalid List layout for first list in List.concat : {:?}",
|
||||
first_list_layout
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue