more wip; now we just need to generate the exposed procs at the right time

This commit is contained in:
Folkert 2023-02-08 22:56:38 +01:00
commit 6c15284a8f
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
255 changed files with 11686 additions and 3025 deletions

View file

@ -11,8 +11,8 @@ use crate::llvm::refcounting::{
use inkwell::attributes::{Attribute, AttributeLoc};
use inkwell::types::{BasicType, BasicTypeEnum, StructType};
use inkwell::values::{
BasicValue, BasicValueEnum, CallSiteValue, FunctionValue, InstructionValue, IntValue,
PointerValue, StructValue,
BasicValueEnum, CallSiteValue, FunctionValue, InstructionValue, IntValue, PointerValue,
StructValue,
};
use inkwell::AddressSpace;
use roc_error_macros::internal_error;
@ -206,7 +206,7 @@ fn build_transform_caller_help<'a, 'ctx, 'env>(
let block = env.builder.get_insert_block().expect("to be in a function");
let di_location = env.builder.get_current_debug_location().unwrap();
let arg_type = env.context.i8_type().ptr_type(AddressSpace::Generic);
let arg_type = env.context.i8_type().ptr_type(AddressSpace::default());
let function_value = crate::llvm::refcounting::build_header_help(
env,
@ -244,7 +244,7 @@ fn build_transform_caller_help<'a, 'ctx, 'env>(
for (argument_ptr, layout) in arguments.iter().zip(argument_layouts) {
let basic_type =
basic_type_from_layout(env, layout_interner, *layout).ptr_type(AddressSpace::Generic);
basic_type_from_layout(env, layout_interner, *layout).ptr_type(AddressSpace::default());
let cast_ptr = env.builder.build_pointer_cast(
argument_ptr.into_pointer_value(),
@ -274,7 +274,7 @@ fn build_transform_caller_help<'a, 'ctx, 'env>(
}
(true, layout) => {
let closure_type = basic_type_from_layout(env, layout_interner, layout)
.ptr_type(AddressSpace::Generic);
.ptr_type(AddressSpace::default());
let closure_cast =
env.builder
@ -310,8 +310,7 @@ fn build_transform_caller_help<'a, 'ctx, 'env>(
env.builder.build_return(None);
env.builder.position_at_end(block);
env.builder
.set_current_debug_location(env.context, di_location);
env.builder.set_current_debug_location(di_location);
function_value
}
@ -375,7 +374,7 @@ fn build_rc_wrapper<'a, 'ctx, 'env>(
let function_value = match env.module.get_function(fn_name.as_str()) {
Some(function_value) => function_value,
None => {
let arg_type = env.context.i8_type().ptr_type(AddressSpace::Generic);
let arg_type = env.context.i8_type().ptr_type(AddressSpace::default());
let function_value = match rc_operation {
Mode::Inc | Mode::Dec => crate::llvm::refcounting::build_header_help(
@ -411,7 +410,7 @@ fn build_rc_wrapper<'a, 'ctx, 'env>(
generic_value_ptr.set_name(Symbol::ARG_1.as_str(&env.interns));
let value_type = basic_type_from_layout(env, layout_interner, layout);
let value_ptr_type = value_type.ptr_type(AddressSpace::Generic);
let value_ptr_type = value_type.ptr_type(AddressSpace::default());
let value_ptr =
env.builder
.build_pointer_cast(generic_value_ptr, value_ptr_type, "load_opaque");
@ -449,8 +448,7 @@ fn build_rc_wrapper<'a, 'ctx, 'env>(
};
env.builder.position_at_end(block);
env.builder
.set_current_debug_location(env.context, di_location);
env.builder.set_current_debug_location(di_location);
function_value
}
@ -472,7 +470,7 @@ pub fn build_eq_wrapper<'a, 'ctx, 'env>(
let function_value = match env.module.get_function(fn_name.as_str()) {
Some(function_value) => function_value,
None => {
let arg_type = env.context.i8_type().ptr_type(AddressSpace::Generic);
let arg_type = env.context.i8_type().ptr_type(AddressSpace::default());
let function_value = crate::llvm::refcounting::build_header_help(
env,
@ -502,7 +500,7 @@ pub fn build_eq_wrapper<'a, 'ctx, 'env>(
value_ptr2.set_name(Symbol::ARG_2.as_str(&env.interns));
let value_type = basic_type_from_layout(env, layout_interner, layout)
.ptr_type(AddressSpace::Generic);
.ptr_type(AddressSpace::default());
let value_cast1 = env
.builder
@ -533,8 +531,7 @@ pub fn build_eq_wrapper<'a, 'ctx, 'env>(
};
env.builder.position_at_end(block);
env.builder
.set_current_debug_location(env.context, di_location);
env.builder.set_current_debug_location(di_location);
function_value
}
@ -557,7 +554,7 @@ pub fn build_compare_wrapper<'a, 'ctx, 'env>(
let function_value = match env.module.get_function(fn_name) {
Some(function_value) => function_value,
None => {
let arg_type = env.context.i8_type().ptr_type(AddressSpace::Generic);
let arg_type = env.context.i8_type().ptr_type(AddressSpace::default());
let function_value = crate::llvm::refcounting::build_header_help(
env,
@ -592,7 +589,7 @@ pub fn build_compare_wrapper<'a, 'ctx, 'env>(
value_ptr2.set_name(Symbol::ARG_3.as_str(&env.interns));
let value_type = basic_type_from_layout(env, layout_interner, layout);
let value_ptr_type = value_type.ptr_type(AddressSpace::Generic);
let value_ptr_type = value_type.ptr_type(AddressSpace::default());
let value_cast1 =
env.builder
@ -623,7 +620,7 @@ pub fn build_compare_wrapper<'a, 'ctx, 'env>(
_ => {
let closure_type =
basic_type_from_layout(env, layout_interner, closure_data_repr);
let closure_ptr_type = closure_type.ptr_type(AddressSpace::Generic);
let closure_ptr_type = closure_type.ptr_type(AddressSpace::default());
let closure_cast = env.builder.build_pointer_cast(
closure_ptr,
@ -659,8 +656,7 @@ pub fn build_compare_wrapper<'a, 'ctx, 'env>(
};
env.builder.position_at_end(block);
env.builder
.set_current_debug_location(env.context, di_location);
env.builder.set_current_debug_location(di_location);
function_value
}
@ -798,7 +794,7 @@ fn ptr_len_cap<'a, 'ctx, 'env>(
let ptr = env.builder.build_int_to_ptr(
lower_word,
env.context.i8_type().ptr_type(AddressSpace::Generic),
env.context.i8_type().ptr_type(AddressSpace::default()),
"list_ptr",
);

View file

@ -24,8 +24,8 @@ use inkwell::types::{
};
use inkwell::values::BasicValueEnum::{self, *};
use inkwell::values::{
BasicMetadataValueEnum, BasicValue, CallSiteValue, FunctionValue, InstructionValue, IntValue,
PhiValue, PointerValue, StructValue,
BasicMetadataValueEnum, CallSiteValue, FunctionValue, InstructionValue, IntValue, PhiValue,
PointerValue, StructValue,
};
use inkwell::OptimizationLevel;
use inkwell::{AddressSpace, IntPredicate};
@ -37,7 +37,6 @@ use roc_collections::all::{ImMap, MutMap, MutSet};
use roc_debug_flags::dbg_do;
#[cfg(debug_assertions)]
use roc_debug_flags::ROC_PRINT_LLVM_FN_VERIFICATION;
use roc_error_macros::internal_error;
use roc_module::symbol::{Interns, ModuleId, Symbol};
use roc_mono::ir::{
BranchInfo, CallType, CrashTag, EntryPoint, GlueLayouts, JoinPointId, ListLiteralElement,
@ -157,7 +156,7 @@ macro_rules! debug_info_init {
/* current_scope */ lexical_block.as_debug_info_scope(),
/* inlined_at */ None,
);
$env.builder.set_current_debug_location(&$env.context, loc);
$env.builder.set_current_debug_location(loc);
}};
}
@ -686,7 +685,7 @@ fn promote_to_wasm_test_wrapper<'a, 'ctx, 'env>(
let output_type = match roc_main_fn.get_type().get_return_type() {
Some(return_type) => {
let output_type = return_type.ptr_type(AddressSpace::Generic);
let output_type = return_type.ptr_type(AddressSpace::default());
output_type.into()
}
None => {
@ -880,7 +879,7 @@ fn small_str_ptr_width_8<'a, 'ctx, 'env>(
let len = env.ptr_int().const_int(word2, false);
let cap = env.ptr_int().const_int(word3, false);
let address_space = AddressSpace::Generic;
let address_space = AddressSpace::default();
let ptr_type = env.context.i8_type().ptr_type(address_space);
let ptr = env.builder.build_int_to_ptr(ptr, ptr_type, "to_u8_ptr");
@ -907,7 +906,7 @@ fn small_str_ptr_width_4<'a, 'ctx, 'env>(
let len = env.ptr_int().const_int(word2 as u64, false);
let cap = env.ptr_int().const_int(word3 as u64, false);
let address_space = AddressSpace::Generic;
let address_space = AddressSpace::default();
let ptr_type = env.context.i8_type().ptr_type(address_space);
let ptr = env.builder.build_int_to_ptr(ptr, ptr_type, "to_u8_ptr");
@ -1049,7 +1048,7 @@ fn struct_pointer_from_fields<'a, 'ctx, 'env, I>(
.builder
.build_bitcast(
input_pointer,
struct_type.ptr_type(AddressSpace::Generic),
struct_type.ptr_type(AddressSpace::default()),
"struct_ptr",
)
.into_pointer_value();
@ -1310,7 +1309,7 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
let data_ptr = env.builder.build_pointer_cast(
opaque_data_ptr,
struct_type.ptr_type(AddressSpace::Generic),
struct_type.ptr_type(AddressSpace::default()),
"to_data_pointer",
);
@ -1528,14 +1527,14 @@ fn build_tag_field_value<'a, 'ctx, 'env>(
value: BasicValueEnum<'ctx>,
tag_field_layout: InLayout<'a>,
) -> BasicValueEnum<'ctx> {
if let Layout::RecursivePointer = layout_interner.get(tag_field_layout) {
if let Layout::RecursivePointer(_) = layout_interner.get(tag_field_layout) {
debug_assert!(value.is_pointer_value());
// we store recursive pointers as `i64*`
env.builder
.build_pointer_cast(
value.into_pointer_value(),
env.context.i64_type().ptr_type(AddressSpace::Generic),
env.context.i64_type().ptr_type(AddressSpace::default()),
"cast_recursive_pointer",
)
.into()
@ -1751,7 +1750,7 @@ fn build_tag<'a, 'ctx, 'env>(
);
if tag_id == *nullable_id as _ {
let output_type = roc_union.struct_type().ptr_type(AddressSpace::Generic);
let output_type = roc_union.struct_type().ptr_type(AddressSpace::default());
return output_type.const_null().into();
}
@ -2003,7 +2002,7 @@ fn lookup_at_index_ptr<'a, 'ctx, 'env>(
let ptr = env.builder.build_pointer_cast(
value,
struct_type.ptr_type(AddressSpace::Generic),
struct_type.ptr_type(AddressSpace::default()),
"cast_lookup_at_index_ptr",
);
@ -2020,7 +2019,7 @@ fn lookup_at_index_ptr<'a, 'ctx, 'env>(
"load_at_index_ptr_old",
);
if let Some(Layout::RecursivePointer) = field_layouts
if let Some(Layout::RecursivePointer(_)) = field_layouts
.get(index as usize)
.map(|l| layout_interner.get(*l))
{
@ -2058,7 +2057,7 @@ fn lookup_at_index_ptr2<'a, 'ctx, 'env>(
let data_ptr = env.builder.build_pointer_cast(
value,
struct_type.ptr_type(AddressSpace::Generic),
struct_type.ptr_type(AddressSpace::default()),
"cast_lookup_at_index_ptr",
);
@ -2080,7 +2079,7 @@ fn lookup_at_index_ptr2<'a, 'ctx, 'env>(
"load_at_index_ptr",
);
if let Some(Layout::RecursivePointer) = field_layouts
if let Some(Layout::RecursivePointer(_)) = field_layouts
.get(index as usize)
.map(|l| layout_interner.get(*l))
{
@ -2181,7 +2180,7 @@ pub fn allocate_with_refcount_help<'a, 'ctx, 'env>(
)
.into_pointer_value();
let ptr_type = value_type.ptr_type(AddressSpace::Generic);
let ptr_type = value_type.ptr_type(AddressSpace::default());
env.builder
.build_pointer_cast(ptr, ptr_type, "alloc_cast_to_desired")
@ -2406,7 +2405,7 @@ pub fn store_roc_value_opaque<'a, 'ctx, 'env>(
value: BasicValueEnum<'ctx>,
) {
let target_type =
basic_type_from_layout(env, layout_interner, layout).ptr_type(AddressSpace::Generic);
basic_type_from_layout(env, layout_interner, layout).ptr_type(AddressSpace::default());
let destination =
env.builder
.build_pointer_cast(opaque_destination, target_type, "store_roc_value_opaque");
@ -2481,7 +2480,10 @@ pub fn build_exp_stmt<'a, 'ctx, 'env>(
let mut stack = Vec::with_capacity_in(queue.len(), env.arena);
for (symbol, expr, layout) in queue {
debug_assert!(layout_interner.get(*layout) != Layout::RecursivePointer);
debug_assert!(!matches!(
layout_interner.get(*layout),
Layout::RecursivePointer(_)
));
let val = build_exp_expr(
env,
@ -2656,7 +2658,7 @@ pub fn build_exp_stmt<'a, 'ctx, 'env>(
let basic_type = basic_type_from_layout(env, layout_interner, param.layout);
let phi_type = if layout_interner.is_passed_by_reference(param.layout) {
basic_type.ptr_type(AddressSpace::Generic).into()
basic_type.ptr_type(AddressSpace::default()).into()
} else {
basic_type
};
@ -3199,7 +3201,7 @@ fn complex_bitcast_from_bigger_than_to<'ctx>(
// then read it back as a different type
let to_type_pointer = builder.build_pointer_cast(
argument_pointer,
to_type.ptr_type(inkwell::AddressSpace::Generic),
to_type.ptr_type(inkwell::AddressSpace::default()),
name,
);
@ -3222,7 +3224,7 @@ fn complex_bitcast_to_bigger_than_from<'ctx>(
storage,
from_value
.get_type()
.ptr_type(inkwell::AddressSpace::Generic),
.ptr_type(inkwell::AddressSpace::default()),
name,
);
@ -3583,7 +3585,7 @@ fn expose_function_to_host_help_c_abi_generic<'a, 'ctx, 'env>(
argument_types.insert(0, output_type);
}
Some(return_type) => {
let output_type = return_type.ptr_type(AddressSpace::Generic);
let output_type = return_type.ptr_type(AddressSpace::default());
argument_types.insert(0, output_type.into());
}
}
@ -3635,7 +3637,7 @@ fn expose_function_to_host_help_c_abi_generic<'a, 'ctx, 'env>(
// bitcast the ptr
let fastcc_ptr = env.builder.build_pointer_cast(
arg.into_pointer_value(),
fastcc_type.ptr_type(AddressSpace::Generic),
fastcc_type.ptr_type(AddressSpace::default()),
"bitcast_arg",
);
@ -3730,7 +3732,7 @@ fn expose_function_to_host_help_c_abi_gen_test<'a, 'ctx, 'env>(
let return_type = wrapper_return_type;
let c_function_spec = {
let output_type = return_type.ptr_type(AddressSpace::Generic);
let output_type = return_type.ptr_type(AddressSpace::default());
argument_types.push(output_type.into());
FunctionSpec::cconv(env, CCReturn::Void, None, &argument_types)
};
@ -3894,7 +3896,10 @@ fn expose_function_to_host_help_c_abi_v2<'a, 'ctx, 'env>(
let c_abi_roc_str_type = env.context.struct_type(
&[
env.context.i8_type().ptr_type(AddressSpace::Generic).into(),
env.context
.i8_type()
.ptr_type(AddressSpace::default())
.into(),
env.ptr_int().into(),
env.ptr_int().into(),
],
@ -4013,7 +4018,7 @@ fn expose_function_to_host_help_c_abi_v2<'a, 'ctx, 'env>(
// bitcast the ptr
let fastcc_ptr = env.builder.build_pointer_cast(
arg.into_pointer_value(),
fastcc_type.ptr_type(AddressSpace::Generic),
fastcc_type.ptr_type(AddressSpace::default()),
"bitcast_arg",
);
@ -4190,7 +4195,7 @@ pub fn get_sjlj_buffer<'a, 'ctx, 'env>(env: &Env<'a, 'ctx, 'env>) -> PointerValu
env.builder.build_pointer_cast(
global.as_pointer_value(),
env.context.i32_type().ptr_type(AddressSpace::Generic),
env.context.i32_type().ptr_type(AddressSpace::default()),
"cast_sjlj_buffer",
)
}
@ -4207,12 +4212,12 @@ pub fn build_setjmp_call<'a, 'ctx, 'env>(env: &Env<'a, 'ctx, 'env>) -> BasicValu
let buf_type = env
.context
.i8_type()
.ptr_type(AddressSpace::Generic)
.ptr_type(AddressSpace::default())
.array_type(5);
let jmp_buf_i8p_arr = env.builder.build_pointer_cast(
jmp_buf,
buf_type.ptr_type(AddressSpace::Generic),
buf_type.ptr_type(AddressSpace::default()),
"jmp_buf [5 x i8*]",
);
@ -4253,7 +4258,7 @@ pub fn build_setjmp_call<'a, 'ctx, 'env>(env: &Env<'a, 'ctx, 'env>) -> BasicValu
.builder
.build_pointer_cast(
jmp_buf,
env.context.i8_type().ptr_type(AddressSpace::Generic),
env.context.i8_type().ptr_type(AddressSpace::default()),
"jmp_buf i8*",
)
.into();
@ -4410,7 +4415,7 @@ fn roc_call_result_type<'a, 'ctx, 'env>(
env.context.struct_type(
&[
env.context.i64_type().into(),
zig_str_type(env).ptr_type(AddressSpace::Generic).into(),
zig_str_type(env).ptr_type(AddressSpace::default()).into(),
return_type,
],
false,
@ -4486,7 +4491,7 @@ fn make_exception_catching_wrapper<'a, 'ctx, 'env>(
basic_type_from_layout(env, layout_interner, return_layout),
);
// argument_types.push(wrapper_return_type.ptr_type(AddressSpace::Generic).into());
// argument_types.push(wrapper_return_type.ptr_type(AddressSpace::default()).into());
// let wrapper_function_type = env.context.void_type().fn_type(&argument_types, false);
let wrapper_function_spec = FunctionSpec::cconv(
@ -5050,7 +5055,7 @@ fn build_closure_caller<'a, 'ctx, 'env>(
for layout in arguments {
let arg_type = basic_type_from_layout(env, layout_interner, *layout);
let arg_ptr_type = arg_type.ptr_type(AddressSpace::Generic);
let arg_ptr_type = arg_type.ptr_type(AddressSpace::default());
argument_types.push(arg_ptr_type.into());
}
@ -5059,7 +5064,7 @@ fn build_closure_caller<'a, 'ctx, 'env>(
let basic_type =
basic_type_from_layout(env, layout_interner, lambda_set.runtime_representation());
basic_type.ptr_type(AddressSpace::Generic)
basic_type.ptr_type(AddressSpace::default())
};
argument_types.push(closure_argument_type.into());
@ -5068,7 +5073,7 @@ fn build_closure_caller<'a, 'ctx, 'env>(
let result_type = basic_type_from_layout(env, layout_interner, result);
let output_type = { result_type.ptr_type(AddressSpace::Generic) };
let output_type = { result_type.ptr_type(AddressSpace::default()) };
argument_types.push(output_type.into());
// STEP 1: build function header
@ -5584,7 +5589,7 @@ fn to_cc_type_builtin<'a, 'ctx, 'env>(
basic_type_from_builtin(env, builtin)
}
Builtin::Str | Builtin::List(_) => {
let address_space = AddressSpace::Generic;
let address_space = AddressSpace::default();
let field_types: [BasicTypeEnum; 3] = [
env.context.i8_type().ptr_type(address_space).into(),
env.ptr_int().into(),
@ -5699,7 +5704,7 @@ impl<'ctx> FunctionSpec<'ctx> {
let (typ, opt_sret_parameter) = match cc_return {
CCReturn::ByPointer => {
// turn the output type into a pointer type. Make it the first argument to the function
let output_type = return_type.unwrap().ptr_type(AddressSpace::Generic);
let output_type = return_type.unwrap().ptr_type(AddressSpace::default());
let mut arguments: Vec<'_, BasicTypeEnum> =
bumpalo::vec![in env.arena; output_type.into()];
@ -5741,7 +5746,7 @@ impl<'ctx> FunctionSpec<'ctx> {
return_type.fn_type(&function_arguments(env, &argument_types), false)
}
RocReturn::ByPointer => {
argument_types.push(return_type.ptr_type(AddressSpace::Generic).into());
argument_types.push(return_type.ptr_type(AddressSpace::default()).into());
env.context
.void_type()
.fn_type(&function_arguments(env, &argument_types), false)
@ -5989,7 +5994,7 @@ fn define_global_str_literal_ptr<'a, 'ctx, 'env>(
let ptr = env.builder.build_pointer_cast(
global.as_pointer_value(),
env.context.i8_type().ptr_type(AddressSpace::Generic),
env.context.i8_type().ptr_type(AddressSpace::default()),
"to_opaque",
);
@ -6134,23 +6139,3 @@ pub fn add_func<'ctx>(
fn_val
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum WhenRecursive<'a> {
Unreachable,
Loop(UnionLayout<'a>),
}
impl<'a> WhenRecursive<'a> {
pub fn unwrap_recursive_pointer(&self, layout: Layout<'a>) -> Layout<'a> {
match layout {
Layout::RecursivePointer => match self {
WhenRecursive::Loop(lay) => Layout::Union(*lay),
WhenRecursive::Unreachable => {
internal_error!("cannot compare recursive pointers outside of a structure")
}
},
_ => layout,
}
}
}

View file

@ -74,7 +74,7 @@ fn pass_element_as_opaque<'a, 'ctx, 'env>(
env.builder
.build_pointer_cast(
element_ptr,
env.context.i8_type().ptr_type(AddressSpace::Generic),
env.context.i8_type().ptr_type(AddressSpace::default()),
"pass_element_as_opaque",
)
.into()
@ -97,7 +97,7 @@ pub(crate) fn pass_as_opaque<'a, 'ctx, 'env>(
env.builder
.build_pointer_cast(
ptr,
env.context.i8_type().ptr_type(AddressSpace::Generic),
env.context.i8_type().ptr_type(AddressSpace::default()),
"pass_as_opaque",
)
.into()
@ -133,7 +133,7 @@ pub(crate) fn list_get_unsafe<'a, 'ctx, 'env>(
let builder = env.builder;
let elem_type = basic_type_from_layout(env, layout_interner, element_layout);
let ptr_type = elem_type.ptr_type(AddressSpace::Generic);
let ptr_type = elem_type.ptr_type(AddressSpace::default());
// Load the pointer to the array data
let array_data_ptr = load_list_ptr(builder, wrapper_struct, ptr_type);
@ -804,7 +804,7 @@ pub(crate) fn decref<'a, 'ctx, 'env>(
let (_, pointer) = load_list(
env.builder,
wrapper_struct,
env.context.i8_type().ptr_type(AddressSpace::Generic),
env.context.i8_type().ptr_type(AddressSpace::default()),
);
crate::llvm::refcounting::decref_pointer_check_null(env, pointer, alignment);

View file

@ -32,7 +32,7 @@ pub(crate) fn decode_from_utf8_result<'a, 'ctx, 'env>(
PtrWidth::Bytes4 | PtrWidth::Bytes8 => {
let result_ptr_cast = env.builder.build_pointer_cast(
pointer,
record_type.ptr_type(AddressSpace::Generic),
record_type.ptr_type(AddressSpace::default()),
"to_unnamed",
);

View file

@ -1,17 +1,14 @@
use crate::llvm::build::{
get_tag_id, tag_pointer_clear_tag_id, Env, WhenRecursive, FAST_CALL_CONV,
};
use crate::llvm::build::{get_tag_id, tag_pointer_clear_tag_id, Env, FAST_CALL_CONV};
use crate::llvm::build_list::{list_len, load_list_ptr};
use crate::llvm::build_str::str_equal;
use crate::llvm::convert::basic_type_from_layout;
use bumpalo::collections::Vec;
use inkwell::types::BasicType;
use inkwell::values::{
BasicValue, BasicValueEnum, FunctionValue, IntValue, PointerValue, StructValue,
};
use inkwell::values::{BasicValueEnum, FunctionValue, IntValue, PointerValue, StructValue};
use inkwell::{AddressSpace, FloatPredicate, IntPredicate};
use roc_builtins::bitcode;
use roc_builtins::bitcode::{FloatWidth, IntWidth};
use roc_error_macros::internal_error;
use roc_module::symbol::Symbol;
use roc_mono::layout::{
Builtin, InLayout, Layout, LayoutIds, LayoutInterner, STLayoutInterner, UnionLayout,
@ -38,7 +35,6 @@ pub fn generic_eq<'a, 'ctx, 'env>(
rhs_val,
lhs_layout,
rhs_layout,
WhenRecursive::Unreachable,
)
}
@ -59,7 +55,6 @@ pub fn generic_neq<'a, 'ctx, 'env>(
rhs_val,
lhs_layout,
rhs_layout,
WhenRecursive::Unreachable,
)
}
@ -69,8 +64,8 @@ fn build_eq_builtin<'a, 'ctx, 'env>(
layout_ids: &mut LayoutIds<'a>,
lhs_val: BasicValueEnum<'ctx>,
rhs_val: BasicValueEnum<'ctx>,
builtin_layout: InLayout<'a>,
builtin: &Builtin<'a>,
when_recursive: WhenRecursive<'a>,
) -> BasicValueEnum<'ctx> {
let int_cmp = |pred, label| {
let int_val = env.builder.build_int_compare(
@ -129,19 +124,15 @@ fn build_eq_builtin<'a, 'ctx, 'env>(
Builtin::Decimal => dec_binop_with_unchecked(env, bitcode::DEC_EQ, lhs_val, rhs_val),
Builtin::Str => str_equal(env, lhs_val, rhs_val),
Builtin::List(elem) => {
let list_layout = layout_interner.insert(Layout::Builtin(*builtin));
build_list_eq(
env,
layout_interner,
layout_ids,
list_layout,
*elem,
lhs_val.into_struct_value(),
rhs_val.into_struct_value(),
when_recursive,
)
}
Builtin::List(elem) => build_list_eq(
env,
layout_interner,
layout_ids,
builtin_layout,
*elem,
lhs_val.into_struct_value(),
rhs_val.into_struct_value(),
),
}
}
@ -153,7 +144,6 @@ fn build_eq<'a, 'ctx, 'env>(
rhs_val: BasicValueEnum<'ctx>,
lhs_layout: InLayout<'a>,
rhs_layout: InLayout<'a>,
when_recursive: WhenRecursive<'a>,
) -> BasicValueEnum<'ctx> {
let lhs_layout = &layout_interner.runtime_representation_in(lhs_layout);
let rhs_layout = &layout_interner.runtime_representation_in(rhs_layout);
@ -171,16 +161,16 @@ fn build_eq<'a, 'ctx, 'env>(
layout_ids,
lhs_val,
rhs_val,
*lhs_layout,
&builtin,
when_recursive,
),
Layout::Struct { field_layouts, .. } => build_struct_eq(
env,
layout_interner,
layout_ids,
*lhs_layout,
field_layouts,
when_recursive,
lhs_val.into_struct_value(),
rhs_val.into_struct_value(),
),
@ -191,7 +181,7 @@ fn build_eq<'a, 'ctx, 'env>(
env,
layout_interner,
layout_ids,
when_recursive,
*lhs_layout,
&union_layout,
lhs_val,
rhs_val,
@ -201,47 +191,48 @@ fn build_eq<'a, 'ctx, 'env>(
env,
layout_interner,
layout_ids,
when_recursive,
*lhs_layout,
inner_layout,
lhs_val,
rhs_val,
),
Layout::RecursivePointer => match when_recursive {
WhenRecursive::Unreachable => {
unreachable!("recursion pointers should never be compared directly")
}
Layout::RecursivePointer(rec_layout) => {
let layout = rec_layout;
WhenRecursive::Loop(union_layout) => {
let layout = layout_interner.insert(Layout::Union(union_layout));
let bt = basic_type_from_layout(env, layout_interner, layout);
let bt = basic_type_from_layout(env, layout_interner, layout);
// cast the i64 pointer to a pointer to block of memory
let field1_cast = env.builder.build_pointer_cast(
lhs_val.into_pointer_value(),
bt.into_pointer_type(),
"i64_to_opaque",
);
// cast the i64 pointer to a pointer to block of memory
let field1_cast = env.builder.build_pointer_cast(
lhs_val.into_pointer_value(),
bt.into_pointer_type(),
"i64_to_opaque",
);
let field2_cast = env.builder.build_pointer_cast(
rhs_val.into_pointer_value(),
bt.into_pointer_type(),
"i64_to_opaque",
);
let field2_cast = env.builder.build_pointer_cast(
rhs_val.into_pointer_value(),
bt.into_pointer_type(),
"i64_to_opaque",
);
let union_layout = match layout_interner.get(rec_layout) {
Layout::Union(union_layout) => {
debug_assert!(!matches!(union_layout, UnionLayout::NonRecursive(..)));
union_layout
}
_ => internal_error!(),
};
build_tag_eq(
env,
layout_interner,
layout_ids,
WhenRecursive::Loop(union_layout),
&union_layout,
field1_cast.into(),
field2_cast.into(),
)
}
},
build_tag_eq(
env,
layout_interner,
layout_ids,
rec_layout,
&union_layout,
field1_cast.into(),
field2_cast.into(),
)
}
}
}
@ -251,8 +242,8 @@ fn build_neq_builtin<'a, 'ctx, 'env>(
layout_ids: &mut LayoutIds<'a>,
lhs_val: BasicValueEnum<'ctx>,
rhs_val: BasicValueEnum<'ctx>,
builtin_layout: InLayout<'a>,
builtin: &Builtin<'a>,
when_recursive: WhenRecursive<'a>,
) -> BasicValueEnum<'ctx> {
let int_cmp = |pred, label| {
let int_val = env.builder.build_int_compare(
@ -317,7 +308,6 @@ fn build_neq_builtin<'a, 'ctx, 'env>(
result.into()
}
Builtin::List(elem) => {
let builtin_layout = layout_interner.insert(Layout::Builtin(*builtin));
let is_equal = build_list_eq(
env,
layout_interner,
@ -326,7 +316,6 @@ fn build_neq_builtin<'a, 'ctx, 'env>(
*elem,
lhs_val.into_struct_value(),
rhs_val.into_struct_value(),
when_recursive,
)
.into_int_value();
@ -345,7 +334,6 @@ fn build_neq<'a, 'ctx, 'env>(
rhs_val: BasicValueEnum<'ctx>,
lhs_layout: InLayout<'a>,
rhs_layout: InLayout<'a>,
when_recursive: WhenRecursive<'a>,
) -> BasicValueEnum<'ctx> {
if lhs_layout != rhs_layout {
panic!(
@ -361,8 +349,8 @@ fn build_neq<'a, 'ctx, 'env>(
layout_ids,
lhs_val,
rhs_val,
lhs_layout,
&builtin,
when_recursive,
),
Layout::Struct { field_layouts, .. } => {
@ -370,8 +358,8 @@ fn build_neq<'a, 'ctx, 'env>(
env,
layout_interner,
layout_ids,
lhs_layout,
field_layouts,
when_recursive,
lhs_val.into_struct_value(),
rhs_val.into_struct_value(),
)
@ -387,7 +375,7 @@ fn build_neq<'a, 'ctx, 'env>(
env,
layout_interner,
layout_ids,
when_recursive,
lhs_layout,
&union_layout,
lhs_val,
rhs_val,
@ -404,7 +392,6 @@ fn build_neq<'a, 'ctx, 'env>(
env,
layout_interner,
layout_ids,
when_recursive,
lhs_layout,
inner_layout,
lhs_val,
@ -417,7 +404,7 @@ fn build_neq<'a, 'ctx, 'env>(
result.into()
}
Layout::RecursivePointer => {
Layout::RecursivePointer(_) => {
unreachable!("recursion pointers should never be compared directly")
}
Layout::LambdaSet(_) => unreachable!("cannot compare closure"),
@ -432,15 +419,17 @@ fn build_list_eq<'a, 'ctx, 'env>(
element_layout: InLayout<'a>,
list1: StructValue<'ctx>,
list2: StructValue<'ctx>,
when_recursive: WhenRecursive<'a>,
) -> BasicValueEnum<'ctx> {
let block = env.builder.get_insert_block().expect("to be in a function");
let di_location = env.builder.get_current_debug_location().unwrap();
let symbol = Symbol::LIST_EQ;
let element_layout = layout_interner.get(element_layout);
let element_layout = when_recursive.unwrap_recursive_pointer(element_layout);
let element_layout = layout_interner.insert(element_layout);
let element_layout = if let Layout::RecursivePointer(rec) = layout_interner.get(element_layout)
{
rec
} else {
element_layout
};
let fn_name = layout_ids
.get(symbol, &element_layout)
.to_symbol_string(symbol, &env.interns);
@ -461,7 +450,6 @@ fn build_list_eq<'a, 'ctx, 'env>(
env,
layout_interner,
layout_ids,
when_recursive,
function_value,
element_layout,
);
@ -471,8 +459,7 @@ fn build_list_eq<'a, 'ctx, 'env>(
};
env.builder.position_at_end(block);
env.builder
.set_current_debug_location(env.context, di_location);
env.builder.set_current_debug_location(di_location);
let call = env
.builder
.build_call(function, &[list1.into(), list2.into()], "list_eq");
@ -486,7 +473,6 @@ fn build_list_eq_help<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
when_recursive: WhenRecursive<'a>,
parent: FunctionValue<'ctx>,
element_layout: InLayout<'a>,
) {
@ -511,7 +497,7 @@ fn build_list_eq_help<'a, 'ctx, 'env>(
/* current_scope */ lexical_block.as_debug_info_scope(),
/* inlined_at */ None,
);
builder.set_current_debug_location(ctx, loc);
builder.set_current_debug_location(loc);
}
// Add args to scope
@ -548,7 +534,7 @@ fn build_list_eq_help<'a, 'ctx, 'env>(
let builder = env.builder;
let element_type = basic_type_from_layout(env, layout_interner, element_layout);
let ptr_type = element_type.ptr_type(AddressSpace::Generic);
let ptr_type = element_type.ptr_type(AddressSpace::default());
let ptr1 = load_list_ptr(env.builder, list1, ptr_type);
let ptr2 = load_list_ptr(env.builder, list2, ptr_type);
@ -605,7 +591,6 @@ fn build_list_eq_help<'a, 'ctx, 'env>(
elem2,
element_layout,
element_layout,
when_recursive,
)
.into_int_value();
@ -646,16 +631,14 @@ fn build_struct_eq<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
struct_layout: InLayout<'a>,
field_layouts: &'a [InLayout<'a>],
when_recursive: WhenRecursive<'a>,
struct1: StructValue<'ctx>,
struct2: StructValue<'ctx>,
) -> BasicValueEnum<'ctx> {
let block = env.builder.get_insert_block().expect("to be in a function");
let di_location = env.builder.get_current_debug_location().unwrap();
let struct_layout = layout_interner.insert(Layout::struct_no_name_order(field_layouts));
let symbol = Symbol::GENERIC_EQ;
let fn_name = layout_ids
.get(symbol, &struct_layout)
@ -678,7 +661,6 @@ fn build_struct_eq<'a, 'ctx, 'env>(
layout_interner,
layout_ids,
function_value,
when_recursive,
field_layouts,
);
@ -687,8 +669,7 @@ fn build_struct_eq<'a, 'ctx, 'env>(
};
env.builder.position_at_end(block);
env.builder
.set_current_debug_location(env.context, di_location);
env.builder.set_current_debug_location(di_location);
let call = env
.builder
.build_call(function, &[struct1.into(), struct2.into()], "struct_eq");
@ -703,7 +684,6 @@ fn build_struct_eq_help<'a, 'ctx, 'env>(
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
parent: FunctionValue<'ctx>,
when_recursive: WhenRecursive<'a>,
field_layouts: &[InLayout<'a>],
) {
let ctx = env.context;
@ -727,7 +707,7 @@ fn build_struct_eq_help<'a, 'ctx, 'env>(
/* current_scope */ lexical_block.as_debug_info_scope(),
/* inlined_at */ None,
);
builder.set_current_debug_location(ctx, loc);
builder.set_current_debug_location(loc);
}
// Add args to scope
@ -761,42 +741,40 @@ fn build_struct_eq_help<'a, 'ctx, 'env>(
.build_extract_value(struct2, index as u32, "eq_field")
.unwrap();
let are_equal = if let Layout::RecursivePointer = layout_interner.get(*field_layout) {
match &when_recursive {
WhenRecursive::Unreachable => {
unreachable!("The current layout should not be recursive, but is")
}
WhenRecursive::Loop(union_layout) => {
let field_layout = layout_interner.insert(Layout::Union(*union_layout));
let are_equal = if let Layout::RecursivePointer(rec_layout) =
layout_interner.get(*field_layout)
{
debug_assert!(
matches!(layout_interner.get(rec_layout), Layout::Union(union_layout) if !matches!(union_layout, UnionLayout::NonRecursive(..)))
);
let bt = basic_type_from_layout(env, layout_interner, field_layout);
let field_layout = rec_layout;
// cast the i64 pointer to a pointer to block of memory
let field1_cast = env.builder.build_pointer_cast(
field1.into_pointer_value(),
bt.into_pointer_type(),
"i64_to_opaque",
);
let bt = basic_type_from_layout(env, layout_interner, field_layout);
let field2_cast = env.builder.build_pointer_cast(
field2.into_pointer_value(),
bt.into_pointer_type(),
"i64_to_opaque",
);
// cast the i64 pointer to a pointer to block of memory
let field1_cast = env.builder.build_pointer_cast(
field1.into_pointer_value(),
bt.into_pointer_type(),
"i64_to_opaque",
);
build_eq(
env,
layout_interner,
layout_ids,
field1_cast.into(),
field2_cast.into(),
field_layout,
field_layout,
WhenRecursive::Loop(*union_layout),
)
.into_int_value()
}
}
let field2_cast = env.builder.build_pointer_cast(
field2.into_pointer_value(),
bt.into_pointer_type(),
"i64_to_opaque",
);
build_eq(
env,
layout_interner,
layout_ids,
field1_cast.into(),
field2_cast.into(),
field_layout,
field_layout,
)
.into_int_value()
} else {
let lhs = use_roc_value(env, layout_interner, *field_layout, field1, "field1");
let rhs = use_roc_value(env, layout_interner, *field_layout, field2, "field2");
@ -808,7 +786,6 @@ fn build_struct_eq_help<'a, 'ctx, 'env>(
rhs,
*field_layout,
*field_layout,
when_recursive,
)
.into_int_value()
};
@ -839,7 +816,7 @@ fn build_tag_eq<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
when_recursive: WhenRecursive<'a>,
tag_layout: InLayout<'a>,
union_layout: &UnionLayout<'a>,
tag1: BasicValueEnum<'ctx>,
tag2: BasicValueEnum<'ctx>,
@ -847,7 +824,6 @@ fn build_tag_eq<'a, 'ctx, 'env>(
let block = env.builder.get_insert_block().expect("to be in a function");
let di_location = env.builder.get_current_debug_location().unwrap();
let tag_layout = layout_interner.insert(Layout::Union(*union_layout));
let symbol = Symbol::GENERIC_EQ;
let fn_name = layout_ids
.get(symbol, &tag_layout)
@ -869,7 +845,6 @@ fn build_tag_eq<'a, 'ctx, 'env>(
env,
layout_interner,
layout_ids,
when_recursive,
function_value,
union_layout,
);
@ -879,8 +854,7 @@ fn build_tag_eq<'a, 'ctx, 'env>(
};
env.builder.position_at_end(block);
env.builder
.set_current_debug_location(env.context, di_location);
env.builder.set_current_debug_location(di_location);
let call = env
.builder
.build_call(function, &[tag1.into(), tag2.into()], "tag_eq");
@ -894,7 +868,6 @@ fn build_tag_eq_help<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
when_recursive: WhenRecursive<'a>,
parent: FunctionValue<'ctx>,
union_layout: &UnionLayout<'a>,
) {
@ -919,7 +892,7 @@ fn build_tag_eq_help<'a, 'ctx, 'env>(
/* current_scope */ lexical_block.as_debug_info_scope(),
/* inlined_at */ None,
);
builder.set_current_debug_location(ctx, loc);
builder.set_current_debug_location(loc);
}
// Add args to scope
@ -999,12 +972,14 @@ fn build_tag_eq_help<'a, 'ctx, 'env>(
let block = env.context.append_basic_block(parent, "tag_id_modify");
env.builder.position_at_end(block);
let struct_layout =
layout_interner.insert(Layout::struct_no_name_order(field_layouts));
let answer = eq_ptr_to_struct(
env,
layout_interner,
layout_ids,
union_layout,
Some(when_recursive),
struct_layout,
field_layouts,
tag1,
tag2,
@ -1070,12 +1045,14 @@ fn build_tag_eq_help<'a, 'ctx, 'env>(
let block = env.context.append_basic_block(parent, "tag_id_modify");
env.builder.position_at_end(block);
let struct_layout =
layout_interner.insert(Layout::struct_no_name_order(field_layouts));
let answer = eq_ptr_to_struct(
env,
layout_interner,
layout_ids,
union_layout,
None,
struct_layout,
field_layouts,
tag1,
tag2,
@ -1131,12 +1108,13 @@ fn build_tag_eq_help<'a, 'ctx, 'env>(
env.builder.position_at_end(compare_other);
let struct_layout = layout_interner.insert(Layout::struct_no_name_order(other_fields));
let answer = eq_ptr_to_struct(
env,
layout_interner,
layout_ids,
union_layout,
None,
struct_layout,
other_fields,
tag1.into_pointer_value(),
tag2.into_pointer_value(),
@ -1229,12 +1207,14 @@ fn build_tag_eq_help<'a, 'ctx, 'env>(
let block = env.context.append_basic_block(parent, "tag_id_modify");
env.builder.position_at_end(block);
let struct_layout =
layout_interner.insert(Layout::struct_no_name_order(field_layouts));
let answer = eq_ptr_to_struct(
env,
layout_interner,
layout_ids,
union_layout,
None,
struct_layout,
field_layouts,
tag1,
tag2,
@ -1268,12 +1248,13 @@ fn build_tag_eq_help<'a, 'ctx, 'env>(
env.builder.position_at_end(compare_fields);
let struct_layout = layout_interner.insert(Layout::struct_no_name_order(field_layouts));
let answer = eq_ptr_to_struct(
env,
layout_interner,
layout_ids,
union_layout,
None,
struct_layout,
field_layouts,
tag1.into_pointer_value(),
tag2.into_pointer_value(),
@ -1288,27 +1269,24 @@ fn eq_ptr_to_struct<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
union_layout: &UnionLayout<'a>,
opt_when_recursive: Option<WhenRecursive<'a>>,
struct_layout: InLayout<'a>,
field_layouts: &'a [InLayout<'a>],
tag1: PointerValue<'ctx>,
tag2: PointerValue<'ctx>,
) -> IntValue<'ctx> {
let struct_layout = layout_interner.insert(Layout::struct_no_name_order(field_layouts));
let wrapper_type = basic_type_from_layout(env, layout_interner, struct_layout);
debug_assert!(wrapper_type.is_struct_type());
// cast the opaque pointer to a pointer of the correct shape
let struct1_ptr = env.builder.build_pointer_cast(
tag1,
wrapper_type.ptr_type(AddressSpace::Generic),
wrapper_type.ptr_type(AddressSpace::default()),
"opaque_to_correct",
);
let struct2_ptr = env.builder.build_pointer_cast(
tag2,
wrapper_type.ptr_type(AddressSpace::Generic),
wrapper_type.ptr_type(AddressSpace::default()),
"opaque_to_correct",
);
@ -1326,8 +1304,8 @@ fn eq_ptr_to_struct<'a, 'ctx, 'env>(
env,
layout_interner,
layout_ids,
struct_layout,
field_layouts,
opt_when_recursive.unwrap_or(WhenRecursive::Loop(*union_layout)),
struct1,
struct2,
)
@ -1340,7 +1318,6 @@ fn build_box_eq<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
when_recursive: WhenRecursive<'a>,
box_layout: InLayout<'a>,
inner_layout: InLayout<'a>,
tag1: BasicValueEnum<'ctx>,
@ -1370,7 +1347,6 @@ fn build_box_eq<'a, 'ctx, 'env>(
env,
layout_interner,
layout_ids,
when_recursive,
function_value,
inner_layout,
);
@ -1380,8 +1356,7 @@ fn build_box_eq<'a, 'ctx, 'env>(
};
env.builder.position_at_end(block);
env.builder
.set_current_debug_location(env.context, di_location);
env.builder.set_current_debug_location(di_location);
let call = env
.builder
.build_call(function, &[tag1.into(), tag2.into()], "tag_eq");
@ -1395,7 +1370,6 @@ fn build_box_eq_help<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
when_recursive: WhenRecursive<'a>,
parent: FunctionValue<'ctx>,
inner_layout: InLayout<'a>,
) {
@ -1420,7 +1394,7 @@ fn build_box_eq_help<'a, 'ctx, 'env>(
/* current_scope */ lexical_block.as_debug_info_scope(),
/* inlined_at */ None,
);
builder.set_current_debug_location(ctx, loc);
builder.set_current_debug_location(loc);
}
// Add args to scope
@ -1472,7 +1446,6 @@ fn build_box_eq_help<'a, 'ctx, 'env>(
value2,
inner_layout,
inner_layout,
when_recursive,
);
env.builder.build_return(Some(&is_equal));

View file

@ -44,13 +44,13 @@ pub fn basic_type_from_layout<'a, 'ctx, 'env>(
Boxed(inner_layout) => {
let inner_type = basic_type_from_layout(env, layout_interner, inner_layout);
inner_type.ptr_type(AddressSpace::Generic).into()
inner_type.ptr_type(AddressSpace::default()).into()
}
Union(union_layout) => basic_type_from_union_layout(env, layout_interner, &union_layout),
RecursivePointer => env
RecursivePointer(_) => env
.context
.i64_type()
.ptr_type(AddressSpace::Generic)
.ptr_type(AddressSpace::default())
.as_basic_type_enum(),
Builtin(builtin) => basic_type_from_builtin(env, &builtin),
@ -109,7 +109,7 @@ pub fn basic_type_from_union_layout<'a, 'ctx, 'env>(
Recursive(_)
| NonNullableUnwrapped(_)
| NullableWrapped { .. }
| NullableUnwrapped { .. } => struct_type.ptr_type(AddressSpace::Generic).into(),
| NullableUnwrapped { .. } => struct_type.ptr_type(AddressSpace::default()).into(),
}
}
@ -158,7 +158,7 @@ pub fn argument_type_from_layout<'a, 'ctx, 'env>(
let base = basic_type_from_layout(env, layout_interner, layout);
if layout_interner.is_passed_by_reference(layout) {
base.ptr_type(AddressSpace::Generic).into()
base.ptr_type(AddressSpace::default()).into()
} else {
base
}
@ -176,7 +176,7 @@ pub fn argument_type_from_union_layout<'a, 'ctx, 'env>(
let heap_type = basic_type_from_union_layout(env, layout_interner, union_layout);
if let UnionLayout::NonRecursive(_) = union_layout {
heap_type.ptr_type(AddressSpace::Generic).into()
heap_type.ptr_type(AddressSpace::default()).into()
} else {
heap_type
}
@ -375,7 +375,7 @@ impl<'ctx> RocUnion<'ctx> {
let cast_pointer = env.builder.build_pointer_cast(
data_buffer,
data.get_type().ptr_type(AddressSpace::Generic),
data.get_type().ptr_type(AddressSpace::default()),
"to_data_ptr",
);
@ -436,7 +436,7 @@ pub fn zig_dec_type<'a, 'ctx, 'env>(env: &Env<'a, 'ctx, 'env>) -> StructType<'ct
}
pub fn zig_has_tag_id_type<'a, 'ctx, 'env>(env: &Env<'a, 'ctx, 'env>) -> StructType<'ctx> {
let u8_ptr_t = env.context.i8_type().ptr_type(AddressSpace::Generic);
let u8_ptr_t = env.context.i8_type().ptr_type(AddressSpace::default());
env.context
.struct_type(&[env.context.bool_type().into(), u8_ptr_t.into()], false)

View file

@ -9,6 +9,7 @@ use inkwell::types::{BasicMetadataTypeEnum, BasicType, BasicTypeEnum};
use inkwell::values::{BasicValueEnum, FunctionValue, IntValue, PointerValue};
use inkwell::AddressSpace;
use roc_builtins::bitcode;
use roc_error_macros::internal_error;
use roc_module::symbol::Symbol;
use roc_mono::ir::LookupType;
use roc_mono::layout::{
@ -19,7 +20,7 @@ use roc_region::all::Region;
use super::build::BuilderExt;
use super::build::{
add_func, load_roc_value, load_symbol_and_layout, use_roc_value, FunctionSpec, LlvmBackendMode,
Scope, WhenRecursive,
Scope,
};
use super::convert::struct_type_from_union_layout;
@ -98,7 +99,7 @@ fn read_state<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
ptr: PointerValue<'ctx>,
) -> (IntValue<'ctx>, IntValue<'ctx>) {
let ptr_type = env.ptr_int().ptr_type(AddressSpace::Generic);
let ptr_type = env.ptr_int().ptr_type(AddressSpace::default());
let ptr = env.builder.build_pointer_cast(ptr, ptr_type, "");
let one = env.ptr_int().const_int(1, false);
@ -118,7 +119,7 @@ fn write_state<'a, 'ctx, 'env>(
count: IntValue<'ctx>,
offset: IntValue<'ctx>,
) {
let ptr_type = env.ptr_int().ptr_type(AddressSpace::Generic);
let ptr_type = env.ptr_int().ptr_type(AddressSpace::default());
let ptr = env.builder.build_pointer_cast(ptr, ptr_type, "");
let one = env.ptr_int().const_int(1, false);
@ -220,7 +221,6 @@ pub(crate) fn clone_to_shared_memory<'a, 'ctx, 'env>(
cursors,
value,
layout,
WhenRecursive::Unreachable,
);
offset = extra_offset;
@ -252,7 +252,7 @@ pub(crate) fn clone_to_shared_memory<'a, 'ctx, 'env>(
)
};
let u32_ptr = env.context.i32_type().ptr_type(AddressSpace::Generic);
let u32_ptr = env.context.i32_type().ptr_type(AddressSpace::default());
let ptr = env
.builder
.build_pointer_cast(ptr, u32_ptr, "cast_ptr_type");
@ -286,7 +286,6 @@ fn build_clone<'a, 'ctx, 'env>(
cursors: Cursors<'ctx>,
value: BasicValueEnum<'ctx>,
layout: InLayout<'a>,
when_recursive: WhenRecursive<'a>,
) -> IntValue<'ctx> {
match layout_interner.get(layout) {
Layout::Builtin(builtin) => build_clone_builtin(
@ -297,7 +296,6 @@ fn build_clone<'a, 'ctx, 'env>(
cursors,
value,
builtin,
when_recursive,
),
Layout::Struct { field_layouts, .. } => build_clone_struct(
@ -308,7 +306,6 @@ fn build_clone<'a, 'ctx, 'env>(
cursors,
value,
field_layouts,
when_recursive,
),
// Since we will never actually display functions (and hence lambda sets)
@ -326,7 +323,7 @@ fn build_clone<'a, 'ctx, 'env>(
)
};
let ptr_type = value.get_type().ptr_type(AddressSpace::Generic);
let ptr_type = value.get_type().ptr_type(AddressSpace::default());
let ptr = env
.builder
.build_pointer_cast(ptr, ptr_type, "cast_ptr_type");
@ -343,7 +340,6 @@ fn build_clone<'a, 'ctx, 'env>(
cursors,
value,
union_layout,
WhenRecursive::Loop(union_layout),
)
}
}
@ -376,39 +372,39 @@ fn build_clone<'a, 'ctx, 'env>(
cursors,
value,
inner_layout,
when_recursive,
)
}
Layout::RecursivePointer => match when_recursive {
WhenRecursive::Unreachable => {
unreachable!("recursion pointers should never be compared directly")
}
Layout::RecursivePointer(rec_layout) => {
let layout = rec_layout;
WhenRecursive::Loop(union_layout) => {
let layout = layout_interner.insert(Layout::Union(union_layout));
let bt = basic_type_from_layout(env, layout_interner, layout);
let bt = basic_type_from_layout(env, layout_interner, layout);
// cast the i64 pointer to a pointer to block of memory
let field1_cast = env.builder.build_pointer_cast(
value.into_pointer_value(),
bt.into_pointer_type(),
"i64_to_opaque",
);
// cast the i64 pointer to a pointer to block of memory
let field1_cast = env.builder.build_pointer_cast(
value.into_pointer_value(),
bt.into_pointer_type(),
"i64_to_opaque",
);
let union_layout = match layout_interner.get(rec_layout) {
Layout::Union(union_layout) => {
debug_assert!(!matches!(union_layout, UnionLayout::NonRecursive(..)));
union_layout
}
_ => internal_error!(),
};
build_clone_tag(
env,
layout_interner,
layout_ids,
ptr,
cursors,
field1_cast.into(),
union_layout,
WhenRecursive::Loop(union_layout),
)
}
},
build_clone_tag(
env,
layout_interner,
layout_ids,
ptr,
cursors,
field1_cast.into(),
union_layout,
)
}
}
}
@ -420,7 +416,6 @@ fn build_clone_struct<'a, 'ctx, 'env>(
cursors: Cursors<'ctx>,
value: BasicValueEnum<'ctx>,
field_layouts: &[InLayout<'a>],
when_recursive: WhenRecursive<'a>,
) -> IntValue<'ctx> {
let layout = Layout::struct_no_name_order(field_layouts);
@ -447,7 +442,6 @@ fn build_clone_struct<'a, 'ctx, 'env>(
cursors,
field,
*field_layout,
when_recursive,
);
let field_width = env
@ -472,7 +466,6 @@ fn build_clone_tag<'a, 'ctx, 'env>(
cursors: Cursors<'ctx>,
value: BasicValueEnum<'ctx>,
union_layout: UnionLayout<'a>,
when_recursive: WhenRecursive<'a>,
) -> IntValue<'ctx> {
let layout = layout_interner.insert(Layout::Union(union_layout));
let layout_id = layout_ids.get(Symbol::CLONE, &layout);
@ -486,7 +479,10 @@ fn build_clone_tag<'a, 'ctx, 'env>(
let function_type = env.ptr_int().fn_type(
&[
env.context.i8_type().ptr_type(AddressSpace::Generic).into(),
env.context
.i8_type()
.ptr_type(AddressSpace::default())
.into(),
env.ptr_int().into(),
env.ptr_int().into(),
BasicMetadataTypeEnum::from(value.get_type()),
@ -512,13 +508,11 @@ fn build_clone_tag<'a, 'ctx, 'env>(
layout_interner,
layout_ids,
union_layout,
when_recursive,
function_value,
);
env.builder.position_at_end(block);
env.builder
.set_current_debug_location(env.context, di_location);
env.builder.set_current_debug_location(di_location);
function_value
}
@ -563,7 +557,7 @@ fn load_tag_data<'a, 'ctx, 'env>(
let data_ptr = env.builder.build_pointer_cast(
raw_data_ptr,
tag_type.ptr_type(AddressSpace::Generic),
tag_type.ptr_type(AddressSpace::default()),
"data_ptr",
);
@ -575,7 +569,6 @@ fn build_clone_tag_help<'a, 'ctx, 'env>(
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
union_layout: UnionLayout<'a>,
when_recursive: WhenRecursive<'a>,
fn_val: FunctionValue<'ctx>,
) {
use bumpalo::collections::Vec;
@ -643,16 +636,8 @@ fn build_clone_tag_help<'a, 'ctx, 'env>(
basic_type,
);
let answer = build_clone(
env,
layout_interner,
layout_ids,
ptr,
cursors,
data,
layout,
when_recursive,
);
let answer =
build_clone(env, layout_interner, layout_ids, ptr, cursors, data, layout);
env.builder.build_return(Some(&answer));
@ -712,17 +697,8 @@ fn build_clone_tag_help<'a, 'ctx, 'env>(
),
};
let when_recursive = WhenRecursive::Loop(union_layout);
let answer = build_clone(
env,
layout_interner,
layout_ids,
ptr,
cursors,
data,
layout,
when_recursive,
);
let answer =
build_clone(env, layout_interner, layout_ids, ptr, cursors, data, layout);
env.builder.build_return(Some(&answer));
@ -762,17 +738,7 @@ fn build_clone_tag_help<'a, 'ctx, 'env>(
let data = load_tag_data(env, layout_interner, union_layout, tag_value, basic_type);
let when_recursive = WhenRecursive::Loop(union_layout);
let answer = build_clone(
env,
layout_interner,
layout_ids,
ptr,
cursors,
data,
layout,
when_recursive,
);
let answer = build_clone(env, layout_interner, layout_ids, ptr, cursors, data, layout);
env.builder.build_return(Some(&answer));
}
@ -831,17 +797,8 @@ fn build_clone_tag_help<'a, 'ctx, 'env>(
let data =
load_tag_data(env, layout_interner, union_layout, tag_value, basic_type);
let when_recursive = WhenRecursive::Loop(union_layout);
let answer = build_clone(
env,
layout_interner,
layout_ids,
ptr,
cursors,
data,
layout,
when_recursive,
);
let answer =
build_clone(env, layout_interner, layout_ids, ptr, cursors, data, layout);
env.builder.build_return(Some(&answer));
@ -917,17 +874,8 @@ fn build_clone_tag_help<'a, 'ctx, 'env>(
basic_type,
);
let when_recursive = WhenRecursive::Loop(union_layout);
let answer = build_clone(
env,
layout_interner,
layout_ids,
ptr,
cursors,
data,
layout,
when_recursive,
);
let answer =
build_clone(env, layout_interner, layout_ids, ptr, cursors, data, layout);
env.builder.build_return(Some(&answer));
}
@ -978,7 +926,7 @@ fn build_copy<'a, 'ctx, 'env>(
)
};
let ptr_type = value.get_type().ptr_type(AddressSpace::Generic);
let ptr_type = value.get_type().ptr_type(AddressSpace::default());
let ptr = env
.builder
.build_pointer_cast(ptr, ptr_type, "cast_ptr_type");
@ -997,7 +945,6 @@ fn build_clone_builtin<'a, 'ctx, 'env>(
cursors: Cursors<'ctx>,
value: BasicValueEnum<'ctx>,
builtin: Builtin<'a>,
when_recursive: WhenRecursive<'a>,
) -> IntValue<'ctx> {
use Builtin::*;
@ -1051,7 +998,7 @@ fn build_clone_builtin<'a, 'ctx, 'env>(
let dest = pointer_at_offset(bd, env.context.i8_type(), ptr, elements_start_offset);
let src = bd.build_pointer_cast(
elements,
env.context.i8_type().ptr_type(AddressSpace::Generic),
env.context.i8_type().ptr_type(AddressSpace::default()),
"to_bytes_pointer",
);
bd.build_memcpy(dest, 1, src, 1, elements_width).unwrap();
@ -1061,7 +1008,7 @@ fn build_clone_builtin<'a, 'ctx, 'env>(
let element_type = basic_type_from_layout(env, layout_interner, elem);
let elements = bd.build_pointer_cast(
elements,
element_type.ptr_type(AddressSpace::Generic),
element_type.ptr_type(AddressSpace::default()),
"elements",
);
@ -1102,7 +1049,6 @@ fn build_clone_builtin<'a, 'ctx, 'env>(
cursors,
element,
elem,
when_recursive,
);
bd.build_store(rest_offset, new_offset);

View file

@ -4,7 +4,6 @@ use crate::llvm::build::{CCReturn, Env, FunctionSpec};
use crate::llvm::convert::zig_str_type;
use inkwell::module::Linkage;
use inkwell::types::BasicType;
use inkwell::values::BasicValue;
use inkwell::AddressSpace;
use roc_builtins::bitcode;
@ -19,7 +18,7 @@ pub fn add_default_roc_externs(env: &Env<'_, '_, '_>) {
let builder = env.builder;
let usize_type = env.ptr_int();
let i8_ptr_type = ctx.i8_type().ptr_type(AddressSpace::Generic);
let i8_ptr_type = ctx.i8_type().ptr_type(AddressSpace::default());
match env.mode {
super::build::LlvmBackendMode::CliTest => {
@ -271,7 +270,7 @@ pub fn build_longjmp_call(env: &Env) {
// Call the LLVM-intrinsic longjmp: `void @llvm.eh.sjlj.longjmp(i8* %setjmp_buf)`
let jmp_buf_i8p = env.builder.build_pointer_cast(
jmp_buf,
env.context.i8_type().ptr_type(AddressSpace::Generic),
env.context.i8_type().ptr_type(AddressSpace::default()),
"jmp_buf i8*",
);
let _call = env.build_intrinsic_call(LLVM_LONGJMP, &[jmp_buf_i8p.into()]);

View file

@ -75,7 +75,7 @@ pub(crate) fn add_intrinsics<'ctx>(ctx: &'ctx Context, module: &Module<'ctx>) {
// https://releases.llvm.org/10.0.0/docs/LangRef.html#standard-c-library-intrinsics
let i1_type = ctx.bool_type();
let i8_type = ctx.i8_type();
let i8_ptr_type = i8_type.ptr_type(AddressSpace::Generic);
let i8_ptr_type = i8_type.ptr_type(AddressSpace::default());
let i32_type = ctx.i32_type();
let void_type = ctx.void_type();

View file

@ -13,6 +13,7 @@ use roc_module::{low_level::LowLevel, symbol::Symbol};
use roc_mono::{
ir::HigherOrderLowLevel,
layout::{Builtin, InLayout, LambdaSet, Layout, LayoutIds, LayoutInterner, STLayoutInterner},
list_element_layout,
};
use roc_target::PtrWidth;
@ -49,15 +50,6 @@ use super::{
convert::zig_dec_type,
};
macro_rules! list_element_layout {
($interner:expr, $list_layout:expr) => {
match $interner.get($list_layout) {
Layout::Builtin(Builtin::List(list_layout)) => list_layout,
_ => unreachable!("invalid list layout"),
}
};
}
pub(crate) fn run_low_level<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_interner: &mut STLayoutInterner<'a>,
@ -256,7 +248,7 @@ pub(crate) fn run_low_level<'a, 'ctx, 'env>(
let roc_return_type =
basic_type_from_layout(env, layout_interner, layout)
.ptr_type(AddressSpace::Generic);
.ptr_type(AddressSpace::default());
let roc_return_alloca = env.builder.build_pointer_cast(
zig_return_alloca,
@ -497,7 +489,7 @@ pub(crate) fn run_low_level<'a, 'ctx, 'env>(
let return_type = basic_type_from_layout(env, layout_interner, layout);
let cast_result = env.builder.build_pointer_cast(
result,
return_type.ptr_type(AddressSpace::Generic),
return_type.ptr_type(AddressSpace::default()),
"cast",
);
@ -1663,7 +1655,7 @@ fn dec_alloca<'a, 'ctx, 'env>(
let ptr = env.builder.build_pointer_cast(
alloca,
value.get_type().ptr_type(AddressSpace::Generic),
value.get_type().ptr_type(AddressSpace::default()),
"cast_to_i128_ptr",
);
@ -2013,7 +2005,7 @@ fn build_int_unary_op<'a, 'ctx, 'env>(
let roc_return_type =
basic_type_from_layout(env, layout_interner, return_layout)
.ptr_type(AddressSpace::Generic);
.ptr_type(AddressSpace::default());
let roc_return_alloca = env.builder.build_pointer_cast(
zig_return_alloca,

View file

@ -3,7 +3,7 @@ use crate::llvm::bitcode::call_void_bitcode_fn;
use crate::llvm::build::BuilderExt;
use crate::llvm::build::{
add_func, cast_basic_basic, get_tag_id, tag_pointer_clear_tag_id, use_roc_value, Env,
WhenRecursive, FAST_CALL_CONV,
FAST_CALL_CONV,
};
use crate::llvm::build_list::{incrementing_elem_loop, list_capacity, load_list};
use crate::llvm::convert::{basic_type_from_layout, zig_str_type, RocUnion};
@ -11,9 +11,7 @@ use bumpalo::collections::Vec;
use inkwell::basic_block::BasicBlock;
use inkwell::module::Linkage;
use inkwell::types::{AnyTypeEnum, BasicMetadataTypeEnum, BasicType, BasicTypeEnum};
use inkwell::values::{
BasicValue, BasicValueEnum, FunctionValue, IntValue, PointerValue, StructValue,
};
use inkwell::values::{BasicValueEnum, FunctionValue, IntValue, PointerValue, StructValue};
use inkwell::{AddressSpace, IntPredicate};
use roc_module::symbol::Interns;
use roc_module::symbol::Symbol;
@ -40,7 +38,7 @@ impl<'ctx> PointerToRefcount<'ctx> {
let value = env.builder.build_pointer_cast(
ptr,
refcount_type.ptr_type(AddressSpace::Generic),
refcount_type.ptr_type(AddressSpace::default()),
"to_refcount_ptr",
);
@ -54,7 +52,7 @@ impl<'ctx> PointerToRefcount<'ctx> {
let builder = env.builder;
// pointer to usize
let refcount_type = env.ptr_int();
let refcount_ptr_type = refcount_type.ptr_type(AddressSpace::Generic);
let refcount_ptr_type = refcount_type.ptr_type(AddressSpace::default());
let ptr_as_usize_ptr =
builder.build_pointer_cast(data_ptr, refcount_ptr_type, "as_usize_ptr");
@ -148,7 +146,7 @@ impl<'ctx> PointerToRefcount<'ctx> {
None => {
// inc and dec return void
let fn_type = context.void_type().fn_type(
&[env.ptr_int().ptr_type(AddressSpace::Generic).into()],
&[env.ptr_int().ptr_type(AddressSpace::default()).into()],
false,
);
@ -172,8 +170,7 @@ impl<'ctx> PointerToRefcount<'ctx> {
let refcount_ptr = self.value;
env.builder.position_at_end(block);
env.builder
.set_current_debug_location(env.context, di_location);
env.builder.set_current_debug_location(di_location);
let call = env
.builder
@ -216,7 +213,7 @@ fn incref_pointer<'a, 'ctx, 'env>(
env.builder
.build_pointer_cast(
pointer,
env.ptr_int().ptr_type(AddressSpace::Generic),
env.ptr_int().ptr_type(AddressSpace::default()),
"to_isize_ptr",
)
.into(),
@ -238,7 +235,7 @@ fn decref_pointer<'a, 'ctx, 'env>(
env.builder
.build_pointer_cast(
pointer,
env.ptr_int().ptr_type(AddressSpace::Generic),
env.ptr_int().ptr_type(AddressSpace::default()),
"to_isize_ptr",
)
.into(),
@ -261,7 +258,7 @@ pub fn decref_pointer_check_null<'a, 'ctx, 'env>(
env.builder
.build_pointer_cast(
pointer,
env.context.i8_type().ptr_type(AddressSpace::Generic),
env.context.i8_type().ptr_type(AddressSpace::default()),
"to_i8_ptr",
)
.into(),
@ -277,7 +274,6 @@ fn modify_refcount_struct<'a, 'ctx, 'env>(
layout_ids: &mut LayoutIds<'a>,
layouts: &'a [InLayout<'a>],
mode: Mode,
when_recursive: &WhenRecursive<'a>,
) -> FunctionValue<'ctx> {
let block = env.builder.get_insert_block().expect("to be in a function");
let di_location = env.builder.get_current_debug_location().unwrap();
@ -304,7 +300,6 @@ fn modify_refcount_struct<'a, 'ctx, 'env>(
layout_interner,
layout_ids,
mode,
when_recursive,
layouts,
function_value,
);
@ -314,8 +309,7 @@ fn modify_refcount_struct<'a, 'ctx, 'env>(
};
env.builder.position_at_end(block);
env.builder
.set_current_debug_location(env.context, di_location);
env.builder.set_current_debug_location(di_location);
function
}
@ -326,7 +320,6 @@ fn modify_refcount_struct_help<'a, 'ctx, 'env>(
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
mode: Mode,
when_recursive: &WhenRecursive<'a>,
layouts: &[InLayout<'a>],
fn_val: FunctionValue<'ctx>,
) {
@ -368,7 +361,6 @@ fn modify_refcount_struct_help<'a, 'ctx, 'env>(
layout_interner,
layout_ids,
mode.to_call_mode(fn_val),
when_recursive,
field_value,
*field_layout,
);
@ -430,7 +422,6 @@ fn modify_refcount_builtin<'a, 'ctx, 'env>(
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
mode: Mode,
when_recursive: &WhenRecursive<'a>,
layout: InLayout<'a>,
builtin: &Builtin<'a>,
) -> Option<FunctionValue<'ctx>> {
@ -438,14 +429,8 @@ fn modify_refcount_builtin<'a, 'ctx, 'env>(
match builtin {
List(element_layout) => {
let function = modify_refcount_list(
env,
layout_interner,
layout_ids,
mode,
when_recursive,
*element_layout,
);
let function =
modify_refcount_list(env, layout_interner, layout_ids, mode, *element_layout);
Some(function)
}
@ -473,15 +458,7 @@ fn modify_refcount_layout<'a, 'ctx, 'env>(
value: BasicValueEnum<'ctx>,
layout: InLayout<'a>,
) {
modify_refcount_layout_help(
env,
layout_interner,
layout_ids,
call_mode,
&WhenRecursive::Unreachable,
value,
layout,
);
modify_refcount_layout_help(env, layout_interner, layout_ids, call_mode, value, layout);
}
fn modify_refcount_layout_help<'a, 'ctx, 'env>(
@ -489,7 +466,6 @@ fn modify_refcount_layout_help<'a, 'ctx, 'env>(
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
call_mode: CallMode<'ctx>,
when_recursive: &WhenRecursive<'a>,
value: BasicValueEnum<'ctx>,
layout: InLayout<'a>,
) {
@ -498,38 +474,28 @@ fn modify_refcount_layout_help<'a, 'ctx, 'env>(
CallMode::Dec => Mode::Dec,
};
let function = match modify_refcount_layout_build_function(
env,
layout_interner,
layout_ids,
mode,
when_recursive,
layout,
) {
Some(f) => f,
None => return,
};
let function =
match modify_refcount_layout_build_function(env, layout_interner, layout_ids, mode, layout)
{
Some(f) => f,
None => return,
};
match layout_interner.get(layout) {
Layout::RecursivePointer => match when_recursive {
WhenRecursive::Unreachable => {
unreachable!("recursion pointers should never be hashed directly")
}
WhenRecursive::Loop(union_layout) => {
let layout = layout_interner.insert(Layout::Union(*union_layout));
Layout::RecursivePointer(rec_layout) => {
let layout = rec_layout;
let bt = basic_type_from_layout(env, layout_interner, layout);
let bt = basic_type_from_layout(env, layout_interner, layout);
// cast the i64 pointer to a pointer to block of memory
let field_cast = env.builder.build_pointer_cast(
value.into_pointer_value(),
bt.into_pointer_type(),
"i64_to_opaque",
);
// cast the i64 pointer to a pointer to block of memory
let field_cast = env.builder.build_pointer_cast(
value.into_pointer_value(),
bt.into_pointer_type(),
"i64_to_opaque",
);
call_help(env, function, call_mode, field_cast.into());
}
},
call_help(env, function, call_mode, field_cast.into());
}
_ => {
call_help(env, function, call_mode, value);
}
@ -568,21 +534,14 @@ fn modify_refcount_layout_build_function<'a, 'ctx, 'env>(
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
mode: Mode,
when_recursive: &WhenRecursive<'a>,
layout: InLayout<'a>,
) -> Option<FunctionValue<'ctx>> {
use Layout::*;
match layout_interner.get(layout) {
Builtin(builtin) => modify_refcount_builtin(
env,
layout_interner,
layout_ids,
mode,
when_recursive,
layout,
&builtin,
),
Builtin(builtin) => {
modify_refcount_builtin(env, layout_interner, layout_ids, mode, layout, &builtin)
}
Boxed(inner) => {
let function = modify_refcount_boxed(env, layout_interner, layout_ids, mode, inner);
@ -600,27 +559,14 @@ fn modify_refcount_layout_build_function<'a, 'ctx, 'env>(
}
NonRecursive(tags) => {
let function = modify_refcount_nonrecursive(
env,
layout_interner,
layout_ids,
mode,
when_recursive,
tags,
);
let function =
modify_refcount_nonrecursive(env, layout_interner, layout_ids, mode, tags);
Some(function)
}
_ => {
let function = build_rec_union(
env,
layout_interner,
layout_ids,
mode,
&WhenRecursive::Loop(variant),
variant,
);
let function = build_rec_union(env, layout_interner, layout_ids, mode, variant);
Some(function)
}
@ -628,43 +574,30 @@ fn modify_refcount_layout_build_function<'a, 'ctx, 'env>(
}
Struct { field_layouts, .. } => {
let function = modify_refcount_struct(
env,
layout_interner,
layout_ids,
field_layouts,
mode,
when_recursive,
);
let function =
modify_refcount_struct(env, layout_interner, layout_ids, field_layouts, mode);
Some(function)
}
Layout::RecursivePointer => match when_recursive {
WhenRecursive::Unreachable => {
unreachable!("recursion pointers cannot be in/decremented directly")
}
WhenRecursive::Loop(union_layout) => {
let layout = layout_interner.insert(Layout::Union(*union_layout));
Layout::RecursivePointer(rec_layout) => {
let layout = rec_layout;
let function = modify_refcount_layout_build_function(
env,
layout_interner,
layout_ids,
mode,
when_recursive,
layout,
)?;
let function = modify_refcount_layout_build_function(
env,
layout_interner,
layout_ids,
mode,
layout,
)?;
Some(function)
}
},
Some(function)
}
LambdaSet(lambda_set) => modify_refcount_layout_build_function(
env,
layout_interner,
layout_ids,
mode,
when_recursive,
lambda_set.runtime_representation(),
),
}
@ -675,15 +608,18 @@ fn modify_refcount_list<'a, 'ctx, 'env>(
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
mode: Mode,
when_recursive: &WhenRecursive<'a>,
element_layout: InLayout<'a>,
) -> FunctionValue<'ctx> {
let block = env.builder.get_insert_block().expect("to be in a function");
let di_location = env.builder.get_current_debug_location().unwrap();
let element_layout = layout_interner.get(element_layout);
let element_layout = when_recursive.unwrap_recursive_pointer(element_layout);
let element_layout = layout_interner.insert(element_layout);
let element_layout = if let Layout::RecursivePointer(rec) = layout_interner.get(element_layout)
{
rec
} else {
element_layout
};
let list_layout = layout_interner.insert(Layout::Builtin(Builtin::List(element_layout)));
let (_, fn_name) = function_name_from_mode(
layout_ids,
@ -705,7 +641,6 @@ fn modify_refcount_list<'a, 'ctx, 'env>(
layout_interner,
layout_ids,
mode,
when_recursive,
list_layout,
element_layout,
function_value,
@ -716,8 +651,7 @@ fn modify_refcount_list<'a, 'ctx, 'env>(
};
env.builder.position_at_end(block);
env.builder
.set_current_debug_location(env.context, di_location);
env.builder.set_current_debug_location(di_location);
function
}
@ -734,7 +668,6 @@ fn modify_refcount_list_help<'a, 'ctx, 'env>(
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
mode: Mode,
when_recursive: &WhenRecursive<'a>,
layout: InLayout<'a>,
element_layout: InLayout<'a>,
fn_val: FunctionValue<'ctx>,
@ -777,7 +710,7 @@ fn modify_refcount_list_help<'a, 'ctx, 'env>(
if layout_interner.contains_refcounted(element_layout) {
let ptr_type = basic_type_from_layout(env, layout_interner, element_layout)
.ptr_type(AddressSpace::Generic);
.ptr_type(AddressSpace::default());
let (len, ptr) = load_list(env.builder, original_wrapper, ptr_type);
@ -787,7 +720,6 @@ fn modify_refcount_list_help<'a, 'ctx, 'env>(
layout_interner,
layout_ids,
mode.to_call_mode(fn_val),
when_recursive,
element,
element_layout,
);
@ -849,8 +781,7 @@ fn modify_refcount_str<'a, 'ctx, 'env>(
};
env.builder.position_at_end(block);
env.builder
.set_current_debug_location(env.context, di_location);
env.builder.set_current_debug_location(di_location);
function
}
@ -959,8 +890,7 @@ fn modify_refcount_boxed<'a, 'ctx, 'env>(
};
env.builder.position_at_end(block);
env.builder
.set_current_debug_location(env.context, di_location);
env.builder.set_current_debug_location(di_location);
function
}
@ -1093,7 +1023,6 @@ fn build_rec_union<'a, 'ctx, 'env>(
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
mode: Mode,
when_recursive: &WhenRecursive<'a>,
union_layout: UnionLayout<'a>,
) -> FunctionValue<'ctx> {
let layout = layout_interner.insert(Layout::Union(union_layout));
@ -1121,14 +1050,12 @@ fn build_rec_union<'a, 'ctx, 'env>(
layout_interner,
layout_ids,
mode,
when_recursive,
union_layout,
function_value,
);
env.builder.position_at_end(block);
env.builder
.set_current_debug_location(env.context, di_location);
env.builder.set_current_debug_location(di_location);
function_value
}
@ -1143,7 +1070,6 @@ fn build_rec_union_help<'a, 'ctx, 'env>(
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
mode: Mode,
when_recursive: &WhenRecursive<'a>,
union_layout: UnionLayout<'a>,
fn_val: FunctionValue<'ctx>,
) {
@ -1235,7 +1161,6 @@ fn build_rec_union_help<'a, 'ctx, 'env>(
env,
layout_interner,
layout_ids,
when_recursive,
parent,
fn_val,
union_layout,
@ -1268,7 +1193,6 @@ fn build_rec_union_recursive_decrement<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
when_recursive: &WhenRecursive<'a>,
parent: FunctionValue<'ctx>,
decrement_fn: FunctionValue<'ctx>,
union_layout: UnionLayout<'a>,
@ -1316,7 +1240,7 @@ fn build_rec_union_recursive_decrement<'a, 'ctx, 'env>(
// cast the opaque pointer to a pointer of the correct shape
let struct_ptr = env.builder.build_pointer_cast(
value_ptr,
wrapper_type.ptr_type(AddressSpace::Generic),
wrapper_type.ptr_type(AddressSpace::default()),
"opaque_to_correct_recursive_decrement",
);
@ -1326,7 +1250,7 @@ fn build_rec_union_recursive_decrement<'a, 'ctx, 'env>(
let mut deferred_nonrec = Vec::new_in(env.arena);
for (i, field_layout) in field_layouts.iter().enumerate() {
if let Layout::RecursivePointer = layout_interner.get(*field_layout) {
if let Layout::RecursivePointer(_) = layout_interner.get(*field_layout) {
// this field has type `*i64`, but is really a pointer to the data we want
let elem_pointer = env
.builder
@ -1339,7 +1263,7 @@ fn build_rec_union_recursive_decrement<'a, 'ctx, 'env>(
.unwrap();
let ptr_as_i64_ptr = env.builder.new_build_load(
env.context.i64_type().ptr_type(AddressSpace::Generic),
env.context.i64_type().ptr_type(AddressSpace::default()),
elem_pointer,
"load_recursive_pointer",
);
@ -1396,7 +1320,6 @@ fn build_rec_union_recursive_decrement<'a, 'ctx, 'env>(
layout_interner,
layout_ids,
mode.to_call_mode(decrement_fn),
when_recursive,
field,
*field_layout,
);
@ -1503,15 +1426,7 @@ pub fn build_reset<'a, 'ctx, 'env>(
let fn_name = layout_id.to_symbol_string(Symbol::DEC, &env.interns);
let fn_name = format!("{}_reset", fn_name);
let when_recursive = WhenRecursive::Loop(union_layout);
let dec_function = build_rec_union(
env,
layout_interner,
layout_ids,
Mode::Dec,
&when_recursive,
union_layout,
);
let dec_function = build_rec_union(env, layout_interner, layout_ids, Mode::Dec, union_layout);
let function = match env.module.get_function(fn_name.as_str()) {
Some(function_value) => function_value,
@ -1526,15 +1441,13 @@ pub fn build_reset<'a, 'ctx, 'env>(
env,
layout_interner,
layout_ids,
&when_recursive,
union_layout,
function_value,
dec_function,
);
env.builder.position_at_end(block);
env.builder
.set_current_debug_location(env.context, di_location);
env.builder.set_current_debug_location(di_location);
function_value
}
@ -1548,7 +1461,6 @@ fn build_reuse_rec_union_help<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
when_recursive: &WhenRecursive<'a>,
union_layout: UnionLayout<'a>,
reset_function: FunctionValue<'ctx>,
dec_function: FunctionValue<'ctx>,
@ -1626,7 +1538,6 @@ fn build_reuse_rec_union_help<'a, 'ctx, 'env>(
env,
layout_interner,
layout_ids,
when_recursive,
parent,
dec_function,
union_layout,
@ -1665,7 +1576,6 @@ fn modify_refcount_nonrecursive<'a, 'ctx, 'env>(
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
mode: Mode,
when_recursive: &WhenRecursive<'a>,
fields: &'a [&'a [InLayout<'a>]],
) -> FunctionValue<'ctx> {
let union_layout = UnionLayout::NonRecursive(fields);
@ -1694,7 +1604,6 @@ fn modify_refcount_nonrecursive<'a, 'ctx, 'env>(
layout_interner,
layout_ids,
mode,
when_recursive,
fields,
function_value,
);
@ -1704,8 +1613,7 @@ fn modify_refcount_nonrecursive<'a, 'ctx, 'env>(
};
env.builder.position_at_end(block);
env.builder
.set_current_debug_location(env.context, di_location);
env.builder.set_current_debug_location(di_location);
function
}
@ -1715,7 +1623,6 @@ fn modify_refcount_nonrecursive_help<'a, 'ctx, 'env>(
layout_interner: &mut STLayoutInterner<'a>,
layout_ids: &mut LayoutIds<'a>,
mode: Mode,
when_recursive: &WhenRecursive<'a>,
tags: &'a [&'a [InLayout<'a>]],
fn_val: FunctionValue<'ctx>,
) {
@ -1805,19 +1712,12 @@ fn modify_refcount_nonrecursive_help<'a, 'ctx, 'env>(
let cast_tag_data_pointer = env.builder.build_pointer_cast(
opaque_tag_data_ptr,
data_struct_type.ptr_type(AddressSpace::Generic),
data_struct_type.ptr_type(AddressSpace::default()),
"cast_to_concrete_tag",
);
for (i, field_layout) in field_layouts.iter().enumerate() {
if let Layout::RecursivePointer = layout_interner.get(*field_layout) {
let recursive_union_layout = match when_recursive {
WhenRecursive::Unreachable => {
panic!("non-recursive tag unions cannot contain naked recursion pointers!");
}
WhenRecursive::Loop(recursive_union_layout) => recursive_union_layout,
};
if let Layout::RecursivePointer(union_layout) = layout_interner.get(*field_layout) {
// This field is a pointer to the recursive pointer.
let field_ptr = env
.builder
@ -1831,7 +1731,7 @@ fn modify_refcount_nonrecursive_help<'a, 'ctx, 'env>(
// This is the actual pointer to the recursive data.
let field_value = env.builder.new_build_load(
env.context.i64_type().ptr_type(AddressSpace::Generic),
env.context.i64_type().ptr_type(AddressSpace::default()),
field_ptr,
"load_recursive_pointer",
);
@ -1839,7 +1739,6 @@ fn modify_refcount_nonrecursive_help<'a, 'ctx, 'env>(
debug_assert!(field_value.is_pointer_value());
// therefore we must cast it to our desired type
let union_layout = layout_interner.insert(Layout::Union(*recursive_union_layout));
let union_type = basic_type_from_layout(env, layout_interner, union_layout);
let recursive_ptr_field_value =
cast_basic_basic(env.builder, field_value, union_type);
@ -1849,9 +1748,8 @@ fn modify_refcount_nonrecursive_help<'a, 'ctx, 'env>(
layout_interner,
layout_ids,
mode.to_call_mode(fn_val),
when_recursive,
recursive_ptr_field_value,
Layout::RECURSIVE_PTR,
*field_layout,
)
} else if layout_interner.contains_refcounted(*field_layout) {
let field_ptr = env
@ -1879,7 +1777,6 @@ fn modify_refcount_nonrecursive_help<'a, 'ctx, 'env>(
layout_interner,
layout_ids,
mode.to_call_mode(fn_val),
when_recursive,
field_value,
*field_layout,
);