change representation of list/str

This commit is contained in:
Folkert 2020-11-21 22:45:22 +01:00
parent 52772df2c3
commit 8013af7e97
6 changed files with 45 additions and 65 deletions

View file

@ -118,7 +118,7 @@ const RocStr = struct {
// Str.split // Str.split
pub fn strSplitInPlace( pub fn strSplitInPlace(
bytes_array: [*]u128, array: [*]RocStr,
array_len: usize, array_len: usize,
str_bytes_ptrs: [*]u8, str_bytes_ptrs: [*]u8,
str_len: usize, str_len: usize,
@ -126,8 +126,6 @@ pub fn strSplitInPlace(
delimiter_len: usize delimiter_len: usize
) callconv(.C) void { ) callconv(.C) void {
var array = @ptrCast([*]RocStr, bytes_array);
var ret_array_index : usize = 0; var ret_array_index : usize = 0;
var sliceStart_index : usize = 0; var sliceStart_index : usize = 0;

View file

@ -550,11 +550,9 @@ pub fn build_exp_literal<'a, 'ctx, 'env>(
let len_type = env.ptr_int(); let len_type = env.ptr_int();
let len = len_type.const_int(bytes_len, false); let len = len_type.const_int(bytes_len, false);
// NOTE we rely on CHAR_LAYOUT turning into a `i8`
let ptr = allocate_list(env, InPlace::Clone, &CHAR_LAYOUT, len); let ptr = allocate_list(env, InPlace::Clone, &CHAR_LAYOUT, len);
let int_type = ptr_int(ctx, ptr_bytes);
let ptr_as_int = builder.build_ptr_to_int(ptr, int_type, "list_cast_ptr");
let struct_type = collection(ctx, ptr_bytes); let struct_type = collection(ctx, ptr_bytes);
let len = BasicValueEnum::IntValue(env.ptr_int().const_int(len_u64, false));
let mut struct_val; let mut struct_val;
@ -562,9 +560,9 @@ pub fn build_exp_literal<'a, 'ctx, 'env>(
struct_val = builder struct_val = builder
.build_insert_value( .build_insert_value(
struct_type.get_undef(), struct_type.get_undef(),
ptr_as_int, ptr,
Builtin::WRAPPER_PTR, Builtin::WRAPPER_PTR,
"insert_ptr", "insert_ptr_str_literal",
) )
.unwrap(); .unwrap();
@ -1168,8 +1166,10 @@ fn list_literal<'a, 'ctx, 'env>(
} }
let ptr_bytes = env.ptr_bytes; let ptr_bytes = env.ptr_bytes;
let int_type = ptr_int(ctx, ptr_bytes);
let ptr_as_int = builder.build_ptr_to_int(ptr, int_type, "list_cast_ptr"); let u8_ptr_type = ctx.i8_type().ptr_type(AddressSpace::Generic);
let generic_ptr = cast_basic_basic(builder, ptr.into(), u8_ptr_type.into());
let struct_type = collection(ctx, ptr_bytes); let struct_type = collection(ctx, ptr_bytes);
let len = BasicValueEnum::IntValue(env.ptr_int().const_int(len_u64, false)); let len = BasicValueEnum::IntValue(env.ptr_int().const_int(len_u64, false));
let mut struct_val; let mut struct_val;
@ -1178,9 +1178,9 @@ fn list_literal<'a, 'ctx, 'env>(
struct_val = builder struct_val = builder
.build_insert_value( .build_insert_value(
struct_type.get_undef(), struct_type.get_undef(),
ptr_as_int, generic_ptr,
Builtin::WRAPPER_PTR, Builtin::WRAPPER_PTR,
"insert_ptr", "insert_ptr_list_literal",
) )
.unwrap(); .unwrap();

View file

@ -1760,12 +1760,7 @@ pub fn load_list<'ctx>(
wrapper_struct: StructValue<'ctx>, wrapper_struct: StructValue<'ctx>,
ptr_type: PointerType<'ctx>, ptr_type: PointerType<'ctx>,
) -> (IntValue<'ctx>, PointerValue<'ctx>) { ) -> (IntValue<'ctx>, PointerValue<'ctx>) {
let ptr_as_int = builder let ptr = load_list_ptr(builder, wrapper_struct, ptr_type);
.build_extract_value(wrapper_struct, Builtin::WRAPPER_PTR, "read_list_ptr")
.unwrap()
.into_int_value();
let ptr = builder.build_int_to_ptr(ptr_as_int, ptr_type, "list_cast_ptr");
let length = builder let length = builder
.build_extract_value(wrapper_struct, Builtin::WRAPPER_LEN, "list_len") .build_extract_value(wrapper_struct, Builtin::WRAPPER_LEN, "list_len")
@ -1780,12 +1775,14 @@ pub fn load_list_ptr<'ctx>(
wrapper_struct: StructValue<'ctx>, wrapper_struct: StructValue<'ctx>,
ptr_type: PointerType<'ctx>, ptr_type: PointerType<'ctx>,
) -> PointerValue<'ctx> { ) -> PointerValue<'ctx> {
let ptr_as_int = builder // a `*mut u8` pointer
let generic_ptr = builder
.build_extract_value(wrapper_struct, Builtin::WRAPPER_PTR, "read_list_ptr") .build_extract_value(wrapper_struct, Builtin::WRAPPER_PTR, "read_list_ptr")
.unwrap() .unwrap()
.into_int_value(); .into_pointer_value();
builder.build_int_to_ptr(ptr_as_int, ptr_type, "list_cast_ptr") // cast to the expected pointer type
cast_basic_basic(builder, generic_ptr.into(), ptr_type.into()).into_pointer_value()
} }
pub fn clone_nonempty_list<'a, 'ctx, 'env>( pub fn clone_nonempty_list<'a, 'ctx, 'env>(
@ -1810,9 +1807,6 @@ pub fn clone_nonempty_list<'a, 'ctx, 'env>(
// Allocate space for the new array that we'll copy into. // Allocate space for the new array that we'll copy into.
let clone_ptr = allocate_list(env, inplace, elem_layout, list_len); let clone_ptr = allocate_list(env, inplace, elem_layout, list_len);
let int_type = ptr_int(ctx, ptr_bytes);
let ptr_as_int = builder.build_ptr_to_int(clone_ptr, int_type, "list_cast_ptr");
// TODO check if malloc returned null; if so, runtime error for OOM! // TODO check if malloc returned null; if so, runtime error for OOM!
// Either memcpy or deep clone the array elements // Either memcpy or deep clone the array elements
@ -1829,6 +1823,9 @@ pub fn clone_nonempty_list<'a, 'ctx, 'env>(
} }
// Create a fresh wrapper struct for the newly populated array // Create a fresh wrapper struct for the newly populated array
let u8_ptr_type = ctx.i8_type().ptr_type(AddressSpace::Generic);
let generic_ptr = cast_basic_basic(builder, clone_ptr.into(), u8_ptr_type.into());
let struct_type = collection(ctx, env.ptr_bytes); let struct_type = collection(ctx, env.ptr_bytes);
let mut struct_val; let mut struct_val;
@ -1836,9 +1833,9 @@ pub fn clone_nonempty_list<'a, 'ctx, 'env>(
struct_val = builder struct_val = builder
.build_insert_value( .build_insert_value(
struct_type.get_undef(), struct_type.get_undef(),
ptr_as_int, generic_ptr,
Builtin::WRAPPER_PTR, Builtin::WRAPPER_PTR,
"insert_ptr", "insert_ptr_clone_nonempty_list",
) )
.unwrap(); .unwrap();
@ -1915,26 +1912,28 @@ pub fn allocate_list<'a, 'ctx, 'env>(
pub fn store_list<'a, 'ctx, 'env>( pub fn store_list<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>, env: &Env<'a, 'ctx, 'env>,
list_ptr: PointerValue<'ctx>, pointer_to_first_element: PointerValue<'ctx>,
len: IntValue<'ctx>, len: IntValue<'ctx>,
) -> BasicValueEnum<'ctx> { ) -> BasicValueEnum<'ctx> {
let ctx = env.context; let ctx = env.context;
let builder = env.builder; let builder = env.builder;
let ptr_bytes = env.ptr_bytes; let ptr_bytes = env.ptr_bytes;
let int_type = ptr_int(ctx, ptr_bytes);
let ptr_as_int = builder.build_ptr_to_int(list_ptr, int_type, "list_cast_ptr");
let struct_type = collection(ctx, ptr_bytes); let struct_type = collection(ctx, ptr_bytes);
let u8_ptr_type = ctx.i8_type().ptr_type(AddressSpace::Generic);
let generic_ptr =
cast_basic_basic(builder, pointer_to_first_element.into(), u8_ptr_type.into());
let mut struct_val; let mut struct_val;
// Store the pointer // Store the pointer
struct_val = builder struct_val = builder
.build_insert_value( .build_insert_value(
struct_type.get_undef(), struct_type.get_undef(),
ptr_as_int, generic_ptr,
Builtin::WRAPPER_PTR, Builtin::WRAPPER_PTR,
"insert_ptr", "insert_ptr_store_list",
) )
.unwrap(); .unwrap();

View file

@ -60,17 +60,20 @@ pub fn str_split<'a, 'ctx, 'env>(
let ret_list_ptr = let ret_list_ptr =
allocate_list(env, inplace, &Layout::Builtin(Builtin::Str), segment_count); allocate_list(env, inplace, &Layout::Builtin(Builtin::Str), segment_count);
// convert `*mut RocStr` to `*mut i128` // get the RocStr type defined by zig
let ret_list_ptr_u128s = builder.build_bitcast( 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, ret_list_ptr,
ctx.i128_type().ptr_type(AddressSpace::Generic), roc_str_type.ptr_type(AddressSpace::Generic),
"ret_u128_list", "convert_to_zig_rocstr",
); );
call_void_bitcode_fn( call_void_bitcode_fn(
env, env,
&[ &[
ret_list_ptr_u128s, ret_list_ptr_zig_rocstr,
BasicValueEnum::IntValue(segment_count), BasicValueEnum::IntValue(segment_count),
BasicValueEnum::PointerValue(str_bytes_ptr), BasicValueEnum::PointerValue(str_bytes_ptr),
BasicValueEnum::IntValue(str_len), BasicValueEnum::IntValue(str_len),
@ -533,7 +536,6 @@ fn clone_nonempty_str<'a, 'ctx, 'env>(
Smallness::Big => { Smallness::Big => {
let clone_ptr = allocate_list(env, inplace, &CHAR_LAYOUT, len); let clone_ptr = allocate_list(env, inplace, &CHAR_LAYOUT, len);
let int_type = ptr_int(ctx, ptr_bytes); let int_type = ptr_int(ctx, ptr_bytes);
let ptr_as_int = builder.build_ptr_to_int(clone_ptr, int_type, "list_cast_ptr");
// TODO check if malloc returned null; if so, runtime error for OOM! // TODO check if malloc returned null; if so, runtime error for OOM!
@ -551,7 +553,7 @@ fn clone_nonempty_str<'a, 'ctx, 'env>(
struct_val = builder struct_val = builder
.build_insert_value( .build_insert_value(
struct_type.get_undef(), struct_type.get_undef(),
ptr_as_int, clone_ptr,
Builtin::WRAPPER_PTR, Builtin::WRAPPER_PTR,
"insert_ptr", "insert_ptr",
) )

View file

@ -203,26 +203,13 @@ pub fn block_of_memory<'ctx>(
/// Two usize values. Could be a wrapper for a List or a Str. /// Two usize values. Could be a wrapper for a List or a Str.
/// ///
/// It would be nicer if we could store this as a tuple containing one usize /// This way, we always initialize it to (*mut u8, usize), and may have to cast the pointer type
/// and one pointer. However, if we do that, we run into a problem with the /// for lists.
/// empty list: it doesn't know what pointer type it should initailize to,
/// so it can only create an empty (usize, usize) struct.
///
/// This way, we always initialize it to (usize, usize), and then if there's
/// actually a pointer, we use build_int_to_ptr and build_ptr_to_int to convert
/// the field when necessary. (It's not allowed to cast the entire struct from
/// (usize, usize) to (usize, ptr) or vice versa.)
pub fn collection(ctx: &Context, ptr_bytes: u32) -> StructType<'_> { pub fn collection(ctx: &Context, ptr_bytes: u32) -> StructType<'_> {
let int_type = BasicTypeEnum::IntType(ptr_int(ctx, ptr_bytes)); let usize_type = ptr_int(ctx, ptr_bytes);
let u8_ptr = ctx.i8_type().ptr_type(AddressSpace::Generic);
ctx.struct_type(&[int_type, int_type], false) ctx.struct_type(&[u8_ptr.into(), usize_type.into()], false)
}
/// Two usize values.
pub fn collection_int_wrapper(ctx: &Context, ptr_bytes: u32) -> StructType<'_> {
let usize_type = BasicTypeEnum::IntType(ptr_int(ctx, ptr_bytes));
ctx.struct_type(&[usize_type, usize_type], false)
} }
pub fn ptr_int(ctx: &Context, ptr_bytes: u32) -> IntType<'_> { pub fn ptr_int(ctx: &Context, ptr_bytes: u32) -> IntType<'_> {

View file

@ -79,19 +79,13 @@ impl<'ctx> PointerToRefcount<'ctx> {
} }
pub fn from_list_wrapper(env: &Env<'_, 'ctx, '_>, list_wrapper: StructValue<'ctx>) -> Self { pub fn from_list_wrapper(env: &Env<'_, 'ctx, '_>, list_wrapper: StructValue<'ctx>) -> Self {
let ptr_as_int = env let data_ptr = env
.builder .builder
.build_extract_value(list_wrapper, Builtin::WRAPPER_PTR, "read_list_ptr") .build_extract_value(list_wrapper, Builtin::WRAPPER_PTR, "read_list_ptr")
.unwrap() .unwrap()
.into_int_value(); .into_pointer_value();
let ptr = env.builder.build_int_to_ptr( Self::from_ptr_to_data(env, data_ptr)
ptr_as_int,
env.context.i64_type().ptr_type(AddressSpace::Generic),
"list_int_to_ptr",
);
Self::from_ptr_to_data(env, ptr)
} }
pub fn get_refcount<'a, 'env>(&self, env: &Env<'a, 'ctx, 'env>) -> IntValue<'ctx> { pub fn get_refcount<'a, 'env>(&self, env: &Env<'a, 'ctx, 'env>) -> IntValue<'ctx> {