2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
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.
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.
24 #include <ao_lisp_os.h>
26 typedef uint16_t ao_poly;
27 typedef int16_t ao_signed_poly;
31 struct ao_lisp_os_save {
34 uint16_t const_checksum;
35 uint16_t const_checksum_inv;
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))
42 ao_lisp_os_save(void);
45 ao_lisp_os_restore_save(struct ao_lisp_os_save *save, int offset);
48 ao_lisp_os_restore(void);
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
58 #define _atom(n) ao_lisp_atom_poly(ao_lisp_atom_intern(n))
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")
83 #include "ao_lisp_const.h"
85 #define AO_LISP_POOL 3072
87 extern uint8_t ao_lisp_pool[AO_LISP_POOL + AO_LISP_POOL_EXTRA];
91 #define AO_LISP_CONS 0
93 #define AO_LISP_STRING 2
94 #define AO_LISP_OTHER 3
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
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
109 /* Leave two bits for types to use as they please */
110 #define AO_LISP_OTHER_TYPE_MASK 0x3f
112 #define AO_LISP_NIL 0
114 extern uint16_t ao_lisp_top;
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
122 extern uint8_t ao_lisp_exception;
125 ao_lisp_is_const(ao_poly poly) {
126 return poly & AO_LISP_CONST;
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 #define AO_LISP_IS_INT(p) (ao_lisp_base_type(p) == AO_LISP_INT);
134 ao_lisp_ref(ao_poly poly);
137 ao_lisp_poly(const void *addr, ao_poly type);
139 struct ao_lisp_type {
140 int (*size)(void *addr);
141 void (*mark)(void *addr);
142 void (*move)(void *addr);
146 struct ao_lisp_cons {
151 struct ao_lisp_atom {
163 struct ao_lisp_frame {
167 struct ao_lisp_val vals[];
170 /* Set on type when the frame escapes the lambda */
171 #define AO_LISP_FRAME_MARK 0x80
172 #define AO_LISP_FRAME_PRINT 0x40
174 static inline int ao_lisp_frame_marked(struct ao_lisp_frame *f) {
175 return f->type & AO_LISP_FRAME_MARK;
178 static inline struct ao_lisp_frame *
179 ao_lisp_poly_frame(ao_poly poly) {
180 return ao_lisp_ref(poly);
183 static inline ao_poly
184 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
185 return ao_lisp_poly(frame, AO_LISP_OTHER);
189 eval_sexpr, /* Evaluate an sexpr */
190 eval_val, /* Value computed */
191 eval_formal, /* Formal computed */
192 eval_exec, /* Start a lambda evaluation */
193 eval_cond, /* Start next cond clause */
194 eval_cond_test, /* Check cond condition */
195 eval_progn, /* Start next progn entry */
196 eval_while, /* Start while condition */
197 eval_while_test, /* Check while condition */
198 eval_macro, /* Finished with macro generation */
201 struct ao_lisp_stack {
202 uint8_t type; /* AO_LISP_STACK */
203 uint8_t state; /* enum eval_state */
204 ao_poly prev; /* previous stack frame */
205 ao_poly sexprs; /* expressions to evaluate */
206 ao_poly values; /* values computed */
207 ao_poly values_tail; /* end of the values list for easy appending */
208 ao_poly frame; /* current lookup frame */
209 ao_poly list; /* most recent function call */
212 #define AO_LISP_STACK_MARK 0x80 /* set on type when a reference has been taken */
213 #define AO_LISP_STACK_PRINT 0x40 /* stack is being printed */
215 static inline int ao_lisp_stack_marked(struct ao_lisp_stack *s) {
216 return s->type & AO_LISP_STACK_MARK;
219 static inline void ao_lisp_stack_mark(struct ao_lisp_stack *s) {
220 s->type |= AO_LISP_STACK_MARK;
223 static inline struct ao_lisp_stack *
224 ao_lisp_poly_stack(ao_poly p)
226 return ao_lisp_ref(p);
229 static inline ao_poly
230 ao_lisp_stack_poly(struct ao_lisp_stack *stack)
232 return ao_lisp_poly(stack, AO_LISP_OTHER);
235 extern ao_poly ao_lisp_v;
237 #define AO_LISP_FUNC_LAMBDA 0
238 #define AO_LISP_FUNC_NLAMBDA 1
239 #define AO_LISP_FUNC_MACRO 2
240 #define AO_LISP_FUNC_LEXPR 3
242 #define AO_LISP_FUNC_FREE_ARGS 0x80
243 #define AO_LISP_FUNC_MASK 0x7f
245 #define AO_LISP_FUNC_F_LAMBDA (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LAMBDA)
246 #define AO_LISP_FUNC_F_NLAMBDA (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_NLAMBDA)
247 #define AO_LISP_FUNC_F_MACRO (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_MACRO)
248 #define AO_LISP_FUNC_F_LEXPR (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LEXPR)
250 struct ao_lisp_builtin {
256 enum ao_lisp_builtin_id {
285 builtin_greater_equal,
297 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
299 extern const ao_lisp_func_t ao_lisp_builtins[];
301 static inline ao_lisp_func_t
302 ao_lisp_func(struct ao_lisp_builtin *b)
304 return ao_lisp_builtins[b->func];
307 struct ao_lisp_lambda {
314 static inline struct ao_lisp_lambda *
315 ao_lisp_poly_lambda(ao_poly poly)
317 return ao_lisp_ref(poly);
320 static inline ao_poly
321 ao_lisp_lambda_poly(struct ao_lisp_lambda *lambda)
323 return ao_lisp_poly(lambda, AO_LISP_OTHER);
327 ao_lisp_poly_other(ao_poly poly) {
328 return ao_lisp_ref(poly);
331 static inline uint8_t
332 ao_lisp_other_type(void *other) {
334 if ((*((uint8_t *) other) & AO_LISP_OTHER_TYPE_MASK) >= AO_LISP_NUM_TYPE)
337 return *((uint8_t *) other) & AO_LISP_OTHER_TYPE_MASK;
340 static inline ao_poly
341 ao_lisp_other_poly(const void *other)
343 return ao_lisp_poly(other, AO_LISP_OTHER);
347 ao_lisp_size_round(int size)
349 return (size + 3) & ~3;
353 ao_lisp_size(const struct ao_lisp_type *type, void *addr)
355 return ao_lisp_size_round(type->size(addr));
358 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
360 static inline int ao_lisp_poly_base_type(ao_poly poly) {
361 return poly & AO_LISP_TYPE_MASK;
364 static inline int ao_lisp_poly_type(ao_poly poly) {
365 int type = poly & AO_LISP_TYPE_MASK;
366 if (type == AO_LISP_OTHER)
367 return ao_lisp_other_type(ao_lisp_poly_other(poly));
371 static inline struct ao_lisp_cons *
372 ao_lisp_poly_cons(ao_poly poly)
374 return ao_lisp_ref(poly);
377 static inline ao_poly
378 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
380 return ao_lisp_poly(cons, AO_LISP_CONS);
384 ao_lisp_poly_int(ao_poly poly)
386 return (int) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
389 static inline ao_poly
390 ao_lisp_int_poly(int i)
392 return ((ao_poly) i << 2) | AO_LISP_INT;
396 ao_lisp_poly_string(ao_poly poly)
398 return ao_lisp_ref(poly);
401 static inline ao_poly
402 ao_lisp_string_poly(char *s)
404 return ao_lisp_poly(s, AO_LISP_STRING);
407 static inline struct ao_lisp_atom *
408 ao_lisp_poly_atom(ao_poly poly)
410 return ao_lisp_ref(poly);
413 static inline ao_poly
414 ao_lisp_atom_poly(struct ao_lisp_atom *a)
416 return ao_lisp_poly(a, AO_LISP_OTHER);
419 static inline struct ao_lisp_builtin *
420 ao_lisp_poly_builtin(ao_poly poly)
422 return ao_lisp_ref(poly);
425 static inline ao_poly
426 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
428 return ao_lisp_poly(b, AO_LISP_OTHER);
431 /* memory functions */
433 extern int ao_lisp_collects[2];
434 extern int ao_lisp_freed[2];
435 extern int ao_lisp_loops[2];
437 /* returns 1 if the object was already marked */
439 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
441 /* returns 1 if the object was already marked */
443 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr);
446 ao_lisp_move_map(void *addr);
448 /* returns 1 if the object was already moved */
450 ao_lisp_move(const struct ao_lisp_type *type, void **ref);
452 /* returns 1 if the object was already moved */
454 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref);
457 ao_lisp_alloc(int size);
459 #define AO_LISP_COLLECT_FULL 1
460 #define AO_LISP_COLLECT_INCREMENTAL 0
463 ao_lisp_collect(uint8_t style);
466 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons);
468 struct ao_lisp_cons *
469 ao_lisp_cons_fetch(int id);
472 ao_lisp_poly_stash(int id, ao_poly poly);
475 ao_lisp_poly_fetch(int id);
478 ao_lisp_string_stash(int id, char *string);
481 ao_lisp_string_fetch(int id);
484 ao_lisp_stack_stash(int id, struct ao_lisp_stack *stack) {
485 ao_lisp_poly_stash(id, ao_lisp_stack_poly(stack));
488 static inline struct ao_lisp_stack *
489 ao_lisp_stack_fetch(int id) {
490 return ao_lisp_poly_stack(ao_lisp_poly_fetch(id));
494 extern const struct ao_lisp_type ao_lisp_cons_type;
496 struct ao_lisp_cons *
497 ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr);
499 extern struct ao_lisp_cons *ao_lisp_cons_free_list;
502 ao_lisp_cons_free(struct ao_lisp_cons *cons);
505 ao_lisp_cons_print(ao_poly);
508 ao_lisp_cons_patom(ao_poly);
511 ao_lisp_cons_length(struct ao_lisp_cons *cons);
514 extern const struct ao_lisp_type ao_lisp_string_type;
517 ao_lisp_string_copy(char *a);
520 ao_lisp_string_cat(char *a, char *b);
523 ao_lisp_string_pack(struct ao_lisp_cons *cons);
526 ao_lisp_string_unpack(char *a);
529 ao_lisp_string_print(ao_poly s);
532 ao_lisp_string_patom(ao_poly s);
535 extern const struct ao_lisp_type ao_lisp_atom_type;
537 extern struct ao_lisp_atom *ao_lisp_atoms;
538 extern struct ao_lisp_frame *ao_lisp_frame_global;
539 extern struct ao_lisp_frame *ao_lisp_frame_current;
542 ao_lisp_atom_print(ao_poly a);
544 struct ao_lisp_atom *
545 ao_lisp_atom_intern(char *name);
548 ao_lisp_atom_ref(struct ao_lisp_frame *frame, ao_poly atom);
551 ao_lisp_atom_get(ao_poly atom);
554 ao_lisp_atom_set(ao_poly atom, ao_poly val);
558 ao_lisp_int_print(ao_poly i);
562 ao_lisp_poly_print(ao_poly p);
565 ao_lisp_poly_patom(ao_poly p);
568 ao_lisp_poly_mark(ao_poly p, uint8_t note_cons);
570 /* returns 1 if the object has already been moved */
572 ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
577 ao_lisp_eval_clear_globals(void);
580 ao_lisp_eval_restart(void);
583 ao_lisp_eval(ao_poly p);
586 ao_lisp_set_cond(struct ao_lisp_cons *cons);
590 ao_lisp_builtin_print(ao_poly b);
592 extern const struct ao_lisp_type ao_lisp_builtin_type;
594 /* Check argument count */
596 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
598 /* Check argument type */
600 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
602 /* Fetch an arg (nil if off the end) */
604 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
607 ao_lisp_args_name(uint8_t args);
610 extern struct ao_lisp_cons *ao_lisp_read_cons;
611 extern struct ao_lisp_cons *ao_lisp_read_cons_tail;
612 extern struct ao_lisp_cons *ao_lisp_read_stack;
619 ao_lisp_read_eval_print(void);
622 extern const struct ao_lisp_type ao_lisp_frame_type;
624 #define AO_LISP_FRAME_FREE 6
626 extern struct ao_lisp_frame *ao_lisp_frame_free_list[AO_LISP_FRAME_FREE];
629 ao_lisp_frame_mark(struct ao_lisp_frame *frame);
632 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
634 struct ao_lisp_frame *
635 ao_lisp_frame_new(int num);
638 ao_lisp_frame_free(struct ao_lisp_frame *frame);
641 ao_lisp_frame_bind(struct ao_lisp_frame *frame, int num, ao_poly atom, ao_poly val);
644 ao_lisp_frame_add(struct ao_lisp_frame **frame, ao_poly atom, ao_poly val);
647 ao_lisp_frame_print(ao_poly p);
650 extern const struct ao_lisp_type ao_lisp_lambda_type;
652 extern const char *ao_lisp_state_names[];
654 struct ao_lisp_lambda *
655 ao_lisp_lambda_new(ao_poly cons);
658 ao_lisp_lambda_print(ao_poly lambda);
661 ao_lisp_lambda(struct ao_lisp_cons *cons);
664 ao_lisp_lexpr(struct ao_lisp_cons *cons);
667 ao_lisp_nlambda(struct ao_lisp_cons *cons);
670 ao_lisp_macro(struct ao_lisp_cons *cons);
673 ao_lisp_lambda_eval(void);
678 ao_lisp_save(struct ao_lisp_cons *cons);
681 ao_lisp_restore(struct ao_lisp_cons *cons);
685 extern const struct ao_lisp_type ao_lisp_stack_type;
686 extern struct ao_lisp_stack *ao_lisp_stack;
687 extern struct ao_lisp_stack *ao_lisp_stack_free_list;
690 ao_lisp_stack_reset(struct ao_lisp_stack *stack);
693 ao_lisp_stack_push(void);
696 ao_lisp_stack_pop(void);
699 ao_lisp_stack_clear(void);
702 ao_lisp_stack_print(ao_poly stack);
705 ao_lisp_stack_eval(void);
708 ao_lisp_call_cc(struct ao_lisp_cons *cons);
713 ao_lisp_error_poly(char *name, ao_poly poly, ao_poly last);
716 ao_lisp_error_frame(int indent, char *name, struct ao_lisp_frame *frame);
719 ao_lisp_error(int error, char *format, ...);
721 /* debugging macros */
725 int ao_lisp_stack_depth;
727 #define DBG_INDENT() do { int _s; for(_s = 0; _s < ao_lisp_stack_depth; _s++) printf(" "); } while(0)
728 #define DBG_IN() (++ao_lisp_stack_depth)
729 #define DBG_OUT() (--ao_lisp_stack_depth)
730 #define DBG_RESET() (ao_lisp_stack_depth = 0)
731 #define DBG(...) printf(__VA_ARGS__)
732 #define DBGI(...) do { DBG("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
733 #define DBG_CONS(a) ao_lisp_cons_print(ao_lisp_cons_poly(a))
734 #define DBG_POLY(a) ao_lisp_poly_print(a)
735 #define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_lisp_pool) : -1)
736 #define DBG_STACK() ao_lisp_stack_print()
738 ao_lisp_frames_dump(void)
740 struct ao_lisp_stack *s;
741 DBGI(".. current frame: "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
742 for (s = ao_lisp_stack; s; s = ao_lisp_poly_stack(s->prev)) {
743 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
746 #define DBG_FRAMES() ao_lisp_frames_dump()
761 #define DBG_MEM_START 1
766 extern int dbg_move_depth;
768 #define MDBG_OFFSET(a) ((int) ((uint8_t *) (a) - ao_lisp_pool))
773 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
774 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
775 #define MDBG_MOVE_IN() (dbg_move_depth++)
776 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
781 #define MDBG_MOVE(...)
782 #define MDBG_MORE(...)
783 #define MDBG_MOVE_IN()
784 #define MDBG_MOVE_OUT()
788 #endif /* _AO_LISP_H_ */