altos/lisp: Split out read debug, add memory validation
[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 #define DBG_MEM         0
19 #define DBG_EVAL        0
20 #define DBG_READ        0
21 #define DBG_FREE_CONS   0
22 #define NDEBUG          1
23
24 #include <stdint.h>
25 #include <string.h>
26 #include <ao_lisp_os.h>
27 #ifndef __BYTE_ORDER
28 #include <endian.h>
29 #endif
30
31 typedef uint16_t        ao_poly;
32 typedef int16_t         ao_signed_poly;
33
34 #ifdef AO_LISP_SAVE
35
36 struct ao_lisp_os_save {
37         ao_poly         atoms;
38         ao_poly         globals;
39         uint16_t        const_checksum;
40         uint16_t        const_checksum_inv;
41 };
42
43 #define AO_LISP_POOL_EXTRA      (sizeof(struct ao_lisp_os_save))
44 #define AO_LISP_POOL    ((int) (AO_LISP_POOL_TOTAL - AO_LISP_POOL_EXTRA))
45
46 int
47 ao_lisp_os_save(void);
48
49 int
50 ao_lisp_os_restore_save(struct ao_lisp_os_save *save, int offset);
51
52 int
53 ao_lisp_os_restore(void);
54
55 #endif
56
57 #ifdef AO_LISP_MAKE_CONST
58 #define AO_LISP_POOL_CONST      16384
59 extern uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));
60 #define ao_lisp_pool ao_lisp_const
61 #define AO_LISP_POOL AO_LISP_POOL_CONST
62
63 #define _atom(n) ao_lisp_atom_poly(ao_lisp_atom_intern(n))
64 #define _bool(v) ao_lisp_bool_poly(ao_lisp_bool_get(v))
65
66 #define _ao_lisp_bool_true      _bool(1)
67 #define _ao_lisp_bool_false     _bool(0)
68
69 #define _ao_lisp_atom_eof       _atom("eof")
70 #define _ao_lisp_atom_else      _atom("else")
71
72 #define AO_LISP_BUILTIN_ATOMS
73 #include "ao_lisp_builtin.h"
74
75 #else
76 #include "ao_lisp_const.h"
77 #ifndef AO_LISP_POOL
78 #define AO_LISP_POOL    3072
79 #endif
80 extern uint8_t          ao_lisp_pool[AO_LISP_POOL + AO_LISP_POOL_EXTRA] __attribute__((aligned(4)));
81 #endif
82
83 /* Primitive types */
84 #define AO_LISP_CONS            0
85 #define AO_LISP_INT             1
86 #define AO_LISP_STRING          2
87 #define AO_LISP_OTHER           3
88
89 #define AO_LISP_TYPE_MASK       0x0003
90 #define AO_LISP_TYPE_SHIFT      2
91 #define AO_LISP_REF_MASK        0x7ffc
92 #define AO_LISP_CONST           0x8000
93
94 /* These have a type value at the start of the struct */
95 #define AO_LISP_ATOM            4
96 #define AO_LISP_BUILTIN         5
97 #define AO_LISP_FRAME           6
98 #define AO_LISP_FRAME_VALS      7
99 #define AO_LISP_LAMBDA          8
100 #define AO_LISP_STACK           9
101 #define AO_LISP_BOOL            10
102 #define AO_LISP_BIGINT          11
103 #define AO_LISP_FLOAT           12
104 #define AO_LISP_NUM_TYPE        13
105
106 /* Leave two bits for types to use as they please */
107 #define AO_LISP_OTHER_TYPE_MASK 0x3f
108
109 #define AO_LISP_NIL     0
110
111 extern uint16_t         ao_lisp_top;
112
113 #define AO_LISP_OOM             0x01
114 #define AO_LISP_DIVIDE_BY_ZERO  0x02
115 #define AO_LISP_INVALID         0x04
116 #define AO_LISP_UNDEFINED       0x08
117 #define AO_LISP_REDEFINED       0x10
118 #define AO_LISP_EOF             0x20
119 #define AO_LISP_EXIT            0x40
120
121 extern uint8_t          ao_lisp_exception;
122
123 static inline int
124 ao_lisp_is_const(ao_poly poly) {
125         return poly & AO_LISP_CONST;
126 }
127
128 #define AO_LISP_IS_CONST(a)     (ao_lisp_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_const + AO_LISP_POOL_CONST)
129 #define AO_LISP_IS_POOL(a)      (ao_lisp_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_pool + AO_LISP_POOL)
130 #define AO_LISP_IS_INT(p)       (ao_lisp_poly_base_type(p) == AO_LISP_INT)
131
132 void *
133 ao_lisp_ref(ao_poly poly);
134
135 ao_poly
136 ao_lisp_poly(const void *addr, ao_poly type);
137
138 struct ao_lisp_type {
139         int     (*size)(void *addr);
140         void    (*mark)(void *addr);
141         void    (*move)(void *addr);
142         char    name[];
143 };
144
145 struct ao_lisp_cons {
146         ao_poly         car;
147         ao_poly         cdr;
148 };
149
150 struct ao_lisp_atom {
151         uint8_t         type;
152         uint8_t         pad[1];
153         ao_poly         next;
154         char            name[];
155 };
156
157 struct ao_lisp_val {
158         ao_poly         atom;
159         ao_poly         val;
160 };
161
162 struct ao_lisp_frame_vals {
163         uint8_t                 type;
164         uint8_t                 size;
165         struct ao_lisp_val      vals[];
166 };
167
168 struct ao_lisp_frame {
169         uint8_t                 type;
170         uint8_t                 num;
171         ao_poly                 prev;
172         ao_poly                 vals;
173 };
174
175 struct ao_lisp_bool {
176         uint8_t                 type;
177         uint8_t                 value;
178         uint16_t                pad;
179 };
180
181 struct ao_lisp_bigint {
182         uint32_t                value;
183 };
184
185 struct ao_lisp_float {
186         uint8_t                 type;
187         uint8_t                 pad1;
188         uint16_t                pad2;
189         float                   value;
190 };
191
192 #if __BYTE_ORDER == __LITTLE_ENDIAN
193 static inline uint32_t
194 ao_lisp_int_bigint(int32_t i) {
195         return AO_LISP_BIGINT | (i << 8);
196 }
197 static inline int32_t
198 ao_lisp_bigint_int(uint32_t bi) {
199         return (int32_t) bi >> 8;
200 }
201 #else
202 static inline uint32_t
203 ao_lisp_int_bigint(int32_t i) {
204         return (uint32_t) (i & 0xffffff) | (AO_LISP_BIGINT << 24);
205 }
206 static inlint int32_t
207 ao_lisp_bigint_int(uint32_t bi) {
208         return (int32_t) (bi << 8) >> 8;
209 }
210 #endif
211
212 #define AO_LISP_MIN_INT         (-(1 << (15 - AO_LISP_TYPE_SHIFT)))
213 #define AO_LISP_MAX_INT         ((1 << (15 - AO_LISP_TYPE_SHIFT)) - 1)
214 #define AO_LISP_MIN_BIGINT      (-(1 << 24))
215 #define AO_LISP_MAX_BIGINT      ((1 << 24) - 1)
216
217 #define AO_LISP_NOT_INTEGER     0x7fffffff
218
219 /* Set on type when the frame escapes the lambda */
220 #define AO_LISP_FRAME_MARK      0x80
221 #define AO_LISP_FRAME_PRINT     0x40
222
223 static inline int ao_lisp_frame_marked(struct ao_lisp_frame *f) {
224         return f->type & AO_LISP_FRAME_MARK;
225 }
226
227 static inline struct ao_lisp_frame *
228 ao_lisp_poly_frame(ao_poly poly) {
229         return ao_lisp_ref(poly);
230 }
231
232 static inline ao_poly
233 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
234         return ao_lisp_poly(frame, AO_LISP_OTHER);
235 }
236
237 static inline struct ao_lisp_frame_vals *
238 ao_lisp_poly_frame_vals(ao_poly poly) {
239         return ao_lisp_ref(poly);
240 }
241
242 static inline ao_poly
243 ao_lisp_frame_vals_poly(struct ao_lisp_frame_vals *vals) {
244         return ao_lisp_poly(vals, AO_LISP_OTHER);
245 }
246
247 enum eval_state {
248         eval_sexpr,             /* Evaluate an sexpr */
249         eval_val,               /* Value computed */
250         eval_formal,            /* Formal computed */
251         eval_exec,              /* Start a lambda evaluation */
252         eval_apply,             /* Execute apply */
253         eval_cond,              /* Start next cond clause */
254         eval_cond_test,         /* Check cond condition */
255         eval_begin,             /* Start next begin entry */
256         eval_while,             /* Start while condition */
257         eval_while_test,        /* Check while condition */
258         eval_macro,             /* Finished with macro generation */
259 };
260
261 struct ao_lisp_stack {
262         uint8_t                 type;           /* AO_LISP_STACK */
263         uint8_t                 state;          /* enum eval_state */
264         ao_poly                 prev;           /* previous stack frame */
265         ao_poly                 sexprs;         /* expressions to evaluate */
266         ao_poly                 values;         /* values computed */
267         ao_poly                 values_tail;    /* end of the values list for easy appending */
268         ao_poly                 frame;          /* current lookup frame */
269         ao_poly                 list;           /* most recent function call */
270 };
271
272 #define AO_LISP_STACK_MARK      0x80    /* set on type when a reference has been taken */
273 #define AO_LISP_STACK_PRINT     0x40    /* stack is being printed */
274
275 static inline int ao_lisp_stack_marked(struct ao_lisp_stack *s) {
276         return s->type & AO_LISP_STACK_MARK;
277 }
278
279 static inline void ao_lisp_stack_mark(struct ao_lisp_stack *s) {
280         s->type |= AO_LISP_STACK_MARK;
281 }
282
283 static inline struct ao_lisp_stack *
284 ao_lisp_poly_stack(ao_poly p)
285 {
286         return ao_lisp_ref(p);
287 }
288
289 static inline ao_poly
290 ao_lisp_stack_poly(struct ao_lisp_stack *stack)
291 {
292         return ao_lisp_poly(stack, AO_LISP_OTHER);
293 }
294
295 extern ao_poly                  ao_lisp_v;
296
297 #define AO_LISP_FUNC_LAMBDA     0
298 #define AO_LISP_FUNC_NLAMBDA    1
299 #define AO_LISP_FUNC_MACRO      2
300 #define AO_LISP_FUNC_LEXPR      3
301
302 #define AO_LISP_FUNC_FREE_ARGS  0x80
303 #define AO_LISP_FUNC_MASK       0x7f
304
305 #define AO_LISP_FUNC_F_LAMBDA   (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LAMBDA)
306 #define AO_LISP_FUNC_F_NLAMBDA  (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_NLAMBDA)
307 #define AO_LISP_FUNC_F_MACRO    (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_MACRO)
308 #define AO_LISP_FUNC_F_LEXPR    (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LEXPR)
309
310 struct ao_lisp_builtin {
311         uint8_t         type;
312         uint8_t         args;
313         uint16_t        func;
314 };
315
316 #define AO_LISP_BUILTIN_ID
317 #include "ao_lisp_builtin.h"
318
319 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
320
321 extern const ao_lisp_func_t     ao_lisp_builtins[];
322
323 static inline ao_lisp_func_t
324 ao_lisp_func(struct ao_lisp_builtin *b)
325 {
326         return ao_lisp_builtins[b->func];
327 }
328
329 struct ao_lisp_lambda {
330         uint8_t         type;
331         uint8_t         args;
332         ao_poly         code;
333         ao_poly         frame;
334 };
335
336 static inline struct ao_lisp_lambda *
337 ao_lisp_poly_lambda(ao_poly poly)
338 {
339         return ao_lisp_ref(poly);
340 }
341
342 static inline ao_poly
343 ao_lisp_lambda_poly(struct ao_lisp_lambda *lambda)
344 {
345         return ao_lisp_poly(lambda, AO_LISP_OTHER);
346 }
347
348 static inline void *
349 ao_lisp_poly_other(ao_poly poly) {
350         return ao_lisp_ref(poly);
351 }
352
353 static inline uint8_t
354 ao_lisp_other_type(void *other) {
355 #if DBG_MEM
356         if ((*((uint8_t *) other) & AO_LISP_OTHER_TYPE_MASK) >= AO_LISP_NUM_TYPE)
357                 ao_lisp_abort();
358 #endif
359         return *((uint8_t *) other) & AO_LISP_OTHER_TYPE_MASK;
360 }
361
362 static inline ao_poly
363 ao_lisp_other_poly(const void *other)
364 {
365         return ao_lisp_poly(other, AO_LISP_OTHER);
366 }
367
368 static inline int
369 ao_lisp_size_round(int size)
370 {
371         return (size + 3) & ~3;
372 }
373
374 static inline int
375 ao_lisp_size(const struct ao_lisp_type *type, void *addr)
376 {
377         return ao_lisp_size_round(type->size(addr));
378 }
379
380 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
381
382 static inline int ao_lisp_poly_base_type(ao_poly poly) {
383         return poly & AO_LISP_TYPE_MASK;
384 }
385
386 static inline int ao_lisp_poly_type(ao_poly poly) {
387         int     type = poly & AO_LISP_TYPE_MASK;
388         if (type == AO_LISP_OTHER)
389                 return ao_lisp_other_type(ao_lisp_poly_other(poly));
390         return type;
391 }
392
393 static inline int
394 ao_lisp_is_cons(ao_poly poly) {
395         return (ao_lisp_poly_base_type(poly) == AO_LISP_CONS);
396 }
397
398 static inline int
399 ao_lisp_is_pair(ao_poly poly) {
400         return poly != AO_LISP_NIL && (ao_lisp_poly_base_type(poly) == AO_LISP_CONS);
401 }
402
403 static inline struct ao_lisp_cons *
404 ao_lisp_poly_cons(ao_poly poly)
405 {
406         return ao_lisp_ref(poly);
407 }
408
409 static inline ao_poly
410 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
411 {
412         return ao_lisp_poly(cons, AO_LISP_CONS);
413 }
414
415 static inline int32_t
416 ao_lisp_poly_int(ao_poly poly)
417 {
418         return (int32_t) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
419 }
420
421 static inline ao_poly
422 ao_lisp_int_poly(int32_t i)
423 {
424         return ((ao_poly) i << 2) | AO_LISP_INT;
425 }
426
427 static inline struct ao_lisp_bigint *
428 ao_lisp_poly_bigint(ao_poly poly)
429 {
430         return ao_lisp_ref(poly);
431 }
432
433 static inline ao_poly
434 ao_lisp_bigint_poly(struct ao_lisp_bigint *bi)
435 {
436         return ao_lisp_poly(bi, AO_LISP_OTHER);
437 }
438
439 static inline char *
440 ao_lisp_poly_string(ao_poly poly)
441 {
442         return ao_lisp_ref(poly);
443 }
444
445 static inline ao_poly
446 ao_lisp_string_poly(char *s)
447 {
448         return ao_lisp_poly(s, AO_LISP_STRING);
449 }
450
451 static inline struct ao_lisp_atom *
452 ao_lisp_poly_atom(ao_poly poly)
453 {
454         return ao_lisp_ref(poly);
455 }
456
457 static inline ao_poly
458 ao_lisp_atom_poly(struct ao_lisp_atom *a)
459 {
460         return ao_lisp_poly(a, AO_LISP_OTHER);
461 }
462
463 static inline struct ao_lisp_builtin *
464 ao_lisp_poly_builtin(ao_poly poly)
465 {
466         return ao_lisp_ref(poly);
467 }
468
469 static inline ao_poly
470 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
471 {
472         return ao_lisp_poly(b, AO_LISP_OTHER);
473 }
474
475 static inline ao_poly
476 ao_lisp_bool_poly(struct ao_lisp_bool *b)
477 {
478         return ao_lisp_poly(b, AO_LISP_OTHER);
479 }
480
481 static inline struct ao_lisp_bool *
482 ao_lisp_poly_bool(ao_poly poly)
483 {
484         return ao_lisp_ref(poly);
485 }
486
487 static inline ao_poly
488 ao_lisp_float_poly(struct ao_lisp_float *f)
489 {
490         return ao_lisp_poly(f, AO_LISP_OTHER);
491 }
492
493 static inline struct ao_lisp_float *
494 ao_lisp_poly_float(ao_poly poly)
495 {
496         return ao_lisp_ref(poly);
497 }
498
499 float
500 ao_lisp_poly_number(ao_poly p);
501
502 /* memory functions */
503
504 extern int ao_lisp_collects[2];
505 extern int ao_lisp_freed[2];
506 extern int ao_lisp_loops[2];
507
508 /* returns 1 if the object was already marked */
509 int
510 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
511
512 /* returns 1 if the object was already marked */
513 int
514 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr);
515
516 void *
517 ao_lisp_move_map(void *addr);
518
519 /* returns 1 if the object was already moved */
520 int
521 ao_lisp_move(const struct ao_lisp_type *type, void **ref);
522
523 /* returns 1 if the object was already moved */
524 int
525 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref);
526
527 void *
528 ao_lisp_alloc(int size);
529
530 #define AO_LISP_COLLECT_FULL            1
531 #define AO_LISP_COLLECT_INCREMENTAL     0
532
533 int
534 ao_lisp_collect(uint8_t style);
535
536 #if DBG_FREE_CONS
537 void
538 ao_lisp_cons_check(struct ao_lisp_cons *cons);
539 #endif
540
541 void
542 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons);
543
544 struct ao_lisp_cons *
545 ao_lisp_cons_fetch(int id);
546
547 void
548 ao_lisp_poly_stash(int id, ao_poly poly);
549
550 ao_poly
551 ao_lisp_poly_fetch(int id);
552
553 void
554 ao_lisp_string_stash(int id, char *string);
555
556 char *
557 ao_lisp_string_fetch(int id);
558
559 static inline void
560 ao_lisp_stack_stash(int id, struct ao_lisp_stack *stack) {
561         ao_lisp_poly_stash(id, ao_lisp_stack_poly(stack));
562 }
563
564 static inline struct ao_lisp_stack *
565 ao_lisp_stack_fetch(int id) {
566         return ao_lisp_poly_stack(ao_lisp_poly_fetch(id));
567 }
568
569 void
570 ao_lisp_frame_stash(int id, struct ao_lisp_frame *frame);
571
572 struct ao_lisp_frame *
573 ao_lisp_frame_fetch(int id);
574
575 /* bool */
576
577 extern const struct ao_lisp_type ao_lisp_bool_type;
578
579 void
580 ao_lisp_bool_write(ao_poly v);
581
582 #ifdef AO_LISP_MAKE_CONST
583 struct ao_lisp_bool     *ao_lisp_true, *ao_lisp_false;
584
585 struct ao_lisp_bool *
586 ao_lisp_bool_get(uint8_t value);
587 #endif
588
589 /* cons */
590 extern const struct ao_lisp_type ao_lisp_cons_type;
591
592 struct ao_lisp_cons *
593 ao_lisp_cons_cons(ao_poly car, ao_poly cdr);
594
595 /* Return a cons or NULL for a proper list, else error */
596 struct ao_lisp_cons *
597 ao_lisp_cons_cdr(struct ao_lisp_cons *cons);
598
599 ao_poly
600 ao_lisp__cons(ao_poly car, ao_poly cdr);
601
602 extern struct ao_lisp_cons *ao_lisp_cons_free_list;
603
604 void
605 ao_lisp_cons_free(struct ao_lisp_cons *cons);
606
607 void
608 ao_lisp_cons_write(ao_poly);
609
610 void
611 ao_lisp_cons_display(ao_poly);
612
613 int
614 ao_lisp_cons_length(struct ao_lisp_cons *cons);
615
616 /* string */
617 extern const struct ao_lisp_type ao_lisp_string_type;
618
619 char *
620 ao_lisp_string_copy(char *a);
621
622 char *
623 ao_lisp_string_cat(char *a, char *b);
624
625 ao_poly
626 ao_lisp_string_pack(struct ao_lisp_cons *cons);
627
628 ao_poly
629 ao_lisp_string_unpack(char *a);
630
631 void
632 ao_lisp_string_write(ao_poly s);
633
634 void
635 ao_lisp_string_display(ao_poly s);
636
637 /* atom */
638 extern const struct ao_lisp_type ao_lisp_atom_type;
639
640 extern struct ao_lisp_atom      *ao_lisp_atoms;
641 extern struct ao_lisp_frame     *ao_lisp_frame_global;
642 extern struct ao_lisp_frame     *ao_lisp_frame_current;
643
644 void
645 ao_lisp_atom_write(ao_poly a);
646
647 struct ao_lisp_atom *
648 ao_lisp_atom_intern(char *name);
649
650 ao_poly *
651 ao_lisp_atom_ref(ao_poly atom);
652
653 ao_poly
654 ao_lisp_atom_get(ao_poly atom);
655
656 ao_poly
657 ao_lisp_atom_set(ao_poly atom, ao_poly val);
658
659 ao_poly
660 ao_lisp_atom_def(ao_poly atom, ao_poly val);
661
662 /* int */
663 void
664 ao_lisp_int_write(ao_poly i);
665
666 int32_t
667 ao_lisp_poly_integer(ao_poly p);
668
669 ao_poly
670 ao_lisp_integer_poly(int32_t i);
671
672 static inline int
673 ao_lisp_integer_typep(uint8_t t)
674 {
675         return (t == AO_LISP_INT) || (t == AO_LISP_BIGINT);
676 }
677
678 void
679 ao_lisp_bigint_write(ao_poly i);
680
681 extern const struct ao_lisp_type        ao_lisp_bigint_type;
682 /* prim */
683 void
684 ao_lisp_poly_write(ao_poly p);
685
686 void
687 ao_lisp_poly_display(ao_poly p);
688
689 int
690 ao_lisp_poly_mark(ao_poly p, uint8_t note_cons);
691
692 /* returns 1 if the object has already been moved */
693 int
694 ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
695
696 /* eval */
697
698 void
699 ao_lisp_eval_clear_globals(void);
700
701 int
702 ao_lisp_eval_restart(void);
703
704 ao_poly
705 ao_lisp_eval(ao_poly p);
706
707 ao_poly
708 ao_lisp_set_cond(struct ao_lisp_cons *cons);
709
710 /* float */
711 extern const struct ao_lisp_type ao_lisp_float_type;
712
713 void
714 ao_lisp_float_write(ao_poly p);
715
716 ao_poly
717 ao_lisp_float_get(float value);
718
719 static inline uint8_t
720 ao_lisp_number_typep(uint8_t t)
721 {
722         return ao_lisp_integer_typep(t) || (t == AO_LISP_FLOAT);
723 }
724
725 float
726 ao_lisp_poly_number(ao_poly p);
727
728 /* builtin */
729 void
730 ao_lisp_builtin_write(ao_poly b);
731
732 extern const struct ao_lisp_type ao_lisp_builtin_type;
733
734 /* Check argument count */
735 ao_poly
736 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
737
738 /* Check argument type */
739 ao_poly
740 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
741
742 /* Fetch an arg (nil if off the end) */
743 ao_poly
744 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
745
746 char *
747 ao_lisp_args_name(uint8_t args);
748
749 /* read */
750 extern struct ao_lisp_cons      *ao_lisp_read_cons;
751 extern struct ao_lisp_cons      *ao_lisp_read_cons_tail;
752 extern struct ao_lisp_cons      *ao_lisp_read_stack;
753
754 ao_poly
755 ao_lisp_read(void);
756
757 /* rep */
758 ao_poly
759 ao_lisp_read_eval_print(void);
760
761 /* frame */
762 extern const struct ao_lisp_type ao_lisp_frame_type;
763 extern const struct ao_lisp_type ao_lisp_frame_vals_type;
764
765 #define AO_LISP_FRAME_FREE      6
766
767 extern struct ao_lisp_frame     *ao_lisp_frame_free_list[AO_LISP_FRAME_FREE];
768
769 ao_poly
770 ao_lisp_frame_mark(struct ao_lisp_frame *frame);
771
772 ao_poly *
773 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
774
775 struct ao_lisp_frame *
776 ao_lisp_frame_new(int num);
777
778 void
779 ao_lisp_frame_free(struct ao_lisp_frame *frame);
780
781 void
782 ao_lisp_frame_bind(struct ao_lisp_frame *frame, int num, ao_poly atom, ao_poly val);
783
784 ao_poly
785 ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val);
786
787 void
788 ao_lisp_frame_write(ao_poly p);
789
790 void
791 ao_lisp_frame_init(void);
792
793 /* lambda */
794 extern const struct ao_lisp_type ao_lisp_lambda_type;
795
796 extern const char *ao_lisp_state_names[];
797
798 struct ao_lisp_lambda *
799 ao_lisp_lambda_new(ao_poly cons);
800
801 void
802 ao_lisp_lambda_write(ao_poly lambda);
803
804 ao_poly
805 ao_lisp_lambda_eval(void);
806
807 /* stack */
808
809 extern const struct ao_lisp_type ao_lisp_stack_type;
810 extern struct ao_lisp_stack     *ao_lisp_stack;
811 extern struct ao_lisp_stack     *ao_lisp_stack_free_list;
812
813 void
814 ao_lisp_stack_reset(struct ao_lisp_stack *stack);
815
816 int
817 ao_lisp_stack_push(void);
818
819 void
820 ao_lisp_stack_pop(void);
821
822 void
823 ao_lisp_stack_clear(void);
824
825 void
826 ao_lisp_stack_write(ao_poly stack);
827
828 ao_poly
829 ao_lisp_stack_eval(void);
830
831 /* error */
832
833 void
834 ao_lisp_vprintf(char *format, va_list args);
835
836 void
837 ao_lisp_printf(char *format, ...);
838
839 void
840 ao_lisp_error_poly(char *name, ao_poly poly, ao_poly last);
841
842 void
843 ao_lisp_error_frame(int indent, char *name, struct ao_lisp_frame *frame);
844
845 ao_poly
846 ao_lisp_error(int error, char *format, ...);
847
848 /* builtins */
849
850 #define AO_LISP_BUILTIN_DECLS
851 #include "ao_lisp_builtin.h"
852
853 /* debugging macros */
854
855 #if DBG_EVAL || DBG_READ || DBG_MEM
856 #define DBG_CODE        1
857 int ao_lisp_stack_depth;
858 #define DBG_DO(a)       a
859 #define DBG_INDENT()    do { int _s; for(_s = 0; _s < ao_lisp_stack_depth; _s++) printf("  "); } while(0)
860 #define DBG_IN()        (++ao_lisp_stack_depth)
861 #define DBG_OUT()       (--ao_lisp_stack_depth)
862 #define DBG_RESET()     (ao_lisp_stack_depth = 0)
863 #define DBG(...)        ao_lisp_printf(__VA_ARGS__)
864 #define DBGI(...)       do { printf("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
865 #define DBG_CONS(a)     ao_lisp_cons_write(ao_lisp_cons_poly(a))
866 #define DBG_POLY(a)     ao_lisp_poly_write(a)
867 #define OFFSET(a)       ((a) ? (int) ((uint8_t *) a - ao_lisp_pool) : -1)
868 #define DBG_STACK()     ao_lisp_stack_write(ao_lisp_stack_poly(ao_lisp_stack))
869 static inline void
870 ao_lisp_frames_dump(void)
871 {
872         struct ao_lisp_stack *s;
873         DBGI(".. current frame: "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
874         for (s = ao_lisp_stack; s; s = ao_lisp_poly_stack(s->prev)) {
875                 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
876         }
877 }
878 #define DBG_FRAMES()    ao_lisp_frames_dump()
879 #else
880 #define DBG_DO(a)
881 #define DBG_INDENT()
882 #define DBG_IN()
883 #define DBG_OUT()
884 #define DBG(...)
885 #define DBGI(...)
886 #define DBG_CONS(a)
887 #define DBG_POLY(a)
888 #define DBG_RESET()
889 #define DBG_STACK()
890 #define DBG_FRAMES()
891 #endif
892
893 #if DBG_READ
894 #define RDBGI(...)      DBGI(__VA_ARGS__)
895 #define RDBG_IN()       DBG_IN()
896 #define RDBG_OUT()      DBG_OUT()
897 #else
898 #define RDBGI(...)
899 #define RDBG_IN()
900 #define RDBG_OUT()
901 #endif
902
903 #define DBG_MEM_START   1
904
905 #if DBG_MEM
906
907 #include <assert.h>
908 extern int dbg_move_depth;
909 #define MDBG_DUMP 1
910 #define MDBG_OFFSET(a)  ((a) ? (int) ((uint8_t *) (a) - ao_lisp_pool) : -1)
911
912 extern int dbg_mem;
913
914 #define MDBG_DO(a)      DBG_DO(a)
915 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf ("  "); printf(__VA_ARGS__); } } while (0)
916 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
917 #define MDBG_MOVE_IN()  (dbg_move_depth++)
918 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
919
920 #else
921
922 #define MDBG_DO(a)
923 #define MDBG_MOVE(...)
924 #define MDBG_MORE(...)
925 #define MDBG_MOVE_IN()
926 #define MDBG_MOVE_OUT()
927
928 #endif
929
930 #endif /* _AO_LISP_H_ */