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