-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.c
411 lines (355 loc) · 8.54 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include <config.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/time.h>
#include <libfirm/firm.h>
#include <libfirm/be.h>
#include "driver/firm_opt.h"
#include "driver/firm_machine.h"
#include "type.h"
#include "parser.h"
#include "ast_t.h"
#include "semantic.h"
#include "ast2firm.h"
#include "plugins.h"
#include "type_hash.h"
#include "mangle.h"
#include "adt/error.h"
#include "adt/strutil.h"
#define LINKER "gcc -m32"
typedef struct file_list_entry_t file_list_entry_t;
struct file_list_entry_t {
const char *name; /**< filename or NULL for stdin */
file_list_entry_t *next;
};
static file_list_entry_t *temp_files;
static machine_triple_t *target_machine;
static bool dump_graphs;
static bool dump_asts;
static bool verbose;
static bool had_parse_errors;
typedef enum compile_mode_t {
Compile,
CompileAndLink
} compile_mode_t;
static void get_output_name(char *buf, size_t buflen, const char *inputname,
const char *newext)
{
size_t last_dot = 0xffffffff;
size_t i = 0;
for (const char *c = inputname; *c != 0; ++c) {
if (*c == '.')
last_dot = i;
++i;
}
if (last_dot == 0xffffffff)
last_dot = i;
if (last_dot >= buflen)
panic("filename too long");
memcpy(buf, inputname, last_dot);
size_t extlen = strlen(newext) + 1;
if (extlen + last_dot >= buflen)
panic("filename too long");
memcpy(buf+last_dot, newext, extlen);
}
static void dump_ast(const context_t *context, const char *name,
const char *ext)
{
if (!dump_asts)
return;
char filename[4096];
get_output_name(filename, sizeof(filename), name, ext);
FILE* out = fopen(filename, "w");
if (out == NULL) {
fprintf(stderr, "Warning: couldn't open '%s': %s\n", filename,
strerror(errno));
} else {
print_ast(out, context);
}
fclose(out);
}
static void do_parse_file(FILE *in, const char *input_name)
{
bool result = parse_file(in, input_name);
if (!result) {
fprintf(stderr, "syntax errors found...\n");
had_parse_errors = true;
return;
}
}
static void do_check_semantic(void)
{
bool result = check_semantic();
if (!result) {
fprintf(stderr, "Semantic errors found\n");
exit(1);
}
const module_t *module = modules;
for ( ; module != NULL; module = module->next) {
dump_ast(&module->context, module->name->string,
"-semantic.txt");
}
}
static void do_link(const char *in, const char *out)
{
char buf[4096];
if (out == NULL) {
out = "a.out";
}
int res = snprintf(buf, sizeof(buf), "%s -x assembler %s -o %s", LINKER, in, out);
if (res < 0 || res >= (int) sizeof(buf)) {
panic("Couldn't construct linker commandline (too long?)");
}
if (verbose) {
puts(buf);
}
int err = system(buf);
if (err != 0) {
fprintf(stderr, "linker reported an error\n");
exit(1);
}
}
static void usage(const char *argv0)
{
fprintf(stderr, "Usage: %s input1 input2 [-o output]\n", argv0);
}
static void setup_target(void)
{
if (firm_is_unixish_os(target_machine)) {
set_add_underscore_prefix(false);
} else if (firm_is_darwin_os(target_machine)
|| firm_is_windows_os(target_machine)) {
set_add_underscore_prefix(true);
} else {
panic("Unsupported operating system ");
}
}
static const char *try_dir(const char *dir)
{
if (dir == NULL)
return dir;
if (access(dir, R_OK | W_OK | X_OK) == 0)
return dir;
return NULL;
}
static const char *get_tempdir(void)
{
static const char *tmpdir = NULL;
if (tmpdir != NULL)
return tmpdir;
if (tmpdir == NULL)
tmpdir = try_dir(getenv("TMPDIR"));
if (tmpdir == NULL)
tmpdir = try_dir(getenv("TMP"));
if (tmpdir == NULL)
tmpdir = try_dir(getenv("TEMP"));
#ifdef P_tmpdir
if (tmpdir == NULL)
tmpdir = try_dir(P_tmpdir);
#endif
if (tmpdir == NULL)
tmpdir = try_dir("/var/tmp");
if (tmpdir == NULL)
tmpdir = try_dir("/usr/tmp");
if (tmpdir == NULL)
tmpdir = try_dir("/tmp");
if (tmpdir == NULL)
tmpdir = ".";
return tmpdir;
}
/**
* an own version of tmpnam, which: writes in a buffer, emits no warnings
* during linking (like glibc/gnu ld do for tmpnam)...
*/
static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
{
const char *tempdir = get_tempdir();
snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
int fd = mkstemp(buffer);
if (fd == -1) {
fprintf(stderr, "could not create temporary file: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
FILE *out = fdopen(fd, "w");
if (out == NULL) {
fprintf(stderr, "could not create temporary file FILE*\n");
exit(EXIT_FAILURE);
}
file_list_entry_t *entry = xmalloc(sizeof(*entry));
memset(entry, 0, sizeof(*entry));
size_t name_len = strlen(buffer) + 1;
char *name = malloc(name_len);
memcpy(name, buffer, name_len);
entry->name = name;
entry->next = temp_files;
temp_files = entry;
return out;
}
static void free_temp_files(void)
{
file_list_entry_t *entry = temp_files;
file_list_entry_t *next;
for ( ; entry != NULL; entry = next) {
next = entry->next;
unlink(entry->name);
free((char*) entry->name);
free(entry);
}
temp_files = NULL;
}
int main(int argc, const char **argv)
{
int opt_level = 2;
init_symbol_table();
init_tokens();
init_type_module();
init_typehash();
init_ast_module();
init_parser();
init_semantic_module();
search_plugins();
initialize_plugins();
gen_firm_init();
init_ast2firm();
init_mangle();
/* early options parsing */
for (int i = 1; i < argc; ++i) {
const char *arg = argv[i];
if (arg[0] != '-')
continue;
const char *option = &arg[1];
if (option[0] == 'O') {
sscanf(&option[1], "%d", &opt_level);
}
}
const char *target = getenv("TARGET");
if (target != NULL)
target_machine = firm_parse_machine_triple(target);
if (target_machine == NULL)
target_machine = firm_get_host_machine();
choose_optimization_pack(opt_level);
setup_firm_for_machine(target_machine);
setup_target();
const char *outname = NULL;
compile_mode_t mode = CompileAndLink;
int parsed = 0;
for (int i = 1; i < argc; ++i) {
const char *arg = argv[i];
if (strcmp(arg, "-o") == 0) {
++i;
if (i >= argc) {
usage(argv[0]);
return 1;
}
outname = argv[i];
} else if (strstart(arg, "-O")) {
/* already processed in first pass */
} else if (strcmp(arg, "--dump") == 0) {
dump_graphs = 1;
dump_asts = 1;
} else if (strcmp(arg, "--dump-ast") == 0) {
dump_asts = 1;
} else if (strcmp(arg, "--dump-graph") == 0) {
dump_graphs = 1;
} else if (strcmp(arg, "--help") == 0) {
usage(argv[0]);
return 0;
} else if (strcmp(arg, "-S") == 0) {
mode = Compile;
} else if (strcmp(arg, "-c") == 0) {
mode = CompileAndLink;
} else if (strcmp(arg, "-v") == 0) {
verbose = 1;
} else if (strncmp(arg, "-b", 2) == 0) {
const char *bearg = arg+2;
if (bearg[0] == 0) {
++i;
if (i >= argc) {
usage(argv[0]);
return 1;
}
bearg = argv[i];
}
if (!be_parse_arg(bearg)) {
fprintf(stderr, "Invalid backend option: %s\n", bearg);
usage(argv[0]);
return 1;
}
if (strcmp(bearg, "help") == 0) {
return 1;
}
} else if (arg[0] == '-') {
fprintf(stderr, "Invalid option '%s'\n", arg);
return 1;
} else {
const char *filename = argv[i];
FILE *in;
if (strcmp(filename, "-") == 0) {
in = stdin;
/* nitpicking: is there a way so we can't have a normal file
* with the same name? probably not... */
filename = "<stdin>";
} else {
in = fopen(filename, "r");
if (in == NULL) {
fprintf(stderr, "Couldn't open file '%s' for reading: %s\n",
filename, strerror(errno));
exit(1);
}
}
do_parse_file(in, filename);
parsed++;
if (in != stdin) {
fclose(in);
}
}
}
if (parsed == 0) {
fprintf(stderr, "Error: no input files specified\n");
return 0;
}
if (had_parse_errors) {
return 1;
}
do_check_semantic();
ast2firm(modules);
const char *asmname;
char temp[1024];
if (mode == Compile) {
asmname = outname;
} else {
FILE *tempf = make_temp_file(temp, sizeof(temp), "ccs");
fclose(tempf);
asmname = temp;
}
FILE* asm_out = fopen(asmname, "w");
if (asm_out == NULL) {
fprintf(stderr, "Couldn't open output '%s'\n", asmname);
return 1;
}
generate_code(asm_out, asmname);
fclose(asm_out);
if (mode == CompileAndLink) {
do_link(asmname, outname);
}
//free_temp_files();
(void)free_temp_files;
gen_firm_finish();
exit_mangle();
exit_ast2firm();
free_plugins();
exit_semantic_module();
exit_parser();
exit_ast_module();
exit_type_module();
exit_typehash();
exit_tokens();
exit_symbol_table();
return 0;
}