altos/lisp: Change GC to do moves in batches of 32
[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 <stdint.h>
19 #include <string.h>
20 //#include <stdio.h>
21 #include <ao_lisp_os.h>
22
23 typedef uint16_t        ao_poly;
24 typedef int16_t         ao_signed_poly;
25
26 #ifdef AO_LISP_SAVE
27
28 struct ao_lisp_os_save {
29         ao_poly         atoms;
30         ao_poly         globals;
31         uint16_t        const_checksum;
32         uint16_t        const_checksum_inv;
33 };
34
35 #define AO_LISP_POOL_EXTRA      (sizeof(struct ao_lisp_os_save))
36 #define AO_LISP_POOL    ((int) (AO_LISP_POOL_TOTAL - AO_LISP_POOL_EXTRA))
37
38 int
39 ao_lisp_os_save(void);
40
41 int
42 ao_lisp_os_restore_save(struct ao_lisp_os_save *save, int offset);
43
44 int
45 ao_lisp_os_restore(void);
46
47 #endif
48
49 #ifdef AO_LISP_MAKE_CONST
50 #define AO_LISP_POOL_CONST      16384
51 extern uint8_t ao_lisp_const[AO_LISP_POOL_CONST];
52 #define ao_lisp_pool ao_lisp_const
53 #define AO_LISP_POOL AO_LISP_POOL_CONST
54
55 #define _atom(n) ao_lisp_atom_poly(ao_lisp_atom_intern(n))
56
57 #define _ao_lisp_atom_quote     _atom("quote")
58 #define _ao_lisp_atom_set       _atom("set")
59 #define _ao_lisp_atom_setq      _atom("setq")
60 #define _ao_lisp_atom_t         _atom("t")
61 #define _ao_lisp_atom_car       _atom("car")
62 #define _ao_lisp_atom_cdr       _atom("cdr")
63 #define _ao_lisp_atom_cons      _atom("cons")
64 #define _ao_lisp_atom_last      _atom("last")
65 #define _ao_lisp_atom_length    _atom("length")
66 #define _ao_lisp_atom_cond      _atom("cond")
67 #define _ao_lisp_atom_lambda    _atom("lambda")
68 #define _ao_lisp_atom_led       _atom("led")
69 #define _ao_lisp_atom_delay     _atom("delay")
70 #define _ao_lisp_atom_pack      _atom("pack")
71 #define _ao_lisp_atom_unpack    _atom("unpack")
72 #define _ao_lisp_atom_flush     _atom("flush")
73 #define _ao_lisp_atom_eval      _atom("eval")
74 #define _ao_lisp_atom_read      _atom("read")
75 #define _ao_lisp_atom_eof       _atom("eof")
76 #define _ao_lisp_atom_save      _atom("save")
77 #define _ao_lisp_atom_restore   _atom("restore")
78 #else
79 #include "ao_lisp_const.h"
80 #ifndef AO_LISP_POOL
81 #define AO_LISP_POOL    3072
82 #endif
83 extern uint8_t          ao_lisp_pool[AO_LISP_POOL + AO_LISP_POOL_EXTRA];
84 #endif
85
86 /* Primitive types */
87 #define AO_LISP_CONS            0
88 #define AO_LISP_INT             1
89 #define AO_LISP_STRING          2
90 #define AO_LISP_OTHER           3
91
92 #define AO_LISP_TYPE_MASK       0x0003
93 #define AO_LISP_TYPE_SHIFT      2
94 #define AO_LISP_REF_MASK        0x7ffc
95 #define AO_LISP_CONST           0x8000
96
97 /* These have a type value at the start of the struct */
98 #define AO_LISP_ATOM            4
99 #define AO_LISP_BUILTIN         5
100 #define AO_LISP_FRAME           6
101 #define AO_LISP_LAMBDA          7
102 #define AO_LISP_NUM_TYPE        8
103
104 #define AO_LISP_NIL     0
105
106 extern uint16_t         ao_lisp_top;
107
108 #define AO_LISP_OOM             0x01
109 #define AO_LISP_DIVIDE_BY_ZERO  0x02
110 #define AO_LISP_INVALID         0x04
111 #define AO_LISP_UNDEFINED       0x08
112 #define AO_LISP_EOF             0x10
113
114 extern uint8_t          ao_lisp_exception;
115
116 static inline int
117 ao_lisp_is_const(ao_poly poly) {
118         return poly & AO_LISP_CONST;
119 }
120
121 #define AO_LISP_POOL_BASE       (ao_lisp_pool - 4)
122 #define AO_LISP_CONST_BASE      (ao_lisp_const - 4)
123
124 #define AO_LISP_IS_CONST(a)     (ao_lisp_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_const + AO_LISP_POOL_CONST)
125 #define AO_LISP_IS_POOL(a)      (ao_lisp_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_pool + AO_LISP_POOL)
126
127 void *
128 ao_lisp_ref(ao_poly poly);
129
130 ao_poly
131 ao_lisp_poly(const void *addr, ao_poly type);
132
133 struct ao_lisp_type {
134         int     (*size)(void *addr);
135         void    (*mark)(void *addr);
136         void    (*move)(void *addr);
137         char    name[];
138 };
139
140 struct ao_lisp_cons {
141         ao_poly         car;
142         ao_poly         cdr;
143 };
144
145 struct ao_lisp_atom {
146         uint8_t         type;
147         uint8_t         pad[1];
148         ao_poly         next;
149         char            name[];
150 };
151
152 struct ao_lisp_val {
153         ao_poly         atom;
154         ao_poly         val;
155 };
156
157 struct ao_lisp_frame {
158         uint8_t                 type;
159         uint8_t                 num;
160         ao_poly                 next;
161         struct ao_lisp_val      vals[];
162 };
163
164 static inline struct ao_lisp_frame *
165 ao_lisp_poly_frame(ao_poly poly) {
166         return ao_lisp_ref(poly);
167 }
168
169 static inline ao_poly
170 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
171         return ao_lisp_poly(frame, AO_LISP_OTHER);
172 }
173
174 enum eval_state {
175         eval_sexpr,             /* Evaluate an sexpr */
176         eval_val,
177         eval_formal,
178         eval_exec,
179         eval_cond,
180         eval_cond_test,
181         eval_progn,
182         eval_while,
183         eval_while_test,
184 };
185
186 struct ao_lisp_stack {
187         uint8_t                 state;          /* enum eval_state */
188         ao_poly                 prev;           /* previous stack frame */
189         ao_poly                 sexprs;         /* expressions to evaluate */
190         ao_poly                 values;         /* values computed */
191         ao_poly                 values_tail;    /* end of the values list for easy appending */
192         ao_poly                 frame;          /* current lookup frame */
193         ao_poly                 list;           /* most recent function call */
194 };
195
196 static inline struct ao_lisp_stack *
197 ao_lisp_poly_stack(ao_poly p)
198 {
199         return ao_lisp_ref(p);
200 }
201
202 static inline ao_poly
203 ao_lisp_stack_poly(struct ao_lisp_stack *stack)
204 {
205         return ao_lisp_poly(stack, AO_LISP_OTHER);
206 }
207
208 extern struct ao_lisp_stack     *ao_lisp_stack;
209 extern ao_poly                  ao_lisp_v;
210
211 #define AO_LISP_FUNC_LAMBDA     0
212 #define AO_LISP_FUNC_NLAMBDA    1
213 #define AO_LISP_FUNC_MACRO      2
214 #define AO_LISP_FUNC_LEXPR      3
215
216 struct ao_lisp_builtin {
217         uint8_t         type;
218         uint8_t         args;
219         uint16_t        func;
220 };
221
222 enum ao_lisp_builtin_id {
223         builtin_eval,
224         builtin_read,
225         builtin_lambda,
226         builtin_lexpr,
227         builtin_nlambda,
228         builtin_macro,
229         builtin_car,
230         builtin_cdr,
231         builtin_cons,
232         builtin_last,
233         builtin_length,
234         builtin_quote,
235         builtin_set,
236         builtin_setq,
237         builtin_cond,
238         builtin_progn,
239         builtin_while,
240         builtin_print,
241         builtin_patom,
242         builtin_plus,
243         builtin_minus,
244         builtin_times,
245         builtin_divide,
246         builtin_mod,
247         builtin_equal,
248         builtin_less,
249         builtin_greater,
250         builtin_less_equal,
251         builtin_greater_equal,
252         builtin_pack,
253         builtin_unpack,
254         builtin_flush,
255         builtin_delay,
256         builtin_led,
257         builtin_save,
258         builtin_restore,
259         _builtin_last
260 };
261
262 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
263
264 extern const ao_lisp_func_t     ao_lisp_builtins[];
265
266 static inline ao_lisp_func_t
267 ao_lisp_func(struct ao_lisp_builtin *b)
268 {
269         return ao_lisp_builtins[b->func];
270 }
271
272 struct ao_lisp_lambda {
273         uint8_t         type;
274         uint8_t         args;
275         ao_poly         code;
276         ao_poly         frame;
277 };
278
279 static inline struct ao_lisp_lambda *
280 ao_lisp_poly_lambda(ao_poly poly)
281 {
282         return ao_lisp_ref(poly);
283 }
284
285 static inline ao_poly
286 ao_lisp_lambda_poly(struct ao_lisp_lambda *lambda)
287 {
288         return ao_lisp_poly(lambda, AO_LISP_OTHER);
289 }
290
291 static inline void *
292 ao_lisp_poly_other(ao_poly poly) {
293         return ao_lisp_ref(poly);
294 }
295
296 static inline uint8_t
297 ao_lisp_other_type(void *other) {
298         return *((uint8_t *) other);
299 }
300
301 static inline ao_poly
302 ao_lisp_other_poly(const void *other)
303 {
304         return ao_lisp_poly(other, AO_LISP_OTHER);
305 }
306
307 static inline int
308 ao_lisp_size_round(int size)
309 {
310         return (size + 3) & ~3;
311 }
312
313 static inline int
314 ao_lisp_size(const struct ao_lisp_type *type, void *addr)
315 {
316         return ao_lisp_size_round(type->size(addr));
317 }
318
319 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
320
321 static inline int ao_lisp_poly_base_type(ao_poly poly) {
322         return poly & AO_LISP_TYPE_MASK;
323 }
324
325 static inline int ao_lisp_poly_type(ao_poly poly) {
326         int     type = poly & AO_LISP_TYPE_MASK;
327         if (type == AO_LISP_OTHER)
328                 return ao_lisp_other_type(ao_lisp_poly_other(poly));
329         return type;
330 }
331
332 static inline struct ao_lisp_cons *
333 ao_lisp_poly_cons(ao_poly poly)
334 {
335         return ao_lisp_ref(poly);
336 }
337
338 static inline ao_poly
339 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
340 {
341         return ao_lisp_poly(cons, AO_LISP_CONS);
342 }
343
344 static inline int
345 ao_lisp_poly_int(ao_poly poly)
346 {
347         return (int) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
348 }
349
350 static inline ao_poly
351 ao_lisp_int_poly(int i)
352 {
353         return ((ao_poly) i << 2) | AO_LISP_INT;
354 }
355
356 static inline char *
357 ao_lisp_poly_string(ao_poly poly)
358 {
359         return ao_lisp_ref(poly);
360 }
361
362 static inline ao_poly
363 ao_lisp_string_poly(char *s)
364 {
365         return ao_lisp_poly(s, AO_LISP_STRING);
366 }
367
368 static inline struct ao_lisp_atom *
369 ao_lisp_poly_atom(ao_poly poly)
370 {
371         return ao_lisp_ref(poly);
372 }
373
374 static inline ao_poly
375 ao_lisp_atom_poly(struct ao_lisp_atom *a)
376 {
377         return ao_lisp_poly(a, AO_LISP_OTHER);
378 }
379
380 static inline struct ao_lisp_builtin *
381 ao_lisp_poly_builtin(ao_poly poly)
382 {
383         return ao_lisp_ref(poly);
384 }
385
386 static inline ao_poly
387 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
388 {
389         return ao_lisp_poly(b, AO_LISP_OTHER);
390 }
391
392 /* memory functions */
393 /* returns 1 if the object was already marked */
394 int
395 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
396
397 /* returns 1 if the object was already marked */
398 int
399 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr);
400
401 void *
402 ao_lisp_move_map(void *addr);
403
404 /* returns 1 if the object was already moved */
405 int
406 ao_lisp_move(const struct ao_lisp_type *type, void **ref);
407
408 /* returns 1 if the object was already moved */
409 int
410 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref);
411
412 void *
413 ao_lisp_alloc(int size);
414
415 void
416 ao_lisp_collect(void);
417
418 void
419 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons);
420
421 struct ao_lisp_cons *
422 ao_lisp_cons_fetch(int id);
423
424 void
425 ao_lisp_string_stash(int id, char *string);
426
427 char *
428 ao_lisp_string_fetch(int id);
429
430 void
431 ao_lisp_poly_stash(int id, ao_poly poly);
432
433 ao_poly
434 ao_lisp_poly_fetch(int id);
435
436 /* cons */
437 extern const struct ao_lisp_type ao_lisp_cons_type;
438
439 struct ao_lisp_cons *
440 ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr);
441
442 void
443 ao_lisp_cons_print(ao_poly);
444
445 void
446 ao_lisp_cons_patom(ao_poly);
447
448 int
449 ao_lisp_cons_length(struct ao_lisp_cons *cons);
450
451 /* string */
452 extern const struct ao_lisp_type ao_lisp_string_type;
453
454 char *
455 ao_lisp_string_copy(char *a);
456
457 char *
458 ao_lisp_string_cat(char *a, char *b);
459
460 ao_poly
461 ao_lisp_string_pack(struct ao_lisp_cons *cons);
462
463 ao_poly
464 ao_lisp_string_unpack(char *a);
465
466 void
467 ao_lisp_string_print(ao_poly s);
468
469 void
470 ao_lisp_string_patom(ao_poly s);
471
472 /* atom */
473 extern const struct ao_lisp_type ao_lisp_atom_type;
474
475 extern struct ao_lisp_atom      *ao_lisp_atoms;
476 extern struct ao_lisp_frame     *ao_lisp_frame_global;
477 extern struct ao_lisp_frame     *ao_lisp_frame_current;
478
479 void
480 ao_lisp_atom_print(ao_poly a);
481
482 struct ao_lisp_atom *
483 ao_lisp_atom_intern(char *name);
484
485 ao_poly
486 ao_lisp_atom_get(ao_poly atom);
487
488 ao_poly
489 ao_lisp_atom_set(ao_poly atom, ao_poly val);
490
491 /* int */
492 void
493 ao_lisp_int_print(ao_poly i);
494
495 /* prim */
496 void
497 ao_lisp_poly_print(ao_poly p);
498
499 void
500 ao_lisp_poly_patom(ao_poly p);
501
502 int
503 ao_lisp_poly_mark(ao_poly p, uint8_t note_cons);
504
505 /* returns 1 if the object has already been moved */
506 int
507 ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
508
509 /* eval */
510
511 void
512 ao_lisp_eval_clear_globals(void);
513
514 int
515 ao_lisp_eval_restart(void);
516
517 ao_poly
518 ao_lisp_eval(ao_poly p);
519
520 ao_poly
521 ao_lisp_set_cond(struct ao_lisp_cons *cons);
522
523 /* builtin */
524 void
525 ao_lisp_builtin_print(ao_poly b);
526
527 extern const struct ao_lisp_type ao_lisp_builtin_type;
528
529 /* Check argument count */
530 ao_poly
531 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
532
533 /* Check argument type */
534 ao_poly
535 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
536
537 /* Fetch an arg (nil if off the end) */
538 ao_poly
539 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
540
541 char *
542 ao_lisp_args_name(uint8_t args);
543
544 /* read */
545 extern struct ao_lisp_cons      *ao_lisp_read_cons;
546 extern struct ao_lisp_cons      *ao_lisp_read_cons_tail;
547 extern struct ao_lisp_cons      *ao_lisp_read_stack;
548
549 ao_poly
550 ao_lisp_read(void);
551
552 /* rep */
553 ao_poly
554 ao_lisp_read_eval_print(void);
555
556 /* frame */
557 extern const struct ao_lisp_type ao_lisp_frame_type;
558
559 ao_poly *
560 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
561
562 struct ao_lisp_frame *
563 ao_lisp_frame_new(int num);
564
565 int
566 ao_lisp_frame_add(struct ao_lisp_frame **frame, ao_poly atom, ao_poly val);
567
568 void
569 ao_lisp_frame_print(ao_poly p);
570
571 /* lambda */
572 extern const struct ao_lisp_type ao_lisp_lambda_type;
573
574 struct ao_lisp_lambda *
575 ao_lisp_lambda_new(ao_poly cons);
576
577 void
578 ao_lisp_lambda_print(ao_poly lambda);
579
580 ao_poly
581 ao_lisp_lambda(struct ao_lisp_cons *cons);
582
583 ao_poly
584 ao_lisp_lexpr(struct ao_lisp_cons *cons);
585
586 ao_poly
587 ao_lisp_nlambda(struct ao_lisp_cons *cons);
588
589 ao_poly
590 ao_lisp_macro(struct ao_lisp_cons *cons);
591
592 ao_poly
593 ao_lisp_lambda_eval(void);
594
595 /* save */
596
597 ao_poly
598 ao_lisp_save(struct ao_lisp_cons *cons);
599
600 ao_poly
601 ao_lisp_restore(struct ao_lisp_cons *cons);
602
603 /* error */
604
605 extern const struct ao_lisp_type ao_lisp_stack_type;
606
607 void
608 ao_lisp_stack_print(void);
609
610 ao_poly
611 ao_lisp_error(int error, char *format, ...);
612
613 /* debugging macros */
614
615 #if DBG_EVAL
616 #define DBG_CODE        1
617 int ao_lisp_stack_depth;
618 #define DBG_DO(a)       a
619 #define DBG_INDENT()    do { int _s; for(_s = 0; _s < ao_lisp_stack_depth; _s++) printf("  "); } while(0)
620 #define DBG_IN()        (++ao_lisp_stack_depth)
621 #define DBG_OUT()       (--ao_lisp_stack_depth)
622 #define DBG_RESET()     (ao_lisp_stack_depth = 0)
623 #define DBG(...)        printf(__VA_ARGS__)
624 #define DBGI(...)       do { DBG("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
625 #define DBG_CONS(a)     ao_lisp_cons_print(ao_lisp_cons_poly(a))
626 #define DBG_POLY(a)     ao_lisp_poly_print(a)
627 #define OFFSET(a)       ((a) ? (int) ((uint8_t *) a - ao_lisp_pool) : -1)
628 #define DBG_STACK()     ao_lisp_stack_print()
629 static inline void
630 ao_lisp_frames_dump(void)
631 {
632         struct ao_lisp_stack *s;
633         DBGI(".. current frame: "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
634         for (s = ao_lisp_stack; s; s = ao_lisp_poly_stack(s->prev)) {
635                 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
636         }
637 }
638 #define DBG_FRAMES()    ao_lisp_frames_dump()
639 #else
640 #define DBG_DO(a)
641 #define DBG_INDENT()
642 #define DBG_IN()
643 #define DBG_OUT()
644 #define DBG(...)
645 #define DBGI(...)
646 #define DBG_CONS(a)
647 #define DBG_POLY(a)
648 #define DBG_RESET()
649 #define DBG_STACK()
650 #define DBG_FRAMES()
651 #endif
652
653 #define DBG_MEM         1
654 #define DBG_MEM_START   1
655
656 #if DBG_MEM
657
658 #include <assert.h>
659 extern int dbg_move_depth;
660 #define MDBG_DUMP 1
661 #define MDBG_OFFSET(a)  ((int) ((uint8_t *) (a) - ao_lisp_pool))
662
663 extern int dbg_mem;
664
665 #define MDBG_DO(a)      a
666 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf ("  "); printf(__VA_ARGS__); } } while (0)
667 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
668 #define MDBG_MOVE_IN()  (dbg_move_depth++)
669 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
670
671 #else
672
673 #define MDBG_DO(a)
674 #define MDBG_MOVE(...)
675 #define MDBG_MORE(...)
676 #define MDBG_MOVE_IN()
677 #define MDBG_MOVE_OUT()
678
679 #endif
680
681 #endif /* _AO_LISP_H_ */