-Werror implemented
[cparser] / main.c
1 #include <config.h>
2
3 #define _GNU_SOURCE
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdbool.h>
8 #include <errno.h>
9 #include <string.h>
10 #include <assert.h>
11
12 #ifdef _WIN32
13
14 #include <fcntl.h>
15 #include <io.h>
16
17 /* no eXecute on Win32 */
18 #define X_OK 0
19 #define W_OK 2
20 #define R_OK 4
21
22 #define O_RDWR          _O_RDWR
23 #define O_CREAT         _O_CREAT
24 #define O_EXCL          _O_EXCL
25 #define O_BINARY        _O_BINARY
26
27 /* remap some names, we are not in the POSIX world */
28 #define access(fname, mode)      _access(fname, mode)
29 #define mktemp(tmpl)             _mktemp(tmpl)
30 #define open(fname, oflag, mode) _open(fname, oflag, mode)
31 #define fdopen(fd, mode)         _fdopen(fd, mode)
32 #define popen(cmd, mode)         _popen(cmd, mode)
33 #define pclose(file)             _pclose(file)
34
35 #else
36 #include <unistd.h>
37 #define HAVE_MKSTEMP
38 #endif
39
40 #ifndef WITH_LIBCORE
41 #define WITH_LIBCORE
42 #endif
43
44 #include <libfirm/firm.h>
45 #include <libfirm/be.h>
46
47 #include "lexer.h"
48 #include "token_t.h"
49 #include "types.h"
50 #include "type_hash.h"
51 #include "parser.h"
52 #include "ast2firm.h"
53 #include "diagnostic.h"
54 #include "lang_features.h"
55 #include "driver/firm_opt.h"
56 #include "driver/firm_cmdline.h"
57 #include "adt/error.h"
58 #include "write_fluffy.h"
59 #include "revision.h"
60
61 #ifndef PREPROCESSOR
62 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int"
63 #endif
64
65 #ifndef LINKER
66 #define LINKER    "gcc -m32"
67 #endif
68
69 #ifndef ASSEMBLER
70 #define ASSEMBLER "as --32"
71 #endif
72
73 /** The current c mode/dialect. */
74 unsigned int c_mode = _C89|_C99|_GNUC;
75
76 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
77 unsigned int machine_size = 32;
78
79 /** true if the char type is signed. */
80 bool char_is_signed = true;
81
82 /** true for strict language checking. */
83 bool strict_mode = false;
84
85 static int            verbose;
86 static struct obstack cppflags_obst;
87
88 #if defined(_DEBUG) || defined(FIRM_DEBUG)
89 /**
90  * Debug printf implementation.
91  *
92  * @param fmt  printf style format parameter
93  */
94 void dbg_printf(const char *fmt, ...)
95 {
96         va_list list;
97
98         if (firm_dump.debug_print) {
99                 va_start(list, fmt);
100                 vprintf(fmt, list);
101                 va_end(list);
102         }  /* if */
103 }
104 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
105
106 static void initialize_firm(void)
107 {
108         firm_early_init();
109
110         dump_consts_local(1);
111         dump_keepalive_edges(1);
112 }
113
114 static void get_output_name(char *buf, size_t buflen, const char *inputname,
115                             const char *newext)
116 {
117         size_t last_dot = 0xffffffff;
118         size_t i = 0;
119
120         if(inputname == NULL) {
121                 snprintf(buf, buflen, "a%s", newext);
122                 return;
123         }
124
125         for(const char *c = inputname; *c != 0; ++c) {
126                 if(*c == '.')
127                         last_dot = i;
128                 ++i;
129         }
130         if(last_dot == 0xffffffff)
131                 last_dot = i;
132
133         if(last_dot >= buflen)
134                 panic("filename too long");
135         memcpy(buf, inputname, last_dot);
136
137         size_t extlen = strlen(newext) + 1;
138         if(extlen + last_dot >= buflen)
139                 panic("filename too long");
140         memcpy(buf+last_dot, newext, extlen);
141 }
142
143 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
144 {
145         lexer_open_stream(in, input);
146         translation_unit_t *unit = parse();
147         return unit;
148 }
149
150 static void lextest(FILE *in, const char *fname)
151 {
152         lexer_open_stream(in, fname);
153
154         do {
155                 lexer_next_preprocessing_token();
156                 print_token(stdout, &lexer_token);
157                 puts("");
158         } while(lexer_token.type != T_EOF);
159 }
160
161 static FILE* preprocess(FILE* in, const char *fname)
162 {
163         char buf[4096];
164         const char *flags = obstack_finish(&cppflags_obst);
165
166         if(in != stdin) {
167                 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
168         } else {
169                 /* read from stdin */
170                 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -", flags);
171         }
172
173         if(verbose) {
174                 puts(buf);
175         }
176         FILE* f = popen(buf, "r");
177         if (f == NULL) {
178                 fprintf(stderr, "invoking preprocessor failed\n");
179                 exit(1);
180         }
181         return f;
182 }
183
184 static void do_link(const char *out, const char *in)
185 {
186         char buf[4096];
187
188         snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
189         if(verbose) {
190                 puts(buf);
191         }
192         int err = system(buf);
193         if(err != 0) {
194                 fprintf(stderr, "linker reported an error\n");
195                 exit(1);
196         }
197 }
198
199 static void assemble(const char *out, const char *in)
200 {
201         char buf[4096];
202
203         snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
204         if(verbose) {
205                 puts(buf);
206         }
207
208         int err = system(buf);
209         if(err != 0) {
210                 fprintf(stderr, "assembler reported an error\n");
211                 exit(1);
212         }
213 }
214
215 static const char *try_dir(const char *dir)
216 {
217         if(dir == NULL)
218                 return dir;
219         if(access(dir, R_OK | W_OK | X_OK) == 0)
220                 return dir;
221         return NULL;
222 }
223
224 static const char *get_tempdir(void)
225 {
226         static const char *tmpdir = NULL;
227
228         if(tmpdir != NULL)
229                 return tmpdir;
230
231         if(tmpdir == NULL)
232                 tmpdir = try_dir(getenv("TMPDIR"));
233         if(tmpdir == NULL)
234                 tmpdir = try_dir(getenv("TMP"));
235         if(tmpdir == NULL)
236                 tmpdir = try_dir(getenv("TEMP"));
237
238 #ifdef P_tmpdir
239         if(tmpdir == NULL)
240                 tmpdir = try_dir(P_tmpdir);
241 #endif
242
243         if(tmpdir == NULL)
244                 tmpdir = try_dir("/var/tmp");
245         if(tmpdir == NULL)
246                 tmpdir = try_dir("/usr/tmp");
247         if(tmpdir == NULL)
248                 tmpdir = try_dir("/tmp");
249
250         if(tmpdir == NULL)
251                 tmpdir = ".";
252
253         return tmpdir;
254 }
255
256 #ifndef HAVE_MKSTEMP
257 /* cheap and nasty mkstemp replacement */
258 static int mkstemp(char *templ)
259 {
260         mktemp(templ);
261         return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
262 }
263 #endif
264
265 /**
266  * an own version of tmpnam, which: writes in a buffer, appends a user specified
267  * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
268  */
269 static FILE *make_temp_file(char *buffer, size_t buflen,
270                             const char *prefix, const char *suffix)
271 {
272         const char *tempdir = get_tempdir();
273
274         /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
275         (void) suffix;
276         suffix = "";
277
278         snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
279
280         int fd = mkstemp(buffer);
281         if(fd == -1) {
282                 fprintf(stderr, "couldn't create temporary file: %s\n",
283                         strerror(errno));
284                 exit(1);
285         }
286         FILE *out = fdopen(fd, "w");
287         if(out == NULL) {
288                 fprintf(stderr, "couldn't create temporary file FILE*\n");
289                 exit(1);
290         }
291
292         return out;
293 }
294
295 /**
296  * Do the necessary lowering for compound parameters.
297  */
298 void lower_compound_params(void)
299 {
300         lower_params_t params;
301
302         params.def_ptr_alignment    = 4;
303         params.flags                = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
304         params.hidden_params        = ADD_HIDDEN_ALWAYS_IN_FRONT;
305         params.find_pointer_type    = NULL;
306         params.ret_compound_in_regs = NULL;
307         lower_calls_with_compounds(&params);
308 }
309
310 typedef enum compile_mode_t {
311         ParseOnly,
312         Compile,
313         CompileDump,
314         CompileAssemble,
315         CompileAssembleLink,
316         LexTest,
317         PrintAst,
318         PrintFluffy
319 } compile_mode_t;
320
321 static void usage(const char *argv0)
322 {
323         fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
324 }
325
326 int main(int argc, char **argv)
327 {
328         initialize_firm();
329
330         const char     *input        = NULL;
331         const char     *outname      = NULL;
332         const char     *dumpfunction = NULL;
333         compile_mode_t  mode         = CompileAssembleLink;
334
335         obstack_init(&cppflags_obst);
336
337 #define GET_ARG_AFTER(def, args)                                             \
338         def = &arg[sizeof(args)-1];                                              \
339         if(def[0] == '\0') {                                                     \
340                 ++i;                                                                 \
341                 if(i >= argc) {                                                      \
342                         fprintf(stderr, "error: expected argument after '" args "'\n");  \
343                         argument_errors = true;                                          \
344                         break;                                                           \
345                 }                                                                    \
346                 def = argv[i];                                                       \
347                 if(def[0] == '-' && def[1] != '\0') {                                \
348                         fprintf(stderr, "error: expected argument after '" args "'\n");  \
349                         argument_errors = true;                                          \
350                         continue;                                                        \
351                 }                                                                    \
352         }
353
354         bool help_displayed  = false;
355         bool argument_errors = false;
356         for(int i = 1; i < argc; ++i) {
357                 const char *arg = argv[i];
358                 if(strncmp(arg, "-o", 2) == 0) {
359                         GET_ARG_AFTER(outname, "-o");
360                 } else if(strcmp(arg, "-c") == 0) {
361                         mode = CompileAssemble;
362                 } else if(strcmp(arg, "-S") == 0) {
363                         mode = Compile;
364                 } else if(strcmp(arg, "--gcc") == 0) {
365                         c_mode |= _GNUC;
366                 } else if(strcmp(arg, "--no-gcc") == 0) {
367                         c_mode &= ~_GNUC;
368                 } else if(strcmp(arg, "--ms") == 0) {
369                         c_mode |= _MS;
370                 } else if(strcmp(arg, "--signed-chars") == 0) {
371                         char_is_signed = true;
372                 } else if(strcmp(arg, "--unsigned-chars") == 0) {
373                         char_is_signed = false;
374                 } else if(strcmp(arg, "--strict") == 0) {
375                         strict_mode = true;
376                 } else if(strcmp(arg, "--no-ms") == 0) {
377                         c_mode &= ~_MS;
378                 } else if(strcmp(arg, "--lextest") == 0) {
379                         mode = LexTest;
380                 } else if(strcmp(arg, "--print-ast") == 0) {
381                         mode = PrintAst;
382                 } else if(strcmp(arg, "--print-fluffy") == 0) {
383                         mode = PrintFluffy;
384                 } else if(strcmp(arg, "--version") == 0) {
385                         firm_version_t ver;
386                         firm_get_version(&ver);
387                         printf("cparser (%d.%d %s) using libFirm (%u.%u", 0, 1, cparser_REVISION, ver.major, ver.minor);
388                         if(ver.revision[0] != 0) {
389                                 putchar(' ');
390                                 fputs(ver.revision, stdout);
391                         }
392                         if(ver.build[0] != 0) {
393                                 putchar(' ');
394                                 fputs(ver.build, stdout);
395                         }
396                         puts(")\n");
397                         exit(EXIT_SUCCESS);
398                 } else if(strcmp(arg, "-fsyntax-only") == 0) {
399                         mode = ParseOnly;
400                 } else if(strncmp(arg, "-I", 2) == 0) {
401                         const char *opt;
402                         GET_ARG_AFTER(opt, "-I");
403                         obstack_printf(&cppflags_obst, " -I%s", opt);
404                 } else if(strncmp(arg, "-D", 2) == 0) {
405                         const char *opt;
406                         GET_ARG_AFTER(opt, "-D");
407                         obstack_printf(&cppflags_obst, " -D%s", opt);
408                 } else if(strncmp(arg, "-U", 2) == 0) {
409                         const char *opt;
410                         GET_ARG_AFTER(opt, "-U");
411                         obstack_printf(&cppflags_obst, " -U%s", opt);
412                 } else if(strcmp(arg, "--dump-function") == 0) {
413                         ++i;
414                         if(i >= argc) {
415                                 fprintf(stderr, "error: "
416                                         "expected argument after '--dump-function'\n");
417                                 argument_errors = true;
418                                 break;
419                         }
420                         dumpfunction = argv[i];
421                         mode         = CompileDump;
422                 } else if(strcmp(arg, "-v") == 0) {
423                         verbose = 1;
424                 } else if(arg[0] == '-' && arg[1] == 'f') {
425                         const char *opt;
426                         GET_ARG_AFTER(opt, "-f");
427
428                         if(strcmp(opt, "omit-frame-pointer") == 0) {
429                                 firm_be_option("omitfp");
430                         } else if(strcmp(opt, "no-omit-frame-pointer") == 0) {
431                                 firm_be_option("omitfp=no");
432                         } else {
433                                 int res = firm_option(opt);
434                                 if (res == 0) {
435                                         fprintf(stderr, "error: unknown Firm option '-f %s'\n",
436                                                 opt);
437                                         argument_errors = true;
438                                         continue;
439                                 } else if (res == -1) {
440                                         help_displayed = true;
441                                 }
442                         }
443                 } else if(arg[0] == '-' && arg[1] == 'b') {
444                         const char *opt;
445                         GET_ARG_AFTER(opt, "-b");
446                         int res = firm_be_option(opt);
447                         if (res == 0) {
448                                 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
449                                         opt);
450                                 argument_errors = true;
451                         } else if (res == -1) {
452                                 help_displayed = true;
453                         }
454                 } else if(arg[0] == '-' && arg[1] == 'W') {
455                         const char *opt;
456                         GET_ARG_AFTER(opt, "-W");
457                         if (strcmp(opt, "error") == 0) {
458                                 warnings_are_errors = true;
459                         } else {
460                                 fprintf(stderr, "warning: ignoring gcc option -W%s\n", opt);
461                         }
462                 } else if(arg[0] == '-' && arg[1] == 'm') {
463                         const char *opt;
464                         GET_ARG_AFTER(opt, "-m");
465                         char *endptr;
466                         long int value = strtol(opt, &endptr, 10);
467                         if (*endptr != '\0') {
468                                 fprintf(stderr, "error: wrong option '-m %s'\n",  opt);
469                                 argument_errors = true;
470                         }
471                         if (value != 16 && value != 32 && value != 64) {
472                                 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
473                                 argument_errors = true;
474                         } else {
475                                 machine_size = (unsigned int)value;
476                         }
477                 } else if(arg[0] == '-') {
478                         if (arg[1] == '\0') {
479                                 if(input != NULL) {
480                                         fprintf(stderr, "error: multiple input files specified\n");
481                                         argument_errors = true;
482                                 } else {
483                                         input = arg;
484                                 }
485                         } else if(strcmp(arg, "-pedantic") == 0) {
486                                 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
487                         } else if(arg[1] == 'O' ||
488                                         arg[1] == 'g' ||
489                                         strncmp(arg + 1, "std=", 4) == 0) {
490                                 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
491                         } else {
492                                 fprintf(stderr, "error: unknown argument '%s'\n", arg);
493                                 argument_errors = true;
494                         }
495                 } else {
496                         if(input != NULL) {
497                                 fprintf(stderr, "error: multiple input files specified\n");
498                                 argument_errors = true;
499                         } else {
500                                 input = arg;
501                         }
502                 }
503         }
504
505         /* we do the lowering in ast2firm */
506         firm_opt.lower_bitfields = FALSE;
507
508         if(help_displayed) {
509                 return !argument_errors;
510         }
511         if(argument_errors) {
512                 usage(argv[0]);
513                 return 1;
514         }
515
516         gen_firm_init();
517         init_symbol_table();
518         init_tokens();
519         init_types();
520         init_typehash();
521         init_basic_types();
522         init_lexer();
523         init_ast();
524         init_parser();
525         init_ast2firm();
526
527         FILE *out = NULL;
528         char  outnamebuf[4096];
529         if(outname == NULL) {
530                 switch(mode) {
531                 case PrintAst:
532                 case PrintFluffy:
533                 case LexTest:
534                         if(outname == NULL)
535                                 outname = "-";
536                         break;
537                 case ParseOnly:
538                         break;
539                 case Compile:
540                         get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
541                         outname = outnamebuf;
542                         break;
543                 case CompileAssemble:
544                         get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
545                         outname = outnamebuf;
546                         break;
547                 case CompileDump:
548                         get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
549                                         ".vcg");
550                         outname = outnamebuf;
551                         break;
552                 case CompileAssembleLink:
553                         outname = "a.out";
554                         break;
555                 }
556         }
557
558         if(outname != NULL) {
559                 if(strcmp(outname, "-") == 0) {
560                         out = stdout;
561                 } else {
562                         out = fopen(outname, "w");
563                         if(out == NULL) {
564                                 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
565                                         strerror(errno));
566                                 return 1;
567                         }
568                 }
569         }
570
571         FILE *in;
572         if(input == NULL) {
573                 fprintf(stderr, "%s: no input files\n", argv[0]);
574                 return 1;
575         } else if(strcmp(input, "-") == 0) {
576                 in    = stdin;
577                 input = "<stdin>";
578         } else {
579                 in = fopen(input, "r");
580                 if(in == NULL) {
581                         fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
582                         return 1;
583                 }
584         }
585
586         if(mode == LexTest) {
587                 lextest(in, input);
588                 fclose(in);
589                 return 0;
590         }
591
592         FILE *preprocessed_in = preprocess(in, input);
593         translation_unit_t *const unit = do_parsing(preprocessed_in, input);
594         pclose(preprocessed_in);
595         if(unit == NULL) {
596                 /* parsing failed because of errors */
597                 fprintf(stderr, "%u error(s), %u warning(s)\n", error_count, warning_count);
598                 return EXIT_FAILURE;
599         }
600         if (warning_count > 0) {
601                 fprintf(stderr, "%u warning(s)\n", warning_count);
602         }
603
604         if(mode == PrintAst) {
605                 type_set_output(out);
606                 ast_set_output(out);
607                 print_ast(unit);
608                 return 0;
609         }
610         if(mode == PrintFluffy) {
611                 type_set_output(out);
612                 ast_set_output(out);
613                 write_fluffy_decls(out, unit);
614         }
615
616         translation_unit_to_firm(unit);
617
618         if(mode == ParseOnly) {
619                 return 0;
620         }
621
622         FILE *asm_out;
623         char  asm_tempfile[1024];
624         if(mode == CompileDump) {
625                 asm_out = NULL;
626                 firm_be_opt.selection = BE_NONE;
627         } else if(mode == Compile) {
628                 asm_out = out;
629         } else {
630                 asm_out
631                         = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
632         }
633         gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
634
635         if(mode == CompileDump) {
636                 /* find irg */
637                 ident    *id     = new_id_from_str(dumpfunction);
638                 ir_graph *irg    = NULL;
639                 int       n_irgs = get_irp_n_irgs();
640                 for(int i = 0; i < n_irgs; ++i) {
641                         ir_graph *tirg   = get_irp_irg(i);
642                         ident    *irg_id = get_entity_ident(get_irg_entity(tirg));
643                         if(irg_id == id) {
644                                 irg = tirg;
645                                 break;
646                         }
647                 }
648
649                 if(irg == NULL) {
650                         fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
651                         return 1;
652                 }
653
654                 dump_ir_block_graph_file(irg, out);
655                 fclose(out);
656                 return 0;
657         }
658
659         fclose(asm_out);
660
661         /* assemble assembler and create object file */
662         char obj_tfile[1024];
663         if(mode == CompileAssemble || mode == CompileAssembleLink) {
664                 const char *obj_outfile;
665                 if(mode == CompileAssemble) {
666                         fclose(out);
667                         obj_outfile = outname;
668                 } else {
669                         FILE *tempf
670                                 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
671                         fclose(tempf);
672                         obj_outfile = obj_tfile;
673                 }
674
675                 assemble(obj_outfile, asm_tempfile);
676         }
677
678         /* link object file */
679         if(mode == CompileAssembleLink) {
680                 do_link(outname, obj_tfile);
681         }
682
683         obstack_free(&cppflags_obst, NULL);
684
685         exit_ast2firm();
686         exit_parser();
687         exit_ast();
688         exit_lexer();
689         exit_typehash();
690         exit_types();
691         exit_tokens();
692         exit_symbol_table();
693         return 0;
694 }