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