summaryrefslogtreecommitdiff
path: root/src/libzisp/value/ptr.zig
blob: 2ed376560edaf402ca15b3332ec2a88f4769e57a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const std = @import("std");

const gc = @import("../gc.zig");
const value = @import("../value.zig");

const Hval = gc.Hval;

const Value = value.Value;

// Zig API

pub fn check(v: Value) bool {
    return v.isPtr();
}

pub fn assert(v: Value) void {
    if (!check(v)) {
        v.dump();
        @panic("not a pointer");
    }
}

// Foreign Pointers

pub fn checkForeign(v: Value) bool {
    return check(v) and v.ptr.is_foreign;
}

pub fn assertForeign(v: Value) void {
    if (!checkForeign(v)) {
        v.dump();
        @panic("not foreign pointer");
    }
}

pub fn packForeign(int: u50) Value {
    return .{ .fptr = .{ .value = int } };
}

pub fn unpackForeign(v: Value) u50 {
    assertForeign(v);
    return v.fptr.value;
}

// Zisp Pointers

pub fn checkZisp(v: Value) bool {
    return check(v) and !v.ptr.is_foreign;
}

pub fn assertZisp(v: Value) void {
    if (!checkZisp(v)) {
        v.dump();
        @panic("not zisp pointer");
    }
}

pub fn checkWeak(v: Value) bool {
    return checkZisp(v) and v.zptr.is_weak;
}

pub fn assertWeak(v: Value) void {
    if (!checkWeak(v)) {
        v.dump();
        @panic("not zisp weak pointer");
    }
}

pub fn checkZispTag(v: Value, tag: Tag) bool {
    return checkZisp(v) and unpack(v).@"1" == tag;
}

pub fn assertZispTag(v: Value, tag: Tag) void {
    if (!checkZispTag(v, tag)) {
        v.dump();
        @panic("not zisp pointer or wrong tag");
    }
}

pub fn checkStrong(v: Value) bool {
    return checkZisp(v) and !v.zptr.is_weak;
}

pub fn assertStrong(v: Value) void {
    if (!checkStrong(v)) {
        v.dump();
        @panic("not zisp strong pointer");
    }
}

pub fn packZisp(ptr: *Hval, tag: Tag, is_weak: bool) Value {
    return .{ .zptr = .{
        .tagged_value = tagPtr(ptr, tag),
        .is_weak = is_weak,
    } };
}

pub fn pack(ptr: *Hval, tag: Tag) Value {
    return packZisp(ptr, tag, false);
}

pub fn packWeak(ptr: *Hval, tag: Tag) Value {
    return packZisp(ptr, tag, true);
}

// Unpacks weak as well; no need for a separate fn.
pub fn unpack(v: Value) struct { *Hval, Tag } {
    assertZisp(v);
    return untagPtr(v.zptr.tagged_value);
}

pub fn setWeakNull(v: *Value) void {
    assertWeak(v.*);
    v.zptr.tagged_value = 0;
}

pub fn isWeakNull(v: Value) bool {
    assertWeak(v);
    return v.zptr.tagged_value == 0;
}

fn tagPtr(ptr: *Hval, tag: Tag) u48 {
    const int: usize = @intFromPtr(ptr);
    const untagged: u48 = @intCast(int);
    return untagged | @intFromEnum(tag);
}

fn untagPtr(tagged: u48) struct { *Hval, Tag } {
    const untagged: u48 = tagged & 0xfffffffffff8;
    const ptr: *Hval = @ptrFromInt(untagged);
    const int: u3 = @truncate(tagged);
    const tag: Tag = @enumFromInt(int);
    return .{ ptr, tag };
}

pub const Tag = enum(u3) {
    /// Pair aka cons cell aka *[2]Value
    pair,
    /// Sequence of various kinds (16-bit meta, 48-bit length, then data)
    seq,
    /// Procedure
    proc,
};

// Zisp API

pub fn predForeign(v: Value) Value {
    return value.boole.pack(checkForeign(v));
}

pub fn makeWeak(v: Value) Value {
    assertStrong(v);
    var copy = v;
    copy.zptr.is_weak = true;
    return copy;
}

pub fn predWeak(v: Value) Value {
    return value.boole.pack(checkWeak(v));
}

pub fn predWeakNull(v: Value) Value {
    return value.boole.pack(isWeakNull(v));
}

pub fn getWeak(v: Value) Value {
    if (isWeakNull(v)) {
        return value.boole.f;
    } else {
        var copy = v;
        copy.zptr.is_weak = false;
        return copy;
    }
}