mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-14 07:45:04 +00:00
improve lowlevel unwrapping
This commit is contained in:
parent
c0ace1ee0b
commit
e8530eaca5
12 changed files with 249 additions and 246 deletions
|
@ -11,9 +11,9 @@ use crate::llvm::build_list::{
|
|||
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_reserve, list_sort_with, list_sublist, list_swap,
|
||||
list_symbol_to_c_abi, list_to_c_abi, list_with_capacity,
|
||||
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,
|
||||
|
@ -5354,18 +5354,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
|
||||
|
@ -5549,26 +5562,13 @@ 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(), 2);
|
||||
debug_assert_eq!(args.len(), 3);
|
||||
|
||||
let (list, list_layout) = load_symbol_and_layout(scope, &args[0]);
|
||||
let original_wrapper = list.into_struct_value();
|
||||
|
||||
let record = load_symbol(scope, &args[1]).into_struct_value();
|
||||
|
||||
let len = env
|
||||
.builder
|
||||
.build_extract_value(record, 0, "get_len")
|
||||
.unwrap();
|
||||
|
||||
let start = env
|
||||
.builder
|
||||
.build_extract_value(record, 1, "get_start")
|
||||
.unwrap();
|
||||
let start = load_symbol(scope, &args[1]);
|
||||
let len = load_symbol(scope, &args[2]);
|
||||
|
||||
let element_layout = list_element_layout!(list_layout);
|
||||
list_sublist(
|
||||
|
|
|
@ -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>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue