altos/lisp: Add save/restore infrastructure. Needs OS support to work.
[fw/altos] / src / lisp / ao_lisp.h
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  */
14
15 #ifndef _AO_LISP_H_
16 #define _AO_LISP_H_
17
18 #include <stdint.h>
19 #include <string.h>
20 //#include <stdio.h>
21 #include <ao_lisp_os.h>
22
23 typedef uint16_t        ao_poly;
24 typedef int16_t         ao_signed_poly;
25
26 #ifdef AO_LISP_SAVE
27
28 struct ao_lisp_os_save {
29         ao_poly ao_lisp_atoms;
30         ao_poly ao_lisp_globals;
31 };
32
33 #define AO_LISP_POOL    (AO_LISP_POOL_TOTAL - sizeof (struct ao_lisp_os_save))
34
35 int
36 ao_lisp_os_save(void);
37
38 int
39 ao_lisp_os_restore(void);
40
41 #endif
42
43 #ifdef AO_LISP_MAKE_CONST
44 #define AO_LISP_POOL_CONST      16384
45 extern uint8_t ao_lisp_const[AO_LISP_POOL_CONST];
46 #define ao_lisp_pool ao_lisp_const
47 #define AO_LISP_POOL AO_LISP_POOL_CONST
48
49 #define _atom(n) ao_lisp_atom_poly(ao_lisp_atom_intern(n))
50
51 #define _ao_lisp_atom_quote     _atom("quote")
52 #define _ao_lisp_atom_set       _atom("set")
53 #define _ao_lisp_atom_setq      _atom("setq")
54 #define _ao_lisp_atom_t         _atom("t")
55 #define _ao_lisp_atom_car       _atom("car")
56 #define _ao_lisp_atom_cdr       _atom("cdr")
57 #define _ao_lisp_atom_cons      _atom("cons")
58 #define _ao_lisp_atom_last      _atom("last")
59 #define _ao_lisp_atom_length    _atom("length")
60 #define _ao_lisp_atom_cond      _atom("cond")
61 #define _ao_lisp_atom_lambda    _atom("lambda")
62 #define _ao_lisp_atom_led       _atom("led")
63 #define _ao_lisp_atom_delay     _atom("delay")
64 #define _ao_lisp_atom_pack      _atom("pack")
65 #define _ao_lisp_atom_unpack    _atom("unpack")
66 #define _ao_lisp_atom_flush     _atom("flush")
67 #define _ao_lisp_atom_eval      _atom("eval")
68 #define _ao_lisp_atom_read      _atom("read")
69 #define _ao_lisp_atom_eof       _atom("eof")
70 #else
71 #include "ao_lisp_const.h"
72 #ifndef AO_LISP_POOL
73 #define AO_LISP_POOL    16384
74 #endif
75 extern uint8_t          ao_lisp_pool[AO_LISP_POOL];
76 #endif
77
78 /* Primitive types */
79 #define AO_LISP_CONS            0
80 #define AO_LISP_INT             1
81 #define AO_LISP_STRING          2
82 #define AO_LISP_OTHER           3
83
84 #define AO_LISP_TYPE_MASK       0x0003
85 #define AO_LISP_TYPE_SHIFT      2
86 #define AO_LISP_REF_MASK        0x7ffc
87 #define AO_LISP_CONST           0x8000
88
89 /* These have a type value at the start of the struct */
90 #define AO_LISP_ATOM            4
91 #define AO_LISP_BUILTIN         5
92 #define AO_LISP_FRAME           6
93 #define AO_LISP_LAMBDA          7
94 #define AO_LISP_NUM_TYPE        8
95
96 #define AO_LISP_NIL     0
97
98 extern uint16_t         ao_lisp_top;
99
100 #define AO_LISP_OOM             0x01
101 #define AO_LISP_DIVIDE_BY_ZERO  0x02
102 #define AO_LISP_INVALID         0x04
103 #define AO_LISP_UNDEFINED       0x08
104 #define AO_LISP_EOF             0x10
105
106 extern uint8_t          ao_lisp_exception;
107
108 static inline int
109 ao_lisp_is_const(ao_poly poly) {
110         return poly & AO_LISP_CONST;
111 }
112
113 #define AO_LISP_POOL_BASE       (ao_lisp_pool - 4)
114 #define AO_LISP_CONST_BASE      (ao_lisp_const - 4)
115
116 #define AO_LISP_IS_CONST(a)     (ao_lisp_const <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_const + AO_LISP_POOL_CONST)
117 #define AO_LISP_IS_POOL(a)      (ao_lisp_pool <= ((uint8_t *) (a)) && ((uint8_t *) (a)) < ao_lisp_pool + AO_LISP_POOL)
118
119 void *
120 ao_lisp_ref(ao_poly poly);
121
122 ao_poly
123 ao_lisp_poly(const void *addr, ao_poly type);
124
125 struct ao_lisp_type {
126         int     (*size)(void *addr);
127         void    (*mark)(void *addr);
128         void    (*move)(void *addr);
129 };
130
131 struct ao_lisp_cons {
132         ao_poly         car;
133         ao_poly         cdr;
134 };
135
136 struct ao_lisp_atom {
137         uint8_t         type;
138         uint8_t         pad[1];
139         ao_poly         next;
140         char            name[];
141 };
142
143 struct ao_lisp_val {
144         ao_poly         atom;
145         ao_poly         val;
146 };
147
148 struct ao_lisp_frame {
149         uint8_t                 type;
150         uint8_t                 num;
151         ao_poly                 next;
152         struct ao_lisp_val      vals[];
153 };
154
155 static inline struct ao_lisp_frame *
156 ao_lisp_poly_frame(ao_poly poly) {
157         return ao_lisp_ref(poly);
158 }
159
160 static inline ao_poly
161 ao_lisp_frame_poly(struct ao_lisp_frame *frame) {
162         return ao_lisp_poly(frame, AO_LISP_OTHER);
163 }
164
165 enum eval_state {
166         eval_sexpr,             /* Evaluate an sexpr */
167         eval_val,
168         eval_formal,
169         eval_exec,
170         eval_cond,
171         eval_cond_test,
172         eval_progn,
173         eval_while,
174         eval_while_test,
175 };
176
177 struct ao_lisp_stack {
178         uint8_t                 state;          /* enum eval_state */
179         ao_poly                 prev;           /* previous stack frame */
180         ao_poly                 sexprs;         /* expressions to evaluate */
181         ao_poly                 values;         /* values computed */
182         ao_poly                 values_tail;    /* end of the values list for easy appending */
183         ao_poly                 frame;          /* current lookup frame */
184         ao_poly                 list;           /* most recent function call */
185 };
186
187 static inline struct ao_lisp_stack *
188 ao_lisp_poly_stack(ao_poly p)
189 {
190         return ao_lisp_ref(p);
191 }
192
193 static inline ao_poly
194 ao_lisp_stack_poly(struct ao_lisp_stack *stack)
195 {
196         return ao_lisp_poly(stack, AO_LISP_OTHER);
197 }
198
199 extern struct ao_lisp_stack     *ao_lisp_stack;
200 extern ao_poly                  ao_lisp_v;
201
202 #define AO_LISP_FUNC_LAMBDA     0
203 #define AO_LISP_FUNC_NLAMBDA    1
204 #define AO_LISP_FUNC_MACRO      2
205 #define AO_LISP_FUNC_LEXPR      3
206
207 struct ao_lisp_builtin {
208         uint8_t         type;
209         uint8_t         args;
210         uint16_t        func;
211 };
212
213 enum ao_lisp_builtin_id {
214         builtin_eval,
215         builtin_read,
216         builtin_lambda,
217         builtin_lexpr,
218         builtin_nlambda,
219         builtin_macro,
220         builtin_car,
221         builtin_cdr,
222         builtin_cons,
223         builtin_last,
224         builtin_length,
225         builtin_quote,
226         builtin_set,
227         builtin_setq,
228         builtin_cond,
229         builtin_progn,
230         builtin_while,
231         builtin_print,
232         builtin_patom,
233         builtin_plus,
234         builtin_minus,
235         builtin_times,
236         builtin_divide,
237         builtin_mod,
238         builtin_equal,
239         builtin_less,
240         builtin_greater,
241         builtin_less_equal,
242         builtin_greater_equal,
243         builtin_pack,
244         builtin_unpack,
245         builtin_flush,
246         builtin_delay,
247         builtin_led,
248         builtin_save,
249         builtin_restore,
250         _builtin_last
251 };
252
253 typedef ao_poly (*ao_lisp_func_t)(struct ao_lisp_cons *cons);
254
255 extern const ao_lisp_func_t     ao_lisp_builtins[];
256
257 static inline ao_lisp_func_t
258 ao_lisp_func(struct ao_lisp_builtin *b)
259 {
260         return ao_lisp_builtins[b->func];
261 }
262
263 struct ao_lisp_lambda {
264         uint8_t         type;
265         uint8_t         args;
266         ao_poly         code;
267         ao_poly         frame;
268 };
269
270 static inline struct ao_lisp_lambda *
271 ao_lisp_poly_lambda(ao_poly poly)
272 {
273         return ao_lisp_ref(poly);
274 }
275
276 static inline ao_poly
277 ao_lisp_lambda_poly(struct ao_lisp_lambda *lambda)
278 {
279         return ao_lisp_poly(lambda, AO_LISP_OTHER);
280 }
281
282 static inline void *
283 ao_lisp_poly_other(ao_poly poly) {
284         return ao_lisp_ref(poly);
285 }
286
287 static inline uint8_t
288 ao_lisp_other_type(void *other) {
289         return *((uint8_t *) other);
290 }
291
292 static inline ao_poly
293 ao_lisp_other_poly(const void *other)
294 {
295         return ao_lisp_poly(other, AO_LISP_OTHER);
296 }
297
298 static inline int
299 ao_lisp_mem_round(int size)
300 {
301         return (size + 3) & ~3;
302 }
303
304 #define AO_LISP_OTHER_POLY(other) ((ao_poly)(other) + AO_LISP_OTHER)
305
306 static inline int ao_lisp_poly_base_type(ao_poly poly) {
307         return poly & AO_LISP_TYPE_MASK;
308 }
309
310 static inline int ao_lisp_poly_type(ao_poly poly) {
311         int     type = poly & AO_LISP_TYPE_MASK;
312         if (type == AO_LISP_OTHER)
313                 return ao_lisp_other_type(ao_lisp_poly_other(poly));
314         return type;
315 }
316
317 static inline struct ao_lisp_cons *
318 ao_lisp_poly_cons(ao_poly poly)
319 {
320         return ao_lisp_ref(poly);
321 }
322
323 static inline ao_poly
324 ao_lisp_cons_poly(struct ao_lisp_cons *cons)
325 {
326         return ao_lisp_poly(cons, AO_LISP_CONS);
327 }
328
329 static inline int
330 ao_lisp_poly_int(ao_poly poly)
331 {
332         return (int) ((ao_signed_poly) poly >> AO_LISP_TYPE_SHIFT);
333 }
334
335 static inline ao_poly
336 ao_lisp_int_poly(int i)
337 {
338         return ((ao_poly) i << 2) | AO_LISP_INT;
339 }
340
341 static inline char *
342 ao_lisp_poly_string(ao_poly poly)
343 {
344         return ao_lisp_ref(poly);
345 }
346
347 static inline ao_poly
348 ao_lisp_string_poly(char *s)
349 {
350         return ao_lisp_poly(s, AO_LISP_STRING);
351 }
352
353 static inline struct ao_lisp_atom *
354 ao_lisp_poly_atom(ao_poly poly)
355 {
356         return ao_lisp_ref(poly);
357 }
358
359 static inline ao_poly
360 ao_lisp_atom_poly(struct ao_lisp_atom *a)
361 {
362         return ao_lisp_poly(a, AO_LISP_OTHER);
363 }
364
365 static inline struct ao_lisp_builtin *
366 ao_lisp_poly_builtin(ao_poly poly)
367 {
368         return ao_lisp_ref(poly);
369 }
370
371 static inline ao_poly
372 ao_lisp_builtin_poly(struct ao_lisp_builtin *b)
373 {
374         return ao_lisp_poly(b, AO_LISP_OTHER);
375 }
376
377 /* memory functions */
378 /* returns 1 if the object was already marked */
379 int
380 ao_lisp_mark(const struct ao_lisp_type *type, void *addr);
381
382 /* returns 1 if the object was already marked */
383 int
384 ao_lisp_mark_memory(void *addr, int size);
385
386 void *
387 ao_lisp_move_map(void *addr);
388
389 /* returns 1 if the object was already moved */
390 int
391 ao_lisp_move(const struct ao_lisp_type *type, void **ref);
392
393 /* returns 1 if the object was already moved */
394 int
395 ao_lisp_move_memory(void **ref, int size);
396
397 void *
398 ao_lisp_alloc(int size);
399
400 void
401 ao_lisp_collect(void);
402
403 int
404 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr);
405
406 int
407 ao_lisp_root_poly_add(ao_poly *p);
408
409 void
410 ao_lisp_root_clear(void *addr);
411
412 /* cons */
413 extern const struct ao_lisp_type ao_lisp_cons_type;
414
415 struct ao_lisp_cons *
416 ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr);
417
418 void
419 ao_lisp_cons_print(ao_poly);
420
421 void
422 ao_lisp_cons_patom(ao_poly);
423
424 int
425 ao_lisp_cons_length(struct ao_lisp_cons *cons);
426
427 /* string */
428 extern const struct ao_lisp_type ao_lisp_string_type;
429
430 char *
431 ao_lisp_string_new(int len);
432
433 char *
434 ao_lisp_string_copy(char *a);
435
436 char *
437 ao_lisp_string_cat(char *a, char *b);
438
439 ao_poly
440 ao_lisp_string_pack(struct ao_lisp_cons *cons);
441
442 ao_poly
443 ao_lisp_string_unpack(char *a);
444
445 void
446 ao_lisp_string_print(ao_poly s);
447
448 void
449 ao_lisp_string_patom(ao_poly s);
450
451 /* atom */
452 extern const struct ao_lisp_type ao_lisp_atom_type;
453
454 extern struct ao_lisp_atom      *ao_lisp_atoms;
455 extern struct ao_lisp_frame     *ao_lisp_frame_global;
456 extern struct ao_lisp_frame     *ao_lisp_frame_current;
457
458 void
459 ao_lisp_atom_print(ao_poly a);
460
461 struct ao_lisp_atom *
462 ao_lisp_atom_intern(char *name);
463
464 ao_poly
465 ao_lisp_atom_get(ao_poly atom);
466
467 ao_poly
468 ao_lisp_atom_set(ao_poly atom, ao_poly val);
469
470 /* int */
471 void
472 ao_lisp_int_print(ao_poly i);
473
474 /* prim */
475 void
476 ao_lisp_poly_print(ao_poly p);
477
478 void
479 ao_lisp_poly_patom(ao_poly p);
480
481 int
482 ao_lisp_poly_mark(ao_poly p, uint8_t note_cons);
483
484 /* returns 1 if the object has already been moved */
485 int
486 ao_lisp_poly_move(ao_poly *p, uint8_t note_cons);
487
488 /* eval */
489
490 void
491 ao_lisp_eval_clear_globals(void);
492
493 int
494 ao_lisp_eval_restart(void);
495
496 ao_poly
497 ao_lisp_eval(ao_poly p);
498
499 ao_poly
500 ao_lisp_set_cond(struct ao_lisp_cons *cons);
501
502 /* builtin */
503 void
504 ao_lisp_builtin_print(ao_poly b);
505
506 extern const struct ao_lisp_type ao_lisp_builtin_type;
507
508 /* Check argument count */
509 ao_poly
510 ao_lisp_check_argc(ao_poly name, struct ao_lisp_cons *cons, int min, int max);
511
512 /* Check argument type */
513 ao_poly
514 ao_lisp_check_argt(ao_poly name, struct ao_lisp_cons *cons, int argc, int type, int nil_ok);
515
516 /* Fetch an arg (nil if off the end) */
517 ao_poly
518 ao_lisp_arg(struct ao_lisp_cons *cons, int argc);
519
520 char *
521 ao_lisp_args_name(uint8_t args);
522
523 /* read */
524 ao_poly
525 ao_lisp_read(void);
526
527 /* rep */
528 ao_poly
529 ao_lisp_read_eval_print(void);
530
531 /* frame */
532 extern const struct ao_lisp_type ao_lisp_frame_type;
533
534 ao_poly *
535 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom);
536
537 struct ao_lisp_frame *
538 ao_lisp_frame_new(int num);
539
540 int
541 ao_lisp_frame_add(struct ao_lisp_frame **frame, ao_poly atom, ao_poly val);
542
543 void
544 ao_lisp_frame_print(ao_poly p);
545
546 /* lambda */
547 extern const struct ao_lisp_type ao_lisp_lambda_type;
548
549 struct ao_lisp_lambda *
550 ao_lisp_lambda_new(ao_poly cons);
551
552 void
553 ao_lisp_lambda_print(ao_poly lambda);
554
555 ao_poly
556 ao_lisp_lambda(struct ao_lisp_cons *cons);
557
558 ao_poly
559 ao_lisp_lexpr(struct ao_lisp_cons *cons);
560
561 ao_poly
562 ao_lisp_nlambda(struct ao_lisp_cons *cons);
563
564 ao_poly
565 ao_lisp_macro(struct ao_lisp_cons *cons);
566
567 ao_poly
568 ao_lisp_lambda_eval(void);
569
570 /* save */
571
572 ao_poly
573 ao_lisp_save(struct ao_lisp_cons *cons);
574
575 ao_poly
576 ao_lisp_restore(struct ao_lisp_cons *cons);
577
578 /* error */
579
580 void
581 ao_lisp_stack_print(void);
582
583 ao_poly
584 ao_lisp_error(int error, char *format, ...);
585
586 /* debugging macros */
587
588 #if DBG_EVAL
589 #define DBG_CODE        1
590 int ao_lisp_stack_depth;
591 #define DBG_DO(a)       a
592 #define DBG_INDENT()    do { int _s; for(_s = 0; _s < ao_lisp_stack_depth; _s++) printf("  "); } while(0)
593 #define DBG_IN()        (++ao_lisp_stack_depth)
594 #define DBG_OUT()       (--ao_lisp_stack_depth)
595 #define DBG_RESET()     (ao_lisp_stack_depth = 0)
596 #define DBG(...)        printf(__VA_ARGS__)
597 #define DBGI(...)       do { DBG("%4d: ", __LINE__); DBG_INDENT(); DBG(__VA_ARGS__); } while (0)
598 #define DBG_CONS(a)     ao_lisp_cons_print(ao_lisp_cons_poly(a))
599 #define DBG_POLY(a)     ao_lisp_poly_print(a)
600 #define OFFSET(a)       ((a) ? (int) ((uint8_t *) a - ao_lisp_pool) : -1)
601 #define DBG_STACK()     ao_lisp_stack_print()
602 static inline void
603 ao_lisp_frames_dump(void)
604 {
605         struct ao_lisp_stack *s;
606         DBGI(".. current frame: "); DBG_POLY(ao_lisp_frame_poly(ao_lisp_frame_current)); DBG("\n");
607         for (s = ao_lisp_stack; s; s = ao_lisp_poly_stack(s->prev)) {
608                 DBGI(".. stack frame: "); DBG_POLY(s->frame); DBG("\n");
609         }
610 }
611 #define DBG_FRAMES()    ao_lisp_frames_dump()
612 #else
613 #define DBG_DO(a)
614 #define DBG_INDENT()
615 #define DBG_IN()
616 #define DBG_OUT()
617 #define DBG(...)
618 #define DBGI(...)
619 #define DBG_CONS(a)
620 #define DBG_POLY(a)
621 #define DBG_RESET()
622 #define DBG_STACK()
623 #define DBG_FRAMES()
624 #endif
625
626 #endif /* _AO_LISP_H_ */