Build erased types

This commit is contained in:
Ayaz Hafiz 2023-07-06 15:32:42 -05:00
parent b8c1436a75
commit e1c88c6101
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
3 changed files with 55 additions and 8 deletions

View file

@ -4,12 +4,12 @@ use crate::llvm::convert::{
argument_type_from_layout, basic_type_from_builtin, basic_type_from_layout, zig_str_type,
};
use crate::llvm::expect::{clone_to_shared_memory, SharedMemoryPointer};
use crate::llvm::fn_ptr;
use crate::llvm::memcpy::build_memcpy;
use crate::llvm::refcounting::{
build_reset, decrement_refcount_layout, increment_refcount_layout, PointerToRefcount,
};
use crate::llvm::struct_::{struct_from_fields, RocStruct};
use crate::llvm::{erased, fn_ptr};
use bumpalo::collections::Vec;
use bumpalo::Bump;
use inkwell::attributes::{Attribute, AttributeLoc};
@ -912,7 +912,6 @@ pub(crate) fn build_exp_call<'a, 'ctx>(
CallType::ByName {
name,
specialization_id,
arg_layouts,
ret_layout,
..
} => {
@ -930,7 +929,6 @@ pub(crate) fn build_exp_call<'a, 'ctx>(
roc_call_with_args(
env,
layout_interner,
arg_layouts,
*ret_layout,
*name,
FuncBorrowSpec::Some(func_spec),
@ -1128,10 +1126,14 @@ pub(crate) fn build_exp_expr<'a, 'ctx>(
FunctionPointer { lambda_name } => {
let function_ptr_type =
fn_ptr::pointer_type_expecting_layout(env, layout_interner, layout);
let alloca = fn_ptr::build(env, layout_interner, *lambda_name, function_ptr_type);
let alloca = fn_ptr::build(env, *lambda_name, function_ptr_type);
alloca.into()
}
ErasedMake { .. } => todo_lambda_erasure!(),
ErasedMake { value, callee } => {
let value = value.map(|sym| scope.load_symbol(&sym).into_pointer_value());
let callee = scope.load_symbol(callee).into_pointer_value();
erased::build(env, value, callee).into()
}
ErasedLoad { .. } => todo_lambda_erasure!(),
Reset {
@ -5659,7 +5661,6 @@ fn function_value_by_name_help<'a, 'ctx>(
fn roc_call_with_args<'a, 'ctx>(
env: &Env<'a, 'ctx, '_>,
layout_interner: &STLayoutInterner<'a>,
argument_layouts: &[InLayout<'a>],
result_layout: InLayout<'a>,
name: LambdaName<'a>,
func_spec: FuncBorrowSpec,

View file

@ -1,4 +1,8 @@
use inkwell::{types::StructType, AddressSpace};
use inkwell::{
types::StructType,
values::{PointerValue, StructValue},
AddressSpace,
};
use super::build::Env;
@ -17,3 +21,46 @@ pub fn basic_type<'a, 'ctx>(env: &Env<'a, 'ctx, '_>) -> StructType<'ctx> {
env.context
.struct_type(&[ptr_ty.into(), ptr_ty.into(), ptr_ty.into()], false)
}
fn bitcast_to_opaque_ptr<'ctx>(
env: &Env<'_, 'ctx, '_>,
value: PointerValue<'ctx>,
) -> PointerValue<'ctx> {
env.builder
.build_bitcast(
value,
env.context.i8_type().ptr_type(AddressSpace::default()),
"to_opaque_ptr",
)
.into_pointer_value()
}
pub fn build<'a, 'ctx>(
env: &Env<'a, 'ctx, '_>,
value: Option<PointerValue<'ctx>>,
callee: PointerValue<'ctx>,
) -> StructValue<'ctx> {
let struct_type = basic_type(env);
let struct_value = struct_type.const_zero().into();
let struct_value = match value {
Some(value) => {
let value = bitcast_to_opaque_ptr(env, value);
env.builder
.build_insert_value(struct_value, value, 0, "insert_value")
.unwrap()
}
None => struct_value,
};
let callee = bitcast_to_opaque_ptr(env, callee);
let struct_value = env
.builder
.build_insert_value(struct_value, callee, 1, "insert_callee")
.unwrap();
// TODO: insert refcounter
struct_value.into_struct_value()
}

View file

@ -61,7 +61,6 @@ pub fn pointer_type_expecting_layout<'a, 'ctx>(
pub fn build<'a, 'ctx>(
env: &Env<'a, 'ctx, '_>,
layout_interner: &STLayoutInterner<'a>,
lambda_name: LambdaName<'a>,
function_ptr_type: PointerType<'ctx>,
) -> BasicValueEnum<'ctx> {