Merge branch 'trunk' into list-push

This commit is contained in:
Richard Feldman 2020-06-21 22:23:24 -04:00 committed by GitHub
commit 44b19d3174
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 182 additions and 8 deletions

View File

@ -498,7 +498,7 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
),
);
// push : List a -> a -> List a
// push : List elem -> elem -> List elem
add_type(
Symbol::LIST_PUSH,
SolvedType::Func(
@ -513,6 +513,15 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
SolvedType::Func(vec![flex(TVAR1)], Box::new(list_type(flex(TVAR1)))),
);
// repeat : Int, elem -> List elem
add_type(
Symbol::LIST_REPEAT,
SolvedType::Func(
vec![int_type(), flex(TVAR1)],
Box::new(list_type(flex(TVAR1))),
),
);
// len : List * -> Int
add_type(
Symbol::LIST_LEN,

View File

@ -615,6 +615,30 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
)
});
// repeat : Int, elem -> List elem
add_type(Symbol::LIST_REPEAT, {
let u = UVAR1;
let v = UVAR2;
let star1 = UVAR4;
let star2 = UVAR5;
let a = TVAR1;
unique_function(
vec![
int_type(star1),
SolvedType::Apply(Symbol::ATTR_ATTR, vec![disjunction(u, vec![v]), flex(a)]),
],
SolvedType::Apply(
Symbol::ATTR_ATTR,
vec![
boolean(star2),
SolvedType::Apply(Symbol::LIST_LIST, vec![attr_type(u, a)]),
],
),
)
});
// push : Attr (w | u | v) (List (Attr u a))
// , Attr (u | v) a
// -> Attr * (List (Attr u a))

View File

@ -433,11 +433,7 @@ pub fn build_expr<'a, 'ctx, 'env>(
let builder = env.builder;
if elems.is_empty() {
let struct_type = collection(ctx, env.ptr_bytes);
// The pointer should be null (aka zero) and the length should be zero,
// so the whole struct should be a const_zero
BasicValueEnum::StructValue(struct_type.const_zero())
empty_list(env)
} else {
let len_u64 = elems.len() as u64;
let elem_bytes = elem_layout.stack_size(env.ptr_bytes) as u64;
@ -1471,6 +1467,131 @@ fn call_with_args<'a, 'ctx, 'env>(
"cast_collection",
)
}
Symbol::LIST_REPEAT => {
// List.repeat : Int, elem -> List elem
debug_assert!(args.len() == 2);
// Number of repeats
let list_len = args[0].0.into_int_value();
let builder = env.builder;
let ctx = env.context;
let (elem, elem_layout) = args[1];
let elem_type = basic_type_from_layout(env.arena, ctx, elem_layout, env.ptr_bytes);
// list_len > 0
// We have to do a loop below, continuously adding the `elem`
// to the output list `List elem` until we have reached the
// number of repeats. This `comparison` is used to check
// if we need to do any looping; because if we dont, then we
// dont need to allocate memory for the index or the check
// if index != 0
let comparison = builder.build_int_compare(
IntPredicate::UGT,
list_len,
ctx.i64_type().const_int(0, false),
"atleastzero",
);
let build_then = || {
// Allocate space for the new array that we'll copy into.
let elem_bytes = elem_layout.stack_size(env.ptr_bytes) as u64;
let list_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!
};
let index_name = "#index";
let start_alloca = builder.build_alloca(ctx.i64_type(), index_name);
builder.build_store(start_alloca, list_len);
let loop_bb = ctx.append_basic_block(parent, "loop");
builder.build_unconditional_branch(loop_bb);
builder.position_at_end(loop_bb);
// #index = #index - 1
let curr_index = builder
.build_load(start_alloca, index_name)
.into_int_value();
let next_index = builder.build_int_sub(
curr_index,
ctx.i64_type().const_int(1, false),
"nextindex",
);
builder.build_store(start_alloca, next_index);
let elem_ptr =
unsafe { builder.build_in_bounds_gep(list_ptr, &[curr_index], "load_index") };
// Mutate the new array in-place to change the element.
builder.build_store(elem_ptr, elem);
// #index != 0
let end_cond = builder.build_int_compare(
IntPredicate::NE,
ctx.i64_type().const_int(0, false),
curr_index,
"loopcond",
);
let after_bb = ctx.append_basic_block(parent, "afterloop");
builder.build_conditional_branch(end_cond, loop_bb, after_bb);
builder.position_at_end(after_bb);
let ptr_bytes = env.ptr_bytes;
let int_type = ptr_int(ctx, ptr_bytes);
let ptr_as_int = builder.build_ptr_to_int(list_ptr, int_type, "list_cast_ptr");
let struct_type = collection(ctx, 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, list_len, Builtin::WRAPPER_LEN, "insert_len")
.unwrap();
builder.build_bitcast(
struct_val.into_struct_value(),
collection(ctx, ptr_bytes),
"cast_collection",
)
};
let build_else = || empty_list(env);
let struct_type = collection(ctx, env.ptr_bytes);
build_basic_phi2(
env,
parent,
comparison,
build_then,
build_else,
BasicTypeEnum::StructType(struct_type),
)
}
Symbol::INT_DIV_UNSAFE => {
debug_assert!(args.len() == 2);
@ -1637,6 +1758,16 @@ enum InPlace {
Clone,
}
fn empty_list<'a, 'ctx, 'env>(env: &Env<'a, 'ctx, 'env>) -> BasicValueEnum<'ctx> {
let ctx = env.context;
let struct_type = collection(ctx, env.ptr_bytes);
// The pointer should be null (aka zero) and the length should be zero,
// so the whole struct should be a const_zero
BasicValueEnum::StructValue(struct_type.const_zero())
}
fn bounds_check_comparison<'ctx>(
builder: &Builder<'ctx>,
elem_index: IntValue<'ctx>,

View File

@ -502,6 +502,15 @@ mod gen_builtins {
assert_evals_to!("List.single 5.6", &[5.6], &'static [f64]);
}
#[test]
fn list_repeat() {
assert_evals_to!("List.repeat 5 1", &[1, 1, 1, 1, 1], &'static [i64]);
assert_evals_to!("List.repeat 4 2", &[2, 2, 2, 2], &'static [i64]);
assert_evals_to!("List.repeat 0 []", &[], &'static [i64]);
assert_evals_to!("List.repeat 2 []", &[&[], &[]], &'static [&'static [i64]]);
}
#[test]
fn empty_list_len() {
with_larger_debug_stack(|| {

View File

@ -692,6 +692,7 @@ define_builtins! {
15 LIST_FIRST: "first"
16 LIST_FIRST_ARG: "first#list"
17 LIST_SINGLE: "single"
18 LIST_REPEAT: "repeat"
}
7 RESULT: "Result" => {
0 RESULT_RESULT: "Result" imported // the Result.Result type alias

View File

@ -2099,8 +2099,8 @@ mod test_uniq_solve {
reverse
"#
),
"Attr * (Attr * (List (Attr (a | b) c)) -> Attr (* | a | b) (List (Attr b c)))",
// "Attr * (Attr * (List (Attr (a | b) c)) -> Attr (* | a | b) (List (Attr a c)))",
// "Attr * (Attr * (List (Attr (a | b) c)) -> Attr (* | a | b) (List (Attr b c)))",
"Attr * (Attr * (List (Attr (a | b) c)) -> Attr (* | a | b) (List (Attr a c)))",
);
}