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.
23 #include <ao_lisp_os.h>
28 typedef uint16_t ao_poly;
29 typedef int16_t ao_signed_poly;
33 struct ao_lisp_os_save {
36 uint16_t const_checksum;
37 uint16_t const_checksum_inv;
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))
44 ao_lisp_os_save(void);
47 ao_lisp_os_restore_save(struct ao_lisp_os_save *save, int offset);
50 ao_lisp_os_restore(void);
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
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))
63 #define _ao_lisp_bool_true _bool(1)
64 #define _ao_lisp_bool_false _bool(0)
66 #define _ao_lisp_atom_eof _atom("eof")
67 #define _ao_lisp_atom_else _atom("else")
69 #define AO_LISP_BUILTIN_ATOMS
70 #include "ao_lisp_builtin.h"
73 #include "ao_lisp_const.h"
75 #define AO_LISP_POOL 3072
77 extern uint8_t ao_lisp_pool[AO_LISP_POOL + AO_LISP_POOL_EXTRA] __attribute__((aligned(4)));
81 #define AO_LISP_CONS 0
83 #define AO_LISP_STRING 2
84 #define AO_LISP_OTHER 3
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
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
101 /* Leave two bits for types to use as they please */
102 #define AO_LISP_OTHER_TYPE_MASK 0x3f
104 #define AO_LISP_NIL 0
106 extern uint16_t ao_lisp_top;
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
115 extern uint8_t ao_lisp_exception;
118 ao_lisp_is_const(ao_poly poly) {
119 return poly & AO_LISP_CONST;
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)
127 ao_lisp_ref(ao_poly poly);
130 ao_lisp_poly(const void *addr, ao_poly type);
132 struct ao_lisp_type {
133 int (*size)(void *addr);
134 void (*mark)(void *addr);
135 void (*move)(void *addr);
139 struct ao_lisp_cons {
144 struct ao_lisp_atom {
156 struct ao_lisp_frame {
160 struct ao_lisp_val vals[];
163 struct ao_lisp_bool {
169 struct ao_lisp_bigint {
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);
178 static inline int32_t
179 ao_lisp_bigint_int(uint32_t bi) {
180 return (int32_t) bi >> 8;
183 static inline uint32_t
184 ao_lisp_int_bigint(int32_t i) {
185 return (uint32_t) (i & 0xffffff) | (AO_LISP_BIGINT << 24);
187 static inlint int32_t
188 ao_lisp_bigint_int(uint32_t bi) {
189 return (int32_t) (bi << 8) >> 8;
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)
196 #define AO_LISP_NOT_INTEGER 0x7fffffff
198 /* Set on type when the frame escapes the lambda */
199 #define AO_LISP_FRAME_MARK 0x80
200 #define AO_LISP_FRAME_PRINT 0x40
202 static inline int ao_lisp_frame_marked(struct ao_lisp_frame *f) {
203 return f->type & AO_LISP_FRAME_MARK;
206 static inline struct ao_lisp_frame *
207 ao_lisp_poly_frame(ao_poly poly) {
208 return ao_lisp_ref(poly);
211 static inline ao_poly
212 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
213 return ao_lisp_poly(frame, AO_LISP_OTHER);
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 */
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 */
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 */
244 static inline int ao_lisp_stack_marked(struct ao_lisp_stack *s) {
245 return s->type & AO_LISP_STACK_MARK;
248 static inline void ao_lisp_stack_mark(struct ao_lisp_stack *s) {
249 s->type |= AO_LISP_STACK_MARK;
252 static inline struct ao_lisp_stack *
253 ao_lisp_poly_stack(ao_poly p)
255 return ao_lisp_ref(p);
258 static inline ao_poly
259 ao_lisp_stack_poly(struct ao_lisp_stack *stack)
261 return ao_lisp_poly(stack, AO_LISP_OTHER);
264 extern ao_poly ao_lisp_v;
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
271 #define AO_LISP_FUNC_FREE_ARGS 0x80
272 #define AO_LISP_FUNC_MASK 0x7f
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)
279 struct ao_lisp_builtin {
285 #define AO_LISP_BUILTIN_ID
286 #include "ao_lisp_builtin.h"
288 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
290 extern const ao_lisp_func_t ao_lisp_builtins[];
292 static inline ao_lisp_func_t
293 ao_lisp_func(struct ao_lisp_builtin *b)
295 return ao_lisp_builtins[b->func];
298 struct ao_lisp_lambda {
305 static inline struct ao_lisp_lambda *
306 ao_lisp_poly_lambda(ao_poly poly)
308 return ao_lisp_ref(poly);
311 static inline ao_poly
312 ao_lisp_lambda_poly(struct ao_lisp_lambda *lambda)
314 return ao_lisp_poly(lambda, AO_LISP_OTHER);
318 ao_lisp_poly_other(ao_poly poly) {
319 return ao_lisp_ref(poly);
322 static inline uint8_t
323 ao_lisp_other_type(void *other) {
325 if ((*((uint8_t *) other) & AO_LISP_OTHER_TYPE_MASK) >= AO_LISP_NUM_TYPE)
328 return *((uint8_t *) other) & AO_LISP_OTHER_TYPE_MASK;
331 static inline ao_poly
332 ao_lisp_other_poly(const void *other)
334 return ao_lisp_poly(other, AO_LISP_OTHER);
338 ao_lisp_size_round(int size)
340 return (size + 3) & ~3;
344 ao_lisp_size(const struct ao_lisp_type *type, void *addr)
346 return ao_lisp_size_round(type->size(addr));
349 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
351 static inline int ao_lisp_poly_base_type(ao_poly poly) {
352 return poly & AO_LISP_TYPE_MASK;
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));
362 static inline struct ao_lisp_cons *
363 ao_lisp_poly_cons(ao_poly poly)
365 return ao_lisp_ref(poly);
368 static inline ao_poly
369 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
371 return ao_lisp_poly(cons, AO_LISP_CONS);
374 static inline int32_t
375 ao_lisp_poly_int(ao_poly poly)
377 return (int32_t) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
380 static inline ao_poly
381 ao_lisp_int_poly(int32_t i)
383 return ((ao_poly) i << 2) | AO_LISP_INT;
386 static inline struct ao_lisp_bigint *
387 ao_lisp_poly_bigint(ao_poly poly)
389 return ao_lisp_ref(poly);
392 static inline ao_poly
393 ao_lisp_bigint_poly(struct ao_lisp_bigint *bi)
395 return ao_lisp_poly(bi, AO_LISP_OTHER);
399 ao_lisp_poly_string(ao_poly poly)
401 return ao_lisp_ref(poly);
404 static inline ao_poly
405 ao_lisp_string_poly(char *s)
407 return ao_lisp_poly(s, AO_LISP_STRING);
410 static inline struct ao_lisp_atom *
411 ao_lisp_poly_atom(ao_poly poly)
413 return ao_lisp_ref(poly);
416 static inline ao_poly
417 ao_lisp_atom_poly(struct ao_lisp_atom *a)
419 return ao_lisp_poly(a, AO_LISP_OTHER);
422 static inline struct ao_lisp_builtin *
423 ao_lisp_poly_builtin(ao_poly poly)
425 return ao_lisp_ref(poly);
428 static inline ao_poly
429 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
431 return ao_lisp_poly(b, AO_LISP_OTHER);
434 static inline ao_poly
435 ao_lisp_bool_poly(struct ao_lisp_bool *b)
437 return ao_lisp_poly(b, AO_LISP_OTHER);
440 static inline struct ao_lisp_bool *
441 ao_lisp_poly_bool(ao_poly poly)
443 return ao_lisp_ref(poly);
445 /* memory functions */
447 extern int ao_lisp_collects[2];
448 extern int ao_lisp_freed[2];
449 extern int ao_lisp_loops[2];
451 /* returns 1 if the object was already marked */
453 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
455 /* returns 1 if the object was already marked */
457 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr);
460 ao_lisp_move_map(void *addr);
462 /* returns 1 if the object was already moved */
464 ao_lisp_move(const struct ao_lisp_type *type, void **ref);
466 /* returns 1 if the object was already moved */
468 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref);
471 ao_lisp_alloc(int size);
473 #define AO_LISP_COLLECT_FULL 1
474 #define AO_LISP_COLLECT_INCREMENTAL 0
477 ao_lisp_collect(uint8_t style);
480 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons);
482 struct ao_lisp_cons *
483 ao_lisp_cons_fetch(int id);
486 ao_lisp_poly_stash(int id, ao_poly poly);
489 ao_lisp_poly_fetch(int id);
492 ao_lisp_string_stash(int id, char *string);
495 ao_lisp_string_fetch(int id);
498 ao_lisp_stack_stash(int id, struct ao_lisp_stack *stack) {
499 ao_lisp_poly_stash(id, ao_lisp_stack_poly(stack));
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));
509 extern const struct ao_lisp_type ao_lisp_bool_type;
512 ao_lisp_bool_write(ao_poly v);
514 #ifdef AO_LISP_MAKE_CONST
515 struct ao_lisp_bool *ao_lisp_true, *ao_lisp_false;
517 struct ao_lisp_bool *
518 ao_lisp_bool_get(uint8_t value);
522 extern const struct ao_lisp_type ao_lisp_cons_type;
524 struct ao_lisp_cons *
525 ao_lisp_cons_cons(ao_poly car, ao_poly cdr);
528 ao_lisp__cons(ao_poly car, ao_poly cdr);
530 extern struct ao_lisp_cons *ao_lisp_cons_free_list;
533 ao_lisp_cons_free(struct ao_lisp_cons *cons);
536 ao_lisp_cons_write(ao_poly);
539 ao_lisp_cons_display(ao_poly);
542 ao_lisp_cons_length(struct ao_lisp_cons *cons);
545 extern const struct ao_lisp_type ao_lisp_string_type;
548 ao_lisp_string_copy(char *a);
551 ao_lisp_string_cat(char *a, char *b);
554 ao_lisp_string_pack(struct ao_lisp_cons *cons);
557 ao_lisp_string_unpack(char *a);
560 ao_lisp_string_write(ao_poly s);
563 ao_lisp_string_display(ao_poly s);
566 extern const struct ao_lisp_type ao_lisp_atom_type;
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;
573 ao_lisp_atom_write(ao_poly a);
575 struct ao_lisp_atom *
576 ao_lisp_atom_intern(char *name);
579 ao_lisp_atom_ref(struct ao_lisp_frame *frame, ao_poly atom);
582 ao_lisp_atom_get(ao_poly atom);
585 ao_lisp_atom_set(ao_poly atom, ao_poly val);
589 ao_lisp_int_write(ao_poly i);
592 ao_lisp_poly_integer(ao_poly p);
595 ao_lisp_integer_poly(int32_t i);
598 ao_lisp_integer_typep(uint8_t t)
600 return (t == AO_LISP_INT) || (t == AO_LISP_BIGINT);
604 ao_lisp_bigint_write(ao_poly i);
606 extern const struct ao_lisp_type ao_lisp_bigint_type;
609 ao_lisp_poly_write(ao_poly p);
612 ao_lisp_poly_display(ao_poly p);
615 ao_lisp_poly_mark(ao_poly p, uint8_t note_cons);
617 /* returns 1 if the object has already been moved */
619 ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
624 ao_lisp_eval_clear_globals(void);
627 ao_lisp_eval_restart(void);
630 ao_lisp_eval(ao_poly p);
633 ao_lisp_set_cond(struct ao_lisp_cons *cons);
637 ao_lisp_builtin_write(ao_poly b);
639 extern const struct ao_lisp_type ao_lisp_builtin_type;
641 /* Check argument count */
643 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
645 /* Check argument type */
647 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
649 /* Fetch an arg (nil if off the end) */
651 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
654 ao_lisp_args_name(uint8_t args);
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;
666 ao_lisp_read_eval_print(void);
669 extern const struct ao_lisp_type ao_lisp_frame_type;
671 #define AO_LISP_FRAME_FREE 6
673 extern struct ao_lisp_frame *ao_lisp_frame_free_list[AO_LISP_FRAME_FREE];
676 ao_lisp_frame_mark(struct ao_lisp_frame *frame);
679 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
681 struct ao_lisp_frame *
682 ao_lisp_frame_new(int num);
685 ao_lisp_frame_free(struct ao_lisp_frame *frame);
688 ao_lisp_frame_bind(struct ao_lisp_frame *frame, int num, ao_poly atom, ao_poly val);
691 ao_lisp_frame_add(struct ao_lisp_frame **frame, ao_poly atom, ao_poly val);
694 ao_lisp_frame_write(ao_poly p);
697 extern const struct ao_lisp_type ao_lisp_lambda_type;
699 extern const char *ao_lisp_state_names[];
701 struct ao_lisp_lambda *
702 ao_lisp_lambda_new(ao_poly cons);
705 ao_lisp_lambda_write(ao_poly lambda);
708 ao_lisp_lambda_eval(void);
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;
717 ao_lisp_stack_reset(struct ao_lisp_stack *stack);
720 ao_lisp_stack_push(void);
723 ao_lisp_stack_pop(void);
726 ao_lisp_stack_clear(void);
729 ao_lisp_stack_write(ao_poly stack);
732 ao_lisp_stack_eval(void);
737 ao_lisp_error_poly(char *name, ao_poly poly, ao_poly last);
740 ao_lisp_error_frame(int indent, char *name, struct ao_lisp_frame *frame);
743 ao_lisp_error(int error, char *format, ...);
747 #define AO_LISP_BUILTIN_DECLS
748 #include "ao_lisp_builtin.h"
750 /* debugging macros */
754 int ao_lisp_stack_depth;
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))
767 ao_lisp_frames_dump(void)
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");
775 #define DBG_FRAMES() ao_lisp_frames_dump()
790 #define DBG_MEM_START 1
795 extern int dbg_move_depth;
797 #define MDBG_OFFSET(a) ((int) ((uint8_t *) (a) - ao_lisp_pool))
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))
810 #define MDBG_MOVE(...)
811 #define MDBG_MORE(...)
812 #define MDBG_MOVE_IN()
813 #define MDBG_MOVE_OUT()
817 #endif /* _AO_LISP_H_ */