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