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