Merge remote-tracking branch 'origin/trunk' into recursive-layouts

This commit is contained in:
Folkert 2020-09-07 16:53:15 +02:00
commit f857203673
64 changed files with 5025 additions and 2214 deletions

View file

@ -1,9 +1,9 @@
use crate::layout_id::LayoutIds;
use crate::llvm::build_list::{
allocate_list, build_basic_phi2, clone_nonempty_list, empty_list, empty_polymorphic_list,
incrementing_index_loop, list_append, list_concat, list_get_unsafe, list_is_not_empty,
list_join, list_len, list_prepend, list_repeat, list_reverse, list_set, list_single,
load_list_ptr,
incrementing_elem_loop, list_append, list_concat, list_get_unsafe, list_is_not_empty,
list_join, list_keep_if, list_len, list_map, list_prepend, list_repeat, list_reverse, list_set,
list_single, list_walk_right, load_list_ptr, store_list, LoopListArg,
};
use crate::llvm::compare::{build_eq, build_neq};
use crate::llvm::convert::{
@ -46,7 +46,6 @@ pub enum OptLevel {
Optimize,
}
// pub type Scope<'a, 'ctx> = ImMap<Symbol, (Layout<'a>, PointerValue<'ctx>)>;
#[derive(Default, Debug, Clone, PartialEq)]
pub struct Scope<'a, 'ctx> {
symbols: ImMap<Symbol, (Layout<'a>, PointerValue<'ctx>)>,
@ -1999,9 +1998,9 @@ fn run_low_level<'a, 'ctx, 'env>(
// List.reverse : List elem -> List elem
debug_assert_eq!(args.len(), 1);
let list = &args[0];
let (list, list_layout) = load_symbol_and_layout(env, scope, &args[0]);
list_reverse(env, parent, scope, list)
list_reverse(env, parent, list, list_layout)
}
ListConcat => {
debug_assert_eq!(args.len(), 2);
@ -2012,6 +2011,47 @@ fn run_low_level<'a, 'ctx, 'env>(
list_concat(env, parent, first_list, second_list, list_layout)
}
ListMap => {
// List.map : List before, (before -> after) -> List after
debug_assert_eq!(args.len(), 2);
let (list, list_layout) = load_symbol_and_layout(env, scope, &args[0]);
let (func, func_layout) = load_symbol_and_layout(env, scope, &args[1]);
list_map(env, parent, func, func_layout, list, list_layout)
}
ListKeepIf => {
// List.keepIf : List elem, (elem -> Bool) -> List elem
debug_assert_eq!(args.len(), 2);
let (list, list_layout) = load_symbol_and_layout(env, scope, &args[0]);
let (func, func_layout) = load_symbol_and_layout(env, scope, &args[1]);
list_keep_if(env, parent, func, func_layout, list, list_layout)
}
ListWalkRight => {
// List.walkRight : List elem, (elem -> accum -> accum), accum -> accum
debug_assert_eq!(args.len(), 3);
let (list, list_layout) = load_symbol_and_layout(env, scope, &args[0]);
let (func, func_layout) = load_symbol_and_layout(env, scope, &args[1]);
let (default, default_layout) = load_symbol_and_layout(env, scope, &args[2]);
list_walk_right(
env,
parent,
list,
list_layout,
func,
func_layout,
default,
default_layout,
)
}
ListAppend => {
// List.append : List elem, elem -> List elem
debug_assert_eq!(args.len(), 2);
@ -2035,9 +2075,8 @@ fn run_low_level<'a, 'ctx, 'env>(
debug_assert_eq!(args.len(), 1);
let (list, outer_list_layout) = load_symbol_and_layout(env, scope, &args[0]);
let outer_wrapper_struct = list.into_struct_value();
list_join(env, parent, outer_wrapper_struct, outer_list_layout)
list_join(env, parent, list, outer_list_layout)
}
NumAbs | NumNeg | NumRound | NumSqrtUnchecked | NumSin | NumCos | NumToFloat => {
debug_assert_eq!(args.len(), 1);
@ -2317,14 +2356,9 @@ fn str_concat<'a, 'ctx, 'env>(
let combined_str_ptr = allocate_list(env, &CHAR_LAYOUT, combined_str_len);
// FIRST LOOP
let first_loop = |first_index| {
let first_str_ptr = load_list_ptr(builder, first_str_wrapper, ptr_type);
// The pointer to the element in the first list
let first_str_char_ptr = unsafe {
builder.build_in_bounds_gep(first_str_ptr, &[first_index], "load_index")
};
let first_str_ptr = load_list_ptr(builder, first_str_wrapper, ptr_type);
let first_loop = |first_index, first_str_elem| {
// The pointer to the element in the combined list
let combined_str_elem_ptr = unsafe {
builder.build_in_bounds_gep(
@ -2334,19 +2368,20 @@ fn str_concat<'a, 'ctx, 'env>(
)
};
let first_str_elem = builder.build_load(first_str_char_ptr, "get_elem");
// Mutate the new array in-place to change the element.
builder.build_store(combined_str_elem_ptr, first_str_elem);
};
let index_name = "#index";
let index_alloca = incrementing_index_loop(
let index_alloca = incrementing_elem_loop(
builder,
parent,
ctx,
first_str_len,
LoopListArg {
ptr: first_str_ptr,
len: first_str_len,
},
index_name,
None,
first_loop,
@ -2356,14 +2391,9 @@ fn str_concat<'a, 'ctx, 'env>(
builder.build_store(index_alloca, ctx.i64_type().const_int(0, false));
// SECOND LOOP
let second_loop = |second_index| {
let second_str_ptr = load_list_ptr(builder, second_str_wrapper, ptr_type);
// The pointer to the element in the second list
let second_str_char_ptr = unsafe {
builder.build_in_bounds_gep(second_str_ptr, &[second_index], "load_index")
};
let second_str_ptr = load_list_ptr(builder, second_str_wrapper, ptr_type);
let second_loop = |second_index, second_str_elem| {
// The pointer to the element in the combined str.
// Note that the pointer does not start at the index
// 0, it starts at the index of first_str_len. In that
@ -2382,55 +2412,24 @@ fn str_concat<'a, 'ctx, 'env>(
)
};
let second_str_elem = builder.build_load(second_str_char_ptr, "get_elem");
// Mutate the new array in-place to change the element.
builder.build_store(combined_str_char_ptr, second_str_elem);
};
incrementing_index_loop(
incrementing_elem_loop(
builder,
parent,
ctx,
second_str_len,
LoopListArg {
ptr: second_str_ptr,
len: second_str_len,
},
index_name,
Some(index_alloca),
second_loop,
);
let ptr_bytes = env.ptr_bytes;
let int_type = ptr_int(ctx, ptr_bytes);
let ptr_as_int = builder.build_ptr_to_int(combined_str_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,
combined_str_len,
Builtin::WRAPPER_LEN,
"insert_len",
)
.unwrap();
builder.build_bitcast(
struct_val.into_struct_value(),
collection(ctx, ptr_bytes),
"cast_collection",
)
store_list(env, combined_str_ptr, combined_str_len)
};
build_basic_phi2(