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.
21 #include <ao_lisp_os.h>
23 typedef uint16_t ao_poly;
24 typedef int16_t ao_signed_poly;
28 struct ao_lisp_os_save {
31 uint16_t const_checksum;
32 uint16_t const_checksum_inv;
35 #define AO_LISP_POOL_EXTRA (sizeof(struct ao_lisp_os_save))
36 #define AO_LISP_POOL ((int) (AO_LISP_POOL_TOTAL - AO_LISP_POOL_EXTRA))
39 ao_lisp_os_save(void);
42 ao_lisp_os_restore_save(struct ao_lisp_os_save *save, int offset);
45 ao_lisp_os_restore(void);
49 #ifdef AO_LISP_MAKE_CONST
50 #define AO_LISP_POOL_CONST 16384
51 extern uint8_t ao_lisp_const[AO_LISP_POOL_CONST];
52 #define ao_lisp_pool ao_lisp_const
53 #define AO_LISP_POOL AO_LISP_POOL_CONST
55 #define _atom(n) ao_lisp_atom_poly(ao_lisp_atom_intern(n))
57 #define _ao_lisp_atom_quote _atom("quote")
58 #define _ao_lisp_atom_set _atom("set")
59 #define _ao_lisp_atom_setq _atom("setq")
60 #define _ao_lisp_atom_t _atom("t")
61 #define _ao_lisp_atom_car _atom("car")
62 #define _ao_lisp_atom_cdr _atom("cdr")
63 #define _ao_lisp_atom_cons _atom("cons")
64 #define _ao_lisp_atom_last _atom("last")
65 #define _ao_lisp_atom_length _atom("length")
66 #define _ao_lisp_atom_cond _atom("cond")
67 #define _ao_lisp_atom_lambda _atom("lambda")
68 #define _ao_lisp_atom_led _atom("led")
69 #define _ao_lisp_atom_delay _atom("delay")
70 #define _ao_lisp_atom_pack _atom("pack")
71 #define _ao_lisp_atom_unpack _atom("unpack")
72 #define _ao_lisp_atom_flush _atom("flush")
73 #define _ao_lisp_atom_eval _atom("eval")
74 #define _ao_lisp_atom_read _atom("read")
75 #define _ao_lisp_atom_eof _atom("eof")
76 #define _ao_lisp_atom_save _atom("save")
77 #define _ao_lisp_atom_restore _atom("restore")
79 #include "ao_lisp_const.h"
81 #define AO_LISP_POOL 3072
83 extern uint8_t ao_lisp_pool[AO_LISP_POOL + AO_LISP_POOL_EXTRA];
87 #define AO_LISP_CONS 0
89 #define AO_LISP_STRING 2
90 #define AO_LISP_OTHER 3
92 #define AO_LISP_TYPE_MASK 0x0003
93 #define AO_LISP_TYPE_SHIFT 2
94 #define AO_LISP_REF_MASK 0x7ffc
95 #define AO_LISP_CONST 0x8000
97 /* These have a type value at the start of the struct */
98 #define AO_LISP_ATOM 4
99 #define AO_LISP_BUILTIN 5
100 #define AO_LISP_FRAME 6
101 #define AO_LISP_LAMBDA 7
102 #define AO_LISP_NUM_TYPE 8
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
114 extern uint8_t ao_lisp_exception;
117 ao_lisp_is_const(ao_poly poly) {
118 return poly & AO_LISP_CONST;
121 #define AO_LISP_POOL_BASE (ao_lisp_pool - 4)
122 #define AO_LISP_CONST_BASE (ao_lisp_const - 4)
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)
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 static inline struct ao_lisp_frame *
165 ao_lisp_poly_frame(ao_poly poly) {
166 return ao_lisp_ref(poly);
169 static inline ao_poly
170 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
171 return ao_lisp_poly(frame, AO_LISP_OTHER);
175 eval_sexpr, /* Evaluate an sexpr */
186 struct ao_lisp_stack {
187 uint8_t state; /* enum eval_state */
188 ao_poly prev; /* previous stack frame */
189 ao_poly sexprs; /* expressions to evaluate */
190 ao_poly values; /* values computed */
191 ao_poly values_tail; /* end of the values list for easy appending */
192 ao_poly frame; /* current lookup frame */
193 ao_poly list; /* most recent function call */
196 static inline struct ao_lisp_stack *
197 ao_lisp_poly_stack(ao_poly p)
199 return ao_lisp_ref(p);
202 static inline ao_poly
203 ao_lisp_stack_poly(struct ao_lisp_stack *stack)
205 return ao_lisp_poly(stack, AO_LISP_OTHER);
208 extern struct ao_lisp_stack *ao_lisp_stack;
209 extern struct ao_lisp_stack *ao_lisp_stack_free_list;
210 extern ao_poly ao_lisp_v;
212 #define AO_LISP_FUNC_LAMBDA 0
213 #define AO_LISP_FUNC_NLAMBDA 1
214 #define AO_LISP_FUNC_MACRO 2
215 #define AO_LISP_FUNC_LEXPR 3
217 #define AO_LISP_FUNC_FREE_ARGS 0x80
218 #define AO_LISP_FUNC_MASK 0x7f
220 #define AO_LISP_FUNC_F_LAMBDA (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LAMBDA)
221 #define AO_LISP_FUNC_F_NLAMBDA (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_NLAMBDA)
222 #define AO_LISP_FUNC_F_MACRO (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_MACRO)
223 #define AO_LISP_FUNC_F_LEXPR (AO_LISP_FUNC_FREE_ARGS | AO_LISP_FUNC_LEXPR)
225 struct ao_lisp_builtin {
231 enum ao_lisp_builtin_id {
260 builtin_greater_equal,
271 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
273 extern const ao_lisp_func_t ao_lisp_builtins[];
275 static inline ao_lisp_func_t
276 ao_lisp_func(struct ao_lisp_builtin *b)
278 return ao_lisp_builtins[b->func];
281 struct ao_lisp_lambda {
288 static inline struct ao_lisp_lambda *
289 ao_lisp_poly_lambda(ao_poly poly)
291 return ao_lisp_ref(poly);
294 static inline ao_poly
295 ao_lisp_lambda_poly(struct ao_lisp_lambda *lambda)
297 return ao_lisp_poly(lambda, AO_LISP_OTHER);
301 ao_lisp_poly_other(ao_poly poly) {
302 return ao_lisp_ref(poly);
305 static inline uint8_t
306 ao_lisp_other_type(void *other) {
307 return *((uint8_t *) other);
310 static inline ao_poly
311 ao_lisp_other_poly(const void *other)
313 return ao_lisp_poly(other, AO_LISP_OTHER);
317 ao_lisp_size_round(int size)
319 return (size + 3) & ~3;
323 ao_lisp_size(const struct ao_lisp_type *type, void *addr)
325 return ao_lisp_size_round(type->size(addr));
328 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
330 static inline int ao_lisp_poly_base_type(ao_poly poly) {
331 return poly & AO_LISP_TYPE_MASK;
334 static inline int ao_lisp_poly_type(ao_poly poly) {
335 int type = poly & AO_LISP_TYPE_MASK;
336 if (type == AO_LISP_OTHER)
337 return ao_lisp_other_type(ao_lisp_poly_other(poly));
341 static inline struct ao_lisp_cons *
342 ao_lisp_poly_cons(ao_poly poly)
344 return ao_lisp_ref(poly);
347 static inline ao_poly
348 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
350 return ao_lisp_poly(cons, AO_LISP_CONS);
354 ao_lisp_poly_int(ao_poly poly)
356 return (int) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
359 static inline ao_poly
360 ao_lisp_int_poly(int i)
362 return ((ao_poly) i << 2) | AO_LISP_INT;
366 ao_lisp_poly_string(ao_poly poly)
368 return ao_lisp_ref(poly);
371 static inline ao_poly
372 ao_lisp_string_poly(char *s)
374 return ao_lisp_poly(s, AO_LISP_STRING);
377 static inline struct ao_lisp_atom *
378 ao_lisp_poly_atom(ao_poly poly)
380 return ao_lisp_ref(poly);
383 static inline ao_poly
384 ao_lisp_atom_poly(struct ao_lisp_atom *a)
386 return ao_lisp_poly(a, AO_LISP_OTHER);
389 static inline struct ao_lisp_builtin *
390 ao_lisp_poly_builtin(ao_poly poly)
392 return ao_lisp_ref(poly);
395 static inline ao_poly
396 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
398 return ao_lisp_poly(b, AO_LISP_OTHER);
401 /* memory functions */
403 extern int ao_lisp_collects;
405 /* returns 1 if the object was already marked */
407 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
409 /* returns 1 if the object was already marked */
411 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr);
414 ao_lisp_move_map(void *addr);
416 /* returns 1 if the object was already moved */
418 ao_lisp_move(const struct ao_lisp_type *type, void **ref);
420 /* returns 1 if the object was already moved */
422 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref);
425 ao_lisp_alloc(int size);
428 ao_lisp_collect(void);
431 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons);
433 struct ao_lisp_cons *
434 ao_lisp_cons_fetch(int id);
437 ao_lisp_string_stash(int id, char *string);
440 ao_lisp_string_fetch(int id);
443 ao_lisp_poly_stash(int id, ao_poly poly);
446 ao_lisp_poly_fetch(int id);
449 extern const struct ao_lisp_type ao_lisp_cons_type;
451 struct ao_lisp_cons *
452 ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr);
454 extern struct ao_lisp_cons *ao_lisp_cons_free_list;
457 ao_lisp_cons_free(struct ao_lisp_cons *cons);
460 ao_lisp_cons_print(ao_poly);
463 ao_lisp_cons_patom(ao_poly);
466 ao_lisp_cons_length(struct ao_lisp_cons *cons);
469 extern const struct ao_lisp_type ao_lisp_string_type;
472 ao_lisp_string_copy(char *a);
475 ao_lisp_string_cat(char *a, char *b);
478 ao_lisp_string_pack(struct ao_lisp_cons *cons);
481 ao_lisp_string_unpack(char *a);
484 ao_lisp_string_print(ao_poly s);
487 ao_lisp_string_patom(ao_poly s);
490 extern const struct ao_lisp_type ao_lisp_atom_type;
492 extern struct ao_lisp_atom *ao_lisp_atoms;
493 extern struct ao_lisp_frame *ao_lisp_frame_global;
494 extern struct ao_lisp_frame *ao_lisp_frame_current;
497 ao_lisp_atom_print(ao_poly a);
499 struct ao_lisp_atom *
500 ao_lisp_atom_intern(char *name);
503 ao_lisp_atom_get(ao_poly atom);
506 ao_lisp_atom_set(ao_poly atom, ao_poly val);
510 ao_lisp_int_print(ao_poly i);
514 ao_lisp_poly_print(ao_poly p);
517 ao_lisp_poly_patom(ao_poly p);
520 ao_lisp_poly_mark(ao_poly p, uint8_t note_cons);
522 /* returns 1 if the object has already been moved */
524 ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
529 ao_lisp_eval_clear_globals(void);
532 ao_lisp_eval_restart(void);
535 ao_lisp_eval(ao_poly p);
538 ao_lisp_set_cond(struct ao_lisp_cons *cons);
542 ao_lisp_builtin_print(ao_poly b);
544 extern const struct ao_lisp_type ao_lisp_builtin_type;
546 /* Check argument count */
548 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
550 /* Check argument type */
552 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
554 /* Fetch an arg (nil if off the end) */
556 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
559 ao_lisp_args_name(uint8_t args);
562 extern struct ao_lisp_cons *ao_lisp_read_cons;
563 extern struct ao_lisp_cons *ao_lisp_read_cons_tail;
564 extern struct ao_lisp_cons *ao_lisp_read_stack;
571 ao_lisp_read_eval_print(void);
574 extern const struct ao_lisp_type ao_lisp_frame_type;
577 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
579 struct ao_lisp_frame *
580 ao_lisp_frame_new(int num);
583 ao_lisp_frame_add(struct ao_lisp_frame **frame, ao_poly atom, ao_poly val);
586 ao_lisp_frame_print(ao_poly p);
589 extern const struct ao_lisp_type ao_lisp_lambda_type;
591 struct ao_lisp_lambda *
592 ao_lisp_lambda_new(ao_poly cons);
595 ao_lisp_lambda_print(ao_poly lambda);
598 ao_lisp_lambda(struct ao_lisp_cons *cons);
601 ao_lisp_lexpr(struct ao_lisp_cons *cons);
604 ao_lisp_nlambda(struct ao_lisp_cons *cons);
607 ao_lisp_macro(struct ao_lisp_cons *cons);
610 ao_lisp_lambda_eval(void);
615 ao_lisp_save(struct ao_lisp_cons *cons);
618 ao_lisp_restore(struct ao_lisp_cons *cons);
622 extern const struct ao_lisp_type ao_lisp_stack_type;
625 ao_lisp_stack_print(void);
628 ao_lisp_error(int error, char *format, ...);
630 /* debugging macros */
634 int ao_lisp_stack_depth;
636 #define DBG_INDENT() do { int _s; for(_s = 0; _s < ao_lisp_stack_depth; _s++) printf(" "); } while(0)
637 #define DBG_IN() (++ao_lisp_stack_depth)
638 #define DBG_OUT() (--ao_lisp_stack_depth)
639 #define DBG_RESET() (ao_lisp_stack_depth = 0)
640 #define DBG(...) printf(__VA_ARGS__)
641 #define DBGI(...) do { DBG("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
642 #define DBG_CONS(a) ao_lisp_cons_print(ao_lisp_cons_poly(a))
643 #define DBG_POLY(a) ao_lisp_poly_print(a)
644 #define OFFSET(a) ((a) ? (int) ((uint8_t *) a - ao_lisp_pool) : -1)
645 #define DBG_STACK() ao_lisp_stack_print()
647 ao_lisp_frames_dump(void)
649 struct ao_lisp_stack *s;
650 DBGI(".. current frame: "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
651 for (s = ao_lisp_stack; s; s = ao_lisp_poly_stack(s->prev)) {
652 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
655 #define DBG_FRAMES() ao_lisp_frames_dump()
671 #define DBG_MEM_START 1
676 extern int dbg_move_depth;
678 #define MDBG_OFFSET(a) ((int) ((uint8_t *) (a) - ao_lisp_pool))
683 #define MDBG_MOVE(...) do { if (dbg_mem) { int d; for (d = 0; d < dbg_move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
684 #define MDBG_MORE(...) do { if (dbg_mem) printf(__VA_ARGS__); } while (0)
685 #define MDBG_MOVE_IN() (dbg_move_depth++)
686 #define MDBG_MOVE_OUT() (assert(--dbg_move_depth >= 0))
691 #define MDBG_MOVE(...)
692 #define MDBG_MORE(...)
693 #define MDBG_MOVE_IN()
694 #define MDBG_MOVE_OUT()
698 #endif /* _AO_LISP_H_ */