Separate out str_is_not_empty

Also have list_is_not_empty use ptr_int instead of i64
This commit is contained in:
Richard Feldman 2020-09-12 21:37:25 -04:00
parent 648758f752
commit 8e8a41079e
2 changed files with 26 additions and 25 deletions

View file

@ -231,7 +231,7 @@ pub fn list_join<'a, 'ctx, 'env>(
// outer_list_len > 0
// We do this check to avoid allocating memory. If the input
// list is empty, then we can just return an empty list.
let comparison = list_is_not_empty(builder, ctx, outer_list_len);
let comparison = list_is_not_empty(env, outer_list_len);
let build_then = || {
let list_len_sum_name = "#listslengthsum";
@ -284,7 +284,7 @@ pub fn list_join<'a, 'ctx, 'env>(
let inner_list_len = list_len(builder, inner_list_wrapper);
// inner_list_len > 0
let inner_list_comparison = list_is_not_empty(builder, ctx, inner_list_len);
let inner_list_comparison = list_is_not_empty(env, inner_list_len);
let inner_list_non_empty_block =
ctx.append_basic_block(parent, "inner_list_non_empty");
@ -1011,14 +1011,13 @@ pub fn list_concat<'a, 'ctx, 'env>(
// first_list_len > 0
// We do this check to avoid allocating memory. If the first input
// list is empty, then we can just return the second list cloned
let first_list_length_comparison = list_is_not_empty(builder, ctx, first_list_len);
let first_list_length_comparison = list_is_not_empty(env, first_list_len);
let if_first_list_is_empty = || {
// second_list_len > 0
// We do this check to avoid allocating memory. If the second input
// list is empty, then we can just return an empty list
let second_list_length_comparison =
list_is_not_empty(builder, ctx, second_list_len);
let second_list_length_comparison = list_is_not_empty(env, second_list_len);
let build_second_list_then = || {
let elem_type =
@ -1065,8 +1064,7 @@ pub fn list_concat<'a, 'ctx, 'env>(
// second_list_len > 0
// We do this check to avoid allocating memory. If the second input
// list is empty, then we can just return the first list cloned
let second_list_length_comparison =
list_is_not_empty(builder, ctx, second_list_len);
let second_list_length_comparison = list_is_not_empty(env, second_list_len);
let if_second_list_is_not_empty = || {
let combined_list_len =
@ -1390,16 +1388,12 @@ pub fn empty_list<'a, 'ctx, 'env>(env: &Env<'a, 'ctx, 'env>) -> BasicValueEnum<'
BasicValueEnum::StructValue(struct_type.const_zero())
}
pub fn list_is_not_empty<'ctx>(
builder: &Builder<'ctx>,
ctx: &'ctx Context,
list_len: IntValue<'ctx>,
) -> IntValue<'ctx> {
builder.build_int_compare(
fn list_is_not_empty<'ctx>(env: &Env<'_, 'ctx, '_>, len: IntValue<'ctx>) -> IntValue<'ctx> {
env.builder.build_int_compare(
IntPredicate::UGT,
list_len,
ctx.i64_type().const_int(0, false),
"greaterthanzero",
len,
env.ptr_int().const_zero(),
"list_len_is_nonzero",
)
}

View file

@ -1,7 +1,7 @@
use crate::llvm::build::{ptr_from_symbol, Env, Scope};
use crate::llvm::build_list::{
allocate_list, build_basic_phi2, empty_list, incrementing_elem_loop, list_is_not_empty,
list_len, load_list_ptr, store_list, LoopListArg,
allocate_list, build_basic_phi2, empty_list, incrementing_elem_loop, list_len, load_list_ptr,
store_list, LoopListArg,
};
use crate::llvm::convert::{collection, ptr_int};
use inkwell::types::BasicTypeEnum;
@ -10,6 +10,8 @@ use inkwell::{AddressSpace, IntPredicate};
use roc_module::symbol::Symbol;
use roc_mono::layout::{Builtin, Layout};
pub static CHAR_LAYOUT: Layout = Layout::Builtin(Builtin::Int8);
/// Str.concat : Str, Str -> Str
pub fn str_concat<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
@ -41,15 +43,13 @@ pub fn str_concat<'a, 'ctx, 'env>(
// first_str_len > 0
// We do this check to avoid allocating memory. If the first input
// str is empty, then we can just return the second str cloned
let first_str_length_comparison =
list_is_not_empty(builder, ctx, first_str_len);
let first_str_length_comparison = str_is_not_empty(env, first_str_len);
let if_first_str_is_empty = || {
// second_str_len > 0
// We do this check to avoid allocating memory. If the second input
// str is empty, then we can just return an empty str
let second_str_length_comparison =
list_is_not_empty(builder, ctx, second_str_len);
let second_str_length_comparison = str_is_not_empty(env, second_str_len);
let if_second_str_is_nonempty = || {
let (new_wrapper, _) = clone_nonempty_str(
@ -89,8 +89,7 @@ pub fn str_concat<'a, 'ctx, 'env>(
// second_str_len > 0
// We do this check to avoid allocating memory. If the second input
// str is empty, then we can just return the first str cloned
let second_str_length_comparison =
list_is_not_empty(builder, ctx, second_str_len);
let second_str_length_comparison = str_is_not_empty(env, second_str_len);
let if_second_str_is_not_empty = || {
let combined_str_len = builder.build_int_add(
@ -426,4 +425,12 @@ where
ret_type,
)
}
pub static CHAR_LAYOUT: Layout = Layout::Builtin(Builtin::Int8);
fn str_is_not_empty<'ctx>(env: &Env<'_, 'ctx, '_>, len: IntValue<'ctx>) -> IntValue<'ctx> {
env.builder.build_int_compare(
IntPredicate::UGT,
len,
env.ptr_int().const_zero(),
"str_len_is_nonzero",
)
}