de9c57251bc954378b00bd2f134e69f445a1f468
[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         { .name = "call/cc",    .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_call_cc },
75 };
76
77 #define N_FUNC (sizeof funcs / sizeof funcs[0])
78
79 struct ao_lisp_frame    *globals;
80
81 static int
82 is_atom(int offset)
83 {
84         struct ao_lisp_atom *a;
85
86         for (a = ao_lisp_atoms; a; a = ao_lisp_poly_atom(a->next))
87                 if (((uint8_t *) a->name - ao_lisp_const) == offset)
88                         return strlen(a->name);
89         return 0;
90 }
91
92 #define AO_FEC_CRC_INIT 0xffff
93
94 static inline uint16_t
95 ao_fec_crc_byte(uint8_t byte, uint16_t crc)
96 {
97         uint8_t bit;
98
99         for (bit = 0; bit < 8; bit++) {
100                 if (((crc & 0x8000) >> 8) ^ (byte & 0x80))
101                         crc = (crc << 1) ^ 0x8005;
102                 else
103                         crc = (crc << 1);
104                 byte <<= 1;
105         }
106         return crc;
107 }
108
109 uint16_t
110 ao_fec_crc(const uint8_t *bytes, uint8_t len)
111 {
112         uint16_t        crc = AO_FEC_CRC_INIT;
113
114         while (len--)
115                 crc = ao_fec_crc_byte(*bytes++, crc);
116         return crc;
117 }
118
119 struct ao_lisp_macro_stack {
120         struct ao_lisp_macro_stack *next;
121         ao_poly p;
122 };
123
124 struct ao_lisp_macro_stack *macro_stack;
125
126 int
127 ao_lisp_macro_push(ao_poly p)
128 {
129         struct ao_lisp_macro_stack *m = macro_stack;
130
131         while (m) {
132                 if (m->p == p)
133                         return 1;
134                 m = m->next;
135         }
136         m = malloc (sizeof (struct ao_lisp_macro_stack));
137         m->p = p;
138         m->next = macro_stack;
139         macro_stack = m;
140         return 0;
141 }
142
143 void
144 ao_lisp_macro_pop(void)
145 {
146         struct ao_lisp_macro_stack *m = macro_stack;
147
148         macro_stack = m->next;
149         free(m);
150 }
151
152 #define DBG_MACRO 0
153 #if DBG_MACRO
154 int macro_scan_depth;
155
156 void indent(void)
157 {
158         int i;
159         for (i = 0; i < macro_scan_depth; i++)
160                 printf("  ");
161 }
162 #define MACRO_DEBUG(a)  a
163 #else
164 #define MACRO_DEBUG(a)
165 #endif
166
167 ao_poly
168 ao_has_macro(ao_poly p);
169
170 ao_poly
171 ao_macro_test_get(ao_poly atom)
172 {
173         ao_poly *ref = ao_lisp_atom_ref(ao_lisp_frame_global, atom);
174         if (ref)
175                 return *ref;
176         return AO_LISP_NIL;
177 }
178
179 ao_poly
180 ao_is_macro(ao_poly p)
181 {
182         struct ao_lisp_builtin  *builtin;
183         struct ao_lisp_lambda   *lambda;
184         ao_poly ret;
185
186         MACRO_DEBUG(indent(); printf ("is macro "); ao_lisp_poly_print(p); printf("\n"); ++macro_scan_depth);
187         switch (ao_lisp_poly_type(p)) {
188         case AO_LISP_ATOM:
189                 if (ao_lisp_macro_push(p))
190                         ret = AO_LISP_NIL;
191                 else {
192                         if (ao_is_macro(ao_macro_test_get(p)))
193                                 ret = p;
194                         else
195                                 ret = AO_LISP_NIL;
196                         ao_lisp_macro_pop();
197                 }
198                 break;
199         case AO_LISP_CONS:
200                 ret = ao_has_macro(p);
201                 break;
202         case AO_LISP_BUILTIN:
203                 builtin = ao_lisp_poly_builtin(p);
204                 if ((builtin->args & AO_LISP_FUNC_MASK) == AO_LISP_FUNC_MACRO)
205                         ret = p;
206                 else
207                         ret = 0;
208                 break;
209
210         case AO_LISP_LAMBDA:
211                 lambda = ao_lisp_poly_lambda(p);
212                 if (lambda->args == AO_LISP_FUNC_MACRO)
213                         ret = p;
214                 else
215                         ret = ao_has_macro(lambda->code);
216                 break;
217         default:
218                 ret = AO_LISP_NIL;
219                 break;
220         }
221         MACRO_DEBUG(--macro_scan_depth; indent(); printf ("... "); ao_lisp_poly_print(ret); printf("\n"));
222         return ret;
223 }
224
225 ao_poly
226 ao_has_macro(ao_poly p)
227 {
228         struct ao_lisp_cons     *cons;
229         struct ao_lisp_lambda   *lambda;
230         ao_poly                 m;
231
232         if (p == AO_LISP_NIL)
233                 return AO_LISP_NIL;
234
235         MACRO_DEBUG(indent(); printf("has macro "); ao_lisp_poly_print(p); printf("\n"); ++macro_scan_depth);
236         switch (ao_lisp_poly_type(p)) {
237         case AO_LISP_LAMBDA:
238                 lambda = ao_lisp_poly_lambda(p);
239                 p = ao_has_macro(lambda->code);
240                 break;
241         case AO_LISP_CONS:
242                 cons = ao_lisp_poly_cons(p);
243                 if ((p = ao_is_macro(cons->car)))
244                         break;
245
246                 cons = ao_lisp_poly_cons(cons->cdr);
247                 p = AO_LISP_NIL;
248                 while (cons) {
249                         m = ao_has_macro(cons->car);
250                         if (m) {
251                                 p = m;
252                                 break;
253                         }
254                         cons = ao_lisp_poly_cons(cons->cdr);
255                 }
256                 break;
257
258         default:
259                 p = AO_LISP_NIL;
260                 break;
261         }
262         MACRO_DEBUG(--macro_scan_depth; indent(); printf("... "); ao_lisp_poly_print(p); printf("\n"));
263         return p;
264 }
265
266 int
267 ao_lisp_read_eval_abort(void)
268 {
269         ao_poly in, out = AO_LISP_NIL;
270         for(;;) {
271                 in = ao_lisp_read();
272                 if (in == _ao_lisp_atom_eof)
273                         break;
274                 out = ao_lisp_eval(in);
275                 if (ao_lisp_exception)
276                         return 0;
277                 ao_lisp_poly_print(out);
278                 putchar ('\n');
279         }
280         return 1;
281 }
282
283 static FILE     *in;
284 static FILE     *out;
285
286 int
287 ao_lisp_getc(void)
288 {
289         return getc(in);
290 }
291
292 static const struct option options[] = {
293         { .name = "out", .has_arg = 1, .val = 'o' },
294         { 0, 0, 0, 0 }
295 };
296
297 static void usage(char *program)
298 {
299         fprintf(stderr, "usage: %s [--out=<output>] [input]\n", program);
300         exit(1);
301 }
302
303 int
304 main(int argc, char **argv)
305 {
306         int     f, o;
307         ao_poly val;
308         struct ao_lisp_atom     *a;
309         struct ao_lisp_builtin  *b;
310         int     in_atom = 0;
311         char    *out_name = NULL;
312         int     c;
313
314         in = stdin;
315         out = stdout;
316
317         while ((c = getopt_long(argc, argv, "o:", options, NULL)) != -1) {
318                 switch (c) {
319                 case 'o':
320                         out_name = optarg;
321                         break;
322                 default:
323                         usage(argv[0]);
324                         break;
325                 }
326         }
327
328         for (f = 0; f < (int) N_FUNC; f++) {
329                 b = ao_lisp_make_builtin(funcs[f].func, funcs[f].args);
330                 a = ao_lisp_atom_intern(funcs[f].name);
331                 ao_lisp_atom_set(ao_lisp_atom_poly(a),
332                                  ao_lisp_builtin_poly(b));
333         }
334
335         /* boolean constants */
336         ao_lisp_atom_set(ao_lisp_atom_poly(ao_lisp_atom_intern("nil")),
337                          AO_LISP_NIL);
338         a = ao_lisp_atom_intern("t");
339         ao_lisp_atom_set(ao_lisp_atom_poly(a),
340                          ao_lisp_atom_poly(a));
341
342         /* end of file value */
343         a = ao_lisp_atom_intern("eof");
344         ao_lisp_atom_set(ao_lisp_atom_poly(a),
345                          ao_lisp_atom_poly(a));
346
347         if (argv[optind]){
348                 in = fopen(argv[optind], "r");
349                 if (!in) {
350                         perror(argv[optind]);
351                         exit(1);
352                 }
353         }
354         if (!ao_lisp_read_eval_abort()) {
355                 fprintf(stderr, "eval failed\n");
356                 exit(1);
357         }
358
359         /* Reduce to referenced values */
360         ao_lisp_collect(AO_LISP_COLLECT_FULL);
361
362         for (f = 0; f < ao_lisp_frame_global->num; f++) {
363                 val = ao_has_macro(ao_lisp_frame_global->vals[f].val);
364                 if (val != AO_LISP_NIL) {
365                         printf("error: function %s contains unresolved macro: ",
366                                ao_lisp_poly_atom(ao_lisp_frame_global->vals[f].atom)->name);
367                         ao_lisp_poly_print(val);
368                         printf("\n");
369                         exit(1);
370                 }
371         }
372
373         if (out_name) {
374                 out = fopen(out_name, "w");
375                 if (!out) {
376                         perror(out_name);
377                         exit(1);
378                 }
379         }
380
381         fprintf(out, "/* Generated file, do not edit */\n\n");
382
383         fprintf(out, "#define AO_LISP_POOL_CONST %d\n", ao_lisp_top);
384         fprintf(out, "extern const uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));\n");
385         fprintf(out, "#define ao_builtin_atoms 0x%04x\n", ao_lisp_atom_poly(ao_lisp_atoms));
386         fprintf(out, "#define ao_builtin_frame 0x%04x\n", ao_lisp_frame_poly(ao_lisp_frame_global));
387         fprintf(out, "#define ao_lisp_const_checksum ((uint16_t) 0x%04x)\n", ao_fec_crc(ao_lisp_const, ao_lisp_top));
388
389
390         for (a = ao_lisp_atoms; a; a = ao_lisp_poly_atom(a->next)) {
391                 char    *n = a->name, c;
392                 fprintf(out, "#define _ao_lisp_atom_");
393                 while ((c = *n++)) {
394                         if (isalnum(c))
395                                 fprintf(out, "%c", c);
396                         else
397                                 fprintf(out, "%02x", c);
398                 }
399                 fprintf(out, "  0x%04x\n", ao_lisp_atom_poly(a));
400         }
401         fprintf(out, "#ifdef AO_LISP_CONST_BITS\n");
402         fprintf(out, "const uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute((aligned(4))) = {");
403         for (o = 0; o < ao_lisp_top; o++) {
404                 uint8_t c;
405                 if ((o & 0xf) == 0)
406                         fprintf(out, "\n\t");
407                 else
408                         fprintf(out, " ");
409                 c = ao_lisp_const[o];
410                 if (!in_atom)
411                         in_atom = is_atom(o);
412                 if (in_atom) {
413                         fprintf(out, " '%c',", c);
414                         in_atom--;
415                 } else {
416                         fprintf(out, "0x%02x,", c);
417                 }
418         }
419         fprintf(out, "\n};\n");
420         fprintf(out, "#endif /* AO_LISP_CONST_BITS */\n");
421         exit(0);
422 }