altos/scheme: Add ports. Split scheme code up.
[fw/altos] / src / scheme / ao_scheme_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_scheme.h"
16 #include <stdlib.h>
17 #include <ctype.h>
18 #include <unistd.h>
19 #include <getopt.h>
20 #include <stdbool.h>
21
22 static struct ao_scheme_builtin *
23 ao_scheme_make_builtin(enum ao_scheme_builtin_id func, int args) {
24         struct ao_scheme_builtin *b = ao_scheme_alloc(sizeof (struct ao_scheme_builtin));
25
26         b->type = AO_SCHEME_BUILTIN;
27         b->func = func;
28         b->args = args;
29         return b;
30 }
31
32 struct builtin_func {
33         const char      *feature;
34         const char      *name;
35         int             args;
36         enum ao_scheme_builtin_id       func;
37 };
38
39 struct builtin_atom {
40         const char      *feature;
41         const char      *name;
42 };
43
44 #define AO_SCHEME_BUILTIN_CONSTS
45 #define AO_SCHEME_BUILTIN_ATOM_NAMES
46
47 #include "ao_scheme_builtin.h"
48
49 #define N_FUNC          (sizeof funcs / sizeof funcs[0])
50
51 #define N_ATOM          (sizeof atoms / sizeof atoms[0])
52
53 struct ao_scheme_frame  *globals;
54
55 static int
56 is_atom(int offset)
57 {
58         struct ao_scheme_atom *a;
59
60         for (a = ao_scheme_atoms; a; a = ao_scheme_poly_atom(a->next))
61                 if (((uint8_t *) a->name - ao_scheme_const) == offset)
62                         return strlen(a->name);
63         return 0;
64 }
65
66 #define AO_FEC_CRC_INIT 0xffff
67
68 static inline uint16_t
69 ao_fec_crc_byte(uint8_t byte, uint16_t crc)
70 {
71         uint8_t bit;
72
73         for (bit = 0; bit < 8; bit++) {
74                 if (((crc & 0x8000) >> 8) ^ (byte & 0x80))
75                         crc = (crc << 1) ^ 0x8005;
76                 else
77                         crc = (crc << 1);
78                 byte <<= 1;
79         }
80         return crc;
81 }
82
83 static uint16_t
84 ao_fec_crc(const uint8_t *bytes, uint8_t len)
85 {
86         uint16_t        crc = AO_FEC_CRC_INIT;
87
88         while (len--)
89                 crc = ao_fec_crc_byte(*bytes++, crc);
90         return crc;
91 }
92
93 struct ao_scheme_macro_stack {
94         struct ao_scheme_macro_stack *next;
95         ao_poly p;
96 };
97
98 struct ao_scheme_macro_stack *macro_stack;
99
100 static int
101 ao_scheme_macro_push(ao_poly p)
102 {
103         struct ao_scheme_macro_stack *m = macro_stack;
104
105         while (m) {
106                 if (m->p == p)
107                         return 1;
108                 m = m->next;
109         }
110         m = malloc (sizeof (struct ao_scheme_macro_stack));
111         m->p = p;
112         m->next = macro_stack;
113         macro_stack = m;
114         return 0;
115 }
116
117 static void
118 ao_scheme_macro_pop(void)
119 {
120         struct ao_scheme_macro_stack *m = macro_stack;
121
122         macro_stack = m->next;
123         free(m);
124 }
125
126 #define DBG_MACRO 0
127 #if DBG_MACRO
128 static int macro_scan_depth;
129
130 static void indent(void)
131 {
132         int i;
133         for (i = 0; i < macro_scan_depth; i++)
134                 printf("  ");
135 }
136 #define MACRO_DEBUG(a)  a
137 #else
138 #define MACRO_DEBUG(a)
139 #endif
140
141 ao_poly
142 ao_has_macro(ao_poly p);
143
144 static ao_poly
145 ao_macro_test_get(ao_poly atom)
146 {
147         ao_poly *ref = ao_scheme_atom_ref(atom, NULL);
148         if (ref)
149                 return *ref;
150         return AO_SCHEME_NIL;
151 }
152
153 static ao_poly
154 ao_is_macro(ao_poly p)
155 {
156         struct ao_scheme_builtin        *builtin;
157         struct ao_scheme_lambda *lambda;
158         ao_poly ret;
159
160         MACRO_DEBUG(indent(); ao_scheme_printf ("is macro %v\n", p); ++macro_scan_depth);
161         switch (ao_scheme_poly_type(p)) {
162         case AO_SCHEME_ATOM:
163                 if (ao_scheme_macro_push(p))
164                         ret = AO_SCHEME_NIL;
165                 else {
166                         if (ao_is_macro(ao_macro_test_get(p)))
167                                 ret = p;
168                         else
169                                 ret = AO_SCHEME_NIL;
170                         ao_scheme_macro_pop();
171                 }
172                 break;
173         case AO_SCHEME_CONS:
174                 ret = ao_has_macro(p);
175                 break;
176         case AO_SCHEME_BUILTIN:
177                 builtin = ao_scheme_poly_builtin(p);
178                 if ((builtin->args & AO_SCHEME_FUNC_MASK) == AO_SCHEME_FUNC_MACRO)
179                         ret = p;
180                 else
181                         ret = 0;
182                 break;
183
184         case AO_SCHEME_LAMBDA:
185                 lambda = ao_scheme_poly_lambda(p);
186                 if (lambda->args == AO_SCHEME_FUNC_MACRO)
187                         ret = p;
188                 else
189                         ret = ao_has_macro(lambda->code);
190                 break;
191         default:
192                 ret = AO_SCHEME_NIL;
193                 break;
194         }
195         MACRO_DEBUG(--macro_scan_depth; indent(); ao_scheme_printf ("... %v\n", ret););
196         return ret;
197 }
198
199 ao_poly
200 ao_has_macro(ao_poly p)
201 {
202         struct ao_scheme_cons   *cons;
203         struct ao_scheme_lambda *lambda;
204         ao_poly                 m;
205         ao_poly                 list;
206
207         if (p == AO_SCHEME_NIL)
208                 return AO_SCHEME_NIL;
209
210         MACRO_DEBUG(indent(); ao_scheme_printf("has macro %v\n", p); ++macro_scan_depth);
211         switch (ao_scheme_poly_type(p)) {
212         case AO_SCHEME_LAMBDA:
213                 lambda = ao_scheme_poly_lambda(p);
214                 p = ao_has_macro(ao_scheme_poly_cons(lambda->code)->cdr);
215                 break;
216         case AO_SCHEME_CONS:
217                 cons = ao_scheme_poly_cons(p);
218                 if ((p = ao_is_macro(cons->car)))
219                         break;
220
221                 list = cons->cdr;
222                 p = AO_SCHEME_NIL;
223                 while (ao_scheme_is_pair(list)) {
224                         cons = ao_scheme_poly_cons(list);
225                         m = ao_has_macro(cons->car);
226                         if (m) {
227                                 p = m;
228                                 break;
229                         }
230                         list = cons->cdr;
231                 }
232                 break;
233
234         default:
235                 p = AO_SCHEME_NIL;
236                 break;
237         }
238         MACRO_DEBUG(--macro_scan_depth; indent(); ao_scheme_printf("... %v\n", p));
239         return p;
240 }
241
242 static struct ao_scheme_builtin *
243 ao_scheme_get_builtin(ao_poly p)
244 {
245         if (ao_scheme_poly_type(p) == AO_SCHEME_BUILTIN)
246                 return ao_scheme_poly_builtin(p);
247         return NULL;
248 }
249
250 struct seen_builtin {
251         struct seen_builtin             *next;
252         struct ao_scheme_builtin        *builtin;
253 };
254
255 static struct seen_builtin *seen_builtins;
256
257 static int
258 ao_scheme_seen_builtin(struct ao_scheme_builtin *b)
259 {
260         struct seen_builtin     *s;
261
262         for (s = seen_builtins; s; s = s->next)
263                 if (s->builtin == b)
264                         return 1;
265         s = malloc (sizeof (struct seen_builtin));
266         s->builtin = b;
267         s->next = seen_builtins;
268         seen_builtins = s;
269         return 0;
270 }
271
272 static int
273 ao_scheme_read_eval_abort(FILE *read_file)
274 {
275         ao_poly in;
276
277         for(;;) {
278                 in = ao_scheme_read(read_file);
279                 if (in == _ao_scheme_atom_eof)
280                         break;
281                 (void) ao_scheme_eval(in);
282                 if (ao_scheme_exception) {
283                         ao_scheme_fprintf(stderr, "make_const failed on %v\n", in);
284                         return 0;
285                 }
286         }
287         return 1;
288 }
289
290 static FILE     *in;
291 static FILE     *out;
292
293 struct feature {
294         struct feature  *next;
295         char            name[];
296 };
297
298 static struct feature *enable;
299 static struct feature *disable;
300
301 static void
302 ao_scheme_add_feature(struct feature **list, char *name)
303 {
304         struct feature *feature = malloc (sizeof (struct feature) + strlen(name) + 1);
305         strcpy(feature->name, name);
306         feature->next = *list;
307         *list = feature;
308 }
309
310 static bool
311 _ao_scheme_has_feature(struct feature *list, const char *name, bool skip_undef)
312 {
313         if (skip_undef && !strcmp(name, "UNDEF"))
314                 return false;
315
316         while (list) {
317                 if (!strcmp(list->name, name))
318                         return true;
319                 list = list->next;
320         }
321         return false;
322 }
323
324 static bool
325 ao_scheme_has_undef(struct feature *list)
326 {
327         return _ao_scheme_has_feature(list, "UNDEF", false);
328 }
329
330 static bool
331 ao_scheme_has_feature(struct feature *list, const char *name)
332 {
333         return _ao_scheme_has_feature(list, name, true);
334 }
335
336 static void
337 ao_scheme_add_features(struct feature **list, const char *names)
338 {
339         char    *saveptr = NULL;
340         char    *name;
341         char    *copy = strdup(names);
342         char    *save = copy;
343
344         while ((name = strtok_r(copy, ",", &saveptr)) != NULL) {
345                 copy = NULL;
346                 if (!ao_scheme_has_feature(*list, name))
347                         ao_scheme_add_feature(list, name);
348         }
349         free(save);
350 }
351
352 int
353 ao_scheme_getc(void)
354 {
355         return getc(in);
356 }
357
358 static const struct option options[] = {
359         { .name = "out", .has_arg = 1, .val = 'o' },
360         { .name = "disable", .has_arg = 1, .val = 'd' },
361         { .name = "enable", .has_arg = 1, .val = 'e' },
362         { 0, 0, 0, 0 }
363 };
364
365 static void usage(char *program)
366 {
367         fprintf(stderr, "usage: %s [--out=<output>] [--disable={feature,...}] [--enable={feature,...} [input]\n", program);
368         exit(1);
369 }
370
371 int
372 main(int argc, char **argv)
373 {
374         int     f, o, an;
375         ao_poly val;
376         struct ao_scheme_atom   *a;
377         struct ao_scheme_builtin        *b;
378         struct feature                  *d;
379         int     in_atom = 0;
380         char    *out_name = NULL;
381         int     c;
382         enum ao_scheme_builtin_id       prev_func;
383         enum ao_scheme_builtin_id       target_func;
384         enum ao_scheme_builtin_id       func_map[_builtin_last];
385
386         in = stdin;
387         out = stdout;
388
389         while ((c = getopt_long(argc, argv, "o:d:e:", options, NULL)) != -1) {
390                 switch (c) {
391                 case 'o':
392                         out_name = optarg;
393                         break;
394                 case 'd':
395                         ao_scheme_add_features(&disable, optarg);
396                         break;
397                 case 'e':
398                         ao_scheme_add_features(&enable, optarg);
399                         break;
400                 default:
401                         usage(argv[0]);
402                         break;
403                 }
404         }
405
406         ao_scheme_frame_init();
407
408         /* Boolean values #f and #t */
409         ao_scheme_bool_get(0);
410         ao_scheme_bool_get(1);
411
412         prev_func = _builtin_last;
413         target_func = 0;
414         b = NULL;
415         for (f = 0; f < (int) N_FUNC; f++) {
416                 if (ao_scheme_has_feature(enable, funcs[f].feature) || !ao_scheme_has_feature(disable, funcs[f].feature)) {
417                         if (funcs[f].func != prev_func) {
418                                 prev_func = funcs[f].func;
419                                 b = ao_scheme_make_builtin(prev_func, funcs[f].args);
420
421                                 /* Target may have only a subset of
422                                  * the enum values; record what those
423                                  * values will be here. This obviously
424                                  * depends on the functions in the
425                                  * array being in the same order as
426                                  * the enumeration; which
427                                  * ao_scheme_make_builtin ensures.
428                                  */
429                                 func_map[prev_func] = target_func++;
430                         }
431                         a = ao_scheme_atom_intern((char *) funcs[f].name);
432                         ao_scheme_atom_def(ao_scheme_atom_poly(a),
433                                            ao_scheme_builtin_poly(b));
434                 }
435         }
436
437         /* atoms */
438         for (an = 0; an < (int) N_ATOM; an++) {
439                 if (ao_scheme_has_feature(enable, atoms[an].feature) || !ao_scheme_has_feature(disable, atoms[an].feature))
440                         a = ao_scheme_atom_intern((char *) atoms[an].name);
441         }
442
443         while (argv[optind]) {
444                 in = fopen(argv[optind], "r");
445                 if (!in) {
446                         perror(argv[optind]);
447                         exit(1);
448                 }
449                 if (!ao_scheme_read_eval_abort(in)) {
450                         fprintf(stderr, "eval failed\n");
451                         exit(1);
452                 }
453                 fclose(in);
454                 optind++;
455         }
456
457         if (!ao_scheme_has_undef(enable) && ao_scheme_has_undef(disable)) {
458                 struct ao_scheme_cons cons;
459
460                 cons.car = _ao_scheme_atom_undef;
461                 cons.cdr = AO_SCHEME_NIL;
462                 ao_scheme_do_undef(&cons);
463         }
464
465         /* Reduce to referenced values */
466         ao_scheme_collect(AO_SCHEME_COLLECT_FULL);
467
468         for (f = 0; f < ao_scheme_frame_global->num; f++) {
469                 struct ao_scheme_frame_vals     *vals = ao_scheme_poly_frame_vals(ao_scheme_frame_global->vals);
470
471                 val = ao_has_macro(vals->vals[f].val);
472                 if (val != AO_SCHEME_NIL) {
473                         fprintf(stderr, "error: function %s contains unresolved macro: ",
474                                 ao_scheme_poly_atom(vals->vals[f].atom)->name);
475                         ao_scheme_poly_write(stderr, val, true);
476                         fprintf(stderr, "\n");
477                         exit(1);
478                 }
479
480                 /* Remap builtin enum values to match target set */
481                 b = ao_scheme_get_builtin(vals->vals[f].val);
482                 if (b != NULL) {
483                         if (!ao_scheme_seen_builtin(b))
484                                 b->func = func_map[b->func];
485                 }
486         }
487
488         if (out_name) {
489                 out = fopen(out_name, "w");
490                 if (!out) {
491                         perror(out_name);
492                         exit(1);
493                 }
494         }
495
496         fprintf(out, "/* Generated file, do not edit */\n\n");
497
498         for (d = disable; d; d = d->next)
499                 fprintf(out, "#undef AO_SCHEME_FEATURE_%s\n", d->name);
500
501         fprintf(out, "#define AO_SCHEME_POOL_CONST %d\n", ao_scheme_top);
502         fprintf(out, "extern const uint8_t ao_scheme_const[AO_SCHEME_POOL_CONST] __attribute__((aligned(4)));\n");
503         fprintf(out, "#define ao_builtin_atoms 0x%04x\n", ao_scheme_atom_poly(ao_scheme_atoms));
504         fprintf(out, "#define ao_builtin_frame 0x%04x\n", ao_scheme_frame_poly(ao_scheme_frame_global));
505         fprintf(out, "#define ao_scheme_const_checksum ((uint16_t) 0x%04x)\n", ao_fec_crc(ao_scheme_const, ao_scheme_top));
506
507         fprintf(out, "#define _ao_scheme_bool_false 0x%04x\n", ao_scheme_bool_poly(ao_scheme_false));
508         fprintf(out, "#define _ao_scheme_bool_true 0x%04x\n", ao_scheme_bool_poly(ao_scheme_true));
509
510         for (a = ao_scheme_atoms; a; a = ao_scheme_poly_atom(a->next)) {
511                 const char      *n = a->name;
512                 char            ch;
513                 fprintf(out, "#define _ao_scheme_atom_");
514                 while ((ch = *n++)) {
515                         if (isalnum(ch))
516                                 fprintf(out, "%c", ch);
517                         else
518                                 fprintf(out, "%02x", ch);
519                 }
520                 fprintf(out, "  0x%04x\n", ao_scheme_atom_poly(a));
521         }
522         fprintf(out, "#ifdef AO_SCHEME_CONST_BITS\n");
523         fprintf(out, "const uint8_t ao_scheme_const[AO_SCHEME_POOL_CONST] __attribute((aligned(4))) = {");
524         for (o = 0; o < ao_scheme_top; o++) {
525                 uint8_t ch;
526                 if ((o & 0xf) == 0)
527                         fprintf(out, "\n\t");
528                 else
529                         fprintf(out, " ");
530                 ch = ao_scheme_const[o];
531                 if (!in_atom)
532                         in_atom = is_atom(o);
533                 if (in_atom) {
534                         fprintf(out, " '%c',", ch);
535                         in_atom--;
536                 } else {
537                         fprintf(out, "0x%02x,", ch);
538                 }
539         }
540         fprintf(out, "\n};\n");
541         fprintf(out, "#endif /* AO_SCHEME_CONST_BITS */\n");
542         exit(0);
543 }