Fix List.single

This commit is contained in:
Richard Feldman 2020-06-23 19:34:09 -04:00
parent 30a95e90d1
commit 750523ba5c
4 changed files with 109 additions and 100 deletions

View file

@ -55,6 +55,7 @@ pub fn builtin_defs(var_store: &mut VarStore) -> MutMap<Symbol, Def> {
Symbol::LIST_SET => list_set, Symbol::LIST_SET => list_set,
Symbol::LIST_FIRST => list_first, Symbol::LIST_FIRST => list_first,
Symbol::LIST_IS_EMPTY => list_is_empty, Symbol::LIST_IS_EMPTY => list_is_empty,
Symbol::LIST_SINGLE => list_single,
Symbol::NUM_ADD => num_add, Symbol::NUM_ADD => num_add,
Symbol::NUM_SUB => num_sub, Symbol::NUM_SUB => num_sub,
Symbol::NUM_MUL => num_mul, 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) 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 /// List.len : List * -> Int
fn list_len(symbol: Symbol, var_store: &mut VarStore) -> Def { fn list_len(symbol: Symbol, var_store: &mut VarStore) -> Def {
let body = RunLowLevel { let body = RunLowLevel {

View file

@ -1020,107 +1020,35 @@ fn call_with_args<'a, 'ctx, 'env>(
_parent: FunctionValue<'ctx>, _parent: FunctionValue<'ctx>,
args: &[(BasicValueEnum<'ctx>, &'a Layout<'a>)], args: &[(BasicValueEnum<'ctx>, &'a Layout<'a>)],
) -> BasicValueEnum<'ctx> { ) -> BasicValueEnum<'ctx> {
match symbol { let fn_name = layout_ids
Symbol::LIST_SINGLE => { .get(symbol, layout)
// List.single : a -> List a .to_symbol_string(symbol, &env.interns);
debug_assert!(args.len() == 1); let fn_val = env
.module
let (elem, elem_layout) = args[0]; .get_function(fn_name.as_str())
.unwrap_or_else(|| {
let builder = env.builder; if symbol.is_builtin() {
let ctx = env.context; panic!("Unrecognized builtin function: {:?}", symbol)
} else {
let elem_type = basic_type_from_layout(env.arena, ctx, elem_layout, env.ptr_bytes); panic!("Unrecognized non-builtin function: {:?}", symbol)
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);
let fn_val = env
.module
.get_function(fn_name.as_str())
.unwrap_or_else(|| {
if symbol.is_builtin() {
panic!("Unrecognized builtin function: {:?}", symbol)
} else {
panic!("Unrecognized non-builtin function: {:?}", symbol)
}
});
let mut arg_vals: Vec<BasicValueEnum> = Vec::with_capacity_in(args.len(), env.arena);
for (arg, _layout) in args.iter() {
arg_vals.push(*arg);
} }
});
let call = env let mut arg_vals: Vec<BasicValueEnum> = Vec::with_capacity_in(args.len(), env.arena);
.builder
.build_call(fn_val, arg_vals.into_bump_slice(), "call");
call.set_call_convention(fn_val.get_call_conventions()); for (arg, _layout) in args.iter() {
arg_vals.push(*arg);
call.try_as_basic_value()
.left()
.unwrap_or_else(|| panic!("LLVM error: Invalid call by name for name {:?}", symbol))
}
} }
let call = env
.builder
.build_call(fn_val, arg_vals.into_bump_slice(), "call");
call.set_call_convention(fn_val.get_call_conventions());
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>( fn call_intrinsic<'a, 'ctx, 'env>(
@ -1374,6 +1302,74 @@ fn run_low_level<'a, 'ctx, 'env>(
BasicValueEnum::IntValue(answer) 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 => { NumAbs | NumNeg | NumRound | NumSqrt | NumSin | NumCos | NumToFloat => {
debug_assert_eq!(args.len(), 1); debug_assert_eq!(args.len(), 1);

View file

@ -8,6 +8,7 @@ pub enum LowLevel {
ListSet, ListSet,
ListSetInPlace, ListSetInPlace,
ListIsEmpty, ListIsEmpty,
ListSingle,
NumAdd, NumAdd,
NumSub, NumSub,
NumMul, NumMul,

View file

@ -755,9 +755,9 @@ fn annotate_low_level_usage(
} }
} }
NumAdd | NumSub | NumMul | NumGt | NumGte | NumLt | NumLte | NumAbs | NumNeg ListSingle | NumAdd | NumSub | NumMul | NumGt | NumGte | NumLt | NumLte | NumAbs
| NumDivUnchecked | NumRemUnchecked | NumSqrt | NumRound | NumSin | NumCos | Eq | NotEq | NumNeg | NumDivUnchecked | NumRemUnchecked | NumSqrt | NumRound | NumSin | NumCos
| And | Or | Not | NumToFloat => { | Eq | NotEq | And | Or | Not | NumToFloat => {
for (_, arg) in args { for (_, arg) in args {
annotate_usage(&arg, usage); annotate_usage(&arg, usage);
} }