generate elem refcount function and pass it into decref lowlevel

This commit is contained in:
Brendan Hansknecht 2024-07-10 20:45:15 -07:00
parent 516afaff41
commit ba9b15f7d6
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
3 changed files with 38 additions and 11 deletions

View file

@ -2102,6 +2102,17 @@ impl<'a, 'r> WasmBackend<'a, 'r> {
self.register_helper_proc(spec_sym, spec_layout, ProcSource::Helper);
}
self.get_existing_refcount_fn_index(proc_symbol, layout, op)
}
/// return a pointer (table index) to a refcount helper procedure.
/// This allows it to be indirectly called from Zig code
pub fn get_existing_refcount_fn_index(
&mut self,
proc_symbol: Symbol,
layout: InLayout<'a>,
op: HelperOp,
) -> u32 {
let layout_repr = if op.is_indirect() {
LayoutRepr::Ptr(layout)
} else {

View file

@ -341,6 +341,8 @@ impl<'a> LowLevelCall<'a> {
ListDecref => {
let input_list: Symbol = self.arguments[0];
let dec_fn_sym = self.arguments[1];
let list_layout = backend
.layout_interner
.get_repr(backend.storage.symbol_layouts[&input_list]);
@ -350,8 +352,12 @@ impl<'a> LowLevelCall<'a> {
elem_layout.stack_size_and_alignment(backend.layout_interner);
let elem_refcounted = backend.layout_interner.contains_refcounted(elem_in_layout);
let dec_fn_ptr =
build_refcount_element_fn(backend, elem_in_layout, HelperOp::IndirectDec);
let dec_fn = backend.get_existing_refcount_fn_index(
dec_fn_sym,
elem_in_layout,
HelperOp::IndirectDec,
);
let dec_fn_ptr = backend.get_fn_ptr(dec_fn);
// Zig arguments Wasm types
// input_list: &RocList i32
@ -2998,11 +3004,6 @@ fn build_refcount_element_fn<'a>(
elem_layout: InLayout<'a>,
rc_op: HelperOp,
) -> i32 {
// The refcount function receives a pointer to an element in the list
// This is the same as a Struct containing the element
let in_memory_layout = backend
.layout_interner
.insert_direct_no_semantic(LayoutRepr::Struct(backend.env.arena.alloc([elem_layout])));
let rc_fn = backend.get_refcount_fn_index(in_memory_layout, rc_op);
let rc_fn = backend.get_refcount_fn_index(elem_layout, rc_op);
backend.get_fn_ptr(rc_fn)
}