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)
51 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)
119 snprintf(buf, sizeof(buf), PREPROCESSOR " %s", fname);
121 /* read from stdin */
122 snprintf(buf, sizeof(buf), PREPROCESSOR " -");
128 FILE* f = popen(buf, "r");
130 fprintf(stderr, "invoking preprocessor failed\n");
136 static void do_link(const char *out, const char *in)
140 snprintf(buf, sizeof(buf), "%s %s -o %s", LINKER, in, out);
144 int err = system(buf);
146 fprintf(stderr, "linker reported an error\n");
151 static void assemble(const char *out, const char *in)
155 snprintf(buf, sizeof(buf), "%s %s -o %s", ASSEMBLER, in, out);
160 int err = system(buf);
162 fprintf(stderr, "assembler reported an error\n");
167 static const char *try_dir(const char *dir)
171 if(access(dir, R_OK | W_OK | X_OK) == 0)
176 static const char *get_tempdir(void)
178 static const char *tmpdir = NULL;
184 tmpdir = try_dir(getenv("TMPDIR"));
186 tmpdir = try_dir(getenv("TMP"));
188 tmpdir = try_dir(getenv("TEMP"));
192 tmpdir = try_dir(P_tmpdir);
196 tmpdir = try_dir("/var/tmp");
198 tmpdir = try_dir("/usr/tmp");
200 tmpdir = try_dir("/tmp");
209 * an own version of tmpnam, which: writes in a buffer, appends a user specified
210 * suffix, emits no warnings during linking (like glibc/gnu ld do for tmpnam)...
212 static FILE *make_temp_file(char *buffer, size_t buflen,
213 const char *prefix, const char *suffix)
215 const char *tempdir = get_tempdir();
217 /* oh well... mkstemp doesn't accept a suffix after XXXXXX... */
221 snprintf(buffer, buflen, "%s/%sXXXXXX%s", tempdir, prefix, suffix);
223 int fd = mkstemp(buffer);
225 fprintf(stderr, "couldn't create temporary file: %s\n",
229 FILE *out = fdopen(fd, "w");
231 fprintf(stderr, "couldn't create temporary file FILE*\n");
239 * Do the necessary lowering for compound parameters.
241 void lower_compound_params(void)
243 lower_params_t params;
245 params.def_ptr_alignment = 4;
246 params.flags = LF_COMPOUND_RETURN | LF_RETURN_HIDDEN;
247 params.hidden_params = ADD_HIDDEN_ALWAYS_IN_FRONT;
248 params.find_pointer_type = NULL;
249 params.ret_compound_in_regs = NULL;
250 lower_calls_with_compounds(¶ms);
253 static void create_firm_prog(translation_unit_t *unit)
255 translation_unit_to_firm(unit);
257 //dump_globals_as_text(dump_verbosity_max, "-globals");
259 int n_irgs = get_irp_n_irgs();
260 for(int i = 0; i < n_irgs; ++i) {
261 ir_graph *const irg = get_irp_irg(i);
265 lower_compound_params();
268 for(int i = 0; i < n_irgs; ++i) {
269 ir_graph *const irg = get_irp_irg(i);
274 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 for(int i = 1; i < argc; ++i) {
308 const char *arg = argv[i];
309 if(strcmp(arg, "-o") == 0) {
316 } else if(strcmp(arg, "-c") == 0) {
317 mode = CompileAssemble;
318 } else if(strcmp(arg, "-S") == 0) {
320 } else if(strcmp(arg, "--lextest") == 0) {
322 } else if(strcmp(arg, "--print-ast") == 0) {
324 } else if(strcmp(arg, "--print-fluffy") == 0) {
326 } else if(strcmp(arg, "--dump") == 0) {
328 } else if(strcmp(arg, "--dump-function") == 0) {
334 dumpfunction = argv[i];
336 } else if(strcmp(arg, "-v") == 0) {
338 } else if(arg[0] == '-' && arg[1] == 'f') {
339 const char *opt = &arg[2];
352 int res = firm_option(opt);
354 fprintf(stderr, "Error: unknown Firm option %s\n", opt);
357 } else if (res == -1) /* help option */
359 } else if(arg[0] == '-' && arg[1] == 'b') {
360 const char *opt = &arg[2];
373 int res = firm_be_option(opt);
375 fprintf(stderr, "Error: unknown Firm backend option %s\n", opt);
378 } else if (res == -1) /* help option */
380 } else if(arg[0] == '-') {
381 if (arg[1] == '\0') {
383 } else if (arg[1] == 'D' ||
388 strncmp(arg + 1, "std=", 4) == 0) {
389 fprintf(stderr, "Warning: Ignoring option '%s'\n", arg);
396 fprintf(stderr, "Error: multiple input files specified\n");
406 char outnamebuf[4096];
407 if(outname == NULL) {
414 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".s");
415 outname = outnamebuf;
417 case CompileAssemble:
418 get_output_name(outnamebuf, sizeof(outnamebuf), input, ".o");
419 outname = outnamebuf;
422 get_output_name(outnamebuf, sizeof(outnamebuf), dumpfunction,
424 outname = outnamebuf;
426 case CompileAssembleLink:
430 assert(outname != NULL);
433 if(strcmp(outname, "-") == 0) {
436 out = fopen(outname, "w");
438 fprintf(stderr, "Couldn't open '%s' for writing: %s\n", outname,
446 fprintf(stderr, "%s: no input files\n", argv[0]);
448 } else if(strcmp(input, "-") == 0) {
452 in = fopen(input, "r");
454 fprintf(stderr, "Couldn't open '%s': %s\n", input, strerror(errno));
459 if(mode == LexTest) {
465 FILE *preprocessed_in = preprocess(in, input);
466 translation_unit_t *const unit = do_parsing(preprocessed_in, input);
467 pclose(preprocessed_in);
471 if(mode == PrintAst) {
476 if(mode == PrintFluffy) {
477 write_fluffy_decls(out, unit);
480 create_firm_prog(unit);
483 char asm_tempfile[1024];
484 if(mode == CompileDump) {
486 firm_be_opt.selection = BE_NONE;
487 } else if(mode == Compile) {
491 = make_temp_file(asm_tempfile, sizeof(asm_tempfile), "cc", ".s");
493 gen_firm_finish(asm_out, input, /*c_mode=*/1, /*firm_const_exists=*/0);
495 if(mode == CompileDump) {
497 ident *id = new_id_from_str(dumpfunction);
498 ir_graph *irg = NULL;
499 int n_irgs = get_irp_n_irgs();
500 for(int i = 0; i < n_irgs; ++i) {
501 ir_graph *tirg = get_irp_irg(i);
502 ident *irg_id = get_entity_ident(get_irg_entity(tirg));
510 fprintf(stderr, "No graph for function '%s' found\n", dumpfunction);
514 dump_ir_block_graph_file(irg, out);
521 /* assemble assembler and create object file */
522 char obj_tfile[1024];
523 if(mode == CompileAssemble || mode == CompileAssembleLink) {
524 const char *obj_outfile;
525 if(mode == CompileAssemble) {
527 obj_outfile = outname;
530 = make_temp_file(obj_tfile, sizeof(obj_tfile), "cc", ".o");
532 obj_outfile = obj_tfile;
535 assemble(obj_outfile, asm_tempfile);
538 /* link object file */
539 if(mode == CompileAssembleLink) {
540 do_link(outname, obj_tfile);