mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-02 19:32:17 +00:00
move List.sublist to backend
This commit is contained in:
parent
8f8b2463ea
commit
98d2c57edf
10 changed files with 102 additions and 33 deletions
|
@ -888,6 +888,45 @@ pub fn listTakeFirst(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn listSublist(
|
||||||
|
list: RocList,
|
||||||
|
alignment: u32,
|
||||||
|
element_width: usize,
|
||||||
|
rec: extern struct { len: usize, start: usize },
|
||||||
|
dec: Dec,
|
||||||
|
) callconv(.C) RocList {
|
||||||
|
const start = rec.start;
|
||||||
|
const len = rec.len;
|
||||||
|
if (list.bytes) |source_ptr| {
|
||||||
|
const size = list.len();
|
||||||
|
|
||||||
|
if (start >= size) {
|
||||||
|
return RocList.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
const keep_len = std.math.min(len, size - start);
|
||||||
|
const drop_len = std.math.max(start, 0);
|
||||||
|
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i < drop_len) : (i += 1) {
|
||||||
|
const element = source_ptr + i * element_width;
|
||||||
|
dec(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
const output = RocList.allocate(alignment, keep_len, element_width);
|
||||||
|
const target_ptr = output.bytes orelse unreachable;
|
||||||
|
|
||||||
|
@memcpy(target_ptr, source_ptr + start * element_width, keep_len * element_width);
|
||||||
|
|
||||||
|
utils.decref(list.bytes, size * element_width, alignment);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
} else {
|
||||||
|
return RocList.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn listTakeLast(
|
pub fn listTakeLast(
|
||||||
list: RocList,
|
list: RocList,
|
||||||
alignment: u32,
|
alignment: u32,
|
||||||
|
|
|
@ -47,6 +47,7 @@ comptime {
|
||||||
exportListFn(list.listConcat, "concat");
|
exportListFn(list.listConcat, "concat");
|
||||||
exportListFn(list.listTakeFirst, "take_first");
|
exportListFn(list.listTakeFirst, "take_first");
|
||||||
exportListFn(list.listTakeLast, "take_last");
|
exportListFn(list.listTakeLast, "take_last");
|
||||||
|
exportListFn(list.listSublist, "sublist");
|
||||||
exportListFn(list.listDrop, "drop");
|
exportListFn(list.listDrop, "drop");
|
||||||
exportListFn(list.listDropAt, "drop_at");
|
exportListFn(list.listDropAt, "drop_at");
|
||||||
exportListFn(list.listSet, "set");
|
exportListFn(list.listSet, "set");
|
||||||
|
|
|
@ -185,6 +185,7 @@ pub const LIST_APPEND: &str = "roc_builtins.list.append";
|
||||||
pub const LIST_PREPEND: &str = "roc_builtins.list.prepend";
|
pub const LIST_PREPEND: &str = "roc_builtins.list.prepend";
|
||||||
pub const LIST_TAKE_FIRST: &str = "roc_builtins.list.take_first";
|
pub const LIST_TAKE_FIRST: &str = "roc_builtins.list.take_first";
|
||||||
pub const LIST_TAKE_LAST: &str = "roc_builtins.list.take_last";
|
pub const LIST_TAKE_LAST: &str = "roc_builtins.list.take_last";
|
||||||
|
pub const LIST_SUBLIST: &str = "roc_builtins.list.sublist";
|
||||||
pub const LIST_DROP: &str = "roc_builtins.list.drop";
|
pub const LIST_DROP: &str = "roc_builtins.list.drop";
|
||||||
pub const LIST_DROP_AT: &str = "roc_builtins.list.drop_at";
|
pub const LIST_DROP_AT: &str = "roc_builtins.list.drop_at";
|
||||||
pub const LIST_SWAP: &str = "roc_builtins.list.swap";
|
pub const LIST_SWAP: &str = "roc_builtins.list.swap";
|
||||||
|
|
|
@ -2070,34 +2070,9 @@ fn list_sublist(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||||
let sym_list = Symbol::ARG_1;
|
let sym_list = Symbol::ARG_1;
|
||||||
let sym_rec = Symbol::ARG_2;
|
let sym_rec = Symbol::ARG_2;
|
||||||
|
|
||||||
let start_var = var_store.fresh();
|
let body = RunLowLevel {
|
||||||
let len_var = var_store.fresh();
|
op: LowLevel::ListSublist,
|
||||||
|
args: vec![(list_var, Var(sym_list)), (rec_var, Var(sym_rec))],
|
||||||
let get_start = Access {
|
|
||||||
record_var: rec_var,
|
|
||||||
ext_var: var_store.fresh(),
|
|
||||||
field_var: var_store.fresh(),
|
|
||||||
loc_expr: Box::new(no_region(Var(sym_rec))),
|
|
||||||
field: "start".into(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let get_len = Access {
|
|
||||||
record_var: rec_var,
|
|
||||||
ext_var: var_store.fresh(),
|
|
||||||
field_var: var_store.fresh(),
|
|
||||||
loc_expr: Box::new(no_region(Var(sym_rec))),
|
|
||||||
field: "len".into(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let body_drop = RunLowLevel {
|
|
||||||
op: LowLevel::ListDrop,
|
|
||||||
args: vec![(list_var, Var(sym_list)), (start_var, get_start)],
|
|
||||||
ret_var: list_var,
|
|
||||||
};
|
|
||||||
|
|
||||||
let body_take = RunLowLevel {
|
|
||||||
op: LowLevel::ListTakeFirst,
|
|
||||||
args: vec![(list_var, body_drop), (len_var, get_len)],
|
|
||||||
ret_var: list_var,
|
ret_var: list_var,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2105,7 +2080,7 @@ fn list_sublist(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||||
symbol,
|
symbol,
|
||||||
vec![(list_var, sym_list), (rec_var, sym_rec)],
|
vec![(list_var, sym_list), (rec_var, sym_rec)],
|
||||||
var_store,
|
var_store,
|
||||||
body_take,
|
body,
|
||||||
list_var,
|
list_var,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ use crate::llvm::build_list::{
|
||||||
list_contains, list_drop, list_drop_at, list_find_trivial_not_found, list_find_unsafe,
|
list_contains, list_drop, list_drop_at, list_find_trivial_not_found, list_find_unsafe,
|
||||||
list_get_unsafe, list_join, list_keep_errs, list_keep_if, list_keep_oks, list_len, list_map,
|
list_get_unsafe, list_join, list_keep_errs, list_keep_if, list_keep_oks, list_len, list_map,
|
||||||
list_map2, list_map3, list_map4, list_map_with_index, list_prepend, list_range, list_repeat,
|
list_map2, list_map3, list_map4, list_map_with_index, list_prepend, list_range, list_repeat,
|
||||||
list_reverse, list_set, list_single, list_sort_with, list_swap, list_take_first,
|
list_reverse, list_set, list_single, list_sort_with, list_sublist, list_swap, list_take_first,
|
||||||
list_take_last,
|
list_take_last,
|
||||||
};
|
};
|
||||||
use crate::llvm::build_str::{
|
use crate::llvm::build_str::{
|
||||||
|
@ -5210,6 +5210,27 @@ fn run_low_level<'a, 'ctx, 'env>(
|
||||||
_ => unreachable!("Invalid layout {:?} in List.takeLast", list_layout),
|
_ => unreachable!("Invalid layout {:?} in List.takeLast", list_layout),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ListSublist => {
|
||||||
|
// List.sublist : List elem, { start : Nat, len : Nat } -> List elem
|
||||||
|
debug_assert_eq!(args.len(), 2);
|
||||||
|
|
||||||
|
let (list, list_layout) = load_symbol_and_layout(scope, &args[0]);
|
||||||
|
let original_wrapper = list.into_struct_value();
|
||||||
|
|
||||||
|
let rec = load_symbol(scope, &args[1]);
|
||||||
|
|
||||||
|
match list_layout {
|
||||||
|
Layout::Builtin(Builtin::EmptyList) => empty_list(env),
|
||||||
|
Layout::Builtin(Builtin::List(element_layout)) => list_sublist(
|
||||||
|
env,
|
||||||
|
layout_ids,
|
||||||
|
original_wrapper,
|
||||||
|
rec.into_struct_value(),
|
||||||
|
element_layout,
|
||||||
|
),
|
||||||
|
_ => unreachable!("Invalid layout {:?} in List.sublist", list_layout),
|
||||||
|
}
|
||||||
|
}
|
||||||
ListDrop => {
|
ListDrop => {
|
||||||
// List.drop : List elem, Nat -> List elem
|
// List.drop : List elem, Nat -> List elem
|
||||||
debug_assert_eq!(args.len(), 2);
|
debug_assert_eq!(args.len(), 2);
|
||||||
|
|
|
@ -338,6 +338,33 @@ pub fn list_take_last<'a, 'ctx, 'env>(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// List.sublist : List elem, { start : Nat, len : Nat } -> List elem
|
||||||
|
pub fn list_sublist<'a, 'ctx, 'env>(
|
||||||
|
env: &Env<'a, 'ctx, 'env>,
|
||||||
|
layout_ids: &mut LayoutIds<'a>,
|
||||||
|
original_wrapper: StructValue<'ctx>,
|
||||||
|
len_and_start: StructValue<'ctx>,
|
||||||
|
element_layout: &Layout<'a>,
|
||||||
|
) -> BasicValueEnum<'ctx> {
|
||||||
|
let dec_element_fn = build_dec_wrapper(env, layout_ids, element_layout);
|
||||||
|
call_bitcode_fn_returns_list(
|
||||||
|
env,
|
||||||
|
&[
|
||||||
|
pass_list_cc(env, original_wrapper.into()),
|
||||||
|
env.alignment_intvalue(element_layout),
|
||||||
|
layout_width(env, element_layout),
|
||||||
|
complex_bitcast(
|
||||||
|
env.builder,
|
||||||
|
len_and_start.into(),
|
||||||
|
env.str_list_c_abi().into(),
|
||||||
|
"to_i128",
|
||||||
|
),
|
||||||
|
dec_element_fn.as_global_value().as_pointer_value().into(),
|
||||||
|
],
|
||||||
|
bitcode::LIST_SUBLIST,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// List.drop : List elem, Nat -> List elem
|
/// List.drop : List elem, Nat -> List elem
|
||||||
pub fn list_drop<'a, 'ctx, 'env>(
|
pub fn list_drop<'a, 'ctx, 'env>(
|
||||||
env: &Env<'a, 'ctx, 'env>,
|
env: &Env<'a, 'ctx, 'env>,
|
||||||
|
|
|
@ -34,9 +34,10 @@ pub fn build_call_low_level<'a>(
|
||||||
| ListContains | ListAppend | ListPrepend | ListJoin | ListRange | ListMap | ListMap2
|
| ListContains | ListAppend | ListPrepend | ListJoin | ListRange | ListMap | ListMap2
|
||||||
| ListMap3 | ListMap4 | ListMapWithIndex | ListKeepIf | ListWalk | ListWalkUntil
|
| ListMap3 | ListMap4 | ListMapWithIndex | ListKeepIf | ListWalk | ListWalkUntil
|
||||||
| ListWalkBackwards | ListKeepOks | ListKeepErrs | ListSortWith | ListTakeFirst
|
| ListWalkBackwards | ListKeepOks | ListKeepErrs | ListSortWith | ListTakeFirst
|
||||||
| ListTakeLast | ListDrop | ListDropAt | ListSwap | ListAny | ListFindUnsafe | DictSize
|
| ListTakeLast | ListSublist | ListDrop | ListDropAt | ListSwap | ListAny
|
||||||
| DictEmpty | DictInsert | DictRemove | DictContains | DictGetUnsafe | DictKeys
|
| ListFindUnsafe | DictSize | DictEmpty | DictInsert | DictRemove | DictContains
|
||||||
| DictValues | DictUnion | DictIntersection | DictDifference | DictWalk | SetFromList => {
|
| DictGetUnsafe | DictKeys | DictValues | DictUnion | DictIntersection | DictDifference
|
||||||
|
| DictWalk | SetFromList => {
|
||||||
return NotImplemented;
|
return NotImplemented;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,6 +45,7 @@ pub enum LowLevel {
|
||||||
ListSortWith,
|
ListSortWith,
|
||||||
ListTakeFirst,
|
ListTakeFirst,
|
||||||
ListTakeLast,
|
ListTakeLast,
|
||||||
|
ListSublist,
|
||||||
ListDrop,
|
ListDrop,
|
||||||
ListDropAt,
|
ListDropAt,
|
||||||
ListSwap,
|
ListSwap,
|
||||||
|
@ -138,6 +139,7 @@ macro_rules! first_order {
|
||||||
| ListSet
|
| ListSet
|
||||||
| ListTakeFirst
|
| ListTakeFirst
|
||||||
| ListTakeLast
|
| ListTakeLast
|
||||||
|
| ListSublist
|
||||||
| ListDrop
|
| ListDrop
|
||||||
| ListDropAt
|
| ListDropAt
|
||||||
| ListSingle
|
| ListSingle
|
||||||
|
|
|
@ -968,6 +968,7 @@ pub fn lowlevel_borrow_signature(arena: &Bump, op: LowLevel) -> &[bool] {
|
||||||
ListAppend => arena.alloc_slice_copy(&[owned, owned]),
|
ListAppend => arena.alloc_slice_copy(&[owned, owned]),
|
||||||
ListTakeFirst => arena.alloc_slice_copy(&[owned, irrelevant]),
|
ListTakeFirst => arena.alloc_slice_copy(&[owned, irrelevant]),
|
||||||
ListTakeLast => arena.alloc_slice_copy(&[owned, irrelevant]),
|
ListTakeLast => arena.alloc_slice_copy(&[owned, irrelevant]),
|
||||||
|
ListSublist => arena.alloc_slice_copy(&[owned, irrelevant]),
|
||||||
ListDrop => arena.alloc_slice_copy(&[owned, irrelevant]),
|
ListDrop => arena.alloc_slice_copy(&[owned, irrelevant]),
|
||||||
ListDropAt => arena.alloc_slice_copy(&[owned, irrelevant]),
|
ListDropAt => arena.alloc_slice_copy(&[owned, irrelevant]),
|
||||||
ListSwap => arena.alloc_slice_copy(&[owned, irrelevant, irrelevant]),
|
ListSwap => arena.alloc_slice_copy(&[owned, irrelevant, irrelevant]),
|
||||||
|
|
|
@ -102,6 +102,7 @@ enum FirstOrder {
|
||||||
ListSet,
|
ListSet,
|
||||||
ListTakeFirst,
|
ListTakeFirst,
|
||||||
ListTakeLast,
|
ListTakeLast,
|
||||||
|
ListSublist,
|
||||||
ListDrop,
|
ListDrop,
|
||||||
ListDropAt,
|
ListDropAt,
|
||||||
ListSingle,
|
ListSingle,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue