Merge remote-tracking branch 'origin/trunk' into roc-exposed-return

This commit is contained in:
Brendan Hansknecht 2022-07-09 09:18:48 -07:00
commit b3fa1d2dc2
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
99 changed files with 2209 additions and 4380 deletions

View file

@ -8,12 +8,12 @@ use crate::llvm::build_dict::{
};
use crate::llvm::build_hash::generic_hash;
use crate::llvm::build_list::{
self, allocate_list, empty_polymorphic_list, list_append, list_concat, list_drop_at,
self, allocate_list, empty_polymorphic_list, list_append_unsafe, list_concat, list_drop_at,
list_get_unsafe, list_len, list_map, list_map2, list_map3, list_map4, list_prepend,
list_replace_unsafe, list_sort_with, list_sublist, list_swap, list_symbol_to_c_abi,
list_to_c_abi, list_with_capacity,
list_replace_unsafe, list_reserve, list_sort_with, list_sublist, list_swap,
list_symbol_to_c_abi, list_to_c_abi, list_with_capacity, pass_update_mode,
};
use crate::llvm::build_str::{str_from_float, str_from_int, str_from_utf8, str_from_utf8_range};
use crate::llvm::build_str::{str_from_float, str_from_int};
use crate::llvm::compare::{generic_eq, generic_neq};
use crate::llvm::convert::{
self, argument_type_from_layout, basic_type_from_builtin, basic_type_from_layout,
@ -5358,18 +5358,31 @@ fn run_low_level<'a, 'ctx, 'env>(
str_from_float(env, scope, args[0])
}
StrFromUtf8 => {
// Str.fromUtf8 : List U8 -> Result Str Utf8Problem
debug_assert_eq!(args.len(), 1);
str_from_utf8(env, scope, args[0], update_mode)
}
StrFromUtf8Range => {
debug_assert_eq!(args.len(), 2);
debug_assert_eq!(args.len(), 3);
let count_and_start = load_symbol(scope, &args[1]).into_struct_value();
let list = args[0];
let start = load_symbol(scope, &args[1]);
let count = load_symbol(scope, &args[2]);
str_from_utf8_range(env, scope, args[0], count_and_start)
let result_type = env.module.get_struct_type("str.FromUtf8Result").unwrap();
let result_ptr = env
.builder
.build_alloca(result_type, "alloca_utf8_validate_bytes_result");
call_void_bitcode_fn(
env,
&[
result_ptr.into(),
list_symbol_to_c_abi(env, scope, list).into(),
start,
count,
pass_update_mode(env, update_mode),
],
bitcode::STR_FROM_UTF8_RANGE,
);
crate::llvm::build_str::decode_from_utf8_result(env, result_ptr).into()
}
StrToUtf8 => {
// Str.fromInt : Str -> List U8
@ -5513,14 +5526,24 @@ fn run_low_level<'a, 'ctx, 'env>(
list_concat(env, first_list, second_list, element_layout)
}
ListAppend => {
// List.append : List elem, elem -> List elem
ListAppendUnsafe => {
// List.appendUnsafe : List elem, elem -> List elem
debug_assert_eq!(args.len(), 2);
let original_wrapper = load_symbol(scope, &args[0]).into_struct_value();
let (elem, elem_layout) = load_symbol_and_layout(scope, &args[1]);
list_append(env, original_wrapper, elem, elem_layout, update_mode)
list_append_unsafe(env, original_wrapper, elem, elem_layout)
}
ListReserve => {
// List.reserve : List elem, Nat -> List elem
debug_assert_eq!(args.len(), 2);
let (list, list_layout) = load_symbol_and_layout(scope, &args[0]);
let element_layout = list_element_layout!(list_layout);
let spare = load_symbol(scope, &args[1]);
list_reserve(env, list, spare, element_layout, update_mode)
}
ListSwap => {
// List.swap : List elem, Nat, Nat -> List elem
@ -5543,10 +5566,6 @@ fn run_low_level<'a, 'ctx, 'env>(
)
}
ListSublist => {
// List.sublist : List elem, { start : Nat, len : Nat } -> List elem
//
// As a low-level, record is destructed
// List.sublist : List elem, start : Nat, len : Nat -> List elem
debug_assert_eq!(args.len(), 3);
let (list, list_layout) = load_symbol_and_layout(scope, &args[0]);

View file

@ -146,24 +146,42 @@ pub fn list_get_unsafe<'a, 'ctx, 'env>(
result
}
/// List.append : List elem, elem -> List elem
pub fn list_append<'a, 'ctx, 'env>(
/// List.reserve : List elem, Nat -> List elem
pub fn list_reserve<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
original_wrapper: StructValue<'ctx>,
element: BasicValueEnum<'ctx>,
list: BasicValueEnum<'ctx>,
spare: BasicValueEnum<'ctx>,
element_layout: &Layout<'a>,
update_mode: UpdateMode,
) -> BasicValueEnum<'ctx> {
call_list_bitcode_fn(
env,
&[
list_to_c_abi(env, original_wrapper.into()).into(),
list_to_c_abi(env, list).into(),
env.alignment_intvalue(element_layout),
pass_element_as_opaque(env, element, *element_layout),
spare,
layout_width(env, element_layout),
pass_update_mode(env, update_mode),
],
bitcode::LIST_APPEND,
bitcode::LIST_RESERVE,
)
}
/// List.appendUnsafe : List elem, elem -> List elem
pub fn list_append_unsafe<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
original_wrapper: StructValue<'ctx>,
element: BasicValueEnum<'ctx>,
element_layout: &Layout<'a>,
) -> BasicValueEnum<'ctx> {
call_list_bitcode_fn(
env,
&[
list_to_c_abi(env, original_wrapper.into()).into(),
pass_element_as_opaque(env, element, *element_layout),
layout_width(env, element_layout),
],
bitcode::LIST_APPEND_UNSAFE,
)
}
@ -807,6 +825,7 @@ pub fn store_list<'a, 'ctx, 'env>(
.build_insert_value(struct_val, len, Builtin::WRAPPER_LEN, "insert_len")
.unwrap();
// Store the capacity
struct_val = builder
.build_insert_value(
struct_val,

View file

@ -1,17 +1,14 @@
use crate::llvm::bitcode::{call_bitcode_fn, call_str_bitcode_fn, call_void_bitcode_fn};
use crate::llvm::bitcode::{call_bitcode_fn, call_str_bitcode_fn};
use crate::llvm::build::{Env, Scope};
use crate::llvm::build_list::pass_update_mode;
use inkwell::builder::Builder;
use inkwell::values::{BasicValueEnum, IntValue, PointerValue, StructValue};
use inkwell::AddressSpace;
use morphic_lib::UpdateMode;
use roc_builtins::bitcode::{self, IntWidth};
use roc_module::symbol::Symbol;
use roc_mono::layout::{Builtin, Layout};
use roc_target::PtrWidth;
use super::build::{create_entry_block_alloca, load_symbol};
use super::build_list::list_symbol_to_c_abi;
pub static CHAR_LAYOUT: Layout = Layout::u8();
@ -70,7 +67,7 @@ pub fn str_from_int<'a, 'ctx, 'env>(
call_str_bitcode_fn(env, &[value.into()], &bitcode::STR_FROM_INT[int_width])
}
fn decode_from_utf8_result<'a, 'ctx, 'env>(
pub fn decode_from_utf8_result<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
pointer: PointerValue<'ctx>,
) -> StructValue<'ctx> {
@ -106,67 +103,6 @@ fn decode_from_utf8_result<'a, 'ctx, 'env>(
}
}
/// Str.fromUtf8 : List U8, { count : Nat, start : Nat } -> { a : Bool, b : Str, c : Nat, d : I8 }
pub fn str_from_utf8_range<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
scope: &Scope<'a, 'ctx>,
list: Symbol,
count_and_start: StructValue<'ctx>,
) -> BasicValueEnum<'ctx> {
let builder = env.builder;
let result_type = env.module.get_struct_type("str.FromUtf8Result").unwrap();
let result_ptr = builder.build_alloca(result_type, "alloca_utf8_validate_bytes_result");
let count = env
.builder
.build_extract_value(count_and_start, 0, "get_count")
.unwrap();
let start = env
.builder
.build_extract_value(count_and_start, 1, "get_start")
.unwrap();
call_void_bitcode_fn(
env,
&[
result_ptr.into(),
list_symbol_to_c_abi(env, scope, list).into(),
count,
start,
],
bitcode::STR_FROM_UTF8_RANGE,
);
decode_from_utf8_result(env, result_ptr).into()
}
/// Str.fromUtf8 : List U8 -> { a : Bool, b : Str, c : Nat, d : I8 }
pub fn str_from_utf8<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
scope: &Scope<'a, 'ctx>,
list: Symbol,
update_mode: UpdateMode,
) -> BasicValueEnum<'ctx> {
let builder = env.builder;
let result_type = env.module.get_struct_type("str.FromUtf8Result").unwrap();
let result_ptr = builder.build_alloca(result_type, "alloca_utf8_validate_bytes_result");
call_void_bitcode_fn(
env,
&[
result_ptr.into(),
list_symbol_to_c_abi(env, scope, list).into(),
pass_update_mode(env, update_mode),
],
bitcode::STR_FROM_UTF8,
);
decode_from_utf8_result(env, result_ptr).into()
}
/// Str.fromFloat : Int -> Str
pub fn str_from_float<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,