17 #include <libfirm/firm.h>
18 #include <libfirm/be.h>
22 #include "type_hash.h"
25 #include "driver/firm_cmdline.h"
26 #include "adt/error.h"
27 #include "write_fluffy.h"
28 #include "driver/firm_opt.h"
31 #define PREPROCESSOR "cpp -std=c99 -U__WCHAR_TYPE__ -D__WCHAR_TYPE__=int"
39 #define ASSEMBLER "as"
43 /* remap some names */
44 #define popen(cmd, mode) _popen(cmd, mode)
45 #define pclose(file) _pclose(file)
50 static struct obstack cppflags_obst;
52 static void initialize_firm(void)
57 dump_keepalive_edges(1);
60 static void dump(ir_graph *irg, const char *suffix)
63 dump_ir_block_graph(irg, suffix);
67 static void get_output_name(char *buf, size_t buflen, const char *inputname,
70 size_t last_dot = 0xffffffff;
73 if(inputname == NULL) {
74 snprintf(buf, buflen, "a%s", newext);
78 for(const char *c = inputname; *c != 0; ++c) {
83 if(last_dot == 0xffffffff)
86 if(last_dot >= buflen)
87 panic("filename too long");
88 memcpy(buf, inputname, last_dot);
90 size_t extlen = strlen(newext) + 1;
91 if(extlen + last_dot >= buflen)
92 panic("filename too long");
93 memcpy(buf+last_dot, newext, extlen);
96 static translation_unit_t *do_parsing(FILE *const in, const char *const input)
98 lexer_open_stream(in, input);
99 translation_unit_t *unit = parse();
103 static void lextest(FILE *in, const char *fname)
105 lexer_open_stream(in, fname);
108 lexer_next_preprocessing_token();
109 print_token(stdout, &lexer_token);
111 } while(lexer_token.type != T_EOF);
114 static FILE* preprocess(FILE* in, const char *fname)
117 const char *flags = obstack_finish(&cppflags_obst);
120 snprintf(buf, sizeof(buf), PREPROCESSOR " %s %s", flags, fname);
122 /* read from stdin */
123 snprintf(buf, sizeof(buf), PREPROCESSOR " %s -", flags);
129 FILE* f = popen(buf, "r");
131 fprintf(stderr, "invoking preprocessor failed\n");
137 static void do_link(const char *out, const char *in)
141 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
145 int err = system(buf);
147 fprintf(stderr, "linker reported an error\n");
152 static void assemble(const char *out, const char *in)
156 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
161 int err = system(buf);
163 fprintf(stderr, "assembler reported an error\n");
168 static const char *try_dir(const char *dir)
172 if(access(dir, R_OK | W_OK | X_OK) == 0)
177 static const char *get_tempdir(void)
179 static const char *tmpdir = NULL;
185 tmpdir = try_dir(getenv("TMPDIR"));
187 tmpdir = try_dir(getenv("TMP"));
189 tmpdir = try_dir(getenv("TEMP"));
193 tmpdir = try_dir(P_tmpdir);
197 tmpdir = try_dir("/var/tmp");
199 tmpdir = try_dir("/usr/tmp");
201 tmpdir = try_dir("/tmp");
210 * an own version of tmpnam, which: writes in a buffer, appends a user specified
211 * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
213 static FILE *make_temp_file(char *buffer, size_t buflen,
214 const char *prefix, const char *suffix)
216 const char *tempdir = get_tempdir();
218 /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
222 snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
224 int fd = mkstemp(buffer);
226 fprintf(stderr, "couldn't create temporary file: %s\n",
230 FILE *out = fdopen(fd, "w");
232 fprintf(stderr, "couldn't create temporary file FILE*\n");
240 * Do the necessary lowering for compound parameters.
242 void lower_compound_params(void)
244 lower_params_t params;
246 params.def_ptr_alignment = 4;
247 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
248 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
249 params.find_pointer_type = NULL;
250 params.ret_compound_in_regs = NULL;
251 lower_calls_with_compounds(¶ms);
254 static void create_firm_prog(translation_unit_t *unit)
256 translation_unit_to_firm(unit);
258 int n_irgs = get_irp_n_irgs();
259 for(int i = 0; i < n_irgs; ++i) {
260 ir_graph *const irg = get_irp_irg(i);
264 lower_compound_params();
267 for(int i = 0; i < n_irgs; ++i) {
268 ir_graph *const irg = get_irp_irg(i);
273 typedef enum compile_mode_t {
284 static void usage(const char *argv0)
286 fprintf(stderr, "Usage %s input [-o output] [-c]\n", argv0);
289 int main(int argc, char **argv)
302 const char *input = NULL;
303 const char *outname = NULL;
304 const char *dumpfunction = NULL;
305 compile_mode_t mode = CompileAssembleLink;
307 obstack_init(&cppflags_obst);
309 #define GET_ARG_AFTER(def, args) \
310 def = &arg[sizeof(args)-1]; \
311 if(def[0] == '\0') { \
314 fprintf(stderr, "error: expected argument after '" args "'\n"); \
315 argument_errors = true; \
319 if(def[0] == '-' && def[1] != '\0') { \
320 fprintf(stderr, "error: expected argument after '" args "'\n"); \
321 argument_errors = true; \
326 bool help_displayed = false;
327 bool argument_errors = false;
328 for(int i = 1; i < argc; ++i) {
329 const char *arg = argv[i];
330 if(strncmp(arg, "-o", 2) == 0) {
331 GET_ARG_AFTER(outname, "-o");
332 } else if(strcmp(arg, "-c") == 0) {
333 mode = CompileAssemble;
334 } else if(strcmp(arg, "-S") == 0) {
336 } else if(strcmp(arg, "--lextest") == 0) {
338 } else if(strcmp(arg, "--print-ast") == 0) {
340 } else if(strcmp(arg, "--print-fluffy") == 0) {
342 } else if(strcmp(arg, "-fsyntax-only") == 0) {
344 } else if(strncmp(arg, "-I", 2) == 0) {
346 GET_ARG_AFTER(opt, "-I");
347 obstack_printf(&cppflags_obst, " -I%s", opt);
348 } else if(strncmp(arg, "-D", 2) == 0) {
350 GET_ARG_AFTER(opt, "-D");
351 obstack_printf(&cppflags_obst, " -D%s", opt);
352 } else if(strcmp(arg, "--dump") == 0) {
354 } else if(strcmp(arg, "--dump-function") == 0) {
357 fprintf(stderr, "error: "
358 "expected argument after '--dump-function'\n");
359 argument_errors = true;
362 dumpfunction = argv[i];
364 } else if(strcmp(arg, "-v") == 0) {
366 } else if(arg[0] == '-' && arg[1] == 'f') {
368 GET_ARG_AFTER(opt, "-f");
369 int res = firm_option(opt);
371 fprintf(stderr, "error: unknown Firm option '-f %s'\n", opt);
372 argument_errors = true;
374 } else if (res == -1) {
375 help_displayed = true;
377 } else if(arg[0] == '-' && arg[1] == 'b') {
379 GET_ARG_AFTER(opt, "-b");
380 int res = firm_be_option(opt);
382 fprintf(stderr, "error: unknown Firm backend option '-b %s'\n",
384 argument_errors = true;
385 } else if (res == -1) {
386 help_displayed = true;
388 } else if(arg[0] == '-') {
389 if (arg[1] == '\0') {
391 fprintf(stderr, "error: multiple input files specified\n");
392 argument_errors = true;
396 } else if(strcmp(arg, "-pedantic") == 0) {
397 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
398 } else if(arg[1] == 'O' ||
402 strncmp(arg + 1, "std=", 4) == 0) {
403 fprintf(stderr, "warning: ignoring gcc option '%s'\n", arg);
405 fprintf(stderr, "error: unknown argument '%s'\n", arg);
406 argument_errors = true;
410 fprintf(stderr, "error: multiple input files specified\n");
411 argument_errors = true;
419 return !argument_errors;
421 if(argument_errors) {
427 char outnamebuf[4096];
428 if(outname == NULL) {
439 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
440 outname = outnamebuf;
442 case CompileAssemble:
443 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
444 outname = outnamebuf;
447 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
449 outname = outnamebuf;
451 case CompileAssembleLink:
457 if(outname != NULL) {
458 if(strcmp(outname, "-") == 0) {
461 out = fopen(outname, "w");
463 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
472 fprintf(stderr, "%s: no input files\n", argv[0]);
474 } else if(strcmp(input, "-") == 0) {
478 in = fopen(input, "r");
480 fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
485 if(mode == LexTest) {
491 FILE *preprocessed_in = preprocess(in, input);
492 translation_unit_t *const unit = do_parsing(preprocessed_in, input);
493 pclose(preprocessed_in);
497 if(mode == PrintAst) {
498 type_set_output(out);
503 if(mode == PrintFluffy) {
504 type_set_output(out);
506 write_fluffy_decls(out, unit);
510 create_firm_prog(unit);
512 if(mode == ParseOnly) {
517 char asm_tempfile[1024];
518 if(mode == CompileDump) {
520 firm_be_opt.selection = BE_NONE;
521 } else if(mode == Compile) {
525 = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
527 gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
529 if(mode == CompileDump) {
531 ident *id = new_id_from_str(dumpfunction);
532 ir_graph *irg = NULL;
533 int n_irgs = get_irp_n_irgs();
534 for(int i = 0; i < n_irgs; ++i) {
535 ir_graph *tirg = get_irp_irg(i);
536 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
544 fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
548 dump_ir_block_graph_file(irg, out);
555 /* assemble assembler and create object file */
556 char obj_tfile[1024];
557 if(mode == CompileAssemble || mode == CompileAssembleLink) {
558 const char *obj_outfile;
559 if(mode == CompileAssemble) {
561 obj_outfile = outname;
564 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
566 obj_outfile = obj_tfile;
569 assemble(obj_outfile, asm_tempfile);
572 /* link object file */
573 if(mode == CompileAssembleLink) {
574 do_link(outname, obj_tfile);
577 obstack_free(&cppflags_obst, NULL);