60bb80f01e8059b01c534b7a306d933f59d3579d
[fw/altos] / src / lisp / ao_lisp_make_const.c
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 #include "ao_lisp.h"
16 #include <stdlib.h>
17 #include <ctype.h>
18 #include <unistd.h>
19 #include <getopt.h>
20
21 static struct ao_lisp_builtin *
22 ao_lisp_make_builtin(enum ao_lisp_builtin_id func, int args) {
23         struct ao_lisp_builtin *b = ao_lisp_alloc(sizeof (struct ao_lisp_builtin));
24
25         b->type = AO_LISP_BUILTIN;
26         b->func = func;
27         b->args = args;
28         return b;
29 }
30
31 struct builtin_func {
32         char    *name;
33         int     args;
34         int     func;
35 };
36
37 struct builtin_func funcs[] = {
38         { .name = "eval",       .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_eval },
39         { .name = "read",       .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_read },
40         { .name = "lambda",     .args = AO_LISP_FUNC_NLAMBDA,   .func = builtin_lambda },
41         { .name = "lexpr",      .args = AO_LISP_FUNC_NLAMBDA,   .func = builtin_lexpr },
42         { .name = "nlambda",    .args = AO_LISP_FUNC_NLAMBDA,   .func = builtin_nlambda },
43         { .name = "macro",      .args = AO_LISP_FUNC_NLAMBDA,   .func = builtin_macro },
44         { .name = "car",        .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_car },
45         { .name = "cdr",        .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_cdr },
46         { .name = "cons",       .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_cons },
47         { .name = "last",       .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_last },
48         { .name = "length",     .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_length },
49         { .name = "quote",      .args = AO_LISP_FUNC_NLAMBDA,   .func = builtin_quote },
50         { .name = "set",        .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_set },
51         { .name = "setq",       .args = AO_LISP_FUNC_MACRO,     .func = builtin_setq },
52         { .name = "cond",       .args = AO_LISP_FUNC_NLAMBDA,   .func = builtin_cond },
53         { .name = "progn",      .args = AO_LISP_FUNC_NLAMBDA,   .func = builtin_progn },
54         { .name = "while",      .args = AO_LISP_FUNC_NLAMBDA,   .func = builtin_while },
55         { .name = "print",      .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_print },
56         { .name = "patom",      .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_patom },
57         { .name = "+",          .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_plus },
58         { .name = "-",          .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_minus },
59         { .name = "*",          .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_times },
60         { .name = "/",          .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_divide },
61         { .name = "%",          .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_mod },
62         { .name = "=",          .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_equal },
63         { .name = "<",          .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_less },
64         { .name = ">",          .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_greater },
65         { .name = "<=",         .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_less_equal },
66         { .name = ">=",         .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_greater_equal },
67         { .name = "pack",       .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_pack },
68         { .name = "unpack",     .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_unpack },
69         { .name = "flush",      .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_flush },
70         { .name = "delay",      .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_delay },
71         { .name = "led",        .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_led },
72         { .name = "save",       .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_save },
73         { .name = "restore",    .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_restore },
74 };
75
76 #define N_FUNC (sizeof funcs / sizeof funcs[0])
77
78 struct ao_lisp_frame    *globals;
79
80 static int
81 is_atom(int offset)
82 {
83         struct ao_lisp_atom *a;
84
85         for (a = ao_lisp_atoms; a; a = ao_lisp_poly_atom(a->next))
86                 if (((uint8_t *) a->name - ao_lisp_const) == offset)
87                         return strlen(a->name);
88         return 0;
89 }
90
91 #define AO_FEC_CRC_INIT 0xffff
92
93 static inline uint16_t
94 ao_fec_crc_byte(uint8_t byte, uint16_t crc)
95 {
96         uint8_t bit;
97
98         for (bit = 0; bit < 8; bit++) {
99                 if (((crc & 0x8000) >> 8) ^ (byte & 0x80))
100                         crc = (crc << 1) ^ 0x8005;
101                 else
102                         crc = (crc << 1);
103                 byte <<= 1;
104         }
105         return crc;
106 }
107
108 uint16_t
109 ao_fec_crc(const uint8_t *bytes, uint8_t len)
110 {
111         uint16_t        crc = AO_FEC_CRC_INIT;
112
113         while (len--)
114                 crc = ao_fec_crc_byte(*bytes++, crc);
115         return crc;
116 }
117
118 struct ao_lisp_macro_stack {
119         struct ao_lisp_macro_stack *next;
120         ao_poly p;
121 };
122
123 struct ao_lisp_macro_stack *macro_stack;
124
125 int
126 ao_lisp_macro_push(ao_poly p)
127 {
128         struct ao_lisp_macro_stack *m = macro_stack;
129
130         while (m) {
131                 if (m->p == p)
132                         return 1;
133                 m = m->next;
134         }
135         m = malloc (sizeof (struct ao_lisp_macro_stack));
136         m->p = p;
137         m->next = macro_stack;
138         macro_stack = m;
139 }
140
141 void
142 ao_lisp_macro_pop(void)
143 {
144         struct ao_lisp_macro_stack *m = macro_stack;
145
146         macro_stack = m->next;
147         free(m);
148 }
149
150 #define DBG_MACRO 0
151 #if DBG_MACRO
152 int macro_scan_depth;
153
154 void indent(void)
155 {
156         int i;
157         for (i = 0; i < macro_scan_depth; i++)
158                 printf("  ");
159 }
160 #define MACRO_DEBUG(a)  a
161 #else
162 #define MACRO_DEBUG(a)
163 #endif
164
165 ao_poly
166 ao_has_macro(ao_poly p);
167
168 ao_poly
169 ao_macro_test_get(ao_poly atom)
170 {
171         ao_poly *ref = ao_lisp_atom_ref(ao_lisp_frame_global, atom);
172         if (ref)
173                 return *ref;
174         return AO_LISP_NIL;
175 }
176
177 ao_poly
178 ao_is_macro(ao_poly p)
179 {
180         struct ao_lisp_builtin  *builtin;
181         struct ao_lisp_lambda   *lambda;
182         ao_poly ret;
183
184         MACRO_DEBUG(indent(); printf ("is macro "); ao_lisp_poly_print(p); printf("\n"); ++macro_scan_depth);
185         switch (ao_lisp_poly_type(p)) {
186         case AO_LISP_ATOM:
187                 if (ao_lisp_macro_push(p))
188                         ret = AO_LISP_NIL;
189                 else {
190                         if (ao_is_macro(ao_macro_test_get(p)))
191                                 ret = p;
192                         else
193                                 ret = AO_LISP_NIL;
194                         ao_lisp_macro_pop();
195                 }
196                 break;
197         case AO_LISP_CONS:
198                 ret = ao_has_macro(p);
199                 break;
200         case AO_LISP_BUILTIN:
201                 builtin = ao_lisp_poly_builtin(p);
202                 if ((builtin->args & AO_LISP_FUNC_MASK) == AO_LISP_FUNC_MACRO)
203                         ret = p;
204                 else
205                         ret = 0;
206                 break;
207
208         case AO_LISP_LAMBDA:
209                 lambda = ao_lisp_poly_lambda(p);
210                 if (lambda->args == AO_LISP_FUNC_MACRO)
211                         ret = p;
212                 else
213                         ret = ao_has_macro(lambda->code);
214                 break;
215         default:
216                 ret = AO_LISP_NIL;
217                 break;
218         }
219         MACRO_DEBUG(--macro_scan_depth; indent(); printf ("... "); ao_lisp_poly_print(ret); printf("\n"));
220         return ret;
221 }
222
223 ao_poly
224 ao_has_macro(ao_poly p)
225 {
226         struct ao_lisp_cons     *cons;
227         struct ao_lisp_lambda   *lambda;
228         ao_poly                 m;
229
230         if (p == AO_LISP_NIL)
231                 return AO_LISP_NIL;
232
233         MACRO_DEBUG(indent(); printf("has macro "); ao_lisp_poly_print(p); printf("\n"); ++macro_scan_depth);
234         switch (ao_lisp_poly_type(p)) {
235         case AO_LISP_LAMBDA:
236                 lambda = ao_lisp_poly_lambda(p);
237                 p = ao_has_macro(lambda->code);
238                 break;
239         case AO_LISP_CONS:
240                 cons = ao_lisp_poly_cons(p);
241                 if ((p = ao_is_macro(cons->car)))
242                         break;
243
244                 cons = ao_lisp_poly_cons(cons->cdr);
245                 p = AO_LISP_NIL;
246                 while (cons) {
247                         m = ao_has_macro(cons->car);
248                         if (m) {
249                                 p = m;
250                                 break;
251                         }
252                         cons = ao_lisp_poly_cons(cons->cdr);
253                 }
254                 break;
255
256         default:
257                 p = AO_LISP_NIL;
258                 break;
259         }
260         MACRO_DEBUG(--macro_scan_depth; indent(); printf("... "); ao_lisp_poly_print(p); printf("\n"));
261         return p;
262 }
263
264 int
265 ao_lisp_read_eval_abort(void)
266 {
267         ao_poly in, out = AO_LISP_NIL;
268         for(;;) {
269                 in = ao_lisp_read();
270                 if (in == _ao_lisp_atom_eof)
271                         break;
272                 out = ao_lisp_eval(in);
273                 if (ao_lisp_exception)
274                         return 0;
275                 ao_lisp_poly_print(out);
276                 putchar ('\n');
277         }
278         return 1;
279 }
280
281 static FILE     *in;
282 static FILE     *out;
283
284 int
285 ao_lisp_getc(void)
286 {
287         return getc(in);
288 }
289
290 static const struct option options[] = {
291         { .name = "out", .has_arg = 1, .val = 'o' },
292         { 0, 0, 0, 0 }
293 };
294
295 static void usage(char *program)
296 {
297         fprintf(stderr, "usage: %s [--out=<output>] [input]\n", program);
298         exit(1);
299 }
300
301 int
302 main(int argc, char **argv)
303 {
304         int     f, o;
305         ao_poly val;
306         struct ao_lisp_atom     *a;
307         struct ao_lisp_builtin  *b;
308         int     in_atom;
309         char    *out_name;
310         int     c;
311
312         in = stdin;
313         out = stdout;
314
315         while ((c = getopt_long(argc, argv, "o:", options, NULL)) != -1) {
316                 switch (c) {
317                 case 'o':
318                         out_name = optarg;
319                         break;
320                 default:
321                         usage(argv[0]);
322                         break;
323                 }
324         }
325
326         for (f = 0; f < (int) N_FUNC; f++) {
327                 b = ao_lisp_make_builtin(funcs[f].func, funcs[f].args);
328                 a = ao_lisp_atom_intern(funcs[f].name);
329                 ao_lisp_atom_set(ao_lisp_atom_poly(a),
330                                  ao_lisp_builtin_poly(b));
331         }
332
333         /* boolean constants */
334         ao_lisp_atom_set(ao_lisp_atom_poly(ao_lisp_atom_intern("nil")),
335                          AO_LISP_NIL);
336         a = ao_lisp_atom_intern("t");
337         ao_lisp_atom_set(ao_lisp_atom_poly(a),
338                          ao_lisp_atom_poly(a));
339
340         /* end of file value */
341         a = ao_lisp_atom_intern("eof");
342         ao_lisp_atom_set(ao_lisp_atom_poly(a),
343                          ao_lisp_atom_poly(a));
344
345         if (argv[optind]){
346                 in = fopen(argv[optind], "r");
347                 if (!in) {
348                         perror(argv[optind]);
349                         exit(1);
350                 }
351         }
352         if (!ao_lisp_read_eval_abort()) {
353                 fprintf(stderr, "eval failed\n");
354                 exit(1);
355         }
356
357         /* Reduce to referenced values */
358         ao_lisp_collect(AO_LISP_COLLECT_FULL);
359
360         for (f = 0; f < ao_lisp_frame_num(ao_lisp_frame_global); f++) {
361                 val = ao_has_macro(ao_lisp_frame_global->vals[f].val);
362                 if (val != AO_LISP_NIL) {
363                         printf("error: function %s contains unresolved macro: ",
364                                ao_lisp_poly_atom(ao_lisp_frame_global->vals[f].atom)->name);
365                         ao_lisp_poly_print(val);
366                         printf("\n");
367                         exit(1);
368                 }
369         }
370
371         if (out_name) {
372                 out = fopen(out_name, "w");
373                 if (!out) {
374                         perror(out_name);
375                         exit(1);
376                 }
377         }
378
379         fprintf(out, "/* Generated file, do not edit */\n\n");
380
381         fprintf(out, "#define AO_LISP_POOL_CONST %d\n", ao_lisp_top);
382         fprintf(out, "extern const uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));\n");
383         fprintf(out, "#define ao_builtin_atoms 0x%04x\n", ao_lisp_atom_poly(ao_lisp_atoms));
384         fprintf(out, "#define ao_builtin_frame 0x%04x\n", ao_lisp_frame_poly(ao_lisp_frame_global));
385         fprintf(out, "#define ao_lisp_const_checksum ((uint16_t) 0x%04x)\n", ao_fec_crc(ao_lisp_const, ao_lisp_top));
386
387
388         for (a = ao_lisp_atoms; a; a = ao_lisp_poly_atom(a->next)) {
389                 char    *n = a->name, c;
390                 fprintf(out, "#define _ao_lisp_atom_");
391                 while ((c = *n++)) {
392                         if (isalnum(c))
393                                 fprintf(out, "%c", c);
394                         else
395                                 fprintf(out, "%02x", c);
396                 }
397                 fprintf(out, "  0x%04x\n", ao_lisp_atom_poly(a));
398         }
399         fprintf(out, "#ifdef AO_LISP_CONST_BITS\n");
400         fprintf(out, "const uint8_t ao_lisp_const[] = {");
401         for (o = 0; o < ao_lisp_top; o++) {
402                 uint8_t c;
403                 if ((o & 0xf) == 0)
404                         fprintf(out, "\n\t");
405                 else
406                         fprintf(out, " ");
407                 c = ao_lisp_const[o];
408                 if (!in_atom)
409                         in_atom = is_atom(o);
410                 if (in_atom) {
411                         fprintf(out, " '%c',", c);
412                         in_atom--;
413                 } else {
414                         fprintf(out, "0x%02x,", c);
415                 }
416         }
417         fprintf(out, "\n};\n");
418         fprintf(out, "#endif /* AO_LISP_CONST_BITS */\n");
419         exit(0);
420 }