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