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