-
Notifications
You must be signed in to change notification settings - Fork 14
/
hh_compiler.c
393 lines (342 loc) · 9.67 KB
/
hh_compiler.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
385
386
387
388
389
390
391
392
393
/* This file is part of Hedgehog LISP.
* Copyright (C) 2003, 2004, 2005 Oliotalo Ltd.
* See file LICENSE.LGPL for pertinent licensing conditions.
*
* Authors: Kenneth Oksanen <[email protected]>
* Lars Wirzenius <[email protected]>
*/
/* The main compiler driver file. */
#define HH_COMPILER 1
#include "hh_common.h"
#include "hh_ast.h"
#include "hh_macroexpand.h"
#include "hh_opt.h"
#include "hh_lambda.h"
#include "hh_uses.h"
#include "hh_codegen.h"
#include "hh_peephole.h"
#include "hh_output.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#ifndef sun
#include <getopt.h>
#endif
#ifndef DEFAULT_PRELUDE
#define DEFAULT_PRELUDE "prelude.d"
#endif
void hh_print_compiler(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
static void *safe_malloc(size_t bytes)
{
void *p;
if (bytes == 0)
bytes = 1;
p = malloc(bytes);
if (p == NULL) {
fprintf(stderr, "malloc failed allocating %lu bytes\n",
(unsigned long) bytes);
exit(1);
}
return p;
}
static char *make_output_name(const char *orig, const char *new_suffix)
{
char *new, *slash, *dot;
new = safe_malloc(strlen(orig) + strlen(new_suffix) + 1);
strcpy(new, orig);
/* Remove directories so that only basename remains. */
for (;;) {
slash = strrchr(new, '/');
if (slash == NULL)
break;
if (slash[1] == '\0') {
slash[0] = '\0';
continue;
}
memmove(new, slash + 1, strlen(slash + 1) + 1);
}
/* Replace last existing suffix or add suffix, if none was there. */
dot = strrchr(new, '.');
if (dot == NULL)
strcat(new, new_suffix);
else
strcpy(dot, new_suffix);
return new;
}
static void hh_usage(const char *argv0)
{
printf("\
This is hhc, the HedgeHog Lisp compiler.\n\
Usage: %s [options] file...\n\
-h, --help Display this help and exit\n\
-D <name> Define the given conditional compilation name.\n\
-g Add debugging info to the compiled byte code.\n\
-o <file> Place the byte code program into the given file.\n\
The default is the last source code file name with\n\
the suffix replaced with \".hlo\".\n\
-p, --prelude <file> Name of the standard prelude file, or '-' if none.\n\
Default is \"%s\".\n\
-x, --hex Produce a hex dump of the byte code.\n\
If no output file is explicitely specified, \n\
write the hex dump to stdout.\n\
-X, --hex-c Produce a C constant containing the hex dump\n\
of the byte code. If no output file is explicitely\n\
specified, write the hex dump to stdout.\n\
-v, --verbose <level> Dump various debugging information about the\n\
compilation process\n\
This program is distributed under the General Public License.\n\
Report bugs to <[email protected]>\n",
argv0, DEFAULT_PRELUDE);
exit(0);
}
static int hh_verbose = 0;
#define MAX_N_PRELUDES 256
static char *hh_preludes[MAX_N_PRELUDES];
int hh_n_preludes = 0;
static int ends_with(const char *str, const char *pat)
{
size_t len, patlen;
len = strlen(str);
patlen = strlen(pat);
return len >= patlen && strcmp(str + len - patlen, pat) == 0;
}
static int cmp(const void *a, const void *b)
{
return strcmp(*(const char **) a, *(const char **) b);
}
static void find_prelude_files(const char *dirname)
{
DIR *dir;
struct dirent *ent;
char *tab[MAX_N_PRELUDES];
int n, i;
char *s;
dir = opendir(dirname);
if (dir == NULL) {
perror("opendir failed");
fprintf(stderr, "Can not open default prelude directory '%s'.\n", dirname);
return;
}
n = 0;
while ((ent = readdir(dir)) != NULL) {
if (!ends_with(ent->d_name, ".hl"))
continue;
if (n >= MAX_N_PRELUDES) {
fprintf(stderr, "Too many prelude files.\n");
exit(1);
}
s = safe_malloc(strlen(dirname) + strlen(ent->d_name) + 2);
sprintf(s, "%s/%s", dirname, ent->d_name);
tab[n++] = s;
}
closedir(dir);
if (hh_n_preludes + n >= MAX_N_PRELUDES) {
fprintf(stderr, "Too many prelude files.\n");
exit(1);
}
qsort(tab, n, sizeof(tab[0]), cmp);
for (i = 0; i < n; ++i)
hh_preludes[hh_n_preludes++] = tab[i];
}
#ifndef sun
static struct option hh_longopts[] = {
{ "prelude", required_argument, NULL, 'p' },
{ "verbose", optional_argument, NULL, 'v' },
{ "debug", no_argument, NULL, 'g' },
{ "hex", no_argument, NULL, 'x' },
{ "hex-c", no_argument, NULL, 'X' },
{ "help", no_argument, NULL, 'h' },
{ NULL, 0, NULL, 0 }
};
#endif
int main(int argc, char **argv)
{
int c, i, generate_debug_data = 0, default_preludes_only = 1;
hh_ast_t *module, *prog = NULL, *n;
hh_code_t *insns;
char *last_filename = NULL, *output_filename = NULL, *asm_filename;
FILE *code_fp = NULL, *asm_fp;
hh_output_type_t output_type = HH_BYTECODE;
hh_n_preludes = 0;
#ifdef sun
/* We can't assume getopt_long here. */
while ((c = getopt(argc, argv, "D:p:o:v:hxXg")) != -1)
#else
while ((c = getopt_long(argc, argv, "D:p:o:v:hxXg", hh_longopts, &optind))
!= -1)
#endif
switch (c) {
case 'p':
HH_ASSERT(optarg != NULL);
if (strcmp(optarg, "-") == 0)
hh_n_preludes = -1;
else {
if (default_preludes_only)
hh_n_preludes = 0;
default_preludes_only = 0;
find_prelude_files(optarg);
}
break;
case 'o':
HH_ASSERT(optarg != NULL);
output_filename = optarg;
break;
case 'v':
if (optarg != NULL) {
char *endptr;
hh_verbose = strtol(optarg, &endptr, 0);
if (endptr == optarg || *endptr != '\0') {
fprintf(stderr, "%s: option '-v' requires an integer argument\n",
argv[0]);
exit(1);
}
} else
hh_verbose = 1;
break;
case 'g':
generate_debug_data = 1;
break;
case 'x':
output_type = HH_HEX;
break;
case 'X':
output_type = HH_HEX_C;
break;
case 'D':
HH_ASSERT(optarg != NULL);
hh_directive_define(optarg);
break;
case 'h':
hh_usage(argv[0]);
HH_NOTREACHED;
exit(0);
break;
}
if (optind >= argc) {
fprintf(stderr, "No input files given.\n");
exit(1);
}
if (hh_n_preludes == 0)
find_prelude_files(DEFAULT_PRELUDE);
else if (hh_n_preludes == -1)
hh_n_preludes = 0;
#ifdef HH_LINUX
hh_directive_define("HH_LINUX");
#endif
#ifdef HH_SUNOS
hh_directive_define("HH_SUNOS");
#endif
#ifdef HH_BSD
hh_directive_define("HH_BSD");
#endif
#ifdef HH_UNIX
hh_directive_define("HH_UNIX");
#endif
hh_ast_init();
hh_gen_init(generate_debug_data);
/* Read prelude files. */
for (i = 0; i < hh_n_preludes; i++) {
if (hh_verbose)
fprintf(stderr, "Reading prelude '%s'.\n", hh_preludes[i]);
module = hh_ast_read_file(hh_preludes[i]);
if (hh_verbose) {
hh_ast_dump(module);
fprintf(stderr, "\n");
}
if (prog == NULL)
prog = module;
else if (prog != NULL) {
/* Append the module to the end of the whole program. */
for (n = prog; n->u.ast[1] != NULL; n = n->u.ast[1])
HH_ASSERT(n->arity == 2);
n->u.ast[1] = module;
}
}
/* Treat the rest of the arguments as input files. Read them all
into `prog'. */
for (i = optind; i < argc; i++) {
if (hh_verbose)
fprintf(stderr, "Reading file %s:\n", argv[i]);
last_filename = argv[i];
module = hh_ast_read_file(argv[i]);
if (hh_verbose) {
hh_ast_dump(module);
fprintf(stderr, "\n");
}
if (prog == NULL)
prog = module;
else if (prog != NULL) {
/* Append the module to the end of the whole program. */
for (n = prog; n->u.ast[1] != NULL; n = n->u.ast[1])
HH_ASSERT(n->arity == 2);
n->u.ast[1] = module;
}
}
/* After lexing and parsing the program comes macro expansion,
syntax tree level optimizations, uses-analysis which throws away
unused code, linearization to byte code, and output.
Some day we may add additional peephole optimization passes. */
if (hh_verbose)
fprintf(stderr, "After macro expansion:\n");
hh_macroexpand(&prog, NULL, 0);
if (hh_verbose >= 2) {
hh_ast_dump(prog);
fprintf(stderr, "\nAfter lambda-lifting:\n");
}
prog = hh_lambda(prog);
if (hh_verbose >= 2) {
hh_ast_dump(prog);
fprintf(stderr, "\nAfter algebraic optimizations:\n");
}
prog = hh_opt(prog);
if (hh_verbose) {
hh_ast_dump(prog);
fprintf(stderr, "\nFinal byte code as assembler:\n");
}
hh_uses(prog);
insns = hh_gen_code(prog);
insns = hh_peephole(insns);
/* Output the compiled byte code in the specified format. */
if (output_filename == NULL) {
if (output_type == HH_BYTECODE) {
if (last_filename == NULL) {
fprintf(stderr,
"No output or input file (other than prelude) specified.\n");
exit(1);
}
output_filename = make_output_name(last_filename, ".hlo");
} else
/* We're generating the byte code dump in some more readable
form. Throw it into stdout. */
code_fp = stdout;
}
if (code_fp == NULL) {
HH_ASSERT(output_filename != NULL);
code_fp = fopen(output_filename, "wb");
if (code_fp == NULL) {
fprintf(stderr, "Could not open output file `%s' for writing.\n",
output_filename);
exit(1);
}
}
asm_filename = make_output_name(last_filename, ".hls");
asm_fp = fopen(asm_filename, "w");
if (asm_fp == NULL) {
fprintf(stderr, "Could not open output file `%s' for writing.\n",
asm_filename);
exit(1);
}
hh_output(insns, code_fp, output_type, generate_debug_data, asm_fp);
fclose(asm_fp);
fclose(code_fp);
return 0;
}