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