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