- Try to move OS/target specific stuff to lang_features.h and main.c
[cparser] / main.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #include <config.h>
21
22 #define _GNU_SOURCE
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdbool.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <assert.h>
30
31 #ifdef _WIN32
32
33 #include <fcntl.h>
34 #include <io.h>
35
36 /* no eXecute on Win32 */
37 #define X_OK 0
38 #define W_OK 2
39 #define R_OK 4
40
41 #define O_RDWR          _O_RDWR
42 #define O_CREAT         _O_CREAT
43 #define O_EXCL          _O_EXCL
44 #define O_BINARY        _O_BINARY
45
46 /* remap some names, we are not in the POSIX world */
47 #define access(fname, mode)      _access(fname, mode)
48 #define mktemp(tmpl)             _mktemp(tmpl)
49 #define open(fname, oflag, mode) _open(fname, oflag, mode)
50 #define fdopen(fd, mode)         _fdopen(fd, mode)
51 #define popen(cmd, mode)         _popen(cmd, mode)
52 #define pclose(file)             _pclose(file)
53
54 #else
55 #include <unistd.h>
56 #define HAVE_MKSTEMP
57 #endif
58
59 #ifndef WITH_LIBCORE
60 #define WITH_LIBCORE
61 #endif
62
63 #include <libfirm/firm.h>
64 #include <libfirm/be.h>
65
66 #include "lexer.h"
67 #include "token_t.h"
68 #include "types.h"
69 #include "type_hash.h"
70 #include "parser.h"
71 #include "type_t.h"
72 #include "ast2firm.h"
73 #include "diagnostic.h"
74 #include "lang_features.h"
75 #include "driver/firm_opt.h"
76 #include "driver/firm_cmdline.h"
77 #include "adt/error.h"
78 #include "write_fluffy.h"
79 #include "write_caml.h"
80 #include "revision.h"
81 #include "warning.h"
82
83 #ifndef PREPROCESSOR
84 #ifdef __APPLE__
85 #define PREPROCESSOR "gcc -E -std=c99 -m32 -U__STRICT_ANSI__"
86 #else
87 #define PREPROCESSOR "cpp -std=c99 -m32 -U__STRICT_ANSI__"
88 #endif
89 #endif
90
91 #ifndef LINKER
92 #define LINKER    "gcc -m32"
93 #endif
94
95 #ifndef ASSEMBLER
96 #ifdef __APPLE__
97 #define ASSEMBLER "gcc -c -xassembler"
98 #else
99 #define ASSEMBLER "as --32"
100 #endif
101 #endif
102
103 /** The current c mode/dialect. */
104 lang_features_t c_mode = _C89 | _ANSI | _C99 | _GNUC;
105
106 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
107 unsigned int machine_size = 32;
108
109 /** true if the char type is signed. */
110 bool char_is_signed = true;
111
112 /** true for strict language checking. */
113 bool strict_mode = false;
114
115 /** use builtins for some libc functions */
116 bool use_builtins = false;
117
118 /** we have extern function with const attribute. */
119 bool have_const_functions = false;
120
121 /* to switch on printing of implicit casts */
122 extern bool print_implicit_casts;
123
124 /* to switch on printing of parenthesis to indicate operator precedence */
125 extern bool print_parenthesis;
126
127 static int             verbose;
128 static struct obstack  cppflags_obst, ldflags_obst;
129 static char            dep_target[1024];
130 static const char     *outname;
131
132 typedef struct file_list_entry_t file_list_entry_t;
133
134 typedef enum filetype_t {
135         FILETYPE_AUTODETECT,
136         FILETYPE_C,
137         FILETYPE_PREPROCESSED_C,
138         FILETYPE_CXX,
139         FILETYPE_PREPROCESSED_CXX,
140         FILETYPE_ASSEMBLER,
141         FILETYPE_PREPROCESSED_ASSEMBLER,
142         FILETYPE_OBJECT,
143         FILETYPE_UNKNOWN
144 } filetype_t;
145
146 struct file_list_entry_t {
147         const char        *name; /**< filename or NULL for stdin */
148         filetype_t         type;
149         file_list_entry_t *next;
150 };
151
152 #if defined(_DEBUG) || defined(FIRM_DEBUG)
153 /**
154  * Debug printf implementation.
155  *
156  * @param fmt  printf style format parameter
157  */
158 void dbg_printf(const char *fmt, ...)
159 {
160         va_list list;
161
162         if (firm_dump.debug_print) {
163                 va_start(list, fmt);
164                 vprintf(fmt, list);
165                 va_end(list);
166         }  /* if */
167 }
168 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
169
170 static void initialize_firm(void)
171 {
172         firm_early_init();
173
174         dump_consts_local(1);
175         dump_keepalive_edges(1);
176 }
177
178 static void get_output_name(char *buf, size_t buflen, const char *inputname,
179                             const char *newext)
180 {
181         size_t last_dot = 0xffffffff;
182         size_t i = 0;
183
184         if(inputname == NULL) {
185                 snprintf(buf, buflen, "a%s", newext);
186                 return;
187         }
188
189         for(const char *c = inputname; *c != 0; ++c) {
190                 if(*c == '.')
191                         last_dot = i;
192                 ++i;
193         }
194         if(last_dot == 0xffffffff)
195                 last_dot = i;
196
197         if(last_dot >= buflen)
198                 panic("filename too long");
199         memcpy(buf, inputname, last_dot);
200
201         size_t extlen = strlen(newext) + 1;
202         if(extlen + last_dot >= buflen)
203                 panic("filename too long");
204         memcpy(buf+last_dot, newext, extlen);
205 }
206
207 #include "builtins.h"
208
209 static translation_unit_t *do_parsing(FILE *const in, const char *const input_name)
210 {
211         start_parsing();
212
213         if (use_builtins) {
214                 lexer_open_buffer(builtins, sizeof(builtins)-1, "<builtin>");
215                 parse();
216         }
217
218         lexer_open_stream(in, input_name);
219         parse();
220
221         translation_unit_t *unit = finish_parsing();
222         return unit;
223 }
224
225 static void lextest(FILE *in, const char *fname)
226 {
227         lexer_open_stream(in, fname);
228
229         do {
230                 lexer_next_preprocessing_token();
231                 print_token(stdout, &lexer_token);
232                 puts("");
233         } while(lexer_token.type != T_EOF);
234 }
235
236 static void add_flag(struct obstack *obst, const char *format, ...)
237 {
238         va_list ap;
239         va_start(ap, format);
240
241         char buf[4096];
242         vsnprintf(buf, sizeof(buf), format, ap);
243
244         /* escape stuff... */
245         obstack_1grow(obst, ' ');
246         for (char *c = buf; *c != '\0'; ++c) {
247                 switch(*c) {
248                 case '"':
249                 case '\'':
250                 case '`':
251                 case ' ':
252                 case '\t':
253                 case '\n':
254                 case '\r':
255                 case '\\':
256                 case '$':
257                 case '(':
258                 case ')':
259                         obstack_1grow(obst, '\\');
260                         /* fallthrough */
261                 default:
262                         obstack_1grow(obst, *c);
263                         break;
264                 }
265         }
266
267         va_end(ap);
268 }
269
270 static const char *type_to_string(type_t *type)
271 {
272         assert(type->kind == TYPE_ATOMIC);
273         return get_atomic_kind_name(type->atomic.akind);
274 }
275
276 static FILE *preprocess(const char *fname)
277 {
278         obstack_1grow(&cppflags_obst, '\0');
279         const char *flags = obstack_finish(&cppflags_obst);
280
281         obstack_printf(&cppflags_obst, "%s", PREPROCESSOR);
282
283         /* setup default defines */
284         add_flag(&cppflags_obst, "-U__WCHAR_TYPE__");
285         add_flag(&cppflags_obst, "-D__WCHAR_TYPE__=%s",
286                  type_to_string(type_wchar_t));
287         add_flag(&cppflags_obst, "-U__SIZE_TYPE__");
288         add_flag(&cppflags_obst, "-D__SIZE_TYPE__=%s", type_to_string(type_size_t));
289
290         /* handle dependency generation */
291         if (dep_target[0] != '\0') {
292                 add_flag(&cppflags_obst, "-MF");
293                 add_flag(&cppflags_obst, "%s", dep_target);
294                 if (outname != NULL) {
295                                 add_flag(&cppflags_obst, "-MQ");
296                                 add_flag(&cppflags_obst, "%s", outname);
297                 }
298         }
299         if (flags[0] != '\0') {
300                 obstack_printf(&cppflags_obst, " %s", flags);
301         }
302         add_flag(&cppflags_obst, "%s", fname);
303
304         obstack_1grow(&cppflags_obst, '\0');
305         const char *buf = obstack_finish(&cppflags_obst);
306         if(verbose) {
307                 puts(buf);
308         }
309
310         FILE *f = popen(buf, "r");
311         if(f == NULL) {
312                 fprintf(stderr, "invoking preprocessor failed\n");
313                 exit(1);
314         }
315
316         return f;
317 }
318
319 static void assemble(const char *out, const char *in)
320 {
321         char buf[4096];
322
323         snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
324         if(verbose) {
325                 puts(buf);
326         }
327
328         int err = system(buf);
329         if(err != 0) {
330                 fprintf(stderr, "assembler reported an error\n");
331                 exit(1);
332         }
333 }
334
335 static const char *try_dir(const char *dir)
336 {
337         if(dir == NULL)
338                 return dir;
339         if(access(dir, R_OK | W_OK | X_OK) == 0)
340                 return dir;
341         return NULL;
342 }
343
344 static const char *get_tempdir(void)
345 {
346         static const char *tmpdir = NULL;
347
348         if(tmpdir != NULL)
349                 return tmpdir;
350
351         if(tmpdir == NULL)
352                 tmpdir = try_dir(getenv("TMPDIR"));
353         if(tmpdir == NULL)
354                 tmpdir = try_dir(getenv("TMP"));
355         if(tmpdir == NULL)
356                 tmpdir = try_dir(getenv("TEMP"));
357
358 #ifdef P_tmpdir
359         if(tmpdir == NULL)
360                 tmpdir = try_dir(P_tmpdir);
361 #endif
362
363         if(tmpdir == NULL)
364                 tmpdir = try_dir("/var/tmp");
365         if(tmpdir == NULL)
366                 tmpdir = try_dir("/usr/tmp");
367         if(tmpdir == NULL)
368                 tmpdir = try_dir("/tmp");
369
370         if(tmpdir == NULL)
371                 tmpdir = ".";
372
373         return tmpdir;
374 }
375
376 #ifndef HAVE_MKSTEMP
377 /* cheap and nasty mkstemp replacement */
378 static int mkstemp(char *templ)
379 {
380         mktemp(templ);
381         return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
382 }
383 #endif
384
385 /**
386  * an own version of tmpnam, which: writes in a buffer, emits no warnings
387  * during linking (like glibc/gnu ld do for tmpnam)...
388  */
389 static FILE *make_temp_file(char *buffer, size_t buflen, const char *prefix)
390 {
391         const char *tempdir = get_tempdir();
392
393         snprintf(buffer, buflen, "%s/%sXXXXXX", tempdir, prefix);
394
395         int fd = mkstemp(buffer);
396         if(fd == -1) {
397                 fprintf(stderr, "couldn't create temporary file: %s\n",
398                         strerror(errno));
399                 exit(1);
400         }
401         FILE *out = fdopen(fd, "w");
402         if(out == NULL) {
403                 fprintf(stderr, "couldn't create temporary file FILE*\n");
404                 exit(1);
405         }
406
407         return out;
408 }
409
410 /**
411  * Do the necessary lowering for compound parameters.
412  */
413 void lower_compound_params(void)
414 {
415         lower_params_t params;
416
417         params.def_ptr_alignment    = 4;
418         params.flags                = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
419         params.hidden_params        = ADD_HIDDEN_ALWAYS_IN_FRONT;
420         params.find_pointer_type    = NULL;
421         params.ret_compound_in_regs = NULL;
422         lower_calls_with_compounds(&params);
423 }
424
425 typedef enum compile_mode_t {
426         BenchmarkParser,
427         PreprocessOnly,
428         ParseOnly,
429         Compile,
430         CompileDump,
431         CompileAssemble,
432         CompileAssembleLink,
433         LexTest,
434         PrintAst,
435         PrintFluffy,
436         PrintCaml
437 } compile_mode_t;
438
439 static void usage(const char *argv0)
440 {
441         fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
442 }
443
444 static void print_cparser_version(void) {
445         firm_version_t ver;
446         firm_get_version(&ver);
447
448         printf("cparser (%s) using libFirm (%u.%u",
449                 cparser_REVISION, ver.major, ver.minor);
450         if(ver.revision[0] != 0) {
451                 putchar(' ');
452                 fputs(ver.revision, stdout);
453         }
454         if(ver.build[0] != 0) {
455                 putchar(' ');
456                 fputs(ver.build, stdout);
457         }
458         puts(")\n");
459 }
460
461 static void set_be_option(const char *arg)
462 {
463         int res = firm_be_option(arg);
464         (void) res;
465         assert(res);
466 }
467
468 static void set_option(const char *arg)
469 {
470         int res = firm_option(arg);
471         (void) res;
472         assert(res);
473 }
474
475 static void copy_file(FILE *dest, FILE *input)
476 {
477         char buf[16384];
478
479         while (!feof(input) && !ferror(dest)) {
480                 size_t read = fread(buf, 1, sizeof(buf), input);
481                 if(fwrite(buf, 1, read, dest) != read) {
482                         perror("couldn't write output");
483                 }
484         }
485 }
486
487 static inline bool streq(char const* a, char const* b)
488 {
489         return strcmp(a, b) == 0;
490 }
491
492 static inline bool strstart(char const* str, char const* start)
493 {
494         do {
495                 if (*start == '\0')
496                         return true;
497         } while (*str++ == *start++);
498         return false;
499 }
500
501 static FILE *open_file(const char *filename)
502 {
503         if (streq(filename, "-")) {
504                 return stdin;
505         }
506
507         FILE *in = fopen(filename, "r");
508         if(in == NULL) {
509                 fprintf(stderr, "Couldn't open '%s': %s\n", filename,
510                                 strerror(errno));
511                 exit(1);
512         }
513
514         return in;
515 }
516
517 static filetype_t get_filetype_from_string(const char *string)
518 {
519         if (streq(string, "c") || streq(string, "c-header"))
520                 return FILETYPE_C;
521         if (streq(string, "c++") || streq(string, "c++-header"))
522                 return FILETYPE_CXX;
523         if (streq(string, "assembler"))
524                 return FILETYPE_PREPROCESSED_ASSEMBLER;
525         if (streq(string, "assembler-with-cpp"))
526                 return FILETYPE_ASSEMBLER;
527         if (streq(string, "none"))
528                 return FILETYPE_AUTODETECT;
529
530         return FILETYPE_UNKNOWN;
531 }
532
533 static void init_os_support(void)
534 {
535         /* OS option must be set to the backend */
536         switch (firm_opt.os_support) {
537         case OS_SUPPORT_MINGW:
538                 set_be_option("ia32-gasmode=mingw");
539                 break;
540         case OS_SUPPORT_LINUX:
541                 set_be_option("ia32-gasmode=elf");
542                 break;
543         case OS_SUPPORT_MACHO:
544                 set_be_option("ia32-gasmode=macho");
545                 set_be_option("ia32-stackalign=4");
546                 set_be_option("pic");
547                 break;
548         }
549 }
550
551 typedef enum lang_standard_t {
552         STANDARD_DEFAULT, /* gnu99 (for C, GCC does gnu89) or gnu++98 (for C++) */
553         STANDARD_ANSI,    /* c89 (for C) or c++98 (for C++) */
554         STANDARD_C89,     /* ISO C90 (sic) */
555         STANDARD_C90,     /* ISO C90 as modified in amendment 1 */
556         STANDARD_C99,     /* ISO C99 */
557         STANDARD_GNU89,   /* ISO C90 plus GNU extensions (including some C99) */
558         STANDARD_GNU99,   /* ISO C99 plus GNU extensions */
559         STANDARD_CXX98,   /* ISO C++ 1998 plus amendments */
560         STANDARD_GNUXX98  /* ISO C++ 1998 plus amendments and GNU extensions */
561 } lang_standard_t;
562
563 int main(int argc, char **argv)
564 {
565         initialize_firm();
566
567         const char        *dumpfunction         = NULL;
568         compile_mode_t     mode                 = CompileAssembleLink;
569         int                opt_level            = 1;
570         int                result               = EXIT_SUCCESS;
571         char               cpu_arch[16]         = "ia32";
572         file_list_entry_t *files                = NULL;
573         file_list_entry_t *last_file            = NULL;
574         bool               construct_dep_target = false;
575         struct obstack     file_obst;
576
577         /* hack for now... */
578         if (strstr(argv[0], "pptest") != NULL) {
579                 extern int pptest_main(int argc, char **argv);
580                 return pptest_main(argc, argv);
581         }
582
583         obstack_init(&cppflags_obst);
584         obstack_init(&ldflags_obst);
585         obstack_init(&file_obst);
586
587 #define GET_ARG_AFTER(def, args)                                             \
588         def = &arg[sizeof(args)-1];                                              \
589         if(def[0] == '\0') {                                                     \
590                 ++i;                                                                 \
591                 if(i >= argc) {                                                      \
592                         fprintf(stderr, "error: expected argument after '" args "'\n");  \
593                         argument_errors = true;                                          \
594                         break;                                                           \
595                 }                                                                    \
596                 def = argv[i];                                                       \
597                 if(def[0] == '-' && def[1] != '\0') {                                \
598                         fprintf(stderr, "error: expected argument after '" args "'\n");  \
599                         argument_errors = true;                                          \
600                         continue;                                                        \
601                 }                                                                    \
602         }
603
604 #define SINGLE_OPTION(ch) (option[0] == (ch) && option[1] == '\0')
605
606         /* early options parsing (find out optimisation level and OS) */
607         for (int i = 1; i < argc; ++i) {
608                 const char *arg = argv[i];
609                 if(arg[0] != '-')
610                         continue;
611
612                 const char *option = &arg[1];
613                 if (option[0] == 'O') {
614                         sscanf(&option[1], "%d", &opt_level);
615                 }
616                 if (strcmp(arg, "-fwin32") == 0) {
617                         firm_opt.os_support = OS_SUPPORT_MINGW;
618                 } else if (strcmp(arg, "-fmac") == 0) {
619                         firm_opt.os_support = OS_SUPPORT_MACHO;
620                 } else if (strcmp(arg, "-flinux") == 0) {
621                         firm_opt.os_support = OS_SUPPORT_LINUX;
622                 }
623         }
624
625         /* set target/os specific stuff */
626         init_os_support();
627
628         /* apply optimisation level */
629         switch(opt_level) {
630         case 0:
631                 set_option("no-opt");
632                 break;
633         case 1:
634                 set_option("no-inline");
635                 break;
636         default:
637         case 4:
638                 set_option("strict-aliasing");
639                 /* use_builtins = true; */
640                 /* fallthrough */
641         case 3:
642                 set_option("cond-eval");
643                 set_option("if-conv");
644                 /* fallthrough */
645         case 2:
646                 set_option("inline");
647                 set_option("deconv");
648                 set_be_option("omitfp");
649                 break;
650         }
651
652         /* parse rest of options */
653         lang_standard_t standard        = STANDARD_DEFAULT;
654         lang_features_t features_on     = 0;
655         lang_features_t features_off    = 0;
656         filetype_t      forced_filetype = FILETYPE_AUTODETECT;
657         bool            help_displayed  = false;
658         bool            argument_errors = false;
659         for(int i = 1; i < argc; ++i) {
660                 const char *arg = argv[i];
661                 if (arg[0] == '-' && arg[1] != '\0') {
662                         /* an option */
663                         const char *option = &arg[1];
664                         if(option[0] == 'o') {
665                                 GET_ARG_AFTER(outname, "-o");
666                         } else if(option[0] == 'g') {
667                                 set_be_option("debuginfo=stabs");
668                                 set_be_option("omitfp=no");
669                                 set_be_option("ia32-nooptcc=yes");
670                         } else if(SINGLE_OPTION('c')) {
671                                 mode = CompileAssemble;
672                         } else if(SINGLE_OPTION('E')) {
673                                 mode = PreprocessOnly;
674                         } else if(SINGLE_OPTION('S')) {
675                                 mode = Compile;
676                         } else if(option[0] == 'O') {
677                                 continue;
678                         } else if(option[0] == 'I') {
679                                 const char *opt;
680                                 GET_ARG_AFTER(opt, "-I");
681                                 add_flag(&cppflags_obst, "-I%s", opt);
682                         } else if(option[0] == 'D') {
683                                 const char *opt;
684                                 GET_ARG_AFTER(opt, "-D");
685                                 add_flag(&cppflags_obst, "-D%s", opt);
686                         } else if(option[0] == 'U') {
687                                 const char *opt;
688                                 GET_ARG_AFTER(opt, "-U");
689                                 add_flag(&cppflags_obst, "-U%s", opt);
690                         } else if(option[0] == 'l') {
691                                 const char *opt;
692                                 GET_ARG_AFTER(opt, "-l");
693                                 add_flag(&ldflags_obst, "-l%s", opt);
694                         } else if(option[0] == 'L') {
695                                 const char *opt;
696                                 GET_ARG_AFTER(opt, "-L");
697                                 add_flag(&ldflags_obst, "-L%s", opt);
698                         } else if(SINGLE_OPTION('v')) {
699                                 verbose = 1;
700                         } else if(SINGLE_OPTION('w')) {
701                                 memset(&warning, 0, sizeof(warning));
702                         } else if(option[0] == 'x') {
703                                 const char *opt;
704                                 GET_ARG_AFTER(opt, "-x");
705                                 forced_filetype = get_filetype_from_string(opt);
706                                 if (forced_filetype == FILETYPE_UNKNOWN) {
707                                         fprintf(stderr, "Unknown language '%s'\n", opt);
708                                         argument_errors = true;
709                                 }
710                         } else if (streq(option, "M")) {
711                                 mode = PreprocessOnly;
712                                 add_flag(&cppflags_obst, "-M");
713                         } else if (streq(option, "MMD") ||
714                                    streq(option, "MD")) {
715                             construct_dep_target = true;
716                                 add_flag(&cppflags_obst, "-%s", option);
717                         } else if(streq(option, "MM")  ||
718                                    streq(option, "MP")) {
719                                 add_flag(&cppflags_obst, "-%s", option);
720                         } else if (streq(option, "MT") ||
721                                    streq(option, "MQ") ||
722                                    streq(option, "MF")) {
723                                 const char *opt;
724                                 GET_ARG_AFTER(opt, "-MT");
725                                 add_flag(&cppflags_obst, "-%s", option);
726                                 add_flag(&cppflags_obst, "%s", opt);
727                         } else if (streq(option, "pipe")) {
728                                 /* here for gcc compatibility */
729                         } else if (option[0] == 'f') {
730                                 char const *orig_opt;
731                                 GET_ARG_AFTER(orig_opt, "-f");
732
733                                 if (strstart(orig_opt, "align-loops=") ||
734                                     strstart(orig_opt, "align-jumps=") ||
735                                     strstart(orig_opt, "align-functions=")) {
736                                         fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
737                                 } else {
738                                         char const *opt         = orig_opt;
739                                         bool        truth_value = true;
740                                         if (opt[0] == 'n' && opt[1] == 'o' && opt[2] == '-') {
741                                                 truth_value = false;
742                                                 opt += 3;
743                                         }
744
745                                         if (streq(opt, "dollars-in-identifiers")) {
746                                                 allow_dollar_in_symbol = truth_value;
747                                         } if (streq(opt, "builtins")) {
748                                                 use_builtins = truth_value;
749                                         } else if (streq(opt, "short-wchar")) {
750                                                 wchar_atomic_kind = truth_value ? ATOMIC_TYPE_USHORT
751                                                         : ATOMIC_TYPE_INT;
752                                         } else if (streq(opt, "syntax-only")) {
753                                                 mode = truth_value ? ParseOnly : CompileAssembleLink;
754                                         } else if (streq(opt, "omit-frame-pointer")) {
755                                                 set_be_option(truth_value ? "omitfp" : "omitfp=no");
756                                         } else if (streq(opt, "strength-reduce")) {
757                                                 firm_option(truth_value ? "strength-red" : "no-strength-red");
758                                         } else if (streq(opt, "fast-math")               ||
759                                                    streq(opt, "jump-tables")             ||
760                                                    streq(opt, "unroll-loops")            ||
761                                                    streq(opt, "expensive-optimizations") ||
762                                                    streq(opt, "common")                  ||
763                                                    streq(opt, "PIC")                     ||
764                                                    streq(opt, "align-loops")             ||
765                                                    streq(opt, "align-jumps")             ||
766                                                    streq(opt, "align-functions")) {
767                                                 fprintf(stderr, "ignoring gcc option '-f%s'\n", orig_opt);
768                                         } else {
769                                                 int res = firm_option(orig_opt);
770                                                 if (res == 0) {
771                                                         fprintf(stderr, "error: unknown Firm option '-f%s'\n",
772                                                                 orig_opt);
773                                                         argument_errors = true;
774                                                         continue;
775                                                 } else if (res == -1) {
776                                                         help_displayed = true;
777                                                 }
778                                         }
779                                 }
780                         } else if (option[0] == 'b') {
781                                 const char *opt;
782                                 GET_ARG_AFTER(opt, "-b");
783                                 int res = firm_be_option(opt);
784                                 if (res == 0) {
785                                         fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
786                                                 opt);
787                                         argument_errors = true;
788                                 } else if (res == -1) {
789                                         help_displayed = true;
790                                 } else if (strstart(opt, "isa=")) {
791                                         strncpy(cpu_arch, opt, sizeof(cpu_arch));
792                                 }
793                         } else if (option[0] == 'W') {
794                                 if (strstart(option + 1, "l,")) // a gcc-style linker option
795                                 {
796                                         const char *opt;
797                                         GET_ARG_AFTER(opt, "-Wl,");
798                                         add_flag(&ldflags_obst, "-Wl,%s", opt);
799                                 }
800                                 else set_warning_opt(&option[1]);
801                         } else if(option[0] == 'm') {
802                                 /* -m options */
803                                 const char *opt;
804                                 char arch_opt[64];
805
806                                 GET_ARG_AFTER(opt, "-m");
807                                 if (strstart(opt, "arch=")) {
808                                         GET_ARG_AFTER(opt, "-march=");
809                                         snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
810                                         int res = firm_be_option(arch_opt);
811                                         if (res == 0)
812                                                 argument_errors = true;
813                                         else {
814                                                 snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
815                                                 int res = firm_be_option(arch_opt);
816                                                 if (res == 0)
817                                                         argument_errors = true;
818                                         }
819                                 } else if (strstart(opt, "tune=")) {
820                                         GET_ARG_AFTER(opt, "-mtune=");
821                                         snprintf(arch_opt, sizeof(arch_opt), "%s-opt=%s", cpu_arch, opt);
822                                         int res = firm_be_option(arch_opt);
823                                         if (res == 0)
824                                                 argument_errors = true;
825                                 } else if (strstart(opt, "cpu=")) {
826                                         GET_ARG_AFTER(opt, "-mcpu=");
827                                         snprintf(arch_opt, sizeof(arch_opt), "%s-arch=%s", cpu_arch, opt);
828                                         int res = firm_be_option(arch_opt);
829                                         if (res == 0)
830                                                 argument_errors = true;
831                                 } else if (strstart(opt, "fpmath=")) {
832                                         GET_ARG_AFTER(opt, "-mfpmath=");
833                                         if (streq(opt, "387"))
834                                                 opt = "x87";
835                                         else if (streq(opt, "sse"))
836                                                 opt = "sse2";
837                                         else {
838                                                 fprintf(stderr, "error: option -mfpumath supports only 387 or sse\n");
839                                                 argument_errors = true;
840                                         }
841                                         if(!argument_errors) {
842                                                 snprintf(arch_opt, sizeof(arch_opt), "%s-fpunit=%s", cpu_arch, opt);
843                                                 int res = firm_be_option(arch_opt);
844                                                 if (res == 0)
845                                                         argument_errors = true;
846                                         }
847                                 } else if (strstart(opt, "preferred-stack-boundary=")) {
848                                         GET_ARG_AFTER(opt, "-mpreferred-stack-boundary=");
849                                         snprintf(arch_opt, sizeof(arch_opt), "%s-stackalign=%s", cpu_arch, opt);
850                                         int res = firm_be_option(arch_opt);
851                                         if (res == 0)
852                                                 argument_errors = true;
853                                 } else if (streq(opt, "omit-leaf-frame-pointer")) {
854                                         set_be_option("omitleaffp=1");
855                                 } else if (streq(opt, "no-omit-leaf-frame-pointer")) {
856                                         set_be_option("omitleaffp=0");
857                                 } else {
858                                         char *endptr;
859                                         long int value = strtol(opt, &endptr, 10);
860                                         if (*endptr != '\0') {
861                                                 fprintf(stderr, "error: wrong option '-m %s'\n",  opt);
862                                                 argument_errors = true;
863                                         }
864                                         if (value != 16 && value != 32 && value != 64) {
865                                                 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
866                                                 argument_errors = true;
867                                         } else {
868                                                 machine_size = (unsigned int)value;
869                                         }
870                                 }
871                         } else if (streq(option, "pg")) {
872                                 set_be_option("gprof");
873                                 add_flag(&ldflags_obst, "-pg");
874                         } else if (streq(option, "pedantic") ||
875                                    streq(option, "ansi")) {
876                                 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
877                         } else if (streq(option, "shared")) {
878                                 add_flag(&ldflags_obst, "-shared");
879                         } else if (strstart(option, "std=")) {
880                                 const char *const o = &option[4];
881                                 standard =
882                                         streq(o, "c++")            ? STANDARD_CXX98   :
883                                         streq(o, "c++98")          ? STANDARD_CXX98   :
884                                         streq(o, "c89")            ? STANDARD_C89     :
885                                         streq(o, "c99")            ? STANDARD_C99     :
886                                         streq(o, "c9x")            ? STANDARD_C99     : // deprecated
887                                         streq(o, "gnu++98")        ? STANDARD_GNUXX98 :
888                                         streq(o, "gnu89")          ? STANDARD_GNU89   :
889                                         streq(o, "gnu99")          ? STANDARD_GNU99   :
890                                         streq(o, "gnu9x")          ? STANDARD_GNU99   : // deprecated
891                                         streq(o, "iso9899:1990")   ? STANDARD_C89     :
892                                         streq(o, "iso9899:199409") ? STANDARD_C90     :
893                                         streq(o, "iso9899:1999")   ? STANDARD_C99     :
894                                         streq(o, "iso9899:199x")   ? STANDARD_C99     : // deprecated
895                                         (fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg), standard);
896                         } else if (streq(option, "version")) {
897                                 print_cparser_version();
898                         } else if (option[0] == '-') {
899                                 /* double dash option */
900                                 ++option;
901                                 if (streq(option, "gcc")) {
902                                         features_on  |=  _GNUC;
903                                         features_off &= ~_GNUC;
904                                 } else if (streq(option, "no-gcc")) {
905                                         features_on  &= ~_GNUC;
906                                         features_off |=  _GNUC;
907                                 } else if (streq(option, "ms")) {
908                                         features_on  |=  _MS;
909                                         features_off &= ~_MS;
910                                 } else if (streq(option, "no-ms")) {
911                                         features_on  &= ~_MS;
912                                         features_off |=  _MS;
913                                 } else if (streq(option, "signed-chars")) {
914                                         char_is_signed = true;
915                                 } else if (streq(option, "unsigned-chars")) {
916                                         char_is_signed = false;
917                                 } else if (streq(option, "strict")) {
918                                         strict_mode = true;
919                                 } else if (streq(option, "lextest")) {
920                                         mode = LexTest;
921                                 } else if (streq(option, "benchmark")) {
922                                         mode = BenchmarkParser;
923                                 } else if (streq(option, "print-ast")) {
924                                         mode = PrintAst;
925                                 } else if (streq(option, "print-implicit-cast")) {
926                                         print_implicit_casts = true;
927                                 } else if (streq(option, "print-parenthesis")) {
928                                         print_parenthesis = true;
929                                 } else if (streq(option, "print-fluffy")) {
930                                         mode = PrintFluffy;
931                                 } else if (streq(option, "print-caml")) {
932                                         mode = PrintCaml;
933                                 } else if (streq(option, "version")) {
934                                         print_cparser_version();
935                                         exit(EXIT_SUCCESS);
936                                 } else if (streq(option, "dump-function")) {
937                                         ++i;
938                                         if(i >= argc) {
939                                                 fprintf(stderr, "error: "
940                                                         "expected argument after '--dump-function'\n");
941                                                 argument_errors = true;
942                                                 break;
943                                         }
944                                         dumpfunction = argv[i];
945                                         mode         = CompileDump;
946                                 } else {
947                                         fprintf(stderr, "error: unknown argument '%s'\n", arg);
948                                         argument_errors = true;
949                                 }
950                         } else {
951                                 fprintf(stderr, "error: unknown argument '%s'\n", arg);
952                                 argument_errors = true;
953                         }
954                 } else {
955                         filetype_t type = forced_filetype;
956                         if (type == FILETYPE_AUTODETECT) {
957                                 if (streq(arg, "-")) {
958                                         /* - implicitly means C source file */
959                                         type = FILETYPE_C;
960                                 } else {
961                                         const char *suffix = strrchr(arg, '.');
962                                         /* Ensure there is at least one char before the suffix */
963                                         if (suffix != NULL && suffix != arg) {
964                                                 ++suffix;
965                                                 type =
966                                                         streq(suffix, "S")   ? FILETYPE_ASSEMBLER              :
967                                                         streq(suffix, "a")   ? FILETYPE_OBJECT                 :
968                                                         streq(suffix, "c")   ? FILETYPE_C                      :
969                                                         streq(suffix, "cc")  ? FILETYPE_CXX                    :
970                                                         streq(suffix, "cpp") ? FILETYPE_CXX                    :
971                                                         streq(suffix, "cxx") ? FILETYPE_CXX                    :
972                                                         streq(suffix, "h")   ? FILETYPE_C                      :
973                                                         streq(suffix, "o")   ? FILETYPE_OBJECT                 :
974                                                         streq(suffix, "s")   ? FILETYPE_PREPROCESSED_ASSEMBLER :
975                                                         streq(suffix, "so")  ? FILETYPE_OBJECT                 :
976                                                         FILETYPE_AUTODETECT;
977                                         }
978                                 }
979
980                                 if (type == FILETYPE_AUTODETECT) {
981                                         fprintf(stderr, "'%s': file format not recognized\n", arg);
982                                         continue;
983                                 }
984                         }
985
986                         file_list_entry_t *entry
987                                 = obstack_alloc(&file_obst, sizeof(entry[0]));
988                         memset(entry, 0, sizeof(entry[0]));
989                         entry->name = arg;
990                         entry->type = type;
991
992                         if (last_file != NULL) {
993                                 last_file->next = entry;
994                         } else {
995                                 files = entry;
996                         }
997                         last_file = entry;
998                 }
999         }
1000
1001         if (files == NULL) {
1002                 fprintf(stderr, "error: no input files specified\n");
1003                 argument_errors = true;
1004         }
1005
1006         if (help_displayed) {
1007                 return !argument_errors;
1008         }
1009         if (argument_errors) {
1010                 usage(argv[0]);
1011                 return 1;
1012         }
1013
1014         /* we do the lowering in ast2firm */
1015         firm_opt.lower_bitfields = FALSE;
1016
1017         gen_firm_init();
1018         init_symbol_table();
1019         init_types();
1020         init_typehash();
1021         init_basic_types();
1022         init_lexer();
1023         init_ast();
1024         init_parser();
1025         init_ast2firm();
1026
1027         if (construct_dep_target) {
1028                 if (outname != 0 && strlen(outname) >= 2) {
1029                         get_output_name(dep_target, sizeof(dep_target), outname, ".d");
1030                 } else {
1031                         get_output_name(dep_target, sizeof(dep_target), files->name, ".d");
1032                 }
1033         } else {
1034                 dep_target[0] = '\0';
1035         }
1036
1037         char outnamebuf[4096];
1038         if (outname == NULL) {
1039                 const char *filename = files->name;
1040
1041                 switch(mode) {
1042                 case BenchmarkParser:
1043                 case PrintAst:
1044                 case PrintFluffy:
1045                 case PrintCaml:
1046                 case LexTest:
1047                 case PreprocessOnly:
1048                 case ParseOnly:
1049                         outname = "-";
1050                         break;
1051                 case Compile:
1052                         get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".s");
1053                         outname = outnamebuf;
1054                         break;
1055                 case CompileAssemble:
1056                         get_output_name(outnamebuf, sizeof(outnamebuf), filename, ".o");
1057                         outname = outnamebuf;
1058                         break;
1059                 case CompileDump:
1060                         get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
1061                                         ".vcg");
1062                         outname = outnamebuf;
1063                         break;
1064                 case CompileAssembleLink:
1065 #ifdef _WIN32
1066                         outname = "a.exe";
1067 #else
1068                         outname = "a.out";
1069 #endif
1070                         break;
1071                 }
1072         }
1073
1074         assert(outname != NULL);
1075
1076         FILE *out;
1077         if (streq(outname, "-")) {
1078                 out = stdout;
1079         } else {
1080                 out = fopen(outname, "w");
1081                 if(out == NULL) {
1082                         fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
1083                                         strerror(errno));
1084                         return 1;
1085                 }
1086         }
1087
1088         file_list_entry_t *file;
1089         for (file = files; file != NULL; file = file->next) {
1090                 char        asm_tempfile[1024];
1091                 const char *filename = file->name;
1092                 filetype_t  filetype = file->type;
1093
1094                 if (filetype == FILETYPE_OBJECT)
1095                         continue;
1096
1097                 FILE *in = NULL;
1098                 if (mode == LexTest) {
1099                         if (in == NULL)
1100                                 in = open_file(filename);
1101                         lextest(in, filename);
1102                         fclose(in);
1103                         exit(EXIT_SUCCESS);
1104                 }
1105
1106                 FILE *preprocessed_in = NULL;
1107                 switch (filetype) {
1108                         case FILETYPE_C:         filetype = FILETYPE_PREPROCESSED_C;         goto preprocess;
1109                         case FILETYPE_CXX:       filetype = FILETYPE_PREPROCESSED_CXX;       goto preprocess;
1110                         case FILETYPE_ASSEMBLER: filetype = FILETYPE_PREPROCESSED_ASSEMBLER; goto preprocess;
1111 preprocess:
1112                                 /* no support for input on FILE* yet */
1113                                 if (in != NULL)
1114                                         panic("internal compiler error: in for preprocessor != NULL");
1115
1116                                 preprocessed_in = preprocess(filename);
1117                                 if (mode == PreprocessOnly) {
1118                                         copy_file(out, preprocessed_in);
1119                                         int result = pclose(preprocessed_in);
1120                                         fclose(out);
1121                                         return result;
1122                                 }
1123
1124                                 in = preprocessed_in;
1125                                 break;
1126
1127                         default:
1128                                 break;
1129                 }
1130
1131                 FILE *asm_out;
1132                 if(mode == Compile) {
1133                         asm_out = out;
1134                 } else {
1135                         asm_out = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "ccs");
1136                 }
1137
1138                 if (in == NULL)
1139                         in = open_file(filename);
1140
1141                 /* preprocess and compile */
1142                 if (filetype == FILETYPE_PREPROCESSED_C) {
1143                         char const* invalid_mode;
1144                         switch (standard) {
1145                                 case STANDARD_ANSI:
1146                                 case STANDARD_C89:   c_mode = _C89;                break;
1147                                 /* TODO ^v determine difference between these two */
1148                                 case STANDARD_C90:   c_mode = _C89;                break;
1149                                 case STANDARD_C99:   c_mode = _C89 | _C99;         break;
1150                                 case STANDARD_GNU89: c_mode = _C89 |        _GNUC; break;
1151
1152 default_c_warn:
1153                                         fprintf(stderr,
1154                                                         "warning: command line option \"-std=%s\" is not valid for C\n",
1155                                                         invalid_mode);
1156                                         /* FALLTHROUGH */
1157                                 case STANDARD_DEFAULT:
1158                                 case STANDARD_GNU99:   c_mode = _C89 | _C99 | _GNUC; break;
1159
1160                                 case STANDARD_CXX98:   invalid_mode = "c++98"; goto default_c_warn;
1161                                 case STANDARD_GNUXX98: invalid_mode = "gnu98"; goto default_c_warn;
1162                         }
1163                         goto do_parsing;
1164                 } else if (filetype == FILETYPE_PREPROCESSED_CXX) {
1165                         char const* invalid_mode;
1166                         switch (standard) {
1167                                 case STANDARD_C89:   invalid_mode = "c89";   goto default_cxx_warn;
1168                                 case STANDARD_C90:   invalid_mode = "c90";   goto default_cxx_warn;
1169                                 case STANDARD_C99:   invalid_mode = "c99";   goto default_cxx_warn;
1170                                 case STANDARD_GNU89: invalid_mode = "gnu89"; goto default_cxx_warn;
1171                                 case STANDARD_GNU99: invalid_mode = "gnu99"; goto default_cxx_warn;
1172
1173                                 case STANDARD_ANSI:
1174                                 case STANDARD_CXX98: c_mode = _CXX; break;
1175
1176 default_cxx_warn:
1177                                         fprintf(stderr,
1178                                                         "warning: command line option \"-std=%s\" is not valid for C++\n",
1179                                                         invalid_mode);
1180                                 case STANDARD_DEFAULT:
1181                                 case STANDARD_GNUXX98: c_mode = _CXX | _GNUC; break;
1182                         }
1183
1184 do_parsing:
1185                         c_mode |= features_on;
1186                         c_mode &= ~features_off;
1187                         init_tokens();
1188                         translation_unit_t *const unit = do_parsing(in, filename);
1189
1190                         /* prints the AST even if errors occurred */
1191                         if (mode == PrintAst) {
1192                                 type_set_output(out);
1193                                 ast_set_output(out);
1194                                 print_ast(unit);
1195                         }
1196
1197                         if(error_count > 0) {
1198                                 /* parsing failed because of errors */
1199                                 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count,
1200                                         warning_count);
1201                                 result = EXIT_FAILURE;
1202                                 continue;
1203                         } else if(warning_count > 0) {
1204                                 fprintf(stderr, "%u warning(s)\n", warning_count);
1205                         }
1206
1207                         if (in == preprocessed_in) {
1208                                 int pp_result = pclose(preprocessed_in);
1209                                 if (pp_result != EXIT_SUCCESS) {
1210                                         exit(EXIT_FAILURE);
1211                                 }
1212                         }
1213
1214                         if(mode == BenchmarkParser) {
1215                                 return result;
1216                         } else if(mode == PrintFluffy) {
1217                                 write_fluffy_decls(out, unit);
1218                                 continue;
1219                         } else if (mode == PrintCaml) {
1220                                 write_caml_decls(out, unit);
1221                                 continue;
1222                         }
1223
1224                         translation_unit_to_firm(unit);
1225
1226                         if (mode == ParseOnly) {
1227                                 continue;
1228                         }
1229
1230                         if (mode == CompileDump) {
1231                                 /* find irg */
1232                                 ident    *id     = new_id_from_str(dumpfunction);
1233                                 ir_graph *irg    = NULL;
1234                                 int       n_irgs = get_irp_n_irgs();
1235                                 for(int i = 0; i < n_irgs; ++i) {
1236                                         ir_graph *tirg   = get_irp_irg(i);
1237                                         ident    *irg_id = get_entity_ident(get_irg_entity(tirg));
1238                                         if(irg_id == id) {
1239                                                 irg = tirg;
1240                                                 break;
1241                                         }
1242                                 }
1243
1244                                 if(irg == NULL) {
1245                                         fprintf(stderr, "No graph for function '%s' found\n",
1246                                                 dumpfunction);
1247                                         exit(1);
1248                                 }
1249
1250                                 dump_ir_block_graph_file(irg, out);
1251                                 fclose(out);
1252                                 exit(0);
1253                         }
1254
1255                         gen_firm_finish(asm_out, filename, /*c_mode=*/1,
1256                                         have_const_functions);
1257                         if (asm_out != out) {
1258                                 fclose(asm_out);
1259                         }
1260                 } else if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1261                         copy_file(asm_out, in);
1262                         if (in == preprocessed_in) {
1263                                 int pp_result = pclose(preprocessed_in);
1264                                 if (pp_result != EXIT_SUCCESS) {
1265                                         return pp_result;
1266                                 }
1267                         }
1268                         if(asm_out != out) {
1269                                 fclose(asm_out);
1270                         }
1271                 }
1272
1273                 if (mode == Compile)
1274                         continue;
1275
1276                 /* if we're here then we have preprocessed assembly */
1277                 filename = asm_tempfile;
1278                 filetype = FILETYPE_PREPROCESSED_ASSEMBLER;
1279
1280                 /* assemble */
1281                 if (filetype == FILETYPE_PREPROCESSED_ASSEMBLER) {
1282                         char        temp[1024];
1283                         const char *filename_o;
1284                         if(mode == CompileAssemble) {
1285                                 fclose(out);
1286                                 filename_o = outname;
1287                         } else {
1288                                 FILE *tempf = make_temp_file(temp, sizeof(temp), "cco");
1289                                 fclose(tempf);
1290                                 filename_o = temp;
1291                         }
1292
1293                         assemble(filename_o, filename);
1294
1295                         size_t len = strlen(filename_o) + 1;
1296                         filename = obstack_copy(&file_obst, filename_o, len);
1297                         filetype = FILETYPE_OBJECT;
1298                 }
1299
1300                 /* ok we're done here, process next file */
1301                 file->name = filename;
1302                 file->type = filetype;
1303         }
1304
1305         if (result != EXIT_SUCCESS)
1306                 return result;
1307
1308         /* link program file */
1309         if(mode == CompileAssembleLink) {
1310                 obstack_1grow(&ldflags_obst, '\0');
1311                 const char *flags = obstack_finish(&ldflags_obst);
1312
1313                 /* construct commandline */
1314                 obstack_printf(&file_obst, "%s", LINKER);
1315                 for (file_list_entry_t *entry = files; entry != NULL;
1316                                 entry = entry->next) {
1317                         if (entry->type != FILETYPE_OBJECT)
1318                                 continue;
1319
1320                         add_flag(&file_obst, "%s", entry->name);
1321                 }
1322
1323                 add_flag(&file_obst, "-o");
1324                 add_flag(&file_obst, outname);
1325                 obstack_printf(&file_obst, "%s", flags);
1326                 obstack_1grow(&file_obst, '\0');
1327
1328                 char *commandline = obstack_finish(&file_obst);
1329
1330                 if(verbose) {
1331                         puts(commandline);
1332                 }
1333                 int err = system(commandline);
1334                 if(err != EXIT_SUCCESS) {
1335                         fprintf(stderr, "linker reported an error\n");
1336                         exit(1);
1337                 }
1338         }
1339
1340         obstack_free(&cppflags_obst, NULL);
1341         obstack_free(&ldflags_obst, NULL);
1342         obstack_free(&file_obst, NULL);
1343
1344         exit_ast2firm();
1345         exit_parser();
1346         exit_ast();
1347         exit_lexer();
1348         exit_typehash();
1349         exit_types();
1350         exit_tokens();
1351         exit_symbol_table();
1352         return 0;
1353 }