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_FRAME_VALS 7
96 #define AO_LISP_LAMBDA 8
97 #define AO_LISP_STACK 9
98 #define AO_LISP_BOOL 10
99 #define AO_LISP_BIGINT 11
100 #define AO_LISP_FLOAT 12
101 #define AO_LISP_NUM_TYPE 13
103 /* Leave two bits for types to use as they please */
104 #define AO_LISP_OTHER_TYPE_MASK 0x3f
106 #define AO_LISP_NIL 0
108 extern uint16_t ao_lisp_top;
110 #define AO_LISP_OOM 0x01
111 #define AO_LISP_DIVIDE_BY_ZERO 0x02
112 #define AO_LISP_INVALID 0x04
113 #define AO_LISP_UNDEFINED 0x08
114 #define AO_LISP_REDEFINED 0x10
115 #define AO_LISP_EOF 0x20
116 #define AO_LISP_EXIT 0x40
118 extern uint8_t ao_lisp_exception;
121 ao_lisp_is_const(ao_poly poly) {
122 return poly & AO_LISP_CONST;
125 #define AO_LISP_IS_CONST(a) (ao_lisp_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_const + AO_LISP_POOL_CONST)
126 #define AO_LISP_IS_POOL(a) (ao_lisp_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_pool + AO_LISP_POOL)
127 #define AO_LISP_IS_INT(p) (ao_lisp_poly_base_type(p) == AO_LISP_INT)
130 ao_lisp_ref(ao_poly poly);
133 ao_lisp_poly(const void *addr, ao_poly type);
135 struct ao_lisp_type {
136 int (*size)(void *addr);
137 void (*mark)(void *addr);
138 void (*move)(void *addr);
142 struct ao_lisp_cons {
147 struct ao_lisp_atom {
159 struct ao_lisp_frame_vals {
162 struct ao_lisp_val vals[];
165 struct ao_lisp_frame {
172 struct ao_lisp_bool {
178 struct ao_lisp_bigint {
182 struct ao_lisp_float {
189 #if __BYTE_ORDER == __LITTLE_ENDIAN
190 static inline uint32_t
191 ao_lisp_int_bigint(int32_t i) {
192 return AO_LISP_BIGINT | (i << 8);
194 static inline int32_t
195 ao_lisp_bigint_int(uint32_t bi) {
196 return (int32_t) bi >> 8;
199 static inline uint32_t
200 ao_lisp_int_bigint(int32_t i) {
201 return (uint32_t) (i & 0xffffff) | (AO_LISP_BIGINT << 24);
203 static inlint int32_t
204 ao_lisp_bigint_int(uint32_t bi) {
205 return (int32_t) (bi << 8) >> 8;
209 #define AO_LISP_MIN_INT (-(1 << (15 - AO_LISP_TYPE_SHIFT)))
210 #define AO_LISP_MAX_INT ((1 << (15 - AO_LISP_TYPE_SHIFT)) - 1)
212 #define AO_LISP_NOT_INTEGER 0x7fffffff
214 /* Set on type when the frame escapes the lambda */
215 #define AO_LISP_FRAME_MARK 0x80
216 #define AO_LISP_FRAME_PRINT 0x40
218 static inline int ao_lisp_frame_marked(struct ao_lisp_frame *f) {
219 return f->type & AO_LISP_FRAME_MARK;
222 static inline struct ao_lisp_frame *
223 ao_lisp_poly_frame(ao_poly poly) {
224 return ao_lisp_ref(poly);
227 static inline ao_poly
228 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
229 return ao_lisp_poly(frame, AO_LISP_OTHER);
232 static inline struct ao_lisp_frame_vals *
233 ao_lisp_poly_frame_vals(ao_poly poly) {
234 return ao_lisp_ref(poly);
237 static inline ao_poly
238 ao_lisp_frame_vals_poly(struct ao_lisp_frame_vals *vals) {
239 return ao_lisp_poly(vals, AO_LISP_OTHER);
243 eval_sexpr, /* Evaluate an sexpr */
244 eval_val, /* Value computed */
245 eval_formal, /* Formal computed */
246 eval_exec, /* Start a lambda evaluation */
247 eval_apply, /* Execute apply */
248 eval_cond, /* Start next cond clause */
249 eval_cond_test, /* Check cond condition */
250 eval_begin, /* Start next begin entry */
251 eval_while, /* Start while condition */
252 eval_while_test, /* Check while condition */
253 eval_macro, /* Finished with macro generation */
256 struct ao_lisp_stack {
257 uint8_t type; /* AO_LISP_STACK */
258 uint8_t state; /* enum eval_state */
259 ao_poly prev; /* previous stack frame */
260 ao_poly sexprs; /* expressions to evaluate */
261 ao_poly values; /* values computed */
262 ao_poly values_tail; /* end of the values list for easy appending */
263 ao_poly frame; /* current lookup frame */
264 ao_poly list; /* most recent function call */
267 #define AO_LISP_STACK_MARK 0x80 /* set on type when a reference has been taken */
268 #define AO_LISP_STACK_PRINT 0x40 /* stack is being printed */
270 static inline int ao_lisp_stack_marked(struct ao_lisp_stack *s) {
271 return s->type & AO_LISP_STACK_MARK;
274 static inline void ao_lisp_stack_mark(struct ao_lisp_stack *s) {
275 s->type |= AO_LISP_STACK_MARK;
278 static inline struct ao_lisp_stack *
279 ao_lisp_poly_stack(ao_poly p)
281 return ao_lisp_ref(p);
284 static inline ao_poly
285 ao_lisp_stack_poly(struct ao_lisp_stack *stack)
287 return ao_lisp_poly(stack, AO_LISP_OTHER);
290 extern ao_poly ao_lisp_v;
292 #define AO_LISP_FUNC_LAMBDA 0
293 #define AO_LISP_FUNC_NLAMBDA 1
294 #define AO_LISP_FUNC_MACRO 2
295 #define AO_LISP_FUNC_LEXPR 3
297 #define AO_LISP_FUNC_FREE_ARGS 0x80
298 #define AO_LISP_FUNC_MASK 0x7f
300 #define AO_LISP_FUNC_F_LAMBDA (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LAMBDA)
301 #define AO_LISP_FUNC_F_NLAMBDA (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_NLAMBDA)
302 #define AO_LISP_FUNC_F_MACRO (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_MACRO)
303 #define AO_LISP_FUNC_F_LEXPR (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LEXPR)
305 struct ao_lisp_builtin {
311 #define AO_LISP_BUILTIN_ID
312 #include "ao_lisp_builtin.h"
314 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
316 extern const ao_lisp_func_t ao_lisp_builtins[];
318 static inline ao_lisp_func_t
319 ao_lisp_func(struct ao_lisp_builtin *b)
321 return ao_lisp_builtins[b->func];
324 struct ao_lisp_lambda {
331 static inline struct ao_lisp_lambda *
332 ao_lisp_poly_lambda(ao_poly poly)
334 return ao_lisp_ref(poly);
337 static inline ao_poly
338 ao_lisp_lambda_poly(struct ao_lisp_lambda *lambda)
340 return ao_lisp_poly(lambda, AO_LISP_OTHER);
344 ao_lisp_poly_other(ao_poly poly) {
345 return ao_lisp_ref(poly);
348 static inline uint8_t
349 ao_lisp_other_type(void *other) {
351 if ((*((uint8_t *) other) & AO_LISP_OTHER_TYPE_MASK) >= AO_LISP_NUM_TYPE)
354 return *((uint8_t *) other) & AO_LISP_OTHER_TYPE_MASK;
357 static inline ao_poly
358 ao_lisp_other_poly(const void *other)
360 return ao_lisp_poly(other, AO_LISP_OTHER);
364 ao_lisp_size_round(int size)
366 return (size + 3) & ~3;
370 ao_lisp_size(const struct ao_lisp_type *type, void *addr)
372 return ao_lisp_size_round(type->size(addr));
375 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
377 static inline int ao_lisp_poly_base_type(ao_poly poly) {
378 return poly & AO_LISP_TYPE_MASK;
381 static inline int ao_lisp_poly_type(ao_poly poly) {
382 int type = poly & AO_LISP_TYPE_MASK;
383 if (type == AO_LISP_OTHER)
384 return ao_lisp_other_type(ao_lisp_poly_other(poly));
388 static inline struct ao_lisp_cons *
389 ao_lisp_poly_cons(ao_poly poly)
391 return ao_lisp_ref(poly);
394 static inline ao_poly
395 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
397 return ao_lisp_poly(cons, AO_LISP_CONS);
400 static inline int32_t
401 ao_lisp_poly_int(ao_poly poly)
403 return (int32_t) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
406 static inline ao_poly
407 ao_lisp_int_poly(int32_t i)
409 return ((ao_poly) i << 2) | AO_LISP_INT;
412 static inline struct ao_lisp_bigint *
413 ao_lisp_poly_bigint(ao_poly poly)
415 return ao_lisp_ref(poly);
418 static inline ao_poly
419 ao_lisp_bigint_poly(struct ao_lisp_bigint *bi)
421 return ao_lisp_poly(bi, AO_LISP_OTHER);
425 ao_lisp_poly_string(ao_poly poly)
427 return ao_lisp_ref(poly);
430 static inline ao_poly
431 ao_lisp_string_poly(char *s)
433 return ao_lisp_poly(s, AO_LISP_STRING);
436 static inline struct ao_lisp_atom *
437 ao_lisp_poly_atom(ao_poly poly)
439 return ao_lisp_ref(poly);
442 static inline ao_poly
443 ao_lisp_atom_poly(struct ao_lisp_atom *a)
445 return ao_lisp_poly(a, AO_LISP_OTHER);
448 static inline struct ao_lisp_builtin *
449 ao_lisp_poly_builtin(ao_poly poly)
451 return ao_lisp_ref(poly);
454 static inline ao_poly
455 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
457 return ao_lisp_poly(b, AO_LISP_OTHER);
460 static inline ao_poly
461 ao_lisp_bool_poly(struct ao_lisp_bool *b)
463 return ao_lisp_poly(b, AO_LISP_OTHER);
466 static inline struct ao_lisp_bool *
467 ao_lisp_poly_bool(ao_poly poly)
469 return ao_lisp_ref(poly);
472 static inline ao_poly
473 ao_lisp_float_poly(struct ao_lisp_float *f)
475 return ao_lisp_poly(f, AO_LISP_OTHER);
478 static inline struct ao_lisp_float *
479 ao_lisp_poly_float(ao_poly poly)
481 return ao_lisp_ref(poly);
485 ao_lisp_poly_number(ao_poly p);
487 /* memory functions */
489 extern int ao_lisp_collects[2];
490 extern int ao_lisp_freed[2];
491 extern int ao_lisp_loops[2];
493 /* returns 1 if the object was already marked */
495 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
497 /* returns 1 if the object was already marked */
499 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr);
502 ao_lisp_move_map(void *addr);
504 /* returns 1 if the object was already moved */
506 ao_lisp_move(const struct ao_lisp_type *type, void **ref);
508 /* returns 1 if the object was already moved */
510 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref);
513 ao_lisp_alloc(int size);
515 #define AO_LISP_COLLECT_FULL 1
516 #define AO_LISP_COLLECT_INCREMENTAL 0
519 ao_lisp_collect(uint8_t style);
522 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons);
524 struct ao_lisp_cons *
525 ao_lisp_cons_fetch(int id);
528 ao_lisp_poly_stash(int id, ao_poly poly);
531 ao_lisp_poly_fetch(int id);
534 ao_lisp_string_stash(int id, char *string);
537 ao_lisp_string_fetch(int id);
540 ao_lisp_stack_stash(int id, struct ao_lisp_stack *stack) {
541 ao_lisp_poly_stash(id, ao_lisp_stack_poly(stack));
544 static inline struct ao_lisp_stack *
545 ao_lisp_stack_fetch(int id) {
546 return ao_lisp_poly_stack(ao_lisp_poly_fetch(id));
550 ao_lisp_frame_stash(int id, struct ao_lisp_frame *frame);
552 struct ao_lisp_frame *
553 ao_lisp_frame_fetch(int id);
557 extern const struct ao_lisp_type ao_lisp_bool_type;
560 ao_lisp_bool_write(ao_poly v);
562 #ifdef AO_LISP_MAKE_CONST
563 struct ao_lisp_bool *ao_lisp_true, *ao_lisp_false;
565 struct ao_lisp_bool *
566 ao_lisp_bool_get(uint8_t value);
570 extern const struct ao_lisp_type ao_lisp_cons_type;
572 struct ao_lisp_cons *
573 ao_lisp_cons_cons(ao_poly car, ao_poly cdr);
575 /* Return a cons or NULL for a proper list, else error */
576 struct ao_lisp_cons *
577 ao_lisp_cons_cdr(struct ao_lisp_cons *cons);
580 ao_lisp__cons(ao_poly car, ao_poly cdr);
582 extern struct ao_lisp_cons *ao_lisp_cons_free_list;
585 ao_lisp_cons_free(struct ao_lisp_cons *cons);
588 ao_lisp_cons_write(ao_poly);
591 ao_lisp_cons_display(ao_poly);
594 ao_lisp_cons_length(struct ao_lisp_cons *cons);
597 extern const struct ao_lisp_type ao_lisp_string_type;
600 ao_lisp_string_copy(char *a);
603 ao_lisp_string_cat(char *a, char *b);
606 ao_lisp_string_pack(struct ao_lisp_cons *cons);
609 ao_lisp_string_unpack(char *a);
612 ao_lisp_string_write(ao_poly s);
615 ao_lisp_string_display(ao_poly s);
618 extern const struct ao_lisp_type ao_lisp_atom_type;
620 extern struct ao_lisp_atom *ao_lisp_atoms;
621 extern struct ao_lisp_frame *ao_lisp_frame_global;
622 extern struct ao_lisp_frame *ao_lisp_frame_current;
625 ao_lisp_atom_write(ao_poly a);
627 struct ao_lisp_atom *
628 ao_lisp_atom_intern(char *name);
631 ao_lisp_atom_ref(ao_poly atom);
634 ao_lisp_atom_get(ao_poly atom);
637 ao_lisp_atom_set(ao_poly atom, ao_poly val);
640 ao_lisp_atom_def(ao_poly atom, ao_poly val);
644 ao_lisp_int_write(ao_poly i);
647 ao_lisp_poly_integer(ao_poly p);
650 ao_lisp_integer_poly(int32_t i);
653 ao_lisp_integer_typep(uint8_t t)
655 return (t == AO_LISP_INT) || (t == AO_LISP_BIGINT);
659 ao_lisp_bigint_write(ao_poly i);
661 extern const struct ao_lisp_type ao_lisp_bigint_type;
664 ao_lisp_poly_write(ao_poly p);
667 ao_lisp_poly_display(ao_poly p);
670 ao_lisp_poly_mark(ao_poly p, uint8_t note_cons);
672 /* returns 1 if the object has already been moved */
674 ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
679 ao_lisp_eval_clear_globals(void);
682 ao_lisp_eval_restart(void);
685 ao_lisp_eval(ao_poly p);
688 ao_lisp_set_cond(struct ao_lisp_cons *cons);
691 extern const struct ao_lisp_type ao_lisp_float_type;
694 ao_lisp_float_write(ao_poly p);
697 ao_lisp_float_get(float value);
699 static inline uint8_t
700 ao_lisp_number_typep(uint8_t t)
702 return ao_lisp_integer_typep(t) || (t == AO_LISP_FLOAT);
706 ao_lisp_poly_number(ao_poly p);
710 ao_lisp_builtin_write(ao_poly b);
712 extern const struct ao_lisp_type ao_lisp_builtin_type;
714 /* Check argument count */
716 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
718 /* Check argument type */
720 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
722 /* Fetch an arg (nil if off the end) */
724 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
727 ao_lisp_args_name(uint8_t args);
730 extern struct ao_lisp_cons *ao_lisp_read_cons;
731 extern struct ao_lisp_cons *ao_lisp_read_cons_tail;
732 extern struct ao_lisp_cons *ao_lisp_read_stack;
739 ao_lisp_read_eval_print(void);
742 extern const struct ao_lisp_type ao_lisp_frame_type;
743 extern const struct ao_lisp_type ao_lisp_frame_vals_type;
745 #define AO_LISP_FRAME_FREE 6
747 extern struct ao_lisp_frame *ao_lisp_frame_free_list[AO_LISP_FRAME_FREE];
750 ao_lisp_frame_mark(struct ao_lisp_frame *frame);
753 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
755 struct ao_lisp_frame *
756 ao_lisp_frame_new(int num);
759 ao_lisp_frame_free(struct ao_lisp_frame *frame);
762 ao_lisp_frame_bind(struct ao_lisp_frame *frame, int num, ao_poly atom, ao_poly val);
765 ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val);
768 ao_lisp_frame_write(ao_poly p);
771 ao_lisp_frame_init(void);
774 extern const struct ao_lisp_type ao_lisp_lambda_type;
776 extern const char *ao_lisp_state_names[];
778 struct ao_lisp_lambda *
779 ao_lisp_lambda_new(ao_poly cons);
782 ao_lisp_lambda_write(ao_poly lambda);
785 ao_lisp_lambda_eval(void);
789 extern const struct ao_lisp_type ao_lisp_stack_type;
790 extern struct ao_lisp_stack *ao_lisp_stack;
791 extern struct ao_lisp_stack *ao_lisp_stack_free_list;
794 ao_lisp_stack_reset(struct ao_lisp_stack *stack);
797 ao_lisp_stack_push(void);
800 ao_lisp_stack_pop(void);
803 ao_lisp_stack_clear(void);
806 ao_lisp_stack_write(ao_poly stack);
809 ao_lisp_stack_eval(void);
814 ao_lisp_error_poly(char *name, ao_poly poly, ao_poly last);
817 ao_lisp_error_frame(int indent, char *name, struct ao_lisp_frame *frame);
820 ao_lisp_error(int error, char *format, ...);
824 #define AO_LISP_BUILTIN_DECLS
825 #include "ao_lisp_builtin.h"
827 /* debugging macros */
831 int ao_lisp_stack_depth;
833 #define DBG_INDENT() do { int _s; for(_s = 0; _s < ao_lisp_stack_depth; _s++) printf(" "); } while(0)
834 #define DBG_IN() (++ao_lisp_stack_depth)
835 #define DBG_OUT() (--ao_lisp_stack_depth)
836 #define DBG_RESET() (ao_lisp_stack_depth = 0)
837 #define DBG(...) printf(__VA_ARGS__)
838 #define DBGI(...) do { DBG("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
839 #define DBG_CONS(a) ao_lisp_cons_write(ao_lisp_cons_poly(a))
840 #define DBG_POLY(a) ao_lisp_poly_write(a)
841 #define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_lisp_pool) : -1)
842 #define DBG_STACK() ao_lisp_stack_write(ao_lisp_stack_poly(ao_lisp_stack))
844 ao_lisp_frames_dump(void)
846 struct ao_lisp_stack *s;
847 DBGI(".. current frame: "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
848 for (s = ao_lisp_stack; s; s = ao_lisp_poly_stack(s->prev)) {
849 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
852 #define DBG_FRAMES() ao_lisp_frames_dump()
867 #define DBG_MEM_START 1
872 extern int dbg_move_depth;
874 #define MDBG_OFFSET(a) ((a) ? (int) ((uint8_t *) (a) - ao_lisp_pool) : -1)
879 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
880 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
881 #define MDBG_MOVE_IN() (dbg_move_depth++)
882 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
887 #define MDBG_MOVE(...)
888 #define MDBG_MORE(...)
889 #define MDBG_MOVE_IN()
890 #define MDBG_MOVE_OUT()
894 #endif /* _AO_LISP_H_ */