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>
25 typedef uint16_t ao_poly;
26 typedef int16_t ao_signed_poly;
30 struct ao_lisp_os_save {
33 uint16_t const_checksum;
34 uint16_t const_checksum_inv;
37 #define AO_LISP_POOL_EXTRA (sizeof(struct ao_lisp_os_save))
38 #define AO_LISP_POOL ((int) (AO_LISP_POOL_TOTAL - AO_LISP_POOL_EXTRA))
41 ao_lisp_os_save(void);
44 ao_lisp_os_restore_save(struct ao_lisp_os_save *save, int offset);
47 ao_lisp_os_restore(void);
51 #ifdef AO_LISP_MAKE_CONST
52 #define AO_LISP_POOL_CONST 16384
53 extern uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));
54 #define ao_lisp_pool ao_lisp_const
55 #define AO_LISP_POOL AO_LISP_POOL_CONST
57 #define _atom(n) ao_lisp_atom_poly(ao_lisp_atom_intern(n))
58 #define _bool(v) ao_lisp_bool_poly(ao_lisp_bool_get(v))
60 #define _ao_lisp_bool_true _bool(1)
61 #define _ao_lisp_bool_false _bool(0)
63 #define _ao_lisp_atom_eof _atom("eof")
64 #define _ao_lisp_atom_else _atom("else")
66 #define AO_LISP_BUILTIN_ATOMS
67 #include "ao_lisp_builtin.h"
70 #include "ao_lisp_const.h"
72 #define AO_LISP_POOL 3072
74 extern uint8_t ao_lisp_pool[AO_LISP_POOL + AO_LISP_POOL_EXTRA] __attribute__((aligned(4)));
78 #define AO_LISP_CONS 0
80 #define AO_LISP_STRING 2
81 #define AO_LISP_OTHER 3
83 #define AO_LISP_TYPE_MASK 0x0003
84 #define AO_LISP_TYPE_SHIFT 2
85 #define AO_LISP_REF_MASK 0x7ffc
86 #define AO_LISP_CONST 0x8000
88 /* These have a type value at the start of the struct */
89 #define AO_LISP_ATOM 4
90 #define AO_LISP_BUILTIN 5
91 #define AO_LISP_FRAME 6
92 #define AO_LISP_LAMBDA 7
93 #define AO_LISP_STACK 8
94 #define AO_LISP_BOOL 9
95 #define AO_LISP_NUM_TYPE 10
97 /* Leave two bits for types to use as they please */
98 #define AO_LISP_OTHER_TYPE_MASK 0x3f
100 #define AO_LISP_NIL 0
102 extern uint16_t ao_lisp_top;
104 #define AO_LISP_OOM 0x01
105 #define AO_LISP_DIVIDE_BY_ZERO 0x02
106 #define AO_LISP_INVALID 0x04
107 #define AO_LISP_UNDEFINED 0x08
108 #define AO_LISP_EOF 0x10
109 #define AO_LISP_EXIT 0x20
111 extern uint8_t ao_lisp_exception;
114 ao_lisp_is_const(ao_poly poly) {
115 return poly & AO_LISP_CONST;
118 #define AO_LISP_IS_CONST(a) (ao_lisp_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_const + AO_LISP_POOL_CONST)
119 #define AO_LISP_IS_POOL(a) (ao_lisp_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_pool + AO_LISP_POOL)
120 #define AO_LISP_IS_INT(p) (ao_lisp_poly_base_type(p) == AO_LISP_INT)
123 ao_lisp_ref(ao_poly poly);
126 ao_lisp_poly(const void *addr, ao_poly type);
128 struct ao_lisp_type {
129 int (*size)(void *addr);
130 void (*mark)(void *addr);
131 void (*move)(void *addr);
135 struct ao_lisp_cons {
140 struct ao_lisp_atom {
152 struct ao_lisp_frame {
156 struct ao_lisp_val vals[];
159 struct ao_lisp_bool {
165 /* Set on type when the frame escapes the lambda */
166 #define AO_LISP_FRAME_MARK 0x80
167 #define AO_LISP_FRAME_PRINT 0x40
169 static inline int ao_lisp_frame_marked(struct ao_lisp_frame *f) {
170 return f->type & AO_LISP_FRAME_MARK;
173 static inline struct ao_lisp_frame *
174 ao_lisp_poly_frame(ao_poly poly) {
175 return ao_lisp_ref(poly);
178 static inline ao_poly
179 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
180 return ao_lisp_poly(frame, AO_LISP_OTHER);
184 eval_sexpr, /* Evaluate an sexpr */
185 eval_val, /* Value computed */
186 eval_formal, /* Formal computed */
187 eval_exec, /* Start a lambda evaluation */
188 eval_apply, /* Execute apply */
189 eval_cond, /* Start next cond clause */
190 eval_cond_test, /* Check cond condition */
191 eval_progn, /* Start next progn entry */
192 eval_while, /* Start while condition */
193 eval_while_test, /* Check while condition */
194 eval_macro, /* Finished with macro generation */
197 struct ao_lisp_stack {
198 uint8_t type; /* AO_LISP_STACK */
199 uint8_t state; /* enum eval_state */
200 ao_poly prev; /* previous stack frame */
201 ao_poly sexprs; /* expressions to evaluate */
202 ao_poly values; /* values computed */
203 ao_poly values_tail; /* end of the values list for easy appending */
204 ao_poly frame; /* current lookup frame */
205 ao_poly list; /* most recent function call */
208 #define AO_LISP_STACK_MARK 0x80 /* set on type when a reference has been taken */
209 #define AO_LISP_STACK_PRINT 0x40 /* stack is being printed */
211 static inline int ao_lisp_stack_marked(struct ao_lisp_stack *s) {
212 return s->type & AO_LISP_STACK_MARK;
215 static inline void ao_lisp_stack_mark(struct ao_lisp_stack *s) {
216 s->type |= AO_LISP_STACK_MARK;
219 static inline struct ao_lisp_stack *
220 ao_lisp_poly_stack(ao_poly p)
222 return ao_lisp_ref(p);
225 static inline ao_poly
226 ao_lisp_stack_poly(struct ao_lisp_stack *stack)
228 return ao_lisp_poly(stack, AO_LISP_OTHER);
231 extern ao_poly ao_lisp_v;
233 #define AO_LISP_FUNC_LAMBDA 0
234 #define AO_LISP_FUNC_NLAMBDA 1
235 #define AO_LISP_FUNC_MACRO 2
236 #define AO_LISP_FUNC_LEXPR 3
238 #define AO_LISP_FUNC_FREE_ARGS 0x80
239 #define AO_LISP_FUNC_MASK 0x7f
241 #define AO_LISP_FUNC_F_LAMBDA (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LAMBDA)
242 #define AO_LISP_FUNC_F_NLAMBDA (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_NLAMBDA)
243 #define AO_LISP_FUNC_F_MACRO (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_MACRO)
244 #define AO_LISP_FUNC_F_LEXPR (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LEXPR)
246 struct ao_lisp_builtin {
252 #define AO_LISP_BUILTIN_ID
253 #include "ao_lisp_builtin.h"
255 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
257 extern const ao_lisp_func_t ao_lisp_builtins[];
259 static inline ao_lisp_func_t
260 ao_lisp_func(struct ao_lisp_builtin *b)
262 return ao_lisp_builtins[b->func];
265 struct ao_lisp_lambda {
272 static inline struct ao_lisp_lambda *
273 ao_lisp_poly_lambda(ao_poly poly)
275 return ao_lisp_ref(poly);
278 static inline ao_poly
279 ao_lisp_lambda_poly(struct ao_lisp_lambda *lambda)
281 return ao_lisp_poly(lambda, AO_LISP_OTHER);
285 ao_lisp_poly_other(ao_poly poly) {
286 return ao_lisp_ref(poly);
289 static inline uint8_t
290 ao_lisp_other_type(void *other) {
292 if ((*((uint8_t *) other) & AO_LISP_OTHER_TYPE_MASK) >= AO_LISP_NUM_TYPE)
295 return *((uint8_t *) other) & AO_LISP_OTHER_TYPE_MASK;
298 static inline ao_poly
299 ao_lisp_other_poly(const void *other)
301 return ao_lisp_poly(other, AO_LISP_OTHER);
305 ao_lisp_size_round(int size)
307 return (size + 3) & ~3;
311 ao_lisp_size(const struct ao_lisp_type *type, void *addr)
313 return ao_lisp_size_round(type->size(addr));
316 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
318 static inline int ao_lisp_poly_base_type(ao_poly poly) {
319 return poly & AO_LISP_TYPE_MASK;
322 static inline int ao_lisp_poly_type(ao_poly poly) {
323 int type = poly & AO_LISP_TYPE_MASK;
324 if (type == AO_LISP_OTHER)
325 return ao_lisp_other_type(ao_lisp_poly_other(poly));
329 static inline struct ao_lisp_cons *
330 ao_lisp_poly_cons(ao_poly poly)
332 return ao_lisp_ref(poly);
335 static inline ao_poly
336 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
338 return ao_lisp_poly(cons, AO_LISP_CONS);
342 ao_lisp_poly_int(ao_poly poly)
344 return (int) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
347 static inline ao_poly
348 ao_lisp_int_poly(int i)
350 return ((ao_poly) i << 2) | AO_LISP_INT;
354 ao_lisp_poly_string(ao_poly poly)
356 return ao_lisp_ref(poly);
359 static inline ao_poly
360 ao_lisp_string_poly(char *s)
362 return ao_lisp_poly(s, AO_LISP_STRING);
365 static inline struct ao_lisp_atom *
366 ao_lisp_poly_atom(ao_poly poly)
368 return ao_lisp_ref(poly);
371 static inline ao_poly
372 ao_lisp_atom_poly(struct ao_lisp_atom *a)
374 return ao_lisp_poly(a, AO_LISP_OTHER);
377 static inline struct ao_lisp_builtin *
378 ao_lisp_poly_builtin(ao_poly poly)
380 return ao_lisp_ref(poly);
383 static inline ao_poly
384 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
386 return ao_lisp_poly(b, AO_LISP_OTHER);
389 static inline ao_poly
390 ao_lisp_bool_poly(struct ao_lisp_bool *b)
392 return ao_lisp_poly(b, AO_LISP_OTHER);
395 static inline struct ao_lisp_bool *
396 ao_lisp_poly_bool(ao_poly poly)
398 return ao_lisp_ref(poly);
400 /* memory functions */
402 extern int ao_lisp_collects[2];
403 extern int ao_lisp_freed[2];
404 extern int ao_lisp_loops[2];
406 /* returns 1 if the object was already marked */
408 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
410 /* returns 1 if the object was already marked */
412 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr);
415 ao_lisp_move_map(void *addr);
417 /* returns 1 if the object was already moved */
419 ao_lisp_move(const struct ao_lisp_type *type, void **ref);
421 /* returns 1 if the object was already moved */
423 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref);
426 ao_lisp_alloc(int size);
428 #define AO_LISP_COLLECT_FULL 1
429 #define AO_LISP_COLLECT_INCREMENTAL 0
432 ao_lisp_collect(uint8_t style);
435 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons);
437 struct ao_lisp_cons *
438 ao_lisp_cons_fetch(int id);
441 ao_lisp_poly_stash(int id, ao_poly poly);
444 ao_lisp_poly_fetch(int id);
447 ao_lisp_string_stash(int id, char *string);
450 ao_lisp_string_fetch(int id);
453 ao_lisp_stack_stash(int id, struct ao_lisp_stack *stack) {
454 ao_lisp_poly_stash(id, ao_lisp_stack_poly(stack));
457 static inline struct ao_lisp_stack *
458 ao_lisp_stack_fetch(int id) {
459 return ao_lisp_poly_stack(ao_lisp_poly_fetch(id));
464 extern const struct ao_lisp_type ao_lisp_bool_type;
467 ao_lisp_bool_write(ao_poly v);
469 #ifdef AO_LISP_MAKE_CONST
470 struct ao_lisp_bool *ao_lisp_true, *ao_lisp_false;
472 struct ao_lisp_bool *
473 ao_lisp_bool_get(uint8_t value);
477 extern const struct ao_lisp_type ao_lisp_cons_type;
479 struct ao_lisp_cons *
480 ao_lisp_cons_cons(ao_poly car, ao_poly cdr);
483 ao_lisp__cons(ao_poly car, ao_poly cdr);
485 extern struct ao_lisp_cons *ao_lisp_cons_free_list;
488 ao_lisp_cons_free(struct ao_lisp_cons *cons);
491 ao_lisp_cons_write(ao_poly);
494 ao_lisp_cons_display(ao_poly);
497 ao_lisp_cons_length(struct ao_lisp_cons *cons);
500 extern const struct ao_lisp_type ao_lisp_string_type;
503 ao_lisp_string_copy(char *a);
506 ao_lisp_string_cat(char *a, char *b);
509 ao_lisp_string_pack(struct ao_lisp_cons *cons);
512 ao_lisp_string_unpack(char *a);
515 ao_lisp_string_write(ao_poly s);
518 ao_lisp_string_display(ao_poly s);
521 extern const struct ao_lisp_type ao_lisp_atom_type;
523 extern struct ao_lisp_atom *ao_lisp_atoms;
524 extern struct ao_lisp_frame *ao_lisp_frame_global;
525 extern struct ao_lisp_frame *ao_lisp_frame_current;
528 ao_lisp_atom_write(ao_poly a);
530 struct ao_lisp_atom *
531 ao_lisp_atom_intern(char *name);
534 ao_lisp_atom_ref(struct ao_lisp_frame *frame, ao_poly atom);
537 ao_lisp_atom_get(ao_poly atom);
540 ao_lisp_atom_set(ao_poly atom, ao_poly val);
544 ao_lisp_int_write(ao_poly i);
548 ao_lisp_poly_write(ao_poly p);
551 ao_lisp_poly_display(ao_poly p);
554 ao_lisp_poly_mark(ao_poly p, uint8_t note_cons);
556 /* returns 1 if the object has already been moved */
558 ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
563 ao_lisp_eval_clear_globals(void);
566 ao_lisp_eval_restart(void);
569 ao_lisp_eval(ao_poly p);
572 ao_lisp_set_cond(struct ao_lisp_cons *cons);
576 ao_lisp_builtin_write(ao_poly b);
578 extern const struct ao_lisp_type ao_lisp_builtin_type;
580 /* Check argument count */
582 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
584 /* Check argument type */
586 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
588 /* Fetch an arg (nil if off the end) */
590 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
593 ao_lisp_args_name(uint8_t args);
596 extern struct ao_lisp_cons *ao_lisp_read_cons;
597 extern struct ao_lisp_cons *ao_lisp_read_cons_tail;
598 extern struct ao_lisp_cons *ao_lisp_read_stack;
605 ao_lisp_read_eval_print(void);
608 extern const struct ao_lisp_type ao_lisp_frame_type;
610 #define AO_LISP_FRAME_FREE 6
612 extern struct ao_lisp_frame *ao_lisp_frame_free_list[AO_LISP_FRAME_FREE];
615 ao_lisp_frame_mark(struct ao_lisp_frame *frame);
618 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
620 struct ao_lisp_frame *
621 ao_lisp_frame_new(int num);
624 ao_lisp_frame_free(struct ao_lisp_frame *frame);
627 ao_lisp_frame_bind(struct ao_lisp_frame *frame, int num, ao_poly atom, ao_poly val);
630 ao_lisp_frame_add(struct ao_lisp_frame **frame, ao_poly atom, ao_poly val);
633 ao_lisp_frame_write(ao_poly p);
636 extern const struct ao_lisp_type ao_lisp_lambda_type;
638 extern const char *ao_lisp_state_names[];
640 struct ao_lisp_lambda *
641 ao_lisp_lambda_new(ao_poly cons);
644 ao_lisp_lambda_write(ao_poly lambda);
647 ao_lisp_lambda_eval(void);
651 extern const struct ao_lisp_type ao_lisp_stack_type;
652 extern struct ao_lisp_stack *ao_lisp_stack;
653 extern struct ao_lisp_stack *ao_lisp_stack_free_list;
656 ao_lisp_stack_reset(struct ao_lisp_stack *stack);
659 ao_lisp_stack_push(void);
662 ao_lisp_stack_pop(void);
665 ao_lisp_stack_clear(void);
668 ao_lisp_stack_write(ao_poly stack);
671 ao_lisp_stack_eval(void);
676 ao_lisp_error_poly(char *name, ao_poly poly, ao_poly last);
679 ao_lisp_error_frame(int indent, char *name, struct ao_lisp_frame *frame);
682 ao_lisp_error(int error, char *format, ...);
686 #define AO_LISP_BUILTIN_DECLS
687 #include "ao_lisp_builtin.h"
689 /* debugging macros */
693 int ao_lisp_stack_depth;
695 #define DBG_INDENT() do { int _s; for(_s = 0; _s < ao_lisp_stack_depth; _s++) printf(" "); } while(0)
696 #define DBG_IN() (++ao_lisp_stack_depth)
697 #define DBG_OUT() (--ao_lisp_stack_depth)
698 #define DBG_RESET() (ao_lisp_stack_depth = 0)
699 #define DBG(...) printf(__VA_ARGS__)
700 #define DBGI(...) do { DBG("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
701 #define DBG_CONS(a) ao_lisp_cons_write(ao_lisp_cons_poly(a))
702 #define DBG_POLY(a) ao_lisp_poly_write(a)
703 #define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_lisp_pool) : -1)
704 #define DBG_STACK() ao_lisp_stack_write(ao_lisp_stack_poly(ao_lisp_stack))
706 ao_lisp_frames_dump(void)
708 struct ao_lisp_stack *s;
709 DBGI(".. current frame: "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
710 for (s = ao_lisp_stack; s; s = ao_lisp_poly_stack(s->prev)) {
711 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
714 #define DBG_FRAMES() ao_lisp_frames_dump()
729 #define DBG_MEM_START 1
734 extern int dbg_move_depth;
736 #define MDBG_OFFSET(a) ((int) ((uint8_t *) (a) - ao_lisp_pool))
741 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
742 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
743 #define MDBG_MOVE_IN() (dbg_move_depth++)
744 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
749 #define MDBG_MOVE(...)
750 #define MDBG_MORE(...)
751 #define MDBG_MOVE_IN()
752 #define MDBG_MOVE_OUT()
756 #endif /* _AO_LISP_H_ */