mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 06:14:46 +00:00
Try a different Struct approach
This commit is contained in:
parent
8450597a07
commit
8da7f26237
4 changed files with 98 additions and 63 deletions
|
@ -3,12 +3,14 @@ use bumpalo::Bump;
|
|||
use inkwell::builder::Builder;
|
||||
use inkwell::context::Context;
|
||||
use inkwell::module::{Linkage, Module};
|
||||
use inkwell::types::{BasicTypeEnum, PointerType, StructType};
|
||||
use inkwell::types::BasicTypeEnum;
|
||||
use inkwell::values::BasicValueEnum::{self, *};
|
||||
use inkwell::values::{FunctionValue, IntValue, PointerValue};
|
||||
use inkwell::{AddressSpace, FloatPredicate, IntPredicate};
|
||||
|
||||
use crate::llvm::convert::{basic_type_from_layout, get_array_type, get_fn_type};
|
||||
use crate::llvm::convert::{
|
||||
basic_type_from_layout, collection_wrapper, get_array_type, get_fn_type,
|
||||
};
|
||||
use roc_collections::all::ImMap;
|
||||
use roc_module::symbol::{Interns, Symbol};
|
||||
use roc_mono::expr::{Expr, Proc, Procs};
|
||||
|
@ -208,30 +210,37 @@ pub fn build_expr<'a, 'ctx, 'env>(
|
|||
Array { elem_layout, elems } => {
|
||||
let ctx = env.context;
|
||||
let elem_type = basic_type_from_layout(ctx, elem_layout);
|
||||
let builder = env.builder;
|
||||
|
||||
if elems.is_empty() {
|
||||
let array_type = get_array_type(&elem_type, 0);
|
||||
let ptr_type = array_type.ptr_type(AddressSpace::Generic);
|
||||
let struct_type = array_wrapper(ctx, ptr_type);
|
||||
let zero = BasicValueEnum::IntValue(ctx.i32_type().const_zero());
|
||||
let val = struct_type.const_named_struct(&[
|
||||
BasicValueEnum::PointerValue(ptr_type.const_null()), // pointer
|
||||
zero, // length
|
||||
zero, // capacity
|
||||
]);
|
||||
let struct_type = collection_wrapper(ctx, ptr_type);
|
||||
let struct_val = struct_type.const_zero();
|
||||
|
||||
BasicValueEnum::StructValue(val)
|
||||
// The first field in the struct should be the pointer.
|
||||
builder
|
||||
.build_insert_value(
|
||||
struct_val,
|
||||
BasicValueEnum::PointerValue(ptr_type.const_null()),
|
||||
0,
|
||||
"insert_ptr",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
BasicValueEnum::StructValue(struct_val)
|
||||
} else {
|
||||
let builder = env.builder;
|
||||
let len_u64 = elems.len() as u64;
|
||||
let elem_bytes = elem_layout.stack_size(env.pointer_bytes) as u64;
|
||||
let bytes_len = elem_bytes * len_u64;
|
||||
|
||||
let len = ctx.i32_type().const_int(bytes_len, false);
|
||||
let ptr = env
|
||||
.builder
|
||||
.build_array_malloc(elem_type, len, "create_list_ptr")
|
||||
.unwrap();
|
||||
let ptr = {
|
||||
let bytes_len = elem_bytes * len_u64;
|
||||
let len = ctx.i32_type().const_int(bytes_len, false);
|
||||
|
||||
env.builder
|
||||
.build_array_malloc(elem_type, len, "create_list_ptr")
|
||||
.unwrap()
|
||||
};
|
||||
|
||||
// Copy the elements from the list literal into the array
|
||||
for (index, elem) in elems.iter().enumerate() {
|
||||
|
@ -243,32 +252,29 @@ pub fn build_expr<'a, 'ctx, 'env>(
|
|||
builder.build_store(elem_ptr, val);
|
||||
}
|
||||
|
||||
let struct_type = array_wrapper(ctx, ptr.get_type());
|
||||
let len = BasicValueEnum::IntValue(ctx.i32_type().const_int(len_u64, false));
|
||||
let tuple_ptr = builder.build_alloca(struct_type, "create_list_tuple");
|
||||
let ptr_val = BasicValueEnum::PointerValue(ptr);
|
||||
let struct_type = collection_wrapper(ctx, ptr.get_type());
|
||||
let len = dbg!(BasicValueEnum::IntValue(
|
||||
ctx.i32_type().const_int(len_u64, false)
|
||||
));
|
||||
let struct_val = struct_type.const_zero();
|
||||
|
||||
{
|
||||
let field_ptr =
|
||||
unsafe { builder.build_struct_gep(tuple_ptr, 0, "list_tuple_ptr") };
|
||||
// Field 0: pointer
|
||||
builder
|
||||
.build_insert_value(struct_val, ptr_val, 0, "insert_ptr")
|
||||
.unwrap();
|
||||
|
||||
builder.build_store(field_ptr, BasicValueEnum::PointerValue(ptr));
|
||||
}
|
||||
// Field 1: length
|
||||
builder
|
||||
.build_insert_value(struct_val, len, 1, "insert_len")
|
||||
.unwrap();
|
||||
|
||||
{
|
||||
let field_ptr =
|
||||
unsafe { builder.build_struct_gep(tuple_ptr, 1, "list_tuple_len") };
|
||||
// Field 2: capacity (initially set to length)
|
||||
builder
|
||||
.build_insert_value(struct_val, len, 2, "insert_capacity")
|
||||
.unwrap();
|
||||
|
||||
builder.build_store(field_ptr, len);
|
||||
}
|
||||
|
||||
{
|
||||
let field_ptr =
|
||||
unsafe { builder.build_struct_gep(tuple_ptr, 2, "list_tuple_capacity") };
|
||||
|
||||
builder.build_store(field_ptr, len);
|
||||
}
|
||||
|
||||
BasicValueEnum::PointerValue(tuple_ptr)
|
||||
BasicValueEnum::StructValue(struct_val)
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
|
@ -608,12 +614,11 @@ fn call_with_args<'a, 'ctx, 'env>(
|
|||
Symbol::LIST_LEN => {
|
||||
debug_assert!(args.len() == 1);
|
||||
|
||||
let tuple_ptr = args[0].into_pointer_value();
|
||||
let tuple_struct = dbg!(args[0].into_struct_value());
|
||||
let builder = env.builder;
|
||||
let list_len_ptr = unsafe { builder.build_struct_gep(tuple_ptr, 1, "list_tuple_len") };
|
||||
|
||||
// Get the 32-bit int length and cast it to a 64-bit int
|
||||
let i32_val = builder.build_load(list_len_ptr, "List.len").into_int_value();
|
||||
let i32_val = dbg!(builder.build_extract_value(tuple_struct, 1, "unwrapped_list_len").unwrap().into_int_value());
|
||||
|
||||
BasicValueEnum::IntValue(builder.build_int_cast(i32_val, env.context.i64_type(), "i32_to_i64"))
|
||||
}
|
||||
|
@ -705,11 +710,3 @@ fn call_with_args<'a, 'ctx, 'env>(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// (pointer: usize, length: u32, capacity: u32)
|
||||
fn array_wrapper<'ctx>(ctx: &'ctx Context, ptr_type: PointerType<'ctx>) -> StructType<'ctx> {
|
||||
let ptr_type_enum = BasicTypeEnum::PointerType(ptr_type);
|
||||
let u32_type = BasicTypeEnum::IntType(ctx.i32_type());
|
||||
|
||||
ctx.struct_type(&[ptr_type_enum, u32_type, u32_type], false)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use inkwell::context::Context;
|
||||
use inkwell::types::BasicTypeEnum::{self, *};
|
||||
use inkwell::types::{ArrayType, BasicType, FunctionType};
|
||||
use inkwell::types::{ArrayType, BasicType, FunctionType, PointerType, StructType};
|
||||
use inkwell::AddressSpace;
|
||||
|
||||
use roc_mono::layout::Layout;
|
||||
|
@ -62,15 +62,36 @@ pub fn basic_type_from_layout<'ctx>(
|
|||
Builtin(builtin) => match builtin {
|
||||
Int64 => context.i64_type().as_basic_type_enum(),
|
||||
Float64 => context.f64_type().as_basic_type_enum(),
|
||||
Str => context
|
||||
Str | EmptyStr => context
|
||||
.i8_type()
|
||||
.ptr_type(AddressSpace::Generic)
|
||||
.as_basic_type_enum(),
|
||||
Map(_, _) => panic!("TODO layout_to_basic_type for Builtin::Map"),
|
||||
Set(_) => panic!("TODO layout_to_basic_type for Builtin::Set"),
|
||||
List(elem_layout) => basic_type_from_layout(context, elem_layout)
|
||||
.ptr_type(AddressSpace::Generic)
|
||||
.as_basic_type_enum(),
|
||||
Map(_, _) | EmptyMap => panic!("TODO layout_to_basic_type for Builtin::Map"),
|
||||
Set(_) | EmptySet => panic!("TODO layout_to_basic_type for Builtin::Set"),
|
||||
List(elem_layout) => {
|
||||
let ptr_type =
|
||||
basic_type_from_layout(context, elem_layout).ptr_type(AddressSpace::Generic);
|
||||
|
||||
collection_wrapper(context, ptr_type).into()
|
||||
}
|
||||
EmptyList => {
|
||||
let array_type =
|
||||
get_array_type(&context.opaque_struct_type("empty_list_elem").into(), 0);
|
||||
let ptr_type = array_type.ptr_type(AddressSpace::Generic);
|
||||
|
||||
collection_wrapper(context, ptr_type).into()
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// (pointer: usize, length: u32, capacity: u32)
|
||||
pub fn collection_wrapper<'ctx>(
|
||||
ctx: &'ctx Context,
|
||||
ptr_type: PointerType<'ctx>,
|
||||
) -> StructType<'ctx> {
|
||||
let ptr_type_enum = BasicTypeEnum::PointerType(ptr_type);
|
||||
let u32_type = BasicTypeEnum::IntType(ctx.i32_type());
|
||||
|
||||
ctx.struct_type(&[ptr_type_enum, u32_type, u32_type], false)
|
||||
}
|
||||
|
|
|
@ -482,6 +482,12 @@ mod test_gen {
|
|||
assert_evals_to!("1234.0", 1234.0, f64);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_list_len() {
|
||||
// assert_evals_to!("List.len [ 12, 9, 6, 3 ]", 4, i64);
|
||||
assert_llvm_evals_to!("List.len []", 0, i64, |x| x);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn int_list_len() {
|
||||
// assert_evals_to!("List.len [ 12, 9, 6, 3 ]", 4, i64);
|
||||
|
|
|
@ -23,6 +23,10 @@ pub enum Builtin<'a> {
|
|||
Map(&'a Layout<'a>, &'a Layout<'a>),
|
||||
Set(&'a Layout<'a>),
|
||||
List(&'a Layout<'a>),
|
||||
EmptyStr,
|
||||
EmptyList,
|
||||
EmptyMap,
|
||||
EmptySet,
|
||||
}
|
||||
|
||||
impl<'a> Layout<'a> {
|
||||
|
@ -94,10 +98,10 @@ impl<'a> Builtin<'a> {
|
|||
match self {
|
||||
Int64 => Builtin::I64_SIZE,
|
||||
Float64 => Builtin::F64_SIZE,
|
||||
Str => Builtin::STR_WORDS * pointer_size,
|
||||
Map(_, _) => Builtin::MAP_WORDS * pointer_size,
|
||||
Set(_) => Builtin::SET_WORDS * pointer_size,
|
||||
List(_) => Builtin::LIST_WORDS * pointer_size,
|
||||
Str | EmptyStr => Builtin::STR_WORDS * pointer_size,
|
||||
Map(_, _) | EmptyMap => Builtin::MAP_WORDS * pointer_size,
|
||||
Set(_) | EmptySet => Builtin::SET_WORDS * pointer_size,
|
||||
List(_) | EmptyList => Builtin::LIST_WORDS * pointer_size,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -131,9 +135,16 @@ fn layout_from_flat_type<'a>(
|
|||
}
|
||||
Symbol::STR_STR => Ok(Layout::Builtin(Builtin::Str)),
|
||||
Symbol::LIST_LIST => {
|
||||
let elem_layout = Layout::from_var(arena, args[0], subs)?;
|
||||
use roc_types::subs::Content::*;
|
||||
|
||||
Ok(Layout::Builtin(Builtin::List(arena.alloc(elem_layout))))
|
||||
match subs.get_without_compacting(args[0]).content {
|
||||
FlexVar(_) | RigidVar(_) => Ok(Layout::Builtin(Builtin::EmptyList)),
|
||||
content => {
|
||||
let elem_layout = Layout::from_content(arena, content, subs)?;
|
||||
|
||||
Ok(Layout::Builtin(Builtin::List(arena.alloc(elem_layout))))
|
||||
}
|
||||
}
|
||||
}
|
||||
Symbol::ATTR_ATTR => {
|
||||
debug_assert!(args.len() == 2);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue