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