-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
384 lines (345 loc) · 11 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* CEKF - VM supporting amb
* Copyright (C) 2022-2023 Bill Hails
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <getopt.h>
#include <time.h>
#include <errno.h>
#include "common.h"
#include "ast.h"
#include "ast_debug.h"
#include "lambda_debug.h"
#include "lambda_conversion.h"
#include "lambda_simplification.h"
#include "annotate.h"
#include "anf.h"
#include "anf_normalize.h"
#include "memory.h"
#include "step.h"
#include "debug.h"
#include "anf_pp.h"
#include "bytecode.h"
#include "hash.h"
#include "lambda_pp.h"
#include "anf_normalize.h"
#include "bigint.h"
#include "tc_analyze.h"
#include "tc_debug.h"
#include "tpmc_mermaid.h"
#include "arithmetic.h"
#include "builtins_helper.h"
#include "inline.h"
#include "pratt.h"
#include "pratt_parser.h"
#include "pratt_scanner.h"
int report_flag = 0;
static int help_flag = 0;
static int anf_flag = 0;
static int ast_flag = 0;
static int lambda_flag = 0;
static int inline_flag = 0;
extern bool assertions_failed;
extern int assertions_accumulate;
static char *binary_output_file = NULL;
static char *binary_input_file = NULL;
static char *snippet = NULL;
extern AstStringArray *include_paths;
static void report_build_mode(char *prog) {
printf("%s - ", prog);
#ifdef BUILD_MODE
switch (BUILD_MODE) {
case 0:
printf("debug build\n");
break;
case 1:
printf("test build\n");
break;
case 2:
printf("production build\n");
break;
default:
printf("unrecognised build\n");
break;
}
#else
printf("unspecified build\n");
#endif
}
static void usage(char *prog, int status) {
report_build_mode(prog);
printf("usage: %s <options> [---] [<filename> [<arguments> ...]]\n", prog);
printf("options:\n%s",
" --anf Display the generated ANF.\n"
" --assertions-accumulate Don't exit on the first assertion failure.\n"
" --ast Display the parsed AST before lambda conversion.\n"
" --binary-in=<file> Read byte code from file.\n"
" --binary-out=<file> Write byte code to file.\n"
" --dump-bytecode Dump the generated bytecode.\n"
" -e<snippet>\n"
" --exec=<snippet> Execute the snippet of code directly\n"
" -h\n"
" --help This help.\n"
" -i<dir>\n"
" --include=<dir> Add dir to the list of directories to be\n"
" searched.\n"
" --inline Display the entire intermediate code\n"
" after inlining.\n"
" -l<function>\n"
" --lambda=<function> Display the intermediate code\n"
" generated for the function.\n"
" -l\n"
" --lambda Display the entire intermediate code.\n"
" --report Report statistics.\n"
" -m <function>\n"
" --tpmc=<function> Produce a mermaid graph of the\n"
" function's TPMC state table.\n"
);
exit(status);
}
static int processArgs(int argc, char *argv[]) {
int c;
while (1) {
static struct option long_options[] = {
{ "report", no_argument, &report_flag, 1 },
{ "anf", no_argument, &anf_flag, 1 },
{ "ast", no_argument, &ast_flag, 1 },
{ "dump-bytecode", no_argument, &dump_bytecode_flag, 1 },
{ "exec", required_argument, 0, 'e' },
{ "help", no_argument, 0, 'h' },
{ "inline", no_argument, &inline_flag, 1 },
{ "assertions-accumulate", no_argument, &assertions_accumulate, 1 },
{ "tpmc", required_argument, 0, 'm' },
{ "lambda", optional_argument, 0, 'l' },
{ "include", required_argument, 0, 'i' },
{ "binary-out", required_argument, 0, 'o' },
{ "binary-in", required_argument, 0, 'b' },
{ 0, 0, 0, 0 }
};
int option_index = 0;
c = getopt_long(argc, argv, "l::hm:e:i:o:b:", long_options, &option_index);
if (c == -1)
break;
if (c == 'm') {
tpmc_mermaid_function = optarg;
}
if (c == 'l') {
if (optarg) {
lambda_conversion_function = optarg;
} else {
lambda_flag = 1;
}
}
if (c == 'o') {
binary_output_file = optarg;
}
if (c == 'b') {
binary_input_file = optarg;
}
if (c == 'e') {
snippet = optarg;
}
if (c == 'i') {
pushAstStringArray(include_paths, strdup(optarg));
}
if (c == '?' || c == 'h') {
help_flag = 1;
}
}
if (help_flag) {
usage(argv[0], 0);
}
if (binary_input_file != NULL && binary_output_file != NULL) {
printf("incompatible --binary-out and --binary-in arguments\n");
usage(argv[0], 1);
}
if (optind >= argc && !binary_input_file && !snippet) {
eprintf("need filename or --binary-in or --e argument\n");
exit(1);
}
return optind;
}
static AstProg *parseFile(char *file) {
AstProg *prog = prattParseFile(file);
int save = PROTECT(prog);
if (hadErrors()) {
exit(1);
}
if (ast_flag) {
PrattUTF8 *dest = newPrattUTF8();
PROTECT(dest);
ppAstProg(dest, prog);
printf("%s\n", dest->entries);
}
UNPROTECT(save);
return prog;
}
static AstProg *parseString(char *string, char*origin) {
AstProg *prog = prattParseString(string, origin);
int save = PROTECT(prog);
if (hadErrors()) {
exit(1);
}
if (ast_flag) {
PrattUTF8 *dest = newPrattUTF8();
PROTECT(dest);
ppAstProg(dest, prog);
printf("%s\n", dest->entries);
}
UNPROTECT(save);
return prog;
}
static LamExp *convertProg(AstProg *prog) {
LamExp *exp = lamConvertProg(prog);
int save = PROTECT(exp);
if (hadErrors()) {
exit(1);
}
exp = lamPerformSimplifications(exp);
REPLACE_PROTECT(save, exp);
if (lambda_flag) {
ppLamExp(exp);
eprintf("\n");
}
UNPROTECT(save);
return exp;
}
static LamExp *inlineExp(LamExp *exp) __attribute__((unused));
static LamExp *inlineExp(LamExp *exp) {
exp = inlineLamExp(exp);
int save = PROTECT(exp);
if (inline_flag) {
ppLamExp(exp);
eprintf("\n");
}
UNPROTECT(save);
return exp;
}
static void typeCheck(LamExp *exp, BuiltIns *builtIns) {
TcEnv *env = tc_init(builtIns);
int save = PROTECT(env);
TcType *res __attribute__((unused)) = tc_analyze(exp, env);
if (hadErrors()) {
exit(1);
}
#ifdef DEBUG_TC
ppTcType(res);
eprintf("\n");
#endif
UNPROTECT(save);
}
static void annotate(Exp *anfExp, BuiltIns *builtIns) {
annotateAnf(anfExp, builtIns);
#ifdef DEBUG_ANF
ppExp(anfExp);
eprintf("\n");
#endif
}
static ByteCodeArray generateByteCodes(Exp *anfExp) {
ByteCodeArray byteCodes;
initByteCodeArray(&byteCodes, 8);
writeExp(anfExp, &byteCodes);
writeEnd(&byteCodes);
return byteCodes;
}
static void report(char *prog, clock_t begin, clock_t compiled, clock_t end) {
if (report_flag) {
printf("\n");
report_build_mode(prog);
double time_spent = (double) (end - begin) / CLOCKS_PER_SEC;
printf("elapsed time: %.4lf\n", time_spent);
double compile_time = (double) (compiled - begin) / CLOCKS_PER_SEC;
printf("compile time: %.4lf\n", compile_time);
double run_time = (double) (end - compiled) / CLOCKS_PER_SEC;
printf("run time: %.4lf\n", run_time);
reportMemory();
reportSteps();
}
}
int main(int argc, char *argv[]) {
clock_t begin = clock();
initProtection();
init_arithmetic();
initNamespaces();
include_paths = newAstStringArray();
int save = PROTECT(include_paths);
initFileIdStack();
initParserStack();
int nextargc = processArgs(argc, argv);
BuiltIns *builtIns = registerBuiltIns(argc, binary_input_file ? nextargc : nextargc + 1, argv);
PROTECT(builtIns);
if (binary_input_file) {
ByteCodeArray byteCodes;
initByteCodeArray(&byteCodes, 8);
switch (readBinaryInputFile(&byteCodes, binary_input_file)) {
case BYTECODES_BADFILE:
fprintf(stderr, "%s: %s\n", binary_input_file, strerror(errno));
exit(1);
case BYTECODES_BADHEADER:
fprintf(stderr, "%s: invalid header\n", binary_input_file);
exit(1);
case BYTECODES_BADVERSION:
fprintf(stderr, "%s: invalid version\n", binary_input_file);
exit(1);
case BYTECODES_OK:
break;
default:
cant_happen("unrecognised status from bytecode reader");
}
clock_t compiled = clock();
run(byteCodes, builtIns);
UNPROTECT(save);
clock_t end = clock();
report(argv[0], begin, compiled, end);
} else {
AstProg *prog = NULL;
if (snippet) {
prog = parseString(snippet, "command-line");
} else {
prog = parseFile(argv[nextargc++]);
}
int save2 = PROTECT(prog);
LamExp *exp = convertProg(prog);
REPLACE_PROTECT(save2, exp);
typeCheck(exp, builtIns);
exp = inlineExp(exp);
REPLACE_PROTECT(save2, exp);
Exp *anfExp = anfNormalize(exp);
REPLACE_PROTECT(save2, anfExp);
if (anf_flag) {
ppExp(anfExp);
eprintf("\n");
}
annotate(anfExp, builtIns);
ByteCodeArray byteCodes = generateByteCodes(anfExp);
if (binary_output_file != NULL) {
if (!writeBinaryOutputFile(&byteCodes, binary_output_file)) {
fprintf(stderr, "%s: %s\n", binary_output_file, strerror(errno));
exit(1);
}
exit(0);
}
UNPROTECT(save2);
clock_t compiled = clock();
run(byteCodes, builtIns);
UNPROTECT(save);
clock_t end = clock();
report(argv[0], begin, compiled, end);
}
exit(assertions_failed ? 1 : 0);
}