mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-04 04:08:19 +00:00
reset ref implementation
This commit is contained in:
parent
d4ed6f7778
commit
9e8b1c0dee
5 changed files with 214 additions and 82 deletions
|
@ -5,8 +5,7 @@ use crate::llvm::convert::{
|
|||
};
|
||||
use crate::llvm::expect::{clone_to_shared_memory, SharedMemoryPointer};
|
||||
use crate::llvm::refcounting::{
|
||||
build_reset, build_resetref, decrement_refcount_layout, increment_refcount_layout,
|
||||
PointerToRefcount,
|
||||
build_reset, decrement_refcount_layout, increment_refcount_layout, PointerToRefcount,
|
||||
};
|
||||
use bumpalo::collections::Vec;
|
||||
use bumpalo::Bump;
|
||||
|
@ -1247,8 +1246,7 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
|
|||
};
|
||||
|
||||
let ctx = env.context;
|
||||
let then_block = ctx.append_basic_block(parent, "then_resetref");
|
||||
let else_block = ctx.append_basic_block(parent, "else_decref");
|
||||
let not_unique_block = ctx.append_basic_block(parent, "else_decref");
|
||||
let cont_block = ctx.append_basic_block(parent, "cont");
|
||||
|
||||
let refcount_ptr =
|
||||
|
@ -1259,29 +1257,15 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
|
|||
UpdateMode::Immutable => refcount_ptr.is_1(env),
|
||||
};
|
||||
|
||||
let parent_block = env.builder.get_insert_block().unwrap();
|
||||
|
||||
env.builder
|
||||
.build_conditional_branch(is_unique, then_block, else_block);
|
||||
.build_conditional_branch(is_unique, cont_block, not_unique_block);
|
||||
|
||||
{
|
||||
// reset, when used on a unique reference, eagerly decrements the components of the
|
||||
// referenced value, and returns the location of the now-invalid cell
|
||||
env.builder.position_at_end(then_block);
|
||||
|
||||
let reset_function = build_resetref(env, layout_interner, layout_ids, union_layout);
|
||||
let call =
|
||||
env.builder
|
||||
.build_call(reset_function, &[tag_ptr.into()], "call_resetref");
|
||||
|
||||
call.set_call_convention(FAST_CALL_CONV);
|
||||
|
||||
let _ = call.try_as_basic_value();
|
||||
|
||||
env.builder.build_unconditional_branch(cont_block);
|
||||
}
|
||||
{
|
||||
// If reset is used on a shared, non-reusable reference, it behaves
|
||||
// like dec and returns NULL, which instructs reuse to behave like ctor
|
||||
env.builder.position_at_end(else_block);
|
||||
env.builder.position_at_end(not_unique_block);
|
||||
refcount_ptr.decrement(env, layout_interner, layout);
|
||||
env.builder.build_unconditional_branch(cont_block);
|
||||
}
|
||||
|
@ -1290,7 +1274,7 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
|
|||
let phi = env.builder.build_phi(tag_ptr.get_type(), "branch");
|
||||
|
||||
let null_ptr = tag_ptr.get_type().const_null();
|
||||
phi.add_incoming(&[(&tag_ptr, then_block), (&null_ptr, else_block)]);
|
||||
phi.add_incoming(&[(&tag_ptr, parent_block), (&null_ptr, not_unique_block)]);
|
||||
|
||||
phi.as_basic_value()
|
||||
}
|
||||
|
|
|
@ -1543,61 +1543,6 @@ pub fn build_reset<'a, 'ctx, 'env>(
|
|||
function
|
||||
}
|
||||
|
||||
pub fn build_resetref<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
layout_interner: &mut STLayoutInterner<'a>,
|
||||
layout_ids: &mut LayoutIds<'a>,
|
||||
union_layout: UnionLayout<'a>,
|
||||
) -> FunctionValue<'ctx> {
|
||||
// TODO update to not decref the children.
|
||||
todo!("update to not decref the children.");
|
||||
let mode = Mode::Dec;
|
||||
|
||||
let union_layout_in = layout_interner.insert(Layout::Union(union_layout));
|
||||
let layout_id = layout_ids.get(Symbol::DEC, &union_layout_in);
|
||||
let fn_name = layout_id.to_symbol_string(Symbol::DEC, &env.interns);
|
||||
let fn_name = format!("{}_resetref", 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 function = match env.module.get_function(fn_name.as_str()) {
|
||||
Some(function_value) => function_value,
|
||||
None => {
|
||||
let block = env.builder.get_insert_block().expect("to be in a function");
|
||||
let di_location = env.builder.get_current_debug_location().unwrap();
|
||||
|
||||
let basic_type = basic_type_from_layout(env, layout_interner, union_layout_in);
|
||||
let function_value = build_header(env, basic_type, mode, &fn_name);
|
||||
|
||||
build_reuse_rec_union_help(
|
||||
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);
|
||||
|
||||
function_value
|
||||
}
|
||||
};
|
||||
|
||||
function
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn build_reuse_rec_union_help<'a, 'ctx, 'env>(
|
||||
env: &Env<'a, 'ctx, 'env>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue