Skip to content

Commit

Permalink
Another null check
Browse files Browse the repository at this point in the history
  • Loading branch information
Victorious3 committed May 1, 2024
1 parent f1e7337 commit 192e13a
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/compiler.pr
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,18 @@ def convert_ref_to_ref(tpe: &typechecking::Type, value: Value, loc: &Value, stat
def convert_ref_to_ptr(tpe: &typechecking::Type, value: Value, loc: &Value, state: &State) -> Value {
if is_weak_ref(value.tpe) {
let meta = state.extract_value(pointer(ref_meta), value, [0], loc)

let ref_value = state.ptr_to_int(meta, 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_val = state.load(ref_meta, meta, loc)
let cnt = state.extract_value(builtins::int64_, meta_val, [0], loc)
let iszero = state.icmp(CompareInt::sle, cnt, [ kind = ValueKind::INT, tpe = builtins::int64_, i = 0 ] !Value)
Expand All @@ -1500,6 +1512,7 @@ def convert_ref_to_ptr(tpe: &typechecking::Type, value: Value, loc: &Value, stat
let if_true = make_label(state)
push_label(if_true, state)
br.value.br.if_true = if_true
br1.value.br.if_true = if_true

// Null pointer
state.store(res, [ kind = ValueKind::ZEROINITIALIZER, tpe = pointer(tpe.tpe) ] !Value)
Expand Down

0 comments on commit 192e13a

Please sign in to comment.