mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 23:04:49 +00:00
Fix List.single
This commit is contained in:
parent
30a95e90d1
commit
750523ba5c
4 changed files with 109 additions and 100 deletions
|
@ -55,6 +55,7 @@ pub fn builtin_defs(var_store: &mut VarStore) -> MutMap<Symbol, Def> {
|
|||
Symbol::LIST_SET => list_set,
|
||||
Symbol::LIST_FIRST => list_first,
|
||||
Symbol::LIST_IS_EMPTY => list_is_empty,
|
||||
Symbol::LIST_SINGLE => list_single,
|
||||
Symbol::NUM_ADD => num_add,
|
||||
Symbol::NUM_SUB => num_sub,
|
||||
Symbol::NUM_MUL => num_mul,
|
||||
|
@ -422,6 +423,17 @@ fn list_is_empty(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
|||
defn(symbol, vec![Symbol::ARG_1], var_store, body)
|
||||
}
|
||||
|
||||
/// List.single : elem -> List elem
|
||||
fn list_single(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||
let body = RunLowLevel {
|
||||
op: LowLevel::ListSingle,
|
||||
args: vec![(var_store.fresh(), Var(Symbol::ARG_1))],
|
||||
ret_var: var_store.fresh(),
|
||||
};
|
||||
|
||||
defn(symbol, vec![Symbol::ARG_1], var_store, body)
|
||||
}
|
||||
|
||||
/// List.len : List * -> Int
|
||||
fn list_len(symbol: Symbol, var_store: &mut VarStore) -> Def {
|
||||
let body = RunLowLevel {
|
||||
|
|
|
@ -1020,76 +1020,6 @@ fn call_with_args<'a, 'ctx, 'env>(
|
|||
_parent: FunctionValue<'ctx>,
|
||||
args: &[(BasicValueEnum<'ctx>, &'a Layout<'a>)],
|
||||
) -> BasicValueEnum<'ctx> {
|
||||
match symbol {
|
||||
Symbol::LIST_SINGLE => {
|
||||
// List.single : a -> List a
|
||||
debug_assert!(args.len() == 1);
|
||||
|
||||
let (elem, elem_layout) = args[0];
|
||||
|
||||
let builder = env.builder;
|
||||
let ctx = env.context;
|
||||
|
||||
let elem_type = basic_type_from_layout(env.arena, ctx, elem_layout, env.ptr_bytes);
|
||||
let elem_bytes = elem_layout.stack_size(env.ptr_bytes) as u64;
|
||||
|
||||
let ptr = {
|
||||
let bytes_len = elem_bytes;
|
||||
let len_type = env.ptr_int();
|
||||
let len = len_type.const_int(bytes_len, false);
|
||||
|
||||
env.builder
|
||||
.build_array_malloc(elem_type, len, "create_list_ptr")
|
||||
.unwrap()
|
||||
|
||||
// TODO check if malloc returned null; if so, runtime error for OOM!
|
||||
};
|
||||
|
||||
// Put the element into the list
|
||||
let elem_ptr = unsafe {
|
||||
builder.build_in_bounds_gep(
|
||||
ptr,
|
||||
&[ctx.i32_type().const_int(
|
||||
// 0 as in 0 index of our new list
|
||||
0 as u64, false,
|
||||
)],
|
||||
"index",
|
||||
)
|
||||
};
|
||||
|
||||
builder.build_store(elem_ptr, elem);
|
||||
|
||||
let ptr_bytes = env.ptr_bytes;
|
||||
let int_type = ptr_int(ctx, ptr_bytes);
|
||||
let ptr_as_int = builder.build_ptr_to_int(ptr, int_type, "list_cast_ptr");
|
||||
let struct_type = collection(ctx, ptr_bytes);
|
||||
let len = BasicValueEnum::IntValue(env.ptr_int().const_int(1, false));
|
||||
|
||||
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, len, Builtin::WRAPPER_LEN, "insert_len")
|
||||
.unwrap();
|
||||
|
||||
//
|
||||
builder.build_bitcast(
|
||||
struct_val.into_struct_value(),
|
||||
collection(ctx, ptr_bytes),
|
||||
"cast_collection",
|
||||
)
|
||||
}
|
||||
_ => {
|
||||
let fn_name = layout_ids
|
||||
.get(symbol, layout)
|
||||
.to_symbol_string(symbol, &env.interns);
|
||||
|
@ -1119,8 +1049,6 @@ fn call_with_args<'a, 'ctx, 'env>(
|
|||
call.try_as_basic_value()
|
||||
.left()
|
||||
.unwrap_or_else(|| panic!("LLVM error: Invalid call by name for name {:?}", symbol))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn call_intrinsic<'a, 'ctx, 'env>(
|
||||
|
@ -1374,6 +1302,74 @@ fn run_low_level<'a, 'ctx, 'env>(
|
|||
|
||||
BasicValueEnum::IntValue(answer)
|
||||
}
|
||||
ListSingle => {
|
||||
// List.single : a -> List a
|
||||
debug_assert_eq!(args.len(), 1);
|
||||
|
||||
let elem = build_expr(env, layout_ids, scope, parent, &args[0].0);
|
||||
let builder = env.builder;
|
||||
let ctx = env.context;
|
||||
|
||||
let elem_layout = &args[0].1;
|
||||
let elem_type = basic_type_from_layout(env.arena, ctx, &elem_layout, env.ptr_bytes);
|
||||
let elem_bytes = elem_layout.stack_size(env.ptr_bytes) as u64;
|
||||
|
||||
let ptr = {
|
||||
let bytes_len = elem_bytes;
|
||||
let len_type = env.ptr_int();
|
||||
let len = len_type.const_int(bytes_len, false);
|
||||
|
||||
env.builder
|
||||
.build_array_malloc(elem_type, len, "create_list_ptr")
|
||||
.unwrap()
|
||||
|
||||
// TODO check if malloc returned null; if so, runtime error for OOM!
|
||||
};
|
||||
|
||||
// Put the element into the list
|
||||
let elem_ptr = unsafe {
|
||||
builder.build_in_bounds_gep(
|
||||
ptr,
|
||||
&[ctx.i32_type().const_int(
|
||||
// 0 as in 0 index of our new list
|
||||
0 as u64, false,
|
||||
)],
|
||||
"index",
|
||||
)
|
||||
};
|
||||
|
||||
builder.build_store(elem_ptr, elem);
|
||||
|
||||
let ptr_bytes = env.ptr_bytes;
|
||||
let int_type = ptr_int(ctx, ptr_bytes);
|
||||
let ptr_as_int = builder.build_ptr_to_int(ptr, int_type, "list_cast_ptr");
|
||||
let struct_type = collection(ctx, ptr_bytes);
|
||||
let len = BasicValueEnum::IntValue(env.ptr_int().const_int(1, false));
|
||||
|
||||
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, len, Builtin::WRAPPER_LEN, "insert_len")
|
||||
.unwrap();
|
||||
|
||||
//
|
||||
builder.build_bitcast(
|
||||
struct_val.into_struct_value(),
|
||||
collection(ctx, ptr_bytes),
|
||||
"cast_collection",
|
||||
)
|
||||
}
|
||||
NumAbs | NumNeg | NumRound | NumSqrt | NumSin | NumCos | NumToFloat => {
|
||||
debug_assert_eq!(args.len(), 1);
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ pub enum LowLevel {
|
|||
ListSet,
|
||||
ListSetInPlace,
|
||||
ListIsEmpty,
|
||||
ListSingle,
|
||||
NumAdd,
|
||||
NumSub,
|
||||
NumMul,
|
||||
|
|
|
@ -755,9 +755,9 @@ fn annotate_low_level_usage(
|
|||
}
|
||||
}
|
||||
|
||||
NumAdd | NumSub | NumMul | NumGt | NumGte | NumLt | NumLte | NumAbs | NumNeg
|
||||
| NumDivUnchecked | NumRemUnchecked | NumSqrt | NumRound | NumSin | NumCos | Eq | NotEq
|
||||
| And | Or | Not | NumToFloat => {
|
||||
ListSingle | NumAdd | NumSub | NumMul | NumGt | NumGte | NumLt | NumLte | NumAbs
|
||||
| NumNeg | NumDivUnchecked | NumRemUnchecked | NumSqrt | NumRound | NumSin | NumCos
|
||||
| Eq | NotEq | And | Or | Not | NumToFloat => {
|
||||
for (_, arg) in args {
|
||||
annotate_usage(&arg, usage);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue