simplify string splitting LLVM codegen

This commit is contained in:
Folkert 2022-07-06 01:12:03 +02:00
parent 2455819362
commit 52605fcca9
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
5 changed files with 19 additions and 66 deletions

View file

@ -13,9 +13,7 @@ use crate::llvm::build_list::{
list_replace_unsafe, list_sort_with, list_sublist, list_swap, list_symbol_to_c_abi,
list_to_c_abi, list_with_capacity,
};
use crate::llvm::build_str::{
str_from_float, str_from_int, str_from_utf8, str_from_utf8_range, str_split,
};
use crate::llvm::build_str::{str_from_float, str_from_int, str_from_utf8, str_from_utf8_range};
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,
@ -5311,7 +5309,10 @@ fn run_low_level<'a, 'ctx, 'env>(
// Str.split : Str, Str -> List Str
debug_assert_eq!(args.len(), 2);
str_split(env, scope, args[0], args[1])
let string = load_symbol(scope, &args[0]);
let delimiter = load_symbol(scope, &args[1]);
call_list_bitcode_fn(env, &[string, delimiter], bitcode::STR_STR_SPLIT)
}
StrIsEmpty => {
// Str.isEmpty : Str -> Str

View file

@ -1,6 +1,6 @@
use crate::llvm::bitcode::{call_bitcode_fn, call_str_bitcode_fn, call_void_bitcode_fn};
use crate::llvm::build::{Env, Scope};
use crate::llvm::build_list::{allocate_list, pass_update_mode, store_list};
use crate::llvm::build_list::pass_update_mode;
use inkwell::builder::Builder;
use inkwell::values::{BasicValueEnum, IntValue, PointerValue, StructValue};
use inkwell::AddressSpace;
@ -10,48 +10,12 @@ use roc_module::symbol::Symbol;
use roc_mono::layout::{Builtin, Layout};
use roc_target::PtrWidth;
use super::bitcode::call_list_bitcode_fn;
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();
/// Str.split : Str, Str -> List Str
pub fn str_split<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
scope: &Scope<'a, 'ctx>,
str_symbol: Symbol,
delimiter_symbol: Symbol,
) -> BasicValueEnum<'ctx> {
let builder = env.builder;
let string = load_symbol(scope, &str_symbol);
let delimiter = load_symbol(scope, &delimiter_symbol);
let segment_count =
call_bitcode_fn(env, &[string, delimiter], bitcode::STR_COUNT_SEGMENTS).into_int_value();
// a pointer to the elements
let ret_list_ptr = allocate_list(env, &Layout::Builtin(Builtin::Str), segment_count);
// get the RocStr type defined by zig
let roc_str_type = env.module.get_struct_type("str.RocStr").unwrap();
// convert `*mut { *mut u8, i64 }` to `*mut RocStr`
let ret_list_ptr_zig_rocstr = builder.build_bitcast(
ret_list_ptr,
roc_str_type.ptr_type(AddressSpace::Generic),
"convert_to_zig_rocstr",
);
call_void_bitcode_fn(
env,
&[ret_list_ptr_zig_rocstr, string, delimiter],
bitcode::STR_STR_SPLIT_IN_PLACE,
);
store_list(env, ret_list_ptr, segment_count)
}
pub fn str_symbol_to_c_abi<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
scope: &Scope<'a, 'ctx>,