List.appendUnsafe and List.reserve

This commit is contained in:
Folkert 2022-07-07 22:35:32 +02:00
parent dda79a255e
commit 56c9787e8f
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
13 changed files with 165 additions and 81 deletions

View file

@ -8,10 +8,10 @@ use crate::llvm::build_dict::{
};
use crate::llvm::build_hash::generic_hash;
use crate::llvm::build_list::{
self, allocate_list, empty_polymorphic_list, list_append, list_concat, list_drop_at,
self, allocate_list, empty_polymorphic_list, list_append_unsafe, list_concat, list_drop_at,
list_get_unsafe, list_len, list_map, list_map2, list_map3, list_map4, list_prepend,
list_replace_unsafe, list_sort_with, list_sublist, list_swap, list_symbol_to_c_abi,
list_to_c_abi, list_with_capacity,
list_replace_unsafe, list_reserve, list_sort_with, list_sublist, list_swap,
list_symbol_to_c_abi, list_to_c_abi, list_with_capacity,
};
use crate::llvm::build_str::{str_from_float, str_from_int, str_from_utf8, str_from_utf8_range};
use crate::llvm::compare::{generic_eq, generic_neq};
@ -5531,14 +5531,24 @@ fn run_low_level<'a, 'ctx, 'env>(
list_concat(env, first_list, second_list, element_layout)
}
ListAppend => {
// List.append : List elem, elem -> List elem
ListAppendUnsafe => {
// List.appendUnsafe : List elem, elem -> List elem
debug_assert_eq!(args.len(), 2);
let original_wrapper = load_symbol(scope, &args[0]).into_struct_value();
let (elem, elem_layout) = load_symbol_and_layout(scope, &args[1]);
list_append(env, original_wrapper, elem, elem_layout, update_mode)
list_append_unsafe(env, original_wrapper, elem, elem_layout)
}
ListReserve => {
// List.reserve : List elem, Nat -> List elem
debug_assert_eq!(args.len(), 2);
let (list, list_layout) = load_symbol_and_layout(scope, &args[0]);
let element_layout = list_element_layout!(list_layout);
let spare = load_symbol(scope, &args[1]);
list_reserve(env, list, spare, element_layout, update_mode)
}
ListSwap => {
// List.swap : List elem, Nat, Nat -> List elem

View file

@ -146,24 +146,42 @@ pub fn list_get_unsafe<'a, 'ctx, 'env>(
result
}
/// List.append : List elem, elem -> List elem
pub fn list_append<'a, 'ctx, 'env>(
/// List.reserve : List elem, Nat -> List elem
pub fn list_reserve<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
original_wrapper: StructValue<'ctx>,
element: BasicValueEnum<'ctx>,
list: BasicValueEnum<'ctx>,
spare: BasicValueEnum<'ctx>,
element_layout: &Layout<'a>,
update_mode: UpdateMode,
) -> BasicValueEnum<'ctx> {
call_list_bitcode_fn(
env,
&[
list_to_c_abi(env, original_wrapper.into()).into(),
list_to_c_abi(env, list).into(),
env.alignment_intvalue(element_layout),
pass_element_as_opaque(env, element, *element_layout),
spare,
layout_width(env, element_layout),
pass_update_mode(env, update_mode),
],
bitcode::LIST_APPEND,
bitcode::LIST_RESERVE,
)
}
/// List.appendUnsafe : List elem, elem -> List elem
pub fn list_append_unsafe<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
original_wrapper: StructValue<'ctx>,
element: BasicValueEnum<'ctx>,
element_layout: &Layout<'a>,
) -> BasicValueEnum<'ctx> {
call_list_bitcode_fn(
env,
&[
list_to_c_abi(env, original_wrapper.into()).into(),
pass_element_as_opaque(env, element, *element_layout),
layout_width(env, element_layout),
],
bitcode::LIST_APPEND_UNSAFE,
)
}