2 Copyright (C) 2017 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_FIX32 = 0x00010000,
35 TYPEPRIM_FIX64 = 0x00020000,
36 TYPEPRIM_LIST = 0x00030000,
37 TYPEPRIM_VECTOR = 0x00040000,
38 TYPEPRIM_SUBR = 0x00050000,
41 TYPEPRIM_NOPOOL_MASK = 0x70000000,
42 TYPEPRIM_TUPLE = 0x70010000,
44 // TYPEPRIM is half of EVALTYPE
45 TYPEPRIM_MASK = 0x7fff0000
50 EVALTYPE_FIX32 = TYPEPRIM_FIX32,
52 EVALTYPE_FIX64 = TYPEPRIM_FIX64,
54 EVALTYPE_LIST = TYPEPRIM_LIST,
57 EVALTYPE_VECTOR = TYPEPRIM_VECTOR,
59 EVALTYPE_SUBR = TYPEPRIM_SUBR,
61 EVALTYPE_TUPLE = TYPEPRIM_TUPLE,
64 static inline uint32_t
67 return x & TYPEPRIM_MASK;
71 TYPEPRIM_EQ (evaltype a, evaltype b)
73 return !((a ^ b) & TYPEPRIM_MASK);
88 An Object's value is accessed through a concrete `foo_object`
91 `object` can be used to refer to Objects of unspecified type, which
92 are opaque except for their `type` field. Checked downcasts can be
93 performed via the `as_foo` functions; unchecked downcasts via
94 `object.foo` (use only when type information is locally
95 obvious). Some objects can be upcast to more specific supertypes,
96 such as `pool_object` for objects that are known to be storeable in
99 The generic `object` type should not be used to accept parameters
100 that have constraints on their type, and should not be used to
101 return objects that are of a statically-known type. Encoding type
102 information in function signatures allows strictly local reasoning
106 typedef union object object;
110 alignas (16) evaltype type;
118 alignas (16) evaltype type;
125 alignas (16) evaltype type;
133 alignas (16) evaltype type;
141 alignas (16) evaltype type;
149 /// no rest; is a NOPOOL type
152 /// allocation can be anywhere
157 /// Object of a type that can be stored in the pool.
158 /// NB. a pool_object* can point outside the pool; contrast with pool_ptr.
159 typedef union pool_object
161 /// any pool object has a type and a rest
164 alignas (16) evaltype type;
168 /// objects of statically known type
172 vector_object vector;
177 /// any object has a type
180 alignas (16) evaltype type;
184 /// objects of statically known type
185 /// use as_X() for checked downcast
190 vector_object vector;
195 Initialization helpers.
198 static inline fix64_object
199 new_fix64 (uint64_t n)
201 return (fix64_object)
203 .type = EVALTYPE_FIX64,.rest = 0,.val = n,};
206 static inline list_object
207 new_list (pool_ptr head)
211 .type = EVALTYPE_LIST,.rest = 0,.head = head,};
214 static inline vector_object
215 new_vector (heap_ptr body, uint32_t length)
217 return (vector_object)
219 .type = EVALTYPE_VECTOR,.rest = 0,.len = length,.body = body,};
222 static inline tuple_object
223 new_tuple (object * body, uint32_t length)
225 return (tuple_object)
227 .type = EVALTYPE_TUPLE,.len = length,.body = body,};
230 static inline subr_object
231 new_subr (void (*fn) ())
235 .type = EVALTYPE_SUBR,.rest = 0,.fn = fn,};
239 Common object operations.
242 uint32_t list_length (const list_object * o);
248 static inline list_object *
251 assert (TYPEPRIM_EQ (o->type, EVALTYPE_LIST));
255 static inline vector_object *
256 as_vector (object * o)
258 assert (TYPEPRIM_EQ (o->type, EVALTYPE_VECTOR));
262 static inline pool_object *
265 if (TYPEPRIM (p->type) & TYPEPRIM_NOPOOL_MASK)
267 return (pool_object *) p;