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