17 #include <libfirm/firm.h>
18 #include <libfirm/be.h>
22 #include "type_hash.h"
25 #include "lang_features.h"
26 #include "driver/firm_cmdline.h"
27 #include "adt/error.h"
28 #include "write_fluffy.h"
29 #include "driver/firm_opt.h"
32 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int"
36 #define LINKER "gcc -m32"
40 #define ASSEMBLER "as --32"
44 /* remap some names */
45 #define popen(cmd, mode) _popen(cmd, mode)
46 #define pclose(file) _pclose(file)
49 /** The current c mode/dialect */
50 unsigned int c_mode = _C99|_GNUC;
54 static struct obstack cppflags_obst;
56 static void initialize_firm(void)
61 dump_keepalive_edges(1);
64 static void dump(ir_graph *irg, const char *suffix)
67 dump_ir_block_graph(irg, suffix);
71 static void get_output_name(char *buf, size_t buflen, const char *inputname,
74 size_t last_dot = 0xffffffff;
77 if(inputname == NULL) {
78 snprintf(buf, buflen, "a%s", newext);
82 for(const char *c = inputname; *c != 0; ++c) {
87 if(last_dot == 0xffffffff)
90 if(last_dot >= buflen)
91 panic("filename too long");
92 memcpy(buf, inputname, last_dot);
94 size_t extlen = strlen(newext) + 1;
95 if(extlen + last_dot >= buflen)
96 panic("filename too long");
97 memcpy(buf+last_dot, newext, extlen);
100 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
102 lexer_open_stream(in, input);
103 translation_unit_t *unit = parse();
107 static void lextest(FILE *in, const char *fname)
109 lexer_open_stream(in, fname);
112 lexer_next_preprocessing_token();
113 print_token(stdout, &lexer_token);
115 } while(lexer_token.type != T_EOF);
118 static FILE* preprocess(FILE* in, const char *fname)
121 const char *flags = obstack_finish(&cppflags_obst);
124 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
126 /* read from stdin */
127 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -", flags);
133 FILE* f = popen(buf, "r");
135 fprintf(stderr, "invoking preprocessor failed\n");
141 static void do_link(const char *out, const char *in)
145 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
149 int err = system(buf);
151 fprintf(stderr, "linker reported an error\n");
156 static void assemble(const char *out, const char *in)
160 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
165 int err = system(buf);
167 fprintf(stderr, "assembler reported an error\n");
172 static const char *try_dir(const char *dir)
176 if(access(dir, R_OK | W_OK | X_OK) == 0)
181 static const char *get_tempdir(void)
183 static const char *tmpdir = NULL;
189 tmpdir = try_dir(getenv("TMPDIR"));
191 tmpdir = try_dir(getenv("TMP"));
193 tmpdir = try_dir(getenv("TEMP"));
197 tmpdir = try_dir(P_tmpdir);
201 tmpdir = try_dir("/var/tmp");
203 tmpdir = try_dir("/usr/tmp");
205 tmpdir = try_dir("/tmp");
214 * an own version of tmpnam, which: writes in a buffer, appends a user specified
215 * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
217 static FILE *make_temp_file(char *buffer, size_t buflen,
218 const char *prefix, const char *suffix)
220 const char *tempdir = get_tempdir();
222 /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
226 snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
228 int fd = mkstemp(buffer);
230 fprintf(stderr, "couldn't create temporary file: %s\n",
234 FILE *out = fdopen(fd, "w");
236 fprintf(stderr, "couldn't create temporary file FILE*\n");
244 * Do the necessary lowering for compound parameters.
246 void lower_compound_params(void)
248 lower_params_t params;
250 params.def_ptr_alignment = 4;
251 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
252 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
253 params.find_pointer_type = NULL;
254 params.ret_compound_in_regs = NULL;
255 lower_calls_with_compounds(¶ms);
258 static void create_firm_prog(translation_unit_t *unit)
260 translation_unit_to_firm(unit);
262 int n_irgs = get_irp_n_irgs();
263 for(int i = 0; i < n_irgs; ++i) {
264 ir_graph *const irg = get_irp_irg(i);
268 lower_compound_params();
271 for(int i = 0; i < n_irgs; ++i) {
272 ir_graph *const irg = get_irp_irg(i);
277 typedef enum compile_mode_t {
288 static void usage(const char *argv0)
290 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
293 int main(int argc, char **argv)
297 const char *input = NULL;
298 const char *outname = NULL;
299 const char *dumpfunction = NULL;
300 compile_mode_t mode = CompileAssembleLink;
302 obstack_init(&cppflags_obst);
304 #define GET_ARG_AFTER(def, args) \
305 def = &arg[sizeof(args)-1]; \
306 if(def[0] == '\0') { \
309 fprintf(stderr, "error: expected argument after '" args "'\n"); \
310 argument_errors = true; \
314 if(def[0] == '-' && def[1] != '\0') { \
315 fprintf(stderr, "error: expected argument after '" args "'\n"); \
316 argument_errors = true; \
321 bool help_displayed = false;
322 bool argument_errors = false;
323 for(int i = 1; i < argc; ++i) {
324 const char *arg = argv[i];
325 if(strncmp(arg, "-o", 2) == 0) {
326 GET_ARG_AFTER(outname, "-o");
327 } else if(strcmp(arg, "-c") == 0) {
328 mode = CompileAssemble;
329 } else if(strcmp(arg, "-S") == 0) {
331 } else if(strcmp(arg, "--gcc") == 0) {
333 } else if(strcmp(arg, "--no-gcc") == 0) {
335 } else if(strcmp(arg, "--ms") == 0) {
337 } else if(strcmp(arg, "--no-ms") == 0) {
339 } else if(strcmp(arg, "--lextest") == 0) {
341 } else if(strcmp(arg, "--print-ast") == 0) {
343 } else if(strcmp(arg, "--print-fluffy") == 0) {
345 } else if(strcmp(arg, "-fsyntax-only") == 0) {
347 } else if(strncmp(arg, "-I", 2) == 0) {
349 GET_ARG_AFTER(opt, "-I");
350 obstack_printf(&cppflags_obst, " -I%s", opt);
351 } else if(strncmp(arg, "-D", 2) == 0) {
353 GET_ARG_AFTER(opt, "-D");
354 obstack_printf(&cppflags_obst, " -D%s", opt);
355 } else if(strcmp(arg, "--dump") == 0) {
357 } else if(strcmp(arg, "--dump-function") == 0) {
360 fprintf(stderr, "error: "
361 "expected argument after '--dump-function'\n");
362 argument_errors = true;
365 dumpfunction = argv[i];
367 } else if(strcmp(arg, "-v") == 0) {
369 } else if(arg[0] == '-' && arg[1] == 'f') {
371 GET_ARG_AFTER(opt, "-f");
372 int res = firm_option(opt);
374 fprintf(stderr, "error: unknown Firm option '-f %s'\n", opt);
375 argument_errors = true;
377 } else if (res == -1) {
378 help_displayed = true;
380 } else if(arg[0] == '-' && arg[1] == 'b') {
382 GET_ARG_AFTER(opt, "-b");
383 int res = firm_be_option(opt);
385 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
387 argument_errors = true;
388 } else if (res == -1) {
389 help_displayed = true;
391 } else if(arg[0] == '-') {
392 if (arg[1] == '\0') {
394 fprintf(stderr, "error: multiple input files specified\n");
395 argument_errors = true;
399 } else if(strcmp(arg, "-pedantic") == 0) {
400 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
401 } else if(arg[1] == 'O' ||
405 strncmp(arg + 1, "std=", 4) == 0) {
406 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
408 fprintf(stderr, "error: unknown argument '%s'\n", arg);
409 argument_errors = true;
413 fprintf(stderr, "error: multiple input files specified\n");
414 argument_errors = true;
422 return !argument_errors;
424 if(argument_errors) {
439 char outnamebuf[4096];
440 if(outname == NULL) {
451 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
452 outname = outnamebuf;
454 case CompileAssemble:
455 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
456 outname = outnamebuf;
459 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
461 outname = outnamebuf;
463 case CompileAssembleLink:
469 if(outname != NULL) {
470 if(strcmp(outname, "-") == 0) {
473 out = fopen(outname, "w");
475 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
484 fprintf(stderr, "%s: no input files\n", argv[0]);
486 } else if(strcmp(input, "-") == 0) {
490 in = fopen(input, "r");
492 fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
497 if(mode == LexTest) {
503 FILE *preprocessed_in = preprocess(in, input);
504 translation_unit_t *const unit = do_parsing(preprocessed_in, input);
505 pclose(preprocessed_in);
509 if(mode == PrintAst) {
510 type_set_output(out);
515 if(mode == PrintFluffy) {
516 type_set_output(out);
518 write_fluffy_decls(out, unit);
522 create_firm_prog(unit);
524 if(mode == ParseOnly) {
529 char asm_tempfile[1024];
530 if(mode == CompileDump) {
532 firm_be_opt.selection = BE_NONE;
533 } else if(mode == Compile) {
537 = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
539 gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
541 if(mode == CompileDump) {
543 ident *id = new_id_from_str(dumpfunction);
544 ir_graph *irg = NULL;
545 int n_irgs = get_irp_n_irgs();
546 for(int i = 0; i < n_irgs; ++i) {
547 ir_graph *tirg = get_irp_irg(i);
548 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
556 fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
560 dump_ir_block_graph_file(irg, out);
567 /* assemble assembler and create object file */
568 char obj_tfile[1024];
569 if(mode == CompileAssemble || mode == CompileAssembleLink) {
570 const char *obj_outfile;
571 if(mode == CompileAssemble) {
573 obj_outfile = outname;
576 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
578 obj_outfile = obj_tfile;
581 assemble(obj_outfile, asm_tempfile);
584 /* link object file */
585 if(mode == CompileAssembleLink) {
586 do_link(outname, obj_tfile);
589 obstack_free(&cppflags_obst, NULL);