Implement OBLISTs
[muddle-interpreter.git] / src / object.h
1 /*
2 Copyright (C) 2017-2018 Keziah Wesley
3
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
7 later version.
8
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.
13
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/>.
17 */
18
19 #ifndef OBJECT_H
20 #define OBJECT_H
21
22 #include "alloc.h"
23
24 #include <assert.h>
25 #include <stdalign.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28
29 typedef uint32_t evaltype;
30
31 enum
32 {
33 // pool OK
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,
42
43 // can't be in pool
44   TYPEPRIM_NOPOOL_MASK = 0x70000000,
45   TYPEPRIM_VECTOR_BODY = 0x70000000,
46   TYPEPRIM_TUPLE = 0x70010000,
47
48 // TYPEPRIM is half of EVALTYPE
49   TYPEPRIM_MASK = 0x7fff0000
50 };
51
52 enum
53 {
54   EVALTYPE_LOSE = TYPEPRIM_LOSE,
55
56   EVALTYPE_FIX32 = TYPEPRIM_FIX32,
57
58   EVALTYPE_FIX64 = TYPEPRIM_FIX64,
59
60   EVALTYPE_LIST = TYPEPRIM_LIST,
61   EVALTYPE_FORM,
62   EVALTYPE_FALSE,
63
64   EVALTYPE_VECTOR = TYPEPRIM_VECTOR,
65
66   EVALTYPE_UVECTOR = TYPEPRIM_UVECTOR,
67   EVALTYPE_OBLIST,
68
69   EVALTYPE_SUBR = TYPEPRIM_SUBR,
70
71   EVALTYPE_ATOM = TYPEPRIM_ATOM,
72
73   EVALTYPE_VECTOR_BODY = TYPEPRIM_VECTOR_BODY,
74   EVALTYPE_ATOM_BODY,
75
76   EVALTYPE_TUPLE = TYPEPRIM_TUPLE,
77 };
78
79 static inline uint32_t
80 TYPEPRIM (evaltype x)
81 {
82   return x & TYPEPRIM_MASK;
83 }
84
85 static inline bool
86 TYPEPRIM_EQ (evaltype a, evaltype b)
87 {
88   return !((a ^ b) & TYPEPRIM_MASK);
89 }
90
91 typedef struct
92 {
93   uint32_t _dummy;
94 } opaque32;
95 typedef struct
96 {
97   uint64_t _dummy;
98 } opaque64;
99
100 /**
101 Object types.
102
103 An Object's value is accessed through a concrete `foo_object`
104 type.
105
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
112 the pool.
113
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
118 about types.
119 */
120
121 typedef union object object;
122
123 typedef struct
124 {
125   alignas (8) uint32_t _pad;
126   int32_t n;
127 } fix32_val;
128 typedef struct
129 {
130   alignas (16) evaltype type;
131   pool_ptr rest;
132   fix32_val val;
133 } fix32_object;
134
135 typedef struct
136 {
137   alignas (8) int64_t n;
138 } fix64_val;
139 typedef struct
140 {
141   alignas (16) evaltype type;
142   pool_ptr rest;
143   fix64_val val;
144 } fix64_object;
145
146 typedef struct
147 {
148   alignas (8) uint32_t _pad;
149   pool_ptr head;
150 } list_val;
151 typedef struct
152 {
153   alignas (16) evaltype type;
154   pool_ptr rest;
155   list_val val;
156 } list_object;
157
158 typedef struct
159 {
160   alignas (8) uint32_t len;
161   heap_ptr body;
162 } vector_val;
163 typedef struct
164 {
165   alignas (16) evaltype type;
166   pool_ptr rest;
167   vector_val val;
168 } vector_object;
169
170 typedef struct
171 {
172   alignas (8) uint32_t len;
173   heap_ptr body;
174 } uvector_val;
175 typedef struct
176 {
177   alignas (16) evaltype type;
178   pool_ptr rest;
179   uvector_val val;
180 } uvector_object;
181
182 typedef struct
183 {
184   alignas (8) void (*fn) ();
185 } subr_val;
186 typedef struct
187 {
188   alignas (16) evaltype type;
189   pool_ptr rest;
190   subr_val val;
191 } subr_object;
192
193 typedef struct
194 {
195   alignas (8) uint32_t namelen;
196   heap_ptr body;
197 } atom_val;
198 typedef struct
199 {
200   alignas (16) evaltype type;
201   pool_ptr rest;
202   atom_val val;
203 } atom_object;
204
205 typedef struct
206 {
207   alignas (16)
208     /// no rest; is a NOPOOL type
209   evaltype type;
210   uint32_t len;
211   /// allocation can be anywhere
212   object *body;
213   // uniq_id uid ??
214 } tuple_object;
215
216 typedef struct
217 {
218   alignas (16) evaltype type;
219   uint32_t grow;
220   uint32_t len;
221   uint32_t gc;
222 } dope_object;
223
224 /// Value half of a poolable object, for storage in a uvector.
225 typedef union uv_val
226 {
227   fix32_val fix32;
228   fix64_val fix64;
229   list_val list;
230   vector_val vector;
231   uvector_val uvector;
232   subr_val subr;
233   atom_val atom;
234 } uv_val;
235
236 /// Object of a type that can be stored in the pool.
237 /// NB. a pool_object* can point outside the pool; contrast with pool_ptr.
238 typedef union pool_object
239 {
240   /// any pool object has a type and a rest
241   struct
242   {
243     // NB. never take the address of these type-punned fields!
244     alignas (16) evaltype type;
245     pool_ptr rest;
246     uv_val val;
247   };
248   /// objects of statically known type
249   fix32_object fix32;
250   fix64_object fix64;
251   list_object list;
252   vector_object vector;
253   uvector_object uvector;
254   atom_object atom;
255 } pool_object;
256
257 union object
258 {
259   /// any object has a type
260   struct
261   {
262     // NB. never take the address of these type-punned fields!
263     alignas (16) evaltype type;
264     opaque32 _unknown0;
265     opaque64 _unknown1;
266   };
267   /// objects of statically known type
268   /// use as_X() for checked downcast
269   pool_object pool;
270   fix32_object fix32;
271   fix64_object fix64;
272   list_object list;
273   vector_object vector;
274   uvector_object uvector;
275   atom_object atom;
276   tuple_object tuple;
277 };
278
279 /**
280 Initialization helpers.
281 */
282
283 static inline fix32_object
284 new_fix32 (int32_t n)
285 {
286   return (fix32_object)
287   {
288     .type = EVALTYPE_FIX32,.rest = 0,.val = (fix32_val)
289     {
290     .n = n}
291   };
292 }
293
294 static inline fix64_object
295 new_fix64 (int64_t n)
296 {
297   return (fix64_object)
298   {
299     .type = EVALTYPE_FIX64,.rest = 0,.val = (fix64_val)
300     {
301     .n = n}
302   };
303 }
304
305 static inline list_object
306 new_list (pool_ptr head)
307 {
308   return (list_object)
309   {
310     .type = EVALTYPE_LIST,.rest = 0,.val = (list_val)
311     {
312     .head = head}
313   ,};
314 }
315
316 // TODO: take a dope_object like uvector
317 static inline vector_object
318 new_vector (heap_ptr body, uint32_t length)
319 {
320   return (vector_object)
321   {
322     .type = EVALTYPE_VECTOR,.rest = 0,.val = (vector_val)
323     {
324     .len = length,.body = body}
325   ,};
326 }
327
328 static inline uvector_object
329 new_uvector (heap_ptr body, uint32_t length)
330 {
331   return (uvector_object)
332   {
333     .type = EVALTYPE_UVECTOR,.rest = 0,.val = (uvector_val)
334     {
335     .len = length,.body = body}
336   };
337 }
338
339 static inline tuple_object
340 new_tuple (object * body, uint32_t length)
341 {
342   return (tuple_object)
343   {
344   .type = EVALTYPE_TUPLE,.len = length,.body = body};
345 }
346
347 static inline subr_object
348 new_subr (void (*fn) ())
349 {
350   return (subr_object)
351   {
352     .type = EVALTYPE_SUBR,.rest = 0,.val = (subr_val)
353     {
354     .fn = fn}
355   };
356 }
357
358 static inline atom_object
359 new_atom (pool_ptr body, uint32_t namelen)
360 {
361   return (atom_object)
362   {
363     .type = EVALTYPE_ATOM,.rest = 0,.val = (atom_val)
364     {
365     .body = body,.namelen = namelen}
366   };
367 }
368
369 static inline dope_object
370 new_dope (uint32_t len, evaltype type)
371 {
372   return (dope_object)
373   {
374   .type = type,.grow = 0,.len = len,.gc = 0};
375 }
376
377 /**
378 Common object operations.
379 */
380
381 uint32_t list_length (const list_object * o);
382
383 dope_object *uv_dope (const uvector_object * o);
384
385 static inline evaltype
386 utype (const uvector_object * o)
387 {
388   return uv_dope (o)->type;
389 }
390
391 // Change the EVALTYPE of an object. New type must have same PRIMTYPE.
392 static inline void
393 chtype (object * o, evaltype type)
394 {
395   assert (TYPEPRIM_EQ (o->type, type));
396   o->type = type;
397 }
398
399 /**
400 Checked downcasts.
401 */
402
403 static inline list_object *
404 as_list (object * o)
405 {
406   assert (TYPEPRIM_EQ (o->type, EVALTYPE_LIST));
407   return &o->list;
408 }
409
410 static inline vector_object *
411 as_vector (object * o)
412 {
413   assert (TYPEPRIM_EQ (o->type, EVALTYPE_VECTOR));
414   return &o->vector;
415 }
416
417 static inline uvector_object *
418 as_uvector (object * o)
419 {
420   assert (TYPEPRIM_EQ (o->type, EVALTYPE_UVECTOR));
421   return &o->uvector;
422 }
423
424 static inline pool_object *
425 as_pool (object * p)
426 {
427   assert (!(TYPEPRIM (p->type) & TYPEPRIM_NOPOOL_MASK));
428   return (pool_object *) p;
429 }
430
431 static inline atom_object *
432 as_atom (object * o)
433 {
434   assert (TYPEPRIM_EQ (o->type, EVALTYPE_ATOM));
435   return &o->atom;
436 }
437
438 #endif // OBJECT_H