17 /* no eXecute on Win32 */
22 #define O_RDWR _O_RDWR
23 #define O_CREAT _O_CREAT
24 #define O_EXCL _O_EXCL
25 #define O_BINARY _O_BINARY
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)
44 #include <libfirm/firm.h>
45 #include <libfirm/be.h>
49 #include "type_hash.h"
52 #include "lang_features.h"
53 #include "driver/firm_opt.h"
54 #include "driver/firm_cmdline.h"
55 #include "adt/error.h"
56 #include "write_fluffy.h"
60 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int"
64 #define LINKER "gcc -m32"
68 #define ASSEMBLER "as --32"
71 /** The current c mode/dialect. */
72 unsigned int c_mode = _C99|_GNUC;
74 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
75 unsigned int machine_size = 32;
77 /** true if the char type is signed. */
78 bool char_is_signed = true;
81 static struct obstack cppflags_obst;
83 #if defined(_DEBUG) || defined(FIRM_DEBUG)
85 * Debug printf implementation.
87 * @param fmt printf style format parameter
89 void dbg_printf(const char *fmt, ...)
93 if (firm_dump.debug_print) {
99 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
101 static void initialize_firm(void)
105 dump_consts_local(1);
106 dump_keepalive_edges(1);
109 static void get_output_name(char *buf, size_t buflen, const char *inputname,
112 size_t last_dot = 0xffffffff;
115 if(inputname == NULL) {
116 snprintf(buf, buflen, "a%s", newext);
120 for(const char *c = inputname; *c != 0; ++c) {
125 if(last_dot == 0xffffffff)
128 if(last_dot >= buflen)
129 panic("filename too long");
130 memcpy(buf, inputname, last_dot);
132 size_t extlen = strlen(newext) + 1;
133 if(extlen + last_dot >= buflen)
134 panic("filename too long");
135 memcpy(buf+last_dot, newext, extlen);
138 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
140 lexer_open_stream(in, input);
141 translation_unit_t *unit = parse();
145 static void lextest(FILE *in, const char *fname)
147 lexer_open_stream(in, fname);
150 lexer_next_preprocessing_token();
151 print_token(stdout, &lexer_token);
153 } while(lexer_token.type != T_EOF);
156 static FILE* preprocess(FILE* in, const char *fname)
159 const char *flags = obstack_finish(&cppflags_obst);
162 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
164 /* read from stdin */
165 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -", flags);
171 FILE* f = popen(buf, "r");
173 fprintf(stderr, "invoking preprocessor failed\n");
179 static void do_link(const char *out, const char *in)
183 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
187 int err = system(buf);
189 fprintf(stderr, "linker reported an error\n");
194 static void assemble(const char *out, const char *in)
198 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
203 int err = system(buf);
205 fprintf(stderr, "assembler reported an error\n");
210 static const char *try_dir(const char *dir)
214 if(access(dir, R_OK | W_OK | X_OK) == 0)
219 static const char *get_tempdir(void)
221 static const char *tmpdir = NULL;
227 tmpdir = try_dir(getenv("TMPDIR"));
229 tmpdir = try_dir(getenv("TMP"));
231 tmpdir = try_dir(getenv("TEMP"));
235 tmpdir = try_dir(P_tmpdir);
239 tmpdir = try_dir("/var/tmp");
241 tmpdir = try_dir("/usr/tmp");
243 tmpdir = try_dir("/tmp");
252 /* cheap and nasty mkstemp replacement */
253 static int mkstemp(char *templ)
256 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
261 * an own version of tmpnam, which: writes in a buffer, appends a user specified
262 * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
264 static FILE *make_temp_file(char *buffer, size_t buflen,
265 const char *prefix, const char *suffix)
267 const char *tempdir = get_tempdir();
269 /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
273 snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
275 int fd = mkstemp(buffer);
277 fprintf(stderr, "couldn't create temporary file: %s\n",
281 FILE *out = fdopen(fd, "w");
283 fprintf(stderr, "couldn't create temporary file FILE*\n");
291 * Do the necessary lowering for compound parameters.
293 void lower_compound_params(void)
295 lower_params_t params;
297 params.def_ptr_alignment = 4;
298 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
299 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
300 params.find_pointer_type = NULL;
301 params.ret_compound_in_regs = NULL;
302 lower_calls_with_compounds(¶ms);
305 typedef enum compile_mode_t {
316 static void usage(const char *argv0)
318 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
321 int main(int argc, char **argv)
325 const char *input = NULL;
326 const char *outname = NULL;
327 const char *dumpfunction = NULL;
328 compile_mode_t mode = CompileAssembleLink;
330 obstack_init(&cppflags_obst);
332 #define GET_ARG_AFTER(def, args) \
333 def = &arg[sizeof(args)-1]; \
334 if(def[0] == '\0') { \
337 fprintf(stderr, "error: expected argument after '" args "'\n"); \
338 argument_errors = true; \
342 if(def[0] == '-' && def[1] != '\0') { \
343 fprintf(stderr, "error: expected argument after '" args "'\n"); \
344 argument_errors = true; \
349 bool help_displayed = false;
350 bool argument_errors = false;
351 for(int i = 1; i < argc; ++i) {
352 const char *arg = argv[i];
353 if(strncmp(arg, "-o", 2) == 0) {
354 GET_ARG_AFTER(outname, "-o");
355 } else if(strcmp(arg, "-c") == 0) {
356 mode = CompileAssemble;
357 } else if(strcmp(arg, "-S") == 0) {
359 } else if(strcmp(arg, "--gcc") == 0) {
361 } else if(strcmp(arg, "--no-gcc") == 0) {
363 } else if(strcmp(arg, "--ms") == 0) {
365 } else if(strcmp(arg, "--signed-chars") == 0) {
366 char_is_signed = true;
367 } else if(strcmp(arg, "--unsigned-chars") == 0) {
368 char_is_signed = false;
369 } else if(strcmp(arg, "--no-ms") == 0) {
371 } else if(strcmp(arg, "--lextest") == 0) {
373 } else if(strcmp(arg, "--print-ast") == 0) {
375 } else if(strcmp(arg, "--print-fluffy") == 0) {
377 } else if(strcmp(arg, "--version") == 0) {
379 firm_get_version(&ver);
380 printf("cparser (%d.%d %s) using libFirm (%d.%d", 0, 1, cparser_REVISION, ver.major, ver.minor);
381 if(ver.revision[0] != 0) {
383 fputs(ver.revision, stdout);
385 if(ver.build[0] != 0) {
387 fputs(ver.build, stdout);
391 } else if(strcmp(arg, "-fsyntax-only") == 0) {
393 } else if(strncmp(arg, "-I", 2) == 0) {
395 GET_ARG_AFTER(opt, "-I");
396 obstack_printf(&cppflags_obst, " -I%s", opt);
397 } else if(strncmp(arg, "-D", 2) == 0) {
399 GET_ARG_AFTER(opt, "-D");
400 obstack_printf(&cppflags_obst, " -D%s", opt);
401 } else if(strcmp(arg, "--dump-function") == 0) {
404 fprintf(stderr, "error: "
405 "expected argument after '--dump-function'\n");
406 argument_errors = true;
409 dumpfunction = argv[i];
411 } else if(strcmp(arg, "-v") == 0) {
413 } else if(arg[0] == '-' && arg[1] == 'f') {
415 GET_ARG_AFTER(opt, "-f");
416 int res = firm_option(opt);
418 fprintf(stderr, "error: unknown Firm option '-f %s'\n", opt);
419 argument_errors = true;
421 } else if (res == -1) {
422 help_displayed = true;
424 } else if(arg[0] == '-' && arg[1] == 'b') {
426 GET_ARG_AFTER(opt, "-b");
427 int res = firm_be_option(opt);
429 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
431 argument_errors = true;
432 } else if (res == -1) {
433 help_displayed = true;
435 } else if(arg[0] == '-' && arg[1] == 'm') {
437 GET_ARG_AFTER(opt, "-m");
439 long int value = strtol(opt, &endptr, 10);
440 if (*endptr != '\0') {
441 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
442 argument_errors = true;
444 if (value != 16 && value != 32 && value != 64) {
445 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
446 argument_errors = true;
448 machine_size = (unsigned int)value;
449 } else if(arg[0] == '-') {
450 if (arg[1] == '\0') {
452 fprintf(stderr, "error: multiple input files specified\n");
453 argument_errors = true;
457 } else if(strcmp(arg, "-pedantic") == 0) {
458 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
459 } else if(arg[1] == 'O' ||
463 strncmp(arg + 1, "std=", 4) == 0) {
464 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
466 fprintf(stderr, "error: unknown argument '%s'\n", arg);
467 argument_errors = true;
471 fprintf(stderr, "error: multiple input files specified\n");
472 argument_errors = true;
480 return !argument_errors;
482 if(argument_errors) {
498 char outnamebuf[4096];
499 if(outname == NULL) {
510 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
511 outname = outnamebuf;
513 case CompileAssemble:
514 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
515 outname = outnamebuf;
518 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
520 outname = outnamebuf;
522 case CompileAssembleLink:
528 if(outname != NULL) {
529 if(strcmp(outname, "-") == 0) {
532 out = fopen(outname, "w");
534 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
543 fprintf(stderr, "%s: no input files\n", argv[0]);
545 } else if(strcmp(input, "-") == 0) {
549 in = fopen(input, "r");
551 fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
556 if(mode == LexTest) {
562 FILE *preprocessed_in = preprocess(in, input);
563 translation_unit_t *const unit = do_parsing(preprocessed_in, input);
564 pclose(preprocessed_in);
568 if(mode == PrintAst) {
569 type_set_output(out);
574 if(mode == PrintFluffy) {
575 type_set_output(out);
577 write_fluffy_decls(out, unit);
580 translation_unit_to_firm(unit);
582 if(mode == ParseOnly) {
587 char asm_tempfile[1024];
588 if(mode == CompileDump) {
590 firm_be_opt.selection = BE_NONE;
591 } else if(mode == Compile) {
595 = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
597 gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
599 if(mode == CompileDump) {
601 ident *id = new_id_from_str(dumpfunction);
602 ir_graph *irg = NULL;
603 int n_irgs = get_irp_n_irgs();
604 for(int i = 0; i < n_irgs; ++i) {
605 ir_graph *tirg = get_irp_irg(i);
606 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
614 fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
618 dump_ir_block_graph_file(irg, out);
625 /* assemble assembler and create object file */
626 char obj_tfile[1024];
627 if(mode == CompileAssemble || mode == CompileAssembleLink) {
628 const char *obj_outfile;
629 if(mode == CompileAssemble) {
631 obj_outfile = outname;
634 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
636 obj_outfile = obj_tfile;
639 assemble(obj_outfile, asm_tempfile);
642 /* link object file */
643 if(mode == CompileAssembleLink) {
644 do_link(outname, obj_tfile);
647 obstack_free(&cppflags_obst, NULL);