Mostly implemented big and small string work for Str.concat. Just some bugs somewhere in the llvm

This commit is contained in:
Chad Stearns 2020-09-12 17:39:41 -04:00
parent 89ca6735eb
commit a6c3d8edef
4 changed files with 82 additions and 55 deletions

View file

@ -1,5 +1,4 @@
interface Str exposes [ Str, isEmpty, join ] imports [] interface Str exposes [ Str, isEmpty, join ] imports []
## Types ## Types
## Dealing with text is a deep topic, so by design, Roc's `Str` module sticks ## Dealing with text is a deep topic, so by design, Roc's `Str` module sticks

View file

@ -269,7 +269,7 @@ pub fn build_exp_literal<'a, 'ctx, 'env>(
&[ctx &[ctx
.i8_type() .i8_type()
.const_int(((env.ptr_bytes * 2) - 1) as u64, false)], .const_int(((env.ptr_bytes * 2) - 1) as u64, false)],
"final_byte", "str_literal_final_byte",
) )
}; };

View file

@ -1,14 +1,12 @@
use crate::llvm::build::{ptr_from_symbol, Env, Scope}; use crate::llvm::build::{ptr_from_symbol, Env, Scope};
use crate::llvm::build_list::{ use crate::llvm::build_list::{
allocate_list, build_basic_phi2, clone_nonempty_list, empty_list, incrementing_elem_loop, allocate_list, build_basic_phi2, empty_list, incrementing_elem_loop, list_is_not_empty,
list_is_not_empty, list_len, load_list_ptr, store_list, LoopListArg, list_len, load_list_ptr, store_list, LoopListArg,
}; };
use crate::llvm::convert::{basic_type_from_layout, collection, get_ptr_type, ptr_int}; use crate::llvm::convert::{collection, ptr_int};
use inkwell::builder::Builder; use inkwell::types::BasicTypeEnum;
use inkwell::context::Context;
use inkwell::types::{BasicTypeEnum, PointerType};
use inkwell::values::{BasicValueEnum, FunctionValue, IntValue, PointerValue, StructValue}; use inkwell::values::{BasicValueEnum, FunctionValue, IntValue, PointerValue, StructValue};
use inkwell::AddressSpace; use inkwell::{AddressSpace, IntPredicate};
use roc_module::symbol::Symbol; use roc_module::symbol::Symbol;
use roc_mono::layout::{Builtin, Layout}; use roc_mono::layout::{Builtin, Layout};
@ -201,7 +199,6 @@ pub fn str_concat<'a, 'ctx, 'env>(
) )
} }
/// Str.len : Str -> Int
pub fn str_len<'a, 'ctx, 'env>( pub fn str_len<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>, env: &Env<'a, 'ctx, 'env>,
parent: FunctionValue<'ctx>, parent: FunctionValue<'ctx>,
@ -240,45 +237,44 @@ fn load_str<'a, 'ctx, 'env, Callback>(
env: &Env<'a, 'ctx, 'env>, env: &Env<'a, 'ctx, 'env>,
parent: FunctionValue<'ctx>, parent: FunctionValue<'ctx>,
wrapper_ptr: PointerValue<'ctx>, wrapper_ptr: PointerValue<'ctx>,
mut cb: Callback, cb: Callback,
) -> BasicValueEnum<'ctx> ) -> BasicValueEnum<'ctx>
where where
Callback: FnMut(PointerValue<'ctx>, IntValue<'ctx>, Smallness) -> BasicValueEnum<'ctx>, Callback: Fn(PointerValue<'ctx>, IntValue<'ctx>, Smallness) -> BasicValueEnum<'ctx>,
{ {
// let builder = env.builder; let builder = env.builder;
// let ctx = env.context; let ctx = env.context;
//
// let if_small = |final_byte| { let if_small = |final_byte| {
// let bitmask = ctx.i8_type().const_int(0b0111_1111, false); let bitmask = ctx.i8_type().const_int(0b0111_1111, false);
//
// let len = builder.build_and(final_byte, bitmask, "small_str_length"); let len = builder.build_and(final_byte, bitmask, "small_str_length");
//
// cb( cb(
// wrapper_ptr, wrapper_ptr,
// builder.build_int_cast(len, env.ptr_int(), "len_as_usize"), builder.build_int_cast(len, env.ptr_int(), "len_as_usize"),
// Smallness::Small, Smallness::Small,
// ) )
// }; };
//
// let if_big = |wrapper_struct| { let if_big = |wrapper_struct| {
// let list_ptr = load_list_ptr( let list_ptr = load_list_ptr(
// builder, builder,
// wrapper_struct, wrapper_struct,
// env.context.i8_type().ptr_type(AddressSpace::Generic), env.context.i8_type().ptr_type(AddressSpace::Generic),
// ); );
//
// cb(list_ptr, list_len(builder, wrapper_struct), Smallness::Big) cb(list_ptr, list_len(builder, wrapper_struct), Smallness::Big)
// }; };
//
// if_small_str( if_small_str(
// env, env,
// parent, parent,
// wrapper_ptr, wrapper_ptr,
// if_small, if_small,
// if_big, if_big,
// BasicTypeEnum::IntType(env.ptr_int()), BasicTypeEnum::IntType(env.ptr_int()),
// ) )
panic!("TODO uncomment this implementation")
} }
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
@ -365,15 +361,10 @@ where
let builder = env.builder; let builder = env.builder;
let ctx = env.context; let ctx = env.context;
let ptr_bytes = env.ptr_bytes; let array_ptr_type = ctx.i8_type().ptr_type(AddressSpace::Generic);
let array_ptr_type = ctx
.i8_type()
.array_type(ptr_bytes * 2)
.ptr_type(AddressSpace::Generic);
let byte_array_ptr = builder let byte_array_ptr = builder
.build_bitcast(wrapper_ptr, array_ptr_type, "str_as_array") .build_bitcast(wrapper_ptr, array_ptr_type, "str_as_array_ptr")
.into_pointer_value(); .into_pointer_value();
let final_byte_ptr = unsafe { let final_byte_ptr = unsafe {
@ -387,12 +378,19 @@ where
}; };
let final_byte = builder let final_byte = builder
.build_load(final_byte_ptr, "final_byte") .build_load(final_byte_ptr, "load_final_byte")
.into_int_value(); .into_int_value();
let bitmask = ctx.i8_type().const_int(0b1000_0000, false); let bitmask = ctx.i8_type().const_int(0b1000_0000, false);
let is_small = builder.build_and(final_byte, bitmask, "is_small"); let is_small_i8 = builder.build_int_compare(
IntPredicate::NE,
ctx.i8_type().const_zero(),
builder.build_and(final_byte, bitmask, "is_small"),
"is_small_comparison",
);
let is_small = builder.build_int_cast(is_small_i8, ctx.bool_type(), "is_small_as_bool");
build_basic_phi2( build_basic_phi2(
env, env,

View file

@ -19,6 +19,10 @@ mod gen_str {
assert_evals_to!("Str.concat \"\" \"second str\"", "second str", &'static str); assert_evals_to!("Str.concat \"\" \"second str\"", "second str", &'static str);
assert_evals_to!("Str.concat \"first str\" \"\"", "first str", &'static str); assert_evals_to!("Str.concat \"first str\" \"\"", "first str", &'static str);
assert_evals_to!("Str.concat \"\" \"\"", "", &'static str); assert_evals_to!("Str.concat \"\" \"\"", "", &'static str);
}
#[test]
fn big_str_concat() {
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"
@ -32,6 +36,32 @@ mod gen_str {
); );
} }
#[test]
fn small_str_concat() {
assert_evals_to!(
"Str.concat \"JJJJJJJ\" \"JJJJJJJJ\"",
[
0x4a,
0x4a,
0x4a,
0x4a,
0x4a,
0x4a,
0x4a,
0x4a,
0x4a,
0x4a,
0x4a,
0x4a,
0x4a,
0x4a,
0x4a,
0b1000_1111
],
[u8; 16]
);
}
#[test] #[test]
fn small_str() { fn small_str() {
assert_evals_to!( assert_evals_to!(