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_cmdline.h"
54 #include "adt/error.h"
55 #include "write_fluffy.h"
56 #include "driver/firm_opt.h"
59 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int"
63 #define LINKER "gcc -m32"
67 #define ASSEMBLER "as --32"
70 /** The current c mode/dialect. */
71 unsigned int c_mode = _C99|_GNUC;
73 /** The 'machine size', 16, 32 or 64 bit, 32bit is the default. */
74 unsigned int machine_size = 32;
76 /** true if the char type is signed. */
77 bool char_is_signed = true;
80 static struct obstack cppflags_obst;
82 #if defined(_DEBUG) || defined(FIRM_DEBUG)
84 * Debug printf implementation.
86 * @param fmt printf style format parameter
88 void dbg_printf(const char *fmt, ...)
92 if (firm_dump.debug_print) {
98 #endif /* defined(_DEBUG) || defined(FIRM_DEBUG) */
100 static void initialize_firm(void)
104 dump_consts_local(1);
105 dump_keepalive_edges(1);
108 static void get_output_name(char *buf, size_t buflen, const char *inputname,
111 size_t last_dot = 0xffffffff;
114 if(inputname == NULL) {
115 snprintf(buf, buflen, "a%s", newext);
119 for(const char *c = inputname; *c != 0; ++c) {
124 if(last_dot == 0xffffffff)
127 if(last_dot >= buflen)
128 panic("filename too long");
129 memcpy(buf, inputname, last_dot);
131 size_t extlen = strlen(newext) + 1;
132 if(extlen + last_dot >= buflen)
133 panic("filename too long");
134 memcpy(buf+last_dot, newext, extlen);
137 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
139 lexer_open_stream(in, input);
140 translation_unit_t *unit = parse();
144 static void lextest(FILE *in, const char *fname)
146 lexer_open_stream(in, fname);
149 lexer_next_preprocessing_token();
150 print_token(stdout, &lexer_token);
152 } while(lexer_token.type != T_EOF);
155 static FILE* preprocess(FILE* in, const char *fname)
158 const char *flags = obstack_finish(&cppflags_obst);
161 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
163 /* read from stdin */
164 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -", flags);
170 FILE* f = popen(buf, "r");
172 fprintf(stderr, "invoking preprocessor failed\n");
178 static void do_link(const char *out, const char *in)
182 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
186 int err = system(buf);
188 fprintf(stderr, "linker reported an error\n");
193 static void assemble(const char *out, const char *in)
197 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
202 int err = system(buf);
204 fprintf(stderr, "assembler reported an error\n");
209 static const char *try_dir(const char *dir)
213 if(access(dir, R_OK | W_OK | X_OK) == 0)
218 static const char *get_tempdir(void)
220 static const char *tmpdir = NULL;
226 tmpdir = try_dir(getenv("TMPDIR"));
228 tmpdir = try_dir(getenv("TMP"));
230 tmpdir = try_dir(getenv("TEMP"));
234 tmpdir = try_dir(P_tmpdir);
238 tmpdir = try_dir("/var/tmp");
240 tmpdir = try_dir("/usr/tmp");
242 tmpdir = try_dir("/tmp");
251 /* cheap and nasty mkstemp replacement */
252 static int mkstemp(char *templ)
255 return open(templ, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
260 * an own version of tmpnam, which: writes in a buffer, appends a user specified
261 * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
263 static FILE *make_temp_file(char *buffer, size_t buflen,
264 const char *prefix, const char *suffix)
266 const char *tempdir = get_tempdir();
268 /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
272 snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
274 int fd = mkstemp(buffer);
276 fprintf(stderr, "couldn't create temporary file: %s\n",
280 FILE *out = fdopen(fd, "w");
282 fprintf(stderr, "couldn't create temporary file FILE*\n");
290 * Do the necessary lowering for compound parameters.
292 void lower_compound_params(void)
294 lower_params_t params;
296 params.def_ptr_alignment = 4;
297 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
298 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
299 params.find_pointer_type = NULL;
300 params.ret_compound_in_regs = NULL;
301 lower_calls_with_compounds(¶ms);
304 typedef enum compile_mode_t {
315 static void usage(const char *argv0)
317 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
320 int main(int argc, char **argv)
324 const char *input = NULL;
325 const char *outname = NULL;
326 const char *dumpfunction = NULL;
327 compile_mode_t mode = CompileAssembleLink;
329 obstack_init(&cppflags_obst);
331 #define GET_ARG_AFTER(def, args) \
332 def = &arg[sizeof(args)-1]; \
333 if(def[0] == '\0') { \
336 fprintf(stderr, "error: expected argument after '" args "'\n"); \
337 argument_errors = true; \
341 if(def[0] == '-' && def[1] != '\0') { \
342 fprintf(stderr, "error: expected argument after '" args "'\n"); \
343 argument_errors = true; \
348 bool help_displayed = false;
349 bool argument_errors = false;
350 for(int i = 1; i < argc; ++i) {
351 const char *arg = argv[i];
352 if(strncmp(arg, "-o", 2) == 0) {
353 GET_ARG_AFTER(outname, "-o");
354 } else if(strcmp(arg, "-c") == 0) {
355 mode = CompileAssemble;
356 } else if(strcmp(arg, "-S") == 0) {
358 } else if(strcmp(arg, "--gcc") == 0) {
360 } else if(strcmp(arg, "--no-gcc") == 0) {
362 } else if(strcmp(arg, "--ms") == 0) {
364 } else if(strcmp(arg, "--signed-chars") == 0) {
365 char_is_signed = true;
366 } else if(strcmp(arg, "--unsigned-chars") == 0) {
367 char_is_signed = false;
368 } else if(strcmp(arg, "--no-ms") == 0) {
370 } else if(strcmp(arg, "--lextest") == 0) {
372 } else if(strcmp(arg, "--print-ast") == 0) {
374 } else if(strcmp(arg, "--print-fluffy") == 0) {
376 } else if(strcmp(arg, "-fsyntax-only") == 0) {
378 } else if(strncmp(arg, "-I", 2) == 0) {
380 GET_ARG_AFTER(opt, "-I");
381 obstack_printf(&cppflags_obst, " -I%s", opt);
382 } else if(strncmp(arg, "-D", 2) == 0) {
384 GET_ARG_AFTER(opt, "-D");
385 obstack_printf(&cppflags_obst, " -D%s", opt);
386 } else if(strcmp(arg, "--dump-function") == 0) {
389 fprintf(stderr, "error: "
390 "expected argument after '--dump-function'\n");
391 argument_errors = true;
394 dumpfunction = argv[i];
396 } else if(strcmp(arg, "-v") == 0) {
398 } else if(arg[0] == '-' && arg[1] == 'f') {
400 GET_ARG_AFTER(opt, "-f");
401 int res = firm_option(opt);
403 fprintf(stderr, "error: unknown Firm option '-f %s'\n", opt);
404 argument_errors = true;
406 } else if (res == -1) {
407 help_displayed = true;
409 } else if(arg[0] == '-' && arg[1] == 'b') {
411 GET_ARG_AFTER(opt, "-b");
412 int res = firm_be_option(opt);
414 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
416 argument_errors = true;
417 } else if (res == -1) {
418 help_displayed = true;
420 } else if(arg[0] == '-' && arg[1] == 'm') {
422 GET_ARG_AFTER(opt, "-m");
424 long int value = strtol(opt, &endptr, 10);
425 if (*endptr != '\0') {
426 fprintf(stderr, "error: wrong option '-m %s'\n", opt);
427 argument_errors = true;
429 if (value != 16 && value != 32 && value != 64) {
430 fprintf(stderr, "error: option -m supports only 16, 32 or 64\n");
431 argument_errors = true;
433 machine_size = (unsigned int)value;
434 } else if(arg[0] == '-') {
435 if (arg[1] == '\0') {
437 fprintf(stderr, "error: multiple input files specified\n");
438 argument_errors = true;
442 } else if(strcmp(arg, "-pedantic") == 0) {
443 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
444 } else if(arg[1] == 'O' ||
448 strncmp(arg + 1, "std=", 4) == 0) {
449 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
451 fprintf(stderr, "error: unknown argument '%s'\n", arg);
452 argument_errors = true;
456 fprintf(stderr, "error: multiple input files specified\n");
457 argument_errors = true;
465 return !argument_errors;
467 if(argument_errors) {
483 char outnamebuf[4096];
484 if(outname == NULL) {
495 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
496 outname = outnamebuf;
498 case CompileAssemble:
499 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
500 outname = outnamebuf;
503 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
505 outname = outnamebuf;
507 case CompileAssembleLink:
513 if(outname != NULL) {
514 if(strcmp(outname, "-") == 0) {
517 out = fopen(outname, "w");
519 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
528 fprintf(stderr, "%s: no input files\n", argv[0]);
530 } else if(strcmp(input, "-") == 0) {
534 in = fopen(input, "r");
536 fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
541 if(mode == LexTest) {
547 FILE *preprocessed_in = preprocess(in, input);
548 translation_unit_t *const unit = do_parsing(preprocessed_in, input);
549 pclose(preprocessed_in);
553 if(mode == PrintAst) {
554 type_set_output(out);
559 if(mode == PrintFluffy) {
560 type_set_output(out);
562 write_fluffy_decls(out, unit);
565 translation_unit_to_firm(unit);
567 if(mode == ParseOnly) {
572 char asm_tempfile[1024];
573 if(mode == CompileDump) {
575 firm_be_opt.selection = BE_NONE;
576 } else if(mode == Compile) {
580 = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
582 gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
584 if(mode == CompileDump) {
586 ident *id = new_id_from_str(dumpfunction);
587 ir_graph *irg = NULL;
588 int n_irgs = get_irp_n_irgs();
589 for(int i = 0; i < n_irgs; ++i) {
590 ir_graph *tirg = get_irp_irg(i);
591 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
599 fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
603 dump_ir_block_graph_file(irg, out);
610 /* assemble assembler and create object file */
611 char obj_tfile[1024];
612 if(mode == CompileAssemble || mode == CompileAssembleLink) {
613 const char *obj_outfile;
614 if(mode == CompileAssemble) {
616 obj_outfile = outname;
619 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
621 obj_outfile = obj_tfile;
624 assemble(obj_outfile, asm_tempfile);
627 /* link object file */
628 if(mode == CompileAssembleLink) {
629 do_link(outname, obj_tfile);
632 obstack_free(&cppflags_obst, NULL);