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_FLOAT 11
100 #define AO_LISP_NUM_TYPE 12
102 /* Leave two bits for types to use as they please */
103 #define AO_LISP_OTHER_TYPE_MASK 0x3f
105 #define AO_LISP_NIL 0
107 extern uint16_t ao_lisp_top;
109 #define AO_LISP_OOM 0x01
110 #define AO_LISP_DIVIDE_BY_ZERO 0x02
111 #define AO_LISP_INVALID 0x04
112 #define AO_LISP_UNDEFINED 0x08
113 #define AO_LISP_EOF 0x10
114 #define AO_LISP_EXIT 0x20
116 extern uint8_t ao_lisp_exception;
119 ao_lisp_is_const(ao_poly poly) {
120 return poly & AO_LISP_CONST;
123 #define AO_LISP_IS_CONST(a) (ao_lisp_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_const + AO_LISP_POOL_CONST)
124 #define AO_LISP_IS_POOL(a) (ao_lisp_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_pool + AO_LISP_POOL)
125 #define AO_LISP_IS_INT(p) (ao_lisp_poly_base_type(p) == AO_LISP_INT)
128 ao_lisp_ref(ao_poly poly);
131 ao_lisp_poly(const void *addr, ao_poly type);
133 struct ao_lisp_type {
134 int (*size)(void *addr);
135 void (*mark)(void *addr);
136 void (*move)(void *addr);
140 struct ao_lisp_cons {
145 struct ao_lisp_atom {
157 struct ao_lisp_frame {
161 struct ao_lisp_val vals[];
164 struct ao_lisp_bool {
170 struct ao_lisp_bigint {
174 struct ao_lisp_float {
181 #if __BYTE_ORDER == __LITTLE_ENDIAN
182 static inline uint32_t
183 ao_lisp_int_bigint(int32_t i) {
184 return AO_LISP_BIGINT | (i << 8);
186 static inline int32_t
187 ao_lisp_bigint_int(uint32_t bi) {
188 return (int32_t) bi >> 8;
191 static inline uint32_t
192 ao_lisp_int_bigint(int32_t i) {
193 return (uint32_t) (i & 0xffffff) | (AO_LISP_BIGINT << 24);
195 static inlint int32_t
196 ao_lisp_bigint_int(uint32_t bi) {
197 return (int32_t) (bi << 8) >> 8;
201 #define AO_LISP_MIN_INT (-(1 << (15 - AO_LISP_TYPE_SHIFT)))
202 #define AO_LISP_MAX_INT ((1 << (15 - AO_LISP_TYPE_SHIFT)) - 1)
204 #define AO_LISP_NOT_INTEGER 0x7fffffff
206 /* Set on type when the frame escapes the lambda */
207 #define AO_LISP_FRAME_MARK 0x80
208 #define AO_LISP_FRAME_PRINT 0x40
210 static inline int ao_lisp_frame_marked(struct ao_lisp_frame *f) {
211 return f->type & AO_LISP_FRAME_MARK;
214 static inline struct ao_lisp_frame *
215 ao_lisp_poly_frame(ao_poly poly) {
216 return ao_lisp_ref(poly);
219 static inline ao_poly
220 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
221 return ao_lisp_poly(frame, AO_LISP_OTHER);
225 eval_sexpr, /* Evaluate an sexpr */
226 eval_val, /* Value computed */
227 eval_formal, /* Formal computed */
228 eval_exec, /* Start a lambda evaluation */
229 eval_apply, /* Execute apply */
230 eval_cond, /* Start next cond clause */
231 eval_cond_test, /* Check cond condition */
232 eval_progn, /* Start next progn entry */
233 eval_while, /* Start while condition */
234 eval_while_test, /* Check while condition */
235 eval_macro, /* Finished with macro generation */
238 struct ao_lisp_stack {
239 uint8_t type; /* AO_LISP_STACK */
240 uint8_t state; /* enum eval_state */
241 ao_poly prev; /* previous stack frame */
242 ao_poly sexprs; /* expressions to evaluate */
243 ao_poly values; /* values computed */
244 ao_poly values_tail; /* end of the values list for easy appending */
245 ao_poly frame; /* current lookup frame */
246 ao_poly list; /* most recent function call */
249 #define AO_LISP_STACK_MARK 0x80 /* set on type when a reference has been taken */
250 #define AO_LISP_STACK_PRINT 0x40 /* stack is being printed */
252 static inline int ao_lisp_stack_marked(struct ao_lisp_stack *s) {
253 return s->type & AO_LISP_STACK_MARK;
256 static inline void ao_lisp_stack_mark(struct ao_lisp_stack *s) {
257 s->type |= AO_LISP_STACK_MARK;
260 static inline struct ao_lisp_stack *
261 ao_lisp_poly_stack(ao_poly p)
263 return ao_lisp_ref(p);
266 static inline ao_poly
267 ao_lisp_stack_poly(struct ao_lisp_stack *stack)
269 return ao_lisp_poly(stack, AO_LISP_OTHER);
272 extern ao_poly ao_lisp_v;
274 #define AO_LISP_FUNC_LAMBDA 0
275 #define AO_LISP_FUNC_NLAMBDA 1
276 #define AO_LISP_FUNC_MACRO 2
277 #define AO_LISP_FUNC_LEXPR 3
279 #define AO_LISP_FUNC_FREE_ARGS 0x80
280 #define AO_LISP_FUNC_MASK 0x7f
282 #define AO_LISP_FUNC_F_LAMBDA (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LAMBDA)
283 #define AO_LISP_FUNC_F_NLAMBDA (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_NLAMBDA)
284 #define AO_LISP_FUNC_F_MACRO (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_MACRO)
285 #define AO_LISP_FUNC_F_LEXPR (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LEXPR)
287 struct ao_lisp_builtin {
293 #define AO_LISP_BUILTIN_ID
294 #include "ao_lisp_builtin.h"
296 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
298 extern const ao_lisp_func_t ao_lisp_builtins[];
300 static inline ao_lisp_func_t
301 ao_lisp_func(struct ao_lisp_builtin *b)
303 return ao_lisp_builtins[b->func];
306 struct ao_lisp_lambda {
313 static inline struct ao_lisp_lambda *
314 ao_lisp_poly_lambda(ao_poly poly)
316 return ao_lisp_ref(poly);
319 static inline ao_poly
320 ao_lisp_lambda_poly(struct ao_lisp_lambda *lambda)
322 return ao_lisp_poly(lambda, AO_LISP_OTHER);
326 ao_lisp_poly_other(ao_poly poly) {
327 return ao_lisp_ref(poly);
330 static inline uint8_t
331 ao_lisp_other_type(void *other) {
333 if ((*((uint8_t *) other) & AO_LISP_OTHER_TYPE_MASK) >= AO_LISP_NUM_TYPE)
336 return *((uint8_t *) other) & AO_LISP_OTHER_TYPE_MASK;
339 static inline ao_poly
340 ao_lisp_other_poly(const void *other)
342 return ao_lisp_poly(other, AO_LISP_OTHER);
346 ao_lisp_size_round(int size)
348 return (size + 3) & ~3;
352 ao_lisp_size(const struct ao_lisp_type *type, void *addr)
354 return ao_lisp_size_round(type->size(addr));
357 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
359 static inline int ao_lisp_poly_base_type(ao_poly poly) {
360 return poly & AO_LISP_TYPE_MASK;
363 static inline int ao_lisp_poly_type(ao_poly poly) {
364 int type = poly & AO_LISP_TYPE_MASK;
365 if (type == AO_LISP_OTHER)
366 return ao_lisp_other_type(ao_lisp_poly_other(poly));
370 static inline struct ao_lisp_cons *
371 ao_lisp_poly_cons(ao_poly poly)
373 return ao_lisp_ref(poly);
376 static inline ao_poly
377 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
379 return ao_lisp_poly(cons, AO_LISP_CONS);
382 static inline int32_t
383 ao_lisp_poly_int(ao_poly poly)
385 return (int32_t) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
388 static inline ao_poly
389 ao_lisp_int_poly(int32_t i)
391 return ((ao_poly) i << 2) | AO_LISP_INT;
394 static inline struct ao_lisp_bigint *
395 ao_lisp_poly_bigint(ao_poly poly)
397 return ao_lisp_ref(poly);
400 static inline ao_poly
401 ao_lisp_bigint_poly(struct ao_lisp_bigint *bi)
403 return ao_lisp_poly(bi, AO_LISP_OTHER);
407 ao_lisp_poly_string(ao_poly poly)
409 return ao_lisp_ref(poly);
412 static inline ao_poly
413 ao_lisp_string_poly(char *s)
415 return ao_lisp_poly(s, AO_LISP_STRING);
418 static inline struct ao_lisp_atom *
419 ao_lisp_poly_atom(ao_poly poly)
421 return ao_lisp_ref(poly);
424 static inline ao_poly
425 ao_lisp_atom_poly(struct ao_lisp_atom *a)
427 return ao_lisp_poly(a, AO_LISP_OTHER);
430 static inline struct ao_lisp_builtin *
431 ao_lisp_poly_builtin(ao_poly poly)
433 return ao_lisp_ref(poly);
436 static inline ao_poly
437 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
439 return ao_lisp_poly(b, AO_LISP_OTHER);
442 static inline ao_poly
443 ao_lisp_bool_poly(struct ao_lisp_bool *b)
445 return ao_lisp_poly(b, AO_LISP_OTHER);
448 static inline struct ao_lisp_bool *
449 ao_lisp_poly_bool(ao_poly poly)
451 return ao_lisp_ref(poly);
454 static inline ao_poly
455 ao_lisp_float_poly(struct ao_lisp_float *f)
457 return ao_lisp_poly(f, AO_LISP_OTHER);
460 static inline struct ao_lisp_float *
461 ao_lisp_poly_float(ao_poly poly)
463 return ao_lisp_ref(poly);
467 ao_lisp_poly_number(ao_poly p);
469 /* memory functions */
471 extern int ao_lisp_collects[2];
472 extern int ao_lisp_freed[2];
473 extern int ao_lisp_loops[2];
475 /* returns 1 if the object was already marked */
477 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
479 /* returns 1 if the object was already marked */
481 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr);
484 ao_lisp_move_map(void *addr);
486 /* returns 1 if the object was already moved */
488 ao_lisp_move(const struct ao_lisp_type *type, void **ref);
490 /* returns 1 if the object was already moved */
492 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref);
495 ao_lisp_alloc(int size);
497 #define AO_LISP_COLLECT_FULL 1
498 #define AO_LISP_COLLECT_INCREMENTAL 0
501 ao_lisp_collect(uint8_t style);
504 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons);
506 struct ao_lisp_cons *
507 ao_lisp_cons_fetch(int id);
510 ao_lisp_poly_stash(int id, ao_poly poly);
513 ao_lisp_poly_fetch(int id);
516 ao_lisp_string_stash(int id, char *string);
519 ao_lisp_string_fetch(int id);
522 ao_lisp_stack_stash(int id, struct ao_lisp_stack *stack) {
523 ao_lisp_poly_stash(id, ao_lisp_stack_poly(stack));
526 static inline struct ao_lisp_stack *
527 ao_lisp_stack_fetch(int id) {
528 return ao_lisp_poly_stack(ao_lisp_poly_fetch(id));
533 extern const struct ao_lisp_type ao_lisp_bool_type;
536 ao_lisp_bool_write(ao_poly v);
538 #ifdef AO_LISP_MAKE_CONST
539 struct ao_lisp_bool *ao_lisp_true, *ao_lisp_false;
541 struct ao_lisp_bool *
542 ao_lisp_bool_get(uint8_t value);
546 extern const struct ao_lisp_type ao_lisp_cons_type;
548 struct ao_lisp_cons *
549 ao_lisp_cons_cons(ao_poly car, ao_poly cdr);
551 /* Return a cons or NULL for a proper list, else error */
552 struct ao_lisp_cons *
553 ao_lisp_cons_cdr(struct ao_lisp_cons *cons);
556 ao_lisp__cons(ao_poly car, ao_poly cdr);
558 extern struct ao_lisp_cons *ao_lisp_cons_free_list;
561 ao_lisp_cons_free(struct ao_lisp_cons *cons);
564 ao_lisp_cons_write(ao_poly);
567 ao_lisp_cons_display(ao_poly);
570 ao_lisp_cons_length(struct ao_lisp_cons *cons);
573 extern const struct ao_lisp_type ao_lisp_string_type;
576 ao_lisp_string_copy(char *a);
579 ao_lisp_string_cat(char *a, char *b);
582 ao_lisp_string_pack(struct ao_lisp_cons *cons);
585 ao_lisp_string_unpack(char *a);
588 ao_lisp_string_write(ao_poly s);
591 ao_lisp_string_display(ao_poly s);
594 extern const struct ao_lisp_type ao_lisp_atom_type;
596 extern struct ao_lisp_atom *ao_lisp_atoms;
597 extern struct ao_lisp_frame *ao_lisp_frame_global;
598 extern struct ao_lisp_frame *ao_lisp_frame_current;
601 ao_lisp_atom_write(ao_poly a);
603 struct ao_lisp_atom *
604 ao_lisp_atom_intern(char *name);
607 ao_lisp_atom_ref(struct ao_lisp_frame *frame, ao_poly atom);
610 ao_lisp_atom_get(ao_poly atom);
613 ao_lisp_atom_set(ao_poly atom, ao_poly val);
617 ao_lisp_int_write(ao_poly i);
620 ao_lisp_poly_integer(ao_poly p);
623 ao_lisp_integer_poly(int32_t i);
626 ao_lisp_integer_typep(uint8_t t)
628 return (t == AO_LISP_INT) || (t == AO_LISP_BIGINT);
632 ao_lisp_bigint_write(ao_poly i);
634 extern const struct ao_lisp_type ao_lisp_bigint_type;
637 ao_lisp_poly_write(ao_poly p);
640 ao_lisp_poly_display(ao_poly p);
643 ao_lisp_poly_mark(ao_poly p, uint8_t note_cons);
645 /* returns 1 if the object has already been moved */
647 ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
652 ao_lisp_eval_clear_globals(void);
655 ao_lisp_eval_restart(void);
658 ao_lisp_eval(ao_poly p);
661 ao_lisp_set_cond(struct ao_lisp_cons *cons);
664 extern const struct ao_lisp_type ao_lisp_float_type;
667 ao_lisp_float_write(ao_poly p);
670 ao_lisp_float_get(float value);
672 static inline uint8_t
673 ao_lisp_number_typep(uint8_t t)
675 return ao_lisp_integer_typep(t) || (t == AO_LISP_FLOAT);
679 ao_lisp_poly_number(ao_poly p);
683 ao_lisp_builtin_write(ao_poly b);
685 extern const struct ao_lisp_type ao_lisp_builtin_type;
687 /* Check argument count */
689 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
691 /* Check argument type */
693 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
695 /* Fetch an arg (nil if off the end) */
697 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
700 ao_lisp_args_name(uint8_t args);
703 extern struct ao_lisp_cons *ao_lisp_read_cons;
704 extern struct ao_lisp_cons *ao_lisp_read_cons_tail;
705 extern struct ao_lisp_cons *ao_lisp_read_stack;
712 ao_lisp_read_eval_print(void);
715 extern const struct ao_lisp_type ao_lisp_frame_type;
717 #define AO_LISP_FRAME_FREE 6
719 extern struct ao_lisp_frame *ao_lisp_frame_free_list[AO_LISP_FRAME_FREE];
722 ao_lisp_frame_mark(struct ao_lisp_frame *frame);
725 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
727 struct ao_lisp_frame *
728 ao_lisp_frame_new(int num);
731 ao_lisp_frame_free(struct ao_lisp_frame *frame);
734 ao_lisp_frame_bind(struct ao_lisp_frame *frame, int num, ao_poly atom, ao_poly val);
737 ao_lisp_frame_add(struct ao_lisp_frame **frame, ao_poly atom, ao_poly val);
740 ao_lisp_frame_write(ao_poly p);
743 extern const struct ao_lisp_type ao_lisp_lambda_type;
745 extern const char *ao_lisp_state_names[];
747 struct ao_lisp_lambda *
748 ao_lisp_lambda_new(ao_poly cons);
751 ao_lisp_lambda_write(ao_poly lambda);
754 ao_lisp_lambda_eval(void);
758 extern const struct ao_lisp_type ao_lisp_stack_type;
759 extern struct ao_lisp_stack *ao_lisp_stack;
760 extern struct ao_lisp_stack *ao_lisp_stack_free_list;
763 ao_lisp_stack_reset(struct ao_lisp_stack *stack);
766 ao_lisp_stack_push(void);
769 ao_lisp_stack_pop(void);
772 ao_lisp_stack_clear(void);
775 ao_lisp_stack_write(ao_poly stack);
778 ao_lisp_stack_eval(void);
783 ao_lisp_error_poly(char *name, ao_poly poly, ao_poly last);
786 ao_lisp_error_frame(int indent, char *name, struct ao_lisp_frame *frame);
789 ao_lisp_error(int error, char *format, ...);
793 #define AO_LISP_BUILTIN_DECLS
794 #include "ao_lisp_builtin.h"
796 /* debugging macros */
800 int ao_lisp_stack_depth;
802 #define DBG_INDENT() do { int _s; for(_s = 0; _s < ao_lisp_stack_depth; _s++) printf(" "); } while(0)
803 #define DBG_IN() (++ao_lisp_stack_depth)
804 #define DBG_OUT() (--ao_lisp_stack_depth)
805 #define DBG_RESET() (ao_lisp_stack_depth = 0)
806 #define DBG(...) printf(__VA_ARGS__)
807 #define DBGI(...) do { DBG("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
808 #define DBG_CONS(a) ao_lisp_cons_write(ao_lisp_cons_poly(a))
809 #define DBG_POLY(a) ao_lisp_poly_write(a)
810 #define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_lisp_pool) : -1)
811 #define DBG_STACK() ao_lisp_stack_write(ao_lisp_stack_poly(ao_lisp_stack))
813 ao_lisp_frames_dump(void)
815 struct ao_lisp_stack *s;
816 DBGI(".. current frame: "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
817 for (s = ao_lisp_stack; s; s = ao_lisp_poly_stack(s->prev)) {
818 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
821 #define DBG_FRAMES() ao_lisp_frames_dump()
836 #define DBG_MEM_START 1
841 extern int dbg_move_depth;
843 #define MDBG_OFFSET(a) ((int) ((uint8_t *) (a) - ao_lisp_pool))
848 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
849 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
850 #define MDBG_MOVE_IN() (dbg_move_depth++)
851 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
856 #define MDBG_MOVE(...)
857 #define MDBG_MORE(...)
858 #define MDBG_MOVE_IN()
859 #define MDBG_MOVE_OUT()
863 #endif /* _AO_LISP_H_ */