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