altos/lisp: Evaluate macros once, then smash them into place
[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,               /* Value computed */
177         eval_formal,            /* Formal computed */
178         eval_exec,              /* Start a lambda evaluation */
179         eval_cond,              /* Start next cond clause */
180         eval_cond_test,         /* Check cond condition */
181         eval_progn,             /* Start next progn entry */
182         eval_while,             /* Start while condition */
183         eval_while_test,        /* Check while condition */
184         eval_macro,             /* Finished with macro generation */
185 };
186
187 struct ao_lisp_stack {
188         uint8_t                 state;          /* enum eval_state */
189         ao_poly                 prev;           /* previous stack frame */
190         ao_poly                 sexprs;         /* expressions to evaluate */
191         ao_poly                 values;         /* values computed */
192         ao_poly                 values_tail;    /* end of the values list for easy appending */
193         ao_poly                 frame;          /* current lookup frame */
194         ao_poly                 list;           /* most recent function call */
195 };
196
197 static inline struct ao_lisp_stack *
198 ao_lisp_poly_stack(ao_poly p)
199 {
200         return ao_lisp_ref(p);
201 }
202
203 static inline ao_poly
204 ao_lisp_stack_poly(struct ao_lisp_stack *stack)
205 {
206         return ao_lisp_poly(stack, AO_LISP_OTHER);
207 }
208
209 extern struct ao_lisp_stack     *ao_lisp_stack;
210 extern struct ao_lisp_stack     *ao_lisp_stack_free_list;
211 extern ao_poly                  ao_lisp_v;
212
213 #define AO_LISP_FUNC_LAMBDA     0
214 #define AO_LISP_FUNC_NLAMBDA    1
215 #define AO_LISP_FUNC_MACRO      2
216 #define AO_LISP_FUNC_LEXPR      3
217
218 #define AO_LISP_FUNC_FREE_ARGS  0x80
219 #define AO_LISP_FUNC_MASK       0x7f
220
221 #define AO_LISP_FUNC_F_LAMBDA   (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LAMBDA)
222 #define AO_LISP_FUNC_F_NLAMBDA  (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_NLAMBDA)
223 #define AO_LISP_FUNC_F_MACRO    (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_MACRO)
224 #define AO_LISP_FUNC_F_LEXPR    (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LEXPR)
225
226 struct ao_lisp_builtin {
227         uint8_t         type;
228         uint8_t         args;
229         uint16_t        func;
230 };
231
232 enum ao_lisp_builtin_id {
233         builtin_eval,
234         builtin_read,
235         builtin_lambda,
236         builtin_lexpr,
237         builtin_nlambda,
238         builtin_macro,
239         builtin_car,
240         builtin_cdr,
241         builtin_cons,
242         builtin_last,
243         builtin_length,
244         builtin_quote,
245         builtin_set,
246         builtin_setq,
247         builtin_cond,
248         builtin_progn,
249         builtin_while,
250         builtin_print,
251         builtin_patom,
252         builtin_plus,
253         builtin_minus,
254         builtin_times,
255         builtin_divide,
256         builtin_mod,
257         builtin_equal,
258         builtin_less,
259         builtin_greater,
260         builtin_less_equal,
261         builtin_greater_equal,
262         builtin_pack,
263         builtin_unpack,
264         builtin_flush,
265         builtin_delay,
266         builtin_led,
267         builtin_save,
268         builtin_restore,
269         _builtin_last
270 };
271
272 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
273
274 extern const ao_lisp_func_t     ao_lisp_builtins[];
275
276 static inline ao_lisp_func_t
277 ao_lisp_func(struct ao_lisp_builtin *b)
278 {
279         return ao_lisp_builtins[b->func];
280 }
281
282 struct ao_lisp_lambda {
283         uint8_t         type;
284         uint8_t         args;
285         ao_poly         code;
286         ao_poly         frame;
287 };
288
289 static inline struct ao_lisp_lambda *
290 ao_lisp_poly_lambda(ao_poly poly)
291 {
292         return ao_lisp_ref(poly);
293 }
294
295 static inline ao_poly
296 ao_lisp_lambda_poly(struct ao_lisp_lambda *lambda)
297 {
298         return ao_lisp_poly(lambda, AO_LISP_OTHER);
299 }
300
301 static inline void *
302 ao_lisp_poly_other(ao_poly poly) {
303         return ao_lisp_ref(poly);
304 }
305
306 static inline uint8_t
307 ao_lisp_other_type(void *other) {
308         return *((uint8_t *) other);
309 }
310
311 static inline ao_poly
312 ao_lisp_other_poly(const void *other)
313 {
314         return ao_lisp_poly(other, AO_LISP_OTHER);
315 }
316
317 static inline int
318 ao_lisp_size_round(int size)
319 {
320         return (size + 3) & ~3;
321 }
322
323 static inline int
324 ao_lisp_size(const struct ao_lisp_type *type, void *addr)
325 {
326         return ao_lisp_size_round(type->size(addr));
327 }
328
329 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
330
331 static inline int ao_lisp_poly_base_type(ao_poly poly) {
332         return poly & AO_LISP_TYPE_MASK;
333 }
334
335 static inline int ao_lisp_poly_type(ao_poly poly) {
336         int     type = poly & AO_LISP_TYPE_MASK;
337         if (type == AO_LISP_OTHER)
338                 return ao_lisp_other_type(ao_lisp_poly_other(poly));
339         return type;
340 }
341
342 static inline struct ao_lisp_cons *
343 ao_lisp_poly_cons(ao_poly poly)
344 {
345         return ao_lisp_ref(poly);
346 }
347
348 static inline ao_poly
349 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
350 {
351         return ao_lisp_poly(cons, AO_LISP_CONS);
352 }
353
354 static inline int
355 ao_lisp_poly_int(ao_poly poly)
356 {
357         return (int) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
358 }
359
360 static inline ao_poly
361 ao_lisp_int_poly(int i)
362 {
363         return ((ao_poly) i << 2) | AO_LISP_INT;
364 }
365
366 static inline char *
367 ao_lisp_poly_string(ao_poly poly)
368 {
369         return ao_lisp_ref(poly);
370 }
371
372 static inline ao_poly
373 ao_lisp_string_poly(char *s)
374 {
375         return ao_lisp_poly(s, AO_LISP_STRING);
376 }
377
378 static inline struct ao_lisp_atom *
379 ao_lisp_poly_atom(ao_poly poly)
380 {
381         return ao_lisp_ref(poly);
382 }
383
384 static inline ao_poly
385 ao_lisp_atom_poly(struct ao_lisp_atom *a)
386 {
387         return ao_lisp_poly(a, AO_LISP_OTHER);
388 }
389
390 static inline struct ao_lisp_builtin *
391 ao_lisp_poly_builtin(ao_poly poly)
392 {
393         return ao_lisp_ref(poly);
394 }
395
396 static inline ao_poly
397 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
398 {
399         return ao_lisp_poly(b, AO_LISP_OTHER);
400 }
401
402 /* memory functions */
403
404 extern int ao_lisp_collects;
405
406 /* returns 1 if the object was already marked */
407 int
408 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
409
410 /* returns 1 if the object was already marked */
411 int
412 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr);
413
414 void *
415 ao_lisp_move_map(void *addr);
416
417 /* returns 1 if the object was already moved */
418 int
419 ao_lisp_move(const struct ao_lisp_type *type, void **ref);
420
421 /* returns 1 if the object was already moved */
422 int
423 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref);
424
425 void *
426 ao_lisp_alloc(int size);
427
428 void
429 ao_lisp_collect(void);
430
431 void
432 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons);
433
434 struct ao_lisp_cons *
435 ao_lisp_cons_fetch(int id);
436
437 void
438 ao_lisp_string_stash(int id, char *string);
439
440 char *
441 ao_lisp_string_fetch(int id);
442
443 void
444 ao_lisp_poly_stash(int id, ao_poly poly);
445
446 ao_poly
447 ao_lisp_poly_fetch(int id);
448
449 /* cons */
450 extern const struct ao_lisp_type ao_lisp_cons_type;
451
452 struct ao_lisp_cons *
453 ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr);
454
455 extern struct ao_lisp_cons *ao_lisp_cons_free_list;
456
457 void
458 ao_lisp_cons_free(struct ao_lisp_cons *cons);
459
460 void
461 ao_lisp_cons_print(ao_poly);
462
463 void
464 ao_lisp_cons_patom(ao_poly);
465
466 int
467 ao_lisp_cons_length(struct ao_lisp_cons *cons);
468
469 /* string */
470 extern const struct ao_lisp_type ao_lisp_string_type;
471
472 char *
473 ao_lisp_string_copy(char *a);
474
475 char *
476 ao_lisp_string_cat(char *a, char *b);
477
478 ao_poly
479 ao_lisp_string_pack(struct ao_lisp_cons *cons);
480
481 ao_poly
482 ao_lisp_string_unpack(char *a);
483
484 void
485 ao_lisp_string_print(ao_poly s);
486
487 void
488 ao_lisp_string_patom(ao_poly s);
489
490 /* atom */
491 extern const struct ao_lisp_type ao_lisp_atom_type;
492
493 extern struct ao_lisp_atom      *ao_lisp_atoms;
494 extern struct ao_lisp_frame     *ao_lisp_frame_global;
495 extern struct ao_lisp_frame     *ao_lisp_frame_current;
496
497 void
498 ao_lisp_atom_print(ao_poly a);
499
500 struct ao_lisp_atom *
501 ao_lisp_atom_intern(char *name);
502
503 ao_poly
504 ao_lisp_atom_get(ao_poly atom);
505
506 ao_poly
507 ao_lisp_atom_set(ao_poly atom, ao_poly val);
508
509 /* int */
510 void
511 ao_lisp_int_print(ao_poly i);
512
513 /* prim */
514 void
515 ao_lisp_poly_print(ao_poly p);
516
517 void
518 ao_lisp_poly_patom(ao_poly p);
519
520 int
521 ao_lisp_poly_mark(ao_poly p, uint8_t note_cons);
522
523 /* returns 1 if the object has already been moved */
524 int
525 ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
526
527 /* eval */
528
529 void
530 ao_lisp_eval_clear_globals(void);
531
532 int
533 ao_lisp_eval_restart(void);
534
535 ao_poly
536 ao_lisp_eval(ao_poly p);
537
538 ao_poly
539 ao_lisp_set_cond(struct ao_lisp_cons *cons);
540
541 /* builtin */
542 void
543 ao_lisp_builtin_print(ao_poly b);
544
545 extern const struct ao_lisp_type ao_lisp_builtin_type;
546
547 /* Check argument count */
548 ao_poly
549 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
550
551 /* Check argument type */
552 ao_poly
553 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
554
555 /* Fetch an arg (nil if off the end) */
556 ao_poly
557 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
558
559 char *
560 ao_lisp_args_name(uint8_t args);
561
562 /* read */
563 extern struct ao_lisp_cons      *ao_lisp_read_cons;
564 extern struct ao_lisp_cons      *ao_lisp_read_cons_tail;
565 extern struct ao_lisp_cons      *ao_lisp_read_stack;
566
567 ao_poly
568 ao_lisp_read(void);
569
570 /* rep */
571 ao_poly
572 ao_lisp_read_eval_print(void);
573
574 /* frame */
575 extern const struct ao_lisp_type ao_lisp_frame_type;
576
577 ao_poly *
578 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
579
580 struct ao_lisp_frame *
581 ao_lisp_frame_new(int num);
582
583 int
584 ao_lisp_frame_add(struct ao_lisp_frame **frame, ao_poly atom, ao_poly val);
585
586 void
587 ao_lisp_frame_print(ao_poly p);
588
589 /* lambda */
590 extern const struct ao_lisp_type ao_lisp_lambda_type;
591
592 struct ao_lisp_lambda *
593 ao_lisp_lambda_new(ao_poly cons);
594
595 void
596 ao_lisp_lambda_print(ao_poly lambda);
597
598 ao_poly
599 ao_lisp_lambda(struct ao_lisp_cons *cons);
600
601 ao_poly
602 ao_lisp_lexpr(struct ao_lisp_cons *cons);
603
604 ao_poly
605 ao_lisp_nlambda(struct ao_lisp_cons *cons);
606
607 ao_poly
608 ao_lisp_macro(struct ao_lisp_cons *cons);
609
610 ao_poly
611 ao_lisp_lambda_eval(void);
612
613 /* save */
614
615 ao_poly
616 ao_lisp_save(struct ao_lisp_cons *cons);
617
618 ao_poly
619 ao_lisp_restore(struct ao_lisp_cons *cons);
620
621 /* error */
622
623 extern const struct ao_lisp_type ao_lisp_stack_type;
624
625 void
626 ao_lisp_stack_print(void);
627
628 ao_poly
629 ao_lisp_error(int error, char *format, ...);
630
631 /* debugging macros */
632
633 #if DBG_EVAL
634 #define DBG_CODE        1
635 int ao_lisp_stack_depth;
636 #define DBG_DO(a)       a
637 #define DBG_INDENT()    do { int _s; for(_s = 0; _s < ao_lisp_stack_depth; _s++) printf("  "); } while(0)
638 #define DBG_IN()        (++ao_lisp_stack_depth)
639 #define DBG_OUT()       (--ao_lisp_stack_depth)
640 #define DBG_RESET()     (ao_lisp_stack_depth = 0)
641 #define DBG(...)        printf(__VA_ARGS__)
642 #define DBGI(...)       do { DBG("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
643 #define DBG_CONS(a)     ao_lisp_cons_print(ao_lisp_cons_poly(a))
644 #define DBG_POLY(a)     ao_lisp_poly_print(a)
645 #define OFFSET(a)       ((a) ? (int) ((uint8_t *) a - ao_lisp_pool) : -1)
646 #define DBG_STACK()     ao_lisp_stack_print()
647 static inline void
648 ao_lisp_frames_dump(void)
649 {
650         struct ao_lisp_stack *s;
651         DBGI(".. current frame: "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
652         for (s = ao_lisp_stack; s; s = ao_lisp_poly_stack(s->prev)) {
653                 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
654         }
655 }
656 #define DBG_FRAMES()    ao_lisp_frames_dump()
657 #else
658 #define DBG_DO(a)
659 #define DBG_INDENT()
660 #define DBG_IN()
661 #define DBG_OUT()
662 #define DBG(...)
663 #define DBGI(...)
664 #define DBG_CONS(a)
665 #define DBG_POLY(a)
666 #define DBG_RESET()
667 #define DBG_STACK()
668 #define DBG_FRAMES()
669 #endif
670
671 #define DBG_MEM         0
672 #define DBG_MEM_START   1
673
674 #if DBG_MEM
675
676 #include <assert.h>
677 extern int dbg_move_depth;
678 #define MDBG_DUMP 1
679 #define MDBG_OFFSET(a)  ((int) ((uint8_t *) (a) - ao_lisp_pool))
680
681 extern int dbg_mem;
682
683 #define MDBG_DO(a)      a
684 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf ("  "); printf(__VA_ARGS__); } } while (0)
685 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
686 #define MDBG_MOVE_IN()  (dbg_move_depth++)
687 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
688
689 #else
690
691 #define MDBG_DO(a)
692 #define MDBG_MOVE(...)
693 #define MDBG_MORE(...)
694 #define MDBG_MOVE_IN()
695 #define MDBG_MOVE_OUT()
696
697 #endif
698
699 #endif /* _AO_LISP_H_ */