diff --git a/src/compiler.pr b/src/compiler.pr index 11bc686..4b3de14 100644 --- a/src/compiler.pr +++ b/src/compiler.pr @@ -1428,6 +1428,17 @@ def convert_ref_to_ref(tpe: &typechecking::Type, value: Value, loc: &Value, stat // Convert weak ref to strong reference // Need to check the ref count and return a null reference if its 0 if is_weak_ref(value.tpe) and is_ref(tpe) { + let ref_value = state.ptr_to_int(extract1_ret, loc) + let isnull = state.icmp(CompareInt::eq, ref_value, + [ kind = ValueKind::INT, tpe = builtins::int64_, i = 0 ] !Value, loc) + + let br1 = make_insn_dbg(InsnKind::BR, loc) + br1.value.br = [ cond = isnull ] !InsnBr + push_insn(br1, state) + let nonnull = make_label(state) + push_label(nonnull, state) + br1.value.br.if_false = nonnull + let meta = state.load(ref_meta, extract1_ret, loc) let refcount = state.extract_value(builtins::int64_, meta, [0], loc) let iszero = state.icmp(CompareInt::sle, refcount, @@ -1439,6 +1450,7 @@ def convert_ref_to_ref(tpe: &typechecking::Type, value: Value, loc: &Value, stat push_insn(br, state) let if_true = make_label(state) push_label(if_true, state) + br1.value.br.if_true = if_true br.value.br.if_true = if_true // Null reference