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_EOF 0x10
115 #define AO_LISP_EXIT 0x20
117 extern uint8_t ao_lisp_exception;
120 ao_lisp_is_const(ao_poly poly) {
121 return poly & AO_LISP_CONST;
124 #define AO_LISP_IS_CONST(a) (ao_lisp_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_const + AO_LISP_POOL_CONST)
125 #define AO_LISP_IS_POOL(a) (ao_lisp_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_pool + AO_LISP_POOL)
126 #define AO_LISP_IS_INT(p) (ao_lisp_poly_base_type(p) == AO_LISP_INT)
129 ao_lisp_ref(ao_poly poly);
132 ao_lisp_poly(const void *addr, ao_poly type);
134 struct ao_lisp_type {
135 int (*size)(void *addr);
136 void (*mark)(void *addr);
137 void (*move)(void *addr);
141 struct ao_lisp_cons {
146 struct ao_lisp_atom {
158 struct ao_lisp_frame_vals {
161 struct ao_lisp_val vals[];
164 struct ao_lisp_frame {
171 struct ao_lisp_bool {
177 struct ao_lisp_bigint {
181 struct ao_lisp_float {
188 #if __BYTE_ORDER == __LITTLE_ENDIAN
189 static inline uint32_t
190 ao_lisp_int_bigint(int32_t i) {
191 return AO_LISP_BIGINT | (i << 8);
193 static inline int32_t
194 ao_lisp_bigint_int(uint32_t bi) {
195 return (int32_t) bi >> 8;
198 static inline uint32_t
199 ao_lisp_int_bigint(int32_t i) {
200 return (uint32_t) (i & 0xffffff) | (AO_LISP_BIGINT << 24);
202 static inlint int32_t
203 ao_lisp_bigint_int(uint32_t bi) {
204 return (int32_t) (bi << 8) >> 8;
208 #define AO_LISP_MIN_INT (-(1 << (15 - AO_LISP_TYPE_SHIFT)))
209 #define AO_LISP_MAX_INT ((1 << (15 - AO_LISP_TYPE_SHIFT)) - 1)
211 #define AO_LISP_NOT_INTEGER 0x7fffffff
213 /* Set on type when the frame escapes the lambda */
214 #define AO_LISP_FRAME_MARK 0x80
215 #define AO_LISP_FRAME_PRINT 0x40
217 static inline int ao_lisp_frame_marked(struct ao_lisp_frame *f) {
218 return f->type & AO_LISP_FRAME_MARK;
221 static inline struct ao_lisp_frame *
222 ao_lisp_poly_frame(ao_poly poly) {
223 return ao_lisp_ref(poly);
226 static inline ao_poly
227 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
228 return ao_lisp_poly(frame, AO_LISP_OTHER);
231 static inline struct ao_lisp_frame_vals *
232 ao_lisp_poly_frame_vals(ao_poly poly) {
233 return ao_lisp_ref(poly);
236 static inline ao_poly
237 ao_lisp_frame_vals_poly(struct ao_lisp_frame_vals *vals) {
238 return ao_lisp_poly(vals, AO_LISP_OTHER);
242 eval_sexpr, /* Evaluate an sexpr */
243 eval_val, /* Value computed */
244 eval_formal, /* Formal computed */
245 eval_exec, /* Start a lambda evaluation */
246 eval_apply, /* Execute apply */
247 eval_cond, /* Start next cond clause */
248 eval_cond_test, /* Check cond condition */
249 eval_begin, /* Start next begin entry */
250 eval_while, /* Start while condition */
251 eval_while_test, /* Check while condition */
252 eval_macro, /* Finished with macro generation */
255 struct ao_lisp_stack {
256 uint8_t type; /* AO_LISP_STACK */
257 uint8_t state; /* enum eval_state */
258 ao_poly prev; /* previous stack frame */
259 ao_poly sexprs; /* expressions to evaluate */
260 ao_poly values; /* values computed */
261 ao_poly values_tail; /* end of the values list for easy appending */
262 ao_poly frame; /* current lookup frame */
263 ao_poly list; /* most recent function call */
266 #define AO_LISP_STACK_MARK 0x80 /* set on type when a reference has been taken */
267 #define AO_LISP_STACK_PRINT 0x40 /* stack is being printed */
269 static inline int ao_lisp_stack_marked(struct ao_lisp_stack *s) {
270 return s->type & AO_LISP_STACK_MARK;
273 static inline void ao_lisp_stack_mark(struct ao_lisp_stack *s) {
274 s->type |= AO_LISP_STACK_MARK;
277 static inline struct ao_lisp_stack *
278 ao_lisp_poly_stack(ao_poly p)
280 return ao_lisp_ref(p);
283 static inline ao_poly
284 ao_lisp_stack_poly(struct ao_lisp_stack *stack)
286 return ao_lisp_poly(stack, AO_LISP_OTHER);
289 extern ao_poly ao_lisp_v;
291 #define AO_LISP_FUNC_LAMBDA 0
292 #define AO_LISP_FUNC_NLAMBDA 1
293 #define AO_LISP_FUNC_MACRO 2
294 #define AO_LISP_FUNC_LEXPR 3
296 #define AO_LISP_FUNC_FREE_ARGS 0x80
297 #define AO_LISP_FUNC_MASK 0x7f
299 #define AO_LISP_FUNC_F_LAMBDA (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LAMBDA)
300 #define AO_LISP_FUNC_F_NLAMBDA (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_NLAMBDA)
301 #define AO_LISP_FUNC_F_MACRO (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_MACRO)
302 #define AO_LISP_FUNC_F_LEXPR (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LEXPR)
304 struct ao_lisp_builtin {
310 #define AO_LISP_BUILTIN_ID
311 #include "ao_lisp_builtin.h"
313 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
315 extern const ao_lisp_func_t ao_lisp_builtins[];
317 static inline ao_lisp_func_t
318 ao_lisp_func(struct ao_lisp_builtin *b)
320 return ao_lisp_builtins[b->func];
323 struct ao_lisp_lambda {
330 static inline struct ao_lisp_lambda *
331 ao_lisp_poly_lambda(ao_poly poly)
333 return ao_lisp_ref(poly);
336 static inline ao_poly
337 ao_lisp_lambda_poly(struct ao_lisp_lambda *lambda)
339 return ao_lisp_poly(lambda, AO_LISP_OTHER);
343 ao_lisp_poly_other(ao_poly poly) {
344 return ao_lisp_ref(poly);
347 static inline uint8_t
348 ao_lisp_other_type(void *other) {
350 if ((*((uint8_t *) other) & AO_LISP_OTHER_TYPE_MASK) >= AO_LISP_NUM_TYPE)
353 return *((uint8_t *) other) & AO_LISP_OTHER_TYPE_MASK;
356 static inline ao_poly
357 ao_lisp_other_poly(const void *other)
359 return ao_lisp_poly(other, AO_LISP_OTHER);
363 ao_lisp_size_round(int size)
365 return (size + 3) & ~3;
369 ao_lisp_size(const struct ao_lisp_type *type, void *addr)
371 return ao_lisp_size_round(type->size(addr));
374 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
376 static inline int ao_lisp_poly_base_type(ao_poly poly) {
377 return poly & AO_LISP_TYPE_MASK;
380 static inline int ao_lisp_poly_type(ao_poly poly) {
381 int type = poly & AO_LISP_TYPE_MASK;
382 if (type == AO_LISP_OTHER)
383 return ao_lisp_other_type(ao_lisp_poly_other(poly));
387 static inline struct ao_lisp_cons *
388 ao_lisp_poly_cons(ao_poly poly)
390 return ao_lisp_ref(poly);
393 static inline ao_poly
394 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
396 return ao_lisp_poly(cons, AO_LISP_CONS);
399 static inline int32_t
400 ao_lisp_poly_int(ao_poly poly)
402 return (int32_t) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
405 static inline ao_poly
406 ao_lisp_int_poly(int32_t i)
408 return ((ao_poly) i << 2) | AO_LISP_INT;
411 static inline struct ao_lisp_bigint *
412 ao_lisp_poly_bigint(ao_poly poly)
414 return ao_lisp_ref(poly);
417 static inline ao_poly
418 ao_lisp_bigint_poly(struct ao_lisp_bigint *bi)
420 return ao_lisp_poly(bi, AO_LISP_OTHER);
424 ao_lisp_poly_string(ao_poly poly)
426 return ao_lisp_ref(poly);
429 static inline ao_poly
430 ao_lisp_string_poly(char *s)
432 return ao_lisp_poly(s, AO_LISP_STRING);
435 static inline struct ao_lisp_atom *
436 ao_lisp_poly_atom(ao_poly poly)
438 return ao_lisp_ref(poly);
441 static inline ao_poly
442 ao_lisp_atom_poly(struct ao_lisp_atom *a)
444 return ao_lisp_poly(a, AO_LISP_OTHER);
447 static inline struct ao_lisp_builtin *
448 ao_lisp_poly_builtin(ao_poly poly)
450 return ao_lisp_ref(poly);
453 static inline ao_poly
454 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
456 return ao_lisp_poly(b, AO_LISP_OTHER);
459 static inline ao_poly
460 ao_lisp_bool_poly(struct ao_lisp_bool *b)
462 return ao_lisp_poly(b, AO_LISP_OTHER);
465 static inline struct ao_lisp_bool *
466 ao_lisp_poly_bool(ao_poly poly)
468 return ao_lisp_ref(poly);
471 static inline ao_poly
472 ao_lisp_float_poly(struct ao_lisp_float *f)
474 return ao_lisp_poly(f, AO_LISP_OTHER);
477 static inline struct ao_lisp_float *
478 ao_lisp_poly_float(ao_poly poly)
480 return ao_lisp_ref(poly);
484 ao_lisp_poly_number(ao_poly p);
486 /* memory functions */
488 extern int ao_lisp_collects[2];
489 extern int ao_lisp_freed[2];
490 extern int ao_lisp_loops[2];
492 /* returns 1 if the object was already marked */
494 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
496 /* returns 1 if the object was already marked */
498 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr);
501 ao_lisp_move_map(void *addr);
503 /* returns 1 if the object was already moved */
505 ao_lisp_move(const struct ao_lisp_type *type, void **ref);
507 /* returns 1 if the object was already moved */
509 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref);
512 ao_lisp_alloc(int size);
514 #define AO_LISP_COLLECT_FULL 1
515 #define AO_LISP_COLLECT_INCREMENTAL 0
518 ao_lisp_collect(uint8_t style);
521 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons);
523 struct ao_lisp_cons *
524 ao_lisp_cons_fetch(int id);
527 ao_lisp_poly_stash(int id, ao_poly poly);
530 ao_lisp_poly_fetch(int id);
533 ao_lisp_string_stash(int id, char *string);
536 ao_lisp_string_fetch(int id);
539 ao_lisp_stack_stash(int id, struct ao_lisp_stack *stack) {
540 ao_lisp_poly_stash(id, ao_lisp_stack_poly(stack));
543 static inline struct ao_lisp_stack *
544 ao_lisp_stack_fetch(int id) {
545 return ao_lisp_poly_stack(ao_lisp_poly_fetch(id));
549 ao_lisp_frame_stash(int id, struct ao_lisp_frame *frame);
551 struct ao_lisp_frame *
552 ao_lisp_frame_fetch(int id);
556 extern const struct ao_lisp_type ao_lisp_bool_type;
559 ao_lisp_bool_write(ao_poly v);
561 #ifdef AO_LISP_MAKE_CONST
562 struct ao_lisp_bool *ao_lisp_true, *ao_lisp_false;
564 struct ao_lisp_bool *
565 ao_lisp_bool_get(uint8_t value);
569 extern const struct ao_lisp_type ao_lisp_cons_type;
571 struct ao_lisp_cons *
572 ao_lisp_cons_cons(ao_poly car, ao_poly cdr);
574 /* Return a cons or NULL for a proper list, else error */
575 struct ao_lisp_cons *
576 ao_lisp_cons_cdr(struct ao_lisp_cons *cons);
579 ao_lisp__cons(ao_poly car, ao_poly cdr);
581 extern struct ao_lisp_cons *ao_lisp_cons_free_list;
584 ao_lisp_cons_free(struct ao_lisp_cons *cons);
587 ao_lisp_cons_write(ao_poly);
590 ao_lisp_cons_display(ao_poly);
593 ao_lisp_cons_length(struct ao_lisp_cons *cons);
596 extern const struct ao_lisp_type ao_lisp_string_type;
599 ao_lisp_string_copy(char *a);
602 ao_lisp_string_cat(char *a, char *b);
605 ao_lisp_string_pack(struct ao_lisp_cons *cons);
608 ao_lisp_string_unpack(char *a);
611 ao_lisp_string_write(ao_poly s);
614 ao_lisp_string_display(ao_poly s);
617 extern const struct ao_lisp_type ao_lisp_atom_type;
619 extern struct ao_lisp_atom *ao_lisp_atoms;
620 extern struct ao_lisp_frame *ao_lisp_frame_global;
621 extern struct ao_lisp_frame *ao_lisp_frame_current;
624 ao_lisp_atom_write(ao_poly a);
626 struct ao_lisp_atom *
627 ao_lisp_atom_intern(char *name);
630 ao_lisp_atom_ref(struct ao_lisp_frame *frame, ao_poly atom);
633 ao_lisp_atom_get(ao_poly atom);
636 ao_lisp_atom_set(ao_poly atom, ao_poly val);
640 ao_lisp_int_write(ao_poly i);
643 ao_lisp_poly_integer(ao_poly p);
646 ao_lisp_integer_poly(int32_t i);
649 ao_lisp_integer_typep(uint8_t t)
651 return (t == AO_LISP_INT) || (t == AO_LISP_BIGINT);
655 ao_lisp_bigint_write(ao_poly i);
657 extern const struct ao_lisp_type ao_lisp_bigint_type;
660 ao_lisp_poly_write(ao_poly p);
663 ao_lisp_poly_display(ao_poly p);
666 ao_lisp_poly_mark(ao_poly p, uint8_t note_cons);
668 /* returns 1 if the object has already been moved */
670 ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
675 ao_lisp_eval_clear_globals(void);
678 ao_lisp_eval_restart(void);
681 ao_lisp_eval(ao_poly p);
684 ao_lisp_set_cond(struct ao_lisp_cons *cons);
687 extern const struct ao_lisp_type ao_lisp_float_type;
690 ao_lisp_float_write(ao_poly p);
693 ao_lisp_float_get(float value);
695 static inline uint8_t
696 ao_lisp_number_typep(uint8_t t)
698 return ao_lisp_integer_typep(t) || (t == AO_LISP_FLOAT);
702 ao_lisp_poly_number(ao_poly p);
706 ao_lisp_builtin_write(ao_poly b);
708 extern const struct ao_lisp_type ao_lisp_builtin_type;
710 /* Check argument count */
712 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
714 /* Check argument type */
716 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
718 /* Fetch an arg (nil if off the end) */
720 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
723 ao_lisp_args_name(uint8_t args);
726 extern struct ao_lisp_cons *ao_lisp_read_cons;
727 extern struct ao_lisp_cons *ao_lisp_read_cons_tail;
728 extern struct ao_lisp_cons *ao_lisp_read_stack;
735 ao_lisp_read_eval_print(void);
738 extern const struct ao_lisp_type ao_lisp_frame_type;
739 extern const struct ao_lisp_type ao_lisp_frame_vals_type;
741 #define AO_LISP_FRAME_FREE 6
743 extern struct ao_lisp_frame *ao_lisp_frame_free_list[AO_LISP_FRAME_FREE];
746 ao_lisp_frame_mark(struct ao_lisp_frame *frame);
749 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
751 struct ao_lisp_frame *
752 ao_lisp_frame_new(int num);
755 ao_lisp_frame_free(struct ao_lisp_frame *frame);
758 ao_lisp_frame_bind(struct ao_lisp_frame *frame, int num, ao_poly atom, ao_poly val);
761 ao_lisp_frame_add(struct ao_lisp_frame **frame, ao_poly atom, ao_poly val);
764 ao_lisp_frame_write(ao_poly p);
767 extern const struct ao_lisp_type ao_lisp_lambda_type;
769 extern const char *ao_lisp_state_names[];
771 struct ao_lisp_lambda *
772 ao_lisp_lambda_new(ao_poly cons);
775 ao_lisp_lambda_write(ao_poly lambda);
778 ao_lisp_lambda_eval(void);
782 extern const struct ao_lisp_type ao_lisp_stack_type;
783 extern struct ao_lisp_stack *ao_lisp_stack;
784 extern struct ao_lisp_stack *ao_lisp_stack_free_list;
787 ao_lisp_stack_reset(struct ao_lisp_stack *stack);
790 ao_lisp_stack_push(void);
793 ao_lisp_stack_pop(void);
796 ao_lisp_stack_clear(void);
799 ao_lisp_stack_write(ao_poly stack);
802 ao_lisp_stack_eval(void);
807 ao_lisp_error_poly(char *name, ao_poly poly, ao_poly last);
810 ao_lisp_error_frame(int indent, char *name, struct ao_lisp_frame *frame);
813 ao_lisp_error(int error, char *format, ...);
817 #define AO_LISP_BUILTIN_DECLS
818 #include "ao_lisp_builtin.h"
820 /* debugging macros */
824 int ao_lisp_stack_depth;
826 #define DBG_INDENT() do { int _s; for(_s = 0; _s < ao_lisp_stack_depth; _s++) printf(" "); } while(0)
827 #define DBG_IN() (++ao_lisp_stack_depth)
828 #define DBG_OUT() (--ao_lisp_stack_depth)
829 #define DBG_RESET() (ao_lisp_stack_depth = 0)
830 #define DBG(...) printf(__VA_ARGS__)
831 #define DBGI(...) do { DBG("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
832 #define DBG_CONS(a) ao_lisp_cons_write(ao_lisp_cons_poly(a))
833 #define DBG_POLY(a) ao_lisp_poly_write(a)
834 #define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_lisp_pool) : -1)
835 #define DBG_STACK() ao_lisp_stack_write(ao_lisp_stack_poly(ao_lisp_stack))
837 ao_lisp_frames_dump(void)
839 struct ao_lisp_stack *s;
840 DBGI(".. current frame: "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
841 for (s = ao_lisp_stack; s; s = ao_lisp_poly_stack(s->prev)) {
842 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
845 #define DBG_FRAMES() ao_lisp_frames_dump()
860 #define DBG_MEM_START 1
865 extern int dbg_move_depth;
867 #define MDBG_OFFSET(a) ((int) ((uint8_t *) (a) - ao_lisp_pool))
872 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
873 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
874 #define MDBG_MOVE_IN() (dbg_move_depth++)
875 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
880 #define MDBG_MOVE(...)
881 #define MDBG_MORE(...)
882 #define MDBG_MOVE_IN()
883 #define MDBG_MOVE_OUT()
887 #endif /* _AO_LISP_H_ */