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