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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
//! See /doc/0/0-value.md to understand the NaN-packing strategy.
const builtin = @import("builtin");
const std = @import("std");
const gc = @import("gc.zig");
pub const double = @import("value/double.zig");
pub const fixnum = @import("value/fixnum.zig");
pub const ptr = @import("value/ptr.zig");
pub const list = @import("value/list.zig");
pub const istr = @import("value/istr.zig");
pub const sstr = @import("value/sstr.zig");
pub const srat = @import("value/srat.zig");
pub const rune = @import("value/rune.zig");
pub const char = @import("value/char.zig");
pub const misc = @import("value/misc.zig");
pub const array = @import("value/array.zig");
pub const boole = @import("value/boole.zig");
pub const string = @import("value/string.zig");
const endian = builtin.target.cpu.arch.endian();
const max = std.math.maxInt;
/// Used to find the length of a rune or short string.
pub fn sstrLen(x: u64) u8 {
const bytes: @Vector(8, u8) = @bitCast(x);
const nulls: @Vector(8, u8) = @splat(0);
const comps: u8 = @bitCast(bytes == nulls);
// Two bits will always be 0, since the actual short string starts at the
// third byte; third lowest or third highest depending on endianness. So,
// depending on endianness, either cut off the two leading bits and ensure
// that the second-last is set, or ensure that the second highest set, to
// limit the length to 6.
return switch (endian) {
.big => @clz(comps << 2 | 2),
.little => @ctz(comps | 64),
};
}
/// Transform back and forth between rune and sstr
pub fn runeXsstr(v: Value) Value {
return @bitCast(v.bits ^ (1 << 50));
}
// Make sure false/true only differ in LSb.
pub const MiscValue = enum(u8) { f, t, nil, eof, none };
// zig fmt: off
pub const f = Value{ .misc = .{ .value = .f } };
pub const t = Value{ .misc = .{ .value = .t } };
pub const nil = Value{ .misc = .{ .value = .nil } };
pub const eof = Value{ .misc = .{ .value = .eof } };
pub const none = Value{ .misc = .{ .value = .none } };
// zig fmt: on
/// A plain (unpacked, untagged) pointer into the Zisp heap.
pub const Zptr = *align(16) anyopaque;
/// Values for the lowest 4 bits of a heap pointer, indicating the heap type.
pub const HeapType = enum(u4) {
/// Unused so the 48-bit payload of a NaN-packed pointer is never zero and
/// cannot be confused for an actual NaN even in case of a null-index.
_unused = 0,
/// Array of various types (see `Array` type for details)
array,
pub fn PtrType(self: HeapType) type {
return switch (self) {
.array => array.ArrayPtr,
else => @panic("not implemented"),
};
}
};
/// Make a "pointer etc." high 16-bit pattern with a 3-bit sub-range tag.
fn ptrEtc(comptime tag: u3) u16 {
return @as(u16, 0x7ff8) + tag;
}
const hi16_tag_ptr = ptrEtc(0b000);
const hi16_tag_list = ptrEtc(0b001);
const hi16_tag_istr = ptrEtc(0b010);
const hi16_tag_sstr = ptrEtc(0b011);
const hi15_tag_srat = @as(u15, @intCast(ptrEtc(0b100) >> 1));
const hi16_tag_rune = ptrEtc(0b111);
/// Represents a Zisp value.
pub const Value = packed union {
/// To get an agnostic value for direct comparison with == i.e. eq? as well
/// as manual bit-fiddling to test for and extract packed values.
bits: u64,
/// Regular double, including +nan, -nan, +inf, -inf.
double: f64,
// Some of the structs below are just for inspection, whereas others are to
// initialize a new value of that category, as well as to read it as such.
// Some of these are not used at all and are merely demonstrative.
// Note: Zig packed struct fields are ordered from LSb to MSb.
/// Inspection through the lens of the general IEEE 754 double layout.
ieee: packed struct {
rest: u51,
quiet: bool,
exp: u11,
sign: bool,
},
/// 52-bit signed fixnum
fixnum: packed struct {
code: u51,
negative: bool,
_exp: u11 = max(u11),
_is_fixnum: bool = true,
},
/// Ordinary heap pointer
ptr: packed struct {
index: u44,
htype: HeapType,
_hi16_tag: u16 = hi16_tag_ptr,
},
/// List pointer
list: packed struct {
len_tagged_ptr: u48,
_hi16_tag: u16 = hi16_tag_list,
},
/// Istr pointer
istr: packed struct {
ptr: u48,
_hi16_tag: u16 = hi16_tag_istr,
},
/// Short string
sstr: packed struct {
// actually [6]u8 but packed struct cannot contain arrays
bytes: u48,
_hi16_tag: u16 = hi16_tag_sstr,
},
/// Small rat (rational number)
srat: packed struct {
q: u24,
p: i25,
_hi16_tag: u15 = hi15_tag_srat,
},
/// Rune (6-byte ASCII string)
rune: packed struct {
// actually [6]u8 but packed struct cannot contain arrays
name: u48,
_hi16_tag: u16 = hi16_tag_rune,
},
// TODO: Use a general Small Value type registration mechanism.
char: packed struct {
value: u24,
_sv_tag: u24 = 0x000080,
_hi16_tag: u16 = hi16_tag_rune,
},
// TODO: Use a general Small Value type registration mechanism.
misc: packed struct {
value: MiscValue,
_sv_tag: u40 = 0x0000000080,
_hi16_tag: u16 = hi16_tag_rune,
},
/// Dumps the value for inspection.
pub fn dump(v: Value) void {
std.debug.print(
\\value: 0x{x}
\\sign: {}, exp: {b}, quiet: {}
\\rest: 0x{x}
\\
, .{ v.bits, v.ieee.sign, v.ieee.exp, v.ieee.quiet, v.ieee.rest });
}
/// To treat the 64-bit value as an 8-byte const array.
pub fn bufRO(v: *const Value) *const [8]u8 {
return @ptrCast(v);
}
/// To treat the 64-bit value as an 8-byte mutable array.
pub fn bufRW(v: *Value) *[8]u8 {
return @ptrCast(v);
}
/// Checks for bit-equality i.e. == comprison.
pub fn eq(v1: Value, v2: Value) bool {
return v1.bits == v2.bits;
}
// It would be great if we could just write the most readable code in the
// following functions, using the packed struct definitions above, but the
// optimizer isn't smart enough, so manual bit fiddling it is.
/// Checks for a double, including: +nan, -nan, +inf, -inf.
pub fn isDouble(v: Value) bool {
// Readable version:
//
// return v.ieee.exp != max(u11) or v.ieee.rest == 0;
//
// Optimized:
//
// 1. Shift the 12 highest bits all the way to the right, then mask out
// the sign bit. Allows efficiently checking if the exponent is all
// set by comparing to 0x7ff.
//
// 2. Shift out 13 high bits (sign, expt, quiet) so we can easily check
// if the rest bits are zero.
//
const expt = v.bits >> 52 & 0x7ff;
const rest = v.bits << 13;
return expt != 0x7ff or rest == 0;
}
// Imagine there's an isPacked() implemented as !isDouble() here, used to
// make the following functions more readable.
/// Checks for a fixnum integer.
pub fn isFixnum(v: Value) bool {
// Readable version:
//
// return v.isPacked() and v.ieee.sign;
//
// Optimized:
//
// 1. Shift the 12 highest bits all the way to the right; these need to
// be all set, since the sign bit (and all exponent bits) being set
// marks a fixnum.
//
// 2. But also verify that the lowest 51 bits aren't zero, since that
// would be a cqNaN or Infinity.
//
const expt = v.bits >> 52;
const rest = v.bits << 13;
return expt == 0xfff and rest != 0;
}
/// Checks if the value is any type-tagged pointer. You probably want to
/// use getPtr() or getPtrAny() instead, so you can combine the check with
/// the extraction of the pointer and type tag.
pub fn isPtrAny(v: Value) bool {
const hi: u16 = @intCast(v.bits >> 48);
const lo: u48 = @truncate(v.bits);
return hi == hi16_tag_ptr and lo != 0;
}
/// Checks for a pointer with a given type tag, returning null on failure
/// and the pointer value otherwise.
pub fn getPtr(v: Value, comptime ht: HeapType) ?ht.PtrType() {
// Check if the 20 highest bits equal the combination of the 16-bit
// pattern marking a pointer, followed by the 4-bit heap type tag.
const hi20_bits: u20 = @intCast(v.bits >> 44);
const ptr_bits: u20 = hi16_tag_ptr;
const tag_bits: u20 = @intFromEnum(ht);
if (hi20_bits != ptr_bits << 4 | tag_bits) return null;
return @ptrFromInt(v.bits << 20 >> 16);
}
/// Checks for a pointer and returns the value and tag separately, or null
/// if this isn't a pointer at all. Could be useful for a dispatch table.
/// Note that unlike getPtr(), this can actually return a null pointer as
/// such, not conflating it with the "not a heap pointer" case.
pub fn getPtrAny(v: Value) ?struct { HeapType, ?Zptr } {
const hi16_bits: u16 = @intCast(v.bits >> 48);
if (hi16_bits != hi16_tag_ptr) return null;
const ht: u4 = @intCast(v.bits << 16 >> 60);
const pval: u48 = @intCast(v.bits << 20 >> 16);
return .{ @enumFromInt(ht), @ptrFromInt(pval) };
}
/// Checks if the value is a list pointer.
pub fn isList(v: Value) bool {
return v.bits >> 48 == hi16_tag_list;
}
/// Checks if the value is an istr pointer.
pub fn isIstr(v: Value) bool {
return v.bits >> 48 == hi16_tag_istr;
}
/// Checks if the value is a short string.
pub fn isSstr(v: Value) bool {
return v.bits >> 48 == hi16_tag_sstr;
}
/// Checks if the value is a small rat (rational number).
pub fn isSrat(v: Value) bool {
return v.bits >> 49 == hi15_tag_srat;
}
/// Checks for a rune.
pub fn isRune(v: Value) bool {
// This check isn't used much, since one typically checks for specific
// runes via direct equality, so the efficiency here is unimportant.
const hi = v.bits >> 48;
const lo = v.bits << 16;
const msb_mask = 0x7f7f7f7f7f7f0000;
return hi == 0x7fff and lo != 0 and lo & msb_mask == lo;
}
// TODO: Use a general Small Value type registration mechanism.
pub fn isChar(v: Value) bool {
return v.bits >> 24 == 0x7fff000080;
}
// TODO: Use a general Small Value type registration mechanism.
pub fn isMisc(v: Value) bool {
return v.bits >> 8 == 0x7fff0000000080;
}
};
|