2 Copyright (C) 2017-2018 Keziah Wesley
4 You can redistribute and/or modify this file under the terms of the
5 GNU Affero General Public License as published by the Free Software
6 Foundation, either version 3 of the License, or (at your option) any
9 This file is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Affero General Public License for more details.
14 You should have received a copy of the GNU Affero General Public
15 License along with this file. If not, see
16 <http://www.gnu.org/licenses/>.
29 typedef uint32_t evaltype;
34 TYPEPRIM_LOSE = 0x00000000,
35 TYPEPRIM_FIX32 = 0x00010000,
36 TYPEPRIM_FIX64 = 0x00020000,
37 TYPEPRIM_LIST = 0x00030000,
38 TYPEPRIM_VECTOR = 0x00040000,
39 TYPEPRIM_UVECTOR = 0x00050000,
40 TYPEPRIM_SUBR = 0x00060000,
41 TYPEPRIM_ATOM = 0x00070000,
44 TYPEPRIM_NOPOOL_MASK = 0x70000000,
45 TYPEPRIM_VECTOR_BODY = 0x70000000,
46 TYPEPRIM_TUPLE = 0x70010000,
48 // TYPEPRIM is half of EVALTYPE
49 TYPEPRIM_MASK = 0x7fff0000
54 EVALTYPE_LOSE = TYPEPRIM_LOSE,
56 EVALTYPE_FIX32 = TYPEPRIM_FIX32,
58 EVALTYPE_FIX64 = TYPEPRIM_FIX64,
60 EVALTYPE_LIST = TYPEPRIM_LIST,
64 EVALTYPE_VECTOR = TYPEPRIM_VECTOR,
66 EVALTYPE_UVECTOR = TYPEPRIM_UVECTOR,
69 EVALTYPE_SUBR = TYPEPRIM_SUBR,
71 EVALTYPE_ATOM = TYPEPRIM_ATOM,
73 EVALTYPE_VECTOR_BODY = TYPEPRIM_VECTOR_BODY,
76 EVALTYPE_TUPLE = TYPEPRIM_TUPLE,
79 static inline uint32_t
82 return x & TYPEPRIM_MASK;
86 TYPEPRIM_EQ (evaltype a, evaltype b)
88 return !((a ^ b) & TYPEPRIM_MASK);
103 An Object's value is accessed through a concrete `foo_object`
106 `object` can be used to refer to Objects of unspecified type, which
107 are opaque except for their `type` field. Checked downcasts can be
108 performed via the `as_foo` functions; unchecked downcasts via
109 `object.foo` (use only when type information is locally
110 obvious). Some objects can be upcast to more specific supertypes,
111 such as `pool_object` for objects that are known to be storeable in
114 The generic `object` type should not be used to accept parameters
115 that have constraints on their type, and should not be used to
116 return objects that are of a statically-known type. Encoding type
117 information in function signatures allows strictly local reasoning
121 typedef union object object;
125 alignas (8) uint32_t _pad;
130 alignas (16) evaltype type;
137 alignas (8) int64_t n;
141 alignas (16) evaltype type;
148 alignas (8) uint32_t _pad;
153 alignas (16) evaltype type;
160 alignas (8) uint32_t len;
165 alignas (16) evaltype type;
172 alignas (8) uint32_t len;
177 alignas (16) evaltype type;
184 alignas (8) void (*fn) ();
188 alignas (16) evaltype type;
195 alignas (8) uint32_t _pad;
200 alignas (16) evaltype type;
208 /// no rest; is a NOPOOL type
211 /// allocation can be anywhere
218 alignas (16) evaltype type;
224 /// Object of a type that can be stored in the pool.
225 /// NB. a pool_object* can point outside the pool; contrast with pool_ptr.
226 typedef union pool_object
228 /// any pool object has a type and a rest
231 // NB. never take the address of these type-punned fields!
232 alignas (16) evaltype type;
236 /// objects of statically known type
240 vector_object vector;
241 uvector_object uvector;
245 /// Value half of a poolable object, for storage in a uvector.
259 /// any object has a type
262 // NB. never take the address of these type-punned fields!
263 alignas (16) evaltype type;
267 /// objects of statically known type
268 /// use as_X() for checked downcast
273 vector_object vector;
274 uvector_object uvector;
280 Initialization helpers.
283 static inline fix32_object
284 new_fix32 (int32_t n)
286 return (fix32_object)
288 .type = EVALTYPE_FIX32,.rest = 0,.val = (fix32_val)
294 static inline fix64_object
295 new_fix64 (int64_t n)
297 return (fix64_object)
299 .type = EVALTYPE_FIX64,.rest = 0,.val = (fix64_val)
305 static inline list_object
306 new_list (pool_ptr head)
310 .type = EVALTYPE_LIST,.rest = 0,.val = (list_val)
316 static inline vector_object
317 new_vector (heap_ptr body, uint32_t length)
319 return (vector_object)
321 .type = EVALTYPE_VECTOR,.rest = 0,.val = (vector_val)
323 .len = length,.body = body}
327 static inline uvector_object
328 new_uvector (heap_ptr body, uint32_t length)
330 return (uvector_object)
332 .type = EVALTYPE_VECTOR,.rest = 0,.val = (uvector_val)
334 .len = length,.body = body}
338 static inline tuple_object
339 new_tuple (object * body, uint32_t length)
341 return (tuple_object)
343 .type = EVALTYPE_TUPLE,.len = length,.body = body};
346 static inline subr_object
347 new_subr (void (*fn) ())
351 .type = EVALTYPE_SUBR,.rest = 0,.val = (subr_val)
357 static inline atom_object
358 new_atom (pool_ptr body)
362 .type = EVALTYPE_ATOM,.rest = 0,.val = (atom_val)
369 Common object operations.
372 uint32_t list_length (const list_object * o);
378 static inline list_object *
381 assert (TYPEPRIM_EQ (o->type, EVALTYPE_LIST));
385 static inline vector_object *
386 as_vector (object * o)
388 assert (TYPEPRIM_EQ (o->type, EVALTYPE_VECTOR));
392 static inline uvector_object *
393 as_uvector (object * o)
395 assert (TYPEPRIM_EQ (o->type, EVALTYPE_UVECTOR));
399 static inline pool_object *
402 assert (!(TYPEPRIM (p->type) & TYPEPRIM_NOPOOL_MASK));
403 return (pool_object *) p;