altos/lisp: Change GC move API
[fw/altos] / src / lisp / ao_lisp.h
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program 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  * General Public License for more details.
13  */
14
15 #ifndef _AO_LISP_H_
16 #define _AO_LISP_H_
17
18 #include <stdlib.h>
19
20 #if !defined(AO_LISP_TEST) && !defined(AO_LISP_MAKE_CONST)
21 #include <ao.h>
22 #define AO_LISP_ALTOS   1
23 #define abort() ao_panic(1)
24 #endif
25
26 #include <stdint.h>
27 #include <string.h>
28 #include <stdio.h>
29
30 #ifdef AO_LISP_MAKE_CONST
31 #define AO_LISP_POOL_CONST      16384
32 extern uint8_t ao_lisp_const[AO_LISP_POOL_CONST];
33 #define ao_lisp_pool ao_lisp_const
34 #define AO_LISP_POOL AO_LISP_POOL_CONST
35
36 #define _atom(n) ao_lisp_atom_poly(ao_lisp_atom_intern(n))
37
38 #define _ao_lisp_atom_quote     _atom("quote")
39 #define _ao_lisp_atom_set       _atom("set")
40 #define _ao_lisp_atom_setq      _atom("setq")
41 #define _ao_lisp_atom_t         _atom("t")
42 #define _ao_lisp_atom_car       _atom("car")
43 #define _ao_lisp_atom_cdr       _atom("cdr")
44 #define _ao_lisp_atom_cons      _atom("cons")
45 #define _ao_lisp_atom_cond      _atom("cond")
46 #else
47 #include "ao_lisp_const.h"
48 #ifndef AO_LISP_POOL
49 #define AO_LISP_POOL    16384
50 #endif
51 extern uint8_t          ao_lisp_pool[AO_LISP_POOL];
52 #endif
53
54 /* Primitive types */
55 #define AO_LISP_CONS            0
56 #define AO_LISP_INT             1
57 #define AO_LISP_STRING          2
58 #define AO_LISP_OTHER           3
59
60 #define AO_LISP_TYPE_MASK       0x0003
61 #define AO_LISP_TYPE_SHIFT      2
62 #define AO_LISP_REF_MASK        0x7ffc
63 #define AO_LISP_CONST           0x8000
64
65 /* These have a type value at the start of the struct */
66 #define AO_LISP_ATOM            4
67 #define AO_LISP_BUILTIN         5
68 #define AO_LISP_FRAME           6
69 #define AO_LISP_NUM_TYPE        7
70
71 #define AO_LISP_NIL     0
72
73 extern uint16_t         ao_lisp_top;
74
75 #define AO_LISP_OOM             0x01
76 #define AO_LISP_DIVIDE_BY_ZERO  0x02
77 #define AO_LISP_INVALID         0x04
78
79 extern uint8_t          ao_lisp_exception;
80
81 typedef uint16_t        ao_poly;
82 typedef int16_t         ao_signed_poly;
83
84 static inline int
85 ao_lisp_is_const(ao_poly poly) {
86         return poly & AO_LISP_CONST;
87 }
88
89 #define AO_LISP_POOL_BASE       (ao_lisp_pool - 4)
90 #define AO_LISP_CONST_BASE      (ao_lisp_const - 4)
91
92 #define AO_LISP_IS_CONST(a)     (ao_lisp_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_const + AO_LISP_POOL_CONST)
93 #define AO_LISP_IS_POOL(a)      (ao_lisp_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_pool + AO_LISP_POOL)
94
95 static inline void *
96 ao_lisp_ref(ao_poly poly) {
97         if (poly == 0xBEEF)
98                 abort();
99         if (poly == AO_LISP_NIL)
100                 return NULL;
101         if (poly & AO_LISP_CONST)
102                 return (void *) (AO_LISP_CONST_BASE + (poly & AO_LISP_REF_MASK));
103         return (void *) (AO_LISP_POOL_BASE + (poly & AO_LISP_REF_MASK));
104 }
105
106 static inline ao_poly
107 ao_lisp_poly(const void *addr, ao_poly type) {
108         const uint8_t   *a = addr;
109         if (a == NULL)
110                 return AO_LISP_NIL;
111         if (AO_LISP_IS_CONST(a))
112                 return AO_LISP_CONST | (a - AO_LISP_CONST_BASE) | type;
113         return (a - AO_LISP_POOL_BASE) | type;
114 }
115
116 struct ao_lisp_type {
117         void    (*mark)(void *addr);
118         int     (*size)(void *addr);
119         void    (*move)(void *addr);
120 };
121
122 struct ao_lisp_cons {
123         ao_poly         car;
124         ao_poly         cdr;
125 };
126
127 struct ao_lisp_atom {
128         uint8_t         type;
129         uint8_t         pad[1];
130         ao_poly         next;
131         char            name[];
132 };
133
134 struct ao_lisp_val {
135         ao_poly         atom;
136         ao_poly         val;
137 };
138
139 struct ao_lisp_frame {
140         uint8_t                 type;
141         uint8_t                 num;
142         ao_poly                 next;
143         struct ao_lisp_val      vals[];
144 };
145
146 static inline struct ao_lisp_frame *
147 ao_lisp_poly_frame(ao_poly poly) {
148         return ao_lisp_ref(poly);
149 }
150
151 static inline ao_poly
152 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
153         return ao_lisp_poly(frame, AO_LISP_OTHER);
154 }
155
156 #define AO_LISP_LAMBDA  0
157 #define AO_LISP_NLAMBDA 1
158 #define AO_LISP_MACRO   2
159 #define AO_LISP_LEXPR   3
160
161 struct ao_lisp_builtin {
162         uint8_t         type;
163         uint8_t         args;
164         uint16_t        func;
165 };
166
167 enum ao_lisp_builtin_id {
168         builtin_car,
169         builtin_cdr,
170         builtin_cons,
171         builtin_quote,
172         builtin_set,
173         builtin_setq,
174         builtin_cond,
175         builtin_print,
176         builtin_plus,
177         builtin_minus,
178         builtin_times,
179         builtin_divide,
180         builtin_mod,
181         builtin_equal,
182         builtin_less,
183         builtin_greater,
184         builtin_less_equal,
185         builtin_greater_equal,
186         builtin_last
187 };
188
189 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
190
191 extern ao_lisp_func_t   ao_lisp_builtins[];
192
193 static inline ao_lisp_func_t
194 ao_lisp_func(struct ao_lisp_builtin *b)
195 {
196         return ao_lisp_builtins[b->func];
197 }
198
199 static inline void *
200 ao_lisp_poly_other(ao_poly poly) {
201         return ao_lisp_ref(poly);
202 }
203
204 static inline uint8_t
205 ao_lisp_other_type(void *other) {
206         return *((uint8_t *) other);
207 }
208
209 static inline ao_poly
210 ao_lisp_other_poly(const void *other)
211 {
212         return ao_lisp_poly(other, AO_LISP_OTHER);
213 }
214
215 static inline int
216 ao_lisp_mem_round(int size)
217 {
218         return (size + 3) & ~3;
219 }
220
221 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
222
223 static inline int ao_lisp_poly_type(ao_poly poly) {
224         int     type = poly & AO_LISP_TYPE_MASK;
225         if (type == AO_LISP_OTHER)
226                 return ao_lisp_other_type(ao_lisp_poly_other(poly));
227         return type;
228 }
229
230 static inline struct ao_lisp_cons *
231 ao_lisp_poly_cons(ao_poly poly)
232 {
233         return ao_lisp_ref(poly);
234 }
235
236 static inline ao_poly
237 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
238 {
239         return ao_lisp_poly(cons, AO_LISP_CONS);
240 }
241
242 static inline int
243 ao_lisp_poly_int(ao_poly poly)
244 {
245         return (int) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
246 }
247
248 static inline ao_poly
249 ao_lisp_int_poly(int i)
250 {
251         return ((ao_poly) i << 2) | AO_LISP_INT;
252 }
253
254 static inline char *
255 ao_lisp_poly_string(ao_poly poly)
256 {
257         return ao_lisp_ref(poly);
258 }
259
260 static inline ao_poly
261 ao_lisp_string_poly(char *s)
262 {
263         return ao_lisp_poly(s, AO_LISP_STRING);
264 }
265
266 static inline struct ao_lisp_atom *
267 ao_lisp_poly_atom(ao_poly poly)
268 {
269         return ao_lisp_ref(poly);
270 }
271
272 static inline ao_poly
273 ao_lisp_atom_poly(struct ao_lisp_atom *a)
274 {
275         return ao_lisp_poly(a, AO_LISP_OTHER);
276 }
277
278 static inline struct ao_lisp_builtin *
279 ao_lisp_poly_builtin(ao_poly poly)
280 {
281         return ao_lisp_ref(poly);
282 }
283
284 static inline ao_poly
285 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
286 {
287         return ao_lisp_poly(b, AO_LISP_OTHER);
288 }
289
290 /* memory functions */
291 /* returns 1 if the object was already marked */
292 int
293 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
294
295 /* returns 1 if the object was already marked */
296 int
297 ao_lisp_mark_memory(void *addr, int size);
298
299 void *
300 ao_lisp_move_map(void *addr);
301
302 /* returns 1 if the object was already moved */
303 int
304 ao_lisp_move(const struct ao_lisp_type *type, void **ref);
305
306 /* returns 1 if the object was already moved */
307 int
308 ao_lisp_move_memory(void **ref, int size);
309
310 void *
311 ao_lisp_alloc(int size);
312
313 void
314 ao_lisp_collect(void);
315
316 int
317 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr);
318
319 int
320 ao_lisp_root_poly_add(ao_poly *p);
321
322 void
323 ao_lisp_root_clear(void *addr);
324
325 /* cons */
326 extern const struct ao_lisp_type ao_lisp_cons_type;
327
328 struct ao_lisp_cons *
329 ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr);
330
331 void
332 ao_lisp_cons_print(ao_poly);
333
334 /* string */
335 extern const struct ao_lisp_type ao_lisp_string_type;
336
337 char *
338 ao_lisp_string_new(int len);
339
340 char *
341 ao_lisp_string_copy(char *a);
342
343 char *
344 ao_lisp_string_cat(char *a, char *b);
345
346 void
347 ao_lisp_string_print(ao_poly s);
348
349 /* atom */
350 extern const struct ao_lisp_type ao_lisp_atom_type;
351
352 extern struct ao_lisp_atom *ao_lisp_atoms;
353
354 extern struct ao_lisp_frame *ao_lisp_frame_current;
355
356 void
357 ao_lisp_atom_print(ao_poly a);
358
359 struct ao_lisp_atom *
360 ao_lisp_atom_intern(char *name);
361
362 ao_poly
363 ao_lisp_atom_get(ao_poly atom);
364
365 ao_poly
366 ao_lisp_atom_set(ao_poly atom, ao_poly val);
367
368 /* int */
369 void
370 ao_lisp_int_print(ao_poly i);
371
372 /* prim */
373 ao_poly
374 ao_lisp_poly_print(ao_poly p);
375
376 int
377 ao_lisp_poly_mark(ao_poly p);
378
379 /* returns 1 if the object has already been moved */
380 int
381 ao_lisp_poly_move(ao_poly *p);
382
383 /* eval */
384
385 ao_poly
386 ao_lisp_eval(ao_poly p);
387
388 ao_poly
389 ao_lisp_set_cond(struct ao_lisp_cons *cons);
390
391 /* builtin */
392 void
393 ao_lisp_builtin_print(ao_poly b);
394
395 extern const struct ao_lisp_type ao_lisp_builtin_type;
396
397 /* Check argument count */
398 ao_poly
399 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
400
401 /* Check argument type */
402 ao_poly
403 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
404
405 /* Fetch an arg (nil if off the end) */
406 ao_poly
407 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
408
409 /* read */
410 ao_poly
411 ao_lisp_read(void);
412
413 /* rep */
414 ao_poly
415 ao_lisp_read_eval_print(void);
416
417 /* frame */
418 extern const struct ao_lisp_type ao_lisp_frame_type;
419
420 ao_poly *
421 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
422
423 struct ao_lisp_frame *
424 ao_lisp_frame_new(int num);
425
426 struct ao_lisp_frame *
427 ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val);
428
429 /* error */
430
431 ao_poly
432 ao_lisp_error(int error, char *format, ...);
433
434 #endif /* _AO_LISP_H_ */